1 /* Linux driver for Philips webcam
2 USB and Video4Linux interface part.
3 (C) 1999-2003 Nemosoft Unv.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 This code forms the interface between the USB layers and the Philips
23 specific stuff. Some adanved stuff of the driver falls under an
24 NDA, signed between me and Philips B.V., Eindhoven, the Netherlands, and
25 is thus not distributed in source form. The binary pwcx.o module
26 contains the code that falls under the NDA.
28 In case you're wondering: 'pwc' stands for "Philips WebCam", but
29 I really didn't want to type 'philips_web_cam' every time (I'm lazy as
30 any Linux kernel hacker, but I don't like uncomprehensible abbreviations
33 Oh yes, convention: to disctinguish between all the various pointers to
34 device-structures, I use these names for the pointer variables:
35 udev: struct usb_device *
36 vdev: struct video_device *
37 pdev: struct pwc_devive *
41 - Alvarado: adding whitebalance code
42 - Alistar Moire: QuickCam 3000 Pro device/product ID
43 - Tony Hoyle: Creative Labs Webcam 5 device/product ID
44 - Mark Burazin: solving hang in VIDIOCSYNC when camera gets unplugged
45 - Jk Fang: Sotec Afina Eye ID
46 - Xavier Roche: QuickCam Pro 4000 ID
47 - Jens Knudsen: QuickCam Zoom ID
48 - J. Debert: QuickCam for Notebooks ID
51 #include <linux/errno.h>
52 #include <linux/init.h>
54 #include <linux/module.h>
55 #include <linux/poll.h>
56 #include <linux/slab.h>
57 #include <linux/vmalloc.h>
58 #include <linux/wrapper.h>
62 #include "pwc-ioctl.h"
63 #include "pwc-uncompress.h"
65 /* Function prototypes and driver templates */
67 /* hotplug device table support */
68 static struct usb_device_id pwc_device_table [] = {
69 { USB_DEVICE(0x0471, 0x0302) }, /* Philips models */
70 { USB_DEVICE(0x0471, 0x0303) },
71 { USB_DEVICE(0x0471, 0x0304) },
72 { USB_DEVICE(0x0471, 0x0307) },
73 { USB_DEVICE(0x0471, 0x0308) },
74 { USB_DEVICE(0x0471, 0x030C) },
75 { USB_DEVICE(0x0471, 0x0310) },
76 { USB_DEVICE(0x0471, 0x0311) },
77 { USB_DEVICE(0x0471, 0x0312) },
78 { USB_DEVICE(0x0471, 0x0313) }, /* the 'new' 720K */
79 { USB_DEVICE(0x069A, 0x0001) }, /* Askey */
80 { USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam Pro 3000 */
81 { USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
82 { USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam Pro 4000 */
83 { USB_DEVICE(0x046D, 0x08B3) }, /* Logitech QuickCam Zoom */
84 { USB_DEVICE(0x046D, 0x08B4) }, /* Logitech (reserved) */
85 { USB_DEVICE(0x046D, 0x08B5) }, /* Logitech (reserved) */
86 { USB_DEVICE(0x046D, 0x08B6) }, /* Logitech (reserved) */
87 { USB_DEVICE(0x046D, 0x08B7) }, /* Logitech (reserved) */
88 { USB_DEVICE(0x046D, 0x08B8) }, /* Logitech (reserved) */
89 { USB_DEVICE(0x055D, 0x9000) }, /* Samsung */
90 { USB_DEVICE(0x055D, 0x9001) },
91 { USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
92 { USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
93 { USB_DEVICE(0x04CC, 0x8116) }, /* Afina Eye */
94 { USB_DEVICE(0x0d81, 0x1910) }, /* Visionite */
95 { USB_DEVICE(0x0d81, 0x1900) },
98 MODULE_DEVICE_TABLE(usb, pwc_device_table);
100 static void *usb_pwc_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id);
101 static void usb_pwc_disconnect(struct usb_device *udev, void *ptr);
103 static struct usb_driver pwc_driver =
105 name: "Philips webcam", /* name */
106 id_table: pwc_device_table,
107 probe: usb_pwc_probe, /* probe() */
108 disconnect: usb_pwc_disconnect, /* disconnect() */
111 #define MAX_DEV_HINTS 20
112 #define MAX_ISOC_ERRORS 20
114 static int default_size = PSZ_QCIF;
115 static int default_fps = 10;
116 static int default_fbufs = 3; /* Default number of frame buffers */
117 static int default_mbufs = 2; /* Default number of mmap() buffers */
118 int pwc_trace = TRACE_MODULE | TRACE_FLOW | TRACE_PWCX;
119 static int power_save = 0;
120 static int led_on = 100, led_off = 0; /* defaults to LED that is on while in use */
121 int pwc_preferred_compression = 2; /* 0..3 = uncompressed..high */
124 char serial_number[30];
126 struct pwc_device *pdev;
127 } device_hint[MAX_DEV_HINTS];
131 static int pwc_video_open(struct video_device *vdev, int mode);
132 static void pwc_video_close(struct video_device *vdev);
133 static long pwc_video_read(struct video_device *vdev, char *buf, unsigned long count, int noblock);
134 static long pwc_video_write(struct video_device *vdev, const char *buf, unsigned long count, int noblock);
135 static unsigned int pwc_video_poll(struct video_device *vdev, struct file *file, poll_table *wait);
136 static int pwc_video_ioctl(struct video_device *vdev, unsigned int cmd, void *arg);
137 static int pwc_video_mmap(struct video_device *vdev, const char *adr, unsigned long size);
139 static struct video_device pwc_template = {
141 name: "Philips Webcam", /* Filled in later */
142 type: VID_TYPE_CAPTURE,
143 hardware: VID_HARDWARE_PWC,
144 open: pwc_video_open,
145 close: pwc_video_close,
146 read: pwc_video_read,
147 write: pwc_video_write,
148 poll: pwc_video_poll,
149 ioctl: pwc_video_ioctl,
150 mmap: pwc_video_mmap,
151 initialize: NULL, /* initialize */
155 /***************************************************************************/
157 /* Okay, this is some magic that I worked out and the reasoning behind it...
159 The biggest problem with any USB device is of course: "what to do
160 when the user unplugs the device while it is in use by an application?"
161 We have several options:
162 1) Curse them with the 7 plagues when they do (requires divine intervention)
163 2) Tell them not to (won't work: they'll do it anyway)
164 3) Oops the kernel (this will have a negative effect on a user's uptime)
165 4) Do something sensible.
167 Of course, we go for option 4.
169 It happens that this device will be linked to two times, once from
170 usb_device and once from the video_device in their respective 'private'
171 pointers. This is done when the device is probed() and all initialization
172 succeeded. The pwc_device struct links back to both structures.
174 When a device is unplugged while in use it will be removed from the
175 list of known USB devices; I also de-register it as a V4L device, but
176 unfortunately I can't free the memory since the struct is still in use
177 by the file descriptor. This free-ing is then deferend until the first
178 opportunity. Crude, but it works.
180 A small 'advantage' is that if a user unplugs the cam and plugs it back
181 in, it should get assigned the same video device minor, but unfortunately
182 it's non-trivial to re-link the cam back to the video device... (that
183 would surely be magic! :))
186 /***************************************************************************/
187 /* Private functions */
189 /* Here we want the physical address of the memory.
190 * This is used when initializing the contents of the area.
192 static inline unsigned long kvirt_to_pa(unsigned long adr)
194 unsigned long kva, ret;
196 kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
197 kva |= adr & (PAGE_SIZE-1); /* restore the offset */
202 static void * rvmalloc(unsigned long size)
207 size=PAGE_ALIGN(size);
208 mem=vmalloc_32(size);
211 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
212 adr=(unsigned long) mem;
215 mem_map_reserve(vmalloc_to_page((void *)adr));
223 static void rvfree(void * mem, unsigned long size)
229 adr=(unsigned long) mem;
230 while ((long) size > 0)
232 mem_map_unreserve(vmalloc_to_page((void *)adr));
243 static int pwc_allocate_buffers(struct pwc_device *pdev)
248 Trace(TRACE_MEMORY, ">> pwc_allocate_buffers(pdev = 0x%p)\n", pdev);
254 if (pdev->magic != PWC_MAGIC) {
255 Err("allocate_buffers(): magic failed.\n");
259 /* Allocate Isochronuous pipe buffers */
260 for (i = 0; i < MAX_ISO_BUFS; i++) {
261 if (pdev->sbuf[i].data == NULL) {
262 kbuf = kmalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
264 Err("Failed to allocate iso buffer %d.\n", i);
267 Trace(TRACE_MEMORY, "Allocated iso buffer at %p.\n", kbuf);
268 pdev->sbuf[i].data = kbuf;
269 memset(kbuf, 0, ISO_BUFFER_SIZE);
273 /* Allocate frame buffer structure */
274 if (pdev->fbuf == NULL) {
275 kbuf = kmalloc(default_fbufs * sizeof(struct pwc_frame_buf), GFP_KERNEL);
277 Err("Failed to allocate frame buffer structure.\n");
280 Trace(TRACE_MEMORY, "Allocated frame buffer structure at %p.\n", kbuf);
282 memset(kbuf, 0, default_fbufs * sizeof(struct pwc_frame_buf));
284 /* create frame buffers, and make circular ring */
285 for (i = 0; i < default_fbufs; i++) {
286 if (pdev->fbuf[i].data == NULL) {
287 kbuf = vmalloc(PWC_FRAME_SIZE); /* need vmalloc since frame buffer > 128K */
289 Err("Failed to allocate frame buffer %d.\n", i);
292 Trace(TRACE_MEMORY, "Allocated frame buffer %d at %p.\n", i, kbuf);
293 pdev->fbuf[i].data = kbuf;
294 memset(kbuf, 128, PWC_FRAME_SIZE);
298 /* Allocate decompressor table space */
300 if (pdev->decompressor != NULL) {
301 kbuf = kmalloc(pdev->decompressor->table_size, GFP_KERNEL);
303 Err("Failed to allocate decompress table.\n");
306 Trace(TRACE_MEMORY, "Allocated decompress table %p.\n", kbuf);
308 pdev->decompress_data = kbuf;
310 /* Allocate image buffer; double buffer for mmap() */
311 kbuf = rvmalloc(default_mbufs * pdev->len_per_image);
313 Err("Failed to allocate image buffer(s).\n");
316 Trace(TRACE_MEMORY, "Allocated image buffer at %p.\n", kbuf);
317 pdev->image_data = kbuf;
318 for (i = 0; i < default_mbufs; i++)
319 pdev->image_ptr[i] = kbuf + i * pdev->len_per_image;
320 for (; i < MAX_IMAGES; i++)
321 pdev->image_ptr[i] = NULL;
325 Trace(TRACE_MEMORY, "<< pwc_allocate_buffers()\n");
329 static void pwc_free_buffers(struct pwc_device *pdev)
333 Trace(TRACE_MEMORY, "Entering free_buffers(%p).\n", pdev);
338 if (pdev->magic != PWC_MAGIC) {
339 Err("free_buffers(): magic failed.\n");
344 /* Release Iso-pipe buffers */
345 for (i = 0; i < MAX_ISO_BUFS; i++)
346 if (pdev->sbuf[i].data != NULL) {
347 Trace(TRACE_MEMORY, "Freeing ISO buffer at %p.\n", pdev->sbuf[i].data);
348 kfree(pdev->sbuf[i].data);
349 pdev->sbuf[i].data = NULL;
352 /* The same for frame buffers */
353 if (pdev->fbuf != NULL) {
354 for (i = 0; i < default_fbufs; i++) {
355 if (pdev->fbuf[i].data != NULL) {
356 Trace(TRACE_MEMORY, "Freeing frame buffer %d at %p.\n", i, pdev->fbuf[i].data);
357 vfree(pdev->fbuf[i].data);
358 pdev->fbuf[i].data = NULL;
365 /* Intermediate decompression buffer & tables */
366 if (pdev->decompress_data != NULL) {
367 Trace(TRACE_MEMORY, "Freeing decompression buffer at %p.\n", pdev->decompress_data);
368 kfree(pdev->decompress_data);
369 pdev->decompress_data = NULL;
371 pdev->decompressor = NULL;
373 /* Release image buffers */
374 if (pdev->image_data != NULL) {
375 Trace(TRACE_MEMORY, "Freeing image buffer at %p.\n", pdev->image_data);
376 rvfree(pdev->image_data, default_mbufs * pdev->len_per_image);
378 pdev->image_data = NULL;
380 Trace(TRACE_MEMORY, "Leaving free_buffers().\n");
383 /* The frame & image buffer mess.
385 Yes, this is a mess. Well, it used to be simple, but alas... In this
386 module, 3 buffers schemes are used to get the data from the USB bus to
387 the user program. The first scheme involves the ISO buffers (called thus
388 since they transport ISO data from the USB controller), and not really
389 interesting. Suffices to say the data from this buffer is quickly
390 gathered in an interrupt handler (pwc_isoc_handler) and placed into the
393 The frame buffer is the second scheme, and is the central element here.
394 It collects the data from a single frame from the camera (hence, the
395 name). Frames are delimited by the USB camera with a short USB packet,
396 so that's easy to detect. The frame buffers form a list that is filled
397 by the camera+USB controller and drained by the user process through
398 either read() or mmap().
400 The image buffer is the third scheme, in which frames are decompressed
401 and converted into planar format. For mmap() there is more than
402 one image buffer available.
404 The frame buffers provide the image buffering. In case the user process
405 is a bit slow, this introduces lag and some undesired side-effects.
406 The problem arises when the frame buffer is full. I used to drop the last
407 frame, which makes the data in the queue stale very quickly. But dropping
408 the frame at the head of the queue proved to be a litte bit more difficult.
409 I tried a circular linked scheme, but this introduced more problems than
412 Because filling and draining are completely asynchronous processes, this
413 requires some fiddling with pointers and mutexes.
415 Eventually, I came up with a system with 2 lists: an 'empty' frame list
416 and a 'full' frame list:
417 * Initially, all frame buffers but one are on the 'empty' list; the one
418 remaining buffer is our initial fill frame.
419 * If a frame is needed for filling, we try to take it from the 'empty'
420 list, unless that list is empty, in which case we take the buffer at
421 the head of the 'full' list.
422 * When our fill buffer has been filled, it is appended to the 'full'
424 * If a frame is needed by read() or mmap(), it is taken from the head of
425 the 'full' list, handled, and then appended to the 'empty' list. If no
426 buffer is present on the 'full' list, we wait.
427 The advantage is that the buffer that is currently being decompressed/
428 converted, is on neither list, and thus not in our way (any other scheme
429 I tried had the problem of old data lingering in the queue).
431 Whatever strategy you choose, it always remains a tradeoff: with more
432 frame buffers the chances of a missed frame are reduced. On the other
433 hand, on slower machines it introduces lag because the queue will
438 \brief Find next frame buffer to fill. Take from empty or full list, whichever comes first.
440 static inline int pwc_next_fill_frame(struct pwc_device *pdev)
446 spin_lock_irqsave(&pdev->ptrlock, flags);
447 if (pdev->fill_frame != NULL) {
448 /* append to 'full' list */
449 if (pdev->full_frames == NULL) {
450 pdev->full_frames = pdev->fill_frame;
451 pdev->full_frames_tail = pdev->full_frames;
454 pdev->full_frames_tail->next = pdev->fill_frame;
455 pdev->full_frames_tail = pdev->fill_frame;
458 if (pdev->empty_frames != NULL) {
459 /* We have empty frames available. That's easy */
460 pdev->fill_frame = pdev->empty_frames;
461 pdev->empty_frames = pdev->empty_frames->next;
464 /* Hmm. Take it from the full list */
467 if (pdev->full_frames == NULL) {
468 Err("Neither empty or full frames available!\n");
469 spin_unlock_irqrestore(&pdev->ptrlock, flags);
473 pdev->fill_frame = pdev->full_frames;
474 pdev->full_frames = pdev->full_frames->next;
477 pdev->fill_frame->next = NULL;
479 Trace(TRACE_SEQUENCE, "Assigning sequence number %d.\n", pdev->sequence);
480 pdev->fill_frame->sequence = pdev->sequence++;
482 spin_unlock_irqrestore(&pdev->ptrlock, flags);
488 \brief Reset all buffers, pointers and lists, except for the image_used[] buffer.
490 If the image_used[] buffer is cleared too, mmap()/VIDIOCSYNC will run into trouble.
492 static void pwc_reset_buffers(struct pwc_device *pdev)
497 spin_lock_irqsave(&pdev->ptrlock, flags);
498 pdev->full_frames = NULL;
499 pdev->full_frames_tail = NULL;
500 for (i = 0; i < default_fbufs; i++) {
501 pdev->fbuf[i].filled = 0;
503 pdev->fbuf[i].next = &pdev->fbuf[i - 1];
505 pdev->fbuf->next = NULL;
507 pdev->empty_frames = &pdev->fbuf[default_fbufs - 1];
508 pdev->empty_frames_tail = pdev->fbuf;
509 pdev->read_frame = NULL;
510 pdev->fill_frame = pdev->empty_frames;
511 pdev->empty_frames = pdev->empty_frames->next;
513 pdev->image_read_pos = 0;
514 pdev->fill_image = 0;
515 spin_unlock_irqrestore(&pdev->ptrlock, flags);
520 \brief Do all the handling for getting one frame: get pointer, decompress, advance pointers.
522 static int pwc_handle_frame(struct pwc_device *pdev)
527 spin_lock_irqsave(&pdev->ptrlock, flags);
528 /* First grab our read_frame; this is removed from all lists, so
529 we can release the lock after this without problems */
530 if (pdev->read_frame != NULL) {
531 /* This can't theoretically happen */
532 Err("Huh? Read frame still in use?\n");
535 if (pdev->full_frames == NULL) {
536 Err("Woops. No frames ready.\n");
539 pdev->read_frame = pdev->full_frames;
540 pdev->full_frames = pdev->full_frames->next;
541 pdev->read_frame->next = NULL;
544 if (pdev->read_frame != NULL) {
546 Trace(TRACE_SEQUENCE, "Decompressing frame %d\n", pdev->read_frame->sequence);
548 /* Decompression is a lenghty process, so it's outside of the lock.
549 This gives the isoc_handler the opportunity to fill more frames
552 spin_unlock_irqrestore(&pdev->ptrlock, flags);
553 ret = pwc_decompress(pdev);
554 spin_lock_irqsave(&pdev->ptrlock, flags);
556 /* We're done with read_buffer, tack it to the end of the empty buffer list */
557 if (pdev->empty_frames == NULL) {
558 pdev->empty_frames = pdev->read_frame;
559 pdev->empty_frames_tail = pdev->empty_frames;
562 pdev->empty_frames_tail->next = pdev->read_frame;
563 pdev->empty_frames_tail = pdev->read_frame;
565 pdev->read_frame = NULL;
568 spin_unlock_irqrestore(&pdev->ptrlock, flags);
573 \brief Advance pointers of image buffer (after each user request)
575 static inline void pwc_next_image(struct pwc_device *pdev)
577 pdev->image_used[pdev->fill_image] = 0;
578 pdev->fill_image = (pdev->fill_image + 1) % default_mbufs;
583 /* This gets called for the Isochronous pipe (video). This is done in
584 * interrupt time, so it has to be fast, not crash, and not stall. Neat.
586 static void pwc_isoc_handler(struct urb *urb)
588 struct pwc_device *pdev;
591 struct pwc_frame_buf *fbuf;
592 unsigned char *fillptr = 0, *iso_buf = 0;
595 pdev = (struct pwc_device *)urb->context;
597 Err("isoc_handler() called with NULL device?!\n");
601 if (pdev->magic != PWC_MAGIC) {
602 Err("isoc_handler() called with bad magic!\n");
606 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
607 Trace(TRACE_OPEN, "pwc_isoc_handler(): URB (%p) unlinked %ssynchronuously.\n", urb, urb->status == -ENOENT ? "" : "a");
610 if (urb->status != -EINPROGRESS && urb->status != 0) {
614 switch(urb->status) {
615 case -ENOSR: errmsg = "Buffer error (overrun)"; break;
616 case -EPIPE: errmsg = "Stalled (device not responding)"; break;
617 case -EOVERFLOW: errmsg = "Babble (bad cable?)"; break;
618 case -EPROTO: errmsg = "Bit-stuff error (bad cable?)"; break;
619 case -EILSEQ: errmsg = "CRC/Timeout (could be anything)"; break;
620 case -ETIMEDOUT: errmsg = "NAK (device does not respond)"; break;
622 Trace(TRACE_FLOW, "pwc_isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
623 /* Give up after a number of contiguous errors on the USB bus.
624 Appearantly something is wrong so we simulate an unplug event.
626 if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
628 Info("Too many ISOC errors, bailing out.\n");
629 pdev->error_status = EIO;
633 return; // better luck next time
636 fbuf = pdev->fill_frame;
638 Err("pwc_isoc_handler without valid fill frame.\n");
642 fillptr = fbuf->data + fbuf->filled;
644 /* Premature wakeup */
646 wake_up_interruptible(&pdev->frameq);
650 /* Reset ISOC error counter. We did get here, after all. */
651 pdev->visoc_errors = 0;
653 /* vsync: 0 = don't copy data
658 for (i = 0; i < urb->number_of_packets; i++) {
659 fst = urb->iso_frame_desc[i].status;
660 flen = urb->iso_frame_desc[i].actual_length;
661 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
663 if (flen > 0) { /* if valid data... */
664 if (pdev->vsync > 0) { /* ...and we are not sync-hunting... */
667 /* ...copy data to frame buffer, if possible */
668 if (flen + fbuf->filled > pdev->frame_size) {
669 Trace(TRACE_FLOW, "Frame buffer overflow (flen = %d, frame_size = %d).\n", flen, pdev->frame_size);
670 pdev->vsync = 0; /* Hmm, let's wait for an EOF (end-of-frame) */
671 pdev->vframes_error++;
674 memmove(fillptr, iso_buf, flen);
678 fbuf->filled += flen;
681 if (flen < pdev->vlast_packet_size) {
682 /* Shorter packet... We probably have the end of an image-frame;
683 wake up read() process and let select()/poll() do something.
684 Decompression is done in user time over there.
686 if (pdev->vsync == 2) {
687 /* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
688 frames on the USB wire after an exposure change. This conditition is
689 however detected in the cam and a bit is set in the header.
691 if (pdev->type == 730) {
692 unsigned char *ptr = (unsigned char *)fbuf->data;
694 if (ptr[1] == 1 && ptr[0] & 0x10) {
696 Debug("Hyundai CMOS sensor bug. Dropping frame %d.\n", fbuf->sequence);
698 pdev->drop_frames += 2;
699 pdev->vframes_error++;
701 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
703 Info("Snapshot button pressed.\n");
705 Info("Snapshot button released.\n");
707 if ((ptr[0] ^ pdev->vmirror) & 0x02) {
709 Info("Image is mirrored.\n");
711 Info("Image is normal.\n");
713 pdev->vmirror = ptr[0] & 0x03;
714 /* Sometimes the trailer of the 730 is still sent as a 4 byte packet
715 after a short frame; this condition is filtered out specifically. A 4 byte
716 frame doesn't make sense anyway.
717 So we get either this sequence:
718 drop_bit set -> 4 byte frame -> short frame -> good frame
720 drop_bit set -> short frame -> good frame
721 So we drop either 3 or 2 frames in all!
723 if (fbuf->filled == 4)
727 /* In case we were instructed to drop the frame, do so silently.
728 The buffer pointers are not updated either (but the counters are reset below).
730 if (pdev->drop_frames > 0)
733 /* Check for underflow first */
734 if (fbuf->filled < pdev->frame_size) {
735 Trace(TRACE_FLOW, "Frame buffer underflow (%d bytes); discarded.\n", fbuf->filled);
736 pdev->vframes_error++;
739 /* Send only once per EOF */
740 awake = 1; /* delay wake_ups */
742 /* Find our next frame to fill. This will always succeed, since we
743 * nick a frame from either empty or full list, but if we had to
744 * take it from the full list, it means a frame got dropped.
746 if (pwc_next_fill_frame(pdev)) {
747 pdev->vframes_dumped++;
748 if ((pdev->vframe_count > FRAME_LOWMARK) && (pwc_trace & TRACE_FLOW)) {
749 if (pdev->vframes_dumped < 20)
750 Trace(TRACE_FLOW, "Dumping frame %d.\n", pdev->vframe_count);
751 if (pdev->vframes_dumped == 20)
752 Trace(TRACE_FLOW, "Dumping frame %d (last message).\n", pdev->vframe_count);
755 fbuf = pdev->fill_frame;
758 pdev->vframe_count++;
761 fillptr = fbuf->data;
763 } /* .. flen < last_packet_size */
764 pdev->vlast_packet_size = flen;
765 } /* ..status == 0 */
767 /* This is normally not interesting to the user, unless you are really debugging something */
769 static int iso_error = 0;
772 Trace(TRACE_FLOW, "Iso frame %d of USB has error %d\n", i, fst);
777 wake_up_interruptible(&pdev->frameq);
781 static int pwc_isoc_init(struct pwc_device *pdev)
783 struct usb_device *udev;
787 struct usb_interface_descriptor *idesc;
796 /* Get the current alternate interface, adjust packet size */
797 if (!udev->actconfig)
799 idesc = &udev->actconfig->interface[0].altsetting[pdev->valternate];
803 /* Search video endpoint */
804 pdev->vmax_packet_size = -1;
805 for (i = 0; i < idesc->bNumEndpoints; i++)
806 if ((idesc->endpoint[i].bEndpointAddress & 0xF) == pdev->vendpoint) {
807 pdev->vmax_packet_size = idesc->endpoint[i].wMaxPacketSize;
811 if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
812 Err("Failed to find packet size for video endpoint in current alternate setting.\n");
813 return -ENFILE; /* Odd error, that should be noticable */
816 /* Set alternate interface */
818 Trace(TRACE_OPEN, "Setting alternate interface %d\n", pdev->valternate);
819 ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
823 for (i = 0; i < MAX_ISO_BUFS; i++) {
824 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC);
826 Err("Failed to allocate urb %d\n", i);
830 pdev->sbuf[i].urb = urb;
831 Trace(TRACE_MEMORY, "Allocated URB at 0x%p\n", urb);
834 /* De-allocate in reverse order */
836 if (pdev->sbuf[i].urb != NULL)
837 usb_free_urb(pdev->sbuf[i].urb);
838 pdev->sbuf[i].urb = NULL;
844 /* init URB structure */
845 for (i = 0; i < MAX_ISO_BUFS; i++) {
846 urb = pdev->sbuf[i].urb;
848 urb->next = pdev->sbuf[(i + 1) % MAX_ISO_BUFS].urb;
850 urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
851 urb->transfer_flags = USB_ISO_ASAP;
852 urb->transfer_buffer = pdev->sbuf[i].data;
853 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
854 urb->complete = pwc_isoc_handler;
856 urb->start_frame = 0;
857 urb->number_of_packets = ISO_FRAMES_PER_DESC;
858 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
859 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
860 urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
865 for (i = 0; i < MAX_ISO_BUFS; i++) {
866 ret = usb_submit_urb(pdev->sbuf[i].urb);
868 Err("isoc_init() submit_urb %d failed with error %d\n", i, ret);
870 Trace(TRACE_OPEN, "URB 0x%p submitted.\n", pdev->sbuf[i].urb);
875 Trace(TRACE_OPEN, "<< pwc_isoc_init()\n");
879 static void pwc_isoc_cleanup(struct pwc_device *pdev)
883 Trace(TRACE_OPEN, ">> pwc_isoc_cleanup()\n");
887 /* Unlinking ISOC buffers one by one */
888 for (i = 0; i < MAX_ISO_BUFS; i++) {
891 urb = pdev->sbuf[i].urb;
893 if (pdev->iso_init) {
894 Trace(TRACE_MEMORY, "Unlinking URB %p\n", urb);
897 Trace(TRACE_MEMORY, "Freeing URB\n");
899 pdev->sbuf[i].urb = NULL;
903 /* Stop camera, but only if we are sure the camera is still there (unplug
904 is signalled by EPIPE)
906 if (pdev->error_status && pdev->error_status != EPIPE) {
907 Trace(TRACE_OPEN, "Setting alternate interface 0.\n");
908 usb_set_interface(pdev->udev, 0, 0);
912 Trace(TRACE_OPEN, "<< pwc_isoc_cleanup()\n");
915 int pwc_try_video_mode(struct pwc_device *pdev, int width, int height, int new_fps, int new_compression, int new_snapshot)
918 /* Stop isoc stuff */
919 pwc_isoc_cleanup(pdev);
920 /* Reset parameters */
921 pwc_reset_buffers(pdev);
922 /* Try to set video mode... */
923 ret = pwc_set_video_mode(pdev, width, height, new_fps, new_compression, new_snapshot);
924 if (ret) /* That failed... restore old mode (we know that worked) */
925 ret = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
927 if (pwc_isoc_init(pdev) < 0)
928 Info("Failed to restart ISOC transfers in pwc_try_video_mode.\n");
929 pdev->drop_frames++; /* try to avoid garbage during switch */
934 /***************************************************************************/
935 /* Video4Linux functions */
937 static int pwc_video_open(struct video_device *vdev, int mode)
940 struct pwc_device *pdev;
942 Trace(TRACE_OPEN, ">> video_open called(vdev = 0x%p).\n", vdev);
946 pdev = (struct pwc_device *)vdev->priv;
952 down(&pdev->modlock);
953 if (!pdev->usb_init) {
954 Trace(TRACE_OPEN, "Doing first time initialization.\n");
958 /* Query sensor type */
959 const char *sensor_type = NULL;
961 i = pwc_get_cmos_sensor(pdev);
963 case -1: /* Unknown, show nothing */; break;
964 case 0x00: sensor_type = "Hyundai CMOS sensor"; break;
965 case 0x20: sensor_type = "Sony CCD sensor + TDA8787"; break;
966 case 0x2E: sensor_type = "Sony CCD sensor + Exas 98L59"; break;
967 case 0x2F: sensor_type = "Sony CCD sensor + ADI 9804"; break;
968 case 0x30: sensor_type = "Sharp CCD sensor + TDA8787"; break;
969 case 0x3E: sensor_type = "Sharp CCD sensor + Exas 98L59"; break;
970 case 0x3F: sensor_type = "Sharp CCD sensor + ADI 9804"; break;
971 case 0x40: sensor_type = "UPA 1021 sensor"; break;
972 case 0x100: sensor_type = "VGA sensor"; break;
973 case 0x101: sensor_type = "PAL MR sensor"; break;
974 default: sensor_type = "unknown type of sensor"; break;
976 if (sensor_type != NULL)
977 Info("This %s camera is equipped with a %s (%d).\n", pdev->vdev.name, sensor_type, i);
983 i = pwc_camera_power(pdev, 1);
985 Info("Failed to restore power to the camera! (%d)\n", i);
987 /* Set LED on/off time */
988 if (pwc_set_leds(pdev, led_on, led_off) < 0)
989 Info("Failed to set LED on/off time.\n");
991 /* Find our decompressor, if any */
992 pdev->decompressor = pwc_find_decompressor(pdev->type);
994 Debug("Found decompressor for %d at 0x%p\n", pdev->type, pdev->decompressor);
997 /* So far, so good. Allocate memory. */
998 i = pwc_allocate_buffers(pdev);
1000 Trace(TRACE_OPEN, "Failed to allocate buffer memory.\n");
1005 /* Reset buffers & parameters */
1006 pwc_reset_buffers(pdev);
1007 for (i = 0; i < default_mbufs; i++)
1008 pdev->image_used[i] = 0;
1009 pdev->vframe_count = 0;
1010 pdev->vframes_dumped = 0;
1011 pdev->vframes_error = 0;
1012 pdev->visoc_errors = 0;
1013 pdev->error_status = 0;
1018 /* Set some defaults */
1019 pdev->vsnapshot = 0;
1021 /* Start iso pipe for video; first try the last used video size
1022 (or the default one); if that fails try QCIF/10 or QSIF/10;
1023 it that fails too, give up.
1025 i = pwc_set_video_mode(pdev, pwc_image_sizes[pdev->vsize].x, pwc_image_sizes[pdev->vsize].y, pdev->vframes, pdev->vcompression, 0);
1027 Trace(TRACE_OPEN, "First attempt at set_video_mode failed.\n");
1028 if (pdev->type == 730 || pdev->type == 740 || pdev->type == 750)
1029 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QSIF].x, pwc_image_sizes[PSZ_QSIF].y, 10, pdev->vcompression, 0);
1031 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QCIF].x, pwc_image_sizes[PSZ_QCIF].y, 10, pdev->vcompression, 0);
1034 Trace(TRACE_OPEN, "Second attempt at set_video_mode failed.\n");
1039 i = pwc_isoc_init(pdev);
1041 Trace(TRACE_OPEN, "Failed to init ISOC stuff = %d.\n", i);
1047 /* lock decompressor; this has a small race condition, since we
1048 could in theory unload pwcx.o between pwc_find_decompressor()
1049 above and this call. I doubt it's ever going to be a problem.
1051 if (pdev->decompressor != NULL)
1052 pdev->decompressor->lock();
1054 Trace(TRACE_OPEN, "<< video_open() returns 0.\n");
1058 /* Note that all cleanup is done in the reverse order as in _open */
1059 static void pwc_video_close(struct video_device *vdev)
1061 struct pwc_device *pdev;
1064 Trace(TRACE_OPEN, ">> video_close called(vdev = 0x%p).\n", vdev);
1066 pdev = (struct pwc_device *)vdev->priv;
1067 if (pdev->vopen == 0)
1068 Info("video_close() called on closed device?\n");
1070 /* Dump statistics, but only if a reasonable amount of frames were
1071 processed (to prevent endless log-entries in case of snap-shot
1074 if (pdev->vframe_count > 20)
1075 Info("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev->vframe_count, pdev->vframes_dumped, pdev->vframes_error);
1077 if (pdev->decompressor != NULL) {
1078 pdev->decompressor->exit();
1079 pdev->decompressor->unlock();
1080 pdev->decompressor = NULL;
1083 pwc_isoc_cleanup(pdev);
1084 pwc_free_buffers(pdev);
1086 /* Turn off LEDS and power down camera, but only when not unplugged */
1087 if (pdev->error_status != EPIPE) {
1088 if (pwc_set_leds(pdev, 0, 0) < 0)
1089 Info("Failed to set LED on/off time.\n");
1091 i = pwc_camera_power(pdev, 0);
1093 Err("Failed to power down camera (%d)\n", i);
1097 Trace(TRACE_OPEN, "<< video_close()\n");
1101 * FIXME: what about two parallel reads ????
1102 * ANSWER: Not supported. You can't open the device more than once,
1103 despite what the V4L1 interface says. First, I don't see
1104 the need, second there's no mechanism of alerting the
1105 2nd/3rd/... process of events like changing image size.
1106 And I don't see the point of blocking that for the
1107 2nd/3rd/... process.
1108 In multi-threaded environments reading parallel from any
1109 device is tricky anyhow.
1112 static long pwc_video_read(struct video_device *vdev, char *buf, unsigned long count, int noblock)
1114 struct pwc_device *pdev;
1115 DECLARE_WAITQUEUE(wait, current);
1117 Trace(TRACE_READ, "video_read(0x%p, %p, %ld, %d) called.\n", vdev, buf, count, noblock);
1123 if (pdev->error_status)
1124 return -pdev->error_status; /* Something happened, report what. */
1126 /* In case we're doing partial reads, we don't have to wait for a frame */
1127 if (pdev->image_read_pos == 0) {
1128 /* Do wait queueing according to the (doc)book */
1129 add_wait_queue(&pdev->frameq, &wait);
1130 while (pdev->full_frames == NULL) {
1131 /* Check for unplugged/etc. here */
1132 if (pdev->error_status) {
1133 remove_wait_queue(&pdev->frameq, &wait);
1134 set_current_state(TASK_RUNNING);
1135 return -pdev->error_status ;
1138 remove_wait_queue(&pdev->frameq, &wait);
1139 set_current_state(TASK_RUNNING);
1140 return -EWOULDBLOCK;
1142 if (signal_pending(current)) {
1143 remove_wait_queue(&pdev->frameq, &wait);
1144 set_current_state(TASK_RUNNING);
1145 return -ERESTARTSYS;
1148 set_current_state(TASK_INTERRUPTIBLE);
1150 remove_wait_queue(&pdev->frameq, &wait);
1151 set_current_state(TASK_RUNNING);
1153 /* Decompress and release frame */
1154 if (pwc_handle_frame(pdev))
1158 Trace(TRACE_READ, "Copying data to user space.\n");
1159 /* copy bytes to user space; we allow for partial reads */
1160 if (count + pdev->image_read_pos > pdev->view.size)
1161 count = pdev->view.size - pdev->image_read_pos;
1162 if (copy_to_user(buf, pdev->image_ptr[pdev->fill_image] + pdev->image_read_pos, count))
1164 pdev->image_read_pos += count;
1165 if (pdev->image_read_pos >= pdev->view.size) { /* All data has been read */
1166 pdev->image_read_pos = 0;
1167 pwc_next_image(pdev);
1173 static long pwc_video_write(struct video_device *vdev, const char *buf, unsigned long count, int noblock)
1178 static unsigned int pwc_video_poll(struct video_device *vdev, struct file *file, poll_table *wait)
1180 struct pwc_device *pdev;
1188 poll_wait(file, &pdev->frameq, wait);
1189 if (pdev->error_status)
1191 if (pdev->full_frames != NULL) /* we have frames waiting */
1192 return (POLLIN | POLLRDNORM);
1197 static int pwc_video_ioctl(struct video_device *vdev, unsigned int cmd, void *arg)
1199 struct pwc_device *pdev;
1200 DECLARE_WAITQUEUE(wait, current);
1209 /* Query cabapilities */
1212 struct video_capability caps;
1214 strcpy(caps.name, vdev->name);
1215 caps.type = VID_TYPE_CAPTURE;
1218 caps.minwidth = pdev->view_min.x;
1219 caps.minheight = pdev->view_min.y;
1220 caps.maxwidth = pdev->view_max.x;
1221 caps.maxheight = pdev->view_max.y;
1222 if (copy_to_user(arg, &caps, sizeof(caps)))
1227 /* Channel functions (simulate 1 channel) */
1230 struct video_channel v;
1232 if (copy_from_user(&v, arg, sizeof(v)))
1239 v.type = VIDEO_TYPE_CAMERA;
1240 strcpy(v.name, "Webcam");
1242 if (copy_to_user(arg, &v, sizeof(v)))
1250 /* The spec says the argument is an integer, but
1251 the bttv driver uses a video_channel arg, which
1252 makes sense becasue it also has the norm flag.
1254 struct video_channel v;
1256 if (copy_from_user(&v, arg, sizeof(v)))
1266 /* Picture functions; contrast etc. */
1269 struct video_picture p;
1274 val = pwc_get_brightness(pdev);
1278 p.brightness = 0xffff;
1279 val = pwc_get_contrast(pdev);
1283 p.contrast = 0xffff;
1284 /* Gamma, Whiteness, what's the difference? :) */
1285 val = pwc_get_gamma(pdev);
1289 p.whiteness = 0xffff;
1290 val = pwc_get_saturation(pdev);
1296 p.palette = VIDEO_PALETTE_YUV420P;
1297 p.hue = 0xFFFF; /* N/A */
1299 if (copy_to_user(arg, &p, sizeof(p)))
1306 struct video_picture p;
1308 if (copy_from_user(&p, arg, sizeof(p)))
1312 * FIXME: Suppose we are mid read
1313 ANSWER: No problem: the firmware of the camera
1314 can handle brightness/contrast/etc
1315 changes at _any_ time, and the palette
1316 is used exactly once in the uncompress
1319 pwc_set_brightness(pdev, p.brightness);
1320 pwc_set_contrast(pdev, p.contrast);
1321 pwc_set_gamma(pdev, p.whiteness);
1322 pwc_set_saturation(pdev, p.colour);
1323 if (p.palette && p.palette != VIDEO_PALETTE_YUV420P) {
1329 /* Window/size parameters */
1332 struct video_window vw;
1336 vw.width = pdev->view.x;
1337 vw.height = pdev->view.y;
1339 vw.flags = (pdev->vframes << PWC_FPS_SHIFT) |
1340 (pdev->vsnapshot ? PWC_FPS_SNAPSHOT : 0);
1342 if (copy_to_user(arg, &vw, sizeof(vw)))
1349 struct video_window vw;
1350 int fps, snapshot, ret;
1352 if (copy_from_user(&vw, arg, sizeof(vw)))
1355 fps = (vw.flags & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
1356 snapshot = vw.flags & PWC_FPS_SNAPSHOT;
1358 fps = pdev->vframes;
1359 if (pdev->view.x == vw.width && pdev->view.y && fps == pdev->vframes && snapshot == pdev->vsnapshot)
1361 ret = pwc_try_video_mode(pdev, vw.width, vw.height, fps, pdev->vcompression, snapshot);
1367 /* We don't have overlay support (yet) */
1370 struct video_buffer vb;
1376 vb.bytesperline = 0;
1378 if (copy_to_user((void *)arg, (void *)&vb, sizeof(vb)))
1383 /* mmap() functions */
1386 /* Tell the user program how much memory is needed for a mmap() */
1387 struct video_mbuf vm;
1390 memset(&vm, 0, sizeof(vm));
1391 vm.size = default_mbufs * pdev->len_per_image;
1392 vm.frames = default_mbufs; /* double buffering should be enough for most applications */
1393 for (i = 0; i < default_mbufs; i++)
1394 vm.offsets[i] = i * pdev->len_per_image;
1396 if (copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
1401 case VIDIOCMCAPTURE:
1403 /* Start capture into a given image buffer (called 'frame' in video_mmap structure) */
1404 struct video_mmap vm;
1406 if (copy_from_user((void *)&vm, (void *)arg, sizeof(vm)))
1408 Trace(TRACE_READ, "VIDIOCMCAPTURE: %dx%d, frame %d, format %d\n", vm.width, vm.height, vm.frame, vm.format);
1409 if (vm.frame < 0 || vm.frame >= default_mbufs)
1412 /* xawtv is nasty. It probes the available palettes
1413 by setting a very small image size and trying
1414 various palettes... The driver doesn't support
1415 such small images, so I'm working around it.
1417 if (vm.format && vm.format != VIDEO_PALETTE_YUV420P)
1420 if ((vm.width != pdev->view.x || vm.height != pdev->view.y) &&
1421 (vm.width >= pdev->view_min.x && vm.height >= pdev->view_min.y)) {
1424 Trace(TRACE_OPEN, "VIDIOCMCAPTURE: changing size to please xawtv :-(.\n");
1425 ret = pwc_try_video_mode(pdev, vm.width, vm.height, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1428 } /* ... size mismatch */
1430 /* FIXME: should we lock here? */
1431 if (pdev->image_used[vm.frame])
1432 return -EBUSY; /* buffer wasn't available. Bummer */
1433 pdev->image_used[vm.frame] = 1;
1435 /* Okay, we're done here. In the SYNC call we wait until a
1436 frame comes available, then expand image into the given
1438 In contrast to the CPiA cam the Philips cams deliver a
1439 constant stream, almost like a grabber card. Also,
1440 we have separate buffers for the rawdata and the image,
1441 meaning we can nearly always expand into the requested buffer.
1443 Trace(TRACE_READ, "VIDIOCMCAPTURE done.\n");
1449 /* The doc says: "Whenever a buffer is used it should
1450 call VIDIOCSYNC to free this frame up and continue."
1452 The only odd thing about this whole procedure is
1453 that MCAPTURE flags the buffer as "in use", and
1454 SYNC immediately unmarks it, while it isn't
1455 after SYNC that you know that the buffer actually
1456 got filled! So you better not start a CAPTURE in
1457 the same frame immediately (use double buffering).
1458 This is not a problem for this cam, since it has
1459 extra intermediate buffers, but a hardware
1460 grabber card will then overwrite the buffer
1465 if (copy_from_user((void *)&mbuf, arg, sizeof(int)))
1468 Trace(TRACE_READ, "VIDIOCSYNC called (%d).\n", mbuf);
1471 if (mbuf < 0 || mbuf >= default_mbufs)
1473 /* check if this buffer was requested anyway */
1474 if (pdev->image_used[mbuf] == 0)
1477 /* Add ourselves to the frame wait-queue.
1478 In the loop, check for error conditions and signals.
1480 FIXME: needs auditing for safety.
1481 QUESTION: In what respect? I think that using the
1484 add_wait_queue(&pdev->frameq, &wait);
1485 while (pdev->full_frames == NULL) {
1486 if (pdev->error_status) {
1487 remove_wait_queue(&pdev->frameq, &wait);
1488 set_current_state(TASK_RUNNING);
1489 return -pdev->error_status;
1491 if (signal_pending(current)) {
1492 remove_wait_queue(&pdev->frameq, &wait);
1493 set_current_state(TASK_RUNNING);
1494 return -ERESTARTSYS;
1497 set_current_state(TASK_INTERRUPTIBLE);
1499 remove_wait_queue(&pdev->frameq, &wait);
1500 set_current_state(TASK_RUNNING);
1502 /* The frame is ready. Expand in the image buffer
1503 requested by the user. I don't care if you
1504 mmap() 5 buffers and request data in this order:
1505 buffer 4 2 3 0 1 2 3 0 4 3 1 . . .
1506 Grabber hardware may not be so forgiving.
1508 Trace(TRACE_READ, "VIDIOCSYNC: frame ready.\n");
1509 pdev->fill_image = mbuf; /* tell in which buffer we want the image to be expanded */
1510 /* Decompress, etc */
1511 ret = pwc_handle_frame(pdev);
1512 pdev->image_used[mbuf] = 0;
1520 struct video_audio v;
1522 strcpy(v.name, "Microphone");
1523 v.audio = -1; /* unknown audio minor */
1525 v.mode = VIDEO_SOUND_MONO;
1532 if (copy_to_user(arg, &v, sizeof(v)))
1539 struct video_audio v;
1541 if (copy_from_user(&v, arg, sizeof(v)))
1543 /* Dummy: nothing can be set */
1549 struct video_unit vu;
1551 vu.video = pdev->vdev.minor & 0x3F;
1552 vu.audio = -1; /* not known yet */
1556 if (copy_to_user(arg, &vu, sizeof(vu)))
1561 return pwc_ioctl(pdev, cmd, arg);
1566 static int pwc_video_mmap(struct video_device *vdev, const char *adr, unsigned long size)
1568 struct pwc_device *pdev;
1569 unsigned long start = (unsigned long)adr;
1570 unsigned long page, pos;
1572 Trace(TRACE_MEMORY, "mmap(0x%p, 0x%p, %lu) called.\n", vdev, adr, size);
1575 pos = (unsigned long)pdev->image_data;
1577 page = kvirt_to_pa(pos);
1578 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
1583 if (size > PAGE_SIZE)
1592 /***************************************************************************/
1595 /* This function gets called when a new device is plugged in or the usb core
1599 static void *usb_pwc_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
1601 struct pwc_device *pdev = NULL;
1602 int vendor_id, product_id, type_id;
1604 int video_nr = -1; /* default: use next available device */
1605 char serial_number[30], *name;
1607 /* Check if we can handle this device */
1608 Trace(TRACE_PROBE, "probe() called [%04X %04X], if %d\n", udev->descriptor.idVendor, udev->descriptor.idProduct, ifnum);
1610 /* the interfaces are probed one by one. We are only interested in the
1611 video interface (0) now.
1612 Interface 1 is the Audio Control, and interface 2 Audio itself.
1617 vendor_id = udev->descriptor.idVendor;
1618 product_id = udev->descriptor.idProduct;
1620 if (vendor_id == 0x0471) {
1621 switch (product_id) {
1623 Info("Philips PCA645VC USB webcam detected.\n");
1624 name = "Philips 645 webcam";
1628 Info("Philips PCA646VC USB webcam detected.\n");
1629 name = "Philips 646 webcam";
1633 Info("Askey VC010 type 2 USB webcam detected.\n");
1634 name = "Askey VC010 webcam";
1638 Info("Philips PCVC675K (Vesta) USB webcam detected.\n");
1639 name = "Philips 675 webcam";
1643 Info("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
1644 name = "Philips 680 webcam";
1648 Info("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
1649 name = "Philips 690 webcam";
1653 Info("Philips PCVC730K (ToUCam Fun) USB webcam detected.\n");
1654 name = "Philips 730 webcam";
1658 Info("Philips PCVC740K (ToUCam Pro) USB webcam detected.\n");
1659 name = "Philips 740 webcam";
1663 Info("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
1664 name = "Philips 750 webcam";
1668 Info("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
1669 name = "Philips 720 webcam";
1677 else if (vendor_id == 0x069A) {
1678 switch(product_id) {
1680 Info("Askey VC010 type 1 USB webcam detected.\n");
1681 name = "Askey VC010 webcam";
1689 else if (vendor_id == 0x046d) {
1690 switch(product_id) {
1692 Info("Logitech QuickCam Pro 3000 USB webcam detected.\n");
1693 name = "Logitech QuickCam Pro 3000";
1694 type_id = 740; /* CCD sensor */
1697 Info("Logitech QuickCam for Notebook Pro USB webcam detected.\n");
1698 name = "Logitech QuickCam Notebook Pro";
1699 type_id = 740; /* CCD sensor */
1702 Info("Logitech QuickCam 4000 Pro USB webcam detected.\n");
1703 name = "Logitech QuickCam Pro 4000";
1704 type_id = 740; /* CCD sensor */
1707 Info("Logitech QuickCam Zoom USB webcam detected.\n");
1708 name = "Logitech QuickCam Zoom";
1709 type_id = 740; /* CCD sensor */
1716 Info("Logitech QuickCam detected (reserved ID).\n");
1717 name = "Logitech QuickCam (res.)";
1718 type_id = 730; /* Assuming CMOS */
1725 else if (vendor_id == 0x055d) {
1726 /* I don't know the difference between the C10 and the C30;
1727 I suppose the difference is the sensor, but both cameras
1728 work equally well with a type_id of 675
1730 switch(product_id) {
1732 Info("Samsung MPC-C10 USB webcam detected.\n");
1733 name = "Samsung MPC-C10";
1737 Info("Samsung MPC-C30 USB webcam detected.\n");
1738 name = "Samsung MPC-C30";
1746 else if (vendor_id == 0x041e) {
1747 switch(product_id) {
1749 Info("Creative Labs Webcam 5 detected.\n");
1750 name = "Creative Labs Webcam 5";
1754 Info("Creative Labs Webcam Pro Ex detected.\n");
1755 name = "Creative Labs Webcam Pro Ex";
1763 else if (vendor_id == 0x04cc) {
1764 switch(product_id) {
1766 Info("Sotec Afina Eye USB webcam detected.\n");
1767 name = "Sotec Afina Eye";
1775 else if (vendor_id == 0x0d81) {
1776 switch(product_id) {
1778 Info("Visionite VCS-UC300 USB webcam detected.\n");
1779 name = "Visionite VCS-UC300";
1780 type_id = 740; /* CCD sensor */
1783 Info("Visionite VCS-UM100 USB webcam detected.\n");
1784 name = "Visionite VCS-UM100";
1785 type_id = 730; /* CMOS sensor */
1793 return NULL; /* Not any of the know types; but the list keeps growing. */
1795 memset(serial_number, 0, 30);
1796 usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
1797 Trace(TRACE_PROBE, "Device serial number is %s\n", serial_number);
1799 if (udev->descriptor.bNumConfigurations > 1)
1800 Info("Warning: more than 1 configuration available.\n");
1802 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1803 pdev = kmalloc(sizeof(struct pwc_device), GFP_KERNEL);
1805 Err("Oops, could not allocate memory for pwc_device.\n");
1808 memset(pdev, 0, sizeof(struct pwc_device));
1809 pdev->type = type_id;
1810 pwc_construct(pdev);
1811 pdev->vsize = default_size;
1812 pdev->vframes = default_fps;
1814 init_MUTEX(&pdev->modlock);
1815 pdev->ptrlock = SPIN_LOCK_UNLOCKED;
1818 init_waitqueue_head(&pdev->frameq);
1819 pdev->vcompression = pwc_preferred_compression;
1821 memcpy(&pdev->vdev, &pwc_template, sizeof(pwc_template));
1822 strcpy(pdev->vdev.name, name);
1823 SET_MODULE_OWNER(&pdev->vdev);
1824 pdev->vdev.priv = pdev;
1826 pdev->release = udev->descriptor.bcdDevice;
1827 Trace(TRACE_PROBE, "Release: %04x\n", pdev->release);
1829 /* Now search device_hint[] table for a match, so we can hint a node number. */
1830 for (hint = 0; hint < MAX_DEV_HINTS; hint++) {
1831 if (((device_hint[hint].type == -1) || (device_hint[hint].type == pdev->type)) &&
1832 (device_hint[hint].pdev == NULL)) {
1833 /* so far, so good... try serial number */
1834 if ((device_hint[hint].serial_number[0] == '*') || !strcmp(device_hint[hint].serial_number, serial_number)) {
1836 video_nr = device_hint[hint].device_node;
1837 Trace(TRACE_PROBE, "Found hint, will try to register as /dev/video%d\n", video_nr);
1843 i = video_register_device(&pdev->vdev, VFL_TYPE_GRABBER, video_nr);
1845 Err("Failed to register as video device (%d).\n", i);
1846 kfree(pdev); /* Oops, no memory leaks please */
1850 Info("Registered as /dev/video%d.\n", pdev->vdev.minor & 0x3F);
1853 if (hint < MAX_DEV_HINTS)
1854 device_hint[hint].pdev = pdev;
1856 Trace(TRACE_PROBE, "probe() function returning struct at 0x%p.\n", pdev);
1860 /* The user janked out the cable... */
1861 static void usb_pwc_disconnect(struct usb_device *udev, void *ptr)
1863 struct pwc_device *pdev;
1867 pdev = (struct pwc_device *)ptr;
1869 Err("pwc_disconnect() Called without private pointer.\n");
1873 if (pdev->udev == NULL) {
1874 Err("pwc_disconnect() already called for %p\n", pdev);
1878 if (pdev->udev != udev) {
1879 Err("pwc_disconnect() Woops: pointer mismatch udev/pdev.\n");
1884 if (pdev->magic != PWC_MAGIC) {
1885 Err("pwc_disconnect() Magic number failed. Consult your scrolls and try again.\n");
1891 /* We got unplugged; this is signalled by an EPIPE error code */
1893 Info("Disconnected while webcam is in use!\n");
1894 pdev->error_status = EPIPE;
1897 /* Alert waiting processes */
1898 wake_up_interruptible(&pdev->frameq);
1899 /* Wait until device is closed */
1902 /* Device is now closed, so we can safely unregister it */
1903 Trace(TRACE_PROBE, "Unregistering video device in disconnect().\n");
1904 video_unregister_device(&pdev->vdev);
1906 /* Free memory (don't set pdev to 0 just yet) */
1909 /* search device_hint[] table if we occupy a slot, by any chance */
1910 for (hint = 0; hint < MAX_DEV_HINTS; hint++)
1911 if (device_hint[hint].pdev == pdev)
1912 device_hint[hint].pdev = NULL;
1918 /* *grunt* We have to do atoi ourselves :-( */
1919 static int pwc_atoi(const char *s)
1924 while (*s != '\0' && *s >= '0' && *s <= '9') {
1925 k = 10 * k + (*s - '0');
1933 * Initialization code & module stuff
1936 static char *size = NULL;
1938 static int fbufs = 0;
1939 static int mbufs = 0;
1940 static int trace = -1;
1941 static int compression = -1;
1942 static int leds[2] = { -1, -1 };
1943 static char *dev_hint[MAX_DEV_HINTS] = { };
1945 MODULE_PARM(size, "s");
1946 MODULE_PARM_DESC(size, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
1947 MODULE_PARM(fps, "i");
1948 MODULE_PARM_DESC(fps, "Initial frames per second. Varies with model, useful range 5-30");
1949 MODULE_PARM(fbufs, "i");
1950 MODULE_PARM_DESC(fbufs, "Number of internal frame buffers to reserve");
1951 MODULE_PARM(mbufs, "i");
1952 MODULE_PARM_DESC(mbufs, "Number of external (mmap()ed) image buffers");
1953 MODULE_PARM(trace, "i");
1954 MODULE_PARM_DESC(trace, "For debugging purposes");
1955 MODULE_PARM(power_save, "i");
1956 MODULE_PARM_DESC(power_save, "Turn power save feature in camera on or off");
1957 MODULE_PARM(compression, "i");
1958 MODULE_PARM_DESC(compression, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
1959 MODULE_PARM(leds, "2i");
1960 MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
1961 MODULE_PARM(dev_hint, "0-20s");
1962 MODULE_PARM_DESC(dev_hint, "Device node hints");
1964 MODULE_DESCRIPTION("Philips USB & OEM webcam driver");
1965 MODULE_AUTHOR("Nemosoft Unv. <nemosoft@smcc.demon.nl>");
1966 MODULE_LICENSE("GPL");
1968 static int __init usb_pwc_init(void)
1971 char *sizenames[PSZ_MAX] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };
1973 Info("Philips PCA645/646 + PCVC675/680/690 + PCVC730/740/750 webcam module version " PWC_VERSION " loaded.\n");
1974 Info("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
1975 Info("the Creative WebCam 5, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
1978 if (fps < 4 || fps > 30) {
1979 Err("Framerate out of bounds (4-30).\n");
1983 Info("Default framerate set to %d.\n", default_fps);
1987 /* string; try matching with array */
1988 for (sz = 0; sz < PSZ_MAX; sz++) {
1989 if (!strcmp(sizenames[sz], size)) { /* Found! */
1994 if (sz == PSZ_MAX) {
1995 Err("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
1998 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);
2001 if (mbufs < 1 || mbufs > MAX_IMAGES) {
2002 Err("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES);
2005 default_mbufs = mbufs;
2006 Info("Number of image buffers set to %d.\n", default_mbufs);
2009 if (fbufs < 2 || fbufs > MAX_FRAMES) {
2010 Err("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES);
2013 default_fbufs = fbufs;
2014 Info("Number of frame buffers set to %d.\n", default_fbufs);
2017 Info("Trace options: 0x%04x\n", trace);
2020 if (compression >= 0) {
2021 if (compression > 3) {
2022 Err("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
2025 pwc_preferred_compression = compression;
2026 Info("Preferred compression set to %d.\n", pwc_preferred_compression);
2029 Info("Enabling power save on open/close.\n");
2035 /* Big device node whoopla. Basicly, it allows you to assign a
2036 device node (/dev/videoX) to a camera, based on its type
2037 & serial number. The format is [type[.serialnumber]:]node.
2039 Any camera that isn't matched by these rules gets the next
2040 available free device node.
2042 for (i = 0; i < MAX_DEV_HINTS; i++) {
2043 char *s, *colon, *dot;
2045 /* This loop also initializes the array */
2046 device_hint[i].pdev = NULL;
2048 if (s != NULL && *s != '\0') {
2049 device_hint[i].type = -1; /* wildcard */
2050 strcpy(device_hint[i].serial_number, "*");
2052 /* parse string: chop at ':' & '/' */
2054 while (*colon != '\0' && *colon != ':')
2056 while (*dot != '\0' && *dot != '.')
2058 /* Few sanity checks */
2059 if (*dot != '\0' && dot > colon) {
2060 Err("Malformed camera hint: the colon must be after the dot.\n");
2064 if (*colon == '\0') {
2067 Err("Malformed camera hint: no colon + device node given.\n");
2071 /* No type or serial number specified, just a number. */
2072 device_hint[i].device_node = pwc_atoi(s);
2076 /* There's a colon, so we have at least a type and a device node */
2077 device_hint[i].type = pwc_atoi(s);
2078 device_hint[i].device_node = pwc_atoi(colon + 1);
2080 /* There's a serial number as well */
2085 while (*dot != ':' && k < 29) {
2086 device_hint[i].serial_number[k++] = *dot;
2089 device_hint[i].serial_number[k] = '\0';
2093 Debug("device_hint[%d]:\n", i);
2094 Debug(" type : %d\n", device_hint[i].type);
2095 Debug(" serial# : %s\n", device_hint[i].serial_number);
2096 Debug(" node : %d\n", device_hint[i].device_node);
2100 device_hint[i].type = 0; /* not filled */
2101 } /* ..for MAX_DEV_HINTS */
2103 Trace(TRACE_PROBE, "Registering driver at address 0x%p.\n", &pwc_driver);
2104 return usb_register(&pwc_driver);
2107 static void __exit usb_pwc_exit(void)
2109 Trace(TRACE_MODULE, "Deregistering driver.\n");
2110 usb_deregister(&pwc_driver);
2111 Info("Philips webcam module removed.\n");
2114 module_init(usb_pwc_init);
2115 module_exit(usb_pwc_exit);