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>
30 #include <linux/kref.h>
32 #include <linux/usb.h>
33 #include <linux/vmalloc.h>
34 #include <linux/videodev2.h>
35 #include <media/v4l2-common.h>
37 #include "stk-webcam.h"
41 module_param(hflip
, bool, 0444);
42 MODULE_PARM_DESC(hflip
, "Horizontal image flip (mirror). Defaults to 1");
45 module_param(vflip
, bool, 0444);
46 MODULE_PARM_DESC(vflip
, "Vertical image flip. Defaults to 1");
49 module_param(debug
, int, 0444);
50 MODULE_PARM_DESC(debug
, "Debug v4l ioctls. Defaults to 0");
52 MODULE_LICENSE("GPL");
53 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
54 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
58 /* Some cameras have audio interfaces, we aren't interested in those */
59 static struct usb_device_id stkwebcam_table
[] = {
60 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
61 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
64 MODULE_DEVICE_TABLE(usb
, stkwebcam_table
);
66 static void stk_camera_cleanup(struct kref
*kref
)
68 struct stk_camera
*dev
= to_stk_camera(kref
);
70 STK_INFO("Syntek USB2.0 Camera release resources"
71 " video device /dev/video%d\n", dev
->vdev
.minor
);
72 video_unregister_device(&dev
->vdev
);
73 dev
->vdev
.priv
= NULL
;
75 if (dev
->sio_bufs
!= NULL
|| dev
->isobufs
!= NULL
)
76 STK_ERROR("We are leaking memory\n");
77 usb_put_intf(dev
->interface
);
85 int stk_camera_write_reg(struct stk_camera
*dev
, u16 index
, u8 value
)
87 struct usb_device
*udev
= dev
->udev
;
90 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
92 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
104 int stk_camera_read_reg(struct stk_camera
*dev
, u16 index
, int *value
)
106 struct usb_device
*udev
= dev
->udev
;
109 ret
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
111 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
123 static int stk_start_stream(struct stk_camera
*dev
)
127 int value_116
, value_117
;
129 if (!is_present(dev
))
131 if (!is_memallocd(dev
) || !is_initialised(dev
)) {
132 STK_ERROR("FIXME: Buffers are not allocated\n");
135 ret
= usb_set_interface(dev
->udev
, 0, 5);
138 STK_ERROR("usb_set_interface failed !\n");
139 if (stk_sensor_wakeup(dev
))
140 STK_ERROR("error awaking the sensor\n");
142 stk_camera_read_reg(dev
, 0x0116, &value_116
);
143 stk_camera_read_reg(dev
, 0x0117, &value_117
);
145 stk_camera_write_reg(dev
, 0x0116, 0x0000);
146 stk_camera_write_reg(dev
, 0x0117, 0x0000);
148 stk_camera_read_reg(dev
, 0x0100, &value
);
149 stk_camera_write_reg(dev
, 0x0100, value
| 0x80);
151 stk_camera_write_reg(dev
, 0x0116, value_116
);
152 stk_camera_write_reg(dev
, 0x0117, value_117
);
153 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
154 if (dev
->isobufs
[i
].urb
) {
155 ret
= usb_submit_urb(dev
->isobufs
[i
].urb
, GFP_KERNEL
);
156 atomic_inc(&dev
->urbs_used
);
165 static int stk_stop_stream(struct stk_camera
*dev
)
169 if (is_present(dev
)) {
170 stk_camera_read_reg(dev
, 0x0100, &value
);
171 stk_camera_write_reg(dev
, 0x0100, value
& ~0x80);
172 if (dev
->isobufs
!= NULL
) {
173 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
174 if (dev
->isobufs
[i
].urb
)
175 usb_kill_urb(dev
->isobufs
[i
].urb
);
178 unset_streaming(dev
);
180 if (usb_set_interface(dev
->udev
, 0, 0))
181 STK_ERROR("usb_set_interface failed !\n");
182 if (stk_sensor_sleep(dev
))
183 STK_ERROR("error suspending the sensor\n");
189 * This seems to be the shortest init sequence we
190 * must do in order to find the sensor
191 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
192 * is also reset. Maybe powers down it?
193 * Rest of values don't make a difference
196 static struct regval stk1125_initvals
[] = {
197 /*TODO: What means this sequence? */
226 static int stk_initialise(struct stk_camera
*dev
)
230 if (!is_present(dev
))
232 if (is_initialised(dev
))
234 rv
= stk1125_initvals
;
235 while (rv
->reg
!= 0xffff) {
236 ret
= stk_camera_write_reg(dev
, rv
->reg
, rv
->val
);
241 if (stk_sensor_init(dev
) == 0) {
242 set_initialised(dev
);
248 /* sysfs functions */
249 /*FIXME cleanup this */
251 static ssize_t
show_brightness(struct device
*class,
252 struct device_attribute
*attr
, char *buf
)
254 struct video_device
*vdev
= to_video_device(class);
255 struct stk_camera
*dev
= vdev_to_camera(vdev
);
257 return sprintf(buf
, "%X\n", dev
->vsettings
.brightness
);
260 static ssize_t
store_brightness(struct device
*class,
261 struct device_attribute
*attr
, const char *buf
, size_t count
)
267 struct video_device
*vdev
= to_video_device(class);
268 struct stk_camera
*dev
= vdev_to_camera(vdev
);
270 value
= simple_strtoul(buf
, &endp
, 16);
272 dev
->vsettings
.brightness
= (int) value
;
274 ret
= stk_sensor_set_brightness(dev
, value
>> 8);
281 static ssize_t
show_hflip(struct device
*class,
282 struct device_attribute
*attr
, char *buf
)
284 struct video_device
*vdev
= to_video_device(class);
285 struct stk_camera
*dev
= vdev_to_camera(vdev
);
287 return sprintf(buf
, "%d\n", dev
->vsettings
.hflip
);
290 static ssize_t
store_hflip(struct device
*class,
291 struct device_attribute
*attr
, const char *buf
, size_t count
)
293 struct video_device
*vdev
= to_video_device(class);
294 struct stk_camera
*dev
= vdev_to_camera(vdev
);
296 if (strncmp(buf
, "1", 1) == 0)
297 dev
->vsettings
.hflip
= 1;
298 else if (strncmp(buf
, "0", 1) == 0)
299 dev
->vsettings
.hflip
= 0;
306 static ssize_t
show_vflip(struct device
*class,
307 struct device_attribute
*attr
, char *buf
)
309 struct video_device
*vdev
= to_video_device(class);
310 struct stk_camera
*dev
= vdev_to_camera(vdev
);
312 return sprintf(buf
, "%d\n", dev
->vsettings
.vflip
);
315 static ssize_t
store_vflip(struct device
*class,
316 struct device_attribute
*attr
, const char *buf
, size_t count
)
318 struct video_device
*vdev
= to_video_device(class);
319 struct stk_camera
*dev
= vdev_to_camera(vdev
);
321 if (strncmp(buf
, "1", 1) == 0)
322 dev
->vsettings
.vflip
= 1;
323 else if (strncmp(buf
, "0", 1) == 0)
324 dev
->vsettings
.vflip
= 0;
331 static DEVICE_ATTR(brightness
, S_IRUGO
| S_IWUGO
,
332 show_brightness
, store_brightness
);
333 static DEVICE_ATTR(hflip
, S_IRUGO
| S_IWUGO
, show_hflip
, store_hflip
);
334 static DEVICE_ATTR(vflip
, S_IRUGO
| S_IWUGO
, show_vflip
, store_vflip
);
336 static int stk_create_sysfs_files(struct video_device
*vdev
)
340 ret
= video_device_create_file(vdev
, &dev_attr_brightness
);
341 ret
+= video_device_create_file(vdev
, &dev_attr_hflip
);
342 ret
+= video_device_create_file(vdev
, &dev_attr_vflip
);
346 static void stk_remove_sysfs_files(struct video_device
*vdev
)
348 video_device_remove_file(vdev
, &dev_attr_brightness
);
349 video_device_remove_file(vdev
, &dev_attr_hflip
);
350 video_device_remove_file(vdev
, &dev_attr_vflip
);
354 /* *********************************************** */
356 * This function is called as an URB transfert is complete (Isochronous pipe).
357 * So, the traitement is done in interrupt time, so it has be fast, not crash,
358 * and not stall. Neat.
360 static void stk_isoc_handler(struct urb
*urb
)
367 unsigned char *fill
= NULL
;
368 unsigned char *iso_buf
= NULL
;
370 struct stk_camera
*dev
;
371 struct stk_sio_buffer
*fb
;
373 dev
= (struct stk_camera
*) urb
->context
;
376 STK_ERROR("isoc_handler called with NULL device !\n");
380 if (urb
->status
== -ENOENT
|| urb
->status
== -ECONNRESET
381 || urb
->status
== -ESHUTDOWN
) {
382 atomic_dec(&dev
->urbs_used
);
386 spin_lock_irqsave(&dev
->spinlock
, flags
);
388 if (urb
->status
!= -EINPROGRESS
&& urb
->status
!= 0) {
389 STK_ERROR("isoc_handler: urb->status == %d\n", urb
->status
);
393 if (list_empty(&dev
->sio_avail
)) {
394 /*FIXME Stop streaming after a while */
395 (void) (printk_ratelimit() &&
396 STK_ERROR("isoc_handler without available buffer!\n"));
399 fb
= list_first_entry(&dev
->sio_avail
,
400 struct stk_sio_buffer
, list
);
401 fill
= fb
->buffer
+ fb
->v4lbuf
.bytesused
;
403 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
404 if (urb
->iso_frame_desc
[i
].status
!= 0) {
405 if (urb
->iso_frame_desc
[i
].status
!= -EXDEV
)
406 STK_ERROR("Frame %d has error %d\n", i
,
407 urb
->iso_frame_desc
[i
].status
);
410 framelen
= urb
->iso_frame_desc
[i
].actual_length
;
411 iso_buf
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
414 continue; /* no data */
417 * we found something informational from there
418 * the isoc frames have to type of headers
419 * type1: 00 xx 00 00 or 20 xx 00 00
420 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
421 * xx is a sequencer which has never been seen over 0x3f
422 * imho data written down looks like bayer, i see similarities
423 * after every 640 bytes
425 if (*iso_buf
& 0x80) {
428 /* This marks a new frame */
429 if (fb
->v4lbuf
.bytesused
!= 0
430 && fb
->v4lbuf
.bytesused
!= dev
->frame_size
) {
431 (void) (printk_ratelimit() &&
432 STK_ERROR("frame %d, "
433 "bytesused=%d, skipping\n",
434 i
, fb
->v4lbuf
.bytesused
));
435 fb
->v4lbuf
.bytesused
= 0;
437 } else if (fb
->v4lbuf
.bytesused
== dev
->frame_size
) {
438 list_move_tail(dev
->sio_avail
.next
,
440 wake_up(&dev
->wait_frame
);
441 if (list_empty(&dev
->sio_avail
)) {
442 (void) (printk_ratelimit() &&
443 STK_ERROR("No buffer available\n"));
446 fb
= list_first_entry(&dev
->sio_avail
,
447 struct stk_sio_buffer
, list
);
448 fb
->v4lbuf
.bytesused
= 0;
456 /* Our buffer is full !!! */
457 if (framelen
+ fb
->v4lbuf
.bytesused
> dev
->frame_size
) {
458 (void) (printk_ratelimit() &&
459 STK_ERROR("Frame buffer overflow, lost sync\n"));
460 /*FIXME Do something here? */
463 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
464 memcpy(fill
, iso_buf
, framelen
);
465 spin_lock_irqsave(&dev
->spinlock
, flags
);
468 /* New size of our buffer */
469 fb
->v4lbuf
.bytesused
+= framelen
;
473 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
474 urb
->dev
= dev
->udev
;
475 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
477 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
482 /* -------------------------------------------- */
484 static int stk_prepare_iso(struct stk_camera
*dev
)
489 struct usb_device
*udev
;
496 STK_ERROR("isobufs already allocated. Bad\n");
498 dev
->isobufs
= kzalloc(MAX_ISO_BUFS
* sizeof(*dev
->isobufs
),
500 if (dev
->isobufs
== NULL
) {
501 STK_ERROR("Unable to allocate iso buffers\n");
504 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
505 if (dev
->isobufs
[i
].data
== NULL
) {
506 kbuf
= kzalloc(ISO_BUFFER_SIZE
, GFP_KERNEL
);
508 STK_ERROR("Failed to allocate iso buffer %d\n",
512 dev
->isobufs
[i
].data
= kbuf
;
514 STK_ERROR("isobuf data already allocated\n");
515 if (dev
->isobufs
[i
].urb
== NULL
) {
516 urb
= usb_alloc_urb(ISO_FRAMES_PER_DESC
, GFP_KERNEL
);
518 STK_ERROR("Failed to allocate URB %d\n", i
);
521 dev
->isobufs
[i
].urb
= urb
;
523 STK_ERROR("Killing URB\n");
524 usb_kill_urb(dev
->isobufs
[i
].urb
);
525 urb
= dev
->isobufs
[i
].urb
;
529 urb
->pipe
= usb_rcvisocpipe(udev
, dev
->isoc_ep
);
530 urb
->transfer_flags
= URB_ISO_ASAP
;
531 urb
->transfer_buffer
= dev
->isobufs
[i
].data
;
532 urb
->transfer_buffer_length
= ISO_BUFFER_SIZE
;
533 urb
->complete
= stk_isoc_handler
;
535 urb
->start_frame
= 0;
536 urb
->number_of_packets
= ISO_FRAMES_PER_DESC
;
538 for (j
= 0; j
< ISO_FRAMES_PER_DESC
; j
++) {
539 urb
->iso_frame_desc
[j
].offset
= j
* ISO_MAX_FRAME_SIZE
;
540 urb
->iso_frame_desc
[j
].length
= ISO_MAX_FRAME_SIZE
;
547 for (i
= 0; i
< MAX_ISO_BUFS
&& dev
->isobufs
[i
].data
; i
++)
548 kfree(dev
->isobufs
[i
].data
);
549 for (i
= 0; i
< MAX_ISO_BUFS
&& dev
->isobufs
[i
].urb
; i
++)
550 usb_free_urb(dev
->isobufs
[i
].urb
);
556 static void stk_clean_iso(struct stk_camera
*dev
)
560 if (dev
== NULL
|| dev
->isobufs
== NULL
)
563 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
566 urb
= dev
->isobufs
[i
].urb
;
568 if (atomic_read(&dev
->urbs_used
))
572 kfree(dev
->isobufs
[i
].data
);
576 unset_memallocd(dev
);
579 static int stk_setup_siobuf(struct stk_camera
*dev
, int index
)
581 struct stk_sio_buffer
*buf
= dev
->sio_bufs
+ index
;
582 INIT_LIST_HEAD(&buf
->list
);
583 buf
->v4lbuf
.length
= PAGE_ALIGN(dev
->frame_size
);
584 buf
->buffer
= vmalloc_user(buf
->v4lbuf
.length
);
585 if (buf
->buffer
== NULL
)
589 buf
->v4lbuf
.index
= index
;
590 buf
->v4lbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
591 buf
->v4lbuf
.field
= V4L2_FIELD_NONE
;
592 buf
->v4lbuf
.memory
= V4L2_MEMORY_MMAP
;
593 buf
->v4lbuf
.m
.offset
= 2*index
*buf
->v4lbuf
.length
;
597 static int stk_free_sio_buffers(struct stk_camera
*dev
)
602 if (dev
->n_sbufs
== 0 || dev
->sio_bufs
== NULL
)
605 * If any buffers are mapped, we cannot free them at all.
607 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
608 if (dev
->sio_bufs
[i
].mapcount
> 0)
614 spin_lock_irqsave(&dev
->spinlock
, flags
);
615 INIT_LIST_HEAD(&dev
->sio_avail
);
616 INIT_LIST_HEAD(&dev
->sio_full
);
617 nbufs
= dev
->n_sbufs
;
619 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
620 for (i
= 0; i
< nbufs
; i
++) {
621 if (dev
->sio_bufs
[i
].buffer
!= NULL
)
622 vfree(dev
->sio_bufs
[i
].buffer
);
624 kfree(dev
->sio_bufs
);
625 dev
->sio_bufs
= NULL
;
629 static int stk_prepare_sio_buffers(struct stk_camera
*dev
, unsigned n_sbufs
)
632 if (dev
->sio_bufs
!= NULL
)
633 STK_ERROR("sio_bufs already allocated\n");
635 dev
->sio_bufs
= kzalloc(n_sbufs
* sizeof(struct stk_sio_buffer
),
637 if (dev
->sio_bufs
== NULL
)
639 for (i
= 0; i
< n_sbufs
; i
++) {
640 if (stk_setup_siobuf(dev
, i
))
641 return (dev
->n_sbufs
> 1)? 0 : -ENOMEM
;
648 static int stk_allocate_buffers(struct stk_camera
*dev
, unsigned n_sbufs
)
651 err
= stk_prepare_iso(dev
);
656 err
= stk_prepare_sio_buffers(dev
, n_sbufs
);
658 stk_free_sio_buffers(dev
);
664 static void stk_free_buffers(struct stk_camera
*dev
)
667 stk_free_sio_buffers(dev
);
669 /* -------------------------------------------- */
671 /* v4l file operations */
673 static int v4l_stk_open(struct inode
*inode
, struct file
*fp
)
675 struct stk_camera
*dev
;
676 struct video_device
*vdev
;
678 vdev
= video_devdata(fp
);
679 dev
= vdev_to_camera(vdev
);
681 if (dev
== NULL
|| !is_present(dev
))
683 fp
->private_data
= vdev
;
684 kref_get(&dev
->kref
);
685 usb_autopm_get_interface(dev
->interface
);
690 static int v4l_stk_release(struct inode
*inode
, struct file
*fp
)
692 struct stk_camera
*dev
;
693 struct video_device
*vdev
;
695 vdev
= video_devdata(fp
);
697 STK_ERROR("v4l_release called w/o video devdata\n");
700 dev
= vdev_to_camera(vdev
);
702 STK_ERROR("v4l_release called on removed device\n");
706 if (dev
->owner
!= fp
) {
707 usb_autopm_put_interface(dev
->interface
);
708 kref_put(&dev
->kref
, stk_camera_cleanup
);
712 stk_stop_stream(dev
);
714 stk_free_buffers(dev
);
718 usb_autopm_put_interface(dev
->interface
);
719 kref_put(&dev
->kref
, stk_camera_cleanup
);
724 static ssize_t
v4l_stk_read(struct file
*fp
, char __user
*buf
,
725 size_t count
, loff_t
*f_pos
)
730 struct stk_camera
*dev
;
731 struct video_device
*vdev
;
732 struct stk_sio_buffer
*sbuf
;
734 vdev
= video_devdata(fp
);
737 dev
= vdev_to_camera(vdev
);
742 if (!is_present(dev
))
744 if (dev
->owner
&& dev
->owner
!= fp
)
747 if (!is_streaming(dev
)) {
748 if (stk_initialise(dev
)
749 || stk_allocate_buffers(dev
, 3)
750 || stk_start_stream(dev
))
752 spin_lock_irqsave(&dev
->spinlock
, flags
);
753 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
754 list_add_tail(&dev
->sio_bufs
[i
].list
, &dev
->sio_avail
);
755 dev
->sio_bufs
[i
].v4lbuf
.flags
= V4L2_BUF_FLAG_QUEUED
;
757 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
760 if (fp
->f_flags
& O_NONBLOCK
&& list_empty(&dev
->sio_full
))
762 ret
= wait_event_interruptible(dev
->wait_frame
,
763 !list_empty(&dev
->sio_full
) || !is_present(dev
));
766 if (!is_present(dev
))
769 if (count
+ *f_pos
> dev
->frame_size
)
770 count
= dev
->frame_size
- *f_pos
;
771 spin_lock_irqsave(&dev
->spinlock
, flags
);
772 if (list_empty(&dev
->sio_full
)) {
773 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
774 STK_ERROR("BUG: No siobufs ready\n");
777 sbuf
= list_first_entry(&dev
->sio_full
, struct stk_sio_buffer
, list
);
778 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
780 if (copy_to_user(buf
, sbuf
->buffer
+ *f_pos
, count
))
785 if (*f_pos
>= dev
->frame_size
) {
787 spin_lock_irqsave(&dev
->spinlock
, flags
);
788 list_move_tail(&sbuf
->list
, &dev
->sio_avail
);
789 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
794 static unsigned int v4l_stk_poll(struct file
*fp
, poll_table
*wait
)
796 struct stk_camera
*dev
;
797 struct video_device
*vdev
;
799 vdev
= video_devdata(fp
);
804 dev
= vdev_to_camera(vdev
);
808 poll_wait(fp
, &dev
->wait_frame
, wait
);
810 if (!is_present(dev
))
813 if (!list_empty(&dev
->sio_full
))
814 return (POLLIN
| POLLRDNORM
);
820 static void stk_v4l_vm_open(struct vm_area_struct
*vma
)
822 struct stk_sio_buffer
*sbuf
= vma
->vm_private_data
;
825 static void stk_v4l_vm_close(struct vm_area_struct
*vma
)
827 struct stk_sio_buffer
*sbuf
= vma
->vm_private_data
;
829 if (sbuf
->mapcount
== 0)
830 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
832 static struct vm_operations_struct stk_v4l_vm_ops
= {
833 .open
= stk_v4l_vm_open
,
834 .close
= stk_v4l_vm_close
837 static int v4l_stk_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
841 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
842 struct stk_camera
*dev
;
843 struct video_device
*vdev
;
844 struct stk_sio_buffer
*sbuf
= NULL
;
846 if (!(vma
->vm_flags
& VM_WRITE
) || !(vma
->vm_flags
& VM_SHARED
))
849 vdev
= video_devdata(fp
);
850 dev
= vdev_to_camera(vdev
);
852 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
853 if (dev
->sio_bufs
[i
].v4lbuf
.m
.offset
== offset
) {
854 sbuf
= dev
->sio_bufs
+ i
;
860 ret
= remap_vmalloc_range(vma
, sbuf
->buffer
, 0);
863 vma
->vm_flags
|= VM_DONTEXPAND
;
864 vma
->vm_private_data
= sbuf
;
865 vma
->vm_ops
= &stk_v4l_vm_ops
;
866 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
867 stk_v4l_vm_open(vma
);
871 /* v4l ioctl handlers */
873 static int stk_vidioc_querycap(struct file
*filp
,
874 void *priv
, struct v4l2_capability
*cap
)
876 strcpy(cap
->driver
, "stk");
877 strcpy(cap
->card
, "stk");
878 cap
->version
= DRIVER_VERSION_NUM
;
880 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
881 | V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
885 static int stk_vidioc_enum_input(struct file
*filp
,
886 void *priv
, struct v4l2_input
*input
)
888 if (input
->index
!= 0)
891 strcpy(input
->name
, "Syntek USB Camera");
892 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
897 static int stk_vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
903 static int stk_vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
912 static int stk_vidioc_s_std(struct file
*filp
, void *priv
, v4l2_std_id
*a
)
917 /* List of all V4Lv2 controls supported by the driver */
918 static struct v4l2_queryctrl stk_controls
[] = {
920 .id
= V4L2_CID_BRIGHTNESS
,
921 .type
= V4L2_CTRL_TYPE_INTEGER
,
922 .name
= "Brightness",
926 .default_value
= 0x6000,
928 /*TODO: get more controls to work */
931 static int stk_vidioc_queryctrl(struct file
*filp
,
932 void *priv
, struct v4l2_queryctrl
*c
)
936 nbr
= ARRAY_SIZE(stk_controls
);
938 for (i
= 0; i
< nbr
; i
++) {
939 if (stk_controls
[i
].id
== c
->id
) {
940 memcpy(c
, &stk_controls
[i
],
941 sizeof(struct v4l2_queryctrl
));
948 static int stk_vidioc_g_ctrl(struct file
*filp
,
949 void *priv
, struct v4l2_control
*c
)
951 struct stk_camera
*dev
= priv
;
953 case V4L2_CID_BRIGHTNESS
:
954 c
->value
= dev
->vsettings
.brightness
;
962 static int stk_vidioc_s_ctrl(struct file
*filp
,
963 void *priv
, struct v4l2_control
*c
)
965 struct stk_camera
*dev
= priv
;
967 case V4L2_CID_BRIGHTNESS
:
968 dev
->vsettings
.brightness
= c
->value
;
969 return stk_sensor_set_brightness(dev
, c
->value
>> 8);
977 static int stk_vidioc_enum_fmt_cap(struct file
*filp
,
978 void *priv
, struct v4l2_fmtdesc
*fmtd
)
982 switch (fmtd
->index
) {
984 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB565
;
985 strcpy(fmtd
->description
, "r5g6b5");
988 fmtd
->pixelformat
= V4L2_PIX_FMT_RGB565X
;
989 strcpy(fmtd
->description
, "r5g6b5BE");
992 fmtd
->pixelformat
= V4L2_PIX_FMT_UYVY
;
993 strcpy(fmtd
->description
, "yuv4:2:2");
996 fmtd
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
997 strcpy(fmtd
->description
, "Raw bayer");
1000 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
1001 strcpy(fmtd
->description
, "yuv4:2:2");
1009 static struct stk_size
{
1014 { .w
= 1280, .h
= 1024, .m
= MODE_SXGA
, },
1015 { .w
= 640, .h
= 480, .m
= MODE_VGA
, },
1016 { .w
= 352, .h
= 288, .m
= MODE_CIF
, },
1017 { .w
= 320, .h
= 240, .m
= MODE_QVGA
, },
1018 { .w
= 176, .h
= 144, .m
= MODE_QCIF
, },
1021 static int stk_vidioc_g_fmt_cap(struct file
*filp
,
1022 void *priv
, struct v4l2_format
*f
)
1024 struct v4l2_pix_format
*pix_format
= &f
->fmt
.pix
;
1025 struct stk_camera
*dev
= priv
;
1028 for (i
= 0; i
< ARRAY_SIZE(stk_sizes
)
1029 && stk_sizes
[i
].m
!= dev
->vsettings
.mode
;
1031 if (i
== ARRAY_SIZE(stk_sizes
)) {
1032 STK_ERROR("ERROR: mode invalid\n");
1035 pix_format
->width
= stk_sizes
[i
].w
;
1036 pix_format
->height
= stk_sizes
[i
].h
;
1037 pix_format
->field
= V4L2_FIELD_NONE
;
1038 pix_format
->colorspace
= V4L2_COLORSPACE_SRGB
;
1039 pix_format
->priv
= 0;
1040 pix_format
->pixelformat
= dev
->vsettings
.palette
;
1041 if (dev
->vsettings
.palette
== V4L2_PIX_FMT_SBGGR8
)
1042 pix_format
->bytesperline
= pix_format
->width
;
1044 pix_format
->bytesperline
= 2 * pix_format
->width
;
1045 pix_format
->sizeimage
= pix_format
->bytesperline
1046 * pix_format
->height
;
1050 static int stk_vidioc_try_fmt_cap(struct file
*filp
,
1051 void *priv
, struct v4l2_format
*fmtd
)
1054 switch (fmtd
->fmt
.pix
.pixelformat
) {
1055 case V4L2_PIX_FMT_RGB565
:
1056 case V4L2_PIX_FMT_RGB565X
:
1057 case V4L2_PIX_FMT_UYVY
:
1058 case V4L2_PIX_FMT_YUYV
:
1059 case V4L2_PIX_FMT_SBGGR8
:
1064 for (i
= 1; i
< ARRAY_SIZE(stk_sizes
); i
++) {
1065 if (fmtd
->fmt
.pix
.width
> stk_sizes
[i
].w
)
1068 if (i
== ARRAY_SIZE(stk_sizes
)
1069 || (abs(fmtd
->fmt
.pix
.width
- stk_sizes
[i
-1].w
)
1070 < abs(fmtd
->fmt
.pix
.width
- stk_sizes
[i
].w
))) {
1071 fmtd
->fmt
.pix
.height
= stk_sizes
[i
-1].h
;
1072 fmtd
->fmt
.pix
.width
= stk_sizes
[i
-1].w
;
1073 fmtd
->fmt
.pix
.priv
= i
- 1;
1075 fmtd
->fmt
.pix
.height
= stk_sizes
[i
].h
;
1076 fmtd
->fmt
.pix
.width
= stk_sizes
[i
].w
;
1077 fmtd
->fmt
.pix
.priv
= i
;
1080 fmtd
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1081 fmtd
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SRGB
;
1082 if (fmtd
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_SBGGR8
)
1083 fmtd
->fmt
.pix
.bytesperline
= fmtd
->fmt
.pix
.width
;
1085 fmtd
->fmt
.pix
.bytesperline
= 2 * fmtd
->fmt
.pix
.width
;
1086 fmtd
->fmt
.pix
.sizeimage
= fmtd
->fmt
.pix
.bytesperline
1087 * fmtd
->fmt
.pix
.height
;
1091 static int stk_setup_format(struct stk_camera
*dev
)
1095 if (dev
->vsettings
.palette
== V4L2_PIX_FMT_SBGGR8
)
1099 while (stk_sizes
[i
].m
!= dev
->vsettings
.mode
1100 && i
< ARRAY_SIZE(stk_sizes
))
1102 if (i
== ARRAY_SIZE(stk_sizes
)) {
1103 STK_ERROR("Something is broken in %s\n", __FUNCTION__
);
1106 /* This registers controls some timings, not sure of what. */
1107 stk_camera_write_reg(dev
, 0x001b, 0x0e);
1108 if (dev
->vsettings
.mode
== MODE_SXGA
)
1109 stk_camera_write_reg(dev
, 0x001c, 0x0e);
1111 stk_camera_write_reg(dev
, 0x001c, 0x46);
1113 * Registers 0x0115 0x0114 are the size of each line (bytes),
1114 * regs 0x0117 0x0116 are the heigth of the image.
1116 stk_camera_write_reg(dev
, 0x0115,
1117 ((stk_sizes
[i
].w
* depth
) >> 8) & 0xff);
1118 stk_camera_write_reg(dev
, 0x0114,
1119 (stk_sizes
[i
].w
* depth
) & 0xff);
1120 stk_camera_write_reg(dev
, 0x0117,
1121 (stk_sizes
[i
].h
>> 8) & 0xff);
1122 stk_camera_write_reg(dev
, 0x0116,
1123 stk_sizes
[i
].h
& 0xff);
1124 return stk_sensor_configure(dev
);
1127 static int stk_vidioc_s_fmt_cap(struct file
*filp
,
1128 void *priv
, struct v4l2_format
*fmtd
)
1131 struct stk_camera
*dev
= priv
;
1135 if (!is_present(dev
))
1137 if (is_streaming(dev
))
1139 if (dev
->owner
&& dev
->owner
!= filp
)
1141 ret
= stk_vidioc_try_fmt_cap(filp
, priv
, fmtd
);
1146 dev
->vsettings
.palette
= fmtd
->fmt
.pix
.pixelformat
;
1147 stk_free_buffers(dev
);
1148 dev
->frame_size
= fmtd
->fmt
.pix
.sizeimage
;
1149 dev
->vsettings
.mode
= stk_sizes
[fmtd
->fmt
.pix
.priv
].m
;
1151 stk_initialise(dev
);
1152 return stk_setup_format(dev
);
1155 static int stk_vidioc_reqbufs(struct file
*filp
,
1156 void *priv
, struct v4l2_requestbuffers
*rb
)
1158 struct stk_camera
*dev
= priv
;
1162 if (rb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1164 if (rb
->memory
!= V4L2_MEMORY_MMAP
)
1166 if (is_streaming(dev
)
1167 || (dev
->owner
&& dev
->owner
!= filp
))
1171 /*FIXME If they ask for zero, we must stop streaming and free */
1174 /* Arbitrary limit */
1175 else if (rb
->count
> 5)
1178 stk_allocate_buffers(dev
, rb
->count
);
1179 rb
->count
= dev
->n_sbufs
;
1183 static int stk_vidioc_querybuf(struct file
*filp
,
1184 void *priv
, struct v4l2_buffer
*buf
)
1187 struct stk_camera
*dev
= priv
;
1188 struct stk_sio_buffer
*sbuf
;
1190 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1195 if (index
< 0 || index
>= dev
->n_sbufs
)
1197 sbuf
= dev
->sio_bufs
+ buf
->index
;
1198 *buf
= sbuf
->v4lbuf
;
1202 static int stk_vidioc_qbuf(struct file
*filp
,
1203 void *priv
, struct v4l2_buffer
*buf
)
1205 struct stk_camera
*dev
= priv
;
1206 struct stk_sio_buffer
*sbuf
;
1207 unsigned long flags
;
1208 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1211 if (buf
->memory
!= V4L2_MEMORY_MMAP
)
1214 if (buf
->index
< 0 || buf
->index
>= dev
->n_sbufs
)
1216 sbuf
= dev
->sio_bufs
+ buf
->index
;
1217 if (sbuf
->v4lbuf
.flags
& V4L2_BUF_FLAG_QUEUED
)
1219 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1220 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1221 spin_lock_irqsave(&dev
->spinlock
, flags
);
1222 list_add_tail(&sbuf
->list
, &dev
->sio_avail
);
1223 *buf
= sbuf
->v4lbuf
;
1224 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1228 static int stk_vidioc_dqbuf(struct file
*filp
,
1229 void *priv
, struct v4l2_buffer
*buf
)
1231 struct stk_camera
*dev
= priv
;
1232 struct stk_sio_buffer
*sbuf
;
1233 unsigned long flags
;
1236 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
1237 || !is_streaming(dev
))
1240 if (filp
->f_flags
& O_NONBLOCK
&& list_empty(&dev
->sio_full
))
1241 return -EWOULDBLOCK
;
1242 ret
= wait_event_interruptible(dev
->wait_frame
,
1243 !list_empty(&dev
->sio_full
) || !is_present(dev
));
1246 if (!is_present(dev
))
1249 spin_lock_irqsave(&dev
->spinlock
, flags
);
1250 sbuf
= list_first_entry(&dev
->sio_full
, struct stk_sio_buffer
, list
);
1251 list_del_init(&sbuf
->list
);
1252 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1253 sbuf
->v4lbuf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
1254 sbuf
->v4lbuf
.flags
|= V4L2_BUF_FLAG_DONE
;
1255 sbuf
->v4lbuf
.sequence
= ++dev
->sequence
;
1256 do_gettimeofday(&sbuf
->v4lbuf
.timestamp
);
1258 *buf
= sbuf
->v4lbuf
;
1262 static int stk_vidioc_streamon(struct file
*filp
,
1263 void *priv
, enum v4l2_buf_type type
)
1265 struct stk_camera
*dev
= priv
;
1266 if (is_streaming(dev
))
1268 if (dev
->sio_bufs
== NULL
)
1271 return stk_start_stream(dev
);
1274 static int stk_vidioc_streamoff(struct file
*filp
,
1275 void *priv
, enum v4l2_buf_type type
)
1277 struct stk_camera
*dev
= priv
;
1278 unsigned long flags
;
1280 stk_stop_stream(dev
);
1281 spin_lock_irqsave(&dev
->spinlock
, flags
);
1282 INIT_LIST_HEAD(&dev
->sio_avail
);
1283 INIT_LIST_HEAD(&dev
->sio_full
);
1284 for (i
= 0; i
< dev
->n_sbufs
; i
++) {
1285 INIT_LIST_HEAD(&dev
->sio_bufs
[i
].list
);
1286 dev
->sio_bufs
[i
].v4lbuf
.flags
= 0;
1288 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1293 static int stk_vidioc_g_parm(struct file
*filp
,
1294 void *priv
, struct v4l2_streamparm
*sp
)
1296 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1299 sp
->parm
.capture
.capability
= 0;
1300 sp
->parm
.capture
.capturemode
= 0;
1301 /*FIXME This is not correct */
1302 sp
->parm
.capture
.timeperframe
.numerator
= 1;
1303 sp
->parm
.capture
.timeperframe
.denominator
= 30;
1304 sp
->parm
.capture
.readbuffers
= 2;
1305 sp
->parm
.capture
.extendedmode
= 0;
1309 static struct file_operations v4l_stk_fops
= {
1310 .owner
= THIS_MODULE
,
1311 .open
= v4l_stk_open
,
1312 .release
= v4l_stk_release
,
1313 .read
= v4l_stk_read
,
1314 .poll
= v4l_stk_poll
,
1315 .mmap
= v4l_stk_mmap
,
1316 .ioctl
= video_ioctl2
,
1317 #ifdef CONFIG_COMPAT
1318 .compat_ioctl
= v4l_compat_ioctl32
,
1323 static void stk_v4l_dev_release(struct video_device
*vd
)
1327 static struct video_device stk_v4l_data
= {
1328 .name
= "stkwebcam",
1329 .type
= VFL_TYPE_GRABBER
,
1330 .type2
= VID_TYPE_CAPTURE
,
1332 .tvnorms
= V4L2_STD_UNKNOWN
,
1333 .current_norm
= V4L2_STD_UNKNOWN
,
1334 .fops
= &v4l_stk_fops
,
1335 .release
= stk_v4l_dev_release
,
1337 .vidioc_querycap
= stk_vidioc_querycap
,
1338 .vidioc_enum_fmt_cap
= stk_vidioc_enum_fmt_cap
,
1339 .vidioc_try_fmt_cap
= stk_vidioc_try_fmt_cap
,
1340 .vidioc_s_fmt_cap
= stk_vidioc_s_fmt_cap
,
1341 .vidioc_g_fmt_cap
= stk_vidioc_g_fmt_cap
,
1342 .vidioc_enum_input
= stk_vidioc_enum_input
,
1343 .vidioc_s_input
= stk_vidioc_s_input
,
1344 .vidioc_g_input
= stk_vidioc_g_input
,
1345 .vidioc_s_std
= stk_vidioc_s_std
,
1346 .vidioc_reqbufs
= stk_vidioc_reqbufs
,
1347 .vidioc_querybuf
= stk_vidioc_querybuf
,
1348 .vidioc_qbuf
= stk_vidioc_qbuf
,
1349 .vidioc_dqbuf
= stk_vidioc_dqbuf
,
1350 .vidioc_streamon
= stk_vidioc_streamon
,
1351 .vidioc_streamoff
= stk_vidioc_streamoff
,
1352 .vidioc_queryctrl
= stk_vidioc_queryctrl
,
1353 .vidioc_g_ctrl
= stk_vidioc_g_ctrl
,
1354 .vidioc_s_ctrl
= stk_vidioc_s_ctrl
,
1355 .vidioc_g_parm
= stk_vidioc_g_parm
,
1359 static int stk_register_video_device(struct stk_camera
*dev
)
1363 dev
->vdev
= stk_v4l_data
;
1364 dev
->vdev
.debug
= debug
;
1365 dev
->vdev
.dev
= &dev
->interface
->dev
;
1366 dev
->vdev
.priv
= dev
;
1367 err
= video_register_device(&dev
->vdev
, VFL_TYPE_GRABBER
, -1);
1369 STK_ERROR("v4l registration failed\n");
1371 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1372 " /dev/video%d\n", dev
->vdev
.minor
);
1379 static int stk_camera_probe(struct usb_interface
*interface
,
1380 const struct usb_device_id
*id
)
1385 struct stk_camera
*dev
= NULL
;
1386 struct usb_device
*udev
= interface_to_usbdev(interface
);
1387 struct usb_host_interface
*iface_desc
;
1388 struct usb_endpoint_descriptor
*endpoint
;
1390 dev
= kzalloc(sizeof(struct stk_camera
), GFP_KERNEL
);
1392 STK_ERROR("Out of memory !\n");
1396 kref_init(&dev
->kref
);
1397 spin_lock_init(&dev
->spinlock
);
1398 init_waitqueue_head(&dev
->wait_frame
);
1401 dev
->interface
= interface
;
1402 usb_get_intf(interface
);
1404 dev
->vsettings
.vflip
= vflip
;
1405 dev
->vsettings
.hflip
= hflip
;
1409 /* Set up the endpoint information
1410 * use only the first isoc-in endpoint
1411 * for the current alternate setting */
1412 iface_desc
= interface
->cur_altsetting
;
1414 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1415 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1418 && ((endpoint
->bEndpointAddress
1419 & USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
)
1420 && ((endpoint
->bmAttributes
1421 & USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_ISOC
)) {
1422 /* we found an isoc in endpoint */
1423 dev
->isoc_ep
= (endpoint
->bEndpointAddress
& 0xF);
1427 if (!dev
->isoc_ep
) {
1428 STK_ERROR("Could not find isoc-in endpoint");
1429 kref_put(&dev
->kref
, stk_camera_cleanup
);
1432 dev
->vsettings
.brightness
= 0x7fff;
1433 dev
->vsettings
.palette
= V4L2_PIX_FMT_RGB565
;
1434 dev
->vsettings
.mode
= MODE_VGA
;
1435 dev
->frame_size
= 640 * 480 * 2;
1437 INIT_LIST_HEAD(&dev
->sio_avail
);
1438 INIT_LIST_HEAD(&dev
->sio_full
);
1440 usb_set_intfdata(interface
, dev
);
1442 err
= stk_register_video_device(dev
);
1444 kref_put(&dev
->kref
, stk_camera_cleanup
);
1448 stk_create_sysfs_files(&dev
->vdev
);
1449 usb_autopm_enable(dev
->interface
);
1454 static void stk_camera_disconnect(struct usb_interface
*interface
)
1456 struct stk_camera
*dev
= usb_get_intfdata(interface
);
1458 usb_set_intfdata(interface
, NULL
);
1461 wake_up_interruptible(&dev
->wait_frame
);
1462 stk_remove_sysfs_files(&dev
->vdev
);
1464 kref_put(&dev
->kref
, stk_camera_cleanup
);
1468 int stk_camera_suspend(struct usb_interface
*intf
, pm_message_t message
)
1470 struct stk_camera
*dev
= usb_get_intfdata(intf
);
1471 if (is_streaming(dev
)) {
1472 stk_stop_stream(dev
);
1473 /* yes, this is ugly */
1479 int stk_camera_resume(struct usb_interface
*intf
)
1481 struct stk_camera
*dev
= usb_get_intfdata(intf
);
1482 if (!is_initialised(dev
))
1484 unset_initialised(dev
);
1485 stk_initialise(dev
);
1486 stk_setup_format(dev
);
1487 if (is_streaming(dev
))
1488 stk_start_stream(dev
);
1493 static struct usb_driver stk_camera_driver
= {
1494 .name
= "stkwebcam",
1495 .probe
= stk_camera_probe
,
1496 .disconnect
= stk_camera_disconnect
,
1497 .id_table
= stkwebcam_table
,
1499 .suspend
= stk_camera_suspend
,
1500 .resume
= stk_camera_resume
,
1505 static int __init
stk_camera_init(void)
1509 result
= usb_register(&stk_camera_driver
);
1511 STK_ERROR("usb_register failed ! Error number %d\n", result
);
1517 static void __exit
stk_camera_exit(void)
1519 usb_deregister(&stk_camera_driver
);
1522 module_init(stk_camera_init
);
1523 module_exit(stk_camera_exit
);