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