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 
pwc_alloc_urb_buffer(struct usb_device * dev,size_t size,dma_addr_t * dma_handle)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 
pwc_free_urb_buffer(struct usb_device * dev,size_t size,void * buffer,dma_addr_t dma_handle)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 
pwc_get_next_fill_buf(struct pwc_device * pdev)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 
pwc_snapshot_button(struct pwc_device * pdev,int down)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 
pwc_frame_complete(struct pwc_device * pdev)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  */
pwc_isoc_handler(struct urb * urb)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 */
pwc_isoc_init(struct pwc_device * pdev)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 
pwc_iso_stop(struct pwc_device * pdev)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 
pwc_iso_free(struct pwc_device * pdev)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 */
pwc_isoc_cleanup(struct pwc_device * pdev)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 */
pwc_cleanup_queued_bufs(struct pwc_device * pdev,enum vb2_buffer_state state)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
pwc_sensor_type_to_string(unsigned int sensor_type)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 
pwc_video_release(struct v4l2_device * v)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 
queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])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 
buffer_init(struct vb2_buffer * vb)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 
buffer_prepare(struct vb2_buffer * vb)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 
buffer_finish(struct vb2_buffer * vb)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 
buffer_cleanup(struct vb2_buffer * vb)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 
buffer_queue(struct vb2_buffer * vb)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 
start_streaming(struct vb2_queue * vq,unsigned int count)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 		return -ENODEV;
715 
716 	if (mutex_lock_interruptible(&pdev->v4l2_lock))
717 		return -ERESTARTSYS;
718 	/* Turn on camera and set LEDS on */
719 	pwc_camera_power(pdev, 1);
720 	pwc_set_leds(pdev, leds[0], leds[1]);
721 
722 	r = pwc_isoc_init(pdev);
723 	if (r) {
724 		/* If we failed turn camera and LEDS back off */
725 		pwc_set_leds(pdev, 0, 0);
726 		pwc_camera_power(pdev, 0);
727 		/* And cleanup any queued bufs!! */
728 		pwc_cleanup_queued_bufs(pdev, VB2_BUF_STATE_QUEUED);
729 	}
730 	mutex_unlock(&pdev->v4l2_lock);
731 
732 	return r;
733 }
734 
stop_streaming(struct vb2_queue * vq)735 static void stop_streaming(struct vb2_queue *vq)
736 {
737 	struct pwc_device *pdev = vb2_get_drv_priv(vq);
738 
739 	mutex_lock(&pdev->v4l2_lock);
740 	if (pdev->udev) {
741 		pwc_set_leds(pdev, 0, 0);
742 		pwc_camera_power(pdev, 0);
743 		pwc_isoc_cleanup(pdev);
744 	}
745 
746 	pwc_cleanup_queued_bufs(pdev, VB2_BUF_STATE_ERROR);
747 	if (pdev->fill_buf)
748 		vb2_buffer_done(&pdev->fill_buf->vb.vb2_buf,
749 				VB2_BUF_STATE_ERROR);
750 	mutex_unlock(&pdev->v4l2_lock);
751 }
752 
753 static const struct vb2_ops pwc_vb_queue_ops = {
754 	.queue_setup		= queue_setup,
755 	.buf_init		= buffer_init,
756 	.buf_prepare		= buffer_prepare,
757 	.buf_finish		= buffer_finish,
758 	.buf_cleanup		= buffer_cleanup,
759 	.buf_queue		= buffer_queue,
760 	.start_streaming	= start_streaming,
761 	.stop_streaming		= stop_streaming,
762 };
763 
764 /***************************************************************************/
765 /* USB functions */
766 
767 /* This function gets called when a new device is plugged in or the usb core
768  * is loaded.
769  */
770 
usb_pwc_probe(struct usb_interface * intf,const struct usb_device_id * id)771 static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
772 {
773 	struct usb_device *udev = interface_to_usbdev(intf);
774 	struct pwc_device *pdev = NULL;
775 	int vendor_id, product_id, type_id;
776 	int rc;
777 	int features = 0;
778 	int compression = 0;
779 	int my_power_save = power_save;
780 	char serial_number[30], *name;
781 
782 	vendor_id = le16_to_cpu(udev->descriptor.idVendor);
783 	product_id = le16_to_cpu(udev->descriptor.idProduct);
784 
785 	/* Check if we can handle this device */
786 	PWC_DEBUG_PROBE("probe() called [%04X %04X], if %d\n",
787 		vendor_id, product_id,
788 		intf->altsetting->desc.bInterfaceNumber);
789 
790 	/* the interfaces are probed one by one. We are only interested in the
791 	   video interface (0) now.
792 	   Interface 1 is the Audio Control, and interface 2 Audio itself.
793 	 */
794 	if (intf->altsetting->desc.bInterfaceNumber > 0)
795 		return -ENODEV;
796 
797 	if (vendor_id == 0x0471) {
798 		switch (product_id) {
799 		case 0x0302:
800 			PWC_INFO("Philips PCA645VC USB webcam detected.\n");
801 			name = "Philips 645 webcam";
802 			type_id = 645;
803 			break;
804 		case 0x0303:
805 			PWC_INFO("Philips PCA646VC USB webcam detected.\n");
806 			name = "Philips 646 webcam";
807 			type_id = 646;
808 			break;
809 		case 0x0304:
810 			PWC_INFO("Askey VC010 type 2 USB webcam detected.\n");
811 			name = "Askey VC010 webcam";
812 			type_id = 646;
813 			break;
814 		case 0x0307:
815 			PWC_INFO("Philips PCVC675K (Vesta) USB webcam detected.\n");
816 			name = "Philips 675 webcam";
817 			type_id = 675;
818 			break;
819 		case 0x0308:
820 			PWC_INFO("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
821 			name = "Philips 680 webcam";
822 			type_id = 680;
823 			break;
824 		case 0x030C:
825 			PWC_INFO("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
826 			name = "Philips 690 webcam";
827 			type_id = 690;
828 			break;
829 		case 0x0310:
830 			PWC_INFO("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
831 			name = "Philips 730 webcam";
832 			type_id = 730;
833 			break;
834 		case 0x0311:
835 			PWC_INFO("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
836 			name = "Philips 740 webcam";
837 			type_id = 740;
838 			break;
839 		case 0x0312:
840 			PWC_INFO("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
841 			name = "Philips 750 webcam";
842 			type_id = 750;
843 			break;
844 		case 0x0313:
845 			PWC_INFO("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
846 			name = "Philips 720K/40 webcam";
847 			type_id = 720;
848 			break;
849 		case 0x0329:
850 			PWC_INFO("Philips SPC 900NC USB webcam detected.\n");
851 			name = "Philips SPC 900NC webcam";
852 			type_id = 740;
853 			break;
854 		case 0x032C:
855 			PWC_INFO("Philips SPC 880NC USB webcam detected.\n");
856 			name = "Philips SPC 880NC webcam";
857 			type_id = 740;
858 			break;
859 		default:
860 			return -ENODEV;
861 		}
862 	}
863 	else if (vendor_id == 0x069A) {
864 		switch(product_id) {
865 		case 0x0001:
866 			PWC_INFO("Askey VC010 type 1 USB webcam detected.\n");
867 			name = "Askey VC010 webcam";
868 			type_id = 645;
869 			break;
870 		default:
871 			return -ENODEV;
872 		}
873 	}
874 	else if (vendor_id == 0x046d) {
875 		switch(product_id) {
876 		case 0x08b0:
877 			PWC_INFO("Logitech QuickCam Pro 3000 USB webcam detected.\n");
878 			name = "Logitech QuickCam Pro 3000";
879 			type_id = 740; /* CCD sensor */
880 			break;
881 		case 0x08b1:
882 			PWC_INFO("Logitech QuickCam Notebook Pro USB webcam detected.\n");
883 			name = "Logitech QuickCam Notebook Pro";
884 			type_id = 740; /* CCD sensor */
885 			break;
886 		case 0x08b2:
887 			PWC_INFO("Logitech QuickCam 4000 Pro USB webcam detected.\n");
888 			name = "Logitech QuickCam Pro 4000";
889 			type_id = 740; /* CCD sensor */
890 			if (my_power_save == -1)
891 				my_power_save = 1;
892 			break;
893 		case 0x08b3:
894 			PWC_INFO("Logitech QuickCam Zoom USB webcam detected.\n");
895 			name = "Logitech QuickCam Zoom";
896 			type_id = 740; /* CCD sensor */
897 			break;
898 		case 0x08B4:
899 			PWC_INFO("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
900 			name = "Logitech QuickCam Zoom";
901 			type_id = 740; /* CCD sensor */
902 			if (my_power_save == -1)
903 				my_power_save = 1;
904 			break;
905 		case 0x08b5:
906 			PWC_INFO("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
907 			name = "Logitech QuickCam Orbit";
908 			type_id = 740; /* CCD sensor */
909 			if (my_power_save == -1)
910 				my_power_save = 1;
911 			features |= FEATURE_MOTOR_PANTILT;
912 			break;
913 		case 0x08b6:
914 			PWC_INFO("Logitech/Cisco VT Camera webcam detected.\n");
915 			name = "Cisco VT Camera";
916 			type_id = 740; /* CCD sensor */
917 			break;
918 		case 0x08b7:
919 			PWC_INFO("Logitech ViewPort AV 100 webcam detected.\n");
920 			name = "Logitech ViewPort AV 100";
921 			type_id = 740; /* CCD sensor */
922 			break;
923 		case 0x08b8: /* Where this released? */
924 			PWC_INFO("Logitech QuickCam detected (reserved ID).\n");
925 			name = "Logitech QuickCam (res.)";
926 			type_id = 730; /* Assuming CMOS */
927 			break;
928 		default:
929 			return -ENODEV;
930 		}
931 	}
932 	else if (vendor_id == 0x055d) {
933 		/* I don't know the difference between the C10 and the C30;
934 		   I suppose the difference is the sensor, but both cameras
935 		   work equally well with a type_id of 675
936 		 */
937 		switch(product_id) {
938 		case 0x9000:
939 			PWC_INFO("Samsung MPC-C10 USB webcam detected.\n");
940 			name = "Samsung MPC-C10";
941 			type_id = 675;
942 			break;
943 		case 0x9001:
944 			PWC_INFO("Samsung MPC-C30 USB webcam detected.\n");
945 			name = "Samsung MPC-C30";
946 			type_id = 675;
947 			break;
948 		case 0x9002:
949 			PWC_INFO("Samsung SNC-35E (v3.0) USB webcam detected.\n");
950 			name = "Samsung MPC-C30";
951 			type_id = 740;
952 			break;
953 		default:
954 			return -ENODEV;
955 		}
956 	}
957 	else if (vendor_id == 0x041e) {
958 		switch(product_id) {
959 		case 0x400c:
960 			PWC_INFO("Creative Labs Webcam 5 detected.\n");
961 			name = "Creative Labs Webcam 5";
962 			type_id = 730;
963 			if (my_power_save == -1)
964 				my_power_save = 1;
965 			break;
966 		case 0x4011:
967 			PWC_INFO("Creative Labs Webcam Pro Ex detected.\n");
968 			name = "Creative Labs Webcam Pro Ex";
969 			type_id = 740;
970 			break;
971 		default:
972 			return -ENODEV;
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 		}
985 	}
986 	else if (vendor_id == 0x06be) {
987 		switch(product_id) {
988 		case 0x8116:
989 			/* This is essentially the same cam as the Sotec Afina Eye */
990 			PWC_INFO("AME Co. Afina Eye USB webcam detected.\n");
991 			name = "AME Co. Afina Eye";
992 			type_id = 750;
993 			break;
994 		default:
995 			return -ENODEV;
996 		}
997 
998 	}
999 	else if (vendor_id == 0x0d81) {
1000 		switch(product_id) {
1001 		case 0x1900:
1002 			PWC_INFO("Visionite VCS-UC300 USB webcam detected.\n");
1003 			name = "Visionite VCS-UC300";
1004 			type_id = 740; /* CCD sensor */
1005 			break;
1006 		case 0x1910:
1007 			PWC_INFO("Visionite VCS-UM100 USB webcam detected.\n");
1008 			name = "Visionite VCS-UM100";
1009 			type_id = 730; /* CMOS sensor */
1010 			break;
1011 		default:
1012 			return -ENODEV;
1013 		}
1014 	}
1015 	else
1016 		return -ENODEV; /* Not any of the know types; but the list keeps growing. */
1017 
1018 	if (my_power_save == -1)
1019 		my_power_save = 0;
1020 
1021 	memset(serial_number, 0, 30);
1022 	usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
1023 	PWC_DEBUG_PROBE("Device serial number is %s\n", serial_number);
1024 
1025 	if (udev->descriptor.bNumConfigurations > 1)
1026 		PWC_WARNING("Warning: more than 1 configuration available.\n");
1027 
1028 	/* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1029 	pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
1030 	if (pdev == NULL) {
1031 		PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
1032 		return -ENOMEM;
1033 	}
1034 	pdev->type = type_id;
1035 	pdev->features = features;
1036 	pwc_construct(pdev); /* set min/max sizes correct */
1037 
1038 	mutex_init(&pdev->v4l2_lock);
1039 	mutex_init(&pdev->vb_queue_lock);
1040 	spin_lock_init(&pdev->queued_bufs_lock);
1041 	INIT_LIST_HEAD(&pdev->queued_bufs);
1042 
1043 	pdev->udev = udev;
1044 	pdev->power_save = my_power_save;
1045 
1046 	/* Init videobuf2 queue structure */
1047 	pdev->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1048 	pdev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1049 	pdev->vb_queue.drv_priv = pdev;
1050 	pdev->vb_queue.buf_struct_size = sizeof(struct pwc_frame_buf);
1051 	pdev->vb_queue.ops = &pwc_vb_queue_ops;
1052 	pdev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1053 	pdev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1054 	pdev->vb_queue.lock = &pdev->vb_queue_lock;
1055 	rc = vb2_queue_init(&pdev->vb_queue);
1056 	if (rc < 0) {
1057 		PWC_ERROR("Oops, could not initialize vb2 queue.\n");
1058 		goto err_free_mem;
1059 	}
1060 
1061 	/* Init video_device structure */
1062 	pdev->vdev = pwc_template;
1063 	strscpy(pdev->vdev.name, name, sizeof(pdev->vdev.name));
1064 	pdev->vdev.queue = &pdev->vb_queue;
1065 	video_set_drvdata(&pdev->vdev, pdev);
1066 
1067 	pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1068 	PWC_DEBUG_PROBE("Release: %04x\n", pdev->release);
1069 
1070 	/* Allocate USB command buffers */
1071 	pdev->ctrl_buf = kmalloc(sizeof(pdev->cmd_buf), GFP_KERNEL);
1072 	if (!pdev->ctrl_buf) {
1073 		PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
1074 		rc = -ENOMEM;
1075 		goto err_free_mem;
1076 	}
1077 
1078 #ifdef CONFIG_USB_PWC_DEBUG
1079 	/* Query sensor type */
1080 	if (pwc_get_cmos_sensor(pdev, &rc) >= 0) {
1081 		PWC_DEBUG_OPEN("This %s camera is equipped with a %s (%d).\n",
1082 				pdev->vdev.name,
1083 				pwc_sensor_type_to_string(rc), rc);
1084 	}
1085 #endif
1086 
1087 	/* Set the leds off */
1088 	pwc_set_leds(pdev, 0, 0);
1089 
1090 	/* Setup initial videomode */
1091 	rc = pwc_set_video_mode(pdev, MAX_WIDTH, MAX_HEIGHT,
1092 				V4L2_PIX_FMT_YUV420, 30, &compression, 1);
1093 	if (rc)
1094 		goto err_free_mem;
1095 
1096 	/* Register controls (and read default values from camera */
1097 	rc = pwc_init_controls(pdev);
1098 	if (rc) {
1099 		PWC_ERROR("Failed to register v4l2 controls (%d).\n", rc);
1100 		goto err_free_mem;
1101 	}
1102 
1103 	/* And powerdown the camera until streaming starts */
1104 	pwc_camera_power(pdev, 0);
1105 
1106 	/* Register the v4l2_device structure */
1107 	pdev->v4l2_dev.release = pwc_video_release;
1108 	rc = v4l2_device_register(&intf->dev, &pdev->v4l2_dev);
1109 	if (rc) {
1110 		PWC_ERROR("Failed to register v4l2-device (%d).\n", rc);
1111 		goto err_free_controls;
1112 	}
1113 
1114 	pdev->v4l2_dev.ctrl_handler = &pdev->ctrl_handler;
1115 	pdev->vdev.v4l2_dev = &pdev->v4l2_dev;
1116 	pdev->vdev.lock = &pdev->v4l2_lock;
1117 	pdev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1118 				 V4L2_CAP_READWRITE;
1119 
1120 	rc = video_register_device(&pdev->vdev, VFL_TYPE_VIDEO, -1);
1121 	if (rc < 0) {
1122 		PWC_ERROR("Failed to register as video device (%d).\n", rc);
1123 		goto err_unregister_v4l2_dev;
1124 	}
1125 	PWC_INFO("Registered as %s.\n", video_device_node_name(&pdev->vdev));
1126 
1127 #ifdef CONFIG_USB_PWC_INPUT_EVDEV
1128 	/* register webcam snapshot button input device */
1129 	pdev->button_dev = input_allocate_device();
1130 	if (!pdev->button_dev) {
1131 		rc = -ENOMEM;
1132 		goto err_video_unreg;
1133 	}
1134 
1135 	usb_make_path(udev, pdev->button_phys, sizeof(pdev->button_phys));
1136 	strlcat(pdev->button_phys, "/input0", sizeof(pdev->button_phys));
1137 
1138 	pdev->button_dev->name = "PWC snapshot button";
1139 	pdev->button_dev->phys = pdev->button_phys;
1140 	usb_to_input_id(pdev->udev, &pdev->button_dev->id);
1141 	pdev->button_dev->dev.parent = &pdev->udev->dev;
1142 	pdev->button_dev->evbit[0] = BIT_MASK(EV_KEY);
1143 	pdev->button_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
1144 
1145 	rc = input_register_device(pdev->button_dev);
1146 	if (rc) {
1147 		input_free_device(pdev->button_dev);
1148 		pdev->button_dev = NULL;
1149 		goto err_video_unreg;
1150 	}
1151 #endif
1152 
1153 	return 0;
1154 
1155 #ifdef CONFIG_USB_PWC_INPUT_EVDEV
1156 err_video_unreg:
1157 	video_unregister_device(&pdev->vdev);
1158 #endif
1159 err_unregister_v4l2_dev:
1160 	v4l2_device_unregister(&pdev->v4l2_dev);
1161 err_free_controls:
1162 	v4l2_ctrl_handler_free(&pdev->ctrl_handler);
1163 err_free_mem:
1164 	kfree(pdev->ctrl_buf);
1165 	kfree(pdev);
1166 	return rc;
1167 }
1168 
1169 /* The user yanked out the cable... */
usb_pwc_disconnect(struct usb_interface * intf)1170 static void usb_pwc_disconnect(struct usb_interface *intf)
1171 {
1172 	struct v4l2_device *v = usb_get_intfdata(intf);
1173 	struct pwc_device *pdev = container_of(v, struct pwc_device, v4l2_dev);
1174 
1175 	mutex_lock(&pdev->vb_queue_lock);
1176 	mutex_lock(&pdev->v4l2_lock);
1177 	/* No need to keep the urbs around after disconnection */
1178 	if (pdev->vb_queue.streaming)
1179 		pwc_isoc_cleanup(pdev);
1180 	pdev->udev = NULL;
1181 
1182 	v4l2_device_disconnect(&pdev->v4l2_dev);
1183 	video_unregister_device(&pdev->vdev);
1184 	mutex_unlock(&pdev->v4l2_lock);
1185 	mutex_unlock(&pdev->vb_queue_lock);
1186 
1187 #ifdef CONFIG_USB_PWC_INPUT_EVDEV
1188 	if (pdev->button_dev)
1189 		input_unregister_device(pdev->button_dev);
1190 #endif
1191 
1192 	v4l2_device_put(&pdev->v4l2_dev);
1193 }
1194 
1195 
1196 /*
1197  * Initialization code & module stuff
1198  */
1199 
1200 static unsigned int leds_nargs;
1201 
1202 #ifdef CONFIG_USB_PWC_DEBUG
1203 module_param_named(trace, pwc_trace, int, 0644);
1204 #endif
1205 module_param(power_save, int, 0644);
1206 module_param_array(leds, int, &leds_nargs, 0444);
1207 
1208 #ifdef CONFIG_USB_PWC_DEBUG
1209 MODULE_PARM_DESC(trace, "For debugging purposes");
1210 #endif
1211 MODULE_PARM_DESC(power_save, "Turn power saving for new cameras on or off");
1212 MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
1213 
1214 MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
1215 MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
1216 MODULE_LICENSE("GPL");
1217 MODULE_ALIAS("pwcx");
1218 MODULE_VERSION( PWC_VERSION );
1219 
1220 module_usb_driver(pwc_driver);
1221