All Omnivision sensors now us software autoexposure calculations
[microdia.git] / sn9c20x.h
blob6f762980ce80ababd1bd50ce67cf21db13b0999b
1 /**
2 * @file sn9c20x.h
3 * @author Nicolas VIVIEN
4 * @date 2008-02-01
6 * @brief Driver for SN9C20X USB video cameras
8 * @note Copyright (C) Nicolas VIVIEN
10 * @par Licences
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
15 * any later version.
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>
32 #ifndef SN9C20X_H
33 #define SN9C20X_H
35 #define DRIVER_NAME "sn9c20x" /**< Name of this driver */
36 #define DRIVER_VERSION "v2008.11" /**< Version of this driver */
37 #define DRIVER_VERSION_NUM 0x000000 /**< Version numerical of this driver */
38 #define DRIVER_DESC "SN9C20X 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 SN9C20X "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 */
47 /* SN9C201: */
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_6251_PID 0x6251 /**< SN9C201 + OV9650 */
57 #define USB_6253_PID 0x6253 /**< SN9C201 + OV9650 */
58 #define USB_6260_PID 0x6260 /**< SN9C201 + OV7670ISP */
59 #define USB_6262_PID 0x6262 /**< SN9C201 + OM6802 */
60 #define USB_6270_PID 0x6270 /**< SN9C201 + MI0360/MT9V111 */
61 #define USB_627A_PID 0x627a /**< SN9C201 + S5K53BEB */
62 #define USB_627B_PID 0x627b /**< SN9C201 + OV7660 */
63 #define USB_627C_PID 0x627c /**< SN9C201 + HV7131R */
64 #define USB_627F_PID 0x627f /**< EEPROM */
65 /* SN9C202:*/
66 #define USB_6280_PID 0x6280 /**< SN9C202 + MI1300 */
67 #define USB_6282_PID 0x6282 /**< SN9C202 + MI1310 */
68 #define USB_6283_PID 0x6283 /**< SN9C202 + S5K4AAFX */
69 #define USB_6288_PID 0x6288 /**< SN9C202 + OV9655 */
70 #define USB_628A_PID 0x628a /**< SN9C202 + ICM107 */
71 #define USB_628B_PID 0x628b /**< SN9C202 + CX1332 */
72 #define USB_628C_PID 0x628c /**< SN9C202 + MI1320 */
73 #define USB_628E_PID 0x628e /**< SN9C202 + SOI968 */
74 #define USB_628F_PID 0x628f /**< SN9C202 + OV9650 */
75 #define USB_62A0_PID 0x62a0 /**< SN9C202 + OV7670ISP */
76 #define USB_62A2_PID 0x62a2 /**< SN9C202 + OM6802 */
77 #define USB_62B0_PID 0x62b0 /**< SN9C202 + MI0360/MT9V111 */
78 #define USB_62B3_PID 0x62b3 /**< SN9C202 + OV9655 */
79 #define USB_62BA_PID 0x62ba /**< SN9C202 + S5K53BEB */
80 #define USB_62BB_PID 0x62bb /**< SN9C202 + OV7660 */
81 #define USB_62BC_PID 0x62bc /**< SN9C202 + HV7131R */
82 #define USB_62BE_PID 0x62be /**< SN9C202 + OV7663 */
83 /* *** "Clone"-devices ***: */
85 /* Microsoft LifeCam VX-6000: */
86 #define USB_00F4_PID 0x00f4 /**< => 628f (SN9C202 + OV9650) */
87 /* Trust WB-3600R: */
88 #define USB_013D_PID 0x013d /**< => 627b (SN9C201 + OV7660) */
89 /* Chicony Panda 7, Trust WB-3450P: */
90 #define USB_A128_PID 0xa128 /**< => 62be (SN9C202 + OV7663) */
92 #define SN9C20X_USB_DEVICE(vend, prod, sensor, address) \
93 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
94 USB_DEVICE_ID_MATCH_INT_CLASS, \
95 .idVendor = (vend), \
96 .idProduct = (prod), \
97 .bInterfaceClass = 0xff, \
98 .driver_info = ((address << 8) | sensor)
100 /** V4L2-CONTROLS for compatibility with older kernels: */
101 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 25)
102 #define V4L2_CID_SHARPNESS (V4L2_CID_PRIVATE_BASE + 0)
103 #define V4L2_CID_EXPOSURE_AUTO (V4L2_CID_PRIVATE_BASE + 1)
104 enum v4l2_exposure_auto_type {
105 V4L2_EXPOSURE_AUTO = 0,
106 V4L2_EXPOSURE_MANUAL = 1,
107 V4L2_EXPOSURE_SHUTTER_PRIORITY = 2,
108 V4L2_EXPOSURE_APERTURE_PRIORITY = 3
110 #endif
113 * @def MAX_URBS
114 * Number maximal of URBS
116 * @def ISO_FRAMES_PER_DESC
117 * Number frames per ISOC descriptor
119 #define MAX_URBS 200
120 #define ISO_FRAMES_PER_DESC 10
123 * @def hb_multiplier(wMaxPacketSize)
124 * USB endpoint high bandwidth multiplier
126 * @def max_packet_sz(wMaxPacketSize)
127 * USB endpoint maximum packet size
129 * These values are both encoded within the wMaxPacketSize field of the usb_endpoint_descriptor structure.
130 * The 11(10:0) lowest bits hold the MaxPacketSize(according to the usb specs a value over 1024 is undefined),
131 * and the bits 12:11 will give the high bandwidth multiplier(this should be a value of 0-2).
134 #define hb_multiplier(wMaxPacketSize) (((wMaxPacketSize >> 11) & 0x03) + 1)
135 #define max_packet_sz(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
138 * @def DRIVER_SUPPORT
140 * List of supported devices
142 #define DRIVER_SUPPORT "SN9C20X USB Camera"
144 #define SN9C20X_INFO (1 << 0)
145 #define SN9C20X_WARNING (1 << 1)
146 #define SN9C20X_ERROR (1 << 2)
147 #define SN9C20X_DEBUG (1 << 3)
148 #define SN9C20X_STREAM (1 << 4)
151 * @def UDIA_INFO(str, args...)
152 * Print information message.
153 * @a Use this function like the function printf.
155 * @def UDIA_ERROR(str, args...)
156 * Print error message.
157 * @a Use this function like the function printf.
159 * @def UDIA_WARNING(str, args...)
160 * Print warning message.
161 * @a Use this function like the function printf.
163 * @def UDIA_DEBUG(str, args...)
164 * Print debug message.
165 * @a Use this function like the function printf.
167 * @def UDIA_STREAM(str, args...)
168 * Print stream debug message.
169 * @a Use this function like the function printf.
171 extern __u8 log_level;
173 #define UDIA_INFO(str, args...) \
174 do { \
175 if (log_level & SN9C20X_INFO) \
176 printk(KERN_INFO PREFIX str, ##args); \
177 } while (0)
179 #define UDIA_WARNING(str, args...) \
180 do { \
181 if (log_level & SN9C20X_WARNING) \
182 printk(KERN_WARNING PREFIX str, ##args); \
183 } while (0)
185 #define UDIA_ERROR(str, args...) \
186 do { \
187 if (log_level & SN9C20X_ERROR) \
188 printk(KERN_ERR PREFIX str, ##args); \
189 } while (0)
191 #define UDIA_DEBUG(str, args...) \
192 do { \
193 if (log_level & SN9C20X_DEBUG) \
194 printk(KERN_DEBUG PREFIX str, ##args); \
195 } while (0)
197 #define UDIA_STREAM(str, args...) \
198 do { \
199 if (log_level & SN9C20X_STREAM) \
200 printk(KERN_DEBUG PREFIX str, ##args); \
201 } while (0)
204 enum sn9c20x_buffer_state {
205 SN9C20X_BUF_STATE_IDLE = 0,
206 SN9C20X_BUF_STATE_QUEUED = 1,
207 SN9C20X_BUF_STATE_ACTIVE = 2,
208 SN9C20X_BUF_STATE_DONE = 3,
209 SN9C20X_BUF_STATE_ERROR = 4,
212 enum sn9c20x_mode {
213 SN9C20X_MODE_IDLE = 0,
214 SN9C20X_MODE_READ = 1,
215 SN9C20X_MODE_STREAM = 2,
218 struct sn9c20x_buffer {
219 unsigned long vma_use_count;
220 struct list_head stream;
222 /* Touched by interrupt handler. */
223 struct v4l2_buffer buf;
224 struct list_head queue;
225 wait_queue_head_t wait;
226 enum sn9c20x_buffer_state state;
229 #define SN9C20X_QUEUE_STREAMING (1 << 0)
230 #define SN9C20X_QUEUE_DISCONNECTED (1 << 1)
231 #define SN9C20X_QUEUE_DROP_INCOMPLETE (1 << 2)
233 struct sn9c20x_video_queue {
234 void *mem;
235 unsigned int flags;
236 __u32 sequence;
238 unsigned int count;
239 unsigned int min_buffers;
240 unsigned int max_buffers;
241 unsigned int buf_size;
243 struct sn9c20x_buffer *buffer;
244 struct sn9c20x_buffer *read_buffer;
245 struct mutex mutex; /* protects buffers and mainqueue */
246 spinlock_t irqlock; /* protects irqqueue */
248 struct list_head mainqueue;
249 struct list_head irqqueue;
253 * @struct sn9c20x_urb
255 struct sn9c20x_urb {
256 void *data;
257 struct urb *urb;
261 * @struct sn9c20x_video
263 struct sn9c20x_video {
264 struct v4l2_pix_format format;
265 int fps; /**< FPS setting */
266 int brightness; /**< Brightness setting */
267 int contrast; /**< Contrast setting */
268 int gamma; /**< Gamma setting */
269 int colour; /**< Colour setting */
270 int hue; /**< Hue setting */
271 int hflip; /**< Horizontal flip */
272 int vflip; /**< Vertical flip */
273 int exposure; /**< Exposure */
274 int gain; /**< Gain */
275 int sharpness; /**< Sharpness */
276 int red_gain; /**< Red Gain Control */
277 int blue_gain; /**< Blue Gain Control */
278 int auto_exposure; /**< Automatic exposure */
279 int auto_gain; /**< Automatic gain */
280 int auto_whitebalance; /**< Automatic whitebalance */
284 * @struct sn9c20x_debugfs
286 struct sn9c20x_debugfs {
287 struct dentry *dent_device;
288 struct dentry *dent_bridge_addr;
289 struct dentry *dent_bridge_val;
290 struct dentry *dent_bridge_dump;
291 struct dentry *dent_sensor_addr;
292 struct dentry *dent_sensor_val8;
293 struct dentry *dent_sensor_val16;
294 struct dentry *dent_sensor_val32;
296 __u16 bridge_addr; /**< Current bridge register address */
297 __u8 sensor_addr; /**< Current sensor register address */
300 enum sn9c20x_endpoints {
301 SN9C20X_VID_ISOC = 1,
302 SN9C20X_BULK = 2,
303 SN9C20X_INT = 3,
304 SN9C20X_AUD_ISOC = 4
307 enum sn9c20x_sensors {
308 PROBE_SENSOR = 0,
309 OV9650_SENSOR = 1,
310 OV9655_SENSOR = 2,
311 SOI968_SENSOR = 3,
312 OV7660_SENSOR = 4,
313 OV7670_SENSOR = 5,
314 MT9M111_SENSOR = 6,
315 MT9V111_SENSOR = 7,
316 MT9V011_SENSOR = 8,
317 MT9M001_SENSOR = 9,
318 HV7131R_SENSOR = 10,
319 MT9V112_SENSOR = 11,
324 struct usb_sn9c20x;
326 struct sn9c20x_video_mode {
327 __u16 width;
328 __u16 height;
329 __u8 scale;
330 __u16 window[4];
333 struct sn9c20x_video_format {
334 __u32 pix_fmt;
335 char desc[32];
336 __u8 depth;
337 void (*set_format) (struct usb_sn9c20x *dev);
340 struct sn9c20x_i2c_regs {
341 __u8 address;
342 __u16 value;
345 extern struct sn9c20x_i2c_regs mt9v112_init[];
346 extern struct sn9c20x_i2c_regs mt9v111_init[];
347 extern struct sn9c20x_i2c_regs mt9v011_init[];
348 extern struct sn9c20x_i2c_regs mt9m111_init[];
349 extern struct sn9c20x_i2c_regs mt9m001_init[];
350 extern struct sn9c20x_i2c_regs soi968_init[];
351 extern struct sn9c20x_i2c_regs ov9650_init[];
352 extern struct sn9c20x_i2c_regs ov9655_init[];
353 extern struct sn9c20x_i2c_regs ov7660_init[];
354 extern struct sn9c20x_i2c_regs ov7670_init[];
355 extern struct sn9c20x_i2c_regs hv7131r_init[];
357 #define SN9C20X_N_FMTS 4
358 #define SN9C20X_N_MODES 6
360 extern struct sn9c20x_video_format sn9c20x_fmts[SN9C20X_N_FMTS];
361 extern struct sn9c20x_video_mode sn9c20x_modes[SN9C20X_N_MODES];
363 struct sn9c20x_camera {
365 /* Sensor ID */
366 __u8 sensor;
368 /* SCCB/I2C interface */
369 __u8 i2c_flags;
370 __u8 address;
372 int min_yavg, max_yavg, min_stable_yavg, max_stable_yavg;
374 atomic_t yavg;
376 int vstart;
377 int hstart;
379 int (*flip_detect) (struct usb_sn9c20x *dev);
380 int (*button_detect) (struct usb_sn9c20x *dev);
381 int (*set_hvflip) (struct usb_sn9c20x *dev);
382 /* image quality functions */
383 int (*set_exposure) (struct usb_sn9c20x *dev);
384 int (*set_gain) (struct usb_sn9c20x *dev);
385 int (*set_auto_exposure) (struct usb_sn9c20x *dev);
386 int (*set_auto_gain) (struct usb_sn9c20x *dev);
387 int (*set_auto_whitebalance) (struct usb_sn9c20x *dev);
388 int (*set_contrast) (struct usb_sn9c20x *dev);
389 int (*set_brightness) (struct usb_sn9c20x *dev);
390 int (*set_gamma) (struct usb_sn9c20x *dev);
391 int (*set_sharpness) (struct usb_sn9c20x *dev);
392 int (*set_red_gain) (struct usb_sn9c20x *dev);
393 int (*set_blue_gain) (struct usb_sn9c20x *dev);
397 * @struct usb_sn9c20x
399 struct usb_sn9c20x {
400 struct video_device *vdev; /**< Pointer on a V4L2 video device */
401 struct usb_device *udev; /**< Pointer on a USB device */
402 struct usb_interface *interface; /**< Pointer on a USB interface */
404 int release; /**< Release of the device (bcdDevice) */
406 struct sn9c20x_video vsettings; /**< Video settings (brightness, whiteness...) */
407 struct sn9c20x_debugfs debug; /**< debugfs information structure */
409 struct kref vopen; /**< Video status (Opened or Closed) */
410 struct file *owner; /**< file handler of stream owner */
411 enum sn9c20x_mode mode;/**< camera mode */
413 int vframes_overflow; /**< Buffer overflow frames */
414 int vframes_incomplete; /**< Incomplete frames */
415 int vframes_dropped; /**< Dropped frames */
417 struct mutex mutex; /**< Mutex */
419 struct sn9c20x_urb urbs[MAX_URBS];
421 int resolution;
423 unsigned int frozen:1;
424 struct sn9c20x_video_queue queue;
425 struct sn9c20x_camera camera;
429 * @def SN9C20X_PERCENT
430 * Calculate a value from a percent
432 #define SN9C20X_PERCENT(x, y) (((int)x * (int)y) / 100)
435 extern struct mutex open_lock;
436 extern __u8 jpeg;
438 int usb_sn9c20x_control_write(struct usb_sn9c20x *, __u16, __u8 *, __u16);
439 int usb_sn9c20x_control_read(struct usb_sn9c20x *, __u16, __u8 *, __u16);
441 int usb_sn9c20x_isoc_init(struct usb_sn9c20x *,
442 struct usb_endpoint_descriptor *);
443 void usb_sn9c20x_completion_handler(struct urb *);
444 int usb_sn9c20x_init_urbs(struct usb_sn9c20x *);
445 void usb_sn9c20x_uninit_urbs(struct usb_sn9c20x *);
446 void usb_sn9c20x_delete(struct kref *);
448 int sn9c20x_initialize(struct usb_sn9c20x *dev);
449 int sn9c20x_initialize_sensor(struct usb_sn9c20x *dev);
450 int sn9c20x_enable_video(struct usb_sn9c20x *dev, int enable);
452 int dev_sn9c20x_call_constantly(struct usb_sn9c20x *dev);
453 int dev_sn9c20x_flip_detection(struct usb_sn9c20x *dev);
454 int dev_sn9c20x_button_detection(struct usb_sn9c20x *dev);
455 int dev_sn9c20x_camera_set_exposure(struct usb_sn9c20x *);
456 int dev_sn9c20x_camera_set_gain(struct usb_sn9c20x *);
457 int dev_sn9c20x_camera_set_hvflip(struct usb_sn9c20x *);
458 int dev_sn9c20x_camera_set_auto_exposure(struct usb_sn9c20x *dev);
459 int dev_sn9c20x_camera_set_auto_gain(struct usb_sn9c20x *dev);
460 int dev_sn9c20x_camera_set_auto_whitebalance(struct usb_sn9c20x *dev);
461 int dev_sn9c20x_perform_soft_ae(struct usb_sn9c20x *dev);
463 void v4l2_set_control_default(struct usb_sn9c20x *, __u32, __u16);
464 int v4l_sn9c20x_select_video_mode(struct usb_sn9c20x *, int);
465 int v4l_sn9c20x_register_video_device(struct usb_sn9c20x *);
466 int v4l_sn9c20x_unregister_video_device(struct usb_sn9c20x *);
468 int sn9c20x_create_sysfs_files(struct video_device *);
469 void sn9c20x_remove_sysfs_files(struct video_device *);
471 #ifdef CONFIG_SN9C20X_DEBUGFS
472 void sn9c20x_init_debugfs(void);
473 void sn9c20x_uninit_debugfs(void);
474 int sn9c20x_create_debugfs_files(struct usb_sn9c20x *);
475 int sn9c20x_remove_debugfs_files(struct usb_sn9c20x *);
476 #else
477 static inline void sn9c20x_init_debugfs(void) {}
478 static inline void sn9c20x_uninit_debugfs(void) {}
479 static inline int sn9c20x_create_debugfs_files(struct usb_sn9c20x *dev)
481 return 0;
483 static inline int sn9c20x_remove_debugfs_files(struct usb_sn9c20x *dev)
485 return 0;
487 #endif
489 void sn9c20x_queue_init(struct sn9c20x_video_queue *);
490 int sn9c20x_alloc_buffers(struct sn9c20x_video_queue *,
491 unsigned int, unsigned int);
492 int sn9c20x_free_buffers(struct sn9c20x_video_queue *);
493 int sn9c20x_queue_enable(struct sn9c20x_video_queue *, int);
494 void sn9c20x_queue_cancel(struct sn9c20x_video_queue *, int);
495 unsigned int sn9c20x_queue_poll(struct sn9c20x_video_queue *,
496 struct file *, poll_table *);
497 int sn9c20x_query_buffer(struct sn9c20x_video_queue *, struct v4l2_buffer *);
498 int sn9c20x_queue_buffer(struct sn9c20x_video_queue *, struct v4l2_buffer *);
499 int sn9c20x_dequeue_buffer(struct sn9c20x_video_queue *,
500 struct v4l2_buffer *, int);
501 struct sn9c20x_buffer *sn9c20x_queue_next_buffer(
502 struct sn9c20x_video_queue *, struct sn9c20x_buffer *);
504 static inline int sn9c20x_queue_streaming(struct sn9c20x_video_queue *queue)
506 return queue->flags & SN9C20X_QUEUE_STREAMING;
509 /* sensor hv7131r has no header-file of its own: */
510 int hv7131r_initialize(struct usb_sn9c20x *dev);
511 int hv7131r_set_exposure(struct usb_sn9c20x *dev);
512 int hv7131r_set_gain(struct usb_sn9c20x *dev);
513 int hv7131r_set_hvflip(struct usb_sn9c20x *dev);
515 #endif