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")
121 int stk_camera_write_reg(struct stk_camera
*dev
, u16 index
, u8 value
)
123 struct usb_device
*udev
= dev
->udev
;
126 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
128 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
140 int stk_camera_read_reg(struct stk_camera
*dev
, u16 index
, int *value
)
142 struct usb_device
*udev
= dev
->udev
;
145 ret
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
147 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
159 static int stk_start_stream(struct stk_camera
*dev
)
163 int value_116
, value_117
;
165 if (!is_present(dev
))
167 if (!is_memallocd(dev
) || !is_initialised(dev
)) {
168 STK_ERROR("FIXME: Buffers are not allocated\n");
171 ret
= usb_set_interface(dev
->udev
, 0, 5);
174 STK_ERROR("usb_set_interface failed !\n");
175 if (stk_sensor_wakeup(dev
))
176 STK_ERROR("error awaking the sensor\n");
178 stk_camera_read_reg(dev
, 0x0116, &value_116
);
179 stk_camera_read_reg(dev
, 0x0117, &value_117
);
181 stk_camera_write_reg(dev
, 0x0116, 0x0000);
182 stk_camera_write_reg(dev
, 0x0117, 0x0000);
184 stk_camera_read_reg(dev
, 0x0100, &value
);
185 stk_camera_write_reg(dev
, 0x0100, value
| 0x80);
187 stk_camera_write_reg(dev
, 0x0116, value_116
);
188 stk_camera_write_reg(dev
, 0x0117, value_117
);
189 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
190 if (dev
->isobufs
[i
].urb
) {
191 ret
= usb_submit_urb(dev
->isobufs
[i
].urb
, GFP_KERNEL
);
192 atomic_inc(&dev
->urbs_used
);
201 static int stk_stop_stream(struct stk_camera
*dev
)
205 if (is_present(dev
)) {
206 stk_camera_read_reg(dev
, 0x0100, &value
);
207 stk_camera_write_reg(dev
, 0x0100, value
& ~0x80);
208 if (dev
->isobufs
!= NULL
) {
209 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
210 if (dev
->isobufs
[i
].urb
)
211 usb_kill_urb(dev
->isobufs
[i
].urb
);
214 unset_streaming(dev
);
216 if (usb_set_interface(dev
->udev
, 0, 0))
217 STK_ERROR("usb_set_interface failed !\n");
218 if (stk_sensor_sleep(dev
))
219 STK_ERROR("error suspending the sensor\n");
225 * This seems to be the shortest init sequence we
226 * must do in order to find the sensor
227 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
228 * is also reset. Maybe powers down it?
229 * Rest of values don't make a difference
232 static struct regval stk1125_initvals
[] = {
233 /*TODO: What means this sequence? */
262 static int stk_initialise(struct stk_camera
*dev
)
266 if (!is_present(dev
))
268 if (is_initialised(dev
))
270 rv
= stk1125_initvals
;
271 while (rv
->reg
!= 0xffff) {
272 ret
= stk_camera_write_reg(dev
, rv
->reg
, rv
->val
);
277 if (stk_sensor_init(dev
) == 0) {
278 set_initialised(dev
);
284 /* *********************************************** */
286 * This function is called as an URB transfert is complete (Isochronous pipe).
287 * So, the traitement is done in interrupt time, so it has be fast, not crash,
288 * and not stall. Neat.
290 static void stk_isoc_handler(struct urb
*urb
)
297 unsigned char *fill
= NULL
;
298 unsigned char *iso_buf
= NULL
;
300 struct stk_camera
*dev
;
301 struct stk_sio_buffer
*fb
;
303 dev
= (struct stk_camera
*) urb
->context
;
306 STK_ERROR("isoc_handler called with NULL device !\n");
310 if (urb
->status
== -ENOENT
|| urb
->status
== -ECONNRESET
311 || urb
->status
== -ESHUTDOWN
) {
312 atomic_dec(&dev
->urbs_used
);
316 spin_lock_irqsave(&dev
->spinlock
, flags
);
318 if (urb
->status
!= -EINPROGRESS
&& urb
->status
!= 0) {
319 STK_ERROR("isoc_handler: urb->status == %d\n", urb
->status
);
323 if (list_empty(&dev
->sio_avail
)) {
324 /*FIXME Stop streaming after a while */
325 (void) (printk_ratelimit() &&
326 STK_ERROR("isoc_handler without available buffer!\n"));
329 fb
= list_first_entry(&dev
->sio_avail
,
330 struct stk_sio_buffer
, list
);
331 fill
= fb
->buffer
+ fb
->v4lbuf
.bytesused
;
333 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
334 if (urb
->iso_frame_desc
[i
].status
!= 0) {
335 if (urb
->iso_frame_desc
[i
].status
!= -EXDEV
)
336 STK_ERROR("Frame %d has error %d\n", i
,
337 urb
->iso_frame_desc
[i
].status
);
340 framelen
= urb
->iso_frame_desc
[i
].actual_length
;
341 iso_buf
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
344 continue; /* no data */
347 * we found something informational from there
348 * the isoc frames have to type of headers
349 * type1: 00 xx 00 00 or 20 xx 00 00
350 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
351 * xx is a sequencer which has never been seen over 0x3f
352 * imho data written down looks like bayer, i see similarities
353 * after every 640 bytes
355 if (*iso_buf
& 0x80) {
358 /* This marks a new frame */
359 if (fb
->v4lbuf
.bytesused
!= 0
360 && fb
->v4lbuf
.bytesused
!= dev
->frame_size
) {
361 (void) (printk_ratelimit() &&
362 STK_ERROR("frame %d, "
363 "bytesused=%d, skipping\n",
364 i
, fb
->v4lbuf
.bytesused
));
365 fb
->v4lbuf
.bytesused
= 0;
367 } else if (fb
->v4lbuf
.bytesused
== dev
->frame_size
) {
368 if (list_is_singular(&dev
->sio_avail
)) {
369 /* Always reuse the last buffer */
370 fb
->v4lbuf
.bytesused
= 0;
373 list_move_tail(dev
->sio_avail
.next
,
375 wake_up(&dev
->wait_frame
);
376 fb
= list_first_entry(&dev
->sio_avail
,
377 struct stk_sio_buffer
, list
);
378 fb
->v4lbuf
.bytesused
= 0;
387 /* Our buffer is full !!! */
388 if (framelen
+ fb
->v4lbuf
.bytesused
> dev
->frame_size
) {
389 (void) (printk_ratelimit() &&
390 STK_ERROR("Frame buffer overflow, lost sync\n"));
391 /*FIXME Do something here? */
394 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
395 memcpy(fill
, iso_buf
, framelen
);
396 spin_lock_irqsave(&dev
->spinlock
, flags
);
399 /* New size of our buffer */
400 fb
->v4lbuf
.bytesused
+= framelen
;
404 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
405 urb
->dev
= dev
->udev
;
406 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
408 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
413 /* -------------------------------------------- */
415 static int stk_prepare_iso(struct stk_camera
*dev
)
420 struct usb_device
*udev
;
427 STK_ERROR("isobufs already allocated. Bad\n");
429 dev
->isobufs
= kcalloc(MAX_ISO_BUFS
, sizeof(*dev
->isobufs
),
431 if (dev
->isobufs
== NULL
) {
432 STK_ERROR("Unable to allocate iso buffers\n");
435 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
436 if (dev
->isobufs
[i
].data
== NULL
) {
437 kbuf
= kzalloc(ISO_BUFFER_SIZE
, GFP_KERNEL
);
439 STK_ERROR("Failed to allocate iso buffer %d\n",
443 dev
->isobufs
[i
].data
= kbuf
;
445 STK_ERROR("isobuf data already allocated\n");
446 if (dev
->isobufs
[i
].urb
== NULL
) {
447 urb
= usb_alloc_urb(ISO_FRAMES_PER_DESC
, GFP_KERNEL
);
449 STK_ERROR("Failed to allocate URB %d\n", i
);
452 dev
->isobufs
[i
].urb
= urb
;
454 STK_ERROR("Killing URB\n");
455 usb_kill_urb(dev
->isobufs
[i
].urb
);
456 urb
= dev
->isobufs
[i
].urb
;
460 urb
->pipe
= usb_rcvisocpipe(udev
, dev
->isoc_ep
);
461 urb
->transfer_flags
= URB_ISO_ASAP
;
462 urb
->transfer_buffer
= dev
->isobufs
[i
].data
;
463 urb
->transfer_buffer_length
= ISO_BUFFER_SIZE
;
464 urb
->complete
= stk_isoc_handler
;
466 urb
->start_frame
= 0;
467 urb
->number_of_packets
= ISO_FRAMES_PER_DESC
;
469 for (j
= 0; j
< ISO_FRAMES_PER_DESC
; j
++) {
470 urb
->iso_frame_desc
[j
].offset
= j
* ISO_MAX_FRAME_SIZE
;
471 urb
->iso_frame_desc
[j
].length
= ISO_MAX_FRAME_SIZE
;
478 for (i
= 0; i
< MAX_ISO_BUFS
&& dev
->isobufs
[i
].data
; i
++)
479 kfree(dev
->isobufs
[i
].data
);
480 for (i
= 0; i
< MAX_ISO_BUFS
&& dev
->isobufs
[i
].urb
; i
++)
481 usb_free_urb(dev
->isobufs
[i
].urb
);
487 static void stk_clean_iso(struct stk_camera
*dev
)
491 if (dev
== NULL
|| dev
->isobufs
== NULL
)
494 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
497 urb
= dev
->isobufs
[i
].urb
;
499 if (atomic_read(&dev
->urbs_used
) && is_present(dev
))
503 kfree(dev
->isobufs
[i
].data
);
507 unset_memallocd(dev
);
510 static int stk_setup_siobuf(struct stk_camera
*dev
, int index
)
512 struct stk_sio_buffer
*buf
= dev
->sio_bufs
+ index
;
513 INIT_LIST_HEAD(&buf
->list
);
514 buf
->v4lbuf
.length
= PAGE_ALIGN(dev
->frame_size
);
515 buf
->buffer
= vmalloc_user(buf
->v4lbuf
.length
);
516 if (buf
->buffer
== NULL
)
520 buf
->v4lbuf
.index
= index
;
521 buf
->v4lbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
522 buf
->v4lbuf
.flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
523 buf
->v4lbuf
.field
= V4L2_FIELD_NONE
;
524 buf
->v4lbuf
.memory
= V4L2_MEMORY_MMAP
;
525 buf
->v4lbuf
.m
.offset
= 2*index
*buf
->v4lbuf
.length
;
529 static int stk_free_sio_buffers(struct stk_camera
*dev
)
534 if (dev
->n_sbufs
== 0 || dev
->sio_bufs
== NULL
)
537 * If any buffers are mapped, we cannot free them at all.
539 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
540 if (dev
->sio_bufs
[i
].mapcount
> 0)
546 spin_lock_irqsave(&dev
->spinlock
, flags
);
547 INIT_LIST_HEAD(&dev
->sio_avail
);
548 INIT_LIST_HEAD(&dev
->sio_full
);
549 nbufs
= dev
->n_sbufs
;
551 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
552 for (i
= 0; i
< nbufs
; i
++) {
553 if (dev
->sio_bufs
[i
].buffer
!= NULL
)
554 vfree(dev
->sio_bufs
[i
].buffer
);
556 kfree(dev
->sio_bufs
);
557 dev
->sio_bufs
= NULL
;
561 static int stk_prepare_sio_buffers(struct stk_camera
*dev
, unsigned n_sbufs
)
564 if (dev
->sio_bufs
!= NULL
)
565 STK_ERROR("sio_bufs already allocated\n");
567 dev
->sio_bufs
= kzalloc(n_sbufs
* sizeof(struct stk_sio_buffer
),
569 if (dev
->sio_bufs
== NULL
)
571 for (i
= 0; i
< n_sbufs
; i
++) {
572 if (stk_setup_siobuf(dev
, i
))
573 return (dev
->n_sbufs
> 1 ? 0 : -ENOMEM
);
580 static int stk_allocate_buffers(struct stk_camera
*dev
, unsigned n_sbufs
)
583 err
= stk_prepare_iso(dev
);
588 err
= stk_prepare_sio_buffers(dev
, n_sbufs
);
590 stk_free_sio_buffers(dev
);
596 static void stk_free_buffers(struct stk_camera
*dev
)
599 stk_free_sio_buffers(dev
);
601 /* -------------------------------------------- */
603 /* v4l file operations */
605 static int v4l_stk_open(struct file
*fp
)
607 struct stk_camera
*dev
= video_drvdata(fp
);
610 if (dev
== NULL
|| !is_present(dev
))
613 if (mutex_lock_interruptible(&dev
->lock
))
615 if (!dev
->first_init
)
616 stk_camera_write_reg(dev
, 0x0, 0x24);
620 err
= v4l2_fh_open(fp
);
622 usb_autopm_get_interface(dev
->interface
);
623 mutex_unlock(&dev
->lock
);
627 static int v4l_stk_release(struct file
*fp
)
629 struct stk_camera
*dev
= video_drvdata(fp
);
631 mutex_lock(&dev
->lock
);
632 if (dev
->owner
== fp
) {
633 stk_stop_stream(dev
);
634 stk_free_buffers(dev
);
635 stk_camera_write_reg(dev
, 0x0, 0x49); /* turn off the LED */
636 unset_initialised(dev
);
641 usb_autopm_put_interface(dev
->interface
);
642 mutex_unlock(&dev
->lock
);
643 return v4l2_fh_release(fp
);
646 static ssize_t
stk_read(struct file
*fp
, char __user
*buf
,
647 size_t count
, loff_t
*f_pos
)
652 struct stk_sio_buffer
*sbuf
;
653 struct stk_camera
*dev
= video_drvdata(fp
);
655 if (!is_present(dev
))
657 if (dev
->owner
&& (!dev
->reading
|| dev
->owner
!= fp
))
660 if (!is_streaming(dev
)) {
661 if (stk_initialise(dev
)
662 || stk_allocate_buffers(dev
, 3)
663 || stk_start_stream(dev
))
666 spin_lock_irqsave(&dev
->spinlock
, flags
);
667 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
668 list_add_tail(&dev
->sio_bufs
[i
].list
, &dev
->sio_avail
);
669 dev
->sio_bufs
[i
].v4lbuf
.flags
= V4L2_BUF_FLAG_QUEUED
;
671 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
674 if (fp
->f_flags
& O_NONBLOCK
&& list_empty(&dev
->sio_full
))
676 ret
= wait_event_interruptible(dev
->wait_frame
,
677 !list_empty(&dev
->sio_full
) || !is_present(dev
));
680 if (!is_present(dev
))
683 if (count
+ *f_pos
> dev
->frame_size
)
684 count
= dev
->frame_size
- *f_pos
;
685 spin_lock_irqsave(&dev
->spinlock
, flags
);
686 if (list_empty(&dev
->sio_full
)) {
687 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
688 STK_ERROR("BUG: No siobufs ready\n");
691 sbuf
= list_first_entry(&dev
->sio_full
, struct stk_sio_buffer
, list
);
692 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
694 if (copy_to_user(buf
, sbuf
->buffer
+ *f_pos
, count
))
699 if (*f_pos
>= dev
->frame_size
) {
701 spin_lock_irqsave(&dev
->spinlock
, flags
);
702 list_move_tail(&sbuf
->list
, &dev
->sio_avail
);
703 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
708 static ssize_t
v4l_stk_read(struct file
*fp
, char __user
*buf
,
709 size_t count
, loff_t
*f_pos
)
711 struct stk_camera
*dev
= video_drvdata(fp
);
714 if (mutex_lock_interruptible(&dev
->lock
))
716 ret
= stk_read(fp
, buf
, count
, f_pos
);
717 mutex_unlock(&dev
->lock
);
721 static unsigned int v4l_stk_poll(struct file
*fp
, poll_table
*wait
)
723 struct stk_camera
*dev
= video_drvdata(fp
);
724 unsigned res
= v4l2_ctrl_poll(fp
, wait
);
726 poll_wait(fp
, &dev
->wait_frame
, wait
);
728 if (!is_present(dev
))
731 if (!list_empty(&dev
->sio_full
))
732 return res
| POLLIN
| POLLRDNORM
;
738 static void stk_v4l_vm_open(struct vm_area_struct
*vma
)
740 struct stk_sio_buffer
*sbuf
= vma
->vm_private_data
;
743 static void stk_v4l_vm_close(struct vm_area_struct
*vma
)
745 struct stk_sio_buffer
*sbuf
= vma
->vm_private_data
;
747 if (sbuf
->mapcount
== 0)
748 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
750 static const struct vm_operations_struct stk_v4l_vm_ops
= {
751 .open
= stk_v4l_vm_open
,
752 .close
= stk_v4l_vm_close
755 static int v4l_stk_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
759 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
760 struct stk_camera
*dev
= video_drvdata(fp
);
761 struct stk_sio_buffer
*sbuf
= NULL
;
763 if (!(vma
->vm_flags
& VM_WRITE
) || !(vma
->vm_flags
& VM_SHARED
))
766 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
767 if (dev
->sio_bufs
[i
].v4lbuf
.m
.offset
== offset
) {
768 sbuf
= dev
->sio_bufs
+ i
;
774 ret
= remap_vmalloc_range(vma
, sbuf
->buffer
, 0);
777 vma
->vm_flags
|= VM_DONTEXPAND
;
778 vma
->vm_private_data
= sbuf
;
779 vma
->vm_ops
= &stk_v4l_vm_ops
;
780 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
781 stk_v4l_vm_open(vma
);
785 /* v4l ioctl handlers */
787 static int stk_vidioc_querycap(struct file
*filp
,
788 void *priv
, struct v4l2_capability
*cap
)
790 struct stk_camera
*dev
= video_drvdata(filp
);
792 strcpy(cap
->driver
, "stk");
793 strcpy(cap
->card
, "stk");
794 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
796 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
797 | V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
798 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
802 static int stk_vidioc_enum_input(struct file
*filp
,
803 void *priv
, struct v4l2_input
*input
)
805 if (input
->index
!= 0)
808 strcpy(input
->name
, "Syntek USB Camera");
809 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
814 static int stk_vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
820 static int stk_vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
822 return i
? -EINVAL
: 0;
825 static int stk_s_ctrl(struct v4l2_ctrl
*ctrl
)
827 struct stk_camera
*dev
=
828 container_of(ctrl
->handler
, struct stk_camera
, hdl
);
831 case V4L2_CID_BRIGHTNESS
:
832 return stk_sensor_set_brightness(dev
, ctrl
->val
);
834 if (dmi_check_system(stk_upside_down_dmi_table
))
835 dev
->vsettings
.hflip
= !ctrl
->val
;
837 dev
->vsettings
.hflip
= ctrl
->val
;
840 if (dmi_check_system(stk_upside_down_dmi_table
))
841 dev
->vsettings
.vflip
= !ctrl
->val
;
843 dev
->vsettings
.vflip
= ctrl
->val
;
852 static int stk_vidioc_enum_fmt_vid_cap(struct file
*filp
,
853 void *priv
, struct v4l2_fmtdesc
*fmtd
)
855 switch (fmtd
->index
) {
857 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB565
;
858 strcpy(fmtd
->description
, "r5g6b5");
861 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB565X
;
862 strcpy(fmtd
->description
, "r5g6b5BE");
865 fmtd
->pixelformat
= V4L2_PIX_FMT_UYVY
;
866 strcpy(fmtd
->description
, "yuv4:2:2");
869 fmtd
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
870 strcpy(fmtd
->description
, "Raw bayer");
873 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
874 strcpy(fmtd
->description
, "yuv4:2:2");
882 static struct stk_size
{
887 { .w
= 1280, .h
= 1024, .m
= MODE_SXGA
, },
888 { .w
= 640, .h
= 480, .m
= MODE_VGA
, },
889 { .w
= 352, .h
= 288, .m
= MODE_CIF
, },
890 { .w
= 320, .h
= 240, .m
= MODE_QVGA
, },
891 { .w
= 176, .h
= 144, .m
= MODE_QCIF
, },
894 static int stk_vidioc_g_fmt_vid_cap(struct file
*filp
,
895 void *priv
, struct v4l2_format
*f
)
897 struct v4l2_pix_format
*pix_format
= &f
->fmt
.pix
;
898 struct stk_camera
*dev
= video_drvdata(filp
);
901 for (i
= 0; i
< ARRAY_SIZE(stk_sizes
) &&
902 stk_sizes
[i
].m
!= dev
->vsettings
.mode
; i
++)
904 if (i
== ARRAY_SIZE(stk_sizes
)) {
905 STK_ERROR("ERROR: mode invalid\n");
908 pix_format
->width
= stk_sizes
[i
].w
;
909 pix_format
->height
= stk_sizes
[i
].h
;
910 pix_format
->field
= V4L2_FIELD_NONE
;
911 pix_format
->colorspace
= V4L2_COLORSPACE_SRGB
;
912 pix_format
->pixelformat
= dev
->vsettings
.palette
;
913 if (dev
->vsettings
.palette
== V4L2_PIX_FMT_SBGGR8
)
914 pix_format
->bytesperline
= pix_format
->width
;
916 pix_format
->bytesperline
= 2 * pix_format
->width
;
917 pix_format
->sizeimage
= pix_format
->bytesperline
918 * pix_format
->height
;
919 pix_format
->priv
= 0;
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
;
963 fmtd
->fmt
.pix
.priv
= 0;
967 static int stk_vidioc_try_fmt_vid_cap(struct file
*filp
,
968 void *priv
, struct v4l2_format
*fmtd
)
970 return stk_try_fmt_vid_cap(filp
, fmtd
, NULL
);
973 static int stk_setup_format(struct stk_camera
*dev
)
977 if (dev
->vsettings
.palette
== V4L2_PIX_FMT_SBGGR8
)
981 while (i
< ARRAY_SIZE(stk_sizes
) &&
982 stk_sizes
[i
].m
!= dev
->vsettings
.mode
)
984 if (i
== ARRAY_SIZE(stk_sizes
)) {
985 STK_ERROR("Something is broken in %s\n", __func__
);
988 /* This registers controls some timings, not sure of what. */
989 stk_camera_write_reg(dev
, 0x001b, 0x0e);
990 if (dev
->vsettings
.mode
== MODE_SXGA
)
991 stk_camera_write_reg(dev
, 0x001c, 0x0e);
993 stk_camera_write_reg(dev
, 0x001c, 0x46);
995 * Registers 0x0115 0x0114 are the size of each line (bytes),
996 * regs 0x0117 0x0116 are the heigth of the image.
998 stk_camera_write_reg(dev
, 0x0115,
999 ((stk_sizes
[i
].w
* depth
) >> 8) & 0xff);
1000 stk_camera_write_reg(dev
, 0x0114,
1001 (stk_sizes
[i
].w
* depth
) & 0xff);
1002 stk_camera_write_reg(dev
, 0x0117,
1003 (stk_sizes
[i
].h
>> 8) & 0xff);
1004 stk_camera_write_reg(dev
, 0x0116,
1005 stk_sizes
[i
].h
& 0xff);
1006 return stk_sensor_configure(dev
);
1009 static int stk_vidioc_s_fmt_vid_cap(struct file
*filp
,
1010 void *priv
, struct v4l2_format
*fmtd
)
1014 struct stk_camera
*dev
= video_drvdata(filp
);
1018 if (!is_present(dev
))
1020 if (is_streaming(dev
))
1024 ret
= stk_try_fmt_vid_cap(filp
, fmtd
, &idx
);
1028 dev
->vsettings
.palette
= fmtd
->fmt
.pix
.pixelformat
;
1029 stk_free_buffers(dev
);
1030 dev
->frame_size
= fmtd
->fmt
.pix
.sizeimage
;
1031 dev
->vsettings
.mode
= stk_sizes
[idx
].m
;
1033 stk_initialise(dev
);
1034 return stk_setup_format(dev
);
1037 static int stk_vidioc_reqbufs(struct file
*filp
,
1038 void *priv
, struct v4l2_requestbuffers
*rb
)
1040 struct stk_camera
*dev
= video_drvdata(filp
);
1044 if (rb
->memory
!= V4L2_MEMORY_MMAP
)
1046 if (is_streaming(dev
)
1047 || (dev
->owner
&& dev
->owner
!= filp
))
1049 stk_free_buffers(dev
);
1050 if (rb
->count
== 0) {
1051 stk_camera_write_reg(dev
, 0x0, 0x49); /* turn off the LED */
1052 unset_initialised(dev
);
1058 /*FIXME If they ask for zero, we must stop streaming and free */
1061 /* Arbitrary limit */
1062 else if (rb
->count
> 5)
1065 stk_allocate_buffers(dev
, rb
->count
);
1066 rb
->count
= dev
->n_sbufs
;
1070 static int stk_vidioc_querybuf(struct file
*filp
,
1071 void *priv
, struct v4l2_buffer
*buf
)
1073 struct stk_camera
*dev
= video_drvdata(filp
);
1074 struct stk_sio_buffer
*sbuf
;
1076 if (buf
->index
>= dev
->n_sbufs
)
1078 sbuf
= dev
->sio_bufs
+ buf
->index
;
1079 *buf
= sbuf
->v4lbuf
;
1083 static int stk_vidioc_qbuf(struct file
*filp
,
1084 void *priv
, struct v4l2_buffer
*buf
)
1086 struct stk_camera
*dev
= video_drvdata(filp
);
1087 struct stk_sio_buffer
*sbuf
;
1088 unsigned long flags
;
1090 if (buf
->memory
!= V4L2_MEMORY_MMAP
)
1093 if (buf
->index
>= dev
->n_sbufs
)
1095 sbuf
= dev
->sio_bufs
+ buf
->index
;
1096 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_QUEUED
)
1098 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1099 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1100 spin_lock_irqsave(&dev
->spinlock
, flags
);
1101 list_add_tail(&sbuf
->list
, &dev
->sio_avail
);
1102 *buf
= sbuf
->v4lbuf
;
1103 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1107 static int stk_vidioc_dqbuf(struct file
*filp
,
1108 void *priv
, struct v4l2_buffer
*buf
)
1110 struct stk_camera
*dev
= video_drvdata(filp
);
1111 struct stk_sio_buffer
*sbuf
;
1112 unsigned long flags
;
1115 if (!is_streaming(dev
))
1118 if (filp
->f_flags
& O_NONBLOCK
&& list_empty(&dev
->sio_full
))
1119 return -EWOULDBLOCK
;
1120 ret
= wait_event_interruptible(dev
->wait_frame
,
1121 !list_empty(&dev
->sio_full
) || !is_present(dev
));
1124 if (!is_present(dev
))
1127 spin_lock_irqsave(&dev
->spinlock
, flags
);
1128 sbuf
= list_first_entry(&dev
->sio_full
, struct stk_sio_buffer
, list
);
1129 list_del_init(&sbuf
->list
);
1130 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1131 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
1132 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_DONE
;
1133 sbuf
->v4lbuf
.sequence
= ++dev
->sequence
;
1134 v4l2_get_timestamp(&sbuf
->v4lbuf
.timestamp
);
1136 *buf
= sbuf
->v4lbuf
;
1140 static int stk_vidioc_streamon(struct file
*filp
,
1141 void *priv
, enum v4l2_buf_type type
)
1143 struct stk_camera
*dev
= video_drvdata(filp
);
1144 if (is_streaming(dev
))
1146 if (dev
->sio_bufs
== NULL
)
1149 return stk_start_stream(dev
);
1152 static int stk_vidioc_streamoff(struct file
*filp
,
1153 void *priv
, enum v4l2_buf_type type
)
1155 struct stk_camera
*dev
= video_drvdata(filp
);
1156 unsigned long flags
;
1158 stk_stop_stream(dev
);
1159 spin_lock_irqsave(&dev
->spinlock
, flags
);
1160 INIT_LIST_HEAD(&dev
->sio_avail
);
1161 INIT_LIST_HEAD(&dev
->sio_full
);
1162 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
1163 INIT_LIST_HEAD(&dev
->sio_bufs
[i
].list
);
1164 dev
->sio_bufs
[i
].v4lbuf
.flags
= 0;
1166 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1171 static int stk_vidioc_g_parm(struct file
*filp
,
1172 void *priv
, struct v4l2_streamparm
*sp
)
1174 /*FIXME This is not correct */
1175 sp
->parm
.capture
.timeperframe
.numerator
= 1;
1176 sp
->parm
.capture
.timeperframe
.denominator
= 30;
1177 sp
->parm
.capture
.readbuffers
= 2;
1181 static int stk_vidioc_enum_framesizes(struct file
*filp
,
1182 void *priv
, struct v4l2_frmsizeenum
*frms
)
1184 if (frms
->index
>= ARRAY_SIZE(stk_sizes
))
1186 switch (frms
->pixel_format
) {
1187 case V4L2_PIX_FMT_RGB565
:
1188 case V4L2_PIX_FMT_RGB565X
:
1189 case V4L2_PIX_FMT_UYVY
:
1190 case V4L2_PIX_FMT_YUYV
:
1191 case V4L2_PIX_FMT_SBGGR8
:
1192 frms
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1193 frms
->discrete
.width
= stk_sizes
[frms
->index
].w
;
1194 frms
->discrete
.height
= stk_sizes
[frms
->index
].h
;
1196 default: return -EINVAL
;
1200 static const struct v4l2_ctrl_ops stk_ctrl_ops
= {
1201 .s_ctrl
= stk_s_ctrl
,
1204 static struct v4l2_file_operations v4l_stk_fops
= {
1205 .owner
= THIS_MODULE
,
1206 .open
= v4l_stk_open
,
1207 .release
= v4l_stk_release
,
1208 .read
= v4l_stk_read
,
1209 .poll
= v4l_stk_poll
,
1210 .mmap
= v4l_stk_mmap
,
1211 .unlocked_ioctl
= video_ioctl2
,
1214 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops
= {
1215 .vidioc_querycap
= stk_vidioc_querycap
,
1216 .vidioc_enum_fmt_vid_cap
= stk_vidioc_enum_fmt_vid_cap
,
1217 .vidioc_try_fmt_vid_cap
= stk_vidioc_try_fmt_vid_cap
,
1218 .vidioc_s_fmt_vid_cap
= stk_vidioc_s_fmt_vid_cap
,
1219 .vidioc_g_fmt_vid_cap
= stk_vidioc_g_fmt_vid_cap
,
1220 .vidioc_enum_input
= stk_vidioc_enum_input
,
1221 .vidioc_s_input
= stk_vidioc_s_input
,
1222 .vidioc_g_input
= stk_vidioc_g_input
,
1223 .vidioc_reqbufs
= stk_vidioc_reqbufs
,
1224 .vidioc_querybuf
= stk_vidioc_querybuf
,
1225 .vidioc_qbuf
= stk_vidioc_qbuf
,
1226 .vidioc_dqbuf
= stk_vidioc_dqbuf
,
1227 .vidioc_streamon
= stk_vidioc_streamon
,
1228 .vidioc_streamoff
= stk_vidioc_streamoff
,
1229 .vidioc_g_parm
= stk_vidioc_g_parm
,
1230 .vidioc_enum_framesizes
= stk_vidioc_enum_framesizes
,
1231 .vidioc_log_status
= v4l2_ctrl_log_status
,
1232 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1233 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1236 static void stk_v4l_dev_release(struct video_device
*vd
)
1238 struct stk_camera
*dev
= vdev_to_camera(vd
);
1240 if (dev
->sio_bufs
!= NULL
|| dev
->isobufs
!= NULL
)
1241 STK_ERROR("We are leaking memory\n");
1242 usb_put_intf(dev
->interface
);
1246 static struct video_device stk_v4l_data
= {
1247 .name
= "stkwebcam",
1248 .fops
= &v4l_stk_fops
,
1249 .ioctl_ops
= &v4l_stk_ioctl_ops
,
1250 .release
= stk_v4l_dev_release
,
1254 static int stk_register_video_device(struct stk_camera
*dev
)
1258 dev
->vdev
= stk_v4l_data
;
1259 dev
->vdev
.lock
= &dev
->lock
;
1260 dev
->vdev
.debug
= debug
;
1261 dev
->vdev
.v4l2_dev
= &dev
->v4l2_dev
;
1262 set_bit(V4L2_FL_USE_FH_PRIO
, &dev
->vdev
.flags
);
1263 video_set_drvdata(&dev
->vdev
, dev
);
1264 err
= video_register_device(&dev
->vdev
, VFL_TYPE_GRABBER
, -1);
1266 STK_ERROR("v4l registration failed\n");
1268 STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1269 video_device_node_name(&dev
->vdev
));
1276 static int stk_camera_probe(struct usb_interface
*interface
,
1277 const struct usb_device_id
*id
)
1279 struct v4l2_ctrl_handler
*hdl
;
1283 struct stk_camera
*dev
= NULL
;
1284 struct usb_device
*udev
= interface_to_usbdev(interface
);
1285 struct usb_host_interface
*iface_desc
;
1286 struct usb_endpoint_descriptor
*endpoint
;
1288 dev
= kzalloc(sizeof(struct stk_camera
), GFP_KERNEL
);
1290 STK_ERROR("Out of memory !\n");
1293 err
= v4l2_device_register(&interface
->dev
, &dev
->v4l2_dev
);
1295 dev_err(&udev
->dev
, "couldn't register v4l2_device\n");
1300 v4l2_ctrl_handler_init(hdl
, 3);
1301 v4l2_ctrl_new_std(hdl
, &stk_ctrl_ops
,
1302 V4L2_CID_BRIGHTNESS
, 0, 0xff, 0x1, 0x60);
1303 v4l2_ctrl_new_std(hdl
, &stk_ctrl_ops
,
1304 V4L2_CID_HFLIP
, 0, 1, 1, 1);
1305 v4l2_ctrl_new_std(hdl
, &stk_ctrl_ops
,
1306 V4L2_CID_VFLIP
, 0, 1, 1, 1);
1309 dev_err(&udev
->dev
, "couldn't register control\n");
1312 dev
->v4l2_dev
.ctrl_handler
= hdl
;
1314 spin_lock_init(&dev
->spinlock
);
1315 mutex_init(&dev
->lock
);
1316 init_waitqueue_head(&dev
->wait_frame
);
1317 dev
->first_init
= 1; /* webcam LED management */
1320 dev
->interface
= interface
;
1321 usb_get_intf(interface
);
1324 dev
->vsettings
.hflip
= hflip
;
1325 else if (dmi_check_system(stk_upside_down_dmi_table
))
1326 dev
->vsettings
.hflip
= 1;
1328 dev
->vsettings
.hflip
= 0;
1330 dev
->vsettings
.vflip
= vflip
;
1331 else if (dmi_check_system(stk_upside_down_dmi_table
))
1332 dev
->vsettings
.vflip
= 1;
1334 dev
->vsettings
.vflip
= 0;
1338 /* Set up the endpoint information
1339 * use only the first isoc-in endpoint
1340 * for the current alternate setting */
1341 iface_desc
= interface
->cur_altsetting
;
1343 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1344 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1347 && usb_endpoint_is_isoc_in(endpoint
)) {
1348 /* we found an isoc in endpoint */
1349 dev
->isoc_ep
= usb_endpoint_num(endpoint
);
1353 if (!dev
->isoc_ep
) {
1354 STK_ERROR("Could not find isoc-in endpoint");
1358 dev
->vsettings
.palette
= V4L2_PIX_FMT_RGB565
;
1359 dev
->vsettings
.mode
= MODE_VGA
;
1360 dev
->frame_size
= 640 * 480 * 2;
1362 INIT_LIST_HEAD(&dev
->sio_avail
);
1363 INIT_LIST_HEAD(&dev
->sio_full
);
1365 usb_set_intfdata(interface
, dev
);
1367 err
= stk_register_video_device(dev
);
1374 v4l2_ctrl_handler_free(hdl
);
1375 v4l2_device_unregister(&dev
->v4l2_dev
);
1380 static void stk_camera_disconnect(struct usb_interface
*interface
)
1382 struct stk_camera
*dev
= usb_get_intfdata(interface
);
1384 usb_set_intfdata(interface
, NULL
);
1387 wake_up_interruptible(&dev
->wait_frame
);
1389 STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1390 video_device_node_name(&dev
->vdev
));
1392 video_unregister_device(&dev
->vdev
);
1393 v4l2_ctrl_handler_free(&dev
->hdl
);
1394 v4l2_device_unregister(&dev
->v4l2_dev
);
1398 static int stk_camera_suspend(struct usb_interface
*intf
, pm_message_t message
)
1400 struct stk_camera
*dev
= usb_get_intfdata(intf
);
1401 if (is_streaming(dev
)) {
1402 stk_stop_stream(dev
);
1403 /* yes, this is ugly */
1409 static int stk_camera_resume(struct usb_interface
*intf
)
1411 struct stk_camera
*dev
= usb_get_intfdata(intf
);
1412 if (!is_initialised(dev
))
1414 unset_initialised(dev
);
1415 stk_initialise(dev
);
1416 stk_camera_write_reg(dev
, 0x0, 0x49);
1417 stk_setup_format(dev
);
1418 if (is_streaming(dev
))
1419 stk_start_stream(dev
);
1424 static struct usb_driver stk_camera_driver
= {
1425 .name
= "stkwebcam",
1426 .probe
= stk_camera_probe
,
1427 .disconnect
= stk_camera_disconnect
,
1428 .id_table
= stkwebcam_table
,
1430 .suspend
= stk_camera_suspend
,
1431 .resume
= stk_camera_resume
,
1435 module_usb_driver(stk_camera_driver
);