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