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 video_set_drvdata(&dev->vdev, 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);
693 if (dev == NULL || !is_present(dev)) {
697 fp->private_data = vdev;
698 kref_get(&dev->kref);
699 usb_autopm_get_interface(dev->interface);
705 static int v4l_stk_release(struct inode *inode, struct file *fp)
707 struct stk_camera *dev;
708 struct video_device *vdev;
710 vdev = video_devdata(fp);
712 STK_ERROR("v4l_release called w/o video devdata\n");
715 dev = vdev_to_camera(vdev);
717 STK_ERROR("v4l_release called on removed device\n");
721 if (dev->owner != fp) {
722 usb_autopm_put_interface(dev->interface);
723 kref_put(&dev->kref, stk_camera_cleanup);
727 stk_stop_stream(dev);
729 stk_free_buffers(dev);
733 usb_autopm_put_interface(dev->interface);
734 kref_put(&dev->kref, stk_camera_cleanup);
739 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
740 size_t count, loff_t *f_pos)
745 struct stk_camera *dev;
746 struct video_device *vdev;
747 struct stk_sio_buffer *sbuf;
749 vdev = video_devdata(fp);
752 dev = vdev_to_camera(vdev);
757 if (!is_present(dev))
759 if (dev->owner && dev->owner != fp)
762 if (!is_streaming(dev)) {
763 if (stk_initialise(dev)
764 || stk_allocate_buffers(dev, 3)
765 || stk_start_stream(dev))
767 spin_lock_irqsave(&dev->spinlock, flags);
768 for (i = 0; i < dev->n_sbufs; i++) {
769 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
770 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
772 spin_unlock_irqrestore(&dev->spinlock, flags);
775 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
777 ret = wait_event_interruptible(dev->wait_frame,
778 !list_empty(&dev->sio_full) || !is_present(dev));
781 if (!is_present(dev))
784 if (count + *f_pos > dev->frame_size)
785 count = dev->frame_size - *f_pos;
786 spin_lock_irqsave(&dev->spinlock, flags);
787 if (list_empty(&dev->sio_full)) {
788 spin_unlock_irqrestore(&dev->spinlock, flags);
789 STK_ERROR("BUG: No siobufs ready\n");
792 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
793 spin_unlock_irqrestore(&dev->spinlock, flags);
795 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
800 if (*f_pos >= dev->frame_size) {
802 spin_lock_irqsave(&dev->spinlock, flags);
803 list_move_tail(&sbuf->list, &dev->sio_avail);
804 spin_unlock_irqrestore(&dev->spinlock, flags);
809 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
811 struct stk_camera *dev;
812 struct video_device *vdev;
814 vdev = video_devdata(fp);
819 dev = vdev_to_camera(vdev);
823 poll_wait(fp, &dev->wait_frame, wait);
825 if (!is_present(dev))
828 if (!list_empty(&dev->sio_full))
829 return (POLLIN | POLLRDNORM);
835 static void stk_v4l_vm_open(struct vm_area_struct *vma)
837 struct stk_sio_buffer *sbuf = vma->vm_private_data;
840 static void stk_v4l_vm_close(struct vm_area_struct *vma)
842 struct stk_sio_buffer *sbuf = vma->vm_private_data;
844 if (sbuf->mapcount == 0)
845 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
847 static struct vm_operations_struct stk_v4l_vm_ops = {
848 .open = stk_v4l_vm_open,
849 .close = stk_v4l_vm_close
852 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
856 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
857 struct stk_camera *dev;
858 struct video_device *vdev;
859 struct stk_sio_buffer *sbuf = NULL;
861 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
864 vdev = video_devdata(fp);
865 dev = vdev_to_camera(vdev);
867 for (i = 0; i < dev->n_sbufs; i++) {
868 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
869 sbuf = dev->sio_bufs + i;
875 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
878 vma->vm_flags |= VM_DONTEXPAND;
879 vma->vm_private_data = sbuf;
880 vma->vm_ops = &stk_v4l_vm_ops;
881 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
882 stk_v4l_vm_open(vma);
886 /* v4l ioctl handlers */
888 static int stk_vidioc_querycap(struct file *filp,
889 void *priv, struct v4l2_capability *cap)
891 strcpy(cap->driver, "stk");
892 strcpy(cap->card, "stk");
893 cap->version = DRIVER_VERSION_NUM;
895 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
896 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
900 static int stk_vidioc_enum_input(struct file *filp,
901 void *priv, struct v4l2_input *input)
903 if (input->index != 0)
906 strcpy(input->name, "Syntek USB Camera");
907 input->type = V4L2_INPUT_TYPE_CAMERA;
912 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
918 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
927 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
932 /* List of all V4Lv2 controls supported by the driver */
933 static struct v4l2_queryctrl stk_controls[] = {
935 .id = V4L2_CID_BRIGHTNESS,
936 .type = V4L2_CTRL_TYPE_INTEGER,
937 .name = "Brightness",
941 .default_value = 0x6000,
943 /*TODO: get more controls to work */
946 static int stk_vidioc_queryctrl(struct file *filp,
947 void *priv, struct v4l2_queryctrl *c)
951 nbr = ARRAY_SIZE(stk_controls);
953 for (i = 0; i < nbr; i++) {
954 if (stk_controls[i].id == c->id) {
955 memcpy(c, &stk_controls[i],
956 sizeof(struct v4l2_queryctrl));
963 static int stk_vidioc_g_ctrl(struct file *filp,
964 void *priv, struct v4l2_control *c)
966 struct stk_camera *dev = priv;
968 case V4L2_CID_BRIGHTNESS:
969 c->value = dev->vsettings.brightness;
977 static int stk_vidioc_s_ctrl(struct file *filp,
978 void *priv, struct v4l2_control *c)
980 struct stk_camera *dev = priv;
982 case V4L2_CID_BRIGHTNESS:
983 dev->vsettings.brightness = c->value;
984 return stk_sensor_set_brightness(dev, c->value >> 8);
992 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
993 void *priv, struct v4l2_fmtdesc *fmtd)
997 switch (fmtd->index) {
999 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
1000 strcpy(fmtd->description, "r5g6b5");
1003 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
1004 strcpy(fmtd->description, "r5g6b5BE");
1007 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
1008 strcpy(fmtd->description, "yuv4:2:2");
1011 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
1012 strcpy(fmtd->description, "Raw bayer");
1015 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1016 strcpy(fmtd->description, "yuv4:2:2");
1024 static struct stk_size {
1029 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1030 { .w = 640, .h = 480, .m = MODE_VGA, },
1031 { .w = 352, .h = 288, .m = MODE_CIF, },
1032 { .w = 320, .h = 240, .m = MODE_QVGA, },
1033 { .w = 176, .h = 144, .m = MODE_QCIF, },
1036 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
1037 void *priv, struct v4l2_format *f)
1039 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1040 struct stk_camera *dev = priv;
1043 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1044 && stk_sizes[i].m != dev->vsettings.mode;
1046 if (i == ARRAY_SIZE(stk_sizes)) {
1047 STK_ERROR("ERROR: mode invalid\n");
1050 pix_format->width = stk_sizes[i].w;
1051 pix_format->height = stk_sizes[i].h;
1052 pix_format->field = V4L2_FIELD_NONE;
1053 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1054 pix_format->priv = 0;
1055 pix_format->pixelformat = dev->vsettings.palette;
1056 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1057 pix_format->bytesperline = pix_format->width;
1059 pix_format->bytesperline = 2 * pix_format->width;
1060 pix_format->sizeimage = pix_format->bytesperline
1061 * pix_format->height;
1065 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
1066 void *priv, struct v4l2_format *fmtd)
1069 switch (fmtd->fmt.pix.pixelformat) {
1070 case V4L2_PIX_FMT_RGB565:
1071 case V4L2_PIX_FMT_RGB565X:
1072 case V4L2_PIX_FMT_UYVY:
1073 case V4L2_PIX_FMT_YUYV:
1074 case V4L2_PIX_FMT_SBGGR8:
1079 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1080 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1083 if (i == ARRAY_SIZE(stk_sizes)
1084 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1085 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1086 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1087 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1088 fmtd->fmt.pix.priv = i - 1;
1090 fmtd->fmt.pix.height = stk_sizes[i].h;
1091 fmtd->fmt.pix.width = stk_sizes[i].w;
1092 fmtd->fmt.pix.priv = i;
1095 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1096 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1097 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1098 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1100 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1101 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1102 * fmtd->fmt.pix.height;
1106 static int stk_setup_format(struct stk_camera *dev)
1110 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1114 while (stk_sizes[i].m != dev->vsettings.mode
1115 && i < ARRAY_SIZE(stk_sizes))
1117 if (i == ARRAY_SIZE(stk_sizes)) {
1118 STK_ERROR("Something is broken in %s\n", __func__);
1121 /* This registers controls some timings, not sure of what. */
1122 stk_camera_write_reg(dev, 0x001b, 0x0e);
1123 if (dev->vsettings.mode == MODE_SXGA)
1124 stk_camera_write_reg(dev, 0x001c, 0x0e);
1126 stk_camera_write_reg(dev, 0x001c, 0x46);
1128 * Registers 0x0115 0x0114 are the size of each line (bytes),
1129 * regs 0x0117 0x0116 are the heigth of the image.
1131 stk_camera_write_reg(dev, 0x0115,
1132 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1133 stk_camera_write_reg(dev, 0x0114,
1134 (stk_sizes[i].w * depth) & 0xff);
1135 stk_camera_write_reg(dev, 0x0117,
1136 (stk_sizes[i].h >> 8) & 0xff);
1137 stk_camera_write_reg(dev, 0x0116,
1138 stk_sizes[i].h & 0xff);
1139 return stk_sensor_configure(dev);
1142 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1143 void *priv, struct v4l2_format *fmtd)
1146 struct stk_camera *dev = priv;
1150 if (!is_present(dev))
1152 if (is_streaming(dev))
1154 if (dev->owner && dev->owner != filp)
1156 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1161 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1162 stk_free_buffers(dev);
1163 dev->frame_size = fmtd->fmt.pix.sizeimage;
1164 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1166 stk_initialise(dev);
1167 return stk_setup_format(dev);
1170 static int stk_vidioc_reqbufs(struct file *filp,
1171 void *priv, struct v4l2_requestbuffers *rb)
1173 struct stk_camera *dev = priv;
1177 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1179 if (rb->memory != V4L2_MEMORY_MMAP)
1181 if (is_streaming(dev)
1182 || (dev->owner && dev->owner != filp))
1186 /*FIXME If they ask for zero, we must stop streaming and free */
1189 /* Arbitrary limit */
1190 else if (rb->count > 5)
1193 stk_allocate_buffers(dev, rb->count);
1194 rb->count = dev->n_sbufs;
1198 static int stk_vidioc_querybuf(struct file *filp,
1199 void *priv, struct v4l2_buffer *buf)
1202 struct stk_camera *dev = priv;
1203 struct stk_sio_buffer *sbuf;
1205 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1210 if (index < 0 || index >= dev->n_sbufs)
1212 sbuf = dev->sio_bufs + buf->index;
1213 *buf = sbuf->v4lbuf;
1217 static int stk_vidioc_qbuf(struct file *filp,
1218 void *priv, struct v4l2_buffer *buf)
1220 struct stk_camera *dev = priv;
1221 struct stk_sio_buffer *sbuf;
1222 unsigned long flags;
1223 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1226 if (buf->memory != V4L2_MEMORY_MMAP)
1229 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1231 sbuf = dev->sio_bufs + buf->index;
1232 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1234 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1235 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1236 spin_lock_irqsave(&dev->spinlock, flags);
1237 list_add_tail(&sbuf->list, &dev->sio_avail);
1238 *buf = sbuf->v4lbuf;
1239 spin_unlock_irqrestore(&dev->spinlock, flags);
1243 static int stk_vidioc_dqbuf(struct file *filp,
1244 void *priv, struct v4l2_buffer *buf)
1246 struct stk_camera *dev = priv;
1247 struct stk_sio_buffer *sbuf;
1248 unsigned long flags;
1251 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1252 || !is_streaming(dev))
1255 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1256 return -EWOULDBLOCK;
1257 ret = wait_event_interruptible(dev->wait_frame,
1258 !list_empty(&dev->sio_full) || !is_present(dev));
1261 if (!is_present(dev))
1264 spin_lock_irqsave(&dev->spinlock, flags);
1265 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1266 list_del_init(&sbuf->list);
1267 spin_unlock_irqrestore(&dev->spinlock, flags);
1268 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1269 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1270 sbuf->v4lbuf.sequence = ++dev->sequence;
1271 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1273 *buf = sbuf->v4lbuf;
1277 static int stk_vidioc_streamon(struct file *filp,
1278 void *priv, enum v4l2_buf_type type)
1280 struct stk_camera *dev = priv;
1281 if (is_streaming(dev))
1283 if (dev->sio_bufs == NULL)
1286 return stk_start_stream(dev);
1289 static int stk_vidioc_streamoff(struct file *filp,
1290 void *priv, enum v4l2_buf_type type)
1292 struct stk_camera *dev = priv;
1293 unsigned long flags;
1295 stk_stop_stream(dev);
1296 spin_lock_irqsave(&dev->spinlock, flags);
1297 INIT_LIST_HEAD(&dev->sio_avail);
1298 INIT_LIST_HEAD(&dev->sio_full);
1299 for (i = 0; i < dev->n_sbufs; i++) {
1300 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1301 dev->sio_bufs[i].v4lbuf.flags = 0;
1303 spin_unlock_irqrestore(&dev->spinlock, flags);
1308 static int stk_vidioc_g_parm(struct file *filp,
1309 void *priv, struct v4l2_streamparm *sp)
1311 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1314 sp->parm.capture.capability = 0;
1315 sp->parm.capture.capturemode = 0;
1316 /*FIXME This is not correct */
1317 sp->parm.capture.timeperframe.numerator = 1;
1318 sp->parm.capture.timeperframe.denominator = 30;
1319 sp->parm.capture.readbuffers = 2;
1320 sp->parm.capture.extendedmode = 0;
1324 static struct file_operations v4l_stk_fops = {
1325 .owner = THIS_MODULE,
1326 .open = v4l_stk_open,
1327 .release = v4l_stk_release,
1328 .read = v4l_stk_read,
1329 .poll = v4l_stk_poll,
1330 .mmap = v4l_stk_mmap,
1331 .ioctl = video_ioctl2,
1332 #ifdef CONFIG_COMPAT
1333 .compat_ioctl = v4l_compat_ioctl32,
1338 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1339 .vidioc_querycap = stk_vidioc_querycap,
1340 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1341 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1342 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1343 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1344 .vidioc_enum_input = stk_vidioc_enum_input,
1345 .vidioc_s_input = stk_vidioc_s_input,
1346 .vidioc_g_input = stk_vidioc_g_input,
1347 .vidioc_s_std = stk_vidioc_s_std,
1348 .vidioc_reqbufs = stk_vidioc_reqbufs,
1349 .vidioc_querybuf = stk_vidioc_querybuf,
1350 .vidioc_qbuf = stk_vidioc_qbuf,
1351 .vidioc_dqbuf = stk_vidioc_dqbuf,
1352 .vidioc_streamon = stk_vidioc_streamon,
1353 .vidioc_streamoff = stk_vidioc_streamoff,
1354 .vidioc_queryctrl = stk_vidioc_queryctrl,
1355 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1356 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1357 .vidioc_g_parm = stk_vidioc_g_parm,
1360 static void stk_v4l_dev_release(struct video_device *vd)
1364 static struct video_device stk_v4l_data = {
1365 .name = "stkwebcam",
1367 .tvnorms = V4L2_STD_UNKNOWN,
1368 .current_norm = V4L2_STD_UNKNOWN,
1369 .fops = &v4l_stk_fops,
1370 .ioctl_ops = &v4l_stk_ioctl_ops,
1371 .release = stk_v4l_dev_release,
1375 static int stk_register_video_device(struct stk_camera *dev)
1379 dev->vdev = stk_v4l_data;
1380 dev->vdev.debug = debug;
1381 dev->vdev.parent = &dev->interface->dev;
1382 video_set_drvdata(&dev->vdev, dev);
1383 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1385 STK_ERROR("v4l registration failed\n");
1387 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1388 " /dev/video%d\n", dev->vdev.minor);
1395 static int stk_camera_probe(struct usb_interface *interface,
1396 const struct usb_device_id *id)
1401 struct stk_camera *dev = NULL;
1402 struct usb_device *udev = interface_to_usbdev(interface);
1403 struct usb_host_interface *iface_desc;
1404 struct usb_endpoint_descriptor *endpoint;
1406 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1408 STK_ERROR("Out of memory !\n");
1412 kref_init(&dev->kref);
1413 spin_lock_init(&dev->spinlock);
1414 init_waitqueue_head(&dev->wait_frame);
1417 dev->interface = interface;
1418 usb_get_intf(interface);
1420 dev->vsettings.vflip = vflip;
1421 dev->vsettings.hflip = hflip;
1425 /* Set up the endpoint information
1426 * use only the first isoc-in endpoint
1427 * for the current alternate setting */
1428 iface_desc = interface->cur_altsetting;
1430 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1431 endpoint = &iface_desc->endpoint[i].desc;
1434 && ((endpoint->bEndpointAddress
1435 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1436 && ((endpoint->bmAttributes
1437 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1438 /* we found an isoc in endpoint */
1439 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1443 if (!dev->isoc_ep) {
1444 STK_ERROR("Could not find isoc-in endpoint");
1445 kref_put(&dev->kref, stk_camera_cleanup);
1448 dev->vsettings.brightness = 0x7fff;
1449 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1450 dev->vsettings.mode = MODE_VGA;
1451 dev->frame_size = 640 * 480 * 2;
1453 INIT_LIST_HEAD(&dev->sio_avail);
1454 INIT_LIST_HEAD(&dev->sio_full);
1456 usb_set_intfdata(interface, dev);
1458 err = stk_register_video_device(dev);
1460 kref_put(&dev->kref, stk_camera_cleanup);
1464 stk_create_sysfs_files(&dev->vdev);
1465 usb_autopm_enable(dev->interface);
1470 static void stk_camera_disconnect(struct usb_interface *interface)
1472 struct stk_camera *dev = usb_get_intfdata(interface);
1474 usb_set_intfdata(interface, NULL);
1477 wake_up_interruptible(&dev->wait_frame);
1478 stk_remove_sysfs_files(&dev->vdev);
1480 kref_put(&dev->kref, stk_camera_cleanup);
1484 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1486 struct stk_camera *dev = usb_get_intfdata(intf);
1487 if (is_streaming(dev)) {
1488 stk_stop_stream(dev);
1489 /* yes, this is ugly */
1495 static int stk_camera_resume(struct usb_interface *intf)
1497 struct stk_camera *dev = usb_get_intfdata(intf);
1498 if (!is_initialised(dev))
1500 unset_initialised(dev);
1501 stk_initialise(dev);
1502 stk_setup_format(dev);
1503 if (is_streaming(dev))
1504 stk_start_stream(dev);
1509 static struct usb_driver stk_camera_driver = {
1510 .name = "stkwebcam",
1511 .probe = stk_camera_probe,
1512 .disconnect = stk_camera_disconnect,
1513 .id_table = stkwebcam_table,
1515 .suspend = stk_camera_suspend,
1516 .resume = stk_camera_resume,
1521 static int __init stk_camera_init(void)
1525 result = usb_register(&stk_camera_driver);
1527 STK_ERROR("usb_register failed ! Error number %d\n", result);
1533 static void __exit stk_camera_exit(void)
1535 usb_deregister(&stk_camera_driver);
1538 module_init(stk_camera_init);
1539 module_exit(stk_camera_exit);