Add kernel version check, for list_is_singular
[stkwebcam.git] / stk-webcam.c
blob24e3c1b8057f0a886929d8897bfbb99dc85c211b
1 /*
2 * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
4 * Copyright (C) 2006 Nicolas VIVIEN
5 * Copyright 2007 Jaime Velasco Juan <jsagarribay@gmail.com>
7 * Some parts are inspired from cafe_ccic.c
8 * Copyright 2006-2007 Jonathan Corbet
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.
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.
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
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>
32 #include <linux/usb.h>
33 #include <linux/videodev2.h>
34 #include <media/v4l2-common.h>
35 #include <linux/vmalloc.h>
36 #include <linux/version.h>
38 #include "stk-webcam.h"
41 static int hflip = 1;
42 module_param(hflip, bool, 0444);
43 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
45 static int vflip = 1;
46 module_param(vflip, bool, 0444);
47 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
49 static int debug = 0;
50 module_param(debug, int, 0444);
51 MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
53 MODULE_LICENSE("GPL");
54 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
55 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
59 /* Some cameras have audio interfaces, we aren't interested in those */
60 static struct usb_device_id stkwebcam_table[] = {
61 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
62 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
63 { }
65 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
67 void stk_camera_cleanup(struct kref *kref)
69 struct stk_camera *dev = to_stk_camera(kref);
71 STK_INFO("Syntek USB2.0 Camera release resources"
72 " video device /dev/video%d\n", dev->vdev.minor);
73 video_unregister_device(&dev->vdev);
74 dev->vdev.priv = NULL;
76 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
77 STK_ERROR("We are leaking memory\n");
78 usb_put_intf(dev->interface);
79 kfree(dev);
84 * Basic stuff
86 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
88 struct usb_device *udev = dev->udev;
89 int ret;
91 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
92 0x01,
93 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
94 value,
95 index,
96 NULL,
98 500);
99 if (ret < 0)
100 return ret;
101 else
102 return 0;
105 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
107 struct usb_device *udev = dev->udev;
108 int ret;
110 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
111 0x00,
112 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
113 0x00,
114 index,
115 (u8 *) value,
116 sizeof(u8),
117 500);
118 if (ret < 0)
119 return ret;
120 else
121 return 0;
124 static int stk_start_stream(struct stk_camera *dev)
126 int value;
127 int i, ret;
128 int value_116, value_117;
130 if (!is_present(dev))
131 return -ENODEV;
132 if (!is_memallocd(dev) || !is_initialised(dev)) {
133 STK_ERROR("FIXME: Buffers are not allocated\n");
134 return -EFAULT;
136 ret = usb_set_interface(dev->udev, 0, 5);
138 if (ret < 0)
139 STK_ERROR("usb_set_interface failed !\n");
140 if (stk_sensor_wakeup(dev))
141 STK_ERROR("error awaking the sensor\n");
143 stk_camera_read_reg(dev, 0x0116, &value_116);
144 stk_camera_read_reg(dev, 0x0117, &value_117);
146 stk_camera_write_reg(dev, 0x0116, 0x0000);
147 stk_camera_write_reg(dev, 0x0117, 0x0000);
149 stk_camera_read_reg(dev, 0x0100, &value); // read 0x21
150 stk_camera_write_reg(dev, 0x0100, value | 0x80);
152 stk_camera_write_reg(dev, 0x0116, value_116);
153 stk_camera_write_reg(dev, 0x0117, value_117);
154 for (i=0; i<MAX_ISO_BUFS; i++) {
155 if (dev->isobufs[i].urb) {
156 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
157 atomic_inc(&dev->urbs_used);
158 if (ret)
159 return ret;
162 set_streaming(dev);
163 return 0;
166 static int stk_stop_stream(struct stk_camera *dev)
168 int value;
169 int i;
170 if (is_present(dev)) {
171 stk_camera_read_reg(dev, 0x0100, &value);
172 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
173 if (dev->isobufs != NULL) {
174 for (i=0; i<MAX_ISO_BUFS; i++) {
175 if (dev->isobufs[i].urb)
176 usb_kill_urb(dev->isobufs[i].urb);
179 unset_streaming(dev);
181 if (usb_set_interface(dev->udev, 0, 0))
182 STK_ERROR("usb_set_interface failed !\n");
183 if (stk_sensor_sleep(dev))
184 STK_ERROR("error suspending the sensor\n");
186 return 0;
190 * This seems to be the shortest init sequence we
191 * must do in order to find the sensor
192 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
193 * is also reset. Maybe powers down it?
194 * Rest of values don't make a difference
197 static struct regval stk1125_initvals[] = {
198 //TODO: What means this sequence?
199 {0x0000, 0x24},
200 {0x0100, 0x21},
201 {0x0002, 0x68},
202 {0x0003, 0x80},
203 {0x0005, 0x00},
204 {0x0007, 0x03},
205 {0x000d, 0x00},
206 {0x000f, 0x02},
207 {0x0300, 0x12},
208 {0x0350, 0x41},
209 {0x0351, 0x00},
210 {0x0352, 0x00},
211 {0x0353, 0x00},
212 {0x0018, 0x10},
213 {0x0019, 0x00},
214 {0x001b, 0x0e},
215 {0x001c, 0x46},
216 {0x0300, 0x80},
217 {0x001a, 0x04},
218 {0x0110, 0x00},
219 {0x0111, 0x00},
220 {0x0112, 0x00},
221 {0x0113, 0x00},
223 {0xffff, 0xff},
227 static int stk_initialise(struct stk_camera *dev)
229 struct regval *rv;
230 int ret;
231 if (!is_present(dev))
232 return -ENODEV;
233 if (is_initialised(dev))
234 return 0;
235 rv = stk1125_initvals;
236 while (rv->reg != 0xffff) {
237 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
238 if (ret)
239 return ret;
240 rv++;
242 if (stk_sensor_init(dev) == 0) {
243 set_initialised(dev);
244 return 0;
246 else return -1;
249 /* sysfs functions */
250 /*FIXME cleanup this */
252 static ssize_t show_brightness(struct device *class,
253 struct device_attribute *attr, char *buf)
255 struct video_device *vdev = to_video_device(class);
256 struct stk_camera *dev = vdev_to_camera(vdev);
258 return sprintf(buf, "%X\n", dev->vsettings.brightness);
261 static ssize_t store_brightness(struct device *class,
262 struct device_attribute *attr, const char *buf, size_t count)
264 char *endp;
265 unsigned long value;
266 int ret;
268 struct video_device *vdev = to_video_device(class);
269 struct stk_camera *dev = vdev_to_camera(vdev);
271 value = simple_strtoul(buf, &endp, 16);
273 dev->vsettings.brightness = (int) value;
275 ret = stk_sensor_set_brightness(dev, value >> 8);
276 if (ret)
277 return ret;
278 else
279 return count;
282 static ssize_t show_hflip(struct device *class,
283 struct device_attribute *attr, char *buf)
285 struct video_device *vdev = to_video_device(class);
286 struct stk_camera *dev = vdev_to_camera(vdev);
288 return sprintf(buf, "%d\n", dev->vsettings.hflip);
291 static ssize_t store_hflip(struct device *class,
292 struct device_attribute *attr, const char *buf, size_t count)
294 struct video_device *vdev = to_video_device(class);
295 struct stk_camera *dev = vdev_to_camera(vdev);
297 if (strncmp(buf, "1", 1) == 0)
298 dev->vsettings.hflip = 1;
299 else if (strncmp(buf, "0", 1) == 0)
300 dev->vsettings.hflip = 0;
301 else
302 return -EINVAL;
304 return strlen(buf);
307 static ssize_t show_vflip(struct device *class,
308 struct device_attribute *attr, char *buf)
310 struct video_device *vdev = to_video_device(class);
311 struct stk_camera *dev = vdev_to_camera(vdev);
313 return sprintf(buf, "%d\n", dev->vsettings.vflip);
316 static ssize_t store_vflip(struct device *class,
317 struct device_attribute *attr, const char *buf, size_t count)
319 struct video_device *vdev = to_video_device(class);
320 struct stk_camera *dev = vdev_to_camera(vdev);
322 if (strncmp(buf, "1", 1) == 0)
323 dev->vsettings.vflip = 1;
324 else if (strncmp(buf, "0", 1) == 0)
325 dev->vsettings.vflip = 0;
326 else
327 return -EINVAL;
329 return strlen(buf);
332 #ifdef STK_UGLY_HACK
333 static ssize_t store_shack(struct device *class,
334 struct device_attribute *attr, const char *buf, size_t count)
336 char *endp;
337 unsigned long value;
338 unsigned long reg;
340 struct video_device *vdev = to_video_device(class);
341 struct stk_camera *dev = vdev_to_camera(vdev);
343 reg = simple_strtoul(buf, &endp, 16);
344 value = simple_strtoul(endp+1, NULL, 16);
346 stk_sensor_outb(dev, reg, value);
348 return strlen(buf);
351 static ssize_t store_chack(struct device *class,
352 struct device_attribute *attr, const char *buf, size_t count)
354 char *endp;
355 unsigned long value;
356 unsigned long reg;
358 struct video_device *vdev = to_video_device(class);
359 struct stk_camera *dev = vdev_to_camera(vdev);
361 reg = simple_strtoul(buf, &endp, 16);
362 value = simple_strtoul(endp+1, NULL, 16);
364 stk_camera_write_reg(dev, reg, value);
366 return strlen(buf);
369 static DEVICE_ATTR(shack, S_IWUGO, NULL, store_shack);
370 static DEVICE_ATTR(chack, S_IWUGO, NULL, store_chack);
371 #endif
373 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO, show_brightness, store_brightness);
374 static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip);
375 static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip);
377 static int stk_create_sysfs_files(struct video_device *vdev)
379 int ret;
381 ret = video_device_create_file(vdev, &dev_attr_brightness);
382 ret += video_device_create_file(vdev, &dev_attr_hflip);
383 ret += video_device_create_file(vdev, &dev_attr_vflip);
384 #ifdef STK_UGLY_HACK
385 ret += video_device_create_file(vdev, &dev_attr_shack);
386 ret += video_device_create_file(vdev, &dev_attr_chack);
387 #endif
388 return ret;
391 static void stk_remove_sysfs_files(struct video_device *vdev)
393 video_device_remove_file(vdev, &dev_attr_brightness);
394 video_device_remove_file(vdev, &dev_attr_hflip);
395 video_device_remove_file(vdev, &dev_attr_vflip);
396 #ifdef STK_UGLY_HACK
397 video_device_remove_file(vdev, &dev_attr_shack);
398 video_device_remove_file(vdev, &dev_attr_chack);
399 #endif
403 /* *********************************************** */
405 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
406 static inline int list_is_singular(const struct list_head *head)
408 return !list_empty(head) && (head->next == head->prev);
410 #endif
413 * This function is called as an URB transfert is complete (Isochronous pipe).
414 * So, the traitement is done in interrupt time, so it has be fast, not crash,
415 * and not stall. Neat.
417 static void stk_isoc_handler(struct urb *urb)
419 int i;
420 int ret;
421 int framelen;
422 unsigned long flags;
424 unsigned char *fill = NULL;
425 unsigned char *iso_buf = NULL;
427 struct stk_camera *dev;
428 struct stk_sio_buffer *fb;
430 dev = (struct stk_camera *) urb->context;
432 if (dev == NULL) {
433 STK_ERROR("isoc_handler called with NULL device !\n");
434 return;
437 if (urb->status == -ENOENT || urb->status == -ECONNRESET
438 || urb->status == -ESHUTDOWN) {
439 STK_DEBUG("URB unlinked synchronuously !\n");
440 atomic_dec(&dev->urbs_used);
441 return;
444 spin_lock_irqsave(&dev->spinlock, flags);
446 if (urb->status != -EINPROGRESS && urb->status != 0) {
447 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
448 goto resubmit;
451 if (list_empty(&dev->sio_avail)) {
452 /*FIXME Stop streaming after a while */
453 (void) (printk_ratelimit() &&
454 STK_ERROR("isoc_handler without available buffer!\n"));
455 goto resubmit;
457 fb = list_first_entry(&dev->sio_avail,
458 struct stk_sio_buffer, list);
459 fill = fb->buffer + fb->v4lbuf.bytesused;
461 // Copy data
462 for (i=0; i<urb->number_of_packets; i++) {
463 if (urb->iso_frame_desc[i].status != 0) {
464 if (urb->iso_frame_desc[i].status != -EXDEV)
465 STK_ERROR("Frame %d has error %d\n", i,
466 urb->iso_frame_desc[i].status);
467 continue;
469 framelen = urb->iso_frame_desc[i].actual_length;
470 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
472 if (framelen <= 4)
473 continue; /* no data */
475 // we found something informational from there
476 // the isoc frames have to type of headers
477 // type1: 00 xx 00 00 or 20 xx 00 00
478 // type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
479 // xx is a sequencer which has never been seen over 0x3f
480 // imho data written down looks like bayer, i see similarities
481 // after every 640 bytes
482 if (*iso_buf & 0x80) {
483 framelen -= 8;
484 iso_buf += 8;
485 STK_DEBUG("New frame %d bytes\n", framelen);
486 #ifdef STK_PARTIAL_FRAMES
487 if (fb->v4lbuf.bytesused != 0)
488 fb->v4lbuf.bytesused = dev->frame_size;
489 #endif
490 /* This marks a new frame */
491 if (fb->v4lbuf.bytesused != 0
492 && fb->v4lbuf.bytesused != dev->frame_size) {
493 (void ) (printk_ratelimit() &&
494 STK_ERROR("frame %d, "
495 "bytesused=%d, skipping\n",
496 i, fb->v4lbuf.bytesused));
497 fb->v4lbuf.bytesused = 0;
498 fill = fb->buffer;
500 else if (fb->v4lbuf.bytesused == dev->frame_size) {
501 if (list_is_singular(&dev->sio_avail)) {
502 /* Always reuse the last buffer */
503 fb->v4lbuf.bytesused = 0;
504 fill = fb->buffer;
506 else {
507 list_move_tail(dev->sio_avail.next,
508 &dev->sio_full);
509 wake_up(&dev->wait_frame);
510 fb = list_first_entry(&dev->sio_avail,
511 struct stk_sio_buffer, list);
512 fb->v4lbuf.bytesused = 0;
513 fill = fb->buffer;
517 else {
518 framelen -= 4;
519 iso_buf += 4;
520 STK_DEBUG("Got %d bytes\n", framelen);
523 // Our buffer is full !!!
524 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
525 (void) (printk_ratelimit() &&
526 STK_ERROR("Frame buffer overflow, lost sync\n"));
527 //FIXME
528 continue;
530 // All is OK
531 spin_unlock_irqrestore(&dev->spinlock, flags);
532 memcpy(fill, iso_buf, framelen);
533 spin_lock_irqsave(&dev->spinlock, flags);
534 fill += framelen;
536 // New size of our buffer
537 fb->v4lbuf.bytesused += framelen;
540 resubmit:
541 spin_unlock_irqrestore(&dev->spinlock, flags);
542 urb->dev = dev->udev;
543 ret = usb_submit_urb(urb, GFP_ATOMIC);
544 if (ret != 0) {
545 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
546 ret);
550 /* -------------------------------------------- */
552 static int stk_prepare_iso(struct stk_camera *dev)
554 void *kbuf;
555 int i, j;
556 struct urb *urb;
557 struct usb_device *udev;
559 if (dev == NULL)
560 return -ENXIO;
561 udev = dev->udev;
563 if (dev->isobufs)
564 STK_ERROR("isobufs already allocated. Bad\n");
565 else
566 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
567 GFP_KERNEL);
568 if (dev->isobufs == NULL) {
569 STK_ERROR("Unable to allocate iso buffers\n");
570 return -ENOMEM;
572 for (i=0; i<MAX_ISO_BUFS; i++) {
573 if (dev->isobufs[i].data == NULL) {
574 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
575 if (kbuf == NULL) {
576 STK_ERROR("Failed to allocate iso buffer %d\n",
578 goto isobufs_out;
580 dev->isobufs[i].data = kbuf;
582 else {
583 STK_ERROR("isobuf data already allocated\n");
585 if (dev->isobufs[i].urb == NULL) {
586 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
587 if (urb == NULL) {
588 STK_ERROR("Failed to allocate URB %d\n", i);
589 goto isobufs_out;
591 dev->isobufs[i].urb = urb;
593 else {
594 STK_ERROR("Killing URB\n");
595 usb_kill_urb(dev->isobufs[i].urb);
596 urb = dev->isobufs[i].urb;
598 urb->interval = 1;
599 urb->dev = udev;
600 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
601 urb->transfer_flags = URB_ISO_ASAP;
602 urb->transfer_buffer = dev->isobufs[i].data;
603 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
604 urb->complete = stk_isoc_handler;
605 urb->context = dev;
606 urb->start_frame = 0;
607 urb->number_of_packets = ISO_FRAMES_PER_DESC;
609 for (j=0; j<ISO_FRAMES_PER_DESC; j++) {
610 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
611 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
614 set_memallocd(dev);
615 return 0;
617 isobufs_out:
618 for (i=0; i<MAX_ISO_BUFS && dev->isobufs[i].data; i++)
619 kfree(dev->isobufs[i].data);
620 for (i=0; i<MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
621 usb_free_urb(dev->isobufs[i].urb);
622 kfree(dev->isobufs);
623 dev->isobufs = NULL;
624 return -ENOMEM;
627 static void stk_clean_iso(struct stk_camera *dev)
629 int i;
631 if (dev == NULL || dev->isobufs == NULL)
632 return;
634 // Unlinking ISOC buffers
635 for (i=0; i<MAX_ISO_BUFS; i++) {
636 struct urb *urb;
638 urb = dev->isobufs[i].urb;
639 if (urb) {
640 if (atomic_read(&dev->urbs_used))
641 usb_kill_urb(urb);
642 else
643 STK_DEBUG("URBS already unlinked\n");
644 usb_free_urb(urb);
646 if (dev->isobufs[i].data)
647 kfree(dev->isobufs[i].data);
649 kfree(dev->isobufs);
650 dev->isobufs = NULL;
651 unset_memallocd(dev);
654 static int stk_setup_siobuf(struct stk_camera *dev, int index)
656 struct stk_sio_buffer *buf = dev->sio_bufs + index;
657 INIT_LIST_HEAD(&buf->list);
658 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
659 buf->buffer = vmalloc_user(buf->v4lbuf.length);
660 if (buf->buffer == NULL)
661 return -ENOMEM;
662 buf->mapcount = 0;
663 buf->dev = dev;
664 buf->v4lbuf.index = index;
665 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
666 buf->v4lbuf.field = V4L2_FIELD_NONE;
667 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
668 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
669 return 0;
672 static int stk_free_sio_buffers(struct stk_camera *dev)
674 int i;
675 int nbufs;
676 unsigned long flags;
677 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
678 return 0;
680 * If any buffers are mapped, we cannot free them at all.
682 for (i = 0; i < dev->n_sbufs; i++) {
683 if (dev->sio_bufs[i].mapcount > 0) {
684 return -EBUSY;
688 * OK, let's do it.
690 spin_lock_irqsave(&dev->spinlock, flags);
691 INIT_LIST_HEAD(&dev->sio_avail);
692 INIT_LIST_HEAD(&dev->sio_full);
693 nbufs = dev->n_sbufs;
694 dev->n_sbufs = 0;
695 spin_unlock_irqrestore(&dev->spinlock, flags);
696 for (i = 0; i < nbufs; i++) {
697 if (dev->sio_bufs[i].buffer != NULL)
698 vfree(dev->sio_bufs[i].buffer);
700 kfree(dev->sio_bufs);
701 dev->sio_bufs = NULL;
702 return 0;
705 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
707 int i;
708 if (dev->sio_bufs != NULL) {
709 STK_ERROR("sio_bufs already allocated\n");
711 else {
712 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
713 GFP_KERNEL);
714 if (dev->sio_bufs == NULL)
715 return -ENOMEM;
716 for (i=0; i < n_sbufs; i++) {
717 if (stk_setup_siobuf(dev, i))
718 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
719 dev->n_sbufs = i+1;
722 return 0;
725 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
727 int err;
728 err = stk_prepare_iso(dev);
729 if (err) {
730 stk_clean_iso(dev);
731 return err;
733 err = stk_prepare_sio_buffers(dev, n_sbufs);
734 if (err) {
735 stk_free_sio_buffers(dev);
736 return err;
738 return 0;
741 static void stk_free_buffers(struct stk_camera *dev)
743 stk_clean_iso(dev);
744 stk_free_sio_buffers(dev);
746 /* -------------------------------------------- */
748 /* v4l file operations */
750 static int v4l_stk_open(struct inode *inode, struct file *fp)
752 struct stk_camera *dev;
753 struct video_device *vdev;
755 vdev = video_devdata(fp);
756 dev = vdev_to_camera(vdev);
758 if (dev == NULL || !is_present(dev))
759 return -ENXIO;
760 fp->private_data = vdev;
761 kref_get(&dev->kref);
762 usb_autopm_get_interface(dev->interface);
764 return 0;
767 static int v4l_stk_release(struct inode *inode, struct file *fp)
769 struct stk_camera *dev;
770 struct video_device *vdev;
772 vdev = video_devdata(fp);
773 if (vdev == NULL) {
774 STK_ERROR("v4l_release called w/o video devdata\n");
775 return -EFAULT;
777 dev = vdev_to_camera(vdev);
778 if (dev == NULL) {
779 STK_ERROR("v4l_release called on removed device\n");
780 return -ENODEV;
783 if (dev->owner != fp) {
784 usb_autopm_put_interface(dev->interface);
785 kref_put(&dev->kref, stk_camera_cleanup);
786 return 0;
789 // Stop the video stream
790 stk_stop_stream(dev);
792 stk_free_buffers(dev);
794 dev->owner = NULL;
796 usb_autopm_put_interface(dev->interface);
797 kref_put(&dev->kref, stk_camera_cleanup);
799 return 0;
802 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
803 size_t count, loff_t *f_pos)
805 int i;
806 int ret;
807 unsigned long flags;
808 struct stk_camera *dev;
809 struct video_device *vdev;
810 struct stk_sio_buffer *sbuf;
812 vdev = video_devdata(fp);
813 if (vdev == NULL)
814 return -EFAULT;
815 dev = vdev_to_camera(vdev);
817 if (dev == NULL)
818 return -EIO;
820 if (!is_present(dev))
821 return -EIO;
822 if (dev->owner && dev->owner != fp)
823 return -EBUSY;
824 dev->owner = fp;
825 if (!is_streaming(dev)) {
826 if (stk_initialise(dev)
827 || stk_allocate_buffers(dev, 3)
828 || stk_start_stream(dev))
829 return -ENOMEM;
830 spin_lock_irqsave(&dev->spinlock, flags);
831 for (i = 0; i < dev->n_sbufs; i++) {
832 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
833 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
835 spin_unlock_irqrestore(&dev->spinlock, flags);
837 if (*f_pos == 0) {
838 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
839 return -EWOULDBLOCK;
840 ret = wait_event_interruptible(dev->wait_frame,
841 !list_empty(&dev->sio_full) || !is_present(dev));
842 if (ret)
843 return ret;
844 if (!is_present(dev))
845 return -EIO;
847 if (count + *f_pos > dev->frame_size)
848 count = dev->frame_size - *f_pos;
849 spin_lock_irqsave(&dev->spinlock, flags);
850 if (list_empty(&dev->sio_full)) {
851 spin_unlock_irqrestore(&dev->spinlock, flags);
852 STK_ERROR("BUG: No siobufs ready\n");
853 return 0;
855 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
856 spin_unlock_irqrestore(&dev->spinlock, flags);
858 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
859 return -EFAULT;
861 *f_pos += count;
863 if (*f_pos >= dev->frame_size) {
864 *f_pos = 0;
865 spin_lock_irqsave(&dev->spinlock, flags);
866 list_move_tail(&sbuf->list, &dev->sio_avail);
867 spin_unlock_irqrestore(&dev->spinlock, flags);
869 return count;
872 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
874 struct stk_camera *dev;
875 struct video_device *vdev;
877 vdev = video_devdata(fp);
879 if (vdev == NULL)
880 return -EFAULT;
882 dev = vdev_to_camera(vdev);
883 if (dev == NULL)
884 return -ENODEV;
886 poll_wait(fp, &dev->wait_frame, wait);
888 if (!is_present(dev))
889 return POLLERR;
891 if (!list_empty(&dev->sio_full))
892 return (POLLIN | POLLRDNORM);
894 return 0;
898 static void stk_v4l_vm_open(struct vm_area_struct *vma)
900 struct stk_sio_buffer *sbuf = vma->vm_private_data;
901 sbuf->mapcount++;
903 static void stk_v4l_vm_close(struct vm_area_struct *vma)
905 struct stk_sio_buffer *sbuf = vma->vm_private_data;
906 sbuf->mapcount--;
907 if (sbuf->mapcount == 0)
908 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
910 static struct vm_operations_struct stk_v4l_vm_ops = {
911 .open = stk_v4l_vm_open,
912 .close = stk_v4l_vm_close
915 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
917 unsigned int i;
918 int ret;
919 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
920 struct stk_camera *dev;
921 struct video_device *vdev;
922 struct stk_sio_buffer *sbuf = NULL;
924 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
925 return -EINVAL;
927 vdev = video_devdata(fp);
928 dev = vdev_to_camera(vdev);
930 for (i=0; i < dev->n_sbufs; i++) {
931 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
932 sbuf = dev->sio_bufs + i;
933 break;
936 if (sbuf == NULL)
937 return -EINVAL;
938 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
939 if (ret)
940 return ret;
941 vma->vm_flags |= VM_DONTEXPAND;
942 vma->vm_private_data = sbuf;
943 vma->vm_ops = &stk_v4l_vm_ops;
944 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
945 stk_v4l_vm_open(vma);
946 return 0;
949 /* v4l ioctl handlers */
951 #if 0
952 /* From videodev.c *****************************************
953 Handles calls to the obsoleted V4L1 API
954 Due to the nature of VIDIOCGMBUF, each driver that supports
955 V4L1 should implement its own handler for this ioctl.
956 ***********************************************************/
957 static int stk_vidiocgmbuf(struct file *filp,
958 void *priv, struct video_mbuf *vm)
960 int i;
961 struct stk_camera *dev = priv;
963 memset(vm, 0, sizeof(*vm));
965 vm->size = dev->n_sbufs * dev->image_size;
966 vm->frames = dev->n_sbufs;
968 for (i=0; i<dev->n_sbufs; i++)
969 vm->offsets[i] = i * dev->image_size;
970 return 0;
972 #endif
973 // Video 4 Linux v2
975 static int stk_vidioc_querycap(struct file *filp,
976 void *priv, struct v4l2_capability *cap)
978 strcpy(cap->driver, "stk");
979 strcpy(cap->card, "stk");
980 cap->version = DRIVER_VERSION_NUM;
982 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
983 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
984 return 0;
987 static int stk_vidioc_enum_input(struct file *filp,
988 void *priv, struct v4l2_input *input)
990 if (input->index != 0)
991 return -EINVAL;
993 strcpy(input->name, "Syntek USB Camera");
994 input->type = V4L2_INPUT_TYPE_CAMERA;
995 return 0;
999 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1002 *i = 0;
1003 return 0;
1006 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1009 if (i != 0)
1010 return -EINVAL;
1011 else return 0;
1014 /* from vivi.c */
1015 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1017 return 0;
1020 /* List of all V4Lv2 controls supported by the driver */
1021 static struct v4l2_queryctrl stk_controls[] = {
1023 .id = V4L2_CID_BRIGHTNESS,
1024 .type = V4L2_CTRL_TYPE_INTEGER,
1025 .name = "Brightness",
1026 .minimum = 0,
1027 .maximum = 0xffff,
1028 .step = 0x0100,
1029 .default_value = 0x6000,
1031 /*TODO: get more controls to work */
1034 static int stk_vidioc_queryctrl(struct file *filp,
1035 void *priv, struct v4l2_queryctrl *c)
1037 int i;
1038 int nbr;
1039 nbr = ARRAY_SIZE(stk_controls);
1041 for (i=0; i<nbr; i++) {
1042 if (stk_controls[i].id == c->id) {
1043 STK_DEBUG("VIDIOC_QUERYCTRL found\n");
1044 memcpy(c, &stk_controls[i],
1045 sizeof(struct v4l2_queryctrl));
1046 return 0;
1049 return -EINVAL;
1052 static int stk_vidioc_g_ctrl(struct file *filp,
1053 void *priv, struct v4l2_control *c)
1055 struct stk_camera *dev = priv;
1056 switch (c->id) {
1057 case V4L2_CID_BRIGHTNESS:
1058 c->value = dev->vsettings.brightness;
1059 break;
1060 default:
1061 return -EINVAL;
1063 return 0;
1066 static int stk_vidioc_s_ctrl(struct file *filp,
1067 void *priv, struct v4l2_control *c)
1069 struct stk_camera *dev = priv;
1070 switch (c->id) {
1071 case V4L2_CID_BRIGHTNESS:
1072 dev->vsettings.brightness = c->value;
1073 return stk_sensor_set_brightness(dev, c->value >> 8);
1074 default:
1075 return -EINVAL;
1077 return 0;
1081 static int stk_vidioc_enum_fmt_cap(struct file *filp,
1082 void *priv, struct v4l2_fmtdesc *fmtd)
1084 fmtd->flags = 0;
1086 switch (fmtd->index) {
1087 case 0:
1088 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
1089 strcpy(fmtd->description, "r5g6b5");
1090 break;
1091 case 1:
1092 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
1093 strcpy(fmtd->description, "r5g6b5BE");
1094 break;
1095 case 2:
1096 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
1097 strcpy(fmtd->description, "yuv4:2:2");
1098 break;
1099 case 3:
1100 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
1101 strcpy(fmtd->description, "Raw bayer");
1102 break;
1103 case 4:
1104 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1105 strcpy(fmtd->description, "yuv4:2:2");
1106 break;
1107 default:
1108 return -EINVAL;
1110 return 0;
1113 static struct stk_size {
1114 unsigned w;
1115 unsigned h;
1116 enum stk_mode m;
1117 } stk_sizes[] = {
1118 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1119 { .w = 640, .h = 480, .m = MODE_VGA, },
1120 { .w = 352, .h = 288, .m = MODE_CIF, },
1121 { .w = 320, .h = 240, .m = MODE_QVGA, },
1122 { .w = 176, .h = 144, .m = MODE_QCIF, },
1125 static int stk_vidioc_g_fmt_cap(struct file *filp,
1126 void *priv, struct v4l2_format *f)
1128 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1129 struct stk_camera *dev = priv;
1130 int i;
1132 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1133 && stk_sizes[i].m != dev->vsettings.mode;
1134 i++);
1135 if (i == ARRAY_SIZE(stk_sizes)) {
1136 STK_ERROR("ERROR: mode invalid\n");
1137 return -EINVAL;
1139 pix_format->width = stk_sizes[i].w;
1140 pix_format->height = stk_sizes[i].h;
1141 pix_format->field = V4L2_FIELD_NONE;
1142 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1143 pix_format->priv = 0;
1144 pix_format->pixelformat = dev->vsettings.palette;
1145 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1146 pix_format->bytesperline = pix_format->width;
1147 else
1148 pix_format->bytesperline = 2 * pix_format->width;
1149 pix_format->sizeimage = pix_format->bytesperline
1150 * pix_format->height;
1151 return 0;
1154 static int stk_vidioc_try_fmt_cap(struct file *filp,
1155 void *priv, struct v4l2_format *fmtd)
1157 int i;
1158 switch (fmtd->fmt.pix.pixelformat) {
1159 case V4L2_PIX_FMT_RGB565:
1160 case V4L2_PIX_FMT_RGB565X:
1161 case V4L2_PIX_FMT_UYVY:
1162 case V4L2_PIX_FMT_YUYV:
1163 case V4L2_PIX_FMT_SBGGR8:
1164 break;
1165 default:
1166 return -EINVAL;
1168 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1169 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1170 break;
1172 if (i == ARRAY_SIZE(stk_sizes)
1173 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1174 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1175 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1176 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1177 fmtd->fmt.pix.priv = i - 1;
1178 } else {
1179 fmtd->fmt.pix.height = stk_sizes[i].h;
1180 fmtd->fmt.pix.width = stk_sizes[i].w;
1181 fmtd->fmt.pix.priv = i;
1184 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1185 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1186 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1187 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1188 else
1189 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1190 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1191 * fmtd->fmt.pix.height;
1192 return 0;
1195 static int stk_setup_format(struct stk_camera *dev)
1197 int i = 0;
1198 int depth;
1199 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1200 depth = 1;
1201 else
1202 depth = 2;
1203 while (stk_sizes[i].m != dev->vsettings.mode
1204 && i < ARRAY_SIZE(stk_sizes))
1205 i++;
1206 if (i == ARRAY_SIZE(stk_sizes)) {
1207 STK_ERROR("Something is broken in %s\n", __FUNCTION__);
1208 return -EFAULT;
1210 /* This registers controls some timings, not sure of what. */
1211 stk_camera_write_reg(dev, 0x001b, 0x0e);
1212 if (dev->vsettings.mode == MODE_SXGA)
1213 stk_camera_write_reg(dev, 0x001c, 0x0e);
1214 else
1215 stk_camera_write_reg(dev, 0x001c, 0x46);
1217 * Registers 0x0115 0x0114 are the size of each line (bytes),
1218 * regs 0x0117 0x0116 are the heigth of the image.
1220 stk_camera_write_reg(dev, 0x0115,
1221 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1222 stk_camera_write_reg(dev, 0x0114,
1223 (stk_sizes[i].w * depth) & 0xff);
1224 stk_camera_write_reg(dev, 0x0117,
1225 (stk_sizes[i].h >> 8) & 0xff);
1226 stk_camera_write_reg(dev, 0x0116,
1227 stk_sizes[i].h & 0xff);
1228 return stk_sensor_configure(dev);
1231 static int stk_vidioc_s_fmt_cap(struct file *filp,
1232 void *priv, struct v4l2_format *fmtd)
1234 int ret;
1235 struct stk_camera *dev = priv;
1237 if (dev == NULL)
1238 return -ENODEV;
1239 if (!is_present(dev))
1240 return -ENODEV;
1241 if (is_streaming(dev))
1242 return -EBUSY;
1243 if (dev->owner && dev->owner != filp)
1244 return -EBUSY;
1245 ret = stk_vidioc_try_fmt_cap(filp, priv, fmtd);
1246 if (ret)
1247 return ret;
1248 dev->owner = filp;
1250 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1251 stk_free_buffers(dev);
1252 dev->frame_size = fmtd->fmt.pix.sizeimage;
1253 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1255 // Select the new video mode
1256 stk_initialise(dev);
1257 return stk_setup_format(dev);
1260 static int stk_vidioc_reqbufs(struct file *filp,
1261 void *priv, struct v4l2_requestbuffers *rb)
1263 struct stk_camera *dev = priv;
1265 if (dev == NULL)
1266 return -ENODEV;
1267 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1268 return -EINVAL;
1269 if (rb->memory != V4L2_MEMORY_MMAP)
1270 return -EINVAL;
1271 if (is_streaming(dev)
1272 || (dev->owner && dev->owner != filp))
1273 return -EBUSY;
1274 dev->owner = filp;
1276 /*FIXME If they ask for zero, we must stop streaming and free */
1277 if (rb->count < 3)
1278 rb->count = 3;
1279 /* Arbitrary limit */
1280 else if (rb->count > 5)
1281 rb->count = 5;
1283 stk_allocate_buffers(dev, rb->count);
1284 rb->count = dev->n_sbufs;
1285 return 0;
1288 static int stk_vidioc_querybuf(struct file *filp,
1289 void *priv, struct v4l2_buffer *buf)
1291 int index;
1292 struct stk_camera *dev = priv;
1293 struct stk_sio_buffer *sbuf;
1295 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1296 return -EINVAL;
1298 index = buf->index;
1300 if (index < 0 || index >= dev->n_sbufs)
1301 return -EINVAL;
1302 sbuf = dev->sio_bufs + buf->index;
1303 *buf = sbuf->v4lbuf;
1304 return 0;
1307 static int stk_vidioc_qbuf(struct file *filp,
1308 void *priv, struct v4l2_buffer *buf)
1310 struct stk_camera *dev = priv;
1311 struct stk_sio_buffer *sbuf;
1312 unsigned long flags;
1313 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1314 return -EINVAL;
1316 if (buf->memory != V4L2_MEMORY_MMAP)
1317 return -EINVAL;
1319 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1320 return -EINVAL;
1321 sbuf = dev->sio_bufs + buf->index;
1322 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1323 return 0;
1325 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1326 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1327 spin_lock_irqsave(&dev->spinlock, flags);
1328 list_add_tail(&sbuf->list, &dev->sio_avail);
1329 *buf = sbuf->v4lbuf;
1330 spin_unlock_irqrestore(&dev->spinlock, flags);
1331 return 0;
1334 static int stk_vidioc_dqbuf(struct file *filp,
1335 void *priv, struct v4l2_buffer *buf)
1337 struct stk_camera *dev = priv;
1338 struct stk_sio_buffer *sbuf;
1339 unsigned long flags;
1340 int ret;
1342 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1343 || !is_streaming(dev))
1344 return -EINVAL;
1346 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1347 return -EWOULDBLOCK;
1348 ret = wait_event_interruptible(dev->wait_frame,
1349 !list_empty(&dev->sio_full) || !is_present(dev));
1350 if (ret)
1351 return ret;
1352 if (!is_present(dev))
1353 return -EIO;
1355 spin_lock_irqsave(&dev->spinlock, flags);
1356 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1357 list_del_init(&sbuf->list);
1358 spin_unlock_irqrestore(&dev->spinlock, flags);
1359 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1360 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1361 sbuf->v4lbuf.sequence = ++dev->sequence;
1362 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1364 *buf = sbuf->v4lbuf;
1365 return 0;
1368 static int stk_vidioc_streamon(struct file *filp,
1369 void *priv, enum v4l2_buf_type type)
1371 struct stk_camera *dev = priv;
1372 if (is_streaming(dev))
1373 return 0;
1374 if (dev->sio_bufs == NULL)
1375 return -EINVAL;
1376 dev->sequence = 0;
1377 return stk_start_stream(dev);
1380 static int stk_vidioc_streamoff(struct file *filp,
1381 void *priv, enum v4l2_buf_type type)
1383 struct stk_camera *dev = priv;
1384 unsigned long flags;
1385 int i;
1386 stk_stop_stream(dev);
1387 spin_lock_irqsave(&dev->spinlock, flags);
1388 INIT_LIST_HEAD(&dev->sio_avail);
1389 INIT_LIST_HEAD(&dev->sio_full);
1390 for (i=0; i<dev->n_sbufs; i++) {
1391 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1392 dev->sio_bufs[i].v4lbuf.flags = 0;
1394 spin_unlock_irqrestore(&dev->spinlock, flags);
1395 return 0;
1399 static int stk_vidioc_g_parm(struct file *filp,
1400 void *priv, struct v4l2_streamparm *sp)
1402 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1403 return -EINVAL;
1405 sp->parm.capture.capability = 0;
1406 sp->parm.capture.capturemode = 0;
1407 /*FIXME This is not correct */
1408 sp->parm.capture.timeperframe.numerator = 1;
1409 sp->parm.capture.timeperframe.denominator = 30;
1410 sp->parm.capture.readbuffers = 2;
1411 sp->parm.capture.extendedmode = 0;
1412 return 0;
1415 static struct file_operations v4l_stk_fops = {
1416 .owner = THIS_MODULE,
1417 .open = v4l_stk_open,
1418 .release = v4l_stk_release,
1419 .read = v4l_stk_read,
1420 .poll = v4l_stk_poll,
1421 .mmap = v4l_stk_mmap,
1422 .ioctl = video_ioctl2,
1423 #ifdef CONFIG_COMPAT
1424 .compat_ioctl = v4l_compat_ioctl32,
1425 #endif
1426 .llseek = no_llseek
1429 static void stk_v4l_dev_release(struct video_device *vd)
1433 static struct video_device stk_v4l_data = {
1434 .name = "stkwebcam",
1435 .type = VFL_TYPE_GRABBER,
1436 .type2 = VID_TYPE_CAPTURE,
1437 .minor = -1,
1438 .tvnorms = V4L2_STD_UNKNOWN,
1439 .current_norm = V4L2_STD_UNKNOWN,
1440 .fops = &v4l_stk_fops,
1441 .release = stk_v4l_dev_release,
1442 // .vidiocgmbuf = stk_vidiocgmbuf,
1444 .vidioc_querycap = stk_vidioc_querycap,
1445 .vidioc_enum_fmt_cap = stk_vidioc_enum_fmt_cap,
1446 .vidioc_try_fmt_cap = stk_vidioc_try_fmt_cap,
1447 .vidioc_s_fmt_cap = stk_vidioc_s_fmt_cap,
1448 .vidioc_g_fmt_cap = stk_vidioc_g_fmt_cap,
1449 .vidioc_enum_input = stk_vidioc_enum_input,
1450 .vidioc_s_input = stk_vidioc_s_input,
1451 .vidioc_g_input = stk_vidioc_g_input,
1452 .vidioc_s_std = stk_vidioc_s_std,
1453 .vidioc_reqbufs = stk_vidioc_reqbufs,
1454 .vidioc_querybuf = stk_vidioc_querybuf,
1455 .vidioc_qbuf = stk_vidioc_qbuf,
1456 .vidioc_dqbuf = stk_vidioc_dqbuf,
1457 .vidioc_streamon = stk_vidioc_streamon,
1458 .vidioc_streamoff = stk_vidioc_streamoff,
1459 .vidioc_queryctrl = stk_vidioc_queryctrl,
1460 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1461 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1462 .vidioc_g_parm = stk_vidioc_g_parm,
1466 static int stk_register_video_device(struct stk_camera *dev)
1468 int err;
1470 dev->vdev = stk_v4l_data;
1471 dev->vdev.debug = debug;
1472 dev->vdev.dev = &dev->interface->dev;
1473 dev->vdev.priv = dev;
1474 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1475 if (err)
1476 STK_ERROR("v4l registration failed\n");
1477 else
1478 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1479 " /dev/video%d\n", dev->vdev.minor);
1480 return err;
1484 /* USB Stuff */
1486 static int stk_camera_probe(struct usb_interface *interface,
1487 const struct usb_device_id *id)
1489 int i;
1490 int err;
1492 struct stk_camera *dev = NULL;
1493 struct usb_device *udev = interface_to_usbdev(interface);
1494 struct usb_host_interface *iface_desc;
1495 struct usb_endpoint_descriptor *endpoint;
1497 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1498 if (dev == NULL) {
1499 STK_ERROR("Out of memory !\n");
1500 return -ENOMEM;
1503 kref_init(&dev->kref);
1504 spin_lock_init(&dev->spinlock);
1505 init_waitqueue_head(&dev->wait_frame);
1507 dev->udev = udev;
1508 dev->interface = interface;
1509 usb_get_intf(interface);
1511 dev->vsettings.vflip = vflip;
1512 dev->vsettings.hflip = hflip;
1513 dev->n_sbufs = 0;
1514 set_present(dev);
1516 /* Set up the endpoint information
1517 * use only the first isoc-in endpoint
1518 * for the current alternate setting */
1519 iface_desc = interface->cur_altsetting;
1521 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1522 endpoint = &iface_desc->endpoint[i].desc;
1524 if (!dev->isoc_ep
1525 && ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1526 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1527 /* we found an isoc in endpoint */
1528 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1529 break;
1532 if (!dev->isoc_ep) {
1533 STK_ERROR("Could not find isoc-in endpoint");
1534 kref_put(&dev->kref, stk_camera_cleanup);
1535 return -ENODEV;
1537 // Settings
1538 dev->vsettings.brightness = 0x7fff;
1539 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1540 dev->vsettings.mode = MODE_VGA;
1541 dev->frame_size = 640*480*2;
1543 INIT_LIST_HEAD(&dev->sio_avail);
1544 INIT_LIST_HEAD(&dev->sio_full);
1546 usb_set_intfdata(interface, dev);
1548 err = stk_register_video_device(dev);
1549 if (err) {
1550 kref_put(&dev->kref, stk_camera_cleanup);
1551 return err;
1554 stk_create_sysfs_files(&dev->vdev);
1555 usb_autopm_enable(dev->interface);
1557 return 0;
1560 static void stk_camera_disconnect(struct usb_interface *interface)
1562 struct stk_camera *dev = usb_get_intfdata(interface);
1564 usb_set_intfdata(interface, NULL);
1565 unset_present(dev);
1567 // Alert waiting processes
1568 wake_up_interruptible(&dev->wait_frame);
1569 // Remove the entries in the sys filesystem
1570 stk_remove_sysfs_files(&dev->vdev);
1572 // Decrement kernel reference
1573 kref_put(&dev->kref, stk_camera_cleanup);
1576 #ifdef CONFIG_PM
1577 int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1579 struct stk_camera *dev = usb_get_intfdata(intf);
1580 if (is_streaming(dev)) {
1581 stk_stop_stream(dev);
1582 /* yes, this is ugly */
1583 set_streaming(dev);
1585 return 0;
1588 int stk_camera_resume(struct usb_interface *intf)
1590 struct stk_camera *dev = usb_get_intfdata(intf);
1591 if (!is_initialised(dev))
1592 return 0;
1593 unset_initialised(dev);
1594 stk_initialise(dev);
1595 stk_setup_format(dev);
1596 if (is_streaming(dev))
1597 stk_start_stream(dev);
1598 return 0;
1600 #endif
1602 static struct usb_driver stk_camera_driver = {
1603 .name = "stkwebcam",
1604 .probe = stk_camera_probe,
1605 .disconnect = stk_camera_disconnect,
1606 .id_table = stkwebcam_table,
1607 #ifdef CONFIG_PM
1608 .suspend = stk_camera_suspend,
1609 .resume = stk_camera_resume,
1610 #endif
1614 static int __init stk_camera_init(void)
1616 int result;
1618 result = usb_register(&stk_camera_driver);
1619 if (result)
1620 STK_ERROR("usb_register failed ! Error number %d\n", result);
1623 return result;
1626 static void __exit stk_camera_exit(void)
1628 usb_deregister(&stk_camera_driver);
1631 module_init(stk_camera_init);
1632 module_exit(stk_camera_exit);