V4L/DVB (8482): videodev: move all ioctl callbacks to a new v4l2_ioctl_ops struct
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / stk-webcam.c
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/mm.h>
34 #include <linux/vmalloc.h>
35 #include <linux/videodev2.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
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
68 static void stk_camera_cleanup(struct kref *kref)
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
250 #ifdef CONFIG_VIDEO_V4L1_COMPAT
251
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
344 ret = video_device_create_file(vdev, &dev_attr_brightness);
345 ret += video_device_create_file(vdev, &dev_attr_hflip);
346 ret += video_device_create_file(vdev, &dev_attr_vflip);
347 return ret;
348 }
349
350 static void stk_remove_sysfs_files(struct video_device *vdev)
351 {
352 video_device_remove_file(vdev, &dev_attr_brightness);
353 video_device_remove_file(vdev, &dev_attr_hflip);
354 video_device_remove_file(vdev, &dev_attr_vflip);
355 }
356
357 #else
358 #define stk_create_sysfs_files(a)
359 #define stk_remove_sysfs_files(a)
360 #endif
361
362 /* *********************************************** */
363 /*
364 * This function is called as an URB transfert is complete (Isochronous pipe).
365 * So, the traitement is done in interrupt time, so it has be fast, not crash,
366 * and not stall. Neat.
367 */
368 static void stk_isoc_handler(struct urb *urb)
369 {
370 int i;
371 int ret;
372 int framelen;
373 unsigned long flags;
374
375 unsigned char *fill = NULL;
376 unsigned char *iso_buf = NULL;
377
378 struct stk_camera *dev;
379 struct stk_sio_buffer *fb;
380
381 dev = (struct stk_camera *) urb->context;
382
383 if (dev == NULL) {
384 STK_ERROR("isoc_handler called with NULL device !\n");
385 return;
386 }
387
388 if (urb->status == -ENOENT || urb->status == -ECONNRESET
389 || urb->status == -ESHUTDOWN) {
390 atomic_dec(&dev->urbs_used);
391 return;
392 }
393
394 spin_lock_irqsave(&dev->spinlock, flags);
395
396 if (urb->status != -EINPROGRESS && urb->status != 0) {
397 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
398 goto resubmit;
399 }
400
401 if (list_empty(&dev->sio_avail)) {
402 /*FIXME Stop streaming after a while */
403 (void) (printk_ratelimit() &&
404 STK_ERROR("isoc_handler without available buffer!\n"));
405 goto resubmit;
406 }
407 fb = list_first_entry(&dev->sio_avail,
408 struct stk_sio_buffer, list);
409 fill = fb->buffer + fb->v4lbuf.bytesused;
410
411 for (i = 0; i < urb->number_of_packets; i++) {
412 if (urb->iso_frame_desc[i].status != 0) {
413 if (urb->iso_frame_desc[i].status != -EXDEV)
414 STK_ERROR("Frame %d has error %d\n", i,
415 urb->iso_frame_desc[i].status);
416 continue;
417 }
418 framelen = urb->iso_frame_desc[i].actual_length;
419 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
420
421 if (framelen <= 4)
422 continue; /* no data */
423
424 /*
425 * we found something informational from there
426 * the isoc frames have to type of headers
427 * type1: 00 xx 00 00 or 20 xx 00 00
428 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
429 * xx is a sequencer which has never been seen over 0x3f
430 * imho data written down looks like bayer, i see similarities
431 * after every 640 bytes
432 */
433 if (*iso_buf & 0x80) {
434 framelen -= 8;
435 iso_buf += 8;
436 /* This marks a new frame */
437 if (fb->v4lbuf.bytesused != 0
438 && fb->v4lbuf.bytesused != dev->frame_size) {
439 (void) (printk_ratelimit() &&
440 STK_ERROR("frame %d, "
441 "bytesused=%d, skipping\n",
442 i, fb->v4lbuf.bytesused));
443 fb->v4lbuf.bytesused = 0;
444 fill = fb->buffer;
445 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
446 list_move_tail(dev->sio_avail.next,
447 &dev->sio_full);
448 wake_up(&dev->wait_frame);
449 if (list_empty(&dev->sio_avail)) {
450 (void) (printk_ratelimit() &&
451 STK_ERROR("No buffer available\n"));
452 goto resubmit;
453 }
454 fb = list_first_entry(&dev->sio_avail,
455 struct stk_sio_buffer, list);
456 fb->v4lbuf.bytesused = 0;
457 fill = fb->buffer;
458 }
459 } else {
460 framelen -= 4;
461 iso_buf += 4;
462 }
463
464 /* Our buffer is full !!! */
465 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
466 (void) (printk_ratelimit() &&
467 STK_ERROR("Frame buffer overflow, lost sync\n"));
468 /*FIXME Do something here? */
469 continue;
470 }
471 spin_unlock_irqrestore(&dev->spinlock, flags);
472 memcpy(fill, iso_buf, framelen);
473 spin_lock_irqsave(&dev->spinlock, flags);
474 fill += framelen;
475
476 /* New size of our buffer */
477 fb->v4lbuf.bytesused += framelen;
478 }
479
480 resubmit:
481 spin_unlock_irqrestore(&dev->spinlock, flags);
482 urb->dev = dev->udev;
483 ret = usb_submit_urb(urb, GFP_ATOMIC);
484 if (ret != 0) {
485 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
486 ret);
487 }
488 }
489
490 /* -------------------------------------------- */
491
492 static int stk_prepare_iso(struct stk_camera *dev)
493 {
494 void *kbuf;
495 int i, j;
496 struct urb *urb;
497 struct usb_device *udev;
498
499 if (dev == NULL)
500 return -ENXIO;
501 udev = dev->udev;
502
503 if (dev->isobufs)
504 STK_ERROR("isobufs already allocated. Bad\n");
505 else
506 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
507 GFP_KERNEL);
508 if (dev->isobufs == NULL) {
509 STK_ERROR("Unable to allocate iso buffers\n");
510 return -ENOMEM;
511 }
512 for (i = 0; i < MAX_ISO_BUFS; i++) {
513 if (dev->isobufs[i].data == NULL) {
514 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
515 if (kbuf == NULL) {
516 STK_ERROR("Failed to allocate iso buffer %d\n",
517 i);
518 goto isobufs_out;
519 }
520 dev->isobufs[i].data = kbuf;
521 } else
522 STK_ERROR("isobuf data already allocated\n");
523 if (dev->isobufs[i].urb == NULL) {
524 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
525 if (urb == NULL) {
526 STK_ERROR("Failed to allocate URB %d\n", i);
527 goto isobufs_out;
528 }
529 dev->isobufs[i].urb = urb;
530 } else {
531 STK_ERROR("Killing URB\n");
532 usb_kill_urb(dev->isobufs[i].urb);
533 urb = dev->isobufs[i].urb;
534 }
535 urb->interval = 1;
536 urb->dev = udev;
537 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
538 urb->transfer_flags = URB_ISO_ASAP;
539 urb->transfer_buffer = dev->isobufs[i].data;
540 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
541 urb->complete = stk_isoc_handler;
542 urb->context = dev;
543 urb->start_frame = 0;
544 urb->number_of_packets = ISO_FRAMES_PER_DESC;
545
546 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
547 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
548 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
549 }
550 }
551 set_memallocd(dev);
552 return 0;
553
554 isobufs_out:
555 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
556 kfree(dev->isobufs[i].data);
557 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
558 usb_free_urb(dev->isobufs[i].urb);
559 kfree(dev->isobufs);
560 dev->isobufs = NULL;
561 return -ENOMEM;
562 }
563
564 static void stk_clean_iso(struct stk_camera *dev)
565 {
566 int i;
567
568 if (dev == NULL || dev->isobufs == NULL)
569 return;
570
571 for (i = 0; i < MAX_ISO_BUFS; i++) {
572 struct urb *urb;
573
574 urb = dev->isobufs[i].urb;
575 if (urb) {
576 if (atomic_read(&dev->urbs_used))
577 usb_kill_urb(urb);
578 usb_free_urb(urb);
579 }
580 kfree(dev->isobufs[i].data);
581 }
582 kfree(dev->isobufs);
583 dev->isobufs = NULL;
584 unset_memallocd(dev);
585 }
586
587 static int stk_setup_siobuf(struct stk_camera *dev, int index)
588 {
589 struct stk_sio_buffer *buf = dev->sio_bufs + index;
590 INIT_LIST_HEAD(&buf->list);
591 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
592 buf->buffer = vmalloc_user(buf->v4lbuf.length);
593 if (buf->buffer == NULL)
594 return -ENOMEM;
595 buf->mapcount = 0;
596 buf->dev = dev;
597 buf->v4lbuf.index = index;
598 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
599 buf->v4lbuf.field = V4L2_FIELD_NONE;
600 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
601 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
602 return 0;
603 }
604
605 static int stk_free_sio_buffers(struct stk_camera *dev)
606 {
607 int i;
608 int nbufs;
609 unsigned long flags;
610 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
611 return 0;
612 /*
613 * If any buffers are mapped, we cannot free them at all.
614 */
615 for (i = 0; i < dev->n_sbufs; i++) {
616 if (dev->sio_bufs[i].mapcount > 0)
617 return -EBUSY;
618 }
619 /*
620 * OK, let's do it.
621 */
622 spin_lock_irqsave(&dev->spinlock, flags);
623 INIT_LIST_HEAD(&dev->sio_avail);
624 INIT_LIST_HEAD(&dev->sio_full);
625 nbufs = dev->n_sbufs;
626 dev->n_sbufs = 0;
627 spin_unlock_irqrestore(&dev->spinlock, flags);
628 for (i = 0; i < nbufs; i++) {
629 if (dev->sio_bufs[i].buffer != NULL)
630 vfree(dev->sio_bufs[i].buffer);
631 }
632 kfree(dev->sio_bufs);
633 dev->sio_bufs = NULL;
634 return 0;
635 }
636
637 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
638 {
639 int i;
640 if (dev->sio_bufs != NULL)
641 STK_ERROR("sio_bufs already allocated\n");
642 else {
643 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
644 GFP_KERNEL);
645 if (dev->sio_bufs == NULL)
646 return -ENOMEM;
647 for (i = 0; i < n_sbufs; i++) {
648 if (stk_setup_siobuf(dev, i))
649 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
650 dev->n_sbufs = i+1;
651 }
652 }
653 return 0;
654 }
655
656 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
657 {
658 int err;
659 err = stk_prepare_iso(dev);
660 if (err) {
661 stk_clean_iso(dev);
662 return err;
663 }
664 err = stk_prepare_sio_buffers(dev, n_sbufs);
665 if (err) {
666 stk_free_sio_buffers(dev);
667 return err;
668 }
669 return 0;
670 }
671
672 static void stk_free_buffers(struct stk_camera *dev)
673 {
674 stk_clean_iso(dev);
675 stk_free_sio_buffers(dev);
676 }
677 /* -------------------------------------------- */
678
679 /* v4l file operations */
680
681 static int v4l_stk_open(struct inode *inode, struct file *fp)
682 {
683 struct stk_camera *dev;
684 struct video_device *vdev;
685
686 vdev = video_devdata(fp);
687 dev = vdev_to_camera(vdev);
688
689 if (dev == NULL || !is_present(dev))
690 return -ENXIO;
691 fp->private_data = vdev;
692 kref_get(&dev->kref);
693 usb_autopm_get_interface(dev->interface);
694
695 return 0;
696 }
697
698 static int v4l_stk_release(struct inode *inode, struct file *fp)
699 {
700 struct stk_camera *dev;
701 struct video_device *vdev;
702
703 vdev = video_devdata(fp);
704 if (vdev == NULL) {
705 STK_ERROR("v4l_release called w/o video devdata\n");
706 return -EFAULT;
707 }
708 dev = vdev_to_camera(vdev);
709 if (dev == NULL) {
710 STK_ERROR("v4l_release called on removed device\n");
711 return -ENODEV;
712 }
713
714 if (dev->owner != fp) {
715 usb_autopm_put_interface(dev->interface);
716 kref_put(&dev->kref, stk_camera_cleanup);
717 return 0;
718 }
719
720 stk_stop_stream(dev);
721
722 stk_free_buffers(dev);
723
724 dev->owner = NULL;
725
726 usb_autopm_put_interface(dev->interface);
727 kref_put(&dev->kref, stk_camera_cleanup);
728
729 return 0;
730 }
731
732 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
733 size_t count, loff_t *f_pos)
734 {
735 int i;
736 int ret;
737 unsigned long flags;
738 struct stk_camera *dev;
739 struct video_device *vdev;
740 struct stk_sio_buffer *sbuf;
741
742 vdev = video_devdata(fp);
743 if (vdev == NULL)
744 return -EFAULT;
745 dev = vdev_to_camera(vdev);
746
747 if (dev == NULL)
748 return -EIO;
749
750 if (!is_present(dev))
751 return -EIO;
752 if (dev->owner && dev->owner != fp)
753 return -EBUSY;
754 dev->owner = fp;
755 if (!is_streaming(dev)) {
756 if (stk_initialise(dev)
757 || stk_allocate_buffers(dev, 3)
758 || stk_start_stream(dev))
759 return -ENOMEM;
760 spin_lock_irqsave(&dev->spinlock, flags);
761 for (i = 0; i < dev->n_sbufs; i++) {
762 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
763 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
764 }
765 spin_unlock_irqrestore(&dev->spinlock, flags);
766 }
767 if (*f_pos == 0) {
768 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
769 return -EWOULDBLOCK;
770 ret = wait_event_interruptible(dev->wait_frame,
771 !list_empty(&dev->sio_full) || !is_present(dev));
772 if (ret)
773 return ret;
774 if (!is_present(dev))
775 return -EIO;
776 }
777 if (count + *f_pos > dev->frame_size)
778 count = dev->frame_size - *f_pos;
779 spin_lock_irqsave(&dev->spinlock, flags);
780 if (list_empty(&dev->sio_full)) {
781 spin_unlock_irqrestore(&dev->spinlock, flags);
782 STK_ERROR("BUG: No siobufs ready\n");
783 return 0;
784 }
785 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
786 spin_unlock_irqrestore(&dev->spinlock, flags);
787
788 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
789 return -EFAULT;
790
791 *f_pos += count;
792
793 if (*f_pos >= dev->frame_size) {
794 *f_pos = 0;
795 spin_lock_irqsave(&dev->spinlock, flags);
796 list_move_tail(&sbuf->list, &dev->sio_avail);
797 spin_unlock_irqrestore(&dev->spinlock, flags);
798 }
799 return count;
800 }
801
802 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
803 {
804 struct stk_camera *dev;
805 struct video_device *vdev;
806
807 vdev = video_devdata(fp);
808
809 if (vdev == NULL)
810 return -EFAULT;
811
812 dev = vdev_to_camera(vdev);
813 if (dev == NULL)
814 return -ENODEV;
815
816 poll_wait(fp, &dev->wait_frame, wait);
817
818 if (!is_present(dev))
819 return POLLERR;
820
821 if (!list_empty(&dev->sio_full))
822 return (POLLIN | POLLRDNORM);
823
824 return 0;
825 }
826
827
828 static void stk_v4l_vm_open(struct vm_area_struct *vma)
829 {
830 struct stk_sio_buffer *sbuf = vma->vm_private_data;
831 sbuf->mapcount++;
832 }
833 static void stk_v4l_vm_close(struct vm_area_struct *vma)
834 {
835 struct stk_sio_buffer *sbuf = vma->vm_private_data;
836 sbuf->mapcount--;
837 if (sbuf->mapcount == 0)
838 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
839 }
840 static struct vm_operations_struct stk_v4l_vm_ops = {
841 .open = stk_v4l_vm_open,
842 .close = stk_v4l_vm_close
843 };
844
845 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
846 {
847 unsigned int i;
848 int ret;
849 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
850 struct stk_camera *dev;
851 struct video_device *vdev;
852 struct stk_sio_buffer *sbuf = NULL;
853
854 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
855 return -EINVAL;
856
857 vdev = video_devdata(fp);
858 dev = vdev_to_camera(vdev);
859
860 for (i = 0; i < dev->n_sbufs; i++) {
861 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
862 sbuf = dev->sio_bufs + i;
863 break;
864 }
865 }
866 if (sbuf == NULL)
867 return -EINVAL;
868 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
869 if (ret)
870 return ret;
871 vma->vm_flags |= VM_DONTEXPAND;
872 vma->vm_private_data = sbuf;
873 vma->vm_ops = &stk_v4l_vm_ops;
874 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
875 stk_v4l_vm_open(vma);
876 return 0;
877 }
878
879 /* v4l ioctl handlers */
880
881 static int stk_vidioc_querycap(struct file *filp,
882 void *priv, struct v4l2_capability *cap)
883 {
884 strcpy(cap->driver, "stk");
885 strcpy(cap->card, "stk");
886 cap->version = DRIVER_VERSION_NUM;
887
888 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
889 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
890 return 0;
891 }
892
893 static int stk_vidioc_enum_input(struct file *filp,
894 void *priv, struct v4l2_input *input)
895 {
896 if (input->index != 0)
897 return -EINVAL;
898
899 strcpy(input->name, "Syntek USB Camera");
900 input->type = V4L2_INPUT_TYPE_CAMERA;
901 return 0;
902 }
903
904
905 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
906 {
907 *i = 0;
908 return 0;
909 }
910
911 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
912 {
913 if (i != 0)
914 return -EINVAL;
915 else
916 return 0;
917 }
918
919 /* from vivi.c */
920 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
921 {
922 return 0;
923 }
924
925 /* List of all V4Lv2 controls supported by the driver */
926 static struct v4l2_queryctrl stk_controls[] = {
927 {
928 .id = V4L2_CID_BRIGHTNESS,
929 .type = V4L2_CTRL_TYPE_INTEGER,
930 .name = "Brightness",
931 .minimum = 0,
932 .maximum = 0xffff,
933 .step = 0x0100,
934 .default_value = 0x6000,
935 },
936 /*TODO: get more controls to work */
937 };
938
939 static int stk_vidioc_queryctrl(struct file *filp,
940 void *priv, struct v4l2_queryctrl *c)
941 {
942 int i;
943 int nbr;
944 nbr = ARRAY_SIZE(stk_controls);
945
946 for (i = 0; i < nbr; i++) {
947 if (stk_controls[i].id == c->id) {
948 memcpy(c, &stk_controls[i],
949 sizeof(struct v4l2_queryctrl));
950 return 0;
951 }
952 }
953 return -EINVAL;
954 }
955
956 static int stk_vidioc_g_ctrl(struct file *filp,
957 void *priv, struct v4l2_control *c)
958 {
959 struct stk_camera *dev = priv;
960 switch (c->id) {
961 case V4L2_CID_BRIGHTNESS:
962 c->value = dev->vsettings.brightness;
963 break;
964 default:
965 return -EINVAL;
966 }
967 return 0;
968 }
969
970 static int stk_vidioc_s_ctrl(struct file *filp,
971 void *priv, struct v4l2_control *c)
972 {
973 struct stk_camera *dev = priv;
974 switch (c->id) {
975 case V4L2_CID_BRIGHTNESS:
976 dev->vsettings.brightness = c->value;
977 return stk_sensor_set_brightness(dev, c->value >> 8);
978 default:
979 return -EINVAL;
980 }
981 return 0;
982 }
983
984
985 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
986 void *priv, struct v4l2_fmtdesc *fmtd)
987 {
988 fmtd->flags = 0;
989
990 switch (fmtd->index) {
991 case 0:
992 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
993 strcpy(fmtd->description, "r5g6b5");
994 break;
995 case 1:
996 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
997 strcpy(fmtd->description, "r5g6b5BE");
998 break;
999 case 2:
1000 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
1001 strcpy(fmtd->description, "yuv4:2:2");
1002 break;
1003 case 3:
1004 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
1005 strcpy(fmtd->description, "Raw bayer");
1006 break;
1007 case 4:
1008 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1009 strcpy(fmtd->description, "yuv4:2:2");
1010 break;
1011 default:
1012 return -EINVAL;
1013 }
1014 return 0;
1015 }
1016
1017 static struct stk_size {
1018 unsigned w;
1019 unsigned h;
1020 enum stk_mode m;
1021 } stk_sizes[] = {
1022 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1023 { .w = 640, .h = 480, .m = MODE_VGA, },
1024 { .w = 352, .h = 288, .m = MODE_CIF, },
1025 { .w = 320, .h = 240, .m = MODE_QVGA, },
1026 { .w = 176, .h = 144, .m = MODE_QCIF, },
1027 };
1028
1029 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
1030 void *priv, struct v4l2_format *f)
1031 {
1032 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1033 struct stk_camera *dev = priv;
1034 int i;
1035
1036 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1037 && stk_sizes[i].m != dev->vsettings.mode;
1038 i++);
1039 if (i == ARRAY_SIZE(stk_sizes)) {
1040 STK_ERROR("ERROR: mode invalid\n");
1041 return -EINVAL;
1042 }
1043 pix_format->width = stk_sizes[i].w;
1044 pix_format->height = stk_sizes[i].h;
1045 pix_format->field = V4L2_FIELD_NONE;
1046 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1047 pix_format->priv = 0;
1048 pix_format->pixelformat = dev->vsettings.palette;
1049 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1050 pix_format->bytesperline = pix_format->width;
1051 else
1052 pix_format->bytesperline = 2 * pix_format->width;
1053 pix_format->sizeimage = pix_format->bytesperline
1054 * pix_format->height;
1055 return 0;
1056 }
1057
1058 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
1059 void *priv, struct v4l2_format *fmtd)
1060 {
1061 int i;
1062 switch (fmtd->fmt.pix.pixelformat) {
1063 case V4L2_PIX_FMT_RGB565:
1064 case V4L2_PIX_FMT_RGB565X:
1065 case V4L2_PIX_FMT_UYVY:
1066 case V4L2_PIX_FMT_YUYV:
1067 case V4L2_PIX_FMT_SBGGR8:
1068 break;
1069 default:
1070 return -EINVAL;
1071 }
1072 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1073 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1074 break;
1075 }
1076 if (i == ARRAY_SIZE(stk_sizes)
1077 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1078 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1079 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1080 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1081 fmtd->fmt.pix.priv = i - 1;
1082 } else {
1083 fmtd->fmt.pix.height = stk_sizes[i].h;
1084 fmtd->fmt.pix.width = stk_sizes[i].w;
1085 fmtd->fmt.pix.priv = i;
1086 }
1087
1088 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1089 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1090 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1091 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1092 else
1093 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1094 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1095 * fmtd->fmt.pix.height;
1096 return 0;
1097 }
1098
1099 static int stk_setup_format(struct stk_camera *dev)
1100 {
1101 int i = 0;
1102 int depth;
1103 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1104 depth = 1;
1105 else
1106 depth = 2;
1107 while (stk_sizes[i].m != dev->vsettings.mode
1108 && i < ARRAY_SIZE(stk_sizes))
1109 i++;
1110 if (i == ARRAY_SIZE(stk_sizes)) {
1111 STK_ERROR("Something is broken in %s\n", __func__);
1112 return -EFAULT;
1113 }
1114 /* This registers controls some timings, not sure of what. */
1115 stk_camera_write_reg(dev, 0x001b, 0x0e);
1116 if (dev->vsettings.mode == MODE_SXGA)
1117 stk_camera_write_reg(dev, 0x001c, 0x0e);
1118 else
1119 stk_camera_write_reg(dev, 0x001c, 0x46);
1120 /*
1121 * Registers 0x0115 0x0114 are the size of each line (bytes),
1122 * regs 0x0117 0x0116 are the heigth of the image.
1123 */
1124 stk_camera_write_reg(dev, 0x0115,
1125 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1126 stk_camera_write_reg(dev, 0x0114,
1127 (stk_sizes[i].w * depth) & 0xff);
1128 stk_camera_write_reg(dev, 0x0117,
1129 (stk_sizes[i].h >> 8) & 0xff);
1130 stk_camera_write_reg(dev, 0x0116,
1131 stk_sizes[i].h & 0xff);
1132 return stk_sensor_configure(dev);
1133 }
1134
1135 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1136 void *priv, struct v4l2_format *fmtd)
1137 {
1138 int ret;
1139 struct stk_camera *dev = priv;
1140
1141 if (dev == NULL)
1142 return -ENODEV;
1143 if (!is_present(dev))
1144 return -ENODEV;
1145 if (is_streaming(dev))
1146 return -EBUSY;
1147 if (dev->owner && dev->owner != filp)
1148 return -EBUSY;
1149 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1150 if (ret)
1151 return ret;
1152 dev->owner = filp;
1153
1154 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1155 stk_free_buffers(dev);
1156 dev->frame_size = fmtd->fmt.pix.sizeimage;
1157 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1158
1159 stk_initialise(dev);
1160 return stk_setup_format(dev);
1161 }
1162
1163 static int stk_vidioc_reqbufs(struct file *filp,
1164 void *priv, struct v4l2_requestbuffers *rb)
1165 {
1166 struct stk_camera *dev = priv;
1167
1168 if (dev == NULL)
1169 return -ENODEV;
1170 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1171 return -EINVAL;
1172 if (rb->memory != V4L2_MEMORY_MMAP)
1173 return -EINVAL;
1174 if (is_streaming(dev)
1175 || (dev->owner && dev->owner != filp))
1176 return -EBUSY;
1177 dev->owner = filp;
1178
1179 /*FIXME If they ask for zero, we must stop streaming and free */
1180 if (rb->count < 3)
1181 rb->count = 3;
1182 /* Arbitrary limit */
1183 else if (rb->count > 5)
1184 rb->count = 5;
1185
1186 stk_allocate_buffers(dev, rb->count);
1187 rb->count = dev->n_sbufs;
1188 return 0;
1189 }
1190
1191 static int stk_vidioc_querybuf(struct file *filp,
1192 void *priv, struct v4l2_buffer *buf)
1193 {
1194 int index;
1195 struct stk_camera *dev = priv;
1196 struct stk_sio_buffer *sbuf;
1197
1198 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1199 return -EINVAL;
1200
1201 index = buf->index;
1202
1203 if (index < 0 || index >= dev->n_sbufs)
1204 return -EINVAL;
1205 sbuf = dev->sio_bufs + buf->index;
1206 *buf = sbuf->v4lbuf;
1207 return 0;
1208 }
1209
1210 static int stk_vidioc_qbuf(struct file *filp,
1211 void *priv, struct v4l2_buffer *buf)
1212 {
1213 struct stk_camera *dev = priv;
1214 struct stk_sio_buffer *sbuf;
1215 unsigned long flags;
1216 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1217 return -EINVAL;
1218
1219 if (buf->memory != V4L2_MEMORY_MMAP)
1220 return -EINVAL;
1221
1222 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1223 return -EINVAL;
1224 sbuf = dev->sio_bufs + buf->index;
1225 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1226 return 0;
1227 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1228 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1229 spin_lock_irqsave(&dev->spinlock, flags);
1230 list_add_tail(&sbuf->list, &dev->sio_avail);
1231 *buf = sbuf->v4lbuf;
1232 spin_unlock_irqrestore(&dev->spinlock, flags);
1233 return 0;
1234 }
1235
1236 static int stk_vidioc_dqbuf(struct file *filp,
1237 void *priv, struct v4l2_buffer *buf)
1238 {
1239 struct stk_camera *dev = priv;
1240 struct stk_sio_buffer *sbuf;
1241 unsigned long flags;
1242 int ret;
1243
1244 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1245 || !is_streaming(dev))
1246 return -EINVAL;
1247
1248 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1249 return -EWOULDBLOCK;
1250 ret = wait_event_interruptible(dev->wait_frame,
1251 !list_empty(&dev->sio_full) || !is_present(dev));
1252 if (ret)
1253 return ret;
1254 if (!is_present(dev))
1255 return -EIO;
1256
1257 spin_lock_irqsave(&dev->spinlock, flags);
1258 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1259 list_del_init(&sbuf->list);
1260 spin_unlock_irqrestore(&dev->spinlock, flags);
1261 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1262 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1263 sbuf->v4lbuf.sequence = ++dev->sequence;
1264 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1265
1266 *buf = sbuf->v4lbuf;
1267 return 0;
1268 }
1269
1270 static int stk_vidioc_streamon(struct file *filp,
1271 void *priv, enum v4l2_buf_type type)
1272 {
1273 struct stk_camera *dev = priv;
1274 if (is_streaming(dev))
1275 return 0;
1276 if (dev->sio_bufs == NULL)
1277 return -EINVAL;
1278 dev->sequence = 0;
1279 return stk_start_stream(dev);
1280 }
1281
1282 static int stk_vidioc_streamoff(struct file *filp,
1283 void *priv, enum v4l2_buf_type type)
1284 {
1285 struct stk_camera *dev = priv;
1286 unsigned long flags;
1287 int i;
1288 stk_stop_stream(dev);
1289 spin_lock_irqsave(&dev->spinlock, flags);
1290 INIT_LIST_HEAD(&dev->sio_avail);
1291 INIT_LIST_HEAD(&dev->sio_full);
1292 for (i = 0; i < dev->n_sbufs; i++) {
1293 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1294 dev->sio_bufs[i].v4lbuf.flags = 0;
1295 }
1296 spin_unlock_irqrestore(&dev->spinlock, flags);
1297 return 0;
1298 }
1299
1300
1301 static int stk_vidioc_g_parm(struct file *filp,
1302 void *priv, struct v4l2_streamparm *sp)
1303 {
1304 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1305 return -EINVAL;
1306
1307 sp->parm.capture.capability = 0;
1308 sp->parm.capture.capturemode = 0;
1309 /*FIXME This is not correct */
1310 sp->parm.capture.timeperframe.numerator = 1;
1311 sp->parm.capture.timeperframe.denominator = 30;
1312 sp->parm.capture.readbuffers = 2;
1313 sp->parm.capture.extendedmode = 0;
1314 return 0;
1315 }
1316
1317 static struct file_operations v4l_stk_fops = {
1318 .owner = THIS_MODULE,
1319 .open = v4l_stk_open,
1320 .release = v4l_stk_release,
1321 .read = v4l_stk_read,
1322 .poll = v4l_stk_poll,
1323 .mmap = v4l_stk_mmap,
1324 .ioctl = video_ioctl2,
1325 #ifdef CONFIG_COMPAT
1326 .compat_ioctl = v4l_compat_ioctl32,
1327 #endif
1328 .llseek = no_llseek
1329 };
1330
1331 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1332 .vidioc_querycap = stk_vidioc_querycap,
1333 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1334 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1335 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1336 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1337 .vidioc_enum_input = stk_vidioc_enum_input,
1338 .vidioc_s_input = stk_vidioc_s_input,
1339 .vidioc_g_input = stk_vidioc_g_input,
1340 .vidioc_s_std = stk_vidioc_s_std,
1341 .vidioc_reqbufs = stk_vidioc_reqbufs,
1342 .vidioc_querybuf = stk_vidioc_querybuf,
1343 .vidioc_qbuf = stk_vidioc_qbuf,
1344 .vidioc_dqbuf = stk_vidioc_dqbuf,
1345 .vidioc_streamon = stk_vidioc_streamon,
1346 .vidioc_streamoff = stk_vidioc_streamoff,
1347 .vidioc_queryctrl = stk_vidioc_queryctrl,
1348 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1349 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1350 .vidioc_g_parm = stk_vidioc_g_parm,
1351 };
1352
1353 static void stk_v4l_dev_release(struct video_device *vd)
1354 {
1355 }
1356
1357 static struct video_device stk_v4l_data = {
1358 .name = "stkwebcam",
1359 .type = VFL_TYPE_GRABBER,
1360 .type2 = VID_TYPE_CAPTURE,
1361 .minor = -1,
1362 .tvnorms = V4L2_STD_UNKNOWN,
1363 .current_norm = V4L2_STD_UNKNOWN,
1364 .fops = &v4l_stk_fops,
1365 .ioctl_ops = &v4l_stk_ioctl_ops,
1366 .release = stk_v4l_dev_release,
1367 };
1368
1369
1370 static int stk_register_video_device(struct stk_camera *dev)
1371 {
1372 int err;
1373
1374 dev->vdev = stk_v4l_data;
1375 dev->vdev.debug = debug;
1376 dev->vdev.parent = &dev->interface->dev;
1377 dev->vdev.priv = dev;
1378 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1379 if (err)
1380 STK_ERROR("v4l registration failed\n");
1381 else
1382 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1383 " /dev/video%d\n", dev->vdev.minor);
1384 return err;
1385 }
1386
1387
1388 /* USB Stuff */
1389
1390 static int stk_camera_probe(struct usb_interface *interface,
1391 const struct usb_device_id *id)
1392 {
1393 int i;
1394 int err;
1395
1396 struct stk_camera *dev = NULL;
1397 struct usb_device *udev = interface_to_usbdev(interface);
1398 struct usb_host_interface *iface_desc;
1399 struct usb_endpoint_descriptor *endpoint;
1400
1401 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1402 if (dev == NULL) {
1403 STK_ERROR("Out of memory !\n");
1404 return -ENOMEM;
1405 }
1406
1407 kref_init(&dev->kref);
1408 spin_lock_init(&dev->spinlock);
1409 init_waitqueue_head(&dev->wait_frame);
1410
1411 dev->udev = udev;
1412 dev->interface = interface;
1413 usb_get_intf(interface);
1414
1415 dev->vsettings.vflip = vflip;
1416 dev->vsettings.hflip = hflip;
1417 dev->n_sbufs = 0;
1418 set_present(dev);
1419
1420 /* Set up the endpoint information
1421 * use only the first isoc-in endpoint
1422 * for the current alternate setting */
1423 iface_desc = interface->cur_altsetting;
1424
1425 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1426 endpoint = &iface_desc->endpoint[i].desc;
1427
1428 if (!dev->isoc_ep
1429 && ((endpoint->bEndpointAddress
1430 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1431 && ((endpoint->bmAttributes
1432 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1433 /* we found an isoc in endpoint */
1434 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1435 break;
1436 }
1437 }
1438 if (!dev->isoc_ep) {
1439 STK_ERROR("Could not find isoc-in endpoint");
1440 kref_put(&dev->kref, stk_camera_cleanup);
1441 return -ENODEV;
1442 }
1443 dev->vsettings.brightness = 0x7fff;
1444 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1445 dev->vsettings.mode = MODE_VGA;
1446 dev->frame_size = 640 * 480 * 2;
1447
1448 INIT_LIST_HEAD(&dev->sio_avail);
1449 INIT_LIST_HEAD(&dev->sio_full);
1450
1451 usb_set_intfdata(interface, dev);
1452
1453 err = stk_register_video_device(dev);
1454 if (err) {
1455 kref_put(&dev->kref, stk_camera_cleanup);
1456 return err;
1457 }
1458
1459 stk_create_sysfs_files(&dev->vdev);
1460 usb_autopm_enable(dev->interface);
1461
1462 return 0;
1463 }
1464
1465 static void stk_camera_disconnect(struct usb_interface *interface)
1466 {
1467 struct stk_camera *dev = usb_get_intfdata(interface);
1468
1469 usb_set_intfdata(interface, NULL);
1470 unset_present(dev);
1471
1472 wake_up_interruptible(&dev->wait_frame);
1473 stk_remove_sysfs_files(&dev->vdev);
1474
1475 kref_put(&dev->kref, stk_camera_cleanup);
1476 }
1477
1478 #ifdef CONFIG_PM
1479 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1480 {
1481 struct stk_camera *dev = usb_get_intfdata(intf);
1482 if (is_streaming(dev)) {
1483 stk_stop_stream(dev);
1484 /* yes, this is ugly */
1485 set_streaming(dev);
1486 }
1487 return 0;
1488 }
1489
1490 static int stk_camera_resume(struct usb_interface *intf)
1491 {
1492 struct stk_camera *dev = usb_get_intfdata(intf);
1493 if (!is_initialised(dev))
1494 return 0;
1495 unset_initialised(dev);
1496 stk_initialise(dev);
1497 stk_setup_format(dev);
1498 if (is_streaming(dev))
1499 stk_start_stream(dev);
1500 return 0;
1501 }
1502 #endif
1503
1504 static struct usb_driver stk_camera_driver = {
1505 .name = "stkwebcam",
1506 .probe = stk_camera_probe,
1507 .disconnect = stk_camera_disconnect,
1508 .id_table = stkwebcam_table,
1509 #ifdef CONFIG_PM
1510 .suspend = stk_camera_suspend,
1511 .resume = stk_camera_resume,
1512 #endif
1513 };
1514
1515
1516 static int __init stk_camera_init(void)
1517 {
1518 int result;
1519
1520 result = usb_register(&stk_camera_driver);
1521 if (result)
1522 STK_ERROR("usb_register failed ! Error number %d\n", result);
1523
1524
1525 return result;
1526 }
1527
1528 static void __exit stk_camera_exit(void)
1529 {
1530 usb_deregister(&stk_camera_driver);
1531 }
1532
1533 module_init(stk_camera_init);
1534 module_exit(stk_camera_exit);
1535
1536