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);
71 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
73 struct usb_device *udev = dev->udev;
76 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
78 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
90 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
92 struct usb_device *udev = dev->udev;
95 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
97 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
109 static int stk_start_stream(struct stk_camera *dev)
113 int value_116, value_117;
115 if (!is_present(dev))
117 if (!is_memallocd(dev) || !is_initialised(dev)) {
118 STK_ERROR("FIXME: Buffers are not allocated\n");
121 ret = usb_set_interface(dev->udev, 0, 5);
124 STK_ERROR("usb_set_interface failed !\n");
125 if (stk_sensor_wakeup(dev))
126 STK_ERROR("error awaking the sensor\n");
128 stk_camera_read_reg(dev, 0x0116, &value_116);
129 stk_camera_read_reg(dev, 0x0117, &value_117);
131 stk_camera_write_reg(dev, 0x0116, 0x0000);
132 stk_camera_write_reg(dev, 0x0117, 0x0000);
134 stk_camera_read_reg(dev, 0x0100, &value);
135 stk_camera_write_reg(dev, 0x0100, value | 0x80);
137 stk_camera_write_reg(dev, 0x0116, value_116);
138 stk_camera_write_reg(dev, 0x0117, value_117);
139 for (i = 0; i < MAX_ISO_BUFS; i++) {
140 if (dev->isobufs[i].urb) {
141 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
142 atomic_inc(&dev->urbs_used);
151 static int stk_stop_stream(struct stk_camera *dev)
155 if (is_present(dev)) {
156 stk_camera_read_reg(dev, 0x0100, &value);
157 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
158 if (dev->isobufs != NULL) {
159 for (i = 0; i < MAX_ISO_BUFS; i++) {
160 if (dev->isobufs[i].urb)
161 usb_kill_urb(dev->isobufs[i].urb);
164 unset_streaming(dev);
166 if (usb_set_interface(dev->udev, 0, 0))
167 STK_ERROR("usb_set_interface failed !\n");
168 if (stk_sensor_sleep(dev))
169 STK_ERROR("error suspending the sensor\n");
175 * This seems to be the shortest init sequence we
176 * must do in order to find the sensor
177 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
178 * is also reset. Maybe powers down it?
179 * Rest of values don't make a difference
182 static struct regval stk1125_initvals[] = {
183 /*TODO: What means this sequence? */
212 static int stk_initialise(struct stk_camera *dev)
216 if (!is_present(dev))
218 if (is_initialised(dev))
220 rv = stk1125_initvals;
221 while (rv->reg != 0xffff) {
222 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
227 if (stk_sensor_init(dev) == 0) {
228 set_initialised(dev);
234 #ifdef CONFIG_VIDEO_V4L1_COMPAT
236 /* sysfs functions */
237 /*FIXME cleanup this */
239 static ssize_t show_brightness(struct device *class,
240 struct device_attribute *attr, char *buf)
242 struct video_device *vdev = to_video_device(class);
243 struct stk_camera *dev = vdev_to_camera(vdev);
245 return sprintf(buf, "%X\n", dev->vsettings.brightness);
248 static ssize_t store_brightness(struct device *class,
249 struct device_attribute *attr, const char *buf, size_t count)
255 struct video_device *vdev = to_video_device(class);
256 struct stk_camera *dev = vdev_to_camera(vdev);
258 value = simple_strtoul(buf, &endp, 16);
260 dev->vsettings.brightness = (int) value;
262 ret = stk_sensor_set_brightness(dev, value >> 8);
269 static ssize_t show_hflip(struct device *class,
270 struct device_attribute *attr, char *buf)
272 struct video_device *vdev = to_video_device(class);
273 struct stk_camera *dev = vdev_to_camera(vdev);
275 return sprintf(buf, "%d\n", dev->vsettings.hflip);
278 static ssize_t store_hflip(struct device *class,
279 struct device_attribute *attr, const char *buf, size_t count)
281 struct video_device *vdev = to_video_device(class);
282 struct stk_camera *dev = vdev_to_camera(vdev);
284 if (strncmp(buf, "1", 1) == 0)
285 dev->vsettings.hflip = 1;
286 else if (strncmp(buf, "0", 1) == 0)
287 dev->vsettings.hflip = 0;
294 static ssize_t show_vflip(struct device *class,
295 struct device_attribute *attr, char *buf)
297 struct video_device *vdev = to_video_device(class);
298 struct stk_camera *dev = vdev_to_camera(vdev);
300 return sprintf(buf, "%d\n", dev->vsettings.vflip);
303 static ssize_t store_vflip(struct device *class,
304 struct device_attribute *attr, const char *buf, size_t count)
306 struct video_device *vdev = to_video_device(class);
307 struct stk_camera *dev = vdev_to_camera(vdev);
309 if (strncmp(buf, "1", 1) == 0)
310 dev->vsettings.vflip = 1;
311 else if (strncmp(buf, "0", 1) == 0)
312 dev->vsettings.vflip = 0;
319 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO,
320 show_brightness, store_brightness);
321 static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip);
322 static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip);
324 static int stk_create_sysfs_files(struct video_device *vdev)
328 ret = device_create_file(&vdev->dev, &dev_attr_brightness);
329 ret += device_create_file(&vdev->dev, &dev_attr_hflip);
330 ret += device_create_file(&vdev->dev, &dev_attr_vflip);
332 STK_WARNING("Could not create sysfs files\n");
336 static void stk_remove_sysfs_files(struct video_device *vdev)
338 device_remove_file(&vdev->dev, &dev_attr_brightness);
339 device_remove_file(&vdev->dev, &dev_attr_hflip);
340 device_remove_file(&vdev->dev, &dev_attr_vflip);
344 #define stk_create_sysfs_files(a)
345 #define stk_remove_sysfs_files(a)
348 /* *********************************************** */
350 * This function is called as an URB transfert is complete (Isochronous pipe).
351 * So, the traitement is done in interrupt time, so it has be fast, not crash,
352 * and not stall. Neat.
354 static void stk_isoc_handler(struct urb *urb)
361 unsigned char *fill = NULL;
362 unsigned char *iso_buf = NULL;
364 struct stk_camera *dev;
365 struct stk_sio_buffer *fb;
367 dev = (struct stk_camera *) urb->context;
370 STK_ERROR("isoc_handler called with NULL device !\n");
374 if (urb->status == -ENOENT || urb->status == -ECONNRESET
375 || urb->status == -ESHUTDOWN) {
376 atomic_dec(&dev->urbs_used);
380 spin_lock_irqsave(&dev->spinlock, flags);
382 if (urb->status != -EINPROGRESS && urb->status != 0) {
383 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
387 if (list_empty(&dev->sio_avail)) {
388 /*FIXME Stop streaming after a while */
389 (void) (printk_ratelimit() &&
390 STK_ERROR("isoc_handler without available buffer!\n"));
393 fb = list_first_entry(&dev->sio_avail,
394 struct stk_sio_buffer, list);
395 fill = fb->buffer + fb->v4lbuf.bytesused;
397 for (i = 0; i < urb->number_of_packets; i++) {
398 if (urb->iso_frame_desc[i].status != 0) {
399 if (urb->iso_frame_desc[i].status != -EXDEV)
400 STK_ERROR("Frame %d has error %d\n", i,
401 urb->iso_frame_desc[i].status);
404 framelen = urb->iso_frame_desc[i].actual_length;
405 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
408 continue; /* no data */
411 * we found something informational from there
412 * the isoc frames have to type of headers
413 * type1: 00 xx 00 00 or 20 xx 00 00
414 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
415 * xx is a sequencer which has never been seen over 0x3f
416 * imho data written down looks like bayer, i see similarities
417 * after every 640 bytes
419 if (*iso_buf & 0x80) {
422 /* This marks a new frame */
423 if (fb->v4lbuf.bytesused != 0
424 && fb->v4lbuf.bytesused != dev->frame_size) {
425 (void) (printk_ratelimit() &&
426 STK_ERROR("frame %d, "
427 "bytesused=%d, skipping\n",
428 i, fb->v4lbuf.bytesused));
429 fb->v4lbuf.bytesused = 0;
431 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
432 if (list_is_singular(&dev->sio_avail)) {
433 /* Always reuse the last buffer */
434 fb->v4lbuf.bytesused = 0;
437 list_move_tail(dev->sio_avail.next,
439 wake_up(&dev->wait_frame);
440 fb = list_first_entry(&dev->sio_avail,
441 struct stk_sio_buffer, list);
442 fb->v4lbuf.bytesused = 0;
451 /* Our buffer is full !!! */
452 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
453 (void) (printk_ratelimit() &&
454 STK_ERROR("Frame buffer overflow, lost sync\n"));
455 /*FIXME Do something here? */
458 spin_unlock_irqrestore(&dev->spinlock, flags);
459 memcpy(fill, iso_buf, framelen);
460 spin_lock_irqsave(&dev->spinlock, flags);
463 /* New size of our buffer */
464 fb->v4lbuf.bytesused += framelen;
468 spin_unlock_irqrestore(&dev->spinlock, flags);
469 urb->dev = dev->udev;
470 ret = usb_submit_urb(urb, GFP_ATOMIC);
472 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
477 /* -------------------------------------------- */
479 static int stk_prepare_iso(struct stk_camera *dev)
484 struct usb_device *udev;
491 STK_ERROR("isobufs already allocated. Bad\n");
493 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
495 if (dev->isobufs == NULL) {
496 STK_ERROR("Unable to allocate iso buffers\n");
499 for (i = 0; i < MAX_ISO_BUFS; i++) {
500 if (dev->isobufs[i].data == NULL) {
501 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
503 STK_ERROR("Failed to allocate iso buffer %d\n",
507 dev->isobufs[i].data = kbuf;
509 STK_ERROR("isobuf data already allocated\n");
510 if (dev->isobufs[i].urb == NULL) {
511 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
513 STK_ERROR("Failed to allocate URB %d\n", i);
516 dev->isobufs[i].urb = urb;
518 STK_ERROR("Killing URB\n");
519 usb_kill_urb(dev->isobufs[i].urb);
520 urb = dev->isobufs[i].urb;
524 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
525 urb->transfer_flags = URB_ISO_ASAP;
526 urb->transfer_buffer = dev->isobufs[i].data;
527 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
528 urb->complete = stk_isoc_handler;
530 urb->start_frame = 0;
531 urb->number_of_packets = ISO_FRAMES_PER_DESC;
533 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
534 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
535 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
542 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
543 kfree(dev->isobufs[i].data);
544 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
545 usb_free_urb(dev->isobufs[i].urb);
551 static void stk_clean_iso(struct stk_camera *dev)
555 if (dev == NULL || dev->isobufs == NULL)
558 for (i = 0; i < MAX_ISO_BUFS; i++) {
561 urb = dev->isobufs[i].urb;
563 if (atomic_read(&dev->urbs_used))
567 kfree(dev->isobufs[i].data);
571 unset_memallocd(dev);
574 static int stk_setup_siobuf(struct stk_camera *dev, int index)
576 struct stk_sio_buffer *buf = dev->sio_bufs + index;
577 INIT_LIST_HEAD(&buf->list);
578 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
579 buf->buffer = vmalloc_user(buf->v4lbuf.length);
580 if (buf->buffer == NULL)
584 buf->v4lbuf.index = index;
585 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
586 buf->v4lbuf.field = V4L2_FIELD_NONE;
587 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
588 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
592 static int stk_free_sio_buffers(struct stk_camera *dev)
597 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
600 * If any buffers are mapped, we cannot free them at all.
602 for (i = 0; i < dev->n_sbufs; i++) {
603 if (dev->sio_bufs[i].mapcount > 0)
609 spin_lock_irqsave(&dev->spinlock, flags);
610 INIT_LIST_HEAD(&dev->sio_avail);
611 INIT_LIST_HEAD(&dev->sio_full);
612 nbufs = dev->n_sbufs;
614 spin_unlock_irqrestore(&dev->spinlock, flags);
615 for (i = 0; i < nbufs; i++) {
616 if (dev->sio_bufs[i].buffer != NULL)
617 vfree(dev->sio_bufs[i].buffer);
619 kfree(dev->sio_bufs);
620 dev->sio_bufs = NULL;
624 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
627 if (dev->sio_bufs != NULL)
628 STK_ERROR("sio_bufs already allocated\n");
630 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
632 if (dev->sio_bufs == NULL)
634 for (i = 0; i < n_sbufs; i++) {
635 if (stk_setup_siobuf(dev, i))
636 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
643 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
646 err = stk_prepare_iso(dev);
651 err = stk_prepare_sio_buffers(dev, n_sbufs);
653 stk_free_sio_buffers(dev);
659 static void stk_free_buffers(struct stk_camera *dev)
662 stk_free_sio_buffers(dev);
664 /* -------------------------------------------- */
666 /* v4l file operations */
668 static int v4l_stk_open(struct inode *inode, struct file *fp)
670 struct stk_camera *dev;
671 struct video_device *vdev;
673 vdev = video_devdata(fp);
674 dev = vdev_to_camera(vdev);
677 if (dev == NULL || !is_present(dev)) {
681 fp->private_data = dev;
682 usb_autopm_get_interface(dev->interface);
688 static int v4l_stk_release(struct inode *inode, struct file *fp)
690 struct stk_camera *dev = fp->private_data;
692 if (dev->owner != fp) {
693 usb_autopm_put_interface(dev->interface);
697 stk_stop_stream(dev);
699 stk_free_buffers(dev);
703 usb_autopm_put_interface(dev->interface);
708 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
709 size_t count, loff_t *f_pos)
714 struct stk_sio_buffer *sbuf;
715 struct stk_camera *dev = fp->private_data;
720 if (!is_present(dev))
722 if (dev->owner && dev->owner != fp)
725 if (!is_streaming(dev)) {
726 if (stk_initialise(dev)
727 || stk_allocate_buffers(dev, 3)
728 || stk_start_stream(dev))
730 spin_lock_irqsave(&dev->spinlock, flags);
731 for (i = 0; i < dev->n_sbufs; i++) {
732 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
733 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
735 spin_unlock_irqrestore(&dev->spinlock, flags);
738 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
740 ret = wait_event_interruptible(dev->wait_frame,
741 !list_empty(&dev->sio_full) || !is_present(dev));
744 if (!is_present(dev))
747 if (count + *f_pos > dev->frame_size)
748 count = dev->frame_size - *f_pos;
749 spin_lock_irqsave(&dev->spinlock, flags);
750 if (list_empty(&dev->sio_full)) {
751 spin_unlock_irqrestore(&dev->spinlock, flags);
752 STK_ERROR("BUG: No siobufs ready\n");
755 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
756 spin_unlock_irqrestore(&dev->spinlock, flags);
758 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
763 if (*f_pos >= dev->frame_size) {
765 spin_lock_irqsave(&dev->spinlock, flags);
766 list_move_tail(&sbuf->list, &dev->sio_avail);
767 spin_unlock_irqrestore(&dev->spinlock, flags);
772 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
774 struct stk_camera *dev = fp->private_data;
779 poll_wait(fp, &dev->wait_frame, wait);
781 if (!is_present(dev))
784 if (!list_empty(&dev->sio_full))
785 return (POLLIN | POLLRDNORM);
791 static void stk_v4l_vm_open(struct vm_area_struct *vma)
793 struct stk_sio_buffer *sbuf = vma->vm_private_data;
796 static void stk_v4l_vm_close(struct vm_area_struct *vma)
798 struct stk_sio_buffer *sbuf = vma->vm_private_data;
800 if (sbuf->mapcount == 0)
801 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
803 static struct vm_operations_struct stk_v4l_vm_ops = {
804 .open = stk_v4l_vm_open,
805 .close = stk_v4l_vm_close
808 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
812 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
813 struct stk_camera *dev = fp->private_data;
814 struct stk_sio_buffer *sbuf = NULL;
816 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
819 for (i = 0; i < dev->n_sbufs; i++) {
820 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
821 sbuf = dev->sio_bufs + i;
827 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
830 vma->vm_flags |= VM_DONTEXPAND;
831 vma->vm_private_data = sbuf;
832 vma->vm_ops = &stk_v4l_vm_ops;
833 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
834 stk_v4l_vm_open(vma);
838 /* v4l ioctl handlers */
840 static int stk_vidioc_querycap(struct file *filp,
841 void *priv, struct v4l2_capability *cap)
843 strcpy(cap->driver, "stk");
844 strcpy(cap->card, "stk");
845 cap->version = DRIVER_VERSION_NUM;
847 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
848 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
852 static int stk_vidioc_enum_input(struct file *filp,
853 void *priv, struct v4l2_input *input)
855 if (input->index != 0)
858 strcpy(input->name, "Syntek USB Camera");
859 input->type = V4L2_INPUT_TYPE_CAMERA;
864 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
870 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
879 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
884 /* List of all V4Lv2 controls supported by the driver */
885 static struct v4l2_queryctrl stk_controls[] = {
887 .id = V4L2_CID_BRIGHTNESS,
888 .type = V4L2_CTRL_TYPE_INTEGER,
889 .name = "Brightness",
893 .default_value = 0x6000,
895 /*TODO: get more controls to work */
898 static int stk_vidioc_queryctrl(struct file *filp,
899 void *priv, struct v4l2_queryctrl *c)
903 nbr = ARRAY_SIZE(stk_controls);
905 for (i = 0; i < nbr; i++) {
906 if (stk_controls[i].id == c->id) {
907 memcpy(c, &stk_controls[i],
908 sizeof(struct v4l2_queryctrl));
915 static int stk_vidioc_g_ctrl(struct file *filp,
916 void *priv, struct v4l2_control *c)
918 struct stk_camera *dev = priv;
920 case V4L2_CID_BRIGHTNESS:
921 c->value = dev->vsettings.brightness;
929 static int stk_vidioc_s_ctrl(struct file *filp,
930 void *priv, struct v4l2_control *c)
932 struct stk_camera *dev = priv;
934 case V4L2_CID_BRIGHTNESS:
935 dev->vsettings.brightness = c->value;
936 return stk_sensor_set_brightness(dev, c->value >> 8);
944 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
945 void *priv, struct v4l2_fmtdesc *fmtd)
949 switch (fmtd->index) {
951 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
952 strcpy(fmtd->description, "r5g6b5");
955 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
956 strcpy(fmtd->description, "r5g6b5BE");
959 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
960 strcpy(fmtd->description, "yuv4:2:2");
963 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
964 strcpy(fmtd->description, "Raw bayer");
967 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
968 strcpy(fmtd->description, "yuv4:2:2");
976 static struct stk_size {
981 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
982 { .w = 640, .h = 480, .m = MODE_VGA, },
983 { .w = 352, .h = 288, .m = MODE_CIF, },
984 { .w = 320, .h = 240, .m = MODE_QVGA, },
985 { .w = 176, .h = 144, .m = MODE_QCIF, },
988 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
989 void *priv, struct v4l2_format *f)
991 struct v4l2_pix_format *pix_format = &f->fmt.pix;
992 struct stk_camera *dev = priv;
995 for (i = 0; i < ARRAY_SIZE(stk_sizes)
996 && stk_sizes[i].m != dev->vsettings.mode;
998 if (i == ARRAY_SIZE(stk_sizes)) {
999 STK_ERROR("ERROR: mode invalid\n");
1002 pix_format->width = stk_sizes[i].w;
1003 pix_format->height = stk_sizes[i].h;
1004 pix_format->field = V4L2_FIELD_NONE;
1005 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1006 pix_format->priv = 0;
1007 pix_format->pixelformat = dev->vsettings.palette;
1008 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1009 pix_format->bytesperline = pix_format->width;
1011 pix_format->bytesperline = 2 * pix_format->width;
1012 pix_format->sizeimage = pix_format->bytesperline
1013 * pix_format->height;
1017 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
1018 void *priv, struct v4l2_format *fmtd)
1021 switch (fmtd->fmt.pix.pixelformat) {
1022 case V4L2_PIX_FMT_RGB565:
1023 case V4L2_PIX_FMT_RGB565X:
1024 case V4L2_PIX_FMT_UYVY:
1025 case V4L2_PIX_FMT_YUYV:
1026 case V4L2_PIX_FMT_SBGGR8:
1031 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1032 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1035 if (i == ARRAY_SIZE(stk_sizes)
1036 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1037 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1038 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1039 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1040 fmtd->fmt.pix.priv = i - 1;
1042 fmtd->fmt.pix.height = stk_sizes[i].h;
1043 fmtd->fmt.pix.width = stk_sizes[i].w;
1044 fmtd->fmt.pix.priv = i;
1047 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1048 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1049 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1050 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1052 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1053 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1054 * fmtd->fmt.pix.height;
1058 static int stk_setup_format(struct stk_camera *dev)
1062 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1066 while (stk_sizes[i].m != dev->vsettings.mode
1067 && i < ARRAY_SIZE(stk_sizes))
1069 if (i == ARRAY_SIZE(stk_sizes)) {
1070 STK_ERROR("Something is broken in %s\n", __func__);
1073 /* This registers controls some timings, not sure of what. */
1074 stk_camera_write_reg(dev, 0x001b, 0x0e);
1075 if (dev->vsettings.mode == MODE_SXGA)
1076 stk_camera_write_reg(dev, 0x001c, 0x0e);
1078 stk_camera_write_reg(dev, 0x001c, 0x46);
1080 * Registers 0x0115 0x0114 are the size of each line (bytes),
1081 * regs 0x0117 0x0116 are the heigth of the image.
1083 stk_camera_write_reg(dev, 0x0115,
1084 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1085 stk_camera_write_reg(dev, 0x0114,
1086 (stk_sizes[i].w * depth) & 0xff);
1087 stk_camera_write_reg(dev, 0x0117,
1088 (stk_sizes[i].h >> 8) & 0xff);
1089 stk_camera_write_reg(dev, 0x0116,
1090 stk_sizes[i].h & 0xff);
1091 return stk_sensor_configure(dev);
1094 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1095 void *priv, struct v4l2_format *fmtd)
1098 struct stk_camera *dev = priv;
1102 if (!is_present(dev))
1104 if (is_streaming(dev))
1106 if (dev->owner && dev->owner != filp)
1108 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1113 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1114 stk_free_buffers(dev);
1115 dev->frame_size = fmtd->fmt.pix.sizeimage;
1116 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1118 stk_initialise(dev);
1119 return stk_setup_format(dev);
1122 static int stk_vidioc_reqbufs(struct file *filp,
1123 void *priv, struct v4l2_requestbuffers *rb)
1125 struct stk_camera *dev = priv;
1129 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1131 if (rb->memory != V4L2_MEMORY_MMAP)
1133 if (is_streaming(dev)
1134 || (dev->owner && dev->owner != filp))
1138 /*FIXME If they ask for zero, we must stop streaming and free */
1141 /* Arbitrary limit */
1142 else if (rb->count > 5)
1145 stk_allocate_buffers(dev, rb->count);
1146 rb->count = dev->n_sbufs;
1150 static int stk_vidioc_querybuf(struct file *filp,
1151 void *priv, struct v4l2_buffer *buf)
1154 struct stk_camera *dev = priv;
1155 struct stk_sio_buffer *sbuf;
1157 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1162 if (index < 0 || index >= dev->n_sbufs)
1164 sbuf = dev->sio_bufs + buf->index;
1165 *buf = sbuf->v4lbuf;
1169 static int stk_vidioc_qbuf(struct file *filp,
1170 void *priv, struct v4l2_buffer *buf)
1172 struct stk_camera *dev = priv;
1173 struct stk_sio_buffer *sbuf;
1174 unsigned long flags;
1175 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1178 if (buf->memory != V4L2_MEMORY_MMAP)
1181 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1183 sbuf = dev->sio_bufs + buf->index;
1184 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1186 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1187 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1188 spin_lock_irqsave(&dev->spinlock, flags);
1189 list_add_tail(&sbuf->list, &dev->sio_avail);
1190 *buf = sbuf->v4lbuf;
1191 spin_unlock_irqrestore(&dev->spinlock, flags);
1195 static int stk_vidioc_dqbuf(struct file *filp,
1196 void *priv, struct v4l2_buffer *buf)
1198 struct stk_camera *dev = priv;
1199 struct stk_sio_buffer *sbuf;
1200 unsigned long flags;
1203 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1204 || !is_streaming(dev))
1207 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1208 return -EWOULDBLOCK;
1209 ret = wait_event_interruptible(dev->wait_frame,
1210 !list_empty(&dev->sio_full) || !is_present(dev));
1213 if (!is_present(dev))
1216 spin_lock_irqsave(&dev->spinlock, flags);
1217 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1218 list_del_init(&sbuf->list);
1219 spin_unlock_irqrestore(&dev->spinlock, flags);
1220 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1221 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1222 sbuf->v4lbuf.sequence = ++dev->sequence;
1223 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1225 *buf = sbuf->v4lbuf;
1229 static int stk_vidioc_streamon(struct file *filp,
1230 void *priv, enum v4l2_buf_type type)
1232 struct stk_camera *dev = priv;
1233 if (is_streaming(dev))
1235 if (dev->sio_bufs == NULL)
1238 return stk_start_stream(dev);
1241 static int stk_vidioc_streamoff(struct file *filp,
1242 void *priv, enum v4l2_buf_type type)
1244 struct stk_camera *dev = priv;
1245 unsigned long flags;
1247 stk_stop_stream(dev);
1248 spin_lock_irqsave(&dev->spinlock, flags);
1249 INIT_LIST_HEAD(&dev->sio_avail);
1250 INIT_LIST_HEAD(&dev->sio_full);
1251 for (i = 0; i < dev->n_sbufs; i++) {
1252 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1253 dev->sio_bufs[i].v4lbuf.flags = 0;
1255 spin_unlock_irqrestore(&dev->spinlock, flags);
1260 static int stk_vidioc_g_parm(struct file *filp,
1261 void *priv, struct v4l2_streamparm *sp)
1263 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1266 sp->parm.capture.capability = 0;
1267 sp->parm.capture.capturemode = 0;
1268 /*FIXME This is not correct */
1269 sp->parm.capture.timeperframe.numerator = 1;
1270 sp->parm.capture.timeperframe.denominator = 30;
1271 sp->parm.capture.readbuffers = 2;
1272 sp->parm.capture.extendedmode = 0;
1276 static struct file_operations v4l_stk_fops = {
1277 .owner = THIS_MODULE,
1278 .open = v4l_stk_open,
1279 .release = v4l_stk_release,
1280 .read = v4l_stk_read,
1281 .poll = v4l_stk_poll,
1282 .mmap = v4l_stk_mmap,
1283 .ioctl = video_ioctl2,
1284 #ifdef CONFIG_COMPAT
1285 .compat_ioctl = v4l_compat_ioctl32,
1290 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1291 .vidioc_querycap = stk_vidioc_querycap,
1292 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1293 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1294 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1295 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1296 .vidioc_enum_input = stk_vidioc_enum_input,
1297 .vidioc_s_input = stk_vidioc_s_input,
1298 .vidioc_g_input = stk_vidioc_g_input,
1299 .vidioc_s_std = stk_vidioc_s_std,
1300 .vidioc_reqbufs = stk_vidioc_reqbufs,
1301 .vidioc_querybuf = stk_vidioc_querybuf,
1302 .vidioc_qbuf = stk_vidioc_qbuf,
1303 .vidioc_dqbuf = stk_vidioc_dqbuf,
1304 .vidioc_streamon = stk_vidioc_streamon,
1305 .vidioc_streamoff = stk_vidioc_streamoff,
1306 .vidioc_queryctrl = stk_vidioc_queryctrl,
1307 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1308 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1309 .vidioc_g_parm = stk_vidioc_g_parm,
1312 static void stk_v4l_dev_release(struct video_device *vd)
1314 struct stk_camera *dev = vdev_to_camera(vd);
1316 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1317 STK_ERROR("We are leaking memory\n");
1318 usb_put_intf(dev->interface);
1322 static struct video_device stk_v4l_data = {
1323 .name = "stkwebcam",
1325 .tvnorms = V4L2_STD_UNKNOWN,
1326 .current_norm = V4L2_STD_UNKNOWN,
1327 .fops = &v4l_stk_fops,
1328 .ioctl_ops = &v4l_stk_ioctl_ops,
1329 .release = stk_v4l_dev_release,
1333 static int stk_register_video_device(struct stk_camera *dev)
1337 dev->vdev = stk_v4l_data;
1338 dev->vdev.debug = debug;
1339 dev->vdev.parent = &dev->interface->dev;
1340 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1342 STK_ERROR("v4l registration failed\n");
1344 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1345 " /dev/video%d\n", dev->vdev.minor);
1352 static int stk_camera_probe(struct usb_interface *interface,
1353 const struct usb_device_id *id)
1358 struct stk_camera *dev = NULL;
1359 struct usb_device *udev = interface_to_usbdev(interface);
1360 struct usb_host_interface *iface_desc;
1361 struct usb_endpoint_descriptor *endpoint;
1363 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1365 STK_ERROR("Out of memory !\n");
1369 spin_lock_init(&dev->spinlock);
1370 init_waitqueue_head(&dev->wait_frame);
1373 dev->interface = interface;
1374 usb_get_intf(interface);
1376 dev->vsettings.vflip = vflip;
1377 dev->vsettings.hflip = hflip;
1381 /* Set up the endpoint information
1382 * use only the first isoc-in endpoint
1383 * for the current alternate setting */
1384 iface_desc = interface->cur_altsetting;
1386 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1387 endpoint = &iface_desc->endpoint[i].desc;
1390 && ((endpoint->bEndpointAddress
1391 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1392 && ((endpoint->bmAttributes
1393 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1394 /* we found an isoc in endpoint */
1395 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1399 if (!dev->isoc_ep) {
1400 STK_ERROR("Could not find isoc-in endpoint");
1404 dev->vsettings.brightness = 0x7fff;
1405 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1406 dev->vsettings.mode = MODE_VGA;
1407 dev->frame_size = 640 * 480 * 2;
1409 INIT_LIST_HEAD(&dev->sio_avail);
1410 INIT_LIST_HEAD(&dev->sio_full);
1412 usb_set_intfdata(interface, dev);
1414 err = stk_register_video_device(dev);
1419 stk_create_sysfs_files(&dev->vdev);
1420 usb_autopm_enable(dev->interface);
1429 static void stk_camera_disconnect(struct usb_interface *interface)
1431 struct stk_camera *dev = usb_get_intfdata(interface);
1433 usb_set_intfdata(interface, NULL);
1436 wake_up_interruptible(&dev->wait_frame);
1437 stk_remove_sysfs_files(&dev->vdev);
1439 STK_INFO("Syntek USB2.0 Camera release resources"
1440 "video device /dev/video%d\n", dev->vdev.minor);
1442 video_unregister_device(&dev->vdev);
1446 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1448 struct stk_camera *dev = usb_get_intfdata(intf);
1449 if (is_streaming(dev)) {
1450 stk_stop_stream(dev);
1451 /* yes, this is ugly */
1457 static int stk_camera_resume(struct usb_interface *intf)
1459 struct stk_camera *dev = usb_get_intfdata(intf);
1460 if (!is_initialised(dev))
1462 unset_initialised(dev);
1463 stk_initialise(dev);
1464 stk_setup_format(dev);
1465 if (is_streaming(dev))
1466 stk_start_stream(dev);
1471 static struct usb_driver stk_camera_driver = {
1472 .name = "stkwebcam",
1473 .probe = stk_camera_probe,
1474 .disconnect = stk_camera_disconnect,
1475 .id_table = stkwebcam_table,
1477 .suspend = stk_camera_suspend,
1478 .resume = stk_camera_resume,
1483 static int __init stk_camera_init(void)
1487 result = usb_register(&stk_camera_driver);
1489 STK_ERROR("usb_register failed ! Error number %d\n", result);
1495 static void __exit stk_camera_exit(void)
1497 usb_deregister(&stk_camera_driver);
1500 module_init(stk_camera_init);
1501 module_exit(stk_camera_exit);