aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/media
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/media')
-rw-r--r--drivers/usb/media/Kconfig15
-rw-r--r--drivers/usb/media/Makefile7
-rw-r--r--drivers/usb/media/dabusb.c36
-rw-r--r--drivers/usb/media/dabusb.h2
-rw-r--r--drivers/usb/media/et61x251.h28
-rw-r--r--drivers/usb/media/et61x251_core.c321
-rw-r--r--drivers/usb/media/et61x251_sensor.h5
-rw-r--r--drivers/usb/media/et61x251_tas5130d1b.c10
-rw-r--r--drivers/usb/media/ov511.c97
-rw-r--r--drivers/usb/media/ov511.h11
-rw-r--r--drivers/usb/media/pwc/pwc-ctrl.c1
-rw-r--r--drivers/usb/media/pwc/pwc-if.c9
-rw-r--r--drivers/usb/media/se401.c16
-rw-r--r--drivers/usb/media/se401.h3
-rw-r--r--drivers/usb/media/sn9c102.h28
-rw-r--r--drivers/usb/media/sn9c102_core.c326
-rw-r--r--drivers/usb/media/sn9c102_ov7630.c33
-rw-r--r--drivers/usb/media/sn9c102_pas202bca.c238
-rw-r--r--drivers/usb/media/sn9c102_pas202bcb.c2
-rw-r--r--drivers/usb/media/sn9c102_sensor.h15
-rw-r--r--drivers/usb/media/sn9c102_tas5110c1b.c14
-rw-r--r--drivers/usb/media/sn9c102_tas5130d1b.c12
-rw-r--r--drivers/usb/media/stv680.c20
-rw-r--r--drivers/usb/media/stv680.h2
-rw-r--r--drivers/usb/media/usbvideo.c31
-rw-r--r--drivers/usb/media/usbvideo.h5
-rw-r--r--drivers/usb/media/vicam.c22
-rw-r--r--drivers/usb/media/w9968cf.c88
-rw-r--r--drivers/usb/media/w9968cf.h14
-rw-r--r--drivers/usb/media/zc0301.h192
-rw-r--r--drivers/usb/media/zc0301_core.c2055
-rw-r--r--drivers/usb/media/zc0301_pas202bcb.c361
-rw-r--r--drivers/usb/media/zc0301_sensor.h103
33 files changed, 3588 insertions, 534 deletions
diff --git a/drivers/usb/media/Kconfig b/drivers/usb/media/Kconfig
index 0d3d2cc5d7be..189d40f96be5 100644
--- a/drivers/usb/media/Kconfig
+++ b/drivers/usb/media/Kconfig
@@ -191,6 +191,21 @@ config USB_W9968CF
191 To compile this driver as a module, choose M here: the 191 To compile this driver as a module, choose M here: the
192 module will be called w9968cf. 192 module will be called w9968cf.
193 193
194config USB_ZC0301
195 tristate "USB ZC0301 Image Processor and Control Chip support"
196 depends on USB && VIDEO_DEV
197 ---help---
198 Say Y here if you want support for cameras based on the ZC0301
199 Image Processor and Control Chip.
200
201 See <file:Documentation/usb/zc0301.txt> for more informations.
202
203 This driver uses the Video For Linux API. You must say Y or M to
204 "Video For Linux" to use this driver.
205
206 To compile this driver as a module, choose M here: the
207 module will be called zc0301.
208
194config USB_PWC 209config USB_PWC
195 tristate "USB Philips Cameras" 210 tristate "USB Philips Cameras"
196 depends on USB && VIDEO_DEV 211 depends on USB && VIDEO_DEV
diff --git a/drivers/usb/media/Makefile b/drivers/usb/media/Makefile
index 3957aa1be0f2..50e89a33b85e 100644
--- a/drivers/usb/media/Makefile
+++ b/drivers/usb/media/Makefile
@@ -2,8 +2,12 @@
2# Makefile for USB Media drivers 2# Makefile for USB Media drivers
3# 3#
4 4
5sn9c102-objs := sn9c102_core.o sn9c102_hv7131d.o sn9c102_mi0343.o sn9c102_ov7630.o sn9c102_pas106b.o sn9c102_pas202bcb.o sn9c102_tas5110c1b.o sn9c102_tas5130d1b.o 5sn9c102-objs := sn9c102_core.o sn9c102_hv7131d.o sn9c102_mi0343.o \
6 sn9c102_ov7630.o sn9c102_pas106b.o sn9c102_pas202bca.o \
7 sn9c102_pas202bcb.o sn9c102_tas5110c1b.o \
8 sn9c102_tas5130d1b.o
6et61x251-objs := et61x251_core.o et61x251_tas5130d1b.o 9et61x251-objs := et61x251_core.o et61x251_tas5130d1b.o
10zc0301-objs := zc0301_core.o zc0301_pas202bcb.o
7 11
8obj-$(CONFIG_USB_DABUSB) += dabusb.o 12obj-$(CONFIG_USB_DABUSB) += dabusb.o
9obj-$(CONFIG_USB_DSBR) += dsbr100.o 13obj-$(CONFIG_USB_DSBR) += dsbr100.o
@@ -16,4 +20,5 @@ obj-$(CONFIG_USB_SN9C102) += sn9c102.o
16obj-$(CONFIG_USB_STV680) += stv680.o 20obj-$(CONFIG_USB_STV680) += stv680.o
17obj-$(CONFIG_USB_VICAM) += vicam.o usbvideo.o 21obj-$(CONFIG_USB_VICAM) += vicam.o usbvideo.o
18obj-$(CONFIG_USB_W9968CF) += w9968cf.o 22obj-$(CONFIG_USB_W9968CF) += w9968cf.o
23obj-$(CONFIG_USB_ZC0301) += zc0301.o
19obj-$(CONFIG_USB_PWC) += pwc/ 24obj-$(CONFIG_USB_PWC) += pwc/
diff --git a/drivers/usb/media/dabusb.c b/drivers/usb/media/dabusb.c
index 18d8eaf408d5..1774ab7a40d2 100644
--- a/drivers/usb/media/dabusb.c
+++ b/drivers/usb/media/dabusb.c
@@ -38,6 +38,7 @@
38#include <linux/delay.h> 38#include <linux/delay.h>
39#include <linux/usb.h> 39#include <linux/usb.h>
40#include <linux/smp_lock.h> 40#include <linux/smp_lock.h>
41#include <linux/mutex.h>
41 42
42#include "dabusb.h" 43#include "dabusb.h"
43#include "dabfirmware.h" 44#include "dabfirmware.h"
@@ -217,12 +218,11 @@ static int dabusb_alloc_buffers (pdabusb_t s)
217 pipesize, packets, transfer_buffer_length); 218 pipesize, packets, transfer_buffer_length);
218 219
219 while (buffers < (s->total_buffer_size << 10)) { 220 while (buffers < (s->total_buffer_size << 10)) {
220 b = (pbuff_t) kmalloc (sizeof (buff_t), GFP_KERNEL); 221 b = (pbuff_t) kzalloc (sizeof (buff_t), GFP_KERNEL);
221 if (!b) { 222 if (!b) {
222 err("kmalloc(sizeof(buff_t))==NULL"); 223 err("kzalloc(sizeof(buff_t))==NULL");
223 goto err; 224 goto err;
224 } 225 }
225 memset (b, 0, sizeof (buff_t));
226 b->s = s; 226 b->s = s;
227 b->purb = usb_alloc_urb(packets, GFP_KERNEL); 227 b->purb = usb_alloc_urb(packets, GFP_KERNEL);
228 if (!b->purb) { 228 if (!b->purb) {
@@ -571,7 +571,7 @@ static ssize_t dabusb_read (struct file *file, char __user *buf, size_t count, l
571 s->readptr = 0; 571 s->readptr = 0;
572 } 572 }
573 } 573 }
574 err: //up(&s->mutex); 574 err: //mutex_unlock(&s->mutex);
575 return ret; 575 return ret;
576} 576}
577 577
@@ -586,10 +586,10 @@ static int dabusb_open (struct inode *inode, struct file *file)
586 s = &dabusb[devnum - DABUSB_MINOR]; 586 s = &dabusb[devnum - DABUSB_MINOR];
587 587
588 dbg("dabusb_open"); 588 dbg("dabusb_open");
589 down (&s->mutex); 589 mutex_lock(&s->mutex);
590 590
591 while (!s->usbdev || s->opened) { 591 while (!s->usbdev || s->opened) {
592 up (&s->mutex); 592 mutex_unlock(&s->mutex);
593 593
594 if (file->f_flags & O_NONBLOCK) { 594 if (file->f_flags & O_NONBLOCK) {
595 return -EBUSY; 595 return -EBUSY;
@@ -599,15 +599,15 @@ static int dabusb_open (struct inode *inode, struct file *file)
599 if (signal_pending (current)) { 599 if (signal_pending (current)) {
600 return -EAGAIN; 600 return -EAGAIN;
601 } 601 }
602 down (&s->mutex); 602 mutex_lock(&s->mutex);
603 } 603 }
604 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) { 604 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
605 up(&s->mutex); 605 mutex_unlock(&s->mutex);
606 err("set_interface failed"); 606 err("set_interface failed");
607 return -EINVAL; 607 return -EINVAL;
608 } 608 }
609 s->opened = 1; 609 s->opened = 1;
610 up (&s->mutex); 610 mutex_unlock(&s->mutex);
611 611
612 file->f_pos = 0; 612 file->f_pos = 0;
613 file->private_data = s; 613 file->private_data = s;
@@ -621,10 +621,10 @@ static int dabusb_release (struct inode *inode, struct file *file)
621 621
622 dbg("dabusb_release"); 622 dbg("dabusb_release");
623 623
624 down (&s->mutex); 624 mutex_lock(&s->mutex);
625 dabusb_stop (s); 625 dabusb_stop (s);
626 dabusb_free_buffers (s); 626 dabusb_free_buffers (s);
627 up (&s->mutex); 627 mutex_unlock(&s->mutex);
628 628
629 if (!s->remove_pending) { 629 if (!s->remove_pending) {
630 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0) 630 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0)
@@ -649,10 +649,10 @@ static int dabusb_ioctl (struct inode *inode, struct file *file, unsigned int cm
649 if (s->remove_pending) 649 if (s->remove_pending)
650 return -EIO; 650 return -EIO;
651 651
652 down (&s->mutex); 652 mutex_lock(&s->mutex);
653 653
654 if (!s->usbdev) { 654 if (!s->usbdev) {
655 up (&s->mutex); 655 mutex_unlock(&s->mutex);
656 return -EIO; 656 return -EIO;
657 } 657 }
658 658
@@ -692,7 +692,7 @@ static int dabusb_ioctl (struct inode *inode, struct file *file, unsigned int cm
692 ret = -ENOIOCTLCMD; 692 ret = -ENOIOCTLCMD;
693 break; 693 break;
694 } 694 }
695 up (&s->mutex); 695 mutex_unlock(&s->mutex);
696 return ret; 696 return ret;
697} 697}
698 698
@@ -738,7 +738,7 @@ static int dabusb_probe (struct usb_interface *intf,
738 738
739 s = &dabusb[intf->minor]; 739 s = &dabusb[intf->minor];
740 740
741 down (&s->mutex); 741 mutex_lock(&s->mutex);
742 s->remove_pending = 0; 742 s->remove_pending = 0;
743 s->usbdev = usbdev; 743 s->usbdev = usbdev;
744 s->devnum = intf->minor; 744 s->devnum = intf->minor;
@@ -761,7 +761,7 @@ static int dabusb_probe (struct usb_interface *intf,
761 } 761 }
762 dbg("bound to interface: %d", intf->altsetting->desc.bInterfaceNumber); 762 dbg("bound to interface: %d", intf->altsetting->desc.bInterfaceNumber);
763 usb_set_intfdata (intf, s); 763 usb_set_intfdata (intf, s);
764 up (&s->mutex); 764 mutex_unlock(&s->mutex);
765 765
766 retval = usb_register_dev(intf, &dabusb_class); 766 retval = usb_register_dev(intf, &dabusb_class);
767 if (retval) { 767 if (retval) {
@@ -772,7 +772,7 @@ static int dabusb_probe (struct usb_interface *intf,
772 return 0; 772 return 0;
773 773
774 reject: 774 reject:
775 up (&s->mutex); 775 mutex_unlock(&s->mutex);
776 s->usbdev = NULL; 776 s->usbdev = NULL;
777 return -ENODEV; 777 return -ENODEV;
778} 778}
@@ -829,7 +829,7 @@ static int __init dabusb_init (void)
829 for (u = 0; u < NRDABUSB; u++) { 829 for (u = 0; u < NRDABUSB; u++) {
830 pdabusb_t s = &dabusb[u]; 830 pdabusb_t s = &dabusb[u];
831 memset (s, 0, sizeof (dabusb_t)); 831 memset (s, 0, sizeof (dabusb_t));
832 init_MUTEX (&s->mutex); 832 mutex_init (&s->mutex);
833 s->usbdev = NULL; 833 s->usbdev = NULL;
834 s->total_buffer_size = buffers; 834 s->total_buffer_size = buffers;
835 init_waitqueue_head (&s->wait); 835 init_waitqueue_head (&s->wait);
diff --git a/drivers/usb/media/dabusb.h b/drivers/usb/media/dabusb.h
index 10b666e43abc..96b03e4af8b9 100644
--- a/drivers/usb/media/dabusb.h
+++ b/drivers/usb/media/dabusb.h
@@ -18,7 +18,7 @@ typedef enum { _stopped=0, _started } driver_state_t;
18 18
19typedef struct 19typedef struct
20{ 20{
21 struct semaphore mutex; 21 struct mutex mutex;
22 struct usb_device *usbdev; 22 struct usb_device *usbdev;
23 wait_queue_head_t wait; 23 wait_queue_head_t wait;
24 wait_queue_head_t remove_ok; 24 wait_queue_head_t remove_ok;
diff --git a/drivers/usb/media/et61x251.h b/drivers/usb/media/et61x251.h
index 652238f329f3..eee8afc9be72 100644
--- a/drivers/usb/media/et61x251.h
+++ b/drivers/usb/media/et61x251.h
@@ -33,7 +33,9 @@
33#include <linux/types.h> 33#include <linux/types.h>
34#include <linux/param.h> 34#include <linux/param.h>
35#include <linux/rwsem.h> 35#include <linux/rwsem.h>
36#include <asm/semaphore.h> 36#include <linux/mutex.h>
37#include <linux/stddef.h>
38#include <linux/string.h>
37 39
38#include "et61x251_sensor.h" 40#include "et61x251_sensor.h"
39 41
@@ -51,6 +53,7 @@
51#define ET61X251_ALTERNATE_SETTING 13 53#define ET61X251_ALTERNATE_SETTING 13
52#define ET61X251_URB_TIMEOUT msecs_to_jiffies(2 * ET61X251_ISO_PACKETS) 54#define ET61X251_URB_TIMEOUT msecs_to_jiffies(2 * ET61X251_ISO_PACKETS)
53#define ET61X251_CTRL_TIMEOUT 100 55#define ET61X251_CTRL_TIMEOUT 100
56#define ET61X251_FRAME_TIMEOUT 2
54 57
55/*****************************************************************************/ 58/*****************************************************************************/
56 59
@@ -127,15 +130,16 @@ struct et61x251_sysfs_attr {
127 130
128struct et61x251_module_param { 131struct et61x251_module_param {
129 u8 force_munmap; 132 u8 force_munmap;
133 u16 frame_timeout;
130}; 134};
131 135
132static DECLARE_MUTEX(et61x251_sysfs_lock); 136static DEFINE_MUTEX(et61x251_sysfs_lock);
133static DECLARE_RWSEM(et61x251_disconnect); 137static DECLARE_RWSEM(et61x251_disconnect);
134 138
135struct et61x251_device { 139struct et61x251_device {
136 struct video_device* v4ldev; 140 struct video_device* v4ldev;
137 141
138 struct et61x251_sensor* sensor; 142 struct et61x251_sensor sensor;
139 143
140 struct usb_device* usbdev; 144 struct usb_device* usbdev;
141 struct urb* urb[ET61X251_URBS]; 145 struct urb* urb[ET61X251_URBS];
@@ -157,19 +161,28 @@ struct et61x251_device {
157 enum et61x251_dev_state state; 161 enum et61x251_dev_state state;
158 u8 users; 162 u8 users;
159 163
160 struct semaphore dev_sem, fileop_sem; 164 struct mutex dev_mutex, fileop_mutex;
161 spinlock_t queue_lock; 165 spinlock_t queue_lock;
162 wait_queue_head_t open, wait_frame, wait_stream; 166 wait_queue_head_t open, wait_frame, wait_stream;
163}; 167};
164 168
165/*****************************************************************************/ 169/*****************************************************************************/
166 170
171struct et61x251_device*
172et61x251_match_id(struct et61x251_device* cam, const struct usb_device_id *id)
173{
174 if (usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id))
175 return cam;
176
177 return NULL;
178}
179
180
167void 181void
168et61x251_attach_sensor(struct et61x251_device* cam, 182et61x251_attach_sensor(struct et61x251_device* cam,
169 struct et61x251_sensor* sensor) 183 struct et61x251_sensor* sensor)
170{ 184{
171 cam->sensor = sensor; 185 memcpy(&cam->sensor, sensor, sizeof(struct et61x251_sensor));
172 cam->sensor->usbdev = cam->usbdev;
173} 186}
174 187
175/*****************************************************************************/ 188/*****************************************************************************/
@@ -212,7 +225,8 @@ do { \
212 225
213#undef PDBG 226#undef PDBG
214#define PDBG(fmt, args...) \ 227#define PDBG(fmt, args...) \
215dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args) 228dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
229 __FUNCTION__, __LINE__ , ## args)
216 230
217#undef PDBGG 231#undef PDBGG
218#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */ 232#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
diff --git a/drivers/usb/media/et61x251_core.c b/drivers/usb/media/et61x251_core.c
index 2c0171a5ad62..7cc01b828b3d 100644
--- a/drivers/usb/media/et61x251_core.c
+++ b/drivers/usb/media/et61x251_core.c
@@ -25,11 +25,9 @@
25#include <linux/moduleparam.h> 25#include <linux/moduleparam.h>
26#include <linux/errno.h> 26#include <linux/errno.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/string.h>
29#include <linux/device.h> 28#include <linux/device.h>
30#include <linux/fs.h> 29#include <linux/fs.h>
31#include <linux/delay.h> 30#include <linux/delay.h>
32#include <linux/stddef.h>
33#include <linux/compiler.h> 31#include <linux/compiler.h>
34#include <linux/ioctl.h> 32#include <linux/ioctl.h>
35#include <linux/poll.h> 33#include <linux/poll.h>
@@ -50,8 +48,8 @@
50#define ET61X251_MODULE_AUTHOR "(C) 2006 Luca Risolia" 48#define ET61X251_MODULE_AUTHOR "(C) 2006 Luca Risolia"
51#define ET61X251_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>" 49#define ET61X251_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
52#define ET61X251_MODULE_LICENSE "GPL" 50#define ET61X251_MODULE_LICENSE "GPL"
53#define ET61X251_MODULE_VERSION "1:1.01" 51#define ET61X251_MODULE_VERSION "1:1.02"
54#define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 1) 52#define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 2)
55 53
56/*****************************************************************************/ 54/*****************************************************************************/
57 55
@@ -90,6 +88,16 @@ MODULE_PARM_DESC(force_munmap,
90 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"." 88 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
91 "\n"); 89 "\n");
92 90
91static unsigned int frame_timeout[] = {[0 ... ET61X251_MAX_DEVICES-1] =
92 ET61X251_FRAME_TIMEOUT};
93module_param_array(frame_timeout, uint, NULL, 0644);
94MODULE_PARM_DESC(frame_timeout,
95 "\n<n[,...]> Timeout for a video frame in seconds."
96 "\nThis parameter is specific for each detected camera."
97 "\nDefault value is "
98 __MODULE_STRING(ET61X251_FRAME_TIMEOUT)"."
99 "\n");
100
93#ifdef ET61X251_DEBUG 101#ifdef ET61X251_DEBUG
94static unsigned short debug = ET61X251_DEBUG_LEVEL; 102static unsigned short debug = ET61X251_DEBUG_LEVEL;
95module_param(debug, ushort, 0644); 103module_param(debug, ushort, 0644);
@@ -111,8 +119,8 @@ static u32
111et61x251_request_buffers(struct et61x251_device* cam, u32 count, 119et61x251_request_buffers(struct et61x251_device* cam, u32 count,
112 enum et61x251_io_method io) 120 enum et61x251_io_method io)
113{ 121{
114 struct v4l2_pix_format* p = &(cam->sensor->pix_format); 122 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
115 struct v4l2_rect* r = &(cam->sensor->cropcap.bounds); 123 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
116 const size_t imagesize = cam->module_param.force_munmap || 124 const size_t imagesize = cam->module_param.force_munmap ||
117 io == IO_READ ? 125 io == IO_READ ?
118 (p->width * p->height * p->priv) / 8 : 126 (p->width * p->height * p->priv) / 8 :
@@ -268,8 +276,8 @@ et61x251_i2c_try_read(struct et61x251_device* cam,
268 int err = 0, res; 276 int err = 0, res;
269 277
270 data[0] = address; 278 data[0] = address;
271 data[1] = cam->sensor->i2c_slave_id; 279 data[1] = cam->sensor.i2c_slave_id;
272 data[2] = cam->sensor->rsta | 0x10; 280 data[2] = cam->sensor.rsta | 0x10;
273 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02); 281 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
274 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41, 282 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
275 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT); 283 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
@@ -301,8 +309,8 @@ et61x251_i2c_try_write(struct et61x251_device* cam,
301 int err = 0, res; 309 int err = 0, res;
302 310
303 data[0] = address; 311 data[0] = address;
304 data[1] = cam->sensor->i2c_slave_id; 312 data[1] = cam->sensor.i2c_slave_id;
305 data[2] = cam->sensor->rsta | 0x12; 313 data[2] = cam->sensor.rsta | 0x12;
306 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41, 314 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
307 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT); 315 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
308 if (res < 0) 316 if (res < 0)
@@ -334,9 +342,6 @@ et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
334 u8* data = cam->control_buffer; 342 u8* data = cam->control_buffer;
335 int err = 0, res; 343 int err = 0, res;
336 344
337 if (!cam->sensor)
338 return -1;
339
340 data[0] = data2; 345 data[0] = data2;
341 data[1] = data3; 346 data[1] = data3;
342 data[2] = data4; 347 data[2] = data4;
@@ -350,8 +355,8 @@ et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
350 err += res; 355 err += res;
351 356
352 data[0] = address; 357 data[0] = address;
353 data[1] = cam->sensor->i2c_slave_id; 358 data[1] = cam->sensor.i2c_slave_id;
354 data[2] = cam->sensor->rsta | 0x02 | (n << 4); 359 data[2] = cam->sensor.rsta | 0x02 | (n << 4);
355 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41, 360 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
356 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT); 361 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
357 if (res < 0) 362 if (res < 0)
@@ -364,11 +369,11 @@ et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
364 if (res < 0) 369 if (res < 0)
365 err += res; 370 err += res;
366 371
367 err += et61x251_i2c_wait(cam, cam->sensor); 372 err += et61x251_i2c_wait(cam, &cam->sensor);
368 373
369 if (err) 374 if (err)
370 DBG(3, "I2C raw write failed for %s image sensor", 375 DBG(3, "I2C raw write failed for %s image sensor",
371 cam->sensor->name); 376 cam->sensor.name);
372 377
373 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, " 378 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
374 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X," 379 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
@@ -382,19 +387,13 @@ et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
382 387
383int et61x251_i2c_read(struct et61x251_device* cam, u8 address) 388int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
384{ 389{
385 if (!cam->sensor) 390 return et61x251_i2c_try_read(cam, &cam->sensor, address);
386 return -1;
387
388 return et61x251_i2c_try_read(cam, cam->sensor, address);
389} 391}
390 392
391 393
392int et61x251_i2c_write(struct et61x251_device* cam, u8 address, u8 value) 394int et61x251_i2c_write(struct et61x251_device* cam, u8 address, u8 value)
393{ 395{
394 if (!cam->sensor) 396 return et61x251_i2c_try_write(cam, &cam->sensor, address, value);
395 return -1;
396
397 return et61x251_i2c_try_write(cam, cam->sensor, address, value);
398} 397}
399 398
400/*****************************************************************************/ 399/*****************************************************************************/
@@ -417,7 +416,7 @@ static void et61x251_urb_complete(struct urb *urb, struct pt_regs* regs)
417 if ((*f)) 416 if ((*f))
418 (*f)->state = F_QUEUED; 417 (*f)->state = F_QUEUED;
419 DBG(3, "Stream interrupted"); 418 DBG(3, "Stream interrupted");
420 wake_up_interruptible(&cam->wait_stream); 419 wake_up(&cam->wait_stream);
421 } 420 }
422 421
423 if (cam->state & DEV_DISCONNECTED) 422 if (cam->state & DEV_DISCONNECTED)
@@ -435,9 +434,9 @@ static void et61x251_urb_complete(struct urb *urb, struct pt_regs* regs)
435 (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t, 434 (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t,
436 frame); 435 frame);
437 436
438 imagesize = (cam->sensor->pix_format.width * 437 imagesize = (cam->sensor.pix_format.width *
439 cam->sensor->pix_format.height * 438 cam->sensor.pix_format.height *
440 cam->sensor->pix_format.priv) / 8; 439 cam->sensor.pix_format.priv) / 8;
441 440
442 for (i = 0; i < urb->number_of_packets; i++) { 441 for (i = 0; i < urb->number_of_packets; i++) {
443 unsigned int len, status; 442 unsigned int len, status;
@@ -476,7 +475,7 @@ start_of_frame:
476 475
477 if ((*f)->state == F_GRABBING) { 476 if ((*f)->state == F_GRABBING) {
478 if (sof && (*f)->buf.bytesused) { 477 if (sof && (*f)->buf.bytesused) {
479 if (cam->sensor->pix_format.pixelformat == 478 if (cam->sensor.pix_format.pixelformat ==
480 V4L2_PIX_FMT_ET61X251) 479 V4L2_PIX_FMT_ET61X251)
481 goto end_of_frame; 480 goto end_of_frame;
482 else { 481 else {
@@ -521,7 +520,7 @@ end_of_frame:
521 goto resubmit_urb; 520 goto resubmit_urb;
522 521
523 if (sof && 522 if (sof &&
524 cam->sensor->pix_format.pixelformat == 523 cam->sensor.pix_format.pixelformat ==
525 V4L2_PIX_FMT_ET61X251) 524 V4L2_PIX_FMT_ET61X251)
526 goto start_of_frame; 525 goto start_of_frame;
527 } 526 }
@@ -650,21 +649,21 @@ static int et61x251_stop_transfer(struct et61x251_device* cam)
650 649
651static int et61x251_stream_interrupt(struct et61x251_device* cam) 650static int et61x251_stream_interrupt(struct et61x251_device* cam)
652{ 651{
653 int err = 0; 652 long timeout;
654 653
655 cam->stream = STREAM_INTERRUPT; 654 cam->stream = STREAM_INTERRUPT;
656 err = wait_event_timeout(cam->wait_stream, 655 timeout = wait_event_timeout(cam->wait_stream,
657 (cam->stream == STREAM_OFF) || 656 (cam->stream == STREAM_OFF) ||
658 (cam->state & DEV_DISCONNECTED), 657 (cam->state & DEV_DISCONNECTED),
659 ET61X251_URB_TIMEOUT); 658 ET61X251_URB_TIMEOUT);
660 if (cam->state & DEV_DISCONNECTED) 659 if (cam->state & DEV_DISCONNECTED)
661 return -ENODEV; 660 return -ENODEV;
662 else if (err) { 661 else if (cam->stream != STREAM_OFF) {
663 cam->state |= DEV_MISCONFIGURED; 662 cam->state |= DEV_MISCONFIGURED;
664 DBG(1, "URB timeout reached. The camera is misconfigured. To " 663 DBG(1, "URB timeout reached. The camera is misconfigured. To "
665 "use it, close and open /dev/video%d again.", 664 "use it, close and open /dev/video%d again.",
666 cam->v4ldev->minor); 665 cam->v4ldev->minor);
667 return err; 666 return -EIO;
668 } 667 }
669 668
670 return 0; 669 return 0;
@@ -709,18 +708,18 @@ static ssize_t et61x251_show_reg(struct class_device* cd, char* buf)
709 struct et61x251_device* cam; 708 struct et61x251_device* cam;
710 ssize_t count; 709 ssize_t count;
711 710
712 if (down_interruptible(&et61x251_sysfs_lock)) 711 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
713 return -ERESTARTSYS; 712 return -ERESTARTSYS;
714 713
715 cam = video_get_drvdata(to_video_device(cd)); 714 cam = video_get_drvdata(to_video_device(cd));
716 if (!cam) { 715 if (!cam) {
717 up(&et61x251_sysfs_lock); 716 mutex_unlock(&et61x251_sysfs_lock);
718 return -ENODEV; 717 return -ENODEV;
719 } 718 }
720 719
721 count = sprintf(buf, "%u\n", cam->sysfs.reg); 720 count = sprintf(buf, "%u\n", cam->sysfs.reg);
722 721
723 up(&et61x251_sysfs_lock); 722 mutex_unlock(&et61x251_sysfs_lock);
724 723
725 return count; 724 return count;
726} 725}
@@ -733,18 +732,18 @@ et61x251_store_reg(struct class_device* cd, const char* buf, size_t len)
733 u8 index; 732 u8 index;
734 ssize_t count; 733 ssize_t count;
735 734
736 if (down_interruptible(&et61x251_sysfs_lock)) 735 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
737 return -ERESTARTSYS; 736 return -ERESTARTSYS;
738 737
739 cam = video_get_drvdata(to_video_device(cd)); 738 cam = video_get_drvdata(to_video_device(cd));
740 if (!cam) { 739 if (!cam) {
741 up(&et61x251_sysfs_lock); 740 mutex_unlock(&et61x251_sysfs_lock);
742 return -ENODEV; 741 return -ENODEV;
743 } 742 }
744 743
745 index = et61x251_strtou8(buf, len, &count); 744 index = et61x251_strtou8(buf, len, &count);
746 if (index > 0x8e || !count) { 745 if (index > 0x8e || !count) {
747 up(&et61x251_sysfs_lock); 746 mutex_unlock(&et61x251_sysfs_lock);
748 return -EINVAL; 747 return -EINVAL;
749 } 748 }
750 749
@@ -753,7 +752,7 @@ et61x251_store_reg(struct class_device* cd, const char* buf, size_t len)
753 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg); 752 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg);
754 DBG(3, "Written bytes: %zd", count); 753 DBG(3, "Written bytes: %zd", count);
755 754
756 up(&et61x251_sysfs_lock); 755 mutex_unlock(&et61x251_sysfs_lock);
757 756
758 return count; 757 return count;
759} 758}
@@ -765,17 +764,17 @@ static ssize_t et61x251_show_val(struct class_device* cd, char* buf)
765 ssize_t count; 764 ssize_t count;
766 int val; 765 int val;
767 766
768 if (down_interruptible(&et61x251_sysfs_lock)) 767 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
769 return -ERESTARTSYS; 768 return -ERESTARTSYS;
770 769
771 cam = video_get_drvdata(to_video_device(cd)); 770 cam = video_get_drvdata(to_video_device(cd));
772 if (!cam) { 771 if (!cam) {
773 up(&et61x251_sysfs_lock); 772 mutex_unlock(&et61x251_sysfs_lock);
774 return -ENODEV; 773 return -ENODEV;
775 } 774 }
776 775
777 if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) { 776 if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) {
778 up(&et61x251_sysfs_lock); 777 mutex_unlock(&et61x251_sysfs_lock);
779 return -EIO; 778 return -EIO;
780 } 779 }
781 780
@@ -783,7 +782,7 @@ static ssize_t et61x251_show_val(struct class_device* cd, char* buf)
783 782
784 DBG(3, "Read bytes: %zd", count); 783 DBG(3, "Read bytes: %zd", count);
785 784
786 up(&et61x251_sysfs_lock); 785 mutex_unlock(&et61x251_sysfs_lock);
787 786
788 return count; 787 return count;
789} 788}
@@ -797,24 +796,24 @@ et61x251_store_val(struct class_device* cd, const char* buf, size_t len)
797 ssize_t count; 796 ssize_t count;
798 int err; 797 int err;
799 798
800 if (down_interruptible(&et61x251_sysfs_lock)) 799 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
801 return -ERESTARTSYS; 800 return -ERESTARTSYS;
802 801
803 cam = video_get_drvdata(to_video_device(cd)); 802 cam = video_get_drvdata(to_video_device(cd));
804 if (!cam) { 803 if (!cam) {
805 up(&et61x251_sysfs_lock); 804 mutex_unlock(&et61x251_sysfs_lock);
806 return -ENODEV; 805 return -ENODEV;
807 } 806 }
808 807
809 value = et61x251_strtou8(buf, len, &count); 808 value = et61x251_strtou8(buf, len, &count);
810 if (!count) { 809 if (!count) {
811 up(&et61x251_sysfs_lock); 810 mutex_unlock(&et61x251_sysfs_lock);
812 return -EINVAL; 811 return -EINVAL;
813 } 812 }
814 813
815 err = et61x251_write_reg(cam, value, cam->sysfs.reg); 814 err = et61x251_write_reg(cam, value, cam->sysfs.reg);
816 if (err) { 815 if (err) {
817 up(&et61x251_sysfs_lock); 816 mutex_unlock(&et61x251_sysfs_lock);
818 return -EIO; 817 return -EIO;
819 } 818 }
820 819
@@ -822,7 +821,7 @@ et61x251_store_val(struct class_device* cd, const char* buf, size_t len)
822 cam->sysfs.reg, value); 821 cam->sysfs.reg, value);
823 DBG(3, "Written bytes: %zd", count); 822 DBG(3, "Written bytes: %zd", count);
824 823
825 up(&et61x251_sysfs_lock); 824 mutex_unlock(&et61x251_sysfs_lock);
826 825
827 return count; 826 return count;
828} 827}
@@ -833,12 +832,12 @@ static ssize_t et61x251_show_i2c_reg(struct class_device* cd, char* buf)
833 struct et61x251_device* cam; 832 struct et61x251_device* cam;
834 ssize_t count; 833 ssize_t count;
835 834
836 if (down_interruptible(&et61x251_sysfs_lock)) 835 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
837 return -ERESTARTSYS; 836 return -ERESTARTSYS;
838 837
839 cam = video_get_drvdata(to_video_device(cd)); 838 cam = video_get_drvdata(to_video_device(cd));
840 if (!cam) { 839 if (!cam) {
841 up(&et61x251_sysfs_lock); 840 mutex_unlock(&et61x251_sysfs_lock);
842 return -ENODEV; 841 return -ENODEV;
843 } 842 }
844 843
@@ -846,7 +845,7 @@ static ssize_t et61x251_show_i2c_reg(struct class_device* cd, char* buf)
846 845
847 DBG(3, "Read bytes: %zd", count); 846 DBG(3, "Read bytes: %zd", count);
848 847
849 up(&et61x251_sysfs_lock); 848 mutex_unlock(&et61x251_sysfs_lock);
850 849
851 return count; 850 return count;
852} 851}
@@ -859,18 +858,18 @@ et61x251_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
859 u8 index; 858 u8 index;
860 ssize_t count; 859 ssize_t count;
861 860
862 if (down_interruptible(&et61x251_sysfs_lock)) 861 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
863 return -ERESTARTSYS; 862 return -ERESTARTSYS;
864 863
865 cam = video_get_drvdata(to_video_device(cd)); 864 cam = video_get_drvdata(to_video_device(cd));
866 if (!cam) { 865 if (!cam) {
867 up(&et61x251_sysfs_lock); 866 mutex_unlock(&et61x251_sysfs_lock);
868 return -ENODEV; 867 return -ENODEV;
869 } 868 }
870 869
871 index = et61x251_strtou8(buf, len, &count); 870 index = et61x251_strtou8(buf, len, &count);
872 if (!count) { 871 if (!count) {
873 up(&et61x251_sysfs_lock); 872 mutex_unlock(&et61x251_sysfs_lock);
874 return -EINVAL; 873 return -EINVAL;
875 } 874 }
876 875
@@ -879,7 +878,7 @@ et61x251_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
879 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg); 878 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
880 DBG(3, "Written bytes: %zd", count); 879 DBG(3, "Written bytes: %zd", count);
881 880
882 up(&et61x251_sysfs_lock); 881 mutex_unlock(&et61x251_sysfs_lock);
883 882
884 return count; 883 return count;
885} 884}
@@ -891,22 +890,22 @@ static ssize_t et61x251_show_i2c_val(struct class_device* cd, char* buf)
891 ssize_t count; 890 ssize_t count;
892 int val; 891 int val;
893 892
894 if (down_interruptible(&et61x251_sysfs_lock)) 893 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
895 return -ERESTARTSYS; 894 return -ERESTARTSYS;
896 895
897 cam = video_get_drvdata(to_video_device(cd)); 896 cam = video_get_drvdata(to_video_device(cd));
898 if (!cam) { 897 if (!cam) {
899 up(&et61x251_sysfs_lock); 898 mutex_unlock(&et61x251_sysfs_lock);
900 return -ENODEV; 899 return -ENODEV;
901 } 900 }
902 901
903 if (!(cam->sensor->sysfs_ops & ET61X251_I2C_READ)) { 902 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
904 up(&et61x251_sysfs_lock); 903 mutex_unlock(&et61x251_sysfs_lock);
905 return -ENOSYS; 904 return -ENOSYS;
906 } 905 }
907 906
908 if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) { 907 if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
909 up(&et61x251_sysfs_lock); 908 mutex_unlock(&et61x251_sysfs_lock);
910 return -EIO; 909 return -EIO;
911 } 910 }
912 911
@@ -914,7 +913,7 @@ static ssize_t et61x251_show_i2c_val(struct class_device* cd, char* buf)
914 913
915 DBG(3, "Read bytes: %zd", count); 914 DBG(3, "Read bytes: %zd", count);
916 915
917 up(&et61x251_sysfs_lock); 916 mutex_unlock(&et61x251_sysfs_lock);
918 917
919 return count; 918 return count;
920} 919}
@@ -928,29 +927,29 @@ et61x251_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
928 ssize_t count; 927 ssize_t count;
929 int err; 928 int err;
930 929
931 if (down_interruptible(&et61x251_sysfs_lock)) 930 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
932 return -ERESTARTSYS; 931 return -ERESTARTSYS;
933 932
934 cam = video_get_drvdata(to_video_device(cd)); 933 cam = video_get_drvdata(to_video_device(cd));
935 if (!cam) { 934 if (!cam) {
936 up(&et61x251_sysfs_lock); 935 mutex_unlock(&et61x251_sysfs_lock);
937 return -ENODEV; 936 return -ENODEV;
938 } 937 }
939 938
940 if (!(cam->sensor->sysfs_ops & ET61X251_I2C_READ)) { 939 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
941 up(&et61x251_sysfs_lock); 940 mutex_unlock(&et61x251_sysfs_lock);
942 return -ENOSYS; 941 return -ENOSYS;
943 } 942 }
944 943
945 value = et61x251_strtou8(buf, len, &count); 944 value = et61x251_strtou8(buf, len, &count);
946 if (!count) { 945 if (!count) {
947 up(&et61x251_sysfs_lock); 946 mutex_unlock(&et61x251_sysfs_lock);
948 return -EINVAL; 947 return -EINVAL;
949 } 948 }
950 949
951 err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value); 950 err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value);
952 if (err) { 951 if (err) {
953 up(&et61x251_sysfs_lock); 952 mutex_unlock(&et61x251_sysfs_lock);
954 return -EIO; 953 return -EIO;
955 } 954 }
956 955
@@ -958,7 +957,7 @@ et61x251_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
958 cam->sysfs.i2c_reg, value); 957 cam->sysfs.i2c_reg, value);
959 DBG(3, "Written bytes: %zd", count); 958 DBG(3, "Written bytes: %zd", count);
960 959
961 up(&et61x251_sysfs_lock); 960 mutex_unlock(&et61x251_sysfs_lock);
962 961
963 return count; 962 return count;
964} 963}
@@ -980,7 +979,7 @@ static void et61x251_create_sysfs(struct et61x251_device* cam)
980 979
981 video_device_create_file(v4ldev, &class_device_attr_reg); 980 video_device_create_file(v4ldev, &class_device_attr_reg);
982 video_device_create_file(v4ldev, &class_device_attr_val); 981 video_device_create_file(v4ldev, &class_device_attr_val);
983 if (cam->sensor && cam->sensor->sysfs_ops) { 982 if (cam->sensor.sysfs_ops) {
984 video_device_create_file(v4ldev, &class_device_attr_i2c_reg); 983 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
985 video_device_create_file(v4ldev, &class_device_attr_i2c_val); 984 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
986 } 985 }
@@ -1048,7 +1047,7 @@ static int et61x251_set_scale(struct et61x251_device* cam, u8 scale)
1048static int 1047static int
1049et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect) 1048et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1050{ 1049{
1051 struct et61x251_sensor* s = cam->sensor; 1050 struct et61x251_sensor* s = &cam->sensor;
1052 u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left + 1051 u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left +
1053 s->active_pixel.left), 1052 s->active_pixel.left),
1054 fmw_sy = (u16)(rect->top - s->cropcap.bounds.top + 1053 fmw_sy = (u16)(rect->top - s->cropcap.bounds.top +
@@ -1076,7 +1075,7 @@ et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1076 1075
1077static int et61x251_init(struct et61x251_device* cam) 1076static int et61x251_init(struct et61x251_device* cam)
1078{ 1077{
1079 struct et61x251_sensor* s = cam->sensor; 1078 struct et61x251_sensor* s = &cam->sensor;
1080 struct v4l2_control ctrl; 1079 struct v4l2_control ctrl;
1081 struct v4l2_queryctrl *qctrl; 1080 struct v4l2_queryctrl *qctrl;
1082 struct v4l2_rect* rect; 1081 struct v4l2_rect* rect;
@@ -1143,7 +1142,7 @@ static int et61x251_init(struct et61x251_device* cam)
1143 } 1142 }
1144 1143
1145 if (!(cam->state & DEV_INITIALIZED)) { 1144 if (!(cam->state & DEV_INITIALIZED)) {
1146 init_MUTEX(&cam->fileop_sem); 1145 mutex_init(&cam->fileop_mutex);
1147 spin_lock_init(&cam->queue_lock); 1146 spin_lock_init(&cam->queue_lock);
1148 init_waitqueue_head(&cam->wait_frame); 1147 init_waitqueue_head(&cam->wait_frame);
1149 init_waitqueue_head(&cam->wait_stream); 1148 init_waitqueue_head(&cam->wait_stream);
@@ -1161,13 +1160,15 @@ static int et61x251_init(struct et61x251_device* cam)
1161 1160
1162static void et61x251_release_resources(struct et61x251_device* cam) 1161static void et61x251_release_resources(struct et61x251_device* cam)
1163{ 1162{
1164 down(&et61x251_sysfs_lock); 1163 mutex_lock(&et61x251_sysfs_lock);
1165 1164
1166 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor); 1165 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1167 video_set_drvdata(cam->v4ldev, NULL); 1166 video_set_drvdata(cam->v4ldev, NULL);
1168 video_unregister_device(cam->v4ldev); 1167 video_unregister_device(cam->v4ldev);
1169 1168
1170 up(&et61x251_sysfs_lock); 1169 usb_put_dev(cam->usbdev);
1170
1171 mutex_unlock(&et61x251_sysfs_lock);
1171 1172
1172 kfree(cam->control_buffer); 1173 kfree(cam->control_buffer);
1173} 1174}
@@ -1188,7 +1189,7 @@ static int et61x251_open(struct inode* inode, struct file* filp)
1188 1189
1189 cam = video_get_drvdata(video_devdata(filp)); 1190 cam = video_get_drvdata(video_devdata(filp));
1190 1191
1191 if (down_interruptible(&cam->dev_sem)) { 1192 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1192 up_read(&et61x251_disconnect); 1193 up_read(&et61x251_disconnect);
1193 return -ERESTARTSYS; 1194 return -ERESTARTSYS;
1194 } 1195 }
@@ -1200,7 +1201,7 @@ static int et61x251_open(struct inode* inode, struct file* filp)
1200 err = -EWOULDBLOCK; 1201 err = -EWOULDBLOCK;
1201 goto out; 1202 goto out;
1202 } 1203 }
1203 up(&cam->dev_sem); 1204 mutex_unlock(&cam->dev_mutex);
1204 err = wait_event_interruptible_exclusive(cam->open, 1205 err = wait_event_interruptible_exclusive(cam->open,
1205 cam->state & DEV_DISCONNECTED 1206 cam->state & DEV_DISCONNECTED
1206 || !cam->users); 1207 || !cam->users);
@@ -1212,7 +1213,7 @@ static int et61x251_open(struct inode* inode, struct file* filp)
1212 up_read(&et61x251_disconnect); 1213 up_read(&et61x251_disconnect);
1213 return -ENODEV; 1214 return -ENODEV;
1214 } 1215 }
1215 down(&cam->dev_sem); 1216 mutex_lock(&cam->dev_mutex);
1216 } 1217 }
1217 1218
1218 1219
@@ -1240,7 +1241,7 @@ static int et61x251_open(struct inode* inode, struct file* filp)
1240 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor); 1241 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1241 1242
1242out: 1243out:
1243 up(&cam->dev_sem); 1244 mutex_unlock(&cam->dev_mutex);
1244 up_read(&et61x251_disconnect); 1245 up_read(&et61x251_disconnect);
1245 return err; 1246 return err;
1246} 1247}
@@ -1250,7 +1251,7 @@ static int et61x251_release(struct inode* inode, struct file* filp)
1250{ 1251{
1251 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp)); 1252 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1252 1253
1253 down(&cam->dev_sem); /* prevent disconnect() to be called */ 1254 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1254 1255
1255 et61x251_stop_transfer(cam); 1256 et61x251_stop_transfer(cam);
1256 1257
@@ -1258,7 +1259,7 @@ static int et61x251_release(struct inode* inode, struct file* filp)
1258 1259
1259 if (cam->state & DEV_DISCONNECTED) { 1260 if (cam->state & DEV_DISCONNECTED) {
1260 et61x251_release_resources(cam); 1261 et61x251_release_resources(cam);
1261 up(&cam->dev_sem); 1262 mutex_unlock(&cam->dev_mutex);
1262 kfree(cam); 1263 kfree(cam);
1263 return 0; 1264 return 0;
1264 } 1265 }
@@ -1268,7 +1269,7 @@ static int et61x251_release(struct inode* inode, struct file* filp)
1268 1269
1269 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor); 1270 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1270 1271
1271 up(&cam->dev_sem); 1272 mutex_unlock(&cam->dev_mutex);
1272 1273
1273 return 0; 1274 return 0;
1274} 1275}
@@ -1281,28 +1282,29 @@ et61x251_read(struct file* filp, char __user * buf,
1281 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp)); 1282 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1282 struct et61x251_frame_t* f, * i; 1283 struct et61x251_frame_t* f, * i;
1283 unsigned long lock_flags; 1284 unsigned long lock_flags;
1285 long timeout;
1284 int err = 0; 1286 int err = 0;
1285 1287
1286 if (down_interruptible(&cam->fileop_sem)) 1288 if (mutex_lock_interruptible(&cam->fileop_mutex))
1287 return -ERESTARTSYS; 1289 return -ERESTARTSYS;
1288 1290
1289 if (cam->state & DEV_DISCONNECTED) { 1291 if (cam->state & DEV_DISCONNECTED) {
1290 DBG(1, "Device not present"); 1292 DBG(1, "Device not present");
1291 up(&cam->fileop_sem); 1293 mutex_unlock(&cam->fileop_mutex);
1292 return -ENODEV; 1294 return -ENODEV;
1293 } 1295 }
1294 1296
1295 if (cam->state & DEV_MISCONFIGURED) { 1297 if (cam->state & DEV_MISCONFIGURED) {
1296 DBG(1, "The camera is misconfigured. Close and open it " 1298 DBG(1, "The camera is misconfigured. Close and open it "
1297 "again."); 1299 "again.");
1298 up(&cam->fileop_sem); 1300 mutex_unlock(&cam->fileop_mutex);
1299 return -EIO; 1301 return -EIO;
1300 } 1302 }
1301 1303
1302 if (cam->io == IO_MMAP) { 1304 if (cam->io == IO_MMAP) {
1303 DBG(3, "Close and open the device again to choose the read " 1305 DBG(3, "Close and open the device again to choose the read "
1304 "method"); 1306 "method");
1305 up(&cam->fileop_sem); 1307 mutex_unlock(&cam->fileop_mutex);
1306 return -EINVAL; 1308 return -EINVAL;
1307 } 1309 }
1308 1310
@@ -1310,7 +1312,7 @@ et61x251_read(struct file* filp, char __user * buf,
1310 if (!et61x251_request_buffers(cam, cam->nreadbuffers, 1312 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1311 IO_READ)) { 1313 IO_READ)) {
1312 DBG(1, "read() failed, not enough memory"); 1314 DBG(1, "read() failed, not enough memory");
1313 up(&cam->fileop_sem); 1315 mutex_unlock(&cam->fileop_mutex);
1314 return -ENOMEM; 1316 return -ENOMEM;
1315 } 1317 }
1316 cam->io = IO_READ; 1318 cam->io = IO_READ;
@@ -1324,30 +1326,32 @@ et61x251_read(struct file* filp, char __user * buf,
1324 } 1326 }
1325 1327
1326 if (!count) { 1328 if (!count) {
1327 up(&cam->fileop_sem); 1329 mutex_unlock(&cam->fileop_mutex);
1328 return 0; 1330 return 0;
1329 } 1331 }
1330 1332
1331 if (list_empty(&cam->outqueue)) { 1333 if (list_empty(&cam->outqueue)) {
1332 if (filp->f_flags & O_NONBLOCK) { 1334 if (filp->f_flags & O_NONBLOCK) {
1333 up(&cam->fileop_sem); 1335 mutex_unlock(&cam->fileop_mutex);
1334 return -EAGAIN; 1336 return -EAGAIN;
1335 } 1337 }
1336 err = wait_event_interruptible 1338 timeout = wait_event_interruptible_timeout
1337 ( cam->wait_frame, 1339 ( cam->wait_frame,
1338 (!list_empty(&cam->outqueue)) || 1340 (!list_empty(&cam->outqueue)) ||
1339 (cam->state & DEV_DISCONNECTED) || 1341 (cam->state & DEV_DISCONNECTED) ||
1340 (cam->state & DEV_MISCONFIGURED) ); 1342 (cam->state & DEV_MISCONFIGURED),
1341 if (err) { 1343 cam->module_param.frame_timeout *
1342 up(&cam->fileop_sem); 1344 1000 * msecs_to_jiffies(1) );
1343 return err; 1345 if (timeout < 0) {
1346 mutex_unlock(&cam->fileop_mutex);
1347 return timeout;
1344 } 1348 }
1345 if (cam->state & DEV_DISCONNECTED) { 1349 if (cam->state & DEV_DISCONNECTED) {
1346 up(&cam->fileop_sem); 1350 mutex_unlock(&cam->fileop_mutex);
1347 return -ENODEV; 1351 return -ENODEV;
1348 } 1352 }
1349 if (cam->state & DEV_MISCONFIGURED) { 1353 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
1350 up(&cam->fileop_sem); 1354 mutex_unlock(&cam->fileop_mutex);
1351 return -EIO; 1355 return -EIO;
1352 } 1356 }
1353 } 1357 }
@@ -1375,7 +1379,7 @@ exit:
1375 PDBGG("Frame #%lu, bytes read: %zu", 1379 PDBGG("Frame #%lu, bytes read: %zu",
1376 (unsigned long)f->buf.index, count); 1380 (unsigned long)f->buf.index, count);
1377 1381
1378 up(&cam->fileop_sem); 1382 mutex_unlock(&cam->fileop_mutex);
1379 1383
1380 return err ? err : count; 1384 return err ? err : count;
1381} 1385}
@@ -1388,7 +1392,7 @@ static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1388 unsigned long lock_flags; 1392 unsigned long lock_flags;
1389 unsigned int mask = 0; 1393 unsigned int mask = 0;
1390 1394
1391 if (down_interruptible(&cam->fileop_sem)) 1395 if (mutex_lock_interruptible(&cam->fileop_mutex))
1392 return POLLERR; 1396 return POLLERR;
1393 1397
1394 if (cam->state & DEV_DISCONNECTED) { 1398 if (cam->state & DEV_DISCONNECTED) {
@@ -1426,12 +1430,12 @@ static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1426 if (!list_empty(&cam->outqueue)) 1430 if (!list_empty(&cam->outqueue))
1427 mask |= POLLIN | POLLRDNORM; 1431 mask |= POLLIN | POLLRDNORM;
1428 1432
1429 up(&cam->fileop_sem); 1433 mutex_unlock(&cam->fileop_mutex);
1430 1434
1431 return mask; 1435 return mask;
1432 1436
1433error: 1437error:
1434 up(&cam->fileop_sem); 1438 mutex_unlock(&cam->fileop_mutex);
1435 return POLLERR; 1439 return POLLERR;
1436} 1440}
1437 1441
@@ -1465,25 +1469,25 @@ static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1465 void *pos; 1469 void *pos;
1466 u32 i; 1470 u32 i;
1467 1471
1468 if (down_interruptible(&cam->fileop_sem)) 1472 if (mutex_lock_interruptible(&cam->fileop_mutex))
1469 return -ERESTARTSYS; 1473 return -ERESTARTSYS;
1470 1474
1471 if (cam->state & DEV_DISCONNECTED) { 1475 if (cam->state & DEV_DISCONNECTED) {
1472 DBG(1, "Device not present"); 1476 DBG(1, "Device not present");
1473 up(&cam->fileop_sem); 1477 mutex_unlock(&cam->fileop_mutex);
1474 return -ENODEV; 1478 return -ENODEV;
1475 } 1479 }
1476 1480
1477 if (cam->state & DEV_MISCONFIGURED) { 1481 if (cam->state & DEV_MISCONFIGURED) {
1478 DBG(1, "The camera is misconfigured. Close and open it " 1482 DBG(1, "The camera is misconfigured. Close and open it "
1479 "again."); 1483 "again.");
1480 up(&cam->fileop_sem); 1484 mutex_unlock(&cam->fileop_mutex);
1481 return -EIO; 1485 return -EIO;
1482 } 1486 }
1483 1487
1484 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) || 1488 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1485 size != PAGE_ALIGN(cam->frame[0].buf.length)) { 1489 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1486 up(&cam->fileop_sem); 1490 mutex_unlock(&cam->fileop_mutex);
1487 return -EINVAL; 1491 return -EINVAL;
1488 } 1492 }
1489 1493
@@ -1492,7 +1496,7 @@ static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1492 break; 1496 break;
1493 } 1497 }
1494 if (i == cam->nbuffers) { 1498 if (i == cam->nbuffers) {
1495 up(&cam->fileop_sem); 1499 mutex_unlock(&cam->fileop_mutex);
1496 return -EINVAL; 1500 return -EINVAL;
1497 } 1501 }
1498 1502
@@ -1502,7 +1506,7 @@ static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1502 pos = cam->frame[i].bufmem; 1506 pos = cam->frame[i].bufmem;
1503 while (size > 0) { /* size is page-aligned */ 1507 while (size > 0) { /* size is page-aligned */
1504 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) { 1508 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1505 up(&cam->fileop_sem); 1509 mutex_unlock(&cam->fileop_mutex);
1506 return -EAGAIN; 1510 return -EAGAIN;
1507 } 1511 }
1508 start += PAGE_SIZE; 1512 start += PAGE_SIZE;
@@ -1515,7 +1519,7 @@ static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1515 1519
1516 et61x251_vm_open(vma); 1520 et61x251_vm_open(vma);
1517 1521
1518 up(&cam->fileop_sem); 1522 mutex_unlock(&cam->fileop_mutex);
1519 1523
1520 return 0; 1524 return 0;
1521} 1525}
@@ -1557,6 +1561,7 @@ et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1557 1561
1558 memset(&i, 0, sizeof(i)); 1562 memset(&i, 0, sizeof(i));
1559 strcpy(i.name, "Camera"); 1563 strcpy(i.name, "Camera");
1564 i.type = V4L2_INPUT_TYPE_CAMERA;
1560 1565
1561 if (copy_to_user(arg, &i, sizeof(i))) 1566 if (copy_to_user(arg, &i, sizeof(i)))
1562 return -EFAULT; 1567 return -EFAULT;
@@ -1566,7 +1571,19 @@ et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1566 1571
1567 1572
1568static int 1573static int
1569et61x251_vidioc_gs_input(struct et61x251_device* cam, void __user * arg) 1574et61x251_vidioc_g_input(struct et61x251_device* cam, void __user * arg)
1575{
1576 int index = 0;
1577
1578 if (copy_to_user(arg, &index, sizeof(index)))
1579 return -EFAULT;
1580
1581 return 0;
1582}
1583
1584
1585static int
1586et61x251_vidioc_s_input(struct et61x251_device* cam, void __user * arg)
1570{ 1587{
1571 int index; 1588 int index;
1572 1589
@@ -1583,7 +1600,7 @@ et61x251_vidioc_gs_input(struct et61x251_device* cam, void __user * arg)
1583static int 1600static int
1584et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg) 1601et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1585{ 1602{
1586 struct et61x251_sensor* s = cam->sensor; 1603 struct et61x251_sensor* s = &cam->sensor;
1587 struct v4l2_queryctrl qc; 1604 struct v4l2_queryctrl qc;
1588 u8 i; 1605 u8 i;
1589 1606
@@ -1605,7 +1622,7 @@ et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1605static int 1622static int
1606et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg) 1623et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg)
1607{ 1624{
1608 struct et61x251_sensor* s = cam->sensor; 1625 struct et61x251_sensor* s = &cam->sensor;
1609 struct v4l2_control ctrl; 1626 struct v4l2_control ctrl;
1610 int err = 0; 1627 int err = 0;
1611 u8 i; 1628 u8 i;
@@ -1637,7 +1654,7 @@ exit:
1637static int 1654static int
1638et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg) 1655et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1639{ 1656{
1640 struct et61x251_sensor* s = cam->sensor; 1657 struct et61x251_sensor* s = &cam->sensor;
1641 struct v4l2_control ctrl; 1658 struct v4l2_control ctrl;
1642 u8 i; 1659 u8 i;
1643 int err = 0; 1660 int err = 0;
@@ -1650,6 +1667,8 @@ et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1650 1667
1651 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) 1668 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1652 if (ctrl.id == s->qctrl[i].id) { 1669 if (ctrl.id == s->qctrl[i].id) {
1670 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1671 return -EINVAL;
1653 if (ctrl.value < s->qctrl[i].minimum || 1672 if (ctrl.value < s->qctrl[i].minimum ||
1654 ctrl.value > s->qctrl[i].maximum) 1673 ctrl.value > s->qctrl[i].maximum)
1655 return -ERANGE; 1674 return -ERANGE;
@@ -1669,7 +1688,7 @@ et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1669static int 1688static int
1670et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg) 1689et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1671{ 1690{
1672 struct v4l2_cropcap* cc = &(cam->sensor->cropcap); 1691 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1673 1692
1674 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1693 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1675 cc->pixelaspect.numerator = 1; 1694 cc->pixelaspect.numerator = 1;
@@ -1685,7 +1704,7 @@ et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1685static int 1704static int
1686et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg) 1705et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1687{ 1706{
1688 struct et61x251_sensor* s = cam->sensor; 1707 struct et61x251_sensor* s = &cam->sensor;
1689 struct v4l2_crop crop = { 1708 struct v4l2_crop crop = {
1690 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, 1709 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1691 }; 1710 };
@@ -1702,7 +1721,7 @@ et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1702static int 1721static int
1703et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg) 1722et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg)
1704{ 1723{
1705 struct et61x251_sensor* s = cam->sensor; 1724 struct et61x251_sensor* s = &cam->sensor;
1706 struct v4l2_crop crop; 1725 struct v4l2_crop crop;
1707 struct v4l2_rect* rect; 1726 struct v4l2_rect* rect;
1708 struct v4l2_rect* bounds = &(s->cropcap.bounds); 1727 struct v4l2_rect* bounds = &(s->cropcap.bounds);
@@ -1843,7 +1862,7 @@ static int
1843et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg) 1862et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg)
1844{ 1863{
1845 struct v4l2_format format; 1864 struct v4l2_format format;
1846 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format); 1865 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1847 1866
1848 if (copy_from_user(&format, arg, sizeof(format))) 1867 if (copy_from_user(&format, arg, sizeof(format)))
1849 return -EFAULT; 1868 return -EFAULT;
@@ -1868,7 +1887,7 @@ static int
1868et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd, 1887et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd,
1869 void __user * arg) 1888 void __user * arg)
1870{ 1889{
1871 struct et61x251_sensor* s = cam->sensor; 1890 struct et61x251_sensor* s = &cam->sensor;
1872 struct v4l2_format format; 1891 struct v4l2_format format;
1873 struct v4l2_pix_format* pix; 1892 struct v4l2_pix_format* pix;
1874 struct v4l2_pix_format* pfmt = &(s->pix_format); 1893 struct v4l2_pix_format* pfmt = &(s->pix_format);
@@ -2155,7 +2174,7 @@ et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2155 struct v4l2_buffer b; 2174 struct v4l2_buffer b;
2156 struct et61x251_frame_t *f; 2175 struct et61x251_frame_t *f;
2157 unsigned long lock_flags; 2176 unsigned long lock_flags;
2158 int err = 0; 2177 long timeout;
2159 2178
2160 if (copy_from_user(&b, arg, sizeof(b))) 2179 if (copy_from_user(&b, arg, sizeof(b)))
2161 return -EFAULT; 2180 return -EFAULT;
@@ -2168,16 +2187,18 @@ et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2168 return -EINVAL; 2187 return -EINVAL;
2169 if (filp->f_flags & O_NONBLOCK) 2188 if (filp->f_flags & O_NONBLOCK)
2170 return -EAGAIN; 2189 return -EAGAIN;
2171 err = wait_event_interruptible 2190 timeout = wait_event_interruptible_timeout
2172 ( cam->wait_frame, 2191 ( cam->wait_frame,
2173 (!list_empty(&cam->outqueue)) || 2192 (!list_empty(&cam->outqueue)) ||
2174 (cam->state & DEV_DISCONNECTED) || 2193 (cam->state & DEV_DISCONNECTED) ||
2175 (cam->state & DEV_MISCONFIGURED) ); 2194 (cam->state & DEV_MISCONFIGURED),
2176 if (err) 2195 cam->module_param.frame_timeout *
2177 return err; 2196 1000 * msecs_to_jiffies(1) );
2197 if (timeout < 0)
2198 return timeout;
2178 if (cam->state & DEV_DISCONNECTED) 2199 if (cam->state & DEV_DISCONNECTED)
2179 return -ENODEV; 2200 return -ENODEV;
2180 if (cam->state & DEV_MISCONFIGURED) 2201 if (!timeout || (cam->state & DEV_MISCONFIGURED))
2181 return -EIO; 2202 return -EIO;
2182 } 2203 }
2183 2204
@@ -2309,8 +2330,10 @@ static int et61x251_ioctl_v4l2(struct inode* inode, struct file* filp,
2309 return et61x251_vidioc_enuminput(cam, arg); 2330 return et61x251_vidioc_enuminput(cam, arg);
2310 2331
2311 case VIDIOC_G_INPUT: 2332 case VIDIOC_G_INPUT:
2333 return et61x251_vidioc_g_input(cam, arg);
2334
2312 case VIDIOC_S_INPUT: 2335 case VIDIOC_S_INPUT:
2313 return et61x251_vidioc_gs_input(cam, arg); 2336 return et61x251_vidioc_s_input(cam, arg);
2314 2337
2315 case VIDIOC_QUERYCTRL: 2338 case VIDIOC_QUERYCTRL:
2316 return et61x251_vidioc_query_ctrl(cam, arg); 2339 return et61x251_vidioc_query_ctrl(cam, arg);
@@ -2393,19 +2416,19 @@ static int et61x251_ioctl(struct inode* inode, struct file* filp,
2393 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp)); 2416 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2394 int err = 0; 2417 int err = 0;
2395 2418
2396 if (down_interruptible(&cam->fileop_sem)) 2419 if (mutex_lock_interruptible(&cam->fileop_mutex))
2397 return -ERESTARTSYS; 2420 return -ERESTARTSYS;
2398 2421
2399 if (cam->state & DEV_DISCONNECTED) { 2422 if (cam->state & DEV_DISCONNECTED) {
2400 DBG(1, "Device not present"); 2423 DBG(1, "Device not present");
2401 up(&cam->fileop_sem); 2424 mutex_unlock(&cam->fileop_mutex);
2402 return -ENODEV; 2425 return -ENODEV;
2403 } 2426 }
2404 2427
2405 if (cam->state & DEV_MISCONFIGURED) { 2428 if (cam->state & DEV_MISCONFIGURED) {
2406 DBG(1, "The camera is misconfigured. Close and open it " 2429 DBG(1, "The camera is misconfigured. Close and open it "
2407 "again."); 2430 "again.");
2408 up(&cam->fileop_sem); 2431 mutex_unlock(&cam->fileop_mutex);
2409 return -EIO; 2432 return -EIO;
2410 } 2433 }
2411 2434
@@ -2413,7 +2436,7 @@ static int et61x251_ioctl(struct inode* inode, struct file* filp,
2413 2436
2414 err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg); 2437 err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2415 2438
2416 up(&cam->fileop_sem); 2439 mutex_unlock(&cam->fileop_mutex);
2417 2440
2418 return err; 2441 return err;
2419} 2442}
@@ -2459,7 +2482,7 @@ et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2459 goto fail; 2482 goto fail;
2460 } 2483 }
2461 2484
2462 init_MUTEX(&cam->dev_sem); 2485 mutex_init(&cam->dev_mutex);
2463 2486
2464 DBG(2, "ET61X[12]51 PC Camera Controller detected " 2487 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2465 "(vid/pid 0x%04X/0x%04X)",id->idVendor, id->idProduct); 2488 "(vid/pid 0x%04X/0x%04X)",id->idVendor, id->idProduct);
@@ -2470,8 +2493,8 @@ et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2470 break; 2493 break;
2471 } 2494 }
2472 2495
2473 if (!err && cam->sensor) 2496 if (!err)
2474 DBG(2, "%s image sensor detected", cam->sensor->name); 2497 DBG(2, "%s image sensor detected", cam->sensor.name);
2475 else { 2498 else {
2476 DBG(1, "No supported image sensor detected"); 2499 DBG(1, "No supported image sensor detected");
2477 err = -ENODEV; 2500 err = -ENODEV;
@@ -2492,7 +2515,7 @@ et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2492 cam->v4ldev->release = video_device_release; 2515 cam->v4ldev->release = video_device_release;
2493 video_set_drvdata(cam->v4ldev, cam); 2516 video_set_drvdata(cam->v4ldev, cam);
2494 2517
2495 down(&cam->dev_sem); 2518 mutex_lock(&cam->dev_mutex);
2496 2519
2497 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER, 2520 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2498 video_nr[dev_nr]); 2521 video_nr[dev_nr]);
@@ -2502,13 +2525,14 @@ et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2502 DBG(1, "Free /dev/videoX node not found"); 2525 DBG(1, "Free /dev/videoX node not found");
2503 video_nr[dev_nr] = -1; 2526 video_nr[dev_nr] = -1;
2504 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0; 2527 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2505 up(&cam->dev_sem); 2528 mutex_unlock(&cam->dev_mutex);
2506 goto fail; 2529 goto fail;
2507 } 2530 }
2508 2531
2509 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor); 2532 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2510 2533
2511 cam->module_param.force_munmap = force_munmap[dev_nr]; 2534 cam->module_param.force_munmap = force_munmap[dev_nr];
2535 cam->module_param.frame_timeout = frame_timeout[dev_nr];
2512 2536
2513 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0; 2537 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2514 2538
@@ -2519,7 +2543,7 @@ et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2519 2543
2520 usb_set_intfdata(intf, cam); 2544 usb_set_intfdata(intf, cam);
2521 2545
2522 up(&cam->dev_sem); 2546 mutex_unlock(&cam->dev_mutex);
2523 2547
2524 return 0; 2548 return 0;
2525 2549
@@ -2543,7 +2567,7 @@ static void et61x251_usb_disconnect(struct usb_interface* intf)
2543 2567
2544 down_write(&et61x251_disconnect); 2568 down_write(&et61x251_disconnect);
2545 2569
2546 down(&cam->dev_sem); 2570 mutex_lock(&cam->dev_mutex);
2547 2571
2548 DBG(2, "Disconnecting %s...", cam->v4ldev->name); 2572 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2549 2573
@@ -2557,13 +2581,14 @@ static void et61x251_usb_disconnect(struct usb_interface* intf)
2557 et61x251_stop_transfer(cam); 2581 et61x251_stop_transfer(cam);
2558 cam->state |= DEV_DISCONNECTED; 2582 cam->state |= DEV_DISCONNECTED;
2559 wake_up_interruptible(&cam->wait_frame); 2583 wake_up_interruptible(&cam->wait_frame);
2560 wake_up_interruptible(&cam->wait_stream); 2584 wake_up(&cam->wait_stream);
2585 usb_get_dev(cam->usbdev);
2561 } else { 2586 } else {
2562 cam->state |= DEV_DISCONNECTED; 2587 cam->state |= DEV_DISCONNECTED;
2563 et61x251_release_resources(cam); 2588 et61x251_release_resources(cam);
2564 } 2589 }
2565 2590
2566 up(&cam->dev_sem); 2591 mutex_unlock(&cam->dev_mutex);
2567 2592
2568 if (!cam->users) 2593 if (!cam->users)
2569 kfree(cam); 2594 kfree(cam);
diff --git a/drivers/usb/media/et61x251_sensor.h b/drivers/usb/media/et61x251_sensor.h
index b9df91062fc0..56841ae8a207 100644
--- a/drivers/usb/media/et61x251_sensor.h
+++ b/drivers/usb/media/et61x251_sensor.h
@@ -42,6 +42,9 @@ static int (*et61x251_sensor_table[])(struct et61x251_device*) = { \
42 NULL, \ 42 NULL, \
43}; 43};
44 44
45extern struct et61x251_device*
46et61x251_match_id(struct et61x251_device* cam, const struct usb_device_id *id);
47
45extern void 48extern void
46et61x251_attach_sensor(struct et61x251_device* cam, 49et61x251_attach_sensor(struct et61x251_device* cam,
47 struct et61x251_sensor* sensor); 50 struct et61x251_sensor* sensor);
@@ -105,8 +108,6 @@ struct et61x251_sensor {
105 int (*set_pix_format)(struct et61x251_device* cam, 108 int (*set_pix_format)(struct et61x251_device* cam,
106 const struct v4l2_pix_format* pix); 109 const struct v4l2_pix_format* pix);
107 110
108 const struct usb_device* usbdev;
109
110 /* Private */ 111 /* Private */
111 struct v4l2_queryctrl _qctrl[ET61X251_MAX_CTRLS]; 112 struct v4l2_queryctrl _qctrl[ET61X251_MAX_CTRLS];
112 struct v4l2_rect _rect; 113 struct v4l2_rect _rect;
diff --git a/drivers/usb/media/et61x251_tas5130d1b.c b/drivers/usb/media/et61x251_tas5130d1b.c
index 65f1ae9cf2b3..3998d76a307a 100644
--- a/drivers/usb/media/et61x251_tas5130d1b.c
+++ b/drivers/usb/media/et61x251_tas5130d1b.c
@@ -126,12 +126,16 @@ static struct et61x251_sensor tas5130d1b = {
126 126
127int et61x251_probe_tas5130d1b(struct et61x251_device* cam) 127int et61x251_probe_tas5130d1b(struct et61x251_device* cam)
128{ 128{
129 /* This sensor has no identifiers, so let's attach it anyway */ 129 const struct usb_device_id tas5130d1b_id_table[] = {
130 et61x251_attach_sensor(cam, &tas5130d1b); 130 { USB_DEVICE(0x102c, 0x6251), },
131 { }
132 };
131 133
132 /* Sensor detection is based on USB pid/vid */ 134 /* Sensor detection is based on USB pid/vid */
133 if (le16_to_cpu(tas5130d1b.usbdev->descriptor.idProduct) != 0x6251) 135 if (!et61x251_match_id(cam, tas5130d1b_id_table))
134 return -ENODEV; 136 return -ENODEV;
135 137
138 et61x251_attach_sensor(cam, &tas5130d1b);
139
136 return 0; 140 return 0;
137} 141}
diff --git a/drivers/usb/media/ov511.c b/drivers/usb/media/ov511.c
index 51e9cc06f7e3..da44579d6f29 100644
--- a/drivers/usb/media/ov511.c
+++ b/drivers/usb/media/ov511.c
@@ -365,14 +365,14 @@ reg_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
365 365
366 PDEBUG(5, "0x%02X:0x%02X", reg, value); 366 PDEBUG(5, "0x%02X:0x%02X", reg, value);
367 367
368 down(&ov->cbuf_lock); 368 mutex_lock(&ov->cbuf_lock);
369 ov->cbuf[0] = value; 369 ov->cbuf[0] = value;
370 rc = usb_control_msg(ov->dev, 370 rc = usb_control_msg(ov->dev,
371 usb_sndctrlpipe(ov->dev, 0), 371 usb_sndctrlpipe(ov->dev, 0),
372 (ov->bclass == BCL_OV518)?1:2 /* REG_IO */, 372 (ov->bclass == BCL_OV518)?1:2 /* REG_IO */,
373 USB_TYPE_VENDOR | USB_RECIP_DEVICE, 373 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
374 0, (__u16)reg, &ov->cbuf[0], 1, 1000); 374 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
375 up(&ov->cbuf_lock); 375 mutex_unlock(&ov->cbuf_lock);
376 376
377 if (rc < 0) 377 if (rc < 0)
378 err("reg write: error %d: %s", rc, symbolic(urb_errlist, rc)); 378 err("reg write: error %d: %s", rc, symbolic(urb_errlist, rc));
@@ -387,7 +387,7 @@ reg_r(struct usb_ov511 *ov, unsigned char reg)
387{ 387{
388 int rc; 388 int rc;
389 389
390 down(&ov->cbuf_lock); 390 mutex_lock(&ov->cbuf_lock);
391 rc = usb_control_msg(ov->dev, 391 rc = usb_control_msg(ov->dev,
392 usb_rcvctrlpipe(ov->dev, 0), 392 usb_rcvctrlpipe(ov->dev, 0),
393 (ov->bclass == BCL_OV518)?1:3 /* REG_IO */, 393 (ov->bclass == BCL_OV518)?1:3 /* REG_IO */,
@@ -401,7 +401,7 @@ reg_r(struct usb_ov511 *ov, unsigned char reg)
401 PDEBUG(5, "0x%02X:0x%02X", reg, ov->cbuf[0]); 401 PDEBUG(5, "0x%02X:0x%02X", reg, ov->cbuf[0]);
402 } 402 }
403 403
404 up(&ov->cbuf_lock); 404 mutex_unlock(&ov->cbuf_lock);
405 405
406 return rc; 406 return rc;
407} 407}
@@ -444,7 +444,7 @@ ov518_reg_w32(struct usb_ov511 *ov, unsigned char reg, u32 val, int n)
444 444
445 PDEBUG(5, "0x%02X:%7d, n=%d", reg, val, n); 445 PDEBUG(5, "0x%02X:%7d, n=%d", reg, val, n);
446 446
447 down(&ov->cbuf_lock); 447 mutex_lock(&ov->cbuf_lock);
448 448
449 *((__le32 *)ov->cbuf) = __cpu_to_le32(val); 449 *((__le32 *)ov->cbuf) = __cpu_to_le32(val);
450 450
@@ -453,7 +453,7 @@ ov518_reg_w32(struct usb_ov511 *ov, unsigned char reg, u32 val, int n)
453 1 /* REG_IO */, 453 1 /* REG_IO */,
454 USB_TYPE_VENDOR | USB_RECIP_DEVICE, 454 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
455 0, (__u16)reg, ov->cbuf, n, 1000); 455 0, (__u16)reg, ov->cbuf, n, 1000);
456 up(&ov->cbuf_lock); 456 mutex_unlock(&ov->cbuf_lock);
457 457
458 if (rc < 0) 458 if (rc < 0)
459 err("reg write multiple: error %d: %s", rc, 459 err("reg write multiple: error %d: %s", rc,
@@ -768,14 +768,14 @@ i2c_r(struct usb_ov511 *ov, unsigned char reg)
768{ 768{
769 int rc; 769 int rc;
770 770
771 down(&ov->i2c_lock); 771 mutex_lock(&ov->i2c_lock);
772 772
773 if (ov->bclass == BCL_OV518) 773 if (ov->bclass == BCL_OV518)
774 rc = ov518_i2c_read_internal(ov, reg); 774 rc = ov518_i2c_read_internal(ov, reg);
775 else 775 else
776 rc = ov511_i2c_read_internal(ov, reg); 776 rc = ov511_i2c_read_internal(ov, reg);
777 777
778 up(&ov->i2c_lock); 778 mutex_unlock(&ov->i2c_lock);
779 779
780 return rc; 780 return rc;
781} 781}
@@ -785,14 +785,14 @@ i2c_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
785{ 785{
786 int rc; 786 int rc;
787 787
788 down(&ov->i2c_lock); 788 mutex_lock(&ov->i2c_lock);
789 789
790 if (ov->bclass == BCL_OV518) 790 if (ov->bclass == BCL_OV518)
791 rc = ov518_i2c_write_internal(ov, reg, value); 791 rc = ov518_i2c_write_internal(ov, reg, value);
792 else 792 else
793 rc = ov511_i2c_write_internal(ov, reg, value); 793 rc = ov511_i2c_write_internal(ov, reg, value);
794 794
795 up(&ov->i2c_lock); 795 mutex_unlock(&ov->i2c_lock);
796 796
797 return rc; 797 return rc;
798} 798}
@@ -842,9 +842,9 @@ i2c_w_mask(struct usb_ov511 *ov,
842{ 842{
843 int rc; 843 int rc;
844 844
845 down(&ov->i2c_lock); 845 mutex_lock(&ov->i2c_lock);
846 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask); 846 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
847 up(&ov->i2c_lock); 847 mutex_unlock(&ov->i2c_lock);
848 848
849 return rc; 849 return rc;
850} 850}
@@ -880,7 +880,7 @@ i2c_w_slave(struct usb_ov511 *ov,
880{ 880{
881 int rc = 0; 881 int rc = 0;
882 882
883 down(&ov->i2c_lock); 883 mutex_lock(&ov->i2c_lock);
884 884
885 /* Set new slave IDs */ 885 /* Set new slave IDs */
886 rc = i2c_set_slave_internal(ov, slave); 886 rc = i2c_set_slave_internal(ov, slave);
@@ -894,7 +894,7 @@ out:
894 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0) 894 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
895 err("Couldn't restore primary I2C slave"); 895 err("Couldn't restore primary I2C slave");
896 896
897 up(&ov->i2c_lock); 897 mutex_unlock(&ov->i2c_lock);
898 return rc; 898 return rc;
899} 899}
900 900
@@ -906,7 +906,7 @@ i2c_r_slave(struct usb_ov511 *ov,
906{ 906{
907 int rc; 907 int rc;
908 908
909 down(&ov->i2c_lock); 909 mutex_lock(&ov->i2c_lock);
910 910
911 /* Set new slave IDs */ 911 /* Set new slave IDs */
912 rc = i2c_set_slave_internal(ov, slave); 912 rc = i2c_set_slave_internal(ov, slave);
@@ -923,7 +923,7 @@ out:
923 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0) 923 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
924 err("Couldn't restore primary I2C slave"); 924 err("Couldn't restore primary I2C slave");
925 925
926 up(&ov->i2c_lock); 926 mutex_unlock(&ov->i2c_lock);
927 return rc; 927 return rc;
928} 928}
929 929
@@ -933,7 +933,7 @@ ov51x_set_slave_ids(struct usb_ov511 *ov, unsigned char sid)
933{ 933{
934 int rc; 934 int rc;
935 935
936 down(&ov->i2c_lock); 936 mutex_lock(&ov->i2c_lock);
937 937
938 rc = i2c_set_slave_internal(ov, sid); 938 rc = i2c_set_slave_internal(ov, sid);
939 if (rc < 0) 939 if (rc < 0)
@@ -942,7 +942,7 @@ ov51x_set_slave_ids(struct usb_ov511 *ov, unsigned char sid)
942 // FIXME: Is this actually necessary? 942 // FIXME: Is this actually necessary?
943 rc = ov51x_reset(ov, OV511_RESET_NOREGS); 943 rc = ov51x_reset(ov, OV511_RESET_NOREGS);
944out: 944out:
945 up(&ov->i2c_lock); 945 mutex_unlock(&ov->i2c_lock);
946 return rc; 946 return rc;
947} 947}
948 948
@@ -3832,7 +3832,7 @@ ov51x_alloc(struct usb_ov511 *ov)
3832 const int raw_bufsize = OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h); 3832 const int raw_bufsize = OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h);
3833 3833
3834 PDEBUG(4, "entered"); 3834 PDEBUG(4, "entered");
3835 down(&ov->buf_lock); 3835 mutex_lock(&ov->buf_lock);
3836 3836
3837 if (ov->buf_state == BUF_ALLOCATED) 3837 if (ov->buf_state == BUF_ALLOCATED)
3838 goto out; 3838 goto out;
@@ -3879,12 +3879,12 @@ ov51x_alloc(struct usb_ov511 *ov)
3879 3879
3880 ov->buf_state = BUF_ALLOCATED; 3880 ov->buf_state = BUF_ALLOCATED;
3881out: 3881out:
3882 up(&ov->buf_lock); 3882 mutex_unlock(&ov->buf_lock);
3883 PDEBUG(4, "leaving"); 3883 PDEBUG(4, "leaving");
3884 return 0; 3884 return 0;
3885error: 3885error:
3886 ov51x_do_dealloc(ov); 3886 ov51x_do_dealloc(ov);
3887 up(&ov->buf_lock); 3887 mutex_unlock(&ov->buf_lock);
3888 PDEBUG(4, "errored"); 3888 PDEBUG(4, "errored");
3889 return -ENOMEM; 3889 return -ENOMEM;
3890} 3890}
@@ -3893,9 +3893,9 @@ static void
3893ov51x_dealloc(struct usb_ov511 *ov) 3893ov51x_dealloc(struct usb_ov511 *ov)
3894{ 3894{
3895 PDEBUG(4, "entered"); 3895 PDEBUG(4, "entered");
3896 down(&ov->buf_lock); 3896 mutex_lock(&ov->buf_lock);
3897 ov51x_do_dealloc(ov); 3897 ov51x_do_dealloc(ov);
3898 up(&ov->buf_lock); 3898 mutex_unlock(&ov->buf_lock);
3899 PDEBUG(4, "leaving"); 3899 PDEBUG(4, "leaving");
3900} 3900}
3901 3901
@@ -3914,7 +3914,7 @@ ov51x_v4l1_open(struct inode *inode, struct file *file)
3914 3914
3915 PDEBUG(4, "opening"); 3915 PDEBUG(4, "opening");
3916 3916
3917 down(&ov->lock); 3917 mutex_lock(&ov->lock);
3918 3918
3919 err = -EBUSY; 3919 err = -EBUSY;
3920 if (ov->user) 3920 if (ov->user)
@@ -3958,7 +3958,7 @@ ov51x_v4l1_open(struct inode *inode, struct file *file)
3958 ov51x_led_control(ov, 1); 3958 ov51x_led_control(ov, 1);
3959 3959
3960out: 3960out:
3961 up(&ov->lock); 3961 mutex_unlock(&ov->lock);
3962 return err; 3962 return err;
3963} 3963}
3964 3964
@@ -3970,7 +3970,7 @@ ov51x_v4l1_close(struct inode *inode, struct file *file)
3970 3970
3971 PDEBUG(4, "ov511_close"); 3971 PDEBUG(4, "ov511_close");
3972 3972
3973 down(&ov->lock); 3973 mutex_lock(&ov->lock);
3974 3974
3975 ov->user--; 3975 ov->user--;
3976 ov51x_stop_isoc(ov); 3976 ov51x_stop_isoc(ov);
@@ -3981,15 +3981,15 @@ ov51x_v4l1_close(struct inode *inode, struct file *file)
3981 if (ov->dev) 3981 if (ov->dev)
3982 ov51x_dealloc(ov); 3982 ov51x_dealloc(ov);
3983 3983
3984 up(&ov->lock); 3984 mutex_unlock(&ov->lock);
3985 3985
3986 /* Device unplugged while open. Only a minimum of unregistration is done 3986 /* Device unplugged while open. Only a minimum of unregistration is done
3987 * here; the disconnect callback already did the rest. */ 3987 * here; the disconnect callback already did the rest. */
3988 if (!ov->dev) { 3988 if (!ov->dev) {
3989 down(&ov->cbuf_lock); 3989 mutex_lock(&ov->cbuf_lock);
3990 kfree(ov->cbuf); 3990 kfree(ov->cbuf);
3991 ov->cbuf = NULL; 3991 ov->cbuf = NULL;
3992 up(&ov->cbuf_lock); 3992 mutex_unlock(&ov->cbuf_lock);
3993 3993
3994 ov51x_dealloc(ov); 3994 ov51x_dealloc(ov);
3995 kfree(ov); 3995 kfree(ov);
@@ -4449,12 +4449,12 @@ ov51x_v4l1_ioctl(struct inode *inode, struct file *file,
4449 struct usb_ov511 *ov = video_get_drvdata(vdev); 4449 struct usb_ov511 *ov = video_get_drvdata(vdev);
4450 int rc; 4450 int rc;
4451 4451
4452 if (down_interruptible(&ov->lock)) 4452 if (mutex_lock_interruptible(&ov->lock))
4453 return -EINTR; 4453 return -EINTR;
4454 4454
4455 rc = video_usercopy(inode, file, cmd, arg, ov51x_v4l1_ioctl_internal); 4455 rc = video_usercopy(inode, file, cmd, arg, ov51x_v4l1_ioctl_internal);
4456 4456
4457 up(&ov->lock); 4457 mutex_unlock(&ov->lock);
4458 return rc; 4458 return rc;
4459} 4459}
4460 4460
@@ -4468,7 +4468,7 @@ ov51x_v4l1_read(struct file *file, char __user *buf, size_t cnt, loff_t *ppos)
4468 int i, rc = 0, frmx = -1; 4468 int i, rc = 0, frmx = -1;
4469 struct ov511_frame *frame; 4469 struct ov511_frame *frame;
4470 4470
4471 if (down_interruptible(&ov->lock)) 4471 if (mutex_lock_interruptible(&ov->lock))
4472 return -EINTR; 4472 return -EINTR;
4473 4473
4474 PDEBUG(4, "%ld bytes, noblock=%d", count, noblock); 4474 PDEBUG(4, "%ld bytes, noblock=%d", count, noblock);
@@ -4604,11 +4604,11 @@ restart:
4604 4604
4605 PDEBUG(4, "read finished, returning %ld (sweet)", count); 4605 PDEBUG(4, "read finished, returning %ld (sweet)", count);
4606 4606
4607 up(&ov->lock); 4607 mutex_unlock(&ov->lock);
4608 return count; 4608 return count;
4609 4609
4610error: 4610error:
4611 up(&ov->lock); 4611 mutex_unlock(&ov->lock);
4612 return rc; 4612 return rc;
4613} 4613}
4614 4614
@@ -4631,14 +4631,14 @@ ov51x_v4l1_mmap(struct file *file, struct vm_area_struct *vma)
4631 + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)))) 4631 + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))))
4632 return -EINVAL; 4632 return -EINVAL;
4633 4633
4634 if (down_interruptible(&ov->lock)) 4634 if (mutex_lock_interruptible(&ov->lock))
4635 return -EINTR; 4635 return -EINTR;
4636 4636
4637 pos = (unsigned long)ov->fbuf; 4637 pos = (unsigned long)ov->fbuf;
4638 while (size > 0) { 4638 while (size > 0) {
4639 page = vmalloc_to_pfn((void *)pos); 4639 page = vmalloc_to_pfn((void *)pos);
4640 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) { 4640 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
4641 up(&ov->lock); 4641 mutex_unlock(&ov->lock);
4642 return -EAGAIN; 4642 return -EAGAIN;
4643 } 4643 }
4644 start += PAGE_SIZE; 4644 start += PAGE_SIZE;
@@ -4649,7 +4649,7 @@ ov51x_v4l1_mmap(struct file *file, struct vm_area_struct *vma)
4649 size = 0; 4649 size = 0;
4650 } 4650 }
4651 4651
4652 up(&ov->lock); 4652 mutex_unlock(&ov->lock);
4653 return 0; 4653 return 0;
4654} 4654}
4655 4655
@@ -5639,7 +5639,7 @@ static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
5639static ssize_t show_exposure(struct class_device *cd, char *buf) 5639static ssize_t show_exposure(struct class_device *cd, char *buf)
5640{ 5640{
5641 struct usb_ov511 *ov = cd_to_ov(cd); 5641 struct usb_ov511 *ov = cd_to_ov(cd);
5642 unsigned char exp; 5642 unsigned char exp = 0;
5643 5643
5644 if (!ov->dev) 5644 if (!ov->dev)
5645 return -ENODEV; 5645 return -ENODEV;
@@ -5686,13 +5686,11 @@ ov51x_probe(struct usb_interface *intf, const struct usb_device_id *id)
5686 if (idesc->bInterfaceSubClass != 0x00) 5686 if (idesc->bInterfaceSubClass != 0x00)
5687 return -ENODEV; 5687 return -ENODEV;
5688 5688
5689 if ((ov = kmalloc(sizeof(*ov), GFP_KERNEL)) == NULL) { 5689 if ((ov = kzalloc(sizeof(*ov), GFP_KERNEL)) == NULL) {
5690 err("couldn't kmalloc ov struct"); 5690 err("couldn't kmalloc ov struct");
5691 goto error_out; 5691 goto error_out;
5692 } 5692 }
5693 5693
5694 memset(ov, 0, sizeof(*ov));
5695
5696 ov->dev = dev; 5694 ov->dev = dev;
5697 ov->iface = idesc->bInterfaceNumber; 5695 ov->iface = idesc->bInterfaceNumber;
5698 ov->led_policy = led; 5696 ov->led_policy = led;
@@ -5738,11 +5736,10 @@ ov51x_probe(struct usb_interface *intf, const struct usb_device_id *id)
5738 5736
5739 init_waitqueue_head(&ov->wq); 5737 init_waitqueue_head(&ov->wq);
5740 5738
5741 init_MUTEX(&ov->lock); /* to 1 == available */ 5739 mutex_init(&ov->lock); /* to 1 == available */
5742 init_MUTEX(&ov->buf_lock); 5740 mutex_init(&ov->buf_lock);
5743 init_MUTEX(&ov->param_lock); 5741 mutex_init(&ov->i2c_lock);
5744 init_MUTEX(&ov->i2c_lock); 5742 mutex_init(&ov->cbuf_lock);
5745 init_MUTEX(&ov->cbuf_lock);
5746 5743
5747 ov->buf_state = BUF_NOT_ALLOCATED; 5744 ov->buf_state = BUF_NOT_ALLOCATED;
5748 5745
@@ -5833,10 +5830,10 @@ error:
5833 } 5830 }
5834 5831
5835 if (ov->cbuf) { 5832 if (ov->cbuf) {
5836 down(&ov->cbuf_lock); 5833 mutex_lock(&ov->cbuf_lock);
5837 kfree(ov->cbuf); 5834 kfree(ov->cbuf);
5838 ov->cbuf = NULL; 5835 ov->cbuf = NULL;
5839 up(&ov->cbuf_lock); 5836 mutex_unlock(&ov->cbuf_lock);
5840 } 5837 }
5841 5838
5842 kfree(ov); 5839 kfree(ov);
@@ -5881,10 +5878,10 @@ ov51x_disconnect(struct usb_interface *intf)
5881 5878
5882 /* Free the memory */ 5879 /* Free the memory */
5883 if (ov && !ov->user) { 5880 if (ov && !ov->user) {
5884 down(&ov->cbuf_lock); 5881 mutex_lock(&ov->cbuf_lock);
5885 kfree(ov->cbuf); 5882 kfree(ov->cbuf);
5886 ov->cbuf = NULL; 5883 ov->cbuf = NULL;
5887 up(&ov->cbuf_lock); 5884 mutex_unlock(&ov->cbuf_lock);
5888 5885
5889 ov51x_dealloc(ov); 5886 ov51x_dealloc(ov);
5890 kfree(ov); 5887 kfree(ov);
diff --git a/drivers/usb/media/ov511.h b/drivers/usb/media/ov511.h
index 086509a137c6..bce9b3633889 100644
--- a/drivers/usb/media/ov511.h
+++ b/drivers/usb/media/ov511.h
@@ -5,6 +5,7 @@
5#include <linux/videodev.h> 5#include <linux/videodev.h>
6#include <linux/smp_lock.h> 6#include <linux/smp_lock.h>
7#include <linux/usb.h> 7#include <linux/usb.h>
8#include <linux/mutex.h>
8 9
9#define OV511_DEBUG /* Turn on debug messages */ 10#define OV511_DEBUG /* Turn on debug messages */
10 11
@@ -435,7 +436,7 @@ struct usb_ov511 {
435 436
436 int led_policy; /* LED: off|on|auto; OV511+ only */ 437 int led_policy; /* LED: off|on|auto; OV511+ only */
437 438
438 struct semaphore lock; /* Serializes user-accessible operations */ 439 struct mutex lock; /* Serializes user-accessible operations */
439 int user; /* user count for exclusive use */ 440 int user; /* user count for exclusive use */
440 441
441 int streaming; /* Are we streaming Isochronous? */ 442 int streaming; /* Are we streaming Isochronous? */
@@ -473,11 +474,9 @@ struct usb_ov511 {
473 int packet_size; /* Frame size per isoc desc */ 474 int packet_size; /* Frame size per isoc desc */
474 int packet_numbering; /* Is ISO frame numbering enabled? */ 475 int packet_numbering; /* Is ISO frame numbering enabled? */
475 476
476 struct semaphore param_lock; /* params lock for this camera */
477
478 /* Framebuffer/sbuf management */ 477 /* Framebuffer/sbuf management */
479 int buf_state; 478 int buf_state;
480 struct semaphore buf_lock; 479 struct mutex buf_lock;
481 480
482 struct ov51x_decomp_ops *decomp_ops; 481 struct ov51x_decomp_ops *decomp_ops;
483 482
@@ -494,12 +493,12 @@ struct usb_ov511 {
494 int pal; /* Device is designed for PAL resolution */ 493 int pal; /* Device is designed for PAL resolution */
495 494
496 /* I2C interface */ 495 /* I2C interface */
497 struct semaphore i2c_lock; /* Protect I2C controller regs */ 496 struct mutex i2c_lock; /* Protect I2C controller regs */
498 unsigned char primary_i2c_slave; /* I2C write id of sensor */ 497 unsigned char primary_i2c_slave; /* I2C write id of sensor */
499 498
500 /* Control transaction stuff */ 499 /* Control transaction stuff */
501 unsigned char *cbuf; /* Buffer for payload */ 500 unsigned char *cbuf; /* Buffer for payload */
502 struct semaphore cbuf_lock; 501 struct mutex cbuf_lock;
503}; 502};
504 503
505/* Used to represent a list of values and their respective symbolic names */ 504/* Used to represent a list of values and their respective symbolic names */
diff --git a/drivers/usb/media/pwc/pwc-ctrl.c b/drivers/usb/media/pwc/pwc-ctrl.c
index 3ebb6e9cdf92..0398b812e0ce 100644
--- a/drivers/usb/media/pwc/pwc-ctrl.c
+++ b/drivers/usb/media/pwc/pwc-ctrl.c
@@ -41,7 +41,6 @@
41#include <asm/uaccess.h> 41#include <asm/uaccess.h>
42#endif 42#endif
43#include <asm/errno.h> 43#include <asm/errno.h>
44#include <linux/version.h>
45 44
46#include "pwc.h" 45#include "pwc.h"
47#include "pwc-ioctl.h" 46#include "pwc-ioctl.h"
diff --git a/drivers/usb/media/pwc/pwc-if.c b/drivers/usb/media/pwc/pwc-if.c
index 4f9b0dc6fd7b..90eb26042817 100644
--- a/drivers/usb/media/pwc/pwc-if.c
+++ b/drivers/usb/media/pwc/pwc-if.c
@@ -62,7 +62,6 @@
62#include <linux/poll.h> 62#include <linux/poll.h>
63#include <linux/slab.h> 63#include <linux/slab.h>
64#include <linux/vmalloc.h> 64#include <linux/vmalloc.h>
65#include <linux/version.h>
66#include <asm/io.h> 65#include <asm/io.h>
67 66
68#include "pwc.h" 67#include "pwc.h"
@@ -827,13 +826,10 @@ static int pwc_isoc_init(struct pwc_device *pdev)
827 /* Get the current alternate interface, adjust packet size */ 826 /* Get the current alternate interface, adjust packet size */
828 if (!udev->actconfig) 827 if (!udev->actconfig)
829 return -EFAULT; 828 return -EFAULT;
830#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5) 829
831 idesc = &udev->actconfig->interface[0]->altsetting[pdev->valternate];
832#else
833 intf = usb_ifnum_to_if(udev, 0); 830 intf = usb_ifnum_to_if(udev, 0);
834 if (intf) 831 if (intf)
835 idesc = usb_altnum_to_altsetting(intf, pdev->valternate); 832 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
836#endif
837 833
838 if (!idesc) 834 if (!idesc)
839 return -EFAULT; 835 return -EFAULT;
@@ -1871,12 +1867,11 @@ static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id
1871 Info("Warning: more than 1 configuration available.\n"); 1867 Info("Warning: more than 1 configuration available.\n");
1872 1868
1873 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */ 1869 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1874 pdev = kmalloc(sizeof(struct pwc_device), GFP_KERNEL); 1870 pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
1875 if (pdev == NULL) { 1871 if (pdev == NULL) {
1876 Err("Oops, could not allocate memory for pwc_device.\n"); 1872 Err("Oops, could not allocate memory for pwc_device.\n");
1877 return -ENOMEM; 1873 return -ENOMEM;
1878 } 1874 }
1879 memset(pdev, 0, sizeof(struct pwc_device));
1880 pdev->type = type_id; 1875 pdev->type = type_id;
1881 pdev->vsize = default_size; 1876 pdev->vsize = default_size;
1882 pdev->vframes = default_fps; 1877 pdev->vframes = default_fps;
diff --git a/drivers/usb/media/se401.c b/drivers/usb/media/se401.c
index 2ba562285fda..f03ea7f89596 100644
--- a/drivers/usb/media/se401.c
+++ b/drivers/usb/media/se401.c
@@ -1157,21 +1157,21 @@ static int se401_mmap(struct file *file, struct vm_area_struct *vma)
1157 unsigned long size = vma->vm_end-vma->vm_start; 1157 unsigned long size = vma->vm_end-vma->vm_start;
1158 unsigned long page, pos; 1158 unsigned long page, pos;
1159 1159
1160 down(&se401->lock); 1160 mutex_lock(&se401->lock);
1161 1161
1162 if (se401->dev == NULL) { 1162 if (se401->dev == NULL) {
1163 up(&se401->lock); 1163 mutex_unlock(&se401->lock);
1164 return -EIO; 1164 return -EIO;
1165 } 1165 }
1166 if (size > (((SE401_NUMFRAMES * se401->maxframesize) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))) { 1166 if (size > (((SE401_NUMFRAMES * se401->maxframesize) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))) {
1167 up(&se401->lock); 1167 mutex_unlock(&se401->lock);
1168 return -EINVAL; 1168 return -EINVAL;
1169 } 1169 }
1170 pos = (unsigned long)se401->fbuf; 1170 pos = (unsigned long)se401->fbuf;
1171 while (size > 0) { 1171 while (size > 0) {
1172 page = vmalloc_to_pfn((void *)pos); 1172 page = vmalloc_to_pfn((void *)pos);
1173 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) { 1173 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
1174 up(&se401->lock); 1174 mutex_unlock(&se401->lock);
1175 return -EAGAIN; 1175 return -EAGAIN;
1176 } 1176 }
1177 start += PAGE_SIZE; 1177 start += PAGE_SIZE;
@@ -1181,7 +1181,7 @@ static int se401_mmap(struct file *file, struct vm_area_struct *vma)
1181 else 1181 else
1182 size = 0; 1182 size = 0;
1183 } 1183 }
1184 up(&se401->lock); 1184 mutex_unlock(&se401->lock);
1185 1185
1186 return 0; 1186 return 0;
1187} 1187}
@@ -1345,13 +1345,11 @@ static int se401_probe(struct usb_interface *intf,
1345 /* We found one */ 1345 /* We found one */
1346 info("SE401 camera found: %s", camera_name); 1346 info("SE401 camera found: %s", camera_name);
1347 1347
1348 if ((se401 = kmalloc(sizeof(*se401), GFP_KERNEL)) == NULL) { 1348 if ((se401 = kzalloc(sizeof(*se401), GFP_KERNEL)) == NULL) {
1349 err("couldn't kmalloc se401 struct"); 1349 err("couldn't kmalloc se401 struct");
1350 return -ENOMEM; 1350 return -ENOMEM;
1351 } 1351 }
1352 1352
1353 memset(se401, 0, sizeof(*se401));
1354
1355 se401->dev = dev; 1353 se401->dev = dev;
1356 se401->iface = interface->bInterfaceNumber; 1354 se401->iface = interface->bInterfaceNumber;
1357 se401->camera_name = camera_name; 1355 se401->camera_name = camera_name;
@@ -1366,7 +1364,7 @@ static int se401_probe(struct usb_interface *intf,
1366 memcpy(&se401->vdev, &se401_template, sizeof(se401_template)); 1364 memcpy(&se401->vdev, &se401_template, sizeof(se401_template));
1367 memcpy(se401->vdev.name, se401->camera_name, strlen(se401->camera_name)); 1365 memcpy(se401->vdev.name, se401->camera_name, strlen(se401->camera_name));
1368 init_waitqueue_head(&se401->wq); 1366 init_waitqueue_head(&se401->wq);
1369 init_MUTEX(&se401->lock); 1367 mutex_init(&se401->lock);
1370 wmb(); 1368 wmb();
1371 1369
1372 if (video_register_device(&se401->vdev, VFL_TYPE_GRABBER, video_nr) == -1) { 1370 if (video_register_device(&se401->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
diff --git a/drivers/usb/media/se401.h b/drivers/usb/media/se401.h
index 2e5846f1eb20..e88a40d4c86a 100644
--- a/drivers/usb/media/se401.h
+++ b/drivers/usb/media/se401.h
@@ -5,6 +5,7 @@
5#include <asm/uaccess.h> 5#include <asm/uaccess.h>
6#include <linux/videodev.h> 6#include <linux/videodev.h>
7#include <linux/smp_lock.h> 7#include <linux/smp_lock.h>
8#include <linux/mutex.h>
8 9
9#define se401_DEBUG /* Turn on debug messages */ 10#define se401_DEBUG /* Turn on debug messages */
10 11
@@ -189,7 +190,7 @@ struct usb_se401 {
189 int maxframesize; 190 int maxframesize;
190 int cframesize; /* current framesize */ 191 int cframesize; /* current framesize */
191 192
192 struct semaphore lock; 193 struct mutex lock;
193 int user; /* user count for exclusive use */ 194 int user; /* user count for exclusive use */
194 int removed; /* device disconnected */ 195 int removed; /* device disconnected */
195 196
diff --git a/drivers/usb/media/sn9c102.h b/drivers/usb/media/sn9c102.h
index 17d60c1eea7e..1d70a62b9f23 100644
--- a/drivers/usb/media/sn9c102.h
+++ b/drivers/usb/media/sn9c102.h
@@ -33,7 +33,9 @@
33#include <linux/types.h> 33#include <linux/types.h>
34#include <linux/param.h> 34#include <linux/param.h>
35#include <linux/rwsem.h> 35#include <linux/rwsem.h>
36#include <asm/semaphore.h> 36#include <linux/mutex.h>
37#include <linux/string.h>
38#include <linux/stddef.h>
37 39
38#include "sn9c102_sensor.h" 40#include "sn9c102_sensor.h"
39 41
@@ -50,6 +52,7 @@
50#define SN9C102_ALTERNATE_SETTING 8 52#define SN9C102_ALTERNATE_SETTING 8
51#define SN9C102_URB_TIMEOUT msecs_to_jiffies(2 * SN9C102_ISO_PACKETS) 53#define SN9C102_URB_TIMEOUT msecs_to_jiffies(2 * SN9C102_ISO_PACKETS)
52#define SN9C102_CTRL_TIMEOUT 300 54#define SN9C102_CTRL_TIMEOUT 300
55#define SN9C102_FRAME_TIMEOUT 2
53 56
54/*****************************************************************************/ 57/*****************************************************************************/
55 58
@@ -107,16 +110,17 @@ struct sn9c102_sysfs_attr {
107 110
108struct sn9c102_module_param { 111struct sn9c102_module_param {
109 u8 force_munmap; 112 u8 force_munmap;
113 u16 frame_timeout;
110}; 114};
111 115
112static DECLARE_MUTEX(sn9c102_sysfs_lock); 116static DEFINE_MUTEX(sn9c102_sysfs_lock);
113static DECLARE_RWSEM(sn9c102_disconnect); 117static DECLARE_RWSEM(sn9c102_disconnect);
114 118
115struct sn9c102_device { 119struct sn9c102_device {
116 struct video_device* v4ldev; 120 struct video_device* v4ldev;
117 121
118 enum sn9c102_bridge bridge; 122 enum sn9c102_bridge bridge;
119 struct sn9c102_sensor* sensor; 123 struct sn9c102_sensor sensor;
120 124
121 struct usb_device* usbdev; 125 struct usb_device* usbdev;
122 struct urb* urb[SN9C102_URBS]; 126 struct urb* urb[SN9C102_URBS];
@@ -141,19 +145,28 @@ struct sn9c102_device {
141 enum sn9c102_dev_state state; 145 enum sn9c102_dev_state state;
142 u8 users; 146 u8 users;
143 147
144 struct semaphore dev_sem, fileop_sem; 148 struct mutex dev_mutex, fileop_mutex;
145 spinlock_t queue_lock; 149 spinlock_t queue_lock;
146 wait_queue_head_t open, wait_frame, wait_stream; 150 wait_queue_head_t open, wait_frame, wait_stream;
147}; 151};
148 152
149/*****************************************************************************/ 153/*****************************************************************************/
150 154
155struct sn9c102_device*
156sn9c102_match_id(struct sn9c102_device* cam, const struct usb_device_id *id)
157{
158 if (usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id))
159 return cam;
160
161 return NULL;
162}
163
164
151void 165void
152sn9c102_attach_sensor(struct sn9c102_device* cam, 166sn9c102_attach_sensor(struct sn9c102_device* cam,
153 struct sn9c102_sensor* sensor) 167 struct sn9c102_sensor* sensor)
154{ 168{
155 cam->sensor = sensor; 169 memcpy(&cam->sensor, sensor, sizeof(struct sn9c102_sensor));
156 cam->sensor->usbdev = cam->usbdev;
157} 170}
158 171
159/*****************************************************************************/ 172/*****************************************************************************/
@@ -196,7 +209,8 @@ do { \
196 209
197#undef PDBG 210#undef PDBG
198#define PDBG(fmt, args...) \ 211#define PDBG(fmt, args...) \
199dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args) 212dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
213 __FUNCTION__, __LINE__ , ## args)
200 214
201#undef PDBGG 215#undef PDBGG
202#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */ 216#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
diff --git a/drivers/usb/media/sn9c102_core.c b/drivers/usb/media/sn9c102_core.c
index c81397e4714b..4c6cc6395723 100644
--- a/drivers/usb/media/sn9c102_core.c
+++ b/drivers/usb/media/sn9c102_core.c
@@ -25,11 +25,9 @@
25#include <linux/moduleparam.h> 25#include <linux/moduleparam.h>
26#include <linux/errno.h> 26#include <linux/errno.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/string.h>
29#include <linux/device.h> 28#include <linux/device.h>
30#include <linux/fs.h> 29#include <linux/fs.h>
31#include <linux/delay.h> 30#include <linux/delay.h>
32#include <linux/stddef.h>
33#include <linux/compiler.h> 31#include <linux/compiler.h>
34#include <linux/ioctl.h> 32#include <linux/ioctl.h>
35#include <linux/poll.h> 33#include <linux/poll.h>
@@ -49,8 +47,8 @@
49#define SN9C102_MODULE_AUTHOR "(C) 2004-2006 Luca Risolia" 47#define SN9C102_MODULE_AUTHOR "(C) 2004-2006 Luca Risolia"
50#define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>" 48#define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
51#define SN9C102_MODULE_LICENSE "GPL" 49#define SN9C102_MODULE_LICENSE "GPL"
52#define SN9C102_MODULE_VERSION "1:1.26" 50#define SN9C102_MODULE_VERSION "1:1.27"
53#define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 26) 51#define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 27)
54 52
55/*****************************************************************************/ 53/*****************************************************************************/
56 54
@@ -89,6 +87,15 @@ MODULE_PARM_DESC(force_munmap,
89 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"." 87 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
90 "\n"); 88 "\n");
91 89
90static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] =
91 SN9C102_FRAME_TIMEOUT};
92module_param_array(frame_timeout, uint, NULL, 0644);
93MODULE_PARM_DESC(frame_timeout,
94 "\n<n[,...]> Timeout for a video frame in seconds."
95 "\nThis parameter is specific for each detected camera."
96 "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"."
97 "\n");
98
92#ifdef SN9C102_DEBUG 99#ifdef SN9C102_DEBUG
93static unsigned short debug = SN9C102_DEBUG_LEVEL; 100static unsigned short debug = SN9C102_DEBUG_LEVEL;
94module_param(debug, ushort, 0644); 101module_param(debug, ushort, 0644);
@@ -128,8 +135,8 @@ static u32
128sn9c102_request_buffers(struct sn9c102_device* cam, u32 count, 135sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
129 enum sn9c102_io_method io) 136 enum sn9c102_io_method io)
130{ 137{
131 struct v4l2_pix_format* p = &(cam->sensor->pix_format); 138 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
132 struct v4l2_rect* r = &(cam->sensor->cropcap.bounds); 139 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
133 const size_t imagesize = cam->module_param.force_munmap || 140 const size_t imagesize = cam->module_param.force_munmap ||
134 io == IO_READ ? 141 io == IO_READ ?
135 (p->width * p->height * p->priv) / 8 : 142 (p->width * p->height * p->priv) / 8 :
@@ -449,19 +456,13 @@ sn9c102_i2c_try_write(struct sn9c102_device* cam,
449 456
450int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address) 457int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
451{ 458{
452 if (!cam->sensor) 459 return sn9c102_i2c_try_read(cam, &cam->sensor, address);
453 return -1;
454
455 return sn9c102_i2c_try_read(cam, cam->sensor, address);
456} 460}
457 461
458 462
459int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value) 463int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
460{ 464{
461 if (!cam->sensor) 465 return sn9c102_i2c_try_write(cam, &cam->sensor, address, value);
462 return -1;
463
464 return sn9c102_i2c_try_write(cam, cam->sensor, address, value);
465} 466}
466 467
467/*****************************************************************************/ 468/*****************************************************************************/
@@ -505,7 +506,7 @@ sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
505 size_t eoflen = sizeof(sn9c102_eof_header_t), i; 506 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
506 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen; 507 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
507 508
508 if (cam->sensor->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X) 509 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
509 return NULL; /* EOF header does not exist in compressed data */ 510 return NULL; /* EOF header does not exist in compressed data */
510 511
511 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++) 512 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
@@ -535,7 +536,7 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
535 if ((*f)) 536 if ((*f))
536 (*f)->state = F_QUEUED; 537 (*f)->state = F_QUEUED;
537 DBG(3, "Stream interrupted"); 538 DBG(3, "Stream interrupted");
538 wake_up_interruptible(&cam->wait_stream); 539 wake_up(&cam->wait_stream);
539 } 540 }
540 541
541 if (cam->state & DEV_DISCONNECTED) 542 if (cam->state & DEV_DISCONNECTED)
@@ -553,9 +554,9 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
553 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t, 554 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
554 frame); 555 frame);
555 556
556 imagesize = (cam->sensor->pix_format.width * 557 imagesize = (cam->sensor.pix_format.width *
557 cam->sensor->pix_format.height * 558 cam->sensor.pix_format.height *
558 cam->sensor->pix_format.priv) / 8; 559 cam->sensor.pix_format.priv) / 8;
559 560
560 soflen = (cam->bridge) == BRIDGE_SN9C103 ? 561 soflen = (cam->bridge) == BRIDGE_SN9C103 ?
561 sizeof(sn9c103_sof_header_t) : 562 sizeof(sn9c103_sof_header_t) :
@@ -579,7 +580,7 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
579 580
580redo: 581redo:
581 sof = sn9c102_find_sof_header(cam, pos, len); 582 sof = sn9c102_find_sof_header(cam, pos, len);
582 if (!sof) { 583 if (likely(!sof)) {
583 eof = sn9c102_find_eof_header(cam, pos, len); 584 eof = sn9c102_find_eof_header(cam, pos, len);
584 if ((*f)->state == F_GRABBING) { 585 if ((*f)->state == F_GRABBING) {
585end_of_frame: 586end_of_frame:
@@ -589,8 +590,9 @@ end_of_frame:
589 img = (eof > pos) ? eof - pos - 1 : 0; 590 img = (eof > pos) ? eof - pos - 1 : 0;
590 591
591 if ((*f)->buf.bytesused+img > imagesize) { 592 if ((*f)->buf.bytesused+img > imagesize) {
592 u32 b = (*f)->buf.bytesused + img - 593 u32 b;
593 imagesize; 594 b = (*f)->buf.bytesused + img -
595 imagesize;
594 img = imagesize - (*f)->buf.bytesused; 596 img = imagesize - (*f)->buf.bytesused;
595 DBG(3, "Expected EOF not found: " 597 DBG(3, "Expected EOF not found: "
596 "video frame cut"); 598 "video frame cut");
@@ -608,9 +610,10 @@ end_of_frame:
608 (*f)->buf.bytesused += img; 610 (*f)->buf.bytesused += img;
609 611
610 if ((*f)->buf.bytesused == imagesize || 612 if ((*f)->buf.bytesused == imagesize ||
611 (cam->sensor->pix_format.pixelformat == 613 (cam->sensor.pix_format.pixelformat ==
612 V4L2_PIX_FMT_SN9C10X && eof)) { 614 V4L2_PIX_FMT_SN9C10X && eof)) {
613 u32 b = (*f)->buf.bytesused; 615 u32 b;
616 b = (*f)->buf.bytesused;
614 (*f)->state = F_DONE; 617 (*f)->state = F_DONE;
615 (*f)->buf.sequence= ++cam->frame_count; 618 (*f)->buf.sequence= ++cam->frame_count;
616 spin_lock(&cam->queue_lock); 619 spin_lock(&cam->queue_lock);
@@ -667,7 +670,7 @@ start_of_frame:
667 if (eof && eof < sof) 670 if (eof && eof < sof)
668 goto end_of_frame; /* (1) */ 671 goto end_of_frame; /* (1) */
669 else { 672 else {
670 if (cam->sensor->pix_format.pixelformat == 673 if (cam->sensor.pix_format.pixelformat ==
671 V4L2_PIX_FMT_SN9C10X) { 674 V4L2_PIX_FMT_SN9C10X) {
672 eof = sof - soflen; 675 eof = sof - soflen;
673 goto end_of_frame; 676 goto end_of_frame;
@@ -808,20 +811,21 @@ static int sn9c102_stop_transfer(struct sn9c102_device* cam)
808 811
809static int sn9c102_stream_interrupt(struct sn9c102_device* cam) 812static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
810{ 813{
811 int err = 0; 814 long timeout;
812 815
813 cam->stream = STREAM_INTERRUPT; 816 cam->stream = STREAM_INTERRUPT;
814 err = wait_event_timeout(cam->wait_stream, 817 timeout = wait_event_timeout(cam->wait_stream,
815 (cam->stream == STREAM_OFF) || 818 (cam->stream == STREAM_OFF) ||
816 (cam->state & DEV_DISCONNECTED), 819 (cam->state & DEV_DISCONNECTED),
817 SN9C102_URB_TIMEOUT); 820 SN9C102_URB_TIMEOUT);
818 if (cam->state & DEV_DISCONNECTED) 821 if (cam->state & DEV_DISCONNECTED)
819 return -ENODEV; 822 return -ENODEV;
820 else if (err) { 823 else if (cam->stream != STREAM_OFF) {
821 cam->state |= DEV_MISCONFIGURED; 824 cam->state |= DEV_MISCONFIGURED;
822 DBG(1, "The camera is misconfigured. To use it, close and " 825 DBG(1, "URB timeout reached. The camera is misconfigured. "
823 "open /dev/video%d again.", cam->v4ldev->minor); 826 "To use it, close and open /dev/video%d again.",
824 return err; 827 cam->v4ldev->minor);
828 return -EIO;
825 } 829 }
826 830
827 return 0; 831 return 0;
@@ -866,18 +870,18 @@ static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
866 struct sn9c102_device* cam; 870 struct sn9c102_device* cam;
867 ssize_t count; 871 ssize_t count;
868 872
869 if (down_interruptible(&sn9c102_sysfs_lock)) 873 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
870 return -ERESTARTSYS; 874 return -ERESTARTSYS;
871 875
872 cam = video_get_drvdata(to_video_device(cd)); 876 cam = video_get_drvdata(to_video_device(cd));
873 if (!cam) { 877 if (!cam) {
874 up(&sn9c102_sysfs_lock); 878 mutex_unlock(&sn9c102_sysfs_lock);
875 return -ENODEV; 879 return -ENODEV;
876 } 880 }
877 881
878 count = sprintf(buf, "%u\n", cam->sysfs.reg); 882 count = sprintf(buf, "%u\n", cam->sysfs.reg);
879 883
880 up(&sn9c102_sysfs_lock); 884 mutex_unlock(&sn9c102_sysfs_lock);
881 885
882 return count; 886 return count;
883} 887}
@@ -890,18 +894,18 @@ sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
890 u8 index; 894 u8 index;
891 ssize_t count; 895 ssize_t count;
892 896
893 if (down_interruptible(&sn9c102_sysfs_lock)) 897 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
894 return -ERESTARTSYS; 898 return -ERESTARTSYS;
895 899
896 cam = video_get_drvdata(to_video_device(cd)); 900 cam = video_get_drvdata(to_video_device(cd));
897 if (!cam) { 901 if (!cam) {
898 up(&sn9c102_sysfs_lock); 902 mutex_unlock(&sn9c102_sysfs_lock);
899 return -ENODEV; 903 return -ENODEV;
900 } 904 }
901 905
902 index = sn9c102_strtou8(buf, len, &count); 906 index = sn9c102_strtou8(buf, len, &count);
903 if (index > 0x1f || !count) { 907 if (index > 0x1f || !count) {
904 up(&sn9c102_sysfs_lock); 908 mutex_unlock(&sn9c102_sysfs_lock);
905 return -EINVAL; 909 return -EINVAL;
906 } 910 }
907 911
@@ -910,7 +914,7 @@ sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
910 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg); 914 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg);
911 DBG(3, "Written bytes: %zd", count); 915 DBG(3, "Written bytes: %zd", count);
912 916
913 up(&sn9c102_sysfs_lock); 917 mutex_unlock(&sn9c102_sysfs_lock);
914 918
915 return count; 919 return count;
916} 920}
@@ -922,17 +926,17 @@ static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
922 ssize_t count; 926 ssize_t count;
923 int val; 927 int val;
924 928
925 if (down_interruptible(&sn9c102_sysfs_lock)) 929 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
926 return -ERESTARTSYS; 930 return -ERESTARTSYS;
927 931
928 cam = video_get_drvdata(to_video_device(cd)); 932 cam = video_get_drvdata(to_video_device(cd));
929 if (!cam) { 933 if (!cam) {
930 up(&sn9c102_sysfs_lock); 934 mutex_unlock(&sn9c102_sysfs_lock);
931 return -ENODEV; 935 return -ENODEV;
932 } 936 }
933 937
934 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) { 938 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
935 up(&sn9c102_sysfs_lock); 939 mutex_unlock(&sn9c102_sysfs_lock);
936 return -EIO; 940 return -EIO;
937 } 941 }
938 942
@@ -940,7 +944,7 @@ static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
940 944
941 DBG(3, "Read bytes: %zd", count); 945 DBG(3, "Read bytes: %zd", count);
942 946
943 up(&sn9c102_sysfs_lock); 947 mutex_unlock(&sn9c102_sysfs_lock);
944 948
945 return count; 949 return count;
946} 950}
@@ -954,24 +958,24 @@ sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
954 ssize_t count; 958 ssize_t count;
955 int err; 959 int err;
956 960
957 if (down_interruptible(&sn9c102_sysfs_lock)) 961 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
958 return -ERESTARTSYS; 962 return -ERESTARTSYS;
959 963
960 cam = video_get_drvdata(to_video_device(cd)); 964 cam = video_get_drvdata(to_video_device(cd));
961 if (!cam) { 965 if (!cam) {
962 up(&sn9c102_sysfs_lock); 966 mutex_unlock(&sn9c102_sysfs_lock);
963 return -ENODEV; 967 return -ENODEV;
964 } 968 }
965 969
966 value = sn9c102_strtou8(buf, len, &count); 970 value = sn9c102_strtou8(buf, len, &count);
967 if (!count) { 971 if (!count) {
968 up(&sn9c102_sysfs_lock); 972 mutex_unlock(&sn9c102_sysfs_lock);
969 return -EINVAL; 973 return -EINVAL;
970 } 974 }
971 975
972 err = sn9c102_write_reg(cam, value, cam->sysfs.reg); 976 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
973 if (err) { 977 if (err) {
974 up(&sn9c102_sysfs_lock); 978 mutex_unlock(&sn9c102_sysfs_lock);
975 return -EIO; 979 return -EIO;
976 } 980 }
977 981
@@ -979,7 +983,7 @@ sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
979 cam->sysfs.reg, value); 983 cam->sysfs.reg, value);
980 DBG(3, "Written bytes: %zd", count); 984 DBG(3, "Written bytes: %zd", count);
981 985
982 up(&sn9c102_sysfs_lock); 986 mutex_unlock(&sn9c102_sysfs_lock);
983 987
984 return count; 988 return count;
985} 989}
@@ -990,12 +994,12 @@ static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
990 struct sn9c102_device* cam; 994 struct sn9c102_device* cam;
991 ssize_t count; 995 ssize_t count;
992 996
993 if (down_interruptible(&sn9c102_sysfs_lock)) 997 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
994 return -ERESTARTSYS; 998 return -ERESTARTSYS;
995 999
996 cam = video_get_drvdata(to_video_device(cd)); 1000 cam = video_get_drvdata(to_video_device(cd));
997 if (!cam) { 1001 if (!cam) {
998 up(&sn9c102_sysfs_lock); 1002 mutex_unlock(&sn9c102_sysfs_lock);
999 return -ENODEV; 1003 return -ENODEV;
1000 } 1004 }
1001 1005
@@ -1003,7 +1007,7 @@ static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
1003 1007
1004 DBG(3, "Read bytes: %zd", count); 1008 DBG(3, "Read bytes: %zd", count);
1005 1009
1006 up(&sn9c102_sysfs_lock); 1010 mutex_unlock(&sn9c102_sysfs_lock);
1007 1011
1008 return count; 1012 return count;
1009} 1013}
@@ -1016,18 +1020,18 @@ sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1016 u8 index; 1020 u8 index;
1017 ssize_t count; 1021 ssize_t count;
1018 1022
1019 if (down_interruptible(&sn9c102_sysfs_lock)) 1023 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1020 return -ERESTARTSYS; 1024 return -ERESTARTSYS;
1021 1025
1022 cam = video_get_drvdata(to_video_device(cd)); 1026 cam = video_get_drvdata(to_video_device(cd));
1023 if (!cam) { 1027 if (!cam) {
1024 up(&sn9c102_sysfs_lock); 1028 mutex_unlock(&sn9c102_sysfs_lock);
1025 return -ENODEV; 1029 return -ENODEV;
1026 } 1030 }
1027 1031
1028 index = sn9c102_strtou8(buf, len, &count); 1032 index = sn9c102_strtou8(buf, len, &count);
1029 if (!count) { 1033 if (!count) {
1030 up(&sn9c102_sysfs_lock); 1034 mutex_unlock(&sn9c102_sysfs_lock);
1031 return -EINVAL; 1035 return -EINVAL;
1032 } 1036 }
1033 1037
@@ -1036,7 +1040,7 @@ sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1036 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg); 1040 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1037 DBG(3, "Written bytes: %zd", count); 1041 DBG(3, "Written bytes: %zd", count);
1038 1042
1039 up(&sn9c102_sysfs_lock); 1043 mutex_unlock(&sn9c102_sysfs_lock);
1040 1044
1041 return count; 1045 return count;
1042} 1046}
@@ -1048,22 +1052,22 @@ static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1048 ssize_t count; 1052 ssize_t count;
1049 int val; 1053 int val;
1050 1054
1051 if (down_interruptible(&sn9c102_sysfs_lock)) 1055 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1052 return -ERESTARTSYS; 1056 return -ERESTARTSYS;
1053 1057
1054 cam = video_get_drvdata(to_video_device(cd)); 1058 cam = video_get_drvdata(to_video_device(cd));
1055 if (!cam) { 1059 if (!cam) {
1056 up(&sn9c102_sysfs_lock); 1060 mutex_unlock(&sn9c102_sysfs_lock);
1057 return -ENODEV; 1061 return -ENODEV;
1058 } 1062 }
1059 1063
1060 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_READ)) { 1064 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) {
1061 up(&sn9c102_sysfs_lock); 1065 mutex_unlock(&sn9c102_sysfs_lock);
1062 return -ENOSYS; 1066 return -ENOSYS;
1063 } 1067 }
1064 1068
1065 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) { 1069 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1066 up(&sn9c102_sysfs_lock); 1070 mutex_unlock(&sn9c102_sysfs_lock);
1067 return -EIO; 1071 return -EIO;
1068 } 1072 }
1069 1073
@@ -1071,7 +1075,7 @@ static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1071 1075
1072 DBG(3, "Read bytes: %zd", count); 1076 DBG(3, "Read bytes: %zd", count);
1073 1077
1074 up(&sn9c102_sysfs_lock); 1078 mutex_unlock(&sn9c102_sysfs_lock);
1075 1079
1076 return count; 1080 return count;
1077} 1081}
@@ -1085,29 +1089,29 @@ sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1085 ssize_t count; 1089 ssize_t count;
1086 int err; 1090 int err;
1087 1091
1088 if (down_interruptible(&sn9c102_sysfs_lock)) 1092 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1089 return -ERESTARTSYS; 1093 return -ERESTARTSYS;
1090 1094
1091 cam = video_get_drvdata(to_video_device(cd)); 1095 cam = video_get_drvdata(to_video_device(cd));
1092 if (!cam) { 1096 if (!cam) {
1093 up(&sn9c102_sysfs_lock); 1097 mutex_unlock(&sn9c102_sysfs_lock);
1094 return -ENODEV; 1098 return -ENODEV;
1095 } 1099 }
1096 1100
1097 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_WRITE)) { 1101 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) {
1098 up(&sn9c102_sysfs_lock); 1102 mutex_unlock(&sn9c102_sysfs_lock);
1099 return -ENOSYS; 1103 return -ENOSYS;
1100 } 1104 }
1101 1105
1102 value = sn9c102_strtou8(buf, len, &count); 1106 value = sn9c102_strtou8(buf, len, &count);
1103 if (!count) { 1107 if (!count) {
1104 up(&sn9c102_sysfs_lock); 1108 mutex_unlock(&sn9c102_sysfs_lock);
1105 return -EINVAL; 1109 return -EINVAL;
1106 } 1110 }
1107 1111
1108 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value); 1112 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1109 if (err) { 1113 if (err) {
1110 up(&sn9c102_sysfs_lock); 1114 mutex_unlock(&sn9c102_sysfs_lock);
1111 return -EIO; 1115 return -EIO;
1112 } 1116 }
1113 1117
@@ -1115,7 +1119,7 @@ sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1115 cam->sysfs.i2c_reg, value); 1119 cam->sysfs.i2c_reg, value);
1116 DBG(3, "Written bytes: %zd", count); 1120 DBG(3, "Written bytes: %zd", count);
1117 1121
1118 up(&sn9c102_sysfs_lock); 1122 mutex_unlock(&sn9c102_sysfs_lock);
1119 1123
1120 return count; 1124 return count;
1121} 1125}
@@ -1130,18 +1134,18 @@ sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1130 u8 value; 1134 u8 value;
1131 ssize_t count; 1135 ssize_t count;
1132 1136
1133 if (down_interruptible(&sn9c102_sysfs_lock)) 1137 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1134 return -ERESTARTSYS; 1138 return -ERESTARTSYS;
1135 1139
1136 cam = video_get_drvdata(to_video_device(cd)); 1140 cam = video_get_drvdata(to_video_device(cd));
1137 if (!cam) { 1141 if (!cam) {
1138 up(&sn9c102_sysfs_lock); 1142 mutex_unlock(&sn9c102_sysfs_lock);
1139 return -ENODEV; 1143 return -ENODEV;
1140 } 1144 }
1141 1145
1142 bridge = cam->bridge; 1146 bridge = cam->bridge;
1143 1147
1144 up(&sn9c102_sysfs_lock); 1148 mutex_unlock(&sn9c102_sysfs_lock);
1145 1149
1146 value = sn9c102_strtou8(buf, len, &count); 1150 value = sn9c102_strtou8(buf, len, &count);
1147 if (!count) 1151 if (!count)
@@ -1249,7 +1253,7 @@ static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1249 video_device_create_file(v4ldev, &class_device_attr_blue); 1253 video_device_create_file(v4ldev, &class_device_attr_blue);
1250 video_device_create_file(v4ldev, &class_device_attr_red); 1254 video_device_create_file(v4ldev, &class_device_attr_red);
1251 } 1255 }
1252 if (cam->sensor && cam->sensor->sysfs_ops) { 1256 if (cam->sensor.sysfs_ops) {
1253 video_device_create_file(v4ldev, &class_device_attr_i2c_reg); 1257 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1254 video_device_create_file(v4ldev, &class_device_attr_i2c_val); 1258 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1255 } 1259 }
@@ -1312,7 +1316,7 @@ static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1312 1316
1313static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect) 1317static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1314{ 1318{
1315 struct sn9c102_sensor* s = cam->sensor; 1319 struct sn9c102_sensor* s = &cam->sensor;
1316 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left), 1320 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1317 v_start = (u8)(rect->top - s->cropcap.bounds.top), 1321 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1318 h_size = (u8)(rect->width / 16), 1322 h_size = (u8)(rect->width / 16),
@@ -1335,7 +1339,7 @@ static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1335 1339
1336static int sn9c102_init(struct sn9c102_device* cam) 1340static int sn9c102_init(struct sn9c102_device* cam)
1337{ 1341{
1338 struct sn9c102_sensor* s = cam->sensor; 1342 struct sn9c102_sensor* s = &cam->sensor;
1339 struct v4l2_control ctrl; 1343 struct v4l2_control ctrl;
1340 struct v4l2_queryctrl *qctrl; 1344 struct v4l2_queryctrl *qctrl;
1341 struct v4l2_rect* rect; 1345 struct v4l2_rect* rect;
@@ -1404,7 +1408,7 @@ static int sn9c102_init(struct sn9c102_device* cam)
1404 } 1408 }
1405 1409
1406 if (!(cam->state & DEV_INITIALIZED)) { 1410 if (!(cam->state & DEV_INITIALIZED)) {
1407 init_MUTEX(&cam->fileop_sem); 1411 mutex_init(&cam->fileop_mutex);
1408 spin_lock_init(&cam->queue_lock); 1412 spin_lock_init(&cam->queue_lock);
1409 init_waitqueue_head(&cam->wait_frame); 1413 init_waitqueue_head(&cam->wait_frame);
1410 init_waitqueue_head(&cam->wait_stream); 1414 init_waitqueue_head(&cam->wait_stream);
@@ -1422,13 +1426,15 @@ static int sn9c102_init(struct sn9c102_device* cam)
1422 1426
1423static void sn9c102_release_resources(struct sn9c102_device* cam) 1427static void sn9c102_release_resources(struct sn9c102_device* cam)
1424{ 1428{
1425 down(&sn9c102_sysfs_lock); 1429 mutex_lock(&sn9c102_sysfs_lock);
1426 1430
1427 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor); 1431 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1428 video_set_drvdata(cam->v4ldev, NULL); 1432 video_set_drvdata(cam->v4ldev, NULL);
1429 video_unregister_device(cam->v4ldev); 1433 video_unregister_device(cam->v4ldev);
1430 1434
1431 up(&sn9c102_sysfs_lock); 1435 usb_put_dev(cam->usbdev);
1436
1437 mutex_unlock(&sn9c102_sysfs_lock);
1432 1438
1433 kfree(cam->control_buffer); 1439 kfree(cam->control_buffer);
1434} 1440}
@@ -1449,7 +1455,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1449 1455
1450 cam = video_get_drvdata(video_devdata(filp)); 1456 cam = video_get_drvdata(video_devdata(filp));
1451 1457
1452 if (down_interruptible(&cam->dev_sem)) { 1458 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1453 up_read(&sn9c102_disconnect); 1459 up_read(&sn9c102_disconnect);
1454 return -ERESTARTSYS; 1460 return -ERESTARTSYS;
1455 } 1461 }
@@ -1461,7 +1467,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1461 err = -EWOULDBLOCK; 1467 err = -EWOULDBLOCK;
1462 goto out; 1468 goto out;
1463 } 1469 }
1464 up(&cam->dev_sem); 1470 mutex_unlock(&cam->dev_mutex);
1465 err = wait_event_interruptible_exclusive(cam->open, 1471 err = wait_event_interruptible_exclusive(cam->open,
1466 cam->state & DEV_DISCONNECTED 1472 cam->state & DEV_DISCONNECTED
1467 || !cam->users); 1473 || !cam->users);
@@ -1473,7 +1479,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1473 up_read(&sn9c102_disconnect); 1479 up_read(&sn9c102_disconnect);
1474 return -ENODEV; 1480 return -ENODEV;
1475 } 1481 }
1476 down(&cam->dev_sem); 1482 mutex_lock(&cam->dev_mutex);
1477 } 1483 }
1478 1484
1479 1485
@@ -1501,7 +1507,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1501 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor); 1507 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1502 1508
1503out: 1509out:
1504 up(&cam->dev_sem); 1510 mutex_unlock(&cam->dev_mutex);
1505 up_read(&sn9c102_disconnect); 1511 up_read(&sn9c102_disconnect);
1506 return err; 1512 return err;
1507} 1513}
@@ -1511,7 +1517,7 @@ static int sn9c102_release(struct inode* inode, struct file* filp)
1511{ 1517{
1512 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 1518 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1513 1519
1514 down(&cam->dev_sem); /* prevent disconnect() to be called */ 1520 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1515 1521
1516 sn9c102_stop_transfer(cam); 1522 sn9c102_stop_transfer(cam);
1517 1523
@@ -1519,7 +1525,7 @@ static int sn9c102_release(struct inode* inode, struct file* filp)
1519 1525
1520 if (cam->state & DEV_DISCONNECTED) { 1526 if (cam->state & DEV_DISCONNECTED) {
1521 sn9c102_release_resources(cam); 1527 sn9c102_release_resources(cam);
1522 up(&cam->dev_sem); 1528 mutex_unlock(&cam->dev_mutex);
1523 kfree(cam); 1529 kfree(cam);
1524 return 0; 1530 return 0;
1525 } 1531 }
@@ -1529,7 +1535,7 @@ static int sn9c102_release(struct inode* inode, struct file* filp)
1529 1535
1530 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor); 1536 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1531 1537
1532 up(&cam->dev_sem); 1538 mutex_unlock(&cam->dev_mutex);
1533 1539
1534 return 0; 1540 return 0;
1535} 1541}
@@ -1541,35 +1547,36 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1541 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 1547 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1542 struct sn9c102_frame_t* f, * i; 1548 struct sn9c102_frame_t* f, * i;
1543 unsigned long lock_flags; 1549 unsigned long lock_flags;
1550 long timeout;
1544 int err = 0; 1551 int err = 0;
1545 1552
1546 if (down_interruptible(&cam->fileop_sem)) 1553 if (mutex_lock_interruptible(&cam->fileop_mutex))
1547 return -ERESTARTSYS; 1554 return -ERESTARTSYS;
1548 1555
1549 if (cam->state & DEV_DISCONNECTED) { 1556 if (cam->state & DEV_DISCONNECTED) {
1550 DBG(1, "Device not present"); 1557 DBG(1, "Device not present");
1551 up(&cam->fileop_sem); 1558 mutex_unlock(&cam->fileop_mutex);
1552 return -ENODEV; 1559 return -ENODEV;
1553 } 1560 }
1554 1561
1555 if (cam->state & DEV_MISCONFIGURED) { 1562 if (cam->state & DEV_MISCONFIGURED) {
1556 DBG(1, "The camera is misconfigured. Close and open it " 1563 DBG(1, "The camera is misconfigured. Close and open it "
1557 "again."); 1564 "again.");
1558 up(&cam->fileop_sem); 1565 mutex_unlock(&cam->fileop_mutex);
1559 return -EIO; 1566 return -EIO;
1560 } 1567 }
1561 1568
1562 if (cam->io == IO_MMAP) { 1569 if (cam->io == IO_MMAP) {
1563 DBG(3, "Close and open the device again to choose " 1570 DBG(3, "Close and open the device again to choose "
1564 "the read method"); 1571 "the read method");
1565 up(&cam->fileop_sem); 1572 mutex_unlock(&cam->fileop_mutex);
1566 return -EINVAL; 1573 return -EINVAL;
1567 } 1574 }
1568 1575
1569 if (cam->io == IO_NONE) { 1576 if (cam->io == IO_NONE) {
1570 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) { 1577 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1571 DBG(1, "read() failed, not enough memory"); 1578 DBG(1, "read() failed, not enough memory");
1572 up(&cam->fileop_sem); 1579 mutex_unlock(&cam->fileop_mutex);
1573 return -ENOMEM; 1580 return -ENOMEM;
1574 } 1581 }
1575 cam->io = IO_READ; 1582 cam->io = IO_READ;
@@ -1583,30 +1590,32 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1583 } 1590 }
1584 1591
1585 if (!count) { 1592 if (!count) {
1586 up(&cam->fileop_sem); 1593 mutex_unlock(&cam->fileop_mutex);
1587 return 0; 1594 return 0;
1588 } 1595 }
1589 1596
1590 if (list_empty(&cam->outqueue)) { 1597 if (list_empty(&cam->outqueue)) {
1591 if (filp->f_flags & O_NONBLOCK) { 1598 if (filp->f_flags & O_NONBLOCK) {
1592 up(&cam->fileop_sem); 1599 mutex_unlock(&cam->fileop_mutex);
1593 return -EAGAIN; 1600 return -EAGAIN;
1594 } 1601 }
1595 err = wait_event_interruptible 1602 timeout = wait_event_interruptible_timeout
1596 ( cam->wait_frame, 1603 ( cam->wait_frame,
1597 (!list_empty(&cam->outqueue)) || 1604 (!list_empty(&cam->outqueue)) ||
1598 (cam->state & DEV_DISCONNECTED) || 1605 (cam->state & DEV_DISCONNECTED) ||
1599 (cam->state & DEV_MISCONFIGURED) ); 1606 (cam->state & DEV_MISCONFIGURED),
1600 if (err) { 1607 cam->module_param.frame_timeout *
1601 up(&cam->fileop_sem); 1608 1000 * msecs_to_jiffies(1) );
1602 return err; 1609 if (timeout < 0) {
1610 mutex_unlock(&cam->fileop_mutex);
1611 return timeout;
1603 } 1612 }
1604 if (cam->state & DEV_DISCONNECTED) { 1613 if (cam->state & DEV_DISCONNECTED) {
1605 up(&cam->fileop_sem); 1614 mutex_unlock(&cam->fileop_mutex);
1606 return -ENODEV; 1615 return -ENODEV;
1607 } 1616 }
1608 if (cam->state & DEV_MISCONFIGURED) { 1617 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
1609 up(&cam->fileop_sem); 1618 mutex_unlock(&cam->fileop_mutex);
1610 return -EIO; 1619 return -EIO;
1611 } 1620 }
1612 } 1621 }
@@ -1634,7 +1643,7 @@ exit:
1634 PDBGG("Frame #%lu, bytes read: %zu", 1643 PDBGG("Frame #%lu, bytes read: %zu",
1635 (unsigned long)f->buf.index, count); 1644 (unsigned long)f->buf.index, count);
1636 1645
1637 up(&cam->fileop_sem); 1646 mutex_unlock(&cam->fileop_mutex);
1638 1647
1639 return count; 1648 return count;
1640} 1649}
@@ -1647,7 +1656,7 @@ static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1647 unsigned long lock_flags; 1656 unsigned long lock_flags;
1648 unsigned int mask = 0; 1657 unsigned int mask = 0;
1649 1658
1650 if (down_interruptible(&cam->fileop_sem)) 1659 if (mutex_lock_interruptible(&cam->fileop_mutex))
1651 return POLLERR; 1660 return POLLERR;
1652 1661
1653 if (cam->state & DEV_DISCONNECTED) { 1662 if (cam->state & DEV_DISCONNECTED) {
@@ -1685,12 +1694,12 @@ static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1685 if (!list_empty(&cam->outqueue)) 1694 if (!list_empty(&cam->outqueue))
1686 mask |= POLLIN | POLLRDNORM; 1695 mask |= POLLIN | POLLRDNORM;
1687 1696
1688 up(&cam->fileop_sem); 1697 mutex_unlock(&cam->fileop_mutex);
1689 1698
1690 return mask; 1699 return mask;
1691 1700
1692error: 1701error:
1693 up(&cam->fileop_sem); 1702 mutex_unlock(&cam->fileop_mutex);
1694 return POLLERR; 1703 return POLLERR;
1695} 1704}
1696 1705
@@ -1724,25 +1733,25 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1724 void *pos; 1733 void *pos;
1725 u32 i; 1734 u32 i;
1726 1735
1727 if (down_interruptible(&cam->fileop_sem)) 1736 if (mutex_lock_interruptible(&cam->fileop_mutex))
1728 return -ERESTARTSYS; 1737 return -ERESTARTSYS;
1729 1738
1730 if (cam->state & DEV_DISCONNECTED) { 1739 if (cam->state & DEV_DISCONNECTED) {
1731 DBG(1, "Device not present"); 1740 DBG(1, "Device not present");
1732 up(&cam->fileop_sem); 1741 mutex_unlock(&cam->fileop_mutex);
1733 return -ENODEV; 1742 return -ENODEV;
1734 } 1743 }
1735 1744
1736 if (cam->state & DEV_MISCONFIGURED) { 1745 if (cam->state & DEV_MISCONFIGURED) {
1737 DBG(1, "The camera is misconfigured. Close and open it " 1746 DBG(1, "The camera is misconfigured. Close and open it "
1738 "again."); 1747 "again.");
1739 up(&cam->fileop_sem); 1748 mutex_unlock(&cam->fileop_mutex);
1740 return -EIO; 1749 return -EIO;
1741 } 1750 }
1742 1751
1743 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) || 1752 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1744 size != PAGE_ALIGN(cam->frame[0].buf.length)) { 1753 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1745 up(&cam->fileop_sem); 1754 mutex_unlock(&cam->fileop_mutex);
1746 return -EINVAL; 1755 return -EINVAL;
1747 } 1756 }
1748 1757
@@ -1751,7 +1760,7 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1751 break; 1760 break;
1752 } 1761 }
1753 if (i == cam->nbuffers) { 1762 if (i == cam->nbuffers) {
1754 up(&cam->fileop_sem); 1763 mutex_unlock(&cam->fileop_mutex);
1755 return -EINVAL; 1764 return -EINVAL;
1756 } 1765 }
1757 1766
@@ -1761,7 +1770,7 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1761 pos = cam->frame[i].bufmem; 1770 pos = cam->frame[i].bufmem;
1762 while (size > 0) { /* size is page-aligned */ 1771 while (size > 0) { /* size is page-aligned */
1763 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) { 1772 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1764 up(&cam->fileop_sem); 1773 mutex_unlock(&cam->fileop_mutex);
1765 return -EAGAIN; 1774 return -EAGAIN;
1766 } 1775 }
1767 start += PAGE_SIZE; 1776 start += PAGE_SIZE;
@@ -1774,7 +1783,7 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1774 1783
1775 sn9c102_vm_open(vma); 1784 sn9c102_vm_open(vma);
1776 1785
1777 up(&cam->fileop_sem); 1786 mutex_unlock(&cam->fileop_mutex);
1778 1787
1779 return 0; 1788 return 0;
1780} 1789}
@@ -1816,6 +1825,7 @@ sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
1816 1825
1817 memset(&i, 0, sizeof(i)); 1826 memset(&i, 0, sizeof(i));
1818 strcpy(i.name, "Camera"); 1827 strcpy(i.name, "Camera");
1828 i.type = V4L2_INPUT_TYPE_CAMERA;
1819 1829
1820 if (copy_to_user(arg, &i, sizeof(i))) 1830 if (copy_to_user(arg, &i, sizeof(i)))
1821 return -EFAULT; 1831 return -EFAULT;
@@ -1825,7 +1835,19 @@ sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
1825 1835
1826 1836
1827static int 1837static int
1828sn9c102_vidioc_gs_input(struct sn9c102_device* cam, void __user * arg) 1838sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg)
1839{
1840 int index = 0;
1841
1842 if (copy_to_user(arg, &index, sizeof(index)))
1843 return -EFAULT;
1844
1845 return 0;
1846}
1847
1848
1849static int
1850sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg)
1829{ 1851{
1830 int index; 1852 int index;
1831 1853
@@ -1842,7 +1864,7 @@ sn9c102_vidioc_gs_input(struct sn9c102_device* cam, void __user * arg)
1842static int 1864static int
1843sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg) 1865sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
1844{ 1866{
1845 struct sn9c102_sensor* s = cam->sensor; 1867 struct sn9c102_sensor* s = &cam->sensor;
1846 struct v4l2_queryctrl qc; 1868 struct v4l2_queryctrl qc;
1847 u8 i; 1869 u8 i;
1848 1870
@@ -1864,7 +1886,7 @@ sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
1864static int 1886static int
1865sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg) 1887sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
1866{ 1888{
1867 struct sn9c102_sensor* s = cam->sensor; 1889 struct sn9c102_sensor* s = &cam->sensor;
1868 struct v4l2_control ctrl; 1890 struct v4l2_control ctrl;
1869 int err = 0; 1891 int err = 0;
1870 u8 i; 1892 u8 i;
@@ -1896,7 +1918,7 @@ exit:
1896static int 1918static int
1897sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg) 1919sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1898{ 1920{
1899 struct sn9c102_sensor* s = cam->sensor; 1921 struct sn9c102_sensor* s = &cam->sensor;
1900 struct v4l2_control ctrl; 1922 struct v4l2_control ctrl;
1901 u8 i; 1923 u8 i;
1902 int err = 0; 1924 int err = 0;
@@ -1909,6 +1931,8 @@ sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1909 1931
1910 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) 1932 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1911 if (ctrl.id == s->qctrl[i].id) { 1933 if (ctrl.id == s->qctrl[i].id) {
1934 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1935 return -EINVAL;
1912 if (ctrl.value < s->qctrl[i].minimum || 1936 if (ctrl.value < s->qctrl[i].minimum ||
1913 ctrl.value > s->qctrl[i].maximum) 1937 ctrl.value > s->qctrl[i].maximum)
1914 return -ERANGE; 1938 return -ERANGE;
@@ -1931,7 +1955,7 @@ sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1931static int 1955static int
1932sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg) 1956sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
1933{ 1957{
1934 struct v4l2_cropcap* cc = &(cam->sensor->cropcap); 1958 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1935 1959
1936 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1960 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1937 cc->pixelaspect.numerator = 1; 1961 cc->pixelaspect.numerator = 1;
@@ -1947,7 +1971,7 @@ sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
1947static int 1971static int
1948sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg) 1972sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
1949{ 1973{
1950 struct sn9c102_sensor* s = cam->sensor; 1974 struct sn9c102_sensor* s = &cam->sensor;
1951 struct v4l2_crop crop = { 1975 struct v4l2_crop crop = {
1952 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, 1976 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1953 }; 1977 };
@@ -1964,7 +1988,7 @@ sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
1964static int 1988static int
1965sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg) 1989sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
1966{ 1990{
1967 struct sn9c102_sensor* s = cam->sensor; 1991 struct sn9c102_sensor* s = &cam->sensor;
1968 struct v4l2_crop crop; 1992 struct v4l2_crop crop;
1969 struct v4l2_rect* rect; 1993 struct v4l2_rect* rect;
1970 struct v4l2_rect* bounds = &(s->cropcap.bounds); 1994 struct v4l2_rect* bounds = &(s->cropcap.bounds);
@@ -2105,7 +2129,7 @@ static int
2105sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg) 2129sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2106{ 2130{
2107 struct v4l2_format format; 2131 struct v4l2_format format;
2108 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format); 2132 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
2109 2133
2110 if (copy_from_user(&format, arg, sizeof(format))) 2134 if (copy_from_user(&format, arg, sizeof(format)))
2111 return -EFAULT; 2135 return -EFAULT;
@@ -2130,7 +2154,7 @@ static int
2130sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd, 2154sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2131 void __user * arg) 2155 void __user * arg)
2132{ 2156{
2133 struct sn9c102_sensor* s = cam->sensor; 2157 struct sn9c102_sensor* s = &cam->sensor;
2134 struct v4l2_format format; 2158 struct v4l2_format format;
2135 struct v4l2_pix_format* pix; 2159 struct v4l2_pix_format* pix;
2136 struct v4l2_pix_format* pfmt = &(s->pix_format); 2160 struct v4l2_pix_format* pfmt = &(s->pix_format);
@@ -2417,7 +2441,7 @@ sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2417 struct v4l2_buffer b; 2441 struct v4l2_buffer b;
2418 struct sn9c102_frame_t *f; 2442 struct sn9c102_frame_t *f;
2419 unsigned long lock_flags; 2443 unsigned long lock_flags;
2420 int err = 0; 2444 long timeout;
2421 2445
2422 if (copy_from_user(&b, arg, sizeof(b))) 2446 if (copy_from_user(&b, arg, sizeof(b)))
2423 return -EFAULT; 2447 return -EFAULT;
@@ -2430,16 +2454,18 @@ sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2430 return -EINVAL; 2454 return -EINVAL;
2431 if (filp->f_flags & O_NONBLOCK) 2455 if (filp->f_flags & O_NONBLOCK)
2432 return -EAGAIN; 2456 return -EAGAIN;
2433 err = wait_event_interruptible 2457 timeout = wait_event_interruptible_timeout
2434 ( cam->wait_frame, 2458 ( cam->wait_frame,
2435 (!list_empty(&cam->outqueue)) || 2459 (!list_empty(&cam->outqueue)) ||
2436 (cam->state & DEV_DISCONNECTED) || 2460 (cam->state & DEV_DISCONNECTED) ||
2437 (cam->state & DEV_MISCONFIGURED) ); 2461 (cam->state & DEV_MISCONFIGURED),
2438 if (err) 2462 cam->module_param.frame_timeout *
2439 return err; 2463 1000 * msecs_to_jiffies(1) );
2464 if (timeout < 0)
2465 return timeout;
2440 if (cam->state & DEV_DISCONNECTED) 2466 if (cam->state & DEV_DISCONNECTED)
2441 return -ENODEV; 2467 return -ENODEV;
2442 if (cam->state & DEV_MISCONFIGURED) 2468 if (!timeout || (cam->state & DEV_MISCONFIGURED))
2443 return -EIO; 2469 return -EIO;
2444 } 2470 }
2445 2471
@@ -2571,8 +2597,10 @@ static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
2571 return sn9c102_vidioc_enuminput(cam, arg); 2597 return sn9c102_vidioc_enuminput(cam, arg);
2572 2598
2573 case VIDIOC_G_INPUT: 2599 case VIDIOC_G_INPUT:
2600 return sn9c102_vidioc_g_input(cam, arg);
2601
2574 case VIDIOC_S_INPUT: 2602 case VIDIOC_S_INPUT:
2575 return sn9c102_vidioc_gs_input(cam, arg); 2603 return sn9c102_vidioc_s_input(cam, arg);
2576 2604
2577 case VIDIOC_QUERYCTRL: 2605 case VIDIOC_QUERYCTRL:
2578 return sn9c102_vidioc_query_ctrl(cam, arg); 2606 return sn9c102_vidioc_query_ctrl(cam, arg);
@@ -2655,19 +2683,19 @@ static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2655 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 2683 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2656 int err = 0; 2684 int err = 0;
2657 2685
2658 if (down_interruptible(&cam->fileop_sem)) 2686 if (mutex_lock_interruptible(&cam->fileop_mutex))
2659 return -ERESTARTSYS; 2687 return -ERESTARTSYS;
2660 2688
2661 if (cam->state & DEV_DISCONNECTED) { 2689 if (cam->state & DEV_DISCONNECTED) {
2662 DBG(1, "Device not present"); 2690 DBG(1, "Device not present");
2663 up(&cam->fileop_sem); 2691 mutex_unlock(&cam->fileop_mutex);
2664 return -ENODEV; 2692 return -ENODEV;
2665 } 2693 }
2666 2694
2667 if (cam->state & DEV_MISCONFIGURED) { 2695 if (cam->state & DEV_MISCONFIGURED) {
2668 DBG(1, "The camera is misconfigured. Close and open it " 2696 DBG(1, "The camera is misconfigured. Close and open it "
2669 "again."); 2697 "again.");
2670 up(&cam->fileop_sem); 2698 mutex_unlock(&cam->fileop_mutex);
2671 return -EIO; 2699 return -EIO;
2672 } 2700 }
2673 2701
@@ -2675,7 +2703,7 @@ static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2675 2703
2676 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg); 2704 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2677 2705
2678 up(&cam->fileop_sem); 2706 mutex_unlock(&cam->fileop_mutex);
2679 2707
2680 return err; 2708 return err;
2681} 2709}
@@ -2722,7 +2750,7 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2722 goto fail; 2750 goto fail;
2723 } 2751 }
2724 2752
2725 init_MUTEX(&cam->dev_sem); 2753 mutex_init(&cam->dev_mutex);
2726 2754
2727 r = sn9c102_read_reg(cam, 0x00); 2755 r = sn9c102_read_reg(cam, 0x00);
2728 if (r < 0 || r != 0x10) { 2756 if (r < 0 || r != 0x10) {
@@ -2752,10 +2780,10 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2752 break; 2780 break;
2753 } 2781 }
2754 2782
2755 if (!err && cam->sensor) { 2783 if (!err) {
2756 DBG(2, "%s image sensor detected", cam->sensor->name); 2784 DBG(2, "%s image sensor detected", cam->sensor.name);
2757 DBG(3, "Support for %s maintained by %s", 2785 DBG(3, "Support for %s maintained by %s",
2758 cam->sensor->name, cam->sensor->maintainer); 2786 cam->sensor.name, cam->sensor.maintainer);
2759 } else { 2787 } else {
2760 DBG(1, "No supported image sensor detected"); 2788 DBG(1, "No supported image sensor detected");
2761 err = -ENODEV; 2789 err = -ENODEV;
@@ -2776,7 +2804,7 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2776 cam->v4ldev->release = video_device_release; 2804 cam->v4ldev->release = video_device_release;
2777 video_set_drvdata(cam->v4ldev, cam); 2805 video_set_drvdata(cam->v4ldev, cam);
2778 2806
2779 down(&cam->dev_sem); 2807 mutex_lock(&cam->dev_mutex);
2780 2808
2781 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER, 2809 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2782 video_nr[dev_nr]); 2810 video_nr[dev_nr]);
@@ -2786,13 +2814,14 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2786 DBG(1, "Free /dev/videoX node not found"); 2814 DBG(1, "Free /dev/videoX node not found");
2787 video_nr[dev_nr] = -1; 2815 video_nr[dev_nr] = -1;
2788 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; 2816 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2789 up(&cam->dev_sem); 2817 mutex_unlock(&cam->dev_mutex);
2790 goto fail; 2818 goto fail;
2791 } 2819 }
2792 2820
2793 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor); 2821 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2794 2822
2795 cam->module_param.force_munmap = force_munmap[dev_nr]; 2823 cam->module_param.force_munmap = force_munmap[dev_nr];
2824 cam->module_param.frame_timeout = frame_timeout[dev_nr];
2796 2825
2797 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; 2826 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2798 2827
@@ -2803,7 +2832,7 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2803 2832
2804 usb_set_intfdata(intf, cam); 2833 usb_set_intfdata(intf, cam);
2805 2834
2806 up(&cam->dev_sem); 2835 mutex_unlock(&cam->dev_mutex);
2807 2836
2808 return 0; 2837 return 0;
2809 2838
@@ -2827,7 +2856,7 @@ static void sn9c102_usb_disconnect(struct usb_interface* intf)
2827 2856
2828 down_write(&sn9c102_disconnect); 2857 down_write(&sn9c102_disconnect);
2829 2858
2830 down(&cam->dev_sem); 2859 mutex_lock(&cam->dev_mutex);
2831 2860
2832 DBG(2, "Disconnecting %s...", cam->v4ldev->name); 2861 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2833 2862
@@ -2841,13 +2870,14 @@ static void sn9c102_usb_disconnect(struct usb_interface* intf)
2841 sn9c102_stop_transfer(cam); 2870 sn9c102_stop_transfer(cam);
2842 cam->state |= DEV_DISCONNECTED; 2871 cam->state |= DEV_DISCONNECTED;
2843 wake_up_interruptible(&cam->wait_frame); 2872 wake_up_interruptible(&cam->wait_frame);
2844 wake_up_interruptible(&cam->wait_stream); 2873 wake_up(&cam->wait_stream);
2874 usb_get_dev(cam->usbdev);
2845 } else { 2875 } else {
2846 cam->state |= DEV_DISCONNECTED; 2876 cam->state |= DEV_DISCONNECTED;
2847 sn9c102_release_resources(cam); 2877 sn9c102_release_resources(cam);
2848 } 2878 }
2849 2879
2850 up(&cam->dev_sem); 2880 mutex_unlock(&cam->dev_mutex);
2851 2881
2852 if (!cam->users) 2882 if (!cam->users)
2853 kfree(cam); 2883 kfree(cam);
diff --git a/drivers/usb/media/sn9c102_ov7630.c b/drivers/usb/media/sn9c102_ov7630.c
index 4a36519b5af4..42852b7cb042 100644
--- a/drivers/usb/media/sn9c102_ov7630.c
+++ b/drivers/usb/media/sn9c102_ov7630.c
@@ -34,8 +34,8 @@ static int ov7630_init(struct sn9c102_device* cam)
34 err += sn9c102_write_reg(cam, 0x0f, 0x18); 34 err += sn9c102_write_reg(cam, 0x0f, 0x18);
35 err += sn9c102_write_reg(cam, 0x50, 0x19); 35 err += sn9c102_write_reg(cam, 0x50, 0x19);
36 36
37 err += sn9c102_i2c_write(cam, 0x12, 0x8d); 37 err += sn9c102_i2c_write(cam, 0x12, 0x80);
38 err += sn9c102_i2c_write(cam, 0x11, 0x00); 38 err += sn9c102_i2c_write(cam, 0x11, 0x01);
39 err += sn9c102_i2c_write(cam, 0x15, 0x34); 39 err += sn9c102_i2c_write(cam, 0x15, 0x34);
40 err += sn9c102_i2c_write(cam, 0x16, 0x03); 40 err += sn9c102_i2c_write(cam, 0x16, 0x03);
41 err += sn9c102_i2c_write(cam, 0x17, 0x1c); 41 err += sn9c102_i2c_write(cam, 0x17, 0x1c);
@@ -43,12 +43,14 @@ static int ov7630_init(struct sn9c102_device* cam)
43 err += sn9c102_i2c_write(cam, 0x19, 0x06); 43 err += sn9c102_i2c_write(cam, 0x19, 0x06);
44 err += sn9c102_i2c_write(cam, 0x1a, 0xf6); 44 err += sn9c102_i2c_write(cam, 0x1a, 0xf6);
45 err += sn9c102_i2c_write(cam, 0x1b, 0x04); 45 err += sn9c102_i2c_write(cam, 0x1b, 0x04);
46 err += sn9c102_i2c_write(cam, 0x20, 0x44); 46 err += sn9c102_i2c_write(cam, 0x20, 0xf6);
47 err += sn9c102_i2c_write(cam, 0x23, 0xee); 47 err += sn9c102_i2c_write(cam, 0x23, 0xee);
48 err += sn9c102_i2c_write(cam, 0x26, 0xa0); 48 err += sn9c102_i2c_write(cam, 0x26, 0xa0);
49 err += sn9c102_i2c_write(cam, 0x27, 0x9a); 49 err += sn9c102_i2c_write(cam, 0x27, 0x9a);
50 err += sn9c102_i2c_write(cam, 0x28, 0x20); 50 err += sn9c102_i2c_write(cam, 0x28, 0xa0);
51 err += sn9c102_i2c_write(cam, 0x29, 0x30); 51 err += sn9c102_i2c_write(cam, 0x29, 0x30);
52 err += sn9c102_i2c_write(cam, 0x2a, 0xa0);
53 err += sn9c102_i2c_write(cam, 0x2b, 0x1f);
52 err += sn9c102_i2c_write(cam, 0x2f, 0x3d); 54 err += sn9c102_i2c_write(cam, 0x2f, 0x3d);
53 err += sn9c102_i2c_write(cam, 0x30, 0x24); 55 err += sn9c102_i2c_write(cam, 0x30, 0x24);
54 err += sn9c102_i2c_write(cam, 0x32, 0x86); 56 err += sn9c102_i2c_write(cam, 0x32, 0x86);
@@ -80,7 +82,7 @@ static int ov7630_set_ctrl(struct sn9c102_device* cam,
80 err += sn9c102_i2c_write(cam, 0x02, ctrl->value); 82 err += sn9c102_i2c_write(cam, 0x02, ctrl->value);
81 break; 83 break;
82 case V4L2_CID_BLUE_BALANCE: 84 case V4L2_CID_BLUE_BALANCE:
83 err += sn9c102_i2c_write(cam, 0x03, ctrl->value); 85 err += sn9c102_i2c_write(cam, 0x01, ctrl->value);
84 break; 86 break;
85 case V4L2_CID_GAIN: 87 case V4L2_CID_GAIN:
86 err += sn9c102_i2c_write(cam, 0x00, ctrl->value); 88 err += sn9c102_i2c_write(cam, 0x00, ctrl->value);
@@ -108,7 +110,7 @@ static int ov7630_set_ctrl(struct sn9c102_device* cam,
108 err += sn9c102_i2c_write(cam, 0x0d, ctrl->value); 110 err += sn9c102_i2c_write(cam, 0x0d, ctrl->value);
109 break; 111 break;
110 case V4L2_CID_AUTO_WHITE_BALANCE: 112 case V4L2_CID_AUTO_WHITE_BALANCE:
111 err += sn9c102_i2c_write(cam, 0x12, (ctrl->value << 2) | 0x09); 113 err += sn9c102_i2c_write(cam, 0x12, (ctrl->value << 2) | 0x78);
112 break; 114 break;
113 case V4L2_CID_AUTOGAIN: 115 case V4L2_CID_AUTOGAIN:
114 err += sn9c102_i2c_write(cam, 0x13, ctrl->value); 116 err += sn9c102_i2c_write(cam, 0x13, ctrl->value);
@@ -371,26 +373,29 @@ static struct sn9c102_sensor ov7630 = {
371 373
372int sn9c102_probe_ov7630(struct sn9c102_device* cam) 374int sn9c102_probe_ov7630(struct sn9c102_device* cam)
373{ 375{
376 const struct usb_device_id ov7630_id_table[] = {
377 { USB_DEVICE(0x0c45, 0x602c), },
378 { USB_DEVICE(0x0c45, 0x602d), },
379 { USB_DEVICE(0x0c45, 0x608f), },
380 { USB_DEVICE(0x0c45, 0x60b0), },
381 { }
382 };
374 int err = 0; 383 int err = 0;
375 384
376 sn9c102_attach_sensor(cam, &ov7630); 385 if (!sn9c102_match_id(cam, ov7630_id_table))
377
378 if (le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x602c &&
379 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x602d &&
380 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x608f &&
381 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x60b0)
382 return -ENODEV; 386 return -ENODEV;
383 387
384 err += sn9c102_write_reg(cam, 0x01, 0x01); 388 err += sn9c102_write_reg(cam, 0x01, 0x01);
385 err += sn9c102_write_reg(cam, 0x00, 0x01); 389 err += sn9c102_write_reg(cam, 0x00, 0x01);
386 err += sn9c102_write_reg(cam, 0x28, 0x17); 390 err += sn9c102_write_reg(cam, 0x28, 0x17);
387
388 if (err) 391 if (err)
389 return -EIO; 392 return -EIO;
390 393
391 err += sn9c102_i2c_write(cam, 0x0b, 0); 394 err += sn9c102_i2c_try_write(cam, &ov7630, 0x0b, 0);
392 if (err) 395 if (err)
393 return -ENODEV; 396 return -ENODEV;
394 397
398 sn9c102_attach_sensor(cam, &ov7630);
399
395 return 0; 400 return 0;
396} 401}
diff --git a/drivers/usb/media/sn9c102_pas202bca.c b/drivers/usb/media/sn9c102_pas202bca.c
new file mode 100644
index 000000000000..3453237055bb
--- /dev/null
+++ b/drivers/usb/media/sn9c102_pas202bca.c
@@ -0,0 +1,238 @@
1/***************************************************************************
2 * Plug-in for PAS202BCA image sensor connected to the SN9C10x PC Camera *
3 * Controllers *
4 * *
5 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#include <linux/delay.h>
23#include "sn9c102_sensor.h"
24
25
26static struct sn9c102_sensor pas202bca;
27
28
29static int pas202bca_init(struct sn9c102_device* cam)
30{
31 int err = 0;
32
33 err += sn9c102_write_reg(cam, 0x00, 0x10);
34 err += sn9c102_write_reg(cam, 0x00, 0x11);
35 err += sn9c102_write_reg(cam, 0x00, 0x14);
36 err += sn9c102_write_reg(cam, 0x20, 0x17);
37 err += sn9c102_write_reg(cam, 0x30, 0x19);
38 err += sn9c102_write_reg(cam, 0x09, 0x18);
39
40 err += sn9c102_i2c_write(cam, 0x02, 0x14);
41 err += sn9c102_i2c_write(cam, 0x03, 0x40);
42 err += sn9c102_i2c_write(cam, 0x0d, 0x2c);
43 err += sn9c102_i2c_write(cam, 0x0e, 0x01);
44 err += sn9c102_i2c_write(cam, 0x0f, 0xa9);
45 err += sn9c102_i2c_write(cam, 0x10, 0x08);
46 err += sn9c102_i2c_write(cam, 0x13, 0x63);
47 err += sn9c102_i2c_write(cam, 0x15, 0x70);
48 err += sn9c102_i2c_write(cam, 0x11, 0x01);
49
50 msleep(400);
51
52 return err;
53}
54
55
56static int pas202bca_set_pix_format(struct sn9c102_device* cam,
57 const struct v4l2_pix_format* pix)
58{
59 int err = 0;
60
61 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
62 err += sn9c102_write_reg(cam, 0x24, 0x17);
63 else
64 err += sn9c102_write_reg(cam, 0x20, 0x17);
65
66 return err;
67}
68
69
70static int pas202bca_set_ctrl(struct sn9c102_device* cam,
71 const struct v4l2_control* ctrl)
72{
73 int err = 0;
74
75 switch (ctrl->id) {
76 case V4L2_CID_EXPOSURE:
77 err += sn9c102_i2c_write(cam, 0x04, ctrl->value >> 6);
78 err += sn9c102_i2c_write(cam, 0x05, ctrl->value & 0x3f);
79 break;
80 case V4L2_CID_RED_BALANCE:
81 err += sn9c102_i2c_write(cam, 0x09, ctrl->value);
82 break;
83 case V4L2_CID_BLUE_BALANCE:
84 err += sn9c102_i2c_write(cam, 0x07, ctrl->value);
85 break;
86 case V4L2_CID_GAIN:
87 err += sn9c102_i2c_write(cam, 0x10, ctrl->value);
88 break;
89 case SN9C102_V4L2_CID_GREEN_BALANCE:
90 err += sn9c102_i2c_write(cam, 0x08, ctrl->value);
91 break;
92 case SN9C102_V4L2_CID_DAC_MAGNITUDE:
93 err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
94 break;
95 default:
96 return -EINVAL;
97 }
98 err += sn9c102_i2c_write(cam, 0x11, 0x01);
99
100 return err ? -EIO : 0;
101}
102
103
104static int pas202bca_set_crop(struct sn9c102_device* cam,
105 const struct v4l2_rect* rect)
106{
107 struct sn9c102_sensor* s = &pas202bca;
108 int err = 0;
109 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 3,
110 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3;
111
112 err += sn9c102_write_reg(cam, h_start, 0x12);
113 err += sn9c102_write_reg(cam, v_start, 0x13);
114
115 return err;
116}
117
118
119static struct sn9c102_sensor pas202bca = {
120 .name = "PAS202BCA",
121 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
122 .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
123 .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ,
124 .interface = SN9C102_I2C_2WIRES,
125 .i2c_slave_id = 0x40,
126 .init = &pas202bca_init,
127 .qctrl = {
128 {
129 .id = V4L2_CID_EXPOSURE,
130 .type = V4L2_CTRL_TYPE_INTEGER,
131 .name = "exposure",
132 .minimum = 0x01e5,
133 .maximum = 0x3fff,
134 .step = 0x0001,
135 .default_value = 0x01e5,
136 .flags = 0,
137 },
138 {
139 .id = V4L2_CID_GAIN,
140 .type = V4L2_CTRL_TYPE_INTEGER,
141 .name = "global gain",
142 .minimum = 0x00,
143 .maximum = 0x1f,
144 .step = 0x01,
145 .default_value = 0x0c,
146 .flags = 0,
147 },
148 {
149 .id = V4L2_CID_RED_BALANCE,
150 .type = V4L2_CTRL_TYPE_INTEGER,
151 .name = "red balance",
152 .minimum = 0x00,
153 .maximum = 0x0f,
154 .step = 0x01,
155 .default_value = 0x01,
156 .flags = 0,
157 },
158 {
159 .id = V4L2_CID_BLUE_BALANCE,
160 .type = V4L2_CTRL_TYPE_INTEGER,
161 .name = "blue balance",
162 .minimum = 0x00,
163 .maximum = 0x0f,
164 .step = 0x01,
165 .default_value = 0x05,
166 .flags = 0,
167 },
168 {
169 .id = SN9C102_V4L2_CID_GREEN_BALANCE,
170 .type = V4L2_CTRL_TYPE_INTEGER,
171 .name = "green balance",
172 .minimum = 0x00,
173 .maximum = 0x0f,
174 .step = 0x01,
175 .default_value = 0x00,
176 .flags = 0,
177 },
178 {
179 .id = SN9C102_V4L2_CID_DAC_MAGNITUDE,
180 .type = V4L2_CTRL_TYPE_INTEGER,
181 .name = "DAC magnitude",
182 .minimum = 0x00,
183 .maximum = 0xff,
184 .step = 0x01,
185 .default_value = 0x04,
186 .flags = 0,
187 },
188 },
189 .set_ctrl = &pas202bca_set_ctrl,
190 .cropcap = {
191 .bounds = {
192 .left = 0,
193 .top = 0,
194 .width = 640,
195 .height = 480,
196 },
197 .defrect = {
198 .left = 0,
199 .top = 0,
200 .width = 640,
201 .height = 480,
202 },
203 },
204 .set_crop = &pas202bca_set_crop,
205 .pix_format = {
206 .width = 640,
207 .height = 480,
208 .pixelformat = V4L2_PIX_FMT_SBGGR8,
209 .priv = 8,
210 },
211 .set_pix_format = &pas202bca_set_pix_format
212};
213
214
215int sn9c102_probe_pas202bca(struct sn9c102_device* cam)
216{
217 const struct usb_device_id pas202bca_id_table[] = {
218 { USB_DEVICE(0x0c45, 0x60af), },
219 { }
220 };
221 int err = 0;
222
223 if (!sn9c102_match_id(cam,pas202bca_id_table))
224 return -ENODEV;
225
226 err += sn9c102_write_reg(cam, 0x01, 0x01);
227 err += sn9c102_write_reg(cam, 0x40, 0x01);
228 err += sn9c102_write_reg(cam, 0x28, 0x17);
229 if (err)
230 return -EIO;
231
232 if (sn9c102_i2c_try_write(cam, &pas202bca, 0x10, 0)) /* try to write */
233 return -ENODEV;
234
235 sn9c102_attach_sensor(cam, &pas202bca);
236
237 return 0;
238}
diff --git a/drivers/usb/media/sn9c102_pas202bcb.c b/drivers/usb/media/sn9c102_pas202bcb.c
index 5ca54c7daaf2..d068616ab337 100644
--- a/drivers/usb/media/sn9c102_pas202bcb.c
+++ b/drivers/usb/media/sn9c102_pas202bcb.c
@@ -263,7 +263,7 @@ static struct sn9c102_sensor pas202bcb = {
263 263
264 264
265int sn9c102_probe_pas202bcb(struct sn9c102_device* cam) 265int sn9c102_probe_pas202bcb(struct sn9c102_device* cam)
266{ 266{
267 int r0 = 0, r1 = 0, err = 0; 267 int r0 = 0, r1 = 0, err = 0;
268 unsigned int pid = 0; 268 unsigned int pid = 0;
269 269
diff --git a/drivers/usb/media/sn9c102_sensor.h b/drivers/usb/media/sn9c102_sensor.h
index 7d953b24f2f2..2afd9e9d09bb 100644
--- a/drivers/usb/media/sn9c102_sensor.h
+++ b/drivers/usb/media/sn9c102_sensor.h
@@ -66,6 +66,7 @@ extern int sn9c102_probe_hv7131d(struct sn9c102_device* cam);
66extern int sn9c102_probe_mi0343(struct sn9c102_device* cam); 66extern int sn9c102_probe_mi0343(struct sn9c102_device* cam);
67extern int sn9c102_probe_ov7630(struct sn9c102_device* cam); 67extern int sn9c102_probe_ov7630(struct sn9c102_device* cam);
68extern int sn9c102_probe_pas106b(struct sn9c102_device* cam); 68extern int sn9c102_probe_pas106b(struct sn9c102_device* cam);
69extern int sn9c102_probe_pas202bca(struct sn9c102_device* cam);
69extern int sn9c102_probe_pas202bcb(struct sn9c102_device* cam); 70extern int sn9c102_probe_pas202bcb(struct sn9c102_device* cam);
70extern int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam); 71extern int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam);
71extern int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam); 72extern int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam);
@@ -81,12 +82,17 @@ static int (*sn9c102_sensor_table[])(struct sn9c102_device*) = { \
81 &sn9c102_probe_pas106b, /* strong detection based on SENSOR ids */ \ 82 &sn9c102_probe_pas106b, /* strong detection based on SENSOR ids */ \
82 &sn9c102_probe_pas202bcb, /* strong detection based on SENSOR ids */ \ 83 &sn9c102_probe_pas202bcb, /* strong detection based on SENSOR ids */ \
83 &sn9c102_probe_hv7131d, /* strong detection based on SENSOR ids */ \ 84 &sn9c102_probe_hv7131d, /* strong detection based on SENSOR ids */ \
85 &sn9c102_probe_pas202bca, /* detection mostly based on USB pid/vid */ \
84 &sn9c102_probe_ov7630, /* detection mostly based on USB pid/vid */ \ 86 &sn9c102_probe_ov7630, /* detection mostly based on USB pid/vid */ \
85 &sn9c102_probe_tas5110c1b, /* detection based on USB pid/vid */ \ 87 &sn9c102_probe_tas5110c1b, /* detection based on USB pid/vid */ \
86 &sn9c102_probe_tas5130d1b, /* detection based on USB pid/vid */ \ 88 &sn9c102_probe_tas5130d1b, /* detection based on USB pid/vid */ \
87 NULL, \ 89 NULL, \
88}; 90};
89 91
92/* Device identification */
93extern struct sn9c102_device*
94sn9c102_match_id(struct sn9c102_device* cam, const struct usb_device_id *id);
95
90/* Attach a probed sensor to the camera. */ 96/* Attach a probed sensor to the camera. */
91extern void 97extern void
92sn9c102_attach_sensor(struct sn9c102_device* cam, 98sn9c102_attach_sensor(struct sn9c102_device* cam,
@@ -108,6 +114,7 @@ sn9c102_attach_sensor(struct sn9c102_device* cam,
108static const struct usb_device_id sn9c102_id_table[] = { \ 114static const struct usb_device_id sn9c102_id_table[] = { \
109 { USB_DEVICE(0x0c45, 0x6001), }, /* TAS5110C1B */ \ 115 { USB_DEVICE(0x0c45, 0x6001), }, /* TAS5110C1B */ \
110 { USB_DEVICE(0x0c45, 0x6005), }, /* TAS5110C1B */ \ 116 { USB_DEVICE(0x0c45, 0x6005), }, /* TAS5110C1B */ \
117 { USB_DEVICE(0x0c45, 0x6007), }, \
111 { USB_DEVICE(0x0c45, 0x6009), }, /* PAS106B */ \ 118 { USB_DEVICE(0x0c45, 0x6009), }, /* PAS106B */ \
112 { USB_DEVICE(0x0c45, 0x600d), }, /* PAS106B */ \ 119 { USB_DEVICE(0x0c45, 0x600d), }, /* PAS106B */ \
113 { USB_DEVICE(0x0c45, 0x6024), }, \ 120 { USB_DEVICE(0x0c45, 0x6024), }, \
@@ -126,7 +133,7 @@ static const struct usb_device_id sn9c102_id_table[] = { \
126 { SN9C102_USB_DEVICE(0x0c45, 0x6088, 0xff), }, \ 133 { SN9C102_USB_DEVICE(0x0c45, 0x6088, 0xff), }, \
127 { SN9C102_USB_DEVICE(0x0c45, 0x608a, 0xff), }, \ 134 { SN9C102_USB_DEVICE(0x0c45, 0x608a, 0xff), }, \
128 { SN9C102_USB_DEVICE(0x0c45, 0x608b, 0xff), }, \ 135 { SN9C102_USB_DEVICE(0x0c45, 0x608b, 0xff), }, \
129 { SN9C102_USB_DEVICE(0x0c45, 0x608c, 0xff), }, /* HV7131x */ \ 136 { SN9C102_USB_DEVICE(0x0c45, 0x608c, 0xff), }, /* HV7131/R */ \
130 { SN9C102_USB_DEVICE(0x0c45, 0x608e, 0xff), }, /* CIS-VF10 */ \ 137 { SN9C102_USB_DEVICE(0x0c45, 0x608e, 0xff), }, /* CIS-VF10 */ \
131 { SN9C102_USB_DEVICE(0x0c45, 0x608f, 0xff), }, /* OV7630 */ \ 138 { SN9C102_USB_DEVICE(0x0c45, 0x608f, 0xff), }, /* OV7630 */ \
132 { SN9C102_USB_DEVICE(0x0c45, 0x60a0, 0xff), }, \ 139 { SN9C102_USB_DEVICE(0x0c45, 0x60a0, 0xff), }, \
@@ -359,12 +366,6 @@ struct sn9c102_sensor {
359 error code without rolling back. 366 error code without rolling back.
360 */ 367 */
361 368
362 const struct usb_device* usbdev;
363 /*
364 Points to the usb_device struct after the sensor is attached.
365 Do not touch unless you know what you are doing.
366 */
367
368 /* 369 /*
369 Do NOT write to the data below, it's READ ONLY. It is used by the 370 Do NOT write to the data below, it's READ ONLY. It is used by the
370 core module to store successfully updated values of the above 371 core module to store successfully updated values of the above
diff --git a/drivers/usb/media/sn9c102_tas5110c1b.c b/drivers/usb/media/sn9c102_tas5110c1b.c
index 32ddf236cafe..2e08c552f40a 100644
--- a/drivers/usb/media/sn9c102_tas5110c1b.c
+++ b/drivers/usb/media/sn9c102_tas5110c1b.c
@@ -142,14 +142,18 @@ static struct sn9c102_sensor tas5110c1b = {
142 142
143int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam) 143int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam)
144{ 144{
145 /* This sensor has no identifiers, so let's attach it anyway */ 145 const struct usb_device_id tas5110c1b_id_table[] = {
146 sn9c102_attach_sensor(cam, &tas5110c1b); 146 { USB_DEVICE(0x0c45, 0x6001), },
147 { USB_DEVICE(0x0c45, 0x6005), },
148 { USB_DEVICE(0x0c45, 0x60ab), },
149 { }
150 };
147 151
148 /* Sensor detection is based on USB pid/vid */ 152 /* Sensor detection is based on USB pid/vid */
149 if (le16_to_cpu(tas5110c1b.usbdev->descriptor.idProduct) != 0x6001 && 153 if (!sn9c102_match_id(cam, tas5110c1b_id_table))
150 le16_to_cpu(tas5110c1b.usbdev->descriptor.idProduct) != 0x6005 &&
151 le16_to_cpu(tas5110c1b.usbdev->descriptor.idProduct) != 0x60ab)
152 return -ENODEV; 154 return -ENODEV;
153 155
156 sn9c102_attach_sensor(cam, &tas5110c1b);
157
154 return 0; 158 return 0;
155} 159}
diff --git a/drivers/usb/media/sn9c102_tas5130d1b.c b/drivers/usb/media/sn9c102_tas5130d1b.c
index a0728f0ae00c..c7b339740bbf 100644
--- a/drivers/usb/media/sn9c102_tas5130d1b.c
+++ b/drivers/usb/media/sn9c102_tas5130d1b.c
@@ -153,13 +153,17 @@ static struct sn9c102_sensor tas5130d1b = {
153 153
154int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam) 154int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam)
155{ 155{
156 /* This sensor has no identifiers, so let's attach it anyway */ 156 const struct usb_device_id tas5130d1b_id_table[] = {
157 sn9c102_attach_sensor(cam, &tas5130d1b); 157 { USB_DEVICE(0x0c45, 0x6025), },
158 { USB_DEVICE(0x0c45, 0x60aa), },
159 { }
160 };
158 161
159 /* Sensor detection is based on USB pid/vid */ 162 /* Sensor detection is based on USB pid/vid */
160 if (le16_to_cpu(tas5130d1b.usbdev->descriptor.idProduct) != 0x6025 && 163 if (!sn9c102_match_id(cam, tas5130d1b_id_table))
161 le16_to_cpu(tas5130d1b.usbdev->descriptor.idProduct) != 0x60aa)
162 return -ENODEV; 164 return -ENODEV;
163 165
166 sn9c102_attach_sensor(cam, &tas5130d1b);
167
164 return 0; 168 return 0;
165} 169}
diff --git a/drivers/usb/media/stv680.c b/drivers/usb/media/stv680.c
index b497a6a0a206..9636da20748d 100644
--- a/drivers/usb/media/stv680.c
+++ b/drivers/usb/media/stv680.c
@@ -67,6 +67,7 @@
67#include <linux/errno.h> 67#include <linux/errno.h>
68#include <linux/videodev.h> 68#include <linux/videodev.h>
69#include <linux/usb.h> 69#include <linux/usb.h>
70#include <linux/mutex.h>
70 71
71#include "stv680.h" 72#include "stv680.h"
72 73
@@ -317,12 +318,11 @@ static int stv_init (struct usb_stv *stv680)
317 unsigned char *buffer; 318 unsigned char *buffer;
318 unsigned long int bufsize; 319 unsigned long int bufsize;
319 320
320 buffer = kmalloc (40, GFP_KERNEL); 321 buffer = kzalloc (40, GFP_KERNEL);
321 if (buffer == NULL) { 322 if (buffer == NULL) {
322 PDEBUG (0, "STV(e): Out of (small buf) memory"); 323 PDEBUG (0, "STV(e): Out of (small buf) memory");
323 return -1; 324 return -1;
324 } 325 }
325 memset (buffer, 0, 40);
326 udelay (100); 326 udelay (100);
327 327
328 /* set config 1, interface 0, alternate 0 */ 328 /* set config 1, interface 0, alternate 0 */
@@ -1258,22 +1258,22 @@ static int stv680_mmap (struct file *file, struct vm_area_struct *vma)
1258 unsigned long size = vma->vm_end-vma->vm_start; 1258 unsigned long size = vma->vm_end-vma->vm_start;
1259 unsigned long page, pos; 1259 unsigned long page, pos;
1260 1260
1261 down (&stv680->lock); 1261 mutex_lock(&stv680->lock);
1262 1262
1263 if (stv680->udev == NULL) { 1263 if (stv680->udev == NULL) {
1264 up (&stv680->lock); 1264 mutex_unlock(&stv680->lock);
1265 return -EIO; 1265 return -EIO;
1266 } 1266 }
1267 if (size > (((STV680_NUMFRAMES * stv680->maxframesize) + PAGE_SIZE - 1) 1267 if (size > (((STV680_NUMFRAMES * stv680->maxframesize) + PAGE_SIZE - 1)
1268 & ~(PAGE_SIZE - 1))) { 1268 & ~(PAGE_SIZE - 1))) {
1269 up (&stv680->lock); 1269 mutex_unlock(&stv680->lock);
1270 return -EINVAL; 1270 return -EINVAL;
1271 } 1271 }
1272 pos = (unsigned long) stv680->fbuf; 1272 pos = (unsigned long) stv680->fbuf;
1273 while (size > 0) { 1273 while (size > 0) {
1274 page = vmalloc_to_pfn((void *)pos); 1274 page = vmalloc_to_pfn((void *)pos);
1275 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) { 1275 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
1276 up (&stv680->lock); 1276 mutex_unlock(&stv680->lock);
1277 return -EAGAIN; 1277 return -EAGAIN;
1278 } 1278 }
1279 start += PAGE_SIZE; 1279 start += PAGE_SIZE;
@@ -1283,7 +1283,7 @@ static int stv680_mmap (struct file *file, struct vm_area_struct *vma)
1283 else 1283 else
1284 size = 0; 1284 size = 0;
1285 } 1285 }
1286 up (&stv680->lock); 1286 mutex_unlock(&stv680->lock);
1287 1287
1288 return 0; 1288 return 0;
1289} 1289}
@@ -1387,14 +1387,12 @@ static int stv680_probe (struct usb_interface *intf, const struct usb_device_id
1387 goto error; 1387 goto error;
1388 } 1388 }
1389 /* We found one */ 1389 /* We found one */
1390 if ((stv680 = kmalloc (sizeof (*stv680), GFP_KERNEL)) == NULL) { 1390 if ((stv680 = kzalloc (sizeof (*stv680), GFP_KERNEL)) == NULL) {
1391 PDEBUG (0, "STV(e): couldn't kmalloc stv680 struct."); 1391 PDEBUG (0, "STV(e): couldn't kmalloc stv680 struct.");
1392 retval = -ENOMEM; 1392 retval = -ENOMEM;
1393 goto error; 1393 goto error;
1394 } 1394 }
1395 1395
1396 memset (stv680, 0, sizeof (*stv680));
1397
1398 stv680->udev = dev; 1396 stv680->udev = dev;
1399 stv680->camera_name = camera_name; 1397 stv680->camera_name = camera_name;
1400 1398
@@ -1409,7 +1407,7 @@ static int stv680_probe (struct usb_interface *intf, const struct usb_device_id
1409 1407
1410 memcpy (stv680->vdev->name, stv680->camera_name, strlen (stv680->camera_name)); 1408 memcpy (stv680->vdev->name, stv680->camera_name, strlen (stv680->camera_name));
1411 init_waitqueue_head (&stv680->wq); 1409 init_waitqueue_head (&stv680->wq);
1412 init_MUTEX (&stv680->lock); 1410 mutex_init (&stv680->lock);
1413 wmb (); 1411 wmb ();
1414 1412
1415 if (video_register_device (stv680->vdev, VFL_TYPE_GRABBER, video_nr) == -1) { 1413 if (video_register_device (stv680->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
diff --git a/drivers/usb/media/stv680.h b/drivers/usb/media/stv680.h
index b0551cdb280b..ea46e0001e6d 100644
--- a/drivers/usb/media/stv680.h
+++ b/drivers/usb/media/stv680.h
@@ -118,7 +118,7 @@ struct usb_stv {
118 int origGain; 118 int origGain;
119 int origMode; /* original camera mode */ 119 int origMode; /* original camera mode */
120 120
121 struct semaphore lock; /* to lock the structure */ 121 struct mutex lock; /* to lock the structure */
122 int user; /* user count for exclusive use */ 122 int user; /* user count for exclusive use */
123 int removed; /* device disconnected */ 123 int removed; /* device disconnected */
124 int streaming; /* Are we streaming video? */ 124 int streaming; /* Are we streaming video? */
diff --git a/drivers/usb/media/usbvideo.c b/drivers/usb/media/usbvideo.c
index 63a72e550a1b..0b51fae720a9 100644
--- a/drivers/usb/media/usbvideo.c
+++ b/drivers/usb/media/usbvideo.c
@@ -690,14 +690,13 @@ int usbvideo_register(
690 } 690 }
691 691
692 base_size = num_cams * sizeof(struct uvd) + sizeof(struct usbvideo); 692 base_size = num_cams * sizeof(struct uvd) + sizeof(struct usbvideo);
693 cams = (struct usbvideo *) kmalloc(base_size, GFP_KERNEL); 693 cams = (struct usbvideo *) kzalloc(base_size, GFP_KERNEL);
694 if (cams == NULL) { 694 if (cams == NULL) {
695 err("Failed to allocate %d. bytes for usbvideo struct", base_size); 695 err("Failed to allocate %d. bytes for usbvideo struct", base_size);
696 return -ENOMEM; 696 return -ENOMEM;
697 } 697 }
698 dbg("%s: Allocated $%p (%d. bytes) for %d. cameras", 698 dbg("%s: Allocated $%p (%d. bytes) for %d. cameras",
699 __FUNCTION__, cams, base_size, num_cams); 699 __FUNCTION__, cams, base_size, num_cams);
700 memset(cams, 0, base_size);
701 700
702 /* Copy callbacks, apply defaults for those that are not set */ 701 /* Copy callbacks, apply defaults for those that are not set */
703 memmove(&cams->cb, cbTbl, sizeof(cams->cb)); 702 memmove(&cams->cb, cbTbl, sizeof(cams->cb));
@@ -715,7 +714,7 @@ int usbvideo_register(
715 cams->md_module = md; 714 cams->md_module = md;
716 if (cams->md_module == NULL) 715 if (cams->md_module == NULL)
717 warn("%s: module == NULL!", __FUNCTION__); 716 warn("%s: module == NULL!", __FUNCTION__);
718 init_MUTEX(&cams->lock); /* to 1 == available */ 717 mutex_init(&cams->lock); /* to 1 == available */
719 718
720 for (i = 0; i < num_cams; i++) { 719 for (i = 0; i < num_cams; i++) {
721 struct uvd *up = &cams->cam[i]; 720 struct uvd *up = &cams->cam[i];
@@ -863,7 +862,7 @@ static void usbvideo_Disconnect(struct usb_interface *intf)
863 if (uvd->debug > 0) 862 if (uvd->debug > 0)
864 info("%s(%p.)", __FUNCTION__, intf); 863 info("%s(%p.)", __FUNCTION__, intf);
865 864
866 down(&uvd->lock); 865 mutex_lock(&uvd->lock);
867 uvd->remove_pending = 1; /* Now all ISO data will be ignored */ 866 uvd->remove_pending = 1; /* Now all ISO data will be ignored */
868 867
869 /* At this time we ask to cancel outstanding URBs */ 868 /* At this time we ask to cancel outstanding URBs */
@@ -883,7 +882,7 @@ static void usbvideo_Disconnect(struct usb_interface *intf)
883 info("%s: In use, disconnect pending.", __FUNCTION__); 882 info("%s: In use, disconnect pending.", __FUNCTION__);
884 else 883 else
885 usbvideo_CameraRelease(uvd); 884 usbvideo_CameraRelease(uvd);
886 up(&uvd->lock); 885 mutex_unlock(&uvd->lock);
887 info("USB camera disconnected."); 886 info("USB camera disconnected.");
888 887
889 usbvideo_ClientDecModCount(uvd); 888 usbvideo_ClientDecModCount(uvd);
@@ -930,19 +929,19 @@ static int usbvideo_find_struct(struct usbvideo *cams)
930 err("No usbvideo handle?"); 929 err("No usbvideo handle?");
931 return -1; 930 return -1;
932 } 931 }
933 down(&cams->lock); 932 mutex_lock(&cams->lock);
934 for (u = 0; u < cams->num_cameras; u++) { 933 for (u = 0; u < cams->num_cameras; u++) {
935 struct uvd *uvd = &cams->cam[u]; 934 struct uvd *uvd = &cams->cam[u];
936 if (!uvd->uvd_used) /* This one is free */ 935 if (!uvd->uvd_used) /* This one is free */
937 { 936 {
938 uvd->uvd_used = 1; /* In use now */ 937 uvd->uvd_used = 1; /* In use now */
939 init_MUTEX(&uvd->lock); /* to 1 == available */ 938 mutex_init(&uvd->lock); /* to 1 == available */
940 uvd->dev = NULL; 939 uvd->dev = NULL;
941 rv = u; 940 rv = u;
942 break; 941 break;
943 } 942 }
944 } 943 }
945 up(&cams->lock); 944 mutex_unlock(&cams->lock);
946 return rv; 945 return rv;
947} 946}
948 947
@@ -984,7 +983,7 @@ struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams)
984 /* Not relying upon caller we increase module counter ourselves */ 983 /* Not relying upon caller we increase module counter ourselves */
985 usbvideo_ClientIncModCount(uvd); 984 usbvideo_ClientIncModCount(uvd);
986 985
987 down(&uvd->lock); 986 mutex_lock(&uvd->lock);
988 for (i=0; i < USBVIDEO_NUMSBUF; i++) { 987 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
989 uvd->sbuf[i].urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL); 988 uvd->sbuf[i].urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
990 if (uvd->sbuf[i].urb == NULL) { 989 if (uvd->sbuf[i].urb == NULL) {
@@ -1007,7 +1006,7 @@ struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams)
1007 * return control to the client's probe function right now. 1006 * return control to the client's probe function right now.
1008 */ 1007 */
1009allocate_done: 1008allocate_done:
1010 up (&uvd->lock); 1009 mutex_unlock(&uvd->lock);
1011 usbvideo_ClientDecModCount(uvd); 1010 usbvideo_ClientDecModCount(uvd);
1012 return uvd; 1011 return uvd;
1013} 1012}
@@ -1121,7 +1120,7 @@ static int usbvideo_v4l_open(struct inode *inode, struct file *file)
1121 info("%s($%p)", __FUNCTION__, dev); 1120 info("%s($%p)", __FUNCTION__, dev);
1122 1121
1123 usbvideo_ClientIncModCount(uvd); 1122 usbvideo_ClientIncModCount(uvd);
1124 down(&uvd->lock); 1123 mutex_lock(&uvd->lock);
1125 1124
1126 if (uvd->user) { 1125 if (uvd->user) {
1127 err("%s: Someone tried to open an already opened device!", __FUNCTION__); 1126 err("%s: Someone tried to open an already opened device!", __FUNCTION__);
@@ -1202,7 +1201,7 @@ static int usbvideo_v4l_open(struct inode *inode, struct file *file)
1202 } 1201 }
1203 } 1202 }
1204 } 1203 }
1205 up(&uvd->lock); 1204 mutex_unlock(&uvd->lock);
1206 if (errCode != 0) 1205 if (errCode != 0)
1207 usbvideo_ClientDecModCount(uvd); 1206 usbvideo_ClientDecModCount(uvd);
1208 if (uvd->debug > 0) 1207 if (uvd->debug > 0)
@@ -1231,7 +1230,7 @@ static int usbvideo_v4l_close(struct inode *inode, struct file *file)
1231 if (uvd->debug > 1) 1230 if (uvd->debug > 1)
1232 info("%s($%p)", __FUNCTION__, dev); 1231 info("%s($%p)", __FUNCTION__, dev);
1233 1232
1234 down(&uvd->lock); 1233 mutex_lock(&uvd->lock);
1235 GET_CALLBACK(uvd, stopDataPump)(uvd); 1234 GET_CALLBACK(uvd, stopDataPump)(uvd);
1236 usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size); 1235 usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size);
1237 uvd->fbuf = NULL; 1236 uvd->fbuf = NULL;
@@ -1252,7 +1251,7 @@ static int usbvideo_v4l_close(struct inode *inode, struct file *file)
1252 info("usbvideo_v4l_close: Final disconnect."); 1251 info("usbvideo_v4l_close: Final disconnect.");
1253 usbvideo_CameraRelease(uvd); 1252 usbvideo_CameraRelease(uvd);
1254 } 1253 }
1255 up(&uvd->lock); 1254 mutex_unlock(&uvd->lock);
1256 usbvideo_ClientDecModCount(uvd); 1255 usbvideo_ClientDecModCount(uvd);
1257 1256
1258 if (uvd->debug > 1) 1257 if (uvd->debug > 1)
@@ -1512,7 +1511,7 @@ static ssize_t usbvideo_v4l_read(struct file *file, char __user *buf,
1512 if (uvd->debug >= 1) 1511 if (uvd->debug >= 1)
1513 info("%s: %Zd. bytes, noblock=%d.", __FUNCTION__, count, noblock); 1512 info("%s: %Zd. bytes, noblock=%d.", __FUNCTION__, count, noblock);
1514 1513
1515 down(&uvd->lock); 1514 mutex_lock(&uvd->lock);
1516 1515
1517 /* See if a frame is completed, then use it. */ 1516 /* See if a frame is completed, then use it. */
1518 for(i = 0; i < USBVIDEO_NUMFRAMES; i++) { 1517 for(i = 0; i < USBVIDEO_NUMFRAMES; i++) {
@@ -1644,7 +1643,7 @@ static ssize_t usbvideo_v4l_read(struct file *file, char __user *buf,
1644 } 1643 }
1645 } 1644 }
1646read_done: 1645read_done:
1647 up(&uvd->lock); 1646 mutex_unlock(&uvd->lock);
1648 return count; 1647 return count;
1649} 1648}
1650 1649
diff --git a/drivers/usb/media/usbvideo.h b/drivers/usb/media/usbvideo.h
index 6c390a1f981b..135433c2680a 100644
--- a/drivers/usb/media/usbvideo.h
+++ b/drivers/usb/media/usbvideo.h
@@ -19,6 +19,7 @@
19#include <linux/config.h> 19#include <linux/config.h>
20#include <linux/videodev.h> 20#include <linux/videodev.h>
21#include <linux/usb.h> 21#include <linux/usb.h>
22#include <linux/mutex.h>
22 23
23/* Most helpful debugging aid */ 24/* Most helpful debugging aid */
24#define assert(expr) ((void) ((expr) ? 0 : (err("assert failed at line %d",__LINE__)))) 25#define assert(expr) ((void) ((expr) ? 0 : (err("assert failed at line %d",__LINE__))))
@@ -213,7 +214,7 @@ struct uvd {
213 unsigned long flags; /* FLAGS_USBVIDEO_xxx */ 214 unsigned long flags; /* FLAGS_USBVIDEO_xxx */
214 unsigned long paletteBits; /* Which palettes we accept? */ 215 unsigned long paletteBits; /* Which palettes we accept? */
215 unsigned short defaultPalette; /* What palette to use for read() */ 216 unsigned short defaultPalette; /* What palette to use for read() */
216 struct semaphore lock; 217 struct mutex lock;
217 int user; /* user count for exclusive use */ 218 int user; /* user count for exclusive use */
218 219
219 videosize_t videosize; /* Current setting */ 220 videosize_t videosize; /* Current setting */
@@ -272,7 +273,7 @@ struct usbvideo {
272 int num_cameras; /* As allocated */ 273 int num_cameras; /* As allocated */
273 struct usb_driver usbdrv; /* Interface to the USB stack */ 274 struct usb_driver usbdrv; /* Interface to the USB stack */
274 char drvName[80]; /* Driver name */ 275 char drvName[80]; /* Driver name */
275 struct semaphore lock; /* Mutex protecting camera structures */ 276 struct mutex lock; /* Mutex protecting camera structures */
276 struct usbvideo_cb cb; /* Table of callbacks (virtual methods) */ 277 struct usbvideo_cb cb; /* Table of callbacks (virtual methods) */
277 struct video_device vdt; /* Video device template */ 278 struct video_device vdt; /* Video device template */
278 struct uvd *cam; /* Array of camera structures */ 279 struct uvd *cam; /* Array of camera structures */
diff --git a/drivers/usb/media/vicam.c b/drivers/usb/media/vicam.c
index 5df144073871..1d06e53ec7c5 100644
--- a/drivers/usb/media/vicam.c
+++ b/drivers/usb/media/vicam.c
@@ -42,6 +42,7 @@
42#include <linux/vmalloc.h> 42#include <linux/vmalloc.h>
43#include <linux/slab.h> 43#include <linux/slab.h>
44#include <linux/proc_fs.h> 44#include <linux/proc_fs.h>
45#include <linux/mutex.h>
45#include "usbvideo.h" 46#include "usbvideo.h"
46 47
47// #define VICAM_DEBUG 48// #define VICAM_DEBUG
@@ -407,7 +408,7 @@ struct vicam_camera {
407 struct usb_device *udev; // usb device 408 struct usb_device *udev; // usb device
408 409
409 /* guard against simultaneous accesses to the camera */ 410 /* guard against simultaneous accesses to the camera */
410 struct semaphore cam_lock; 411 struct mutex cam_lock;
411 412
412 int is_initialized; 413 int is_initialized;
413 u8 open_count; 414 u8 open_count;
@@ -461,12 +462,12 @@ static int send_control_msg(struct vicam_camera *cam,
461 u16 size) 462 u16 size)
462{ 463{
463 int status = -ENODEV; 464 int status = -ENODEV;
464 down(&cam->cam_lock); 465 mutex_lock(&cam->cam_lock);
465 if (cam->udev) { 466 if (cam->udev) {
466 status = __send_control_msg(cam, request, value, 467 status = __send_control_msg(cam, request, value,
467 index, cp, size); 468 index, cp, size);
468 } 469 }
469 up(&cam->cam_lock); 470 mutex_unlock(&cam->cam_lock);
470 return status; 471 return status;
471} 472}
472static int 473static int
@@ -763,6 +764,7 @@ vicam_open(struct inode *inode, struct file *file)
763 if (!cam) { 764 if (!cam) {
764 printk(KERN_ERR 765 printk(KERN_ERR
765 "vicam video_device improperly initialized"); 766 "vicam video_device improperly initialized");
767 return -EINVAL;
766 } 768 }
767 769
768 /* the videodev_lock held above us protects us from 770 /* the videodev_lock held above us protects us from
@@ -831,13 +833,13 @@ vicam_close(struct inode *inode, struct file *file)
831 rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES); 833 rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
832 kfree(cam->cntrlbuf); 834 kfree(cam->cntrlbuf);
833 835
834 down(&cam->cam_lock); 836 mutex_lock(&cam->cam_lock);
835 837
836 cam->open_count--; 838 cam->open_count--;
837 open_count = cam->open_count; 839 open_count = cam->open_count;
838 udev = cam->udev; 840 udev = cam->udev;
839 841
840 up(&cam->cam_lock); 842 mutex_unlock(&cam->cam_lock);
841 843
842 if (!open_count && !udev) { 844 if (!open_count && !udev) {
843 kfree(cam); 845 kfree(cam);
@@ -960,7 +962,7 @@ read_frame(struct vicam_camera *cam, int framenum)
960 request[8] = 0; 962 request[8] = 0;
961 // bytes 9-15 do not seem to affect exposure or image quality 963 // bytes 9-15 do not seem to affect exposure or image quality
962 964
963 down(&cam->cam_lock); 965 mutex_lock(&cam->cam_lock);
964 966
965 if (!cam->udev) { 967 if (!cam->udev) {
966 goto done; 968 goto done;
@@ -985,7 +987,7 @@ read_frame(struct vicam_camera *cam, int framenum)
985 } 987 }
986 988
987 done: 989 done:
988 up(&cam->cam_lock); 990 mutex_unlock(&cam->cam_lock);
989} 991}
990 992
991static ssize_t 993static ssize_t
@@ -1309,7 +1311,7 @@ vicam_probe( struct usb_interface *intf, const struct usb_device_id *id)
1309 1311
1310 cam->shutter_speed = 15; 1312 cam->shutter_speed = 15;
1311 1313
1312 init_MUTEX(&cam->cam_lock); 1314 mutex_init(&cam->cam_lock);
1313 1315
1314 memcpy(&cam->vdev, &vicam_template, 1316 memcpy(&cam->vdev, &vicam_template,
1315 sizeof (vicam_template)); 1317 sizeof (vicam_template));
@@ -1351,7 +1353,7 @@ vicam_disconnect(struct usb_interface *intf)
1351 1353
1352 /* stop the camera from being used */ 1354 /* stop the camera from being used */
1353 1355
1354 down(&cam->cam_lock); 1356 mutex_lock(&cam->cam_lock);
1355 1357
1356 /* mark the camera as gone */ 1358 /* mark the camera as gone */
1357 1359
@@ -1368,7 +1370,7 @@ vicam_disconnect(struct usb_interface *intf)
1368 1370
1369 open_count = cam->open_count; 1371 open_count = cam->open_count;
1370 1372
1371 up(&cam->cam_lock); 1373 mutex_unlock(&cam->cam_lock);
1372 1374
1373 if (!open_count) { 1375 if (!open_count) {
1374 kfree(cam); 1376 kfree(cam);
diff --git a/drivers/usb/media/w9968cf.c b/drivers/usb/media/w9968cf.c
index 9937fc64c8bf..b57dec3782e0 100644
--- a/drivers/usb/media/w9968cf.c
+++ b/drivers/usb/media/w9968cf.c
@@ -47,6 +47,13 @@
47#include "w9968cf.h" 47#include "w9968cf.h"
48#include "w9968cf_decoder.h" 48#include "w9968cf_decoder.h"
49 49
50static struct w9968cf_vpp_t* w9968cf_vpp;
51static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
52
53static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
54static DEFINE_MUTEX(w9968cf_devlist_mutex); /* semaphore for list traversal */
55
56static DECLARE_RWSEM(w9968cf_disconnect); /* prevent races with open() */
50 57
51 58
52/**************************************************************************** 59/****************************************************************************
@@ -695,13 +702,12 @@ static int w9968cf_allocate_memory(struct w9968cf_device* cam)
695 /* Allocate memory for the isochronous transfer buffers */ 702 /* Allocate memory for the isochronous transfer buffers */
696 for (i = 0; i < W9968CF_URBS; i++) { 703 for (i = 0; i < W9968CF_URBS; i++) {
697 if (!(cam->transfer_buffer[i] = 704 if (!(cam->transfer_buffer[i] =
698 kmalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) { 705 kzalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
699 DBG(1, "Couldn't allocate memory for the isochronous " 706 DBG(1, "Couldn't allocate memory for the isochronous "
700 "transfer buffers (%u bytes)", 707 "transfer buffers (%u bytes)",
701 p_size * W9968CF_ISO_PACKETS) 708 p_size * W9968CF_ISO_PACKETS)
702 return -ENOMEM; 709 return -ENOMEM;
703 } 710 }
704 memset(cam->transfer_buffer[i], 0, W9968CF_ISO_PACKETS*p_size);
705 } 711 }
706 712
707 /* Allocate memory for the temporary frame buffer */ 713 /* Allocate memory for the temporary frame buffer */
@@ -2419,7 +2425,7 @@ w9968cf_configure_camera(struct w9968cf_device* cam,
2419 enum w9968cf_model_id mod_id, 2425 enum w9968cf_model_id mod_id,
2420 const unsigned short dev_nr) 2426 const unsigned short dev_nr)
2421{ 2427{
2422 init_MUTEX(&cam->fileop_sem); 2428 mutex_init(&cam->fileop_mutex);
2423 init_waitqueue_head(&cam->open); 2429 init_waitqueue_head(&cam->open);
2424 spin_lock_init(&cam->urb_lock); 2430 spin_lock_init(&cam->urb_lock);
2425 spin_lock_init(&cam->flist_lock); 2431 spin_lock_init(&cam->flist_lock);
@@ -2647,7 +2653,7 @@ static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
2647 --------------------------------------------------------------------------*/ 2653 --------------------------------------------------------------------------*/
2648static void w9968cf_release_resources(struct w9968cf_device* cam) 2654static void w9968cf_release_resources(struct w9968cf_device* cam)
2649{ 2655{
2650 down(&w9968cf_devlist_sem); 2656 mutex_lock(&w9968cf_devlist_mutex);
2651 2657
2652 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->minor) 2658 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->minor)
2653 2659
@@ -2658,7 +2664,7 @@ static void w9968cf_release_resources(struct w9968cf_device* cam)
2658 kfree(cam->control_buffer); 2664 kfree(cam->control_buffer);
2659 kfree(cam->data_buffer); 2665 kfree(cam->data_buffer);
2660 2666
2661 up(&w9968cf_devlist_sem); 2667 mutex_unlock(&w9968cf_devlist_mutex);
2662} 2668}
2663 2669
2664 2670
@@ -2678,14 +2684,14 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2678 2684
2679 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp)); 2685 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2680 2686
2681 down(&cam->dev_sem); 2687 mutex_lock(&cam->dev_mutex);
2682 2688
2683 if (cam->sensor == CC_UNKNOWN) { 2689 if (cam->sensor == CC_UNKNOWN) {
2684 DBG(2, "No supported image sensor has been detected by the " 2690 DBG(2, "No supported image sensor has been detected by the "
2685 "'ovcamchip' module for the %s (/dev/video%d). Make " 2691 "'ovcamchip' module for the %s (/dev/video%d). Make "
2686 "sure it is loaded *before* (re)connecting the camera.", 2692 "sure it is loaded *before* (re)connecting the camera.",
2687 symbolic(camlist, cam->id), cam->v4ldev->minor) 2693 symbolic(camlist, cam->id), cam->v4ldev->minor)
2688 up(&cam->dev_sem); 2694 mutex_unlock(&cam->dev_mutex);
2689 up_read(&w9968cf_disconnect); 2695 up_read(&w9968cf_disconnect);
2690 return -ENODEV; 2696 return -ENODEV;
2691 } 2697 }
@@ -2694,11 +2700,11 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2694 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'", 2700 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'",
2695 symbolic(camlist, cam->id),cam->v4ldev->minor,cam->command) 2701 symbolic(camlist, cam->id),cam->v4ldev->minor,cam->command)
2696 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) { 2702 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
2697 up(&cam->dev_sem); 2703 mutex_unlock(&cam->dev_mutex);
2698 up_read(&w9968cf_disconnect); 2704 up_read(&w9968cf_disconnect);
2699 return -EWOULDBLOCK; 2705 return -EWOULDBLOCK;
2700 } 2706 }
2701 up(&cam->dev_sem); 2707 mutex_unlock(&cam->dev_mutex);
2702 err = wait_event_interruptible_exclusive(cam->open, 2708 err = wait_event_interruptible_exclusive(cam->open,
2703 cam->disconnected || 2709 cam->disconnected ||
2704 !cam->users); 2710 !cam->users);
@@ -2710,7 +2716,7 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2710 up_read(&w9968cf_disconnect); 2716 up_read(&w9968cf_disconnect);
2711 return -ENODEV; 2717 return -ENODEV;
2712 } 2718 }
2713 down(&cam->dev_sem); 2719 mutex_lock(&cam->dev_mutex);
2714 } 2720 }
2715 2721
2716 DBG(5, "Opening '%s', /dev/video%d ...", 2722 DBG(5, "Opening '%s', /dev/video%d ...",
@@ -2739,7 +2745,7 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2739 2745
2740 DBG(5, "Video device is open") 2746 DBG(5, "Video device is open")
2741 2747
2742 up(&cam->dev_sem); 2748 mutex_unlock(&cam->dev_mutex);
2743 up_read(&w9968cf_disconnect); 2749 up_read(&w9968cf_disconnect);
2744 2750
2745 return 0; 2751 return 0;
@@ -2747,7 +2753,7 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2747deallocate_memory: 2753deallocate_memory:
2748 w9968cf_deallocate_memory(cam); 2754 w9968cf_deallocate_memory(cam);
2749 DBG(2, "Failed to open the video device") 2755 DBG(2, "Failed to open the video device")
2750 up(&cam->dev_sem); 2756 mutex_unlock(&cam->dev_mutex);
2751 up_read(&w9968cf_disconnect); 2757 up_read(&w9968cf_disconnect);
2752 return err; 2758 return err;
2753} 2759}
@@ -2759,13 +2765,13 @@ static int w9968cf_release(struct inode* inode, struct file* filp)
2759 2765
2760 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp)); 2766 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2761 2767
2762 down(&cam->dev_sem); /* prevent disconnect() to be called */ 2768 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
2763 2769
2764 w9968cf_stop_transfer(cam); 2770 w9968cf_stop_transfer(cam);
2765 2771
2766 if (cam->disconnected) { 2772 if (cam->disconnected) {
2767 w9968cf_release_resources(cam); 2773 w9968cf_release_resources(cam);
2768 up(&cam->dev_sem); 2774 mutex_unlock(&cam->dev_mutex);
2769 kfree(cam); 2775 kfree(cam);
2770 return 0; 2776 return 0;
2771 } 2777 }
@@ -2775,7 +2781,7 @@ static int w9968cf_release(struct inode* inode, struct file* filp)
2775 wake_up_interruptible_nr(&cam->open, 1); 2781 wake_up_interruptible_nr(&cam->open, 1);
2776 2782
2777 DBG(5, "Video device closed") 2783 DBG(5, "Video device closed")
2778 up(&cam->dev_sem); 2784 mutex_unlock(&cam->dev_mutex);
2779 return 0; 2785 return 0;
2780} 2786}
2781 2787
@@ -2792,18 +2798,18 @@ w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2792 if (filp->f_flags & O_NONBLOCK) 2798 if (filp->f_flags & O_NONBLOCK)
2793 return -EWOULDBLOCK; 2799 return -EWOULDBLOCK;
2794 2800
2795 if (down_interruptible(&cam->fileop_sem)) 2801 if (mutex_lock_interruptible(&cam->fileop_mutex))
2796 return -ERESTARTSYS; 2802 return -ERESTARTSYS;
2797 2803
2798 if (cam->disconnected) { 2804 if (cam->disconnected) {
2799 DBG(2, "Device not present") 2805 DBG(2, "Device not present")
2800 up(&cam->fileop_sem); 2806 mutex_unlock(&cam->fileop_mutex);
2801 return -ENODEV; 2807 return -ENODEV;
2802 } 2808 }
2803 2809
2804 if (cam->misconfigured) { 2810 if (cam->misconfigured) {
2805 DBG(2, "The camera is misconfigured. Close and open it again.") 2811 DBG(2, "The camera is misconfigured. Close and open it again.")
2806 up(&cam->fileop_sem); 2812 mutex_unlock(&cam->fileop_mutex);
2807 return -EIO; 2813 return -EIO;
2808 } 2814 }
2809 2815
@@ -2818,11 +2824,11 @@ w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2818 cam->frame[1].status == F_READY || 2824 cam->frame[1].status == F_READY ||
2819 cam->disconnected); 2825 cam->disconnected);
2820 if (err) { 2826 if (err) {
2821 up(&cam->fileop_sem); 2827 mutex_unlock(&cam->fileop_mutex);
2822 return err; 2828 return err;
2823 } 2829 }
2824 if (cam->disconnected) { 2830 if (cam->disconnected) {
2825 up(&cam->fileop_sem); 2831 mutex_unlock(&cam->fileop_mutex);
2826 return -ENODEV; 2832 return -ENODEV;
2827 } 2833 }
2828 2834
@@ -2836,7 +2842,7 @@ w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2836 2842
2837 if (copy_to_user(buf, fr->buffer, count)) { 2843 if (copy_to_user(buf, fr->buffer, count)) {
2838 fr->status = F_UNUSED; 2844 fr->status = F_UNUSED;
2839 up(&cam->fileop_sem); 2845 mutex_unlock(&cam->fileop_mutex);
2840 return -EFAULT; 2846 return -EFAULT;
2841 } 2847 }
2842 *f_pos += count; 2848 *f_pos += count;
@@ -2845,7 +2851,7 @@ w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2845 2851
2846 DBG(5, "%zu bytes read", count) 2852 DBG(5, "%zu bytes read", count)
2847 2853
2848 up(&cam->fileop_sem); 2854 mutex_unlock(&cam->fileop_mutex);
2849 return count; 2855 return count;
2850} 2856}
2851 2857
@@ -2899,24 +2905,24 @@ w9968cf_ioctl(struct inode* inode, struct file* filp,
2899 2905
2900 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp)); 2906 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2901 2907
2902 if (down_interruptible(&cam->fileop_sem)) 2908 if (mutex_lock_interruptible(&cam->fileop_mutex))
2903 return -ERESTARTSYS; 2909 return -ERESTARTSYS;
2904 2910
2905 if (cam->disconnected) { 2911 if (cam->disconnected) {
2906 DBG(2, "Device not present") 2912 DBG(2, "Device not present")
2907 up(&cam->fileop_sem); 2913 mutex_unlock(&cam->fileop_mutex);
2908 return -ENODEV; 2914 return -ENODEV;
2909 } 2915 }
2910 2916
2911 if (cam->misconfigured) { 2917 if (cam->misconfigured) {
2912 DBG(2, "The camera is misconfigured. Close and open it again.") 2918 DBG(2, "The camera is misconfigured. Close and open it again.")
2913 up(&cam->fileop_sem); 2919 mutex_unlock(&cam->fileop_mutex);
2914 return -EIO; 2920 return -EIO;
2915 } 2921 }
2916 2922
2917 err = w9968cf_v4l_ioctl(inode, filp, cmd, (void __user *)arg); 2923 err = w9968cf_v4l_ioctl(inode, filp, cmd, (void __user *)arg);
2918 2924
2919 up(&cam->fileop_sem); 2925 mutex_unlock(&cam->fileop_mutex);
2920 return err; 2926 return err;
2921} 2927}
2922 2928
@@ -3499,14 +3505,12 @@ w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3499 return -ENODEV; 3505 return -ENODEV;
3500 3506
3501 cam = (struct w9968cf_device*) 3507 cam = (struct w9968cf_device*)
3502 kmalloc(sizeof(struct w9968cf_device), GFP_KERNEL); 3508 kzalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
3503 if (!cam) 3509 if (!cam)
3504 return -ENOMEM; 3510 return -ENOMEM;
3505 3511
3506 memset(cam, 0, sizeof(*cam)); 3512 mutex_init(&cam->dev_mutex);
3507 3513 mutex_lock(&cam->dev_mutex);
3508 init_MUTEX(&cam->dev_sem);
3509 down(&cam->dev_sem);
3510 3514
3511 cam->usbdev = udev; 3515 cam->usbdev = udev;
3512 /* NOTE: a local copy is used to avoid possible race conditions */ 3516 /* NOTE: a local copy is used to avoid possible race conditions */
@@ -3518,10 +3522,10 @@ w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3518 simcams = W9968CF_SIMCAMS; 3522 simcams = W9968CF_SIMCAMS;
3519 3523
3520 /* How many cameras are connected ? */ 3524 /* How many cameras are connected ? */
3521 down(&w9968cf_devlist_sem); 3525 mutex_lock(&w9968cf_devlist_mutex);
3522 list_for_each(ptr, &w9968cf_dev_list) 3526 list_for_each(ptr, &w9968cf_dev_list)
3523 sc++; 3527 sc++;
3524 up(&w9968cf_devlist_sem); 3528 mutex_unlock(&w9968cf_devlist_mutex);
3525 3529
3526 if (sc >= simcams) { 3530 if (sc >= simcams) {
3527 DBG(2, "Device rejected: too many connected cameras " 3531 DBG(2, "Device rejected: too many connected cameras "
@@ -3532,21 +3536,19 @@ w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3532 3536
3533 3537
3534 /* Allocate 2 bytes of memory for camera control USB transfers */ 3538 /* Allocate 2 bytes of memory for camera control USB transfers */
3535 if (!(cam->control_buffer = kmalloc(2, GFP_KERNEL))) { 3539 if (!(cam->control_buffer = kzalloc(2, GFP_KERNEL))) {
3536 DBG(1,"Couldn't allocate memory for camera control transfers") 3540 DBG(1,"Couldn't allocate memory for camera control transfers")
3537 err = -ENOMEM; 3541 err = -ENOMEM;
3538 goto fail; 3542 goto fail;
3539 } 3543 }
3540 memset(cam->control_buffer, 0, 2);
3541 3544
3542 /* Allocate 8 bytes of memory for USB data transfers to the FSB */ 3545 /* Allocate 8 bytes of memory for USB data transfers to the FSB */
3543 if (!(cam->data_buffer = kmalloc(8, GFP_KERNEL))) { 3546 if (!(cam->data_buffer = kzalloc(8, GFP_KERNEL))) {
3544 DBG(1, "Couldn't allocate memory for data " 3547 DBG(1, "Couldn't allocate memory for data "
3545 "transfers to the FSB") 3548 "transfers to the FSB")
3546 err = -ENOMEM; 3549 err = -ENOMEM;
3547 goto fail; 3550 goto fail;
3548 } 3551 }
3549 memset(cam->data_buffer, 0, 8);
3550 3552
3551 /* Register the V4L device */ 3553 /* Register the V4L device */
3552 cam->v4ldev = video_device_alloc(); 3554 cam->v4ldev = video_device_alloc();
@@ -3583,9 +3585,9 @@ w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3583 w9968cf_configure_camera(cam, udev, mod_id, dev_nr); 3585 w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
3584 3586
3585 /* Add a new entry into the list of V4L registered devices */ 3587 /* Add a new entry into the list of V4L registered devices */
3586 down(&w9968cf_devlist_sem); 3588 mutex_lock(&w9968cf_devlist_mutex);
3587 list_add(&cam->v4llist, &w9968cf_dev_list); 3589 list_add(&cam->v4llist, &w9968cf_dev_list);
3588 up(&w9968cf_devlist_sem); 3590 mutex_unlock(&w9968cf_devlist_mutex);
3589 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0; 3591 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3590 3592
3591 w9968cf_turn_on_led(cam); 3593 w9968cf_turn_on_led(cam);
@@ -3593,7 +3595,7 @@ w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3593 w9968cf_i2c_init(cam); 3595 w9968cf_i2c_init(cam);
3594 3596
3595 usb_set_intfdata(intf, cam); 3597 usb_set_intfdata(intf, cam);
3596 up(&cam->dev_sem); 3598 mutex_unlock(&cam->dev_mutex);
3597 return 0; 3599 return 0;
3598 3600
3599fail: /* Free unused memory */ 3601fail: /* Free unused memory */
@@ -3601,7 +3603,7 @@ fail: /* Free unused memory */
3601 kfree(cam->data_buffer); 3603 kfree(cam->data_buffer);
3602 if (cam->v4ldev) 3604 if (cam->v4ldev)
3603 video_device_release(cam->v4ldev); 3605 video_device_release(cam->v4ldev);
3604 up(&cam->dev_sem); 3606 mutex_unlock(&cam->dev_mutex);
3605 kfree(cam); 3607 kfree(cam);
3606 return err; 3608 return err;
3607} 3609}
@@ -3616,7 +3618,7 @@ static void w9968cf_usb_disconnect(struct usb_interface* intf)
3616 3618
3617 if (cam) { 3619 if (cam) {
3618 /* Prevent concurrent accesses to data */ 3620 /* Prevent concurrent accesses to data */
3619 down(&cam->dev_sem); 3621 mutex_lock(&cam->dev_mutex);
3620 3622
3621 cam->disconnected = 1; 3623 cam->disconnected = 1;
3622 3624
@@ -3635,7 +3637,7 @@ static void w9968cf_usb_disconnect(struct usb_interface* intf)
3635 } else 3637 } else
3636 w9968cf_release_resources(cam); 3638 w9968cf_release_resources(cam);
3637 3639
3638 up(&cam->dev_sem); 3640 mutex_unlock(&cam->dev_mutex);
3639 3641
3640 if (!cam->users) 3642 if (!cam->users)
3641 kfree(cam); 3643 kfree(cam);
diff --git a/drivers/usb/media/w9968cf.h b/drivers/usb/media/w9968cf.h
index 47a6ff794171..a87be719a281 100644
--- a/drivers/usb/media/w9968cf.h
+++ b/drivers/usb/media/w9968cf.h
@@ -32,7 +32,7 @@
32#include <linux/param.h> 32#include <linux/param.h>
33#include <linux/types.h> 33#include <linux/types.h>
34#include <linux/rwsem.h> 34#include <linux/rwsem.h>
35#include <asm/semaphore.h> 35#include <linux/mutex.h>
36 36
37#include <media/ovcamchip.h> 37#include <media/ovcamchip.h>
38 38
@@ -194,14 +194,6 @@ enum w9968cf_vpp_flag {
194 VPP_UYVY_TO_RGBX = 0x08, 194 VPP_UYVY_TO_RGBX = 0x08,
195}; 195};
196 196
197static struct w9968cf_vpp_t* w9968cf_vpp;
198static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
199
200static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
201static DECLARE_MUTEX(w9968cf_devlist_sem); /* semaphore for list traversal */
202
203static DECLARE_RWSEM(w9968cf_disconnect); /* prevent races with open() */
204
205/* Main device driver structure */ 197/* Main device driver structure */
206struct w9968cf_device { 198struct w9968cf_device {
207 struct device dev; /* device structure */ 199 struct device dev; /* device structure */
@@ -277,8 +269,8 @@ struct w9968cf_device {
277 struct i2c_client* sensor_client; 269 struct i2c_client* sensor_client;
278 270
279 /* Locks */ 271 /* Locks */
280 struct semaphore dev_sem, /* for probe, disconnect,open and close */ 272 struct mutex dev_mutex, /* for probe, disconnect,open and close */
281 fileop_sem; /* for read and ioctl */ 273 fileop_mutex; /* for read and ioctl */
282 spinlock_t urb_lock, /* for submit_urb() and unlink_urb() */ 274 spinlock_t urb_lock, /* for submit_urb() and unlink_urb() */
283 flist_lock; /* for requested frame list accesses */ 275 flist_lock; /* for requested frame list accesses */
284 wait_queue_head_t open, wait_queue; 276 wait_queue_head_t open, wait_queue;
diff --git a/drivers/usb/media/zc0301.h b/drivers/usb/media/zc0301.h
new file mode 100644
index 000000000000..8e0655140e60
--- /dev/null
+++ b/drivers/usb/media/zc0301.h
@@ -0,0 +1,192 @@
1/***************************************************************************
2 * V4L2 driver for ZC0301 Image Processor and Control Chip *
3 * *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#ifndef _ZC0301_H_
22#define _ZC0301_H_
23
24#include <linux/version.h>
25#include <linux/usb.h>
26#include <linux/videodev2.h>
27#include <media/v4l2-common.h>
28#include <linux/device.h>
29#include <linux/list.h>
30#include <linux/spinlock.h>
31#include <linux/time.h>
32#include <linux/wait.h>
33#include <linux/types.h>
34#include <linux/param.h>
35#include <linux/mutex.h>
36#include <linux/rwsem.h>
37#include <linux/stddef.h>
38#include <linux/string.h>
39
40#include "zc0301_sensor.h"
41
42/*****************************************************************************/
43
44#define ZC0301_DEBUG
45#define ZC0301_DEBUG_LEVEL 2
46#define ZC0301_MAX_DEVICES 64
47#define ZC0301_FORCE_MUNMAP 0
48#define ZC0301_MAX_FRAMES 32
49#define ZC0301_COMPRESSION_QUALITY 0
50#define ZC0301_URBS 2
51#define ZC0301_ISO_PACKETS 7
52#define ZC0301_ALTERNATE_SETTING 7
53#define ZC0301_URB_TIMEOUT msecs_to_jiffies(2 * ZC0301_ISO_PACKETS)
54#define ZC0301_CTRL_TIMEOUT 100
55#define ZC0301_FRAME_TIMEOUT 2
56
57/*****************************************************************************/
58
59ZC0301_ID_TABLE
60ZC0301_SENSOR_TABLE
61
62enum zc0301_frame_state {
63 F_UNUSED,
64 F_QUEUED,
65 F_GRABBING,
66 F_DONE,
67 F_ERROR,
68};
69
70struct zc0301_frame_t {
71 void* bufmem;
72 struct v4l2_buffer buf;
73 enum zc0301_frame_state state;
74 struct list_head frame;
75 unsigned long vma_use_count;
76};
77
78enum zc0301_dev_state {
79 DEV_INITIALIZED = 0x01,
80 DEV_DISCONNECTED = 0x02,
81 DEV_MISCONFIGURED = 0x04,
82};
83
84enum zc0301_io_method {
85 IO_NONE,
86 IO_READ,
87 IO_MMAP,
88};
89
90enum zc0301_stream_state {
91 STREAM_OFF,
92 STREAM_INTERRUPT,
93 STREAM_ON,
94};
95
96struct zc0301_module_param {
97 u8 force_munmap;
98 u16 frame_timeout;
99};
100
101static DECLARE_RWSEM(zc0301_disconnect);
102
103struct zc0301_device {
104 struct video_device* v4ldev;
105
106 struct zc0301_sensor sensor;
107
108 struct usb_device* usbdev;
109 struct urb* urb[ZC0301_URBS];
110 void* transfer_buffer[ZC0301_URBS];
111 u8* control_buffer;
112
113 struct zc0301_frame_t *frame_current, frame[ZC0301_MAX_FRAMES];
114 struct list_head inqueue, outqueue;
115 u32 frame_count, nbuffers, nreadbuffers;
116
117 enum zc0301_io_method io;
118 enum zc0301_stream_state stream;
119
120 struct v4l2_jpegcompression compression;
121
122 struct zc0301_module_param module_param;
123
124 enum zc0301_dev_state state;
125 u8 users;
126
127 struct mutex dev_mutex, fileop_mutex;
128 spinlock_t queue_lock;
129 wait_queue_head_t open, wait_frame, wait_stream;
130};
131
132/*****************************************************************************/
133
134struct zc0301_device*
135zc0301_match_id(struct zc0301_device* cam, const struct usb_device_id *id)
136{
137 return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL;
138}
139
140void
141zc0301_attach_sensor(struct zc0301_device* cam, struct zc0301_sensor* sensor)
142{
143 memcpy(&cam->sensor, sensor, sizeof(struct zc0301_sensor));
144}
145
146/*****************************************************************************/
147
148#undef DBG
149#undef KDBG
150#ifdef ZC0301_DEBUG
151# define DBG(level, fmt, args...) \
152do { \
153 if (debug >= (level)) { \
154 if ((level) == 1) \
155 dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
156 else if ((level) == 2) \
157 dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
158 else if ((level) >= 3) \
159 dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
160 __FUNCTION__, __LINE__ , ## args); \
161 } \
162} while (0)
163# define KDBG(level, fmt, args...) \
164do { \
165 if (debug >= (level)) { \
166 if ((level) == 1 || (level) == 2) \
167 pr_info("zc0301: " fmt "\n", ## args); \
168 else if ((level) == 3) \
169 pr_debug("zc0301: [%s:%d] " fmt "\n", __FUNCTION__, \
170 __LINE__ , ## args); \
171 } \
172} while (0)
173# define V4LDBG(level, name, cmd) \
174do { \
175 if (debug >= (level)) \
176 v4l_print_ioctl(name, cmd); \
177} while (0)
178#else
179# define DBG(level, fmt, args...) do {;} while(0)
180# define KDBG(level, fmt, args...) do {;} while(0)
181# define V4LDBG(level, name, cmd) do {;} while(0)
182#endif
183
184#undef PDBG
185#define PDBG(fmt, args...) \
186dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
187 __FUNCTION__, __LINE__ , ## args)
188
189#undef PDBGG
190#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
191
192#endif /* _ZC0301_H_ */
diff --git a/drivers/usb/media/zc0301_core.c b/drivers/usb/media/zc0301_core.c
new file mode 100644
index 000000000000..4036c6268bff
--- /dev/null
+++ b/drivers/usb/media/zc0301_core.c
@@ -0,0 +1,2055 @@
1/***************************************************************************
2 * Video4Linux2 driver for ZC0301 Image Processor and Control Chip *
3 * *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * Informations about the chip internals needed to enable the I2C protocol *
7 * have been taken from the documentation of the ZC030x Video4Linux1 *
8 * driver written by Andrew Birkett <andy@nobugs.org> *
9 * *
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 *
13 * (at your option) any later version. *
14 * *
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. *
19 * *
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., 675 Mass Ave, Cambridge, MA 02139, USA. *
23 ***************************************************************************/
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/param.h>
29#include <linux/moduleparam.h>
30#include <linux/errno.h>
31#include <linux/slab.h>
32#include <linux/device.h>
33#include <linux/fs.h>
34#include <linux/delay.h>
35#include <linux/compiler.h>
36#include <linux/ioctl.h>
37#include <linux/poll.h>
38#include <linux/stat.h>
39#include <linux/mm.h>
40#include <linux/vmalloc.h>
41#include <linux/page-flags.h>
42#include <linux/byteorder/generic.h>
43#include <asm/page.h>
44#include <asm/uaccess.h>
45
46#include "zc0301.h"
47
48/*****************************************************************************/
49
50#define ZC0301_MODULE_NAME "V4L2 driver for ZC0301 " \
51 "Image Processor and Control Chip"
52#define ZC0301_MODULE_AUTHOR "(C) 2006 Luca Risolia"
53#define ZC0301_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
54#define ZC0301_MODULE_LICENSE "GPL"
55#define ZC0301_MODULE_VERSION "1:1.03"
56#define ZC0301_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 3)
57
58/*****************************************************************************/
59
60MODULE_DEVICE_TABLE(usb, zc0301_id_table);
61
62MODULE_AUTHOR(ZC0301_MODULE_AUTHOR " " ZC0301_AUTHOR_EMAIL);
63MODULE_DESCRIPTION(ZC0301_MODULE_NAME);
64MODULE_VERSION(ZC0301_MODULE_VERSION);
65MODULE_LICENSE(ZC0301_MODULE_LICENSE);
66
67static short video_nr[] = {[0 ... ZC0301_MAX_DEVICES-1] = -1};
68module_param_array(video_nr, short, NULL, 0444);
69MODULE_PARM_DESC(video_nr,
70 "\n<-1|n[,...]> Specify V4L2 minor mode number."
71 "\n -1 = use next available (default)"
72 "\n n = use minor number n (integer >= 0)"
73 "\nYou can specify up to "
74 __MODULE_STRING(ZC0301_MAX_DEVICES) " cameras this way."
75 "\nFor example:"
76 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
77 "\nthe second registered camera and use auto for the first"
78 "\none and for every other camera."
79 "\n");
80
81static short force_munmap[] = {[0 ... ZC0301_MAX_DEVICES-1] =
82 ZC0301_FORCE_MUNMAP};
83module_param_array(force_munmap, bool, NULL, 0444);
84MODULE_PARM_DESC(force_munmap,
85 "\n<0|1[,...]> Force the application to unmap previously"
86 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
87 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
88 "\nthis feature. This parameter is specific for each"
89 "\ndetected camera."
90 "\n 0 = do not force memory unmapping"
91 "\n 1 = force memory unmapping (save memory)"
92 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
93 "\n");
94
95static unsigned int frame_timeout[] = {[0 ... ZC0301_MAX_DEVICES-1] =
96 ZC0301_FRAME_TIMEOUT};
97module_param_array(frame_timeout, uint, NULL, 0644);
98MODULE_PARM_DESC(frame_timeout,
99 "\n<n[,...]> Timeout for a video frame in seconds."
100 "\nThis parameter is specific for each detected camera."
101 "\nDefault value is "__MODULE_STRING(ZC0301_FRAME_TIMEOUT)"."
102 "\n");
103
104#ifdef ZC0301_DEBUG
105static unsigned short debug = ZC0301_DEBUG_LEVEL;
106module_param(debug, ushort, 0644);
107MODULE_PARM_DESC(debug,
108 "\n<n> Debugging information level, from 0 to 3:"
109 "\n0 = none (use carefully)"
110 "\n1 = critical errors"
111 "\n2 = significant informations"
112 "\n3 = more verbose messages"
113 "\nLevel 3 is useful for testing only, when only "
114 "one device is used."
115 "\nDefault value is "__MODULE_STRING(ZC0301_DEBUG_LEVEL)"."
116 "\n");
117#endif
118
119/*****************************************************************************/
120
121static u32
122zc0301_request_buffers(struct zc0301_device* cam, u32 count,
123 enum zc0301_io_method io)
124{
125 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
126 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
127 const size_t imagesize = cam->module_param.force_munmap ||
128 io == IO_READ ?
129 (p->width * p->height * p->priv) / 8 :
130 (r->width * r->height * p->priv) / 8;
131 void* buff = NULL;
132 u32 i;
133
134 if (count > ZC0301_MAX_FRAMES)
135 count = ZC0301_MAX_FRAMES;
136
137 cam->nbuffers = count;
138 while (cam->nbuffers > 0) {
139 if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
140 break;
141 cam->nbuffers--;
142 }
143
144 for (i = 0; i < cam->nbuffers; i++) {
145 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
146 cam->frame[i].buf.index = i;
147 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
148 cam->frame[i].buf.length = imagesize;
149 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
150 cam->frame[i].buf.sequence = 0;
151 cam->frame[i].buf.field = V4L2_FIELD_NONE;
152 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
153 cam->frame[i].buf.flags = 0;
154 }
155
156 return cam->nbuffers;
157}
158
159
160static void zc0301_release_buffers(struct zc0301_device* cam)
161{
162 if (cam->nbuffers) {
163 vfree(cam->frame[0].bufmem);
164 cam->nbuffers = 0;
165 }
166 cam->frame_current = NULL;
167}
168
169
170static void zc0301_empty_framequeues(struct zc0301_device* cam)
171{
172 u32 i;
173
174 INIT_LIST_HEAD(&cam->inqueue);
175 INIT_LIST_HEAD(&cam->outqueue);
176
177 for (i = 0; i < ZC0301_MAX_FRAMES; i++) {
178 cam->frame[i].state = F_UNUSED;
179 cam->frame[i].buf.bytesused = 0;
180 }
181}
182
183
184static void zc0301_requeue_outqueue(struct zc0301_device* cam)
185{
186 struct zc0301_frame_t *i;
187
188 list_for_each_entry(i, &cam->outqueue, frame) {
189 i->state = F_QUEUED;
190 list_add(&i->frame, &cam->inqueue);
191 }
192
193 INIT_LIST_HEAD(&cam->outqueue);
194}
195
196
197static void zc0301_queue_unusedframes(struct zc0301_device* cam)
198{
199 unsigned long lock_flags;
200 u32 i;
201
202 for (i = 0; i < cam->nbuffers; i++)
203 if (cam->frame[i].state == F_UNUSED) {
204 cam->frame[i].state = F_QUEUED;
205 spin_lock_irqsave(&cam->queue_lock, lock_flags);
206 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
207 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
208 }
209}
210
211/*****************************************************************************/
212
213int zc0301_write_reg(struct zc0301_device* cam, u16 index, u16 value)
214{
215 struct usb_device* udev = cam->usbdev;
216 int res;
217
218 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0xa0, 0x40,
219 value, index, NULL, 0, ZC0301_CTRL_TIMEOUT);
220 if (res < 0) {
221 DBG(3, "Failed to write a register (index 0x%04X, "
222 "value 0x%02X, error %d)",index, value, res);
223 return -1;
224 }
225
226 return 0;
227}
228
229
230int zc0301_read_reg(struct zc0301_device* cam, u16 index)
231{
232 struct usb_device* udev = cam->usbdev;
233 u8* buff = cam->control_buffer;
234 int res;
235
236 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0xa1, 0xc0,
237 0x0001, index, buff, 1, ZC0301_CTRL_TIMEOUT);
238 if (res < 0)
239 DBG(3, "Failed to read a register (index 0x%04X, error %d)",
240 index, res);
241
242 PDBGG("Read: index 0x%04X, value: 0x%04X", index, (int)(*buff));
243
244 return (res >= 0) ? (int)(*buff) : -1;
245}
246
247
248int zc0301_i2c_read(struct zc0301_device* cam, u16 address, u8 length)
249{
250 int err = 0, res, r0, r1;
251
252 err += zc0301_write_reg(cam, 0x0092, address);
253 err += zc0301_write_reg(cam, 0x0090, 0x02);
254
255 msleep(1);
256
257 res = zc0301_read_reg(cam, 0x0091);
258 if (res < 0)
259 err += res;
260 r0 = zc0301_read_reg(cam, 0x0095);
261 if (r0 < 0)
262 err += r0;
263 r1 = zc0301_read_reg(cam, 0x0096);
264 if (r1 < 0)
265 err += r1;
266
267 res = (length <= 1) ? r0 : r0 | (r1 << 8);
268
269 if (err)
270 DBG(3, "I2C read failed at address 0x%04X, value: 0x%04X",
271 address, res);
272
273
274 PDBGG("I2C read: address 0x%04X, value: 0x%04X", address, res);
275
276 return err ? -1 : res;
277}
278
279
280int zc0301_i2c_write(struct zc0301_device* cam, u16 address, u16 value)
281{
282 int err = 0, res;
283
284 err += zc0301_write_reg(cam, 0x0092, address);
285 err += zc0301_write_reg(cam, 0x0093, value & 0xff);
286 err += zc0301_write_reg(cam, 0x0094, value >> 8);
287 err += zc0301_write_reg(cam, 0x0090, 0x01);
288
289 msleep(1);
290
291 res = zc0301_read_reg(cam, 0x0091);
292 if (res < 0)
293 err += res;
294
295 if (err)
296 DBG(3, "I2C write failed at address 0x%04X, value: 0x%04X",
297 address, value);
298
299 PDBGG("I2C write: address 0x%04X, value: 0x%04X", address, value);
300
301 return err ? -1 : 0;
302}
303
304/*****************************************************************************/
305
306static void zc0301_urb_complete(struct urb *urb, struct pt_regs* regs)
307{
308 struct zc0301_device* cam = urb->context;
309 struct zc0301_frame_t** f;
310 size_t imagesize;
311 u8 i;
312 int err = 0;
313
314 if (urb->status == -ENOENT)
315 return;
316
317 f = &cam->frame_current;
318
319 if (cam->stream == STREAM_INTERRUPT) {
320 cam->stream = STREAM_OFF;
321 if ((*f))
322 (*f)->state = F_QUEUED;
323 DBG(3, "Stream interrupted");
324 wake_up(&cam->wait_stream);
325 }
326
327 if (cam->state & DEV_DISCONNECTED)
328 return;
329
330 if (cam->state & DEV_MISCONFIGURED) {
331 wake_up_interruptible(&cam->wait_frame);
332 return;
333 }
334
335 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
336 goto resubmit_urb;
337
338 if (!(*f))
339 (*f) = list_entry(cam->inqueue.next, struct zc0301_frame_t,
340 frame);
341
342 imagesize = (cam->sensor.pix_format.width *
343 cam->sensor.pix_format.height *
344 cam->sensor.pix_format.priv) / 8;
345
346 for (i = 0; i < urb->number_of_packets; i++) {
347 unsigned int len, status;
348 void *pos;
349 u16* soi;
350 u8 sof;
351
352 len = urb->iso_frame_desc[i].actual_length;
353 status = urb->iso_frame_desc[i].status;
354 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
355
356 if (status) {
357 DBG(3, "Error in isochronous frame");
358 (*f)->state = F_ERROR;
359 continue;
360 }
361
362 sof = (*(soi = pos) == 0xd8ff);
363
364 PDBGG("Isochrnous frame: length %u, #%u i,", len, i);
365
366 if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
367start_of_frame:
368 if (sof) {
369 (*f)->state = F_GRABBING;
370 (*f)->buf.bytesused = 0;
371 do_gettimeofday(&(*f)->buf.timestamp);
372 DBG(3, "SOF detected: new video frame");
373 }
374
375 if ((*f)->state == F_GRABBING) {
376 if (sof && (*f)->buf.bytesused)
377 goto end_of_frame;
378
379 if ((*f)->buf.bytesused + len > imagesize) {
380 DBG(3, "Video frame size exceeded");
381 (*f)->state = F_ERROR;
382 continue;
383 }
384
385 memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, len);
386 (*f)->buf.bytesused += len;
387
388 if ((*f)->buf.bytesused == imagesize) {
389 u32 b;
390end_of_frame:
391 b = (*f)->buf.bytesused;
392 (*f)->state = F_DONE;
393 (*f)->buf.sequence= ++cam->frame_count;
394 spin_lock(&cam->queue_lock);
395 list_move_tail(&(*f)->frame, &cam->outqueue);
396 if (!list_empty(&cam->inqueue))
397 (*f) = list_entry(cam->inqueue.next,
398 struct zc0301_frame_t,
399 frame);
400 else
401 (*f) = NULL;
402 spin_unlock(&cam->queue_lock);
403 DBG(3, "Video frame captured: : %lu bytes",
404 (unsigned long)(b));
405
406 if (!(*f))
407 goto resubmit_urb;
408
409 if (sof)
410 goto start_of_frame;
411 }
412 }
413 }
414
415resubmit_urb:
416 urb->dev = cam->usbdev;
417 err = usb_submit_urb(urb, GFP_ATOMIC);
418 if (err < 0 && err != -EPERM) {
419 cam->state |= DEV_MISCONFIGURED;
420 DBG(1, "usb_submit_urb() failed");
421 }
422
423 wake_up_interruptible(&cam->wait_frame);
424}
425
426
427static int zc0301_start_transfer(struct zc0301_device* cam)
428{
429 struct usb_device *udev = cam->usbdev;
430 struct urb* urb;
431 const unsigned int wMaxPacketSize[] = {0, 128, 192, 256, 384,
432 512, 768, 1023};
433 const unsigned int psz = wMaxPacketSize[ZC0301_ALTERNATE_SETTING];
434 s8 i, j;
435 int err = 0;
436
437 for (i = 0; i < ZC0301_URBS; i++) {
438 cam->transfer_buffer[i] = kzalloc(ZC0301_ISO_PACKETS * psz,
439 GFP_KERNEL);
440 if (!cam->transfer_buffer[i]) {
441 err = -ENOMEM;
442 DBG(1, "Not enough memory");
443 goto free_buffers;
444 }
445 }
446
447 for (i = 0; i < ZC0301_URBS; i++) {
448 urb = usb_alloc_urb(ZC0301_ISO_PACKETS, GFP_KERNEL);
449 cam->urb[i] = urb;
450 if (!urb) {
451 err = -ENOMEM;
452 DBG(1, "usb_alloc_urb() failed");
453 goto free_urbs;
454 }
455 urb->dev = udev;
456 urb->context = cam;
457 urb->pipe = usb_rcvisocpipe(udev, 1);
458 urb->transfer_flags = URB_ISO_ASAP;
459 urb->number_of_packets = ZC0301_ISO_PACKETS;
460 urb->complete = zc0301_urb_complete;
461 urb->transfer_buffer = cam->transfer_buffer[i];
462 urb->transfer_buffer_length = psz * ZC0301_ISO_PACKETS;
463 urb->interval = 1;
464 for (j = 0; j < ZC0301_ISO_PACKETS; j++) {
465 urb->iso_frame_desc[j].offset = psz * j;
466 urb->iso_frame_desc[j].length = psz;
467 }
468 }
469
470 err = usb_set_interface(udev, 0, ZC0301_ALTERNATE_SETTING);
471 if (err) {
472 DBG(1, "usb_set_interface() failed");
473 goto free_urbs;
474 }
475
476 cam->frame_current = NULL;
477
478 for (i = 0; i < ZC0301_URBS; i++) {
479 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
480 if (err) {
481 for (j = i-1; j >= 0; j--)
482 usb_kill_urb(cam->urb[j]);
483 DBG(1, "usb_submit_urb() failed, error %d", err);
484 goto free_urbs;
485 }
486 }
487
488 return 0;
489
490free_urbs:
491 for (i = 0; (i < ZC0301_URBS) && cam->urb[i]; i++)
492 usb_free_urb(cam->urb[i]);
493
494free_buffers:
495 for (i = 0; (i < ZC0301_URBS) && cam->transfer_buffer[i]; i++)
496 kfree(cam->transfer_buffer[i]);
497
498 return err;
499}
500
501
502static int zc0301_stop_transfer(struct zc0301_device* cam)
503{
504 struct usb_device *udev = cam->usbdev;
505 s8 i;
506 int err = 0;
507
508 if (cam->state & DEV_DISCONNECTED)
509 return 0;
510
511 for (i = ZC0301_URBS-1; i >= 0; i--) {
512 usb_kill_urb(cam->urb[i]);
513 usb_free_urb(cam->urb[i]);
514 kfree(cam->transfer_buffer[i]);
515 }
516
517 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
518 if (err)
519 DBG(3, "usb_set_interface() failed");
520
521 return err;
522}
523
524
525static int zc0301_stream_interrupt(struct zc0301_device* cam)
526{
527 long timeout;
528
529 cam->stream = STREAM_INTERRUPT;
530 timeout = wait_event_timeout(cam->wait_stream,
531 (cam->stream == STREAM_OFF) ||
532 (cam->state & DEV_DISCONNECTED),
533 ZC0301_URB_TIMEOUT);
534 if (cam->state & DEV_DISCONNECTED)
535 return -ENODEV;
536 else if (cam->stream != STREAM_OFF) {
537 cam->state |= DEV_MISCONFIGURED;
538 DBG(1, "URB timeout reached. The camera is misconfigured. To "
539 "use it, close and open /dev/video%d again.",
540 cam->v4ldev->minor);
541 return -EIO;
542 }
543
544 return 0;
545}
546
547/*****************************************************************************/
548
549static int
550zc0301_set_compression(struct zc0301_device* cam,
551 struct v4l2_jpegcompression* compression)
552{
553 int r, err = 0;
554
555 if ((r = zc0301_read_reg(cam, 0x0008)) < 0)
556 err += r;
557 err += zc0301_write_reg(cam, 0x0008, r | 0x11 | compression->quality);
558
559 return err ? -EIO : 0;
560}
561
562
563static int zc0301_init(struct zc0301_device* cam)
564{
565 struct zc0301_sensor* s = &cam->sensor;
566 struct v4l2_control ctrl;
567 struct v4l2_queryctrl *qctrl;
568 struct v4l2_rect* rect;
569 u8 i = 0;
570 int err = 0;
571
572 if (!(cam->state & DEV_INITIALIZED)) {
573 init_waitqueue_head(&cam->open);
574 qctrl = s->qctrl;
575 rect = &(s->cropcap.defrect);
576 cam->compression.quality = ZC0301_COMPRESSION_QUALITY;
577 } else { /* use current values */
578 qctrl = s->_qctrl;
579 rect = &(s->_rect);
580 }
581
582 if (s->init) {
583 err = s->init(cam);
584 if (err) {
585 DBG(3, "Sensor initialization failed");
586 return err;
587 }
588 }
589
590 if ((err = zc0301_set_compression(cam, &cam->compression))) {
591 DBG(3, "set_compression() failed");
592 return err;
593 }
594
595 if (s->set_crop)
596 if ((err = s->set_crop(cam, rect))) {
597 DBG(3, "set_crop() failed");
598 return err;
599 }
600
601 if (s->set_ctrl) {
602 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
603 if (s->qctrl[i].id != 0 &&
604 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
605 ctrl.id = s->qctrl[i].id;
606 ctrl.value = qctrl[i].default_value;
607 err = s->set_ctrl(cam, &ctrl);
608 if (err) {
609 DBG(3, "Set %s control failed",
610 s->qctrl[i].name);
611 return err;
612 }
613 DBG(3, "Image sensor supports '%s' control",
614 s->qctrl[i].name);
615 }
616 }
617
618 if (!(cam->state & DEV_INITIALIZED)) {
619 mutex_init(&cam->fileop_mutex);
620 spin_lock_init(&cam->queue_lock);
621 init_waitqueue_head(&cam->wait_frame);
622 init_waitqueue_head(&cam->wait_stream);
623 cam->nreadbuffers = 2;
624 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
625 memcpy(&(s->_rect), &(s->cropcap.defrect),
626 sizeof(struct v4l2_rect));
627 cam->state |= DEV_INITIALIZED;
628 }
629
630 DBG(2, "Initialization succeeded");
631 return 0;
632}
633
634
635static void zc0301_release_resources(struct zc0301_device* cam)
636{
637 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
638 video_set_drvdata(cam->v4ldev, NULL);
639 video_unregister_device(cam->v4ldev);
640 kfree(cam->control_buffer);
641}
642
643/*****************************************************************************/
644
645static int zc0301_open(struct inode* inode, struct file* filp)
646{
647 struct zc0301_device* cam;
648 int err = 0;
649
650 /*
651 This is the only safe way to prevent race conditions with
652 disconnect
653 */
654 if (!down_read_trylock(&zc0301_disconnect))
655 return -ERESTARTSYS;
656
657 cam = video_get_drvdata(video_devdata(filp));
658
659 if (mutex_lock_interruptible(&cam->dev_mutex)) {
660 up_read(&zc0301_disconnect);
661 return -ERESTARTSYS;
662 }
663
664 if (cam->users) {
665 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
666 if ((filp->f_flags & O_NONBLOCK) ||
667 (filp->f_flags & O_NDELAY)) {
668 err = -EWOULDBLOCK;
669 goto out;
670 }
671 mutex_unlock(&cam->dev_mutex);
672 err = wait_event_interruptible_exclusive(cam->open,
673 cam->state & DEV_DISCONNECTED
674 || !cam->users);
675 if (err) {
676 up_read(&zc0301_disconnect);
677 return err;
678 }
679 if (cam->state & DEV_DISCONNECTED) {
680 up_read(&zc0301_disconnect);
681 return -ENODEV;
682 }
683 mutex_lock(&cam->dev_mutex);
684 }
685
686
687 if (cam->state & DEV_MISCONFIGURED) {
688 err = zc0301_init(cam);
689 if (err) {
690 DBG(1, "Initialization failed again. "
691 "I will retry on next open().");
692 goto out;
693 }
694 cam->state &= ~DEV_MISCONFIGURED;
695 }
696
697 if ((err = zc0301_start_transfer(cam)))
698 goto out;
699
700 filp->private_data = cam;
701 cam->users++;
702 cam->io = IO_NONE;
703 cam->stream = STREAM_OFF;
704 cam->nbuffers = 0;
705 cam->frame_count = 0;
706 zc0301_empty_framequeues(cam);
707
708 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
709
710out:
711 mutex_unlock(&cam->dev_mutex);
712 up_read(&zc0301_disconnect);
713 return err;
714}
715
716
717static int zc0301_release(struct inode* inode, struct file* filp)
718{
719 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
720
721 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
722
723 zc0301_stop_transfer(cam);
724
725 zc0301_release_buffers(cam);
726
727 if (cam->state & DEV_DISCONNECTED) {
728 zc0301_release_resources(cam);
729 usb_put_dev(cam->usbdev);
730 mutex_unlock(&cam->dev_mutex);
731 kfree(cam);
732 return 0;
733 }
734
735 cam->users--;
736 wake_up_interruptible_nr(&cam->open, 1);
737
738 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
739
740 mutex_unlock(&cam->dev_mutex);
741
742 return 0;
743}
744
745
746static ssize_t
747zc0301_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
748{
749 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
750 struct zc0301_frame_t* f, * i;
751 unsigned long lock_flags;
752 long timeout;
753 int err = 0;
754
755 if (mutex_lock_interruptible(&cam->fileop_mutex))
756 return -ERESTARTSYS;
757
758 if (cam->state & DEV_DISCONNECTED) {
759 DBG(1, "Device not present");
760 mutex_unlock(&cam->fileop_mutex);
761 return -ENODEV;
762 }
763
764 if (cam->state & DEV_MISCONFIGURED) {
765 DBG(1, "The camera is misconfigured. Close and open it "
766 "again.");
767 mutex_unlock(&cam->fileop_mutex);
768 return -EIO;
769 }
770
771 if (cam->io == IO_MMAP) {
772 DBG(3, "Close and open the device again to choose the read "
773 "method");
774 mutex_unlock(&cam->fileop_mutex);
775 return -EINVAL;
776 }
777
778 if (cam->io == IO_NONE) {
779 if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) {
780 DBG(1, "read() failed, not enough memory");
781 mutex_unlock(&cam->fileop_mutex);
782 return -ENOMEM;
783 }
784 cam->io = IO_READ;
785 cam->stream = STREAM_ON;
786 }
787
788 if (list_empty(&cam->inqueue)) {
789 if (!list_empty(&cam->outqueue))
790 zc0301_empty_framequeues(cam);
791 zc0301_queue_unusedframes(cam);
792 }
793
794 if (!count) {
795 mutex_unlock(&cam->fileop_mutex);
796 return 0;
797 }
798
799 if (list_empty(&cam->outqueue)) {
800 if (filp->f_flags & O_NONBLOCK) {
801 mutex_unlock(&cam->fileop_mutex);
802 return -EAGAIN;
803 }
804 timeout = wait_event_interruptible_timeout
805 ( cam->wait_frame,
806 (!list_empty(&cam->outqueue)) ||
807 (cam->state & DEV_DISCONNECTED) ||
808 (cam->state & DEV_MISCONFIGURED),
809 cam->module_param.frame_timeout *
810 1000 * msecs_to_jiffies(1) );
811 if (timeout < 0) {
812 mutex_unlock(&cam->fileop_mutex);
813 return timeout;
814 }
815 if (cam->state & DEV_DISCONNECTED) {
816 mutex_unlock(&cam->fileop_mutex);
817 return -ENODEV;
818 }
819 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
820 mutex_unlock(&cam->fileop_mutex);
821 return -EIO;
822 }
823 }
824
825 f = list_entry(cam->outqueue.prev, struct zc0301_frame_t, frame);
826
827 if (count > f->buf.bytesused)
828 count = f->buf.bytesused;
829
830 if (copy_to_user(buf, f->bufmem, count)) {
831 err = -EFAULT;
832 goto exit;
833 }
834 *f_pos += count;
835
836exit:
837 spin_lock_irqsave(&cam->queue_lock, lock_flags);
838 list_for_each_entry(i, &cam->outqueue, frame)
839 i->state = F_UNUSED;
840 INIT_LIST_HEAD(&cam->outqueue);
841 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
842
843 zc0301_queue_unusedframes(cam);
844
845 PDBGG("Frame #%lu, bytes read: %zu",
846 (unsigned long)f->buf.index, count);
847
848 mutex_unlock(&cam->fileop_mutex);
849
850 return err ? err : count;
851}
852
853
854static unsigned int zc0301_poll(struct file *filp, poll_table *wait)
855{
856 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
857 struct zc0301_frame_t* f;
858 unsigned long lock_flags;
859 unsigned int mask = 0;
860
861 if (mutex_lock_interruptible(&cam->fileop_mutex))
862 return POLLERR;
863
864 if (cam->state & DEV_DISCONNECTED) {
865 DBG(1, "Device not present");
866 goto error;
867 }
868
869 if (cam->state & DEV_MISCONFIGURED) {
870 DBG(1, "The camera is misconfigured. Close and open it "
871 "again.");
872 goto error;
873 }
874
875 if (cam->io == IO_NONE) {
876 if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) {
877 DBG(1, "poll() failed, not enough memory");
878 goto error;
879 }
880 cam->io = IO_READ;
881 cam->stream = STREAM_ON;
882 }
883
884 if (cam->io == IO_READ) {
885 spin_lock_irqsave(&cam->queue_lock, lock_flags);
886 list_for_each_entry(f, &cam->outqueue, frame)
887 f->state = F_UNUSED;
888 INIT_LIST_HEAD(&cam->outqueue);
889 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
890 zc0301_queue_unusedframes(cam);
891 }
892
893 poll_wait(filp, &cam->wait_frame, wait);
894
895 if (!list_empty(&cam->outqueue))
896 mask |= POLLIN | POLLRDNORM;
897
898 mutex_unlock(&cam->fileop_mutex);
899
900 return mask;
901
902error:
903 mutex_unlock(&cam->fileop_mutex);
904 return POLLERR;
905}
906
907
908static void zc0301_vm_open(struct vm_area_struct* vma)
909{
910 struct zc0301_frame_t* f = vma->vm_private_data;
911 f->vma_use_count++;
912}
913
914
915static void zc0301_vm_close(struct vm_area_struct* vma)
916{
917 /* NOTE: buffers are not freed here */
918 struct zc0301_frame_t* f = vma->vm_private_data;
919 f->vma_use_count--;
920}
921
922
923static struct vm_operations_struct zc0301_vm_ops = {
924 .open = zc0301_vm_open,
925 .close = zc0301_vm_close,
926};
927
928
929static int zc0301_mmap(struct file* filp, struct vm_area_struct *vma)
930{
931 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
932 unsigned long size = vma->vm_end - vma->vm_start,
933 start = vma->vm_start;
934 void *pos;
935 u32 i;
936
937 if (mutex_lock_interruptible(&cam->fileop_mutex))
938 return -ERESTARTSYS;
939
940 if (cam->state & DEV_DISCONNECTED) {
941 DBG(1, "Device not present");
942 mutex_unlock(&cam->fileop_mutex);
943 return -ENODEV;
944 }
945
946 if (cam->state & DEV_MISCONFIGURED) {
947 DBG(1, "The camera is misconfigured. Close and open it "
948 "again.");
949 mutex_unlock(&cam->fileop_mutex);
950 return -EIO;
951 }
952
953 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
954 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
955 mutex_unlock(&cam->fileop_mutex);
956 return -EINVAL;
957 }
958
959 for (i = 0; i < cam->nbuffers; i++) {
960 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
961 break;
962 }
963 if (i == cam->nbuffers) {
964 mutex_unlock(&cam->fileop_mutex);
965 return -EINVAL;
966 }
967
968 vma->vm_flags |= VM_IO;
969 vma->vm_flags |= VM_RESERVED;
970
971 pos = cam->frame[i].bufmem;
972 while (size > 0) { /* size is page-aligned */
973 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
974 mutex_unlock(&cam->fileop_mutex);
975 return -EAGAIN;
976 }
977 start += PAGE_SIZE;
978 pos += PAGE_SIZE;
979 size -= PAGE_SIZE;
980 }
981
982 vma->vm_ops = &zc0301_vm_ops;
983 vma->vm_private_data = &cam->frame[i];
984
985 zc0301_vm_open(vma);
986
987 mutex_unlock(&cam->fileop_mutex);
988
989 return 0;
990}
991
992/*****************************************************************************/
993
994static int
995zc0301_vidioc_querycap(struct zc0301_device* cam, void __user * arg)
996{
997 struct v4l2_capability cap = {
998 .driver = "zc0301",
999 .version = ZC0301_MODULE_VERSION_CODE,
1000 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1001 V4L2_CAP_STREAMING,
1002 };
1003
1004 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1005 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1006 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1007 sizeof(cap.bus_info));
1008
1009 if (copy_to_user(arg, &cap, sizeof(cap)))
1010 return -EFAULT;
1011
1012 return 0;
1013}
1014
1015
1016static int
1017zc0301_vidioc_enuminput(struct zc0301_device* cam, void __user * arg)
1018{
1019 struct v4l2_input i;
1020
1021 if (copy_from_user(&i, arg, sizeof(i)))
1022 return -EFAULT;
1023
1024 if (i.index)
1025 return -EINVAL;
1026
1027 memset(&i, 0, sizeof(i));
1028 strcpy(i.name, "Camera");
1029 i.type = V4L2_INPUT_TYPE_CAMERA;
1030
1031 if (copy_to_user(arg, &i, sizeof(i)))
1032 return -EFAULT;
1033
1034 return 0;
1035}
1036
1037
1038static int
1039zc0301_vidioc_g_input(struct zc0301_device* cam, void __user * arg)
1040{
1041 int index = 0;
1042
1043 if (copy_to_user(arg, &index, sizeof(index)))
1044 return -EFAULT;
1045
1046 return 0;
1047}
1048
1049
1050static int
1051zc0301_vidioc_s_input(struct zc0301_device* cam, void __user * arg)
1052{
1053 int index;
1054
1055 if (copy_from_user(&index, arg, sizeof(index)))
1056 return -EFAULT;
1057
1058 if (index != 0)
1059 return -EINVAL;
1060
1061 return 0;
1062}
1063
1064
1065static int
1066zc0301_vidioc_query_ctrl(struct zc0301_device* cam, void __user * arg)
1067{
1068 struct zc0301_sensor* s = &cam->sensor;
1069 struct v4l2_queryctrl qc;
1070 u8 i;
1071
1072 if (copy_from_user(&qc, arg, sizeof(qc)))
1073 return -EFAULT;
1074
1075 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1076 if (qc.id && qc.id == s->qctrl[i].id) {
1077 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1078 if (copy_to_user(arg, &qc, sizeof(qc)))
1079 return -EFAULT;
1080 return 0;
1081 }
1082
1083 return -EINVAL;
1084}
1085
1086
1087static int
1088zc0301_vidioc_g_ctrl(struct zc0301_device* cam, void __user * arg)
1089{
1090 struct zc0301_sensor* s = &cam->sensor;
1091 struct v4l2_control ctrl;
1092 int err = 0;
1093 u8 i;
1094
1095 if (!s->get_ctrl && !s->set_ctrl)
1096 return -EINVAL;
1097
1098 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1099 return -EFAULT;
1100
1101 if (!s->get_ctrl) {
1102 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1103 if (ctrl.id == s->qctrl[i].id) {
1104 ctrl.value = s->_qctrl[i].default_value;
1105 goto exit;
1106 }
1107 return -EINVAL;
1108 } else
1109 err = s->get_ctrl(cam, &ctrl);
1110
1111exit:
1112 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1113 return -EFAULT;
1114
1115 return err;
1116}
1117
1118
1119static int
1120zc0301_vidioc_s_ctrl(struct zc0301_device* cam, void __user * arg)
1121{
1122 struct zc0301_sensor* s = &cam->sensor;
1123 struct v4l2_control ctrl;
1124 u8 i;
1125 int err = 0;
1126
1127 if (!s->set_ctrl)
1128 return -EINVAL;
1129
1130 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1131 return -EFAULT;
1132
1133 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1134 if (ctrl.id == s->qctrl[i].id) {
1135 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1136 return -EINVAL;
1137 if (ctrl.value < s->qctrl[i].minimum ||
1138 ctrl.value > s->qctrl[i].maximum)
1139 return -ERANGE;
1140 ctrl.value -= ctrl.value % s->qctrl[i].step;
1141 break;
1142 }
1143
1144 if ((err = s->set_ctrl(cam, &ctrl)))
1145 return err;
1146
1147 s->_qctrl[i].default_value = ctrl.value;
1148
1149 return 0;
1150}
1151
1152
1153static int
1154zc0301_vidioc_cropcap(struct zc0301_device* cam, void __user * arg)
1155{
1156 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1157
1158 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1159 cc->pixelaspect.numerator = 1;
1160 cc->pixelaspect.denominator = 1;
1161
1162 if (copy_to_user(arg, cc, sizeof(*cc)))
1163 return -EFAULT;
1164
1165 return 0;
1166}
1167
1168
1169static int
1170zc0301_vidioc_g_crop(struct zc0301_device* cam, void __user * arg)
1171{
1172 struct zc0301_sensor* s = &cam->sensor;
1173 struct v4l2_crop crop = {
1174 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1175 };
1176
1177 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1178
1179 if (copy_to_user(arg, &crop, sizeof(crop)))
1180 return -EFAULT;
1181
1182 return 0;
1183}
1184
1185
1186static int
1187zc0301_vidioc_s_crop(struct zc0301_device* cam, void __user * arg)
1188{
1189 struct zc0301_sensor* s = &cam->sensor;
1190 struct v4l2_crop crop;
1191 struct v4l2_rect* rect;
1192 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1193 const enum zc0301_stream_state stream = cam->stream;
1194 const u32 nbuffers = cam->nbuffers;
1195 u32 i;
1196 int err = 0;
1197
1198 if (copy_from_user(&crop, arg, sizeof(crop)))
1199 return -EFAULT;
1200
1201 rect = &(crop.c);
1202
1203 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1204 return -EINVAL;
1205
1206 if (cam->module_param.force_munmap)
1207 for (i = 0; i < cam->nbuffers; i++)
1208 if (cam->frame[i].vma_use_count) {
1209 DBG(3, "VIDIOC_S_CROP failed. "
1210 "Unmap the buffers first.");
1211 return -EINVAL;
1212 }
1213
1214 if (!s->set_crop) {
1215 memcpy(rect, &(s->_rect), sizeof(*rect));
1216 if (copy_to_user(arg, &crop, sizeof(crop)))
1217 return -EFAULT;
1218 return 0;
1219 }
1220
1221 rect->left &= ~7L;
1222 rect->top &= ~7L;
1223 if (rect->width < 8)
1224 rect->width = 8;
1225 if (rect->height < 8)
1226 rect->height = 8;
1227 if (rect->width > bounds->width)
1228 rect->width = bounds->width;
1229 if (rect->height > bounds->height)
1230 rect->height = bounds->height;
1231 if (rect->left < bounds->left)
1232 rect->left = bounds->left;
1233 if (rect->top < bounds->top)
1234 rect->top = bounds->top;
1235 if (rect->left + rect->width > bounds->left + bounds->width)
1236 rect->left = bounds->left+bounds->width - rect->width;
1237 if (rect->top + rect->height > bounds->top + bounds->height)
1238 rect->top = bounds->top+bounds->height - rect->height;
1239 rect->width &= ~7L;
1240 rect->height &= ~7L;
1241
1242 if (cam->stream == STREAM_ON)
1243 if ((err = zc0301_stream_interrupt(cam)))
1244 return err;
1245
1246 if (copy_to_user(arg, &crop, sizeof(crop))) {
1247 cam->stream = stream;
1248 return -EFAULT;
1249 }
1250
1251 if (cam->module_param.force_munmap || cam->io == IO_READ)
1252 zc0301_release_buffers(cam);
1253
1254 if (s->set_crop)
1255 err += s->set_crop(cam, rect);
1256
1257 if (err) { /* atomic, no rollback in ioctl() */
1258 cam->state |= DEV_MISCONFIGURED;
1259 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1260 "use the camera, close and open /dev/video%d again.",
1261 cam->v4ldev->minor);
1262 return -EIO;
1263 }
1264
1265 s->pix_format.width = rect->width;
1266 s->pix_format.height = rect->height;
1267 memcpy(&(s->_rect), rect, sizeof(*rect));
1268
1269 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1270 nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) {
1271 cam->state |= DEV_MISCONFIGURED;
1272 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1273 "use the camera, close and open /dev/video%d again.",
1274 cam->v4ldev->minor);
1275 return -ENOMEM;
1276 }
1277
1278 if (cam->io == IO_READ)
1279 zc0301_empty_framequeues(cam);
1280 else if (cam->module_param.force_munmap)
1281 zc0301_requeue_outqueue(cam);
1282
1283 cam->stream = stream;
1284
1285 return 0;
1286}
1287
1288
1289static int
1290zc0301_vidioc_enum_fmt(struct zc0301_device* cam, void __user * arg)
1291{
1292 struct v4l2_fmtdesc fmtd;
1293
1294 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1295 return -EFAULT;
1296
1297 if (fmtd.index == 0) {
1298 strcpy(fmtd.description, "JPEG");
1299 fmtd.pixelformat = V4L2_PIX_FMT_JPEG;
1300 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1301 } else
1302 return -EINVAL;
1303
1304 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1305 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1306
1307 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1308 return -EFAULT;
1309
1310 return 0;
1311}
1312
1313
1314static int
1315zc0301_vidioc_g_fmt(struct zc0301_device* cam, void __user * arg)
1316{
1317 struct v4l2_format format;
1318 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1319
1320 if (copy_from_user(&format, arg, sizeof(format)))
1321 return -EFAULT;
1322
1323 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1324 return -EINVAL;
1325
1326 pfmt->bytesperline = 0;
1327 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1328 pfmt->field = V4L2_FIELD_NONE;
1329 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1330
1331 if (copy_to_user(arg, &format, sizeof(format)))
1332 return -EFAULT;
1333
1334 return 0;
1335}
1336
1337
1338static int
1339zc0301_vidioc_try_s_fmt(struct zc0301_device* cam, unsigned int cmd,
1340 void __user * arg)
1341{
1342 struct zc0301_sensor* s = &cam->sensor;
1343 struct v4l2_format format;
1344 struct v4l2_pix_format* pix;
1345 struct v4l2_pix_format* pfmt = &(s->pix_format);
1346 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1347 struct v4l2_rect rect;
1348 const enum zc0301_stream_state stream = cam->stream;
1349 const u32 nbuffers = cam->nbuffers;
1350 u32 i;
1351 int err = 0;
1352
1353 if (copy_from_user(&format, arg, sizeof(format)))
1354 return -EFAULT;
1355
1356 pix = &(format.fmt.pix);
1357
1358 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1359 return -EINVAL;
1360
1361 memcpy(&rect, &(s->_rect), sizeof(rect));
1362
1363 if (!s->set_crop) {
1364 pix->width = rect.width;
1365 pix->height = rect.height;
1366 } else {
1367 rect.width = pix->width;
1368 rect.height = pix->height;
1369 }
1370
1371 if (rect.width < 8)
1372 rect.width = 8;
1373 if (rect.height < 8)
1374 rect.height = 8;
1375 if (rect.width > bounds->left + bounds->width - rect.left)
1376 rect.width = bounds->left + bounds->width - rect.left;
1377 if (rect.height > bounds->top + bounds->height - rect.top)
1378 rect.height = bounds->top + bounds->height - rect.top;
1379 rect.width &= ~7L;
1380 rect.height &= ~7L;
1381
1382 pix->width = rect.width;
1383 pix->height = rect.height;
1384 pix->pixelformat = pfmt->pixelformat;
1385 pix->priv = pfmt->priv;
1386 pix->colorspace = pfmt->colorspace;
1387 pix->bytesperline = 0;
1388 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
1389 pix->field = V4L2_FIELD_NONE;
1390
1391 if (cmd == VIDIOC_TRY_FMT) {
1392 if (copy_to_user(arg, &format, sizeof(format)))
1393 return -EFAULT;
1394 return 0;
1395 }
1396
1397 if (cam->module_param.force_munmap)
1398 for (i = 0; i < cam->nbuffers; i++)
1399 if (cam->frame[i].vma_use_count) {
1400 DBG(3, "VIDIOC_S_FMT failed. "
1401 "Unmap the buffers first.");
1402 return -EINVAL;
1403 }
1404
1405 if (cam->stream == STREAM_ON)
1406 if ((err = zc0301_stream_interrupt(cam)))
1407 return err;
1408
1409 if (copy_to_user(arg, &format, sizeof(format))) {
1410 cam->stream = stream;
1411 return -EFAULT;
1412 }
1413
1414 if (cam->module_param.force_munmap || cam->io == IO_READ)
1415 zc0301_release_buffers(cam);
1416
1417 if (s->set_crop)
1418 err += s->set_crop(cam, &rect);
1419
1420 if (err) { /* atomic, no rollback in ioctl() */
1421 cam->state |= DEV_MISCONFIGURED;
1422 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
1423 "use the camera, close and open /dev/video%d again.",
1424 cam->v4ldev->minor);
1425 return -EIO;
1426 }
1427
1428 memcpy(pfmt, pix, sizeof(*pix));
1429 memcpy(&(s->_rect), &rect, sizeof(rect));
1430
1431 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1432 nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) {
1433 cam->state |= DEV_MISCONFIGURED;
1434 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
1435 "use the camera, close and open /dev/video%d again.",
1436 cam->v4ldev->minor);
1437 return -ENOMEM;
1438 }
1439
1440 if (cam->io == IO_READ)
1441 zc0301_empty_framequeues(cam);
1442 else if (cam->module_param.force_munmap)
1443 zc0301_requeue_outqueue(cam);
1444
1445 cam->stream = stream;
1446
1447 return 0;
1448}
1449
1450
1451static int
1452zc0301_vidioc_g_jpegcomp(struct zc0301_device* cam, void __user * arg)
1453{
1454 if (copy_to_user(arg, &cam->compression, sizeof(cam->compression)))
1455 return -EFAULT;
1456
1457 return 0;
1458}
1459
1460
1461static int
1462zc0301_vidioc_s_jpegcomp(struct zc0301_device* cam, void __user * arg)
1463{
1464 struct v4l2_jpegcompression jc;
1465 const enum zc0301_stream_state stream = cam->stream;
1466 int err = 0;
1467
1468 if (copy_from_user(&jc, arg, sizeof(jc)))
1469 return -EFAULT;
1470
1471 if (jc.quality != 0)
1472 return -EINVAL;
1473
1474 if (cam->stream == STREAM_ON)
1475 if ((err = zc0301_stream_interrupt(cam)))
1476 return err;
1477
1478 err += zc0301_set_compression(cam, &jc);
1479 if (err) { /* atomic, no rollback in ioctl() */
1480 cam->state |= DEV_MISCONFIGURED;
1481 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
1482 "problems. To use the camera, close and open "
1483 "/dev/video%d again.", cam->v4ldev->minor);
1484 return -EIO;
1485 }
1486
1487 cam->compression.quality = jc.quality;
1488
1489 cam->stream = stream;
1490
1491 return 0;
1492}
1493
1494
1495static int
1496zc0301_vidioc_reqbufs(struct zc0301_device* cam, void __user * arg)
1497{
1498 struct v4l2_requestbuffers rb;
1499 u32 i;
1500 int err;
1501
1502 if (copy_from_user(&rb, arg, sizeof(rb)))
1503 return -EFAULT;
1504
1505 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1506 rb.memory != V4L2_MEMORY_MMAP)
1507 return -EINVAL;
1508
1509 if (cam->io == IO_READ) {
1510 DBG(3, "Close and open the device again to choose the mmap "
1511 "I/O method");
1512 return -EINVAL;
1513 }
1514
1515 for (i = 0; i < cam->nbuffers; i++)
1516 if (cam->frame[i].vma_use_count) {
1517 DBG(3, "VIDIOC_REQBUFS failed. "
1518 "Previous buffers are still mapped.");
1519 return -EINVAL;
1520 }
1521
1522 if (cam->stream == STREAM_ON)
1523 if ((err = zc0301_stream_interrupt(cam)))
1524 return err;
1525
1526 zc0301_empty_framequeues(cam);
1527
1528 zc0301_release_buffers(cam);
1529 if (rb.count)
1530 rb.count = zc0301_request_buffers(cam, rb.count, IO_MMAP);
1531
1532 if (copy_to_user(arg, &rb, sizeof(rb))) {
1533 zc0301_release_buffers(cam);
1534 cam->io = IO_NONE;
1535 return -EFAULT;
1536 }
1537
1538 cam->io = rb.count ? IO_MMAP : IO_NONE;
1539
1540 return 0;
1541}
1542
1543
1544static int
1545zc0301_vidioc_querybuf(struct zc0301_device* cam, void __user * arg)
1546{
1547 struct v4l2_buffer b;
1548
1549 if (copy_from_user(&b, arg, sizeof(b)))
1550 return -EFAULT;
1551
1552 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1553 b.index >= cam->nbuffers || cam->io != IO_MMAP)
1554 return -EINVAL;
1555
1556 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
1557
1558 if (cam->frame[b.index].vma_use_count)
1559 b.flags |= V4L2_BUF_FLAG_MAPPED;
1560
1561 if (cam->frame[b.index].state == F_DONE)
1562 b.flags |= V4L2_BUF_FLAG_DONE;
1563 else if (cam->frame[b.index].state != F_UNUSED)
1564 b.flags |= V4L2_BUF_FLAG_QUEUED;
1565
1566 if (copy_to_user(arg, &b, sizeof(b)))
1567 return -EFAULT;
1568
1569 return 0;
1570}
1571
1572
1573static int
1574zc0301_vidioc_qbuf(struct zc0301_device* cam, void __user * arg)
1575{
1576 struct v4l2_buffer b;
1577 unsigned long lock_flags;
1578
1579 if (copy_from_user(&b, arg, sizeof(b)))
1580 return -EFAULT;
1581
1582 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1583 b.index >= cam->nbuffers || cam->io != IO_MMAP)
1584 return -EINVAL;
1585
1586 if (cam->frame[b.index].state != F_UNUSED)
1587 return -EINVAL;
1588
1589 cam->frame[b.index].state = F_QUEUED;
1590
1591 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1592 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
1593 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1594
1595 PDBGG("Frame #%lu queued", (unsigned long)b.index);
1596
1597 return 0;
1598}
1599
1600
1601static int
1602zc0301_vidioc_dqbuf(struct zc0301_device* cam, struct file* filp,
1603 void __user * arg)
1604{
1605 struct v4l2_buffer b;
1606 struct zc0301_frame_t *f;
1607 unsigned long lock_flags;
1608 long timeout;
1609
1610 if (copy_from_user(&b, arg, sizeof(b)))
1611 return -EFAULT;
1612
1613 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
1614 return -EINVAL;
1615
1616 if (list_empty(&cam->outqueue)) {
1617 if (cam->stream == STREAM_OFF)
1618 return -EINVAL;
1619 if (filp->f_flags & O_NONBLOCK)
1620 return -EAGAIN;
1621 timeout = wait_event_interruptible_timeout
1622 ( cam->wait_frame,
1623 (!list_empty(&cam->outqueue)) ||
1624 (cam->state & DEV_DISCONNECTED) ||
1625 (cam->state & DEV_MISCONFIGURED),
1626 cam->module_param.frame_timeout *
1627 1000 * msecs_to_jiffies(1) );
1628 if (timeout < 0)
1629 return timeout;
1630 if (cam->state & DEV_DISCONNECTED)
1631 return -ENODEV;
1632 if (!timeout || (cam->state & DEV_MISCONFIGURED))
1633 return -EIO;
1634 }
1635
1636 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1637 f = list_entry(cam->outqueue.next, struct zc0301_frame_t, frame);
1638 list_del(cam->outqueue.next);
1639 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1640
1641 f->state = F_UNUSED;
1642
1643 memcpy(&b, &f->buf, sizeof(b));
1644 if (f->vma_use_count)
1645 b.flags |= V4L2_BUF_FLAG_MAPPED;
1646
1647 if (copy_to_user(arg, &b, sizeof(b)))
1648 return -EFAULT;
1649
1650 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
1651
1652 return 0;
1653}
1654
1655
1656static int
1657zc0301_vidioc_streamon(struct zc0301_device* cam, void __user * arg)
1658{
1659 int type;
1660
1661 if (copy_from_user(&type, arg, sizeof(type)))
1662 return -EFAULT;
1663
1664 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
1665 return -EINVAL;
1666
1667 if (list_empty(&cam->inqueue))
1668 return -EINVAL;
1669
1670 cam->stream = STREAM_ON;
1671
1672 DBG(3, "Stream on");
1673
1674 return 0;
1675}
1676
1677
1678static int
1679zc0301_vidioc_streamoff(struct zc0301_device* cam, void __user * arg)
1680{
1681 int type, err;
1682
1683 if (copy_from_user(&type, arg, sizeof(type)))
1684 return -EFAULT;
1685
1686 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
1687 return -EINVAL;
1688
1689 if (cam->stream == STREAM_ON)
1690 if ((err = zc0301_stream_interrupt(cam)))
1691 return err;
1692
1693 zc0301_empty_framequeues(cam);
1694
1695 DBG(3, "Stream off");
1696
1697 return 0;
1698}
1699
1700
1701static int
1702zc0301_vidioc_g_parm(struct zc0301_device* cam, void __user * arg)
1703{
1704 struct v4l2_streamparm sp;
1705
1706 if (copy_from_user(&sp, arg, sizeof(sp)))
1707 return -EFAULT;
1708
1709 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1710 return -EINVAL;
1711
1712 sp.parm.capture.extendedmode = 0;
1713 sp.parm.capture.readbuffers = cam->nreadbuffers;
1714
1715 if (copy_to_user(arg, &sp, sizeof(sp)))
1716 return -EFAULT;
1717
1718 return 0;
1719}
1720
1721
1722static int
1723zc0301_vidioc_s_parm(struct zc0301_device* cam, void __user * arg)
1724{
1725 struct v4l2_streamparm sp;
1726
1727 if (copy_from_user(&sp, arg, sizeof(sp)))
1728 return -EFAULT;
1729
1730 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1731 return -EINVAL;
1732
1733 sp.parm.capture.extendedmode = 0;
1734
1735 if (sp.parm.capture.readbuffers == 0)
1736 sp.parm.capture.readbuffers = cam->nreadbuffers;
1737
1738 if (sp.parm.capture.readbuffers > ZC0301_MAX_FRAMES)
1739 sp.parm.capture.readbuffers = ZC0301_MAX_FRAMES;
1740
1741 if (copy_to_user(arg, &sp, sizeof(sp)))
1742 return -EFAULT;
1743
1744 cam->nreadbuffers = sp.parm.capture.readbuffers;
1745
1746 return 0;
1747}
1748
1749
1750static int zc0301_ioctl_v4l2(struct inode* inode, struct file* filp,
1751 unsigned int cmd, void __user * arg)
1752{
1753 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
1754
1755 switch (cmd) {
1756
1757 case VIDIOC_QUERYCAP:
1758 return zc0301_vidioc_querycap(cam, arg);
1759
1760 case VIDIOC_ENUMINPUT:
1761 return zc0301_vidioc_enuminput(cam, arg);
1762
1763 case VIDIOC_G_INPUT:
1764 return zc0301_vidioc_g_input(cam, arg);
1765
1766 case VIDIOC_S_INPUT:
1767 return zc0301_vidioc_s_input(cam, arg);
1768
1769 case VIDIOC_QUERYCTRL:
1770 return zc0301_vidioc_query_ctrl(cam, arg);
1771
1772 case VIDIOC_G_CTRL:
1773 return zc0301_vidioc_g_ctrl(cam, arg);
1774
1775 case VIDIOC_S_CTRL_OLD:
1776 case VIDIOC_S_CTRL:
1777 return zc0301_vidioc_s_ctrl(cam, arg);
1778
1779 case VIDIOC_CROPCAP_OLD:
1780 case VIDIOC_CROPCAP:
1781 return zc0301_vidioc_cropcap(cam, arg);
1782
1783 case VIDIOC_G_CROP:
1784 return zc0301_vidioc_g_crop(cam, arg);
1785
1786 case VIDIOC_S_CROP:
1787 return zc0301_vidioc_s_crop(cam, arg);
1788
1789 case VIDIOC_ENUM_FMT:
1790 return zc0301_vidioc_enum_fmt(cam, arg);
1791
1792 case VIDIOC_G_FMT:
1793 return zc0301_vidioc_g_fmt(cam, arg);
1794
1795 case VIDIOC_TRY_FMT:
1796 case VIDIOC_S_FMT:
1797 return zc0301_vidioc_try_s_fmt(cam, cmd, arg);
1798
1799 case VIDIOC_G_JPEGCOMP:
1800 return zc0301_vidioc_g_jpegcomp(cam, arg);
1801
1802 case VIDIOC_S_JPEGCOMP:
1803 return zc0301_vidioc_s_jpegcomp(cam, arg);
1804
1805 case VIDIOC_REQBUFS:
1806 return zc0301_vidioc_reqbufs(cam, arg);
1807
1808 case VIDIOC_QUERYBUF:
1809 return zc0301_vidioc_querybuf(cam, arg);
1810
1811 case VIDIOC_QBUF:
1812 return zc0301_vidioc_qbuf(cam, arg);
1813
1814 case VIDIOC_DQBUF:
1815 return zc0301_vidioc_dqbuf(cam, filp, arg);
1816
1817 case VIDIOC_STREAMON:
1818 return zc0301_vidioc_streamon(cam, arg);
1819
1820 case VIDIOC_STREAMOFF:
1821 return zc0301_vidioc_streamoff(cam, arg);
1822
1823 case VIDIOC_G_PARM:
1824 return zc0301_vidioc_g_parm(cam, arg);
1825
1826 case VIDIOC_S_PARM_OLD:
1827 case VIDIOC_S_PARM:
1828 return zc0301_vidioc_s_parm(cam, arg);
1829
1830 case VIDIOC_G_STD:
1831 case VIDIOC_S_STD:
1832 case VIDIOC_QUERYSTD:
1833 case VIDIOC_ENUMSTD:
1834 case VIDIOC_QUERYMENU:
1835 return -EINVAL;
1836
1837 default:
1838 return -EINVAL;
1839
1840 }
1841}
1842
1843
1844static int zc0301_ioctl(struct inode* inode, struct file* filp,
1845 unsigned int cmd, unsigned long arg)
1846{
1847 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
1848 int err = 0;
1849
1850 if (mutex_lock_interruptible(&cam->fileop_mutex))
1851 return -ERESTARTSYS;
1852
1853 if (cam->state & DEV_DISCONNECTED) {
1854 DBG(1, "Device not present");
1855 mutex_unlock(&cam->fileop_mutex);
1856 return -ENODEV;
1857 }
1858
1859 if (cam->state & DEV_MISCONFIGURED) {
1860 DBG(1, "The camera is misconfigured. Close and open it "
1861 "again.");
1862 mutex_unlock(&cam->fileop_mutex);
1863 return -EIO;
1864 }
1865
1866 V4LDBG(3, "zc0301", cmd);
1867
1868 err = zc0301_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
1869
1870 mutex_unlock(&cam->fileop_mutex);
1871
1872 return err;
1873}
1874
1875
1876static struct file_operations zc0301_fops = {
1877 .owner = THIS_MODULE,
1878 .open = zc0301_open,
1879 .release = zc0301_release,
1880 .ioctl = zc0301_ioctl,
1881 .read = zc0301_read,
1882 .poll = zc0301_poll,
1883 .mmap = zc0301_mmap,
1884 .llseek = no_llseek,
1885};
1886
1887/*****************************************************************************/
1888
1889static int
1890zc0301_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
1891{
1892 struct usb_device *udev = interface_to_usbdev(intf);
1893 struct zc0301_device* cam;
1894 static unsigned int dev_nr = 0;
1895 unsigned int i;
1896 int err = 0;
1897
1898 if (!(cam = kzalloc(sizeof(struct zc0301_device), GFP_KERNEL)))
1899 return -ENOMEM;
1900
1901 cam->usbdev = udev;
1902
1903 if (!(cam->control_buffer = kzalloc(4, GFP_KERNEL))) {
1904 DBG(1, "kmalloc() failed");
1905 err = -ENOMEM;
1906 goto fail;
1907 }
1908
1909 if (!(cam->v4ldev = video_device_alloc())) {
1910 DBG(1, "video_device_alloc() failed");
1911 err = -ENOMEM;
1912 goto fail;
1913 }
1914
1915 mutex_init(&cam->dev_mutex);
1916
1917 DBG(2, "ZC0301 Image Processor and Control Chip detected "
1918 "(vid/pid 0x%04X/0x%04X)",id->idVendor, id->idProduct);
1919
1920 for (i = 0; zc0301_sensor_table[i]; i++) {
1921 err = zc0301_sensor_table[i](cam);
1922 if (!err)
1923 break;
1924 }
1925
1926 if (!err)
1927 DBG(2, "%s image sensor detected", cam->sensor.name);
1928 else {
1929 DBG(1, "No supported image sensor detected");
1930 err = -ENODEV;
1931 goto fail;
1932 }
1933
1934 if (zc0301_init(cam)) {
1935 DBG(1, "Initialization failed. I will retry on open().");
1936 cam->state |= DEV_MISCONFIGURED;
1937 }
1938
1939 strcpy(cam->v4ldev->name, "ZC0301 PC Camera");
1940 cam->v4ldev->owner = THIS_MODULE;
1941 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
1942 cam->v4ldev->hardware = 0;
1943 cam->v4ldev->fops = &zc0301_fops;
1944 cam->v4ldev->minor = video_nr[dev_nr];
1945 cam->v4ldev->release = video_device_release;
1946 video_set_drvdata(cam->v4ldev, cam);
1947
1948 mutex_lock(&cam->dev_mutex);
1949
1950 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
1951 video_nr[dev_nr]);
1952 if (err) {
1953 DBG(1, "V4L2 device registration failed");
1954 if (err == -ENFILE && video_nr[dev_nr] == -1)
1955 DBG(1, "Free /dev/videoX node not found");
1956 video_nr[dev_nr] = -1;
1957 dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0;
1958 mutex_unlock(&cam->dev_mutex);
1959 goto fail;
1960 }
1961
1962 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
1963
1964 cam->module_param.force_munmap = force_munmap[dev_nr];
1965 cam->module_param.frame_timeout = frame_timeout[dev_nr];
1966
1967 dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0;
1968
1969 usb_set_intfdata(intf, cam);
1970
1971 mutex_unlock(&cam->dev_mutex);
1972
1973 return 0;
1974
1975fail:
1976 if (cam) {
1977 kfree(cam->control_buffer);
1978 if (cam->v4ldev)
1979 video_device_release(cam->v4ldev);
1980 kfree(cam);
1981 }
1982 return err;
1983}
1984
1985
1986static void zc0301_usb_disconnect(struct usb_interface* intf)
1987{
1988 struct zc0301_device* cam = usb_get_intfdata(intf);
1989
1990 if (!cam)
1991 return;
1992
1993 down_write(&zc0301_disconnect);
1994
1995 mutex_lock(&cam->dev_mutex);
1996
1997 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
1998
1999 wake_up_interruptible_all(&cam->open);
2000
2001 if (cam->users) {
2002 DBG(2, "Device /dev/video%d is open! Deregistration and "
2003 "memory deallocation are deferred on close.",
2004 cam->v4ldev->minor);
2005 cam->state |= DEV_MISCONFIGURED;
2006 zc0301_stop_transfer(cam);
2007 cam->state |= DEV_DISCONNECTED;
2008 wake_up_interruptible(&cam->wait_frame);
2009 wake_up(&cam->wait_stream);
2010 usb_get_dev(cam->usbdev);
2011 } else {
2012 cam->state |= DEV_DISCONNECTED;
2013 zc0301_release_resources(cam);
2014 }
2015
2016 mutex_unlock(&cam->dev_mutex);
2017
2018 if (!cam->users)
2019 kfree(cam);
2020
2021 up_write(&zc0301_disconnect);
2022}
2023
2024
2025static struct usb_driver zc0301_usb_driver = {
2026 .name = "zc0301",
2027 .id_table = zc0301_id_table,
2028 .probe = zc0301_usb_probe,
2029 .disconnect = zc0301_usb_disconnect,
2030};
2031
2032/*****************************************************************************/
2033
2034static int __init zc0301_module_init(void)
2035{
2036 int err = 0;
2037
2038 KDBG(2, ZC0301_MODULE_NAME " v" ZC0301_MODULE_VERSION);
2039 KDBG(3, ZC0301_MODULE_AUTHOR);
2040
2041 if ((err = usb_register(&zc0301_usb_driver)))
2042 KDBG(1, "usb_register() failed");
2043
2044 return err;
2045}
2046
2047
2048static void __exit zc0301_module_exit(void)
2049{
2050 usb_deregister(&zc0301_usb_driver);
2051}
2052
2053
2054module_init(zc0301_module_init);
2055module_exit(zc0301_module_exit);
diff --git a/drivers/usb/media/zc0301_pas202bcb.c b/drivers/usb/media/zc0301_pas202bcb.c
new file mode 100644
index 000000000000..9d282a22c15f
--- /dev/null
+++ b/drivers/usb/media/zc0301_pas202bcb.c
@@ -0,0 +1,361 @@
1/***************************************************************************
2 * Plug-in for PAS202BCB image sensor connected to the ZC030! Image *
3 * Processor and Control Chip *
4 * *
5 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * Initialization values of the ZC0301 have been taken from the SPCA5XX *
8 * driver maintained by Michel Xhaard <mxhaard@magic.fr> *
9 * *
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 *
13 * (at your option) any later version. *
14 * *
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. *
19 * *
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., 675 Mass Ave, Cambridge, MA 02139, USA. *
23 ***************************************************************************/
24
25/*
26 NOTE: Sensor controls are disabled for now, becouse changing them while
27 streaming sometimes results in out-of-sync video frames. We'll use
28 the default initialization, until we know how to stop and start video
29 in the chip. However, the image quality still looks good under various
30 light conditions.
31*/
32
33#include <linux/delay.h>
34#include "zc0301_sensor.h"
35
36
37static struct zc0301_sensor pas202bcb;
38
39
40static int pas202bcb_init(struct zc0301_device* cam)
41{
42 int err = 0;
43
44 err += zc0301_write_reg(cam, 0x0002, 0x00);
45 err += zc0301_write_reg(cam, 0x0003, 0x02);
46 err += zc0301_write_reg(cam, 0x0004, 0x80);
47 err += zc0301_write_reg(cam, 0x0005, 0x01);
48 err += zc0301_write_reg(cam, 0x0006, 0xE0);
49 err += zc0301_write_reg(cam, 0x0098, 0x00);
50 err += zc0301_write_reg(cam, 0x009A, 0x03);
51 err += zc0301_write_reg(cam, 0x011A, 0x00);
52 err += zc0301_write_reg(cam, 0x011C, 0x03);
53 err += zc0301_write_reg(cam, 0x009B, 0x01);
54 err += zc0301_write_reg(cam, 0x009C, 0xE6);
55 err += zc0301_write_reg(cam, 0x009D, 0x02);
56 err += zc0301_write_reg(cam, 0x009E, 0x86);
57
58 err += zc0301_i2c_write(cam, 0x02, 0x02);
59 err += zc0301_i2c_write(cam, 0x0A, 0x01);
60 err += zc0301_i2c_write(cam, 0x0B, 0x01);
61 err += zc0301_i2c_write(cam, 0x0D, 0x00);
62 err += zc0301_i2c_write(cam, 0x12, 0x05);
63 err += zc0301_i2c_write(cam, 0x13, 0x63);
64 err += zc0301_i2c_write(cam, 0x15, 0x70);
65
66 err += zc0301_write_reg(cam, 0x0101, 0xB7);
67 err += zc0301_write_reg(cam, 0x0100, 0x0D);
68 err += zc0301_write_reg(cam, 0x0189, 0x06);
69 err += zc0301_write_reg(cam, 0x01AD, 0x00);
70 err += zc0301_write_reg(cam, 0x01C5, 0x03);
71 err += zc0301_write_reg(cam, 0x01CB, 0x13);
72 err += zc0301_write_reg(cam, 0x0250, 0x08);
73 err += zc0301_write_reg(cam, 0x0301, 0x08);
74 err += zc0301_write_reg(cam, 0x018D, 0x70);
75 err += zc0301_write_reg(cam, 0x0008, 0x03);
76 err += zc0301_write_reg(cam, 0x01C6, 0x04);
77 err += zc0301_write_reg(cam, 0x01CB, 0x07);
78 err += zc0301_write_reg(cam, 0x0120, 0x11);
79 err += zc0301_write_reg(cam, 0x0121, 0x37);
80 err += zc0301_write_reg(cam, 0x0122, 0x58);
81 err += zc0301_write_reg(cam, 0x0123, 0x79);
82 err += zc0301_write_reg(cam, 0x0124, 0x91);
83 err += zc0301_write_reg(cam, 0x0125, 0xA6);
84 err += zc0301_write_reg(cam, 0x0126, 0xB8);
85 err += zc0301_write_reg(cam, 0x0127, 0xC7);
86 err += zc0301_write_reg(cam, 0x0128, 0xD3);
87 err += zc0301_write_reg(cam, 0x0129, 0xDE);
88 err += zc0301_write_reg(cam, 0x012A, 0xE6);
89 err += zc0301_write_reg(cam, 0x012B, 0xED);
90 err += zc0301_write_reg(cam, 0x012C, 0xF3);
91 err += zc0301_write_reg(cam, 0x012D, 0xF8);
92 err += zc0301_write_reg(cam, 0x012E, 0xFB);
93 err += zc0301_write_reg(cam, 0x012F, 0xFF);
94 err += zc0301_write_reg(cam, 0x0130, 0x26);
95 err += zc0301_write_reg(cam, 0x0131, 0x23);
96 err += zc0301_write_reg(cam, 0x0132, 0x20);
97 err += zc0301_write_reg(cam, 0x0133, 0x1C);
98 err += zc0301_write_reg(cam, 0x0134, 0x16);
99 err += zc0301_write_reg(cam, 0x0135, 0x13);
100 err += zc0301_write_reg(cam, 0x0136, 0x10);
101 err += zc0301_write_reg(cam, 0x0137, 0x0D);
102 err += zc0301_write_reg(cam, 0x0138, 0x0B);
103 err += zc0301_write_reg(cam, 0x0139, 0x09);
104 err += zc0301_write_reg(cam, 0x013A, 0x07);
105 err += zc0301_write_reg(cam, 0x013B, 0x06);
106 err += zc0301_write_reg(cam, 0x013C, 0x05);
107 err += zc0301_write_reg(cam, 0x013D, 0x04);
108 err += zc0301_write_reg(cam, 0x013E, 0x03);
109 err += zc0301_write_reg(cam, 0x013F, 0x02);
110 err += zc0301_write_reg(cam, 0x010A, 0x4C);
111 err += zc0301_write_reg(cam, 0x010B, 0xF5);
112 err += zc0301_write_reg(cam, 0x010C, 0xFF);
113 err += zc0301_write_reg(cam, 0x010D, 0xF9);
114 err += zc0301_write_reg(cam, 0x010E, 0x51);
115 err += zc0301_write_reg(cam, 0x010F, 0xF5);
116 err += zc0301_write_reg(cam, 0x0110, 0xFB);
117 err += zc0301_write_reg(cam, 0x0111, 0xED);
118 err += zc0301_write_reg(cam, 0x0112, 0x5F);
119 err += zc0301_write_reg(cam, 0x0180, 0x00);
120 err += zc0301_write_reg(cam, 0x0019, 0x00);
121 err += zc0301_write_reg(cam, 0x0087, 0x20);
122 err += zc0301_write_reg(cam, 0x0088, 0x21);
123
124 err += zc0301_i2c_write(cam, 0x20, 0x02);
125 err += zc0301_i2c_write(cam, 0x21, 0x1B);
126 err += zc0301_i2c_write(cam, 0x03, 0x44);
127 err += zc0301_i2c_write(cam, 0x0E, 0x01);
128 err += zc0301_i2c_write(cam, 0x0F, 0x00);
129
130 err += zc0301_write_reg(cam, 0x01A9, 0x14);
131 err += zc0301_write_reg(cam, 0x01AA, 0x24);
132 err += zc0301_write_reg(cam, 0x0190, 0x00);
133 err += zc0301_write_reg(cam, 0x0191, 0x02);
134 err += zc0301_write_reg(cam, 0x0192, 0x1B);
135 err += zc0301_write_reg(cam, 0x0195, 0x00);
136 err += zc0301_write_reg(cam, 0x0196, 0x00);
137 err += zc0301_write_reg(cam, 0x0197, 0x4D);
138 err += zc0301_write_reg(cam, 0x018C, 0x10);
139 err += zc0301_write_reg(cam, 0x018F, 0x20);
140 err += zc0301_write_reg(cam, 0x001D, 0x44);
141 err += zc0301_write_reg(cam, 0x001E, 0x6F);
142 err += zc0301_write_reg(cam, 0x001F, 0xAD);
143 err += zc0301_write_reg(cam, 0x0020, 0xEB);
144 err += zc0301_write_reg(cam, 0x0087, 0x0F);
145 err += zc0301_write_reg(cam, 0x0088, 0x0E);
146 err += zc0301_write_reg(cam, 0x0180, 0x40);
147 err += zc0301_write_reg(cam, 0x0192, 0x1B);
148 err += zc0301_write_reg(cam, 0x0191, 0x02);
149 err += zc0301_write_reg(cam, 0x0190, 0x00);
150 err += zc0301_write_reg(cam, 0x0116, 0x1D);
151 err += zc0301_write_reg(cam, 0x0117, 0x40);
152 err += zc0301_write_reg(cam, 0x0118, 0x99);
153 err += zc0301_write_reg(cam, 0x0180, 0x42);
154 err += zc0301_write_reg(cam, 0x0116, 0x1D);
155 err += zc0301_write_reg(cam, 0x0117, 0x40);
156 err += zc0301_write_reg(cam, 0x0118, 0x99);
157 err += zc0301_write_reg(cam, 0x0007, 0x00);
158
159 err += zc0301_i2c_write(cam, 0x11, 0x01);
160
161 msleep(100);
162
163 return err;
164}
165
166
167static int pas202bcb_get_ctrl(struct zc0301_device* cam,
168 struct v4l2_control* ctrl)
169{
170 switch (ctrl->id) {
171 case V4L2_CID_EXPOSURE:
172 {
173 int r1 = zc0301_i2c_read(cam, 0x04, 1),
174 r2 = zc0301_i2c_read(cam, 0x05, 1);
175 if (r1 < 0 || r2 < 0)
176 return -EIO;
177 ctrl->value = (r1 << 6) | (r2 & 0x3f);
178 }
179 return 0;
180 case V4L2_CID_RED_BALANCE:
181 if ((ctrl->value = zc0301_i2c_read(cam, 0x09, 1)) < 0)
182 return -EIO;
183 ctrl->value &= 0x0f;
184 return 0;
185 case V4L2_CID_BLUE_BALANCE:
186 if ((ctrl->value = zc0301_i2c_read(cam, 0x07, 1)) < 0)
187 return -EIO;
188 ctrl->value &= 0x0f;
189 return 0;
190 case V4L2_CID_GAIN:
191 if ((ctrl->value = zc0301_i2c_read(cam, 0x10, 1)) < 0)
192 return -EIO;
193 ctrl->value &= 0x1f;
194 return 0;
195 case ZC0301_V4L2_CID_GREEN_BALANCE:
196 if ((ctrl->value = zc0301_i2c_read(cam, 0x08, 1)) < 0)
197 return -EIO;
198 ctrl->value &= 0x0f;
199 return 0;
200 case ZC0301_V4L2_CID_DAC_MAGNITUDE:
201 if ((ctrl->value = zc0301_i2c_read(cam, 0x0c, 1)) < 0)
202 return -EIO;
203 return 0;
204 default:
205 return -EINVAL;
206 }
207}
208
209
210static int pas202bcb_set_ctrl(struct zc0301_device* cam,
211 const struct v4l2_control* ctrl)
212{
213 int err = 0;
214
215 switch (ctrl->id) {
216 case V4L2_CID_EXPOSURE:
217 err += zc0301_i2c_write(cam, 0x04, ctrl->value >> 6);
218 err += zc0301_i2c_write(cam, 0x05, ctrl->value & 0x3f);
219 break;
220 case V4L2_CID_RED_BALANCE:
221 err += zc0301_i2c_write(cam, 0x09, ctrl->value);
222 break;
223 case V4L2_CID_BLUE_BALANCE:
224 err += zc0301_i2c_write(cam, 0x07, ctrl->value);
225 break;
226 case V4L2_CID_GAIN:
227 err += zc0301_i2c_write(cam, 0x10, ctrl->value);
228 break;
229 case ZC0301_V4L2_CID_GREEN_BALANCE:
230 err += zc0301_i2c_write(cam, 0x08, ctrl->value);
231 break;
232 case ZC0301_V4L2_CID_DAC_MAGNITUDE:
233 err += zc0301_i2c_write(cam, 0x0c, ctrl->value);
234 break;
235 default:
236 return -EINVAL;
237 }
238 err += zc0301_i2c_write(cam, 0x11, 0x01);
239
240 return err ? -EIO : 0;
241}
242
243
244static struct zc0301_sensor pas202bcb = {
245 .name = "PAS202BCB",
246 .init = &pas202bcb_init,
247 .qctrl = {
248 {
249 .id = V4L2_CID_EXPOSURE,
250 .type = V4L2_CTRL_TYPE_INTEGER,
251 .name = "exposure",
252 .minimum = 0x01e5,
253 .maximum = 0x3fff,
254 .step = 0x0001,
255 .default_value = 0x01e5,
256 .flags = V4L2_CTRL_FLAG_DISABLED,
257 },
258 {
259 .id = V4L2_CID_GAIN,
260 .type = V4L2_CTRL_TYPE_INTEGER,
261 .name = "global gain",
262 .minimum = 0x00,
263 .maximum = 0x1f,
264 .step = 0x01,
265 .default_value = 0x0c,
266 .flags = V4L2_CTRL_FLAG_DISABLED,
267 },
268 {
269 .id = ZC0301_V4L2_CID_DAC_MAGNITUDE,
270 .type = V4L2_CTRL_TYPE_INTEGER,
271 .name = "DAC magnitude",
272 .minimum = 0x00,
273 .maximum = 0xff,
274 .step = 0x01,
275 .default_value = 0x00,
276 .flags = V4L2_CTRL_FLAG_DISABLED,
277 },
278 {
279 .id = V4L2_CID_RED_BALANCE,
280 .type = V4L2_CTRL_TYPE_INTEGER,
281 .name = "red balance",
282 .minimum = 0x00,
283 .maximum = 0x0f,
284 .step = 0x01,
285 .default_value = 0x01,
286 .flags = V4L2_CTRL_FLAG_DISABLED,
287 },
288 {
289 .id = V4L2_CID_BLUE_BALANCE,
290 .type = V4L2_CTRL_TYPE_INTEGER,
291 .name = "blue balance",
292 .minimum = 0x00,
293 .maximum = 0x0f,
294 .step = 0x01,
295 .default_value = 0x05,
296 .flags = V4L2_CTRL_FLAG_DISABLED,
297 },
298 {
299 .id = ZC0301_V4L2_CID_GREEN_BALANCE,
300 .type = V4L2_CTRL_TYPE_INTEGER,
301 .name = "green balance",
302 .minimum = 0x00,
303 .maximum = 0x0f,
304 .step = 0x01,
305 .default_value = 0x00,
306 .flags = V4L2_CTRL_FLAG_DISABLED,
307 },
308 },
309 .get_ctrl = &pas202bcb_get_ctrl,
310 .set_ctrl = &pas202bcb_set_ctrl,
311 .cropcap = {
312 .bounds = {
313 .left = 0,
314 .top = 0,
315 .width = 640,
316 .height = 480,
317 },
318 .defrect = {
319 .left = 0,
320 .top = 0,
321 .width = 640,
322 .height = 480,
323 },
324 },
325 .pix_format = {
326 .width = 640,
327 .height = 480,
328 .pixelformat = V4L2_PIX_FMT_JPEG,
329 .priv = 8,
330 },
331};
332
333
334int zc0301_probe_pas202bcb(struct zc0301_device* cam)
335{
336 int r0 = 0, r1 = 0, err = 0;
337 unsigned int pid = 0;
338
339 err += zc0301_write_reg(cam, 0x0000, 0x01);
340 err += zc0301_write_reg(cam, 0x0010, 0x0e);
341 err += zc0301_write_reg(cam, 0x0001, 0x01);
342 err += zc0301_write_reg(cam, 0x0012, 0x03);
343 err += zc0301_write_reg(cam, 0x0012, 0x01);
344 err += zc0301_write_reg(cam, 0x008d, 0x08);
345
346 msleep(10);
347
348 r0 = zc0301_i2c_read(cam, 0x00, 1);
349 r1 = zc0301_i2c_read(cam, 0x01, 1);
350
351 if (r0 < 0 || r1 < 0 || err)
352 return -EIO;
353
354 pid = (r0 << 4) | ((r1 & 0xf0) >> 4);
355 if (pid != 0x017)
356 return -ENODEV;
357
358 zc0301_attach_sensor(cam, &pas202bcb);
359
360 return 0;
361}
diff --git a/drivers/usb/media/zc0301_sensor.h b/drivers/usb/media/zc0301_sensor.h
new file mode 100644
index 000000000000..cf0965a81d01
--- /dev/null
+++ b/drivers/usb/media/zc0301_sensor.h
@@ -0,0 +1,103 @@
1/***************************************************************************
2 * API for image sensors connected to the ZC030! Image Processor and *
3 * Control Chip *
4 * *
5 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#ifndef _ZC0301_SENSOR_H_
23#define _ZC0301_SENSOR_H_
24
25#include <linux/usb.h>
26#include <linux/videodev.h>
27#include <linux/device.h>
28#include <linux/stddef.h>
29#include <linux/errno.h>
30#include <asm/types.h>
31
32struct zc0301_device;
33struct zc0301_sensor;
34
35/*****************************************************************************/
36
37extern int zc0301_probe_pas202bcb(struct zc0301_device* cam);
38
39#define ZC0301_SENSOR_TABLE \
40/* Weak detections must go at the end of the list */ \
41static int (*zc0301_sensor_table[])(struct zc0301_device*) = { \
42 &zc0301_probe_pas202bcb, \
43 NULL, \
44};
45
46extern struct zc0301_device*
47zc0301_match_id(struct zc0301_device* cam, const struct usb_device_id *id);
48
49extern void
50zc0301_attach_sensor(struct zc0301_device* cam, struct zc0301_sensor* sensor);
51
52#define ZC0301_USB_DEVICE(vend, prod, intclass) \
53 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
54 USB_DEVICE_ID_MATCH_INT_CLASS, \
55 .idVendor = (vend), \
56 .idProduct = (prod), \
57 .bInterfaceClass = (intclass)
58
59#define ZC0301_ID_TABLE \
60static const struct usb_device_id zc0301_id_table[] = { \
61 { ZC0301_USB_DEVICE(0x041e, 0x4017, 0xff), }, \
62 { ZC0301_USB_DEVICE(0x041e, 0x401c, 0xff), }, /* PAS106 */ \
63 { ZC0301_USB_DEVICE(0x041e, 0x401e, 0xff), }, /* HV7131B */ \
64 { ZC0301_USB_DEVICE(0x041e, 0x4034, 0xff), }, /* PAS106 */ \
65 { ZC0301_USB_DEVICE(0x041e, 0x4035, 0xff), }, /* PAS106 */ \
66 { ZC0301_USB_DEVICE(0x046d, 0x08ae, 0xff), }, /* PAS202BCB */ \
67 { ZC0301_USB_DEVICE(0x0ac8, 0x0301, 0xff), }, \
68 { ZC0301_USB_DEVICE(0x10fd, 0x8050, 0xff), }, /* TAS5130D */ \
69 { } \
70};
71
72/*****************************************************************************/
73
74extern int zc0301_write_reg(struct zc0301_device*, u16 index, u16 value);
75extern int zc0301_read_reg(struct zc0301_device*, u16 index);
76extern int zc0301_i2c_write(struct zc0301_device*, u16 address, u16 value);
77extern int zc0301_i2c_read(struct zc0301_device*, u16 address, u8 length);
78
79/*****************************************************************************/
80
81#define ZC0301_MAX_CTRLS V4L2_CID_LASTP1-V4L2_CID_BASE+10
82#define ZC0301_V4L2_CID_DAC_MAGNITUDE V4L2_CID_PRIVATE_BASE
83#define ZC0301_V4L2_CID_GREEN_BALANCE V4L2_CID_PRIVATE_BASE + 1
84
85struct zc0301_sensor {
86 char name[32];
87
88 struct v4l2_queryctrl qctrl[ZC0301_MAX_CTRLS];
89 struct v4l2_cropcap cropcap;
90 struct v4l2_pix_format pix_format;
91
92 int (*init)(struct zc0301_device*);
93 int (*get_ctrl)(struct zc0301_device*, struct v4l2_control* ctrl);
94 int (*set_ctrl)(struct zc0301_device*,
95 const struct v4l2_control* ctrl);
96 int (*set_crop)(struct zc0301_device*, const struct v4l2_rect* rect);
97
98 /* Private */
99 struct v4l2_queryctrl _qctrl[ZC0301_MAX_CTRLS];
100 struct v4l2_rect _rect;
101};
102
103#endif /* _ZC0301_SENSOR_H_ */