blob: bbf91e07e38b759cb428e254a92c8f79e61b8ca6 [file] [log] [blame]
Hans de Goedeb517af72010-09-05 16:30:30 -03001/*
2 * Driver for USB webcams based on Konica chipset. This
3 * chipset is used in Intel YC76 camera.
4 *
5 * Copyright (C) 2010 Hans de Goede <hdegoede@redhat.com>
6 *
7 * Based on the usbvideo v4l1 konicawc driver which is:
8 *
9 * Copyright (C) 2002 Simon Evans <spse@secret.org.uk>
10 *
11 * The code for making gspca work with a webcam with 2 isoc endpoints was
12 * taken from the benq gspca subdriver which is:
13 *
14 * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
15 *
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
19 * any later version.
20 *
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.
25 *
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
29 */
30
Joe Perches133a9fe2011-08-21 19:56:57 -030031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Hans de Goedeb517af72010-09-05 16:30:30 -030033#define MODULE_NAME "konica"
34
35#include <linux/input.h>
36#include "gspca.h"
37
38MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
39MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
40MODULE_LICENSE("GPL");
41
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
47
48/* specific webcam descriptor */
49struct sd {
50 struct gspca_dev gspca_dev; /* !! must be the first item */
51 struct urb *last_data_urb;
52 u8 snapshot_pressed;
Hans de Goedeb517af72010-09-05 16:30:30 -030053};
54
Hans de Goedeb517af72010-09-05 16:30:30 -030055
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
62 0x05 -> 320x240
63 0x06 -> 320x240
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
69*/
70static const struct v4l2_pix_format vga_mode[] = {
71 {160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
72 .bytesperline = 160,
73 .sizeimage = 160 * 136 * 3 / 2 + 960,
74 .colorspace = V4L2_COLORSPACE_SRGB,
75 .priv = 0x0a},
76 {176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
77 .bytesperline = 176,
78 .sizeimage = 176 * 144 * 3 / 2 + 960,
79 .colorspace = V4L2_COLORSPACE_SRGB,
80 .priv = 0x04},
81 {320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
82 .bytesperline = 320,
83 .sizeimage = 320 * 240 * 3 / 2 + 960,
84 .colorspace = V4L2_COLORSPACE_SRGB,
85 .priv = 0x05},
86};
87
88static void sd_isoc_irq(struct urb *urb);
89
90static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
91{
92 struct usb_device *dev = gspca_dev->dev;
93 int ret;
94
95 if (gspca_dev->usb_err < 0)
96 return;
97 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
98 0x02,
99 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
100 value,
101 index,
102 NULL,
103 0,
104 1000);
105 if (ret < 0) {
Hans de Goedee1636092012-06-30 09:03:21 -0300106 pr_err("reg_w err writing %02x to %02x: %d\n",
107 value, index, ret);
Hans de Goedeb517af72010-09-05 16:30:30 -0300108 gspca_dev->usb_err = ret;
109 }
110}
111
112static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
113{
114 struct usb_device *dev = gspca_dev->dev;
115 int ret;
116
117 if (gspca_dev->usb_err < 0)
118 return;
119 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
120 0x03,
121 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
122 value,
123 index,
124 gspca_dev->usb_buf,
125 2,
126 1000);
127 if (ret < 0) {
Hans de Goedee1636092012-06-30 09:03:21 -0300128 pr_err("reg_r err %d\n", ret);
Hans de Goedeb517af72010-09-05 16:30:30 -0300129 gspca_dev->usb_err = ret;
130 }
131}
132
133static void konica_stream_on(struct gspca_dev *gspca_dev)
134{
135 reg_w(gspca_dev, 1, 0x0b);
136}
137
138static void konica_stream_off(struct gspca_dev *gspca_dev)
139{
140 reg_w(gspca_dev, 0, 0x0b);
141}
142
143/* this function is called at probe time */
144static int sd_config(struct gspca_dev *gspca_dev,
145 const struct usb_device_id *id)
146{
Hans de Goedeb517af72010-09-05 16:30:30 -0300147 gspca_dev->cam.cam_mode = vga_mode;
148 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
149 gspca_dev->cam.no_urb_create = 1;
Hans de Goedeb517af72010-09-05 16:30:30 -0300150
Hans de Goedeb517af72010-09-05 16:30:30 -0300151 return 0;
152}
153
154/* this function is called at probe and resume time */
155static int sd_init(struct gspca_dev *gspca_dev)
156{
Hans de Goedee1636092012-06-30 09:03:21 -0300157 int i;
158
159 /*
160 * The konica needs a freaking large time to "boot" (approx 6.5 sec.),
161 * and does not want to be bothered while doing so :|
162 * Register 0x10 counts from 1 - 3, with 3 being "ready"
163 */
164 msleep(6000);
165 for (i = 0; i < 20; i++) {
166 reg_r(gspca_dev, 0, 0x10);
167 if (gspca_dev->usb_buf[0] == 3)
168 break;
169 msleep(100);
170 }
Hans de Goedeb517af72010-09-05 16:30:30 -0300171 reg_w(gspca_dev, 0, 0x0d);
172
Hans de Goedee1636092012-06-30 09:03:21 -0300173 return gspca_dev->usb_err;
Hans de Goedeb517af72010-09-05 16:30:30 -0300174}
175
176static int sd_start(struct gspca_dev *gspca_dev)
177{
178 struct sd *sd = (struct sd *) gspca_dev;
179 struct urb *urb;
180 int i, n, packet_size;
181 struct usb_host_interface *alt;
182 struct usb_interface *intf;
183
184 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
185 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
186 if (!alt) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300187 pr_err("Couldn't get altsetting\n");
Hans de Goedeb517af72010-09-05 16:30:30 -0300188 return -EIO;
189 }
190
191 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
192
Hans de Goedeb517af72010-09-05 16:30:30 -0300193 n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
194 reg_w(gspca_dev, n, 0x08);
195
196 konica_stream_on(gspca_dev);
197
198 if (gspca_dev->usb_err)
199 return gspca_dev->usb_err;
200
201 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
202#if MAX_NURBS < 4
203#error "Not enough URBs in the gspca table"
204#endif
205#define SD_NPKT 32
206 for (n = 0; n < 4; n++) {
207 i = n & 1 ? 0 : 1;
208 packet_size =
209 le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
210 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
211 if (!urb) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300212 pr_err("usb_alloc_urb failed\n");
Hans de Goedeb517af72010-09-05 16:30:30 -0300213 return -ENOMEM;
214 }
215 gspca_dev->urb[n] = urb;
Mauro Carvalho Chehab76e241d2010-09-06 13:57:22 -0300216 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
Hans de Goedeb517af72010-09-05 16:30:30 -0300217 packet_size * SD_NPKT,
218 GFP_KERNEL,
219 &urb->transfer_dma);
220 if (urb->transfer_buffer == NULL) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300221 pr_err("usb_buffer_alloc failed\n");
Hans de Goedeb517af72010-09-05 16:30:30 -0300222 return -ENOMEM;
223 }
224
225 urb->dev = gspca_dev->dev;
226 urb->context = gspca_dev;
227 urb->transfer_buffer_length = packet_size * SD_NPKT;
228 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
229 n & 1 ? 0x81 : 0x82);
230 urb->transfer_flags = URB_ISO_ASAP
231 | URB_NO_TRANSFER_DMA_MAP;
232 urb->interval = 1;
233 urb->complete = sd_isoc_irq;
234 urb->number_of_packets = SD_NPKT;
235 for (i = 0; i < SD_NPKT; i++) {
236 urb->iso_frame_desc[i].length = packet_size;
237 urb->iso_frame_desc[i].offset = packet_size * i;
238 }
239 }
240
241 return 0;
242}
243
244static void sd_stopN(struct gspca_dev *gspca_dev)
245{
246 struct sd *sd = (struct sd *) gspca_dev;
247
248 konica_stream_off(gspca_dev);
Jean-François Moine28566432010-10-01 07:33:26 -0300249#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goedeb517af72010-09-05 16:30:30 -0300250 /* Don't keep the button in the pressed state "forever" if it was
251 pressed when streaming is stopped */
252 if (sd->snapshot_pressed) {
253 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
254 input_sync(gspca_dev->input_dev);
255 sd->snapshot_pressed = 0;
256 }
257#endif
258}
259
260/* reception of an URB */
261static void sd_isoc_irq(struct urb *urb)
262{
263 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
264 struct sd *sd = (struct sd *) gspca_dev;
265 struct urb *data_urb, *status_urb;
266 u8 *data;
267 int i, st;
268
269 PDEBUG(D_PACK, "sd isoc irq");
270 if (!gspca_dev->streaming)
271 return;
272
273 if (urb->status != 0) {
274 if (urb->status == -ESHUTDOWN)
275 return; /* disconnection */
276#ifdef CONFIG_PM
277 if (gspca_dev->frozen)
278 return;
279#endif
280 PDEBUG(D_ERR, "urb status: %d", urb->status);
281 st = usb_submit_urb(urb, GFP_ATOMIC);
282 if (st < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300283 pr_err("resubmit urb error %d\n", st);
Hans de Goedeb517af72010-09-05 16:30:30 -0300284 return;
285 }
286
287 /* if this is a data URB (ep 0x82), wait */
288 if (urb->transfer_buffer_length > 32) {
289 sd->last_data_urb = urb;
290 return;
291 }
292
293 status_urb = urb;
294 data_urb = sd->last_data_urb;
295 sd->last_data_urb = NULL;
296
297 if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
298 PDEBUG(D_ERR|D_PACK, "lost sync on frames");
299 goto resubmit;
300 }
301
302 if (data_urb->number_of_packets != status_urb->number_of_packets) {
303 PDEBUG(D_ERR|D_PACK,
304 "no packets does not match, data: %d, status: %d",
305 data_urb->number_of_packets,
306 status_urb->number_of_packets);
307 goto resubmit;
308 }
309
310 for (i = 0; i < status_urb->number_of_packets; i++) {
311 if (data_urb->iso_frame_desc[i].status ||
312 status_urb->iso_frame_desc[i].status) {
313 PDEBUG(D_ERR|D_PACK,
314 "pkt %d data-status %d, status-status %d", i,
315 data_urb->iso_frame_desc[i].status,
316 status_urb->iso_frame_desc[i].status);
317 gspca_dev->last_packet_type = DISCARD_PACKET;
318 continue;
319 }
320
321 if (status_urb->iso_frame_desc[i].actual_length != 1) {
322 PDEBUG(D_ERR|D_PACK,
323 "bad status packet length %d",
324 status_urb->iso_frame_desc[i].actual_length);
325 gspca_dev->last_packet_type = DISCARD_PACKET;
326 continue;
327 }
328
329 st = *((u8 *)status_urb->transfer_buffer
330 + status_urb->iso_frame_desc[i].offset);
331
332 data = (u8 *)data_urb->transfer_buffer
333 + data_urb->iso_frame_desc[i].offset;
334
335 /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
336 * otherwise:
337 * bit 0 0: keep packet
338 * 1: drop packet (padding data)
339 *
340 * bit 4 0 button not clicked
341 * 1 button clicked
342 * button is used to `take a picture' (in software)
343 */
344 if (st & 0x80) {
345 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
346 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
347 } else {
Jean-François Moine28566432010-10-01 07:33:26 -0300348#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goedeb517af72010-09-05 16:30:30 -0300349 u8 button_state = st & 0x40 ? 1 : 0;
350 if (sd->snapshot_pressed != button_state) {
351 input_report_key(gspca_dev->input_dev,
352 KEY_CAMERA,
353 button_state);
354 input_sync(gspca_dev->input_dev);
355 sd->snapshot_pressed = button_state;
356 }
357#endif
358 if (st & 0x01)
359 continue;
360 }
361 gspca_frame_add(gspca_dev, INTER_PACKET, data,
362 data_urb->iso_frame_desc[i].actual_length);
363 }
364
365resubmit:
366 if (data_urb) {
367 st = usb_submit_urb(data_urb, GFP_ATOMIC);
368 if (st < 0)
369 PDEBUG(D_ERR|D_PACK,
370 "usb_submit_urb(data_urb) ret %d", st);
371 }
372 st = usb_submit_urb(status_urb, GFP_ATOMIC);
373 if (st < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300374 pr_err("usb_submit_urb(status_urb) ret %d\n", st);
Hans de Goedeb517af72010-09-05 16:30:30 -0300375}
376
Hans Verkuil1bdee422012-05-14 07:35:37 -0300377static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
Hans de Goedeb517af72010-09-05 16:30:30 -0300378{
Hans Verkuil1bdee422012-05-14 07:35:37 -0300379 struct gspca_dev *gspca_dev =
380 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
Hans de Goedeb517af72010-09-05 16:30:30 -0300381
Hans Verkuil1bdee422012-05-14 07:35:37 -0300382 gspca_dev->usb_err = 0;
383
384 if (!gspca_dev->streaming)
385 return 0;
386
387 switch (ctrl->id) {
388 case V4L2_CID_BRIGHTNESS:
Hans de Goedeb517af72010-09-05 16:30:30 -0300389 konica_stream_off(gspca_dev);
Hans Verkuil1bdee422012-05-14 07:35:37 -0300390 reg_w(gspca_dev, ctrl->val, BRIGHTNESS_REG);
Hans de Goedeb517af72010-09-05 16:30:30 -0300391 konica_stream_on(gspca_dev);
Hans Verkuil1bdee422012-05-14 07:35:37 -0300392 break;
393 case V4L2_CID_CONTRAST:
Hans de Goedeb517af72010-09-05 16:30:30 -0300394 konica_stream_off(gspca_dev);
Hans Verkuil1bdee422012-05-14 07:35:37 -0300395 reg_w(gspca_dev, ctrl->val, CONTRAST_REG);
Hans de Goedeb517af72010-09-05 16:30:30 -0300396 konica_stream_on(gspca_dev);
Hans Verkuil1bdee422012-05-14 07:35:37 -0300397 break;
398 case V4L2_CID_SATURATION:
Hans de Goedeb517af72010-09-05 16:30:30 -0300399 konica_stream_off(gspca_dev);
Hans Verkuil1bdee422012-05-14 07:35:37 -0300400 reg_w(gspca_dev, ctrl->val, SATURATION_REG);
Hans de Goedeb517af72010-09-05 16:30:30 -0300401 konica_stream_on(gspca_dev);
Hans Verkuil1bdee422012-05-14 07:35:37 -0300402 break;
403 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
Hans de Goedeb517af72010-09-05 16:30:30 -0300404 konica_stream_off(gspca_dev);
Hans Verkuil1bdee422012-05-14 07:35:37 -0300405 reg_w(gspca_dev, ctrl->val, WHITEBAL_REG);
Hans de Goedeb517af72010-09-05 16:30:30 -0300406 konica_stream_on(gspca_dev);
Hans Verkuil1bdee422012-05-14 07:35:37 -0300407 break;
408 case V4L2_CID_SHARPNESS:
Hans de Goedeb517af72010-09-05 16:30:30 -0300409 konica_stream_off(gspca_dev);
Hans Verkuil1bdee422012-05-14 07:35:37 -0300410 reg_w(gspca_dev, ctrl->val, SHARPNESS_REG);
Hans de Goedeb517af72010-09-05 16:30:30 -0300411 konica_stream_on(gspca_dev);
Hans Verkuil1bdee422012-05-14 07:35:37 -0300412 break;
Hans de Goedeb517af72010-09-05 16:30:30 -0300413 }
Hans Verkuil1bdee422012-05-14 07:35:37 -0300414 return gspca_dev->usb_err;
Hans de Goedeb517af72010-09-05 16:30:30 -0300415}
416
Hans Verkuil1bdee422012-05-14 07:35:37 -0300417static const struct v4l2_ctrl_ops sd_ctrl_ops = {
418 .s_ctrl = sd_s_ctrl,
419};
420
421static int sd_init_controls(struct gspca_dev *gspca_dev)
Hans de Goedeb517af72010-09-05 16:30:30 -0300422{
Hans Verkuil1bdee422012-05-14 07:35:37 -0300423 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
Hans de Goedeb517af72010-09-05 16:30:30 -0300424
Hans Verkuil1bdee422012-05-14 07:35:37 -0300425 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,
436 0, 33, 1, 25);
437 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
438 V4L2_CID_SHARPNESS, 0, 9, 1, 4);
Hans de Goedeb517af72010-09-05 16:30:30 -0300439
Hans Verkuil1bdee422012-05-14 07:35:37 -0300440 if (hdl->error) {
441 pr_err("Could not initialize controls\n");
442 return hdl->error;
443 }
Hans de Goedeb517af72010-09-05 16:30:30 -0300444 return 0;
445}
446
447/* sub-driver description */
448static const struct sd_desc sd_desc = {
449 .name = MODULE_NAME,
Hans de Goedeb517af72010-09-05 16:30:30 -0300450 .config = sd_config,
451 .init = sd_init,
Hans Verkuil1bdee422012-05-14 07:35:37 -0300452 .init_controls = sd_init_controls,
Hans de Goedeb517af72010-09-05 16:30:30 -0300453 .start = sd_start,
454 .stopN = sd_stopN,
Jean-François Moine28566432010-10-01 07:33:26 -0300455#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goedeb517af72010-09-05 16:30:30 -0300456 .other_input = 1,
457#endif
458};
459
460/* -- module initialisation -- */
Jean-François Moine95c967c2011-01-13 05:20:29 -0300461static const struct usb_device_id device_table[] = {
Hans de Goedeb517af72010-09-05 16:30:30 -0300462 {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
463 {}
464};
465MODULE_DEVICE_TABLE(usb, device_table);
466
467/* -- device connect -- */
468static int sd_probe(struct usb_interface *intf,
469 const struct usb_device_id *id)
470{
471 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
472 THIS_MODULE);
473}
474
475static struct usb_driver sd_driver = {
476 .name = MODULE_NAME,
477 .id_table = device_table,
478 .probe = sd_probe,
479 .disconnect = gspca_disconnect,
480#ifdef CONFIG_PM
481 .suspend = gspca_suspend,
482 .resume = gspca_resume,
Hans de Goede8bb58962012-06-30 06:44:47 -0300483 .reset_resume = gspca_resume,
Hans de Goedeb517af72010-09-05 16:30:30 -0300484#endif
485};
486
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800487module_usb_driver(sd_driver);