2 * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
4 * Copyright (C) 2006 Nicolas VIVIEN
5 * Copyright 2007-2008 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
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>
34 #include <linux/vmalloc.h>
35 #include <linux/videodev2.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
39 #include "stk-webcam.h"
43 module_param(hflip, bool, 0444);
44 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
47 module_param(vflip, bool, 0444);
48 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
51 module_param(debug, int, 0444);
52 MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
54 MODULE_LICENSE("GPL");
55 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
56 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
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) },
66 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
68 static void stk_camera_cleanup(struct kref *kref)
70 struct stk_camera *dev = to_stk_camera(kref);
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;
77 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
78 STK_ERROR("We are leaking memory\n");
79 usb_put_intf(dev->interface);
87 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
89 struct usb_device *udev = dev->udev;
92 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
94 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
106 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
108 struct usb_device *udev = dev->udev;
111 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
113 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
125 static int stk_start_stream(struct stk_camera *dev)
129 int value_116, value_117;
131 if (!is_present(dev))
133 if (!is_memallocd(dev) || !is_initialised(dev)) {
134 STK_ERROR("FIXME: Buffers are not allocated\n");
137 ret = usb_set_interface(dev->udev, 0, 5);
140 STK_ERROR("usb_set_interface failed !\n");
141 if (stk_sensor_wakeup(dev))
142 STK_ERROR("error awaking the sensor\n");
144 stk_camera_read_reg(dev, 0x0116, &value_116);
145 stk_camera_read_reg(dev, 0x0117, &value_117);
147 stk_camera_write_reg(dev, 0x0116, 0x0000);
148 stk_camera_write_reg(dev, 0x0117, 0x0000);
150 stk_camera_read_reg(dev, 0x0100, &value);
151 stk_camera_write_reg(dev, 0x0100, value | 0x80);
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);
167 static int stk_stop_stream(struct stk_camera *dev)
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);
180 unset_streaming(dev);
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");
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
198 static struct regval stk1125_initvals[] = {
199 /*TODO: What means this sequence? */
228 static int stk_initialise(struct stk_camera *dev)
232 if (!is_present(dev))
234 if (is_initialised(dev))
236 rv = stk1125_initvals;
237 while (rv->reg != 0xffff) {
238 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
243 if (stk_sensor_init(dev) == 0) {
244 set_initialised(dev);
250 #ifdef CONFIG_VIDEO_V4L1_COMPAT
252 /* sysfs functions */
253 /*FIXME cleanup this */
255 static ssize_t show_brightness(struct device *class,
256 struct device_attribute *attr, char *buf)
258 struct video_device *vdev = to_video_device(class);
259 struct stk_camera *dev = vdev_to_camera(vdev);
261 return sprintf(buf, "%X\n", dev->vsettings.brightness);
264 static ssize_t store_brightness(struct device *class,
265 struct device_attribute *attr, const char *buf, size_t count)
271 struct video_device *vdev = to_video_device(class);
272 struct stk_camera *dev = vdev_to_camera(vdev);
274 value = simple_strtoul(buf, &endp, 16);
276 dev->vsettings.brightness = (int) value;
278 ret = stk_sensor_set_brightness(dev, value >> 8);
285 static ssize_t show_hflip(struct device *class,
286 struct device_attribute *attr, char *buf)
288 struct video_device *vdev = to_video_device(class);
289 struct stk_camera *dev = vdev_to_camera(vdev);
291 return sprintf(buf, "%d\n", dev->vsettings.hflip);
294 static ssize_t store_hflip(struct device *class,
295 struct device_attribute *attr, const char *buf, size_t count)
297 struct video_device *vdev = to_video_device(class);
298 struct stk_camera *dev = vdev_to_camera(vdev);
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;
310 static ssize_t show_vflip(struct device *class,
311 struct device_attribute *attr, char *buf)
313 struct video_device *vdev = to_video_device(class);
314 struct stk_camera *dev = vdev_to_camera(vdev);
316 return sprintf(buf, "%d\n", dev->vsettings.vflip);
319 static ssize_t store_vflip(struct device *class,
320 struct device_attribute *attr, const char *buf, size_t count)
322 struct video_device *vdev = to_video_device(class);
323 struct stk_camera *dev = vdev_to_camera(vdev);
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;
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);
340 static int stk_create_sysfs_files(struct video_device *vdev)
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);
348 STK_WARNING("Could not create sysfs files\n");
352 static void stk_remove_sysfs_files(struct video_device *vdev)
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);
360 #define stk_create_sysfs_files(a)
361 #define stk_remove_sysfs_files(a)
364 /* *********************************************** */
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.
370 static void stk_isoc_handler(struct urb *urb)
377 unsigned char *fill = NULL;
378 unsigned char *iso_buf = NULL;
380 struct stk_camera *dev;
381 struct stk_sio_buffer *fb;
383 dev = (struct stk_camera *) urb->context;
386 STK_ERROR("isoc_handler called with NULL device !\n");
390 if (urb->status == -ENOENT || urb->status == -ECONNRESET
391 || urb->status == -ESHUTDOWN) {
392 atomic_dec(&dev->urbs_used);
396 spin_lock_irqsave(&dev->spinlock, flags);
398 if (urb->status != -EINPROGRESS && urb->status != 0) {
399 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
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"));
409 fb = list_first_entry(&dev->sio_avail,
410 struct stk_sio_buffer, list);
411 fill = fb->buffer + fb->v4lbuf.bytesused;
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);
420 framelen = urb->iso_frame_desc[i].actual_length;
421 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
424 continue; /* no data */
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
435 if (*iso_buf & 0x80) {
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;
447 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
448 list_move_tail(dev->sio_avail.next,
450 wake_up(&dev->wait_frame);
451 if (list_empty(&dev->sio_avail)) {
452 (void) (printk_ratelimit() &&
453 STK_ERROR("No buffer available\n"));
456 fb = list_first_entry(&dev->sio_avail,
457 struct stk_sio_buffer, list);
458 fb->v4lbuf.bytesused = 0;
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? */
473 spin_unlock_irqrestore(&dev->spinlock, flags);
474 memcpy(fill, iso_buf, framelen);
475 spin_lock_irqsave(&dev->spinlock, flags);
478 /* New size of our buffer */
479 fb->v4lbuf.bytesused += framelen;
483 spin_unlock_irqrestore(&dev->spinlock, flags);
484 urb->dev = dev->udev;
485 ret = usb_submit_urb(urb, GFP_ATOMIC);
487 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
492 /* -------------------------------------------- */
494 static int stk_prepare_iso(struct stk_camera *dev)
499 struct usb_device *udev;
506 STK_ERROR("isobufs already allocated. Bad\n");
508 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
510 if (dev->isobufs == NULL) {
511 STK_ERROR("Unable to allocate iso buffers\n");
514 for (i = 0; i < MAX_ISO_BUFS; i++) {
515 if (dev->isobufs[i].data == NULL) {
516 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
518 STK_ERROR("Failed to allocate iso buffer %d\n",
522 dev->isobufs[i].data = kbuf;
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);
528 STK_ERROR("Failed to allocate URB %d\n", i);
531 dev->isobufs[i].urb = urb;
533 STK_ERROR("Killing URB\n");
534 usb_kill_urb(dev->isobufs[i].urb);
535 urb = dev->isobufs[i].urb;
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;
545 urb->start_frame = 0;
546 urb->number_of_packets = ISO_FRAMES_PER_DESC;
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;
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);
566 static void stk_clean_iso(struct stk_camera *dev)
570 if (dev == NULL || dev->isobufs == NULL)
573 for (i = 0; i < MAX_ISO_BUFS; i++) {
576 urb = dev->isobufs[i].urb;
578 if (atomic_read(&dev->urbs_used))
582 kfree(dev->isobufs[i].data);
586 unset_memallocd(dev);
589 static int stk_setup_siobuf(struct stk_camera *dev, int index)
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)
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;
607 static int stk_free_sio_buffers(struct stk_camera *dev)
612 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
615 * If any buffers are mapped, we cannot free them at all.
617 for (i = 0; i < dev->n_sbufs; i++) {
618 if (dev->sio_bufs[i].mapcount > 0)
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;
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);
634 kfree(dev->sio_bufs);
635 dev->sio_bufs = NULL;
639 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
642 if (dev->sio_bufs != NULL)
643 STK_ERROR("sio_bufs already allocated\n");
645 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
647 if (dev->sio_bufs == NULL)
649 for (i = 0; i < n_sbufs; i++) {
650 if (stk_setup_siobuf(dev, i))
651 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
658 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
661 err = stk_prepare_iso(dev);
666 err = stk_prepare_sio_buffers(dev, n_sbufs);
668 stk_free_sio_buffers(dev);
674 static void stk_free_buffers(struct stk_camera *dev)
677 stk_free_sio_buffers(dev);
679 /* -------------------------------------------- */
681 /* v4l file operations */
683 static int v4l_stk_open(struct inode *inode, struct file *fp)
685 struct stk_camera *dev;
686 struct video_device *vdev;
688 vdev = video_devdata(fp);
689 dev = vdev_to_camera(vdev);
691 if (dev == NULL || !is_present(dev))
693 fp->private_data = vdev;
694 kref_get(&dev->kref);
695 usb_autopm_get_interface(dev->interface);
700 static int v4l_stk_release(struct inode *inode, struct file *fp)
702 struct stk_camera *dev;
703 struct video_device *vdev;
705 vdev = video_devdata(fp);
707 STK_ERROR("v4l_release called w/o video devdata\n");
710 dev = vdev_to_camera(vdev);
712 STK_ERROR("v4l_release called on removed device\n");
716 if (dev->owner != fp) {
717 usb_autopm_put_interface(dev->interface);
718 kref_put(&dev->kref, stk_camera_cleanup);
722 stk_stop_stream(dev);
724 stk_free_buffers(dev);
728 usb_autopm_put_interface(dev->interface);
729 kref_put(&dev->kref, stk_camera_cleanup);
734 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
735 size_t count, loff_t *f_pos)
740 struct stk_camera *dev;
741 struct video_device *vdev;
742 struct stk_sio_buffer *sbuf;
744 vdev = video_devdata(fp);
747 dev = vdev_to_camera(vdev);
752 if (!is_present(dev))
754 if (dev->owner && dev->owner != fp)
757 if (!is_streaming(dev)) {
758 if (stk_initialise(dev)
759 || stk_allocate_buffers(dev, 3)
760 || stk_start_stream(dev))
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;
767 spin_unlock_irqrestore(&dev->spinlock, flags);
770 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
772 ret = wait_event_interruptible(dev->wait_frame,
773 !list_empty(&dev->sio_full) || !is_present(dev));
776 if (!is_present(dev))
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");
787 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
788 spin_unlock_irqrestore(&dev->spinlock, flags);
790 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
795 if (*f_pos >= dev->frame_size) {
797 spin_lock_irqsave(&dev->spinlock, flags);
798 list_move_tail(&sbuf->list, &dev->sio_avail);
799 spin_unlock_irqrestore(&dev->spinlock, flags);
804 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
806 struct stk_camera *dev;
807 struct video_device *vdev;
809 vdev = video_devdata(fp);
814 dev = vdev_to_camera(vdev);
818 poll_wait(fp, &dev->wait_frame, wait);
820 if (!is_present(dev))
823 if (!list_empty(&dev->sio_full))
824 return (POLLIN | POLLRDNORM);
830 static void stk_v4l_vm_open(struct vm_area_struct *vma)
832 struct stk_sio_buffer *sbuf = vma->vm_private_data;
835 static void stk_v4l_vm_close(struct vm_area_struct *vma)
837 struct stk_sio_buffer *sbuf = vma->vm_private_data;
839 if (sbuf->mapcount == 0)
840 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
842 static struct vm_operations_struct stk_v4l_vm_ops = {
843 .open = stk_v4l_vm_open,
844 .close = stk_v4l_vm_close
847 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
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;
856 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
859 vdev = video_devdata(fp);
860 dev = vdev_to_camera(vdev);
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;
870 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
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);
881 /* v4l ioctl handlers */
883 static int stk_vidioc_querycap(struct file *filp,
884 void *priv, struct v4l2_capability *cap)
886 strcpy(cap->driver, "stk");
887 strcpy(cap->card, "stk");
888 cap->version = DRIVER_VERSION_NUM;
890 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
891 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
895 static int stk_vidioc_enum_input(struct file *filp,
896 void *priv, struct v4l2_input *input)
898 if (input->index != 0)
901 strcpy(input->name, "Syntek USB Camera");
902 input->type = V4L2_INPUT_TYPE_CAMERA;
907 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
913 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
922 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
927 /* List of all V4Lv2 controls supported by the driver */
928 static struct v4l2_queryctrl stk_controls[] = {
930 .id = V4L2_CID_BRIGHTNESS,
931 .type = V4L2_CTRL_TYPE_INTEGER,
932 .name = "Brightness",
936 .default_value = 0x6000,
938 /*TODO: get more controls to work */
941 static int stk_vidioc_queryctrl(struct file *filp,
942 void *priv, struct v4l2_queryctrl *c)
946 nbr = ARRAY_SIZE(stk_controls);
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));
958 static int stk_vidioc_g_ctrl(struct file *filp,
959 void *priv, struct v4l2_control *c)
961 struct stk_camera *dev = priv;
963 case V4L2_CID_BRIGHTNESS:
964 c->value = dev->vsettings.brightness;
972 static int stk_vidioc_s_ctrl(struct file *filp,
973 void *priv, struct v4l2_control *c)
975 struct stk_camera *dev = priv;
977 case V4L2_CID_BRIGHTNESS:
978 dev->vsettings.brightness = c->value;
979 return stk_sensor_set_brightness(dev, c->value >> 8);
987 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
988 void *priv, struct v4l2_fmtdesc *fmtd)
992 switch (fmtd->index) {
994 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
995 strcpy(fmtd->description, "r5g6b5");
998 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
999 strcpy(fmtd->description, "r5g6b5BE");
1002 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
1003 strcpy(fmtd->description, "yuv4:2:2");
1006 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
1007 strcpy(fmtd->description, "Raw bayer");
1010 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1011 strcpy(fmtd->description, "yuv4:2:2");
1019 static struct stk_size {
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, },
1031 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
1032 void *priv, struct v4l2_format *f)
1034 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1035 struct stk_camera *dev = priv;
1038 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1039 && stk_sizes[i].m != dev->vsettings.mode;
1041 if (i == ARRAY_SIZE(stk_sizes)) {
1042 STK_ERROR("ERROR: mode invalid\n");
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;
1054 pix_format->bytesperline = 2 * pix_format->width;
1055 pix_format->sizeimage = pix_format->bytesperline
1056 * pix_format->height;
1060 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
1061 void *priv, struct v4l2_format *fmtd)
1064 switch (fmtd->fmt.pix.pixelformat) {
1065 case V4L2_PIX_FMT_RGB565:
1066 case V4L2_PIX_FMT_RGB565X:
1067 case V4L2_PIX_FMT_UYVY:
1068 case V4L2_PIX_FMT_YUYV:
1069 case V4L2_PIX_FMT_SBGGR8:
1074 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1075 if (fmtd->fmt.pix.width > stk_sizes[i].w)
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;
1085 fmtd->fmt.pix.height = stk_sizes[i].h;
1086 fmtd->fmt.pix.width = stk_sizes[i].w;
1087 fmtd->fmt.pix.priv = i;
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;
1095 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1096 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1097 * fmtd->fmt.pix.height;
1101 static int stk_setup_format(struct stk_camera *dev)
1105 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1109 while (stk_sizes[i].m != dev->vsettings.mode
1110 && i < ARRAY_SIZE(stk_sizes))
1112 if (i == ARRAY_SIZE(stk_sizes)) {
1113 STK_ERROR("Something is broken in %s\n", __func__);
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);
1121 stk_camera_write_reg(dev, 0x001c, 0x46);
1123 * Registers 0x0115 0x0114 are the size of each line (bytes),
1124 * regs 0x0117 0x0116 are the heigth of the image.
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);
1137 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1138 void *priv, struct v4l2_format *fmtd)
1141 struct stk_camera *dev = priv;
1145 if (!is_present(dev))
1147 if (is_streaming(dev))
1149 if (dev->owner && dev->owner != filp)
1151 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
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;
1161 stk_initialise(dev);
1162 return stk_setup_format(dev);
1165 static int stk_vidioc_reqbufs(struct file *filp,
1166 void *priv, struct v4l2_requestbuffers *rb)
1168 struct stk_camera *dev = priv;
1172 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1174 if (rb->memory != V4L2_MEMORY_MMAP)
1176 if (is_streaming(dev)
1177 || (dev->owner && dev->owner != filp))
1181 /*FIXME If they ask for zero, we must stop streaming and free */
1184 /* Arbitrary limit */
1185 else if (rb->count > 5)
1188 stk_allocate_buffers(dev, rb->count);
1189 rb->count = dev->n_sbufs;
1193 static int stk_vidioc_querybuf(struct file *filp,
1194 void *priv, struct v4l2_buffer *buf)
1197 struct stk_camera *dev = priv;
1198 struct stk_sio_buffer *sbuf;
1200 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1205 if (index < 0 || index >= dev->n_sbufs)
1207 sbuf = dev->sio_bufs + buf->index;
1208 *buf = sbuf->v4lbuf;
1212 static int stk_vidioc_qbuf(struct file *filp,
1213 void *priv, struct v4l2_buffer *buf)
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)
1221 if (buf->memory != V4L2_MEMORY_MMAP)
1224 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1226 sbuf = dev->sio_bufs + buf->index;
1227 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
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);
1238 static int stk_vidioc_dqbuf(struct file *filp,
1239 void *priv, struct v4l2_buffer *buf)
1241 struct stk_camera *dev = priv;
1242 struct stk_sio_buffer *sbuf;
1243 unsigned long flags;
1246 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1247 || !is_streaming(dev))
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));
1256 if (!is_present(dev))
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);
1268 *buf = sbuf->v4lbuf;
1272 static int stk_vidioc_streamon(struct file *filp,
1273 void *priv, enum v4l2_buf_type type)
1275 struct stk_camera *dev = priv;
1276 if (is_streaming(dev))
1278 if (dev->sio_bufs == NULL)
1281 return stk_start_stream(dev);
1284 static int stk_vidioc_streamoff(struct file *filp,
1285 void *priv, enum v4l2_buf_type type)
1287 struct stk_camera *dev = priv;
1288 unsigned long flags;
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;
1298 spin_unlock_irqrestore(&dev->spinlock, flags);
1303 static int stk_vidioc_g_parm(struct file *filp,
1304 void *priv, struct v4l2_streamparm *sp)
1306 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
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;
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,
1327 #ifdef CONFIG_COMPAT
1328 .compat_ioctl = v4l_compat_ioctl32,
1333 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1334 .vidioc_querycap = stk_vidioc_querycap,
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,
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,
1355 static void stk_v4l_dev_release(struct video_device *vd)
1359 static struct video_device stk_v4l_data = {
1360 .name = "stkwebcam",
1361 .type = VFL_TYPE_GRABBER,
1362 .type2 = VID_TYPE_CAPTURE,
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,
1372 static int stk_register_video_device(struct stk_camera *dev)
1376 dev->vdev = stk_v4l_data;
1377 dev->vdev.debug = debug;
1378 dev->vdev.parent = &dev->interface->dev;
1379 dev->vdev.priv = dev;
1380 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1382 STK_ERROR("v4l registration failed\n");
1384 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1385 " /dev/video%d\n", dev->vdev.minor);
1392 static int stk_camera_probe(struct usb_interface *interface,
1393 const struct usb_device_id *id)
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;
1403 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1405 STK_ERROR("Out of memory !\n");
1409 kref_init(&dev->kref);
1410 spin_lock_init(&dev->spinlock);
1411 init_waitqueue_head(&dev->wait_frame);
1414 dev->interface = interface;
1415 usb_get_intf(interface);
1417 dev->vsettings.vflip = vflip;
1418 dev->vsettings.hflip = hflip;
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;
1427 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1428 endpoint = &iface_desc->endpoint[i].desc;
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);
1440 if (!dev->isoc_ep) {
1441 STK_ERROR("Could not find isoc-in endpoint");
1442 kref_put(&dev->kref, stk_camera_cleanup);
1445 dev->vsettings.brightness = 0x7fff;
1446 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1447 dev->vsettings.mode = MODE_VGA;
1448 dev->frame_size = 640 * 480 * 2;
1450 INIT_LIST_HEAD(&dev->sio_avail);
1451 INIT_LIST_HEAD(&dev->sio_full);
1453 usb_set_intfdata(interface, dev);
1455 err = stk_register_video_device(dev);
1457 kref_put(&dev->kref, stk_camera_cleanup);
1461 stk_create_sysfs_files(&dev->vdev);
1462 usb_autopm_enable(dev->interface);
1467 static void stk_camera_disconnect(struct usb_interface *interface)
1469 struct stk_camera *dev = usb_get_intfdata(interface);
1471 usb_set_intfdata(interface, NULL);
1474 wake_up_interruptible(&dev->wait_frame);
1475 stk_remove_sysfs_files(&dev->vdev);
1477 kref_put(&dev->kref, stk_camera_cleanup);
1481 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1483 struct stk_camera *dev = usb_get_intfdata(intf);
1484 if (is_streaming(dev)) {
1485 stk_stop_stream(dev);
1486 /* yes, this is ugly */
1492 static int stk_camera_resume(struct usb_interface *intf)
1494 struct stk_camera *dev = usb_get_intfdata(intf);
1495 if (!is_initialised(dev))
1497 unset_initialised(dev);
1498 stk_initialise(dev);
1499 stk_setup_format(dev);
1500 if (is_streaming(dev))
1501 stk_start_stream(dev);
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,
1512 .suspend = stk_camera_suspend,
1513 .resume = stk_camera_resume,
1518 static int __init stk_camera_init(void)
1522 result = usb_register(&stk_camera_driver);
1524 STK_ERROR("usb_register failed ! Error number %d\n", result);
1530 static void __exit stk_camera_exit(void)
1532 usb_deregister(&stk_camera_driver);
1535 module_init(stk_camera_init);
1536 module_exit(stk_camera_exit);