aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/zc0301
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@infradead.org>2006-03-25 07:05:39 -0500
committerMauro Carvalho Chehab <mchehab@infradead.org>2006-03-25 07:29:04 -0500
commit9f6933be665ce3b049c274c99810ac754edabf19 (patch)
tree70a670d030c5d5a4175076724e4720a5b967e2bc /drivers/media/video/zc0301
parent7fa033b103bc3f5c37f934695473f63adf140dba (diff)
V4L/DVB (3599a): Move drivers/usb/media to drivers/media/video
Because of historic reasons, there are two separate directories with V4L stuff. Most drivers are located at driver/media/video. However, some code for USB Webcams were inserted under drivers/usb/media. This makes difficult for module authors to know were things should be. Also, makes Kconfig menu confusing for normal users. This patch moves all V4L content under drivers/usb/media to drivers/media/video, and fixes Kconfig/Makefile entries. Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de> Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
Diffstat (limited to 'drivers/media/video/zc0301')
-rw-r--r--drivers/media/video/zc0301/Makefile3
-rw-r--r--drivers/media/video/zc0301/zc0301.h192
-rw-r--r--drivers/media/video/zc0301/zc0301_core.c2055
-rw-r--r--drivers/media/video/zc0301/zc0301_pas202bcb.c361
-rw-r--r--drivers/media/video/zc0301/zc0301_sensor.h103
5 files changed, 2714 insertions, 0 deletions
diff --git a/drivers/media/video/zc0301/Makefile b/drivers/media/video/zc0301/Makefile
new file mode 100644
index 000000000000..d749199d8f06
--- /dev/null
+++ b/drivers/media/video/zc0301/Makefile
@@ -0,0 +1,3 @@
1zc0301-objs := zc0301_core.o zc0301_pas202bcb.o
2
3obj-$(CONFIG_USB_ZC0301) += zc0301.o
diff --git a/drivers/media/video/zc0301/zc0301.h b/drivers/media/video/zc0301/zc0301.h
new file mode 100644
index 000000000000..8e0655140e60
--- /dev/null
+++ b/drivers/media/video/zc0301/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/media/video/zc0301/zc0301_core.c b/drivers/media/video/zc0301/zc0301_core.c
new file mode 100644
index 000000000000..4036c6268bff
--- /dev/null
+++ b/drivers/media/video/zc0301/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/media/video/zc0301/zc0301_pas202bcb.c b/drivers/media/video/zc0301/zc0301_pas202bcb.c
new file mode 100644
index 000000000000..9d282a22c15f
--- /dev/null
+++ b/drivers/media/video/zc0301/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/media/video/zc0301/zc0301_sensor.h b/drivers/media/video/zc0301/zc0301_sensor.h
new file mode 100644
index 000000000000..cf0965a81d01
--- /dev/null
+++ b/drivers/media/video/zc0301/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_ */