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