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