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.44"
52 #define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 44)
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 /*****************************************************************************/
214 Write a sequence of count value/register pairs. Returns -1 after the first
215 failed write, or 0 for no errors.
217 int sn9c102_write_regs(struct sn9c102_device* cam, const u8 valreg[][2],
220 struct usb_device* udev = cam->usbdev;
221 u8* buff = cam->control_buffer;
224 for (i = 0; i < count; i++) {
225 u8 index = valreg[i][1];
228 index is a u8, so it must be <256 and can't be out of range.
229 If we put in a check anyway, gcc annoys us with a warning
230 hat our check is useless. People get all uppity when they
231 see warnings in the kernel compile.
234 *buff = valreg[i][0];
236 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08,
237 0x41, index, 0, buff, 1,
238 SN9C102_CTRL_TIMEOUT);
241 DBG(3, "Failed to write a register (value 0x%02X, "
242 "index 0x%02X, error %d)", *buff, index, res);
246 cam->reg[index] = *buff;
253 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
255 struct usb_device* udev = cam->usbdev;
256 u8* buff = cam->control_buffer;
259 if (index >= ARRAY_SIZE(cam->reg))
264 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
265 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
267 DBG(3, "Failed to write a register (value 0x%02X, index "
268 "0x%02X, error %d)", value, index, res);
272 cam->reg[index] = value;
278 /* NOTE: with the SN9C10[123] reading some registers always returns 0 */
279 int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
281 struct usb_device* udev = cam->usbdev;
282 u8* buff = cam->control_buffer;
285 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
286 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
288 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
291 return (res >= 0) ? (int)(*buff) : -1;
295 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
297 if (index >= ARRAY_SIZE(cam->reg))
300 return cam->reg[index];
305 sn9c102_i2c_wait(struct sn9c102_device* cam,
306 const struct sn9c102_sensor* sensor)
310 for (i = 1; i <= 5; i++) {
311 r = sn9c102_read_reg(cam, 0x08);
316 if (sensor->frequency & SN9C102_I2C_400KHZ)
326 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
327 const struct sn9c102_sensor* sensor)
331 r = sn9c102_read_reg(cam, 0x08);
335 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
343 return err ? -EIO : 0;
348 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
349 const struct sn9c102_sensor* sensor)
352 r = sn9c102_read_reg(cam, 0x08);
353 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
358 sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
359 const struct sn9c102_sensor* sensor, u8 data0,
360 u8 data1, u8 n, u8 buffer[])
362 struct usb_device* udev = cam->usbdev;
363 u8* data = cam->control_buffer;
364 int i = 0, err = 0, res;
367 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
368 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
369 data[1] = data0; /* I2C slave id */
370 data[2] = data1; /* address */
372 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
373 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
377 err += sn9c102_i2c_wait(cam, sensor);
379 /* Read cycle - n bytes */
380 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
381 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
385 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
386 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
390 err += sn9c102_i2c_wait(cam, sensor);
392 /* The first read byte will be placed in data[4] */
393 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
394 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
398 err += sn9c102_i2c_detect_read_error(cam, sensor);
400 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
404 DBG(3, "I2C read failed for %s image sensor", sensor->name);
409 for (i = 0; i < n && i < 5; i++)
410 buffer[n-i-1] = data[4-i];
417 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
418 const struct sn9c102_sensor* sensor, u8 n, u8 data0,
419 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
421 struct usb_device* udev = cam->usbdev;
422 u8* data = cam->control_buffer;
425 /* Write cycle. It usually is address + value */
426 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
427 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
436 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
437 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
441 err += sn9c102_i2c_wait(cam, sensor);
442 err += sn9c102_i2c_detect_write_error(cam, sensor);
445 DBG(3, "I2C write failed for %s image sensor", sensor->name);
447 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
448 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
449 n, data0, data1, data2, data3, data4, data5);
456 sn9c102_i2c_try_read(struct sn9c102_device* cam,
457 const struct sn9c102_sensor* sensor, u8 address)
459 return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
465 sn9c102_i2c_try_write(struct sn9c102_device* cam,
466 const struct sn9c102_sensor* sensor, u8 address, u8 value)
468 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
469 sensor->i2c_slave_id, address,
474 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
476 return sn9c102_i2c_try_read(cam, &cam->sensor, address);
480 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
482 return sn9c102_i2c_try_write(cam, &cam->sensor, address, value);
485 /*****************************************************************************/
487 static size_t sn9c102_sof_length(struct sn9c102_device* cam)
489 switch (cam->bridge) {
505 sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
507 static const char marker[6] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
509 size_t soflen = 0, i, j;
511 soflen = sn9c102_sof_length(cam);
513 for (i = 0; i < len; i++) {
516 /* Read the variable part of the header */
517 if (unlikely(cam->sof.bytesread >= sizeof(marker))) {
518 cam->sof.header[cam->sof.bytesread] = *(m+i);
519 if (++cam->sof.bytesread == soflen) {
520 cam->sof.bytesread = 0;
526 /* Search for the SOF marker (fixed part) in the header */
527 for (j = 0, b=cam->sof.bytesread; j+b < sizeof(marker); j++) {
528 if (unlikely(i+j) == len)
530 if (*(m+i+j) == marker[cam->sof.bytesread]) {
531 cam->sof.header[cam->sof.bytesread] = *(m+i+j);
532 if (++cam->sof.bytesread == sizeof(marker)) {
533 PDBGG("Bytes to analyze: %zd. SOF "
534 "starts at byte #%zd", len, i);
539 cam->sof.bytesread = 0;
550 sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
552 static const u8 eof_header[4][4] = {
553 {0x00, 0x00, 0x00, 0x00},
554 {0x40, 0x00, 0x00, 0x00},
555 {0x80, 0x00, 0x00, 0x00},
556 {0xc0, 0x00, 0x00, 0x00},
560 /* The EOF header does not exist in compressed data */
561 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
562 cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
566 The EOF header might cross the packet boundary, but this is not a
567 problem, since the end of a frame is determined by checking its size
570 for (i = 0; (len >= 4) && (i <= len - 4); i++)
571 for (j = 0; j < ARRAY_SIZE(eof_header); j++)
572 if (!memcmp(mem + i, eof_header[j], 4))
580 sn9c102_write_jpegheader(struct sn9c102_device* cam, struct sn9c102_frame_t* f)
582 static const u8 jpeg_header[589] = {
583 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00, 0x06, 0x04, 0x05,
584 0x06, 0x05, 0x04, 0x06, 0x06, 0x05, 0x06, 0x07, 0x07, 0x06,
585 0x08, 0x0a, 0x10, 0x0a, 0x0a, 0x09, 0x09, 0x0a, 0x14, 0x0e,
586 0x0f, 0x0c, 0x10, 0x17, 0x14, 0x18, 0x18, 0x17, 0x14, 0x16,
587 0x16, 0x1a, 0x1d, 0x25, 0x1f, 0x1a, 0x1b, 0x23, 0x1c, 0x16,
588 0x16, 0x20, 0x2c, 0x20, 0x23, 0x26, 0x27, 0x29, 0x2a, 0x29,
589 0x19, 0x1f, 0x2d, 0x30, 0x2d, 0x28, 0x30, 0x25, 0x28, 0x29,
590 0x28, 0x01, 0x07, 0x07, 0x07, 0x0a, 0x08, 0x0a, 0x13, 0x0a,
591 0x0a, 0x13, 0x28, 0x1a, 0x16, 0x1a, 0x28, 0x28, 0x28, 0x28,
592 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
593 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
594 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
595 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
596 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0xff, 0xc4, 0x01, 0xa2,
597 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
598 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
599 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01,
600 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
601 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
602 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10, 0x00,
603 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,
604 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04,
605 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
606 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,
607 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62,
608 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,
609 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 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, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
614 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
615 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
616 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
617 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3,
618 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3,
619 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3,
620 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02,
621 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04,
622 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
623 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
624 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1,
625 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
626 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19,
627 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
628 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
629 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
630 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
631 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
632 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
633 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
634 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
635 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
636 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3,
637 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
638 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc0, 0x00, 0x11,
639 0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x01, 0x21, 0x00, 0x02,
640 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03,
641 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00
645 memcpy(pos, jpeg_header, sizeof(jpeg_header));
647 *(pos + 7 + 64) = 0x01;
648 if (cam->compression.quality == 0) {
649 memcpy(pos + 7, SN9C102_Y_QTABLE0, 64);
650 memcpy(pos + 8 + 64, SN9C102_UV_QTABLE0, 64);
651 } else if (cam->compression.quality == 1) {
652 memcpy(pos + 7, SN9C102_Y_QTABLE1, 64);
653 memcpy(pos + 8 + 64, SN9C102_UV_QTABLE1, 64);
655 *(pos + 564) = cam->sensor.pix_format.width & 0xFF;
656 *(pos + 563) = (cam->sensor.pix_format.width >> 8) & 0xFF;
657 *(pos + 562) = cam->sensor.pix_format.height & 0xFF;
658 *(pos + 561) = (cam->sensor.pix_format.height >> 8) & 0xFF;
661 f->buf.bytesused += sizeof(jpeg_header);
665 static void sn9c102_urb_complete(struct urb *urb)
667 struct sn9c102_device* cam = urb->context;
668 struct sn9c102_frame_t** f;
669 size_t imagesize, soflen;
673 if (urb->status == -ENOENT)
676 f = &cam->frame_current;
678 if (cam->stream == STREAM_INTERRUPT) {
679 cam->stream = STREAM_OFF;
681 (*f)->state = F_QUEUED;
682 cam->sof.bytesread = 0;
683 DBG(3, "Stream interrupted by application");
684 wake_up(&cam->wait_stream);
687 if (cam->state & DEV_DISCONNECTED)
690 if (cam->state & DEV_MISCONFIGURED) {
691 wake_up_interruptible(&cam->wait_frame);
695 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
699 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
702 imagesize = (cam->sensor.pix_format.width *
703 cam->sensor.pix_format.height *
704 cam->sensor.pix_format.priv) / 8;
705 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
706 imagesize += 589; /* length of jpeg header */
707 soflen = sn9c102_sof_length(cam);
709 for (i = 0; i < urb->number_of_packets; i++) {
710 unsigned int img, len, status;
711 void *pos, *sof, *eof;
713 len = urb->iso_frame_desc[i].actual_length;
714 status = urb->iso_frame_desc[i].status;
715 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
718 DBG(3, "Error in isochronous frame");
719 (*f)->state = F_ERROR;
720 cam->sof.bytesread = 0;
724 PDBGG("Isochrnous frame: length %u, #%u i", len, i);
727 sof = sn9c102_find_sof_header(cam, pos, len);
729 eof = sn9c102_find_eof_header(cam, pos, len);
730 if ((*f)->state == F_GRABBING) {
735 img = (eof > pos) ? eof - pos - 1 : 0;
737 if ((*f)->buf.bytesused + img > imagesize) {
739 b = (*f)->buf.bytesused + img -
741 img = imagesize - (*f)->buf.bytesused;
742 PDBGG("Expected EOF not found: video "
745 DBG(3, "Exceeded limit: +%u "
746 "bytes", (unsigned)(b));
749 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
752 if ((*f)->buf.bytesused == 0)
753 do_gettimeofday(&(*f)->buf.timestamp);
755 (*f)->buf.bytesused += img;
757 if ((*f)->buf.bytesused == imagesize ||
758 ((cam->sensor.pix_format.pixelformat ==
759 V4L2_PIX_FMT_SN9C10X ||
760 cam->sensor.pix_format.pixelformat ==
761 V4L2_PIX_FMT_JPEG) && eof)) {
764 b = (*f)->buf.bytesused;
765 (*f)->state = F_DONE;
766 (*f)->buf.sequence= ++cam->frame_count;
768 spin_lock(&cam->queue_lock);
769 list_move_tail(&(*f)->frame,
771 if (!list_empty(&cam->inqueue))
774 struct sn9c102_frame_t,
778 spin_unlock(&cam->queue_lock);
780 memcpy(cam->sysfs.frame_header,
781 cam->sof.header, soflen);
783 DBG(3, "Video frame captured: %lu "
784 "bytes", (unsigned long)(b));
790 (*f)->state = F_ERROR;
791 DBG(3, "Not expected EOF after %lu "
792 "bytes of image data",
794 ((*f)->buf.bytesused));
801 DBG(3, "EOF without SOF");
805 PDBGG("Ignoring pointless isochronous frame");
809 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
811 (*f)->state = F_GRABBING;
812 (*f)->buf.bytesused = 0;
815 if (cam->sensor.pix_format.pixelformat ==
817 sn9c102_write_jpegheader(cam, (*f));
818 DBG(3, "SOF detected: new video frame");
822 } else if ((*f)->state == F_GRABBING) {
823 eof = sn9c102_find_eof_header(cam, pos, len);
824 if (eof && eof < sof)
825 goto end_of_frame; /* (1) */
827 if (cam->sensor.pix_format.pixelformat ==
828 V4L2_PIX_FMT_SN9C10X ||
829 cam->sensor.pix_format.pixelformat ==
831 if (sof - pos >= soflen) {
833 } else { /* remove header */
835 (*f)->buf.bytesused -=
836 (soflen - (sof - pos));
840 DBG(3, "SOF before expected EOF after "
841 "%lu bytes of image data",
843 ((*f)->buf.bytesused));
851 urb->dev = cam->usbdev;
852 err = usb_submit_urb(urb, GFP_ATOMIC);
853 if (err < 0 && err != -EPERM) {
854 cam->state |= DEV_MISCONFIGURED;
855 DBG(1, "usb_submit_urb() failed");
858 wake_up_interruptible(&cam->wait_frame);
862 static int sn9c102_start_transfer(struct sn9c102_device* cam)
864 struct usb_device *udev = cam->usbdev;
866 struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
867 usb_ifnum_to_if(udev, 0),
868 SN9C102_ALTERNATE_SETTING);
869 const unsigned int psz = le16_to_cpu(altsetting->
870 endpoint[0].desc.wMaxPacketSize);
874 for (i = 0; i < SN9C102_URBS; i++) {
875 cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
877 if (!cam->transfer_buffer[i]) {
879 DBG(1, "Not enough memory");
884 for (i = 0; i < SN9C102_URBS; i++) {
885 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
889 DBG(1, "usb_alloc_urb() failed");
894 urb->pipe = usb_rcvisocpipe(udev, 1);
895 urb->transfer_flags = URB_ISO_ASAP;
896 urb->number_of_packets = SN9C102_ISO_PACKETS;
897 urb->complete = sn9c102_urb_complete;
898 urb->transfer_buffer = cam->transfer_buffer[i];
899 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
901 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
902 urb->iso_frame_desc[j].offset = psz * j;
903 urb->iso_frame_desc[j].length = psz;
908 if (!(cam->reg[0x01] & 0x04)) {
909 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
912 DBG(1, "I/O hardware error");
917 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
919 DBG(1, "usb_set_interface() failed");
923 cam->frame_current = NULL;
924 cam->sof.bytesread = 0;
926 for (i = 0; i < SN9C102_URBS; i++) {
927 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
929 for (j = i-1; j >= 0; j--)
930 usb_kill_urb(cam->urb[j]);
931 DBG(1, "usb_submit_urb() failed, error %d", err);
939 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
940 usb_free_urb(cam->urb[i]);
943 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
944 kfree(cam->transfer_buffer[i]);
950 static int sn9c102_stop_transfer(struct sn9c102_device* cam)
952 struct usb_device *udev = cam->usbdev;
956 if (cam->state & DEV_DISCONNECTED)
959 for (i = SN9C102_URBS-1; i >= 0; i--) {
960 usb_kill_urb(cam->urb[i]);
961 usb_free_urb(cam->urb[i]);
962 kfree(cam->transfer_buffer[i]);
965 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
967 DBG(3, "usb_set_interface() failed");
973 static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
977 cam->stream = STREAM_INTERRUPT;
978 timeout = wait_event_timeout(cam->wait_stream,
979 (cam->stream == STREAM_OFF) ||
980 (cam->state & DEV_DISCONNECTED),
981 SN9C102_URB_TIMEOUT);
982 if (cam->state & DEV_DISCONNECTED)
984 else if (cam->stream != STREAM_OFF) {
985 cam->state |= DEV_MISCONFIGURED;
986 DBG(1, "URB timeout reached. The camera is misconfigured. "
987 "To use it, close and open /dev/video%d again.",
995 /*****************************************************************************/
997 #ifdef CONFIG_VIDEO_ADV_DEBUG
998 static u16 sn9c102_strtou16(const char* buff, size_t len, ssize_t* count)
1005 strncpy(str, buff, len);
1008 strncpy(str, buff, 6);
1012 val = simple_strtoul(str, &endp, 0);
1016 *count = (ssize_t)(endp - str);
1017 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
1024 NOTE 1: being inside one of the following methods implies that the v4l
1025 device exists for sure (see kobjects and reference counters)
1026 NOTE 2: buffers are PAGE_SIZE long
1029 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
1031 struct sn9c102_device* cam;
1034 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1035 return -ERESTARTSYS;
1037 cam = video_get_drvdata(container_of(cd, struct video_device,
1040 mutex_unlock(&sn9c102_sysfs_lock);
1044 count = sprintf(buf, "%u\n", cam->sysfs.reg);
1046 mutex_unlock(&sn9c102_sysfs_lock);
1053 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
1055 struct sn9c102_device* cam;
1059 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1060 return -ERESTARTSYS;
1062 cam = video_get_drvdata(container_of(cd, struct video_device,
1065 mutex_unlock(&sn9c102_sysfs_lock);
1069 index = sn9c102_strtou16(buf, len, &count);
1070 if (index >= ARRAY_SIZE(cam->reg) || !count) {
1071 mutex_unlock(&sn9c102_sysfs_lock);
1075 cam->sysfs.reg = index;
1077 DBG(2, "Moved SN9C1XX register index to 0x%02X", cam->sysfs.reg);
1078 DBG(3, "Written bytes: %zd", count);
1080 mutex_unlock(&sn9c102_sysfs_lock);
1086 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
1088 struct sn9c102_device* cam;
1092 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1093 return -ERESTARTSYS;
1095 cam = video_get_drvdata(container_of(cd, struct video_device,
1098 mutex_unlock(&sn9c102_sysfs_lock);
1102 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
1103 mutex_unlock(&sn9c102_sysfs_lock);
1107 count = sprintf(buf, "%d\n", val);
1109 DBG(3, "Read bytes: %zd, value: %d", count, val);
1111 mutex_unlock(&sn9c102_sysfs_lock);
1118 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
1120 struct sn9c102_device* cam;
1125 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1126 return -ERESTARTSYS;
1128 cam = video_get_drvdata(container_of(cd, struct video_device,
1131 mutex_unlock(&sn9c102_sysfs_lock);
1135 value = sn9c102_strtou16(buf, len, &count);
1137 mutex_unlock(&sn9c102_sysfs_lock);
1141 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
1143 mutex_unlock(&sn9c102_sysfs_lock);
1147 DBG(2, "Written SN9C1XX reg. 0x%02X, val. 0x%02X",
1148 cam->sysfs.reg, value);
1149 DBG(3, "Written bytes: %zd", count);
1151 mutex_unlock(&sn9c102_sysfs_lock);
1157 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
1159 struct sn9c102_device* cam;
1162 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1163 return -ERESTARTSYS;
1165 cam = video_get_drvdata(container_of(cd, struct video_device,
1168 mutex_unlock(&sn9c102_sysfs_lock);
1172 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
1174 DBG(3, "Read bytes: %zd", count);
1176 mutex_unlock(&sn9c102_sysfs_lock);
1183 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1185 struct sn9c102_device* cam;
1189 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1190 return -ERESTARTSYS;
1192 cam = video_get_drvdata(container_of(cd, struct video_device,
1195 mutex_unlock(&sn9c102_sysfs_lock);
1199 index = sn9c102_strtou16(buf, len, &count);
1201 mutex_unlock(&sn9c102_sysfs_lock);
1205 cam->sysfs.i2c_reg = index;
1207 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1208 DBG(3, "Written bytes: %zd", count);
1210 mutex_unlock(&sn9c102_sysfs_lock);
1216 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1218 struct sn9c102_device* cam;
1222 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1223 return -ERESTARTSYS;
1225 cam = video_get_drvdata(container_of(cd, struct video_device,
1228 mutex_unlock(&sn9c102_sysfs_lock);
1232 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) {
1233 mutex_unlock(&sn9c102_sysfs_lock);
1237 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1238 mutex_unlock(&sn9c102_sysfs_lock);
1242 count = sprintf(buf, "%d\n", val);
1244 DBG(3, "Read bytes: %zd, value: %d", count, val);
1246 mutex_unlock(&sn9c102_sysfs_lock);
1253 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1255 struct sn9c102_device* cam;
1260 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1261 return -ERESTARTSYS;
1263 cam = video_get_drvdata(container_of(cd, struct video_device,
1266 mutex_unlock(&sn9c102_sysfs_lock);
1270 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) {
1271 mutex_unlock(&sn9c102_sysfs_lock);
1275 value = sn9c102_strtou16(buf, len, &count);
1277 mutex_unlock(&sn9c102_sysfs_lock);
1281 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1283 mutex_unlock(&sn9c102_sysfs_lock);
1287 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1288 cam->sysfs.i2c_reg, value);
1289 DBG(3, "Written bytes: %zd", count);
1291 mutex_unlock(&sn9c102_sysfs_lock);
1298 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1300 struct sn9c102_device* cam;
1301 enum sn9c102_bridge bridge;
1306 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1307 return -ERESTARTSYS;
1309 cam = video_get_drvdata(container_of(cd, struct video_device,
1312 mutex_unlock(&sn9c102_sysfs_lock);
1316 bridge = cam->bridge;
1318 mutex_unlock(&sn9c102_sysfs_lock);
1320 value = sn9c102_strtou16(buf, len, &count);
1325 case BRIDGE_SN9C101:
1326 case BRIDGE_SN9C102:
1329 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1330 res = sn9c102_store_val(cd, buf, len);
1332 case BRIDGE_SN9C103:
1333 case BRIDGE_SN9C105:
1334 case BRIDGE_SN9C120:
1337 if ((res = sn9c102_store_reg(cd, "0x07", 4)) >= 0)
1338 res = sn9c102_store_val(cd, buf, len);
1347 sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1353 value = sn9c102_strtou16(buf, len, &count);
1354 if (!count || value > 0x7f)
1357 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1358 res = sn9c102_store_val(cd, buf, len);
1365 sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1371 value = sn9c102_strtou16(buf, len, &count);
1372 if (!count || value > 0x7f)
1375 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1376 res = sn9c102_store_val(cd, buf, len);
1382 static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1384 struct sn9c102_device* cam;
1387 cam = video_get_drvdata(container_of(cd, struct video_device,
1392 count = sizeof(cam->sysfs.frame_header);
1393 memcpy(buf, cam->sysfs.frame_header, count);
1395 DBG(3, "Frame header, read bytes: %zd", count);
1401 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1402 sn9c102_show_reg, sn9c102_store_reg);
1403 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1404 sn9c102_show_val, sn9c102_store_val);
1405 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1406 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1407 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1408 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1409 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1410 static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1411 static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1412 static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
1413 sn9c102_show_frame_header, NULL);
1416 static int sn9c102_create_sysfs(struct sn9c102_device* cam)
1418 struct class_device *classdev = &(cam->v4ldev->class_dev);
1421 if ((err = class_device_create_file(classdev, &class_device_attr_reg)))
1423 if ((err = class_device_create_file(classdev, &class_device_attr_val)))
1425 if ((err = class_device_create_file(classdev,
1426 &class_device_attr_frame_header)))
1429 if (cam->sensor.sysfs_ops) {
1430 if ((err = class_device_create_file(classdev,
1431 &class_device_attr_i2c_reg)))
1432 goto err_frame_header;
1433 if ((err = class_device_create_file(classdev,
1434 &class_device_attr_i2c_val)))
1438 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
1439 if ((err = class_device_create_file(classdev,
1440 &class_device_attr_green)))
1443 if ((err = class_device_create_file(classdev,
1444 &class_device_attr_blue)))
1446 if ((err = class_device_create_file(classdev,
1447 &class_device_attr_red)))
1454 class_device_remove_file(classdev, &class_device_attr_blue);
1456 if (cam->sensor.sysfs_ops)
1457 class_device_remove_file(classdev, &class_device_attr_i2c_val);
1459 if (cam->sensor.sysfs_ops)
1460 class_device_remove_file(classdev, &class_device_attr_i2c_reg);
1462 class_device_remove_file(classdev, &class_device_attr_frame_header);
1464 class_device_remove_file(classdev, &class_device_attr_val);
1466 class_device_remove_file(classdev, &class_device_attr_reg);
1470 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1472 /*****************************************************************************/
1475 sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
1479 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
1480 pix->pixelformat == V4L2_PIX_FMT_JPEG) {
1481 switch (cam->bridge) {
1482 case BRIDGE_SN9C101:
1483 case BRIDGE_SN9C102:
1484 case BRIDGE_SN9C103:
1485 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
1488 case BRIDGE_SN9C105:
1489 case BRIDGE_SN9C120:
1490 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
1495 switch (cam->bridge) {
1496 case BRIDGE_SN9C101:
1497 case BRIDGE_SN9C102:
1498 case BRIDGE_SN9C103:
1499 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
1502 case BRIDGE_SN9C105:
1503 case BRIDGE_SN9C120:
1504 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
1510 return err ? -EIO : 0;
1515 sn9c102_set_compression(struct sn9c102_device* cam,
1516 struct v4l2_jpegcompression* compression)
1520 switch (cam->bridge) {
1521 case BRIDGE_SN9C101:
1522 case BRIDGE_SN9C102:
1523 case BRIDGE_SN9C103:
1524 if (compression->quality == 0)
1525 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01,
1527 else if (compression->quality == 1)
1528 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe,
1531 case BRIDGE_SN9C105:
1532 case BRIDGE_SN9C120:
1533 if (compression->quality == 0) {
1534 for (i = 0; i <= 63; i++) {
1535 err += sn9c102_write_reg(cam,
1536 SN9C102_Y_QTABLE1[i],
1538 err += sn9c102_write_reg(cam,
1539 SN9C102_UV_QTABLE1[i],
1542 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0xbf,
1544 } else if (compression->quality == 1) {
1545 for (i = 0; i <= 63; i++) {
1546 err += sn9c102_write_reg(cam,
1547 SN9C102_Y_QTABLE1[i],
1549 err += sn9c102_write_reg(cam,
1550 SN9C102_UV_QTABLE1[i],
1553 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x40,
1559 return err ? -EIO : 0;
1563 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1569 r = cam->reg[0x18] & 0xcf;
1570 else if (scale == 2) {
1571 r = cam->reg[0x18] & 0xcf;
1573 } else if (scale == 4)
1574 r = cam->reg[0x18] | 0x20;
1576 err += sn9c102_write_reg(cam, r, 0x18);
1580 PDBGG("Scaling factor: %u", scale);
1586 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1588 struct sn9c102_sensor* s = &cam->sensor;
1589 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1590 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1591 h_size = (u8)(rect->width / 16),
1592 v_size = (u8)(rect->height / 16);
1595 err += sn9c102_write_reg(cam, h_start, 0x12);
1596 err += sn9c102_write_reg(cam, v_start, 0x13);
1597 err += sn9c102_write_reg(cam, h_size, 0x15);
1598 err += sn9c102_write_reg(cam, v_size, 0x16);
1602 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1603 "%u %u %u %u", h_start, v_start, h_size, v_size);
1609 static int sn9c102_init(struct sn9c102_device* cam)
1611 struct sn9c102_sensor* s = &cam->sensor;
1612 struct v4l2_control ctrl;
1613 struct v4l2_queryctrl *qctrl;
1614 struct v4l2_rect* rect;
1618 if (!(cam->state & DEV_INITIALIZED)) {
1619 init_waitqueue_head(&cam->open);
1621 rect = &(s->cropcap.defrect);
1622 } else { /* use current values */
1627 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1628 err += sn9c102_set_crop(cam, rect);
1635 DBG(3, "Sensor initialization failed");
1640 if (!(cam->state & DEV_INITIALIZED))
1641 if (cam->bridge == BRIDGE_SN9C101 ||
1642 cam->bridge == BRIDGE_SN9C102 ||
1643 cam->bridge == BRIDGE_SN9C103) {
1644 if (s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
1645 s->pix_format.pixelformat= V4L2_PIX_FMT_SBGGR8;
1646 cam->compression.quality = cam->reg[0x17] & 0x01 ?
1649 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1650 s->pix_format.pixelformat = V4L2_PIX_FMT_JPEG;
1651 cam->compression.quality = cam->reg[0x18] & 0x40 ?
1653 err += sn9c102_set_compression(cam, &cam->compression);
1656 err += sn9c102_set_compression(cam, &cam->compression);
1657 err += sn9c102_set_pix_format(cam, &s->pix_format);
1658 if (s->set_pix_format)
1659 err += s->set_pix_format(cam, &s->pix_format);
1663 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
1664 s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
1665 DBG(3, "Compressed video format is active, quality %d",
1666 cam->compression.quality);
1668 DBG(3, "Uncompressed video format is active");
1671 if ((err = s->set_crop(cam, rect))) {
1672 DBG(3, "set_crop() failed");
1677 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1678 if (s->qctrl[i].id != 0 &&
1679 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1680 ctrl.id = s->qctrl[i].id;
1681 ctrl.value = qctrl[i].default_value;
1682 err = s->set_ctrl(cam, &ctrl);
1684 DBG(3, "Set %s control failed",
1688 DBG(3, "Image sensor supports '%s' control",
1693 if (!(cam->state & DEV_INITIALIZED)) {
1694 mutex_init(&cam->fileop_mutex);
1695 spin_lock_init(&cam->queue_lock);
1696 init_waitqueue_head(&cam->wait_frame);
1697 init_waitqueue_head(&cam->wait_stream);
1698 cam->nreadbuffers = 2;
1699 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1700 memcpy(&(s->_rect), &(s->cropcap.defrect),
1701 sizeof(struct v4l2_rect));
1702 cam->state |= DEV_INITIALIZED;
1705 DBG(2, "Initialization succeeded");
1710 static void sn9c102_release_resources(struct sn9c102_device* cam)
1712 mutex_lock(&sn9c102_sysfs_lock);
1714 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1715 video_set_drvdata(cam->v4ldev, NULL);
1716 video_unregister_device(cam->v4ldev);
1718 mutex_unlock(&sn9c102_sysfs_lock);
1720 kfree(cam->control_buffer);
1723 /*****************************************************************************/
1725 static int sn9c102_open(struct inode* inode, struct file* filp)
1727 struct sn9c102_device* cam;
1731 This is the only safe way to prevent race conditions with
1734 if (!down_read_trylock(&sn9c102_disconnect))
1735 return -ERESTARTSYS;
1737 cam = video_get_drvdata(video_devdata(filp));
1739 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1740 up_read(&sn9c102_disconnect);
1741 return -ERESTARTSYS;
1745 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1746 DBG(3, "Simultaneous opens are not supported");
1747 if ((filp->f_flags & O_NONBLOCK) ||
1748 (filp->f_flags & O_NDELAY)) {
1752 mutex_unlock(&cam->dev_mutex);
1753 err = wait_event_interruptible_exclusive(cam->open,
1754 cam->state & DEV_DISCONNECTED
1757 up_read(&sn9c102_disconnect);
1760 if (cam->state & DEV_DISCONNECTED) {
1761 up_read(&sn9c102_disconnect);
1764 mutex_lock(&cam->dev_mutex);
1768 if (cam->state & DEV_MISCONFIGURED) {
1769 err = sn9c102_init(cam);
1771 DBG(1, "Initialization failed again. "
1772 "I will retry on next open().");
1775 cam->state &= ~DEV_MISCONFIGURED;
1778 if ((err = sn9c102_start_transfer(cam)))
1781 filp->private_data = cam;
1784 cam->stream = STREAM_OFF;
1786 cam->frame_count = 0;
1787 sn9c102_empty_framequeues(cam);
1789 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1792 mutex_unlock(&cam->dev_mutex);
1793 up_read(&sn9c102_disconnect);
1798 static int sn9c102_release(struct inode* inode, struct file* filp)
1800 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1802 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1804 sn9c102_stop_transfer(cam);
1806 sn9c102_release_buffers(cam);
1808 if (cam->state & DEV_DISCONNECTED) {
1809 sn9c102_release_resources(cam);
1810 usb_put_dev(cam->usbdev);
1811 mutex_unlock(&cam->dev_mutex);
1817 wake_up_interruptible_nr(&cam->open, 1);
1819 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1821 mutex_unlock(&cam->dev_mutex);
1828 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1830 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1831 struct sn9c102_frame_t* f, * i;
1832 unsigned long lock_flags;
1836 if (mutex_lock_interruptible(&cam->fileop_mutex))
1837 return -ERESTARTSYS;
1839 if (cam->state & DEV_DISCONNECTED) {
1840 DBG(1, "Device not present");
1841 mutex_unlock(&cam->fileop_mutex);
1845 if (cam->state & DEV_MISCONFIGURED) {
1846 DBG(1, "The camera is misconfigured. Close and open it "
1848 mutex_unlock(&cam->fileop_mutex);
1852 if (cam->io == IO_MMAP) {
1853 DBG(3, "Close and open the device again to choose "
1855 mutex_unlock(&cam->fileop_mutex);
1859 if (cam->io == IO_NONE) {
1860 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1861 DBG(1, "read() failed, not enough memory");
1862 mutex_unlock(&cam->fileop_mutex);
1866 cam->stream = STREAM_ON;
1869 if (list_empty(&cam->inqueue)) {
1870 if (!list_empty(&cam->outqueue))
1871 sn9c102_empty_framequeues(cam);
1872 sn9c102_queue_unusedframes(cam);
1876 mutex_unlock(&cam->fileop_mutex);
1880 if (list_empty(&cam->outqueue)) {
1881 if (filp->f_flags & O_NONBLOCK) {
1882 mutex_unlock(&cam->fileop_mutex);
1885 if (!cam->module_param.frame_timeout) {
1886 err = wait_event_interruptible
1888 (!list_empty(&cam->outqueue)) ||
1889 (cam->state & DEV_DISCONNECTED) ||
1890 (cam->state & DEV_MISCONFIGURED) );
1892 mutex_unlock(&cam->fileop_mutex);
1896 timeout = wait_event_interruptible_timeout
1898 (!list_empty(&cam->outqueue)) ||
1899 (cam->state & DEV_DISCONNECTED) ||
1900 (cam->state & DEV_MISCONFIGURED),
1901 cam->module_param.frame_timeout *
1902 1000 * msecs_to_jiffies(1) );
1904 mutex_unlock(&cam->fileop_mutex);
1906 } else if (timeout == 0 &&
1907 !(cam->state & DEV_DISCONNECTED)) {
1908 DBG(1, "Video frame timeout elapsed");
1909 mutex_unlock(&cam->fileop_mutex);
1913 if (cam->state & DEV_DISCONNECTED) {
1914 mutex_unlock(&cam->fileop_mutex);
1917 if (cam->state & DEV_MISCONFIGURED) {
1918 mutex_unlock(&cam->fileop_mutex);
1923 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1925 if (count > f->buf.bytesused)
1926 count = f->buf.bytesused;
1928 if (copy_to_user(buf, f->bufmem, count)) {
1935 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1936 list_for_each_entry(i, &cam->outqueue, frame)
1937 i->state = F_UNUSED;
1938 INIT_LIST_HEAD(&cam->outqueue);
1939 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1941 sn9c102_queue_unusedframes(cam);
1943 PDBGG("Frame #%lu, bytes read: %zu",
1944 (unsigned long)f->buf.index, count);
1946 mutex_unlock(&cam->fileop_mutex);
1952 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1954 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1955 struct sn9c102_frame_t* f;
1956 unsigned long lock_flags;
1957 unsigned int mask = 0;
1959 if (mutex_lock_interruptible(&cam->fileop_mutex))
1962 if (cam->state & DEV_DISCONNECTED) {
1963 DBG(1, "Device not present");
1967 if (cam->state & DEV_MISCONFIGURED) {
1968 DBG(1, "The camera is misconfigured. Close and open it "
1973 if (cam->io == IO_NONE) {
1974 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1976 DBG(1, "poll() failed, not enough memory");
1980 cam->stream = STREAM_ON;
1983 if (cam->io == IO_READ) {
1984 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1985 list_for_each_entry(f, &cam->outqueue, frame)
1986 f->state = F_UNUSED;
1987 INIT_LIST_HEAD(&cam->outqueue);
1988 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1989 sn9c102_queue_unusedframes(cam);
1992 poll_wait(filp, &cam->wait_frame, wait);
1994 if (!list_empty(&cam->outqueue))
1995 mask |= POLLIN | POLLRDNORM;
1997 mutex_unlock(&cam->fileop_mutex);
2002 mutex_unlock(&cam->fileop_mutex);
2007 static void sn9c102_vm_open(struct vm_area_struct* vma)
2009 struct sn9c102_frame_t* f = vma->vm_private_data;
2014 static void sn9c102_vm_close(struct vm_area_struct* vma)
2016 /* NOTE: buffers are not freed here */
2017 struct sn9c102_frame_t* f = vma->vm_private_data;
2022 static struct vm_operations_struct sn9c102_vm_ops = {
2023 .open = sn9c102_vm_open,
2024 .close = sn9c102_vm_close,
2028 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
2030 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2031 unsigned long size = vma->vm_end - vma->vm_start,
2032 start = vma->vm_start;
2036 if (mutex_lock_interruptible(&cam->fileop_mutex))
2037 return -ERESTARTSYS;
2039 if (cam->state & DEV_DISCONNECTED) {
2040 DBG(1, "Device not present");
2041 mutex_unlock(&cam->fileop_mutex);
2045 if (cam->state & DEV_MISCONFIGURED) {
2046 DBG(1, "The camera is misconfigured. Close and open it "
2048 mutex_unlock(&cam->fileop_mutex);
2052 if (!(vma->vm_flags & (VM_WRITE | VM_READ))) {
2053 mutex_unlock(&cam->fileop_mutex);
2057 if (cam->io != IO_MMAP ||
2058 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
2059 mutex_unlock(&cam->fileop_mutex);
2063 for (i = 0; i < cam->nbuffers; i++) {
2064 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
2067 if (i == cam->nbuffers) {
2068 mutex_unlock(&cam->fileop_mutex);
2072 vma->vm_flags |= VM_IO;
2073 vma->vm_flags |= VM_RESERVED;
2075 pos = cam->frame[i].bufmem;
2076 while (size > 0) { /* size is page-aligned */
2077 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
2078 mutex_unlock(&cam->fileop_mutex);
2086 vma->vm_ops = &sn9c102_vm_ops;
2087 vma->vm_private_data = &cam->frame[i];
2089 sn9c102_vm_open(vma);
2091 mutex_unlock(&cam->fileop_mutex);
2096 /*****************************************************************************/
2099 sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
2101 struct v4l2_capability cap = {
2102 .driver = "sn9c102",
2103 .version = SN9C102_MODULE_VERSION_CODE,
2104 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
2108 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
2109 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
2110 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
2111 sizeof(cap.bus_info));
2113 if (copy_to_user(arg, &cap, sizeof(cap)))
2121 sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
2123 struct v4l2_input i;
2125 if (copy_from_user(&i, arg, sizeof(i)))
2131 memset(&i, 0, sizeof(i));
2132 strcpy(i.name, "Camera");
2133 i.type = V4L2_INPUT_TYPE_CAMERA;
2135 if (copy_to_user(arg, &i, sizeof(i)))
2143 sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg)
2147 if (copy_to_user(arg, &index, sizeof(index)))
2155 sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg)
2159 if (copy_from_user(&index, arg, sizeof(index)))
2170 sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
2172 struct sn9c102_sensor* s = &cam->sensor;
2173 struct v4l2_queryctrl qc;
2176 if (copy_from_user(&qc, arg, sizeof(qc)))
2179 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2180 if (qc.id && qc.id == s->qctrl[i].id) {
2181 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
2182 if (copy_to_user(arg, &qc, sizeof(qc)))
2192 sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
2194 struct sn9c102_sensor* s = &cam->sensor;
2195 struct v4l2_control ctrl;
2199 if (!s->get_ctrl && !s->set_ctrl)
2202 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2206 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2207 if (ctrl.id && ctrl.id == s->qctrl[i].id) {
2208 ctrl.value = s->_qctrl[i].default_value;
2213 err = s->get_ctrl(cam, &ctrl);
2216 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
2219 PDBGG("VIDIOC_G_CTRL: id %lu, value %lu",
2220 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
2227 sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
2229 struct sn9c102_sensor* s = &cam->sensor;
2230 struct v4l2_control ctrl;
2237 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2240 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2241 if (ctrl.id == s->qctrl[i].id) {
2242 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
2244 if (ctrl.value < s->qctrl[i].minimum ||
2245 ctrl.value > s->qctrl[i].maximum)
2247 ctrl.value -= ctrl.value % s->qctrl[i].step;
2251 if ((err = s->set_ctrl(cam, &ctrl)))
2254 s->_qctrl[i].default_value = ctrl.value;
2256 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
2257 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
2264 sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
2266 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
2268 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2269 cc->pixelaspect.numerator = 1;
2270 cc->pixelaspect.denominator = 1;
2272 if (copy_to_user(arg, cc, sizeof(*cc)))
2280 sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
2282 struct sn9c102_sensor* s = &cam->sensor;
2283 struct v4l2_crop crop = {
2284 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2287 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
2289 if (copy_to_user(arg, &crop, sizeof(crop)))
2297 sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
2299 struct sn9c102_sensor* s = &cam->sensor;
2300 struct v4l2_crop crop;
2301 struct v4l2_rect* rect;
2302 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2303 struct v4l2_pix_format* pix_format = &(s->pix_format);
2305 const enum sn9c102_stream_state stream = cam->stream;
2306 const u32 nbuffers = cam->nbuffers;
2310 if (copy_from_user(&crop, arg, sizeof(crop)))
2315 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2318 if (cam->module_param.force_munmap)
2319 for (i = 0; i < cam->nbuffers; i++)
2320 if (cam->frame[i].vma_use_count) {
2321 DBG(3, "VIDIOC_S_CROP failed. "
2322 "Unmap the buffers first.");
2326 /* Preserve R,G or B origin */
2327 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
2328 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
2330 if (rect->width < 16)
2332 if (rect->height < 16)
2334 if (rect->width > bounds->width)
2335 rect->width = bounds->width;
2336 if (rect->height > bounds->height)
2337 rect->height = bounds->height;
2338 if (rect->left < bounds->left)
2339 rect->left = bounds->left;
2340 if (rect->top < bounds->top)
2341 rect->top = bounds->top;
2342 if (rect->left + rect->width > bounds->left + bounds->width)
2343 rect->left = bounds->left+bounds->width - rect->width;
2344 if (rect->top + rect->height > bounds->top + bounds->height)
2345 rect->top = bounds->top+bounds->height - rect->height;
2347 rect->width &= ~15L;
2348 rect->height &= ~15L;
2350 if (SN9C102_PRESERVE_IMGSCALE) {
2351 /* Calculate the actual scaling factor */
2353 a = rect->width * rect->height;
2354 b = pix_format->width * pix_format->height;
2355 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2359 if (cam->stream == STREAM_ON)
2360 if ((err = sn9c102_stream_interrupt(cam)))
2363 if (copy_to_user(arg, &crop, sizeof(crop))) {
2364 cam->stream = stream;
2368 if (cam->module_param.force_munmap || cam->io == IO_READ)
2369 sn9c102_release_buffers(cam);
2371 err = sn9c102_set_crop(cam, rect);
2373 err += s->set_crop(cam, rect);
2374 err += sn9c102_set_scale(cam, scale);
2376 if (err) { /* atomic, no rollback in ioctl() */
2377 cam->state |= DEV_MISCONFIGURED;
2378 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2379 "use the camera, close and open /dev/video%d again.",
2380 cam->v4ldev->minor);
2384 s->pix_format.width = rect->width/scale;
2385 s->pix_format.height = rect->height/scale;
2386 memcpy(&(s->_rect), rect, sizeof(*rect));
2388 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2389 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2390 cam->state |= DEV_MISCONFIGURED;
2391 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2392 "use the camera, close and open /dev/video%d again.",
2393 cam->v4ldev->minor);
2397 if (cam->io == IO_READ)
2398 sn9c102_empty_framequeues(cam);
2399 else if (cam->module_param.force_munmap)
2400 sn9c102_requeue_outqueue(cam);
2402 cam->stream = stream;
2409 sn9c102_vidioc_enum_framesizes(struct sn9c102_device* cam, void __user * arg)
2411 struct v4l2_frmsizeenum frmsize;
2413 if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
2416 if (frmsize.index != 0)
2419 switch (cam->bridge) {
2420 case BRIDGE_SN9C101:
2421 case BRIDGE_SN9C102:
2422 case BRIDGE_SN9C103:
2423 if (frmsize.pixel_format != V4L2_PIX_FMT_SN9C10X &&
2424 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
2426 case BRIDGE_SN9C105:
2427 case BRIDGE_SN9C120:
2428 if (frmsize.pixel_format != V4L2_PIX_FMT_JPEG &&
2429 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
2433 frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
2434 frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
2435 frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
2436 frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
2437 frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
2438 memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
2440 if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
2448 sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
2450 struct v4l2_fmtdesc fmtd;
2452 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
2455 if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2458 if (fmtd.index == 0) {
2459 strcpy(fmtd.description, "bayer rgb");
2460 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2461 } else if (fmtd.index == 1) {
2462 switch (cam->bridge) {
2463 case BRIDGE_SN9C101:
2464 case BRIDGE_SN9C102:
2465 case BRIDGE_SN9C103:
2466 strcpy(fmtd.description, "compressed");
2467 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2469 case BRIDGE_SN9C105:
2470 case BRIDGE_SN9C120:
2471 strcpy(fmtd.description, "JPEG");
2472 fmtd.pixelformat = V4L2_PIX_FMT_JPEG;
2475 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2479 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2480 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2482 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
2490 sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2492 struct v4l2_format format;
2493 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
2495 if (copy_from_user(&format, arg, sizeof(format)))
2498 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2501 pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_JPEG) ?
2502 V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB;
2503 pfmt->bytesperline = (pfmt->pixelformat == V4L2_PIX_FMT_SN9C10X ||
2504 pfmt->pixelformat == V4L2_PIX_FMT_JPEG)
2505 ? 0 : (pfmt->width * pfmt->priv) / 8;
2506 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2507 pfmt->field = V4L2_FIELD_NONE;
2508 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2510 if (copy_to_user(arg, &format, sizeof(format)))
2518 sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2521 struct sn9c102_sensor* s = &cam->sensor;
2522 struct v4l2_format format;
2523 struct v4l2_pix_format* pix;
2524 struct v4l2_pix_format* pfmt = &(s->pix_format);
2525 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2526 struct v4l2_rect rect;
2528 const enum sn9c102_stream_state stream = cam->stream;
2529 const u32 nbuffers = cam->nbuffers;
2533 if (copy_from_user(&format, arg, sizeof(format)))
2536 pix = &(format.fmt.pix);
2538 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2541 memcpy(&rect, &(s->_rect), sizeof(rect));
2543 { /* calculate the actual scaling factor */
2545 a = rect.width * rect.height;
2546 b = pix->width * pix->height;
2547 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2550 rect.width = scale * pix->width;
2551 rect.height = scale * pix->height;
2553 if (rect.width < 16)
2555 if (rect.height < 16)
2557 if (rect.width > bounds->left + bounds->width - rect.left)
2558 rect.width = bounds->left + bounds->width - rect.left;
2559 if (rect.height > bounds->top + bounds->height - rect.top)
2560 rect.height = bounds->top + bounds->height - rect.top;
2563 rect.height &= ~15L;
2565 { /* adjust the scaling factor */
2567 a = rect.width * rect.height;
2568 b = pix->width * pix->height;
2569 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2572 pix->width = rect.width / scale;
2573 pix->height = rect.height / scale;
2575 switch (cam->bridge) {
2576 case BRIDGE_SN9C101:
2577 case BRIDGE_SN9C102:
2578 case BRIDGE_SN9C103:
2579 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2580 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2581 pix->pixelformat = pfmt->pixelformat;
2583 case BRIDGE_SN9C105:
2584 case BRIDGE_SN9C120:
2585 if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
2586 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2587 pix->pixelformat = pfmt->pixelformat;
2590 pix->priv = pfmt->priv; /* bpp */
2591 pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_JPEG) ?
2592 V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB;
2593 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
2594 pix->pixelformat == V4L2_PIX_FMT_JPEG)
2595 ? 0 : (pix->width * pix->priv) / 8;
2596 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2597 pix->field = V4L2_FIELD_NONE;
2599 if (cmd == VIDIOC_TRY_FMT) {
2600 if (copy_to_user(arg, &format, sizeof(format)))
2605 if (cam->module_param.force_munmap)
2606 for (i = 0; i < cam->nbuffers; i++)
2607 if (cam->frame[i].vma_use_count) {
2608 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2613 if (cam->stream == STREAM_ON)
2614 if ((err = sn9c102_stream_interrupt(cam)))
2617 if (copy_to_user(arg, &format, sizeof(format))) {
2618 cam->stream = stream;
2622 if (cam->module_param.force_munmap || cam->io == IO_READ)
2623 sn9c102_release_buffers(cam);
2625 err += sn9c102_set_pix_format(cam, pix);
2626 err += sn9c102_set_crop(cam, &rect);
2627 if (s->set_pix_format)
2628 err += s->set_pix_format(cam, pix);
2630 err += s->set_crop(cam, &rect);
2631 err += sn9c102_set_scale(cam, scale);
2633 if (err) { /* atomic, no rollback in ioctl() */
2634 cam->state |= DEV_MISCONFIGURED;
2635 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2636 "use the camera, close and open /dev/video%d again.",
2637 cam->v4ldev->minor);
2641 memcpy(pfmt, pix, sizeof(*pix));
2642 memcpy(&(s->_rect), &rect, sizeof(rect));
2644 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2645 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2646 cam->state |= DEV_MISCONFIGURED;
2647 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2648 "use the camera, close and open /dev/video%d again.",
2649 cam->v4ldev->minor);
2653 if (cam->io == IO_READ)
2654 sn9c102_empty_framequeues(cam);
2655 else if (cam->module_param.force_munmap)
2656 sn9c102_requeue_outqueue(cam);
2658 cam->stream = stream;
2665 sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2667 if (copy_to_user(arg, &cam->compression, sizeof(cam->compression)))
2675 sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2677 struct v4l2_jpegcompression jc;
2678 const enum sn9c102_stream_state stream = cam->stream;
2681 if (copy_from_user(&jc, arg, sizeof(jc)))
2684 if (jc.quality != 0 && jc.quality != 1)
2687 if (cam->stream == STREAM_ON)
2688 if ((err = sn9c102_stream_interrupt(cam)))
2691 err += sn9c102_set_compression(cam, &jc);
2692 if (err) { /* atomic, no rollback in ioctl() */
2693 cam->state |= DEV_MISCONFIGURED;
2694 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2695 "problems. To use the camera, close and open "
2696 "/dev/video%d again.", cam->v4ldev->minor);
2700 cam->compression.quality = jc.quality;
2702 cam->stream = stream;
2709 sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
2711 struct v4l2_requestbuffers rb;
2715 if (copy_from_user(&rb, arg, sizeof(rb)))
2718 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2719 rb.memory != V4L2_MEMORY_MMAP)
2722 if (cam->io == IO_READ) {
2723 DBG(3, "Close and open the device again to choose the mmap "
2728 for (i = 0; i < cam->nbuffers; i++)
2729 if (cam->frame[i].vma_use_count) {
2730 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2735 if (cam->stream == STREAM_ON)
2736 if ((err = sn9c102_stream_interrupt(cam)))
2739 sn9c102_empty_framequeues(cam);
2741 sn9c102_release_buffers(cam);
2743 rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
2745 if (copy_to_user(arg, &rb, sizeof(rb))) {
2746 sn9c102_release_buffers(cam);
2751 cam->io = rb.count ? IO_MMAP : IO_NONE;
2758 sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
2760 struct v4l2_buffer b;
2762 if (copy_from_user(&b, arg, sizeof(b)))
2765 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2766 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2769 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2771 if (cam->frame[b.index].vma_use_count)
2772 b.flags |= V4L2_BUF_FLAG_MAPPED;
2774 if (cam->frame[b.index].state == F_DONE)
2775 b.flags |= V4L2_BUF_FLAG_DONE;
2776 else if (cam->frame[b.index].state != F_UNUSED)
2777 b.flags |= V4L2_BUF_FLAG_QUEUED;
2779 if (copy_to_user(arg, &b, sizeof(b)))
2787 sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
2789 struct v4l2_buffer b;
2790 unsigned long lock_flags;
2792 if (copy_from_user(&b, arg, sizeof(b)))
2795 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2796 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2799 if (cam->frame[b.index].state != F_UNUSED)
2802 cam->frame[b.index].state = F_QUEUED;
2804 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2805 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2806 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2808 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2815 sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2818 struct v4l2_buffer b;
2819 struct sn9c102_frame_t *f;
2820 unsigned long lock_flags;
2824 if (copy_from_user(&b, arg, sizeof(b)))
2827 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2830 if (list_empty(&cam->outqueue)) {
2831 if (cam->stream == STREAM_OFF)
2833 if (filp->f_flags & O_NONBLOCK)
2835 if (!cam->module_param.frame_timeout) {
2836 err = wait_event_interruptible
2838 (!list_empty(&cam->outqueue)) ||
2839 (cam->state & DEV_DISCONNECTED) ||
2840 (cam->state & DEV_MISCONFIGURED) );
2844 timeout = wait_event_interruptible_timeout
2846 (!list_empty(&cam->outqueue)) ||
2847 (cam->state & DEV_DISCONNECTED) ||
2848 (cam->state & DEV_MISCONFIGURED),
2849 cam->module_param.frame_timeout *
2850 1000 * msecs_to_jiffies(1) );
2853 else if (timeout == 0 &&
2854 !(cam->state & DEV_DISCONNECTED)) {
2855 DBG(1, "Video frame timeout elapsed");
2859 if (cam->state & DEV_DISCONNECTED)
2861 if (cam->state & DEV_MISCONFIGURED)
2865 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2866 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
2867 list_del(cam->outqueue.next);
2868 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2870 f->state = F_UNUSED;
2872 memcpy(&b, &f->buf, sizeof(b));
2873 if (f->vma_use_count)
2874 b.flags |= V4L2_BUF_FLAG_MAPPED;
2876 if (copy_to_user(arg, &b, sizeof(b)))
2879 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2886 sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
2890 if (copy_from_user(&type, arg, sizeof(type)))
2893 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2896 cam->stream = STREAM_ON;
2898 DBG(3, "Stream on");
2905 sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
2909 if (copy_from_user(&type, arg, sizeof(type)))
2912 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2915 if (cam->stream == STREAM_ON)
2916 if ((err = sn9c102_stream_interrupt(cam)))
2919 sn9c102_empty_framequeues(cam);
2921 DBG(3, "Stream off");
2928 sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
2930 struct v4l2_streamparm sp;
2932 if (copy_from_user(&sp, arg, sizeof(sp)))
2935 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2938 sp.parm.capture.extendedmode = 0;
2939 sp.parm.capture.readbuffers = cam->nreadbuffers;
2941 if (copy_to_user(arg, &sp, sizeof(sp)))
2949 sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
2951 struct v4l2_streamparm sp;
2953 if (copy_from_user(&sp, arg, sizeof(sp)))
2956 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2959 sp.parm.capture.extendedmode = 0;
2961 if (sp.parm.capture.readbuffers == 0)
2962 sp.parm.capture.readbuffers = cam->nreadbuffers;
2964 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2965 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2967 if (copy_to_user(arg, &sp, sizeof(sp)))
2970 cam->nreadbuffers = sp.parm.capture.readbuffers;
2977 sn9c102_vidioc_enumaudio(struct sn9c102_device* cam, void __user * arg)
2979 struct v4l2_audio audio;
2981 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
2984 if (copy_from_user(&audio, arg, sizeof(audio)))
2987 if (audio.index != 0)
2990 strcpy(audio.name, "Microphone");
2991 audio.capability = 0;
2994 if (copy_to_user(arg, &audio, sizeof(audio)))
3002 sn9c102_vidioc_g_audio(struct sn9c102_device* cam, void __user * arg)
3004 struct v4l2_audio audio;
3006 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
3009 if (copy_from_user(&audio, arg, sizeof(audio)))
3012 memset(&audio, 0, sizeof(audio));
3013 strcpy(audio.name, "Microphone");
3015 if (copy_to_user(arg, &audio, sizeof(audio)))
3023 sn9c102_vidioc_s_audio(struct sn9c102_device* cam, void __user * arg)
3025 struct v4l2_audio audio;
3027 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
3030 if (copy_from_user(&audio, arg, sizeof(audio)))
3033 if (audio.index != 0)
3040 static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
3041 unsigned int cmd, void __user * arg)
3043 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
3047 case VIDIOC_QUERYCAP:
3048 return sn9c102_vidioc_querycap(cam, arg);
3050 case VIDIOC_ENUMINPUT:
3051 return sn9c102_vidioc_enuminput(cam, arg);
3053 case VIDIOC_G_INPUT:
3054 return sn9c102_vidioc_g_input(cam, arg);
3056 case VIDIOC_S_INPUT:
3057 return sn9c102_vidioc_s_input(cam, arg);
3059 case VIDIOC_QUERYCTRL:
3060 return sn9c102_vidioc_query_ctrl(cam, arg);
3063 return sn9c102_vidioc_g_ctrl(cam, arg);
3066 return sn9c102_vidioc_s_ctrl(cam, arg);
3068 case VIDIOC_CROPCAP:
3069 return sn9c102_vidioc_cropcap(cam, arg);
3072 return sn9c102_vidioc_g_crop(cam, arg);
3075 return sn9c102_vidioc_s_crop(cam, arg);
3077 case VIDIOC_ENUM_FRAMESIZES:
3078 return sn9c102_vidioc_enum_framesizes(cam, arg);
3080 case VIDIOC_ENUM_FMT:
3081 return sn9c102_vidioc_enum_fmt(cam, arg);
3084 return sn9c102_vidioc_g_fmt(cam, arg);
3086 case VIDIOC_TRY_FMT:
3088 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
3090 case VIDIOC_G_JPEGCOMP:
3091 return sn9c102_vidioc_g_jpegcomp(cam, arg);
3093 case VIDIOC_S_JPEGCOMP:
3094 return sn9c102_vidioc_s_jpegcomp(cam, arg);
3096 case VIDIOC_REQBUFS:
3097 return sn9c102_vidioc_reqbufs(cam, arg);
3099 case VIDIOC_QUERYBUF:
3100 return sn9c102_vidioc_querybuf(cam, arg);
3103 return sn9c102_vidioc_qbuf(cam, arg);
3106 return sn9c102_vidioc_dqbuf(cam, filp, arg);
3108 case VIDIOC_STREAMON:
3109 return sn9c102_vidioc_streamon(cam, arg);
3111 case VIDIOC_STREAMOFF:
3112 return sn9c102_vidioc_streamoff(cam, arg);
3115 return sn9c102_vidioc_g_parm(cam, arg);
3118 return sn9c102_vidioc_s_parm(cam, arg);
3120 case VIDIOC_ENUMAUDIO:
3121 return sn9c102_vidioc_enumaudio(cam, arg);
3123 case VIDIOC_G_AUDIO:
3124 return sn9c102_vidioc_g_audio(cam, arg);
3126 case VIDIOC_S_AUDIO:
3127 return sn9c102_vidioc_s_audio(cam, arg);
3131 case VIDIOC_QUERYSTD:
3132 case VIDIOC_ENUMSTD:
3133 case VIDIOC_QUERYMENU:
3134 case VIDIOC_ENUM_FRAMEINTERVALS:
3144 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
3145 unsigned int cmd, unsigned long arg)
3147 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
3150 if (mutex_lock_interruptible(&cam->fileop_mutex))
3151 return -ERESTARTSYS;
3153 if (cam->state & DEV_DISCONNECTED) {
3154 DBG(1, "Device not present");
3155 mutex_unlock(&cam->fileop_mutex);
3159 if (cam->state & DEV_MISCONFIGURED) {
3160 DBG(1, "The camera is misconfigured. Close and open it "
3162 mutex_unlock(&cam->fileop_mutex);
3166 V4LDBG(3, "sn9c102", cmd);
3168 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
3170 mutex_unlock(&cam->fileop_mutex);
3175 /*****************************************************************************/
3177 static const struct file_operations sn9c102_fops = {
3178 .owner = THIS_MODULE,
3179 .open = sn9c102_open,
3180 .release = sn9c102_release,
3181 .ioctl = sn9c102_ioctl,
3182 .compat_ioctl = v4l_compat_ioctl32,
3183 .read = sn9c102_read,
3184 .poll = sn9c102_poll,
3185 .mmap = sn9c102_mmap,
3186 .llseek = no_llseek,
3189 /*****************************************************************************/
3191 /* It exists a single interface only. We do not need to validate anything. */
3193 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3195 struct usb_device *udev = interface_to_usbdev(intf);
3196 struct sn9c102_device* cam;
3197 static unsigned int dev_nr = 0;
3201 if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
3206 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
3207 DBG(1, "kzalloc() failed");
3212 if (!(cam->v4ldev = video_device_alloc())) {
3213 DBG(1, "video_device_alloc() failed");
3218 mutex_init(&cam->dev_mutex);
3220 r = sn9c102_read_reg(cam, 0x00);
3221 if (r < 0 || (r != 0x10 && r != 0x11 && r != 0x12)) {
3222 DBG(1, "Sorry, this is not a SN9C1xx-based camera "
3223 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3228 cam->bridge = id->driver_info;
3229 switch (cam->bridge) {
3230 case BRIDGE_SN9C101:
3231 case BRIDGE_SN9C102:
3232 DBG(2, "SN9C10[12] PC Camera Controller detected "
3233 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3235 case BRIDGE_SN9C103:
3236 DBG(2, "SN9C103 PC Camera Controller detected "
3237 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3239 case BRIDGE_SN9C105:
3240 DBG(2, "SN9C105 PC Camera Controller detected "
3241 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3243 case BRIDGE_SN9C120:
3244 DBG(2, "SN9C120 PC Camera Controller detected "
3245 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3249 for (i = 0; i < ARRAY_SIZE(sn9c102_sensor_table); i++) {
3250 err = sn9c102_sensor_table[i](cam);
3256 DBG(2, "%s image sensor detected", cam->sensor.name);
3257 DBG(3, "Support for %s maintained by %s",
3258 cam->sensor.name, cam->sensor.maintainer);
3260 DBG(1, "No supported image sensor detected for this bridge");
3265 if (!(cam->bridge & cam->sensor.supported_bridge)) {
3266 DBG(1, "Bridge not supported");
3271 if (sn9c102_init(cam)) {
3272 DBG(1, "Initialization failed. I will retry on open().");
3273 cam->state |= DEV_MISCONFIGURED;
3276 strcpy(cam->v4ldev->name, "SN9C1xx PC Camera");
3277 cam->v4ldev->owner = THIS_MODULE;
3278 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
3279 cam->v4ldev->hardware = 0;
3280 cam->v4ldev->fops = &sn9c102_fops;
3281 cam->v4ldev->minor = video_nr[dev_nr];
3282 cam->v4ldev->release = video_device_release;
3283 video_set_drvdata(cam->v4ldev, cam);
3285 mutex_lock(&cam->dev_mutex);
3287 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
3290 DBG(1, "V4L2 device registration failed");
3291 if (err == -ENFILE && video_nr[dev_nr] == -1)
3292 DBG(1, "Free /dev/videoX node not found");
3293 video_nr[dev_nr] = -1;
3294 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
3295 mutex_unlock(&cam->dev_mutex);
3299 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
3301 cam->module_param.force_munmap = force_munmap[dev_nr];
3302 cam->module_param.frame_timeout = frame_timeout[dev_nr];
3304 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
3306 #ifdef CONFIG_VIDEO_ADV_DEBUG
3307 err = sn9c102_create_sysfs(cam);
3309 DBG(2, "Optional device control through 'sysfs' "
3312 DBG(2, "Failed to create optional 'sysfs' interface for "
3313 "device controlling. Error #%d", err);
3315 DBG(2, "Optional device control through 'sysfs' interface disabled");
3316 DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
3317 "configuration option to enable it.");
3320 usb_set_intfdata(intf, cam);
3322 mutex_unlock(&cam->dev_mutex);
3328 kfree(cam->control_buffer);
3330 video_device_release(cam->v4ldev);
3337 static void sn9c102_usb_disconnect(struct usb_interface* intf)
3339 struct sn9c102_device* cam = usb_get_intfdata(intf);
3344 down_write(&sn9c102_disconnect);
3346 mutex_lock(&cam->dev_mutex);
3348 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
3350 wake_up_interruptible_all(&cam->open);
3353 DBG(2, "Device /dev/video%d is open! Deregistration and "
3354 "memory deallocation are deferred on close.",
3355 cam->v4ldev->minor);
3356 cam->state |= DEV_MISCONFIGURED;
3357 sn9c102_stop_transfer(cam);
3358 cam->state |= DEV_DISCONNECTED;
3359 wake_up_interruptible(&cam->wait_frame);
3360 wake_up(&cam->wait_stream);
3361 usb_get_dev(cam->usbdev);
3363 cam->state |= DEV_DISCONNECTED;
3364 sn9c102_release_resources(cam);
3367 mutex_unlock(&cam->dev_mutex);
3372 up_write(&sn9c102_disconnect);
3376 static struct usb_driver sn9c102_usb_driver = {
3378 .id_table = sn9c102_id_table,
3379 .probe = sn9c102_usb_probe,
3380 .disconnect = sn9c102_usb_disconnect,
3383 /*****************************************************************************/
3385 static int __init sn9c102_module_init(void)
3389 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
3390 KDBG(3, SN9C102_MODULE_AUTHOR);
3392 if ((err = usb_register(&sn9c102_usb_driver)))
3393 KDBG(1, "usb_register() failed");
3399 static void __exit sn9c102_module_exit(void)
3401 usb_deregister(&sn9c102_usb_driver);
3405 module_init(sn9c102_module_init);
3406 module_exit(sn9c102_module_exit);