blob: 5429ff3b97537336f4846a346c783a913fe92624 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#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 .owner = THIS_MODULE,
114 .name = "Philips webcam", /* name */
115 .id_table = pwc_device_table,
116 .probe = usb_pwc_probe, /* probe() */
117 .disconnect = usb_pwc_disconnect, /* disconnect() */
118};
119
120#define MAX_DEV_HINTS 20
121#define MAX_ISOC_ERRORS 20
122
123static int default_size = PSZ_QCIF;
124static int default_fps = 10;
125static int default_fbufs = 3; /* Default number of frame buffers */
126static int default_mbufs = 2; /* Default number of mmap() buffers */
127 int pwc_trace = TRACE_MODULE | TRACE_FLOW | TRACE_PWCX;
128static int power_save = 0;
129static int led_on = 100, led_off = 0; /* defaults to LED that is on while in use */
Adrian Bunk2c47e7f2005-04-22 15:07:00 -0700130static int pwc_preferred_compression = 2; /* 0..3 = uncompressed..high */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static struct {
132 int type;
133 char serial_number[30];
134 int device_node;
135 struct pwc_device *pdev;
136} device_hint[MAX_DEV_HINTS];
137
138/***/
139
140static int pwc_video_open(struct inode *inode, struct file *file);
141static int pwc_video_close(struct inode *inode, struct file *file);
142static ssize_t pwc_video_read(struct file *file, char __user * buf,
143 size_t count, loff_t *ppos);
144static unsigned int pwc_video_poll(struct file *file, poll_table *wait);
145static int pwc_video_ioctl(struct inode *inode, struct file *file,
146 unsigned int ioctlnr, unsigned long arg);
147static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma);
148
149static struct file_operations pwc_fops = {
150 .owner = THIS_MODULE,
151 .open = pwc_video_open,
152 .release = pwc_video_close,
153 .read = pwc_video_read,
154 .poll = pwc_video_poll,
155 .mmap = pwc_video_mmap,
156 .ioctl = pwc_video_ioctl,
157 .llseek = no_llseek,
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
Steven Cole093cf722005-05-03 19:07:24 -0600273 /* Allocate Isochronous pipe buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 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:
Alan Cox88c18342005-05-27 13:40:53 +0100323#if 0
Al Viro5330e922005-04-26 11:26:53 -0700324 Trace(TRACE_MEMORY,"private_data(%zu)\n",sizeof(struct pwc_dec23_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 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;
Alan Cox88c18342005-05-27 13:40:53 +0100332#endif
333 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335 if (kbuf == NULL) {
336 Err("Failed to allocate decompress table.\n");
337 return -ENOMEM;
338 }
339 pdev->decompress_data = kbuf;
340
341 /* Allocate image buffer; double buffer for mmap() */
342 kbuf = rvmalloc(default_mbufs * pdev->len_per_image);
343 if (kbuf == NULL) {
344 Err("Failed to allocate image buffer(s). needed (%d)\n",default_mbufs * pdev->len_per_image);
345 return -ENOMEM;
346 }
347 Trace(TRACE_MEMORY, "Allocated image buffer at %p.\n", kbuf);
348 pdev->image_data = kbuf;
349 for (i = 0; i < default_mbufs; i++)
350 pdev->image_ptr[i] = kbuf + i * pdev->len_per_image;
351 for (; i < MAX_IMAGES; i++)
352 pdev->image_ptr[i] = NULL;
353
354 kbuf = NULL;
355
356 Trace(TRACE_MEMORY, "<< pwc_allocate_buffers()\n");
357 return 0;
358}
359
360static void pwc_free_buffers(struct pwc_device *pdev)
361{
362 int i;
363
364 Trace(TRACE_MEMORY, "Entering free_buffers(%p).\n", pdev);
365
366 if (pdev == NULL)
367 return;
368#ifdef PWC_MAGIC
369 if (pdev->magic != PWC_MAGIC) {
370 Err("free_buffers(): magic failed.\n");
371 return;
372 }
373#endif
374
375 /* Release Iso-pipe buffers */
376 for (i = 0; i < MAX_ISO_BUFS; i++)
377 if (pdev->sbuf[i].data != NULL) {
378 Trace(TRACE_MEMORY, "Freeing ISO buffer at %p.\n", pdev->sbuf[i].data);
379 kfree(pdev->sbuf[i].data);
380 pdev->sbuf[i].data = NULL;
381 }
382
383 /* The same for frame buffers */
384 if (pdev->fbuf != NULL) {
385 for (i = 0; i < default_fbufs; i++) {
386 if (pdev->fbuf[i].data != NULL) {
387 Trace(TRACE_MEMORY, "Freeing frame buffer %d at %p.\n", i, pdev->fbuf[i].data);
388 vfree(pdev->fbuf[i].data);
389 pdev->fbuf[i].data = NULL;
390 }
391 }
392 kfree(pdev->fbuf);
393 pdev->fbuf = NULL;
394 }
395
396 /* Intermediate decompression buffer & tables */
397 if (pdev->decompress_data != NULL) {
398 Trace(TRACE_MEMORY, "Freeing decompression buffer at %p.\n", pdev->decompress_data);
399 kfree(pdev->decompress_data);
400 pdev->decompress_data = NULL;
401 }
402 pdev->decompressor = NULL;
403
404 /* Release image buffers */
405 if (pdev->image_data != NULL) {
406 Trace(TRACE_MEMORY, "Freeing image buffer at %p.\n", pdev->image_data);
407 rvfree(pdev->image_data, default_mbufs * pdev->len_per_image);
408 }
409 pdev->image_data = NULL;
410
411 Trace(TRACE_MEMORY, "Leaving free_buffers().\n");
412}
413
414/* The frame & image buffer mess.
415
416 Yes, this is a mess. Well, it used to be simple, but alas... In this
417 module, 3 buffers schemes are used to get the data from the USB bus to
418 the user program. The first scheme involves the ISO buffers (called thus
419 since they transport ISO data from the USB controller), and not really
420 interesting. Suffices to say the data from this buffer is quickly
421 gathered in an interrupt handler (pwc_isoc_handler) and placed into the
422 frame buffer.
423
424 The frame buffer is the second scheme, and is the central element here.
425 It collects the data from a single frame from the camera (hence, the
426 name). Frames are delimited by the USB camera with a short USB packet,
427 so that's easy to detect. The frame buffers form a list that is filled
428 by the camera+USB controller and drained by the user process through
429 either read() or mmap().
430
431 The image buffer is the third scheme, in which frames are decompressed
432 and converted into planar format. For mmap() there is more than
433 one image buffer available.
434
435 The frame buffers provide the image buffering. In case the user process
436 is a bit slow, this introduces lag and some undesired side-effects.
437 The problem arises when the frame buffer is full. I used to drop the last
438 frame, which makes the data in the queue stale very quickly. But dropping
439 the frame at the head of the queue proved to be a litte bit more difficult.
440 I tried a circular linked scheme, but this introduced more problems than
441 it solved.
442
443 Because filling and draining are completely asynchronous processes, this
444 requires some fiddling with pointers and mutexes.
445
446 Eventually, I came up with a system with 2 lists: an 'empty' frame list
447 and a 'full' frame list:
448 * Initially, all frame buffers but one are on the 'empty' list; the one
449 remaining buffer is our initial fill frame.
450 * If a frame is needed for filling, we try to take it from the 'empty'
451 list, unless that list is empty, in which case we take the buffer at
452 the head of the 'full' list.
453 * When our fill buffer has been filled, it is appended to the 'full'
454 list.
455 * If a frame is needed by read() or mmap(), it is taken from the head of
456 the 'full' list, handled, and then appended to the 'empty' list. If no
457 buffer is present on the 'full' list, we wait.
458 The advantage is that the buffer that is currently being decompressed/
459 converted, is on neither list, and thus not in our way (any other scheme
460 I tried had the problem of old data lingering in the queue).
461
462 Whatever strategy you choose, it always remains a tradeoff: with more
463 frame buffers the chances of a missed frame are reduced. On the other
464 hand, on slower machines it introduces lag because the queue will
465 always be full.
466 */
467
468/**
469 \brief Find next frame buffer to fill. Take from empty or full list, whichever comes first.
470 */
471static inline int pwc_next_fill_frame(struct pwc_device *pdev)
472{
473 int ret;
474 unsigned long flags;
475
476 ret = 0;
477 spin_lock_irqsave(&pdev->ptrlock, flags);
478 if (pdev->fill_frame != NULL) {
479 /* append to 'full' list */
480 if (pdev->full_frames == NULL) {
481 pdev->full_frames = pdev->fill_frame;
482 pdev->full_frames_tail = pdev->full_frames;
483 }
484 else {
485 pdev->full_frames_tail->next = pdev->fill_frame;
486 pdev->full_frames_tail = pdev->fill_frame;
487 }
488 }
489 if (pdev->empty_frames != NULL) {
490 /* We have empty frames available. That's easy */
491 pdev->fill_frame = pdev->empty_frames;
492 pdev->empty_frames = pdev->empty_frames->next;
493 }
494 else {
495 /* Hmm. Take it from the full list */
496#if PWC_DEBUG
497 /* sanity check */
498 if (pdev->full_frames == NULL) {
499 Err("Neither empty or full frames available!\n");
500 spin_unlock_irqrestore(&pdev->ptrlock, flags);
501 return -EINVAL;
502 }
503#endif
504 pdev->fill_frame = pdev->full_frames;
505 pdev->full_frames = pdev->full_frames->next;
506 ret = 1;
507 }
508 pdev->fill_frame->next = NULL;
509#if PWC_DEBUG
510 Trace(TRACE_SEQUENCE, "Assigning sequence number %d.\n", pdev->sequence);
511 pdev->fill_frame->sequence = pdev->sequence++;
512#endif
513 spin_unlock_irqrestore(&pdev->ptrlock, flags);
514 return ret;
515}
516
517
518/**
519 \brief Reset all buffers, pointers and lists, except for the image_used[] buffer.
520
521 If the image_used[] buffer is cleared too, mmap()/VIDIOCSYNC will run into trouble.
522 */
523static void pwc_reset_buffers(struct pwc_device *pdev)
524{
525 int i;
526 unsigned long flags;
527
528 spin_lock_irqsave(&pdev->ptrlock, flags);
529 pdev->full_frames = NULL;
530 pdev->full_frames_tail = NULL;
531 for (i = 0; i < default_fbufs; i++) {
532 pdev->fbuf[i].filled = 0;
533 if (i > 0)
534 pdev->fbuf[i].next = &pdev->fbuf[i - 1];
535 else
536 pdev->fbuf->next = NULL;
537 }
538 pdev->empty_frames = &pdev->fbuf[default_fbufs - 1];
539 pdev->empty_frames_tail = pdev->fbuf;
540 pdev->read_frame = NULL;
541 pdev->fill_frame = pdev->empty_frames;
542 pdev->empty_frames = pdev->empty_frames->next;
543
544 pdev->image_read_pos = 0;
545 pdev->fill_image = 0;
546 spin_unlock_irqrestore(&pdev->ptrlock, flags);
547}
548
549
550/**
551 \brief Do all the handling for getting one frame: get pointer, decompress, advance pointers.
552 */
553static int pwc_handle_frame(struct pwc_device *pdev)
554{
555 int ret = 0;
556 unsigned long flags;
557
558 spin_lock_irqsave(&pdev->ptrlock, flags);
559 /* First grab our read_frame; this is removed from all lists, so
560 we can release the lock after this without problems */
561 if (pdev->read_frame != NULL) {
562 /* This can't theoretically happen */
563 Err("Huh? Read frame still in use?\n");
564 }
565 else {
566 if (pdev->full_frames == NULL) {
567 Err("Woops. No frames ready.\n");
568 }
569 else {
570 pdev->read_frame = pdev->full_frames;
571 pdev->full_frames = pdev->full_frames->next;
572 pdev->read_frame->next = NULL;
573 }
574
575 if (pdev->read_frame != NULL) {
576#if PWC_DEBUG
577 Trace(TRACE_SEQUENCE, "Decompressing frame %d\n", pdev->read_frame->sequence);
578#endif
579 /* Decompression is a lenghty process, so it's outside of the lock.
580 This gives the isoc_handler the opportunity to fill more frames
581 in the mean time.
582 */
583 spin_unlock_irqrestore(&pdev->ptrlock, flags);
584 ret = pwc_decompress(pdev);
585 spin_lock_irqsave(&pdev->ptrlock, flags);
586
587 /* We're done with read_buffer, tack it to the end of the empty buffer list */
588 if (pdev->empty_frames == NULL) {
589 pdev->empty_frames = pdev->read_frame;
590 pdev->empty_frames_tail = pdev->empty_frames;
591 }
592 else {
593 pdev->empty_frames_tail->next = pdev->read_frame;
594 pdev->empty_frames_tail = pdev->read_frame;
595 }
596 pdev->read_frame = NULL;
597 }
598 }
599 spin_unlock_irqrestore(&pdev->ptrlock, flags);
600 return ret;
601}
602
603/**
604 \brief Advance pointers of image buffer (after each user request)
605*/
606static inline void pwc_next_image(struct pwc_device *pdev)
607{
608 pdev->image_used[pdev->fill_image] = 0;
609 pdev->fill_image = (pdev->fill_image + 1) % default_mbufs;
610}
611
612
613/* This gets called for the Isochronous pipe (video). This is done in
614 * interrupt time, so it has to be fast, not crash, and not stall. Neat.
615 */
616static void pwc_isoc_handler(struct urb *urb, struct pt_regs *regs)
617{
618 struct pwc_device *pdev;
619 int i, fst, flen;
620 int awake;
621 struct pwc_frame_buf *fbuf;
622 unsigned char *fillptr = NULL, *iso_buf = NULL;
623
624 awake = 0;
625 pdev = (struct pwc_device *)urb->context;
626 if (pdev == NULL) {
627 Err("isoc_handler() called with NULL device?!\n");
628 return;
629 }
630#ifdef PWC_MAGIC
631 if (pdev->magic != PWC_MAGIC) {
632 Err("isoc_handler() called with bad magic!\n");
633 return;
634 }
635#endif
636 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
637 Trace(TRACE_OPEN, "pwc_isoc_handler(): URB (%p) unlinked %ssynchronuously.\n", urb, urb->status == -ENOENT ? "" : "a");
638 return;
639 }
640 if (urb->status != -EINPROGRESS && urb->status != 0) {
641 const char *errmsg;
642
643 errmsg = "Unknown";
644 switch(urb->status) {
645 case -ENOSR: errmsg = "Buffer error (overrun)"; break;
646 case -EPIPE: errmsg = "Stalled (device not responding)"; break;
647 case -EOVERFLOW: errmsg = "Babble (bad cable?)"; break;
648 case -EPROTO: errmsg = "Bit-stuff error (bad cable?)"; break;
649 case -EILSEQ: errmsg = "CRC/Timeout (could be anything)"; break;
650 case -ETIMEDOUT: errmsg = "NAK (device does not respond)"; break;
651 }
652 Trace(TRACE_FLOW, "pwc_isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
653 /* Give up after a number of contiguous errors on the USB bus.
654 Appearantly something is wrong so we simulate an unplug event.
655 */
656 if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
657 {
658 Info("Too many ISOC errors, bailing out.\n");
659 pdev->error_status = EIO;
660 awake = 1;
661 wake_up_interruptible(&pdev->frameq);
662 }
663 goto handler_end; // ugly, but practical
664 }
665
666 fbuf = pdev->fill_frame;
667 if (fbuf == NULL) {
668 Err("pwc_isoc_handler without valid fill frame.\n");
669 awake = 1;
670 goto handler_end;
671 }
672 else {
673 fillptr = fbuf->data + fbuf->filled;
674 }
675
676 /* Reset ISOC error counter. We did get here, after all. */
677 pdev->visoc_errors = 0;
678
679 /* vsync: 0 = don't copy data
680 1 = sync-hunt
681 2 = synched
682 */
683 /* Compact data */
684 for (i = 0; i < urb->number_of_packets; i++) {
685 fst = urb->iso_frame_desc[i].status;
686 flen = urb->iso_frame_desc[i].actual_length;
687 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
688 if (fst == 0) {
689 if (flen > 0) { /* if valid data... */
690 if (pdev->vsync > 0) { /* ...and we are not sync-hunting... */
691 pdev->vsync = 2;
692
693 /* ...copy data to frame buffer, if possible */
694 if (flen + fbuf->filled > pdev->frame_total_size) {
695 Trace(TRACE_FLOW, "Frame buffer overflow (flen = %d, frame_total_size = %d).\n", flen, pdev->frame_total_size);
696 pdev->vsync = 0; /* Hmm, let's wait for an EOF (end-of-frame) */
697 pdev->vframes_error++;
698 }
699 else {
700 memmove(fillptr, iso_buf, flen);
701 fillptr += flen;
702 }
703 }
704 fbuf->filled += flen;
705 } /* ..flen > 0 */
706
707 if (flen < pdev->vlast_packet_size) {
708 /* Shorter packet... We probably have the end of an image-frame;
709 wake up read() process and let select()/poll() do something.
710 Decompression is done in user time over there.
711 */
712 if (pdev->vsync == 2) {
713 /* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
714 frames on the USB wire after an exposure change. This conditition is
715 however detected in the cam and a bit is set in the header.
716 */
717 if (pdev->type == 730) {
718 unsigned char *ptr = (unsigned char *)fbuf->data;
719
720 if (ptr[1] == 1 && ptr[0] & 0x10) {
721#if PWC_DEBUG
722 Debug("Hyundai CMOS sensor bug. Dropping frame %d.\n", fbuf->sequence);
723#endif
724 pdev->drop_frames += 2;
725 pdev->vframes_error++;
726 }
727 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
728 if (ptr[0] & 0x01)
729 Info("Snapshot button pressed.\n");
730 else
731 Info("Snapshot button released.\n");
732 }
733 if ((ptr[0] ^ pdev->vmirror) & 0x02) {
734 if (ptr[0] & 0x02)
735 Info("Image is mirrored.\n");
736 else
737 Info("Image is normal.\n");
738 }
739 pdev->vmirror = ptr[0] & 0x03;
740 /* Sometimes the trailer of the 730 is still sent as a 4 byte packet
741 after a short frame; this condition is filtered out specifically. A 4 byte
742 frame doesn't make sense anyway.
743 So we get either this sequence:
744 drop_bit set -> 4 byte frame -> short frame -> good frame
745 Or this one:
746 drop_bit set -> short frame -> good frame
747 So we drop either 3 or 2 frames in all!
748 */
749 if (fbuf->filled == 4)
750 pdev->drop_frames++;
751 }
752
753 /* In case we were instructed to drop the frame, do so silently.
754 The buffer pointers are not updated either (but the counters are reset below).
755 */
756 if (pdev->drop_frames > 0)
757 pdev->drop_frames--;
758 else {
759 /* Check for underflow first */
760 if (fbuf->filled < pdev->frame_total_size) {
761 Trace(TRACE_FLOW, "Frame buffer underflow (%d bytes); discarded.\n", fbuf->filled);
762 pdev->vframes_error++;
763 }
764 else {
765 /* Send only once per EOF */
766 awake = 1; /* delay wake_ups */
767
768 /* Find our next frame to fill. This will always succeed, since we
769 * nick a frame from either empty or full list, but if we had to
770 * take it from the full list, it means a frame got dropped.
771 */
772 if (pwc_next_fill_frame(pdev)) {
773 pdev->vframes_dumped++;
774 if ((pdev->vframe_count > FRAME_LOWMARK) && (pwc_trace & TRACE_FLOW)) {
775 if (pdev->vframes_dumped < 20)
776 Trace(TRACE_FLOW, "Dumping frame %d.\n", pdev->vframe_count);
777 if (pdev->vframes_dumped == 20)
778 Trace(TRACE_FLOW, "Dumping frame %d (last message).\n", pdev->vframe_count);
779 }
780 }
781 fbuf = pdev->fill_frame;
782 }
783 } /* !drop_frames */
784 pdev->vframe_count++;
785 }
786 fbuf->filled = 0;
787 fillptr = fbuf->data;
788 pdev->vsync = 1;
789 } /* .. flen < last_packet_size */
790 pdev->vlast_packet_size = flen;
791 } /* ..status == 0 */
792#if PWC_DEBUG
793 /* This is normally not interesting to the user, unless you are really debugging something */
794 else {
795 static int iso_error = 0;
796 iso_error++;
797 if (iso_error < 20)
798 Trace(TRACE_FLOW, "Iso frame %d of USB has error %d\n", i, fst);
799 }
800#endif
801 }
802
803handler_end:
804 if (awake)
805 wake_up_interruptible(&pdev->frameq);
806
807 urb->dev = pdev->udev;
808 i = usb_submit_urb(urb, GFP_ATOMIC);
809 if (i != 0)
810 Err("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
811}
812
813
814static int pwc_isoc_init(struct pwc_device *pdev)
815{
816 struct usb_device *udev;
817 struct urb *urb;
818 int i, j, ret;
819
820 struct usb_interface *intf;
821 struct usb_host_interface *idesc = NULL;
822
823 if (pdev == NULL)
824 return -EFAULT;
825 if (pdev->iso_init)
826 return 0;
827 pdev->vsync = 0;
828 udev = pdev->udev;
829
830 /* Get the current alternate interface, adjust packet size */
831 if (!udev->actconfig)
832 return -EFAULT;
833#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5)
834 idesc = &udev->actconfig->interface[0]->altsetting[pdev->valternate];
835#else
836 intf = usb_ifnum_to_if(udev, 0);
837 if (intf)
838 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
839#endif
840
841 if (!idesc)
842 return -EFAULT;
843
844 /* Search video endpoint */
845 pdev->vmax_packet_size = -1;
846 for (i = 0; i < idesc->desc.bNumEndpoints; i++)
847 if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
848 pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
849 break;
850 }
851
852 if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
853 Err("Failed to find packet size for video endpoint in current alternate setting.\n");
Steven Cole093cf722005-05-03 19:07:24 -0600854 return -ENFILE; /* Odd error, that should be noticeable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 /* Set alternate interface */
858 ret = 0;
859 Trace(TRACE_OPEN, "Setting alternate interface %d\n", pdev->valternate);
860 ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
861 if (ret < 0)
862 return ret;
863
864 for (i = 0; i < MAX_ISO_BUFS; i++) {
865 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
866 if (urb == NULL) {
867 Err("Failed to allocate urb %d\n", i);
868 ret = -ENOMEM;
869 break;
870 }
871 pdev->sbuf[i].urb = urb;
872 Trace(TRACE_MEMORY, "Allocated URB at 0x%p\n", urb);
873 }
874 if (ret) {
875 /* De-allocate in reverse order */
876 while (i >= 0) {
877 if (pdev->sbuf[i].urb != NULL)
878 usb_free_urb(pdev->sbuf[i].urb);
879 pdev->sbuf[i].urb = NULL;
880 i--;
881 }
882 return ret;
883 }
884
885 /* init URB structure */
886 for (i = 0; i < MAX_ISO_BUFS; i++) {
887 urb = pdev->sbuf[i].urb;
888
889 urb->interval = 1; // devik
890 urb->dev = udev;
891 urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
892 urb->transfer_flags = URB_ISO_ASAP;
893 urb->transfer_buffer = pdev->sbuf[i].data;
894 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
895 urb->complete = pwc_isoc_handler;
896 urb->context = pdev;
897 urb->start_frame = 0;
898 urb->number_of_packets = ISO_FRAMES_PER_DESC;
899 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
900 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
901 urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
902 }
903 }
904
905 /* link */
906 for (i = 0; i < MAX_ISO_BUFS; i++) {
907 ret = usb_submit_urb(pdev->sbuf[i].urb, GFP_KERNEL);
908 if (ret)
909 Err("isoc_init() submit_urb %d failed with error %d\n", i, ret);
910 else
911 Trace(TRACE_MEMORY, "URB 0x%p submitted.\n", pdev->sbuf[i].urb);
912 }
913
914 /* All is done... */
915 pdev->iso_init = 1;
916 Trace(TRACE_OPEN, "<< pwc_isoc_init()\n");
917 return 0;
918}
919
920static void pwc_isoc_cleanup(struct pwc_device *pdev)
921{
922 int i;
923
924 Trace(TRACE_OPEN, ">> pwc_isoc_cleanup()\n");
925 if (pdev == NULL)
926 return;
927
928 /* Unlinking ISOC buffers one by one */
929 for (i = 0; i < MAX_ISO_BUFS; i++) {
930 struct urb *urb;
931
932 urb = pdev->sbuf[i].urb;
933 if (urb != 0) {
934 if (pdev->iso_init) {
935 Trace(TRACE_MEMORY, "Unlinking URB %p\n", urb);
936 usb_kill_urb(urb);
937 }
938 Trace(TRACE_MEMORY, "Freeing URB\n");
939 usb_free_urb(urb);
940 pdev->sbuf[i].urb = NULL;
941 }
942 }
943
944 /* Stop camera, but only if we are sure the camera is still there (unplug
945 is signalled by EPIPE)
946 */
947 if (pdev->error_status && pdev->error_status != EPIPE) {
948 Trace(TRACE_OPEN, "Setting alternate interface 0.\n");
949 usb_set_interface(pdev->udev, 0, 0);
950 }
951
952 pdev->iso_init = 0;
953 Trace(TRACE_OPEN, "<< pwc_isoc_cleanup()\n");
954}
955
956int pwc_try_video_mode(struct pwc_device *pdev, int width, int height, int new_fps, int new_compression, int new_snapshot)
957{
958 int ret, start;
959
960 /* Stop isoc stuff */
961 pwc_isoc_cleanup(pdev);
962 /* Reset parameters */
963 pwc_reset_buffers(pdev);
964 /* Try to set video mode... */
965 start = ret = pwc_set_video_mode(pdev, width, height, new_fps, new_compression, new_snapshot);
966 if (ret) {
967 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 1 failed.\n");
968 /* That failed... restore old mode (we know that worked) */
969 start = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
970 if (start) {
971 Trace(TRACE_FLOW, "pwc_set_video_mode attempt 2 failed.\n");
972 }
973 }
974 if (start == 0)
975 {
976 if (pwc_isoc_init(pdev) < 0)
977 {
978 Info("Failed to restart ISOC transfers in pwc_try_video_mode.\n");
979 ret = -EAGAIN; /* let's try again, who knows if it works a second time */
980 }
981 }
982 pdev->drop_frames++; /* try to avoid garbage during switch */
983 return ret; /* Return original error code */
984}
985
986
987/***************************************************************************/
988/* Video4Linux functions */
989
990static int pwc_video_open(struct inode *inode, struct file *file)
991{
992 int i;
993 struct video_device *vdev = video_devdata(file);
994 struct pwc_device *pdev;
995
996 Trace(TRACE_OPEN, ">> video_open called(vdev = 0x%p).\n", vdev);
997
998 pdev = (struct pwc_device *)vdev->priv;
999 if (pdev == NULL)
1000 BUG();
1001 if (pdev->vopen)
1002 return -EBUSY;
1003
1004 down(&pdev->modlock);
1005 if (!pdev->usb_init) {
1006 Trace(TRACE_OPEN, "Doing first time initialization.\n");
1007 pdev->usb_init = 1;
1008
1009 if (pwc_trace & TRACE_OPEN)
1010 {
1011 /* Query sensor type */
1012 const char *sensor_type = NULL;
1013 int ret;
1014
1015 ret = pwc_get_cmos_sensor(pdev, &i);
1016 if (ret >= 0)
1017 {
1018 switch(i) {
1019 case 0x00: sensor_type = "Hyundai CMOS sensor"; break;
1020 case 0x20: sensor_type = "Sony CCD sensor + TDA8787"; break;
1021 case 0x2E: sensor_type = "Sony CCD sensor + Exas 98L59"; break;
1022 case 0x2F: sensor_type = "Sony CCD sensor + ADI 9804"; break;
1023 case 0x30: sensor_type = "Sharp CCD sensor + TDA8787"; break;
1024 case 0x3E: sensor_type = "Sharp CCD sensor + Exas 98L59"; break;
1025 case 0x3F: sensor_type = "Sharp CCD sensor + ADI 9804"; break;
1026 case 0x40: sensor_type = "UPA 1021 sensor"; break;
1027 case 0x100: sensor_type = "VGA sensor"; break;
1028 case 0x101: sensor_type = "PAL MR sensor"; break;
1029 default: sensor_type = "unknown type of sensor"; break;
1030 }
1031 }
1032 if (sensor_type != NULL)
1033 Info("This %s camera is equipped with a %s (%d).\n", pdev->vdev->name, sensor_type, i);
1034 }
1035 }
1036
1037 /* Turn on camera */
1038 if (power_save) {
1039 i = pwc_camera_power(pdev, 1);
1040 if (i < 0)
1041 Info("Failed to restore power to the camera! (%d)\n", i);
1042 }
1043 /* Set LED on/off time */
1044 if (pwc_set_leds(pdev, led_on, led_off) < 0)
1045 Info("Failed to set LED on/off time.\n");
1046
1047 pwc_construct(pdev); /* set min/max sizes correct */
1048
1049 /* So far, so good. Allocate memory. */
1050 i = pwc_allocate_buffers(pdev);
1051 if (i < 0) {
1052 Trace(TRACE_OPEN, "Failed to allocate buffer memory.\n");
1053 up(&pdev->modlock);
1054 return i;
1055 }
1056
1057 /* Reset buffers & parameters */
1058 pwc_reset_buffers(pdev);
1059 for (i = 0; i < default_mbufs; i++)
1060 pdev->image_used[i] = 0;
1061 pdev->vframe_count = 0;
1062 pdev->vframes_dumped = 0;
1063 pdev->vframes_error = 0;
1064 pdev->visoc_errors = 0;
1065 pdev->error_status = 0;
1066#if PWC_DEBUG
1067 pdev->sequence = 0;
1068#endif
1069 pwc_construct(pdev); /* set min/max sizes correct */
1070
1071 /* Set some defaults */
1072 pdev->vsnapshot = 0;
1073
1074 /* Start iso pipe for video; first try the last used video size
1075 (or the default one); if that fails try QCIF/10 or QSIF/10;
1076 it that fails too, give up.
1077 */
1078 i = pwc_set_video_mode(pdev, pwc_image_sizes[pdev->vsize].x, pwc_image_sizes[pdev->vsize].y, pdev->vframes, pdev->vcompression, 0);
1079 if (i) {
1080 Trace(TRACE_OPEN, "First attempt at set_video_mode failed.\n");
1081 if (pdev->type == 730 || pdev->type == 740 || pdev->type == 750)
1082 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QSIF].x, pwc_image_sizes[PSZ_QSIF].y, 10, pdev->vcompression, 0);
1083 else
1084 i = pwc_set_video_mode(pdev, pwc_image_sizes[PSZ_QCIF].x, pwc_image_sizes[PSZ_QCIF].y, 10, pdev->vcompression, 0);
1085 }
1086 if (i) {
1087 Trace(TRACE_OPEN, "Second attempt at set_video_mode failed.\n");
1088 up(&pdev->modlock);
1089 return i;
1090 }
1091
1092 i = pwc_isoc_init(pdev);
1093 if (i) {
1094 Trace(TRACE_OPEN, "Failed to init ISOC stuff = %d.\n", i);
1095 up(&pdev->modlock);
1096 return i;
1097 }
1098
1099 pdev->vopen++;
1100 file->private_data = vdev;
1101 up(&pdev->modlock);
1102 Trace(TRACE_OPEN, "<< video_open() returns 0.\n");
1103 return 0;
1104}
1105
1106/* Note that all cleanup is done in the reverse order as in _open */
1107static int pwc_video_close(struct inode *inode, struct file *file)
1108{
1109 struct video_device *vdev = file->private_data;
1110 struct pwc_device *pdev;
1111 int i;
1112
1113 Trace(TRACE_OPEN, ">> video_close called(vdev = 0x%p).\n", vdev);
1114
1115 pdev = (struct pwc_device *)vdev->priv;
1116 if (pdev->vopen == 0)
1117 Info("video_close() called on closed device?\n");
1118
1119 /* Dump statistics, but only if a reasonable amount of frames were
1120 processed (to prevent endless log-entries in case of snap-shot
1121 programs)
1122 */
1123 if (pdev->vframe_count > 20)
1124 Info("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev->vframe_count, pdev->vframes_dumped, pdev->vframes_error);
1125
1126 switch (pdev->type)
1127 {
1128 case 675:
1129 case 680:
1130 case 690:
1131 case 720:
1132 case 730:
1133 case 740:
1134 case 750:
Alan Cox88c18342005-05-27 13:40:53 +01001135/* pwc_dec23_exit(); *//* Timon & Kiara */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 break;
1137 case 645:
1138 case 646:
Alan Cox88c18342005-05-27 13:40:53 +01001139/* pwc_dec1_exit(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 break;
1141 }
1142
1143 pwc_isoc_cleanup(pdev);
1144 pwc_free_buffers(pdev);
1145
1146 /* Turn off LEDS and power down camera, but only when not unplugged */
1147 if (pdev->error_status != EPIPE) {
1148 /* Turn LEDs off */
1149 if (pwc_set_leds(pdev, 0, 0) < 0)
1150 Info("Failed to set LED on/off time.\n");
1151 if (power_save) {
1152 i = pwc_camera_power(pdev, 0);
1153 if (i < 0)
1154 Err("Failed to power down camera (%d)\n", i);
1155 }
1156 }
1157 pdev->vopen = 0;
1158 Trace(TRACE_OPEN, "<< video_close()\n");
1159 return 0;
1160}
1161
1162/*
1163 * FIXME: what about two parallel reads ????
1164 * ANSWER: Not supported. You can't open the device more than once,
1165 despite what the V4L1 interface says. First, I don't see
1166 the need, second there's no mechanism of alerting the
1167 2nd/3rd/... process of events like changing image size.
1168 And I don't see the point of blocking that for the
1169 2nd/3rd/... process.
1170 In multi-threaded environments reading parallel from any
1171 device is tricky anyhow.
1172 */
1173
1174static ssize_t pwc_video_read(struct file *file, char __user * buf,
1175 size_t count, loff_t *ppos)
1176{
1177 struct video_device *vdev = file->private_data;
1178 struct pwc_device *pdev;
1179 int noblock = file->f_flags & O_NONBLOCK;
1180 DECLARE_WAITQUEUE(wait, current);
1181 int bytes_to_read;
1182
Al Viro5330e922005-04-26 11:26:53 -07001183 Trace(TRACE_READ, "video_read(0x%p, %p, %zu) called.\n", vdev, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (vdev == NULL)
1185 return -EFAULT;
1186 pdev = vdev->priv;
1187 if (pdev == NULL)
1188 return -EFAULT;
1189 if (pdev->error_status)
1190 return -pdev->error_status; /* Something happened, report what. */
1191
1192 /* In case we're doing partial reads, we don't have to wait for a frame */
1193 if (pdev->image_read_pos == 0) {
1194 /* Do wait queueing according to the (doc)book */
1195 add_wait_queue(&pdev->frameq, &wait);
1196 while (pdev->full_frames == NULL) {
1197 /* Check for unplugged/etc. here */
1198 if (pdev->error_status) {
1199 remove_wait_queue(&pdev->frameq, &wait);
1200 set_current_state(TASK_RUNNING);
1201 return -pdev->error_status ;
1202 }
1203 if (noblock) {
1204 remove_wait_queue(&pdev->frameq, &wait);
1205 set_current_state(TASK_RUNNING);
1206 return -EWOULDBLOCK;
1207 }
1208 if (signal_pending(current)) {
1209 remove_wait_queue(&pdev->frameq, &wait);
1210 set_current_state(TASK_RUNNING);
1211 return -ERESTARTSYS;
1212 }
1213 schedule();
1214 set_current_state(TASK_INTERRUPTIBLE);
1215 }
1216 remove_wait_queue(&pdev->frameq, &wait);
1217 set_current_state(TASK_RUNNING);
1218
1219 /* Decompress and release frame */
1220 if (pwc_handle_frame(pdev))
1221 return -EFAULT;
1222 }
1223
1224 Trace(TRACE_READ, "Copying data to user space.\n");
1225 if (pdev->vpalette == VIDEO_PALETTE_RAW)
1226 bytes_to_read = pdev->frame_size;
1227 else
1228 bytes_to_read = pdev->view.size;
1229
1230 /* copy bytes to user space; we allow for partial reads */
1231 if (count + pdev->image_read_pos > bytes_to_read)
1232 count = bytes_to_read - pdev->image_read_pos;
1233 if (copy_to_user(buf, pdev->image_ptr[pdev->fill_image] + pdev->image_read_pos, count))
1234 return -EFAULT;
1235 pdev->image_read_pos += count;
1236 if (pdev->image_read_pos >= bytes_to_read) { /* All data has been read */
1237 pdev->image_read_pos = 0;
1238 pwc_next_image(pdev);
1239 }
1240 return count;
1241}
1242
1243static unsigned int pwc_video_poll(struct file *file, poll_table *wait)
1244{
1245 struct video_device *vdev = file->private_data;
1246 struct pwc_device *pdev;
1247
1248 if (vdev == NULL)
1249 return -EFAULT;
1250 pdev = vdev->priv;
1251 if (pdev == NULL)
1252 return -EFAULT;
1253
1254 poll_wait(file, &pdev->frameq, wait);
1255 if (pdev->error_status)
1256 return POLLERR;
1257 if (pdev->full_frames != NULL) /* we have frames waiting */
1258 return (POLLIN | POLLRDNORM);
1259
1260 return 0;
1261}
1262
1263static int pwc_video_do_ioctl(struct inode *inode, struct file *file,
1264 unsigned int cmd, void *arg)
1265{
1266 struct video_device *vdev = file->private_data;
1267 struct pwc_device *pdev;
1268 DECLARE_WAITQUEUE(wait, current);
1269
1270 if (vdev == NULL)
1271 return -EFAULT;
1272 pdev = vdev->priv;
1273 if (pdev == NULL)
1274 return -EFAULT;
1275
1276 switch (cmd) {
1277 /* Query cabapilities */
1278 case VIDIOCGCAP:
1279 {
1280 struct video_capability *caps = arg;
1281
1282 strcpy(caps->name, vdev->name);
1283 caps->type = VID_TYPE_CAPTURE;
1284 caps->channels = 1;
1285 caps->audios = 1;
1286 caps->minwidth = pdev->view_min.x;
1287 caps->minheight = pdev->view_min.y;
1288 caps->maxwidth = pdev->view_max.x;
1289 caps->maxheight = pdev->view_max.y;
1290 break;
1291 }
1292
1293 /* Channel functions (simulate 1 channel) */
1294 case VIDIOCGCHAN:
1295 {
1296 struct video_channel *v = arg;
1297
1298 if (v->channel != 0)
1299 return -EINVAL;
1300 v->flags = 0;
1301 v->tuners = 0;
1302 v->type = VIDEO_TYPE_CAMERA;
1303 strcpy(v->name, "Webcam");
1304 return 0;
1305 }
1306
1307 case VIDIOCSCHAN:
1308 {
1309 /* The spec says the argument is an integer, but
1310 the bttv driver uses a video_channel arg, which
1311 makes sense becasue it also has the norm flag.
1312 */
1313 struct video_channel *v = arg;
1314 if (v->channel != 0)
1315 return -EINVAL;
1316 return 0;
1317 }
1318
1319
1320 /* Picture functions; contrast etc. */
1321 case VIDIOCGPICT:
1322 {
1323 struct video_picture *p = arg;
1324 int val;
1325
1326 val = pwc_get_brightness(pdev);
1327 if (val >= 0)
1328 p->brightness = val;
1329 else
1330 p->brightness = 0xffff;
1331 val = pwc_get_contrast(pdev);
1332 if (val >= 0)
1333 p->contrast = val;
1334 else
1335 p->contrast = 0xffff;
1336 /* Gamma, Whiteness, what's the difference? :) */
1337 val = pwc_get_gamma(pdev);
1338 if (val >= 0)
1339 p->whiteness = val;
1340 else
1341 p->whiteness = 0xffff;
1342 val = pwc_get_saturation(pdev);
1343 if (val >= 0)
1344 p->colour = val;
1345 else
1346 p->colour = 0xffff;
1347 p->depth = 24;
1348 p->palette = pdev->vpalette;
1349 p->hue = 0xFFFF; /* N/A */
1350 break;
1351 }
1352
1353 case VIDIOCSPICT:
1354 {
1355 struct video_picture *p = arg;
1356 /*
1357 * FIXME: Suppose we are mid read
1358 ANSWER: No problem: the firmware of the camera
1359 can handle brightness/contrast/etc
1360 changes at _any_ time, and the palette
1361 is used exactly once in the uncompress
1362 routine.
1363 */
1364 pwc_set_brightness(pdev, p->brightness);
1365 pwc_set_contrast(pdev, p->contrast);
1366 pwc_set_gamma(pdev, p->whiteness);
1367 pwc_set_saturation(pdev, p->colour);
1368 if (p->palette && p->palette != pdev->vpalette) {
1369 switch (p->palette) {
1370 case VIDEO_PALETTE_YUV420P:
1371 case VIDEO_PALETTE_RAW:
1372 pdev->vpalette = p->palette;
1373 return pwc_try_video_mode(pdev, pdev->image.x, pdev->image.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1374 break;
1375 default:
1376 return -EINVAL;
1377 break;
1378 }
1379 }
1380 break;
1381 }
1382
1383 /* Window/size parameters */
1384 case VIDIOCGWIN:
1385 {
1386 struct video_window *vw = arg;
1387
1388 vw->x = 0;
1389 vw->y = 0;
1390 vw->width = pdev->view.x;
1391 vw->height = pdev->view.y;
1392 vw->chromakey = 0;
1393 vw->flags = (pdev->vframes << PWC_FPS_SHIFT) |
1394 (pdev->vsnapshot ? PWC_FPS_SNAPSHOT : 0);
1395 break;
1396 }
1397
1398 case VIDIOCSWIN:
1399 {
1400 struct video_window *vw = arg;
1401 int fps, snapshot, ret;
1402
1403 fps = (vw->flags & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
1404 snapshot = vw->flags & PWC_FPS_SNAPSHOT;
1405 if (fps == 0)
1406 fps = pdev->vframes;
1407 if (pdev->view.x == vw->width && pdev->view.y && fps == pdev->vframes && snapshot == pdev->vsnapshot)
1408 return 0;
1409 ret = pwc_try_video_mode(pdev, vw->width, vw->height, fps, pdev->vcompression, snapshot);
1410 if (ret)
1411 return ret;
1412 break;
1413 }
1414
1415 /* We don't have overlay support (yet) */
1416 case VIDIOCGFBUF:
1417 {
1418 struct video_buffer *vb = arg;
1419
1420 memset(vb,0,sizeof(*vb));
1421 break;
1422 }
1423
1424 /* mmap() functions */
1425 case VIDIOCGMBUF:
1426 {
1427 /* Tell the user program how much memory is needed for a mmap() */
1428 struct video_mbuf *vm = arg;
1429 int i;
1430
1431 memset(vm, 0, sizeof(*vm));
1432 vm->size = default_mbufs * pdev->len_per_image;
1433 vm->frames = default_mbufs; /* double buffering should be enough for most applications */
1434 for (i = 0; i < default_mbufs; i++)
1435 vm->offsets[i] = i * pdev->len_per_image;
1436 break;
1437 }
1438
1439 case VIDIOCMCAPTURE:
1440 {
1441 /* Start capture into a given image buffer (called 'frame' in video_mmap structure) */
1442 struct video_mmap *vm = arg;
1443
1444 Trace(TRACE_READ, "VIDIOCMCAPTURE: %dx%d, frame %d, format %d\n", vm->width, vm->height, vm->frame, vm->format);
1445 if (vm->frame < 0 || vm->frame >= default_mbufs)
1446 return -EINVAL;
1447
1448 /* xawtv is nasty. It probes the available palettes
1449 by setting a very small image size and trying
1450 various palettes... The driver doesn't support
1451 such small images, so I'm working around it.
1452 */
1453 if (vm->format)
1454 {
1455 switch (vm->format)
1456 {
1457 case VIDEO_PALETTE_YUV420P:
1458 case VIDEO_PALETTE_RAW:
1459 break;
1460 default:
1461 return -EINVAL;
1462 break;
1463 }
1464 }
1465
1466 if ((vm->width != pdev->view.x || vm->height != pdev->view.y) &&
1467 (vm->width >= pdev->view_min.x && vm->height >= pdev->view_min.y)) {
1468 int ret;
1469
1470 Trace(TRACE_OPEN, "VIDIOCMCAPTURE: changing size to please xawtv :-(.\n");
1471 ret = pwc_try_video_mode(pdev, vm->width, vm->height, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
1472 if (ret)
1473 return ret;
1474 } /* ... size mismatch */
1475
1476 /* FIXME: should we lock here? */
1477 if (pdev->image_used[vm->frame])
1478 return -EBUSY; /* buffer wasn't available. Bummer */
1479 pdev->image_used[vm->frame] = 1;
1480
1481 /* Okay, we're done here. In the SYNC call we wait until a
1482 frame comes available, then expand image into the given
1483 buffer.
1484 In contrast to the CPiA cam the Philips cams deliver a
1485 constant stream, almost like a grabber card. Also,
1486 we have separate buffers for the rawdata and the image,
1487 meaning we can nearly always expand into the requested buffer.
1488 */
1489 Trace(TRACE_READ, "VIDIOCMCAPTURE done.\n");
1490 break;
1491 }
1492
1493 case VIDIOCSYNC:
1494 {
1495 /* The doc says: "Whenever a buffer is used it should
1496 call VIDIOCSYNC to free this frame up and continue."
1497
1498 The only odd thing about this whole procedure is
1499 that MCAPTURE flags the buffer as "in use", and
1500 SYNC immediately unmarks it, while it isn't
1501 after SYNC that you know that the buffer actually
1502 got filled! So you better not start a CAPTURE in
1503 the same frame immediately (use double buffering).
1504 This is not a problem for this cam, since it has
1505 extra intermediate buffers, but a hardware
1506 grabber card will then overwrite the buffer
1507 you're working on.
1508 */
1509 int *mbuf = arg;
1510 int ret;
1511
1512 Trace(TRACE_READ, "VIDIOCSYNC called (%d).\n", *mbuf);
1513
1514 /* bounds check */
1515 if (*mbuf < 0 || *mbuf >= default_mbufs)
1516 return -EINVAL;
1517 /* check if this buffer was requested anyway */
1518 if (pdev->image_used[*mbuf] == 0)
1519 return -EINVAL;
1520
1521 /* Add ourselves to the frame wait-queue.
1522
1523 FIXME: needs auditing for safety.
1524 QUESTION: In what respect? I think that using the
1525 frameq is safe now.
1526 */
1527 add_wait_queue(&pdev->frameq, &wait);
1528 while (pdev->full_frames == NULL) {
1529 if (pdev->error_status) {
1530 remove_wait_queue(&pdev->frameq, &wait);
1531 set_current_state(TASK_RUNNING);
1532 return -pdev->error_status;
1533 }
1534
1535 if (signal_pending(current)) {
1536 remove_wait_queue(&pdev->frameq, &wait);
1537 set_current_state(TASK_RUNNING);
1538 return -ERESTARTSYS;
1539 }
1540 schedule();
1541 set_current_state(TASK_INTERRUPTIBLE);
1542 }
1543 remove_wait_queue(&pdev->frameq, &wait);
1544 set_current_state(TASK_RUNNING);
1545
1546 /* The frame is ready. Expand in the image buffer
1547 requested by the user. I don't care if you
1548 mmap() 5 buffers and request data in this order:
1549 buffer 4 2 3 0 1 2 3 0 4 3 1 . . .
1550 Grabber hardware may not be so forgiving.
1551 */
1552 Trace(TRACE_READ, "VIDIOCSYNC: frame ready.\n");
1553 pdev->fill_image = *mbuf; /* tell in which buffer we want the image to be expanded */
1554 /* Decompress, etc */
1555 ret = pwc_handle_frame(pdev);
1556 pdev->image_used[*mbuf] = 0;
1557 if (ret)
1558 return -EFAULT;
1559 break;
1560 }
1561
1562 case VIDIOCGAUDIO:
1563 {
1564 struct video_audio *v = arg;
1565
1566 strcpy(v->name, "Microphone");
1567 v->audio = -1; /* unknown audio minor */
1568 v->flags = 0;
1569 v->mode = VIDEO_SOUND_MONO;
1570 v->volume = 0;
1571 v->bass = 0;
1572 v->treble = 0;
1573 v->balance = 0x8000;
1574 v->step = 1;
1575 break;
1576 }
1577
1578 case VIDIOCSAUDIO:
1579 {
1580 /* Dummy: nothing can be set */
1581 break;
1582 }
1583
1584 case VIDIOCGUNIT:
1585 {
1586 struct video_unit *vu = arg;
1587
1588 vu->video = pdev->vdev->minor & 0x3F;
1589 vu->audio = -1; /* not known yet */
1590 vu->vbi = -1;
1591 vu->radio = -1;
1592 vu->teletext = -1;
1593 break;
1594 }
1595 default:
1596 return pwc_ioctl(pdev, cmd, arg);
1597 } /* ..switch */
1598 return 0;
1599}
1600
1601static int pwc_video_ioctl(struct inode *inode, struct file *file,
1602 unsigned int cmd, unsigned long arg)
1603{
1604 return video_usercopy(inode, file, cmd, arg, pwc_video_do_ioctl);
1605}
1606
1607
1608static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma)
1609{
1610 struct video_device *vdev = file->private_data;
1611 struct pwc_device *pdev;
1612 unsigned long start = vma->vm_start;
1613 unsigned long size = vma->vm_end-vma->vm_start;
1614 unsigned long page, pos;
1615
1616 Trace(TRACE_MEMORY, "mmap(0x%p, 0x%lx, %lu) called.\n", vdev, start, size);
1617 pdev = vdev->priv;
1618
1619 vma->vm_flags |= VM_IO;
1620
1621 pos = (unsigned long)pdev->image_data;
1622 while (size > 0) {
1623 page = vmalloc_to_pfn((void *)pos);
1624 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1625 return -EAGAIN;
1626
1627 start += PAGE_SIZE;
1628 pos += PAGE_SIZE;
1629 if (size > PAGE_SIZE)
1630 size -= PAGE_SIZE;
1631 else
1632 size = 0;
1633 }
1634
1635 return 0;
1636}
1637
1638/***************************************************************************/
1639/* USB functions */
1640
1641/* This function gets called when a new device is plugged in or the usb core
1642 * is loaded.
1643 */
1644
1645static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
1646{
1647 struct usb_device *udev = interface_to_usbdev(intf);
1648 struct pwc_device *pdev = NULL;
1649 int vendor_id, product_id, type_id;
1650 int i, hint;
1651 int features = 0;
1652 int video_nr = -1; /* default: use next available device */
1653 char serial_number[30], *name;
1654
1655 /* Check if we can handle this device */
1656 Trace(TRACE_PROBE, "probe() called [%04X %04X], if %d\n",
1657 le16_to_cpu(udev->descriptor.idVendor),
1658 le16_to_cpu(udev->descriptor.idProduct),
1659 intf->altsetting->desc.bInterfaceNumber);
1660
1661 /* the interfaces are probed one by one. We are only interested in the
1662 video interface (0) now.
1663 Interface 1 is the Audio Control, and interface 2 Audio itself.
1664 */
1665 if (intf->altsetting->desc.bInterfaceNumber > 0)
1666 return -ENODEV;
1667
1668 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
1669 product_id = le16_to_cpu(udev->descriptor.idProduct);
1670
1671 if (vendor_id == 0x0471) {
1672 switch (product_id) {
1673 case 0x0302:
1674 Info("Philips PCA645VC USB webcam detected.\n");
1675 name = "Philips 645 webcam";
1676 type_id = 645;
1677 break;
1678 case 0x0303:
1679 Info("Philips PCA646VC USB webcam detected.\n");
1680 name = "Philips 646 webcam";
1681 type_id = 646;
1682 break;
1683 case 0x0304:
1684 Info("Askey VC010 type 2 USB webcam detected.\n");
1685 name = "Askey VC010 webcam";
1686 type_id = 646;
1687 break;
1688 case 0x0307:
1689 Info("Philips PCVC675K (Vesta) USB webcam detected.\n");
1690 name = "Philips 675 webcam";
1691 type_id = 675;
1692 break;
1693 case 0x0308:
1694 Info("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
1695 name = "Philips 680 webcam";
1696 type_id = 680;
1697 break;
1698 case 0x030C:
1699 Info("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
1700 name = "Philips 690 webcam";
1701 type_id = 690;
1702 break;
1703 case 0x0310:
1704 Info("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
1705 name = "Philips 730 webcam";
1706 type_id = 730;
1707 break;
1708 case 0x0311:
1709 Info("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
1710 name = "Philips 740 webcam";
1711 type_id = 740;
1712 break;
1713 case 0x0312:
1714 Info("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
1715 name = "Philips 750 webcam";
1716 type_id = 750;
1717 break;
1718 case 0x0313:
1719 Info("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
1720 name = "Philips 720K/40 webcam";
1721 type_id = 720;
1722 break;
1723 default:
1724 return -ENODEV;
1725 break;
1726 }
1727 }
1728 else if (vendor_id == 0x069A) {
1729 switch(product_id) {
1730 case 0x0001:
1731 Info("Askey VC010 type 1 USB webcam detected.\n");
1732 name = "Askey VC010 webcam";
1733 type_id = 645;
1734 break;
1735 default:
1736 return -ENODEV;
1737 break;
1738 }
1739 }
1740 else if (vendor_id == 0x046d) {
1741 switch(product_id) {
1742 case 0x08b0:
1743 Info("Logitech QuickCam Pro 3000 USB webcam detected.\n");
1744 name = "Logitech QuickCam Pro 3000";
1745 type_id = 740; /* CCD sensor */
1746 break;
1747 case 0x08b1:
1748 Info("Logitech QuickCam Notebook Pro USB webcam detected.\n");
1749 name = "Logitech QuickCam Notebook Pro";
1750 type_id = 740; /* CCD sensor */
1751 break;
1752 case 0x08b2:
1753 Info("Logitech QuickCam 4000 Pro USB webcam detected.\n");
1754 name = "Logitech QuickCam Pro 4000";
1755 type_id = 740; /* CCD sensor */
1756 break;
1757 case 0x08b3:
1758 Info("Logitech QuickCam Zoom USB webcam detected.\n");
1759 name = "Logitech QuickCam Zoom";
1760 type_id = 740; /* CCD sensor */
1761 break;
1762 case 0x08B4:
1763 Info("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
1764 name = "Logitech QuickCam Zoom";
1765 type_id = 740; /* CCD sensor */
1766 break;
1767 case 0x08b5:
1768 Info("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
1769 name = "Logitech QuickCam Orbit";
1770 type_id = 740; /* CCD sensor */
1771 features |= FEATURE_MOTOR_PANTILT;
1772 break;
1773 case 0x08b6:
1774 case 0x08b7:
1775 case 0x08b8:
1776 Info("Logitech QuickCam detected (reserved ID).\n");
1777 name = "Logitech QuickCam (res.)";
1778 type_id = 730; /* Assuming CMOS */
1779 break;
1780 default:
1781 return -ENODEV;
1782 break;
1783 }
1784 }
1785 else if (vendor_id == 0x055d) {
1786 /* I don't know the difference between the C10 and the C30;
1787 I suppose the difference is the sensor, but both cameras
1788 work equally well with a type_id of 675
1789 */
1790 switch(product_id) {
1791 case 0x9000:
1792 Info("Samsung MPC-C10 USB webcam detected.\n");
1793 name = "Samsung MPC-C10";
1794 type_id = 675;
1795 break;
1796 case 0x9001:
1797 Info("Samsung MPC-C30 USB webcam detected.\n");
1798 name = "Samsung MPC-C30";
1799 type_id = 675;
1800 break;
1801 default:
1802 return -ENODEV;
1803 break;
1804 }
1805 }
1806 else if (vendor_id == 0x041e) {
1807 switch(product_id) {
1808 case 0x400c:
1809 Info("Creative Labs Webcam 5 detected.\n");
1810 name = "Creative Labs Webcam 5";
1811 type_id = 730;
1812 break;
1813 case 0x4011:
1814 Info("Creative Labs Webcam Pro Ex detected.\n");
1815 name = "Creative Labs Webcam Pro Ex";
1816 type_id = 740;
1817 break;
1818 default:
1819 return -ENODEV;
1820 break;
1821 }
1822 }
1823 else if (vendor_id == 0x04cc) {
1824 switch(product_id) {
1825 case 0x8116:
1826 Info("Sotec Afina Eye USB webcam detected.\n");
1827 name = "Sotec Afina Eye";
1828 type_id = 730;
1829 break;
1830 default:
1831 return -ENODEV;
1832 break;
1833 }
1834 }
1835 else if (vendor_id == 0x06be) {
1836 switch(product_id) {
1837 case 0x8116:
1838 /* This is essentially the same cam as the Sotec Afina Eye */
1839 Info("AME Co. Afina Eye USB webcam detected.\n");
1840 name = "AME Co. Afina Eye";
1841 type_id = 750;
1842 break;
1843 default:
1844 return -ENODEV;
1845 break;
1846 }
1847
1848 }
1849 else if (vendor_id == 0x0d81) {
1850 switch(product_id) {
1851 case 0x1900:
1852 Info("Visionite VCS-UC300 USB webcam detected.\n");
1853 name = "Visionite VCS-UC300";
1854 type_id = 740; /* CCD sensor */
1855 break;
1856 case 0x1910:
1857 Info("Visionite VCS-UM100 USB webcam detected.\n");
1858 name = "Visionite VCS-UM100";
1859 type_id = 730; /* CMOS sensor */
1860 break;
1861 default:
1862 return -ENODEV;
1863 break;
1864 }
1865 }
1866 else
1867 return -ENODEV; /* Not any of the know types; but the list keeps growing. */
1868
1869 memset(serial_number, 0, 30);
1870 usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
1871 Trace(TRACE_PROBE, "Device serial number is %s\n", serial_number);
1872
1873 if (udev->descriptor.bNumConfigurations > 1)
1874 Info("Warning: more than 1 configuration available.\n");
1875
1876 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1877 pdev = kmalloc(sizeof(struct pwc_device), GFP_KERNEL);
1878 if (pdev == NULL) {
1879 Err("Oops, could not allocate memory for pwc_device.\n");
1880 return -ENOMEM;
1881 }
1882 memset(pdev, 0, sizeof(struct pwc_device));
1883 pdev->type = type_id;
1884 pdev->vsize = default_size;
1885 pdev->vframes = default_fps;
1886 strcpy(pdev->serial, serial_number);
1887 pdev->features = features;
1888 if (vendor_id == 0x046D && product_id == 0x08B5)
1889 {
1890 /* Logitech QuickCam Orbit
1891 The ranges have been determined experimentally; they may differ from cam to cam.
1892 Also, the exact ranges left-right and up-down are different for my cam
1893 */
1894 pdev->angle_range.pan_min = -7000;
1895 pdev->angle_range.pan_max = 7000;
1896 pdev->angle_range.tilt_min = -3000;
1897 pdev->angle_range.tilt_max = 2500;
1898 }
1899
1900 init_MUTEX(&pdev->modlock);
1901 spin_lock_init(&pdev->ptrlock);
1902
1903 pdev->udev = udev;
1904 init_waitqueue_head(&pdev->frameq);
1905 pdev->vcompression = pwc_preferred_compression;
1906
1907 /* Allocate video_device structure */
1908 pdev->vdev = video_device_alloc();
1909 if (pdev->vdev == 0)
1910 {
1911 Err("Err, cannot allocate video_device struture. Failing probe.");
1912 kfree(pdev);
1913 return -ENOMEM;
1914 }
1915 memcpy(pdev->vdev, &pwc_template, sizeof(pwc_template));
1916 strcpy(pdev->vdev->name, name);
1917 pdev->vdev->owner = THIS_MODULE;
1918 video_set_drvdata(pdev->vdev, pdev);
1919
1920 pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1921 Trace(TRACE_PROBE, "Release: %04x\n", pdev->release);
1922
1923 /* Now search device_hint[] table for a match, so we can hint a node number. */
1924 for (hint = 0; hint < MAX_DEV_HINTS; hint++) {
1925 if (((device_hint[hint].type == -1) || (device_hint[hint].type == pdev->type)) &&
1926 (device_hint[hint].pdev == NULL)) {
1927 /* so far, so good... try serial number */
1928 if ((device_hint[hint].serial_number[0] == '*') || !strcmp(device_hint[hint].serial_number, serial_number)) {
1929 /* match! */
1930 video_nr = device_hint[hint].device_node;
1931 Trace(TRACE_PROBE, "Found hint, will try to register as /dev/video%d\n", video_nr);
1932 break;
1933 }
1934 }
1935 }
1936
1937 pdev->vdev->release = video_device_release;
1938 i = video_register_device(pdev->vdev, VFL_TYPE_GRABBER, video_nr);
1939 if (i < 0) {
1940 Err("Failed to register as video device (%d).\n", i);
1941 video_device_release(pdev->vdev); /* Drip... drip... drip... */
1942 kfree(pdev); /* Oops, no memory leaks please */
1943 return -EIO;
1944 }
1945 else {
1946 Info("Registered as /dev/video%d.\n", pdev->vdev->minor & 0x3F);
1947 }
1948
1949 /* occupy slot */
1950 if (hint < MAX_DEV_HINTS)
1951 device_hint[hint].pdev = pdev;
1952
1953 Trace(TRACE_PROBE, "probe() function returning struct at 0x%p.\n", pdev);
1954 usb_set_intfdata (intf, pdev);
1955 return 0;
1956}
1957
1958/* The user janked out the cable... */
1959static void usb_pwc_disconnect(struct usb_interface *intf)
1960{
1961 struct pwc_device *pdev;
1962 int hint;
1963
1964 lock_kernel();
1965 pdev = usb_get_intfdata (intf);
1966 usb_set_intfdata (intf, NULL);
1967 if (pdev == NULL) {
1968 Err("pwc_disconnect() Called without private pointer.\n");
1969 goto disconnect_out;
1970 }
1971 if (pdev->udev == NULL) {
1972 Err("pwc_disconnect() already called for %p\n", pdev);
1973 goto disconnect_out;
1974 }
1975 if (pdev->udev != interface_to_usbdev(intf)) {
1976 Err("pwc_disconnect() Woops: pointer mismatch udev/pdev.\n");
1977 goto disconnect_out;
1978 }
1979#ifdef PWC_MAGIC
1980 if (pdev->magic != PWC_MAGIC) {
1981 Err("pwc_disconnect() Magic number failed. Consult your scrolls and try again.\n");
1982 goto disconnect_out;
1983 }
1984#endif
1985
1986 /* We got unplugged; this is signalled by an EPIPE error code */
1987 if (pdev->vopen) {
1988 Info("Disconnected while webcam is in use!\n");
1989 pdev->error_status = EPIPE;
1990 }
1991
1992 /* Alert waiting processes */
1993 wake_up_interruptible(&pdev->frameq);
1994 /* Wait until device is closed */
1995 while (pdev->vopen)
1996 schedule();
1997 /* Device is now closed, so we can safely unregister it */
1998 Trace(TRACE_PROBE, "Unregistering video device in disconnect().\n");
1999 video_unregister_device(pdev->vdev);
2000
2001 /* Free memory (don't set pdev to 0 just yet) */
2002 kfree(pdev);
2003
2004disconnect_out:
2005 /* search device_hint[] table if we occupy a slot, by any chance */
2006 for (hint = 0; hint < MAX_DEV_HINTS; hint++)
2007 if (device_hint[hint].pdev == pdev)
2008 device_hint[hint].pdev = NULL;
2009
2010 unlock_kernel();
2011}
2012
2013
2014/* *grunt* We have to do atoi ourselves :-( */
2015static int pwc_atoi(const char *s)
2016{
2017 int k = 0;
2018
2019 k = 0;
2020 while (*s != '\0' && *s >= '0' && *s <= '9') {
2021 k = 10 * k + (*s - '0');
2022 s++;
2023 }
2024 return k;
2025}
2026
2027
2028/*
2029 * Initialization code & module stuff
2030 */
2031
2032static char size[10];
2033static int fps = 0;
2034static int fbufs = 0;
2035static int mbufs = 0;
2036static int trace = -1;
2037static int compression = -1;
2038static int leds[2] = { -1, -1 };
2039static char *dev_hint[MAX_DEV_HINTS] = { };
2040
2041module_param_string(size, size, sizeof(size), 0);
2042MODULE_PARM_DESC(size, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
2043module_param(fps, int, 0000);
2044MODULE_PARM_DESC(fps, "Initial frames per second. Varies with model, useful range 5-30");
2045module_param(fbufs, int, 0000);
2046MODULE_PARM_DESC(fbufs, "Number of internal frame buffers to reserve");
2047module_param(mbufs, int, 0000);
2048MODULE_PARM_DESC(mbufs, "Number of external (mmap()ed) image buffers");
2049module_param(trace, int, 0000);
2050MODULE_PARM_DESC(trace, "For debugging purposes");
2051module_param(power_save, bool, 0000);
2052MODULE_PARM_DESC(power_save, "Turn power save feature in camera on or off");
2053module_param(compression, int, 0000);
2054MODULE_PARM_DESC(compression, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
2055module_param_array(leds, int, NULL, 0000);
2056MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
2057module_param_array(dev_hint, charp, NULL, 0000);
2058MODULE_PARM_DESC(dev_hint, "Device node hints");
2059
2060MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
2061MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
2062MODULE_LICENSE("GPL");
2063
2064static int __init usb_pwc_init(void)
2065{
2066 int i, sz;
2067 char *sizenames[PSZ_MAX] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };
2068
2069 Info("Philips webcam module version " PWC_VERSION " loaded.\n");
2070 Info("Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.\n");
2071 Info("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
2072 Info("the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
2073
2074 if (fps) {
2075 if (fps < 4 || fps > 30) {
2076 Err("Framerate out of bounds (4-30).\n");
2077 return -EINVAL;
2078 }
2079 default_fps = fps;
2080 Info("Default framerate set to %d.\n", default_fps);
2081 }
2082
2083 if (size[0]) {
2084 /* string; try matching with array */
2085 for (sz = 0; sz < PSZ_MAX; sz++) {
2086 if (!strcmp(sizenames[sz], size)) { /* Found! */
2087 default_size = sz;
2088 break;
2089 }
2090 }
2091 if (sz == PSZ_MAX) {
2092 Err("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
2093 return -EINVAL;
2094 }
2095 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);
2096 }
2097 if (mbufs) {
2098 if (mbufs < 1 || mbufs > MAX_IMAGES) {
2099 Err("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES);
2100 return -EINVAL;
2101 }
2102 default_mbufs = mbufs;
2103 Info("Number of image buffers set to %d.\n", default_mbufs);
2104 }
2105 if (fbufs) {
2106 if (fbufs < 2 || fbufs > MAX_FRAMES) {
2107 Err("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES);
2108 return -EINVAL;
2109 }
2110 default_fbufs = fbufs;
2111 Info("Number of frame buffers set to %d.\n", default_fbufs);
2112 }
2113 if (trace >= 0) {
2114 Info("Trace options: 0x%04x\n", trace);
2115 pwc_trace = trace;
2116 }
2117 if (compression >= 0) {
2118 if (compression > 3) {
2119 Err("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
2120 return -EINVAL;
2121 }
2122 pwc_preferred_compression = compression;
2123 Info("Preferred compression set to %d.\n", pwc_preferred_compression);
2124 }
2125 if (power_save)
2126 Info("Enabling power save on open/close.\n");
2127 if (leds[0] >= 0)
2128 led_on = leds[0];
2129 if (leds[1] >= 0)
2130 led_off = leds[1];
2131
Steven Cole093cf722005-05-03 19:07:24 -06002132 /* Big device node whoopla. Basically, it allows you to assign a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 device node (/dev/videoX) to a camera, based on its type
2134 & serial number. The format is [type[.serialnumber]:]node.
2135
2136 Any camera that isn't matched by these rules gets the next
2137 available free device node.
2138 */
2139 for (i = 0; i < MAX_DEV_HINTS; i++) {
2140 char *s, *colon, *dot;
2141
2142 /* This loop also initializes the array */
2143 device_hint[i].pdev = NULL;
2144 s = dev_hint[i];
2145 if (s != NULL && *s != '\0') {
2146 device_hint[i].type = -1; /* wildcard */
2147 strcpy(device_hint[i].serial_number, "*");
2148
2149 /* parse string: chop at ':' & '/' */
2150 colon = dot = s;
2151 while (*colon != '\0' && *colon != ':')
2152 colon++;
2153 while (*dot != '\0' && *dot != '.')
2154 dot++;
2155 /* Few sanity checks */
2156 if (*dot != '\0' && dot > colon) {
2157 Err("Malformed camera hint: the colon must be after the dot.\n");
2158 return -EINVAL;
2159 }
2160
2161 if (*colon == '\0') {
2162 /* No colon */
2163 if (*dot != '\0') {
2164 Err("Malformed camera hint: no colon + device node given.\n");
2165 return -EINVAL;
2166 }
2167 else {
2168 /* No type or serial number specified, just a number. */
2169 device_hint[i].device_node = pwc_atoi(s);
2170 }
2171 }
2172 else {
2173 /* There's a colon, so we have at least a type and a device node */
2174 device_hint[i].type = pwc_atoi(s);
2175 device_hint[i].device_node = pwc_atoi(colon + 1);
2176 if (*dot != '\0') {
2177 /* There's a serial number as well */
2178 int k;
2179
2180 dot++;
2181 k = 0;
2182 while (*dot != ':' && k < 29) {
2183 device_hint[i].serial_number[k++] = *dot;
2184 dot++;
2185 }
2186 device_hint[i].serial_number[k] = '\0';
2187 }
2188 }
2189#if PWC_DEBUG
2190 Debug("device_hint[%d]:\n", i);
2191 Debug(" type : %d\n", device_hint[i].type);
2192 Debug(" serial# : %s\n", device_hint[i].serial_number);
2193 Debug(" node : %d\n", device_hint[i].device_node);
2194#endif
2195 }
2196 else
2197 device_hint[i].type = 0; /* not filled */
2198 } /* ..for MAX_DEV_HINTS */
2199
2200 Trace(TRACE_PROBE, "Registering driver at address 0x%p.\n", &pwc_driver);
2201 return usb_register(&pwc_driver);
2202}
2203
2204static void __exit usb_pwc_exit(void)
2205{
2206 Trace(TRACE_MODULE, "Deregistering driver.\n");
2207 usb_deregister(&pwc_driver);
2208 Info("Philips webcam module removed.\n");
2209}
2210
2211module_init(usb_pwc_init);
2212module_exit(usb_pwc_exit);
2213