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_AUTHOR "(C) 2004-2006 Luca Risolia"
48 #define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
49 #define SN9C102_MODULE_LICENSE "GPL"
50 #define SN9C102_MODULE_VERSION "1:1.34"
51 #define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 34)
53 /*****************************************************************************/
55 MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
57 MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
58 MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
59 MODULE_VERSION(SN9C102_MODULE_VERSION);
60 MODULE_LICENSE(SN9C102_MODULE_LICENSE);
62 static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
63 module_param_array(video_nr, short, NULL, 0444);
64 MODULE_PARM_DESC(video_nr,
65 "\n<-1|n[,...]> Specify V4L2 minor mode number."
66 "\n -1 = use next available (default)"
67 "\n n = use minor number n (integer >= 0)"
68 "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
71 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
72 "\nthe second camera and use auto for the first"
73 "\none and for every other camera."
76 static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
77 SN9C102_FORCE_MUNMAP};
78 module_param_array(force_munmap, bool, NULL, 0444);
79 MODULE_PARM_DESC(force_munmap,
80 "\n<0|1[,...]> Force the application to unmap previously"
81 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
82 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
83 "\nthis feature. This parameter is specific for each"
85 "\n 0 = do not force memory unmapping"
86 "\n 1 = force memory unmapping (save memory)"
87 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
90 static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] =
91 SN9C102_FRAME_TIMEOUT};
92 module_param_array(frame_timeout, uint, NULL, 0644);
93 MODULE_PARM_DESC(frame_timeout,
94 "\n<0|n[,...]> Timeout for a video frame in seconds before"
95 "\nreturning an I/O error; 0 for infinity."
96 "\nThis parameter is specific for each detected camera."
97 "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"."
101 static unsigned short debug = SN9C102_DEBUG_LEVEL;
102 module_param(debug, ushort, 0644);
103 MODULE_PARM_DESC(debug,
104 "\n<n> Debugging information level, from 0 to 3:"
105 "\n0 = none (use carefully)"
106 "\n1 = critical errors"
107 "\n2 = significant informations"
108 "\n3 = more verbose messages"
109 "\nLevel 3 is useful for testing only, when only "
110 "one device is used."
111 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
115 /*****************************************************************************/
118 sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
119 enum sn9c102_io_method io)
121 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
122 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
123 size_t imagesize = cam->module_param.force_munmap || io == IO_READ ?
124 (p->width * p->height * p->priv) / 8 :
125 (r->width * r->height * p->priv) / 8;
129 if (count > SN9C102_MAX_FRAMES)
130 count = SN9C102_MAX_FRAMES;
132 if (cam->bridge == BRIDGE_SN9C105 || cam->bridge == BRIDGE_SN9C120)
133 imagesize += 589 + 2; /* length of JPEG header + EOI marker */
135 cam->nbuffers = count;
136 while (cam->nbuffers > 0) {
137 if ((buff = vmalloc_32_user(cam->nbuffers *
138 PAGE_ALIGN(imagesize))))
143 for (i = 0; i < cam->nbuffers; i++) {
144 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
145 cam->frame[i].buf.index = i;
146 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
147 cam->frame[i].buf.length = imagesize;
148 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
149 cam->frame[i].buf.sequence = 0;
150 cam->frame[i].buf.field = V4L2_FIELD_NONE;
151 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
152 cam->frame[i].buf.flags = 0;
155 return cam->nbuffers;
159 static void sn9c102_release_buffers(struct sn9c102_device* cam)
162 vfree(cam->frame[0].bufmem);
165 cam->frame_current = NULL;
169 static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
173 INIT_LIST_HEAD(&cam->inqueue);
174 INIT_LIST_HEAD(&cam->outqueue);
176 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
177 cam->frame[i].state = F_UNUSED;
178 cam->frame[i].buf.bytesused = 0;
183 static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
185 struct sn9c102_frame_t *i;
187 list_for_each_entry(i, &cam->outqueue, frame) {
189 list_add(&i->frame, &cam->inqueue);
192 INIT_LIST_HEAD(&cam->outqueue);
196 static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
198 unsigned long lock_flags;
201 for (i = 0; i < cam->nbuffers; i++)
202 if (cam->frame[i].state == F_UNUSED) {
203 cam->frame[i].state = F_QUEUED;
204 spin_lock_irqsave(&cam->queue_lock, lock_flags);
205 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
206 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
210 /*****************************************************************************/
212 int sn9c102_write_regs(struct sn9c102_device* cam, u8* buff, u16 index)
214 struct usb_device* udev = cam->usbdev;
217 if (index + sizeof(buff) >= ARRAY_SIZE(cam->reg))
220 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
221 index, 0, buff, sizeof(buff),
222 SN9C102_CTRL_TIMEOUT*sizeof(buff));
224 DBG(3, "Failed to write registers (index 0x%02X, error %d)",
229 for (i = 0; i < sizeof(buff); i++)
230 cam->reg[index+i] = buff[i];
236 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
238 struct usb_device* udev = cam->usbdev;
239 u8* buff = cam->control_buffer;
242 if (index >= ARRAY_SIZE(cam->reg))
247 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
248 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
250 DBG(3, "Failed to write a register (value 0x%02X, index "
251 "0x%02X, error %d)", value, index, res);
255 cam->reg[index] = value;
261 /* NOTE: reading some registers always returns 0 */
262 static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
264 struct usb_device* udev = cam->usbdev;
265 u8* buff = cam->control_buffer;
268 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
269 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
271 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
274 return (res >= 0) ? (int)(*buff) : -1;
278 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
280 if (index >= ARRAY_SIZE(cam->reg))
283 return cam->reg[index];
288 sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
292 for (i = 1; i <= 5; i++) {
293 r = sn9c102_read_reg(cam, 0x08);
298 if (sensor->frequency & SN9C102_I2C_400KHZ)
308 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
309 struct sn9c102_sensor* sensor)
313 r = sn9c102_read_reg(cam, 0x08);
317 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
325 return err ? -EIO : 0;
330 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
331 struct sn9c102_sensor* sensor)
334 r = sn9c102_read_reg(cam, 0x08);
335 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
340 sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
341 struct sn9c102_sensor* sensor, u8 data0, u8 data1,
344 struct usb_device* udev = cam->usbdev;
345 u8* data = cam->control_buffer;
349 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
350 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
351 data[1] = data0; /* I2C slave id */
352 data[2] = data1; /* address */
354 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
355 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
359 err += sn9c102_i2c_wait(cam, sensor);
361 /* Read cycle - n bytes */
362 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
363 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
367 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
368 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
372 err += sn9c102_i2c_wait(cam, sensor);
374 /* The first read byte will be placed in data[4] */
375 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
376 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
380 err += sn9c102_i2c_detect_read_error(cam, sensor);
382 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
386 DBG(3, "I2C read failed for %s image sensor", sensor->name);
391 memcpy(buffer, data, sizeof(buffer));
398 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
399 struct sn9c102_sensor* sensor, u8 n, u8 data0,
400 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
402 struct usb_device* udev = cam->usbdev;
403 u8* data = cam->control_buffer;
406 /* Write cycle. It usually is address + value */
407 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
408 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
417 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
418 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
422 err += sn9c102_i2c_wait(cam, sensor);
423 err += sn9c102_i2c_detect_write_error(cam, sensor);
426 DBG(3, "I2C write failed for %s image sensor", sensor->name);
428 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
429 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
430 n, data0, data1, data2, data3, data4, data5);
437 sn9c102_i2c_try_read(struct sn9c102_device* cam,
438 struct sn9c102_sensor* sensor, u8 address)
440 return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
446 sn9c102_i2c_try_write(struct sn9c102_device* cam,
447 struct sn9c102_sensor* sensor, u8 address, u8 value)
449 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
450 sensor->i2c_slave_id, address,
455 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
457 return sn9c102_i2c_try_read(cam, &cam->sensor, address);
461 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
463 return sn9c102_i2c_try_write(cam, &cam->sensor, address, value);
466 /*****************************************************************************/
468 static size_t sn9c102_sof_length(struct sn9c102_device* cam)
470 switch (cam->bridge) {
486 sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
488 char sof_header[6] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
489 size_t soflen = 0, i;
491 soflen = sn9c102_sof_length(cam);
493 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
494 if (!memcmp(mem + i, sof_header, sizeof(sof_header))) {
495 memcpy(cam->sof_header, mem + i,
496 sizeof(sn9c102_sof_header_t));
497 /* Skip the header */
498 return mem + i + soflen;
506 sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
508 char eof_header[4][4] = {
509 {0x00, 0x00, 0x00, 0x00},
510 {0x40, 0x00, 0x00, 0x00},
511 {0x80, 0x00, 0x00, 0x00},
512 {0xc0, 0x00, 0x00, 0x00},
516 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
517 cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
518 return NULL; /* EOF header does not exist in compressed data */
520 for (i = 0; (len >= 4) && (i <= len - 4); i++)
521 for (j = 0; j < ARRAY_SIZE(eof_header); j++)
522 if (!memcmp(mem + i, eof_header[j], 4))
530 sn9c102_write_jpegheader(struct sn9c102_device* cam, struct sn9c102_frame_t* f)
532 static u8 jpeg_header[589] = {
533 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00, 0x06, 0x04, 0x05,
534 0x06, 0x05, 0x04, 0x06, 0x06, 0x05, 0x06, 0x07, 0x07, 0x06,
535 0x08, 0x0a, 0x10, 0x0a, 0x0a, 0x09, 0x09, 0x0a, 0x14, 0x0e,
536 0x0f, 0x0c, 0x10, 0x17, 0x14, 0x18, 0x18, 0x17, 0x14, 0x16,
537 0x16, 0x1a, 0x1d, 0x25, 0x1f, 0x1a, 0x1b, 0x23, 0x1c, 0x16,
538 0x16, 0x20, 0x2c, 0x20, 0x23, 0x26, 0x27, 0x29, 0x2a, 0x29,
539 0x19, 0x1f, 0x2d, 0x30, 0x2d, 0x28, 0x30, 0x25, 0x28, 0x29,
540 0x28, 0x01, 0x07, 0x07, 0x07, 0x0a, 0x08, 0x0a, 0x13, 0x0a,
541 0x0a, 0x13, 0x28, 0x1a, 0x16, 0x1a, 0x28, 0x28, 0x28, 0x28,
542 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
543 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
544 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
545 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
546 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0xff, 0xc4, 0x01, 0xa2,
547 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
548 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
549 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01,
550 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
551 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
552 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10, 0x00,
553 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,
554 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04,
555 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
556 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,
557 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62,
558 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,
559 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38,
560 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
561 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
562 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
563 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
564 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
565 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
566 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
567 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3,
568 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3,
569 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3,
570 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02,
571 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04,
572 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
573 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
574 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1,
575 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
576 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19,
577 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
578 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
579 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
580 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
581 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
582 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
583 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
584 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
585 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
586 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3,
587 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
588 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc0, 0x00, 0x11,
589 0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x01, 0x21, 0x00, 0x02,
590 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03,
591 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00
595 memcpy(pos, jpeg_header, sizeof(jpeg_header));
597 *(pos + 7 + 64) = 0x01;
598 if (cam->compression.quality == 0) {
599 memcpy(pos + 7, SN9C102_Y_QTABLE0, 64);
600 memcpy(pos + 8 + 64, SN9C102_UV_QTABLE0, 64);
601 } else if (cam->compression.quality == 1) {
602 memcpy(pos + 7, SN9C102_Y_QTABLE1, 64);
603 memcpy(pos + 8 + 64, SN9C102_UV_QTABLE1, 64);
605 *(pos + 564) = cam->sensor.pix_format.width & 0xFF;
606 *(pos + 563) = (cam->sensor.pix_format.width >> 8) & 0xFF;
607 *(pos + 562) = cam->sensor.pix_format.height & 0xFF;
608 *(pos + 561) = (cam->sensor.pix_format.height >> 8) & 0xFF;
611 f->buf.bytesused += sizeof(jpeg_header);
616 sn9c102_write_eoimarker(struct sn9c102_device* cam, struct sn9c102_frame_t* f)
618 static const u8 eoi_marker[2] = {0xff, 0xd9};
620 memcpy(f->bufmem + f->buf.bytesused, eoi_marker, sizeof(eoi_marker));
621 f->buf.bytesused += sizeof(eoi_marker);
625 static void sn9c102_urb_complete(struct urb *urb)
627 struct sn9c102_device* cam = urb->context;
628 struct sn9c102_frame_t** f;
629 size_t imagesize, soflen;
633 if (urb->status == -ENOENT)
636 f = &cam->frame_current;
638 if (cam->stream == STREAM_INTERRUPT) {
639 cam->stream = STREAM_OFF;
641 (*f)->state = F_QUEUED;
642 DBG(3, "Stream interrupted by application");
643 wake_up(&cam->wait_stream);
646 if (cam->state & DEV_DISCONNECTED)
649 if (cam->state & DEV_MISCONFIGURED) {
650 wake_up_interruptible(&cam->wait_frame);
654 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
658 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
661 imagesize = (cam->sensor.pix_format.width *
662 cam->sensor.pix_format.height *
663 cam->sensor.pix_format.priv) / 8;
664 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
665 imagesize += 589; /* length of jpeg header */
666 soflen = sn9c102_sof_length(cam);
668 for (i = 0; i < urb->number_of_packets; i++) {
669 unsigned int img, len, status;
670 void *pos, *sof, *eof;
672 len = urb->iso_frame_desc[i].actual_length;
673 status = urb->iso_frame_desc[i].status;
674 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
677 DBG(3, "Error in isochronous frame");
678 (*f)->state = F_ERROR;
682 PDBGG("Isochrnous frame: length %u, #%u i", len, i);
685 sof = sn9c102_find_sof_header(cam, pos, len);
687 eof = sn9c102_find_eof_header(cam, pos, len);
688 if ((*f)->state == F_GRABBING) {
693 img = (eof > pos) ? eof - pos - 1 : 0;
695 if ((*f)->buf.bytesused+img > imagesize) {
697 b = (*f)->buf.bytesused + img -
699 img = imagesize - (*f)->buf.bytesused;
700 DBG(3, "Expected EOF not found: "
703 DBG(3, "Exceeded limit: +%u "
704 "bytes", (unsigned)(b));
707 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
710 if ((*f)->buf.bytesused == 0)
711 do_gettimeofday(&(*f)->buf.timestamp);
713 (*f)->buf.bytesused += img;
715 if ((*f)->buf.bytesused == imagesize ||
716 ((cam->sensor.pix_format.pixelformat ==
717 V4L2_PIX_FMT_SN9C10X ||
718 cam->sensor.pix_format.pixelformat ==
719 V4L2_PIX_FMT_JPEG) && eof)) {
722 if (cam->sensor.pix_format.pixelformat
723 == V4L2_PIX_FMT_JPEG)
724 sn9c102_write_eoimarker(cam,
727 b = (*f)->buf.bytesused;
728 (*f)->state = F_DONE;
729 (*f)->buf.sequence= ++cam->frame_count;
731 spin_lock(&cam->queue_lock);
732 list_move_tail(&(*f)->frame,
734 if (!list_empty(&cam->inqueue))
737 struct sn9c102_frame_t,
741 spin_unlock(&cam->queue_lock);
743 memcpy(cam->sysfs.frame_header,
744 cam->sof_header, soflen);
746 DBG(3, "Video frame captured: %lu "
747 "bytes", (unsigned long)(b));
753 (*f)->state = F_ERROR;
754 DBG(3, "Not expected EOF after %lu "
755 "bytes of image data",
757 ((*f)->buf.bytesused));
764 DBG(3, "EOF without SOF");
768 PDBGG("Ignoring pointless isochronous frame");
772 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
774 (*f)->state = F_GRABBING;
775 (*f)->buf.bytesused = 0;
778 if (cam->sensor.pix_format.pixelformat ==
780 sn9c102_write_jpegheader(cam, (*f));
781 DBG(3, "SOF detected: new video frame");
785 } else if ((*f)->state == F_GRABBING) {
786 eof = sn9c102_find_eof_header(cam, pos, len);
787 if (eof && eof < sof)
788 goto end_of_frame; /* (1) */
790 if (cam->sensor.pix_format.pixelformat ==
791 V4L2_PIX_FMT_SN9C10X ||
792 cam->sensor.pix_format.pixelformat ==
797 DBG(3, "SOF before expected EOF after "
798 "%lu bytes of image data",
800 ((*f)->buf.bytesused));
808 urb->dev = cam->usbdev;
809 err = usb_submit_urb(urb, GFP_ATOMIC);
810 if (err < 0 && err != -EPERM) {
811 cam->state |= DEV_MISCONFIGURED;
812 DBG(1, "usb_submit_urb() failed");
815 wake_up_interruptible(&cam->wait_frame);
819 static int sn9c102_start_transfer(struct sn9c102_device* cam)
821 struct usb_device *udev = cam->usbdev;
823 struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
824 usb_ifnum_to_if(udev, 0),
825 SN9C102_ALTERNATE_SETTING);
826 const unsigned int psz = le16_to_cpu(altsetting->
827 endpoint[0].desc.wMaxPacketSize);
831 for (i = 0; i < SN9C102_URBS; i++) {
832 cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
834 if (!cam->transfer_buffer[i]) {
836 DBG(1, "Not enough memory");
841 for (i = 0; i < SN9C102_URBS; i++) {
842 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
846 DBG(1, "usb_alloc_urb() failed");
851 urb->pipe = usb_rcvisocpipe(udev, 1);
852 urb->transfer_flags = URB_ISO_ASAP;
853 urb->number_of_packets = SN9C102_ISO_PACKETS;
854 urb->complete = sn9c102_urb_complete;
855 urb->transfer_buffer = cam->transfer_buffer[i];
856 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
858 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
859 urb->iso_frame_desc[j].offset = psz * j;
860 urb->iso_frame_desc[j].length = psz;
865 if (!(cam->reg[0x01] & 0x04)) {
866 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
869 DBG(1, "I/O hardware error");
874 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
876 DBG(1, "usb_set_interface() failed");
880 cam->frame_current = NULL;
882 for (i = 0; i < SN9C102_URBS; i++) {
883 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
885 for (j = i-1; j >= 0; j--)
886 usb_kill_urb(cam->urb[j]);
887 DBG(1, "usb_submit_urb() failed, error %d", err);
895 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
896 usb_free_urb(cam->urb[i]);
899 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
900 kfree(cam->transfer_buffer[i]);
906 static int sn9c102_stop_transfer(struct sn9c102_device* cam)
908 struct usb_device *udev = cam->usbdev;
912 if (cam->state & DEV_DISCONNECTED)
915 for (i = SN9C102_URBS-1; i >= 0; i--) {
916 usb_kill_urb(cam->urb[i]);
917 usb_free_urb(cam->urb[i]);
918 kfree(cam->transfer_buffer[i]);
921 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
923 DBG(3, "usb_set_interface() failed");
929 static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
933 cam->stream = STREAM_INTERRUPT;
934 timeout = wait_event_timeout(cam->wait_stream,
935 (cam->stream == STREAM_OFF) ||
936 (cam->state & DEV_DISCONNECTED),
937 SN9C102_URB_TIMEOUT);
938 if (cam->state & DEV_DISCONNECTED)
940 else if (cam->stream != STREAM_OFF) {
941 cam->state |= DEV_MISCONFIGURED;
942 DBG(1, "URB timeout reached. The camera is misconfigured. "
943 "To use it, close and open /dev/video%d again.",
951 /*****************************************************************************/
953 #ifdef CONFIG_VIDEO_ADV_DEBUG
954 static u16 sn9c102_strtou16(const char* buff, size_t len, ssize_t* count)
961 strncpy(str, buff, len);
964 strncpy(str, buff, 4);
968 val = simple_strtoul(str, &endp, 0);
972 *count = (ssize_t)(endp - str);
973 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
980 NOTE 1: being inside one of the following methods implies that the v4l
981 device exists for sure (see kobjects and reference counters)
982 NOTE 2: buffers are PAGE_SIZE long
985 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
987 struct sn9c102_device* cam;
990 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
993 cam = video_get_drvdata(container_of(cd, struct video_device,
996 mutex_unlock(&sn9c102_sysfs_lock);
1000 count = sprintf(buf, "%u\n", cam->sysfs.reg);
1002 mutex_unlock(&sn9c102_sysfs_lock);
1009 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
1011 struct sn9c102_device* cam;
1015 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1016 return -ERESTARTSYS;
1018 cam = video_get_drvdata(container_of(cd, struct video_device,
1021 mutex_unlock(&sn9c102_sysfs_lock);
1025 index = sn9c102_strtou16(buf, len, &count);
1026 if (index >= ARRAY_SIZE(cam->reg) || !count) {
1027 mutex_unlock(&sn9c102_sysfs_lock);
1031 cam->sysfs.reg = index;
1033 DBG(2, "Moved SN9C1XX register index to 0x%02X", cam->sysfs.reg);
1034 DBG(3, "Written bytes: %zd", count);
1036 mutex_unlock(&sn9c102_sysfs_lock);
1042 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
1044 struct sn9c102_device* cam;
1048 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1049 return -ERESTARTSYS;
1051 cam = video_get_drvdata(container_of(cd, struct video_device,
1054 mutex_unlock(&sn9c102_sysfs_lock);
1058 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
1059 mutex_unlock(&sn9c102_sysfs_lock);
1063 count = sprintf(buf, "%d\n", val);
1065 DBG(3, "Read bytes: %zd", count);
1067 mutex_unlock(&sn9c102_sysfs_lock);
1074 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
1076 struct sn9c102_device* cam;
1081 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1082 return -ERESTARTSYS;
1084 cam = video_get_drvdata(container_of(cd, struct video_device,
1087 mutex_unlock(&sn9c102_sysfs_lock);
1091 value = sn9c102_strtou16(buf, len, &count);
1093 mutex_unlock(&sn9c102_sysfs_lock);
1097 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
1099 mutex_unlock(&sn9c102_sysfs_lock);
1103 DBG(2, "Written SN9C1XX reg. 0x%02X, val. 0x%02X",
1104 cam->sysfs.reg, value);
1105 DBG(3, "Written bytes: %zd", count);
1107 mutex_unlock(&sn9c102_sysfs_lock);
1113 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
1115 struct sn9c102_device* cam;
1118 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1119 return -ERESTARTSYS;
1121 cam = video_get_drvdata(container_of(cd, struct video_device,
1124 mutex_unlock(&sn9c102_sysfs_lock);
1128 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
1130 DBG(3, "Read bytes: %zd", count);
1132 mutex_unlock(&sn9c102_sysfs_lock);
1139 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1141 struct sn9c102_device* cam;
1145 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1146 return -ERESTARTSYS;
1148 cam = video_get_drvdata(container_of(cd, struct video_device,
1151 mutex_unlock(&sn9c102_sysfs_lock);
1155 index = sn9c102_strtou16(buf, len, &count);
1157 mutex_unlock(&sn9c102_sysfs_lock);
1161 cam->sysfs.i2c_reg = index;
1163 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1164 DBG(3, "Written bytes: %zd", count);
1166 mutex_unlock(&sn9c102_sysfs_lock);
1172 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1174 struct sn9c102_device* cam;
1178 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1179 return -ERESTARTSYS;
1181 cam = video_get_drvdata(container_of(cd, struct video_device,
1184 mutex_unlock(&sn9c102_sysfs_lock);
1188 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) {
1189 mutex_unlock(&sn9c102_sysfs_lock);
1193 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1194 mutex_unlock(&sn9c102_sysfs_lock);
1198 count = sprintf(buf, "%d\n", val);
1200 DBG(3, "Read bytes: %zd", count);
1202 mutex_unlock(&sn9c102_sysfs_lock);
1209 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1211 struct sn9c102_device* cam;
1216 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1217 return -ERESTARTSYS;
1219 cam = video_get_drvdata(container_of(cd, struct video_device,
1222 mutex_unlock(&sn9c102_sysfs_lock);
1226 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) {
1227 mutex_unlock(&sn9c102_sysfs_lock);
1231 value = sn9c102_strtou16(buf, len, &count);
1233 mutex_unlock(&sn9c102_sysfs_lock);
1237 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1239 mutex_unlock(&sn9c102_sysfs_lock);
1243 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1244 cam->sysfs.i2c_reg, value);
1245 DBG(3, "Written bytes: %zd", count);
1247 mutex_unlock(&sn9c102_sysfs_lock);
1254 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1256 struct sn9c102_device* cam;
1257 enum sn9c102_bridge bridge;
1262 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1263 return -ERESTARTSYS;
1265 cam = video_get_drvdata(container_of(cd, struct video_device,
1268 mutex_unlock(&sn9c102_sysfs_lock);
1272 bridge = cam->bridge;
1274 mutex_unlock(&sn9c102_sysfs_lock);
1276 value = sn9c102_strtou16(buf, len, &count);
1281 case BRIDGE_SN9C101:
1282 case BRIDGE_SN9C102:
1285 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1286 res = sn9c102_store_val(cd, buf, len);
1288 case BRIDGE_SN9C103:
1289 case BRIDGE_SN9C105:
1290 case BRIDGE_SN9C120:
1293 if ((res = sn9c102_store_reg(cd, "0x07", 4)) >= 0)
1294 res = sn9c102_store_val(cd, buf, len);
1303 sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1309 value = sn9c102_strtou16(buf, len, &count);
1310 if (!count || value > 0x7f)
1313 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1314 res = sn9c102_store_val(cd, buf, len);
1321 sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1327 value = sn9c102_strtou16(buf, len, &count);
1328 if (!count || value > 0x7f)
1331 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1332 res = sn9c102_store_val(cd, buf, len);
1338 static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1340 struct sn9c102_device* cam;
1343 cam = video_get_drvdata(container_of(cd, struct video_device,
1348 count = sizeof(cam->sysfs.frame_header);
1349 memcpy(buf, cam->sysfs.frame_header, count);
1351 DBG(3, "Frame header, read bytes: %zd", count);
1357 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1358 sn9c102_show_reg, sn9c102_store_reg);
1359 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1360 sn9c102_show_val, sn9c102_store_val);
1361 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1362 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1363 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1364 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1365 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1366 static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1367 static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1368 static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
1369 sn9c102_show_frame_header, NULL);
1372 static int sn9c102_create_sysfs(struct sn9c102_device* cam)
1374 struct video_device *v4ldev = cam->v4ldev;
1377 if ((err = video_device_create_file(v4ldev, &class_device_attr_reg)))
1379 if ((err = video_device_create_file(v4ldev, &class_device_attr_val)))
1381 if ((err = video_device_create_file(v4ldev,
1382 &class_device_attr_frame_header)))
1385 if (cam->sensor.sysfs_ops) {
1386 if ((err = video_device_create_file(v4ldev,
1387 &class_device_attr_i2c_reg)))
1388 goto err_frame_header;
1389 if ((err = video_device_create_file(v4ldev,
1390 &class_device_attr_i2c_val)))
1394 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
1395 if ((err = video_device_create_file(v4ldev,
1396 &class_device_attr_green)))
1399 if ((err = video_device_create_file(v4ldev,
1400 &class_device_attr_blue)))
1402 if ((err = video_device_create_file(v4ldev,
1403 &class_device_attr_red)))
1410 video_device_remove_file(v4ldev, &class_device_attr_blue);
1412 if (cam->sensor.sysfs_ops)
1413 video_device_remove_file(v4ldev, &class_device_attr_i2c_val);
1415 if (cam->sensor.sysfs_ops)
1416 video_device_remove_file(v4ldev, &class_device_attr_i2c_reg);
1418 video_device_remove_file(v4ldev, &class_device_attr_frame_header);
1420 video_device_remove_file(v4ldev, &class_device_attr_val);
1422 video_device_remove_file(v4ldev, &class_device_attr_reg);
1426 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1428 /*****************************************************************************/
1431 sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
1435 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
1436 pix->pixelformat == V4L2_PIX_FMT_JPEG) {
1437 switch (cam->bridge) {
1438 case BRIDGE_SN9C101:
1439 case BRIDGE_SN9C102:
1440 case BRIDGE_SN9C103:
1441 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
1444 case BRIDGE_SN9C105:
1445 case BRIDGE_SN9C120:
1446 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
1451 switch (cam->bridge) {
1452 case BRIDGE_SN9C101:
1453 case BRIDGE_SN9C102:
1454 case BRIDGE_SN9C103:
1455 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
1458 case BRIDGE_SN9C105:
1459 case BRIDGE_SN9C120:
1460 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
1466 return err ? -EIO : 0;
1471 sn9c102_set_compression(struct sn9c102_device* cam,
1472 struct v4l2_jpegcompression* compression)
1476 switch (cam->bridge) {
1477 case BRIDGE_SN9C101:
1478 case BRIDGE_SN9C102:
1479 case BRIDGE_SN9C103:
1480 if (compression->quality == 0)
1481 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01,
1483 else if (compression->quality == 1)
1484 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe,
1487 case BRIDGE_SN9C105:
1488 case BRIDGE_SN9C120:
1489 if (compression->quality == 0) {
1490 for (i = 0; i <= 63; i++) {
1491 err += sn9c102_write_reg(cam,
1492 SN9C102_Y_QTABLE0[i],
1494 err += sn9c102_write_reg(cam,
1495 SN9C102_UV_QTABLE0[i],
1498 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0xbf,
1500 } else if (compression->quality == 1) {
1501 for (i = 0; i <= 63; i++) {
1502 err += sn9c102_write_reg(cam,
1503 SN9C102_Y_QTABLE1[i],
1505 err += sn9c102_write_reg(cam,
1506 SN9C102_UV_QTABLE1[i],
1509 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x40,
1515 return err ? -EIO : 0;
1519 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1525 r = cam->reg[0x18] & 0xcf;
1526 else if (scale == 2) {
1527 r = cam->reg[0x18] & 0xcf;
1529 } else if (scale == 4)
1530 r = cam->reg[0x18] | 0x20;
1532 err += sn9c102_write_reg(cam, r, 0x18);
1536 PDBGG("Scaling factor: %u", scale);
1542 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1544 struct sn9c102_sensor* s = &cam->sensor;
1545 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1546 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1547 h_size = (u8)(rect->width / 16),
1548 v_size = (u8)(rect->height / 16);
1551 err += sn9c102_write_reg(cam, h_start, 0x12);
1552 err += sn9c102_write_reg(cam, v_start, 0x13);
1553 err += sn9c102_write_reg(cam, h_size, 0x15);
1554 err += sn9c102_write_reg(cam, v_size, 0x16);
1558 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1559 "%u %u %u %u", h_start, v_start, h_size, v_size);
1565 static int sn9c102_init(struct sn9c102_device* cam)
1567 struct sn9c102_sensor* s = &cam->sensor;
1568 struct v4l2_control ctrl;
1569 struct v4l2_queryctrl *qctrl;
1570 struct v4l2_rect* rect;
1574 if (!(cam->state & DEV_INITIALIZED)) {
1575 init_waitqueue_head(&cam->open);
1577 rect = &(s->cropcap.defrect);
1578 } else { /* use current values */
1583 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1584 err += sn9c102_set_crop(cam, rect);
1591 DBG(3, "Sensor initialization failed");
1596 if (!(cam->state & DEV_INITIALIZED))
1597 if (cam->bridge == BRIDGE_SN9C101 ||
1598 cam->bridge == BRIDGE_SN9C102 ||
1599 cam->bridge == BRIDGE_SN9C103) {
1600 cam->compression.quality = cam->reg[0x17] & 0x01 ?
1603 cam->compression.quality = cam->reg[0x18] & 0x40 ?
1605 err += sn9c102_set_compression(cam, &cam->compression);
1608 err += sn9c102_set_compression(cam, &cam->compression);
1609 err += sn9c102_set_pix_format(cam, &s->pix_format);
1610 if (s->set_pix_format)
1611 err += s->set_pix_format(cam, &s->pix_format);
1615 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
1616 s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
1617 DBG(3, "Compressed video format is active, quality %d",
1618 cam->compression.quality);
1620 DBG(3, "Uncompressed video format is active");
1623 if ((err = s->set_crop(cam, rect))) {
1624 DBG(3, "set_crop() failed");
1629 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1630 if (s->qctrl[i].id != 0 &&
1631 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1632 ctrl.id = s->qctrl[i].id;
1633 ctrl.value = qctrl[i].default_value;
1634 err = s->set_ctrl(cam, &ctrl);
1636 DBG(3, "Set %s control failed",
1640 DBG(3, "Image sensor supports '%s' control",
1645 if (!(cam->state & DEV_INITIALIZED)) {
1646 mutex_init(&cam->fileop_mutex);
1647 spin_lock_init(&cam->queue_lock);
1648 init_waitqueue_head(&cam->wait_frame);
1649 init_waitqueue_head(&cam->wait_stream);
1650 cam->nreadbuffers = 2;
1651 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1652 memcpy(&(s->_rect), &(s->cropcap.defrect),
1653 sizeof(struct v4l2_rect));
1654 cam->state |= DEV_INITIALIZED;
1657 DBG(2, "Initialization succeeded");
1662 static void sn9c102_release_resources(struct sn9c102_device* cam)
1664 mutex_lock(&sn9c102_sysfs_lock);
1666 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1667 video_set_drvdata(cam->v4ldev, NULL);
1668 video_unregister_device(cam->v4ldev);
1670 mutex_unlock(&sn9c102_sysfs_lock);
1672 kfree(cam->control_buffer);
1675 /*****************************************************************************/
1677 static int sn9c102_open(struct inode* inode, struct file* filp)
1679 struct sn9c102_device* cam;
1683 This is the only safe way to prevent race conditions with
1686 if (!down_read_trylock(&sn9c102_disconnect))
1687 return -ERESTARTSYS;
1689 cam = video_get_drvdata(video_devdata(filp));
1691 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1692 up_read(&sn9c102_disconnect);
1693 return -ERESTARTSYS;
1697 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1698 DBG(3, "Simultaneous opens are not supported");
1699 if ((filp->f_flags & O_NONBLOCK) ||
1700 (filp->f_flags & O_NDELAY)) {
1704 mutex_unlock(&cam->dev_mutex);
1705 err = wait_event_interruptible_exclusive(cam->open,
1706 cam->state & DEV_DISCONNECTED
1709 up_read(&sn9c102_disconnect);
1712 if (cam->state & DEV_DISCONNECTED) {
1713 up_read(&sn9c102_disconnect);
1716 mutex_lock(&cam->dev_mutex);
1720 if (cam->state & DEV_MISCONFIGURED) {
1721 err = sn9c102_init(cam);
1723 DBG(1, "Initialization failed again. "
1724 "I will retry on next open().");
1727 cam->state &= ~DEV_MISCONFIGURED;
1730 if ((err = sn9c102_start_transfer(cam)))
1733 filp->private_data = cam;
1736 cam->stream = STREAM_OFF;
1738 cam->frame_count = 0;
1739 sn9c102_empty_framequeues(cam);
1741 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1744 mutex_unlock(&cam->dev_mutex);
1745 up_read(&sn9c102_disconnect);
1750 static int sn9c102_release(struct inode* inode, struct file* filp)
1752 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1754 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1756 sn9c102_stop_transfer(cam);
1758 sn9c102_release_buffers(cam);
1760 if (cam->state & DEV_DISCONNECTED) {
1761 sn9c102_release_resources(cam);
1762 usb_put_dev(cam->usbdev);
1763 mutex_unlock(&cam->dev_mutex);
1769 wake_up_interruptible_nr(&cam->open, 1);
1771 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1773 mutex_unlock(&cam->dev_mutex);
1780 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1782 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1783 struct sn9c102_frame_t* f, * i;
1784 unsigned long lock_flags;
1788 if (mutex_lock_interruptible(&cam->fileop_mutex))
1789 return -ERESTARTSYS;
1791 if (cam->state & DEV_DISCONNECTED) {
1792 DBG(1, "Device not present");
1793 mutex_unlock(&cam->fileop_mutex);
1797 if (cam->state & DEV_MISCONFIGURED) {
1798 DBG(1, "The camera is misconfigured. Close and open it "
1800 mutex_unlock(&cam->fileop_mutex);
1804 if (cam->io == IO_MMAP) {
1805 DBG(3, "Close and open the device again to choose "
1807 mutex_unlock(&cam->fileop_mutex);
1811 if (cam->io == IO_NONE) {
1812 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1813 DBG(1, "read() failed, not enough memory");
1814 mutex_unlock(&cam->fileop_mutex);
1818 cam->stream = STREAM_ON;
1821 if (list_empty(&cam->inqueue)) {
1822 if (!list_empty(&cam->outqueue))
1823 sn9c102_empty_framequeues(cam);
1824 sn9c102_queue_unusedframes(cam);
1828 mutex_unlock(&cam->fileop_mutex);
1832 if (list_empty(&cam->outqueue)) {
1833 if (filp->f_flags & O_NONBLOCK) {
1834 mutex_unlock(&cam->fileop_mutex);
1837 if (!cam->module_param.frame_timeout) {
1838 err = wait_event_interruptible
1840 (!list_empty(&cam->outqueue)) ||
1841 (cam->state & DEV_DISCONNECTED) ||
1842 (cam->state & DEV_MISCONFIGURED) );
1844 mutex_unlock(&cam->fileop_mutex);
1848 timeout = wait_event_interruptible_timeout
1850 (!list_empty(&cam->outqueue)) ||
1851 (cam->state & DEV_DISCONNECTED) ||
1852 (cam->state & DEV_MISCONFIGURED),
1853 cam->module_param.frame_timeout *
1854 1000 * msecs_to_jiffies(1) );
1856 mutex_unlock(&cam->fileop_mutex);
1858 } else if (timeout == 0 &&
1859 !(cam->state & DEV_DISCONNECTED)) {
1860 DBG(1, "Video frame timeout elapsed");
1861 mutex_unlock(&cam->fileop_mutex);
1865 if (cam->state & DEV_DISCONNECTED) {
1866 mutex_unlock(&cam->fileop_mutex);
1869 if (cam->state & DEV_MISCONFIGURED) {
1870 mutex_unlock(&cam->fileop_mutex);
1875 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1877 if (count > f->buf.bytesused)
1878 count = f->buf.bytesused;
1880 if (copy_to_user(buf, f->bufmem, count)) {
1887 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1888 list_for_each_entry(i, &cam->outqueue, frame)
1889 i->state = F_UNUSED;
1890 INIT_LIST_HEAD(&cam->outqueue);
1891 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1893 sn9c102_queue_unusedframes(cam);
1895 PDBGG("Frame #%lu, bytes read: %zu",
1896 (unsigned long)f->buf.index, count);
1898 mutex_unlock(&cam->fileop_mutex);
1904 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1906 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1907 struct sn9c102_frame_t* f;
1908 unsigned long lock_flags;
1909 unsigned int mask = 0;
1911 if (mutex_lock_interruptible(&cam->fileop_mutex))
1914 if (cam->state & DEV_DISCONNECTED) {
1915 DBG(1, "Device not present");
1919 if (cam->state & DEV_MISCONFIGURED) {
1920 DBG(1, "The camera is misconfigured. Close and open it "
1925 if (cam->io == IO_NONE) {
1926 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1928 DBG(1, "poll() failed, not enough memory");
1932 cam->stream = STREAM_ON;
1935 if (cam->io == IO_READ) {
1936 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1937 list_for_each_entry(f, &cam->outqueue, frame)
1938 f->state = F_UNUSED;
1939 INIT_LIST_HEAD(&cam->outqueue);
1940 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1941 sn9c102_queue_unusedframes(cam);
1944 poll_wait(filp, &cam->wait_frame, wait);
1946 if (!list_empty(&cam->outqueue))
1947 mask |= POLLIN | POLLRDNORM;
1949 mutex_unlock(&cam->fileop_mutex);
1954 mutex_unlock(&cam->fileop_mutex);
1959 static void sn9c102_vm_open(struct vm_area_struct* vma)
1961 struct sn9c102_frame_t* f = vma->vm_private_data;
1966 static void sn9c102_vm_close(struct vm_area_struct* vma)
1968 /* NOTE: buffers are not freed here */
1969 struct sn9c102_frame_t* f = vma->vm_private_data;
1974 static struct vm_operations_struct sn9c102_vm_ops = {
1975 .open = sn9c102_vm_open,
1976 .close = sn9c102_vm_close,
1980 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1982 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1983 unsigned long size = vma->vm_end - vma->vm_start,
1984 start = vma->vm_start;
1988 if (mutex_lock_interruptible(&cam->fileop_mutex))
1989 return -ERESTARTSYS;
1991 if (cam->state & DEV_DISCONNECTED) {
1992 DBG(1, "Device not present");
1993 mutex_unlock(&cam->fileop_mutex);
1997 if (cam->state & DEV_MISCONFIGURED) {
1998 DBG(1, "The camera is misconfigured. Close and open it "
2000 mutex_unlock(&cam->fileop_mutex);
2004 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
2005 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
2006 mutex_unlock(&cam->fileop_mutex);
2010 for (i = 0; i < cam->nbuffers; i++) {
2011 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
2014 if (i == cam->nbuffers) {
2015 mutex_unlock(&cam->fileop_mutex);
2019 vma->vm_flags |= VM_IO;
2020 vma->vm_flags |= VM_RESERVED;
2022 pos = cam->frame[i].bufmem;
2023 while (size > 0) { /* size is page-aligned */
2024 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
2025 mutex_unlock(&cam->fileop_mutex);
2033 vma->vm_ops = &sn9c102_vm_ops;
2034 vma->vm_private_data = &cam->frame[i];
2036 sn9c102_vm_open(vma);
2038 mutex_unlock(&cam->fileop_mutex);
2043 /*****************************************************************************/
2046 sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
2048 struct v4l2_capability cap = {
2049 .driver = "sn9c102",
2050 .version = SN9C102_MODULE_VERSION_CODE,
2051 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
2055 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
2056 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
2057 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
2058 sizeof(cap.bus_info));
2060 if (copy_to_user(arg, &cap, sizeof(cap)))
2068 sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
2070 struct v4l2_input i;
2072 if (copy_from_user(&i, arg, sizeof(i)))
2078 memset(&i, 0, sizeof(i));
2079 strcpy(i.name, "Camera");
2080 i.type = V4L2_INPUT_TYPE_CAMERA;
2082 if (copy_to_user(arg, &i, sizeof(i)))
2090 sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg)
2094 if (copy_to_user(arg, &index, sizeof(index)))
2102 sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg)
2106 if (copy_from_user(&index, arg, sizeof(index)))
2117 sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
2119 struct sn9c102_sensor* s = &cam->sensor;
2120 struct v4l2_queryctrl qc;
2123 if (copy_from_user(&qc, arg, sizeof(qc)))
2126 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2127 if (qc.id && qc.id == s->qctrl[i].id) {
2128 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
2129 if (copy_to_user(arg, &qc, sizeof(qc)))
2139 sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
2141 struct sn9c102_sensor* s = &cam->sensor;
2142 struct v4l2_control ctrl;
2146 if (!s->get_ctrl && !s->set_ctrl)
2149 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2153 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2154 if (ctrl.id && ctrl.id == s->qctrl[i].id) {
2155 ctrl.value = s->_qctrl[i].default_value;
2160 err = s->get_ctrl(cam, &ctrl);
2163 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
2166 PDBGG("VIDIOC_G_CTRL: id %lu, value %lu",
2167 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
2174 sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
2176 struct sn9c102_sensor* s = &cam->sensor;
2177 struct v4l2_control ctrl;
2184 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2187 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2188 if (ctrl.id == s->qctrl[i].id) {
2189 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
2191 if (ctrl.value < s->qctrl[i].minimum ||
2192 ctrl.value > s->qctrl[i].maximum)
2194 ctrl.value -= ctrl.value % s->qctrl[i].step;
2198 if ((err = s->set_ctrl(cam, &ctrl)))
2201 s->_qctrl[i].default_value = ctrl.value;
2203 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
2204 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
2211 sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
2213 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
2215 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2216 cc->pixelaspect.numerator = 1;
2217 cc->pixelaspect.denominator = 1;
2219 if (copy_to_user(arg, cc, sizeof(*cc)))
2227 sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
2229 struct sn9c102_sensor* s = &cam->sensor;
2230 struct v4l2_crop crop = {
2231 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2234 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
2236 if (copy_to_user(arg, &crop, sizeof(crop)))
2244 sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
2246 struct sn9c102_sensor* s = &cam->sensor;
2247 struct v4l2_crop crop;
2248 struct v4l2_rect* rect;
2249 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2250 struct v4l2_pix_format* pix_format = &(s->pix_format);
2252 const enum sn9c102_stream_state stream = cam->stream;
2253 const u32 nbuffers = cam->nbuffers;
2257 if (copy_from_user(&crop, arg, sizeof(crop)))
2262 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2265 if (cam->module_param.force_munmap)
2266 for (i = 0; i < cam->nbuffers; i++)
2267 if (cam->frame[i].vma_use_count) {
2268 DBG(3, "VIDIOC_S_CROP failed. "
2269 "Unmap the buffers first.");
2273 /* Preserve R,G or B origin */
2274 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
2275 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
2277 if (rect->width < 16)
2279 if (rect->height < 16)
2281 if (rect->width > bounds->width)
2282 rect->width = bounds->width;
2283 if (rect->height > bounds->height)
2284 rect->height = bounds->height;
2285 if (rect->left < bounds->left)
2286 rect->left = bounds->left;
2287 if (rect->top < bounds->top)
2288 rect->top = bounds->top;
2289 if (rect->left + rect->width > bounds->left + bounds->width)
2290 rect->left = bounds->left+bounds->width - rect->width;
2291 if (rect->top + rect->height > bounds->top + bounds->height)
2292 rect->top = bounds->top+bounds->height - rect->height;
2294 rect->width &= ~15L;
2295 rect->height &= ~15L;
2297 if (SN9C102_PRESERVE_IMGSCALE) {
2298 /* Calculate the actual scaling factor */
2300 a = rect->width * rect->height;
2301 b = pix_format->width * pix_format->height;
2302 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2306 if (cam->stream == STREAM_ON)
2307 if ((err = sn9c102_stream_interrupt(cam)))
2310 if (copy_to_user(arg, &crop, sizeof(crop))) {
2311 cam->stream = stream;
2315 if (cam->module_param.force_munmap || cam->io == IO_READ)
2316 sn9c102_release_buffers(cam);
2318 err = sn9c102_set_crop(cam, rect);
2320 err += s->set_crop(cam, rect);
2321 err += sn9c102_set_scale(cam, scale);
2323 if (err) { /* atomic, no rollback in ioctl() */
2324 cam->state |= DEV_MISCONFIGURED;
2325 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2326 "use the camera, close and open /dev/video%d again.",
2327 cam->v4ldev->minor);
2331 s->pix_format.width = rect->width/scale;
2332 s->pix_format.height = rect->height/scale;
2333 memcpy(&(s->_rect), rect, sizeof(*rect));
2335 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2336 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2337 cam->state |= DEV_MISCONFIGURED;
2338 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2339 "use the camera, close and open /dev/video%d again.",
2340 cam->v4ldev->minor);
2344 if (cam->io == IO_READ)
2345 sn9c102_empty_framequeues(cam);
2346 else if (cam->module_param.force_munmap)
2347 sn9c102_requeue_outqueue(cam);
2349 cam->stream = stream;
2356 sn9c102_vidioc_enum_framesizes(struct sn9c102_device* cam, void __user * arg)
2358 struct v4l2_frmsizeenum frmsize;
2360 if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
2363 if (frmsize.index != 0)
2366 switch (cam->bridge) {
2367 case BRIDGE_SN9C101:
2368 case BRIDGE_SN9C102:
2369 case BRIDGE_SN9C103:
2370 if (frmsize.pixel_format != V4L2_PIX_FMT_SN9C10X &&
2371 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
2373 case BRIDGE_SN9C105:
2374 case BRIDGE_SN9C120:
2375 if (frmsize.pixel_format != V4L2_PIX_FMT_JPEG &&
2376 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
2380 frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
2381 frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
2382 frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
2383 frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
2384 frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
2385 memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
2387 if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
2395 sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
2397 struct v4l2_fmtdesc fmtd;
2399 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
2402 if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2405 if (fmtd.index == 0) {
2406 strcpy(fmtd.description, "bayer rgb");
2407 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2408 } else if (fmtd.index == 1) {
2409 switch (cam->bridge) {
2410 case BRIDGE_SN9C101:
2411 case BRIDGE_SN9C102:
2412 case BRIDGE_SN9C103:
2413 strcpy(fmtd.description, "compressed");
2414 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2416 case BRIDGE_SN9C105:
2417 case BRIDGE_SN9C120:
2418 strcpy(fmtd.description, "JPEG");
2419 fmtd.pixelformat = V4L2_PIX_FMT_JPEG;
2422 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2426 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2427 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2429 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
2437 sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2439 struct v4l2_format format;
2440 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
2442 if (copy_from_user(&format, arg, sizeof(format)))
2445 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2448 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X ||
2449 pfmt->pixelformat==V4L2_PIX_FMT_JPEG)
2450 ? 0 : (pfmt->width * pfmt->priv) / 8;
2451 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2452 pfmt->field = V4L2_FIELD_NONE;
2453 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2455 if (copy_to_user(arg, &format, sizeof(format)))
2463 sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2466 struct sn9c102_sensor* s = &cam->sensor;
2467 struct v4l2_format format;
2468 struct v4l2_pix_format* pix;
2469 struct v4l2_pix_format* pfmt = &(s->pix_format);
2470 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2471 struct v4l2_rect rect;
2473 const enum sn9c102_stream_state stream = cam->stream;
2474 const u32 nbuffers = cam->nbuffers;
2478 if (copy_from_user(&format, arg, sizeof(format)))
2481 pix = &(format.fmt.pix);
2483 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2486 memcpy(&rect, &(s->_rect), sizeof(rect));
2488 { /* calculate the actual scaling factor */
2490 a = rect.width * rect.height;
2491 b = pix->width * pix->height;
2492 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2495 rect.width = scale * pix->width;
2496 rect.height = scale * pix->height;
2498 if (rect.width < 16)
2500 if (rect.height < 16)
2502 if (rect.width > bounds->left + bounds->width - rect.left)
2503 rect.width = bounds->left + bounds->width - rect.left;
2504 if (rect.height > bounds->top + bounds->height - rect.top)
2505 rect.height = bounds->top + bounds->height - rect.top;
2508 rect.height &= ~15L;
2510 { /* adjust the scaling factor */
2512 a = rect.width * rect.height;
2513 b = pix->width * pix->height;
2514 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2517 pix->width = rect.width / scale;
2518 pix->height = rect.height / scale;
2520 switch (cam->bridge) {
2521 case BRIDGE_SN9C101:
2522 case BRIDGE_SN9C102:
2523 case BRIDGE_SN9C103:
2524 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2525 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2526 pix->pixelformat = pfmt->pixelformat;
2528 case BRIDGE_SN9C105:
2529 case BRIDGE_SN9C120:
2530 if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
2531 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2532 pix->pixelformat = pfmt->pixelformat;
2535 pix->priv = pfmt->priv; /* bpp */
2536 pix->colorspace = pfmt->colorspace;
2537 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
2538 pix->pixelformat == V4L2_PIX_FMT_JPEG)
2539 ? 0 : (pix->width * pix->priv) / 8;
2540 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2541 pix->field = V4L2_FIELD_NONE;
2543 if (cmd == VIDIOC_TRY_FMT) {
2544 if (copy_to_user(arg, &format, sizeof(format)))
2549 if (cam->module_param.force_munmap)
2550 for (i = 0; i < cam->nbuffers; i++)
2551 if (cam->frame[i].vma_use_count) {
2552 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2557 if (cam->stream == STREAM_ON)
2558 if ((err = sn9c102_stream_interrupt(cam)))
2561 if (copy_to_user(arg, &format, sizeof(format))) {
2562 cam->stream = stream;
2566 if (cam->module_param.force_munmap || cam->io == IO_READ)
2567 sn9c102_release_buffers(cam);
2569 err += sn9c102_set_pix_format(cam, pix);
2570 err += sn9c102_set_crop(cam, &rect);
2571 if (s->set_pix_format)
2572 err += s->set_pix_format(cam, pix);
2574 err += s->set_crop(cam, &rect);
2575 err += sn9c102_set_scale(cam, scale);
2577 if (err) { /* atomic, no rollback in ioctl() */
2578 cam->state |= DEV_MISCONFIGURED;
2579 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2580 "use the camera, close and open /dev/video%d again.",
2581 cam->v4ldev->minor);
2585 memcpy(pfmt, pix, sizeof(*pix));
2586 memcpy(&(s->_rect), &rect, sizeof(rect));
2588 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2589 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2590 cam->state |= DEV_MISCONFIGURED;
2591 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2592 "use the camera, close and open /dev/video%d again.",
2593 cam->v4ldev->minor);
2597 if (cam->io == IO_READ)
2598 sn9c102_empty_framequeues(cam);
2599 else if (cam->module_param.force_munmap)
2600 sn9c102_requeue_outqueue(cam);
2602 cam->stream = stream;
2609 sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2611 if (copy_to_user(arg, &cam->compression, sizeof(cam->compression)))
2619 sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2621 struct v4l2_jpegcompression jc;
2622 const enum sn9c102_stream_state stream = cam->stream;
2625 if (copy_from_user(&jc, arg, sizeof(jc)))
2628 if (jc.quality != 0 && jc.quality != 1)
2631 if (cam->stream == STREAM_ON)
2632 if ((err = sn9c102_stream_interrupt(cam)))
2635 err += sn9c102_set_compression(cam, &jc);
2636 if (err) { /* atomic, no rollback in ioctl() */
2637 cam->state |= DEV_MISCONFIGURED;
2638 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2639 "problems. To use the camera, close and open "
2640 "/dev/video%d again.", cam->v4ldev->minor);
2644 cam->compression.quality = jc.quality;
2646 cam->stream = stream;
2653 sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
2655 struct v4l2_requestbuffers rb;
2659 if (copy_from_user(&rb, arg, sizeof(rb)))
2662 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2663 rb.memory != V4L2_MEMORY_MMAP)
2666 if (cam->io == IO_READ) {
2667 DBG(3, "Close and open the device again to choose the mmap "
2672 for (i = 0; i < cam->nbuffers; i++)
2673 if (cam->frame[i].vma_use_count) {
2674 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2679 if (cam->stream == STREAM_ON)
2680 if ((err = sn9c102_stream_interrupt(cam)))
2683 sn9c102_empty_framequeues(cam);
2685 sn9c102_release_buffers(cam);
2687 rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
2689 if (copy_to_user(arg, &rb, sizeof(rb))) {
2690 sn9c102_release_buffers(cam);
2695 cam->io = rb.count ? IO_MMAP : IO_NONE;
2702 sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
2704 struct v4l2_buffer b;
2706 if (copy_from_user(&b, arg, sizeof(b)))
2709 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2710 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2713 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2715 if (cam->frame[b.index].vma_use_count)
2716 b.flags |= V4L2_BUF_FLAG_MAPPED;
2718 if (cam->frame[b.index].state == F_DONE)
2719 b.flags |= V4L2_BUF_FLAG_DONE;
2720 else if (cam->frame[b.index].state != F_UNUSED)
2721 b.flags |= V4L2_BUF_FLAG_QUEUED;
2723 if (copy_to_user(arg, &b, sizeof(b)))
2731 sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
2733 struct v4l2_buffer b;
2734 unsigned long lock_flags;
2736 if (copy_from_user(&b, arg, sizeof(b)))
2739 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2740 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2743 if (cam->frame[b.index].state != F_UNUSED)
2746 cam->frame[b.index].state = F_QUEUED;
2748 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2749 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2750 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2752 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2759 sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2762 struct v4l2_buffer b;
2763 struct sn9c102_frame_t *f;
2764 unsigned long lock_flags;
2768 if (copy_from_user(&b, arg, sizeof(b)))
2771 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2774 if (list_empty(&cam->outqueue)) {
2775 if (cam->stream == STREAM_OFF)
2777 if (filp->f_flags & O_NONBLOCK)
2779 if (!cam->module_param.frame_timeout) {
2780 err = wait_event_interruptible
2782 (!list_empty(&cam->outqueue)) ||
2783 (cam->state & DEV_DISCONNECTED) ||
2784 (cam->state & DEV_MISCONFIGURED) );
2788 timeout = wait_event_interruptible_timeout
2790 (!list_empty(&cam->outqueue)) ||
2791 (cam->state & DEV_DISCONNECTED) ||
2792 (cam->state & DEV_MISCONFIGURED),
2793 cam->module_param.frame_timeout *
2794 1000 * msecs_to_jiffies(1) );
2797 else if (timeout == 0 &&
2798 !(cam->state & DEV_DISCONNECTED)) {
2799 DBG(1, "Video frame timeout elapsed");
2803 if (cam->state & DEV_DISCONNECTED)
2805 if (cam->state & DEV_MISCONFIGURED)
2809 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2810 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
2811 list_del(cam->outqueue.next);
2812 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2814 f->state = F_UNUSED;
2816 memcpy(&b, &f->buf, sizeof(b));
2817 if (f->vma_use_count)
2818 b.flags |= V4L2_BUF_FLAG_MAPPED;
2820 if (copy_to_user(arg, &b, sizeof(b)))
2823 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2830 sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
2834 if (copy_from_user(&type, arg, sizeof(type)))
2837 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2840 if (list_empty(&cam->inqueue))
2843 cam->stream = STREAM_ON;
2845 DBG(3, "Stream on");
2852 sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
2856 if (copy_from_user(&type, arg, sizeof(type)))
2859 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2862 if (cam->stream == STREAM_ON)
2863 if ((err = sn9c102_stream_interrupt(cam)))
2866 sn9c102_empty_framequeues(cam);
2868 DBG(3, "Stream off");
2875 sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
2877 struct v4l2_streamparm sp;
2879 if (copy_from_user(&sp, arg, sizeof(sp)))
2882 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2885 sp.parm.capture.extendedmode = 0;
2886 sp.parm.capture.readbuffers = cam->nreadbuffers;
2888 if (copy_to_user(arg, &sp, sizeof(sp)))
2896 sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
2898 struct v4l2_streamparm sp;
2900 if (copy_from_user(&sp, arg, sizeof(sp)))
2903 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2906 sp.parm.capture.extendedmode = 0;
2908 if (sp.parm.capture.readbuffers == 0)
2909 sp.parm.capture.readbuffers = cam->nreadbuffers;
2911 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2912 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2914 if (copy_to_user(arg, &sp, sizeof(sp)))
2917 cam->nreadbuffers = sp.parm.capture.readbuffers;
2924 sn9c102_vidioc_enumaudio(struct sn9c102_device* cam, void __user * arg)
2926 struct v4l2_audio audio;
2928 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
2931 if (copy_from_user(&audio, arg, sizeof(audio)))
2934 if (audio.index != 0)
2937 strcpy(audio.name, "Microphone");
2938 audio.capability = 0;
2941 if (copy_to_user(arg, &audio, sizeof(audio)))
2949 sn9c102_vidioc_g_audio(struct sn9c102_device* cam, void __user * arg)
2951 struct v4l2_audio audio;
2953 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
2956 if (copy_from_user(&audio, arg, sizeof(audio)))
2959 memset(&audio, 0, sizeof(audio));
2960 strcpy(audio.name, "Microphone");
2962 if (copy_to_user(arg, &audio, sizeof(audio)))
2970 sn9c102_vidioc_s_audio(struct sn9c102_device* cam, void __user * arg)
2972 struct v4l2_audio audio;
2974 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
2977 if (copy_from_user(&audio, arg, sizeof(audio)))
2980 if (audio.index != 0)
2987 static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
2988 unsigned int cmd, void __user * arg)
2990 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2994 case VIDIOC_QUERYCAP:
2995 return sn9c102_vidioc_querycap(cam, arg);
2997 case VIDIOC_ENUMINPUT:
2998 return sn9c102_vidioc_enuminput(cam, arg);
3000 case VIDIOC_G_INPUT:
3001 return sn9c102_vidioc_g_input(cam, arg);
3003 case VIDIOC_S_INPUT:
3004 return sn9c102_vidioc_s_input(cam, arg);
3006 case VIDIOC_QUERYCTRL:
3007 return sn9c102_vidioc_query_ctrl(cam, arg);
3010 return sn9c102_vidioc_g_ctrl(cam, arg);
3013 return sn9c102_vidioc_s_ctrl(cam, arg);
3015 case VIDIOC_CROPCAP:
3016 return sn9c102_vidioc_cropcap(cam, arg);
3019 return sn9c102_vidioc_g_crop(cam, arg);
3022 return sn9c102_vidioc_s_crop(cam, arg);
3024 case VIDIOC_ENUM_FRAMESIZES:
3025 return sn9c102_vidioc_enum_framesizes(cam, arg);
3027 case VIDIOC_ENUM_FMT:
3028 return sn9c102_vidioc_enum_fmt(cam, arg);
3031 return sn9c102_vidioc_g_fmt(cam, arg);
3033 case VIDIOC_TRY_FMT:
3035 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
3037 case VIDIOC_G_JPEGCOMP:
3038 return sn9c102_vidioc_g_jpegcomp(cam, arg);
3040 case VIDIOC_S_JPEGCOMP:
3041 return sn9c102_vidioc_s_jpegcomp(cam, arg);
3043 case VIDIOC_REQBUFS:
3044 return sn9c102_vidioc_reqbufs(cam, arg);
3046 case VIDIOC_QUERYBUF:
3047 return sn9c102_vidioc_querybuf(cam, arg);
3050 return sn9c102_vidioc_qbuf(cam, arg);
3053 return sn9c102_vidioc_dqbuf(cam, filp, arg);
3055 case VIDIOC_STREAMON:
3056 return sn9c102_vidioc_streamon(cam, arg);
3058 case VIDIOC_STREAMOFF:
3059 return sn9c102_vidioc_streamoff(cam, arg);
3062 return sn9c102_vidioc_g_parm(cam, arg);
3065 return sn9c102_vidioc_s_parm(cam, arg);
3067 case VIDIOC_ENUMAUDIO:
3068 return sn9c102_vidioc_enumaudio(cam, arg);
3070 case VIDIOC_G_AUDIO:
3071 return sn9c102_vidioc_g_audio(cam, arg);
3073 case VIDIOC_S_AUDIO:
3074 return sn9c102_vidioc_s_audio(cam, arg);
3078 case VIDIOC_QUERYSTD:
3079 case VIDIOC_ENUMSTD:
3080 case VIDIOC_QUERYMENU:
3081 case VIDIOC_ENUM_FRAMEINTERVALS:
3091 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
3092 unsigned int cmd, unsigned long arg)
3094 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
3097 if (mutex_lock_interruptible(&cam->fileop_mutex))
3098 return -ERESTARTSYS;
3100 if (cam->state & DEV_DISCONNECTED) {
3101 DBG(1, "Device not present");
3102 mutex_unlock(&cam->fileop_mutex);
3106 if (cam->state & DEV_MISCONFIGURED) {
3107 DBG(1, "The camera is misconfigured. Close and open it "
3109 mutex_unlock(&cam->fileop_mutex);
3113 V4LDBG(3, "sn9c102", cmd);
3115 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
3117 mutex_unlock(&cam->fileop_mutex);
3122 /*****************************************************************************/
3124 static const struct file_operations sn9c102_fops = {
3125 .owner = THIS_MODULE,
3126 .open = sn9c102_open,
3127 .release = sn9c102_release,
3128 .ioctl = sn9c102_ioctl,
3129 .compat_ioctl = v4l_compat_ioctl32,
3130 .read = sn9c102_read,
3131 .poll = sn9c102_poll,
3132 .mmap = sn9c102_mmap,
3133 .llseek = no_llseek,
3136 /*****************************************************************************/
3138 /* It exists a single interface only. We do not need to validate anything. */
3140 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3142 struct usb_device *udev = interface_to_usbdev(intf);
3143 struct sn9c102_device* cam;
3144 static unsigned int dev_nr = 0;
3148 if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
3153 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
3154 DBG(1, "kzalloc() failed");
3159 if (!(cam->v4ldev = video_device_alloc())) {
3160 DBG(1, "video_device_alloc() failed");
3165 mutex_init(&cam->dev_mutex);
3167 r = sn9c102_read_reg(cam, 0x00);
3168 if (r < 0 || (r != 0x10 && r != 0x11 && r != 0x12)) {
3169 DBG(1, "Sorry, this is not a SN9C1xx based camera "
3170 "(vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3175 cam->bridge = id->driver_info;
3176 switch (cam->bridge) {
3177 case BRIDGE_SN9C101:
3178 case BRIDGE_SN9C102:
3179 DBG(2, "SN9C10[12] PC Camera Controller detected "
3180 "(vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3182 case BRIDGE_SN9C103:
3183 DBG(2, "SN9C103 PC Camera Controller detected "
3184 "(vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3186 case BRIDGE_SN9C105:
3187 DBG(2, "SN9C105 PC Camera Controller detected "
3188 "(vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3190 case BRIDGE_SN9C120:
3191 DBG(2, "SN9C120 PC Camera Controller detected "
3192 "(vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3196 for (i = 0; sn9c102_sensor_table[i]; i++) {
3197 err = sn9c102_sensor_table[i](cam);
3203 DBG(2, "%s image sensor detected", cam->sensor.name);
3204 DBG(3, "Support for %s maintained by %s",
3205 cam->sensor.name, cam->sensor.maintainer);
3207 DBG(1, "No supported image sensor detected");
3212 if (!(cam->bridge & cam->sensor.supported_bridge)) {
3213 DBG(1, "Bridge not supported");
3218 if (sn9c102_init(cam)) {
3219 DBG(1, "Initialization failed. I will retry on open().");
3220 cam->state |= DEV_MISCONFIGURED;
3223 strcpy(cam->v4ldev->name, "SN9C1xx PC Camera");
3224 cam->v4ldev->owner = THIS_MODULE;
3225 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
3226 cam->v4ldev->hardware = 0;
3227 cam->v4ldev->fops = &sn9c102_fops;
3228 cam->v4ldev->minor = video_nr[dev_nr];
3229 cam->v4ldev->release = video_device_release;
3230 video_set_drvdata(cam->v4ldev, cam);
3232 mutex_lock(&cam->dev_mutex);
3234 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
3237 DBG(1, "V4L2 device registration failed");
3238 if (err == -ENFILE && video_nr[dev_nr] == -1)
3239 DBG(1, "Free /dev/videoX node not found");
3240 video_nr[dev_nr] = -1;
3241 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
3242 mutex_unlock(&cam->dev_mutex);
3246 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
3248 cam->module_param.force_munmap = force_munmap[dev_nr];
3249 cam->module_param.frame_timeout = frame_timeout[dev_nr];
3251 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
3253 #ifdef CONFIG_VIDEO_ADV_DEBUG
3254 err = sn9c102_create_sysfs(cam);
3256 DBG(2, "Optional device control through 'sysfs' "
3259 DBG(2, "Failed to create optional 'sysfs' interface for "
3260 "device controlling. Error #%d", err);
3262 DBG(2, "Optional device control through 'sysfs' interface disabled");
3265 usb_set_intfdata(intf, cam);
3267 mutex_unlock(&cam->dev_mutex);
3273 kfree(cam->control_buffer);
3275 video_device_release(cam->v4ldev);
3282 static void sn9c102_usb_disconnect(struct usb_interface* intf)
3284 struct sn9c102_device* cam = usb_get_intfdata(intf);
3289 down_write(&sn9c102_disconnect);
3291 mutex_lock(&cam->dev_mutex);
3293 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
3295 wake_up_interruptible_all(&cam->open);
3298 DBG(2, "Device /dev/video%d is open! Deregistration and "
3299 "memory deallocation are deferred on close.",
3300 cam->v4ldev->minor);
3301 cam->state |= DEV_MISCONFIGURED;
3302 sn9c102_stop_transfer(cam);
3303 cam->state |= DEV_DISCONNECTED;
3304 wake_up_interruptible(&cam->wait_frame);
3305 wake_up(&cam->wait_stream);
3306 usb_get_dev(cam->usbdev);
3308 cam->state |= DEV_DISCONNECTED;
3309 sn9c102_release_resources(cam);
3312 mutex_unlock(&cam->dev_mutex);
3317 up_write(&sn9c102_disconnect);
3321 static struct usb_driver sn9c102_usb_driver = {
3323 .id_table = sn9c102_id_table,
3324 .probe = sn9c102_usb_probe,
3325 .disconnect = sn9c102_usb_disconnect,
3328 /*****************************************************************************/
3330 static int __init sn9c102_module_init(void)
3334 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
3335 KDBG(3, SN9C102_MODULE_AUTHOR);
3337 if ((err = usb_register(&sn9c102_usb_driver)))
3338 KDBG(1, "usb_register() failed");
3344 static void __exit sn9c102_module_exit(void)
3346 usb_deregister(&sn9c102_usb_driver);
3350 module_init(sn9c102_module_init);
3351 module_exit(sn9c102_module_exit);