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