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>
31 #include <linux/usb.h>
33 #include <linux/vmalloc.h>
34 #include <linux/videodev2.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ioctl.h>
38 #include "stk-webcam.h"
42 module_param(hflip, bool, 0444);
43 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
46 module_param(vflip, bool, 0444);
47 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
50 module_param(debug, int, 0444);
51 MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
53 MODULE_LICENSE("GPL");
54 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
55 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
58 /* bool for webcam LED management */
61 /* Some cameras have audio interfaces, we aren't interested in those */
62 static struct usb_device_id stkwebcam_table[] = {
63 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
64 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
67 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
72 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
74 struct usb_device *udev = dev->udev;
77 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
79 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
91 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
93 struct usb_device *udev = dev->udev;
96 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
98 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
110 static int stk_start_stream(struct stk_camera *dev)
114 int value_116, value_117;
116 if (!is_present(dev))
118 if (!is_memallocd(dev) || !is_initialised(dev)) {
119 STK_ERROR("FIXME: Buffers are not allocated\n");
122 ret = usb_set_interface(dev->udev, 0, 5);
125 STK_ERROR("usb_set_interface failed !\n");
126 if (stk_sensor_wakeup(dev))
127 STK_ERROR("error awaking the sensor\n");
129 stk_camera_read_reg(dev, 0x0116, &value_116);
130 stk_camera_read_reg(dev, 0x0117, &value_117);
132 stk_camera_write_reg(dev, 0x0116, 0x0000);
133 stk_camera_write_reg(dev, 0x0117, 0x0000);
135 stk_camera_read_reg(dev, 0x0100, &value);
136 stk_camera_write_reg(dev, 0x0100, value | 0x80);
138 stk_camera_write_reg(dev, 0x0116, value_116);
139 stk_camera_write_reg(dev, 0x0117, value_117);
140 for (i = 0; i < MAX_ISO_BUFS; i++) {
141 if (dev->isobufs[i].urb) {
142 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
143 atomic_inc(&dev->urbs_used);
152 static int stk_stop_stream(struct stk_camera *dev)
156 if (is_present(dev)) {
157 stk_camera_read_reg(dev, 0x0100, &value);
158 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
159 if (dev->isobufs != NULL) {
160 for (i = 0; i < MAX_ISO_BUFS; i++) {
161 if (dev->isobufs[i].urb)
162 usb_kill_urb(dev->isobufs[i].urb);
165 unset_streaming(dev);
167 if (usb_set_interface(dev->udev, 0, 0))
168 STK_ERROR("usb_set_interface failed !\n");
169 if (stk_sensor_sleep(dev))
170 STK_ERROR("error suspending the sensor\n");
176 * This seems to be the shortest init sequence we
177 * must do in order to find the sensor
178 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
179 * is also reset. Maybe powers down it?
180 * Rest of values don't make a difference
183 static struct regval stk1125_initvals[] = {
184 /*TODO: What means this sequence? */
213 static int stk_initialise(struct stk_camera *dev)
217 if (!is_present(dev))
219 if (is_initialised(dev))
221 rv = stk1125_initvals;
222 while (rv->reg != 0xffff) {
223 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
228 if (stk_sensor_init(dev) == 0) {
229 set_initialised(dev);
235 /* *********************************************** */
237 * This function is called as an URB transfert is complete (Isochronous pipe).
238 * So, the traitement is done in interrupt time, so it has be fast, not crash,
239 * and not stall. Neat.
241 static void stk_isoc_handler(struct urb *urb)
248 unsigned char *fill = NULL;
249 unsigned char *iso_buf = NULL;
251 struct stk_camera *dev;
252 struct stk_sio_buffer *fb;
254 dev = (struct stk_camera *) urb->context;
257 STK_ERROR("isoc_handler called with NULL device !\n");
261 if (urb->status == -ENOENT || urb->status == -ECONNRESET
262 || urb->status == -ESHUTDOWN) {
263 atomic_dec(&dev->urbs_used);
267 spin_lock_irqsave(&dev->spinlock, flags);
269 if (urb->status != -EINPROGRESS && urb->status != 0) {
270 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
274 if (list_empty(&dev->sio_avail)) {
275 /*FIXME Stop streaming after a while */
276 (void) (printk_ratelimit() &&
277 STK_ERROR("isoc_handler without available buffer!\n"));
280 fb = list_first_entry(&dev->sio_avail,
281 struct stk_sio_buffer, list);
282 fill = fb->buffer + fb->v4lbuf.bytesused;
284 for (i = 0; i < urb->number_of_packets; i++) {
285 if (urb->iso_frame_desc[i].status != 0) {
286 if (urb->iso_frame_desc[i].status != -EXDEV)
287 STK_ERROR("Frame %d has error %d\n", i,
288 urb->iso_frame_desc[i].status);
291 framelen = urb->iso_frame_desc[i].actual_length;
292 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
295 continue; /* no data */
298 * we found something informational from there
299 * the isoc frames have to type of headers
300 * type1: 00 xx 00 00 or 20 xx 00 00
301 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
302 * xx is a sequencer which has never been seen over 0x3f
303 * imho data written down looks like bayer, i see similarities
304 * after every 640 bytes
306 if (*iso_buf & 0x80) {
309 /* This marks a new frame */
310 if (fb->v4lbuf.bytesused != 0
311 && fb->v4lbuf.bytesused != dev->frame_size) {
312 (void) (printk_ratelimit() &&
313 STK_ERROR("frame %d, "
314 "bytesused=%d, skipping\n",
315 i, fb->v4lbuf.bytesused));
316 fb->v4lbuf.bytesused = 0;
318 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
319 if (list_is_singular(&dev->sio_avail)) {
320 /* Always reuse the last buffer */
321 fb->v4lbuf.bytesused = 0;
324 list_move_tail(dev->sio_avail.next,
326 wake_up(&dev->wait_frame);
327 fb = list_first_entry(&dev->sio_avail,
328 struct stk_sio_buffer, list);
329 fb->v4lbuf.bytesused = 0;
338 /* Our buffer is full !!! */
339 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
340 (void) (printk_ratelimit() &&
341 STK_ERROR("Frame buffer overflow, lost sync\n"));
342 /*FIXME Do something here? */
345 spin_unlock_irqrestore(&dev->spinlock, flags);
346 memcpy(fill, iso_buf, framelen);
347 spin_lock_irqsave(&dev->spinlock, flags);
350 /* New size of our buffer */
351 fb->v4lbuf.bytesused += framelen;
355 spin_unlock_irqrestore(&dev->spinlock, flags);
356 urb->dev = dev->udev;
357 ret = usb_submit_urb(urb, GFP_ATOMIC);
359 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
364 /* -------------------------------------------- */
366 static int stk_prepare_iso(struct stk_camera *dev)
371 struct usb_device *udev;
378 STK_ERROR("isobufs already allocated. Bad\n");
380 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
382 if (dev->isobufs == NULL) {
383 STK_ERROR("Unable to allocate iso buffers\n");
386 for (i = 0; i < MAX_ISO_BUFS; i++) {
387 if (dev->isobufs[i].data == NULL) {
388 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
390 STK_ERROR("Failed to allocate iso buffer %d\n",
394 dev->isobufs[i].data = kbuf;
396 STK_ERROR("isobuf data already allocated\n");
397 if (dev->isobufs[i].urb == NULL) {
398 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
400 STK_ERROR("Failed to allocate URB %d\n", i);
403 dev->isobufs[i].urb = urb;
405 STK_ERROR("Killing URB\n");
406 usb_kill_urb(dev->isobufs[i].urb);
407 urb = dev->isobufs[i].urb;
411 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
412 urb->transfer_flags = URB_ISO_ASAP;
413 urb->transfer_buffer = dev->isobufs[i].data;
414 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
415 urb->complete = stk_isoc_handler;
417 urb->start_frame = 0;
418 urb->number_of_packets = ISO_FRAMES_PER_DESC;
420 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
421 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
422 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
429 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
430 kfree(dev->isobufs[i].data);
431 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
432 usb_free_urb(dev->isobufs[i].urb);
438 static void stk_clean_iso(struct stk_camera *dev)
442 if (dev == NULL || dev->isobufs == NULL)
445 for (i = 0; i < MAX_ISO_BUFS; i++) {
448 urb = dev->isobufs[i].urb;
450 if (atomic_read(&dev->urbs_used) && is_present(dev))
454 kfree(dev->isobufs[i].data);
458 unset_memallocd(dev);
461 static int stk_setup_siobuf(struct stk_camera *dev, int index)
463 struct stk_sio_buffer *buf = dev->sio_bufs + index;
464 INIT_LIST_HEAD(&buf->list);
465 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
466 buf->buffer = vmalloc_user(buf->v4lbuf.length);
467 if (buf->buffer == NULL)
471 buf->v4lbuf.index = index;
472 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
473 buf->v4lbuf.field = V4L2_FIELD_NONE;
474 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
475 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
479 static int stk_free_sio_buffers(struct stk_camera *dev)
484 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
487 * If any buffers are mapped, we cannot free them at all.
489 for (i = 0; i < dev->n_sbufs; i++) {
490 if (dev->sio_bufs[i].mapcount > 0)
496 spin_lock_irqsave(&dev->spinlock, flags);
497 INIT_LIST_HEAD(&dev->sio_avail);
498 INIT_LIST_HEAD(&dev->sio_full);
499 nbufs = dev->n_sbufs;
501 spin_unlock_irqrestore(&dev->spinlock, flags);
502 for (i = 0; i < nbufs; i++) {
503 if (dev->sio_bufs[i].buffer != NULL)
504 vfree(dev->sio_bufs[i].buffer);
506 kfree(dev->sio_bufs);
507 dev->sio_bufs = NULL;
511 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
514 if (dev->sio_bufs != NULL)
515 STK_ERROR("sio_bufs already allocated\n");
517 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
519 if (dev->sio_bufs == NULL)
521 for (i = 0; i < n_sbufs; i++) {
522 if (stk_setup_siobuf(dev, i))
523 return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
530 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
533 err = stk_prepare_iso(dev);
538 err = stk_prepare_sio_buffers(dev, n_sbufs);
540 stk_free_sio_buffers(dev);
546 static void stk_free_buffers(struct stk_camera *dev)
549 stk_free_sio_buffers(dev);
551 /* -------------------------------------------- */
553 /* v4l file operations */
555 static int v4l_stk_open(struct file *fp)
557 struct stk_camera *dev;
558 struct video_device *vdev;
560 vdev = video_devdata(fp);
561 dev = vdev_to_camera(vdev);
563 if (dev == NULL || !is_present(dev))
567 stk_camera_write_reg(dev, 0x0, 0x24);
571 fp->private_data = dev;
572 usb_autopm_get_interface(dev->interface);
577 static int v4l_stk_release(struct file *fp)
579 struct stk_camera *dev = fp->private_data;
581 if (dev->owner == fp) {
582 stk_stop_stream(dev);
583 stk_free_buffers(dev);
584 stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
585 unset_initialised(dev);
590 usb_autopm_put_interface(dev->interface);
595 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
596 size_t count, loff_t *f_pos)
601 struct stk_sio_buffer *sbuf;
602 struct stk_camera *dev = fp->private_data;
604 if (!is_present(dev))
606 if (dev->owner && dev->owner != fp)
609 if (!is_streaming(dev)) {
610 if (stk_initialise(dev)
611 || stk_allocate_buffers(dev, 3)
612 || stk_start_stream(dev))
614 spin_lock_irqsave(&dev->spinlock, flags);
615 for (i = 0; i < dev->n_sbufs; i++) {
616 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
617 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
619 spin_unlock_irqrestore(&dev->spinlock, flags);
622 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
624 ret = wait_event_interruptible(dev->wait_frame,
625 !list_empty(&dev->sio_full) || !is_present(dev));
628 if (!is_present(dev))
631 if (count + *f_pos > dev->frame_size)
632 count = dev->frame_size - *f_pos;
633 spin_lock_irqsave(&dev->spinlock, flags);
634 if (list_empty(&dev->sio_full)) {
635 spin_unlock_irqrestore(&dev->spinlock, flags);
636 STK_ERROR("BUG: No siobufs ready\n");
639 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
640 spin_unlock_irqrestore(&dev->spinlock, flags);
642 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
647 if (*f_pos >= dev->frame_size) {
649 spin_lock_irqsave(&dev->spinlock, flags);
650 list_move_tail(&sbuf->list, &dev->sio_avail);
651 spin_unlock_irqrestore(&dev->spinlock, flags);
656 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
658 struct stk_camera *dev = fp->private_data;
660 poll_wait(fp, &dev->wait_frame, wait);
662 if (!is_present(dev))
665 if (!list_empty(&dev->sio_full))
666 return POLLIN | POLLRDNORM;
672 static void stk_v4l_vm_open(struct vm_area_struct *vma)
674 struct stk_sio_buffer *sbuf = vma->vm_private_data;
677 static void stk_v4l_vm_close(struct vm_area_struct *vma)
679 struct stk_sio_buffer *sbuf = vma->vm_private_data;
681 if (sbuf->mapcount == 0)
682 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
684 static const struct vm_operations_struct stk_v4l_vm_ops = {
685 .open = stk_v4l_vm_open,
686 .close = stk_v4l_vm_close
689 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
693 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
694 struct stk_camera *dev = fp->private_data;
695 struct stk_sio_buffer *sbuf = NULL;
697 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
700 for (i = 0; i < dev->n_sbufs; i++) {
701 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
702 sbuf = dev->sio_bufs + i;
708 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
711 vma->vm_flags |= VM_DONTEXPAND;
712 vma->vm_private_data = sbuf;
713 vma->vm_ops = &stk_v4l_vm_ops;
714 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
715 stk_v4l_vm_open(vma);
719 /* v4l ioctl handlers */
721 static int stk_vidioc_querycap(struct file *filp,
722 void *priv, struct v4l2_capability *cap)
724 strcpy(cap->driver, "stk");
725 strcpy(cap->card, "stk");
726 cap->version = DRIVER_VERSION_NUM;
728 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
729 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
733 static int stk_vidioc_enum_input(struct file *filp,
734 void *priv, struct v4l2_input *input)
736 if (input->index != 0)
739 strcpy(input->name, "Syntek USB Camera");
740 input->type = V4L2_INPUT_TYPE_CAMERA;
745 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
751 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
760 static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
765 /* List of all V4Lv2 controls supported by the driver */
766 static struct v4l2_queryctrl stk_controls[] = {
768 .id = V4L2_CID_BRIGHTNESS,
769 .type = V4L2_CTRL_TYPE_INTEGER,
770 .name = "Brightness",
774 .default_value = 0x6000,
777 .id = V4L2_CID_HFLIP,
778 .type = V4L2_CTRL_TYPE_BOOLEAN,
779 .name = "Horizontal Flip",
786 .id = V4L2_CID_VFLIP,
787 .type = V4L2_CTRL_TYPE_BOOLEAN,
788 .name = "Vertical Flip",
796 static int stk_vidioc_queryctrl(struct file *filp,
797 void *priv, struct v4l2_queryctrl *c)
801 nbr = ARRAY_SIZE(stk_controls);
803 for (i = 0; i < nbr; i++) {
804 if (stk_controls[i].id == c->id) {
805 memcpy(c, &stk_controls[i],
806 sizeof(struct v4l2_queryctrl));
813 static int stk_vidioc_g_ctrl(struct file *filp,
814 void *priv, struct v4l2_control *c)
816 struct stk_camera *dev = priv;
818 case V4L2_CID_BRIGHTNESS:
819 c->value = dev->vsettings.brightness;
822 c->value = dev->vsettings.hflip;
825 c->value = dev->vsettings.vflip;
833 static int stk_vidioc_s_ctrl(struct file *filp,
834 void *priv, struct v4l2_control *c)
836 struct stk_camera *dev = priv;
838 case V4L2_CID_BRIGHTNESS:
839 dev->vsettings.brightness = c->value;
840 return stk_sensor_set_brightness(dev, c->value >> 8);
842 dev->vsettings.hflip = c->value;
845 dev->vsettings.vflip = c->value;
854 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
855 void *priv, struct v4l2_fmtdesc *fmtd)
857 switch (fmtd->index) {
859 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
860 strcpy(fmtd->description, "r5g6b5");
863 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
864 strcpy(fmtd->description, "r5g6b5BE");
867 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
868 strcpy(fmtd->description, "yuv4:2:2");
871 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
872 strcpy(fmtd->description, "Raw bayer");
875 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
876 strcpy(fmtd->description, "yuv4:2:2");
884 static struct stk_size {
889 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
890 { .w = 640, .h = 480, .m = MODE_VGA, },
891 { .w = 352, .h = 288, .m = MODE_CIF, },
892 { .w = 320, .h = 240, .m = MODE_QVGA, },
893 { .w = 176, .h = 144, .m = MODE_QCIF, },
896 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
897 void *priv, struct v4l2_format *f)
899 struct v4l2_pix_format *pix_format = &f->fmt.pix;
900 struct stk_camera *dev = priv;
903 for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
904 stk_sizes[i].m != dev->vsettings.mode; i++)
906 if (i == ARRAY_SIZE(stk_sizes)) {
907 STK_ERROR("ERROR: mode invalid\n");
910 pix_format->width = stk_sizes[i].w;
911 pix_format->height = stk_sizes[i].h;
912 pix_format->field = V4L2_FIELD_NONE;
913 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
914 pix_format->pixelformat = dev->vsettings.palette;
915 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
916 pix_format->bytesperline = pix_format->width;
918 pix_format->bytesperline = 2 * pix_format->width;
919 pix_format->sizeimage = pix_format->bytesperline
920 * pix_format->height;
924 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
925 void *priv, struct v4l2_format *fmtd)
928 switch (fmtd->fmt.pix.pixelformat) {
929 case V4L2_PIX_FMT_RGB565:
930 case V4L2_PIX_FMT_RGB565X:
931 case V4L2_PIX_FMT_UYVY:
932 case V4L2_PIX_FMT_YUYV:
933 case V4L2_PIX_FMT_SBGGR8:
938 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
939 if (fmtd->fmt.pix.width > stk_sizes[i].w)
942 if (i == ARRAY_SIZE(stk_sizes)
943 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
944 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
945 fmtd->fmt.pix.height = stk_sizes[i-1].h;
946 fmtd->fmt.pix.width = stk_sizes[i-1].w;
947 fmtd->fmt.pix.priv = i - 1;
949 fmtd->fmt.pix.height = stk_sizes[i].h;
950 fmtd->fmt.pix.width = stk_sizes[i].w;
951 fmtd->fmt.pix.priv = i;
954 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
955 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
956 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
957 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
959 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
960 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
961 * fmtd->fmt.pix.height;
965 static int stk_setup_format(struct stk_camera *dev)
969 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
973 while (i < ARRAY_SIZE(stk_sizes) &&
974 stk_sizes[i].m != dev->vsettings.mode)
976 if (i == ARRAY_SIZE(stk_sizes)) {
977 STK_ERROR("Something is broken in %s\n", __func__);
980 /* This registers controls some timings, not sure of what. */
981 stk_camera_write_reg(dev, 0x001b, 0x0e);
982 if (dev->vsettings.mode == MODE_SXGA)
983 stk_camera_write_reg(dev, 0x001c, 0x0e);
985 stk_camera_write_reg(dev, 0x001c, 0x46);
987 * Registers 0x0115 0x0114 are the size of each line (bytes),
988 * regs 0x0117 0x0116 are the heigth of the image.
990 stk_camera_write_reg(dev, 0x0115,
991 ((stk_sizes[i].w * depth) >> 8) & 0xff);
992 stk_camera_write_reg(dev, 0x0114,
993 (stk_sizes[i].w * depth) & 0xff);
994 stk_camera_write_reg(dev, 0x0117,
995 (stk_sizes[i].h >> 8) & 0xff);
996 stk_camera_write_reg(dev, 0x0116,
997 stk_sizes[i].h & 0xff);
998 return stk_sensor_configure(dev);
1001 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1002 void *priv, struct v4l2_format *fmtd)
1005 struct stk_camera *dev = priv;
1009 if (!is_present(dev))
1011 if (is_streaming(dev))
1013 if (dev->owner && dev->owner != filp)
1015 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1020 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1021 stk_free_buffers(dev);
1022 dev->frame_size = fmtd->fmt.pix.sizeimage;
1023 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1025 stk_initialise(dev);
1026 return stk_setup_format(dev);
1029 static int stk_vidioc_reqbufs(struct file *filp,
1030 void *priv, struct v4l2_requestbuffers *rb)
1032 struct stk_camera *dev = priv;
1036 if (rb->memory != V4L2_MEMORY_MMAP)
1038 if (is_streaming(dev)
1039 || (dev->owner && dev->owner != filp))
1043 /*FIXME If they ask for zero, we must stop streaming and free */
1046 /* Arbitrary limit */
1047 else if (rb->count > 5)
1050 stk_allocate_buffers(dev, rb->count);
1051 rb->count = dev->n_sbufs;
1055 static int stk_vidioc_querybuf(struct file *filp,
1056 void *priv, struct v4l2_buffer *buf)
1058 struct stk_camera *dev = priv;
1059 struct stk_sio_buffer *sbuf;
1061 if (buf->index >= dev->n_sbufs)
1063 sbuf = dev->sio_bufs + buf->index;
1064 *buf = sbuf->v4lbuf;
1068 static int stk_vidioc_qbuf(struct file *filp,
1069 void *priv, struct v4l2_buffer *buf)
1071 struct stk_camera *dev = priv;
1072 struct stk_sio_buffer *sbuf;
1073 unsigned long flags;
1075 if (buf->memory != V4L2_MEMORY_MMAP)
1078 if (buf->index >= dev->n_sbufs)
1080 sbuf = dev->sio_bufs + buf->index;
1081 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1083 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1084 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1085 spin_lock_irqsave(&dev->spinlock, flags);
1086 list_add_tail(&sbuf->list, &dev->sio_avail);
1087 *buf = sbuf->v4lbuf;
1088 spin_unlock_irqrestore(&dev->spinlock, flags);
1092 static int stk_vidioc_dqbuf(struct file *filp,
1093 void *priv, struct v4l2_buffer *buf)
1095 struct stk_camera *dev = priv;
1096 struct stk_sio_buffer *sbuf;
1097 unsigned long flags;
1100 if (!is_streaming(dev))
1103 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1104 return -EWOULDBLOCK;
1105 ret = wait_event_interruptible(dev->wait_frame,
1106 !list_empty(&dev->sio_full) || !is_present(dev));
1109 if (!is_present(dev))
1112 spin_lock_irqsave(&dev->spinlock, flags);
1113 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1114 list_del_init(&sbuf->list);
1115 spin_unlock_irqrestore(&dev->spinlock, flags);
1116 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1117 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1118 sbuf->v4lbuf.sequence = ++dev->sequence;
1119 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1121 *buf = sbuf->v4lbuf;
1125 static int stk_vidioc_streamon(struct file *filp,
1126 void *priv, enum v4l2_buf_type type)
1128 struct stk_camera *dev = priv;
1129 if (is_streaming(dev))
1131 if (dev->sio_bufs == NULL)
1134 return stk_start_stream(dev);
1137 static int stk_vidioc_streamoff(struct file *filp,
1138 void *priv, enum v4l2_buf_type type)
1140 struct stk_camera *dev = priv;
1141 unsigned long flags;
1143 stk_stop_stream(dev);
1144 spin_lock_irqsave(&dev->spinlock, flags);
1145 INIT_LIST_HEAD(&dev->sio_avail);
1146 INIT_LIST_HEAD(&dev->sio_full);
1147 for (i = 0; i < dev->n_sbufs; i++) {
1148 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1149 dev->sio_bufs[i].v4lbuf.flags = 0;
1151 spin_unlock_irqrestore(&dev->spinlock, flags);
1156 static int stk_vidioc_g_parm(struct file *filp,
1157 void *priv, struct v4l2_streamparm *sp)
1159 /*FIXME This is not correct */
1160 sp->parm.capture.timeperframe.numerator = 1;
1161 sp->parm.capture.timeperframe.denominator = 30;
1162 sp->parm.capture.readbuffers = 2;
1166 static int stk_vidioc_enum_framesizes(struct file *filp,
1167 void *priv, struct v4l2_frmsizeenum *frms)
1169 if (frms->index >= ARRAY_SIZE(stk_sizes))
1171 switch (frms->pixel_format) {
1172 case V4L2_PIX_FMT_RGB565:
1173 case V4L2_PIX_FMT_RGB565X:
1174 case V4L2_PIX_FMT_UYVY:
1175 case V4L2_PIX_FMT_YUYV:
1176 case V4L2_PIX_FMT_SBGGR8:
1177 frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1178 frms->discrete.width = stk_sizes[frms->index].w;
1179 frms->discrete.height = stk_sizes[frms->index].h;
1181 default: return -EINVAL;
1185 static struct v4l2_file_operations v4l_stk_fops = {
1186 .owner = THIS_MODULE,
1187 .open = v4l_stk_open,
1188 .release = v4l_stk_release,
1189 .read = v4l_stk_read,
1190 .poll = v4l_stk_poll,
1191 .mmap = v4l_stk_mmap,
1192 .ioctl = video_ioctl2,
1195 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1196 .vidioc_querycap = stk_vidioc_querycap,
1197 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1198 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1199 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1200 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1201 .vidioc_enum_input = stk_vidioc_enum_input,
1202 .vidioc_s_input = stk_vidioc_s_input,
1203 .vidioc_g_input = stk_vidioc_g_input,
1204 .vidioc_s_std = stk_vidioc_s_std,
1205 .vidioc_reqbufs = stk_vidioc_reqbufs,
1206 .vidioc_querybuf = stk_vidioc_querybuf,
1207 .vidioc_qbuf = stk_vidioc_qbuf,
1208 .vidioc_dqbuf = stk_vidioc_dqbuf,
1209 .vidioc_streamon = stk_vidioc_streamon,
1210 .vidioc_streamoff = stk_vidioc_streamoff,
1211 .vidioc_queryctrl = stk_vidioc_queryctrl,
1212 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1213 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1214 .vidioc_g_parm = stk_vidioc_g_parm,
1215 .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1218 static void stk_v4l_dev_release(struct video_device *vd)
1220 struct stk_camera *dev = vdev_to_camera(vd);
1222 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1223 STK_ERROR("We are leaking memory\n");
1224 usb_put_intf(dev->interface);
1228 static struct video_device stk_v4l_data = {
1229 .name = "stkwebcam",
1230 .tvnorms = V4L2_STD_UNKNOWN,
1231 .current_norm = V4L2_STD_UNKNOWN,
1232 .fops = &v4l_stk_fops,
1233 .ioctl_ops = &v4l_stk_ioctl_ops,
1234 .release = stk_v4l_dev_release,
1238 static int stk_register_video_device(struct stk_camera *dev)
1242 dev->vdev = stk_v4l_data;
1243 dev->vdev.debug = debug;
1244 dev->vdev.parent = &dev->interface->dev;
1245 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1247 STK_ERROR("v4l registration failed\n");
1249 STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1250 video_device_node_name(&dev->vdev));
1257 static int stk_camera_probe(struct usb_interface *interface,
1258 const struct usb_device_id *id)
1263 struct stk_camera *dev = NULL;
1264 struct usb_device *udev = interface_to_usbdev(interface);
1265 struct usb_host_interface *iface_desc;
1266 struct usb_endpoint_descriptor *endpoint;
1268 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1270 STK_ERROR("Out of memory !\n");
1274 spin_lock_init(&dev->spinlock);
1275 init_waitqueue_head(&dev->wait_frame);
1278 dev->interface = interface;
1279 usb_get_intf(interface);
1281 dev->vsettings.vflip = vflip;
1282 dev->vsettings.hflip = hflip;
1286 /* Set up the endpoint information
1287 * use only the first isoc-in endpoint
1288 * for the current alternate setting */
1289 iface_desc = interface->cur_altsetting;
1291 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1292 endpoint = &iface_desc->endpoint[i].desc;
1295 && usb_endpoint_is_isoc_in(endpoint)) {
1296 /* we found an isoc in endpoint */
1297 dev->isoc_ep = usb_endpoint_num(endpoint);
1301 if (!dev->isoc_ep) {
1302 STK_ERROR("Could not find isoc-in endpoint");
1306 dev->vsettings.brightness = 0x7fff;
1307 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1308 dev->vsettings.mode = MODE_VGA;
1309 dev->frame_size = 640 * 480 * 2;
1311 INIT_LIST_HEAD(&dev->sio_avail);
1312 INIT_LIST_HEAD(&dev->sio_full);
1314 usb_set_intfdata(interface, dev);
1316 err = stk_register_video_device(dev);
1327 static void stk_camera_disconnect(struct usb_interface *interface)
1329 struct stk_camera *dev = usb_get_intfdata(interface);
1331 usb_set_intfdata(interface, NULL);
1334 wake_up_interruptible(&dev->wait_frame);
1336 STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1337 video_device_node_name(&dev->vdev));
1339 video_unregister_device(&dev->vdev);
1343 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1345 struct stk_camera *dev = usb_get_intfdata(intf);
1346 if (is_streaming(dev)) {
1347 stk_stop_stream(dev);
1348 /* yes, this is ugly */
1354 static int stk_camera_resume(struct usb_interface *intf)
1356 struct stk_camera *dev = usb_get_intfdata(intf);
1357 if (!is_initialised(dev))
1359 unset_initialised(dev);
1360 stk_initialise(dev);
1361 stk_camera_write_reg(dev, 0x0, 0x49);
1362 stk_setup_format(dev);
1363 if (is_streaming(dev))
1364 stk_start_stream(dev);
1369 static struct usb_driver stk_camera_driver = {
1370 .name = "stkwebcam",
1371 .probe = stk_camera_probe,
1372 .disconnect = stk_camera_disconnect,
1373 .id_table = stkwebcam_table,
1375 .suspend = stk_camera_suspend,
1376 .resume = stk_camera_resume,
1381 static int __init stk_camera_init(void)
1385 result = usb_register(&stk_camera_driver);
1387 STK_ERROR("usb_register failed ! Error number %d\n", result);
1393 static void __exit stk_camera_exit(void)
1395 usb_deregister(&stk_camera_driver);
1398 module_init(stk_camera_init);
1399 module_exit(stk_camera_exit);