[PATCH] pwc bug fix
[linux-2.6/btrfs-unstable.git] / drivers / usb / media / pwc / pwc-if.c
blobb77e65c03659a0bdffda1a758f7ef04beb44201d
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
28 /*
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
38 without explanation).
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 *
47 /* Contributors:
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>
60 #include <linux/mm.h>
61 #include <linux/module.h>
62 #include <linux/poll.h>
63 #include <linux/slab.h>
64 #include <linux/vmalloc.h>
65 #include <asm/io.h>
67 #include "pwc.h"
68 #include "pwc-ioctl.h"
69 #include "pwc-kiara.h"
70 #include "pwc-timon.h"
71 #include "pwc-uncompress.h"
73 /* Function prototypes and driver templates */
75 /* hotplug device table support */
76 static struct usb_device_id pwc_device_table [] = {
77 { USB_DEVICE(0x0471, 0x0302) }, /* Philips models */
78 { USB_DEVICE(0x0471, 0x0303) },
79 { USB_DEVICE(0x0471, 0x0304) },
80 { USB_DEVICE(0x0471, 0x0307) },
81 { USB_DEVICE(0x0471, 0x0308) },
82 { USB_DEVICE(0x0471, 0x030C) },
83 { USB_DEVICE(0x0471, 0x0310) },
84 { USB_DEVICE(0x0471, 0x0311) },
85 { USB_DEVICE(0x0471, 0x0312) },
86 { USB_DEVICE(0x0471, 0x0313) }, /* the 'new' 720K */
87 { USB_DEVICE(0x069A, 0x0001) }, /* Askey */
88 { USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam Pro 3000 */
89 { USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
90 { USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam Pro 4000 */
91 { USB_DEVICE(0x046D, 0x08B3) }, /* Logitech QuickCam Zoom (old model) */
92 { USB_DEVICE(0x046D, 0x08B4) }, /* Logitech QuickCam Zoom (new model) */
93 { USB_DEVICE(0x046D, 0x08B5) }, /* Logitech QuickCam Orbit/Sphere */
94 { USB_DEVICE(0x046D, 0x08B6) }, /* Logitech (reserved) */
95 { USB_DEVICE(0x046D, 0x08B7) }, /* Logitech (reserved) */
96 { USB_DEVICE(0x046D, 0x08B8) }, /* Logitech (reserved) */
97 { USB_DEVICE(0x055D, 0x9000) }, /* Samsung */
98 { USB_DEVICE(0x055D, 0x9001) },
99 { USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
100 { USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
101 { USB_DEVICE(0x04CC, 0x8116) }, /* Afina Eye */
102 { USB_DEVICE(0x06BE, 0x8116) }, /* new Afina Eye */
103 { USB_DEVICE(0x0d81, 0x1910) }, /* Visionite */
104 { USB_DEVICE(0x0d81, 0x1900) },
107 MODULE_DEVICE_TABLE(usb, pwc_device_table);
109 static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id);
110 static void usb_pwc_disconnect(struct usb_interface *intf);
112 static struct usb_driver pwc_driver = {
113 .owner = THIS_MODULE,
114 .name = "Philips webcam", /* name */
115 .id_table = pwc_device_table,
116 .probe = usb_pwc_probe, /* probe() */
117 .disconnect = usb_pwc_disconnect, /* disconnect() */
120 #define MAX_DEV_HINTS 20
121 #define MAX_ISOC_ERRORS 20
123 static int default_size = PSZ_QCIF;
124 static int default_fps = 10;
125 static int default_fbufs = 3; /* Default number of frame buffers */
126 static int default_mbufs = 2; /* Default number of mmap() buffers */
127 int pwc_trace = TRACE_MODULE | TRACE_FLOW | TRACE_PWCX;
128 static int power_save = 0;
129 static int led_on = 100, led_off = 0; /* defaults to LED that is on while in use */
130 static int pwc_preferred_compression = 2; /* 0..3 = uncompressed..high */
131 static struct {
132 int type;
133 char serial_number[30];
134 int device_node;
135 struct pwc_device *pdev;
136 } device_hint[MAX_DEV_HINTS];
138 /***/
140 static int pwc_video_open(struct inode *inode, struct file *file);
141 static int pwc_video_close(struct inode *inode, struct file *file);
142 static ssize_t pwc_video_read(struct file *file, char __user * buf,
143 size_t count, loff_t *ppos);
144 static unsigned int pwc_video_poll(struct file *file, poll_table *wait);
145 static int pwc_video_ioctl(struct inode *inode, struct file *file,
146 unsigned int ioctlnr, unsigned long arg);
147 static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma);
149 static struct file_operations pwc_fops = {
150 .owner = THIS_MODULE,
151 .open = pwc_video_open,
152 .release = pwc_video_close,
153 .read = pwc_video_read,
154 .poll = pwc_video_poll,
155 .mmap = pwc_video_mmap,
156 .ioctl = pwc_video_ioctl,
157 .llseek = no_llseek,
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,
165 .fops = &pwc_fops,
166 .minor = -1,
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 */
212 ret = __pa(kva);
213 return ret;
216 static void * rvmalloc(unsigned long size)
218 void * mem;
219 unsigned long adr;
221 size=PAGE_ALIGN(size);
222 mem=vmalloc_32(size);
223 if (mem)
225 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
226 adr=(unsigned long) mem;
227 while (size > 0)
229 SetPageReserved(vmalloc_to_page((void *)adr));
230 adr+=PAGE_SIZE;
231 size-=PAGE_SIZE;
234 return mem;
237 static void rvfree(void * mem, unsigned long size)
239 unsigned long adr;
241 if (mem)
243 adr=(unsigned long) mem;
244 while ((long) size > 0)
246 ClearPageReserved(vmalloc_to_page((void *)adr));
247 adr+=PAGE_SIZE;
248 size-=PAGE_SIZE;
250 vfree(mem);
257 static int pwc_allocate_buffers(struct pwc_device *pdev)
259 int i;
260 void *kbuf;
262 Trace(TRACE_MEMORY, ">> pwc_allocate_buffers(pdev = 0x%p)\n", pdev);
264 if (pdev == NULL)
265 return -ENXIO;
267 #ifdef PWC_MAGIC
268 if (pdev->magic != PWC_MAGIC) {
269 Err("allocate_buffers(): magic failed.\n");
270 return -ENXIO;
272 #endif
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);
277 if (kbuf == NULL) {
278 Err("Failed to allocate iso buffer %d.\n", i);
279 return -ENOMEM;
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);
290 if (kbuf == NULL) {
291 Err("Failed to allocate frame buffer structure.\n");
292 return -ENOMEM;
294 Trace(TRACE_MEMORY, "Allocated frame buffer structure at %p.\n", kbuf);
295 pdev->fbuf = 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 */
302 if (kbuf == NULL) {
303 Err("Failed to allocate frame buffer %d.\n", i);
304 return -ENOMEM;
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 */
313 kbuf = NULL;
314 switch (pdev->type)
316 case 675:
317 case 680:
318 case 690:
319 case 720:
320 case 730:
321 case 740:
322 case 750:
323 #if 0
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 */
326 break;
327 case 645:
328 case 646:
329 /* TODO & FIXME */
330 kbuf = kmalloc(sizeof(struct pwc_dec23_private), GFP_KERNEL);
331 break;
332 #endif
335 pdev->decompress_data = kbuf;
337 /* Allocate image buffer; double buffer for mmap() */
338 kbuf = rvmalloc(default_mbufs * pdev->len_per_image);
339 if (kbuf == NULL) {
340 Err("Failed to allocate image buffer(s). needed (%d)\n",default_mbufs * pdev->len_per_image);
341 return -ENOMEM;
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;
350 kbuf = NULL;
352 Trace(TRACE_MEMORY, "<< pwc_allocate_buffers()\n");
353 return 0;
356 static void pwc_free_buffers(struct pwc_device *pdev)
358 int i;
360 Trace(TRACE_MEMORY, "Entering free_buffers(%p).\n", pdev);
362 if (pdev == NULL)
363 return;
364 #ifdef PWC_MAGIC
365 if (pdev->magic != PWC_MAGIC) {
366 Err("free_buffers(): magic failed.\n");
367 return;
369 #endif
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;
388 kfree(pdev->fbuf);
389 pdev->fbuf = 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
418 frame buffer.
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
437 it solved.
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'
450 list.
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
461 always be full.
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)
469 int ret;
470 unsigned long flags;
472 ret = 0;
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;
480 else {
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;
490 else {
491 /* Hmm. Take it from the full list */
492 #if PWC_DEBUG
493 /* sanity check */
494 if (pdev->full_frames == NULL) {
495 Err("Neither empty or full frames available!\n");
496 spin_unlock_irqrestore(&pdev->ptrlock, flags);
497 return -EINVAL;
499 #endif
500 pdev->fill_frame = pdev->full_frames;
501 pdev->full_frames = pdev->full_frames->next;
502 ret = 1;
504 pdev->fill_frame->next = NULL;
505 #if PWC_DEBUG
506 Trace(TRACE_SEQUENCE, "Assigning sequence number %d.\n", pdev->sequence);
507 pdev->fill_frame->sequence = pdev->sequence++;
508 #endif
509 spin_unlock_irqrestore(&pdev->ptrlock, flags);
510 return ret;
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)
521 int i;
522 unsigned long flags;
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;
529 if (i > 0)
530 pdev->fbuf[i].next = &pdev->fbuf[i - 1];
531 else
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)
551 int ret = 0;
552 unsigned long flags;
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");
561 else {
562 if (pdev->full_frames == NULL) {
563 Err("Woops. No frames ready.\n");
565 else {
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) {
572 #if PWC_DEBUG
573 Trace(TRACE_SEQUENCE, "Decompressing frame %d\n", pdev->read_frame->sequence);
574 #endif
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
577 in the mean time.
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;
588 else {
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);
596 return ret;
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;
615 int i, fst, flen;
616 int awake;
617 struct pwc_frame_buf *fbuf;
618 unsigned char *fillptr = NULL, *iso_buf = NULL;
620 awake = 0;
621 pdev = (struct pwc_device *)urb->context;
622 if (pdev == NULL) {
623 Err("isoc_handler() called with NULL device?!\n");
624 return;
626 #ifdef PWC_MAGIC
627 if (pdev->magic != PWC_MAGIC) {
628 Err("isoc_handler() called with bad magic!\n");
629 return;
631 #endif
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");
634 return;
636 if (urb->status != -EINPROGRESS && urb->status != 0) {
637 const char *errmsg;
639 errmsg = "Unknown";
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;
656 awake = 1;
657 wake_up_interruptible(&pdev->frameq);
659 goto handler_end; // ugly, but practical
662 fbuf = pdev->fill_frame;
663 if (fbuf == NULL) {
664 Err("pwc_isoc_handler without valid fill frame.\n");
665 awake = 1;
666 goto handler_end;
668 else {
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
676 1 = sync-hunt
677 2 = synched
679 /* Compact 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;
684 if (fst == 0) {
685 if (flen > 0) { /* if valid data... */
686 if (pdev->vsync > 0) { /* ...and we are not sync-hunting... */
687 pdev->vsync = 2;
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++;
695 else {
696 memmove(fillptr, iso_buf, flen);
697 fillptr += flen;
700 fbuf->filled += flen;
701 } /* ..flen > 0 */
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) {
717 #if PWC_DEBUG
718 Debug("Hyundai CMOS sensor bug. Dropping frame %d.\n", fbuf->sequence);
719 #endif
720 pdev->drop_frames += 2;
721 pdev->vframes_error++;
723 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
724 if (ptr[0] & 0x01)
725 Info("Snapshot button pressed.\n");
726 else
727 Info("Snapshot button released.\n");
729 if ((ptr[0] ^ pdev->vmirror) & 0x02) {
730 if (ptr[0] & 0x02)
731 Info("Image is mirrored.\n");
732 else
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
741 Or this one:
742 drop_bit set -> short frame -> good frame
743 So we drop either 3 or 2 frames in all!
745 if (fbuf->filled == 4)
746 pdev->drop_frames++;
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)
753 pdev->drop_frames--;
754 else {
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++;
760 else {
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;
779 } /* !drop_frames */
780 pdev->vframe_count++;
782 fbuf->filled = 0;
783 fillptr = fbuf->data;
784 pdev->vsync = 1;
785 } /* .. flen < last_packet_size */
786 pdev->vlast_packet_size = flen;
787 } /* ..status == 0 */
788 #if PWC_DEBUG
789 /* This is normally not interesting to the user, unless you are really debugging something */
790 else {
791 static int iso_error = 0;
792 iso_error++;
793 if (iso_error < 20)
794 Trace(TRACE_FLOW, "Iso frame %d of USB has error %d\n", i, fst);
796 #endif
799 handler_end:
800 if (awake)
801 wake_up_interruptible(&pdev->frameq);
803 urb->dev = pdev->udev;
804 i = usb_submit_urb(urb, GFP_ATOMIC);
805 if (i != 0)
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;
813 struct urb *urb;
814 int i, j, ret;
816 struct usb_interface *intf;
817 struct usb_host_interface *idesc = NULL;
819 if (pdev == NULL)
820 return -EFAULT;
821 if (pdev->iso_init)
822 return 0;
823 pdev->vsync = 0;
824 udev = pdev->udev;
826 /* Get the current alternate interface, adjust packet size */
827 if (!udev->actconfig)
828 return -EFAULT;
829 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5)
830 idesc = &udev->actconfig->interface[0]->altsetting[pdev->valternate];
831 #else
832 intf = usb_ifnum_to_if(udev, 0);
833 if (intf)
834 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
835 #endif
837 if (!idesc)
838 return -EFAULT;
840 /* Search video endpoint */
841 pdev->vmax_packet_size = -1;
842 for (i = 0; i < idesc->desc.bNumEndpoints; i++)
843 if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
844 pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
845 break;
848 if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
849 Err("Failed to find packet size for video endpoint in current alternate setting.\n");
850 return -ENFILE; /* Odd error, that should be noticeable */
853 /* Set alternate interface */
854 ret = 0;
855 Trace(TRACE_OPEN, "Setting alternate interface %d\n", pdev->valternate);
856 ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
857 if (ret < 0)
858 return ret;
860 for (i = 0; i < MAX_ISO_BUFS; i++) {
861 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
862 if (urb == NULL) {
863 Err("Failed to allocate urb %d\n", i);
864 ret = -ENOMEM;
865 break;
867 pdev->sbuf[i].urb = urb;
868 Trace(TRACE_MEMORY, "Allocated URB at 0x%p\n", urb);
870 if (ret) {
871 /* De-allocate in reverse order */
872 while (i >= 0) {
873 if (pdev->sbuf[i].urb != NULL)
874 usb_free_urb(pdev->sbuf[i].urb);
875 pdev->sbuf[i].urb = NULL;
876 i--;
878 return ret;
881 /* init URB structure */
882 for (i = 0; i < MAX_ISO_BUFS; i++) {
883 urb = pdev->sbuf[i].urb;
885 urb->interval = 1; // devik
886 urb->dev = udev;
887 urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
888 urb->transfer_flags = URB_ISO_ASAP;
889 urb->transfer_buffer = pdev->sbuf[i].data;
890 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
891 urb->complete = pwc_isoc_handler;
892 urb->context = pdev;
893 urb->start_frame = 0;
894 urb->number_of_packets = ISO_FRAMES_PER_DESC;
895 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
896 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
897 urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
901 /* link */
902 for (i = 0; i < MAX_ISO_BUFS; i++) {
903 ret = usb_submit_urb(pdev->sbuf[i].urb, GFP_KERNEL);
904 if (ret)
905 Err("isoc_init() submit_urb %d failed with error %d\n", i, ret);
906 else
907 Trace(TRACE_MEMORY, "URB 0x%p submitted.\n", pdev->sbuf[i].urb);
910 /* All is done... */
911 pdev->iso_init = 1;
912 Trace(TRACE_OPEN, "<< pwc_isoc_init()\n");
913 return 0;
916 static void pwc_isoc_cleanup(struct pwc_device *pdev)
918 int i;
920 Trace(TRACE_OPEN, ">> pwc_isoc_cleanup()\n");
921 if (pdev == NULL)
922 return;
924 /* Unlinking ISOC buffers one by one */
925 for (i = 0; i < MAX_ISO_BUFS; i++) {
926 struct urb *urb;
928 urb = pdev->sbuf[i].urb;
929 if (urb != 0) {
930 if (pdev->iso_init) {
931 Trace(TRACE_MEMORY, "Unlinking URB %p\n", urb);
932 usb_kill_urb(urb);
934 Trace(TRACE_MEMORY, "Freeing URB\n");
935 usb_free_urb(urb);
936 pdev->sbuf[i].urb = NULL;
940 /* Stop camera, but only if we are sure the camera is still there (unplug
941 is signalled by EPIPE)
943 if (pdev->error_status && pdev->error_status != EPIPE) {
944 Trace(TRACE_OPEN, "Setting alternate interface 0.\n");
945 usb_set_interface(pdev->udev, 0, 0);
948 pdev->iso_init = 0;
949 Trace(TRACE_OPEN, "<< pwc_isoc_cleanup()\n");
952 int pwc_try_video_mode(struct pwc_device *pdev, int width, int height, int new_fps, int new_compression, int new_snapshot)
954 int ret, start;
956 /* Stop isoc stuff */
957 pwc_isoc_cleanup(pdev);
958 /* Reset parameters */
959 pwc_reset_buffers(pdev);
960 /* Try to set video mode... */
961 start = ret = pwc_set_video_mode(pdev, width, height, new_fps, new_compression, new_snapshot);
962 if (ret) {
963 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 1 failed.\n");
964 /* That failed... restore old mode (we know that worked) */
965 start = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
966 if (start) {
967 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 2 failed.\n");
970 if (start == 0)
972 if (pwc_isoc_init(pdev) < 0)
974 Info("Failed to restart ISOC transfers in pwc_try_video_mode.\n");
975 ret = -EAGAIN; /* let's try again, who knows if it works a second time */
978 pdev->drop_frames++; /* try to avoid garbage during switch */
979 return ret; /* Return original error code */
983 /***************************************************************************/
984 /* Video4Linux functions */
986 static int pwc_video_open(struct inode *inode, struct file *file)
988 int i;
989 struct video_device *vdev = video_devdata(file);
990 struct pwc_device *pdev;
992 Trace(TRACE_OPEN, ">> video_open called(vdev = 0x%p).\n", vdev);
994 pdev = (struct pwc_device *)vdev->priv;
995 if (pdev == NULL)
996 BUG();
997 if (pdev->vopen)
998 return -EBUSY;
1000 down(&pdev->modlock);
1001 if (!pdev->usb_init) {
1002 Trace(TRACE_OPEN, "Doing first time initialization.\n");
1003 pdev->usb_init = 1;
1005 if (pwc_trace & TRACE_OPEN)
1007 /* Query sensor type */
1008 const char *sensor_type = NULL;
1009 int ret;
1011 ret = pwc_get_cmos_sensor(pdev, &i);
1012 if (ret >= 0)
1014 switch(i) {
1015 case 0x00: sensor_type = "Hyundai CMOS sensor"; break;
1016 case 0x20: sensor_type = "Sony CCD sensor + TDA8787"; break;
1017 case 0x2E: sensor_type = "Sony CCD sensor + Exas 98L59"; break;
1018 case 0x2F: sensor_type = "Sony CCD sensor + ADI 9804"; break;
1019 case 0x30: sensor_type = "Sharp CCD sensor + TDA8787"; break;
1020 case 0x3E: sensor_type = "Sharp CCD sensor + Exas 98L59"; break;
1021 case 0x3F: sensor_type = "Sharp CCD sensor + ADI 9804"; break;
1022 case 0x40: sensor_type = "UPA 1021 sensor"; break;
1023 case 0x100: sensor_type = "VGA sensor"; break;
1024 case 0x101: sensor_type = "PAL MR sensor"; break;
1025 default: sensor_type = "unknown type of sensor"; break;
1028 if (sensor_type != NULL)
1029 Info("This %s camera is equipped with a %s (%d).\n", pdev->vdev->name, sensor_type, i);
1033 /* Turn on camera */
1034 if (power_save) {
1035 i = pwc_camera_power(pdev, 1);
1036 if (i < 0)
1037 Info("Failed to restore power to the camera! (%d)\n", i);
1039 /* Set LED on/off time */
1040 if (pwc_set_leds(pdev, led_on, led_off) < 0)
1041 Info("Failed to set LED on/off time.\n");
1043 pwc_construct(pdev); /* set min/max sizes correct */
1045 /* So far, so good. Allocate memory. */
1046 i = pwc_allocate_buffers(pdev);
1047 if (i < 0) {
1048 Trace(TRACE_OPEN, "Failed to allocate buffer memory.\n");
1049 up(&pdev->modlock);
1050 return i;
1053 /* Reset buffers & parameters */
1054 pwc_reset_buffers(pdev);
1055 for (i = 0; i < default_mbufs; i++)
1056 pdev->image_used[i] = 0;
1057 pdev->vframe_count = 0;
1058 pdev->vframes_dumped = 0;
1059 pdev->vframes_error = 0;
1060 pdev->visoc_errors = 0;
1061 pdev->error_status = 0;
1062 #if PWC_DEBUG
1063 pdev->sequence = 0;
1064 #endif
1065 pwc_construct(pdev); /* set min/max sizes correct */
1067 /* Set some defaults */
1068 pdev->vsnapshot = 0;
1070 /* Start iso pipe for video; first try the last used video size
1071 (or the default one); if that fails try QCIF/10 or QSIF/10;
1072 it that fails too, give up.
1074 i = pwc_set_video_mode(pdev, pwc_image_sizes[pdev->vsize].x, pwc_image_sizes[pdev->vsize].y, pdev->vframes, pdev->vcompression, 0);
1075 if (i) {
1076 Trace(TRACE_OPEN, "First attempt at set_video_mode failed.\n");
1077 if (pdev->type == 730 || pdev->type == 740 || pdev->type == 750)
1078 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QSIF].x, pwc_image_sizes[PSZ_QSIF].y, 10, pdev->vcompression, 0);
1079 else
1080 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QCIF].x, pwc_image_sizes[PSZ_QCIF].y, 10, pdev->vcompression, 0);
1082 if (i) {
1083 Trace(TRACE_OPEN, "Second attempt at set_video_mode failed.\n");
1084 up(&pdev->modlock);
1085 return i;
1088 i = pwc_isoc_init(pdev);
1089 if (i) {
1090 Trace(TRACE_OPEN, "Failed to init ISOC stuff = %d.\n", i);
1091 up(&pdev->modlock);
1092 return i;
1095 pdev->vopen++;
1096 file->private_data = vdev;
1097 up(&pdev->modlock);
1098 Trace(TRACE_OPEN, "<< video_open() returns 0.\n");
1099 return 0;
1102 /* Note that all cleanup is done in the reverse order as in _open */
1103 static int pwc_video_close(struct inode *inode, struct file *file)
1105 struct video_device *vdev = file->private_data;
1106 struct pwc_device *pdev;
1107 int i;
1109 Trace(TRACE_OPEN, ">> video_close called(vdev = 0x%p).\n", vdev);
1111 pdev = (struct pwc_device *)vdev->priv;
1112 if (pdev->vopen == 0)
1113 Info("video_close() called on closed device?\n");
1115 /* Dump statistics, but only if a reasonable amount of frames were
1116 processed (to prevent endless log-entries in case of snap-shot
1117 programs)
1119 if (pdev->vframe_count > 20)
1120 Info("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev->vframe_count, pdev->vframes_dumped, pdev->vframes_error);
1122 switch (pdev->type)
1124 case 675:
1125 case 680:
1126 case 690:
1127 case 720:
1128 case 730:
1129 case 740:
1130 case 750:
1131 /* pwc_dec23_exit(); *//* Timon & Kiara */
1132 break;
1133 case 645:
1134 case 646:
1135 /* pwc_dec1_exit(); */
1136 break;
1139 pwc_isoc_cleanup(pdev);
1140 pwc_free_buffers(pdev);
1142 /* Turn off LEDS and power down camera, but only when not unplugged */
1143 if (pdev->error_status != EPIPE) {
1144 /* Turn LEDs off */
1145 if (pwc_set_leds(pdev, 0, 0) < 0)
1146 Info("Failed to set LED on/off time.\n");
1147 if (power_save) {
1148 i = pwc_camera_power(pdev, 0);
1149 if (i < 0)
1150 Err("Failed to power down camera (%d)\n", i);
1153 pdev->vopen = 0;
1154 Trace(TRACE_OPEN, "<< video_close()\n");
1155 return 0;
1159 * FIXME: what about two parallel reads ????
1160 * ANSWER: Not supported. You can't open the device more than once,
1161 despite what the V4L1 interface says. First, I don't see
1162 the need, second there's no mechanism of alerting the
1163 2nd/3rd/... process of events like changing image size.
1164 And I don't see the point of blocking that for the
1165 2nd/3rd/... process.
1166 In multi-threaded environments reading parallel from any
1167 device is tricky anyhow.
1170 static ssize_t pwc_video_read(struct file *file, char __user * buf,
1171 size_t count, loff_t *ppos)
1173 struct video_device *vdev = file->private_data;
1174 struct pwc_device *pdev;
1175 int noblock = file->f_flags & O_NONBLOCK;
1176 DECLARE_WAITQUEUE(wait, current);
1177 int bytes_to_read;
1179 Trace(TRACE_READ, "video_read(0x%p, %p, %zu) called.\n", vdev, buf, count);
1180 if (vdev == NULL)
1181 return -EFAULT;
1182 pdev = vdev->priv;
1183 if (pdev == NULL)
1184 return -EFAULT;
1185 if (pdev->error_status)
1186 return -pdev->error_status; /* Something happened, report what. */
1188 /* In case we're doing partial reads, we don't have to wait for a frame */
1189 if (pdev->image_read_pos == 0) {
1190 /* Do wait queueing according to the (doc)book */
1191 add_wait_queue(&pdev->frameq, &wait);
1192 while (pdev->full_frames == NULL) {
1193 /* Check for unplugged/etc. here */
1194 if (pdev->error_status) {
1195 remove_wait_queue(&pdev->frameq, &wait);
1196 set_current_state(TASK_RUNNING);
1197 return -pdev->error_status ;
1199 if (noblock) {
1200 remove_wait_queue(&pdev->frameq, &wait);
1201 set_current_state(TASK_RUNNING);
1202 return -EWOULDBLOCK;
1204 if (signal_pending(current)) {
1205 remove_wait_queue(&pdev->frameq, &wait);
1206 set_current_state(TASK_RUNNING);
1207 return -ERESTARTSYS;
1209 schedule();
1210 set_current_state(TASK_INTERRUPTIBLE);
1212 remove_wait_queue(&pdev->frameq, &wait);
1213 set_current_state(TASK_RUNNING);
1215 /* Decompress and release frame */
1216 if (pwc_handle_frame(pdev))
1217 return -EFAULT;
1220 Trace(TRACE_READ, "Copying data to user space.\n");
1221 if (pdev->vpalette == VIDEO_PALETTE_RAW)
1222 bytes_to_read = pdev->frame_size;
1223 else
1224 bytes_to_read = pdev->view.size;
1226 /* copy bytes to user space; we allow for partial reads */
1227 if (count + pdev->image_read_pos > bytes_to_read)
1228 count = bytes_to_read - pdev->image_read_pos;
1229 if (copy_to_user(buf, pdev->image_ptr[pdev->fill_image] + pdev->image_read_pos, count))
1230 return -EFAULT;
1231 pdev->image_read_pos += count;
1232 if (pdev->image_read_pos >= bytes_to_read) { /* All data has been read */
1233 pdev->image_read_pos = 0;
1234 pwc_next_image(pdev);
1236 return count;
1239 static unsigned int pwc_video_poll(struct file *file, poll_table *wait)
1241 struct video_device *vdev = file->private_data;
1242 struct pwc_device *pdev;
1244 if (vdev == NULL)
1245 return -EFAULT;
1246 pdev = vdev->priv;
1247 if (pdev == NULL)
1248 return -EFAULT;
1250 poll_wait(file, &pdev->frameq, wait);
1251 if (pdev->error_status)
1252 return POLLERR;
1253 if (pdev->full_frames != NULL) /* we have frames waiting */
1254 return (POLLIN | POLLRDNORM);
1256 return 0;
1259 static int pwc_video_do_ioctl(struct inode *inode, struct file *file,
1260 unsigned int cmd, void *arg)
1262 struct video_device *vdev = file->private_data;
1263 struct pwc_device *pdev;
1264 DECLARE_WAITQUEUE(wait, current);
1266 if (vdev == NULL)
1267 return -EFAULT;
1268 pdev = vdev->priv;
1269 if (pdev == NULL)
1270 return -EFAULT;
1272 switch (cmd) {
1273 /* Query cabapilities */
1274 case VIDIOCGCAP:
1276 struct video_capability *caps = arg;
1278 strcpy(caps->name, vdev->name);
1279 caps->type = VID_TYPE_CAPTURE;
1280 caps->channels = 1;
1281 caps->audios = 1;
1282 caps->minwidth = pdev->view_min.x;
1283 caps->minheight = pdev->view_min.y;
1284 caps->maxwidth = pdev->view_max.x;
1285 caps->maxheight = pdev->view_max.y;
1286 break;
1289 /* Channel functions (simulate 1 channel) */
1290 case VIDIOCGCHAN:
1292 struct video_channel *v = arg;
1294 if (v->channel != 0)
1295 return -EINVAL;
1296 v->flags = 0;
1297 v->tuners = 0;
1298 v->type = VIDEO_TYPE_CAMERA;
1299 strcpy(v->name, "Webcam");
1300 return 0;
1303 case VIDIOCSCHAN:
1305 /* The spec says the argument is an integer, but
1306 the bttv driver uses a video_channel arg, which
1307 makes sense becasue it also has the norm flag.
1309 struct video_channel *v = arg;
1310 if (v->channel != 0)
1311 return -EINVAL;
1312 return 0;
1316 /* Picture functions; contrast etc. */
1317 case VIDIOCGPICT:
1319 struct video_picture *p = arg;
1320 int val;
1322 val = pwc_get_brightness(pdev);
1323 if (val >= 0)
1324 p->brightness = val;
1325 else
1326 p->brightness = 0xffff;
1327 val = pwc_get_contrast(pdev);
1328 if (val >= 0)
1329 p->contrast = val;
1330 else
1331 p->contrast = 0xffff;
1332 /* Gamma, Whiteness, what's the difference? :) */
1333 val = pwc_get_gamma(pdev);
1334 if (val >= 0)
1335 p->whiteness = val;
1336 else
1337 p->whiteness = 0xffff;
1338 val = pwc_get_saturation(pdev);
1339 if (val >= 0)
1340 p->colour = val;
1341 else
1342 p->colour = 0xffff;
1343 p->depth = 24;
1344 p->palette = pdev->vpalette;
1345 p->hue = 0xFFFF; /* N/A */
1346 break;
1349 case VIDIOCSPICT:
1351 struct video_picture *p = arg;
1353 * FIXME: Suppose we are mid read
1354 ANSWER: No problem: the firmware of the camera
1355 can handle brightness/contrast/etc
1356 changes at _any_ time, and the palette
1357 is used exactly once in the uncompress
1358 routine.
1360 pwc_set_brightness(pdev, p->brightness);
1361 pwc_set_contrast(pdev, p->contrast);
1362 pwc_set_gamma(pdev, p->whiteness);
1363 pwc_set_saturation(pdev, p->colour);
1364 if (p->palette && p->palette != pdev->vpalette) {
1365 switch (p->palette) {
1366 case VIDEO_PALETTE_YUV420P:
1367 case VIDEO_PALETTE_RAW:
1368 pdev->vpalette = p->palette;
1369 return pwc_try_video_mode(pdev, pdev->image.x, pdev->image.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1370 break;
1371 default:
1372 return -EINVAL;
1373 break;
1376 break;
1379 /* Window/size parameters */
1380 case VIDIOCGWIN:
1382 struct video_window *vw = arg;
1384 vw->x = 0;
1385 vw->y = 0;
1386 vw->width = pdev->view.x;
1387 vw->height = pdev->view.y;
1388 vw->chromakey = 0;
1389 vw->flags = (pdev->vframes << PWC_FPS_SHIFT) |
1390 (pdev->vsnapshot ? PWC_FPS_SNAPSHOT : 0);
1391 break;
1394 case VIDIOCSWIN:
1396 struct video_window *vw = arg;
1397 int fps, snapshot, ret;
1399 fps = (vw->flags & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
1400 snapshot = vw->flags & PWC_FPS_SNAPSHOT;
1401 if (fps == 0)
1402 fps = pdev->vframes;
1403 if (pdev->view.x == vw->width && pdev->view.y && fps == pdev->vframes && snapshot == pdev->vsnapshot)
1404 return 0;
1405 ret = pwc_try_video_mode(pdev, vw->width, vw->height, fps, pdev->vcompression, snapshot);
1406 if (ret)
1407 return ret;
1408 break;
1411 /* We don't have overlay support (yet) */
1412 case VIDIOCGFBUF:
1414 struct video_buffer *vb = arg;
1416 memset(vb,0,sizeof(*vb));
1417 break;
1420 /* mmap() functions */
1421 case VIDIOCGMBUF:
1423 /* Tell the user program how much memory is needed for a mmap() */
1424 struct video_mbuf *vm = arg;
1425 int i;
1427 memset(vm, 0, sizeof(*vm));
1428 vm->size = default_mbufs * pdev->len_per_image;
1429 vm->frames = default_mbufs; /* double buffering should be enough for most applications */
1430 for (i = 0; i < default_mbufs; i++)
1431 vm->offsets[i] = i * pdev->len_per_image;
1432 break;
1435 case VIDIOCMCAPTURE:
1437 /* Start capture into a given image buffer (called 'frame' in video_mmap structure) */
1438 struct video_mmap *vm = arg;
1440 Trace(TRACE_READ, "VIDIOCMCAPTURE: %dx%d, frame %d, format %d\n", vm->width, vm->height, vm->frame, vm->format);
1441 if (vm->frame < 0 || vm->frame >= default_mbufs)
1442 return -EINVAL;
1444 /* xawtv is nasty. It probes the available palettes
1445 by setting a very small image size and trying
1446 various palettes... The driver doesn't support
1447 such small images, so I'm working around it.
1449 if (vm->format)
1451 switch (vm->format)
1453 case VIDEO_PALETTE_YUV420P:
1454 case VIDEO_PALETTE_RAW:
1455 break;
1456 default:
1457 return -EINVAL;
1458 break;
1462 if ((vm->width != pdev->view.x || vm->height != pdev->view.y) &&
1463 (vm->width >= pdev->view_min.x && vm->height >= pdev->view_min.y)) {
1464 int ret;
1466 Trace(TRACE_OPEN, "VIDIOCMCAPTURE: changing size to please xawtv :-(.\n");
1467 ret = pwc_try_video_mode(pdev, vm->width, vm->height, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1468 if (ret)
1469 return ret;
1470 } /* ... size mismatch */
1472 /* FIXME: should we lock here? */
1473 if (pdev->image_used[vm->frame])
1474 return -EBUSY; /* buffer wasn't available. Bummer */
1475 pdev->image_used[vm->frame] = 1;
1477 /* Okay, we're done here. In the SYNC call we wait until a
1478 frame comes available, then expand image into the given
1479 buffer.
1480 In contrast to the CPiA cam the Philips cams deliver a
1481 constant stream, almost like a grabber card. Also,
1482 we have separate buffers for the rawdata and the image,
1483 meaning we can nearly always expand into the requested buffer.
1485 Trace(TRACE_READ, "VIDIOCMCAPTURE done.\n");
1486 break;
1489 case VIDIOCSYNC:
1491 /* The doc says: "Whenever a buffer is used it should
1492 call VIDIOCSYNC to free this frame up and continue."
1494 The only odd thing about this whole procedure is
1495 that MCAPTURE flags the buffer as "in use", and
1496 SYNC immediately unmarks it, while it isn't
1497 after SYNC that you know that the buffer actually
1498 got filled! So you better not start a CAPTURE in
1499 the same frame immediately (use double buffering).
1500 This is not a problem for this cam, since it has
1501 extra intermediate buffers, but a hardware
1502 grabber card will then overwrite the buffer
1503 you're working on.
1505 int *mbuf = arg;
1506 int ret;
1508 Trace(TRACE_READ, "VIDIOCSYNC called (%d).\n", *mbuf);
1510 /* bounds check */
1511 if (*mbuf < 0 || *mbuf >= default_mbufs)
1512 return -EINVAL;
1513 /* check if this buffer was requested anyway */
1514 if (pdev->image_used[*mbuf] == 0)
1515 return -EINVAL;
1517 /* Add ourselves to the frame wait-queue.
1519 FIXME: needs auditing for safety.
1520 QUESTION: In what respect? I think that using the
1521 frameq is safe now.
1523 add_wait_queue(&pdev->frameq, &wait);
1524 while (pdev->full_frames == NULL) {
1525 if (pdev->error_status) {
1526 remove_wait_queue(&pdev->frameq, &wait);
1527 set_current_state(TASK_RUNNING);
1528 return -pdev->error_status;
1531 if (signal_pending(current)) {
1532 remove_wait_queue(&pdev->frameq, &wait);
1533 set_current_state(TASK_RUNNING);
1534 return -ERESTARTSYS;
1536 schedule();
1537 set_current_state(TASK_INTERRUPTIBLE);
1539 remove_wait_queue(&pdev->frameq, &wait);
1540 set_current_state(TASK_RUNNING);
1542 /* The frame is ready. Expand in the image buffer
1543 requested by the user. I don't care if you
1544 mmap() 5 buffers and request data in this order:
1545 buffer 4 2 3 0 1 2 3 0 4 3 1 . . .
1546 Grabber hardware may not be so forgiving.
1548 Trace(TRACE_READ, "VIDIOCSYNC: frame ready.\n");
1549 pdev->fill_image = *mbuf; /* tell in which buffer we want the image to be expanded */
1550 /* Decompress, etc */
1551 ret = pwc_handle_frame(pdev);
1552 pdev->image_used[*mbuf] = 0;
1553 if (ret)
1554 return -EFAULT;
1555 break;
1558 case VIDIOCGAUDIO:
1560 struct video_audio *v = arg;
1562 strcpy(v->name, "Microphone");
1563 v->audio = -1; /* unknown audio minor */
1564 v->flags = 0;
1565 v->mode = VIDEO_SOUND_MONO;
1566 v->volume = 0;
1567 v->bass = 0;
1568 v->treble = 0;
1569 v->balance = 0x8000;
1570 v->step = 1;
1571 break;
1574 case VIDIOCSAUDIO:
1576 /* Dummy: nothing can be set */
1577 break;
1580 case VIDIOCGUNIT:
1582 struct video_unit *vu = arg;
1584 vu->video = pdev->vdev->minor & 0x3F;
1585 vu->audio = -1; /* not known yet */
1586 vu->vbi = -1;
1587 vu->radio = -1;
1588 vu->teletext = -1;
1589 break;
1591 default:
1592 return pwc_ioctl(pdev, cmd, arg);
1593 } /* ..switch */
1594 return 0;
1597 static int pwc_video_ioctl(struct inode *inode, struct file *file,
1598 unsigned int cmd, unsigned long arg)
1600 return video_usercopy(inode, file, cmd, arg, pwc_video_do_ioctl);
1604 static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma)
1606 struct video_device *vdev = file->private_data;
1607 struct pwc_device *pdev;
1608 unsigned long start = vma->vm_start;
1609 unsigned long size = vma->vm_end-vma->vm_start;
1610 unsigned long page, pos;
1612 Trace(TRACE_MEMORY, "mmap(0x%p, 0x%lx, %lu) called.\n", vdev, start, size);
1613 pdev = vdev->priv;
1615 vma->vm_flags |= VM_IO;
1617 pos = (unsigned long)pdev->image_data;
1618 while (size > 0) {
1619 page = vmalloc_to_pfn((void *)pos);
1620 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1621 return -EAGAIN;
1623 start += PAGE_SIZE;
1624 pos += PAGE_SIZE;
1625 if (size > PAGE_SIZE)
1626 size -= PAGE_SIZE;
1627 else
1628 size = 0;
1631 return 0;
1634 /***************************************************************************/
1635 /* USB functions */
1637 /* This function gets called when a new device is plugged in or the usb core
1638 * is loaded.
1641 static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
1643 struct usb_device *udev = interface_to_usbdev(intf);
1644 struct pwc_device *pdev = NULL;
1645 int vendor_id, product_id, type_id;
1646 int i, hint;
1647 int features = 0;
1648 int video_nr = -1; /* default: use next available device */
1649 char serial_number[30], *name;
1651 /* Check if we can handle this device */
1652 Trace(TRACE_PROBE, "probe() called [%04X %04X], if %d\n",
1653 le16_to_cpu(udev->descriptor.idVendor),
1654 le16_to_cpu(udev->descriptor.idProduct),
1655 intf->altsetting->desc.bInterfaceNumber);
1657 /* the interfaces are probed one by one. We are only interested in the
1658 video interface (0) now.
1659 Interface 1 is the Audio Control, and interface 2 Audio itself.
1661 if (intf->altsetting->desc.bInterfaceNumber > 0)
1662 return -ENODEV;
1664 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
1665 product_id = le16_to_cpu(udev->descriptor.idProduct);
1667 if (vendor_id == 0x0471) {
1668 switch (product_id) {
1669 case 0x0302:
1670 Info("Philips PCA645VC USB webcam detected.\n");
1671 name = "Philips 645 webcam";
1672 type_id = 645;
1673 break;
1674 case 0x0303:
1675 Info("Philips PCA646VC USB webcam detected.\n");
1676 name = "Philips 646 webcam";
1677 type_id = 646;
1678 break;
1679 case 0x0304:
1680 Info("Askey VC010 type 2 USB webcam detected.\n");
1681 name = "Askey VC010 webcam";
1682 type_id = 646;
1683 break;
1684 case 0x0307:
1685 Info("Philips PCVC675K (Vesta) USB webcam detected.\n");
1686 name = "Philips 675 webcam";
1687 type_id = 675;
1688 break;
1689 case 0x0308:
1690 Info("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
1691 name = "Philips 680 webcam";
1692 type_id = 680;
1693 break;
1694 case 0x030C:
1695 Info("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
1696 name = "Philips 690 webcam";
1697 type_id = 690;
1698 break;
1699 case 0x0310:
1700 Info("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
1701 name = "Philips 730 webcam";
1702 type_id = 730;
1703 break;
1704 case 0x0311:
1705 Info("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
1706 name = "Philips 740 webcam";
1707 type_id = 740;
1708 break;
1709 case 0x0312:
1710 Info("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
1711 name = "Philips 750 webcam";
1712 type_id = 750;
1713 break;
1714 case 0x0313:
1715 Info("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
1716 name = "Philips 720K/40 webcam";
1717 type_id = 720;
1718 break;
1719 default:
1720 return -ENODEV;
1721 break;
1724 else if (vendor_id == 0x069A) {
1725 switch(product_id) {
1726 case 0x0001:
1727 Info("Askey VC010 type 1 USB webcam detected.\n");
1728 name = "Askey VC010 webcam";
1729 type_id = 645;
1730 break;
1731 default:
1732 return -ENODEV;
1733 break;
1736 else if (vendor_id == 0x046d) {
1737 switch(product_id) {
1738 case 0x08b0:
1739 Info("Logitech QuickCam Pro 3000 USB webcam detected.\n");
1740 name = "Logitech QuickCam Pro 3000";
1741 type_id = 740; /* CCD sensor */
1742 break;
1743 case 0x08b1:
1744 Info("Logitech QuickCam Notebook Pro USB webcam detected.\n");
1745 name = "Logitech QuickCam Notebook Pro";
1746 type_id = 740; /* CCD sensor */
1747 break;
1748 case 0x08b2:
1749 Info("Logitech QuickCam 4000 Pro USB webcam detected.\n");
1750 name = "Logitech QuickCam Pro 4000";
1751 type_id = 740; /* CCD sensor */
1752 break;
1753 case 0x08b3:
1754 Info("Logitech QuickCam Zoom USB webcam detected.\n");
1755 name = "Logitech QuickCam Zoom";
1756 type_id = 740; /* CCD sensor */
1757 break;
1758 case 0x08B4:
1759 Info("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
1760 name = "Logitech QuickCam Zoom";
1761 type_id = 740; /* CCD sensor */
1762 break;
1763 case 0x08b5:
1764 Info("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
1765 name = "Logitech QuickCam Orbit";
1766 type_id = 740; /* CCD sensor */
1767 features |= FEATURE_MOTOR_PANTILT;
1768 break;
1769 case 0x08b6:
1770 case 0x08b7:
1771 case 0x08b8:
1772 Info("Logitech QuickCam detected (reserved ID).\n");
1773 name = "Logitech QuickCam (res.)";
1774 type_id = 730; /* Assuming CMOS */
1775 break;
1776 default:
1777 return -ENODEV;
1778 break;
1781 else if (vendor_id == 0x055d) {
1782 /* I don't know the difference between the C10 and the C30;
1783 I suppose the difference is the sensor, but both cameras
1784 work equally well with a type_id of 675
1786 switch(product_id) {
1787 case 0x9000:
1788 Info("Samsung MPC-C10 USB webcam detected.\n");
1789 name = "Samsung MPC-C10";
1790 type_id = 675;
1791 break;
1792 case 0x9001:
1793 Info("Samsung MPC-C30 USB webcam detected.\n");
1794 name = "Samsung MPC-C30";
1795 type_id = 675;
1796 break;
1797 default:
1798 return -ENODEV;
1799 break;
1802 else if (vendor_id == 0x041e) {
1803 switch(product_id) {
1804 case 0x400c:
1805 Info("Creative Labs Webcam 5 detected.\n");
1806 name = "Creative Labs Webcam 5";
1807 type_id = 730;
1808 break;
1809 case 0x4011:
1810 Info("Creative Labs Webcam Pro Ex detected.\n");
1811 name = "Creative Labs Webcam Pro Ex";
1812 type_id = 740;
1813 break;
1814 default:
1815 return -ENODEV;
1816 break;
1819 else if (vendor_id == 0x04cc) {
1820 switch(product_id) {
1821 case 0x8116:
1822 Info("Sotec Afina Eye USB webcam detected.\n");
1823 name = "Sotec Afina Eye";
1824 type_id = 730;
1825 break;
1826 default:
1827 return -ENODEV;
1828 break;
1831 else if (vendor_id == 0x06be) {
1832 switch(product_id) {
1833 case 0x8116:
1834 /* This is essentially the same cam as the Sotec Afina Eye */
1835 Info("AME Co. Afina Eye USB webcam detected.\n");
1836 name = "AME Co. Afina Eye";
1837 type_id = 750;
1838 break;
1839 default:
1840 return -ENODEV;
1841 break;
1845 else if (vendor_id == 0x0d81) {
1846 switch(product_id) {
1847 case 0x1900:
1848 Info("Visionite VCS-UC300 USB webcam detected.\n");
1849 name = "Visionite VCS-UC300";
1850 type_id = 740; /* CCD sensor */
1851 break;
1852 case 0x1910:
1853 Info("Visionite VCS-UM100 USB webcam detected.\n");
1854 name = "Visionite VCS-UM100";
1855 type_id = 730; /* CMOS sensor */
1856 break;
1857 default:
1858 return -ENODEV;
1859 break;
1862 else
1863 return -ENODEV; /* Not any of the know types; but the list keeps growing. */
1865 memset(serial_number, 0, 30);
1866 usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
1867 Trace(TRACE_PROBE, "Device serial number is %s\n", serial_number);
1869 if (udev->descriptor.bNumConfigurations > 1)
1870 Info("Warning: more than 1 configuration available.\n");
1872 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1873 pdev = kmalloc(sizeof(struct pwc_device), GFP_KERNEL);
1874 if (pdev == NULL) {
1875 Err("Oops, could not allocate memory for pwc_device.\n");
1876 return -ENOMEM;
1878 memset(pdev, 0, sizeof(struct pwc_device));
1879 pdev->type = type_id;
1880 pdev->vsize = default_size;
1881 pdev->vframes = default_fps;
1882 strcpy(pdev->serial, serial_number);
1883 pdev->features = features;
1884 if (vendor_id == 0x046D && product_id == 0x08B5)
1886 /* Logitech QuickCam Orbit
1887 The ranges have been determined experimentally; they may differ from cam to cam.
1888 Also, the exact ranges left-right and up-down are different for my cam
1890 pdev->angle_range.pan_min = -7000;
1891 pdev->angle_range.pan_max = 7000;
1892 pdev->angle_range.tilt_min = -3000;
1893 pdev->angle_range.tilt_max = 2500;
1896 init_MUTEX(&pdev->modlock);
1897 spin_lock_init(&pdev->ptrlock);
1899 pdev->udev = udev;
1900 init_waitqueue_head(&pdev->frameq);
1901 pdev->vcompression = pwc_preferred_compression;
1903 /* Allocate video_device structure */
1904 pdev->vdev = video_device_alloc();
1905 if (pdev->vdev == 0)
1907 Err("Err, cannot allocate video_device struture. Failing probe.");
1908 kfree(pdev);
1909 return -ENOMEM;
1911 memcpy(pdev->vdev, &pwc_template, sizeof(pwc_template));
1912 strcpy(pdev->vdev->name, name);
1913 pdev->vdev->owner = THIS_MODULE;
1914 video_set_drvdata(pdev->vdev, pdev);
1916 pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1917 Trace(TRACE_PROBE, "Release: %04x\n", pdev->release);
1919 /* Now search device_hint[] table for a match, so we can hint a node number. */
1920 for (hint = 0; hint < MAX_DEV_HINTS; hint++) {
1921 if (((device_hint[hint].type == -1) || (device_hint[hint].type == pdev->type)) &&
1922 (device_hint[hint].pdev == NULL)) {
1923 /* so far, so good... try serial number */
1924 if ((device_hint[hint].serial_number[0] == '*') || !strcmp(device_hint[hint].serial_number, serial_number)) {
1925 /* match! */
1926 video_nr = device_hint[hint].device_node;
1927 Trace(TRACE_PROBE, "Found hint, will try to register as /dev/video%d\n", video_nr);
1928 break;
1933 pdev->vdev->release = video_device_release;
1934 i = video_register_device(pdev->vdev, VFL_TYPE_GRABBER, video_nr);
1935 if (i < 0) {
1936 Err("Failed to register as video device (%d).\n", i);
1937 video_device_release(pdev->vdev); /* Drip... drip... drip... */
1938 kfree(pdev); /* Oops, no memory leaks please */
1939 return -EIO;
1941 else {
1942 Info("Registered as /dev/video%d.\n", pdev->vdev->minor & 0x3F);
1945 /* occupy slot */
1946 if (hint < MAX_DEV_HINTS)
1947 device_hint[hint].pdev = pdev;
1949 Trace(TRACE_PROBE, "probe() function returning struct at 0x%p.\n", pdev);
1950 usb_set_intfdata (intf, pdev);
1951 return 0;
1954 /* The user janked out the cable... */
1955 static void usb_pwc_disconnect(struct usb_interface *intf)
1957 struct pwc_device *pdev;
1958 int hint;
1960 lock_kernel();
1961 pdev = usb_get_intfdata (intf);
1962 usb_set_intfdata (intf, NULL);
1963 if (pdev == NULL) {
1964 Err("pwc_disconnect() Called without private pointer.\n");
1965 goto disconnect_out;
1967 if (pdev->udev == NULL) {
1968 Err("pwc_disconnect() already called for %p\n", pdev);
1969 goto disconnect_out;
1971 if (pdev->udev != interface_to_usbdev(intf)) {
1972 Err("pwc_disconnect() Woops: pointer mismatch udev/pdev.\n");
1973 goto disconnect_out;
1975 #ifdef PWC_MAGIC
1976 if (pdev->magic != PWC_MAGIC) {
1977 Err("pwc_disconnect() Magic number failed. Consult your scrolls and try again.\n");
1978 goto disconnect_out;
1980 #endif
1982 /* We got unplugged; this is signalled by an EPIPE error code */
1983 if (pdev->vopen) {
1984 Info("Disconnected while webcam is in use!\n");
1985 pdev->error_status = EPIPE;
1988 /* Alert waiting processes */
1989 wake_up_interruptible(&pdev->frameq);
1990 /* Wait until device is closed */
1991 while (pdev->vopen)
1992 schedule();
1993 /* Device is now closed, so we can safely unregister it */
1994 Trace(TRACE_PROBE, "Unregistering video device in disconnect().\n");
1995 video_unregister_device(pdev->vdev);
1997 /* Free memory (don't set pdev to 0 just yet) */
1998 kfree(pdev);
2000 disconnect_out:
2001 /* search device_hint[] table if we occupy a slot, by any chance */
2002 for (hint = 0; hint < MAX_DEV_HINTS; hint++)
2003 if (device_hint[hint].pdev == pdev)
2004 device_hint[hint].pdev = NULL;
2006 unlock_kernel();
2010 /* *grunt* We have to do atoi ourselves :-( */
2011 static int pwc_atoi(const char *s)
2013 int k = 0;
2015 k = 0;
2016 while (*s != '\0' && *s >= '0' && *s <= '9') {
2017 k = 10 * k + (*s - '0');
2018 s++;
2020 return k;
2025 * Initialization code & module stuff
2028 static char size[10];
2029 static int fps = 0;
2030 static int fbufs = 0;
2031 static int mbufs = 0;
2032 static int trace = -1;
2033 static int compression = -1;
2034 static int leds[2] = { -1, -1 };
2035 static char *dev_hint[MAX_DEV_HINTS] = { };
2037 module_param_string(size, size, sizeof(size), 0);
2038 MODULE_PARM_DESC(size, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
2039 module_param(fps, int, 0000);
2040 MODULE_PARM_DESC(fps, "Initial frames per second. Varies with model, useful range 5-30");
2041 module_param(fbufs, int, 0000);
2042 MODULE_PARM_DESC(fbufs, "Number of internal frame buffers to reserve");
2043 module_param(mbufs, int, 0000);
2044 MODULE_PARM_DESC(mbufs, "Number of external (mmap()ed) image buffers");
2045 module_param(trace, int, 0000);
2046 MODULE_PARM_DESC(trace, "For debugging purposes");
2047 module_param(power_save, bool, 0000);
2048 MODULE_PARM_DESC(power_save, "Turn power save feature in camera on or off");
2049 module_param(compression, int, 0000);
2050 MODULE_PARM_DESC(compression, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
2051 module_param_array(leds, int, NULL, 0000);
2052 MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
2053 module_param_array(dev_hint, charp, NULL, 0000);
2054 MODULE_PARM_DESC(dev_hint, "Device node hints");
2056 MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
2057 MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
2058 MODULE_LICENSE("GPL");
2060 static int __init usb_pwc_init(void)
2062 int i, sz;
2063 char *sizenames[PSZ_MAX] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };
2065 Info("Philips webcam module version " PWC_VERSION " loaded.\n");
2066 Info("Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.\n");
2067 Info("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
2068 Info("the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
2070 if (fps) {
2071 if (fps < 4 || fps > 30) {
2072 Err("Framerate out of bounds (4-30).\n");
2073 return -EINVAL;
2075 default_fps = fps;
2076 Info("Default framerate set to %d.\n", default_fps);
2079 if (size[0]) {
2080 /* string; try matching with array */
2081 for (sz = 0; sz < PSZ_MAX; sz++) {
2082 if (!strcmp(sizenames[sz], size)) { /* Found! */
2083 default_size = sz;
2084 break;
2087 if (sz == PSZ_MAX) {
2088 Err("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
2089 return -EINVAL;
2091 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);
2093 if (mbufs) {
2094 if (mbufs < 1 || mbufs > MAX_IMAGES) {
2095 Err("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES);
2096 return -EINVAL;
2098 default_mbufs = mbufs;
2099 Info("Number of image buffers set to %d.\n", default_mbufs);
2101 if (fbufs) {
2102 if (fbufs < 2 || fbufs > MAX_FRAMES) {
2103 Err("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES);
2104 return -EINVAL;
2106 default_fbufs = fbufs;
2107 Info("Number of frame buffers set to %d.\n", default_fbufs);
2109 if (trace >= 0) {
2110 Info("Trace options: 0x%04x\n", trace);
2111 pwc_trace = trace;
2113 if (compression >= 0) {
2114 if (compression > 3) {
2115 Err("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
2116 return -EINVAL;
2118 pwc_preferred_compression = compression;
2119 Info("Preferred compression set to %d.\n", pwc_preferred_compression);
2121 if (power_save)
2122 Info("Enabling power save on open/close.\n");
2123 if (leds[0] >= 0)
2124 led_on = leds[0];
2125 if (leds[1] >= 0)
2126 led_off = leds[1];
2128 /* Big device node whoopla. Basically, it allows you to assign a
2129 device node (/dev/videoX) to a camera, based on its type
2130 & serial number. The format is [type[.serialnumber]:]node.
2132 Any camera that isn't matched by these rules gets the next
2133 available free device node.
2135 for (i = 0; i < MAX_DEV_HINTS; i++) {
2136 char *s, *colon, *dot;
2138 /* This loop also initializes the array */
2139 device_hint[i].pdev = NULL;
2140 s = dev_hint[i];
2141 if (s != NULL && *s != '\0') {
2142 device_hint[i].type = -1; /* wildcard */
2143 strcpy(device_hint[i].serial_number, "*");
2145 /* parse string: chop at ':' & '/' */
2146 colon = dot = s;
2147 while (*colon != '\0' && *colon != ':')
2148 colon++;
2149 while (*dot != '\0' && *dot != '.')
2150 dot++;
2151 /* Few sanity checks */
2152 if (*dot != '\0' && dot > colon) {
2153 Err("Malformed camera hint: the colon must be after the dot.\n");
2154 return -EINVAL;
2157 if (*colon == '\0') {
2158 /* No colon */
2159 if (*dot != '\0') {
2160 Err("Malformed camera hint: no colon + device node given.\n");
2161 return -EINVAL;
2163 else {
2164 /* No type or serial number specified, just a number. */
2165 device_hint[i].device_node = pwc_atoi(s);
2168 else {
2169 /* There's a colon, so we have at least a type and a device node */
2170 device_hint[i].type = pwc_atoi(s);
2171 device_hint[i].device_node = pwc_atoi(colon + 1);
2172 if (*dot != '\0') {
2173 /* There's a serial number as well */
2174 int k;
2176 dot++;
2177 k = 0;
2178 while (*dot != ':' && k < 29) {
2179 device_hint[i].serial_number[k++] = *dot;
2180 dot++;
2182 device_hint[i].serial_number[k] = '\0';
2185 #if PWC_DEBUG
2186 Debug("device_hint[%d]:\n", i);
2187 Debug(" type : %d\n", device_hint[i].type);
2188 Debug(" serial# : %s\n", device_hint[i].serial_number);
2189 Debug(" node : %d\n", device_hint[i].device_node);
2190 #endif
2192 else
2193 device_hint[i].type = 0; /* not filled */
2194 } /* ..for MAX_DEV_HINTS */
2196 Trace(TRACE_PROBE, "Registering driver at address 0x%p.\n", &pwc_driver);
2197 return usb_register(&pwc_driver);
2200 static void __exit usb_pwc_exit(void)
2202 Trace(TRACE_MODULE, "Deregistering driver.\n");
2203 usb_deregister(&pwc_driver);
2204 Info("Philips webcam module removed.\n");
2207 module_init(usb_pwc_init);
2208 module_exit(usb_pwc_exit);