21 #include <linux/module.h>
23 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/slab.h>
27 #include <linux/device.h>
30 #include <linux/compiler.h>
32 #include <linux/poll.h>
33 #include <linux/stat.h>
36 #include <linux/version.h>
38 #include <asm/byteorder.h>
40 #include <asm/uaccess.h>
46 #define SN9C102_MODULE_NAME "V4L2 driver for SN9C1xx PC Camera Controllers"
47 #define SN9C102_MODULE_ALIAS "sn9c1xx"
48 #define SN9C102_MODULE_AUTHOR "(C) 2004-2007 Luca Risolia"
50 #define SN9C102_MODULE_LICENSE "GPL"
51 #define SN9C102_MODULE_VERSION "1:1.48"
67 "\nSpecify V4L2 minor mode number."
68 "\n-1 = use next available (default)"
69 "\n n = use minor number n (integer >= 0)"
73 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
74 "\nthe second camera and use auto for the first"
75 "\none and for every other camera."
83 "\nForce the application to unmap previously"
84 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
85 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
86 "\nthis feature. This parameter is specific for each"
88 "\n0 = do not force memory unmapping"
89 "\n1 = force memory unmapping (save memory)"
98 "\nTimeout for a video frame in seconds before"
99 "\nreturning an I/O error; 0 for infinity."
100 "\nThis parameter is specific for each detected camera."
109 "\nDebugging information level, from 0 to 3:"
110 "\n0 = none (use carefully)"
111 "\n1 = critical errors"
112 "\n2 = significant informations"
113 "\n3 = more verbose messages"
114 "\nLevel 3 is useful for testing only."
157 imagesize += 589 + 2;
167 for (i = 0; i < cam->
nbuffers; i++) {
171 cam->
frame[
i].buf.length = imagesize;
173 cam->
frame[
i].buf.sequence = 0;
176 cam->
frame[
i].buf.flags = 0;
197 INIT_LIST_HEAD(&cam->inqueue);
202 cam->
frame[
i].buf.bytesused = 0;
213 list_add(&i->
frame, &cam->inqueue);
220 static void sn9c102_queue_unusedframes(
struct sn9c102_device* cam)
230 spin_unlock_irqrestore(&cam->
queue_lock, lock_flags);
247 for (i = 0; i <
count; i++) {
257 *buff = valreg[
i][0];
260 0x41, index, 0, buff, 1,
264 DBG(3,
"Failed to write a register (value 0x%02X, "
265 "index 0x%02X, error %d)", *buff, index, res);
290 DBG(3,
"Failed to write a register (value 0x%02X, index "
291 "0x%02X, error %d)", value, index, res);
311 DBG(3,
"Failed to read a register (index 0x%02X, error %d)",
314 return (res >= 0) ? (
int)(*buff) : -1;
333 for (i = 1; i <= 5; i++) {
366 return err ? -
EIO : 0;
376 return (r < 0 || (r >= 0 && (r & 0x08))) ? -
EIO : 0;
387 int i = 0, err = 0,
res;
400 err += sn9c102_i2c_wait(cam, sensor);
413 err += sn9c102_i2c_wait(cam, sensor);
421 err += sn9c102_i2c_detect_read_error(cam, sensor);
423 PDBGG(
"I2C read: address 0x%02X, first read byte: 0x%02X", data1,
427 DBG(3,
"I2C read failed for %s image sensor", sensor->
name);
432 for (i = 0; i < n && i < 5; i++)
433 buffer[n-i-1] = data[4-i];
464 err += sn9c102_i2c_wait(cam, sensor);
465 err += sn9c102_i2c_detect_write_error(cam, sensor);
468 DBG(3,
"I2C write failed for %s image sensor", sensor->
name);
470 PDBGG(
"I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
471 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
472 n, data0, data1, data2, data3, data4, data5);
505 return sn9c102_i2c_try_write(cam, &cam->
sensor, address, value);
530 static const char marker[6] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
532 size_t soflen = 0,
i,
j;
534 soflen = sn9c102_sof_length(cam);
536 for (i = 0; i < len; i++) {
540 if (
unlikely(cam->
sof.bytesread >=
sizeof(marker))) {
541 cam->
sof.header[cam->
sof.bytesread] = *(m+
i);
542 if (++cam->
sof.bytesread == soflen) {
543 cam->
sof.bytesread = 0;
550 for (j = 0, b=cam->
sof.bytesread; j+b <
sizeof(marker); j++) {
553 if (*(m+i+j) == marker[cam->
sof.bytesread]) {
554 cam->
sof.header[cam->
sof.bytesread] = *(m+i+
j);
555 if (++cam->
sof.bytesread ==
sizeof(marker)) {
556 PDBGG(
"Bytes to analyze: %zd. SOF "
557 "starts at byte #%zd", len, i);
562 cam->
sof.bytesread = 0;
573 sn9c102_find_eof_header(
struct sn9c102_device* cam,
void* mem,
size_t len)
575 static const u8 eof_header[4][4] = {
576 {0x00, 0x00, 0x00, 0x00},
577 {0x40, 0x00, 0x00, 0x00},
578 {0x80, 0x00, 0x00, 0x00},
579 {0xc0, 0x00, 0x00, 0x00},
593 for (i = 0; (len >= 4) && (i <= len - 4); i++)
595 if (!
memcmp(mem + i, eof_header[j], 4))
605 static const u8 jpeg_header[589] = {
606 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00, 0x06, 0x04, 0x05,
607 0x06, 0x05, 0x04, 0x06, 0x06, 0x05, 0x06, 0x07, 0x07, 0x06,
608 0x08, 0x0a, 0x10, 0x0a, 0x0a, 0x09, 0x09, 0x0a, 0x14, 0x0e,
609 0x0f, 0x0c, 0x10, 0x17, 0x14, 0x18, 0x18, 0x17, 0x14, 0x16,
610 0x16, 0x1a, 0x1d, 0x25, 0x1f, 0x1a, 0x1b, 0x23, 0x1c, 0x16,
611 0x16, 0x20, 0x2c, 0x20, 0x23, 0x26, 0x27, 0x29, 0x2a, 0x29,
612 0x19, 0x1f, 0x2d, 0x30, 0x2d, 0x28, 0x30, 0x25, 0x28, 0x29,
613 0x28, 0x01, 0x07, 0x07, 0x07, 0x0a, 0x08, 0x0a, 0x13, 0x0a,
614 0x0a, 0x13, 0x28, 0x1a, 0x16, 0x1a, 0x28, 0x28, 0x28, 0x28,
615 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
616 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
617 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
618 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
619 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0xff, 0xc4, 0x01, 0xa2,
620 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
621 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
622 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01,
623 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
624 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
625 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10, 0x00,
626 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,
627 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04,
628 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
629 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,
630 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62,
631 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,
632 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38,
633 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
634 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
635 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
636 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
637 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
638 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
639 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
640 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3,
641 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3,
642 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3,
643 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02,
644 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04,
645 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
646 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
647 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1,
648 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
649 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19,
650 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
651 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
652 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
653 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
654 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
655 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
656 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
657 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
658 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
659 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3,
660 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
661 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc0, 0x00, 0x11,
662 0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x01, 0x21, 0x00, 0x02,
663 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03,
664 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00
668 memcpy(pos, jpeg_header,
sizeof(jpeg_header));
670 *(pos + 7 + 64) = 0x01;
672 memcpy(pos + 7, SN9C102_Y_QTABLE0, 64);
673 memcpy(pos + 8 + 64, SN9C102_UV_QTABLE0, 64);
675 memcpy(pos + 7, SN9C102_Y_QTABLE1, 64);
676 memcpy(pos + 8 + 64, SN9C102_UV_QTABLE1, 64);
678 *(pos + 564) = cam->
sensor.pix_format.width & 0xFF;
679 *(pos + 563) = (cam->
sensor.pix_format.width >> 8) & 0xFF;
680 *(pos + 562) = cam->
sensor.pix_format.height & 0xFF;
681 *(pos + 561) = (cam->
sensor.pix_format.height >> 8) & 0xFF;
684 f->
buf.bytesused +=
sizeof(jpeg_header);
688 static void sn9c102_urb_complete(
struct urb *
urb)
692 size_t imagesize, soflen;
696 if (urb->status == -
ENOENT)
705 cam->
sof.bytesread = 0;
706 DBG(3,
"Stream interrupted by application");
725 imagesize = (cam->
sensor.pix_format.width *
726 cam->
sensor.pix_format.height *
727 cam->
sensor.pix_format.priv) / 8;
730 soflen = sn9c102_sof_length(cam);
732 for (i = 0; i < urb->number_of_packets; i++) {
733 unsigned int img, len,
status;
734 void *
pos, *sof, *eof;
736 len = urb->iso_frame_desc[
i].actual_length;
737 status = urb->iso_frame_desc[
i].status;
738 pos = urb->iso_frame_desc[
i].offset + urb->transfer_buffer;
741 DBG(3,
"Error in isochronous frame");
743 cam->
sof.bytesread = 0;
747 PDBGG(
"Isochrnous frame: length %u, #%u i", len, i);
750 sof = sn9c102_find_sof_header(cam, pos, len);
752 eof = sn9c102_find_eof_header(cam, pos, len);
758 img = (eof >
pos) ? eof - pos - 1 : 0;
760 if ((*f)->buf.bytesused + img > imagesize) {
762 b = (*f)->buf.bytesused + img -
764 img = imagesize - (*f)->buf.bytesused;
765 PDBGG(
"Expected EOF not found: video "
768 DBG(3,
"Exceeded limit: +%u "
769 "bytes", (
unsigned)(b));
772 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
775 if ((*f)->buf.bytesused == 0)
778 (*f)->buf.bytesused += img;
780 if ((*f)->buf.bytesused == imagesize ||
781 ((cam->
sensor.pix_format.pixelformat ==
783 cam->
sensor.pix_format.pixelformat ==
787 b = (*f)->buf.bytesused;
792 list_move_tail(&(*f)->frame,
794 if (!list_empty(&cam->inqueue))
804 cam->
sof.header, soflen);
806 DBG(3,
"Video frame captured: %lu "
807 "bytes", (
unsigned long)(b));
814 DBG(3,
"Not expected EOF after %lu "
815 "bytes of image data",
817 ((*f)->buf.bytesused));
824 DBG(3,
"EOF without SOF");
828 PDBGG(
"Ignoring pointless isochronous frame");
835 (*f)->buf.bytesused = 0;
838 if (cam->
sensor.pix_format.pixelformat ==
840 sn9c102_write_jpegheader(cam, (*f));
841 DBG(3,
"SOF detected: new video frame");
846 eof = sn9c102_find_eof_header(cam, pos, len);
847 if (eof && eof < sof)
850 if (cam->
sensor.pix_format.pixelformat ==
852 cam->
sensor.pix_format.pixelformat ==
854 if (sof - pos >= soflen) {
858 (*f)->buf.bytesused -=
859 (soflen - (sof -
pos));
863 DBG(3,
"SOF before expected EOF after "
864 "%lu bytes of image data",
866 ((*f)->buf.bytesused));
876 if (err < 0 && err != -
EPERM) {
878 DBG(1,
"usb_submit_urb() failed");
902 DBG(1,
"Not enough memory");
912 DBG(1,
"usb_alloc_urb() failed");
917 urb->pipe = usb_rcvisocpipe(udev, 1);
918 urb->transfer_flags = URB_ISO_ASAP;
920 urb->complete = sn9c102_urb_complete;
925 urb->iso_frame_desc[
j].offset = psz *
j;
926 urb->iso_frame_desc[
j].length = psz;
931 if (!(cam->
reg[0x01] & 0x04)) {
935 DBG(1,
"I/O hardware error");
942 DBG(1,
"usb_set_interface() failed");
947 cam->
sof.bytesread = 0;
952 for (j = i-1; j >= 0; j--)
954 DBG(1,
"usb_submit_urb() failed, error %d", err);
975 struct usb_device *udev = cam->
usbdev;
982 for (i = SN9C102_URBS-1; i >= 0; i--) {
990 DBG(3,
"usb_set_interface() failed");
1007 DBG(1,
"URB timeout reached. The camera is misconfigured. "
1008 "To use it, close and open %s again.",
1009 video_device_node_name(cam->
v4ldev));
1018 #ifdef CONFIG_VIDEO_ADV_DEBUG
1019 static u16 sn9c102_strtou16(
const char* buff,
size_t len,
ssize_t* count)
1037 *count = (
ssize_t)(endp - str);
1038 if ((*count) && (len == *count+1) && (buff[*count] ==
'\n'))
1075 const char* buf,
size_t len)
1090 index = sn9c102_strtou16(buf, len, &count);
1098 DBG(2,
"Moved SN9C1XX register index to 0x%02X", cam->
sysfs.reg);
1099 DBG(3,
"Written bytes: %zd", count);
1128 count =
sprintf(buf,
"%d\n", val);
1130 DBG(3,
"Read bytes: %zd, value: %d", count, val);
1140 const char* buf,
size_t len)
1156 value = sn9c102_strtou16(buf, len, &count);
1168 DBG(2,
"Written SN9C1XX reg. 0x%02X, val. 0x%02X",
1169 cam->
sysfs.reg, value);
1170 DBG(3,
"Written bytes: %zd", count);
1195 DBG(3,
"Read bytes: %zd", count);
1205 const char* buf,
size_t len)
1220 index = sn9c102_strtou16(buf, len, &count);
1228 DBG(2,
"Moved sensor register index to 0x%02X", cam->
sysfs.i2c_reg);
1229 DBG(3,
"Written bytes: %zd", count);
1263 count =
sprintf(buf,
"%d\n", val);
1265 DBG(3,
"Read bytes: %zd, value: %d", count, val);
1275 const char* buf,
size_t len)
1296 value = sn9c102_strtou16(buf, len, &count);
1308 DBG(2,
"Written sensor reg. 0x%02X, val. 0x%02X",
1309 cam->
sysfs.i2c_reg, value);
1310 DBG(3,
"Written bytes: %zd", count);
1320 const char* buf,
size_t len)
1341 value = sn9c102_strtou16(buf, len, &count);
1350 if ((res = sn9c102_store_reg(cd, attr,
"0x11", 4)) >= 0)
1351 res = sn9c102_store_val(cd, attr, buf, len);
1358 if ((res = sn9c102_store_reg(cd, attr,
"0x07", 4)) >= 0)
1359 res = sn9c102_store_val(cd, attr, buf, len);
1369 const char* buf,
size_t len)
1375 value = sn9c102_strtou16(buf, len, &count);
1376 if (!count || value > 0x7f)
1379 if ((res = sn9c102_store_reg(cd, attr,
"0x06", 4)) >= 0)
1380 res = sn9c102_store_val(cd, attr, buf, len);
1388 const char* buf,
size_t len)
1394 value = sn9c102_strtou16(buf, len, &count);
1395 if (!count || value > 0x7f)
1398 if ((res = sn9c102_store_reg(cd, attr,
"0x05", 4)) >= 0)
1399 res = sn9c102_store_val(cd, attr, buf, len);
1416 count =
sizeof(cam->
sysfs.frame_header);
1419 DBG(3,
"Frame header, read bytes: %zd", count);
1428 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1430 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1449 if (cam->
sensor.sysfs_ops) {
1451 goto err_frame_header;
1471 if (cam->
sensor.sysfs_ops)
1474 if (cam->
sensor.sysfs_ops)
1525 return err ? -
EIO : 0;
1539 if (compression->
quality == 0)
1542 else if (compression->
quality == 1)
1548 if (compression->
quality == 0) {
1549 for (i = 0; i <= 63; i++) {
1551 SN9C102_Y_QTABLE1[i],
1554 SN9C102_UV_QTABLE1[i],
1559 }
else if (compression->
quality == 1) {
1560 for (i = 0; i <= 63; i++) {
1562 SN9C102_Y_QTABLE1[i],
1565 SN9C102_UV_QTABLE1[i],
1574 return err ? -
EIO : 0;
1584 r = cam->
reg[0x18] & 0xcf;
1585 else if (scale == 2) {
1586 r = cam->
reg[0x18] & 0xcf;
1588 }
else if (scale == 4)
1589 r = cam->
reg[0x18] | 0x20;
1595 PDBGG(
"Scaling factor: %u", scale);
1606 h_size = (
u8)(rect->
width / 16),
1617 PDBGG(
"h_start, v_start, h_size, v_size, ho_size, vo_size "
1618 "%u %u %u %u", h_start, v_start, h_size, v_size);
1644 err += sn9c102_set_crop(cam, rect);
1651 DBG(3,
"Sensor initialization failed");
1669 err += sn9c102_set_compression(cam, &cam->
compression);
1672 err += sn9c102_set_compression(cam, &cam->
compression);
1673 err += sn9c102_set_pix_format(cam, &s->
pix_format);
1681 DBG(3,
"Compressed video format is active, quality %d",
1684 DBG(3,
"Uncompressed video format is active");
1687 if ((err = s->
set_crop(cam, rect))) {
1688 DBG(3,
"set_crop() failed");
1694 if (s->
qctrl[i].id != 0 &&
1700 DBG(3,
"Set %s control failed",
1704 DBG(3,
"Image sensor supports '%s' control",
1721 DBG(2,
"Initialization succeeded");
1727 static void sn9c102_release_resources(
struct kref *
kref)
1735 DBG(2,
"V4L2 device %s deregistered",
1736 video_device_node_name(cam->
v4ldev));
1748 static int sn9c102_open(
struct file *filp)
1768 cam = video_drvdata(filp);
1775 kref_get(&cam->
kref);
1781 kref_put(&cam->
kref, sn9c102_release_resources);
1787 DBG(1,
"Device not present");
1793 DBG(2,
"Device %s is already in use",
1794 video_device_node_name(cam->
v4ldev));
1795 DBG(3,
"Simultaneous opens are not supported");
1805 DBG(2,
"A blocking open() has been requested. Wait for the "
1806 "device to be released...");
1827 err = sn9c102_init(cam);
1829 DBG(1,
"Initialization failed again. "
1830 "I will retry on next open().");
1836 if ((err = sn9c102_start_transfer(cam)))
1845 sn9c102_empty_framequeues(cam);
1847 DBG(3,
"Video device %s is open", video_device_node_name(cam->
v4ldev));
1852 kref_put(&cam->
kref, sn9c102_release_resources);
1859 static int sn9c102_release(
struct file *filp)
1865 cam = video_drvdata(filp);
1867 sn9c102_stop_transfer(cam);
1868 sn9c102_release_buffers(cam);
1872 DBG(3,
"Video device %s closed", video_device_node_name(cam->
v4ldev));
1874 kref_put(&cam->
kref, sn9c102_release_resources);
1883 sn9c102_read(
struct file* filp,
char __user * buf,
size_t count, loff_t* f_pos)
1895 DBG(1,
"Device not present");
1901 DBG(1,
"The camera is misconfigured. Close and open it "
1908 DBG(3,
"Close and open the device again to choose "
1916 DBG(1,
"read() failed, not enough memory");
1924 if (list_empty(&cam->inqueue)) {
1926 sn9c102_empty_framequeues(cam);
1927 sn9c102_queue_unusedframes(cam);
1963 }
else if (timeout == 0 &&
1965 DBG(1,
"Video frame timeout elapsed");
1982 if (count > f->
buf.bytesused)
1983 count = f->
buf.bytesused;
1995 INIT_LIST_HEAD(&cam->outqueue);
1996 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1998 sn9c102_queue_unusedframes(cam);
2000 PDBGG("Frame
#%lu, bytes read: %zu",
2001 (
unsigned long)f->
buf.index, count);
2014 unsigned int mask = 0;
2020 DBG(1,
"Device not present");
2025 DBG(1,
"The camera is misconfigured. Close and open it "
2033 DBG(1,
"poll() failed, not enough memory");
2044 INIT_LIST_HEAD(&cam->outqueue);
2045 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2046 sn9c102_queue_unusedframes(cam);
2049 poll_wait(filp, &cam->wait_frame, wait);
2051 if (!list_empty(&cam->outqueue))
2079 static const struct vm_operations_struct sn9c102_vm_ops = {
2080 .open = sn9c102_vm_open,
2081 .close = sn9c102_vm_close,
2097 DBG(1,
"Device not present");
2103 DBG(1,
"The camera is misconfigured. Close and open it "
2109 if (!(vma->
vm_flags & (VM_WRITE | VM_READ))) {
2120 for (i = 0; i < cam->
nbuffers; i++) {
2129 vma->
vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
2131 pos = cam->
frame[
i].bufmem;
2142 vma->
vm_ops = &sn9c102_vm_ops;
2144 sn9c102_vm_open(vma);
2157 .driver =
"sn9c102",
2158 .version = LINUX_VERSION_CODE,
2176 sn9c102_vidioc_enuminput(
struct sn9c102_device* cam,
void __user * arg)
2186 memset(&i, 0,
sizeof(i));
2187 strcpy(i.name,
"Camera");
2199 sn9c102_vidioc_g_input(
struct sn9c102_device* cam,
void __user * arg)
2211 sn9c102_vidioc_s_input(
struct sn9c102_device* cam,
void __user * arg)
2226 sn9c102_vidioc_query_ctrl(
struct sn9c102_device* cam,
void __user * arg)
2236 if (qc.id && qc.id == s->
qctrl[i].id) {
2248 sn9c102_vidioc_g_ctrl(
struct sn9c102_device* cam,
void __user * arg)
2275 PDBGG(
"VIDIOC_G_CTRL: id %lu, value %lu",
2276 (
unsigned long)
ctrl.id, (
unsigned long)
ctrl.value);
2283 sn9c102_vidioc_s_ctrl(
struct sn9c102_device* cam,
void __user * arg)
2298 if (s->
qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
2314 PDBGG(
"VIDIOC_S_CTRL: id %lu, value %lu",
2315 (
unsigned long)
ctrl.id, (
unsigned long)
ctrl.value);
2322 sn9c102_vidioc_cropcap(
struct sn9c102_device* cam,
void __user * arg)
2338 sn9c102_vidioc_g_crop(
struct sn9c102_device* cam,
void __user * arg)
2355 sn9c102_vidioc_s_crop(
struct sn9c102_device* cam,
void __user * arg)
2377 for (i = 0; i < cam->
nbuffers; i++)
2378 if (cam->
frame[i].vma_use_count) {
2379 DBG(3,
"VIDIOC_S_CROP failed. "
2380 "Unmap the buffers first.");
2388 if (rect->
width < 16)
2398 if (rect->
top < bounds->
top)
2413 scale = b ? (
u8)((a / b) < 4 ? 1 : ((a /
b) < 16 ? 2 : 4)) : 1;
2418 if ((err = sn9c102_stream_interrupt(cam)))
2427 sn9c102_release_buffers(cam);
2429 err = sn9c102_set_crop(cam, rect);
2432 err += sn9c102_set_scale(cam, scale);
2436 DBG(1,
"VIDIOC_S_CROP failed because of hardware problems. To "
2437 "use the camera, close and open %s again.",
2438 video_device_node_name(cam->
v4ldev));
2447 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->
io)) {
2449 DBG(1,
"VIDIOC_S_CROP failed because of not enough memory. To "
2450 "use the camera, close and open %s again.",
2451 video_device_node_name(cam->
v4ldev));
2456 sn9c102_empty_framequeues(cam);
2458 sn9c102_requeue_outqueue(cam);
2467 sn9c102_vidioc_enum_framesizes(
struct sn9c102_device* cam,
void __user * arg)
2474 if (frmsize.index != 0)
2492 frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
2493 frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
2494 frmsize.stepwise.max_width = cam->
sensor.cropcap.bounds.width;
2495 frmsize.stepwise.max_height = cam->
sensor.cropcap.bounds.height;
2496 memset(&frmsize.reserved, 0,
sizeof(frmsize.reserved));
2506 sn9c102_vidioc_enum_fmt(
struct sn9c102_device* cam,
void __user * arg)
2516 if (fmtd.index == 0) {
2517 strcpy(fmtd.description,
"bayer rgb");
2519 }
else if (fmtd.index == 1) {
2524 strcpy(fmtd.description,
"compressed");
2529 strcpy(fmtd.description,
"JPEG");
2538 memset(&fmtd.reserved, 0,
sizeof(fmtd.reserved));
2548 sn9c102_vidioc_g_fmt(
struct sn9c102_device* cam,
void __user * arg)
2605 scale = b ? (
u8)((a / b) < 4 ? 1 : ((a /
b) < 16 ? 2 : 4)) : 1;
2611 if (rect.
width < 16)
2627 scale = b ? (
u8)((a / b) < 4 ? 1 : ((a /
b) < 16 ? 2 : 4)) : 1;
2664 for (i = 0; i < cam->
nbuffers; i++)
2665 if (cam->
frame[i].vma_use_count) {
2666 DBG(3,
"VIDIOC_S_FMT failed. Unmap the "
2672 if ((err = sn9c102_stream_interrupt(cam)))
2681 sn9c102_release_buffers(cam);
2683 err += sn9c102_set_pix_format(cam, pix);
2684 err += sn9c102_set_crop(cam, &rect);
2689 err += sn9c102_set_scale(cam, scale);
2693 DBG(1,
"VIDIOC_S_FMT failed because of hardware problems. To "
2694 "use the camera, close and open %s again.",
2695 video_device_node_name(cam->
v4ldev));
2699 memcpy(pfmt, pix,
sizeof(*pix));
2703 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->
io)) {
2705 DBG(1,
"VIDIOC_S_FMT failed because of not enough memory. To "
2706 "use the camera, close and open %s again.",
2707 video_device_node_name(cam->
v4ldev));
2712 sn9c102_empty_framequeues(cam);
2714 sn9c102_requeue_outqueue(cam);
2723 sn9c102_vidioc_g_jpegcomp(
struct sn9c102_device* cam,
void __user * arg)
2733 sn9c102_vidioc_s_jpegcomp(
struct sn9c102_device* cam,
void __user * arg)
2742 if (jc.quality != 0 && jc.quality != 1)
2746 if ((err = sn9c102_stream_interrupt(cam)))
2749 err += sn9c102_set_compression(cam, &jc);
2752 DBG(1,
"VIDIOC_S_JPEGCOMP failed because of hardware problems. "
2753 "To use the camera, close and open %s again.",
2754 video_device_node_name(cam->
v4ldev));
2767 sn9c102_vidioc_reqbufs(
struct sn9c102_device* cam,
void __user * arg)
2781 DBG(3,
"Close and open the device again to choose the mmap "
2786 for (i = 0; i < cam->
nbuffers; i++)
2787 if (cam->
frame[i].vma_use_count) {
2788 DBG(3,
"VIDIOC_REQBUFS failed. Previous buffers are "
2794 if ((err = sn9c102_stream_interrupt(cam)))
2797 sn9c102_empty_framequeues(cam);
2799 sn9c102_release_buffers(cam);
2801 rb.count = sn9c102_request_buffers(cam,
rb.count,
IO_MMAP);
2804 sn9c102_release_buffers(cam);
2816 sn9c102_vidioc_querybuf(
struct sn9c102_device* cam,
void __user * arg)
2829 if (cam->
frame[b.index].vma_use_count)
2845 sn9c102_vidioc_qbuf(
struct sn9c102_device* cam,
void __user * arg)
2864 spin_unlock_irqrestore(&cam->
queue_lock, lock_flags);
2866 PDBGG(
"Frame #%lu queued", (
unsigned long)b.index);
2911 else if (timeout == 0 &&
2913 DBG(1,
"Video frame timeout elapsed");
2926 spin_unlock_irqrestore(&cam->
queue_lock, lock_flags);
2937 PDBGG(
"Frame #%lu dequeued", (
unsigned long)f->
buf.index);
2944 sn9c102_vidioc_streamon(
struct sn9c102_device* cam,
void __user * arg)
2956 DBG(3,
"Stream on");
2963 sn9c102_vidioc_streamoff(
struct sn9c102_device* cam,
void __user * arg)
2974 if ((err = sn9c102_stream_interrupt(cam)))
2977 sn9c102_empty_framequeues(cam);
2979 DBG(3,
"Stream off");
2986 sn9c102_vidioc_g_parm(
struct sn9c102_device* cam,
void __user * arg)
2996 sp.parm.capture.extendedmode = 0;
3007 sn9c102_vidioc_s_parm(
struct sn9c102_device* cam,
void __user * arg)
3017 sp.parm.capture.extendedmode = 0;
3019 if (
sp.parm.capture.readbuffers == 0)
3035 sn9c102_vidioc_enumaudio(
struct sn9c102_device* cam,
void __user * arg)
3045 if (
audio.index != 0)
3049 audio.capability = 0;
3060 sn9c102_vidioc_g_audio(
struct sn9c102_device* cam,
void __user * arg)
3081 sn9c102_vidioc_s_audio(
struct sn9c102_device* cam,
void __user * arg)
3091 if (
audio.index != 0)
3098 static long sn9c102_ioctl_v4l2(
struct file *filp,
3099 unsigned int cmd,
void __user *arg)
3106 return sn9c102_vidioc_querycap(cam, arg);
3109 return sn9c102_vidioc_enuminput(cam, arg);
3112 return sn9c102_vidioc_g_input(cam, arg);
3115 return sn9c102_vidioc_s_input(cam, arg);
3118 return sn9c102_vidioc_query_ctrl(cam, arg);
3121 return sn9c102_vidioc_g_ctrl(cam, arg);
3124 return sn9c102_vidioc_s_ctrl(cam, arg);
3127 return sn9c102_vidioc_cropcap(cam, arg);
3130 return sn9c102_vidioc_g_crop(cam, arg);
3133 return sn9c102_vidioc_s_crop(cam, arg);
3136 return sn9c102_vidioc_enum_framesizes(cam, arg);
3139 return sn9c102_vidioc_enum_fmt(cam, arg);
3142 return sn9c102_vidioc_g_fmt(cam, arg);
3146 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
3149 return sn9c102_vidioc_g_jpegcomp(cam, arg);
3152 return sn9c102_vidioc_s_jpegcomp(cam, arg);
3155 return sn9c102_vidioc_reqbufs(cam, arg);
3158 return sn9c102_vidioc_querybuf(cam, arg);
3161 return sn9c102_vidioc_qbuf(cam, arg);
3164 return sn9c102_vidioc_dqbuf(cam, filp, arg);
3167 return sn9c102_vidioc_streamon(cam, arg);
3170 return sn9c102_vidioc_streamoff(cam, arg);
3173 return sn9c102_vidioc_g_parm(cam, arg);
3176 return sn9c102_vidioc_s_parm(cam, arg);
3179 return sn9c102_vidioc_enumaudio(cam, arg);
3182 return sn9c102_vidioc_g_audio(cam, arg);
3185 return sn9c102_vidioc_s_audio(cam, arg);
3194 static long sn9c102_ioctl(
struct file *filp,
3195 unsigned int cmd,
unsigned long arg)
3204 DBG(1,
"Device not present");
3210 DBG(1,
"The camera is misconfigured. Close and open it "
3216 V4LDBG(3,
"sn9c102", cmd);
3218 err = sn9c102_ioctl_v4l2(filp, cmd, (
void __user *)arg);
3229 .open = sn9c102_open,
3230 .release = sn9c102_release,
3231 .unlocked_ioctl = sn9c102_ioctl,
3232 .read = sn9c102_read,
3233 .poll = sn9c102_poll,
3234 .mmap = sn9c102_mmap,
3243 struct usb_device *udev = interface_to_usbdev(intf);
3245 static unsigned int dev_nr;
3255 DBG(1,
"kzalloc() failed");
3261 DBG(1,
"video_device_alloc() failed");
3267 if (r < 0 || (r != 0x10 && r != 0x11 && r != 0x12)) {
3268 DBG(1,
"Sorry, this is not a SN9C1xx-based camera "
3274 cam->
bridge =
id->driver_info;
3278 DBG(2,
"SN9C10[12] PC Camera Controller detected "
3282 DBG(2,
"SN9C103 PC Camera Controller detected "
3286 DBG(2,
"SN9C105 PC Camera Controller detected "
3290 DBG(2,
"SN9C120 PC Camera Controller detected "
3295 for (i = 0; i <
ARRAY_SIZE(sn9c102_sensor_table); i++) {
3296 err = sn9c102_sensor_table[
i](cam);
3302 DBG(2,
"%s image sensor detected", cam->
sensor.name);
3303 DBG(3,
"Support for %s maintained by %s",
3306 DBG(1,
"No supported image sensor detected for this bridge");
3312 DBG(1,
"Bridge not supported");
3317 if (sn9c102_init(cam)) {
3318 DBG(1,
"Initialization failed. I will retry on open().");
3323 cam->
v4ldev->fops = &sn9c102_fops;
3325 cam->
v4ldev->parent = &udev->dev;
3327 init_completion(&cam->
probe);
3332 DBG(1,
"V4L2 device registration failed");
3333 if (err == -
ENFILE && video_nr[dev_nr] == -1)
3334 DBG(1,
"Free /dev/videoX node not found");
3341 DBG(2,
"V4L2 device registered as %s",
3342 video_device_node_name(cam->
v4ldev));
3344 video_set_drvdata(cam->
v4ldev, cam);
3350 #ifdef CONFIG_VIDEO_ADV_DEBUG
3351 err = sn9c102_create_sysfs(cam);
3353 DBG(2,
"Optional device control through 'sysfs' "
3356 DBG(2,
"Failed to create optional 'sysfs' interface for "
3357 "device controlling. Error #%d", err);
3359 DBG(2,
"Optional device control through 'sysfs' interface disabled");
3360 DBG(3,
"Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
3361 "configuration option to enable it.");
3364 usb_set_intfdata(intf, cam);
3365 kref_init(&cam->
kref);
3383 static void sn9c102_usb_disconnect(
struct usb_interface* intf)
3389 cam = usb_get_intfdata(intf);
3391 DBG(2,
"Disconnecting %s...", cam->
v4ldev->name);
3394 DBG(2,
"Device %s is open! Deregistration and memory "
3395 "deallocation are deferred.",
3396 video_device_node_name(cam->
v4ldev));
3398 sn9c102_stop_transfer(cam);
3407 kref_put(&cam->
kref, sn9c102_release_resources);
3413 static struct usb_driver sn9c102_usb_driver = {
3415 .id_table = sn9c102_id_table,
3416 .probe = sn9c102_usb_probe,
3417 .disconnect = sn9c102_usb_disconnect,