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 = video_device_create_file(vdev, &dev_attr_brightness);
345 ret += video_device_create_file(vdev, &dev_attr_hflip);
346 ret += video_device_create_file(vdev, &dev_attr_vflip);
350 static void stk_remove_sysfs_files(struct video_device *vdev)
352 video_device_remove_file(vdev, &dev_attr_brightness);
353 video_device_remove_file(vdev, &dev_attr_hflip);
354 video_device_remove_file(vdev, &dev_attr_vflip);
358 #define stk_create_sysfs_files(a)
359 #define stk_remove_sysfs_files(a)
362 /* *********************************************** */
364 * This function is called as an URB transfert is complete (Isochronous pipe).
365 * So, the traitement is done in interrupt time, so it has be fast, not crash,
366 * and not stall. Neat.
368 static void stk_isoc_handler(struct urb *urb)
375 unsigned char *fill = NULL;
376 unsigned char *iso_buf = NULL;
378 struct stk_camera *dev;
379 struct stk_sio_buffer *fb;
381 dev = (struct stk_camera *) urb->context;
384 STK_ERROR("isoc_handler called with NULL device !\n");
388 if (urb->status == -ENOENT || urb->status == -ECONNRESET
389 || urb->status == -ESHUTDOWN) {
390 atomic_dec(&dev->urbs_used);
394 spin_lock_irqsave(&dev->spinlock, flags);
396 if (urb->status != -EINPROGRESS && urb->status != 0) {
397 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
401 if (list_empty(&dev->sio_avail)) {
402 /*FIXME Stop streaming after a while */
403 (void) (printk_ratelimit() &&
404 STK_ERROR("isoc_handler without available buffer!\n"));
407 fb = list_first_entry(&dev->sio_avail,
408 struct stk_sio_buffer, list);
409 fill = fb->buffer + fb->v4lbuf.bytesused;
411 for (i = 0; i < urb->number_of_packets; i++) {
412 if (urb->iso_frame_desc[i].status != 0) {
413 if (urb->iso_frame_desc[i].status != -EXDEV)
414 STK_ERROR("Frame %d has error %d\n", i,
415 urb->iso_frame_desc[i].status);
418 framelen = urb->iso_frame_desc[i].actual_length;
419 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
422 continue; /* no data */
425 * we found something informational from there
426 * the isoc frames have to type of headers
427 * type1: 00 xx 00 00 or 20 xx 00 00
428 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
429 * xx is a sequencer which has never been seen over 0x3f
430 * imho data written down looks like bayer, i see similarities
431 * after every 640 bytes
433 if (*iso_buf & 0x80) {
436 /* This marks a new frame */
437 if (fb->v4lbuf.bytesused != 0
438 && fb->v4lbuf.bytesused != dev->frame_size) {
439 (void) (printk_ratelimit() &&
440 STK_ERROR("frame %d, "
441 "bytesused=%d, skipping\n",
442 i, fb->v4lbuf.bytesused));
443 fb->v4lbuf.bytesused = 0;
445 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
446 list_move_tail(dev->sio_avail.next,
448 wake_up(&dev->wait_frame);
449 if (list_empty(&dev->sio_avail)) {
450 (void) (printk_ratelimit() &&
451 STK_ERROR("No buffer available\n"));
454 fb = list_first_entry(&dev->sio_avail,
455 struct stk_sio_buffer, list);
456 fb->v4lbuf.bytesused = 0;
464 /* Our buffer is full !!! */
465 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
466 (void) (printk_ratelimit() &&
467 STK_ERROR("Frame buffer overflow, lost sync\n"));
468 /*FIXME Do something here? */
471 spin_unlock_irqrestore(&dev->spinlock, flags);
472 memcpy(fill, iso_buf, framelen);
473 spin_lock_irqsave(&dev->spinlock, flags);
476 /* New size of our buffer */
477 fb->v4lbuf.bytesused += framelen;
481 spin_unlock_irqrestore(&dev->spinlock, flags);
482 urb->dev = dev->udev;
483 ret = usb_submit_urb(urb, GFP_ATOMIC);
485 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
490 /* -------------------------------------------- */
492 static int stk_prepare_iso(struct stk_camera *dev)
497 struct usb_device *udev;
504 STK_ERROR("isobufs already allocated. Bad\n");
506 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
508 if (dev->isobufs == NULL) {
509 STK_ERROR("Unable to allocate iso buffers\n");
512 for (i = 0; i < MAX_ISO_BUFS; i++) {
513 if (dev->isobufs[i].data == NULL) {
514 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
516 STK_ERROR("Failed to allocate iso buffer %d\n",
520 dev->isobufs[i].data = kbuf;
522 STK_ERROR("isobuf data already allocated\n");
523 if (dev->isobufs[i].urb == NULL) {
524 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
526 STK_ERROR("Failed to allocate URB %d\n", i);
529 dev->isobufs[i].urb = urb;
531 STK_ERROR("Killing URB\n");
532 usb_kill_urb(dev->isobufs[i].urb);
533 urb = dev->isobufs[i].urb;
537 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
538 urb->transfer_flags = URB_ISO_ASAP;
539 urb->transfer_buffer = dev->isobufs[i].data;
540 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
541 urb->complete = stk_isoc_handler;
543 urb->start_frame = 0;
544 urb->number_of_packets = ISO_FRAMES_PER_DESC;
546 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
547 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
548 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
555 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
556 kfree(dev->isobufs[i].data);
557 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
558 usb_free_urb(dev->isobufs[i].urb);
564 static void stk_clean_iso(struct stk_camera *dev)
568 if (dev == NULL || dev->isobufs == NULL)
571 for (i = 0; i < MAX_ISO_BUFS; i++) {
574 urb = dev->isobufs[i].urb;
576 if (atomic_read(&dev->urbs_used))
580 kfree(dev->isobufs[i].data);
584 unset_memallocd(dev);
587 static int stk_setup_siobuf(struct stk_camera *dev, int index)
589 struct stk_sio_buffer *buf = dev->sio_bufs + index;
590 INIT_LIST_HEAD(&buf->list);
591 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
592 buf->buffer = vmalloc_user(buf->v4lbuf.length);
593 if (buf->buffer == NULL)
597 buf->v4lbuf.index = index;
598 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
599 buf->v4lbuf.field = V4L2_FIELD_NONE;
600 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
601 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
605 static int stk_free_sio_buffers(struct stk_camera *dev)
610 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
613 * If any buffers are mapped, we cannot free them at all.
615 for (i = 0; i < dev->n_sbufs; i++) {
616 if (dev->sio_bufs[i].mapcount > 0)
622 spin_lock_irqsave(&dev->spinlock, flags);
623 INIT_LIST_HEAD(&dev->sio_avail);
624 INIT_LIST_HEAD(&dev->sio_full);
625 nbufs = dev->n_sbufs;
627 spin_unlock_irqrestore(&dev->spinlock, flags);
628 for (i = 0; i < nbufs; i++) {
629 if (dev->sio_bufs[i].buffer != NULL)
630 vfree(dev->sio_bufs[i].buffer);
632 kfree(dev->sio_bufs);
633 dev->sio_bufs = NULL;
637 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
640 if (dev->sio_bufs != NULL)
641 STK_ERROR("sio_bufs already allocated\n");
643 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
645 if (dev->sio_bufs == NULL)
647 for (i = 0; i < n_sbufs; i++) {
648 if (stk_setup_siobuf(dev, i))
649 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
656 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
659 err = stk_prepare_iso(dev);
664 err = stk_prepare_sio_buffers(dev, n_sbufs);
666 stk_free_sio_buffers(dev);
672 static void stk_free_buffers(struct stk_camera *dev)
675 stk_free_sio_buffers(dev);
677 /* -------------------------------------------- */
679 /* v4l file operations */
681 static int v4l_stk_open(struct inode *inode, struct file *fp)
683 struct stk_camera *dev;
684 struct video_device *vdev;
686 vdev = video_devdata(fp);
687 dev = vdev_to_camera(vdev);
689 if (dev == NULL || !is_present(dev))
691 fp->private_data = vdev;
692 kref_get(&dev->kref);
693 usb_autopm_get_interface(dev->interface);
698 static int v4l_stk_release(struct inode *inode, struct file *fp)
700 struct stk_camera *dev;
701 struct video_device *vdev;
703 vdev = video_devdata(fp);
705 STK_ERROR("v4l_release called w/o video devdata\n");
708 dev = vdev_to_camera(vdev);
710 STK_ERROR("v4l_release called on removed device\n");
714 if (dev->owner != fp) {
715 usb_autopm_put_interface(dev->interface);
716 kref_put(&dev->kref, stk_camera_cleanup);
720 stk_stop_stream(dev);
722 stk_free_buffers(dev);
726 usb_autopm_put_interface(dev->interface);
727 kref_put(&dev->kref, stk_camera_cleanup);
732 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
733 size_t count, loff_t *f_pos)
738 struct stk_camera *dev;
739 struct video_device *vdev;
740 struct stk_sio_buffer *sbuf;
742 vdev = video_devdata(fp);
745 dev = vdev_to_camera(vdev);
750 if (!is_present(dev))
752 if (dev->owner && dev->owner != fp)
755 if (!is_streaming(dev)) {
756 if (stk_initialise(dev)
757 || stk_allocate_buffers(dev, 3)
758 || stk_start_stream(dev))
760 spin_lock_irqsave(&dev->spinlock, flags);
761 for (i = 0; i < dev->n_sbufs; i++) {
762 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
763 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
765 spin_unlock_irqrestore(&dev->spinlock, flags);
768 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
770 ret = wait_event_interruptible(dev->wait_frame,
771 !list_empty(&dev->sio_full) || !is_present(dev));
774 if (!is_present(dev))
777 if (count + *f_pos > dev->frame_size)
778 count = dev->frame_size - *f_pos;
779 spin_lock_irqsave(&dev->spinlock, flags);
780 if (list_empty(&dev->sio_full)) {
781 spin_unlock_irqrestore(&dev->spinlock, flags);
782 STK_ERROR("BUG: No siobufs ready\n");
785 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
786 spin_unlock_irqrestore(&dev->spinlock, flags);
788 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
793 if (*f_pos >= dev->frame_size) {
795 spin_lock_irqsave(&dev->spinlock, flags);
796 list_move_tail(&sbuf->list, &dev->sio_avail);
797 spin_unlock_irqrestore(&dev->spinlock, flags);
802 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
804 struct stk_camera *dev;
805 struct video_device *vdev;
807 vdev = video_devdata(fp);
812 dev = vdev_to_camera(vdev);
816 poll_wait(fp, &dev->wait_frame, wait);
818 if (!is_present(dev))
821 if (!list_empty(&dev->sio_full))
822 return (POLLIN | POLLRDNORM);
828 static void stk_v4l_vm_open(struct vm_area_struct *vma)
830 struct stk_sio_buffer *sbuf = vma->vm_private_data;
833 static void stk_v4l_vm_close(struct vm_area_struct *vma)
835 struct stk_sio_buffer *sbuf = vma->vm_private_data;
837 if (sbuf->mapcount == 0)
838 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
840 static struct vm_operations_struct stk_v4l_vm_ops = {
841 .open = stk_v4l_vm_open,
842 .close = stk_v4l_vm_close
845 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
849 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
850 struct stk_camera *dev;
851 struct video_device *vdev;
852 struct stk_sio_buffer *sbuf = NULL;
854 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
857 vdev = video_devdata(fp);
858 dev = vdev_to_camera(vdev);
860 for (i = 0; i < dev->n_sbufs; i++) {
861 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
862 sbuf = dev->sio_bufs + i;
868 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
871 vma->vm_flags |= VM_DONTEXPAND;
872 vma->vm_private_data = sbuf;
873 vma->vm_ops = &stk_v4l_vm_ops;
874 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
875 stk_v4l_vm_open(vma);
879 /* v4l ioctl handlers */
881 static int stk_vidioc_querycap(struct file *filp,
882 void *priv, struct v4l2_capability *cap)
884 strcpy(cap->driver, "stk");
885 strcpy(cap->card, "stk");
886 cap->version = DRIVER_VERSION_NUM;
888 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
889 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
893 static int stk_vidioc_enum_input(struct file *filp,
894 void *priv, struct v4l2_input *input)
896 if (input->index != 0)
899 strcpy(input->name, "Syntek USB Camera");
900 input->type = V4L2_INPUT_TYPE_CAMERA;
905 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
911 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
920 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
925 /* List of all V4Lv2 controls supported by the driver */
926 static struct v4l2_queryctrl stk_controls[] = {
928 .id = V4L2_CID_BRIGHTNESS,
929 .type = V4L2_CTRL_TYPE_INTEGER,
930 .name = "Brightness",
934 .default_value = 0x6000,
936 /*TODO: get more controls to work */
939 static int stk_vidioc_queryctrl(struct file *filp,
940 void *priv, struct v4l2_queryctrl *c)
944 nbr = ARRAY_SIZE(stk_controls);
946 for (i = 0; i < nbr; i++) {
947 if (stk_controls[i].id == c->id) {
948 memcpy(c, &stk_controls[i],
949 sizeof(struct v4l2_queryctrl));
956 static int stk_vidioc_g_ctrl(struct file *filp,
957 void *priv, struct v4l2_control *c)
959 struct stk_camera *dev = priv;
961 case V4L2_CID_BRIGHTNESS:
962 c->value = dev->vsettings.brightness;
970 static int stk_vidioc_s_ctrl(struct file *filp,
971 void *priv, struct v4l2_control *c)
973 struct stk_camera *dev = priv;
975 case V4L2_CID_BRIGHTNESS:
976 dev->vsettings.brightness = c->value;
977 return stk_sensor_set_brightness(dev, c->value >> 8);
985 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
986 void *priv, struct v4l2_fmtdesc *fmtd)
990 switch (fmtd->index) {
992 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
993 strcpy(fmtd->description, "r5g6b5");
996 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
997 strcpy(fmtd->description, "r5g6b5BE");
1000 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
1001 strcpy(fmtd->description, "yuv4:2:2");
1004 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
1005 strcpy(fmtd->description, "Raw bayer");
1008 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1009 strcpy(fmtd->description, "yuv4:2:2");
1017 static struct stk_size {
1022 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1023 { .w = 640, .h = 480, .m = MODE_VGA, },
1024 { .w = 352, .h = 288, .m = MODE_CIF, },
1025 { .w = 320, .h = 240, .m = MODE_QVGA, },
1026 { .w = 176, .h = 144, .m = MODE_QCIF, },
1029 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
1030 void *priv, struct v4l2_format *f)
1032 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1033 struct stk_camera *dev = priv;
1036 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1037 && stk_sizes[i].m != dev->vsettings.mode;
1039 if (i == ARRAY_SIZE(stk_sizes)) {
1040 STK_ERROR("ERROR: mode invalid\n");
1043 pix_format->width = stk_sizes[i].w;
1044 pix_format->height = stk_sizes[i].h;
1045 pix_format->field = V4L2_FIELD_NONE;
1046 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1047 pix_format->priv = 0;
1048 pix_format->pixelformat = dev->vsettings.palette;
1049 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1050 pix_format->bytesperline = pix_format->width;
1052 pix_format->bytesperline = 2 * pix_format->width;
1053 pix_format->sizeimage = pix_format->bytesperline
1054 * pix_format->height;
1058 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
1059 void *priv, struct v4l2_format *fmtd)
1062 switch (fmtd->fmt.pix.pixelformat) {
1063 case V4L2_PIX_FMT_RGB565:
1064 case V4L2_PIX_FMT_RGB565X:
1065 case V4L2_PIX_FMT_UYVY:
1066 case V4L2_PIX_FMT_YUYV:
1067 case V4L2_PIX_FMT_SBGGR8:
1072 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1073 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1076 if (i == ARRAY_SIZE(stk_sizes)
1077 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1078 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1079 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1080 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1081 fmtd->fmt.pix.priv = i - 1;
1083 fmtd->fmt.pix.height = stk_sizes[i].h;
1084 fmtd->fmt.pix.width = stk_sizes[i].w;
1085 fmtd->fmt.pix.priv = i;
1088 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1089 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1090 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1091 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1093 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1094 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1095 * fmtd->fmt.pix.height;
1099 static int stk_setup_format(struct stk_camera *dev)
1103 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1107 while (stk_sizes[i].m != dev->vsettings.mode
1108 && i < ARRAY_SIZE(stk_sizes))
1110 if (i == ARRAY_SIZE(stk_sizes)) {
1111 STK_ERROR("Something is broken in %s\n", __func__);
1114 /* This registers controls some timings, not sure of what. */
1115 stk_camera_write_reg(dev, 0x001b, 0x0e);
1116 if (dev->vsettings.mode == MODE_SXGA)
1117 stk_camera_write_reg(dev, 0x001c, 0x0e);
1119 stk_camera_write_reg(dev, 0x001c, 0x46);
1121 * Registers 0x0115 0x0114 are the size of each line (bytes),
1122 * regs 0x0117 0x0116 are the heigth of the image.
1124 stk_camera_write_reg(dev, 0x0115,
1125 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1126 stk_camera_write_reg(dev, 0x0114,
1127 (stk_sizes[i].w * depth) & 0xff);
1128 stk_camera_write_reg(dev, 0x0117,
1129 (stk_sizes[i].h >> 8) & 0xff);
1130 stk_camera_write_reg(dev, 0x0116,
1131 stk_sizes[i].h & 0xff);
1132 return stk_sensor_configure(dev);
1135 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1136 void *priv, struct v4l2_format *fmtd)
1139 struct stk_camera *dev = priv;
1143 if (!is_present(dev))
1145 if (is_streaming(dev))
1147 if (dev->owner && dev->owner != filp)
1149 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1154 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1155 stk_free_buffers(dev);
1156 dev->frame_size = fmtd->fmt.pix.sizeimage;
1157 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1159 stk_initialise(dev);
1160 return stk_setup_format(dev);
1163 static int stk_vidioc_reqbufs(struct file *filp,
1164 void *priv, struct v4l2_requestbuffers *rb)
1166 struct stk_camera *dev = priv;
1170 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1172 if (rb->memory != V4L2_MEMORY_MMAP)
1174 if (is_streaming(dev)
1175 || (dev->owner && dev->owner != filp))
1179 /*FIXME If they ask for zero, we must stop streaming and free */
1182 /* Arbitrary limit */
1183 else if (rb->count > 5)
1186 stk_allocate_buffers(dev, rb->count);
1187 rb->count = dev->n_sbufs;
1191 static int stk_vidioc_querybuf(struct file *filp,
1192 void *priv, struct v4l2_buffer *buf)
1195 struct stk_camera *dev = priv;
1196 struct stk_sio_buffer *sbuf;
1198 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1203 if (index < 0 || index >= dev->n_sbufs)
1205 sbuf = dev->sio_bufs + buf->index;
1206 *buf = sbuf->v4lbuf;
1210 static int stk_vidioc_qbuf(struct file *filp,
1211 void *priv, struct v4l2_buffer *buf)
1213 struct stk_camera *dev = priv;
1214 struct stk_sio_buffer *sbuf;
1215 unsigned long flags;
1216 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1219 if (buf->memory != V4L2_MEMORY_MMAP)
1222 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1224 sbuf = dev->sio_bufs + buf->index;
1225 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1227 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1228 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1229 spin_lock_irqsave(&dev->spinlock, flags);
1230 list_add_tail(&sbuf->list, &dev->sio_avail);
1231 *buf = sbuf->v4lbuf;
1232 spin_unlock_irqrestore(&dev->spinlock, flags);
1236 static int stk_vidioc_dqbuf(struct file *filp,
1237 void *priv, struct v4l2_buffer *buf)
1239 struct stk_camera *dev = priv;
1240 struct stk_sio_buffer *sbuf;
1241 unsigned long flags;
1244 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1245 || !is_streaming(dev))
1248 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1249 return -EWOULDBLOCK;
1250 ret = wait_event_interruptible(dev->wait_frame,
1251 !list_empty(&dev->sio_full) || !is_present(dev));
1254 if (!is_present(dev))
1257 spin_lock_irqsave(&dev->spinlock, flags);
1258 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1259 list_del_init(&sbuf->list);
1260 spin_unlock_irqrestore(&dev->spinlock, flags);
1261 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1262 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1263 sbuf->v4lbuf.sequence = ++dev->sequence;
1264 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1266 *buf = sbuf->v4lbuf;
1270 static int stk_vidioc_streamon(struct file *filp,
1271 void *priv, enum v4l2_buf_type type)
1273 struct stk_camera *dev = priv;
1274 if (is_streaming(dev))
1276 if (dev->sio_bufs == NULL)
1279 return stk_start_stream(dev);
1282 static int stk_vidioc_streamoff(struct file *filp,
1283 void *priv, enum v4l2_buf_type type)
1285 struct stk_camera *dev = priv;
1286 unsigned long flags;
1288 stk_stop_stream(dev);
1289 spin_lock_irqsave(&dev->spinlock, flags);
1290 INIT_LIST_HEAD(&dev->sio_avail);
1291 INIT_LIST_HEAD(&dev->sio_full);
1292 for (i = 0; i < dev->n_sbufs; i++) {
1293 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1294 dev->sio_bufs[i].v4lbuf.flags = 0;
1296 spin_unlock_irqrestore(&dev->spinlock, flags);
1301 static int stk_vidioc_g_parm(struct file *filp,
1302 void *priv, struct v4l2_streamparm *sp)
1304 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1307 sp->parm.capture.capability = 0;
1308 sp->parm.capture.capturemode = 0;
1309 /*FIXME This is not correct */
1310 sp->parm.capture.timeperframe.numerator = 1;
1311 sp->parm.capture.timeperframe.denominator = 30;
1312 sp->parm.capture.readbuffers = 2;
1313 sp->parm.capture.extendedmode = 0;
1317 static struct file_operations v4l_stk_fops = {
1318 .owner = THIS_MODULE,
1319 .open = v4l_stk_open,
1320 .release = v4l_stk_release,
1321 .read = v4l_stk_read,
1322 .poll = v4l_stk_poll,
1323 .mmap = v4l_stk_mmap,
1324 .ioctl = video_ioctl2,
1325 #ifdef CONFIG_COMPAT
1326 .compat_ioctl = v4l_compat_ioctl32,
1331 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1332 .vidioc_querycap = stk_vidioc_querycap,
1333 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1334 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1335 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1336 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1337 .vidioc_enum_input = stk_vidioc_enum_input,
1338 .vidioc_s_input = stk_vidioc_s_input,
1339 .vidioc_g_input = stk_vidioc_g_input,
1340 .vidioc_s_std = stk_vidioc_s_std,
1341 .vidioc_reqbufs = stk_vidioc_reqbufs,
1342 .vidioc_querybuf = stk_vidioc_querybuf,
1343 .vidioc_qbuf = stk_vidioc_qbuf,
1344 .vidioc_dqbuf = stk_vidioc_dqbuf,
1345 .vidioc_streamon = stk_vidioc_streamon,
1346 .vidioc_streamoff = stk_vidioc_streamoff,
1347 .vidioc_queryctrl = stk_vidioc_queryctrl,
1348 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1349 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1350 .vidioc_g_parm = stk_vidioc_g_parm,
1353 static void stk_v4l_dev_release(struct video_device *vd)
1357 static struct video_device stk_v4l_data = {
1358 .name = "stkwebcam",
1359 .type = VFL_TYPE_GRABBER,
1360 .type2 = VID_TYPE_CAPTURE,
1362 .tvnorms = V4L2_STD_UNKNOWN,
1363 .current_norm = V4L2_STD_UNKNOWN,
1364 .fops = &v4l_stk_fops,
1365 .ioctl_ops = &v4l_stk_ioctl_ops,
1366 .release = stk_v4l_dev_release,
1370 static int stk_register_video_device(struct stk_camera *dev)
1374 dev->vdev = stk_v4l_data;
1375 dev->vdev.debug = debug;
1376 dev->vdev.parent = &dev->interface->dev;
1377 dev->vdev.priv = dev;
1378 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1380 STK_ERROR("v4l registration failed\n");
1382 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1383 " /dev/video%d\n", dev->vdev.minor);
1390 static int stk_camera_probe(struct usb_interface *interface,
1391 const struct usb_device_id *id)
1396 struct stk_camera *dev = NULL;
1397 struct usb_device *udev = interface_to_usbdev(interface);
1398 struct usb_host_interface *iface_desc;
1399 struct usb_endpoint_descriptor *endpoint;
1401 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1403 STK_ERROR("Out of memory !\n");
1407 kref_init(&dev->kref);
1408 spin_lock_init(&dev->spinlock);
1409 init_waitqueue_head(&dev->wait_frame);
1412 dev->interface = interface;
1413 usb_get_intf(interface);
1415 dev->vsettings.vflip = vflip;
1416 dev->vsettings.hflip = hflip;
1420 /* Set up the endpoint information
1421 * use only the first isoc-in endpoint
1422 * for the current alternate setting */
1423 iface_desc = interface->cur_altsetting;
1425 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1426 endpoint = &iface_desc->endpoint[i].desc;
1429 && ((endpoint->bEndpointAddress
1430 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1431 && ((endpoint->bmAttributes
1432 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1433 /* we found an isoc in endpoint */
1434 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1438 if (!dev->isoc_ep) {
1439 STK_ERROR("Could not find isoc-in endpoint");
1440 kref_put(&dev->kref, stk_camera_cleanup);
1443 dev->vsettings.brightness = 0x7fff;
1444 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1445 dev->vsettings.mode = MODE_VGA;
1446 dev->frame_size = 640 * 480 * 2;
1448 INIT_LIST_HEAD(&dev->sio_avail);
1449 INIT_LIST_HEAD(&dev->sio_full);
1451 usb_set_intfdata(interface, dev);
1453 err = stk_register_video_device(dev);
1455 kref_put(&dev->kref, stk_camera_cleanup);
1459 stk_create_sysfs_files(&dev->vdev);
1460 usb_autopm_enable(dev->interface);
1465 static void stk_camera_disconnect(struct usb_interface *interface)
1467 struct stk_camera *dev = usb_get_intfdata(interface);
1469 usb_set_intfdata(interface, NULL);
1472 wake_up_interruptible(&dev->wait_frame);
1473 stk_remove_sysfs_files(&dev->vdev);
1475 kref_put(&dev->kref, stk_camera_cleanup);
1479 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1481 struct stk_camera *dev = usb_get_intfdata(intf);
1482 if (is_streaming(dev)) {
1483 stk_stop_stream(dev);
1484 /* yes, this is ugly */
1490 static int stk_camera_resume(struct usb_interface *intf)
1492 struct stk_camera *dev = usb_get_intfdata(intf);
1493 if (!is_initialised(dev))
1495 unset_initialised(dev);
1496 stk_initialise(dev);
1497 stk_setup_format(dev);
1498 if (is_streaming(dev))
1499 stk_start_stream(dev);
1504 static struct usb_driver stk_camera_driver = {
1505 .name = "stkwebcam",
1506 .probe = stk_camera_probe,
1507 .disconnect = stk_camera_disconnect,
1508 .id_table = stkwebcam_table,
1510 .suspend = stk_camera_suspend,
1511 .resume = stk_camera_resume,
1516 static int __init stk_camera_init(void)
1520 result = usb_register(&stk_camera_driver);
1522 STK_ERROR("usb_register failed ! Error number %d\n", result);
1528 static void __exit stk_camera_exit(void)
1530 usb_deregister(&stk_camera_driver);
1533 module_init(stk_camera_init);
1534 module_exit(stk_camera_exit);