3 * @author Nicolas VIVIEN
6 * @brief Driver for Microdia USB video camera
8 * @note Copyright (C) Nicolas VIVIEN
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/version.h>
31 #include <linux/errno.h>
32 #include <linux/slab.h>
33 #include <linux/kref.h>
34 #include <linux/stat.h>
36 #include <linux/usb.h>
37 #include <media/v4l2-common.h>
47 * Module parameter to set frame per second
53 * Module parameter to enable/disable the horizontal flip process
59 * Module parameter to enable/disable vflip detection
61 static int flip_detect
;
65 * Module parameter to enable/disable the vertical flip process
71 * Module parameter to set the brightness
73 static int brightness
= MICRODIA_PERCENT(50, 0xFFFF);
77 * Module parameter to set the whiteness
79 static int whiteness
= MICRODIA_PERCENT(20, 0xFFFF);
83 * Module parameter to set the contrast
85 static int contrast
= MICRODIA_PERCENT(50, 0xFFFF);
89 * Module parameter to set the exposure
91 /* static int exposure = MICRODIA_PERCENT(20, 0xFFFF); */
92 static int exposure
= MICRODIA_PERCENT(2, 0xFFFF);
96 * Module parameter to set the sharpness
98 static int sharpness
= MICRODIA_PERCENT(50, 0x3F);
102 * Module parameter to set the red/green/blue gain
104 static int rgb_gain
= MICRODIA_PERCENT(25, 0x7F) |
105 (MICRODIA_PERCENT(25, 0x7F) << 8) |
106 (MICRODIA_PERCENT(25, 0x7F) << 16);
110 * Module parameter to set the minimum number of image buffers
112 static int min_buffers
= 2;
116 * Module parameter to set the maximum number of image buffers
118 static int max_buffers
= 5;
122 * Module parameter to set the exposure
124 static int auto_exposure
= 1;
127 * @var auto_whitebalance
128 * Module parameter to set the exposure
130 static int auto_whitebalance
= 1;
134 * Module parameter to set the log level
140 * @var microdia_table
141 * Define all the hotplug supported devices by this driver
143 static struct usb_device_id microdia_table
[] = {
144 /* SN9C201 + MI1300 */
145 { USB_DEVICE(USB_0C45_VID
, USB_6240_PID
) },
146 /* SN9C201 + MI1310 */
147 { USB_DEVICE(USB_0C45_VID
, USB_6242_PID
) },
148 /* SN9C201 + S5K4AAFX */
149 { USB_DEVICE(USB_0C45_VID
, USB_6243_PID
) },
150 /* SN9C201 + OV9655 */
151 { USB_DEVICE(USB_0C45_VID
, USB_6248_PID
) },
152 /* SN9C201 + CX1332 */
153 { USB_DEVICE(USB_0C45_VID
, USB_624B_PID
) },
154 /* SN9C201 + MI1320 */
155 { USB_DEVICE(USB_0C45_VID
, USB_624C_PID
) },
156 /* SN9C201 + SOI968 */
157 { USB_DEVICE(USB_0C45_VID
, USB_624E_PID
) },
158 /* SN9C201 + OV9650 */
159 { USB_DEVICE(USB_0C45_VID
, USB_624F_PID
) },
160 /* SN9C201 + OV9650 */
161 { USB_DEVICE(USB_0C45_VID
, USB_6253_PID
) },
162 /* SN9C201 + OV7670ISP */
163 { USB_DEVICE(USB_0C45_VID
, USB_6260_PID
) },
164 /* SN9C201 + OM6802 */
165 { USB_DEVICE(USB_0C45_VID
, USB_6262_PID
) },
166 /* SN9C201 + MI0360/MT9V111 */
167 { USB_DEVICE(USB_0C45_VID
, USB_6270_PID
) },
168 /* SN9C201 + S5K53BEB */
169 { USB_DEVICE(USB_0C45_VID
, USB_627A_PID
) },
170 /* SN9C201 + OV7660 */
171 { USB_DEVICE(USB_0C45_VID
, USB_627B_PID
) },
172 /* SN9C201 + HV7131R */
173 { USB_DEVICE(USB_0C45_VID
, USB_627C_PID
) },
175 { USB_DEVICE(USB_0C45_VID
, USB_627F_PID
) },
176 /* SN9C202 + MI1300 */
177 { USB_DEVICE(USB_0C45_VID
, USB_6280_PID
) },
178 /* SN9C202 + MI1310 */
179 { USB_DEVICE(USB_0C45_VID
, USB_6282_PID
) },
180 /* SN9C202 + S5K4AAFX */
181 { USB_DEVICE(USB_0C45_VID
, USB_6283_PID
) },
182 /* SN9C202 + OV9655 */
183 { USB_DEVICE(USB_0C45_VID
, USB_6288_PID
) },
184 /* SN9C202 + ICM107 */
185 { USB_DEVICE(USB_0C45_VID
, USB_628A_PID
) },
186 /* SN9C202 + CX1332 */
187 { USB_DEVICE(USB_0C45_VID
, USB_628B_PID
) },
188 /* SN9C202 + MI1320 */
189 { USB_DEVICE(USB_0C45_VID
, USB_628C_PID
) },
190 /* SN9C202 + SOI968 */
191 { USB_DEVICE(USB_0C45_VID
, USB_628E_PID
) },
192 /* SN9C202 + OV9650 */
193 { USB_DEVICE(USB_0C45_VID
, USB_628F_PID
) },
194 /* SN9C202 + OV7670ISP */
195 { USB_DEVICE(USB_0C45_VID
, USB_62A0_PID
) },
196 /* SN9C202 + OM6802 */
197 { USB_DEVICE(USB_0C45_VID
, USB_62A2_PID
) },
198 /* SN9C202 + MI0360/MT9V111 */
199 { USB_DEVICE(USB_0C45_VID
, USB_62B0_PID
) },
200 /* SN9C202 + OV9655 */
201 { USB_DEVICE(USB_0C45_VID
, USB_62B3_PID
) },
202 /* SN9C202 + S5K53BEB */
203 { USB_DEVICE(USB_0C45_VID
, USB_62BA_PID
) },
204 /* SN9C202 + OV7660 */
205 { USB_DEVICE(USB_0C45_VID
, USB_62BB_PID
) },
206 /* SN9C202 + HV7131R */
207 { USB_DEVICE(USB_0C45_VID
, USB_62BC_PID
) },
208 /* SN9C202 + OV7663 */
209 { USB_DEVICE(USB_0C45_VID
, USB_62BE_PID
) },
210 /* => 628f (SN9C202 + OV9650) */
211 { USB_DEVICE(USB_045E_VID
, USB_00F4_PID
) },
212 /* => 627b (SN9C201 + OV7660) */
213 { USB_DEVICE(USB_145F_VID
, USB_013D_PID
) },
214 /* => 62be (SN9C202 + OV7663 + EEPROM) */
215 { USB_DEVICE(USB_04F2_VID
, USB_A128_PID
) },
220 MODULE_DEVICE_TABLE(usb
, microdia_table
); /**< Define the supported devices */
222 DEFINE_MUTEX(open_lock
); /**< Define global mutex */
224 int microdia_6240_initialize(struct usb_microdia
*dev
);
225 int microdia_6240_start_stream(struct usb_microdia
*dev
);
226 int microdia_6240_stop_stream(struct usb_microdia
*dev
);
227 int microdia_6242_start_stream(struct usb_microdia
*dev
);
228 int microdia_6242_stop_stream(struct usb_microdia
*dev
);
229 int microdia_624e_initialize(struct usb_microdia
*dev
);
230 int microdia_624e_start_stream(struct usb_microdia
*dev
);
231 int microdia_624e_stop_stream(struct usb_microdia
*dev
);
232 int microdia_624f_initialize(struct usb_microdia
*dev
);
233 int microdia_624f_stop_stream(struct usb_microdia
*dev
);
234 int microdia_624f_start_stream(struct usb_microdia
*dev
);
235 int microdia_624f_set_exposure(struct usb_microdia
*dev
);
236 int microdia_624f_flip_detect(struct usb_microdia
*dev
);
237 int microdia_6260_initialize(struct usb_microdia
*dev
);
238 int microdia_6260_start_stream(struct usb_microdia
*dev
);
239 int microdia_6260_stop_stream(struct usb_microdia
*dev
);
240 int microdia_6260_flip_detect(struct usb_microdia
*dev
);
241 int microdia_6270_initialize(struct usb_microdia
*dev
);
242 int microdia_6270_start_stream(struct usb_microdia
*dev
);
243 int microdia_6270_stop_stream(struct usb_microdia
*dev
);
244 int microdia_627b_initialize(struct usb_microdia
*dev
);
245 int microdia_627b_start_stream(struct usb_microdia
*dev
);
246 int microdia_627b_stop_stream(struct usb_microdia
*dev
);
247 int microdia_627f_initialize(struct usb_microdia
*dev
);
248 int microdia_6288_initialize(struct usb_microdia
*dev
);
249 int microdia_6288_start_stream(struct usb_microdia
*dev
);
250 int microdia_6288_stop_stream(struct usb_microdia
*dev
);
255 * This array assigns all specific functions and settings to each cam.
258 struct microdia_camera cameras
[] = {
260 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_6240_PID
),
261 .type
= MICRODIA_VGA
,
262 .sensor_slave_address
= 0x5d,
263 .sensor_flags
= SN9C20X_I2C_2WIRE
,
264 .supported_fmts
= 0x03,
265 .initialize
= microdia_6240_initialize
,
266 .start_stream
= microdia_6240_start_stream
,
267 .stop_stream
= microdia_6240_stop_stream
,
270 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_6242_PID
),
271 .type
= MICRODIA_VGA
,
272 .sensor_slave_address
= 0x5d,
273 .sensor_flags
= SN9C20X_I2C_2WIRE
,
274 .supported_fmts
= 0x03,
275 .initialize
= microdia_624e_initialize
,
276 .start_stream
= microdia_6242_start_stream
,
277 .stop_stream
= microdia_6242_stop_stream
,
280 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_624E_PID
),
281 .type
= MICRODIA_VGA
,
282 .sensor_slave_address
= 0x30,
283 .sensor_flags
= SN9C20X_I2C_2WIRE
,
284 .supported_fmts
= 0x03,
285 .initialize
= microdia_624e_initialize
,
286 .sensor_init
= soi968_initialize
,
287 .start_stream
= microdia_624e_start_stream
,
288 .stop_stream
= microdia_624e_stop_stream
,
289 .set_auto_exposure
= ov965x_set_autoexposure
,
290 /* .set_exposure = soi968_set_exposure, */
293 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_624F_PID
),
294 .type
= MICRODIA_VGA
,
295 .sensor_slave_address
= 0x30,
296 .sensor_flags
= SN9C20X_I2C_2WIRE
,
297 .supported_fmts
= 0x03,
298 .initialize
= microdia_624f_initialize
,
299 .sensor_init
= ov965x_initialize
,
300 .start_stream
= microdia_624f_start_stream
,
301 .stop_stream
= microdia_624f_stop_stream
,
302 .flip_detect
= microdia_624f_flip_detect
,
303 .set_auto_exposure
= ov965x_set_autoexposure
,
304 .set_exposure
= ov965x_set_exposure
,
305 .set_hvflip
= ov965x_set_hvflip
,
308 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_6253_PID
),
309 .type
= MICRODIA_VGA
,
310 .sensor_slave_address
= 0x30,
311 .sensor_flags
= SN9C20X_I2C_2WIRE
,
312 .supported_fmts
= 0x03,
313 .initialize
= microdia_624f_initialize
,
314 .sensor_init
= ov965x_initialize
,
315 .start_stream
= microdia_624f_start_stream
,
316 .stop_stream
= microdia_624f_stop_stream
,
317 .flip_detect
= microdia_624f_flip_detect
,
318 .set_auto_exposure
= ov965x_set_autoexposure
,
319 .set_exposure
= ov965x_set_exposure
,
320 .set_hvflip
= ov965x_set_hvflip
,
323 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_6260_PID
),
324 .type
= MICRODIA_VGA
,
325 .sensor_slave_address
= 0x21,
326 .sensor_flags
= SN9C20X_I2C_2WIRE
,
327 .supported_fmts
= 0x08,
328 .initialize
= microdia_6260_initialize
,
329 .start_stream
= microdia_6260_start_stream
,
330 .stop_stream
= microdia_6260_stop_stream
,
331 .flip_detect
= microdia_6260_flip_detect
,
334 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_6270_PID
),
335 .type
= MICRODIA_VGA
,
336 .sensor_slave_address
= 0,
337 .sensor_flags
= SN9C20X_I2C_2WIRE
,
338 .supported_fmts
= 0x07,
339 .initialize
= microdia_6270_initialize
,
340 .start_stream
= microdia_6270_start_stream
,
341 .stop_stream
= microdia_6270_stop_stream
,
342 .set_exposure
= mt9vx11_set_exposure
,
343 .set_hvflip
= mt9vx11_set_hvflip
,
344 .set_auto_exposure
= mt9v111_set_autoexposure
,
345 .set_auto_whitebalance
= mt9v111_set_autowhitebalance
,
348 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_627B_PID
),
349 .type
= MICRODIA_VGA
,
350 .sensor_slave_address
= 0x21,
351 .sensor_flags
= SN9C20X_I2C_2WIRE
,
352 .supported_fmts
= 0x07,
353 .initialize
= microdia_627b_initialize
,
354 .sensor_init
= ov7660_initialize
,
355 .start_stream
= microdia_627b_start_stream
,
356 .stop_stream
= microdia_627b_stop_stream
,
357 .set_auto_exposure
= ov7660_set_autoexposure
,
358 .set_exposure
= ov7660_set_exposure
,
361 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_627F_PID
),
362 .type
= MICRODIA_VGA
,
363 .sensor_slave_address
= 0x30,
364 .sensor_flags
= SN9C20X_I2C_2WIRE
,
365 .supported_fmts
= 0x03,
366 .initialize
= microdia_624f_initialize
,
367 .sensor_init
= ov965x_initialize
,
368 .start_stream
= microdia_624f_start_stream
,
369 .stop_stream
= microdia_624f_stop_stream
,
370 .flip_detect
= microdia_624f_flip_detect
,
371 .set_exposure
= ov965x_set_exposure
,
372 .set_hvflip
= ov965x_set_hvflip
,
375 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_6288_PID
),
376 .type
= MICRODIA_VGA
,
377 .sensor_slave_address
= 0x30,
378 .sensor_flags
= SN9C20X_I2C_2WIRE
,
379 .supported_fmts
= 0x03,
380 .initialize
= microdia_6288_initialize
,
381 .start_stream
= microdia_6288_start_stream
,
382 .stop_stream
= microdia_6288_stop_stream
,
383 .set_auto_exposure
= ov965x_set_autoexposure
,
386 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_62B3_PID
),
387 .type
= MICRODIA_VGA
,
388 .sensor_slave_address
= 0x30,
389 .sensor_flags
= SN9C20X_I2C_2WIRE
,
390 .supported_fmts
= 0x03,
391 .initialize
= microdia_6288_initialize
,
392 .start_stream
= microdia_6288_start_stream
,
393 .stop_stream
= microdia_6288_stop_stream
,
396 .model
= CAMERA_MODEL(USB_0C45_VID
, USB_62BB_PID
),
397 .type
= MICRODIA_VGA
,
398 .sensor_slave_address
= 0x21,
399 .sensor_flags
= SN9C20X_I2C_2WIRE
,
400 .supported_fmts
= 0x07,
401 .initialize
= microdia_627b_initialize
,
402 .start_stream
= microdia_627b_start_stream
,
403 .stop_stream
= microdia_627b_stop_stream
,
406 .model
= CAMERA_MODEL(USB_145F_VID
, USB_013D_PID
),
407 .type
= MICRODIA_VGA
,
408 .sensor_slave_address
= 0x21,
409 .sensor_flags
= SN9C20X_I2C_2WIRE
,
410 .supported_fmts
= 0x07,
411 .initialize
= microdia_627b_initialize
,
412 .start_stream
= microdia_627b_start_stream
,
413 .stop_stream
= microdia_627b_stop_stream
,
419 * @brief Find device specific microdia_camera structure for model
421 * @param model Model we are looking for
423 * @returns Pointer to device specific microdia_camera structure or NULL
426 static inline struct microdia_camera
*find_camera(__u32 model
)
429 for (i
= 0; i
< ARRAY_SIZE(cameras
); i
++) {
430 if (cameras
[i
].model
== model
)
437 * @param dev Device structure
439 * @returns 0 if all is OK
441 * @brief Initilize an isochronous pipe.
443 * This function permits to initialize an URB transfert (or isochronous pipe).
445 int usb_microdia_isoc_init(struct usb_microdia
*dev
)
449 __u16 iso_max_frame_size
;
451 struct usb_device
*udev
;
458 UDIA_DEBUG("usb_microdia_isoc_init()\n");
460 iso_max_frame_size
= max_packet_sz(le16_to_cpu(dev
->isoc_in_size
)) *
461 hb_multiplier(le16_to_cpu(dev
->isoc_in_size
));
463 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
464 urb
= usb_alloc_urb(ISO_FRAMES_PER_DESC
, GFP_KERNEL
);
467 UDIA_ERROR("Failed to allocate URB %d\n", i
);
468 usb_microdia_isoc_cleanup(dev
);
474 urb
->pipe
= usb_rcvisocpipe(udev
, dev
->isoc_in_endpointAddr
);
475 urb
->transfer_flags
= URB_ISO_ASAP
;
476 urb
->transfer_buffer_length
= iso_max_frame_size
* ISO_FRAMES_PER_DESC
;
477 urb
->complete
= usb_microdia_isoc_handler
;
479 urb
->start_frame
= 0;
480 urb
->number_of_packets
= ISO_FRAMES_PER_DESC
;
482 for (j
= 0; j
< ISO_FRAMES_PER_DESC
; j
++) {
483 urb
->iso_frame_desc
[j
].offset
= j
* iso_max_frame_size
;
484 urb
->iso_frame_desc
[j
].length
= iso_max_frame_size
;
487 dev
->isobuf
[i
].data
= kzalloc(urb
->transfer_buffer_length
,
489 if (dev
->isobuf
[i
].data
== NULL
) {
490 usb_microdia_isoc_cleanup(dev
);
494 urb
->transfer_buffer
= dev
->isobuf
[i
].data
;
495 dev
->isobuf
[i
].urb
= urb
;
498 UDIA_DEBUG("dev->isoc_in_size = %X\n", (unsigned int)dev
->isoc_in_size
);
499 UDIA_DEBUG("dev->isoc_in_endpointAddr = %X\n", dev
->isoc_in_endpointAddr
);
501 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
502 ret
= usb_submit_urb(dev
->isobuf
[i
].urb
, GFP_KERNEL
);
505 UDIA_ERROR("isoc_init() submit_urb %d failed with error %d\n", i
, ret
);
513 * @param urb URB structure
515 * @brief ISOC handler
517 * This function is called as an URB transfert is complete (Isochronous pipe).
518 * So, the traitement is done in interrupt time, so it has be fast, not crash,
519 * and not stall. Neat.
521 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
522 void usb_microdia_isoc_handler(struct urb
*urb
, struct pt_regs
*regs
)
524 void usb_microdia_isoc_handler(struct urb
*urb
)
531 unsigned int framelen
;
534 unsigned char *iso_buf
= NULL
;
537 struct microdia_buffer
*buf
= NULL
;
538 struct usb_microdia
*dev
= urb
->context
;
539 struct microdia_video_queue
*queue
= &dev
->queue
;
541 unsigned char frame_header
[] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
543 UDIA_STREAM("Isoc handler\n");
545 switch (urb
->status
) {
550 UDIA_WARNING("Non-zero status (%d) in video "
551 "completion handler.\n", urb
->status
);
553 case -ENOENT
: /* usb_kill_urb() called. */
557 case -ECONNRESET
: /* usb_unlink_urb() called. */
558 case -ESHUTDOWN
: /* The endpoint is being disabled. */
559 microdia_queue_cancel(queue
);
563 spin_lock_irqsave(&queue
->irqlock
, flags
);
564 if (!list_empty(&queue
->irqqueue
))
565 buf
= list_first_entry(&queue
->irqqueue
, struct microdia_buffer
,
567 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
569 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
570 framestatus
= urb
->iso_frame_desc
[i
].status
;
571 if (framestatus
!= 0) {
572 UDIA_ERROR("Iso frame %d of USB has error %d\n",
576 framelen
= urb
->iso_frame_desc
[i
].actual_length
;
577 iso_buf
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
582 if (memcmp(iso_buf
, frame_header
, 6) == 0 && framelen
== 64) {
583 UDIA_DEBUG("Frame Resolution: %dx%d\n",
584 iso_buf
[0x3a] << 4, iso_buf
[0x3b] << 3);
585 if (buf
->buf
.bytesused
!= 0)
586 buf
->state
= MICRODIA_BUF_STATE_DONE
;
588 if (buf
->state
!= MICRODIA_BUF_STATE_ACTIVE
)
589 buf
->state
= MICRODIA_BUF_STATE_ACTIVE
;
591 if (framelen
+ buf
->buf
.bytesused
> queue
->frame_size
) {
592 UDIA_WARNING("Frame Buffer overflow!\n");
593 dev
->vframes_overflow
++;
594 buf
->state
= MICRODIA_BUF_STATE_DONE
;
596 framelen
= min(queue
->frame_size
- buf
->buf
.bytesused
,
598 mem
= queue
->mem
+ buf
->buf
.m
.offset
+
600 memcpy(mem
, iso_buf
, framelen
);
601 buf
->buf
.bytesused
+= framelen
;
603 if (buf
->state
== MICRODIA_BUF_STATE_DONE
||
604 buf
->state
== MICRODIA_BUF_STATE_ERROR
) {
605 buf
= microdia_queue_next_buffer(queue
, buf
);
607 dev
->vframes_dropped
++;
611 urb
->dev
= dev
->udev
;
613 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
616 UDIA_ERROR("Error (%d) re-submitting urb in "
617 "microdia_isoc_handler.\n", ret
);
623 * @param dev Device structure
625 * @brief Clean-up all the ISOC buffers
627 * This function permits to clean-up all the ISOC buffers.
629 void usb_microdia_isoc_cleanup(struct usb_microdia
*dev
)
634 UDIA_DEBUG("Isoc cleanup\n");
639 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
640 urb
= dev
->isobuf
[i
].urb
;
646 kfree(dev
->isobuf
[i
].data
);
650 dev
->isobuf
[i
].urb
= NULL
;
657 * @param dev Device structure
658 * @param index Choice of the interface
660 * @returns 0 if all is OK
662 * @brief Send the message SET_FEATURE and choose the interface
664 * This function permits to send the message SET_FEATURE on the USB bus.
666 int usb_microdia_set_feature(struct usb_microdia
*dev
, int index
)
669 struct usb_device
*udev
= dev
->udev
;
671 result
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
673 USB_TYPE_STANDARD
| USB_DIR_OUT
| USB_RECIP_DEVICE
,
674 USB_DEVICE_REMOTE_WAKEUP
,
681 UDIA_ERROR("SET FEATURE fail !\n");
683 UDIA_DEBUG("SET FEATURE\n");
690 * @param dev Device structure
692 * @returns 0 if all is OK
694 * @brief Send the message SET_CONFIGURATION
696 * This function permits to send the message SET_CONFIGURATION on the USB bus.
698 int usb_microdia_set_configuration(struct usb_microdia
*dev
)
701 struct usb_device
*udev
= dev
->udev
;
703 result
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
704 USB_REQ_SET_CONFIGURATION
,
705 USB_TYPE_STANDARD
| USB_DIR_OUT
| USB_RECIP_DEVICE
,
707 udev
->config
[0].desc
.bConfigurationValue
,
713 UDIA_ERROR("SET CONFIGURATION fail !\n");
715 UDIA_DEBUG("SET CONFIGURATION %d\n", udev
->config
[0].desc
.bConfigurationValue
);
722 * @param dev Device structure
723 * @param value register to write to
725 * @param length number of bytes
727 * @returns 0 if all is OK
729 * @brief Write a 16-bit value to a 16-bit register
731 * This function permits to write a 16-bit value to a 16-bit register on the USB bus.
733 int usb_microdia_control_write(struct usb_microdia
*dev
, __u16 value
, __u8
*data
, __u16 length
)
736 struct usb_device
*udev
= dev
->udev
;
738 result
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
740 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
,
748 UDIA_ERROR("Write register failed index = 0x%02X\n", value
);
754 * @param dev Device structure
755 * @param index register to read from
757 * @param length number of bytes
759 * @returns 0 if all is OK
761 * @brief Read a 16-bit value from a 16-bit register
763 * This function permits to read a 16-bit value from a 16-bit register on the USB bus.
765 int usb_microdia_control_read(struct usb_microdia
*dev
, __u16 index
, __u8
*data
, __u16 length
)
769 struct usb_device
*udev
= dev
->udev
;
773 result
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
775 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
,
783 UDIA_ERROR("Read register failed 0x%02X\n", index
);
792 * @returns 0 if all is OK
794 * @brief Set the default value about the video settings.
796 * This function permits to set the video settings for each video camera model.
799 static int usb_microdia_default_settings(struct usb_microdia
*dev
)
801 struct v4l2_pix_format
*def_fmt
;
803 dev
->vframes_overflow
= 0;
804 dev
->vframes_incomplete
= 0;
805 dev
->vframes_dropped
= 0;
807 dev
->queue
.min_buffers
= min_buffers
;
808 dev
->queue
.max_buffers
= max_buffers
;
810 def_fmt
= v4l2_enum_supported_formats(dev
, 0);
812 switch (dev
->webcam_model
) {
814 dev
->vsettings
.fps
= fps
;
815 dev
->vsettings
.vflip
= vflip
;
816 dev
->vsettings
.hflip
= hflip
;
817 dev
->vsettings
.brightness
= brightness
& 0xffff;
818 dev
->vsettings
.contrast
= contrast
& 0xffff;
819 dev
->vsettings
.whiteness
= whiteness
& 0xffff;
820 dev
->vsettings
.exposure
= exposure
& 0xffff;
821 dev
->vsettings
.sharpness
= sharpness
& 0x3f;
822 dev
->vsettings
.rgb_gain
[0] = (rgb_gain
>> 16) & 0x7f;
823 dev
->vsettings
.rgb_gain
[1] = (rgb_gain
>> 8) & 0x7f;
824 dev
->vsettings
.rgb_gain
[2] = (rgb_gain
>> 8) & 0x7f;
825 dev
->vsettings
.rgb_gain
[3] = rgb_gain
& 0x7f;
826 dev
->vsettings
.auto_exposure
= auto_exposure
& 1;
827 dev
->vsettings
.auto_whitebalance
= auto_whitebalance
& 1;
828 dev
->vsettings
.hue
= 0xffff;
831 memcpy(&(dev
->vsettings
.format
),
833 sizeof(struct v4l2_pix_format
));
836 sn9c20x_set_resolution(dev
, 640, 480);
843 * @brief Load the driver
848 * @returns 0 if all is OK
850 * This function detects the device and allocate the buffers for the device
851 * and the video interface.
853 static int usb_microdia_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
)
863 struct usb_microdia
*dev
= NULL
;
864 struct usb_device
*udev
= interface_to_usbdev(interface
);
865 struct usb_host_interface
*iface_desc
;
866 struct usb_endpoint_descriptor
*endpoint
;
867 struct microdia_camera
*camera
= NULL
;
869 /* Get USB VendorID and ProductID */
870 vendor_id
= le16_to_cpu(udev
->descriptor
.idVendor
);
871 product_id
= le16_to_cpu(udev
->descriptor
.idProduct
);
873 /* Check if we can handle this device */
874 UDIA_DEBUG("Probe function called with VendorID=%04X, ProductID=%04X and InterfaceNumber=%d\n",
875 vendor_id
, product_id
, interface
->cur_altsetting
->desc
.bInterfaceNumber
);
878 //The interface are probed one by one.
879 // We are interested in the video interface (always the interface '0')
880 // The interfaces '1' or '2' (if presents) are the audio control.
882 if (interface
->cur_altsetting
->desc
.bInterfaceNumber
> 0) {
888 camera
= find_camera(CAMERA_MODEL(vendor_id
, product_id
));
889 if (camera
== NULL
) {
890 UDIA_INFO("Camera %04X:%04X not supported.\n",
891 vendor_id
, product_id
);
896 UDIA_INFO("Microdia USB 2.0 Webcam - %04X:%04X plugged-in.\n",
897 vendor_id
, product_id
);
901 // Allocate structure, initialize pointers, mutexes, etc.
902 // and link it to the usb_device
904 dev
= kzalloc(sizeof(struct usb_microdia
), GFP_KERNEL
);
907 UDIA_ERROR("Out of memory !\n");
912 /* Init mutexes, spinlock, etc. */
913 mutex_init(&dev
->mutex
);
914 kref_init(&dev
->vopen
);
917 dev
->webcam_model
= camera
->model
;
918 dev
->webcam_type
= camera
->type
;
919 if (dev
->webcam_model
== CAMERA_MODEL(USB_0C45_VID
, USB_6260_PID
))
920 dev
->frame_size_divisor
= MICRODIA_FRAME_SIZE_DIVISOR_6260
;
922 dev
->frame_size_divisor
= MICRODIA_FRAME_SIZE_DIVISOR_DEFAULT
;
923 dev
->initialize
= camera
->initialize
;
924 dev
->sensor_init
= camera
->sensor_init
;
925 dev
->stop_stream
= camera
->stop_stream
;
926 dev
->start_stream
= camera
->start_stream
;
927 dev
->set_contrast
= camera
->set_contrast
== NULL
?
928 sn9c20x_set_contrast
: camera
->set_contrast
;
929 dev
->set_brightness
= camera
->set_brightness
== NULL
?
930 sn9c20x_set_brightness
: camera
->set_brightness
;
931 dev
->set_gamma
= camera
->set_gamma
== NULL
?
932 sn9c20x_set_gamma
: camera
->set_gamma
;
933 dev
->set_sharpness
= camera
->set_sharpness
== NULL
?
934 sn9c20x_set_sharpness
: camera
->set_sharpness
;
935 dev
->set_rgb_gain
= camera
->set_rgb_gain
== NULL
?
936 sn9c20x_set_rgb_gain
: camera
->set_rgb_gain
;
937 dev
->set_exposure
= camera
->set_exposure
== NULL
?
938 sn9c20x_set_exposure
: camera
->set_exposure
;
940 UDIA_INFO("Rotate detection enabled\n");
941 dev
->flip_detect
= camera
->flip_detect
;
943 dev
->set_hvflip
= camera
->set_hvflip
;
944 dev
->set_auto_exposure
= camera
->set_auto_exposure
;
945 dev
->set_auto_whitebalance
= camera
->set_auto_whitebalance
;
947 dev
->interface
= interface
;
949 dev
->sensor_slave_address
= camera
->sensor_slave_address
;
950 dev
->sensor_flags
= camera
->sensor_flags
;
951 dev
->supported_fmts
= camera
->supported_fmts
;
953 /* Read the product release */
954 dev
->release
= le16_to_cpu(udev
->descriptor
.bcdDevice
);
955 UDIA_DEBUG("Release: %04x\n", dev
->release
);
957 /* How many interfaces (1 or 3) ? */
958 bNumInterfaces
= udev
->config
->desc
.bNumInterfaces
;
959 UDIA_DEBUG("Number of interfaces : %d\n", bNumInterfaces
);
962 /* Switch on the camera (to detect size of buffers) */
963 dev_microdia_camera_on(dev
);
966 // Set up the endpoint information
967 // use only the first int-in and isoc-in endpoints
968 // for the current alternate setting
970 iface_desc
= interface
->cur_altsetting
;
972 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
973 endpoint
= &iface_desc
->endpoint
[i
].desc
;
975 if (!dev
->int_in_endpointAddr
976 && ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
)
977 && ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)) {
978 /* we found an interrupt in endpoint */
979 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
981 dev
->int_in_size
= buffer_size
;
982 dev
->int_in_endpointAddr
= (endpoint
->bEndpointAddress
& 0xf);
985 if (!dev
->isoc_in_endpointAddr
986 && ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
)
987 && ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_ISOC
)) {
988 /* we found an isoc in endpoint */
989 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
991 dev
->isoc_in_size
= buffer_size
;
992 dev
->isoc_in_endpointAddr
= (endpoint
->bEndpointAddress
& 0xf);
996 if (!(dev
->int_in_endpointAddr
&& dev
->isoc_in_endpointAddr
)) {
997 UDIA_ERROR("Could not find both int-in and isoc-in endpoints\n");
1002 usb_microdia_default_settings(dev
);
1004 dev_microdia_camera_off(dev
);
1006 /* Initialize the video device */
1007 dev
->vdev
= video_device_alloc();
1014 /* Initialize the camera */
1015 dev_microdia_initialize_device(dev
);
1017 /* Register the video device */
1018 ret
= v4l_microdia_register_video_device(dev
);
1023 /* Create the entries in the sys filesystem */
1024 microdia_create_sysfs_files(dev
->vdev
);
1026 #ifdef CONFIG_MICRODIA_DEBUGFS
1027 microdia_create_debugfs_files(dev
);
1030 /* Save our data pointer in this interface device */
1031 usb_set_intfdata(interface
, dev
);
1036 kref_put(&dev
->vopen
, usb_microdia_delete
);
1041 void usb_microdia_delete(struct kref
*kref
)
1043 struct usb_microdia
*dev
;
1044 dev
= container_of(kref
, struct usb_microdia
, vopen
);
1046 if (dev
->vdev
!= NULL
) {
1047 microdia_remove_sysfs_files(dev
->vdev
);
1048 #ifdef CONFIG_MICRODIA_DEBUGFS
1049 microdia_remove_debugfs_files(dev
);
1051 v4l_microdia_unregister_video_device(dev
);
1059 * @brief This function is called when the device is disconnected
1060 * or when the kernel module is unloaded.
1062 static void usb_microdia_disconnect(struct usb_interface
*interface
)
1064 struct usb_microdia
*dev
= usb_get_intfdata(interface
);
1066 UDIA_INFO("Microdia USB 2.0 Webcam unplugged\n");
1068 usb_set_intfdata(interface
, NULL
);
1070 mutex_lock(&open_lock
);
1071 kref_put(&dev
->vopen
, usb_microdia_delete
);
1072 mutex_unlock(&open_lock
);
1077 * @var usb_microdia_driver
1079 * This variable contains some callback
1081 static struct usb_driver usb_microdia_driver
= {
1082 .name
= "usb_microdia_driver",
1083 .probe
= usb_microdia_probe
,
1084 .disconnect
= usb_microdia_disconnect
,
1085 .id_table
= microdia_table
,
1088 module_param(fps
, int, 0444); /**< @brief Module parameter frames per second */
1089 module_param(hflip
, int, 0444); /**< @brief Module parameter horizontal flip process */
1090 module_param(vflip
, int, 0444); /**< @brief Module parameter vertical flip process */
1091 module_param(flip_detect
, int, 0444); /**< @brief Module parameter flip detect */
1092 module_param(auto_exposure
, int, 0444); /**< @brief Module parameter automatic exposure control */
1093 module_param(auto_whitebalance
, int, 0444); /**< @brief Module parameter automatic whitebalance control */
1094 module_param(brightness
, int, 0444); /**< @brief Module parameter brightness */
1095 module_param(whiteness
, int, 0444); /**< @brief Module parameter whiteness */
1096 module_param(contrast
, int, 0444); /**< @brief Module parameter contrast */
1097 module_param(exposure
, int, 0444); /**< @brief Module parameter exposure */
1098 module_param(sharpness
, int, 0444); /**< @brief Module parameter sharpness */
1099 module_param(rgb_gain
, int, 0444); /**< @brief Module parameter red/green/blue gain */
1101 module_param(min_buffers
, int, 0444);
1102 module_param(max_buffers
, int, 0444);
1104 module_param(log_level
, byte
, 0444);
1107 * @returns 0 if all is OK
1109 * @brief Initialize the driver.
1111 * This function is called at first.
1112 * This function permits to define the default values from the command line.
1114 static int __init
usb_microdia_init(void)
1118 UDIA_INFO("Microdia USB 2.0 webcam driver loaded\n");
1120 #ifdef CONFIG_MICRODIA_DEBUGFS
1121 microdia_init_debugfs();
1124 if (fps
< 10 || fps
> 30) {
1125 UDIA_WARNING("Framerate out of bounds [10-30]! Defaulting to 25\n");
1129 if (vflip
!= 0 && vflip
!= 1) {
1130 UDIA_WARNING("Vertical flip should be 0 or 1! Defaulting to 0\n");
1134 if (hflip
!= 0 && hflip
!= 1) {
1135 UDIA_WARNING("Horizontal flip should be 0 or 1! Defaulting to 0\n");
1139 if (sharpness
< 0 || sharpness
> 0x3f) {
1140 UDIA_WARNING("Sharpness should be 0 to 63 ! Defaulting to 31\n");
1144 if ((rgb_gain
>> 16) < 0 || (rgb_gain
>> 16) > 0x7f) {
1145 UDIA_WARNING("Red Gain should be 0 to 127 ! Defaulting to 32\n");
1146 rgb_gain
= (rgb_gain
& 0x0000ffff) | 0x200000;
1149 if (((rgb_gain
>> 8) & 0xFF) < 0 || ((rgb_gain
>> 8) & 0xFF) > 0x7f) {
1150 UDIA_WARNING("Green Gain should be 0 to 127 ! Defaulting to 32\n");
1151 rgb_gain
= (rgb_gain
& 0x00ff00ff) | 0x002000;
1154 if ((rgb_gain
& 0xFF) < 0 || (rgb_gain
& 0xFF) > 0x7f) {
1155 UDIA_WARNING("Blue Gain should be 0 to 127 ! Defaulting to 32\n");
1156 rgb_gain
= (rgb_gain
& 0x00ffff00) | 0x20;
1159 if (auto_exposure
!= 0 && auto_exposure
!= 1) {
1160 UDIA_WARNING("Automatic exposure should be 0 or 1! "
1161 "Defaulting to 1\n");
1165 if (auto_whitebalance
!= 0 && auto_whitebalance
!= 1) {
1166 UDIA_WARNING("Automatic whitebalance should be 0 or 1! "
1167 "Defaulting to 1\n");
1168 auto_whitebalance
= 1;
1171 if (min_buffers
< 2) {
1172 UDIA_WARNING("Minimum buffers can't be less then 2! "
1173 "Defaulting to 2\n");
1178 if (min_buffers
> max_buffers
) {
1179 UDIA_WARNING("Minimum buffers must be less then or equal to "
1180 "max buffers! Defaulting to 2, 10\n");
1185 /* Register the driver with the USB subsystem */
1186 result
= usb_register(&usb_microdia_driver
);
1189 UDIA_ERROR("usb_register failed ! Error number %d\n", result
);
1191 UDIA_INFO(DRIVER_VERSION
" : " DRIVER_DESC
"\n");
1198 * @brief Close the driver
1200 * This function is called at last when you unload the driver.
1202 static void __exit
usb_microdia_exit(void)
1204 UDIA_INFO("usb_microdia_exit: Microdia USB 2.0 webcam driver unloaded\n");
1206 #ifdef CONFIG_MICRODIA_DEBUGFS
1207 microdia_uninit_debugfs();
1210 /* Deregister this driver with the USB subsystem */
1211 usb_deregister(&usb_microdia_driver
);
1215 module_init(usb_microdia_init
); /**< @brief Module initialize */
1216 module_exit(usb_microdia_exit
); /**< @brief Module exit */
1219 MODULE_PARM_DESC(fps
, "Frames per second [10-30]"); /**< @brief Description of 'fps' parameter */
1220 MODULE_PARM_DESC(hflip
, "Horizontal image flip"); /**< @brief Description of 'hflip' parameter */
1221 MODULE_PARM_DESC(vflip
, "Vertical image flip"); /**< @brief Description of 'vflip' parameter */
1222 MODULE_PARM_DESC(flip_detect
, "Image flip detection"); /**< @brief Description of 'vflip_detect' parameter */
1223 MODULE_PARM_DESC(auto_exposure
, "Automatic exposure control"); /**< @brief Description of 'auto_exposure' parameter */
1224 MODULE_PARM_DESC(auto_whitebalance
, "Automatic whitebalance"); /**< @brief Description of 'auto_whitebalance' parameter */
1225 MODULE_PARM_DESC(brightness
, "Brightness setting"); /**< @brief Description of 'brightness' parameter */
1226 MODULE_PARM_DESC(whiteness
, "Whiteness setting"); /**< @brief Description of 'whiteness' parameter */
1227 MODULE_PARM_DESC(exposure
, "Exposure setting"); /**< @brief Description of 'exposure' parameter */
1228 MODULE_PARM_DESC(contrast
, "Contrast setting"); /**< @brief Description of 'contrast' parameter */
1229 MODULE_PARM_DESC(sharpness
, "Sharpness setting"); /**< @brief Description of 'sharpness' parameter */
1230 MODULE_PARM_DESC(rgb_gain
, "Red/Green/Blue Gain setting"); /**< @brief Description of 'RGB Gain' parameter */
1232 MODULE_PARM_DESC(min_buffers
, "Minimum number of image buffers");
1233 MODULE_PARM_DESC(max_buffers
, "Maximum number of image buffers");
1234 MODULE_PARM_DESC(log_level
, " <n>\n"
1235 "Driver log level\n"
1236 "1 = info (default)\n"
1242 MODULE_LICENSE("GPL"); /**< @brief Driver is under licence GPL */
1243 MODULE_AUTHOR(DRIVER_AUTHOR
); /**< @brief Driver is written by Nicolas VIVIEN */
1244 MODULE_DESCRIPTION(DRIVER_DESC
); /**< @brief Define the description of the driver */
1245 MODULE_SUPPORTED_DEVICE(DRIVER_SUPPORT
); /**< @brief List of supported device */