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>
68 #include "pwc-ioctl.h"
69 #include "pwc-kiara.h"
70 #include "pwc-timon.h"
71 #include "pwc-uncompress.h"
73 /* Function prototypes and driver templates */
75 /* hotplug device table support */
76 static struct usb_device_id pwc_device_table [] = {
77 { USB_DEVICE(0x0471, 0x0302) }, /* Philips models */
78 { USB_DEVICE(0x0471, 0x0303) },
79 { USB_DEVICE(0x0471, 0x0304) },
80 { USB_DEVICE(0x0471, 0x0307) },
81 { USB_DEVICE(0x0471, 0x0308) },
82 { USB_DEVICE(0x0471, 0x030C) },
83 { USB_DEVICE(0x0471, 0x0310) },
84 { USB_DEVICE(0x0471, 0x0311) },
85 { USB_DEVICE(0x0471, 0x0312) },
86 { USB_DEVICE(0x0471, 0x0313) }, /* the 'new' 720K */
87 { USB_DEVICE(0x069A, 0x0001) }, /* Askey */
88 { USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam Pro 3000 */
89 { USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
90 { USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam Pro 4000 */
91 { USB_DEVICE(0x046D, 0x08B3) }, /* Logitech QuickCam Zoom (old model) */
92 { USB_DEVICE(0x046D, 0x08B4) }, /* Logitech QuickCam Zoom (new model) */
93 { USB_DEVICE(0x046D, 0x08B5) }, /* Logitech QuickCam Orbit/Sphere */
94 { USB_DEVICE(0x046D, 0x08B6) }, /* Logitech (reserved) */
95 { USB_DEVICE(0x046D, 0x08B7) }, /* Logitech (reserved) */
96 { USB_DEVICE(0x046D, 0x08B8) }, /* Logitech (reserved) */
97 { USB_DEVICE(0x055D, 0x9000) }, /* Samsung */
98 { USB_DEVICE(0x055D, 0x9001) },
99 { USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
100 { USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
101 { USB_DEVICE(0x04CC, 0x8116) }, /* Afina Eye */
102 { USB_DEVICE(0x06BE, 0x8116) }, /* new Afina Eye */
103 { USB_DEVICE(0x0d81, 0x1910) }, /* Visionite */
104 { USB_DEVICE(0x0d81, 0x1900) },
107 MODULE_DEVICE_TABLE(usb, pwc_device_table);
109 static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id);
110 static void usb_pwc_disconnect(struct usb_interface *intf);
112 static struct usb_driver pwc_driver = {
113 .owner = THIS_MODULE,
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,
159 static struct video_device pwc_template = {
160 .owner = THIS_MODULE,
161 .name = "Philips Webcam", /* Filled in later */
162 .type = VID_TYPE_CAPTURE,
163 .hardware = VID_HARDWARE_PWC,
164 .release = video_device_release,
169 /***************************************************************************/
171 /* Okay, this is some magic that I worked out and the reasoning behind it...
173 The biggest problem with any USB device is of course: "what to do
174 when the user unplugs the device while it is in use by an application?"
175 We have several options:
176 1) Curse them with the 7 plagues when they do (requires divine intervention)
177 2) Tell them not to (won't work: they'll do it anyway)
178 3) Oops the kernel (this will have a negative effect on a user's uptime)
179 4) Do something sensible.
181 Of course, we go for option 4.
183 It happens that this device will be linked to two times, once from
184 usb_device and once from the video_device in their respective 'private'
185 pointers. This is done when the device is probed() and all initialization
186 succeeded. The pwc_device struct links back to both structures.
188 When a device is unplugged while in use it will be removed from the
189 list of known USB devices; I also de-register it as a V4L device, but
190 unfortunately I can't free the memory since the struct is still in use
191 by the file descriptor. This free-ing is then deferend until the first
192 opportunity. Crude, but it works.
194 A small 'advantage' is that if a user unplugs the cam and plugs it back
195 in, it should get assigned the same video device minor, but unfortunately
196 it's non-trivial to re-link the cam back to the video device... (that
197 would surely be magic! :))
200 /***************************************************************************/
201 /* Private functions */
203 /* Here we want the physical address of the memory.
204 * This is used when initializing the contents of the area.
206 static inline unsigned long kvirt_to_pa(unsigned long adr)
208 unsigned long kva, ret;
210 kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
211 kva |= adr & (PAGE_SIZE-1); /* restore the offset */
216 static void * rvmalloc(unsigned long size)
221 size=PAGE_ALIGN(size);
222 mem=vmalloc_32(size);
225 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
226 adr=(unsigned long) mem;
229 SetPageReserved(vmalloc_to_page((void *)adr));
237 static void rvfree(void * mem, unsigned long size)
243 adr=(unsigned long) mem;
244 while ((long) size > 0)
246 ClearPageReserved(vmalloc_to_page((void *)adr));
257 static int pwc_allocate_buffers(struct pwc_device *pdev)
262 Trace(TRACE_MEMORY, ">> pwc_allocate_buffers(pdev = 0x%p)\n", pdev);
268 if (pdev->magic != PWC_MAGIC) {
269 Err("allocate_buffers(): magic failed.\n");
273 /* Allocate Isochronous pipe buffers */
274 for (i = 0; i < MAX_ISO_BUFS; i++) {
275 if (pdev->sbuf[i].data == NULL) {
276 kbuf = kmalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
278 Err("Failed to allocate iso buffer %d.\n", i);
281 Trace(TRACE_MEMORY, "Allocated iso buffer at %p.\n", kbuf);
282 pdev->sbuf[i].data = kbuf;
283 memset(kbuf, 0, ISO_BUFFER_SIZE);
287 /* Allocate frame buffer structure */
288 if (pdev->fbuf == NULL) {
289 kbuf = kmalloc(default_fbufs * sizeof(struct pwc_frame_buf), GFP_KERNEL);
291 Err("Failed to allocate frame buffer structure.\n");
294 Trace(TRACE_MEMORY, "Allocated frame buffer structure at %p.\n", kbuf);
296 memset(kbuf, 0, default_fbufs * sizeof(struct pwc_frame_buf));
298 /* create frame buffers, and make circular ring */
299 for (i = 0; i < default_fbufs; i++) {
300 if (pdev->fbuf[i].data == NULL) {
301 kbuf = vmalloc(PWC_FRAME_SIZE); /* need vmalloc since frame buffer > 128K */
303 Err("Failed to allocate frame buffer %d.\n", i);
306 Trace(TRACE_MEMORY, "Allocated frame buffer %d at %p.\n", i, kbuf);
307 pdev->fbuf[i].data = kbuf;
308 memset(kbuf, 128, PWC_FRAME_SIZE);
312 /* Allocate decompressor table space */
324 Trace(TRACE_MEMORY,"private_data(%zu)\n",sizeof(struct pwc_dec23_private));
325 kbuf = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL); /* Timon & Kiara */
330 kbuf = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL);
336 Err("Failed to allocate decompress table.\n");
339 pdev->decompress_data = kbuf;
341 /* Allocate image buffer; double buffer for mmap() */
342 kbuf = rvmalloc(default_mbufs * pdev->len_per_image);
344 Err("Failed to allocate image buffer(s). needed (%d)\n",default_mbufs * pdev->len_per_image);
347 Trace(TRACE_MEMORY, "Allocated image buffer at %p.\n", kbuf);
348 pdev->image_data = kbuf;
349 for (i = 0; i < default_mbufs; i++)
350 pdev->image_ptr[i] = kbuf + i * pdev->len_per_image;
351 for (; i < MAX_IMAGES; i++)
352 pdev->image_ptr[i] = NULL;
356 Trace(TRACE_MEMORY, "<< pwc_allocate_buffers()\n");
360 static void pwc_free_buffers(struct pwc_device *pdev)
364 Trace(TRACE_MEMORY, "Entering free_buffers(%p).\n", pdev);
369 if (pdev->magic != PWC_MAGIC) {
370 Err("free_buffers(): magic failed.\n");
375 /* Release Iso-pipe buffers */
376 for (i = 0; i < MAX_ISO_BUFS; i++)
377 if (pdev->sbuf[i].data != NULL) {
378 Trace(TRACE_MEMORY, "Freeing ISO buffer at %p.\n", pdev->sbuf[i].data);
379 kfree(pdev->sbuf[i].data);
380 pdev->sbuf[i].data = NULL;
383 /* The same for frame buffers */
384 if (pdev->fbuf != NULL) {
385 for (i = 0; i < default_fbufs; i++) {
386 if (pdev->fbuf[i].data != NULL) {
387 Trace(TRACE_MEMORY, "Freeing frame buffer %d at %p.\n", i, pdev->fbuf[i].data);
388 vfree(pdev->fbuf[i].data);
389 pdev->fbuf[i].data = NULL;
396 /* Intermediate decompression buffer & tables */
397 if (pdev->decompress_data != NULL) {
398 Trace(TRACE_MEMORY, "Freeing decompression buffer at %p.\n", pdev->decompress_data);
399 kfree(pdev->decompress_data);
400 pdev->decompress_data = NULL;
402 pdev->decompressor = NULL;
404 /* Release image buffers */
405 if (pdev->image_data != NULL) {
406 Trace(TRACE_MEMORY, "Freeing image buffer at %p.\n", pdev->image_data);
407 rvfree(pdev->image_data, default_mbufs * pdev->len_per_image);
409 pdev->image_data = NULL;
411 Trace(TRACE_MEMORY, "Leaving free_buffers().\n");
414 /* The frame & image buffer mess.
416 Yes, this is a mess. Well, it used to be simple, but alas... In this
417 module, 3 buffers schemes are used to get the data from the USB bus to
418 the user program. The first scheme involves the ISO buffers (called thus
419 since they transport ISO data from the USB controller), and not really
420 interesting. Suffices to say the data from this buffer is quickly
421 gathered in an interrupt handler (pwc_isoc_handler) and placed into the
424 The frame buffer is the second scheme, and is the central element here.
425 It collects the data from a single frame from the camera (hence, the
426 name). Frames are delimited by the USB camera with a short USB packet,
427 so that's easy to detect. The frame buffers form a list that is filled
428 by the camera+USB controller and drained by the user process through
429 either read() or mmap().
431 The image buffer is the third scheme, in which frames are decompressed
432 and converted into planar format. For mmap() there is more than
433 one image buffer available.
435 The frame buffers provide the image buffering. In case the user process
436 is a bit slow, this introduces lag and some undesired side-effects.
437 The problem arises when the frame buffer is full. I used to drop the last
438 frame, which makes the data in the queue stale very quickly. But dropping
439 the frame at the head of the queue proved to be a litte bit more difficult.
440 I tried a circular linked scheme, but this introduced more problems than
443 Because filling and draining are completely asynchronous processes, this
444 requires some fiddling with pointers and mutexes.
446 Eventually, I came up with a system with 2 lists: an 'empty' frame list
447 and a 'full' frame list:
448 * Initially, all frame buffers but one are on the 'empty' list; the one
449 remaining buffer is our initial fill frame.
450 * If a frame is needed for filling, we try to take it from the 'empty'
451 list, unless that list is empty, in which case we take the buffer at
452 the head of the 'full' list.
453 * When our fill buffer has been filled, it is appended to the 'full'
455 * If a frame is needed by read() or mmap(), it is taken from the head of
456 the 'full' list, handled, and then appended to the 'empty' list. If no
457 buffer is present on the 'full' list, we wait.
458 The advantage is that the buffer that is currently being decompressed/
459 converted, is on neither list, and thus not in our way (any other scheme
460 I tried had the problem of old data lingering in the queue).
462 Whatever strategy you choose, it always remains a tradeoff: with more
463 frame buffers the chances of a missed frame are reduced. On the other
464 hand, on slower machines it introduces lag because the queue will
469 \brief Find next frame buffer to fill. Take from empty or full list, whichever comes first.
471 static inline int pwc_next_fill_frame(struct pwc_device *pdev)
477 spin_lock_irqsave(&pdev->ptrlock, flags);
478 if (pdev->fill_frame != NULL) {
479 /* append to 'full' list */
480 if (pdev->full_frames == NULL) {
481 pdev->full_frames = pdev->fill_frame;
482 pdev->full_frames_tail = pdev->full_frames;
485 pdev->full_frames_tail->next = pdev->fill_frame;
486 pdev->full_frames_tail = pdev->fill_frame;
489 if (pdev->empty_frames != NULL) {
490 /* We have empty frames available. That's easy */
491 pdev->fill_frame = pdev->empty_frames;
492 pdev->empty_frames = pdev->empty_frames->next;
495 /* Hmm. Take it from the full list */
498 if (pdev->full_frames == NULL) {
499 Err("Neither empty or full frames available!\n");
500 spin_unlock_irqrestore(&pdev->ptrlock, flags);
504 pdev->fill_frame = pdev->full_frames;
505 pdev->full_frames = pdev->full_frames->next;
508 pdev->fill_frame->next = NULL;
510 Trace(TRACE_SEQUENCE, "Assigning sequence number %d.\n", pdev->sequence);
511 pdev->fill_frame->sequence = pdev->sequence++;
513 spin_unlock_irqrestore(&pdev->ptrlock, flags);
519 \brief Reset all buffers, pointers and lists, except for the image_used[] buffer.
521 If the image_used[] buffer is cleared too, mmap()/VIDIOCSYNC will run into trouble.
523 static void pwc_reset_buffers(struct pwc_device *pdev)
528 spin_lock_irqsave(&pdev->ptrlock, flags);
529 pdev->full_frames = NULL;
530 pdev->full_frames_tail = NULL;
531 for (i = 0; i < default_fbufs; i++) {
532 pdev->fbuf[i].filled = 0;
534 pdev->fbuf[i].next = &pdev->fbuf[i - 1];
536 pdev->fbuf->next = NULL;
538 pdev->empty_frames = &pdev->fbuf[default_fbufs - 1];
539 pdev->empty_frames_tail = pdev->fbuf;
540 pdev->read_frame = NULL;
541 pdev->fill_frame = pdev->empty_frames;
542 pdev->empty_frames = pdev->empty_frames->next;
544 pdev->image_read_pos = 0;
545 pdev->fill_image = 0;
546 spin_unlock_irqrestore(&pdev->ptrlock, flags);
551 \brief Do all the handling for getting one frame: get pointer, decompress, advance pointers.
553 static int pwc_handle_frame(struct pwc_device *pdev)
558 spin_lock_irqsave(&pdev->ptrlock, flags);
559 /* First grab our read_frame; this is removed from all lists, so
560 we can release the lock after this without problems */
561 if (pdev->read_frame != NULL) {
562 /* This can't theoretically happen */
563 Err("Huh? Read frame still in use?\n");
566 if (pdev->full_frames == NULL) {
567 Err("Woops. No frames ready.\n");
570 pdev->read_frame = pdev->full_frames;
571 pdev->full_frames = pdev->full_frames->next;
572 pdev->read_frame->next = NULL;
575 if (pdev->read_frame != NULL) {
577 Trace(TRACE_SEQUENCE, "Decompressing frame %d\n", pdev->read_frame->sequence);
579 /* Decompression is a lenghty process, so it's outside of the lock.
580 This gives the isoc_handler the opportunity to fill more frames
583 spin_unlock_irqrestore(&pdev->ptrlock, flags);
584 ret = pwc_decompress(pdev);
585 spin_lock_irqsave(&pdev->ptrlock, flags);
587 /* We're done with read_buffer, tack it to the end of the empty buffer list */
588 if (pdev->empty_frames == NULL) {
589 pdev->empty_frames = pdev->read_frame;
590 pdev->empty_frames_tail = pdev->empty_frames;
593 pdev->empty_frames_tail->next = pdev->read_frame;
594 pdev->empty_frames_tail = pdev->read_frame;
596 pdev->read_frame = NULL;
599 spin_unlock_irqrestore(&pdev->ptrlock, flags);
604 \brief Advance pointers of image buffer (after each user request)
606 static inline void pwc_next_image(struct pwc_device *pdev)
608 pdev->image_used[pdev->fill_image] = 0;
609 pdev->fill_image = (pdev->fill_image + 1) % default_mbufs;
613 /* This gets called for the Isochronous pipe (video). This is done in
614 * interrupt time, so it has to be fast, not crash, and not stall. Neat.
616 static void pwc_isoc_handler(struct urb *urb, struct pt_regs *regs)
618 struct pwc_device *pdev;
621 struct pwc_frame_buf *fbuf;
622 unsigned char *fillptr = NULL, *iso_buf = NULL;
625 pdev = (struct pwc_device *)urb->context;
627 Err("isoc_handler() called with NULL device?!\n");
631 if (pdev->magic != PWC_MAGIC) {
632 Err("isoc_handler() called with bad magic!\n");
636 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
637 Trace(TRACE_OPEN, "pwc_isoc_handler(): URB (%p) unlinked %ssynchronuously.\n", urb, urb->status == -ENOENT ? "" : "a");
640 if (urb->status != -EINPROGRESS && urb->status != 0) {
644 switch(urb->status) {
645 case -ENOSR: errmsg = "Buffer error (overrun)"; break;
646 case -EPIPE: errmsg = "Stalled (device not responding)"; break;
647 case -EOVERFLOW: errmsg = "Babble (bad cable?)"; break;
648 case -EPROTO: errmsg = "Bit-stuff error (bad cable?)"; break;
649 case -EILSEQ: errmsg = "CRC/Timeout (could be anything)"; break;
650 case -ETIMEDOUT: errmsg = "NAK (device does not respond)"; break;
652 Trace(TRACE_FLOW, "pwc_isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
653 /* Give up after a number of contiguous errors on the USB bus.
654 Appearantly something is wrong so we simulate an unplug event.
656 if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
658 Info("Too many ISOC errors, bailing out.\n");
659 pdev->error_status = EIO;
661 wake_up_interruptible(&pdev->frameq);
663 goto handler_end; // ugly, but practical
666 fbuf = pdev->fill_frame;
668 Err("pwc_isoc_handler without valid fill frame.\n");
673 fillptr = fbuf->data + fbuf->filled;
676 /* Reset ISOC error counter. We did get here, after all. */
677 pdev->visoc_errors = 0;
679 /* vsync: 0 = don't copy data
684 for (i = 0; i < urb->number_of_packets; i++) {
685 fst = urb->iso_frame_desc[i].status;
686 flen = urb->iso_frame_desc[i].actual_length;
687 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
689 if (flen > 0) { /* if valid data... */
690 if (pdev->vsync > 0) { /* ...and we are not sync-hunting... */
693 /* ...copy data to frame buffer, if possible */
694 if (flen + fbuf->filled > pdev->frame_total_size) {
695 Trace(TRACE_FLOW, "Frame buffer overflow (flen = %d, frame_total_size = %d).\n", flen, pdev->frame_total_size);
696 pdev->vsync = 0; /* Hmm, let's wait for an EOF (end-of-frame) */
697 pdev->vframes_error++;
700 memmove(fillptr, iso_buf, flen);
704 fbuf->filled += flen;
707 if (flen < pdev->vlast_packet_size) {
708 /* Shorter packet... We probably have the end of an image-frame;
709 wake up read() process and let select()/poll() do something.
710 Decompression is done in user time over there.
712 if (pdev->vsync == 2) {
713 /* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
714 frames on the USB wire after an exposure change. This conditition is
715 however detected in the cam and a bit is set in the header.
717 if (pdev->type == 730) {
718 unsigned char *ptr = (unsigned char *)fbuf->data;
720 if (ptr[1] == 1 && ptr[0] & 0x10) {
722 Debug("Hyundai CMOS sensor bug. Dropping frame %d.\n", fbuf->sequence);
724 pdev->drop_frames += 2;
725 pdev->vframes_error++;
727 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
729 Info("Snapshot button pressed.\n");
731 Info("Snapshot button released.\n");
733 if ((ptr[0] ^ pdev->vmirror) & 0x02) {
735 Info("Image is mirrored.\n");
737 Info("Image is normal.\n");
739 pdev->vmirror = ptr[0] & 0x03;
740 /* Sometimes the trailer of the 730 is still sent as a 4 byte packet
741 after a short frame; this condition is filtered out specifically. A 4 byte
742 frame doesn't make sense anyway.
743 So we get either this sequence:
744 drop_bit set -> 4 byte frame -> short frame -> good frame
746 drop_bit set -> short frame -> good frame
747 So we drop either 3 or 2 frames in all!
749 if (fbuf->filled == 4)
753 /* In case we were instructed to drop the frame, do so silently.
754 The buffer pointers are not updated either (but the counters are reset below).
756 if (pdev->drop_frames > 0)
759 /* Check for underflow first */
760 if (fbuf->filled < pdev->frame_total_size) {
761 Trace(TRACE_FLOW, "Frame buffer underflow (%d bytes); discarded.\n", fbuf->filled);
762 pdev->vframes_error++;
765 /* Send only once per EOF */
766 awake = 1; /* delay wake_ups */
768 /* Find our next frame to fill. This will always succeed, since we
769 * nick a frame from either empty or full list, but if we had to
770 * take it from the full list, it means a frame got dropped.
772 if (pwc_next_fill_frame(pdev)) {
773 pdev->vframes_dumped++;
774 if ((pdev->vframe_count > FRAME_LOWMARK) && (pwc_trace & TRACE_FLOW)) {
775 if (pdev->vframes_dumped < 20)
776 Trace(TRACE_FLOW, "Dumping frame %d.\n", pdev->vframe_count);
777 if (pdev->vframes_dumped == 20)
778 Trace(TRACE_FLOW, "Dumping frame %d (last message).\n", pdev->vframe_count);
781 fbuf = pdev->fill_frame;
784 pdev->vframe_count++;
787 fillptr = fbuf->data;
789 } /* .. flen < last_packet_size */
790 pdev->vlast_packet_size = flen;
791 } /* ..status == 0 */
793 /* This is normally not interesting to the user, unless you are really debugging something */
795 static int iso_error = 0;
798 Trace(TRACE_FLOW, "Iso frame %d of USB has error %d\n", i, fst);
805 wake_up_interruptible(&pdev->frameq);
807 urb->dev = pdev->udev;
808 i = usb_submit_urb(urb, GFP_ATOMIC);
810 Err("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
814 static int pwc_isoc_init(struct pwc_device *pdev)
816 struct usb_device *udev;
820 struct usb_interface *intf;
821 struct usb_host_interface *idesc = NULL;
830 /* Get the current alternate interface, adjust packet size */
831 if (!udev->actconfig)
833 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5)
834 idesc = &udev->actconfig->interface[0]->altsetting[pdev->valternate];
836 intf = usb_ifnum_to_if(udev, 0);
838 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
844 /* Search video endpoint */
845 pdev->vmax_packet_size = -1;
846 for (i = 0; i < idesc->desc.bNumEndpoints; i++)
847 if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
848 pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
852 if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
853 Err("Failed to find packet size for video endpoint in current alternate setting.\n");
854 return -ENFILE; /* Odd error, that should be noticeable */
857 /* Set alternate interface */
859 Trace(TRACE_OPEN, "Setting alternate interface %d\n", pdev->valternate);
860 ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
864 for (i = 0; i < MAX_ISO_BUFS; i++) {
865 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
867 Err("Failed to allocate urb %d\n", i);
871 pdev->sbuf[i].urb = urb;
872 Trace(TRACE_MEMORY, "Allocated URB at 0x%p\n", urb);
875 /* De-allocate in reverse order */
877 if (pdev->sbuf[i].urb != NULL)
878 usb_free_urb(pdev->sbuf[i].urb);
879 pdev->sbuf[i].urb = NULL;
885 /* init URB structure */
886 for (i = 0; i < MAX_ISO_BUFS; i++) {
887 urb = pdev->sbuf[i].urb;
889 urb->interval = 1; // devik
891 urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
892 urb->transfer_flags = URB_ISO_ASAP;
893 urb->transfer_buffer = pdev->sbuf[i].data;
894 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
895 urb->complete = pwc_isoc_handler;
897 urb->start_frame = 0;
898 urb->number_of_packets = ISO_FRAMES_PER_DESC;
899 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
900 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
901 urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
906 for (i = 0; i < MAX_ISO_BUFS; i++) {
907 ret = usb_submit_urb(pdev->sbuf[i].urb, GFP_KERNEL);
909 Err("isoc_init() submit_urb %d failed with error %d\n", i, ret);
911 Trace(TRACE_MEMORY, "URB 0x%p submitted.\n", pdev->sbuf[i].urb);
916 Trace(TRACE_OPEN, "<< pwc_isoc_init()\n");
920 static void pwc_isoc_cleanup(struct pwc_device *pdev)
924 Trace(TRACE_OPEN, ">> pwc_isoc_cleanup()\n");
928 /* Unlinking ISOC buffers one by one */
929 for (i = 0; i < MAX_ISO_BUFS; i++) {
932 urb = pdev->sbuf[i].urb;
934 if (pdev->iso_init) {
935 Trace(TRACE_MEMORY, "Unlinking URB %p\n", urb);
938 Trace(TRACE_MEMORY, "Freeing URB\n");
940 pdev->sbuf[i].urb = NULL;
944 /* Stop camera, but only if we are sure the camera is still there (unplug
945 is signalled by EPIPE)
947 if (pdev->error_status && pdev->error_status != EPIPE) {
948 Trace(TRACE_OPEN, "Setting alternate interface 0.\n");
949 usb_set_interface(pdev->udev, 0, 0);
953 Trace(TRACE_OPEN, "<< pwc_isoc_cleanup()\n");
956 int pwc_try_video_mode(struct pwc_device *pdev, int width, int height, int new_fps, int new_compression, int new_snapshot)
960 /* Stop isoc stuff */
961 pwc_isoc_cleanup(pdev);
962 /* Reset parameters */
963 pwc_reset_buffers(pdev);
964 /* Try to set video mode... */
965 start = ret = pwc_set_video_mode(pdev, width, height, new_fps, new_compression, new_snapshot);
967 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 1 failed.\n");
968 /* That failed... restore old mode (we know that worked) */
969 start = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
971 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 2 failed.\n");
976 if (pwc_isoc_init(pdev) < 0)
978 Info("Failed to restart ISOC transfers in pwc_try_video_mode.\n");
979 ret = -EAGAIN; /* let's try again, who knows if it works a second time */
982 pdev->drop_frames++; /* try to avoid garbage during switch */
983 return ret; /* Return original error code */
987 /***************************************************************************/
988 /* Video4Linux functions */
990 static int pwc_video_open(struct inode *inode, struct file *file)
993 struct video_device *vdev = video_devdata(file);
994 struct pwc_device *pdev;
996 Trace(TRACE_OPEN, ">> video_open called(vdev = 0x%p).\n", vdev);
998 pdev = (struct pwc_device *)vdev->priv;
1004 down(&pdev->modlock);
1005 if (!pdev->usb_init) {
1006 Trace(TRACE_OPEN, "Doing first time initialization.\n");
1009 if (pwc_trace & TRACE_OPEN)
1011 /* Query sensor type */
1012 const char *sensor_type = NULL;
1015 ret = pwc_get_cmos_sensor(pdev, &i);
1019 case 0x00: sensor_type = "Hyundai CMOS sensor"; break;
1020 case 0x20: sensor_type = "Sony CCD sensor + TDA8787"; break;
1021 case 0x2E: sensor_type = "Sony CCD sensor + Exas 98L59"; break;
1022 case 0x2F: sensor_type = "Sony CCD sensor + ADI 9804"; break;
1023 case 0x30: sensor_type = "Sharp CCD sensor + TDA8787"; break;
1024 case 0x3E: sensor_type = "Sharp CCD sensor + Exas 98L59"; break;
1025 case 0x3F: sensor_type = "Sharp CCD sensor + ADI 9804"; break;
1026 case 0x40: sensor_type = "UPA 1021 sensor"; break;
1027 case 0x100: sensor_type = "VGA sensor"; break;
1028 case 0x101: sensor_type = "PAL MR sensor"; break;
1029 default: sensor_type = "unknown type of sensor"; break;
1032 if (sensor_type != NULL)
1033 Info("This %s camera is equipped with a %s (%d).\n", pdev->vdev->name, sensor_type, i);
1037 /* Turn on camera */
1039 i = pwc_camera_power(pdev, 1);
1041 Info("Failed to restore power to the camera! (%d)\n", i);
1043 /* Set LED on/off time */
1044 if (pwc_set_leds(pdev, led_on, led_off) < 0)
1045 Info("Failed to set LED on/off time.\n");
1047 pwc_construct(pdev); /* set min/max sizes correct */
1049 /* So far, so good. Allocate memory. */
1050 i = pwc_allocate_buffers(pdev);
1052 Trace(TRACE_OPEN, "Failed to allocate buffer memory.\n");
1057 /* Reset buffers & parameters */
1058 pwc_reset_buffers(pdev);
1059 for (i = 0; i < default_mbufs; i++)
1060 pdev->image_used[i] = 0;
1061 pdev->vframe_count = 0;
1062 pdev->vframes_dumped = 0;
1063 pdev->vframes_error = 0;
1064 pdev->visoc_errors = 0;
1065 pdev->error_status = 0;
1069 pwc_construct(pdev); /* set min/max sizes correct */
1071 /* Set some defaults */
1072 pdev->vsnapshot = 0;
1074 /* Start iso pipe for video; first try the last used video size
1075 (or the default one); if that fails try QCIF/10 or QSIF/10;
1076 it that fails too, give up.
1078 i = pwc_set_video_mode(pdev, pwc_image_sizes[pdev->vsize].x, pwc_image_sizes[pdev->vsize].y, pdev->vframes, pdev->vcompression, 0);
1080 Trace(TRACE_OPEN, "First attempt at set_video_mode failed.\n");
1081 if (pdev->type == 730 || pdev->type == 740 || pdev->type == 750)
1082 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QSIF].x, pwc_image_sizes[PSZ_QSIF].y, 10, pdev->vcompression, 0);
1084 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QCIF].x, pwc_image_sizes[PSZ_QCIF].y, 10, pdev->vcompression, 0);
1087 Trace(TRACE_OPEN, "Second attempt at set_video_mode failed.\n");
1092 i = pwc_isoc_init(pdev);
1094 Trace(TRACE_OPEN, "Failed to init ISOC stuff = %d.\n", i);
1100 file->private_data = vdev;
1102 Trace(TRACE_OPEN, "<< video_open() returns 0.\n");
1106 /* Note that all cleanup is done in the reverse order as in _open */
1107 static int pwc_video_close(struct inode *inode, struct file *file)
1109 struct video_device *vdev = file->private_data;
1110 struct pwc_device *pdev;
1113 Trace(TRACE_OPEN, ">> video_close called(vdev = 0x%p).\n", vdev);
1115 pdev = (struct pwc_device *)vdev->priv;
1116 if (pdev->vopen == 0)
1117 Info("video_close() called on closed device?\n");
1119 /* Dump statistics, but only if a reasonable amount of frames were
1120 processed (to prevent endless log-entries in case of snap-shot
1123 if (pdev->vframe_count > 20)
1124 Info("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev->vframe_count, pdev->vframes_dumped, pdev->vframes_error);
1135 /* pwc_dec23_exit(); *//* Timon & Kiara */
1139 /* pwc_dec1_exit(); */
1143 pwc_isoc_cleanup(pdev);
1144 pwc_free_buffers(pdev);
1146 /* Turn off LEDS and power down camera, but only when not unplugged */
1147 if (pdev->error_status != EPIPE) {
1149 if (pwc_set_leds(pdev, 0, 0) < 0)
1150 Info("Failed to set LED on/off time.\n");
1152 i = pwc_camera_power(pdev, 0);
1154 Err("Failed to power down camera (%d)\n", i);
1158 Trace(TRACE_OPEN, "<< video_close()\n");
1163 * FIXME: what about two parallel reads ????
1164 * ANSWER: Not supported. You can't open the device more than once,
1165 despite what the V4L1 interface says. First, I don't see
1166 the need, second there's no mechanism of alerting the
1167 2nd/3rd/... process of events like changing image size.
1168 And I don't see the point of blocking that for the
1169 2nd/3rd/... process.
1170 In multi-threaded environments reading parallel from any
1171 device is tricky anyhow.
1174 static ssize_t pwc_video_read(struct file *file, char __user * buf,
1175 size_t count, loff_t *ppos)
1177 struct video_device *vdev = file->private_data;
1178 struct pwc_device *pdev;
1179 int noblock = file->f_flags & O_NONBLOCK;
1180 DECLARE_WAITQUEUE(wait, current);
1183 Trace(TRACE_READ, "video_read(0x%p, %p, %zu) called.\n", vdev, buf, count);
1189 if (pdev->error_status)
1190 return -pdev->error_status; /* Something happened, report what. */
1192 /* In case we're doing partial reads, we don't have to wait for a frame */
1193 if (pdev->image_read_pos == 0) {
1194 /* Do wait queueing according to the (doc)book */
1195 add_wait_queue(&pdev->frameq, &wait);
1196 while (pdev->full_frames == NULL) {
1197 /* Check for unplugged/etc. here */
1198 if (pdev->error_status) {
1199 remove_wait_queue(&pdev->frameq, &wait);
1200 set_current_state(TASK_RUNNING);
1201 return -pdev->error_status ;
1204 remove_wait_queue(&pdev->frameq, &wait);
1205 set_current_state(TASK_RUNNING);
1206 return -EWOULDBLOCK;
1208 if (signal_pending(current)) {
1209 remove_wait_queue(&pdev->frameq, &wait);
1210 set_current_state(TASK_RUNNING);
1211 return -ERESTARTSYS;
1214 set_current_state(TASK_INTERRUPTIBLE);
1216 remove_wait_queue(&pdev->frameq, &wait);
1217 set_current_state(TASK_RUNNING);
1219 /* Decompress and release frame */
1220 if (pwc_handle_frame(pdev))
1224 Trace(TRACE_READ, "Copying data to user space.\n");
1225 if (pdev->vpalette == VIDEO_PALETTE_RAW)
1226 bytes_to_read = pdev->frame_size;
1228 bytes_to_read = pdev->view.size;
1230 /* copy bytes to user space; we allow for partial reads */
1231 if (count + pdev->image_read_pos > bytes_to_read)
1232 count = bytes_to_read - pdev->image_read_pos;
1233 if (copy_to_user(buf, pdev->image_ptr[pdev->fill_image] + pdev->image_read_pos, count))
1235 pdev->image_read_pos += count;
1236 if (pdev->image_read_pos >= bytes_to_read) { /* All data has been read */
1237 pdev->image_read_pos = 0;
1238 pwc_next_image(pdev);
1243 static unsigned int pwc_video_poll(struct file *file, poll_table *wait)
1245 struct video_device *vdev = file->private_data;
1246 struct pwc_device *pdev;
1254 poll_wait(file, &pdev->frameq, wait);
1255 if (pdev->error_status)
1257 if (pdev->full_frames != NULL) /* we have frames waiting */
1258 return (POLLIN | POLLRDNORM);
1263 static int pwc_video_do_ioctl(struct inode *inode, struct file *file,
1264 unsigned int cmd, void *arg)
1266 struct video_device *vdev = file->private_data;
1267 struct pwc_device *pdev;
1268 DECLARE_WAITQUEUE(wait, current);
1277 /* Query cabapilities */
1280 struct video_capability *caps = arg;
1282 strcpy(caps->name, vdev->name);
1283 caps->type = VID_TYPE_CAPTURE;
1286 caps->minwidth = pdev->view_min.x;
1287 caps->minheight = pdev->view_min.y;
1288 caps->maxwidth = pdev->view_max.x;
1289 caps->maxheight = pdev->view_max.y;
1293 /* Channel functions (simulate 1 channel) */
1296 struct video_channel *v = arg;
1298 if (v->channel != 0)
1302 v->type = VIDEO_TYPE_CAMERA;
1303 strcpy(v->name, "Webcam");
1309 /* The spec says the argument is an integer, but
1310 the bttv driver uses a video_channel arg, which
1311 makes sense becasue it also has the norm flag.
1313 struct video_channel *v = arg;
1314 if (v->channel != 0)
1320 /* Picture functions; contrast etc. */
1323 struct video_picture *p = arg;
1326 val = pwc_get_brightness(pdev);
1328 p->brightness = val;
1330 p->brightness = 0xffff;
1331 val = pwc_get_contrast(pdev);
1335 p->contrast = 0xffff;
1336 /* Gamma, Whiteness, what's the difference? :) */
1337 val = pwc_get_gamma(pdev);
1341 p->whiteness = 0xffff;
1342 val = pwc_get_saturation(pdev);
1348 p->palette = pdev->vpalette;
1349 p->hue = 0xFFFF; /* N/A */
1355 struct video_picture *p = arg;
1357 * FIXME: Suppose we are mid read
1358 ANSWER: No problem: the firmware of the camera
1359 can handle brightness/contrast/etc
1360 changes at _any_ time, and the palette
1361 is used exactly once in the uncompress
1364 pwc_set_brightness(pdev, p->brightness);
1365 pwc_set_contrast(pdev, p->contrast);
1366 pwc_set_gamma(pdev, p->whiteness);
1367 pwc_set_saturation(pdev, p->colour);
1368 if (p->palette && p->palette != pdev->vpalette) {
1369 switch (p->palette) {
1370 case VIDEO_PALETTE_YUV420P:
1371 case VIDEO_PALETTE_RAW:
1372 pdev->vpalette = p->palette;
1373 return pwc_try_video_mode(pdev, pdev->image.x, pdev->image.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1383 /* Window/size parameters */
1386 struct video_window *vw = arg;
1390 vw->width = pdev->view.x;
1391 vw->height = pdev->view.y;
1393 vw->flags = (pdev->vframes << PWC_FPS_SHIFT) |
1394 (pdev->vsnapshot ? PWC_FPS_SNAPSHOT : 0);
1400 struct video_window *vw = arg;
1401 int fps, snapshot, ret;
1403 fps = (vw->flags & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
1404 snapshot = vw->flags & PWC_FPS_SNAPSHOT;
1406 fps = pdev->vframes;
1407 if (pdev->view.x == vw->width && pdev->view.y && fps == pdev->vframes && snapshot == pdev->vsnapshot)
1409 ret = pwc_try_video_mode(pdev, vw->width, vw->height, fps, pdev->vcompression, snapshot);
1415 /* We don't have overlay support (yet) */
1418 struct video_buffer *vb = arg;
1420 memset(vb,0,sizeof(*vb));
1424 /* mmap() functions */
1427 /* Tell the user program how much memory is needed for a mmap() */
1428 struct video_mbuf *vm = arg;
1431 memset(vm, 0, sizeof(*vm));
1432 vm->size = default_mbufs * pdev->len_per_image;
1433 vm->frames = default_mbufs; /* double buffering should be enough for most applications */
1434 for (i = 0; i < default_mbufs; i++)
1435 vm->offsets[i] = i * pdev->len_per_image;
1439 case VIDIOCMCAPTURE:
1441 /* Start capture into a given image buffer (called 'frame' in video_mmap structure) */
1442 struct video_mmap *vm = arg;
1444 Trace(TRACE_READ, "VIDIOCMCAPTURE: %dx%d, frame %d, format %d\n", vm->width, vm->height, vm->frame, vm->format);
1445 if (vm->frame < 0 || vm->frame >= default_mbufs)
1448 /* xawtv is nasty. It probes the available palettes
1449 by setting a very small image size and trying
1450 various palettes... The driver doesn't support
1451 such small images, so I'm working around it.
1457 case VIDEO_PALETTE_YUV420P:
1458 case VIDEO_PALETTE_RAW:
1466 if ((vm->width != pdev->view.x || vm->height != pdev->view.y) &&
1467 (vm->width >= pdev->view_min.x && vm->height >= pdev->view_min.y)) {
1470 Trace(TRACE_OPEN, "VIDIOCMCAPTURE: changing size to please xawtv :-(.\n");
1471 ret = pwc_try_video_mode(pdev, vm->width, vm->height, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1474 } /* ... size mismatch */
1476 /* FIXME: should we lock here? */
1477 if (pdev->image_used[vm->frame])
1478 return -EBUSY; /* buffer wasn't available. Bummer */
1479 pdev->image_used[vm->frame] = 1;
1481 /* Okay, we're done here. In the SYNC call we wait until a
1482 frame comes available, then expand image into the given
1484 In contrast to the CPiA cam the Philips cams deliver a
1485 constant stream, almost like a grabber card. Also,
1486 we have separate buffers for the rawdata and the image,
1487 meaning we can nearly always expand into the requested buffer.
1489 Trace(TRACE_READ, "VIDIOCMCAPTURE done.\n");
1495 /* The doc says: "Whenever a buffer is used it should
1496 call VIDIOCSYNC to free this frame up and continue."
1498 The only odd thing about this whole procedure is
1499 that MCAPTURE flags the buffer as "in use", and
1500 SYNC immediately unmarks it, while it isn't
1501 after SYNC that you know that the buffer actually
1502 got filled! So you better not start a CAPTURE in
1503 the same frame immediately (use double buffering).
1504 This is not a problem for this cam, since it has
1505 extra intermediate buffers, but a hardware
1506 grabber card will then overwrite the buffer
1512 Trace(TRACE_READ, "VIDIOCSYNC called (%d).\n", *mbuf);
1515 if (*mbuf < 0 || *mbuf >= default_mbufs)
1517 /* check if this buffer was requested anyway */
1518 if (pdev->image_used[*mbuf] == 0)
1521 /* Add ourselves to the frame wait-queue.
1523 FIXME: needs auditing for safety.
1524 QUESTION: In what respect? I think that using the
1527 add_wait_queue(&pdev->frameq, &wait);
1528 while (pdev->full_frames == NULL) {
1529 if (pdev->error_status) {
1530 remove_wait_queue(&pdev->frameq, &wait);
1531 set_current_state(TASK_RUNNING);
1532 return -pdev->error_status;
1535 if (signal_pending(current)) {
1536 remove_wait_queue(&pdev->frameq, &wait);
1537 set_current_state(TASK_RUNNING);
1538 return -ERESTARTSYS;
1541 set_current_state(TASK_INTERRUPTIBLE);
1543 remove_wait_queue(&pdev->frameq, &wait);
1544 set_current_state(TASK_RUNNING);
1546 /* The frame is ready. Expand in the image buffer
1547 requested by the user. I don't care if you
1548 mmap() 5 buffers and request data in this order:
1549 buffer 4 2 3 0 1 2 3 0 4 3 1 . . .
1550 Grabber hardware may not be so forgiving.
1552 Trace(TRACE_READ, "VIDIOCSYNC: frame ready.\n");
1553 pdev->fill_image = *mbuf; /* tell in which buffer we want the image to be expanded */
1554 /* Decompress, etc */
1555 ret = pwc_handle_frame(pdev);
1556 pdev->image_used[*mbuf] = 0;
1564 struct video_audio *v = arg;
1566 strcpy(v->name, "Microphone");
1567 v->audio = -1; /* unknown audio minor */
1569 v->mode = VIDEO_SOUND_MONO;
1573 v->balance = 0x8000;
1580 /* Dummy: nothing can be set */
1586 struct video_unit *vu = arg;
1588 vu->video = pdev->vdev->minor & 0x3F;
1589 vu->audio = -1; /* not known yet */
1596 return pwc_ioctl(pdev, cmd, arg);
1601 static int pwc_video_ioctl(struct inode *inode, struct file *file,
1602 unsigned int cmd, unsigned long arg)
1604 return video_usercopy(inode, file, cmd, arg, pwc_video_do_ioctl);
1608 static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma)
1610 struct video_device *vdev = file->private_data;
1611 struct pwc_device *pdev;
1612 unsigned long start = vma->vm_start;
1613 unsigned long size = vma->vm_end-vma->vm_start;
1614 unsigned long page, pos;
1616 Trace(TRACE_MEMORY, "mmap(0x%p, 0x%lx, %lu) called.\n", vdev, start, size);
1619 vma->vm_flags |= VM_IO;
1621 pos = (unsigned long)pdev->image_data;
1623 page = vmalloc_to_pfn((void *)pos);
1624 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1629 if (size > PAGE_SIZE)
1638 /***************************************************************************/
1641 /* This function gets called when a new device is plugged in or the usb core
1645 static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
1647 struct usb_device *udev = interface_to_usbdev(intf);
1648 struct pwc_device *pdev = NULL;
1649 int vendor_id, product_id, type_id;
1652 int video_nr = -1; /* default: use next available device */
1653 char serial_number[30], *name;
1655 /* Check if we can handle this device */
1656 Trace(TRACE_PROBE, "probe() called [%04X %04X], if %d\n",
1657 le16_to_cpu(udev->descriptor.idVendor),
1658 le16_to_cpu(udev->descriptor.idProduct),
1659 intf->altsetting->desc.bInterfaceNumber);
1661 /* the interfaces are probed one by one. We are only interested in the
1662 video interface (0) now.
1663 Interface 1 is the Audio Control, and interface 2 Audio itself.
1665 if (intf->altsetting->desc.bInterfaceNumber > 0)
1668 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
1669 product_id = le16_to_cpu(udev->descriptor.idProduct);
1671 if (vendor_id == 0x0471) {
1672 switch (product_id) {
1674 Info("Philips PCA645VC USB webcam detected.\n");
1675 name = "Philips 645 webcam";
1679 Info("Philips PCA646VC USB webcam detected.\n");
1680 name = "Philips 646 webcam";
1684 Info("Askey VC010 type 2 USB webcam detected.\n");
1685 name = "Askey VC010 webcam";
1689 Info("Philips PCVC675K (Vesta) USB webcam detected.\n");
1690 name = "Philips 675 webcam";
1694 Info("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
1695 name = "Philips 680 webcam";
1699 Info("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
1700 name = "Philips 690 webcam";
1704 Info("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
1705 name = "Philips 730 webcam";
1709 Info("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
1710 name = "Philips 740 webcam";
1714 Info("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
1715 name = "Philips 750 webcam";
1719 Info("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
1720 name = "Philips 720K/40 webcam";
1728 else if (vendor_id == 0x069A) {
1729 switch(product_id) {
1731 Info("Askey VC010 type 1 USB webcam detected.\n");
1732 name = "Askey VC010 webcam";
1740 else if (vendor_id == 0x046d) {
1741 switch(product_id) {
1743 Info("Logitech QuickCam Pro 3000 USB webcam detected.\n");
1744 name = "Logitech QuickCam Pro 3000";
1745 type_id = 740; /* CCD sensor */
1748 Info("Logitech QuickCam Notebook Pro USB webcam detected.\n");
1749 name = "Logitech QuickCam Notebook Pro";
1750 type_id = 740; /* CCD sensor */
1753 Info("Logitech QuickCam 4000 Pro USB webcam detected.\n");
1754 name = "Logitech QuickCam Pro 4000";
1755 type_id = 740; /* CCD sensor */
1758 Info("Logitech QuickCam Zoom USB webcam detected.\n");
1759 name = "Logitech QuickCam Zoom";
1760 type_id = 740; /* CCD sensor */
1763 Info("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
1764 name = "Logitech QuickCam Zoom";
1765 type_id = 740; /* CCD sensor */
1768 Info("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
1769 name = "Logitech QuickCam Orbit";
1770 type_id = 740; /* CCD sensor */
1771 features |= FEATURE_MOTOR_PANTILT;
1776 Info("Logitech QuickCam detected (reserved ID).\n");
1777 name = "Logitech QuickCam (res.)";
1778 type_id = 730; /* Assuming CMOS */
1785 else if (vendor_id == 0x055d) {
1786 /* I don't know the difference between the C10 and the C30;
1787 I suppose the difference is the sensor, but both cameras
1788 work equally well with a type_id of 675
1790 switch(product_id) {
1792 Info("Samsung MPC-C10 USB webcam detected.\n");
1793 name = "Samsung MPC-C10";
1797 Info("Samsung MPC-C30 USB webcam detected.\n");
1798 name = "Samsung MPC-C30";
1806 else if (vendor_id == 0x041e) {
1807 switch(product_id) {
1809 Info("Creative Labs Webcam 5 detected.\n");
1810 name = "Creative Labs Webcam 5";
1814 Info("Creative Labs Webcam Pro Ex detected.\n");
1815 name = "Creative Labs Webcam Pro Ex";
1823 else if (vendor_id == 0x04cc) {
1824 switch(product_id) {
1826 Info("Sotec Afina Eye USB webcam detected.\n");
1827 name = "Sotec Afina Eye";
1835 else if (vendor_id == 0x06be) {
1836 switch(product_id) {
1838 /* This is essentially the same cam as the Sotec Afina Eye */
1839 Info("AME Co. Afina Eye USB webcam detected.\n");
1840 name = "AME Co. Afina Eye";
1849 else if (vendor_id == 0x0d81) {
1850 switch(product_id) {
1852 Info("Visionite VCS-UC300 USB webcam detected.\n");
1853 name = "Visionite VCS-UC300";
1854 type_id = 740; /* CCD sensor */
1857 Info("Visionite VCS-UM100 USB webcam detected.\n");
1858 name = "Visionite VCS-UM100";
1859 type_id = 730; /* CMOS sensor */
1867 return -ENODEV; /* Not any of the know types; but the list keeps growing. */
1869 memset(serial_number, 0, 30);
1870 usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
1871 Trace(TRACE_PROBE, "Device serial number is %s\n", serial_number);
1873 if (udev->descriptor.bNumConfigurations > 1)
1874 Info("Warning: more than 1 configuration available.\n");
1876 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1877 pdev = kmalloc(sizeof(struct pwc_device), GFP_KERNEL);
1879 Err("Oops, could not allocate memory for pwc_device.\n");
1882 memset(pdev, 0, sizeof(struct pwc_device));
1883 pdev->type = type_id;
1884 pdev->vsize = default_size;
1885 pdev->vframes = default_fps;
1886 strcpy(pdev->serial, serial_number);
1887 pdev->features = features;
1888 if (vendor_id == 0x046D && product_id == 0x08B5)
1890 /* Logitech QuickCam Orbit
1891 The ranges have been determined experimentally; they may differ from cam to cam.
1892 Also, the exact ranges left-right and up-down are different for my cam
1894 pdev->angle_range.pan_min = -7000;
1895 pdev->angle_range.pan_max = 7000;
1896 pdev->angle_range.tilt_min = -3000;
1897 pdev->angle_range.tilt_max = 2500;
1900 init_MUTEX(&pdev->modlock);
1901 spin_lock_init(&pdev->ptrlock);
1904 init_waitqueue_head(&pdev->frameq);
1905 pdev->vcompression = pwc_preferred_compression;
1907 /* Allocate video_device structure */
1908 pdev->vdev = video_device_alloc();
1909 if (pdev->vdev == 0)
1911 Err("Err, cannot allocate video_device struture. Failing probe.");
1915 memcpy(pdev->vdev, &pwc_template, sizeof(pwc_template));
1916 strcpy(pdev->vdev->name, name);
1917 pdev->vdev->owner = THIS_MODULE;
1918 video_set_drvdata(pdev->vdev, pdev);
1920 pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1921 Trace(TRACE_PROBE, "Release: %04x\n", pdev->release);
1923 /* Now search device_hint[] table for a match, so we can hint a node number. */
1924 for (hint = 0; hint < MAX_DEV_HINTS; hint++) {
1925 if (((device_hint[hint].type == -1) || (device_hint[hint].type == pdev->type)) &&
1926 (device_hint[hint].pdev == NULL)) {
1927 /* so far, so good... try serial number */
1928 if ((device_hint[hint].serial_number[0] == '*') || !strcmp(device_hint[hint].serial_number, serial_number)) {
1930 video_nr = device_hint[hint].device_node;
1931 Trace(TRACE_PROBE, "Found hint, will try to register as /dev/video%d\n", video_nr);
1937 pdev->vdev->release = video_device_release;
1938 i = video_register_device(pdev->vdev, VFL_TYPE_GRABBER, video_nr);
1940 Err("Failed to register as video device (%d).\n", i);
1941 video_device_release(pdev->vdev); /* Drip... drip... drip... */
1942 kfree(pdev); /* Oops, no memory leaks please */
1946 Info("Registered as /dev/video%d.\n", pdev->vdev->minor & 0x3F);
1950 if (hint < MAX_DEV_HINTS)
1951 device_hint[hint].pdev = pdev;
1953 Trace(TRACE_PROBE, "probe() function returning struct at 0x%p.\n", pdev);
1954 usb_set_intfdata (intf, pdev);
1958 /* The user janked out the cable... */
1959 static void usb_pwc_disconnect(struct usb_interface *intf)
1961 struct pwc_device *pdev;
1965 pdev = usb_get_intfdata (intf);
1966 usb_set_intfdata (intf, NULL);
1968 Err("pwc_disconnect() Called without private pointer.\n");
1969 goto disconnect_out;
1971 if (pdev->udev == NULL) {
1972 Err("pwc_disconnect() already called for %p\n", pdev);
1973 goto disconnect_out;
1975 if (pdev->udev != interface_to_usbdev(intf)) {
1976 Err("pwc_disconnect() Woops: pointer mismatch udev/pdev.\n");
1977 goto disconnect_out;
1980 if (pdev->magic != PWC_MAGIC) {
1981 Err("pwc_disconnect() Magic number failed. Consult your scrolls and try again.\n");
1982 goto disconnect_out;
1986 /* We got unplugged; this is signalled by an EPIPE error code */
1988 Info("Disconnected while webcam is in use!\n");
1989 pdev->error_status = EPIPE;
1992 /* Alert waiting processes */
1993 wake_up_interruptible(&pdev->frameq);
1994 /* Wait until device is closed */
1997 /* Device is now closed, so we can safely unregister it */
1998 Trace(TRACE_PROBE, "Unregistering video device in disconnect().\n");
1999 video_unregister_device(pdev->vdev);
2001 /* Free memory (don't set pdev to 0 just yet) */
2005 /* search device_hint[] table if we occupy a slot, by any chance */
2006 for (hint = 0; hint < MAX_DEV_HINTS; hint++)
2007 if (device_hint[hint].pdev == pdev)
2008 device_hint[hint].pdev = NULL;
2014 /* *grunt* We have to do atoi ourselves :-( */
2015 static int pwc_atoi(const char *s)
2020 while (*s != '\0' && *s >= '0' && *s <= '9') {
2021 k = 10 * k + (*s - '0');
2029 * Initialization code & module stuff
2032 static char size[10];
2034 static int fbufs = 0;
2035 static int mbufs = 0;
2036 static int trace = -1;
2037 static int compression = -1;
2038 static int leds[2] = { -1, -1 };
2039 static char *dev_hint[MAX_DEV_HINTS] = { };
2041 module_param_string(size, size, sizeof(size), 0);
2042 MODULE_PARM_DESC(size, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
2043 module_param(fps, int, 0000);
2044 MODULE_PARM_DESC(fps, "Initial frames per second. Varies with model, useful range 5-30");
2045 module_param(fbufs, int, 0000);
2046 MODULE_PARM_DESC(fbufs, "Number of internal frame buffers to reserve");
2047 module_param(mbufs, int, 0000);
2048 MODULE_PARM_DESC(mbufs, "Number of external (mmap()ed) image buffers");
2049 module_param(trace, int, 0000);
2050 MODULE_PARM_DESC(trace, "For debugging purposes");
2051 module_param(power_save, bool, 0000);
2052 MODULE_PARM_DESC(power_save, "Turn power save feature in camera on or off");
2053 module_param(compression, int, 0000);
2054 MODULE_PARM_DESC(compression, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
2055 module_param_array(leds, int, NULL, 0000);
2056 MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
2057 module_param_array(dev_hint, charp, NULL, 0000);
2058 MODULE_PARM_DESC(dev_hint, "Device node hints");
2060 MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
2061 MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
2062 MODULE_LICENSE("GPL");
2064 static int __init usb_pwc_init(void)
2067 char *sizenames[PSZ_MAX] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };
2069 Info("Philips webcam module version " PWC_VERSION " loaded.\n");
2070 Info("Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.\n");
2071 Info("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
2072 Info("the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
2075 if (fps < 4 || fps > 30) {
2076 Err("Framerate out of bounds (4-30).\n");
2080 Info("Default framerate set to %d.\n", default_fps);
2084 /* string; try matching with array */
2085 for (sz = 0; sz < PSZ_MAX; sz++) {
2086 if (!strcmp(sizenames[sz], size)) { /* Found! */
2091 if (sz == PSZ_MAX) {
2092 Err("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
2095 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);
2098 if (mbufs < 1 || mbufs > MAX_IMAGES) {
2099 Err("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES);
2102 default_mbufs = mbufs;
2103 Info("Number of image buffers set to %d.\n", default_mbufs);
2106 if (fbufs < 2 || fbufs > MAX_FRAMES) {
2107 Err("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES);
2110 default_fbufs = fbufs;
2111 Info("Number of frame buffers set to %d.\n", default_fbufs);
2114 Info("Trace options: 0x%04x\n", trace);
2117 if (compression >= 0) {
2118 if (compression > 3) {
2119 Err("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
2122 pwc_preferred_compression = compression;
2123 Info("Preferred compression set to %d.\n", pwc_preferred_compression);
2126 Info("Enabling power save on open/close.\n");
2132 /* Big device node whoopla. Basically, it allows you to assign a
2133 device node (/dev/videoX) to a camera, based on its type
2134 & serial number. The format is [type[.serialnumber]:]node.
2136 Any camera that isn't matched by these rules gets the next
2137 available free device node.
2139 for (i = 0; i < MAX_DEV_HINTS; i++) {
2140 char *s, *colon, *dot;
2142 /* This loop also initializes the array */
2143 device_hint[i].pdev = NULL;
2145 if (s != NULL && *s != '\0') {
2146 device_hint[i].type = -1; /* wildcard */
2147 strcpy(device_hint[i].serial_number, "*");
2149 /* parse string: chop at ':' & '/' */
2151 while (*colon != '\0' && *colon != ':')
2153 while (*dot != '\0' && *dot != '.')
2155 /* Few sanity checks */
2156 if (*dot != '\0' && dot > colon) {
2157 Err("Malformed camera hint: the colon must be after the dot.\n");
2161 if (*colon == '\0') {
2164 Err("Malformed camera hint: no colon + device node given.\n");
2168 /* No type or serial number specified, just a number. */
2169 device_hint[i].device_node = pwc_atoi(s);
2173 /* There's a colon, so we have at least a type and a device node */
2174 device_hint[i].type = pwc_atoi(s);
2175 device_hint[i].device_node = pwc_atoi(colon + 1);
2177 /* There's a serial number as well */
2182 while (*dot != ':' && k < 29) {
2183 device_hint[i].serial_number[k++] = *dot;
2186 device_hint[i].serial_number[k] = '\0';
2190 Debug("device_hint[%d]:\n", i);
2191 Debug(" type : %d\n", device_hint[i].type);
2192 Debug(" serial# : %s\n", device_hint[i].serial_number);
2193 Debug(" node : %d\n", device_hint[i].device_node);
2197 device_hint[i].type = 0; /* not filled */
2198 } /* ..for MAX_DEV_HINTS */
2200 Trace(TRACE_PROBE, "Registering driver at address 0x%p.\n", &pwc_driver);
2201 return usb_register(&pwc_driver);
2204 static void __exit usb_pwc_exit(void)
2206 Trace(TRACE_MODULE, "Deregistering driver.\n");
2207 usb_deregister(&pwc_driver);
2208 Info("Philips webcam module removed.\n");
2211 module_init(usb_pwc_init);
2212 module_exit(usb_pwc_exit);