V4l2 controls now have a max range of 255.
[microdia.git] / sn9c20x.h
blob222496a426e1f340631b4cbd5f4c8dfbccd2c491
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_BRIDGE (1 << 16)
94 #define SN9C20X_USB_DEVICE(vend, prod, flags) \
95 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
96 USB_DEVICE_ID_MATCH_INT_CLASS, \
97 .idVendor = (vend), \
98 .idProduct = (prod), \
99 .bInterfaceClass = 0xff, \
100 .driver_info = (flags)
102 /** V4L2-CONTROLS for compatibility with older kernels: */
103 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
104 #define V4L2_CID_SHARPNESS (V4L2_CID_PRIVATE_BASE + 0)
105 #define V4L2_CID_EXPOSURE_AUTO (V4L2_CID_PRIVATE_BASE + 1)
106 enum v4l2_exposure_auto_type {
107 V4L2_EXPOSURE_AUTO = 0,
108 V4L2_EXPOSURE_MANUAL = 1,
109 V4L2_EXPOSURE_SHUTTER_PRIORITY = 2,
110 V4L2_EXPOSURE_APERTURE_PRIORITY = 3
112 #endif
115 * @def MAX_URBS
116 * Number maximal of URBS
118 * @def ISO_FRAMES_PER_DESC
119 * Number frames per ISOC descriptor
121 #define MAX_URBS 200
122 #define ISO_FRAMES_PER_DESC 10
125 * @def hb_multiplier(wMaxPacketSize)
126 * USB endpoint high bandwidth multiplier
128 * @def max_packet_sz(wMaxPacketSize)
129 * USB endpoint maximum packet size
131 * These values are both encoded within the wMaxPacketSize field of the usb_endpoint_descriptor structure.
132 * The 11(10:0) lowest bits hold the MaxPacketSize(according to the usb specs a value over 1024 is undefined),
133 * and the bits 12:11 will give the high bandwidth multiplier(this should be a value of 0-2).
136 #define hb_multiplier(wMaxPacketSize) (((wMaxPacketSize >> 11) & 0x03) + 1)
137 #define max_packet_sz(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
140 * @def DRIVER_SUPPORT
142 * List of supported devices
144 #define DRIVER_SUPPORT "SN9C20X USB Camera"
146 #define SN9C20X_INFO (1 << 0)
147 #define SN9C20X_WARNING (1 << 1)
148 #define SN9C20X_ERROR (1 << 2)
149 #define SN9C20X_DEBUG (1 << 3)
150 #define SN9C20X_STREAM (1 << 4)
153 * @def UDIA_INFO(str, args...)
154 * Print information message.
155 * @a Use this function like the function printf.
157 * @def UDIA_ERROR(str, args...)
158 * Print error message.
159 * @a Use this function like the function printf.
161 * @def UDIA_WARNING(str, args...)
162 * Print warning message.
163 * @a Use this function like the function printf.
165 * @def UDIA_DEBUG(str, args...)
166 * Print debug message.
167 * @a Use this function like the function printf.
169 * @def UDIA_STREAM(str, args...)
170 * Print stream debug message.
171 * @a Use this function like the function printf.
173 extern __u8 log_level;
175 #define UDIA_INFO(str, args...) \
176 do { \
177 if (log_level & SN9C20X_INFO) \
178 printk(KERN_INFO PREFIX str, ##args); \
179 } while (0)
181 #define UDIA_WARNING(str, args...) \
182 do { \
183 if (log_level & SN9C20X_WARNING) \
184 printk(KERN_WARNING PREFIX str, ##args); \
185 } while (0)
187 #define UDIA_ERROR(str, args...) \
188 do { \
189 if (log_level & SN9C20X_ERROR) \
190 printk(KERN_ERR PREFIX str, ##args); \
191 } while (0)
193 #define UDIA_DEBUG(str, args...) \
194 do { \
195 if (log_level & SN9C20X_DEBUG) \
196 printk(KERN_DEBUG PREFIX str, ##args); \
197 } while (0)
199 #define UDIA_STREAM(str, args...) \
200 do { \
201 if (log_level & SN9C20X_STREAM) \
202 printk(KERN_DEBUG PREFIX str, ##args); \
203 } while (0)
206 enum sn9c20x_buffer_state {
207 SN9C20X_BUF_STATE_IDLE = 0,
208 SN9C20X_BUF_STATE_QUEUED = 1,
209 SN9C20X_BUF_STATE_ACTIVE = 2,
210 SN9C20X_BUF_STATE_DONE = 3,
211 SN9C20X_BUF_STATE_ERROR = 4,
214 enum sn9c20x_mode {
215 SN9C20X_MODE_IDLE = 0,
216 SN9C20X_MODE_READ = 1,
217 SN9C20X_MODE_STREAM = 2,
220 struct sn9c20x_buffer {
221 unsigned long vma_use_count;
222 struct list_head stream;
224 /* Touched by interrupt handler. */
225 struct v4l2_buffer buf;
226 struct list_head queue;
227 wait_queue_head_t wait;
228 enum sn9c20x_buffer_state state;
231 #define SN9C20X_QUEUE_STREAMING (1 << 0)
232 #define SN9C20X_QUEUE_DISCONNECTED (1 << 1)
233 #define SN9C20X_QUEUE_DROP_INCOMPLETE (1 << 2)
235 struct sn9c20x_video_queue {
236 void *mem;
237 unsigned int flags;
238 __u32 sequence;
240 unsigned int count;
241 unsigned int min_buffers;
242 unsigned int max_buffers;
243 unsigned int buf_size;
245 struct sn9c20x_buffer *buffer;
246 struct sn9c20x_buffer *read_buffer;
247 struct mutex mutex; /* protects buffers and mainqueue */
248 spinlock_t irqlock; /* protects irqqueue */
250 struct list_head mainqueue;
251 struct list_head irqqueue;
255 * @struct sn9c20x_urb
257 struct sn9c20x_urb {
258 void *data;
259 struct urb *urb;
263 * @struct sn9c20x_video
265 struct sn9c20x_video {
266 struct v4l2_pix_format format;
267 int fps; /**< FPS setting */
268 int brightness; /**< Brightness setting */
269 int contrast; /**< Contrast setting */
270 int gamma; /**< Gamma setting */
271 int colour; /**< Colour setting */
272 int hue; /**< Hue setting */
273 int hflip; /**< Horizontal flip */
274 int vflip; /**< Vertical flip */
275 int exposure; /**< Exposure */
276 int gain; /**< Gain */
277 int sharpness; /**< Sharpness */
278 int red_gain; /**< Red Gain Control */
279 int blue_gain; /**< Blue Gain Control */
280 int auto_exposure; /**< Automatic exposure */
281 int auto_gain; /**< Automatic gain */
282 int auto_whitebalance; /**< Automatic whitebalance */
286 * @struct sn9c20x_debugfs
288 struct sn9c20x_debugfs {
289 struct dentry *dent_device;
290 struct dentry *dent_bridge_addr;
291 struct dentry *dent_bridge_val;
292 struct dentry *dent_bridge_dump;
293 struct dentry *dent_sensor_addr;
294 struct dentry *dent_sensor_val8;
295 struct dentry *dent_sensor_val16;
296 struct dentry *dent_sensor_val32;
298 __u16 bridge_addr; /**< Current bridge register address */
299 __u8 sensor_addr; /**< Current sensor register address */
302 enum sn9c20x_endpoints {
303 SN9C20X_VID_ISOC = 1,
304 SN9C20X_BULK = 2,
305 SN9C20X_INT = 3,
306 SN9C20X_AUD_ISOC = 4
309 enum sn9c20x_sensors {
310 UNKNOWN_SENSOR = 0,
311 OV9650_SENSOR = 1,
312 OV9655_SENSOR = 2,
313 SOI968_SENSOR = 3,
314 OV7660_SENSOR = 4,
315 OV7670_SENSOR = 5,
316 MT9M111_SENSOR = 6,
317 MT9V111_SENSOR = 7,
318 MT9V011_SENSOR = 8,
319 MT9M001_SENSOR = 9,
320 HV7131R_SENSOR = 10,
325 struct usb_sn9c20x;
327 #define N_MODES 3
329 struct sn9c20x_video_mode {
330 __u16 width;
331 __u16 height;
332 __u8 scale;
333 __u16 hw_window[4];
334 __u16 clr_window[4];
337 struct sn9c20x_video_format {
338 __u32 pix_fmt;
339 char desc[32];
340 __u8 depth;
341 struct sn9c20x_video_mode modes[N_MODES];
342 int (*set_format) (struct usb_sn9c20x *dev);
345 struct sensor_info {
346 __u8 id;
347 __u8 *name;
348 __u8 address;
350 int min_yavg, max_yavg, min_stable_yavg, max_stable_yavg;
352 int vstart;
353 int hstart;
355 /* sensor specific functions */
356 int (*initialize) (struct usb_sn9c20x *dev);
357 int (*set_exposure) (struct usb_sn9c20x *dev);
358 int (*set_gain) (struct usb_sn9c20x *dev);
359 int (*set_auto_exposure) (struct usb_sn9c20x *dev);
360 int (*set_auto_gain) (struct usb_sn9c20x *dev);
361 int (*set_auto_whitebalance) (struct usb_sn9c20x *dev);
362 int (*flip_detect) (struct usb_sn9c20x *dev);
363 int (*button_detect) (struct usb_sn9c20x *dev);
364 int (*set_hvflip) (struct usb_sn9c20x *dev);
365 int (*set_bayer) (struct usb_sn9c20x *dev);
366 int (*set_yuv422) (struct usb_sn9c20x *dev);
367 int (*get_yavg) (struct usb_sn9c20x *dev);
370 static struct sensor_info sensors[];
372 struct sn9c20x_camera {
374 /* supported resolutions */
375 __u8 nmodes;
376 struct sn9c20x_video_mode *modes;
378 /* supported video formats */
379 __u8 nfmts;
380 struct sn9c20x_video_format *fmts;
382 /* image sensor */
383 struct sensor_info *sensor;
385 /* SCCB/I2C interface */
386 __u8 i2c_flags;
388 /* bridge functions */
389 int (*enable_video) (struct usb_sn9c20x *dev, int enable);
391 /* image quality functions */
392 int (*set_contrast) (struct usb_sn9c20x *dev);
393 int (*set_brightness) (struct usb_sn9c20x *dev);
394 int (*set_gamma) (struct usb_sn9c20x *dev);
395 int (*set_sharpness) (struct usb_sn9c20x *dev);
396 int (*set_red_gain) (struct usb_sn9c20x *dev);
397 int (*set_blue_gain) (struct usb_sn9c20x *dev);
401 * @struct usb_sn9c20x
403 struct usb_sn9c20x {
404 struct video_device *vdev; /**< Pointer on a V4L2 video device */
405 struct usb_device *udev; /**< Pointer on a USB device */
406 struct usb_interface *interface; /**< Pointer on a USB interface */
408 int release; /**< Release of the device (bcdDevice) */
410 struct sn9c20x_video vsettings; /**< Video settings (brightness, whiteness...) */
411 struct sn9c20x_debugfs debug; /**< debugfs information structure */
413 struct kref vopen; /**< Video status (Opened or Closed) */
414 struct file *owner; /**< file handler of stream owner */
415 enum sn9c20x_mode mode;/**< camera mode */
417 int vframes_overflow; /**< Buffer overflow frames */
418 int vframes_incomplete; /**< Incomplete frames */
419 int vframes_dropped; /**< Dropped frames */
421 struct mutex mutex; /**< Mutex */
423 struct sn9c20x_urb urbs[MAX_URBS];
425 int resolution;
427 unsigned int frozen:1;
428 struct sn9c20x_video_queue queue;
429 struct sn9c20x_camera camera;
433 * @def SN9C20X_PERCENT
434 * Calculate a value from a percent
436 #define SN9C20X_PERCENT(x, y) (((int)x * (int)y) / 100)
439 extern struct mutex open_lock;
440 extern int jpeg;
442 int usb_sn9c20x_control_write(struct usb_sn9c20x *, __u16, __u8 *, __u16);
443 int usb_sn9c20x_control_read(struct usb_sn9c20x *, __u16, __u8 *, __u16);
445 int usb_sn9c20x_isoc_init(struct usb_sn9c20x *,
446 struct usb_endpoint_descriptor *);
447 void usb_sn9c20x_completion_handler(struct urb *);
448 int usb_sn9c20x_init_urbs(struct usb_sn9c20x *);
449 void usb_sn9c20x_uninit_urbs(struct usb_sn9c20x *);
450 void usb_sn9c20x_delete(struct kref *);
453 int dev_sn9c20x_initialize_device(struct usb_sn9c20x *, __u32 flags);
454 int dev_sn9c20x_assign_sensor(struct usb_sn9c20x *dev);
455 int dev_sn9c20x_assign_fmts(struct usb_sn9c20x *dev);
456 int dev_sn9c20x_initialize_sensor(struct usb_sn9c20x *dev);
457 int dev_sn9c20x_call_constantly(struct usb_sn9c20x *dev);
458 int dev_sn9c20x_flip_detection(struct usb_sn9c20x *dev);
459 int dev_sn9c20x_button_detection(struct usb_sn9c20x *dev);
460 int dev_sn9c20x_enable_video(struct usb_sn9c20x *, int);
461 int dev_sn9c20x_camera_set_exposure(struct usb_sn9c20x *);
462 int dev_sn9c20x_camera_set_gain(struct usb_sn9c20x *);
463 int dev_sn9c20x_camera_set_hvflip(struct usb_sn9c20x *);
464 int dev_sn9c20x_camera_set_auto_exposure(struct usb_sn9c20x *dev);
465 int dev_sn9c20x_camera_set_auto_gain(struct usb_sn9c20x *dev);
466 int dev_sn9c20x_camera_set_auto_whitebalance(struct usb_sn9c20x *dev);
467 int dev_sn9c20x_perform_soft_ae(struct usb_sn9c20x *dev);
469 int v4l_sn9c20x_select_video_mode(struct usb_sn9c20x *, int);
470 int v4l_sn9c20x_register_video_device(struct usb_sn9c20x *);
471 int v4l_sn9c20x_unregister_video_device(struct usb_sn9c20x *);
473 int sn9c20x_create_sysfs_files(struct video_device *);
474 void sn9c20x_remove_sysfs_files(struct video_device *);
476 #ifdef CONFIG_SN9C20X_DEBUGFS
477 void sn9c20x_init_debugfs(void);
478 void sn9c20x_uninit_debugfs(void);
479 int sn9c20x_create_debugfs_files(struct usb_sn9c20x *);
480 int sn9c20x_remove_debugfs_files(struct usb_sn9c20x *);
481 #else
482 static inline void sn9c20x_init_debugfs(void) {}
483 static inline void sn9c20x_uninit_debugfs(void) {}
484 static inline int sn9c20x_create_debugfs_files(struct usb_sn9c20x *dev)
486 return 0;
488 static inline int sn9c20x_remove_debugfs_files(struct usb_sn9c20x *dev)
490 return 0;
492 #endif
494 void sn9c20x_queue_init(struct sn9c20x_video_queue *);
495 int sn9c20x_alloc_buffers(struct sn9c20x_video_queue *,
496 unsigned int, unsigned int);
497 int sn9c20x_free_buffers(struct sn9c20x_video_queue *);
498 int sn9c20x_queue_enable(struct sn9c20x_video_queue *, int);
499 void sn9c20x_queue_cancel(struct sn9c20x_video_queue *, int);
500 unsigned int sn9c20x_queue_poll(struct sn9c20x_video_queue *,
501 struct file *, poll_table *);
502 int sn9c20x_query_buffer(struct sn9c20x_video_queue *, struct v4l2_buffer *);
503 int sn9c20x_queue_buffer(struct sn9c20x_video_queue *, struct v4l2_buffer *);
504 int sn9c20x_dequeue_buffer(struct sn9c20x_video_queue *,
505 struct v4l2_buffer *, int);
506 struct sn9c20x_buffer *sn9c20x_queue_next_buffer(
507 struct sn9c20x_video_queue *, struct sn9c20x_buffer *);
509 static inline int sn9c20x_queue_streaming(struct sn9c20x_video_queue *queue)
511 return queue->flags & SN9C20X_QUEUE_STREAMING;
514 /* sensor hv7131r has no header-file of its own: */
515 int hv7131r_initialize(struct usb_sn9c20x *dev);
516 int hv7131r_set_exposure(struct usb_sn9c20x *dev);
517 int hv7131r_set_gain(struct usb_sn9c20x *dev);
518 int hv7131r_set_hvflip(struct usb_sn9c20x *dev);
520 #endif