[PATCH] I2C: i2c-algo-pca -- gracefully handle a busy bus
[linux-2.6/next.git] / drivers / usb / media / sn9c102_core.c
blobcf8cfbabefde8da01e8be66e0fe10434c224f1a7
1 /***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers *
3 * *
4 * Copyright (C) 2004-2005 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 ***************************************************************************/
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/param.h>
25 #include <linux/moduleparam.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
29 #include <linux/device.h>
30 #include <linux/fs.h>
31 #include <linux/delay.h>
32 #include <linux/stddef.h>
33 #include <linux/compiler.h>
34 #include <linux/ioctl.h>
35 #include <linux/poll.h>
36 #include <linux/stat.h>
37 #include <linux/mm.h>
38 #include <linux/vmalloc.h>
39 #include <linux/page-flags.h>
40 #include <linux/byteorder/generic.h>
41 #include <asm/page.h>
42 #include <asm/uaccess.h>
44 #include "sn9c102.h"
46 /*****************************************************************************/
48 MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
50 MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
51 MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
52 MODULE_VERSION(SN9C102_MODULE_VERSION);
53 MODULE_LICENSE(SN9C102_MODULE_LICENSE);
55 static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
56 module_param_array(video_nr, short, NULL, 0444);
57 MODULE_PARM_DESC(video_nr,
58 "\n<-1|n[,...]> Specify V4L2 minor mode number."
59 "\n -1 = use next available (default)"
60 "\n n = use minor number n (integer >= 0)"
61 "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
62 " cameras this way."
63 "\nFor example:"
64 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
65 "\nthe second camera and use auto for the first"
66 "\none and for every other camera."
67 "\n");
69 static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
70 SN9C102_FORCE_MUNMAP};
71 module_param_array(force_munmap, bool, NULL, 0444);
72 MODULE_PARM_DESC(force_munmap,
73 "\n<0|1[,...]> Force the application to unmap previously "
74 "\nmapped buffer memory before calling any VIDIOC_S_CROP or "
75 "\nVIDIOC_S_FMT ioctl's. Not all the applications support "
76 "\nthis feature. This parameter is specific for each "
77 "\ndetected camera."
78 "\n 0 = do not force memory unmapping"
79 "\n 1 = force memory unmapping (save memory)"
80 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
81 "\n");
83 #ifdef SN9C102_DEBUG
84 static unsigned short debug = SN9C102_DEBUG_LEVEL;
85 module_param(debug, ushort, 0644);
86 MODULE_PARM_DESC(debug,
87 "\n<n> Debugging information level, from 0 to 3:"
88 "\n0 = none (use carefully)"
89 "\n1 = critical errors"
90 "\n2 = significant informations"
91 "\n3 = more verbose messages"
92 "\nLevel 3 is useful for testing only, when only "
93 "one device is used."
94 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
95 "\n");
96 #endif
98 /*****************************************************************************/
100 static sn9c102_sof_header_t sn9c102_sof_header[] = {
101 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x00},
102 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01},
106 static sn9c102_eof_header_t sn9c102_eof_header[] = {
107 {0x00, 0x00, 0x00, 0x00},
108 {0x40, 0x00, 0x00, 0x00},
109 {0x80, 0x00, 0x00, 0x00},
110 {0xc0, 0x00, 0x00, 0x00},
113 /*****************************************************************************/
115 static void* rvmalloc(size_t size)
117 void* mem;
118 unsigned long adr;
120 size = PAGE_ALIGN(size);
122 mem = vmalloc_32((unsigned long)size);
123 if (!mem)
124 return NULL;
126 memset(mem, 0, size);
128 adr = (unsigned long)mem;
129 while (size > 0) {
130 SetPageReserved(vmalloc_to_page((void *)adr));
131 adr += PAGE_SIZE;
132 size -= PAGE_SIZE;
135 return mem;
139 static void rvfree(void* mem, size_t size)
141 unsigned long adr;
143 if (!mem)
144 return;
146 size = PAGE_ALIGN(size);
148 adr = (unsigned long)mem;
149 while (size > 0) {
150 ClearPageReserved(vmalloc_to_page((void *)adr));
151 adr += PAGE_SIZE;
152 size -= PAGE_SIZE;
155 vfree(mem);
159 static u32
160 sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
161 enum sn9c102_io_method io)
163 struct v4l2_pix_format* p = &(cam->sensor->pix_format);
164 struct v4l2_rect* r = &(cam->sensor->cropcap.bounds);
165 const size_t imagesize = cam->module_param.force_munmap ||
166 io == IO_READ ?
167 (p->width * p->height * p->priv) / 8 :
168 (r->width * r->height * p->priv) / 8;
169 void* buff = NULL;
170 u32 i;
172 if (count > SN9C102_MAX_FRAMES)
173 count = SN9C102_MAX_FRAMES;
175 cam->nbuffers = count;
176 while (cam->nbuffers > 0) {
177 if ((buff = rvmalloc(cam->nbuffers * PAGE_ALIGN(imagesize))))
178 break;
179 cam->nbuffers--;
182 for (i = 0; i < cam->nbuffers; i++) {
183 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
184 cam->frame[i].buf.index = i;
185 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
186 cam->frame[i].buf.length = imagesize;
187 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
188 cam->frame[i].buf.sequence = 0;
189 cam->frame[i].buf.field = V4L2_FIELD_NONE;
190 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
191 cam->frame[i].buf.flags = 0;
194 return cam->nbuffers;
198 static void sn9c102_release_buffers(struct sn9c102_device* cam)
200 if (cam->nbuffers) {
201 rvfree(cam->frame[0].bufmem,
202 cam->nbuffers * cam->frame[0].buf.length);
203 cam->nbuffers = 0;
208 static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
210 u32 i;
212 INIT_LIST_HEAD(&cam->inqueue);
213 INIT_LIST_HEAD(&cam->outqueue);
215 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
216 cam->frame[i].state = F_UNUSED;
217 cam->frame[i].buf.bytesused = 0;
222 static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
224 unsigned long lock_flags;
225 u32 i;
227 for (i = 0; i < cam->nbuffers; i++)
228 if (cam->frame[i].state == F_UNUSED) {
229 cam->frame[i].state = F_QUEUED;
230 spin_lock_irqsave(&cam->queue_lock, lock_flags);
231 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
232 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
236 /*****************************************************************************/
238 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
240 struct usb_device* udev = cam->usbdev;
241 u8* buff = cam->control_buffer;
242 int res;
244 *buff = value;
246 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
247 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
248 if (res < 0) {
249 DBG(3, "Failed to write a register (value 0x%02X, index "
250 "0x%02X, error %d)", value, index, res)
251 return -1;
254 cam->reg[index] = value;
256 return 0;
260 /* NOTE: reading some registers always returns 0 */
261 static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
263 struct usb_device* udev = cam->usbdev;
264 u8* buff = cam->control_buffer;
265 int res;
267 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
268 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
269 if (res < 0)
270 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
271 index, res)
273 return (res >= 0) ? (int)(*buff) : -1;
277 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
279 if (index > 0x1f)
280 return -EINVAL;
282 return cam->reg[index];
286 static int
287 sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
289 int i, r;
291 for (i = 1; i <= 5; i++) {
292 r = sn9c102_read_reg(cam, 0x08);
293 if (r < 0)
294 return -EIO;
295 if (r & 0x04)
296 return 0;
297 if (sensor->frequency & SN9C102_I2C_400KHZ)
298 udelay(5*16);
299 else
300 udelay(16*16);
302 return -EBUSY;
306 static int
307 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
308 struct sn9c102_sensor* sensor)
310 int r;
311 r = sn9c102_read_reg(cam, 0x08);
312 return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0;
316 static int
317 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
318 struct sn9c102_sensor* sensor)
320 int r;
321 r = sn9c102_read_reg(cam, 0x08);
322 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
326 int
327 sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
328 struct sn9c102_sensor* sensor, u8 data0, u8 data1,
329 u8 n, u8 buffer[])
331 struct usb_device* udev = cam->usbdev;
332 u8* data = cam->control_buffer;
333 int err = 0, res;
335 /* Write cycle */
336 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
337 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
338 data[1] = data0; /* I2C slave id */
339 data[2] = data1; /* address */
340 data[7] = 0x10;
341 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
342 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
343 if (res < 0)
344 err += res;
346 err += sn9c102_i2c_wait(cam, sensor);
348 /* Read cycle - n bytes */
349 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
350 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
351 (n << 4) | 0x02;
352 data[1] = data0;
353 data[7] = 0x10;
354 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
355 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
356 if (res < 0)
357 err += res;
359 err += sn9c102_i2c_wait(cam, sensor);
361 /* The first read byte will be placed in data[4] */
362 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
363 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
364 if (res < 0)
365 err += res;
367 err += sn9c102_i2c_detect_read_error(cam, sensor);
369 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
370 data[4])
372 if (err) {
373 DBG(3, "I2C read failed for %s image sensor", sensor->name)
374 return -1;
377 if (buffer)
378 memcpy(buffer, data, sizeof(buffer));
380 return (int)data[4];
384 int
385 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
386 struct sn9c102_sensor* sensor, u8 n, u8 data0,
387 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
389 struct usb_device* udev = cam->usbdev;
390 u8* data = cam->control_buffer;
391 int err = 0, res;
393 /* Write cycle. It usually is address + value */
394 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
395 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
396 | ((n - 1) << 4);
397 data[1] = data0;
398 data[2] = data1;
399 data[3] = data2;
400 data[4] = data3;
401 data[5] = data4;
402 data[6] = data5;
403 data[7] = 0x14;
404 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
405 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
406 if (res < 0)
407 err += res;
409 err += sn9c102_i2c_wait(cam, sensor);
410 err += sn9c102_i2c_detect_write_error(cam, sensor);
412 if (err)
413 DBG(3, "I2C write failed for %s image sensor", sensor->name)
415 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
416 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
417 n, data0, data1, data2, data3, data4, data5)
419 return err ? -1 : 0;
424 sn9c102_i2c_try_read(struct sn9c102_device* cam,
425 struct sn9c102_sensor* sensor, u8 address)
427 return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
428 address, 1, NULL);
433 sn9c102_i2c_try_write(struct sn9c102_device* cam,
434 struct sn9c102_sensor* sensor, u8 address, u8 value)
436 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
437 sensor->i2c_slave_id, address,
438 value, 0, 0, 0);
442 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
444 if (!cam->sensor)
445 return -1;
447 return sn9c102_i2c_try_read(cam, cam->sensor, address);
451 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
453 if (!cam->sensor)
454 return -1;
456 return sn9c102_i2c_try_write(cam, cam->sensor, address, value);
459 /*****************************************************************************/
461 static void*
462 sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
464 size_t soflen = sizeof(sn9c102_sof_header_t), i;
465 u8 j, n = sizeof(sn9c102_sof_header) / soflen;
467 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
468 for (j = 0; j < n; j++)
469 /* It's enough to compare 7 bytes */
470 if (!memcmp(mem + i, sn9c102_sof_header[j], 7)) {
471 memcpy(cam->sof_header, mem + i, soflen);
472 /* Skip the header */
473 return mem + i + soflen;
476 return NULL;
480 static void*
481 sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
483 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
484 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
486 if (cam->sensor->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
487 return NULL; /* EOF header does not exist in compressed data */
489 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
490 for (j = 0; j < n; j++)
491 if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen))
492 return mem + i;
494 return NULL;
498 static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
500 struct sn9c102_device* cam = urb->context;
501 struct sn9c102_frame_t** f;
502 size_t imagesize;
503 unsigned long lock_flags;
504 u8 i;
505 int err = 0;
507 if (urb->status == -ENOENT)
508 return;
510 f = &cam->frame_current;
512 if (cam->stream == STREAM_INTERRUPT) {
513 cam->stream = STREAM_OFF;
514 if ((*f))
515 (*f)->state = F_QUEUED;
516 DBG(3, "Stream interrupted")
517 wake_up_interruptible(&cam->wait_stream);
520 if (cam->state & DEV_DISCONNECTED)
521 return;
523 if (cam->state & DEV_MISCONFIGURED) {
524 wake_up_interruptible(&cam->wait_frame);
525 return;
528 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
529 goto resubmit_urb;
531 if (!(*f))
532 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
533 frame);
535 imagesize = (cam->sensor->pix_format.width *
536 cam->sensor->pix_format.height *
537 cam->sensor->pix_format.priv) / 8;
539 for (i = 0; i < urb->number_of_packets; i++) {
540 unsigned int img, len, status;
541 void *pos, *sof, *eof;
543 len = urb->iso_frame_desc[i].actual_length;
544 status = urb->iso_frame_desc[i].status;
545 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
547 if (status) {
548 DBG(3, "Error in isochronous frame")
549 (*f)->state = F_ERROR;
550 continue;
553 PDBGG("Isochrnous frame: length %u, #%u i", len, i)
556 NOTE: It is probably correct to assume that SOF and EOF
557 headers do not occur between two consecutive packets,
558 but who knows..Whatever is the truth, this assumption
559 doesn't introduce bugs.
562 redo:
563 sof = sn9c102_find_sof_header(cam, pos, len);
564 if (!sof) {
565 eof = sn9c102_find_eof_header(cam, pos, len);
566 if ((*f)->state == F_GRABBING) {
567 end_of_frame:
568 img = len;
570 if (eof)
571 img = (eof > pos) ? eof - pos - 1 : 0;
573 if ((*f)->buf.bytesused+img > imagesize) {
574 u32 b = (*f)->buf.bytesused + img -
575 imagesize;
576 img = imagesize - (*f)->buf.bytesused;
577 DBG(3, "Expected EOF not found: "
578 "video frame cut")
579 if (eof)
580 DBG(3, "Exceeded limit: +%u "
581 "bytes", (unsigned)(b))
584 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
585 img);
587 if ((*f)->buf.bytesused == 0)
588 do_gettimeofday(&(*f)->buf.timestamp);
590 (*f)->buf.bytesused += img;
592 if ((*f)->buf.bytesused == imagesize ||
593 (cam->sensor->pix_format.pixelformat ==
594 V4L2_PIX_FMT_SN9C10X && eof)) {
595 u32 b = (*f)->buf.bytesused;
596 (*f)->state = F_DONE;
597 (*f)->buf.sequence= ++cam->frame_count;
598 spin_lock_irqsave(&cam->queue_lock,
599 lock_flags);
600 list_move_tail(&(*f)->frame,
601 &cam->outqueue);
602 if (!list_empty(&cam->inqueue))
603 (*f) = list_entry(
604 cam->inqueue.next,
605 struct sn9c102_frame_t,
606 frame );
607 else
608 (*f) = NULL;
609 spin_unlock_irqrestore(&cam->queue_lock
610 , lock_flags);
611 memcpy(cam->sysfs.frame_header,
612 cam->sof_header,
613 sizeof(sn9c102_sof_header_t));
614 DBG(3, "Video frame captured: "
615 "%lu bytes", (unsigned long)(b))
617 if (!(*f))
618 goto resubmit_urb;
620 } else if (eof) {
621 (*f)->state = F_ERROR;
622 DBG(3, "Not expected EOF after %lu "
623 "bytes of image data",
624 (unsigned long)((*f)->buf.bytesused))
627 if (sof) /* (1) */
628 goto start_of_frame;
630 } else if (eof) {
631 DBG(3, "EOF without SOF")
632 continue;
634 } else {
635 PDBGG("Ignoring pointless isochronous frame")
636 continue;
639 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
640 start_of_frame:
641 (*f)->state = F_GRABBING;
642 (*f)->buf.bytesused = 0;
643 len -= (sof - pos);
644 pos = sof;
645 DBG(3, "SOF detected: new video frame")
646 if (len)
647 goto redo;
649 } else if ((*f)->state == F_GRABBING) {
650 eof = sn9c102_find_eof_header(cam, pos, len);
651 if (eof && eof < sof)
652 goto end_of_frame; /* (1) */
653 else {
654 if (cam->sensor->pix_format.pixelformat ==
655 V4L2_PIX_FMT_SN9C10X) {
656 eof = sof-sizeof(sn9c102_sof_header_t);
657 goto end_of_frame;
658 } else {
659 DBG(3, "SOF before expected EOF after "
660 "%lu bytes of image data",
661 (unsigned long)((*f)->buf.bytesused))
662 goto start_of_frame;
668 resubmit_urb:
669 urb->dev = cam->usbdev;
670 err = usb_submit_urb(urb, GFP_ATOMIC);
671 if (err < 0 && err != -EPERM) {
672 cam->state |= DEV_MISCONFIGURED;
673 DBG(1, "usb_submit_urb() failed")
676 wake_up_interruptible(&cam->wait_frame);
680 static int sn9c102_start_transfer(struct sn9c102_device* cam)
682 struct usb_device *udev = cam->usbdev;
683 struct urb* urb;
684 const unsigned int wMaxPacketSize[] = {0, 128, 256, 384, 512,
685 680, 800, 900, 1023};
686 const unsigned int psz = wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
687 s8 i, j;
688 int err = 0;
690 for (i = 0; i < SN9C102_URBS; i++) {
691 cam->transfer_buffer[i] = kmalloc(SN9C102_ISO_PACKETS * psz,
692 GFP_KERNEL);
693 if (!cam->transfer_buffer[i]) {
694 err = -ENOMEM;
695 DBG(1, "Not enough memory")
696 goto free_buffers;
700 for (i = 0; i < SN9C102_URBS; i++) {
701 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
702 cam->urb[i] = urb;
703 if (!urb) {
704 err = -ENOMEM;
705 DBG(1, "usb_alloc_urb() failed")
706 goto free_urbs;
708 urb->dev = udev;
709 urb->context = cam;
710 urb->pipe = usb_rcvisocpipe(udev, 1);
711 urb->transfer_flags = URB_ISO_ASAP;
712 urb->number_of_packets = SN9C102_ISO_PACKETS;
713 urb->complete = sn9c102_urb_complete;
714 urb->transfer_buffer = cam->transfer_buffer[i];
715 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
716 urb->interval = 1;
717 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
718 urb->iso_frame_desc[j].offset = psz * j;
719 urb->iso_frame_desc[j].length = psz;
723 /* Enable video */
724 if (!(cam->reg[0x01] & 0x04)) {
725 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
726 if (err) {
727 err = -EIO;
728 DBG(1, "I/O hardware error")
729 goto free_urbs;
733 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
734 if (err) {
735 DBG(1, "usb_set_interface() failed")
736 goto free_urbs;
739 cam->frame_current = NULL;
741 for (i = 0; i < SN9C102_URBS; i++) {
742 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
743 if (err) {
744 for (j = i-1; j >= 0; j--)
745 usb_kill_urb(cam->urb[j]);
746 DBG(1, "usb_submit_urb() failed, error %d", err)
747 goto free_urbs;
751 return 0;
753 free_urbs:
754 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
755 usb_free_urb(cam->urb[i]);
757 free_buffers:
758 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
759 kfree(cam->transfer_buffer[i]);
761 return err;
765 static int sn9c102_stop_transfer(struct sn9c102_device* cam)
767 struct usb_device *udev = cam->usbdev;
768 s8 i;
769 int err = 0;
771 if (cam->state & DEV_DISCONNECTED)
772 return 0;
774 for (i = SN9C102_URBS-1; i >= 0; i--) {
775 usb_kill_urb(cam->urb[i]);
776 usb_free_urb(cam->urb[i]);
777 kfree(cam->transfer_buffer[i]);
780 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
781 if (err)
782 DBG(3, "usb_set_interface() failed")
784 return err;
788 static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
790 int err = 0;
792 cam->stream = STREAM_INTERRUPT;
793 err = wait_event_timeout(cam->wait_stream,
794 (cam->stream == STREAM_OFF) ||
795 (cam->state & DEV_DISCONNECTED),
796 SN9C102_URB_TIMEOUT);
797 if (cam->state & DEV_DISCONNECTED)
798 return -ENODEV;
799 else if (err) {
800 cam->state |= DEV_MISCONFIGURED;
801 DBG(1, "The camera is misconfigured. To use it, close and "
802 "open /dev/video%d again.", cam->v4ldev->minor)
803 return err;
806 return 0;
809 /*****************************************************************************/
811 static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
813 char str[5];
814 char* endp;
815 unsigned long val;
817 if (len < 4) {
818 strncpy(str, buff, len);
819 str[len+1] = '\0';
820 } else {
821 strncpy(str, buff, 4);
822 str[4] = '\0';
825 val = simple_strtoul(str, &endp, 0);
827 *count = 0;
828 if (val <= 0xff)
829 *count = (ssize_t)(endp - str);
830 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
831 *count += 1;
833 return (u8)val;
837 NOTE 1: being inside one of the following methods implies that the v4l
838 device exists for sure (see kobjects and reference counters)
839 NOTE 2: buffers are PAGE_SIZE long
842 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
844 struct sn9c102_device* cam;
845 ssize_t count;
847 if (down_interruptible(&sn9c102_sysfs_lock))
848 return -ERESTARTSYS;
850 cam = video_get_drvdata(to_video_device(cd));
851 if (!cam) {
852 up(&sn9c102_sysfs_lock);
853 return -ENODEV;
856 count = sprintf(buf, "%u\n", cam->sysfs.reg);
858 up(&sn9c102_sysfs_lock);
860 return count;
864 static ssize_t
865 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
867 struct sn9c102_device* cam;
868 u8 index;
869 ssize_t count;
871 if (down_interruptible(&sn9c102_sysfs_lock))
872 return -ERESTARTSYS;
874 cam = video_get_drvdata(to_video_device(cd));
875 if (!cam) {
876 up(&sn9c102_sysfs_lock);
877 return -ENODEV;
880 index = sn9c102_strtou8(buf, len, &count);
881 if (index > 0x1f || !count) {
882 up(&sn9c102_sysfs_lock);
883 return -EINVAL;
886 cam->sysfs.reg = index;
888 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg)
889 DBG(3, "Written bytes: %zd", count)
891 up(&sn9c102_sysfs_lock);
893 return count;
897 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
899 struct sn9c102_device* cam;
900 ssize_t count;
901 int val;
903 if (down_interruptible(&sn9c102_sysfs_lock))
904 return -ERESTARTSYS;
906 cam = video_get_drvdata(to_video_device(cd));
907 if (!cam) {
908 up(&sn9c102_sysfs_lock);
909 return -ENODEV;
912 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
913 up(&sn9c102_sysfs_lock);
914 return -EIO;
917 count = sprintf(buf, "%d\n", val);
919 DBG(3, "Read bytes: %zd", count)
921 up(&sn9c102_sysfs_lock);
923 return count;
927 static ssize_t
928 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
930 struct sn9c102_device* cam;
931 u8 value;
932 ssize_t count;
933 int err;
935 if (down_interruptible(&sn9c102_sysfs_lock))
936 return -ERESTARTSYS;
938 cam = video_get_drvdata(to_video_device(cd));
939 if (!cam) {
940 up(&sn9c102_sysfs_lock);
941 return -ENODEV;
944 value = sn9c102_strtou8(buf, len, &count);
945 if (!count) {
946 up(&sn9c102_sysfs_lock);
947 return -EINVAL;
950 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
951 if (err) {
952 up(&sn9c102_sysfs_lock);
953 return -EIO;
956 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
957 cam->sysfs.reg, value)
958 DBG(3, "Written bytes: %zd", count)
960 up(&sn9c102_sysfs_lock);
962 return count;
966 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
968 struct sn9c102_device* cam;
969 ssize_t count;
971 if (down_interruptible(&sn9c102_sysfs_lock))
972 return -ERESTARTSYS;
974 cam = video_get_drvdata(to_video_device(cd));
975 if (!cam) {
976 up(&sn9c102_sysfs_lock);
977 return -ENODEV;
980 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
982 DBG(3, "Read bytes: %zd", count)
984 up(&sn9c102_sysfs_lock);
986 return count;
990 static ssize_t
991 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
993 struct sn9c102_device* cam;
994 u8 index;
995 ssize_t count;
997 if (down_interruptible(&sn9c102_sysfs_lock))
998 return -ERESTARTSYS;
1000 cam = video_get_drvdata(to_video_device(cd));
1001 if (!cam) {
1002 up(&sn9c102_sysfs_lock);
1003 return -ENODEV;
1006 index = sn9c102_strtou8(buf, len, &count);
1007 if (!count) {
1008 up(&sn9c102_sysfs_lock);
1009 return -EINVAL;
1012 cam->sysfs.i2c_reg = index;
1014 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg)
1015 DBG(3, "Written bytes: %zd", count)
1017 up(&sn9c102_sysfs_lock);
1019 return count;
1023 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1025 struct sn9c102_device* cam;
1026 ssize_t count;
1027 int val;
1029 if (down_interruptible(&sn9c102_sysfs_lock))
1030 return -ERESTARTSYS;
1032 cam = video_get_drvdata(to_video_device(cd));
1033 if (!cam) {
1034 up(&sn9c102_sysfs_lock);
1035 return -ENODEV;
1038 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_READ)) {
1039 up(&sn9c102_sysfs_lock);
1040 return -ENOSYS;
1043 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1044 up(&sn9c102_sysfs_lock);
1045 return -EIO;
1048 count = sprintf(buf, "%d\n", val);
1050 DBG(3, "Read bytes: %zd", count)
1052 up(&sn9c102_sysfs_lock);
1054 return count;
1058 static ssize_t
1059 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1061 struct sn9c102_device* cam;
1062 u8 value;
1063 ssize_t count;
1064 int err;
1066 if (down_interruptible(&sn9c102_sysfs_lock))
1067 return -ERESTARTSYS;
1069 cam = video_get_drvdata(to_video_device(cd));
1070 if (!cam) {
1071 up(&sn9c102_sysfs_lock);
1072 return -ENODEV;
1075 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_WRITE)) {
1076 up(&sn9c102_sysfs_lock);
1077 return -ENOSYS;
1080 value = sn9c102_strtou8(buf, len, &count);
1081 if (!count) {
1082 up(&sn9c102_sysfs_lock);
1083 return -EINVAL;
1086 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1087 if (err) {
1088 up(&sn9c102_sysfs_lock);
1089 return -EIO;
1092 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1093 cam->sysfs.i2c_reg, value)
1094 DBG(3, "Written bytes: %zd", count)
1096 up(&sn9c102_sysfs_lock);
1098 return count;
1102 static ssize_t
1103 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1105 struct sn9c102_device* cam;
1106 enum sn9c102_bridge bridge;
1107 ssize_t res = 0;
1108 u8 value;
1109 ssize_t count;
1111 if (down_interruptible(&sn9c102_sysfs_lock))
1112 return -ERESTARTSYS;
1114 cam = video_get_drvdata(to_video_device(cd));
1115 if (!cam) {
1116 up(&sn9c102_sysfs_lock);
1117 return -ENODEV;
1120 bridge = cam->bridge;
1122 up(&sn9c102_sysfs_lock);
1124 value = sn9c102_strtou8(buf, len, &count);
1125 if (!count)
1126 return -EINVAL;
1128 switch (bridge) {
1129 case BRIDGE_SN9C101:
1130 case BRIDGE_SN9C102:
1131 if (value > 0x0f)
1132 return -EINVAL;
1133 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1134 res = sn9c102_store_val(cd, buf, len);
1135 break;
1136 case BRIDGE_SN9C103:
1137 if (value > 0x7f)
1138 return -EINVAL;
1139 if ((res = sn9c102_store_reg(cd, "0x04", 4)) >= 0)
1140 res = sn9c102_store_val(cd, buf, len);
1141 break;
1144 return res;
1148 static ssize_t
1149 sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1151 ssize_t res = 0;
1152 u8 value;
1153 ssize_t count;
1155 value = sn9c102_strtou8(buf, len, &count);
1156 if (!count || value > 0x7f)
1157 return -EINVAL;
1159 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1160 res = sn9c102_store_val(cd, buf, len);
1162 return res;
1166 static ssize_t
1167 sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1169 ssize_t res = 0;
1170 u8 value;
1171 ssize_t count;
1173 value = sn9c102_strtou8(buf, len, &count);
1174 if (!count || value > 0x7f)
1175 return -EINVAL;
1177 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1178 res = sn9c102_store_val(cd, buf, len);
1180 return res;
1184 static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1186 struct sn9c102_device* cam;
1187 ssize_t count;
1189 cam = video_get_drvdata(to_video_device(cd));
1190 if (!cam)
1191 return -ENODEV;
1193 count = sizeof(cam->sysfs.frame_header);
1194 memcpy(buf, cam->sysfs.frame_header, count);
1196 DBG(3, "Frame header, read bytes: %zd", count)
1198 return count;
1202 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1203 sn9c102_show_reg, sn9c102_store_reg);
1204 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1205 sn9c102_show_val, sn9c102_store_val);
1206 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1207 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1208 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1209 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1210 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1211 static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1212 static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1213 static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
1214 sn9c102_show_frame_header, NULL);
1217 static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1219 struct video_device *v4ldev = cam->v4ldev;
1221 video_device_create_file(v4ldev, &class_device_attr_reg);
1222 video_device_create_file(v4ldev, &class_device_attr_val);
1223 video_device_create_file(v4ldev, &class_device_attr_frame_header);
1224 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
1225 video_device_create_file(v4ldev, &class_device_attr_green);
1226 else if (cam->bridge == BRIDGE_SN9C103) {
1227 video_device_create_file(v4ldev, &class_device_attr_blue);
1228 video_device_create_file(v4ldev, &class_device_attr_red);
1230 if (cam->sensor->sysfs_ops) {
1231 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1232 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1236 /*****************************************************************************/
1238 static int
1239 sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
1241 int err = 0;
1243 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
1244 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, 0x18);
1245 else
1246 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, 0x18);
1248 return err ? -EIO : 0;
1252 static int
1253 sn9c102_set_compression(struct sn9c102_device* cam,
1254 struct v4l2_jpegcompression* compression)
1256 int err = 0;
1258 if (compression->quality == 0)
1259 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, 0x17);
1260 else if (compression->quality == 1)
1261 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, 0x17);
1263 return err ? -EIO : 0;
1267 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1269 u8 r = 0;
1270 int err = 0;
1272 if (scale == 1)
1273 r = cam->reg[0x18] & 0xcf;
1274 else if (scale == 2) {
1275 r = cam->reg[0x18] & 0xcf;
1276 r |= 0x10;
1277 } else if (scale == 4)
1278 r = cam->reg[0x18] | 0x20;
1280 err += sn9c102_write_reg(cam, r, 0x18);
1281 if (err)
1282 return -EIO;
1284 PDBGG("Scaling factor: %u", scale)
1286 return 0;
1290 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1292 struct sn9c102_sensor* s = cam->sensor;
1293 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1294 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1295 h_size = (u8)(rect->width / 16),
1296 v_size = (u8)(rect->height / 16);
1297 int err = 0;
1299 err += sn9c102_write_reg(cam, h_start, 0x12);
1300 err += sn9c102_write_reg(cam, v_start, 0x13);
1301 err += sn9c102_write_reg(cam, h_size, 0x15);
1302 err += sn9c102_write_reg(cam, v_size, 0x16);
1303 if (err)
1304 return -EIO;
1306 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1307 "%u %u %u %u", h_start, v_start, h_size, v_size)
1309 return 0;
1313 static int sn9c102_init(struct sn9c102_device* cam)
1315 struct sn9c102_sensor* s = cam->sensor;
1316 struct v4l2_control ctrl;
1317 struct v4l2_queryctrl *qctrl;
1318 struct v4l2_rect* rect;
1319 u8 i = 0, n = 0;
1320 int err = 0;
1322 if (!(cam->state & DEV_INITIALIZED)) {
1323 init_waitqueue_head(&cam->open);
1324 qctrl = s->qctrl;
1325 rect = &(s->cropcap.defrect);
1326 } else { /* use current values */
1327 qctrl = s->_qctrl;
1328 rect = &(s->_rect);
1331 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1332 err += sn9c102_set_crop(cam, rect);
1333 if (err)
1334 return err;
1336 if (s->init) {
1337 err = s->init(cam);
1338 if (err) {
1339 DBG(3, "Sensor initialization failed")
1340 return err;
1344 if (!(cam->state & DEV_INITIALIZED))
1345 cam->compression.quality = cam->reg[0x17] & 0x01 ? 0 : 1;
1346 else
1347 err += sn9c102_set_compression(cam, &cam->compression);
1348 err += sn9c102_set_pix_format(cam, &s->pix_format);
1349 if (s->set_pix_format)
1350 err += s->set_pix_format(cam, &s->pix_format);
1351 if (err)
1352 return err;
1354 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1355 DBG(3, "Compressed video format is active, quality %d",
1356 cam->compression.quality)
1357 else
1358 DBG(3, "Uncompressed video format is active")
1360 if (s->set_crop)
1361 if ((err = s->set_crop(cam, rect))) {
1362 DBG(3, "set_crop() failed")
1363 return err;
1366 if (s->set_ctrl) {
1367 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1368 for (i = 0; i < n; i++)
1369 if (s->qctrl[i].id != 0 &&
1370 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1371 ctrl.id = s->qctrl[i].id;
1372 ctrl.value = qctrl[i].default_value;
1373 err = s->set_ctrl(cam, &ctrl);
1374 if (err) {
1375 DBG(3, "Set %s control failed",
1376 s->qctrl[i].name)
1377 return err;
1379 DBG(3, "Image sensor supports '%s' control",
1380 s->qctrl[i].name)
1384 if (!(cam->state & DEV_INITIALIZED)) {
1385 init_MUTEX(&cam->fileop_sem);
1386 spin_lock_init(&cam->queue_lock);
1387 init_waitqueue_head(&cam->wait_frame);
1388 init_waitqueue_head(&cam->wait_stream);
1389 cam->nreadbuffers = 2;
1390 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1391 memcpy(&(s->_rect), &(s->cropcap.defrect),
1392 sizeof(struct v4l2_rect));
1393 cam->state |= DEV_INITIALIZED;
1396 DBG(2, "Initialization succeeded")
1397 return 0;
1401 static void sn9c102_release_resources(struct sn9c102_device* cam)
1403 down(&sn9c102_sysfs_lock);
1405 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor)
1406 video_set_drvdata(cam->v4ldev, NULL);
1407 video_unregister_device(cam->v4ldev);
1409 up(&sn9c102_sysfs_lock);
1411 kfree(cam->control_buffer);
1414 /*****************************************************************************/
1416 static int sn9c102_open(struct inode* inode, struct file* filp)
1418 struct sn9c102_device* cam;
1419 int err = 0;
1422 This is the only safe way to prevent race conditions with
1423 disconnect
1425 if (!down_read_trylock(&sn9c102_disconnect))
1426 return -ERESTARTSYS;
1428 cam = video_get_drvdata(video_devdata(filp));
1430 if (down_interruptible(&cam->dev_sem)) {
1431 up_read(&sn9c102_disconnect);
1432 return -ERESTARTSYS;
1435 if (cam->users) {
1436 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor)
1437 if ((filp->f_flags & O_NONBLOCK) ||
1438 (filp->f_flags & O_NDELAY)) {
1439 err = -EWOULDBLOCK;
1440 goto out;
1442 up(&cam->dev_sem);
1443 err = wait_event_interruptible_exclusive(cam->open,
1444 cam->state & DEV_DISCONNECTED
1445 || !cam->users);
1446 if (err) {
1447 up_read(&sn9c102_disconnect);
1448 return err;
1450 if (cam->state & DEV_DISCONNECTED) {
1451 up_read(&sn9c102_disconnect);
1452 return -ENODEV;
1454 down(&cam->dev_sem);
1458 if (cam->state & DEV_MISCONFIGURED) {
1459 err = sn9c102_init(cam);
1460 if (err) {
1461 DBG(1, "Initialization failed again. "
1462 "I will retry on next open().")
1463 goto out;
1465 cam->state &= ~DEV_MISCONFIGURED;
1468 if ((err = sn9c102_start_transfer(cam)))
1469 goto out;
1471 filp->private_data = cam;
1472 cam->users++;
1473 cam->io = IO_NONE;
1474 cam->stream = STREAM_OFF;
1475 cam->nbuffers = 0;
1476 cam->frame_count = 0;
1477 sn9c102_empty_framequeues(cam);
1479 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor)
1481 out:
1482 up(&cam->dev_sem);
1483 up_read(&sn9c102_disconnect);
1484 return err;
1488 static int sn9c102_release(struct inode* inode, struct file* filp)
1490 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1492 down(&cam->dev_sem); /* prevent disconnect() to be called */
1494 sn9c102_stop_transfer(cam);
1496 sn9c102_release_buffers(cam);
1498 if (cam->state & DEV_DISCONNECTED) {
1499 sn9c102_release_resources(cam);
1500 up(&cam->dev_sem);
1501 kfree(cam);
1502 return 0;
1505 cam->users--;
1506 wake_up_interruptible_nr(&cam->open, 1);
1508 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor)
1510 up(&cam->dev_sem);
1512 return 0;
1516 static ssize_t
1517 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1519 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1520 struct sn9c102_frame_t* f, * i;
1521 unsigned long lock_flags;
1522 int err = 0;
1524 if (down_interruptible(&cam->fileop_sem))
1525 return -ERESTARTSYS;
1527 if (cam->state & DEV_DISCONNECTED) {
1528 DBG(1, "Device not present")
1529 up(&cam->fileop_sem);
1530 return -ENODEV;
1533 if (cam->state & DEV_MISCONFIGURED) {
1534 DBG(1, "The camera is misconfigured. Close and open it again.")
1535 up(&cam->fileop_sem);
1536 return -EIO;
1539 if (cam->io == IO_MMAP) {
1540 DBG(3, "Close and open the device again to choose "
1541 "the read method")
1542 up(&cam->fileop_sem);
1543 return -EINVAL;
1546 if (cam->io == IO_NONE) {
1547 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1548 DBG(1, "read() failed, not enough memory")
1549 up(&cam->fileop_sem);
1550 return -ENOMEM;
1552 cam->io = IO_READ;
1553 cam->stream = STREAM_ON;
1554 sn9c102_queue_unusedframes(cam);
1557 if (!count) {
1558 up(&cam->fileop_sem);
1559 return 0;
1562 if (list_empty(&cam->outqueue)) {
1563 if (filp->f_flags & O_NONBLOCK) {
1564 up(&cam->fileop_sem);
1565 return -EAGAIN;
1567 err = wait_event_interruptible
1568 ( cam->wait_frame,
1569 (!list_empty(&cam->outqueue)) ||
1570 (cam->state & DEV_DISCONNECTED) ||
1571 (cam->state & DEV_MISCONFIGURED) );
1572 if (err) {
1573 up(&cam->fileop_sem);
1574 return err;
1576 if (cam->state & DEV_DISCONNECTED) {
1577 up(&cam->fileop_sem);
1578 return -ENODEV;
1580 if (cam->state & DEV_MISCONFIGURED) {
1581 up(&cam->fileop_sem);
1582 return -EIO;
1586 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1588 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1589 list_for_each_entry(i, &cam->outqueue, frame)
1590 i->state = F_UNUSED;
1591 INIT_LIST_HEAD(&cam->outqueue);
1592 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1594 sn9c102_queue_unusedframes(cam);
1596 if (count > f->buf.bytesused)
1597 count = f->buf.bytesused;
1599 if (copy_to_user(buf, f->bufmem, count)) {
1600 up(&cam->fileop_sem);
1601 return -EFAULT;
1603 *f_pos += count;
1605 PDBGG("Frame #%lu, bytes read: %zu", (unsigned long)f->buf.index,count)
1607 up(&cam->fileop_sem);
1609 return count;
1613 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1615 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1616 unsigned int mask = 0;
1618 if (down_interruptible(&cam->fileop_sem))
1619 return POLLERR;
1621 if (cam->state & DEV_DISCONNECTED) {
1622 DBG(1, "Device not present")
1623 goto error;
1626 if (cam->state & DEV_MISCONFIGURED) {
1627 DBG(1, "The camera is misconfigured. Close and open it again.")
1628 goto error;
1631 if (cam->io == IO_NONE) {
1632 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1633 IO_READ)) {
1634 DBG(1, "poll() failed, not enough memory")
1635 goto error;
1637 cam->io = IO_READ;
1638 cam->stream = STREAM_ON;
1641 if (cam->io == IO_READ)
1642 sn9c102_queue_unusedframes(cam);
1644 poll_wait(filp, &cam->wait_frame, wait);
1646 if (!list_empty(&cam->outqueue))
1647 mask |= POLLIN | POLLRDNORM;
1649 up(&cam->fileop_sem);
1651 return mask;
1653 error:
1654 up(&cam->fileop_sem);
1655 return POLLERR;
1659 static void sn9c102_vm_open(struct vm_area_struct* vma)
1661 struct sn9c102_frame_t* f = vma->vm_private_data;
1662 f->vma_use_count++;
1666 static void sn9c102_vm_close(struct vm_area_struct* vma)
1668 /* NOTE: buffers are not freed here */
1669 struct sn9c102_frame_t* f = vma->vm_private_data;
1670 f->vma_use_count--;
1674 static struct vm_operations_struct sn9c102_vm_ops = {
1675 .open = sn9c102_vm_open,
1676 .close = sn9c102_vm_close,
1680 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1682 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1683 unsigned long size = vma->vm_end - vma->vm_start,
1684 start = vma->vm_start,
1685 pos,
1686 page;
1687 u32 i;
1689 if (down_interruptible(&cam->fileop_sem))
1690 return -ERESTARTSYS;
1692 if (cam->state & DEV_DISCONNECTED) {
1693 DBG(1, "Device not present")
1694 up(&cam->fileop_sem);
1695 return -ENODEV;
1698 if (cam->state & DEV_MISCONFIGURED) {
1699 DBG(1, "The camera is misconfigured. Close and open it again.")
1700 up(&cam->fileop_sem);
1701 return -EIO;
1704 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1705 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1706 up(&cam->fileop_sem);
1707 return -EINVAL;
1710 for (i = 0; i < cam->nbuffers; i++) {
1711 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1712 break;
1714 if (i == cam->nbuffers) {
1715 up(&cam->fileop_sem);
1716 return -EINVAL;
1719 /* VM_IO is eventually going to replace PageReserved altogether */
1720 vma->vm_flags |= VM_IO;
1721 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1723 pos = (unsigned long)cam->frame[i].bufmem;
1724 while (size > 0) { /* size is page-aligned */
1725 page = vmalloc_to_pfn((void *)pos);
1726 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
1727 vma->vm_page_prot)) {
1728 up(&cam->fileop_sem);
1729 return -EAGAIN;
1731 start += PAGE_SIZE;
1732 pos += PAGE_SIZE;
1733 size -= PAGE_SIZE;
1736 vma->vm_ops = &sn9c102_vm_ops;
1737 vma->vm_private_data = &cam->frame[i];
1739 sn9c102_vm_open(vma);
1741 up(&cam->fileop_sem);
1743 return 0;
1747 static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
1748 unsigned int cmd, void __user * arg)
1750 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1752 switch (cmd) {
1754 case VIDIOC_QUERYCAP:
1756 struct v4l2_capability cap = {
1757 .driver = "sn9c102",
1758 .version = SN9C102_MODULE_VERSION_CODE,
1759 .capabilities = V4L2_CAP_VIDEO_CAPTURE |
1760 V4L2_CAP_READWRITE |
1761 V4L2_CAP_STREAMING,
1764 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1765 if (usb_make_path(cam->usbdev, cap.bus_info,
1766 sizeof(cap.bus_info)) < 0)
1767 strlcpy(cap.bus_info, cam->dev.bus_id,
1768 sizeof(cap.bus_info));
1770 if (copy_to_user(arg, &cap, sizeof(cap)))
1771 return -EFAULT;
1773 return 0;
1776 case VIDIOC_ENUMINPUT:
1778 struct v4l2_input i;
1780 if (copy_from_user(&i, arg, sizeof(i)))
1781 return -EFAULT;
1783 if (i.index)
1784 return -EINVAL;
1786 memset(&i, 0, sizeof(i));
1787 strcpy(i.name, "USB");
1789 if (copy_to_user(arg, &i, sizeof(i)))
1790 return -EFAULT;
1792 return 0;
1795 case VIDIOC_G_INPUT:
1796 case VIDIOC_S_INPUT:
1798 int index;
1800 if (copy_from_user(&index, arg, sizeof(index)))
1801 return -EFAULT;
1803 if (index != 0)
1804 return -EINVAL;
1806 return 0;
1809 case VIDIOC_QUERYCTRL:
1811 struct sn9c102_sensor* s = cam->sensor;
1812 struct v4l2_queryctrl qc;
1813 u8 i, n;
1815 if (copy_from_user(&qc, arg, sizeof(qc)))
1816 return -EFAULT;
1818 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1819 for (i = 0; i < n; i++)
1820 if (qc.id && qc.id == s->qctrl[i].id) {
1821 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1822 if (copy_to_user(arg, &qc, sizeof(qc)))
1823 return -EFAULT;
1824 return 0;
1827 return -EINVAL;
1830 case VIDIOC_G_CTRL:
1832 struct sn9c102_sensor* s = cam->sensor;
1833 struct v4l2_control ctrl;
1834 int err = 0;
1836 if (!s->get_ctrl)
1837 return -EINVAL;
1839 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1840 return -EFAULT;
1842 err = s->get_ctrl(cam, &ctrl);
1844 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1845 return -EFAULT;
1847 return err;
1850 case VIDIOC_S_CTRL_OLD:
1851 case VIDIOC_S_CTRL:
1853 struct sn9c102_sensor* s = cam->sensor;
1854 struct v4l2_control ctrl;
1855 u8 i, n;
1856 int err = 0;
1858 if (!s->set_ctrl)
1859 return -EINVAL;
1861 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1862 return -EFAULT;
1864 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1865 for (i = 0; i < n; i++)
1866 if (ctrl.id == s->qctrl[i].id) {
1867 if (ctrl.value < s->qctrl[i].minimum ||
1868 ctrl.value > s->qctrl[i].maximum)
1869 return -ERANGE;
1870 ctrl.value -= ctrl.value % s->qctrl[i].step;
1871 break;
1874 if ((err = s->set_ctrl(cam, &ctrl)))
1875 return err;
1877 s->_qctrl[i].default_value = ctrl.value;
1879 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
1880 (unsigned long)ctrl.id, (unsigned long)ctrl.value)
1882 return 0;
1885 case VIDIOC_CROPCAP:
1887 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1889 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1890 cc->pixelaspect.numerator = 1;
1891 cc->pixelaspect.denominator = 1;
1893 if (copy_to_user(arg, cc, sizeof(*cc)))
1894 return -EFAULT;
1896 return 0;
1899 case VIDIOC_G_CROP:
1901 struct sn9c102_sensor* s = cam->sensor;
1902 struct v4l2_crop crop = {
1903 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1906 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1908 if (copy_to_user(arg, &crop, sizeof(crop)))
1909 return -EFAULT;
1911 return 0;
1914 case VIDIOC_S_CROP:
1916 struct sn9c102_sensor* s = cam->sensor;
1917 struct v4l2_crop crop;
1918 struct v4l2_rect* rect;
1919 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1920 struct v4l2_pix_format* pix_format = &(s->pix_format);
1921 u8 scale;
1922 const enum sn9c102_stream_state stream = cam->stream;
1923 const u32 nbuffers = cam->nbuffers;
1924 u32 i;
1925 int err = 0;
1927 if (copy_from_user(&crop, arg, sizeof(crop)))
1928 return -EFAULT;
1930 rect = &(crop.c);
1932 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1933 return -EINVAL;
1935 if (cam->module_param.force_munmap)
1936 for (i = 0; i < cam->nbuffers; i++)
1937 if (cam->frame[i].vma_use_count) {
1938 DBG(3, "VIDIOC_S_CROP failed. "
1939 "Unmap the buffers first.")
1940 return -EINVAL;
1943 /* Preserve R,G or B origin */
1944 rect->left = (s->_rect.left & 1L) ?
1945 rect->left | 1L : rect->left & ~1L;
1946 rect->top = (s->_rect.top & 1L) ?
1947 rect->top | 1L : rect->top & ~1L;
1949 if (rect->width < 16)
1950 rect->width = 16;
1951 if (rect->height < 16)
1952 rect->height = 16;
1953 if (rect->width > bounds->width)
1954 rect->width = bounds->width;
1955 if (rect->height > bounds->height)
1956 rect->height = bounds->height;
1957 if (rect->left < bounds->left)
1958 rect->left = bounds->left;
1959 if (rect->top < bounds->top)
1960 rect->top = bounds->top;
1961 if (rect->left + rect->width > bounds->left + bounds->width)
1962 rect->left = bounds->left+bounds->width - rect->width;
1963 if (rect->top + rect->height > bounds->top + bounds->height)
1964 rect->top = bounds->top+bounds->height - rect->height;
1966 rect->width &= ~15L;
1967 rect->height &= ~15L;
1969 if (SN9C102_PRESERVE_IMGSCALE) {
1970 /* Calculate the actual scaling factor */
1971 u32 a, b;
1972 a = rect->width * rect->height;
1973 b = pix_format->width * pix_format->height;
1974 scale = b ? (u8)((a / b) < 4 ? 1 :
1975 ((a / b) < 16 ? 2 : 4)) : 1;
1976 } else
1977 scale = 1;
1979 if (cam->stream == STREAM_ON)
1980 if ((err = sn9c102_stream_interrupt(cam)))
1981 return err;
1983 if (copy_to_user(arg, &crop, sizeof(crop))) {
1984 cam->stream = stream;
1985 return -EFAULT;
1988 if (cam->module_param.force_munmap || cam->io == IO_READ)
1989 sn9c102_release_buffers(cam);
1991 err = sn9c102_set_crop(cam, rect);
1992 if (s->set_crop)
1993 err += s->set_crop(cam, rect);
1994 err += sn9c102_set_scale(cam, scale);
1996 if (err) { /* atomic, no rollback in ioctl() */
1997 cam->state |= DEV_MISCONFIGURED;
1998 DBG(1, "VIDIOC_S_CROP failed because of hardware "
1999 "problems. To use the camera, close and open "
2000 "/dev/video%d again.", cam->v4ldev->minor)
2001 return -EIO;
2004 s->pix_format.width = rect->width/scale;
2005 s->pix_format.height = rect->height/scale;
2006 memcpy(&(s->_rect), rect, sizeof(*rect));
2008 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2009 nbuffers != sn9c102_request_buffers(cam, nbuffers,
2010 cam->io)) {
2011 cam->state |= DEV_MISCONFIGURED;
2012 DBG(1, "VIDIOC_S_CROP failed because of not enough "
2013 "memory. To use the camera, close and open "
2014 "/dev/video%d again.", cam->v4ldev->minor)
2015 return -ENOMEM;
2018 cam->stream = stream;
2020 return 0;
2023 case VIDIOC_ENUM_FMT:
2025 struct v4l2_fmtdesc fmtd;
2027 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
2028 return -EFAULT;
2030 if (fmtd.index == 0) {
2031 strcpy(fmtd.description, "bayer rgb");
2032 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2033 } else if (fmtd.index == 1) {
2034 strcpy(fmtd.description, "compressed");
2035 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2036 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2037 } else
2038 return -EINVAL;
2040 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2041 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2043 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
2044 return -EFAULT;
2046 return 0;
2049 case VIDIOC_G_FMT:
2051 struct v4l2_format format;
2052 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
2054 if (copy_from_user(&format, arg, sizeof(format)))
2055 return -EFAULT;
2057 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2058 return -EINVAL;
2060 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X)
2061 ? 0 : (pfmt->width * pfmt->priv) / 8;
2062 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2063 pfmt->field = V4L2_FIELD_NONE;
2064 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2066 if (copy_to_user(arg, &format, sizeof(format)))
2067 return -EFAULT;
2069 return 0;
2072 case VIDIOC_TRY_FMT:
2073 case VIDIOC_S_FMT:
2075 struct sn9c102_sensor* s = cam->sensor;
2076 struct v4l2_format format;
2077 struct v4l2_pix_format* pix;
2078 struct v4l2_pix_format* pfmt = &(s->pix_format);
2079 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2080 struct v4l2_rect rect;
2081 u8 scale;
2082 const enum sn9c102_stream_state stream = cam->stream;
2083 const u32 nbuffers = cam->nbuffers;
2084 u32 i;
2085 int err = 0;
2087 if (copy_from_user(&format, arg, sizeof(format)))
2088 return -EFAULT;
2090 pix = &(format.fmt.pix);
2092 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2093 return -EINVAL;
2095 memcpy(&rect, &(s->_rect), sizeof(rect));
2097 { /* calculate the actual scaling factor */
2098 u32 a, b;
2099 a = rect.width * rect.height;
2100 b = pix->width * pix->height;
2101 scale = b ? (u8)((a / b) < 4 ? 1 :
2102 ((a / b) < 16 ? 2 : 4)) : 1;
2105 rect.width = scale * pix->width;
2106 rect.height = scale * pix->height;
2108 if (rect.width < 16)
2109 rect.width = 16;
2110 if (rect.height < 16)
2111 rect.height = 16;
2112 if (rect.width > bounds->left + bounds->width - rect.left)
2113 rect.width = bounds->left + bounds->width - rect.left;
2114 if (rect.height > bounds->top + bounds->height - rect.top)
2115 rect.height = bounds->top + bounds->height - rect.top;
2117 rect.width &= ~15L;
2118 rect.height &= ~15L;
2120 { /* adjust the scaling factor */
2121 u32 a, b;
2122 a = rect.width * rect.height;
2123 b = pix->width * pix->height;
2124 scale = b ? (u8)((a / b) < 4 ? 1 :
2125 ((a / b) < 16 ? 2 : 4)) : 1;
2128 pix->width = rect.width / scale;
2129 pix->height = rect.height / scale;
2131 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2132 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2133 pix->pixelformat = pfmt->pixelformat;
2134 pix->priv = pfmt->priv; /* bpp */
2135 pix->colorspace = pfmt->colorspace;
2136 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
2137 ? 0 : (pix->width * pix->priv) / 8;
2138 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2139 pix->field = V4L2_FIELD_NONE;
2141 if (cmd == VIDIOC_TRY_FMT) {
2142 if (copy_to_user(arg, &format, sizeof(format)))
2143 return -EFAULT;
2144 return 0;
2147 if (cam->module_param.force_munmap)
2148 for (i = 0; i < cam->nbuffers; i++)
2149 if (cam->frame[i].vma_use_count) {
2150 DBG(3, "VIDIOC_S_FMT failed. "
2151 "Unmap the buffers first.")
2152 return -EINVAL;
2155 if (cam->stream == STREAM_ON)
2156 if ((err = sn9c102_stream_interrupt(cam)))
2157 return err;
2159 if (copy_to_user(arg, &format, sizeof(format))) {
2160 cam->stream = stream;
2161 return -EFAULT;
2164 if (cam->module_param.force_munmap || cam->io == IO_READ)
2165 sn9c102_release_buffers(cam);
2167 err += sn9c102_set_pix_format(cam, pix);
2168 err += sn9c102_set_crop(cam, &rect);
2169 if (s->set_pix_format)
2170 err += s->set_pix_format(cam, pix);
2171 if (s->set_crop)
2172 err += s->set_crop(cam, &rect);
2173 err += sn9c102_set_scale(cam, scale);
2175 if (err) { /* atomic, no rollback in ioctl() */
2176 cam->state |= DEV_MISCONFIGURED;
2177 DBG(1, "VIDIOC_S_FMT failed because of hardware "
2178 "problems. To use the camera, close and open "
2179 "/dev/video%d again.", cam->v4ldev->minor)
2180 return -EIO;
2183 memcpy(pfmt, pix, sizeof(*pix));
2184 memcpy(&(s->_rect), &rect, sizeof(rect));
2186 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2187 nbuffers != sn9c102_request_buffers(cam, nbuffers,
2188 cam->io)) {
2189 cam->state |= DEV_MISCONFIGURED;
2190 DBG(1, "VIDIOC_S_FMT failed because of not enough "
2191 "memory. To use the camera, close and open "
2192 "/dev/video%d again.", cam->v4ldev->minor)
2193 return -ENOMEM;
2196 cam->stream = stream;
2198 return 0;
2201 case VIDIOC_G_JPEGCOMP:
2203 if (copy_to_user(arg, &cam->compression,
2204 sizeof(cam->compression)))
2205 return -EFAULT;
2207 return 0;
2210 case VIDIOC_S_JPEGCOMP:
2212 struct v4l2_jpegcompression jc;
2213 const enum sn9c102_stream_state stream = cam->stream;
2214 int err = 0;
2216 if (copy_from_user(&jc, arg, sizeof(jc)))
2217 return -EFAULT;
2219 if (jc.quality != 0 && jc.quality != 1)
2220 return -EINVAL;
2222 if (cam->stream == STREAM_ON)
2223 if ((err = sn9c102_stream_interrupt(cam)))
2224 return err;
2226 err += sn9c102_set_compression(cam, &jc);
2227 if (err) { /* atomic, no rollback in ioctl() */
2228 cam->state |= DEV_MISCONFIGURED;
2229 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2230 "problems. To use the camera, close and open "
2231 "/dev/video%d again.", cam->v4ldev->minor)
2232 return -EIO;
2235 cam->compression.quality = jc.quality;
2237 cam->stream = stream;
2239 return 0;
2242 case VIDIOC_REQBUFS:
2244 struct v4l2_requestbuffers rb;
2245 u32 i;
2246 int err;
2248 if (copy_from_user(&rb, arg, sizeof(rb)))
2249 return -EFAULT;
2251 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2252 rb.memory != V4L2_MEMORY_MMAP)
2253 return -EINVAL;
2255 if (cam->io == IO_READ) {
2256 DBG(3, "Close and open the device again to choose "
2257 "the mmap I/O method")
2258 return -EINVAL;
2261 for (i = 0; i < cam->nbuffers; i++)
2262 if (cam->frame[i].vma_use_count) {
2263 DBG(3, "VIDIOC_REQBUFS failed. "
2264 "Previous buffers are still mapped.")
2265 return -EINVAL;
2268 if (cam->stream == STREAM_ON)
2269 if ((err = sn9c102_stream_interrupt(cam)))
2270 return err;
2272 sn9c102_empty_framequeues(cam);
2274 sn9c102_release_buffers(cam);
2275 if (rb.count)
2276 rb.count = sn9c102_request_buffers(cam, rb.count,
2277 IO_MMAP);
2279 if (copy_to_user(arg, &rb, sizeof(rb))) {
2280 sn9c102_release_buffers(cam);
2281 cam->io = IO_NONE;
2282 return -EFAULT;
2285 cam->io = rb.count ? IO_MMAP : IO_NONE;
2287 return 0;
2290 case VIDIOC_QUERYBUF:
2292 struct v4l2_buffer b;
2294 if (copy_from_user(&b, arg, sizeof(b)))
2295 return -EFAULT;
2297 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2298 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2299 return -EINVAL;
2301 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2303 if (cam->frame[b.index].vma_use_count)
2304 b.flags |= V4L2_BUF_FLAG_MAPPED;
2306 if (cam->frame[b.index].state == F_DONE)
2307 b.flags |= V4L2_BUF_FLAG_DONE;
2308 else if (cam->frame[b.index].state != F_UNUSED)
2309 b.flags |= V4L2_BUF_FLAG_QUEUED;
2311 if (copy_to_user(arg, &b, sizeof(b)))
2312 return -EFAULT;
2314 return 0;
2317 case VIDIOC_QBUF:
2319 struct v4l2_buffer b;
2320 unsigned long lock_flags;
2322 if (copy_from_user(&b, arg, sizeof(b)))
2323 return -EFAULT;
2325 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2326 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2327 return -EINVAL;
2329 if (cam->frame[b.index].state != F_UNUSED)
2330 return -EINVAL;
2332 cam->frame[b.index].state = F_QUEUED;
2334 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2335 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2336 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2338 PDBGG("Frame #%lu queued", (unsigned long)b.index)
2340 return 0;
2343 case VIDIOC_DQBUF:
2345 struct v4l2_buffer b;
2346 struct sn9c102_frame_t *f;
2347 unsigned long lock_flags;
2348 int err = 0;
2350 if (copy_from_user(&b, arg, sizeof(b)))
2351 return -EFAULT;
2353 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2354 return -EINVAL;
2356 if (list_empty(&cam->outqueue)) {
2357 if (cam->stream == STREAM_OFF)
2358 return -EINVAL;
2359 if (filp->f_flags & O_NONBLOCK)
2360 return -EAGAIN;
2361 err = wait_event_interruptible
2362 ( cam->wait_frame,
2363 (!list_empty(&cam->outqueue)) ||
2364 (cam->state & DEV_DISCONNECTED) ||
2365 (cam->state & DEV_MISCONFIGURED) );
2366 if (err)
2367 return err;
2368 if (cam->state & DEV_DISCONNECTED)
2369 return -ENODEV;
2370 if (cam->state & DEV_MISCONFIGURED)
2371 return -EIO;
2374 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2375 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t,
2376 frame);
2377 list_del(cam->outqueue.next);
2378 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2380 f->state = F_UNUSED;
2382 memcpy(&b, &f->buf, sizeof(b));
2383 if (f->vma_use_count)
2384 b.flags |= V4L2_BUF_FLAG_MAPPED;
2386 if (copy_to_user(arg, &b, sizeof(b)))
2387 return -EFAULT;
2389 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index)
2391 return 0;
2394 case VIDIOC_STREAMON:
2396 int type;
2398 if (copy_from_user(&type, arg, sizeof(type)))
2399 return -EFAULT;
2401 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2402 return -EINVAL;
2404 if (list_empty(&cam->inqueue))
2405 return -EINVAL;
2407 cam->stream = STREAM_ON;
2409 DBG(3, "Stream on")
2411 return 0;
2414 case VIDIOC_STREAMOFF:
2416 int type, err;
2418 if (copy_from_user(&type, arg, sizeof(type)))
2419 return -EFAULT;
2421 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2422 return -EINVAL;
2424 if (cam->stream == STREAM_ON)
2425 if ((err = sn9c102_stream_interrupt(cam)))
2426 return err;
2428 sn9c102_empty_framequeues(cam);
2430 DBG(3, "Stream off")
2432 return 0;
2435 case VIDIOC_G_PARM:
2437 struct v4l2_streamparm sp;
2439 if (copy_from_user(&sp, arg, sizeof(sp)))
2440 return -EFAULT;
2442 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2443 return -EINVAL;
2445 sp.parm.capture.extendedmode = 0;
2446 sp.parm.capture.readbuffers = cam->nreadbuffers;
2448 if (copy_to_user(arg, &sp, sizeof(sp)))
2449 return -EFAULT;
2451 return 0;
2454 case VIDIOC_S_PARM_OLD:
2455 case VIDIOC_S_PARM:
2457 struct v4l2_streamparm sp;
2459 if (copy_from_user(&sp, arg, sizeof(sp)))
2460 return -EFAULT;
2462 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2463 return -EINVAL;
2465 sp.parm.capture.extendedmode = 0;
2467 if (sp.parm.capture.readbuffers == 0)
2468 sp.parm.capture.readbuffers = cam->nreadbuffers;
2470 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2471 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2473 if (copy_to_user(arg, &sp, sizeof(sp)))
2474 return -EFAULT;
2476 cam->nreadbuffers = sp.parm.capture.readbuffers;
2478 return 0;
2481 case VIDIOC_G_STD:
2482 case VIDIOC_S_STD:
2483 case VIDIOC_QUERYSTD:
2484 case VIDIOC_ENUMSTD:
2485 case VIDIOC_QUERYMENU:
2486 return -EINVAL;
2488 default:
2489 return -EINVAL;
2495 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2496 unsigned int cmd, unsigned long arg)
2498 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2499 int err = 0;
2501 if (down_interruptible(&cam->fileop_sem))
2502 return -ERESTARTSYS;
2504 if (cam->state & DEV_DISCONNECTED) {
2505 DBG(1, "Device not present")
2506 up(&cam->fileop_sem);
2507 return -ENODEV;
2510 if (cam->state & DEV_MISCONFIGURED) {
2511 DBG(1, "The camera is misconfigured. Close and open it again.")
2512 up(&cam->fileop_sem);
2513 return -EIO;
2516 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2518 up(&cam->fileop_sem);
2520 return err;
2524 static struct file_operations sn9c102_fops = {
2525 .owner = THIS_MODULE,
2526 .open = sn9c102_open,
2527 .release = sn9c102_release,
2528 .ioctl = sn9c102_ioctl,
2529 .read = sn9c102_read,
2530 .poll = sn9c102_poll,
2531 .mmap = sn9c102_mmap,
2532 .llseek = no_llseek,
2535 /*****************************************************************************/
2537 /* It exists a single interface only. We do not need to validate anything. */
2538 static int
2539 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2541 struct usb_device *udev = interface_to_usbdev(intf);
2542 struct sn9c102_device* cam;
2543 static unsigned int dev_nr = 0;
2544 unsigned int i, n;
2545 int err = 0, r;
2547 n = sizeof(sn9c102_id_table)/sizeof(sn9c102_id_table[0]);
2548 for (i = 0; i < n-1; i++)
2549 if (le16_to_cpu(udev->descriptor.idVendor) ==
2550 sn9c102_id_table[i].idVendor &&
2551 le16_to_cpu(udev->descriptor.idProduct) ==
2552 sn9c102_id_table[i].idProduct)
2553 break;
2554 if (i == n-1)
2555 return -ENODEV;
2557 if (!(cam = kmalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2558 return -ENOMEM;
2559 memset(cam, 0, sizeof(*cam));
2561 cam->usbdev = udev;
2563 memcpy(&cam->dev, &udev->dev, sizeof(struct device));
2565 if (!(cam->control_buffer = kmalloc(8, GFP_KERNEL))) {
2566 DBG(1, "kmalloc() failed")
2567 err = -ENOMEM;
2568 goto fail;
2570 memset(cam->control_buffer, 0, 8);
2572 if (!(cam->v4ldev = video_device_alloc())) {
2573 DBG(1, "video_device_alloc() failed")
2574 err = -ENOMEM;
2575 goto fail;
2578 init_MUTEX(&cam->dev_sem);
2580 r = sn9c102_read_reg(cam, 0x00);
2581 if (r < 0 || r != 0x10) {
2582 DBG(1, "Sorry, this is not a SN9C10x based camera "
2583 "(vid/pid 0x%04X/0x%04X)",
2584 sn9c102_id_table[i].idVendor,sn9c102_id_table[i].idProduct)
2585 err = -ENODEV;
2586 goto fail;
2589 cam->bridge = (sn9c102_id_table[i].idProduct & 0xffc0) == 0x6080 ?
2590 BRIDGE_SN9C103 : BRIDGE_SN9C102;
2591 switch (cam->bridge) {
2592 case BRIDGE_SN9C101:
2593 case BRIDGE_SN9C102:
2594 DBG(2, "SN9C10[12] PC Camera Controller detected "
2595 "(vid/pid 0x%04X/0x%04X)", sn9c102_id_table[i].idVendor,
2596 sn9c102_id_table[i].idProduct)
2597 break;
2598 case BRIDGE_SN9C103:
2599 DBG(2, "SN9C103 PC Camera Controller detected "
2600 "(vid/pid 0x%04X/0x%04X)", sn9c102_id_table[i].idVendor,
2601 sn9c102_id_table[i].idProduct)
2602 break;
2605 for (i = 0; sn9c102_sensor_table[i]; i++) {
2606 err = sn9c102_sensor_table[i](cam);
2607 if (!err)
2608 break;
2611 if (!err && cam->sensor) {
2612 DBG(2, "%s image sensor detected", cam->sensor->name)
2613 DBG(3, "Support for %s maintained by %s",
2614 cam->sensor->name, cam->sensor->maintainer)
2615 } else {
2616 DBG(1, "No supported image sensor detected")
2617 err = -ENODEV;
2618 goto fail;
2621 if (sn9c102_init(cam)) {
2622 DBG(1, "Initialization failed. I will retry on open().")
2623 cam->state |= DEV_MISCONFIGURED;
2626 strcpy(cam->v4ldev->name, "SN9C10x PC Camera");
2627 cam->v4ldev->owner = THIS_MODULE;
2628 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2629 cam->v4ldev->hardware = VID_HARDWARE_SN9C102;
2630 cam->v4ldev->fops = &sn9c102_fops;
2631 cam->v4ldev->minor = video_nr[dev_nr];
2632 cam->v4ldev->release = video_device_release;
2633 video_set_drvdata(cam->v4ldev, cam);
2635 down(&cam->dev_sem);
2637 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2638 video_nr[dev_nr]);
2639 if (err) {
2640 DBG(1, "V4L2 device registration failed")
2641 if (err == -ENFILE && video_nr[dev_nr] == -1)
2642 DBG(1, "Free /dev/videoX node not found")
2643 video_nr[dev_nr] = -1;
2644 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2645 up(&cam->dev_sem);
2646 goto fail;
2649 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor)
2651 cam->module_param.force_munmap = force_munmap[dev_nr];
2653 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2655 sn9c102_create_sysfs(cam);
2656 DBG(2, "Optional device control through 'sysfs' interface ready")
2658 usb_set_intfdata(intf, cam);
2660 up(&cam->dev_sem);
2662 return 0;
2664 fail:
2665 if (cam) {
2666 kfree(cam->control_buffer);
2667 if (cam->v4ldev)
2668 video_device_release(cam->v4ldev);
2669 kfree(cam);
2671 return err;
2675 static void sn9c102_usb_disconnect(struct usb_interface* intf)
2677 struct sn9c102_device* cam = usb_get_intfdata(intf);
2679 if (!cam)
2680 return;
2682 down_write(&sn9c102_disconnect);
2684 down(&cam->dev_sem);
2686 DBG(2, "Disconnecting %s...", cam->v4ldev->name)
2688 wake_up_interruptible_all(&cam->open);
2690 if (cam->users) {
2691 DBG(2, "Device /dev/video%d is open! Deregistration and "
2692 "memory deallocation are deferred on close.",
2693 cam->v4ldev->minor)
2694 cam->state |= DEV_MISCONFIGURED;
2695 sn9c102_stop_transfer(cam);
2696 cam->state |= DEV_DISCONNECTED;
2697 wake_up_interruptible(&cam->wait_frame);
2698 wake_up_interruptible(&cam->wait_stream);
2699 } else {
2700 cam->state |= DEV_DISCONNECTED;
2701 sn9c102_release_resources(cam);
2704 up(&cam->dev_sem);
2706 if (!cam->users)
2707 kfree(cam);
2709 up_write(&sn9c102_disconnect);
2713 static struct usb_driver sn9c102_usb_driver = {
2714 .owner = THIS_MODULE,
2715 .name = "sn9c102",
2716 .id_table = sn9c102_id_table,
2717 .probe = sn9c102_usb_probe,
2718 .disconnect = sn9c102_usb_disconnect,
2721 /*****************************************************************************/
2723 static int __init sn9c102_module_init(void)
2725 int err = 0;
2727 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION)
2728 KDBG(3, SN9C102_MODULE_AUTHOR)
2730 if ((err = usb_register(&sn9c102_usb_driver)))
2731 KDBG(1, "usb_register() failed")
2733 return err;
2737 static void __exit sn9c102_module_exit(void)
2739 usb_deregister(&sn9c102_usb_driver);
2743 module_init(sn9c102_module_init);
2744 module_exit(sn9c102_module_exit);