Complete renaming to sn920x and declare driver "v2009.01"
[microdia.git] / sn9c20x.h
blob061a4e96c06778e7c696532249d006f742e57b33
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 "v2009.01" /**< Version of this driver */
37 #define DRIVER_VERSION_NUM 0x200901 /**< 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 and microdia project" \
40 " <microdia@googlegroups.com>" /**< Author of this driver */
41 #define PREFIX DRIVER_NAME ": " /**< Prefix use for the SN9C20X "printk" */
43 #define USB_0C45_VID 0x0c45 /**< Vendor ID of MICRODIA */
44 #define USB_045E_VID 0x045e /**< Vendor ID of MICROSOFT */
45 #define USB_04F2_VID 0x04f2 /**< Vendor ID of CHICONY */
46 #define USB_145F_VID 0x145f /**< Vendor ID of TRUST */
48 /* SN9C201: */
49 #define USB_6240_PID 0x6240 /**< SN9C201 + MI1300 */
50 #define USB_6242_PID 0x6242 /**< SN9C201 + MI1310 */
51 #define USB_6243_PID 0x6243 /**< SN9C201 + S5K4AAFX */
52 #define USB_6248_PID 0x6248 /**< SN9C201 + OV9655 */
53 #define USB_624B_PID 0x624b /**< SN9C201 + CX1332 */
54 #define USB_624C_PID 0x624c /**< SN9C201 + MI1320 */
55 #define USB_624E_PID 0x624e /**< SN9C201 + SOI968 */
56 #define USB_624F_PID 0x624f /**< SN9C201 + OV9650 */
57 #define USB_6251_PID 0x6251 /**< SN9C201 + OV9650 */
58 #define USB_6253_PID 0x6253 /**< SN9C201 + OV9650 */
59 #define USB_6260_PID 0x6260 /**< SN9C201 + OV7670ISP */
60 #define USB_6262_PID 0x6262 /**< SN9C201 + OM6802 */
61 #define USB_6270_PID 0x6270 /**< SN9C201 + MI0360/MT9V111 */
62 #define USB_627A_PID 0x627a /**< SN9C201 + S5K53BEB */
63 #define USB_627B_PID 0x627b /**< SN9C201 + OV7660 */
64 #define USB_627C_PID 0x627c /**< SN9C201 + HV7131R */
65 #define USB_627F_PID 0x627f /**< EEPROM */
66 /* SN9C202:*/
67 #define USB_6280_PID 0x6280 /**< SN9C202 + MI1300 */
68 #define USB_6282_PID 0x6282 /**< SN9C202 + MI1310 */
69 #define USB_6283_PID 0x6283 /**< SN9C202 + S5K4AAFX */
70 #define USB_6288_PID 0x6288 /**< SN9C202 + OV9655 */
71 #define USB_628A_PID 0x628a /**< SN9C202 + ICM107 */
72 #define USB_628B_PID 0x628b /**< SN9C202 + CX1332 */
73 #define USB_628C_PID 0x628c /**< SN9C202 + MI1320 */
74 #define USB_628E_PID 0x628e /**< SN9C202 + SOI968 */
75 #define USB_628F_PID 0x628f /**< SN9C202 + OV9650 */
76 #define USB_62A0_PID 0x62a0 /**< SN9C202 + OV7670ISP */
77 #define USB_62A2_PID 0x62a2 /**< SN9C202 + OM6802 */
78 #define USB_62B0_PID 0x62b0 /**< SN9C202 + MI0360/MT9V111 */
79 #define USB_62B3_PID 0x62b3 /**< SN9C202 + OV9655 */
80 #define USB_62BA_PID 0x62ba /**< SN9C202 + S5K53BEB */
81 #define USB_62BB_PID 0x62bb /**< SN9C202 + OV7660 */
82 #define USB_62BC_PID 0x62bc /**< SN9C202 + HV7131R */
83 #define USB_62BE_PID 0x62be /**< SN9C202 + OV7663 */
84 /* *** "Clone"-devices ***: */
86 /* Microsoft LifeCam VX-6000: */
87 #define USB_00F4_PID 0x00f4 /**< => 628f (SN9C202 + OV9650) */
88 /* Trust WB-3600R: */
89 #define USB_013D_PID 0x013d /**< => 627b (SN9C201 + OV7660) */
90 /* Chicony Panda 7, Trust WB-3450P: */
91 #define USB_A128_PID 0xa128 /**< => 62be (SN9C202 + OV7663) */
93 #define SN9C20X_USB_DEVICE(vend, prod, sensor, address) \
94 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
95 USB_DEVICE_ID_MATCH_INT_CLASS, \
96 .idVendor = (vend), \
97 .idProduct = (prod), \
98 .bInterfaceClass = 0xff, \
99 .driver_info = ((address << 8) | sensor)
101 /** V4L2-CONTROLS for compatibility with older kernels: */
102 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 25)
103 #define V4L2_CID_SHARPNESS (V4L2_CID_PRIVATE_BASE + 0)
104 #define V4L2_CID_EXPOSURE_AUTO (V4L2_CID_PRIVATE_BASE + 1)
105 enum v4l2_exposure_auto_type {
106 V4L2_EXPOSURE_AUTO = 0,
107 V4L2_EXPOSURE_MANUAL = 1,
108 V4L2_EXPOSURE_SHUTTER_PRIORITY = 2,
109 V4L2_EXPOSURE_APERTURE_PRIORITY = 3
111 #endif
114 * @def MAX_URBS
115 * Number maximal of URBS
117 * @def ISO_FRAMES_PER_DESC
118 * Number frames per ISOC descriptor
120 #define MAX_URBS 200
121 #define ISO_FRAMES_PER_DESC 10
124 * @def hb_multiplier(wMaxPacketSize)
125 * USB endpoint high bandwidth multiplier
127 * @def max_packet_sz(wMaxPacketSize)
128 * USB endpoint maximum packet size
130 * These values are both encoded within the wMaxPacketSize field of the usb_endpoint_descriptor structure.
131 * The 11(10:0) lowest bits hold the MaxPacketSize(according to the usb specs a value over 1024 is undefined),
132 * and the bits 12:11 will give the high bandwidth multiplier(this should be a value of 0-2).
135 #define hb_multiplier(wMaxPacketSize) (((wMaxPacketSize >> 11) & 0x03) + 1)
136 #define max_packet_sz(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
139 * @def DRIVER_SUPPORT
141 * List of supported devices
143 #define DRIVER_SUPPORT "SN9C20X USB Camera"
145 #define SN9C20X_INFO (1 << 0)
146 #define SN9C20X_WARNING (1 << 1)
147 #define SN9C20X_ERROR (1 << 2)
148 #define SN9C20X_DEBUG (1 << 3)
149 #define SN9C20X_STREAM (1 << 4)
152 * @def UDIA_INFO(str, args...)
153 * Print information message.
154 * @a Use this function like the function printf.
156 * @def UDIA_ERROR(str, args...)
157 * Print error message.
158 * @a Use this function like the function printf.
160 * @def UDIA_WARNING(str, args...)
161 * Print warning message.
162 * @a Use this function like the function printf.
164 * @def UDIA_DEBUG(str, args...)
165 * Print debug message.
166 * @a Use this function like the function printf.
168 * @def UDIA_STREAM(str, args...)
169 * Print stream debug message.
170 * @a Use this function like the function printf.
172 extern __u8 log_level;
174 #define UDIA_INFO(str, args...) \
175 do { \
176 if (log_level & SN9C20X_INFO) \
177 printk(KERN_INFO PREFIX str, ##args); \
178 } while (0)
180 #define UDIA_WARNING(str, args...) \
181 do { \
182 if (log_level & SN9C20X_WARNING) \
183 printk(KERN_WARNING PREFIX str, ##args); \
184 } while (0)
186 #define UDIA_ERROR(str, args...) \
187 do { \
188 if (log_level & SN9C20X_ERROR) \
189 printk(KERN_ERR PREFIX str, ##args); \
190 } while (0)
192 #define UDIA_DEBUG(str, args...) \
193 do { \
194 if (log_level & SN9C20X_DEBUG) \
195 printk(KERN_DEBUG PREFIX str, ##args); \
196 } while (0)
198 #define UDIA_STREAM(str, args...) \
199 do { \
200 if (log_level & SN9C20X_STREAM) \
201 printk(KERN_DEBUG PREFIX str, ##args); \
202 } while (0)
205 enum sn9c20x_buffer_state {
206 SN9C20X_BUF_STATE_IDLE = 0,
207 SN9C20X_BUF_STATE_QUEUED = 1,
208 SN9C20X_BUF_STATE_ACTIVE = 2,
209 SN9C20X_BUF_STATE_DONE = 3,
210 SN9C20X_BUF_STATE_ERROR = 4,
213 enum sn9c20x_mode {
214 SN9C20X_MODE_IDLE = 0,
215 SN9C20X_MODE_READ = 1,
216 SN9C20X_MODE_STREAM = 2,
219 struct sn9c20x_buffer {
220 unsigned long vma_use_count;
221 struct list_head stream;
223 /* Touched by interrupt handler. */
224 struct v4l2_buffer buf;
225 struct list_head queue;
226 wait_queue_head_t wait;
227 enum sn9c20x_buffer_state state;
230 #define SN9C20X_QUEUE_STREAMING (1 << 0)
231 #define SN9C20X_QUEUE_DISCONNECTED (1 << 1)
232 #define SN9C20X_QUEUE_DROP_INCOMPLETE (1 << 2)
234 struct sn9c20x_video_queue {
235 void *mem;
236 unsigned int flags;
237 __u32 sequence;
239 unsigned int count;
240 unsigned int min_buffers;
241 unsigned int max_buffers;
242 unsigned int buf_size;
244 struct sn9c20x_buffer *buffer;
245 struct sn9c20x_buffer *read_buffer;
246 struct mutex mutex; /* protects buffers and mainqueue */
247 spinlock_t irqlock; /* protects irqqueue */
249 struct list_head mainqueue;
250 struct list_head irqqueue;
254 * @struct sn9c20x_urb
256 struct sn9c20x_urb {
257 void *data;
258 struct urb *urb;
262 * @struct sn9c20x_video
264 struct sn9c20x_video {
265 struct v4l2_pix_format format;
266 int fps; /**< FPS setting */
267 int brightness; /**< Brightness setting */
268 int contrast; /**< Contrast setting */
269 int gamma; /**< Gamma setting */
270 int colour; /**< Colour setting */
271 int hue; /**< Hue setting */
272 int hflip; /**< Horizontal flip */
273 int vflip; /**< Vertical flip */
274 int exposure; /**< Exposure */
275 int gain; /**< Gain */
276 int sharpness; /**< Sharpness */
277 int red_gain; /**< Red Gain Control */
278 int blue_gain; /**< Blue Gain Control */
279 int auto_exposure; /**< Automatic exposure */
280 int auto_gain; /**< Automatic gain */
281 int auto_whitebalance; /**< Automatic whitebalance */
285 * @struct sn9c20x_debugfs
287 struct sn9c20x_debugfs {
288 struct dentry *dent_device;
289 struct dentry *dent_bridge_addr;
290 struct dentry *dent_bridge_val;
291 struct dentry *dent_bridge_dump;
292 struct dentry *dent_sensor_addr;
293 struct dentry *dent_sensor_val8;
294 struct dentry *dent_sensor_val16;
295 struct dentry *dent_sensor_val32;
297 __u16 bridge_addr; /**< Current bridge register address */
298 __u8 sensor_addr; /**< Current sensor register address */
301 enum sn9c20x_endpoints {
302 SN9C20X_VID_ISOC = 1,
303 SN9C20X_BULK = 2,
304 SN9C20X_INT = 3,
305 SN9C20X_AUD_ISOC = 4
308 enum sn9c20x_sensors {
309 PROBE_SENSOR = 0,
310 OV9650_SENSOR = 1,
311 OV9655_SENSOR = 2,
312 SOI968_SENSOR = 3,
313 OV7660_SENSOR = 4,
314 OV7670_SENSOR = 5,
315 MT9M111_SENSOR = 6,
316 MT9V111_SENSOR = 7,
317 MT9V011_SENSOR = 8,
318 MT9M001_SENSOR = 9,
319 HV7131R_SENSOR = 10,
320 MT9V112_SENSOR = 11,
325 struct usb_sn9c20x;
327 struct sn9c20x_video_mode {
328 __u16 width;
329 __u16 height;
330 __u8 scale;
331 __u16 window[4];
334 struct sn9c20x_video_format {
335 __u32 pix_fmt;
336 char desc[32];
337 __u8 depth;
338 void (*set_format) (struct usb_sn9c20x *dev);
341 struct sn9c20x_i2c_regs {
342 __u8 address;
343 __u16 value;
346 extern struct sn9c20x_i2c_regs mt9v112_init[];
347 extern struct sn9c20x_i2c_regs mt9v111_init[];
348 extern struct sn9c20x_i2c_regs mt9v011_init[];
349 extern struct sn9c20x_i2c_regs mt9m111_init[];
350 extern struct sn9c20x_i2c_regs mt9m001_init[];
351 extern struct sn9c20x_i2c_regs soi968_init[];
352 extern struct sn9c20x_i2c_regs ov9650_init[];
353 extern struct sn9c20x_i2c_regs ov9655_init[];
354 extern struct sn9c20x_i2c_regs ov7660_init[];
355 extern struct sn9c20x_i2c_regs ov7670_init[];
356 extern struct sn9c20x_i2c_regs hv7131r_init[];
358 #define SN9C20X_N_FMTS 4
359 #define SN9C20X_N_MODES 6
361 extern struct sn9c20x_video_format sn9c20x_fmts[SN9C20X_N_FMTS];
362 extern struct sn9c20x_video_mode sn9c20x_modes[SN9C20X_N_MODES];
364 struct sn9c20x_camera {
366 /* Sensor ID */
367 __u8 sensor;
369 /* SCCB/I2C interface */
370 __u8 i2c_flags;
371 __u8 address;
373 int min_yavg, max_yavg, min_stable_yavg, max_stable_yavg;
375 atomic_t yavg;
377 int vstart;
378 int hstart;
380 int (*flip_detect) (struct usb_sn9c20x *dev);
381 int (*button_detect) (struct usb_sn9c20x *dev);
382 int (*set_hvflip) (struct usb_sn9c20x *dev);
383 /* image quality functions */
384 int (*set_exposure) (struct usb_sn9c20x *dev);
385 int (*set_gain) (struct usb_sn9c20x *dev);
386 int (*set_auto_exposure) (struct usb_sn9c20x *dev);
387 int (*set_auto_gain) (struct usb_sn9c20x *dev);
388 int (*set_auto_whitebalance) (struct usb_sn9c20x *dev);
389 int (*set_contrast) (struct usb_sn9c20x *dev);
390 int (*set_brightness) (struct usb_sn9c20x *dev);
391 int (*set_gamma) (struct usb_sn9c20x *dev);
392 int (*set_sharpness) (struct usb_sn9c20x *dev);
393 int (*set_red_gain) (struct usb_sn9c20x *dev);
394 int (*set_blue_gain) (struct usb_sn9c20x *dev);
398 * @struct usb_sn9c20x
400 struct usb_sn9c20x {
401 struct video_device *vdev; /**< Pointer on a V4L2 video device */
402 struct usb_device *udev; /**< Pointer on a USB device */
403 struct usb_interface *interface; /**< Pointer on a USB interface */
405 int release; /**< Release of the device (bcdDevice) */
407 struct sn9c20x_video vsettings; /**< Video settings (brightness, whiteness...) */
408 struct sn9c20x_debugfs debug; /**< debugfs information structure */
410 struct kref vopen; /**< Video status (Opened or Closed) */
411 struct file *owner; /**< file handler of stream owner */
412 enum sn9c20x_mode mode;/**< camera mode */
414 int vframes_overflow; /**< Buffer overflow frames */
415 int vframes_incomplete; /**< Incomplete frames */
416 int vframes_dropped; /**< Dropped frames */
418 struct mutex mutex; /**< Mutex */
420 struct sn9c20x_urb urbs[MAX_URBS];
422 int resolution;
424 unsigned int frozen:1;
425 struct sn9c20x_video_queue queue;
426 struct sn9c20x_camera camera;
430 * @def SN9C20X_PERCENT
431 * Calculate a value from a percent
433 #define SN9C20X_PERCENT(x, y) (((int)x * (int)y) / 100)
436 extern struct mutex open_lock;
437 extern __u8 jpeg;
439 int usb_sn9c20x_control_write(struct usb_sn9c20x *, __u16, __u8 *, __u16);
440 int usb_sn9c20x_control_read(struct usb_sn9c20x *, __u16, __u8 *, __u16);
442 int usb_sn9c20x_isoc_init(struct usb_sn9c20x *,
443 struct usb_endpoint_descriptor *);
444 void usb_sn9c20x_completion_handler(struct urb *);
445 int usb_sn9c20x_init_urbs(struct usb_sn9c20x *);
446 void usb_sn9c20x_uninit_urbs(struct usb_sn9c20x *);
447 void usb_sn9c20x_delete(struct kref *);
449 int sn9c20x_initialize(struct usb_sn9c20x *dev);
450 int sn9c20x_initialize_sensor(struct usb_sn9c20x *dev);
451 int sn9c20x_enable_video(struct usb_sn9c20x *dev, int enable);
453 int dev_sn9c20x_call_constantly(struct usb_sn9c20x *dev);
454 int dev_sn9c20x_flip_detection(struct usb_sn9c20x *dev);
455 int dev_sn9c20x_button_detection(struct usb_sn9c20x *dev);
456 int dev_sn9c20x_camera_set_exposure(struct usb_sn9c20x *);
457 int dev_sn9c20x_camera_set_gain(struct usb_sn9c20x *);
458 int dev_sn9c20x_camera_set_hvflip(struct usb_sn9c20x *);
459 int dev_sn9c20x_camera_set_auto_exposure(struct usb_sn9c20x *dev);
460 int dev_sn9c20x_camera_set_auto_gain(struct usb_sn9c20x *dev);
461 int dev_sn9c20x_camera_set_auto_whitebalance(struct usb_sn9c20x *dev);
462 int dev_sn9c20x_perform_soft_ae(struct usb_sn9c20x *dev);
464 void v4l2_set_control_default(struct usb_sn9c20x *, __u32, __u16);
465 int v4l_sn9c20x_select_video_mode(struct usb_sn9c20x *, int);
466 int v4l_sn9c20x_register_video_device(struct usb_sn9c20x *);
467 int v4l_sn9c20x_unregister_video_device(struct usb_sn9c20x *);
469 int sn9c20x_create_sysfs_files(struct video_device *);
470 void sn9c20x_remove_sysfs_files(struct video_device *);
472 #ifdef CONFIG_SN9C20X_DEBUGFS
473 void sn9c20x_init_debugfs(void);
474 void sn9c20x_uninit_debugfs(void);
475 int sn9c20x_create_debugfs_files(struct usb_sn9c20x *);
476 int sn9c20x_remove_debugfs_files(struct usb_sn9c20x *);
477 #else
478 static inline void sn9c20x_init_debugfs(void) {}
479 static inline void sn9c20x_uninit_debugfs(void) {}
480 static inline int sn9c20x_create_debugfs_files(struct usb_sn9c20x *dev)
482 return 0;
484 static inline int sn9c20x_remove_debugfs_files(struct usb_sn9c20x *dev)
486 return 0;
488 #endif
490 void sn9c20x_queue_init(struct sn9c20x_video_queue *);
491 int sn9c20x_alloc_buffers(struct sn9c20x_video_queue *,
492 unsigned int, unsigned int);
493 int sn9c20x_free_buffers(struct sn9c20x_video_queue *);
494 int sn9c20x_queue_enable(struct sn9c20x_video_queue *, int);
495 void sn9c20x_queue_cancel(struct sn9c20x_video_queue *, int);
496 unsigned int sn9c20x_queue_poll(struct sn9c20x_video_queue *,
497 struct file *, poll_table *);
498 int sn9c20x_query_buffer(struct sn9c20x_video_queue *, struct v4l2_buffer *);
499 int sn9c20x_queue_buffer(struct sn9c20x_video_queue *, struct v4l2_buffer *);
500 int sn9c20x_dequeue_buffer(struct sn9c20x_video_queue *,
501 struct v4l2_buffer *, int);
502 struct sn9c20x_buffer *sn9c20x_queue_next_buffer(
503 struct sn9c20x_video_queue *, struct sn9c20x_buffer *);
505 static inline int sn9c20x_queue_streaming(struct sn9c20x_video_queue *queue)
507 return queue->flags & SN9C20X_QUEUE_STREAMING;
510 /* sensor hv7131r has no header-file of its own: */
511 int hv7131r_initialize(struct usb_sn9c20x *dev);
512 int hv7131r_set_exposure(struct usb_sn9c20x *dev);
513 int hv7131r_set_gain(struct usb_sn9c20x *dev);
514 int hv7131r_set_hvflip(struct usb_sn9c20x *dev);
516 #endif