1 /***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers *
4 * Copyright (C) 2004 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>
41 #include <asm/uaccess.h>
45 /*****************************************************************************/
47 MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
49 MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
50 MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
51 MODULE_VERSION(SN9C102_MODULE_VERSION);
52 MODULE_LICENSE(SN9C102_MODULE_LICENSE);
54 static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
55 module_param_array(video_nr, short, NULL, 0444);
56 MODULE_PARM_DESC(video_nr,
57 "\n<-1|n[,...]> Specify V4L2 minor mode number."
58 "\n -1 = use next available (default)"
59 "\n n = use minor number n (integer >= 0)"
60 "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
63 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
64 "\nthe second camera and use auto for the first"
65 "\none and for every other camera."
69 static unsigned short debug = SN9C102_DEBUG_LEVEL;
70 module_param(debug, ushort, 0644);
71 MODULE_PARM_DESC(debug,
72 "\n<n> Debugging information level, from 0 to 3:"
73 "\n0 = none (use carefully)"
74 "\n1 = critical errors"
75 "\n2 = significant informations"
76 "\n3 = more verbose messages"
77 "\nLevel 3 is useful for testing only, when only "
79 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
83 /*****************************************************************************/
85 typedef char sn9c102_sof_header_t[12];
86 typedef char sn9c102_eof_header_t[4];
88 static sn9c102_sof_header_t sn9c102_sof_header[] = {
89 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x00},
90 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01},
94 static sn9c102_eof_header_t sn9c102_eof_header[] = {
95 {0x00, 0x00, 0x00, 0x00},
96 {0x40, 0x00, 0x00, 0x00},
97 {0x80, 0x00, 0x00, 0x00},
98 {0xc0, 0x00, 0x00, 0x00},
101 /*****************************************************************************/
103 static void* rvmalloc(size_t size)
108 size = PAGE_ALIGN(size);
110 mem = vmalloc_32((unsigned long)size);
114 memset(mem, 0, size);
116 adr = (unsigned long)mem;
118 SetPageReserved(vmalloc_to_page((void *)adr));
127 static void rvfree(void* mem, size_t size)
134 size = PAGE_ALIGN(size);
136 adr = (unsigned long)mem;
138 ClearPageReserved(vmalloc_to_page((void *)adr));
147 static u32 sn9c102_request_buffers(struct sn9c102_device* cam, u32 count)
149 struct v4l2_pix_format* p = &(cam->sensor->pix_format);
150 const size_t imagesize = (p->width * p->height * p->priv)/8;
154 if (count > SN9C102_MAX_FRAMES)
155 count = SN9C102_MAX_FRAMES;
157 cam->nbuffers = count;
158 while (cam->nbuffers > 0) {
159 if ((buff = rvmalloc(cam->nbuffers * PAGE_ALIGN(imagesize))))
164 for (i = 0; i < cam->nbuffers; i++) {
165 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
166 cam->frame[i].buf.index = i;
167 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
168 cam->frame[i].buf.length = imagesize;
169 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
170 cam->frame[i].buf.sequence = 0;
171 cam->frame[i].buf.field = V4L2_FIELD_NONE;
172 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
173 cam->frame[i].buf.flags = 0;
176 return cam->nbuffers;
180 static void sn9c102_release_buffers(struct sn9c102_device* cam)
183 rvfree(cam->frame[0].bufmem,
184 cam->nbuffers * cam->frame[0].buf.length);
190 static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
194 INIT_LIST_HEAD(&cam->inqueue);
195 INIT_LIST_HEAD(&cam->outqueue);
197 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
198 cam->frame[i].state = F_UNUSED;
199 cam->frame[i].buf.bytesused = 0;
204 static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
206 unsigned long lock_flags;
209 for (i = 0; i < cam->nbuffers; i++)
210 if (cam->frame[i].state == F_UNUSED) {
211 cam->frame[i].state = F_QUEUED;
212 spin_lock_irqsave(&cam->queue_lock, lock_flags);
213 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
214 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
218 /*****************************************************************************/
220 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
222 struct usb_device* udev = cam->usbdev;
223 u8* buff = cam->control_buffer;
228 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
229 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
231 DBG(3, "Failed to write a register (value 0x%02X, index "
232 "0x%02X, error %d)", value, index, res)
236 cam->reg[index] = value;
242 /* NOTE: reading some registers always returns 0 */
243 static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
245 struct usb_device* udev = cam->usbdev;
246 u8* buff = cam->control_buffer;
249 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
250 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
252 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
255 return (res >= 0) ? (int)(*buff) : -1;
259 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
264 return cam->reg[index];
269 sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
273 for (i = 1; i <= 5; i++) {
274 r = sn9c102_read_reg(cam, 0x08);
279 if (sensor->frequency & SN9C102_I2C_400KHZ)
289 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
290 struct sn9c102_sensor* sensor)
293 r = sn9c102_read_reg(cam, 0x08);
294 return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0;
299 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
300 struct sn9c102_sensor* sensor)
303 r = sn9c102_read_reg(cam, 0x08);
304 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
309 sn9c102_i2c_try_read(struct sn9c102_device* cam,
310 struct sn9c102_sensor* sensor, u8 address)
312 struct usb_device* udev = cam->usbdev;
313 u8* data = cam->control_buffer;
316 /* Write cycle - address */
317 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
318 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
319 data[1] = sensor->slave_write_id;
322 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
323 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
327 err += sn9c102_i2c_wait(cam, sensor);
329 /* Read cycle - 1 byte */
330 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
331 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
333 data[1] = sensor->slave_read_id;
335 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
336 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
340 err += sn9c102_i2c_wait(cam, sensor);
342 /* The read byte will be placed in data[4] */
343 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
344 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
348 err += sn9c102_i2c_detect_read_error(cam, sensor);
351 DBG(3, "I2C read failed for %s image sensor", sensor->name)
353 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[4])
355 return err ? -1 : (int)data[4];
360 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
361 struct sn9c102_sensor* sensor, u8 n, u8 data0,
362 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
364 struct usb_device* udev = cam->usbdev;
365 u8* data = cam->control_buffer;
368 /* Write cycle. It usually is address + value */
369 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
370 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
379 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
380 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
384 err += sn9c102_i2c_wait(cam, sensor);
385 err += sn9c102_i2c_detect_write_error(cam, sensor);
388 DBG(3, "I2C write failed for %s image sensor", sensor->name)
390 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
391 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
392 n, data0, data1, data2, data3, data4, data5)
399 sn9c102_i2c_try_write(struct sn9c102_device* cam,
400 struct sn9c102_sensor* sensor, u8 address, u8 value)
402 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
403 sensor->slave_write_id, address,
408 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
413 return sn9c102_i2c_try_read(cam, cam->sensor, address);
417 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
422 return sn9c102_i2c_try_write(cam, cam->sensor, address, value);
425 /*****************************************************************************/
428 sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
430 size_t soflen = sizeof(sn9c102_sof_header_t), i;
431 u8 j, n = sizeof(sn9c102_sof_header) / soflen;
433 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
434 for (j = 0; j < n; j++)
435 /* It's enough to compare 7 bytes */
436 if (!memcmp(mem + i, sn9c102_sof_header[j], 7))
437 /* Skips the header */
438 return mem + i + soflen;
445 sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
447 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
448 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
450 if (cam->sensor->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
451 return NULL; /* EOF header does not exist in compressed data */
453 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
454 for (j = 0; j < n; j++)
455 if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen))
462 static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
464 struct sn9c102_device* cam = urb->context;
465 struct sn9c102_frame_t** f;
466 unsigned long lock_flags;
470 if (urb->status == -ENOENT)
473 f = &cam->frame_current;
475 if (cam->stream == STREAM_INTERRUPT) {
476 cam->stream = STREAM_OFF;
478 (*f)->state = F_QUEUED;
479 DBG(3, "Stream interrupted")
480 wake_up_interruptible(&cam->wait_stream);
483 if ((cam->state & DEV_DISCONNECTED)||(cam->state & DEV_MISCONFIGURED))
486 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
490 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
493 for (i = 0; i < urb->number_of_packets; i++) {
494 unsigned int img, len, status;
495 void *pos, *sof, *eof;
497 len = urb->iso_frame_desc[i].actual_length;
498 status = urb->iso_frame_desc[i].status;
499 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
502 DBG(3, "Error in isochronous frame")
503 (*f)->state = F_ERROR;
507 PDBGG("Isochrnous frame: length %u, #%u i", len, i)
510 NOTE: It is probably correct to assume that SOF and EOF
511 headers do not occur between two consecutive packets,
512 but who knows..Whatever is the truth, this assumption
513 doesn't introduce bugs.
517 sof = sn9c102_find_sof_header(cam, pos, len);
519 eof = sn9c102_find_eof_header(cam, pos, len);
520 if ((*f)->state == F_GRABBING) {
525 img = (eof > pos) ? eof - pos - 1 : 0;
527 if ((*f)->buf.bytesused+img>(*f)->buf.length) {
528 u32 b = (*f)->buf.bytesused + img -
530 img = (*f)->buf.length -
532 DBG(3, "Expected EOF not found: "
535 DBG(3, "Exceeded limit: +%u "
536 "bytes", (unsigned)(b))
539 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
542 if ((*f)->buf.bytesused == 0)
543 do_gettimeofday(&(*f)->buf.timestamp);
545 (*f)->buf.bytesused += img;
547 if ((*f)->buf.bytesused == (*f)->buf.length ||
548 (cam->sensor->pix_format.pixelformat ==
549 V4L2_PIX_FMT_SN9C10X && eof)) {
550 u32 b = (*f)->buf.bytesused;
551 (*f)->state = F_DONE;
552 (*f)->buf.sequence= ++cam->frame_count;
553 spin_lock_irqsave(&cam->queue_lock,
555 list_move_tail(&(*f)->frame,
557 if (!list_empty(&cam->inqueue))
560 struct sn9c102_frame_t,
564 spin_unlock_irqrestore(&cam->queue_lock
566 DBG(3, "Video frame captured: "
567 "%lu bytes", (unsigned long)(b))
573 (*f)->state = F_ERROR;
574 DBG(3, "Not expected EOF after %lu "
575 "bytes of image data",
576 (unsigned long)((*f)->buf.bytesused))
583 DBG(3, "EOF without SOF")
587 PDBGG("Ignoring pointless isochronous frame")
591 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
593 (*f)->state = F_GRABBING;
594 (*f)->buf.bytesused = 0;
597 DBG(3, "SOF detected: new video frame")
601 } else if ((*f)->state == F_GRABBING) {
602 eof = sn9c102_find_eof_header(cam, pos, len);
603 if (eof && eof < sof)
604 goto end_of_frame; /* (1) */
606 if (cam->sensor->pix_format.pixelformat ==
607 V4L2_PIX_FMT_SN9C10X) {
608 eof = sof-sizeof(sn9c102_sof_header_t);
611 DBG(3, "SOF before expected EOF after "
612 "%lu bytes of image data",
613 (unsigned long)((*f)->buf.bytesused))
621 urb->dev = cam->usbdev;
622 err = usb_submit_urb(urb, GFP_ATOMIC);
623 if (err < 0 && err != -EPERM) {
624 cam->state |= DEV_MISCONFIGURED;
625 DBG(1, "usb_submit_urb() failed")
628 wake_up_interruptible(&cam->wait_frame);
632 static int sn9c102_start_transfer(struct sn9c102_device* cam)
634 struct usb_device *udev = cam->usbdev;
636 const unsigned int wMaxPacketSize[] = {0, 128, 256, 384, 512,
637 680, 800, 900, 1023};
638 const unsigned int psz = wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
642 for (i = 0; i < SN9C102_URBS; i++) {
643 cam->transfer_buffer[i] = kmalloc(SN9C102_ISO_PACKETS * psz,
645 if (!cam->transfer_buffer[i]) {
647 DBG(1, "Not enough memory")
652 for (i = 0; i < SN9C102_URBS; i++) {
653 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
657 DBG(1, "usb_alloc_urb() failed")
662 urb->pipe = usb_rcvisocpipe(udev, 1);
663 urb->transfer_flags = URB_ISO_ASAP;
664 urb->number_of_packets = SN9C102_ISO_PACKETS;
665 urb->complete = sn9c102_urb_complete;
666 urb->transfer_buffer = cam->transfer_buffer[i];
667 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
669 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
670 urb->iso_frame_desc[j].offset = psz * j;
671 urb->iso_frame_desc[j].length = psz;
676 if (!(cam->reg[0x01] & 0x04)) {
677 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
680 DBG(1, "I/O hardware error")
685 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
687 DBG(1, "usb_set_interface() failed")
691 cam->frame_current = NULL;
693 for (i = 0; i < SN9C102_URBS; i++) {
694 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
696 for (j = i-1; j >= 0; j--)
697 usb_kill_urb(cam->urb[j]);
698 DBG(1, "usb_submit_urb() failed, error %d", err)
706 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
707 usb_free_urb(cam->urb[i]);
710 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
711 kfree(cam->transfer_buffer[i]);
717 static int sn9c102_stop_transfer(struct sn9c102_device* cam)
719 struct usb_device *udev = cam->usbdev;
723 if (cam->state & DEV_DISCONNECTED)
726 for (i = SN9C102_URBS-1; i >= 0; i--) {
727 usb_kill_urb(cam->urb[i]);
728 usb_free_urb(cam->urb[i]);
729 kfree(cam->transfer_buffer[i]);
732 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
734 DBG(3, "usb_set_interface() failed")
740 int sn9c102_stream_interrupt(struct sn9c102_device* cam)
744 cam->stream = STREAM_INTERRUPT;
745 err = wait_event_timeout(cam->wait_stream,
746 (cam->stream == STREAM_OFF) ||
747 (cam->state & DEV_DISCONNECTED),
748 SN9C102_URB_TIMEOUT);
750 cam->state |= DEV_MISCONFIGURED;
751 DBG(1, "The camera is misconfigured. To use "
752 "it, close and open /dev/video%d "
753 "again.", cam->v4ldev->minor)
756 if (cam->state & DEV_DISCONNECTED)
762 /*****************************************************************************/
764 static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
771 strncpy(str, buff, len);
774 strncpy(str, buff, 4);
778 val = simple_strtoul(str, &endp, 0);
782 *count = (ssize_t)(endp - str);
783 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
790 NOTE 1: being inside one of the following methods implies that the v4l
791 device exists for sure (see kobjects and reference counters)
792 NOTE 2: buffers are PAGE_SIZE long
795 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
797 struct sn9c102_device* cam;
800 if (down_interruptible(&sn9c102_sysfs_lock))
803 cam = video_get_drvdata(to_video_device(cd));
805 up(&sn9c102_sysfs_lock);
809 count = sprintf(buf, "%u\n", cam->sysfs.reg);
811 up(&sn9c102_sysfs_lock);
818 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
820 struct sn9c102_device* cam;
824 if (down_interruptible(&sn9c102_sysfs_lock))
827 cam = video_get_drvdata(to_video_device(cd));
829 up(&sn9c102_sysfs_lock);
833 index = sn9c102_strtou8(buf, len, &count);
834 if (index > 0x1f || !count) {
835 up(&sn9c102_sysfs_lock);
839 cam->sysfs.reg = index;
841 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg)
842 DBG(3, "Written bytes: %zd", count)
844 up(&sn9c102_sysfs_lock);
850 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
852 struct sn9c102_device* cam;
856 if (down_interruptible(&sn9c102_sysfs_lock))
859 cam = video_get_drvdata(to_video_device(cd));
861 up(&sn9c102_sysfs_lock);
865 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
866 up(&sn9c102_sysfs_lock);
870 count = sprintf(buf, "%d\n", val);
872 DBG(3, "Read bytes: %zd", count)
874 up(&sn9c102_sysfs_lock);
881 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
883 struct sn9c102_device* cam;
888 if (down_interruptible(&sn9c102_sysfs_lock))
891 cam = video_get_drvdata(to_video_device(cd));
893 up(&sn9c102_sysfs_lock);
897 value = sn9c102_strtou8(buf, len, &count);
899 up(&sn9c102_sysfs_lock);
903 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
905 up(&sn9c102_sysfs_lock);
909 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
910 cam->sysfs.reg, value)
911 DBG(3, "Written bytes: %zd", count)
913 up(&sn9c102_sysfs_lock);
919 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
921 struct sn9c102_device* cam;
924 if (down_interruptible(&sn9c102_sysfs_lock))
927 cam = video_get_drvdata(to_video_device(cd));
929 up(&sn9c102_sysfs_lock);
933 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
935 DBG(3, "Read bytes: %zd", count)
937 up(&sn9c102_sysfs_lock);
944 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
946 struct sn9c102_device* cam;
950 if (down_interruptible(&sn9c102_sysfs_lock))
953 cam = video_get_drvdata(to_video_device(cd));
955 up(&sn9c102_sysfs_lock);
959 index = sn9c102_strtou8(buf, len, &count);
961 up(&sn9c102_sysfs_lock);
965 cam->sysfs.i2c_reg = index;
967 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg)
968 DBG(3, "Written bytes: %zd", count)
970 up(&sn9c102_sysfs_lock);
976 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
978 struct sn9c102_device* cam;
982 if (down_interruptible(&sn9c102_sysfs_lock))
985 cam = video_get_drvdata(to_video_device(cd));
987 up(&sn9c102_sysfs_lock);
991 if (cam->sensor->slave_read_id == SN9C102_I2C_SLAVEID_UNAVAILABLE) {
992 up(&sn9c102_sysfs_lock);
996 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
997 up(&sn9c102_sysfs_lock);
1001 count = sprintf(buf, "%d\n", val);
1003 DBG(3, "Read bytes: %zd", count)
1005 up(&sn9c102_sysfs_lock);
1012 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1014 struct sn9c102_device* cam;
1019 if (down_interruptible(&sn9c102_sysfs_lock))
1020 return -ERESTARTSYS;
1022 cam = video_get_drvdata(to_video_device(cd));
1024 up(&sn9c102_sysfs_lock);
1028 value = sn9c102_strtou8(buf, len, &count);
1030 up(&sn9c102_sysfs_lock);
1034 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1036 up(&sn9c102_sysfs_lock);
1040 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1041 cam->sysfs.i2c_reg, value)
1042 DBG(3, "Written bytes: %zd", count)
1044 up(&sn9c102_sysfs_lock);
1051 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1053 struct sn9c102_device* cam;
1054 enum sn9c102_bridge bridge;
1059 if (down_interruptible(&sn9c102_sysfs_lock))
1060 return -ERESTARTSYS;
1062 cam = video_get_drvdata(to_video_device(cd));
1064 up(&sn9c102_sysfs_lock);
1068 bridge = cam->bridge;
1070 up(&sn9c102_sysfs_lock);
1072 value = sn9c102_strtou8(buf, len, &count);
1077 case BRIDGE_SN9C101:
1078 case BRIDGE_SN9C102:
1081 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1082 res = sn9c102_store_val(cd, buf, len);
1084 case BRIDGE_SN9C103:
1087 if ((res = sn9c102_store_reg(cd, "0x04", 4)) >= 0)
1088 res = sn9c102_store_val(cd, buf, len);
1097 sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1103 value = sn9c102_strtou8(buf, len, &count);
1104 if (!count || value > 0x7f)
1107 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1108 res = sn9c102_store_val(cd, buf, len);
1115 sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1121 value = sn9c102_strtou8(buf, len, &count);
1122 if (!count || value > 0x7f)
1125 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1126 res = sn9c102_store_val(cd, buf, len);
1132 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1133 sn9c102_show_reg, sn9c102_store_reg);
1134 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1135 sn9c102_show_val, sn9c102_store_val);
1136 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1137 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1138 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1139 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1140 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1141 static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1142 static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1145 static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1147 struct video_device *v4ldev = cam->v4ldev;
1149 video_device_create_file(v4ldev, &class_device_attr_reg);
1150 video_device_create_file(v4ldev, &class_device_attr_val);
1151 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
1152 video_device_create_file(v4ldev, &class_device_attr_green);
1153 else if (cam->bridge == BRIDGE_SN9C103) {
1154 video_device_create_file(v4ldev, &class_device_attr_blue);
1155 video_device_create_file(v4ldev, &class_device_attr_red);
1157 if (cam->sensor->slave_write_id != SN9C102_I2C_SLAVEID_UNAVAILABLE ||
1158 cam->sensor->slave_read_id != SN9C102_I2C_SLAVEID_UNAVAILABLE) {
1159 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1160 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1164 /*****************************************************************************/
1167 sn9c102_set_format(struct sn9c102_device* cam, struct v4l2_pix_format* fmt)
1171 if (fmt->pixelformat == V4L2_PIX_FMT_SN9C10X)
1172 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, 0x18);
1174 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, 0x18);
1176 return err ? -EIO : 0;
1181 sn9c102_set_compression(struct sn9c102_device* cam,
1182 struct v4l2_jpegcompression* compression)
1186 if (compression->quality == 0)
1187 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, 0x17);
1188 else if (compression->quality == 1)
1189 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, 0x17);
1191 return err ? -EIO : 0;
1195 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1201 r = cam->reg[0x18] & 0xcf;
1202 else if (scale == 2) {
1203 r = cam->reg[0x18] & 0xcf;
1205 } else if (scale == 4)
1206 r = cam->reg[0x18] | 0x20;
1208 err += sn9c102_write_reg(cam, r, 0x18);
1212 PDBGG("Scaling factor: %u", scale)
1218 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1220 struct sn9c102_sensor* s = cam->sensor;
1221 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1222 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1223 h_size = (u8)(rect->width / 16),
1224 v_size = (u8)(rect->height / 16);
1227 err += sn9c102_write_reg(cam, h_start, 0x12);
1228 err += sn9c102_write_reg(cam, v_start, 0x13);
1229 err += sn9c102_write_reg(cam, h_size, 0x15);
1230 err += sn9c102_write_reg(cam, v_size, 0x16);
1234 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1235 "%u %u %u %u", h_start, v_start, h_size, v_size)
1241 static int sn9c102_init(struct sn9c102_device* cam)
1243 struct sn9c102_sensor* s = cam->sensor;
1244 struct v4l2_control ctrl;
1245 struct v4l2_queryctrl *qctrl;
1246 struct v4l2_rect* rect;
1250 if (!(cam->state & DEV_INITIALIZED)) {
1251 init_waitqueue_head(&cam->open);
1253 rect = &(s->cropcap.defrect);
1254 } else { /* use current values */
1259 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1260 err += sn9c102_set_crop(cam, rect);
1267 DBG(3, "Sensor initialization failed")
1272 if (!(cam->state & DEV_INITIALIZED))
1273 cam->compression.quality = cam->reg[0x17] & 0x01 ? 0 : 1;
1275 err += sn9c102_set_compression(cam, &cam->compression);
1276 err += sn9c102_set_format(cam, &s->pix_format);
1280 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1281 DBG(3, "Compressed video format is active, quality %d",
1282 cam->compression.quality)
1284 DBG(3, "Uncompressed video format is active")
1287 if ((err = s->set_crop(cam, rect))) {
1288 DBG(3, "set_crop() failed")
1293 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1294 for (i = 0; i < n; i++)
1295 if (s->qctrl[i].id != 0 &&
1296 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1297 ctrl.id = s->qctrl[i].id;
1298 ctrl.value = qctrl[i].default_value;
1299 err = s->set_ctrl(cam, &ctrl);
1301 DBG(3, "Set %s control failed",
1305 DBG(3, "Image sensor supports '%s' control",
1310 if (!(cam->state & DEV_INITIALIZED)) {
1311 init_MUTEX(&cam->fileop_sem);
1312 spin_lock_init(&cam->queue_lock);
1313 init_waitqueue_head(&cam->wait_frame);
1314 init_waitqueue_head(&cam->wait_stream);
1315 cam->nreadbuffers = 2;
1316 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1317 memcpy(&(s->_rect), &(s->cropcap.defrect),
1318 sizeof(struct v4l2_rect));
1319 cam->state |= DEV_INITIALIZED;
1322 DBG(2, "Initialization succeeded")
1327 static void sn9c102_release_resources(struct sn9c102_device* cam)
1329 down(&sn9c102_sysfs_lock);
1331 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor)
1332 video_set_drvdata(cam->v4ldev, NULL);
1333 video_unregister_device(cam->v4ldev);
1335 up(&sn9c102_sysfs_lock);
1337 kfree(cam->control_buffer);
1340 /*****************************************************************************/
1342 static int sn9c102_open(struct inode* inode, struct file* filp)
1344 struct sn9c102_device* cam;
1348 This is the only safe way to prevent race conditions with
1351 if (!down_read_trylock(&sn9c102_disconnect))
1352 return -ERESTARTSYS;
1354 cam = video_get_drvdata(video_devdata(filp));
1356 if (down_interruptible(&cam->dev_sem)) {
1357 up_read(&sn9c102_disconnect);
1358 return -ERESTARTSYS;
1362 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor)
1363 if ((filp->f_flags & O_NONBLOCK) ||
1364 (filp->f_flags & O_NDELAY)) {
1369 err = wait_event_interruptible_exclusive(cam->open,
1370 cam->state & DEV_DISCONNECTED
1373 up_read(&sn9c102_disconnect);
1376 if (cam->state & DEV_DISCONNECTED) {
1377 up_read(&sn9c102_disconnect);
1380 down(&cam->dev_sem);
1384 if (cam->state & DEV_MISCONFIGURED) {
1385 err = sn9c102_init(cam);
1387 DBG(1, "Initialization failed again. "
1388 "I will retry on next open().")
1391 cam->state &= ~DEV_MISCONFIGURED;
1394 if ((err = sn9c102_start_transfer(cam)))
1397 filp->private_data = cam;
1400 cam->stream = STREAM_OFF;
1402 cam->frame_count = 0;
1403 sn9c102_empty_framequeues(cam);
1405 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor)
1409 up_read(&sn9c102_disconnect);
1414 static int sn9c102_release(struct inode* inode, struct file* filp)
1416 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1418 down(&cam->dev_sem); /* prevent disconnect() to be called */
1420 sn9c102_stop_transfer(cam);
1422 sn9c102_release_buffers(cam);
1424 if (cam->state & DEV_DISCONNECTED) {
1425 sn9c102_release_resources(cam);
1432 wake_up_interruptible_nr(&cam->open, 1);
1434 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor)
1443 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1445 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1446 struct sn9c102_frame_t* f, * i;
1447 unsigned long lock_flags;
1450 if (down_interruptible(&cam->fileop_sem))
1451 return -ERESTARTSYS;
1453 if (cam->state & DEV_DISCONNECTED) {
1454 DBG(1, "Device not present")
1455 up(&cam->fileop_sem);
1459 if (cam->state & DEV_MISCONFIGURED) {
1460 DBG(1, "The camera is misconfigured. Close and open it again.")
1461 up(&cam->fileop_sem);
1465 if (cam->io == IO_MMAP) {
1466 DBG(3, "Close and open the device again to choose "
1468 up(&cam->fileop_sem);
1472 if (cam->io == IO_NONE) {
1473 if (!sn9c102_request_buffers(cam, cam->nreadbuffers)) {
1474 DBG(1, "read() failed, not enough memory")
1475 up(&cam->fileop_sem);
1479 cam->stream = STREAM_ON;
1480 sn9c102_queue_unusedframes(cam);
1484 up(&cam->fileop_sem);
1488 if (list_empty(&cam->outqueue)) {
1489 if (filp->f_flags & O_NONBLOCK) {
1490 up(&cam->fileop_sem);
1493 err = wait_event_interruptible
1495 (!list_empty(&cam->outqueue)) ||
1496 (cam->state & DEV_DISCONNECTED) );
1498 up(&cam->fileop_sem);
1501 if (cam->state & DEV_DISCONNECTED) {
1502 up(&cam->fileop_sem);
1507 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1509 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1510 list_for_each_entry(i, &cam->outqueue, frame)
1511 i->state = F_UNUSED;
1512 INIT_LIST_HEAD(&cam->outqueue);
1513 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1515 sn9c102_queue_unusedframes(cam);
1517 if (count > f->buf.bytesused)
1518 count = f->buf.bytesused;
1520 if (copy_to_user(buf, f->bufmem, count)) {
1521 up(&cam->fileop_sem);
1526 PDBGG("Frame #%lu, bytes read: %zu", (unsigned long)f->buf.index,count)
1528 up(&cam->fileop_sem);
1534 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1536 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1537 unsigned int mask = 0;
1539 if (down_interruptible(&cam->fileop_sem))
1542 if (cam->state & DEV_DISCONNECTED) {
1543 DBG(1, "Device not present")
1547 if (cam->state & DEV_MISCONFIGURED) {
1548 DBG(1, "The camera is misconfigured. Close and open it again.")
1552 if (cam->io == IO_NONE) {
1553 if (!sn9c102_request_buffers(cam, 2)) {
1554 DBG(1, "poll() failed, not enough memory")
1558 cam->stream = STREAM_ON;
1561 if (cam->io == IO_READ)
1562 sn9c102_queue_unusedframes(cam);
1564 poll_wait(filp, &cam->wait_frame, wait);
1566 if (!list_empty(&cam->outqueue))
1567 mask |= POLLIN | POLLRDNORM;
1569 up(&cam->fileop_sem);
1574 up(&cam->fileop_sem);
1579 static void sn9c102_vm_open(struct vm_area_struct* vma)
1581 struct sn9c102_frame_t* f = vma->vm_private_data;
1586 static void sn9c102_vm_close(struct vm_area_struct* vma)
1588 /* NOTE: buffers are not freed here */
1589 struct sn9c102_frame_t* f = vma->vm_private_data;
1594 static struct vm_operations_struct sn9c102_vm_ops = {
1595 .open = sn9c102_vm_open,
1596 .close = sn9c102_vm_close,
1600 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1602 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1603 unsigned long size = vma->vm_end - vma->vm_start,
1604 start = vma->vm_start,
1609 if (down_interruptible(&cam->fileop_sem))
1610 return -ERESTARTSYS;
1612 if (cam->state & DEV_DISCONNECTED) {
1613 DBG(1, "Device not present")
1614 up(&cam->fileop_sem);
1618 if (cam->state & DEV_MISCONFIGURED) {
1619 DBG(1, "The camera is misconfigured. Close and open it again.")
1620 up(&cam->fileop_sem);
1624 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1625 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1626 up(&cam->fileop_sem);
1630 for (i = 0; i < cam->nbuffers; i++) {
1631 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1634 if (i == cam->nbuffers) {
1635 up(&cam->fileop_sem);
1639 /* VM_IO is eventually going to replace PageReserved altogether */
1640 vma->vm_flags |= VM_IO;
1641 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1643 pos = (unsigned long)cam->frame[i].bufmem;
1644 while (size > 0) { /* size is page-aligned */
1645 page = vmalloc_to_pfn((void *)pos);
1646 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
1647 vma->vm_page_prot)) {
1648 up(&cam->fileop_sem);
1656 vma->vm_ops = &sn9c102_vm_ops;
1657 vma->vm_private_data = &cam->frame[i];
1659 sn9c102_vm_open(vma);
1661 up(&cam->fileop_sem);
1667 static int sn9c102_v4l2_ioctl(struct inode* inode, struct file* filp,
1668 unsigned int cmd, void __user * arg)
1670 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1674 case VIDIOC_QUERYCAP:
1676 struct v4l2_capability cap = {
1677 .driver = "sn9c102",
1678 .version = SN9C102_MODULE_VERSION_CODE,
1679 .capabilities = V4L2_CAP_VIDEO_CAPTURE |
1680 V4L2_CAP_READWRITE |
1684 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1685 if (usb_make_path(cam->usbdev, cap.bus_info,
1686 sizeof(cap.bus_info)) < 0)
1687 strlcpy(cap.bus_info, cam->dev.bus_id,
1688 sizeof(cap.bus_info));
1690 if (copy_to_user(arg, &cap, sizeof(cap)))
1696 case VIDIOC_ENUMINPUT:
1698 struct v4l2_input i;
1700 if (copy_from_user(&i, arg, sizeof(i)))
1706 memset(&i, 0, sizeof(i));
1707 strcpy(i.name, "USB");
1709 if (copy_to_user(arg, &i, sizeof(i)))
1715 case VIDIOC_G_INPUT:
1716 case VIDIOC_S_INPUT:
1720 if (copy_from_user(&index, arg, sizeof(index)))
1729 case VIDIOC_QUERYCTRL:
1731 struct sn9c102_sensor* s = cam->sensor;
1732 struct v4l2_queryctrl qc;
1735 if (copy_from_user(&qc, arg, sizeof(qc)))
1738 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1739 for (i = 0; i < n; i++)
1740 if (qc.id && qc.id == s->qctrl[i].id) {
1741 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1742 if (copy_to_user(arg, &qc, sizeof(qc)))
1752 struct sn9c102_sensor* s = cam->sensor;
1753 struct v4l2_control ctrl;
1759 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1762 err = s->get_ctrl(cam, &ctrl);
1764 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1772 struct sn9c102_sensor* s = cam->sensor;
1773 struct v4l2_control ctrl;
1780 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1783 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1784 for (i = 0; i < n; i++)
1785 if (ctrl.id == s->qctrl[i].id) {
1786 if (ctrl.value < s->qctrl[i].minimum ||
1787 ctrl.value > s->qctrl[i].maximum)
1789 ctrl.value -= ctrl.value % s->qctrl[i].step;
1793 if ((err = s->set_ctrl(cam, &ctrl)))
1796 s->_qctrl[i].default_value = ctrl.value;
1798 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
1799 (unsigned long)ctrl.id, (unsigned long)ctrl.value)
1804 case VIDIOC_CROPCAP:
1806 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1808 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1809 cc->pixelaspect.numerator = 1;
1810 cc->pixelaspect.denominator = 1;
1812 if (copy_to_user(arg, cc, sizeof(*cc)))
1820 struct sn9c102_sensor* s = cam->sensor;
1821 struct v4l2_crop crop = {
1822 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1825 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1827 if (copy_to_user(arg, &crop, sizeof(crop)))
1835 struct sn9c102_sensor* s = cam->sensor;
1836 struct v4l2_crop crop;
1837 struct v4l2_rect* rect;
1838 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1839 struct v4l2_pix_format* pix_format = &(s->pix_format);
1841 const enum sn9c102_stream_state stream = cam->stream;
1842 const u32 nbuffers = cam->nbuffers;
1846 if (copy_from_user(&crop, arg, sizeof(crop)))
1851 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1854 for (i = 0; i < cam->nbuffers; i++)
1855 if (cam->frame[i].vma_use_count) {
1856 DBG(3, "VIDIOC_S_CROP failed. "
1857 "Unmap the buffers first.")
1861 /* Preserve R,G or B origin */
1862 rect->left = (s->_rect.left & 1L) ?
1863 rect->left | 1L : rect->left & ~1L;
1864 rect->top = (s->_rect.top & 1L) ?
1865 rect->top | 1L : rect->top & ~1L;
1867 if (rect->width < 16)
1869 if (rect->height < 16)
1871 if (rect->width > bounds->width)
1872 rect->width = bounds->width;
1873 if (rect->height > bounds->height)
1874 rect->height = bounds->height;
1875 if (rect->left < bounds->left)
1876 rect->left = bounds->left;
1877 if (rect->top < bounds->top)
1878 rect->top = bounds->top;
1879 if (rect->left + rect->width > bounds->left + bounds->width)
1880 rect->left = bounds->left+bounds->width - rect->width;
1881 if (rect->top + rect->height > bounds->top + bounds->height)
1882 rect->top = bounds->top+bounds->height - rect->height;
1884 rect->width &= ~15L;
1885 rect->height &= ~15L;
1887 if (SN9C102_PRESERVE_IMGSCALE) {
1888 /* Calculate the actual scaling factor */
1890 a = rect->width * rect->height;
1891 b = pix_format->width * pix_format->height;
1892 scale = b ? (u8)((a / b) < 4 ? 1 :
1893 ((a / b) < 16 ? 2 : 4)) : 1;
1897 if (cam->stream == STREAM_ON)
1898 if ((err = sn9c102_stream_interrupt(cam)))
1901 if (copy_to_user(arg, &crop, sizeof(crop))) {
1902 cam->stream = stream;
1906 sn9c102_release_buffers(cam);
1908 err = sn9c102_set_crop(cam, rect);
1910 err += s->set_crop(cam, rect);
1911 err += sn9c102_set_scale(cam, scale);
1913 if (err) { /* atomic, no rollback in ioctl() */
1914 cam->state |= DEV_MISCONFIGURED;
1915 DBG(1, "VIDIOC_S_CROP failed because of hardware "
1916 "problems. To use the camera, close and open "
1917 "/dev/video%d again.", cam->v4ldev->minor)
1921 s->pix_format.width = rect->width/scale;
1922 s->pix_format.height = rect->height/scale;
1923 memcpy(&(s->_rect), rect, sizeof(*rect));
1925 if (nbuffers != sn9c102_request_buffers(cam, nbuffers)) {
1926 cam->state |= DEV_MISCONFIGURED;
1927 DBG(1, "VIDIOC_S_CROP failed because of not enough "
1928 "memory. To use the camera, close and open "
1929 "/dev/video%d again.", cam->v4ldev->minor)
1933 cam->stream = stream;
1938 case VIDIOC_ENUM_FMT:
1940 struct v4l2_fmtdesc fmtd;
1942 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1945 if (fmtd.index == 0) {
1946 strcpy(fmtd.description, "bayer rgb");
1947 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
1948 } else if (fmtd.index == 1) {
1949 strcpy(fmtd.description, "compressed");
1950 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
1951 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1955 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1956 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1958 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1966 struct v4l2_format format;
1967 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
1969 if (copy_from_user(&format, arg, sizeof(format)))
1972 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1975 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X)
1976 ? 0 : (pfmt->width * pfmt->priv) / 8;
1977 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1978 pfmt->field = V4L2_FIELD_NONE;
1979 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1981 if (copy_to_user(arg, &format, sizeof(format)))
1987 case VIDIOC_TRY_FMT:
1990 struct sn9c102_sensor* s = cam->sensor;
1991 struct v4l2_format format;
1992 struct v4l2_pix_format* pix;
1993 struct v4l2_pix_format* pfmt = &(s->pix_format);
1994 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1995 struct v4l2_rect rect;
1997 const enum sn9c102_stream_state stream = cam->stream;
1998 const u32 nbuffers = cam->nbuffers;
2002 if (copy_from_user(&format, arg, sizeof(format)))
2005 pix = &(format.fmt.pix);
2007 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2010 memcpy(&rect, &(s->_rect), sizeof(rect));
2012 { /* calculate the actual scaling factor */
2014 a = rect.width * rect.height;
2015 b = pix->width * pix->height;
2016 scale = b ? (u8)((a / b) < 4 ? 1 :
2017 ((a / b) < 16 ? 2 : 4)) : 1;
2020 rect.width = scale * pix->width;
2021 rect.height = scale * pix->height;
2023 if (rect.width < 16)
2025 if (rect.height < 16)
2027 if (rect.width > bounds->left + bounds->width - rect.left)
2028 rect.width = bounds->left + bounds->width - rect.left;
2029 if (rect.height > bounds->top + bounds->height - rect.top)
2030 rect.height = bounds->top + bounds->height - rect.top;
2033 rect.height &= ~15L;
2035 { /* adjust the scaling factor */
2037 a = rect.width * rect.height;
2038 b = pix->width * pix->height;
2039 scale = b ? (u8)((a / b) < 4 ? 1 :
2040 ((a / b) < 16 ? 2 : 4)) : 1;
2043 pix->width = rect.width / scale;
2044 pix->height = rect.height / scale;
2046 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2047 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2048 pix->pixelformat = pfmt->pixelformat;
2049 pix->priv = pfmt->priv; /* bpp */
2050 pix->colorspace = pfmt->colorspace;
2051 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
2052 ? 0 : (pix->width * pix->priv) / 8;
2053 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2054 pix->field = V4L2_FIELD_NONE;
2056 if (cmd == VIDIOC_TRY_FMT) {
2057 if (copy_to_user(arg, &format, sizeof(format)))
2062 for (i = 0; i < cam->nbuffers; i++)
2063 if (cam->frame[i].vma_use_count) {
2064 DBG(3, "VIDIOC_S_FMT failed. "
2065 "Unmap the buffers first.")
2069 if (cam->stream == STREAM_ON)
2070 if ((err = sn9c102_stream_interrupt(cam)))
2073 if (copy_to_user(arg, &format, sizeof(format))) {
2074 cam->stream = stream;
2078 sn9c102_release_buffers(cam);
2080 err += sn9c102_set_format(cam, pix);
2081 err += sn9c102_set_crop(cam, &rect);
2083 err += s->set_crop(cam, &rect);
2084 err += sn9c102_set_scale(cam, scale);
2086 if (err) { /* atomic, no rollback in ioctl() */
2087 cam->state |= DEV_MISCONFIGURED;
2088 DBG(1, "VIDIOC_S_FMT failed because of hardware "
2089 "problems. To use the camera, close and open "
2090 "/dev/video%d again.", cam->v4ldev->minor)
2094 memcpy(pfmt, pix, sizeof(*pix));
2095 memcpy(&(s->_rect), &rect, sizeof(rect));
2097 if (nbuffers != sn9c102_request_buffers(cam, nbuffers)) {
2098 cam->state |= DEV_MISCONFIGURED;
2099 DBG(1, "VIDIOC_S_FMT failed because of not enough "
2100 "memory. To use the camera, close and open "
2101 "/dev/video%d again.", cam->v4ldev->minor)
2105 cam->stream = stream;
2110 case VIDIOC_G_JPEGCOMP:
2112 if (copy_to_user(arg, &cam->compression,
2113 sizeof(cam->compression)))
2119 case VIDIOC_S_JPEGCOMP:
2121 struct v4l2_jpegcompression jc;
2122 const enum sn9c102_stream_state stream = cam->stream;
2125 if (copy_from_user(&jc, arg, sizeof(jc)))
2128 if (jc.quality != 0 && jc.quality != 1)
2131 if (cam->stream == STREAM_ON)
2132 if ((err = sn9c102_stream_interrupt(cam)))
2135 err += sn9c102_set_compression(cam, &jc);
2136 if (err) { /* atomic, no rollback in ioctl() */
2137 cam->state |= DEV_MISCONFIGURED;
2138 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2139 "problems. To use the camera, close and open "
2140 "/dev/video%d again.", cam->v4ldev->minor)
2144 cam->compression.quality = jc.quality;
2146 cam->stream = stream;
2151 case VIDIOC_REQBUFS:
2153 struct v4l2_requestbuffers rb;
2157 if (copy_from_user(&rb, arg, sizeof(rb)))
2160 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2161 rb.memory != V4L2_MEMORY_MMAP)
2164 if (cam->io == IO_READ) {
2165 DBG(3, "Close and open the device again to choose "
2166 "the mmap I/O method")
2170 for (i = 0; i < cam->nbuffers; i++)
2171 if (cam->frame[i].vma_use_count) {
2172 DBG(3, "VIDIOC_REQBUFS failed. "
2173 "Previous buffers are still mapped.")
2177 if (cam->stream == STREAM_ON)
2178 if ((err = sn9c102_stream_interrupt(cam)))
2181 sn9c102_empty_framequeues(cam);
2183 sn9c102_release_buffers(cam);
2185 rb.count = sn9c102_request_buffers(cam, rb.count);
2187 if (copy_to_user(arg, &rb, sizeof(rb))) {
2188 sn9c102_release_buffers(cam);
2193 cam->io = rb.count ? IO_MMAP : IO_NONE;
2198 case VIDIOC_QUERYBUF:
2200 struct v4l2_buffer b;
2202 if (copy_from_user(&b, arg, sizeof(b)))
2205 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2206 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2209 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2211 if (cam->frame[b.index].vma_use_count)
2212 b.flags |= V4L2_BUF_FLAG_MAPPED;
2214 if (cam->frame[b.index].state == F_DONE)
2215 b.flags |= V4L2_BUF_FLAG_DONE;
2216 else if (cam->frame[b.index].state != F_UNUSED)
2217 b.flags |= V4L2_BUF_FLAG_QUEUED;
2219 if (copy_to_user(arg, &b, sizeof(b)))
2227 struct v4l2_buffer b;
2228 unsigned long lock_flags;
2230 if (copy_from_user(&b, arg, sizeof(b)))
2233 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2234 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2237 if (cam->frame[b.index].state != F_UNUSED)
2240 cam->frame[b.index].state = F_QUEUED;
2242 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2243 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2244 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2246 PDBGG("Frame #%lu queued", (unsigned long)b.index)
2253 struct v4l2_buffer b;
2254 struct sn9c102_frame_t *f;
2255 unsigned long lock_flags;
2258 if (copy_from_user(&b, arg, sizeof(b)))
2261 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2264 if (list_empty(&cam->outqueue)) {
2265 if (cam->stream == STREAM_OFF)
2267 if (filp->f_flags & O_NONBLOCK)
2269 err = wait_event_interruptible
2271 (!list_empty(&cam->outqueue)) ||
2272 (cam->state & DEV_DISCONNECTED) );
2275 if (cam->state & DEV_DISCONNECTED)
2279 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2280 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t,
2282 list_del(cam->outqueue.next);
2283 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2285 f->state = F_UNUSED;
2287 memcpy(&b, &f->buf, sizeof(b));
2288 if (f->vma_use_count)
2289 b.flags |= V4L2_BUF_FLAG_MAPPED;
2291 if (copy_to_user(arg, &b, sizeof(b)))
2294 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index)
2299 case VIDIOC_STREAMON:
2303 if (copy_from_user(&type, arg, sizeof(type)))
2306 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2309 if (list_empty(&cam->inqueue))
2312 cam->stream = STREAM_ON;
2319 case VIDIOC_STREAMOFF:
2323 if (copy_from_user(&type, arg, sizeof(type)))
2326 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2329 if (cam->stream == STREAM_ON)
2330 if ((err = sn9c102_stream_interrupt(cam)))
2333 sn9c102_empty_framequeues(cam);
2335 DBG(3, "Stream off")
2342 struct v4l2_streamparm sp;
2344 if (copy_from_user(&sp, arg, sizeof(sp)))
2347 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2350 sp.parm.capture.extendedmode = 0;
2351 sp.parm.capture.readbuffers = cam->nreadbuffers;
2353 if (copy_to_user(arg, &sp, sizeof(sp)))
2361 struct v4l2_streamparm sp;
2363 if (copy_from_user(&sp, arg, sizeof(sp)))
2366 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2369 sp.parm.capture.extendedmode = 0;
2371 if (sp.parm.capture.readbuffers == 0)
2372 sp.parm.capture.readbuffers = cam->nreadbuffers;
2374 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2375 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2377 if (copy_to_user(arg, &sp, sizeof(sp)))
2380 cam->nreadbuffers = sp.parm.capture.readbuffers;
2387 case VIDIOC_QUERYSTD:
2388 case VIDIOC_ENUMSTD:
2389 case VIDIOC_QUERYMENU:
2399 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2400 unsigned int cmd, unsigned long arg)
2402 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2405 if (down_interruptible(&cam->fileop_sem))
2406 return -ERESTARTSYS;
2408 if (cam->state & DEV_DISCONNECTED) {
2409 DBG(1, "Device not present")
2410 up(&cam->fileop_sem);
2414 if (cam->state & DEV_MISCONFIGURED) {
2415 DBG(1, "The camera is misconfigured. Close and open it again.")
2416 up(&cam->fileop_sem);
2420 err = sn9c102_v4l2_ioctl(inode, filp, cmd, (void __user *)arg);
2422 up(&cam->fileop_sem);
2428 static struct file_operations sn9c102_fops = {
2429 .owner = THIS_MODULE,
2430 .open = sn9c102_open,
2431 .release = sn9c102_release,
2432 .ioctl = sn9c102_ioctl,
2433 .read = sn9c102_read,
2434 .poll = sn9c102_poll,
2435 .mmap = sn9c102_mmap,
2436 .llseek = no_llseek,
2439 /*****************************************************************************/
2441 /* It exists a single interface only. We do not need to validate anything. */
2443 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2445 struct usb_device *udev = interface_to_usbdev(intf);
2446 struct sn9c102_device* cam;
2447 static unsigned int dev_nr = 0;
2451 n = sizeof(sn9c102_id_table)/sizeof(sn9c102_id_table[0]);
2452 for (i = 0; i < n-1; i++)
2453 if (udev->descriptor.idVendor==sn9c102_id_table[i].idVendor &&
2454 udev->descriptor.idProduct==sn9c102_id_table[i].idProduct)
2459 if (!(cam = kmalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2461 memset(cam, 0, sizeof(*cam));
2465 memcpy(&cam->dev, &udev->dev, sizeof(struct device));
2467 if (!(cam->control_buffer = kmalloc(8, GFP_KERNEL))) {
2468 DBG(1, "kmalloc() failed")
2472 memset(cam->control_buffer, 0, 8);
2474 if (!(cam->v4ldev = video_device_alloc())) {
2475 DBG(1, "video_device_alloc() failed")
2480 init_MUTEX(&cam->dev_sem);
2482 r = sn9c102_read_reg(cam, 0x00);
2483 if (r < 0 || r != 0x10) {
2484 DBG(1, "Sorry, this is not a SN9C10x based camera "
2485 "(vid/pid 0x%04X/0x%04X)",
2486 sn9c102_id_table[i].idVendor,sn9c102_id_table[i].idProduct)
2491 cam->bridge = (sn9c102_id_table[i].idProduct & 0xffc0) == 0x6080 ?
2492 BRIDGE_SN9C103 : BRIDGE_SN9C102;
2493 switch (cam->bridge) {
2494 case BRIDGE_SN9C101:
2495 case BRIDGE_SN9C102:
2496 DBG(2, "SN9C10[12] PC Camera Controller detected "
2497 "(vid/pid 0x%04X/0x%04X)", sn9c102_id_table[i].idVendor,
2498 sn9c102_id_table[i].idProduct)
2500 case BRIDGE_SN9C103:
2501 DBG(2, "SN9C103 PC Camera Controller detected "
2502 "(vid/pid 0x%04X/0x%04X)", sn9c102_id_table[i].idVendor,
2503 sn9c102_id_table[i].idProduct)
2507 for (i = 0; sn9c102_sensor_table[i]; i++) {
2508 err = sn9c102_sensor_table[i](cam);
2513 if (!err && cam->sensor) {
2514 DBG(2, "%s image sensor detected", cam->sensor->name)
2515 DBG(3, "Support for %s maintained by %s",
2516 cam->sensor->name, cam->sensor->maintainer)
2518 DBG(1, "No supported image sensor detected")
2523 if (sn9c102_init(cam)) {
2524 DBG(1, "Initialization failed. I will retry on open().")
2525 cam->state |= DEV_MISCONFIGURED;
2528 strcpy(cam->v4ldev->name, "SN9C10x PC Camera");
2529 cam->v4ldev->owner = THIS_MODULE;
2530 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2531 cam->v4ldev->hardware = VID_HARDWARE_SN9C102;
2532 cam->v4ldev->fops = &sn9c102_fops;
2533 cam->v4ldev->minor = video_nr[dev_nr];
2534 cam->v4ldev->release = video_device_release;
2535 video_set_drvdata(cam->v4ldev, cam);
2537 down(&cam->dev_sem);
2539 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2542 DBG(1, "V4L2 device registration failed")
2543 if (err == -ENFILE && video_nr[dev_nr] == -1)
2544 DBG(1, "Free /dev/videoX node not found")
2545 video_nr[dev_nr] = -1;
2546 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2551 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor)
2553 sn9c102_create_sysfs(cam);
2555 usb_set_intfdata(intf, cam);
2563 kfree(cam->control_buffer);
2565 video_device_release(cam->v4ldev);
2572 static void sn9c102_usb_disconnect(struct usb_interface* intf)
2574 struct sn9c102_device* cam = usb_get_intfdata(intf);
2579 down_write(&sn9c102_disconnect);
2581 down(&cam->dev_sem);
2583 DBG(2, "Disconnecting %s...", cam->v4ldev->name)
2585 wake_up_interruptible_all(&cam->open);
2588 DBG(2, "Device /dev/video%d is open! Deregistration and "
2589 "memory deallocation are deferred on close.",
2591 cam->state |= DEV_MISCONFIGURED;
2592 sn9c102_stop_transfer(cam);
2593 cam->state |= DEV_DISCONNECTED;
2594 wake_up_interruptible(&cam->wait_frame);
2595 wake_up_interruptible(&cam->wait_stream);
2597 cam->state |= DEV_DISCONNECTED;
2598 sn9c102_release_resources(cam);
2606 up_write(&sn9c102_disconnect);
2610 static struct usb_driver sn9c102_usb_driver = {
2611 .owner = THIS_MODULE,
2613 .id_table = sn9c102_id_table,
2614 .probe = sn9c102_usb_probe,
2615 .disconnect = sn9c102_usb_disconnect,
2618 /*****************************************************************************/
2620 static int __init sn9c102_module_init(void)
2624 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION)
2625 KDBG(3, SN9C102_MODULE_AUTHOR)
2627 if ((err = usb_register(&sn9c102_usb_driver)))
2628 KDBG(1, "usb_register() failed")
2634 static void __exit sn9c102_module_exit(void)
2636 usb_deregister(&sn9c102_usb_driver);
2640 module_init(sn9c102_module_init);
2641 module_exit(sn9c102_module_exit);