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/errno.h>
26 #include <linux/slab.h>
27 #include <linux/device.h>
29 #include <linux/delay.h>
30 #include <linux/compiler.h>
31 #include <linux/ioctl.h>
32 #include <linux/poll.h>
33 #include <linux/stat.h>
35 #include <linux/vmalloc.h>
36 #include <linux/page-flags.h>
37 #include <asm/byteorder.h>
39 #include <asm/uaccess.h>
43 /*****************************************************************************/
45 #define SN9C102_MODULE_NAME "V4L2 driver for SN9C1xx PC Camera Controllers"
46 #define SN9C102_MODULE_ALIAS "sn9c1xx"
47 #define SN9C102_MODULE_AUTHOR "(C) 2004-2007 Luca Risolia"
48 #define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
49 #define SN9C102_MODULE_LICENSE "GPL"
50 #define SN9C102_MODULE_VERSION "1:1.47pre49"
51 #define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 47)
53 /*****************************************************************************/
55 MODULE_DEVICE_TABLE(usb
, sn9c102_id_table
);
57 MODULE_AUTHOR(SN9C102_MODULE_AUTHOR
" " SN9C102_AUTHOR_EMAIL
);
58 MODULE_DESCRIPTION(SN9C102_MODULE_NAME
);
59 MODULE_ALIAS(SN9C102_MODULE_ALIAS
);
60 MODULE_VERSION(SN9C102_MODULE_VERSION
);
61 MODULE_LICENSE(SN9C102_MODULE_LICENSE
);
63 static short video_nr
[] = {[0 ... SN9C102_MAX_DEVICES
-1] = -1};
64 module_param_array(video_nr
, short, NULL
, 0444);
65 MODULE_PARM_DESC(video_nr
,
67 "\nSpecify 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
,
83 "\nForce the application to unmap previously"
84 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
85 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
86 "\nthis feature. This parameter is specific for each"
88 "\n0 = do not force memory unmapping"
89 "\n1 = force memory unmapping (save memory)"
90 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP
)"."
93 static unsigned int frame_timeout
[] = {[0 ... SN9C102_MAX_DEVICES
-1] =
94 SN9C102_FRAME_TIMEOUT
};
95 module_param_array(frame_timeout
, uint
, NULL
, 0644);
96 MODULE_PARM_DESC(frame_timeout
,
98 "\nTimeout for a video frame in seconds before"
99 "\nreturning an I/O error; 0 for infinity."
100 "\nThis parameter is specific for each detected camera."
101 "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT
)"."
105 static unsigned short debug
= SN9C102_DEBUG_LEVEL
;
106 module_param(debug
, ushort
, 0644);
107 MODULE_PARM_DESC(debug
,
109 "\nDebugging information level, from 0 to 3:"
110 "\n0 = none (use carefully)"
111 "\n1 = critical errors"
112 "\n2 = significant informations"
113 "\n3 = more verbose messages"
114 "\nLevel 3 is useful for testing only."
115 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL
)"."
120 Add the probe entries to this table. Be sure to add the entry in the right
121 place, since, on failure, the next probing routine is called according to
122 the order of the list below, from top to bottom.
124 static int (*sn9c102_sensor_table
[])(struct sn9c102_device
*) = {
125 &sn9c102_probe_hv7131d
, /* strong detection based on SENSOR ids */
126 &sn9c102_probe_hv7131r
, /* strong detection based on SENSOR ids */
127 &sn9c102_probe_mi0343
, /* strong detection based on SENSOR ids */
128 &sn9c102_probe_mi0360
, /* strong detection based on SENSOR ids */
129 &sn9c102_probe_mt9v111
, /* strong detection based on SENSOR ids */
130 &sn9c102_probe_pas106b
, /* strong detection based on SENSOR ids */
131 &sn9c102_probe_pas202bcb
, /* strong detection based on SENSOR ids */
132 &sn9c102_probe_ov7630
, /* strong detection based on SENSOR ids */
133 &sn9c102_probe_ov7660
, /* strong detection based on SENSOR ids */
134 &sn9c102_probe_tas5110c1b
, /* detection based on USB pid/vid */
135 &sn9c102_probe_tas5110d
, /* detection based on USB pid/vid */
136 &sn9c102_probe_tas5130d1b
, /* detection based on USB pid/vid */
139 /*****************************************************************************/
142 sn9c102_request_buffers(struct sn9c102_device
* cam
, u32 count
,
143 enum sn9c102_io_method io
)
145 struct v4l2_pix_format
* p
= &(cam
->sensor
.pix_format
);
146 struct v4l2_rect
* r
= &(cam
->sensor
.cropcap
.bounds
);
147 size_t imagesize
= cam
->module_param
.force_munmap
|| io
== IO_READ
?
148 (p
->width
* p
->height
* p
->priv
) / 8 :
149 (r
->width
* r
->height
* p
->priv
) / 8;
153 if (count
> SN9C102_MAX_FRAMES
)
154 count
= SN9C102_MAX_FRAMES
;
156 if (cam
->bridge
== BRIDGE_SN9C105
|| cam
->bridge
== BRIDGE_SN9C120
)
157 imagesize
+= 589 + 2; /* length of JPEG header + EOI marker */
159 cam
->nbuffers
= count
;
160 while (cam
->nbuffers
> 0) {
161 if ((buff
= vmalloc_32_user(cam
->nbuffers
*
162 PAGE_ALIGN(imagesize
))))
167 for (i
= 0; i
< cam
->nbuffers
; i
++) {
168 cam
->frame
[i
].bufmem
= buff
+ i
*PAGE_ALIGN(imagesize
);
169 cam
->frame
[i
].buf
.index
= i
;
170 cam
->frame
[i
].buf
.m
.offset
= i
*PAGE_ALIGN(imagesize
);
171 cam
->frame
[i
].buf
.length
= imagesize
;
172 cam
->frame
[i
].buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
173 cam
->frame
[i
].buf
.sequence
= 0;
174 cam
->frame
[i
].buf
.field
= V4L2_FIELD_NONE
;
175 cam
->frame
[i
].buf
.memory
= V4L2_MEMORY_MMAP
;
176 cam
->frame
[i
].buf
.flags
= 0;
179 return cam
->nbuffers
;
183 static void sn9c102_release_buffers(struct sn9c102_device
* cam
)
186 vfree(cam
->frame
[0].bufmem
);
189 cam
->frame_current
= NULL
;
193 static void sn9c102_empty_framequeues(struct sn9c102_device
* cam
)
197 INIT_LIST_HEAD(&cam
->inqueue
);
198 INIT_LIST_HEAD(&cam
->outqueue
);
200 for (i
= 0; i
< SN9C102_MAX_FRAMES
; i
++) {
201 cam
->frame
[i
].state
= F_UNUSED
;
202 cam
->frame
[i
].buf
.bytesused
= 0;
207 static void sn9c102_requeue_outqueue(struct sn9c102_device
* cam
)
209 struct sn9c102_frame_t
*i
;
211 list_for_each_entry(i
, &cam
->outqueue
, frame
) {
213 list_add(&i
->frame
, &cam
->inqueue
);
216 INIT_LIST_HEAD(&cam
->outqueue
);
220 static void sn9c102_queue_unusedframes(struct sn9c102_device
* cam
)
222 unsigned long lock_flags
;
225 for (i
= 0; i
< cam
->nbuffers
; i
++)
226 if (cam
->frame
[i
].state
== F_UNUSED
) {
227 cam
->frame
[i
].state
= F_QUEUED
;
228 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
229 list_add_tail(&cam
->frame
[i
].frame
, &cam
->inqueue
);
230 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
234 /*****************************************************************************/
237 Write a sequence of count value/register pairs. Returns -1 after the first
238 failed write, or 0 for no errors.
240 int sn9c102_write_regs(struct sn9c102_device
* cam
, const u8 valreg
[][2],
243 struct usb_device
* udev
= cam
->usbdev
;
244 u8
* buff
= cam
->control_buffer
;
247 for (i
= 0; i
< count
; i
++) {
248 u8 index
= valreg
[i
][1];
251 index is a u8, so it must be <256 and can't be out of range.
252 If we put in a check anyway, gcc annoys us with a warning
253 hat our check is useless. People get all uppity when they
254 see warnings in the kernel compile.
257 *buff
= valreg
[i
][0];
259 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x08,
260 0x41, index
, 0, buff
, 1,
261 SN9C102_CTRL_TIMEOUT
);
264 DBG(3, "Failed to write a register (value 0x%02X, "
265 "index 0x%02X, error %d)", *buff
, index
, res
);
269 cam
->reg
[index
] = *buff
;
276 int sn9c102_write_reg(struct sn9c102_device
* cam
, u8 value
, u16 index
)
278 struct usb_device
* udev
= cam
->usbdev
;
279 u8
* buff
= cam
->control_buffer
;
282 if (index
>= ARRAY_SIZE(cam
->reg
))
287 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x08, 0x41,
288 index
, 0, buff
, 1, SN9C102_CTRL_TIMEOUT
);
290 DBG(3, "Failed to write a register (value 0x%02X, index "
291 "0x%02X, error %d)", value
, index
, res
);
295 cam
->reg
[index
] = value
;
301 /* NOTE: with the SN9C10[123] reading some registers always returns 0 */
302 int sn9c102_read_reg(struct sn9c102_device
* cam
, u16 index
)
304 struct usb_device
* udev
= cam
->usbdev
;
305 u8
* buff
= cam
->control_buffer
;
308 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), 0x00, 0xc1,
309 index
, 0, buff
, 1, SN9C102_CTRL_TIMEOUT
);
311 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
314 return (res
>= 0) ? (int)(*buff
) : -1;
318 int sn9c102_pread_reg(struct sn9c102_device
* cam
, u16 index
)
320 if (index
>= ARRAY_SIZE(cam
->reg
))
323 return cam
->reg
[index
];
328 sn9c102_i2c_wait(struct sn9c102_device
* cam
,
329 const struct sn9c102_sensor
* sensor
)
333 for (i
= 1; i
<= 5; i
++) {
334 r
= sn9c102_read_reg(cam
, 0x08);
339 if (sensor
->frequency
& SN9C102_I2C_400KHZ
)
349 sn9c102_i2c_detect_read_error(struct sn9c102_device
* cam
,
350 const struct sn9c102_sensor
* sensor
)
354 r
= sn9c102_read_reg(cam
, 0x08);
358 if (cam
->bridge
== BRIDGE_SN9C101
|| cam
->bridge
== BRIDGE_SN9C102
) {
366 return err
? -EIO
: 0;
371 sn9c102_i2c_detect_write_error(struct sn9c102_device
* cam
,
372 const struct sn9c102_sensor
* sensor
)
375 r
= sn9c102_read_reg(cam
, 0x08);
376 return (r
< 0 || (r
>= 0 && (r
& 0x08))) ? -EIO
: 0;
381 sn9c102_i2c_try_raw_read(struct sn9c102_device
* cam
,
382 const struct sn9c102_sensor
* sensor
, u8 data0
,
383 u8 data1
, u8 n
, u8 buffer
[])
385 struct usb_device
* udev
= cam
->usbdev
;
386 u8
* data
= cam
->control_buffer
;
387 int i
= 0, err
= 0, res
;
390 data
[0] = ((sensor
->interface
== SN9C102_I2C_2WIRES
) ? 0x80 : 0) |
391 ((sensor
->frequency
& SN9C102_I2C_400KHZ
) ? 0x01 : 0) | 0x10;
392 data
[1] = data0
; /* I2C slave id */
393 data
[2] = data1
; /* address */
395 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x08, 0x41,
396 0x08, 0, data
, 8, SN9C102_CTRL_TIMEOUT
);
400 err
+= sn9c102_i2c_wait(cam
, sensor
);
402 /* Read cycle - n bytes */
403 data
[0] = ((sensor
->interface
== SN9C102_I2C_2WIRES
) ? 0x80 : 0) |
404 ((sensor
->frequency
& SN9C102_I2C_400KHZ
) ? 0x01 : 0) |
408 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x08, 0x41,
409 0x08, 0, data
, 8, SN9C102_CTRL_TIMEOUT
);
413 err
+= sn9c102_i2c_wait(cam
, sensor
);
415 /* The first read byte will be placed in data[4] */
416 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), 0x00, 0xc1,
417 0x0a, 0, data
, 5, SN9C102_CTRL_TIMEOUT
);
421 err
+= sn9c102_i2c_detect_read_error(cam
, sensor
);
423 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1
,
427 DBG(3, "I2C read failed for %s image sensor", sensor
->name
);
432 for (i
= 0; i
< n
&& i
< 5; i
++)
433 buffer
[n
-i
-1] = data
[4-i
];
440 sn9c102_i2c_try_raw_write(struct sn9c102_device
* cam
,
441 const struct sn9c102_sensor
* sensor
, u8 n
, u8 data0
,
442 u8 data1
, u8 data2
, u8 data3
, u8 data4
, u8 data5
)
444 struct usb_device
* udev
= cam
->usbdev
;
445 u8
* data
= cam
->control_buffer
;
448 /* Write cycle. It usually is address + value */
449 data
[0] = ((sensor
->interface
== SN9C102_I2C_2WIRES
) ? 0x80 : 0) |
450 ((sensor
->frequency
& SN9C102_I2C_400KHZ
) ? 0x01 : 0)
459 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x08, 0x41,
460 0x08, 0, data
, 8, SN9C102_CTRL_TIMEOUT
);
464 err
+= sn9c102_i2c_wait(cam
, sensor
);
465 err
+= sn9c102_i2c_detect_write_error(cam
, sensor
);
468 DBG(3, "I2C write failed for %s image sensor", sensor
->name
);
470 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
471 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
472 n
, data0
, data1
, data2
, data3
, data4
, data5
);
479 sn9c102_i2c_try_read(struct sn9c102_device
* cam
,
480 const struct sn9c102_sensor
* sensor
, u8 address
)
482 return sn9c102_i2c_try_raw_read(cam
, sensor
, sensor
->i2c_slave_id
,
487 static int sn9c102_i2c_try_write(struct sn9c102_device
* cam
,
488 const struct sn9c102_sensor
* sensor
,
489 u8 address
, u8 value
)
491 return sn9c102_i2c_try_raw_write(cam
, sensor
, 3,
492 sensor
->i2c_slave_id
, address
,
497 int sn9c102_i2c_read(struct sn9c102_device
* cam
, u8 address
)
499 return sn9c102_i2c_try_read(cam
, &cam
->sensor
, address
);
503 int sn9c102_i2c_write(struct sn9c102_device
* cam
, u8 address
, u8 value
)
505 return sn9c102_i2c_try_write(cam
, &cam
->sensor
, address
, value
);
508 /*****************************************************************************/
510 static size_t sn9c102_sof_length(struct sn9c102_device
* cam
)
512 switch (cam
->bridge
) {
528 sn9c102_find_sof_header(struct sn9c102_device
* cam
, void* mem
, size_t len
)
530 static const char marker
[6] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
532 size_t soflen
= 0, i
, j
;
534 soflen
= sn9c102_sof_length(cam
);
536 for (i
= 0; i
< len
; i
++) {
539 /* Read the variable part of the header */
540 if (unlikely(cam
->sof
.bytesread
>= sizeof(marker
))) {
541 cam
->sof
.header
[cam
->sof
.bytesread
] = *(m
+i
);
542 if (++cam
->sof
.bytesread
== soflen
) {
543 cam
->sof
.bytesread
= 0;
549 /* Search for the SOF marker (fixed part) in the header */
550 for (j
= 0, b
=cam
->sof
.bytesread
; j
+b
< sizeof(marker
); j
++) {
551 if (unlikely(i
+j
== len
))
553 if (*(m
+i
+j
) == marker
[cam
->sof
.bytesread
]) {
554 cam
->sof
.header
[cam
->sof
.bytesread
] = *(m
+i
+j
);
555 if (++cam
->sof
.bytesread
== sizeof(marker
)) {
556 PDBGG("Bytes to analyze: %zd. SOF "
557 "starts at byte #%zd", len
, i
);
562 cam
->sof
.bytesread
= 0;
573 sn9c102_find_eof_header(struct sn9c102_device
* cam
, void* mem
, size_t len
)
575 static const u8 eof_header
[4][4] = {
576 {0x00, 0x00, 0x00, 0x00},
577 {0x40, 0x00, 0x00, 0x00},
578 {0x80, 0x00, 0x00, 0x00},
579 {0xc0, 0x00, 0x00, 0x00},
583 /* The EOF header does not exist in compressed data */
584 if (cam
->sensor
.pix_format
.pixelformat
== V4L2_PIX_FMT_SN9C10X
||
585 cam
->sensor
.pix_format
.pixelformat
== V4L2_PIX_FMT_JPEG
)
589 The EOF header might cross the packet boundary, but this is not a
590 problem, since the end of a frame is determined by checking its size
593 for (i
= 0; (len
>= 4) && (i
<= len
- 4); i
++)
594 for (j
= 0; j
< ARRAY_SIZE(eof_header
); j
++)
595 if (!memcmp(mem
+ i
, eof_header
[j
], 4))
603 sn9c102_write_jpegheader(struct sn9c102_device
* cam
, struct sn9c102_frame_t
* f
)
605 static const u8 jpeg_header
[589] = {
606 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00, 0x06, 0x04, 0x05,
607 0x06, 0x05, 0x04, 0x06, 0x06, 0x05, 0x06, 0x07, 0x07, 0x06,
608 0x08, 0x0a, 0x10, 0x0a, 0x0a, 0x09, 0x09, 0x0a, 0x14, 0x0e,
609 0x0f, 0x0c, 0x10, 0x17, 0x14, 0x18, 0x18, 0x17, 0x14, 0x16,
610 0x16, 0x1a, 0x1d, 0x25, 0x1f, 0x1a, 0x1b, 0x23, 0x1c, 0x16,
611 0x16, 0x20, 0x2c, 0x20, 0x23, 0x26, 0x27, 0x29, 0x2a, 0x29,
612 0x19, 0x1f, 0x2d, 0x30, 0x2d, 0x28, 0x30, 0x25, 0x28, 0x29,
613 0x28, 0x01, 0x07, 0x07, 0x07, 0x0a, 0x08, 0x0a, 0x13, 0x0a,
614 0x0a, 0x13, 0x28, 0x1a, 0x16, 0x1a, 0x28, 0x28, 0x28, 0x28,
615 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
616 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
617 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
618 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
619 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0xff, 0xc4, 0x01, 0xa2,
620 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
621 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
622 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01,
623 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
624 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
625 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10, 0x00,
626 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,
627 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04,
628 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
629 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,
630 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62,
631 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,
632 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38,
633 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
634 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
635 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
636 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
637 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
638 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
639 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
640 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3,
641 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3,
642 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3,
643 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02,
644 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04,
645 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
646 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
647 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1,
648 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
649 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19,
650 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
651 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
652 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
653 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
654 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
655 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
656 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
657 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
658 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
659 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3,
660 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
661 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc0, 0x00, 0x11,
662 0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x01, 0x21, 0x00, 0x02,
663 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03,
664 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00
668 memcpy(pos
, jpeg_header
, sizeof(jpeg_header
));
670 *(pos
+ 7 + 64) = 0x01;
671 if (cam
->compression
.quality
== 0) {
672 memcpy(pos
+ 7, SN9C102_Y_QTABLE0
, 64);
673 memcpy(pos
+ 8 + 64, SN9C102_UV_QTABLE0
, 64);
674 } else if (cam
->compression
.quality
== 1) {
675 memcpy(pos
+ 7, SN9C102_Y_QTABLE1
, 64);
676 memcpy(pos
+ 8 + 64, SN9C102_UV_QTABLE1
, 64);
678 *(pos
+ 564) = cam
->sensor
.pix_format
.width
& 0xFF;
679 *(pos
+ 563) = (cam
->sensor
.pix_format
.width
>> 8) & 0xFF;
680 *(pos
+ 562) = cam
->sensor
.pix_format
.height
& 0xFF;
681 *(pos
+ 561) = (cam
->sensor
.pix_format
.height
>> 8) & 0xFF;
684 f
->buf
.bytesused
+= sizeof(jpeg_header
);
688 static void sn9c102_urb_complete(struct urb
*urb
)
690 struct sn9c102_device
* cam
= urb
->context
;
691 struct sn9c102_frame_t
** f
;
692 size_t imagesize
, soflen
;
696 if (urb
->status
== -ENOENT
)
699 f
= &cam
->frame_current
;
701 if (cam
->stream
== STREAM_INTERRUPT
) {
702 cam
->stream
= STREAM_OFF
;
704 (*f
)->state
= F_QUEUED
;
705 cam
->sof
.bytesread
= 0;
706 DBG(3, "Stream interrupted by application");
707 wake_up(&cam
->wait_stream
);
710 if (cam
->state
& DEV_DISCONNECTED
)
713 if (cam
->state
& DEV_MISCONFIGURED
) {
714 wake_up_interruptible(&cam
->wait_frame
);
718 if (cam
->stream
== STREAM_OFF
|| list_empty(&cam
->inqueue
))
722 (*f
) = list_entry(cam
->inqueue
.next
, struct sn9c102_frame_t
,
725 imagesize
= (cam
->sensor
.pix_format
.width
*
726 cam
->sensor
.pix_format
.height
*
727 cam
->sensor
.pix_format
.priv
) / 8;
728 if (cam
->sensor
.pix_format
.pixelformat
== V4L2_PIX_FMT_JPEG
)
729 imagesize
+= 589; /* length of jpeg header */
730 soflen
= sn9c102_sof_length(cam
);
732 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
733 unsigned int img
, len
, status
;
734 void *pos
, *sof
, *eof
;
736 len
= urb
->iso_frame_desc
[i
].actual_length
;
737 status
= urb
->iso_frame_desc
[i
].status
;
738 pos
= urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
;
741 DBG(3, "Error in isochronous frame");
742 (*f
)->state
= F_ERROR
;
743 cam
->sof
.bytesread
= 0;
747 PDBGG("Isochrnous frame: length %u, #%u i", len
, i
);
750 sof
= sn9c102_find_sof_header(cam
, pos
, len
);
752 eof
= sn9c102_find_eof_header(cam
, pos
, len
);
753 if ((*f
)->state
== F_GRABBING
) {
758 img
= (eof
> pos
) ? eof
- pos
- 1 : 0;
760 if ((*f
)->buf
.bytesused
+ img
> imagesize
) {
762 b
= (*f
)->buf
.bytesused
+ img
-
764 img
= imagesize
- (*f
)->buf
.bytesused
;
765 PDBGG("Expected EOF not found: video "
768 DBG(3, "Exceeded limit: +%u "
769 "bytes", (unsigned)(b
));
772 memcpy((*f
)->bufmem
+ (*f
)->buf
.bytesused
, pos
,
775 if ((*f
)->buf
.bytesused
== 0)
776 do_gettimeofday(&(*f
)->buf
.timestamp
);
778 (*f
)->buf
.bytesused
+= img
;
780 if ((*f
)->buf
.bytesused
== imagesize
||
781 ((cam
->sensor
.pix_format
.pixelformat
==
782 V4L2_PIX_FMT_SN9C10X
||
783 cam
->sensor
.pix_format
.pixelformat
==
784 V4L2_PIX_FMT_JPEG
) && eof
)) {
787 b
= (*f
)->buf
.bytesused
;
788 (*f
)->state
= F_DONE
;
789 (*f
)->buf
.sequence
= ++cam
->frame_count
;
791 spin_lock(&cam
->queue_lock
);
792 list_move_tail(&(*f
)->frame
,
794 if (!list_empty(&cam
->inqueue
))
797 struct sn9c102_frame_t
,
801 spin_unlock(&cam
->queue_lock
);
803 memcpy(cam
->sysfs
.frame_header
,
804 cam
->sof
.header
, soflen
);
806 DBG(3, "Video frame captured: %lu "
807 "bytes", (unsigned long)(b
));
813 (*f
)->state
= F_ERROR
;
814 DBG(3, "Not expected EOF after %lu "
815 "bytes of image data",
817 ((*f
)->buf
.bytesused
));
824 DBG(3, "EOF without SOF");
828 PDBGG("Ignoring pointless isochronous frame");
832 } else if ((*f
)->state
== F_QUEUED
|| (*f
)->state
== F_ERROR
) {
834 (*f
)->state
= F_GRABBING
;
835 (*f
)->buf
.bytesused
= 0;
838 if (cam
->sensor
.pix_format
.pixelformat
==
840 sn9c102_write_jpegheader(cam
, (*f
));
841 DBG(3, "SOF detected: new video frame");
845 } else if ((*f
)->state
== F_GRABBING
) {
846 eof
= sn9c102_find_eof_header(cam
, pos
, len
);
847 if (eof
&& eof
< sof
)
848 goto end_of_frame
; /* (1) */
850 if (cam
->sensor
.pix_format
.pixelformat
==
851 V4L2_PIX_FMT_SN9C10X
||
852 cam
->sensor
.pix_format
.pixelformat
==
854 if (sof
- pos
>= soflen
) {
856 } else { /* remove header */
858 (*f
)->buf
.bytesused
-=
859 (soflen
- (sof
- pos
));
863 DBG(3, "SOF before expected EOF after "
864 "%lu bytes of image data",
866 ((*f
)->buf
.bytesused
));
874 urb
->dev
= cam
->usbdev
;
875 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
876 if (err
< 0 && err
!= -EPERM
) {
877 cam
->state
|= DEV_MISCONFIGURED
;
878 DBG(1, "usb_submit_urb() failed");
881 wake_up_interruptible(&cam
->wait_frame
);
885 static int sn9c102_start_transfer(struct sn9c102_device
* cam
)
887 struct usb_device
*udev
= cam
->usbdev
;
889 struct usb_host_interface
* altsetting
= usb_altnum_to_altsetting(
890 usb_ifnum_to_if(udev
, 0),
891 SN9C102_ALTERNATE_SETTING
);
892 const unsigned int psz
= le16_to_cpu(altsetting
->
893 endpoint
[0].desc
.wMaxPacketSize
);
897 for (i
= 0; i
< SN9C102_URBS
; i
++) {
898 cam
->transfer_buffer
[i
] = kzalloc(SN9C102_ISO_PACKETS
* psz
,
900 if (!cam
->transfer_buffer
[i
]) {
902 DBG(1, "Not enough memory");
907 for (i
= 0; i
< SN9C102_URBS
; i
++) {
908 urb
= usb_alloc_urb(SN9C102_ISO_PACKETS
, GFP_KERNEL
);
912 DBG(1, "usb_alloc_urb() failed");
917 urb
->pipe
= usb_rcvisocpipe(udev
, 1);
918 urb
->transfer_flags
= URB_ISO_ASAP
;
919 urb
->number_of_packets
= SN9C102_ISO_PACKETS
;
920 urb
->complete
= sn9c102_urb_complete
;
921 urb
->transfer_buffer
= cam
->transfer_buffer
[i
];
922 urb
->transfer_buffer_length
= psz
* SN9C102_ISO_PACKETS
;
924 for (j
= 0; j
< SN9C102_ISO_PACKETS
; j
++) {
925 urb
->iso_frame_desc
[j
].offset
= psz
* j
;
926 urb
->iso_frame_desc
[j
].length
= psz
;
931 if (!(cam
->reg
[0x01] & 0x04)) {
932 err
= sn9c102_write_reg(cam
, cam
->reg
[0x01] | 0x04, 0x01);
935 DBG(1, "I/O hardware error");
940 err
= usb_set_interface(udev
, 0, SN9C102_ALTERNATE_SETTING
);
942 DBG(1, "usb_set_interface() failed");
946 cam
->frame_current
= NULL
;
947 cam
->sof
.bytesread
= 0;
949 for (i
= 0; i
< SN9C102_URBS
; i
++) {
950 err
= usb_submit_urb(cam
->urb
[i
], GFP_KERNEL
);
952 for (j
= i
-1; j
>= 0; j
--)
953 usb_kill_urb(cam
->urb
[j
]);
954 DBG(1, "usb_submit_urb() failed, error %d", err
);
962 for (i
= 0; (i
< SN9C102_URBS
) && cam
->urb
[i
]; i
++)
963 usb_free_urb(cam
->urb
[i
]);
966 for (i
= 0; (i
< SN9C102_URBS
) && cam
->transfer_buffer
[i
]; i
++)
967 kfree(cam
->transfer_buffer
[i
]);
973 static int sn9c102_stop_transfer(struct sn9c102_device
* cam
)
975 struct usb_device
*udev
= cam
->usbdev
;
979 if (cam
->state
& DEV_DISCONNECTED
)
982 for (i
= SN9C102_URBS
-1; i
>= 0; i
--) {
983 usb_kill_urb(cam
->urb
[i
]);
984 usb_free_urb(cam
->urb
[i
]);
985 kfree(cam
->transfer_buffer
[i
]);
988 err
= usb_set_interface(udev
, 0, 0); /* 0 Mb/s */
990 DBG(3, "usb_set_interface() failed");
996 static int sn9c102_stream_interrupt(struct sn9c102_device
* cam
)
1000 cam
->stream
= STREAM_INTERRUPT
;
1001 timeout
= wait_event_timeout(cam
->wait_stream
,
1002 (cam
->stream
== STREAM_OFF
) ||
1003 (cam
->state
& DEV_DISCONNECTED
),
1004 SN9C102_URB_TIMEOUT
);
1005 if (cam
->state
& DEV_DISCONNECTED
)
1007 else if (cam
->stream
!= STREAM_OFF
) {
1008 cam
->state
|= DEV_MISCONFIGURED
;
1009 DBG(1, "URB timeout reached. The camera is misconfigured. "
1010 "To use it, close and open /dev/video%d again.",
1018 /*****************************************************************************/
1020 #ifdef CONFIG_VIDEO_ADV_DEBUG
1021 static u16
sn9c102_strtou16(const char* buff
, size_t len
, ssize_t
* count
)
1028 strncpy(str
, buff
, len
);
1031 strncpy(str
, buff
, 6);
1035 val
= simple_strtoul(str
, &endp
, 0);
1039 *count
= (ssize_t
)(endp
- str
);
1040 if ((*count
) && (len
== *count
+1) && (buff
[*count
] == '\n'))
1047 NOTE 1: being inside one of the following methods implies that the v4l
1048 device exists for sure (see kobjects and reference counters)
1049 NOTE 2: buffers are PAGE_SIZE long
1052 static ssize_t
sn9c102_show_reg(struct device
* cd
,
1053 struct device_attribute
*attr
, char* buf
)
1055 struct sn9c102_device
* cam
;
1058 if (mutex_lock_interruptible(&sn9c102_sysfs_lock
))
1059 return -ERESTARTSYS
;
1061 cam
= video_get_drvdata(container_of(cd
, struct video_device
, dev
));
1063 mutex_unlock(&sn9c102_sysfs_lock
);
1067 count
= sprintf(buf
, "%u\n", cam
->sysfs
.reg
);
1069 mutex_unlock(&sn9c102_sysfs_lock
);
1076 sn9c102_store_reg(struct device
* cd
, struct device_attribute
*attr
,
1077 const char* buf
, size_t len
)
1079 struct sn9c102_device
* cam
;
1083 if (mutex_lock_interruptible(&sn9c102_sysfs_lock
))
1084 return -ERESTARTSYS
;
1086 cam
= video_get_drvdata(container_of(cd
, struct video_device
, dev
));
1088 mutex_unlock(&sn9c102_sysfs_lock
);
1092 index
= sn9c102_strtou16(buf
, len
, &count
);
1093 if (index
>= ARRAY_SIZE(cam
->reg
) || !count
) {
1094 mutex_unlock(&sn9c102_sysfs_lock
);
1098 cam
->sysfs
.reg
= index
;
1100 DBG(2, "Moved SN9C1XX register index to 0x%02X", cam
->sysfs
.reg
);
1101 DBG(3, "Written bytes: %zd", count
);
1103 mutex_unlock(&sn9c102_sysfs_lock
);
1109 static ssize_t
sn9c102_show_val(struct device
* cd
,
1110 struct device_attribute
*attr
, char* buf
)
1112 struct sn9c102_device
* cam
;
1116 if (mutex_lock_interruptible(&sn9c102_sysfs_lock
))
1117 return -ERESTARTSYS
;
1119 cam
= video_get_drvdata(container_of(cd
, struct video_device
, dev
));
1121 mutex_unlock(&sn9c102_sysfs_lock
);
1125 if ((val
= sn9c102_read_reg(cam
, cam
->sysfs
.reg
)) < 0) {
1126 mutex_unlock(&sn9c102_sysfs_lock
);
1130 count
= sprintf(buf
, "%d\n", val
);
1132 DBG(3, "Read bytes: %zd, value: %d", count
, val
);
1134 mutex_unlock(&sn9c102_sysfs_lock
);
1141 sn9c102_store_val(struct device
* cd
, struct device_attribute
*attr
,
1142 const char* buf
, size_t len
)
1144 struct sn9c102_device
* cam
;
1149 if (mutex_lock_interruptible(&sn9c102_sysfs_lock
))
1150 return -ERESTARTSYS
;
1152 cam
= video_get_drvdata(container_of(cd
, struct video_device
, dev
));
1154 mutex_unlock(&sn9c102_sysfs_lock
);
1158 value
= sn9c102_strtou16(buf
, len
, &count
);
1160 mutex_unlock(&sn9c102_sysfs_lock
);
1164 err
= sn9c102_write_reg(cam
, value
, cam
->sysfs
.reg
);
1166 mutex_unlock(&sn9c102_sysfs_lock
);
1170 DBG(2, "Written SN9C1XX reg. 0x%02X, val. 0x%02X",
1171 cam
->sysfs
.reg
, value
);
1172 DBG(3, "Written bytes: %zd", count
);
1174 mutex_unlock(&sn9c102_sysfs_lock
);
1180 static ssize_t
sn9c102_show_i2c_reg(struct device
* cd
,
1181 struct device_attribute
*attr
, char* buf
)
1183 struct sn9c102_device
* cam
;
1186 if (mutex_lock_interruptible(&sn9c102_sysfs_lock
))
1187 return -ERESTARTSYS
;
1189 cam
= video_get_drvdata(container_of(cd
, struct video_device
, dev
));
1191 mutex_unlock(&sn9c102_sysfs_lock
);
1195 count
= sprintf(buf
, "%u\n", cam
->sysfs
.i2c_reg
);
1197 DBG(3, "Read bytes: %zd", count
);
1199 mutex_unlock(&sn9c102_sysfs_lock
);
1206 sn9c102_store_i2c_reg(struct device
* cd
, struct device_attribute
*attr
,
1207 const char* buf
, size_t len
)
1209 struct sn9c102_device
* cam
;
1213 if (mutex_lock_interruptible(&sn9c102_sysfs_lock
))
1214 return -ERESTARTSYS
;
1216 cam
= video_get_drvdata(container_of(cd
, struct video_device
, dev
));
1218 mutex_unlock(&sn9c102_sysfs_lock
);
1222 index
= sn9c102_strtou16(buf
, len
, &count
);
1224 mutex_unlock(&sn9c102_sysfs_lock
);
1228 cam
->sysfs
.i2c_reg
= index
;
1230 DBG(2, "Moved sensor register index to 0x%02X", cam
->sysfs
.i2c_reg
);
1231 DBG(3, "Written bytes: %zd", count
);
1233 mutex_unlock(&sn9c102_sysfs_lock
);
1239 static ssize_t
sn9c102_show_i2c_val(struct device
* cd
,
1240 struct device_attribute
*attr
, char* buf
)
1242 struct sn9c102_device
* cam
;
1246 if (mutex_lock_interruptible(&sn9c102_sysfs_lock
))
1247 return -ERESTARTSYS
;
1249 cam
= video_get_drvdata(container_of(cd
, struct video_device
, dev
));
1251 mutex_unlock(&sn9c102_sysfs_lock
);
1255 if (!(cam
->sensor
.sysfs_ops
& SN9C102_I2C_READ
)) {
1256 mutex_unlock(&sn9c102_sysfs_lock
);
1260 if ((val
= sn9c102_i2c_read(cam
, cam
->sysfs
.i2c_reg
)) < 0) {
1261 mutex_unlock(&sn9c102_sysfs_lock
);
1265 count
= sprintf(buf
, "%d\n", val
);
1267 DBG(3, "Read bytes: %zd, value: %d", count
, val
);
1269 mutex_unlock(&sn9c102_sysfs_lock
);
1276 sn9c102_store_i2c_val(struct device
* cd
, struct device_attribute
*attr
,
1277 const char* buf
, size_t len
)
1279 struct sn9c102_device
* cam
;
1284 if (mutex_lock_interruptible(&sn9c102_sysfs_lock
))
1285 return -ERESTARTSYS
;
1287 cam
= video_get_drvdata(container_of(cd
, struct video_device
, dev
));
1289 mutex_unlock(&sn9c102_sysfs_lock
);
1293 if (!(cam
->sensor
.sysfs_ops
& SN9C102_I2C_WRITE
)) {
1294 mutex_unlock(&sn9c102_sysfs_lock
);
1298 value
= sn9c102_strtou16(buf
, len
, &count
);
1300 mutex_unlock(&sn9c102_sysfs_lock
);
1304 err
= sn9c102_i2c_write(cam
, cam
->sysfs
.i2c_reg
, value
);
1306 mutex_unlock(&sn9c102_sysfs_lock
);
1310 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1311 cam
->sysfs
.i2c_reg
, value
);
1312 DBG(3, "Written bytes: %zd", count
);
1314 mutex_unlock(&sn9c102_sysfs_lock
);
1321 sn9c102_store_green(struct device
* cd
, struct device_attribute
*attr
,
1322 const char* buf
, size_t len
)
1324 struct sn9c102_device
* cam
;
1325 enum sn9c102_bridge bridge
;
1330 if (mutex_lock_interruptible(&sn9c102_sysfs_lock
))
1331 return -ERESTARTSYS
;
1333 cam
= video_get_drvdata(container_of(cd
, struct video_device
, dev
));
1335 mutex_unlock(&sn9c102_sysfs_lock
);
1339 bridge
= cam
->bridge
;
1341 mutex_unlock(&sn9c102_sysfs_lock
);
1343 value
= sn9c102_strtou16(buf
, len
, &count
);
1348 case BRIDGE_SN9C101
:
1349 case BRIDGE_SN9C102
:
1352 if ((res
= sn9c102_store_reg(cd
, attr
, "0x11", 4)) >= 0)
1353 res
= sn9c102_store_val(cd
, attr
, buf
, len
);
1355 case BRIDGE_SN9C103
:
1356 case BRIDGE_SN9C105
:
1357 case BRIDGE_SN9C120
:
1360 if ((res
= sn9c102_store_reg(cd
, attr
, "0x07", 4)) >= 0)
1361 res
= sn9c102_store_val(cd
, attr
, buf
, len
);
1370 sn9c102_store_blue(struct device
* cd
, struct device_attribute
*attr
,
1371 const char* buf
, size_t len
)
1377 value
= sn9c102_strtou16(buf
, len
, &count
);
1378 if (!count
|| value
> 0x7f)
1381 if ((res
= sn9c102_store_reg(cd
, attr
, "0x06", 4)) >= 0)
1382 res
= sn9c102_store_val(cd
, attr
, buf
, len
);
1389 sn9c102_store_red(struct device
* cd
, struct device_attribute
*attr
,
1390 const char* buf
, size_t len
)
1396 value
= sn9c102_strtou16(buf
, len
, &count
);
1397 if (!count
|| value
> 0x7f)
1400 if ((res
= sn9c102_store_reg(cd
, attr
, "0x05", 4)) >= 0)
1401 res
= sn9c102_store_val(cd
, attr
, buf
, len
);
1407 static ssize_t
sn9c102_show_frame_header(struct device
* cd
,
1408 struct device_attribute
*attr
,
1411 struct sn9c102_device
* cam
;
1414 cam
= video_get_drvdata(container_of(cd
, struct video_device
, dev
));
1418 count
= sizeof(cam
->sysfs
.frame_header
);
1419 memcpy(buf
, cam
->sysfs
.frame_header
, count
);
1421 DBG(3, "Frame header, read bytes: %zd", count
);
1427 static DEVICE_ATTR(reg
, S_IRUGO
| S_IWUSR
, sn9c102_show_reg
, sn9c102_store_reg
);
1428 static DEVICE_ATTR(val
, S_IRUGO
| S_IWUSR
, sn9c102_show_val
, sn9c102_store_val
);
1429 static DEVICE_ATTR(i2c_reg
, S_IRUGO
| S_IWUSR
,
1430 sn9c102_show_i2c_reg
, sn9c102_store_i2c_reg
);
1431 static DEVICE_ATTR(i2c_val
, S_IRUGO
| S_IWUSR
,
1432 sn9c102_show_i2c_val
, sn9c102_store_i2c_val
);
1433 static DEVICE_ATTR(green
, S_IWUGO
, NULL
, sn9c102_store_green
);
1434 static DEVICE_ATTR(blue
, S_IWUGO
, NULL
, sn9c102_store_blue
);
1435 static DEVICE_ATTR(red
, S_IWUGO
, NULL
, sn9c102_store_red
);
1436 static DEVICE_ATTR(frame_header
, S_IRUGO
, sn9c102_show_frame_header
, NULL
);
1439 static int sn9c102_create_sysfs(struct sn9c102_device
* cam
)
1441 struct device
*dev
= &(cam
->v4ldev
->dev
);
1444 if ((err
= device_create_file(dev
, &dev_attr_reg
)))
1446 if ((err
= device_create_file(dev
, &dev_attr_val
)))
1448 if ((err
= device_create_file(dev
, &dev_attr_frame_header
)))
1451 if (cam
->sensor
.sysfs_ops
) {
1452 if ((err
= device_create_file(dev
, &dev_attr_i2c_reg
)))
1453 goto err_frame_header
;
1454 if ((err
= device_create_file(dev
, &dev_attr_i2c_val
)))
1458 if (cam
->bridge
== BRIDGE_SN9C101
|| cam
->bridge
== BRIDGE_SN9C102
) {
1459 if ((err
= device_create_file(dev
, &dev_attr_green
)))
1462 if ((err
= device_create_file(dev
, &dev_attr_blue
)))
1464 if ((err
= device_create_file(dev
, &dev_attr_red
)))
1471 device_remove_file(dev
, &dev_attr_blue
);
1473 if (cam
->sensor
.sysfs_ops
)
1474 device_remove_file(dev
, &dev_attr_i2c_val
);
1476 if (cam
->sensor
.sysfs_ops
)
1477 device_remove_file(dev
, &dev_attr_i2c_reg
);
1479 device_remove_file(dev
, &dev_attr_frame_header
);
1481 device_remove_file(dev
, &dev_attr_val
);
1483 device_remove_file(dev
, &dev_attr_reg
);
1487 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1489 /*****************************************************************************/
1492 sn9c102_set_pix_format(struct sn9c102_device
* cam
, struct v4l2_pix_format
* pix
)
1496 if (pix
->pixelformat
== V4L2_PIX_FMT_SN9C10X
||
1497 pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) {
1498 switch (cam
->bridge
) {
1499 case BRIDGE_SN9C101
:
1500 case BRIDGE_SN9C102
:
1501 case BRIDGE_SN9C103
:
1502 err
+= sn9c102_write_reg(cam
, cam
->reg
[0x18] | 0x80,
1505 case BRIDGE_SN9C105
:
1506 case BRIDGE_SN9C120
:
1507 err
+= sn9c102_write_reg(cam
, cam
->reg
[0x18] & 0x7f,
1512 switch (cam
->bridge
) {
1513 case BRIDGE_SN9C101
:
1514 case BRIDGE_SN9C102
:
1515 case BRIDGE_SN9C103
:
1516 err
+= sn9c102_write_reg(cam
, cam
->reg
[0x18] & 0x7f,
1519 case BRIDGE_SN9C105
:
1520 case BRIDGE_SN9C120
:
1521 err
+= sn9c102_write_reg(cam
, cam
->reg
[0x18] | 0x80,
1527 return err
? -EIO
: 0;
1532 sn9c102_set_compression(struct sn9c102_device
* cam
,
1533 struct v4l2_jpegcompression
* compression
)
1537 switch (cam
->bridge
) {
1538 case BRIDGE_SN9C101
:
1539 case BRIDGE_SN9C102
:
1540 case BRIDGE_SN9C103
:
1541 if (compression
->quality
== 0)
1542 err
+= sn9c102_write_reg(cam
, cam
->reg
[0x17] | 0x01,
1544 else if (compression
->quality
== 1)
1545 err
+= sn9c102_write_reg(cam
, cam
->reg
[0x17] & 0xfe,
1548 case BRIDGE_SN9C105
:
1549 case BRIDGE_SN9C120
:
1550 if (compression
->quality
== 0) {
1551 for (i
= 0; i
<= 63; i
++) {
1552 err
+= sn9c102_write_reg(cam
,
1553 SN9C102_Y_QTABLE1
[i
],
1555 err
+= sn9c102_write_reg(cam
,
1556 SN9C102_UV_QTABLE1
[i
],
1559 err
+= sn9c102_write_reg(cam
, cam
->reg
[0x18] & 0xbf,
1561 } else if (compression
->quality
== 1) {
1562 for (i
= 0; i
<= 63; i
++) {
1563 err
+= sn9c102_write_reg(cam
,
1564 SN9C102_Y_QTABLE1
[i
],
1566 err
+= sn9c102_write_reg(cam
,
1567 SN9C102_UV_QTABLE1
[i
],
1570 err
+= sn9c102_write_reg(cam
, cam
->reg
[0x18] | 0x40,
1576 return err
? -EIO
: 0;
1580 static int sn9c102_set_scale(struct sn9c102_device
* cam
, u8 scale
)
1586 r
= cam
->reg
[0x18] & 0xcf;
1587 else if (scale
== 2) {
1588 r
= cam
->reg
[0x18] & 0xcf;
1590 } else if (scale
== 4)
1591 r
= cam
->reg
[0x18] | 0x20;
1593 err
+= sn9c102_write_reg(cam
, r
, 0x18);
1597 PDBGG("Scaling factor: %u", scale
);
1603 static int sn9c102_set_crop(struct sn9c102_device
* cam
, struct v4l2_rect
* rect
)
1605 struct sn9c102_sensor
* s
= &cam
->sensor
;
1606 u8 h_start
= (u8
)(rect
->left
- s
->cropcap
.bounds
.left
),
1607 v_start
= (u8
)(rect
->top
- s
->cropcap
.bounds
.top
),
1608 h_size
= (u8
)(rect
->width
/ 16),
1609 v_size
= (u8
)(rect
->height
/ 16);
1612 err
+= sn9c102_write_reg(cam
, h_start
, 0x12);
1613 err
+= sn9c102_write_reg(cam
, v_start
, 0x13);
1614 err
+= sn9c102_write_reg(cam
, h_size
, 0x15);
1615 err
+= sn9c102_write_reg(cam
, v_size
, 0x16);
1619 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1620 "%u %u %u %u", h_start
, v_start
, h_size
, v_size
);
1626 static int sn9c102_init(struct sn9c102_device
* cam
)
1628 struct sn9c102_sensor
* s
= &cam
->sensor
;
1629 struct v4l2_control ctrl
;
1630 struct v4l2_queryctrl
*qctrl
;
1631 struct v4l2_rect
* rect
;
1635 if (!(cam
->state
& DEV_INITIALIZED
)) {
1636 mutex_init(&cam
->open_mutex
);
1637 init_waitqueue_head(&cam
->wait_open
);
1639 rect
= &(s
->cropcap
.defrect
);
1640 } else { /* use current values */
1645 err
+= sn9c102_set_scale(cam
, rect
->width
/ s
->pix_format
.width
);
1646 err
+= sn9c102_set_crop(cam
, rect
);
1653 DBG(3, "Sensor initialization failed");
1658 if (!(cam
->state
& DEV_INITIALIZED
))
1659 if (cam
->bridge
== BRIDGE_SN9C101
||
1660 cam
->bridge
== BRIDGE_SN9C102
||
1661 cam
->bridge
== BRIDGE_SN9C103
) {
1662 if (s
->pix_format
.pixelformat
== V4L2_PIX_FMT_JPEG
)
1663 s
->pix_format
.pixelformat
= V4L2_PIX_FMT_SBGGR8
;
1664 cam
->compression
.quality
= cam
->reg
[0x17] & 0x01 ?
1667 if (s
->pix_format
.pixelformat
== V4L2_PIX_FMT_SN9C10X
)
1668 s
->pix_format
.pixelformat
= V4L2_PIX_FMT_JPEG
;
1669 cam
->compression
.quality
= cam
->reg
[0x18] & 0x40 ?
1671 err
+= sn9c102_set_compression(cam
, &cam
->compression
);
1674 err
+= sn9c102_set_compression(cam
, &cam
->compression
);
1675 err
+= sn9c102_set_pix_format(cam
, &s
->pix_format
);
1676 if (s
->set_pix_format
)
1677 err
+= s
->set_pix_format(cam
, &s
->pix_format
);
1681 if (s
->pix_format
.pixelformat
== V4L2_PIX_FMT_SN9C10X
||
1682 s
->pix_format
.pixelformat
== V4L2_PIX_FMT_JPEG
)
1683 DBG(3, "Compressed video format is active, quality %d",
1684 cam
->compression
.quality
);
1686 DBG(3, "Uncompressed video format is active");
1689 if ((err
= s
->set_crop(cam
, rect
))) {
1690 DBG(3, "set_crop() failed");
1695 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1696 if (s
->qctrl
[i
].id
!= 0 &&
1697 !(s
->qctrl
[i
].flags
& V4L2_CTRL_FLAG_DISABLED
)) {
1698 ctrl
.id
= s
->qctrl
[i
].id
;
1699 ctrl
.value
= qctrl
[i
].default_value
;
1700 err
= s
->set_ctrl(cam
, &ctrl
);
1702 DBG(3, "Set %s control failed",
1706 DBG(3, "Image sensor supports '%s' control",
1711 if (!(cam
->state
& DEV_INITIALIZED
)) {
1712 mutex_init(&cam
->fileop_mutex
);
1713 spin_lock_init(&cam
->queue_lock
);
1714 init_waitqueue_head(&cam
->wait_frame
);
1715 init_waitqueue_head(&cam
->wait_stream
);
1716 cam
->nreadbuffers
= 2;
1717 memcpy(s
->_qctrl
, s
->qctrl
, sizeof(s
->qctrl
));
1718 memcpy(&(s
->_rect
), &(s
->cropcap
.defrect
),
1719 sizeof(struct v4l2_rect
));
1720 cam
->state
|= DEV_INITIALIZED
;
1723 DBG(2, "Initialization succeeded");
1727 /*****************************************************************************/
1729 static void sn9c102_release_resources(struct kref
*kref
)
1731 struct sn9c102_device
*cam
;
1733 mutex_lock(&sn9c102_sysfs_lock
);
1735 cam
= container_of(kref
, struct sn9c102_device
, kref
);
1737 DBG(2, "V4L2 device /dev/video%d deregistered", cam
->v4ldev
->num
);
1738 video_set_drvdata(cam
->v4ldev
, NULL
);
1739 video_unregister_device(cam
->v4ldev
);
1740 usb_put_dev(cam
->usbdev
);
1741 kfree(cam
->control_buffer
);
1744 mutex_unlock(&sn9c102_sysfs_lock
);
1749 static int sn9c102_open(struct file
*filp
)
1751 struct sn9c102_device
* cam
;
1755 A read_trylock() in open() is the only safe way to prevent race
1756 conditions with disconnect(), one close() and multiple (not
1757 necessarily simultaneous) attempts to open(). For example, it
1758 prevents from waiting for a second access, while the device
1759 structure is being deallocated, after a possible disconnect() and
1760 during a following close() holding the write lock: given that, after
1761 this deallocation, no access will be possible anymore, using the
1762 non-trylock version would have let open() gain the access to the
1763 device structure improperly.
1764 For this reason the lock must also not be per-device.
1766 if (!down_read_trylock(&sn9c102_dev_lock
))
1767 return -ERESTARTSYS
;
1769 cam
= video_drvdata(filp
);
1771 if (wait_for_completion_interruptible(&cam
->probe
)) {
1772 up_read(&sn9c102_dev_lock
);
1773 return -ERESTARTSYS
;
1776 kref_get(&cam
->kref
);
1779 Make sure to isolate all the simultaneous opens.
1781 if (mutex_lock_interruptible(&cam
->open_mutex
)) {
1782 kref_put(&cam
->kref
, sn9c102_release_resources
);
1783 up_read(&sn9c102_dev_lock
);
1784 return -ERESTARTSYS
;
1787 if (cam
->state
& DEV_DISCONNECTED
) {
1788 DBG(1, "Device not present");
1794 DBG(2, "Device /dev/video%d is already in use",
1796 DBG(3, "Simultaneous opens are not supported");
1798 open() must follow the open flags and should block
1799 eventually while the device is in use.
1801 if ((filp
->f_flags
& O_NONBLOCK
) ||
1802 (filp
->f_flags
& O_NDELAY
)) {
1806 DBG(2, "A blocking open() has been requested. Wait for the "
1807 "device to be released...");
1808 up_read(&sn9c102_dev_lock
);
1810 We will not release the "open_mutex" lock, so that only one
1811 process can be in the wait queue below. This way the process
1812 will be sleeping while holding the lock, without loosing its
1813 priority after any wake_up().
1815 err
= wait_event_interruptible_exclusive(cam
->wait_open
,
1816 (cam
->state
& DEV_DISCONNECTED
)
1818 down_read(&sn9c102_dev_lock
);
1821 if (cam
->state
& DEV_DISCONNECTED
) {
1827 if (cam
->state
& DEV_MISCONFIGURED
) {
1828 err
= sn9c102_init(cam
);
1830 DBG(1, "Initialization failed again. "
1831 "I will retry on next open().");
1834 cam
->state
&= ~DEV_MISCONFIGURED
;
1837 if ((err
= sn9c102_start_transfer(cam
)))
1840 filp
->private_data
= cam
;
1843 cam
->stream
= STREAM_OFF
;
1845 cam
->frame_count
= 0;
1846 sn9c102_empty_framequeues(cam
);
1848 DBG(3, "Video device /dev/video%d is open", cam
->v4ldev
->num
);
1851 mutex_unlock(&cam
->open_mutex
);
1853 kref_put(&cam
->kref
, sn9c102_release_resources
);
1855 up_read(&sn9c102_dev_lock
);
1860 static int sn9c102_release(struct file
*filp
)
1862 struct sn9c102_device
* cam
;
1864 down_write(&sn9c102_dev_lock
);
1866 cam
= video_drvdata(filp
);
1868 sn9c102_stop_transfer(cam
);
1869 sn9c102_release_buffers(cam
);
1871 wake_up_interruptible_nr(&cam
->wait_open
, 1);
1873 DBG(3, "Video device /dev/video%d closed", cam
->v4ldev
->num
);
1875 kref_put(&cam
->kref
, sn9c102_release_resources
);
1877 up_write(&sn9c102_dev_lock
);
1884 sn9c102_read(struct file
* filp
, char __user
* buf
, size_t count
, loff_t
* f_pos
)
1886 struct sn9c102_device
*cam
= video_drvdata(filp
);
1887 struct sn9c102_frame_t
* f
, * i
;
1888 unsigned long lock_flags
;
1892 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
1893 return -ERESTARTSYS
;
1895 if (cam
->state
& DEV_DISCONNECTED
) {
1896 DBG(1, "Device not present");
1897 mutex_unlock(&cam
->fileop_mutex
);
1901 if (cam
->state
& DEV_MISCONFIGURED
) {
1902 DBG(1, "The camera is misconfigured. Close and open it "
1904 mutex_unlock(&cam
->fileop_mutex
);
1908 if (cam
->io
== IO_MMAP
) {
1909 DBG(3, "Close and open the device again to choose "
1911 mutex_unlock(&cam
->fileop_mutex
);
1915 if (cam
->io
== IO_NONE
) {
1916 if (!sn9c102_request_buffers(cam
,cam
->nreadbuffers
, IO_READ
)) {
1917 DBG(1, "read() failed, not enough memory");
1918 mutex_unlock(&cam
->fileop_mutex
);
1922 cam
->stream
= STREAM_ON
;
1925 if (list_empty(&cam
->inqueue
)) {
1926 if (!list_empty(&cam
->outqueue
))
1927 sn9c102_empty_framequeues(cam
);
1928 sn9c102_queue_unusedframes(cam
);
1932 mutex_unlock(&cam
->fileop_mutex
);
1936 if (list_empty(&cam
->outqueue
)) {
1937 if (filp
->f_flags
& O_NONBLOCK
) {
1938 mutex_unlock(&cam
->fileop_mutex
);
1941 if (!cam
->module_param
.frame_timeout
) {
1942 err
= wait_event_interruptible
1944 (!list_empty(&cam
->outqueue
)) ||
1945 (cam
->state
& DEV_DISCONNECTED
) ||
1946 (cam
->state
& DEV_MISCONFIGURED
) );
1948 mutex_unlock(&cam
->fileop_mutex
);
1952 timeout
= wait_event_interruptible_timeout
1954 (!list_empty(&cam
->outqueue
)) ||
1955 (cam
->state
& DEV_DISCONNECTED
) ||
1956 (cam
->state
& DEV_MISCONFIGURED
),
1958 cam
->module_param
.frame_timeout
* 1000
1962 mutex_unlock(&cam
->fileop_mutex
);
1964 } else if (timeout
== 0 &&
1965 !(cam
->state
& DEV_DISCONNECTED
)) {
1966 DBG(1, "Video frame timeout elapsed");
1967 mutex_unlock(&cam
->fileop_mutex
);
1971 if (cam
->state
& DEV_DISCONNECTED
) {
1972 mutex_unlock(&cam
->fileop_mutex
);
1975 if (cam
->state
& DEV_MISCONFIGURED
) {
1976 mutex_unlock(&cam
->fileop_mutex
);
1981 f
= list_entry(cam
->outqueue
.prev
, struct sn9c102_frame_t
, frame
);
1983 if (count
> f
->buf
.bytesused
)
1984 count
= f
->buf
.bytesused
;
1986 if (copy_to_user(buf
, f
->bufmem
, count
)) {
1993 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
1994 list_for_each_entry(i
, &cam
->outqueue
, frame
)
1995 i
->state
= F_UNUSED
;
1996 INIT_LIST_HEAD(&cam
->outqueue
);
1997 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
1999 sn9c102_queue_unusedframes(cam
);
2001 PDBGG("Frame #%lu, bytes read: %zu",
2002 (unsigned long)f
->buf
.index
, count
);
2004 mutex_unlock(&cam
->fileop_mutex
);
2010 static unsigned int sn9c102_poll(struct file
*filp
, poll_table
*wait
)
2012 struct sn9c102_device
*cam
= video_drvdata(filp
);
2013 struct sn9c102_frame_t
* f
;
2014 unsigned long lock_flags
;
2015 unsigned int mask
= 0;
2017 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
2020 if (cam
->state
& DEV_DISCONNECTED
) {
2021 DBG(1, "Device not present");
2025 if (cam
->state
& DEV_MISCONFIGURED
) {
2026 DBG(1, "The camera is misconfigured. Close and open it "
2031 if (cam
->io
== IO_NONE
) {
2032 if (!sn9c102_request_buffers(cam
, cam
->nreadbuffers
,
2034 DBG(1, "poll() failed, not enough memory");
2038 cam
->stream
= STREAM_ON
;
2041 if (cam
->io
== IO_READ
) {
2042 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
2043 list_for_each_entry(f
, &cam
->outqueue
, frame
)
2044 f
->state
= F_UNUSED
;
2045 INIT_LIST_HEAD(&cam
->outqueue
);
2046 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
2047 sn9c102_queue_unusedframes(cam
);
2050 poll_wait(filp
, &cam
->wait_frame
, wait
);
2052 if (!list_empty(&cam
->outqueue
))
2053 mask
|= POLLIN
| POLLRDNORM
;
2055 mutex_unlock(&cam
->fileop_mutex
);
2060 mutex_unlock(&cam
->fileop_mutex
);
2065 static void sn9c102_vm_open(struct vm_area_struct
* vma
)
2067 struct sn9c102_frame_t
* f
= vma
->vm_private_data
;
2072 static void sn9c102_vm_close(struct vm_area_struct
* vma
)
2074 /* NOTE: buffers are not freed here */
2075 struct sn9c102_frame_t
* f
= vma
->vm_private_data
;
2080 static const struct vm_operations_struct sn9c102_vm_ops
= {
2081 .open
= sn9c102_vm_open
,
2082 .close
= sn9c102_vm_close
,
2086 static int sn9c102_mmap(struct file
* filp
, struct vm_area_struct
*vma
)
2088 struct sn9c102_device
*cam
= video_drvdata(filp
);
2089 unsigned long size
= vma
->vm_end
- vma
->vm_start
,
2090 start
= vma
->vm_start
;
2094 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
2095 return -ERESTARTSYS
;
2097 if (cam
->state
& DEV_DISCONNECTED
) {
2098 DBG(1, "Device not present");
2099 mutex_unlock(&cam
->fileop_mutex
);
2103 if (cam
->state
& DEV_MISCONFIGURED
) {
2104 DBG(1, "The camera is misconfigured. Close and open it "
2106 mutex_unlock(&cam
->fileop_mutex
);
2110 if (!(vma
->vm_flags
& (VM_WRITE
| VM_READ
))) {
2111 mutex_unlock(&cam
->fileop_mutex
);
2115 if (cam
->io
!= IO_MMAP
||
2116 size
!= PAGE_ALIGN(cam
->frame
[0].buf
.length
)) {
2117 mutex_unlock(&cam
->fileop_mutex
);
2121 for (i
= 0; i
< cam
->nbuffers
; i
++) {
2122 if ((cam
->frame
[i
].buf
.m
.offset
>>PAGE_SHIFT
) == vma
->vm_pgoff
)
2125 if (i
== cam
->nbuffers
) {
2126 mutex_unlock(&cam
->fileop_mutex
);
2130 vma
->vm_flags
|= VM_IO
;
2131 vma
->vm_flags
|= VM_RESERVED
;
2133 pos
= cam
->frame
[i
].bufmem
;
2134 while (size
> 0) { /* size is page-aligned */
2135 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
2136 mutex_unlock(&cam
->fileop_mutex
);
2144 vma
->vm_ops
= &sn9c102_vm_ops
;
2145 vma
->vm_private_data
= &cam
->frame
[i
];
2146 sn9c102_vm_open(vma
);
2148 mutex_unlock(&cam
->fileop_mutex
);
2153 /*****************************************************************************/
2156 sn9c102_vidioc_querycap(struct sn9c102_device
* cam
, void __user
* arg
)
2158 struct v4l2_capability cap
= {
2159 .driver
= "sn9c102",
2160 .version
= SN9C102_MODULE_VERSION_CODE
,
2161 .capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
2165 strlcpy(cap
.card
, cam
->v4ldev
->name
, sizeof(cap
.card
));
2166 if (usb_make_path(cam
->usbdev
, cap
.bus_info
, sizeof(cap
.bus_info
)) < 0)
2167 strlcpy(cap
.bus_info
, dev_name(&cam
->usbdev
->dev
),
2168 sizeof(cap
.bus_info
));
2170 if (copy_to_user(arg
, &cap
, sizeof(cap
)))
2178 sn9c102_vidioc_enuminput(struct sn9c102_device
* cam
, void __user
* arg
)
2180 struct v4l2_input i
;
2182 if (copy_from_user(&i
, arg
, sizeof(i
)))
2188 memset(&i
, 0, sizeof(i
));
2189 strcpy(i
.name
, "Camera");
2190 i
.type
= V4L2_INPUT_TYPE_CAMERA
;
2192 if (copy_to_user(arg
, &i
, sizeof(i
)))
2200 sn9c102_vidioc_g_input(struct sn9c102_device
* cam
, void __user
* arg
)
2204 if (copy_to_user(arg
, &index
, sizeof(index
)))
2212 sn9c102_vidioc_s_input(struct sn9c102_device
* cam
, void __user
* arg
)
2216 if (copy_from_user(&index
, arg
, sizeof(index
)))
2227 sn9c102_vidioc_query_ctrl(struct sn9c102_device
* cam
, void __user
* arg
)
2229 struct sn9c102_sensor
* s
= &cam
->sensor
;
2230 struct v4l2_queryctrl qc
;
2233 if (copy_from_user(&qc
, arg
, sizeof(qc
)))
2236 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
2237 if (qc
.id
&& qc
.id
== s
->qctrl
[i
].id
) {
2238 memcpy(&qc
, &(s
->qctrl
[i
]), sizeof(qc
));
2239 if (copy_to_user(arg
, &qc
, sizeof(qc
)))
2249 sn9c102_vidioc_g_ctrl(struct sn9c102_device
* cam
, void __user
* arg
)
2251 struct sn9c102_sensor
* s
= &cam
->sensor
;
2252 struct v4l2_control ctrl
;
2256 if (!s
->get_ctrl
&& !s
->set_ctrl
)
2259 if (copy_from_user(&ctrl
, arg
, sizeof(ctrl
)))
2263 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
2264 if (ctrl
.id
&& ctrl
.id
== s
->qctrl
[i
].id
) {
2265 ctrl
.value
= s
->_qctrl
[i
].default_value
;
2270 err
= s
->get_ctrl(cam
, &ctrl
);
2273 if (copy_to_user(arg
, &ctrl
, sizeof(ctrl
)))
2276 PDBGG("VIDIOC_G_CTRL: id %lu, value %lu",
2277 (unsigned long)ctrl
.id
, (unsigned long)ctrl
.value
);
2284 sn9c102_vidioc_s_ctrl(struct sn9c102_device
* cam
, void __user
* arg
)
2286 struct sn9c102_sensor
* s
= &cam
->sensor
;
2287 struct v4l2_control ctrl
;
2294 if (copy_from_user(&ctrl
, arg
, sizeof(ctrl
)))
2297 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
2298 if (ctrl
.id
== s
->qctrl
[i
].id
) {
2299 if (s
->qctrl
[i
].flags
& V4L2_CTRL_FLAG_DISABLED
)
2301 if (ctrl
.value
< s
->qctrl
[i
].minimum
||
2302 ctrl
.value
> s
->qctrl
[i
].maximum
)
2304 ctrl
.value
-= ctrl
.value
% s
->qctrl
[i
].step
;
2308 if ((err
= s
->set_ctrl(cam
, &ctrl
)))
2311 s
->_qctrl
[i
].default_value
= ctrl
.value
;
2313 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
2314 (unsigned long)ctrl
.id
, (unsigned long)ctrl
.value
);
2321 sn9c102_vidioc_cropcap(struct sn9c102_device
* cam
, void __user
* arg
)
2323 struct v4l2_cropcap
* cc
= &(cam
->sensor
.cropcap
);
2325 cc
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2326 cc
->pixelaspect
.numerator
= 1;
2327 cc
->pixelaspect
.denominator
= 1;
2329 if (copy_to_user(arg
, cc
, sizeof(*cc
)))
2337 sn9c102_vidioc_g_crop(struct sn9c102_device
* cam
, void __user
* arg
)
2339 struct sn9c102_sensor
* s
= &cam
->sensor
;
2340 struct v4l2_crop crop
= {
2341 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
2344 memcpy(&(crop
.c
), &(s
->_rect
), sizeof(struct v4l2_rect
));
2346 if (copy_to_user(arg
, &crop
, sizeof(crop
)))
2354 sn9c102_vidioc_s_crop(struct sn9c102_device
* cam
, void __user
* arg
)
2356 struct sn9c102_sensor
* s
= &cam
->sensor
;
2357 struct v4l2_crop crop
;
2358 struct v4l2_rect
* rect
;
2359 struct v4l2_rect
* bounds
= &(s
->cropcap
.bounds
);
2360 struct v4l2_pix_format
* pix_format
= &(s
->pix_format
);
2362 const enum sn9c102_stream_state stream
= cam
->stream
;
2363 const u32 nbuffers
= cam
->nbuffers
;
2367 if (copy_from_user(&crop
, arg
, sizeof(crop
)))
2372 if (crop
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2375 if (cam
->module_param
.force_munmap
)
2376 for (i
= 0; i
< cam
->nbuffers
; i
++)
2377 if (cam
->frame
[i
].vma_use_count
) {
2378 DBG(3, "VIDIOC_S_CROP failed. "
2379 "Unmap the buffers first.");
2383 /* Preserve R,G or B origin */
2384 rect
->left
= (s
->_rect
.left
& 1L) ? rect
->left
| 1L : rect
->left
& ~1L;
2385 rect
->top
= (s
->_rect
.top
& 1L) ? rect
->top
| 1L : rect
->top
& ~1L;
2387 if (rect
->width
< 16)
2389 if (rect
->height
< 16)
2391 if (rect
->width
> bounds
->width
)
2392 rect
->width
= bounds
->width
;
2393 if (rect
->height
> bounds
->height
)
2394 rect
->height
= bounds
->height
;
2395 if (rect
->left
< bounds
->left
)
2396 rect
->left
= bounds
->left
;
2397 if (rect
->top
< bounds
->top
)
2398 rect
->top
= bounds
->top
;
2399 if (rect
->left
+ rect
->width
> bounds
->left
+ bounds
->width
)
2400 rect
->left
= bounds
->left
+bounds
->width
- rect
->width
;
2401 if (rect
->top
+ rect
->height
> bounds
->top
+ bounds
->height
)
2402 rect
->top
= bounds
->top
+bounds
->height
- rect
->height
;
2404 rect
->width
&= ~15L;
2405 rect
->height
&= ~15L;
2407 if (SN9C102_PRESERVE_IMGSCALE
) {
2408 /* Calculate the actual scaling factor */
2410 a
= rect
->width
* rect
->height
;
2411 b
= pix_format
->width
* pix_format
->height
;
2412 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : ((a
/ b
) < 16 ? 2 : 4)) : 1;
2416 if (cam
->stream
== STREAM_ON
)
2417 if ((err
= sn9c102_stream_interrupt(cam
)))
2420 if (copy_to_user(arg
, &crop
, sizeof(crop
))) {
2421 cam
->stream
= stream
;
2425 if (cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
)
2426 sn9c102_release_buffers(cam
);
2428 err
= sn9c102_set_crop(cam
, rect
);
2430 err
+= s
->set_crop(cam
, rect
);
2431 err
+= sn9c102_set_scale(cam
, scale
);
2433 if (err
) { /* atomic, no rollback in ioctl() */
2434 cam
->state
|= DEV_MISCONFIGURED
;
2435 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2436 "use the camera, close and open /dev/video%d again.",
2441 s
->pix_format
.width
= rect
->width
/scale
;
2442 s
->pix_format
.height
= rect
->height
/scale
;
2443 memcpy(&(s
->_rect
), rect
, sizeof(*rect
));
2445 if ((cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
) &&
2446 nbuffers
!= sn9c102_request_buffers(cam
, nbuffers
, cam
->io
)) {
2447 cam
->state
|= DEV_MISCONFIGURED
;
2448 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2449 "use the camera, close and open /dev/video%d again.",
2454 if (cam
->io
== IO_READ
)
2455 sn9c102_empty_framequeues(cam
);
2456 else if (cam
->module_param
.force_munmap
)
2457 sn9c102_requeue_outqueue(cam
);
2459 cam
->stream
= stream
;
2466 sn9c102_vidioc_enum_framesizes(struct sn9c102_device
* cam
, void __user
* arg
)
2468 struct v4l2_frmsizeenum frmsize
;
2470 if (copy_from_user(&frmsize
, arg
, sizeof(frmsize
)))
2473 if (frmsize
.index
!= 0)
2476 switch (cam
->bridge
) {
2477 case BRIDGE_SN9C101
:
2478 case BRIDGE_SN9C102
:
2479 case BRIDGE_SN9C103
:
2480 if (frmsize
.pixel_format
!= V4L2_PIX_FMT_SN9C10X
&&
2481 frmsize
.pixel_format
!= V4L2_PIX_FMT_SBGGR8
)
2483 case BRIDGE_SN9C105
:
2484 case BRIDGE_SN9C120
:
2485 if (frmsize
.pixel_format
!= V4L2_PIX_FMT_JPEG
&&
2486 frmsize
.pixel_format
!= V4L2_PIX_FMT_SBGGR8
)
2490 frmsize
.type
= V4L2_FRMSIZE_TYPE_STEPWISE
;
2491 frmsize
.stepwise
.min_width
= frmsize
.stepwise
.step_width
= 16;
2492 frmsize
.stepwise
.min_height
= frmsize
.stepwise
.step_height
= 16;
2493 frmsize
.stepwise
.max_width
= cam
->sensor
.cropcap
.bounds
.width
;
2494 frmsize
.stepwise
.max_height
= cam
->sensor
.cropcap
.bounds
.height
;
2495 memset(&frmsize
.reserved
, 0, sizeof(frmsize
.reserved
));
2497 if (copy_to_user(arg
, &frmsize
, sizeof(frmsize
)))
2505 sn9c102_vidioc_enum_fmt(struct sn9c102_device
* cam
, void __user
* arg
)
2507 struct v4l2_fmtdesc fmtd
;
2509 if (copy_from_user(&fmtd
, arg
, sizeof(fmtd
)))
2512 if (fmtd
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2515 if (fmtd
.index
== 0) {
2516 strcpy(fmtd
.description
, "bayer rgb");
2517 fmtd
.pixelformat
= V4L2_PIX_FMT_SBGGR8
;
2518 } else if (fmtd
.index
== 1) {
2519 switch (cam
->bridge
) {
2520 case BRIDGE_SN9C101
:
2521 case BRIDGE_SN9C102
:
2522 case BRIDGE_SN9C103
:
2523 strcpy(fmtd
.description
, "compressed");
2524 fmtd
.pixelformat
= V4L2_PIX_FMT_SN9C10X
;
2526 case BRIDGE_SN9C105
:
2527 case BRIDGE_SN9C120
:
2528 strcpy(fmtd
.description
, "JPEG");
2529 fmtd
.pixelformat
= V4L2_PIX_FMT_JPEG
;
2532 fmtd
.flags
= V4L2_FMT_FLAG_COMPRESSED
;
2536 fmtd
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2537 memset(&fmtd
.reserved
, 0, sizeof(fmtd
.reserved
));
2539 if (copy_to_user(arg
, &fmtd
, sizeof(fmtd
)))
2547 sn9c102_vidioc_g_fmt(struct sn9c102_device
* cam
, void __user
* arg
)
2549 struct v4l2_format format
;
2550 struct v4l2_pix_format
* pfmt
= &(cam
->sensor
.pix_format
);
2552 if (copy_from_user(&format
, arg
, sizeof(format
)))
2555 if (format
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2558 pfmt
->colorspace
= (pfmt
->pixelformat
== V4L2_PIX_FMT_JPEG
) ?
2559 V4L2_COLORSPACE_JPEG
: V4L2_COLORSPACE_SRGB
;
2560 pfmt
->bytesperline
= (pfmt
->pixelformat
== V4L2_PIX_FMT_SN9C10X
||
2561 pfmt
->pixelformat
== V4L2_PIX_FMT_JPEG
)
2562 ? 0 : (pfmt
->width
* pfmt
->priv
) / 8;
2563 pfmt
->sizeimage
= pfmt
->height
* ((pfmt
->width
*pfmt
->priv
)/8);
2564 pfmt
->field
= V4L2_FIELD_NONE
;
2565 memcpy(&(format
.fmt
.pix
), pfmt
, sizeof(*pfmt
));
2567 if (copy_to_user(arg
, &format
, sizeof(format
)))
2575 sn9c102_vidioc_try_s_fmt(struct sn9c102_device
* cam
, unsigned int cmd
,
2578 struct sn9c102_sensor
* s
= &cam
->sensor
;
2579 struct v4l2_format format
;
2580 struct v4l2_pix_format
* pix
;
2581 struct v4l2_pix_format
* pfmt
= &(s
->pix_format
);
2582 struct v4l2_rect
* bounds
= &(s
->cropcap
.bounds
);
2583 struct v4l2_rect rect
;
2585 const enum sn9c102_stream_state stream
= cam
->stream
;
2586 const u32 nbuffers
= cam
->nbuffers
;
2590 if (copy_from_user(&format
, arg
, sizeof(format
)))
2593 pix
= &(format
.fmt
.pix
);
2595 if (format
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2598 memcpy(&rect
, &(s
->_rect
), sizeof(rect
));
2600 { /* calculate the actual scaling factor */
2602 a
= rect
.width
* rect
.height
;
2603 b
= pix
->width
* pix
->height
;
2604 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : ((a
/ b
) < 16 ? 2 : 4)) : 1;
2607 rect
.width
= scale
* pix
->width
;
2608 rect
.height
= scale
* pix
->height
;
2610 if (rect
.width
< 16)
2612 if (rect
.height
< 16)
2614 if (rect
.width
> bounds
->left
+ bounds
->width
- rect
.left
)
2615 rect
.width
= bounds
->left
+ bounds
->width
- rect
.left
;
2616 if (rect
.height
> bounds
->top
+ bounds
->height
- rect
.top
)
2617 rect
.height
= bounds
->top
+ bounds
->height
- rect
.top
;
2620 rect
.height
&= ~15L;
2622 { /* adjust the scaling factor */
2624 a
= rect
.width
* rect
.height
;
2625 b
= pix
->width
* pix
->height
;
2626 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : ((a
/ b
) < 16 ? 2 : 4)) : 1;
2629 pix
->width
= rect
.width
/ scale
;
2630 pix
->height
= rect
.height
/ scale
;
2632 switch (cam
->bridge
) {
2633 case BRIDGE_SN9C101
:
2634 case BRIDGE_SN9C102
:
2635 case BRIDGE_SN9C103
:
2636 if (pix
->pixelformat
!= V4L2_PIX_FMT_SN9C10X
&&
2637 pix
->pixelformat
!= V4L2_PIX_FMT_SBGGR8
)
2638 pix
->pixelformat
= pfmt
->pixelformat
;
2640 case BRIDGE_SN9C105
:
2641 case BRIDGE_SN9C120
:
2642 if (pix
->pixelformat
!= V4L2_PIX_FMT_JPEG
&&
2643 pix
->pixelformat
!= V4L2_PIX_FMT_SBGGR8
)
2644 pix
->pixelformat
= pfmt
->pixelformat
;
2647 pix
->priv
= pfmt
->priv
; /* bpp */
2648 pix
->colorspace
= (pix
->pixelformat
== V4L2_PIX_FMT_JPEG
) ?
2649 V4L2_COLORSPACE_JPEG
: V4L2_COLORSPACE_SRGB
;
2650 pix
->bytesperline
= (pix
->pixelformat
== V4L2_PIX_FMT_SN9C10X
||
2651 pix
->pixelformat
== V4L2_PIX_FMT_JPEG
)
2652 ? 0 : (pix
->width
* pix
->priv
) / 8;
2653 pix
->sizeimage
= pix
->height
* ((pix
->width
* pix
->priv
) / 8);
2654 pix
->field
= V4L2_FIELD_NONE
;
2656 if (cmd
== VIDIOC_TRY_FMT
) {
2657 if (copy_to_user(arg
, &format
, sizeof(format
)))
2662 if (cam
->module_param
.force_munmap
)
2663 for (i
= 0; i
< cam
->nbuffers
; i
++)
2664 if (cam
->frame
[i
].vma_use_count
) {
2665 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2670 if (cam
->stream
== STREAM_ON
)
2671 if ((err
= sn9c102_stream_interrupt(cam
)))
2674 if (copy_to_user(arg
, &format
, sizeof(format
))) {
2675 cam
->stream
= stream
;
2679 if (cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
)
2680 sn9c102_release_buffers(cam
);
2682 err
+= sn9c102_set_pix_format(cam
, pix
);
2683 err
+= sn9c102_set_crop(cam
, &rect
);
2684 if (s
->set_pix_format
)
2685 err
+= s
->set_pix_format(cam
, pix
);
2687 err
+= s
->set_crop(cam
, &rect
);
2688 err
+= sn9c102_set_scale(cam
, scale
);
2690 if (err
) { /* atomic, no rollback in ioctl() */
2691 cam
->state
|= DEV_MISCONFIGURED
;
2692 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2693 "use the camera, close and open /dev/video%d again.",
2698 memcpy(pfmt
, pix
, sizeof(*pix
));
2699 memcpy(&(s
->_rect
), &rect
, sizeof(rect
));
2701 if ((cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
) &&
2702 nbuffers
!= sn9c102_request_buffers(cam
, nbuffers
, cam
->io
)) {
2703 cam
->state
|= DEV_MISCONFIGURED
;
2704 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2705 "use the camera, close and open /dev/video%d again.",
2710 if (cam
->io
== IO_READ
)
2711 sn9c102_empty_framequeues(cam
);
2712 else if (cam
->module_param
.force_munmap
)
2713 sn9c102_requeue_outqueue(cam
);
2715 cam
->stream
= stream
;
2722 sn9c102_vidioc_g_jpegcomp(struct sn9c102_device
* cam
, void __user
* arg
)
2724 if (copy_to_user(arg
, &cam
->compression
, sizeof(cam
->compression
)))
2732 sn9c102_vidioc_s_jpegcomp(struct sn9c102_device
* cam
, void __user
* arg
)
2734 struct v4l2_jpegcompression jc
;
2735 const enum sn9c102_stream_state stream
= cam
->stream
;
2738 if (copy_from_user(&jc
, arg
, sizeof(jc
)))
2741 if (jc
.quality
!= 0 && jc
.quality
!= 1)
2744 if (cam
->stream
== STREAM_ON
)
2745 if ((err
= sn9c102_stream_interrupt(cam
)))
2748 err
+= sn9c102_set_compression(cam
, &jc
);
2749 if (err
) { /* atomic, no rollback in ioctl() */
2750 cam
->state
|= DEV_MISCONFIGURED
;
2751 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2752 "problems. To use the camera, close and open "
2753 "/dev/video%d again.", cam
->v4ldev
->num
);
2757 cam
->compression
.quality
= jc
.quality
;
2759 cam
->stream
= stream
;
2766 sn9c102_vidioc_reqbufs(struct sn9c102_device
* cam
, void __user
* arg
)
2768 struct v4l2_requestbuffers rb
;
2772 if (copy_from_user(&rb
, arg
, sizeof(rb
)))
2775 if (rb
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2776 rb
.memory
!= V4L2_MEMORY_MMAP
)
2779 if (cam
->io
== IO_READ
) {
2780 DBG(3, "Close and open the device again to choose the mmap "
2785 for (i
= 0; i
< cam
->nbuffers
; i
++)
2786 if (cam
->frame
[i
].vma_use_count
) {
2787 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2792 if (cam
->stream
== STREAM_ON
)
2793 if ((err
= sn9c102_stream_interrupt(cam
)))
2796 sn9c102_empty_framequeues(cam
);
2798 sn9c102_release_buffers(cam
);
2800 rb
.count
= sn9c102_request_buffers(cam
, rb
.count
, IO_MMAP
);
2802 if (copy_to_user(arg
, &rb
, sizeof(rb
))) {
2803 sn9c102_release_buffers(cam
);
2808 cam
->io
= rb
.count
? IO_MMAP
: IO_NONE
;
2815 sn9c102_vidioc_querybuf(struct sn9c102_device
* cam
, void __user
* arg
)
2817 struct v4l2_buffer b
;
2819 if (copy_from_user(&b
, arg
, sizeof(b
)))
2822 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2823 b
.index
>= cam
->nbuffers
|| cam
->io
!= IO_MMAP
)
2826 memcpy(&b
, &cam
->frame
[b
.index
].buf
, sizeof(b
));
2828 if (cam
->frame
[b
.index
].vma_use_count
)
2829 b
.flags
|= V4L2_BUF_FLAG_MAPPED
;
2831 if (cam
->frame
[b
.index
].state
== F_DONE
)
2832 b
.flags
|= V4L2_BUF_FLAG_DONE
;
2833 else if (cam
->frame
[b
.index
].state
!= F_UNUSED
)
2834 b
.flags
|= V4L2_BUF_FLAG_QUEUED
;
2836 if (copy_to_user(arg
, &b
, sizeof(b
)))
2844 sn9c102_vidioc_qbuf(struct sn9c102_device
* cam
, void __user
* arg
)
2846 struct v4l2_buffer b
;
2847 unsigned long lock_flags
;
2849 if (copy_from_user(&b
, arg
, sizeof(b
)))
2852 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2853 b
.index
>= cam
->nbuffers
|| cam
->io
!= IO_MMAP
)
2856 if (cam
->frame
[b
.index
].state
!= F_UNUSED
)
2859 cam
->frame
[b
.index
].state
= F_QUEUED
;
2861 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
2862 list_add_tail(&cam
->frame
[b
.index
].frame
, &cam
->inqueue
);
2863 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
2865 PDBGG("Frame #%lu queued", (unsigned long)b
.index
);
2872 sn9c102_vidioc_dqbuf(struct sn9c102_device
* cam
, struct file
* filp
,
2875 struct v4l2_buffer b
;
2876 struct sn9c102_frame_t
*f
;
2877 unsigned long lock_flags
;
2881 if (copy_from_user(&b
, arg
, sizeof(b
)))
2884 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2887 if (list_empty(&cam
->outqueue
)) {
2888 if (cam
->stream
== STREAM_OFF
)
2890 if (filp
->f_flags
& O_NONBLOCK
)
2892 if (!cam
->module_param
.frame_timeout
) {
2893 err
= wait_event_interruptible
2895 (!list_empty(&cam
->outqueue
)) ||
2896 (cam
->state
& DEV_DISCONNECTED
) ||
2897 (cam
->state
& DEV_MISCONFIGURED
) );
2901 timeout
= wait_event_interruptible_timeout
2903 (!list_empty(&cam
->outqueue
)) ||
2904 (cam
->state
& DEV_DISCONNECTED
) ||
2905 (cam
->state
& DEV_MISCONFIGURED
),
2906 cam
->module_param
.frame_timeout
*
2907 1000 * msecs_to_jiffies(1) );
2910 else if (timeout
== 0 &&
2911 !(cam
->state
& DEV_DISCONNECTED
)) {
2912 DBG(1, "Video frame timeout elapsed");
2916 if (cam
->state
& DEV_DISCONNECTED
)
2918 if (cam
->state
& DEV_MISCONFIGURED
)
2922 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
2923 f
= list_entry(cam
->outqueue
.next
, struct sn9c102_frame_t
, frame
);
2924 list_del(cam
->outqueue
.next
);
2925 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
2927 f
->state
= F_UNUSED
;
2929 memcpy(&b
, &f
->buf
, sizeof(b
));
2930 if (f
->vma_use_count
)
2931 b
.flags
|= V4L2_BUF_FLAG_MAPPED
;
2933 if (copy_to_user(arg
, &b
, sizeof(b
)))
2936 PDBGG("Frame #%lu dequeued", (unsigned long)f
->buf
.index
);
2943 sn9c102_vidioc_streamon(struct sn9c102_device
* cam
, void __user
* arg
)
2947 if (copy_from_user(&type
, arg
, sizeof(type
)))
2950 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2953 cam
->stream
= STREAM_ON
;
2955 DBG(3, "Stream on");
2962 sn9c102_vidioc_streamoff(struct sn9c102_device
* cam
, void __user
* arg
)
2966 if (copy_from_user(&type
, arg
, sizeof(type
)))
2969 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2972 if (cam
->stream
== STREAM_ON
)
2973 if ((err
= sn9c102_stream_interrupt(cam
)))
2976 sn9c102_empty_framequeues(cam
);
2978 DBG(3, "Stream off");
2985 sn9c102_vidioc_g_parm(struct sn9c102_device
* cam
, void __user
* arg
)
2987 struct v4l2_streamparm sp
;
2989 if (copy_from_user(&sp
, arg
, sizeof(sp
)))
2992 if (sp
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2995 sp
.parm
.capture
.extendedmode
= 0;
2996 sp
.parm
.capture
.readbuffers
= cam
->nreadbuffers
;
2998 if (copy_to_user(arg
, &sp
, sizeof(sp
)))
3006 sn9c102_vidioc_s_parm(struct sn9c102_device
* cam
, void __user
* arg
)
3008 struct v4l2_streamparm sp
;
3010 if (copy_from_user(&sp
, arg
, sizeof(sp
)))
3013 if (sp
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
3016 sp
.parm
.capture
.extendedmode
= 0;
3018 if (sp
.parm
.capture
.readbuffers
== 0)
3019 sp
.parm
.capture
.readbuffers
= cam
->nreadbuffers
;
3021 if (sp
.parm
.capture
.readbuffers
> SN9C102_MAX_FRAMES
)
3022 sp
.parm
.capture
.readbuffers
= SN9C102_MAX_FRAMES
;
3024 if (copy_to_user(arg
, &sp
, sizeof(sp
)))
3027 cam
->nreadbuffers
= sp
.parm
.capture
.readbuffers
;
3034 sn9c102_vidioc_enumaudio(struct sn9c102_device
* cam
, void __user
* arg
)
3036 struct v4l2_audio audio
;
3038 if (cam
->bridge
== BRIDGE_SN9C101
|| cam
->bridge
== BRIDGE_SN9C102
)
3041 if (copy_from_user(&audio
, arg
, sizeof(audio
)))
3044 if (audio
.index
!= 0)
3047 strcpy(audio
.name
, "Microphone");
3048 audio
.capability
= 0;
3051 if (copy_to_user(arg
, &audio
, sizeof(audio
)))
3059 sn9c102_vidioc_g_audio(struct sn9c102_device
* cam
, void __user
* arg
)
3061 struct v4l2_audio audio
;
3063 if (cam
->bridge
== BRIDGE_SN9C101
|| cam
->bridge
== BRIDGE_SN9C102
)
3066 if (copy_from_user(&audio
, arg
, sizeof(audio
)))
3069 memset(&audio
, 0, sizeof(audio
));
3070 strcpy(audio
.name
, "Microphone");
3072 if (copy_to_user(arg
, &audio
, sizeof(audio
)))
3080 sn9c102_vidioc_s_audio(struct sn9c102_device
* cam
, void __user
* arg
)
3082 struct v4l2_audio audio
;
3084 if (cam
->bridge
== BRIDGE_SN9C101
|| cam
->bridge
== BRIDGE_SN9C102
)
3087 if (copy_from_user(&audio
, arg
, sizeof(audio
)))
3090 if (audio
.index
!= 0)
3097 static long sn9c102_ioctl_v4l2(struct file
*filp
,
3098 unsigned int cmd
, void __user
*arg
)
3100 struct sn9c102_device
*cam
= video_drvdata(filp
);
3104 case VIDIOC_QUERYCAP
:
3105 return sn9c102_vidioc_querycap(cam
, arg
);
3107 case VIDIOC_ENUMINPUT
:
3108 return sn9c102_vidioc_enuminput(cam
, arg
);
3110 case VIDIOC_G_INPUT
:
3111 return sn9c102_vidioc_g_input(cam
, arg
);
3113 case VIDIOC_S_INPUT
:
3114 return sn9c102_vidioc_s_input(cam
, arg
);
3116 case VIDIOC_QUERYCTRL
:
3117 return sn9c102_vidioc_query_ctrl(cam
, arg
);
3120 return sn9c102_vidioc_g_ctrl(cam
, arg
);
3123 return sn9c102_vidioc_s_ctrl(cam
, arg
);
3125 case VIDIOC_CROPCAP
:
3126 return sn9c102_vidioc_cropcap(cam
, arg
);
3129 return sn9c102_vidioc_g_crop(cam
, arg
);
3132 return sn9c102_vidioc_s_crop(cam
, arg
);
3134 case VIDIOC_ENUM_FRAMESIZES
:
3135 return sn9c102_vidioc_enum_framesizes(cam
, arg
);
3137 case VIDIOC_ENUM_FMT
:
3138 return sn9c102_vidioc_enum_fmt(cam
, arg
);
3141 return sn9c102_vidioc_g_fmt(cam
, arg
);
3143 case VIDIOC_TRY_FMT
:
3145 return sn9c102_vidioc_try_s_fmt(cam
, cmd
, arg
);
3147 case VIDIOC_G_JPEGCOMP
:
3148 return sn9c102_vidioc_g_jpegcomp(cam
, arg
);
3150 case VIDIOC_S_JPEGCOMP
:
3151 return sn9c102_vidioc_s_jpegcomp(cam
, arg
);
3153 case VIDIOC_REQBUFS
:
3154 return sn9c102_vidioc_reqbufs(cam
, arg
);
3156 case VIDIOC_QUERYBUF
:
3157 return sn9c102_vidioc_querybuf(cam
, arg
);
3160 return sn9c102_vidioc_qbuf(cam
, arg
);
3163 return sn9c102_vidioc_dqbuf(cam
, filp
, arg
);
3165 case VIDIOC_STREAMON
:
3166 return sn9c102_vidioc_streamon(cam
, arg
);
3168 case VIDIOC_STREAMOFF
:
3169 return sn9c102_vidioc_streamoff(cam
, arg
);
3172 return sn9c102_vidioc_g_parm(cam
, arg
);
3175 return sn9c102_vidioc_s_parm(cam
, arg
);
3177 case VIDIOC_ENUMAUDIO
:
3178 return sn9c102_vidioc_enumaudio(cam
, arg
);
3180 case VIDIOC_G_AUDIO
:
3181 return sn9c102_vidioc_g_audio(cam
, arg
);
3183 case VIDIOC_S_AUDIO
:
3184 return sn9c102_vidioc_s_audio(cam
, arg
);
3188 case VIDIOC_QUERYSTD
:
3189 case VIDIOC_ENUMSTD
:
3190 case VIDIOC_QUERYMENU
:
3191 case VIDIOC_ENUM_FRAMEINTERVALS
:
3201 static long sn9c102_ioctl(struct file
*filp
,
3202 unsigned int cmd
, unsigned long arg
)
3204 struct sn9c102_device
*cam
= video_drvdata(filp
);
3207 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
3208 return -ERESTARTSYS
;
3210 if (cam
->state
& DEV_DISCONNECTED
) {
3211 DBG(1, "Device not present");
3212 mutex_unlock(&cam
->fileop_mutex
);
3216 if (cam
->state
& DEV_MISCONFIGURED
) {
3217 DBG(1, "The camera is misconfigured. Close and open it "
3219 mutex_unlock(&cam
->fileop_mutex
);
3223 V4LDBG(3, "sn9c102", cmd
);
3225 err
= sn9c102_ioctl_v4l2(filp
, cmd
, (void __user
*)arg
);
3227 mutex_unlock(&cam
->fileop_mutex
);
3232 /*****************************************************************************/
3234 static const struct v4l2_file_operations sn9c102_fops
= {
3235 .owner
= THIS_MODULE
,
3236 .open
= sn9c102_open
,
3237 .release
= sn9c102_release
,
3238 .ioctl
= sn9c102_ioctl
,
3239 .read
= sn9c102_read
,
3240 .poll
= sn9c102_poll
,
3241 .mmap
= sn9c102_mmap
,
3244 /*****************************************************************************/
3246 /* It exists a single interface only. We do not need to validate anything. */
3248 sn9c102_usb_probe(struct usb_interface
* intf
, const struct usb_device_id
* id
)
3250 struct usb_device
*udev
= interface_to_usbdev(intf
);
3251 struct sn9c102_device
* cam
;
3252 static unsigned int dev_nr
;
3256 if (!(cam
= kzalloc(sizeof(struct sn9c102_device
), GFP_KERNEL
)))
3261 if (!(cam
->control_buffer
= kzalloc(8, GFP_KERNEL
))) {
3262 DBG(1, "kzalloc() failed");
3267 if (!(cam
->v4ldev
= video_device_alloc())) {
3268 DBG(1, "video_device_alloc() failed");
3273 r
= sn9c102_read_reg(cam
, 0x00);
3274 if (r
< 0 || (r
!= 0x10 && r
!= 0x11 && r
!= 0x12)) {
3275 DBG(1, "Sorry, this is not a SN9C1xx-based camera "
3276 "(vid:pid 0x%04X:0x%04X)", id
->idVendor
, id
->idProduct
);
3281 cam
->bridge
= id
->driver_info
;
3282 switch (cam
->bridge
) {
3283 case BRIDGE_SN9C101
:
3284 case BRIDGE_SN9C102
:
3285 DBG(2, "SN9C10[12] PC Camera Controller detected "
3286 "(vid:pid 0x%04X:0x%04X)", id
->idVendor
, id
->idProduct
);
3288 case BRIDGE_SN9C103
:
3289 DBG(2, "SN9C103 PC Camera Controller detected "
3290 "(vid:pid 0x%04X:0x%04X)", id
->idVendor
, id
->idProduct
);
3292 case BRIDGE_SN9C105
:
3293 DBG(2, "SN9C105 PC Camera Controller detected "
3294 "(vid:pid 0x%04X:0x%04X)", id
->idVendor
, id
->idProduct
);
3296 case BRIDGE_SN9C120
:
3297 DBG(2, "SN9C120 PC Camera Controller detected "
3298 "(vid:pid 0x%04X:0x%04X)", id
->idVendor
, id
->idProduct
);
3302 for (i
= 0; i
< ARRAY_SIZE(sn9c102_sensor_table
); i
++) {
3303 err
= sn9c102_sensor_table
[i
](cam
);
3309 DBG(2, "%s image sensor detected", cam
->sensor
.name
);
3310 DBG(3, "Support for %s maintained by %s",
3311 cam
->sensor
.name
, cam
->sensor
.maintainer
);
3313 DBG(1, "No supported image sensor detected for this bridge");
3318 if (!(cam
->bridge
& cam
->sensor
.supported_bridge
)) {
3319 DBG(1, "Bridge not supported");
3324 if (sn9c102_init(cam
)) {
3325 DBG(1, "Initialization failed. I will retry on open().");
3326 cam
->state
|= DEV_MISCONFIGURED
;
3329 strcpy(cam
->v4ldev
->name
, "SN9C1xx PC Camera");
3330 cam
->v4ldev
->fops
= &sn9c102_fops
;
3331 cam
->v4ldev
->minor
= video_nr
[dev_nr
];
3332 cam
->v4ldev
->release
= video_device_release
;
3333 cam
->v4ldev
->parent
= &udev
->dev
;
3335 init_completion(&cam
->probe
);
3337 err
= video_register_device(cam
->v4ldev
, VFL_TYPE_GRABBER
,
3340 DBG(1, "V4L2 device registration failed");
3341 if (err
== -ENFILE
&& video_nr
[dev_nr
] == -1)
3342 DBG(1, "Free /dev/videoX node not found");
3343 video_nr
[dev_nr
] = -1;
3344 dev_nr
= (dev_nr
< SN9C102_MAX_DEVICES
-1) ? dev_nr
+1 : 0;
3345 complete_all(&cam
->probe
);
3349 DBG(2, "V4L2 device registered as /dev/video%d", cam
->v4ldev
->num
);
3351 video_set_drvdata(cam
->v4ldev
, cam
);
3352 cam
->module_param
.force_munmap
= force_munmap
[dev_nr
];
3353 cam
->module_param
.frame_timeout
= frame_timeout
[dev_nr
];
3355 dev_nr
= (dev_nr
< SN9C102_MAX_DEVICES
-1) ? dev_nr
+1 : 0;
3357 #ifdef CONFIG_VIDEO_ADV_DEBUG
3358 err
= sn9c102_create_sysfs(cam
);
3360 DBG(2, "Optional device control through 'sysfs' "
3363 DBG(2, "Failed to create optional 'sysfs' interface for "
3364 "device controlling. Error #%d", err
);
3366 DBG(2, "Optional device control through 'sysfs' interface disabled");
3367 DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
3368 "configuration option to enable it.");
3371 usb_set_intfdata(intf
, cam
);
3372 kref_init(&cam
->kref
);
3373 usb_get_dev(cam
->usbdev
);
3375 complete_all(&cam
->probe
);
3381 kfree(cam
->control_buffer
);
3383 video_device_release(cam
->v4ldev
);
3390 static void sn9c102_usb_disconnect(struct usb_interface
* intf
)
3392 struct sn9c102_device
* cam
;
3394 down_write(&sn9c102_dev_lock
);
3396 cam
= usb_get_intfdata(intf
);
3398 DBG(2, "Disconnecting %s...", cam
->v4ldev
->name
);
3401 DBG(2, "Device /dev/video%d is open! Deregistration and "
3402 "memory deallocation are deferred.",
3404 cam
->state
|= DEV_MISCONFIGURED
;
3405 sn9c102_stop_transfer(cam
);
3406 cam
->state
|= DEV_DISCONNECTED
;
3407 wake_up_interruptible(&cam
->wait_frame
);
3408 wake_up(&cam
->wait_stream
);
3410 cam
->state
|= DEV_DISCONNECTED
;
3412 wake_up_interruptible_all(&cam
->wait_open
);
3414 kref_put(&cam
->kref
, sn9c102_release_resources
);
3416 up_write(&sn9c102_dev_lock
);
3420 static struct usb_driver sn9c102_usb_driver
= {
3422 .id_table
= sn9c102_id_table
,
3423 .probe
= sn9c102_usb_probe
,
3424 .disconnect
= sn9c102_usb_disconnect
,
3427 /*****************************************************************************/
3429 static int __init
sn9c102_module_init(void)
3433 KDBG(2, SN9C102_MODULE_NAME
" v" SN9C102_MODULE_VERSION
);
3434 KDBG(3, SN9C102_MODULE_AUTHOR
);
3436 if ((err
= usb_register(&sn9c102_usb_driver
)))
3437 KDBG(1, "usb_register() failed");
3443 static void __exit
sn9c102_module_exit(void)
3445 usb_deregister(&sn9c102_usb_driver
);
3449 module_init(sn9c102_module_init
);
3450 module_exit(sn9c102_module_exit
);