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.
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #define MODULE_NAME "konica"
31 #include <linux/input.h>
34 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
35 MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
36 MODULE_LICENSE("GPL");
38 #define WHITEBAL_REG 0x01
39 #define BRIGHTNESS_REG 0x02
40 #define SHARPNESS_REG 0x03
41 #define CONTRAST_REG 0x04
42 #define SATURATION_REG 0x05
44 /* specific webcam descriptor */
46 struct gspca_dev gspca_dev
; /* !! must be the first item */
47 struct urb
*last_data_urb
;
52 /* .priv is what goes to register 8 for this mode, known working values:
53 0x00 -> 176x144, cropped
54 0x01 -> 176x144, cropped
55 0x02 -> 176x144, cropped
56 0x03 -> 176x144, cropped
57 0x04 -> 176x144, binned
60 0x07 -> 160x120, cropped
61 0x08 -> 160x120, cropped
62 0x09 -> 160x120, binned (note has 136 lines)
63 0x0a -> 160x120, binned (note has 136 lines)
64 0x0b -> 160x120, cropped
66 static const struct v4l2_pix_format vga_mode
[] = {
67 {160, 120, V4L2_PIX_FMT_KONICA420
, V4L2_FIELD_NONE
,
69 .sizeimage
= 160 * 136 * 3 / 2 + 960,
70 .colorspace
= V4L2_COLORSPACE_SRGB
,
72 {176, 144, V4L2_PIX_FMT_KONICA420
, V4L2_FIELD_NONE
,
74 .sizeimage
= 176 * 144 * 3 / 2 + 960,
75 .colorspace
= V4L2_COLORSPACE_SRGB
,
77 {320, 240, V4L2_PIX_FMT_KONICA420
, V4L2_FIELD_NONE
,
79 .sizeimage
= 320 * 240 * 3 / 2 + 960,
80 .colorspace
= V4L2_COLORSPACE_SRGB
,
84 static void sd_isoc_irq(struct urb
*urb
);
86 static void reg_w(struct gspca_dev
*gspca_dev
, u16 value
, u16 index
)
88 struct usb_device
*dev
= gspca_dev
->dev
;
91 if (gspca_dev
->usb_err
< 0)
93 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
95 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
102 pr_err("reg_w err writing %02x to %02x: %d\n",
104 gspca_dev
->usb_err
= ret
;
108 static void reg_r(struct gspca_dev
*gspca_dev
, u16 value
, u16 index
)
110 struct usb_device
*dev
= gspca_dev
->dev
;
113 if (gspca_dev
->usb_err
< 0)
115 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
117 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
124 pr_err("reg_r err %d\n", ret
);
125 gspca_dev
->usb_err
= ret
;
127 * Make sure the buffer is zeroed to avoid uninitialized
130 memset(gspca_dev
->usb_buf
, 0, 2);
134 static void konica_stream_on(struct gspca_dev
*gspca_dev
)
136 reg_w(gspca_dev
, 1, 0x0b);
139 static void konica_stream_off(struct gspca_dev
*gspca_dev
)
141 reg_w(gspca_dev
, 0, 0x0b);
144 /* this function is called at probe time */
145 static int sd_config(struct gspca_dev
*gspca_dev
,
146 const struct usb_device_id
*id
)
148 gspca_dev
->cam
.cam_mode
= vga_mode
;
149 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(vga_mode
);
150 gspca_dev
->cam
.no_urb_create
= 1;
155 /* this function is called at probe and resume time */
156 static int sd_init(struct gspca_dev
*gspca_dev
)
161 * The konica needs a freaking large time to "boot" (approx 6.5 sec.),
162 * and does not want to be bothered while doing so :|
163 * Register 0x10 counts from 1 - 3, with 3 being "ready"
166 for (i
= 0; i
< 20; i
++) {
167 reg_r(gspca_dev
, 0, 0x10);
168 if (gspca_dev
->usb_buf
[0] == 3)
172 reg_w(gspca_dev
, 0, 0x0d);
174 return gspca_dev
->usb_err
;
177 static int sd_start(struct gspca_dev
*gspca_dev
)
179 struct sd
*sd
= (struct sd
*) gspca_dev
;
181 int i
, n
, packet_size
;
182 struct usb_host_interface
*alt
;
183 struct usb_interface
*intf
;
185 intf
= usb_ifnum_to_if(sd
->gspca_dev
.dev
, sd
->gspca_dev
.iface
);
186 alt
= usb_altnum_to_altsetting(intf
, sd
->gspca_dev
.alt
);
188 pr_err("Couldn't get altsetting\n");
192 if (alt
->desc
.bNumEndpoints
< 2)
195 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
197 n
= gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
;
198 reg_w(gspca_dev
, n
, 0x08);
200 konica_stream_on(gspca_dev
);
202 if (gspca_dev
->usb_err
)
203 return gspca_dev
->usb_err
;
205 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
207 #error "Not enough URBs in the gspca table"
210 for (n
= 0; n
< 4; n
++) {
213 le16_to_cpu(alt
->endpoint
[i
].desc
.wMaxPacketSize
);
214 urb
= usb_alloc_urb(SD_NPKT
, GFP_KERNEL
);
217 gspca_dev
->urb
[n
] = urb
;
218 urb
->transfer_buffer
= usb_alloc_coherent(gspca_dev
->dev
,
219 packet_size
* SD_NPKT
,
222 if (urb
->transfer_buffer
== NULL
) {
223 pr_err("usb_buffer_alloc failed\n");
227 urb
->dev
= gspca_dev
->dev
;
228 urb
->context
= gspca_dev
;
229 urb
->transfer_buffer_length
= packet_size
* SD_NPKT
;
230 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
231 n
& 1 ? 0x81 : 0x82);
232 urb
->transfer_flags
= URB_ISO_ASAP
233 | URB_NO_TRANSFER_DMA_MAP
;
235 urb
->complete
= sd_isoc_irq
;
236 urb
->number_of_packets
= SD_NPKT
;
237 for (i
= 0; i
< SD_NPKT
; i
++) {
238 urb
->iso_frame_desc
[i
].length
= packet_size
;
239 urb
->iso_frame_desc
[i
].offset
= packet_size
* i
;
246 static void sd_stopN(struct gspca_dev
*gspca_dev
)
248 struct sd
*sd __maybe_unused
= (struct sd
*) gspca_dev
;
250 konica_stream_off(gspca_dev
);
251 #if IS_ENABLED(CONFIG_INPUT)
252 /* Don't keep the button in the pressed state "forever" if it was
253 pressed when streaming is stopped */
254 if (sd
->snapshot_pressed
) {
255 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 0);
256 input_sync(gspca_dev
->input_dev
);
257 sd
->snapshot_pressed
= 0;
262 /* reception of an URB */
263 static void sd_isoc_irq(struct urb
*urb
)
265 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
266 struct sd
*sd
= (struct sd
*) gspca_dev
;
267 struct urb
*data_urb
, *status_urb
;
271 gspca_dbg(gspca_dev
, D_PACK
, "sd isoc irq\n");
272 if (!gspca_dev
->streaming
)
275 if (urb
->status
!= 0) {
276 if (urb
->status
== -ESHUTDOWN
)
277 return; /* disconnection */
279 if (gspca_dev
->frozen
)
282 gspca_err(gspca_dev
, "urb status: %d\n", urb
->status
);
283 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
285 pr_err("resubmit urb error %d\n", st
);
289 /* if this is a data URB (ep 0x82), wait */
290 if (urb
->transfer_buffer_length
> 32) {
291 sd
->last_data_urb
= urb
;
296 data_urb
= sd
->last_data_urb
;
297 sd
->last_data_urb
= NULL
;
299 if (!data_urb
|| data_urb
->start_frame
!= status_urb
->start_frame
) {
300 gspca_err(gspca_dev
, "lost sync on frames\n");
304 if (data_urb
->number_of_packets
!= status_urb
->number_of_packets
) {
305 gspca_err(gspca_dev
, "no packets does not match, data: %d, status: %d\n",
306 data_urb
->number_of_packets
,
307 status_urb
->number_of_packets
);
311 for (i
= 0; i
< status_urb
->number_of_packets
; i
++) {
312 if (data_urb
->iso_frame_desc
[i
].status
||
313 status_urb
->iso_frame_desc
[i
].status
) {
314 gspca_err(gspca_dev
, "pkt %d data-status %d, status-status %d\n",
316 data_urb
->iso_frame_desc
[i
].status
,
317 status_urb
->iso_frame_desc
[i
].status
);
318 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
322 if (status_urb
->iso_frame_desc
[i
].actual_length
!= 1) {
323 gspca_err(gspca_dev
, "bad status packet length %d\n",
324 status_urb
->iso_frame_desc
[i
].actual_length
);
325 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
329 st
= *((u8
*)status_urb
->transfer_buffer
330 + status_urb
->iso_frame_desc
[i
].offset
);
332 data
= (u8
*)data_urb
->transfer_buffer
333 + data_urb
->iso_frame_desc
[i
].offset
;
335 /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
337 * bit 0 0: keep packet
338 * 1: drop packet (padding data)
340 * bit 4 0 button not clicked
342 * button is used to `take a picture' (in software)
345 gspca_frame_add(gspca_dev
, LAST_PACKET
, NULL
, 0);
346 gspca_frame_add(gspca_dev
, FIRST_PACKET
, NULL
, 0);
348 #if IS_ENABLED(CONFIG_INPUT)
349 u8 button_state
= st
& 0x40 ? 1 : 0;
350 if (sd
->snapshot_pressed
!= button_state
) {
351 input_report_key(gspca_dev
->input_dev
,
354 input_sync(gspca_dev
->input_dev
);
355 sd
->snapshot_pressed
= button_state
;
361 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
,
362 data_urb
->iso_frame_desc
[i
].actual_length
);
367 st
= usb_submit_urb(data_urb
, GFP_ATOMIC
);
369 gspca_err(gspca_dev
, "usb_submit_urb(data_urb) ret %d\n",
372 st
= usb_submit_urb(status_urb
, GFP_ATOMIC
);
374 gspca_err(gspca_dev
, "usb_submit_urb(status_urb) ret %d\n", st
);
377 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
379 struct gspca_dev
*gspca_dev
=
380 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
382 gspca_dev
->usb_err
= 0;
384 if (!gspca_dev
->streaming
)
388 case V4L2_CID_BRIGHTNESS
:
389 konica_stream_off(gspca_dev
);
390 reg_w(gspca_dev
, ctrl
->val
, BRIGHTNESS_REG
);
391 konica_stream_on(gspca_dev
);
393 case V4L2_CID_CONTRAST
:
394 konica_stream_off(gspca_dev
);
395 reg_w(gspca_dev
, ctrl
->val
, CONTRAST_REG
);
396 konica_stream_on(gspca_dev
);
398 case V4L2_CID_SATURATION
:
399 konica_stream_off(gspca_dev
);
400 reg_w(gspca_dev
, ctrl
->val
, SATURATION_REG
);
401 konica_stream_on(gspca_dev
);
403 case V4L2_CID_WHITE_BALANCE_TEMPERATURE
:
404 konica_stream_off(gspca_dev
);
405 reg_w(gspca_dev
, ctrl
->val
, WHITEBAL_REG
);
406 konica_stream_on(gspca_dev
);
408 case V4L2_CID_SHARPNESS
:
409 konica_stream_off(gspca_dev
);
410 reg_w(gspca_dev
, ctrl
->val
, SHARPNESS_REG
);
411 konica_stream_on(gspca_dev
);
414 return gspca_dev
->usb_err
;
417 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
421 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
423 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
425 gspca_dev
->vdev
.ctrl_handler
= hdl
;
426 v4l2_ctrl_handler_init(hdl
, 5);
427 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
428 V4L2_CID_BRIGHTNESS
, 0, 9, 1, 4);
429 /* Needs to be verified */
430 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
431 V4L2_CID_CONTRAST
, 0, 9, 1, 4);
432 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
433 V4L2_CID_SATURATION
, 0, 9, 1, 4);
434 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
435 V4L2_CID_WHITE_BALANCE_TEMPERATURE
,
437 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
438 V4L2_CID_SHARPNESS
, 0, 9, 1, 4);
441 pr_err("Could not initialize controls\n");
447 /* sub-driver description */
448 static const struct sd_desc sd_desc
= {
452 .init_controls
= sd_init_controls
,
455 #if IS_ENABLED(CONFIG_INPUT)
460 /* -- module initialisation -- */
461 static const struct usb_device_id device_table
[] = {
462 {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
465 MODULE_DEVICE_TABLE(usb
, device_table
);
467 /* -- device connect -- */
468 static int sd_probe(struct usb_interface
*intf
,
469 const struct usb_device_id
*id
)
471 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
475 static struct usb_driver sd_driver
= {
477 .id_table
= device_table
,
479 .disconnect
= gspca_disconnect
,
481 .suspend
= gspca_suspend
,
482 .resume
= gspca_resume
,
483 .reset_resume
= gspca_resume
,
487 module_usb_driver(sd_driver
);