[media] pwc: Remove a bunch of bogus sanity checks / don't return EFAULT wrongly
[linux-2.6-block.git] / drivers / media / video / pwc / pwc-if.c
CommitLineData
1da177e4
LT
1/* Linux driver for Philips webcam
2 USB and Video4Linux interface part.
3 (C) 1999-2004 Nemosoft Unv.
2b455db6 4 (C) 2004-2006 Luc Saillard (luc@saillard.org)
1da177e4
LT
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
d56410e0 28/*
1da177e4
LT
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
d56410e0 32 is thus not distributed in source form. The binary pwcx.o module
1da177e4 33 contains the code that falls under the NDA.
d56410e0
MCC
34
35 In case you're wondering: 'pwc' stands for "Philips WebCam", but
1da177e4
LT
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).
d56410e0 39
1da177e4
LT
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 *
9a7b2d1f 43 vdev: struct video_device (member of pwc_dev)
1da177e4
LT
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
e32a7ecc 56 - Pham Thanh Nam: webcam snapshot button as an event input device
1da177e4
LT
57*/
58
59#include <linux/errno.h>
60#include <linux/init.h>
61#include <linux/mm.h>
62#include <linux/module.h>
63#include <linux/poll.h>
64#include <linux/slab.h>
e32a7ecc
NPT
65#ifdef CONFIG_USB_PWC_INPUT_EVDEV
66#include <linux/usb/input.h>
67#endif
1da177e4
LT
68#include <linux/vmalloc.h>
69#include <asm/io.h>
2d8d7762 70#include <linux/kernel.h> /* simple_strtol() */
1da177e4
LT
71
72#include "pwc.h"
1da177e4
LT
73#include "pwc-kiara.h"
74#include "pwc-timon.h"
2b455db6
LS
75#include "pwc-dec23.h"
76#include "pwc-dec1.h"
1da177e4
LT
77#include "pwc-uncompress.h"
78
79/* Function prototypes and driver templates */
80
81/* hotplug device table support */
2b455db6 82static const struct usb_device_id pwc_device_table [] = {
1da177e4
LT
83 { USB_DEVICE(0x0471, 0x0302) }, /* Philips models */
84 { USB_DEVICE(0x0471, 0x0303) },
85 { USB_DEVICE(0x0471, 0x0304) },
86 { USB_DEVICE(0x0471, 0x0307) },
87 { USB_DEVICE(0x0471, 0x0308) },
88 { USB_DEVICE(0x0471, 0x030C) },
89 { USB_DEVICE(0x0471, 0x0310) },
2b455db6 90 { USB_DEVICE(0x0471, 0x0311) }, /* Philips ToUcam PRO II */
1da177e4
LT
91 { USB_DEVICE(0x0471, 0x0312) },
92 { USB_DEVICE(0x0471, 0x0313) }, /* the 'new' 720K */
2b455db6 93 { USB_DEVICE(0x0471, 0x0329) }, /* Philips SPC 900NC PC Camera */
1da177e4
LT
94 { USB_DEVICE(0x069A, 0x0001) }, /* Askey */
95 { USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam Pro 3000 */
96 { USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
97 { USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam Pro 4000 */
98 { USB_DEVICE(0x046D, 0x08B3) }, /* Logitech QuickCam Zoom (old model) */
99 { USB_DEVICE(0x046D, 0x08B4) }, /* Logitech QuickCam Zoom (new model) */
100 { USB_DEVICE(0x046D, 0x08B5) }, /* Logitech QuickCam Orbit/Sphere */
6b1ce3c1
MCC
101 { USB_DEVICE(0x046D, 0x08B6) }, /* Cisco VT Camera */
102 { USB_DEVICE(0x046D, 0x08B7) }, /* Logitech ViewPort AV 100 */
1da177e4 103 { USB_DEVICE(0x046D, 0x08B8) }, /* Logitech (reserved) */
2b455db6
LS
104 { USB_DEVICE(0x055D, 0x9000) }, /* Samsung MPC-C10 */
105 { USB_DEVICE(0x055D, 0x9001) }, /* Samsung MPC-C30 */
106 { USB_DEVICE(0x055D, 0x9002) }, /* Samsung SNC-35E (Ver3.0) */
1da177e4
LT
107 { USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
108 { USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
109 { USB_DEVICE(0x04CC, 0x8116) }, /* Afina Eye */
110 { USB_DEVICE(0x06BE, 0x8116) }, /* new Afina Eye */
111 { USB_DEVICE(0x0d81, 0x1910) }, /* Visionite */
112 { USB_DEVICE(0x0d81, 0x1900) },
113 { }
114};
115MODULE_DEVICE_TABLE(usb, pwc_device_table);
116
117static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id);
118static void usb_pwc_disconnect(struct usb_interface *intf);
119
120static struct usb_driver pwc_driver = {
1da177e4
LT
121 .name = "Philips webcam", /* name */
122 .id_table = pwc_device_table,
123 .probe = usb_pwc_probe, /* probe() */
124 .disconnect = usb_pwc_disconnect, /* disconnect() */
125};
126
127#define MAX_DEV_HINTS 20
128#define MAX_ISOC_ERRORS 20
129
130static int default_size = PSZ_QCIF;
131static int default_fps = 10;
132static int default_fbufs = 3; /* Default number of frame buffers */
b930e1d8 133 int pwc_mbufs = 2; /* Default number of mmap() buffers */
05ad3907 134#ifdef CONFIG_USB_PWC_DEBUG
b930e1d8 135 int pwc_trace = PWC_DEBUG_LEVEL;
2b455db6 136#endif
ff699e6b
DSL
137static int power_save;
138static int led_on = 100, led_off; /* defaults to LED that is on while in use */
b20c3cf0 139static int pwc_preferred_compression = 1; /* 0..3 = uncompressed..high */
1da177e4
LT
140static struct {
141 int type;
142 char serial_number[30];
143 int device_node;
144 struct pwc_device *pdev;
145} device_hint[MAX_DEV_HINTS];
146
147/***/
148
bec43661
HV
149static int pwc_video_open(struct file *file);
150static int pwc_video_close(struct file *file);
2b455db6 151static ssize_t pwc_video_read(struct file *file, char __user *buf,
1da177e4
LT
152 size_t count, loff_t *ppos);
153static unsigned int pwc_video_poll(struct file *file, poll_table *wait);
1da177e4 154static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma);
9a7b2d1f 155static void pwc_video_release(struct video_device *vfd);
1da177e4 156
bec43661 157static const struct v4l2_file_operations pwc_fops = {
1da177e4
LT
158 .owner = THIS_MODULE,
159 .open = pwc_video_open,
160 .release = pwc_video_close,
161 .read = pwc_video_read,
162 .poll = pwc_video_poll,
163 .mmap = pwc_video_mmap,
afa38521 164 .unlocked_ioctl = video_ioctl2,
1da177e4
LT
165};
166static struct video_device pwc_template = {
1da177e4 167 .name = "Philips Webcam", /* Filled in later */
9a7b2d1f 168 .release = pwc_video_release,
1da177e4 169 .fops = &pwc_fops,
9a7b2d1f 170 .ioctl_ops = &pwc_ioctl_ops,
1da177e4
LT
171};
172
1da177e4
LT
173/***************************************************************************/
174/* Private functions */
175
176/* Here we want the physical address of the memory.
177 * This is used when initializing the contents of the area.
178 */
1da177e4 179
1da177e4 180
2b455db6
LS
181
182static void *pwc_rvmalloc(unsigned long size)
1da177e4
LT
183{
184 void * mem;
185 unsigned long adr;
186
d56410e0 187 mem=vmalloc_32(size);
2b455db6
LS
188 if (!mem)
189 return NULL;
190
191 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
192 adr=(unsigned long) mem;
193 while (size > 0)
194 {
195 SetPageReserved(vmalloc_to_page((void *)adr));
196 adr += PAGE_SIZE;
197 size -= PAGE_SIZE;
198 }
1da177e4
LT
199 return mem;
200}
201
2b455db6 202static void pwc_rvfree(void * mem, unsigned long size)
1da177e4 203{
d56410e0 204 unsigned long adr;
1da177e4 205
2b455db6
LS
206 if (!mem)
207 return;
208
209 adr=(unsigned long) mem;
210 while ((long) size > 0)
211 {
212 ClearPageReserved(vmalloc_to_page((void *)adr));
213 adr += PAGE_SIZE;
214 size -= PAGE_SIZE;
215 }
216 vfree(mem);
1da177e4
LT
217}
218
219
220
221
222static int pwc_allocate_buffers(struct pwc_device *pdev)
223{
2b455db6 224 int i, err;
1da177e4
LT
225 void *kbuf;
226
2b455db6 227 PWC_DEBUG_MEMORY(">> pwc_allocate_buffers(pdev = 0x%p)\n", pdev);
1da177e4 228
2b455db6 229 /* Allocate Isochronuous pipe buffers */
1da177e4
LT
230 for (i = 0; i < MAX_ISO_BUFS; i++) {
231 if (pdev->sbuf[i].data == NULL) {
2b455db6 232 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
1da177e4 233 if (kbuf == NULL) {
2b455db6 234 PWC_ERROR("Failed to allocate iso buffer %d.\n", i);
1da177e4
LT
235 return -ENOMEM;
236 }
2b455db6 237 PWC_DEBUG_MEMORY("Allocated iso buffer at %p.\n", kbuf);
1da177e4 238 pdev->sbuf[i].data = kbuf;
1da177e4
LT
239 }
240 }
241
242 /* Allocate frame buffer structure */
243 if (pdev->fbuf == NULL) {
2b455db6 244 kbuf = kzalloc(default_fbufs * sizeof(struct pwc_frame_buf), GFP_KERNEL);
1da177e4 245 if (kbuf == NULL) {
2b455db6 246 PWC_ERROR("Failed to allocate frame buffer structure.\n");
1da177e4
LT
247 return -ENOMEM;
248 }
2b455db6 249 PWC_DEBUG_MEMORY("Allocated frame buffer structure at %p.\n", kbuf);
1da177e4 250 pdev->fbuf = kbuf;
1da177e4 251 }
2b455db6 252
1da177e4
LT
253 /* create frame buffers, and make circular ring */
254 for (i = 0; i < default_fbufs; i++) {
255 if (pdev->fbuf[i].data == NULL) {
101b25b5 256 kbuf = vzalloc(PWC_FRAME_SIZE); /* need vmalloc since frame buffer > 128K */
1da177e4 257 if (kbuf == NULL) {
2b455db6 258 PWC_ERROR("Failed to allocate frame buffer %d.\n", i);
1da177e4
LT
259 return -ENOMEM;
260 }
2b455db6 261 PWC_DEBUG_MEMORY("Allocated frame buffer %d at %p.\n", i, kbuf);
1da177e4 262 pdev->fbuf[i].data = kbuf;
1da177e4
LT
263 }
264 }
d56410e0 265
1da177e4 266 /* Allocate decompressor table space */
2b455db6
LS
267 if (DEVICE_USE_CODEC1(pdev->type))
268 err = pwc_dec1_alloc(pdev);
269 else
270 err = pwc_dec23_alloc(pdev);
271
272 if (err) {
273 PWC_ERROR("Failed to allocate decompress table.\n");
274 return err;
275 }
d56410e0 276
1da177e4 277 /* Allocate image buffer; double buffer for mmap() */
2b455db6 278 kbuf = pwc_rvmalloc(pwc_mbufs * pdev->len_per_image);
1da177e4 279 if (kbuf == NULL) {
2b455db6
LS
280 PWC_ERROR("Failed to allocate image buffer(s). needed (%d)\n",
281 pwc_mbufs * pdev->len_per_image);
1da177e4
LT
282 return -ENOMEM;
283 }
2b455db6 284 PWC_DEBUG_MEMORY("Allocated image buffer at %p.\n", kbuf);
1da177e4 285 pdev->image_data = kbuf;
2b455db6
LS
286 for (i = 0; i < pwc_mbufs; i++) {
287 pdev->images[i].offset = i * pdev->len_per_image;
288 pdev->images[i].vma_use_count = 0;
289 }
290 for (; i < MAX_IMAGES; i++) {
291 pdev->images[i].offset = 0;
292 }
1da177e4
LT
293
294 kbuf = NULL;
d56410e0 295
2b455db6 296 PWC_DEBUG_MEMORY("<< pwc_allocate_buffers()\n");
1da177e4
LT
297 return 0;
298}
299
300static void pwc_free_buffers(struct pwc_device *pdev)
301{
302 int i;
303
2b455db6 304 PWC_DEBUG_MEMORY("Entering free_buffers(%p).\n", pdev);
1da177e4 305
1da177e4
LT
306 /* Release Iso-pipe buffers */
307 for (i = 0; i < MAX_ISO_BUFS; i++)
308 if (pdev->sbuf[i].data != NULL) {
2b455db6 309 PWC_DEBUG_MEMORY("Freeing ISO buffer at %p.\n", pdev->sbuf[i].data);
1da177e4
LT
310 kfree(pdev->sbuf[i].data);
311 pdev->sbuf[i].data = NULL;
312 }
313
314 /* The same for frame buffers */
315 if (pdev->fbuf != NULL) {
316 for (i = 0; i < default_fbufs; i++) {
317 if (pdev->fbuf[i].data != NULL) {
2b455db6 318 PWC_DEBUG_MEMORY("Freeing frame buffer %d at %p.\n", i, pdev->fbuf[i].data);
1da177e4
LT
319 vfree(pdev->fbuf[i].data);
320 pdev->fbuf[i].data = NULL;
321 }
322 }
323 kfree(pdev->fbuf);
324 pdev->fbuf = NULL;
325 }
326
327 /* Intermediate decompression buffer & tables */
328 if (pdev->decompress_data != NULL) {
2b455db6 329 PWC_DEBUG_MEMORY("Freeing decompression buffer at %p.\n", pdev->decompress_data);
1da177e4
LT
330 kfree(pdev->decompress_data);
331 pdev->decompress_data = NULL;
332 }
1da177e4
LT
333
334 /* Release image buffers */
335 if (pdev->image_data != NULL) {
2b455db6
LS
336 PWC_DEBUG_MEMORY("Freeing image buffer at %p.\n", pdev->image_data);
337 pwc_rvfree(pdev->image_data, pwc_mbufs * pdev->len_per_image);
1da177e4
LT
338 }
339 pdev->image_data = NULL;
d56410e0 340
2b455db6 341 PWC_DEBUG_MEMORY("Leaving free_buffers().\n");
1da177e4
LT
342}
343
d56410e0 344/* The frame & image buffer mess.
1da177e4
LT
345
346 Yes, this is a mess. Well, it used to be simple, but alas... In this
347 module, 3 buffers schemes are used to get the data from the USB bus to
348 the user program. The first scheme involves the ISO buffers (called thus
349 since they transport ISO data from the USB controller), and not really
d56410e0 350 interesting. Suffices to say the data from this buffer is quickly
1da177e4
LT
351 gathered in an interrupt handler (pwc_isoc_handler) and placed into the
352 frame buffer.
353
354 The frame buffer is the second scheme, and is the central element here.
355 It collects the data from a single frame from the camera (hence, the
356 name). Frames are delimited by the USB camera with a short USB packet,
357 so that's easy to detect. The frame buffers form a list that is filled
358 by the camera+USB controller and drained by the user process through
359 either read() or mmap().
360
361 The image buffer is the third scheme, in which frames are decompressed
362 and converted into planar format. For mmap() there is more than
363 one image buffer available.
364
365 The frame buffers provide the image buffering. In case the user process
366 is a bit slow, this introduces lag and some undesired side-effects.
367 The problem arises when the frame buffer is full. I used to drop the last
368 frame, which makes the data in the queue stale very quickly. But dropping
369 the frame at the head of the queue proved to be a litte bit more difficult.
370 I tried a circular linked scheme, but this introduced more problems than
371 it solved.
372
373 Because filling and draining are completely asynchronous processes, this
374 requires some fiddling with pointers and mutexes.
375
376 Eventually, I came up with a system with 2 lists: an 'empty' frame list
377 and a 'full' frame list:
378 * Initially, all frame buffers but one are on the 'empty' list; the one
379 remaining buffer is our initial fill frame.
d56410e0
MCC
380 * If a frame is needed for filling, we try to take it from the 'empty'
381 list, unless that list is empty, in which case we take the buffer at
1da177e4
LT
382 the head of the 'full' list.
383 * When our fill buffer has been filled, it is appended to the 'full'
384 list.
385 * If a frame is needed by read() or mmap(), it is taken from the head of
386 the 'full' list, handled, and then appended to the 'empty' list. If no
387 buffer is present on the 'full' list, we wait.
388 The advantage is that the buffer that is currently being decompressed/
389 converted, is on neither list, and thus not in our way (any other scheme
390 I tried had the problem of old data lingering in the queue).
391
392 Whatever strategy you choose, it always remains a tradeoff: with more
393 frame buffers the chances of a missed frame are reduced. On the other
394 hand, on slower machines it introduces lag because the queue will
395 always be full.
396 */
397
398/**
399 \brief Find next frame buffer to fill. Take from empty or full list, whichever comes first.
400 */
2b455db6 401static int pwc_next_fill_frame(struct pwc_device *pdev)
1da177e4
LT
402{
403 int ret;
404 unsigned long flags;
405
406 ret = 0;
407 spin_lock_irqsave(&pdev->ptrlock, flags);
408 if (pdev->fill_frame != NULL) {
409 /* append to 'full' list */
410 if (pdev->full_frames == NULL) {
411 pdev->full_frames = pdev->fill_frame;
412 pdev->full_frames_tail = pdev->full_frames;
413 }
414 else {
415 pdev->full_frames_tail->next = pdev->fill_frame;
416 pdev->full_frames_tail = pdev->fill_frame;
417 }
418 }
419 if (pdev->empty_frames != NULL) {
420 /* We have empty frames available. That's easy */
421 pdev->fill_frame = pdev->empty_frames;
422 pdev->empty_frames = pdev->empty_frames->next;
423 }
424 else {
425 /* Hmm. Take it from the full list */
1da177e4
LT
426 /* sanity check */
427 if (pdev->full_frames == NULL) {
2b455db6 428 PWC_ERROR("Neither empty or full frames available!\n");
1da177e4
LT
429 spin_unlock_irqrestore(&pdev->ptrlock, flags);
430 return -EINVAL;
431 }
1da177e4
LT
432 pdev->fill_frame = pdev->full_frames;
433 pdev->full_frames = pdev->full_frames->next;
434 ret = 1;
435 }
436 pdev->fill_frame->next = NULL;
1da177e4
LT
437 spin_unlock_irqrestore(&pdev->ptrlock, flags);
438 return ret;
439}
440
441
442/**
443 \brief Reset all buffers, pointers and lists, except for the image_used[] buffer.
444
445 If the image_used[] buffer is cleared too, mmap()/VIDIOCSYNC will run into trouble.
446 */
447static void pwc_reset_buffers(struct pwc_device *pdev)
448{
449 int i;
450 unsigned long flags;
451
645635b0 452 PWC_DEBUG_MEMORY(">> %s __enter__\n", __func__);
2b455db6 453
1da177e4
LT
454 spin_lock_irqsave(&pdev->ptrlock, flags);
455 pdev->full_frames = NULL;
456 pdev->full_frames_tail = NULL;
457 for (i = 0; i < default_fbufs; i++) {
458 pdev->fbuf[i].filled = 0;
459 if (i > 0)
460 pdev->fbuf[i].next = &pdev->fbuf[i - 1];
461 else
462 pdev->fbuf->next = NULL;
463 }
464 pdev->empty_frames = &pdev->fbuf[default_fbufs - 1];
465 pdev->empty_frames_tail = pdev->fbuf;
466 pdev->read_frame = NULL;
467 pdev->fill_frame = pdev->empty_frames;
468 pdev->empty_frames = pdev->empty_frames->next;
469
470 pdev->image_read_pos = 0;
471 pdev->fill_image = 0;
472 spin_unlock_irqrestore(&pdev->ptrlock, flags);
2b455db6 473
645635b0 474 PWC_DEBUG_MEMORY("<< %s __leaving__\n", __func__);
1da177e4
LT
475}
476
477
478/**
479 \brief Do all the handling for getting one frame: get pointer, decompress, advance pointers.
480 */
2b455db6 481int pwc_handle_frame(struct pwc_device *pdev)
1da177e4
LT
482{
483 int ret = 0;
484 unsigned long flags;
485
486 spin_lock_irqsave(&pdev->ptrlock, flags);
487 /* First grab our read_frame; this is removed from all lists, so
488 we can release the lock after this without problems */
489 if (pdev->read_frame != NULL) {
490 /* This can't theoretically happen */
2b455db6
LS
491 PWC_ERROR("Huh? Read frame still in use?\n");
492 spin_unlock_irqrestore(&pdev->ptrlock, flags);
493 return ret;
494 }
495
496
497 if (pdev->full_frames == NULL) {
498 PWC_ERROR("Woops. No frames ready.\n");
1da177e4
LT
499 }
500 else {
2b455db6
LS
501 pdev->read_frame = pdev->full_frames;
502 pdev->full_frames = pdev->full_frames->next;
503 pdev->read_frame->next = NULL;
504 }
505
506 if (pdev->read_frame != NULL) {
efad798b 507 /* Decompression is a lengthy process, so it's outside of the lock.
2b455db6
LS
508 This gives the isoc_handler the opportunity to fill more frames
509 in the mean time.
510 */
511 spin_unlock_irqrestore(&pdev->ptrlock, flags);
512 ret = pwc_decompress(pdev);
513 spin_lock_irqsave(&pdev->ptrlock, flags);
514
515 /* We're done with read_buffer, tack it to the end of the empty buffer list */
516 if (pdev->empty_frames == NULL) {
517 pdev->empty_frames = pdev->read_frame;
518 pdev->empty_frames_tail = pdev->empty_frames;
1da177e4
LT
519 }
520 else {
2b455db6
LS
521 pdev->empty_frames_tail->next = pdev->read_frame;
522 pdev->empty_frames_tail = pdev->read_frame;
1da177e4 523 }
2b455db6 524 pdev->read_frame = NULL;
1da177e4
LT
525 }
526 spin_unlock_irqrestore(&pdev->ptrlock, flags);
527 return ret;
528}
529
530/**
531 \brief Advance pointers of image buffer (after each user request)
532*/
2b455db6 533void pwc_next_image(struct pwc_device *pdev)
1da177e4
LT
534{
535 pdev->image_used[pdev->fill_image] = 0;
2b455db6 536 pdev->fill_image = (pdev->fill_image + 1) % pwc_mbufs;
1da177e4
LT
537}
538
2b455db6
LS
539/**
540 * Print debug information when a frame is discarded because all of our buffer
541 * is full
542 */
543static void pwc_frame_dumped(struct pwc_device *pdev)
544{
545 pdev->vframes_dumped++;
546 if (pdev->vframe_count < FRAME_LOWMARK)
547 return;
548
549 if (pdev->vframes_dumped < 20)
550 PWC_DEBUG_FLOW("Dumping frame %d\n", pdev->vframe_count);
551 else if (pdev->vframes_dumped == 20)
552 PWC_DEBUG_FLOW("Dumping frame %d (last message)\n",
553 pdev->vframe_count);
554}
555
e32a7ecc
NPT
556static void pwc_snapshot_button(struct pwc_device *pdev, int down)
557{
558 if (down) {
559 PWC_TRACE("Snapshot button pressed.\n");
560 pdev->snapshot_button_status = 1;
561 } else {
562 PWC_TRACE("Snapshot button released.\n");
563 }
564
565#ifdef CONFIG_USB_PWC_INPUT_EVDEV
566 if (pdev->button_dev) {
bcd3e4b3 567 input_report_key(pdev->button_dev, KEY_CAMERA, down);
e32a7ecc
NPT
568 input_sync(pdev->button_dev);
569 }
570#endif
571}
572
2b455db6
LS
573static int pwc_rcv_short_packet(struct pwc_device *pdev, const struct pwc_frame_buf *fbuf)
574{
575 int awake = 0;
576
577 /* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
578 frames on the USB wire after an exposure change. This conditition is
579 however detected in the cam and a bit is set in the header.
580 */
581 if (pdev->type == 730) {
582 unsigned char *ptr = (unsigned char *)fbuf->data;
583
584 if (ptr[1] == 1 && ptr[0] & 0x10) {
585 PWC_TRACE("Hyundai CMOS sensor bug. Dropping frame.\n");
586 pdev->drop_frames += 2;
587 pdev->vframes_error++;
588 }
589 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
e32a7ecc 590 pwc_snapshot_button(pdev, ptr[0] & 0x01);
2b455db6
LS
591 }
592 if ((ptr[0] ^ pdev->vmirror) & 0x02) {
593 if (ptr[0] & 0x02)
594 PWC_TRACE("Image is mirrored.\n");
595 else
596 PWC_TRACE("Image is normal.\n");
597 }
598 pdev->vmirror = ptr[0] & 0x03;
599 /* Sometimes the trailer of the 730 is still sent as a 4 byte packet
600 after a short frame; this condition is filtered out specifically. A 4 byte
601 frame doesn't make sense anyway.
602 So we get either this sequence:
603 drop_bit set -> 4 byte frame -> short frame -> good frame
604 Or this one:
605 drop_bit set -> short frame -> good frame
606 So we drop either 3 or 2 frames in all!
607 */
608 if (fbuf->filled == 4)
609 pdev->drop_frames++;
610 }
611 else if (pdev->type == 740 || pdev->type == 720) {
612 unsigned char *ptr = (unsigned char *)fbuf->data;
613 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
e32a7ecc 614 pwc_snapshot_button(pdev, ptr[0] & 0x01);
2b455db6
LS
615 }
616 pdev->vmirror = ptr[0] & 0x03;
617 }
618
619 /* In case we were instructed to drop the frame, do so silently.
620 The buffer pointers are not updated either (but the counters are reset below).
621 */
622 if (pdev->drop_frames > 0)
623 pdev->drop_frames--;
624 else {
625 /* Check for underflow first */
626 if (fbuf->filled < pdev->frame_total_size) {
627 PWC_DEBUG_FLOW("Frame buffer underflow (%d bytes);"
628 " discarded.\n", fbuf->filled);
629 pdev->vframes_error++;
630 }
631 else {
632 /* Send only once per EOF */
633 awake = 1; /* delay wake_ups */
634
635 /* Find our next frame to fill. This will always succeed, since we
636 * nick a frame from either empty or full list, but if we had to
637 * take it from the full list, it means a frame got dropped.
638 */
639 if (pwc_next_fill_frame(pdev))
640 pwc_frame_dumped(pdev);
641
642 }
643 } /* !drop_frames */
644 pdev->vframe_count++;
645 return awake;
646}
1da177e4
LT
647
648/* This gets called for the Isochronous pipe (video). This is done in
649 * interrupt time, so it has to be fast, not crash, and not stall. Neat.
650 */
7d12e780 651static void pwc_isoc_handler(struct urb *urb)
1da177e4
LT
652{
653 struct pwc_device *pdev;
654 int i, fst, flen;
655 int awake;
656 struct pwc_frame_buf *fbuf;
657 unsigned char *fillptr = NULL, *iso_buf = NULL;
658
659 awake = 0;
660 pdev = (struct pwc_device *)urb->context;
661 if (pdev == NULL) {
2b455db6 662 PWC_ERROR("isoc_handler() called with NULL device?!\n");
1da177e4
LT
663 return;
664 }
2b455db6 665
1da177e4 666 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
2b455db6 667 PWC_DEBUG_OPEN("URB (%p) unlinked %ssynchronuously.\n", urb, urb->status == -ENOENT ? "" : "a");
1da177e4
LT
668 return;
669 }
670 if (urb->status != -EINPROGRESS && urb->status != 0) {
671 const char *errmsg;
672
673 errmsg = "Unknown";
674 switch(urb->status) {
675 case -ENOSR: errmsg = "Buffer error (overrun)"; break;
676 case -EPIPE: errmsg = "Stalled (device not responding)"; break;
677 case -EOVERFLOW: errmsg = "Babble (bad cable?)"; break;
678 case -EPROTO: errmsg = "Bit-stuff error (bad cable?)"; break;
679 case -EILSEQ: errmsg = "CRC/Timeout (could be anything)"; break;
38e2bfc9 680 case -ETIME: errmsg = "Device does not respond"; break;
1da177e4 681 }
2b455db6 682 PWC_DEBUG_FLOW("pwc_isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
d56410e0 683 /* Give up after a number of contiguous errors on the USB bus.
1da177e4
LT
684 Appearantly something is wrong so we simulate an unplug event.
685 */
686 if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
687 {
2b455db6 688 PWC_INFO("Too many ISOC errors, bailing out.\n");
1da177e4
LT
689 pdev->error_status = EIO;
690 awake = 1;
691 wake_up_interruptible(&pdev->frameq);
692 }
693 goto handler_end; // ugly, but practical
694 }
695
696 fbuf = pdev->fill_frame;
697 if (fbuf == NULL) {
2b455db6 698 PWC_ERROR("pwc_isoc_handler without valid fill frame.\n");
1da177e4
LT
699 awake = 1;
700 goto handler_end;
701 }
702 else {
703 fillptr = fbuf->data + fbuf->filled;
704 }
705
706 /* Reset ISOC error counter. We did get here, after all. */
707 pdev->visoc_errors = 0;
708
709 /* vsync: 0 = don't copy data
d56410e0
MCC
710 1 = sync-hunt
711 2 = synched
1da177e4
LT
712 */
713 /* Compact data */
714 for (i = 0; i < urb->number_of_packets; i++) {
715 fst = urb->iso_frame_desc[i].status;
716 flen = urb->iso_frame_desc[i].actual_length;
717 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
718 if (fst == 0) {
719 if (flen > 0) { /* if valid data... */
720 if (pdev->vsync > 0) { /* ...and we are not sync-hunting... */
721 pdev->vsync = 2;
722
723 /* ...copy data to frame buffer, if possible */
724 if (flen + fbuf->filled > pdev->frame_total_size) {
2b455db6 725 PWC_DEBUG_FLOW("Frame buffer overflow (flen = %d, frame_total_size = %d).\n", flen, pdev->frame_total_size);
1da177e4
LT
726 pdev->vsync = 0; /* Hmm, let's wait for an EOF (end-of-frame) */
727 pdev->vframes_error++;
728 }
729 else {
730 memmove(fillptr, iso_buf, flen);
731 fillptr += flen;
732 }
733 }
734 fbuf->filled += flen;
735 } /* ..flen > 0 */
736
737 if (flen < pdev->vlast_packet_size) {
d56410e0 738 /* Shorter packet... We probably have the end of an image-frame;
1da177e4
LT
739 wake up read() process and let select()/poll() do something.
740 Decompression is done in user time over there.
2b455db6 741 */
1da177e4 742 if (pdev->vsync == 2) {
2b455db6
LS
743 if (pwc_rcv_short_packet(pdev, fbuf)) {
744 awake = 1;
745 fbuf = pdev->fill_frame;
1da177e4 746 }
1da177e4
LT
747 }
748 fbuf->filled = 0;
749 fillptr = fbuf->data;
750 pdev->vsync = 1;
2b455db6
LS
751 }
752
1da177e4
LT
753 pdev->vlast_packet_size = flen;
754 } /* ..status == 0 */
1da177e4 755 else {
2b455db6 756 /* This is normally not interesting to the user, unless
ff699e6b
DSL
757 * you are really debugging something, default = 0 */
758 static int iso_error;
1da177e4
LT
759 iso_error++;
760 if (iso_error < 20)
2b455db6 761 PWC_DEBUG_FLOW("Iso frame %d of USB has error %d\n", i, fst);
1da177e4 762 }
1da177e4
LT
763 }
764
765handler_end:
766 if (awake)
767 wake_up_interruptible(&pdev->frameq);
768
769 urb->dev = pdev->udev;
770 i = usb_submit_urb(urb, GFP_ATOMIC);
771 if (i != 0)
2b455db6 772 PWC_ERROR("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
1da177e4
LT
773}
774
775
2b455db6 776int pwc_isoc_init(struct pwc_device *pdev)
1da177e4
LT
777{
778 struct usb_device *udev;
779 struct urb *urb;
780 int i, j, ret;
1da177e4
LT
781 struct usb_interface *intf;
782 struct usb_host_interface *idesc = NULL;
783
1da177e4
LT
784 if (pdev->iso_init)
785 return 0;
786 pdev->vsync = 0;
787 udev = pdev->udev;
788
789 /* Get the current alternate interface, adjust packet size */
1da177e4
LT
790 intf = usb_ifnum_to_if(udev, 0);
791 if (intf)
792 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
1da177e4 793 if (!idesc)
c2464121 794 return -EIO;
1da177e4
LT
795
796 /* Search video endpoint */
797 pdev->vmax_packet_size = -1;
2b455db6 798 for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
1da177e4
LT
799 if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
800 pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
801 break;
802 }
2b455db6 803 }
d56410e0 804
1da177e4 805 if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
2b455db6 806 PWC_ERROR("Failed to find packet size for video endpoint in current alternate setting.\n");
093cf723 807 return -ENFILE; /* Odd error, that should be noticeable */
1da177e4
LT
808 }
809
810 /* Set alternate interface */
811 ret = 0;
2b455db6 812 PWC_DEBUG_OPEN("Setting alternate interface %d\n", pdev->valternate);
1da177e4
LT
813 ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
814 if (ret < 0)
815 return ret;
816
817 for (i = 0; i < MAX_ISO_BUFS; i++) {
818 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
819 if (urb == NULL) {
2b455db6 820 PWC_ERROR("Failed to allocate urb %d\n", i);
1da177e4
LT
821 ret = -ENOMEM;
822 break;
823 }
824 pdev->sbuf[i].urb = urb;
2b455db6 825 PWC_DEBUG_MEMORY("Allocated URB at 0x%p\n", urb);
1da177e4
LT
826 }
827 if (ret) {
828 /* De-allocate in reverse order */
444f4f91 829 while (i--) {
90b2625a 830 usb_free_urb(pdev->sbuf[i].urb);
1da177e4 831 pdev->sbuf[i].urb = NULL;
1da177e4
LT
832 }
833 return ret;
834 }
835
d56410e0 836 /* init URB structure */
1da177e4
LT
837 for (i = 0; i < MAX_ISO_BUFS; i++) {
838 urb = pdev->sbuf[i].urb;
839
840 urb->interval = 1; // devik
841 urb->dev = udev;
d56410e0 842 urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
1da177e4 843 urb->transfer_flags = URB_ISO_ASAP;
d56410e0
MCC
844 urb->transfer_buffer = pdev->sbuf[i].data;
845 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
846 urb->complete = pwc_isoc_handler;
847 urb->context = pdev;
1da177e4
LT
848 urb->start_frame = 0;
849 urb->number_of_packets = ISO_FRAMES_PER_DESC;
850 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
851 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
852 urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
853 }
854 }
855
856 /* link */
857 for (i = 0; i < MAX_ISO_BUFS; i++) {
858 ret = usb_submit_urb(pdev->sbuf[i].urb, GFP_KERNEL);
622d9f5d 859 if (ret) {
2b455db6 860 PWC_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
622d9f5d
HG
861 pdev->iso_init = 1;
862 pwc_isoc_cleanup(pdev);
863 return ret;
864 }
865 PWC_DEBUG_MEMORY("URB 0x%p submitted.\n", pdev->sbuf[i].urb);
1da177e4
LT
866 }
867
868 /* All is done... */
869 pdev->iso_init = 1;
2b455db6 870 PWC_DEBUG_OPEN("<< pwc_isoc_init()\n");
1da177e4
LT
871 return 0;
872}
873
0b67f5c5 874static void pwc_iso_stop(struct pwc_device *pdev)
1da177e4
LT
875{
876 int i;
877
1da177e4
LT
878 /* Unlinking ISOC buffers one by one */
879 for (i = 0; i < MAX_ISO_BUFS; i++) {
880 struct urb *urb;
881
882 urb = pdev->sbuf[i].urb;
5fa1247a 883 if (urb) {
0b67f5c5
ON
884 PWC_DEBUG_MEMORY("Unlinking URB %p\n", urb);
885 usb_kill_urb(urb);
886 }
887 }
888}
889
890static void pwc_iso_free(struct pwc_device *pdev)
891{
892 int i;
893
894 /* Freeing ISOC buffers one by one */
895 for (i = 0; i < MAX_ISO_BUFS; i++) {
896 struct urb *urb;
897
898 urb = pdev->sbuf[i].urb;
5fa1247a 899 if (urb) {
2b455db6 900 PWC_DEBUG_MEMORY("Freeing URB\n");
1da177e4
LT
901 usb_free_urb(urb);
902 pdev->sbuf[i].urb = NULL;
903 }
904 }
0b67f5c5
ON
905}
906
907void pwc_isoc_cleanup(struct pwc_device *pdev)
908{
909 PWC_DEBUG_OPEN(">> pwc_isoc_cleanup()\n");
c2464121 910
0b67f5c5
ON
911 if (pdev->iso_init == 0)
912 return;
913
914 pwc_iso_stop(pdev);
915 pwc_iso_free(pdev);
1da177e4
LT
916
917 /* Stop camera, but only if we are sure the camera is still there (unplug
d56410e0 918 is signalled by EPIPE)
1da177e4 919 */
c7d18867 920 if (pdev->error_status != EPIPE) {
2b455db6 921 PWC_DEBUG_OPEN("Setting alternate interface 0.\n");
1da177e4
LT
922 usb_set_interface(pdev->udev, 0, 0);
923 }
924
925 pdev->iso_init = 0;
2b455db6 926 PWC_DEBUG_OPEN("<< pwc_isoc_cleanup()\n");
1da177e4
LT
927}
928
2b455db6
LS
929/*********
930 * sysfs
931 *********/
54bd5b66 932static struct pwc_device *cd_to_pwc(struct device *cd)
2b455db6
LS
933{
934 struct video_device *vdev = to_video_device(cd);
935 return video_get_drvdata(vdev);
936}
937
54bd5b66
KS
938static ssize_t show_pan_tilt(struct device *class_dev,
939 struct device_attribute *attr, char *buf)
2b455db6
LS
940{
941 struct pwc_device *pdev = cd_to_pwc(class_dev);
942 return sprintf(buf, "%d %d\n", pdev->pan_angle, pdev->tilt_angle);
943}
944
54bd5b66
KS
945static ssize_t store_pan_tilt(struct device *class_dev,
946 struct device_attribute *attr,
947 const char *buf, size_t count)
2b455db6
LS
948{
949 struct pwc_device *pdev = cd_to_pwc(class_dev);
950 int pan, tilt;
951 int ret = -EINVAL;
952
953 if (strncmp(buf, "reset", 5) == 0)
954 ret = pwc_mpt_reset(pdev, 0x3);
955
956 else if (sscanf(buf, "%d %d", &pan, &tilt) > 0)
957 ret = pwc_mpt_set_angle(pdev, pan, tilt);
958
959 if (ret < 0)
960 return ret;
961 return strlen(buf);
962}
54bd5b66
KS
963static DEVICE_ATTR(pan_tilt, S_IRUGO | S_IWUSR, show_pan_tilt,
964 store_pan_tilt);
2b455db6 965
54bd5b66
KS
966static ssize_t show_snapshot_button_status(struct device *class_dev,
967 struct device_attribute *attr, char *buf)
2b455db6
LS
968{
969 struct pwc_device *pdev = cd_to_pwc(class_dev);
970 int status = pdev->snapshot_button_status;
971 pdev->snapshot_button_status = 0;
972 return sprintf(buf, "%d\n", status);
973}
974
54bd5b66
KS
975static DEVICE_ATTR(button, S_IRUGO | S_IWUSR, show_snapshot_button_status,
976 NULL);
2b455db6 977
9a7b2d1f 978static int pwc_create_sysfs_files(struct pwc_device *pdev)
2b455db6 979{
c12e3be0
JG
980 int rc;
981
9a7b2d1f 982 rc = device_create_file(&pdev->vdev.dev, &dev_attr_button);
c12e3be0
JG
983 if (rc)
984 goto err;
985 if (pdev->features & FEATURE_MOTOR_PANTILT) {
9a7b2d1f 986 rc = device_create_file(&pdev->vdev.dev, &dev_attr_pan_tilt);
89dec01b
DT
987 if (rc)
988 goto err_button;
c12e3be0
JG
989 }
990
991 return 0;
992
993err_button:
9a7b2d1f 994 device_remove_file(&pdev->vdev.dev, &dev_attr_button);
c12e3be0 995err:
f894dfd7 996 PWC_ERROR("Could not create sysfs files.\n");
c12e3be0 997 return rc;
2b455db6
LS
998}
999
9a7b2d1f 1000static void pwc_remove_sysfs_files(struct pwc_device *pdev)
2b455db6 1001{
2b455db6 1002 if (pdev->features & FEATURE_MOTOR_PANTILT)
9a7b2d1f
HG
1003 device_remove_file(&pdev->vdev.dev, &dev_attr_pan_tilt);
1004 device_remove_file(&pdev->vdev.dev, &dev_attr_button);
2b455db6
LS
1005}
1006
05ad3907 1007#ifdef CONFIG_USB_PWC_DEBUG
2b455db6
LS
1008static const char *pwc_sensor_type_to_string(unsigned int sensor_type)
1009{
1010 switch(sensor_type) {
1011 case 0x00:
1012 return "Hyundai CMOS sensor";
1013 case 0x20:
1014 return "Sony CCD sensor + TDA8787";
1015 case 0x2E:
1016 return "Sony CCD sensor + Exas 98L59";
1017 case 0x2F:
1018 return "Sony CCD sensor + ADI 9804";
1019 case 0x30:
1020 return "Sharp CCD sensor + TDA8787";
1021 case 0x3E:
1022 return "Sharp CCD sensor + Exas 98L59";
1023 case 0x3F:
1024 return "Sharp CCD sensor + ADI 9804";
1025 case 0x40:
1026 return "UPA 1021 sensor";
1027 case 0x100:
1028 return "VGA sensor";
1029 case 0x101:
1030 return "PAL MR sensor";
1031 default:
657de3cd 1032 return "unknown type of sensor";
2b455db6
LS
1033 }
1034}
1035#endif
1da177e4
LT
1036
1037/***************************************************************************/
1038/* Video4Linux functions */
1039
bec43661 1040static int pwc_video_open(struct file *file)
1da177e4 1041{
2b455db6 1042 int i, ret;
1da177e4
LT
1043 struct video_device *vdev = video_devdata(file);
1044 struct pwc_device *pdev;
1045
2b455db6 1046 PWC_DEBUG_OPEN(">> video_open called(vdev = 0x%p).\n", vdev);
d56410e0 1047
601e9444 1048 pdev = video_get_drvdata(vdev);
2b455db6
LS
1049 if (pdev->vopen) {
1050 PWC_DEBUG_OPEN("I'm busy, someone is using the device.\n");
1da177e4 1051 return -EBUSY;
2b455db6 1052 }
d56410e0 1053
5c8e2403 1054 pwc_construct(pdev); /* set min/max sizes correct */
1da177e4 1055 if (!pdev->usb_init) {
2b455db6 1056 PWC_DEBUG_OPEN("Doing first time initialization.\n");
1da177e4 1057 pdev->usb_init = 1;
d56410e0 1058
2b455db6
LS
1059 /* Query sensor type */
1060 ret = pwc_get_cmos_sensor(pdev, &i);
1061 if (ret >= 0)
1da177e4 1062 {
2b455db6 1063 PWC_DEBUG_OPEN("This %s camera is equipped with a %s (%d).\n",
9a7b2d1f 1064 pdev->vdev.name,
2b455db6 1065 pwc_sensor_type_to_string(i), i);
1da177e4
LT
1066 }
1067 }
1068
1069 /* Turn on camera */
1070 if (power_save) {
1071 i = pwc_camera_power(pdev, 1);
1072 if (i < 0)
2b455db6 1073 PWC_DEBUG_OPEN("Failed to restore power to the camera! (%d)\n", i);
1da177e4
LT
1074 }
1075 /* Set LED on/off time */
1076 if (pwc_set_leds(pdev, led_on, led_off) < 0)
2b455db6 1077 PWC_DEBUG_OPEN("Failed to set LED on/off time.\n");
d56410e0 1078
1da177e4
LT
1079
1080 /* So far, so good. Allocate memory. */
1081 i = pwc_allocate_buffers(pdev);
1082 if (i < 0) {
2b455db6
LS
1083 PWC_DEBUG_OPEN("Failed to allocate buffers memory.\n");
1084 pwc_free_buffers(pdev);
1da177e4
LT
1085 return i;
1086 }
d56410e0 1087
1da177e4
LT
1088 /* Reset buffers & parameters */
1089 pwc_reset_buffers(pdev);
2b455db6 1090 for (i = 0; i < pwc_mbufs; i++)
1da177e4
LT
1091 pdev->image_used[i] = 0;
1092 pdev->vframe_count = 0;
1093 pdev->vframes_dumped = 0;
1094 pdev->vframes_error = 0;
1095 pdev->visoc_errors = 0;
1096 pdev->error_status = 0;
1da177e4
LT
1097 pwc_construct(pdev); /* set min/max sizes correct */
1098
1099 /* Set some defaults */
1100 pdev->vsnapshot = 0;
1101
3751e288 1102 /* Set video size, first try the last used video size
1da177e4
LT
1103 (or the default one); if that fails try QCIF/10 or QSIF/10;
1104 it that fails too, give up.
1105 */
1106 i = pwc_set_video_mode(pdev, pwc_image_sizes[pdev->vsize].x, pwc_image_sizes[pdev->vsize].y, pdev->vframes, pdev->vcompression, 0);
1107 if (i) {
2b455db6
LS
1108 unsigned int default_resolution;
1109 PWC_DEBUG_OPEN("First attempt at set_video_mode failed.\n");
1110 if (pdev->type>= 730)
1111 default_resolution = PSZ_QSIF;
1da177e4 1112 else
2b455db6
LS
1113 default_resolution = PSZ_QCIF;
1114
1115 i = pwc_set_video_mode(pdev,
1116 pwc_image_sizes[default_resolution].x,
1117 pwc_image_sizes[default_resolution].y,
1118 10,
1119 pdev->vcompression,
1120 0);
1da177e4
LT
1121 }
1122 if (i) {
2b455db6
LS
1123 PWC_DEBUG_OPEN("Second attempt at set_video_mode failed.\n");
1124 pwc_free_buffers(pdev);
1da177e4
LT
1125 return i;
1126 }
d56410e0 1127
2b455db6
LS
1128 /* Initialize the webcam to sane value */
1129 pwc_set_brightness(pdev, 0x7fff);
1130 pwc_set_agc(pdev, 1, 0);
1131
1da177e4
LT
1132 pdev->vopen++;
1133 file->private_data = vdev;
2b455db6 1134 PWC_DEBUG_OPEN("<< video_open() returns 0.\n");
1da177e4
LT
1135 return 0;
1136}
1137
9a7b2d1f 1138static void pwc_video_release(struct video_device *vfd)
85237f20 1139{
9a7b2d1f
HG
1140 struct pwc_device *pdev = container_of(vfd, struct pwc_device, vdev);
1141 int hint;
e32a7ecc 1142
9a7b2d1f
HG
1143 /* search device_hint[] table if we occupy a slot, by any chance */
1144 for (hint = 0; hint < MAX_DEV_HINTS; hint++)
1145 if (device_hint[hint].pdev == pdev)
1146 device_hint[hint].pdev = NULL;
89dec01b
DT
1147
1148 kfree(pdev);
85237f20
ON
1149}
1150
1da177e4 1151/* Note that all cleanup is done in the reverse order as in _open */
bec43661 1152static int pwc_video_close(struct file *file)
1da177e4
LT
1153{
1154 struct video_device *vdev = file->private_data;
1155 struct pwc_device *pdev;
9a7b2d1f 1156 int i;
1da177e4 1157
2b455db6 1158 PWC_DEBUG_OPEN(">> video_close called(vdev = 0x%p).\n", vdev);
1da177e4 1159
601e9444 1160 pdev = video_get_drvdata(vdev);
1da177e4 1161 if (pdev->vopen == 0)
2b455db6 1162 PWC_DEBUG_MODULE("video_close() called on closed device?\n");
1da177e4
LT
1163
1164 /* Dump statistics, but only if a reasonable amount of frames were
1165 processed (to prevent endless log-entries in case of snap-shot
1166 programs)
1167 */
1168 if (pdev->vframe_count > 20)
2b455db6 1169 PWC_DEBUG_MODULE("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev->vframe_count, pdev->vframes_dumped, pdev->vframes_error);
1da177e4 1170
2b455db6
LS
1171 if (DEVICE_USE_CODEC1(pdev->type))
1172 pwc_dec1_exit();
1173 else
1174 pwc_dec23_exit();
1da177e4
LT
1175
1176 pwc_isoc_cleanup(pdev);
1177 pwc_free_buffers(pdev);
1178
1179 /* Turn off LEDS and power down camera, but only when not unplugged */
85237f20 1180 if (!pdev->unplugged) {
1da177e4
LT
1181 /* Turn LEDs off */
1182 if (pwc_set_leds(pdev, 0, 0) < 0)
2b455db6 1183 PWC_DEBUG_MODULE("Failed to set LED on/off time.\n");
1da177e4
LT
1184 if (power_save) {
1185 i = pwc_camera_power(pdev, 0);
1186 if (i < 0)
2b455db6 1187 PWC_ERROR("Failed to power down camera (%d)\n", i);
1da177e4 1188 }
85237f20 1189 pdev->vopen--;
7b9fbc3e 1190 PWC_DEBUG_OPEN("<< video_close() vopen=%d\n", pdev->vopen);
1da177e4 1191 }
85237f20 1192
1da177e4
LT
1193 return 0;
1194}
1195
1196/*
1197 * FIXME: what about two parallel reads ????
1198 * ANSWER: Not supported. You can't open the device more than once,
d56410e0
MCC
1199 despite what the V4L1 interface says. First, I don't see
1200 the need, second there's no mechanism of alerting the
1201 2nd/3rd/... process of events like changing image size.
1202 And I don't see the point of blocking that for the
1203 2nd/3rd/... process.
1204 In multi-threaded environments reading parallel from any
1205 device is tricky anyhow.
1da177e4
LT
1206 */
1207
2b455db6 1208static ssize_t pwc_video_read(struct file *file, char __user *buf,
1da177e4
LT
1209 size_t count, loff_t *ppos)
1210{
1211 struct video_device *vdev = file->private_data;
1212 struct pwc_device *pdev;
1213 int noblock = file->f_flags & O_NONBLOCK;
1214 DECLARE_WAITQUEUE(wait, current);
0b67f5c5 1215 int bytes_to_read, rv = 0;
2b455db6 1216 void *image_buffer_addr;
1da177e4 1217
2b455db6
LS
1218 PWC_DEBUG_READ("pwc_video_read(vdev=0x%p, buf=%p, count=%zd) called.\n",
1219 vdev, buf, count);
601e9444 1220 pdev = video_get_drvdata(vdev);
0b67f5c5 1221
0b67f5c5
ON
1222 if (pdev->error_status) {
1223 rv = -pdev->error_status; /* Something happened, report what. */
1224 goto err_out;
1225 }
1da177e4 1226
3751e288
HG
1227 /* Start the stream (if not already started) */
1228 rv = pwc_isoc_init(pdev);
1229 if (rv)
1230 goto err_out;
1231
1da177e4
LT
1232 /* In case we're doing partial reads, we don't have to wait for a frame */
1233 if (pdev->image_read_pos == 0) {
1234 /* Do wait queueing according to the (doc)book */
1235 add_wait_queue(&pdev->frameq, &wait);
1236 while (pdev->full_frames == NULL) {
1237 /* Check for unplugged/etc. here */
1238 if (pdev->error_status) {
1239 remove_wait_queue(&pdev->frameq, &wait);
1240 set_current_state(TASK_RUNNING);
0b67f5c5
ON
1241 rv = -pdev->error_status ;
1242 goto err_out;
1da177e4 1243 }
d56410e0
MCC
1244 if (noblock) {
1245 remove_wait_queue(&pdev->frameq, &wait);
1246 set_current_state(TASK_RUNNING);
0b67f5c5
ON
1247 rv = -EWOULDBLOCK;
1248 goto err_out;
d56410e0
MCC
1249 }
1250 if (signal_pending(current)) {
1251 remove_wait_queue(&pdev->frameq, &wait);
1252 set_current_state(TASK_RUNNING);
0b67f5c5
ON
1253 rv = -ERESTARTSYS;
1254 goto err_out;
d56410e0 1255 }
b577f962 1256 mutex_unlock(&pdev->modlock);
d56410e0
MCC
1257 schedule();
1258 set_current_state(TASK_INTERRUPTIBLE);
b577f962 1259 mutex_lock(&pdev->modlock);
1da177e4
LT
1260 }
1261 remove_wait_queue(&pdev->frameq, &wait);
1262 set_current_state(TASK_RUNNING);
d56410e0 1263
1da177e4 1264 /* Decompress and release frame */
c2464121
HG
1265 rv = pwc_handle_frame(pdev);
1266 if (rv)
0b67f5c5 1267 goto err_out;
1da177e4
LT
1268 }
1269
2b455db6 1270 PWC_DEBUG_READ("Copying data to user space.\n");
479567ce 1271 if (pdev->pixfmt != V4L2_PIX_FMT_YUV420)
2b455db6 1272 bytes_to_read = pdev->frame_size + sizeof(struct pwc_raw_frame);
1da177e4 1273 else
d56410e0 1274 bytes_to_read = pdev->view.size;
1da177e4
LT
1275
1276 /* copy bytes to user space; we allow for partial reads */
1277 if (count + pdev->image_read_pos > bytes_to_read)
1278 count = bytes_to_read - pdev->image_read_pos;
2b455db6
LS
1279 image_buffer_addr = pdev->image_data;
1280 image_buffer_addr += pdev->images[pdev->fill_image].offset;
1281 image_buffer_addr += pdev->image_read_pos;
0b67f5c5
ON
1282 if (copy_to_user(buf, image_buffer_addr, count)) {
1283 rv = -EFAULT;
1284 goto err_out;
1285 }
1da177e4
LT
1286 pdev->image_read_pos += count;
1287 if (pdev->image_read_pos >= bytes_to_read) { /* All data has been read */
1288 pdev->image_read_pos = 0;
1289 pwc_next_image(pdev);
1290 }
1291 return count;
0b67f5c5 1292err_out:
0b67f5c5 1293 return rv;
1da177e4
LT
1294}
1295
1296static unsigned int pwc_video_poll(struct file *file, poll_table *wait)
1297{
1298 struct video_device *vdev = file->private_data;
1299 struct pwc_device *pdev;
3751e288 1300 int ret;
1da177e4 1301
601e9444 1302 pdev = video_get_drvdata(vdev);
1da177e4 1303
3751e288 1304 /* Start the stream (if not already started) */
3751e288 1305 ret = pwc_isoc_init(pdev);
3751e288
HG
1306 if (ret)
1307 return ret;
1308
1da177e4
LT
1309 poll_wait(file, &pdev->frameq, wait);
1310 if (pdev->error_status)
1311 return POLLERR;
1312 if (pdev->full_frames != NULL) /* we have frames waiting */
1313 return (POLLIN | POLLRDNORM);
1314
1315 return 0;
1316}
1317
1da177e4
LT
1318static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma)
1319{
1320 struct video_device *vdev = file->private_data;
1321 struct pwc_device *pdev;
2b455db6
LS
1322 unsigned long start;
1323 unsigned long size;
1324 unsigned long page, pos = 0;
1325 int index;
d56410e0 1326
645635b0 1327 PWC_DEBUG_MEMORY(">> %s\n", __func__);
601e9444 1328 pdev = video_get_drvdata(vdev);
2b455db6
LS
1329 size = vma->vm_end - vma->vm_start;
1330 start = vma->vm_start;
d56410e0 1331
2b455db6
LS
1332 /* Find the idx buffer for this mapping */
1333 for (index = 0; index < pwc_mbufs; index++) {
1334 pos = pdev->images[index].offset;
1335 if ((pos>>PAGE_SHIFT) == vma->vm_pgoff)
1336 break;
1337 }
1338 if (index == MAX_IMAGES)
1339 return -EINVAL;
1340 if (index == 0) {
1341 /*
1342 * Special case for v4l1. In v4l1, we map only one big buffer,
1343 * but in v4l2 each buffer is mapped
1344 */
1345 unsigned long total_size;
1346 total_size = pwc_mbufs * pdev->len_per_image;
1347 if (size != pdev->len_per_image && size != total_size) {
1348 PWC_ERROR("Wrong size (%lu) needed to be len_per_image=%d or total_size=%lu\n",
1349 size, pdev->len_per_image, total_size);
1350 return -EINVAL;
1351 }
1352 } else if (size > pdev->len_per_image)
1353 return -EINVAL;
1354
1355 vma->vm_flags |= VM_IO; /* from 2.6.9-acX */
1da177e4 1356
2b455db6 1357 pos += (unsigned long)pdev->image_data;
1da177e4
LT
1358 while (size > 0) {
1359 page = vmalloc_to_pfn((void *)pos);
1360 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
1361 return -EAGAIN;
1da177e4
LT
1362 start += PAGE_SIZE;
1363 pos += PAGE_SIZE;
1364 if (size > PAGE_SIZE)
1365 size -= PAGE_SIZE;
1366 else
1367 size = 0;
1368 }
1da177e4
LT
1369 return 0;
1370}
1371
1372/***************************************************************************/
1373/* USB functions */
1374
1375/* This function gets called when a new device is plugged in or the usb core
1376 * is loaded.
1377 */
1378
1379static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
1380{
1381 struct usb_device *udev = interface_to_usbdev(intf);
1382 struct pwc_device *pdev = NULL;
1383 int vendor_id, product_id, type_id;
89dec01b 1384 int hint, rc;
1da177e4
LT
1385 int features = 0;
1386 int video_nr = -1; /* default: use next available device */
1387 char serial_number[30], *name;
1388
2b455db6
LS
1389 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
1390 product_id = le16_to_cpu(udev->descriptor.idProduct);
1391
1da177e4 1392 /* Check if we can handle this device */
2b455db6
LS
1393 PWC_DEBUG_PROBE("probe() called [%04X %04X], if %d\n",
1394 vendor_id, product_id,
1da177e4
LT
1395 intf->altsetting->desc.bInterfaceNumber);
1396
1397 /* the interfaces are probed one by one. We are only interested in the
1398 video interface (0) now.
1399 Interface 1 is the Audio Control, and interface 2 Audio itself.
1400 */
1401 if (intf->altsetting->desc.bInterfaceNumber > 0)
1402 return -ENODEV;
1403
1da177e4
LT
1404 if (vendor_id == 0x0471) {
1405 switch (product_id) {
1406 case 0x0302:
2b455db6 1407 PWC_INFO("Philips PCA645VC USB webcam detected.\n");
1da177e4
LT
1408 name = "Philips 645 webcam";
1409 type_id = 645;
1410 break;
1411 case 0x0303:
2b455db6 1412 PWC_INFO("Philips PCA646VC USB webcam detected.\n");
1da177e4
LT
1413 name = "Philips 646 webcam";
1414 type_id = 646;
1415 break;
1416 case 0x0304:
2b455db6 1417 PWC_INFO("Askey VC010 type 2 USB webcam detected.\n");
1da177e4
LT
1418 name = "Askey VC010 webcam";
1419 type_id = 646;
1420 break;
1421 case 0x0307:
2b455db6 1422 PWC_INFO("Philips PCVC675K (Vesta) USB webcam detected.\n");
1da177e4
LT
1423 name = "Philips 675 webcam";
1424 type_id = 675;
1425 break;
1426 case 0x0308:
2b455db6 1427 PWC_INFO("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
1da177e4
LT
1428 name = "Philips 680 webcam";
1429 type_id = 680;
1430 break;
1431 case 0x030C:
2b455db6 1432 PWC_INFO("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
1da177e4
LT
1433 name = "Philips 690 webcam";
1434 type_id = 690;
1435 break;
1436 case 0x0310:
2b455db6 1437 PWC_INFO("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
1da177e4
LT
1438 name = "Philips 730 webcam";
1439 type_id = 730;
1440 break;
1441 case 0x0311:
2b455db6 1442 PWC_INFO("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
1da177e4
LT
1443 name = "Philips 740 webcam";
1444 type_id = 740;
1445 break;
1446 case 0x0312:
2b455db6 1447 PWC_INFO("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
1da177e4
LT
1448 name = "Philips 750 webcam";
1449 type_id = 750;
1450 break;
1451 case 0x0313:
2b455db6 1452 PWC_INFO("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
1da177e4
LT
1453 name = "Philips 720K/40 webcam";
1454 type_id = 720;
1455 break;
2b455db6
LS
1456 case 0x0329:
1457 PWC_INFO("Philips SPC 900NC USB webcam detected.\n");
1458 name = "Philips SPC 900NC webcam";
9ee6d78c 1459 type_id = 740;
2b455db6 1460 break;
1da177e4
LT
1461 default:
1462 return -ENODEV;
1463 break;
1464 }
1465 }
1466 else if (vendor_id == 0x069A) {
1467 switch(product_id) {
1468 case 0x0001:
2b455db6 1469 PWC_INFO("Askey VC010 type 1 USB webcam detected.\n");
1da177e4
LT
1470 name = "Askey VC010 webcam";
1471 type_id = 645;
1472 break;
1473 default:
1474 return -ENODEV;
1475 break;
1476 }
1477 }
1478 else if (vendor_id == 0x046d) {
1479 switch(product_id) {
1480 case 0x08b0:
2b455db6 1481 PWC_INFO("Logitech QuickCam Pro 3000 USB webcam detected.\n");
1da177e4
LT
1482 name = "Logitech QuickCam Pro 3000";
1483 type_id = 740; /* CCD sensor */
1484 break;
1485 case 0x08b1:
2b455db6 1486 PWC_INFO("Logitech QuickCam Notebook Pro USB webcam detected.\n");
1da177e4
LT
1487 name = "Logitech QuickCam Notebook Pro";
1488 type_id = 740; /* CCD sensor */
1489 break;
1490 case 0x08b2:
2b455db6 1491 PWC_INFO("Logitech QuickCam 4000 Pro USB webcam detected.\n");
1da177e4
LT
1492 name = "Logitech QuickCam Pro 4000";
1493 type_id = 740; /* CCD sensor */
1494 break;
1495 case 0x08b3:
2b455db6 1496 PWC_INFO("Logitech QuickCam Zoom USB webcam detected.\n");
1da177e4
LT
1497 name = "Logitech QuickCam Zoom";
1498 type_id = 740; /* CCD sensor */
1499 break;
1500 case 0x08B4:
2b455db6 1501 PWC_INFO("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
1da177e4
LT
1502 name = "Logitech QuickCam Zoom";
1503 type_id = 740; /* CCD sensor */
2b455db6 1504 power_save = 1;
1da177e4
LT
1505 break;
1506 case 0x08b5:
2b455db6 1507 PWC_INFO("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
1da177e4
LT
1508 name = "Logitech QuickCam Orbit";
1509 type_id = 740; /* CCD sensor */
1510 features |= FEATURE_MOTOR_PANTILT;
1511 break;
1512 case 0x08b6:
a63e157f
JT
1513 PWC_INFO("Logitech/Cisco VT Camera webcam detected.\n");
1514 name = "Cisco VT Camera";
1515 type_id = 740; /* CCD sensor */
1516 break;
1da177e4 1517 case 0x08b7:
6b1ce3c1
MCC
1518 PWC_INFO("Logitech ViewPort AV 100 webcam detected.\n");
1519 name = "Logitech ViewPort AV 100";
1520 type_id = 740; /* CCD sensor */
1521 break;
1522 case 0x08b8: /* Where this released? */
2b455db6 1523 PWC_INFO("Logitech QuickCam detected (reserved ID).\n");
1da177e4
LT
1524 name = "Logitech QuickCam (res.)";
1525 type_id = 730; /* Assuming CMOS */
1526 break;
d56410e0 1527 default:
1da177e4 1528 return -ENODEV;
d56410e0
MCC
1529 break;
1530 }
1531 }
1da177e4
LT
1532 else if (vendor_id == 0x055d) {
1533 /* I don't know the difference between the C10 and the C30;
1534 I suppose the difference is the sensor, but both cameras
1535 work equally well with a type_id of 675
1536 */
1537 switch(product_id) {
1538 case 0x9000:
2b455db6 1539 PWC_INFO("Samsung MPC-C10 USB webcam detected.\n");
1da177e4
LT
1540 name = "Samsung MPC-C10";
1541 type_id = 675;
1542 break;
1543 case 0x9001:
2b455db6 1544 PWC_INFO("Samsung MPC-C30 USB webcam detected.\n");
1da177e4
LT
1545 name = "Samsung MPC-C30";
1546 type_id = 675;
1547 break;
2b455db6
LS
1548 case 0x9002:
1549 PWC_INFO("Samsung SNC-35E (v3.0) USB webcam detected.\n");
1550 name = "Samsung MPC-C30";
1551 type_id = 740;
1552 break;
1da177e4
LT
1553 default:
1554 return -ENODEV;
1555 break;
1556 }
1557 }
1558 else if (vendor_id == 0x041e) {
1559 switch(product_id) {
1560 case 0x400c:
2b455db6 1561 PWC_INFO("Creative Labs Webcam 5 detected.\n");
1da177e4
LT
1562 name = "Creative Labs Webcam 5";
1563 type_id = 730;
1564 break;
1565 case 0x4011:
2b455db6 1566 PWC_INFO("Creative Labs Webcam Pro Ex detected.\n");
1da177e4
LT
1567 name = "Creative Labs Webcam Pro Ex";
1568 type_id = 740;
1569 break;
1570 default:
1571 return -ENODEV;
1572 break;
1573 }
1574 }
1575 else if (vendor_id == 0x04cc) {
1576 switch(product_id) {
1577 case 0x8116:
2b455db6 1578 PWC_INFO("Sotec Afina Eye USB webcam detected.\n");
1da177e4
LT
1579 name = "Sotec Afina Eye";
1580 type_id = 730;
1581 break;
1582 default:
1583 return -ENODEV;
1584 break;
1585 }
1586 }
1587 else if (vendor_id == 0x06be) {
1588 switch(product_id) {
1589 case 0x8116:
1590 /* This is essentially the same cam as the Sotec Afina Eye */
2b455db6 1591 PWC_INFO("AME Co. Afina Eye USB webcam detected.\n");
1da177e4
LT
1592 name = "AME Co. Afina Eye";
1593 type_id = 750;
1594 break;
1595 default:
1596 return -ENODEV;
1597 break;
1598 }
d56410e0 1599
1da177e4
LT
1600 }
1601 else if (vendor_id == 0x0d81) {
1602 switch(product_id) {
1603 case 0x1900:
2b455db6 1604 PWC_INFO("Visionite VCS-UC300 USB webcam detected.\n");
1da177e4
LT
1605 name = "Visionite VCS-UC300";
1606 type_id = 740; /* CCD sensor */
1607 break;
1608 case 0x1910:
2b455db6 1609 PWC_INFO("Visionite VCS-UM100 USB webcam detected.\n");
1da177e4
LT
1610 name = "Visionite VCS-UM100";
1611 type_id = 730; /* CMOS sensor */
1612 break;
1613 default:
1614 return -ENODEV;
1615 break;
1616 }
1617 }
d56410e0 1618 else
1da177e4
LT
1619 return -ENODEV; /* Not any of the know types; but the list keeps growing. */
1620
1621 memset(serial_number, 0, 30);
1622 usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
2b455db6 1623 PWC_DEBUG_PROBE("Device serial number is %s\n", serial_number);
1da177e4
LT
1624
1625 if (udev->descriptor.bNumConfigurations > 1)
2b455db6 1626 PWC_WARNING("Warning: more than 1 configuration available.\n");
1da177e4
LT
1627
1628 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
80b6ca48 1629 pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
1da177e4 1630 if (pdev == NULL) {
2b455db6 1631 PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
1da177e4
LT
1632 return -ENOMEM;
1633 }
1da177e4
LT
1634 pdev->type = type_id;
1635 pdev->vsize = default_size;
1636 pdev->vframes = default_fps;
1637 strcpy(pdev->serial, serial_number);
1638 pdev->features = features;
89dec01b 1639 if (vendor_id == 0x046D && product_id == 0x08B5) {
1da177e4 1640 /* Logitech QuickCam Orbit
d56410e0
MCC
1641 The ranges have been determined experimentally; they may differ from cam to cam.
1642 Also, the exact ranges left-right and up-down are different for my cam
1643 */
1da177e4
LT
1644 pdev->angle_range.pan_min = -7000;
1645 pdev->angle_range.pan_max = 7000;
1646 pdev->angle_range.tilt_min = -3000;
1647 pdev->angle_range.tilt_max = 2500;
1648 }
1649
b9378fdb 1650 mutex_init(&pdev->modlock);
1da177e4
LT
1651 spin_lock_init(&pdev->ptrlock);
1652
1653 pdev->udev = udev;
1654 init_waitqueue_head(&pdev->frameq);
1655 pdev->vcompression = pwc_preferred_compression;
1656
9a7b2d1f
HG
1657 /* Init video_device structure */
1658 memcpy(&pdev->vdev, &pwc_template, sizeof(pwc_template));
1659 pdev->vdev.parent = &intf->dev;
1660 pdev->vdev.lock = &pdev->modlock;
1661 strcpy(pdev->vdev.name, name);
1662 video_set_drvdata(&pdev->vdev, pdev);
1da177e4
LT
1663
1664 pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
2b455db6 1665 PWC_DEBUG_PROBE("Release: %04x\n", pdev->release);
1da177e4
LT
1666
1667 /* Now search device_hint[] table for a match, so we can hint a node number. */
1668 for (hint = 0; hint < MAX_DEV_HINTS; hint++) {
1669 if (((device_hint[hint].type == -1) || (device_hint[hint].type == pdev->type)) &&
1670 (device_hint[hint].pdev == NULL)) {
1671 /* so far, so good... try serial number */
1672 if ((device_hint[hint].serial_number[0] == '*') || !strcmp(device_hint[hint].serial_number, serial_number)) {
657de3cd
TP
1673 /* match! */
1674 video_nr = device_hint[hint].device_node;
1675 PWC_DEBUG_PROBE("Found hint, will try to register as /dev/video%d\n", video_nr);
1676 break;
1da177e4
LT
1677 }
1678 }
1679 }
1680
1da177e4 1681 /* occupy slot */
d56410e0 1682 if (hint < MAX_DEV_HINTS)
1da177e4
LT
1683 device_hint[hint].pdev = pdev;
1684
2b455db6 1685 PWC_DEBUG_PROBE("probe() function returning struct at 0x%p.\n", pdev);
89dec01b 1686 usb_set_intfdata(intf, pdev);
2b455db6
LS
1687
1688 /* Set the leds off */
1689 pwc_set_leds(pdev, 0, 0);
1690 pwc_camera_power(pdev, 0);
1691
9a7b2d1f 1692 rc = video_register_device(&pdev->vdev, VFL_TYPE_GRABBER, video_nr);
479567ce
HV
1693 if (rc < 0) {
1694 PWC_ERROR("Failed to register as video device (%d).\n", rc);
9a7b2d1f 1695 goto err_free_mem;
479567ce 1696 }
9a7b2d1f 1697 rc = pwc_create_sysfs_files(pdev);
479567ce
HV
1698 if (rc)
1699 goto err_video_unreg;
1700
9a7b2d1f 1701 PWC_INFO("Registered as %s.\n", video_device_node_name(&pdev->vdev));
479567ce 1702
e32a7ecc
NPT
1703#ifdef CONFIG_USB_PWC_INPUT_EVDEV
1704 /* register webcam snapshot button input device */
1705 pdev->button_dev = input_allocate_device();
1706 if (!pdev->button_dev) {
1707 PWC_ERROR("Err, insufficient memory for webcam snapshot button device.");
89dec01b 1708 rc = -ENOMEM;
9a7b2d1f 1709 pwc_remove_sysfs_files(pdev);
89dec01b 1710 goto err_video_unreg;
e32a7ecc
NPT
1711 }
1712
89dec01b
DT
1713 usb_make_path(udev, pdev->button_phys, sizeof(pdev->button_phys));
1714 strlcat(pdev->button_phys, "/input0", sizeof(pdev->button_phys));
1715
e32a7ecc 1716 pdev->button_dev->name = "PWC snapshot button";
89dec01b 1717 pdev->button_dev->phys = pdev->button_phys;
e32a7ecc
NPT
1718 usb_to_input_id(pdev->udev, &pdev->button_dev->id);
1719 pdev->button_dev->dev.parent = &pdev->udev->dev;
1720 pdev->button_dev->evbit[0] = BIT_MASK(EV_KEY);
bcd3e4b3 1721 pdev->button_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
e32a7ecc
NPT
1722
1723 rc = input_register_device(pdev->button_dev);
1724 if (rc) {
1725 input_free_device(pdev->button_dev);
e32a7ecc 1726 pdev->button_dev = NULL;
9a7b2d1f 1727 pwc_remove_sysfs_files(pdev);
89dec01b 1728 goto err_video_unreg;
e32a7ecc
NPT
1729 }
1730#endif
1731
1da177e4 1732 return 0;
c12e3be0 1733
89dec01b 1734err_video_unreg:
c12e3be0
JG
1735 if (hint < MAX_DEV_HINTS)
1736 device_hint[hint].pdev = NULL;
9a7b2d1f 1737 video_unregister_device(&pdev->vdev);
89dec01b
DT
1738err_free_mem:
1739 kfree(pdev);
c12e3be0 1740 return rc;
1da177e4
LT
1741}
1742
89dec01b 1743/* The user yanked out the cable... */
1da177e4
LT
1744static void usb_pwc_disconnect(struct usb_interface *intf)
1745{
9a7b2d1f 1746 struct pwc_device *pdev = usb_get_intfdata(intf);
1da177e4 1747
7074f407 1748 mutex_lock(&pdev->modlock);
1da177e4 1749 usb_set_intfdata (intf, NULL);
d56410e0 1750
1da177e4 1751 /* We got unplugged; this is signalled by an EPIPE error code */
9a7b2d1f
HG
1752 pdev->error_status = EPIPE;
1753 pdev->unplugged = 1;
1da177e4
LT
1754
1755 /* Alert waiting processes */
1756 wake_up_interruptible(&pdev->frameq);
1da177e4 1757
9a7b2d1f
HG
1758 /* No need to keep the urbs around after disconnection */
1759 pwc_isoc_cleanup(pdev);
1da177e4 1760
7074f407 1761 mutex_unlock(&pdev->modlock);
9a7b2d1f
HG
1762
1763 pwc_remove_sysfs_files(pdev);
1764 video_unregister_device(&pdev->vdev);
1765
1766#ifdef CONFIG_USB_PWC_INPUT_EVDEV
1767 if (pdev->button_dev)
1768 input_unregister_device(pdev->button_dev);
1769#endif
1da177e4
LT
1770}
1771
1da177e4 1772
d56410e0
MCC
1773/*
1774 * Initialization code & module stuff
1da177e4
LT
1775 */
1776
2b455db6
LS
1777static char *size;
1778static int fps;
1779static int fbufs;
1780static int mbufs;
1da177e4
LT
1781static int compression = -1;
1782static int leds[2] = { -1, -1 };
64a6f950 1783static unsigned int leds_nargs;
2b455db6 1784static char *dev_hint[MAX_DEV_HINTS];
64a6f950 1785static unsigned int dev_hint_nargs;
2b455db6
LS
1786
1787module_param(size, charp, 0444);
1788module_param(fps, int, 0444);
1789module_param(fbufs, int, 0444);
1790module_param(mbufs, int, 0444);
05ad3907 1791#ifdef CONFIG_USB_PWC_DEBUG
2b455db6
LS
1792module_param_named(trace, pwc_trace, int, 0644);
1793#endif
1794module_param(power_save, int, 0444);
1795module_param(compression, int, 0444);
1796module_param_array(leds, int, &leds_nargs, 0444);
1797module_param_array(dev_hint, charp, &dev_hint_nargs, 0444);
1da177e4 1798
1da177e4 1799MODULE_PARM_DESC(size, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
1da177e4 1800MODULE_PARM_DESC(fps, "Initial frames per second. Varies with model, useful range 5-30");
1da177e4 1801MODULE_PARM_DESC(fbufs, "Number of internal frame buffers to reserve");
1da177e4 1802MODULE_PARM_DESC(mbufs, "Number of external (mmap()ed) image buffers");
4315c414 1803#ifdef CONFIG_USB_PWC_DEBUG
1da177e4 1804MODULE_PARM_DESC(trace, "For debugging purposes");
4315c414 1805#endif
1da177e4 1806MODULE_PARM_DESC(power_save, "Turn power save feature in camera on or off");
1da177e4 1807MODULE_PARM_DESC(compression, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
1da177e4 1808MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
1da177e4
LT
1809MODULE_PARM_DESC(dev_hint, "Device node hints");
1810
1811MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
1812MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
1813MODULE_LICENSE("GPL");
2b455db6
LS
1814MODULE_ALIAS("pwcx");
1815MODULE_VERSION( PWC_VERSION );
1da177e4
LT
1816
1817static int __init usb_pwc_init(void)
1818{
1819 int i, sz;
1820 char *sizenames[PSZ_MAX] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };
1821
2b455db6
LS
1822 PWC_INFO("Philips webcam module version " PWC_VERSION " loaded.\n");
1823 PWC_INFO("Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.\n");
1824 PWC_INFO("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
1825 PWC_INFO("the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
1da177e4
LT
1826
1827 if (fps) {
1828 if (fps < 4 || fps > 30) {
2b455db6 1829 PWC_ERROR("Framerate out of bounds (4-30).\n");
1da177e4
LT
1830 return -EINVAL;
1831 }
1832 default_fps = fps;
2b455db6 1833 PWC_DEBUG_MODULE("Default framerate set to %d.\n", default_fps);
1da177e4
LT
1834 }
1835
2b455db6 1836 if (size) {
1da177e4
LT
1837 /* string; try matching with array */
1838 for (sz = 0; sz < PSZ_MAX; sz++) {
1839 if (!strcmp(sizenames[sz], size)) { /* Found! */
1840 default_size = sz;
1841 break;
1842 }
1843 }
1844 if (sz == PSZ_MAX) {
2b455db6 1845 PWC_ERROR("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
1da177e4
LT
1846 return -EINVAL;
1847 }
2b455db6 1848 PWC_DEBUG_MODULE("Default image size set to %s [%dx%d].\n", sizenames[default_size], pwc_image_sizes[default_size].x, pwc_image_sizes[default_size].y);
1da177e4
LT
1849 }
1850 if (mbufs) {
1851 if (mbufs < 1 || mbufs > MAX_IMAGES) {
2b455db6 1852 PWC_ERROR("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES);
1da177e4
LT
1853 return -EINVAL;
1854 }
2b455db6
LS
1855 pwc_mbufs = mbufs;
1856 PWC_DEBUG_MODULE("Number of image buffers set to %d.\n", pwc_mbufs);
1da177e4
LT
1857 }
1858 if (fbufs) {
1859 if (fbufs < 2 || fbufs > MAX_FRAMES) {
2b455db6 1860 PWC_ERROR("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES);
1da177e4
LT
1861 return -EINVAL;
1862 }
1863 default_fbufs = fbufs;
2b455db6 1864 PWC_DEBUG_MODULE("Number of frame buffers set to %d.\n", default_fbufs);
1da177e4 1865 }
05ad3907 1866#ifdef CONFIG_USB_PWC_DEBUG
2b455db6
LS
1867 if (pwc_trace >= 0) {
1868 PWC_DEBUG_MODULE("Trace options: 0x%04x\n", pwc_trace);
1da177e4 1869 }
2b455db6 1870#endif
1da177e4
LT
1871 if (compression >= 0) {
1872 if (compression > 3) {
2b455db6 1873 PWC_ERROR("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
1da177e4
LT
1874 return -EINVAL;
1875 }
1876 pwc_preferred_compression = compression;
2b455db6 1877 PWC_DEBUG_MODULE("Preferred compression set to %d.\n", pwc_preferred_compression);
1da177e4
LT
1878 }
1879 if (power_save)
2b455db6 1880 PWC_DEBUG_MODULE("Enabling power save on open/close.\n");
1da177e4
LT
1881 if (leds[0] >= 0)
1882 led_on = leds[0];
1883 if (leds[1] >= 0)
1884 led_off = leds[1];
1885
093cf723 1886 /* Big device node whoopla. Basically, it allows you to assign a
1da177e4
LT
1887 device node (/dev/videoX) to a camera, based on its type
1888 & serial number. The format is [type[.serialnumber]:]node.
1889
1890 Any camera that isn't matched by these rules gets the next
1891 available free device node.
1892 */
1893 for (i = 0; i < MAX_DEV_HINTS; i++) {
1894 char *s, *colon, *dot;
1895
1896 /* This loop also initializes the array */
1897 device_hint[i].pdev = NULL;
1898 s = dev_hint[i];
1899 if (s != NULL && *s != '\0') {
1900 device_hint[i].type = -1; /* wildcard */
1901 strcpy(device_hint[i].serial_number, "*");
1902
1903 /* parse string: chop at ':' & '/' */
1904 colon = dot = s;
1905 while (*colon != '\0' && *colon != ':')
1906 colon++;
1907 while (*dot != '\0' && *dot != '.')
1908 dot++;
1909 /* Few sanity checks */
1910 if (*dot != '\0' && dot > colon) {
2b455db6 1911 PWC_ERROR("Malformed camera hint: the colon must be after the dot.\n");
1da177e4
LT
1912 return -EINVAL;
1913 }
1914
1915 if (*colon == '\0') {
1916 /* No colon */
1917 if (*dot != '\0') {
2b455db6 1918 PWC_ERROR("Malformed camera hint: no colon + device node given.\n");
1da177e4
LT
1919 return -EINVAL;
1920 }
1921 else {
1922 /* No type or serial number specified, just a number. */
2d8d7762
AS
1923 device_hint[i].device_node =
1924 simple_strtol(s, NULL, 10);
1da177e4
LT
1925 }
1926 }
1927 else {
1928 /* There's a colon, so we have at least a type and a device node */
2d8d7762
AS
1929 device_hint[i].type =
1930 simple_strtol(s, NULL, 10);
1931 device_hint[i].device_node =
1932 simple_strtol(colon + 1, NULL, 10);
1da177e4
LT
1933 if (*dot != '\0') {
1934 /* There's a serial number as well */
1935 int k;
d56410e0 1936
1da177e4
LT
1937 dot++;
1938 k = 0;
1939 while (*dot != ':' && k < 29) {
1940 device_hint[i].serial_number[k++] = *dot;
1941 dot++;
1942 }
1943 device_hint[i].serial_number[k] = '\0';
1944 }
1945 }
2b455db6
LS
1946 PWC_TRACE("device_hint[%d]:\n", i);
1947 PWC_TRACE(" type : %d\n", device_hint[i].type);
1948 PWC_TRACE(" serial# : %s\n", device_hint[i].serial_number);
1949 PWC_TRACE(" node : %d\n", device_hint[i].device_node);
1da177e4
LT
1950 }
1951 else
1952 device_hint[i].type = 0; /* not filled */
1953 } /* ..for MAX_DEV_HINTS */
1954
2b455db6 1955 PWC_DEBUG_PROBE("Registering driver at address 0x%p.\n", &pwc_driver);
1da177e4
LT
1956 return usb_register(&pwc_driver);
1957}
1958
1959static void __exit usb_pwc_exit(void)
1960{
2b455db6 1961 PWC_DEBUG_MODULE("Deregistering driver.\n");
1da177e4 1962 usb_deregister(&pwc_driver);
2b455db6 1963 PWC_INFO("Philips webcam module removed.\n");
1da177e4
LT
1964}
1965
1966module_init(usb_pwc_init);
1967module_exit(usb_pwc_exit);
1968
2b455db6 1969/* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */