1 /* GSPCA subdrivers for Genesys Logic webcams with the GL860 chip
4 * 2009/09/24 Olivier Lorin <o.lorin@laposte.net>
5 * GSPCA by Jean-Francois Moine <http://moinejf.free.fr>
6 * Thanks BUGabundo and Malmostoso for your amazing help!
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 MODULE_AUTHOR("Olivier Lorin <o.lorin@laposte.net>");
28 MODULE_DESCRIPTION("Genesys Logic USB PC Camera Driver");
29 MODULE_LICENSE("GPL");
31 /*======================== static function declarations ====================*/
33 static void (*dev_init_settings
)(struct gspca_dev
*gspca_dev
);
35 static int sd_config(struct gspca_dev
*gspca_dev
,
36 const struct usb_device_id
*id
);
37 static int sd_init(struct gspca_dev
*gspca_dev
);
38 static int sd_isoc_init(struct gspca_dev
*gspca_dev
);
39 static int sd_start(struct gspca_dev
*gspca_dev
);
40 static void sd_stop0(struct gspca_dev
*gspca_dev
);
41 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
43 static void sd_callback(struct gspca_dev
*gspca_dev
);
45 static int gl860_guess_sensor(struct gspca_dev
*gspca_dev
,
46 u16 vendor_id
, u16 product_id
);
48 /*============================ driver options ==============================*/
50 static s32 AC50Hz
= 0xff;
51 module_param(AC50Hz
, int, 0644);
52 MODULE_PARM_DESC(AC50Hz
, " Does AC power frequency is 50Hz? (0/1)");
54 static char sensor
[7];
55 module_param_string(sensor
, sensor
, sizeof(sensor
), 0644);
56 MODULE_PARM_DESC(sensor
,
57 " Driver sensor ('MI1320'/'MI2020'/'OV9655'/'OV2640')");
59 /*============================ webcam controls =============================*/
61 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
63 struct gspca_dev
*gspca_dev
=
64 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
65 struct sd
*sd
= (struct sd
*) gspca_dev
;
68 case V4L2_CID_BRIGHTNESS
:
69 sd
->vcur
.brightness
= ctrl
->val
;
71 case V4L2_CID_CONTRAST
:
72 sd
->vcur
.contrast
= ctrl
->val
;
74 case V4L2_CID_SATURATION
:
75 sd
->vcur
.saturation
= ctrl
->val
;
78 sd
->vcur
.hue
= ctrl
->val
;
81 sd
->vcur
.gamma
= ctrl
->val
;
84 sd
->vcur
.mirror
= ctrl
->val
;
87 sd
->vcur
.flip
= ctrl
->val
;
89 case V4L2_CID_POWER_LINE_FREQUENCY
:
90 sd
->vcur
.AC50Hz
= ctrl
->val
;
92 case V4L2_CID_WHITE_BALANCE_TEMPERATURE
:
93 sd
->vcur
.whitebal
= ctrl
->val
;
95 case V4L2_CID_SHARPNESS
:
96 sd
->vcur
.sharpness
= ctrl
->val
;
98 case V4L2_CID_BACKLIGHT_COMPENSATION
:
99 sd
->vcur
.backlight
= ctrl
->val
;
105 if (gspca_dev
->streaming
)
111 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
115 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
117 struct sd
*sd
= (struct sd
*) gspca_dev
;
118 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
120 gspca_dev
->vdev
.ctrl_handler
= hdl
;
121 v4l2_ctrl_handler_init(hdl
, 11);
123 if (sd
->vmax
.brightness
)
124 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
, V4L2_CID_BRIGHTNESS
,
125 0, sd
->vmax
.brightness
, 1,
126 sd
->vcur
.brightness
);
128 if (sd
->vmax
.contrast
)
129 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
, V4L2_CID_CONTRAST
,
130 0, sd
->vmax
.contrast
, 1,
133 if (sd
->vmax
.saturation
)
134 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
, V4L2_CID_SATURATION
,
135 0, sd
->vmax
.saturation
, 1,
136 sd
->vcur
.saturation
);
139 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
, V4L2_CID_HUE
,
140 0, sd
->vmax
.hue
, 1, sd
->vcur
.hue
);
143 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
, V4L2_CID_GAMMA
,
144 0, sd
->vmax
.gamma
, 1, sd
->vcur
.gamma
);
147 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
, V4L2_CID_HFLIP
,
148 0, sd
->vmax
.mirror
, 1, sd
->vcur
.mirror
);
151 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
, V4L2_CID_VFLIP
,
152 0, sd
->vmax
.flip
, 1, sd
->vcur
.flip
);
155 v4l2_ctrl_new_std_menu(hdl
, &sd_ctrl_ops
,
156 V4L2_CID_POWER_LINE_FREQUENCY
,
157 sd
->vmax
.AC50Hz
, 0, sd
->vcur
.AC50Hz
);
159 if (sd
->vmax
.whitebal
)
160 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
161 V4L2_CID_WHITE_BALANCE_TEMPERATURE
,
162 0, sd
->vmax
.whitebal
, 1, sd
->vcur
.whitebal
);
164 if (sd
->vmax
.sharpness
)
165 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
, V4L2_CID_SHARPNESS
,
166 0, sd
->vmax
.sharpness
, 1,
169 if (sd
->vmax
.backlight
)
170 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
171 V4L2_CID_BACKLIGHT_COMPENSATION
,
172 0, sd
->vmax
.backlight
, 1,
176 pr_err("Could not initialize controls\n");
183 /*==================== sud-driver structure initialisation =================*/
185 static const struct sd_desc sd_desc_mi1320
= {
189 .init_controls
= sd_init_controls
,
190 .isoc_init
= sd_isoc_init
,
193 .pkt_scan
= sd_pkt_scan
,
194 .dq_callback
= sd_callback
,
197 static const struct sd_desc sd_desc_mi2020
= {
201 .init_controls
= sd_init_controls
,
202 .isoc_init
= sd_isoc_init
,
205 .pkt_scan
= sd_pkt_scan
,
206 .dq_callback
= sd_callback
,
209 static const struct sd_desc sd_desc_ov2640
= {
213 .init_controls
= sd_init_controls
,
214 .isoc_init
= sd_isoc_init
,
217 .pkt_scan
= sd_pkt_scan
,
218 .dq_callback
= sd_callback
,
221 static const struct sd_desc sd_desc_ov9655
= {
225 .init_controls
= sd_init_controls
,
226 .isoc_init
= sd_isoc_init
,
229 .pkt_scan
= sd_pkt_scan
,
230 .dq_callback
= sd_callback
,
233 /*=========================== sub-driver image sizes =======================*/
235 static struct v4l2_pix_format mi2020_mode
[] = {
236 { 640, 480, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
238 .sizeimage
= 640 * 480,
239 .colorspace
= V4L2_COLORSPACE_SRGB
,
242 { 800, 598, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
244 .sizeimage
= 800 * 598,
245 .colorspace
= V4L2_COLORSPACE_SRGB
,
248 {1280, 1024, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
249 .bytesperline
= 1280,
250 .sizeimage
= 1280 * 1024,
251 .colorspace
= V4L2_COLORSPACE_SRGB
,
254 {1600, 1198, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
255 .bytesperline
= 1600,
256 .sizeimage
= 1600 * 1198,
257 .colorspace
= V4L2_COLORSPACE_SRGB
,
262 static struct v4l2_pix_format ov2640_mode
[] = {
263 { 640, 480, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
265 .sizeimage
= 640 * 480,
266 .colorspace
= V4L2_COLORSPACE_SRGB
,
269 { 800, 600, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
271 .sizeimage
= 800 * 600,
272 .colorspace
= V4L2_COLORSPACE_SRGB
,
275 {1280, 960, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
276 .bytesperline
= 1280,
277 .sizeimage
= 1280 * 960,
278 .colorspace
= V4L2_COLORSPACE_SRGB
,
281 {1600, 1200, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
282 .bytesperline
= 1600,
283 .sizeimage
= 1600 * 1200,
284 .colorspace
= V4L2_COLORSPACE_SRGB
,
289 static struct v4l2_pix_format mi1320_mode
[] = {
290 { 640, 480, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
292 .sizeimage
= 640 * 480,
293 .colorspace
= V4L2_COLORSPACE_SRGB
,
296 { 800, 600, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
298 .sizeimage
= 800 * 600,
299 .colorspace
= V4L2_COLORSPACE_SRGB
,
302 {1280, 960, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
303 .bytesperline
= 1280,
304 .sizeimage
= 1280 * 960,
305 .colorspace
= V4L2_COLORSPACE_SRGB
,
310 static struct v4l2_pix_format ov9655_mode
[] = {
311 { 640, 480, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
313 .sizeimage
= 640 * 480,
314 .colorspace
= V4L2_COLORSPACE_SRGB
,
317 {1280, 960, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
318 .bytesperline
= 1280,
319 .sizeimage
= 1280 * 960,
320 .colorspace
= V4L2_COLORSPACE_SRGB
,
325 /*========================= sud-driver functions ===========================*/
327 /* This function is called at probe time */
328 static int sd_config(struct gspca_dev
*gspca_dev
,
329 const struct usb_device_id
*id
)
331 struct sd
*sd
= (struct sd
*) gspca_dev
;
333 u16 vendor_id
, product_id
;
335 /* Get USB VendorID and ProductID */
336 vendor_id
= id
->idVendor
;
337 product_id
= id
->idProduct
;
343 if (strcmp(sensor
, "MI1320") == 0)
344 sd
->sensor
= ID_MI1320
;
345 else if (strcmp(sensor
, "OV2640") == 0)
346 sd
->sensor
= ID_OV2640
;
347 else if (strcmp(sensor
, "OV9655") == 0)
348 sd
->sensor
= ID_OV9655
;
349 else if (strcmp(sensor
, "MI2020") == 0)
350 sd
->sensor
= ID_MI2020
;
352 /* Get sensor and set the suitable init/start/../stop functions */
353 if (gl860_guess_sensor(gspca_dev
, vendor_id
, product_id
) == -1)
356 cam
= &gspca_dev
->cam
;
358 switch (sd
->sensor
) {
360 gspca_dev
->sd_desc
= &sd_desc_mi1320
;
361 cam
->cam_mode
= mi1320_mode
;
362 cam
->nmodes
= ARRAY_SIZE(mi1320_mode
);
363 dev_init_settings
= mi1320_init_settings
;
367 gspca_dev
->sd_desc
= &sd_desc_mi2020
;
368 cam
->cam_mode
= mi2020_mode
;
369 cam
->nmodes
= ARRAY_SIZE(mi2020_mode
);
370 dev_init_settings
= mi2020_init_settings
;
374 gspca_dev
->sd_desc
= &sd_desc_ov2640
;
375 cam
->cam_mode
= ov2640_mode
;
376 cam
->nmodes
= ARRAY_SIZE(ov2640_mode
);
377 dev_init_settings
= ov2640_init_settings
;
381 gspca_dev
->sd_desc
= &sd_desc_ov9655
;
382 cam
->cam_mode
= ov9655_mode
;
383 cam
->nmodes
= ARRAY_SIZE(ov9655_mode
);
384 dev_init_settings
= ov9655_init_settings
;
388 dev_init_settings(gspca_dev
);
390 ((struct sd
*) gspca_dev
)->vcur
.AC50Hz
= AC50Hz
;
395 /* This function is called at probe time after sd_config */
396 static int sd_init(struct gspca_dev
*gspca_dev
)
398 struct sd
*sd
= (struct sd
*) gspca_dev
;
400 return sd
->dev_init_at_startup(gspca_dev
);
403 /* This function is called before to choose the alt setting */
404 static int sd_isoc_init(struct gspca_dev
*gspca_dev
)
406 struct sd
*sd
= (struct sd
*) gspca_dev
;
408 return sd
->dev_configure_alt(gspca_dev
);
411 /* This function is called to start the webcam */
412 static int sd_start(struct gspca_dev
*gspca_dev
)
414 struct sd
*sd
= (struct sd
*) gspca_dev
;
416 return sd
->dev_init_pre_alt(gspca_dev
);
419 /* This function is called to stop the webcam */
420 static void sd_stop0(struct gspca_dev
*gspca_dev
)
422 struct sd
*sd
= (struct sd
*) gspca_dev
;
424 if (!sd
->gspca_dev
.present
)
427 return sd
->dev_post_unset_alt(gspca_dev
);
430 /* This function is called when an image is being received */
431 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
434 struct sd
*sd
= (struct sd
*) gspca_dev
;
437 s32 mode
= (s32
) gspca_dev
->curr_mode
;
439 sd
->swapRB
* (gspca_dev
->cam
.cam_mode
[mode
].bytesperline
+ 1);
441 /* Test only against 0202h, so endianness does not matter */
442 switch (*(s16
*) data
) {
443 case 0x0202: /* End of frame, start a new one */
444 gspca_frame_add(gspca_dev
, LAST_PACKET
, NULL
, 0);
446 if (sd
->nbIm
>= 0 && sd
->nbIm
< 10)
448 gspca_frame_add(gspca_dev
, FIRST_PACKET
, NULL
, 0);
454 if (nSkipped
+ len
<= nToSkip
)
457 if (nSkipped
< nToSkip
&& nSkipped
+ len
> nToSkip
) {
458 data
+= nToSkip
- nSkipped
;
459 len
-= nToSkip
- nSkipped
;
460 nSkipped
= nToSkip
+ 1;
462 gspca_frame_add(gspca_dev
,
463 INTER_PACKET
, data
, len
);
469 /* This function is called when an image has been read */
470 /* This function is used to monitor webcam orientation */
471 static void sd_callback(struct gspca_dev
*gspca_dev
)
473 struct sd
*sd
= (struct sd
*) gspca_dev
;
479 /* Probe sensor orientation */
480 ctrl_in(gspca_dev
, 0xc0, 2, 0x0000, 0x0000, 1, (void *)&state
);
482 /* C8/40 means upside-down (looking backwards) */
483 /* D8/50 means right-up (looking onwards) */
484 upsideDown
= (state
== 0xc8 || state
== 0x40);
486 if (upsideDown
&& sd
->nbRightUp
> -4) {
487 if (sd
->nbRightUp
> 0)
489 if (sd
->nbRightUp
== -3) {
495 if (!upsideDown
&& sd
->nbRightUp
< 4) {
496 if (sd
->nbRightUp
< 0)
498 if (sd
->nbRightUp
== 3) {
507 sd
->dev_camera_settings(gspca_dev
);
510 /*=================== USB driver structure initialisation ==================*/
512 static const struct usb_device_id device_table
[] = {
513 {USB_DEVICE(0x05e3, 0x0503)},
514 {USB_DEVICE(0x05e3, 0xf191)},
518 MODULE_DEVICE_TABLE(usb
, device_table
);
520 static int sd_probe(struct usb_interface
*intf
,
521 const struct usb_device_id
*id
)
523 return gspca_dev_probe(intf
, id
,
524 &sd_desc_mi1320
, sizeof(struct sd
), THIS_MODULE
);
527 static void sd_disconnect(struct usb_interface
*intf
)
529 gspca_disconnect(intf
);
532 static struct usb_driver sd_driver
= {
534 .id_table
= device_table
,
536 .disconnect
= sd_disconnect
,
538 .suspend
= gspca_suspend
,
539 .resume
= gspca_resume
,
540 .reset_resume
= gspca_resume
,
544 /*====================== Init and Exit module functions ====================*/
546 module_usb_driver(sd_driver
);
548 /*==========================================================================*/
550 int gl860_RTx(struct gspca_dev
*gspca_dev
,
551 unsigned char pref
, u32 req
, u16 val
, u16 index
,
552 s32 len
, void *pdata
)
554 struct usb_device
*udev
= gspca_dev
->dev
;
557 if (pref
== 0x40) { /* Send */
559 memcpy(gspca_dev
->usb_buf
, pdata
, len
);
560 r
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
561 req
, pref
, val
, index
,
563 len
, 400 + 200 * (len
> 1));
565 r
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
566 req
, pref
, val
, index
, NULL
, len
, 400);
568 } else { /* Receive */
570 r
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
571 req
, pref
, val
, index
,
573 len
, 400 + 200 * (len
> 1));
574 memcpy(pdata
, gspca_dev
->usb_buf
, len
);
576 r
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
577 req
, pref
, val
, index
, NULL
, len
, 400);
582 pr_err("ctrl transfer failed %4d [p%02x r%d v%04x i%04x len%d]\n",
583 r
, pref
, req
, val
, index
, len
);
584 else if (len
> 1 && r
< len
)
585 PERR("short ctrl transfer %d/%d", r
, len
);
592 int fetch_validx(struct gspca_dev
*gspca_dev
, struct validx
*tbl
, int len
)
596 for (n
= 0; n
< len
; n
++) {
597 if (tbl
[n
].idx
!= 0xffff)
598 ctrl_out(gspca_dev
, 0x40, 1, tbl
[n
].val
,
599 tbl
[n
].idx
, 0, NULL
);
600 else if (tbl
[n
].val
== 0xffff)
608 int keep_on_fetching_validx(struct gspca_dev
*gspca_dev
, struct validx
*tbl
,
612 if (tbl
[n
].idx
!= 0xffff)
613 ctrl_out(gspca_dev
, 0x40, 1, tbl
[n
].val
, tbl
[n
].idx
,
615 else if (tbl
[n
].val
== 0xffff)
623 void fetch_idxdata(struct gspca_dev
*gspca_dev
, struct idxdata
*tbl
, int len
)
627 for (n
= 0; n
< len
; n
++) {
628 if (memcmp(tbl
[n
].data
, "\xff\xff\xff", 3) != 0)
629 ctrl_out(gspca_dev
, 0x40, 3, 0x7a00, tbl
[n
].idx
,
636 static int gl860_guess_sensor(struct gspca_dev
*gspca_dev
,
637 u16 vendor_id
, u16 product_id
)
639 struct sd
*sd
= (struct sd
*) gspca_dev
;
640 u8 probe
, nb26
, nb96
, nOV
, ntry
;
642 if (product_id
== 0xf191)
643 sd
->sensor
= ID_MI1320
;
645 if (sd
->sensor
== 0xff) {
646 ctrl_in(gspca_dev
, 0xc0, 2, 0x0000, 0x0004, 1, &probe
);
647 ctrl_in(gspca_dev
, 0xc0, 2, 0x0000, 0x0004, 1, &probe
);
649 ctrl_out(gspca_dev
, 0x40, 1, 0x0000, 0x0000, 0, NULL
);
651 ctrl_out(gspca_dev
, 0x40, 1, 0x0010, 0x0010, 0, NULL
);
653 ctrl_out(gspca_dev
, 0x40, 1, 0x0008, 0x00c0, 0, NULL
);
655 ctrl_out(gspca_dev
, 0x40, 1, 0x0001, 0x00c1, 0, NULL
);
657 ctrl_out(gspca_dev
, 0x40, 1, 0x0001, 0x00c2, 0, NULL
);
659 ctrl_out(gspca_dev
, 0x40, 1, 0x0020, 0x0006, 0, NULL
);
661 ctrl_out(gspca_dev
, 0x40, 1, 0x006a, 0x000d, 0, NULL
);
664 PDEBUG(D_PROBE
, "probing for sensor MI2020 or OVXXXX");
666 for (ntry
= 0; ntry
< 4; ntry
++) {
667 ctrl_out(gspca_dev
, 0x40, 1, 0x0040, 0x0000, 0, NULL
);
669 ctrl_out(gspca_dev
, 0x40, 1, 0x0063, 0x0006, 0, NULL
);
671 ctrl_out(gspca_dev
, 0x40, 1, 0x7a00, 0x8030, 0, NULL
);
673 ctrl_in(gspca_dev
, 0xc0, 2, 0x7a00, 0x8030, 1, &probe
);
674 PDEBUG(D_PROBE
, "probe=0x%02x", probe
);
680 PDEBUG(D_PROBE
, "0xff -> OVXXXX");
681 PDEBUG(D_PROBE
, "probing for sensor OV2640 or OV9655");
684 for (ntry
= 0; ntry
< 4; ntry
++) {
685 ctrl_out(gspca_dev
, 0x40, 1, 0x0040, 0x0000,
688 ctrl_out(gspca_dev
, 0x40, 1, 0x6000, 0x800a,
692 /* Wait for 26(OV2640) or 96(OV9655) */
693 ctrl_in(gspca_dev
, 0xc0, 2, 0x6000, 0x800a,
696 if (probe
== 0x26 || probe
== 0x40) {
698 "probe=0x%02x -> OV2640",
700 sd
->sensor
= ID_OV2640
;
704 if (probe
== 0x96 || probe
== 0x55) {
706 "probe=0x%02x -> OV9655",
708 sd
->sensor
= ID_OV9655
;
712 PDEBUG(D_PROBE
, "probe=0x%02x", probe
);
719 if (nb26
< 4 && nb96
< 4)
722 PDEBUG(D_PROBE
, "Not any 0xff -> MI2020");
723 sd
->sensor
= ID_MI2020
;
728 PDEBUG(D_PROBE
, "05e3:f191 sensor MI1320 (1.3M)");
729 } else if (_MI2020_
) {
730 PDEBUG(D_PROBE
, "05e3:0503 sensor MI2020 (2.0M)");
731 } else if (_OV9655_
) {
732 PDEBUG(D_PROBE
, "05e3:0503 sensor OV9655 (1.3M)");
733 } else if (_OV2640_
) {
734 PDEBUG(D_PROBE
, "05e3:0503 sensor OV2640 (2.0M)");
736 PDEBUG(D_PROBE
, "***** Unknown sensor *****");