Merge branch 'for-linus' of git://git.infradead.org/users/sameo/mfd-2.6
[linux-btrfs-devel.git] / drivers / media / video / gspca / mars.c
blob0196209a948a879846fcbb660b11a0fe91feddc0
1 /*
2 * Mars-Semi MR97311A library
3 * Copyright (C) 2005 <bradlch@hotmail.com>
5 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
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.
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.
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
22 #define MODULE_NAME "mars"
24 #include "gspca.h"
25 #include "jpeg.h"
27 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
28 MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
29 MODULE_LICENSE("GPL");
31 /* controls */
32 enum e_ctrl {
33 BRIGHTNESS,
34 COLORS,
35 GAMMA,
36 SHARPNESS,
37 ILLUM_TOP,
38 ILLUM_BOT,
39 NCTRLS /* number of controls */
42 /* specific webcam descriptor */
43 struct sd {
44 struct gspca_dev gspca_dev; /* !! must be the first item */
46 struct gspca_ctrl ctrls[NCTRLS];
48 u8 quality;
49 #define QUALITY_MIN 40
50 #define QUALITY_MAX 70
51 #define QUALITY_DEF 50
53 u8 jpeg_hdr[JPEG_HDR_SZ];
56 /* V4L2 controls supported by the driver */
57 static void setbrightness(struct gspca_dev *gspca_dev);
58 static void setcolors(struct gspca_dev *gspca_dev);
59 static void setgamma(struct gspca_dev *gspca_dev);
60 static void setsharpness(struct gspca_dev *gspca_dev);
61 static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val);
62 static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val);
64 static const struct ctrl sd_ctrls[NCTRLS] = {
65 [BRIGHTNESS] = {
67 .id = V4L2_CID_BRIGHTNESS,
68 .type = V4L2_CTRL_TYPE_INTEGER,
69 .name = "Brightness",
70 .minimum = 0,
71 .maximum = 30,
72 .step = 1,
73 .default_value = 15,
75 .set_control = setbrightness
77 [COLORS] = {
79 .id = V4L2_CID_SATURATION,
80 .type = V4L2_CTRL_TYPE_INTEGER,
81 .name = "Color",
82 .minimum = 1,
83 .maximum = 255,
84 .step = 1,
85 .default_value = 200,
87 .set_control = setcolors
89 [GAMMA] = {
91 .id = V4L2_CID_GAMMA,
92 .type = V4L2_CTRL_TYPE_INTEGER,
93 .name = "Gamma",
94 .minimum = 0,
95 .maximum = 3,
96 .step = 1,
97 .default_value = 1,
99 .set_control = setgamma
101 [SHARPNESS] = {
103 .id = V4L2_CID_SHARPNESS,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Sharpness",
106 .minimum = 0,
107 .maximum = 2,
108 .step = 1,
109 .default_value = 1,
111 .set_control = setsharpness
113 [ILLUM_TOP] = {
115 .id = V4L2_CID_ILLUMINATORS_1,
116 .type = V4L2_CTRL_TYPE_BOOLEAN,
117 .name = "Top illuminator",
118 .minimum = 0,
119 .maximum = 1,
120 .step = 1,
121 .default_value = 0,
122 .flags = V4L2_CTRL_FLAG_UPDATE,
124 .set = sd_setilluminator1
126 [ILLUM_BOT] = {
128 .id = V4L2_CID_ILLUMINATORS_2,
129 .type = V4L2_CTRL_TYPE_BOOLEAN,
130 .name = "Bottom illuminator",
131 .minimum = 0,
132 .maximum = 1,
133 .step = 1,
134 .default_value = 0,
135 .flags = V4L2_CTRL_FLAG_UPDATE,
137 .set = sd_setilluminator2
141 static const struct v4l2_pix_format vga_mode[] = {
142 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
143 .bytesperline = 320,
144 .sizeimage = 320 * 240 * 3 / 8 + 590,
145 .colorspace = V4L2_COLORSPACE_JPEG,
146 .priv = 2},
147 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
148 .bytesperline = 640,
149 .sizeimage = 640 * 480 * 3 / 8 + 590,
150 .colorspace = V4L2_COLORSPACE_JPEG,
151 .priv = 1},
154 static const __u8 mi_data[0x20] = {
155 /* 01 02 03 04 05 06 07 08 */
156 0x48, 0x22, 0x01, 0x47, 0x10, 0x00, 0x00, 0x00,
157 /* 09 0a 0b 0c 0d 0e 0f 10 */
158 0x00, 0x01, 0x30, 0x01, 0x30, 0x01, 0x30, 0x01,
159 /* 11 12 13 14 15 16 17 18 */
160 0x30, 0x00, 0x04, 0x00, 0x06, 0x01, 0xe2, 0x02,
161 /* 19 1a 1b 1c 1d 1e 1f 20 */
162 0x82, 0x00, 0x20, 0x17, 0x80, 0x08, 0x0c, 0x00
165 /* write <len> bytes from gspca_dev->usb_buf */
166 static void reg_w(struct gspca_dev *gspca_dev,
167 int len)
169 int alen, ret;
171 if (gspca_dev->usb_err < 0)
172 return;
174 ret = usb_bulk_msg(gspca_dev->dev,
175 usb_sndbulkpipe(gspca_dev->dev, 4),
176 gspca_dev->usb_buf,
177 len,
178 &alen,
179 500); /* timeout in milliseconds */
180 if (ret < 0) {
181 err("reg write [%02x] error %d",
182 gspca_dev->usb_buf[0], ret);
183 gspca_dev->usb_err = ret;
187 static void mi_w(struct gspca_dev *gspca_dev,
188 u8 addr,
189 u8 value)
191 gspca_dev->usb_buf[0] = 0x1f;
192 gspca_dev->usb_buf[1] = 0; /* control byte */
193 gspca_dev->usb_buf[2] = addr;
194 gspca_dev->usb_buf[3] = value;
196 reg_w(gspca_dev, 4);
199 static void setbrightness(struct gspca_dev *gspca_dev)
201 struct sd *sd = (struct sd *) gspca_dev;
203 gspca_dev->usb_buf[0] = 0x61;
204 gspca_dev->usb_buf[1] = sd->ctrls[BRIGHTNESS].val;
205 reg_w(gspca_dev, 2);
208 static void setcolors(struct gspca_dev *gspca_dev)
210 struct sd *sd = (struct sd *) gspca_dev;
211 s16 val;
213 val = sd->ctrls[COLORS].val;
214 gspca_dev->usb_buf[0] = 0x5f;
215 gspca_dev->usb_buf[1] = val << 3;
216 gspca_dev->usb_buf[2] = ((val >> 2) & 0xf8) | 0x04;
217 reg_w(gspca_dev, 3);
220 static void setgamma(struct gspca_dev *gspca_dev)
222 struct sd *sd = (struct sd *) gspca_dev;
224 gspca_dev->usb_buf[0] = 0x06;
225 gspca_dev->usb_buf[1] = sd->ctrls[GAMMA].val * 0x40;
226 reg_w(gspca_dev, 2);
229 static void setsharpness(struct gspca_dev *gspca_dev)
231 struct sd *sd = (struct sd *) gspca_dev;
233 gspca_dev->usb_buf[0] = 0x67;
234 gspca_dev->usb_buf[1] = sd->ctrls[SHARPNESS].val * 4 + 3;
235 reg_w(gspca_dev, 2);
238 static void setilluminators(struct gspca_dev *gspca_dev)
240 struct sd *sd = (struct sd *) gspca_dev;
242 gspca_dev->usb_buf[0] = 0x22;
243 if (sd->ctrls[ILLUM_TOP].val)
244 gspca_dev->usb_buf[1] = 0x76;
245 else if (sd->ctrls[ILLUM_BOT].val)
246 gspca_dev->usb_buf[1] = 0x7a;
247 else
248 gspca_dev->usb_buf[1] = 0x7e;
249 reg_w(gspca_dev, 2);
252 /* this function is called at probe time */
253 static int sd_config(struct gspca_dev *gspca_dev,
254 const struct usb_device_id *id)
256 struct sd *sd = (struct sd *) gspca_dev;
257 struct cam *cam;
259 cam = &gspca_dev->cam;
260 cam->cam_mode = vga_mode;
261 cam->nmodes = ARRAY_SIZE(vga_mode);
262 cam->ctrls = sd->ctrls;
263 sd->quality = QUALITY_DEF;
264 gspca_dev->nbalt = 9; /* use the altsetting 08 */
265 return 0;
268 /* this function is called at probe and resume time */
269 static int sd_init(struct gspca_dev *gspca_dev)
271 gspca_dev->ctrl_inac = (1 << ILLUM_TOP) | (1 << ILLUM_BOT);
272 return 0;
275 static int sd_start(struct gspca_dev *gspca_dev)
277 struct sd *sd = (struct sd *) gspca_dev;
278 u8 *data;
279 int i;
281 /* create the JPEG header */
282 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
283 0x21); /* JPEG 422 */
284 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
286 data = gspca_dev->usb_buf;
288 data[0] = 0x01; /* address */
289 data[1] = 0x01;
290 reg_w(gspca_dev, 2);
293 Initialize the MR97113 chip register
295 data[0] = 0x00; /* address */
296 data[1] = 0x0c | 0x01; /* reg 0 */
297 data[2] = 0x01; /* reg 1 */
298 data[3] = gspca_dev->width / 8; /* h_size , reg 2 */
299 data[4] = gspca_dev->height / 8; /* v_size , reg 3 */
300 data[5] = 0x30; /* reg 4, MI, PAS5101 :
301 * 0x30 for 24mhz , 0x28 for 12mhz */
302 data[6] = 0x02; /* reg 5, H start - was 0x04 */
303 data[7] = sd->ctrls[GAMMA].val * 0x40; /* reg 0x06: gamma */
304 data[8] = 0x01; /* reg 7, V start - was 0x03 */
305 /* if (h_size == 320 ) */
306 /* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
307 /* else */
308 data[9] = 0x52; /* reg 8, 24MHz, no scale down */
309 /*jfm: from win trace*/
310 data[10] = 0x18;
312 reg_w(gspca_dev, 11);
314 data[0] = 0x23; /* address */
315 data[1] = 0x09; /* reg 35, append frame header */
317 reg_w(gspca_dev, 2);
319 data[0] = 0x3c; /* address */
320 /* if (gspca_dev->width == 1280) */
321 /* data[1] = 200; * reg 60, pc-cam frame size
322 * (unit: 4KB) 800KB */
323 /* else */
324 data[1] = 50; /* 50 reg 60, pc-cam frame size
325 * (unit: 4KB) 200KB */
326 reg_w(gspca_dev, 2);
328 /* auto dark-gain */
329 data[0] = 0x5e; /* address */
330 data[1] = 0; /* reg 94, Y Gain (auto) */
331 /*jfm: from win trace*/
332 /* reg 0x5f/0x60 (LE) = saturation */
333 /* h (60): xxxx x100
334 * l (5f): xxxx x000 */
335 data[2] = sd->ctrls[COLORS].val << 3;
336 data[3] = ((sd->ctrls[COLORS].val >> 2) & 0xf8) | 0x04;
337 data[4] = sd->ctrls[BRIGHTNESS].val; /* reg 0x61 = brightness */
338 data[5] = 0x00;
340 reg_w(gspca_dev, 6);
342 data[0] = 0x67;
343 /*jfm: from win trace*/
344 data[1] = sd->ctrls[SHARPNESS].val * 4 + 3;
345 data[2] = 0x14;
346 reg_w(gspca_dev, 3);
348 data[0] = 0x69;
349 data[1] = 0x2f;
350 data[2] = 0x28;
351 data[3] = 0x42;
352 reg_w(gspca_dev, 4);
354 data[0] = 0x63;
355 data[1] = 0x07;
356 reg_w(gspca_dev, 2);
357 /*jfm: win trace - many writes here to reg 0x64*/
359 /* initialize the MI sensor */
360 for (i = 0; i < sizeof mi_data; i++)
361 mi_w(gspca_dev, i + 1, mi_data[i]);
363 data[0] = 0x00;
364 data[1] = 0x4d; /* ISOC transferring enable... */
365 reg_w(gspca_dev, 2);
367 gspca_dev->ctrl_inac = 0; /* activate the illuminator controls */
368 return gspca_dev->usb_err;
371 static void sd_stopN(struct gspca_dev *gspca_dev)
373 struct sd *sd = (struct sd *) gspca_dev;
375 gspca_dev->ctrl_inac = (1 << ILLUM_TOP) | (1 << ILLUM_BOT);
376 if (sd->ctrls[ILLUM_TOP].val || sd->ctrls[ILLUM_BOT].val) {
377 sd->ctrls[ILLUM_TOP].val = 0;
378 sd->ctrls[ILLUM_BOT].val = 0;
379 setilluminators(gspca_dev);
380 msleep(20);
383 gspca_dev->usb_buf[0] = 1;
384 gspca_dev->usb_buf[1] = 0;
385 reg_w(gspca_dev, 2);
388 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
389 u8 *data, /* isoc packet */
390 int len) /* iso packet length */
392 struct sd *sd = (struct sd *) gspca_dev;
393 int p;
395 if (len < 6) {
396 /* gspca_dev->last_packet_type = DISCARD_PACKET; */
397 return;
399 for (p = 0; p < len - 6; p++) {
400 if (data[0 + p] == 0xff
401 && data[1 + p] == 0xff
402 && data[2 + p] == 0x00
403 && data[3 + p] == 0xff
404 && data[4 + p] == 0x96) {
405 if (data[5 + p] == 0x64
406 || data[5 + p] == 0x65
407 || data[5 + p] == 0x66
408 || data[5 + p] == 0x67) {
409 PDEBUG(D_PACK, "sof offset: %d len: %d",
410 p, len);
411 gspca_frame_add(gspca_dev, LAST_PACKET,
412 data, p);
414 /* put the JPEG header */
415 gspca_frame_add(gspca_dev, FIRST_PACKET,
416 sd->jpeg_hdr, JPEG_HDR_SZ);
417 data += p + 16;
418 len -= p + 16;
419 break;
423 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
426 static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val)
428 struct sd *sd = (struct sd *) gspca_dev;
430 /* only one illuminator may be on */
431 sd->ctrls[ILLUM_TOP].val = val;
432 if (val)
433 sd->ctrls[ILLUM_BOT].val = 0;
434 setilluminators(gspca_dev);
435 return gspca_dev->usb_err;
438 static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val)
440 struct sd *sd = (struct sd *) gspca_dev;
442 /* only one illuminator may be on */
443 sd->ctrls[ILLUM_BOT].val = val;
444 if (val)
445 sd->ctrls[ILLUM_TOP].val = 0;
446 setilluminators(gspca_dev);
447 return gspca_dev->usb_err;
450 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
451 struct v4l2_jpegcompression *jcomp)
453 struct sd *sd = (struct sd *) gspca_dev;
455 if (jcomp->quality < QUALITY_MIN)
456 sd->quality = QUALITY_MIN;
457 else if (jcomp->quality > QUALITY_MAX)
458 sd->quality = QUALITY_MAX;
459 else
460 sd->quality = jcomp->quality;
461 if (gspca_dev->streaming)
462 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
463 return 0;
466 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
467 struct v4l2_jpegcompression *jcomp)
469 struct sd *sd = (struct sd *) gspca_dev;
471 memset(jcomp, 0, sizeof *jcomp);
472 jcomp->quality = sd->quality;
473 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
474 | V4L2_JPEG_MARKER_DQT;
475 return 0;
478 /* sub-driver description */
479 static const struct sd_desc sd_desc = {
480 .name = MODULE_NAME,
481 .ctrls = sd_ctrls,
482 .nctrls = NCTRLS,
483 .config = sd_config,
484 .init = sd_init,
485 .start = sd_start,
486 .stopN = sd_stopN,
487 .pkt_scan = sd_pkt_scan,
488 .get_jcomp = sd_get_jcomp,
489 .set_jcomp = sd_set_jcomp,
492 /* -- module initialisation -- */
493 static const struct usb_device_id device_table[] = {
494 {USB_DEVICE(0x093a, 0x050f)},
497 MODULE_DEVICE_TABLE(usb, device_table);
499 /* -- device connect -- */
500 static int sd_probe(struct usb_interface *intf,
501 const struct usb_device_id *id)
503 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
504 THIS_MODULE);
507 static struct usb_driver sd_driver = {
508 .name = MODULE_NAME,
509 .id_table = device_table,
510 .probe = sd_probe,
511 .disconnect = gspca_disconnect,
512 #ifdef CONFIG_PM
513 .suspend = gspca_suspend,
514 .resume = gspca_resume,
515 #endif
518 /* -- module insert / remove -- */
519 static int __init sd_mod_init(void)
521 return usb_register(&sd_driver);
523 static void __exit sd_mod_exit(void)
525 usb_deregister(&sd_driver);
528 module_init(sd_mod_init);
529 module_exit(sd_mod_exit);