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/string.h>
29 #include <linux/device.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>
38 #include <linux/vmalloc.h>
39 #include <linux/page-flags.h>
40 #include <linux/byteorder/generic.h>
42 #include <asm/uaccess.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)
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."
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"
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)"."
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 "
94 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
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},
105 static sn9c103_sof_header_t sn9c103_sof_header[] = {
106 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x20},
109 static sn9c102_eof_header_t sn9c102_eof_header[] = {
110 {0x00, 0x00, 0x00, 0x00},
111 {0x40, 0x00, 0x00, 0x00},
112 {0x80, 0x00, 0x00, 0x00},
113 {0xc0, 0x00, 0x00, 0x00},
116 /*****************************************************************************/
118 static void* rvmalloc(size_t size)
123 size = PAGE_ALIGN(size);
125 mem = vmalloc_32((unsigned long)size);
129 memset(mem, 0, size);
131 adr = (unsigned long)mem;
133 SetPageReserved(vmalloc_to_page((void *)adr));
142 static void rvfree(void* mem, size_t size)
149 size = PAGE_ALIGN(size);
151 adr = (unsigned long)mem;
153 ClearPageReserved(vmalloc_to_page((void *)adr));
163 sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
164 enum sn9c102_io_method io)
166 struct v4l2_pix_format* p = &(cam->sensor->pix_format);
167 struct v4l2_rect* r = &(cam->sensor->cropcap.bounds);
168 const size_t imagesize = cam->module_param.force_munmap ||
170 (p->width * p->height * p->priv) / 8 :
171 (r->width * r->height * p->priv) / 8;
175 if (count > SN9C102_MAX_FRAMES)
176 count = SN9C102_MAX_FRAMES;
178 cam->nbuffers = count;
179 while (cam->nbuffers > 0) {
180 if ((buff = rvmalloc(cam->nbuffers * PAGE_ALIGN(imagesize))))
185 for (i = 0; i < cam->nbuffers; i++) {
186 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
187 cam->frame[i].buf.index = i;
188 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
189 cam->frame[i].buf.length = imagesize;
190 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
191 cam->frame[i].buf.sequence = 0;
192 cam->frame[i].buf.field = V4L2_FIELD_NONE;
193 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
194 cam->frame[i].buf.flags = 0;
197 return cam->nbuffers;
201 static void sn9c102_release_buffers(struct sn9c102_device* cam)
204 rvfree(cam->frame[0].bufmem,
205 cam->nbuffers * PAGE_ALIGN(cam->frame[0].buf.length));
208 cam->frame_current = NULL;
212 static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
216 INIT_LIST_HEAD(&cam->inqueue);
217 INIT_LIST_HEAD(&cam->outqueue);
219 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
220 cam->frame[i].state = F_UNUSED;
221 cam->frame[i].buf.bytesused = 0;
226 static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
228 struct sn9c102_frame_t *i;
230 list_for_each_entry(i, &cam->outqueue, frame) {
232 list_add(&i->frame, &cam->inqueue);
235 INIT_LIST_HEAD(&cam->outqueue);
239 static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
241 unsigned long lock_flags;
244 for (i = 0; i < cam->nbuffers; i++)
245 if (cam->frame[i].state == F_UNUSED) {
246 cam->frame[i].state = F_QUEUED;
247 spin_lock_irqsave(&cam->queue_lock, lock_flags);
248 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
249 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
253 /*****************************************************************************/
255 int sn9c102_write_regs(struct sn9c102_device* cam, u8* buff, u16 index)
257 struct usb_device* udev = cam->usbdev;
260 if (index + sizeof(buff) >= ARRAY_SIZE(cam->reg))
263 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
264 index, 0, buff, sizeof(buff),
265 SN9C102_CTRL_TIMEOUT*sizeof(buff));
267 DBG(3, "Failed to write registers (index 0x%02X, error %d)",
272 for (i = 0; i < sizeof(buff); i++)
273 cam->reg[index+i] = buff[i];
279 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
281 struct usb_device* udev = cam->usbdev;
282 u8* buff = cam->control_buffer;
285 if (index >= ARRAY_SIZE(cam->reg))
290 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
291 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
293 DBG(3, "Failed to write a register (value 0x%02X, index "
294 "0x%02X, error %d)", value, index, res);
298 cam->reg[index] = value;
304 /* NOTE: reading some registers always returns 0 */
305 static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
307 struct usb_device* udev = cam->usbdev;
308 u8* buff = cam->control_buffer;
311 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
312 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
314 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
317 return (res >= 0) ? (int)(*buff) : -1;
321 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
323 if (index >= ARRAY_SIZE(cam->reg))
326 return cam->reg[index];
331 sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
335 for (i = 1; i <= 5; i++) {
336 r = sn9c102_read_reg(cam, 0x08);
341 if (sensor->frequency & SN9C102_I2C_400KHZ)
351 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
352 struct sn9c102_sensor* sensor)
355 r = sn9c102_read_reg(cam, 0x08);
356 return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0;
361 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
362 struct sn9c102_sensor* sensor)
365 r = sn9c102_read_reg(cam, 0x08);
366 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
371 sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
372 struct sn9c102_sensor* sensor, u8 data0, u8 data1,
375 struct usb_device* udev = cam->usbdev;
376 u8* data = cam->control_buffer;
380 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
381 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
382 data[1] = data0; /* I2C slave id */
383 data[2] = data1; /* address */
385 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
386 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
390 err += sn9c102_i2c_wait(cam, sensor);
392 /* Read cycle - n bytes */
393 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
394 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
398 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
399 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
403 err += sn9c102_i2c_wait(cam, sensor);
405 /* The first read byte will be placed in data[4] */
406 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
407 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
411 err += sn9c102_i2c_detect_read_error(cam, sensor);
413 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
417 DBG(3, "I2C read failed for %s image sensor", sensor->name);
422 memcpy(buffer, data, sizeof(buffer));
429 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
430 struct sn9c102_sensor* sensor, u8 n, u8 data0,
431 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
433 struct usb_device* udev = cam->usbdev;
434 u8* data = cam->control_buffer;
437 /* Write cycle. It usually is address + value */
438 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
439 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
448 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
449 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
453 err += sn9c102_i2c_wait(cam, sensor);
454 err += sn9c102_i2c_detect_write_error(cam, sensor);
457 DBG(3, "I2C write failed for %s image sensor", sensor->name);
459 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
460 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
461 n, data0, data1, data2, data3, data4, data5);
468 sn9c102_i2c_try_read(struct sn9c102_device* cam,
469 struct sn9c102_sensor* sensor, u8 address)
471 return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
477 sn9c102_i2c_try_write(struct sn9c102_device* cam,
478 struct sn9c102_sensor* sensor, u8 address, u8 value)
480 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
481 sensor->i2c_slave_id, address,
486 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
491 return sn9c102_i2c_try_read(cam, cam->sensor, address);
495 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
500 return sn9c102_i2c_try_write(cam, cam->sensor, address, value);
503 /*****************************************************************************/
506 sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
508 size_t soflen = 0, i;
511 switch (cam->bridge) {
514 soflen = sizeof(sn9c102_sof_header_t);
515 n = sizeof(sn9c102_sof_header) / soflen;
518 soflen = sizeof(sn9c103_sof_header_t);
519 n = sizeof(sn9c103_sof_header) / soflen;
522 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
523 for (j = 0; j < n; j++)
524 /* The invariable part of the header is 6 bytes long */
525 if ((cam->bridge != BRIDGE_SN9C103 &&
526 !memcmp(mem + i, sn9c102_sof_header[j], 6)) ||
527 (cam->bridge == BRIDGE_SN9C103 &&
528 !memcmp(mem + i, sn9c103_sof_header[j], 6))) {
529 memcpy(cam->sof_header, mem + i, soflen);
530 /* Skip the header */
531 return mem + i + soflen;
539 sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
541 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
542 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
544 if (cam->sensor->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
545 return NULL; /* EOF header does not exist in compressed data */
547 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
548 for (j = 0; j < n; j++)
549 if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen))
556 static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
558 struct sn9c102_device* cam = urb->context;
559 struct sn9c102_frame_t** f;
560 size_t imagesize, soflen;
564 if (urb->status == -ENOENT)
567 f = &cam->frame_current;
569 if (cam->stream == STREAM_INTERRUPT) {
570 cam->stream = STREAM_OFF;
572 (*f)->state = F_QUEUED;
573 DBG(3, "Stream interrupted");
574 wake_up_interruptible(&cam->wait_stream);
577 if (cam->state & DEV_DISCONNECTED)
580 if (cam->state & DEV_MISCONFIGURED) {
581 wake_up_interruptible(&cam->wait_frame);
585 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
589 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
592 imagesize = (cam->sensor->pix_format.width *
593 cam->sensor->pix_format.height *
594 cam->sensor->pix_format.priv) / 8;
596 soflen = (cam->bridge) == BRIDGE_SN9C103 ?
597 sizeof(sn9c103_sof_header_t) :
598 sizeof(sn9c102_sof_header_t);
600 for (i = 0; i < urb->number_of_packets; i++) {
601 unsigned int img, len, status;
602 void *pos, *sof, *eof;
604 len = urb->iso_frame_desc[i].actual_length;
605 status = urb->iso_frame_desc[i].status;
606 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
609 DBG(3, "Error in isochronous frame");
610 (*f)->state = F_ERROR;
614 PDBGG("Isochrnous frame: length %u, #%u i", len, i);
617 sof = sn9c102_find_sof_header(cam, pos, len);
619 eof = sn9c102_find_eof_header(cam, pos, len);
620 if ((*f)->state == F_GRABBING) {
625 img = (eof > pos) ? eof - pos - 1 : 0;
627 if ((*f)->buf.bytesused+img > imagesize) {
628 u32 b = (*f)->buf.bytesused + img -
630 img = imagesize - (*f)->buf.bytesused;
631 DBG(3, "Expected EOF not found: "
634 DBG(3, "Exceeded limit: +%u "
635 "bytes", (unsigned)(b));
638 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
641 if ((*f)->buf.bytesused == 0)
642 do_gettimeofday(&(*f)->buf.timestamp);
644 (*f)->buf.bytesused += img;
646 if ((*f)->buf.bytesused == imagesize ||
647 (cam->sensor->pix_format.pixelformat ==
648 V4L2_PIX_FMT_SN9C10X && eof)) {
649 u32 b = (*f)->buf.bytesused;
650 (*f)->state = F_DONE;
651 (*f)->buf.sequence= ++cam->frame_count;
652 spin_lock(&cam->queue_lock);
653 list_move_tail(&(*f)->frame,
655 if (!list_empty(&cam->inqueue))
658 struct sn9c102_frame_t,
662 spin_unlock(&cam->queue_lock);
663 memcpy(cam->sysfs.frame_header,
664 cam->sof_header, soflen);
665 DBG(3, "Video frame captured: %lu "
666 "bytes", (unsigned long)(b));
672 (*f)->state = F_ERROR;
673 DBG(3, "Not expected EOF after %lu "
674 "bytes of image data",
676 ((*f)->buf.bytesused));
683 DBG(3, "EOF without SOF");
687 PDBGG("Ignoring pointless isochronous frame");
691 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
693 (*f)->state = F_GRABBING;
694 (*f)->buf.bytesused = 0;
697 DBG(3, "SOF detected: new video frame");
701 } else if ((*f)->state == F_GRABBING) {
702 eof = sn9c102_find_eof_header(cam, pos, len);
703 if (eof && eof < sof)
704 goto end_of_frame; /* (1) */
706 if (cam->sensor->pix_format.pixelformat ==
707 V4L2_PIX_FMT_SN9C10X) {
711 DBG(3, "SOF before expected EOF after "
712 "%lu bytes of image data",
714 ((*f)->buf.bytesused));
722 urb->dev = cam->usbdev;
723 err = usb_submit_urb(urb, GFP_ATOMIC);
724 if (err < 0 && err != -EPERM) {
725 cam->state |= DEV_MISCONFIGURED;
726 DBG(1, "usb_submit_urb() failed");
729 wake_up_interruptible(&cam->wait_frame);
733 static int sn9c102_start_transfer(struct sn9c102_device* cam)
735 struct usb_device *udev = cam->usbdev;
737 const unsigned int sn9c102_wMaxPacketSize[] = {0, 128, 256, 384, 512,
738 680, 800, 900, 1023};
739 const unsigned int sn9c103_wMaxPacketSize[] = {0, 128, 256, 384, 512,
740 680, 800, 900, 1003};
741 const unsigned int psz = (cam->bridge == BRIDGE_SN9C103) ?
742 sn9c103_wMaxPacketSize[SN9C102_ALTERNATE_SETTING] :
743 sn9c102_wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
747 for (i = 0; i < SN9C102_URBS; i++) {
748 cam->transfer_buffer[i] = kmalloc(SN9C102_ISO_PACKETS * psz,
750 if (!cam->transfer_buffer[i]) {
752 DBG(1, "Not enough memory");
757 for (i = 0; i < SN9C102_URBS; i++) {
758 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
762 DBG(1, "usb_alloc_urb() failed");
767 urb->pipe = usb_rcvisocpipe(udev, 1);
768 urb->transfer_flags = URB_ISO_ASAP;
769 urb->number_of_packets = SN9C102_ISO_PACKETS;
770 urb->complete = sn9c102_urb_complete;
771 urb->transfer_buffer = cam->transfer_buffer[i];
772 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
774 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
775 urb->iso_frame_desc[j].offset = psz * j;
776 urb->iso_frame_desc[j].length = psz;
781 if (!(cam->reg[0x01] & 0x04)) {
782 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
785 DBG(1, "I/O hardware error");
790 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
792 DBG(1, "usb_set_interface() failed");
796 cam->frame_current = NULL;
798 for (i = 0; i < SN9C102_URBS; i++) {
799 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
801 for (j = i-1; j >= 0; j--)
802 usb_kill_urb(cam->urb[j]);
803 DBG(1, "usb_submit_urb() failed, error %d", err);
811 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
812 usb_free_urb(cam->urb[i]);
815 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
816 kfree(cam->transfer_buffer[i]);
822 static int sn9c102_stop_transfer(struct sn9c102_device* cam)
824 struct usb_device *udev = cam->usbdev;
828 if (cam->state & DEV_DISCONNECTED)
831 for (i = SN9C102_URBS-1; i >= 0; i--) {
832 usb_kill_urb(cam->urb[i]);
833 usb_free_urb(cam->urb[i]);
834 kfree(cam->transfer_buffer[i]);
837 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
839 DBG(3, "usb_set_interface() failed");
845 static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
849 cam->stream = STREAM_INTERRUPT;
850 err = wait_event_timeout(cam->wait_stream,
851 (cam->stream == STREAM_OFF) ||
852 (cam->state & DEV_DISCONNECTED),
853 SN9C102_URB_TIMEOUT);
854 if (cam->state & DEV_DISCONNECTED)
857 cam->state |= DEV_MISCONFIGURED;
858 DBG(1, "The camera is misconfigured. To use it, close and "
859 "open /dev/video%d again.", cam->v4ldev->minor);
866 /*****************************************************************************/
868 static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
875 strncpy(str, buff, len);
878 strncpy(str, buff, 4);
882 val = simple_strtoul(str, &endp, 0);
886 *count = (ssize_t)(endp - str);
887 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
894 NOTE 1: being inside one of the following methods implies that the v4l
895 device exists for sure (see kobjects and reference counters)
896 NOTE 2: buffers are PAGE_SIZE long
899 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
901 struct sn9c102_device* cam;
904 if (down_interruptible(&sn9c102_sysfs_lock))
907 cam = video_get_drvdata(to_video_device(cd));
909 up(&sn9c102_sysfs_lock);
913 count = sprintf(buf, "%u\n", cam->sysfs.reg);
915 up(&sn9c102_sysfs_lock);
922 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
924 struct sn9c102_device* cam;
928 if (down_interruptible(&sn9c102_sysfs_lock))
931 cam = video_get_drvdata(to_video_device(cd));
933 up(&sn9c102_sysfs_lock);
937 index = sn9c102_strtou8(buf, len, &count);
938 if (index > 0x1f || !count) {
939 up(&sn9c102_sysfs_lock);
943 cam->sysfs.reg = index;
945 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg);
946 DBG(3, "Written bytes: %zd", count);
948 up(&sn9c102_sysfs_lock);
954 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
956 struct sn9c102_device* cam;
960 if (down_interruptible(&sn9c102_sysfs_lock))
963 cam = video_get_drvdata(to_video_device(cd));
965 up(&sn9c102_sysfs_lock);
969 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
970 up(&sn9c102_sysfs_lock);
974 count = sprintf(buf, "%d\n", val);
976 DBG(3, "Read bytes: %zd", count);
978 up(&sn9c102_sysfs_lock);
985 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
987 struct sn9c102_device* cam;
992 if (down_interruptible(&sn9c102_sysfs_lock))
995 cam = video_get_drvdata(to_video_device(cd));
997 up(&sn9c102_sysfs_lock);
1001 value = sn9c102_strtou8(buf, len, &count);
1003 up(&sn9c102_sysfs_lock);
1007 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
1009 up(&sn9c102_sysfs_lock);
1013 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
1014 cam->sysfs.reg, value);
1015 DBG(3, "Written bytes: %zd", count);
1017 up(&sn9c102_sysfs_lock);
1023 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
1025 struct sn9c102_device* cam;
1028 if (down_interruptible(&sn9c102_sysfs_lock))
1029 return -ERESTARTSYS;
1031 cam = video_get_drvdata(to_video_device(cd));
1033 up(&sn9c102_sysfs_lock);
1037 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
1039 DBG(3, "Read bytes: %zd", count);
1041 up(&sn9c102_sysfs_lock);
1048 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1050 struct sn9c102_device* cam;
1054 if (down_interruptible(&sn9c102_sysfs_lock))
1055 return -ERESTARTSYS;
1057 cam = video_get_drvdata(to_video_device(cd));
1059 up(&sn9c102_sysfs_lock);
1063 index = sn9c102_strtou8(buf, len, &count);
1065 up(&sn9c102_sysfs_lock);
1069 cam->sysfs.i2c_reg = index;
1071 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1072 DBG(3, "Written bytes: %zd", count);
1074 up(&sn9c102_sysfs_lock);
1080 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1082 struct sn9c102_device* cam;
1086 if (down_interruptible(&sn9c102_sysfs_lock))
1087 return -ERESTARTSYS;
1089 cam = video_get_drvdata(to_video_device(cd));
1091 up(&sn9c102_sysfs_lock);
1095 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_READ)) {
1096 up(&sn9c102_sysfs_lock);
1100 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1101 up(&sn9c102_sysfs_lock);
1105 count = sprintf(buf, "%d\n", val);
1107 DBG(3, "Read bytes: %zd", count);
1109 up(&sn9c102_sysfs_lock);
1116 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1118 struct sn9c102_device* cam;
1123 if (down_interruptible(&sn9c102_sysfs_lock))
1124 return -ERESTARTSYS;
1126 cam = video_get_drvdata(to_video_device(cd));
1128 up(&sn9c102_sysfs_lock);
1132 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_WRITE)) {
1133 up(&sn9c102_sysfs_lock);
1137 value = sn9c102_strtou8(buf, len, &count);
1139 up(&sn9c102_sysfs_lock);
1143 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1145 up(&sn9c102_sysfs_lock);
1149 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1150 cam->sysfs.i2c_reg, value);
1151 DBG(3, "Written bytes: %zd", count);
1153 up(&sn9c102_sysfs_lock);
1160 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1162 struct sn9c102_device* cam;
1163 enum sn9c102_bridge bridge;
1168 if (down_interruptible(&sn9c102_sysfs_lock))
1169 return -ERESTARTSYS;
1171 cam = video_get_drvdata(to_video_device(cd));
1173 up(&sn9c102_sysfs_lock);
1177 bridge = cam->bridge;
1179 up(&sn9c102_sysfs_lock);
1181 value = sn9c102_strtou8(buf, len, &count);
1186 case BRIDGE_SN9C101:
1187 case BRIDGE_SN9C102:
1190 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1191 res = sn9c102_store_val(cd, buf, len);
1193 case BRIDGE_SN9C103:
1196 if ((res = sn9c102_store_reg(cd, "0x04", 4)) >= 0)
1197 res = sn9c102_store_val(cd, buf, len);
1206 sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1212 value = sn9c102_strtou8(buf, len, &count);
1213 if (!count || value > 0x7f)
1216 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1217 res = sn9c102_store_val(cd, buf, len);
1224 sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1230 value = sn9c102_strtou8(buf, len, &count);
1231 if (!count || value > 0x7f)
1234 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1235 res = sn9c102_store_val(cd, buf, len);
1241 static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1243 struct sn9c102_device* cam;
1246 cam = video_get_drvdata(to_video_device(cd));
1250 count = sizeof(cam->sysfs.frame_header);
1251 memcpy(buf, cam->sysfs.frame_header, count);
1253 DBG(3, "Frame header, read bytes: %zd", count);
1259 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1260 sn9c102_show_reg, sn9c102_store_reg);
1261 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1262 sn9c102_show_val, sn9c102_store_val);
1263 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1264 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1265 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1266 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1267 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1268 static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1269 static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1270 static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
1271 sn9c102_show_frame_header, NULL);
1274 static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1276 struct video_device *v4ldev = cam->v4ldev;
1278 video_device_create_file(v4ldev, &class_device_attr_reg);
1279 video_device_create_file(v4ldev, &class_device_attr_val);
1280 video_device_create_file(v4ldev, &class_device_attr_frame_header);
1281 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
1282 video_device_create_file(v4ldev, &class_device_attr_green);
1283 else if (cam->bridge == BRIDGE_SN9C103) {
1284 video_device_create_file(v4ldev, &class_device_attr_blue);
1285 video_device_create_file(v4ldev, &class_device_attr_red);
1287 if (cam->sensor && cam->sensor->sysfs_ops) {
1288 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1289 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1293 /*****************************************************************************/
1296 sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
1300 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
1301 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, 0x18);
1303 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, 0x18);
1305 return err ? -EIO : 0;
1310 sn9c102_set_compression(struct sn9c102_device* cam,
1311 struct v4l2_jpegcompression* compression)
1315 if (compression->quality == 0)
1316 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, 0x17);
1317 else if (compression->quality == 1)
1318 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, 0x17);
1320 return err ? -EIO : 0;
1324 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1330 r = cam->reg[0x18] & 0xcf;
1331 else if (scale == 2) {
1332 r = cam->reg[0x18] & 0xcf;
1334 } else if (scale == 4)
1335 r = cam->reg[0x18] | 0x20;
1337 err += sn9c102_write_reg(cam, r, 0x18);
1341 PDBGG("Scaling factor: %u", scale);
1347 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1349 struct sn9c102_sensor* s = cam->sensor;
1350 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1351 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1352 h_size = (u8)(rect->width / 16),
1353 v_size = (u8)(rect->height / 16);
1356 err += sn9c102_write_reg(cam, h_start, 0x12);
1357 err += sn9c102_write_reg(cam, v_start, 0x13);
1358 err += sn9c102_write_reg(cam, h_size, 0x15);
1359 err += sn9c102_write_reg(cam, v_size, 0x16);
1363 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1364 "%u %u %u %u", h_start, v_start, h_size, v_size);
1370 static int sn9c102_init(struct sn9c102_device* cam)
1372 struct sn9c102_sensor* s = cam->sensor;
1373 struct v4l2_control ctrl;
1374 struct v4l2_queryctrl *qctrl;
1375 struct v4l2_rect* rect;
1379 if (!(cam->state & DEV_INITIALIZED)) {
1380 init_waitqueue_head(&cam->open);
1382 rect = &(s->cropcap.defrect);
1383 } else { /* use current values */
1388 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1389 err += sn9c102_set_crop(cam, rect);
1396 DBG(3, "Sensor initialization failed");
1401 if (!(cam->state & DEV_INITIALIZED))
1402 cam->compression.quality = cam->reg[0x17] & 0x01 ? 0 : 1;
1404 err += sn9c102_set_compression(cam, &cam->compression);
1405 err += sn9c102_set_pix_format(cam, &s->pix_format);
1406 if (s->set_pix_format)
1407 err += s->set_pix_format(cam, &s->pix_format);
1411 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1412 DBG(3, "Compressed video format is active, quality %d",
1413 cam->compression.quality);
1415 DBG(3, "Uncompressed video format is active");
1418 if ((err = s->set_crop(cam, rect))) {
1419 DBG(3, "set_crop() failed");
1424 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1425 if (s->qctrl[i].id != 0 &&
1426 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1427 ctrl.id = s->qctrl[i].id;
1428 ctrl.value = qctrl[i].default_value;
1429 err = s->set_ctrl(cam, &ctrl);
1431 DBG(3, "Set %s control failed",
1435 DBG(3, "Image sensor supports '%s' control",
1440 if (!(cam->state & DEV_INITIALIZED)) {
1441 init_MUTEX(&cam->fileop_sem);
1442 spin_lock_init(&cam->queue_lock);
1443 init_waitqueue_head(&cam->wait_frame);
1444 init_waitqueue_head(&cam->wait_stream);
1445 cam->nreadbuffers = 2;
1446 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1447 memcpy(&(s->_rect), &(s->cropcap.defrect),
1448 sizeof(struct v4l2_rect));
1449 cam->state |= DEV_INITIALIZED;
1452 DBG(2, "Initialization succeeded");
1457 static void sn9c102_release_resources(struct sn9c102_device* cam)
1459 down(&sn9c102_sysfs_lock);
1461 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1462 video_set_drvdata(cam->v4ldev, NULL);
1463 video_unregister_device(cam->v4ldev);
1465 up(&sn9c102_sysfs_lock);
1467 kfree(cam->control_buffer);
1470 /*****************************************************************************/
1472 static int sn9c102_open(struct inode* inode, struct file* filp)
1474 struct sn9c102_device* cam;
1478 This is the only safe way to prevent race conditions with
1481 if (!down_read_trylock(&sn9c102_disconnect))
1482 return -ERESTARTSYS;
1484 cam = video_get_drvdata(video_devdata(filp));
1486 if (down_interruptible(&cam->dev_sem)) {
1487 up_read(&sn9c102_disconnect);
1488 return -ERESTARTSYS;
1492 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1493 if ((filp->f_flags & O_NONBLOCK) ||
1494 (filp->f_flags & O_NDELAY)) {
1499 err = wait_event_interruptible_exclusive(cam->open,
1500 cam->state & DEV_DISCONNECTED
1503 up_read(&sn9c102_disconnect);
1506 if (cam->state & DEV_DISCONNECTED) {
1507 up_read(&sn9c102_disconnect);
1510 down(&cam->dev_sem);
1514 if (cam->state & DEV_MISCONFIGURED) {
1515 err = sn9c102_init(cam);
1517 DBG(1, "Initialization failed again. "
1518 "I will retry on next open().");
1521 cam->state &= ~DEV_MISCONFIGURED;
1524 if ((err = sn9c102_start_transfer(cam)))
1527 filp->private_data = cam;
1530 cam->stream = STREAM_OFF;
1532 cam->frame_count = 0;
1533 sn9c102_empty_framequeues(cam);
1535 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1539 up_read(&sn9c102_disconnect);
1544 static int sn9c102_release(struct inode* inode, struct file* filp)
1546 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1548 down(&cam->dev_sem); /* prevent disconnect() to be called */
1550 sn9c102_stop_transfer(cam);
1552 sn9c102_release_buffers(cam);
1554 if (cam->state & DEV_DISCONNECTED) {
1555 sn9c102_release_resources(cam);
1562 wake_up_interruptible_nr(&cam->open, 1);
1564 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1573 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1575 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1576 struct sn9c102_frame_t* f, * i;
1577 unsigned long lock_flags;
1580 if (down_interruptible(&cam->fileop_sem))
1581 return -ERESTARTSYS;
1583 if (cam->state & DEV_DISCONNECTED) {
1584 DBG(1, "Device not present");
1585 up(&cam->fileop_sem);
1589 if (cam->state & DEV_MISCONFIGURED) {
1590 DBG(1, "The camera is misconfigured. Close and open it "
1592 up(&cam->fileop_sem);
1596 if (cam->io == IO_MMAP) {
1597 DBG(3, "Close and open the device again to choose "
1599 up(&cam->fileop_sem);
1603 if (cam->io == IO_NONE) {
1604 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1605 DBG(1, "read() failed, not enough memory");
1606 up(&cam->fileop_sem);
1610 cam->stream = STREAM_ON;
1613 if (list_empty(&cam->inqueue)) {
1614 if (!list_empty(&cam->outqueue))
1615 sn9c102_empty_framequeues(cam);
1616 sn9c102_queue_unusedframes(cam);
1620 up(&cam->fileop_sem);
1624 if (list_empty(&cam->outqueue)) {
1625 if (filp->f_flags & O_NONBLOCK) {
1626 up(&cam->fileop_sem);
1629 err = wait_event_interruptible
1631 (!list_empty(&cam->outqueue)) ||
1632 (cam->state & DEV_DISCONNECTED) ||
1633 (cam->state & DEV_MISCONFIGURED) );
1635 up(&cam->fileop_sem);
1638 if (cam->state & DEV_DISCONNECTED) {
1639 up(&cam->fileop_sem);
1642 if (cam->state & DEV_MISCONFIGURED) {
1643 up(&cam->fileop_sem);
1648 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1650 if (count > f->buf.bytesused)
1651 count = f->buf.bytesused;
1653 if (copy_to_user(buf, f->bufmem, count)) {
1660 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1661 list_for_each_entry(i, &cam->outqueue, frame)
1662 i->state = F_UNUSED;
1663 INIT_LIST_HEAD(&cam->outqueue);
1664 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1666 sn9c102_queue_unusedframes(cam);
1668 PDBGG("Frame #%lu, bytes read: %zu",
1669 (unsigned long)f->buf.index, count);
1671 up(&cam->fileop_sem);
1677 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1679 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1680 struct sn9c102_frame_t* f;
1681 unsigned long lock_flags;
1682 unsigned int mask = 0;
1684 if (down_interruptible(&cam->fileop_sem))
1687 if (cam->state & DEV_DISCONNECTED) {
1688 DBG(1, "Device not present");
1692 if (cam->state & DEV_MISCONFIGURED) {
1693 DBG(1, "The camera is misconfigured. Close and open it "
1698 if (cam->io == IO_NONE) {
1699 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1701 DBG(1, "poll() failed, not enough memory");
1705 cam->stream = STREAM_ON;
1708 if (cam->io == IO_READ) {
1709 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1710 list_for_each_entry(f, &cam->outqueue, frame)
1711 f->state = F_UNUSED;
1712 INIT_LIST_HEAD(&cam->outqueue);
1713 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1714 sn9c102_queue_unusedframes(cam);
1717 poll_wait(filp, &cam->wait_frame, wait);
1719 if (!list_empty(&cam->outqueue))
1720 mask |= POLLIN | POLLRDNORM;
1722 up(&cam->fileop_sem);
1727 up(&cam->fileop_sem);
1732 static void sn9c102_vm_open(struct vm_area_struct* vma)
1734 struct sn9c102_frame_t* f = vma->vm_private_data;
1739 static void sn9c102_vm_close(struct vm_area_struct* vma)
1741 /* NOTE: buffers are not freed here */
1742 struct sn9c102_frame_t* f = vma->vm_private_data;
1747 static struct vm_operations_struct sn9c102_vm_ops = {
1748 .open = sn9c102_vm_open,
1749 .close = sn9c102_vm_close,
1753 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1755 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1756 unsigned long size = vma->vm_end - vma->vm_start,
1757 start = vma->vm_start,
1762 if (down_interruptible(&cam->fileop_sem))
1763 return -ERESTARTSYS;
1765 if (cam->state & DEV_DISCONNECTED) {
1766 DBG(1, "Device not present");
1767 up(&cam->fileop_sem);
1771 if (cam->state & DEV_MISCONFIGURED) {
1772 DBG(1, "The camera is misconfigured. Close and open it "
1774 up(&cam->fileop_sem);
1778 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1779 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1780 up(&cam->fileop_sem);
1784 for (i = 0; i < cam->nbuffers; i++) {
1785 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1788 if (i == cam->nbuffers) {
1789 up(&cam->fileop_sem);
1793 /* VM_IO is eventually going to replace PageReserved altogether */
1794 vma->vm_flags |= VM_IO;
1795 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1797 pos = (unsigned long)cam->frame[i].bufmem;
1798 while (size > 0) { /* size is page-aligned */
1799 page = vmalloc_to_pfn((void *)pos);
1800 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
1801 vma->vm_page_prot)) {
1802 up(&cam->fileop_sem);
1810 vma->vm_ops = &sn9c102_vm_ops;
1811 vma->vm_private_data = &cam->frame[i];
1813 sn9c102_vm_open(vma);
1815 up(&cam->fileop_sem);
1820 /*****************************************************************************/
1823 sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
1825 struct v4l2_capability cap = {
1826 .driver = "sn9c102",
1827 .version = SN9C102_MODULE_VERSION_CODE,
1828 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1832 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1833 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1834 strlcpy(cap.bus_info, cam->dev.bus_id, sizeof(cap.bus_info));
1836 if (copy_to_user(arg, &cap, sizeof(cap)))
1844 sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
1846 struct v4l2_input i;
1848 if (copy_from_user(&i, arg, sizeof(i)))
1854 memset(&i, 0, sizeof(i));
1855 strcpy(i.name, "USB");
1857 if (copy_to_user(arg, &i, sizeof(i)))
1865 sn9c102_vidioc_gs_input(struct sn9c102_device* cam, void __user * arg)
1869 if (copy_from_user(&index, arg, sizeof(index)))
1880 sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
1882 struct sn9c102_sensor* s = cam->sensor;
1883 struct v4l2_queryctrl qc;
1886 if (copy_from_user(&qc, arg, sizeof(qc)))
1889 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1890 if (qc.id && qc.id == s->qctrl[i].id) {
1891 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1892 if (copy_to_user(arg, &qc, sizeof(qc)))
1902 sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
1904 struct sn9c102_sensor* s = cam->sensor;
1905 struct v4l2_control ctrl;
1909 if (!s->get_ctrl && !s->set_ctrl)
1912 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1916 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1917 if (ctrl.id && ctrl.id == s->qctrl[i].id) {
1918 ctrl.value = s->_qctrl[i].default_value;
1923 err = s->get_ctrl(cam, &ctrl);
1926 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1934 sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1936 struct sn9c102_sensor* s = cam->sensor;
1937 struct v4l2_control ctrl;
1944 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1947 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1948 if (ctrl.id == s->qctrl[i].id) {
1949 if (ctrl.value < s->qctrl[i].minimum ||
1950 ctrl.value > s->qctrl[i].maximum)
1952 ctrl.value -= ctrl.value % s->qctrl[i].step;
1956 if ((err = s->set_ctrl(cam, &ctrl)))
1959 s->_qctrl[i].default_value = ctrl.value;
1961 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
1962 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
1969 sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
1971 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1973 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1974 cc->pixelaspect.numerator = 1;
1975 cc->pixelaspect.denominator = 1;
1977 if (copy_to_user(arg, cc, sizeof(*cc)))
1985 sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
1987 struct sn9c102_sensor* s = cam->sensor;
1988 struct v4l2_crop crop = {
1989 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1992 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1994 if (copy_to_user(arg, &crop, sizeof(crop)))
2002 sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
2004 struct sn9c102_sensor* s = cam->sensor;
2005 struct v4l2_crop crop;
2006 struct v4l2_rect* rect;
2007 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2008 struct v4l2_pix_format* pix_format = &(s->pix_format);
2010 const enum sn9c102_stream_state stream = cam->stream;
2011 const u32 nbuffers = cam->nbuffers;
2015 if (copy_from_user(&crop, arg, sizeof(crop)))
2020 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2023 if (cam->module_param.force_munmap)
2024 for (i = 0; i < cam->nbuffers; i++)
2025 if (cam->frame[i].vma_use_count) {
2026 DBG(3, "VIDIOC_S_CROP failed. "
2027 "Unmap the buffers first.");
2031 /* Preserve R,G or B origin */
2032 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
2033 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
2035 if (rect->width < 16)
2037 if (rect->height < 16)
2039 if (rect->width > bounds->width)
2040 rect->width = bounds->width;
2041 if (rect->height > bounds->height)
2042 rect->height = bounds->height;
2043 if (rect->left < bounds->left)
2044 rect->left = bounds->left;
2045 if (rect->top < bounds->top)
2046 rect->top = bounds->top;
2047 if (rect->left + rect->width > bounds->left + bounds->width)
2048 rect->left = bounds->left+bounds->width - rect->width;
2049 if (rect->top + rect->height > bounds->top + bounds->height)
2050 rect->top = bounds->top+bounds->height - rect->height;
2052 rect->width &= ~15L;
2053 rect->height &= ~15L;
2055 if (SN9C102_PRESERVE_IMGSCALE) {
2056 /* Calculate the actual scaling factor */
2058 a = rect->width * rect->height;
2059 b = pix_format->width * pix_format->height;
2060 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2064 if (cam->stream == STREAM_ON)
2065 if ((err = sn9c102_stream_interrupt(cam)))
2068 if (copy_to_user(arg, &crop, sizeof(crop))) {
2069 cam->stream = stream;
2073 if (cam->module_param.force_munmap || cam->io == IO_READ)
2074 sn9c102_release_buffers(cam);
2076 err = sn9c102_set_crop(cam, rect);
2078 err += s->set_crop(cam, rect);
2079 err += sn9c102_set_scale(cam, scale);
2081 if (err) { /* atomic, no rollback in ioctl() */
2082 cam->state |= DEV_MISCONFIGURED;
2083 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2084 "use the camera, close and open /dev/video%d again.",
2085 cam->v4ldev->minor);
2089 s->pix_format.width = rect->width/scale;
2090 s->pix_format.height = rect->height/scale;
2091 memcpy(&(s->_rect), rect, sizeof(*rect));
2093 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2094 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2095 cam->state |= DEV_MISCONFIGURED;
2096 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2097 "use the camera, close and open /dev/video%d again.",
2098 cam->v4ldev->minor);
2102 if (cam->io == IO_READ)
2103 sn9c102_empty_framequeues(cam);
2104 else if (cam->module_param.force_munmap)
2105 sn9c102_requeue_outqueue(cam);
2107 cam->stream = stream;
2114 sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
2116 struct v4l2_fmtdesc fmtd;
2118 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
2121 if (fmtd.index == 0) {
2122 strcpy(fmtd.description, "bayer rgb");
2123 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2124 } else if (fmtd.index == 1) {
2125 strcpy(fmtd.description, "compressed");
2126 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2127 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2131 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2132 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2134 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
2142 sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2144 struct v4l2_format format;
2145 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
2147 if (copy_from_user(&format, arg, sizeof(format)))
2150 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2153 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X)
2154 ? 0 : (pfmt->width * pfmt->priv) / 8;
2155 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2156 pfmt->field = V4L2_FIELD_NONE;
2157 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2159 if (copy_to_user(arg, &format, sizeof(format)))
2167 sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2170 struct sn9c102_sensor* s = cam->sensor;
2171 struct v4l2_format format;
2172 struct v4l2_pix_format* pix;
2173 struct v4l2_pix_format* pfmt = &(s->pix_format);
2174 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2175 struct v4l2_rect rect;
2177 const enum sn9c102_stream_state stream = cam->stream;
2178 const u32 nbuffers = cam->nbuffers;
2182 if (copy_from_user(&format, arg, sizeof(format)))
2185 pix = &(format.fmt.pix);
2187 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2190 memcpy(&rect, &(s->_rect), sizeof(rect));
2192 { /* calculate the actual scaling factor */
2194 a = rect.width * rect.height;
2195 b = pix->width * pix->height;
2196 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2199 rect.width = scale * pix->width;
2200 rect.height = scale * pix->height;
2202 if (rect.width < 16)
2204 if (rect.height < 16)
2206 if (rect.width > bounds->left + bounds->width - rect.left)
2207 rect.width = bounds->left + bounds->width - rect.left;
2208 if (rect.height > bounds->top + bounds->height - rect.top)
2209 rect.height = bounds->top + bounds->height - rect.top;
2212 rect.height &= ~15L;
2214 { /* adjust the scaling factor */
2216 a = rect.width * rect.height;
2217 b = pix->width * pix->height;
2218 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2221 pix->width = rect.width / scale;
2222 pix->height = rect.height / scale;
2224 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2225 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2226 pix->pixelformat = pfmt->pixelformat;
2227 pix->priv = pfmt->priv; /* bpp */
2228 pix->colorspace = pfmt->colorspace;
2229 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
2230 ? 0 : (pix->width * pix->priv) / 8;
2231 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2232 pix->field = V4L2_FIELD_NONE;
2234 if (cmd == VIDIOC_TRY_FMT) {
2235 if (copy_to_user(arg, &format, sizeof(format)))
2240 if (cam->module_param.force_munmap)
2241 for (i = 0; i < cam->nbuffers; i++)
2242 if (cam->frame[i].vma_use_count) {
2243 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2248 if (cam->stream == STREAM_ON)
2249 if ((err = sn9c102_stream_interrupt(cam)))
2252 if (copy_to_user(arg, &format, sizeof(format))) {
2253 cam->stream = stream;
2257 if (cam->module_param.force_munmap || cam->io == IO_READ)
2258 sn9c102_release_buffers(cam);
2260 err += sn9c102_set_pix_format(cam, pix);
2261 err += sn9c102_set_crop(cam, &rect);
2262 if (s->set_pix_format)
2263 err += s->set_pix_format(cam, pix);
2265 err += s->set_crop(cam, &rect);
2266 err += sn9c102_set_scale(cam, scale);
2268 if (err) { /* atomic, no rollback in ioctl() */
2269 cam->state |= DEV_MISCONFIGURED;
2270 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2271 "use the camera, close and open /dev/video%d again.",
2272 cam->v4ldev->minor);
2276 memcpy(pfmt, pix, sizeof(*pix));
2277 memcpy(&(s->_rect), &rect, sizeof(rect));
2279 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2280 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2281 cam->state |= DEV_MISCONFIGURED;
2282 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2283 "use the camera, close and open /dev/video%d again.",
2284 cam->v4ldev->minor);
2288 if (cam->io == IO_READ)
2289 sn9c102_empty_framequeues(cam);
2290 else if (cam->module_param.force_munmap)
2291 sn9c102_requeue_outqueue(cam);
2293 cam->stream = stream;
2300 sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2302 if (copy_to_user(arg, &cam->compression,
2303 sizeof(cam->compression)))
2311 sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2313 struct v4l2_jpegcompression jc;
2314 const enum sn9c102_stream_state stream = cam->stream;
2317 if (copy_from_user(&jc, arg, sizeof(jc)))
2320 if (jc.quality != 0 && jc.quality != 1)
2323 if (cam->stream == STREAM_ON)
2324 if ((err = sn9c102_stream_interrupt(cam)))
2327 err += sn9c102_set_compression(cam, &jc);
2328 if (err) { /* atomic, no rollback in ioctl() */
2329 cam->state |= DEV_MISCONFIGURED;
2330 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2331 "problems. To use the camera, close and open "
2332 "/dev/video%d again.", cam->v4ldev->minor);
2336 cam->compression.quality = jc.quality;
2338 cam->stream = stream;
2345 sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
2347 struct v4l2_requestbuffers rb;
2351 if (copy_from_user(&rb, arg, sizeof(rb)))
2354 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2355 rb.memory != V4L2_MEMORY_MMAP)
2358 if (cam->io == IO_READ) {
2359 DBG(3, "Close and open the device again to choose the mmap "
2364 for (i = 0; i < cam->nbuffers; i++)
2365 if (cam->frame[i].vma_use_count) {
2366 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2371 if (cam->stream == STREAM_ON)
2372 if ((err = sn9c102_stream_interrupt(cam)))
2375 sn9c102_empty_framequeues(cam);
2377 sn9c102_release_buffers(cam);
2379 rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
2381 if (copy_to_user(arg, &rb, sizeof(rb))) {
2382 sn9c102_release_buffers(cam);
2387 cam->io = rb.count ? IO_MMAP : IO_NONE;
2394 sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
2396 struct v4l2_buffer b;
2398 if (copy_from_user(&b, arg, sizeof(b)))
2401 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2402 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2405 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2407 if (cam->frame[b.index].vma_use_count)
2408 b.flags |= V4L2_BUF_FLAG_MAPPED;
2410 if (cam->frame[b.index].state == F_DONE)
2411 b.flags |= V4L2_BUF_FLAG_DONE;
2412 else if (cam->frame[b.index].state != F_UNUSED)
2413 b.flags |= V4L2_BUF_FLAG_QUEUED;
2415 if (copy_to_user(arg, &b, sizeof(b)))
2423 sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
2425 struct v4l2_buffer b;
2426 unsigned long lock_flags;
2428 if (copy_from_user(&b, arg, sizeof(b)))
2431 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2432 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2435 if (cam->frame[b.index].state != F_UNUSED)
2438 cam->frame[b.index].state = F_QUEUED;
2440 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2441 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2442 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2444 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2451 sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2454 struct v4l2_buffer b;
2455 struct sn9c102_frame_t *f;
2456 unsigned long lock_flags;
2459 if (copy_from_user(&b, arg, sizeof(b)))
2462 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2465 if (list_empty(&cam->outqueue)) {
2466 if (cam->stream == STREAM_OFF)
2468 if (filp->f_flags & O_NONBLOCK)
2470 err = wait_event_interruptible
2472 (!list_empty(&cam->outqueue)) ||
2473 (cam->state & DEV_DISCONNECTED) ||
2474 (cam->state & DEV_MISCONFIGURED) );
2477 if (cam->state & DEV_DISCONNECTED)
2479 if (cam->state & DEV_MISCONFIGURED)
2483 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2484 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
2485 list_del(cam->outqueue.next);
2486 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2488 f->state = F_UNUSED;
2490 memcpy(&b, &f->buf, sizeof(b));
2491 if (f->vma_use_count)
2492 b.flags |= V4L2_BUF_FLAG_MAPPED;
2494 if (copy_to_user(arg, &b, sizeof(b)))
2497 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2504 sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
2508 if (copy_from_user(&type, arg, sizeof(type)))
2511 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2514 if (list_empty(&cam->inqueue))
2517 cam->stream = STREAM_ON;
2519 DBG(3, "Stream on");
2526 sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
2530 if (copy_from_user(&type, arg, sizeof(type)))
2533 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2536 if (cam->stream == STREAM_ON)
2537 if ((err = sn9c102_stream_interrupt(cam)))
2540 sn9c102_empty_framequeues(cam);
2542 DBG(3, "Stream off");
2549 sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
2551 struct v4l2_streamparm sp;
2553 if (copy_from_user(&sp, arg, sizeof(sp)))
2556 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2559 sp.parm.capture.extendedmode = 0;
2560 sp.parm.capture.readbuffers = cam->nreadbuffers;
2562 if (copy_to_user(arg, &sp, sizeof(sp)))
2570 sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
2572 struct v4l2_streamparm sp;
2574 if (copy_from_user(&sp, arg, sizeof(sp)))
2577 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2580 sp.parm.capture.extendedmode = 0;
2582 if (sp.parm.capture.readbuffers == 0)
2583 sp.parm.capture.readbuffers = cam->nreadbuffers;
2585 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2586 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2588 if (copy_to_user(arg, &sp, sizeof(sp)))
2591 cam->nreadbuffers = sp.parm.capture.readbuffers;
2597 static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
2598 unsigned int cmd, void __user * arg)
2600 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2604 case VIDIOC_QUERYCAP:
2605 return sn9c102_vidioc_querycap(cam, arg);
2607 case VIDIOC_ENUMINPUT:
2608 return sn9c102_vidioc_enuminput(cam, arg);
2610 case VIDIOC_G_INPUT:
2611 case VIDIOC_S_INPUT:
2612 return sn9c102_vidioc_gs_input(cam, arg);
2614 case VIDIOC_QUERYCTRL:
2615 return sn9c102_vidioc_query_ctrl(cam, arg);
2618 return sn9c102_vidioc_g_ctrl(cam, arg);
2620 case VIDIOC_S_CTRL_OLD:
2622 return sn9c102_vidioc_s_ctrl(cam, arg);
2624 case VIDIOC_CROPCAP_OLD:
2625 case VIDIOC_CROPCAP:
2626 return sn9c102_vidioc_cropcap(cam, arg);
2629 return sn9c102_vidioc_g_crop(cam, arg);
2632 return sn9c102_vidioc_s_crop(cam, arg);
2634 case VIDIOC_ENUM_FMT:
2635 return sn9c102_vidioc_enum_fmt(cam, arg);
2638 return sn9c102_vidioc_g_fmt(cam, arg);
2640 case VIDIOC_TRY_FMT:
2642 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
2644 case VIDIOC_G_JPEGCOMP:
2645 return sn9c102_vidioc_g_jpegcomp(cam, arg);
2647 case VIDIOC_S_JPEGCOMP:
2648 return sn9c102_vidioc_s_jpegcomp(cam, arg);
2650 case VIDIOC_REQBUFS:
2651 return sn9c102_vidioc_reqbufs(cam, arg);
2653 case VIDIOC_QUERYBUF:
2654 return sn9c102_vidioc_querybuf(cam, arg);
2657 return sn9c102_vidioc_qbuf(cam, arg);
2660 return sn9c102_vidioc_dqbuf(cam, filp, arg);
2662 case VIDIOC_STREAMON:
2663 return sn9c102_vidioc_streamon(cam, arg);
2665 case VIDIOC_STREAMOFF:
2666 return sn9c102_vidioc_streamoff(cam, arg);
2669 return sn9c102_vidioc_g_parm(cam, arg);
2671 case VIDIOC_S_PARM_OLD:
2673 return sn9c102_vidioc_s_parm(cam, arg);
2677 case VIDIOC_QUERYSTD:
2678 case VIDIOC_ENUMSTD:
2679 case VIDIOC_QUERYMENU:
2689 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2690 unsigned int cmd, unsigned long arg)
2692 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2695 if (down_interruptible(&cam->fileop_sem))
2696 return -ERESTARTSYS;
2698 if (cam->state & DEV_DISCONNECTED) {
2699 DBG(1, "Device not present");
2700 up(&cam->fileop_sem);
2704 if (cam->state & DEV_MISCONFIGURED) {
2705 DBG(1, "The camera is misconfigured. Close and open it "
2707 up(&cam->fileop_sem);
2711 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2713 up(&cam->fileop_sem);
2718 /*****************************************************************************/
2720 static struct file_operations sn9c102_fops = {
2721 .owner = THIS_MODULE,
2722 .open = sn9c102_open,
2723 .release = sn9c102_release,
2724 .ioctl = sn9c102_ioctl,
2725 .read = sn9c102_read,
2726 .poll = sn9c102_poll,
2727 .mmap = sn9c102_mmap,
2728 .llseek = no_llseek,
2731 /*****************************************************************************/
2733 /* It exists a single interface only. We do not need to validate anything. */
2735 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2737 struct usb_device *udev = interface_to_usbdev(intf);
2738 struct sn9c102_device* cam;
2739 static unsigned int dev_nr = 0;
2743 if (!(cam = kmalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2747 memcpy(&cam->dev, &udev->dev, sizeof(struct device));
2749 if (!(cam->control_buffer = kmalloc(8, GFP_KERNEL))) {
2750 DBG(1, "kmalloc() failed");
2755 if (!(cam->v4ldev = video_device_alloc())) {
2756 DBG(1, "video_device_alloc() failed");
2761 init_MUTEX(&cam->dev_sem);
2763 r = sn9c102_read_reg(cam, 0x00);
2764 if (r < 0 || r != 0x10) {
2765 DBG(1, "Sorry, this is not a SN9C10x based camera "
2766 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2771 cam->bridge = (id->idProduct & 0xffc0) == 0x6080 ?
2772 BRIDGE_SN9C103 : BRIDGE_SN9C102;
2773 switch (cam->bridge) {
2774 case BRIDGE_SN9C101:
2775 case BRIDGE_SN9C102:
2776 DBG(2, "SN9C10[12] PC Camera Controller detected "
2777 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2779 case BRIDGE_SN9C103:
2780 DBG(2, "SN9C103 PC Camera Controller detected "
2781 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2785 for (i = 0; sn9c102_sensor_table[i]; i++) {
2786 err = sn9c102_sensor_table[i](cam);
2791 if (!err && cam->sensor) {
2792 DBG(2, "%s image sensor detected", cam->sensor->name);
2793 DBG(3, "Support for %s maintained by %s",
2794 cam->sensor->name, cam->sensor->maintainer);
2796 DBG(1, "No supported image sensor detected");
2801 if (sn9c102_init(cam)) {
2802 DBG(1, "Initialization failed. I will retry on open().");
2803 cam->state |= DEV_MISCONFIGURED;
2806 strcpy(cam->v4ldev->name, "SN9C10x PC Camera");
2807 cam->v4ldev->owner = THIS_MODULE;
2808 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2809 cam->v4ldev->hardware = VID_HARDWARE_SN9C102;
2810 cam->v4ldev->fops = &sn9c102_fops;
2811 cam->v4ldev->minor = video_nr[dev_nr];
2812 cam->v4ldev->release = video_device_release;
2813 video_set_drvdata(cam->v4ldev, cam);
2815 down(&cam->dev_sem);
2817 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2820 DBG(1, "V4L2 device registration failed");
2821 if (err == -ENFILE && video_nr[dev_nr] == -1)
2822 DBG(1, "Free /dev/videoX node not found");
2823 video_nr[dev_nr] = -1;
2824 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2829 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2831 cam->module_param.force_munmap = force_munmap[dev_nr];
2833 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2835 sn9c102_create_sysfs(cam);
2836 DBG(2, "Optional device control through 'sysfs' interface ready");
2838 usb_set_intfdata(intf, cam);
2846 kfree(cam->control_buffer);
2848 video_device_release(cam->v4ldev);
2855 static void sn9c102_usb_disconnect(struct usb_interface* intf)
2857 struct sn9c102_device* cam = usb_get_intfdata(intf);
2862 down_write(&sn9c102_disconnect);
2864 down(&cam->dev_sem);
2866 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2868 wake_up_interruptible_all(&cam->open);
2871 DBG(2, "Device /dev/video%d is open! Deregistration and "
2872 "memory deallocation are deferred on close.",
2873 cam->v4ldev->minor);
2874 cam->state |= DEV_MISCONFIGURED;
2875 sn9c102_stop_transfer(cam);
2876 cam->state |= DEV_DISCONNECTED;
2877 wake_up_interruptible(&cam->wait_frame);
2878 wake_up_interruptible(&cam->wait_stream);
2880 cam->state |= DEV_DISCONNECTED;
2881 sn9c102_release_resources(cam);
2889 up_write(&sn9c102_disconnect);
2893 static struct usb_driver sn9c102_usb_driver = {
2895 .id_table = sn9c102_id_table,
2896 .probe = sn9c102_usb_probe,
2897 .disconnect = sn9c102_usb_disconnect,
2900 /*****************************************************************************/
2902 static int __init sn9c102_module_init(void)
2906 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
2907 KDBG(3, SN9C102_MODULE_AUTHOR);
2909 if ((err = usb_register(&sn9c102_usb_driver)))
2910 KDBG(1, "usb_register() failed");
2916 static void __exit sn9c102_module_exit(void)
2918 usb_deregister(&sn9c102_usb_driver);
2922 module_init(sn9c102_module_init);
2923 module_exit(sn9c102_module_exit);