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 if (list_is_singular(&dev->sio_avail)) {
449 /* Always reuse the last buffer */
450 fb->v4lbuf.bytesused = 0;
453 list_move_tail(dev->sio_avail.next,
455 wake_up(&dev->wait_frame);
456 fb = list_first_entry(&dev->sio_avail,
457 struct stk_sio_buffer, list);
458 fb->v4lbuf.bytesused = 0;
467 /* Our buffer is full !!! */
468 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
469 (void) (printk_ratelimit() &&
470 STK_ERROR("Frame buffer overflow, lost sync\n"));
471 /*FIXME Do something here? */
474 spin_unlock_irqrestore(&dev->spinlock, flags);
475 memcpy(fill, iso_buf, framelen);
476 spin_lock_irqsave(&dev->spinlock, flags);
479 /* New size of our buffer */
480 fb->v4lbuf.bytesused += framelen;
484 spin_unlock_irqrestore(&dev->spinlock, flags);
485 urb->dev = dev->udev;
486 ret = usb_submit_urb(urb, GFP_ATOMIC);
488 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
493 /* -------------------------------------------- */
495 static int stk_prepare_iso(struct stk_camera *dev)
500 struct usb_device *udev;
507 STK_ERROR("isobufs already allocated. Bad\n");
509 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
511 if (dev->isobufs == NULL) {
512 STK_ERROR("Unable to allocate iso buffers\n");
515 for (i = 0; i < MAX_ISO_BUFS; i++) {
516 if (dev->isobufs[i].data == NULL) {
517 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
519 STK_ERROR("Failed to allocate iso buffer %d\n",
523 dev->isobufs[i].data = kbuf;
525 STK_ERROR("isobuf data already allocated\n");
526 if (dev->isobufs[i].urb == NULL) {
527 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
529 STK_ERROR("Failed to allocate URB %d\n", i);
532 dev->isobufs[i].urb = urb;
534 STK_ERROR("Killing URB\n");
535 usb_kill_urb(dev->isobufs[i].urb);
536 urb = dev->isobufs[i].urb;
540 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
541 urb->transfer_flags = URB_ISO_ASAP;
542 urb->transfer_buffer = dev->isobufs[i].data;
543 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
544 urb->complete = stk_isoc_handler;
546 urb->start_frame = 0;
547 urb->number_of_packets = ISO_FRAMES_PER_DESC;
549 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
550 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
551 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
558 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
559 kfree(dev->isobufs[i].data);
560 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
561 usb_free_urb(dev->isobufs[i].urb);
567 static void stk_clean_iso(struct stk_camera *dev)
571 if (dev == NULL || dev->isobufs == NULL)
574 for (i = 0; i < MAX_ISO_BUFS; i++) {
577 urb = dev->isobufs[i].urb;
579 if (atomic_read(&dev->urbs_used))
583 kfree(dev->isobufs[i].data);
587 unset_memallocd(dev);
590 static int stk_setup_siobuf(struct stk_camera *dev, int index)
592 struct stk_sio_buffer *buf = dev->sio_bufs + index;
593 INIT_LIST_HEAD(&buf->list);
594 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
595 buf->buffer = vmalloc_user(buf->v4lbuf.length);
596 if (buf->buffer == NULL)
600 buf->v4lbuf.index = index;
601 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
602 buf->v4lbuf.field = V4L2_FIELD_NONE;
603 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
604 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
608 static int stk_free_sio_buffers(struct stk_camera *dev)
613 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
616 * If any buffers are mapped, we cannot free them at all.
618 for (i = 0; i < dev->n_sbufs; i++) {
619 if (dev->sio_bufs[i].mapcount > 0)
625 spin_lock_irqsave(&dev->spinlock, flags);
626 INIT_LIST_HEAD(&dev->sio_avail);
627 INIT_LIST_HEAD(&dev->sio_full);
628 nbufs = dev->n_sbufs;
630 spin_unlock_irqrestore(&dev->spinlock, flags);
631 for (i = 0; i < nbufs; i++) {
632 if (dev->sio_bufs[i].buffer != NULL)
633 vfree(dev->sio_bufs[i].buffer);
635 kfree(dev->sio_bufs);
636 dev->sio_bufs = NULL;
640 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
643 if (dev->sio_bufs != NULL)
644 STK_ERROR("sio_bufs already allocated\n");
646 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
648 if (dev->sio_bufs == NULL)
650 for (i = 0; i < n_sbufs; i++) {
651 if (stk_setup_siobuf(dev, i))
652 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
659 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
662 err = stk_prepare_iso(dev);
667 err = stk_prepare_sio_buffers(dev, n_sbufs);
669 stk_free_sio_buffers(dev);
675 static void stk_free_buffers(struct stk_camera *dev)
678 stk_free_sio_buffers(dev);
680 /* -------------------------------------------- */
682 /* v4l file operations */
684 static int v4l_stk_open(struct inode *inode, struct file *fp)
686 struct stk_camera *dev;
687 struct video_device *vdev;
689 vdev = video_devdata(fp);
690 dev = vdev_to_camera(vdev);
692 if (dev == NULL || !is_present(dev))
694 fp->private_data = vdev;
695 kref_get(&dev->kref);
696 usb_autopm_get_interface(dev->interface);
701 static int v4l_stk_release(struct inode *inode, struct file *fp)
703 struct stk_camera *dev;
704 struct video_device *vdev;
706 vdev = video_devdata(fp);
708 STK_ERROR("v4l_release called w/o video devdata\n");
711 dev = vdev_to_camera(vdev);
713 STK_ERROR("v4l_release called on removed device\n");
717 if (dev->owner != fp) {
718 usb_autopm_put_interface(dev->interface);
719 kref_put(&dev->kref, stk_camera_cleanup);
723 stk_stop_stream(dev);
725 stk_free_buffers(dev);
729 usb_autopm_put_interface(dev->interface);
730 kref_put(&dev->kref, stk_camera_cleanup);
735 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
736 size_t count, loff_t *f_pos)
741 struct stk_camera *dev;
742 struct video_device *vdev;
743 struct stk_sio_buffer *sbuf;
745 vdev = video_devdata(fp);
748 dev = vdev_to_camera(vdev);
753 if (!is_present(dev))
755 if (dev->owner && dev->owner != fp)
758 if (!is_streaming(dev)) {
759 if (stk_initialise(dev)
760 || stk_allocate_buffers(dev, 3)
761 || stk_start_stream(dev))
763 spin_lock_irqsave(&dev->spinlock, flags);
764 for (i = 0; i < dev->n_sbufs; i++) {
765 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
766 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
768 spin_unlock_irqrestore(&dev->spinlock, flags);
771 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
773 ret = wait_event_interruptible(dev->wait_frame,
774 !list_empty(&dev->sio_full) || !is_present(dev));
777 if (!is_present(dev))
780 if (count + *f_pos > dev->frame_size)
781 count = dev->frame_size - *f_pos;
782 spin_lock_irqsave(&dev->spinlock, flags);
783 if (list_empty(&dev->sio_full)) {
784 spin_unlock_irqrestore(&dev->spinlock, flags);
785 STK_ERROR("BUG: No siobufs ready\n");
788 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
789 spin_unlock_irqrestore(&dev->spinlock, flags);
791 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
796 if (*f_pos >= dev->frame_size) {
798 spin_lock_irqsave(&dev->spinlock, flags);
799 list_move_tail(&sbuf->list, &dev->sio_avail);
800 spin_unlock_irqrestore(&dev->spinlock, flags);
805 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
807 struct stk_camera *dev;
808 struct video_device *vdev;
810 vdev = video_devdata(fp);
815 dev = vdev_to_camera(vdev);
819 poll_wait(fp, &dev->wait_frame, wait);
821 if (!is_present(dev))
824 if (!list_empty(&dev->sio_full))
825 return (POLLIN | POLLRDNORM);
831 static void stk_v4l_vm_open(struct vm_area_struct *vma)
833 struct stk_sio_buffer *sbuf = vma->vm_private_data;
836 static void stk_v4l_vm_close(struct vm_area_struct *vma)
838 struct stk_sio_buffer *sbuf = vma->vm_private_data;
840 if (sbuf->mapcount == 0)
841 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
843 static struct vm_operations_struct stk_v4l_vm_ops = {
844 .open = stk_v4l_vm_open,
845 .close = stk_v4l_vm_close
848 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
852 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
853 struct stk_camera *dev;
854 struct video_device *vdev;
855 struct stk_sio_buffer *sbuf = NULL;
857 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
860 vdev = video_devdata(fp);
861 dev = vdev_to_camera(vdev);
863 for (i = 0; i < dev->n_sbufs; i++) {
864 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
865 sbuf = dev->sio_bufs + i;
871 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
874 vma->vm_flags |= VM_DONTEXPAND;
875 vma->vm_private_data = sbuf;
876 vma->vm_ops = &stk_v4l_vm_ops;
877 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
878 stk_v4l_vm_open(vma);
882 /* v4l ioctl handlers */
884 static int stk_vidioc_querycap(struct file *filp,
885 void *priv, struct v4l2_capability *cap)
887 strcpy(cap->driver, "stk");
888 strcpy(cap->card, "stk");
889 cap->version = DRIVER_VERSION_NUM;
891 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
892 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
896 static int stk_vidioc_enum_input(struct file *filp,
897 void *priv, struct v4l2_input *input)
899 if (input->index != 0)
902 strcpy(input->name, "Syntek USB Camera");
903 input->type = V4L2_INPUT_TYPE_CAMERA;
908 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
914 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
923 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
928 /* List of all V4Lv2 controls supported by the driver */
929 static struct v4l2_queryctrl stk_controls[] = {
931 .id = V4L2_CID_BRIGHTNESS,
932 .type = V4L2_CTRL_TYPE_INTEGER,
933 .name = "Brightness",
937 .default_value = 0x6000,
939 /*TODO: get more controls to work */
942 static int stk_vidioc_queryctrl(struct file *filp,
943 void *priv, struct v4l2_queryctrl *c)
947 nbr = ARRAY_SIZE(stk_controls);
949 for (i = 0; i < nbr; i++) {
950 if (stk_controls[i].id == c->id) {
951 memcpy(c, &stk_controls[i],
952 sizeof(struct v4l2_queryctrl));
959 static int stk_vidioc_g_ctrl(struct file *filp,
960 void *priv, struct v4l2_control *c)
962 struct stk_camera *dev = priv;
964 case V4L2_CID_BRIGHTNESS:
965 c->value = dev->vsettings.brightness;
973 static int stk_vidioc_s_ctrl(struct file *filp,
974 void *priv, struct v4l2_control *c)
976 struct stk_camera *dev = priv;
978 case V4L2_CID_BRIGHTNESS:
979 dev->vsettings.brightness = c->value;
980 return stk_sensor_set_brightness(dev, c->value >> 8);
988 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
989 void *priv, struct v4l2_fmtdesc *fmtd)
993 switch (fmtd->index) {
995 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
996 strcpy(fmtd->description, "r5g6b5");
999 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
1000 strcpy(fmtd->description, "r5g6b5BE");
1003 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
1004 strcpy(fmtd->description, "yuv4:2:2");
1007 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
1008 strcpy(fmtd->description, "Raw bayer");
1011 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1012 strcpy(fmtd->description, "yuv4:2:2");
1020 static struct stk_size {
1025 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1026 { .w = 640, .h = 480, .m = MODE_VGA, },
1027 { .w = 352, .h = 288, .m = MODE_CIF, },
1028 { .w = 320, .h = 240, .m = MODE_QVGA, },
1029 { .w = 176, .h = 144, .m = MODE_QCIF, },
1032 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
1033 void *priv, struct v4l2_format *f)
1035 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1036 struct stk_camera *dev = priv;
1039 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1040 && stk_sizes[i].m != dev->vsettings.mode;
1042 if (i == ARRAY_SIZE(stk_sizes)) {
1043 STK_ERROR("ERROR: mode invalid\n");
1046 pix_format->width = stk_sizes[i].w;
1047 pix_format->height = stk_sizes[i].h;
1048 pix_format->field = V4L2_FIELD_NONE;
1049 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1050 pix_format->priv = 0;
1051 pix_format->pixelformat = dev->vsettings.palette;
1052 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1053 pix_format->bytesperline = pix_format->width;
1055 pix_format->bytesperline = 2 * pix_format->width;
1056 pix_format->sizeimage = pix_format->bytesperline
1057 * pix_format->height;
1061 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
1062 void *priv, struct v4l2_format *fmtd)
1065 switch (fmtd->fmt.pix.pixelformat) {
1066 case V4L2_PIX_FMT_RGB565:
1067 case V4L2_PIX_FMT_RGB565X:
1068 case V4L2_PIX_FMT_UYVY:
1069 case V4L2_PIX_FMT_YUYV:
1070 case V4L2_PIX_FMT_SBGGR8:
1075 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1076 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1079 if (i == ARRAY_SIZE(stk_sizes)
1080 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1081 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1082 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1083 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1084 fmtd->fmt.pix.priv = i - 1;
1086 fmtd->fmt.pix.height = stk_sizes[i].h;
1087 fmtd->fmt.pix.width = stk_sizes[i].w;
1088 fmtd->fmt.pix.priv = i;
1091 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1092 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1093 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1094 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1096 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1097 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1098 * fmtd->fmt.pix.height;
1102 static int stk_setup_format(struct stk_camera *dev)
1106 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1110 while (stk_sizes[i].m != dev->vsettings.mode
1111 && i < ARRAY_SIZE(stk_sizes))
1113 if (i == ARRAY_SIZE(stk_sizes)) {
1114 STK_ERROR("Something is broken in %s\n", __func__);
1117 /* This registers controls some timings, not sure of what. */
1118 stk_camera_write_reg(dev, 0x001b, 0x0e);
1119 if (dev->vsettings.mode == MODE_SXGA)
1120 stk_camera_write_reg(dev, 0x001c, 0x0e);
1122 stk_camera_write_reg(dev, 0x001c, 0x46);
1124 * Registers 0x0115 0x0114 are the size of each line (bytes),
1125 * regs 0x0117 0x0116 are the heigth of the image.
1127 stk_camera_write_reg(dev, 0x0115,
1128 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1129 stk_camera_write_reg(dev, 0x0114,
1130 (stk_sizes[i].w * depth) & 0xff);
1131 stk_camera_write_reg(dev, 0x0117,
1132 (stk_sizes[i].h >> 8) & 0xff);
1133 stk_camera_write_reg(dev, 0x0116,
1134 stk_sizes[i].h & 0xff);
1135 return stk_sensor_configure(dev);
1138 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1139 void *priv, struct v4l2_format *fmtd)
1142 struct stk_camera *dev = priv;
1146 if (!is_present(dev))
1148 if (is_streaming(dev))
1150 if (dev->owner && dev->owner != filp)
1152 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1157 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1158 stk_free_buffers(dev);
1159 dev->frame_size = fmtd->fmt.pix.sizeimage;
1160 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1162 stk_initialise(dev);
1163 return stk_setup_format(dev);
1166 static int stk_vidioc_reqbufs(struct file *filp,
1167 void *priv, struct v4l2_requestbuffers *rb)
1169 struct stk_camera *dev = priv;
1173 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1175 if (rb->memory != V4L2_MEMORY_MMAP)
1177 if (is_streaming(dev)
1178 || (dev->owner && dev->owner != filp))
1182 /*FIXME If they ask for zero, we must stop streaming and free */
1185 /* Arbitrary limit */
1186 else if (rb->count > 5)
1189 stk_allocate_buffers(dev, rb->count);
1190 rb->count = dev->n_sbufs;
1194 static int stk_vidioc_querybuf(struct file *filp,
1195 void *priv, struct v4l2_buffer *buf)
1198 struct stk_camera *dev = priv;
1199 struct stk_sio_buffer *sbuf;
1201 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1206 if (index < 0 || index >= dev->n_sbufs)
1208 sbuf = dev->sio_bufs + buf->index;
1209 *buf = sbuf->v4lbuf;
1213 static int stk_vidioc_qbuf(struct file *filp,
1214 void *priv, struct v4l2_buffer *buf)
1216 struct stk_camera *dev = priv;
1217 struct stk_sio_buffer *sbuf;
1218 unsigned long flags;
1219 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1222 if (buf->memory != V4L2_MEMORY_MMAP)
1225 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1227 sbuf = dev->sio_bufs + buf->index;
1228 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1230 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1231 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1232 spin_lock_irqsave(&dev->spinlock, flags);
1233 list_add_tail(&sbuf->list, &dev->sio_avail);
1234 *buf = sbuf->v4lbuf;
1235 spin_unlock_irqrestore(&dev->spinlock, flags);
1239 static int stk_vidioc_dqbuf(struct file *filp,
1240 void *priv, struct v4l2_buffer *buf)
1242 struct stk_camera *dev = priv;
1243 struct stk_sio_buffer *sbuf;
1244 unsigned long flags;
1247 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1248 || !is_streaming(dev))
1251 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1252 return -EWOULDBLOCK;
1253 ret = wait_event_interruptible(dev->wait_frame,
1254 !list_empty(&dev->sio_full) || !is_present(dev));
1257 if (!is_present(dev))
1260 spin_lock_irqsave(&dev->spinlock, flags);
1261 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1262 list_del_init(&sbuf->list);
1263 spin_unlock_irqrestore(&dev->spinlock, flags);
1264 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1265 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1266 sbuf->v4lbuf.sequence = ++dev->sequence;
1267 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1269 *buf = sbuf->v4lbuf;
1273 static int stk_vidioc_streamon(struct file *filp,
1274 void *priv, enum v4l2_buf_type type)
1276 struct stk_camera *dev = priv;
1277 if (is_streaming(dev))
1279 if (dev->sio_bufs == NULL)
1282 return stk_start_stream(dev);
1285 static int stk_vidioc_streamoff(struct file *filp,
1286 void *priv, enum v4l2_buf_type type)
1288 struct stk_camera *dev = priv;
1289 unsigned long flags;
1291 stk_stop_stream(dev);
1292 spin_lock_irqsave(&dev->spinlock, flags);
1293 INIT_LIST_HEAD(&dev->sio_avail);
1294 INIT_LIST_HEAD(&dev->sio_full);
1295 for (i = 0; i < dev->n_sbufs; i++) {
1296 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1297 dev->sio_bufs[i].v4lbuf.flags = 0;
1299 spin_unlock_irqrestore(&dev->spinlock, flags);
1304 static int stk_vidioc_g_parm(struct file *filp,
1305 void *priv, struct v4l2_streamparm *sp)
1307 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1310 sp->parm.capture.capability = 0;
1311 sp->parm.capture.capturemode = 0;
1312 /*FIXME This is not correct */
1313 sp->parm.capture.timeperframe.numerator = 1;
1314 sp->parm.capture.timeperframe.denominator = 30;
1315 sp->parm.capture.readbuffers = 2;
1316 sp->parm.capture.extendedmode = 0;
1320 static struct file_operations v4l_stk_fops = {
1321 .owner = THIS_MODULE,
1322 .open = v4l_stk_open,
1323 .release = v4l_stk_release,
1324 .read = v4l_stk_read,
1325 .poll = v4l_stk_poll,
1326 .mmap = v4l_stk_mmap,
1327 .ioctl = video_ioctl2,
1328 #ifdef CONFIG_COMPAT
1329 .compat_ioctl = v4l_compat_ioctl32,
1334 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1335 .vidioc_querycap = stk_vidioc_querycap,
1336 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1337 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1338 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1339 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1340 .vidioc_enum_input = stk_vidioc_enum_input,
1341 .vidioc_s_input = stk_vidioc_s_input,
1342 .vidioc_g_input = stk_vidioc_g_input,
1343 .vidioc_s_std = stk_vidioc_s_std,
1344 .vidioc_reqbufs = stk_vidioc_reqbufs,
1345 .vidioc_querybuf = stk_vidioc_querybuf,
1346 .vidioc_qbuf = stk_vidioc_qbuf,
1347 .vidioc_dqbuf = stk_vidioc_dqbuf,
1348 .vidioc_streamon = stk_vidioc_streamon,
1349 .vidioc_streamoff = stk_vidioc_streamoff,
1350 .vidioc_queryctrl = stk_vidioc_queryctrl,
1351 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1352 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1353 .vidioc_g_parm = stk_vidioc_g_parm,
1356 static void stk_v4l_dev_release(struct video_device *vd)
1360 static struct video_device stk_v4l_data = {
1361 .name = "stkwebcam",
1362 .type = VFL_TYPE_GRABBER,
1363 .type2 = VID_TYPE_CAPTURE,
1365 .tvnorms = V4L2_STD_UNKNOWN,
1366 .current_norm = V4L2_STD_UNKNOWN,
1367 .fops = &v4l_stk_fops,
1368 .ioctl_ops = &v4l_stk_ioctl_ops,
1369 .release = stk_v4l_dev_release,
1373 static int stk_register_video_device(struct stk_camera *dev)
1377 dev->vdev = stk_v4l_data;
1378 dev->vdev.debug = debug;
1379 dev->vdev.parent = &dev->interface->dev;
1380 dev->vdev.priv = dev;
1381 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1383 STK_ERROR("v4l registration failed\n");
1385 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1386 " /dev/video%d\n", dev->vdev.minor);
1393 static int stk_camera_probe(struct usb_interface *interface,
1394 const struct usb_device_id *id)
1399 struct stk_camera *dev = NULL;
1400 struct usb_device *udev = interface_to_usbdev(interface);
1401 struct usb_host_interface *iface_desc;
1402 struct usb_endpoint_descriptor *endpoint;
1404 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1406 STK_ERROR("Out of memory !\n");
1410 kref_init(&dev->kref);
1411 spin_lock_init(&dev->spinlock);
1412 init_waitqueue_head(&dev->wait_frame);
1415 dev->interface = interface;
1416 usb_get_intf(interface);
1418 dev->vsettings.vflip = vflip;
1419 dev->vsettings.hflip = hflip;
1423 /* Set up the endpoint information
1424 * use only the first isoc-in endpoint
1425 * for the current alternate setting */
1426 iface_desc = interface->cur_altsetting;
1428 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1429 endpoint = &iface_desc->endpoint[i].desc;
1432 && ((endpoint->bEndpointAddress
1433 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1434 && ((endpoint->bmAttributes
1435 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1436 /* we found an isoc in endpoint */
1437 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1441 if (!dev->isoc_ep) {
1442 STK_ERROR("Could not find isoc-in endpoint");
1443 kref_put(&dev->kref, stk_camera_cleanup);
1446 dev->vsettings.brightness = 0x7fff;
1447 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1448 dev->vsettings.mode = MODE_VGA;
1449 dev->frame_size = 640 * 480 * 2;
1451 INIT_LIST_HEAD(&dev->sio_avail);
1452 INIT_LIST_HEAD(&dev->sio_full);
1454 usb_set_intfdata(interface, dev);
1456 err = stk_register_video_device(dev);
1458 kref_put(&dev->kref, stk_camera_cleanup);
1462 stk_create_sysfs_files(&dev->vdev);
1463 usb_autopm_enable(dev->interface);
1468 static void stk_camera_disconnect(struct usb_interface *interface)
1470 struct stk_camera *dev = usb_get_intfdata(interface);
1472 usb_set_intfdata(interface, NULL);
1475 wake_up_interruptible(&dev->wait_frame);
1476 stk_remove_sysfs_files(&dev->vdev);
1478 kref_put(&dev->kref, stk_camera_cleanup);
1482 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1484 struct stk_camera *dev = usb_get_intfdata(intf);
1485 if (is_streaming(dev)) {
1486 stk_stop_stream(dev);
1487 /* yes, this is ugly */
1493 static int stk_camera_resume(struct usb_interface *intf)
1495 struct stk_camera *dev = usb_get_intfdata(intf);
1496 if (!is_initialised(dev))
1498 unset_initialised(dev);
1499 stk_initialise(dev);
1500 stk_setup_format(dev);
1501 if (is_streaming(dev))
1502 stk_start_stream(dev);
1507 static struct usb_driver stk_camera_driver = {
1508 .name = "stkwebcam",
1509 .probe = stk_camera_probe,
1510 .disconnect = stk_camera_disconnect,
1511 .id_table = stkwebcam_table,
1513 .suspend = stk_camera_suspend,
1514 .resume = stk_camera_resume,
1519 static int __init stk_camera_init(void)
1523 result = usb_register(&stk_camera_driver);
1525 STK_ERROR("usb_register failed ! Error number %d\n", result);
1531 static void __exit stk_camera_exit(void)
1533 usb_deregister(&stk_camera_driver);
1536 module_init(stk_camera_init);
1537 module_exit(stk_camera_exit);