1 /*
2  *		Mars-Semi MR97311A library
3  *		Copyright (C) 2005 <bradlch@hotmail.com>
4  *
5  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 
24 #define MODULE_NAME "mars"
25 
26 #include "gspca.h"
27 #include "jpeg.h"
28 
29 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
30 MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
31 MODULE_LICENSE("GPL");
32 
33 /* controls */
34 enum e_ctrl {
35 	BRIGHTNESS,
36 	COLORS,
37 	GAMMA,
38 	SHARPNESS,
39 	ILLUM_TOP,
40 	ILLUM_BOT,
41 	NCTRLS		/* number of controls */
42 };
43 
44 /* specific webcam descriptor */
45 struct sd {
46 	struct gspca_dev gspca_dev;	/* !! must be the first item */
47 
48 	struct gspca_ctrl ctrls[NCTRLS];
49 
50 	u8 quality;
51 #define QUALITY_MIN 40
52 #define QUALITY_MAX 70
53 #define QUALITY_DEF 50
54 
55 	u8 jpeg_hdr[JPEG_HDR_SZ];
56 };
57 
58 /* V4L2 controls supported by the driver */
59 static void setbrightness(struct gspca_dev *gspca_dev);
60 static void setcolors(struct gspca_dev *gspca_dev);
61 static void setgamma(struct gspca_dev *gspca_dev);
62 static void setsharpness(struct gspca_dev *gspca_dev);
63 static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val);
64 static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val);
65 
66 static const struct ctrl sd_ctrls[NCTRLS] = {
67 [BRIGHTNESS] = {
68 	    {
69 		.id      = V4L2_CID_BRIGHTNESS,
70 		.type    = V4L2_CTRL_TYPE_INTEGER,
71 		.name    = "Brightness",
72 		.minimum = 0,
73 		.maximum = 30,
74 		.step    = 1,
75 		.default_value = 15,
76 	    },
77 	    .set_control = setbrightness
78 	},
79 [COLORS] = {
80 	    {
81 		.id      = V4L2_CID_SATURATION,
82 		.type    = V4L2_CTRL_TYPE_INTEGER,
83 		.name    = "Color",
84 		.minimum = 1,
85 		.maximum = 255,
86 		.step    = 1,
87 		.default_value = 200,
88 	    },
89 	    .set_control = setcolors
90 	},
91 [GAMMA] = {
92 	    {
93 		.id      = V4L2_CID_GAMMA,
94 		.type    = V4L2_CTRL_TYPE_INTEGER,
95 		.name    = "Gamma",
96 		.minimum = 0,
97 		.maximum = 3,
98 		.step    = 1,
99 		.default_value = 1,
100 	    },
101 	    .set_control = setgamma
102 	},
103 [SHARPNESS] = {
104 	    {
105 		.id	 = V4L2_CID_SHARPNESS,
106 		.type    = V4L2_CTRL_TYPE_INTEGER,
107 		.name    = "Sharpness",
108 		.minimum = 0,
109 		.maximum = 2,
110 		.step    = 1,
111 		.default_value = 1,
112 	    },
113 	    .set_control = setsharpness
114 	},
115 [ILLUM_TOP] = {
116 	    {
117 		.id	 = V4L2_CID_ILLUMINATORS_1,
118 		.type    = V4L2_CTRL_TYPE_BOOLEAN,
119 		.name    = "Top illuminator",
120 		.minimum = 0,
121 		.maximum = 1,
122 		.step    = 1,
123 		.default_value = 0,
124 		.flags = V4L2_CTRL_FLAG_UPDATE,
125 	    },
126 	    .set = sd_setilluminator1
127 	},
128 [ILLUM_BOT] = {
129 	    {
130 		.id	 = V4L2_CID_ILLUMINATORS_2,
131 		.type    = V4L2_CTRL_TYPE_BOOLEAN,
132 		.name    = "Bottom illuminator",
133 		.minimum = 0,
134 		.maximum = 1,
135 		.step    = 1,
136 		.default_value = 0,
137 		.flags = V4L2_CTRL_FLAG_UPDATE,
138 	    },
139 	    .set = sd_setilluminator2
140 	},
141 };
142 
143 static const struct v4l2_pix_format vga_mode[] = {
144 	{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
145 		.bytesperline = 320,
146 		.sizeimage = 320 * 240 * 3 / 8 + 590,
147 		.colorspace = V4L2_COLORSPACE_JPEG,
148 		.priv = 2},
149 	{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
150 		.bytesperline = 640,
151 		.sizeimage = 640 * 480 * 3 / 8 + 590,
152 		.colorspace = V4L2_COLORSPACE_JPEG,
153 		.priv = 1},
154 };
155 
156 static const __u8 mi_data[0x20] = {
157 /*	 01    02   03     04    05    06    07    08 */
158 	0x48, 0x22, 0x01, 0x47, 0x10, 0x00, 0x00, 0x00,
159 /*	 09    0a   0b     0c    0d    0e    0f    10 */
160 	0x00, 0x01, 0x30, 0x01, 0x30, 0x01, 0x30, 0x01,
161 /*	 11    12   13     14    15    16    17    18 */
162 	0x30, 0x00, 0x04, 0x00, 0x06, 0x01, 0xe2, 0x02,
163 /*	 19    1a   1b     1c    1d    1e    1f    20 */
164 	0x82, 0x00, 0x20, 0x17, 0x80, 0x08, 0x0c, 0x00
165 };
166 
167 /* write <len> bytes from gspca_dev->usb_buf */
reg_w(struct gspca_dev * gspca_dev,int len)168 static void reg_w(struct gspca_dev *gspca_dev,
169 		 int len)
170 {
171 	int alen, ret;
172 
173 	if (gspca_dev->usb_err < 0)
174 		return;
175 
176 	ret = usb_bulk_msg(gspca_dev->dev,
177 			usb_sndbulkpipe(gspca_dev->dev, 4),
178 			gspca_dev->usb_buf,
179 			len,
180 			&alen,
181 			500);	/* timeout in milliseconds */
182 	if (ret < 0) {
183 		pr_err("reg write [%02x] error %d\n",
184 		       gspca_dev->usb_buf[0], ret);
185 		gspca_dev->usb_err = ret;
186 	}
187 }
188 
mi_w(struct gspca_dev * gspca_dev,u8 addr,u8 value)189 static void mi_w(struct gspca_dev *gspca_dev,
190 		 u8 addr,
191 		 u8 value)
192 {
193 	gspca_dev->usb_buf[0] = 0x1f;
194 	gspca_dev->usb_buf[1] = 0;			/* control byte */
195 	gspca_dev->usb_buf[2] = addr;
196 	gspca_dev->usb_buf[3] = value;
197 
198 	reg_w(gspca_dev, 4);
199 }
200 
setbrightness(struct gspca_dev * gspca_dev)201 static void setbrightness(struct gspca_dev *gspca_dev)
202 {
203 	struct sd *sd = (struct sd *) gspca_dev;
204 
205 	gspca_dev->usb_buf[0] = 0x61;
206 	gspca_dev->usb_buf[1] = sd->ctrls[BRIGHTNESS].val;
207 	reg_w(gspca_dev, 2);
208 }
209 
setcolors(struct gspca_dev * gspca_dev)210 static void setcolors(struct gspca_dev *gspca_dev)
211 {
212 	struct sd *sd = (struct sd *) gspca_dev;
213 	s16 val;
214 
215 	val = sd->ctrls[COLORS].val;
216 	gspca_dev->usb_buf[0] = 0x5f;
217 	gspca_dev->usb_buf[1] = val << 3;
218 	gspca_dev->usb_buf[2] = ((val >> 2) & 0xf8) | 0x04;
219 	reg_w(gspca_dev, 3);
220 }
221 
setgamma(struct gspca_dev * gspca_dev)222 static void setgamma(struct gspca_dev *gspca_dev)
223 {
224 	struct sd *sd = (struct sd *) gspca_dev;
225 
226 	gspca_dev->usb_buf[0] = 0x06;
227 	gspca_dev->usb_buf[1] = sd->ctrls[GAMMA].val * 0x40;
228 	reg_w(gspca_dev, 2);
229 }
230 
setsharpness(struct gspca_dev * gspca_dev)231 static void setsharpness(struct gspca_dev *gspca_dev)
232 {
233 	struct sd *sd = (struct sd *) gspca_dev;
234 
235 	gspca_dev->usb_buf[0] = 0x67;
236 	gspca_dev->usb_buf[1] = sd->ctrls[SHARPNESS].val * 4 + 3;
237 	reg_w(gspca_dev, 2);
238 }
239 
setilluminators(struct gspca_dev * gspca_dev)240 static void setilluminators(struct gspca_dev *gspca_dev)
241 {
242 	struct sd *sd = (struct sd *) gspca_dev;
243 
244 	gspca_dev->usb_buf[0] = 0x22;
245 	if (sd->ctrls[ILLUM_TOP].val)
246 		gspca_dev->usb_buf[1] = 0x76;
247 	else if (sd->ctrls[ILLUM_BOT].val)
248 		gspca_dev->usb_buf[1] = 0x7a;
249 	else
250 		gspca_dev->usb_buf[1] = 0x7e;
251 	reg_w(gspca_dev, 2);
252 }
253 
254 /* this function is called at probe time */
sd_config(struct gspca_dev * gspca_dev,const struct usb_device_id * id)255 static int sd_config(struct gspca_dev *gspca_dev,
256 			const struct usb_device_id *id)
257 {
258 	struct sd *sd = (struct sd *) gspca_dev;
259 	struct cam *cam;
260 
261 	cam = &gspca_dev->cam;
262 	cam->cam_mode = vga_mode;
263 	cam->nmodes = ARRAY_SIZE(vga_mode);
264 	cam->ctrls = sd->ctrls;
265 	sd->quality = QUALITY_DEF;
266 	return 0;
267 }
268 
269 /* this function is called at probe and resume time */
sd_init(struct gspca_dev * gspca_dev)270 static int sd_init(struct gspca_dev *gspca_dev)
271 {
272 	gspca_dev->ctrl_inac = (1 << ILLUM_TOP) | (1 << ILLUM_BOT);
273 	return 0;
274 }
275 
sd_start(struct gspca_dev * gspca_dev)276 static int sd_start(struct gspca_dev *gspca_dev)
277 {
278 	struct sd *sd = (struct sd *) gspca_dev;
279 	u8 *data;
280 	int i;
281 
282 	/* create the JPEG header */
283 	jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
284 			0x21);		/* JPEG 422 */
285 	jpeg_set_qual(sd->jpeg_hdr, sd->quality);
286 
287 	data = gspca_dev->usb_buf;
288 
289 	data[0] = 0x01;		/* address */
290 	data[1] = 0x01;
291 	reg_w(gspca_dev, 2);
292 
293 	/*
294 	   Initialize the MR97113 chip register
295 	 */
296 	data[0] = 0x00;		/* address */
297 	data[1] = 0x0c | 0x01;	/* reg 0 */
298 	data[2] = 0x01;		/* reg 1 */
299 	data[3] = gspca_dev->width / 8;		/* h_size , reg 2 */
300 	data[4] = gspca_dev->height / 8;	/* v_size , reg 3 */
301 	data[5] = 0x30;		/* reg 4, MI, PAS5101 :
302 				 *	0x30 for 24mhz , 0x28 for 12mhz */
303 	data[6] = 0x02;		/* reg 5, H start - was 0x04 */
304 	data[7] = sd->ctrls[GAMMA].val * 0x40;	/* reg 0x06: gamma */
305 	data[8] = 0x01;		/* reg 7, V start - was 0x03 */
306 /*	if (h_size == 320 ) */
307 /*		data[9]= 0x56;	 * reg 8, 24MHz, 2:1 scale down */
308 /*	else */
309 	data[9] = 0x52;		/* reg 8, 24MHz, no scale down */
310 /*jfm: from win trace*/
311 	data[10] = 0x18;
312 
313 	reg_w(gspca_dev, 11);
314 
315 	data[0] = 0x23;		/* address */
316 	data[1] = 0x09;		/* reg 35, append frame header */
317 
318 	reg_w(gspca_dev, 2);
319 
320 	data[0] = 0x3c;		/* address */
321 /*	if (gspca_dev->width == 1280) */
322 /*		data[1] = 200;	 * reg 60, pc-cam frame size
323 				 *	(unit: 4KB) 800KB */
324 /*	else */
325 	data[1] = 50;		/* 50 reg 60, pc-cam frame size
326 				 *	(unit: 4KB) 200KB */
327 	reg_w(gspca_dev, 2);
328 
329 	/* auto dark-gain */
330 	data[0] = 0x5e;		/* address */
331 	data[1] = 0;		/* reg 94, Y Gain (auto) */
332 /*jfm: from win trace*/
333 				/* reg 0x5f/0x60 (LE) = saturation */
334 				/* h (60): xxxx x100
335 				 * l (5f): xxxx x000 */
336 	data[2] = sd->ctrls[COLORS].val << 3;
337 	data[3] = ((sd->ctrls[COLORS].val >> 2) & 0xf8) | 0x04;
338 	data[4] = sd->ctrls[BRIGHTNESS].val; /* reg 0x61 = brightness */
339 	data[5] = 0x00;
340 
341 	reg_w(gspca_dev, 6);
342 
343 	data[0] = 0x67;
344 /*jfm: from win trace*/
345 	data[1] = sd->ctrls[SHARPNESS].val * 4 + 3;
346 	data[2] = 0x14;
347 	reg_w(gspca_dev, 3);
348 
349 	data[0] = 0x69;
350 	data[1] = 0x2f;
351 	data[2] = 0x28;
352 	data[3] = 0x42;
353 	reg_w(gspca_dev, 4);
354 
355 	data[0] = 0x63;
356 	data[1] = 0x07;
357 	reg_w(gspca_dev, 2);
358 /*jfm: win trace - many writes here to reg 0x64*/
359 
360 	/* initialize the MI sensor */
361 	for (i = 0; i < sizeof mi_data; i++)
362 		mi_w(gspca_dev, i + 1, mi_data[i]);
363 
364 	data[0] = 0x00;
365 	data[1] = 0x4d;		/* ISOC transferring enable... */
366 	reg_w(gspca_dev, 2);
367 
368 	gspca_dev->ctrl_inac = 0; /* activate the illuminator controls */
369 	return gspca_dev->usb_err;
370 }
371 
sd_stopN(struct gspca_dev * gspca_dev)372 static void sd_stopN(struct gspca_dev *gspca_dev)
373 {
374 	struct sd *sd = (struct sd *) gspca_dev;
375 
376 	gspca_dev->ctrl_inac = (1 << ILLUM_TOP) | (1 << ILLUM_BOT);
377 	if (sd->ctrls[ILLUM_TOP].val || sd->ctrls[ILLUM_BOT].val) {
378 		sd->ctrls[ILLUM_TOP].val = 0;
379 		sd->ctrls[ILLUM_BOT].val = 0;
380 		setilluminators(gspca_dev);
381 		msleep(20);
382 	}
383 
384 	gspca_dev->usb_buf[0] = 1;
385 	gspca_dev->usb_buf[1] = 0;
386 	reg_w(gspca_dev, 2);
387 }
388 
sd_pkt_scan(struct gspca_dev * gspca_dev,u8 * data,int len)389 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
390 			u8 *data,			/* isoc packet */
391 			int len)			/* iso packet length */
392 {
393 	struct sd *sd = (struct sd *) gspca_dev;
394 	int p;
395 
396 	if (len < 6) {
397 /*		gspca_dev->last_packet_type = DISCARD_PACKET; */
398 		return;
399 	}
400 	for (p = 0; p < len - 6; p++) {
401 		if (data[0 + p] == 0xff
402 		    && data[1 + p] == 0xff
403 		    && data[2 + p] == 0x00
404 		    && data[3 + p] == 0xff
405 		    && data[4 + p] == 0x96) {
406 			if (data[5 + p] == 0x64
407 			    || data[5 + p] == 0x65
408 			    || data[5 + p] == 0x66
409 			    || data[5 + p] == 0x67) {
410 				PDEBUG(D_PACK, "sof offset: %d len: %d",
411 					p, len);
412 				gspca_frame_add(gspca_dev, LAST_PACKET,
413 						data, p);
414 
415 				/* put the JPEG header */
416 				gspca_frame_add(gspca_dev, FIRST_PACKET,
417 					sd->jpeg_hdr, JPEG_HDR_SZ);
418 				data += p + 16;
419 				len -= p + 16;
420 				break;
421 			}
422 		}
423 	}
424 	gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
425 }
426 
sd_setilluminator1(struct gspca_dev * gspca_dev,__s32 val)427 static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val)
428 {
429 	struct sd *sd = (struct sd *) gspca_dev;
430 
431 	/* only one illuminator may be on */
432 	sd->ctrls[ILLUM_TOP].val = val;
433 	if (val)
434 		sd->ctrls[ILLUM_BOT].val = 0;
435 	setilluminators(gspca_dev);
436 	return gspca_dev->usb_err;
437 }
438 
sd_setilluminator2(struct gspca_dev * gspca_dev,__s32 val)439 static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val)
440 {
441 	struct sd *sd = (struct sd *) gspca_dev;
442 
443 	/* only one illuminator may be on */
444 	sd->ctrls[ILLUM_BOT].val = val;
445 	if (val)
446 		sd->ctrls[ILLUM_TOP].val = 0;
447 	setilluminators(gspca_dev);
448 	return gspca_dev->usb_err;
449 }
450 
sd_set_jcomp(struct gspca_dev * gspca_dev,struct v4l2_jpegcompression * jcomp)451 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
452 			struct v4l2_jpegcompression *jcomp)
453 {
454 	struct sd *sd = (struct sd *) gspca_dev;
455 
456 	if (jcomp->quality < QUALITY_MIN)
457 		sd->quality = QUALITY_MIN;
458 	else if (jcomp->quality > QUALITY_MAX)
459 		sd->quality = QUALITY_MAX;
460 	else
461 		sd->quality = jcomp->quality;
462 	if (gspca_dev->streaming)
463 		jpeg_set_qual(sd->jpeg_hdr, sd->quality);
464 	return 0;
465 }
466 
sd_get_jcomp(struct gspca_dev * gspca_dev,struct v4l2_jpegcompression * jcomp)467 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
468 			struct v4l2_jpegcompression *jcomp)
469 {
470 	struct sd *sd = (struct sd *) gspca_dev;
471 
472 	memset(jcomp, 0, sizeof *jcomp);
473 	jcomp->quality = sd->quality;
474 	jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
475 			| V4L2_JPEG_MARKER_DQT;
476 	return 0;
477 }
478 
479 /* sub-driver description */
480 static const struct sd_desc sd_desc = {
481 	.name = MODULE_NAME,
482 	.ctrls = sd_ctrls,
483 	.nctrls = NCTRLS,
484 	.config = sd_config,
485 	.init = sd_init,
486 	.start = sd_start,
487 	.stopN = sd_stopN,
488 	.pkt_scan = sd_pkt_scan,
489 	.get_jcomp = sd_get_jcomp,
490 	.set_jcomp = sd_set_jcomp,
491 };
492 
493 /* -- module initialisation -- */
494 static const struct usb_device_id device_table[] = {
495 	{USB_DEVICE(0x093a, 0x050f)},
496 	{}
497 };
498 MODULE_DEVICE_TABLE(usb, device_table);
499 
500 /* -- device connect -- */
sd_probe(struct usb_interface * intf,const struct usb_device_id * id)501 static int sd_probe(struct usb_interface *intf,
502 			const struct usb_device_id *id)
503 {
504 	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
505 				THIS_MODULE);
506 }
507 
508 static struct usb_driver sd_driver = {
509 	.name = MODULE_NAME,
510 	.id_table = device_table,
511 	.probe = sd_probe,
512 	.disconnect = gspca_disconnect,
513 #ifdef CONFIG_PM
514 	.suspend = gspca_suspend,
515 	.resume = gspca_resume,
516 #endif
517 };
518 
519 module_usb_driver(sd_driver);
520