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