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