1 /* Linux driver for Philips webcam
2 USB and Video4Linux interface part.
3 (C) 1999-2004 Nemosoft Unv.
4 (C) 2004 Luc Saillard (luc@saillard.org)
6 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
7 driver and thus may have bugs that are not present in the original version.
8 Please send bug reports and support requests to <luc@saillard.org>.
9 The decompression routines have been implemented by reverse-engineering the
10 Nemosoft binary pwcx module. Caveat emptor.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 This code forms the interface between the USB layers and the Philips
30 specific stuff. Some adanved stuff of the driver falls under an
31 NDA, signed between me and Philips B.V., Eindhoven, the Netherlands, and
32 is thus not distributed in source form. The binary pwcx.o module
33 contains the code that falls under the NDA.
35 In case you're wondering: 'pwc' stands for "Philips WebCam", but
36 I really didn't want to type 'philips_web_cam' every time (I'm lazy as
37 any Linux kernel hacker, but I don't like uncomprehensible abbreviations
40 Oh yes, convention: to disctinguish between all the various pointers to
41 device-structures, I use these names for the pointer variables:
42 udev: struct usb_device *
43 vdev: struct video_device *
44 pdev: struct pwc_devive *
48 - Alvarado: adding whitebalance code
49 - Alistar Moire: QuickCam 3000 Pro device/product ID
50 - Tony Hoyle: Creative Labs Webcam 5 device/product ID
51 - Mark Burazin: solving hang in VIDIOCSYNC when camera gets unplugged
52 - Jk Fang: Sotec Afina Eye ID
53 - Xavier Roche: QuickCam Pro 4000 ID
54 - Jens Knudsen: QuickCam Zoom ID
55 - J. Debert: QuickCam for Notebooks ID
58 #include <linux/errno.h>
59 #include <linux/init.h>
61 #include <linux/module.h>
62 #include <linux/poll.h>
63 #include <linux/slab.h>
64 #include <linux/vmalloc.h>
68 #include "pwc-ioctl.h"
69 #include "pwc-kiara.h"
70 #include "pwc-timon.h"
71 #include "pwc-dec23.h"
73 #include "pwc-uncompress.h"
75 /* Function prototypes and driver templates */
77 /* hotplug device table support */
78 static struct usb_device_id pwc_device_table
[] = {
79 { USB_DEVICE(0x0471, 0x0302) }, /* Philips models */
80 { USB_DEVICE(0x0471, 0x0303) },
81 { USB_DEVICE(0x0471, 0x0304) },
82 { USB_DEVICE(0x0471, 0x0307) },
83 { USB_DEVICE(0x0471, 0x0308) },
84 { USB_DEVICE(0x0471, 0x030C) },
85 { USB_DEVICE(0x0471, 0x0310) },
86 { USB_DEVICE(0x0471, 0x0311) },
87 { USB_DEVICE(0x0471, 0x0312) },
88 { USB_DEVICE(0x0471, 0x0313) }, /* the 'new' 720K */
89 { USB_DEVICE(0x069A, 0x0001) }, /* Askey */
90 { USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam Pro 3000 */
91 { USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
92 { USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam Pro 4000 */
93 { USB_DEVICE(0x046D, 0x08B3) }, /* Logitech QuickCam Zoom (old model) */
94 { USB_DEVICE(0x046D, 0x08B4) }, /* Logitech QuickCam Zoom (new model) */
95 { USB_DEVICE(0x046D, 0x08B5) }, /* Logitech QuickCam Orbit/Sphere */
96 { USB_DEVICE(0x046D, 0x08B6) }, /* Logitech (reserved) */
97 { USB_DEVICE(0x046D, 0x08B7) }, /* Logitech (reserved) */
98 { USB_DEVICE(0x046D, 0x08B8) }, /* Logitech (reserved) */
99 { USB_DEVICE(0x055D, 0x9000) }, /* Samsung */
100 { USB_DEVICE(0x055D, 0x9001) },
101 { USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
102 { USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
103 { USB_DEVICE(0x04CC, 0x8116) }, /* Afina Eye */
104 { USB_DEVICE(0x06BE, 0x8116) }, /* new Afina Eye */
105 { USB_DEVICE(0x0d81, 0x1910) }, /* Visionite */
106 { USB_DEVICE(0x0d81, 0x1900) },
109 MODULE_DEVICE_TABLE(usb
, pwc_device_table
);
111 static int usb_pwc_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
);
112 static void usb_pwc_disconnect(struct usb_interface
*intf
);
114 static struct usb_driver pwc_driver
= {
115 .owner
= THIS_MODULE
,
116 .name
= "Philips webcam", /* name */
117 .id_table
= pwc_device_table
,
118 .probe
= usb_pwc_probe
, /* probe() */
119 .disconnect
= usb_pwc_disconnect
, /* disconnect() */
122 #define MAX_DEV_HINTS 20
123 #define MAX_ISOC_ERRORS 20
125 static int default_size
= PSZ_QCIF
;
126 static int default_fps
= 10;
127 static int default_fbufs
= 3; /* Default number of frame buffers */
128 static int default_mbufs
= 2; /* Default number of mmap() buffers */
129 int pwc_trace
= TRACE_MODULE
| TRACE_FLOW
| TRACE_PWCX
;
130 static int power_save
= 0;
131 static int led_on
= 100, led_off
= 0; /* defaults to LED that is on while in use */
132 static int pwc_preferred_compression
= 2; /* 0..3 = uncompressed..high */
135 char serial_number
[30];
137 struct pwc_device
*pdev
;
138 } device_hint
[MAX_DEV_HINTS
];
142 static int pwc_video_open(struct inode
*inode
, struct file
*file
);
143 static int pwc_video_close(struct inode
*inode
, struct file
*file
);
144 static ssize_t
pwc_video_read(struct file
*file
, char __user
* buf
,
145 size_t count
, loff_t
*ppos
);
146 static unsigned int pwc_video_poll(struct file
*file
, poll_table
*wait
);
147 static int pwc_video_ioctl(struct inode
*inode
, struct file
*file
,
148 unsigned int ioctlnr
, unsigned long arg
);
149 static int pwc_video_mmap(struct file
*file
, struct vm_area_struct
*vma
);
151 static struct file_operations pwc_fops
= {
152 .owner
= THIS_MODULE
,
153 .open
= pwc_video_open
,
154 .release
= pwc_video_close
,
155 .read
= pwc_video_read
,
156 .poll
= pwc_video_poll
,
157 .mmap
= pwc_video_mmap
,
158 .ioctl
= pwc_video_ioctl
,
161 static struct video_device pwc_template
= {
162 .owner
= THIS_MODULE
,
163 .name
= "Philips Webcam", /* Filled in later */
164 .type
= VID_TYPE_CAPTURE
,
165 .hardware
= VID_HARDWARE_PWC
,
166 .release
= video_device_release
,
171 /***************************************************************************/
173 /* Okay, this is some magic that I worked out and the reasoning behind it...
175 The biggest problem with any USB device is of course: "what to do
176 when the user unplugs the device while it is in use by an application?"
177 We have several options:
178 1) Curse them with the 7 plagues when they do (requires divine intervention)
179 2) Tell them not to (won't work: they'll do it anyway)
180 3) Oops the kernel (this will have a negative effect on a user's uptime)
181 4) Do something sensible.
183 Of course, we go for option 4.
185 It happens that this device will be linked to two times, once from
186 usb_device and once from the video_device in their respective 'private'
187 pointers. This is done when the device is probed() and all initialization
188 succeeded. The pwc_device struct links back to both structures.
190 When a device is unplugged while in use it will be removed from the
191 list of known USB devices; I also de-register it as a V4L device, but
192 unfortunately I can't free the memory since the struct is still in use
193 by the file descriptor. This free-ing is then deferend until the first
194 opportunity. Crude, but it works.
196 A small 'advantage' is that if a user unplugs the cam and plugs it back
197 in, it should get assigned the same video device minor, but unfortunately
198 it's non-trivial to re-link the cam back to the video device... (that
199 would surely be magic! :))
202 /***************************************************************************/
203 /* Private functions */
205 /* Here we want the physical address of the memory.
206 * This is used when initializing the contents of the area.
208 static inline unsigned long kvirt_to_pa(unsigned long adr
)
210 unsigned long kva
, ret
;
212 kva
= (unsigned long) page_address(vmalloc_to_page((void *)adr
));
213 kva
|= adr
& (PAGE_SIZE
-1); /* restore the offset */
218 static void * rvmalloc(unsigned long size
)
223 size
=PAGE_ALIGN(size
);
224 mem
=vmalloc_32(size
);
227 memset(mem
, 0, size
); /* Clear the ram out, no junk to the user */
228 adr
=(unsigned long) mem
;
231 SetPageReserved(vmalloc_to_page((void *)adr
));
239 static void rvfree(void * mem
, unsigned long size
)
245 adr
=(unsigned long) mem
;
246 while ((long) size
> 0)
248 ClearPageReserved(vmalloc_to_page((void *)adr
));
259 static int pwc_allocate_buffers(struct pwc_device
*pdev
)
264 Trace(TRACE_MEMORY
, ">> pwc_allocate_buffers(pdev = 0x%p)\n", pdev
);
270 if (pdev
->magic
!= PWC_MAGIC
) {
271 Err("allocate_buffers(): magic failed.\n");
275 /* Allocate Isochronous pipe buffers */
276 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
277 if (pdev
->sbuf
[i
].data
== NULL
) {
278 kbuf
= kmalloc(ISO_BUFFER_SIZE
, GFP_KERNEL
);
280 Err("Failed to allocate iso buffer %d.\n", i
);
283 Trace(TRACE_MEMORY
, "Allocated iso buffer at %p.\n", kbuf
);
284 pdev
->sbuf
[i
].data
= kbuf
;
285 memset(kbuf
, 0, ISO_BUFFER_SIZE
);
289 /* Allocate frame buffer structure */
290 if (pdev
->fbuf
== NULL
) {
291 kbuf
= kmalloc(default_fbufs
* sizeof(struct pwc_frame_buf
), GFP_KERNEL
);
293 Err("Failed to allocate frame buffer structure.\n");
296 Trace(TRACE_MEMORY
, "Allocated frame buffer structure at %p.\n", kbuf
);
298 memset(kbuf
, 0, default_fbufs
* sizeof(struct pwc_frame_buf
));
300 /* create frame buffers, and make circular ring */
301 for (i
= 0; i
< default_fbufs
; i
++) {
302 if (pdev
->fbuf
[i
].data
== NULL
) {
303 kbuf
= vmalloc(PWC_FRAME_SIZE
); /* need vmalloc since frame buffer > 128K */
305 Err("Failed to allocate frame buffer %d.\n", i
);
308 Trace(TRACE_MEMORY
, "Allocated frame buffer %d at %p.\n", i
, kbuf
);
309 pdev
->fbuf
[i
].data
= kbuf
;
310 memset(kbuf
, 128, PWC_FRAME_SIZE
);
314 /* Allocate decompressor table space */
325 Trace(TRACE_MEMORY
,"private_data(%zu)\n",sizeof(struct pwc_dec23_private
));
326 kbuf
= kmalloc(sizeof(struct pwc_dec23_private
), GFP_KERNEL
); /* Timon & Kiara */
331 kbuf
= kmalloc(sizeof(struct pwc_dec23_private
), GFP_KERNEL
);
335 Err("Failed to allocate decompress table.\n");
338 pdev
->decompress_data
= kbuf
;
340 /* Allocate image buffer; double buffer for mmap() */
341 kbuf
= rvmalloc(default_mbufs
* pdev
->len_per_image
);
343 Err("Failed to allocate image buffer(s). needed (%d)\n",default_mbufs
* pdev
->len_per_image
);
346 Trace(TRACE_MEMORY
, "Allocated image buffer at %p.\n", kbuf
);
347 pdev
->image_data
= kbuf
;
348 for (i
= 0; i
< default_mbufs
; i
++)
349 pdev
->image_ptr
[i
] = kbuf
+ i
* pdev
->len_per_image
;
350 for (; i
< MAX_IMAGES
; i
++)
351 pdev
->image_ptr
[i
] = NULL
;
355 Trace(TRACE_MEMORY
, "<< pwc_allocate_buffers()\n");
359 static void pwc_free_buffers(struct pwc_device
*pdev
)
363 Trace(TRACE_MEMORY
, "Entering free_buffers(%p).\n", pdev
);
368 if (pdev
->magic
!= PWC_MAGIC
) {
369 Err("free_buffers(): magic failed.\n");
374 /* Release Iso-pipe buffers */
375 for (i
= 0; i
< MAX_ISO_BUFS
; i
++)
376 if (pdev
->sbuf
[i
].data
!= NULL
) {
377 Trace(TRACE_MEMORY
, "Freeing ISO buffer at %p.\n", pdev
->sbuf
[i
].data
);
378 kfree(pdev
->sbuf
[i
].data
);
379 pdev
->sbuf
[i
].data
= NULL
;
382 /* The same for frame buffers */
383 if (pdev
->fbuf
!= NULL
) {
384 for (i
= 0; i
< default_fbufs
; i
++) {
385 if (pdev
->fbuf
[i
].data
!= NULL
) {
386 Trace(TRACE_MEMORY
, "Freeing frame buffer %d at %p.\n", i
, pdev
->fbuf
[i
].data
);
387 vfree(pdev
->fbuf
[i
].data
);
388 pdev
->fbuf
[i
].data
= NULL
;
395 /* Intermediate decompression buffer & tables */
396 if (pdev
->decompress_data
!= NULL
) {
397 Trace(TRACE_MEMORY
, "Freeing decompression buffer at %p.\n", pdev
->decompress_data
);
398 kfree(pdev
->decompress_data
);
399 pdev
->decompress_data
= NULL
;
401 pdev
->decompressor
= NULL
;
403 /* Release image buffers */
404 if (pdev
->image_data
!= NULL
) {
405 Trace(TRACE_MEMORY
, "Freeing image buffer at %p.\n", pdev
->image_data
);
406 rvfree(pdev
->image_data
, default_mbufs
* pdev
->len_per_image
);
408 pdev
->image_data
= NULL
;
410 Trace(TRACE_MEMORY
, "Leaving free_buffers().\n");
413 /* The frame & image buffer mess.
415 Yes, this is a mess. Well, it used to be simple, but alas... In this
416 module, 3 buffers schemes are used to get the data from the USB bus to
417 the user program. The first scheme involves the ISO buffers (called thus
418 since they transport ISO data from the USB controller), and not really
419 interesting. Suffices to say the data from this buffer is quickly
420 gathered in an interrupt handler (pwc_isoc_handler) and placed into the
423 The frame buffer is the second scheme, and is the central element here.
424 It collects the data from a single frame from the camera (hence, the
425 name). Frames are delimited by the USB camera with a short USB packet,
426 so that's easy to detect. The frame buffers form a list that is filled
427 by the camera+USB controller and drained by the user process through
428 either read() or mmap().
430 The image buffer is the third scheme, in which frames are decompressed
431 and converted into planar format. For mmap() there is more than
432 one image buffer available.
434 The frame buffers provide the image buffering. In case the user process
435 is a bit slow, this introduces lag and some undesired side-effects.
436 The problem arises when the frame buffer is full. I used to drop the last
437 frame, which makes the data in the queue stale very quickly. But dropping
438 the frame at the head of the queue proved to be a litte bit more difficult.
439 I tried a circular linked scheme, but this introduced more problems than
442 Because filling and draining are completely asynchronous processes, this
443 requires some fiddling with pointers and mutexes.
445 Eventually, I came up with a system with 2 lists: an 'empty' frame list
446 and a 'full' frame list:
447 * Initially, all frame buffers but one are on the 'empty' list; the one
448 remaining buffer is our initial fill frame.
449 * If a frame is needed for filling, we try to take it from the 'empty'
450 list, unless that list is empty, in which case we take the buffer at
451 the head of the 'full' list.
452 * When our fill buffer has been filled, it is appended to the 'full'
454 * If a frame is needed by read() or mmap(), it is taken from the head of
455 the 'full' list, handled, and then appended to the 'empty' list. If no
456 buffer is present on the 'full' list, we wait.
457 The advantage is that the buffer that is currently being decompressed/
458 converted, is on neither list, and thus not in our way (any other scheme
459 I tried had the problem of old data lingering in the queue).
461 Whatever strategy you choose, it always remains a tradeoff: with more
462 frame buffers the chances of a missed frame are reduced. On the other
463 hand, on slower machines it introduces lag because the queue will
468 \brief Find next frame buffer to fill. Take from empty or full list, whichever comes first.
470 static inline int pwc_next_fill_frame(struct pwc_device
*pdev
)
476 spin_lock_irqsave(&pdev
->ptrlock
, flags
);
477 if (pdev
->fill_frame
!= NULL
) {
478 /* append to 'full' list */
479 if (pdev
->full_frames
== NULL
) {
480 pdev
->full_frames
= pdev
->fill_frame
;
481 pdev
->full_frames_tail
= pdev
->full_frames
;
484 pdev
->full_frames_tail
->next
= pdev
->fill_frame
;
485 pdev
->full_frames_tail
= pdev
->fill_frame
;
488 if (pdev
->empty_frames
!= NULL
) {
489 /* We have empty frames available. That's easy */
490 pdev
->fill_frame
= pdev
->empty_frames
;
491 pdev
->empty_frames
= pdev
->empty_frames
->next
;
494 /* Hmm. Take it from the full list */
497 if (pdev
->full_frames
== NULL
) {
498 Err("Neither empty or full frames available!\n");
499 spin_unlock_irqrestore(&pdev
->ptrlock
, flags
);
503 pdev
->fill_frame
= pdev
->full_frames
;
504 pdev
->full_frames
= pdev
->full_frames
->next
;
507 pdev
->fill_frame
->next
= NULL
;
509 Trace(TRACE_SEQUENCE
, "Assigning sequence number %d.\n", pdev
->sequence
);
510 pdev
->fill_frame
->sequence
= pdev
->sequence
++;
512 spin_unlock_irqrestore(&pdev
->ptrlock
, flags
);
518 \brief Reset all buffers, pointers and lists, except for the image_used[] buffer.
520 If the image_used[] buffer is cleared too, mmap()/VIDIOCSYNC will run into trouble.
522 static void pwc_reset_buffers(struct pwc_device
*pdev
)
527 spin_lock_irqsave(&pdev
->ptrlock
, flags
);
528 pdev
->full_frames
= NULL
;
529 pdev
->full_frames_tail
= NULL
;
530 for (i
= 0; i
< default_fbufs
; i
++) {
531 pdev
->fbuf
[i
].filled
= 0;
533 pdev
->fbuf
[i
].next
= &pdev
->fbuf
[i
- 1];
535 pdev
->fbuf
->next
= NULL
;
537 pdev
->empty_frames
= &pdev
->fbuf
[default_fbufs
- 1];
538 pdev
->empty_frames_tail
= pdev
->fbuf
;
539 pdev
->read_frame
= NULL
;
540 pdev
->fill_frame
= pdev
->empty_frames
;
541 pdev
->empty_frames
= pdev
->empty_frames
->next
;
543 pdev
->image_read_pos
= 0;
544 pdev
->fill_image
= 0;
545 spin_unlock_irqrestore(&pdev
->ptrlock
, flags
);
550 \brief Do all the handling for getting one frame: get pointer, decompress, advance pointers.
552 static int pwc_handle_frame(struct pwc_device
*pdev
)
557 spin_lock_irqsave(&pdev
->ptrlock
, flags
);
558 /* First grab our read_frame; this is removed from all lists, so
559 we can release the lock after this without problems */
560 if (pdev
->read_frame
!= NULL
) {
561 /* This can't theoretically happen */
562 Err("Huh? Read frame still in use?\n");
565 if (pdev
->full_frames
== NULL
) {
566 Err("Woops. No frames ready.\n");
569 pdev
->read_frame
= pdev
->full_frames
;
570 pdev
->full_frames
= pdev
->full_frames
->next
;
571 pdev
->read_frame
->next
= NULL
;
574 if (pdev
->read_frame
!= NULL
) {
576 Trace(TRACE_SEQUENCE
, "Decompressing frame %d\n", pdev
->read_frame
->sequence
);
578 /* Decompression is a lenghty process, so it's outside of the lock.
579 This gives the isoc_handler the opportunity to fill more frames
582 spin_unlock_irqrestore(&pdev
->ptrlock
, flags
);
583 ret
= pwc_decompress(pdev
);
584 spin_lock_irqsave(&pdev
->ptrlock
, flags
);
586 /* We're done with read_buffer, tack it to the end of the empty buffer list */
587 if (pdev
->empty_frames
== NULL
) {
588 pdev
->empty_frames
= pdev
->read_frame
;
589 pdev
->empty_frames_tail
= pdev
->empty_frames
;
592 pdev
->empty_frames_tail
->next
= pdev
->read_frame
;
593 pdev
->empty_frames_tail
= pdev
->read_frame
;
595 pdev
->read_frame
= NULL
;
598 spin_unlock_irqrestore(&pdev
->ptrlock
, flags
);
603 \brief Advance pointers of image buffer (after each user request)
605 static inline void pwc_next_image(struct pwc_device
*pdev
)
607 pdev
->image_used
[pdev
->fill_image
] = 0;
608 pdev
->fill_image
= (pdev
->fill_image
+ 1) % default_mbufs
;
612 /* This gets called for the Isochronous pipe (video). This is done in
613 * interrupt time, so it has to be fast, not crash, and not stall. Neat.
615 static void pwc_isoc_handler(struct urb
*urb
, struct pt_regs
*regs
)
617 struct pwc_device
*pdev
;
620 struct pwc_frame_buf
*fbuf
;
621 unsigned char *fillptr
= NULL
, *iso_buf
= NULL
;
624 pdev
= (struct pwc_device
*)urb
->context
;
626 Err("isoc_handler() called with NULL device?!\n");
630 if (pdev
->magic
!= PWC_MAGIC
) {
631 Err("isoc_handler() called with bad magic!\n");
635 if (urb
->status
== -ENOENT
|| urb
->status
== -ECONNRESET
) {
636 Trace(TRACE_OPEN
, "pwc_isoc_handler(): URB (%p) unlinked %ssynchronuously.\n", urb
, urb
->status
== -ENOENT
? "" : "a");
639 if (urb
->status
!= -EINPROGRESS
&& urb
->status
!= 0) {
643 switch(urb
->status
) {
644 case -ENOSR
: errmsg
= "Buffer error (overrun)"; break;
645 case -EPIPE
: errmsg
= "Stalled (device not responding)"; break;
646 case -EOVERFLOW
: errmsg
= "Babble (bad cable?)"; break;
647 case -EPROTO
: errmsg
= "Bit-stuff error (bad cable?)"; break;
648 case -EILSEQ
: errmsg
= "CRC/Timeout (could be anything)"; break;
649 case -ETIMEDOUT
: errmsg
= "NAK (device does not respond)"; break;
651 Trace(TRACE_FLOW
, "pwc_isoc_handler() called with status %d [%s].\n", urb
->status
, errmsg
);
652 /* Give up after a number of contiguous errors on the USB bus.
653 Appearantly something is wrong so we simulate an unplug event.
655 if (++pdev
->visoc_errors
> MAX_ISOC_ERRORS
)
657 Info("Too many ISOC errors, bailing out.\n");
658 pdev
->error_status
= EIO
;
660 wake_up_interruptible(&pdev
->frameq
);
662 goto handler_end
; // ugly, but practical
665 fbuf
= pdev
->fill_frame
;
667 Err("pwc_isoc_handler without valid fill frame.\n");
672 fillptr
= fbuf
->data
+ fbuf
->filled
;
675 /* Reset ISOC error counter. We did get here, after all. */
676 pdev
->visoc_errors
= 0;
678 /* vsync: 0 = don't copy data
683 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
684 fst
= urb
->iso_frame_desc
[i
].status
;
685 flen
= urb
->iso_frame_desc
[i
].actual_length
;
686 iso_buf
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
688 if (flen
> 0) { /* if valid data... */
689 if (pdev
->vsync
> 0) { /* ...and we are not sync-hunting... */
692 /* ...copy data to frame buffer, if possible */
693 if (flen
+ fbuf
->filled
> pdev
->frame_total_size
) {
694 Trace(TRACE_FLOW
, "Frame buffer overflow (flen = %d, frame_total_size = %d).\n", flen
, pdev
->frame_total_size
);
695 pdev
->vsync
= 0; /* Hmm, let's wait for an EOF (end-of-frame) */
696 pdev
->vframes_error
++;
699 memmove(fillptr
, iso_buf
, flen
);
703 fbuf
->filled
+= flen
;
706 if (flen
< pdev
->vlast_packet_size
) {
707 /* Shorter packet... We probably have the end of an image-frame;
708 wake up read() process and let select()/poll() do something.
709 Decompression is done in user time over there.
711 if (pdev
->vsync
== 2) {
712 /* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
713 frames on the USB wire after an exposure change. This conditition is
714 however detected in the cam and a bit is set in the header.
716 if (pdev
->type
== 730) {
717 unsigned char *ptr
= (unsigned char *)fbuf
->data
;
719 if (ptr
[1] == 1 && ptr
[0] & 0x10) {
721 Debug("Hyundai CMOS sensor bug. Dropping frame %d.\n", fbuf
->sequence
);
723 pdev
->drop_frames
+= 2;
724 pdev
->vframes_error
++;
726 if ((ptr
[0] ^ pdev
->vmirror
) & 0x01) {
728 Info("Snapshot button pressed.\n");
730 Info("Snapshot button released.\n");
732 if ((ptr
[0] ^ pdev
->vmirror
) & 0x02) {
734 Info("Image is mirrored.\n");
736 Info("Image is normal.\n");
738 pdev
->vmirror
= ptr
[0] & 0x03;
739 /* Sometimes the trailer of the 730 is still sent as a 4 byte packet
740 after a short frame; this condition is filtered out specifically. A 4 byte
741 frame doesn't make sense anyway.
742 So we get either this sequence:
743 drop_bit set -> 4 byte frame -> short frame -> good frame
745 drop_bit set -> short frame -> good frame
746 So we drop either 3 or 2 frames in all!
748 if (fbuf
->filled
== 4)
752 /* In case we were instructed to drop the frame, do so silently.
753 The buffer pointers are not updated either (but the counters are reset below).
755 if (pdev
->drop_frames
> 0)
758 /* Check for underflow first */
759 if (fbuf
->filled
< pdev
->frame_total_size
) {
760 Trace(TRACE_FLOW
, "Frame buffer underflow (%d bytes); discarded.\n", fbuf
->filled
);
761 pdev
->vframes_error
++;
764 /* Send only once per EOF */
765 awake
= 1; /* delay wake_ups */
767 /* Find our next frame to fill. This will always succeed, since we
768 * nick a frame from either empty or full list, but if we had to
769 * take it from the full list, it means a frame got dropped.
771 if (pwc_next_fill_frame(pdev
)) {
772 pdev
->vframes_dumped
++;
773 if ((pdev
->vframe_count
> FRAME_LOWMARK
) && (pwc_trace
& TRACE_FLOW
)) {
774 if (pdev
->vframes_dumped
< 20)
775 Trace(TRACE_FLOW
, "Dumping frame %d.\n", pdev
->vframe_count
);
776 if (pdev
->vframes_dumped
== 20)
777 Trace(TRACE_FLOW
, "Dumping frame %d (last message).\n", pdev
->vframe_count
);
780 fbuf
= pdev
->fill_frame
;
783 pdev
->vframe_count
++;
786 fillptr
= fbuf
->data
;
788 } /* .. flen < last_packet_size */
789 pdev
->vlast_packet_size
= flen
;
790 } /* ..status == 0 */
792 /* This is normally not interesting to the user, unless you are really debugging something */
794 static int iso_error
= 0;
797 Trace(TRACE_FLOW
, "Iso frame %d of USB has error %d\n", i
, fst
);
804 wake_up_interruptible(&pdev
->frameq
);
806 urb
->dev
= pdev
->udev
;
807 i
= usb_submit_urb(urb
, GFP_ATOMIC
);
809 Err("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i
);
813 static int pwc_isoc_init(struct pwc_device
*pdev
)
815 struct usb_device
*udev
;
819 struct usb_interface
*intf
;
820 struct usb_host_interface
*idesc
= NULL
;
829 /* Get the current alternate interface, adjust packet size */
830 if (!udev
->actconfig
)
832 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5)
833 idesc
= &udev
->actconfig
->interface
[0]->altsetting
[pdev
->valternate
];
835 intf
= usb_ifnum_to_if(udev
, 0);
837 idesc
= usb_altnum_to_altsetting(intf
, pdev
->valternate
);
843 /* Search video endpoint */
844 pdev
->vmax_packet_size
= -1;
845 for (i
= 0; i
< idesc
->desc
.bNumEndpoints
; i
++)
846 if ((idesc
->endpoint
[i
].desc
.bEndpointAddress
& 0xF) == pdev
->vendpoint
) {
847 pdev
->vmax_packet_size
= le16_to_cpu(idesc
->endpoint
[i
].desc
.wMaxPacketSize
);
851 if (pdev
->vmax_packet_size
< 0 || pdev
->vmax_packet_size
> ISO_MAX_FRAME_SIZE
) {
852 Err("Failed to find packet size for video endpoint in current alternate setting.\n");
853 return -ENFILE
; /* Odd error, that should be noticeable */
856 /* Set alternate interface */
858 Trace(TRACE_OPEN
, "Setting alternate interface %d\n", pdev
->valternate
);
859 ret
= usb_set_interface(pdev
->udev
, 0, pdev
->valternate
);
863 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
864 urb
= usb_alloc_urb(ISO_FRAMES_PER_DESC
, GFP_KERNEL
);
866 Err("Failed to allocate urb %d\n", i
);
870 pdev
->sbuf
[i
].urb
= urb
;
871 Trace(TRACE_MEMORY
, "Allocated URB at 0x%p\n", urb
);
874 /* De-allocate in reverse order */
876 if (pdev
->sbuf
[i
].urb
!= NULL
)
877 usb_free_urb(pdev
->sbuf
[i
].urb
);
878 pdev
->sbuf
[i
].urb
= NULL
;
884 /* init URB structure */
885 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
886 urb
= pdev
->sbuf
[i
].urb
;
888 urb
->interval
= 1; // devik
890 urb
->pipe
= usb_rcvisocpipe(udev
, pdev
->vendpoint
);
891 urb
->transfer_flags
= URB_ISO_ASAP
;
892 urb
->transfer_buffer
= pdev
->sbuf
[i
].data
;
893 urb
->transfer_buffer_length
= ISO_BUFFER_SIZE
;
894 urb
->complete
= pwc_isoc_handler
;
896 urb
->start_frame
= 0;
897 urb
->number_of_packets
= ISO_FRAMES_PER_DESC
;
898 for (j
= 0; j
< ISO_FRAMES_PER_DESC
; j
++) {
899 urb
->iso_frame_desc
[j
].offset
= j
* ISO_MAX_FRAME_SIZE
;
900 urb
->iso_frame_desc
[j
].length
= pdev
->vmax_packet_size
;
905 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
906 ret
= usb_submit_urb(pdev
->sbuf
[i
].urb
, GFP_KERNEL
);
908 Err("isoc_init() submit_urb %d failed with error %d\n", i
, ret
);
910 Trace(TRACE_MEMORY
, "URB 0x%p submitted.\n", pdev
->sbuf
[i
].urb
);
915 Trace(TRACE_OPEN
, "<< pwc_isoc_init()\n");
919 static void pwc_isoc_cleanup(struct pwc_device
*pdev
)
923 Trace(TRACE_OPEN
, ">> pwc_isoc_cleanup()\n");
927 /* Unlinking ISOC buffers one by one */
928 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
931 urb
= pdev
->sbuf
[i
].urb
;
933 if (pdev
->iso_init
) {
934 Trace(TRACE_MEMORY
, "Unlinking URB %p\n", urb
);
937 Trace(TRACE_MEMORY
, "Freeing URB\n");
939 pdev
->sbuf
[i
].urb
= NULL
;
943 /* Stop camera, but only if we are sure the camera is still there (unplug
944 is signalled by EPIPE)
946 if (pdev
->error_status
&& pdev
->error_status
!= EPIPE
) {
947 Trace(TRACE_OPEN
, "Setting alternate interface 0.\n");
948 usb_set_interface(pdev
->udev
, 0, 0);
952 Trace(TRACE_OPEN
, "<< pwc_isoc_cleanup()\n");
955 int pwc_try_video_mode(struct pwc_device
*pdev
, int width
, int height
, int new_fps
, int new_compression
, int new_snapshot
)
959 /* Stop isoc stuff */
960 pwc_isoc_cleanup(pdev
);
961 /* Reset parameters */
962 pwc_reset_buffers(pdev
);
963 /* Try to set video mode... */
964 start
= ret
= pwc_set_video_mode(pdev
, width
, height
, new_fps
, new_compression
, new_snapshot
);
966 Trace(TRACE_FLOW
, "pwc_set_video_mode attempt 1 failed.\n");
967 /* That failed... restore old mode (we know that worked) */
968 start
= pwc_set_video_mode(pdev
, pdev
->view
.x
, pdev
->view
.y
, pdev
->vframes
, pdev
->vcompression
, pdev
->vsnapshot
);
970 Trace(TRACE_FLOW
, "pwc_set_video_mode attempt 2 failed.\n");
975 if (pwc_isoc_init(pdev
) < 0)
977 Info("Failed to restart ISOC transfers in pwc_try_video_mode.\n");
978 ret
= -EAGAIN
; /* let's try again, who knows if it works a second time */
981 pdev
->drop_frames
++; /* try to avoid garbage during switch */
982 return ret
; /* Return original error code */
986 /***************************************************************************/
987 /* Video4Linux functions */
989 static int pwc_video_open(struct inode
*inode
, struct file
*file
)
992 struct video_device
*vdev
= video_devdata(file
);
993 struct pwc_device
*pdev
;
995 Trace(TRACE_OPEN
, ">> video_open called(vdev = 0x%p).\n", vdev
);
997 pdev
= (struct pwc_device
*)vdev
->priv
;
1003 down(&pdev
->modlock
);
1004 if (!pdev
->usb_init
) {
1005 Trace(TRACE_OPEN
, "Doing first time initialization.\n");
1008 if (pwc_trace
& TRACE_OPEN
)
1010 /* Query sensor type */
1011 const char *sensor_type
= NULL
;
1014 ret
= pwc_get_cmos_sensor(pdev
, &i
);
1018 case 0x00: sensor_type
= "Hyundai CMOS sensor"; break;
1019 case 0x20: sensor_type
= "Sony CCD sensor + TDA8787"; break;
1020 case 0x2E: sensor_type
= "Sony CCD sensor + Exas 98L59"; break;
1021 case 0x2F: sensor_type
= "Sony CCD sensor + ADI 9804"; break;
1022 case 0x30: sensor_type
= "Sharp CCD sensor + TDA8787"; break;
1023 case 0x3E: sensor_type
= "Sharp CCD sensor + Exas 98L59"; break;
1024 case 0x3F: sensor_type
= "Sharp CCD sensor + ADI 9804"; break;
1025 case 0x40: sensor_type
= "UPA 1021 sensor"; break;
1026 case 0x100: sensor_type
= "VGA sensor"; break;
1027 case 0x101: sensor_type
= "PAL MR sensor"; break;
1028 default: sensor_type
= "unknown type of sensor"; break;
1031 if (sensor_type
!= NULL
)
1032 Info("This %s camera is equipped with a %s (%d).\n", pdev
->vdev
->name
, sensor_type
, i
);
1036 /* Turn on camera */
1038 i
= pwc_camera_power(pdev
, 1);
1040 Info("Failed to restore power to the camera! (%d)\n", i
);
1042 /* Set LED on/off time */
1043 if (pwc_set_leds(pdev
, led_on
, led_off
) < 0)
1044 Info("Failed to set LED on/off time.\n");
1046 pwc_construct(pdev
); /* set min/max sizes correct */
1048 /* So far, so good. Allocate memory. */
1049 i
= pwc_allocate_buffers(pdev
);
1051 Trace(TRACE_OPEN
, "Failed to allocate buffer memory.\n");
1056 /* Reset buffers & parameters */
1057 pwc_reset_buffers(pdev
);
1058 for (i
= 0; i
< default_mbufs
; i
++)
1059 pdev
->image_used
[i
] = 0;
1060 pdev
->vframe_count
= 0;
1061 pdev
->vframes_dumped
= 0;
1062 pdev
->vframes_error
= 0;
1063 pdev
->visoc_errors
= 0;
1064 pdev
->error_status
= 0;
1068 pwc_construct(pdev
); /* set min/max sizes correct */
1070 /* Set some defaults */
1071 pdev
->vsnapshot
= 0;
1073 /* Start iso pipe for video; first try the last used video size
1074 (or the default one); if that fails try QCIF/10 or QSIF/10;
1075 it that fails too, give up.
1077 i
= pwc_set_video_mode(pdev
, pwc_image_sizes
[pdev
->vsize
].x
, pwc_image_sizes
[pdev
->vsize
].y
, pdev
->vframes
, pdev
->vcompression
, 0);
1079 Trace(TRACE_OPEN
, "First attempt at set_video_mode failed.\n");
1080 if (pdev
->type
== 730 || pdev
->type
== 740 || pdev
->type
== 750)
1081 i
= pwc_set_video_mode(pdev
, pwc_image_sizes
[PSZ_QSIF
].x
, pwc_image_sizes
[PSZ_QSIF
].y
, 10, pdev
->vcompression
, 0);
1083 i
= pwc_set_video_mode(pdev
, pwc_image_sizes
[PSZ_QCIF
].x
, pwc_image_sizes
[PSZ_QCIF
].y
, 10, pdev
->vcompression
, 0);
1086 Trace(TRACE_OPEN
, "Second attempt at set_video_mode failed.\n");
1091 i
= pwc_isoc_init(pdev
);
1093 Trace(TRACE_OPEN
, "Failed to init ISOC stuff = %d.\n", i
);
1099 file
->private_data
= vdev
;
1101 Trace(TRACE_OPEN
, "<< video_open() returns 0.\n");
1105 /* Note that all cleanup is done in the reverse order as in _open */
1106 static int pwc_video_close(struct inode
*inode
, struct file
*file
)
1108 struct video_device
*vdev
= file
->private_data
;
1109 struct pwc_device
*pdev
;
1112 Trace(TRACE_OPEN
, ">> video_close called(vdev = 0x%p).\n", vdev
);
1114 pdev
= (struct pwc_device
*)vdev
->priv
;
1115 if (pdev
->vopen
== 0)
1116 Info("video_close() called on closed device?\n");
1118 /* Dump statistics, but only if a reasonable amount of frames were
1119 processed (to prevent endless log-entries in case of snap-shot
1122 if (pdev
->vframe_count
> 20)
1123 Info("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev
->vframe_count
, pdev
->vframes_dumped
, pdev
->vframes_error
);
1134 pwc_dec23_exit(); /* Timon & Kiara */
1142 pwc_isoc_cleanup(pdev
);
1143 pwc_free_buffers(pdev
);
1145 /* Turn off LEDS and power down camera, but only when not unplugged */
1146 if (pdev
->error_status
!= EPIPE
) {
1148 if (pwc_set_leds(pdev
, 0, 0) < 0)
1149 Info("Failed to set LED on/off time.\n");
1151 i
= pwc_camera_power(pdev
, 0);
1153 Err("Failed to power down camera (%d)\n", i
);
1157 Trace(TRACE_OPEN
, "<< video_close()\n");
1162 * FIXME: what about two parallel reads ????
1163 * ANSWER: Not supported. You can't open the device more than once,
1164 despite what the V4L1 interface says. First, I don't see
1165 the need, second there's no mechanism of alerting the
1166 2nd/3rd/... process of events like changing image size.
1167 And I don't see the point of blocking that for the
1168 2nd/3rd/... process.
1169 In multi-threaded environments reading parallel from any
1170 device is tricky anyhow.
1173 static ssize_t
pwc_video_read(struct file
*file
, char __user
* buf
,
1174 size_t count
, loff_t
*ppos
)
1176 struct video_device
*vdev
= file
->private_data
;
1177 struct pwc_device
*pdev
;
1178 int noblock
= file
->f_flags
& O_NONBLOCK
;
1179 DECLARE_WAITQUEUE(wait
, current
);
1182 Trace(TRACE_READ
, "video_read(0x%p, %p, %zu) called.\n", vdev
, buf
, count
);
1188 if (pdev
->error_status
)
1189 return -pdev
->error_status
; /* Something happened, report what. */
1191 /* In case we're doing partial reads, we don't have to wait for a frame */
1192 if (pdev
->image_read_pos
== 0) {
1193 /* Do wait queueing according to the (doc)book */
1194 add_wait_queue(&pdev
->frameq
, &wait
);
1195 while (pdev
->full_frames
== NULL
) {
1196 /* Check for unplugged/etc. here */
1197 if (pdev
->error_status
) {
1198 remove_wait_queue(&pdev
->frameq
, &wait
);
1199 set_current_state(TASK_RUNNING
);
1200 return -pdev
->error_status
;
1203 remove_wait_queue(&pdev
->frameq
, &wait
);
1204 set_current_state(TASK_RUNNING
);
1205 return -EWOULDBLOCK
;
1207 if (signal_pending(current
)) {
1208 remove_wait_queue(&pdev
->frameq
, &wait
);
1209 set_current_state(TASK_RUNNING
);
1210 return -ERESTARTSYS
;
1213 set_current_state(TASK_INTERRUPTIBLE
);
1215 remove_wait_queue(&pdev
->frameq
, &wait
);
1216 set_current_state(TASK_RUNNING
);
1218 /* Decompress and release frame */
1219 if (pwc_handle_frame(pdev
))
1223 Trace(TRACE_READ
, "Copying data to user space.\n");
1224 if (pdev
->vpalette
== VIDEO_PALETTE_RAW
)
1225 bytes_to_read
= pdev
->frame_size
;
1227 bytes_to_read
= pdev
->view
.size
;
1229 /* copy bytes to user space; we allow for partial reads */
1230 if (count
+ pdev
->image_read_pos
> bytes_to_read
)
1231 count
= bytes_to_read
- pdev
->image_read_pos
;
1232 if (copy_to_user(buf
, pdev
->image_ptr
[pdev
->fill_image
] + pdev
->image_read_pos
, count
))
1234 pdev
->image_read_pos
+= count
;
1235 if (pdev
->image_read_pos
>= bytes_to_read
) { /* All data has been read */
1236 pdev
->image_read_pos
= 0;
1237 pwc_next_image(pdev
);
1242 static unsigned int pwc_video_poll(struct file
*file
, poll_table
*wait
)
1244 struct video_device
*vdev
= file
->private_data
;
1245 struct pwc_device
*pdev
;
1253 poll_wait(file
, &pdev
->frameq
, wait
);
1254 if (pdev
->error_status
)
1256 if (pdev
->full_frames
!= NULL
) /* we have frames waiting */
1257 return (POLLIN
| POLLRDNORM
);
1262 static int pwc_video_do_ioctl(struct inode
*inode
, struct file
*file
,
1263 unsigned int cmd
, void *arg
)
1265 struct video_device
*vdev
= file
->private_data
;
1266 struct pwc_device
*pdev
;
1267 DECLARE_WAITQUEUE(wait
, current
);
1276 /* Query cabapilities */
1279 struct video_capability
*caps
= arg
;
1281 strcpy(caps
->name
, vdev
->name
);
1282 caps
->type
= VID_TYPE_CAPTURE
;
1285 caps
->minwidth
= pdev
->view_min
.x
;
1286 caps
->minheight
= pdev
->view_min
.y
;
1287 caps
->maxwidth
= pdev
->view_max
.x
;
1288 caps
->maxheight
= pdev
->view_max
.y
;
1292 /* Channel functions (simulate 1 channel) */
1295 struct video_channel
*v
= arg
;
1297 if (v
->channel
!= 0)
1301 v
->type
= VIDEO_TYPE_CAMERA
;
1302 strcpy(v
->name
, "Webcam");
1308 /* The spec says the argument is an integer, but
1309 the bttv driver uses a video_channel arg, which
1310 makes sense becasue it also has the norm flag.
1312 struct video_channel
*v
= arg
;
1313 if (v
->channel
!= 0)
1319 /* Picture functions; contrast etc. */
1322 struct video_picture
*p
= arg
;
1325 val
= pwc_get_brightness(pdev
);
1327 p
->brightness
= val
;
1329 p
->brightness
= 0xffff;
1330 val
= pwc_get_contrast(pdev
);
1334 p
->contrast
= 0xffff;
1335 /* Gamma, Whiteness, what's the difference? :) */
1336 val
= pwc_get_gamma(pdev
);
1340 p
->whiteness
= 0xffff;
1341 val
= pwc_get_saturation(pdev
);
1347 p
->palette
= pdev
->vpalette
;
1348 p
->hue
= 0xFFFF; /* N/A */
1354 struct video_picture
*p
= arg
;
1356 * FIXME: Suppose we are mid read
1357 ANSWER: No problem: the firmware of the camera
1358 can handle brightness/contrast/etc
1359 changes at _any_ time, and the palette
1360 is used exactly once in the uncompress
1363 pwc_set_brightness(pdev
, p
->brightness
);
1364 pwc_set_contrast(pdev
, p
->contrast
);
1365 pwc_set_gamma(pdev
, p
->whiteness
);
1366 pwc_set_saturation(pdev
, p
->colour
);
1367 if (p
->palette
&& p
->palette
!= pdev
->vpalette
) {
1368 switch (p
->palette
) {
1369 case VIDEO_PALETTE_YUV420P
:
1370 case VIDEO_PALETTE_RAW
:
1371 pdev
->vpalette
= p
->palette
;
1372 return pwc_try_video_mode(pdev
, pdev
->image
.x
, pdev
->image
.y
, pdev
->vframes
, pdev
->vcompression
, pdev
->vsnapshot
);
1382 /* Window/size parameters */
1385 struct video_window
*vw
= arg
;
1389 vw
->width
= pdev
->view
.x
;
1390 vw
->height
= pdev
->view
.y
;
1392 vw
->flags
= (pdev
->vframes
<< PWC_FPS_SHIFT
) |
1393 (pdev
->vsnapshot
? PWC_FPS_SNAPSHOT
: 0);
1399 struct video_window
*vw
= arg
;
1400 int fps
, snapshot
, ret
;
1402 fps
= (vw
->flags
& PWC_FPS_FRMASK
) >> PWC_FPS_SHIFT
;
1403 snapshot
= vw
->flags
& PWC_FPS_SNAPSHOT
;
1405 fps
= pdev
->vframes
;
1406 if (pdev
->view
.x
== vw
->width
&& pdev
->view
.y
&& fps
== pdev
->vframes
&& snapshot
== pdev
->vsnapshot
)
1408 ret
= pwc_try_video_mode(pdev
, vw
->width
, vw
->height
, fps
, pdev
->vcompression
, snapshot
);
1414 /* We don't have overlay support (yet) */
1417 struct video_buffer
*vb
= arg
;
1419 memset(vb
,0,sizeof(*vb
));
1423 /* mmap() functions */
1426 /* Tell the user program how much memory is needed for a mmap() */
1427 struct video_mbuf
*vm
= arg
;
1430 memset(vm
, 0, sizeof(*vm
));
1431 vm
->size
= default_mbufs
* pdev
->len_per_image
;
1432 vm
->frames
= default_mbufs
; /* double buffering should be enough for most applications */
1433 for (i
= 0; i
< default_mbufs
; i
++)
1434 vm
->offsets
[i
] = i
* pdev
->len_per_image
;
1438 case VIDIOCMCAPTURE
:
1440 /* Start capture into a given image buffer (called 'frame' in video_mmap structure) */
1441 struct video_mmap
*vm
= arg
;
1443 Trace(TRACE_READ
, "VIDIOCMCAPTURE: %dx%d, frame %d, format %d\n", vm
->width
, vm
->height
, vm
->frame
, vm
->format
);
1444 if (vm
->frame
< 0 || vm
->frame
>= default_mbufs
)
1447 /* xawtv is nasty. It probes the available palettes
1448 by setting a very small image size and trying
1449 various palettes... The driver doesn't support
1450 such small images, so I'm working around it.
1456 case VIDEO_PALETTE_YUV420P
:
1457 case VIDEO_PALETTE_RAW
:
1465 if ((vm
->width
!= pdev
->view
.x
|| vm
->height
!= pdev
->view
.y
) &&
1466 (vm
->width
>= pdev
->view_min
.x
&& vm
->height
>= pdev
->view_min
.y
)) {
1469 Trace(TRACE_OPEN
, "VIDIOCMCAPTURE: changing size to please xawtv :-(.\n");
1470 ret
= pwc_try_video_mode(pdev
, vm
->width
, vm
->height
, pdev
->vframes
, pdev
->vcompression
, pdev
->vsnapshot
);
1473 } /* ... size mismatch */
1475 /* FIXME: should we lock here? */
1476 if (pdev
->image_used
[vm
->frame
])
1477 return -EBUSY
; /* buffer wasn't available. Bummer */
1478 pdev
->image_used
[vm
->frame
] = 1;
1480 /* Okay, we're done here. In the SYNC call we wait until a
1481 frame comes available, then expand image into the given
1483 In contrast to the CPiA cam the Philips cams deliver a
1484 constant stream, almost like a grabber card. Also,
1485 we have separate buffers for the rawdata and the image,
1486 meaning we can nearly always expand into the requested buffer.
1488 Trace(TRACE_READ
, "VIDIOCMCAPTURE done.\n");
1494 /* The doc says: "Whenever a buffer is used it should
1495 call VIDIOCSYNC to free this frame up and continue."
1497 The only odd thing about this whole procedure is
1498 that MCAPTURE flags the buffer as "in use", and
1499 SYNC immediately unmarks it, while it isn't
1500 after SYNC that you know that the buffer actually
1501 got filled! So you better not start a CAPTURE in
1502 the same frame immediately (use double buffering).
1503 This is not a problem for this cam, since it has
1504 extra intermediate buffers, but a hardware
1505 grabber card will then overwrite the buffer
1511 Trace(TRACE_READ
, "VIDIOCSYNC called (%d).\n", *mbuf
);
1514 if (*mbuf
< 0 || *mbuf
>= default_mbufs
)
1516 /* check if this buffer was requested anyway */
1517 if (pdev
->image_used
[*mbuf
] == 0)
1520 /* Add ourselves to the frame wait-queue.
1522 FIXME: needs auditing for safety.
1523 QUESTION: In what respect? I think that using the
1526 add_wait_queue(&pdev
->frameq
, &wait
);
1527 while (pdev
->full_frames
== NULL
) {
1528 if (pdev
->error_status
) {
1529 remove_wait_queue(&pdev
->frameq
, &wait
);
1530 set_current_state(TASK_RUNNING
);
1531 return -pdev
->error_status
;
1534 if (signal_pending(current
)) {
1535 remove_wait_queue(&pdev
->frameq
, &wait
);
1536 set_current_state(TASK_RUNNING
);
1537 return -ERESTARTSYS
;
1540 set_current_state(TASK_INTERRUPTIBLE
);
1542 remove_wait_queue(&pdev
->frameq
, &wait
);
1543 set_current_state(TASK_RUNNING
);
1545 /* The frame is ready. Expand in the image buffer
1546 requested by the user. I don't care if you
1547 mmap() 5 buffers and request data in this order:
1548 buffer 4 2 3 0 1 2 3 0 4 3 1 . . .
1549 Grabber hardware may not be so forgiving.
1551 Trace(TRACE_READ
, "VIDIOCSYNC: frame ready.\n");
1552 pdev
->fill_image
= *mbuf
; /* tell in which buffer we want the image to be expanded */
1553 /* Decompress, etc */
1554 ret
= pwc_handle_frame(pdev
);
1555 pdev
->image_used
[*mbuf
] = 0;
1563 struct video_audio
*v
= arg
;
1565 strcpy(v
->name
, "Microphone");
1566 v
->audio
= -1; /* unknown audio minor */
1568 v
->mode
= VIDEO_SOUND_MONO
;
1572 v
->balance
= 0x8000;
1579 /* Dummy: nothing can be set */
1585 struct video_unit
*vu
= arg
;
1587 vu
->video
= pdev
->vdev
->minor
& 0x3F;
1588 vu
->audio
= -1; /* not known yet */
1595 return pwc_ioctl(pdev
, cmd
, arg
);
1600 static int pwc_video_ioctl(struct inode
*inode
, struct file
*file
,
1601 unsigned int cmd
, unsigned long arg
)
1603 return video_usercopy(inode
, file
, cmd
, arg
, pwc_video_do_ioctl
);
1607 static int pwc_video_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1609 struct video_device
*vdev
= file
->private_data
;
1610 struct pwc_device
*pdev
;
1611 unsigned long start
= vma
->vm_start
;
1612 unsigned long size
= vma
->vm_end
-vma
->vm_start
;
1613 unsigned long page
, pos
;
1615 Trace(TRACE_MEMORY
, "mmap(0x%p, 0x%lx, %lu) called.\n", vdev
, start
, size
);
1618 vma
->vm_flags
|= VM_IO
;
1620 pos
= (unsigned long)pdev
->image_data
;
1622 page
= vmalloc_to_pfn((void *)pos
);
1623 if (remap_pfn_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
))
1628 if (size
> PAGE_SIZE
)
1637 /***************************************************************************/
1640 /* This function gets called when a new device is plugged in or the usb core
1644 static int usb_pwc_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1646 struct usb_device
*udev
= interface_to_usbdev(intf
);
1647 struct pwc_device
*pdev
= NULL
;
1648 int vendor_id
, product_id
, type_id
;
1651 int video_nr
= -1; /* default: use next available device */
1652 char serial_number
[30], *name
;
1654 /* Check if we can handle this device */
1655 Trace(TRACE_PROBE
, "probe() called [%04X %04X], if %d\n",
1656 le16_to_cpu(udev
->descriptor
.idVendor
),
1657 le16_to_cpu(udev
->descriptor
.idProduct
),
1658 intf
->altsetting
->desc
.bInterfaceNumber
);
1660 /* the interfaces are probed one by one. We are only interested in the
1661 video interface (0) now.
1662 Interface 1 is the Audio Control, and interface 2 Audio itself.
1664 if (intf
->altsetting
->desc
.bInterfaceNumber
> 0)
1667 vendor_id
= le16_to_cpu(udev
->descriptor
.idVendor
);
1668 product_id
= le16_to_cpu(udev
->descriptor
.idProduct
);
1670 if (vendor_id
== 0x0471) {
1671 switch (product_id
) {
1673 Info("Philips PCA645VC USB webcam detected.\n");
1674 name
= "Philips 645 webcam";
1678 Info("Philips PCA646VC USB webcam detected.\n");
1679 name
= "Philips 646 webcam";
1683 Info("Askey VC010 type 2 USB webcam detected.\n");
1684 name
= "Askey VC010 webcam";
1688 Info("Philips PCVC675K (Vesta) USB webcam detected.\n");
1689 name
= "Philips 675 webcam";
1693 Info("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
1694 name
= "Philips 680 webcam";
1698 Info("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
1699 name
= "Philips 690 webcam";
1703 Info("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
1704 name
= "Philips 730 webcam";
1708 Info("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
1709 name
= "Philips 740 webcam";
1713 Info("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
1714 name
= "Philips 750 webcam";
1718 Info("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
1719 name
= "Philips 720K/40 webcam";
1727 else if (vendor_id
== 0x069A) {
1728 switch(product_id
) {
1730 Info("Askey VC010 type 1 USB webcam detected.\n");
1731 name
= "Askey VC010 webcam";
1739 else if (vendor_id
== 0x046d) {
1740 switch(product_id
) {
1742 Info("Logitech QuickCam Pro 3000 USB webcam detected.\n");
1743 name
= "Logitech QuickCam Pro 3000";
1744 type_id
= 740; /* CCD sensor */
1747 Info("Logitech QuickCam Notebook Pro USB webcam detected.\n");
1748 name
= "Logitech QuickCam Notebook Pro";
1749 type_id
= 740; /* CCD sensor */
1752 Info("Logitech QuickCam 4000 Pro USB webcam detected.\n");
1753 name
= "Logitech QuickCam Pro 4000";
1754 type_id
= 740; /* CCD sensor */
1757 Info("Logitech QuickCam Zoom USB webcam detected.\n");
1758 name
= "Logitech QuickCam Zoom";
1759 type_id
= 740; /* CCD sensor */
1762 Info("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
1763 name
= "Logitech QuickCam Zoom";
1764 type_id
= 740; /* CCD sensor */
1767 Info("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
1768 name
= "Logitech QuickCam Orbit";
1769 type_id
= 740; /* CCD sensor */
1770 features
|= FEATURE_MOTOR_PANTILT
;
1775 Info("Logitech QuickCam detected (reserved ID).\n");
1776 name
= "Logitech QuickCam (res.)";
1777 type_id
= 730; /* Assuming CMOS */
1784 else if (vendor_id
== 0x055d) {
1785 /* I don't know the difference between the C10 and the C30;
1786 I suppose the difference is the sensor, but both cameras
1787 work equally well with a type_id of 675
1789 switch(product_id
) {
1791 Info("Samsung MPC-C10 USB webcam detected.\n");
1792 name
= "Samsung MPC-C10";
1796 Info("Samsung MPC-C30 USB webcam detected.\n");
1797 name
= "Samsung MPC-C30";
1805 else if (vendor_id
== 0x041e) {
1806 switch(product_id
) {
1808 Info("Creative Labs Webcam 5 detected.\n");
1809 name
= "Creative Labs Webcam 5";
1813 Info("Creative Labs Webcam Pro Ex detected.\n");
1814 name
= "Creative Labs Webcam Pro Ex";
1822 else if (vendor_id
== 0x04cc) {
1823 switch(product_id
) {
1825 Info("Sotec Afina Eye USB webcam detected.\n");
1826 name
= "Sotec Afina Eye";
1834 else if (vendor_id
== 0x06be) {
1835 switch(product_id
) {
1837 /* This is essentially the same cam as the Sotec Afina Eye */
1838 Info("AME Co. Afina Eye USB webcam detected.\n");
1839 name
= "AME Co. Afina Eye";
1848 else if (vendor_id
== 0x0d81) {
1849 switch(product_id
) {
1851 Info("Visionite VCS-UC300 USB webcam detected.\n");
1852 name
= "Visionite VCS-UC300";
1853 type_id
= 740; /* CCD sensor */
1856 Info("Visionite VCS-UM100 USB webcam detected.\n");
1857 name
= "Visionite VCS-UM100";
1858 type_id
= 730; /* CMOS sensor */
1866 return -ENODEV
; /* Not any of the know types; but the list keeps growing. */
1868 memset(serial_number
, 0, 30);
1869 usb_string(udev
, udev
->descriptor
.iSerialNumber
, serial_number
, 29);
1870 Trace(TRACE_PROBE
, "Device serial number is %s\n", serial_number
);
1872 if (udev
->descriptor
.bNumConfigurations
> 1)
1873 Info("Warning: more than 1 configuration available.\n");
1875 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1876 pdev
= kmalloc(sizeof(struct pwc_device
), GFP_KERNEL
);
1878 Err("Oops, could not allocate memory for pwc_device.\n");
1881 memset(pdev
, 0, sizeof(struct pwc_device
));
1882 pdev
->type
= type_id
;
1883 pdev
->vsize
= default_size
;
1884 pdev
->vframes
= default_fps
;
1885 strcpy(pdev
->serial
, serial_number
);
1886 pdev
->features
= features
;
1887 if (vendor_id
== 0x046D && product_id
== 0x08B5)
1889 /* Logitech QuickCam Orbit
1890 The ranges have been determined experimentally; they may differ from cam to cam.
1891 Also, the exact ranges left-right and up-down are different for my cam
1893 pdev
->angle_range
.pan_min
= -7000;
1894 pdev
->angle_range
.pan_max
= 7000;
1895 pdev
->angle_range
.tilt_min
= -3000;
1896 pdev
->angle_range
.tilt_max
= 2500;
1899 init_MUTEX(&pdev
->modlock
);
1900 spin_lock_init(&pdev
->ptrlock
);
1903 init_waitqueue_head(&pdev
->frameq
);
1904 pdev
->vcompression
= pwc_preferred_compression
;
1906 /* Allocate video_device structure */
1907 pdev
->vdev
= video_device_alloc();
1908 if (pdev
->vdev
== 0)
1910 Err("Err, cannot allocate video_device struture. Failing probe.");
1914 memcpy(pdev
->vdev
, &pwc_template
, sizeof(pwc_template
));
1915 strcpy(pdev
->vdev
->name
, name
);
1916 pdev
->vdev
->owner
= THIS_MODULE
;
1917 video_set_drvdata(pdev
->vdev
, pdev
);
1919 pdev
->release
= le16_to_cpu(udev
->descriptor
.bcdDevice
);
1920 Trace(TRACE_PROBE
, "Release: %04x\n", pdev
->release
);
1922 /* Now search device_hint[] table for a match, so we can hint a node number. */
1923 for (hint
= 0; hint
< MAX_DEV_HINTS
; hint
++) {
1924 if (((device_hint
[hint
].type
== -1) || (device_hint
[hint
].type
== pdev
->type
)) &&
1925 (device_hint
[hint
].pdev
== NULL
)) {
1926 /* so far, so good... try serial number */
1927 if ((device_hint
[hint
].serial_number
[0] == '*') || !strcmp(device_hint
[hint
].serial_number
, serial_number
)) {
1929 video_nr
= device_hint
[hint
].device_node
;
1930 Trace(TRACE_PROBE
, "Found hint, will try to register as /dev/video%d\n", video_nr
);
1936 pdev
->vdev
->release
= video_device_release
;
1937 i
= video_register_device(pdev
->vdev
, VFL_TYPE_GRABBER
, video_nr
);
1939 Err("Failed to register as video device (%d).\n", i
);
1940 video_device_release(pdev
->vdev
); /* Drip... drip... drip... */
1941 kfree(pdev
); /* Oops, no memory leaks please */
1945 Info("Registered as /dev/video%d.\n", pdev
->vdev
->minor
& 0x3F);
1949 if (hint
< MAX_DEV_HINTS
)
1950 device_hint
[hint
].pdev
= pdev
;
1952 Trace(TRACE_PROBE
, "probe() function returning struct at 0x%p.\n", pdev
);
1953 usb_set_intfdata (intf
, pdev
);
1957 /* The user janked out the cable... */
1958 static void usb_pwc_disconnect(struct usb_interface
*intf
)
1960 struct pwc_device
*pdev
;
1964 pdev
= usb_get_intfdata (intf
);
1965 usb_set_intfdata (intf
, NULL
);
1967 Err("pwc_disconnect() Called without private pointer.\n");
1968 goto disconnect_out
;
1970 if (pdev
->udev
== NULL
) {
1971 Err("pwc_disconnect() already called for %p\n", pdev
);
1972 goto disconnect_out
;
1974 if (pdev
->udev
!= interface_to_usbdev(intf
)) {
1975 Err("pwc_disconnect() Woops: pointer mismatch udev/pdev.\n");
1976 goto disconnect_out
;
1979 if (pdev
->magic
!= PWC_MAGIC
) {
1980 Err("pwc_disconnect() Magic number failed. Consult your scrolls and try again.\n");
1981 goto disconnect_out
;
1985 /* We got unplugged; this is signalled by an EPIPE error code */
1987 Info("Disconnected while webcam is in use!\n");
1988 pdev
->error_status
= EPIPE
;
1991 /* Alert waiting processes */
1992 wake_up_interruptible(&pdev
->frameq
);
1993 /* Wait until device is closed */
1996 /* Device is now closed, so we can safely unregister it */
1997 Trace(TRACE_PROBE
, "Unregistering video device in disconnect().\n");
1998 video_unregister_device(pdev
->vdev
);
2000 /* Free memory (don't set pdev to 0 just yet) */
2004 /* search device_hint[] table if we occupy a slot, by any chance */
2005 for (hint
= 0; hint
< MAX_DEV_HINTS
; hint
++)
2006 if (device_hint
[hint
].pdev
== pdev
)
2007 device_hint
[hint
].pdev
= NULL
;
2013 /* *grunt* We have to do atoi ourselves :-( */
2014 static int pwc_atoi(const char *s
)
2019 while (*s
!= '\0' && *s
>= '0' && *s
<= '9') {
2020 k
= 10 * k
+ (*s
- '0');
2028 * Initialization code & module stuff
2031 static char size
[10];
2033 static int fbufs
= 0;
2034 static int mbufs
= 0;
2035 static int trace
= -1;
2036 static int compression
= -1;
2037 static int leds
[2] = { -1, -1 };
2038 static char *dev_hint
[MAX_DEV_HINTS
] = { };
2040 module_param_string(size
, size
, sizeof(size
), 0);
2041 MODULE_PARM_DESC(size
, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
2042 module_param(fps
, int, 0000);
2043 MODULE_PARM_DESC(fps
, "Initial frames per second. Varies with model, useful range 5-30");
2044 module_param(fbufs
, int, 0000);
2045 MODULE_PARM_DESC(fbufs
, "Number of internal frame buffers to reserve");
2046 module_param(mbufs
, int, 0000);
2047 MODULE_PARM_DESC(mbufs
, "Number of external (mmap()ed) image buffers");
2048 module_param(trace
, int, 0000);
2049 MODULE_PARM_DESC(trace
, "For debugging purposes");
2050 module_param(power_save
, bool, 0000);
2051 MODULE_PARM_DESC(power_save
, "Turn power save feature in camera on or off");
2052 module_param(compression
, int, 0000);
2053 MODULE_PARM_DESC(compression
, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
2054 module_param_array(leds
, int, NULL
, 0000);
2055 MODULE_PARM_DESC(leds
, "LED on,off time in milliseconds");
2056 module_param_array(dev_hint
, charp
, NULL
, 0000);
2057 MODULE_PARM_DESC(dev_hint
, "Device node hints");
2059 MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
2060 MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
2061 MODULE_LICENSE("GPL");
2063 static int __init
usb_pwc_init(void)
2066 char *sizenames
[PSZ_MAX
] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };
2068 Info("Philips webcam module version " PWC_VERSION
" loaded.\n");
2069 Info("Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.\n");
2070 Info("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
2071 Info("the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
2074 if (fps
< 4 || fps
> 30) {
2075 Err("Framerate out of bounds (4-30).\n");
2079 Info("Default framerate set to %d.\n", default_fps
);
2083 /* string; try matching with array */
2084 for (sz
= 0; sz
< PSZ_MAX
; sz
++) {
2085 if (!strcmp(sizenames
[sz
], size
)) { /* Found! */
2090 if (sz
== PSZ_MAX
) {
2091 Err("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
2094 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
);
2097 if (mbufs
< 1 || mbufs
> MAX_IMAGES
) {
2098 Err("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES
);
2101 default_mbufs
= mbufs
;
2102 Info("Number of image buffers set to %d.\n", default_mbufs
);
2105 if (fbufs
< 2 || fbufs
> MAX_FRAMES
) {
2106 Err("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES
);
2109 default_fbufs
= fbufs
;
2110 Info("Number of frame buffers set to %d.\n", default_fbufs
);
2113 Info("Trace options: 0x%04x\n", trace
);
2116 if (compression
>= 0) {
2117 if (compression
> 3) {
2118 Err("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
2121 pwc_preferred_compression
= compression
;
2122 Info("Preferred compression set to %d.\n", pwc_preferred_compression
);
2125 Info("Enabling power save on open/close.\n");
2131 /* Big device node whoopla. Basically, it allows you to assign a
2132 device node (/dev/videoX) to a camera, based on its type
2133 & serial number. The format is [type[.serialnumber]:]node.
2135 Any camera that isn't matched by these rules gets the next
2136 available free device node.
2138 for (i
= 0; i
< MAX_DEV_HINTS
; i
++) {
2139 char *s
, *colon
, *dot
;
2141 /* This loop also initializes the array */
2142 device_hint
[i
].pdev
= NULL
;
2144 if (s
!= NULL
&& *s
!= '\0') {
2145 device_hint
[i
].type
= -1; /* wildcard */
2146 strcpy(device_hint
[i
].serial_number
, "*");
2148 /* parse string: chop at ':' & '/' */
2150 while (*colon
!= '\0' && *colon
!= ':')
2152 while (*dot
!= '\0' && *dot
!= '.')
2154 /* Few sanity checks */
2155 if (*dot
!= '\0' && dot
> colon
) {
2156 Err("Malformed camera hint: the colon must be after the dot.\n");
2160 if (*colon
== '\0') {
2163 Err("Malformed camera hint: no colon + device node given.\n");
2167 /* No type or serial number specified, just a number. */
2168 device_hint
[i
].device_node
= pwc_atoi(s
);
2172 /* There's a colon, so we have at least a type and a device node */
2173 device_hint
[i
].type
= pwc_atoi(s
);
2174 device_hint
[i
].device_node
= pwc_atoi(colon
+ 1);
2176 /* There's a serial number as well */
2181 while (*dot
!= ':' && k
< 29) {
2182 device_hint
[i
].serial_number
[k
++] = *dot
;
2185 device_hint
[i
].serial_number
[k
] = '\0';
2189 Debug("device_hint[%d]:\n", i
);
2190 Debug(" type : %d\n", device_hint
[i
].type
);
2191 Debug(" serial# : %s\n", device_hint
[i
].serial_number
);
2192 Debug(" node : %d\n", device_hint
[i
].device_node
);
2196 device_hint
[i
].type
= 0; /* not filled */
2197 } /* ..for MAX_DEV_HINTS */
2199 Trace(TRACE_PROBE
, "Registering driver at address 0x%p.\n", &pwc_driver
);
2200 return usb_register(&pwc_driver
);
2203 static void __exit
usb_pwc_exit(void)
2205 Trace(TRACE_MODULE
, "Deregistering driver.\n");
2206 usb_deregister(&pwc_driver
);
2207 Info("Philips webcam module removed.\n");
2210 module_init(usb_pwc_init
);
2211 module_exit(usb_pwc_exit
);