1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
5 * Copyright (C) 2006 Nicolas VIVIEN
6 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
8 * Some parts are inspired from cafe_ccic.c
9 * Copyright 2006-2007 Jonathan Corbet
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
20 #include <linux/dmi.h>
21 #include <linux/usb.h>
23 #include <linux/vmalloc.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/v4l2-event.h>
29 #include "stk-webcam.h"
32 static int hflip = -1;
33 module_param(hflip, int, 0444);
34 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 0");
36 static int vflip = -1;
37 module_param(vflip, int, 0444);
38 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 0");
41 module_param(debug, int, 0444);
42 MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
46 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
48 /* Some cameras have audio interfaces, we aren't interested in those */
49 static const struct usb_device_id stkwebcam_table[] = {
50 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
51 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
54 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
57 * The stk webcam laptop module is mounted upside down in some laptops :(
59 * Some background information (thanks to Hans de Goede for providing this):
61 * 1) Once upon a time the stkwebcam driver was written
63 * 2) The webcam in question was used mostly in Asus laptop models, including
64 * the laptop of the original author of the driver, and in these models, in
65 * typical Asus fashion (see the long long list for uvc cams inside v4l-utils),
66 * they mounted the webcam-module the wrong way up. So the hflip and vflip
67 * module options were given a default value of 1 (the correct value for
68 * upside down mounted models)
70 * 3) Years later I got a bug report from a user with a laptop with stkwebcam,
71 * where the module was actually mounted the right way up, and thus showed
72 * upside down under Linux. So now I was facing the choice of 2 options:
74 * a) Add a not-upside-down list to stkwebcam, which overrules the default.
76 * b) Do it like all the other drivers do, and make the default right for
77 * cams mounted the proper way and add an upside-down model list, with
78 * models where we need to flip-by-default.
80 * Despite knowing that going b) would cause a period of pain where we were
81 * building the table I opted to go for option b), since a) is just too ugly,
82 * and worse different from how every other driver does it leading to
83 * confusion in the long run. This change was made in kernel 3.6.
85 * So for any user report about upside-down images since kernel 3.6 ask them
86 * to provide the output of 'sudo dmidecode' so the laptop can be added in
89 static const struct dmi_system_id stk_upside_down_dmi_table[] = {
93 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
94 DMI_MATCH(DMI_PRODUCT_NAME, "G1")
99 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
100 DMI_MATCH(DMI_PRODUCT_NAME, "F3JC")
106 DMI_MATCH(DMI_SYS_VENDOR, "HCL Infosystems Limited"),
107 DMI_MATCH(DMI_PRODUCT_NAME, "T12Rg-H")
111 .ident = "ASUS A6VM",
113 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
114 DMI_MATCH(DMI_PRODUCT_NAME, "A6VM")
124 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
126 struct usb_device *udev = dev->udev;
129 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
131 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
143 int stk_camera_read_reg(struct stk_camera *dev, u16 index, u8 *value)
145 struct usb_device *udev = dev->udev;
149 buf = kmalloc(sizeof(u8), GFP_KERNEL);
153 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
155 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
172 static int stk_start_stream(struct stk_camera *dev)
176 u8 value_116, value_117;
179 if (!is_present(dev))
181 if (!is_memallocd(dev) || !is_initialised(dev)) {
182 pr_err("FIXME: Buffers are not allocated\n");
185 ret = usb_set_interface(dev->udev, 0, 5);
188 pr_err("usb_set_interface failed !\n");
189 if (stk_sensor_wakeup(dev))
190 pr_err("error awaking the sensor\n");
192 stk_camera_read_reg(dev, 0x0116, &value_116);
193 stk_camera_read_reg(dev, 0x0117, &value_117);
195 stk_camera_write_reg(dev, 0x0116, 0x0000);
196 stk_camera_write_reg(dev, 0x0117, 0x0000);
198 stk_camera_read_reg(dev, 0x0100, &value);
199 stk_camera_write_reg(dev, 0x0100, value | 0x80);
201 stk_camera_write_reg(dev, 0x0116, value_116);
202 stk_camera_write_reg(dev, 0x0117, value_117);
203 for (i = 0; i < MAX_ISO_BUFS; i++) {
204 if (dev->isobufs[i].urb) {
205 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
206 atomic_inc(&dev->urbs_used);
215 static int stk_stop_stream(struct stk_camera *dev)
219 if (is_present(dev)) {
220 stk_camera_read_reg(dev, 0x0100, &value);
221 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
222 if (dev->isobufs != NULL) {
223 for (i = 0; i < MAX_ISO_BUFS; i++) {
224 if (dev->isobufs[i].urb)
225 usb_kill_urb(dev->isobufs[i].urb);
228 unset_streaming(dev);
230 if (usb_set_interface(dev->udev, 0, 0))
231 pr_err("usb_set_interface failed !\n");
232 if (stk_sensor_sleep(dev))
233 pr_err("error suspending the sensor\n");
239 * This seems to be the shortest init sequence we
240 * must do in order to find the sensor
241 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
242 * is also reset. Maybe powers down it?
243 * Rest of values don't make a difference
246 static struct regval stk1125_initvals[] = {
247 /*TODO: What means this sequence? */
276 static int stk_initialise(struct stk_camera *dev)
280 if (!is_present(dev))
282 if (is_initialised(dev))
284 rv = stk1125_initvals;
285 while (rv->reg != 0xffff) {
286 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
291 if (stk_sensor_init(dev) == 0) {
292 set_initialised(dev);
298 /* *********************************************** */
300 * This function is called as an URB transfert is complete (Isochronous pipe).
301 * So, the traitement is done in interrupt time, so it has be fast, not crash,
302 * and not stall. Neat.
304 static void stk_isoc_handler(struct urb *urb)
311 unsigned char *fill = NULL;
312 unsigned char *iso_buf = NULL;
314 struct stk_camera *dev;
315 struct stk_sio_buffer *fb;
317 dev = (struct stk_camera *) urb->context;
320 pr_err("isoc_handler called with NULL device !\n");
324 if (urb->status == -ENOENT || urb->status == -ECONNRESET
325 || urb->status == -ESHUTDOWN) {
326 atomic_dec(&dev->urbs_used);
330 spin_lock_irqsave(&dev->spinlock, flags);
332 if (urb->status != -EINPROGRESS && urb->status != 0) {
333 pr_err("isoc_handler: urb->status == %d\n", urb->status);
337 if (list_empty(&dev->sio_avail)) {
338 /*FIXME Stop streaming after a while */
339 pr_err_ratelimited("isoc_handler without available buffer!\n");
342 fb = list_first_entry(&dev->sio_avail,
343 struct stk_sio_buffer, list);
344 fill = fb->buffer + fb->v4lbuf.bytesused;
346 for (i = 0; i < urb->number_of_packets; i++) {
347 if (urb->iso_frame_desc[i].status != 0) {
348 if (urb->iso_frame_desc[i].status != -EXDEV)
349 pr_err("Frame %d has error %d\n",
350 i, urb->iso_frame_desc[i].status);
353 framelen = urb->iso_frame_desc[i].actual_length;
354 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
357 continue; /* no data */
360 * we found something informational from there
361 * the isoc frames have to type of headers
362 * type1: 00 xx 00 00 or 20 xx 00 00
363 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
364 * xx is a sequencer which has never been seen over 0x3f
365 * imho data written down looks like bayer, i see similarities
366 * after every 640 bytes
368 if (*iso_buf & 0x80) {
371 /* This marks a new frame */
372 if (fb->v4lbuf.bytesused != 0
373 && fb->v4lbuf.bytesused != dev->frame_size) {
374 pr_err_ratelimited("frame %d, bytesused=%d, skipping\n",
375 i, fb->v4lbuf.bytesused);
376 fb->v4lbuf.bytesused = 0;
378 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
379 if (list_is_singular(&dev->sio_avail)) {
380 /* Always reuse the last buffer */
381 fb->v4lbuf.bytesused = 0;
384 list_move_tail(dev->sio_avail.next,
386 wake_up(&dev->wait_frame);
387 fb = list_first_entry(&dev->sio_avail,
388 struct stk_sio_buffer, list);
389 fb->v4lbuf.bytesused = 0;
398 /* Our buffer is full !!! */
399 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
400 pr_err_ratelimited("Frame buffer overflow, lost sync\n");
401 /*FIXME Do something here? */
404 spin_unlock_irqrestore(&dev->spinlock, flags);
405 memcpy(fill, iso_buf, framelen);
406 spin_lock_irqsave(&dev->spinlock, flags);
409 /* New size of our buffer */
410 fb->v4lbuf.bytesused += framelen;
414 spin_unlock_irqrestore(&dev->spinlock, flags);
415 urb->dev = dev->udev;
416 ret = usb_submit_urb(urb, GFP_ATOMIC);
418 pr_err("Error (%d) re-submitting urb in stk_isoc_handler\n",
423 /* -------------------------------------------- */
425 static int stk_prepare_iso(struct stk_camera *dev)
430 struct usb_device *udev;
437 pr_err("isobufs already allocated. Bad\n");
439 dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
441 if (dev->isobufs == NULL) {
442 pr_err("Unable to allocate iso buffers\n");
445 for (i = 0; i < MAX_ISO_BUFS; i++) {
446 if (dev->isobufs[i].data == NULL) {
447 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
449 pr_err("Failed to allocate iso buffer %d\n", i);
452 dev->isobufs[i].data = kbuf;
454 pr_err("isobuf data already allocated\n");
455 if (dev->isobufs[i].urb == NULL) {
456 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
459 dev->isobufs[i].urb = urb;
461 pr_err("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 vfree(dev->sio_bufs[i].buffer);
561 kfree(dev->sio_bufs);
562 dev->sio_bufs = NULL;
566 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
569 if (dev->sio_bufs != NULL)
570 pr_err("sio_bufs already allocated\n");
572 dev->sio_bufs = kcalloc(n_sbufs,
573 sizeof(struct stk_sio_buffer),
575 if (dev->sio_bufs == NULL)
577 for (i = 0; i < n_sbufs; i++) {
578 if (stk_setup_siobuf(dev, i))
579 return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
586 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
589 err = stk_prepare_iso(dev);
594 err = stk_prepare_sio_buffers(dev, n_sbufs);
596 stk_free_sio_buffers(dev);
602 static void stk_free_buffers(struct stk_camera *dev)
605 stk_free_sio_buffers(dev);
607 /* -------------------------------------------- */
609 /* v4l file operations */
611 static int v4l_stk_open(struct file *fp)
613 struct stk_camera *dev = video_drvdata(fp);
616 if (dev == NULL || !is_present(dev))
619 if (mutex_lock_interruptible(&dev->lock))
621 if (!dev->first_init)
622 stk_camera_write_reg(dev, 0x0, 0x24);
626 err = v4l2_fh_open(fp);
628 usb_autopm_get_interface(dev->interface);
629 mutex_unlock(&dev->lock);
633 static int v4l_stk_release(struct file *fp)
635 struct stk_camera *dev = video_drvdata(fp);
637 mutex_lock(&dev->lock);
638 if (dev->owner == fp) {
639 stk_stop_stream(dev);
640 stk_free_buffers(dev);
641 stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
642 unset_initialised(dev);
646 usb_autopm_put_interface(dev->interface);
647 mutex_unlock(&dev->lock);
648 return v4l2_fh_release(fp);
651 static ssize_t stk_read(struct file *fp, char __user *buf,
652 size_t count, loff_t *f_pos)
657 struct stk_sio_buffer *sbuf;
658 struct stk_camera *dev = video_drvdata(fp);
660 if (!is_present(dev))
662 if (dev->owner && (!dev->reading || dev->owner != fp))
665 if (!is_streaming(dev)) {
666 if (stk_initialise(dev)
667 || stk_allocate_buffers(dev, 3)
668 || stk_start_stream(dev))
671 spin_lock_irqsave(&dev->spinlock, flags);
672 for (i = 0; i < dev->n_sbufs; i++) {
673 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
674 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
676 spin_unlock_irqrestore(&dev->spinlock, flags);
679 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
681 ret = wait_event_interruptible(dev->wait_frame,
682 !list_empty(&dev->sio_full) || !is_present(dev));
685 if (!is_present(dev))
688 if (count + *f_pos > dev->frame_size)
689 count = dev->frame_size - *f_pos;
690 spin_lock_irqsave(&dev->spinlock, flags);
691 if (list_empty(&dev->sio_full)) {
692 spin_unlock_irqrestore(&dev->spinlock, flags);
693 pr_err("BUG: No siobufs ready\n");
696 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
697 spin_unlock_irqrestore(&dev->spinlock, flags);
699 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
704 if (*f_pos >= dev->frame_size) {
706 spin_lock_irqsave(&dev->spinlock, flags);
707 list_move_tail(&sbuf->list, &dev->sio_avail);
708 spin_unlock_irqrestore(&dev->spinlock, flags);
713 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
714 size_t count, loff_t *f_pos)
716 struct stk_camera *dev = video_drvdata(fp);
719 if (mutex_lock_interruptible(&dev->lock))
721 ret = stk_read(fp, buf, count, f_pos);
722 mutex_unlock(&dev->lock);
726 static __poll_t v4l_stk_poll(struct file *fp, poll_table *wait)
728 struct stk_camera *dev = video_drvdata(fp);
729 __poll_t res = v4l2_ctrl_poll(fp, wait);
731 poll_wait(fp, &dev->wait_frame, wait);
733 if (!is_present(dev))
736 if (!list_empty(&dev->sio_full))
737 return res | EPOLLIN | EPOLLRDNORM;
743 static void stk_v4l_vm_open(struct vm_area_struct *vma)
745 struct stk_sio_buffer *sbuf = vma->vm_private_data;
748 static void stk_v4l_vm_close(struct vm_area_struct *vma)
750 struct stk_sio_buffer *sbuf = vma->vm_private_data;
752 if (sbuf->mapcount == 0)
753 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
755 static const struct vm_operations_struct stk_v4l_vm_ops = {
756 .open = stk_v4l_vm_open,
757 .close = stk_v4l_vm_close
760 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
764 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
765 struct stk_camera *dev = video_drvdata(fp);
766 struct stk_sio_buffer *sbuf = NULL;
768 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
771 for (i = 0; i < dev->n_sbufs; i++) {
772 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
773 sbuf = dev->sio_bufs + i;
779 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
782 vma->vm_flags |= VM_DONTEXPAND;
783 vma->vm_private_data = sbuf;
784 vma->vm_ops = &stk_v4l_vm_ops;
785 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
786 stk_v4l_vm_open(vma);
790 /* v4l ioctl handlers */
792 static int stk_vidioc_querycap(struct file *filp,
793 void *priv, struct v4l2_capability *cap)
795 struct stk_camera *dev = video_drvdata(filp);
797 strscpy(cap->driver, "stk", sizeof(cap->driver));
798 strscpy(cap->card, "stk", sizeof(cap->card));
799 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
803 static int stk_vidioc_enum_input(struct file *filp,
804 void *priv, struct v4l2_input *input)
806 if (input->index != 0)
809 strscpy(input->name, "Syntek USB Camera", sizeof(input->name));
810 input->type = V4L2_INPUT_TYPE_CAMERA;
815 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
821 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
823 return i ? -EINVAL : 0;
826 static int stk_s_ctrl(struct v4l2_ctrl *ctrl)
828 struct stk_camera *dev =
829 container_of(ctrl->handler, struct stk_camera, hdl);
832 case V4L2_CID_BRIGHTNESS:
833 return stk_sensor_set_brightness(dev, ctrl->val);
835 if (dmi_check_system(stk_upside_down_dmi_table))
836 dev->vsettings.hflip = !ctrl->val;
838 dev->vsettings.hflip = ctrl->val;
841 if (dmi_check_system(stk_upside_down_dmi_table))
842 dev->vsettings.vflip = !ctrl->val;
844 dev->vsettings.vflip = ctrl->val;
853 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
854 void *priv, struct v4l2_fmtdesc *fmtd)
856 switch (fmtd->index) {
858 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
859 strscpy(fmtd->description, "r5g6b5", sizeof(fmtd->description));
862 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
863 strscpy(fmtd->description, "r5g6b5BE", sizeof(fmtd->description));
866 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
867 strscpy(fmtd->description, "yuv4:2:2", sizeof(fmtd->description));
870 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
871 strscpy(fmtd->description, "Raw bayer", sizeof(fmtd->description));
874 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
875 strscpy(fmtd->description, "yuv4:2:2", sizeof(fmtd->description));
883 static struct stk_size {
888 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
889 { .w = 640, .h = 480, .m = MODE_VGA, },
890 { .w = 352, .h = 288, .m = MODE_CIF, },
891 { .w = 320, .h = 240, .m = MODE_QVGA, },
892 { .w = 176, .h = 144, .m = MODE_QCIF, },
895 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
896 void *priv, struct v4l2_format *f)
898 struct v4l2_pix_format *pix_format = &f->fmt.pix;
899 struct stk_camera *dev = video_drvdata(filp);
902 for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
903 stk_sizes[i].m != dev->vsettings.mode; i++)
905 if (i == ARRAY_SIZE(stk_sizes)) {
906 pr_err("ERROR: mode invalid\n");
909 pix_format->width = stk_sizes[i].w;
910 pix_format->height = stk_sizes[i].h;
911 pix_format->field = V4L2_FIELD_NONE;
912 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
913 pix_format->pixelformat = dev->vsettings.palette;
914 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
915 pix_format->bytesperline = pix_format->width;
917 pix_format->bytesperline = 2 * pix_format->width;
918 pix_format->sizeimage = pix_format->bytesperline
919 * pix_format->height;
923 static int stk_try_fmt_vid_cap(struct file *filp,
924 struct v4l2_format *fmtd, int *idx)
927 switch (fmtd->fmt.pix.pixelformat) {
928 case V4L2_PIX_FMT_RGB565:
929 case V4L2_PIX_FMT_RGB565X:
930 case V4L2_PIX_FMT_UYVY:
931 case V4L2_PIX_FMT_YUYV:
932 case V4L2_PIX_FMT_SBGGR8:
937 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
938 if (fmtd->fmt.pix.width > stk_sizes[i].w)
941 if (i == ARRAY_SIZE(stk_sizes)
942 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
943 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
944 fmtd->fmt.pix.height = stk_sizes[i-1].h;
945 fmtd->fmt.pix.width = stk_sizes[i-1].w;
949 fmtd->fmt.pix.height = stk_sizes[i].h;
950 fmtd->fmt.pix.width = stk_sizes[i].w;
955 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
956 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
957 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
958 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
960 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
961 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
962 * fmtd->fmt.pix.height;
966 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
967 void *priv, struct v4l2_format *fmtd)
969 return stk_try_fmt_vid_cap(filp, fmtd, NULL);
972 static int stk_setup_format(struct stk_camera *dev)
976 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
980 while (i < ARRAY_SIZE(stk_sizes) &&
981 stk_sizes[i].m != dev->vsettings.mode)
983 if (i == ARRAY_SIZE(stk_sizes)) {
984 pr_err("Something is broken in %s\n", __func__);
987 /* This registers controls some timings, not sure of what. */
988 stk_camera_write_reg(dev, 0x001b, 0x0e);
989 if (dev->vsettings.mode == MODE_SXGA)
990 stk_camera_write_reg(dev, 0x001c, 0x0e);
992 stk_camera_write_reg(dev, 0x001c, 0x46);
994 * Registers 0x0115 0x0114 are the size of each line (bytes),
995 * regs 0x0117 0x0116 are the height of the image.
997 stk_camera_write_reg(dev, 0x0115,
998 ((stk_sizes[i].w * depth) >> 8) & 0xff);
999 stk_camera_write_reg(dev, 0x0114,
1000 (stk_sizes[i].w * depth) & 0xff);
1001 stk_camera_write_reg(dev, 0x0117,
1002 (stk_sizes[i].h >> 8) & 0xff);
1003 stk_camera_write_reg(dev, 0x0116,
1004 stk_sizes[i].h & 0xff);
1005 return stk_sensor_configure(dev);
1008 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1009 void *priv, struct v4l2_format *fmtd)
1013 struct stk_camera *dev = video_drvdata(filp);
1017 if (!is_present(dev))
1019 if (is_streaming(dev))
1023 ret = stk_try_fmt_vid_cap(filp, fmtd, &idx);
1027 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1028 stk_free_buffers(dev);
1029 dev->frame_size = fmtd->fmt.pix.sizeimage;
1030 dev->vsettings.mode = stk_sizes[idx].m;
1032 stk_initialise(dev);
1033 return stk_setup_format(dev);
1036 static int stk_vidioc_reqbufs(struct file *filp,
1037 void *priv, struct v4l2_requestbuffers *rb)
1039 struct stk_camera *dev = video_drvdata(filp);
1043 if (rb->memory != V4L2_MEMORY_MMAP)
1045 if (is_streaming(dev)
1046 || (dev->owner && dev->owner != filp))
1048 stk_free_buffers(dev);
1049 if (rb->count == 0) {
1050 stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
1051 unset_initialised(dev);
1057 /*FIXME If they ask for zero, we must stop streaming and free */
1060 /* Arbitrary limit */
1061 else if (rb->count > 5)
1064 stk_allocate_buffers(dev, rb->count);
1065 rb->count = dev->n_sbufs;
1069 static int stk_vidioc_querybuf(struct file *filp,
1070 void *priv, struct v4l2_buffer *buf)
1072 struct stk_camera *dev = video_drvdata(filp);
1073 struct stk_sio_buffer *sbuf;
1075 if (buf->index >= dev->n_sbufs)
1077 sbuf = dev->sio_bufs + buf->index;
1078 *buf = sbuf->v4lbuf;
1082 static int stk_vidioc_qbuf(struct file *filp,
1083 void *priv, struct v4l2_buffer *buf)
1085 struct stk_camera *dev = video_drvdata(filp);
1086 struct stk_sio_buffer *sbuf;
1087 unsigned long flags;
1089 if (buf->memory != V4L2_MEMORY_MMAP)
1092 if (buf->index >= dev->n_sbufs)
1094 sbuf = dev->sio_bufs + buf->index;
1095 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1097 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1098 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1099 spin_lock_irqsave(&dev->spinlock, flags);
1100 list_add_tail(&sbuf->list, &dev->sio_avail);
1101 *buf = sbuf->v4lbuf;
1102 spin_unlock_irqrestore(&dev->spinlock, flags);
1106 static int stk_vidioc_dqbuf(struct file *filp,
1107 void *priv, struct v4l2_buffer *buf)
1109 struct stk_camera *dev = video_drvdata(filp);
1110 struct stk_sio_buffer *sbuf;
1111 unsigned long flags;
1114 if (!is_streaming(dev))
1117 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1118 return -EWOULDBLOCK;
1119 ret = wait_event_interruptible(dev->wait_frame,
1120 !list_empty(&dev->sio_full) || !is_present(dev));
1123 if (!is_present(dev))
1126 spin_lock_irqsave(&dev->spinlock, flags);
1127 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1128 list_del_init(&sbuf->list);
1129 spin_unlock_irqrestore(&dev->spinlock, flags);
1130 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1131 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1132 sbuf->v4lbuf.sequence = ++dev->sequence;
1133 sbuf->v4lbuf.timestamp = ns_to_timeval(ktime_get_ns());
1135 *buf = sbuf->v4lbuf;
1139 static int stk_vidioc_streamon(struct file *filp,
1140 void *priv, enum v4l2_buf_type type)
1142 struct stk_camera *dev = video_drvdata(filp);
1143 if (is_streaming(dev))
1145 if (dev->sio_bufs == NULL)
1148 return stk_start_stream(dev);
1151 static int stk_vidioc_streamoff(struct file *filp,
1152 void *priv, enum v4l2_buf_type type)
1154 struct stk_camera *dev = video_drvdata(filp);
1155 unsigned long flags;
1157 stk_stop_stream(dev);
1158 spin_lock_irqsave(&dev->spinlock, flags);
1159 INIT_LIST_HEAD(&dev->sio_avail);
1160 INIT_LIST_HEAD(&dev->sio_full);
1161 for (i = 0; i < dev->n_sbufs; i++) {
1162 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1163 dev->sio_bufs[i].v4lbuf.flags = 0;
1165 spin_unlock_irqrestore(&dev->spinlock, flags);
1170 static int stk_vidioc_g_parm(struct file *filp,
1171 void *priv, struct v4l2_streamparm *sp)
1173 /*FIXME This is not correct */
1174 sp->parm.capture.timeperframe.numerator = 1;
1175 sp->parm.capture.timeperframe.denominator = 30;
1176 sp->parm.capture.readbuffers = 2;
1180 static int stk_vidioc_enum_framesizes(struct file *filp,
1181 void *priv, struct v4l2_frmsizeenum *frms)
1183 if (frms->index >= ARRAY_SIZE(stk_sizes))
1185 switch (frms->pixel_format) {
1186 case V4L2_PIX_FMT_RGB565:
1187 case V4L2_PIX_FMT_RGB565X:
1188 case V4L2_PIX_FMT_UYVY:
1189 case V4L2_PIX_FMT_YUYV:
1190 case V4L2_PIX_FMT_SBGGR8:
1191 frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1192 frms->discrete.width = stk_sizes[frms->index].w;
1193 frms->discrete.height = stk_sizes[frms->index].h;
1195 default: return -EINVAL;
1199 static const struct v4l2_ctrl_ops stk_ctrl_ops = {
1200 .s_ctrl = stk_s_ctrl,
1203 static const struct v4l2_file_operations v4l_stk_fops = {
1204 .owner = THIS_MODULE,
1205 .open = v4l_stk_open,
1206 .release = v4l_stk_release,
1207 .read = v4l_stk_read,
1208 .poll = v4l_stk_poll,
1209 .mmap = v4l_stk_mmap,
1210 .unlocked_ioctl = video_ioctl2,
1213 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1214 .vidioc_querycap = stk_vidioc_querycap,
1215 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1216 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1217 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1218 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1219 .vidioc_enum_input = stk_vidioc_enum_input,
1220 .vidioc_s_input = stk_vidioc_s_input,
1221 .vidioc_g_input = stk_vidioc_g_input,
1222 .vidioc_reqbufs = stk_vidioc_reqbufs,
1223 .vidioc_querybuf = stk_vidioc_querybuf,
1224 .vidioc_qbuf = stk_vidioc_qbuf,
1225 .vidioc_dqbuf = stk_vidioc_dqbuf,
1226 .vidioc_streamon = stk_vidioc_streamon,
1227 .vidioc_streamoff = stk_vidioc_streamoff,
1228 .vidioc_g_parm = stk_vidioc_g_parm,
1229 .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1230 .vidioc_log_status = v4l2_ctrl_log_status,
1231 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1232 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1235 static void stk_v4l_dev_release(struct video_device *vd)
1237 struct stk_camera *dev = vdev_to_camera(vd);
1239 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1240 pr_err("We are leaking memory\n");
1241 usb_put_intf(dev->interface);
1244 static const struct video_device stk_v4l_data = {
1245 .name = "stkwebcam",
1246 .fops = &v4l_stk_fops,
1247 .ioctl_ops = &v4l_stk_ioctl_ops,
1248 .release = stk_v4l_dev_release,
1252 static int stk_register_video_device(struct stk_camera *dev)
1256 dev->vdev = stk_v4l_data;
1257 dev->vdev.lock = &dev->lock;
1258 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1259 dev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1261 video_set_drvdata(&dev->vdev, dev);
1262 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1264 pr_err("v4l registration failed\n");
1266 pr_info("Syntek USB2.0 Camera is now controlling device %s\n",
1267 video_device_node_name(&dev->vdev));
1274 static int stk_camera_probe(struct usb_interface *interface,
1275 const struct usb_device_id *id)
1277 struct v4l2_ctrl_handler *hdl;
1281 struct stk_camera *dev = NULL;
1282 struct usb_device *udev = interface_to_usbdev(interface);
1283 struct usb_host_interface *iface_desc;
1284 struct usb_endpoint_descriptor *endpoint;
1286 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1288 pr_err("Out of memory !\n");
1291 err = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1293 dev_err(&udev->dev, "couldn't register v4l2_device\n");
1298 v4l2_ctrl_handler_init(hdl, 3);
1299 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1300 V4L2_CID_BRIGHTNESS, 0, 0xff, 0x1, 0x60);
1301 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1302 V4L2_CID_HFLIP, 0, 1, 1, 1);
1303 v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1304 V4L2_CID_VFLIP, 0, 1, 1, 1);
1307 dev_err(&udev->dev, "couldn't register control\n");
1310 dev->v4l2_dev.ctrl_handler = hdl;
1312 spin_lock_init(&dev->spinlock);
1313 mutex_init(&dev->lock);
1314 init_waitqueue_head(&dev->wait_frame);
1315 dev->first_init = 1; /* webcam LED management */
1318 dev->interface = interface;
1319 usb_get_intf(interface);
1322 dev->vsettings.hflip = hflip;
1323 else if (dmi_check_system(stk_upside_down_dmi_table))
1324 dev->vsettings.hflip = 1;
1326 dev->vsettings.hflip = 0;
1328 dev->vsettings.vflip = vflip;
1329 else if (dmi_check_system(stk_upside_down_dmi_table))
1330 dev->vsettings.vflip = 1;
1332 dev->vsettings.vflip = 0;
1336 /* Set up the endpoint information
1337 * use only the first isoc-in endpoint
1338 * for the current alternate setting */
1339 iface_desc = interface->cur_altsetting;
1341 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1342 endpoint = &iface_desc->endpoint[i].desc;
1345 && usb_endpoint_is_isoc_in(endpoint)) {
1346 /* we found an isoc in endpoint */
1347 dev->isoc_ep = usb_endpoint_num(endpoint);
1351 if (!dev->isoc_ep) {
1352 pr_err("Could not find isoc-in endpoint\n");
1356 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1357 dev->vsettings.mode = MODE_VGA;
1358 dev->frame_size = 640 * 480 * 2;
1360 INIT_LIST_HEAD(&dev->sio_avail);
1361 INIT_LIST_HEAD(&dev->sio_full);
1363 usb_set_intfdata(interface, dev);
1365 err = stk_register_video_device(dev);
1372 v4l2_ctrl_handler_free(hdl);
1373 v4l2_device_unregister(&dev->v4l2_dev);
1378 static void stk_camera_disconnect(struct usb_interface *interface)
1380 struct stk_camera *dev = usb_get_intfdata(interface);
1382 usb_set_intfdata(interface, NULL);
1385 wake_up_interruptible(&dev->wait_frame);
1387 pr_info("Syntek USB2.0 Camera release resources device %s\n",
1388 video_device_node_name(&dev->vdev));
1390 video_unregister_device(&dev->vdev);
1391 v4l2_ctrl_handler_free(&dev->hdl);
1392 v4l2_device_unregister(&dev->v4l2_dev);
1397 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1399 struct stk_camera *dev = usb_get_intfdata(intf);
1400 if (is_streaming(dev)) {
1401 stk_stop_stream(dev);
1402 /* yes, this is ugly */
1408 static int stk_camera_resume(struct usb_interface *intf)
1410 struct stk_camera *dev = usb_get_intfdata(intf);
1411 if (!is_initialised(dev))
1413 unset_initialised(dev);
1414 stk_initialise(dev);
1415 stk_camera_write_reg(dev, 0x0, 0x49);
1416 stk_setup_format(dev);
1417 if (is_streaming(dev))
1418 stk_start_stream(dev);
1423 static struct usb_driver stk_camera_driver = {
1424 .name = "stkwebcam",
1425 .probe = stk_camera_probe,
1426 .disconnect = stk_camera_disconnect,
1427 .id_table = stkwebcam_table,
1429 .suspend = stk_camera_suspend,
1430 .resume = stk_camera_resume,
1434 module_usb_driver(stk_camera_driver);