1 /***************************************************************************
2 * V4L2 driver for SN9C1xx PC Camera Controllers *
4 * Copyright (C) 2004-2007 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/param.h>
25 #include <linux/moduleparam.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/device.h>
30 #include <linux/delay.h>
31 #include <linux/compiler.h>
32 #include <linux/ioctl.h>
33 #include <linux/poll.h>
34 #include <linux/stat.h>
36 #include <linux/vmalloc.h>
37 #include <linux/page-flags.h>
38 #include <linux/byteorder/generic.h>
40 #include <asm/uaccess.h>
44 /*****************************************************************************/
46 #define SN9C102_MODULE_NAME "V4L2 driver for SN9C1xx PC Camera Controllers"
47 #define SN9C102_MODULE_ALIAS "sn9c1xx"
48 #define SN9C102_MODULE_AUTHOR "(C) 2004-2007 Luca Risolia"
49 #define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
50 #define SN9C102_MODULE_LICENSE "GPL"
51 #define SN9C102_MODULE_VERSION "1:1.39"
52 #define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 39)
54 /*****************************************************************************/
56 MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
58 MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
59 MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
60 MODULE_ALIAS(SN9C102_MODULE_ALIAS);
61 MODULE_VERSION(SN9C102_MODULE_VERSION);
62 MODULE_LICENSE(SN9C102_MODULE_LICENSE);
64 static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
65 module_param_array(video_nr, short, NULL, 0444);
66 MODULE_PARM_DESC(video_nr,
67 "\n<-1|n[,...]> Specify V4L2 minor mode number."
68 "\n -1 = use next available (default)"
69 "\n n = use minor number n (integer >= 0)"
70 "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
73 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
74 "\nthe second camera and use auto for the first"
75 "\none and for every other camera."
78 static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
79 SN9C102_FORCE_MUNMAP};
80 module_param_array(force_munmap, bool, NULL, 0444);
81 MODULE_PARM_DESC(force_munmap,
82 "\n<0|1[,...]> Force the application to unmap previously"
83 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
84 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
85 "\nthis feature. This parameter is specific for each"
87 "\n 0 = do not force memory unmapping"
88 "\n 1 = force memory unmapping (save memory)"
89 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
92 static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] =
93 SN9C102_FRAME_TIMEOUT};
94 module_param_array(frame_timeout, uint, NULL, 0644);
95 MODULE_PARM_DESC(frame_timeout,
96 "\n<0|n[,...]> Timeout for a video frame in seconds before"
97 "\nreturning an I/O error; 0 for infinity."
98 "\nThis parameter is specific for each detected camera."
99 "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"."
103 static unsigned short debug = SN9C102_DEBUG_LEVEL;
104 module_param(debug, ushort, 0644);
105 MODULE_PARM_DESC(debug,
106 "\n<n> Debugging information level, from 0 to 3:"
107 "\n0 = none (use carefully)"
108 "\n1 = critical errors"
109 "\n2 = significant informations"
110 "\n3 = more verbose messages"
111 "\nLevel 3 is useful for testing only."
112 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
116 /*****************************************************************************/
119 sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
120 enum sn9c102_io_method io)
122 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
123 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
124 size_t imagesize = cam->module_param.force_munmap || io == IO_READ ?
125 (p->width * p->height * p->priv) / 8 :
126 (r->width * r->height * p->priv) / 8;
130 if (count > SN9C102_MAX_FRAMES)
131 count = SN9C102_MAX_FRAMES;
133 if (cam->bridge == BRIDGE_SN9C105 || cam->bridge == BRIDGE_SN9C120)
134 imagesize += 589 + 2; /* length of JPEG header + EOI marker */
136 cam->nbuffers = count;
137 while (cam->nbuffers > 0) {
138 if ((buff = vmalloc_32_user(cam->nbuffers *
139 PAGE_ALIGN(imagesize))))
144 for (i = 0; i < cam->nbuffers; i++) {
145 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
146 cam->frame[i].buf.index = i;
147 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
148 cam->frame[i].buf.length = imagesize;
149 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
150 cam->frame[i].buf.sequence = 0;
151 cam->frame[i].buf.field = V4L2_FIELD_NONE;
152 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
153 cam->frame[i].buf.flags = 0;
156 return cam->nbuffers;
160 static void sn9c102_release_buffers(struct sn9c102_device* cam)
163 vfree(cam->frame[0].bufmem);
166 cam->frame_current = NULL;
170 static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
174 INIT_LIST_HEAD(&cam->inqueue);
175 INIT_LIST_HEAD(&cam->outqueue);
177 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
178 cam->frame[i].state = F_UNUSED;
179 cam->frame[i].buf.bytesused = 0;
184 static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
186 struct sn9c102_frame_t *i;
188 list_for_each_entry(i, &cam->outqueue, frame) {
190 list_add(&i->frame, &cam->inqueue);
193 INIT_LIST_HEAD(&cam->outqueue);
197 static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
199 unsigned long lock_flags;
202 for (i = 0; i < cam->nbuffers; i++)
203 if (cam->frame[i].state == F_UNUSED) {
204 cam->frame[i].state = F_QUEUED;
205 spin_lock_irqsave(&cam->queue_lock, lock_flags);
206 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
207 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
211 /*****************************************************************************/
213 * Write a sequence of count value/register pairs. Returns -1 after the
214 * first failed write, or 0 for no errors.
216 int sn9c102_write_regs(struct sn9c102_device* cam, const u8 valreg[][2],
219 struct usb_device* udev = cam->usbdev;
220 u8* value = cam->control_buffer; /* Needed for DMA'able memory */
223 for (i = 0; i < count; i++) {
224 u8 index = valreg[i][1];
227 * index is a u8, so it must be <256 and can't be out of range.
228 * If we put in a check anyway, gcc annoys us with a warning
229 * that our check is useless. People get all uppity when they
230 * see warnings in the kernel compile.
233 *value = valreg[i][0];
234 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
235 0x08, 0x41, index, 0,
236 value, 1, SN9C102_CTRL_TIMEOUT);
238 DBG(3, "Failed to write a register (value 0x%02X, "
239 "index 0x%02X, error %d)", *value, index, res);
243 cam->reg[index] = *value;
250 int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
252 struct usb_device* udev = cam->usbdev;
253 u8* buff = cam->control_buffer;
256 if (index >= ARRAY_SIZE(cam->reg))
261 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
262 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
264 DBG(3, "Failed to write a register (value 0x%02X, index "
265 "0x%02X, error %d)", value, index, res);
269 cam->reg[index] = value;
275 /* NOTE: reading some registers always returns 0 */
276 static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
278 struct usb_device* udev = cam->usbdev;
279 u8* buff = cam->control_buffer;
282 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
283 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
285 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
288 return (res >= 0) ? (int)(*buff) : -1;
292 int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
294 if (index >= ARRAY_SIZE(cam->reg))
297 return cam->reg[index];
302 sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
306 for (i = 1; i <= 5; i++) {
307 r = sn9c102_read_reg(cam, 0x08);
312 if (sensor->frequency & SN9C102_I2C_400KHZ)
322 sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
323 struct sn9c102_sensor* sensor)
327 r = sn9c102_read_reg(cam, 0x08);
331 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
339 return err ? -EIO : 0;
344 sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
345 struct sn9c102_sensor* sensor)
348 r = sn9c102_read_reg(cam, 0x08);
349 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
354 sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
355 struct sn9c102_sensor* sensor, u8 data0, u8 data1,
358 struct usb_device* udev = cam->usbdev;
359 u8* data = cam->control_buffer;
363 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
364 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
365 data[1] = data0; /* I2C slave id */
366 data[2] = data1; /* address */
368 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
369 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
373 err += sn9c102_i2c_wait(cam, sensor);
375 /* Read cycle - n bytes */
376 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
377 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
381 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
382 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
386 err += sn9c102_i2c_wait(cam, sensor);
388 /* The first read byte will be placed in data[4] */
389 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
390 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
394 err += sn9c102_i2c_detect_read_error(cam, sensor);
396 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
400 DBG(3, "I2C read failed for %s image sensor", sensor->name);
405 memcpy(buffer, data, sizeof(buffer));
412 sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
413 struct sn9c102_sensor* sensor, u8 n, u8 data0,
414 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
416 struct usb_device* udev = cam->usbdev;
417 u8* data = cam->control_buffer;
420 /* Write cycle. It usually is address + value */
421 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
422 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
431 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
432 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
436 err += sn9c102_i2c_wait(cam, sensor);
437 err += sn9c102_i2c_detect_write_error(cam, sensor);
440 DBG(3, "I2C write failed for %s image sensor", sensor->name);
442 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
443 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
444 n, data0, data1, data2, data3, data4, data5);
451 sn9c102_i2c_try_read(struct sn9c102_device* cam,
452 struct sn9c102_sensor* sensor, u8 address)
454 return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
460 sn9c102_i2c_try_write(struct sn9c102_device* cam,
461 struct sn9c102_sensor* sensor, u8 address, u8 value)
463 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
464 sensor->i2c_slave_id, address,
469 int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
471 return sn9c102_i2c_try_read(cam, &cam->sensor, address);
475 int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
477 return sn9c102_i2c_try_write(cam, &cam->sensor, address, value);
480 /*****************************************************************************/
482 static size_t sn9c102_sof_length(struct sn9c102_device* cam)
484 switch (cam->bridge) {
500 sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
502 static const char marker[6] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
504 size_t soflen = 0, i, j;
506 soflen = sn9c102_sof_length(cam);
508 for (i = 0; i < len; i++) {
511 /* Read the variable part of the header */
512 if (unlikely(cam->sof.bytesread >= sizeof(marker))) {
513 cam->sof.header[cam->sof.bytesread] = *(m+i);
514 if (++cam->sof.bytesread == soflen) {
515 cam->sof.bytesread = 0;
521 /* Search for the SOF marker (fixed part) in the header */
522 for (j = 0, b=cam->sof.bytesread; j+b < sizeof(marker); j++) {
523 if (unlikely(i+j) == len)
525 if (*(m+i+j) == marker[cam->sof.bytesread]) {
526 cam->sof.header[cam->sof.bytesread] = *(m+i+j);
527 if (++cam->sof.bytesread == sizeof(marker)) {
528 PDBGG("Bytes to analyze: %zd. SOF "
529 "starts at byte #%zd", len, i);
534 cam->sof.bytesread = 0;
545 sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
547 static const u8 eof_header[4][4] = {
548 {0x00, 0x00, 0x00, 0x00},
549 {0x40, 0x00, 0x00, 0x00},
550 {0x80, 0x00, 0x00, 0x00},
551 {0xc0, 0x00, 0x00, 0x00},
555 /* The EOF header does not exist in compressed data */
556 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
557 cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
561 The EOF header might cross the packet boundary, but this is not a
562 problem, since the end of a frame is determined by checking its size
565 for (i = 0; (len >= 4) && (i <= len - 4); i++)
566 for (j = 0; j < ARRAY_SIZE(eof_header); j++)
567 if (!memcmp(mem + i, eof_header[j], 4))
575 sn9c102_write_jpegheader(struct sn9c102_device* cam, struct sn9c102_frame_t* f)
577 static const u8 jpeg_header[589] = {
578 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00, 0x06, 0x04, 0x05,
579 0x06, 0x05, 0x04, 0x06, 0x06, 0x05, 0x06, 0x07, 0x07, 0x06,
580 0x08, 0x0a, 0x10, 0x0a, 0x0a, 0x09, 0x09, 0x0a, 0x14, 0x0e,
581 0x0f, 0x0c, 0x10, 0x17, 0x14, 0x18, 0x18, 0x17, 0x14, 0x16,
582 0x16, 0x1a, 0x1d, 0x25, 0x1f, 0x1a, 0x1b, 0x23, 0x1c, 0x16,
583 0x16, 0x20, 0x2c, 0x20, 0x23, 0x26, 0x27, 0x29, 0x2a, 0x29,
584 0x19, 0x1f, 0x2d, 0x30, 0x2d, 0x28, 0x30, 0x25, 0x28, 0x29,
585 0x28, 0x01, 0x07, 0x07, 0x07, 0x0a, 0x08, 0x0a, 0x13, 0x0a,
586 0x0a, 0x13, 0x28, 0x1a, 0x16, 0x1a, 0x28, 0x28, 0x28, 0x28,
587 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
588 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
589 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
590 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
591 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0xff, 0xc4, 0x01, 0xa2,
592 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
593 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
594 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01,
595 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
596 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
597 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10, 0x00,
598 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,
599 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04,
600 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
601 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,
602 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62,
603 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,
604 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38,
605 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
606 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
607 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
608 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
609 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
610 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
611 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
612 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3,
613 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3,
614 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3,
615 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02,
616 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04,
617 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
618 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
619 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1,
620 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
621 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19,
622 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
623 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
624 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
625 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
626 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
627 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
628 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
629 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
630 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
631 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3,
632 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
633 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc0, 0x00, 0x11,
634 0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x01, 0x21, 0x00, 0x02,
635 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03,
636 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00
640 memcpy(pos, jpeg_header, sizeof(jpeg_header));
642 *(pos + 7 + 64) = 0x01;
643 if (cam->compression.quality == 0) {
644 memcpy(pos + 7, SN9C102_Y_QTABLE0, 64);
645 memcpy(pos + 8 + 64, SN9C102_UV_QTABLE0, 64);
646 } else if (cam->compression.quality == 1) {
647 memcpy(pos + 7, SN9C102_Y_QTABLE1, 64);
648 memcpy(pos + 8 + 64, SN9C102_UV_QTABLE1, 64);
650 *(pos + 564) = cam->sensor.pix_format.width & 0xFF;
651 *(pos + 563) = (cam->sensor.pix_format.width >> 8) & 0xFF;
652 *(pos + 562) = cam->sensor.pix_format.height & 0xFF;
653 *(pos + 561) = (cam->sensor.pix_format.height >> 8) & 0xFF;
656 f->buf.bytesused += sizeof(jpeg_header);
661 sn9c102_write_eoimarker(struct sn9c102_device* cam, struct sn9c102_frame_t* f)
663 static const u8 eoi_marker[2] = {0xff, 0xd9};
665 memcpy(f->bufmem + f->buf.bytesused, eoi_marker, sizeof(eoi_marker));
666 f->buf.bytesused += sizeof(eoi_marker);
670 static void sn9c102_urb_complete(struct urb *urb)
672 struct sn9c102_device* cam = urb->context;
673 struct sn9c102_frame_t** f;
674 size_t imagesize, soflen;
678 if (urb->status == -ENOENT)
681 f = &cam->frame_current;
683 if (cam->stream == STREAM_INTERRUPT) {
684 cam->stream = STREAM_OFF;
686 (*f)->state = F_QUEUED;
687 cam->sof.bytesread = 0;
688 DBG(3, "Stream interrupted by application");
689 wake_up(&cam->wait_stream);
692 if (cam->state & DEV_DISCONNECTED)
695 if (cam->state & DEV_MISCONFIGURED) {
696 wake_up_interruptible(&cam->wait_frame);
700 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
704 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
707 imagesize = (cam->sensor.pix_format.width *
708 cam->sensor.pix_format.height *
709 cam->sensor.pix_format.priv) / 8;
710 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
711 imagesize += 589; /* length of jpeg header */
712 soflen = sn9c102_sof_length(cam);
714 for (i = 0; i < urb->number_of_packets; i++) {
715 unsigned int img, len, status;
716 void *pos, *sof, *eof;
718 len = urb->iso_frame_desc[i].actual_length;
719 status = urb->iso_frame_desc[i].status;
720 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
723 DBG(3, "Error in isochronous frame");
724 (*f)->state = F_ERROR;
725 cam->sof.bytesread = 0;
729 PDBGG("Isochrnous frame: length %u, #%u i", len, i);
732 sof = sn9c102_find_sof_header(cam, pos, len);
734 eof = sn9c102_find_eof_header(cam, pos, len);
735 if ((*f)->state == F_GRABBING) {
740 img = (eof > pos) ? eof - pos - 1 : 0;
742 if ((*f)->buf.bytesused + img > imagesize) {
744 b = (*f)->buf.bytesused + img -
746 img = imagesize - (*f)->buf.bytesused;
747 PDBGG("Expected EOF not found: video "
750 DBG(3, "Exceeded limit: +%u "
751 "bytes", (unsigned)(b));
754 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
757 if ((*f)->buf.bytesused == 0)
758 do_gettimeofday(&(*f)->buf.timestamp);
760 (*f)->buf.bytesused += img;
762 if ((*f)->buf.bytesused == imagesize ||
763 ((cam->sensor.pix_format.pixelformat ==
764 V4L2_PIX_FMT_SN9C10X ||
765 cam->sensor.pix_format.pixelformat ==
766 V4L2_PIX_FMT_JPEG) && eof)) {
769 b = (*f)->buf.bytesused;
770 (*f)->state = F_DONE;
771 (*f)->buf.sequence= ++cam->frame_count;
773 spin_lock(&cam->queue_lock);
774 list_move_tail(&(*f)->frame,
776 if (!list_empty(&cam->inqueue))
779 struct sn9c102_frame_t,
783 spin_unlock(&cam->queue_lock);
785 memcpy(cam->sysfs.frame_header,
786 cam->sof.header, soflen);
788 DBG(3, "Video frame captured: %lu "
789 "bytes", (unsigned long)(b));
795 (*f)->state = F_ERROR;
796 DBG(3, "Not expected EOF after %lu "
797 "bytes of image data",
799 ((*f)->buf.bytesused));
806 DBG(3, "EOF without SOF");
810 PDBGG("Ignoring pointless isochronous frame");
814 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
816 (*f)->state = F_GRABBING;
817 (*f)->buf.bytesused = 0;
820 if (cam->sensor.pix_format.pixelformat ==
822 sn9c102_write_jpegheader(cam, (*f));
823 DBG(3, "SOF detected: new video frame");
827 } else if ((*f)->state == F_GRABBING) {
828 eof = sn9c102_find_eof_header(cam, pos, len);
829 if (eof && eof < sof)
830 goto end_of_frame; /* (1) */
832 if (cam->sensor.pix_format.pixelformat ==
833 V4L2_PIX_FMT_SN9C10X ||
834 cam->sensor.pix_format.pixelformat ==
836 if (sof - pos >= soflen) {
838 } else { /* remove header */
840 (*f)->buf.bytesused -=
841 (soflen - (sof - pos));
845 DBG(3, "SOF before expected EOF after "
846 "%lu bytes of image data",
848 ((*f)->buf.bytesused));
856 urb->dev = cam->usbdev;
857 err = usb_submit_urb(urb, GFP_ATOMIC);
858 if (err < 0 && err != -EPERM) {
859 cam->state |= DEV_MISCONFIGURED;
860 DBG(1, "usb_submit_urb() failed");
863 wake_up_interruptible(&cam->wait_frame);
867 static int sn9c102_start_transfer(struct sn9c102_device* cam)
869 struct usb_device *udev = cam->usbdev;
871 struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
872 usb_ifnum_to_if(udev, 0),
873 SN9C102_ALTERNATE_SETTING);
874 const unsigned int psz = le16_to_cpu(altsetting->
875 endpoint[0].desc.wMaxPacketSize);
879 for (i = 0; i < SN9C102_URBS; i++) {
880 cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
882 if (!cam->transfer_buffer[i]) {
884 DBG(1, "Not enough memory");
889 for (i = 0; i < SN9C102_URBS; i++) {
890 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
894 DBG(1, "usb_alloc_urb() failed");
899 urb->pipe = usb_rcvisocpipe(udev, 1);
900 urb->transfer_flags = URB_ISO_ASAP;
901 urb->number_of_packets = SN9C102_ISO_PACKETS;
902 urb->complete = sn9c102_urb_complete;
903 urb->transfer_buffer = cam->transfer_buffer[i];
904 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
906 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
907 urb->iso_frame_desc[j].offset = psz * j;
908 urb->iso_frame_desc[j].length = psz;
913 if (!(cam->reg[0x01] & 0x04)) {
914 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
917 DBG(1, "I/O hardware error");
922 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
924 DBG(1, "usb_set_interface() failed");
928 cam->frame_current = NULL;
929 cam->sof.bytesread = 0;
931 for (i = 0; i < SN9C102_URBS; i++) {
932 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
934 for (j = i-1; j >= 0; j--)
935 usb_kill_urb(cam->urb[j]);
936 DBG(1, "usb_submit_urb() failed, error %d", err);
944 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
945 usb_free_urb(cam->urb[i]);
948 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
949 kfree(cam->transfer_buffer[i]);
955 static int sn9c102_stop_transfer(struct sn9c102_device* cam)
957 struct usb_device *udev = cam->usbdev;
961 if (cam->state & DEV_DISCONNECTED)
964 for (i = SN9C102_URBS-1; i >= 0; i--) {
965 usb_kill_urb(cam->urb[i]);
966 usb_free_urb(cam->urb[i]);
967 kfree(cam->transfer_buffer[i]);
970 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
972 DBG(3, "usb_set_interface() failed");
978 static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
982 cam->stream = STREAM_INTERRUPT;
983 timeout = wait_event_timeout(cam->wait_stream,
984 (cam->stream == STREAM_OFF) ||
985 (cam->state & DEV_DISCONNECTED),
986 SN9C102_URB_TIMEOUT);
987 if (cam->state & DEV_DISCONNECTED)
989 else if (cam->stream != STREAM_OFF) {
990 cam->state |= DEV_MISCONFIGURED;
991 DBG(1, "URB timeout reached. The camera is misconfigured. "
992 "To use it, close and open /dev/video%d again.",
1000 /*****************************************************************************/
1002 #ifdef CONFIG_VIDEO_ADV_DEBUG
1003 static u16 sn9c102_strtou16(const char* buff, size_t len, ssize_t* count)
1010 strncpy(str, buff, len);
1013 strncpy(str, buff, 6);
1017 val = simple_strtoul(str, &endp, 0);
1021 *count = (ssize_t)(endp - str);
1022 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
1029 NOTE 1: being inside one of the following methods implies that the v4l
1030 device exists for sure (see kobjects and reference counters)
1031 NOTE 2: buffers are PAGE_SIZE long
1034 static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
1036 struct sn9c102_device* cam;
1039 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1040 return -ERESTARTSYS;
1042 cam = video_get_drvdata(container_of(cd, struct video_device,
1045 mutex_unlock(&sn9c102_sysfs_lock);
1049 count = sprintf(buf, "%u\n", cam->sysfs.reg);
1051 mutex_unlock(&sn9c102_sysfs_lock);
1058 sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
1060 struct sn9c102_device* cam;
1064 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1065 return -ERESTARTSYS;
1067 cam = video_get_drvdata(container_of(cd, struct video_device,
1070 mutex_unlock(&sn9c102_sysfs_lock);
1074 index = sn9c102_strtou16(buf, len, &count);
1075 if (index >= ARRAY_SIZE(cam->reg) || !count) {
1076 mutex_unlock(&sn9c102_sysfs_lock);
1080 cam->sysfs.reg = index;
1082 DBG(2, "Moved SN9C1XX register index to 0x%02X", cam->sysfs.reg);
1083 DBG(3, "Written bytes: %zd", count);
1085 mutex_unlock(&sn9c102_sysfs_lock);
1091 static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
1093 struct sn9c102_device* cam;
1097 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1098 return -ERESTARTSYS;
1100 cam = video_get_drvdata(container_of(cd, struct video_device,
1103 mutex_unlock(&sn9c102_sysfs_lock);
1107 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
1108 mutex_unlock(&sn9c102_sysfs_lock);
1112 count = sprintf(buf, "%d\n", val);
1114 DBG(3, "Read bytes: %zd, value: %d", count, val);
1116 mutex_unlock(&sn9c102_sysfs_lock);
1123 sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
1125 struct sn9c102_device* cam;
1130 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1131 return -ERESTARTSYS;
1133 cam = video_get_drvdata(container_of(cd, struct video_device,
1136 mutex_unlock(&sn9c102_sysfs_lock);
1140 value = sn9c102_strtou16(buf, len, &count);
1142 mutex_unlock(&sn9c102_sysfs_lock);
1146 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
1148 mutex_unlock(&sn9c102_sysfs_lock);
1152 DBG(2, "Written SN9C1XX reg. 0x%02X, val. 0x%02X",
1153 cam->sysfs.reg, value);
1154 DBG(3, "Written bytes: %zd", count);
1156 mutex_unlock(&sn9c102_sysfs_lock);
1162 static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
1164 struct sn9c102_device* cam;
1167 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1168 return -ERESTARTSYS;
1170 cam = video_get_drvdata(container_of(cd, struct video_device,
1173 mutex_unlock(&sn9c102_sysfs_lock);
1177 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
1179 DBG(3, "Read bytes: %zd", count);
1181 mutex_unlock(&sn9c102_sysfs_lock);
1188 sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1190 struct sn9c102_device* cam;
1194 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1195 return -ERESTARTSYS;
1197 cam = video_get_drvdata(container_of(cd, struct video_device,
1200 mutex_unlock(&sn9c102_sysfs_lock);
1204 index = sn9c102_strtou16(buf, len, &count);
1206 mutex_unlock(&sn9c102_sysfs_lock);
1210 cam->sysfs.i2c_reg = index;
1212 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1213 DBG(3, "Written bytes: %zd", count);
1215 mutex_unlock(&sn9c102_sysfs_lock);
1221 static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1223 struct sn9c102_device* cam;
1227 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1228 return -ERESTARTSYS;
1230 cam = video_get_drvdata(container_of(cd, struct video_device,
1233 mutex_unlock(&sn9c102_sysfs_lock);
1237 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) {
1238 mutex_unlock(&sn9c102_sysfs_lock);
1242 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1243 mutex_unlock(&sn9c102_sysfs_lock);
1247 count = sprintf(buf, "%d\n", val);
1249 DBG(3, "Read bytes: %zd, value: %d", count, val);
1251 mutex_unlock(&sn9c102_sysfs_lock);
1258 sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1260 struct sn9c102_device* cam;
1265 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1266 return -ERESTARTSYS;
1268 cam = video_get_drvdata(container_of(cd, struct video_device,
1271 mutex_unlock(&sn9c102_sysfs_lock);
1275 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) {
1276 mutex_unlock(&sn9c102_sysfs_lock);
1280 value = sn9c102_strtou16(buf, len, &count);
1282 mutex_unlock(&sn9c102_sysfs_lock);
1286 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1288 mutex_unlock(&sn9c102_sysfs_lock);
1292 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1293 cam->sysfs.i2c_reg, value);
1294 DBG(3, "Written bytes: %zd", count);
1296 mutex_unlock(&sn9c102_sysfs_lock);
1303 sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1305 struct sn9c102_device* cam;
1306 enum sn9c102_bridge bridge;
1311 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1312 return -ERESTARTSYS;
1314 cam = video_get_drvdata(container_of(cd, struct video_device,
1317 mutex_unlock(&sn9c102_sysfs_lock);
1321 bridge = cam->bridge;
1323 mutex_unlock(&sn9c102_sysfs_lock);
1325 value = sn9c102_strtou16(buf, len, &count);
1330 case BRIDGE_SN9C101:
1331 case BRIDGE_SN9C102:
1334 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1335 res = sn9c102_store_val(cd, buf, len);
1337 case BRIDGE_SN9C103:
1338 case BRIDGE_SN9C105:
1339 case BRIDGE_SN9C120:
1342 if ((res = sn9c102_store_reg(cd, "0x07", 4)) >= 0)
1343 res = sn9c102_store_val(cd, buf, len);
1352 sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1358 value = sn9c102_strtou16(buf, len, &count);
1359 if (!count || value > 0x7f)
1362 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1363 res = sn9c102_store_val(cd, buf, len);
1370 sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1376 value = sn9c102_strtou16(buf, len, &count);
1377 if (!count || value > 0x7f)
1380 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1381 res = sn9c102_store_val(cd, buf, len);
1387 static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1389 struct sn9c102_device* cam;
1392 cam = video_get_drvdata(container_of(cd, struct video_device,
1397 count = sizeof(cam->sysfs.frame_header);
1398 memcpy(buf, cam->sysfs.frame_header, count);
1400 DBG(3, "Frame header, read bytes: %zd", count);
1406 static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1407 sn9c102_show_reg, sn9c102_store_reg);
1408 static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1409 sn9c102_show_val, sn9c102_store_val);
1410 static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1411 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1412 static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1413 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1414 static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1415 static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1416 static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1417 static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
1418 sn9c102_show_frame_header, NULL);
1421 static int sn9c102_create_sysfs(struct sn9c102_device* cam)
1423 struct class_device *classdev = &(cam->v4ldev->class_dev);
1426 if ((err = class_device_create_file(classdev, &class_device_attr_reg)))
1428 if ((err = class_device_create_file(classdev, &class_device_attr_val)))
1430 if ((err = class_device_create_file(classdev,
1431 &class_device_attr_frame_header)))
1434 if (cam->sensor.sysfs_ops) {
1435 if ((err = class_device_create_file(classdev,
1436 &class_device_attr_i2c_reg)))
1437 goto err_frame_header;
1438 if ((err = class_device_create_file(classdev,
1439 &class_device_attr_i2c_val)))
1443 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
1444 if ((err = class_device_create_file(classdev,
1445 &class_device_attr_green)))
1448 if ((err = class_device_create_file(classdev,
1449 &class_device_attr_blue)))
1451 if ((err = class_device_create_file(classdev,
1452 &class_device_attr_red)))
1459 class_device_remove_file(classdev, &class_device_attr_blue);
1461 if (cam->sensor.sysfs_ops)
1462 class_device_remove_file(classdev, &class_device_attr_i2c_val);
1464 if (cam->sensor.sysfs_ops)
1465 class_device_remove_file(classdev, &class_device_attr_i2c_reg);
1467 class_device_remove_file(classdev, &class_device_attr_frame_header);
1469 class_device_remove_file(classdev, &class_device_attr_val);
1471 class_device_remove_file(classdev, &class_device_attr_reg);
1475 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1477 /*****************************************************************************/
1480 sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
1484 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
1485 pix->pixelformat == V4L2_PIX_FMT_JPEG) {
1486 switch (cam->bridge) {
1487 case BRIDGE_SN9C101:
1488 case BRIDGE_SN9C102:
1489 case BRIDGE_SN9C103:
1490 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
1493 case BRIDGE_SN9C105:
1494 case BRIDGE_SN9C120:
1495 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
1500 switch (cam->bridge) {
1501 case BRIDGE_SN9C101:
1502 case BRIDGE_SN9C102:
1503 case BRIDGE_SN9C103:
1504 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
1507 case BRIDGE_SN9C105:
1508 case BRIDGE_SN9C120:
1509 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
1515 return err ? -EIO : 0;
1520 sn9c102_set_compression(struct sn9c102_device* cam,
1521 struct v4l2_jpegcompression* compression)
1525 switch (cam->bridge) {
1526 case BRIDGE_SN9C101:
1527 case BRIDGE_SN9C102:
1528 case BRIDGE_SN9C103:
1529 if (compression->quality == 0)
1530 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01,
1532 else if (compression->quality == 1)
1533 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe,
1536 case BRIDGE_SN9C105:
1537 case BRIDGE_SN9C120:
1538 if (compression->quality == 0) {
1539 for (i = 0; i <= 63; i++) {
1540 err += sn9c102_write_reg(cam,
1541 SN9C102_Y_QTABLE1[i],
1543 err += sn9c102_write_reg(cam,
1544 SN9C102_UV_QTABLE1[i],
1547 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0xbf,
1549 } else if (compression->quality == 1) {
1550 for (i = 0; i <= 63; i++) {
1551 err += sn9c102_write_reg(cam,
1552 SN9C102_Y_QTABLE1[i],
1554 err += sn9c102_write_reg(cam,
1555 SN9C102_UV_QTABLE1[i],
1558 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x40,
1564 return err ? -EIO : 0;
1568 static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1574 r = cam->reg[0x18] & 0xcf;
1575 else if (scale == 2) {
1576 r = cam->reg[0x18] & 0xcf;
1578 } else if (scale == 4)
1579 r = cam->reg[0x18] | 0x20;
1581 err += sn9c102_write_reg(cam, r, 0x18);
1585 PDBGG("Scaling factor: %u", scale);
1591 static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1593 struct sn9c102_sensor* s = &cam->sensor;
1594 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1595 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1596 h_size = (u8)(rect->width / 16),
1597 v_size = (u8)(rect->height / 16);
1600 err += sn9c102_write_reg(cam, h_start, 0x12);
1601 err += sn9c102_write_reg(cam, v_start, 0x13);
1602 err += sn9c102_write_reg(cam, h_size, 0x15);
1603 err += sn9c102_write_reg(cam, v_size, 0x16);
1607 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1608 "%u %u %u %u", h_start, v_start, h_size, v_size);
1614 static int sn9c102_init(struct sn9c102_device* cam)
1616 struct sn9c102_sensor* s = &cam->sensor;
1617 struct v4l2_control ctrl;
1618 struct v4l2_queryctrl *qctrl;
1619 struct v4l2_rect* rect;
1623 if (!(cam->state & DEV_INITIALIZED)) {
1624 init_waitqueue_head(&cam->open);
1626 rect = &(s->cropcap.defrect);
1627 } else { /* use current values */
1632 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1633 err += sn9c102_set_crop(cam, rect);
1640 DBG(3, "Sensor initialization failed");
1645 if (!(cam->state & DEV_INITIALIZED))
1646 if (cam->bridge == BRIDGE_SN9C101 ||
1647 cam->bridge == BRIDGE_SN9C102 ||
1648 cam->bridge == BRIDGE_SN9C103) {
1649 if (s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
1650 s->pix_format.pixelformat= V4L2_PIX_FMT_SBGGR8;
1651 cam->compression.quality = cam->reg[0x17] & 0x01 ?
1654 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1655 s->pix_format.pixelformat = V4L2_PIX_FMT_JPEG;
1656 cam->compression.quality = cam->reg[0x18] & 0x40 ?
1658 err += sn9c102_set_compression(cam, &cam->compression);
1661 err += sn9c102_set_compression(cam, &cam->compression);
1662 err += sn9c102_set_pix_format(cam, &s->pix_format);
1663 if (s->set_pix_format)
1664 err += s->set_pix_format(cam, &s->pix_format);
1668 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
1669 s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
1670 DBG(3, "Compressed video format is active, quality %d",
1671 cam->compression.quality);
1673 DBG(3, "Uncompressed video format is active");
1676 if ((err = s->set_crop(cam, rect))) {
1677 DBG(3, "set_crop() failed");
1682 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1683 if (s->qctrl[i].id != 0 &&
1684 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1685 ctrl.id = s->qctrl[i].id;
1686 ctrl.value = qctrl[i].default_value;
1687 err = s->set_ctrl(cam, &ctrl);
1689 DBG(3, "Set %s control failed",
1693 DBG(3, "Image sensor supports '%s' control",
1698 if (!(cam->state & DEV_INITIALIZED)) {
1699 mutex_init(&cam->fileop_mutex);
1700 spin_lock_init(&cam->queue_lock);
1701 init_waitqueue_head(&cam->wait_frame);
1702 init_waitqueue_head(&cam->wait_stream);
1703 cam->nreadbuffers = 2;
1704 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1705 memcpy(&(s->_rect), &(s->cropcap.defrect),
1706 sizeof(struct v4l2_rect));
1707 cam->state |= DEV_INITIALIZED;
1710 DBG(2, "Initialization succeeded");
1715 static void sn9c102_release_resources(struct sn9c102_device* cam)
1717 mutex_lock(&sn9c102_sysfs_lock);
1719 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1720 video_set_drvdata(cam->v4ldev, NULL);
1721 video_unregister_device(cam->v4ldev);
1723 mutex_unlock(&sn9c102_sysfs_lock);
1725 kfree(cam->control_buffer);
1728 /*****************************************************************************/
1730 static int sn9c102_open(struct inode* inode, struct file* filp)
1732 struct sn9c102_device* cam;
1736 This is the only safe way to prevent race conditions with
1739 if (!down_read_trylock(&sn9c102_disconnect))
1740 return -ERESTARTSYS;
1742 cam = video_get_drvdata(video_devdata(filp));
1744 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1745 up_read(&sn9c102_disconnect);
1746 return -ERESTARTSYS;
1750 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1751 DBG(3, "Simultaneous opens are not supported");
1752 if ((filp->f_flags & O_NONBLOCK) ||
1753 (filp->f_flags & O_NDELAY)) {
1757 mutex_unlock(&cam->dev_mutex);
1758 err = wait_event_interruptible_exclusive(cam->open,
1759 cam->state & DEV_DISCONNECTED
1762 up_read(&sn9c102_disconnect);
1765 if (cam->state & DEV_DISCONNECTED) {
1766 up_read(&sn9c102_disconnect);
1769 mutex_lock(&cam->dev_mutex);
1773 if (cam->state & DEV_MISCONFIGURED) {
1774 err = sn9c102_init(cam);
1776 DBG(1, "Initialization failed again. "
1777 "I will retry on next open().");
1780 cam->state &= ~DEV_MISCONFIGURED;
1783 if ((err = sn9c102_start_transfer(cam)))
1786 filp->private_data = cam;
1789 cam->stream = STREAM_OFF;
1791 cam->frame_count = 0;
1792 sn9c102_empty_framequeues(cam);
1794 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1797 mutex_unlock(&cam->dev_mutex);
1798 up_read(&sn9c102_disconnect);
1803 static int sn9c102_release(struct inode* inode, struct file* filp)
1805 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1807 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1809 sn9c102_stop_transfer(cam);
1811 sn9c102_release_buffers(cam);
1813 if (cam->state & DEV_DISCONNECTED) {
1814 sn9c102_release_resources(cam);
1815 usb_put_dev(cam->usbdev);
1816 mutex_unlock(&cam->dev_mutex);
1822 wake_up_interruptible_nr(&cam->open, 1);
1824 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1826 mutex_unlock(&cam->dev_mutex);
1833 sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1835 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1836 struct sn9c102_frame_t* f, * i;
1837 unsigned long lock_flags;
1841 if (mutex_lock_interruptible(&cam->fileop_mutex))
1842 return -ERESTARTSYS;
1844 if (cam->state & DEV_DISCONNECTED) {
1845 DBG(1, "Device not present");
1846 mutex_unlock(&cam->fileop_mutex);
1850 if (cam->state & DEV_MISCONFIGURED) {
1851 DBG(1, "The camera is misconfigured. Close and open it "
1853 mutex_unlock(&cam->fileop_mutex);
1857 if (cam->io == IO_MMAP) {
1858 DBG(3, "Close and open the device again to choose "
1860 mutex_unlock(&cam->fileop_mutex);
1864 if (cam->io == IO_NONE) {
1865 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1866 DBG(1, "read() failed, not enough memory");
1867 mutex_unlock(&cam->fileop_mutex);
1871 cam->stream = STREAM_ON;
1874 if (list_empty(&cam->inqueue)) {
1875 if (!list_empty(&cam->outqueue))
1876 sn9c102_empty_framequeues(cam);
1877 sn9c102_queue_unusedframes(cam);
1881 mutex_unlock(&cam->fileop_mutex);
1885 if (list_empty(&cam->outqueue)) {
1886 if (filp->f_flags & O_NONBLOCK) {
1887 mutex_unlock(&cam->fileop_mutex);
1890 if (!cam->module_param.frame_timeout) {
1891 err = wait_event_interruptible
1893 (!list_empty(&cam->outqueue)) ||
1894 (cam->state & DEV_DISCONNECTED) ||
1895 (cam->state & DEV_MISCONFIGURED) );
1897 mutex_unlock(&cam->fileop_mutex);
1901 timeout = wait_event_interruptible_timeout
1903 (!list_empty(&cam->outqueue)) ||
1904 (cam->state & DEV_DISCONNECTED) ||
1905 (cam->state & DEV_MISCONFIGURED),
1906 cam->module_param.frame_timeout *
1907 1000 * msecs_to_jiffies(1) );
1909 mutex_unlock(&cam->fileop_mutex);
1911 } else if (timeout == 0 &&
1912 !(cam->state & DEV_DISCONNECTED)) {
1913 DBG(1, "Video frame timeout elapsed");
1914 mutex_unlock(&cam->fileop_mutex);
1918 if (cam->state & DEV_DISCONNECTED) {
1919 mutex_unlock(&cam->fileop_mutex);
1922 if (cam->state & DEV_MISCONFIGURED) {
1923 mutex_unlock(&cam->fileop_mutex);
1928 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1930 if (count > f->buf.bytesused)
1931 count = f->buf.bytesused;
1933 if (copy_to_user(buf, f->bufmem, count)) {
1940 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1941 list_for_each_entry(i, &cam->outqueue, frame)
1942 i->state = F_UNUSED;
1943 INIT_LIST_HEAD(&cam->outqueue);
1944 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1946 sn9c102_queue_unusedframes(cam);
1948 PDBGG("Frame #%lu, bytes read: %zu",
1949 (unsigned long)f->buf.index, count);
1951 mutex_unlock(&cam->fileop_mutex);
1957 static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1959 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1960 struct sn9c102_frame_t* f;
1961 unsigned long lock_flags;
1962 unsigned int mask = 0;
1964 if (mutex_lock_interruptible(&cam->fileop_mutex))
1967 if (cam->state & DEV_DISCONNECTED) {
1968 DBG(1, "Device not present");
1972 if (cam->state & DEV_MISCONFIGURED) {
1973 DBG(1, "The camera is misconfigured. Close and open it "
1978 if (cam->io == IO_NONE) {
1979 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1981 DBG(1, "poll() failed, not enough memory");
1985 cam->stream = STREAM_ON;
1988 if (cam->io == IO_READ) {
1989 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1990 list_for_each_entry(f, &cam->outqueue, frame)
1991 f->state = F_UNUSED;
1992 INIT_LIST_HEAD(&cam->outqueue);
1993 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1994 sn9c102_queue_unusedframes(cam);
1997 poll_wait(filp, &cam->wait_frame, wait);
1999 if (!list_empty(&cam->outqueue))
2000 mask |= POLLIN | POLLRDNORM;
2002 mutex_unlock(&cam->fileop_mutex);
2007 mutex_unlock(&cam->fileop_mutex);
2012 static void sn9c102_vm_open(struct vm_area_struct* vma)
2014 struct sn9c102_frame_t* f = vma->vm_private_data;
2019 static void sn9c102_vm_close(struct vm_area_struct* vma)
2021 /* NOTE: buffers are not freed here */
2022 struct sn9c102_frame_t* f = vma->vm_private_data;
2027 static struct vm_operations_struct sn9c102_vm_ops = {
2028 .open = sn9c102_vm_open,
2029 .close = sn9c102_vm_close,
2033 static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
2035 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2036 unsigned long size = vma->vm_end - vma->vm_start,
2037 start = vma->vm_start;
2041 if (mutex_lock_interruptible(&cam->fileop_mutex))
2042 return -ERESTARTSYS;
2044 if (cam->state & DEV_DISCONNECTED) {
2045 DBG(1, "Device not present");
2046 mutex_unlock(&cam->fileop_mutex);
2050 if (cam->state & DEV_MISCONFIGURED) {
2051 DBG(1, "The camera is misconfigured. Close and open it "
2053 mutex_unlock(&cam->fileop_mutex);
2057 if (!(vma->vm_flags & (VM_WRITE | VM_READ))) {
2058 mutex_unlock(&cam->fileop_mutex);
2062 if (cam->io != IO_MMAP ||
2063 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
2064 mutex_unlock(&cam->fileop_mutex);
2068 for (i = 0; i < cam->nbuffers; i++) {
2069 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
2072 if (i == cam->nbuffers) {
2073 mutex_unlock(&cam->fileop_mutex);
2077 vma->vm_flags |= VM_IO;
2078 vma->vm_flags |= VM_RESERVED;
2080 pos = cam->frame[i].bufmem;
2081 while (size > 0) { /* size is page-aligned */
2082 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
2083 mutex_unlock(&cam->fileop_mutex);
2091 vma->vm_ops = &sn9c102_vm_ops;
2092 vma->vm_private_data = &cam->frame[i];
2094 sn9c102_vm_open(vma);
2096 mutex_unlock(&cam->fileop_mutex);
2101 /*****************************************************************************/
2104 sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
2106 struct v4l2_capability cap = {
2107 .driver = "sn9c102",
2108 .version = SN9C102_MODULE_VERSION_CODE,
2109 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
2113 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
2114 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
2115 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
2116 sizeof(cap.bus_info));
2118 if (copy_to_user(arg, &cap, sizeof(cap)))
2126 sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
2128 struct v4l2_input i;
2130 if (copy_from_user(&i, arg, sizeof(i)))
2136 memset(&i, 0, sizeof(i));
2137 strcpy(i.name, "Camera");
2138 i.type = V4L2_INPUT_TYPE_CAMERA;
2140 if (copy_to_user(arg, &i, sizeof(i)))
2148 sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg)
2152 if (copy_to_user(arg, &index, sizeof(index)))
2160 sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg)
2164 if (copy_from_user(&index, arg, sizeof(index)))
2175 sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
2177 struct sn9c102_sensor* s = &cam->sensor;
2178 struct v4l2_queryctrl qc;
2181 if (copy_from_user(&qc, arg, sizeof(qc)))
2184 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2185 if (qc.id && qc.id == s->qctrl[i].id) {
2186 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
2187 if (copy_to_user(arg, &qc, sizeof(qc)))
2197 sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
2199 struct sn9c102_sensor* s = &cam->sensor;
2200 struct v4l2_control ctrl;
2204 if (!s->get_ctrl && !s->set_ctrl)
2207 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2211 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2212 if (ctrl.id && ctrl.id == s->qctrl[i].id) {
2213 ctrl.value = s->_qctrl[i].default_value;
2218 err = s->get_ctrl(cam, &ctrl);
2221 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
2224 PDBGG("VIDIOC_G_CTRL: id %lu, value %lu",
2225 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
2232 sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
2234 struct sn9c102_sensor* s = &cam->sensor;
2235 struct v4l2_control ctrl;
2242 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2245 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2246 if (ctrl.id == s->qctrl[i].id) {
2247 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
2249 if (ctrl.value < s->qctrl[i].minimum ||
2250 ctrl.value > s->qctrl[i].maximum)
2252 ctrl.value -= ctrl.value % s->qctrl[i].step;
2256 if ((err = s->set_ctrl(cam, &ctrl)))
2259 s->_qctrl[i].default_value = ctrl.value;
2261 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
2262 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
2269 sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
2271 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
2273 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2274 cc->pixelaspect.numerator = 1;
2275 cc->pixelaspect.denominator = 1;
2277 if (copy_to_user(arg, cc, sizeof(*cc)))
2285 sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
2287 struct sn9c102_sensor* s = &cam->sensor;
2288 struct v4l2_crop crop = {
2289 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2292 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
2294 if (copy_to_user(arg, &crop, sizeof(crop)))
2302 sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
2304 struct sn9c102_sensor* s = &cam->sensor;
2305 struct v4l2_crop crop;
2306 struct v4l2_rect* rect;
2307 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2308 struct v4l2_pix_format* pix_format = &(s->pix_format);
2310 const enum sn9c102_stream_state stream = cam->stream;
2311 const u32 nbuffers = cam->nbuffers;
2315 if (copy_from_user(&crop, arg, sizeof(crop)))
2320 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2323 if (cam->module_param.force_munmap)
2324 for (i = 0; i < cam->nbuffers; i++)
2325 if (cam->frame[i].vma_use_count) {
2326 DBG(3, "VIDIOC_S_CROP failed. "
2327 "Unmap the buffers first.");
2331 /* Preserve R,G or B origin */
2332 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
2333 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
2335 if (rect->width < 16)
2337 if (rect->height < 16)
2339 if (rect->width > bounds->width)
2340 rect->width = bounds->width;
2341 if (rect->height > bounds->height)
2342 rect->height = bounds->height;
2343 if (rect->left < bounds->left)
2344 rect->left = bounds->left;
2345 if (rect->top < bounds->top)
2346 rect->top = bounds->top;
2347 if (rect->left + rect->width > bounds->left + bounds->width)
2348 rect->left = bounds->left+bounds->width - rect->width;
2349 if (rect->top + rect->height > bounds->top + bounds->height)
2350 rect->top = bounds->top+bounds->height - rect->height;
2352 rect->width &= ~15L;
2353 rect->height &= ~15L;
2355 if (SN9C102_PRESERVE_IMGSCALE) {
2356 /* Calculate the actual scaling factor */
2358 a = rect->width * rect->height;
2359 b = pix_format->width * pix_format->height;
2360 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2364 if (cam->stream == STREAM_ON)
2365 if ((err = sn9c102_stream_interrupt(cam)))
2368 if (copy_to_user(arg, &crop, sizeof(crop))) {
2369 cam->stream = stream;
2373 if (cam->module_param.force_munmap || cam->io == IO_READ)
2374 sn9c102_release_buffers(cam);
2376 err = sn9c102_set_crop(cam, rect);
2378 err += s->set_crop(cam, rect);
2379 err += sn9c102_set_scale(cam, scale);
2381 if (err) { /* atomic, no rollback in ioctl() */
2382 cam->state |= DEV_MISCONFIGURED;
2383 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2384 "use the camera, close and open /dev/video%d again.",
2385 cam->v4ldev->minor);
2389 s->pix_format.width = rect->width/scale;
2390 s->pix_format.height = rect->height/scale;
2391 memcpy(&(s->_rect), rect, sizeof(*rect));
2393 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2394 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2395 cam->state |= DEV_MISCONFIGURED;
2396 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2397 "use the camera, close and open /dev/video%d again.",
2398 cam->v4ldev->minor);
2402 if (cam->io == IO_READ)
2403 sn9c102_empty_framequeues(cam);
2404 else if (cam->module_param.force_munmap)
2405 sn9c102_requeue_outqueue(cam);
2407 cam->stream = stream;
2414 sn9c102_vidioc_enum_framesizes(struct sn9c102_device* cam, void __user * arg)
2416 struct v4l2_frmsizeenum frmsize;
2418 if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
2421 if (frmsize.index != 0)
2424 switch (cam->bridge) {
2425 case BRIDGE_SN9C101:
2426 case BRIDGE_SN9C102:
2427 case BRIDGE_SN9C103:
2428 if (frmsize.pixel_format != V4L2_PIX_FMT_SN9C10X &&
2429 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
2431 case BRIDGE_SN9C105:
2432 case BRIDGE_SN9C120:
2433 if (frmsize.pixel_format != V4L2_PIX_FMT_JPEG &&
2434 frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
2438 frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
2439 frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
2440 frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
2441 frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
2442 frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
2443 memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));
2445 if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
2453 sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
2455 struct v4l2_fmtdesc fmtd;
2457 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
2460 if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2463 if (fmtd.index == 0) {
2464 strcpy(fmtd.description, "bayer rgb");
2465 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2466 } else if (fmtd.index == 1) {
2467 switch (cam->bridge) {
2468 case BRIDGE_SN9C101:
2469 case BRIDGE_SN9C102:
2470 case BRIDGE_SN9C103:
2471 strcpy(fmtd.description, "compressed");
2472 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2474 case BRIDGE_SN9C105:
2475 case BRIDGE_SN9C120:
2476 strcpy(fmtd.description, "JPEG");
2477 fmtd.pixelformat = V4L2_PIX_FMT_JPEG;
2480 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2484 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2485 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2487 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
2495 sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2497 struct v4l2_format format;
2498 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
2500 if (copy_from_user(&format, arg, sizeof(format)))
2503 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2506 pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_JPEG) ?
2507 V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB;
2508 pfmt->bytesperline = (pfmt->pixelformat == V4L2_PIX_FMT_SN9C10X ||
2509 pfmt->pixelformat == V4L2_PIX_FMT_JPEG)
2510 ? 0 : (pfmt->width * pfmt->priv) / 8;
2511 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2512 pfmt->field = V4L2_FIELD_NONE;
2513 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2515 if (copy_to_user(arg, &format, sizeof(format)))
2523 sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2526 struct sn9c102_sensor* s = &cam->sensor;
2527 struct v4l2_format format;
2528 struct v4l2_pix_format* pix;
2529 struct v4l2_pix_format* pfmt = &(s->pix_format);
2530 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2531 struct v4l2_rect rect;
2533 const enum sn9c102_stream_state stream = cam->stream;
2534 const u32 nbuffers = cam->nbuffers;
2538 if (copy_from_user(&format, arg, sizeof(format)))
2541 pix = &(format.fmt.pix);
2543 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2546 memcpy(&rect, &(s->_rect), sizeof(rect));
2548 { /* calculate the actual scaling factor */
2550 a = rect.width * rect.height;
2551 b = pix->width * pix->height;
2552 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2555 rect.width = scale * pix->width;
2556 rect.height = scale * pix->height;
2558 if (rect.width < 16)
2560 if (rect.height < 16)
2562 if (rect.width > bounds->left + bounds->width - rect.left)
2563 rect.width = bounds->left + bounds->width - rect.left;
2564 if (rect.height > bounds->top + bounds->height - rect.top)
2565 rect.height = bounds->top + bounds->height - rect.top;
2568 rect.height &= ~15L;
2570 { /* adjust the scaling factor */
2572 a = rect.width * rect.height;
2573 b = pix->width * pix->height;
2574 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2577 pix->width = rect.width / scale;
2578 pix->height = rect.height / scale;
2580 switch (cam->bridge) {
2581 case BRIDGE_SN9C101:
2582 case BRIDGE_SN9C102:
2583 case BRIDGE_SN9C103:
2584 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2585 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2586 pix->pixelformat = pfmt->pixelformat;
2588 case BRIDGE_SN9C105:
2589 case BRIDGE_SN9C120:
2590 if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
2591 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2592 pix->pixelformat = pfmt->pixelformat;
2595 pix->priv = pfmt->priv; /* bpp */
2596 pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_JPEG) ?
2597 V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB;
2598 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
2599 pix->pixelformat == V4L2_PIX_FMT_JPEG)
2600 ? 0 : (pix->width * pix->priv) / 8;
2601 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2602 pix->field = V4L2_FIELD_NONE;
2604 if (cmd == VIDIOC_TRY_FMT) {
2605 if (copy_to_user(arg, &format, sizeof(format)))
2610 if (cam->module_param.force_munmap)
2611 for (i = 0; i < cam->nbuffers; i++)
2612 if (cam->frame[i].vma_use_count) {
2613 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2618 if (cam->stream == STREAM_ON)
2619 if ((err = sn9c102_stream_interrupt(cam)))
2622 if (copy_to_user(arg, &format, sizeof(format))) {
2623 cam->stream = stream;
2627 if (cam->module_param.force_munmap || cam->io == IO_READ)
2628 sn9c102_release_buffers(cam);
2630 err += sn9c102_set_pix_format(cam, pix);
2631 err += sn9c102_set_crop(cam, &rect);
2632 if (s->set_pix_format)
2633 err += s->set_pix_format(cam, pix);
2635 err += s->set_crop(cam, &rect);
2636 err += sn9c102_set_scale(cam, scale);
2638 if (err) { /* atomic, no rollback in ioctl() */
2639 cam->state |= DEV_MISCONFIGURED;
2640 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2641 "use the camera, close and open /dev/video%d again.",
2642 cam->v4ldev->minor);
2646 memcpy(pfmt, pix, sizeof(*pix));
2647 memcpy(&(s->_rect), &rect, sizeof(rect));
2649 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2650 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2651 cam->state |= DEV_MISCONFIGURED;
2652 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2653 "use the camera, close and open /dev/video%d again.",
2654 cam->v4ldev->minor);
2658 if (cam->io == IO_READ)
2659 sn9c102_empty_framequeues(cam);
2660 else if (cam->module_param.force_munmap)
2661 sn9c102_requeue_outqueue(cam);
2663 cam->stream = stream;
2670 sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2672 if (copy_to_user(arg, &cam->compression, sizeof(cam->compression)))
2680 sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2682 struct v4l2_jpegcompression jc;
2683 const enum sn9c102_stream_state stream = cam->stream;
2686 if (copy_from_user(&jc, arg, sizeof(jc)))
2689 if (jc.quality != 0 && jc.quality != 1)
2692 if (cam->stream == STREAM_ON)
2693 if ((err = sn9c102_stream_interrupt(cam)))
2696 err += sn9c102_set_compression(cam, &jc);
2697 if (err) { /* atomic, no rollback in ioctl() */
2698 cam->state |= DEV_MISCONFIGURED;
2699 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2700 "problems. To use the camera, close and open "
2701 "/dev/video%d again.", cam->v4ldev->minor);
2705 cam->compression.quality = jc.quality;
2707 cam->stream = stream;
2714 sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
2716 struct v4l2_requestbuffers rb;
2720 if (copy_from_user(&rb, arg, sizeof(rb)))
2723 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2724 rb.memory != V4L2_MEMORY_MMAP)
2727 if (cam->io == IO_READ) {
2728 DBG(3, "Close and open the device again to choose the mmap "
2733 for (i = 0; i < cam->nbuffers; i++)
2734 if (cam->frame[i].vma_use_count) {
2735 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2740 if (cam->stream == STREAM_ON)
2741 if ((err = sn9c102_stream_interrupt(cam)))
2744 sn9c102_empty_framequeues(cam);
2746 sn9c102_release_buffers(cam);
2748 rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
2750 if (copy_to_user(arg, &rb, sizeof(rb))) {
2751 sn9c102_release_buffers(cam);
2756 cam->io = rb.count ? IO_MMAP : IO_NONE;
2763 sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
2765 struct v4l2_buffer b;
2767 if (copy_from_user(&b, arg, sizeof(b)))
2770 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2771 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2774 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2776 if (cam->frame[b.index].vma_use_count)
2777 b.flags |= V4L2_BUF_FLAG_MAPPED;
2779 if (cam->frame[b.index].state == F_DONE)
2780 b.flags |= V4L2_BUF_FLAG_DONE;
2781 else if (cam->frame[b.index].state != F_UNUSED)
2782 b.flags |= V4L2_BUF_FLAG_QUEUED;
2784 if (copy_to_user(arg, &b, sizeof(b)))
2792 sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
2794 struct v4l2_buffer b;
2795 unsigned long lock_flags;
2797 if (copy_from_user(&b, arg, sizeof(b)))
2800 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2801 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2804 if (cam->frame[b.index].state != F_UNUSED)
2807 cam->frame[b.index].state = F_QUEUED;
2809 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2810 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2811 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2813 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2820 sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2823 struct v4l2_buffer b;
2824 struct sn9c102_frame_t *f;
2825 unsigned long lock_flags;
2829 if (copy_from_user(&b, arg, sizeof(b)))
2832 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2835 if (list_empty(&cam->outqueue)) {
2836 if (cam->stream == STREAM_OFF)
2838 if (filp->f_flags & O_NONBLOCK)
2840 if (!cam->module_param.frame_timeout) {
2841 err = wait_event_interruptible
2843 (!list_empty(&cam->outqueue)) ||
2844 (cam->state & DEV_DISCONNECTED) ||
2845 (cam->state & DEV_MISCONFIGURED) );
2849 timeout = wait_event_interruptible_timeout
2851 (!list_empty(&cam->outqueue)) ||
2852 (cam->state & DEV_DISCONNECTED) ||
2853 (cam->state & DEV_MISCONFIGURED),
2854 cam->module_param.frame_timeout *
2855 1000 * msecs_to_jiffies(1) );
2858 else if (timeout == 0 &&
2859 !(cam->state & DEV_DISCONNECTED)) {
2860 DBG(1, "Video frame timeout elapsed");
2864 if (cam->state & DEV_DISCONNECTED)
2866 if (cam->state & DEV_MISCONFIGURED)
2870 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2871 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
2872 list_del(cam->outqueue.next);
2873 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2875 f->state = F_UNUSED;
2877 memcpy(&b, &f->buf, sizeof(b));
2878 if (f->vma_use_count)
2879 b.flags |= V4L2_BUF_FLAG_MAPPED;
2881 if (copy_to_user(arg, &b, sizeof(b)))
2884 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2891 sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
2895 if (copy_from_user(&type, arg, sizeof(type)))
2898 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2901 cam->stream = STREAM_ON;
2903 DBG(3, "Stream on");
2910 sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
2914 if (copy_from_user(&type, arg, sizeof(type)))
2917 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2920 if (cam->stream == STREAM_ON)
2921 if ((err = sn9c102_stream_interrupt(cam)))
2924 sn9c102_empty_framequeues(cam);
2926 DBG(3, "Stream off");
2933 sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
2935 struct v4l2_streamparm sp;
2937 if (copy_from_user(&sp, arg, sizeof(sp)))
2940 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2943 sp.parm.capture.extendedmode = 0;
2944 sp.parm.capture.readbuffers = cam->nreadbuffers;
2946 if (copy_to_user(arg, &sp, sizeof(sp)))
2954 sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
2956 struct v4l2_streamparm sp;
2958 if (copy_from_user(&sp, arg, sizeof(sp)))
2961 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2964 sp.parm.capture.extendedmode = 0;
2966 if (sp.parm.capture.readbuffers == 0)
2967 sp.parm.capture.readbuffers = cam->nreadbuffers;
2969 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2970 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2972 if (copy_to_user(arg, &sp, sizeof(sp)))
2975 cam->nreadbuffers = sp.parm.capture.readbuffers;
2982 sn9c102_vidioc_enumaudio(struct sn9c102_device* cam, void __user * arg)
2984 struct v4l2_audio audio;
2986 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
2989 if (copy_from_user(&audio, arg, sizeof(audio)))
2992 if (audio.index != 0)
2995 strcpy(audio.name, "Microphone");
2996 audio.capability = 0;
2999 if (copy_to_user(arg, &audio, sizeof(audio)))
3007 sn9c102_vidioc_g_audio(struct sn9c102_device* cam, void __user * arg)
3009 struct v4l2_audio audio;
3011 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
3014 if (copy_from_user(&audio, arg, sizeof(audio)))
3017 memset(&audio, 0, sizeof(audio));
3018 strcpy(audio.name, "Microphone");
3020 if (copy_to_user(arg, &audio, sizeof(audio)))
3028 sn9c102_vidioc_s_audio(struct sn9c102_device* cam, void __user * arg)
3030 struct v4l2_audio audio;
3032 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
3035 if (copy_from_user(&audio, arg, sizeof(audio)))
3038 if (audio.index != 0)
3045 static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
3046 unsigned int cmd, void __user * arg)
3048 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
3052 case VIDIOC_QUERYCAP:
3053 return sn9c102_vidioc_querycap(cam, arg);
3055 case VIDIOC_ENUMINPUT:
3056 return sn9c102_vidioc_enuminput(cam, arg);
3058 case VIDIOC_G_INPUT:
3059 return sn9c102_vidioc_g_input(cam, arg);
3061 case VIDIOC_S_INPUT:
3062 return sn9c102_vidioc_s_input(cam, arg);
3064 case VIDIOC_QUERYCTRL:
3065 return sn9c102_vidioc_query_ctrl(cam, arg);
3068 return sn9c102_vidioc_g_ctrl(cam, arg);
3071 return sn9c102_vidioc_s_ctrl(cam, arg);
3073 case VIDIOC_CROPCAP:
3074 return sn9c102_vidioc_cropcap(cam, arg);
3077 return sn9c102_vidioc_g_crop(cam, arg);
3080 return sn9c102_vidioc_s_crop(cam, arg);
3082 case VIDIOC_ENUM_FRAMESIZES:
3083 return sn9c102_vidioc_enum_framesizes(cam, arg);
3085 case VIDIOC_ENUM_FMT:
3086 return sn9c102_vidioc_enum_fmt(cam, arg);
3089 return sn9c102_vidioc_g_fmt(cam, arg);
3091 case VIDIOC_TRY_FMT:
3093 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
3095 case VIDIOC_G_JPEGCOMP:
3096 return sn9c102_vidioc_g_jpegcomp(cam, arg);
3098 case VIDIOC_S_JPEGCOMP:
3099 return sn9c102_vidioc_s_jpegcomp(cam, arg);
3101 case VIDIOC_REQBUFS:
3102 return sn9c102_vidioc_reqbufs(cam, arg);
3104 case VIDIOC_QUERYBUF:
3105 return sn9c102_vidioc_querybuf(cam, arg);
3108 return sn9c102_vidioc_qbuf(cam, arg);
3111 return sn9c102_vidioc_dqbuf(cam, filp, arg);
3113 case VIDIOC_STREAMON:
3114 return sn9c102_vidioc_streamon(cam, arg);
3116 case VIDIOC_STREAMOFF:
3117 return sn9c102_vidioc_streamoff(cam, arg);
3120 return sn9c102_vidioc_g_parm(cam, arg);
3123 return sn9c102_vidioc_s_parm(cam, arg);
3125 case VIDIOC_ENUMAUDIO:
3126 return sn9c102_vidioc_enumaudio(cam, arg);
3128 case VIDIOC_G_AUDIO:
3129 return sn9c102_vidioc_g_audio(cam, arg);
3131 case VIDIOC_S_AUDIO:
3132 return sn9c102_vidioc_s_audio(cam, arg);
3136 case VIDIOC_QUERYSTD:
3137 case VIDIOC_ENUMSTD:
3138 case VIDIOC_QUERYMENU:
3139 case VIDIOC_ENUM_FRAMEINTERVALS:
3149 static int sn9c102_ioctl(struct inode* inode, struct file* filp,
3150 unsigned int cmd, unsigned long arg)
3152 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
3155 if (mutex_lock_interruptible(&cam->fileop_mutex))
3156 return -ERESTARTSYS;
3158 if (cam->state & DEV_DISCONNECTED) {
3159 DBG(1, "Device not present");
3160 mutex_unlock(&cam->fileop_mutex);
3164 if (cam->state & DEV_MISCONFIGURED) {
3165 DBG(1, "The camera is misconfigured. Close and open it "
3167 mutex_unlock(&cam->fileop_mutex);
3171 V4LDBG(3, "sn9c102", cmd);
3173 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
3175 mutex_unlock(&cam->fileop_mutex);
3180 /*****************************************************************************/
3182 static const struct file_operations sn9c102_fops = {
3183 .owner = THIS_MODULE,
3184 .open = sn9c102_open,
3185 .release = sn9c102_release,
3186 .ioctl = sn9c102_ioctl,
3187 .compat_ioctl = v4l_compat_ioctl32,
3188 .read = sn9c102_read,
3189 .poll = sn9c102_poll,
3190 .mmap = sn9c102_mmap,
3191 .llseek = no_llseek,
3194 /*****************************************************************************/
3196 /* It exists a single interface only. We do not need to validate anything. */
3198 sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3200 struct usb_device *udev = interface_to_usbdev(intf);
3201 struct sn9c102_device* cam;
3202 static unsigned int dev_nr = 0;
3206 if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
3211 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
3212 DBG(1, "kzalloc() failed");
3217 if (!(cam->v4ldev = video_device_alloc())) {
3218 DBG(1, "video_device_alloc() failed");
3223 mutex_init(&cam->dev_mutex);
3225 r = sn9c102_read_reg(cam, 0x00);
3226 if (r < 0 || (r != 0x10 && r != 0x11 && r != 0x12)) {
3227 DBG(1, "Sorry, this is not a SN9C1xx-based camera "
3228 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3233 cam->bridge = id->driver_info;
3234 switch (cam->bridge) {
3235 case BRIDGE_SN9C101:
3236 case BRIDGE_SN9C102:
3237 DBG(2, "SN9C10[12] PC Camera Controller detected "
3238 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3240 case BRIDGE_SN9C103:
3241 DBG(2, "SN9C103 PC Camera Controller detected "
3242 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3244 case BRIDGE_SN9C105:
3245 DBG(2, "SN9C105 PC Camera Controller detected "
3246 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3248 case BRIDGE_SN9C120:
3249 DBG(2, "SN9C120 PC Camera Controller detected "
3250 "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3254 for (i = 0; sn9c102_sensor_table[i]; i++) {
3255 err = sn9c102_sensor_table[i](cam);
3261 DBG(2, "%s image sensor detected", cam->sensor.name);
3262 DBG(3, "Support for %s maintained by %s",
3263 cam->sensor.name, cam->sensor.maintainer);
3265 DBG(1, "No supported image sensor detected");
3270 if (!(cam->bridge & cam->sensor.supported_bridge)) {
3271 DBG(1, "Bridge not supported");
3276 if (sn9c102_init(cam)) {
3277 DBG(1, "Initialization failed. I will retry on open().");
3278 cam->state |= DEV_MISCONFIGURED;
3281 strcpy(cam->v4ldev->name, "SN9C1xx PC Camera");
3282 cam->v4ldev->owner = THIS_MODULE;
3283 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
3284 cam->v4ldev->hardware = 0;
3285 cam->v4ldev->fops = &sn9c102_fops;
3286 cam->v4ldev->minor = video_nr[dev_nr];
3287 cam->v4ldev->release = video_device_release;
3288 video_set_drvdata(cam->v4ldev, cam);
3290 mutex_lock(&cam->dev_mutex);
3292 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
3295 DBG(1, "V4L2 device registration failed");
3296 if (err == -ENFILE && video_nr[dev_nr] == -1)
3297 DBG(1, "Free /dev/videoX node not found");
3298 video_nr[dev_nr] = -1;
3299 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
3300 mutex_unlock(&cam->dev_mutex);
3304 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
3306 cam->module_param.force_munmap = force_munmap[dev_nr];
3307 cam->module_param.frame_timeout = frame_timeout[dev_nr];
3309 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
3311 #ifdef CONFIG_VIDEO_ADV_DEBUG
3312 err = sn9c102_create_sysfs(cam);
3314 DBG(2, "Optional device control through 'sysfs' "
3317 DBG(2, "Failed to create optional 'sysfs' interface for "
3318 "device controlling. Error #%d", err);
3320 DBG(2, "Optional device control through 'sysfs' interface disabled");
3321 DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
3322 "configuration option to enable it.");
3325 usb_set_intfdata(intf, cam);
3327 mutex_unlock(&cam->dev_mutex);
3333 kfree(cam->control_buffer);
3335 video_device_release(cam->v4ldev);
3342 static void sn9c102_usb_disconnect(struct usb_interface* intf)
3344 struct sn9c102_device* cam = usb_get_intfdata(intf);
3349 down_write(&sn9c102_disconnect);
3351 mutex_lock(&cam->dev_mutex);
3353 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
3355 wake_up_interruptible_all(&cam->open);
3358 DBG(2, "Device /dev/video%d is open! Deregistration and "
3359 "memory deallocation are deferred on close.",
3360 cam->v4ldev->minor);
3361 cam->state |= DEV_MISCONFIGURED;
3362 sn9c102_stop_transfer(cam);
3363 cam->state |= DEV_DISCONNECTED;
3364 wake_up_interruptible(&cam->wait_frame);
3365 wake_up(&cam->wait_stream);
3366 usb_get_dev(cam->usbdev);
3368 cam->state |= DEV_DISCONNECTED;
3369 sn9c102_release_resources(cam);
3372 mutex_unlock(&cam->dev_mutex);
3377 up_write(&sn9c102_disconnect);
3381 static struct usb_driver sn9c102_usb_driver = {
3383 .id_table = sn9c102_id_table,
3384 .probe = sn9c102_usb_probe,
3385 .disconnect = sn9c102_usb_disconnect,
3388 /*****************************************************************************/
3390 static int __init sn9c102_module_init(void)
3394 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
3395 KDBG(3, SN9C102_MODULE_AUTHOR);
3397 if ((err = usb_register(&sn9c102_usb_driver)))
3398 KDBG(1, "usb_register() failed");
3404 static void __exit sn9c102_module_exit(void)
3406 usb_deregister(&sn9c102_usb_driver);
3410 module_init(sn9c102_module_init);
3411 module_exit(sn9c102_module_exit);