1 /***************************************************************************
2 * V4L2 driver for SN9C10[12] 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/time.h>
32 #include <linux/delay.h>
33 #include <linux/stddef.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 static unsigned int nv;
56 module_param_array(video_nr, short, nv, 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."
70 static unsigned short debug = SN9C102_DEBUG_LEVEL;
71 module_param(debug, ushort, 0644);
72 MODULE_PARM_DESC(debug,
73 "\n<n> Debugging information level, from 0 to 3:"
74 "\n0 = none (use carefully)"
75 "\n1 = critical errors"
76 "\n2 = significant informations"
77 "\n3 = more verbose messages"
78 "\nLevel 3 is useful for testing only, when only "
80 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
84 /*****************************************************************************/
86 typedef char sn9c102_sof_header_t[7];
87 typedef char sn9c102_eof_header_t[4];
89 static sn9c102_sof_header_t sn9c102_sof_header[] = {
90 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x00},
91 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01},
94 /* Number of random bytes that complete the SOF above headers */
95 #define SN9C102_SOFLEN 5
97 static sn9c102_eof_header_t sn9c102_eof_header[] = {
98 {0x00, 0x00, 0x00, 0x00},
99 {0x40, 0x00, 0x00, 0x00},
100 {0x80, 0x00, 0x00, 0x00},
101 {0xc0, 0x00, 0x00, 0x00},
104 /*****************************************************************************/
106 static inline unsigned long kvirt_to_pa(unsigned long adr)
108 unsigned long kva, ret;
110 kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
111 kva |= adr & (PAGE_SIZE-1);
117 static void* rvmalloc(size_t size)
122 size = PAGE_ALIGN(size);
124 mem = vmalloc_32((unsigned long)size);
128 memset(mem, 0, size);
130 adr = (unsigned long)mem;
132 SetPageReserved(vmalloc_to_page((void *)adr));
141 static void rvfree(void* mem, size_t size)
148 size = PAGE_ALIGN(size);
150 adr = (unsigned long)mem;
152 ClearPageReserved(vmalloc_to_page((void *)adr));
161 static u32 sn9c102_request_buffers(struct sn9c102_device* cam, u32 count)
163 struct v4l2_pix_format* p = &(cam->sensor->pix_format);
164 const size_t imagesize = (p->width * p->height * p->priv)/8;
168 if (count > SN9C102_MAX_FRAMES)
169 count = SN9C102_MAX_FRAMES;
171 cam->nbuffers = count;
172 while (cam->nbuffers > 0) {
173 if ((buff = rvmalloc(cam->nbuffers * imagesize)))
178 for (i = 0; i < cam->nbuffers; i++) {
179 cam->frame[i].bufmem = buff + i*imagesize;
180 cam->frame[i].buf.index = i;
181 cam->frame[i].buf.m.offset = i*imagesize;
182 cam->frame[i].buf.length = imagesize;
183 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
184 cam->frame[i].buf.sequence = 0;
185 cam->frame[i].buf.field = V4L2_FIELD_NONE;
186 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
187 cam->frame[i].buf.flags = 0;
190 return cam->nbuffers;
194 static void sn9c102_release_buffers(struct sn9c102_device* cam)
197 rvfree(cam->frame[0].bufmem,
198 cam->nbuffers * cam->frame[0].buf.length);
204 static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
208 INIT_LIST_HEAD(&cam->inqueue);
209 INIT_LIST_HEAD(&cam->outqueue);
211 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
212 cam->frame[i].state = F_UNUSED;
213 cam->frame[i].buf.bytesused = 0;
218 static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
220 unsigned long lock_flags;
223 for (i = 0; i < cam->nbuffers; i++)
224 if (cam->frame[i].state == F_UNUSED) {
225 cam->frame[i].state = F_QUEUED;
226 spin_lock_irqsave(&cam->queue_lock, lock_flags);
227 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
228 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
232 /*****************************************************************************/
234 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
236 struct usb_device* udev = cam->usbdev;
237 u8* buff = cam->control_buffer;
241 value = (value & 0xcf) | (cam->reg[0x18] & 0x30);
245 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
246 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
248 DBG(3, "Failed to write a register (value 0x%02X, index "
249 "0x%02X, error %d)", value, index, res)
253 cam->reg[index] = value;
259 /* NOTE: reading some registers always returns 0 */
260 static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
262 struct usb_device* udev = cam->usbdev;
263 u8* buff = cam->control_buffer;
266 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
267 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
269 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
272 return (res >= 0) ? (int)(*buff) : -1;
276 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
281 return cam->reg[index];
286 sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
290 for (i = 1; i <= 5; i++) {
291 r = sn9c102_read_reg(cam, 0x08);
296 if (sensor->frequency & SN9C102_I2C_400KHZ)
306 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
307 struct sn9c102_sensor* sensor)
310 r = sn9c102_read_reg(cam, 0x08);
311 return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0;
316 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
317 struct sn9c102_sensor* sensor)
320 r = sn9c102_read_reg(cam, 0x08);
321 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
326 sn9c102_i2c_try_read(struct sn9c102_device* cam,
327 struct sn9c102_sensor* sensor, u8 address)
329 struct usb_device* udev = cam->usbdev;
330 u8* data = cam->control_buffer;
333 /* Write cycle - address */
334 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
335 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
336 data[1] = sensor->slave_write_id;
339 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
340 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
344 err += sn9c102_i2c_wait(cam, sensor);
346 /* Read cycle - 1 byte */
347 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
348 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
350 data[1] = sensor->slave_read_id;
352 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
353 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
357 err += sn9c102_i2c_wait(cam, sensor);
359 /* The read byte will be placed in data[4] */
360 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
361 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
365 err += sn9c102_i2c_detect_read_error(cam, sensor);
368 DBG(3, "I2C read failed for %s image sensor", sensor->name)
370 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[4])
372 return err ? -1 : (int)data[4];
377 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
378 struct sn9c102_sensor* sensor, u8 n, u8 data0,
379 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
381 struct usb_device* udev = cam->usbdev;
382 u8* data = cam->control_buffer;
385 /* Write cycle. It usually is address + value */
386 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
387 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
396 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
397 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
401 err += sn9c102_i2c_wait(cam, sensor);
402 err += sn9c102_i2c_detect_write_error(cam, sensor);
405 DBG(3, "I2C write failed for %s image sensor", sensor->name)
407 PDBGG("I2C write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
408 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
409 n, data0, data1, data2, data3, data4, data5)
416 sn9c102_i2c_try_write(struct sn9c102_device* cam,
417 struct sn9c102_sensor* sensor, u8 address, u8 value)
419 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
420 sensor->slave_write_id, address,
425 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
430 return sn9c102_i2c_try_read(cam, cam->sensor, address);
434 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
439 return sn9c102_i2c_try_write(cam, cam->sensor, address, value);
442 /*****************************************************************************/
444 static void* sn9c102_find_sof_header(void* mem, size_t len)
446 size_t soflen=sizeof(sn9c102_sof_header_t), SOFLEN=SN9C102_SOFLEN, i;
447 u8 j, n = sizeof(sn9c102_sof_header) / soflen;
449 for (i = 0; (len >= soflen+SOFLEN) && (i <= len-soflen-SOFLEN); i++)
450 for (j = 0; j < n; j++)
451 if (!memcmp(mem + i, sn9c102_sof_header[j], soflen))
452 /* Skips the header */
453 return mem + i + soflen + SOFLEN;
459 static void* sn9c102_find_eof_header(void* mem, size_t len)
461 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
462 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
464 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
465 for (j = 0; j < n; j++)
466 if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen))
473 static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
475 struct sn9c102_device* cam = urb->context;
476 struct sn9c102_frame_t** f;
477 unsigned long lock_flags;
481 if (urb->status == -ENOENT)
484 f = &cam->frame_current;
486 if (cam->stream == STREAM_INTERRUPT) {
487 cam->stream = STREAM_OFF;
489 (*f)->state = F_QUEUED;
490 DBG(3, "Stream interrupted")
491 wake_up_interruptible(&cam->wait_stream);
494 if ((cam->state & DEV_DISCONNECTED)||(cam->state & DEV_MISCONFIGURED))
497 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
501 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
504 for (i = 0; i < urb->number_of_packets; i++) {
505 unsigned int img, len, status;
506 void *pos, *sof, *eof;
508 len = urb->iso_frame_desc[i].actual_length;
509 status = urb->iso_frame_desc[i].status;
510 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
513 DBG(3, "Error in isochronous frame")
514 (*f)->state = F_ERROR;
518 PDBGG("Isochrnous frame: length %u, #%u i", len, i)
520 /* NOTE: It is probably correct to assume that SOF and EOF
521 headers do not occur between two consecutive packets,
522 but who knows..Whatever is the truth, this assumption
523 doesn't introduce bugs. */
526 sof = sn9c102_find_sof_header(pos, len);
528 eof = sn9c102_find_eof_header(pos, len);
529 if ((*f)->state == F_GRABBING) {
534 img = (eof > pos) ? eof - pos - 1 : 0;
536 if ((*f)->buf.bytesused+img>(*f)->buf.length) {
537 u32 b = (*f)->buf.bytesused + img -
539 img = (*f)->buf.length -
541 DBG(3, "Expected EOF not found: "
544 DBG(3, "Exceeded limit: +%u "
545 "bytes", (unsigned)(b))
548 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
551 if ((*f)->buf.bytesused == 0)
552 do_gettimeofday(&(*f)->buf.timestamp);
554 (*f)->buf.bytesused += img;
556 if ((*f)->buf.bytesused == (*f)->buf.length) {
557 u32 b = (*f)->buf.bytesused;
558 (*f)->state = F_DONE;
559 (*f)->buf.sequence= ++cam->frame_count;
560 spin_lock_irqsave(&cam->queue_lock,
562 list_move_tail(&(*f)->frame,
564 if (!list_empty(&cam->inqueue))
567 struct sn9c102_frame_t,
571 spin_unlock_irqrestore(&cam->queue_lock
573 DBG(3, "Video frame captured: "
574 "%lu bytes", (unsigned long)(b))
580 (*f)->state = F_ERROR;
581 DBG(3, "Not expected EOF after %lu "
582 "bytes of image data",
583 (unsigned long)((*f)->buf.bytesused))
590 DBG(3, "EOF without SOF")
594 PDBGG("Ignoring pointless isochronous frame")
598 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
600 (*f)->state = F_GRABBING;
601 (*f)->buf.bytesused = 0;
604 DBG(3, "SOF detected: new video frame")
608 } else if ((*f)->state == F_GRABBING) {
609 eof = sn9c102_find_eof_header(pos, len);
610 if (eof && eof < sof)
611 goto end_of_frame; /* (1) */
613 DBG(3, "SOF before expected EOF after %lu "
614 "bytes of image data",
615 (unsigned long)((*f)->buf.bytesused))
622 urb->dev = cam->usbdev;
623 err = usb_submit_urb(urb, GFP_ATOMIC);
624 if (err < 0 && err != -EPERM) {
625 cam->state |= DEV_MISCONFIGURED;
626 DBG(1, "usb_submit_urb() failed")
629 wake_up_interruptible(&cam->wait_frame);
633 static int sn9c102_start_transfer(struct sn9c102_device* cam)
635 struct usb_device *udev = cam->usbdev;
637 const unsigned int wMaxPacketSize[] = {0, 128, 256, 384, 512,
638 680, 800, 900, 1023};
639 const unsigned int psz = wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
643 for (i = 0; i < SN9C102_URBS; i++) {
644 cam->transfer_buffer[i] = kmalloc(SN9C102_ISO_PACKETS * psz,
646 if (!cam->transfer_buffer[i]) {
648 DBG(1, "Not enough memory")
653 for (i = 0; i < SN9C102_URBS; i++) {
654 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
658 DBG(1, "usb_alloc_urb() failed")
663 urb->pipe = usb_rcvisocpipe(udev, 1);
664 urb->transfer_flags = URB_ISO_ASAP;
665 urb->number_of_packets = SN9C102_ISO_PACKETS;
666 urb->complete = sn9c102_urb_complete;
667 urb->transfer_buffer = cam->transfer_buffer[i];
668 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
670 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
671 urb->iso_frame_desc[j].offset = psz * j;
672 urb->iso_frame_desc[j].length = psz;
677 if (!(cam->reg[0x01] & 0x04)) {
678 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
681 DBG(1, "I/O hardware error")
686 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
688 DBG(1, "usb_set_interface() failed")
692 cam->frame_current = NULL;
694 for (i = 0; i < SN9C102_URBS; i++) {
695 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
697 for (j = i-1; j >= 0; j--)
698 usb_kill_urb(cam->urb[j]);
699 DBG(1, "usb_submit_urb() failed, error %d", err)
707 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
708 usb_free_urb(cam->urb[i]);
711 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
712 kfree(cam->transfer_buffer[i]);
718 static int sn9c102_stop_transfer(struct sn9c102_device* cam)
720 struct usb_device *udev = cam->usbdev;
724 if (cam->state & DEV_DISCONNECTED)
727 for (i = SN9C102_URBS-1; i >= 0; i--) {
728 usb_kill_urb(cam->urb[i]);
729 usb_free_urb(cam->urb[i]);
730 kfree(cam->transfer_buffer[i]);
733 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
735 DBG(3, "usb_set_interface() failed")
740 /*****************************************************************************/
742 static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
749 strncpy(str, buff, len);
752 strncpy(str, buff, 4);
756 val = simple_strtoul(str, &endp, 0);
760 *count = (ssize_t)(endp - str);
761 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
767 /* NOTE 1: being inside one of the following methods implies that the v4l
768 device exists for sure (see kobjects and reference counters)
769 NOTE 2: buffers are PAGE_SIZE long */
771 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
773 struct sn9c102_device* cam;
776 if (down_interruptible(&sn9c102_sysfs_lock))
779 cam = video_get_drvdata(to_video_device(cd));
781 up(&sn9c102_sysfs_lock);
785 count = sprintf(buf, "%u\n", cam->sysfs.reg);
787 up(&sn9c102_sysfs_lock);
794 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
796 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 index = sn9c102_strtou8(buf, len, &count);
810 if (index > 0x1f || !count) {
811 up(&sn9c102_sysfs_lock);
815 cam->sysfs.reg = index;
817 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg)
818 DBG(3, "Written bytes: %zd", count)
820 up(&sn9c102_sysfs_lock);
826 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
828 struct sn9c102_device* cam;
832 if (down_interruptible(&sn9c102_sysfs_lock))
835 cam = video_get_drvdata(to_video_device(cd));
837 up(&sn9c102_sysfs_lock);
841 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
842 up(&sn9c102_sysfs_lock);
846 count = sprintf(buf, "%d\n", val);
848 DBG(3, "Read bytes: %zd", count)
850 up(&sn9c102_sysfs_lock);
857 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
859 struct sn9c102_device* cam;
864 if (down_interruptible(&sn9c102_sysfs_lock))
867 cam = video_get_drvdata(to_video_device(cd));
869 up(&sn9c102_sysfs_lock);
873 value = sn9c102_strtou8(buf, len, &count);
875 up(&sn9c102_sysfs_lock);
879 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
881 up(&sn9c102_sysfs_lock);
885 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
886 cam->sysfs.reg, value)
887 DBG(3, "Written bytes: %zd", count)
889 up(&sn9c102_sysfs_lock);
895 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
897 struct sn9c102_device* cam;
900 if (down_interruptible(&sn9c102_sysfs_lock))
903 cam = video_get_drvdata(to_video_device(cd));
905 up(&sn9c102_sysfs_lock);
909 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
911 DBG(3, "Read bytes: %zd", count)
913 up(&sn9c102_sysfs_lock);
920 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
922 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 index = sn9c102_strtou8(buf, len, &count);
937 up(&sn9c102_sysfs_lock);
941 cam->sysfs.i2c_reg = index;
943 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg)
944 DBG(3, "Written bytes: %zd", count)
946 up(&sn9c102_sysfs_lock);
952 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
954 struct sn9c102_device* cam;
958 if (down_interruptible(&sn9c102_sysfs_lock))
961 cam = video_get_drvdata(to_video_device(cd));
963 up(&sn9c102_sysfs_lock);
967 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
968 up(&sn9c102_sysfs_lock);
972 count = sprintf(buf, "%d\n", val);
974 DBG(3, "Read bytes: %zd", count)
976 up(&sn9c102_sysfs_lock);
983 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
985 struct sn9c102_device* cam;
990 if (down_interruptible(&sn9c102_sysfs_lock))
993 cam = video_get_drvdata(to_video_device(cd));
995 up(&sn9c102_sysfs_lock);
999 value = sn9c102_strtou8(buf, len, &count);
1001 up(&sn9c102_sysfs_lock);
1005 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1007 up(&sn9c102_sysfs_lock);
1011 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1012 cam->sysfs.i2c_reg, value)
1013 DBG(3, "Written bytes: %zd", count)
1015 up(&sn9c102_sysfs_lock);
1022 sn9c102_store_redblue(struct class_device* cd, const char* buf, size_t len)
1028 value = sn9c102_strtou8(buf, len, &count);
1032 if ((res = sn9c102_store_reg(cd, "0x10", 4)) >= 0)
1033 res = sn9c102_store_val(cd, buf, len);
1040 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1046 value = sn9c102_strtou8(buf, len, &count);
1047 if (!count || value > 0x0f)
1050 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1051 res = sn9c102_store_val(cd, buf, len);
1057 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1058 sn9c102_show_reg, sn9c102_store_reg);
1059 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1060 sn9c102_show_val, sn9c102_store_val);
1061 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1062 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1063 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1064 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1065 static CLASS_DEVICE_ATTR(redblue, S_IWUGO, NULL, sn9c102_store_redblue);
1066 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1069 static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1071 struct video_device *v4ldev = cam->v4ldev;
1073 video_device_create_file(v4ldev, &class_device_attr_reg);
1074 video_device_create_file(v4ldev, &class_device_attr_val);
1075 video_device_create_file(v4ldev, &class_device_attr_redblue);
1076 video_device_create_file(v4ldev, &class_device_attr_green);
1077 if (cam->sensor->slave_write_id && cam->sensor->slave_read_id) {
1078 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1079 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1083 /*****************************************************************************/
1085 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1091 r = cam->reg[0x18] & 0xcf;
1092 else if (scale == 2) {
1093 r = cam->reg[0x18] & 0xcf;
1095 } else if (scale == 4)
1096 r = cam->reg[0x18] | 0x20;
1098 err += sn9c102_write_reg(cam, r, 0x18);
1102 PDBGG("Scaling factor: %u", scale)
1108 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1110 struct sn9c102_sensor* s = cam->sensor;
1111 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1112 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1113 h_size = (u8)(rect->width / 16),
1114 v_size = (u8)(rect->height / 16),
1117 ae_endx = h_size / 2,
1118 ae_endy = v_size / 2;
1121 /* These are a sort of stroboscopic signal for some sensors */
1122 err += sn9c102_write_reg(cam, h_size, 0x1a);
1123 err += sn9c102_write_reg(cam, v_size, 0x1b);
1125 err += sn9c102_write_reg(cam, h_start, 0x12);
1126 err += sn9c102_write_reg(cam, v_start, 0x13);
1127 err += sn9c102_write_reg(cam, h_size, 0x15);
1128 err += sn9c102_write_reg(cam, v_size, 0x16);
1129 err += sn9c102_write_reg(cam, ae_strx, 0x1c);
1130 err += sn9c102_write_reg(cam, ae_stry, 0x1d);
1131 err += sn9c102_write_reg(cam, ae_endx, 0x1e);
1132 err += sn9c102_write_reg(cam, ae_endy, 0x1f);
1136 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1137 "%u %u %u %u %u %u", h_start, v_start, h_size, v_size, ho_size,
1144 static int sn9c102_init(struct sn9c102_device* cam)
1146 struct sn9c102_sensor* s = cam->sensor;
1147 struct v4l2_control ctrl;
1148 struct v4l2_queryctrl *qctrl;
1149 struct v4l2_rect* rect;
1153 if (!(cam->state & DEV_INITIALIZED)) {
1154 init_waitqueue_head(&cam->open);
1156 rect = &(s->cropcap.defrect);
1157 } else { /* use current values */
1162 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1163 err += sn9c102_set_crop(cam, rect);
1170 DBG(3, "Sensor initialization failed")
1176 if ((err = s->set_crop(cam, rect))) {
1177 DBG(3, "set_crop() failed")
1182 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1183 for (i = 0; i < n; i++)
1184 if (s->qctrl[i].id != 0 &&
1185 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1186 ctrl.id = s->qctrl[i].id;
1187 ctrl.value = qctrl[i].default_value;
1188 err = s->set_ctrl(cam, &ctrl);
1190 DBG(3, "Set control failed")
1196 if (!(cam->state & DEV_INITIALIZED)) {
1197 init_MUTEX(&cam->fileop_sem);
1198 spin_lock_init(&cam->queue_lock);
1199 init_waitqueue_head(&cam->wait_frame);
1200 init_waitqueue_head(&cam->wait_stream);
1201 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1202 memcpy(&(s->_rect), &(s->cropcap.defrect),
1203 sizeof(struct v4l2_rect));
1204 cam->state |= DEV_INITIALIZED;
1207 DBG(2, "Initialization succeeded")
1212 static void sn9c102_release_resources(struct sn9c102_device* cam)
1214 down(&sn9c102_sysfs_lock);
1216 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor)
1217 video_set_drvdata(cam->v4ldev, NULL);
1218 video_unregister_device(cam->v4ldev);
1220 up(&sn9c102_sysfs_lock);
1222 kfree(cam->control_buffer);
1225 /*****************************************************************************/
1227 static int sn9c102_open(struct inode* inode, struct file* filp)
1229 struct sn9c102_device* cam;
1232 /* This the only safe way to prevent race conditions with disconnect */
1233 if (!down_read_trylock(&sn9c102_disconnect))
1234 return -ERESTARTSYS;
1236 cam = video_get_drvdata(video_devdata(filp));
1238 if (down_interruptible(&cam->dev_sem)) {
1239 up_read(&sn9c102_disconnect);
1240 return -ERESTARTSYS;
1244 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor)
1245 if ((filp->f_flags & O_NONBLOCK) ||
1246 (filp->f_flags & O_NDELAY)) {
1251 err = wait_event_interruptible_exclusive(cam->open,
1252 cam->state & DEV_DISCONNECTED
1255 up_read(&sn9c102_disconnect);
1258 if (cam->state & DEV_DISCONNECTED) {
1259 up_read(&sn9c102_disconnect);
1262 down(&cam->dev_sem);
1266 if (cam->state & DEV_MISCONFIGURED) {
1267 err = sn9c102_init(cam);
1269 DBG(1, "Initialization failed again. "
1270 "I will retry on next open().")
1273 cam->state &= ~DEV_MISCONFIGURED;
1276 if ((err = sn9c102_start_transfer(cam)))
1279 filp->private_data = cam;
1282 cam->stream = STREAM_OFF;
1284 cam->frame_count = 0;
1285 sn9c102_empty_framequeues(cam);
1287 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor)
1291 up_read(&sn9c102_disconnect);
1296 static int sn9c102_release(struct inode* inode, struct file* filp)
1298 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1300 down(&cam->dev_sem); /* prevent disconnect() to be called */
1302 sn9c102_stop_transfer(cam);
1304 sn9c102_release_buffers(cam);
1306 if (cam->state & DEV_DISCONNECTED) {
1307 sn9c102_release_resources(cam);
1314 wake_up_interruptible_nr(&cam->open, 1);
1316 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor)
1325 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1327 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1328 struct sn9c102_frame_t* f, * i;
1329 unsigned long lock_flags;
1332 if (down_interruptible(&cam->fileop_sem))
1333 return -ERESTARTSYS;
1335 if (cam->state & DEV_DISCONNECTED) {
1336 DBG(1, "Device not present")
1337 up(&cam->fileop_sem);
1341 if (cam->state & DEV_MISCONFIGURED) {
1342 DBG(1, "The camera is misconfigured. Close and open it again.")
1343 up(&cam->fileop_sem);
1347 if (cam->io == IO_MMAP) {
1348 DBG(3, "Close and open the device again to choose "
1350 up(&cam->fileop_sem);
1354 if (cam->io == IO_NONE) {
1355 if (!sn9c102_request_buffers(cam, 2)) {
1356 DBG(1, "read() failed, not enough memory")
1357 up(&cam->fileop_sem);
1361 cam->stream = STREAM_ON;
1362 sn9c102_queue_unusedframes(cam);
1366 up(&cam->fileop_sem);
1370 if (list_empty(&cam->outqueue)) {
1371 if (filp->f_flags & O_NONBLOCK) {
1372 up(&cam->fileop_sem);
1375 err = wait_event_interruptible
1377 (!list_empty(&cam->outqueue)) ||
1378 (cam->state & DEV_DISCONNECTED) );
1380 up(&cam->fileop_sem);
1383 if (cam->state & DEV_DISCONNECTED) {
1384 up(&cam->fileop_sem);
1389 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1391 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1392 list_for_each_entry(i, &cam->outqueue, frame)
1393 i->state = F_UNUSED;
1394 INIT_LIST_HEAD(&cam->outqueue);
1395 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1397 sn9c102_queue_unusedframes(cam);
1399 if (count > f->buf.length)
1400 count = f->buf.length;
1402 if (copy_to_user(buf, f->bufmem, count)) {
1403 up(&cam->fileop_sem);
1408 PDBGG("Frame #%lu, bytes read: %zu", (unsigned long)f->buf.index,count)
1410 up(&cam->fileop_sem);
1416 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1418 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1419 unsigned int mask = 0;
1421 if (down_interruptible(&cam->fileop_sem))
1424 if (cam->state & DEV_DISCONNECTED) {
1425 DBG(1, "Device not present")
1429 if (cam->state & DEV_MISCONFIGURED) {
1430 DBG(1, "The camera is misconfigured. Close and open it again.")
1434 if (cam->io == IO_NONE) {
1435 if (!sn9c102_request_buffers(cam, 2)) {
1436 DBG(1, "poll() failed, not enough memory")
1440 cam->stream = STREAM_ON;
1443 if (cam->io == IO_READ)
1444 sn9c102_queue_unusedframes(cam);
1446 poll_wait(filp, &cam->wait_frame, wait);
1448 if (!list_empty(&cam->outqueue))
1449 mask |= POLLIN | POLLRDNORM;
1451 up(&cam->fileop_sem);
1456 up(&cam->fileop_sem);
1461 static void sn9c102_vm_open(struct vm_area_struct* vma)
1463 struct sn9c102_frame_t* f = vma->vm_private_data;
1468 static void sn9c102_vm_close(struct vm_area_struct* vma)
1470 /* NOTE: buffers are not freed here */
1471 struct sn9c102_frame_t* f = vma->vm_private_data;
1476 static struct vm_operations_struct sn9c102_vm_ops = {
1477 .open = sn9c102_vm_open,
1478 .close = sn9c102_vm_close,
1482 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1484 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1485 unsigned long size = vma->vm_end - vma->vm_start,
1486 start = vma->vm_start,
1491 if (down_interruptible(&cam->fileop_sem))
1492 return -ERESTARTSYS;
1494 if (cam->state & DEV_DISCONNECTED) {
1495 DBG(1, "Device not present")
1496 up(&cam->fileop_sem);
1500 if (cam->state & DEV_MISCONFIGURED) {
1501 DBG(1, "The camera is misconfigured. Close and open it again.")
1502 up(&cam->fileop_sem);
1506 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1507 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1508 up(&cam->fileop_sem);
1512 for (i = 0; i < cam->nbuffers; i++) {
1513 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1516 if (i == cam->nbuffers) {
1517 up(&cam->fileop_sem);
1521 pos = (unsigned long)cam->frame[i].bufmem;
1522 while (size > 0) { /* size is page-aligned */
1523 page = kvirt_to_pa(pos);
1524 if (remap_page_range(vma, start, page, PAGE_SIZE,
1525 vma->vm_page_prot)) {
1526 up(&cam->fileop_sem);
1534 vma->vm_ops = &sn9c102_vm_ops;
1535 vma->vm_flags &= ~VM_IO; /* not I/O memory */
1536 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1537 vma->vm_private_data = &cam->frame[i];
1539 sn9c102_vm_open(vma);
1541 up(&cam->fileop_sem);
1547 static int sn9c102_v4l2_ioctl(struct inode* inode, struct file* filp,
1548 unsigned int cmd, void __user * arg)
1550 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1554 case VIDIOC_QUERYCAP:
1556 struct v4l2_capability cap = {
1557 .driver = "sn9c102",
1558 .version = SN9C102_MODULE_VERSION_CODE,
1559 .capabilities = V4L2_CAP_VIDEO_CAPTURE |
1560 V4L2_CAP_READWRITE |
1564 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1565 strlcpy(cap.bus_info, cam->dev.bus_id, sizeof(cap.bus_info));
1567 if (copy_to_user(arg, &cap, sizeof(cap)))
1573 case VIDIOC_ENUMINPUT:
1575 struct v4l2_input i;
1577 if (copy_from_user(&i, arg, sizeof(i)))
1583 memset(&i, 0, sizeof(i));
1584 strcpy(i.name, "USB");
1586 if (copy_to_user(arg, &i, sizeof(i)))
1592 case VIDIOC_G_INPUT:
1593 case VIDIOC_S_INPUT:
1597 if (copy_from_user(&index, arg, sizeof(index)))
1606 case VIDIOC_QUERYCTRL:
1608 struct sn9c102_sensor* s = cam->sensor;
1609 struct v4l2_queryctrl qc;
1612 if (copy_from_user(&qc, arg, sizeof(qc)))
1615 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1616 for (i = 0; i < n; i++)
1617 if (qc.id && qc.id == s->qctrl[i].id) {
1618 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1619 if (copy_to_user(arg, &qc, sizeof(qc)))
1629 struct sn9c102_sensor* s = cam->sensor;
1630 struct v4l2_control ctrl;
1636 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1639 err = s->get_ctrl(cam, &ctrl);
1641 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1649 struct sn9c102_sensor* s = cam->sensor;
1650 struct v4l2_control ctrl;
1657 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1660 if ((err = s->set_ctrl(cam, &ctrl)))
1663 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1664 for (i = 0; i < n; i++)
1665 if (ctrl.id == s->qctrl[i].id) {
1666 s->_qctrl[i].default_value = ctrl.value;
1673 case VIDIOC_CROPCAP:
1675 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1677 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1678 cc->pixelaspect.numerator = 1;
1679 cc->pixelaspect.denominator = 1;
1681 if (copy_to_user(arg, cc, sizeof(*cc)))
1689 struct sn9c102_sensor* s = cam->sensor;
1690 struct v4l2_crop crop = {
1691 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1694 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1696 if (copy_to_user(arg, &crop, sizeof(crop)))
1704 struct sn9c102_sensor* s = cam->sensor;
1705 struct v4l2_crop crop;
1706 struct v4l2_rect* rect;
1707 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1708 struct v4l2_pix_format* pix_format = &(s->pix_format);
1710 const enum sn9c102_stream_state stream = cam->stream;
1711 const u32 nbuffers = cam->nbuffers;
1715 if (copy_from_user(&crop, arg, sizeof(crop)))
1720 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1723 for (i = 0; i < cam->nbuffers; i++)
1724 if (cam->frame[i].vma_use_count) {
1725 DBG(3, "VIDIOC_S_CROP failed. "
1726 "Unmap the buffers first.")
1730 if (rect->width < 16)
1732 if (rect->height < 16)
1734 if (rect->width > bounds->width)
1735 rect->width = bounds->width;
1736 if (rect->height > bounds->height)
1737 rect->height = bounds->height;
1738 if (rect->left < bounds->left)
1739 rect->left = bounds->left;
1740 if (rect->top < bounds->top)
1741 rect->top = bounds->top;
1742 if (rect->left + rect->width > bounds->left + bounds->width)
1743 rect->left = bounds->left+bounds->width - rect->width;
1744 if (rect->top + rect->height > bounds->top + bounds->height)
1745 rect->top = bounds->top+bounds->height - rect->height;
1747 rect->width &= ~15L;
1748 rect->height &= ~15L;
1750 { /* calculate the scaling factor */
1752 a = rect->width * rect->height;
1753 b = pix_format->width * pix_format->height;
1754 scale = b ? (u8)((a / b) <= 1 ? 1 : ((a / b) == 3 ? 2 :
1755 ((a / b) > 4 ? 4 : (a / b)))) : 1;
1758 if (cam->stream == STREAM_ON) {
1759 cam->stream = STREAM_INTERRUPT;
1760 err = wait_event_interruptible
1762 (cam->stream == STREAM_OFF) ||
1763 (cam->state & DEV_DISCONNECTED) );
1765 cam->state |= DEV_MISCONFIGURED;
1766 DBG(1, "The camera is misconfigured. To use "
1767 "it, close and open /dev/video%d "
1768 "again.", cam->v4ldev->minor)
1771 if (cam->state & DEV_DISCONNECTED)
1775 if (copy_to_user(arg, &crop, sizeof(crop))) {
1776 cam->stream = stream;
1780 sn9c102_release_buffers(cam);
1782 err = sn9c102_set_crop(cam, rect);
1784 err += s->set_crop(cam, rect);
1785 err += sn9c102_set_scale(cam, scale);
1787 if (err) { /* atomic, no rollback in ioctl() */
1788 cam->state |= DEV_MISCONFIGURED;
1789 DBG(1, "VIDIOC_S_CROP failed because of hardware "
1790 "problems. To use the camera, close and open "
1791 "/dev/video%d again.", cam->v4ldev->minor)
1795 s->pix_format.width = rect->width/scale;
1796 s->pix_format.height = rect->height/scale;
1797 memcpy(&(s->_rect), rect, sizeof(*rect));
1799 if (nbuffers != sn9c102_request_buffers(cam, nbuffers)) {
1800 cam->state |= DEV_MISCONFIGURED;
1801 DBG(1, "VIDIOC_S_CROP failed because of not enough "
1802 "memory. To use the camera, close and open "
1803 "/dev/video%d again.", cam->v4ldev->minor)
1807 cam->stream = stream;
1812 case VIDIOC_ENUM_FMT:
1814 struct sn9c102_sensor* s = cam->sensor;
1815 struct v4l2_fmtdesc fmtd;
1817 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1820 if (fmtd.index != 0)
1823 memset(&fmtd, 0, sizeof(fmtd));
1825 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1826 strcpy(fmtd.description, "bayer rgb");
1827 fmtd.pixelformat = s->pix_format.pixelformat;
1829 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1837 struct v4l2_format format;
1838 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
1840 if (copy_from_user(&format, arg, sizeof(format)))
1843 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1846 pfmt->bytesperline = (pfmt->width * pfmt->priv) / 8;
1847 pfmt->sizeimage = pfmt->height * pfmt->bytesperline;
1848 pfmt->field = V4L2_FIELD_NONE;
1849 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1851 if (copy_to_user(arg, &format, sizeof(format)))
1857 case VIDIOC_TRY_FMT:
1860 struct sn9c102_sensor* s = cam->sensor;
1861 struct v4l2_format format;
1862 struct v4l2_pix_format* pix;
1863 struct v4l2_pix_format* pfmt = &(s->pix_format);
1864 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1865 struct v4l2_rect rect;
1867 const enum sn9c102_stream_state stream = cam->stream;
1868 const u32 nbuffers = cam->nbuffers;
1872 if (copy_from_user(&format, arg, sizeof(format)))
1875 pix = &(format.fmt.pix);
1877 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1880 memcpy(&rect, &(s->_rect), sizeof(rect));
1882 { /* calculate the scaling factor */
1884 a = rect.width * rect.height;
1885 b = pix->width * pix->height;
1886 scale = b ? (u8)((a / b) <= 1 ? 1 : ((a / b) == 3 ? 2 :
1887 ((a / b) > 4 ? 4 : (a / b)))) : 1;
1890 rect.width = scale * pix->width;
1891 rect.height = scale * pix->height;
1893 if (rect.width < 16)
1895 if (rect.height < 16)
1897 if (rect.width > bounds->left + bounds->width - rect.left)
1898 rect.width = bounds->left+bounds->width - rect.left;
1899 if (rect.height > bounds->top + bounds->height - rect.top)
1900 rect.height = bounds->top + bounds->height - rect.top;
1903 rect.height &= ~15L;
1905 pix->width = rect.width / scale;
1906 pix->height = rect.height / scale;
1908 pix->pixelformat = pfmt->pixelformat;
1909 pix->priv = pfmt->priv; /* bpp */
1910 pix->colorspace = pfmt->colorspace;
1911 pix->bytesperline = (pix->width * pix->priv) / 8;
1912 pix->sizeimage = pix->height * pix->bytesperline;
1913 pix->field = V4L2_FIELD_NONE;
1915 if (cmd == VIDIOC_TRY_FMT)
1918 for (i = 0; i < cam->nbuffers; i++)
1919 if (cam->frame[i].vma_use_count) {
1920 DBG(3, "VIDIOC_S_FMT failed. "
1921 "Unmap the buffers first.")
1925 if (cam->stream == STREAM_ON) {
1926 cam->stream = STREAM_INTERRUPT;
1927 err = wait_event_interruptible
1929 (cam->stream == STREAM_OFF) ||
1930 (cam->state & DEV_DISCONNECTED) );
1932 cam->state |= DEV_MISCONFIGURED;
1933 DBG(1, "The camera is misconfigured. To use "
1934 "it, close and open /dev/video%d "
1935 "again.", cam->v4ldev->minor)
1938 if (cam->state & DEV_DISCONNECTED)
1942 if (copy_to_user(arg, &format, sizeof(format))) {
1943 cam->stream = stream;
1947 sn9c102_release_buffers(cam);
1949 err = sn9c102_set_crop(cam, &rect);
1951 err += s->set_crop(cam, &rect);
1952 err += sn9c102_set_scale(cam, scale);
1954 if (err) { /* atomic, no rollback in ioctl() */
1955 cam->state |= DEV_MISCONFIGURED;
1956 DBG(1, "VIDIOC_S_FMT failed because of hardware "
1957 "problems. To use the camera, close and open "
1958 "/dev/video%d again.", cam->v4ldev->minor)
1962 memcpy(pfmt, pix, sizeof(*pix));
1963 memcpy(&(s->_rect), &rect, sizeof(rect));
1965 if (nbuffers != sn9c102_request_buffers(cam, nbuffers)) {
1966 cam->state |= DEV_MISCONFIGURED;
1967 DBG(1, "VIDIOC_S_FMT failed because of not enough "
1968 "memory. To use the camera, close and open "
1969 "/dev/video%d again.", cam->v4ldev->minor)
1973 cam->stream = stream;
1978 case VIDIOC_REQBUFS:
1980 struct v4l2_requestbuffers rb;
1984 if (copy_from_user(&rb, arg, sizeof(rb)))
1987 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1988 rb.memory != V4L2_MEMORY_MMAP)
1991 if (cam->io == IO_READ) {
1992 DBG(3, "Close and open the device again to choose "
1993 "the mmap I/O method")
1997 for (i = 0; i < cam->nbuffers; i++)
1998 if (cam->frame[i].vma_use_count) {
1999 DBG(3, "VIDIOC_REQBUFS failed. "
2000 "Previous buffers are still mapped.")
2004 if (cam->stream == STREAM_ON) {
2005 cam->stream = STREAM_INTERRUPT;
2006 err = wait_event_interruptible
2008 (cam->stream == STREAM_OFF) ||
2009 (cam->state & DEV_DISCONNECTED) );
2011 cam->state |= DEV_MISCONFIGURED;
2012 DBG(1, "The camera is misconfigured. To use "
2013 "it, close and open /dev/video%d "
2014 "again.", cam->v4ldev->minor)
2017 if (cam->state & DEV_DISCONNECTED)
2021 sn9c102_empty_framequeues(cam);
2023 sn9c102_release_buffers(cam);
2025 rb.count = sn9c102_request_buffers(cam, rb.count);
2027 if (copy_to_user(arg, &rb, sizeof(rb))) {
2028 sn9c102_release_buffers(cam);
2033 cam->io = rb.count ? IO_MMAP : IO_NONE;
2038 case VIDIOC_QUERYBUF:
2040 struct v4l2_buffer b;
2042 if (copy_from_user(&b, arg, sizeof(b)))
2045 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2046 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2049 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2051 if (cam->frame[b.index].vma_use_count)
2052 b.flags |= V4L2_BUF_FLAG_MAPPED;
2054 if (cam->frame[b.index].state == F_DONE)
2055 b.flags |= V4L2_BUF_FLAG_DONE;
2056 else if (cam->frame[b.index].state != F_UNUSED)
2057 b.flags |= V4L2_BUF_FLAG_QUEUED;
2059 if (copy_to_user(arg, &b, sizeof(b)))
2067 struct v4l2_buffer b;
2068 unsigned long lock_flags;
2070 if (copy_from_user(&b, arg, sizeof(b)))
2073 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2074 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2077 if (cam->frame[b.index].state != F_UNUSED)
2080 cam->frame[b.index].state = F_QUEUED;
2082 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2083 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2084 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2086 PDBGG("Frame #%lu queued", (unsigned long)b.index)
2093 struct v4l2_buffer b;
2094 struct sn9c102_frame_t *f;
2095 unsigned long lock_flags;
2098 if (copy_from_user(&b, arg, sizeof(b)))
2101 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2104 if (list_empty(&cam->outqueue)) {
2105 if (cam->stream == STREAM_OFF)
2107 if (filp->f_flags & O_NONBLOCK)
2109 err = wait_event_interruptible
2111 (!list_empty(&cam->outqueue)) ||
2112 (cam->state & DEV_DISCONNECTED) );
2115 if (cam->state & DEV_DISCONNECTED)
2119 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2120 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t,
2122 list_del(&cam->outqueue);
2123 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2125 f->state = F_UNUSED;
2127 memcpy(&b, &f->buf, sizeof(b));
2128 if (f->vma_use_count)
2129 b.flags |= V4L2_BUF_FLAG_MAPPED;
2131 if (copy_to_user(arg, &b, sizeof(b)))
2134 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index)
2139 case VIDIOC_STREAMON:
2143 if (copy_from_user(&type, arg, sizeof(type)))
2146 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2149 if (list_empty(&cam->inqueue))
2152 cam->stream = STREAM_ON;
2159 case VIDIOC_STREAMOFF:
2163 if (copy_from_user(&type, arg, sizeof(type)))
2166 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2169 if (cam->stream == STREAM_ON) {
2170 cam->stream = STREAM_INTERRUPT;
2171 err = wait_event_interruptible
2173 (cam->stream == STREAM_OFF) ||
2174 (cam->state & DEV_DISCONNECTED) );
2176 cam->state |= DEV_MISCONFIGURED;
2177 DBG(1, "The camera is misconfigured. To use "
2178 "it, close and open /dev/video%d "
2179 "again.", cam->v4ldev->minor)
2182 if (cam->state & DEV_DISCONNECTED)
2186 sn9c102_empty_framequeues(cam);
2188 DBG(3, "Stream off")
2195 case VIDIOC_QUERYSTD:
2196 case VIDIOC_ENUMSTD:
2197 case VIDIOC_QUERYMENU:
2209 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2210 unsigned int cmd, unsigned long arg)
2212 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2215 if (down_interruptible(&cam->fileop_sem))
2216 return -ERESTARTSYS;
2218 if (cam->state & DEV_DISCONNECTED) {
2219 DBG(1, "Device not present")
2220 up(&cam->fileop_sem);
2224 if (cam->state & DEV_MISCONFIGURED) {
2225 DBG(1, "The camera is misconfigured. Close and open it again.")
2226 up(&cam->fileop_sem);
2230 err = sn9c102_v4l2_ioctl(inode, filp, cmd, (void __user *)arg);
2232 up(&cam->fileop_sem);
2238 static struct file_operations sn9c102_fops = {
2239 .owner = THIS_MODULE,
2240 .open = sn9c102_open,
2241 .release = sn9c102_release,
2242 .ioctl = sn9c102_ioctl,
2243 .read = sn9c102_read,
2244 .poll = sn9c102_poll,
2245 .mmap = sn9c102_mmap,
2246 .llseek = no_llseek,
2249 /*****************************************************************************/
2251 /* It exists a single interface only. We do not need to validate anything. */
2253 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2255 struct usb_device *udev = interface_to_usbdev(intf);
2256 struct sn9c102_device* cam;
2257 static unsigned int dev_nr = 0;
2261 n = sizeof(sn9c102_id_table)/sizeof(sn9c102_id_table[0]);
2262 for (i = 0; i < n-1; i++)
2263 if (udev->descriptor.idVendor==sn9c102_id_table[i].idVendor &&
2264 udev->descriptor.idProduct==sn9c102_id_table[i].idProduct)
2269 if (!(cam = kmalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2271 memset(cam, 0, sizeof(*cam));
2275 memcpy(&cam->dev, &udev->dev, sizeof(struct device));
2277 if (!(cam->control_buffer = kmalloc(8, GFP_KERNEL))) {
2278 DBG(1, "kmalloc() failed")
2282 memset(cam->control_buffer, 0, 8);
2284 if (!(cam->v4ldev = video_device_alloc())) {
2285 DBG(1, "video_device_alloc() failed")
2290 init_MUTEX(&cam->dev_sem);
2292 r = sn9c102_read_reg(cam, 0x00);
2293 if (r < 0 || r != 0x10) {
2294 DBG(1, "Sorry, this is not a SN9C10[12] based camera "
2295 "(vid/pid 0x%04X/0x%04X)",
2296 sn9c102_id_table[i].idVendor,sn9c102_id_table[i].idProduct)
2301 DBG(2, "SN9C10[12] PC Camera Controller detected "
2302 "(vid/pid 0x%04X/0x%04X)",
2303 sn9c102_id_table[i].idVendor, sn9c102_id_table[i].idProduct)
2305 for (i = 0; sn9c102_sensor_table[i]; i++) {
2306 err = sn9c102_sensor_table[i](cam);
2311 if (!err && cam->sensor) {
2312 DBG(2, "%s image sensor detected", cam->sensor->name)
2313 DBG(3, "Support for %s maintained by %s",
2314 cam->sensor->name, cam->sensor->maintainer)
2316 DBG(1, "No supported image sensor detected")
2321 if (sn9c102_init(cam)) {
2322 DBG(1, "Initialization failed. I will retry on open().")
2323 cam->state |= DEV_MISCONFIGURED;
2326 strcpy(cam->v4ldev->name, "SN9C10[12] PC Camera");
2327 cam->v4ldev->owner = THIS_MODULE;
2328 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2329 cam->v4ldev->hardware = VID_HARDWARE_SN9C102;
2330 cam->v4ldev->fops = &sn9c102_fops;
2331 cam->v4ldev->minor = video_nr[dev_nr];
2332 cam->v4ldev->release = video_device_release;
2333 video_set_drvdata(cam->v4ldev, cam);
2335 down(&cam->dev_sem);
2337 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2340 DBG(1, "V4L2 device registration failed")
2341 if (err == -ENFILE && video_nr[dev_nr] == -1)
2342 DBG(1, "Free /dev/videoX node not found")
2343 video_nr[dev_nr] = -1;
2344 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2349 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor)
2351 sn9c102_create_sysfs(cam);
2353 usb_set_intfdata(intf, cam);
2361 kfree(cam->control_buffer);
2363 video_device_release(cam->v4ldev);
2370 static void sn9c102_usb_disconnect(struct usb_interface* intf)
2372 struct sn9c102_device* cam = usb_get_intfdata(intf);
2377 down_write(&sn9c102_disconnect);
2379 down(&cam->dev_sem);
2381 DBG(2, "Disconnecting %s...", cam->v4ldev->name)
2383 wake_up_interruptible_all(&cam->open);
2386 DBG(2, "Device /dev/video%d is open! Deregistration and "
2387 "memory deallocation are deferred on close.",
2389 cam->state |= DEV_MISCONFIGURED;
2390 sn9c102_stop_transfer(cam);
2391 cam->state |= DEV_DISCONNECTED;
2392 wake_up_interruptible(&cam->wait_frame);
2393 wake_up_interruptible(&cam->wait_stream);
2395 cam->state |= DEV_DISCONNECTED;
2396 sn9c102_release_resources(cam);
2404 up_write(&sn9c102_disconnect);
2408 static struct usb_driver sn9c102_usb_driver = {
2409 .owner = THIS_MODULE,
2411 .id_table = sn9c102_id_table,
2412 .probe = sn9c102_usb_probe,
2413 .disconnect = sn9c102_usb_disconnect,
2416 /*****************************************************************************/
2418 static int __init sn9c102_module_init(void)
2422 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION)
2423 KDBG(3, SN9C102_MODULE_AUTHOR)
2425 if ((err = usb_register(&sn9c102_usb_driver)))
2426 KDBG(1, "usb_register() failed")
2432 static void __exit sn9c102_module_exit(void)
2434 usb_deregister(&sn9c102_usb_driver);
2438 module_init(sn9c102_module_init);
2439 module_exit(sn9c102_module_exit);