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 .name = "Philips webcam", /* name */
114 .id_table = pwc_device_table,
115 .probe = usb_pwc_probe, /* probe() */
116 .disconnect = usb_pwc_disconnect, /* disconnect() */
119 #define MAX_DEV_HINTS 20
120 #define MAX_ISOC_ERRORS 20
122 static int default_size = PSZ_QCIF;
123 static int default_fps = 10;
124 static int default_fbufs = 3; /* Default number of frame buffers */
125 static int default_mbufs = 2; /* Default number of mmap() buffers */
126 int pwc_trace = TRACE_MODULE | TRACE_FLOW | TRACE_PWCX;
127 static int power_save = 0;
128 static int led_on = 100, led_off = 0; /* defaults to LED that is on while in use */
129 static int pwc_preferred_compression = 2; /* 0..3 = uncompressed..high */
132 char serial_number[30];
134 struct pwc_device *pdev;
135 } device_hint[MAX_DEV_HINTS];
139 static int pwc_video_open(struct inode *inode, struct file *file);
140 static int pwc_video_close(struct inode *inode, struct file *file);
141 static ssize_t pwc_video_read(struct file *file, char __user * buf,
142 size_t count, loff_t *ppos);
143 static unsigned int pwc_video_poll(struct file *file, poll_table *wait);
144 static int pwc_video_ioctl(struct inode *inode, struct file *file,
145 unsigned int ioctlnr, unsigned long arg);
146 static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma);
148 static struct file_operations pwc_fops = {
149 .owner = THIS_MODULE,
150 .open = pwc_video_open,
151 .release = pwc_video_close,
152 .read = pwc_video_read,
153 .poll = pwc_video_poll,
154 .mmap = pwc_video_mmap,
155 .ioctl = pwc_video_ioctl,
156 .compat_ioctl = v4l_compat_ioctl32,
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);
335 pdev->decompress_data = kbuf;
337 /* Allocate image buffer; double buffer for mmap() */
338 kbuf = rvmalloc(default_mbufs * pdev->len_per_image);
340 Err("Failed to allocate image buffer(s). needed (%d)\n",default_mbufs * pdev->len_per_image);
343 Trace(TRACE_MEMORY, "Allocated image buffer at %p.\n", kbuf);
344 pdev->image_data = kbuf;
345 for (i = 0; i < default_mbufs; i++)
346 pdev->image_ptr[i] = kbuf + i * pdev->len_per_image;
347 for (; i < MAX_IMAGES; i++)
348 pdev->image_ptr[i] = NULL;
352 Trace(TRACE_MEMORY, "<< pwc_allocate_buffers()\n");
356 static void pwc_free_buffers(struct pwc_device *pdev)
360 Trace(TRACE_MEMORY, "Entering free_buffers(%p).\n", pdev);
365 if (pdev->magic != PWC_MAGIC) {
366 Err("free_buffers(): magic failed.\n");
371 /* Release Iso-pipe buffers */
372 for (i = 0; i < MAX_ISO_BUFS; i++)
373 if (pdev->sbuf[i].data != NULL) {
374 Trace(TRACE_MEMORY, "Freeing ISO buffer at %p.\n", pdev->sbuf[i].data);
375 kfree(pdev->sbuf[i].data);
376 pdev->sbuf[i].data = NULL;
379 /* The same for frame buffers */
380 if (pdev->fbuf != NULL) {
381 for (i = 0; i < default_fbufs; i++) {
382 if (pdev->fbuf[i].data != NULL) {
383 Trace(TRACE_MEMORY, "Freeing frame buffer %d at %p.\n", i, pdev->fbuf[i].data);
384 vfree(pdev->fbuf[i].data);
385 pdev->fbuf[i].data = NULL;
392 /* Intermediate decompression buffer & tables */
393 if (pdev->decompress_data != NULL) {
394 Trace(TRACE_MEMORY, "Freeing decompression buffer at %p.\n", pdev->decompress_data);
395 kfree(pdev->decompress_data);
396 pdev->decompress_data = NULL;
398 pdev->decompressor = NULL;
400 /* Release image buffers */
401 if (pdev->image_data != NULL) {
402 Trace(TRACE_MEMORY, "Freeing image buffer at %p.\n", pdev->image_data);
403 rvfree(pdev->image_data, default_mbufs * pdev->len_per_image);
405 pdev->image_data = NULL;
407 Trace(TRACE_MEMORY, "Leaving free_buffers().\n");
410 /* The frame & image buffer mess.
412 Yes, this is a mess. Well, it used to be simple, but alas... In this
413 module, 3 buffers schemes are used to get the data from the USB bus to
414 the user program. The first scheme involves the ISO buffers (called thus
415 since they transport ISO data from the USB controller), and not really
416 interesting. Suffices to say the data from this buffer is quickly
417 gathered in an interrupt handler (pwc_isoc_handler) and placed into the
420 The frame buffer is the second scheme, and is the central element here.
421 It collects the data from a single frame from the camera (hence, the
422 name). Frames are delimited by the USB camera with a short USB packet,
423 so that's easy to detect. The frame buffers form a list that is filled
424 by the camera+USB controller and drained by the user process through
425 either read() or mmap().
427 The image buffer is the third scheme, in which frames are decompressed
428 and converted into planar format. For mmap() there is more than
429 one image buffer available.
431 The frame buffers provide the image buffering. In case the user process
432 is a bit slow, this introduces lag and some undesired side-effects.
433 The problem arises when the frame buffer is full. I used to drop the last
434 frame, which makes the data in the queue stale very quickly. But dropping
435 the frame at the head of the queue proved to be a litte bit more difficult.
436 I tried a circular linked scheme, but this introduced more problems than
439 Because filling and draining are completely asynchronous processes, this
440 requires some fiddling with pointers and mutexes.
442 Eventually, I came up with a system with 2 lists: an 'empty' frame list
443 and a 'full' frame list:
444 * Initially, all frame buffers but one are on the 'empty' list; the one
445 remaining buffer is our initial fill frame.
446 * If a frame is needed for filling, we try to take it from the 'empty'
447 list, unless that list is empty, in which case we take the buffer at
448 the head of the 'full' list.
449 * When our fill buffer has been filled, it is appended to the 'full'
451 * If a frame is needed by read() or mmap(), it is taken from the head of
452 the 'full' list, handled, and then appended to the 'empty' list. If no
453 buffer is present on the 'full' list, we wait.
454 The advantage is that the buffer that is currently being decompressed/
455 converted, is on neither list, and thus not in our way (any other scheme
456 I tried had the problem of old data lingering in the queue).
458 Whatever strategy you choose, it always remains a tradeoff: with more
459 frame buffers the chances of a missed frame are reduced. On the other
460 hand, on slower machines it introduces lag because the queue will
465 \brief Find next frame buffer to fill. Take from empty or full list, whichever comes first.
467 static inline int pwc_next_fill_frame(struct pwc_device *pdev)
473 spin_lock_irqsave(&pdev->ptrlock, flags);
474 if (pdev->fill_frame != NULL) {
475 /* append to 'full' list */
476 if (pdev->full_frames == NULL) {
477 pdev->full_frames = pdev->fill_frame;
478 pdev->full_frames_tail = pdev->full_frames;
481 pdev->full_frames_tail->next = pdev->fill_frame;
482 pdev->full_frames_tail = pdev->fill_frame;
485 if (pdev->empty_frames != NULL) {
486 /* We have empty frames available. That's easy */
487 pdev->fill_frame = pdev->empty_frames;
488 pdev->empty_frames = pdev->empty_frames->next;
491 /* Hmm. Take it from the full list */
494 if (pdev->full_frames == NULL) {
495 Err("Neither empty or full frames available!\n");
496 spin_unlock_irqrestore(&pdev->ptrlock, flags);
500 pdev->fill_frame = pdev->full_frames;
501 pdev->full_frames = pdev->full_frames->next;
504 pdev->fill_frame->next = NULL;
506 Trace(TRACE_SEQUENCE, "Assigning sequence number %d.\n", pdev->sequence);
507 pdev->fill_frame->sequence = pdev->sequence++;
509 spin_unlock_irqrestore(&pdev->ptrlock, flags);
515 \brief Reset all buffers, pointers and lists, except for the image_used[] buffer.
517 If the image_used[] buffer is cleared too, mmap()/VIDIOCSYNC will run into trouble.
519 static void pwc_reset_buffers(struct pwc_device *pdev)
524 spin_lock_irqsave(&pdev->ptrlock, flags);
525 pdev->full_frames = NULL;
526 pdev->full_frames_tail = NULL;
527 for (i = 0; i < default_fbufs; i++) {
528 pdev->fbuf[i].filled = 0;
530 pdev->fbuf[i].next = &pdev->fbuf[i - 1];
532 pdev->fbuf->next = NULL;
534 pdev->empty_frames = &pdev->fbuf[default_fbufs - 1];
535 pdev->empty_frames_tail = pdev->fbuf;
536 pdev->read_frame = NULL;
537 pdev->fill_frame = pdev->empty_frames;
538 pdev->empty_frames = pdev->empty_frames->next;
540 pdev->image_read_pos = 0;
541 pdev->fill_image = 0;
542 spin_unlock_irqrestore(&pdev->ptrlock, flags);
547 \brief Do all the handling for getting one frame: get pointer, decompress, advance pointers.
549 static int pwc_handle_frame(struct pwc_device *pdev)
554 spin_lock_irqsave(&pdev->ptrlock, flags);
555 /* First grab our read_frame; this is removed from all lists, so
556 we can release the lock after this without problems */
557 if (pdev->read_frame != NULL) {
558 /* This can't theoretically happen */
559 Err("Huh? Read frame still in use?\n");
562 if (pdev->full_frames == NULL) {
563 Err("Woops. No frames ready.\n");
566 pdev->read_frame = pdev->full_frames;
567 pdev->full_frames = pdev->full_frames->next;
568 pdev->read_frame->next = NULL;
571 if (pdev->read_frame != NULL) {
573 Trace(TRACE_SEQUENCE, "Decompressing frame %d\n", pdev->read_frame->sequence);
575 /* Decompression is a lenghty process, so it's outside of the lock.
576 This gives the isoc_handler the opportunity to fill more frames
579 spin_unlock_irqrestore(&pdev->ptrlock, flags);
580 ret = pwc_decompress(pdev);
581 spin_lock_irqsave(&pdev->ptrlock, flags);
583 /* We're done with read_buffer, tack it to the end of the empty buffer list */
584 if (pdev->empty_frames == NULL) {
585 pdev->empty_frames = pdev->read_frame;
586 pdev->empty_frames_tail = pdev->empty_frames;
589 pdev->empty_frames_tail->next = pdev->read_frame;
590 pdev->empty_frames_tail = pdev->read_frame;
592 pdev->read_frame = NULL;
595 spin_unlock_irqrestore(&pdev->ptrlock, flags);
600 \brief Advance pointers of image buffer (after each user request)
602 static inline void pwc_next_image(struct pwc_device *pdev)
604 pdev->image_used[pdev->fill_image] = 0;
605 pdev->fill_image = (pdev->fill_image + 1) % default_mbufs;
609 /* This gets called for the Isochronous pipe (video). This is done in
610 * interrupt time, so it has to be fast, not crash, and not stall. Neat.
612 static void pwc_isoc_handler(struct urb *urb, struct pt_regs *regs)
614 struct pwc_device *pdev;
617 struct pwc_frame_buf *fbuf;
618 unsigned char *fillptr = NULL, *iso_buf = NULL;
621 pdev = (struct pwc_device *)urb->context;
623 Err("isoc_handler() called with NULL device?!\n");
627 if (pdev->magic != PWC_MAGIC) {
628 Err("isoc_handler() called with bad magic!\n");
632 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
633 Trace(TRACE_OPEN, "pwc_isoc_handler(): URB (%p) unlinked %ssynchronuously.\n", urb, urb->status == -ENOENT ? "" : "a");
636 if (urb->status != -EINPROGRESS && urb->status != 0) {
640 switch(urb->status) {
641 case -ENOSR: errmsg = "Buffer error (overrun)"; break;
642 case -EPIPE: errmsg = "Stalled (device not responding)"; break;
643 case -EOVERFLOW: errmsg = "Babble (bad cable?)"; break;
644 case -EPROTO: errmsg = "Bit-stuff error (bad cable?)"; break;
645 case -EILSEQ: errmsg = "CRC/Timeout (could be anything)"; break;
646 case -ETIMEDOUT: errmsg = "NAK (device does not respond)"; break;
648 Trace(TRACE_FLOW, "pwc_isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
649 /* Give up after a number of contiguous errors on the USB bus.
650 Appearantly something is wrong so we simulate an unplug event.
652 if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
654 Info("Too many ISOC errors, bailing out.\n");
655 pdev->error_status = EIO;
657 wake_up_interruptible(&pdev->frameq);
659 goto handler_end; // ugly, but practical
662 fbuf = pdev->fill_frame;
664 Err("pwc_isoc_handler without valid fill frame.\n");
669 fillptr = fbuf->data + fbuf->filled;
672 /* Reset ISOC error counter. We did get here, after all. */
673 pdev->visoc_errors = 0;
675 /* vsync: 0 = don't copy data
680 for (i = 0; i < urb->number_of_packets; i++) {
681 fst = urb->iso_frame_desc[i].status;
682 flen = urb->iso_frame_desc[i].actual_length;
683 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
685 if (flen > 0) { /* if valid data... */
686 if (pdev->vsync > 0) { /* ...and we are not sync-hunting... */
689 /* ...copy data to frame buffer, if possible */
690 if (flen + fbuf->filled > pdev->frame_total_size) {
691 Trace(TRACE_FLOW, "Frame buffer overflow (flen = %d, frame_total_size = %d).\n", flen, pdev->frame_total_size);
692 pdev->vsync = 0; /* Hmm, let's wait for an EOF (end-of-frame) */
693 pdev->vframes_error++;
696 memmove(fillptr, iso_buf, flen);
700 fbuf->filled += flen;
703 if (flen < pdev->vlast_packet_size) {
704 /* Shorter packet... We probably have the end of an image-frame;
705 wake up read() process and let select()/poll() do something.
706 Decompression is done in user time over there.
708 if (pdev->vsync == 2) {
709 /* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
710 frames on the USB wire after an exposure change. This conditition is
711 however detected in the cam and a bit is set in the header.
713 if (pdev->type == 730) {
714 unsigned char *ptr = (unsigned char *)fbuf->data;
716 if (ptr[1] == 1 && ptr[0] & 0x10) {
718 Debug("Hyundai CMOS sensor bug. Dropping frame %d.\n", fbuf->sequence);
720 pdev->drop_frames += 2;
721 pdev->vframes_error++;
723 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
725 Info("Snapshot button pressed.\n");
727 Info("Snapshot button released.\n");
729 if ((ptr[0] ^ pdev->vmirror) & 0x02) {
731 Info("Image is mirrored.\n");
733 Info("Image is normal.\n");
735 pdev->vmirror = ptr[0] & 0x03;
736 /* Sometimes the trailer of the 730 is still sent as a 4 byte packet
737 after a short frame; this condition is filtered out specifically. A 4 byte
738 frame doesn't make sense anyway.
739 So we get either this sequence:
740 drop_bit set -> 4 byte frame -> short frame -> good frame
742 drop_bit set -> short frame -> good frame
743 So we drop either 3 or 2 frames in all!
745 if (fbuf->filled == 4)
749 /* In case we were instructed to drop the frame, do so silently.
750 The buffer pointers are not updated either (but the counters are reset below).
752 if (pdev->drop_frames > 0)
755 /* Check for underflow first */
756 if (fbuf->filled < pdev->frame_total_size) {
757 Trace(TRACE_FLOW, "Frame buffer underflow (%d bytes); discarded.\n", fbuf->filled);
758 pdev->vframes_error++;
761 /* Send only once per EOF */
762 awake = 1; /* delay wake_ups */
764 /* Find our next frame to fill. This will always succeed, since we
765 * nick a frame from either empty or full list, but if we had to
766 * take it from the full list, it means a frame got dropped.
768 if (pwc_next_fill_frame(pdev)) {
769 pdev->vframes_dumped++;
770 if ((pdev->vframe_count > FRAME_LOWMARK) && (pwc_trace & TRACE_FLOW)) {
771 if (pdev->vframes_dumped < 20)
772 Trace(TRACE_FLOW, "Dumping frame %d.\n", pdev->vframe_count);
773 if (pdev->vframes_dumped == 20)
774 Trace(TRACE_FLOW, "Dumping frame %d (last message).\n", pdev->vframe_count);
777 fbuf = pdev->fill_frame;
780 pdev->vframe_count++;
783 fillptr = fbuf->data;
785 } /* .. flen < last_packet_size */
786 pdev->vlast_packet_size = flen;
787 } /* ..status == 0 */
789 /* This is normally not interesting to the user, unless you are really debugging something */
791 static int iso_error = 0;
794 Trace(TRACE_FLOW, "Iso frame %d of USB has error %d\n", i, fst);
801 wake_up_interruptible(&pdev->frameq);
803 urb->dev = pdev->udev;
804 i = usb_submit_urb(urb, GFP_ATOMIC);
806 Err("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
810 static int pwc_isoc_init(struct pwc_device *pdev)
812 struct usb_device *udev;
816 struct usb_interface *intf;
817 struct usb_host_interface *idesc = NULL;
826 /* Get the current alternate interface, adjust packet size */
827 if (!udev->actconfig)
830 intf = usb_ifnum_to_if(udev, 0);
832 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
837 /* Search video endpoint */
838 pdev->vmax_packet_size = -1;
839 for (i = 0; i < idesc->desc.bNumEndpoints; i++)
840 if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
841 pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
845 if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
846 Err("Failed to find packet size for video endpoint in current alternate setting.\n");
847 return -ENFILE; /* Odd error, that should be noticeable */
850 /* Set alternate interface */
852 Trace(TRACE_OPEN, "Setting alternate interface %d\n", pdev->valternate);
853 ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
857 for (i = 0; i < MAX_ISO_BUFS; i++) {
858 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
860 Err("Failed to allocate urb %d\n", i);
864 pdev->sbuf[i].urb = urb;
865 Trace(TRACE_MEMORY, "Allocated URB at 0x%p\n", urb);
868 /* De-allocate in reverse order */
870 if (pdev->sbuf[i].urb != NULL)
871 usb_free_urb(pdev->sbuf[i].urb);
872 pdev->sbuf[i].urb = NULL;
878 /* init URB structure */
879 for (i = 0; i < MAX_ISO_BUFS; i++) {
880 urb = pdev->sbuf[i].urb;
882 urb->interval = 1; // devik
884 urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
885 urb->transfer_flags = URB_ISO_ASAP;
886 urb->transfer_buffer = pdev->sbuf[i].data;
887 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
888 urb->complete = pwc_isoc_handler;
890 urb->start_frame = 0;
891 urb->number_of_packets = ISO_FRAMES_PER_DESC;
892 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
893 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
894 urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
899 for (i = 0; i < MAX_ISO_BUFS; i++) {
900 ret = usb_submit_urb(pdev->sbuf[i].urb, GFP_KERNEL);
902 Err("isoc_init() submit_urb %d failed with error %d\n", i, ret);
904 Trace(TRACE_MEMORY, "URB 0x%p submitted.\n", pdev->sbuf[i].urb);
909 Trace(TRACE_OPEN, "<< pwc_isoc_init()\n");
913 static void pwc_isoc_cleanup(struct pwc_device *pdev)
917 Trace(TRACE_OPEN, ">> pwc_isoc_cleanup()\n");
921 /* Unlinking ISOC buffers one by one */
922 for (i = 0; i < MAX_ISO_BUFS; i++) {
925 urb = pdev->sbuf[i].urb;
927 if (pdev->iso_init) {
928 Trace(TRACE_MEMORY, "Unlinking URB %p\n", urb);
931 Trace(TRACE_MEMORY, "Freeing URB\n");
933 pdev->sbuf[i].urb = NULL;
937 /* Stop camera, but only if we are sure the camera is still there (unplug
938 is signalled by EPIPE)
940 if (pdev->error_status && pdev->error_status != EPIPE) {
941 Trace(TRACE_OPEN, "Setting alternate interface 0.\n");
942 usb_set_interface(pdev->udev, 0, 0);
946 Trace(TRACE_OPEN, "<< pwc_isoc_cleanup()\n");
949 int pwc_try_video_mode(struct pwc_device *pdev, int width, int height, int new_fps, int new_compression, int new_snapshot)
953 /* Stop isoc stuff */
954 pwc_isoc_cleanup(pdev);
955 /* Reset parameters */
956 pwc_reset_buffers(pdev);
957 /* Try to set video mode... */
958 start = ret = pwc_set_video_mode(pdev, width, height, new_fps, new_compression, new_snapshot);
960 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 1 failed.\n");
961 /* That failed... restore old mode (we know that worked) */
962 start = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
964 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 2 failed.\n");
969 if (pwc_isoc_init(pdev) < 0)
971 Info("Failed to restart ISOC transfers in pwc_try_video_mode.\n");
972 ret = -EAGAIN; /* let's try again, who knows if it works a second time */
975 pdev->drop_frames++; /* try to avoid garbage during switch */
976 return ret; /* Return original error code */
980 /***************************************************************************/
981 /* Video4Linux functions */
983 static int pwc_video_open(struct inode *inode, struct file *file)
986 struct video_device *vdev = video_devdata(file);
987 struct pwc_device *pdev;
989 Trace(TRACE_OPEN, ">> video_open called(vdev = 0x%p).\n", vdev);
991 pdev = (struct pwc_device *)vdev->priv;
997 down(&pdev->modlock);
998 if (!pdev->usb_init) {
999 Trace(TRACE_OPEN, "Doing first time initialization.\n");
1002 if (pwc_trace & TRACE_OPEN)
1004 /* Query sensor type */
1005 const char *sensor_type = NULL;
1008 ret = pwc_get_cmos_sensor(pdev, &i);
1012 case 0x00: sensor_type = "Hyundai CMOS sensor"; break;
1013 case 0x20: sensor_type = "Sony CCD sensor + TDA8787"; break;
1014 case 0x2E: sensor_type = "Sony CCD sensor + Exas 98L59"; break;
1015 case 0x2F: sensor_type = "Sony CCD sensor + ADI 9804"; break;
1016 case 0x30: sensor_type = "Sharp CCD sensor + TDA8787"; break;
1017 case 0x3E: sensor_type = "Sharp CCD sensor + Exas 98L59"; break;
1018 case 0x3F: sensor_type = "Sharp CCD sensor + ADI 9804"; break;
1019 case 0x40: sensor_type = "UPA 1021 sensor"; break;
1020 case 0x100: sensor_type = "VGA sensor"; break;
1021 case 0x101: sensor_type = "PAL MR sensor"; break;
1022 default: sensor_type = "unknown type of sensor"; break;
1025 if (sensor_type != NULL)
1026 Info("This %s camera is equipped with a %s (%d).\n", pdev->vdev->name, sensor_type, i);
1030 /* Turn on camera */
1032 i = pwc_camera_power(pdev, 1);
1034 Info("Failed to restore power to the camera! (%d)\n", i);
1036 /* Set LED on/off time */
1037 if (pwc_set_leds(pdev, led_on, led_off) < 0)
1038 Info("Failed to set LED on/off time.\n");
1040 pwc_construct(pdev); /* set min/max sizes correct */
1042 /* So far, so good. Allocate memory. */
1043 i = pwc_allocate_buffers(pdev);
1045 Trace(TRACE_OPEN, "Failed to allocate buffer memory.\n");
1050 /* Reset buffers & parameters */
1051 pwc_reset_buffers(pdev);
1052 for (i = 0; i < default_mbufs; i++)
1053 pdev->image_used[i] = 0;
1054 pdev->vframe_count = 0;
1055 pdev->vframes_dumped = 0;
1056 pdev->vframes_error = 0;
1057 pdev->visoc_errors = 0;
1058 pdev->error_status = 0;
1062 pwc_construct(pdev); /* set min/max sizes correct */
1064 /* Set some defaults */
1065 pdev->vsnapshot = 0;
1067 /* Start iso pipe for video; first try the last used video size
1068 (or the default one); if that fails try QCIF/10 or QSIF/10;
1069 it that fails too, give up.
1071 i = pwc_set_video_mode(pdev, pwc_image_sizes[pdev->vsize].x, pwc_image_sizes[pdev->vsize].y, pdev->vframes, pdev->vcompression, 0);
1073 Trace(TRACE_OPEN, "First attempt at set_video_mode failed.\n");
1074 if (pdev->type == 730 || pdev->type == 740 || pdev->type == 750)
1075 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QSIF].x, pwc_image_sizes[PSZ_QSIF].y, 10, pdev->vcompression, 0);
1077 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QCIF].x, pwc_image_sizes[PSZ_QCIF].y, 10, pdev->vcompression, 0);
1080 Trace(TRACE_OPEN, "Second attempt at set_video_mode failed.\n");
1085 i = pwc_isoc_init(pdev);
1087 Trace(TRACE_OPEN, "Failed to init ISOC stuff = %d.\n", i);
1093 file->private_data = vdev;
1095 Trace(TRACE_OPEN, "<< video_open() returns 0.\n");
1099 /* Note that all cleanup is done in the reverse order as in _open */
1100 static int pwc_video_close(struct inode *inode, struct file *file)
1102 struct video_device *vdev = file->private_data;
1103 struct pwc_device *pdev;
1106 Trace(TRACE_OPEN, ">> video_close called(vdev = 0x%p).\n", vdev);
1108 pdev = (struct pwc_device *)vdev->priv;
1109 if (pdev->vopen == 0)
1110 Info("video_close() called on closed device?\n");
1112 /* Dump statistics, but only if a reasonable amount of frames were
1113 processed (to prevent endless log-entries in case of snap-shot
1116 if (pdev->vframe_count > 20)
1117 Info("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev->vframe_count, pdev->vframes_dumped, pdev->vframes_error);
1128 /* pwc_dec23_exit(); *//* Timon & Kiara */
1132 /* pwc_dec1_exit(); */
1136 pwc_isoc_cleanup(pdev);
1137 pwc_free_buffers(pdev);
1139 /* Turn off LEDS and power down camera, but only when not unplugged */
1140 if (pdev->error_status != EPIPE) {
1142 if (pwc_set_leds(pdev, 0, 0) < 0)
1143 Info("Failed to set LED on/off time.\n");
1145 i = pwc_camera_power(pdev, 0);
1147 Err("Failed to power down camera (%d)\n", i);
1151 Trace(TRACE_OPEN, "<< video_close()\n");
1156 * FIXME: what about two parallel reads ????
1157 * ANSWER: Not supported. You can't open the device more than once,
1158 despite what the V4L1 interface says. First, I don't see
1159 the need, second there's no mechanism of alerting the
1160 2nd/3rd/... process of events like changing image size.
1161 And I don't see the point of blocking that for the
1162 2nd/3rd/... process.
1163 In multi-threaded environments reading parallel from any
1164 device is tricky anyhow.
1167 static ssize_t pwc_video_read(struct file *file, char __user * buf,
1168 size_t count, loff_t *ppos)
1170 struct video_device *vdev = file->private_data;
1171 struct pwc_device *pdev;
1172 int noblock = file->f_flags & O_NONBLOCK;
1173 DECLARE_WAITQUEUE(wait, current);
1176 Trace(TRACE_READ, "video_read(0x%p, %p, %zu) called.\n", vdev, buf, count);
1182 if (pdev->error_status)
1183 return -pdev->error_status; /* Something happened, report what. */
1185 /* In case we're doing partial reads, we don't have to wait for a frame */
1186 if (pdev->image_read_pos == 0) {
1187 /* Do wait queueing according to the (doc)book */
1188 add_wait_queue(&pdev->frameq, &wait);
1189 while (pdev->full_frames == NULL) {
1190 /* Check for unplugged/etc. here */
1191 if (pdev->error_status) {
1192 remove_wait_queue(&pdev->frameq, &wait);
1193 set_current_state(TASK_RUNNING);
1194 return -pdev->error_status ;
1197 remove_wait_queue(&pdev->frameq, &wait);
1198 set_current_state(TASK_RUNNING);
1199 return -EWOULDBLOCK;
1201 if (signal_pending(current)) {
1202 remove_wait_queue(&pdev->frameq, &wait);
1203 set_current_state(TASK_RUNNING);
1204 return -ERESTARTSYS;
1207 set_current_state(TASK_INTERRUPTIBLE);
1209 remove_wait_queue(&pdev->frameq, &wait);
1210 set_current_state(TASK_RUNNING);
1212 /* Decompress and release frame */
1213 if (pwc_handle_frame(pdev))
1217 Trace(TRACE_READ, "Copying data to user space.\n");
1218 if (pdev->vpalette == VIDEO_PALETTE_RAW)
1219 bytes_to_read = pdev->frame_size;
1221 bytes_to_read = pdev->view.size;
1223 /* copy bytes to user space; we allow for partial reads */
1224 if (count + pdev->image_read_pos > bytes_to_read)
1225 count = bytes_to_read - pdev->image_read_pos;
1226 if (copy_to_user(buf, pdev->image_ptr[pdev->fill_image] + pdev->image_read_pos, count))
1228 pdev->image_read_pos += count;
1229 if (pdev->image_read_pos >= bytes_to_read) { /* All data has been read */
1230 pdev->image_read_pos = 0;
1231 pwc_next_image(pdev);
1236 static unsigned int pwc_video_poll(struct file *file, poll_table *wait)
1238 struct video_device *vdev = file->private_data;
1239 struct pwc_device *pdev;
1247 poll_wait(file, &pdev->frameq, wait);
1248 if (pdev->error_status)
1250 if (pdev->full_frames != NULL) /* we have frames waiting */
1251 return (POLLIN | POLLRDNORM);
1256 static int pwc_video_do_ioctl(struct inode *inode, struct file *file,
1257 unsigned int cmd, void *arg)
1259 struct video_device *vdev = file->private_data;
1260 struct pwc_device *pdev;
1261 DECLARE_WAITQUEUE(wait, current);
1270 /* Query cabapilities */
1273 struct video_capability *caps = arg;
1275 strcpy(caps->name, vdev->name);
1276 caps->type = VID_TYPE_CAPTURE;
1279 caps->minwidth = pdev->view_min.x;
1280 caps->minheight = pdev->view_min.y;
1281 caps->maxwidth = pdev->view_max.x;
1282 caps->maxheight = pdev->view_max.y;
1286 /* Channel functions (simulate 1 channel) */
1289 struct video_channel *v = arg;
1291 if (v->channel != 0)
1295 v->type = VIDEO_TYPE_CAMERA;
1296 strcpy(v->name, "Webcam");
1302 /* The spec says the argument is an integer, but
1303 the bttv driver uses a video_channel arg, which
1304 makes sense becasue it also has the norm flag.
1306 struct video_channel *v = arg;
1307 if (v->channel != 0)
1313 /* Picture functions; contrast etc. */
1316 struct video_picture *p = arg;
1319 val = pwc_get_brightness(pdev);
1321 p->brightness = val;
1323 p->brightness = 0xffff;
1324 val = pwc_get_contrast(pdev);
1328 p->contrast = 0xffff;
1329 /* Gamma, Whiteness, what's the difference? :) */
1330 val = pwc_get_gamma(pdev);
1334 p->whiteness = 0xffff;
1335 val = pwc_get_saturation(pdev);
1341 p->palette = pdev->vpalette;
1342 p->hue = 0xFFFF; /* N/A */
1348 struct video_picture *p = arg;
1350 * FIXME: Suppose we are mid read
1351 ANSWER: No problem: the firmware of the camera
1352 can handle brightness/contrast/etc
1353 changes at _any_ time, and the palette
1354 is used exactly once in the uncompress
1357 pwc_set_brightness(pdev, p->brightness);
1358 pwc_set_contrast(pdev, p->contrast);
1359 pwc_set_gamma(pdev, p->whiteness);
1360 pwc_set_saturation(pdev, p->colour);
1361 if (p->palette && p->palette != pdev->vpalette) {
1362 switch (p->palette) {
1363 case VIDEO_PALETTE_YUV420P:
1364 case VIDEO_PALETTE_RAW:
1365 pdev->vpalette = p->palette;
1366 return pwc_try_video_mode(pdev, pdev->image.x, pdev->image.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1376 /* Window/size parameters */
1379 struct video_window *vw = arg;
1383 vw->width = pdev->view.x;
1384 vw->height = pdev->view.y;
1386 vw->flags = (pdev->vframes << PWC_FPS_SHIFT) |
1387 (pdev->vsnapshot ? PWC_FPS_SNAPSHOT : 0);
1393 struct video_window *vw = arg;
1394 int fps, snapshot, ret;
1396 fps = (vw->flags & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
1397 snapshot = vw->flags & PWC_FPS_SNAPSHOT;
1399 fps = pdev->vframes;
1400 if (pdev->view.x == vw->width && pdev->view.y && fps == pdev->vframes && snapshot == pdev->vsnapshot)
1402 ret = pwc_try_video_mode(pdev, vw->width, vw->height, fps, pdev->vcompression, snapshot);
1408 /* We don't have overlay support (yet) */
1411 struct video_buffer *vb = arg;
1413 memset(vb,0,sizeof(*vb));
1417 /* mmap() functions */
1420 /* Tell the user program how much memory is needed for a mmap() */
1421 struct video_mbuf *vm = arg;
1424 memset(vm, 0, sizeof(*vm));
1425 vm->size = default_mbufs * pdev->len_per_image;
1426 vm->frames = default_mbufs; /* double buffering should be enough for most applications */
1427 for (i = 0; i < default_mbufs; i++)
1428 vm->offsets[i] = i * pdev->len_per_image;
1432 case VIDIOCMCAPTURE:
1434 /* Start capture into a given image buffer (called 'frame' in video_mmap structure) */
1435 struct video_mmap *vm = arg;
1437 Trace(TRACE_READ, "VIDIOCMCAPTURE: %dx%d, frame %d, format %d\n", vm->width, vm->height, vm->frame, vm->format);
1438 if (vm->frame < 0 || vm->frame >= default_mbufs)
1441 /* xawtv is nasty. It probes the available palettes
1442 by setting a very small image size and trying
1443 various palettes... The driver doesn't support
1444 such small images, so I'm working around it.
1450 case VIDEO_PALETTE_YUV420P:
1451 case VIDEO_PALETTE_RAW:
1459 if ((vm->width != pdev->view.x || vm->height != pdev->view.y) &&
1460 (vm->width >= pdev->view_min.x && vm->height >= pdev->view_min.y)) {
1463 Trace(TRACE_OPEN, "VIDIOCMCAPTURE: changing size to please xawtv :-(.\n");
1464 ret = pwc_try_video_mode(pdev, vm->width, vm->height, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1467 } /* ... size mismatch */
1469 /* FIXME: should we lock here? */
1470 if (pdev->image_used[vm->frame])
1471 return -EBUSY; /* buffer wasn't available. Bummer */
1472 pdev->image_used[vm->frame] = 1;
1474 /* Okay, we're done here. In the SYNC call we wait until a
1475 frame comes available, then expand image into the given
1477 In contrast to the CPiA cam the Philips cams deliver a
1478 constant stream, almost like a grabber card. Also,
1479 we have separate buffers for the rawdata and the image,
1480 meaning we can nearly always expand into the requested buffer.
1482 Trace(TRACE_READ, "VIDIOCMCAPTURE done.\n");
1488 /* The doc says: "Whenever a buffer is used it should
1489 call VIDIOCSYNC to free this frame up and continue."
1491 The only odd thing about this whole procedure is
1492 that MCAPTURE flags the buffer as "in use", and
1493 SYNC immediately unmarks it, while it isn't
1494 after SYNC that you know that the buffer actually
1495 got filled! So you better not start a CAPTURE in
1496 the same frame immediately (use double buffering).
1497 This is not a problem for this cam, since it has
1498 extra intermediate buffers, but a hardware
1499 grabber card will then overwrite the buffer
1505 Trace(TRACE_READ, "VIDIOCSYNC called (%d).\n", *mbuf);
1508 if (*mbuf < 0 || *mbuf >= default_mbufs)
1510 /* check if this buffer was requested anyway */
1511 if (pdev->image_used[*mbuf] == 0)
1514 /* Add ourselves to the frame wait-queue.
1516 FIXME: needs auditing for safety.
1517 QUESTION: In what respect? I think that using the
1520 add_wait_queue(&pdev->frameq, &wait);
1521 while (pdev->full_frames == NULL) {
1522 if (pdev->error_status) {
1523 remove_wait_queue(&pdev->frameq, &wait);
1524 set_current_state(TASK_RUNNING);
1525 return -pdev->error_status;
1528 if (signal_pending(current)) {
1529 remove_wait_queue(&pdev->frameq, &wait);
1530 set_current_state(TASK_RUNNING);
1531 return -ERESTARTSYS;
1534 set_current_state(TASK_INTERRUPTIBLE);
1536 remove_wait_queue(&pdev->frameq, &wait);
1537 set_current_state(TASK_RUNNING);
1539 /* The frame is ready. Expand in the image buffer
1540 requested by the user. I don't care if you
1541 mmap() 5 buffers and request data in this order:
1542 buffer 4 2 3 0 1 2 3 0 4 3 1 . . .
1543 Grabber hardware may not be so forgiving.
1545 Trace(TRACE_READ, "VIDIOCSYNC: frame ready.\n");
1546 pdev->fill_image = *mbuf; /* tell in which buffer we want the image to be expanded */
1547 /* Decompress, etc */
1548 ret = pwc_handle_frame(pdev);
1549 pdev->image_used[*mbuf] = 0;
1557 struct video_audio *v = arg;
1559 strcpy(v->name, "Microphone");
1560 v->audio = -1; /* unknown audio minor */
1562 v->mode = VIDEO_SOUND_MONO;
1566 v->balance = 0x8000;
1573 /* Dummy: nothing can be set */
1579 struct video_unit *vu = arg;
1581 vu->video = pdev->vdev->minor & 0x3F;
1582 vu->audio = -1; /* not known yet */
1589 return pwc_ioctl(pdev, cmd, arg);
1594 static int pwc_video_ioctl(struct inode *inode, struct file *file,
1595 unsigned int cmd, unsigned long arg)
1597 return video_usercopy(inode, file, cmd, arg, pwc_video_do_ioctl);
1601 static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma)
1603 struct video_device *vdev = file->private_data;
1604 struct pwc_device *pdev;
1605 unsigned long start = vma->vm_start;
1606 unsigned long size = vma->vm_end-vma->vm_start;
1607 unsigned long page, pos;
1609 Trace(TRACE_MEMORY, "mmap(0x%p, 0x%lx, %lu) called.\n", vdev, start, size);
1612 vma->vm_flags |= VM_IO;
1614 pos = (unsigned long)pdev->image_data;
1616 page = vmalloc_to_pfn((void *)pos);
1617 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1622 if (size > PAGE_SIZE)
1631 /***************************************************************************/
1634 /* This function gets called when a new device is plugged in or the usb core
1638 static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
1640 struct usb_device *udev = interface_to_usbdev(intf);
1641 struct pwc_device *pdev = NULL;
1642 int vendor_id, product_id, type_id;
1645 int video_nr = -1; /* default: use next available device */
1646 char serial_number[30], *name;
1648 /* Check if we can handle this device */
1649 Trace(TRACE_PROBE, "probe() called [%04X %04X], if %d\n",
1650 le16_to_cpu(udev->descriptor.idVendor),
1651 le16_to_cpu(udev->descriptor.idProduct),
1652 intf->altsetting->desc.bInterfaceNumber);
1654 /* the interfaces are probed one by one. We are only interested in the
1655 video interface (0) now.
1656 Interface 1 is the Audio Control, and interface 2 Audio itself.
1658 if (intf->altsetting->desc.bInterfaceNumber > 0)
1661 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
1662 product_id = le16_to_cpu(udev->descriptor.idProduct);
1664 if (vendor_id == 0x0471) {
1665 switch (product_id) {
1667 Info("Philips PCA645VC USB webcam detected.\n");
1668 name = "Philips 645 webcam";
1672 Info("Philips PCA646VC USB webcam detected.\n");
1673 name = "Philips 646 webcam";
1677 Info("Askey VC010 type 2 USB webcam detected.\n");
1678 name = "Askey VC010 webcam";
1682 Info("Philips PCVC675K (Vesta) USB webcam detected.\n");
1683 name = "Philips 675 webcam";
1687 Info("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
1688 name = "Philips 680 webcam";
1692 Info("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
1693 name = "Philips 690 webcam";
1697 Info("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
1698 name = "Philips 730 webcam";
1702 Info("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
1703 name = "Philips 740 webcam";
1707 Info("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
1708 name = "Philips 750 webcam";
1712 Info("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
1713 name = "Philips 720K/40 webcam";
1721 else if (vendor_id == 0x069A) {
1722 switch(product_id) {
1724 Info("Askey VC010 type 1 USB webcam detected.\n");
1725 name = "Askey VC010 webcam";
1733 else if (vendor_id == 0x046d) {
1734 switch(product_id) {
1736 Info("Logitech QuickCam Pro 3000 USB webcam detected.\n");
1737 name = "Logitech QuickCam Pro 3000";
1738 type_id = 740; /* CCD sensor */
1741 Info("Logitech QuickCam Notebook Pro USB webcam detected.\n");
1742 name = "Logitech QuickCam Notebook Pro";
1743 type_id = 740; /* CCD sensor */
1746 Info("Logitech QuickCam 4000 Pro USB webcam detected.\n");
1747 name = "Logitech QuickCam Pro 4000";
1748 type_id = 740; /* CCD sensor */
1751 Info("Logitech QuickCam Zoom USB webcam detected.\n");
1752 name = "Logitech QuickCam Zoom";
1753 type_id = 740; /* CCD sensor */
1756 Info("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
1757 name = "Logitech QuickCam Zoom";
1758 type_id = 740; /* CCD sensor */
1761 Info("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
1762 name = "Logitech QuickCam Orbit";
1763 type_id = 740; /* CCD sensor */
1764 features |= FEATURE_MOTOR_PANTILT;
1769 Info("Logitech QuickCam detected (reserved ID).\n");
1770 name = "Logitech QuickCam (res.)";
1771 type_id = 730; /* Assuming CMOS */
1778 else if (vendor_id == 0x055d) {
1779 /* I don't know the difference between the C10 and the C30;
1780 I suppose the difference is the sensor, but both cameras
1781 work equally well with a type_id of 675
1783 switch(product_id) {
1785 Info("Samsung MPC-C10 USB webcam detected.\n");
1786 name = "Samsung MPC-C10";
1790 Info("Samsung MPC-C30 USB webcam detected.\n");
1791 name = "Samsung MPC-C30";
1799 else if (vendor_id == 0x041e) {
1800 switch(product_id) {
1802 Info("Creative Labs Webcam 5 detected.\n");
1803 name = "Creative Labs Webcam 5";
1807 Info("Creative Labs Webcam Pro Ex detected.\n");
1808 name = "Creative Labs Webcam Pro Ex";
1816 else if (vendor_id == 0x04cc) {
1817 switch(product_id) {
1819 Info("Sotec Afina Eye USB webcam detected.\n");
1820 name = "Sotec Afina Eye";
1828 else if (vendor_id == 0x06be) {
1829 switch(product_id) {
1831 /* This is essentially the same cam as the Sotec Afina Eye */
1832 Info("AME Co. Afina Eye USB webcam detected.\n");
1833 name = "AME Co. Afina Eye";
1842 else if (vendor_id == 0x0d81) {
1843 switch(product_id) {
1845 Info("Visionite VCS-UC300 USB webcam detected.\n");
1846 name = "Visionite VCS-UC300";
1847 type_id = 740; /* CCD sensor */
1850 Info("Visionite VCS-UM100 USB webcam detected.\n");
1851 name = "Visionite VCS-UM100";
1852 type_id = 730; /* CMOS sensor */
1860 return -ENODEV; /* Not any of the know types; but the list keeps growing. */
1862 memset(serial_number, 0, 30);
1863 usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
1864 Trace(TRACE_PROBE, "Device serial number is %s\n", serial_number);
1866 if (udev->descriptor.bNumConfigurations > 1)
1867 Info("Warning: more than 1 configuration available.\n");
1869 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1870 pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
1872 Err("Oops, could not allocate memory for pwc_device.\n");
1875 pdev->type = type_id;
1876 pdev->vsize = default_size;
1877 pdev->vframes = default_fps;
1878 strcpy(pdev->serial, serial_number);
1879 pdev->features = features;
1880 if (vendor_id == 0x046D && product_id == 0x08B5)
1882 /* Logitech QuickCam Orbit
1883 The ranges have been determined experimentally; they may differ from cam to cam.
1884 Also, the exact ranges left-right and up-down are different for my cam
1886 pdev->angle_range.pan_min = -7000;
1887 pdev->angle_range.pan_max = 7000;
1888 pdev->angle_range.tilt_min = -3000;
1889 pdev->angle_range.tilt_max = 2500;
1892 init_MUTEX(&pdev->modlock);
1893 spin_lock_init(&pdev->ptrlock);
1896 init_waitqueue_head(&pdev->frameq);
1897 pdev->vcompression = pwc_preferred_compression;
1899 /* Allocate video_device structure */
1900 pdev->vdev = video_device_alloc();
1901 if (pdev->vdev == 0)
1903 Err("Err, cannot allocate video_device struture. Failing probe.");
1907 memcpy(pdev->vdev, &pwc_template, sizeof(pwc_template));
1908 strcpy(pdev->vdev->name, name);
1909 pdev->vdev->owner = THIS_MODULE;
1910 video_set_drvdata(pdev->vdev, pdev);
1912 pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1913 Trace(TRACE_PROBE, "Release: %04x\n", pdev->release);
1915 /* Now search device_hint[] table for a match, so we can hint a node number. */
1916 for (hint = 0; hint < MAX_DEV_HINTS; hint++) {
1917 if (((device_hint[hint].type == -1) || (device_hint[hint].type == pdev->type)) &&
1918 (device_hint[hint].pdev == NULL)) {
1919 /* so far, so good... try serial number */
1920 if ((device_hint[hint].serial_number[0] == '*') || !strcmp(device_hint[hint].serial_number, serial_number)) {
1922 video_nr = device_hint[hint].device_node;
1923 Trace(TRACE_PROBE, "Found hint, will try to register as /dev/video%d\n", video_nr);
1929 pdev->vdev->release = video_device_release;
1930 i = video_register_device(pdev->vdev, VFL_TYPE_GRABBER, video_nr);
1932 Err("Failed to register as video device (%d).\n", i);
1933 video_device_release(pdev->vdev); /* Drip... drip... drip... */
1934 kfree(pdev); /* Oops, no memory leaks please */
1938 Info("Registered as /dev/video%d.\n", pdev->vdev->minor & 0x3F);
1942 if (hint < MAX_DEV_HINTS)
1943 device_hint[hint].pdev = pdev;
1945 Trace(TRACE_PROBE, "probe() function returning struct at 0x%p.\n", pdev);
1946 usb_set_intfdata (intf, pdev);
1950 /* The user janked out the cable... */
1951 static void usb_pwc_disconnect(struct usb_interface *intf)
1953 struct pwc_device *pdev;
1957 pdev = usb_get_intfdata (intf);
1958 usb_set_intfdata (intf, NULL);
1960 Err("pwc_disconnect() Called without private pointer.\n");
1961 goto disconnect_out;
1963 if (pdev->udev == NULL) {
1964 Err("pwc_disconnect() already called for %p\n", pdev);
1965 goto disconnect_out;
1967 if (pdev->udev != interface_to_usbdev(intf)) {
1968 Err("pwc_disconnect() Woops: pointer mismatch udev/pdev.\n");
1969 goto disconnect_out;
1972 if (pdev->magic != PWC_MAGIC) {
1973 Err("pwc_disconnect() Magic number failed. Consult your scrolls and try again.\n");
1974 goto disconnect_out;
1978 /* We got unplugged; this is signalled by an EPIPE error code */
1980 Info("Disconnected while webcam is in use!\n");
1981 pdev->error_status = EPIPE;
1984 /* Alert waiting processes */
1985 wake_up_interruptible(&pdev->frameq);
1986 /* Wait until device is closed */
1989 /* Device is now closed, so we can safely unregister it */
1990 Trace(TRACE_PROBE, "Unregistering video device in disconnect().\n");
1991 video_unregister_device(pdev->vdev);
1993 /* Free memory (don't set pdev to 0 just yet) */
1997 /* search device_hint[] table if we occupy a slot, by any chance */
1998 for (hint = 0; hint < MAX_DEV_HINTS; hint++)
1999 if (device_hint[hint].pdev == pdev)
2000 device_hint[hint].pdev = NULL;
2006 /* *grunt* We have to do atoi ourselves :-( */
2007 static int pwc_atoi(const char *s)
2012 while (*s != '\0' && *s >= '0' && *s <= '9') {
2013 k = 10 * k + (*s - '0');
2021 * Initialization code & module stuff
2024 static char size[10];
2026 static int fbufs = 0;
2027 static int mbufs = 0;
2028 static int trace = -1;
2029 static int compression = -1;
2030 static int leds[2] = { -1, -1 };
2031 static char *dev_hint[MAX_DEV_HINTS] = { };
2033 module_param_string(size, size, sizeof(size), 0);
2034 MODULE_PARM_DESC(size, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
2035 module_param(fps, int, 0000);
2036 MODULE_PARM_DESC(fps, "Initial frames per second. Varies with model, useful range 5-30");
2037 module_param(fbufs, int, 0000);
2038 MODULE_PARM_DESC(fbufs, "Number of internal frame buffers to reserve");
2039 module_param(mbufs, int, 0000);
2040 MODULE_PARM_DESC(mbufs, "Number of external (mmap()ed) image buffers");
2041 module_param(trace, int, 0000);
2042 MODULE_PARM_DESC(trace, "For debugging purposes");
2043 module_param(power_save, bool, 0000);
2044 MODULE_PARM_DESC(power_save, "Turn power save feature in camera on or off");
2045 module_param(compression, int, 0000);
2046 MODULE_PARM_DESC(compression, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
2047 module_param_array(leds, int, NULL, 0000);
2048 MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
2049 module_param_array(dev_hint, charp, NULL, 0000);
2050 MODULE_PARM_DESC(dev_hint, "Device node hints");
2052 MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
2053 MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
2054 MODULE_LICENSE("GPL");
2056 static int __init usb_pwc_init(void)
2059 char *sizenames[PSZ_MAX] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };
2061 Info("Philips webcam module version " PWC_VERSION " loaded.\n");
2062 Info("Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.\n");
2063 Info("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
2064 Info("the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
2067 if (fps < 4 || fps > 30) {
2068 Err("Framerate out of bounds (4-30).\n");
2072 Info("Default framerate set to %d.\n", default_fps);
2076 /* string; try matching with array */
2077 for (sz = 0; sz < PSZ_MAX; sz++) {
2078 if (!strcmp(sizenames[sz], size)) { /* Found! */
2083 if (sz == PSZ_MAX) {
2084 Err("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
2087 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);
2090 if (mbufs < 1 || mbufs > MAX_IMAGES) {
2091 Err("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES);
2094 default_mbufs = mbufs;
2095 Info("Number of image buffers set to %d.\n", default_mbufs);
2098 if (fbufs < 2 || fbufs > MAX_FRAMES) {
2099 Err("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES);
2102 default_fbufs = fbufs;
2103 Info("Number of frame buffers set to %d.\n", default_fbufs);
2106 Info("Trace options: 0x%04x\n", trace);
2109 if (compression >= 0) {
2110 if (compression > 3) {
2111 Err("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
2114 pwc_preferred_compression = compression;
2115 Info("Preferred compression set to %d.\n", pwc_preferred_compression);
2118 Info("Enabling power save on open/close.\n");
2124 /* Big device node whoopla. Basically, it allows you to assign a
2125 device node (/dev/videoX) to a camera, based on its type
2126 & serial number. The format is [type[.serialnumber]:]node.
2128 Any camera that isn't matched by these rules gets the next
2129 available free device node.
2131 for (i = 0; i < MAX_DEV_HINTS; i++) {
2132 char *s, *colon, *dot;
2134 /* This loop also initializes the array */
2135 device_hint[i].pdev = NULL;
2137 if (s != NULL && *s != '\0') {
2138 device_hint[i].type = -1; /* wildcard */
2139 strcpy(device_hint[i].serial_number, "*");
2141 /* parse string: chop at ':' & '/' */
2143 while (*colon != '\0' && *colon != ':')
2145 while (*dot != '\0' && *dot != '.')
2147 /* Few sanity checks */
2148 if (*dot != '\0' && dot > colon) {
2149 Err("Malformed camera hint: the colon must be after the dot.\n");
2153 if (*colon == '\0') {
2156 Err("Malformed camera hint: no colon + device node given.\n");
2160 /* No type or serial number specified, just a number. */
2161 device_hint[i].device_node = pwc_atoi(s);
2165 /* There's a colon, so we have at least a type and a device node */
2166 device_hint[i].type = pwc_atoi(s);
2167 device_hint[i].device_node = pwc_atoi(colon + 1);
2169 /* There's a serial number as well */
2174 while (*dot != ':' && k < 29) {
2175 device_hint[i].serial_number[k++] = *dot;
2178 device_hint[i].serial_number[k] = '\0';
2182 Debug("device_hint[%d]:\n", i);
2183 Debug(" type : %d\n", device_hint[i].type);
2184 Debug(" serial# : %s\n", device_hint[i].serial_number);
2185 Debug(" node : %d\n", device_hint[i].device_node);
2189 device_hint[i].type = 0; /* not filled */
2190 } /* ..for MAX_DEV_HINTS */
2192 Trace(TRACE_PROBE, "Registering driver at address 0x%p.\n", &pwc_driver);
2193 return usb_register(&pwc_driver);
2196 static void __exit usb_pwc_exit(void)
2198 Trace(TRACE_MODULE, "Deregistering driver.\n");
2199 usb_deregister(&pwc_driver);
2200 Info("Philips webcam module removed.\n");
2203 module_init(usb_pwc_init);
2204 module_exit(usb_pwc_exit);