diff options
Diffstat (limited to 'drivers/media/video/stk-webcam.c')
-rw-r--r-- | drivers/media/video/stk-webcam.c | 1465 |
1 files changed, 1465 insertions, 0 deletions
diff --git a/drivers/media/video/stk-webcam.c b/drivers/media/video/stk-webcam.c new file mode 100644 index 000000000000..d37e5e2594b4 --- /dev/null +++ b/drivers/media/video/stk-webcam.c | |||
@@ -0,0 +1,1465 @@ | |||
1 | /* | ||
2 | * stk-webcam.c : Driver for Syntek 1125 USB webcam controller | ||
3 | * | ||
4 | * Copyright (C) 2006 Nicolas VIVIEN | ||
5 | * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com> | ||
6 | * | ||
7 | * Some parts are inspired from cafe_ccic.c | ||
8 | * Copyright 2006-2007 Jonathan Corbet | ||
9 | * | ||
10 | * This program is free software; you can redistribute it and/or modify | ||
11 | * it under the terms of the GNU General Public License as published by | ||
12 | * the Free Software Foundation; either version 2 of the License, or | ||
13 | * any later version. | ||
14 | * | ||
15 | * This program is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | * GNU General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU General Public License | ||
21 | * along with this program; if not, write to the Free Software | ||
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
23 | */ | ||
24 | |||
25 | #include <linux/module.h> | ||
26 | #include <linux/init.h> | ||
27 | #include <linux/kernel.h> | ||
28 | #include <linux/errno.h> | ||
29 | #include <linux/slab.h> | ||
30 | #include <linux/kref.h> | ||
31 | |||
32 | #include <linux/usb.h> | ||
33 | #include <linux/vmalloc.h> | ||
34 | #include <linux/videodev2.h> | ||
35 | #include <media/v4l2-common.h> | ||
36 | |||
37 | #include "stk-webcam.h" | ||
38 | |||
39 | |||
40 | static int hflip = 1; | ||
41 | module_param(hflip, bool, 0444); | ||
42 | MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1"); | ||
43 | |||
44 | static int vflip = 1; | ||
45 | module_param(vflip, bool, 0444); | ||
46 | MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1"); | ||
47 | |||
48 | static int debug; | ||
49 | module_param(debug, int, 0444); | ||
50 | MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0"); | ||
51 | |||
52 | MODULE_LICENSE("GPL"); | ||
53 | MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN"); | ||
54 | MODULE_DESCRIPTION("Syntek DC1125 webcam driver"); | ||
55 | |||
56 | |||
57 | |||
58 | /* Some cameras have audio interfaces, we aren't interested in those */ | ||
59 | static struct usb_device_id stkwebcam_table[] = { | ||
60 | { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) }, | ||
61 | { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) }, | ||
62 | { } | ||
63 | }; | ||
64 | MODULE_DEVICE_TABLE(usb, stkwebcam_table); | ||
65 | |||
66 | void stk_camera_cleanup(struct kref *kref) | ||
67 | { | ||
68 | struct stk_camera *dev = to_stk_camera(kref); | ||
69 | |||
70 | STK_INFO("Syntek USB2.0 Camera release resources" | ||
71 | " video device /dev/video%d\n", dev->vdev.minor); | ||
72 | video_unregister_device(&dev->vdev); | ||
73 | dev->vdev.priv = NULL; | ||
74 | |||
75 | if (dev->sio_bufs != NULL || dev->isobufs != NULL) | ||
76 | STK_ERROR("We are leaking memory\n"); | ||
77 | usb_put_intf(dev->interface); | ||
78 | kfree(dev); | ||
79 | } | ||
80 | |||
81 | |||
82 | /* | ||
83 | * Basic stuff | ||
84 | */ | ||
85 | int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value) | ||
86 | { | ||
87 | struct usb_device *udev = dev->udev; | ||
88 | int ret; | ||
89 | |||
90 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | ||
91 | 0x01, | ||
92 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | ||
93 | value, | ||
94 | index, | ||
95 | NULL, | ||
96 | 0, | ||
97 | 500); | ||
98 | if (ret < 0) | ||
99 | return ret; | ||
100 | else | ||
101 | return 0; | ||
102 | } | ||
103 | |||
104 | int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value) | ||
105 | { | ||
106 | struct usb_device *udev = dev->udev; | ||
107 | int ret; | ||
108 | |||
109 | ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), | ||
110 | 0x00, | ||
111 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | ||
112 | 0x00, | ||
113 | index, | ||
114 | (u8 *) value, | ||
115 | sizeof(u8), | ||
116 | 500); | ||
117 | if (ret < 0) | ||
118 | return ret; | ||
119 | else | ||
120 | return 0; | ||
121 | } | ||
122 | |||
123 | static int stk_start_stream(struct stk_camera *dev) | ||
124 | { | ||
125 | int value; | ||
126 | int i, ret; | ||
127 | int value_116, value_117; | ||
128 | |||
129 | if (!is_present(dev)) | ||
130 | return -ENODEV; | ||
131 | if (!is_memallocd(dev) || !is_initialised(dev)) { | ||
132 | STK_ERROR("FIXME: Buffers are not allocated\n"); | ||
133 | return -EFAULT; | ||
134 | } | ||
135 | ret = usb_set_interface(dev->udev, 0, 5); | ||
136 | |||
137 | if (ret < 0) | ||
138 | STK_ERROR("usb_set_interface failed !\n"); | ||
139 | if (stk_sensor_wakeup(dev)) | ||
140 | STK_ERROR("error awaking the sensor\n"); | ||
141 | |||
142 | stk_camera_read_reg(dev, 0x0116, &value_116); | ||
143 | stk_camera_read_reg(dev, 0x0117, &value_117); | ||
144 | |||
145 | stk_camera_write_reg(dev, 0x0116, 0x0000); | ||
146 | stk_camera_write_reg(dev, 0x0117, 0x0000); | ||
147 | |||
148 | stk_camera_read_reg(dev, 0x0100, &value); | ||
149 | stk_camera_write_reg(dev, 0x0100, value | 0x80); | ||
150 | |||
151 | stk_camera_write_reg(dev, 0x0116, value_116); | ||
152 | stk_camera_write_reg(dev, 0x0117, value_117); | ||
153 | for (i = 0; i < MAX_ISO_BUFS; i++) { | ||
154 | if (dev->isobufs[i].urb) { | ||
155 | ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL); | ||
156 | atomic_inc(&dev->urbs_used); | ||
157 | if (ret) | ||
158 | return ret; | ||
159 | } | ||
160 | } | ||
161 | set_streaming(dev); | ||
162 | return 0; | ||
163 | } | ||
164 | |||
165 | static int stk_stop_stream(struct stk_camera *dev) | ||
166 | { | ||
167 | int value; | ||
168 | int i; | ||
169 | if (is_present(dev)) { | ||
170 | stk_camera_read_reg(dev, 0x0100, &value); | ||
171 | stk_camera_write_reg(dev, 0x0100, value & ~0x80); | ||
172 | if (dev->isobufs != NULL) { | ||
173 | for (i = 0; i < MAX_ISO_BUFS; i++) { | ||
174 | if (dev->isobufs[i].urb) | ||
175 | usb_kill_urb(dev->isobufs[i].urb); | ||
176 | } | ||
177 | } | ||
178 | unset_streaming(dev); | ||
179 | |||
180 | if (usb_set_interface(dev->udev, 0, 0)) | ||
181 | STK_ERROR("usb_set_interface failed !\n"); | ||
182 | if (stk_sensor_sleep(dev)) | ||
183 | STK_ERROR("error suspending the sensor\n"); | ||
184 | } | ||
185 | return 0; | ||
186 | } | ||
187 | |||
188 | /* | ||
189 | * This seems to be the shortest init sequence we | ||
190 | * must do in order to find the sensor | ||
191 | * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor | ||
192 | * is also reset. Maybe powers down it? | ||
193 | * Rest of values don't make a difference | ||
194 | */ | ||
195 | |||
196 | static struct regval stk1125_initvals[] = { | ||
197 | /*TODO: What means this sequence? */ | ||
198 | {0x0000, 0x24}, | ||
199 | {0x0100, 0x21}, | ||
200 | {0x0002, 0x68}, | ||
201 | {0x0003, 0x80}, | ||
202 | {0x0005, 0x00}, | ||
203 | {0x0007, 0x03}, | ||
204 | {0x000d, 0x00}, | ||
205 | {0x000f, 0x02}, | ||
206 | {0x0300, 0x12}, | ||
207 | {0x0350, 0x41}, | ||
208 | {0x0351, 0x00}, | ||
209 | {0x0352, 0x00}, | ||
210 | {0x0353, 0x00}, | ||
211 | {0x0018, 0x10}, | ||
212 | {0x0019, 0x00}, | ||
213 | {0x001b, 0x0e}, | ||
214 | {0x001c, 0x46}, | ||
215 | {0x0300, 0x80}, | ||
216 | {0x001a, 0x04}, | ||
217 | {0x0110, 0x00}, | ||
218 | {0x0111, 0x00}, | ||
219 | {0x0112, 0x00}, | ||
220 | {0x0113, 0x00}, | ||
221 | |||
222 | {0xffff, 0xff}, | ||
223 | }; | ||
224 | |||
225 | |||
226 | static int stk_initialise(struct stk_camera *dev) | ||
227 | { | ||
228 | struct regval *rv; | ||
229 | int ret; | ||
230 | if (!is_present(dev)) | ||
231 | return -ENODEV; | ||
232 | if (is_initialised(dev)) | ||
233 | return 0; | ||
234 | rv = stk1125_initvals; | ||
235 | while (rv->reg != 0xffff) { | ||
236 | ret = stk_camera_write_reg(dev, rv->reg, rv->val); | ||
237 | if (ret) | ||
238 | return ret; | ||
239 | rv++; | ||
240 | } | ||
241 | if (stk_sensor_init(dev) == 0) { | ||
242 | set_initialised(dev); | ||
243 | return 0; | ||
244 | } else | ||
245 | return -1; | ||
246 | } | ||
247 | |||
248 | /* sysfs functions */ | ||
249 | /*FIXME cleanup this */ | ||
250 | |||
251 | static ssize_t show_brightness(struct device *class, | ||
252 | struct device_attribute *attr, char *buf) | ||
253 | { | ||
254 | struct video_device *vdev = to_video_device(class); | ||
255 | struct stk_camera *dev = vdev_to_camera(vdev); | ||
256 | |||
257 | return sprintf(buf, "%X\n", dev->vsettings.brightness); | ||
258 | } | ||
259 | |||
260 | static ssize_t store_brightness(struct device *class, | ||
261 | struct device_attribute *attr, const char *buf, size_t count) | ||
262 | { | ||
263 | char *endp; | ||
264 | unsigned long value; | ||
265 | int ret; | ||
266 | |||
267 | struct video_device *vdev = to_video_device(class); | ||
268 | struct stk_camera *dev = vdev_to_camera(vdev); | ||
269 | |||
270 | value = simple_strtoul(buf, &endp, 16); | ||
271 | |||
272 | dev->vsettings.brightness = (int) value; | ||
273 | |||
274 | ret = stk_sensor_set_brightness(dev, value >> 8); | ||
275 | if (ret) | ||
276 | return ret; | ||
277 | else | ||
278 | return count; | ||
279 | } | ||
280 | |||
281 | static ssize_t show_hflip(struct device *class, | ||
282 | struct device_attribute *attr, char *buf) | ||
283 | { | ||
284 | struct video_device *vdev = to_video_device(class); | ||
285 | struct stk_camera *dev = vdev_to_camera(vdev); | ||
286 | |||
287 | return sprintf(buf, "%d\n", dev->vsettings.hflip); | ||
288 | } | ||
289 | |||
290 | static ssize_t store_hflip(struct device *class, | ||
291 | struct device_attribute *attr, const char *buf, size_t count) | ||
292 | { | ||
293 | struct video_device *vdev = to_video_device(class); | ||
294 | struct stk_camera *dev = vdev_to_camera(vdev); | ||
295 | |||
296 | if (strncmp(buf, "1", 1) == 0) | ||
297 | dev->vsettings.hflip = 1; | ||
298 | else if (strncmp(buf, "0", 1) == 0) | ||
299 | dev->vsettings.hflip = 0; | ||
300 | else | ||
301 | return -EINVAL; | ||
302 | |||
303 | return strlen(buf); | ||
304 | } | ||
305 | |||
306 | static ssize_t show_vflip(struct device *class, | ||
307 | struct device_attribute *attr, char *buf) | ||
308 | { | ||
309 | struct video_device *vdev = to_video_device(class); | ||
310 | struct stk_camera *dev = vdev_to_camera(vdev); | ||
311 | |||
312 | return sprintf(buf, "%d\n", dev->vsettings.vflip); | ||
313 | } | ||
314 | |||
315 | static ssize_t store_vflip(struct device *class, | ||
316 | struct device_attribute *attr, const char *buf, size_t count) | ||
317 | { | ||
318 | struct video_device *vdev = to_video_device(class); | ||
319 | struct stk_camera *dev = vdev_to_camera(vdev); | ||
320 | |||
321 | if (strncmp(buf, "1", 1) == 0) | ||
322 | dev->vsettings.vflip = 1; | ||
323 | else if (strncmp(buf, "0", 1) == 0) | ||
324 | dev->vsettings.vflip = 0; | ||
325 | else | ||
326 | return -EINVAL; | ||
327 | |||
328 | return strlen(buf); | ||
329 | } | ||
330 | |||
331 | static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO, | ||
332 | show_brightness, store_brightness); | ||
333 | static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip); | ||
334 | static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip); | ||
335 | |||
336 | static int stk_create_sysfs_files(struct video_device *vdev) | ||
337 | { | ||
338 | int ret; | ||
339 | |||
340 | ret = video_device_create_file(vdev, &dev_attr_brightness); | ||
341 | ret += video_device_create_file(vdev, &dev_attr_hflip); | ||
342 | ret += video_device_create_file(vdev, &dev_attr_vflip); | ||
343 | return ret; | ||
344 | } | ||
345 | |||
346 | static void stk_remove_sysfs_files(struct video_device *vdev) | ||
347 | { | ||
348 | video_device_remove_file(vdev, &dev_attr_brightness); | ||
349 | video_device_remove_file(vdev, &dev_attr_hflip); | ||
350 | video_device_remove_file(vdev, &dev_attr_vflip); | ||
351 | } | ||
352 | |||
353 | |||
354 | /* *********************************************** */ | ||
355 | /* | ||
356 | * This function is called as an URB transfert is complete (Isochronous pipe). | ||
357 | * So, the traitement is done in interrupt time, so it has be fast, not crash, | ||
358 | * and not stall. Neat. | ||
359 | */ | ||
360 | static void stk_isoc_handler(struct urb *urb) | ||
361 | { | ||
362 | int i; | ||
363 | int ret; | ||
364 | int framelen; | ||
365 | unsigned long flags; | ||
366 | |||
367 | unsigned char *fill = NULL; | ||
368 | unsigned char *iso_buf = NULL; | ||
369 | |||
370 | struct stk_camera *dev; | ||
371 | struct stk_sio_buffer *fb; | ||
372 | |||
373 | dev = (struct stk_camera *) urb->context; | ||
374 | |||
375 | if (dev == NULL) { | ||
376 | STK_ERROR("isoc_handler called with NULL device !\n"); | ||
377 | return; | ||
378 | } | ||
379 | |||
380 | if (urb->status == -ENOENT || urb->status == -ECONNRESET | ||
381 | || urb->status == -ESHUTDOWN) { | ||
382 | atomic_dec(&dev->urbs_used); | ||
383 | return; | ||
384 | } | ||
385 | |||
386 | spin_lock_irqsave(&dev->spinlock, flags); | ||
387 | |||
388 | if (urb->status != -EINPROGRESS && urb->status != 0) { | ||
389 | STK_ERROR("isoc_handler: urb->status == %d\n", urb->status); | ||
390 | goto resubmit; | ||
391 | } | ||
392 | |||
393 | if (list_empty(&dev->sio_avail)) { | ||
394 | /*FIXME Stop streaming after a while */ | ||
395 | (void) (printk_ratelimit() && | ||
396 | STK_ERROR("isoc_handler without available buffer!\n")); | ||
397 | goto resubmit; | ||
398 | } | ||
399 | fb = list_first_entry(&dev->sio_avail, | ||
400 | struct stk_sio_buffer, list); | ||
401 | fill = fb->buffer + fb->v4lbuf.bytesused; | ||
402 | |||
403 | for (i = 0; i < urb->number_of_packets; i++) { | ||
404 | if (urb->iso_frame_desc[i].status != 0) { | ||
405 | if (urb->iso_frame_desc[i].status != -EXDEV) | ||
406 | STK_ERROR("Frame %d has error %d\n", i, | ||
407 | urb->iso_frame_desc[i].status); | ||
408 | continue; | ||
409 | } | ||
410 | framelen = urb->iso_frame_desc[i].actual_length; | ||
411 | iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset; | ||
412 | |||
413 | if (framelen <= 4) | ||
414 | continue; /* no data */ | ||
415 | |||
416 | /* | ||
417 | * we found something informational from there | ||
418 | * the isoc frames have to type of headers | ||
419 | * type1: 00 xx 00 00 or 20 xx 00 00 | ||
420 | * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00 | ||
421 | * xx is a sequencer which has never been seen over 0x3f | ||
422 | * imho data written down looks like bayer, i see similarities | ||
423 | * after every 640 bytes | ||
424 | */ | ||
425 | if (*iso_buf & 0x80) { | ||
426 | framelen -= 8; | ||
427 | iso_buf += 8; | ||
428 | /* This marks a new frame */ | ||
429 | if (fb->v4lbuf.bytesused != 0 | ||
430 | && fb->v4lbuf.bytesused != dev->frame_size) { | ||
431 | (void) (printk_ratelimit() && | ||
432 | STK_ERROR("frame %d, " | ||
433 | "bytesused=%d, skipping\n", | ||
434 | i, fb->v4lbuf.bytesused)); | ||
435 | fb->v4lbuf.bytesused = 0; | ||
436 | fill = fb->buffer; | ||
437 | } else if (fb->v4lbuf.bytesused == dev->frame_size) { | ||
438 | list_move_tail(dev->sio_avail.next, | ||
439 | &dev->sio_full); | ||
440 | wake_up(&dev->wait_frame); | ||
441 | if (list_empty(&dev->sio_avail)) { | ||
442 | (void) (printk_ratelimit() && | ||
443 | STK_ERROR("No buffer available\n")); | ||
444 | goto resubmit; | ||
445 | } | ||
446 | fb = list_first_entry(&dev->sio_avail, | ||
447 | struct stk_sio_buffer, list); | ||
448 | fb->v4lbuf.bytesused = 0; | ||
449 | fill = fb->buffer; | ||
450 | } | ||
451 | } else { | ||
452 | framelen -= 4; | ||
453 | iso_buf += 4; | ||
454 | } | ||
455 | |||
456 | /* Our buffer is full !!! */ | ||
457 | if (framelen + fb->v4lbuf.bytesused > dev->frame_size) { | ||
458 | (void) (printk_ratelimit() && | ||
459 | STK_ERROR("Frame buffer overflow, lost sync\n")); | ||
460 | /*FIXME Do something here? */ | ||
461 | continue; | ||
462 | } | ||
463 | spin_unlock_irqrestore(&dev->spinlock, flags); | ||
464 | memcpy(fill, iso_buf, framelen); | ||
465 | spin_lock_irqsave(&dev->spinlock, flags); | ||
466 | fill += framelen; | ||
467 | |||
468 | /* New size of our buffer */ | ||
469 | fb->v4lbuf.bytesused += framelen; | ||
470 | } | ||
471 | |||
472 | resubmit: | ||
473 | spin_unlock_irqrestore(&dev->spinlock, flags); | ||
474 | urb->dev = dev->udev; | ||
475 | ret = usb_submit_urb(urb, GFP_ATOMIC); | ||
476 | if (ret != 0) { | ||
477 | STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n", | ||
478 | ret); | ||
479 | } | ||
480 | } | ||
481 | |||
482 | /* -------------------------------------------- */ | ||
483 | |||
484 | static int stk_prepare_iso(struct stk_camera *dev) | ||
485 | { | ||
486 | void *kbuf; | ||
487 | int i, j; | ||
488 | struct urb *urb; | ||
489 | struct usb_device *udev; | ||
490 | |||
491 | if (dev == NULL) | ||
492 | return -ENXIO; | ||
493 | udev = dev->udev; | ||
494 | |||
495 | if (dev->isobufs) | ||
496 | STK_ERROR("isobufs already allocated. Bad\n"); | ||
497 | else | ||
498 | dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs), | ||
499 | GFP_KERNEL); | ||
500 | if (dev->isobufs == NULL) { | ||
501 | STK_ERROR("Unable to allocate iso buffers\n"); | ||
502 | return -ENOMEM; | ||
503 | } | ||
504 | for (i = 0; i < MAX_ISO_BUFS; i++) { | ||
505 | if (dev->isobufs[i].data == NULL) { | ||
506 | kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL); | ||
507 | if (kbuf == NULL) { | ||
508 | STK_ERROR("Failed to allocate iso buffer %d\n", | ||
509 | i); | ||
510 | goto isobufs_out; | ||
511 | } | ||
512 | dev->isobufs[i].data = kbuf; | ||
513 | } else | ||
514 | STK_ERROR("isobuf data already allocated\n"); | ||
515 | if (dev->isobufs[i].urb == NULL) { | ||
516 | urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL); | ||
517 | if (urb == NULL) { | ||
518 | STK_ERROR("Failed to allocate URB %d\n", i); | ||
519 | goto isobufs_out; | ||
520 | } | ||
521 | dev->isobufs[i].urb = urb; | ||
522 | } else { | ||
523 | STK_ERROR("Killing URB\n"); | ||
524 | usb_kill_urb(dev->isobufs[i].urb); | ||
525 | urb = dev->isobufs[i].urb; | ||
526 | } | ||
527 | urb->interval = 1; | ||
528 | urb->dev = udev; | ||
529 | urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep); | ||
530 | urb->transfer_flags = URB_ISO_ASAP; | ||
531 | urb->transfer_buffer = dev->isobufs[i].data; | ||
532 | urb->transfer_buffer_length = ISO_BUFFER_SIZE; | ||
533 | urb->complete = stk_isoc_handler; | ||
534 | urb->context = dev; | ||
535 | urb->start_frame = 0; | ||
536 | urb->number_of_packets = ISO_FRAMES_PER_DESC; | ||
537 | |||
538 | for (j = 0; j < ISO_FRAMES_PER_DESC; j++) { | ||
539 | urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE; | ||
540 | urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE; | ||
541 | } | ||
542 | } | ||
543 | set_memallocd(dev); | ||
544 | return 0; | ||
545 | |||
546 | isobufs_out: | ||
547 | for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++) | ||
548 | kfree(dev->isobufs[i].data); | ||
549 | for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++) | ||
550 | usb_free_urb(dev->isobufs[i].urb); | ||
551 | kfree(dev->isobufs); | ||
552 | dev->isobufs = NULL; | ||
553 | return -ENOMEM; | ||
554 | } | ||
555 | |||
556 | static void stk_clean_iso(struct stk_camera *dev) | ||
557 | { | ||
558 | int i; | ||
559 | |||
560 | if (dev == NULL || dev->isobufs == NULL) | ||
561 | return; | ||
562 | |||
563 | for (i = 0; i < MAX_ISO_BUFS; i++) { | ||
564 | struct urb *urb; | ||
565 | |||
566 | urb = dev->isobufs[i].urb; | ||
567 | if (urb) { | ||
568 | if (atomic_read(&dev->urbs_used)) | ||
569 | usb_kill_urb(urb); | ||
570 | usb_free_urb(urb); | ||
571 | } | ||
572 | kfree(dev->isobufs[i].data); | ||
573 | } | ||
574 | kfree(dev->isobufs); | ||
575 | dev->isobufs = NULL; | ||
576 | unset_memallocd(dev); | ||
577 | } | ||
578 | |||
579 | static int stk_setup_siobuf(struct stk_camera *dev, int index) | ||
580 | { | ||
581 | struct stk_sio_buffer *buf = dev->sio_bufs + index; | ||
582 | INIT_LIST_HEAD(&buf->list); | ||
583 | buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size); | ||
584 | buf->buffer = vmalloc_user(buf->v4lbuf.length); | ||
585 | if (buf->buffer == NULL) | ||
586 | return -ENOMEM; | ||
587 | buf->mapcount = 0; | ||
588 | buf->dev = dev; | ||
589 | buf->v4lbuf.index = index; | ||
590 | buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
591 | buf->v4lbuf.field = V4L2_FIELD_NONE; | ||
592 | buf->v4lbuf.memory = V4L2_MEMORY_MMAP; | ||
593 | buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length; | ||
594 | return 0; | ||
595 | } | ||
596 | |||
597 | static int stk_free_sio_buffers(struct stk_camera *dev) | ||
598 | { | ||
599 | int i; | ||
600 | int nbufs; | ||
601 | unsigned long flags; | ||
602 | if (dev->n_sbufs == 0 || dev->sio_bufs == NULL) | ||
603 | return 0; | ||
604 | /* | ||
605 | * If any buffers are mapped, we cannot free them at all. | ||
606 | */ | ||
607 | for (i = 0; i < dev->n_sbufs; i++) { | ||
608 | if (dev->sio_bufs[i].mapcount > 0) | ||
609 | return -EBUSY; | ||
610 | } | ||
611 | /* | ||
612 | * OK, let's do it. | ||
613 | */ | ||
614 | spin_lock_irqsave(&dev->spinlock, flags); | ||
615 | INIT_LIST_HEAD(&dev->sio_avail); | ||
616 | INIT_LIST_HEAD(&dev->sio_full); | ||
617 | nbufs = dev->n_sbufs; | ||
618 | dev->n_sbufs = 0; | ||
619 | spin_unlock_irqrestore(&dev->spinlock, flags); | ||
620 | for (i = 0; i < nbufs; i++) { | ||
621 | if (dev->sio_bufs[i].buffer != NULL) | ||
622 | vfree(dev->sio_bufs[i].buffer); | ||
623 | } | ||
624 | kfree(dev->sio_bufs); | ||
625 | dev->sio_bufs = NULL; | ||
626 | return 0; | ||
627 | } | ||
628 | |||
629 | static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs) | ||
630 | { | ||
631 | int i; | ||
632 | if (dev->sio_bufs != NULL) | ||
633 | STK_ERROR("sio_bufs already allocated\n"); | ||
634 | else { | ||
635 | dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer), | ||
636 | GFP_KERNEL); | ||
637 | if (dev->sio_bufs == NULL) | ||
638 | return -ENOMEM; | ||
639 | for (i = 0; i < n_sbufs; i++) { | ||
640 | if (stk_setup_siobuf(dev, i)) | ||
641 | return (dev->n_sbufs > 1)? 0 : -ENOMEM; | ||
642 | dev->n_sbufs = i+1; | ||
643 | } | ||
644 | } | ||
645 | return 0; | ||
646 | } | ||
647 | |||
648 | static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs) | ||
649 | { | ||
650 | int err; | ||
651 | err = stk_prepare_iso(dev); | ||
652 | if (err) { | ||
653 | stk_clean_iso(dev); | ||
654 | return err; | ||
655 | } | ||
656 | err = stk_prepare_sio_buffers(dev, n_sbufs); | ||
657 | if (err) { | ||
658 | stk_free_sio_buffers(dev); | ||
659 | return err; | ||
660 | } | ||
661 | return 0; | ||
662 | } | ||
663 | |||
664 | static void stk_free_buffers(struct stk_camera *dev) | ||
665 | { | ||
666 | stk_clean_iso(dev); | ||
667 | stk_free_sio_buffers(dev); | ||
668 | } | ||
669 | /* -------------------------------------------- */ | ||
670 | |||
671 | /* v4l file operations */ | ||
672 | |||
673 | static int v4l_stk_open(struct inode *inode, struct file *fp) | ||
674 | { | ||
675 | struct stk_camera *dev; | ||
676 | struct video_device *vdev; | ||
677 | |||
678 | vdev = video_devdata(fp); | ||
679 | dev = vdev_to_camera(vdev); | ||
680 | |||
681 | if (dev == NULL || !is_present(dev)) | ||
682 | return -ENXIO; | ||
683 | fp->private_data = vdev; | ||
684 | kref_get(&dev->kref); | ||
685 | |||
686 | return 0; | ||
687 | } | ||
688 | |||
689 | static int v4l_stk_release(struct inode *inode, struct file *fp) | ||
690 | { | ||
691 | struct stk_camera *dev; | ||
692 | struct video_device *vdev; | ||
693 | |||
694 | vdev = video_devdata(fp); | ||
695 | if (vdev == NULL) { | ||
696 | STK_ERROR("v4l_release called w/o video devdata\n"); | ||
697 | return -EFAULT; | ||
698 | } | ||
699 | dev = vdev_to_camera(vdev); | ||
700 | if (dev == NULL) { | ||
701 | STK_ERROR("v4l_release called on removed device\n"); | ||
702 | return -ENODEV; | ||
703 | } | ||
704 | |||
705 | if (dev->owner != fp) { | ||
706 | kref_put(&dev->kref, stk_camera_cleanup); | ||
707 | return 0; | ||
708 | } | ||
709 | |||
710 | stk_stop_stream(dev); | ||
711 | |||
712 | stk_free_buffers(dev); | ||
713 | |||
714 | dev->owner = NULL; | ||
715 | |||
716 | kref_put(&dev->kref, stk_camera_cleanup); | ||
717 | |||
718 | return 0; | ||
719 | } | ||
720 | |||
721 | static ssize_t v4l_stk_read(struct file *fp, char __user *buf, | ||
722 | size_t count, loff_t *f_pos) | ||
723 | { | ||
724 | int i; | ||
725 | int ret; | ||
726 | unsigned long flags; | ||
727 | struct stk_camera *dev; | ||
728 | struct video_device *vdev; | ||
729 | struct stk_sio_buffer *sbuf; | ||
730 | |||
731 | vdev = video_devdata(fp); | ||
732 | if (vdev == NULL) | ||
733 | return -EFAULT; | ||
734 | dev = vdev_to_camera(vdev); | ||
735 | |||
736 | if (dev == NULL) | ||
737 | return -EIO; | ||
738 | |||
739 | if (!is_present(dev)) | ||
740 | return -EIO; | ||
741 | if (dev->owner && dev->owner != fp) | ||
742 | return -EBUSY; | ||
743 | dev->owner = fp; | ||
744 | if (!is_streaming(dev)) { | ||
745 | if (stk_initialise(dev) | ||
746 | || stk_allocate_buffers(dev, 3) | ||
747 | || stk_start_stream(dev)) | ||
748 | return -ENOMEM; | ||
749 | spin_lock_irqsave(&dev->spinlock, flags); | ||
750 | for (i = 0; i < dev->n_sbufs; i++) { | ||
751 | list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail); | ||
752 | dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED; | ||
753 | } | ||
754 | spin_unlock_irqrestore(&dev->spinlock, flags); | ||
755 | } | ||
756 | if (*f_pos == 0) { | ||
757 | if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full)) | ||
758 | return -EWOULDBLOCK; | ||
759 | ret = wait_event_interruptible(dev->wait_frame, | ||
760 | !list_empty(&dev->sio_full) || !is_present(dev)); | ||
761 | if (ret) | ||
762 | return ret; | ||
763 | if (!is_present(dev)) | ||
764 | return -EIO; | ||
765 | } | ||
766 | if (count + *f_pos > dev->frame_size) | ||
767 | count = dev->frame_size - *f_pos; | ||
768 | spin_lock_irqsave(&dev->spinlock, flags); | ||
769 | if (list_empty(&dev->sio_full)) { | ||
770 | spin_unlock_irqrestore(&dev->spinlock, flags); | ||
771 | STK_ERROR("BUG: No siobufs ready\n"); | ||
772 | return 0; | ||
773 | } | ||
774 | sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list); | ||
775 | spin_unlock_irqrestore(&dev->spinlock, flags); | ||
776 | |||
777 | if (copy_to_user(buf, sbuf->buffer + *f_pos, count)) | ||
778 | return -EFAULT; | ||
779 | |||
780 | *f_pos += count; | ||
781 | |||
782 | if (*f_pos >= dev->frame_size) { | ||
783 | *f_pos = 0; | ||
784 | spin_lock_irqsave(&dev->spinlock, flags); | ||
785 | list_move_tail(&sbuf->list, &dev->sio_avail); | ||
786 | spin_unlock_irqrestore(&dev->spinlock, flags); | ||
787 | } | ||
788 | return count; | ||
789 | } | ||
790 | |||
791 | static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait) | ||
792 | { | ||
793 | struct stk_camera *dev; | ||
794 | struct video_device *vdev; | ||
795 | |||
796 | vdev = video_devdata(fp); | ||
797 | |||
798 | if (vdev == NULL) | ||
799 | return -EFAULT; | ||
800 | |||
801 | dev = vdev_to_camera(vdev); | ||
802 | if (dev == NULL) | ||
803 | return -ENODEV; | ||
804 | |||
805 | poll_wait(fp, &dev->wait_frame, wait); | ||
806 | |||
807 | if (!is_present(dev)) | ||
808 | return POLLERR; | ||
809 | |||
810 | if (!list_empty(&dev->sio_full)) | ||
811 | return (POLLIN | POLLRDNORM); | ||
812 | |||
813 | return 0; | ||
814 | } | ||
815 | |||
816 | |||
817 | static void stk_v4l_vm_open(struct vm_area_struct *vma) | ||
818 | { | ||
819 | struct stk_sio_buffer *sbuf = vma->vm_private_data; | ||
820 | sbuf->mapcount++; | ||
821 | } | ||
822 | static void stk_v4l_vm_close(struct vm_area_struct *vma) | ||
823 | { | ||
824 | struct stk_sio_buffer *sbuf = vma->vm_private_data; | ||
825 | sbuf->mapcount--; | ||
826 | if (sbuf->mapcount == 0) | ||
827 | sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED; | ||
828 | } | ||
829 | static struct vm_operations_struct stk_v4l_vm_ops = { | ||
830 | .open = stk_v4l_vm_open, | ||
831 | .close = stk_v4l_vm_close | ||
832 | }; | ||
833 | |||
834 | static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma) | ||
835 | { | ||
836 | unsigned int i; | ||
837 | int ret; | ||
838 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; | ||
839 | struct stk_camera *dev; | ||
840 | struct video_device *vdev; | ||
841 | struct stk_sio_buffer *sbuf = NULL; | ||
842 | |||
843 | if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) | ||
844 | return -EINVAL; | ||
845 | |||
846 | vdev = video_devdata(fp); | ||
847 | dev = vdev_to_camera(vdev); | ||
848 | |||
849 | for (i = 0; i < dev->n_sbufs; i++) { | ||
850 | if (dev->sio_bufs[i].v4lbuf.m.offset == offset) { | ||
851 | sbuf = dev->sio_bufs + i; | ||
852 | break; | ||
853 | } | ||
854 | } | ||
855 | if (sbuf == NULL) | ||
856 | return -EINVAL; | ||
857 | ret = remap_vmalloc_range(vma, sbuf->buffer, 0); | ||
858 | if (ret) | ||
859 | return ret; | ||
860 | vma->vm_flags |= VM_DONTEXPAND; | ||
861 | vma->vm_private_data = sbuf; | ||
862 | vma->vm_ops = &stk_v4l_vm_ops; | ||
863 | sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED; | ||
864 | stk_v4l_vm_open(vma); | ||
865 | return 0; | ||
866 | } | ||
867 | |||
868 | /* v4l ioctl handlers */ | ||
869 | |||
870 | static int stk_vidioc_querycap(struct file *filp, | ||
871 | void *priv, struct v4l2_capability *cap) | ||
872 | { | ||
873 | strcpy(cap->driver, "stk"); | ||
874 | strcpy(cap->card, "stk"); | ||
875 | cap->version = DRIVER_VERSION_NUM; | ||
876 | |||
877 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | ||
878 | | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; | ||
879 | return 0; | ||
880 | } | ||
881 | |||
882 | static int stk_vidioc_enum_input(struct file *filp, | ||
883 | void *priv, struct v4l2_input *input) | ||
884 | { | ||
885 | if (input->index != 0) | ||
886 | return -EINVAL; | ||
887 | |||
888 | strcpy(input->name, "Syntek USB Camera"); | ||
889 | input->type = V4L2_INPUT_TYPE_CAMERA; | ||
890 | return 0; | ||
891 | } | ||
892 | |||
893 | |||
894 | static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i) | ||
895 | { | ||
896 | *i = 0; | ||
897 | return 0; | ||
898 | } | ||
899 | |||
900 | static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i) | ||
901 | { | ||
902 | if (i != 0) | ||
903 | return -EINVAL; | ||
904 | else | ||
905 | return 0; | ||
906 | } | ||
907 | |||
908 | /* from vivi.c */ | ||
909 | static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a) | ||
910 | { | ||
911 | return 0; | ||
912 | } | ||
913 | |||
914 | /* List of all V4Lv2 controls supported by the driver */ | ||
915 | static struct v4l2_queryctrl stk_controls[] = { | ||
916 | { | ||
917 | .id = V4L2_CID_BRIGHTNESS, | ||
918 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
919 | .name = "Brightness", | ||
920 | .minimum = 0, | ||
921 | .maximum = 0xffff, | ||
922 | .step = 0x0100, | ||
923 | .default_value = 0x6000, | ||
924 | }, | ||
925 | /*TODO: get more controls to work */ | ||
926 | }; | ||
927 | |||
928 | static int stk_vidioc_queryctrl(struct file *filp, | ||
929 | void *priv, struct v4l2_queryctrl *c) | ||
930 | { | ||
931 | int i; | ||
932 | int nbr; | ||
933 | nbr = ARRAY_SIZE(stk_controls); | ||
934 | |||
935 | for (i = 0; i < nbr; i++) { | ||
936 | if (stk_controls[i].id == c->id) { | ||
937 | memcpy(c, &stk_controls[i], | ||
938 | sizeof(struct v4l2_queryctrl)); | ||
939 | return 0; | ||
940 | } | ||
941 | } | ||
942 | return -EINVAL; | ||
943 | } | ||
944 | |||
945 | static int stk_vidioc_g_ctrl(struct file *filp, | ||
946 | void *priv, struct v4l2_control *c) | ||
947 | { | ||
948 | struct stk_camera *dev = priv; | ||
949 | switch (c->id) { | ||
950 | case V4L2_CID_BRIGHTNESS: | ||
951 | c->value = dev->vsettings.brightness; | ||
952 | break; | ||
953 | default: | ||
954 | return -EINVAL; | ||
955 | } | ||
956 | return 0; | ||
957 | } | ||
958 | |||
959 | static int stk_vidioc_s_ctrl(struct file *filp, | ||
960 | void *priv, struct v4l2_control *c) | ||
961 | { | ||
962 | struct stk_camera *dev = priv; | ||
963 | switch (c->id) { | ||
964 | case V4L2_CID_BRIGHTNESS: | ||
965 | dev->vsettings.brightness = c->value; | ||
966 | return stk_sensor_set_brightness(dev, c->value >> 8); | ||
967 | default: | ||
968 | return -EINVAL; | ||
969 | } | ||
970 | return 0; | ||
971 | } | ||
972 | |||
973 | |||
974 | static int stk_vidioc_enum_fmt_cap(struct file *filp, | ||
975 | void *priv, struct v4l2_fmtdesc *fmtd) | ||
976 | { | ||
977 | fmtd->flags = 0; | ||
978 | |||
979 | switch (fmtd->index) { | ||
980 | case 0: | ||
981 | fmtd->pixelformat = V4L2_PIX_FMT_RGB565; | ||
982 | strcpy(fmtd->description, "r5g6b5"); | ||
983 | break; | ||
984 | case 1: | ||
985 | fmtd->pixelformat = V4L2_PIX_FMT_RGB565X; | ||
986 | strcpy(fmtd->description, "r5g6b5BE"); | ||
987 | break; | ||
988 | case 2: | ||
989 | fmtd->pixelformat = V4L2_PIX_FMT_UYVY; | ||
990 | strcpy(fmtd->description, "yuv4:2:2"); | ||
991 | break; | ||
992 | case 3: | ||
993 | fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8; | ||
994 | strcpy(fmtd->description, "Raw bayer"); | ||
995 | break; | ||
996 | default: | ||
997 | return -EINVAL; | ||
998 | } | ||
999 | return 0; | ||
1000 | } | ||
1001 | |||
1002 | static struct stk_size { | ||
1003 | unsigned w; | ||
1004 | unsigned h; | ||
1005 | enum stk_mode m; | ||
1006 | } stk_sizes[] = { | ||
1007 | { .w = 1280, .h = 1024, .m = MODE_SXGA, }, | ||
1008 | { .w = 640, .h = 480, .m = MODE_VGA, }, | ||
1009 | { .w = 352, .h = 288, .m = MODE_CIF, }, | ||
1010 | { .w = 320, .h = 240, .m = MODE_QVGA, }, | ||
1011 | { .w = 176, .h = 144, .m = MODE_QCIF, }, | ||
1012 | }; | ||
1013 | |||
1014 | static int stk_vidioc_g_fmt_cap(struct file *filp, | ||
1015 | void *priv, struct v4l2_format *f) | ||
1016 | { | ||
1017 | struct v4l2_pix_format *pix_format = &f->fmt.pix; | ||
1018 | struct stk_camera *dev = priv; | ||
1019 | int i; | ||
1020 | |||
1021 | for (i = 0; i < ARRAY_SIZE(stk_sizes) | ||
1022 | && stk_sizes[i].m != dev->vsettings.mode; | ||
1023 | i++); | ||
1024 | if (i == ARRAY_SIZE(stk_sizes)) { | ||
1025 | STK_ERROR("ERROR: mode invalid\n"); | ||
1026 | return -EINVAL; | ||
1027 | } | ||
1028 | pix_format->width = stk_sizes[i].w; | ||
1029 | pix_format->height = stk_sizes[i].h; | ||
1030 | pix_format->field = V4L2_FIELD_NONE; | ||
1031 | pix_format->colorspace = V4L2_COLORSPACE_SRGB; | ||
1032 | pix_format->priv = 0; | ||
1033 | pix_format->pixelformat = dev->vsettings.palette; | ||
1034 | if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8) | ||
1035 | pix_format->bytesperline = pix_format->width; | ||
1036 | else | ||
1037 | pix_format->bytesperline = 2 * pix_format->width; | ||
1038 | pix_format->sizeimage = pix_format->bytesperline | ||
1039 | * pix_format->height; | ||
1040 | return 0; | ||
1041 | } | ||
1042 | |||
1043 | static int stk_vidioc_try_fmt_cap(struct file *filp, | ||
1044 | void *priv, struct v4l2_format *fmtd) | ||
1045 | { | ||
1046 | int i; | ||
1047 | switch (fmtd->fmt.pix.pixelformat) { | ||
1048 | case V4L2_PIX_FMT_RGB565: | ||
1049 | case V4L2_PIX_FMT_RGB565X: | ||
1050 | case V4L2_PIX_FMT_UYVY: | ||
1051 | case V4L2_PIX_FMT_SBGGR8: | ||
1052 | break; | ||
1053 | default: | ||
1054 | return -EINVAL; | ||
1055 | } | ||
1056 | for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) { | ||
1057 | if (fmtd->fmt.pix.width > stk_sizes[i].w) | ||
1058 | break; | ||
1059 | } | ||
1060 | if (i == ARRAY_SIZE(stk_sizes) | ||
1061 | || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w) | ||
1062 | < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) { | ||
1063 | fmtd->fmt.pix.height = stk_sizes[i-1].h; | ||
1064 | fmtd->fmt.pix.width = stk_sizes[i-1].w; | ||
1065 | fmtd->fmt.pix.priv = i - 1; | ||
1066 | } else { | ||
1067 | fmtd->fmt.pix.height = stk_sizes[i].h; | ||
1068 | fmtd->fmt.pix.width = stk_sizes[i].w; | ||
1069 | fmtd->fmt.pix.priv = i; | ||
1070 | } | ||
1071 | |||
1072 | fmtd->fmt.pix.field = V4L2_FIELD_NONE; | ||
1073 | fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; | ||
1074 | if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8) | ||
1075 | fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width; | ||
1076 | else | ||
1077 | fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width; | ||
1078 | fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline | ||
1079 | * fmtd->fmt.pix.height; | ||
1080 | return 0; | ||
1081 | } | ||
1082 | |||
1083 | static int stk_vidioc_s_fmt_cap(struct file *filp, | ||
1084 | void *priv, struct v4l2_format *fmtd) | ||
1085 | { | ||
1086 | int ret; | ||
1087 | struct stk_camera *dev = priv; | ||
1088 | |||
1089 | if (dev == NULL) | ||
1090 | return -ENODEV; | ||
1091 | if (!is_present(dev)) | ||
1092 | return -ENODEV; | ||
1093 | if (is_streaming(dev)) | ||
1094 | return -EBUSY; | ||
1095 | if (dev->owner && dev->owner != filp) | ||
1096 | return -EBUSY; | ||
1097 | dev->owner = filp; | ||
1098 | ret = stk_vidioc_try_fmt_cap(filp, priv, fmtd); | ||
1099 | if (ret) | ||
1100 | return ret; | ||
1101 | |||
1102 | dev->vsettings.palette = fmtd->fmt.pix.pixelformat; | ||
1103 | stk_free_buffers(dev); | ||
1104 | dev->frame_size = fmtd->fmt.pix.sizeimage; | ||
1105 | dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m; | ||
1106 | |||
1107 | stk_initialise(dev); | ||
1108 | /* This registers controls some timings, not sure of what. */ | ||
1109 | stk_camera_write_reg(dev, 0x001b, 0x0e); | ||
1110 | if (dev->vsettings.mode == MODE_SXGA) | ||
1111 | stk_camera_write_reg(dev, 0x001c, 0x0e); | ||
1112 | else | ||
1113 | stk_camera_write_reg(dev, 0x001c, 0x46); | ||
1114 | /* | ||
1115 | * Registers 0x0115 0x0114 are the size of each line (bytes), | ||
1116 | * regs 0x0117 0x0116 are the heigth of the image. | ||
1117 | */ | ||
1118 | stk_camera_write_reg(dev, 0x0115, | ||
1119 | (fmtd->fmt.pix.bytesperline >> 8) & 0xff); | ||
1120 | stk_camera_write_reg(dev, 0x0114, | ||
1121 | fmtd->fmt.pix.bytesperline & 0xff); | ||
1122 | stk_camera_write_reg(dev, 0x0117, | ||
1123 | (fmtd->fmt.pix.height >> 8) & 0xff); | ||
1124 | stk_camera_write_reg(dev, 0x0116, | ||
1125 | fmtd->fmt.pix.height & 0xff); | ||
1126 | return stk_sensor_configure(dev); | ||
1127 | } | ||
1128 | |||
1129 | static int stk_vidioc_reqbufs(struct file *filp, | ||
1130 | void *priv, struct v4l2_requestbuffers *rb) | ||
1131 | { | ||
1132 | struct stk_camera *dev = priv; | ||
1133 | |||
1134 | if (dev == NULL) | ||
1135 | return -ENODEV; | ||
1136 | if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
1137 | return -EINVAL; | ||
1138 | if (rb->memory != V4L2_MEMORY_MMAP) | ||
1139 | return -EINVAL; | ||
1140 | if (is_streaming(dev) | ||
1141 | || (dev->owner && dev->owner != filp)) | ||
1142 | return -EBUSY; | ||
1143 | dev->owner = filp; | ||
1144 | |||
1145 | /*FIXME If they ask for zero, we must stop streaming and free */ | ||
1146 | if (rb->count < 3) | ||
1147 | rb->count = 3; | ||
1148 | /* Arbitrary limit */ | ||
1149 | else if (rb->count > 5) | ||
1150 | rb->count = 5; | ||
1151 | |||
1152 | stk_allocate_buffers(dev, rb->count); | ||
1153 | rb->count = dev->n_sbufs; | ||
1154 | return 0; | ||
1155 | } | ||
1156 | |||
1157 | static int stk_vidioc_querybuf(struct file *filp, | ||
1158 | void *priv, struct v4l2_buffer *buf) | ||
1159 | { | ||
1160 | int index; | ||
1161 | struct stk_camera *dev = priv; | ||
1162 | struct stk_sio_buffer *sbuf; | ||
1163 | |||
1164 | if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
1165 | return -EINVAL; | ||
1166 | |||
1167 | index = buf->index; | ||
1168 | |||
1169 | if (index < 0 || index >= dev->n_sbufs) | ||
1170 | return -EINVAL; | ||
1171 | sbuf = dev->sio_bufs + buf->index; | ||
1172 | *buf = sbuf->v4lbuf; | ||
1173 | return 0; | ||
1174 | } | ||
1175 | |||
1176 | static int stk_vidioc_qbuf(struct file *filp, | ||
1177 | void *priv, struct v4l2_buffer *buf) | ||
1178 | { | ||
1179 | struct stk_camera *dev = priv; | ||
1180 | struct stk_sio_buffer *sbuf; | ||
1181 | unsigned long flags; | ||
1182 | if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
1183 | return -EINVAL; | ||
1184 | |||
1185 | if (buf->memory != V4L2_MEMORY_MMAP) | ||
1186 | return -EINVAL; | ||
1187 | |||
1188 | if (buf->index < 0 || buf->index >= dev->n_sbufs) | ||
1189 | return -EINVAL; | ||
1190 | sbuf = dev->sio_bufs + buf->index; | ||
1191 | if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) | ||
1192 | return 0; | ||
1193 | sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED; | ||
1194 | sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE; | ||
1195 | spin_lock_irqsave(&dev->spinlock, flags); | ||
1196 | list_add_tail(&sbuf->list, &dev->sio_avail); | ||
1197 | *buf = sbuf->v4lbuf; | ||
1198 | spin_unlock_irqrestore(&dev->spinlock, flags); | ||
1199 | return 0; | ||
1200 | } | ||
1201 | |||
1202 | static int stk_vidioc_dqbuf(struct file *filp, | ||
1203 | void *priv, struct v4l2_buffer *buf) | ||
1204 | { | ||
1205 | struct stk_camera *dev = priv; | ||
1206 | struct stk_sio_buffer *sbuf; | ||
1207 | unsigned long flags; | ||
1208 | int ret; | ||
1209 | |||
1210 | if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE | ||
1211 | || !is_streaming(dev)) | ||
1212 | return -EINVAL; | ||
1213 | |||
1214 | if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full)) | ||
1215 | return -EWOULDBLOCK; | ||
1216 | ret = wait_event_interruptible(dev->wait_frame, | ||
1217 | !list_empty(&dev->sio_full) || !is_present(dev)); | ||
1218 | if (ret) | ||
1219 | return ret; | ||
1220 | if (!is_present(dev)) | ||
1221 | return -EIO; | ||
1222 | |||
1223 | spin_lock_irqsave(&dev->spinlock, flags); | ||
1224 | sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list); | ||
1225 | list_del_init(&sbuf->list); | ||
1226 | spin_unlock_irqrestore(&dev->spinlock, flags); | ||
1227 | sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED; | ||
1228 | sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE; | ||
1229 | sbuf->v4lbuf.sequence = ++dev->sequence; | ||
1230 | do_gettimeofday(&sbuf->v4lbuf.timestamp); | ||
1231 | |||
1232 | *buf = sbuf->v4lbuf; | ||
1233 | return 0; | ||
1234 | } | ||
1235 | |||
1236 | static int stk_vidioc_streamon(struct file *filp, | ||
1237 | void *priv, enum v4l2_buf_type type) | ||
1238 | { | ||
1239 | struct stk_camera *dev = priv; | ||
1240 | if (is_streaming(dev)) | ||
1241 | return 0; | ||
1242 | if (dev->sio_bufs == NULL) | ||
1243 | return -EINVAL; | ||
1244 | dev->sequence = 0; | ||
1245 | return stk_start_stream(dev); | ||
1246 | } | ||
1247 | |||
1248 | static int stk_vidioc_streamoff(struct file *filp, | ||
1249 | void *priv, enum v4l2_buf_type type) | ||
1250 | { | ||
1251 | struct stk_camera *dev = priv; | ||
1252 | unsigned long flags; | ||
1253 | int i; | ||
1254 | stk_stop_stream(dev); | ||
1255 | spin_lock_irqsave(&dev->spinlock, flags); | ||
1256 | INIT_LIST_HEAD(&dev->sio_avail); | ||
1257 | INIT_LIST_HEAD(&dev->sio_full); | ||
1258 | for (i = 0; i < dev->n_sbufs; i++) { | ||
1259 | INIT_LIST_HEAD(&dev->sio_bufs[i].list); | ||
1260 | dev->sio_bufs[i].v4lbuf.flags = 0; | ||
1261 | } | ||
1262 | spin_unlock_irqrestore(&dev->spinlock, flags); | ||
1263 | return 0; | ||
1264 | } | ||
1265 | |||
1266 | |||
1267 | static int stk_vidioc_g_parm(struct file *filp, | ||
1268 | void *priv, struct v4l2_streamparm *sp) | ||
1269 | { | ||
1270 | if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
1271 | return -EINVAL; | ||
1272 | |||
1273 | sp->parm.capture.capability = 0; | ||
1274 | sp->parm.capture.capturemode = 0; | ||
1275 | /*FIXME This is not correct */ | ||
1276 | sp->parm.capture.timeperframe.numerator = 1; | ||
1277 | sp->parm.capture.timeperframe.denominator = 30; | ||
1278 | sp->parm.capture.readbuffers = 2; | ||
1279 | sp->parm.capture.extendedmode = 0; | ||
1280 | return 0; | ||
1281 | } | ||
1282 | |||
1283 | static struct file_operations v4l_stk_fops = { | ||
1284 | .owner = THIS_MODULE, | ||
1285 | .open = v4l_stk_open, | ||
1286 | .release = v4l_stk_release, | ||
1287 | .read = v4l_stk_read, | ||
1288 | .poll = v4l_stk_poll, | ||
1289 | .mmap = v4l_stk_mmap, | ||
1290 | .ioctl = video_ioctl2, | ||
1291 | .llseek = no_llseek | ||
1292 | }; | ||
1293 | |||
1294 | static void stk_v4l_dev_release(struct video_device *vd) | ||
1295 | { | ||
1296 | } | ||
1297 | |||
1298 | static struct video_device stk_v4l_data = { | ||
1299 | .name = "stkwebcam", | ||
1300 | .type = VFL_TYPE_GRABBER, | ||
1301 | .type2 = VID_TYPE_CAPTURE, | ||
1302 | .minor = -1, | ||
1303 | .tvnorms = V4L2_STD_UNKNOWN, | ||
1304 | .current_norm = V4L2_STD_UNKNOWN, | ||
1305 | .fops = &v4l_stk_fops, | ||
1306 | .release = stk_v4l_dev_release, | ||
1307 | |||
1308 | .vidioc_querycap = stk_vidioc_querycap, | ||
1309 | .vidioc_enum_fmt_cap = stk_vidioc_enum_fmt_cap, | ||
1310 | .vidioc_try_fmt_cap = stk_vidioc_try_fmt_cap, | ||
1311 | .vidioc_s_fmt_cap = stk_vidioc_s_fmt_cap, | ||
1312 | .vidioc_g_fmt_cap = stk_vidioc_g_fmt_cap, | ||
1313 | .vidioc_enum_input = stk_vidioc_enum_input, | ||
1314 | .vidioc_s_input = stk_vidioc_s_input, | ||
1315 | .vidioc_g_input = stk_vidioc_g_input, | ||
1316 | .vidioc_s_std = stk_vidioc_s_std, | ||
1317 | .vidioc_reqbufs = stk_vidioc_reqbufs, | ||
1318 | .vidioc_querybuf = stk_vidioc_querybuf, | ||
1319 | .vidioc_qbuf = stk_vidioc_qbuf, | ||
1320 | .vidioc_dqbuf = stk_vidioc_dqbuf, | ||
1321 | .vidioc_streamon = stk_vidioc_streamon, | ||
1322 | .vidioc_streamoff = stk_vidioc_streamoff, | ||
1323 | .vidioc_queryctrl = stk_vidioc_queryctrl, | ||
1324 | .vidioc_g_ctrl = stk_vidioc_g_ctrl, | ||
1325 | .vidioc_s_ctrl = stk_vidioc_s_ctrl, | ||
1326 | .vidioc_g_parm = stk_vidioc_g_parm, | ||
1327 | }; | ||
1328 | |||
1329 | |||
1330 | static int stk_register_video_device(struct stk_camera *dev) | ||
1331 | { | ||
1332 | int err; | ||
1333 | |||
1334 | dev->vdev = stk_v4l_data; | ||
1335 | dev->vdev.debug = debug; | ||
1336 | dev->vdev.dev = &dev->interface->dev; | ||
1337 | dev->vdev.priv = dev; | ||
1338 | err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1); | ||
1339 | if (err) | ||
1340 | STK_ERROR("v4l registration failed\n"); | ||
1341 | else | ||
1342 | STK_INFO("Syntek USB2.0 Camera is now controlling video device" | ||
1343 | " /dev/video%d\n", dev->vdev.minor); | ||
1344 | return err; | ||
1345 | } | ||
1346 | |||
1347 | |||
1348 | /* USB Stuff */ | ||
1349 | |||
1350 | static int stk_camera_probe(struct usb_interface *interface, | ||
1351 | const struct usb_device_id *id) | ||
1352 | { | ||
1353 | int i; | ||
1354 | int err; | ||
1355 | |||
1356 | struct stk_camera *dev = NULL; | ||
1357 | struct usb_device *udev = interface_to_usbdev(interface); | ||
1358 | struct usb_host_interface *iface_desc; | ||
1359 | struct usb_endpoint_descriptor *endpoint; | ||
1360 | |||
1361 | dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL); | ||
1362 | if (dev == NULL) { | ||
1363 | STK_ERROR("Out of memory !\n"); | ||
1364 | return -ENOMEM; | ||
1365 | } | ||
1366 | |||
1367 | kref_init(&dev->kref); | ||
1368 | spin_lock_init(&dev->spinlock); | ||
1369 | init_waitqueue_head(&dev->wait_frame); | ||
1370 | |||
1371 | dev->udev = udev; | ||
1372 | dev->interface = interface; | ||
1373 | usb_get_intf(interface); | ||
1374 | |||
1375 | dev->vsettings.vflip = vflip; | ||
1376 | dev->vsettings.hflip = hflip; | ||
1377 | dev->n_sbufs = 0; | ||
1378 | set_present(dev); | ||
1379 | |||
1380 | /* Set up the endpoint information | ||
1381 | * use only the first isoc-in endpoint | ||
1382 | * for the current alternate setting */ | ||
1383 | iface_desc = interface->cur_altsetting; | ||
1384 | |||
1385 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | ||
1386 | endpoint = &iface_desc->endpoint[i].desc; | ||
1387 | |||
1388 | if (!dev->isoc_ep | ||
1389 | && ((endpoint->bEndpointAddress | ||
1390 | & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) | ||
1391 | && ((endpoint->bmAttributes | ||
1392 | & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) { | ||
1393 | /* we found an isoc in endpoint */ | ||
1394 | dev->isoc_ep = (endpoint->bEndpointAddress & 0xF); | ||
1395 | break; | ||
1396 | } | ||
1397 | } | ||
1398 | if (!dev->isoc_ep) { | ||
1399 | STK_ERROR("Could not find isoc-in endpoint"); | ||
1400 | kref_put(&dev->kref, stk_camera_cleanup); | ||
1401 | return -ENODEV; | ||
1402 | } | ||
1403 | dev->vsettings.brightness = 0x7fff; | ||
1404 | dev->vsettings.palette = V4L2_PIX_FMT_RGB565; | ||
1405 | dev->vsettings.mode = MODE_VGA; | ||
1406 | dev->frame_size = 640*480*2; | ||
1407 | |||
1408 | INIT_LIST_HEAD(&dev->sio_avail); | ||
1409 | INIT_LIST_HEAD(&dev->sio_full); | ||
1410 | |||
1411 | usb_set_intfdata(interface, dev); | ||
1412 | |||
1413 | err = stk_register_video_device(dev); | ||
1414 | if (err) { | ||
1415 | kref_put(&dev->kref, stk_camera_cleanup); | ||
1416 | return err; | ||
1417 | } | ||
1418 | |||
1419 | stk_create_sysfs_files(&dev->vdev); | ||
1420 | |||
1421 | return 0; | ||
1422 | } | ||
1423 | |||
1424 | static void stk_camera_disconnect(struct usb_interface *interface) | ||
1425 | { | ||
1426 | struct stk_camera *dev = usb_get_intfdata(interface); | ||
1427 | |||
1428 | usb_set_intfdata(interface, NULL); | ||
1429 | unset_present(dev); | ||
1430 | |||
1431 | wake_up_interruptible(&dev->wait_frame); | ||
1432 | stk_remove_sysfs_files(&dev->vdev); | ||
1433 | |||
1434 | kref_put(&dev->kref, stk_camera_cleanup); | ||
1435 | } | ||
1436 | |||
1437 | static struct usb_driver stk_camera_driver = { | ||
1438 | .name = "stkwebcam", | ||
1439 | .probe = stk_camera_probe, | ||
1440 | .disconnect = stk_camera_disconnect, | ||
1441 | .id_table = stkwebcam_table, | ||
1442 | }; | ||
1443 | |||
1444 | |||
1445 | static int __init stk_camera_init(void) | ||
1446 | { | ||
1447 | int result; | ||
1448 | |||
1449 | result = usb_register(&stk_camera_driver); | ||
1450 | if (result) | ||
1451 | STK_ERROR("usb_register failed ! Error number %d\n", result); | ||
1452 | |||
1453 | |||
1454 | return result; | ||
1455 | } | ||
1456 | |||
1457 | static void __exit stk_camera_exit(void) | ||
1458 | { | ||
1459 | usb_deregister(&stk_camera_driver); | ||
1460 | } | ||
1461 | |||
1462 | module_init(stk_camera_init); | ||
1463 | module_exit(stk_camera_exit); | ||
1464 | |||
1465 | |||