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