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/dmi.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>
38 #include <media/v4l2-event.h>
40 #include "stk-webcam.h"
43 static int hflip = -1;
44 module_param(hflip, int, 0444);
45 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 0");
47 static int vflip = -1;
48 module_param(vflip, int, 0444);
49 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 0");
52 module_param(debug, int, 0444);
53 MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
55 MODULE_LICENSE("GPL");
56 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
57 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
59 /* Some cameras have audio interfaces, we aren't interested in those */
60 static struct usb_device_id stkwebcam_table[] = {
61 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
62 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
65 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
68 * The stk webcam laptop module is mounted upside down in some laptops :(
70 * Some background information (thanks to Hans de Goede for providing this):
72 * 1) Once upon a time the stkwebcam driver was written
74 * 2) The webcam in question was used mostly in Asus laptop models, including
75 * the laptop of the original author of the driver, and in these models, in
76 * typical Asus fashion (see the long long list for uvc cams inside v4l-utils),
77 * they mounted the webcam-module the wrong way up. So the hflip and vflip
78 * module options were given a default value of 1 (the correct value for
79 * upside down mounted models)
81 * 3) Years later I got a bug report from a user with a laptop with stkwebcam,
82 * where the module was actually mounted the right way up, and thus showed
83 * upside down under Linux. So now I was facing the choice of 2 options:
85 * a) Add a not-upside-down list to stkwebcam, which overrules the default.
87 * b) Do it like all the other drivers do, and make the default right for
88 * cams mounted the proper way and add an upside-down model list, with
89 * models where we need to flip-by-default.
91 * Despite knowing that going b) would cause a period of pain where we were
92 * building the table I opted to go for option b), since a) is just too ugly,
93 * and worse different from how every other driver does it leading to
94 * confusion in the long run. This change was made in kernel 3.6.
96 * So for any user report about upside-down images since kernel 3.6 ask them
97 * to provide the output of 'sudo dmidecode' so the laptop can be added in
100 static const struct dmi_system_id stk_upside_down_dmi_table[] = {
104 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
105 DMI_MATCH(DMI_PRODUCT_NAME, "G1")
108 .ident = "ASUS F3JC",
110 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
111 DMI_MATCH(DMI_PRODUCT_NAME, "F3JC")
117 DMI_MATCH(DMI_SYS_VENDOR, "HCL Infosystems Limited"),
118 DMI_MATCH(DMI_PRODUCT_NAME, "T12Rg-H")
128 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
130 struct usb_device *udev = dev->udev;
133 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
135 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
147 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
149 struct usb_device *udev = dev->udev;
152 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
154 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
166 static int stk_start_stream(struct stk_camera *dev)
170 int value_116, value_117;
172 if (!is_present(dev))
174 if (!is_memallocd(dev) || !is_initialised(dev)) {
175 STK_ERROR("FIXME: Buffers are not allocated\n");
178 ret = usb_set_interface(dev->udev, 0, 5);
181 STK_ERROR("usb_set_interface failed !\n");
182 if (stk_sensor_wakeup(dev))
183 STK_ERROR("error awaking the sensor\n");
185 stk_camera_read_reg(dev, 0x0116, &value_116);
186 stk_camera_read_reg(dev, 0x0117, &value_117);
188 stk_camera_write_reg(dev, 0x0116, 0x0000);
189 stk_camera_write_reg(dev, 0x0117, 0x0000);
191 stk_camera_read_reg(dev, 0x0100, &value);
192 stk_camera_write_reg(dev, 0x0100, value | 0x80);
194 stk_camera_write_reg(dev, 0x0116, value_116);
195 stk_camera_write_reg(dev, 0x0117, value_117);
196 for (i = 0; i < MAX_ISO_BUFS; i++) {
197 if (dev->isobufs[i].urb) {
198 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
199 atomic_inc(&dev->urbs_used);
208 static int stk_stop_stream(struct stk_camera *dev)
212 if (is_present(dev)) {
213 stk_camera_read_reg(dev, 0x0100, &value);
214 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
215 if (dev->isobufs != NULL) {
216 for (i = 0; i < MAX_ISO_BUFS; i++) {
217 if (dev->isobufs[i].urb)
218 usb_kill_urb(dev->isobufs[i].urb);
221 unset_streaming(dev);
223 if (usb_set_interface(dev->udev, 0, 0))
224 STK_ERROR("usb_set_interface failed !\n");
225 if (stk_sensor_sleep(dev))
226 STK_ERROR("error suspending the sensor\n");
232 * This seems to be the shortest init sequence we
233 * must do in order to find the sensor
234 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
235 * is also reset. Maybe powers down it?
236 * Rest of values don't make a difference
239 static struct regval stk1125_initvals[] = {
240 /*TODO: What means this sequence? */
269 static int stk_initialise(struct stk_camera *dev)
273 if (!is_present(dev))
275 if (is_initialised(dev))
277 rv = stk1125_initvals;
278 while (rv->reg != 0xffff) {
279 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
284 if (stk_sensor_init(dev) == 0) {
285 set_initialised(dev);
291 /* *********************************************** */
293 * This function is called as an URB transfert is complete (Isochronous pipe).
294 * So, the traitement is done in interrupt time, so it has be fast, not crash,
295 * and not stall. Neat.
297 static void stk_isoc_handler(struct urb *urb)
304 unsigned char *fill = NULL;
305 unsigned char *iso_buf = NULL;
307 struct stk_camera *dev;
308 struct stk_sio_buffer *fb;
310 dev = (struct stk_camera *) urb->context;
313 STK_ERROR("isoc_handler called with NULL device !\n");
317 if (urb->status == -ENOENT || urb->status == -ECONNRESET
318 || urb->status == -ESHUTDOWN) {
319 atomic_dec(&dev->urbs_used);
323 spin_lock_irqsave(&dev->spinlock, flags);
325 if (urb->status != -EINPROGRESS && urb->status != 0) {
326 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
330 if (list_empty(&dev->sio_avail)) {
331 /*FIXME Stop streaming after a while */
332 (void) (printk_ratelimit() &&
333 STK_ERROR("isoc_handler without available buffer!\n"));
336 fb = list_first_entry(&dev->sio_avail,
337 struct stk_sio_buffer, list);
338 fill = fb->buffer + fb->v4lbuf.bytesused;
340 for (i = 0; i < urb->number_of_packets; i++) {
341 if (urb->iso_frame_desc[i].status != 0) {
342 if (urb->iso_frame_desc[i].status != -EXDEV)
343 STK_ERROR("Frame %d has error %d\n", i,
344 urb->iso_frame_desc[i].status);
347 framelen = urb->iso_frame_desc[i].actual_length;
348 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
351 continue; /* no data */
354 * we found something informational from there
355 * the isoc frames have to type of headers
356 * type1: 00 xx 00 00 or 20 xx 00 00
357 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
358 * xx is a sequencer which has never been seen over 0x3f
359 * imho data written down looks like bayer, i see similarities
360 * after every 640 bytes
362 if (*iso_buf & 0x80) {
365 /* This marks a new frame */
366 if (fb->v4lbuf.bytesused != 0
367 && fb->v4lbuf.bytesused != dev->frame_size) {
368 (void) (printk_ratelimit() &&
369 STK_ERROR("frame %d, "
370 "bytesused=%d, skipping\n",
371 i, fb->v4lbuf.bytesused));
372 fb->v4lbuf.bytesused = 0;
374 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
375 if (list_is_singular(&dev->sio_avail)) {
376 /* Always reuse the last buffer */
377 fb->v4lbuf.bytesused = 0;
380 list_move_tail(dev->sio_avail.next,
382 wake_up(&dev->wait_frame);
383 fb = list_first_entry(&dev->sio_avail,
384 struct stk_sio_buffer, list);
385 fb->v4lbuf.bytesused = 0;
394 /* Our buffer is full !!! */
395 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
396 (void) (printk_ratelimit() &&
397 STK_ERROR("Frame buffer overflow, lost sync\n"));
398 /*FIXME Do something here? */
401 spin_unlock_irqrestore(&dev->spinlock, flags);
402 memcpy(fill, iso_buf, framelen);
403 spin_lock_irqsave(&dev->spinlock, flags);
406 /* New size of our buffer */
407 fb->v4lbuf.bytesused += framelen;
411 spin_unlock_irqrestore(&dev->spinlock, flags);
412 urb->dev = dev->udev;
413 ret = usb_submit_urb(urb, GFP_ATOMIC);
415 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
420 /* -------------------------------------------- */
422 static int stk_prepare_iso(struct stk_camera *dev)
427 struct usb_device *udev;
434 STK_ERROR("isobufs already allocated. Bad\n");
436 dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
438 if (dev->isobufs == NULL) {
439 STK_ERROR("Unable to allocate iso buffers\n");
442 for (i = 0; i < MAX_ISO_BUFS; i++) {
443 if (dev->isobufs[i].data == NULL) {
444 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
446 STK_ERROR("Failed to allocate iso buffer %d\n",
450 dev->isobufs[i].data = kbuf;
452 STK_ERROR("isobuf data already allocated\n");
453 if (dev->isobufs[i].urb == NULL) {
454 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
456 STK_ERROR("Failed to allocate URB %d\n", i);
459 dev->isobufs[i].urb = urb;
461 STK_ERROR("Killing URB\n");
462 usb_kill_urb(dev->isobufs[i].urb);
463 urb = dev->isobufs[i].urb;
467 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
468 urb->transfer_flags = URB_ISO_ASAP;
469 urb->transfer_buffer = dev->isobufs[i].data;
470 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
471 urb->complete = stk_isoc_handler;
473 urb->start_frame = 0;
474 urb->number_of_packets = ISO_FRAMES_PER_DESC;
476 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
477 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
478 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
485 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
486 kfree(dev->isobufs[i].data);
487 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
488 usb_free_urb(dev->isobufs[i].urb);
494 static void stk_clean_iso(struct stk_camera *dev)
498 if (dev == NULL || dev->isobufs == NULL)
501 for (i = 0; i < MAX_ISO_BUFS; i++) {
504 urb = dev->isobufs[i].urb;
506 if (atomic_read(&dev->urbs_used) && is_present(dev))
510 kfree(dev->isobufs[i].data);
514 unset_memallocd(dev);
517 static int stk_setup_siobuf(struct stk_camera *dev, int index)
519 struct stk_sio_buffer *buf = dev->sio_bufs + index;
520 INIT_LIST_HEAD(&buf->list);
521 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
522 buf->buffer = vmalloc_user(buf->v4lbuf.length);
523 if (buf->buffer == NULL)
527 buf->v4lbuf.index = index;
528 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
529 buf->v4lbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
530 buf->v4lbuf.field = V4L2_FIELD_NONE;
531 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
532 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
536 static int stk_free_sio_buffers(struct stk_camera *dev)
541 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
544 * If any buffers are mapped, we cannot free them at all.
546 for (i = 0; i < dev->n_sbufs; i++) {
547 if (dev->sio_bufs[i].mapcount > 0)
553 spin_lock_irqsave(&dev->spinlock, flags);
554 INIT_LIST_HEAD(&dev->sio_avail);
555 INIT_LIST_HEAD(&dev->sio_full);
556 nbufs = dev->n_sbufs;
558 spin_unlock_irqrestore(&dev->spinlock, flags);
559 for (i = 0; i < nbufs; i++) {
560 if (dev->sio_bufs[i].buffer != NULL)
561 vfree(dev->sio_bufs[i].buffer);
563 kfree(dev->sio_bufs);
564 dev->sio_bufs = NULL;
568 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
571 if (dev->sio_bufs != NULL)
572 STK_ERROR("sio_bufs already allocated\n");
574 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
576 if (dev->sio_bufs == NULL)
578 for (i = 0; i < n_sbufs; i++) {
579 if (stk_setup_siobuf(dev, i))
580 return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
587 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
590 err = stk_prepare_iso(dev);
595 err = stk_prepare_sio_buffers(dev, n_sbufs);
597 stk_free_sio_buffers(dev);
603 static void stk_free_buffers(struct stk_camera *dev)
606 stk_free_sio_buffers(dev);
608 /* -------------------------------------------- */
610 /* v4l file operations */
612 static int v4l_stk_open(struct file *fp)
614 struct stk_camera *dev = video_drvdata(fp);
617 if (dev == NULL || !is_present(dev))
620 if (mutex_lock_interruptible(&dev->lock))
622 if (!dev->first_init)
623 stk_camera_write_reg(dev, 0x0, 0x24);
627 err = v4l2_fh_open(fp);
629 usb_autopm_get_interface(dev->interface);
630 mutex_unlock(&dev->lock);
634 static int v4l_stk_release(struct file *fp)
636 struct stk_camera *dev = video_drvdata(fp);
638 mutex_lock(&dev->lock);
639 if (dev->owner == fp) {
640 stk_stop_stream(dev);
641 stk_free_buffers(dev);
642 stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
643 unset_initialised(dev);
648 usb_autopm_put_interface(dev->interface);
649 mutex_unlock(&dev->lock);
650 return v4l2_fh_release(fp);
653 static ssize_t stk_read(struct file *fp, char __user *buf,
654 size_t count, loff_t *f_pos)
659 struct stk_sio_buffer *sbuf;
660 struct stk_camera *dev = video_drvdata(fp);
662 if (!is_present(dev))
664 if (dev->owner && (!dev->reading || dev->owner != fp))
667 if (!is_streaming(dev)) {
668 if (stk_initialise(dev)
669 || stk_allocate_buffers(dev, 3)
670 || stk_start_stream(dev))
673 spin_lock_irqsave(&dev->spinlock, flags);
674 for (i = 0; i < dev->n_sbufs; i++) {
675 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
676 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
678 spin_unlock_irqrestore(&dev->spinlock, flags);
681 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
683 ret = wait_event_interruptible(dev->wait_frame,
684 !list_empty(&dev->sio_full) || !is_present(dev));
687 if (!is_present(dev))
690 if (count + *f_pos > dev->frame_size)
691 count = dev->frame_size - *f_pos;
692 spin_lock_irqsave(&dev->spinlock, flags);
693 if (list_empty(&dev->sio_full)) {
694 spin_unlock_irqrestore(&dev->spinlock, flags);
695 STK_ERROR("BUG: No siobufs ready\n");
698 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
699 spin_unlock_irqrestore(&dev->spinlock, flags);
701 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
706 if (*f_pos >= dev->frame_size) {
708 spin_lock_irqsave(&dev->spinlock, flags);
709 list_move_tail(&sbuf->list, &dev->sio_avail);
710 spin_unlock_irqrestore(&dev->spinlock, flags);
715 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
716 size_t count, loff_t *f_pos)
718 struct stk_camera *dev = video_drvdata(fp);
721 if (mutex_lock_interruptible(&dev->lock))
723 ret = stk_read(fp, buf, count, f_pos);
724 mutex_unlock(&dev->lock);
728 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
730 struct stk_camera *dev = video_drvdata(fp);
731 unsigned res = v4l2_ctrl_poll(fp, wait);
733 poll_wait(fp, &dev->wait_frame, wait);
735 if (!is_present(dev))
738 if (!list_empty(&dev->sio_full))
739 return res | POLLIN | POLLRDNORM;
745 static void stk_v4l_vm_open(struct vm_area_struct *vma)
747 struct stk_sio_buffer *sbuf = vma->vm_private_data;
750 static void stk_v4l_vm_close(struct vm_area_struct *vma)
752 struct stk_sio_buffer *sbuf = vma->vm_private_data;
754 if (sbuf->mapcount == 0)
755 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
757 static const struct vm_operations_struct stk_v4l_vm_ops = {
758 .open = stk_v4l_vm_open,
759 .close = stk_v4l_vm_close
762 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
766 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
767 struct stk_camera *dev = video_drvdata(fp);
768 struct stk_sio_buffer *sbuf = NULL;
770 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
773 for (i = 0; i < dev->n_sbufs; i++) {
774 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
775 sbuf = dev->sio_bufs + i;
781 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
784 vma->vm_flags |= VM_DONTEXPAND;
785 vma->vm_private_data = sbuf;
786 vma->vm_ops = &stk_v4l_vm_ops;
787 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
788 stk_v4l_vm_open(vma);
792 /* v4l ioctl handlers */
794 static int stk_vidioc_querycap(struct file *filp,
795 void *priv, struct v4l2_capability *cap)
797 struct stk_camera *dev = video_drvdata(filp);
799 strcpy(cap->driver, "stk");
800 strcpy(cap->card, "stk");
801 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
803 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
804 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
805 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
809 static int stk_vidioc_enum_input(struct file *filp,
810 void *priv, struct v4l2_input *input)
812 if (input->index != 0)
815 strcpy(input->name, "Syntek USB Camera");
816 input->type = V4L2_INPUT_TYPE_CAMERA;
821 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
827 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
829 return i ? -EINVAL : 0;
832 static int stk_s_ctrl(struct v4l2_ctrl *ctrl)
834 struct stk_camera *dev =
835 container_of(ctrl->handler, struct stk_camera, hdl);
838 case V4L2_CID_BRIGHTNESS:
839 return stk_sensor_set_brightness(dev, ctrl->val);
841 if (dmi_check_system(stk_upside_down_dmi_table))
842 dev->vsettings.hflip = !ctrl->val;
844 dev->vsettings.hflip = ctrl->val;
847 if (dmi_check_system(stk_upside_down_dmi_table))
848 dev->vsettings.vflip = !ctrl->val;
850 dev->vsettings.vflip = ctrl->val;
859 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
860 void *priv, struct v4l2_fmtdesc *fmtd)
862 switch (fmtd->index) {
864 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
865 strcpy(fmtd->description, "r5g6b5");
868 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
869 strcpy(fmtd->description, "r5g6b5BE");
872 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
873 strcpy(fmtd->description, "yuv4:2:2");
876 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
877 strcpy(fmtd->description, "Raw bayer");
880 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
881 strcpy(fmtd->description, "yuv4:2:2");
889 static struct stk_size {
894 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
895 { .w = 640, .h = 480, .m = MODE_VGA, },
896 { .w = 352, .h = 288, .m = MODE_CIF, },
897 { .w = 320, .h = 240, .m = MODE_QVGA, },
898 { .w = 176, .h = 144, .m = MODE_QCIF, },
901 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
902 void *priv, struct v4l2_format *f)
904 struct v4l2_pix_format *pix_format = &f->fmt.pix;
905 struct stk_camera *dev = video_drvdata(filp);
908 for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
909 stk_sizes[i].m != dev->vsettings.mode; i++)
911 if (i == ARRAY_SIZE(stk_sizes)) {
912 STK_ERROR("ERROR: mode invalid\n");
915 pix_format->width = stk_sizes[i].w;
916 pix_format->height = stk_sizes[i].h;
917 pix_format->field = V4L2_FIELD_NONE;
918 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
919 pix_format->pixelformat = dev->vsettings.palette;
920 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
921 pix_format->bytesperline = pix_format->width;
923 pix_format->bytesperline = 2 * pix_format->width;
924 pix_format->sizeimage = pix_format->bytesperline
925 * pix_format->height;
929 static int stk_try_fmt_vid_cap(struct file *filp,
930 struct v4l2_format *fmtd, int *idx)
933 switch (fmtd->fmt.pix.pixelformat) {
934 case V4L2_PIX_FMT_RGB565:
935 case V4L2_PIX_FMT_RGB565X:
936 case V4L2_PIX_FMT_UYVY:
937 case V4L2_PIX_FMT_YUYV:
938 case V4L2_PIX_FMT_SBGGR8:
943 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
944 if (fmtd->fmt.pix.width > stk_sizes[i].w)
947 if (i == ARRAY_SIZE(stk_sizes)
948 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
949 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
950 fmtd->fmt.pix.height = stk_sizes[i-1].h;
951 fmtd->fmt.pix.width = stk_sizes[i-1].w;
955 fmtd->fmt.pix.height = stk_sizes[i].h;
956 fmtd->fmt.pix.width = stk_sizes[i].w;
961 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
962 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
963 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
964 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
966 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
967 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
968 * fmtd->fmt.pix.height;
972 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
973 void *priv, struct v4l2_format *fmtd)
975 return stk_try_fmt_vid_cap(filp, fmtd, NULL);
978 static int stk_setup_format(struct stk_camera *dev)
982 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
986 while (i < ARRAY_SIZE(stk_sizes) &&
987 stk_sizes[i].m != dev->vsettings.mode)
989 if (i == ARRAY_SIZE(stk_sizes)) {
990 STK_ERROR("Something is broken in %s\n", __func__);
993 /* This registers controls some timings, not sure of what. */
994 stk_camera_write_reg(dev, 0x001b, 0x0e);
995 if (dev->vsettings.mode == MODE_SXGA)
996 stk_camera_write_reg(dev, 0x001c, 0x0e);
998 stk_camera_write_reg(dev, 0x001c, 0x46);
1000 * Registers 0x0115 0x0114 are the size of each line (bytes),
1001 * regs 0x0117 0x0116 are the heigth of the image.
1003 stk_camera_write_reg(dev, 0x0115,
1004 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1005 stk_camera_write_reg(dev, 0x0114,
1006 (stk_sizes[i].w * depth) & 0xff);
1007 stk_camera_write_reg(dev, 0x0117,
1008 (stk_sizes[i].h >> 8) & 0xff);
1009 stk_camera_write_reg(dev, 0x0116,
1010 stk_sizes[i].h & 0xff);
1011 return stk_sensor_configure(dev);
1014 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1015 void *priv, struct v4l2_format *fmtd)
1019 struct stk_camera *dev = video_drvdata(filp);
1023 if (!is_present(dev))
1025 if (is_streaming(dev))
1029 ret = stk_try_fmt_vid_cap(filp, fmtd, &idx);
1033 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1034 stk_free_buffers(dev);
1035 dev->frame_size = fmtd->fmt.pix.sizeimage;
1036 dev->vsettings.mode = stk_sizes[idx].m;
1038 stk_initialise(dev);
1039 return stk_setup_format(dev);
1042 static int stk_vidioc_reqbufs(struct file *filp,
1043 void *priv, struct v4l2_requestbuffers *rb)
1045 struct stk_camera *dev = video_drvdata(filp);
1049 if (rb->memory != V4L2_MEMORY_MMAP)
1051 if (is_streaming(dev)
1052 || (dev->owner && dev->owner != filp))
1054 stk_free_buffers(dev);
1055 if (rb->count == 0) {
1056 stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
1057 unset_initialised(dev);
1063 /*FIXME If they ask for zero, we must stop streaming and free */
1066 /* Arbitrary limit */
1067 else if (rb->count > 5)
1070 stk_allocate_buffers(dev, rb->count);
1071 rb->count = dev->n_sbufs;
1075 static int stk_vidioc_querybuf(struct file *filp,
1076 void *priv, struct v4l2_buffer *buf)
1078 struct stk_camera *dev = video_drvdata(filp);
1079 struct stk_sio_buffer *sbuf;
1081 if (buf->index >= dev->n_sbufs)
1083 sbuf = dev->sio_bufs + buf->index;
1084 *buf = sbuf->v4lbuf;
1088 static int stk_vidioc_qbuf(struct file *filp,
1089 void *priv, struct v4l2_buffer *buf)
1091 struct stk_camera *dev = video_drvdata(filp);
1092 struct stk_sio_buffer *sbuf;
1093 unsigned long flags;
1095 if (buf->memory != V4L2_MEMORY_MMAP)
1098 if (buf->index >= dev->n_sbufs)
1100 sbuf = dev->sio_bufs + buf->index;
1101 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1103 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1104 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1105 spin_lock_irqsave(&dev->spinlock, flags);
1106 list_add_tail(&sbuf->list, &dev->sio_avail);
1107 *buf = sbuf->v4lbuf;
1108 spin_unlock_irqrestore(&dev->spinlock, flags);
1112 static int stk_vidioc_dqbuf(struct file *filp,
1113 void *priv, struct v4l2_buffer *buf)
1115 struct stk_camera *dev = video_drvdata(filp);
1116 struct stk_sio_buffer *sbuf;
1117 unsigned long flags;
1120 if (!is_streaming(dev))
1123 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1124 return -EWOULDBLOCK;
1125 ret = wait_event_interruptible(dev->wait_frame,
1126 !list_empty(&dev->sio_full) || !is_present(dev));
1129 if (!is_present(dev))
1132 spin_lock_irqsave(&dev->spinlock, flags);
1133 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1134 list_del_init(&sbuf->list);
1135 spin_unlock_irqrestore(&dev->spinlock, flags);
1136 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1137 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1138 sbuf->v4lbuf.sequence = ++dev->sequence;
1139 v4l2_get_timestamp(&sbuf->v4lbuf.timestamp);
1141 *buf = sbuf->v4lbuf;
1145 static int stk_vidioc_streamon(struct file *filp,
1146 void *priv, enum v4l2_buf_type type)
1148 struct stk_camera *dev = video_drvdata(filp);
1149 if (is_streaming(dev))
1151 if (dev->sio_bufs == NULL)
1154 return stk_start_stream(dev);
1157 static int stk_vidioc_streamoff(struct file *filp,
1158 void *priv, enum v4l2_buf_type type)
1160 struct stk_camera *dev = video_drvdata(filp);
1161 unsigned long flags;
1163 stk_stop_stream(dev);
1164 spin_lock_irqsave(&dev->spinlock, flags);
1165 INIT_LIST_HEAD(&dev->sio_avail);
1166 INIT_LIST_HEAD(&dev->sio_full);
1167 for (i = 0; i < dev->n_sbufs; i++) {
1168 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1169 dev->sio_bufs[i].v4lbuf.flags = 0;
1171 spin_unlock_irqrestore(&dev->spinlock, flags);
1176 static int stk_vidioc_g_parm(struct file *filp,
1177 void *priv, struct v4l2_streamparm *sp)
1179 /*FIXME This is not correct */
1180 sp->parm.capture.timeperframe.numerator = 1;
1181 sp->parm.capture.timeperframe.denominator = 30;
1182 sp->parm.capture.readbuffers = 2;
1186 static int stk_vidioc_enum_framesizes(struct file *filp,
1187 void *priv, struct v4l2_frmsizeenum *frms)
1189 if (frms->index >= ARRAY_SIZE(stk_sizes))
1191 switch (frms->pixel_format) {
1192 case V4L2_PIX_FMT_RGB565:
1193 case V4L2_PIX_FMT_RGB565X:
1194 case V4L2_PIX_FMT_UYVY:
1195 case V4L2_PIX_FMT_YUYV:
1196 case V4L2_PIX_FMT_SBGGR8:
1197 frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1198 frms->discrete.width = stk_sizes[frms->index].w;
1199 frms->discrete.height = stk_sizes[frms->index].h;
1201 default: return -EINVAL;
1205 static const struct v4l2_ctrl_ops stk_ctrl_ops = {
1206 .s_ctrl = stk_s_ctrl,
1209 static struct v4l2_file_operations v4l_stk_fops = {
1210 .owner = THIS_MODULE,
1211 .open = v4l_stk_open,
1212 .release = v4l_stk_release,
1213 .read = v4l_stk_read,
1214 .poll = v4l_stk_poll,
1215 .mmap = v4l_stk_mmap,
1216 .unlocked_ioctl = video_ioctl2,
1219 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1220 .vidioc_querycap = stk_vidioc_querycap,
1221 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1222 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1223 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1224 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1225 .vidioc_enum_input = stk_vidioc_enum_input,
1226 .vidioc_s_input = stk_vidioc_s_input,
1227 .vidioc_g_input = stk_vidioc_g_input,
1228 .vidioc_reqbufs = stk_vidioc_reqbufs,
1229 .vidioc_querybuf = stk_vidioc_querybuf,
1230 .vidioc_qbuf = stk_vidioc_qbuf,
1231 .vidioc_dqbuf = stk_vidioc_dqbuf,
1232 .vidioc_streamon = stk_vidioc_streamon,
1233 .vidioc_streamoff = stk_vidioc_streamoff,
1234 .vidioc_g_parm = stk_vidioc_g_parm,
1235 .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1236 .vidioc_log_status = v4l2_ctrl_log_status,
1237 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1238 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1241 static void stk_v4l_dev_release(struct video_device *vd)
1243 struct stk_camera *dev = vdev_to_camera(vd);
1245 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1246 STK_ERROR("We are leaking memory\n");
1247 usb_put_intf(dev->interface);
1251 static struct video_device stk_v4l_data = {
1252 .name = "stkwebcam",
1253 .fops = &v4l_stk_fops,
1254 .ioctl_ops = &v4l_stk_ioctl_ops,
1255 .release = stk_v4l_dev_release,
1259 static int stk_register_video_device(struct stk_camera *dev)
1263 dev->vdev = stk_v4l_data;
1264 dev->vdev.lock = &dev->lock;
1265 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1266 video_set_drvdata(&dev->vdev, dev);
1267 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1269 STK_ERROR("v4l registration failed\n");
1271 STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1272 video_device_node_name(&dev->vdev));
1279 static int stk_camera_probe(struct usb_interface *interface,
1280 const struct usb_device_id *id)
1282 struct v4l2_ctrl_handler *hdl;
1286 struct stk_camera *dev = NULL;
1287 struct usb_device *udev = interface_to_usbdev(interface);
1288 struct usb_host_interface *iface_desc;
1289 struct usb_endpoint_descriptor *endpoint;
1291 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1293 STK_ERROR("Out of memory !\n");
1296 err = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1298 dev_err(&udev->dev, "couldn't register v4l2_device\n");
1303 v4l2_ctrl_handler_init(hdl, 3);
1304 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1305 V4L2_CID_BRIGHTNESS, 0, 0xff, 0x1, 0x60);
1306 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1307 V4L2_CID_HFLIP, 0, 1, 1, 1);
1308 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1309 V4L2_CID_VFLIP, 0, 1, 1, 1);
1312 dev_err(&udev->dev, "couldn't register control\n");
1315 dev->v4l2_dev.ctrl_handler = hdl;
1317 spin_lock_init(&dev->spinlock);
1318 mutex_init(&dev->lock);
1319 init_waitqueue_head(&dev->wait_frame);
1320 dev->first_init = 1; /* webcam LED management */
1323 dev->interface = interface;
1324 usb_get_intf(interface);
1327 dev->vsettings.hflip = hflip;
1328 else if (dmi_check_system(stk_upside_down_dmi_table))
1329 dev->vsettings.hflip = 1;
1331 dev->vsettings.hflip = 0;
1333 dev->vsettings.vflip = vflip;
1334 else if (dmi_check_system(stk_upside_down_dmi_table))
1335 dev->vsettings.vflip = 1;
1337 dev->vsettings.vflip = 0;
1341 /* Set up the endpoint information
1342 * use only the first isoc-in endpoint
1343 * for the current alternate setting */
1344 iface_desc = interface->cur_altsetting;
1346 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1347 endpoint = &iface_desc->endpoint[i].desc;
1350 && usb_endpoint_is_isoc_in(endpoint)) {
1351 /* we found an isoc in endpoint */
1352 dev->isoc_ep = usb_endpoint_num(endpoint);
1356 if (!dev->isoc_ep) {
1357 STK_ERROR("Could not find isoc-in endpoint");
1361 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1362 dev->vsettings.mode = MODE_VGA;
1363 dev->frame_size = 640 * 480 * 2;
1365 INIT_LIST_HEAD(&dev->sio_avail);
1366 INIT_LIST_HEAD(&dev->sio_full);
1368 usb_set_intfdata(interface, dev);
1370 err = stk_register_video_device(dev);
1377 v4l2_ctrl_handler_free(hdl);
1378 v4l2_device_unregister(&dev->v4l2_dev);
1383 static void stk_camera_disconnect(struct usb_interface *interface)
1385 struct stk_camera *dev = usb_get_intfdata(interface);
1387 usb_set_intfdata(interface, NULL);
1390 wake_up_interruptible(&dev->wait_frame);
1392 STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1393 video_device_node_name(&dev->vdev));
1395 video_unregister_device(&dev->vdev);
1396 v4l2_ctrl_handler_free(&dev->hdl);
1397 v4l2_device_unregister(&dev->v4l2_dev);
1401 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1403 struct stk_camera *dev = usb_get_intfdata(intf);
1404 if (is_streaming(dev)) {
1405 stk_stop_stream(dev);
1406 /* yes, this is ugly */
1412 static int stk_camera_resume(struct usb_interface *intf)
1414 struct stk_camera *dev = usb_get_intfdata(intf);
1415 if (!is_initialised(dev))
1417 unset_initialised(dev);
1418 stk_initialise(dev);
1419 stk_camera_write_reg(dev, 0x0, 0x49);
1420 stk_setup_format(dev);
1421 if (is_streaming(dev))
1422 stk_start_stream(dev);
1427 static struct usb_driver stk_camera_driver = {
1428 .name = "stkwebcam",
1429 .probe = stk_camera_probe,
1430 .disconnect = stk_camera_disconnect,
1431 .id_table = stkwebcam_table,
1433 .suspend = stk_camera_suspend,
1434 .resume = stk_camera_resume,
1438 module_usb_driver(stk_camera_driver);