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