2 * Driver for USB webcams based on Konica chipset. This
3 * chipset is used in Intel YC76 camera.
5 * Copyright (C) 2010 Hans de Goede <hdegoede@redhat.com>
7 * Based on the usbvideo v4l1 konicawc driver which is:
9 * Copyright (C) 2002 Simon Evans <spse@secret.org.uk>
11 * The code for making gspca work with a webcam with 2 isoc endpoints was
12 * taken from the benq gspca subdriver which is:
14 * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #define MODULE_NAME "konica"
33 #include <linux/input.h>
36 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
37 MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
38 MODULE_LICENSE("GPL");
40 #define WHITEBAL_REG 0x01
41 #define BRIGHTNESS_REG 0x02
42 #define SHARPNESS_REG 0x03
43 #define CONTRAST_REG 0x04
44 #define SATURATION_REG 0x05
46 /* specific webcam descriptor */
48 struct gspca_dev gspca_dev
; /* !! must be the first item */
49 struct urb
*last_data_urb
;
58 /* V4L2 controls supported by the driver */
59 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
);
60 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
);
61 static int sd_setcontrast(struct gspca_dev
*gspca_dev
, __s32 val
);
62 static int sd_getcontrast(struct gspca_dev
*gspca_dev
, __s32
*val
);
63 static int sd_setsaturation(struct gspca_dev
*gspca_dev
, __s32 val
);
64 static int sd_getsaturation(struct gspca_dev
*gspca_dev
, __s32
*val
);
65 static int sd_setwhitebal(struct gspca_dev
*gspca_dev
, __s32 val
);
66 static int sd_getwhitebal(struct gspca_dev
*gspca_dev
, __s32
*val
);
67 static int sd_setsharpness(struct gspca_dev
*gspca_dev
, __s32 val
);
68 static int sd_getsharpness(struct gspca_dev
*gspca_dev
, __s32
*val
);
70 static const struct ctrl sd_ctrls
[] = {
71 #define SD_BRIGHTNESS 0
74 .id
= V4L2_CID_BRIGHTNESS
,
75 .type
= V4L2_CTRL_TYPE_INTEGER
,
80 #define BRIGHTNESS_DEFAULT 4
81 .default_value
= BRIGHTNESS_DEFAULT
,
84 .set
= sd_setbrightness
,
85 .get
= sd_getbrightness
,
90 .id
= V4L2_CID_CONTRAST
,
91 .type
= V4L2_CTRL_TYPE_INTEGER
,
96 #define CONTRAST_DEFAULT 10
97 .default_value
= CONTRAST_DEFAULT
,
100 .set
= sd_setcontrast
,
101 .get
= sd_getcontrast
,
103 #define SD_SATURATION 2
106 .id
= V4L2_CID_SATURATION
,
107 .type
= V4L2_CTRL_TYPE_INTEGER
,
108 .name
= "Saturation",
112 #define SATURATION_DEFAULT 4
113 .default_value
= SATURATION_DEFAULT
,
116 .set
= sd_setsaturation
,
117 .get
= sd_getsaturation
,
119 #define SD_WHITEBAL 3
122 .id
= V4L2_CID_WHITE_BALANCE_TEMPERATURE
,
123 .type
= V4L2_CTRL_TYPE_INTEGER
,
124 .name
= "White Balance",
128 #define WHITEBAL_DEFAULT 25
129 .default_value
= WHITEBAL_DEFAULT
,
132 .set
= sd_setwhitebal
,
133 .get
= sd_getwhitebal
,
135 #define SD_SHARPNESS 4
138 .id
= V4L2_CID_SHARPNESS
,
139 .type
= V4L2_CTRL_TYPE_INTEGER
,
144 #define SHARPNESS_DEFAULT 4
145 .default_value
= SHARPNESS_DEFAULT
,
148 .set
= sd_setsharpness
,
149 .get
= sd_getsharpness
,
153 /* .priv is what goes to register 8 for this mode, known working values:
154 0x00 -> 176x144, cropped
155 0x01 -> 176x144, cropped
156 0x02 -> 176x144, cropped
157 0x03 -> 176x144, cropped
158 0x04 -> 176x144, binned
161 0x07 -> 160x120, cropped
162 0x08 -> 160x120, cropped
163 0x09 -> 160x120, binned (note has 136 lines)
164 0x0a -> 160x120, binned (note has 136 lines)
165 0x0b -> 160x120, cropped
167 static const struct v4l2_pix_format vga_mode
[] = {
168 {160, 120, V4L2_PIX_FMT_KONICA420
, V4L2_FIELD_NONE
,
170 .sizeimage
= 160 * 136 * 3 / 2 + 960,
171 .colorspace
= V4L2_COLORSPACE_SRGB
,
173 {176, 144, V4L2_PIX_FMT_KONICA420
, V4L2_FIELD_NONE
,
175 .sizeimage
= 176 * 144 * 3 / 2 + 960,
176 .colorspace
= V4L2_COLORSPACE_SRGB
,
178 {320, 240, V4L2_PIX_FMT_KONICA420
, V4L2_FIELD_NONE
,
180 .sizeimage
= 320 * 240 * 3 / 2 + 960,
181 .colorspace
= V4L2_COLORSPACE_SRGB
,
185 static void sd_isoc_irq(struct urb
*urb
);
187 static void reg_w(struct gspca_dev
*gspca_dev
, u16 value
, u16 index
)
189 struct usb_device
*dev
= gspca_dev
->dev
;
192 if (gspca_dev
->usb_err
< 0)
194 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
196 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
203 err("reg_w err %d", ret
);
204 gspca_dev
->usb_err
= ret
;
208 static void reg_r(struct gspca_dev
*gspca_dev
, u16 value
, u16 index
)
210 struct usb_device
*dev
= gspca_dev
->dev
;
213 if (gspca_dev
->usb_err
< 0)
215 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
217 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
224 err("reg_w err %d", ret
);
225 gspca_dev
->usb_err
= ret
;
229 static void konica_stream_on(struct gspca_dev
*gspca_dev
)
231 reg_w(gspca_dev
, 1, 0x0b);
234 static void konica_stream_off(struct gspca_dev
*gspca_dev
)
236 reg_w(gspca_dev
, 0, 0x0b);
239 /* this function is called at probe time */
240 static int sd_config(struct gspca_dev
*gspca_dev
,
241 const struct usb_device_id
*id
)
243 struct sd
*sd
= (struct sd
*) gspca_dev
;
245 gspca_dev
->cam
.cam_mode
= vga_mode
;
246 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(vga_mode
);
247 gspca_dev
->cam
.no_urb_create
= 1;
248 /* The highest alt setting has an isoc packetsize of 0, so we
249 don't want to use it */
252 sd
->brightness
= BRIGHTNESS_DEFAULT
;
253 sd
->contrast
= CONTRAST_DEFAULT
;
254 sd
->saturation
= SATURATION_DEFAULT
;
255 sd
->whitebal
= WHITEBAL_DEFAULT
;
256 sd
->sharpness
= SHARPNESS_DEFAULT
;
261 /* this function is called at probe and resume time */
262 static int sd_init(struct gspca_dev
*gspca_dev
)
264 /* HDG not sure if these 2 reads are needed */
265 reg_r(gspca_dev
, 0, 0x10);
266 PDEBUG(D_PROBE
, "Reg 0x10 reads: %02x %02x",
267 gspca_dev
->usb_buf
[0], gspca_dev
->usb_buf
[1]);
268 reg_r(gspca_dev
, 0, 0x10);
269 PDEBUG(D_PROBE
, "Reg 0x10 reads: %02x %02x",
270 gspca_dev
->usb_buf
[0], gspca_dev
->usb_buf
[1]);
271 reg_w(gspca_dev
, 0, 0x0d);
276 static int sd_start(struct gspca_dev
*gspca_dev
)
278 struct sd
*sd
= (struct sd
*) gspca_dev
;
280 int i
, n
, packet_size
;
281 struct usb_host_interface
*alt
;
282 struct usb_interface
*intf
;
284 intf
= usb_ifnum_to_if(sd
->gspca_dev
.dev
, sd
->gspca_dev
.iface
);
285 alt
= usb_altnum_to_altsetting(intf
, sd
->gspca_dev
.alt
);
287 err("Couldn't get altsetting");
291 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
293 reg_w(gspca_dev
, sd
->brightness
, BRIGHTNESS_REG
);
294 reg_w(gspca_dev
, sd
->whitebal
, WHITEBAL_REG
);
295 reg_w(gspca_dev
, sd
->contrast
, CONTRAST_REG
);
296 reg_w(gspca_dev
, sd
->saturation
, SATURATION_REG
);
297 reg_w(gspca_dev
, sd
->sharpness
, SHARPNESS_REG
);
299 n
= gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
;
300 reg_w(gspca_dev
, n
, 0x08);
302 konica_stream_on(gspca_dev
);
304 if (gspca_dev
->usb_err
)
305 return gspca_dev
->usb_err
;
307 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
309 #error "Not enough URBs in the gspca table"
312 for (n
= 0; n
< 4; n
++) {
315 le16_to_cpu(alt
->endpoint
[i
].desc
.wMaxPacketSize
);
316 urb
= usb_alloc_urb(SD_NPKT
, GFP_KERNEL
);
318 err("usb_alloc_urb failed");
321 gspca_dev
->urb
[n
] = urb
;
322 urb
->transfer_buffer
= usb_alloc_coherent(gspca_dev
->dev
,
323 packet_size
* SD_NPKT
,
326 if (urb
->transfer_buffer
== NULL
) {
327 err("usb_buffer_alloc failed");
331 urb
->dev
= gspca_dev
->dev
;
332 urb
->context
= gspca_dev
;
333 urb
->transfer_buffer_length
= packet_size
* SD_NPKT
;
334 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
335 n
& 1 ? 0x81 : 0x82);
336 urb
->transfer_flags
= URB_ISO_ASAP
337 | URB_NO_TRANSFER_DMA_MAP
;
339 urb
->complete
= sd_isoc_irq
;
340 urb
->number_of_packets
= SD_NPKT
;
341 for (i
= 0; i
< SD_NPKT
; i
++) {
342 urb
->iso_frame_desc
[i
].length
= packet_size
;
343 urb
->iso_frame_desc
[i
].offset
= packet_size
* i
;
350 static void sd_stopN(struct gspca_dev
*gspca_dev
)
352 struct sd
*sd
= (struct sd
*) gspca_dev
;
354 konica_stream_off(gspca_dev
);
355 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
356 /* Don't keep the button in the pressed state "forever" if it was
357 pressed when streaming is stopped */
358 if (sd
->snapshot_pressed
) {
359 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 0);
360 input_sync(gspca_dev
->input_dev
);
361 sd
->snapshot_pressed
= 0;
366 /* reception of an URB */
367 static void sd_isoc_irq(struct urb
*urb
)
369 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
370 struct sd
*sd
= (struct sd
*) gspca_dev
;
371 struct urb
*data_urb
, *status_urb
;
375 PDEBUG(D_PACK
, "sd isoc irq");
376 if (!gspca_dev
->streaming
)
379 if (urb
->status
!= 0) {
380 if (urb
->status
== -ESHUTDOWN
)
381 return; /* disconnection */
383 if (gspca_dev
->frozen
)
386 PDEBUG(D_ERR
, "urb status: %d", urb
->status
);
387 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
389 err("resubmit urb error %d", st
);
393 /* if this is a data URB (ep 0x82), wait */
394 if (urb
->transfer_buffer_length
> 32) {
395 sd
->last_data_urb
= urb
;
400 data_urb
= sd
->last_data_urb
;
401 sd
->last_data_urb
= NULL
;
403 if (!data_urb
|| data_urb
->start_frame
!= status_urb
->start_frame
) {
404 PDEBUG(D_ERR
|D_PACK
, "lost sync on frames");
408 if (data_urb
->number_of_packets
!= status_urb
->number_of_packets
) {
410 "no packets does not match, data: %d, status: %d",
411 data_urb
->number_of_packets
,
412 status_urb
->number_of_packets
);
416 for (i
= 0; i
< status_urb
->number_of_packets
; i
++) {
417 if (data_urb
->iso_frame_desc
[i
].status
||
418 status_urb
->iso_frame_desc
[i
].status
) {
420 "pkt %d data-status %d, status-status %d", i
,
421 data_urb
->iso_frame_desc
[i
].status
,
422 status_urb
->iso_frame_desc
[i
].status
);
423 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
427 if (status_urb
->iso_frame_desc
[i
].actual_length
!= 1) {
429 "bad status packet length %d",
430 status_urb
->iso_frame_desc
[i
].actual_length
);
431 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
435 st
= *((u8
*)status_urb
->transfer_buffer
436 + status_urb
->iso_frame_desc
[i
].offset
);
438 data
= (u8
*)data_urb
->transfer_buffer
439 + data_urb
->iso_frame_desc
[i
].offset
;
441 /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
443 * bit 0 0: keep packet
444 * 1: drop packet (padding data)
446 * bit 4 0 button not clicked
448 * button is used to `take a picture' (in software)
451 gspca_frame_add(gspca_dev
, LAST_PACKET
, NULL
, 0);
452 gspca_frame_add(gspca_dev
, FIRST_PACKET
, NULL
, 0);
454 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
455 u8 button_state
= st
& 0x40 ? 1 : 0;
456 if (sd
->snapshot_pressed
!= button_state
) {
457 input_report_key(gspca_dev
->input_dev
,
460 input_sync(gspca_dev
->input_dev
);
461 sd
->snapshot_pressed
= button_state
;
467 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
,
468 data_urb
->iso_frame_desc
[i
].actual_length
);
473 st
= usb_submit_urb(data_urb
, GFP_ATOMIC
);
476 "usb_submit_urb(data_urb) ret %d", st
);
478 st
= usb_submit_urb(status_urb
, GFP_ATOMIC
);
480 err("usb_submit_urb(status_urb) ret %d", st
);
483 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
)
485 struct sd
*sd
= (struct sd
*) gspca_dev
;
487 sd
->brightness
= val
;
488 if (gspca_dev
->streaming
) {
489 konica_stream_off(gspca_dev
);
490 reg_w(gspca_dev
, sd
->brightness
, BRIGHTNESS_REG
);
491 konica_stream_on(gspca_dev
);
497 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
)
499 struct sd
*sd
= (struct sd
*) gspca_dev
;
501 *val
= sd
->brightness
;
506 static int sd_setcontrast(struct gspca_dev
*gspca_dev
, __s32 val
)
508 struct sd
*sd
= (struct sd
*) gspca_dev
;
511 if (gspca_dev
->streaming
) {
512 konica_stream_off(gspca_dev
);
513 reg_w(gspca_dev
, sd
->contrast
, CONTRAST_REG
);
514 konica_stream_on(gspca_dev
);
520 static int sd_getcontrast(struct gspca_dev
*gspca_dev
, __s32
*val
)
522 struct sd
*sd
= (struct sd
*) gspca_dev
;
529 static int sd_setsaturation(struct gspca_dev
*gspca_dev
, __s32 val
)
531 struct sd
*sd
= (struct sd
*) gspca_dev
;
533 sd
->saturation
= val
;
534 if (gspca_dev
->streaming
) {
535 konica_stream_off(gspca_dev
);
536 reg_w(gspca_dev
, sd
->saturation
, SATURATION_REG
);
537 konica_stream_on(gspca_dev
);
542 static int sd_getsaturation(struct gspca_dev
*gspca_dev
, __s32
*val
)
544 struct sd
*sd
= (struct sd
*) gspca_dev
;
546 *val
= sd
->saturation
;
551 static int sd_setwhitebal(struct gspca_dev
*gspca_dev
, __s32 val
)
553 struct sd
*sd
= (struct sd
*) gspca_dev
;
556 if (gspca_dev
->streaming
) {
557 konica_stream_off(gspca_dev
);
558 reg_w(gspca_dev
, sd
->whitebal
, WHITEBAL_REG
);
559 konica_stream_on(gspca_dev
);
564 static int sd_getwhitebal(struct gspca_dev
*gspca_dev
, __s32
*val
)
566 struct sd
*sd
= (struct sd
*) gspca_dev
;
573 static int sd_setsharpness(struct gspca_dev
*gspca_dev
, __s32 val
)
575 struct sd
*sd
= (struct sd
*) gspca_dev
;
578 if (gspca_dev
->streaming
) {
579 konica_stream_off(gspca_dev
);
580 reg_w(gspca_dev
, sd
->sharpness
, SHARPNESS_REG
);
581 konica_stream_on(gspca_dev
);
586 static int sd_getsharpness(struct gspca_dev
*gspca_dev
, __s32
*val
)
588 struct sd
*sd
= (struct sd
*) gspca_dev
;
590 *val
= sd
->sharpness
;
595 /* sub-driver description */
596 static const struct sd_desc sd_desc
= {
599 .nctrls
= ARRAY_SIZE(sd_ctrls
),
604 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
609 /* -- module initialisation -- */
610 static const struct usb_device_id device_table
[] = {
611 {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
614 MODULE_DEVICE_TABLE(usb
, device_table
);
616 /* -- device connect -- */
617 static int sd_probe(struct usb_interface
*intf
,
618 const struct usb_device_id
*id
)
620 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
624 static struct usb_driver sd_driver
= {
626 .id_table
= device_table
,
628 .disconnect
= gspca_disconnect
,
630 .suspend
= gspca_suspend
,
631 .resume
= gspca_resume
,
635 /* -- module insert / remove -- */
636 static int __init
sd_mod_init(void)
638 return usb_register(&sd_driver
);
640 static void __exit
sd_mod_exit(void)
642 usb_deregister(&sd_driver
);
645 module_init(sd_mod_init
);
646 module_exit(sd_mod_exit
);