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/compiler.h>
35 #include <linux/ioctl.h>
36 #include <linux/poll.h>
37 #include <linux/stat.h>
39 #include <linux/vmalloc.h>
40 #include <linux/page-flags.h>
42 #include <asm/uaccess.h>
46 /*****************************************************************************/
48 MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
50 MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
51 MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
52 MODULE_VERSION(SN9C102_MODULE_VERSION);
53 MODULE_LICENSE(SN9C102_MODULE_LICENSE);
55 static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
56 static unsigned int nv;
57 module_param_array(video_nr, short, nv, 0444);
58 MODULE_PARM_DESC(video_nr,
59 "\n<-1|n[,...]> Specify V4L2 minor mode number."
60 "\n -1 = use next available (default)"
61 "\n n = use minor number n (integer >= 0)"
62 "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
65 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
66 "\nthe second camera and use auto for the first"
67 "\none and for every other camera."
71 static unsigned short debug = SN9C102_DEBUG_LEVEL;
72 module_param(debug, ushort, 0644);
73 MODULE_PARM_DESC(debug,
74 "\n<n> Debugging information level, from 0 to 3:"
75 "\n0 = none (use carefully)"
76 "\n1 = critical errors"
77 "\n2 = significant informations"
78 "\n3 = more verbose messages"
79 "\nLevel 3 is useful for testing only, when only "
81 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
85 /*****************************************************************************/
87 typedef char sn9c102_sof_header_t[12];
88 typedef char sn9c102_eof_header_t[4];
90 static sn9c102_sof_header_t sn9c102_sof_header[] = {
91 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x00},
92 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01},
96 static sn9c102_eof_header_t sn9c102_eof_header[] = {
97 {0x00, 0x00, 0x00, 0x00},
98 {0x40, 0x00, 0x00, 0x00},
99 {0x80, 0x00, 0x00, 0x00},
100 {0xc0, 0x00, 0x00, 0x00},
103 /*****************************************************************************/
105 static inline unsigned long kvirt_to_pa(unsigned long adr)
107 unsigned long kva, ret;
109 kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
110 kva |= adr & (PAGE_SIZE-1);
116 static void* rvmalloc(size_t size)
121 size = PAGE_ALIGN(size);
123 mem = vmalloc_32((unsigned long)size);
127 memset(mem, 0, size);
129 adr = (unsigned long)mem;
131 SetPageReserved(vmalloc_to_page((void *)adr));
140 static void rvfree(void* mem, size_t size)
147 size = PAGE_ALIGN(size);
149 adr = (unsigned long)mem;
151 ClearPageReserved(vmalloc_to_page((void *)adr));
160 static u32 sn9c102_request_buffers(struct sn9c102_device* cam, u32 count)
162 struct v4l2_pix_format* p = &(cam->sensor->pix_format);
163 const size_t imagesize = (p->width * p->height * p->priv)/8;
167 if (count > SN9C102_MAX_FRAMES)
168 count = SN9C102_MAX_FRAMES;
170 cam->nbuffers = count;
171 while (cam->nbuffers > 0) {
172 if ((buff = rvmalloc(cam->nbuffers * imagesize)))
177 for (i = 0; i < cam->nbuffers; i++) {
178 cam->frame[i].bufmem = buff + i*imagesize;
179 cam->frame[i].buf.index = i;
180 cam->frame[i].buf.m.offset = i*imagesize;
181 cam->frame[i].buf.length = imagesize;
182 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
183 cam->frame[i].buf.sequence = 0;
184 cam->frame[i].buf.field = V4L2_FIELD_NONE;
185 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
186 cam->frame[i].buf.flags = 0;
189 return cam->nbuffers;
193 static void sn9c102_release_buffers(struct sn9c102_device* cam)
196 rvfree(cam->frame[0].bufmem,
197 cam->nbuffers * cam->frame[0].buf.length);
203 static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
207 INIT_LIST_HEAD(&cam->inqueue);
208 INIT_LIST_HEAD(&cam->outqueue);
210 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
211 cam->frame[i].state = F_UNUSED;
212 cam->frame[i].buf.bytesused = 0;
217 static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
219 unsigned long lock_flags;
222 for (i = 0; i < cam->nbuffers; i++)
223 if (cam->frame[i].state == F_UNUSED) {
224 cam->frame[i].state = F_QUEUED;
225 spin_lock_irqsave(&cam->queue_lock, lock_flags);
226 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
227 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
231 /*****************************************************************************/
233 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
235 struct usb_device* udev = cam->usbdev;
236 u8* buff = cam->control_buffer;
241 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
242 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
244 DBG(3, "Failed to write a register (value 0x%02X, index "
245 "0x%02X, error %d)", value, index, res)
249 cam->reg[index] = value;
255 /* NOTE: reading some registers always returns 0 */
256 static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
258 struct usb_device* udev = cam->usbdev;
259 u8* buff = cam->control_buffer;
262 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
263 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
265 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
268 return (res >= 0) ? (int)(*buff) : -1;
272 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
277 return cam->reg[index];
282 sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
286 for (i = 1; i <= 5; i++) {
287 r = sn9c102_read_reg(cam, 0x08);
292 if (sensor->frequency & SN9C102_I2C_400KHZ)
302 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
303 struct sn9c102_sensor* sensor)
306 r = sn9c102_read_reg(cam, 0x08);
307 return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0;
312 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
313 struct sn9c102_sensor* sensor)
316 r = sn9c102_read_reg(cam, 0x08);
317 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
322 sn9c102_i2c_try_read(struct sn9c102_device* cam,
323 struct sn9c102_sensor* sensor, u8 address)
325 struct usb_device* udev = cam->usbdev;
326 u8* data = cam->control_buffer;
329 /* Write cycle - address */
330 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
331 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
332 data[1] = sensor->slave_write_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 /* Read cycle - 1 byte */
343 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
344 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
346 data[1] = sensor->slave_read_id;
348 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
349 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
353 err += sn9c102_i2c_wait(cam, sensor);
355 /* The read byte will be placed in data[4] */
356 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
357 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
361 err += sn9c102_i2c_detect_read_error(cam, sensor);
364 DBG(3, "I2C read failed for %s image sensor", sensor->name)
366 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[4])
368 return err ? -1 : (int)data[4];
373 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
374 struct sn9c102_sensor* sensor, u8 n, u8 data0,
375 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
377 struct usb_device* udev = cam->usbdev;
378 u8* data = cam->control_buffer;
381 /* Write cycle. It usually is address + value */
382 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
383 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
392 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
393 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
397 err += sn9c102_i2c_wait(cam, sensor);
398 err += sn9c102_i2c_detect_write_error(cam, sensor);
401 DBG(3, "I2C write failed for %s image sensor", sensor->name)
403 PDBGG("I2C write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
404 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
405 n, data0, data1, data2, data3, data4, data5)
412 sn9c102_i2c_try_write(struct sn9c102_device* cam,
413 struct sn9c102_sensor* sensor, u8 address, u8 value)
415 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
416 sensor->slave_write_id, address,
421 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
426 return sn9c102_i2c_try_read(cam, cam->sensor, address);
430 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
435 return sn9c102_i2c_try_write(cam, cam->sensor, address, value);
438 /*****************************************************************************/
440 static void* sn9c102_find_sof_header(void* mem, size_t len)
442 size_t soflen = sizeof(sn9c102_sof_header_t), i;
443 u8 j, n = sizeof(sn9c102_sof_header) / soflen;
445 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
446 for (j = 0; j < n; j++)
447 /* It's enough to compare 7 bytes */
448 if (!memcmp(mem + i, sn9c102_sof_header[j], 7))
449 /* Skips the header */
450 return mem + i + soflen;
456 static void* sn9c102_find_eof_header(void* mem, size_t len)
458 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
459 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
461 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
462 for (j = 0; j < n; j++)
463 if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen))
470 static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
472 struct sn9c102_device* cam = urb->context;
473 struct sn9c102_frame_t** f;
474 unsigned long lock_flags;
478 if (urb->status == -ENOENT)
481 f = &cam->frame_current;
483 if (cam->stream == STREAM_INTERRUPT) {
484 cam->stream = STREAM_OFF;
486 (*f)->state = F_QUEUED;
487 DBG(3, "Stream interrupted")
488 wake_up_interruptible(&cam->wait_stream);
491 if ((cam->state & DEV_DISCONNECTED)||(cam->state & DEV_MISCONFIGURED))
494 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
498 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
501 for (i = 0; i < urb->number_of_packets; i++) {
502 unsigned int img, len, status;
503 void *pos, *sof, *eof;
505 len = urb->iso_frame_desc[i].actual_length;
506 status = urb->iso_frame_desc[i].status;
507 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
510 DBG(3, "Error in isochronous frame")
511 (*f)->state = F_ERROR;
515 PDBGG("Isochrnous frame: length %u, #%u i", len, i)
518 NOTE: It is probably correct to assume that SOF and EOF
519 headers do not occur between two consecutive packets,
520 but who knows..Whatever is the truth, this assumption
521 doesn't introduce bugs.
525 sof = sn9c102_find_sof_header(pos, len);
527 eof = sn9c102_find_eof_header(pos, len);
528 if ((*f)->state == F_GRABBING) {
533 img = (eof > pos) ? eof - pos - 1 : 0;
535 if ((*f)->buf.bytesused+img>(*f)->buf.length) {
536 u32 b = (*f)->buf.bytesused + img -
538 img = (*f)->buf.length -
540 DBG(3, "Expected EOF not found: "
543 DBG(3, "Exceeded limit: +%u "
544 "bytes", (unsigned)(b))
547 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
550 if ((*f)->buf.bytesused == 0)
551 do_gettimeofday(&(*f)->buf.timestamp);
553 (*f)->buf.bytesused += img;
555 if ((*f)->buf.bytesused == (*f)->buf.length) {
556 u32 b = (*f)->buf.bytesused;
557 (*f)->state = F_DONE;
558 (*f)->buf.sequence= ++cam->frame_count;
559 spin_lock_irqsave(&cam->queue_lock,
561 list_move_tail(&(*f)->frame,
563 if (!list_empty(&cam->inqueue))
566 struct sn9c102_frame_t,
570 spin_unlock_irqrestore(&cam->queue_lock
572 DBG(3, "Video frame captured: "
573 "%lu bytes", (unsigned long)(b))
579 (*f)->state = F_ERROR;
580 DBG(3, "Not expected EOF after %lu "
581 "bytes of image data",
582 (unsigned long)((*f)->buf.bytesused))
589 DBG(3, "EOF without SOF")
593 PDBGG("Ignoring pointless isochronous frame")
597 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
599 (*f)->state = F_GRABBING;
600 (*f)->buf.bytesused = 0;
603 DBG(3, "SOF detected: new video frame")
607 } else if ((*f)->state == F_GRABBING) {
608 eof = sn9c102_find_eof_header(pos, len);
609 if (eof && eof < sof)
610 goto end_of_frame; /* (1) */
612 DBG(3, "SOF before expected EOF after %lu "
613 "bytes of image data",
614 (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")
739 /*****************************************************************************/
741 static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
748 strncpy(str, buff, len);
751 strncpy(str, buff, 4);
755 val = simple_strtoul(str, &endp, 0);
759 *count = (ssize_t)(endp - str);
760 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
772 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
774 struct sn9c102_device* cam;
777 if (down_interruptible(&sn9c102_sysfs_lock))
780 cam = video_get_drvdata(to_video_device(cd));
782 up(&sn9c102_sysfs_lock);
786 count = sprintf(buf, "%u\n", cam->sysfs.reg);
788 up(&sn9c102_sysfs_lock);
795 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
797 struct sn9c102_device* cam;
801 if (down_interruptible(&sn9c102_sysfs_lock))
804 cam = video_get_drvdata(to_video_device(cd));
806 up(&sn9c102_sysfs_lock);
810 index = sn9c102_strtou8(buf, len, &count);
811 if (index > 0x1f || !count) {
812 up(&sn9c102_sysfs_lock);
816 cam->sysfs.reg = index;
818 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg)
819 DBG(3, "Written bytes: %zd", count)
821 up(&sn9c102_sysfs_lock);
827 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
829 struct sn9c102_device* cam;
833 if (down_interruptible(&sn9c102_sysfs_lock))
836 cam = video_get_drvdata(to_video_device(cd));
838 up(&sn9c102_sysfs_lock);
842 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
843 up(&sn9c102_sysfs_lock);
847 count = sprintf(buf, "%d\n", val);
849 DBG(3, "Read bytes: %zd", count)
851 up(&sn9c102_sysfs_lock);
858 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
860 struct sn9c102_device* cam;
865 if (down_interruptible(&sn9c102_sysfs_lock))
868 cam = video_get_drvdata(to_video_device(cd));
870 up(&sn9c102_sysfs_lock);
874 value = sn9c102_strtou8(buf, len, &count);
876 up(&sn9c102_sysfs_lock);
880 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
882 up(&sn9c102_sysfs_lock);
886 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
887 cam->sysfs.reg, value)
888 DBG(3, "Written bytes: %zd", count)
890 up(&sn9c102_sysfs_lock);
896 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
898 struct sn9c102_device* cam;
901 if (down_interruptible(&sn9c102_sysfs_lock))
904 cam = video_get_drvdata(to_video_device(cd));
906 up(&sn9c102_sysfs_lock);
910 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
912 DBG(3, "Read bytes: %zd", count)
914 up(&sn9c102_sysfs_lock);
921 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
923 struct sn9c102_device* cam;
927 if (down_interruptible(&sn9c102_sysfs_lock))
930 cam = video_get_drvdata(to_video_device(cd));
932 up(&sn9c102_sysfs_lock);
936 index = sn9c102_strtou8(buf, len, &count);
938 up(&sn9c102_sysfs_lock);
942 cam->sysfs.i2c_reg = index;
944 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg)
945 DBG(3, "Written bytes: %zd", count)
947 up(&sn9c102_sysfs_lock);
953 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
955 struct sn9c102_device* cam;
959 if (down_interruptible(&sn9c102_sysfs_lock))
962 cam = video_get_drvdata(to_video_device(cd));
964 up(&sn9c102_sysfs_lock);
968 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
969 up(&sn9c102_sysfs_lock);
973 count = sprintf(buf, "%d\n", val);
975 DBG(3, "Read bytes: %zd", count)
977 up(&sn9c102_sysfs_lock);
984 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
986 struct sn9c102_device* cam;
991 if (down_interruptible(&sn9c102_sysfs_lock))
994 cam = video_get_drvdata(to_video_device(cd));
996 up(&sn9c102_sysfs_lock);
1000 value = sn9c102_strtou8(buf, len, &count);
1002 up(&sn9c102_sysfs_lock);
1006 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1008 up(&sn9c102_sysfs_lock);
1012 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1013 cam->sysfs.i2c_reg, value)
1014 DBG(3, "Written bytes: %zd", count)
1016 up(&sn9c102_sysfs_lock);
1023 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1029 value = sn9c102_strtou8(buf, len, &count);
1030 if (!count || value > 0x0f)
1033 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1034 res = sn9c102_store_val(cd, buf, len);
1040 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1041 sn9c102_show_reg, sn9c102_store_reg);
1042 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1043 sn9c102_show_val, sn9c102_store_val);
1044 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1045 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1046 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1047 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1048 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1051 static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1053 struct video_device *v4ldev = cam->v4ldev;
1055 video_device_create_file(v4ldev, &class_device_attr_reg);
1056 video_device_create_file(v4ldev, &class_device_attr_val);
1057 video_device_create_file(v4ldev, &class_device_attr_green);
1058 if (cam->sensor->slave_write_id && cam->sensor->slave_read_id) {
1059 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1060 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1064 /*****************************************************************************/
1066 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1072 r = cam->reg[0x18] & 0xcf;
1073 else if (scale == 2) {
1074 r = cam->reg[0x18] & 0xcf;
1076 } else if (scale == 4)
1077 r = cam->reg[0x18] | 0x20;
1079 err += sn9c102_write_reg(cam, r, 0x18);
1083 PDBGG("Scaling factor: %u", scale)
1089 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1091 struct sn9c102_sensor* s = cam->sensor;
1092 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1093 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1094 h_size = (u8)(rect->width / 16),
1095 v_size = (u8)(rect->height / 16),
1098 ae_endx = h_size / 2,
1099 ae_endy = v_size / 2;
1102 err += sn9c102_write_reg(cam, h_start, 0x12);
1103 err += sn9c102_write_reg(cam, v_start, 0x13);
1104 err += sn9c102_write_reg(cam, h_size, 0x15);
1105 err += sn9c102_write_reg(cam, v_size, 0x16);
1106 err += sn9c102_write_reg(cam, ae_strx, 0x1c);
1107 err += sn9c102_write_reg(cam, ae_stry, 0x1d);
1108 err += sn9c102_write_reg(cam, ae_endx, 0x1e);
1109 err += sn9c102_write_reg(cam, ae_endy, 0x1f);
1113 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1114 "%u %u %u %u", h_start, v_start, h_size, v_size)
1120 static int sn9c102_init(struct sn9c102_device* cam)
1122 struct sn9c102_sensor* s = cam->sensor;
1123 struct v4l2_control ctrl;
1124 struct v4l2_queryctrl *qctrl;
1125 struct v4l2_rect* rect;
1129 if (!(cam->state & DEV_INITIALIZED)) {
1130 init_waitqueue_head(&cam->open);
1132 rect = &(s->cropcap.defrect);
1133 } else { /* use current values */
1138 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1139 err += sn9c102_set_crop(cam, rect);
1146 DBG(3, "Sensor initialization failed")
1152 if ((err = s->set_crop(cam, rect))) {
1153 DBG(3, "set_crop() failed")
1158 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1159 for (i = 0; i < n; i++)
1160 if (s->qctrl[i].id != 0 &&
1161 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1162 ctrl.id = s->qctrl[i].id;
1163 ctrl.value = qctrl[i].default_value;
1164 err = s->set_ctrl(cam, &ctrl);
1166 DBG(3, "Set control failed")
1172 if (!(cam->state & DEV_INITIALIZED)) {
1173 init_MUTEX(&cam->fileop_sem);
1174 spin_lock_init(&cam->queue_lock);
1175 init_waitqueue_head(&cam->wait_frame);
1176 init_waitqueue_head(&cam->wait_stream);
1177 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1178 memcpy(&(s->_rect), &(s->cropcap.defrect),
1179 sizeof(struct v4l2_rect));
1180 cam->state |= DEV_INITIALIZED;
1183 DBG(2, "Initialization succeeded")
1188 static void sn9c102_release_resources(struct sn9c102_device* cam)
1190 down(&sn9c102_sysfs_lock);
1192 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor)
1193 video_set_drvdata(cam->v4ldev, NULL);
1194 video_unregister_device(cam->v4ldev);
1196 up(&sn9c102_sysfs_lock);
1198 kfree(cam->control_buffer);
1201 /*****************************************************************************/
1203 static int sn9c102_open(struct inode* inode, struct file* filp)
1205 struct sn9c102_device* cam;
1209 This is the only safe way to prevent race conditions with
1212 if (!down_read_trylock(&sn9c102_disconnect))
1213 return -ERESTARTSYS;
1215 cam = video_get_drvdata(video_devdata(filp));
1217 if (down_interruptible(&cam->dev_sem)) {
1218 up_read(&sn9c102_disconnect);
1219 return -ERESTARTSYS;
1223 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor)
1224 if ((filp->f_flags & O_NONBLOCK) ||
1225 (filp->f_flags & O_NDELAY)) {
1230 err = wait_event_interruptible_exclusive(cam->open,
1231 cam->state & DEV_DISCONNECTED
1234 up_read(&sn9c102_disconnect);
1237 if (cam->state & DEV_DISCONNECTED) {
1238 up_read(&sn9c102_disconnect);
1241 down(&cam->dev_sem);
1245 if (cam->state & DEV_MISCONFIGURED) {
1246 err = sn9c102_init(cam);
1248 DBG(1, "Initialization failed again. "
1249 "I will retry on next open().")
1252 cam->state &= ~DEV_MISCONFIGURED;
1255 if ((err = sn9c102_start_transfer(cam)))
1258 filp->private_data = cam;
1261 cam->stream = STREAM_OFF;
1263 cam->frame_count = 0;
1264 sn9c102_empty_framequeues(cam);
1266 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor)
1270 up_read(&sn9c102_disconnect);
1275 static int sn9c102_release(struct inode* inode, struct file* filp)
1277 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1279 down(&cam->dev_sem); /* prevent disconnect() to be called */
1281 sn9c102_stop_transfer(cam);
1283 sn9c102_release_buffers(cam);
1285 if (cam->state & DEV_DISCONNECTED) {
1286 sn9c102_release_resources(cam);
1293 wake_up_interruptible_nr(&cam->open, 1);
1295 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor)
1304 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1306 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1307 struct sn9c102_frame_t* f, * i;
1308 unsigned long lock_flags;
1311 if (down_interruptible(&cam->fileop_sem))
1312 return -ERESTARTSYS;
1314 if (cam->state & DEV_DISCONNECTED) {
1315 DBG(1, "Device not present")
1316 up(&cam->fileop_sem);
1320 if (cam->state & DEV_MISCONFIGURED) {
1321 DBG(1, "The camera is misconfigured. Close and open it again.")
1322 up(&cam->fileop_sem);
1326 if (cam->io == IO_MMAP) {
1327 DBG(3, "Close and open the device again to choose "
1329 up(&cam->fileop_sem);
1333 if (cam->io == IO_NONE) {
1334 if (!sn9c102_request_buffers(cam, 2)) {
1335 DBG(1, "read() failed, not enough memory")
1336 up(&cam->fileop_sem);
1340 cam->stream = STREAM_ON;
1341 sn9c102_queue_unusedframes(cam);
1345 up(&cam->fileop_sem);
1349 if (list_empty(&cam->outqueue)) {
1350 if (filp->f_flags & O_NONBLOCK) {
1351 up(&cam->fileop_sem);
1354 err = wait_event_interruptible
1356 (!list_empty(&cam->outqueue)) ||
1357 (cam->state & DEV_DISCONNECTED) );
1359 up(&cam->fileop_sem);
1362 if (cam->state & DEV_DISCONNECTED) {
1363 up(&cam->fileop_sem);
1368 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1370 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1371 list_for_each_entry(i, &cam->outqueue, frame)
1372 i->state = F_UNUSED;
1373 INIT_LIST_HEAD(&cam->outqueue);
1374 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1376 sn9c102_queue_unusedframes(cam);
1378 if (count > f->buf.length)
1379 count = f->buf.length;
1381 if (copy_to_user(buf, f->bufmem, count)) {
1382 up(&cam->fileop_sem);
1387 PDBGG("Frame #%lu, bytes read: %zu", (unsigned long)f->buf.index,count)
1389 up(&cam->fileop_sem);
1395 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1397 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1398 unsigned int mask = 0;
1400 if (down_interruptible(&cam->fileop_sem))
1403 if (cam->state & DEV_DISCONNECTED) {
1404 DBG(1, "Device not present")
1408 if (cam->state & DEV_MISCONFIGURED) {
1409 DBG(1, "The camera is misconfigured. Close and open it again.")
1413 if (cam->io == IO_NONE) {
1414 if (!sn9c102_request_buffers(cam, 2)) {
1415 DBG(1, "poll() failed, not enough memory")
1419 cam->stream = STREAM_ON;
1422 if (cam->io == IO_READ)
1423 sn9c102_queue_unusedframes(cam);
1425 poll_wait(filp, &cam->wait_frame, wait);
1427 if (!list_empty(&cam->outqueue))
1428 mask |= POLLIN | POLLRDNORM;
1430 up(&cam->fileop_sem);
1435 up(&cam->fileop_sem);
1440 static void sn9c102_vm_open(struct vm_area_struct* vma)
1442 struct sn9c102_frame_t* f = vma->vm_private_data;
1447 static void sn9c102_vm_close(struct vm_area_struct* vma)
1449 /* NOTE: buffers are not freed here */
1450 struct sn9c102_frame_t* f = vma->vm_private_data;
1455 static struct vm_operations_struct sn9c102_vm_ops = {
1456 .open = sn9c102_vm_open,
1457 .close = sn9c102_vm_close,
1461 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1463 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1464 unsigned long size = vma->vm_end - vma->vm_start,
1465 start = vma->vm_start,
1470 if (down_interruptible(&cam->fileop_sem))
1471 return -ERESTARTSYS;
1473 if (cam->state & DEV_DISCONNECTED) {
1474 DBG(1, "Device not present")
1475 up(&cam->fileop_sem);
1479 if (cam->state & DEV_MISCONFIGURED) {
1480 DBG(1, "The camera is misconfigured. Close and open it again.")
1481 up(&cam->fileop_sem);
1485 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1486 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1487 up(&cam->fileop_sem);
1491 for (i = 0; i < cam->nbuffers; i++) {
1492 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1495 if (i == cam->nbuffers) {
1496 up(&cam->fileop_sem);
1500 pos = (unsigned long)cam->frame[i].bufmem;
1501 while (size > 0) { /* size is page-aligned */
1502 page = kvirt_to_pa(pos);
1503 if (remap_page_range(vma, start, page, PAGE_SIZE,
1504 vma->vm_page_prot)) {
1505 up(&cam->fileop_sem);
1513 vma->vm_ops = &sn9c102_vm_ops;
1514 vma->vm_flags &= ~VM_IO; /* not I/O memory */
1515 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1516 vma->vm_private_data = &cam->frame[i];
1518 sn9c102_vm_open(vma);
1520 up(&cam->fileop_sem);
1526 static int sn9c102_v4l2_ioctl(struct inode* inode, struct file* filp,
1527 unsigned int cmd, void __user * arg)
1529 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1533 case VIDIOC_QUERYCAP:
1535 struct v4l2_capability cap = {
1536 .driver = "sn9c102",
1537 .version = SN9C102_MODULE_VERSION_CODE,
1538 .capabilities = V4L2_CAP_VIDEO_CAPTURE |
1539 V4L2_CAP_READWRITE |
1543 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1544 strlcpy(cap.bus_info, cam->dev.bus_id, sizeof(cap.bus_info));
1546 if (copy_to_user(arg, &cap, sizeof(cap)))
1552 case VIDIOC_ENUMINPUT:
1554 struct v4l2_input i;
1556 if (copy_from_user(&i, arg, sizeof(i)))
1562 memset(&i, 0, sizeof(i));
1563 strcpy(i.name, "USB");
1565 if (copy_to_user(arg, &i, sizeof(i)))
1571 case VIDIOC_G_INPUT:
1572 case VIDIOC_S_INPUT:
1576 if (copy_from_user(&index, arg, sizeof(index)))
1585 case VIDIOC_QUERYCTRL:
1587 struct sn9c102_sensor* s = cam->sensor;
1588 struct v4l2_queryctrl qc;
1591 if (copy_from_user(&qc, arg, sizeof(qc)))
1594 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1595 for (i = 0; i < n; i++)
1596 if (qc.id && qc.id == s->qctrl[i].id) {
1597 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1598 if (copy_to_user(arg, &qc, sizeof(qc)))
1608 struct sn9c102_sensor* s = cam->sensor;
1609 struct v4l2_control ctrl;
1615 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1618 err = s->get_ctrl(cam, &ctrl);
1620 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1628 struct sn9c102_sensor* s = cam->sensor;
1629 struct v4l2_control ctrl;
1636 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1639 if ((err = s->set_ctrl(cam, &ctrl)))
1642 n = sizeof(s->qctrl) / sizeof(s->qctrl[0]);
1643 for (i = 0; i < n; i++)
1644 if (ctrl.id == s->qctrl[i].id) {
1645 s->_qctrl[i].default_value = ctrl.value;
1652 case VIDIOC_CROPCAP:
1654 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1656 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1657 cc->pixelaspect.numerator = 1;
1658 cc->pixelaspect.denominator = 1;
1660 if (copy_to_user(arg, cc, sizeof(*cc)))
1668 struct sn9c102_sensor* s = cam->sensor;
1669 struct v4l2_crop crop = {
1670 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1673 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1675 if (copy_to_user(arg, &crop, sizeof(crop)))
1683 struct sn9c102_sensor* s = cam->sensor;
1684 struct v4l2_crop crop;
1685 struct v4l2_rect* rect;
1686 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1687 struct v4l2_pix_format* pix_format = &(s->pix_format);
1689 const enum sn9c102_stream_state stream = cam->stream;
1690 const u32 nbuffers = cam->nbuffers;
1694 if (copy_from_user(&crop, arg, sizeof(crop)))
1699 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1702 for (i = 0; i < cam->nbuffers; i++)
1703 if (cam->frame[i].vma_use_count) {
1704 DBG(3, "VIDIOC_S_CROP failed. "
1705 "Unmap the buffers first.")
1709 /* Preserve R,G or B origin */
1710 rect->left = (s->_rect.left & 1L) ?
1711 rect->left | 1L : rect->left & ~1L;
1712 rect->top = (s->_rect.top & 1L) ?
1713 rect->top | 1L : rect->top & ~1L;
1715 if (rect->width < 16)
1717 if (rect->height < 16)
1719 if (rect->width > bounds->width)
1720 rect->width = bounds->width;
1721 if (rect->height > bounds->height)
1722 rect->height = bounds->height;
1723 if (rect->left < bounds->left)
1724 rect->left = bounds->left;
1725 if (rect->top < bounds->top)
1726 rect->top = bounds->top;
1727 if (rect->left + rect->width > bounds->left + bounds->width)
1728 rect->left = bounds->left+bounds->width - rect->width;
1729 if (rect->top + rect->height > bounds->top + bounds->height)
1730 rect->top = bounds->top+bounds->height - rect->height;
1732 rect->width &= ~15L;
1733 rect->height &= ~15L;
1735 if (SN9C102_PRESERVE_IMGSCALE) {
1736 /* Calculate the actual scaling factor */
1738 a = rect->width * rect->height;
1739 b = pix_format->width * pix_format->height;
1740 scale = b ? (u8)((a / b) < 4 ? 1 :
1741 ((a / b) < 16 ? 2 : 4)) : 1;
1745 if (cam->stream == STREAM_ON) {
1746 cam->stream = STREAM_INTERRUPT;
1747 err = wait_event_interruptible
1749 (cam->stream == STREAM_OFF) ||
1750 (cam->state & DEV_DISCONNECTED) );
1752 cam->state |= DEV_MISCONFIGURED;
1753 DBG(1, "The camera is misconfigured. To use "
1754 "it, close and open /dev/video%d "
1755 "again.", cam->v4ldev->minor)
1758 if (cam->state & DEV_DISCONNECTED)
1762 if (copy_to_user(arg, &crop, sizeof(crop))) {
1763 cam->stream = stream;
1767 sn9c102_release_buffers(cam);
1769 err = sn9c102_set_crop(cam, rect);
1771 err += s->set_crop(cam, rect);
1772 err += sn9c102_set_scale(cam, scale);
1774 if (err) { /* atomic, no rollback in ioctl() */
1775 cam->state |= DEV_MISCONFIGURED;
1776 DBG(1, "VIDIOC_S_CROP failed because of hardware "
1777 "problems. To use the camera, close and open "
1778 "/dev/video%d again.", cam->v4ldev->minor)
1782 s->pix_format.width = rect->width/scale;
1783 s->pix_format.height = rect->height/scale;
1784 memcpy(&(s->_rect), rect, sizeof(*rect));
1786 if (nbuffers != sn9c102_request_buffers(cam, nbuffers)) {
1787 cam->state |= DEV_MISCONFIGURED;
1788 DBG(1, "VIDIOC_S_CROP failed because of not enough "
1789 "memory. To use the camera, close and open "
1790 "/dev/video%d again.", cam->v4ldev->minor)
1794 cam->stream = stream;
1799 case VIDIOC_ENUM_FMT:
1801 struct sn9c102_sensor* s = cam->sensor;
1802 struct v4l2_fmtdesc fmtd;
1804 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1807 if (fmtd.index != 0)
1810 memset(&fmtd, 0, sizeof(fmtd));
1812 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1813 strcpy(fmtd.description, "bayer rgb");
1814 fmtd.pixelformat = s->pix_format.pixelformat;
1816 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1824 struct v4l2_format format;
1825 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
1827 if (copy_from_user(&format, arg, sizeof(format)))
1830 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1833 pfmt->bytesperline = (pfmt->width * pfmt->priv) / 8;
1834 pfmt->sizeimage = pfmt->height * pfmt->bytesperline;
1835 pfmt->field = V4L2_FIELD_NONE;
1836 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1838 if (copy_to_user(arg, &format, sizeof(format)))
1844 case VIDIOC_TRY_FMT:
1847 struct sn9c102_sensor* s = cam->sensor;
1848 struct v4l2_format format;
1849 struct v4l2_pix_format* pix;
1850 struct v4l2_pix_format* pfmt = &(s->pix_format);
1851 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1852 struct v4l2_rect rect;
1854 const enum sn9c102_stream_state stream = cam->stream;
1855 const u32 nbuffers = cam->nbuffers;
1859 if (copy_from_user(&format, arg, sizeof(format)))
1862 pix = &(format.fmt.pix);
1864 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1867 memcpy(&rect, &(s->_rect), sizeof(rect));
1869 { /* calculate the actual scaling factor */
1871 a = rect.width * rect.height;
1872 b = pix->width * pix->height;
1873 scale = b ? (u8)((a / b) < 4 ? 1 :
1874 ((a / b) < 16 ? 2 : 4)) : 1;
1877 rect.width = scale * pix->width;
1878 rect.height = scale * pix->height;
1880 if (rect.width < 16)
1882 if (rect.height < 16)
1884 if (rect.width > bounds->left + bounds->width - rect.left)
1885 rect.width = bounds->left + bounds->width - rect.left;
1886 if (rect.height > bounds->top + bounds->height - rect.top)
1887 rect.height = bounds->top + bounds->height - rect.top;
1890 rect.height &= ~15L;
1892 { /* adjust the scaling factor */
1894 a = rect.width * rect.height;
1895 b = pix->width * pix->height;
1896 scale = b ? (u8)((a / b) < 4 ? 1 :
1897 ((a / b) < 16 ? 2 : 4)) : 1;
1900 pix->width = rect.width / scale;
1901 pix->height = rect.height / scale;
1903 pix->pixelformat = pfmt->pixelformat;
1904 pix->priv = pfmt->priv; /* bpp */
1905 pix->colorspace = pfmt->colorspace;
1906 pix->bytesperline = (pix->width * pix->priv) / 8;
1907 pix->sizeimage = pix->height * pix->bytesperline;
1908 pix->field = V4L2_FIELD_NONE;
1910 if (cmd == VIDIOC_TRY_FMT)
1913 for (i = 0; i < cam->nbuffers; i++)
1914 if (cam->frame[i].vma_use_count) {
1915 DBG(3, "VIDIOC_S_FMT failed. "
1916 "Unmap the buffers first.")
1920 if (cam->stream == STREAM_ON) {
1921 cam->stream = STREAM_INTERRUPT;
1922 err = wait_event_interruptible
1924 (cam->stream == STREAM_OFF) ||
1925 (cam->state & DEV_DISCONNECTED) );
1927 cam->state |= DEV_MISCONFIGURED;
1928 DBG(1, "The camera is misconfigured. To use "
1929 "it, close and open /dev/video%d "
1930 "again.", cam->v4ldev->minor)
1933 if (cam->state & DEV_DISCONNECTED)
1937 if (copy_to_user(arg, &format, sizeof(format))) {
1938 cam->stream = stream;
1942 sn9c102_release_buffers(cam);
1944 err = sn9c102_set_crop(cam, &rect);
1946 err += s->set_crop(cam, &rect);
1947 err += sn9c102_set_scale(cam, scale);
1949 if (err) { /* atomic, no rollback in ioctl() */
1950 cam->state |= DEV_MISCONFIGURED;
1951 DBG(1, "VIDIOC_S_FMT failed because of hardware "
1952 "problems. To use the camera, close and open "
1953 "/dev/video%d again.", cam->v4ldev->minor)
1957 memcpy(pfmt, pix, sizeof(*pix));
1958 memcpy(&(s->_rect), &rect, sizeof(rect));
1960 if (nbuffers != sn9c102_request_buffers(cam, nbuffers)) {
1961 cam->state |= DEV_MISCONFIGURED;
1962 DBG(1, "VIDIOC_S_FMT failed because of not enough "
1963 "memory. To use the camera, close and open "
1964 "/dev/video%d again.", cam->v4ldev->minor)
1968 cam->stream = stream;
1973 case VIDIOC_REQBUFS:
1975 struct v4l2_requestbuffers rb;
1979 if (copy_from_user(&rb, arg, sizeof(rb)))
1982 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1983 rb.memory != V4L2_MEMORY_MMAP)
1986 if (cam->io == IO_READ) {
1987 DBG(3, "Close and open the device again to choose "
1988 "the mmap I/O method")
1992 for (i = 0; i < cam->nbuffers; i++)
1993 if (cam->frame[i].vma_use_count) {
1994 DBG(3, "VIDIOC_REQBUFS failed. "
1995 "Previous buffers are still mapped.")
1999 if (cam->stream == STREAM_ON) {
2000 cam->stream = STREAM_INTERRUPT;
2001 err = wait_event_interruptible
2003 (cam->stream == STREAM_OFF) ||
2004 (cam->state & DEV_DISCONNECTED) );
2006 cam->state |= DEV_MISCONFIGURED;
2007 DBG(1, "The camera is misconfigured. To use "
2008 "it, close and open /dev/video%d "
2009 "again.", cam->v4ldev->minor)
2012 if (cam->state & DEV_DISCONNECTED)
2016 sn9c102_empty_framequeues(cam);
2018 sn9c102_release_buffers(cam);
2020 rb.count = sn9c102_request_buffers(cam, rb.count);
2022 if (copy_to_user(arg, &rb, sizeof(rb))) {
2023 sn9c102_release_buffers(cam);
2028 cam->io = rb.count ? IO_MMAP : IO_NONE;
2033 case VIDIOC_QUERYBUF:
2035 struct v4l2_buffer b;
2037 if (copy_from_user(&b, arg, sizeof(b)))
2040 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2041 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2044 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2046 if (cam->frame[b.index].vma_use_count)
2047 b.flags |= V4L2_BUF_FLAG_MAPPED;
2049 if (cam->frame[b.index].state == F_DONE)
2050 b.flags |= V4L2_BUF_FLAG_DONE;
2051 else if (cam->frame[b.index].state != F_UNUSED)
2052 b.flags |= V4L2_BUF_FLAG_QUEUED;
2054 if (copy_to_user(arg, &b, sizeof(b)))
2062 struct v4l2_buffer b;
2063 unsigned long lock_flags;
2065 if (copy_from_user(&b, arg, sizeof(b)))
2068 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2069 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2072 if (cam->frame[b.index].state != F_UNUSED)
2075 cam->frame[b.index].state = F_QUEUED;
2077 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2078 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2079 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2081 PDBGG("Frame #%lu queued", (unsigned long)b.index)
2088 struct v4l2_buffer b;
2089 struct sn9c102_frame_t *f;
2090 unsigned long lock_flags;
2093 if (copy_from_user(&b, arg, sizeof(b)))
2096 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2099 if (list_empty(&cam->outqueue)) {
2100 if (cam->stream == STREAM_OFF)
2102 if (filp->f_flags & O_NONBLOCK)
2104 err = wait_event_interruptible
2106 (!list_empty(&cam->outqueue)) ||
2107 (cam->state & DEV_DISCONNECTED) );
2110 if (cam->state & DEV_DISCONNECTED)
2114 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2115 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t,
2117 list_del(cam->outqueue.next);
2118 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2120 f->state = F_UNUSED;
2122 memcpy(&b, &f->buf, sizeof(b));
2123 if (f->vma_use_count)
2124 b.flags |= V4L2_BUF_FLAG_MAPPED;
2126 if (copy_to_user(arg, &b, sizeof(b)))
2129 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index)
2134 case VIDIOC_STREAMON:
2138 if (copy_from_user(&type, arg, sizeof(type)))
2141 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2144 if (list_empty(&cam->inqueue))
2147 cam->stream = STREAM_ON;
2154 case VIDIOC_STREAMOFF:
2158 if (copy_from_user(&type, arg, sizeof(type)))
2161 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2164 if (cam->stream == STREAM_ON) {
2165 cam->stream = STREAM_INTERRUPT;
2166 err = wait_event_interruptible
2168 (cam->stream == STREAM_OFF) ||
2169 (cam->state & DEV_DISCONNECTED) );
2171 cam->state |= DEV_MISCONFIGURED;
2172 DBG(1, "The camera is misconfigured. To use "
2173 "it, close and open /dev/video%d "
2174 "again.", cam->v4ldev->minor)
2177 if (cam->state & DEV_DISCONNECTED)
2181 sn9c102_empty_framequeues(cam);
2183 DBG(3, "Stream off")
2190 case VIDIOC_QUERYSTD:
2191 case VIDIOC_ENUMSTD:
2192 case VIDIOC_QUERYMENU:
2204 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2205 unsigned int cmd, unsigned long arg)
2207 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2210 if (down_interruptible(&cam->fileop_sem))
2211 return -ERESTARTSYS;
2213 if (cam->state & DEV_DISCONNECTED) {
2214 DBG(1, "Device not present")
2215 up(&cam->fileop_sem);
2219 if (cam->state & DEV_MISCONFIGURED) {
2220 DBG(1, "The camera is misconfigured. Close and open it again.")
2221 up(&cam->fileop_sem);
2225 err = sn9c102_v4l2_ioctl(inode, filp, cmd, (void __user *)arg);
2227 up(&cam->fileop_sem);
2233 static struct file_operations sn9c102_fops = {
2234 .owner = THIS_MODULE,
2235 .open = sn9c102_open,
2236 .release = sn9c102_release,
2237 .ioctl = sn9c102_ioctl,
2238 .read = sn9c102_read,
2239 .poll = sn9c102_poll,
2240 .mmap = sn9c102_mmap,
2241 .llseek = no_llseek,
2244 /*****************************************************************************/
2246 /* It exists a single interface only. We do not need to validate anything. */
2248 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2250 struct usb_device *udev = interface_to_usbdev(intf);
2251 struct sn9c102_device* cam;
2252 static unsigned int dev_nr = 0;
2256 n = sizeof(sn9c102_id_table)/sizeof(sn9c102_id_table[0]);
2257 for (i = 0; i < n-1; i++)
2258 if (udev->descriptor.idVendor==sn9c102_id_table[i].idVendor &&
2259 udev->descriptor.idProduct==sn9c102_id_table[i].idProduct)
2264 if (!(cam = kmalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2266 memset(cam, 0, sizeof(*cam));
2270 memcpy(&cam->dev, &udev->dev, sizeof(struct device));
2272 if (!(cam->control_buffer = kmalloc(8, GFP_KERNEL))) {
2273 DBG(1, "kmalloc() failed")
2277 memset(cam->control_buffer, 0, 8);
2279 if (!(cam->v4ldev = video_device_alloc())) {
2280 DBG(1, "video_device_alloc() failed")
2285 init_MUTEX(&cam->dev_sem);
2287 r = sn9c102_read_reg(cam, 0x00);
2288 if (r < 0 || r != 0x10) {
2289 DBG(1, "Sorry, this is not a SN9C10[12] based camera "
2290 "(vid/pid 0x%04X/0x%04X)",
2291 sn9c102_id_table[i].idVendor,sn9c102_id_table[i].idProduct)
2296 DBG(2, "SN9C10[12] PC Camera Controller detected "
2297 "(vid/pid 0x%04X/0x%04X)",
2298 sn9c102_id_table[i].idVendor, sn9c102_id_table[i].idProduct)
2300 for (i = 0; sn9c102_sensor_table[i]; i++) {
2301 err = sn9c102_sensor_table[i](cam);
2306 if (!err && cam->sensor) {
2307 DBG(2, "%s image sensor detected", cam->sensor->name)
2308 DBG(3, "Support for %s maintained by %s",
2309 cam->sensor->name, cam->sensor->maintainer)
2311 DBG(1, "No supported image sensor detected")
2316 if (sn9c102_init(cam)) {
2317 DBG(1, "Initialization failed. I will retry on open().")
2318 cam->state |= DEV_MISCONFIGURED;
2321 strcpy(cam->v4ldev->name, "SN9C10[12] PC Camera");
2322 cam->v4ldev->owner = THIS_MODULE;
2323 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2324 cam->v4ldev->hardware = VID_HARDWARE_SN9C102;
2325 cam->v4ldev->fops = &sn9c102_fops;
2326 cam->v4ldev->minor = video_nr[dev_nr];
2327 cam->v4ldev->release = video_device_release;
2328 video_set_drvdata(cam->v4ldev, cam);
2330 down(&cam->dev_sem);
2332 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2335 DBG(1, "V4L2 device registration failed")
2336 if (err == -ENFILE && video_nr[dev_nr] == -1)
2337 DBG(1, "Free /dev/videoX node not found")
2338 video_nr[dev_nr] = -1;
2339 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2344 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor)
2346 sn9c102_create_sysfs(cam);
2348 usb_set_intfdata(intf, cam);
2356 kfree(cam->control_buffer);
2358 video_device_release(cam->v4ldev);
2365 static void sn9c102_usb_disconnect(struct usb_interface* intf)
2367 struct sn9c102_device* cam = usb_get_intfdata(intf);
2372 down_write(&sn9c102_disconnect);
2374 down(&cam->dev_sem);
2376 DBG(2, "Disconnecting %s...", cam->v4ldev->name)
2378 wake_up_interruptible_all(&cam->open);
2381 DBG(2, "Device /dev/video%d is open! Deregistration and "
2382 "memory deallocation are deferred on close.",
2384 cam->state |= DEV_MISCONFIGURED;
2385 sn9c102_stop_transfer(cam);
2386 cam->state |= DEV_DISCONNECTED;
2387 wake_up_interruptible(&cam->wait_frame);
2388 wake_up_interruptible(&cam->wait_stream);
2390 cam->state |= DEV_DISCONNECTED;
2391 sn9c102_release_resources(cam);
2399 up_write(&sn9c102_disconnect);
2403 static struct usb_driver sn9c102_usb_driver = {
2404 .owner = THIS_MODULE,
2406 .id_table = sn9c102_id_table,
2407 .probe = sn9c102_usb_probe,
2408 .disconnect = sn9c102_usb_disconnect,
2411 /*****************************************************************************/
2413 static int __init sn9c102_module_init(void)
2417 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION)
2418 KDBG(3, SN9C102_MODULE_AUTHOR)
2420 if ((err = usb_register(&sn9c102_usb_driver)))
2421 KDBG(1, "usb_register() failed")
2427 static void __exit sn9c102_module_exit(void)
2429 usb_deregister(&sn9c102_usb_driver);
2433 module_init(sn9c102_module_init);
2434 module_exit(sn9c102_module_exit);