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