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");
39 NCTRLS
/* number of controls */
42 /* specific webcam descriptor */
44 struct gspca_dev gspca_dev
; /* !! must be the first item */
46 struct gspca_ctrl ctrls
[NCTRLS
];
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
] = {
67 .id
= V4L2_CID_BRIGHTNESS
,
68 .type
= V4L2_CTRL_TYPE_INTEGER
,
75 .set_control
= setbrightness
79 .id
= V4L2_CID_SATURATION
,
80 .type
= V4L2_CTRL_TYPE_INTEGER
,
87 .set_control
= setcolors
92 .type
= V4L2_CTRL_TYPE_INTEGER
,
99 .set_control
= setgamma
103 .id
= V4L2_CID_SHARPNESS
,
104 .type
= V4L2_CTRL_TYPE_INTEGER
,
111 .set_control
= setsharpness
115 .id
= V4L2_CID_ILLUMINATORS_1
,
116 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
117 .name
= "Top illuminator",
122 .flags
= V4L2_CTRL_FLAG_UPDATE
,
124 .set
= sd_setilluminator1
128 .id
= V4L2_CID_ILLUMINATORS_2
,
129 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
130 .name
= "Bottom illuminator",
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
,
144 .sizeimage
= 320 * 240 * 3 / 8 + 590,
145 .colorspace
= V4L2_COLORSPACE_JPEG
,
147 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
149 .sizeimage
= 640 * 480 * 3 / 8 + 590,
150 .colorspace
= V4L2_COLORSPACE_JPEG
,
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
,
171 if (gspca_dev
->usb_err
< 0)
174 ret
= usb_bulk_msg(gspca_dev
->dev
,
175 usb_sndbulkpipe(gspca_dev
->dev
, 4),
179 500); /* timeout in milliseconds */
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
,
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
;
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
;
208 static void setcolors(struct gspca_dev
*gspca_dev
)
210 struct sd
*sd
= (struct sd
*) gspca_dev
;
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;
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;
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;
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;
248 gspca_dev
->usb_buf
[1] = 0x7e;
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
;
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 */
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
);
275 static int sd_start(struct gspca_dev
*gspca_dev
)
277 struct sd
*sd
= (struct sd
*) gspca_dev
;
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 */
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 */
308 data
[9] = 0x52; /* reg 8, 24MHz, no scale down */
309 /*jfm: from win trace*/
312 reg_w(gspca_dev
, 11);
314 data
[0] = 0x23; /* address */
315 data
[1] = 0x09; /* reg 35, append frame header */
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 */
324 data
[1] = 50; /* 50 reg 60, pc-cam frame size
325 * (unit: 4KB) 200KB */
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 */
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 */
343 /*jfm: from win trace*/
344 data
[1] = sd
->ctrls
[SHARPNESS
].val
* 4 + 3;
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
]);
364 data
[1] = 0x4d; /* ISOC transferring enable... */
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
);
383 gspca_dev
->usb_buf
[0] = 1;
384 gspca_dev
->usb_buf
[1] = 0;
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
;
396 /* gspca_dev->last_packet_type = DISCARD_PACKET; */
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",
411 gspca_frame_add(gspca_dev
, LAST_PACKET
,
414 /* put the JPEG header */
415 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
416 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
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
;
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
;
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
;
460 sd
->quality
= jcomp
->quality
;
461 if (gspca_dev
->streaming
)
462 jpeg_set_qual(sd
->jpeg_hdr
, sd
->quality
);
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
;
478 /* sub-driver description */
479 static const struct sd_desc sd_desc
= {
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
),
507 static struct usb_driver sd_driver
= {
509 .id_table
= device_table
,
511 .disconnect
= gspca_disconnect
,
513 .suspend
= gspca_suspend
,
514 .resume
= gspca_resume
,
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
);