1 /***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers *
4 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
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. *
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. *
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/device.h>
30 #include <linux/delay.h>
31 #include <linux/compiler.h>
32 #include <linux/ioctl.h>
33 #include <linux/poll.h>
34 #include <linux/stat.h>
36 #include <linux/vmalloc.h>
37 #include <linux/page-flags.h>
38 #include <linux/byteorder/generic.h>
40 #include <asm/uaccess.h>
44 /*****************************************************************************/
46 #define SN9C102_MODULE_NAME "V4L2 driver for SN9C10x PC Camera Controllers"
47 #define SN9C102_MODULE_AUTHOR "(C) 2004-2006 Luca Risolia"
48 #define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
49 #define SN9C102_MODULE_LICENSE "GPL"
50 #define SN9C102_MODULE_VERSION "1:1.27"
51 #define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 27)
53 /*****************************************************************************/
55 MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
57 MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
58 MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
59 MODULE_VERSION(SN9C102_MODULE_VERSION);
60 MODULE_LICENSE(SN9C102_MODULE_LICENSE);
62 static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
63 module_param_array(video_nr, short, NULL, 0444);
64 MODULE_PARM_DESC(video_nr,
65 "\n<-1|n[,...]> Specify V4L2 minor mode number."
66 "\n -1 = use next available (default)"
67 "\n n = use minor number n (integer >= 0)"
68 "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
71 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
72 "\nthe second camera and use auto for the first"
73 "\none and for every other camera."
76 static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
77 SN9C102_FORCE_MUNMAP};
78 module_param_array(force_munmap, bool, NULL, 0444);
79 MODULE_PARM_DESC(force_munmap,
80 "\n<0|1[,...]> Force the application to unmap previously"
81 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
82 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
83 "\nthis feature. This parameter is specific for each"
85 "\n 0 = do not force memory unmapping"
86 "\n 1 = force memory unmapping (save memory)"
87 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
90 static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] =
91 SN9C102_FRAME_TIMEOUT};
92 module_param_array(frame_timeout, uint, NULL, 0644);
93 MODULE_PARM_DESC(frame_timeout,
94 "\n<n[,...]> Timeout for a video frame in seconds."
95 "\nThis parameter is specific for each detected camera."
96 "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"."
100 static unsigned short debug = SN9C102_DEBUG_LEVEL;
101 module_param(debug, ushort, 0644);
102 MODULE_PARM_DESC(debug,
103 "\n<n> Debugging information level, from 0 to 3:"
104 "\n0 = none (use carefully)"
105 "\n1 = critical errors"
106 "\n2 = significant informations"
107 "\n3 = more verbose messages"
108 "\nLevel 3 is useful for testing only, when only "
109 "one device is used."
110 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
114 /*****************************************************************************/
116 static sn9c102_sof_header_t sn9c102_sof_header[] = {
117 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x00},
118 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01},
121 static sn9c103_sof_header_t sn9c103_sof_header[] = {
122 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x20},
125 static sn9c102_eof_header_t sn9c102_eof_header[] = {
126 {0x00, 0x00, 0x00, 0x00},
127 {0x40, 0x00, 0x00, 0x00},
128 {0x80, 0x00, 0x00, 0x00},
129 {0xc0, 0x00, 0x00, 0x00},
132 /*****************************************************************************/
135 sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
136 enum sn9c102_io_method io)
138 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
139 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
140 const size_t imagesize = cam->module_param.force_munmap ||
142 (p->width * p->height * p->priv) / 8 :
143 (r->width * r->height * p->priv) / 8;
147 if (count > SN9C102_MAX_FRAMES)
148 count = SN9C102_MAX_FRAMES;
150 cam->nbuffers = count;
151 while (cam->nbuffers > 0) {
152 if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
157 for (i = 0; i < cam->nbuffers; i++) {
158 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
159 cam->frame[i].buf.index = i;
160 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
161 cam->frame[i].buf.length = imagesize;
162 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
163 cam->frame[i].buf.sequence = 0;
164 cam->frame[i].buf.field = V4L2_FIELD_NONE;
165 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
166 cam->frame[i].buf.flags = 0;
169 return cam->nbuffers;
173 static void sn9c102_release_buffers(struct sn9c102_device* cam)
176 vfree(cam->frame[0].bufmem);
179 cam->frame_current = NULL;
183 static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
187 INIT_LIST_HEAD(&cam->inqueue);
188 INIT_LIST_HEAD(&cam->outqueue);
190 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
191 cam->frame[i].state = F_UNUSED;
192 cam->frame[i].buf.bytesused = 0;
197 static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
199 struct sn9c102_frame_t *i;
201 list_for_each_entry(i, &cam->outqueue, frame) {
203 list_add(&i->frame, &cam->inqueue);
206 INIT_LIST_HEAD(&cam->outqueue);
210 static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
212 unsigned long lock_flags;
215 for (i = 0; i < cam->nbuffers; i++)
216 if (cam->frame[i].state == F_UNUSED) {
217 cam->frame[i].state = F_QUEUED;
218 spin_lock_irqsave(&cam->queue_lock, lock_flags);
219 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
220 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
224 /*****************************************************************************/
226 int sn9c102_write_regs(struct sn9c102_device* cam, u8* buff, u16 index)
228 struct usb_device* udev = cam->usbdev;
231 if (index + sizeof(buff) >= ARRAY_SIZE(cam->reg))
234 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
235 index, 0, buff, sizeof(buff),
236 SN9C102_CTRL_TIMEOUT*sizeof(buff));
238 DBG(3, "Failed to write registers (index 0x%02X, error %d)",
243 for (i = 0; i < sizeof(buff); i++)
244 cam->reg[index+i] = buff[i];
250 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
252 struct usb_device* udev = cam->usbdev;
253 u8* buff = cam->control_buffer;
256 if (index >= ARRAY_SIZE(cam->reg))
261 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
262 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
264 DBG(3, "Failed to write a register (value 0x%02X, index "
265 "0x%02X, error %d)", value, index, res);
269 cam->reg[index] = value;
275 /* NOTE: reading some registers always returns 0 */
276 static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
278 struct usb_device* udev = cam->usbdev;
279 u8* buff = cam->control_buffer;
282 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
283 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
285 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
288 return (res >= 0) ? (int)(*buff) : -1;
292 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
294 if (index >= ARRAY_SIZE(cam->reg))
297 return cam->reg[index];
302 sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
306 for (i = 1; i <= 5; i++) {
307 r = sn9c102_read_reg(cam, 0x08);
312 if (sensor->frequency & SN9C102_I2C_400KHZ)
322 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
323 struct sn9c102_sensor* sensor)
326 r = sn9c102_read_reg(cam, 0x08);
327 return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0;
332 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
333 struct sn9c102_sensor* sensor)
336 r = sn9c102_read_reg(cam, 0x08);
337 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
342 sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
343 struct sn9c102_sensor* sensor, u8 data0, u8 data1,
346 struct usb_device* udev = cam->usbdev;
347 u8* data = cam->control_buffer;
351 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
352 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
353 data[1] = data0; /* I2C slave id */
354 data[2] = data1; /* address */
356 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
357 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
361 err += sn9c102_i2c_wait(cam, sensor);
363 /* Read cycle - n bytes */
364 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
365 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
369 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
370 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
374 err += sn9c102_i2c_wait(cam, sensor);
376 /* The first read byte will be placed in data[4] */
377 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
378 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
382 err += sn9c102_i2c_detect_read_error(cam, sensor);
384 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
388 DBG(3, "I2C read failed for %s image sensor", sensor->name);
393 memcpy(buffer, data, sizeof(buffer));
400 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
401 struct sn9c102_sensor* sensor, u8 n, u8 data0,
402 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
404 struct usb_device* udev = cam->usbdev;
405 u8* data = cam->control_buffer;
408 /* Write cycle. It usually is address + value */
409 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
410 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
419 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
420 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
424 err += sn9c102_i2c_wait(cam, sensor);
425 err += sn9c102_i2c_detect_write_error(cam, sensor);
428 DBG(3, "I2C write failed for %s image sensor", sensor->name);
430 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
431 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
432 n, data0, data1, data2, data3, data4, data5);
439 sn9c102_i2c_try_read(struct sn9c102_device* cam,
440 struct sn9c102_sensor* sensor, u8 address)
442 return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
448 sn9c102_i2c_try_write(struct sn9c102_device* cam,
449 struct sn9c102_sensor* sensor, u8 address, u8 value)
451 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
452 sensor->i2c_slave_id, address,
457 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
459 return sn9c102_i2c_try_read(cam, &cam->sensor, address);
463 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
465 return sn9c102_i2c_try_write(cam, &cam->sensor, address, value);
468 /*****************************************************************************/
471 sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
473 size_t soflen = 0, i;
476 switch (cam->bridge) {
479 soflen = sizeof(sn9c102_sof_header_t);
480 n = sizeof(sn9c102_sof_header) / soflen;
483 soflen = sizeof(sn9c103_sof_header_t);
484 n = sizeof(sn9c103_sof_header) / soflen;
487 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
488 for (j = 0; j < n; j++)
489 /* The invariable part of the header is 6 bytes long */
490 if ((cam->bridge != BRIDGE_SN9C103 &&
491 !memcmp(mem + i, sn9c102_sof_header[j], 6)) ||
492 (cam->bridge == BRIDGE_SN9C103 &&
493 !memcmp(mem + i, sn9c103_sof_header[j], 6))) {
494 memcpy(cam->sof_header, mem + i, soflen);
495 /* Skip the header */
496 return mem + i + soflen;
504 sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
506 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
507 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
509 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
510 return NULL; /* EOF header does not exist in compressed data */
512 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
513 for (j = 0; j < n; j++)
514 if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen))
521 static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
523 struct sn9c102_device* cam = urb->context;
524 struct sn9c102_frame_t** f;
525 size_t imagesize, soflen;
529 if (urb->status == -ENOENT)
532 f = &cam->frame_current;
534 if (cam->stream == STREAM_INTERRUPT) {
535 cam->stream = STREAM_OFF;
537 (*f)->state = F_QUEUED;
538 DBG(3, "Stream interrupted");
539 wake_up(&cam->wait_stream);
542 if (cam->state & DEV_DISCONNECTED)
545 if (cam->state & DEV_MISCONFIGURED) {
546 wake_up_interruptible(&cam->wait_frame);
550 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
554 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
557 imagesize = (cam->sensor.pix_format.width *
558 cam->sensor.pix_format.height *
559 cam->sensor.pix_format.priv) / 8;
561 soflen = (cam->bridge) == BRIDGE_SN9C103 ?
562 sizeof(sn9c103_sof_header_t) :
563 sizeof(sn9c102_sof_header_t);
565 for (i = 0; i < urb->number_of_packets; i++) {
566 unsigned int img, len, status;
567 void *pos, *sof, *eof;
569 len = urb->iso_frame_desc[i].actual_length;
570 status = urb->iso_frame_desc[i].status;
571 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
574 DBG(3, "Error in isochronous frame");
575 (*f)->state = F_ERROR;
579 PDBGG("Isochrnous frame: length %u, #%u i", len, i);
582 sof = sn9c102_find_sof_header(cam, pos, len);
584 eof = sn9c102_find_eof_header(cam, pos, len);
585 if ((*f)->state == F_GRABBING) {
590 img = (eof > pos) ? eof - pos - 1 : 0;
592 if ((*f)->buf.bytesused+img > imagesize) {
594 b = (*f)->buf.bytesused + img -
596 img = imagesize - (*f)->buf.bytesused;
597 DBG(3, "Expected EOF not found: "
600 DBG(3, "Exceeded limit: +%u "
601 "bytes", (unsigned)(b));
604 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
607 if ((*f)->buf.bytesused == 0)
608 do_gettimeofday(&(*f)->buf.timestamp);
610 (*f)->buf.bytesused += img;
612 if ((*f)->buf.bytesused == imagesize ||
613 (cam->sensor.pix_format.pixelformat ==
614 V4L2_PIX_FMT_SN9C10X && eof)) {
616 b = (*f)->buf.bytesused;
617 (*f)->state = F_DONE;
618 (*f)->buf.sequence= ++cam->frame_count;
619 spin_lock(&cam->queue_lock);
620 list_move_tail(&(*f)->frame,
622 if (!list_empty(&cam->inqueue))
625 struct sn9c102_frame_t,
629 spin_unlock(&cam->queue_lock);
630 memcpy(cam->sysfs.frame_header,
631 cam->sof_header, soflen);
632 DBG(3, "Video frame captured: %lu "
633 "bytes", (unsigned long)(b));
639 (*f)->state = F_ERROR;
640 DBG(3, "Not expected EOF after %lu "
641 "bytes of image data",
643 ((*f)->buf.bytesused));
650 DBG(3, "EOF without SOF");
654 PDBGG("Ignoring pointless isochronous frame");
658 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
660 (*f)->state = F_GRABBING;
661 (*f)->buf.bytesused = 0;
664 DBG(3, "SOF detected: new video frame");
668 } else if ((*f)->state == F_GRABBING) {
669 eof = sn9c102_find_eof_header(cam, pos, len);
670 if (eof && eof < sof)
671 goto end_of_frame; /* (1) */
673 if (cam->sensor.pix_format.pixelformat ==
674 V4L2_PIX_FMT_SN9C10X) {
678 DBG(3, "SOF before expected EOF after "
679 "%lu bytes of image data",
681 ((*f)->buf.bytesused));
689 urb->dev = cam->usbdev;
690 err = usb_submit_urb(urb, GFP_ATOMIC);
691 if (err < 0 && err != -EPERM) {
692 cam->state |= DEV_MISCONFIGURED;
693 DBG(1, "usb_submit_urb() failed");
696 wake_up_interruptible(&cam->wait_frame);
700 static int sn9c102_start_transfer(struct sn9c102_device* cam)
702 struct usb_device *udev = cam->usbdev;
704 const unsigned int sn9c102_wMaxPacketSize[] = {0, 128, 256, 384, 512,
705 680, 800, 900, 1023};
706 const unsigned int sn9c103_wMaxPacketSize[] = {0, 128, 256, 384, 512,
707 680, 800, 900, 1003};
708 const unsigned int psz = (cam->bridge == BRIDGE_SN9C103) ?
709 sn9c103_wMaxPacketSize[SN9C102_ALTERNATE_SETTING] :
710 sn9c102_wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
714 for (i = 0; i < SN9C102_URBS; i++) {
715 cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
717 if (!cam->transfer_buffer[i]) {
719 DBG(1, "Not enough memory");
724 for (i = 0; i < SN9C102_URBS; i++) {
725 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
729 DBG(1, "usb_alloc_urb() failed");
734 urb->pipe = usb_rcvisocpipe(udev, 1);
735 urb->transfer_flags = URB_ISO_ASAP;
736 urb->number_of_packets = SN9C102_ISO_PACKETS;
737 urb->complete = sn9c102_urb_complete;
738 urb->transfer_buffer = cam->transfer_buffer[i];
739 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
741 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
742 urb->iso_frame_desc[j].offset = psz * j;
743 urb->iso_frame_desc[j].length = psz;
748 if (!(cam->reg[0x01] & 0x04)) {
749 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
752 DBG(1, "I/O hardware error");
757 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
759 DBG(1, "usb_set_interface() failed");
763 cam->frame_current = NULL;
765 for (i = 0; i < SN9C102_URBS; i++) {
766 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
768 for (j = i-1; j >= 0; j--)
769 usb_kill_urb(cam->urb[j]);
770 DBG(1, "usb_submit_urb() failed, error %d", err);
778 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
779 usb_free_urb(cam->urb[i]);
782 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
783 kfree(cam->transfer_buffer[i]);
789 static int sn9c102_stop_transfer(struct sn9c102_device* cam)
791 struct usb_device *udev = cam->usbdev;
795 if (cam->state & DEV_DISCONNECTED)
798 for (i = SN9C102_URBS-1; i >= 0; i--) {
799 usb_kill_urb(cam->urb[i]);
800 usb_free_urb(cam->urb[i]);
801 kfree(cam->transfer_buffer[i]);
804 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
806 DBG(3, "usb_set_interface() failed");
812 static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
816 cam->stream = STREAM_INTERRUPT;
817 timeout = wait_event_timeout(cam->wait_stream,
818 (cam->stream == STREAM_OFF) ||
819 (cam->state & DEV_DISCONNECTED),
820 SN9C102_URB_TIMEOUT);
821 if (cam->state & DEV_DISCONNECTED)
823 else if (cam->stream != STREAM_OFF) {
824 cam->state |= DEV_MISCONFIGURED;
825 DBG(1, "URB timeout reached. The camera is misconfigured. "
826 "To use it, close and open /dev/video%d again.",
834 /*****************************************************************************/
836 #ifdef CONFIG_VIDEO_ADV_DEBUG
837 static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
844 strncpy(str, buff, len);
847 strncpy(str, buff, 4);
851 val = simple_strtoul(str, &endp, 0);
855 *count = (ssize_t)(endp - str);
856 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
863 NOTE 1: being inside one of the following methods implies that the v4l
864 device exists for sure (see kobjects and reference counters)
865 NOTE 2: buffers are PAGE_SIZE long
868 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
870 struct sn9c102_device* cam;
873 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
876 cam = video_get_drvdata(to_video_device(cd));
878 mutex_unlock(&sn9c102_sysfs_lock);
882 count = sprintf(buf, "%u\n", cam->sysfs.reg);
884 mutex_unlock(&sn9c102_sysfs_lock);
891 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
893 struct sn9c102_device* cam;
897 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
900 cam = video_get_drvdata(to_video_device(cd));
902 mutex_unlock(&sn9c102_sysfs_lock);
906 index = sn9c102_strtou8(buf, len, &count);
907 if (index > 0x1f || !count) {
908 mutex_unlock(&sn9c102_sysfs_lock);
912 cam->sysfs.reg = index;
914 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg);
915 DBG(3, "Written bytes: %zd", count);
917 mutex_unlock(&sn9c102_sysfs_lock);
923 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
925 struct sn9c102_device* cam;
929 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
932 cam = video_get_drvdata(to_video_device(cd));
934 mutex_unlock(&sn9c102_sysfs_lock);
938 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
939 mutex_unlock(&sn9c102_sysfs_lock);
943 count = sprintf(buf, "%d\n", val);
945 DBG(3, "Read bytes: %zd", count);
947 mutex_unlock(&sn9c102_sysfs_lock);
954 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
956 struct sn9c102_device* cam;
961 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
964 cam = video_get_drvdata(to_video_device(cd));
966 mutex_unlock(&sn9c102_sysfs_lock);
970 value = sn9c102_strtou8(buf, len, &count);
972 mutex_unlock(&sn9c102_sysfs_lock);
976 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
978 mutex_unlock(&sn9c102_sysfs_lock);
982 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
983 cam->sysfs.reg, value);
984 DBG(3, "Written bytes: %zd", count);
986 mutex_unlock(&sn9c102_sysfs_lock);
992 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
994 struct sn9c102_device* cam;
997 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1000 cam = video_get_drvdata(to_video_device(cd));
1002 mutex_unlock(&sn9c102_sysfs_lock);
1006 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
1008 DBG(3, "Read bytes: %zd", count);
1010 mutex_unlock(&sn9c102_sysfs_lock);
1017 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1019 struct sn9c102_device* cam;
1023 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1024 return -ERESTARTSYS;
1026 cam = video_get_drvdata(to_video_device(cd));
1028 mutex_unlock(&sn9c102_sysfs_lock);
1032 index = sn9c102_strtou8(buf, len, &count);
1034 mutex_unlock(&sn9c102_sysfs_lock);
1038 cam->sysfs.i2c_reg = index;
1040 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1041 DBG(3, "Written bytes: %zd", count);
1043 mutex_unlock(&sn9c102_sysfs_lock);
1049 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1051 struct sn9c102_device* cam;
1055 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1056 return -ERESTARTSYS;
1058 cam = video_get_drvdata(to_video_device(cd));
1060 mutex_unlock(&sn9c102_sysfs_lock);
1064 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) {
1065 mutex_unlock(&sn9c102_sysfs_lock);
1069 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1070 mutex_unlock(&sn9c102_sysfs_lock);
1074 count = sprintf(buf, "%d\n", val);
1076 DBG(3, "Read bytes: %zd", count);
1078 mutex_unlock(&sn9c102_sysfs_lock);
1085 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1087 struct sn9c102_device* cam;
1092 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1093 return -ERESTARTSYS;
1095 cam = video_get_drvdata(to_video_device(cd));
1097 mutex_unlock(&sn9c102_sysfs_lock);
1101 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) {
1102 mutex_unlock(&sn9c102_sysfs_lock);
1106 value = sn9c102_strtou8(buf, len, &count);
1108 mutex_unlock(&sn9c102_sysfs_lock);
1112 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1114 mutex_unlock(&sn9c102_sysfs_lock);
1118 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1119 cam->sysfs.i2c_reg, value);
1120 DBG(3, "Written bytes: %zd", count);
1122 mutex_unlock(&sn9c102_sysfs_lock);
1129 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1131 struct sn9c102_device* cam;
1132 enum sn9c102_bridge bridge;
1137 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1138 return -ERESTARTSYS;
1140 cam = video_get_drvdata(to_video_device(cd));
1142 mutex_unlock(&sn9c102_sysfs_lock);
1146 bridge = cam->bridge;
1148 mutex_unlock(&sn9c102_sysfs_lock);
1150 value = sn9c102_strtou8(buf, len, &count);
1155 case BRIDGE_SN9C101:
1156 case BRIDGE_SN9C102:
1159 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1160 res = sn9c102_store_val(cd, buf, len);
1162 case BRIDGE_SN9C103:
1165 if ((res = sn9c102_store_reg(cd, "0x04", 4)) >= 0)
1166 res = sn9c102_store_val(cd, buf, len);
1175 sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1181 value = sn9c102_strtou8(buf, len, &count);
1182 if (!count || value > 0x7f)
1185 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1186 res = sn9c102_store_val(cd, buf, len);
1193 sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1199 value = sn9c102_strtou8(buf, len, &count);
1200 if (!count || value > 0x7f)
1203 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1204 res = sn9c102_store_val(cd, buf, len);
1210 static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1212 struct sn9c102_device* cam;
1215 cam = video_get_drvdata(to_video_device(cd));
1219 count = sizeof(cam->sysfs.frame_header);
1220 memcpy(buf, cam->sysfs.frame_header, count);
1222 DBG(3, "Frame header, read bytes: %zd", count);
1228 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1229 sn9c102_show_reg, sn9c102_store_reg);
1230 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1231 sn9c102_show_val, sn9c102_store_val);
1232 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1233 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1234 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1235 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1236 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1237 static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1238 static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1239 static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
1240 sn9c102_show_frame_header, NULL);
1243 static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1245 struct video_device *v4ldev = cam->v4ldev;
1247 video_device_create_file(v4ldev, &class_device_attr_reg);
1248 video_device_create_file(v4ldev, &class_device_attr_val);
1249 video_device_create_file(v4ldev, &class_device_attr_frame_header);
1250 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
1251 video_device_create_file(v4ldev, &class_device_attr_green);
1252 else if (cam->bridge == BRIDGE_SN9C103) {
1253 video_device_create_file(v4ldev, &class_device_attr_blue);
1254 video_device_create_file(v4ldev, &class_device_attr_red);
1256 if (cam->sensor.sysfs_ops) {
1257 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1258 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1261 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1263 /*****************************************************************************/
1266 sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
1270 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
1271 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, 0x18);
1273 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, 0x18);
1275 return err ? -EIO : 0;
1280 sn9c102_set_compression(struct sn9c102_device* cam,
1281 struct v4l2_jpegcompression* compression)
1285 if (compression->quality == 0)
1286 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, 0x17);
1287 else if (compression->quality == 1)
1288 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, 0x17);
1290 return err ? -EIO : 0;
1294 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1300 r = cam->reg[0x18] & 0xcf;
1301 else if (scale == 2) {
1302 r = cam->reg[0x18] & 0xcf;
1304 } else if (scale == 4)
1305 r = cam->reg[0x18] | 0x20;
1307 err += sn9c102_write_reg(cam, r, 0x18);
1311 PDBGG("Scaling factor: %u", scale);
1317 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1319 struct sn9c102_sensor* s = &cam->sensor;
1320 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1321 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1322 h_size = (u8)(rect->width / 16),
1323 v_size = (u8)(rect->height / 16);
1326 err += sn9c102_write_reg(cam, h_start, 0x12);
1327 err += sn9c102_write_reg(cam, v_start, 0x13);
1328 err += sn9c102_write_reg(cam, h_size, 0x15);
1329 err += sn9c102_write_reg(cam, v_size, 0x16);
1333 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1334 "%u %u %u %u", h_start, v_start, h_size, v_size);
1340 static int sn9c102_init(struct sn9c102_device* cam)
1342 struct sn9c102_sensor* s = &cam->sensor;
1343 struct v4l2_control ctrl;
1344 struct v4l2_queryctrl *qctrl;
1345 struct v4l2_rect* rect;
1349 if (!(cam->state & DEV_INITIALIZED)) {
1350 init_waitqueue_head(&cam->open);
1352 rect = &(s->cropcap.defrect);
1353 } else { /* use current values */
1358 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1359 err += sn9c102_set_crop(cam, rect);
1366 DBG(3, "Sensor initialization failed");
1371 if (!(cam->state & DEV_INITIALIZED))
1372 cam->compression.quality = cam->reg[0x17] & 0x01 ? 0 : 1;
1374 err += sn9c102_set_compression(cam, &cam->compression);
1375 err += sn9c102_set_pix_format(cam, &s->pix_format);
1376 if (s->set_pix_format)
1377 err += s->set_pix_format(cam, &s->pix_format);
1381 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1382 DBG(3, "Compressed video format is active, quality %d",
1383 cam->compression.quality);
1385 DBG(3, "Uncompressed video format is active");
1388 if ((err = s->set_crop(cam, rect))) {
1389 DBG(3, "set_crop() failed");
1394 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1395 if (s->qctrl[i].id != 0 &&
1396 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1397 ctrl.id = s->qctrl[i].id;
1398 ctrl.value = qctrl[i].default_value;
1399 err = s->set_ctrl(cam, &ctrl);
1401 DBG(3, "Set %s control failed",
1405 DBG(3, "Image sensor supports '%s' control",
1410 if (!(cam->state & DEV_INITIALIZED)) {
1411 mutex_init(&cam->fileop_mutex);
1412 spin_lock_init(&cam->queue_lock);
1413 init_waitqueue_head(&cam->wait_frame);
1414 init_waitqueue_head(&cam->wait_stream);
1415 cam->nreadbuffers = 2;
1416 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1417 memcpy(&(s->_rect), &(s->cropcap.defrect),
1418 sizeof(struct v4l2_rect));
1419 cam->state |= DEV_INITIALIZED;
1422 DBG(2, "Initialization succeeded");
1427 static void sn9c102_release_resources(struct sn9c102_device* cam)
1429 mutex_lock(&sn9c102_sysfs_lock);
1431 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1432 video_set_drvdata(cam->v4ldev, NULL);
1433 video_unregister_device(cam->v4ldev);
1435 usb_put_dev(cam->usbdev);
1437 mutex_unlock(&sn9c102_sysfs_lock);
1439 kfree(cam->control_buffer);
1442 /*****************************************************************************/
1444 static int sn9c102_open(struct inode* inode, struct file* filp)
1446 struct sn9c102_device* cam;
1450 This is the only safe way to prevent race conditions with
1453 if (!down_read_trylock(&sn9c102_disconnect))
1454 return -ERESTARTSYS;
1456 cam = video_get_drvdata(video_devdata(filp));
1458 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1459 up_read(&sn9c102_disconnect);
1460 return -ERESTARTSYS;
1464 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1465 if ((filp->f_flags & O_NONBLOCK) ||
1466 (filp->f_flags & O_NDELAY)) {
1470 mutex_unlock(&cam->dev_mutex);
1471 err = wait_event_interruptible_exclusive(cam->open,
1472 cam->state & DEV_DISCONNECTED
1475 up_read(&sn9c102_disconnect);
1478 if (cam->state & DEV_DISCONNECTED) {
1479 up_read(&sn9c102_disconnect);
1482 mutex_lock(&cam->dev_mutex);
1486 if (cam->state & DEV_MISCONFIGURED) {
1487 err = sn9c102_init(cam);
1489 DBG(1, "Initialization failed again. "
1490 "I will retry on next open().");
1493 cam->state &= ~DEV_MISCONFIGURED;
1496 if ((err = sn9c102_start_transfer(cam)))
1499 filp->private_data = cam;
1502 cam->stream = STREAM_OFF;
1504 cam->frame_count = 0;
1505 sn9c102_empty_framequeues(cam);
1507 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1510 mutex_unlock(&cam->dev_mutex);
1511 up_read(&sn9c102_disconnect);
1516 static int sn9c102_release(struct inode* inode, struct file* filp)
1518 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1520 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1522 sn9c102_stop_transfer(cam);
1524 sn9c102_release_buffers(cam);
1526 if (cam->state & DEV_DISCONNECTED) {
1527 sn9c102_release_resources(cam);
1528 mutex_unlock(&cam->dev_mutex);
1534 wake_up_interruptible_nr(&cam->open, 1);
1536 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1538 mutex_unlock(&cam->dev_mutex);
1545 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1547 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1548 struct sn9c102_frame_t* f, * i;
1549 unsigned long lock_flags;
1553 if (mutex_lock_interruptible(&cam->fileop_mutex))
1554 return -ERESTARTSYS;
1556 if (cam->state & DEV_DISCONNECTED) {
1557 DBG(1, "Device not present");
1558 mutex_unlock(&cam->fileop_mutex);
1562 if (cam->state & DEV_MISCONFIGURED) {
1563 DBG(1, "The camera is misconfigured. Close and open it "
1565 mutex_unlock(&cam->fileop_mutex);
1569 if (cam->io == IO_MMAP) {
1570 DBG(3, "Close and open the device again to choose "
1572 mutex_unlock(&cam->fileop_mutex);
1576 if (cam->io == IO_NONE) {
1577 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1578 DBG(1, "read() failed, not enough memory");
1579 mutex_unlock(&cam->fileop_mutex);
1583 cam->stream = STREAM_ON;
1586 if (list_empty(&cam->inqueue)) {
1587 if (!list_empty(&cam->outqueue))
1588 sn9c102_empty_framequeues(cam);
1589 sn9c102_queue_unusedframes(cam);
1593 mutex_unlock(&cam->fileop_mutex);
1597 if (list_empty(&cam->outqueue)) {
1598 if (filp->f_flags & O_NONBLOCK) {
1599 mutex_unlock(&cam->fileop_mutex);
1602 timeout = wait_event_interruptible_timeout
1604 (!list_empty(&cam->outqueue)) ||
1605 (cam->state & DEV_DISCONNECTED) ||
1606 (cam->state & DEV_MISCONFIGURED),
1607 cam->module_param.frame_timeout *
1608 1000 * msecs_to_jiffies(1) );
1610 mutex_unlock(&cam->fileop_mutex);
1613 if (cam->state & DEV_DISCONNECTED) {
1614 mutex_unlock(&cam->fileop_mutex);
1617 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
1618 mutex_unlock(&cam->fileop_mutex);
1623 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1625 if (count > f->buf.bytesused)
1626 count = f->buf.bytesused;
1628 if (copy_to_user(buf, f->bufmem, count)) {
1635 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1636 list_for_each_entry(i, &cam->outqueue, frame)
1637 i->state = F_UNUSED;
1638 INIT_LIST_HEAD(&cam->outqueue);
1639 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1641 sn9c102_queue_unusedframes(cam);
1643 PDBGG("Frame #%lu, bytes read: %zu",
1644 (unsigned long)f->buf.index, count);
1646 mutex_unlock(&cam->fileop_mutex);
1652 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1654 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1655 struct sn9c102_frame_t* f;
1656 unsigned long lock_flags;
1657 unsigned int mask = 0;
1659 if (mutex_lock_interruptible(&cam->fileop_mutex))
1662 if (cam->state & DEV_DISCONNECTED) {
1663 DBG(1, "Device not present");
1667 if (cam->state & DEV_MISCONFIGURED) {
1668 DBG(1, "The camera is misconfigured. Close and open it "
1673 if (cam->io == IO_NONE) {
1674 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1676 DBG(1, "poll() failed, not enough memory");
1680 cam->stream = STREAM_ON;
1683 if (cam->io == IO_READ) {
1684 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1685 list_for_each_entry(f, &cam->outqueue, frame)
1686 f->state = F_UNUSED;
1687 INIT_LIST_HEAD(&cam->outqueue);
1688 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1689 sn9c102_queue_unusedframes(cam);
1692 poll_wait(filp, &cam->wait_frame, wait);
1694 if (!list_empty(&cam->outqueue))
1695 mask |= POLLIN | POLLRDNORM;
1697 mutex_unlock(&cam->fileop_mutex);
1702 mutex_unlock(&cam->fileop_mutex);
1707 static void sn9c102_vm_open(struct vm_area_struct* vma)
1709 struct sn9c102_frame_t* f = vma->vm_private_data;
1714 static void sn9c102_vm_close(struct vm_area_struct* vma)
1716 /* NOTE: buffers are not freed here */
1717 struct sn9c102_frame_t* f = vma->vm_private_data;
1722 static struct vm_operations_struct sn9c102_vm_ops = {
1723 .open = sn9c102_vm_open,
1724 .close = sn9c102_vm_close,
1728 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1730 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1731 unsigned long size = vma->vm_end - vma->vm_start,
1732 start = vma->vm_start;
1736 if (mutex_lock_interruptible(&cam->fileop_mutex))
1737 return -ERESTARTSYS;
1739 if (cam->state & DEV_DISCONNECTED) {
1740 DBG(1, "Device not present");
1741 mutex_unlock(&cam->fileop_mutex);
1745 if (cam->state & DEV_MISCONFIGURED) {
1746 DBG(1, "The camera is misconfigured. Close and open it "
1748 mutex_unlock(&cam->fileop_mutex);
1752 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1753 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1754 mutex_unlock(&cam->fileop_mutex);
1758 for (i = 0; i < cam->nbuffers; i++) {
1759 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1762 if (i == cam->nbuffers) {
1763 mutex_unlock(&cam->fileop_mutex);
1767 vma->vm_flags |= VM_IO;
1768 vma->vm_flags |= VM_RESERVED;
1770 pos = cam->frame[i].bufmem;
1771 while (size > 0) { /* size is page-aligned */
1772 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1773 mutex_unlock(&cam->fileop_mutex);
1781 vma->vm_ops = &sn9c102_vm_ops;
1782 vma->vm_private_data = &cam->frame[i];
1784 sn9c102_vm_open(vma);
1786 mutex_unlock(&cam->fileop_mutex);
1791 /*****************************************************************************/
1794 sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
1796 struct v4l2_capability cap = {
1797 .driver = "sn9c102",
1798 .version = SN9C102_MODULE_VERSION_CODE,
1799 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1803 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1804 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1805 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1806 sizeof(cap.bus_info));
1808 if (copy_to_user(arg, &cap, sizeof(cap)))
1816 sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
1818 struct v4l2_input i;
1820 if (copy_from_user(&i, arg, sizeof(i)))
1826 memset(&i, 0, sizeof(i));
1827 strcpy(i.name, "Camera");
1828 i.type = V4L2_INPUT_TYPE_CAMERA;
1830 if (copy_to_user(arg, &i, sizeof(i)))
1838 sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg)
1842 if (copy_to_user(arg, &index, sizeof(index)))
1850 sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg)
1854 if (copy_from_user(&index, arg, sizeof(index)))
1865 sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
1867 struct sn9c102_sensor* s = &cam->sensor;
1868 struct v4l2_queryctrl qc;
1871 if (copy_from_user(&qc, arg, sizeof(qc)))
1874 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1875 if (qc.id && qc.id == s->qctrl[i].id) {
1876 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1877 if (copy_to_user(arg, &qc, sizeof(qc)))
1887 sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
1889 struct sn9c102_sensor* s = &cam->sensor;
1890 struct v4l2_control ctrl;
1894 if (!s->get_ctrl && !s->set_ctrl)
1897 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1901 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1902 if (ctrl.id && ctrl.id == s->qctrl[i].id) {
1903 ctrl.value = s->_qctrl[i].default_value;
1908 err = s->get_ctrl(cam, &ctrl);
1911 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1919 sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1921 struct sn9c102_sensor* s = &cam->sensor;
1922 struct v4l2_control ctrl;
1929 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1932 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1933 if (ctrl.id == s->qctrl[i].id) {
1934 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1936 if (ctrl.value < s->qctrl[i].minimum ||
1937 ctrl.value > s->qctrl[i].maximum)
1939 ctrl.value -= ctrl.value % s->qctrl[i].step;
1943 if ((err = s->set_ctrl(cam, &ctrl)))
1946 s->_qctrl[i].default_value = ctrl.value;
1948 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
1949 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
1956 sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
1958 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1960 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1961 cc->pixelaspect.numerator = 1;
1962 cc->pixelaspect.denominator = 1;
1964 if (copy_to_user(arg, cc, sizeof(*cc)))
1972 sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
1974 struct sn9c102_sensor* s = &cam->sensor;
1975 struct v4l2_crop crop = {
1976 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1979 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1981 if (copy_to_user(arg, &crop, sizeof(crop)))
1989 sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
1991 struct sn9c102_sensor* s = &cam->sensor;
1992 struct v4l2_crop crop;
1993 struct v4l2_rect* rect;
1994 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1995 struct v4l2_pix_format* pix_format = &(s->pix_format);
1997 const enum sn9c102_stream_state stream = cam->stream;
1998 const u32 nbuffers = cam->nbuffers;
2002 if (copy_from_user(&crop, arg, sizeof(crop)))
2007 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2010 if (cam->module_param.force_munmap)
2011 for (i = 0; i < cam->nbuffers; i++)
2012 if (cam->frame[i].vma_use_count) {
2013 DBG(3, "VIDIOC_S_CROP failed. "
2014 "Unmap the buffers first.");
2018 /* Preserve R,G or B origin */
2019 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
2020 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
2022 if (rect->width < 16)
2024 if (rect->height < 16)
2026 if (rect->width > bounds->width)
2027 rect->width = bounds->width;
2028 if (rect->height > bounds->height)
2029 rect->height = bounds->height;
2030 if (rect->left < bounds->left)
2031 rect->left = bounds->left;
2032 if (rect->top < bounds->top)
2033 rect->top = bounds->top;
2034 if (rect->left + rect->width > bounds->left + bounds->width)
2035 rect->left = bounds->left+bounds->width - rect->width;
2036 if (rect->top + rect->height > bounds->top + bounds->height)
2037 rect->top = bounds->top+bounds->height - rect->height;
2039 rect->width &= ~15L;
2040 rect->height &= ~15L;
2042 if (SN9C102_PRESERVE_IMGSCALE) {
2043 /* Calculate the actual scaling factor */
2045 a = rect->width * rect->height;
2046 b = pix_format->width * pix_format->height;
2047 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2051 if (cam->stream == STREAM_ON)
2052 if ((err = sn9c102_stream_interrupt(cam)))
2055 if (copy_to_user(arg, &crop, sizeof(crop))) {
2056 cam->stream = stream;
2060 if (cam->module_param.force_munmap || cam->io == IO_READ)
2061 sn9c102_release_buffers(cam);
2063 err = sn9c102_set_crop(cam, rect);
2065 err += s->set_crop(cam, rect);
2066 err += sn9c102_set_scale(cam, scale);
2068 if (err) { /* atomic, no rollback in ioctl() */
2069 cam->state |= DEV_MISCONFIGURED;
2070 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2071 "use the camera, close and open /dev/video%d again.",
2072 cam->v4ldev->minor);
2076 s->pix_format.width = rect->width/scale;
2077 s->pix_format.height = rect->height/scale;
2078 memcpy(&(s->_rect), rect, sizeof(*rect));
2080 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2081 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2082 cam->state |= DEV_MISCONFIGURED;
2083 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2084 "use the camera, close and open /dev/video%d again.",
2085 cam->v4ldev->minor);
2089 if (cam->io == IO_READ)
2090 sn9c102_empty_framequeues(cam);
2091 else if (cam->module_param.force_munmap)
2092 sn9c102_requeue_outqueue(cam);
2094 cam->stream = stream;
2101 sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
2103 struct v4l2_fmtdesc fmtd;
2105 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
2108 if (fmtd.index == 0) {
2109 strcpy(fmtd.description, "bayer rgb");
2110 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2111 } else if (fmtd.index == 1) {
2112 strcpy(fmtd.description, "compressed");
2113 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2114 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2118 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2119 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2121 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
2129 sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2131 struct v4l2_format format;
2132 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
2134 if (copy_from_user(&format, arg, sizeof(format)))
2137 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2140 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X)
2141 ? 0 : (pfmt->width * pfmt->priv) / 8;
2142 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2143 pfmt->field = V4L2_FIELD_NONE;
2144 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2146 if (copy_to_user(arg, &format, sizeof(format)))
2154 sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2157 struct sn9c102_sensor* s = &cam->sensor;
2158 struct v4l2_format format;
2159 struct v4l2_pix_format* pix;
2160 struct v4l2_pix_format* pfmt = &(s->pix_format);
2161 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2162 struct v4l2_rect rect;
2164 const enum sn9c102_stream_state stream = cam->stream;
2165 const u32 nbuffers = cam->nbuffers;
2169 if (copy_from_user(&format, arg, sizeof(format)))
2172 pix = &(format.fmt.pix);
2174 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2177 memcpy(&rect, &(s->_rect), sizeof(rect));
2179 { /* calculate the actual scaling factor */
2181 a = rect.width * rect.height;
2182 b = pix->width * pix->height;
2183 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2186 rect.width = scale * pix->width;
2187 rect.height = scale * pix->height;
2189 if (rect.width < 16)
2191 if (rect.height < 16)
2193 if (rect.width > bounds->left + bounds->width - rect.left)
2194 rect.width = bounds->left + bounds->width - rect.left;
2195 if (rect.height > bounds->top + bounds->height - rect.top)
2196 rect.height = bounds->top + bounds->height - rect.top;
2199 rect.height &= ~15L;
2201 { /* adjust the scaling factor */
2203 a = rect.width * rect.height;
2204 b = pix->width * pix->height;
2205 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2208 pix->width = rect.width / scale;
2209 pix->height = rect.height / scale;
2211 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2212 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2213 pix->pixelformat = pfmt->pixelformat;
2214 pix->priv = pfmt->priv; /* bpp */
2215 pix->colorspace = pfmt->colorspace;
2216 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
2217 ? 0 : (pix->width * pix->priv) / 8;
2218 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2219 pix->field = V4L2_FIELD_NONE;
2221 if (cmd == VIDIOC_TRY_FMT) {
2222 if (copy_to_user(arg, &format, sizeof(format)))
2227 if (cam->module_param.force_munmap)
2228 for (i = 0; i < cam->nbuffers; i++)
2229 if (cam->frame[i].vma_use_count) {
2230 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2235 if (cam->stream == STREAM_ON)
2236 if ((err = sn9c102_stream_interrupt(cam)))
2239 if (copy_to_user(arg, &format, sizeof(format))) {
2240 cam->stream = stream;
2244 if (cam->module_param.force_munmap || cam->io == IO_READ)
2245 sn9c102_release_buffers(cam);
2247 err += sn9c102_set_pix_format(cam, pix);
2248 err += sn9c102_set_crop(cam, &rect);
2249 if (s->set_pix_format)
2250 err += s->set_pix_format(cam, pix);
2252 err += s->set_crop(cam, &rect);
2253 err += sn9c102_set_scale(cam, scale);
2255 if (err) { /* atomic, no rollback in ioctl() */
2256 cam->state |= DEV_MISCONFIGURED;
2257 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2258 "use the camera, close and open /dev/video%d again.",
2259 cam->v4ldev->minor);
2263 memcpy(pfmt, pix, sizeof(*pix));
2264 memcpy(&(s->_rect), &rect, sizeof(rect));
2266 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2267 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2268 cam->state |= DEV_MISCONFIGURED;
2269 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2270 "use the camera, close and open /dev/video%d again.",
2271 cam->v4ldev->minor);
2275 if (cam->io == IO_READ)
2276 sn9c102_empty_framequeues(cam);
2277 else if (cam->module_param.force_munmap)
2278 sn9c102_requeue_outqueue(cam);
2280 cam->stream = stream;
2287 sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2289 if (copy_to_user(arg, &cam->compression,
2290 sizeof(cam->compression)))
2298 sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2300 struct v4l2_jpegcompression jc;
2301 const enum sn9c102_stream_state stream = cam->stream;
2304 if (copy_from_user(&jc, arg, sizeof(jc)))
2307 if (jc.quality != 0 && jc.quality != 1)
2310 if (cam->stream == STREAM_ON)
2311 if ((err = sn9c102_stream_interrupt(cam)))
2314 err += sn9c102_set_compression(cam, &jc);
2315 if (err) { /* atomic, no rollback in ioctl() */
2316 cam->state |= DEV_MISCONFIGURED;
2317 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2318 "problems. To use the camera, close and open "
2319 "/dev/video%d again.", cam->v4ldev->minor);
2323 cam->compression.quality = jc.quality;
2325 cam->stream = stream;
2332 sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
2334 struct v4l2_requestbuffers rb;
2338 if (copy_from_user(&rb, arg, sizeof(rb)))
2341 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2342 rb.memory != V4L2_MEMORY_MMAP)
2345 if (cam->io == IO_READ) {
2346 DBG(3, "Close and open the device again to choose the mmap "
2351 for (i = 0; i < cam->nbuffers; i++)
2352 if (cam->frame[i].vma_use_count) {
2353 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2358 if (cam->stream == STREAM_ON)
2359 if ((err = sn9c102_stream_interrupt(cam)))
2362 sn9c102_empty_framequeues(cam);
2364 sn9c102_release_buffers(cam);
2366 rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
2368 if (copy_to_user(arg, &rb, sizeof(rb))) {
2369 sn9c102_release_buffers(cam);
2374 cam->io = rb.count ? IO_MMAP : IO_NONE;
2381 sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
2383 struct v4l2_buffer b;
2385 if (copy_from_user(&b, arg, sizeof(b)))
2388 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2389 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2392 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2394 if (cam->frame[b.index].vma_use_count)
2395 b.flags |= V4L2_BUF_FLAG_MAPPED;
2397 if (cam->frame[b.index].state == F_DONE)
2398 b.flags |= V4L2_BUF_FLAG_DONE;
2399 else if (cam->frame[b.index].state != F_UNUSED)
2400 b.flags |= V4L2_BUF_FLAG_QUEUED;
2402 if (copy_to_user(arg, &b, sizeof(b)))
2410 sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
2412 struct v4l2_buffer b;
2413 unsigned long lock_flags;
2415 if (copy_from_user(&b, arg, sizeof(b)))
2418 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2419 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2422 if (cam->frame[b.index].state != F_UNUSED)
2425 cam->frame[b.index].state = F_QUEUED;
2427 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2428 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2429 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2431 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2438 sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2441 struct v4l2_buffer b;
2442 struct sn9c102_frame_t *f;
2443 unsigned long lock_flags;
2446 if (copy_from_user(&b, arg, sizeof(b)))
2449 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2452 if (list_empty(&cam->outqueue)) {
2453 if (cam->stream == STREAM_OFF)
2455 if (filp->f_flags & O_NONBLOCK)
2457 timeout = wait_event_interruptible_timeout
2459 (!list_empty(&cam->outqueue)) ||
2460 (cam->state & DEV_DISCONNECTED) ||
2461 (cam->state & DEV_MISCONFIGURED),
2462 cam->module_param.frame_timeout *
2463 1000 * msecs_to_jiffies(1) );
2466 if (cam->state & DEV_DISCONNECTED)
2468 if (!timeout || (cam->state & DEV_MISCONFIGURED))
2472 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2473 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
2474 list_del(cam->outqueue.next);
2475 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2477 f->state = F_UNUSED;
2479 memcpy(&b, &f->buf, sizeof(b));
2480 if (f->vma_use_count)
2481 b.flags |= V4L2_BUF_FLAG_MAPPED;
2483 if (copy_to_user(arg, &b, sizeof(b)))
2486 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2493 sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
2497 if (copy_from_user(&type, arg, sizeof(type)))
2500 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2503 if (list_empty(&cam->inqueue))
2506 cam->stream = STREAM_ON;
2508 DBG(3, "Stream on");
2515 sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
2519 if (copy_from_user(&type, arg, sizeof(type)))
2522 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2525 if (cam->stream == STREAM_ON)
2526 if ((err = sn9c102_stream_interrupt(cam)))
2529 sn9c102_empty_framequeues(cam);
2531 DBG(3, "Stream off");
2538 sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
2540 struct v4l2_streamparm sp;
2542 if (copy_from_user(&sp, arg, sizeof(sp)))
2545 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2548 sp.parm.capture.extendedmode = 0;
2549 sp.parm.capture.readbuffers = cam->nreadbuffers;
2551 if (copy_to_user(arg, &sp, sizeof(sp)))
2559 sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
2561 struct v4l2_streamparm sp;
2563 if (copy_from_user(&sp, arg, sizeof(sp)))
2566 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2569 sp.parm.capture.extendedmode = 0;
2571 if (sp.parm.capture.readbuffers == 0)
2572 sp.parm.capture.readbuffers = cam->nreadbuffers;
2574 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2575 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2577 if (copy_to_user(arg, &sp, sizeof(sp)))
2580 cam->nreadbuffers = sp.parm.capture.readbuffers;
2586 static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
2587 unsigned int cmd, void __user * arg)
2589 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2593 case VIDIOC_QUERYCAP:
2594 return sn9c102_vidioc_querycap(cam, arg);
2596 case VIDIOC_ENUMINPUT:
2597 return sn9c102_vidioc_enuminput(cam, arg);
2599 case VIDIOC_G_INPUT:
2600 return sn9c102_vidioc_g_input(cam, arg);
2602 case VIDIOC_S_INPUT:
2603 return sn9c102_vidioc_s_input(cam, arg);
2605 case VIDIOC_QUERYCTRL:
2606 return sn9c102_vidioc_query_ctrl(cam, arg);
2609 return sn9c102_vidioc_g_ctrl(cam, arg);
2611 case VIDIOC_S_CTRL_OLD:
2613 return sn9c102_vidioc_s_ctrl(cam, arg);
2615 case VIDIOC_CROPCAP_OLD:
2616 case VIDIOC_CROPCAP:
2617 return sn9c102_vidioc_cropcap(cam, arg);
2620 return sn9c102_vidioc_g_crop(cam, arg);
2623 return sn9c102_vidioc_s_crop(cam, arg);
2625 case VIDIOC_ENUM_FMT:
2626 return sn9c102_vidioc_enum_fmt(cam, arg);
2629 return sn9c102_vidioc_g_fmt(cam, arg);
2631 case VIDIOC_TRY_FMT:
2633 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
2635 case VIDIOC_G_JPEGCOMP:
2636 return sn9c102_vidioc_g_jpegcomp(cam, arg);
2638 case VIDIOC_S_JPEGCOMP:
2639 return sn9c102_vidioc_s_jpegcomp(cam, arg);
2641 case VIDIOC_REQBUFS:
2642 return sn9c102_vidioc_reqbufs(cam, arg);
2644 case VIDIOC_QUERYBUF:
2645 return sn9c102_vidioc_querybuf(cam, arg);
2648 return sn9c102_vidioc_qbuf(cam, arg);
2651 return sn9c102_vidioc_dqbuf(cam, filp, arg);
2653 case VIDIOC_STREAMON:
2654 return sn9c102_vidioc_streamon(cam, arg);
2656 case VIDIOC_STREAMOFF:
2657 return sn9c102_vidioc_streamoff(cam, arg);
2660 return sn9c102_vidioc_g_parm(cam, arg);
2662 case VIDIOC_S_PARM_OLD:
2664 return sn9c102_vidioc_s_parm(cam, arg);
2668 case VIDIOC_QUERYSTD:
2669 case VIDIOC_ENUMSTD:
2670 case VIDIOC_QUERYMENU:
2680 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2681 unsigned int cmd, unsigned long arg)
2683 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2686 if (mutex_lock_interruptible(&cam->fileop_mutex))
2687 return -ERESTARTSYS;
2689 if (cam->state & DEV_DISCONNECTED) {
2690 DBG(1, "Device not present");
2691 mutex_unlock(&cam->fileop_mutex);
2695 if (cam->state & DEV_MISCONFIGURED) {
2696 DBG(1, "The camera is misconfigured. Close and open it "
2698 mutex_unlock(&cam->fileop_mutex);
2702 V4LDBG(3, "sn9c102", cmd);
2704 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2706 mutex_unlock(&cam->fileop_mutex);
2711 /*****************************************************************************/
2713 static struct file_operations sn9c102_fops = {
2714 .owner = THIS_MODULE,
2715 .open = sn9c102_open,
2716 .release = sn9c102_release,
2717 .ioctl = sn9c102_ioctl,
2718 .read = sn9c102_read,
2719 .poll = sn9c102_poll,
2720 .mmap = sn9c102_mmap,
2721 .llseek = no_llseek,
2724 /*****************************************************************************/
2726 /* It exists a single interface only. We do not need to validate anything. */
2728 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2730 struct usb_device *udev = interface_to_usbdev(intf);
2731 struct sn9c102_device* cam;
2732 static unsigned int dev_nr = 0;
2736 if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2741 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2742 DBG(1, "kmalloc() failed");
2747 if (!(cam->v4ldev = video_device_alloc())) {
2748 DBG(1, "video_device_alloc() failed");
2753 mutex_init(&cam->dev_mutex);
2755 r = sn9c102_read_reg(cam, 0x00);
2756 if (r < 0 || r != 0x10) {
2757 DBG(1, "Sorry, this is not a SN9C10x based camera "
2758 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2763 cam->bridge = (id->idProduct & 0xffc0) == 0x6080 ?
2764 BRIDGE_SN9C103 : BRIDGE_SN9C102;
2765 switch (cam->bridge) {
2766 case BRIDGE_SN9C101:
2767 case BRIDGE_SN9C102:
2768 DBG(2, "SN9C10[12] PC Camera Controller detected "
2769 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2771 case BRIDGE_SN9C103:
2772 DBG(2, "SN9C103 PC Camera Controller detected "
2773 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2777 for (i = 0; sn9c102_sensor_table[i]; i++) {
2778 err = sn9c102_sensor_table[i](cam);
2784 DBG(2, "%s image sensor detected", cam->sensor.name);
2785 DBG(3, "Support for %s maintained by %s",
2786 cam->sensor.name, cam->sensor.maintainer);
2788 DBG(1, "No supported image sensor detected");
2793 if (sn9c102_init(cam)) {
2794 DBG(1, "Initialization failed. I will retry on open().");
2795 cam->state |= DEV_MISCONFIGURED;
2798 strcpy(cam->v4ldev->name, "SN9C10x PC Camera");
2799 cam->v4ldev->owner = THIS_MODULE;
2800 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2801 cam->v4ldev->hardware = 0;
2802 cam->v4ldev->fops = &sn9c102_fops;
2803 cam->v4ldev->minor = video_nr[dev_nr];
2804 cam->v4ldev->release = video_device_release;
2805 video_set_drvdata(cam->v4ldev, cam);
2807 mutex_lock(&cam->dev_mutex);
2809 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2812 DBG(1, "V4L2 device registration failed");
2813 if (err == -ENFILE && video_nr[dev_nr] == -1)
2814 DBG(1, "Free /dev/videoX node not found");
2815 video_nr[dev_nr] = -1;
2816 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2817 mutex_unlock(&cam->dev_mutex);
2821 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2823 cam->module_param.force_munmap = force_munmap[dev_nr];
2824 cam->module_param.frame_timeout = frame_timeout[dev_nr];
2826 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2828 #ifdef CONFIG_VIDEO_ADV_DEBUG
2829 sn9c102_create_sysfs(cam);
2830 DBG(2, "Optional device control through 'sysfs' interface ready");
2833 usb_set_intfdata(intf, cam);
2835 mutex_unlock(&cam->dev_mutex);
2841 kfree(cam->control_buffer);
2843 video_device_release(cam->v4ldev);
2850 static void sn9c102_usb_disconnect(struct usb_interface* intf)
2852 struct sn9c102_device* cam = usb_get_intfdata(intf);
2857 down_write(&sn9c102_disconnect);
2859 mutex_lock(&cam->dev_mutex);
2861 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2863 wake_up_interruptible_all(&cam->open);
2866 DBG(2, "Device /dev/video%d is open! Deregistration and "
2867 "memory deallocation are deferred on close.",
2868 cam->v4ldev->minor);
2869 cam->state |= DEV_MISCONFIGURED;
2870 sn9c102_stop_transfer(cam);
2871 cam->state |= DEV_DISCONNECTED;
2872 wake_up_interruptible(&cam->wait_frame);
2873 wake_up(&cam->wait_stream);
2874 usb_get_dev(cam->usbdev);
2876 cam->state |= DEV_DISCONNECTED;
2877 sn9c102_release_resources(cam);
2880 mutex_unlock(&cam->dev_mutex);
2885 up_write(&sn9c102_disconnect);
2889 static struct usb_driver sn9c102_usb_driver = {
2891 .id_table = sn9c102_id_table,
2892 .probe = sn9c102_usb_probe,
2893 .disconnect = sn9c102_usb_disconnect,
2896 /*****************************************************************************/
2898 static int __init sn9c102_module_init(void)
2902 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
2903 KDBG(3, SN9C102_MODULE_AUTHOR);
2905 if ((err = usb_register(&sn9c102_usb_driver)))
2906 KDBG(1, "usb_register() failed");
2912 static void __exit sn9c102_module_exit(void)
2914 usb_deregister(&sn9c102_usb_driver);
2918 module_init(sn9c102_module_init);
2919 module_exit(sn9c102_module_exit);