1 /***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers *
4 * Copyright (C) 2004-2005 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},
106 static sn9c102_eof_header_t sn9c102_eof_header[] = {
107 {0x00, 0x00, 0x00, 0x00},
108 {0x40, 0x00, 0x00, 0x00},
109 {0x80, 0x00, 0x00, 0x00},
110 {0xc0, 0x00, 0x00, 0x00},
113 /*****************************************************************************/
115 static void* rvmalloc(size_t size)
120 size = PAGE_ALIGN(size);
122 mem = vmalloc_32((unsigned long)size);
126 memset(mem, 0, size);
128 adr = (unsigned long)mem;
130 SetPageReserved(vmalloc_to_page((void *)adr));
139 static void rvfree(void* mem, size_t size)
146 size = PAGE_ALIGN(size);
148 adr = (unsigned long)mem;
150 ClearPageReserved(vmalloc_to_page((void *)adr));
160 sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
161 enum sn9c102_io_method io)
163 struct v4l2_pix_format* p = &(cam->sensor->pix_format);
164 struct v4l2_rect* r = &(cam->sensor->cropcap.bounds);
165 const size_t imagesize = cam->module_param.force_munmap ||
167 (p->width * p->height * p->priv)/8 :
168 (r->width * r->height * p->priv)/8;
172 if (count > SN9C102_MAX_FRAMES)
173 count = SN9C102_MAX_FRAMES;
175 cam->nbuffers = count;
176 while (cam->nbuffers > 0) {
177 if ((buff = rvmalloc(cam->nbuffers * PAGE_ALIGN(imagesize))))
182 for (i = 0; i < cam->nbuffers; i++) {
183 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
184 cam->frame[i].buf.index = i;
185 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
186 cam->frame[i].buf.length = imagesize;
187 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
188 cam->frame[i].buf.sequence = 0;
189 cam->frame[i].buf.field = V4L2_FIELD_NONE;
190 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
191 cam->frame[i].buf.flags = 0;
194 return cam->nbuffers;
198 static void sn9c102_release_buffers(struct sn9c102_device* cam)
201 rvfree(cam->frame[0].bufmem,
202 cam->nbuffers * cam->frame[0].buf.length);
208 static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
212 INIT_LIST_HEAD(&cam->inqueue);
213 INIT_LIST_HEAD(&cam->outqueue);
215 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
216 cam->frame[i].state = F_UNUSED;
217 cam->frame[i].buf.bytesused = 0;
222 static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
224 unsigned long lock_flags;
227 for (i = 0; i < cam->nbuffers; i++)
228 if (cam->frame[i].state == F_UNUSED) {
229 cam->frame[i].state = F_QUEUED;
230 spin_lock_irqsave(&cam->queue_lock, lock_flags);
231 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
232 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
236 /*****************************************************************************/
238 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
240 struct usb_device* udev = cam->usbdev;
241 u8* buff = cam->control_buffer;
246 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
247 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
249 DBG(3, "Failed to write a register (value 0x%02X, index "
250 "0x%02X, error %d)", value, index, res)
254 cam->reg[index] = value;
260 /* NOTE: reading some registers always returns 0 */
261 static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
263 struct usb_device* udev = cam->usbdev;
264 u8* buff = cam->control_buffer;
267 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
268 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
270 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
273 return (res >= 0) ? (int)(*buff) : -1;
277 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
282 return cam->reg[index];
287 sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
291 for (i = 1; i <= 5; i++) {
292 r = sn9c102_read_reg(cam, 0x08);
297 if (sensor->frequency & SN9C102_I2C_400KHZ)
307 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
308 struct sn9c102_sensor* sensor)
311 r = sn9c102_read_reg(cam, 0x08);
312 return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0;
317 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
318 struct sn9c102_sensor* sensor)
321 r = sn9c102_read_reg(cam, 0x08);
322 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
327 sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
328 struct sn9c102_sensor* sensor, u8 data0, u8 data1,
331 struct usb_device* udev = cam->usbdev;
332 u8* data = cam->control_buffer;
336 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
337 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
338 data[1] = data0; /* I2C slave id */
339 data[2] = data1; /* address */
341 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
342 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
346 err += sn9c102_i2c_wait(cam, sensor);
348 /* Read cycle - n bytes */
349 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
350 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
354 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
355 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
359 err += sn9c102_i2c_wait(cam, sensor);
361 /* The first read byte will be placed in data[4] */
362 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
363 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
367 err += sn9c102_i2c_detect_read_error(cam, sensor);
369 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
373 DBG(3, "I2C read failed for %s image sensor", sensor->name)
378 memcpy(buffer, data, sizeof(buffer));
385 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
386 struct sn9c102_sensor* sensor, u8 n, u8 data0,
387 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
389 struct usb_device* udev = cam->usbdev;
390 u8* data = cam->control_buffer;
393 /* Write cycle. It usually is address + value */
394 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
395 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
404 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
405 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
409 err += sn9c102_i2c_wait(cam, sensor);
410 err += sn9c102_i2c_detect_write_error(cam, sensor);
413 DBG(3, "I2C write failed for %s image sensor", sensor->name)
415 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
416 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
417 n, data0, data1, data2, data3, data4, data5)
424 sn9c102_i2c_try_read(struct sn9c102_device* cam,
425 struct sn9c102_sensor* sensor, u8 address)
427 return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
433 sn9c102_i2c_try_write(struct sn9c102_device* cam,
434 struct sn9c102_sensor* sensor, u8 address, u8 value)
436 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
437 sensor->i2c_slave_id, address,
442 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
447 return sn9c102_i2c_try_read(cam, cam->sensor, address);
451 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
456 return sn9c102_i2c_try_write(cam, cam->sensor, address, value);
459 /*****************************************************************************/
462 sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
464 size_t soflen = sizeof(sn9c102_sof_header_t), i;
465 u8 j, n = sizeof(sn9c102_sof_header) / soflen;
467 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
468 for (j = 0; j < n; j++)
469 /* It's enough to compare 7 bytes */
470 if (!memcmp(mem + i, sn9c102_sof_header[j], 7)) {
471 memcpy(cam->sof_header, mem + i, soflen);
472 /* Skip the header */
473 return mem + i + soflen;
481 sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
483 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
484 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
486 if (cam->sensor->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
487 return NULL; /* EOF header does not exist in compressed data */
489 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
490 for (j = 0; j < n; j++)
491 if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen))
498 static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
500 struct sn9c102_device* cam = urb->context;
501 struct sn9c102_frame_t** f;
502 unsigned long lock_flags;
506 if (urb->status == -ENOENT)
509 f = &cam->frame_current;
511 if (cam->stream == STREAM_INTERRUPT) {
512 cam->stream = STREAM_OFF;
514 (*f)->state = F_QUEUED;
515 DBG(3, "Stream interrupted")
516 wake_up_interruptible(&cam->wait_stream);
519 if ((cam->state & DEV_DISCONNECTED)||(cam->state & DEV_MISCONFIGURED))
522 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
526 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
529 for (i = 0; i < urb->number_of_packets; i++) {
530 unsigned int img, len, status;
531 void *pos, *sof, *eof;
533 len = urb->iso_frame_desc[i].actual_length;
534 status = urb->iso_frame_desc[i].status;
535 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
538 DBG(3, "Error in isochronous frame")
539 (*f)->state = F_ERROR;
543 PDBGG("Isochrnous frame: length %u, #%u i", len, i)
546 NOTE: It is probably correct to assume that SOF and EOF
547 headers do not occur between two consecutive packets,
548 but who knows..Whatever is the truth, this assumption
549 doesn't introduce bugs.
553 sof = sn9c102_find_sof_header(cam, pos, len);
555 eof = sn9c102_find_eof_header(cam, pos, len);
556 if ((*f)->state == F_GRABBING) {
561 img = (eof > pos) ? eof - pos - 1 : 0;
563 if ((*f)->buf.bytesused+img>(*f)->buf.length) {
564 u32 b = (*f)->buf.bytesused + img -
566 img = (*f)->buf.length -
568 DBG(3, "Expected EOF not found: "
571 DBG(3, "Exceeded limit: +%u "
572 "bytes", (unsigned)(b))
575 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
578 if ((*f)->buf.bytesused == 0)
579 do_gettimeofday(&(*f)->buf.timestamp);
581 (*f)->buf.bytesused += img;
583 if ((*f)->buf.bytesused == (*f)->buf.length ||
584 (cam->sensor->pix_format.pixelformat ==
585 V4L2_PIX_FMT_SN9C10X && eof)) {
586 u32 b = (*f)->buf.bytesused;
587 (*f)->state = F_DONE;
588 (*f)->buf.sequence= ++cam->frame_count;
589 spin_lock_irqsave(&cam->queue_lock,
591 list_move_tail(&(*f)->frame,
593 if (!list_empty(&cam->inqueue))
596 struct sn9c102_frame_t,
600 spin_unlock_irqrestore(&cam->queue_lock
602 memcpy(cam->sysfs.frame_header,
604 sizeof(sn9c102_sof_header_t));
605 DBG(3, "Video frame captured: "
606 "%lu bytes", (unsigned long)(b))
612 (*f)->state = F_ERROR;
613 DBG(3, "Not expected EOF after %lu "
614 "bytes of image data",
615 (unsigned long)((*f)->buf.bytesused))
622 DBG(3, "EOF without SOF")
626 PDBGG("Ignoring pointless isochronous frame")
630 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
632 (*f)->state = F_GRABBING;
633 (*f)->buf.bytesused = 0;
636 DBG(3, "SOF detected: new video frame")
640 } else if ((*f)->state == F_GRABBING) {
641 eof = sn9c102_find_eof_header(cam, pos, len);
642 if (eof && eof < sof)
643 goto end_of_frame; /* (1) */
645 if (cam->sensor->pix_format.pixelformat ==
646 V4L2_PIX_FMT_SN9C10X) {
647 eof = sof-sizeof(sn9c102_sof_header_t);
650 DBG(3, "SOF before expected EOF after "
651 "%lu bytes of image data",
652 (unsigned long)((*f)->buf.bytesused))
660 urb->dev = cam->usbdev;
661 err = usb_submit_urb(urb, GFP_ATOMIC);
662 if (err < 0 && err != -EPERM) {
663 cam->state |= DEV_MISCONFIGURED;
664 DBG(1, "usb_submit_urb() failed")
667 wake_up_interruptible(&cam->wait_frame);
671 static int sn9c102_start_transfer(struct sn9c102_device* cam)
673 struct usb_device *udev = cam->usbdev;
675 const unsigned int wMaxPacketSize[] = {0, 128, 256, 384, 512,
676 680, 800, 900, 1023};
677 const unsigned int psz = wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
681 for (i = 0; i < SN9C102_URBS; i++) {
682 cam->transfer_buffer[i] = kmalloc(SN9C102_ISO_PACKETS * psz,
684 if (!cam->transfer_buffer[i]) {
686 DBG(1, "Not enough memory")
691 for (i = 0; i < SN9C102_URBS; i++) {
692 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
696 DBG(1, "usb_alloc_urb() failed")
701 urb->pipe = usb_rcvisocpipe(udev, 1);
702 urb->transfer_flags = URB_ISO_ASAP;
703 urb->number_of_packets = SN9C102_ISO_PACKETS;
704 urb->complete = sn9c102_urb_complete;
705 urb->transfer_buffer = cam->transfer_buffer[i];
706 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
708 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
709 urb->iso_frame_desc[j].offset = psz * j;
710 urb->iso_frame_desc[j].length = psz;
715 if (!(cam->reg[0x01] & 0x04)) {
716 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
719 DBG(1, "I/O hardware error")
724 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
726 DBG(1, "usb_set_interface() failed")
730 cam->frame_current = NULL;
732 for (i = 0; i < SN9C102_URBS; i++) {
733 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
735 for (j = i-1; j >= 0; j--)
736 usb_kill_urb(cam->urb[j]);
737 DBG(1, "usb_submit_urb() failed, error %d", err)
745 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
746 usb_free_urb(cam->urb[i]);
749 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
750 kfree(cam->transfer_buffer[i]);
756 static int sn9c102_stop_transfer(struct sn9c102_device* cam)
758 struct usb_device *udev = cam->usbdev;
762 if (cam->state & DEV_DISCONNECTED)
765 for (i = SN9C102_URBS-1; i >= 0; i--) {
766 usb_kill_urb(cam->urb[i]);
767 usb_free_urb(cam->urb[i]);
768 kfree(cam->transfer_buffer[i]);
771 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
773 DBG(3, "usb_set_interface() failed")
779 int sn9c102_stream_interrupt(struct sn9c102_device* cam)
783 cam->stream = STREAM_INTERRUPT;
784 err = wait_event_timeout(cam->wait_stream,
785 (cam->stream == STREAM_OFF) ||
786 (cam->state & DEV_DISCONNECTED),
787 SN9C102_URB_TIMEOUT);
788 if (cam->state & DEV_DISCONNECTED)
791 cam->state |= DEV_MISCONFIGURED;
792 DBG(1, "The camera is misconfigured. To use it, close and "
793 "open /dev/video%d again.", cam->v4ldev->minor)
800 /*****************************************************************************/
802 static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
809 strncpy(str, buff, len);
812 strncpy(str, buff, 4);
816 val = simple_strtoul(str, &endp, 0);
820 *count = (ssize_t)(endp - str);
821 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
828 NOTE 1: being inside one of the following methods implies that the v4l
829 device exists for sure (see kobjects and reference counters)
830 NOTE 2: buffers are PAGE_SIZE long
833 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
835 struct sn9c102_device* cam;
838 if (down_interruptible(&sn9c102_sysfs_lock))
841 cam = video_get_drvdata(to_video_device(cd));
843 up(&sn9c102_sysfs_lock);
847 count = sprintf(buf, "%u\n", cam->sysfs.reg);
849 up(&sn9c102_sysfs_lock);
856 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
858 struct sn9c102_device* cam;
862 if (down_interruptible(&sn9c102_sysfs_lock))
865 cam = video_get_drvdata(to_video_device(cd));
867 up(&sn9c102_sysfs_lock);
871 index = sn9c102_strtou8(buf, len, &count);
872 if (index > 0x1f || !count) {
873 up(&sn9c102_sysfs_lock);
877 cam->sysfs.reg = index;
879 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg)
880 DBG(3, "Written bytes: %zd", count)
882 up(&sn9c102_sysfs_lock);
888 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
890 struct sn9c102_device* cam;
894 if (down_interruptible(&sn9c102_sysfs_lock))
897 cam = video_get_drvdata(to_video_device(cd));
899 up(&sn9c102_sysfs_lock);
903 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
904 up(&sn9c102_sysfs_lock);
908 count = sprintf(buf, "%d\n", val);
910 DBG(3, "Read bytes: %zd", count)
912 up(&sn9c102_sysfs_lock);
919 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
921 struct sn9c102_device* cam;
926 if (down_interruptible(&sn9c102_sysfs_lock))
929 cam = video_get_drvdata(to_video_device(cd));
931 up(&sn9c102_sysfs_lock);
935 value = sn9c102_strtou8(buf, len, &count);
937 up(&sn9c102_sysfs_lock);
941 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
943 up(&sn9c102_sysfs_lock);
947 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
948 cam->sysfs.reg, value)
949 DBG(3, "Written bytes: %zd", count)
951 up(&sn9c102_sysfs_lock);
957 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
959 struct sn9c102_device* cam;
962 if (down_interruptible(&sn9c102_sysfs_lock))
965 cam = video_get_drvdata(to_video_device(cd));
967 up(&sn9c102_sysfs_lock);
971 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
973 DBG(3, "Read bytes: %zd", count)
975 up(&sn9c102_sysfs_lock);
982 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
984 struct sn9c102_device* cam;
988 if (down_interruptible(&sn9c102_sysfs_lock))
991 cam = video_get_drvdata(to_video_device(cd));
993 up(&sn9c102_sysfs_lock);
997 index = sn9c102_strtou8(buf, len, &count);
999 up(&sn9c102_sysfs_lock);
1003 cam->sysfs.i2c_reg = index;
1005 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg)
1006 DBG(3, "Written bytes: %zd", count)
1008 up(&sn9c102_sysfs_lock);
1014 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1016 struct sn9c102_device* cam;
1020 if (down_interruptible(&sn9c102_sysfs_lock))
1021 return -ERESTARTSYS;
1023 cam = video_get_drvdata(to_video_device(cd));
1025 up(&sn9c102_sysfs_lock);
1029 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_READ)) {
1030 up(&sn9c102_sysfs_lock);
1034 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1035 up(&sn9c102_sysfs_lock);
1039 count = sprintf(buf, "%d\n", val);
1041 DBG(3, "Read bytes: %zd", count)
1043 up(&sn9c102_sysfs_lock);
1050 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1052 struct sn9c102_device* cam;
1057 if (down_interruptible(&sn9c102_sysfs_lock))
1058 return -ERESTARTSYS;
1060 cam = video_get_drvdata(to_video_device(cd));
1062 up(&sn9c102_sysfs_lock);
1066 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_WRITE)) {
1067 up(&sn9c102_sysfs_lock);
1071 value = sn9c102_strtou8(buf, len, &count);
1073 up(&sn9c102_sysfs_lock);
1077 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1079 up(&sn9c102_sysfs_lock);
1083 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1084 cam->sysfs.i2c_reg, value)
1085 DBG(3, "Written bytes: %zd", count)
1087 up(&sn9c102_sysfs_lock);
1094 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1096 struct sn9c102_device* cam;
1097 enum sn9c102_bridge bridge;
1102 if (down_interruptible(&sn9c102_sysfs_lock))
1103 return -ERESTARTSYS;
1105 cam = video_get_drvdata(to_video_device(cd));
1107 up(&sn9c102_sysfs_lock);
1111 bridge = cam->bridge;
1113 up(&sn9c102_sysfs_lock);
1115 value = sn9c102_strtou8(buf, len, &count);
1120 case BRIDGE_SN9C101:
1121 case BRIDGE_SN9C102:
1124 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1125 res = sn9c102_store_val(cd, buf, len);
1127 case BRIDGE_SN9C103:
1130 if ((res = sn9c102_store_reg(cd, "0x04", 4)) >= 0)
1131 res = sn9c102_store_val(cd, buf, len);
1140 sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1146 value = sn9c102_strtou8(buf, len, &count);
1147 if (!count || value > 0x7f)
1150 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1151 res = sn9c102_store_val(cd, buf, len);
1158 sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1164 value = sn9c102_strtou8(buf, len, &count);
1165 if (!count || value > 0x7f)
1168 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1169 res = sn9c102_store_val(cd, buf, len);
1175 static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1177 struct sn9c102_device* cam;
1180 cam = video_get_drvdata(to_video_device(cd));
1184 count = sizeof(cam->sysfs.frame_header);
1185 memcpy(buf, cam->sysfs.frame_header, count);
1187 DBG(3, "Frame header, read bytes: %zd", count)
1193 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1194 sn9c102_show_reg, sn9c102_store_reg);
1195 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1196 sn9c102_show_val, sn9c102_store_val);
1197 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1198 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1199 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1200 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1201 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1202 static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1203 static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1204 static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
1205 sn9c102_show_frame_header, NULL);
1208 static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1210 struct video_device *v4ldev = cam->v4ldev;
1212 video_device_create_file(v4ldev, &class_device_attr_reg);
1213 video_device_create_file(v4ldev, &class_device_attr_val);
1214 video_device_create_file(v4ldev, &class_device_attr_frame_header);
1215 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
1216 video_device_create_file(v4ldev, &class_device_attr_green);
1217 else if (cam->bridge == BRIDGE_SN9C103) {
1218 video_device_create_file(v4ldev, &class_device_attr_blue);
1219 video_device_create_file(v4ldev, &class_device_attr_red);
1221 if (cam->sensor->sysfs_ops) {
1222 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1223 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1227 /*****************************************************************************/
1230 sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
1234 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
1235 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, 0x18);
1237 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, 0x18);
1239 return err ? -EIO : 0;
1244 sn9c102_set_compression(struct sn9c102_device* cam,
1245 struct v4l2_jpegcompression* compression)
1249 if (compression->quality == 0)
1250 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, 0x17);
1251 else if (compression->quality == 1)
1252 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, 0x17);
1254 return err ? -EIO : 0;
1258 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1264 r = cam->reg[0x18] & 0xcf;
1265 else if (scale == 2) {
1266 r = cam->reg[0x18] & 0xcf;
1268 } else if (scale == 4)
1269 r = cam->reg[0x18] | 0x20;
1271 err += sn9c102_write_reg(cam, r, 0x18);
1275 PDBGG("Scaling factor: %u", scale)
1281 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1283 struct sn9c102_sensor* s = cam->sensor;
1284 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1285 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1286 h_size = (u8)(rect->width / 16),
1287 v_size = (u8)(rect->height / 16);
1290 err += sn9c102_write_reg(cam, h_start, 0x12);
1291 err += sn9c102_write_reg(cam, v_start, 0x13);
1292 err += sn9c102_write_reg(cam, h_size, 0x15);
1293 err += sn9c102_write_reg(cam, v_size, 0x16);
1297 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1298 "%u %u %u %u", h_start, v_start, h_size, v_size)
1304 static int sn9c102_init(struct sn9c102_device* cam)
1306 struct sn9c102_sensor* s = cam->sensor;
1307 struct v4l2_control ctrl;
1308 struct v4l2_queryctrl *qctrl;
1309 struct v4l2_rect* rect;
1313 if (!(cam->state & DEV_INITIALIZED)) {
1314 init_waitqueue_head(&cam->open);
1316 rect = &(s->cropcap.defrect);
1317 } else { /* use current values */
1322 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1323 err += sn9c102_set_crop(cam, rect);
1330 DBG(3, "Sensor initialization failed")
1335 if (!(cam->state & DEV_INITIALIZED))
1336 cam->compression.quality = cam->reg[0x17] & 0x01 ? 0 : 1;
1338 err += sn9c102_set_compression(cam, &cam->compression);
1339 err += sn9c102_set_pix_format(cam, &s->pix_format);
1340 if (s->set_pix_format)
1341 err += s->set_pix_format(cam, &s->pix_format);
1345 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1346 DBG(3, "Compressed video format is active, quality %d",
1347 cam->compression.quality)
1349 DBG(3, "Uncompressed video format is active")
1352 if ((err = s->set_crop(cam, rect))) {
1353 DBG(3, "set_crop() failed")
1358 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1359 for (i = 0; i < n; i++)
1360 if (s->qctrl[i].id != 0 &&
1361 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1362 ctrl.id = s->qctrl[i].id;
1363 ctrl.value = qctrl[i].default_value;
1364 err = s->set_ctrl(cam, &ctrl);
1366 DBG(3, "Set %s control failed",
1370 DBG(3, "Image sensor supports '%s' control",
1375 if (!(cam->state & DEV_INITIALIZED)) {
1376 init_MUTEX(&cam->fileop_sem);
1377 spin_lock_init(&cam->queue_lock);
1378 init_waitqueue_head(&cam->wait_frame);
1379 init_waitqueue_head(&cam->wait_stream);
1380 cam->nreadbuffers = 2;
1381 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1382 memcpy(&(s->_rect), &(s->cropcap.defrect),
1383 sizeof(struct v4l2_rect));
1384 cam->state |= DEV_INITIALIZED;
1387 DBG(2, "Initialization succeeded")
1392 static void sn9c102_release_resources(struct sn9c102_device* cam)
1394 down(&sn9c102_sysfs_lock);
1396 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor)
1397 video_set_drvdata(cam->v4ldev, NULL);
1398 video_unregister_device(cam->v4ldev);
1400 up(&sn9c102_sysfs_lock);
1402 kfree(cam->control_buffer);
1405 /*****************************************************************************/
1407 static int sn9c102_open(struct inode* inode, struct file* filp)
1409 struct sn9c102_device* cam;
1413 This is the only safe way to prevent race conditions with
1416 if (!down_read_trylock(&sn9c102_disconnect))
1417 return -ERESTARTSYS;
1419 cam = video_get_drvdata(video_devdata(filp));
1421 if (down_interruptible(&cam->dev_sem)) {
1422 up_read(&sn9c102_disconnect);
1423 return -ERESTARTSYS;
1427 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor)
1428 if ((filp->f_flags & O_NONBLOCK) ||
1429 (filp->f_flags & O_NDELAY)) {
1434 err = wait_event_interruptible_exclusive(cam->open,
1435 cam->state & DEV_DISCONNECTED
1438 up_read(&sn9c102_disconnect);
1441 if (cam->state & DEV_DISCONNECTED) {
1442 up_read(&sn9c102_disconnect);
1445 down(&cam->dev_sem);
1449 if (cam->state & DEV_MISCONFIGURED) {
1450 err = sn9c102_init(cam);
1452 DBG(1, "Initialization failed again. "
1453 "I will retry on next open().")
1456 cam->state &= ~DEV_MISCONFIGURED;
1459 if ((err = sn9c102_start_transfer(cam)))
1462 filp->private_data = cam;
1465 cam->stream = STREAM_OFF;
1467 cam->frame_count = 0;
1468 sn9c102_empty_framequeues(cam);
1470 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor)
1474 up_read(&sn9c102_disconnect);
1479 static int sn9c102_release(struct inode* inode, struct file* filp)
1481 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1483 down(&cam->dev_sem); /* prevent disconnect() to be called */
1485 sn9c102_stop_transfer(cam);
1487 sn9c102_release_buffers(cam);
1489 if (cam->state & DEV_DISCONNECTED) {
1490 sn9c102_release_resources(cam);
1497 wake_up_interruptible_nr(&cam->open, 1);
1499 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor)
1508 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1510 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1511 struct sn9c102_frame_t* f, * i;
1512 unsigned long lock_flags;
1515 if (down_interruptible(&cam->fileop_sem))
1516 return -ERESTARTSYS;
1518 if (cam->state & DEV_DISCONNECTED) {
1519 DBG(1, "Device not present")
1520 up(&cam->fileop_sem);
1524 if (cam->state & DEV_MISCONFIGURED) {
1525 DBG(1, "The camera is misconfigured. Close and open it again.")
1526 up(&cam->fileop_sem);
1530 if (cam->io == IO_MMAP) {
1531 DBG(3, "Close and open the device again to choose "
1533 up(&cam->fileop_sem);
1537 if (cam->io == IO_NONE) {
1538 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1539 DBG(1, "read() failed, not enough memory")
1540 up(&cam->fileop_sem);
1544 cam->stream = STREAM_ON;
1545 sn9c102_queue_unusedframes(cam);
1549 up(&cam->fileop_sem);
1553 if (list_empty(&cam->outqueue)) {
1554 if (filp->f_flags & O_NONBLOCK) {
1555 up(&cam->fileop_sem);
1558 err = wait_event_interruptible
1560 (!list_empty(&cam->outqueue)) ||
1561 (cam->state & DEV_DISCONNECTED) );
1563 up(&cam->fileop_sem);
1566 if (cam->state & DEV_DISCONNECTED) {
1567 up(&cam->fileop_sem);
1572 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1574 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1575 list_for_each_entry(i, &cam->outqueue, frame)
1576 i->state = F_UNUSED;
1577 INIT_LIST_HEAD(&cam->outqueue);
1578 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1580 sn9c102_queue_unusedframes(cam);
1582 if (count > f->buf.bytesused)
1583 count = f->buf.bytesused;
1585 if (copy_to_user(buf, f->bufmem, count)) {
1586 up(&cam->fileop_sem);
1591 PDBGG("Frame #%lu, bytes read: %zu", (unsigned long)f->buf.index,count)
1593 up(&cam->fileop_sem);
1599 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1601 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1602 unsigned int mask = 0;
1604 if (down_interruptible(&cam->fileop_sem))
1607 if (cam->state & DEV_DISCONNECTED) {
1608 DBG(1, "Device not present")
1612 if (cam->state & DEV_MISCONFIGURED) {
1613 DBG(1, "The camera is misconfigured. Close and open it again.")
1617 if (cam->io == IO_NONE) {
1618 if (!sn9c102_request_buffers(cam, 2, IO_READ)) {
1619 DBG(1, "poll() failed, not enough memory")
1623 cam->stream = STREAM_ON;
1626 if (cam->io == IO_READ)
1627 sn9c102_queue_unusedframes(cam);
1629 poll_wait(filp, &cam->wait_frame, wait);
1631 if (!list_empty(&cam->outqueue))
1632 mask |= POLLIN | POLLRDNORM;
1634 up(&cam->fileop_sem);
1639 up(&cam->fileop_sem);
1644 static void sn9c102_vm_open(struct vm_area_struct* vma)
1646 struct sn9c102_frame_t* f = vma->vm_private_data;
1651 static void sn9c102_vm_close(struct vm_area_struct* vma)
1653 /* NOTE: buffers are not freed here */
1654 struct sn9c102_frame_t* f = vma->vm_private_data;
1659 static struct vm_operations_struct sn9c102_vm_ops = {
1660 .open = sn9c102_vm_open,
1661 .close = sn9c102_vm_close,
1665 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1667 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1668 unsigned long size = vma->vm_end - vma->vm_start,
1669 start = vma->vm_start,
1674 if (down_interruptible(&cam->fileop_sem))
1675 return -ERESTARTSYS;
1677 if (cam->state & DEV_DISCONNECTED) {
1678 DBG(1, "Device not present")
1679 up(&cam->fileop_sem);
1683 if (cam->state & DEV_MISCONFIGURED) {
1684 DBG(1, "The camera is misconfigured. Close and open it again.")
1685 up(&cam->fileop_sem);
1689 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1690 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1691 up(&cam->fileop_sem);
1695 for (i = 0; i < cam->nbuffers; i++) {
1696 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1699 if (i == cam->nbuffers) {
1700 up(&cam->fileop_sem);
1704 /* VM_IO is eventually going to replace PageReserved altogether */
1705 vma->vm_flags |= VM_IO;
1706 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1708 pos = (unsigned long)cam->frame[i].bufmem;
1709 while (size > 0) { /* size is page-aligned */
1710 page = vmalloc_to_pfn((void *)pos);
1711 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
1712 vma->vm_page_prot)) {
1713 up(&cam->fileop_sem);
1721 vma->vm_ops = &sn9c102_vm_ops;
1722 vma->vm_private_data = &cam->frame[i];
1724 sn9c102_vm_open(vma);
1726 up(&cam->fileop_sem);
1732 static int sn9c102_v4l2_ioctl(struct inode* inode, struct file* filp,
1733 unsigned int cmd, void __user * arg)
1735 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1739 case VIDIOC_QUERYCAP:
1741 struct v4l2_capability cap = {
1742 .driver = "sn9c102",
1743 .version = SN9C102_MODULE_VERSION_CODE,
1744 .capabilities = V4L2_CAP_VIDEO_CAPTURE |
1745 V4L2_CAP_READWRITE |
1749 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1750 if (usb_make_path(cam->usbdev, cap.bus_info,
1751 sizeof(cap.bus_info)) < 0)
1752 strlcpy(cap.bus_info, cam->dev.bus_id,
1753 sizeof(cap.bus_info));
1755 if (copy_to_user(arg, &cap, sizeof(cap)))
1761 case VIDIOC_ENUMINPUT:
1763 struct v4l2_input i;
1765 if (copy_from_user(&i, arg, sizeof(i)))
1771 memset(&i, 0, sizeof(i));
1772 strcpy(i.name, "USB");
1774 if (copy_to_user(arg, &i, sizeof(i)))
1780 case VIDIOC_G_INPUT:
1781 case VIDIOC_S_INPUT:
1785 if (copy_from_user(&index, arg, sizeof(index)))
1794 case VIDIOC_QUERYCTRL:
1796 struct sn9c102_sensor* s = cam->sensor;
1797 struct v4l2_queryctrl qc;
1800 if (copy_from_user(&qc, arg, sizeof(qc)))
1803 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1804 for (i = 0; i < n; i++)
1805 if (qc.id && qc.id == s->qctrl[i].id) {
1806 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1807 if (copy_to_user(arg, &qc, sizeof(qc)))
1817 struct sn9c102_sensor* s = cam->sensor;
1818 struct v4l2_control ctrl;
1824 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1827 err = s->get_ctrl(cam, &ctrl);
1829 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1835 case VIDIOC_S_CTRL_OLD:
1838 struct sn9c102_sensor* s = cam->sensor;
1839 struct v4l2_control ctrl;
1846 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1849 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1850 for (i = 0; i < n; i++)
1851 if (ctrl.id == s->qctrl[i].id) {
1852 if (ctrl.value < s->qctrl[i].minimum ||
1853 ctrl.value > s->qctrl[i].maximum)
1855 ctrl.value -= ctrl.value % s->qctrl[i].step;
1859 if ((err = s->set_ctrl(cam, &ctrl)))
1862 s->_qctrl[i].default_value = ctrl.value;
1864 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
1865 (unsigned long)ctrl.id, (unsigned long)ctrl.value)
1870 case VIDIOC_CROPCAP:
1872 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1874 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1875 cc->pixelaspect.numerator = 1;
1876 cc->pixelaspect.denominator = 1;
1878 if (copy_to_user(arg, cc, sizeof(*cc)))
1886 struct sn9c102_sensor* s = cam->sensor;
1887 struct v4l2_crop crop = {
1888 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1891 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1893 if (copy_to_user(arg, &crop, sizeof(crop)))
1901 struct sn9c102_sensor* s = cam->sensor;
1902 struct v4l2_crop crop;
1903 struct v4l2_rect* rect;
1904 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1905 struct v4l2_pix_format* pix_format = &(s->pix_format);
1907 const enum sn9c102_stream_state stream = cam->stream;
1908 const u32 nbuffers = cam->nbuffers;
1912 if (copy_from_user(&crop, arg, sizeof(crop)))
1917 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1920 if (cam->module_param.force_munmap)
1921 for (i = 0; i < cam->nbuffers; i++)
1922 if (cam->frame[i].vma_use_count) {
1923 DBG(3, "VIDIOC_S_CROP failed. "
1924 "Unmap the buffers first.")
1928 /* Preserve R,G or B origin */
1929 rect->left = (s->_rect.left & 1L) ?
1930 rect->left | 1L : rect->left & ~1L;
1931 rect->top = (s->_rect.top & 1L) ?
1932 rect->top | 1L : rect->top & ~1L;
1934 if (rect->width < 16)
1936 if (rect->height < 16)
1938 if (rect->width > bounds->width)
1939 rect->width = bounds->width;
1940 if (rect->height > bounds->height)
1941 rect->height = bounds->height;
1942 if (rect->left < bounds->left)
1943 rect->left = bounds->left;
1944 if (rect->top < bounds->top)
1945 rect->top = bounds->top;
1946 if (rect->left + rect->width > bounds->left + bounds->width)
1947 rect->left = bounds->left+bounds->width - rect->width;
1948 if (rect->top + rect->height > bounds->top + bounds->height)
1949 rect->top = bounds->top+bounds->height - rect->height;
1951 rect->width &= ~15L;
1952 rect->height &= ~15L;
1954 if (SN9C102_PRESERVE_IMGSCALE) {
1955 /* Calculate the actual scaling factor */
1957 a = rect->width * rect->height;
1958 b = pix_format->width * pix_format->height;
1959 scale = b ? (u8)((a / b) < 4 ? 1 :
1960 ((a / b) < 16 ? 2 : 4)) : 1;
1964 if (cam->stream == STREAM_ON)
1965 if ((err = sn9c102_stream_interrupt(cam)))
1968 if (copy_to_user(arg, &crop, sizeof(crop))) {
1969 cam->stream = stream;
1973 if (cam->module_param.force_munmap)
1974 sn9c102_release_buffers(cam);
1976 err = sn9c102_set_crop(cam, rect);
1978 err += s->set_crop(cam, rect);
1979 err += sn9c102_set_scale(cam, scale);
1981 if (err) { /* atomic, no rollback in ioctl() */
1982 cam->state |= DEV_MISCONFIGURED;
1983 DBG(1, "VIDIOC_S_CROP failed because of hardware "
1984 "problems. To use the camera, close and open "
1985 "/dev/video%d again.", cam->v4ldev->minor)
1989 s->pix_format.width = rect->width/scale;
1990 s->pix_format.height = rect->height/scale;
1991 memcpy(&(s->_rect), rect, sizeof(*rect));
1993 if (cam->module_param.force_munmap &&
1994 nbuffers != sn9c102_request_buffers(cam, nbuffers,
1996 cam->state |= DEV_MISCONFIGURED;
1997 DBG(1, "VIDIOC_S_CROP failed because of not enough "
1998 "memory. To use the camera, close and open "
1999 "/dev/video%d again.", cam->v4ldev->minor)
2003 cam->stream = stream;
2008 case VIDIOC_ENUM_FMT:
2010 struct v4l2_fmtdesc fmtd;
2012 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
2015 if (fmtd.index == 0) {
2016 strcpy(fmtd.description, "bayer rgb");
2017 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2018 } else if (fmtd.index == 1) {
2019 strcpy(fmtd.description, "compressed");
2020 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2021 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2025 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2026 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2028 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
2036 struct v4l2_format format;
2037 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
2039 if (copy_from_user(&format, arg, sizeof(format)))
2042 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2045 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X)
2046 ? 0 : (pfmt->width * pfmt->priv) / 8;
2047 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2048 pfmt->field = V4L2_FIELD_NONE;
2049 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2051 if (copy_to_user(arg, &format, sizeof(format)))
2057 case VIDIOC_TRY_FMT:
2060 struct sn9c102_sensor* s = cam->sensor;
2061 struct v4l2_format format;
2062 struct v4l2_pix_format* pix;
2063 struct v4l2_pix_format* pfmt = &(s->pix_format);
2064 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2065 struct v4l2_rect rect;
2067 const enum sn9c102_stream_state stream = cam->stream;
2068 const u32 nbuffers = cam->nbuffers;
2072 if (copy_from_user(&format, arg, sizeof(format)))
2075 pix = &(format.fmt.pix);
2077 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2080 memcpy(&rect, &(s->_rect), sizeof(rect));
2082 { /* calculate the actual scaling factor */
2084 a = rect.width * rect.height;
2085 b = pix->width * pix->height;
2086 scale = b ? (u8)((a / b) < 4 ? 1 :
2087 ((a / b) < 16 ? 2 : 4)) : 1;
2090 rect.width = scale * pix->width;
2091 rect.height = scale * pix->height;
2093 if (rect.width < 16)
2095 if (rect.height < 16)
2097 if (rect.width > bounds->left + bounds->width - rect.left)
2098 rect.width = bounds->left + bounds->width - rect.left;
2099 if (rect.height > bounds->top + bounds->height - rect.top)
2100 rect.height = bounds->top + bounds->height - rect.top;
2103 rect.height &= ~15L;
2105 { /* adjust the scaling factor */
2107 a = rect.width * rect.height;
2108 b = pix->width * pix->height;
2109 scale = b ? (u8)((a / b) < 4 ? 1 :
2110 ((a / b) < 16 ? 2 : 4)) : 1;
2113 pix->width = rect.width / scale;
2114 pix->height = rect.height / scale;
2116 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2117 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2118 pix->pixelformat = pfmt->pixelformat;
2119 pix->priv = pfmt->priv; /* bpp */
2120 pix->colorspace = pfmt->colorspace;
2121 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
2122 ? 0 : (pix->width * pix->priv) / 8;
2123 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2124 pix->field = V4L2_FIELD_NONE;
2126 if (cmd == VIDIOC_TRY_FMT) {
2127 if (copy_to_user(arg, &format, sizeof(format)))
2132 if (cam->module_param.force_munmap)
2133 for (i = 0; i < cam->nbuffers; i++)
2134 if (cam->frame[i].vma_use_count) {
2135 DBG(3, "VIDIOC_S_FMT failed. "
2136 "Unmap the buffers first.")
2140 if (cam->stream == STREAM_ON)
2141 if ((err = sn9c102_stream_interrupt(cam)))
2144 if (copy_to_user(arg, &format, sizeof(format))) {
2145 cam->stream = stream;
2149 if (cam->module_param.force_munmap)
2150 sn9c102_release_buffers(cam);
2152 err += sn9c102_set_pix_format(cam, pix);
2153 err += sn9c102_set_crop(cam, &rect);
2154 if (s->set_pix_format)
2155 err += s->set_pix_format(cam, pix);
2157 err += s->set_crop(cam, &rect);
2158 err += sn9c102_set_scale(cam, scale);
2160 if (err) { /* atomic, no rollback in ioctl() */
2161 cam->state |= DEV_MISCONFIGURED;
2162 DBG(1, "VIDIOC_S_FMT failed because of hardware "
2163 "problems. To use the camera, close and open "
2164 "/dev/video%d again.", cam->v4ldev->minor)
2168 memcpy(pfmt, pix, sizeof(*pix));
2169 memcpy(&(s->_rect), &rect, sizeof(rect));
2171 if (cam->module_param.force_munmap &&
2172 nbuffers != sn9c102_request_buffers(cam, nbuffers,
2174 cam->state |= DEV_MISCONFIGURED;
2175 DBG(1, "VIDIOC_S_FMT failed because of not enough "
2176 "memory. To use the camera, close and open "
2177 "/dev/video%d again.", cam->v4ldev->minor)
2181 cam->stream = stream;
2186 case VIDIOC_G_JPEGCOMP:
2188 if (copy_to_user(arg, &cam->compression,
2189 sizeof(cam->compression)))
2195 case VIDIOC_S_JPEGCOMP:
2197 struct v4l2_jpegcompression jc;
2198 const enum sn9c102_stream_state stream = cam->stream;
2201 if (copy_from_user(&jc, arg, sizeof(jc)))
2204 if (jc.quality != 0 && jc.quality != 1)
2207 if (cam->stream == STREAM_ON)
2208 if ((err = sn9c102_stream_interrupt(cam)))
2211 err += sn9c102_set_compression(cam, &jc);
2212 if (err) { /* atomic, no rollback in ioctl() */
2213 cam->state |= DEV_MISCONFIGURED;
2214 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2215 "problems. To use the camera, close and open "
2216 "/dev/video%d again.", cam->v4ldev->minor)
2220 cam->compression.quality = jc.quality;
2222 cam->stream = stream;
2227 case VIDIOC_REQBUFS:
2229 struct v4l2_requestbuffers rb;
2233 if (copy_from_user(&rb, arg, sizeof(rb)))
2236 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2237 rb.memory != V4L2_MEMORY_MMAP)
2240 if (cam->io == IO_READ) {
2241 DBG(3, "Close and open the device again to choose "
2242 "the mmap I/O method")
2246 for (i = 0; i < cam->nbuffers; i++)
2247 if (cam->frame[i].vma_use_count) {
2248 DBG(3, "VIDIOC_REQBUFS failed. "
2249 "Previous buffers are still mapped.")
2253 if (cam->stream == STREAM_ON)
2254 if ((err = sn9c102_stream_interrupt(cam)))
2257 sn9c102_empty_framequeues(cam);
2259 sn9c102_release_buffers(cam);
2261 rb.count = sn9c102_request_buffers(cam, rb.count,
2264 if (copy_to_user(arg, &rb, sizeof(rb))) {
2265 sn9c102_release_buffers(cam);
2270 cam->io = rb.count ? IO_MMAP : IO_NONE;
2275 case VIDIOC_QUERYBUF:
2277 struct v4l2_buffer b;
2279 if (copy_from_user(&b, arg, sizeof(b)))
2282 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2283 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2286 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2288 if (cam->frame[b.index].vma_use_count)
2289 b.flags |= V4L2_BUF_FLAG_MAPPED;
2291 if (cam->frame[b.index].state == F_DONE)
2292 b.flags |= V4L2_BUF_FLAG_DONE;
2293 else if (cam->frame[b.index].state != F_UNUSED)
2294 b.flags |= V4L2_BUF_FLAG_QUEUED;
2296 if (copy_to_user(arg, &b, sizeof(b)))
2304 struct v4l2_buffer b;
2305 unsigned long lock_flags;
2307 if (copy_from_user(&b, arg, sizeof(b)))
2310 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2311 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2314 if (cam->frame[b.index].state != F_UNUSED)
2317 cam->frame[b.index].state = F_QUEUED;
2319 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2320 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2321 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2323 PDBGG("Frame #%lu queued", (unsigned long)b.index)
2330 struct v4l2_buffer b;
2331 struct sn9c102_frame_t *f;
2332 unsigned long lock_flags;
2335 if (copy_from_user(&b, arg, sizeof(b)))
2338 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2341 if (list_empty(&cam->outqueue)) {
2342 if (cam->stream == STREAM_OFF)
2344 if (filp->f_flags & O_NONBLOCK)
2346 err = wait_event_interruptible
2348 (!list_empty(&cam->outqueue)) ||
2349 (cam->state & DEV_DISCONNECTED) );
2352 if (cam->state & DEV_DISCONNECTED)
2356 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2357 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t,
2359 list_del(cam->outqueue.next);
2360 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2362 f->state = F_UNUSED;
2364 memcpy(&b, &f->buf, sizeof(b));
2365 if (f->vma_use_count)
2366 b.flags |= V4L2_BUF_FLAG_MAPPED;
2368 if (copy_to_user(arg, &b, sizeof(b)))
2371 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index)
2376 case VIDIOC_STREAMON:
2380 if (copy_from_user(&type, arg, sizeof(type)))
2383 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2386 if (list_empty(&cam->inqueue))
2389 cam->stream = STREAM_ON;
2396 case VIDIOC_STREAMOFF:
2400 if (copy_from_user(&type, arg, sizeof(type)))
2403 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2406 if (cam->stream == STREAM_ON)
2407 if ((err = sn9c102_stream_interrupt(cam)))
2410 sn9c102_empty_framequeues(cam);
2412 DBG(3, "Stream off")
2419 struct v4l2_streamparm sp;
2421 if (copy_from_user(&sp, arg, sizeof(sp)))
2424 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2427 sp.parm.capture.extendedmode = 0;
2428 sp.parm.capture.readbuffers = cam->nreadbuffers;
2430 if (copy_to_user(arg, &sp, sizeof(sp)))
2436 case VIDIOC_S_PARM_OLD:
2439 struct v4l2_streamparm sp;
2441 if (copy_from_user(&sp, arg, sizeof(sp)))
2444 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2447 sp.parm.capture.extendedmode = 0;
2449 if (sp.parm.capture.readbuffers == 0)
2450 sp.parm.capture.readbuffers = cam->nreadbuffers;
2452 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2453 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2455 if (copy_to_user(arg, &sp, sizeof(sp)))
2458 cam->nreadbuffers = sp.parm.capture.readbuffers;
2465 case VIDIOC_QUERYSTD:
2466 case VIDIOC_ENUMSTD:
2467 case VIDIOC_QUERYMENU:
2477 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2478 unsigned int cmd, unsigned long arg)
2480 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2483 if (down_interruptible(&cam->fileop_sem))
2484 return -ERESTARTSYS;
2486 if (cam->state & DEV_DISCONNECTED) {
2487 DBG(1, "Device not present")
2488 up(&cam->fileop_sem);
2492 if (cam->state & DEV_MISCONFIGURED) {
2493 DBG(1, "The camera is misconfigured. Close and open it again.")
2494 up(&cam->fileop_sem);
2498 err = sn9c102_v4l2_ioctl(inode, filp, cmd, (void __user *)arg);
2500 up(&cam->fileop_sem);
2506 static struct file_operations sn9c102_fops = {
2507 .owner = THIS_MODULE,
2508 .open = sn9c102_open,
2509 .release = sn9c102_release,
2510 .ioctl = sn9c102_ioctl,
2511 .read = sn9c102_read,
2512 .poll = sn9c102_poll,
2513 .mmap = sn9c102_mmap,
2514 .llseek = no_llseek,
2517 /*****************************************************************************/
2519 /* It exists a single interface only. We do not need to validate anything. */
2521 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2523 struct usb_device *udev = interface_to_usbdev(intf);
2524 struct sn9c102_device* cam;
2525 static unsigned int dev_nr = 0;
2529 n = sizeof(sn9c102_id_table)/sizeof(sn9c102_id_table[0]);
2530 for (i = 0; i < n-1; i++)
2531 if (le16_to_cpu(udev->descriptor.idVendor) ==
2532 sn9c102_id_table[i].idVendor &&
2533 le16_to_cpu(udev->descriptor.idProduct) ==
2534 sn9c102_id_table[i].idProduct)
2539 if (!(cam = kmalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2541 memset(cam, 0, sizeof(*cam));
2545 memcpy(&cam->dev, &udev->dev, sizeof(struct device));
2547 if (!(cam->control_buffer = kmalloc(8, GFP_KERNEL))) {
2548 DBG(1, "kmalloc() failed")
2552 memset(cam->control_buffer, 0, 8);
2554 if (!(cam->v4ldev = video_device_alloc())) {
2555 DBG(1, "video_device_alloc() failed")
2560 init_MUTEX(&cam->dev_sem);
2562 r = sn9c102_read_reg(cam, 0x00);
2563 if (r < 0 || r != 0x10) {
2564 DBG(1, "Sorry, this is not a SN9C10x based camera "
2565 "(vid/pid 0x%04X/0x%04X)",
2566 sn9c102_id_table[i].idVendor,sn9c102_id_table[i].idProduct)
2571 cam->bridge = (sn9c102_id_table[i].idProduct & 0xffc0) == 0x6080 ?
2572 BRIDGE_SN9C103 : BRIDGE_SN9C102;
2573 switch (cam->bridge) {
2574 case BRIDGE_SN9C101:
2575 case BRIDGE_SN9C102:
2576 DBG(2, "SN9C10[12] PC Camera Controller detected "
2577 "(vid/pid 0x%04X/0x%04X)", sn9c102_id_table[i].idVendor,
2578 sn9c102_id_table[i].idProduct)
2580 case BRIDGE_SN9C103:
2581 DBG(2, "SN9C103 PC Camera Controller detected "
2582 "(vid/pid 0x%04X/0x%04X)", sn9c102_id_table[i].idVendor,
2583 sn9c102_id_table[i].idProduct)
2587 for (i = 0; sn9c102_sensor_table[i]; i++) {
2588 err = sn9c102_sensor_table[i](cam);
2593 if (!err && cam->sensor) {
2594 DBG(2, "%s image sensor detected", cam->sensor->name)
2595 DBG(3, "Support for %s maintained by %s",
2596 cam->sensor->name, cam->sensor->maintainer)
2598 DBG(1, "No supported image sensor detected")
2603 if (sn9c102_init(cam)) {
2604 DBG(1, "Initialization failed. I will retry on open().")
2605 cam->state |= DEV_MISCONFIGURED;
2608 strcpy(cam->v4ldev->name, "SN9C10x PC Camera");
2609 cam->v4ldev->owner = THIS_MODULE;
2610 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2611 cam->v4ldev->hardware = VID_HARDWARE_SN9C102;
2612 cam->v4ldev->fops = &sn9c102_fops;
2613 cam->v4ldev->minor = video_nr[dev_nr];
2614 cam->v4ldev->release = video_device_release;
2615 video_set_drvdata(cam->v4ldev, cam);
2617 down(&cam->dev_sem);
2619 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2622 DBG(1, "V4L2 device registration failed")
2623 if (err == -ENFILE && video_nr[dev_nr] == -1)
2624 DBG(1, "Free /dev/videoX node not found")
2625 video_nr[dev_nr] = -1;
2626 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2631 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor)
2633 cam->module_param.force_munmap = force_munmap[dev_nr];
2635 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2637 sn9c102_create_sysfs(cam);
2638 DBG(2, "Optional device control through 'sysfs' interface ready")
2640 usb_set_intfdata(intf, cam);
2648 kfree(cam->control_buffer);
2650 video_device_release(cam->v4ldev);
2657 static void sn9c102_usb_disconnect(struct usb_interface* intf)
2659 struct sn9c102_device* cam = usb_get_intfdata(intf);
2664 down_write(&sn9c102_disconnect);
2666 down(&cam->dev_sem);
2668 DBG(2, "Disconnecting %s...", cam->v4ldev->name)
2670 wake_up_interruptible_all(&cam->open);
2673 DBG(2, "Device /dev/video%d is open! Deregistration and "
2674 "memory deallocation are deferred on close.",
2676 cam->state |= DEV_MISCONFIGURED;
2677 sn9c102_stop_transfer(cam);
2678 cam->state |= DEV_DISCONNECTED;
2679 wake_up_interruptible(&cam->wait_frame);
2680 wake_up_interruptible(&cam->wait_stream);
2682 cam->state |= DEV_DISCONNECTED;
2683 sn9c102_release_resources(cam);
2691 up_write(&sn9c102_disconnect);
2695 static struct usb_driver sn9c102_usb_driver = {
2696 .owner = THIS_MODULE,
2698 .id_table = sn9c102_id_table,
2699 .probe = sn9c102_usb_probe,
2700 .disconnect = sn9c102_usb_disconnect,
2703 /*****************************************************************************/
2705 static int __init sn9c102_module_init(void)
2709 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION)
2710 KDBG(3, SN9C102_MODULE_AUTHOR)
2712 if ((err = usb_register(&sn9c102_usb_driver)))
2713 KDBG(1, "usb_register() failed")
2719 static void __exit sn9c102_module_exit(void)
2721 usb_deregister(&sn9c102_usb_driver);
2725 module_init(sn9c102_module_init);
2726 module_exit(sn9c102_module_exit);