aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/media/pwc/pwc-if.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/media/pwc/pwc-if.c')
-rw-r--r--drivers/usb/media/pwc/pwc-if.c2205
1 files changed, 0 insertions, 2205 deletions
diff --git a/drivers/usb/media/pwc/pwc-if.c b/drivers/usb/media/pwc/pwc-if.c
deleted file mode 100644
index 90eb26042817..000000000000
--- a/drivers/usb/media/pwc/pwc-if.c
+++ /dev/null
@@ -1,2205 +0,0 @@
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)
5
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.
11
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.
16
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.
21
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
25
26*/
27
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.
34
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).
39
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 *
45*/
46
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
56*/
57
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>
66
67#include "pwc.h"
68#include "pwc-ioctl.h"
69#include "pwc-kiara.h"
70#include "pwc-timon.h"
71#include "pwc-uncompress.h"
72
73/* Function prototypes and driver templates */
74
75/* hotplug device table support */
76static 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) },
105 { }
106};
107MODULE_DEVICE_TABLE(usb, pwc_device_table);
108
109static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id);
110static void usb_pwc_disconnect(struct usb_interface *intf);
111
112static struct usb_driver pwc_driver = {
113 .name = "Philips webcam", /* name */
114 .id_table = pwc_device_table,
115 .probe = usb_pwc_probe, /* probe() */
116 .disconnect = usb_pwc_disconnect, /* disconnect() */
117};
118
119#define MAX_DEV_HINTS 20
120#define MAX_ISOC_ERRORS 20
121
122static int default_size = PSZ_QCIF;
123static int default_fps = 10;
124static int default_fbufs = 3; /* Default number of frame buffers */
125static int default_mbufs = 2; /* Default number of mmap() buffers */
126 int pwc_trace = TRACE_MODULE | TRACE_FLOW | TRACE_PWCX;
127static int power_save = 0;
128static int led_on = 100, led_off = 0; /* defaults to LED that is on while in use */
129static int pwc_preferred_compression = 2; /* 0..3 = uncompressed..high */
130static struct {
131 int type;
132 char serial_number[30];
133 int device_node;
134 struct pwc_device *pdev;
135} device_hint[MAX_DEV_HINTS];
136
137/***/
138
139static int pwc_video_open(struct inode *inode, struct file *file);
140static int pwc_video_close(struct inode *inode, struct file *file);
141static ssize_t pwc_video_read(struct file *file, char __user * buf,
142 size_t count, loff_t *ppos);
143static unsigned int pwc_video_poll(struct file *file, poll_table *wait);
144static int pwc_video_ioctl(struct inode *inode, struct file *file,
145 unsigned int ioctlnr, unsigned long arg);
146static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma);
147
148static struct file_operations pwc_fops = {
149 .owner = THIS_MODULE,
150 .open = pwc_video_open,
151 .release = pwc_video_close,
152 .read = pwc_video_read,
153 .poll = pwc_video_poll,
154 .mmap = pwc_video_mmap,
155 .ioctl = pwc_video_ioctl,
156 .compat_ioctl = v4l_compat_ioctl32,
157 .llseek = no_llseek,
158};
159static 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,
167};
168
169/***************************************************************************/
170
171/* Okay, this is some magic that I worked out and the reasoning behind it...
172
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.
180
181 Of course, we go for option 4.
182
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.
187
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.
193
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! :))
198*/
199
200/***************************************************************************/
201/* Private functions */
202
203/* Here we want the physical address of the memory.
204 * This is used when initializing the contents of the area.
205 */
206static inline unsigned long kvirt_to_pa(unsigned long adr)
207{
208 unsigned long kva, ret;
209
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;
214}
215
216static void * rvmalloc(unsigned long size)
217{
218 void * mem;
219 unsigned long adr;
220
221 size=PAGE_ALIGN(size);
222 mem=vmalloc_32(size);
223 if (mem)
224 {
225 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
226 adr=(unsigned long) mem;
227 while (size > 0)
228 {
229 SetPageReserved(vmalloc_to_page((void *)adr));
230 adr+=PAGE_SIZE;
231 size-=PAGE_SIZE;
232 }
233 }
234 return mem;
235}
236
237static void rvfree(void * mem, unsigned long size)
238{
239 unsigned long adr;
240
241 if (mem)
242 {
243 adr=(unsigned long) mem;
244 while ((long) size > 0)
245 {
246 ClearPageReserved(vmalloc_to_page((void *)adr));
247 adr+=PAGE_SIZE;
248 size-=PAGE_SIZE;
249 }
250 vfree(mem);
251 }
252}
253
254
255
256
257static int pwc_allocate_buffers(struct pwc_device *pdev)
258{
259 int i;
260 void *kbuf;
261
262 Trace(TRACE_MEMORY, ">> pwc_allocate_buffers(pdev = 0x%p)\n", pdev);
263
264 if (pdev == NULL)
265 return -ENXIO;
266
267#ifdef PWC_MAGIC
268 if (pdev->magic != PWC_MAGIC) {
269 Err("allocate_buffers(): magic failed.\n");
270 return -ENXIO;
271 }
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;
280 }
281 Trace(TRACE_MEMORY, "Allocated iso buffer at %p.\n", kbuf);
282 pdev->sbuf[i].data = kbuf;
283 memset(kbuf, 0, ISO_BUFFER_SIZE);
284 }
285 }
286
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;
293 }
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));
297 }
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;
305 }
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);
309 }
310 }
311
312 /* Allocate decompressor table space */
313 kbuf = NULL;
314 switch (pdev->type)
315 {
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
333 ;
334 }
335 pdev->decompress_data = kbuf;
336
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;
342 }
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;
349
350 kbuf = NULL;
351
352 Trace(TRACE_MEMORY, "<< pwc_allocate_buffers()\n");
353 return 0;
354}
355
356static void pwc_free_buffers(struct pwc_device *pdev)
357{
358 int i;
359
360 Trace(TRACE_MEMORY, "Entering free_buffers(%p).\n", pdev);
361
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;
368 }
369#endif
370
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;
377 }
378
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;
386 }
387 }
388 kfree(pdev->fbuf);
389 pdev->fbuf = NULL;
390 }
391
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;
397 }
398 pdev->decompressor = NULL;
399
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);
404 }
405 pdev->image_data = NULL;
406
407 Trace(TRACE_MEMORY, "Leaving free_buffers().\n");
408}
409
410/* The frame & image buffer mess.
411
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.
419
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().
426
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.
430
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.
438
439 Because filling and draining are completely asynchronous processes, this
440 requires some fiddling with pointers and mutexes.
441
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).
457
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.
462 */
463
464/**
465 \brief Find next frame buffer to fill. Take from empty or full list, whichever comes first.
466 */
467static inline int pwc_next_fill_frame(struct pwc_device *pdev)
468{
469 int ret;
470 unsigned long flags;
471
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;
479 }
480 else {
481 pdev->full_frames_tail->next = pdev->fill_frame;
482 pdev->full_frames_tail = pdev->fill_frame;
483 }
484 }
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;
489 }
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;
498 }
499#endif
500 pdev->fill_frame = pdev->full_frames;
501 pdev->full_frames = pdev->full_frames->next;
502 ret = 1;
503 }
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;
511}
512
513
514/**
515 \brief Reset all buffers, pointers and lists, except for the image_used[] buffer.
516
517 If the image_used[] buffer is cleared too, mmap()/VIDIOCSYNC will run into trouble.
518 */
519static void pwc_reset_buffers(struct pwc_device *pdev)
520{
521 int i;
522 unsigned long flags;
523
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;
533 }
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;
539
540 pdev->image_read_pos = 0;
541 pdev->fill_image = 0;
542 spin_unlock_irqrestore(&pdev->ptrlock, flags);
543}
544
545
546/**
547 \brief Do all the handling for getting one frame: get pointer, decompress, advance pointers.
548 */
549static int pwc_handle_frame(struct pwc_device *pdev)
550{
551 int ret = 0;
552 unsigned long flags;
553
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");
560 }
561 else {
562 if (pdev->full_frames == NULL) {
563 Err("Woops. No frames ready.\n");
564 }
565 else {
566 pdev->read_frame = pdev->full_frames;
567 pdev->full_frames = pdev->full_frames->next;
568 pdev->read_frame->next = NULL;
569 }
570
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.
578 */
579 spin_unlock_irqrestore(&pdev->ptrlock, flags);
580 ret = pwc_decompress(pdev);
581 spin_lock_irqsave(&pdev->ptrlock, flags);
582
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;
587 }
588 else {
589 pdev->empty_frames_tail->next = pdev->read_frame;
590 pdev->empty_frames_tail = pdev->read_frame;
591 }
592 pdev->read_frame = NULL;
593 }
594 }
595 spin_unlock_irqrestore(&pdev->ptrlock, flags);
596 return ret;
597}
598
599/**
600 \brief Advance pointers of image buffer (after each user request)
601*/
602static inline void pwc_next_image(struct pwc_device *pdev)
603{
604 pdev->image_used[pdev->fill_image] = 0;
605 pdev->fill_image = (pdev->fill_image + 1) % default_mbufs;
606}
607
608
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.
611 */
612static void pwc_isoc_handler(struct urb *urb, struct pt_regs *regs)
613{
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;
619
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;
625 }
626#ifdef PWC_MAGIC
627 if (pdev->magic != PWC_MAGIC) {
628 Err("isoc_handler() called with bad magic!\n");
629 return;
630 }
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;
635 }
636 if (urb->status != -EINPROGRESS && urb->status != 0) {
637 const char *errmsg;
638
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;
647 }
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.
651 */
652 if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
653 {
654 Info("Too many ISOC errors, bailing out.\n");
655 pdev->error_status = EIO;
656 awake = 1;
657 wake_up_interruptible(&pdev->frameq);
658 }
659 goto handler_end; // ugly, but practical
660 }
661
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;
667 }
668 else {
669 fillptr = fbuf->data + fbuf->filled;
670 }
671
672 /* Reset ISOC error counter. We did get here, after all. */
673 pdev->visoc_errors = 0;
674
675 /* vsync: 0 = don't copy data
676 1 = sync-hunt
677 2 = synched
678 */
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;
688
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++;
694 }
695 else {
696 memmove(fillptr, iso_buf, flen);
697 fillptr += flen;
698 }
699 }
700 fbuf->filled += flen;
701 } /* ..flen > 0 */
702
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.
707 */
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.
712 */
713 if (pdev->type == 730) {
714 unsigned char *ptr = (unsigned char *)fbuf->data;
715
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++;
722 }
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");
728 }
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");
734 }
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!
744 */
745 if (fbuf->filled == 4)
746 pdev->drop_frames++;
747 }
748
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).
751 */
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++;
759 }
760 else {
761 /* Send only once per EOF */
762 awake = 1; /* delay wake_ups */
763
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.
767 */
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);
775 }
776 }
777 fbuf = pdev->fill_frame;
778 }
779 } /* !drop_frames */
780 pdev->vframe_count++;
781 }
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);
795 }
796#endif
797 }
798
799handler_end:
800 if (awake)
801 wake_up_interruptible(&pdev->frameq);
802
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);
807}
808
809
810static int pwc_isoc_init(struct pwc_device *pdev)
811{
812 struct usb_device *udev;
813 struct urb *urb;
814 int i, j, ret;
815
816 struct usb_interface *intf;
817 struct usb_host_interface *idesc = NULL;
818
819 if (pdev == NULL)
820 return -EFAULT;
821 if (pdev->iso_init)
822 return 0;
823 pdev->vsync = 0;
824 udev = pdev->udev;
825
826 /* Get the current alternate interface, adjust packet size */
827 if (!udev->actconfig)
828 return -EFAULT;
829
830 intf = usb_ifnum_to_if(udev, 0);
831 if (intf)
832 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
833
834 if (!idesc)
835 return -EFAULT;
836
837 /* Search video endpoint */
838 pdev->vmax_packet_size = -1;
839 for (i = 0; i < idesc->desc.bNumEndpoints; i++)
840 if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
841 pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
842 break;
843 }
844
845 if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
846 Err("Failed to find packet size for video endpoint in current alternate setting.\n");
847 return -ENFILE; /* Odd error, that should be noticeable */
848 }
849
850 /* Set alternate interface */
851 ret = 0;
852 Trace(TRACE_OPEN, "Setting alternate interface %d\n", pdev->valternate);
853 ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
854 if (ret < 0)
855 return ret;
856
857 for (i = 0; i < MAX_ISO_BUFS; i++) {
858 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
859 if (urb == NULL) {
860 Err("Failed to allocate urb %d\n", i);
861 ret = -ENOMEM;
862 break;
863 }
864 pdev->sbuf[i].urb = urb;
865 Trace(TRACE_MEMORY, "Allocated URB at 0x%p\n", urb);
866 }
867 if (ret) {
868 /* De-allocate in reverse order */
869 while (i >= 0) {
870 if (pdev->sbuf[i].urb != NULL)
871 usb_free_urb(pdev->sbuf[i].urb);
872 pdev->sbuf[i].urb = NULL;
873 i--;
874 }
875 return ret;
876 }
877
878 /* init URB structure */
879 for (i = 0; i < MAX_ISO_BUFS; i++) {
880 urb = pdev->sbuf[i].urb;
881
882 urb->interval = 1; // devik
883 urb->dev = udev;
884 urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
885 urb->transfer_flags = URB_ISO_ASAP;
886 urb->transfer_buffer = pdev->sbuf[i].data;
887 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
888 urb->complete = pwc_isoc_handler;
889 urb->context = pdev;
890 urb->start_frame = 0;
891 urb->number_of_packets = ISO_FRAMES_PER_DESC;
892 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
893 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
894 urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
895 }
896 }
897
898 /* link */
899 for (i = 0; i < MAX_ISO_BUFS; i++) {
900 ret = usb_submit_urb(pdev->sbuf[i].urb, GFP_KERNEL);
901 if (ret)
902 Err("isoc_init() submit_urb %d failed with error %d\n", i, ret);
903 else
904 Trace(TRACE_MEMORY, "URB 0x%p submitted.\n", pdev->sbuf[i].urb);
905 }
906
907 /* All is done... */
908 pdev->iso_init = 1;
909 Trace(TRACE_OPEN, "<< pwc_isoc_init()\n");
910 return 0;
911}
912
913static void pwc_isoc_cleanup(struct pwc_device *pdev)
914{
915 int i;
916
917 Trace(TRACE_OPEN, ">> pwc_isoc_cleanup()\n");
918 if (pdev == NULL)
919 return;
920
921 /* Unlinking ISOC buffers one by one */
922 for (i = 0; i < MAX_ISO_BUFS; i++) {
923 struct urb *urb;
924
925 urb = pdev->sbuf[i].urb;
926 if (urb != 0) {
927 if (pdev->iso_init) {
928 Trace(TRACE_MEMORY, "Unlinking URB %p\n", urb);
929 usb_kill_urb(urb);
930 }
931 Trace(TRACE_MEMORY, "Freeing URB\n");
932 usb_free_urb(urb);
933 pdev->sbuf[i].urb = NULL;
934 }
935 }
936
937 /* Stop camera, but only if we are sure the camera is still there (unplug
938 is signalled by EPIPE)
939 */
940 if (pdev->error_status && pdev->error_status != EPIPE) {
941 Trace(TRACE_OPEN, "Setting alternate interface 0.\n");
942 usb_set_interface(pdev->udev, 0, 0);
943 }
944
945 pdev->iso_init = 0;
946 Trace(TRACE_OPEN, "<< pwc_isoc_cleanup()\n");
947}
948
949int pwc_try_video_mode(struct pwc_device *pdev, int width, int height, int new_fps, int new_compression, int new_snapshot)
950{
951 int ret, start;
952
953 /* Stop isoc stuff */
954 pwc_isoc_cleanup(pdev);
955 /* Reset parameters */
956 pwc_reset_buffers(pdev);
957 /* Try to set video mode... */
958 start = ret = pwc_set_video_mode(pdev, width, height, new_fps, new_compression, new_snapshot);
959 if (ret) {
960 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 1 failed.\n");
961 /* That failed... restore old mode (we know that worked) */
962 start = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
963 if (start) {
964 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 2 failed.\n");
965 }
966 }
967 if (start == 0)
968 {
969 if (pwc_isoc_init(pdev) < 0)
970 {
971 Info("Failed to restart ISOC transfers in pwc_try_video_mode.\n");
972 ret = -EAGAIN; /* let's try again, who knows if it works a second time */
973 }
974 }
975 pdev->drop_frames++; /* try to avoid garbage during switch */
976 return ret; /* Return original error code */
977}
978
979
980/***************************************************************************/
981/* Video4Linux functions */
982
983static int pwc_video_open(struct inode *inode, struct file *file)
984{
985 int i;
986 struct video_device *vdev = video_devdata(file);
987 struct pwc_device *pdev;
988
989 Trace(TRACE_OPEN, ">> video_open called(vdev = 0x%p).\n", vdev);
990
991 pdev = (struct pwc_device *)vdev->priv;
992 if (pdev == NULL)
993 BUG();
994 if (pdev->vopen)
995 return -EBUSY;
996
997 down(&pdev->modlock);
998 if (!pdev->usb_init) {
999 Trace(TRACE_OPEN, "Doing first time initialization.\n");
1000 pdev->usb_init = 1;
1001
1002 if (pwc_trace & TRACE_OPEN)
1003 {
1004 /* Query sensor type */
1005 const char *sensor_type = NULL;
1006 int ret;
1007
1008 ret = pwc_get_cmos_sensor(pdev, &i);
1009 if (ret >= 0)
1010 {
1011 switch(i) {
1012 case 0x00: sensor_type = "Hyundai CMOS sensor"; break;
1013 case 0x20: sensor_type = "Sony CCD sensor + TDA8787"; break;
1014 case 0x2E: sensor_type = "Sony CCD sensor + Exas 98L59"; break;
1015 case 0x2F: sensor_type = "Sony CCD sensor + ADI 9804"; break;
1016 case 0x30: sensor_type = "Sharp CCD sensor + TDA8787"; break;
1017 case 0x3E: sensor_type = "Sharp CCD sensor + Exas 98L59"; break;
1018 case 0x3F: sensor_type = "Sharp CCD sensor + ADI 9804"; break;
1019 case 0x40: sensor_type = "UPA 1021 sensor"; break;
1020 case 0x100: sensor_type = "VGA sensor"; break;
1021 case 0x101: sensor_type = "PAL MR sensor"; break;
1022 default: sensor_type = "unknown type of sensor"; break;
1023 }
1024 }
1025 if (sensor_type != NULL)
1026 Info("This %s camera is equipped with a %s (%d).\n", pdev->vdev->name, sensor_type, i);
1027 }
1028 }
1029
1030 /* Turn on camera */
1031 if (power_save) {
1032 i = pwc_camera_power(pdev, 1);
1033 if (i < 0)
1034 Info("Failed to restore power to the camera! (%d)\n", i);
1035 }
1036 /* Set LED on/off time */
1037 if (pwc_set_leds(pdev, led_on, led_off) < 0)
1038 Info("Failed to set LED on/off time.\n");
1039
1040 pwc_construct(pdev); /* set min/max sizes correct */
1041
1042 /* So far, so good. Allocate memory. */
1043 i = pwc_allocate_buffers(pdev);
1044 if (i < 0) {
1045 Trace(TRACE_OPEN, "Failed to allocate buffer memory.\n");
1046 up(&pdev->modlock);
1047 return i;
1048 }
1049
1050 /* Reset buffers & parameters */
1051 pwc_reset_buffers(pdev);
1052 for (i = 0; i < default_mbufs; i++)
1053 pdev->image_used[i] = 0;
1054 pdev->vframe_count = 0;
1055 pdev->vframes_dumped = 0;
1056 pdev->vframes_error = 0;
1057 pdev->visoc_errors = 0;
1058 pdev->error_status = 0;
1059#if PWC_DEBUG
1060 pdev->sequence = 0;
1061#endif
1062 pwc_construct(pdev); /* set min/max sizes correct */
1063
1064 /* Set some defaults */
1065 pdev->vsnapshot = 0;
1066
1067 /* Start iso pipe for video; first try the last used video size
1068 (or the default one); if that fails try QCIF/10 or QSIF/10;
1069 it that fails too, give up.
1070 */
1071 i = pwc_set_video_mode(pdev, pwc_image_sizes[pdev->vsize].x, pwc_image_sizes[pdev->vsize].y, pdev->vframes, pdev->vcompression, 0);
1072 if (i) {
1073 Trace(TRACE_OPEN, "First attempt at set_video_mode failed.\n");
1074 if (pdev->type == 730 || pdev->type == 740 || pdev->type == 750)
1075 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QSIF].x, pwc_image_sizes[PSZ_QSIF].y, 10, pdev->vcompression, 0);
1076 else
1077 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QCIF].x, pwc_image_sizes[PSZ_QCIF].y, 10, pdev->vcompression, 0);
1078 }
1079 if (i) {
1080 Trace(TRACE_OPEN, "Second attempt at set_video_mode failed.\n");
1081 up(&pdev->modlock);
1082 return i;
1083 }
1084
1085 i = pwc_isoc_init(pdev);
1086 if (i) {
1087 Trace(TRACE_OPEN, "Failed to init ISOC stuff = %d.\n", i);
1088 up(&pdev->modlock);
1089 return i;
1090 }
1091
1092 pdev->vopen++;
1093 file->private_data = vdev;
1094 up(&pdev->modlock);
1095 Trace(TRACE_OPEN, "<< video_open() returns 0.\n");
1096 return 0;
1097}
1098
1099/* Note that all cleanup is done in the reverse order as in _open */
1100static int pwc_video_close(struct inode *inode, struct file *file)
1101{
1102 struct video_device *vdev = file->private_data;
1103 struct pwc_device *pdev;
1104 int i;
1105
1106 Trace(TRACE_OPEN, ">> video_close called(vdev = 0x%p).\n", vdev);
1107
1108 pdev = (struct pwc_device *)vdev->priv;
1109 if (pdev->vopen == 0)
1110 Info("video_close() called on closed device?\n");
1111
1112 /* Dump statistics, but only if a reasonable amount of frames were
1113 processed (to prevent endless log-entries in case of snap-shot
1114 programs)
1115 */
1116 if (pdev->vframe_count > 20)
1117 Info("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev->vframe_count, pdev->vframes_dumped, pdev->vframes_error);
1118
1119 switch (pdev->type)
1120 {
1121 case 675:
1122 case 680:
1123 case 690:
1124 case 720:
1125 case 730:
1126 case 740:
1127 case 750:
1128/* pwc_dec23_exit(); *//* Timon & Kiara */
1129 break;
1130 case 645:
1131 case 646:
1132/* pwc_dec1_exit(); */
1133 break;
1134 }
1135
1136 pwc_isoc_cleanup(pdev);
1137 pwc_free_buffers(pdev);
1138
1139 /* Turn off LEDS and power down camera, but only when not unplugged */
1140 if (pdev->error_status != EPIPE) {
1141 /* Turn LEDs off */
1142 if (pwc_set_leds(pdev, 0, 0) < 0)
1143 Info("Failed to set LED on/off time.\n");
1144 if (power_save) {
1145 i = pwc_camera_power(pdev, 0);
1146 if (i < 0)
1147 Err("Failed to power down camera (%d)\n", i);
1148 }
1149 }
1150 pdev->vopen = 0;
1151 Trace(TRACE_OPEN, "<< video_close()\n");
1152 return 0;
1153}
1154
1155/*
1156 * FIXME: what about two parallel reads ????
1157 * ANSWER: Not supported. You can't open the device more than once,
1158 despite what the V4L1 interface says. First, I don't see
1159 the need, second there's no mechanism of alerting the
1160 2nd/3rd/... process of events like changing image size.
1161 And I don't see the point of blocking that for the
1162 2nd/3rd/... process.
1163 In multi-threaded environments reading parallel from any
1164 device is tricky anyhow.
1165 */
1166
1167static ssize_t pwc_video_read(struct file *file, char __user * buf,
1168 size_t count, loff_t *ppos)
1169{
1170 struct video_device *vdev = file->private_data;
1171 struct pwc_device *pdev;
1172 int noblock = file->f_flags & O_NONBLOCK;
1173 DECLARE_WAITQUEUE(wait, current);
1174 int bytes_to_read;
1175
1176 Trace(TRACE_READ, "video_read(0x%p, %p, %zu) called.\n", vdev, buf, count);
1177 if (vdev == NULL)
1178 return -EFAULT;
1179 pdev = vdev->priv;
1180 if (pdev == NULL)
1181 return -EFAULT;
1182 if (pdev->error_status)
1183 return -pdev->error_status; /* Something happened, report what. */
1184
1185 /* In case we're doing partial reads, we don't have to wait for a frame */
1186 if (pdev->image_read_pos == 0) {
1187 /* Do wait queueing according to the (doc)book */
1188 add_wait_queue(&pdev->frameq, &wait);
1189 while (pdev->full_frames == NULL) {
1190 /* Check for unplugged/etc. here */
1191 if (pdev->error_status) {
1192 remove_wait_queue(&pdev->frameq, &wait);
1193 set_current_state(TASK_RUNNING);
1194 return -pdev->error_status ;
1195 }
1196 if (noblock) {
1197 remove_wait_queue(&pdev->frameq, &wait);
1198 set_current_state(TASK_RUNNING);
1199 return -EWOULDBLOCK;
1200 }
1201 if (signal_pending(current)) {
1202 remove_wait_queue(&pdev->frameq, &wait);
1203 set_current_state(TASK_RUNNING);
1204 return -ERESTARTSYS;
1205 }
1206 schedule();
1207 set_current_state(TASK_INTERRUPTIBLE);
1208 }
1209 remove_wait_queue(&pdev->frameq, &wait);
1210 set_current_state(TASK_RUNNING);
1211
1212 /* Decompress and release frame */
1213 if (pwc_handle_frame(pdev))
1214 return -EFAULT;
1215 }
1216
1217 Trace(TRACE_READ, "Copying data to user space.\n");
1218 if (pdev->vpalette == VIDEO_PALETTE_RAW)
1219 bytes_to_read = pdev->frame_size;
1220 else
1221 bytes_to_read = pdev->view.size;
1222
1223 /* copy bytes to user space; we allow for partial reads */
1224 if (count + pdev->image_read_pos > bytes_to_read)
1225 count = bytes_to_read - pdev->image_read_pos;
1226 if (copy_to_user(buf, pdev->image_ptr[pdev->fill_image] + pdev->image_read_pos, count))
1227 return -EFAULT;
1228 pdev->image_read_pos += count;
1229 if (pdev->image_read_pos >= bytes_to_read) { /* All data has been read */
1230 pdev->image_read_pos = 0;
1231 pwc_next_image(pdev);
1232 }
1233 return count;
1234}
1235
1236static unsigned int pwc_video_poll(struct file *file, poll_table *wait)
1237{
1238 struct video_device *vdev = file->private_data;
1239 struct pwc_device *pdev;
1240
1241 if (vdev == NULL)
1242 return -EFAULT;
1243 pdev = vdev->priv;
1244 if (pdev == NULL)
1245 return -EFAULT;
1246
1247 poll_wait(file, &pdev->frameq, wait);
1248 if (pdev->error_status)
1249 return POLLERR;
1250 if (pdev->full_frames != NULL) /* we have frames waiting */
1251 return (POLLIN | POLLRDNORM);
1252
1253 return 0;
1254}
1255
1256static int pwc_video_do_ioctl(struct inode *inode, struct file *file,
1257 unsigned int cmd, void *arg)
1258{
1259 struct video_device *vdev = file->private_data;
1260 struct pwc_device *pdev;
1261 DECLARE_WAITQUEUE(wait, current);
1262
1263 if (vdev == NULL)
1264 return -EFAULT;
1265 pdev = vdev->priv;
1266 if (pdev == NULL)
1267 return -EFAULT;
1268
1269 switch (cmd) {
1270 /* Query cabapilities */
1271 case VIDIOCGCAP:
1272 {
1273 struct video_capability *caps = arg;
1274
1275 strcpy(caps->name, vdev->name);
1276 caps->type = VID_TYPE_CAPTURE;
1277 caps->channels = 1;
1278 caps->audios = 1;
1279 caps->minwidth = pdev->view_min.x;
1280 caps->minheight = pdev->view_min.y;
1281 caps->maxwidth = pdev->view_max.x;
1282 caps->maxheight = pdev->view_max.y;
1283 break;
1284 }
1285
1286 /* Channel functions (simulate 1 channel) */
1287 case VIDIOCGCHAN:
1288 {
1289 struct video_channel *v = arg;
1290
1291 if (v->channel != 0)
1292 return -EINVAL;
1293 v->flags = 0;
1294 v->tuners = 0;
1295 v->type = VIDEO_TYPE_CAMERA;
1296 strcpy(v->name, "Webcam");
1297 return 0;
1298 }
1299
1300 case VIDIOCSCHAN:
1301 {
1302 /* The spec says the argument is an integer, but
1303 the bttv driver uses a video_channel arg, which
1304 makes sense becasue it also has the norm flag.
1305 */
1306 struct video_channel *v = arg;
1307 if (v->channel != 0)
1308 return -EINVAL;
1309 return 0;
1310 }
1311
1312
1313 /* Picture functions; contrast etc. */
1314 case VIDIOCGPICT:
1315 {
1316 struct video_picture *p = arg;
1317 int val;
1318
1319 val = pwc_get_brightness(pdev);
1320 if (val >= 0)
1321 p->brightness = val;
1322 else
1323 p->brightness = 0xffff;
1324 val = pwc_get_contrast(pdev);
1325 if (val >= 0)
1326 p->contrast = val;
1327 else
1328 p->contrast = 0xffff;
1329 /* Gamma, Whiteness, what's the difference? :) */
1330 val = pwc_get_gamma(pdev);
1331 if (val >= 0)
1332 p->whiteness = val;
1333 else
1334 p->whiteness = 0xffff;
1335 val = pwc_get_saturation(pdev);
1336 if (val >= 0)
1337 p->colour = val;
1338 else
1339 p->colour = 0xffff;
1340 p->depth = 24;
1341 p->palette = pdev->vpalette;
1342 p->hue = 0xFFFF; /* N/A */
1343 break;
1344 }
1345
1346 case VIDIOCSPICT:
1347 {
1348 struct video_picture *p = arg;
1349 /*
1350 * FIXME: Suppose we are mid read
1351 ANSWER: No problem: the firmware of the camera
1352 can handle brightness/contrast/etc
1353 changes at _any_ time, and the palette
1354 is used exactly once in the uncompress
1355 routine.
1356 */
1357 pwc_set_brightness(pdev, p->brightness);
1358 pwc_set_contrast(pdev, p->contrast);
1359 pwc_set_gamma(pdev, p->whiteness);
1360 pwc_set_saturation(pdev, p->colour);
1361 if (p->palette && p->palette != pdev->vpalette) {
1362 switch (p->palette) {
1363 case VIDEO_PALETTE_YUV420P:
1364 case VIDEO_PALETTE_RAW:
1365 pdev->vpalette = p->palette;
1366 return pwc_try_video_mode(pdev, pdev->image.x, pdev->image.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1367 break;
1368 default:
1369 return -EINVAL;
1370 break;
1371 }
1372 }
1373 break;
1374 }
1375
1376 /* Window/size parameters */
1377 case VIDIOCGWIN:
1378 {
1379 struct video_window *vw = arg;
1380
1381 vw->x = 0;
1382 vw->y = 0;
1383 vw->width = pdev->view.x;
1384 vw->height = pdev->view.y;
1385 vw->chromakey = 0;
1386 vw->flags = (pdev->vframes << PWC_FPS_SHIFT) |
1387 (pdev->vsnapshot ? PWC_FPS_SNAPSHOT : 0);
1388 break;
1389 }
1390
1391 case VIDIOCSWIN:
1392 {
1393 struct video_window *vw = arg;
1394 int fps, snapshot, ret;
1395
1396 fps = (vw->flags & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
1397 snapshot = vw->flags & PWC_FPS_SNAPSHOT;
1398 if (fps == 0)
1399 fps = pdev->vframes;
1400 if (pdev->view.x == vw->width && pdev->view.y && fps == pdev->vframes && snapshot == pdev->vsnapshot)
1401 return 0;
1402 ret = pwc_try_video_mode(pdev, vw->width, vw->height, fps, pdev->vcompression, snapshot);
1403 if (ret)
1404 return ret;
1405 break;
1406 }
1407
1408 /* We don't have overlay support (yet) */
1409 case VIDIOCGFBUF:
1410 {
1411 struct video_buffer *vb = arg;
1412
1413 memset(vb,0,sizeof(*vb));
1414 break;
1415 }
1416
1417 /* mmap() functions */
1418 case VIDIOCGMBUF:
1419 {
1420 /* Tell the user program how much memory is needed for a mmap() */
1421 struct video_mbuf *vm = arg;
1422 int i;
1423
1424 memset(vm, 0, sizeof(*vm));
1425 vm->size = default_mbufs * pdev->len_per_image;
1426 vm->frames = default_mbufs; /* double buffering should be enough for most applications */
1427 for (i = 0; i < default_mbufs; i++)
1428 vm->offsets[i] = i * pdev->len_per_image;
1429 break;
1430 }
1431
1432 case VIDIOCMCAPTURE:
1433 {
1434 /* Start capture into a given image buffer (called 'frame' in video_mmap structure) */
1435 struct video_mmap *vm = arg;
1436
1437 Trace(TRACE_READ, "VIDIOCMCAPTURE: %dx%d, frame %d, format %d\n", vm->width, vm->height, vm->frame, vm->format);
1438 if (vm->frame < 0 || vm->frame >= default_mbufs)
1439 return -EINVAL;
1440
1441 /* xawtv is nasty. It probes the available palettes
1442 by setting a very small image size and trying
1443 various palettes... The driver doesn't support
1444 such small images, so I'm working around it.
1445 */
1446 if (vm->format)
1447 {
1448 switch (vm->format)
1449 {
1450 case VIDEO_PALETTE_YUV420P:
1451 case VIDEO_PALETTE_RAW:
1452 break;
1453 default:
1454 return -EINVAL;
1455 break;
1456 }
1457 }
1458
1459 if ((vm->width != pdev->view.x || vm->height != pdev->view.y) &&
1460 (vm->width >= pdev->view_min.x && vm->height >= pdev->view_min.y)) {
1461 int ret;
1462
1463 Trace(TRACE_OPEN, "VIDIOCMCAPTURE: changing size to please xawtv :-(.\n");
1464 ret = pwc_try_video_mode(pdev, vm->width, vm->height, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1465 if (ret)
1466 return ret;
1467 } /* ... size mismatch */
1468
1469 /* FIXME: should we lock here? */
1470 if (pdev->image_used[vm->frame])
1471 return -EBUSY; /* buffer wasn't available. Bummer */
1472 pdev->image_used[vm->frame] = 1;
1473
1474 /* Okay, we're done here. In the SYNC call we wait until a
1475 frame comes available, then expand image into the given
1476 buffer.
1477 In contrast to the CPiA cam the Philips cams deliver a
1478 constant stream, almost like a grabber card. Also,
1479 we have separate buffers for the rawdata and the image,
1480 meaning we can nearly always expand into the requested buffer.
1481 */
1482 Trace(TRACE_READ, "VIDIOCMCAPTURE done.\n");
1483 break;
1484 }
1485
1486 case VIDIOCSYNC:
1487 {
1488 /* The doc says: "Whenever a buffer is used it should
1489 call VIDIOCSYNC to free this frame up and continue."
1490
1491 The only odd thing about this whole procedure is
1492 that MCAPTURE flags the buffer as "in use", and
1493 SYNC immediately unmarks it, while it isn't
1494 after SYNC that you know that the buffer actually
1495 got filled! So you better not start a CAPTURE in
1496 the same frame immediately (use double buffering).
1497 This is not a problem for this cam, since it has
1498 extra intermediate buffers, but a hardware
1499 grabber card will then overwrite the buffer
1500 you're working on.
1501 */
1502 int *mbuf = arg;
1503 int ret;
1504
1505 Trace(TRACE_READ, "VIDIOCSYNC called (%d).\n", *mbuf);
1506
1507 /* bounds check */
1508 if (*mbuf < 0 || *mbuf >= default_mbufs)
1509 return -EINVAL;
1510 /* check if this buffer was requested anyway */
1511 if (pdev->image_used[*mbuf] == 0)
1512 return -EINVAL;
1513
1514 /* Add ourselves to the frame wait-queue.
1515
1516 FIXME: needs auditing for safety.
1517 QUESTION: In what respect? I think that using the
1518 frameq is safe now.
1519 */
1520 add_wait_queue(&pdev->frameq, &wait);
1521 while (pdev->full_frames == NULL) {
1522 if (pdev->error_status) {
1523 remove_wait_queue(&pdev->frameq, &wait);
1524 set_current_state(TASK_RUNNING);
1525 return -pdev->error_status;
1526 }
1527
1528 if (signal_pending(current)) {
1529 remove_wait_queue(&pdev->frameq, &wait);
1530 set_current_state(TASK_RUNNING);
1531 return -ERESTARTSYS;
1532 }
1533 schedule();
1534 set_current_state(TASK_INTERRUPTIBLE);
1535 }
1536 remove_wait_queue(&pdev->frameq, &wait);
1537 set_current_state(TASK_RUNNING);
1538
1539 /* The frame is ready. Expand in the image buffer
1540 requested by the user. I don't care if you
1541 mmap() 5 buffers and request data in this order:
1542 buffer 4 2 3 0 1 2 3 0 4 3 1 . . .
1543 Grabber hardware may not be so forgiving.
1544 */
1545 Trace(TRACE_READ, "VIDIOCSYNC: frame ready.\n");
1546 pdev->fill_image = *mbuf; /* tell in which buffer we want the image to be expanded */
1547 /* Decompress, etc */
1548 ret = pwc_handle_frame(pdev);
1549 pdev->image_used[*mbuf] = 0;
1550 if (ret)
1551 return -EFAULT;
1552 break;
1553 }
1554
1555 case VIDIOCGAUDIO:
1556 {
1557 struct video_audio *v = arg;
1558
1559 strcpy(v->name, "Microphone");
1560 v->audio = -1; /* unknown audio minor */
1561 v->flags = 0;
1562 v->mode = VIDEO_SOUND_MONO;
1563 v->volume = 0;
1564 v->bass = 0;
1565 v->treble = 0;
1566 v->balance = 0x8000;
1567 v->step = 1;
1568 break;
1569 }
1570
1571 case VIDIOCSAUDIO:
1572 {
1573 /* Dummy: nothing can be set */
1574 break;
1575 }
1576
1577 case VIDIOCGUNIT:
1578 {
1579 struct video_unit *vu = arg;
1580
1581 vu->video = pdev->vdev->minor & 0x3F;
1582 vu->audio = -1; /* not known yet */
1583 vu->vbi = -1;
1584 vu->radio = -1;
1585 vu->teletext = -1;
1586 break;
1587 }
1588 default:
1589 return pwc_ioctl(pdev, cmd, arg);
1590 } /* ..switch */
1591 return 0;
1592}
1593
1594static int pwc_video_ioctl(struct inode *inode, struct file *file,
1595 unsigned int cmd, unsigned long arg)
1596{
1597 return video_usercopy(inode, file, cmd, arg, pwc_video_do_ioctl);
1598}
1599
1600
1601static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma)
1602{
1603 struct video_device *vdev = file->private_data;
1604 struct pwc_device *pdev;
1605 unsigned long start = vma->vm_start;
1606 unsigned long size = vma->vm_end-vma->vm_start;
1607 unsigned long page, pos;
1608
1609 Trace(TRACE_MEMORY, "mmap(0x%p, 0x%lx, %lu) called.\n", vdev, start, size);
1610 pdev = vdev->priv;
1611
1612 vma->vm_flags |= VM_IO;
1613
1614 pos = (unsigned long)pdev->image_data;
1615 while (size > 0) {
1616 page = vmalloc_to_pfn((void *)pos);
1617 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1618 return -EAGAIN;
1619
1620 start += PAGE_SIZE;
1621 pos += PAGE_SIZE;
1622 if (size > PAGE_SIZE)
1623 size -= PAGE_SIZE;
1624 else
1625 size = 0;
1626 }
1627
1628 return 0;
1629}
1630
1631/***************************************************************************/
1632/* USB functions */
1633
1634/* This function gets called when a new device is plugged in or the usb core
1635 * is loaded.
1636 */
1637
1638static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
1639{
1640 struct usb_device *udev = interface_to_usbdev(intf);
1641 struct pwc_device *pdev = NULL;
1642 int vendor_id, product_id, type_id;
1643 int i, hint;
1644 int features = 0;
1645 int video_nr = -1; /* default: use next available device */
1646 char serial_number[30], *name;
1647
1648 /* Check if we can handle this device */
1649 Trace(TRACE_PROBE, "probe() called [%04X %04X], if %d\n",
1650 le16_to_cpu(udev->descriptor.idVendor),
1651 le16_to_cpu(udev->descriptor.idProduct),
1652 intf->altsetting->desc.bInterfaceNumber);
1653
1654 /* the interfaces are probed one by one. We are only interested in the
1655 video interface (0) now.
1656 Interface 1 is the Audio Control, and interface 2 Audio itself.
1657 */
1658 if (intf->altsetting->desc.bInterfaceNumber > 0)
1659 return -ENODEV;
1660
1661 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
1662 product_id = le16_to_cpu(udev->descriptor.idProduct);
1663
1664 if (vendor_id == 0x0471) {
1665 switch (product_id) {
1666 case 0x0302:
1667 Info("Philips PCA645VC USB webcam detected.\n");
1668 name = "Philips 645 webcam";
1669 type_id = 645;
1670 break;
1671 case 0x0303:
1672 Info("Philips PCA646VC USB webcam detected.\n");
1673 name = "Philips 646 webcam";
1674 type_id = 646;
1675 break;
1676 case 0x0304:
1677 Info("Askey VC010 type 2 USB webcam detected.\n");
1678 name = "Askey VC010 webcam";
1679 type_id = 646;
1680 break;
1681 case 0x0307:
1682 Info("Philips PCVC675K (Vesta) USB webcam detected.\n");
1683 name = "Philips 675 webcam";
1684 type_id = 675;
1685 break;
1686 case 0x0308:
1687 Info("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
1688 name = "Philips 680 webcam";
1689 type_id = 680;
1690 break;
1691 case 0x030C:
1692 Info("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
1693 name = "Philips 690 webcam";
1694 type_id = 690;
1695 break;
1696 case 0x0310:
1697 Info("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
1698 name = "Philips 730 webcam";
1699 type_id = 730;
1700 break;
1701 case 0x0311:
1702 Info("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
1703 name = "Philips 740 webcam";
1704 type_id = 740;
1705 break;
1706 case 0x0312:
1707 Info("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
1708 name = "Philips 750 webcam";
1709 type_id = 750;
1710 break;
1711 case 0x0313:
1712 Info("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
1713 name = "Philips 720K/40 webcam";
1714 type_id = 720;
1715 break;
1716 default:
1717 return -ENODEV;
1718 break;
1719 }
1720 }
1721 else if (vendor_id == 0x069A) {
1722 switch(product_id) {
1723 case 0x0001:
1724 Info("Askey VC010 type 1 USB webcam detected.\n");
1725 name = "Askey VC010 webcam";
1726 type_id = 645;
1727 break;
1728 default:
1729 return -ENODEV;
1730 break;
1731 }
1732 }
1733 else if (vendor_id == 0x046d) {
1734 switch(product_id) {
1735 case 0x08b0:
1736 Info("Logitech QuickCam Pro 3000 USB webcam detected.\n");
1737 name = "Logitech QuickCam Pro 3000";
1738 type_id = 740; /* CCD sensor */
1739 break;
1740 case 0x08b1:
1741 Info("Logitech QuickCam Notebook Pro USB webcam detected.\n");
1742 name = "Logitech QuickCam Notebook Pro";
1743 type_id = 740; /* CCD sensor */
1744 break;
1745 case 0x08b2:
1746 Info("Logitech QuickCam 4000 Pro USB webcam detected.\n");
1747 name = "Logitech QuickCam Pro 4000";
1748 type_id = 740; /* CCD sensor */
1749 break;
1750 case 0x08b3:
1751 Info("Logitech QuickCam Zoom USB webcam detected.\n");
1752 name = "Logitech QuickCam Zoom";
1753 type_id = 740; /* CCD sensor */
1754 break;
1755 case 0x08B4:
1756 Info("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
1757 name = "Logitech QuickCam Zoom";
1758 type_id = 740; /* CCD sensor */
1759 break;
1760 case 0x08b5:
1761 Info("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
1762 name = "Logitech QuickCam Orbit";
1763 type_id = 740; /* CCD sensor */
1764 features |= FEATURE_MOTOR_PANTILT;
1765 break;
1766 case 0x08b6:
1767 case 0x08b7:
1768 case 0x08b8:
1769 Info("Logitech QuickCam detected (reserved ID).\n");
1770 name = "Logitech QuickCam (res.)";
1771 type_id = 730; /* Assuming CMOS */
1772 break;
1773 default:
1774 return -ENODEV;
1775 break;
1776 }
1777 }
1778 else if (vendor_id == 0x055d) {
1779 /* I don't know the difference between the C10 and the C30;
1780 I suppose the difference is the sensor, but both cameras
1781 work equally well with a type_id of 675
1782 */
1783 switch(product_id) {
1784 case 0x9000:
1785 Info("Samsung MPC-C10 USB webcam detected.\n");
1786 name = "Samsung MPC-C10";
1787 type_id = 675;
1788 break;
1789 case 0x9001:
1790 Info("Samsung MPC-C30 USB webcam detected.\n");
1791 name = "Samsung MPC-C30";
1792 type_id = 675;
1793 break;
1794 default:
1795 return -ENODEV;
1796 break;
1797 }
1798 }
1799 else if (vendor_id == 0x041e) {
1800 switch(product_id) {
1801 case 0x400c:
1802 Info("Creative Labs Webcam 5 detected.\n");
1803 name = "Creative Labs Webcam 5";
1804 type_id = 730;
1805 break;
1806 case 0x4011:
1807 Info("Creative Labs Webcam Pro Ex detected.\n");
1808 name = "Creative Labs Webcam Pro Ex";
1809 type_id = 740;
1810 break;
1811 default:
1812 return -ENODEV;
1813 break;
1814 }
1815 }
1816 else if (vendor_id == 0x04cc) {
1817 switch(product_id) {
1818 case 0x8116:
1819 Info("Sotec Afina Eye USB webcam detected.\n");
1820 name = "Sotec Afina Eye";
1821 type_id = 730;
1822 break;
1823 default:
1824 return -ENODEV;
1825 break;
1826 }
1827 }
1828 else if (vendor_id == 0x06be) {
1829 switch(product_id) {
1830 case 0x8116:
1831 /* This is essentially the same cam as the Sotec Afina Eye */
1832 Info("AME Co. Afina Eye USB webcam detected.\n");
1833 name = "AME Co. Afina Eye";
1834 type_id = 750;
1835 break;
1836 default:
1837 return -ENODEV;
1838 break;
1839 }
1840
1841 }
1842 else if (vendor_id == 0x0d81) {
1843 switch(product_id) {
1844 case 0x1900:
1845 Info("Visionite VCS-UC300 USB webcam detected.\n");
1846 name = "Visionite VCS-UC300";
1847 type_id = 740; /* CCD sensor */
1848 break;
1849 case 0x1910:
1850 Info("Visionite VCS-UM100 USB webcam detected.\n");
1851 name = "Visionite VCS-UM100";
1852 type_id = 730; /* CMOS sensor */
1853 break;
1854 default:
1855 return -ENODEV;
1856 break;
1857 }
1858 }
1859 else
1860 return -ENODEV; /* Not any of the know types; but the list keeps growing. */
1861
1862 memset(serial_number, 0, 30);
1863 usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
1864 Trace(TRACE_PROBE, "Device serial number is %s\n", serial_number);
1865
1866 if (udev->descriptor.bNumConfigurations > 1)
1867 Info("Warning: more than 1 configuration available.\n");
1868
1869 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1870 pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
1871 if (pdev == NULL) {
1872 Err("Oops, could not allocate memory for pwc_device.\n");
1873 return -ENOMEM;
1874 }
1875 pdev->type = type_id;
1876 pdev->vsize = default_size;
1877 pdev->vframes = default_fps;
1878 strcpy(pdev->serial, serial_number);
1879 pdev->features = features;
1880 if (vendor_id == 0x046D && product_id == 0x08B5)
1881 {
1882 /* Logitech QuickCam Orbit
1883 The ranges have been determined experimentally; they may differ from cam to cam.
1884 Also, the exact ranges left-right and up-down are different for my cam
1885 */
1886 pdev->angle_range.pan_min = -7000;
1887 pdev->angle_range.pan_max = 7000;
1888 pdev->angle_range.tilt_min = -3000;
1889 pdev->angle_range.tilt_max = 2500;
1890 }
1891
1892 init_MUTEX(&pdev->modlock);
1893 spin_lock_init(&pdev->ptrlock);
1894
1895 pdev->udev = udev;
1896 init_waitqueue_head(&pdev->frameq);
1897 pdev->vcompression = pwc_preferred_compression;
1898
1899 /* Allocate video_device structure */
1900 pdev->vdev = video_device_alloc();
1901 if (pdev->vdev == 0)
1902 {
1903 Err("Err, cannot allocate video_device struture. Failing probe.");
1904 kfree(pdev);
1905 return -ENOMEM;
1906 }
1907 memcpy(pdev->vdev, &pwc_template, sizeof(pwc_template));
1908 strcpy(pdev->vdev->name, name);
1909 pdev->vdev->owner = THIS_MODULE;
1910 video_set_drvdata(pdev->vdev, pdev);
1911
1912 pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1913 Trace(TRACE_PROBE, "Release: %04x\n", pdev->release);
1914
1915 /* Now search device_hint[] table for a match, so we can hint a node number. */
1916 for (hint = 0; hint < MAX_DEV_HINTS; hint++) {
1917 if (((device_hint[hint].type == -1) || (device_hint[hint].type == pdev->type)) &&
1918 (device_hint[hint].pdev == NULL)) {
1919 /* so far, so good... try serial number */
1920 if ((device_hint[hint].serial_number[0] == '*') || !strcmp(device_hint[hint].serial_number, serial_number)) {
1921 /* match! */
1922 video_nr = device_hint[hint].device_node;
1923 Trace(TRACE_PROBE, "Found hint, will try to register as /dev/video%d\n", video_nr);
1924 break;
1925 }
1926 }
1927 }
1928
1929 pdev->vdev->release = video_device_release;
1930 i = video_register_device(pdev->vdev, VFL_TYPE_GRABBER, video_nr);
1931 if (i < 0) {
1932 Err("Failed to register as video device (%d).\n", i);
1933 video_device_release(pdev->vdev); /* Drip... drip... drip... */
1934 kfree(pdev); /* Oops, no memory leaks please */
1935 return -EIO;
1936 }
1937 else {
1938 Info("Registered as /dev/video%d.\n", pdev->vdev->minor & 0x3F);
1939 }
1940
1941 /* occupy slot */
1942 if (hint < MAX_DEV_HINTS)
1943 device_hint[hint].pdev = pdev;
1944
1945 Trace(TRACE_PROBE, "probe() function returning struct at 0x%p.\n", pdev);
1946 usb_set_intfdata (intf, pdev);
1947 return 0;
1948}
1949
1950/* The user janked out the cable... */
1951static void usb_pwc_disconnect(struct usb_interface *intf)
1952{
1953 struct pwc_device *pdev;
1954 int hint;
1955
1956 lock_kernel();
1957 pdev = usb_get_intfdata (intf);
1958 usb_set_intfdata (intf, NULL);
1959 if (pdev == NULL) {
1960 Err("pwc_disconnect() Called without private pointer.\n");
1961 goto disconnect_out;
1962 }
1963 if (pdev->udev == NULL) {
1964 Err("pwc_disconnect() already called for %p\n", pdev);
1965 goto disconnect_out;
1966 }
1967 if (pdev->udev != interface_to_usbdev(intf)) {
1968 Err("pwc_disconnect() Woops: pointer mismatch udev/pdev.\n");
1969 goto disconnect_out;
1970 }
1971#ifdef PWC_MAGIC
1972 if (pdev->magic != PWC_MAGIC) {
1973 Err("pwc_disconnect() Magic number failed. Consult your scrolls and try again.\n");
1974 goto disconnect_out;
1975 }
1976#endif
1977
1978 /* We got unplugged; this is signalled by an EPIPE error code */
1979 if (pdev->vopen) {
1980 Info("Disconnected while webcam is in use!\n");
1981 pdev->error_status = EPIPE;
1982 }
1983
1984 /* Alert waiting processes */
1985 wake_up_interruptible(&pdev->frameq);
1986 /* Wait until device is closed */
1987 while (pdev->vopen)
1988 schedule();
1989 /* Device is now closed, so we can safely unregister it */
1990 Trace(TRACE_PROBE, "Unregistering video device in disconnect().\n");
1991 video_unregister_device(pdev->vdev);
1992
1993 /* Free memory (don't set pdev to 0 just yet) */
1994 kfree(pdev);
1995
1996disconnect_out:
1997 /* search device_hint[] table if we occupy a slot, by any chance */
1998 for (hint = 0; hint < MAX_DEV_HINTS; hint++)
1999 if (device_hint[hint].pdev == pdev)
2000 device_hint[hint].pdev = NULL;
2001
2002 unlock_kernel();
2003}
2004
2005
2006/* *grunt* We have to do atoi ourselves :-( */
2007static int pwc_atoi(const char *s)
2008{
2009 int k = 0;
2010
2011 k = 0;
2012 while (*s != '\0' && *s >= '0' && *s <= '9') {
2013 k = 10 * k + (*s - '0');
2014 s++;
2015 }
2016 return k;
2017}
2018
2019
2020/*
2021 * Initialization code & module stuff
2022 */
2023
2024static char size[10];
2025static int fps = 0;
2026static int fbufs = 0;
2027static int mbufs = 0;
2028static int trace = -1;
2029static int compression = -1;
2030static int leds[2] = { -1, -1 };
2031static char *dev_hint[MAX_DEV_HINTS] = { };
2032
2033module_param_string(size, size, sizeof(size), 0);
2034MODULE_PARM_DESC(size, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
2035module_param(fps, int, 0000);
2036MODULE_PARM_DESC(fps, "Initial frames per second. Varies with model, useful range 5-30");
2037module_param(fbufs, int, 0000);
2038MODULE_PARM_DESC(fbufs, "Number of internal frame buffers to reserve");
2039module_param(mbufs, int, 0000);
2040MODULE_PARM_DESC(mbufs, "Number of external (mmap()ed) image buffers");
2041module_param(trace, int, 0000);
2042MODULE_PARM_DESC(trace, "For debugging purposes");
2043module_param(power_save, bool, 0000);
2044MODULE_PARM_DESC(power_save, "Turn power save feature in camera on or off");
2045module_param(compression, int, 0000);
2046MODULE_PARM_DESC(compression, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
2047module_param_array(leds, int, NULL, 0000);
2048MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
2049module_param_array(dev_hint, charp, NULL, 0000);
2050MODULE_PARM_DESC(dev_hint, "Device node hints");
2051
2052MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
2053MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
2054MODULE_LICENSE("GPL");
2055
2056static int __init usb_pwc_init(void)
2057{
2058 int i, sz;
2059 char *sizenames[PSZ_MAX] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };
2060
2061 Info("Philips webcam module version " PWC_VERSION " loaded.\n");
2062 Info("Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.\n");
2063 Info("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
2064 Info("the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
2065
2066 if (fps) {
2067 if (fps < 4 || fps > 30) {
2068 Err("Framerate out of bounds (4-30).\n");
2069 return -EINVAL;
2070 }
2071 default_fps = fps;
2072 Info("Default framerate set to %d.\n", default_fps);
2073 }
2074
2075 if (size[0]) {
2076 /* string; try matching with array */
2077 for (sz = 0; sz < PSZ_MAX; sz++) {
2078 if (!strcmp(sizenames[sz], size)) { /* Found! */
2079 default_size = sz;
2080 break;
2081 }
2082 }
2083 if (sz == PSZ_MAX) {
2084 Err("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
2085 return -EINVAL;
2086 }
2087 Info("Default image size set to %s [%dx%d].\n", sizenames[default_size], pwc_image_sizes[default_size].x, pwc_image_sizes[default_size].y);
2088 }
2089 if (mbufs) {
2090 if (mbufs < 1 || mbufs > MAX_IMAGES) {
2091 Err("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES);
2092 return -EINVAL;
2093 }
2094 default_mbufs = mbufs;
2095 Info("Number of image buffers set to %d.\n", default_mbufs);
2096 }
2097 if (fbufs) {
2098 if (fbufs < 2 || fbufs > MAX_FRAMES) {
2099 Err("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES);
2100 return -EINVAL;
2101 }
2102 default_fbufs = fbufs;
2103 Info("Number of frame buffers set to %d.\n", default_fbufs);
2104 }
2105 if (trace >= 0) {
2106 Info("Trace options: 0x%04x\n", trace);
2107 pwc_trace = trace;
2108 }
2109 if (compression >= 0) {
2110 if (compression > 3) {
2111 Err("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
2112 return -EINVAL;
2113 }
2114 pwc_preferred_compression = compression;
2115 Info("Preferred compression set to %d.\n", pwc_preferred_compression);
2116 }
2117 if (power_save)
2118 Info("Enabling power save on open/close.\n");
2119 if (leds[0] >= 0)
2120 led_on = leds[0];
2121 if (leds[1] >= 0)
2122 led_off = leds[1];
2123
2124 /* Big device node whoopla. Basically, it allows you to assign a
2125 device node (/dev/videoX) to a camera, based on its type
2126 & serial number. The format is [type[.serialnumber]:]node.
2127
2128 Any camera that isn't matched by these rules gets the next
2129 available free device node.
2130 */
2131 for (i = 0; i < MAX_DEV_HINTS; i++) {
2132 char *s, *colon, *dot;
2133
2134 /* This loop also initializes the array */
2135 device_hint[i].pdev = NULL;
2136 s = dev_hint[i];
2137 if (s != NULL && *s != '\0') {
2138 device_hint[i].type = -1; /* wildcard */
2139 strcpy(device_hint[i].serial_number, "*");
2140
2141 /* parse string: chop at ':' & '/' */
2142 colon = dot = s;
2143 while (*colon != '\0' && *colon != ':')
2144 colon++;
2145 while (*dot != '\0' && *dot != '.')
2146 dot++;
2147 /* Few sanity checks */
2148 if (*dot != '\0' && dot > colon) {
2149 Err("Malformed camera hint: the colon must be after the dot.\n");
2150 return -EINVAL;
2151 }
2152
2153 if (*colon == '\0') {
2154 /* No colon */
2155 if (*dot != '\0') {
2156 Err("Malformed camera hint: no colon + device node given.\n");
2157 return -EINVAL;
2158 }
2159 else {
2160 /* No type or serial number specified, just a number. */
2161 device_hint[i].device_node = pwc_atoi(s);
2162 }
2163 }
2164 else {
2165 /* There's a colon, so we have at least a type and a device node */
2166 device_hint[i].type = pwc_atoi(s);
2167 device_hint[i].device_node = pwc_atoi(colon + 1);
2168 if (*dot != '\0') {
2169 /* There's a serial number as well */
2170 int k;
2171
2172 dot++;
2173 k = 0;
2174 while (*dot != ':' && k < 29) {
2175 device_hint[i].serial_number[k++] = *dot;
2176 dot++;
2177 }
2178 device_hint[i].serial_number[k] = '\0';
2179 }
2180 }
2181#if PWC_DEBUG
2182 Debug("device_hint[%d]:\n", i);
2183 Debug(" type : %d\n", device_hint[i].type);
2184 Debug(" serial# : %s\n", device_hint[i].serial_number);
2185 Debug(" node : %d\n", device_hint[i].device_node);
2186#endif
2187 }
2188 else
2189 device_hint[i].type = 0; /* not filled */
2190 } /* ..for MAX_DEV_HINTS */
2191
2192 Trace(TRACE_PROBE, "Registering driver at address 0x%p.\n", &pwc_driver);
2193 return usb_register(&pwc_driver);
2194}
2195
2196static void __exit usb_pwc_exit(void)
2197{
2198 Trace(TRACE_MODULE, "Deregistering driver.\n");
2199 usb_deregister(&pwc_driver);
2200 Info("Philips webcam module removed.\n");
2201}
2202
2203module_init(usb_pwc_init);
2204module_exit(usb_pwc_exit);
2205