Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / media / video / gspca / konica.c
1 /*
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
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
33 #define MODULE_NAME "konica"
34
35 #include <linux/input.h>
36 #include "gspca.h"
37
38 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
39 MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
40 MODULE_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 */
49 struct sd {
50         struct gspca_dev gspca_dev;     /* !! must be the first item */
51         struct urb *last_data_urb;
52         u8 snapshot_pressed;
53         u8 brightness;
54         u8 contrast;
55         u8 saturation;
56         u8 whitebal;
57         u8 sharpness;
58 };
59
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);
71
72 static const struct ctrl sd_ctrls[] = {
73 #define SD_BRIGHTNESS 0
74         {
75             {
76                 .id      = V4L2_CID_BRIGHTNESS,
77                 .type    = V4L2_CTRL_TYPE_INTEGER,
78                 .name    = "Brightness",
79                 .minimum = 0,
80                 .maximum = 9,
81                 .step = 1,
82 #define BRIGHTNESS_DEFAULT 4
83                 .default_value = BRIGHTNESS_DEFAULT,
84                 .flags = 0,
85             },
86             .set = sd_setbrightness,
87             .get = sd_getbrightness,
88         },
89 #define SD_CONTRAST 1
90         {
91             {
92                 .id = V4L2_CID_CONTRAST,
93                 .type = V4L2_CTRL_TYPE_INTEGER,
94                 .name = "Contrast",
95                 .minimum = 0,
96                 .maximum = 9,
97                 .step = 4,
98 #define CONTRAST_DEFAULT 10
99                 .default_value = CONTRAST_DEFAULT,
100                 .flags = 0,
101             },
102             .set = sd_setcontrast,
103             .get = sd_getcontrast,
104         },
105 #define SD_SATURATION 2
106         {
107             {
108                 .id     = V4L2_CID_SATURATION,
109                 .type   = V4L2_CTRL_TYPE_INTEGER,
110                 .name   = "Saturation",
111                 .minimum = 0,
112                 .maximum = 9,
113                 .step   = 1,
114 #define SATURATION_DEFAULT 4
115                 .default_value = SATURATION_DEFAULT,
116                 .flags = 0,
117             },
118             .set = sd_setsaturation,
119             .get = sd_getsaturation,
120         },
121 #define SD_WHITEBAL 3
122         {
123             {
124                 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
125                 .type = V4L2_CTRL_TYPE_INTEGER,
126                 .name = "White Balance",
127                 .minimum = 0,
128                 .maximum = 33,
129                 .step = 1,
130 #define WHITEBAL_DEFAULT 25
131                 .default_value = WHITEBAL_DEFAULT,
132                 .flags = 0,
133             },
134             .set = sd_setwhitebal,
135             .get = sd_getwhitebal,
136         },
137 #define SD_SHARPNESS 4
138         {
139             {
140                 .id = V4L2_CID_SHARPNESS,
141                 .type = V4L2_CTRL_TYPE_INTEGER,
142                 .name = "Sharpness",
143                 .minimum = 0,
144                 .maximum = 9,
145                 .step = 1,
146 #define SHARPNESS_DEFAULT 4
147                 .default_value = SHARPNESS_DEFAULT,
148                 .flags = 0,
149             },
150             .set = sd_setsharpness,
151             .get = sd_getsharpness,
152         },
153 };
154
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
161    0x05 -> 320x240
162    0x06 -> 320x240
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
168 */
169 static const struct v4l2_pix_format vga_mode[] = {
170         {160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
171                 .bytesperline = 160,
172                 .sizeimage = 160 * 136 * 3 / 2 + 960,
173                 .colorspace = V4L2_COLORSPACE_SRGB,
174                 .priv = 0x0a},
175         {176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
176                 .bytesperline = 176,
177                 .sizeimage = 176 * 144 * 3 / 2 + 960,
178                 .colorspace = V4L2_COLORSPACE_SRGB,
179                 .priv = 0x04},
180         {320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
181                 .bytesperline = 320,
182                 .sizeimage = 320 * 240 * 3 / 2 + 960,
183                 .colorspace = V4L2_COLORSPACE_SRGB,
184                 .priv = 0x05},
185 };
186
187 static void sd_isoc_irq(struct urb *urb);
188
189 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
190 {
191         struct usb_device *dev = gspca_dev->dev;
192         int ret;
193
194         if (gspca_dev->usb_err < 0)
195                 return;
196         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
197                         0x02,
198                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
199                         value,
200                         index,
201                         NULL,
202                         0,
203                         1000);
204         if (ret < 0) {
205                 pr_err("reg_w err %d\n", ret);
206                 gspca_dev->usb_err = ret;
207         }
208 }
209
210 static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
211 {
212         struct usb_device *dev = gspca_dev->dev;
213         int ret;
214
215         if (gspca_dev->usb_err < 0)
216                 return;
217         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
218                         0x03,
219                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
220                         value,
221                         index,
222                         gspca_dev->usb_buf,
223                         2,
224                         1000);
225         if (ret < 0) {
226                 pr_err("reg_w err %d\n", ret);
227                 gspca_dev->usb_err = ret;
228         }
229 }
230
231 static void konica_stream_on(struct gspca_dev *gspca_dev)
232 {
233         reg_w(gspca_dev, 1, 0x0b);
234 }
235
236 static void konica_stream_off(struct gspca_dev *gspca_dev)
237 {
238         reg_w(gspca_dev, 0, 0x0b);
239 }
240
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)
244 {
245         struct sd *sd = (struct sd *) gspca_dev;
246
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 */
252         gspca_dev->nbalt--;
253
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;
259
260         return 0;
261 }
262
263 /* this function is called at probe and resume time */
264 static int sd_init(struct gspca_dev *gspca_dev)
265 {
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);
274
275         return 0;
276 }
277
278 static int sd_start(struct gspca_dev *gspca_dev)
279 {
280         struct sd *sd = (struct sd *) gspca_dev;
281         struct urb *urb;
282         int i, n, packet_size;
283         struct usb_host_interface *alt;
284         struct usb_interface *intf;
285
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);
288         if (!alt) {
289                 pr_err("Couldn't get altsetting\n");
290                 return -EIO;
291         }
292
293         if (alt->desc.bNumEndpoints < 2)
294                 return -ENODEV;
295
296         packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
297
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);
303
304         n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
305         reg_w(gspca_dev, n, 0x08);
306
307         konica_stream_on(gspca_dev);
308
309         if (gspca_dev->usb_err)
310                 return gspca_dev->usb_err;
311
312         /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
313 #if MAX_NURBS < 4
314 #error "Not enough URBs in the gspca table"
315 #endif
316 #define SD_NPKT 32
317         for (n = 0; n < 4; n++) {
318                 i = n & 1 ? 0 : 1;
319                 packet_size =
320                         le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
321                 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
322                 if (!urb) {
323                         pr_err("usb_alloc_urb failed\n");
324                         return -ENOMEM;
325                 }
326                 gspca_dev->urb[n] = urb;
327                 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
328                                                 packet_size * SD_NPKT,
329                                                 GFP_KERNEL,
330                                                 &urb->transfer_dma);
331                 if (urb->transfer_buffer == NULL) {
332                         pr_err("usb_buffer_alloc failed\n");
333                         return -ENOMEM;
334                 }
335
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;
343                 urb->interval = 1;
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;
349                 }
350         }
351
352         return 0;
353 }
354
355 static void sd_stopN(struct gspca_dev *gspca_dev)
356 {
357         struct sd *sd = (struct sd *) gspca_dev;
358
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;
367         }
368 #endif
369 }
370
371 /* reception of an URB */
372 static void sd_isoc_irq(struct urb *urb)
373 {
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;
377         u8 *data;
378         int i, st;
379
380         PDEBUG(D_PACK, "sd isoc irq");
381         if (!gspca_dev->streaming)
382                 return;
383
384         if (urb->status != 0) {
385                 if (urb->status == -ESHUTDOWN)
386                         return;         /* disconnection */
387 #ifdef CONFIG_PM
388                 if (gspca_dev->frozen)
389                         return;
390 #endif
391                 PDEBUG(D_ERR, "urb status: %d", urb->status);
392                 st = usb_submit_urb(urb, GFP_ATOMIC);
393                 if (st < 0)
394                         pr_err("resubmit urb error %d\n", st);
395                 return;
396         }
397
398         /* if this is a data URB (ep 0x82), wait */
399         if (urb->transfer_buffer_length > 32) {
400                 sd->last_data_urb = urb;
401                 return;
402         }
403
404         status_urb = urb;
405         data_urb   = sd->last_data_urb;
406         sd->last_data_urb = NULL;
407
408         if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
409                 PDEBUG(D_ERR|D_PACK, "lost sync on frames");
410                 goto resubmit;
411         }
412
413         if (data_urb->number_of_packets != status_urb->number_of_packets) {
414                 PDEBUG(D_ERR|D_PACK,
415                        "no packets does not match, data: %d, status: %d",
416                        data_urb->number_of_packets,
417                        status_urb->number_of_packets);
418                 goto resubmit;
419         }
420
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) {
424                         PDEBUG(D_ERR|D_PACK,
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;
429                         continue;
430                 }
431
432                 if (status_urb->iso_frame_desc[i].actual_length != 1) {
433                         PDEBUG(D_ERR|D_PACK,
434                                "bad status packet length %d",
435                                status_urb->iso_frame_desc[i].actual_length);
436                         gspca_dev->last_packet_type = DISCARD_PACKET;
437                         continue;
438                 }
439
440                 st = *((u8 *)status_urb->transfer_buffer
441                                 + status_urb->iso_frame_desc[i].offset);
442
443                 data = (u8 *)data_urb->transfer_buffer
444                                 + data_urb->iso_frame_desc[i].offset;
445
446                 /* st: 0x80-0xff: frame start with frame number (ie 0-7f)
447                  * otherwise:
448                  * bit 0 0: keep packet
449                  *       1: drop packet (padding data)
450                  *
451                  * bit 4 0 button not clicked
452                  *       1 button clicked
453                  * button is used to `take a picture' (in software)
454                  */
455                 if (st & 0x80) {
456                         gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
457                         gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
458                 } else {
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,
463                                                  KEY_CAMERA,
464                                                  button_state);
465                                 input_sync(gspca_dev->input_dev);
466                                 sd->snapshot_pressed = button_state;
467                         }
468 #endif
469                         if (st & 0x01)
470                                 continue;
471                 }
472                 gspca_frame_add(gspca_dev, INTER_PACKET, data,
473                                 data_urb->iso_frame_desc[i].actual_length);
474         }
475
476 resubmit:
477         if (data_urb) {
478                 st = usb_submit_urb(data_urb, GFP_ATOMIC);
479                 if (st < 0)
480                         PDEBUG(D_ERR|D_PACK,
481                                "usb_submit_urb(data_urb) ret %d", st);
482         }
483         st = usb_submit_urb(status_urb, GFP_ATOMIC);
484         if (st < 0)
485                 pr_err("usb_submit_urb(status_urb) ret %d\n", st);
486 }
487
488 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
489 {
490         struct sd *sd = (struct sd *) gspca_dev;
491
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);
497         }
498
499         return 0;
500 }
501
502 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
503 {
504         struct sd *sd = (struct sd *) gspca_dev;
505
506         *val = sd->brightness;
507
508         return 0;
509 }
510
511 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
512 {
513         struct sd *sd = (struct sd *) gspca_dev;
514
515         sd->contrast = val;
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);
520         }
521
522         return 0;
523 }
524
525 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
526 {
527         struct sd *sd = (struct sd *) gspca_dev;
528
529         *val = sd->contrast;
530
531         return 0;
532 }
533
534 static int sd_setsaturation(struct gspca_dev *gspca_dev, __s32 val)
535 {
536         struct sd *sd = (struct sd *) gspca_dev;
537
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);
543         }
544         return 0;
545 }
546
547 static int sd_getsaturation(struct gspca_dev *gspca_dev, __s32 *val)
548 {
549         struct sd *sd = (struct sd *) gspca_dev;
550
551         *val = sd->saturation;
552
553         return 0;
554 }
555
556 static int sd_setwhitebal(struct gspca_dev *gspca_dev, __s32 val)
557 {
558         struct sd *sd = (struct sd *) gspca_dev;
559
560         sd->whitebal = val;
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);
565         }
566         return 0;
567 }
568
569 static int sd_getwhitebal(struct gspca_dev *gspca_dev, __s32 *val)
570 {
571         struct sd *sd = (struct sd *) gspca_dev;
572
573         *val = sd->whitebal;
574
575         return 0;
576 }
577
578 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
579 {
580         struct sd *sd = (struct sd *) gspca_dev;
581
582         sd->sharpness = val;
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);
587         }
588         return 0;
589 }
590
591 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
592 {
593         struct sd *sd = (struct sd *) gspca_dev;
594
595         *val = sd->sharpness;
596
597         return 0;
598 }
599
600 /* sub-driver description */
601 static const struct sd_desc sd_desc = {
602         .name = MODULE_NAME,
603         .ctrls = sd_ctrls,
604         .nctrls = ARRAY_SIZE(sd_ctrls),
605         .config = sd_config,
606         .init = sd_init,
607         .start = sd_start,
608         .stopN = sd_stopN,
609 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
610         .other_input = 1,
611 #endif
612 };
613
614 /* -- module initialisation -- */
615 static const struct usb_device_id device_table[] = {
616         {USB_DEVICE(0x04c8, 0x0720)}, /* Intel YC 76 */
617         {}
618 };
619 MODULE_DEVICE_TABLE(usb, device_table);
620
621 /* -- device connect -- */
622 static int sd_probe(struct usb_interface *intf,
623                         const struct usb_device_id *id)
624 {
625         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
626                                 THIS_MODULE);
627 }
628
629 static struct usb_driver sd_driver = {
630         .name = MODULE_NAME,
631         .id_table = device_table,
632         .probe = sd_probe,
633         .disconnect = gspca_disconnect,
634 #ifdef CONFIG_PM
635         .suspend = gspca_suspend,
636         .resume = gspca_resume,
637 #endif
638 };
639
640 /* -- module insert / remove -- */
641 static int __init sd_mod_init(void)
642 {
643         return usb_register(&sd_driver);
644 }
645 static void __exit sd_mod_exit(void)
646 {
647         usb_deregister(&sd_driver);
648 }
649
650 module_init(sd_mod_init);
651 module_exit(sd_mod_exit);