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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #define MODULE_NAME "konica"
35 #include <linux/input.h>
38 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
39 MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
40 MODULE_LICENSE("GPL");
42 #define WHITEBAL_REG 0x01
43 #define BRIGHTNESS_REG 0x02
44 #define SHARPNESS_REG 0x03
45 #define CONTRAST_REG 0x04
46 #define SATURATION_REG 0x05
48 /* specific webcam descriptor */
50 struct gspca_dev gspca_dev; /* !! must be the first item */
51 struct urb *last_data_urb;
60 /* V4L2 controls supported by the driver */
61 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
62 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
63 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
64 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
65 static int sd_setsaturation(struct gspca_dev *gspca_dev, __s32 val);
66 static int sd_getsaturation(struct gspca_dev *gspca_dev, __s32 *val);
67 static int sd_setwhitebal(struct gspca_dev *gspca_dev, __s32 val);
68 static int sd_getwhitebal(struct gspca_dev *gspca_dev, __s32 *val);
69 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
70 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
72 static const struct ctrl sd_ctrls[] = {
73 #define SD_BRIGHTNESS 0
76 .id = V4L2_CID_BRIGHTNESS,
77 .type = V4L2_CTRL_TYPE_INTEGER,
82 #define BRIGHTNESS_DEFAULT 4
83 .default_value = BRIGHTNESS_DEFAULT,
86 .set = sd_setbrightness,
87 .get = sd_getbrightness,
92 .id = V4L2_CID_CONTRAST,
93 .type = V4L2_CTRL_TYPE_INTEGER,
98 #define CONTRAST_DEFAULT 10
99 .default_value = CONTRAST_DEFAULT,
102 .set = sd_setcontrast,
103 .get = sd_getcontrast,
105 #define SD_SATURATION 2
108 .id = V4L2_CID_SATURATION,
109 .type = V4L2_CTRL_TYPE_INTEGER,
110 .name = "Saturation",
114 #define SATURATION_DEFAULT 4
115 .default_value = SATURATION_DEFAULT,
118 .set = sd_setsaturation,
119 .get = sd_getsaturation,
121 #define SD_WHITEBAL 3
124 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
125 .type = V4L2_CTRL_TYPE_INTEGER,
126 .name = "White Balance",
130 #define WHITEBAL_DEFAULT 25
131 .default_value = WHITEBAL_DEFAULT,
134 .set = sd_setwhitebal,
135 .get = sd_getwhitebal,
137 #define SD_SHARPNESS 4
140 .id = V4L2_CID_SHARPNESS,
141 .type = V4L2_CTRL_TYPE_INTEGER,
146 #define SHARPNESS_DEFAULT 4
147 .default_value = SHARPNESS_DEFAULT,
150 .set = sd_setsharpness,
151 .get = sd_getsharpness,
155 /* .priv is what goes to register 8 for this mode, known working values:
156 0x00 -> 176x144, cropped
157 0x01 -> 176x144, cropped
158 0x02 -> 176x144, cropped
159 0x03 -> 176x144, cropped
160 0x04 -> 176x144, binned
163 0x07 -> 160x120, cropped
164 0x08 -> 160x120, cropped
165 0x09 -> 160x120, binned (note has 136 lines)
166 0x0a -> 160x120, binned (note has 136 lines)
167 0x0b -> 160x120, cropped
169 static const struct v4l2_pix_format vga_mode[] = {
170 {160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
172 .sizeimage = 160 * 136 * 3 / 2 + 960,
173 .colorspace = V4L2_COLORSPACE_SRGB,
175 {176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
177 .sizeimage = 176 * 144 * 3 / 2 + 960,
178 .colorspace = V4L2_COLORSPACE_SRGB,
180 {320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
182 .sizeimage = 320 * 240 * 3 / 2 + 960,
183 .colorspace = V4L2_COLORSPACE_SRGB,
187 static void sd_isoc_irq(struct urb *urb);
189 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
191 struct usb_device *dev = gspca_dev->dev;
194 if (gspca_dev->usb_err < 0)
196 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
198 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
205 pr_err("reg_w err %d\n", ret);
206 gspca_dev->usb_err = ret;
210 static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
212 struct usb_device *dev = gspca_dev->dev;
215 if (gspca_dev->usb_err < 0)
217 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
219 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
226 pr_err("reg_w err %d\n", ret);
227 gspca_dev->usb_err = ret;
231 static void konica_stream_on(struct gspca_dev *gspca_dev)
233 reg_w(gspca_dev, 1, 0x0b);
236 static void konica_stream_off(struct gspca_dev *gspca_dev)
238 reg_w(gspca_dev, 0, 0x0b);
241 /* this function is called at probe time */
242 static int sd_config(struct gspca_dev *gspca_dev,
243 const struct usb_device_id *id)
245 struct sd *sd = (struct sd *) gspca_dev;
247 gspca_dev->cam.cam_mode = vga_mode;
248 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
249 gspca_dev->cam.no_urb_create = 1;
250 /* The highest alt setting has an isoc packetsize of 0, so we
251 don't want to use it */
254 sd->brightness = BRIGHTNESS_DEFAULT;
255 sd->contrast = CONTRAST_DEFAULT;
256 sd->saturation = SATURATION_DEFAULT;
257 sd->whitebal = WHITEBAL_DEFAULT;
258 sd->sharpness = SHARPNESS_DEFAULT;
263 /* this function is called at probe and resume time */
264 static int sd_init(struct gspca_dev *gspca_dev)
266 /* HDG not sure if these 2 reads are needed */
267 reg_r(gspca_dev, 0, 0x10);
268 PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x",
269 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
270 reg_r(gspca_dev, 0, 0x10);
271 PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x",
272 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
273 reg_w(gspca_dev, 0, 0x0d);
278 static int sd_start(struct gspca_dev *gspca_dev)
280 struct sd *sd = (struct sd *) gspca_dev;
282 int i, n, packet_size;
283 struct usb_host_interface *alt;
284 struct usb_interface *intf;
286 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
287 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
289 pr_err("Couldn't get altsetting\n");
293 if (alt->desc.bNumEndpoints < 2)
296 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
298 reg_w(gspca_dev, sd->brightness, BRIGHTNESS_REG);
299 reg_w(gspca_dev, sd->whitebal, WHITEBAL_REG);
300 reg_w(gspca_dev, sd->contrast, CONTRAST_REG);
301 reg_w(gspca_dev, sd->saturation, SATURATION_REG);
302 reg_w(gspca_dev, sd->sharpness, SHARPNESS_REG);
304 n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
305 reg_w(gspca_dev, n, 0x08);
307 konica_stream_on(gspca_dev);
309 if (gspca_dev->usb_err)
310 return gspca_dev->usb_err;
312 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
314 #error "Not enough URBs in the gspca table"
317 for (n = 0; n < 4; n++) {
320 le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
321 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
323 pr_err("usb_alloc_urb failed\n");
326 gspca_dev->urb[n] = urb;
327 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
328 packet_size * SD_NPKT,
331 if (urb->transfer_buffer == NULL) {
332 pr_err("usb_buffer_alloc failed\n");
336 urb->dev = gspca_dev->dev;
337 urb->context = gspca_dev;
338 urb->transfer_buffer_length = packet_size * SD_NPKT;
339 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
340 n & 1 ? 0x81 : 0x82);
341 urb->transfer_flags = URB_ISO_ASAP
342 | URB_NO_TRANSFER_DMA_MAP;
344 urb->complete = sd_isoc_irq;
345 urb->number_of_packets = SD_NPKT;
346 for (i = 0; i < SD_NPKT; i++) {
347 urb->iso_frame_desc[i].length = packet_size;
348 urb->iso_frame_desc[i].offset = packet_size * i;
355 static void sd_stopN(struct gspca_dev *gspca_dev)
357 struct sd *sd = (struct sd *) gspca_dev;
359 konica_stream_off(gspca_dev);
360 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
361 /* Don't keep the button in the pressed state "forever" if it was
362 pressed when streaming is stopped */
363 if (sd->snapshot_pressed) {
364 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
365 input_sync(gspca_dev->input_dev);
366 sd->snapshot_pressed = 0;
371 /* reception of an URB */
372 static void sd_isoc_irq(struct urb *urb)
374 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
375 struct sd *sd = (struct sd *) gspca_dev;
376 struct urb *data_urb, *status_urb;
380 PDEBUG(D_PACK, "sd isoc irq");
381 if (!gspca_dev->streaming)
384 if (urb->status != 0) {
385 if (urb->status == -ESHUTDOWN)
386 return; /* disconnection */
388 if (gspca_dev->frozen)
391 PDEBUG(D_ERR, "urb status: %d", urb->status);
392 st = usb_submit_urb(urb, GFP_ATOMIC);
394 pr_err("resubmit urb error %d\n", st);
398 /* if this is a data URB (ep 0x82), wait */
399 if (urb->transfer_buffer_length > 32) {
400 sd->last_data_urb = urb;
405 data_urb = sd->last_data_urb;
406 sd->last_data_urb = NULL;
408 if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
409 PDEBUG(D_ERR|D_PACK, "lost sync on frames");
413 if (data_urb->number_of_packets != status_urb->number_of_packets) {
415 "no packets does not match, data: %d, status: %d",
416 data_urb->number_of_packets,
417 status_urb->number_of_packets);
421 for (i = 0; i < status_urb->number_of_packets; i++) {
422 if (data_urb->iso_frame_desc[i].status ||
423 status_urb->iso_frame_desc[i].status) {
425 "pkt %d data-status %d, status-status %d", i,
426 data_urb->iso_frame_desc[i].status,
427 status_urb->iso_frame_desc[i].status);
428 gspca_dev->last_packet_type = DISCARD_PACKET;
432 if (status_urb->iso_frame_desc[i].actual_length != 1) {
434 "bad status packet length %d",
435 status_urb->iso_frame_desc[i].actual_length);
436 gspca_dev->last_packet_type = DISCARD_PACKET;
440 st = *((u8 *)status_urb->transfer_buffer
441 + status_urb->iso_frame_desc[i].offset);
443 data = (u8 *)data_urb->transfer_buffer
444 + data_urb->iso_frame_desc[i].offset;
446 /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
448 * bit 0 0: keep packet
449 * 1: drop packet (padding data)
451 * bit 4 0 button not clicked
453 * button is used to `take a picture' (in software)
456 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
457 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
459 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
460 u8 button_state = st & 0x40 ? 1 : 0;
461 if (sd->snapshot_pressed != button_state) {
462 input_report_key(gspca_dev->input_dev,
465 input_sync(gspca_dev->input_dev);
466 sd->snapshot_pressed = button_state;
472 gspca_frame_add(gspca_dev, INTER_PACKET, data,
473 data_urb->iso_frame_desc[i].actual_length);
478 st = usb_submit_urb(data_urb, GFP_ATOMIC);
481 "usb_submit_urb(data_urb) ret %d", st);
483 st = usb_submit_urb(status_urb, GFP_ATOMIC);
485 pr_err("usb_submit_urb(status_urb) ret %d\n", st);
488 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
490 struct sd *sd = (struct sd *) gspca_dev;
492 sd->brightness = val;
493 if (gspca_dev->streaming) {
494 konica_stream_off(gspca_dev);
495 reg_w(gspca_dev, sd->brightness, BRIGHTNESS_REG);
496 konica_stream_on(gspca_dev);
502 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
504 struct sd *sd = (struct sd *) gspca_dev;
506 *val = sd->brightness;
511 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
513 struct sd *sd = (struct sd *) gspca_dev;
516 if (gspca_dev->streaming) {
517 konica_stream_off(gspca_dev);
518 reg_w(gspca_dev, sd->contrast, CONTRAST_REG);
519 konica_stream_on(gspca_dev);
525 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
527 struct sd *sd = (struct sd *) gspca_dev;
534 static int sd_setsaturation(struct gspca_dev *gspca_dev, __s32 val)
536 struct sd *sd = (struct sd *) gspca_dev;
538 sd->saturation = val;
539 if (gspca_dev->streaming) {
540 konica_stream_off(gspca_dev);
541 reg_w(gspca_dev, sd->saturation, SATURATION_REG);
542 konica_stream_on(gspca_dev);
547 static int sd_getsaturation(struct gspca_dev *gspca_dev, __s32 *val)
549 struct sd *sd = (struct sd *) gspca_dev;
551 *val = sd->saturation;
556 static int sd_setwhitebal(struct gspca_dev *gspca_dev, __s32 val)
558 struct sd *sd = (struct sd *) gspca_dev;
561 if (gspca_dev->streaming) {
562 konica_stream_off(gspca_dev);
563 reg_w(gspca_dev, sd->whitebal, WHITEBAL_REG);
564 konica_stream_on(gspca_dev);
569 static int sd_getwhitebal(struct gspca_dev *gspca_dev, __s32 *val)
571 struct sd *sd = (struct sd *) gspca_dev;
578 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
580 struct sd *sd = (struct sd *) gspca_dev;
583 if (gspca_dev->streaming) {
584 konica_stream_off(gspca_dev);
585 reg_w(gspca_dev, sd->sharpness, SHARPNESS_REG);
586 konica_stream_on(gspca_dev);
591 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
593 struct sd *sd = (struct sd *) gspca_dev;
595 *val = sd->sharpness;
600 /* sub-driver description */
601 static const struct sd_desc sd_desc = {
604 .nctrls = ARRAY_SIZE(sd_ctrls),
609 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
614 /* -- module initialisation -- */
615 static const struct usb_device_id device_table[] = {
616 {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
619 MODULE_DEVICE_TABLE(usb, device_table);
621 /* -- device connect -- */
622 static int sd_probe(struct usb_interface *intf,
623 const struct usb_device_id *id)
625 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
629 static struct usb_driver sd_driver = {
631 .id_table = device_table,
633 .disconnect = gspca_disconnect,
635 .suspend = gspca_suspend,
636 .resume = gspca_resume,
640 /* -- module insert / remove -- */
641 static int __init sd_mod_init(void)
643 return usb_register(&sd_driver);
645 static void __exit sd_mod_exit(void)
647 usb_deregister(&sd_driver);
650 module_init(sd_mod_init);
651 module_exit(sd_mod_exit);