1 /***************************************************************************
2 * V4L2 driver for SN9C1xx PC Camera Controllers *
4 * Copyright (C) 2004-2007 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/device.h>
30 #include <linux/delay.h>
31 #include <linux/compiler.h>
32 #include <linux/ioctl.h>
33 #include <linux/poll.h>
34 #include <linux/stat.h>
36 #include <linux/vmalloc.h>
37 #include <linux/page-flags.h>
38 #include <linux/byteorder/generic.h>
40 #include <asm/uaccess.h>
44 /*****************************************************************************/
46 #define SN9C102_MODULE_NAME "V4L2 driver for SN9C1xx PC Camera Controllers"
47 #define SN9C102_MODULE_ALIAS "sn9c1xx"
48 #define SN9C102_MODULE_AUTHOR "(C) 2004-2007 Luca Risolia"
49 #define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
50 #define SN9C102_MODULE_LICENSE "GPL"
51 #define SN9C102_MODULE_VERSION "1:1.39"
52 #define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 39)
54 /*****************************************************************************/
56 MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
58 MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
59 MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
60 MODULE_ALIAS(SN9C102_MODULE_ALIAS);
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)"."
92 static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] =
93 SN9C102_FRAME_TIMEOUT};
94 module_param_array(frame_timeout, uint, NULL, 0644);
95 MODULE_PARM_DESC(frame_timeout,
96 "\n<0|n[,...]> Timeout for a video frame in seconds before"
97 "\nreturning an I/O error; 0 for infinity."
98 "\nThis parameter is specific for each detected camera."
99 "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"."
103 static unsigned short debug = SN9C102_DEBUG_LEVEL;
104 module_param(debug, ushort, 0644);
105 MODULE_PARM_DESC(debug,
106 "\n<n> Debugging information level, from 0 to 3:"
107 "\n0 = none (use carefully)"
108 "\n1 = critical errors"
109 "\n2 = significant informations"
110 "\n3 = more verbose messages"
111 "\nLevel 3 is useful for testing only."
112 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
116 /*****************************************************************************/
119 sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
120 enum sn9c102_io_method io)
122 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
123 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
124 size_t imagesize = cam->module_param.force_munmap || io == IO_READ ?
125 (p->width * p->height * p->priv) / 8 :
126 (r->width * r->height * p->priv) / 8;
130 if (count > SN9C102_MAX_FRAMES)
131 count = SN9C102_MAX_FRAMES;
133 if (cam->bridge == BRIDGE_SN9C105 || cam->bridge == BRIDGE_SN9C120)
134 imagesize += 589 + 2; /* length of JPEG header + EOI marker */
136 cam->nbuffers = count;
137 while (cam->nbuffers > 0) {
138 if ((buff = vmalloc_32_user(cam->nbuffers *
139 PAGE_ALIGN(imagesize))))
144 for (i = 0; i < cam->nbuffers; i++) {
145 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
146 cam->frame[i].buf.index = i;
147 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
148 cam->frame[i].buf.length = imagesize;
149 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
150 cam->frame[i].buf.sequence = 0;
151 cam->frame[i].buf.field = V4L2_FIELD_NONE;
152 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
153 cam->frame[i].buf.flags = 0;
156 return cam->nbuffers;
160 static void sn9c102_release_buffers(struct sn9c102_device* cam)
163 vfree(cam->frame[0].bufmem);
166 cam->frame_current = NULL;
170 static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
174 INIT_LIST_HEAD(&cam->inqueue);
175 INIT_LIST_HEAD(&cam->outqueue);
177 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
178 cam->frame[i].state = F_UNUSED;
179 cam->frame[i].buf.bytesused = 0;
184 static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
186 struct sn9c102_frame_t *i;
188 list_for_each_entry(i, &cam->outqueue, frame) {
190 list_add(&i->frame, &cam->inqueue);
193 INIT_LIST_HEAD(&cam->outqueue);
197 static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
199 unsigned long lock_flags;
202 for (i = 0; i < cam->nbuffers; i++)
203 if (cam->frame[i].state == F_UNUSED) {
204 cam->frame[i].state = F_QUEUED;
205 spin_lock_irqsave(&cam->queue_lock, lock_flags);
206 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
207 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
211 /*****************************************************************************/
213 int sn9c102_write_regs(struct sn9c102_device* cam, u8* buff, u16 index)
215 struct usb_device* udev = cam->usbdev;
218 if (index + sizeof(buff) >= ARRAY_SIZE(cam->reg))
221 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
222 index, 0, buff, sizeof(buff),
223 SN9C102_CTRL_TIMEOUT*sizeof(buff));
225 DBG(3, "Failed to write registers (index 0x%02X, error %d)",
230 for (i = 0; i < sizeof(buff); i++)
231 cam->reg[index+i] = buff[i];
237 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
239 struct usb_device* udev = cam->usbdev;
240 u8* buff = cam->control_buffer;
243 if (index >= ARRAY_SIZE(cam->reg))
248 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
249 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
251 DBG(3, "Failed to write a register (value 0x%02X, index "
252 "0x%02X, error %d)", value, index, res);
256 cam->reg[index] = value;
262 /* NOTE: reading some registers always returns 0 */
263 static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
265 struct usb_device* udev = cam->usbdev;
266 u8* buff = cam->control_buffer;
269 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
270 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
272 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
275 return (res >= 0) ? (int)(*buff) : -1;
279 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
281 if (index >= ARRAY_SIZE(cam->reg))
284 return cam->reg[index];
289 sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
293 for (i = 1; i <= 5; i++) {
294 r = sn9c102_read_reg(cam, 0x08);
299 if (sensor->frequency & SN9C102_I2C_400KHZ)
309 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
310 struct sn9c102_sensor* sensor)
314 r = sn9c102_read_reg(cam, 0x08);
318 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
326 return err ? -EIO : 0;
331 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
332 struct sn9c102_sensor* sensor)
335 r = sn9c102_read_reg(cam, 0x08);
336 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
341 sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
342 struct sn9c102_sensor* sensor, u8 data0, u8 data1,
345 struct usb_device* udev = cam->usbdev;
346 u8* data = cam->control_buffer;
350 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
351 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
352 data[1] = data0; /* I2C slave id */
353 data[2] = data1; /* address */
355 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
356 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
360 err += sn9c102_i2c_wait(cam, sensor);
362 /* Read cycle - n bytes */
363 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
364 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
368 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
369 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
373 err += sn9c102_i2c_wait(cam, sensor);
375 /* The first read byte will be placed in data[4] */
376 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
377 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
381 err += sn9c102_i2c_detect_read_error(cam, sensor);
383 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
387 DBG(3, "I2C read failed for %s image sensor", sensor->name);
392 memcpy(buffer, data, sizeof(buffer));
399 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
400 struct sn9c102_sensor* sensor, u8 n, u8 data0,
401 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
403 struct usb_device* udev = cam->usbdev;
404 u8* data = cam->control_buffer;
407 /* Write cycle. It usually is address + value */
408 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
409 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
418 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
419 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
423 err += sn9c102_i2c_wait(cam, sensor);
424 err += sn9c102_i2c_detect_write_error(cam, sensor);
427 DBG(3, "I2C write failed for %s image sensor", sensor->name);
429 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
430 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
431 n, data0, data1, data2, data3, data4, data5);
438 sn9c102_i2c_try_read(struct sn9c102_device* cam,
439 struct sn9c102_sensor* sensor, u8 address)
441 return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
447 sn9c102_i2c_try_write(struct sn9c102_device* cam,
448 struct sn9c102_sensor* sensor, u8 address, u8 value)
450 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
451 sensor->i2c_slave_id, address,
456 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
458 return sn9c102_i2c_try_read(cam, &cam->sensor, address);
462 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
464 return sn9c102_i2c_try_write(cam, &cam->sensor, address, value);
467 /*****************************************************************************/
469 static size_t sn9c102_sof_length(struct sn9c102_device* cam)
471 switch (cam->bridge) {
487 sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
489 const char marker[6] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
491 size_t soflen = 0, i, j;
493 soflen = sn9c102_sof_length(cam);
495 for (i = 0; i < len; i++) {
498 /* Read the variable part of the header */
499 if (unlikely(cam->sof.bytesread >= sizeof(marker))) {
500 cam->sof.header[cam->sof.bytesread] = *(m+i);
501 if (++cam->sof.bytesread == soflen) {
502 cam->sof.bytesread = 0;
508 /* Search for the SOF marker (fixed part) in the header */
509 for (j = 0, b=cam->sof.bytesread; j+b < sizeof(marker); j++) {
510 if (unlikely(i+j) == len)
512 if (*(m+i+j) == marker[cam->sof.bytesread]) {
513 cam->sof.header[cam->sof.bytesread] = *(m+i+j);
514 if (++cam->sof.bytesread == sizeof(marker)) {
515 PDBGG("Bytes to analyze: %zd. SOF "
516 "starts at byte #%zd", len, i);
521 cam->sof.bytesread = 0;
532 sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
534 char eof_header[4][4] = {
535 {0x00, 0x00, 0x00, 0x00},
536 {0x40, 0x00, 0x00, 0x00},
537 {0x80, 0x00, 0x00, 0x00},
538 {0xc0, 0x00, 0x00, 0x00},
542 /* The EOF header does not exist in compressed data */
543 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
544 cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
548 The EOF header might cross the packet boundary, but this is not a
549 problem, since the end of a frame is determined by checking its size
552 for (i = 0; (len >= 4) && (i <= len - 4); i++)
553 for (j = 0; j < ARRAY_SIZE(eof_header); j++)
554 if (!memcmp(mem + i, eof_header[j], 4))
562 sn9c102_write_jpegheader(struct sn9c102_device* cam, struct sn9c102_frame_t* f)
564 static u8 jpeg_header[589] = {
565 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00, 0x06, 0x04, 0x05,
566 0x06, 0x05, 0x04, 0x06, 0x06, 0x05, 0x06, 0x07, 0x07, 0x06,
567 0x08, 0x0a, 0x10, 0x0a, 0x0a, 0x09, 0x09, 0x0a, 0x14, 0x0e,
568 0x0f, 0x0c, 0x10, 0x17, 0x14, 0x18, 0x18, 0x17, 0x14, 0x16,
569 0x16, 0x1a, 0x1d, 0x25, 0x1f, 0x1a, 0x1b, 0x23, 0x1c, 0x16,
570 0x16, 0x20, 0x2c, 0x20, 0x23, 0x26, 0x27, 0x29, 0x2a, 0x29,
571 0x19, 0x1f, 0x2d, 0x30, 0x2d, 0x28, 0x30, 0x25, 0x28, 0x29,
572 0x28, 0x01, 0x07, 0x07, 0x07, 0x0a, 0x08, 0x0a, 0x13, 0x0a,
573 0x0a, 0x13, 0x28, 0x1a, 0x16, 0x1a, 0x28, 0x28, 0x28, 0x28,
574 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
575 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
576 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
577 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
578 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0xff, 0xc4, 0x01, 0xa2,
579 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
580 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
581 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01,
582 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
583 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
584 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10, 0x00,
585 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,
586 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04,
587 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
588 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,
589 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62,
590 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,
591 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38,
592 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
593 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
594 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
595 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
596 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
597 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
598 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
599 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3,
600 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3,
601 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3,
602 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02,
603 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04,
604 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
605 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
606 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1,
607 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
608 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19,
609 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
610 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
611 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
612 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
613 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
614 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
615 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
616 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
617 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
618 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3,
619 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
620 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc0, 0x00, 0x11,
621 0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x01, 0x21, 0x00, 0x02,
622 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03,
623 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00
627 memcpy(pos, jpeg_header, sizeof(jpeg_header));
629 *(pos + 7 + 64) = 0x01;
630 if (cam->compression.quality == 0) {
631 memcpy(pos + 7, SN9C102_Y_QTABLE0, 64);
632 memcpy(pos + 8 + 64, SN9C102_UV_QTABLE0, 64);
633 } else if (cam->compression.quality == 1) {
634 memcpy(pos + 7, SN9C102_Y_QTABLE1, 64);
635 memcpy(pos + 8 + 64, SN9C102_UV_QTABLE1, 64);
637 *(pos + 564) = cam->sensor.pix_format.width & 0xFF;
638 *(pos + 563) = (cam->sensor.pix_format.width >> 8) & 0xFF;
639 *(pos + 562) = cam->sensor.pix_format.height & 0xFF;
640 *(pos + 561) = (cam->sensor.pix_format.height >> 8) & 0xFF;
643 f->buf.bytesused += sizeof(jpeg_header);
648 sn9c102_write_eoimarker(struct sn9c102_device* cam, struct sn9c102_frame_t* f)
650 static const u8 eoi_marker[2] = {0xff, 0xd9};
652 memcpy(f->bufmem + f->buf.bytesused, eoi_marker, sizeof(eoi_marker));
653 f->buf.bytesused += sizeof(eoi_marker);
657 static void sn9c102_urb_complete(struct urb *urb)
659 struct sn9c102_device* cam = urb->context;
660 struct sn9c102_frame_t** f;
661 size_t imagesize, soflen;
665 if (urb->status == -ENOENT)
668 f = &cam->frame_current;
670 if (cam->stream == STREAM_INTERRUPT) {
671 cam->stream = STREAM_OFF;
673 (*f)->state = F_QUEUED;
674 cam->sof.bytesread = 0;
675 DBG(3, "Stream interrupted by application");
676 wake_up(&cam->wait_stream);
679 if (cam->state & DEV_DISCONNECTED)
682 if (cam->state & DEV_MISCONFIGURED) {
683 wake_up_interruptible(&cam->wait_frame);
687 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
691 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
694 imagesize = (cam->sensor.pix_format.width *
695 cam->sensor.pix_format.height *
696 cam->sensor.pix_format.priv) / 8;
697 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
698 imagesize += 589; /* length of jpeg header */
699 soflen = sn9c102_sof_length(cam);
701 for (i = 0; i < urb->number_of_packets; i++) {
702 unsigned int img, len, status;
703 void *pos, *sof, *eof;
705 len = urb->iso_frame_desc[i].actual_length;
706 status = urb->iso_frame_desc[i].status;
707 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
710 DBG(3, "Error in isochronous frame");
711 (*f)->state = F_ERROR;
712 cam->sof.bytesread = 0;
716 PDBGG("Isochrnous frame: length %u, #%u i", len, i);
719 sof = sn9c102_find_sof_header(cam, pos, len);
721 eof = sn9c102_find_eof_header(cam, pos, len);
722 if ((*f)->state == F_GRABBING) {
727 img = (eof > pos) ? eof - pos - 1 : 0;
729 if ((*f)->buf.bytesused + img > imagesize) {
731 b = (*f)->buf.bytesused + img -
733 img = imagesize - (*f)->buf.bytesused;
734 PDBGG("Expected EOF not found: video "
737 DBG(3, "Exceeded limit: +%u "
738 "bytes", (unsigned)(b));
741 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
744 if ((*f)->buf.bytesused == 0)
745 do_gettimeofday(&(*f)->buf.timestamp);
747 (*f)->buf.bytesused += img;
749 if ((*f)->buf.bytesused == imagesize ||
750 ((cam->sensor.pix_format.pixelformat ==
751 V4L2_PIX_FMT_SN9C10X ||
752 cam->sensor.pix_format.pixelformat ==
753 V4L2_PIX_FMT_JPEG) && eof)) {
756 b = (*f)->buf.bytesused;
757 (*f)->state = F_DONE;
758 (*f)->buf.sequence= ++cam->frame_count;
760 spin_lock(&cam->queue_lock);
761 list_move_tail(&(*f)->frame,
763 if (!list_empty(&cam->inqueue))
766 struct sn9c102_frame_t,
770 spin_unlock(&cam->queue_lock);
772 memcpy(cam->sysfs.frame_header,
773 cam->sof.header, soflen);
775 DBG(3, "Video frame captured: %lu "
776 "bytes", (unsigned long)(b));
782 (*f)->state = F_ERROR;
783 DBG(3, "Not expected EOF after %lu "
784 "bytes of image data",
786 ((*f)->buf.bytesused));
793 DBG(3, "EOF without SOF");
797 PDBGG("Ignoring pointless isochronous frame");
801 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
803 (*f)->state = F_GRABBING;
804 (*f)->buf.bytesused = 0;
807 if (cam->sensor.pix_format.pixelformat ==
809 sn9c102_write_jpegheader(cam, (*f));
810 DBG(3, "SOF detected: new video frame");
814 } else if ((*f)->state == F_GRABBING) {
815 eof = sn9c102_find_eof_header(cam, pos, len);
816 if (eof && eof < sof)
817 goto end_of_frame; /* (1) */
819 if (cam->sensor.pix_format.pixelformat ==
820 V4L2_PIX_FMT_SN9C10X ||
821 cam->sensor.pix_format.pixelformat ==
823 if (sof - pos >= soflen) {
825 } else { /* remove header */
827 (*f)->buf.bytesused -=
828 (soflen - (sof - pos));
832 DBG(3, "SOF before expected EOF after "
833 "%lu bytes of image data",
835 ((*f)->buf.bytesused));
843 urb->dev = cam->usbdev;
844 err = usb_submit_urb(urb, GFP_ATOMIC);
845 if (err < 0 && err != -EPERM) {
846 cam->state |= DEV_MISCONFIGURED;
847 DBG(1, "usb_submit_urb() failed");
850 wake_up_interruptible(&cam->wait_frame);
854 static int sn9c102_start_transfer(struct sn9c102_device* cam)
856 struct usb_device *udev = cam->usbdev;
858 struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
859 usb_ifnum_to_if(udev, 0),
860 SN9C102_ALTERNATE_SETTING);
861 const unsigned int psz = le16_to_cpu(altsetting->
862 endpoint[0].desc.wMaxPacketSize);
866 for (i = 0; i < SN9C102_URBS; i++) {
867 cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
869 if (!cam->transfer_buffer[i]) {
871 DBG(1, "Not enough memory");
876 for (i = 0; i < SN9C102_URBS; i++) {
877 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
881 DBG(1, "usb_alloc_urb() failed");
886 urb->pipe = usb_rcvisocpipe(udev, 1);
887 urb->transfer_flags = URB_ISO_ASAP;
888 urb->number_of_packets = SN9C102_ISO_PACKETS;
889 urb->complete = sn9c102_urb_complete;
890 urb->transfer_buffer = cam->transfer_buffer[i];
891 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
893 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
894 urb->iso_frame_desc[j].offset = psz * j;
895 urb->iso_frame_desc[j].length = psz;
900 if (!(cam->reg[0x01] & 0x04)) {
901 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
904 DBG(1, "I/O hardware error");
909 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
911 DBG(1, "usb_set_interface() failed");
915 cam->frame_current = NULL;
916 cam->sof.bytesread = 0;
918 for (i = 0; i < SN9C102_URBS; i++) {
919 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
921 for (j = i-1; j >= 0; j--)
922 usb_kill_urb(cam->urb[j]);
923 DBG(1, "usb_submit_urb() failed, error %d", err);
931 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
932 usb_free_urb(cam->urb[i]);
935 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
936 kfree(cam->transfer_buffer[i]);
942 static int sn9c102_stop_transfer(struct sn9c102_device* cam)
944 struct usb_device *udev = cam->usbdev;
948 if (cam->state & DEV_DISCONNECTED)
951 for (i = SN9C102_URBS-1; i >= 0; i--) {
952 usb_kill_urb(cam->urb[i]);
953 usb_free_urb(cam->urb[i]);
954 kfree(cam->transfer_buffer[i]);
957 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
959 DBG(3, "usb_set_interface() failed");
965 static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
969 cam->stream = STREAM_INTERRUPT;
970 timeout = wait_event_timeout(cam->wait_stream,
971 (cam->stream == STREAM_OFF) ||
972 (cam->state & DEV_DISCONNECTED),
973 SN9C102_URB_TIMEOUT);
974 if (cam->state & DEV_DISCONNECTED)
976 else if (cam->stream != STREAM_OFF) {
977 cam->state |= DEV_MISCONFIGURED;
978 DBG(1, "URB timeout reached. The camera is misconfigured. "
979 "To use it, close and open /dev/video%d again.",
987 /*****************************************************************************/
989 #ifdef CONFIG_VIDEO_ADV_DEBUG
990 static u16 sn9c102_strtou16(const char* buff, size_t len, ssize_t* count)
997 strncpy(str, buff, len);
1000 strncpy(str, buff, 6);
1004 val = simple_strtoul(str, &endp, 0);
1008 *count = (ssize_t)(endp - str);
1009 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
1016 NOTE 1: being inside one of the following methods implies that the v4l
1017 device exists for sure (see kobjects and reference counters)
1018 NOTE 2: buffers are PAGE_SIZE long
1021 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
1023 struct sn9c102_device* cam;
1026 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1027 return -ERESTARTSYS;
1029 cam = video_get_drvdata(container_of(cd, struct video_device,
1032 mutex_unlock(&sn9c102_sysfs_lock);
1036 count = sprintf(buf, "%u\n", cam->sysfs.reg);
1038 mutex_unlock(&sn9c102_sysfs_lock);
1045 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
1047 struct sn9c102_device* cam;
1051 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1052 return -ERESTARTSYS;
1054 cam = video_get_drvdata(container_of(cd, struct video_device,
1057 mutex_unlock(&sn9c102_sysfs_lock);
1061 index = sn9c102_strtou16(buf, len, &count);
1062 if (index >= ARRAY_SIZE(cam->reg) || !count) {
1063 mutex_unlock(&sn9c102_sysfs_lock);
1067 cam->sysfs.reg = index;
1069 DBG(2, "Moved SN9C1XX register index to 0x%02X", cam->sysfs.reg);
1070 DBG(3, "Written bytes: %zd", count);
1072 mutex_unlock(&sn9c102_sysfs_lock);
1078 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
1080 struct sn9c102_device* cam;
1084 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1085 return -ERESTARTSYS;
1087 cam = video_get_drvdata(container_of(cd, struct video_device,
1090 mutex_unlock(&sn9c102_sysfs_lock);
1094 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
1095 mutex_unlock(&sn9c102_sysfs_lock);
1099 count = sprintf(buf, "%d\n", val);
1101 DBG(3, "Read bytes: %zd, value: %d", count, val);
1103 mutex_unlock(&sn9c102_sysfs_lock);
1110 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
1112 struct sn9c102_device* cam;
1117 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1118 return -ERESTARTSYS;
1120 cam = video_get_drvdata(container_of(cd, struct video_device,
1123 mutex_unlock(&sn9c102_sysfs_lock);
1127 value = sn9c102_strtou16(buf, len, &count);
1129 mutex_unlock(&sn9c102_sysfs_lock);
1133 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
1135 mutex_unlock(&sn9c102_sysfs_lock);
1139 DBG(2, "Written SN9C1XX reg. 0x%02X, val. 0x%02X",
1140 cam->sysfs.reg, value);
1141 DBG(3, "Written bytes: %zd", count);
1143 mutex_unlock(&sn9c102_sysfs_lock);
1149 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
1151 struct sn9c102_device* cam;
1154 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1155 return -ERESTARTSYS;
1157 cam = video_get_drvdata(container_of(cd, struct video_device,
1160 mutex_unlock(&sn9c102_sysfs_lock);
1164 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
1166 DBG(3, "Read bytes: %zd", count);
1168 mutex_unlock(&sn9c102_sysfs_lock);
1175 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1177 struct sn9c102_device* cam;
1181 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1182 return -ERESTARTSYS;
1184 cam = video_get_drvdata(container_of(cd, struct video_device,
1187 mutex_unlock(&sn9c102_sysfs_lock);
1191 index = sn9c102_strtou16(buf, len, &count);
1193 mutex_unlock(&sn9c102_sysfs_lock);
1197 cam->sysfs.i2c_reg = index;
1199 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1200 DBG(3, "Written bytes: %zd", count);
1202 mutex_unlock(&sn9c102_sysfs_lock);
1208 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1210 struct sn9c102_device* cam;
1214 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1215 return -ERESTARTSYS;
1217 cam = video_get_drvdata(container_of(cd, struct video_device,
1220 mutex_unlock(&sn9c102_sysfs_lock);
1224 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) {
1225 mutex_unlock(&sn9c102_sysfs_lock);
1229 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1230 mutex_unlock(&sn9c102_sysfs_lock);
1234 count = sprintf(buf, "%d\n", val);
1236 DBG(3, "Read bytes: %zd, value: %d", count, val);
1238 mutex_unlock(&sn9c102_sysfs_lock);
1245 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1247 struct sn9c102_device* cam;
1252 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1253 return -ERESTARTSYS;
1255 cam = video_get_drvdata(container_of(cd, struct video_device,
1258 mutex_unlock(&sn9c102_sysfs_lock);
1262 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) {
1263 mutex_unlock(&sn9c102_sysfs_lock);
1267 value = sn9c102_strtou16(buf, len, &count);
1269 mutex_unlock(&sn9c102_sysfs_lock);
1273 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1275 mutex_unlock(&sn9c102_sysfs_lock);
1279 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1280 cam->sysfs.i2c_reg, value);
1281 DBG(3, "Written bytes: %zd", count);
1283 mutex_unlock(&sn9c102_sysfs_lock);
1290 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1292 struct sn9c102_device* cam;
1293 enum sn9c102_bridge bridge;
1298 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1299 return -ERESTARTSYS;
1301 cam = video_get_drvdata(container_of(cd, struct video_device,
1304 mutex_unlock(&sn9c102_sysfs_lock);
1308 bridge = cam->bridge;
1310 mutex_unlock(&sn9c102_sysfs_lock);
1312 value = sn9c102_strtou16(buf, len, &count);
1317 case BRIDGE_SN9C101:
1318 case BRIDGE_SN9C102:
1321 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1322 res = sn9c102_store_val(cd, buf, len);
1324 case BRIDGE_SN9C103:
1325 case BRIDGE_SN9C105:
1326 case BRIDGE_SN9C120:
1329 if ((res = sn9c102_store_reg(cd, "0x07", 4)) >= 0)
1330 res = sn9c102_store_val(cd, buf, len);
1339 sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1345 value = sn9c102_strtou16(buf, len, &count);
1346 if (!count || value > 0x7f)
1349 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1350 res = sn9c102_store_val(cd, buf, len);
1357 sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1363 value = sn9c102_strtou16(buf, len, &count);
1364 if (!count || value > 0x7f)
1367 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1368 res = sn9c102_store_val(cd, buf, len);
1374 static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1376 struct sn9c102_device* cam;
1379 cam = video_get_drvdata(container_of(cd, struct video_device,
1384 count = sizeof(cam->sysfs.frame_header);
1385 memcpy(buf, cam->sysfs.frame_header, count);
1387 DBG(3, "Frame header, read bytes: %zd", count);
1393 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1394 sn9c102_show_reg, sn9c102_store_reg);
1395 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1396 sn9c102_show_val, sn9c102_store_val);
1397 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1398 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1399 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1400 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1401 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1402 static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1403 static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1404 static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
1405 sn9c102_show_frame_header, NULL);
1408 static int sn9c102_create_sysfs(struct sn9c102_device* cam)
1410 struct video_device *v4ldev = cam->v4ldev;
1413 if ((err = video_device_create_file(v4ldev, &class_device_attr_reg)))
1415 if ((err = video_device_create_file(v4ldev, &class_device_attr_val)))
1417 if ((err = video_device_create_file(v4ldev,
1418 &class_device_attr_frame_header)))
1421 if (cam->sensor.sysfs_ops) {
1422 if ((err = video_device_create_file(v4ldev,
1423 &class_device_attr_i2c_reg)))
1424 goto err_frame_header;
1425 if ((err = video_device_create_file(v4ldev,
1426 &class_device_attr_i2c_val)))
1430 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
1431 if ((err = video_device_create_file(v4ldev,
1432 &class_device_attr_green)))
1435 if ((err = video_device_create_file(v4ldev,
1436 &class_device_attr_blue)))
1438 if ((err = video_device_create_file(v4ldev,
1439 &class_device_attr_red)))
1446 video_device_remove_file(v4ldev, &class_device_attr_blue);
1448 if (cam->sensor.sysfs_ops)
1449 video_device_remove_file(v4ldev, &class_device_attr_i2c_val);
1451 if (cam->sensor.sysfs_ops)
1452 video_device_remove_file(v4ldev, &class_device_attr_i2c_reg);
1454 video_device_remove_file(v4ldev, &class_device_attr_frame_header);
1456 video_device_remove_file(v4ldev, &class_device_attr_val);
1458 video_device_remove_file(v4ldev, &class_device_attr_reg);
1462 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1464 /*****************************************************************************/
1467 sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
1471 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
1472 pix->pixelformat == V4L2_PIX_FMT_JPEG) {
1473 switch (cam->bridge) {
1474 case BRIDGE_SN9C101:
1475 case BRIDGE_SN9C102:
1476 case BRIDGE_SN9C103:
1477 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
1480 case BRIDGE_SN9C105:
1481 case BRIDGE_SN9C120:
1482 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
1487 switch (cam->bridge) {
1488 case BRIDGE_SN9C101:
1489 case BRIDGE_SN9C102:
1490 case BRIDGE_SN9C103:
1491 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
1494 case BRIDGE_SN9C105:
1495 case BRIDGE_SN9C120:
1496 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
1502 return err ? -EIO : 0;
1507 sn9c102_set_compression(struct sn9c102_device* cam,
1508 struct v4l2_jpegcompression* compression)
1512 switch (cam->bridge) {
1513 case BRIDGE_SN9C101:
1514 case BRIDGE_SN9C102:
1515 case BRIDGE_SN9C103:
1516 if (compression->quality == 0)
1517 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01,
1519 else if (compression->quality == 1)
1520 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe,
1523 case BRIDGE_SN9C105:
1524 case BRIDGE_SN9C120:
1525 if (compression->quality == 0) {
1526 for (i = 0; i <= 63; i++) {
1527 err += sn9c102_write_reg(cam,
1528 SN9C102_Y_QTABLE1[i],
1530 err += sn9c102_write_reg(cam,
1531 SN9C102_UV_QTABLE1[i],
1534 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0xbf,
1536 } else if (compression->quality == 1) {
1537 for (i = 0; i <= 63; i++) {
1538 err += sn9c102_write_reg(cam,
1539 SN9C102_Y_QTABLE1[i],
1541 err += sn9c102_write_reg(cam,
1542 SN9C102_UV_QTABLE1[i],
1545 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x40,
1551 return err ? -EIO : 0;
1555 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1561 r = cam->reg[0x18] & 0xcf;
1562 else if (scale == 2) {
1563 r = cam->reg[0x18] & 0xcf;
1565 } else if (scale == 4)
1566 r = cam->reg[0x18] | 0x20;
1568 err += sn9c102_write_reg(cam, r, 0x18);
1572 PDBGG("Scaling factor: %u", scale);
1578 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1580 struct sn9c102_sensor* s = &cam->sensor;
1581 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1582 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1583 h_size = (u8)(rect->width / 16),
1584 v_size = (u8)(rect->height / 16);
1587 err += sn9c102_write_reg(cam, h_start, 0x12);
1588 err += sn9c102_write_reg(cam, v_start, 0x13);
1589 err += sn9c102_write_reg(cam, h_size, 0x15);
1590 err += sn9c102_write_reg(cam, v_size, 0x16);
1594 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1595 "%u %u %u %u", h_start, v_start, h_size, v_size);
1601 static int sn9c102_init(struct sn9c102_device* cam)
1603 struct sn9c102_sensor* s = &cam->sensor;
1604 struct v4l2_control ctrl;
1605 struct v4l2_queryctrl *qctrl;
1606 struct v4l2_rect* rect;
1610 if (!(cam->state & DEV_INITIALIZED)) {
1611 init_waitqueue_head(&cam->open);
1613 rect = &(s->cropcap.defrect);
1614 } else { /* use current values */
1619 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1620 err += sn9c102_set_crop(cam, rect);
1627 DBG(3, "Sensor initialization failed");
1632 if (!(cam->state & DEV_INITIALIZED))
1633 if (cam->bridge == BRIDGE_SN9C101 ||
1634 cam->bridge == BRIDGE_SN9C102 ||
1635 cam->bridge == BRIDGE_SN9C103) {
1636 if (s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
1637 s->pix_format.pixelformat= V4L2_PIX_FMT_SBGGR8;
1638 cam->compression.quality = cam->reg[0x17] & 0x01 ?
1641 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1642 s->pix_format.pixelformat = V4L2_PIX_FMT_JPEG;
1643 cam->compression.quality = cam->reg[0x18] & 0x40 ?
1645 err += sn9c102_set_compression(cam, &cam->compression);
1648 err += sn9c102_set_compression(cam, &cam->compression);
1649 err += sn9c102_set_pix_format(cam, &s->pix_format);
1650 if (s->set_pix_format)
1651 err += s->set_pix_format(cam, &s->pix_format);
1655 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
1656 s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
1657 DBG(3, "Compressed video format is active, quality %d",
1658 cam->compression.quality);
1660 DBG(3, "Uncompressed video format is active");
1663 if ((err = s->set_crop(cam, rect))) {
1664 DBG(3, "set_crop() failed");
1669 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1670 if (s->qctrl[i].id != 0 &&
1671 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1672 ctrl.id = s->qctrl[i].id;
1673 ctrl.value = qctrl[i].default_value;
1674 err = s->set_ctrl(cam, &ctrl);
1676 DBG(3, "Set %s control failed",
1680 DBG(3, "Image sensor supports '%s' control",
1685 if (!(cam->state & DEV_INITIALIZED)) {
1686 mutex_init(&cam->fileop_mutex);
1687 spin_lock_init(&cam->queue_lock);
1688 init_waitqueue_head(&cam->wait_frame);
1689 init_waitqueue_head(&cam->wait_stream);
1690 cam->nreadbuffers = 2;
1691 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1692 memcpy(&(s->_rect), &(s->cropcap.defrect),
1693 sizeof(struct v4l2_rect));
1694 cam->state |= DEV_INITIALIZED;
1697 DBG(2, "Initialization succeeded");
1702 static void sn9c102_release_resources(struct sn9c102_device* cam)
1704 mutex_lock(&sn9c102_sysfs_lock);
1706 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1707 video_set_drvdata(cam->v4ldev, NULL);
1708 video_unregister_device(cam->v4ldev);
1710 mutex_unlock(&sn9c102_sysfs_lock);
1712 kfree(cam->control_buffer);
1715 /*****************************************************************************/
1717 static int sn9c102_open(struct inode* inode, struct file* filp)
1719 struct sn9c102_device* cam;
1723 This is the only safe way to prevent race conditions with
1726 if (!down_read_trylock(&sn9c102_disconnect))
1727 return -ERESTARTSYS;
1729 cam = video_get_drvdata(video_devdata(filp));
1731 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1732 up_read(&sn9c102_disconnect);
1733 return -ERESTARTSYS;
1737 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1738 DBG(3, "Simultaneous opens are not supported");
1739 if ((filp->f_flags & O_NONBLOCK) ||
1740 (filp->f_flags & O_NDELAY)) {
1744 mutex_unlock(&cam->dev_mutex);
1745 err = wait_event_interruptible_exclusive(cam->open,
1746 cam->state & DEV_DISCONNECTED
1749 up_read(&sn9c102_disconnect);
1752 if (cam->state & DEV_DISCONNECTED) {
1753 up_read(&sn9c102_disconnect);
1756 mutex_lock(&cam->dev_mutex);
1760 if (cam->state & DEV_MISCONFIGURED) {
1761 err = sn9c102_init(cam);
1763 DBG(1, "Initialization failed again. "
1764 "I will retry on next open().");
1767 cam->state &= ~DEV_MISCONFIGURED;
1770 if ((err = sn9c102_start_transfer(cam)))
1773 filp->private_data = cam;
1776 cam->stream = STREAM_OFF;
1778 cam->frame_count = 0;
1779 sn9c102_empty_framequeues(cam);
1781 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1784 mutex_unlock(&cam->dev_mutex);
1785 up_read(&sn9c102_disconnect);
1790 static int sn9c102_release(struct inode* inode, struct file* filp)
1792 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1794 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1796 sn9c102_stop_transfer(cam);
1798 sn9c102_release_buffers(cam);
1800 if (cam->state & DEV_DISCONNECTED) {
1801 sn9c102_release_resources(cam);
1802 usb_put_dev(cam->usbdev);
1803 mutex_unlock(&cam->dev_mutex);
1809 wake_up_interruptible_nr(&cam->open, 1);
1811 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1813 mutex_unlock(&cam->dev_mutex);
1820 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1822 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1823 struct sn9c102_frame_t* f, * i;
1824 unsigned long lock_flags;
1828 if (mutex_lock_interruptible(&cam->fileop_mutex))
1829 return -ERESTARTSYS;
1831 if (cam->state & DEV_DISCONNECTED) {
1832 DBG(1, "Device not present");
1833 mutex_unlock(&cam->fileop_mutex);
1837 if (cam->state & DEV_MISCONFIGURED) {
1838 DBG(1, "The camera is misconfigured. Close and open it "
1840 mutex_unlock(&cam->fileop_mutex);
1844 if (cam->io == IO_MMAP) {
1845 DBG(3, "Close and open the device again to choose "
1847 mutex_unlock(&cam->fileop_mutex);
1851 if (cam->io == IO_NONE) {
1852 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1853 DBG(1, "read() failed, not enough memory");
1854 mutex_unlock(&cam->fileop_mutex);
1858 cam->stream = STREAM_ON;
1861 if (list_empty(&cam->inqueue)) {
1862 if (!list_empty(&cam->outqueue))
1863 sn9c102_empty_framequeues(cam);
1864 sn9c102_queue_unusedframes(cam);
1868 mutex_unlock(&cam->fileop_mutex);
1872 if (list_empty(&cam->outqueue)) {
1873 if (filp->f_flags & O_NONBLOCK) {
1874 mutex_unlock(&cam->fileop_mutex);
1877 if (!cam->module_param.frame_timeout) {
1878 err = wait_event_interruptible
1880 (!list_empty(&cam->outqueue)) ||
1881 (cam->state & DEV_DISCONNECTED) ||
1882 (cam->state & DEV_MISCONFIGURED) );
1884 mutex_unlock(&cam->fileop_mutex);
1888 timeout = wait_event_interruptible_timeout
1890 (!list_empty(&cam->outqueue)) ||
1891 (cam->state & DEV_DISCONNECTED) ||
1892 (cam->state & DEV_MISCONFIGURED),
1893 cam->module_param.frame_timeout *
1894 1000 * msecs_to_jiffies(1) );
1896 mutex_unlock(&cam->fileop_mutex);
1898 } else if (timeout == 0 &&
1899 !(cam->state & DEV_DISCONNECTED)) {
1900 DBG(1, "Video frame timeout elapsed");
1901 mutex_unlock(&cam->fileop_mutex);
1905 if (cam->state & DEV_DISCONNECTED) {
1906 mutex_unlock(&cam->fileop_mutex);
1909 if (cam->state & DEV_MISCONFIGURED) {
1910 mutex_unlock(&cam->fileop_mutex);
1915 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1917 if (count > f->buf.bytesused)
1918 count = f->buf.bytesused;
1920 if (copy_to_user(buf, f->bufmem, count)) {
1927 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1928 list_for_each_entry(i, &cam->outqueue, frame)
1929 i->state = F_UNUSED;
1930 INIT_LIST_HEAD(&cam->outqueue);
1931 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1933 sn9c102_queue_unusedframes(cam);
1935 PDBGG("Frame #%lu, bytes read: %zu",
1936 (unsigned long)f->buf.index, count);
1938 mutex_unlock(&cam->fileop_mutex);
1944 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1946 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1947 struct sn9c102_frame_t* f;
1948 unsigned long lock_flags;
1949 unsigned int mask = 0;
1951 if (mutex_lock_interruptible(&cam->fileop_mutex))
1954 if (cam->state & DEV_DISCONNECTED) {
1955 DBG(1, "Device not present");
1959 if (cam->state & DEV_MISCONFIGURED) {
1960 DBG(1, "The camera is misconfigured. Close and open it "
1965 if (cam->io == IO_NONE) {
1966 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1968 DBG(1, "poll() failed, not enough memory");
1972 cam->stream = STREAM_ON;
1975 if (cam->io == IO_READ) {
1976 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1977 list_for_each_entry(f, &cam->outqueue, frame)
1978 f->state = F_UNUSED;
1979 INIT_LIST_HEAD(&cam->outqueue);
1980 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1981 sn9c102_queue_unusedframes(cam);
1984 poll_wait(filp, &cam->wait_frame, wait);
1986 if (!list_empty(&cam->outqueue))
1987 mask |= POLLIN | POLLRDNORM;
1989 mutex_unlock(&cam->fileop_mutex);
1994 mutex_unlock(&cam->fileop_mutex);
1999 static void sn9c102_vm_open(struct vm_area_struct* vma)
2001 struct sn9c102_frame_t* f = vma->vm_private_data;
2006 static void sn9c102_vm_close(struct vm_area_struct* vma)
2008 /* NOTE: buffers are not freed here */
2009 struct sn9c102_frame_t* f = vma->vm_private_data;
2014 static struct vm_operations_struct sn9c102_vm_ops = {
2015 .open = sn9c102_vm_open,
2016 .close = sn9c102_vm_close,
2020 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
2022 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2023 unsigned long size = vma->vm_end - vma->vm_start,
2024 start = vma->vm_start;
2028 if (mutex_lock_interruptible(&cam->fileop_mutex))
2029 return -ERESTARTSYS;
2031 if (cam->state & DEV_DISCONNECTED) {
2032 DBG(1, "Device not present");
2033 mutex_unlock(&cam->fileop_mutex);
2037 if (cam->state & DEV_MISCONFIGURED) {
2038 DBG(1, "The camera is misconfigured. Close and open it "
2040 mutex_unlock(&cam->fileop_mutex);
2044 if (!(vma->vm_flags & (VM_WRITE | VM_READ))) {
2045 mutex_unlock(&cam->fileop_mutex);
2049 if (cam->io != IO_MMAP ||
2050 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
2051 mutex_unlock(&cam->fileop_mutex);
2055 for (i = 0; i < cam->nbuffers; i++) {
2056 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
2059 if (i == cam->nbuffers) {
2060 mutex_unlock(&cam->fileop_mutex);
2064 vma->vm_flags |= VM_IO;
2065 vma->vm_flags |= VM_RESERVED;
2067 pos = cam->frame[i].bufmem;
2068 while (size > 0) { /* size is page-aligned */
2069 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
2070 mutex_unlock(&cam->fileop_mutex);
2078 vma->vm_ops = &sn9c102_vm_ops;
2079 vma->vm_private_data = &cam->frame[i];
2081 sn9c102_vm_open(vma);
2083 mutex_unlock(&cam->fileop_mutex);
2088 /*****************************************************************************/
2091 sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
2093 struct v4l2_capability cap = {
2094 .driver = "sn9c102",
2095 .version = SN9C102_MODULE_VERSION_CODE,
2096 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
2100 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
2101 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
2102 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
2103 sizeof(cap.bus_info));
2105 if (copy_to_user(arg, &cap, sizeof(cap)))
2113 sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
2115 struct v4l2_input i;
2117 if (copy_from_user(&i, arg, sizeof(i)))
2123 memset(&i, 0, sizeof(i));
2124 strcpy(i.name, "Camera");
2125 i.type = V4L2_INPUT_TYPE_CAMERA;
2127 if (copy_to_user(arg, &i, sizeof(i)))
2135 sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg)
2139 if (copy_to_user(arg, &index, sizeof(index)))
2147 sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg)
2151 if (copy_from_user(&index, arg, sizeof(index)))
2162 sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
2164 struct sn9c102_sensor* s = &cam->sensor;
2165 struct v4l2_queryctrl qc;
2168 if (copy_from_user(&qc, arg, sizeof(qc)))
2171 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2172 if (qc.id && qc.id == s->qctrl[i].id) {
2173 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
2174 if (copy_to_user(arg, &qc, sizeof(qc)))
2184 sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
2186 struct sn9c102_sensor* s = &cam->sensor;
2187 struct v4l2_control ctrl;
2191 if (!s->get_ctrl && !s->set_ctrl)
2194 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2198 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2199 if (ctrl.id && ctrl.id == s->qctrl[i].id) {
2200 ctrl.value = s->_qctrl[i].default_value;
2205 err = s->get_ctrl(cam, &ctrl);
2208 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
2211 PDBGG("VIDIOC_G_CTRL: id %lu, value %lu",
2212 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
2219 sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
2221 struct sn9c102_sensor* s = &cam->sensor;
2222 struct v4l2_control ctrl;
2229 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2232 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2233 if (ctrl.id == s->qctrl[i].id) {
2234 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
2236 if (ctrl.value < s->qctrl[i].minimum ||
2237 ctrl.value > s->qctrl[i].maximum)
2239 ctrl.value -= ctrl.value % s->qctrl[i].step;
2243 if ((err = s->set_ctrl(cam, &ctrl)))
2246 s->_qctrl[i].default_value = ctrl.value;
2248 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
2249 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
2256 sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
2258 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
2260 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2261 cc->pixelaspect.numerator = 1;
2262 cc->pixelaspect.denominator = 1;
2264 if (copy_to_user(arg, cc, sizeof(*cc)))
2272 sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
2274 struct sn9c102_sensor* s = &cam->sensor;
2275 struct v4l2_crop crop = {
2276 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2279 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
2281 if (copy_to_user(arg, &crop, sizeof(crop)))
2289 sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
2291 struct sn9c102_sensor* s = &cam->sensor;
2292 struct v4l2_crop crop;
2293 struct v4l2_rect* rect;
2294 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2295 struct v4l2_pix_format* pix_format = &(s->pix_format);
2297 const enum sn9c102_stream_state stream = cam->stream;
2298 const u32 nbuffers = cam->nbuffers;
2302 if (copy_from_user(&crop, arg, sizeof(crop)))
2307 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2310 if (cam->module_param.force_munmap)
2311 for (i = 0; i < cam->nbuffers; i++)
2312 if (cam->frame[i].vma_use_count) {
2313 DBG(3, "VIDIOC_S_CROP failed. "
2314 "Unmap the buffers first.");
2318 /* Preserve R,G or B origin */
2319 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
2320 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
2322 if (rect->width < 16)
2324 if (rect->height < 16)
2326 if (rect->width > bounds->width)
2327 rect->width = bounds->width;
2328 if (rect->height > bounds->height)
2329 rect->height = bounds->height;
2330 if (rect->left < bounds->left)
2331 rect->left = bounds->left;
2332 if (rect->top < bounds->top)
2333 rect->top = bounds->top;
2334 if (rect->left + rect->width > bounds->left + bounds->width)
2335 rect->left = bounds->left+bounds->width - rect->width;
2336 if (rect->top + rect->height > bounds->top + bounds->height)
2337 rect->top = bounds->top+bounds->height - rect->height;
2339 rect->width &= ~15L;
2340 rect->height &= ~15L;
2342 if (SN9C102_PRESERVE_IMGSCALE) {
2343 /* Calculate the actual scaling factor */
2345 a = rect->width * rect->height;
2346 b = pix_format->width * pix_format->height;
2347 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2351 if (cam->stream == STREAM_ON)
2352 if ((err = sn9c102_stream_interrupt(cam)))
2355 if (copy_to_user(arg, &crop, sizeof(crop))) {
2356 cam->stream = stream;
2360 if (cam->module_param.force_munmap || cam->io == IO_READ)
2361 sn9c102_release_buffers(cam);
2363 err = sn9c102_set_crop(cam, rect);
2365 err += s->set_crop(cam, rect);
2366 err += sn9c102_set_scale(cam, scale);
2368 if (err) { /* atomic, no rollback in ioctl() */
2369 cam->state |= DEV_MISCONFIGURED;
2370 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2371 "use the camera, close and open /dev/video%d again.",
2372 cam->v4ldev->minor);
2376 s->pix_format.width = rect->width/scale;
2377 s->pix_format.height = rect->height/scale;
2378 memcpy(&(s->_rect), rect, sizeof(*rect));
2380 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2381 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2382 cam->state |= DEV_MISCONFIGURED;
2383 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2384 "use the camera, close and open /dev/video%d again.",
2385 cam->v4ldev->minor);
2389 if (cam->io == IO_READ)
2390 sn9c102_empty_framequeues(cam);
2391 else if (cam->module_param.force_munmap)
2392 sn9c102_requeue_outqueue(cam);
2394 cam->stream = stream;
2401 sn9c102_vidioc_enum_framesizes(struct sn9c102_device* cam, void __user * arg)
2403 struct v4l2_frmsizeenum frmsize;
2405 if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
2408 if (frmsize.index != 0)
2411 switch (cam->bridge) {
2412 case BRIDGE_SN9C101:
2413 case BRIDGE_SN9C102:
2414 case BRIDGE_SN9C103:
2415 if (frmsize.pixel_format != V4L2_PIX_FMT_SN9C10X &&
2416 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
2418 case BRIDGE_SN9C105:
2419 case BRIDGE_SN9C120:
2420 if (frmsize.pixel_format != V4L2_PIX_FMT_JPEG &&
2421 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
2425 frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
2426 frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
2427 frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
2428 frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
2429 frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
2430 memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
2432 if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
2440 sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
2442 struct v4l2_fmtdesc fmtd;
2444 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
2447 if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2450 if (fmtd.index == 0) {
2451 strcpy(fmtd.description, "bayer rgb");
2452 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2453 } else if (fmtd.index == 1) {
2454 switch (cam->bridge) {
2455 case BRIDGE_SN9C101:
2456 case BRIDGE_SN9C102:
2457 case BRIDGE_SN9C103:
2458 strcpy(fmtd.description, "compressed");
2459 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2461 case BRIDGE_SN9C105:
2462 case BRIDGE_SN9C120:
2463 strcpy(fmtd.description, "JPEG");
2464 fmtd.pixelformat = V4L2_PIX_FMT_JPEG;
2467 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2471 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2472 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2474 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
2482 sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2484 struct v4l2_format format;
2485 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
2487 if (copy_from_user(&format, arg, sizeof(format)))
2490 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2493 pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_JPEG) ?
2494 V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB;
2495 pfmt->bytesperline = (pfmt->pixelformat == V4L2_PIX_FMT_SN9C10X ||
2496 pfmt->pixelformat == V4L2_PIX_FMT_JPEG)
2497 ? 0 : (pfmt->width * pfmt->priv) / 8;
2498 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2499 pfmt->field = V4L2_FIELD_NONE;
2500 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2502 if (copy_to_user(arg, &format, sizeof(format)))
2510 sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2513 struct sn9c102_sensor* s = &cam->sensor;
2514 struct v4l2_format format;
2515 struct v4l2_pix_format* pix;
2516 struct v4l2_pix_format* pfmt = &(s->pix_format);
2517 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2518 struct v4l2_rect rect;
2520 const enum sn9c102_stream_state stream = cam->stream;
2521 const u32 nbuffers = cam->nbuffers;
2525 if (copy_from_user(&format, arg, sizeof(format)))
2528 pix = &(format.fmt.pix);
2530 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2533 memcpy(&rect, &(s->_rect), sizeof(rect));
2535 { /* calculate the actual scaling factor */
2537 a = rect.width * rect.height;
2538 b = pix->width * pix->height;
2539 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2542 rect.width = scale * pix->width;
2543 rect.height = scale * pix->height;
2545 if (rect.width < 16)
2547 if (rect.height < 16)
2549 if (rect.width > bounds->left + bounds->width - rect.left)
2550 rect.width = bounds->left + bounds->width - rect.left;
2551 if (rect.height > bounds->top + bounds->height - rect.top)
2552 rect.height = bounds->top + bounds->height - rect.top;
2555 rect.height &= ~15L;
2557 { /* adjust the scaling factor */
2559 a = rect.width * rect.height;
2560 b = pix->width * pix->height;
2561 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2564 pix->width = rect.width / scale;
2565 pix->height = rect.height / scale;
2567 switch (cam->bridge) {
2568 case BRIDGE_SN9C101:
2569 case BRIDGE_SN9C102:
2570 case BRIDGE_SN9C103:
2571 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2572 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2573 pix->pixelformat = pfmt->pixelformat;
2575 case BRIDGE_SN9C105:
2576 case BRIDGE_SN9C120:
2577 if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
2578 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2579 pix->pixelformat = pfmt->pixelformat;
2582 pix->priv = pfmt->priv; /* bpp */
2583 pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_JPEG) ?
2584 V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB;
2585 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
2586 pix->pixelformat == V4L2_PIX_FMT_JPEG)
2587 ? 0 : (pix->width * pix->priv) / 8;
2588 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2589 pix->field = V4L2_FIELD_NONE;
2591 if (cmd == VIDIOC_TRY_FMT) {
2592 if (copy_to_user(arg, &format, sizeof(format)))
2597 if (cam->module_param.force_munmap)
2598 for (i = 0; i < cam->nbuffers; i++)
2599 if (cam->frame[i].vma_use_count) {
2600 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2605 if (cam->stream == STREAM_ON)
2606 if ((err = sn9c102_stream_interrupt(cam)))
2609 if (copy_to_user(arg, &format, sizeof(format))) {
2610 cam->stream = stream;
2614 if (cam->module_param.force_munmap || cam->io == IO_READ)
2615 sn9c102_release_buffers(cam);
2617 err += sn9c102_set_pix_format(cam, pix);
2618 err += sn9c102_set_crop(cam, &rect);
2619 if (s->set_pix_format)
2620 err += s->set_pix_format(cam, pix);
2622 err += s->set_crop(cam, &rect);
2623 err += sn9c102_set_scale(cam, scale);
2625 if (err) { /* atomic, no rollback in ioctl() */
2626 cam->state |= DEV_MISCONFIGURED;
2627 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2628 "use the camera, close and open /dev/video%d again.",
2629 cam->v4ldev->minor);
2633 memcpy(pfmt, pix, sizeof(*pix));
2634 memcpy(&(s->_rect), &rect, sizeof(rect));
2636 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2637 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2638 cam->state |= DEV_MISCONFIGURED;
2639 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2640 "use the camera, close and open /dev/video%d again.",
2641 cam->v4ldev->minor);
2645 if (cam->io == IO_READ)
2646 sn9c102_empty_framequeues(cam);
2647 else if (cam->module_param.force_munmap)
2648 sn9c102_requeue_outqueue(cam);
2650 cam->stream = stream;
2657 sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2659 if (copy_to_user(arg, &cam->compression, sizeof(cam->compression)))
2667 sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2669 struct v4l2_jpegcompression jc;
2670 const enum sn9c102_stream_state stream = cam->stream;
2673 if (copy_from_user(&jc, arg, sizeof(jc)))
2676 if (jc.quality != 0 && jc.quality != 1)
2679 if (cam->stream == STREAM_ON)
2680 if ((err = sn9c102_stream_interrupt(cam)))
2683 err += sn9c102_set_compression(cam, &jc);
2684 if (err) { /* atomic, no rollback in ioctl() */
2685 cam->state |= DEV_MISCONFIGURED;
2686 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2687 "problems. To use the camera, close and open "
2688 "/dev/video%d again.", cam->v4ldev->minor);
2692 cam->compression.quality = jc.quality;
2694 cam->stream = stream;
2701 sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
2703 struct v4l2_requestbuffers rb;
2707 if (copy_from_user(&rb, arg, sizeof(rb)))
2710 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2711 rb.memory != V4L2_MEMORY_MMAP)
2714 if (cam->io == IO_READ) {
2715 DBG(3, "Close and open the device again to choose the mmap "
2720 for (i = 0; i < cam->nbuffers; i++)
2721 if (cam->frame[i].vma_use_count) {
2722 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2727 if (cam->stream == STREAM_ON)
2728 if ((err = sn9c102_stream_interrupt(cam)))
2731 sn9c102_empty_framequeues(cam);
2733 sn9c102_release_buffers(cam);
2735 rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
2737 if (copy_to_user(arg, &rb, sizeof(rb))) {
2738 sn9c102_release_buffers(cam);
2743 cam->io = rb.count ? IO_MMAP : IO_NONE;
2750 sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
2752 struct v4l2_buffer b;
2754 if (copy_from_user(&b, arg, sizeof(b)))
2757 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2758 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2761 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2763 if (cam->frame[b.index].vma_use_count)
2764 b.flags |= V4L2_BUF_FLAG_MAPPED;
2766 if (cam->frame[b.index].state == F_DONE)
2767 b.flags |= V4L2_BUF_FLAG_DONE;
2768 else if (cam->frame[b.index].state != F_UNUSED)
2769 b.flags |= V4L2_BUF_FLAG_QUEUED;
2771 if (copy_to_user(arg, &b, sizeof(b)))
2779 sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
2781 struct v4l2_buffer b;
2782 unsigned long lock_flags;
2784 if (copy_from_user(&b, arg, sizeof(b)))
2787 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2788 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2791 if (cam->frame[b.index].state != F_UNUSED)
2794 cam->frame[b.index].state = F_QUEUED;
2796 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2797 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2798 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2800 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2807 sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2810 struct v4l2_buffer b;
2811 struct sn9c102_frame_t *f;
2812 unsigned long lock_flags;
2816 if (copy_from_user(&b, arg, sizeof(b)))
2819 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2822 if (list_empty(&cam->outqueue)) {
2823 if (cam->stream == STREAM_OFF)
2825 if (filp->f_flags & O_NONBLOCK)
2827 if (!cam->module_param.frame_timeout) {
2828 err = wait_event_interruptible
2830 (!list_empty(&cam->outqueue)) ||
2831 (cam->state & DEV_DISCONNECTED) ||
2832 (cam->state & DEV_MISCONFIGURED) );
2836 timeout = wait_event_interruptible_timeout
2838 (!list_empty(&cam->outqueue)) ||
2839 (cam->state & DEV_DISCONNECTED) ||
2840 (cam->state & DEV_MISCONFIGURED),
2841 cam->module_param.frame_timeout *
2842 1000 * msecs_to_jiffies(1) );
2845 else if (timeout == 0 &&
2846 !(cam->state & DEV_DISCONNECTED)) {
2847 DBG(1, "Video frame timeout elapsed");
2851 if (cam->state & DEV_DISCONNECTED)
2853 if (cam->state & DEV_MISCONFIGURED)
2857 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2858 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
2859 list_del(cam->outqueue.next);
2860 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2862 f->state = F_UNUSED;
2864 memcpy(&b, &f->buf, sizeof(b));
2865 if (f->vma_use_count)
2866 b.flags |= V4L2_BUF_FLAG_MAPPED;
2868 if (copy_to_user(arg, &b, sizeof(b)))
2871 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2878 sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
2882 if (copy_from_user(&type, arg, sizeof(type)))
2885 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2888 cam->stream = STREAM_ON;
2890 DBG(3, "Stream on");
2897 sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
2901 if (copy_from_user(&type, arg, sizeof(type)))
2904 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2907 if (cam->stream == STREAM_ON)
2908 if ((err = sn9c102_stream_interrupt(cam)))
2911 sn9c102_empty_framequeues(cam);
2913 DBG(3, "Stream off");
2920 sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
2922 struct v4l2_streamparm sp;
2924 if (copy_from_user(&sp, arg, sizeof(sp)))
2927 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2930 sp.parm.capture.extendedmode = 0;
2931 sp.parm.capture.readbuffers = cam->nreadbuffers;
2933 if (copy_to_user(arg, &sp, sizeof(sp)))
2941 sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
2943 struct v4l2_streamparm sp;
2945 if (copy_from_user(&sp, arg, sizeof(sp)))
2948 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2951 sp.parm.capture.extendedmode = 0;
2953 if (sp.parm.capture.readbuffers == 0)
2954 sp.parm.capture.readbuffers = cam->nreadbuffers;
2956 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2957 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2959 if (copy_to_user(arg, &sp, sizeof(sp)))
2962 cam->nreadbuffers = sp.parm.capture.readbuffers;
2969 sn9c102_vidioc_enumaudio(struct sn9c102_device* cam, void __user * arg)
2971 struct v4l2_audio audio;
2973 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
2976 if (copy_from_user(&audio, arg, sizeof(audio)))
2979 if (audio.index != 0)
2982 strcpy(audio.name, "Microphone");
2983 audio.capability = 0;
2986 if (copy_to_user(arg, &audio, sizeof(audio)))
2994 sn9c102_vidioc_g_audio(struct sn9c102_device* cam, void __user * arg)
2996 struct v4l2_audio audio;
2998 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
3001 if (copy_from_user(&audio, arg, sizeof(audio)))
3004 memset(&audio, 0, sizeof(audio));
3005 strcpy(audio.name, "Microphone");
3007 if (copy_to_user(arg, &audio, sizeof(audio)))
3015 sn9c102_vidioc_s_audio(struct sn9c102_device* cam, void __user * arg)
3017 struct v4l2_audio audio;
3019 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
3022 if (copy_from_user(&audio, arg, sizeof(audio)))
3025 if (audio.index != 0)
3032 static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
3033 unsigned int cmd, void __user * arg)
3035 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
3039 case VIDIOC_QUERYCAP:
3040 return sn9c102_vidioc_querycap(cam, arg);
3042 case VIDIOC_ENUMINPUT:
3043 return sn9c102_vidioc_enuminput(cam, arg);
3045 case VIDIOC_G_INPUT:
3046 return sn9c102_vidioc_g_input(cam, arg);
3048 case VIDIOC_S_INPUT:
3049 return sn9c102_vidioc_s_input(cam, arg);
3051 case VIDIOC_QUERYCTRL:
3052 return sn9c102_vidioc_query_ctrl(cam, arg);
3055 return sn9c102_vidioc_g_ctrl(cam, arg);
3058 return sn9c102_vidioc_s_ctrl(cam, arg);
3060 case VIDIOC_CROPCAP:
3061 return sn9c102_vidioc_cropcap(cam, arg);
3064 return sn9c102_vidioc_g_crop(cam, arg);
3067 return sn9c102_vidioc_s_crop(cam, arg);
3069 case VIDIOC_ENUM_FRAMESIZES:
3070 return sn9c102_vidioc_enum_framesizes(cam, arg);
3072 case VIDIOC_ENUM_FMT:
3073 return sn9c102_vidioc_enum_fmt(cam, arg);
3076 return sn9c102_vidioc_g_fmt(cam, arg);
3078 case VIDIOC_TRY_FMT:
3080 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
3082 case VIDIOC_G_JPEGCOMP:
3083 return sn9c102_vidioc_g_jpegcomp(cam, arg);
3085 case VIDIOC_S_JPEGCOMP:
3086 return sn9c102_vidioc_s_jpegcomp(cam, arg);
3088 case VIDIOC_REQBUFS:
3089 return sn9c102_vidioc_reqbufs(cam, arg);
3091 case VIDIOC_QUERYBUF:
3092 return sn9c102_vidioc_querybuf(cam, arg);
3095 return sn9c102_vidioc_qbuf(cam, arg);
3098 return sn9c102_vidioc_dqbuf(cam, filp, arg);
3100 case VIDIOC_STREAMON:
3101 return sn9c102_vidioc_streamon(cam, arg);
3103 case VIDIOC_STREAMOFF:
3104 return sn9c102_vidioc_streamoff(cam, arg);
3107 return sn9c102_vidioc_g_parm(cam, arg);
3110 return sn9c102_vidioc_s_parm(cam, arg);
3112 case VIDIOC_ENUMAUDIO:
3113 return sn9c102_vidioc_enumaudio(cam, arg);
3115 case VIDIOC_G_AUDIO:
3116 return sn9c102_vidioc_g_audio(cam, arg);
3118 case VIDIOC_S_AUDIO:
3119 return sn9c102_vidioc_s_audio(cam, arg);
3123 case VIDIOC_QUERYSTD:
3124 case VIDIOC_ENUMSTD:
3125 case VIDIOC_QUERYMENU:
3126 case VIDIOC_ENUM_FRAMEINTERVALS:
3136 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
3137 unsigned int cmd, unsigned long arg)
3139 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
3142 if (mutex_lock_interruptible(&cam->fileop_mutex))
3143 return -ERESTARTSYS;
3145 if (cam->state & DEV_DISCONNECTED) {
3146 DBG(1, "Device not present");
3147 mutex_unlock(&cam->fileop_mutex);
3151 if (cam->state & DEV_MISCONFIGURED) {
3152 DBG(1, "The camera is misconfigured. Close and open it "
3154 mutex_unlock(&cam->fileop_mutex);
3158 V4LDBG(3, "sn9c102", cmd);
3160 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
3162 mutex_unlock(&cam->fileop_mutex);
3167 /*****************************************************************************/
3169 static const struct file_operations sn9c102_fops = {
3170 .owner = THIS_MODULE,
3171 .open = sn9c102_open,
3172 .release = sn9c102_release,
3173 .ioctl = sn9c102_ioctl,
3174 .compat_ioctl = v4l_compat_ioctl32,
3175 .read = sn9c102_read,
3176 .poll = sn9c102_poll,
3177 .mmap = sn9c102_mmap,
3178 .llseek = no_llseek,
3181 /*****************************************************************************/
3183 /* It exists a single interface only. We do not need to validate anything. */
3185 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3187 struct usb_device *udev = interface_to_usbdev(intf);
3188 struct sn9c102_device* cam;
3189 static unsigned int dev_nr = 0;
3193 if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
3198 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
3199 DBG(1, "kzalloc() failed");
3204 if (!(cam->v4ldev = video_device_alloc())) {
3205 DBG(1, "video_device_alloc() failed");
3210 mutex_init(&cam->dev_mutex);
3212 r = sn9c102_read_reg(cam, 0x00);
3213 if (r < 0 || (r != 0x10 && r != 0x11 && r != 0x12)) {
3214 DBG(1, "Sorry, this is not a SN9C1xx-based camera "
3215 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3220 cam->bridge = id->driver_info;
3221 switch (cam->bridge) {
3222 case BRIDGE_SN9C101:
3223 case BRIDGE_SN9C102:
3224 DBG(2, "SN9C10[12] PC Camera Controller detected "
3225 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3227 case BRIDGE_SN9C103:
3228 DBG(2, "SN9C103 PC Camera Controller detected "
3229 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3231 case BRIDGE_SN9C105:
3232 DBG(2, "SN9C105 PC Camera Controller detected "
3233 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3235 case BRIDGE_SN9C120:
3236 DBG(2, "SN9C120 PC Camera Controller detected "
3237 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3241 for (i = 0; sn9c102_sensor_table[i]; i++) {
3242 err = sn9c102_sensor_table[i](cam);
3248 DBG(2, "%s image sensor detected", cam->sensor.name);
3249 DBG(3, "Support for %s maintained by %s",
3250 cam->sensor.name, cam->sensor.maintainer);
3252 DBG(1, "No supported image sensor detected");
3257 if (!(cam->bridge & cam->sensor.supported_bridge)) {
3258 DBG(1, "Bridge not supported");
3263 if (sn9c102_init(cam)) {
3264 DBG(1, "Initialization failed. I will retry on open().");
3265 cam->state |= DEV_MISCONFIGURED;
3268 strcpy(cam->v4ldev->name, "SN9C1xx PC Camera");
3269 cam->v4ldev->owner = THIS_MODULE;
3270 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
3271 cam->v4ldev->hardware = 0;
3272 cam->v4ldev->fops = &sn9c102_fops;
3273 cam->v4ldev->minor = video_nr[dev_nr];
3274 cam->v4ldev->release = video_device_release;
3275 video_set_drvdata(cam->v4ldev, cam);
3277 mutex_lock(&cam->dev_mutex);
3279 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
3282 DBG(1, "V4L2 device registration failed");
3283 if (err == -ENFILE && video_nr[dev_nr] == -1)
3284 DBG(1, "Free /dev/videoX node not found");
3285 video_nr[dev_nr] = -1;
3286 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
3287 mutex_unlock(&cam->dev_mutex);
3291 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
3293 cam->module_param.force_munmap = force_munmap[dev_nr];
3294 cam->module_param.frame_timeout = frame_timeout[dev_nr];
3296 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
3298 #ifdef CONFIG_VIDEO_ADV_DEBUG
3299 err = sn9c102_create_sysfs(cam);
3301 DBG(2, "Optional device control through 'sysfs' "
3304 DBG(2, "Failed to create optional 'sysfs' interface for "
3305 "device controlling. Error #%d", err);
3307 DBG(2, "Optional device control through 'sysfs' interface disabled");
3308 DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
3309 "configuration option to enable it.");
3312 usb_set_intfdata(intf, cam);
3314 mutex_unlock(&cam->dev_mutex);
3320 kfree(cam->control_buffer);
3322 video_device_release(cam->v4ldev);
3329 static void sn9c102_usb_disconnect(struct usb_interface* intf)
3331 struct sn9c102_device* cam = usb_get_intfdata(intf);
3336 down_write(&sn9c102_disconnect);
3338 mutex_lock(&cam->dev_mutex);
3340 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
3342 wake_up_interruptible_all(&cam->open);
3345 DBG(2, "Device /dev/video%d is open! Deregistration and "
3346 "memory deallocation are deferred on close.",
3347 cam->v4ldev->minor);
3348 cam->state |= DEV_MISCONFIGURED;
3349 sn9c102_stop_transfer(cam);
3350 cam->state |= DEV_DISCONNECTED;
3351 wake_up_interruptible(&cam->wait_frame);
3352 wake_up(&cam->wait_stream);
3353 usb_get_dev(cam->usbdev);
3355 cam->state |= DEV_DISCONNECTED;
3356 sn9c102_release_resources(cam);
3359 mutex_unlock(&cam->dev_mutex);
3364 up_write(&sn9c102_disconnect);
3368 static struct usb_driver sn9c102_usb_driver = {
3370 .id_table = sn9c102_id_table,
3371 .probe = sn9c102_usb_probe,
3372 .disconnect = sn9c102_usb_disconnect,
3375 /*****************************************************************************/
3377 static int __init sn9c102_module_init(void)
3381 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
3382 KDBG(3, SN9C102_MODULE_AUTHOR);
3384 if ((err = usb_register(&sn9c102_usb_driver)))
3385 KDBG(1, "usb_register() failed");
3391 static void __exit sn9c102_module_exit(void)
3393 usb_deregister(&sn9c102_usb_driver);
3397 module_init(sn9c102_module_init);
3398 module_exit(sn9c102_module_exit);