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;
56 /* .priv is what goes to register 8 for this mode, known working values:
57 0x00 -> 176x144, cropped
58 0x01 -> 176x144, cropped
59 0x02 -> 176x144, cropped
60 0x03 -> 176x144, cropped
61 0x04 -> 176x144, binned
64 0x07 -> 160x120, cropped
65 0x08 -> 160x120, cropped
66 0x09 -> 160x120, binned (note has 136 lines)
67 0x0a -> 160x120, binned (note has 136 lines)
68 0x0b -> 160x120, cropped
70 static const struct v4l2_pix_format vga_mode[] = {
71 {160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
73 .sizeimage = 160 * 136 * 3 / 2 + 960,
74 .colorspace = V4L2_COLORSPACE_SRGB,
76 {176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
78 .sizeimage = 176 * 144 * 3 / 2 + 960,
79 .colorspace = V4L2_COLORSPACE_SRGB,
81 {320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
83 .sizeimage = 320 * 240 * 3 / 2 + 960,
84 .colorspace = V4L2_COLORSPACE_SRGB,
88 static void sd_isoc_irq(struct urb *urb);
90 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
92 struct usb_device *dev = gspca_dev->dev;
95 if (gspca_dev->usb_err < 0)
97 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
99 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
106 pr_err("reg_w err %d\n", ret);
107 gspca_dev->usb_err = ret;
111 static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
113 struct usb_device *dev = gspca_dev->dev;
116 if (gspca_dev->usb_err < 0)
118 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
120 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
127 pr_err("reg_w err %d\n", ret);
128 gspca_dev->usb_err = ret;
132 static void konica_stream_on(struct gspca_dev *gspca_dev)
134 reg_w(gspca_dev, 1, 0x0b);
137 static void konica_stream_off(struct gspca_dev *gspca_dev)
139 reg_w(gspca_dev, 0, 0x0b);
142 /* this function is called at probe time */
143 static int sd_config(struct gspca_dev *gspca_dev,
144 const struct usb_device_id *id)
146 gspca_dev->cam.cam_mode = vga_mode;
147 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
148 gspca_dev->cam.no_urb_create = 1;
153 /* this function is called at probe and resume time */
154 static int sd_init(struct gspca_dev *gspca_dev)
156 /* HDG not sure if these 2 reads are needed */
157 reg_r(gspca_dev, 0, 0x10);
158 PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x",
159 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
160 reg_r(gspca_dev, 0, 0x10);
161 PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x",
162 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
163 reg_w(gspca_dev, 0, 0x0d);
168 static int sd_start(struct gspca_dev *gspca_dev)
170 struct sd *sd = (struct sd *) gspca_dev;
172 int i, n, packet_size;
173 struct usb_host_interface *alt;
174 struct usb_interface *intf;
176 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
177 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
179 pr_err("Couldn't get altsetting\n");
183 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
185 n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
186 reg_w(gspca_dev, n, 0x08);
188 konica_stream_on(gspca_dev);
190 if (gspca_dev->usb_err)
191 return gspca_dev->usb_err;
193 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
195 #error "Not enough URBs in the gspca table"
198 for (n = 0; n < 4; n++) {
201 le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
202 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
204 pr_err("usb_alloc_urb failed\n");
207 gspca_dev->urb[n] = urb;
208 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
209 packet_size * SD_NPKT,
212 if (urb->transfer_buffer == NULL) {
213 pr_err("usb_buffer_alloc failed\n");
217 urb->dev = gspca_dev->dev;
218 urb->context = gspca_dev;
219 urb->transfer_buffer_length = packet_size * SD_NPKT;
220 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
221 n & 1 ? 0x81 : 0x82);
222 urb->transfer_flags = URB_ISO_ASAP
223 | URB_NO_TRANSFER_DMA_MAP;
225 urb->complete = sd_isoc_irq;
226 urb->number_of_packets = SD_NPKT;
227 for (i = 0; i < SD_NPKT; i++) {
228 urb->iso_frame_desc[i].length = packet_size;
229 urb->iso_frame_desc[i].offset = packet_size * i;
236 static void sd_stopN(struct gspca_dev *gspca_dev)
238 struct sd *sd = (struct sd *) gspca_dev;
240 konica_stream_off(gspca_dev);
241 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
242 /* Don't keep the button in the pressed state "forever" if it was
243 pressed when streaming is stopped */
244 if (sd->snapshot_pressed) {
245 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
246 input_sync(gspca_dev->input_dev);
247 sd->snapshot_pressed = 0;
252 /* reception of an URB */
253 static void sd_isoc_irq(struct urb *urb)
255 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
256 struct sd *sd = (struct sd *) gspca_dev;
257 struct urb *data_urb, *status_urb;
261 PDEBUG(D_PACK, "sd isoc irq");
262 if (!gspca_dev->streaming)
265 if (urb->status != 0) {
266 if (urb->status == -ESHUTDOWN)
267 return; /* disconnection */
269 if (gspca_dev->frozen)
272 PDEBUG(D_ERR, "urb status: %d", urb->status);
273 st = usb_submit_urb(urb, GFP_ATOMIC);
275 pr_err("resubmit urb error %d\n", st);
279 /* if this is a data URB (ep 0x82), wait */
280 if (urb->transfer_buffer_length > 32) {
281 sd->last_data_urb = urb;
286 data_urb = sd->last_data_urb;
287 sd->last_data_urb = NULL;
289 if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
290 PDEBUG(D_ERR|D_PACK, "lost sync on frames");
294 if (data_urb->number_of_packets != status_urb->number_of_packets) {
296 "no packets does not match, data: %d, status: %d",
297 data_urb->number_of_packets,
298 status_urb->number_of_packets);
302 for (i = 0; i < status_urb->number_of_packets; i++) {
303 if (data_urb->iso_frame_desc[i].status ||
304 status_urb->iso_frame_desc[i].status) {
306 "pkt %d data-status %d, status-status %d", i,
307 data_urb->iso_frame_desc[i].status,
308 status_urb->iso_frame_desc[i].status);
309 gspca_dev->last_packet_type = DISCARD_PACKET;
313 if (status_urb->iso_frame_desc[i].actual_length != 1) {
315 "bad status packet length %d",
316 status_urb->iso_frame_desc[i].actual_length);
317 gspca_dev->last_packet_type = DISCARD_PACKET;
321 st = *((u8 *)status_urb->transfer_buffer
322 + status_urb->iso_frame_desc[i].offset);
324 data = (u8 *)data_urb->transfer_buffer
325 + data_urb->iso_frame_desc[i].offset;
327 /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
329 * bit 0 0: keep packet
330 * 1: drop packet (padding data)
332 * bit 4 0 button not clicked
334 * button is used to `take a picture' (in software)
337 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
338 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
340 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
341 u8 button_state = st & 0x40 ? 1 : 0;
342 if (sd->snapshot_pressed != button_state) {
343 input_report_key(gspca_dev->input_dev,
346 input_sync(gspca_dev->input_dev);
347 sd->snapshot_pressed = button_state;
353 gspca_frame_add(gspca_dev, INTER_PACKET, data,
354 data_urb->iso_frame_desc[i].actual_length);
359 st = usb_submit_urb(data_urb, GFP_ATOMIC);
362 "usb_submit_urb(data_urb) ret %d", st);
364 st = usb_submit_urb(status_urb, GFP_ATOMIC);
366 pr_err("usb_submit_urb(status_urb) ret %d\n", st);
369 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
371 struct gspca_dev *gspca_dev =
372 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
374 gspca_dev->usb_err = 0;
376 if (!gspca_dev->streaming)
380 case V4L2_CID_BRIGHTNESS:
381 konica_stream_off(gspca_dev);
382 reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
383 konica_stream_on(gspca_dev);
385 case V4L2_CID_CONTRAST:
386 konica_stream_off(gspca_dev);
387 reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
388 konica_stream_on(gspca_dev);
390 case V4L2_CID_SATURATION:
391 konica_stream_off(gspca_dev);
392 reg_w(gspca_dev, ctrl->val, SATURATION_REG);
393 konica_stream_on(gspca_dev);
395 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
396 konica_stream_off(gspca_dev);
397 reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
398 konica_stream_on(gspca_dev);
400 case V4L2_CID_SHARPNESS:
401 konica_stream_off(gspca_dev);
402 reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
403 konica_stream_on(gspca_dev);
406 return gspca_dev->usb_err;
409 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
413 static int sd_init_controls(struct gspca_dev *gspca_dev)
415 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
417 gspca_dev->vdev.ctrl_handler = hdl;
418 v4l2_ctrl_handler_init(hdl, 5);
419 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
420 V4L2_CID_BRIGHTNESS, 0, 9, 1, 4);
421 /* Needs to be verified */
422 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
423 V4L2_CID_CONTRAST, 0, 9, 1, 4);
424 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
425 V4L2_CID_SATURATION, 0, 9, 1, 4);
426 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
427 V4L2_CID_WHITE_BALANCE_TEMPERATURE,
429 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
430 V4L2_CID_SHARPNESS, 0, 9, 1, 4);
433 pr_err("Could not initialize controls\n");
439 /* sub-driver description */
440 static const struct sd_desc sd_desc = {
444 .init_controls = sd_init_controls,
447 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
452 /* -- module initialisation -- */
453 static const struct usb_device_id device_table[] = {
454 {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
457 MODULE_DEVICE_TABLE(usb, device_table);
459 /* -- device connect -- */
460 static int sd_probe(struct usb_interface *intf,
461 const struct usb_device_id *id)
463 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
467 static struct usb_driver sd_driver = {
469 .id_table = device_table,
471 .disconnect = gspca_disconnect,
473 .suspend = gspca_suspend,
474 .resume = gspca_resume,
478 module_usb_driver(sd_driver);