1 /***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers *
4 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/param.h>
25 #include <linux/moduleparam.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
29 #include <linux/device.h>
31 #include <linux/delay.h>
32 #include <linux/stddef.h>
33 #include <linux/compiler.h>
34 #include <linux/ioctl.h>
35 #include <linux/poll.h>
36 #include <linux/stat.h>
38 #include <linux/vmalloc.h>
39 #include <linux/page-flags.h>
40 #include <linux/byteorder/generic.h>
42 #include <asm/uaccess.h>
46 /*****************************************************************************/
48 #define SN9C102_MODULE_NAME "V4L2 driver for SN9C10x PC Camera Controllers"
49 #define SN9C102_MODULE_AUTHOR "(C) 2004-2006 Luca Risolia"
50 #define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
51 #define SN9C102_MODULE_LICENSE "GPL"
52 #define SN9C102_MODULE_VERSION "1:1.26"
53 #define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 26)
55 /*****************************************************************************/
57 MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
59 MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
60 MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
61 MODULE_VERSION(SN9C102_MODULE_VERSION);
62 MODULE_LICENSE(SN9C102_MODULE_LICENSE);
64 static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
65 module_param_array(video_nr, short, NULL, 0444);
66 MODULE_PARM_DESC(video_nr,
67 "\n<-1|n[,...]> Specify V4L2 minor mode number."
68 "\n -1 = use next available (default)"
69 "\n n = use minor number n (integer >= 0)"
70 "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
73 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
74 "\nthe second camera and use auto for the first"
75 "\none and for every other camera."
78 static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
79 SN9C102_FORCE_MUNMAP};
80 module_param_array(force_munmap, bool, NULL, 0444);
81 MODULE_PARM_DESC(force_munmap,
82 "\n<0|1[,...]> Force the application to unmap previously"
83 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
84 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
85 "\nthis feature. This parameter is specific for each"
87 "\n 0 = do not force memory unmapping"
88 "\n 1 = force memory unmapping (save memory)"
89 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
93 static unsigned short debug = SN9C102_DEBUG_LEVEL;
94 module_param(debug, ushort, 0644);
95 MODULE_PARM_DESC(debug,
96 "\n<n> Debugging information level, from 0 to 3:"
97 "\n0 = none (use carefully)"
98 "\n1 = critical errors"
99 "\n2 = significant informations"
100 "\n3 = more verbose messages"
101 "\nLevel 3 is useful for testing only, when only "
102 "one device is used."
103 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
107 /*****************************************************************************/
109 static sn9c102_sof_header_t sn9c102_sof_header[] = {
110 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x00},
111 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01},
114 static sn9c103_sof_header_t sn9c103_sof_header[] = {
115 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x20},
118 static sn9c102_eof_header_t sn9c102_eof_header[] = {
119 {0x00, 0x00, 0x00, 0x00},
120 {0x40, 0x00, 0x00, 0x00},
121 {0x80, 0x00, 0x00, 0x00},
122 {0xc0, 0x00, 0x00, 0x00},
125 /*****************************************************************************/
128 sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
129 enum sn9c102_io_method io)
131 struct v4l2_pix_format* p = &(cam->sensor->pix_format);
132 struct v4l2_rect* r = &(cam->sensor->cropcap.bounds);
133 const size_t imagesize = cam->module_param.force_munmap ||
135 (p->width * p->height * p->priv) / 8 :
136 (r->width * r->height * p->priv) / 8;
140 if (count > SN9C102_MAX_FRAMES)
141 count = SN9C102_MAX_FRAMES;
143 cam->nbuffers = count;
144 while (cam->nbuffers > 0) {
145 if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
150 for (i = 0; i < cam->nbuffers; i++) {
151 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
152 cam->frame[i].buf.index = i;
153 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
154 cam->frame[i].buf.length = imagesize;
155 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
156 cam->frame[i].buf.sequence = 0;
157 cam->frame[i].buf.field = V4L2_FIELD_NONE;
158 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
159 cam->frame[i].buf.flags = 0;
162 return cam->nbuffers;
166 static void sn9c102_release_buffers(struct sn9c102_device* cam)
169 vfree(cam->frame[0].bufmem);
172 cam->frame_current = NULL;
176 static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
180 INIT_LIST_HEAD(&cam->inqueue);
181 INIT_LIST_HEAD(&cam->outqueue);
183 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
184 cam->frame[i].state = F_UNUSED;
185 cam->frame[i].buf.bytesused = 0;
190 static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
192 struct sn9c102_frame_t *i;
194 list_for_each_entry(i, &cam->outqueue, frame) {
196 list_add(&i->frame, &cam->inqueue);
199 INIT_LIST_HEAD(&cam->outqueue);
203 static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
205 unsigned long lock_flags;
208 for (i = 0; i < cam->nbuffers; i++)
209 if (cam->frame[i].state == F_UNUSED) {
210 cam->frame[i].state = F_QUEUED;
211 spin_lock_irqsave(&cam->queue_lock, lock_flags);
212 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
213 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
217 /*****************************************************************************/
219 int sn9c102_write_regs(struct sn9c102_device* cam, u8* buff, u16 index)
221 struct usb_device* udev = cam->usbdev;
224 if (index + sizeof(buff) >= ARRAY_SIZE(cam->reg))
227 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
228 index, 0, buff, sizeof(buff),
229 SN9C102_CTRL_TIMEOUT*sizeof(buff));
231 DBG(3, "Failed to write registers (index 0x%02X, error %d)",
236 for (i = 0; i < sizeof(buff); i++)
237 cam->reg[index+i] = buff[i];
243 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
245 struct usb_device* udev = cam->usbdev;
246 u8* buff = cam->control_buffer;
249 if (index >= ARRAY_SIZE(cam->reg))
254 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
255 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
257 DBG(3, "Failed to write a register (value 0x%02X, index "
258 "0x%02X, error %d)", value, index, res);
262 cam->reg[index] = value;
268 /* NOTE: reading some registers always returns 0 */
269 static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
271 struct usb_device* udev = cam->usbdev;
272 u8* buff = cam->control_buffer;
275 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
276 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
278 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
281 return (res >= 0) ? (int)(*buff) : -1;
285 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
287 if (index >= ARRAY_SIZE(cam->reg))
290 return cam->reg[index];
295 sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
299 for (i = 1; i <= 5; i++) {
300 r = sn9c102_read_reg(cam, 0x08);
305 if (sensor->frequency & SN9C102_I2C_400KHZ)
315 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
316 struct sn9c102_sensor* sensor)
319 r = sn9c102_read_reg(cam, 0x08);
320 return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0;
325 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
326 struct sn9c102_sensor* sensor)
329 r = sn9c102_read_reg(cam, 0x08);
330 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
335 sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
336 struct sn9c102_sensor* sensor, u8 data0, u8 data1,
339 struct usb_device* udev = cam->usbdev;
340 u8* data = cam->control_buffer;
344 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
345 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
346 data[1] = data0; /* I2C slave id */
347 data[2] = data1; /* address */
349 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
350 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
354 err += sn9c102_i2c_wait(cam, sensor);
356 /* Read cycle - n bytes */
357 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
358 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
362 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
363 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
367 err += sn9c102_i2c_wait(cam, sensor);
369 /* The first read byte will be placed in data[4] */
370 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
371 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
375 err += sn9c102_i2c_detect_read_error(cam, sensor);
377 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
381 DBG(3, "I2C read failed for %s image sensor", sensor->name);
386 memcpy(buffer, data, sizeof(buffer));
393 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
394 struct sn9c102_sensor* sensor, u8 n, u8 data0,
395 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
397 struct usb_device* udev = cam->usbdev;
398 u8* data = cam->control_buffer;
401 /* Write cycle. It usually is address + value */
402 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
403 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
412 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
413 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
417 err += sn9c102_i2c_wait(cam, sensor);
418 err += sn9c102_i2c_detect_write_error(cam, sensor);
421 DBG(3, "I2C write failed for %s image sensor", sensor->name);
423 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
424 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
425 n, data0, data1, data2, data3, data4, data5);
432 sn9c102_i2c_try_read(struct sn9c102_device* cam,
433 struct sn9c102_sensor* sensor, u8 address)
435 return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
441 sn9c102_i2c_try_write(struct sn9c102_device* cam,
442 struct sn9c102_sensor* sensor, u8 address, u8 value)
444 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
445 sensor->i2c_slave_id, address,
450 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
455 return sn9c102_i2c_try_read(cam, cam->sensor, address);
459 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
464 return sn9c102_i2c_try_write(cam, cam->sensor, address, value);
467 /*****************************************************************************/
470 sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
472 size_t soflen = 0, i;
475 switch (cam->bridge) {
478 soflen = sizeof(sn9c102_sof_header_t);
479 n = sizeof(sn9c102_sof_header) / soflen;
482 soflen = sizeof(sn9c103_sof_header_t);
483 n = sizeof(sn9c103_sof_header) / soflen;
486 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
487 for (j = 0; j < n; j++)
488 /* The invariable part of the header is 6 bytes long */
489 if ((cam->bridge != BRIDGE_SN9C103 &&
490 !memcmp(mem + i, sn9c102_sof_header[j], 6)) ||
491 (cam->bridge == BRIDGE_SN9C103 &&
492 !memcmp(mem + i, sn9c103_sof_header[j], 6))) {
493 memcpy(cam->sof_header, mem + i, soflen);
494 /* Skip the header */
495 return mem + i + soflen;
503 sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
505 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
506 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
508 if (cam->sensor->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
509 return NULL; /* EOF header does not exist in compressed data */
511 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
512 for (j = 0; j < n; j++)
513 if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen))
520 static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
522 struct sn9c102_device* cam = urb->context;
523 struct sn9c102_frame_t** f;
524 size_t imagesize, soflen;
528 if (urb->status == -ENOENT)
531 f = &cam->frame_current;
533 if (cam->stream == STREAM_INTERRUPT) {
534 cam->stream = STREAM_OFF;
536 (*f)->state = F_QUEUED;
537 DBG(3, "Stream interrupted");
538 wake_up_interruptible(&cam->wait_stream);
541 if (cam->state & DEV_DISCONNECTED)
544 if (cam->state & DEV_MISCONFIGURED) {
545 wake_up_interruptible(&cam->wait_frame);
549 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
553 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
556 imagesize = (cam->sensor->pix_format.width *
557 cam->sensor->pix_format.height *
558 cam->sensor->pix_format.priv) / 8;
560 soflen = (cam->bridge) == BRIDGE_SN9C103 ?
561 sizeof(sn9c103_sof_header_t) :
562 sizeof(sn9c102_sof_header_t);
564 for (i = 0; i < urb->number_of_packets; i++) {
565 unsigned int img, len, status;
566 void *pos, *sof, *eof;
568 len = urb->iso_frame_desc[i].actual_length;
569 status = urb->iso_frame_desc[i].status;
570 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
573 DBG(3, "Error in isochronous frame");
574 (*f)->state = F_ERROR;
578 PDBGG("Isochrnous frame: length %u, #%u i", len, i);
581 sof = sn9c102_find_sof_header(cam, pos, len);
583 eof = sn9c102_find_eof_header(cam, pos, len);
584 if ((*f)->state == F_GRABBING) {
589 img = (eof > pos) ? eof - pos - 1 : 0;
591 if ((*f)->buf.bytesused+img > imagesize) {
592 u32 b = (*f)->buf.bytesused + img -
594 img = imagesize - (*f)->buf.bytesused;
595 DBG(3, "Expected EOF not found: "
598 DBG(3, "Exceeded limit: +%u "
599 "bytes", (unsigned)(b));
602 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
605 if ((*f)->buf.bytesused == 0)
606 do_gettimeofday(&(*f)->buf.timestamp);
608 (*f)->buf.bytesused += img;
610 if ((*f)->buf.bytesused == imagesize ||
611 (cam->sensor->pix_format.pixelformat ==
612 V4L2_PIX_FMT_SN9C10X && eof)) {
613 u32 b = (*f)->buf.bytesused;
614 (*f)->state = F_DONE;
615 (*f)->buf.sequence= ++cam->frame_count;
616 spin_lock(&cam->queue_lock);
617 list_move_tail(&(*f)->frame,
619 if (!list_empty(&cam->inqueue))
622 struct sn9c102_frame_t,
626 spin_unlock(&cam->queue_lock);
627 memcpy(cam->sysfs.frame_header,
628 cam->sof_header, soflen);
629 DBG(3, "Video frame captured: %lu "
630 "bytes", (unsigned long)(b));
636 (*f)->state = F_ERROR;
637 DBG(3, "Not expected EOF after %lu "
638 "bytes of image data",
640 ((*f)->buf.bytesused));
647 DBG(3, "EOF without SOF");
651 PDBGG("Ignoring pointless isochronous frame");
655 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
657 (*f)->state = F_GRABBING;
658 (*f)->buf.bytesused = 0;
661 DBG(3, "SOF detected: new video frame");
665 } else if ((*f)->state == F_GRABBING) {
666 eof = sn9c102_find_eof_header(cam, pos, len);
667 if (eof && eof < sof)
668 goto end_of_frame; /* (1) */
670 if (cam->sensor->pix_format.pixelformat ==
671 V4L2_PIX_FMT_SN9C10X) {
675 DBG(3, "SOF before expected EOF after "
676 "%lu bytes of image data",
678 ((*f)->buf.bytesused));
686 urb->dev = cam->usbdev;
687 err = usb_submit_urb(urb, GFP_ATOMIC);
688 if (err < 0 && err != -EPERM) {
689 cam->state |= DEV_MISCONFIGURED;
690 DBG(1, "usb_submit_urb() failed");
693 wake_up_interruptible(&cam->wait_frame);
697 static int sn9c102_start_transfer(struct sn9c102_device* cam)
699 struct usb_device *udev = cam->usbdev;
701 const unsigned int sn9c102_wMaxPacketSize[] = {0, 128, 256, 384, 512,
702 680, 800, 900, 1023};
703 const unsigned int sn9c103_wMaxPacketSize[] = {0, 128, 256, 384, 512,
704 680, 800, 900, 1003};
705 const unsigned int psz = (cam->bridge == BRIDGE_SN9C103) ?
706 sn9c103_wMaxPacketSize[SN9C102_ALTERNATE_SETTING] :
707 sn9c102_wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
711 for (i = 0; i < SN9C102_URBS; i++) {
712 cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
714 if (!cam->transfer_buffer[i]) {
716 DBG(1, "Not enough memory");
721 for (i = 0; i < SN9C102_URBS; i++) {
722 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
726 DBG(1, "usb_alloc_urb() failed");
731 urb->pipe = usb_rcvisocpipe(udev, 1);
732 urb->transfer_flags = URB_ISO_ASAP;
733 urb->number_of_packets = SN9C102_ISO_PACKETS;
734 urb->complete = sn9c102_urb_complete;
735 urb->transfer_buffer = cam->transfer_buffer[i];
736 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
738 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
739 urb->iso_frame_desc[j].offset = psz * j;
740 urb->iso_frame_desc[j].length = psz;
745 if (!(cam->reg[0x01] & 0x04)) {
746 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
749 DBG(1, "I/O hardware error");
754 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
756 DBG(1, "usb_set_interface() failed");
760 cam->frame_current = NULL;
762 for (i = 0; i < SN9C102_URBS; i++) {
763 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
765 for (j = i-1; j >= 0; j--)
766 usb_kill_urb(cam->urb[j]);
767 DBG(1, "usb_submit_urb() failed, error %d", err);
775 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
776 usb_free_urb(cam->urb[i]);
779 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
780 kfree(cam->transfer_buffer[i]);
786 static int sn9c102_stop_transfer(struct sn9c102_device* cam)
788 struct usb_device *udev = cam->usbdev;
792 if (cam->state & DEV_DISCONNECTED)
795 for (i = SN9C102_URBS-1; i >= 0; i--) {
796 usb_kill_urb(cam->urb[i]);
797 usb_free_urb(cam->urb[i]);
798 kfree(cam->transfer_buffer[i]);
801 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
803 DBG(3, "usb_set_interface() failed");
809 static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
813 cam->stream = STREAM_INTERRUPT;
814 err = wait_event_timeout(cam->wait_stream,
815 (cam->stream == STREAM_OFF) ||
816 (cam->state & DEV_DISCONNECTED),
817 SN9C102_URB_TIMEOUT);
818 if (cam->state & DEV_DISCONNECTED)
821 cam->state |= DEV_MISCONFIGURED;
822 DBG(1, "The camera is misconfigured. To use it, close and "
823 "open /dev/video%d again.", cam->v4ldev->minor);
830 /*****************************************************************************/
832 #ifdef CONFIG_VIDEO_ADV_DEBUG
833 static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
840 strncpy(str, buff, len);
843 strncpy(str, buff, 4);
847 val = simple_strtoul(str, &endp, 0);
851 *count = (ssize_t)(endp - str);
852 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
859 NOTE 1: being inside one of the following methods implies that the v4l
860 device exists for sure (see kobjects and reference counters)
861 NOTE 2: buffers are PAGE_SIZE long
864 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
866 struct sn9c102_device* cam;
869 if (down_interruptible(&sn9c102_sysfs_lock))
872 cam = video_get_drvdata(to_video_device(cd));
874 up(&sn9c102_sysfs_lock);
878 count = sprintf(buf, "%u\n", cam->sysfs.reg);
880 up(&sn9c102_sysfs_lock);
887 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
889 struct sn9c102_device* cam;
893 if (down_interruptible(&sn9c102_sysfs_lock))
896 cam = video_get_drvdata(to_video_device(cd));
898 up(&sn9c102_sysfs_lock);
902 index = sn9c102_strtou8(buf, len, &count);
903 if (index > 0x1f || !count) {
904 up(&sn9c102_sysfs_lock);
908 cam->sysfs.reg = index;
910 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg);
911 DBG(3, "Written bytes: %zd", count);
913 up(&sn9c102_sysfs_lock);
919 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
921 struct sn9c102_device* cam;
925 if (down_interruptible(&sn9c102_sysfs_lock))
928 cam = video_get_drvdata(to_video_device(cd));
930 up(&sn9c102_sysfs_lock);
934 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
935 up(&sn9c102_sysfs_lock);
939 count = sprintf(buf, "%d\n", val);
941 DBG(3, "Read bytes: %zd", count);
943 up(&sn9c102_sysfs_lock);
950 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
952 struct sn9c102_device* cam;
957 if (down_interruptible(&sn9c102_sysfs_lock))
960 cam = video_get_drvdata(to_video_device(cd));
962 up(&sn9c102_sysfs_lock);
966 value = sn9c102_strtou8(buf, len, &count);
968 up(&sn9c102_sysfs_lock);
972 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
974 up(&sn9c102_sysfs_lock);
978 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
979 cam->sysfs.reg, value);
980 DBG(3, "Written bytes: %zd", count);
982 up(&sn9c102_sysfs_lock);
988 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
990 struct sn9c102_device* cam;
993 if (down_interruptible(&sn9c102_sysfs_lock))
996 cam = video_get_drvdata(to_video_device(cd));
998 up(&sn9c102_sysfs_lock);
1002 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
1004 DBG(3, "Read bytes: %zd", count);
1006 up(&sn9c102_sysfs_lock);
1013 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1015 struct sn9c102_device* cam;
1019 if (down_interruptible(&sn9c102_sysfs_lock))
1020 return -ERESTARTSYS;
1022 cam = video_get_drvdata(to_video_device(cd));
1024 up(&sn9c102_sysfs_lock);
1028 index = sn9c102_strtou8(buf, len, &count);
1030 up(&sn9c102_sysfs_lock);
1034 cam->sysfs.i2c_reg = index;
1036 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1037 DBG(3, "Written bytes: %zd", count);
1039 up(&sn9c102_sysfs_lock);
1045 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1047 struct sn9c102_device* cam;
1051 if (down_interruptible(&sn9c102_sysfs_lock))
1052 return -ERESTARTSYS;
1054 cam = video_get_drvdata(to_video_device(cd));
1056 up(&sn9c102_sysfs_lock);
1060 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_READ)) {
1061 up(&sn9c102_sysfs_lock);
1065 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1066 up(&sn9c102_sysfs_lock);
1070 count = sprintf(buf, "%d\n", val);
1072 DBG(3, "Read bytes: %zd", count);
1074 up(&sn9c102_sysfs_lock);
1081 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1083 struct sn9c102_device* cam;
1088 if (down_interruptible(&sn9c102_sysfs_lock))
1089 return -ERESTARTSYS;
1091 cam = video_get_drvdata(to_video_device(cd));
1093 up(&sn9c102_sysfs_lock);
1097 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_WRITE)) {
1098 up(&sn9c102_sysfs_lock);
1102 value = sn9c102_strtou8(buf, len, &count);
1104 up(&sn9c102_sysfs_lock);
1108 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1110 up(&sn9c102_sysfs_lock);
1114 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1115 cam->sysfs.i2c_reg, value);
1116 DBG(3, "Written bytes: %zd", count);
1118 up(&sn9c102_sysfs_lock);
1125 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1127 struct sn9c102_device* cam;
1128 enum sn9c102_bridge bridge;
1133 if (down_interruptible(&sn9c102_sysfs_lock))
1134 return -ERESTARTSYS;
1136 cam = video_get_drvdata(to_video_device(cd));
1138 up(&sn9c102_sysfs_lock);
1142 bridge = cam->bridge;
1144 up(&sn9c102_sysfs_lock);
1146 value = sn9c102_strtou8(buf, len, &count);
1151 case BRIDGE_SN9C101:
1152 case BRIDGE_SN9C102:
1155 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1156 res = sn9c102_store_val(cd, buf, len);
1158 case BRIDGE_SN9C103:
1161 if ((res = sn9c102_store_reg(cd, "0x04", 4)) >= 0)
1162 res = sn9c102_store_val(cd, buf, len);
1171 sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1177 value = sn9c102_strtou8(buf, len, &count);
1178 if (!count || value > 0x7f)
1181 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1182 res = sn9c102_store_val(cd, buf, len);
1189 sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1195 value = sn9c102_strtou8(buf, len, &count);
1196 if (!count || value > 0x7f)
1199 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1200 res = sn9c102_store_val(cd, buf, len);
1206 static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1208 struct sn9c102_device* cam;
1211 cam = video_get_drvdata(to_video_device(cd));
1215 count = sizeof(cam->sysfs.frame_header);
1216 memcpy(buf, cam->sysfs.frame_header, count);
1218 DBG(3, "Frame header, read bytes: %zd", count);
1224 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1225 sn9c102_show_reg, sn9c102_store_reg);
1226 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1227 sn9c102_show_val, sn9c102_store_val);
1228 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1229 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1230 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1231 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1232 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1233 static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1234 static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1235 static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
1236 sn9c102_show_frame_header, NULL);
1239 static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1241 struct video_device *v4ldev = cam->v4ldev;
1243 video_device_create_file(v4ldev, &class_device_attr_reg);
1244 video_device_create_file(v4ldev, &class_device_attr_val);
1245 video_device_create_file(v4ldev, &class_device_attr_frame_header);
1246 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
1247 video_device_create_file(v4ldev, &class_device_attr_green);
1248 else if (cam->bridge == BRIDGE_SN9C103) {
1249 video_device_create_file(v4ldev, &class_device_attr_blue);
1250 video_device_create_file(v4ldev, &class_device_attr_red);
1252 if (cam->sensor && cam->sensor->sysfs_ops) {
1253 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1254 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1257 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1259 /*****************************************************************************/
1262 sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
1266 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
1267 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, 0x18);
1269 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, 0x18);
1271 return err ? -EIO : 0;
1276 sn9c102_set_compression(struct sn9c102_device* cam,
1277 struct v4l2_jpegcompression* compression)
1281 if (compression->quality == 0)
1282 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, 0x17);
1283 else if (compression->quality == 1)
1284 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, 0x17);
1286 return err ? -EIO : 0;
1290 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1296 r = cam->reg[0x18] & 0xcf;
1297 else if (scale == 2) {
1298 r = cam->reg[0x18] & 0xcf;
1300 } else if (scale == 4)
1301 r = cam->reg[0x18] | 0x20;
1303 err += sn9c102_write_reg(cam, r, 0x18);
1307 PDBGG("Scaling factor: %u", scale);
1313 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1315 struct sn9c102_sensor* s = cam->sensor;
1316 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1317 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1318 h_size = (u8)(rect->width / 16),
1319 v_size = (u8)(rect->height / 16);
1322 err += sn9c102_write_reg(cam, h_start, 0x12);
1323 err += sn9c102_write_reg(cam, v_start, 0x13);
1324 err += sn9c102_write_reg(cam, h_size, 0x15);
1325 err += sn9c102_write_reg(cam, v_size, 0x16);
1329 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1330 "%u %u %u %u", h_start, v_start, h_size, v_size);
1336 static int sn9c102_init(struct sn9c102_device* cam)
1338 struct sn9c102_sensor* s = cam->sensor;
1339 struct v4l2_control ctrl;
1340 struct v4l2_queryctrl *qctrl;
1341 struct v4l2_rect* rect;
1345 if (!(cam->state & DEV_INITIALIZED)) {
1346 init_waitqueue_head(&cam->open);
1348 rect = &(s->cropcap.defrect);
1349 } else { /* use current values */
1354 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1355 err += sn9c102_set_crop(cam, rect);
1362 DBG(3, "Sensor initialization failed");
1367 if (!(cam->state & DEV_INITIALIZED))
1368 cam->compression.quality = cam->reg[0x17] & 0x01 ? 0 : 1;
1370 err += sn9c102_set_compression(cam, &cam->compression);
1371 err += sn9c102_set_pix_format(cam, &s->pix_format);
1372 if (s->set_pix_format)
1373 err += s->set_pix_format(cam, &s->pix_format);
1377 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1378 DBG(3, "Compressed video format is active, quality %d",
1379 cam->compression.quality);
1381 DBG(3, "Uncompressed video format is active");
1384 if ((err = s->set_crop(cam, rect))) {
1385 DBG(3, "set_crop() failed");
1390 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1391 if (s->qctrl[i].id != 0 &&
1392 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1393 ctrl.id = s->qctrl[i].id;
1394 ctrl.value = qctrl[i].default_value;
1395 err = s->set_ctrl(cam, &ctrl);
1397 DBG(3, "Set %s control failed",
1401 DBG(3, "Image sensor supports '%s' control",
1406 if (!(cam->state & DEV_INITIALIZED)) {
1407 init_MUTEX(&cam->fileop_sem);
1408 spin_lock_init(&cam->queue_lock);
1409 init_waitqueue_head(&cam->wait_frame);
1410 init_waitqueue_head(&cam->wait_stream);
1411 cam->nreadbuffers = 2;
1412 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1413 memcpy(&(s->_rect), &(s->cropcap.defrect),
1414 sizeof(struct v4l2_rect));
1415 cam->state |= DEV_INITIALIZED;
1418 DBG(2, "Initialization succeeded");
1423 static void sn9c102_release_resources(struct sn9c102_device* cam)
1425 down(&sn9c102_sysfs_lock);
1427 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1428 video_set_drvdata(cam->v4ldev, NULL);
1429 video_unregister_device(cam->v4ldev);
1431 up(&sn9c102_sysfs_lock);
1433 kfree(cam->control_buffer);
1436 /*****************************************************************************/
1438 static int sn9c102_open(struct inode* inode, struct file* filp)
1440 struct sn9c102_device* cam;
1444 This is the only safe way to prevent race conditions with
1447 if (!down_read_trylock(&sn9c102_disconnect))
1448 return -ERESTARTSYS;
1450 cam = video_get_drvdata(video_devdata(filp));
1452 if (down_interruptible(&cam->dev_sem)) {
1453 up_read(&sn9c102_disconnect);
1454 return -ERESTARTSYS;
1458 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1459 if ((filp->f_flags & O_NONBLOCK) ||
1460 (filp->f_flags & O_NDELAY)) {
1465 err = wait_event_interruptible_exclusive(cam->open,
1466 cam->state & DEV_DISCONNECTED
1469 up_read(&sn9c102_disconnect);
1472 if (cam->state & DEV_DISCONNECTED) {
1473 up_read(&sn9c102_disconnect);
1476 down(&cam->dev_sem);
1480 if (cam->state & DEV_MISCONFIGURED) {
1481 err = sn9c102_init(cam);
1483 DBG(1, "Initialization failed again. "
1484 "I will retry on next open().");
1487 cam->state &= ~DEV_MISCONFIGURED;
1490 if ((err = sn9c102_start_transfer(cam)))
1493 filp->private_data = cam;
1496 cam->stream = STREAM_OFF;
1498 cam->frame_count = 0;
1499 sn9c102_empty_framequeues(cam);
1501 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1505 up_read(&sn9c102_disconnect);
1510 static int sn9c102_release(struct inode* inode, struct file* filp)
1512 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1514 down(&cam->dev_sem); /* prevent disconnect() to be called */
1516 sn9c102_stop_transfer(cam);
1518 sn9c102_release_buffers(cam);
1520 if (cam->state & DEV_DISCONNECTED) {
1521 sn9c102_release_resources(cam);
1528 wake_up_interruptible_nr(&cam->open, 1);
1530 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1539 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1541 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1542 struct sn9c102_frame_t* f, * i;
1543 unsigned long lock_flags;
1546 if (down_interruptible(&cam->fileop_sem))
1547 return -ERESTARTSYS;
1549 if (cam->state & DEV_DISCONNECTED) {
1550 DBG(1, "Device not present");
1551 up(&cam->fileop_sem);
1555 if (cam->state & DEV_MISCONFIGURED) {
1556 DBG(1, "The camera is misconfigured. Close and open it "
1558 up(&cam->fileop_sem);
1562 if (cam->io == IO_MMAP) {
1563 DBG(3, "Close and open the device again to choose "
1565 up(&cam->fileop_sem);
1569 if (cam->io == IO_NONE) {
1570 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1571 DBG(1, "read() failed, not enough memory");
1572 up(&cam->fileop_sem);
1576 cam->stream = STREAM_ON;
1579 if (list_empty(&cam->inqueue)) {
1580 if (!list_empty(&cam->outqueue))
1581 sn9c102_empty_framequeues(cam);
1582 sn9c102_queue_unusedframes(cam);
1586 up(&cam->fileop_sem);
1590 if (list_empty(&cam->outqueue)) {
1591 if (filp->f_flags & O_NONBLOCK) {
1592 up(&cam->fileop_sem);
1595 err = wait_event_interruptible
1597 (!list_empty(&cam->outqueue)) ||
1598 (cam->state & DEV_DISCONNECTED) ||
1599 (cam->state & DEV_MISCONFIGURED) );
1601 up(&cam->fileop_sem);
1604 if (cam->state & DEV_DISCONNECTED) {
1605 up(&cam->fileop_sem);
1608 if (cam->state & DEV_MISCONFIGURED) {
1609 up(&cam->fileop_sem);
1614 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1616 if (count > f->buf.bytesused)
1617 count = f->buf.bytesused;
1619 if (copy_to_user(buf, f->bufmem, count)) {
1626 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1627 list_for_each_entry(i, &cam->outqueue, frame)
1628 i->state = F_UNUSED;
1629 INIT_LIST_HEAD(&cam->outqueue);
1630 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1632 sn9c102_queue_unusedframes(cam);
1634 PDBGG("Frame #%lu, bytes read: %zu",
1635 (unsigned long)f->buf.index, count);
1637 up(&cam->fileop_sem);
1643 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1645 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1646 struct sn9c102_frame_t* f;
1647 unsigned long lock_flags;
1648 unsigned int mask = 0;
1650 if (down_interruptible(&cam->fileop_sem))
1653 if (cam->state & DEV_DISCONNECTED) {
1654 DBG(1, "Device not present");
1658 if (cam->state & DEV_MISCONFIGURED) {
1659 DBG(1, "The camera is misconfigured. Close and open it "
1664 if (cam->io == IO_NONE) {
1665 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1667 DBG(1, "poll() failed, not enough memory");
1671 cam->stream = STREAM_ON;
1674 if (cam->io == IO_READ) {
1675 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1676 list_for_each_entry(f, &cam->outqueue, frame)
1677 f->state = F_UNUSED;
1678 INIT_LIST_HEAD(&cam->outqueue);
1679 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1680 sn9c102_queue_unusedframes(cam);
1683 poll_wait(filp, &cam->wait_frame, wait);
1685 if (!list_empty(&cam->outqueue))
1686 mask |= POLLIN | POLLRDNORM;
1688 up(&cam->fileop_sem);
1693 up(&cam->fileop_sem);
1698 static void sn9c102_vm_open(struct vm_area_struct* vma)
1700 struct sn9c102_frame_t* f = vma->vm_private_data;
1705 static void sn9c102_vm_close(struct vm_area_struct* vma)
1707 /* NOTE: buffers are not freed here */
1708 struct sn9c102_frame_t* f = vma->vm_private_data;
1713 static struct vm_operations_struct sn9c102_vm_ops = {
1714 .open = sn9c102_vm_open,
1715 .close = sn9c102_vm_close,
1719 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1721 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1722 unsigned long size = vma->vm_end - vma->vm_start,
1723 start = vma->vm_start;
1727 if (down_interruptible(&cam->fileop_sem))
1728 return -ERESTARTSYS;
1730 if (cam->state & DEV_DISCONNECTED) {
1731 DBG(1, "Device not present");
1732 up(&cam->fileop_sem);
1736 if (cam->state & DEV_MISCONFIGURED) {
1737 DBG(1, "The camera is misconfigured. Close and open it "
1739 up(&cam->fileop_sem);
1743 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1744 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1745 up(&cam->fileop_sem);
1749 for (i = 0; i < cam->nbuffers; i++) {
1750 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1753 if (i == cam->nbuffers) {
1754 up(&cam->fileop_sem);
1758 vma->vm_flags |= VM_IO;
1759 vma->vm_flags |= VM_RESERVED;
1761 pos = cam->frame[i].bufmem;
1762 while (size > 0) { /* size is page-aligned */
1763 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1764 up(&cam->fileop_sem);
1772 vma->vm_ops = &sn9c102_vm_ops;
1773 vma->vm_private_data = &cam->frame[i];
1775 sn9c102_vm_open(vma);
1777 up(&cam->fileop_sem);
1782 /*****************************************************************************/
1785 sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
1787 struct v4l2_capability cap = {
1788 .driver = "sn9c102",
1789 .version = SN9C102_MODULE_VERSION_CODE,
1790 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1794 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1795 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1796 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1797 sizeof(cap.bus_info));
1799 if (copy_to_user(arg, &cap, sizeof(cap)))
1807 sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
1809 struct v4l2_input i;
1811 if (copy_from_user(&i, arg, sizeof(i)))
1817 memset(&i, 0, sizeof(i));
1818 strcpy(i.name, "Camera");
1820 if (copy_to_user(arg, &i, sizeof(i)))
1828 sn9c102_vidioc_gs_input(struct sn9c102_device* cam, void __user * arg)
1832 if (copy_from_user(&index, arg, sizeof(index)))
1843 sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
1845 struct sn9c102_sensor* s = cam->sensor;
1846 struct v4l2_queryctrl qc;
1849 if (copy_from_user(&qc, arg, sizeof(qc)))
1852 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1853 if (qc.id && qc.id == s->qctrl[i].id) {
1854 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1855 if (copy_to_user(arg, &qc, sizeof(qc)))
1865 sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
1867 struct sn9c102_sensor* s = cam->sensor;
1868 struct v4l2_control ctrl;
1872 if (!s->get_ctrl && !s->set_ctrl)
1875 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1879 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1880 if (ctrl.id && ctrl.id == s->qctrl[i].id) {
1881 ctrl.value = s->_qctrl[i].default_value;
1886 err = s->get_ctrl(cam, &ctrl);
1889 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1897 sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1899 struct sn9c102_sensor* s = cam->sensor;
1900 struct v4l2_control ctrl;
1907 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1910 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1911 if (ctrl.id == s->qctrl[i].id) {
1912 if (ctrl.value < s->qctrl[i].minimum ||
1913 ctrl.value > s->qctrl[i].maximum)
1915 ctrl.value -= ctrl.value % s->qctrl[i].step;
1919 if ((err = s->set_ctrl(cam, &ctrl)))
1922 s->_qctrl[i].default_value = ctrl.value;
1924 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
1925 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
1932 sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
1934 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1936 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1937 cc->pixelaspect.numerator = 1;
1938 cc->pixelaspect.denominator = 1;
1940 if (copy_to_user(arg, cc, sizeof(*cc)))
1948 sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
1950 struct sn9c102_sensor* s = cam->sensor;
1951 struct v4l2_crop crop = {
1952 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1955 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1957 if (copy_to_user(arg, &crop, sizeof(crop)))
1965 sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
1967 struct sn9c102_sensor* s = cam->sensor;
1968 struct v4l2_crop crop;
1969 struct v4l2_rect* rect;
1970 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1971 struct v4l2_pix_format* pix_format = &(s->pix_format);
1973 const enum sn9c102_stream_state stream = cam->stream;
1974 const u32 nbuffers = cam->nbuffers;
1978 if (copy_from_user(&crop, arg, sizeof(crop)))
1983 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1986 if (cam->module_param.force_munmap)
1987 for (i = 0; i < cam->nbuffers; i++)
1988 if (cam->frame[i].vma_use_count) {
1989 DBG(3, "VIDIOC_S_CROP failed. "
1990 "Unmap the buffers first.");
1994 /* Preserve R,G or B origin */
1995 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1996 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1998 if (rect->width < 16)
2000 if (rect->height < 16)
2002 if (rect->width > bounds->width)
2003 rect->width = bounds->width;
2004 if (rect->height > bounds->height)
2005 rect->height = bounds->height;
2006 if (rect->left < bounds->left)
2007 rect->left = bounds->left;
2008 if (rect->top < bounds->top)
2009 rect->top = bounds->top;
2010 if (rect->left + rect->width > bounds->left + bounds->width)
2011 rect->left = bounds->left+bounds->width - rect->width;
2012 if (rect->top + rect->height > bounds->top + bounds->height)
2013 rect->top = bounds->top+bounds->height - rect->height;
2015 rect->width &= ~15L;
2016 rect->height &= ~15L;
2018 if (SN9C102_PRESERVE_IMGSCALE) {
2019 /* Calculate the actual scaling factor */
2021 a = rect->width * rect->height;
2022 b = pix_format->width * pix_format->height;
2023 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2027 if (cam->stream == STREAM_ON)
2028 if ((err = sn9c102_stream_interrupt(cam)))
2031 if (copy_to_user(arg, &crop, sizeof(crop))) {
2032 cam->stream = stream;
2036 if (cam->module_param.force_munmap || cam->io == IO_READ)
2037 sn9c102_release_buffers(cam);
2039 err = sn9c102_set_crop(cam, rect);
2041 err += s->set_crop(cam, rect);
2042 err += sn9c102_set_scale(cam, scale);
2044 if (err) { /* atomic, no rollback in ioctl() */
2045 cam->state |= DEV_MISCONFIGURED;
2046 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2047 "use the camera, close and open /dev/video%d again.",
2048 cam->v4ldev->minor);
2052 s->pix_format.width = rect->width/scale;
2053 s->pix_format.height = rect->height/scale;
2054 memcpy(&(s->_rect), rect, sizeof(*rect));
2056 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2057 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2058 cam->state |= DEV_MISCONFIGURED;
2059 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2060 "use the camera, close and open /dev/video%d again.",
2061 cam->v4ldev->minor);
2065 if (cam->io == IO_READ)
2066 sn9c102_empty_framequeues(cam);
2067 else if (cam->module_param.force_munmap)
2068 sn9c102_requeue_outqueue(cam);
2070 cam->stream = stream;
2077 sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
2079 struct v4l2_fmtdesc fmtd;
2081 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
2084 if (fmtd.index == 0) {
2085 strcpy(fmtd.description, "bayer rgb");
2086 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2087 } else if (fmtd.index == 1) {
2088 strcpy(fmtd.description, "compressed");
2089 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2090 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2094 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2095 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2097 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
2105 sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2107 struct v4l2_format format;
2108 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
2110 if (copy_from_user(&format, arg, sizeof(format)))
2113 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2116 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X)
2117 ? 0 : (pfmt->width * pfmt->priv) / 8;
2118 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2119 pfmt->field = V4L2_FIELD_NONE;
2120 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2122 if (copy_to_user(arg, &format, sizeof(format)))
2130 sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2133 struct sn9c102_sensor* s = cam->sensor;
2134 struct v4l2_format format;
2135 struct v4l2_pix_format* pix;
2136 struct v4l2_pix_format* pfmt = &(s->pix_format);
2137 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2138 struct v4l2_rect rect;
2140 const enum sn9c102_stream_state stream = cam->stream;
2141 const u32 nbuffers = cam->nbuffers;
2145 if (copy_from_user(&format, arg, sizeof(format)))
2148 pix = &(format.fmt.pix);
2150 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2153 memcpy(&rect, &(s->_rect), sizeof(rect));
2155 { /* calculate the actual scaling factor */
2157 a = rect.width * rect.height;
2158 b = pix->width * pix->height;
2159 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2162 rect.width = scale * pix->width;
2163 rect.height = scale * pix->height;
2165 if (rect.width < 16)
2167 if (rect.height < 16)
2169 if (rect.width > bounds->left + bounds->width - rect.left)
2170 rect.width = bounds->left + bounds->width - rect.left;
2171 if (rect.height > bounds->top + bounds->height - rect.top)
2172 rect.height = bounds->top + bounds->height - rect.top;
2175 rect.height &= ~15L;
2177 { /* adjust the scaling factor */
2179 a = rect.width * rect.height;
2180 b = pix->width * pix->height;
2181 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2184 pix->width = rect.width / scale;
2185 pix->height = rect.height / scale;
2187 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2188 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2189 pix->pixelformat = pfmt->pixelformat;
2190 pix->priv = pfmt->priv; /* bpp */
2191 pix->colorspace = pfmt->colorspace;
2192 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
2193 ? 0 : (pix->width * pix->priv) / 8;
2194 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2195 pix->field = V4L2_FIELD_NONE;
2197 if (cmd == VIDIOC_TRY_FMT) {
2198 if (copy_to_user(arg, &format, sizeof(format)))
2203 if (cam->module_param.force_munmap)
2204 for (i = 0; i < cam->nbuffers; i++)
2205 if (cam->frame[i].vma_use_count) {
2206 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2211 if (cam->stream == STREAM_ON)
2212 if ((err = sn9c102_stream_interrupt(cam)))
2215 if (copy_to_user(arg, &format, sizeof(format))) {
2216 cam->stream = stream;
2220 if (cam->module_param.force_munmap || cam->io == IO_READ)
2221 sn9c102_release_buffers(cam);
2223 err += sn9c102_set_pix_format(cam, pix);
2224 err += sn9c102_set_crop(cam, &rect);
2225 if (s->set_pix_format)
2226 err += s->set_pix_format(cam, pix);
2228 err += s->set_crop(cam, &rect);
2229 err += sn9c102_set_scale(cam, scale);
2231 if (err) { /* atomic, no rollback in ioctl() */
2232 cam->state |= DEV_MISCONFIGURED;
2233 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2234 "use the camera, close and open /dev/video%d again.",
2235 cam->v4ldev->minor);
2239 memcpy(pfmt, pix, sizeof(*pix));
2240 memcpy(&(s->_rect), &rect, sizeof(rect));
2242 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2243 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2244 cam->state |= DEV_MISCONFIGURED;
2245 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2246 "use the camera, close and open /dev/video%d again.",
2247 cam->v4ldev->minor);
2251 if (cam->io == IO_READ)
2252 sn9c102_empty_framequeues(cam);
2253 else if (cam->module_param.force_munmap)
2254 sn9c102_requeue_outqueue(cam);
2256 cam->stream = stream;
2263 sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2265 if (copy_to_user(arg, &cam->compression,
2266 sizeof(cam->compression)))
2274 sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2276 struct v4l2_jpegcompression jc;
2277 const enum sn9c102_stream_state stream = cam->stream;
2280 if (copy_from_user(&jc, arg, sizeof(jc)))
2283 if (jc.quality != 0 && jc.quality != 1)
2286 if (cam->stream == STREAM_ON)
2287 if ((err = sn9c102_stream_interrupt(cam)))
2290 err += sn9c102_set_compression(cam, &jc);
2291 if (err) { /* atomic, no rollback in ioctl() */
2292 cam->state |= DEV_MISCONFIGURED;
2293 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2294 "problems. To use the camera, close and open "
2295 "/dev/video%d again.", cam->v4ldev->minor);
2299 cam->compression.quality = jc.quality;
2301 cam->stream = stream;
2308 sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
2310 struct v4l2_requestbuffers rb;
2314 if (copy_from_user(&rb, arg, sizeof(rb)))
2317 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2318 rb.memory != V4L2_MEMORY_MMAP)
2321 if (cam->io == IO_READ) {
2322 DBG(3, "Close and open the device again to choose the mmap "
2327 for (i = 0; i < cam->nbuffers; i++)
2328 if (cam->frame[i].vma_use_count) {
2329 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2334 if (cam->stream == STREAM_ON)
2335 if ((err = sn9c102_stream_interrupt(cam)))
2338 sn9c102_empty_framequeues(cam);
2340 sn9c102_release_buffers(cam);
2342 rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
2344 if (copy_to_user(arg, &rb, sizeof(rb))) {
2345 sn9c102_release_buffers(cam);
2350 cam->io = rb.count ? IO_MMAP : IO_NONE;
2357 sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
2359 struct v4l2_buffer b;
2361 if (copy_from_user(&b, arg, sizeof(b)))
2364 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2365 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2368 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2370 if (cam->frame[b.index].vma_use_count)
2371 b.flags |= V4L2_BUF_FLAG_MAPPED;
2373 if (cam->frame[b.index].state == F_DONE)
2374 b.flags |= V4L2_BUF_FLAG_DONE;
2375 else if (cam->frame[b.index].state != F_UNUSED)
2376 b.flags |= V4L2_BUF_FLAG_QUEUED;
2378 if (copy_to_user(arg, &b, sizeof(b)))
2386 sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
2388 struct v4l2_buffer b;
2389 unsigned long lock_flags;
2391 if (copy_from_user(&b, arg, sizeof(b)))
2394 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2395 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2398 if (cam->frame[b.index].state != F_UNUSED)
2401 cam->frame[b.index].state = F_QUEUED;
2403 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2404 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2405 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2407 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2414 sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2417 struct v4l2_buffer b;
2418 struct sn9c102_frame_t *f;
2419 unsigned long lock_flags;
2422 if (copy_from_user(&b, arg, sizeof(b)))
2425 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2428 if (list_empty(&cam->outqueue)) {
2429 if (cam->stream == STREAM_OFF)
2431 if (filp->f_flags & O_NONBLOCK)
2433 err = wait_event_interruptible
2435 (!list_empty(&cam->outqueue)) ||
2436 (cam->state & DEV_DISCONNECTED) ||
2437 (cam->state & DEV_MISCONFIGURED) );
2440 if (cam->state & DEV_DISCONNECTED)
2442 if (cam->state & DEV_MISCONFIGURED)
2446 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2447 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
2448 list_del(cam->outqueue.next);
2449 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2451 f->state = F_UNUSED;
2453 memcpy(&b, &f->buf, sizeof(b));
2454 if (f->vma_use_count)
2455 b.flags |= V4L2_BUF_FLAG_MAPPED;
2457 if (copy_to_user(arg, &b, sizeof(b)))
2460 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2467 sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
2471 if (copy_from_user(&type, arg, sizeof(type)))
2474 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2477 if (list_empty(&cam->inqueue))
2480 cam->stream = STREAM_ON;
2482 DBG(3, "Stream on");
2489 sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
2493 if (copy_from_user(&type, arg, sizeof(type)))
2496 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2499 if (cam->stream == STREAM_ON)
2500 if ((err = sn9c102_stream_interrupt(cam)))
2503 sn9c102_empty_framequeues(cam);
2505 DBG(3, "Stream off");
2512 sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
2514 struct v4l2_streamparm sp;
2516 if (copy_from_user(&sp, arg, sizeof(sp)))
2519 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2522 sp.parm.capture.extendedmode = 0;
2523 sp.parm.capture.readbuffers = cam->nreadbuffers;
2525 if (copy_to_user(arg, &sp, sizeof(sp)))
2533 sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
2535 struct v4l2_streamparm sp;
2537 if (copy_from_user(&sp, arg, sizeof(sp)))
2540 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2543 sp.parm.capture.extendedmode = 0;
2545 if (sp.parm.capture.readbuffers == 0)
2546 sp.parm.capture.readbuffers = cam->nreadbuffers;
2548 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2549 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2551 if (copy_to_user(arg, &sp, sizeof(sp)))
2554 cam->nreadbuffers = sp.parm.capture.readbuffers;
2560 static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
2561 unsigned int cmd, void __user * arg)
2563 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2567 case VIDIOC_QUERYCAP:
2568 return sn9c102_vidioc_querycap(cam, arg);
2570 case VIDIOC_ENUMINPUT:
2571 return sn9c102_vidioc_enuminput(cam, arg);
2573 case VIDIOC_G_INPUT:
2574 case VIDIOC_S_INPUT:
2575 return sn9c102_vidioc_gs_input(cam, arg);
2577 case VIDIOC_QUERYCTRL:
2578 return sn9c102_vidioc_query_ctrl(cam, arg);
2581 return sn9c102_vidioc_g_ctrl(cam, arg);
2583 case VIDIOC_S_CTRL_OLD:
2585 return sn9c102_vidioc_s_ctrl(cam, arg);
2587 case VIDIOC_CROPCAP_OLD:
2588 case VIDIOC_CROPCAP:
2589 return sn9c102_vidioc_cropcap(cam, arg);
2592 return sn9c102_vidioc_g_crop(cam, arg);
2595 return sn9c102_vidioc_s_crop(cam, arg);
2597 case VIDIOC_ENUM_FMT:
2598 return sn9c102_vidioc_enum_fmt(cam, arg);
2601 return sn9c102_vidioc_g_fmt(cam, arg);
2603 case VIDIOC_TRY_FMT:
2605 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
2607 case VIDIOC_G_JPEGCOMP:
2608 return sn9c102_vidioc_g_jpegcomp(cam, arg);
2610 case VIDIOC_S_JPEGCOMP:
2611 return sn9c102_vidioc_s_jpegcomp(cam, arg);
2613 case VIDIOC_REQBUFS:
2614 return sn9c102_vidioc_reqbufs(cam, arg);
2616 case VIDIOC_QUERYBUF:
2617 return sn9c102_vidioc_querybuf(cam, arg);
2620 return sn9c102_vidioc_qbuf(cam, arg);
2623 return sn9c102_vidioc_dqbuf(cam, filp, arg);
2625 case VIDIOC_STREAMON:
2626 return sn9c102_vidioc_streamon(cam, arg);
2628 case VIDIOC_STREAMOFF:
2629 return sn9c102_vidioc_streamoff(cam, arg);
2632 return sn9c102_vidioc_g_parm(cam, arg);
2634 case VIDIOC_S_PARM_OLD:
2636 return sn9c102_vidioc_s_parm(cam, arg);
2640 case VIDIOC_QUERYSTD:
2641 case VIDIOC_ENUMSTD:
2642 case VIDIOC_QUERYMENU:
2652 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2653 unsigned int cmd, unsigned long arg)
2655 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2658 if (down_interruptible(&cam->fileop_sem))
2659 return -ERESTARTSYS;
2661 if (cam->state & DEV_DISCONNECTED) {
2662 DBG(1, "Device not present");
2663 up(&cam->fileop_sem);
2667 if (cam->state & DEV_MISCONFIGURED) {
2668 DBG(1, "The camera is misconfigured. Close and open it "
2670 up(&cam->fileop_sem);
2674 V4LDBG(3, "sn9c102", cmd);
2676 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2678 up(&cam->fileop_sem);
2683 /*****************************************************************************/
2685 static struct file_operations sn9c102_fops = {
2686 .owner = THIS_MODULE,
2687 .open = sn9c102_open,
2688 .release = sn9c102_release,
2689 .ioctl = sn9c102_ioctl,
2690 .read = sn9c102_read,
2691 .poll = sn9c102_poll,
2692 .mmap = sn9c102_mmap,
2693 .llseek = no_llseek,
2696 /*****************************************************************************/
2698 /* It exists a single interface only. We do not need to validate anything. */
2700 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2702 struct usb_device *udev = interface_to_usbdev(intf);
2703 struct sn9c102_device* cam;
2704 static unsigned int dev_nr = 0;
2708 if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2713 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2714 DBG(1, "kmalloc() failed");
2719 if (!(cam->v4ldev = video_device_alloc())) {
2720 DBG(1, "video_device_alloc() failed");
2725 init_MUTEX(&cam->dev_sem);
2727 r = sn9c102_read_reg(cam, 0x00);
2728 if (r < 0 || r != 0x10) {
2729 DBG(1, "Sorry, this is not a SN9C10x based camera "
2730 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2735 cam->bridge = (id->idProduct & 0xffc0) == 0x6080 ?
2736 BRIDGE_SN9C103 : BRIDGE_SN9C102;
2737 switch (cam->bridge) {
2738 case BRIDGE_SN9C101:
2739 case BRIDGE_SN9C102:
2740 DBG(2, "SN9C10[12] PC Camera Controller detected "
2741 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2743 case BRIDGE_SN9C103:
2744 DBG(2, "SN9C103 PC Camera Controller detected "
2745 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2749 for (i = 0; sn9c102_sensor_table[i]; i++) {
2750 err = sn9c102_sensor_table[i](cam);
2755 if (!err && cam->sensor) {
2756 DBG(2, "%s image sensor detected", cam->sensor->name);
2757 DBG(3, "Support for %s maintained by %s",
2758 cam->sensor->name, cam->sensor->maintainer);
2760 DBG(1, "No supported image sensor detected");
2765 if (sn9c102_init(cam)) {
2766 DBG(1, "Initialization failed. I will retry on open().");
2767 cam->state |= DEV_MISCONFIGURED;
2770 strcpy(cam->v4ldev->name, "SN9C10x PC Camera");
2771 cam->v4ldev->owner = THIS_MODULE;
2772 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2773 cam->v4ldev->hardware = 0;
2774 cam->v4ldev->fops = &sn9c102_fops;
2775 cam->v4ldev->minor = video_nr[dev_nr];
2776 cam->v4ldev->release = video_device_release;
2777 video_set_drvdata(cam->v4ldev, cam);
2779 down(&cam->dev_sem);
2781 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2784 DBG(1, "V4L2 device registration failed");
2785 if (err == -ENFILE && video_nr[dev_nr] == -1)
2786 DBG(1, "Free /dev/videoX node not found");
2787 video_nr[dev_nr] = -1;
2788 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2793 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2795 cam->module_param.force_munmap = force_munmap[dev_nr];
2797 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2799 #ifdef CONFIG_VIDEO_ADV_DEBUG
2800 sn9c102_create_sysfs(cam);
2801 DBG(2, "Optional device control through 'sysfs' interface ready");
2804 usb_set_intfdata(intf, cam);
2812 kfree(cam->control_buffer);
2814 video_device_release(cam->v4ldev);
2821 static void sn9c102_usb_disconnect(struct usb_interface* intf)
2823 struct sn9c102_device* cam = usb_get_intfdata(intf);
2828 down_write(&sn9c102_disconnect);
2830 down(&cam->dev_sem);
2832 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2834 wake_up_interruptible_all(&cam->open);
2837 DBG(2, "Device /dev/video%d is open! Deregistration and "
2838 "memory deallocation are deferred on close.",
2839 cam->v4ldev->minor);
2840 cam->state |= DEV_MISCONFIGURED;
2841 sn9c102_stop_transfer(cam);
2842 cam->state |= DEV_DISCONNECTED;
2843 wake_up_interruptible(&cam->wait_frame);
2844 wake_up_interruptible(&cam->wait_stream);
2846 cam->state |= DEV_DISCONNECTED;
2847 sn9c102_release_resources(cam);
2855 up_write(&sn9c102_disconnect);
2859 static struct usb_driver sn9c102_usb_driver = {
2861 .id_table = sn9c102_id_table,
2862 .probe = sn9c102_usb_probe,
2863 .disconnect = sn9c102_usb_disconnect,
2866 /*****************************************************************************/
2868 static int __init sn9c102_module_init(void)
2872 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
2873 KDBG(3, SN9C102_MODULE_AUTHOR);
2875 if ((err = usb_register(&sn9c102_usb_driver)))
2876 KDBG(1, "usb_register() failed");
2882 static void __exit sn9c102_module_exit(void)
2884 usb_deregister(&sn9c102_usb_driver);
2888 module_init(sn9c102_module_init);
2889 module_exit(sn9c102_module_exit);