1 /* Linux driver for Philips webcam
2 USB and Video4Linux interface part.
3 (C) 1999-2004 Nemosoft Unv.
4 (C) 2004 Luc Saillard (luc@saillard.org)
6 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
7 driver and thus may have bugs that are not present in the original version.
8 Please send bug reports and support requests to <luc@saillard.org>.
9 The decompression routines have been implemented by reverse-engineering the
10 Nemosoft binary pwcx module. Caveat emptor.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 This code forms the interface between the USB layers and the Philips
30 specific stuff. Some adanved stuff of the driver falls under an
31 NDA, signed between me and Philips B.V., Eindhoven, the Netherlands, and
32 is thus not distributed in source form. The binary pwcx.o module
33 contains the code that falls under the NDA.
35 In case you're wondering: 'pwc' stands for "Philips WebCam", but
36 I really didn't want to type 'philips_web_cam' every time (I'm lazy as
37 any Linux kernel hacker, but I don't like uncomprehensible abbreviations
40 Oh yes, convention: to disctinguish between all the various pointers to
41 device-structures, I use these names for the pointer variables:
42 udev: struct usb_device *
43 vdev: struct video_device *
44 pdev: struct pwc_devive *
48 - Alvarado: adding whitebalance code
49 - Alistar Moire: QuickCam 3000 Pro device/product ID
50 - Tony Hoyle: Creative Labs Webcam 5 device/product ID
51 - Mark Burazin: solving hang in VIDIOCSYNC when camera gets unplugged
52 - Jk Fang: Sotec Afina Eye ID
53 - Xavier Roche: QuickCam Pro 4000 ID
54 - Jens Knudsen: QuickCam Zoom ID
55 - J. Debert: QuickCam for Notebooks ID
58 #include <linux/errno.h>
59 #include <linux/init.h>
61 #include <linux/module.h>
62 #include <linux/poll.h>
63 #include <linux/slab.h>
64 #include <linux/vmalloc.h>
65 #include <linux/version.h>
69 #include "pwc-ioctl.h"
70 #include "pwc-kiara.h"
71 #include "pwc-timon.h"
72 #include "pwc-uncompress.h"
74 /* Function prototypes and driver templates */
76 /* hotplug device table support */
77 static struct usb_device_id pwc_device_table [] = {
78 { USB_DEVICE(0x0471, 0x0302) }, /* Philips models */
79 { USB_DEVICE(0x0471, 0x0303) },
80 { USB_DEVICE(0x0471, 0x0304) },
81 { USB_DEVICE(0x0471, 0x0307) },
82 { USB_DEVICE(0x0471, 0x0308) },
83 { USB_DEVICE(0x0471, 0x030C) },
84 { USB_DEVICE(0x0471, 0x0310) },
85 { USB_DEVICE(0x0471, 0x0311) },
86 { USB_DEVICE(0x0471, 0x0312) },
87 { USB_DEVICE(0x0471, 0x0313) }, /* the 'new' 720K */
88 { USB_DEVICE(0x069A, 0x0001) }, /* Askey */
89 { USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam Pro 3000 */
90 { USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
91 { USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam Pro 4000 */
92 { USB_DEVICE(0x046D, 0x08B3) }, /* Logitech QuickCam Zoom (old model) */
93 { USB_DEVICE(0x046D, 0x08B4) }, /* Logitech QuickCam Zoom (new model) */
94 { USB_DEVICE(0x046D, 0x08B5) }, /* Logitech QuickCam Orbit/Sphere */
95 { USB_DEVICE(0x046D, 0x08B6) }, /* Logitech (reserved) */
96 { USB_DEVICE(0x046D, 0x08B7) }, /* Logitech (reserved) */
97 { USB_DEVICE(0x046D, 0x08B8) }, /* Logitech (reserved) */
98 { USB_DEVICE(0x055D, 0x9000) }, /* Samsung */
99 { USB_DEVICE(0x055D, 0x9001) },
100 { USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
101 { USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
102 { USB_DEVICE(0x04CC, 0x8116) }, /* Afina Eye */
103 { USB_DEVICE(0x06BE, 0x8116) }, /* new Afina Eye */
104 { USB_DEVICE(0x0d81, 0x1910) }, /* Visionite */
105 { USB_DEVICE(0x0d81, 0x1900) },
108 MODULE_DEVICE_TABLE(usb, pwc_device_table);
110 static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id);
111 static void usb_pwc_disconnect(struct usb_interface *intf);
113 static struct usb_driver pwc_driver = {
114 .name = "Philips webcam", /* name */
115 .id_table = pwc_device_table,
116 .probe = usb_pwc_probe, /* probe() */
117 .disconnect = usb_pwc_disconnect, /* disconnect() */
120 #define MAX_DEV_HINTS 20
121 #define MAX_ISOC_ERRORS 20
123 static int default_size = PSZ_QCIF;
124 static int default_fps = 10;
125 static int default_fbufs = 3; /* Default number of frame buffers */
126 static int default_mbufs = 2; /* Default number of mmap() buffers */
127 int pwc_trace = TRACE_MODULE | TRACE_FLOW | TRACE_PWCX;
128 static int power_save = 0;
129 static int led_on = 100, led_off = 0; /* defaults to LED that is on while in use */
130 static int pwc_preferred_compression = 2; /* 0..3 = uncompressed..high */
133 char serial_number[30];
135 struct pwc_device *pdev;
136 } device_hint[MAX_DEV_HINTS];
140 static int pwc_video_open(struct inode *inode, struct file *file);
141 static int pwc_video_close(struct inode *inode, struct file *file);
142 static ssize_t pwc_video_read(struct file *file, char __user * buf,
143 size_t count, loff_t *ppos);
144 static unsigned int pwc_video_poll(struct file *file, poll_table *wait);
145 static int pwc_video_ioctl(struct inode *inode, struct file *file,
146 unsigned int ioctlnr, unsigned long arg);
147 static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma);
149 static struct file_operations pwc_fops = {
150 .owner = THIS_MODULE,
151 .open = pwc_video_open,
152 .release = pwc_video_close,
153 .read = pwc_video_read,
154 .poll = pwc_video_poll,
155 .mmap = pwc_video_mmap,
156 .ioctl = pwc_video_ioctl,
157 .compat_ioctl = v4l_compat_ioctl32,
160 static struct video_device pwc_template = {
161 .owner = THIS_MODULE,
162 .name = "Philips Webcam", /* Filled in later */
163 .type = VID_TYPE_CAPTURE,
164 .hardware = VID_HARDWARE_PWC,
165 .release = video_device_release,
170 /***************************************************************************/
172 /* Okay, this is some magic that I worked out and the reasoning behind it...
174 The biggest problem with any USB device is of course: "what to do
175 when the user unplugs the device while it is in use by an application?"
176 We have several options:
177 1) Curse them with the 7 plagues when they do (requires divine intervention)
178 2) Tell them not to (won't work: they'll do it anyway)
179 3) Oops the kernel (this will have a negative effect on a user's uptime)
180 4) Do something sensible.
182 Of course, we go for option 4.
184 It happens that this device will be linked to two times, once from
185 usb_device and once from the video_device in their respective 'private'
186 pointers. This is done when the device is probed() and all initialization
187 succeeded. The pwc_device struct links back to both structures.
189 When a device is unplugged while in use it will be removed from the
190 list of known USB devices; I also de-register it as a V4L device, but
191 unfortunately I can't free the memory since the struct is still in use
192 by the file descriptor. This free-ing is then deferend until the first
193 opportunity. Crude, but it works.
195 A small 'advantage' is that if a user unplugs the cam and plugs it back
196 in, it should get assigned the same video device minor, but unfortunately
197 it's non-trivial to re-link the cam back to the video device... (that
198 would surely be magic! :))
201 /***************************************************************************/
202 /* Private functions */
204 /* Here we want the physical address of the memory.
205 * This is used when initializing the contents of the area.
207 static inline unsigned long kvirt_to_pa(unsigned long adr)
209 unsigned long kva, ret;
211 kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
212 kva |= adr & (PAGE_SIZE-1); /* restore the offset */
217 static void * rvmalloc(unsigned long size)
222 size=PAGE_ALIGN(size);
223 mem=vmalloc_32(size);
226 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
227 adr=(unsigned long) mem;
230 SetPageReserved(vmalloc_to_page((void *)adr));
238 static void rvfree(void * mem, unsigned long size)
244 adr=(unsigned long) mem;
245 while ((long) size > 0)
247 ClearPageReserved(vmalloc_to_page((void *)adr));
258 static int pwc_allocate_buffers(struct pwc_device *pdev)
263 Trace(TRACE_MEMORY, ">> pwc_allocate_buffers(pdev = 0x%p)\n", pdev);
269 if (pdev->magic != PWC_MAGIC) {
270 Err("allocate_buffers(): magic failed.\n");
274 /* Allocate Isochronous pipe buffers */
275 for (i = 0; i < MAX_ISO_BUFS; i++) {
276 if (pdev->sbuf[i].data == NULL) {
277 kbuf = kmalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
279 Err("Failed to allocate iso buffer %d.\n", i);
282 Trace(TRACE_MEMORY, "Allocated iso buffer at %p.\n", kbuf);
283 pdev->sbuf[i].data = kbuf;
284 memset(kbuf, 0, ISO_BUFFER_SIZE);
288 /* Allocate frame buffer structure */
289 if (pdev->fbuf == NULL) {
290 kbuf = kmalloc(default_fbufs * sizeof(struct pwc_frame_buf), GFP_KERNEL);
292 Err("Failed to allocate frame buffer structure.\n");
295 Trace(TRACE_MEMORY, "Allocated frame buffer structure at %p.\n", kbuf);
297 memset(kbuf, 0, default_fbufs * sizeof(struct pwc_frame_buf));
299 /* create frame buffers, and make circular ring */
300 for (i = 0; i < default_fbufs; i++) {
301 if (pdev->fbuf[i].data == NULL) {
302 kbuf = vmalloc(PWC_FRAME_SIZE); /* need vmalloc since frame buffer > 128K */
304 Err("Failed to allocate frame buffer %d.\n", i);
307 Trace(TRACE_MEMORY, "Allocated frame buffer %d at %p.\n", i, kbuf);
308 pdev->fbuf[i].data = kbuf;
309 memset(kbuf, 128, PWC_FRAME_SIZE);
313 /* Allocate decompressor table space */
325 Trace(TRACE_MEMORY,"private_data(%zu)\n",sizeof(struct pwc_dec23_private));
326 kbuf = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL); /* Timon & Kiara */
331 kbuf = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL);
336 pdev->decompress_data = kbuf;
338 /* Allocate image buffer; double buffer for mmap() */
339 kbuf = rvmalloc(default_mbufs * pdev->len_per_image);
341 Err("Failed to allocate image buffer(s). needed (%d)\n",default_mbufs * pdev->len_per_image);
344 Trace(TRACE_MEMORY, "Allocated image buffer at %p.\n", kbuf);
345 pdev->image_data = kbuf;
346 for (i = 0; i < default_mbufs; i++)
347 pdev->image_ptr[i] = kbuf + i * pdev->len_per_image;
348 for (; i < MAX_IMAGES; i++)
349 pdev->image_ptr[i] = NULL;
353 Trace(TRACE_MEMORY, "<< pwc_allocate_buffers()\n");
357 static void pwc_free_buffers(struct pwc_device *pdev)
361 Trace(TRACE_MEMORY, "Entering free_buffers(%p).\n", pdev);
366 if (pdev->magic != PWC_MAGIC) {
367 Err("free_buffers(): magic failed.\n");
372 /* Release Iso-pipe buffers */
373 for (i = 0; i < MAX_ISO_BUFS; i++)
374 if (pdev->sbuf[i].data != NULL) {
375 Trace(TRACE_MEMORY, "Freeing ISO buffer at %p.\n", pdev->sbuf[i].data);
376 kfree(pdev->sbuf[i].data);
377 pdev->sbuf[i].data = NULL;
380 /* The same for frame buffers */
381 if (pdev->fbuf != NULL) {
382 for (i = 0; i < default_fbufs; i++) {
383 if (pdev->fbuf[i].data != NULL) {
384 Trace(TRACE_MEMORY, "Freeing frame buffer %d at %p.\n", i, pdev->fbuf[i].data);
385 vfree(pdev->fbuf[i].data);
386 pdev->fbuf[i].data = NULL;
393 /* Intermediate decompression buffer & tables */
394 if (pdev->decompress_data != NULL) {
395 Trace(TRACE_MEMORY, "Freeing decompression buffer at %p.\n", pdev->decompress_data);
396 kfree(pdev->decompress_data);
397 pdev->decompress_data = NULL;
399 pdev->decompressor = NULL;
401 /* Release image buffers */
402 if (pdev->image_data != NULL) {
403 Trace(TRACE_MEMORY, "Freeing image buffer at %p.\n", pdev->image_data);
404 rvfree(pdev->image_data, default_mbufs * pdev->len_per_image);
406 pdev->image_data = NULL;
408 Trace(TRACE_MEMORY, "Leaving free_buffers().\n");
411 /* The frame & image buffer mess.
413 Yes, this is a mess. Well, it used to be simple, but alas... In this
414 module, 3 buffers schemes are used to get the data from the USB bus to
415 the user program. The first scheme involves the ISO buffers (called thus
416 since they transport ISO data from the USB controller), and not really
417 interesting. Suffices to say the data from this buffer is quickly
418 gathered in an interrupt handler (pwc_isoc_handler) and placed into the
421 The frame buffer is the second scheme, and is the central element here.
422 It collects the data from a single frame from the camera (hence, the
423 name). Frames are delimited by the USB camera with a short USB packet,
424 so that's easy to detect. The frame buffers form a list that is filled
425 by the camera+USB controller and drained by the user process through
426 either read() or mmap().
428 The image buffer is the third scheme, in which frames are decompressed
429 and converted into planar format. For mmap() there is more than
430 one image buffer available.
432 The frame buffers provide the image buffering. In case the user process
433 is a bit slow, this introduces lag and some undesired side-effects.
434 The problem arises when the frame buffer is full. I used to drop the last
435 frame, which makes the data in the queue stale very quickly. But dropping
436 the frame at the head of the queue proved to be a litte bit more difficult.
437 I tried a circular linked scheme, but this introduced more problems than
440 Because filling and draining are completely asynchronous processes, this
441 requires some fiddling with pointers and mutexes.
443 Eventually, I came up with a system with 2 lists: an 'empty' frame list
444 and a 'full' frame list:
445 * Initially, all frame buffers but one are on the 'empty' list; the one
446 remaining buffer is our initial fill frame.
447 * If a frame is needed for filling, we try to take it from the 'empty'
448 list, unless that list is empty, in which case we take the buffer at
449 the head of the 'full' list.
450 * When our fill buffer has been filled, it is appended to the 'full'
452 * If a frame is needed by read() or mmap(), it is taken from the head of
453 the 'full' list, handled, and then appended to the 'empty' list. If no
454 buffer is present on the 'full' list, we wait.
455 The advantage is that the buffer that is currently being decompressed/
456 converted, is on neither list, and thus not in our way (any other scheme
457 I tried had the problem of old data lingering in the queue).
459 Whatever strategy you choose, it always remains a tradeoff: with more
460 frame buffers the chances of a missed frame are reduced. On the other
461 hand, on slower machines it introduces lag because the queue will
466 \brief Find next frame buffer to fill. Take from empty or full list, whichever comes first.
468 static inline int pwc_next_fill_frame(struct pwc_device *pdev)
474 spin_lock_irqsave(&pdev->ptrlock, flags);
475 if (pdev->fill_frame != NULL) {
476 /* append to 'full' list */
477 if (pdev->full_frames == NULL) {
478 pdev->full_frames = pdev->fill_frame;
479 pdev->full_frames_tail = pdev->full_frames;
482 pdev->full_frames_tail->next = pdev->fill_frame;
483 pdev->full_frames_tail = pdev->fill_frame;
486 if (pdev->empty_frames != NULL) {
487 /* We have empty frames available. That's easy */
488 pdev->fill_frame = pdev->empty_frames;
489 pdev->empty_frames = pdev->empty_frames->next;
492 /* Hmm. Take it from the full list */
495 if (pdev->full_frames == NULL) {
496 Err("Neither empty or full frames available!\n");
497 spin_unlock_irqrestore(&pdev->ptrlock, flags);
501 pdev->fill_frame = pdev->full_frames;
502 pdev->full_frames = pdev->full_frames->next;
505 pdev->fill_frame->next = NULL;
507 Trace(TRACE_SEQUENCE, "Assigning sequence number %d.\n", pdev->sequence);
508 pdev->fill_frame->sequence = pdev->sequence++;
510 spin_unlock_irqrestore(&pdev->ptrlock, flags);
516 \brief Reset all buffers, pointers and lists, except for the image_used[] buffer.
518 If the image_used[] buffer is cleared too, mmap()/VIDIOCSYNC will run into trouble.
520 static void pwc_reset_buffers(struct pwc_device *pdev)
525 spin_lock_irqsave(&pdev->ptrlock, flags);
526 pdev->full_frames = NULL;
527 pdev->full_frames_tail = NULL;
528 for (i = 0; i < default_fbufs; i++) {
529 pdev->fbuf[i].filled = 0;
531 pdev->fbuf[i].next = &pdev->fbuf[i - 1];
533 pdev->fbuf->next = NULL;
535 pdev->empty_frames = &pdev->fbuf[default_fbufs - 1];
536 pdev->empty_frames_tail = pdev->fbuf;
537 pdev->read_frame = NULL;
538 pdev->fill_frame = pdev->empty_frames;
539 pdev->empty_frames = pdev->empty_frames->next;
541 pdev->image_read_pos = 0;
542 pdev->fill_image = 0;
543 spin_unlock_irqrestore(&pdev->ptrlock, flags);
548 \brief Do all the handling for getting one frame: get pointer, decompress, advance pointers.
550 static int pwc_handle_frame(struct pwc_device *pdev)
555 spin_lock_irqsave(&pdev->ptrlock, flags);
556 /* First grab our read_frame; this is removed from all lists, so
557 we can release the lock after this without problems */
558 if (pdev->read_frame != NULL) {
559 /* This can't theoretically happen */
560 Err("Huh? Read frame still in use?\n");
563 if (pdev->full_frames == NULL) {
564 Err("Woops. No frames ready.\n");
567 pdev->read_frame = pdev->full_frames;
568 pdev->full_frames = pdev->full_frames->next;
569 pdev->read_frame->next = NULL;
572 if (pdev->read_frame != NULL) {
574 Trace(TRACE_SEQUENCE, "Decompressing frame %d\n", pdev->read_frame->sequence);
576 /* Decompression is a lenghty process, so it's outside of the lock.
577 This gives the isoc_handler the opportunity to fill more frames
580 spin_unlock_irqrestore(&pdev->ptrlock, flags);
581 ret = pwc_decompress(pdev);
582 spin_lock_irqsave(&pdev->ptrlock, flags);
584 /* We're done with read_buffer, tack it to the end of the empty buffer list */
585 if (pdev->empty_frames == NULL) {
586 pdev->empty_frames = pdev->read_frame;
587 pdev->empty_frames_tail = pdev->empty_frames;
590 pdev->empty_frames_tail->next = pdev->read_frame;
591 pdev->empty_frames_tail = pdev->read_frame;
593 pdev->read_frame = NULL;
596 spin_unlock_irqrestore(&pdev->ptrlock, flags);
601 \brief Advance pointers of image buffer (after each user request)
603 static inline void pwc_next_image(struct pwc_device *pdev)
605 pdev->image_used[pdev->fill_image] = 0;
606 pdev->fill_image = (pdev->fill_image + 1) % default_mbufs;
610 /* This gets called for the Isochronous pipe (video). This is done in
611 * interrupt time, so it has to be fast, not crash, and not stall. Neat.
613 static void pwc_isoc_handler(struct urb *urb, struct pt_regs *regs)
615 struct pwc_device *pdev;
618 struct pwc_frame_buf *fbuf;
619 unsigned char *fillptr = NULL, *iso_buf = NULL;
622 pdev = (struct pwc_device *)urb->context;
624 Err("isoc_handler() called with NULL device?!\n");
628 if (pdev->magic != PWC_MAGIC) {
629 Err("isoc_handler() called with bad magic!\n");
633 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
634 Trace(TRACE_OPEN, "pwc_isoc_handler(): URB (%p) unlinked %ssynchronuously.\n", urb, urb->status == -ENOENT ? "" : "a");
637 if (urb->status != -EINPROGRESS && urb->status != 0) {
641 switch(urb->status) {
642 case -ENOSR: errmsg = "Buffer error (overrun)"; break;
643 case -EPIPE: errmsg = "Stalled (device not responding)"; break;
644 case -EOVERFLOW: errmsg = "Babble (bad cable?)"; break;
645 case -EPROTO: errmsg = "Bit-stuff error (bad cable?)"; break;
646 case -EILSEQ: errmsg = "CRC/Timeout (could be anything)"; break;
647 case -ETIMEDOUT: errmsg = "NAK (device does not respond)"; break;
649 Trace(TRACE_FLOW, "pwc_isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
650 /* Give up after a number of contiguous errors on the USB bus.
651 Appearantly something is wrong so we simulate an unplug event.
653 if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
655 Info("Too many ISOC errors, bailing out.\n");
656 pdev->error_status = EIO;
658 wake_up_interruptible(&pdev->frameq);
660 goto handler_end; // ugly, but practical
663 fbuf = pdev->fill_frame;
665 Err("pwc_isoc_handler without valid fill frame.\n");
670 fillptr = fbuf->data + fbuf->filled;
673 /* Reset ISOC error counter. We did get here, after all. */
674 pdev->visoc_errors = 0;
676 /* vsync: 0 = don't copy data
681 for (i = 0; i < urb->number_of_packets; i++) {
682 fst = urb->iso_frame_desc[i].status;
683 flen = urb->iso_frame_desc[i].actual_length;
684 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
686 if (flen > 0) { /* if valid data... */
687 if (pdev->vsync > 0) { /* ...and we are not sync-hunting... */
690 /* ...copy data to frame buffer, if possible */
691 if (flen + fbuf->filled > pdev->frame_total_size) {
692 Trace(TRACE_FLOW, "Frame buffer overflow (flen = %d, frame_total_size = %d).\n", flen, pdev->frame_total_size);
693 pdev->vsync = 0; /* Hmm, let's wait for an EOF (end-of-frame) */
694 pdev->vframes_error++;
697 memmove(fillptr, iso_buf, flen);
701 fbuf->filled += flen;
704 if (flen < pdev->vlast_packet_size) {
705 /* Shorter packet... We probably have the end of an image-frame;
706 wake up read() process and let select()/poll() do something.
707 Decompression is done in user time over there.
709 if (pdev->vsync == 2) {
710 /* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
711 frames on the USB wire after an exposure change. This conditition is
712 however detected in the cam and a bit is set in the header.
714 if (pdev->type == 730) {
715 unsigned char *ptr = (unsigned char *)fbuf->data;
717 if (ptr[1] == 1 && ptr[0] & 0x10) {
719 Debug("Hyundai CMOS sensor bug. Dropping frame %d.\n", fbuf->sequence);
721 pdev->drop_frames += 2;
722 pdev->vframes_error++;
724 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
726 Info("Snapshot button pressed.\n");
728 Info("Snapshot button released.\n");
730 if ((ptr[0] ^ pdev->vmirror) & 0x02) {
732 Info("Image is mirrored.\n");
734 Info("Image is normal.\n");
736 pdev->vmirror = ptr[0] & 0x03;
737 /* Sometimes the trailer of the 730 is still sent as a 4 byte packet
738 after a short frame; this condition is filtered out specifically. A 4 byte
739 frame doesn't make sense anyway.
740 So we get either this sequence:
741 drop_bit set -> 4 byte frame -> short frame -> good frame
743 drop_bit set -> short frame -> good frame
744 So we drop either 3 or 2 frames in all!
746 if (fbuf->filled == 4)
750 /* In case we were instructed to drop the frame, do so silently.
751 The buffer pointers are not updated either (but the counters are reset below).
753 if (pdev->drop_frames > 0)
756 /* Check for underflow first */
757 if (fbuf->filled < pdev->frame_total_size) {
758 Trace(TRACE_FLOW, "Frame buffer underflow (%d bytes); discarded.\n", fbuf->filled);
759 pdev->vframes_error++;
762 /* Send only once per EOF */
763 awake = 1; /* delay wake_ups */
765 /* Find our next frame to fill. This will always succeed, since we
766 * nick a frame from either empty or full list, but if we had to
767 * take it from the full list, it means a frame got dropped.
769 if (pwc_next_fill_frame(pdev)) {
770 pdev->vframes_dumped++;
771 if ((pdev->vframe_count > FRAME_LOWMARK) && (pwc_trace & TRACE_FLOW)) {
772 if (pdev->vframes_dumped < 20)
773 Trace(TRACE_FLOW, "Dumping frame %d.\n", pdev->vframe_count);
774 if (pdev->vframes_dumped == 20)
775 Trace(TRACE_FLOW, "Dumping frame %d (last message).\n", pdev->vframe_count);
778 fbuf = pdev->fill_frame;
781 pdev->vframe_count++;
784 fillptr = fbuf->data;
786 } /* .. flen < last_packet_size */
787 pdev->vlast_packet_size = flen;
788 } /* ..status == 0 */
790 /* This is normally not interesting to the user, unless you are really debugging something */
792 static int iso_error = 0;
795 Trace(TRACE_FLOW, "Iso frame %d of USB has error %d\n", i, fst);
802 wake_up_interruptible(&pdev->frameq);
804 urb->dev = pdev->udev;
805 i = usb_submit_urb(urb, GFP_ATOMIC);
807 Err("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
811 static int pwc_isoc_init(struct pwc_device *pdev)
813 struct usb_device *udev;
817 struct usb_interface *intf;
818 struct usb_host_interface *idesc = NULL;
827 /* Get the current alternate interface, adjust packet size */
828 if (!udev->actconfig)
830 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5)
831 idesc = &udev->actconfig->interface[0]->altsetting[pdev->valternate];
833 intf = usb_ifnum_to_if(udev, 0);
835 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
841 /* Search video endpoint */
842 pdev->vmax_packet_size = -1;
843 for (i = 0; i < idesc->desc.bNumEndpoints; i++)
844 if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
845 pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
849 if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
850 Err("Failed to find packet size for video endpoint in current alternate setting.\n");
851 return -ENFILE; /* Odd error, that should be noticeable */
854 /* Set alternate interface */
856 Trace(TRACE_OPEN, "Setting alternate interface %d\n", pdev->valternate);
857 ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
861 for (i = 0; i < MAX_ISO_BUFS; i++) {
862 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
864 Err("Failed to allocate urb %d\n", i);
868 pdev->sbuf[i].urb = urb;
869 Trace(TRACE_MEMORY, "Allocated URB at 0x%p\n", urb);
872 /* De-allocate in reverse order */
874 if (pdev->sbuf[i].urb != NULL)
875 usb_free_urb(pdev->sbuf[i].urb);
876 pdev->sbuf[i].urb = NULL;
882 /* init URB structure */
883 for (i = 0; i < MAX_ISO_BUFS; i++) {
884 urb = pdev->sbuf[i].urb;
886 urb->interval = 1; // devik
888 urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
889 urb->transfer_flags = URB_ISO_ASAP;
890 urb->transfer_buffer = pdev->sbuf[i].data;
891 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
892 urb->complete = pwc_isoc_handler;
894 urb->start_frame = 0;
895 urb->number_of_packets = ISO_FRAMES_PER_DESC;
896 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
897 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
898 urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
903 for (i = 0; i < MAX_ISO_BUFS; i++) {
904 ret = usb_submit_urb(pdev->sbuf[i].urb, GFP_KERNEL);
906 Err("isoc_init() submit_urb %d failed with error %d\n", i, ret);
908 Trace(TRACE_MEMORY, "URB 0x%p submitted.\n", pdev->sbuf[i].urb);
913 Trace(TRACE_OPEN, "<< pwc_isoc_init()\n");
917 static void pwc_isoc_cleanup(struct pwc_device *pdev)
921 Trace(TRACE_OPEN, ">> pwc_isoc_cleanup()\n");
925 /* Unlinking ISOC buffers one by one */
926 for (i = 0; i < MAX_ISO_BUFS; i++) {
929 urb = pdev->sbuf[i].urb;
931 if (pdev->iso_init) {
932 Trace(TRACE_MEMORY, "Unlinking URB %p\n", urb);
935 Trace(TRACE_MEMORY, "Freeing URB\n");
937 pdev->sbuf[i].urb = NULL;
941 /* Stop camera, but only if we are sure the camera is still there (unplug
942 is signalled by EPIPE)
944 if (pdev->error_status && pdev->error_status != EPIPE) {
945 Trace(TRACE_OPEN, "Setting alternate interface 0.\n");
946 usb_set_interface(pdev->udev, 0, 0);
950 Trace(TRACE_OPEN, "<< pwc_isoc_cleanup()\n");
953 int pwc_try_video_mode(struct pwc_device *pdev, int width, int height, int new_fps, int new_compression, int new_snapshot)
957 /* Stop isoc stuff */
958 pwc_isoc_cleanup(pdev);
959 /* Reset parameters */
960 pwc_reset_buffers(pdev);
961 /* Try to set video mode... */
962 start = ret = pwc_set_video_mode(pdev, width, height, new_fps, new_compression, new_snapshot);
964 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 1 failed.\n");
965 /* That failed... restore old mode (we know that worked) */
966 start = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
968 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 2 failed.\n");
973 if (pwc_isoc_init(pdev) < 0)
975 Info("Failed to restart ISOC transfers in pwc_try_video_mode.\n");
976 ret = -EAGAIN; /* let's try again, who knows if it works a second time */
979 pdev->drop_frames++; /* try to avoid garbage during switch */
980 return ret; /* Return original error code */
984 /***************************************************************************/
985 /* Video4Linux functions */
987 static int pwc_video_open(struct inode *inode, struct file *file)
990 struct video_device *vdev = video_devdata(file);
991 struct pwc_device *pdev;
993 Trace(TRACE_OPEN, ">> video_open called(vdev = 0x%p).\n", vdev);
995 pdev = (struct pwc_device *)vdev->priv;
1001 down(&pdev->modlock);
1002 if (!pdev->usb_init) {
1003 Trace(TRACE_OPEN, "Doing first time initialization.\n");
1006 if (pwc_trace & TRACE_OPEN)
1008 /* Query sensor type */
1009 const char *sensor_type = NULL;
1012 ret = pwc_get_cmos_sensor(pdev, &i);
1016 case 0x00: sensor_type = "Hyundai CMOS sensor"; break;
1017 case 0x20: sensor_type = "Sony CCD sensor + TDA8787"; break;
1018 case 0x2E: sensor_type = "Sony CCD sensor + Exas 98L59"; break;
1019 case 0x2F: sensor_type = "Sony CCD sensor + ADI 9804"; break;
1020 case 0x30: sensor_type = "Sharp CCD sensor + TDA8787"; break;
1021 case 0x3E: sensor_type = "Sharp CCD sensor + Exas 98L59"; break;
1022 case 0x3F: sensor_type = "Sharp CCD sensor + ADI 9804"; break;
1023 case 0x40: sensor_type = "UPA 1021 sensor"; break;
1024 case 0x100: sensor_type = "VGA sensor"; break;
1025 case 0x101: sensor_type = "PAL MR sensor"; break;
1026 default: sensor_type = "unknown type of sensor"; break;
1029 if (sensor_type != NULL)
1030 Info("This %s camera is equipped with a %s (%d).\n", pdev->vdev->name, sensor_type, i);
1034 /* Turn on camera */
1036 i = pwc_camera_power(pdev, 1);
1038 Info("Failed to restore power to the camera! (%d)\n", i);
1040 /* Set LED on/off time */
1041 if (pwc_set_leds(pdev, led_on, led_off) < 0)
1042 Info("Failed to set LED on/off time.\n");
1044 pwc_construct(pdev); /* set min/max sizes correct */
1046 /* So far, so good. Allocate memory. */
1047 i = pwc_allocate_buffers(pdev);
1049 Trace(TRACE_OPEN, "Failed to allocate buffer memory.\n");
1054 /* Reset buffers & parameters */
1055 pwc_reset_buffers(pdev);
1056 for (i = 0; i < default_mbufs; i++)
1057 pdev->image_used[i] = 0;
1058 pdev->vframe_count = 0;
1059 pdev->vframes_dumped = 0;
1060 pdev->vframes_error = 0;
1061 pdev->visoc_errors = 0;
1062 pdev->error_status = 0;
1066 pwc_construct(pdev); /* set min/max sizes correct */
1068 /* Set some defaults */
1069 pdev->vsnapshot = 0;
1071 /* Start iso pipe for video; first try the last used video size
1072 (or the default one); if that fails try QCIF/10 or QSIF/10;
1073 it that fails too, give up.
1075 i = pwc_set_video_mode(pdev, pwc_image_sizes[pdev->vsize].x, pwc_image_sizes[pdev->vsize].y, pdev->vframes, pdev->vcompression, 0);
1077 Trace(TRACE_OPEN, "First attempt at set_video_mode failed.\n");
1078 if (pdev->type == 730 || pdev->type == 740 || pdev->type == 750)
1079 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QSIF].x, pwc_image_sizes[PSZ_QSIF].y, 10, pdev->vcompression, 0);
1081 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QCIF].x, pwc_image_sizes[PSZ_QCIF].y, 10, pdev->vcompression, 0);
1084 Trace(TRACE_OPEN, "Second attempt at set_video_mode failed.\n");
1089 i = pwc_isoc_init(pdev);
1091 Trace(TRACE_OPEN, "Failed to init ISOC stuff = %d.\n", i);
1097 file->private_data = vdev;
1099 Trace(TRACE_OPEN, "<< video_open() returns 0.\n");
1103 /* Note that all cleanup is done in the reverse order as in _open */
1104 static int pwc_video_close(struct inode *inode, struct file *file)
1106 struct video_device *vdev = file->private_data;
1107 struct pwc_device *pdev;
1110 Trace(TRACE_OPEN, ">> video_close called(vdev = 0x%p).\n", vdev);
1112 pdev = (struct pwc_device *)vdev->priv;
1113 if (pdev->vopen == 0)
1114 Info("video_close() called on closed device?\n");
1116 /* Dump statistics, but only if a reasonable amount of frames were
1117 processed (to prevent endless log-entries in case of snap-shot
1120 if (pdev->vframe_count > 20)
1121 Info("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev->vframe_count, pdev->vframes_dumped, pdev->vframes_error);
1132 /* pwc_dec23_exit(); *//* Timon & Kiara */
1136 /* pwc_dec1_exit(); */
1140 pwc_isoc_cleanup(pdev);
1141 pwc_free_buffers(pdev);
1143 /* Turn off LEDS and power down camera, but only when not unplugged */
1144 if (pdev->error_status != EPIPE) {
1146 if (pwc_set_leds(pdev, 0, 0) < 0)
1147 Info("Failed to set LED on/off time.\n");
1149 i = pwc_camera_power(pdev, 0);
1151 Err("Failed to power down camera (%d)\n", i);
1155 Trace(TRACE_OPEN, "<< video_close()\n");
1160 * FIXME: what about two parallel reads ????
1161 * ANSWER: Not supported. You can't open the device more than once,
1162 despite what the V4L1 interface says. First, I don't see
1163 the need, second there's no mechanism of alerting the
1164 2nd/3rd/... process of events like changing image size.
1165 And I don't see the point of blocking that for the
1166 2nd/3rd/... process.
1167 In multi-threaded environments reading parallel from any
1168 device is tricky anyhow.
1171 static ssize_t pwc_video_read(struct file *file, char __user * buf,
1172 size_t count, loff_t *ppos)
1174 struct video_device *vdev = file->private_data;
1175 struct pwc_device *pdev;
1176 int noblock = file->f_flags & O_NONBLOCK;
1177 DECLARE_WAITQUEUE(wait, current);
1180 Trace(TRACE_READ, "video_read(0x%p, %p, %zu) called.\n", vdev, buf, count);
1186 if (pdev->error_status)
1187 return -pdev->error_status; /* Something happened, report what. */
1189 /* In case we're doing partial reads, we don't have to wait for a frame */
1190 if (pdev->image_read_pos == 0) {
1191 /* Do wait queueing according to the (doc)book */
1192 add_wait_queue(&pdev->frameq, &wait);
1193 while (pdev->full_frames == NULL) {
1194 /* Check for unplugged/etc. here */
1195 if (pdev->error_status) {
1196 remove_wait_queue(&pdev->frameq, &wait);
1197 set_current_state(TASK_RUNNING);
1198 return -pdev->error_status ;
1201 remove_wait_queue(&pdev->frameq, &wait);
1202 set_current_state(TASK_RUNNING);
1203 return -EWOULDBLOCK;
1205 if (signal_pending(current)) {
1206 remove_wait_queue(&pdev->frameq, &wait);
1207 set_current_state(TASK_RUNNING);
1208 return -ERESTARTSYS;
1211 set_current_state(TASK_INTERRUPTIBLE);
1213 remove_wait_queue(&pdev->frameq, &wait);
1214 set_current_state(TASK_RUNNING);
1216 /* Decompress and release frame */
1217 if (pwc_handle_frame(pdev))
1221 Trace(TRACE_READ, "Copying data to user space.\n");
1222 if (pdev->vpalette == VIDEO_PALETTE_RAW)
1223 bytes_to_read = pdev->frame_size;
1225 bytes_to_read = pdev->view.size;
1227 /* copy bytes to user space; we allow for partial reads */
1228 if (count + pdev->image_read_pos > bytes_to_read)
1229 count = bytes_to_read - pdev->image_read_pos;
1230 if (copy_to_user(buf, pdev->image_ptr[pdev->fill_image] + pdev->image_read_pos, count))
1232 pdev->image_read_pos += count;
1233 if (pdev->image_read_pos >= bytes_to_read) { /* All data has been read */
1234 pdev->image_read_pos = 0;
1235 pwc_next_image(pdev);
1240 static unsigned int pwc_video_poll(struct file *file, poll_table *wait)
1242 struct video_device *vdev = file->private_data;
1243 struct pwc_device *pdev;
1251 poll_wait(file, &pdev->frameq, wait);
1252 if (pdev->error_status)
1254 if (pdev->full_frames != NULL) /* we have frames waiting */
1255 return (POLLIN | POLLRDNORM);
1260 static int pwc_video_do_ioctl(struct inode *inode, struct file *file,
1261 unsigned int cmd, void *arg)
1263 struct video_device *vdev = file->private_data;
1264 struct pwc_device *pdev;
1265 DECLARE_WAITQUEUE(wait, current);
1274 /* Query cabapilities */
1277 struct video_capability *caps = arg;
1279 strcpy(caps->name, vdev->name);
1280 caps->type = VID_TYPE_CAPTURE;
1283 caps->minwidth = pdev->view_min.x;
1284 caps->minheight = pdev->view_min.y;
1285 caps->maxwidth = pdev->view_max.x;
1286 caps->maxheight = pdev->view_max.y;
1290 /* Channel functions (simulate 1 channel) */
1293 struct video_channel *v = arg;
1295 if (v->channel != 0)
1299 v->type = VIDEO_TYPE_CAMERA;
1300 strcpy(v->name, "Webcam");
1306 /* The spec says the argument is an integer, but
1307 the bttv driver uses a video_channel arg, which
1308 makes sense becasue it also has the norm flag.
1310 struct video_channel *v = arg;
1311 if (v->channel != 0)
1317 /* Picture functions; contrast etc. */
1320 struct video_picture *p = arg;
1323 val = pwc_get_brightness(pdev);
1325 p->brightness = val;
1327 p->brightness = 0xffff;
1328 val = pwc_get_contrast(pdev);
1332 p->contrast = 0xffff;
1333 /* Gamma, Whiteness, what's the difference? :) */
1334 val = pwc_get_gamma(pdev);
1338 p->whiteness = 0xffff;
1339 val = pwc_get_saturation(pdev);
1345 p->palette = pdev->vpalette;
1346 p->hue = 0xFFFF; /* N/A */
1352 struct video_picture *p = arg;
1354 * FIXME: Suppose we are mid read
1355 ANSWER: No problem: the firmware of the camera
1356 can handle brightness/contrast/etc
1357 changes at _any_ time, and the palette
1358 is used exactly once in the uncompress
1361 pwc_set_brightness(pdev, p->brightness);
1362 pwc_set_contrast(pdev, p->contrast);
1363 pwc_set_gamma(pdev, p->whiteness);
1364 pwc_set_saturation(pdev, p->colour);
1365 if (p->palette && p->palette != pdev->vpalette) {
1366 switch (p->palette) {
1367 case VIDEO_PALETTE_YUV420P:
1368 case VIDEO_PALETTE_RAW:
1369 pdev->vpalette = p->palette;
1370 return pwc_try_video_mode(pdev, pdev->image.x, pdev->image.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1380 /* Window/size parameters */
1383 struct video_window *vw = arg;
1387 vw->width = pdev->view.x;
1388 vw->height = pdev->view.y;
1390 vw->flags = (pdev->vframes << PWC_FPS_SHIFT) |
1391 (pdev->vsnapshot ? PWC_FPS_SNAPSHOT : 0);
1397 struct video_window *vw = arg;
1398 int fps, snapshot, ret;
1400 fps = (vw->flags & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
1401 snapshot = vw->flags & PWC_FPS_SNAPSHOT;
1403 fps = pdev->vframes;
1404 if (pdev->view.x == vw->width && pdev->view.y && fps == pdev->vframes && snapshot == pdev->vsnapshot)
1406 ret = pwc_try_video_mode(pdev, vw->width, vw->height, fps, pdev->vcompression, snapshot);
1412 /* We don't have overlay support (yet) */
1415 struct video_buffer *vb = arg;
1417 memset(vb,0,sizeof(*vb));
1421 /* mmap() functions */
1424 /* Tell the user program how much memory is needed for a mmap() */
1425 struct video_mbuf *vm = arg;
1428 memset(vm, 0, sizeof(*vm));
1429 vm->size = default_mbufs * pdev->len_per_image;
1430 vm->frames = default_mbufs; /* double buffering should be enough for most applications */
1431 for (i = 0; i < default_mbufs; i++)
1432 vm->offsets[i] = i * pdev->len_per_image;
1436 case VIDIOCMCAPTURE:
1438 /* Start capture into a given image buffer (called 'frame' in video_mmap structure) */
1439 struct video_mmap *vm = arg;
1441 Trace(TRACE_READ, "VIDIOCMCAPTURE: %dx%d, frame %d, format %d\n", vm->width, vm->height, vm->frame, vm->format);
1442 if (vm->frame < 0 || vm->frame >= default_mbufs)
1445 /* xawtv is nasty. It probes the available palettes
1446 by setting a very small image size and trying
1447 various palettes... The driver doesn't support
1448 such small images, so I'm working around it.
1454 case VIDEO_PALETTE_YUV420P:
1455 case VIDEO_PALETTE_RAW:
1463 if ((vm->width != pdev->view.x || vm->height != pdev->view.y) &&
1464 (vm->width >= pdev->view_min.x && vm->height >= pdev->view_min.y)) {
1467 Trace(TRACE_OPEN, "VIDIOCMCAPTURE: changing size to please xawtv :-(.\n");
1468 ret = pwc_try_video_mode(pdev, vm->width, vm->height, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1471 } /* ... size mismatch */
1473 /* FIXME: should we lock here? */
1474 if (pdev->image_used[vm->frame])
1475 return -EBUSY; /* buffer wasn't available. Bummer */
1476 pdev->image_used[vm->frame] = 1;
1478 /* Okay, we're done here. In the SYNC call we wait until a
1479 frame comes available, then expand image into the given
1481 In contrast to the CPiA cam the Philips cams deliver a
1482 constant stream, almost like a grabber card. Also,
1483 we have separate buffers for the rawdata and the image,
1484 meaning we can nearly always expand into the requested buffer.
1486 Trace(TRACE_READ, "VIDIOCMCAPTURE done.\n");
1492 /* The doc says: "Whenever a buffer is used it should
1493 call VIDIOCSYNC to free this frame up and continue."
1495 The only odd thing about this whole procedure is
1496 that MCAPTURE flags the buffer as "in use", and
1497 SYNC immediately unmarks it, while it isn't
1498 after SYNC that you know that the buffer actually
1499 got filled! So you better not start a CAPTURE in
1500 the same frame immediately (use double buffering).
1501 This is not a problem for this cam, since it has
1502 extra intermediate buffers, but a hardware
1503 grabber card will then overwrite the buffer
1509 Trace(TRACE_READ, "VIDIOCSYNC called (%d).\n", *mbuf);
1512 if (*mbuf < 0 || *mbuf >= default_mbufs)
1514 /* check if this buffer was requested anyway */
1515 if (pdev->image_used[*mbuf] == 0)
1518 /* Add ourselves to the frame wait-queue.
1520 FIXME: needs auditing for safety.
1521 QUESTION: In what respect? I think that using the
1524 add_wait_queue(&pdev->frameq, &wait);
1525 while (pdev->full_frames == NULL) {
1526 if (pdev->error_status) {
1527 remove_wait_queue(&pdev->frameq, &wait);
1528 set_current_state(TASK_RUNNING);
1529 return -pdev->error_status;
1532 if (signal_pending(current)) {
1533 remove_wait_queue(&pdev->frameq, &wait);
1534 set_current_state(TASK_RUNNING);
1535 return -ERESTARTSYS;
1538 set_current_state(TASK_INTERRUPTIBLE);
1540 remove_wait_queue(&pdev->frameq, &wait);
1541 set_current_state(TASK_RUNNING);
1543 /* The frame is ready. Expand in the image buffer
1544 requested by the user. I don't care if you
1545 mmap() 5 buffers and request data in this order:
1546 buffer 4 2 3 0 1 2 3 0 4 3 1 . . .
1547 Grabber hardware may not be so forgiving.
1549 Trace(TRACE_READ, "VIDIOCSYNC: frame ready.\n");
1550 pdev->fill_image = *mbuf; /* tell in which buffer we want the image to be expanded */
1551 /* Decompress, etc */
1552 ret = pwc_handle_frame(pdev);
1553 pdev->image_used[*mbuf] = 0;
1561 struct video_audio *v = arg;
1563 strcpy(v->name, "Microphone");
1564 v->audio = -1; /* unknown audio minor */
1566 v->mode = VIDEO_SOUND_MONO;
1570 v->balance = 0x8000;
1577 /* Dummy: nothing can be set */
1583 struct video_unit *vu = arg;
1585 vu->video = pdev->vdev->minor & 0x3F;
1586 vu->audio = -1; /* not known yet */
1593 return pwc_ioctl(pdev, cmd, arg);
1598 static int pwc_video_ioctl(struct inode *inode, struct file *file,
1599 unsigned int cmd, unsigned long arg)
1601 return video_usercopy(inode, file, cmd, arg, pwc_video_do_ioctl);
1605 static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma)
1607 struct video_device *vdev = file->private_data;
1608 struct pwc_device *pdev;
1609 unsigned long start = vma->vm_start;
1610 unsigned long size = vma->vm_end-vma->vm_start;
1611 unsigned long page, pos;
1613 Trace(TRACE_MEMORY, "mmap(0x%p, 0x%lx, %lu) called.\n", vdev, start, size);
1616 vma->vm_flags |= VM_IO;
1618 pos = (unsigned long)pdev->image_data;
1620 page = vmalloc_to_pfn((void *)pos);
1621 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1626 if (size > PAGE_SIZE)
1635 /***************************************************************************/
1638 /* This function gets called when a new device is plugged in or the usb core
1642 static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
1644 struct usb_device *udev = interface_to_usbdev(intf);
1645 struct pwc_device *pdev = NULL;
1646 int vendor_id, product_id, type_id;
1649 int video_nr = -1; /* default: use next available device */
1650 char serial_number[30], *name;
1652 /* Check if we can handle this device */
1653 Trace(TRACE_PROBE, "probe() called [%04X %04X], if %d\n",
1654 le16_to_cpu(udev->descriptor.idVendor),
1655 le16_to_cpu(udev->descriptor.idProduct),
1656 intf->altsetting->desc.bInterfaceNumber);
1658 /* the interfaces are probed one by one. We are only interested in the
1659 video interface (0) now.
1660 Interface 1 is the Audio Control, and interface 2 Audio itself.
1662 if (intf->altsetting->desc.bInterfaceNumber > 0)
1665 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
1666 product_id = le16_to_cpu(udev->descriptor.idProduct);
1668 if (vendor_id == 0x0471) {
1669 switch (product_id) {
1671 Info("Philips PCA645VC USB webcam detected.\n");
1672 name = "Philips 645 webcam";
1676 Info("Philips PCA646VC USB webcam detected.\n");
1677 name = "Philips 646 webcam";
1681 Info("Askey VC010 type 2 USB webcam detected.\n");
1682 name = "Askey VC010 webcam";
1686 Info("Philips PCVC675K (Vesta) USB webcam detected.\n");
1687 name = "Philips 675 webcam";
1691 Info("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
1692 name = "Philips 680 webcam";
1696 Info("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
1697 name = "Philips 690 webcam";
1701 Info("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
1702 name = "Philips 730 webcam";
1706 Info("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
1707 name = "Philips 740 webcam";
1711 Info("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
1712 name = "Philips 750 webcam";
1716 Info("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
1717 name = "Philips 720K/40 webcam";
1725 else if (vendor_id == 0x069A) {
1726 switch(product_id) {
1728 Info("Askey VC010 type 1 USB webcam detected.\n");
1729 name = "Askey VC010 webcam";
1737 else if (vendor_id == 0x046d) {
1738 switch(product_id) {
1740 Info("Logitech QuickCam Pro 3000 USB webcam detected.\n");
1741 name = "Logitech QuickCam Pro 3000";
1742 type_id = 740; /* CCD sensor */
1745 Info("Logitech QuickCam Notebook Pro USB webcam detected.\n");
1746 name = "Logitech QuickCam Notebook Pro";
1747 type_id = 740; /* CCD sensor */
1750 Info("Logitech QuickCam 4000 Pro USB webcam detected.\n");
1751 name = "Logitech QuickCam Pro 4000";
1752 type_id = 740; /* CCD sensor */
1755 Info("Logitech QuickCam Zoom USB webcam detected.\n");
1756 name = "Logitech QuickCam Zoom";
1757 type_id = 740; /* CCD sensor */
1760 Info("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
1761 name = "Logitech QuickCam Zoom";
1762 type_id = 740; /* CCD sensor */
1765 Info("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
1766 name = "Logitech QuickCam Orbit";
1767 type_id = 740; /* CCD sensor */
1768 features |= FEATURE_MOTOR_PANTILT;
1773 Info("Logitech QuickCam detected (reserved ID).\n");
1774 name = "Logitech QuickCam (res.)";
1775 type_id = 730; /* Assuming CMOS */
1782 else if (vendor_id == 0x055d) {
1783 /* I don't know the difference between the C10 and the C30;
1784 I suppose the difference is the sensor, but both cameras
1785 work equally well with a type_id of 675
1787 switch(product_id) {
1789 Info("Samsung MPC-C10 USB webcam detected.\n");
1790 name = "Samsung MPC-C10";
1794 Info("Samsung MPC-C30 USB webcam detected.\n");
1795 name = "Samsung MPC-C30";
1803 else if (vendor_id == 0x041e) {
1804 switch(product_id) {
1806 Info("Creative Labs Webcam 5 detected.\n");
1807 name = "Creative Labs Webcam 5";
1811 Info("Creative Labs Webcam Pro Ex detected.\n");
1812 name = "Creative Labs Webcam Pro Ex";
1820 else if (vendor_id == 0x04cc) {
1821 switch(product_id) {
1823 Info("Sotec Afina Eye USB webcam detected.\n");
1824 name = "Sotec Afina Eye";
1832 else if (vendor_id == 0x06be) {
1833 switch(product_id) {
1835 /* This is essentially the same cam as the Sotec Afina Eye */
1836 Info("AME Co. Afina Eye USB webcam detected.\n");
1837 name = "AME Co. Afina Eye";
1846 else if (vendor_id == 0x0d81) {
1847 switch(product_id) {
1849 Info("Visionite VCS-UC300 USB webcam detected.\n");
1850 name = "Visionite VCS-UC300";
1851 type_id = 740; /* CCD sensor */
1854 Info("Visionite VCS-UM100 USB webcam detected.\n");
1855 name = "Visionite VCS-UM100";
1856 type_id = 730; /* CMOS sensor */
1864 return -ENODEV; /* Not any of the know types; but the list keeps growing. */
1866 memset(serial_number, 0, 30);
1867 usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
1868 Trace(TRACE_PROBE, "Device serial number is %s\n", serial_number);
1870 if (udev->descriptor.bNumConfigurations > 1)
1871 Info("Warning: more than 1 configuration available.\n");
1873 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1874 pdev = kmalloc(sizeof(struct pwc_device), GFP_KERNEL);
1876 Err("Oops, could not allocate memory for pwc_device.\n");
1879 memset(pdev, 0, sizeof(struct pwc_device));
1880 pdev->type = type_id;
1881 pdev->vsize = default_size;
1882 pdev->vframes = default_fps;
1883 strcpy(pdev->serial, serial_number);
1884 pdev->features = features;
1885 if (vendor_id == 0x046D && product_id == 0x08B5)
1887 /* Logitech QuickCam Orbit
1888 The ranges have been determined experimentally; they may differ from cam to cam.
1889 Also, the exact ranges left-right and up-down are different for my cam
1891 pdev->angle_range.pan_min = -7000;
1892 pdev->angle_range.pan_max = 7000;
1893 pdev->angle_range.tilt_min = -3000;
1894 pdev->angle_range.tilt_max = 2500;
1897 init_MUTEX(&pdev->modlock);
1898 spin_lock_init(&pdev->ptrlock);
1901 init_waitqueue_head(&pdev->frameq);
1902 pdev->vcompression = pwc_preferred_compression;
1904 /* Allocate video_device structure */
1905 pdev->vdev = video_device_alloc();
1906 if (pdev->vdev == 0)
1908 Err("Err, cannot allocate video_device struture. Failing probe.");
1912 memcpy(pdev->vdev, &pwc_template, sizeof(pwc_template));
1913 strcpy(pdev->vdev->name, name);
1914 pdev->vdev->owner = THIS_MODULE;
1915 video_set_drvdata(pdev->vdev, pdev);
1917 pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1918 Trace(TRACE_PROBE, "Release: %04x\n", pdev->release);
1920 /* Now search device_hint[] table for a match, so we can hint a node number. */
1921 for (hint = 0; hint < MAX_DEV_HINTS; hint++) {
1922 if (((device_hint[hint].type == -1) || (device_hint[hint].type == pdev->type)) &&
1923 (device_hint[hint].pdev == NULL)) {
1924 /* so far, so good... try serial number */
1925 if ((device_hint[hint].serial_number[0] == '*') || !strcmp(device_hint[hint].serial_number, serial_number)) {
1927 video_nr = device_hint[hint].device_node;
1928 Trace(TRACE_PROBE, "Found hint, will try to register as /dev/video%d\n", video_nr);
1934 pdev->vdev->release = video_device_release;
1935 i = video_register_device(pdev->vdev, VFL_TYPE_GRABBER, video_nr);
1937 Err("Failed to register as video device (%d).\n", i);
1938 video_device_release(pdev->vdev); /* Drip... drip... drip... */
1939 kfree(pdev); /* Oops, no memory leaks please */
1943 Info("Registered as /dev/video%d.\n", pdev->vdev->minor & 0x3F);
1947 if (hint < MAX_DEV_HINTS)
1948 device_hint[hint].pdev = pdev;
1950 Trace(TRACE_PROBE, "probe() function returning struct at 0x%p.\n", pdev);
1951 usb_set_intfdata (intf, pdev);
1955 /* The user janked out the cable... */
1956 static void usb_pwc_disconnect(struct usb_interface *intf)
1958 struct pwc_device *pdev;
1962 pdev = usb_get_intfdata (intf);
1963 usb_set_intfdata (intf, NULL);
1965 Err("pwc_disconnect() Called without private pointer.\n");
1966 goto disconnect_out;
1968 if (pdev->udev == NULL) {
1969 Err("pwc_disconnect() already called for %p\n", pdev);
1970 goto disconnect_out;
1972 if (pdev->udev != interface_to_usbdev(intf)) {
1973 Err("pwc_disconnect() Woops: pointer mismatch udev/pdev.\n");
1974 goto disconnect_out;
1977 if (pdev->magic != PWC_MAGIC) {
1978 Err("pwc_disconnect() Magic number failed. Consult your scrolls and try again.\n");
1979 goto disconnect_out;
1983 /* We got unplugged; this is signalled by an EPIPE error code */
1985 Info("Disconnected while webcam is in use!\n");
1986 pdev->error_status = EPIPE;
1989 /* Alert waiting processes */
1990 wake_up_interruptible(&pdev->frameq);
1991 /* Wait until device is closed */
1994 /* Device is now closed, so we can safely unregister it */
1995 Trace(TRACE_PROBE, "Unregistering video device in disconnect().\n");
1996 video_unregister_device(pdev->vdev);
1998 /* Free memory (don't set pdev to 0 just yet) */
2002 /* search device_hint[] table if we occupy a slot, by any chance */
2003 for (hint = 0; hint < MAX_DEV_HINTS; hint++)
2004 if (device_hint[hint].pdev == pdev)
2005 device_hint[hint].pdev = NULL;
2011 /* *grunt* We have to do atoi ourselves :-( */
2012 static int pwc_atoi(const char *s)
2017 while (*s != '\0' && *s >= '0' && *s <= '9') {
2018 k = 10 * k + (*s - '0');
2026 * Initialization code & module stuff
2029 static char size[10];
2031 static int fbufs = 0;
2032 static int mbufs = 0;
2033 static int trace = -1;
2034 static int compression = -1;
2035 static int leds[2] = { -1, -1 };
2036 static char *dev_hint[MAX_DEV_HINTS] = { };
2038 module_param_string(size, size, sizeof(size), 0);
2039 MODULE_PARM_DESC(size, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
2040 module_param(fps, int, 0000);
2041 MODULE_PARM_DESC(fps, "Initial frames per second. Varies with model, useful range 5-30");
2042 module_param(fbufs, int, 0000);
2043 MODULE_PARM_DESC(fbufs, "Number of internal frame buffers to reserve");
2044 module_param(mbufs, int, 0000);
2045 MODULE_PARM_DESC(mbufs, "Number of external (mmap()ed) image buffers");
2046 module_param(trace, int, 0000);
2047 MODULE_PARM_DESC(trace, "For debugging purposes");
2048 module_param(power_save, bool, 0000);
2049 MODULE_PARM_DESC(power_save, "Turn power save feature in camera on or off");
2050 module_param(compression, int, 0000);
2051 MODULE_PARM_DESC(compression, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
2052 module_param_array(leds, int, NULL, 0000);
2053 MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
2054 module_param_array(dev_hint, charp, NULL, 0000);
2055 MODULE_PARM_DESC(dev_hint, "Device node hints");
2057 MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
2058 MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
2059 MODULE_LICENSE("GPL");
2061 static int __init usb_pwc_init(void)
2064 char *sizenames[PSZ_MAX] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };
2066 Info("Philips webcam module version " PWC_VERSION " loaded.\n");
2067 Info("Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.\n");
2068 Info("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
2069 Info("the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
2072 if (fps < 4 || fps > 30) {
2073 Err("Framerate out of bounds (4-30).\n");
2077 Info("Default framerate set to %d.\n", default_fps);
2081 /* string; try matching with array */
2082 for (sz = 0; sz < PSZ_MAX; sz++) {
2083 if (!strcmp(sizenames[sz], size)) { /* Found! */
2088 if (sz == PSZ_MAX) {
2089 Err("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
2092 Info("Default image size set to %s [%dx%d].\n", sizenames[default_size], pwc_image_sizes[default_size].x, pwc_image_sizes[default_size].y);
2095 if (mbufs < 1 || mbufs > MAX_IMAGES) {
2096 Err("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES);
2099 default_mbufs = mbufs;
2100 Info("Number of image buffers set to %d.\n", default_mbufs);
2103 if (fbufs < 2 || fbufs > MAX_FRAMES) {
2104 Err("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES);
2107 default_fbufs = fbufs;
2108 Info("Number of frame buffers set to %d.\n", default_fbufs);
2111 Info("Trace options: 0x%04x\n", trace);
2114 if (compression >= 0) {
2115 if (compression > 3) {
2116 Err("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
2119 pwc_preferred_compression = compression;
2120 Info("Preferred compression set to %d.\n", pwc_preferred_compression);
2123 Info("Enabling power save on open/close.\n");
2129 /* Big device node whoopla. Basically, it allows you to assign a
2130 device node (/dev/videoX) to a camera, based on its type
2131 & serial number. The format is [type[.serialnumber]:]node.
2133 Any camera that isn't matched by these rules gets the next
2134 available free device node.
2136 for (i = 0; i < MAX_DEV_HINTS; i++) {
2137 char *s, *colon, *dot;
2139 /* This loop also initializes the array */
2140 device_hint[i].pdev = NULL;
2142 if (s != NULL && *s != '\0') {
2143 device_hint[i].type = -1; /* wildcard */
2144 strcpy(device_hint[i].serial_number, "*");
2146 /* parse string: chop at ':' & '/' */
2148 while (*colon != '\0' && *colon != ':')
2150 while (*dot != '\0' && *dot != '.')
2152 /* Few sanity checks */
2153 if (*dot != '\0' && dot > colon) {
2154 Err("Malformed camera hint: the colon must be after the dot.\n");
2158 if (*colon == '\0') {
2161 Err("Malformed camera hint: no colon + device node given.\n");
2165 /* No type or serial number specified, just a number. */
2166 device_hint[i].device_node = pwc_atoi(s);
2170 /* There's a colon, so we have at least a type and a device node */
2171 device_hint[i].type = pwc_atoi(s);
2172 device_hint[i].device_node = pwc_atoi(colon + 1);
2174 /* There's a serial number as well */
2179 while (*dot != ':' && k < 29) {
2180 device_hint[i].serial_number[k++] = *dot;
2183 device_hint[i].serial_number[k] = '\0';
2187 Debug("device_hint[%d]:\n", i);
2188 Debug(" type : %d\n", device_hint[i].type);
2189 Debug(" serial# : %s\n", device_hint[i].serial_number);
2190 Debug(" node : %d\n", device_hint[i].device_node);
2194 device_hint[i].type = 0; /* not filled */
2195 } /* ..for MAX_DEV_HINTS */
2197 Trace(TRACE_PROBE, "Registering driver at address 0x%p.\n", &pwc_driver);
2198 return usb_register(&pwc_driver);
2201 static void __exit usb_pwc_exit(void)
2203 Trace(TRACE_MODULE, "Deregistering driver.\n");
2204 usb_deregister(&pwc_driver);
2205 Info("Philips webcam module removed.\n");
2208 module_init(usb_pwc_init);
2209 module_exit(usb_pwc_exit);