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
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"
25 #define QUANT_VAL 1 /* quantization table */
28 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
29 MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
30 MODULE_LICENSE("GPL");
32 /* specific webcam descriptor */
34 struct gspca_dev gspca_dev; /* !! must be the first item */
42 /* V4L2 controls supported by the driver */
43 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
44 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
45 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
46 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
47 static int sd_setgamma(struct gspca_dev *gspca_dev, __s32 val);
48 static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val);
49 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
50 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
52 static struct ctrl sd_ctrls[] = {
55 .id = V4L2_CID_BRIGHTNESS,
56 .type = V4L2_CTRL_TYPE_INTEGER,
61 #define BRIGHTNESS_DEF 15
62 .default_value = BRIGHTNESS_DEF,
64 .set = sd_setbrightness,
65 .get = sd_getbrightness,
69 .id = V4L2_CID_SATURATION,
70 .type = V4L2_CTRL_TYPE_INTEGER,
76 .default_value = COLOR_DEF,
84 .type = V4L2_CTRL_TYPE_INTEGER,
90 .default_value = GAMMA_DEF,
97 .id = V4L2_CID_SHARPNESS,
98 .type = V4L2_CTRL_TYPE_INTEGER,
103 #define SHARPNESS_DEF 1
104 .default_value = SHARPNESS_DEF,
106 .set = sd_setsharpness,
107 .get = sd_getsharpness,
111 static const struct v4l2_pix_format vga_mode[] = {
112 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
114 .sizeimage = 320 * 240 * 3 / 8 + 590,
115 .colorspace = V4L2_COLORSPACE_JPEG,
117 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
119 .sizeimage = 640 * 480 * 3 / 8 + 590,
120 .colorspace = V4L2_COLORSPACE_JPEG,
124 static const __u8 mi_data[0x20] = {
125 /* 01 02 03 04 05 06 07 08 */
126 0x48, 0x22, 0x01, 0x47, 0x10, 0x00, 0x00, 0x00,
127 /* 09 0a 0b 0c 0d 0e 0f 10 */
128 0x00, 0x01, 0x30, 0x01, 0x30, 0x01, 0x30, 0x01,
129 /* 11 12 13 14 15 16 17 18 */
130 0x30, 0x00, 0x04, 0x00, 0x06, 0x01, 0xe2, 0x02,
131 /* 19 1a 1b 1c 1d 1e 1f 20 */
132 0x82, 0x00, 0x20, 0x17, 0x80, 0x08, 0x0c, 0x00
135 /* write <len> bytes from gspca_dev->usb_buf */
136 static int reg_w(struct gspca_dev *gspca_dev,
141 ret = usb_bulk_msg(gspca_dev->dev,
142 usb_sndbulkpipe(gspca_dev->dev, 4),
146 500); /* timeout in milliseconds */
148 PDEBUG(D_ERR, "reg write [%02x] error %d",
149 gspca_dev->usb_buf[0], ret);
153 static void mi_w(struct gspca_dev *gspca_dev,
157 gspca_dev->usb_buf[0] = 0x1f;
158 gspca_dev->usb_buf[1] = 0; /* control byte */
159 gspca_dev->usb_buf[2] = addr;
160 gspca_dev->usb_buf[3] = value;
165 /* this function is called at probe time */
166 static int sd_config(struct gspca_dev *gspca_dev,
167 const struct usb_device_id *id)
169 struct sd *sd = (struct sd *) gspca_dev;
172 cam = &gspca_dev->cam;
173 cam->cam_mode = vga_mode;
174 cam->nmodes = ARRAY_SIZE(vga_mode);
175 sd->brightness = BRIGHTNESS_DEF;
176 sd->colors = COLOR_DEF;
177 sd->gamma = GAMMA_DEF;
178 sd->sharpness = SHARPNESS_DEF;
179 gspca_dev->nbalt = 9; /* use the altsetting 08 */
183 /* this function is called at probe and resume time */
184 static int sd_init(struct gspca_dev *gspca_dev)
189 static int sd_start(struct gspca_dev *gspca_dev)
191 struct sd *sd = (struct sd *) gspca_dev;
196 data = gspca_dev->usb_buf;
198 data[0] = 0x01; /* address */
200 err_code = reg_w(gspca_dev, 2);
205 Initialize the MR97113 chip register
207 data[0] = 0x00; /* address */
208 data[1] = 0x0c | 0x01; /* reg 0 */
209 data[2] = 0x01; /* reg 1 */
210 data[3] = gspca_dev->width / 8; /* h_size , reg 2 */
211 data[4] = gspca_dev->height / 8; /* v_size , reg 3 */
212 data[5] = 0x30; /* reg 4, MI, PAS5101 :
213 * 0x30 for 24mhz , 0x28 for 12mhz */
214 data[6] = 0x02; /* reg 5, H start - was 0x04 */
215 data[7] = sd->gamma * 0x40; /* reg 0x06: gamma */
216 data[8] = 0x01; /* reg 7, V start - was 0x03 */
217 /* if (h_size == 320 ) */
218 /* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
220 data[9] = 0x52; /* reg 8, 24MHz, no scale down */
221 /*jfm: from win trace*/
224 err_code = reg_w(gspca_dev, 11);
228 data[0] = 0x23; /* address */
229 data[1] = 0x09; /* reg 35, append frame header */
231 err_code = reg_w(gspca_dev, 2);
235 data[0] = 0x3c; /* address */
236 /* if (gspca_dev->width == 1280) */
237 /* data[1] = 200; * reg 60, pc-cam frame size
238 * (unit: 4KB) 800KB */
240 data[1] = 50; /* 50 reg 60, pc-cam frame size
241 * (unit: 4KB) 200KB */
242 err_code = reg_w(gspca_dev, 2);
247 data[0] = 0x5e; /* address */
248 data[1] = 0; /* reg 94, Y Gain (auto) */
249 /*jfm: from win trace*/
250 /* reg 0x5f/0x60 (LE) = saturation */
252 * l (5f): xxxx x000 */
253 data[2] = sd->colors << 3;
254 data[3] = ((sd->colors >> 2) & 0xf8) | 0x04;
255 data[4] = sd->brightness; /* reg 0x61 = brightness */
258 err_code = reg_w(gspca_dev, 6);
263 /*jfm: from win trace*/
264 data[1] = sd->sharpness * 4 + 3;
266 err_code = reg_w(gspca_dev, 3);
274 err_code = reg_w(gspca_dev, 4);
280 err_code = reg_w(gspca_dev, 2);
281 /*jfm: win trace - many writes here to reg 0x64*/
285 /* initialize the MI sensor */
286 for (i = 0; i < sizeof mi_data; i++)
287 mi_w(gspca_dev, i + 1, mi_data[i]);
290 data[1] = 0x4d; /* ISOC transfering enable... */
295 static void sd_stopN(struct gspca_dev *gspca_dev)
299 gspca_dev->usb_buf[0] = 1;
300 gspca_dev->usb_buf[1] = 0;
301 result = reg_w(gspca_dev, 2);
303 PDEBUG(D_ERR, "Camera Stop failed");
306 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
307 struct gspca_frame *frame, /* target */
308 __u8 *data, /* isoc packet */
309 int len) /* iso packet length */
314 /* gspca_dev->last_packet_type = DISCARD_PACKET; */
317 for (p = 0; p < len - 6; p++) {
318 if (data[0 + p] == 0xff
319 && data[1 + p] == 0xff
320 && data[2 + p] == 0x00
321 && data[3 + p] == 0xff
322 && data[4 + p] == 0x96) {
323 if (data[5 + p] == 0x64
324 || data[5 + p] == 0x65
325 || data[5 + p] == 0x66
326 || data[5 + p] == 0x67) {
327 PDEBUG(D_PACK, "sof offset: %d len: %d",
329 frame = gspca_frame_add(gspca_dev, LAST_PACKET,
332 /* put the JPEG header */
333 jpeg_put_header(gspca_dev, frame, 0x21);
340 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
343 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
345 struct sd *sd = (struct sd *) gspca_dev;
347 sd->brightness = val;
348 if (gspca_dev->streaming) {
349 gspca_dev->usb_buf[0] = 0x61;
350 gspca_dev->usb_buf[1] = val;
356 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
358 struct sd *sd = (struct sd *) gspca_dev;
360 *val = sd->brightness;
364 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
366 struct sd *sd = (struct sd *) gspca_dev;
369 if (gspca_dev->streaming) {
372 gspca_dev->usb_buf[0] = 0x5f;
373 gspca_dev->usb_buf[1] = sd->colors << 3;
374 gspca_dev->usb_buf[2] = ((sd->colors >> 2) & 0xf8) | 0x04;
380 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
382 struct sd *sd = (struct sd *) gspca_dev;
388 static int sd_setgamma(struct gspca_dev *gspca_dev, __s32 val)
390 struct sd *sd = (struct sd *) gspca_dev;
393 if (gspca_dev->streaming) {
394 gspca_dev->usb_buf[0] = 0x06;
395 gspca_dev->usb_buf[1] = val * 0x40;
401 static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val)
403 struct sd *sd = (struct sd *) gspca_dev;
409 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
411 struct sd *sd = (struct sd *) gspca_dev;
414 if (gspca_dev->streaming) {
415 gspca_dev->usb_buf[0] = 0x67;
416 gspca_dev->usb_buf[1] = val * 4 + 3;
422 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
424 struct sd *sd = (struct sd *) gspca_dev;
426 *val = sd->sharpness;
430 /* sub-driver description */
431 static const struct sd_desc sd_desc = {
434 .nctrls = ARRAY_SIZE(sd_ctrls),
439 .pkt_scan = sd_pkt_scan,
442 /* -- module initialisation -- */
443 static const __devinitdata struct usb_device_id device_table[] = {
444 {USB_DEVICE(0x093a, 0x050f)},
447 MODULE_DEVICE_TABLE(usb, device_table);
449 /* -- device connect -- */
450 static int sd_probe(struct usb_interface *intf,
451 const struct usb_device_id *id)
453 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
457 static struct usb_driver sd_driver = {
459 .id_table = device_table,
461 .disconnect = gspca_disconnect,
463 .suspend = gspca_suspend,
464 .resume = gspca_resume,
468 /* -- module insert / remove -- */
469 static int __init sd_mod_init(void)
473 ret = usb_register(&sd_driver);
476 PDEBUG(D_PROBE, "registered");
479 static void __exit sd_mod_exit(void)
481 usb_deregister(&sd_driver);
482 PDEBUG(D_PROBE, "deregistered");
485 module_init(sd_mod_init);
486 module_exit(sd_mod_exit);