Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / drivers / media / usb / pwc / pwc-if.c
1 /* Linux driver for Philips webcam
2    USB and Video4Linux interface part.
3    (C) 1999-2004 Nemosoft Unv.
4    (C) 2004-2006 Luc Saillard (luc@saillard.org)
5    (C) 2011 Hans de Goede <hdegoede@redhat.com>
6
7    NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
8    driver and thus may have bugs that are not present in the original version.
9    Please send bug reports and support requests to <luc@saillard.org>.
10    The decompression routines have been implemented by reverse-engineering the
11    Nemosoft binary pwcx module. Caveat emptor.
12
13    This program is free software; you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 2 of the License, or
16    (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
27 */
28
29 /*
30    This code forms the interface between the USB layers and the Philips
31    specific stuff. Some adanved stuff of the driver falls under an
32    NDA, signed between me and Philips B.V., Eindhoven, the Netherlands, and
33    is thus not distributed in source form. The binary pwcx.o module
34    contains the code that falls under the NDA.
35
36    In case you're wondering: 'pwc' stands for "Philips WebCam", but
37    I really didn't want to type 'philips_web_cam' every time (I'm lazy as
38    any Linux kernel hacker, but I don't like uncomprehensible abbreviations
39    without explanation).
40
41    Oh yes, convention: to disctinguish between all the various pointers to
42    device-structures, I use these names for the pointer variables:
43    udev: struct usb_device *
44    vdev: struct video_device (member of pwc_dev)
45    pdev: struct pwc_devive *
46 */
47
48 /* Contributors:
49    - Alvarado: adding whitebalance code
50    - Alistar Moire: QuickCam 3000 Pro device/product ID
51    - Tony Hoyle: Creative Labs Webcam 5 device/product ID
52    - Mark Burazin: solving hang in VIDIOCSYNC when camera gets unplugged
53    - Jk Fang: Sotec Afina Eye ID
54    - Xavier Roche: QuickCam Pro 4000 ID
55    - Jens Knudsen: QuickCam Zoom ID
56    - J. Debert: QuickCam for Notebooks ID
57    - Pham Thanh Nam: webcam snapshot button as an event input device
58 */
59
60 #include <linux/errno.h>
61 #include <linux/init.h>
62 #include <linux/mm.h>
63 #include <linux/module.h>
64 #include <linux/poll.h>
65 #include <linux/slab.h>
66 #ifdef CONFIG_USB_PWC_INPUT_EVDEV
67 #include <linux/usb/input.h>
68 #endif
69 #include <linux/vmalloc.h>
70 #include <asm/io.h>
71 #include <linux/kernel.h>               /* simple_strtol() */
72
73 #include "pwc.h"
74 #include "pwc-kiara.h"
75 #include "pwc-timon.h"
76 #include "pwc-dec23.h"
77 #include "pwc-dec1.h"
78
79 /* Function prototypes and driver templates */
80
81 /* hotplug device table support */
82 static const struct usb_device_id pwc_device_table [] = {
83         { USB_DEVICE(0x0471, 0x0302) }, /* Philips models */
84         { USB_DEVICE(0x0471, 0x0303) },
85         { USB_DEVICE(0x0471, 0x0304) },
86         { USB_DEVICE(0x0471, 0x0307) },
87         { USB_DEVICE(0x0471, 0x0308) },
88         { USB_DEVICE(0x0471, 0x030C) },
89         { USB_DEVICE(0x0471, 0x0310) },
90         { USB_DEVICE(0x0471, 0x0311) }, /* Philips ToUcam PRO II */
91         { USB_DEVICE(0x0471, 0x0312) },
92         { USB_DEVICE(0x0471, 0x0313) }, /* the 'new' 720K */
93         { USB_DEVICE(0x0471, 0x0329) }, /* Philips SPC 900NC PC Camera */
94         { USB_DEVICE(0x069A, 0x0001) }, /* Askey */
95         { USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam Pro 3000 */
96         { USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
97         { USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam Pro 4000 */
98         { USB_DEVICE(0x046D, 0x08B3) }, /* Logitech QuickCam Zoom (old model) */
99         { USB_DEVICE(0x046D, 0x08B4) }, /* Logitech QuickCam Zoom (new model) */
100         { USB_DEVICE(0x046D, 0x08B5) }, /* Logitech QuickCam Orbit/Sphere */
101         { USB_DEVICE(0x046D, 0x08B6) }, /* Cisco VT Camera */
102         { USB_DEVICE(0x046D, 0x08B7) }, /* Logitech ViewPort AV 100 */
103         { USB_DEVICE(0x046D, 0x08B8) }, /* Logitech (reserved) */
104         { USB_DEVICE(0x055D, 0x9000) }, /* Samsung MPC-C10 */
105         { USB_DEVICE(0x055D, 0x9001) }, /* Samsung MPC-C30 */
106         { USB_DEVICE(0x055D, 0x9002) }, /* Samsung SNC-35E (Ver3.0) */
107         { USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
108         { USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
109         { USB_DEVICE(0x04CC, 0x8116) }, /* Afina Eye */
110         { USB_DEVICE(0x06BE, 0x8116) }, /* new Afina Eye */
111         { USB_DEVICE(0x0d81, 0x1910) }, /* Visionite */
112         { USB_DEVICE(0x0d81, 0x1900) },
113         { }
114 };
115 MODULE_DEVICE_TABLE(usb, pwc_device_table);
116
117 static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id);
118 static void usb_pwc_disconnect(struct usb_interface *intf);
119 static void pwc_isoc_cleanup(struct pwc_device *pdev);
120
121 static struct usb_driver pwc_driver = {
122         .name =                 "Philips webcam",       /* name */
123         .id_table =             pwc_device_table,
124         .probe =                usb_pwc_probe,          /* probe() */
125         .disconnect =           usb_pwc_disconnect,     /* disconnect() */
126 };
127
128 #define MAX_DEV_HINTS   20
129 #define MAX_ISOC_ERRORS 20
130
131 #ifdef CONFIG_USB_PWC_DEBUG
132         int pwc_trace = PWC_DEBUG_LEVEL;
133 #endif
134 static int power_save = -1;
135 static int leds[2] = { 100, 0 };
136
137 /***/
138
139 static const struct v4l2_file_operations pwc_fops = {
140         .owner =        THIS_MODULE,
141         .open =         v4l2_fh_open,
142         .release =      vb2_fop_release,
143         .read =         vb2_fop_read,
144         .poll =         vb2_fop_poll,
145         .mmap =         vb2_fop_mmap,
146         .unlocked_ioctl = video_ioctl2,
147 };
148 static struct video_device pwc_template = {
149         .name =         "Philips Webcam",       /* Filled in later */
150         .release =      video_device_release_empty,
151         .fops =         &pwc_fops,
152         .ioctl_ops =    &pwc_ioctl_ops,
153 };
154
155 /***************************************************************************/
156 /* Private functions */
157
158 static struct pwc_frame_buf *pwc_get_next_fill_buf(struct pwc_device *pdev)
159 {
160         unsigned long flags = 0;
161         struct pwc_frame_buf *buf = NULL;
162
163         spin_lock_irqsave(&pdev->queued_bufs_lock, flags);
164         if (list_empty(&pdev->queued_bufs))
165                 goto leave;
166
167         buf = list_entry(pdev->queued_bufs.next, struct pwc_frame_buf, list);
168         list_del(&buf->list);
169 leave:
170         spin_unlock_irqrestore(&pdev->queued_bufs_lock, flags);
171         return buf;
172 }
173
174 static void pwc_snapshot_button(struct pwc_device *pdev, int down)
175 {
176         if (down) {
177                 PWC_TRACE("Snapshot button pressed.\n");
178         } else {
179                 PWC_TRACE("Snapshot button released.\n");
180         }
181
182 #ifdef CONFIG_USB_PWC_INPUT_EVDEV
183         if (pdev->button_dev) {
184                 input_report_key(pdev->button_dev, KEY_CAMERA, down);
185                 input_sync(pdev->button_dev);
186         }
187 #endif
188 }
189
190 static void pwc_frame_complete(struct pwc_device *pdev)
191 {
192         struct pwc_frame_buf *fbuf = pdev->fill_buf;
193
194         /* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
195            frames on the USB wire after an exposure change. This conditition is
196            however detected  in the cam and a bit is set in the header.
197            */
198         if (pdev->type == 730) {
199                 unsigned char *ptr = (unsigned char *)fbuf->data;
200
201                 if (ptr[1] == 1 && ptr[0] & 0x10) {
202                         PWC_TRACE("Hyundai CMOS sensor bug. Dropping frame.\n");
203                         pdev->drop_frames += 2;
204                 }
205                 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
206                         pwc_snapshot_button(pdev, ptr[0] & 0x01);
207                 }
208                 if ((ptr[0] ^ pdev->vmirror) & 0x02) {
209                         if (ptr[0] & 0x02)
210                                 PWC_TRACE("Image is mirrored.\n");
211                         else
212                                 PWC_TRACE("Image is normal.\n");
213                 }
214                 pdev->vmirror = ptr[0] & 0x03;
215                 /* Sometimes the trailer of the 730 is still sent as a 4 byte packet
216                    after a short frame; this condition is filtered out specifically. A 4 byte
217                    frame doesn't make sense anyway.
218                    So we get either this sequence:
219                    drop_bit set -> 4 byte frame -> short frame -> good frame
220                    Or this one:
221                    drop_bit set -> short frame -> good frame
222                    So we drop either 3 or 2 frames in all!
223                    */
224                 if (fbuf->filled == 4)
225                         pdev->drop_frames++;
226         } else if (pdev->type == 740 || pdev->type == 720) {
227                 unsigned char *ptr = (unsigned char *)fbuf->data;
228                 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
229                         pwc_snapshot_button(pdev, ptr[0] & 0x01);
230                 }
231                 pdev->vmirror = ptr[0] & 0x03;
232         }
233
234         /* In case we were instructed to drop the frame, do so silently. */
235         if (pdev->drop_frames > 0) {
236                 pdev->drop_frames--;
237         } else {
238                 /* Check for underflow first */
239                 if (fbuf->filled < pdev->frame_total_size) {
240                         PWC_DEBUG_FLOW("Frame buffer underflow (%d bytes);"
241                                        " discarded.\n", fbuf->filled);
242                 } else {
243                         fbuf->vb.v4l2_buf.field = V4L2_FIELD_NONE;
244                         fbuf->vb.v4l2_buf.sequence = pdev->vframe_count;
245                         vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
246                         pdev->fill_buf = NULL;
247                         pdev->vsync = 0;
248                 }
249         } /* !drop_frames */
250         pdev->vframe_count++;
251 }
252
253 /* This gets called for the Isochronous pipe (video). This is done in
254  * interrupt time, so it has to be fast, not crash, and not stall. Neat.
255  */
256 static void pwc_isoc_handler(struct urb *urb)
257 {
258         struct pwc_device *pdev = (struct pwc_device *)urb->context;
259         int i, fst, flen;
260         unsigned char *iso_buf = NULL;
261
262         if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
263             urb->status == -ESHUTDOWN) {
264                 PWC_DEBUG_OPEN("URB (%p) unlinked %ssynchronuously.\n", urb, urb->status == -ENOENT ? "" : "a");
265                 return;
266         }
267
268         if (pdev->fill_buf == NULL)
269                 pdev->fill_buf = pwc_get_next_fill_buf(pdev);
270
271         if (urb->status != 0) {
272                 const char *errmsg;
273
274                 errmsg = "Unknown";
275                 switch(urb->status) {
276                         case -ENOSR:            errmsg = "Buffer error (overrun)"; break;
277                         case -EPIPE:            errmsg = "Stalled (device not responding)"; break;
278                         case -EOVERFLOW:        errmsg = "Babble (bad cable?)"; break;
279                         case -EPROTO:           errmsg = "Bit-stuff error (bad cable?)"; break;
280                         case -EILSEQ:           errmsg = "CRC/Timeout (could be anything)"; break;
281                         case -ETIME:            errmsg = "Device does not respond"; break;
282                 }
283                 PWC_ERROR("pwc_isoc_handler() called with status %d [%s].\n",
284                           urb->status, errmsg);
285                 /* Give up after a number of contiguous errors */
286                 if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
287                 {
288                         PWC_ERROR("Too many ISOC errors, bailing out.\n");
289                         if (pdev->fill_buf) {
290                                 vb2_buffer_done(&pdev->fill_buf->vb,
291                                                 VB2_BUF_STATE_ERROR);
292                                 pdev->fill_buf = NULL;
293                         }
294                 }
295                 pdev->vsync = 0; /* Drop the current frame */
296                 goto handler_end;
297         }
298
299         /* Reset ISOC error counter. We did get here, after all. */
300         pdev->visoc_errors = 0;
301
302         /* vsync: 0 = don't copy data
303                   1 = sync-hunt
304                   2 = synched
305          */
306         /* Compact data */
307         for (i = 0; i < urb->number_of_packets; i++) {
308                 fst  = urb->iso_frame_desc[i].status;
309                 flen = urb->iso_frame_desc[i].actual_length;
310                 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
311                 if (fst != 0) {
312                         PWC_ERROR("Iso frame %d has error %d\n", i, fst);
313                         continue;
314                 }
315                 if (flen > 0 && pdev->vsync) {
316                         struct pwc_frame_buf *fbuf = pdev->fill_buf;
317
318                         if (pdev->vsync == 1) {
319                                 v4l2_get_timestamp(
320                                         &fbuf->vb.v4l2_buf.timestamp);
321                                 pdev->vsync = 2;
322                         }
323
324                         if (flen + fbuf->filled > pdev->frame_total_size) {
325                                 PWC_ERROR("Frame overflow (%d > %d)\n",
326                                           flen + fbuf->filled,
327                                           pdev->frame_total_size);
328                                 pdev->vsync = 0; /* Let's wait for an EOF */
329                         } else {
330                                 memcpy(fbuf->data + fbuf->filled, iso_buf,
331                                        flen);
332                                 fbuf->filled += flen;
333                         }
334                 }
335                 if (flen < pdev->vlast_packet_size) {
336                         /* Shorter packet... end of frame */
337                         if (pdev->vsync == 2)
338                                 pwc_frame_complete(pdev);
339                         if (pdev->fill_buf == NULL)
340                                 pdev->fill_buf = pwc_get_next_fill_buf(pdev);
341                         if (pdev->fill_buf) {
342                                 pdev->fill_buf->filled = 0;
343                                 pdev->vsync = 1;
344                         }
345                 }
346                 pdev->vlast_packet_size = flen;
347         }
348
349 handler_end:
350         i = usb_submit_urb(urb, GFP_ATOMIC);
351         if (i != 0)
352                 PWC_ERROR("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
353 }
354
355 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
356 static int pwc_isoc_init(struct pwc_device *pdev)
357 {
358         struct usb_device *udev;
359         struct urb *urb;
360         int i, j, ret;
361         struct usb_interface *intf;
362         struct usb_host_interface *idesc = NULL;
363         int compression = 0; /* 0..3 = uncompressed..high */
364
365         pdev->vsync = 0;
366         pdev->vlast_packet_size = 0;
367         pdev->fill_buf = NULL;
368         pdev->vframe_count = 0;
369         pdev->visoc_errors = 0;
370         udev = pdev->udev;
371
372 retry:
373         /* We first try with low compression and then retry with a higher
374            compression setting if there is not enough bandwidth. */
375         ret = pwc_set_video_mode(pdev, pdev->width, pdev->height, pdev->pixfmt,
376                                  pdev->vframes, &compression, 1);
377
378         /* Get the current alternate interface, adjust packet size */
379         intf = usb_ifnum_to_if(udev, 0);
380         if (intf)
381                 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
382         if (!idesc)
383                 return -EIO;
384
385         /* Search video endpoint */
386         pdev->vmax_packet_size = -1;
387         for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
388                 if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
389                         pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
390                         break;
391                 }
392         }
393
394         if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
395                 PWC_ERROR("Failed to find packet size for video endpoint in current alternate setting.\n");
396                 return -ENFILE; /* Odd error, that should be noticeable */
397         }
398
399         /* Set alternate interface */
400         PWC_DEBUG_OPEN("Setting alternate interface %d\n", pdev->valternate);
401         ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
402         if (ret == -ENOSPC && compression < 3) {
403                 compression++;
404                 goto retry;
405         }
406         if (ret < 0)
407                 return ret;
408
409         /* Allocate and init Isochronuous urbs */
410         for (i = 0; i < MAX_ISO_BUFS; i++) {
411                 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
412                 if (urb == NULL) {
413                         PWC_ERROR("Failed to allocate urb %d\n", i);
414                         pwc_isoc_cleanup(pdev);
415                         return -ENOMEM;
416                 }
417                 pdev->urbs[i] = urb;
418                 PWC_DEBUG_MEMORY("Allocated URB at 0x%p\n", urb);
419
420                 urb->interval = 1; // devik
421                 urb->dev = udev;
422                 urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
423                 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
424                 urb->transfer_buffer = usb_alloc_coherent(udev,
425                                                           ISO_BUFFER_SIZE,
426                                                           GFP_KERNEL,
427                                                           &urb->transfer_dma);
428                 if (urb->transfer_buffer == NULL) {
429                         PWC_ERROR("Failed to allocate urb buffer %d\n", i);
430                         pwc_isoc_cleanup(pdev);
431                         return -ENOMEM;
432                 }
433                 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
434                 urb->complete = pwc_isoc_handler;
435                 urb->context = pdev;
436                 urb->start_frame = 0;
437                 urb->number_of_packets = ISO_FRAMES_PER_DESC;
438                 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
439                         urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
440                         urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
441                 }
442         }
443
444         /* link */
445         for (i = 0; i < MAX_ISO_BUFS; i++) {
446                 ret = usb_submit_urb(pdev->urbs[i], GFP_KERNEL);
447                 if (ret == -ENOSPC && compression < 3) {
448                         compression++;
449                         pwc_isoc_cleanup(pdev);
450                         goto retry;
451                 }
452                 if (ret) {
453                         PWC_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
454                         pwc_isoc_cleanup(pdev);
455                         return ret;
456                 }
457                 PWC_DEBUG_MEMORY("URB 0x%p submitted.\n", pdev->urbs[i]);
458         }
459
460         /* All is done... */
461         PWC_DEBUG_OPEN("<< pwc_isoc_init()\n");
462         return 0;
463 }
464
465 static void pwc_iso_stop(struct pwc_device *pdev)
466 {
467         int i;
468
469         /* Unlinking ISOC buffers one by one */
470         for (i = 0; i < MAX_ISO_BUFS; i++) {
471                 if (pdev->urbs[i]) {
472                         PWC_DEBUG_MEMORY("Unlinking URB %p\n", pdev->urbs[i]);
473                         usb_kill_urb(pdev->urbs[i]);
474                 }
475         }
476 }
477
478 static void pwc_iso_free(struct pwc_device *pdev)
479 {
480         int i;
481
482         /* Freeing ISOC buffers one by one */
483         for (i = 0; i < MAX_ISO_BUFS; i++) {
484                 if (pdev->urbs[i]) {
485                         PWC_DEBUG_MEMORY("Freeing URB\n");
486                         if (pdev->urbs[i]->transfer_buffer) {
487                                 usb_free_coherent(pdev->udev,
488                                         pdev->urbs[i]->transfer_buffer_length,
489                                         pdev->urbs[i]->transfer_buffer,
490                                         pdev->urbs[i]->transfer_dma);
491                         }
492                         usb_free_urb(pdev->urbs[i]);
493                         pdev->urbs[i] = NULL;
494                 }
495         }
496 }
497
498 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
499 static void pwc_isoc_cleanup(struct pwc_device *pdev)
500 {
501         PWC_DEBUG_OPEN(">> pwc_isoc_cleanup()\n");
502
503         pwc_iso_stop(pdev);
504         pwc_iso_free(pdev);
505         usb_set_interface(pdev->udev, 0, 0);
506
507         PWC_DEBUG_OPEN("<< pwc_isoc_cleanup()\n");
508 }
509
510 /* Must be called with vb_queue_lock hold */
511 static void pwc_cleanup_queued_bufs(struct pwc_device *pdev)
512 {
513         unsigned long flags = 0;
514
515         spin_lock_irqsave(&pdev->queued_bufs_lock, flags);
516         while (!list_empty(&pdev->queued_bufs)) {
517                 struct pwc_frame_buf *buf;
518
519                 buf = list_entry(pdev->queued_bufs.next, struct pwc_frame_buf,
520                                  list);
521                 list_del(&buf->list);
522                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
523         }
524         spin_unlock_irqrestore(&pdev->queued_bufs_lock, flags);
525 }
526
527 #ifdef CONFIG_USB_PWC_DEBUG
528 static const char *pwc_sensor_type_to_string(unsigned int sensor_type)
529 {
530         switch(sensor_type) {
531                 case 0x00:
532                         return "Hyundai CMOS sensor";
533                 case 0x20:
534                         return "Sony CCD sensor + TDA8787";
535                 case 0x2E:
536                         return "Sony CCD sensor + Exas 98L59";
537                 case 0x2F:
538                         return "Sony CCD sensor + ADI 9804";
539                 case 0x30:
540                         return "Sharp CCD sensor + TDA8787";
541                 case 0x3E:
542                         return "Sharp CCD sensor + Exas 98L59";
543                 case 0x3F:
544                         return "Sharp CCD sensor + ADI 9804";
545                 case 0x40:
546                         return "UPA 1021 sensor";
547                 case 0x100:
548                         return "VGA sensor";
549                 case 0x101:
550                         return "PAL MR sensor";
551                 default:
552                         return "unknown type of sensor";
553         }
554 }
555 #endif
556
557 /***************************************************************************/
558 /* Video4Linux functions */
559
560 static void pwc_video_release(struct v4l2_device *v)
561 {
562         struct pwc_device *pdev = container_of(v, struct pwc_device, v4l2_dev);
563
564         v4l2_ctrl_handler_free(&pdev->ctrl_handler);
565         v4l2_device_unregister(&pdev->v4l2_dev);
566         kfree(pdev->ctrl_buf);
567         kfree(pdev);
568 }
569
570 /***************************************************************************/
571 /* Videobuf2 operations */
572
573 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
574                                 unsigned int *nbuffers, unsigned int *nplanes,
575                                 unsigned int sizes[], void *alloc_ctxs[])
576 {
577         struct pwc_device *pdev = vb2_get_drv_priv(vq);
578         int size;
579
580         if (*nbuffers < MIN_FRAMES)
581                 *nbuffers = MIN_FRAMES;
582         else if (*nbuffers > MAX_FRAMES)
583                 *nbuffers = MAX_FRAMES;
584
585         *nplanes = 1;
586
587         size = pwc_get_size(pdev, MAX_WIDTH, MAX_HEIGHT);
588         sizes[0] = PAGE_ALIGN(pwc_image_sizes[size][0] *
589                               pwc_image_sizes[size][1] * 3 / 2);
590
591         return 0;
592 }
593
594 static int buffer_init(struct vb2_buffer *vb)
595 {
596         struct pwc_frame_buf *buf = container_of(vb, struct pwc_frame_buf, vb);
597
598         /* need vmalloc since frame buffer > 128K */
599         buf->data = vzalloc(PWC_FRAME_SIZE);
600         if (buf->data == NULL)
601                 return -ENOMEM;
602
603         return 0;
604 }
605
606 static int buffer_prepare(struct vb2_buffer *vb)
607 {
608         struct pwc_device *pdev = vb2_get_drv_priv(vb->vb2_queue);
609
610         /* Don't allow queing new buffers after device disconnection */
611         if (!pdev->udev)
612                 return -ENODEV;
613
614         return 0;
615 }
616
617 static int buffer_finish(struct vb2_buffer *vb)
618 {
619         struct pwc_device *pdev = vb2_get_drv_priv(vb->vb2_queue);
620         struct pwc_frame_buf *buf = container_of(vb, struct pwc_frame_buf, vb);
621
622         /*
623          * Application has called dqbuf and is getting back a buffer we've
624          * filled, take the pwc data we've stored in buf->data and decompress
625          * it into a usable format, storing the result in the vb2_buffer
626          */
627         return pwc_decompress(pdev, buf);
628 }
629
630 static void buffer_cleanup(struct vb2_buffer *vb)
631 {
632         struct pwc_frame_buf *buf = container_of(vb, struct pwc_frame_buf, vb);
633
634         vfree(buf->data);
635 }
636
637 static void buffer_queue(struct vb2_buffer *vb)
638 {
639         struct pwc_device *pdev = vb2_get_drv_priv(vb->vb2_queue);
640         struct pwc_frame_buf *buf = container_of(vb, struct pwc_frame_buf, vb);
641         unsigned long flags = 0;
642
643         /* Check the device has not disconnected between prep and queuing */
644         if (!pdev->udev) {
645                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
646                 return;
647         }
648
649         spin_lock_irqsave(&pdev->queued_bufs_lock, flags);
650         list_add_tail(&buf->list, &pdev->queued_bufs);
651         spin_unlock_irqrestore(&pdev->queued_bufs_lock, flags);
652 }
653
654 static int start_streaming(struct vb2_queue *vq, unsigned int count)
655 {
656         struct pwc_device *pdev = vb2_get_drv_priv(vq);
657         int r;
658
659         if (!pdev->udev)
660                 return -ENODEV;
661
662         if (mutex_lock_interruptible(&pdev->v4l2_lock))
663                 return -ERESTARTSYS;
664         /* Turn on camera and set LEDS on */
665         pwc_camera_power(pdev, 1);
666         pwc_set_leds(pdev, leds[0], leds[1]);
667
668         r = pwc_isoc_init(pdev);
669         if (r) {
670                 /* If we failed turn camera and LEDS back off */
671                 pwc_set_leds(pdev, 0, 0);
672                 pwc_camera_power(pdev, 0);
673                 /* And cleanup any queued bufs!! */
674                 pwc_cleanup_queued_bufs(pdev);
675         }
676         mutex_unlock(&pdev->v4l2_lock);
677
678         return r;
679 }
680
681 static int stop_streaming(struct vb2_queue *vq)
682 {
683         struct pwc_device *pdev = vb2_get_drv_priv(vq);
684
685         if (mutex_lock_interruptible(&pdev->v4l2_lock))
686                 return -ERESTARTSYS;
687         if (pdev->udev) {
688                 pwc_set_leds(pdev, 0, 0);
689                 pwc_camera_power(pdev, 0);
690                 pwc_isoc_cleanup(pdev);
691         }
692
693         pwc_cleanup_queued_bufs(pdev);
694         mutex_unlock(&pdev->v4l2_lock);
695
696         return 0;
697 }
698
699 static struct vb2_ops pwc_vb_queue_ops = {
700         .queue_setup            = queue_setup,
701         .buf_init               = buffer_init,
702         .buf_prepare            = buffer_prepare,
703         .buf_finish             = buffer_finish,
704         .buf_cleanup            = buffer_cleanup,
705         .buf_queue              = buffer_queue,
706         .start_streaming        = start_streaming,
707         .stop_streaming         = stop_streaming,
708         .wait_prepare           = vb2_ops_wait_prepare,
709         .wait_finish            = vb2_ops_wait_finish,
710 };
711
712 /***************************************************************************/
713 /* USB functions */
714
715 /* This function gets called when a new device is plugged in or the usb core
716  * is loaded.
717  */
718
719 static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
720 {
721         struct usb_device *udev = interface_to_usbdev(intf);
722         struct pwc_device *pdev = NULL;
723         int vendor_id, product_id, type_id;
724         int rc;
725         int features = 0;
726         int compression = 0;
727         int my_power_save = power_save;
728         char serial_number[30], *name;
729
730         vendor_id = le16_to_cpu(udev->descriptor.idVendor);
731         product_id = le16_to_cpu(udev->descriptor.idProduct);
732
733         /* Check if we can handle this device */
734         PWC_DEBUG_PROBE("probe() called [%04X %04X], if %d\n",
735                 vendor_id, product_id,
736                 intf->altsetting->desc.bInterfaceNumber);
737
738         /* the interfaces are probed one by one. We are only interested in the
739            video interface (0) now.
740            Interface 1 is the Audio Control, and interface 2 Audio itself.
741          */
742         if (intf->altsetting->desc.bInterfaceNumber > 0)
743                 return -ENODEV;
744
745         if (vendor_id == 0x0471) {
746                 switch (product_id) {
747                 case 0x0302:
748                         PWC_INFO("Philips PCA645VC USB webcam detected.\n");
749                         name = "Philips 645 webcam";
750                         type_id = 645;
751                         break;
752                 case 0x0303:
753                         PWC_INFO("Philips PCA646VC USB webcam detected.\n");
754                         name = "Philips 646 webcam";
755                         type_id = 646;
756                         break;
757                 case 0x0304:
758                         PWC_INFO("Askey VC010 type 2 USB webcam detected.\n");
759                         name = "Askey VC010 webcam";
760                         type_id = 646;
761                         break;
762                 case 0x0307:
763                         PWC_INFO("Philips PCVC675K (Vesta) USB webcam detected.\n");
764                         name = "Philips 675 webcam";
765                         type_id = 675;
766                         break;
767                 case 0x0308:
768                         PWC_INFO("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
769                         name = "Philips 680 webcam";
770                         type_id = 680;
771                         break;
772                 case 0x030C:
773                         PWC_INFO("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
774                         name = "Philips 690 webcam";
775                         type_id = 690;
776                         break;
777                 case 0x0310:
778                         PWC_INFO("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
779                         name = "Philips 730 webcam";
780                         type_id = 730;
781                         break;
782                 case 0x0311:
783                         PWC_INFO("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
784                         name = "Philips 740 webcam";
785                         type_id = 740;
786                         break;
787                 case 0x0312:
788                         PWC_INFO("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
789                         name = "Philips 750 webcam";
790                         type_id = 750;
791                         break;
792                 case 0x0313:
793                         PWC_INFO("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
794                         name = "Philips 720K/40 webcam";
795                         type_id = 720;
796                         break;
797                 case 0x0329:
798                         PWC_INFO("Philips SPC 900NC USB webcam detected.\n");
799                         name = "Philips SPC 900NC webcam";
800                         type_id = 740;
801                         break;
802                 default:
803                         return -ENODEV;
804                         break;
805                 }
806         }
807         else if (vendor_id == 0x069A) {
808                 switch(product_id) {
809                 case 0x0001:
810                         PWC_INFO("Askey VC010 type 1 USB webcam detected.\n");
811                         name = "Askey VC010 webcam";
812                         type_id = 645;
813                         break;
814                 default:
815                         return -ENODEV;
816                         break;
817                 }
818         }
819         else if (vendor_id == 0x046d) {
820                 switch(product_id) {
821                 case 0x08b0:
822                         PWC_INFO("Logitech QuickCam Pro 3000 USB webcam detected.\n");
823                         name = "Logitech QuickCam Pro 3000";
824                         type_id = 740; /* CCD sensor */
825                         break;
826                 case 0x08b1:
827                         PWC_INFO("Logitech QuickCam Notebook Pro USB webcam detected.\n");
828                         name = "Logitech QuickCam Notebook Pro";
829                         type_id = 740; /* CCD sensor */
830                         break;
831                 case 0x08b2:
832                         PWC_INFO("Logitech QuickCam 4000 Pro USB webcam detected.\n");
833                         name = "Logitech QuickCam Pro 4000";
834                         type_id = 740; /* CCD sensor */
835                         if (my_power_save == -1)
836                                 my_power_save = 1;
837                         break;
838                 case 0x08b3:
839                         PWC_INFO("Logitech QuickCam Zoom USB webcam detected.\n");
840                         name = "Logitech QuickCam Zoom";
841                         type_id = 740; /* CCD sensor */
842                         break;
843                 case 0x08B4:
844                         PWC_INFO("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
845                         name = "Logitech QuickCam Zoom";
846                         type_id = 740; /* CCD sensor */
847                         if (my_power_save == -1)
848                                 my_power_save = 1;
849                         break;
850                 case 0x08b5:
851                         PWC_INFO("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
852                         name = "Logitech QuickCam Orbit";
853                         type_id = 740; /* CCD sensor */
854                         if (my_power_save == -1)
855                                 my_power_save = 1;
856                         features |= FEATURE_MOTOR_PANTILT;
857                         break;
858                 case 0x08b6:
859                         PWC_INFO("Logitech/Cisco VT Camera webcam detected.\n");
860                         name = "Cisco VT Camera";
861                         type_id = 740; /* CCD sensor */
862                         break;
863                 case 0x08b7:
864                         PWC_INFO("Logitech ViewPort AV 100 webcam detected.\n");
865                         name = "Logitech ViewPort AV 100";
866                         type_id = 740; /* CCD sensor */
867                         break;
868                 case 0x08b8: /* Where this released? */
869                         PWC_INFO("Logitech QuickCam detected (reserved ID).\n");
870                         name = "Logitech QuickCam (res.)";
871                         type_id = 730; /* Assuming CMOS */
872                         break;
873                 default:
874                         return -ENODEV;
875                         break;
876                 }
877         }
878         else if (vendor_id == 0x055d) {
879                 /* I don't know the difference between the C10 and the C30;
880                    I suppose the difference is the sensor, but both cameras
881                    work equally well with a type_id of 675
882                  */
883                 switch(product_id) {
884                 case 0x9000:
885                         PWC_INFO("Samsung MPC-C10 USB webcam detected.\n");
886                         name = "Samsung MPC-C10";
887                         type_id = 675;
888                         break;
889                 case 0x9001:
890                         PWC_INFO("Samsung MPC-C30 USB webcam detected.\n");
891                         name = "Samsung MPC-C30";
892                         type_id = 675;
893                         break;
894                 case 0x9002:
895                         PWC_INFO("Samsung SNC-35E (v3.0) USB webcam detected.\n");
896                         name = "Samsung MPC-C30";
897                         type_id = 740;
898                         break;
899                 default:
900                         return -ENODEV;
901                         break;
902                 }
903         }
904         else if (vendor_id == 0x041e) {
905                 switch(product_id) {
906                 case 0x400c:
907                         PWC_INFO("Creative Labs Webcam 5 detected.\n");
908                         name = "Creative Labs Webcam 5";
909                         type_id = 730;
910                         if (my_power_save == -1)
911                                 my_power_save = 1;
912                         break;
913                 case 0x4011:
914                         PWC_INFO("Creative Labs Webcam Pro Ex detected.\n");
915                         name = "Creative Labs Webcam Pro Ex";
916                         type_id = 740;
917                         break;
918                 default:
919                         return -ENODEV;
920                         break;
921                 }
922         }
923         else if (vendor_id == 0x04cc) {
924                 switch(product_id) {
925                 case 0x8116:
926                         PWC_INFO("Sotec Afina Eye USB webcam detected.\n");
927                         name = "Sotec Afina Eye";
928                         type_id = 730;
929                         break;
930                 default:
931                         return -ENODEV;
932                         break;
933                 }
934         }
935         else if (vendor_id == 0x06be) {
936                 switch(product_id) {
937                 case 0x8116:
938                         /* This is essentially the same cam as the Sotec Afina Eye */
939                         PWC_INFO("AME Co. Afina Eye USB webcam detected.\n");
940                         name = "AME Co. Afina Eye";
941                         type_id = 750;
942                         break;
943                 default:
944                         return -ENODEV;
945                         break;
946                 }
947
948         }
949         else if (vendor_id == 0x0d81) {
950                 switch(product_id) {
951                 case 0x1900:
952                         PWC_INFO("Visionite VCS-UC300 USB webcam detected.\n");
953                         name = "Visionite VCS-UC300";
954                         type_id = 740; /* CCD sensor */
955                         break;
956                 case 0x1910:
957                         PWC_INFO("Visionite VCS-UM100 USB webcam detected.\n");
958                         name = "Visionite VCS-UM100";
959                         type_id = 730; /* CMOS sensor */
960                         break;
961                 default:
962                         return -ENODEV;
963                         break;
964                 }
965         }
966         else
967                 return -ENODEV; /* Not any of the know types; but the list keeps growing. */
968
969         if (my_power_save == -1)
970                 my_power_save = 0;
971
972         memset(serial_number, 0, 30);
973         usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
974         PWC_DEBUG_PROBE("Device serial number is %s\n", serial_number);
975
976         if (udev->descriptor.bNumConfigurations > 1)
977                 PWC_WARNING("Warning: more than 1 configuration available.\n");
978
979         /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
980         pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
981         if (pdev == NULL) {
982                 PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
983                 return -ENOMEM;
984         }
985         pdev->type = type_id;
986         pdev->features = features;
987         pwc_construct(pdev); /* set min/max sizes correct */
988
989         mutex_init(&pdev->v4l2_lock);
990         mutex_init(&pdev->vb_queue_lock);
991         spin_lock_init(&pdev->queued_bufs_lock);
992         INIT_LIST_HEAD(&pdev->queued_bufs);
993
994         pdev->udev = udev;
995         pdev->power_save = my_power_save;
996
997         /* Init videobuf2 queue structure */
998         pdev->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
999         pdev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1000         pdev->vb_queue.drv_priv = pdev;
1001         pdev->vb_queue.buf_struct_size = sizeof(struct pwc_frame_buf);
1002         pdev->vb_queue.ops = &pwc_vb_queue_ops;
1003         pdev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1004         pdev->vb_queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1005         rc = vb2_queue_init(&pdev->vb_queue);
1006         if (rc < 0) {
1007                 PWC_ERROR("Oops, could not initialize vb2 queue.\n");
1008                 goto err_free_mem;
1009         }
1010
1011         /* Init video_device structure */
1012         pdev->vdev = pwc_template;
1013         strcpy(pdev->vdev.name, name);
1014         pdev->vdev.queue = &pdev->vb_queue;
1015         pdev->vdev.queue->lock = &pdev->vb_queue_lock;
1016         set_bit(V4L2_FL_USE_FH_PRIO, &pdev->vdev.flags);
1017         video_set_drvdata(&pdev->vdev, pdev);
1018
1019         pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1020         PWC_DEBUG_PROBE("Release: %04x\n", pdev->release);
1021
1022         /* Allocate USB command buffers */
1023         pdev->ctrl_buf = kmalloc(sizeof(pdev->cmd_buf), GFP_KERNEL);
1024         if (!pdev->ctrl_buf) {
1025                 PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
1026                 rc = -ENOMEM;
1027                 goto err_free_mem;
1028         }
1029
1030 #ifdef CONFIG_USB_PWC_DEBUG
1031         /* Query sensor type */
1032         if (pwc_get_cmos_sensor(pdev, &rc) >= 0) {
1033                 PWC_DEBUG_OPEN("This %s camera is equipped with a %s (%d).\n",
1034                                 pdev->vdev.name,
1035                                 pwc_sensor_type_to_string(rc), rc);
1036         }
1037 #endif
1038
1039         /* Set the leds off */
1040         pwc_set_leds(pdev, 0, 0);
1041
1042         /* Setup initial videomode */
1043         rc = pwc_set_video_mode(pdev, MAX_WIDTH, MAX_HEIGHT,
1044                                 V4L2_PIX_FMT_YUV420, 30, &compression, 1);
1045         if (rc)
1046                 goto err_free_mem;
1047
1048         /* Register controls (and read default values from camera */
1049         rc = pwc_init_controls(pdev);
1050         if (rc) {
1051                 PWC_ERROR("Failed to register v4l2 controls (%d).\n", rc);
1052                 goto err_free_mem;
1053         }
1054
1055         /* And powerdown the camera until streaming starts */
1056         pwc_camera_power(pdev, 0);
1057
1058         /* Register the v4l2_device structure */
1059         pdev->v4l2_dev.release = pwc_video_release;
1060         rc = v4l2_device_register(&intf->dev, &pdev->v4l2_dev);
1061         if (rc) {
1062                 PWC_ERROR("Failed to register v4l2-device (%d).\n", rc);
1063                 goto err_free_controls;
1064         }
1065
1066         pdev->v4l2_dev.ctrl_handler = &pdev->ctrl_handler;
1067         pdev->vdev.v4l2_dev = &pdev->v4l2_dev;
1068         pdev->vdev.lock = &pdev->v4l2_lock;
1069
1070         rc = video_register_device(&pdev->vdev, VFL_TYPE_GRABBER, -1);
1071         if (rc < 0) {
1072                 PWC_ERROR("Failed to register as video device (%d).\n", rc);
1073                 goto err_unregister_v4l2_dev;
1074         }
1075         PWC_INFO("Registered as %s.\n", video_device_node_name(&pdev->vdev));
1076
1077 #ifdef CONFIG_USB_PWC_INPUT_EVDEV
1078         /* register webcam snapshot button input device */
1079         pdev->button_dev = input_allocate_device();
1080         if (!pdev->button_dev) {
1081                 PWC_ERROR("Err, insufficient memory for webcam snapshot button device.");
1082                 rc = -ENOMEM;
1083                 goto err_video_unreg;
1084         }
1085
1086         usb_make_path(udev, pdev->button_phys, sizeof(pdev->button_phys));
1087         strlcat(pdev->button_phys, "/input0", sizeof(pdev->button_phys));
1088
1089         pdev->button_dev->name = "PWC snapshot button";
1090         pdev->button_dev->phys = pdev->button_phys;
1091         usb_to_input_id(pdev->udev, &pdev->button_dev->id);
1092         pdev->button_dev->dev.parent = &pdev->udev->dev;
1093         pdev->button_dev->evbit[0] = BIT_MASK(EV_KEY);
1094         pdev->button_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
1095
1096         rc = input_register_device(pdev->button_dev);
1097         if (rc) {
1098                 input_free_device(pdev->button_dev);
1099                 pdev->button_dev = NULL;
1100                 goto err_video_unreg;
1101         }
1102 #endif
1103
1104         return 0;
1105
1106 err_video_unreg:
1107         video_unregister_device(&pdev->vdev);
1108 err_unregister_v4l2_dev:
1109         v4l2_device_unregister(&pdev->v4l2_dev);
1110 err_free_controls:
1111         v4l2_ctrl_handler_free(&pdev->ctrl_handler);
1112 err_free_mem:
1113         kfree(pdev->ctrl_buf);
1114         kfree(pdev);
1115         return rc;
1116 }
1117
1118 /* The user yanked out the cable... */
1119 static void usb_pwc_disconnect(struct usb_interface *intf)
1120 {
1121         struct v4l2_device *v = usb_get_intfdata(intf);
1122         struct pwc_device *pdev = container_of(v, struct pwc_device, v4l2_dev);
1123
1124         mutex_lock(&pdev->vb_queue_lock);
1125         mutex_lock(&pdev->v4l2_lock);
1126         /* No need to keep the urbs around after disconnection */
1127         if (pdev->vb_queue.streaming)
1128                 pwc_isoc_cleanup(pdev);
1129         pdev->udev = NULL;
1130         pwc_cleanup_queued_bufs(pdev);
1131
1132         v4l2_device_disconnect(&pdev->v4l2_dev);
1133         video_unregister_device(&pdev->vdev);
1134         mutex_unlock(&pdev->v4l2_lock);
1135         mutex_unlock(&pdev->vb_queue_lock);
1136
1137 #ifdef CONFIG_USB_PWC_INPUT_EVDEV
1138         if (pdev->button_dev)
1139                 input_unregister_device(pdev->button_dev);
1140 #endif
1141
1142         v4l2_device_put(&pdev->v4l2_dev);
1143 }
1144
1145
1146 /*
1147  * Initialization code & module stuff
1148  */
1149
1150 static unsigned int leds_nargs;
1151
1152 #ifdef CONFIG_USB_PWC_DEBUG
1153 module_param_named(trace, pwc_trace, int, 0644);
1154 #endif
1155 module_param(power_save, int, 0644);
1156 module_param_array(leds, int, &leds_nargs, 0444);
1157
1158 #ifdef CONFIG_USB_PWC_DEBUG
1159 MODULE_PARM_DESC(trace, "For debugging purposes");
1160 #endif
1161 MODULE_PARM_DESC(power_save, "Turn power saving for new cameras on or off");
1162 MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
1163
1164 MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
1165 MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
1166 MODULE_LICENSE("GPL");
1167 MODULE_ALIAS("pwcx");
1168 MODULE_VERSION( PWC_VERSION );
1169
1170 module_usb_driver(pwc_driver);