1 /***************************************************************************
2 * V4L2 driver for ET61X[12]51 PC Camera Controllers *
4 * Copyright (C) 2006-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 ET61X251_MODULE_NAME "V4L2 driver for ET61X[12]51 " \
47 "PC Camera Controllers"
48 #define ET61X251_MODULE_AUTHOR "(C) 2006-2007 Luca Risolia"
49 #define ET61X251_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
50 #define ET61X251_MODULE_LICENSE "GPL"
51 #define ET61X251_MODULE_VERSION "1:1.09"
52 #define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 9)
54 /*****************************************************************************/
56 MODULE_DEVICE_TABLE(usb
, et61x251_id_table
);
58 MODULE_AUTHOR(ET61X251_MODULE_AUTHOR
" " ET61X251_AUTHOR_EMAIL
);
59 MODULE_DESCRIPTION(ET61X251_MODULE_NAME
);
60 MODULE_VERSION(ET61X251_MODULE_VERSION
);
61 MODULE_LICENSE(ET61X251_MODULE_LICENSE
);
63 static short video_nr
[] = {[0 ... ET61X251_MAX_DEVICES
-1] = -1};
64 module_param_array(video_nr
, short, NULL
, 0444);
65 MODULE_PARM_DESC(video_nr
,
66 "\n<-1|n[,...]> Specify V4L2 minor mode number."
67 "\n -1 = use next available (default)"
68 "\n n = use minor number n (integer >= 0)"
69 "\nYou can specify up to "
70 __MODULE_STRING(ET61X251_MAX_DEVICES
) " cameras this way."
72 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
73 "\nthe second registered camera and use auto for the first"
74 "\none and for every other camera."
77 static short force_munmap
[] = {[0 ... ET61X251_MAX_DEVICES
-1] =
78 ET61X251_FORCE_MUNMAP
};
79 module_param_array(force_munmap
, bool, NULL
, 0444);
80 MODULE_PARM_DESC(force_munmap
,
81 "\n<0|1[,...]> Force the application to unmap previously"
82 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
83 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
84 "\nthis feature. This parameter is specific for each"
86 "\n 0 = do not force memory unmapping"
87 "\n 1 = force memory unmapping (save memory)"
88 "\nDefault value is "__MODULE_STRING(ET61X251_FORCE_MUNMAP
)"."
91 static unsigned int frame_timeout
[] = {[0 ... ET61X251_MAX_DEVICES
-1] =
92 ET61X251_FRAME_TIMEOUT
};
93 module_param_array(frame_timeout
, uint
, NULL
, 0644);
94 MODULE_PARM_DESC(frame_timeout
,
95 "\n<n[,...]> Timeout for a video frame in seconds."
96 "\nThis parameter is specific for each detected camera."
98 __MODULE_STRING(ET61X251_FRAME_TIMEOUT
)"."
101 #ifdef ET61X251_DEBUG
102 static unsigned short debug
= ET61X251_DEBUG_LEVEL
;
103 module_param(debug
, ushort
, 0644);
104 MODULE_PARM_DESC(debug
,
105 "\n<n> Debugging information level, from 0 to 3:"
106 "\n0 = none (use carefully)"
107 "\n1 = critical errors"
108 "\n2 = significant informations"
109 "\n3 = more verbose messages"
110 "\nLevel 3 is useful for testing only, when only "
111 "one device is used."
112 "\nDefault value is "__MODULE_STRING(ET61X251_DEBUG_LEVEL
)"."
116 /*****************************************************************************/
119 et61x251_request_buffers(struct et61x251_device
* cam
, u32 count
,
120 enum et61x251_io_method io
)
122 struct v4l2_pix_format
* p
= &(cam
->sensor
.pix_format
);
123 struct v4l2_rect
* r
= &(cam
->sensor
.cropcap
.bounds
);
124 const size_t imagesize
= cam
->module_param
.force_munmap
||
126 (p
->width
* p
->height
* p
->priv
) / 8 :
127 (r
->width
* r
->height
* p
->priv
) / 8;
131 if (count
> ET61X251_MAX_FRAMES
)
132 count
= ET61X251_MAX_FRAMES
;
134 cam
->nbuffers
= count
;
135 while (cam
->nbuffers
> 0) {
136 if ((buff
= vmalloc_32_user(cam
->nbuffers
*
137 PAGE_ALIGN(imagesize
))))
142 for (i
= 0; i
< cam
->nbuffers
; i
++) {
143 cam
->frame
[i
].bufmem
= buff
+ i
*PAGE_ALIGN(imagesize
);
144 cam
->frame
[i
].buf
.index
= i
;
145 cam
->frame
[i
].buf
.m
.offset
= i
*PAGE_ALIGN(imagesize
);
146 cam
->frame
[i
].buf
.length
= imagesize
;
147 cam
->frame
[i
].buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
148 cam
->frame
[i
].buf
.sequence
= 0;
149 cam
->frame
[i
].buf
.field
= V4L2_FIELD_NONE
;
150 cam
->frame
[i
].buf
.memory
= V4L2_MEMORY_MMAP
;
151 cam
->frame
[i
].buf
.flags
= 0;
154 return cam
->nbuffers
;
158 static void et61x251_release_buffers(struct et61x251_device
* cam
)
161 vfree(cam
->frame
[0].bufmem
);
164 cam
->frame_current
= NULL
;
168 static void et61x251_empty_framequeues(struct et61x251_device
* cam
)
172 INIT_LIST_HEAD(&cam
->inqueue
);
173 INIT_LIST_HEAD(&cam
->outqueue
);
175 for (i
= 0; i
< ET61X251_MAX_FRAMES
; i
++) {
176 cam
->frame
[i
].state
= F_UNUSED
;
177 cam
->frame
[i
].buf
.bytesused
= 0;
182 static void et61x251_requeue_outqueue(struct et61x251_device
* cam
)
184 struct et61x251_frame_t
*i
;
186 list_for_each_entry(i
, &cam
->outqueue
, frame
) {
188 list_add(&i
->frame
, &cam
->inqueue
);
191 INIT_LIST_HEAD(&cam
->outqueue
);
195 static void et61x251_queue_unusedframes(struct et61x251_device
* cam
)
197 unsigned long lock_flags
;
200 for (i
= 0; i
< cam
->nbuffers
; i
++)
201 if (cam
->frame
[i
].state
== F_UNUSED
) {
202 cam
->frame
[i
].state
= F_QUEUED
;
203 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
204 list_add_tail(&cam
->frame
[i
].frame
, &cam
->inqueue
);
205 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
209 /*****************************************************************************/
211 int et61x251_write_reg(struct et61x251_device
* cam
, u8 value
, u16 index
)
213 struct usb_device
* udev
= cam
->usbdev
;
214 u8
* buff
= cam
->control_buffer
;
219 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
220 0, index
, buff
, 1, ET61X251_CTRL_TIMEOUT
);
222 DBG(3, "Failed to write a register (value 0x%02X, index "
223 "0x%02X, error %d)", value
, index
, res
);
231 int et61x251_read_reg(struct et61x251_device
* cam
, u16 index
)
233 struct usb_device
* udev
= cam
->usbdev
;
234 u8
* buff
= cam
->control_buffer
;
237 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), 0x00, 0xc1,
238 0, index
, buff
, 1, ET61X251_CTRL_TIMEOUT
);
240 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
243 return (res
>= 0) ? (int)(*buff
) : -1;
248 et61x251_i2c_wait(struct et61x251_device
* cam
,
249 const struct et61x251_sensor
* sensor
)
253 for (i
= 1; i
<= 8; i
++) {
254 if (sensor
->interface
== ET61X251_I2C_3WIRES
) {
255 r
= et61x251_read_reg(cam
, 0x8e);
256 if (!(r
& 0x02) && (r
>= 0))
259 r
= et61x251_read_reg(cam
, 0x8b);
260 if (!(r
& 0x01) && (r
>= 0))
265 udelay(8*8); /* minimum for sensors at 400kHz */
273 et61x251_i2c_try_read(struct et61x251_device
* cam
,
274 const struct et61x251_sensor
* sensor
, u8 address
)
276 struct usb_device
* udev
= cam
->usbdev
;
277 u8
* data
= cam
->control_buffer
;
281 data
[1] = cam
->sensor
.i2c_slave_id
;
282 data
[2] = cam
->sensor
.rsta
| 0x10;
283 data
[3] = !(et61x251_read_reg(cam
, 0x8b) & 0x02);
284 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
285 0, 0x88, data
, 4, ET61X251_CTRL_TIMEOUT
);
289 err
+= et61x251_i2c_wait(cam
, sensor
);
291 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), 0x00, 0xc1,
292 0, 0x80, data
, 8, ET61X251_CTRL_TIMEOUT
);
297 DBG(3, "I2C read failed for %s image sensor", sensor
->name
);
299 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address
, data
[0]);
301 return err
? -1 : (int)data
[0];
306 et61x251_i2c_try_write(struct et61x251_device
* cam
,
307 const struct et61x251_sensor
* sensor
, u8 address
,
310 struct usb_device
* udev
= cam
->usbdev
;
311 u8
* data
= cam
->control_buffer
;
315 data
[1] = cam
->sensor
.i2c_slave_id
;
316 data
[2] = cam
->sensor
.rsta
| 0x12;
317 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
318 0, 0x88, data
, 3, ET61X251_CTRL_TIMEOUT
);
323 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
324 0, 0x80, data
, 1, ET61X251_CTRL_TIMEOUT
);
328 err
+= et61x251_i2c_wait(cam
, sensor
);
331 DBG(3, "I2C write failed for %s image sensor", sensor
->name
);
333 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address
, value
);
340 et61x251_i2c_raw_write(struct et61x251_device
* cam
, u8 n
, u8 data1
, u8 data2
,
341 u8 data3
, u8 data4
, u8 data5
, u8 data6
, u8 data7
,
342 u8 data8
, u8 address
)
344 struct usb_device
* udev
= cam
->usbdev
;
345 u8
* data
= cam
->control_buffer
;
355 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
356 0, 0x81, data
, n
-1, ET61X251_CTRL_TIMEOUT
);
361 data
[1] = cam
->sensor
.i2c_slave_id
;
362 data
[2] = cam
->sensor
.rsta
| 0x02 | (n
<< 4);
363 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
364 0, 0x88, data
, 3, ET61X251_CTRL_TIMEOUT
);
368 /* Start writing through the serial interface */
370 res
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00, 0x41,
371 0, 0x80, data
, 1, ET61X251_CTRL_TIMEOUT
);
375 err
+= et61x251_i2c_wait(cam
, &cam
->sensor
);
378 DBG(3, "I2C raw write failed for %s image sensor",
381 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
382 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
383 " data6 = 0x%02X, data7 = 0x%02X, data8 = 0x%02X", n
, address
,
384 data1
, data2
, data3
, data4
, data5
, data6
, data7
, data8
);
391 int et61x251_i2c_read(struct et61x251_device
* cam
, u8 address
)
393 return et61x251_i2c_try_read(cam
, &cam
->sensor
, address
);
397 int et61x251_i2c_write(struct et61x251_device
* cam
, u8 address
, u8 value
)
399 return et61x251_i2c_try_write(cam
, &cam
->sensor
, address
, value
);
402 /*****************************************************************************/
404 static void et61x251_urb_complete(struct urb
*urb
)
406 struct et61x251_device
* cam
= urb
->context
;
407 struct et61x251_frame_t
** f
;
412 if (urb
->status
== -ENOENT
)
415 f
= &cam
->frame_current
;
417 if (cam
->stream
== STREAM_INTERRUPT
) {
418 cam
->stream
= STREAM_OFF
;
420 (*f
)->state
= F_QUEUED
;
421 DBG(3, "Stream interrupted");
422 wake_up(&cam
->wait_stream
);
425 if (cam
->state
& DEV_DISCONNECTED
)
428 if (cam
->state
& DEV_MISCONFIGURED
) {
429 wake_up_interruptible(&cam
->wait_frame
);
433 if (cam
->stream
== STREAM_OFF
|| list_empty(&cam
->inqueue
))
437 (*f
) = list_entry(cam
->inqueue
.next
, struct et61x251_frame_t
,
440 imagesize
= (cam
->sensor
.pix_format
.width
*
441 cam
->sensor
.pix_format
.height
*
442 cam
->sensor
.pix_format
.priv
) / 8;
444 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
445 unsigned int len
, status
;
448 const u8 VOID_BYTES
= 6;
451 len
= urb
->iso_frame_desc
[i
].actual_length
;
452 status
= urb
->iso_frame_desc
[i
].status
;
453 pos
= urb
->iso_frame_desc
[i
].offset
+ urb
->transfer_buffer
;
456 DBG(3, "Error in isochronous frame");
457 (*f
)->state
= F_ERROR
;
463 sof
= ((*b1
& 0x3f) == 63);
464 imglen
= ((*b1
& 0xc0) << 2) | *b2
;
466 PDBGG("Isochrnous frame: length %u, #%u i, image length %zu",
469 if ((*f
)->state
== F_QUEUED
|| (*f
)->state
== F_ERROR
)
472 (*f
)->state
= F_GRABBING
;
473 (*f
)->buf
.bytesused
= 0;
474 do_gettimeofday(&(*f
)->buf
.timestamp
);
476 DBG(3, "SOF detected: new video frame");
479 if ((*f
)->state
== F_GRABBING
) {
480 if (sof
&& (*f
)->buf
.bytesused
) {
481 if (cam
->sensor
.pix_format
.pixelformat
==
482 V4L2_PIX_FMT_ET61X251
)
485 DBG(3, "Not expected SOF detected "
487 (unsigned long)(*f
)->buf
.bytesused
);
488 (*f
)->state
= F_ERROR
;
493 if ((*f
)->buf
.bytesused
+ imglen
> imagesize
) {
494 DBG(3, "Video frame size exceeded");
495 (*f
)->state
= F_ERROR
;
501 memcpy((*f
)->bufmem
+(*f
)->buf
.bytesused
, pos
, imglen
);
502 (*f
)->buf
.bytesused
+= imglen
;
504 if ((*f
)->buf
.bytesused
== imagesize
) {
507 b
= (*f
)->buf
.bytesused
;
508 (*f
)->state
= F_DONE
;
509 (*f
)->buf
.sequence
= ++cam
->frame_count
;
510 spin_lock(&cam
->queue_lock
);
511 list_move_tail(&(*f
)->frame
, &cam
->outqueue
);
512 if (!list_empty(&cam
->inqueue
))
513 (*f
) = list_entry(cam
->inqueue
.next
,
514 struct et61x251_frame_t
,
518 spin_unlock(&cam
->queue_lock
);
519 DBG(3, "Video frame captured: : %lu bytes",
526 cam
->sensor
.pix_format
.pixelformat
==
527 V4L2_PIX_FMT_ET61X251
)
534 urb
->dev
= cam
->usbdev
;
535 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
536 if (err
< 0 && err
!= -EPERM
) {
537 cam
->state
|= DEV_MISCONFIGURED
;
538 DBG(1, "usb_submit_urb() failed");
541 wake_up_interruptible(&cam
->wait_frame
);
545 static int et61x251_start_transfer(struct et61x251_device
* cam
)
547 struct usb_device
*udev
= cam
->usbdev
;
549 struct usb_host_interface
* altsetting
= usb_altnum_to_altsetting(
550 usb_ifnum_to_if(udev
, 0),
551 ET61X251_ALTERNATE_SETTING
);
552 const unsigned int psz
= le16_to_cpu(altsetting
->
553 endpoint
[0].desc
.wMaxPacketSize
);
557 for (i
= 0; i
< ET61X251_URBS
; i
++) {
558 cam
->transfer_buffer
[i
] = kzalloc(ET61X251_ISO_PACKETS
* psz
,
560 if (!cam
->transfer_buffer
[i
]) {
562 DBG(1, "Not enough memory");
567 for (i
= 0; i
< ET61X251_URBS
; i
++) {
568 urb
= usb_alloc_urb(ET61X251_ISO_PACKETS
, GFP_KERNEL
);
572 DBG(1, "usb_alloc_urb() failed");
577 urb
->pipe
= usb_rcvisocpipe(udev
, 1);
578 urb
->transfer_flags
= URB_ISO_ASAP
;
579 urb
->number_of_packets
= ET61X251_ISO_PACKETS
;
580 urb
->complete
= et61x251_urb_complete
;
581 urb
->transfer_buffer
= cam
->transfer_buffer
[i
];
582 urb
->transfer_buffer_length
= psz
* ET61X251_ISO_PACKETS
;
584 for (j
= 0; j
< ET61X251_ISO_PACKETS
; j
++) {
585 urb
->iso_frame_desc
[j
].offset
= psz
* j
;
586 urb
->iso_frame_desc
[j
].length
= psz
;
590 err
= et61x251_write_reg(cam
, 0x01, 0x03);
591 err
= et61x251_write_reg(cam
, 0x00, 0x03);
592 err
= et61x251_write_reg(cam
, 0x08, 0x03);
595 DBG(1, "I/O hardware error");
599 err
= usb_set_interface(udev
, 0, ET61X251_ALTERNATE_SETTING
);
601 DBG(1, "usb_set_interface() failed");
605 cam
->frame_current
= NULL
;
607 for (i
= 0; i
< ET61X251_URBS
; i
++) {
608 err
= usb_submit_urb(cam
->urb
[i
], GFP_KERNEL
);
610 for (j
= i
-1; j
>= 0; j
--)
611 usb_kill_urb(cam
->urb
[j
]);
612 DBG(1, "usb_submit_urb() failed, error %d", err
);
620 for (i
= 0; (i
< ET61X251_URBS
) && cam
->urb
[i
]; i
++)
621 usb_free_urb(cam
->urb
[i
]);
624 for (i
= 0; (i
< ET61X251_URBS
) && cam
->transfer_buffer
[i
]; i
++)
625 kfree(cam
->transfer_buffer
[i
]);
631 static int et61x251_stop_transfer(struct et61x251_device
* cam
)
633 struct usb_device
*udev
= cam
->usbdev
;
637 if (cam
->state
& DEV_DISCONNECTED
)
640 for (i
= ET61X251_URBS
-1; i
>= 0; i
--) {
641 usb_kill_urb(cam
->urb
[i
]);
642 usb_free_urb(cam
->urb
[i
]);
643 kfree(cam
->transfer_buffer
[i
]);
646 err
= usb_set_interface(udev
, 0, 0); /* 0 Mb/s */
648 DBG(3, "usb_set_interface() failed");
654 static int et61x251_stream_interrupt(struct et61x251_device
* cam
)
658 cam
->stream
= STREAM_INTERRUPT
;
659 timeout
= wait_event_timeout(cam
->wait_stream
,
660 (cam
->stream
== STREAM_OFF
) ||
661 (cam
->state
& DEV_DISCONNECTED
),
662 ET61X251_URB_TIMEOUT
);
663 if (cam
->state
& DEV_DISCONNECTED
)
665 else if (cam
->stream
!= STREAM_OFF
) {
666 cam
->state
|= DEV_MISCONFIGURED
;
667 DBG(1, "URB timeout reached. The camera is misconfigured. To "
668 "use it, close and open /dev/video%d again.",
676 /*****************************************************************************/
678 #ifdef CONFIG_VIDEO_ADV_DEBUG
679 static u8
et61x251_strtou8(const char* buff
, size_t len
, ssize_t
* count
)
686 strncpy(str
, buff
, len
);
689 strncpy(str
, buff
, 4);
693 val
= simple_strtoul(str
, &endp
, 0);
697 *count
= (ssize_t
)(endp
- str
);
698 if ((*count
) && (len
== *count
+1) && (buff
[*count
] == '\n'))
705 NOTE 1: being inside one of the following methods implies that the v4l
706 device exists for sure (see kobjects and reference counters)
707 NOTE 2: buffers are PAGE_SIZE long
710 static ssize_t
et61x251_show_reg(struct class_device
* cd
, char* buf
)
712 struct et61x251_device
* cam
;
715 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
718 cam
= video_get_drvdata(to_video_device(cd
));
720 mutex_unlock(&et61x251_sysfs_lock
);
724 count
= sprintf(buf
, "%u\n", cam
->sysfs
.reg
);
726 mutex_unlock(&et61x251_sysfs_lock
);
733 et61x251_store_reg(struct class_device
* cd
, const char* buf
, size_t len
)
735 struct et61x251_device
* cam
;
739 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
742 cam
= video_get_drvdata(to_video_device(cd
));
744 mutex_unlock(&et61x251_sysfs_lock
);
748 index
= et61x251_strtou8(buf
, len
, &count
);
749 if (index
> 0x8e || !count
) {
750 mutex_unlock(&et61x251_sysfs_lock
);
754 cam
->sysfs
.reg
= index
;
756 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam
->sysfs
.reg
);
757 DBG(3, "Written bytes: %zd", count
);
759 mutex_unlock(&et61x251_sysfs_lock
);
765 static ssize_t
et61x251_show_val(struct class_device
* cd
, char* buf
)
767 struct et61x251_device
* cam
;
771 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
774 cam
= video_get_drvdata(to_video_device(cd
));
776 mutex_unlock(&et61x251_sysfs_lock
);
780 if ((val
= et61x251_read_reg(cam
, cam
->sysfs
.reg
)) < 0) {
781 mutex_unlock(&et61x251_sysfs_lock
);
785 count
= sprintf(buf
, "%d\n", val
);
787 DBG(3, "Read bytes: %zd", count
);
789 mutex_unlock(&et61x251_sysfs_lock
);
796 et61x251_store_val(struct class_device
* cd
, const char* buf
, size_t len
)
798 struct et61x251_device
* cam
;
803 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
806 cam
= video_get_drvdata(to_video_device(cd
));
808 mutex_unlock(&et61x251_sysfs_lock
);
812 value
= et61x251_strtou8(buf
, len
, &count
);
814 mutex_unlock(&et61x251_sysfs_lock
);
818 err
= et61x251_write_reg(cam
, value
, cam
->sysfs
.reg
);
820 mutex_unlock(&et61x251_sysfs_lock
);
824 DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
825 cam
->sysfs
.reg
, value
);
826 DBG(3, "Written bytes: %zd", count
);
828 mutex_unlock(&et61x251_sysfs_lock
);
834 static ssize_t
et61x251_show_i2c_reg(struct class_device
* cd
, char* buf
)
836 struct et61x251_device
* cam
;
839 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
842 cam
= video_get_drvdata(to_video_device(cd
));
844 mutex_unlock(&et61x251_sysfs_lock
);
848 count
= sprintf(buf
, "%u\n", cam
->sysfs
.i2c_reg
);
850 DBG(3, "Read bytes: %zd", count
);
852 mutex_unlock(&et61x251_sysfs_lock
);
859 et61x251_store_i2c_reg(struct class_device
* cd
, const char* buf
, size_t len
)
861 struct et61x251_device
* cam
;
865 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
868 cam
= video_get_drvdata(to_video_device(cd
));
870 mutex_unlock(&et61x251_sysfs_lock
);
874 index
= et61x251_strtou8(buf
, len
, &count
);
876 mutex_unlock(&et61x251_sysfs_lock
);
880 cam
->sysfs
.i2c_reg
= index
;
882 DBG(2, "Moved sensor register index to 0x%02X", cam
->sysfs
.i2c_reg
);
883 DBG(3, "Written bytes: %zd", count
);
885 mutex_unlock(&et61x251_sysfs_lock
);
891 static ssize_t
et61x251_show_i2c_val(struct class_device
* cd
, char* buf
)
893 struct et61x251_device
* cam
;
897 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
900 cam
= video_get_drvdata(to_video_device(cd
));
902 mutex_unlock(&et61x251_sysfs_lock
);
906 if (!(cam
->sensor
.sysfs_ops
& ET61X251_I2C_READ
)) {
907 mutex_unlock(&et61x251_sysfs_lock
);
911 if ((val
= et61x251_i2c_read(cam
, cam
->sysfs
.i2c_reg
)) < 0) {
912 mutex_unlock(&et61x251_sysfs_lock
);
916 count
= sprintf(buf
, "%d\n", val
);
918 DBG(3, "Read bytes: %zd", count
);
920 mutex_unlock(&et61x251_sysfs_lock
);
927 et61x251_store_i2c_val(struct class_device
* cd
, const char* buf
, size_t len
)
929 struct et61x251_device
* cam
;
934 if (mutex_lock_interruptible(&et61x251_sysfs_lock
))
937 cam
= video_get_drvdata(to_video_device(cd
));
939 mutex_unlock(&et61x251_sysfs_lock
);
943 if (!(cam
->sensor
.sysfs_ops
& ET61X251_I2C_READ
)) {
944 mutex_unlock(&et61x251_sysfs_lock
);
948 value
= et61x251_strtou8(buf
, len
, &count
);
950 mutex_unlock(&et61x251_sysfs_lock
);
954 err
= et61x251_i2c_write(cam
, cam
->sysfs
.i2c_reg
, value
);
956 mutex_unlock(&et61x251_sysfs_lock
);
960 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
961 cam
->sysfs
.i2c_reg
, value
);
962 DBG(3, "Written bytes: %zd", count
);
964 mutex_unlock(&et61x251_sysfs_lock
);
970 static CLASS_DEVICE_ATTR(reg
, S_IRUGO
| S_IWUSR
,
971 et61x251_show_reg
, et61x251_store_reg
);
972 static CLASS_DEVICE_ATTR(val
, S_IRUGO
| S_IWUSR
,
973 et61x251_show_val
, et61x251_store_val
);
974 static CLASS_DEVICE_ATTR(i2c_reg
, S_IRUGO
| S_IWUSR
,
975 et61x251_show_i2c_reg
, et61x251_store_i2c_reg
);
976 static CLASS_DEVICE_ATTR(i2c_val
, S_IRUGO
| S_IWUSR
,
977 et61x251_show_i2c_val
, et61x251_store_i2c_val
);
980 static int et61x251_create_sysfs(struct et61x251_device
* cam
)
982 struct class_device
*classdev
= &(cam
->v4ldev
->class_dev
);
985 if ((err
= class_device_create_file(classdev
, &class_device_attr_reg
)))
987 if ((err
= class_device_create_file(classdev
, &class_device_attr_val
)))
990 if (cam
->sensor
.sysfs_ops
) {
991 if ((err
= class_device_create_file(classdev
,
992 &class_device_attr_i2c_reg
)))
994 if ((err
= class_device_create_file(classdev
,
995 &class_device_attr_i2c_val
)))
1000 if (cam
->sensor
.sysfs_ops
)
1001 class_device_remove_file(classdev
, &class_device_attr_i2c_reg
);
1003 class_device_remove_file(classdev
, &class_device_attr_val
);
1005 class_device_remove_file(classdev
, &class_device_attr_reg
);
1009 #endif /* CONFIG_VIDEO_ADV_DEBUG */
1011 /*****************************************************************************/
1014 et61x251_set_pix_format(struct et61x251_device
* cam
,
1015 struct v4l2_pix_format
* pix
)
1019 if ((r
= et61x251_read_reg(cam
, 0x12)) < 0)
1021 if (pix
->pixelformat
== V4L2_PIX_FMT_ET61X251
)
1022 err
+= et61x251_write_reg(cam
, r
& 0xfd, 0x12);
1024 err
+= et61x251_write_reg(cam
, r
| 0x02, 0x12);
1026 return err
? -EIO
: 0;
1031 et61x251_set_compression(struct et61x251_device
* cam
,
1032 struct v4l2_jpegcompression
* compression
)
1036 if ((r
= et61x251_read_reg(cam
, 0x12)) < 0)
1038 if (compression
->quality
== 0)
1039 err
+= et61x251_write_reg(cam
, r
& 0xfb, 0x12);
1041 err
+= et61x251_write_reg(cam
, r
| 0x04, 0x12);
1043 return err
? -EIO
: 0;
1047 static int et61x251_set_scale(struct et61x251_device
* cam
, u8 scale
)
1051 r
= et61x251_read_reg(cam
, 0x12);
1056 err
+= et61x251_write_reg(cam
, r
& ~0x01, 0x12);
1057 else if (scale
== 2)
1058 err
+= et61x251_write_reg(cam
, r
| 0x01, 0x12);
1063 PDBGG("Scaling factor: %u", scale
);
1070 et61x251_set_crop(struct et61x251_device
* cam
, struct v4l2_rect
* rect
)
1072 struct et61x251_sensor
* s
= &cam
->sensor
;
1073 u16 fmw_sx
= (u16
)(rect
->left
- s
->cropcap
.bounds
.left
+
1074 s
->active_pixel
.left
),
1075 fmw_sy
= (u16
)(rect
->top
- s
->cropcap
.bounds
.top
+
1076 s
->active_pixel
.top
),
1077 fmw_length
= (u16
)(rect
->width
),
1078 fmw_height
= (u16
)(rect
->height
);
1081 err
+= et61x251_write_reg(cam
, fmw_sx
& 0xff, 0x69);
1082 err
+= et61x251_write_reg(cam
, fmw_sy
& 0xff, 0x6a);
1083 err
+= et61x251_write_reg(cam
, fmw_length
& 0xff, 0x6b);
1084 err
+= et61x251_write_reg(cam
, fmw_height
& 0xff, 0x6c);
1085 err
+= et61x251_write_reg(cam
, (fmw_sx
>> 8) | ((fmw_sy
& 0x300) >> 6)
1086 | ((fmw_length
& 0x300) >> 4)
1087 | ((fmw_height
& 0x300) >> 2), 0x6d);
1091 PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
1092 fmw_sx
, fmw_sy
, fmw_length
, fmw_height
);
1098 static int et61x251_init(struct et61x251_device
* cam
)
1100 struct et61x251_sensor
* s
= &cam
->sensor
;
1101 struct v4l2_control ctrl
;
1102 struct v4l2_queryctrl
*qctrl
;
1103 struct v4l2_rect
* rect
;
1107 if (!(cam
->state
& DEV_INITIALIZED
)) {
1108 mutex_init(&cam
->open_mutex
);
1109 init_waitqueue_head(&cam
->wait_open
);
1111 rect
= &(s
->cropcap
.defrect
);
1112 cam
->compression
.quality
= ET61X251_COMPRESSION_QUALITY
;
1113 } else { /* use current values */
1118 err
+= et61x251_set_scale(cam
, rect
->width
/ s
->pix_format
.width
);
1119 err
+= et61x251_set_crop(cam
, rect
);
1126 DBG(3, "Sensor initialization failed");
1131 err
+= et61x251_set_compression(cam
, &cam
->compression
);
1132 err
+= et61x251_set_pix_format(cam
, &s
->pix_format
);
1133 if (s
->set_pix_format
)
1134 err
+= s
->set_pix_format(cam
, &s
->pix_format
);
1138 if (s
->pix_format
.pixelformat
== V4L2_PIX_FMT_ET61X251
)
1139 DBG(3, "Compressed video format is active, quality %d",
1140 cam
->compression
.quality
);
1142 DBG(3, "Uncompressed video format is active");
1145 if ((err
= s
->set_crop(cam
, rect
))) {
1146 DBG(3, "set_crop() failed");
1151 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1152 if (s
->qctrl
[i
].id
!= 0 &&
1153 !(s
->qctrl
[i
].flags
& V4L2_CTRL_FLAG_DISABLED
)) {
1154 ctrl
.id
= s
->qctrl
[i
].id
;
1155 ctrl
.value
= qctrl
[i
].default_value
;
1156 err
= s
->set_ctrl(cam
, &ctrl
);
1158 DBG(3, "Set %s control failed",
1162 DBG(3, "Image sensor supports '%s' control",
1167 if (!(cam
->state
& DEV_INITIALIZED
)) {
1168 mutex_init(&cam
->fileop_mutex
);
1169 spin_lock_init(&cam
->queue_lock
);
1170 init_waitqueue_head(&cam
->wait_frame
);
1171 init_waitqueue_head(&cam
->wait_stream
);
1172 cam
->nreadbuffers
= 2;
1173 memcpy(s
->_qctrl
, s
->qctrl
, sizeof(s
->qctrl
));
1174 memcpy(&(s
->_rect
), &(s
->cropcap
.defrect
),
1175 sizeof(struct v4l2_rect
));
1176 cam
->state
|= DEV_INITIALIZED
;
1179 DBG(2, "Initialization succeeded");
1183 /*****************************************************************************/
1185 static void et61x251_release_resources(struct kref
*kref
)
1187 struct et61x251_device
*cam
;
1189 mutex_lock(&et61x251_sysfs_lock
);
1191 cam
= container_of(kref
, struct et61x251_device
, kref
);
1193 DBG(2, "V4L2 device /dev/video%d deregistered", cam
->v4ldev
->minor
);
1194 video_set_drvdata(cam
->v4ldev
, NULL
);
1195 video_unregister_device(cam
->v4ldev
);
1196 usb_put_dev(cam
->usbdev
);
1197 kfree(cam
->control_buffer
);
1200 mutex_unlock(&et61x251_sysfs_lock
);
1204 static int et61x251_open(struct inode
* inode
, struct file
* filp
)
1206 struct et61x251_device
* cam
;
1209 if (!down_read_trylock(&et61x251_dev_lock
))
1210 return -ERESTARTSYS
;
1212 cam
= video_get_drvdata(video_devdata(filp
));
1214 if (wait_for_completion_interruptible(&cam
->probe
)) {
1215 up_read(&et61x251_dev_lock
);
1216 return -ERESTARTSYS
;
1219 kref_get(&cam
->kref
);
1221 if (mutex_lock_interruptible(&cam
->open_mutex
)) {
1222 kref_put(&cam
->kref
, et61x251_release_resources
);
1223 up_read(&et61x251_dev_lock
);
1224 return -ERESTARTSYS
;
1227 if (cam
->state
& DEV_DISCONNECTED
) {
1228 DBG(1, "Device not present");
1234 DBG(2, "Device /dev/video%d is already in use",
1235 cam
->v4ldev
->minor
);
1236 DBG(3, "Simultaneous opens are not supported");
1237 if ((filp
->f_flags
& O_NONBLOCK
) ||
1238 (filp
->f_flags
& O_NDELAY
)) {
1242 DBG(2, "A blocking open() has been requested. Wait for the "
1243 "device to be released...");
1244 up_read(&et61x251_dev_lock
);
1245 err
= wait_event_interruptible_exclusive(cam
->wait_open
,
1246 (cam
->state
& DEV_DISCONNECTED
)
1248 down_read(&et61x251_dev_lock
);
1251 if (cam
->state
& DEV_DISCONNECTED
) {
1257 if (cam
->state
& DEV_MISCONFIGURED
) {
1258 err
= et61x251_init(cam
);
1260 DBG(1, "Initialization failed again. "
1261 "I will retry on next open().");
1264 cam
->state
&= ~DEV_MISCONFIGURED
;
1267 if ((err
= et61x251_start_transfer(cam
)))
1270 filp
->private_data
= cam
;
1273 cam
->stream
= STREAM_OFF
;
1275 cam
->frame_count
= 0;
1276 et61x251_empty_framequeues(cam
);
1278 DBG(3, "Video device /dev/video%d is open", cam
->v4ldev
->minor
);
1281 mutex_unlock(&cam
->open_mutex
);
1283 kref_put(&cam
->kref
, et61x251_release_resources
);
1284 up_read(&et61x251_dev_lock
);
1289 static int et61x251_release(struct inode
* inode
, struct file
* filp
)
1291 struct et61x251_device
* cam
;
1293 down_write(&et61x251_dev_lock
);
1295 cam
= video_get_drvdata(video_devdata(filp
));
1297 et61x251_stop_transfer(cam
);
1298 et61x251_release_buffers(cam
);
1300 wake_up_interruptible_nr(&cam
->wait_open
, 1);
1302 DBG(3, "Video device /dev/video%d closed", cam
->v4ldev
->minor
);
1304 kref_put(&cam
->kref
, et61x251_release_resources
);
1306 up_write(&et61x251_dev_lock
);
1313 et61x251_read(struct file
* filp
, char __user
* buf
,
1314 size_t count
, loff_t
* f_pos
)
1316 struct et61x251_device
* cam
= video_get_drvdata(video_devdata(filp
));
1317 struct et61x251_frame_t
* f
, * i
;
1318 unsigned long lock_flags
;
1322 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
1323 return -ERESTARTSYS
;
1325 if (cam
->state
& DEV_DISCONNECTED
) {
1326 DBG(1, "Device not present");
1327 mutex_unlock(&cam
->fileop_mutex
);
1331 if (cam
->state
& DEV_MISCONFIGURED
) {
1332 DBG(1, "The camera is misconfigured. Close and open it "
1334 mutex_unlock(&cam
->fileop_mutex
);
1338 if (cam
->io
== IO_MMAP
) {
1339 DBG(3, "Close and open the device again to choose the read "
1341 mutex_unlock(&cam
->fileop_mutex
);
1345 if (cam
->io
== IO_NONE
) {
1346 if (!et61x251_request_buffers(cam
, cam
->nreadbuffers
,
1348 DBG(1, "read() failed, not enough memory");
1349 mutex_unlock(&cam
->fileop_mutex
);
1353 cam
->stream
= STREAM_ON
;
1356 if (list_empty(&cam
->inqueue
)) {
1357 if (!list_empty(&cam
->outqueue
))
1358 et61x251_empty_framequeues(cam
);
1359 et61x251_queue_unusedframes(cam
);
1363 mutex_unlock(&cam
->fileop_mutex
);
1367 if (list_empty(&cam
->outqueue
)) {
1368 if (filp
->f_flags
& O_NONBLOCK
) {
1369 mutex_unlock(&cam
->fileop_mutex
);
1372 timeout
= wait_event_interruptible_timeout
1374 (!list_empty(&cam
->outqueue
)) ||
1375 (cam
->state
& DEV_DISCONNECTED
) ||
1376 (cam
->state
& DEV_MISCONFIGURED
),
1377 cam
->module_param
.frame_timeout
*
1378 1000 * msecs_to_jiffies(1) );
1380 mutex_unlock(&cam
->fileop_mutex
);
1383 if (cam
->state
& DEV_DISCONNECTED
) {
1384 mutex_unlock(&cam
->fileop_mutex
);
1387 if (!timeout
|| (cam
->state
& DEV_MISCONFIGURED
)) {
1388 mutex_unlock(&cam
->fileop_mutex
);
1393 f
= list_entry(cam
->outqueue
.prev
, struct et61x251_frame_t
, frame
);
1395 if (count
> f
->buf
.bytesused
)
1396 count
= f
->buf
.bytesused
;
1398 if (copy_to_user(buf
, f
->bufmem
, count
)) {
1405 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
1406 list_for_each_entry(i
, &cam
->outqueue
, frame
)
1407 i
->state
= F_UNUSED
;
1408 INIT_LIST_HEAD(&cam
->outqueue
);
1409 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
1411 et61x251_queue_unusedframes(cam
);
1413 PDBGG("Frame #%lu, bytes read: %zu",
1414 (unsigned long)f
->buf
.index
, count
);
1416 mutex_unlock(&cam
->fileop_mutex
);
1418 return err
? err
: count
;
1422 static unsigned int et61x251_poll(struct file
*filp
, poll_table
*wait
)
1424 struct et61x251_device
* cam
= video_get_drvdata(video_devdata(filp
));
1425 struct et61x251_frame_t
* f
;
1426 unsigned long lock_flags
;
1427 unsigned int mask
= 0;
1429 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
1432 if (cam
->state
& DEV_DISCONNECTED
) {
1433 DBG(1, "Device not present");
1437 if (cam
->state
& DEV_MISCONFIGURED
) {
1438 DBG(1, "The camera is misconfigured. Close and open it "
1443 if (cam
->io
== IO_NONE
) {
1444 if (!et61x251_request_buffers(cam
, cam
->nreadbuffers
,
1446 DBG(1, "poll() failed, not enough memory");
1450 cam
->stream
= STREAM_ON
;
1453 if (cam
->io
== IO_READ
) {
1454 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
1455 list_for_each_entry(f
, &cam
->outqueue
, frame
)
1456 f
->state
= F_UNUSED
;
1457 INIT_LIST_HEAD(&cam
->outqueue
);
1458 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
1459 et61x251_queue_unusedframes(cam
);
1462 poll_wait(filp
, &cam
->wait_frame
, wait
);
1464 if (!list_empty(&cam
->outqueue
))
1465 mask
|= POLLIN
| POLLRDNORM
;
1467 mutex_unlock(&cam
->fileop_mutex
);
1472 mutex_unlock(&cam
->fileop_mutex
);
1477 static void et61x251_vm_open(struct vm_area_struct
* vma
)
1479 struct et61x251_frame_t
* f
= vma
->vm_private_data
;
1484 static void et61x251_vm_close(struct vm_area_struct
* vma
)
1486 /* NOTE: buffers are not freed here */
1487 struct et61x251_frame_t
* f
= vma
->vm_private_data
;
1492 static struct vm_operations_struct et61x251_vm_ops
= {
1493 .open
= et61x251_vm_open
,
1494 .close
= et61x251_vm_close
,
1498 static int et61x251_mmap(struct file
* filp
, struct vm_area_struct
*vma
)
1500 struct et61x251_device
* cam
= video_get_drvdata(video_devdata(filp
));
1501 unsigned long size
= vma
->vm_end
- vma
->vm_start
,
1502 start
= vma
->vm_start
;
1506 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
1507 return -ERESTARTSYS
;
1509 if (cam
->state
& DEV_DISCONNECTED
) {
1510 DBG(1, "Device not present");
1511 mutex_unlock(&cam
->fileop_mutex
);
1515 if (cam
->state
& DEV_MISCONFIGURED
) {
1516 DBG(1, "The camera is misconfigured. Close and open it "
1518 mutex_unlock(&cam
->fileop_mutex
);
1522 if (!(vma
->vm_flags
& (VM_WRITE
| VM_READ
))) {
1523 mutex_unlock(&cam
->fileop_mutex
);
1527 if (cam
->io
!= IO_MMAP
||
1528 size
!= PAGE_ALIGN(cam
->frame
[0].buf
.length
)) {
1529 mutex_unlock(&cam
->fileop_mutex
);
1533 for (i
= 0; i
< cam
->nbuffers
; i
++) {
1534 if ((cam
->frame
[i
].buf
.m
.offset
>>PAGE_SHIFT
) == vma
->vm_pgoff
)
1537 if (i
== cam
->nbuffers
) {
1538 mutex_unlock(&cam
->fileop_mutex
);
1542 vma
->vm_flags
|= VM_IO
;
1543 vma
->vm_flags
|= VM_RESERVED
;
1545 pos
= cam
->frame
[i
].bufmem
;
1546 while (size
> 0) { /* size is page-aligned */
1547 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
1548 mutex_unlock(&cam
->fileop_mutex
);
1556 vma
->vm_ops
= &et61x251_vm_ops
;
1557 vma
->vm_private_data
= &cam
->frame
[i
];
1558 et61x251_vm_open(vma
);
1560 mutex_unlock(&cam
->fileop_mutex
);
1565 /*****************************************************************************/
1568 et61x251_vidioc_querycap(struct et61x251_device
* cam
, void __user
* arg
)
1570 struct v4l2_capability cap
= {
1571 .driver
= "et61x251",
1572 .version
= ET61X251_MODULE_VERSION_CODE
,
1573 .capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
1577 strlcpy(cap
.card
, cam
->v4ldev
->name
, sizeof(cap
.card
));
1578 if (usb_make_path(cam
->usbdev
, cap
.bus_info
, sizeof(cap
.bus_info
)) < 0)
1579 strlcpy(cap
.bus_info
, cam
->usbdev
->dev
.bus_id
,
1580 sizeof(cap
.bus_info
));
1582 if (copy_to_user(arg
, &cap
, sizeof(cap
)))
1590 et61x251_vidioc_enuminput(struct et61x251_device
* cam
, void __user
* arg
)
1592 struct v4l2_input i
;
1594 if (copy_from_user(&i
, arg
, sizeof(i
)))
1600 memset(&i
, 0, sizeof(i
));
1601 strcpy(i
.name
, "Camera");
1602 i
.type
= V4L2_INPUT_TYPE_CAMERA
;
1604 if (copy_to_user(arg
, &i
, sizeof(i
)))
1612 et61x251_vidioc_g_input(struct et61x251_device
* cam
, void __user
* arg
)
1616 if (copy_to_user(arg
, &index
, sizeof(index
)))
1624 et61x251_vidioc_s_input(struct et61x251_device
* cam
, void __user
* arg
)
1628 if (copy_from_user(&index
, arg
, sizeof(index
)))
1639 et61x251_vidioc_query_ctrl(struct et61x251_device
* cam
, void __user
* arg
)
1641 struct et61x251_sensor
* s
= &cam
->sensor
;
1642 struct v4l2_queryctrl qc
;
1645 if (copy_from_user(&qc
, arg
, sizeof(qc
)))
1648 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1649 if (qc
.id
&& qc
.id
== s
->qctrl
[i
].id
) {
1650 memcpy(&qc
, &(s
->qctrl
[i
]), sizeof(qc
));
1651 if (copy_to_user(arg
, &qc
, sizeof(qc
)))
1661 et61x251_vidioc_g_ctrl(struct et61x251_device
* cam
, void __user
* arg
)
1663 struct et61x251_sensor
* s
= &cam
->sensor
;
1664 struct v4l2_control ctrl
;
1668 if (!s
->get_ctrl
&& !s
->set_ctrl
)
1671 if (copy_from_user(&ctrl
, arg
, sizeof(ctrl
)))
1675 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1676 if (ctrl
.id
== s
->qctrl
[i
].id
) {
1677 ctrl
.value
= s
->_qctrl
[i
].default_value
;
1682 err
= s
->get_ctrl(cam
, &ctrl
);
1685 if (copy_to_user(arg
, &ctrl
, sizeof(ctrl
)))
1693 et61x251_vidioc_s_ctrl(struct et61x251_device
* cam
, void __user
* arg
)
1695 struct et61x251_sensor
* s
= &cam
->sensor
;
1696 struct v4l2_control ctrl
;
1703 if (copy_from_user(&ctrl
, arg
, sizeof(ctrl
)))
1706 for (i
= 0; i
< ARRAY_SIZE(s
->qctrl
); i
++)
1707 if (ctrl
.id
== s
->qctrl
[i
].id
) {
1708 if (s
->qctrl
[i
].flags
& V4L2_CTRL_FLAG_DISABLED
)
1710 if (ctrl
.value
< s
->qctrl
[i
].minimum
||
1711 ctrl
.value
> s
->qctrl
[i
].maximum
)
1713 ctrl
.value
-= ctrl
.value
% s
->qctrl
[i
].step
;
1717 if ((err
= s
->set_ctrl(cam
, &ctrl
)))
1720 s
->_qctrl
[i
].default_value
= ctrl
.value
;
1727 et61x251_vidioc_cropcap(struct et61x251_device
* cam
, void __user
* arg
)
1729 struct v4l2_cropcap
* cc
= &(cam
->sensor
.cropcap
);
1731 cc
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1732 cc
->pixelaspect
.numerator
= 1;
1733 cc
->pixelaspect
.denominator
= 1;
1735 if (copy_to_user(arg
, cc
, sizeof(*cc
)))
1743 et61x251_vidioc_g_crop(struct et61x251_device
* cam
, void __user
* arg
)
1745 struct et61x251_sensor
* s
= &cam
->sensor
;
1746 struct v4l2_crop crop
= {
1747 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1750 memcpy(&(crop
.c
), &(s
->_rect
), sizeof(struct v4l2_rect
));
1752 if (copy_to_user(arg
, &crop
, sizeof(crop
)))
1760 et61x251_vidioc_s_crop(struct et61x251_device
* cam
, void __user
* arg
)
1762 struct et61x251_sensor
* s
= &cam
->sensor
;
1763 struct v4l2_crop crop
;
1764 struct v4l2_rect
* rect
;
1765 struct v4l2_rect
* bounds
= &(s
->cropcap
.bounds
);
1766 struct v4l2_pix_format
* pix_format
= &(s
->pix_format
);
1768 const enum et61x251_stream_state stream
= cam
->stream
;
1769 const u32 nbuffers
= cam
->nbuffers
;
1773 if (copy_from_user(&crop
, arg
, sizeof(crop
)))
1778 if (crop
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1781 if (cam
->module_param
.force_munmap
)
1782 for (i
= 0; i
< cam
->nbuffers
; i
++)
1783 if (cam
->frame
[i
].vma_use_count
) {
1784 DBG(3, "VIDIOC_S_CROP failed. "
1785 "Unmap the buffers first.");
1789 /* Preserve R,G or B origin */
1790 rect
->left
= (s
->_rect
.left
& 1L) ? rect
->left
| 1L : rect
->left
& ~1L;
1791 rect
->top
= (s
->_rect
.top
& 1L) ? rect
->top
| 1L : rect
->top
& ~1L;
1793 if (rect
->width
< 16)
1795 if (rect
->height
< 16)
1797 if (rect
->width
> bounds
->width
)
1798 rect
->width
= bounds
->width
;
1799 if (rect
->height
> bounds
->height
)
1800 rect
->height
= bounds
->height
;
1801 if (rect
->left
< bounds
->left
)
1802 rect
->left
= bounds
->left
;
1803 if (rect
->top
< bounds
->top
)
1804 rect
->top
= bounds
->top
;
1805 if (rect
->left
+ rect
->width
> bounds
->left
+ bounds
->width
)
1806 rect
->left
= bounds
->left
+bounds
->width
- rect
->width
;
1807 if (rect
->top
+ rect
->height
> bounds
->top
+ bounds
->height
)
1808 rect
->top
= bounds
->top
+bounds
->height
- rect
->height
;
1810 rect
->width
&= ~15L;
1811 rect
->height
&= ~15L;
1813 if (ET61X251_PRESERVE_IMGSCALE
) {
1814 /* Calculate the actual scaling factor */
1816 a
= rect
->width
* rect
->height
;
1817 b
= pix_format
->width
* pix_format
->height
;
1818 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : 2) : 1;
1822 if (cam
->stream
== STREAM_ON
)
1823 if ((err
= et61x251_stream_interrupt(cam
)))
1826 if (copy_to_user(arg
, &crop
, sizeof(crop
))) {
1827 cam
->stream
= stream
;
1831 if (cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
)
1832 et61x251_release_buffers(cam
);
1834 err
= et61x251_set_crop(cam
, rect
);
1836 err
+= s
->set_crop(cam
, rect
);
1837 err
+= et61x251_set_scale(cam
, scale
);
1839 if (err
) { /* atomic, no rollback in ioctl() */
1840 cam
->state
|= DEV_MISCONFIGURED
;
1841 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1842 "use the camera, close and open /dev/video%d again.",
1843 cam
->v4ldev
->minor
);
1847 s
->pix_format
.width
= rect
->width
/scale
;
1848 s
->pix_format
.height
= rect
->height
/scale
;
1849 memcpy(&(s
->_rect
), rect
, sizeof(*rect
));
1851 if ((cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
) &&
1852 nbuffers
!= et61x251_request_buffers(cam
, nbuffers
, cam
->io
)) {
1853 cam
->state
|= DEV_MISCONFIGURED
;
1854 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1855 "use the camera, close and open /dev/video%d again.",
1856 cam
->v4ldev
->minor
);
1860 if (cam
->io
== IO_READ
)
1861 et61x251_empty_framequeues(cam
);
1862 else if (cam
->module_param
.force_munmap
)
1863 et61x251_requeue_outqueue(cam
);
1865 cam
->stream
= stream
;
1872 et61x251_vidioc_enum_framesizes(struct et61x251_device
* cam
, void __user
* arg
)
1874 struct v4l2_frmsizeenum frmsize
;
1876 if (copy_from_user(&frmsize
, arg
, sizeof(frmsize
)))
1879 if (frmsize
.index
!= 0)
1882 if (frmsize
.pixel_format
!= V4L2_PIX_FMT_ET61X251
&&
1883 frmsize
.pixel_format
!= V4L2_PIX_FMT_SBGGR8
)
1886 frmsize
.type
= V4L2_FRMSIZE_TYPE_STEPWISE
;
1887 frmsize
.stepwise
.min_width
= frmsize
.stepwise
.step_width
= 16;
1888 frmsize
.stepwise
.min_height
= frmsize
.stepwise
.step_height
= 16;
1889 frmsize
.stepwise
.max_width
= cam
->sensor
.cropcap
.bounds
.width
;
1890 frmsize
.stepwise
.max_height
= cam
->sensor
.cropcap
.bounds
.height
;
1891 memset(&frmsize
.reserved
, 0, sizeof(frmsize
.reserved
));
1893 if (copy_to_user(arg
, &frmsize
, sizeof(frmsize
)))
1901 et61x251_vidioc_enum_fmt(struct et61x251_device
* cam
, void __user
* arg
)
1903 struct v4l2_fmtdesc fmtd
;
1905 if (copy_from_user(&fmtd
, arg
, sizeof(fmtd
)))
1908 if (fmtd
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1911 if (fmtd
.index
== 0) {
1912 strcpy(fmtd
.description
, "bayer rgb");
1913 fmtd
.pixelformat
= V4L2_PIX_FMT_SBGGR8
;
1914 } else if (fmtd
.index
== 1) {
1915 strcpy(fmtd
.description
, "compressed");
1916 fmtd
.pixelformat
= V4L2_PIX_FMT_ET61X251
;
1917 fmtd
.flags
= V4L2_FMT_FLAG_COMPRESSED
;
1921 fmtd
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1922 memset(&fmtd
.reserved
, 0, sizeof(fmtd
.reserved
));
1924 if (copy_to_user(arg
, &fmtd
, sizeof(fmtd
)))
1932 et61x251_vidioc_g_fmt(struct et61x251_device
* cam
, void __user
* arg
)
1934 struct v4l2_format format
;
1935 struct v4l2_pix_format
* pfmt
= &(cam
->sensor
.pix_format
);
1937 if (copy_from_user(&format
, arg
, sizeof(format
)))
1940 if (format
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1943 pfmt
->colorspace
= (pfmt
->pixelformat
== V4L2_PIX_FMT_ET61X251
) ?
1944 0 : V4L2_COLORSPACE_SRGB
;
1945 pfmt
->bytesperline
= (pfmt
->pixelformat
==V4L2_PIX_FMT_ET61X251
)
1946 ? 0 : (pfmt
->width
* pfmt
->priv
) / 8;
1947 pfmt
->sizeimage
= pfmt
->height
* ((pfmt
->width
*pfmt
->priv
)/8);
1948 pfmt
->field
= V4L2_FIELD_NONE
;
1949 memcpy(&(format
.fmt
.pix
), pfmt
, sizeof(*pfmt
));
1951 if (copy_to_user(arg
, &format
, sizeof(format
)))
1959 et61x251_vidioc_try_s_fmt(struct et61x251_device
* cam
, unsigned int cmd
,
1962 struct et61x251_sensor
* s
= &cam
->sensor
;
1963 struct v4l2_format format
;
1964 struct v4l2_pix_format
* pix
;
1965 struct v4l2_pix_format
* pfmt
= &(s
->pix_format
);
1966 struct v4l2_rect
* bounds
= &(s
->cropcap
.bounds
);
1967 struct v4l2_rect rect
;
1969 const enum et61x251_stream_state stream
= cam
->stream
;
1970 const u32 nbuffers
= cam
->nbuffers
;
1974 if (copy_from_user(&format
, arg
, sizeof(format
)))
1977 pix
= &(format
.fmt
.pix
);
1979 if (format
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1982 memcpy(&rect
, &(s
->_rect
), sizeof(rect
));
1984 { /* calculate the actual scaling factor */
1986 a
= rect
.width
* rect
.height
;
1987 b
= pix
->width
* pix
->height
;
1988 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : 2) : 1;
1991 rect
.width
= scale
* pix
->width
;
1992 rect
.height
= scale
* pix
->height
;
1994 if (rect
.width
< 16)
1996 if (rect
.height
< 16)
1998 if (rect
.width
> bounds
->left
+ bounds
->width
- rect
.left
)
1999 rect
.width
= bounds
->left
+ bounds
->width
- rect
.left
;
2000 if (rect
.height
> bounds
->top
+ bounds
->height
- rect
.top
)
2001 rect
.height
= bounds
->top
+ bounds
->height
- rect
.top
;
2004 rect
.height
&= ~15L;
2006 { /* adjust the scaling factor */
2008 a
= rect
.width
* rect
.height
;
2009 b
= pix
->width
* pix
->height
;
2010 scale
= b
? (u8
)((a
/ b
) < 4 ? 1 : 2) : 1;
2013 pix
->width
= rect
.width
/ scale
;
2014 pix
->height
= rect
.height
/ scale
;
2016 if (pix
->pixelformat
!= V4L2_PIX_FMT_ET61X251
&&
2017 pix
->pixelformat
!= V4L2_PIX_FMT_SBGGR8
)
2018 pix
->pixelformat
= pfmt
->pixelformat
;
2019 pix
->priv
= pfmt
->priv
; /* bpp */
2020 pix
->colorspace
= (pix
->pixelformat
== V4L2_PIX_FMT_ET61X251
) ?
2021 0 : V4L2_COLORSPACE_SRGB
;
2022 pix
->colorspace
= pfmt
->colorspace
;
2023 pix
->bytesperline
= (pix
->pixelformat
== V4L2_PIX_FMT_ET61X251
)
2024 ? 0 : (pix
->width
* pix
->priv
) / 8;
2025 pix
->sizeimage
= pix
->height
* ((pix
->width
* pix
->priv
) / 8);
2026 pix
->field
= V4L2_FIELD_NONE
;
2028 if (cmd
== VIDIOC_TRY_FMT
) {
2029 if (copy_to_user(arg
, &format
, sizeof(format
)))
2034 if (cam
->module_param
.force_munmap
)
2035 for (i
= 0; i
< cam
->nbuffers
; i
++)
2036 if (cam
->frame
[i
].vma_use_count
) {
2037 DBG(3, "VIDIOC_S_FMT failed. "
2038 "Unmap the buffers first.");
2042 if (cam
->stream
== STREAM_ON
)
2043 if ((err
= et61x251_stream_interrupt(cam
)))
2046 if (copy_to_user(arg
, &format
, sizeof(format
))) {
2047 cam
->stream
= stream
;
2051 if (cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
)
2052 et61x251_release_buffers(cam
);
2054 err
+= et61x251_set_pix_format(cam
, pix
);
2055 err
+= et61x251_set_crop(cam
, &rect
);
2056 if (s
->set_pix_format
)
2057 err
+= s
->set_pix_format(cam
, pix
);
2059 err
+= s
->set_crop(cam
, &rect
);
2060 err
+= et61x251_set_scale(cam
, scale
);
2062 if (err
) { /* atomic, no rollback in ioctl() */
2063 cam
->state
|= DEV_MISCONFIGURED
;
2064 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2065 "use the camera, close and open /dev/video%d again.",
2066 cam
->v4ldev
->minor
);
2070 memcpy(pfmt
, pix
, sizeof(*pix
));
2071 memcpy(&(s
->_rect
), &rect
, sizeof(rect
));
2073 if ((cam
->module_param
.force_munmap
|| cam
->io
== IO_READ
) &&
2074 nbuffers
!= et61x251_request_buffers(cam
, nbuffers
, cam
->io
)) {
2075 cam
->state
|= DEV_MISCONFIGURED
;
2076 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2077 "use the camera, close and open /dev/video%d again.",
2078 cam
->v4ldev
->minor
);
2082 if (cam
->io
== IO_READ
)
2083 et61x251_empty_framequeues(cam
);
2084 else if (cam
->module_param
.force_munmap
)
2085 et61x251_requeue_outqueue(cam
);
2087 cam
->stream
= stream
;
2094 et61x251_vidioc_g_jpegcomp(struct et61x251_device
* cam
, void __user
* arg
)
2096 if (copy_to_user(arg
, &cam
->compression
,
2097 sizeof(cam
->compression
)))
2105 et61x251_vidioc_s_jpegcomp(struct et61x251_device
* cam
, void __user
* arg
)
2107 struct v4l2_jpegcompression jc
;
2108 const enum et61x251_stream_state stream
= cam
->stream
;
2111 if (copy_from_user(&jc
, arg
, sizeof(jc
)))
2114 if (jc
.quality
!= 0 && jc
.quality
!= 1)
2117 if (cam
->stream
== STREAM_ON
)
2118 if ((err
= et61x251_stream_interrupt(cam
)))
2121 err
+= et61x251_set_compression(cam
, &jc
);
2122 if (err
) { /* atomic, no rollback in ioctl() */
2123 cam
->state
|= DEV_MISCONFIGURED
;
2124 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2125 "problems. To use the camera, close and open "
2126 "/dev/video%d again.", cam
->v4ldev
->minor
);
2130 cam
->compression
.quality
= jc
.quality
;
2132 cam
->stream
= stream
;
2139 et61x251_vidioc_reqbufs(struct et61x251_device
* cam
, void __user
* arg
)
2141 struct v4l2_requestbuffers rb
;
2145 if (copy_from_user(&rb
, arg
, sizeof(rb
)))
2148 if (rb
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2149 rb
.memory
!= V4L2_MEMORY_MMAP
)
2152 if (cam
->io
== IO_READ
) {
2153 DBG(3, "Close and open the device again to choose the mmap "
2158 for (i
= 0; i
< cam
->nbuffers
; i
++)
2159 if (cam
->frame
[i
].vma_use_count
) {
2160 DBG(3, "VIDIOC_REQBUFS failed. "
2161 "Previous buffers are still mapped.");
2165 if (cam
->stream
== STREAM_ON
)
2166 if ((err
= et61x251_stream_interrupt(cam
)))
2169 et61x251_empty_framequeues(cam
);
2171 et61x251_release_buffers(cam
);
2173 rb
.count
= et61x251_request_buffers(cam
, rb
.count
, IO_MMAP
);
2175 if (copy_to_user(arg
, &rb
, sizeof(rb
))) {
2176 et61x251_release_buffers(cam
);
2181 cam
->io
= rb
.count
? IO_MMAP
: IO_NONE
;
2188 et61x251_vidioc_querybuf(struct et61x251_device
* cam
, void __user
* arg
)
2190 struct v4l2_buffer b
;
2192 if (copy_from_user(&b
, arg
, sizeof(b
)))
2195 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2196 b
.index
>= cam
->nbuffers
|| cam
->io
!= IO_MMAP
)
2199 memcpy(&b
, &cam
->frame
[b
.index
].buf
, sizeof(b
));
2201 if (cam
->frame
[b
.index
].vma_use_count
)
2202 b
.flags
|= V4L2_BUF_FLAG_MAPPED
;
2204 if (cam
->frame
[b
.index
].state
== F_DONE
)
2205 b
.flags
|= V4L2_BUF_FLAG_DONE
;
2206 else if (cam
->frame
[b
.index
].state
!= F_UNUSED
)
2207 b
.flags
|= V4L2_BUF_FLAG_QUEUED
;
2209 if (copy_to_user(arg
, &b
, sizeof(b
)))
2217 et61x251_vidioc_qbuf(struct et61x251_device
* cam
, void __user
* arg
)
2219 struct v4l2_buffer b
;
2220 unsigned long lock_flags
;
2222 if (copy_from_user(&b
, arg
, sizeof(b
)))
2225 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2226 b
.index
>= cam
->nbuffers
|| cam
->io
!= IO_MMAP
)
2229 if (cam
->frame
[b
.index
].state
!= F_UNUSED
)
2232 cam
->frame
[b
.index
].state
= F_QUEUED
;
2234 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
2235 list_add_tail(&cam
->frame
[b
.index
].frame
, &cam
->inqueue
);
2236 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
2238 PDBGG("Frame #%lu queued", (unsigned long)b
.index
);
2245 et61x251_vidioc_dqbuf(struct et61x251_device
* cam
, struct file
* filp
,
2248 struct v4l2_buffer b
;
2249 struct et61x251_frame_t
*f
;
2250 unsigned long lock_flags
;
2253 if (copy_from_user(&b
, arg
, sizeof(b
)))
2256 if (b
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2259 if (list_empty(&cam
->outqueue
)) {
2260 if (cam
->stream
== STREAM_OFF
)
2262 if (filp
->f_flags
& O_NONBLOCK
)
2264 timeout
= wait_event_interruptible_timeout
2266 (!list_empty(&cam
->outqueue
)) ||
2267 (cam
->state
& DEV_DISCONNECTED
) ||
2268 (cam
->state
& DEV_MISCONFIGURED
),
2269 cam
->module_param
.frame_timeout
*
2270 1000 * msecs_to_jiffies(1) );
2273 if (cam
->state
& DEV_DISCONNECTED
)
2275 if (!timeout
|| (cam
->state
& DEV_MISCONFIGURED
))
2279 spin_lock_irqsave(&cam
->queue_lock
, lock_flags
);
2280 f
= list_entry(cam
->outqueue
.next
, struct et61x251_frame_t
, frame
);
2281 list_del(cam
->outqueue
.next
);
2282 spin_unlock_irqrestore(&cam
->queue_lock
, lock_flags
);
2284 f
->state
= F_UNUSED
;
2286 memcpy(&b
, &f
->buf
, sizeof(b
));
2287 if (f
->vma_use_count
)
2288 b
.flags
|= V4L2_BUF_FLAG_MAPPED
;
2290 if (copy_to_user(arg
, &b
, sizeof(b
)))
2293 PDBGG("Frame #%lu dequeued", (unsigned long)f
->buf
.index
);
2300 et61x251_vidioc_streamon(struct et61x251_device
* cam
, void __user
* arg
)
2304 if (copy_from_user(&type
, arg
, sizeof(type
)))
2307 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2310 cam
->stream
= STREAM_ON
;
2312 DBG(3, "Stream on");
2319 et61x251_vidioc_streamoff(struct et61x251_device
* cam
, void __user
* arg
)
2323 if (copy_from_user(&type
, arg
, sizeof(type
)))
2326 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
|| cam
->io
!= IO_MMAP
)
2329 if (cam
->stream
== STREAM_ON
)
2330 if ((err
= et61x251_stream_interrupt(cam
)))
2333 et61x251_empty_framequeues(cam
);
2335 DBG(3, "Stream off");
2342 et61x251_vidioc_g_parm(struct et61x251_device
* cam
, void __user
* arg
)
2344 struct v4l2_streamparm sp
;
2346 if (copy_from_user(&sp
, arg
, sizeof(sp
)))
2349 if (sp
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2352 sp
.parm
.capture
.extendedmode
= 0;
2353 sp
.parm
.capture
.readbuffers
= cam
->nreadbuffers
;
2355 if (copy_to_user(arg
, &sp
, sizeof(sp
)))
2363 et61x251_vidioc_s_parm(struct et61x251_device
* cam
, void __user
* arg
)
2365 struct v4l2_streamparm sp
;
2367 if (copy_from_user(&sp
, arg
, sizeof(sp
)))
2370 if (sp
.type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2373 sp
.parm
.capture
.extendedmode
= 0;
2375 if (sp
.parm
.capture
.readbuffers
== 0)
2376 sp
.parm
.capture
.readbuffers
= cam
->nreadbuffers
;
2378 if (sp
.parm
.capture
.readbuffers
> ET61X251_MAX_FRAMES
)
2379 sp
.parm
.capture
.readbuffers
= ET61X251_MAX_FRAMES
;
2381 if (copy_to_user(arg
, &sp
, sizeof(sp
)))
2384 cam
->nreadbuffers
= sp
.parm
.capture
.readbuffers
;
2390 static int et61x251_ioctl_v4l2(struct inode
* inode
, struct file
* filp
,
2391 unsigned int cmd
, void __user
* arg
)
2393 struct et61x251_device
* cam
= video_get_drvdata(video_devdata(filp
));
2397 case VIDIOC_QUERYCAP
:
2398 return et61x251_vidioc_querycap(cam
, arg
);
2400 case VIDIOC_ENUMINPUT
:
2401 return et61x251_vidioc_enuminput(cam
, arg
);
2403 case VIDIOC_G_INPUT
:
2404 return et61x251_vidioc_g_input(cam
, arg
);
2406 case VIDIOC_S_INPUT
:
2407 return et61x251_vidioc_s_input(cam
, arg
);
2409 case VIDIOC_QUERYCTRL
:
2410 return et61x251_vidioc_query_ctrl(cam
, arg
);
2413 return et61x251_vidioc_g_ctrl(cam
, arg
);
2416 return et61x251_vidioc_s_ctrl(cam
, arg
);
2418 case VIDIOC_CROPCAP
:
2419 return et61x251_vidioc_cropcap(cam
, arg
);
2422 return et61x251_vidioc_g_crop(cam
, arg
);
2425 return et61x251_vidioc_s_crop(cam
, arg
);
2427 case VIDIOC_ENUM_FMT
:
2428 return et61x251_vidioc_enum_fmt(cam
, arg
);
2431 return et61x251_vidioc_g_fmt(cam
, arg
);
2433 case VIDIOC_TRY_FMT
:
2435 return et61x251_vidioc_try_s_fmt(cam
, cmd
, arg
);
2437 case VIDIOC_ENUM_FRAMESIZES
:
2438 return et61x251_vidioc_enum_framesizes(cam
, arg
);
2440 case VIDIOC_G_JPEGCOMP
:
2441 return et61x251_vidioc_g_jpegcomp(cam
, arg
);
2443 case VIDIOC_S_JPEGCOMP
:
2444 return et61x251_vidioc_s_jpegcomp(cam
, arg
);
2446 case VIDIOC_REQBUFS
:
2447 return et61x251_vidioc_reqbufs(cam
, arg
);
2449 case VIDIOC_QUERYBUF
:
2450 return et61x251_vidioc_querybuf(cam
, arg
);
2453 return et61x251_vidioc_qbuf(cam
, arg
);
2456 return et61x251_vidioc_dqbuf(cam
, filp
, arg
);
2458 case VIDIOC_STREAMON
:
2459 return et61x251_vidioc_streamon(cam
, arg
);
2461 case VIDIOC_STREAMOFF
:
2462 return et61x251_vidioc_streamoff(cam
, arg
);
2465 return et61x251_vidioc_g_parm(cam
, arg
);
2468 return et61x251_vidioc_s_parm(cam
, arg
);
2472 case VIDIOC_QUERYSTD
:
2473 case VIDIOC_ENUMSTD
:
2474 case VIDIOC_QUERYMENU
:
2475 case VIDIOC_ENUM_FRAMEINTERVALS
:
2485 static int et61x251_ioctl(struct inode
* inode
, struct file
* filp
,
2486 unsigned int cmd
, unsigned long arg
)
2488 struct et61x251_device
* cam
= video_get_drvdata(video_devdata(filp
));
2491 if (mutex_lock_interruptible(&cam
->fileop_mutex
))
2492 return -ERESTARTSYS
;
2494 if (cam
->state
& DEV_DISCONNECTED
) {
2495 DBG(1, "Device not present");
2496 mutex_unlock(&cam
->fileop_mutex
);
2500 if (cam
->state
& DEV_MISCONFIGURED
) {
2501 DBG(1, "The camera is misconfigured. Close and open it "
2503 mutex_unlock(&cam
->fileop_mutex
);
2507 V4LDBG(3, "et61x251", cmd
);
2509 err
= et61x251_ioctl_v4l2(inode
, filp
, cmd
, (void __user
*)arg
);
2511 mutex_unlock(&cam
->fileop_mutex
);
2517 static const struct file_operations et61x251_fops
= {
2518 .owner
= THIS_MODULE
,
2519 .open
= et61x251_open
,
2520 .release
= et61x251_release
,
2521 .ioctl
= et61x251_ioctl
,
2522 .compat_ioctl
= v4l_compat_ioctl32
,
2523 .read
= et61x251_read
,
2524 .poll
= et61x251_poll
,
2525 .mmap
= et61x251_mmap
,
2526 .llseek
= no_llseek
,
2529 /*****************************************************************************/
2531 /* It exists a single interface only. We do not need to validate anything. */
2533 et61x251_usb_probe(struct usb_interface
* intf
, const struct usb_device_id
* id
)
2535 struct usb_device
*udev
= interface_to_usbdev(intf
);
2536 struct et61x251_device
* cam
;
2537 static unsigned int dev_nr
= 0;
2541 if (!(cam
= kzalloc(sizeof(struct et61x251_device
), GFP_KERNEL
)))
2546 if (!(cam
->control_buffer
= kzalloc(8, GFP_KERNEL
))) {
2547 DBG(1, "kmalloc() failed");
2552 if (!(cam
->v4ldev
= video_device_alloc())) {
2553 DBG(1, "video_device_alloc() failed");
2558 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2559 "(vid/pid 0x%04X:0x%04X)",id
->idVendor
, id
->idProduct
);
2561 for (i
= 0; et61x251_sensor_table
[i
]; i
++) {
2562 err
= et61x251_sensor_table
[i
](cam
);
2568 DBG(2, "%s image sensor detected", cam
->sensor
.name
);
2570 DBG(1, "No supported image sensor detected");
2575 if (et61x251_init(cam
)) {
2576 DBG(1, "Initialization failed. I will retry on open().");
2577 cam
->state
|= DEV_MISCONFIGURED
;
2580 strcpy(cam
->v4ldev
->name
, "ET61X[12]51 PC Camera");
2581 cam
->v4ldev
->owner
= THIS_MODULE
;
2582 cam
->v4ldev
->type
= VID_TYPE_CAPTURE
| VID_TYPE_SCALES
;
2583 cam
->v4ldev
->hardware
= 0;
2584 cam
->v4ldev
->fops
= &et61x251_fops
;
2585 cam
->v4ldev
->minor
= video_nr
[dev_nr
];
2586 cam
->v4ldev
->release
= video_device_release
;
2587 video_set_drvdata(cam
->v4ldev
, cam
);
2589 init_completion(&cam
->probe
);
2591 err
= video_register_device(cam
->v4ldev
, VFL_TYPE_GRABBER
,
2594 DBG(1, "V4L2 device registration failed");
2595 if (err
== -ENFILE
&& video_nr
[dev_nr
] == -1)
2596 DBG(1, "Free /dev/videoX node not found");
2597 video_nr
[dev_nr
] = -1;
2598 dev_nr
= (dev_nr
< ET61X251_MAX_DEVICES
-1) ? dev_nr
+1 : 0;
2599 complete_all(&cam
->probe
);
2603 DBG(2, "V4L2 device registered as /dev/video%d", cam
->v4ldev
->minor
);
2605 cam
->module_param
.force_munmap
= force_munmap
[dev_nr
];
2606 cam
->module_param
.frame_timeout
= frame_timeout
[dev_nr
];
2608 dev_nr
= (dev_nr
< ET61X251_MAX_DEVICES
-1) ? dev_nr
+1 : 0;
2610 #ifdef CONFIG_VIDEO_ADV_DEBUG
2611 err
= et61x251_create_sysfs(cam
);
2613 DBG(2, "Optional device control through 'sysfs' "
2616 DBG(2, "Failed to create 'sysfs' interface for optional "
2617 "device controlling. Error #%d", err
);
2619 DBG(2, "Optional device control through 'sysfs' interface disabled");
2620 DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
2621 "configuration option to enable it.");
2624 usb_set_intfdata(intf
, cam
);
2625 kref_init(&cam
->kref
);
2626 usb_get_dev(cam
->usbdev
);
2628 complete_all(&cam
->probe
);
2634 kfree(cam
->control_buffer
);
2636 video_device_release(cam
->v4ldev
);
2643 static void et61x251_usb_disconnect(struct usb_interface
* intf
)
2645 struct et61x251_device
* cam
;
2647 down_write(&et61x251_dev_lock
);
2649 cam
= usb_get_intfdata(intf
);
2651 DBG(2, "Disconnecting %s...", cam
->v4ldev
->name
);
2654 DBG(2, "Device /dev/video%d is open! Deregistration and "
2655 "memory deallocation are deferred.",
2656 cam
->v4ldev
->minor
);
2657 cam
->state
|= DEV_MISCONFIGURED
;
2658 et61x251_stop_transfer(cam
);
2659 cam
->state
|= DEV_DISCONNECTED
;
2660 wake_up_interruptible(&cam
->wait_frame
);
2661 wake_up(&cam
->wait_stream
);
2663 cam
->state
|= DEV_DISCONNECTED
;
2665 wake_up_interruptible_all(&cam
->wait_open
);
2667 kref_put(&cam
->kref
, et61x251_release_resources
);
2669 up_write(&et61x251_dev_lock
);
2673 static struct usb_driver et61x251_usb_driver
= {
2675 .id_table
= et61x251_id_table
,
2676 .probe
= et61x251_usb_probe
,
2677 .disconnect
= et61x251_usb_disconnect
,
2680 /*****************************************************************************/
2682 static int __init
et61x251_module_init(void)
2686 KDBG(2, ET61X251_MODULE_NAME
" v" ET61X251_MODULE_VERSION
);
2687 KDBG(3, ET61X251_MODULE_AUTHOR
);
2689 if ((err
= usb_register(&et61x251_usb_driver
)))
2690 KDBG(1, "usb_register() failed");
2696 static void __exit
et61x251_module_exit(void)
2698 usb_deregister(&et61x251_usb_driver
);
2702 module_init(et61x251_module_init
);
2703 module_exit(et61x251_module_exit
);