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, u8 *value)
149 struct usb_device *udev = dev->udev;
153 buf = kmalloc(sizeof(u8), GFP_KERNEL);
157 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
159 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
176 static int stk_start_stream(struct stk_camera *dev)
180 u8 value_116, value_117;
183 if (!is_present(dev))
185 if (!is_memallocd(dev) || !is_initialised(dev)) {
186 STK_ERROR("FIXME: Buffers are not allocated\n");
189 ret = usb_set_interface(dev->udev, 0, 5);
192 STK_ERROR("usb_set_interface failed !\n");
193 if (stk_sensor_wakeup(dev))
194 STK_ERROR("error awaking the sensor\n");
196 stk_camera_read_reg(dev, 0x0116, &value_116);
197 stk_camera_read_reg(dev, 0x0117, &value_117);
199 stk_camera_write_reg(dev, 0x0116, 0x0000);
200 stk_camera_write_reg(dev, 0x0117, 0x0000);
202 stk_camera_read_reg(dev, 0x0100, &value);
203 stk_camera_write_reg(dev, 0x0100, value | 0x80);
205 stk_camera_write_reg(dev, 0x0116, value_116);
206 stk_camera_write_reg(dev, 0x0117, value_117);
207 for (i = 0; i < MAX_ISO_BUFS; i++) {
208 if (dev->isobufs[i].urb) {
209 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
210 atomic_inc(&dev->urbs_used);
219 static int stk_stop_stream(struct stk_camera *dev)
223 if (is_present(dev)) {
224 stk_camera_read_reg(dev, 0x0100, &value);
225 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
226 if (dev->isobufs != NULL) {
227 for (i = 0; i < MAX_ISO_BUFS; i++) {
228 if (dev->isobufs[i].urb)
229 usb_kill_urb(dev->isobufs[i].urb);
232 unset_streaming(dev);
234 if (usb_set_interface(dev->udev, 0, 0))
235 STK_ERROR("usb_set_interface failed !\n");
236 if (stk_sensor_sleep(dev))
237 STK_ERROR("error suspending the sensor\n");
243 * This seems to be the shortest init sequence we
244 * must do in order to find the sensor
245 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
246 * is also reset. Maybe powers down it?
247 * Rest of values don't make a difference
250 static struct regval stk1125_initvals[] = {
251 /*TODO: What means this sequence? */
280 static int stk_initialise(struct stk_camera *dev)
284 if (!is_present(dev))
286 if (is_initialised(dev))
288 rv = stk1125_initvals;
289 while (rv->reg != 0xffff) {
290 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
295 if (stk_sensor_init(dev) == 0) {
296 set_initialised(dev);
302 /* *********************************************** */
304 * This function is called as an URB transfert is complete (Isochronous pipe).
305 * So, the traitement is done in interrupt time, so it has be fast, not crash,
306 * and not stall. Neat.
308 static void stk_isoc_handler(struct urb *urb)
315 unsigned char *fill = NULL;
316 unsigned char *iso_buf = NULL;
318 struct stk_camera *dev;
319 struct stk_sio_buffer *fb;
321 dev = (struct stk_camera *) urb->context;
324 STK_ERROR("isoc_handler called with NULL device !\n");
328 if (urb->status == -ENOENT || urb->status == -ECONNRESET
329 || urb->status == -ESHUTDOWN) {
330 atomic_dec(&dev->urbs_used);
334 spin_lock_irqsave(&dev->spinlock, flags);
336 if (urb->status != -EINPROGRESS && urb->status != 0) {
337 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
341 if (list_empty(&dev->sio_avail)) {
342 /*FIXME Stop streaming after a while */
343 (void) (printk_ratelimit() &&
344 STK_ERROR("isoc_handler without available buffer!\n"));
347 fb = list_first_entry(&dev->sio_avail,
348 struct stk_sio_buffer, list);
349 fill = fb->buffer + fb->v4lbuf.bytesused;
351 for (i = 0; i < urb->number_of_packets; i++) {
352 if (urb->iso_frame_desc[i].status != 0) {
353 if (urb->iso_frame_desc[i].status != -EXDEV)
354 STK_ERROR("Frame %d has error %d\n", i,
355 urb->iso_frame_desc[i].status);
358 framelen = urb->iso_frame_desc[i].actual_length;
359 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
362 continue; /* no data */
365 * we found something informational from there
366 * the isoc frames have to type of headers
367 * type1: 00 xx 00 00 or 20 xx 00 00
368 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
369 * xx is a sequencer which has never been seen over 0x3f
370 * imho data written down looks like bayer, i see similarities
371 * after every 640 bytes
373 if (*iso_buf & 0x80) {
376 /* This marks a new frame */
377 if (fb->v4lbuf.bytesused != 0
378 && fb->v4lbuf.bytesused != dev->frame_size) {
379 (void) (printk_ratelimit() &&
380 STK_ERROR("frame %d, "
381 "bytesused=%d, skipping\n",
382 i, fb->v4lbuf.bytesused));
383 fb->v4lbuf.bytesused = 0;
385 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
386 if (list_is_singular(&dev->sio_avail)) {
387 /* Always reuse the last buffer */
388 fb->v4lbuf.bytesused = 0;
391 list_move_tail(dev->sio_avail.next,
393 wake_up(&dev->wait_frame);
394 fb = list_first_entry(&dev->sio_avail,
395 struct stk_sio_buffer, list);
396 fb->v4lbuf.bytesused = 0;
405 /* Our buffer is full !!! */
406 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
407 (void) (printk_ratelimit() &&
408 STK_ERROR("Frame buffer overflow, lost sync\n"));
409 /*FIXME Do something here? */
412 spin_unlock_irqrestore(&dev->spinlock, flags);
413 memcpy(fill, iso_buf, framelen);
414 spin_lock_irqsave(&dev->spinlock, flags);
417 /* New size of our buffer */
418 fb->v4lbuf.bytesused += framelen;
422 spin_unlock_irqrestore(&dev->spinlock, flags);
423 urb->dev = dev->udev;
424 ret = usb_submit_urb(urb, GFP_ATOMIC);
426 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
431 /* -------------------------------------------- */
433 static int stk_prepare_iso(struct stk_camera *dev)
438 struct usb_device *udev;
445 STK_ERROR("isobufs already allocated. Bad\n");
447 dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
449 if (dev->isobufs == NULL) {
450 STK_ERROR("Unable to allocate iso buffers\n");
453 for (i = 0; i < MAX_ISO_BUFS; i++) {
454 if (dev->isobufs[i].data == NULL) {
455 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
457 STK_ERROR("Failed to allocate iso buffer %d\n",
461 dev->isobufs[i].data = kbuf;
463 STK_ERROR("isobuf data already allocated\n");
464 if (dev->isobufs[i].urb == NULL) {
465 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
468 dev->isobufs[i].urb = urb;
470 STK_ERROR("Killing URB\n");
471 usb_kill_urb(dev->isobufs[i].urb);
472 urb = dev->isobufs[i].urb;
476 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
477 urb->transfer_flags = URB_ISO_ASAP;
478 urb->transfer_buffer = dev->isobufs[i].data;
479 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
480 urb->complete = stk_isoc_handler;
482 urb->start_frame = 0;
483 urb->number_of_packets = ISO_FRAMES_PER_DESC;
485 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
486 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
487 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
494 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
495 kfree(dev->isobufs[i].data);
496 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
497 usb_free_urb(dev->isobufs[i].urb);
503 static void stk_clean_iso(struct stk_camera *dev)
507 if (dev == NULL || dev->isobufs == NULL)
510 for (i = 0; i < MAX_ISO_BUFS; i++) {
513 urb = dev->isobufs[i].urb;
515 if (atomic_read(&dev->urbs_used) && is_present(dev))
519 kfree(dev->isobufs[i].data);
523 unset_memallocd(dev);
526 static int stk_setup_siobuf(struct stk_camera *dev, int index)
528 struct stk_sio_buffer *buf = dev->sio_bufs + index;
529 INIT_LIST_HEAD(&buf->list);
530 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
531 buf->buffer = vmalloc_user(buf->v4lbuf.length);
532 if (buf->buffer == NULL)
536 buf->v4lbuf.index = index;
537 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
538 buf->v4lbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
539 buf->v4lbuf.field = V4L2_FIELD_NONE;
540 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
541 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
545 static int stk_free_sio_buffers(struct stk_camera *dev)
550 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
553 * If any buffers are mapped, we cannot free them at all.
555 for (i = 0; i < dev->n_sbufs; i++) {
556 if (dev->sio_bufs[i].mapcount > 0)
562 spin_lock_irqsave(&dev->spinlock, flags);
563 INIT_LIST_HEAD(&dev->sio_avail);
564 INIT_LIST_HEAD(&dev->sio_full);
565 nbufs = dev->n_sbufs;
567 spin_unlock_irqrestore(&dev->spinlock, flags);
568 for (i = 0; i < nbufs; i++)
569 vfree(dev->sio_bufs[i].buffer);
570 kfree(dev->sio_bufs);
571 dev->sio_bufs = NULL;
575 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
578 if (dev->sio_bufs != NULL)
579 STK_ERROR("sio_bufs already allocated\n");
581 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
583 if (dev->sio_bufs == NULL)
585 for (i = 0; i < n_sbufs; i++) {
586 if (stk_setup_siobuf(dev, i))
587 return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
594 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
597 err = stk_prepare_iso(dev);
602 err = stk_prepare_sio_buffers(dev, n_sbufs);
604 stk_free_sio_buffers(dev);
610 static void stk_free_buffers(struct stk_camera *dev)
613 stk_free_sio_buffers(dev);
615 /* -------------------------------------------- */
617 /* v4l file operations */
619 static int v4l_stk_open(struct file *fp)
621 struct stk_camera *dev = video_drvdata(fp);
624 if (dev == NULL || !is_present(dev))
627 if (mutex_lock_interruptible(&dev->lock))
629 if (!dev->first_init)
630 stk_camera_write_reg(dev, 0x0, 0x24);
634 err = v4l2_fh_open(fp);
636 usb_autopm_get_interface(dev->interface);
637 mutex_unlock(&dev->lock);
641 static int v4l_stk_release(struct file *fp)
643 struct stk_camera *dev = video_drvdata(fp);
645 mutex_lock(&dev->lock);
646 if (dev->owner == fp) {
647 stk_stop_stream(dev);
648 stk_free_buffers(dev);
649 stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
650 unset_initialised(dev);
654 usb_autopm_put_interface(dev->interface);
655 mutex_unlock(&dev->lock);
656 return v4l2_fh_release(fp);
659 static ssize_t stk_read(struct file *fp, char __user *buf,
660 size_t count, loff_t *f_pos)
665 struct stk_sio_buffer *sbuf;
666 struct stk_camera *dev = video_drvdata(fp);
668 if (!is_present(dev))
670 if (dev->owner && (!dev->reading || dev->owner != fp))
673 if (!is_streaming(dev)) {
674 if (stk_initialise(dev)
675 || stk_allocate_buffers(dev, 3)
676 || stk_start_stream(dev))
679 spin_lock_irqsave(&dev->spinlock, flags);
680 for (i = 0; i < dev->n_sbufs; i++) {
681 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
682 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
684 spin_unlock_irqrestore(&dev->spinlock, flags);
687 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
689 ret = wait_event_interruptible(dev->wait_frame,
690 !list_empty(&dev->sio_full) || !is_present(dev));
693 if (!is_present(dev))
696 if (count + *f_pos > dev->frame_size)
697 count = dev->frame_size - *f_pos;
698 spin_lock_irqsave(&dev->spinlock, flags);
699 if (list_empty(&dev->sio_full)) {
700 spin_unlock_irqrestore(&dev->spinlock, flags);
701 STK_ERROR("BUG: No siobufs ready\n");
704 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
705 spin_unlock_irqrestore(&dev->spinlock, flags);
707 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
712 if (*f_pos >= dev->frame_size) {
714 spin_lock_irqsave(&dev->spinlock, flags);
715 list_move_tail(&sbuf->list, &dev->sio_avail);
716 spin_unlock_irqrestore(&dev->spinlock, flags);
721 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
722 size_t count, loff_t *f_pos)
724 struct stk_camera *dev = video_drvdata(fp);
727 if (mutex_lock_interruptible(&dev->lock))
729 ret = stk_read(fp, buf, count, f_pos);
730 mutex_unlock(&dev->lock);
734 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
736 struct stk_camera *dev = video_drvdata(fp);
737 unsigned res = v4l2_ctrl_poll(fp, wait);
739 poll_wait(fp, &dev->wait_frame, wait);
741 if (!is_present(dev))
744 if (!list_empty(&dev->sio_full))
745 return res | POLLIN | POLLRDNORM;
751 static void stk_v4l_vm_open(struct vm_area_struct *vma)
753 struct stk_sio_buffer *sbuf = vma->vm_private_data;
756 static void stk_v4l_vm_close(struct vm_area_struct *vma)
758 struct stk_sio_buffer *sbuf = vma->vm_private_data;
760 if (sbuf->mapcount == 0)
761 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
763 static const struct vm_operations_struct stk_v4l_vm_ops = {
764 .open = stk_v4l_vm_open,
765 .close = stk_v4l_vm_close
768 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
772 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
773 struct stk_camera *dev = video_drvdata(fp);
774 struct stk_sio_buffer *sbuf = NULL;
776 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
779 for (i = 0; i < dev->n_sbufs; i++) {
780 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
781 sbuf = dev->sio_bufs + i;
787 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
790 vma->vm_flags |= VM_DONTEXPAND;
791 vma->vm_private_data = sbuf;
792 vma->vm_ops = &stk_v4l_vm_ops;
793 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
794 stk_v4l_vm_open(vma);
798 /* v4l ioctl handlers */
800 static int stk_vidioc_querycap(struct file *filp,
801 void *priv, struct v4l2_capability *cap)
803 struct stk_camera *dev = video_drvdata(filp);
805 strcpy(cap->driver, "stk");
806 strcpy(cap->card, "stk");
807 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
809 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
810 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
811 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
815 static int stk_vidioc_enum_input(struct file *filp,
816 void *priv, struct v4l2_input *input)
818 if (input->index != 0)
821 strcpy(input->name, "Syntek USB Camera");
822 input->type = V4L2_INPUT_TYPE_CAMERA;
827 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
833 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
835 return i ? -EINVAL : 0;
838 static int stk_s_ctrl(struct v4l2_ctrl *ctrl)
840 struct stk_camera *dev =
841 container_of(ctrl->handler, struct stk_camera, hdl);
844 case V4L2_CID_BRIGHTNESS:
845 return stk_sensor_set_brightness(dev, ctrl->val);
847 if (dmi_check_system(stk_upside_down_dmi_table))
848 dev->vsettings.hflip = !ctrl->val;
850 dev->vsettings.hflip = ctrl->val;
853 if (dmi_check_system(stk_upside_down_dmi_table))
854 dev->vsettings.vflip = !ctrl->val;
856 dev->vsettings.vflip = ctrl->val;
865 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
866 void *priv, struct v4l2_fmtdesc *fmtd)
868 switch (fmtd->index) {
870 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
871 strcpy(fmtd->description, "r5g6b5");
874 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
875 strcpy(fmtd->description, "r5g6b5BE");
878 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
879 strcpy(fmtd->description, "yuv4:2:2");
882 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
883 strcpy(fmtd->description, "Raw bayer");
886 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
887 strcpy(fmtd->description, "yuv4:2:2");
895 static struct stk_size {
900 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
901 { .w = 640, .h = 480, .m = MODE_VGA, },
902 { .w = 352, .h = 288, .m = MODE_CIF, },
903 { .w = 320, .h = 240, .m = MODE_QVGA, },
904 { .w = 176, .h = 144, .m = MODE_QCIF, },
907 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
908 void *priv, struct v4l2_format *f)
910 struct v4l2_pix_format *pix_format = &f->fmt.pix;
911 struct stk_camera *dev = video_drvdata(filp);
914 for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
915 stk_sizes[i].m != dev->vsettings.mode; i++)
917 if (i == ARRAY_SIZE(stk_sizes)) {
918 STK_ERROR("ERROR: mode invalid\n");
921 pix_format->width = stk_sizes[i].w;
922 pix_format->height = stk_sizes[i].h;
923 pix_format->field = V4L2_FIELD_NONE;
924 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
925 pix_format->pixelformat = dev->vsettings.palette;
926 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
927 pix_format->bytesperline = pix_format->width;
929 pix_format->bytesperline = 2 * pix_format->width;
930 pix_format->sizeimage = pix_format->bytesperline
931 * pix_format->height;
935 static int stk_try_fmt_vid_cap(struct file *filp,
936 struct v4l2_format *fmtd, int *idx)
939 switch (fmtd->fmt.pix.pixelformat) {
940 case V4L2_PIX_FMT_RGB565:
941 case V4L2_PIX_FMT_RGB565X:
942 case V4L2_PIX_FMT_UYVY:
943 case V4L2_PIX_FMT_YUYV:
944 case V4L2_PIX_FMT_SBGGR8:
949 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
950 if (fmtd->fmt.pix.width > stk_sizes[i].w)
953 if (i == ARRAY_SIZE(stk_sizes)
954 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
955 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
956 fmtd->fmt.pix.height = stk_sizes[i-1].h;
957 fmtd->fmt.pix.width = stk_sizes[i-1].w;
961 fmtd->fmt.pix.height = stk_sizes[i].h;
962 fmtd->fmt.pix.width = stk_sizes[i].w;
967 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
968 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
969 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
970 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
972 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
973 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
974 * fmtd->fmt.pix.height;
978 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
979 void *priv, struct v4l2_format *fmtd)
981 return stk_try_fmt_vid_cap(filp, fmtd, NULL);
984 static int stk_setup_format(struct stk_camera *dev)
988 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
992 while (i < ARRAY_SIZE(stk_sizes) &&
993 stk_sizes[i].m != dev->vsettings.mode)
995 if (i == ARRAY_SIZE(stk_sizes)) {
996 STK_ERROR("Something is broken in %s\n", __func__);
999 /* This registers controls some timings, not sure of what. */
1000 stk_camera_write_reg(dev, 0x001b, 0x0e);
1001 if (dev->vsettings.mode == MODE_SXGA)
1002 stk_camera_write_reg(dev, 0x001c, 0x0e);
1004 stk_camera_write_reg(dev, 0x001c, 0x46);
1006 * Registers 0x0115 0x0114 are the size of each line (bytes),
1007 * regs 0x0117 0x0116 are the heigth of the image.
1009 stk_camera_write_reg(dev, 0x0115,
1010 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1011 stk_camera_write_reg(dev, 0x0114,
1012 (stk_sizes[i].w * depth) & 0xff);
1013 stk_camera_write_reg(dev, 0x0117,
1014 (stk_sizes[i].h >> 8) & 0xff);
1015 stk_camera_write_reg(dev, 0x0116,
1016 stk_sizes[i].h & 0xff);
1017 return stk_sensor_configure(dev);
1020 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1021 void *priv, struct v4l2_format *fmtd)
1025 struct stk_camera *dev = video_drvdata(filp);
1029 if (!is_present(dev))
1031 if (is_streaming(dev))
1035 ret = stk_try_fmt_vid_cap(filp, fmtd, &idx);
1039 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1040 stk_free_buffers(dev);
1041 dev->frame_size = fmtd->fmt.pix.sizeimage;
1042 dev->vsettings.mode = stk_sizes[idx].m;
1044 stk_initialise(dev);
1045 return stk_setup_format(dev);
1048 static int stk_vidioc_reqbufs(struct file *filp,
1049 void *priv, struct v4l2_requestbuffers *rb)
1051 struct stk_camera *dev = video_drvdata(filp);
1055 if (rb->memory != V4L2_MEMORY_MMAP)
1057 if (is_streaming(dev)
1058 || (dev->owner && dev->owner != filp))
1060 stk_free_buffers(dev);
1061 if (rb->count == 0) {
1062 stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
1063 unset_initialised(dev);
1069 /*FIXME If they ask for zero, we must stop streaming and free */
1072 /* Arbitrary limit */
1073 else if (rb->count > 5)
1076 stk_allocate_buffers(dev, rb->count);
1077 rb->count = dev->n_sbufs;
1081 static int stk_vidioc_querybuf(struct file *filp,
1082 void *priv, struct v4l2_buffer *buf)
1084 struct stk_camera *dev = video_drvdata(filp);
1085 struct stk_sio_buffer *sbuf;
1087 if (buf->index >= dev->n_sbufs)
1089 sbuf = dev->sio_bufs + buf->index;
1090 *buf = sbuf->v4lbuf;
1094 static int stk_vidioc_qbuf(struct file *filp,
1095 void *priv, struct v4l2_buffer *buf)
1097 struct stk_camera *dev = video_drvdata(filp);
1098 struct stk_sio_buffer *sbuf;
1099 unsigned long flags;
1101 if (buf->memory != V4L2_MEMORY_MMAP)
1104 if (buf->index >= dev->n_sbufs)
1106 sbuf = dev->sio_bufs + buf->index;
1107 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1109 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1110 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1111 spin_lock_irqsave(&dev->spinlock, flags);
1112 list_add_tail(&sbuf->list, &dev->sio_avail);
1113 *buf = sbuf->v4lbuf;
1114 spin_unlock_irqrestore(&dev->spinlock, flags);
1118 static int stk_vidioc_dqbuf(struct file *filp,
1119 void *priv, struct v4l2_buffer *buf)
1121 struct stk_camera *dev = video_drvdata(filp);
1122 struct stk_sio_buffer *sbuf;
1123 unsigned long flags;
1126 if (!is_streaming(dev))
1129 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1130 return -EWOULDBLOCK;
1131 ret = wait_event_interruptible(dev->wait_frame,
1132 !list_empty(&dev->sio_full) || !is_present(dev));
1135 if (!is_present(dev))
1138 spin_lock_irqsave(&dev->spinlock, flags);
1139 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1140 list_del_init(&sbuf->list);
1141 spin_unlock_irqrestore(&dev->spinlock, flags);
1142 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1143 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1144 sbuf->v4lbuf.sequence = ++dev->sequence;
1145 v4l2_get_timestamp(&sbuf->v4lbuf.timestamp);
1147 *buf = sbuf->v4lbuf;
1151 static int stk_vidioc_streamon(struct file *filp,
1152 void *priv, enum v4l2_buf_type type)
1154 struct stk_camera *dev = video_drvdata(filp);
1155 if (is_streaming(dev))
1157 if (dev->sio_bufs == NULL)
1160 return stk_start_stream(dev);
1163 static int stk_vidioc_streamoff(struct file *filp,
1164 void *priv, enum v4l2_buf_type type)
1166 struct stk_camera *dev = video_drvdata(filp);
1167 unsigned long flags;
1169 stk_stop_stream(dev);
1170 spin_lock_irqsave(&dev->spinlock, flags);
1171 INIT_LIST_HEAD(&dev->sio_avail);
1172 INIT_LIST_HEAD(&dev->sio_full);
1173 for (i = 0; i < dev->n_sbufs; i++) {
1174 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1175 dev->sio_bufs[i].v4lbuf.flags = 0;
1177 spin_unlock_irqrestore(&dev->spinlock, flags);
1182 static int stk_vidioc_g_parm(struct file *filp,
1183 void *priv, struct v4l2_streamparm *sp)
1185 /*FIXME This is not correct */
1186 sp->parm.capture.timeperframe.numerator = 1;
1187 sp->parm.capture.timeperframe.denominator = 30;
1188 sp->parm.capture.readbuffers = 2;
1192 static int stk_vidioc_enum_framesizes(struct file *filp,
1193 void *priv, struct v4l2_frmsizeenum *frms)
1195 if (frms->index >= ARRAY_SIZE(stk_sizes))
1197 switch (frms->pixel_format) {
1198 case V4L2_PIX_FMT_RGB565:
1199 case V4L2_PIX_FMT_RGB565X:
1200 case V4L2_PIX_FMT_UYVY:
1201 case V4L2_PIX_FMT_YUYV:
1202 case V4L2_PIX_FMT_SBGGR8:
1203 frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1204 frms->discrete.width = stk_sizes[frms->index].w;
1205 frms->discrete.height = stk_sizes[frms->index].h;
1207 default: return -EINVAL;
1211 static const struct v4l2_ctrl_ops stk_ctrl_ops = {
1212 .s_ctrl = stk_s_ctrl,
1215 static struct v4l2_file_operations v4l_stk_fops = {
1216 .owner = THIS_MODULE,
1217 .open = v4l_stk_open,
1218 .release = v4l_stk_release,
1219 .read = v4l_stk_read,
1220 .poll = v4l_stk_poll,
1221 .mmap = v4l_stk_mmap,
1222 .unlocked_ioctl = video_ioctl2,
1225 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1226 .vidioc_querycap = stk_vidioc_querycap,
1227 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1228 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1229 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1230 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1231 .vidioc_enum_input = stk_vidioc_enum_input,
1232 .vidioc_s_input = stk_vidioc_s_input,
1233 .vidioc_g_input = stk_vidioc_g_input,
1234 .vidioc_reqbufs = stk_vidioc_reqbufs,
1235 .vidioc_querybuf = stk_vidioc_querybuf,
1236 .vidioc_qbuf = stk_vidioc_qbuf,
1237 .vidioc_dqbuf = stk_vidioc_dqbuf,
1238 .vidioc_streamon = stk_vidioc_streamon,
1239 .vidioc_streamoff = stk_vidioc_streamoff,
1240 .vidioc_g_parm = stk_vidioc_g_parm,
1241 .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1242 .vidioc_log_status = v4l2_ctrl_log_status,
1243 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1244 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1247 static void stk_v4l_dev_release(struct video_device *vd)
1249 struct stk_camera *dev = vdev_to_camera(vd);
1251 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1252 STK_ERROR("We are leaking memory\n");
1253 usb_put_intf(dev->interface);
1257 static struct video_device stk_v4l_data = {
1258 .name = "stkwebcam",
1259 .fops = &v4l_stk_fops,
1260 .ioctl_ops = &v4l_stk_ioctl_ops,
1261 .release = stk_v4l_dev_release,
1265 static int stk_register_video_device(struct stk_camera *dev)
1269 dev->vdev = stk_v4l_data;
1270 dev->vdev.lock = &dev->lock;
1271 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1272 video_set_drvdata(&dev->vdev, dev);
1273 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1275 STK_ERROR("v4l registration failed\n");
1277 STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1278 video_device_node_name(&dev->vdev));
1285 static int stk_camera_probe(struct usb_interface *interface,
1286 const struct usb_device_id *id)
1288 struct v4l2_ctrl_handler *hdl;
1292 struct stk_camera *dev = NULL;
1293 struct usb_device *udev = interface_to_usbdev(interface);
1294 struct usb_host_interface *iface_desc;
1295 struct usb_endpoint_descriptor *endpoint;
1297 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1299 STK_ERROR("Out of memory !\n");
1302 err = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1304 dev_err(&udev->dev, "couldn't register v4l2_device\n");
1309 v4l2_ctrl_handler_init(hdl, 3);
1310 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1311 V4L2_CID_BRIGHTNESS, 0, 0xff, 0x1, 0x60);
1312 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1313 V4L2_CID_HFLIP, 0, 1, 1, 1);
1314 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1315 V4L2_CID_VFLIP, 0, 1, 1, 1);
1318 dev_err(&udev->dev, "couldn't register control\n");
1321 dev->v4l2_dev.ctrl_handler = hdl;
1323 spin_lock_init(&dev->spinlock);
1324 mutex_init(&dev->lock);
1325 init_waitqueue_head(&dev->wait_frame);
1326 dev->first_init = 1; /* webcam LED management */
1329 dev->interface = interface;
1330 usb_get_intf(interface);
1333 dev->vsettings.hflip = hflip;
1334 else if (dmi_check_system(stk_upside_down_dmi_table))
1335 dev->vsettings.hflip = 1;
1337 dev->vsettings.hflip = 0;
1339 dev->vsettings.vflip = vflip;
1340 else if (dmi_check_system(stk_upside_down_dmi_table))
1341 dev->vsettings.vflip = 1;
1343 dev->vsettings.vflip = 0;
1347 /* Set up the endpoint information
1348 * use only the first isoc-in endpoint
1349 * for the current alternate setting */
1350 iface_desc = interface->cur_altsetting;
1352 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1353 endpoint = &iface_desc->endpoint[i].desc;
1356 && usb_endpoint_is_isoc_in(endpoint)) {
1357 /* we found an isoc in endpoint */
1358 dev->isoc_ep = usb_endpoint_num(endpoint);
1362 if (!dev->isoc_ep) {
1363 STK_ERROR("Could not find isoc-in endpoint");
1367 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1368 dev->vsettings.mode = MODE_VGA;
1369 dev->frame_size = 640 * 480 * 2;
1371 INIT_LIST_HEAD(&dev->sio_avail);
1372 INIT_LIST_HEAD(&dev->sio_full);
1374 usb_set_intfdata(interface, dev);
1376 err = stk_register_video_device(dev);
1383 usb_put_intf(interface);
1385 v4l2_ctrl_handler_free(hdl);
1386 v4l2_device_unregister(&dev->v4l2_dev);
1391 static void stk_camera_disconnect(struct usb_interface *interface)
1393 struct stk_camera *dev = usb_get_intfdata(interface);
1395 usb_set_intfdata(interface, NULL);
1398 wake_up_interruptible(&dev->wait_frame);
1400 STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1401 video_device_node_name(&dev->vdev));
1403 video_unregister_device(&dev->vdev);
1404 v4l2_ctrl_handler_free(&dev->hdl);
1405 v4l2_device_unregister(&dev->v4l2_dev);
1409 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1411 struct stk_camera *dev = usb_get_intfdata(intf);
1412 if (is_streaming(dev)) {
1413 stk_stop_stream(dev);
1414 /* yes, this is ugly */
1420 static int stk_camera_resume(struct usb_interface *intf)
1422 struct stk_camera *dev = usb_get_intfdata(intf);
1423 if (!is_initialised(dev))
1425 unset_initialised(dev);
1426 stk_initialise(dev);
1427 stk_camera_write_reg(dev, 0x0, 0x49);
1428 stk_setup_format(dev);
1429 if (is_streaming(dev))
1430 stk_start_stream(dev);
1435 static struct usb_driver stk_camera_driver = {
1436 .name = "stkwebcam",
1437 .probe = stk_camera_probe,
1438 .disconnect = stk_camera_disconnect,
1439 .id_table = stkwebcam_table,
1441 .suspend = stk_camera_suspend,
1442 .resume = stk_camera_resume,
1446 module_usb_driver(stk_camera_driver);