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"
27 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
28 MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
29 MODULE_LICENSE("GPL");
31 /* specific webcam descriptor */
33 struct gspca_dev gspca_dev; /* !! must be the first item */
44 /* V4L2 controls supported by the driver */
45 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
46 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
47 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
48 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
49 static int sd_setgamma(struct gspca_dev *gspca_dev, __s32 val);
50 static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val);
51 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
52 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
54 static struct ctrl sd_ctrls[] = {
57 .id = V4L2_CID_BRIGHTNESS,
58 .type = V4L2_CTRL_TYPE_INTEGER,
63 #define BRIGHTNESS_DEF 15
64 .default_value = BRIGHTNESS_DEF,
66 .set = sd_setbrightness,
67 .get = sd_getbrightness,
71 .id = V4L2_CID_SATURATION,
72 .type = V4L2_CTRL_TYPE_INTEGER,
78 .default_value = COLOR_DEF,
86 .type = V4L2_CTRL_TYPE_INTEGER,
92 .default_value = GAMMA_DEF,
99 .id = V4L2_CID_SHARPNESS,
100 .type = V4L2_CTRL_TYPE_INTEGER,
105 #define SHARPNESS_DEF 1
106 .default_value = SHARPNESS_DEF,
108 .set = sd_setsharpness,
109 .get = sd_getsharpness,
113 static const struct v4l2_pix_format vga_mode[] = {
114 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
116 .sizeimage = 320 * 240 * 3 / 8 + 590,
117 .colorspace = V4L2_COLORSPACE_JPEG,
119 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
121 .sizeimage = 640 * 480 * 3 / 8 + 590,
122 .colorspace = V4L2_COLORSPACE_JPEG,
126 static const __u8 mi_data[0x20] = {
127 /* 01 02 03 04 05 06 07 08 */
128 0x48, 0x22, 0x01, 0x47, 0x10, 0x00, 0x00, 0x00,
129 /* 09 0a 0b 0c 0d 0e 0f 10 */
130 0x00, 0x01, 0x30, 0x01, 0x30, 0x01, 0x30, 0x01,
131 /* 11 12 13 14 15 16 17 18 */
132 0x30, 0x00, 0x04, 0x00, 0x06, 0x01, 0xe2, 0x02,
133 /* 19 1a 1b 1c 1d 1e 1f 20 */
134 0x82, 0x00, 0x20, 0x17, 0x80, 0x08, 0x0c, 0x00
137 /* write <len> bytes from gspca_dev->usb_buf */
138 static int reg_w(struct gspca_dev *gspca_dev,
143 ret = usb_bulk_msg(gspca_dev->dev,
144 usb_sndbulkpipe(gspca_dev->dev, 4),
148 500); /* timeout in milliseconds */
150 PDEBUG(D_ERR, "reg write [%02x] error %d",
151 gspca_dev->usb_buf[0], ret);
155 static void mi_w(struct gspca_dev *gspca_dev,
159 gspca_dev->usb_buf[0] = 0x1f;
160 gspca_dev->usb_buf[1] = 0; /* control byte */
161 gspca_dev->usb_buf[2] = addr;
162 gspca_dev->usb_buf[3] = value;
167 /* this function is called at probe time */
168 static int sd_config(struct gspca_dev *gspca_dev,
169 const struct usb_device_id *id)
171 struct sd *sd = (struct sd *) gspca_dev;
174 cam = &gspca_dev->cam;
175 cam->cam_mode = vga_mode;
176 cam->nmodes = ARRAY_SIZE(vga_mode);
177 sd->brightness = BRIGHTNESS_DEF;
178 sd->colors = COLOR_DEF;
179 sd->gamma = GAMMA_DEF;
180 sd->sharpness = SHARPNESS_DEF;
182 gspca_dev->nbalt = 9; /* use the altsetting 08 */
186 /* this function is called at probe and resume time */
187 static int sd_init(struct gspca_dev *gspca_dev)
192 static int sd_start(struct gspca_dev *gspca_dev)
194 struct sd *sd = (struct sd *) gspca_dev;
199 /* create the JPEG header */
200 sd->jpeg_hdr = kmalloc(JPEG_HDR_SZ, GFP_KERNEL);
201 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
202 0x21); /* JPEG 422 */
203 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
205 data = gspca_dev->usb_buf;
207 data[0] = 0x01; /* address */
209 err_code = reg_w(gspca_dev, 2);
214 Initialize the MR97113 chip register
216 data[0] = 0x00; /* address */
217 data[1] = 0x0c | 0x01; /* reg 0 */
218 data[2] = 0x01; /* reg 1 */
219 data[3] = gspca_dev->width / 8; /* h_size , reg 2 */
220 data[4] = gspca_dev->height / 8; /* v_size , reg 3 */
221 data[5] = 0x30; /* reg 4, MI, PAS5101 :
222 * 0x30 for 24mhz , 0x28 for 12mhz */
223 data[6] = 0x02; /* reg 5, H start - was 0x04 */
224 data[7] = sd->gamma * 0x40; /* reg 0x06: gamma */
225 data[8] = 0x01; /* reg 7, V start - was 0x03 */
226 /* if (h_size == 320 ) */
227 /* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
229 data[9] = 0x52; /* reg 8, 24MHz, no scale down */
230 /*jfm: from win trace*/
233 err_code = reg_w(gspca_dev, 11);
237 data[0] = 0x23; /* address */
238 data[1] = 0x09; /* reg 35, append frame header */
240 err_code = reg_w(gspca_dev, 2);
244 data[0] = 0x3c; /* address */
245 /* if (gspca_dev->width == 1280) */
246 /* data[1] = 200; * reg 60, pc-cam frame size
247 * (unit: 4KB) 800KB */
249 data[1] = 50; /* 50 reg 60, pc-cam frame size
250 * (unit: 4KB) 200KB */
251 err_code = reg_w(gspca_dev, 2);
256 data[0] = 0x5e; /* address */
257 data[1] = 0; /* reg 94, Y Gain (auto) */
258 /*jfm: from win trace*/
259 /* reg 0x5f/0x60 (LE) = saturation */
261 * l (5f): xxxx x000 */
262 data[2] = sd->colors << 3;
263 data[3] = ((sd->colors >> 2) & 0xf8) | 0x04;
264 data[4] = sd->brightness; /* reg 0x61 = brightness */
267 err_code = reg_w(gspca_dev, 6);
272 /*jfm: from win trace*/
273 data[1] = sd->sharpness * 4 + 3;
275 err_code = reg_w(gspca_dev, 3);
283 err_code = reg_w(gspca_dev, 4);
289 err_code = reg_w(gspca_dev, 2);
290 /*jfm: win trace - many writes here to reg 0x64*/
294 /* initialize the MI sensor */
295 for (i = 0; i < sizeof mi_data; i++)
296 mi_w(gspca_dev, i + 1, mi_data[i]);
299 data[1] = 0x4d; /* ISOC transfering enable... */
304 static void sd_stopN(struct gspca_dev *gspca_dev)
308 gspca_dev->usb_buf[0] = 1;
309 gspca_dev->usb_buf[1] = 0;
310 result = reg_w(gspca_dev, 2);
312 PDEBUG(D_ERR, "Camera Stop failed");
315 static void sd_stop0(struct gspca_dev *gspca_dev)
317 struct sd *sd = (struct sd *) gspca_dev;
322 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
323 struct gspca_frame *frame, /* target */
324 __u8 *data, /* isoc packet */
325 int len) /* iso packet length */
327 struct sd *sd = (struct sd *) gspca_dev;
331 /* gspca_dev->last_packet_type = DISCARD_PACKET; */
334 for (p = 0; p < len - 6; p++) {
335 if (data[0 + p] == 0xff
336 && data[1 + p] == 0xff
337 && data[2 + p] == 0x00
338 && data[3 + p] == 0xff
339 && data[4 + p] == 0x96) {
340 if (data[5 + p] == 0x64
341 || data[5 + p] == 0x65
342 || data[5 + p] == 0x66
343 || data[5 + p] == 0x67) {
344 PDEBUG(D_PACK, "sof offset: %d len: %d",
346 frame = gspca_frame_add(gspca_dev, LAST_PACKET,
349 /* put the JPEG header */
350 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
351 sd->jpeg_hdr, JPEG_HDR_SZ);
358 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
361 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
363 struct sd *sd = (struct sd *) gspca_dev;
365 sd->brightness = val;
366 if (gspca_dev->streaming) {
367 gspca_dev->usb_buf[0] = 0x61;
368 gspca_dev->usb_buf[1] = val;
374 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
376 struct sd *sd = (struct sd *) gspca_dev;
378 *val = sd->brightness;
382 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
384 struct sd *sd = (struct sd *) gspca_dev;
387 if (gspca_dev->streaming) {
390 gspca_dev->usb_buf[0] = 0x5f;
391 gspca_dev->usb_buf[1] = sd->colors << 3;
392 gspca_dev->usb_buf[2] = ((sd->colors >> 2) & 0xf8) | 0x04;
398 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
400 struct sd *sd = (struct sd *) gspca_dev;
406 static int sd_setgamma(struct gspca_dev *gspca_dev, __s32 val)
408 struct sd *sd = (struct sd *) gspca_dev;
411 if (gspca_dev->streaming) {
412 gspca_dev->usb_buf[0] = 0x06;
413 gspca_dev->usb_buf[1] = val * 0x40;
419 static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val)
421 struct sd *sd = (struct sd *) gspca_dev;
427 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
429 struct sd *sd = (struct sd *) gspca_dev;
432 if (gspca_dev->streaming) {
433 gspca_dev->usb_buf[0] = 0x67;
434 gspca_dev->usb_buf[1] = val * 4 + 3;
440 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
442 struct sd *sd = (struct sd *) gspca_dev;
444 *val = sd->sharpness;
448 /* sub-driver description */
449 static const struct sd_desc sd_desc = {
452 .nctrls = ARRAY_SIZE(sd_ctrls),
458 .pkt_scan = sd_pkt_scan,
461 /* -- module initialisation -- */
462 static const __devinitdata struct usb_device_id device_table[] = {
463 {USB_DEVICE(0x093a, 0x050f)},
466 MODULE_DEVICE_TABLE(usb, device_table);
468 /* -- device connect -- */
469 static int sd_probe(struct usb_interface *intf,
470 const struct usb_device_id *id)
472 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
476 static struct usb_driver sd_driver = {
478 .id_table = device_table,
480 .disconnect = gspca_disconnect,
482 .suspend = gspca_suspend,
483 .resume = gspca_resume,
487 /* -- module insert / remove -- */
488 static int __init sd_mod_init(void)
492 ret = usb_register(&sd_driver);
495 PDEBUG(D_PROBE, "registered");
498 static void __exit sd_mod_exit(void)
500 usb_deregister(&sd_driver);
501 PDEBUG(D_PROBE, "deregistered");
504 module_init(sd_mod_init);
505 module_exit(sd_mod_exit);