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/kernel.h>
28 #include <linux/version.h>
29 #include <linux/usb.h>
30 #include <media/v4l2-common.h>
35 #define DRIVER_NAME "microdia" /**< Name of this driver */
36 #define DRIVER_VERSION "v2008.10" /**< Version of this driver */
37 #define DRIVER_VERSION_NUM 0x000000 /**< Version numerical of this driver */
38 #define DRIVER_DESC "Microdia USB 2.0 Webcam Driver"/**< Short description of this driver */
39 #define DRIVER_AUTHOR "Nicolas VIVIEN" /**< Author of this driver */
40 #define PREFIX DRIVER_NAME ": " /**< Prefix use for the MICRODIA "printk" */
42 #define USB_0C45_VID 0x0c45 /**< Vendor ID of MICRODIA */
43 #define USB_045E_VID 0x045e /**< Vendor ID of MICROSOFT */
44 #define USB_04F2_VID 0x04f2 /**< Vendor ID of CHICONY */
45 #define USB_145F_VID 0x145f /**< Vendor ID of TRUST */
48 #define USB_6240_PID 0x6240 /**< SN9C201 + MI1300 */
49 #define USB_6242_PID 0x6242 /**< SN9C201 + MI1310 */
50 #define USB_6243_PID 0x6243 /**< SN9C201 + S5K4AAFX */
51 #define USB_6248_PID 0x6248 /**< SN9C201 + OV9655 */
52 #define USB_624B_PID 0x624b /**< SN9C201 + CX1332 */
53 #define USB_624C_PID 0x624c /**< SN9C201 + MI1320 */
54 #define USB_624E_PID 0x624e /**< SN9C201 + SOI968 */
55 #define USB_624F_PID 0x624f /**< SN9C201 + OV9650 */
56 #define USB_6253_PID 0x6253 /**< SN9C201 + OV9650 */
57 #define USB_6260_PID 0x6260 /**< SN9C201 + OV7670ISP */
58 #define USB_6262_PID 0x6262 /**< SN9C201 + OM6802 */
59 #define USB_6270_PID 0x6270 /**< SN9C201 + MI0360/MT9V111 */
60 #define USB_627A_PID 0x627a /**< SN9C201 + S5K53BEB */
61 #define USB_627B_PID 0x627b /**< SN9C201 + OV7660 */
62 #define USB_627C_PID 0x627c /**< SN9C201 + HV7131R */
63 #define USB_627F_PID 0x627f /**< EEPROM */
65 #define USB_6280_PID 0x6280 /**< SN9C202 + MI1300 */
66 #define USB_6282_PID 0x6282 /**< SN9C202 + MI1310 */
67 #define USB_6283_PID 0x6283 /**< SN9C202 + S5K4AAFX */
68 #define USB_6288_PID 0x6288 /**< SN9C202 + OV9655 */
69 #define USB_628A_PID 0x628a /**< SN9C202 + ICM107 */
70 #define USB_628B_PID 0x628b /**< SN9C202 + CX1332 */
71 #define USB_628C_PID 0x628c /**< SN9C202 + MI1320 */
72 #define USB_628E_PID 0x628e /**< SN9C202 + SOI968 */
73 #define USB_628F_PID 0x628f /**< SN9C202 + OV9650 */
74 #define USB_62A0_PID 0x62a0 /**< SN9C202 + OV7670ISP */
75 #define USB_62A2_PID 0x62a2 /**< SN9C202 + OM6802 */
76 #define USB_62B0_PID 0x62b0 /**< SN9C202 + MI0360/MT9V111 */
77 #define USB_62B3_PID 0x62b3 /**< SN9C202 + OV9655 */
78 #define USB_62BA_PID 0x62ba /**< SN9C202 + S5K53BEB */
79 #define USB_62BB_PID 0x62bb /**< SN9C202 + OV7660 */
80 #define USB_62BC_PID 0x62bc /**< SN9C202 + HV7131R */
81 #define USB_62BE_PID 0x62be /**< SN9C202 + OV7663 */
83 #define USB_6128_PID 0x6128 /**< SN9C325 + OM6802 */
84 #define USB_612A_PID 0x612a /**< SN9C325 + OV7648 + POx1030xC + SOI768 + PO2030N + OV7660 + OV7670 + HV7131R */
85 /* *** "Clone"-devices ***: */
87 /* Microsoft LifeCam VX-6000: */
88 #define USB_00F4_PID 0x00f4 /**< => 628f (SN9C202 + OV9650) */
90 #define USB_013D_PID 0x013d /**< => 627b (SN9C201 + OV7660) */
91 /* Chicony Panda 7, Trust WB-3450P: */
92 #define USB_A128_PID 0xa128 /**< => 62be (SN9C202 + OV7663) */
94 #define CAMERA_MODEL(vid, pid) ((vid << 16) | pid)
95 #define CAMERA_VID(model) ((model >> 16) & 0xFFFF)
96 #define CAMERA_PID(model) (model & 0xFFFF)
99 * @def VID_HARDWARE_MICRODIA
101 * This value must be inserted into the kernel headers linux/videodev.h
102 * It's useful only for the support of V4L v1
104 #define VID_HARDWARE_MICRODIA 88
105 /* FIXME: Can the above be removed ? */
109 * Number maximal of ISOC buffers
111 * @def ISO_FRAMES_PER_DESC
112 * Number frames per ISOC descriptor
114 #define MAX_ISO_BUFS 200
115 #define ISO_FRAMES_PER_DESC 10
118 * @def hb_multiplier(wMaxPacketSize)
119 * USB endpoint high bandwidth multiplier
121 * @def max_packet_sz(wMaxPacketSize)
122 * USB endpoint maximum packet size
124 * These values are both encoded within the wMaxPacketSize field of the usb_endpoint_descriptor structure.
125 * The 11(10:0) lowest bits hold the MaxPacketSize(according to the usb specs a value over 1024 is undefined),
126 * and the bits 12:11 will give the high bandwidth multiplier(this should be a value of 0-2).
129 #define hb_multiplier(wMaxPacketSize) (((wMaxPacketSize >> 11) & 0x03) + 1)
130 #define max_packet_sz(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
133 * @def MICRODIA_FRAME_SIZE
134 * Maximum size after decompression
136 #define MICRODIA_FRAME_SIZE (1280 * 1024 * 3)
139 * @def MICRODIA_FRAME_SIZE_DIVISOR_DEFAULT
140 * Most models have a Frame size of image_size + image_size/2
142 * @def MICRODIA_FRAME_SIZE_DIVISOR_6260
143 * The 6260 has a Frame size of image_size + image_size
145 #define MICRODIA_FRAME_SIZE_DIVISOR_DEFAULT 2
146 #define MICRODIA_FRAME_SIZE_DIVISOR_6260 1
149 * @def DRIVER_SUPPORT
151 * List of supported devices
153 #define DRIVER_SUPPORT "Microdia USB Camera"
155 #define MICRODIA_INFO (1 << 0)
156 #define MICRODIA_WARNING (1 << 1)
157 #define MICRODIA_ERROR (1 << 2)
158 #define MICRODIA_DEBUG (1 << 3)
159 #define MICRODIA_STREAM (1 << 4)
162 * @def UDIA_INFO(str, args...)
163 * Print information message.
164 * @a Use this function like the function printf.
166 * @def UDIA_ERROR(str, args...)
167 * Print error message.
168 * @a Use this function like the function printf.
170 * @def UDIA_WARNING(str, args...)
171 * Print warning message.
172 * @a Use this function like the function printf.
174 * @def UDIA_DEBUG(str, args...)
175 * Print debug message.
176 * @a Use this function like the function printf.
178 * @def UDIA_STREAM(str, args...)
179 * Print stream debug message.
180 * @a Use this function like the function printf.
182 extern __u8 log_level
;
184 #define UDIA_INFO(str, args...) \
186 if (log_level & MICRODIA_INFO) \
187 printk(KERN_INFO PREFIX str, ##args); \
190 #define UDIA_WARNING(str, args...) \
192 if (log_level & MICRODIA_WARNING) \
193 printk(KERN_WARNING PREFIX str, ##args); \
196 #define UDIA_ERROR(str, args...) \
198 if (log_level & MICRODIA_ERROR) \
199 printk(KERN_ERR PREFIX str, ##args); \
202 #define UDIA_DEBUG(str, args...) \
204 if (log_level & MICRODIA_DEBUG) \
205 printk(KERN_DEBUG PREFIX str, ##args); \
208 #define UDIA_STREAM(str, args...) \
210 if (log_level & MICRODIA_STREAM) \
211 printk(KERN_DEBUG PREFIX str, ##args); \
215 * @enum T_MICRODIA_VIDEOMODE Video feature supported by camera
218 MICRODIA_VGA
, /**< For VGA video camera */
219 MICRODIA_SXGA
, /**< For SXGA video camera 1.3M */
220 MICRODIA_UXGA
/**< For UXGA video camera 2M */
221 } T_MICRODIA_VIDEOMODE
;
223 enum microdia_buffer_state
{
224 MICRODIA_BUF_STATE_IDLE
= 0,
225 MICRODIA_BUF_STATE_QUEUED
= 1,
226 MICRODIA_BUF_STATE_ACTIVE
= 2,
227 MICRODIA_BUF_STATE_DONE
= 3,
228 MICRODIA_BUF_STATE_ERROR
= 4,
232 MICRODIA_MODE_IDLE
= 0,
233 MICRODIA_MODE_READ
= 1,
234 MICRODIA_MODE_STREAM
= 2,
237 struct microdia_buffer
{
238 unsigned long vma_use_count
;
239 struct list_head stream
;
241 /* Touched by interrupt handler. */
242 struct v4l2_buffer buf
;
243 struct list_head queue
;
244 wait_queue_head_t wait
;
245 enum microdia_buffer_state state
;
248 struct microdia_video_queue
{
251 unsigned int streaming
:1,
257 unsigned int min_buffers
;
258 unsigned int max_buffers
;
259 unsigned int buf_size
;
260 unsigned int frame_size
; /*current frame size */
261 struct microdia_buffer
*buffer
;
262 struct microdia_buffer
*read_buffer
;
263 struct mutex mutex
; /* protects buffers and mainqueue */
264 spinlock_t irqlock
; /* protects irqqueue */
266 struct list_head mainqueue
;
267 struct list_head irqqueue
;
271 * @struct microdia_iso_buf
273 struct microdia_iso_buf
{
279 * @struct microdia_video
281 struct microdia_video
{
282 struct v4l2_pix_format format
;
283 int fps
; /**< FPS setting */
284 int brightness
; /**< Brightness setting */
285 int contrast
; /**< Contrast setting */
286 int whiteness
; /**< Whiteness setting */
287 int colour
; /**< Colour setting */
288 int hue
; /**< Hue setting */
289 int hflip
; /**< Horizontal flip */
290 int vflip
; /**< Vertical flip */
291 int exposure
; /**< Exposure */
292 int sharpness
; /**< Sharpness */
293 __u8 rgb_gain
[4]; /**< RGB Gain (RGGB) */
294 int auto_exposure
; /**< Automatic exposure */
295 int auto_whitebalance
; /**< Automatic whitebalance */
299 * @struct microdia_debugfs
301 struct microdia_debugfs
{
302 struct dentry
*dent_device
;
303 struct dentry
*dent_bridge_addr
;
304 struct dentry
*dent_bridge_val
;
305 struct dentry
*dent_bridge_dump
;
306 struct dentry
*dent_sensor_addr
;
307 struct dentry
*dent_sensor_val8
;
308 struct dentry
*dent_sensor_val16
;
309 struct dentry
*dent_sensor_val32
;
311 __u16 bridge_addr
; /**< Current bridge register address */
312 __u8 sensor_addr
; /**< Current sensor register address */
316 * @struct usb_microdia
318 struct usb_microdia
{
319 struct video_device
*vdev
; /**< Pointer on a V4L2 video device */
320 struct usb_device
*udev
; /**< Pointer on a USB device */
321 struct usb_interface
*interface
; /**< Pointer on a USB interface */
323 int release
; /**< Release of the device (bcdDevice) */
324 __u32 webcam_model
; /**< Model of video camera device */
325 __u8 webcam_type
; /**< Type of camera : VGA, SXGA (1.3M), UXGA (2M) */
327 int frame_size_divisor
; /**> Used to calculate frame_size */
329 __u8 sensor_slave_address
; /**< I2C/SCCB slave address of the image sensor */
330 __u8 sensor_flags
; /**< I2C/SCCB flags of the image sensor */
332 unsigned char *int_in_buffer
; /**< Interrupt IN buffer */
333 size_t int_in_size
; /**< Interrupt IN buffer size */
334 __u8 int_in_endpointAddr
; /**< Interrupt IN endpoint address */
336 size_t isoc_in_size
; /**< Isochrone IN size */
337 __u8 isoc_in_endpointAddr
; /**< Isochrone IN endpoint address */
339 struct microdia_video vsettings
; /**< Video settings (brightness, whiteness...) */
340 struct microdia_debugfs debug
; /**< debugfs information structure */
342 struct kref vopen
; /**< Video status (Opened or Closed) */
343 struct file
*owner
; /**< file handler of stream owner */
344 enum microdia_mode mode
;/**< camera mode */
346 int vframes_overflow
; /**< Buffer overflow frames */
347 int vframes_incomplete
; /**< Incomplete frames */
348 int vframes_dropped
; /**< Dropped frames */
350 struct mutex mutex
; /**< Mutex */
352 struct microdia_iso_buf isobuf
[MAX_ISO_BUFS
];
356 struct microdia_video_queue queue
;
360 int (*initialize
) (struct usb_microdia
*dev
);
361 int (*sensor_init
) (struct usb_microdia
*dev
);
362 int (*start_stream
) (struct usb_microdia
*dev
);
363 int (*stop_stream
) (struct usb_microdia
*dev
);
364 int (*set_contrast
) (struct usb_microdia
*dev
);
365 int (*set_brightness
) (struct usb_microdia
*dev
);
366 int (*set_gamma
) (struct usb_microdia
*dev
);
367 int (*set_exposure
) (struct usb_microdia
*dev
);
368 int (*flip_detect
) (struct usb_microdia
*dev
);
369 int (*set_hvflip
) (struct usb_microdia
*dev
);
370 int (*set_sharpness
) (struct usb_microdia
*dev
);
371 int (*set_rgb_gain
) (struct usb_microdia
*dev
);
372 int (*set_auto_exposure
) (struct usb_microdia
*dev
);
373 int (*set_auto_whitebalance
) (struct usb_microdia
*dev
);
376 struct microdia_camera
{
378 __u32 model
; /**< Model of video camera device */
379 __u8 type
; /**< Type of camera : VGA, SXGA, UXGA */
381 __u8 sensor_slave_address
;
384 int (*initialize
) (struct usb_microdia
*dev
);
385 int (*sensor_init
) (struct usb_microdia
*dev
);
386 int (*start_stream
) (struct usb_microdia
*dev
);
387 int (*stop_stream
) (struct usb_microdia
*dev
);
388 int (*set_contrast
) (struct usb_microdia
*dev
);
389 int (*set_brightness
) (struct usb_microdia
*dev
);
390 int (*set_gamma
) (struct usb_microdia
*dev
);
391 int (*set_exposure
) (struct usb_microdia
*dev
);
392 int (*flip_detect
) (struct usb_microdia
*dev
);
393 int (*set_hvflip
) (struct usb_microdia
*dev
);
394 int (*set_sharpness
) (struct usb_microdia
*dev
);
395 int (*set_rgb_gain
) (struct usb_microdia
*dev
);
396 int (*set_auto_exposure
) (struct usb_microdia
*dev
);
397 int (*set_auto_whitebalance
) (struct usb_microdia
*dev
);
401 * @def MICRODIA_PERCENT
402 * Calculate a value from a percent
404 #define MICRODIA_PERCENT(x, y) (((int)x * (int)y) / 100)
408 * @def to_microdia_dev(d)
409 * Cast a member of a structure out to the containing structure
411 #define to_microdia_dev(d) container_of(d, struct usb_microdia, kref)
413 extern struct mutex open_lock
;
415 int usb_microdia_control_write(struct usb_microdia
*, __u16
, __u8
*, __u16
);
416 int usb_microdia_control_read(struct usb_microdia
*, __u16
, __u8
*, __u16
);
417 int usb_microdia_set_feature(struct usb_microdia
*, int);
419 int usb_microdia_set_configuration(struct usb_microdia
*);
420 int usb_microdia_isoc_init(struct usb_microdia
*);
421 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
422 void usb_microdia_isoc_handler(struct urb
*, struct pt_regs
*);
424 void usb_microdia_isoc_handler(struct urb
*);
426 void usb_microdia_isoc_cleanup(struct usb_microdia
*);
427 void usb_microdia_delete(struct kref
*);
430 int dev_microdia_initialize_device(struct usb_microdia
*);
431 int dev_microdia_start_stream(struct usb_microdia
*);
432 int dev_microdia_stop_stream(struct usb_microdia
*);
433 int dev_microdia_check_device(struct usb_microdia
*, int);
434 int dev_microdia_camera_on(struct usb_microdia
*);
435 int dev_microdia_camera_off(struct usb_microdia
*);
436 int dev_microdia_camera_asleep(struct usb_microdia
*);
437 int dev_microdia_init_camera(struct usb_microdia
*);
438 int dev_microdia_reconf_camera(struct usb_microdia
*);
439 int dev_microdia_camera_settings(struct usb_microdia
*);
440 int dev_microdia_camera_set_contrast(struct usb_microdia
*);
441 int dev_microdia_camera_set_brightness(struct usb_microdia
*);
442 int dev_microdia_camera_set_gamma(struct usb_microdia
*);
443 int dev_microdia_camera_set_exposure(struct usb_microdia
*);
444 int dev_microdia_camera_set_hvflip(struct usb_microdia
*);
445 int dev_microdia_camera_set_sharpness(struct usb_microdia
*dev
);
446 int dev_microdia_camera_set_rgb_gain(struct usb_microdia
*dev
);
447 int dev_microdia_camera_set_auto_exposure(struct usb_microdia
*dev
);
448 int dev_microdia_camera_set_auto_whitebalance(struct usb_microdia
*dev
);
449 /* int dev_microdia_set_camera_quality(struct usb_microdia *);*/
450 int dev_microdia_set_camera_fps(struct usb_microdia
*);
452 struct v4l2_pix_format
*v4l2_enum_supported_formats(struct usb_microdia
*,
454 int v4l_microdia_select_video_mode(struct usb_microdia
*, int);
455 int v4l_microdia_register_video_device(struct usb_microdia
*);
456 int v4l_microdia_unregister_video_device(struct usb_microdia
*);
458 int microdia_create_sysfs_files(struct video_device
*);
459 void microdia_remove_sysfs_files(struct video_device
*);
461 void microdia_init_debugfs(void);
462 void microdia_uninit_debugfs(void);
463 int microdia_create_debugfs_files(struct usb_microdia
*);
464 int microdia_remove_debugfs_files(struct usb_microdia
*);
466 void microdia_queue_init(struct microdia_video_queue
*);
467 int microdia_alloc_buffers(struct microdia_video_queue
*,
468 unsigned int, unsigned int);
469 int microdia_free_buffers(struct microdia_video_queue
*);
470 int microdia_queue_enable(struct microdia_video_queue
*, int);
471 void microdia_queue_cancel(struct microdia_video_queue
*);
472 unsigned int microdia_queue_poll(struct microdia_video_queue
*,
473 struct file
*, poll_table
*);
474 int microdia_query_buffer(struct microdia_video_queue
*, struct v4l2_buffer
*);
475 int microdia_queue_buffer(struct microdia_video_queue
*, struct v4l2_buffer
*);
476 int microdia_dequeue_buffer(struct microdia_video_queue
*,
477 struct v4l2_buffer
*, int);
478 struct microdia_buffer
*microdia_queue_next_buffer(
479 struct microdia_video_queue
*, struct microdia_buffer
*);
481 int microdia_decompress(struct usb_microdia
*, struct v4l2_buffer
*);