3 * @author Nicolas VIVIEN
6 * @brief Driver for SN9C20X USB video cameras
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 "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 SN9C20X_USB_DEVICE(vend, prod, sensor, address) \
44 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
45 USB_DEVICE_ID_MATCH_INT_CLASS, \
47 .idProduct = (prod), \
48 .bInterfaceClass = 0xff, \
49 .driver_info = ((address << 8) | sensor)
51 /** V4L2-CONTROLS for compatibility with older kernels: */
52 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 25)
53 #define V4L2_CID_SHARPNESS (V4L2_CID_PRIVATE_BASE + 0)
54 #define V4L2_CID_EXPOSURE_AUTO (V4L2_CID_PRIVATE_BASE + 1)
57 #ifndef V4L2_PIX_FMT_SN9C20X_I420
58 #define V4L2_PIX_FMT_SN9C20X_I420 v4l2_fourcc('S', '9', '2', '0')
63 * Number maximal of URBS
65 * @def ISO_FRAMES_PER_DESC
66 * Number frames per ISOC descriptor
69 #define ISO_FRAMES_PER_DESC 10
72 * @def hb_multiplier(wMaxPacketSize)
73 * USB endpoint high bandwidth multiplier
75 * @def max_packet_sz(wMaxPacketSize)
76 * USB endpoint maximum packet size
78 * These values are both encoded within the wMaxPacketSize field of the usb_endpoint_descriptor structure.
79 * The 11(10:0) lowest bits hold the MaxPacketSize(according to the usb specs a value over 1024 is undefined),
80 * and the bits 12:11 will give the high bandwidth multiplier(this should be a value of 0-2).
83 #define hb_multiplier(wMaxPacketSize) (((wMaxPacketSize >> 11) & 0x03) + 1)
84 #define max_packet_sz(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
89 * List of supported devices
91 #define DRIVER_SUPPORT "SN9C20X USB Camera"
93 #define SN9C20X_INFO (1 << 0)
94 #define SN9C20X_WARNING (1 << 1)
95 #define SN9C20X_ERROR (1 << 2)
96 #define SN9C20X_DEBUG (1 << 3)
97 #define SN9C20X_STREAM (1 << 4)
100 * @def UDIA_INFO(str, args...)
101 * Print information message.
102 * @a Use this function like the function printf.
104 * @def UDIA_ERROR(str, args...)
105 * Print error message.
106 * @a Use this function like the function printf.
108 * @def UDIA_WARNING(str, args...)
109 * Print warning message.
110 * @a Use this function like the function printf.
112 * @def UDIA_DEBUG(str, args...)
113 * Print debug message.
114 * @a Use this function like the function printf.
116 * @def UDIA_STREAM(str, args...)
117 * Print stream debug message.
118 * @a Use this function like the function printf.
120 extern __u8 log_level
;
122 #define UDIA_INFO(str, args...) \
124 if (log_level & SN9C20X_INFO) \
125 printk(KERN_INFO PREFIX str, ##args); \
128 #define UDIA_WARNING(str, args...) \
130 if (log_level & SN9C20X_WARNING) \
131 printk(KERN_WARNING PREFIX str, ##args); \
134 #define UDIA_ERROR(str, args...) \
136 if (log_level & SN9C20X_ERROR) \
137 printk(KERN_ERR PREFIX str, ##args); \
140 #define UDIA_DEBUG(str, args...) \
142 if (log_level & SN9C20X_DEBUG) \
143 printk(KERN_DEBUG PREFIX str, ##args); \
146 #define UDIA_STREAM(str, args...) \
148 if (log_level & SN9C20X_STREAM) \
149 printk(KERN_DEBUG PREFIX str, ##args); \
153 enum sn9c20x_buffer_state
{
154 SN9C20X_BUF_STATE_IDLE
= 0,
155 SN9C20X_BUF_STATE_QUEUED
= 1,
156 SN9C20X_BUF_STATE_ACTIVE
= 2,
157 SN9C20X_BUF_STATE_DONE
= 3,
158 SN9C20X_BUF_STATE_ERROR
= 4,
162 SN9C20X_MODE_IDLE
= 0,
163 SN9C20X_MODE_READ
= 1,
164 SN9C20X_MODE_STREAM
= 2,
167 struct sn9c20x_buffer
{
168 unsigned long vma_use_count
;
169 struct list_head stream
;
171 /* Touched by interrupt handler. */
172 struct v4l2_buffer buf
;
173 struct list_head queue
;
174 wait_queue_head_t wait
;
175 enum sn9c20x_buffer_state state
;
178 #define SN9C20X_QUEUE_STREAMING (1 << 0)
179 #define SN9C20X_QUEUE_DISCONNECTED (1 << 1)
180 #define SN9C20X_QUEUE_DROP_INCOMPLETE (1 << 2)
182 struct sn9c20x_video_queue
{
188 unsigned int min_buffers
;
189 unsigned int max_buffers
;
190 unsigned int buf_size
;
192 struct sn9c20x_buffer
*buffer
;
193 struct sn9c20x_buffer
*read_buffer
;
194 struct mutex mutex
; /* protects buffers and mainqueue */
195 spinlock_t irqlock
; /* protects irqqueue */
197 struct list_head mainqueue
;
198 struct list_head irqqueue
;
202 * @struct sn9c20x_urb
210 * @struct sn9c20x_video
212 struct sn9c20x_video
{
213 struct v4l2_pix_format format
;
214 int fps
; /**< FPS setting */
215 int brightness
; /**< Brightness setting */
216 int contrast
; /**< Contrast setting */
217 int gamma
; /**< Gamma setting */
218 int colour
; /**< Colour setting */
219 int hue
; /**< Hue setting */
220 int hflip
; /**< Horizontal flip */
221 int vflip
; /**< Vertical flip */
222 int exposure
; /**< Exposure */
223 int gain
; /**< Gain */
224 int sharpness
; /**< Sharpness */
225 int red_gain
; /**< Red Gain Control */
226 int blue_gain
; /**< Blue Gain Control */
227 int auto_exposure
; /**< Automatic exposure */
228 int auto_gain
; /**< Automatic gain */
229 int auto_whitebalance
; /**< Automatic whitebalance */
233 * @struct sn9c20x_debugfs
235 struct sn9c20x_debugfs
{
236 struct dentry
*dent_device
;
237 struct dentry
*dent_bridge_addr
;
238 struct dentry
*dent_bridge_val
;
239 struct dentry
*dent_bridge_dump
;
240 struct dentry
*dent_sensor_addr
;
241 struct dentry
*dent_sensor_val8
;
242 struct dentry
*dent_sensor_val16
;
243 struct dentry
*dent_sensor_val32
;
245 __u16 bridge_addr
; /**< Current bridge register address */
246 __u8 sensor_addr
; /**< Current sensor register address */
249 enum sn9c20x_endpoints
{
250 SN9C20X_VID_ISOC
= 1,
256 enum sn9c20x_sensors
{
275 struct sn9c20x_video_mode
{
282 struct sn9c20x_video_format
{
286 void (*set_format
) (struct usb_sn9c20x
*dev
);
289 struct sn9c20x_i2c_regs
{
294 #define SN9C20X_N_FMTS 3
295 #define SN9C20X_N_MODES 6
297 extern struct sn9c20x_video_format sn9c20x_fmts
[SN9C20X_N_FMTS
];
298 extern struct sn9c20x_video_mode sn9c20x_modes
[SN9C20X_N_MODES
];
300 struct sn9c20x_camera
{
305 /* SCCB/I2C interface */
309 int min_yavg
, max_yavg
, old_step
, older_step
;
310 unsigned int exposure_step
;
317 int (*flip_detect
) (struct usb_sn9c20x
*dev
);
318 int (*button_detect
) (struct usb_sn9c20x
*dev
);
319 int (*set_hvflip
) (struct usb_sn9c20x
*dev
);
320 /* image quality functions */
321 int (*set_exposure
) (struct usb_sn9c20x
*dev
);
322 int (*set_gain
) (struct usb_sn9c20x
*dev
);
323 int (*set_auto_exposure
) (struct usb_sn9c20x
*dev
);
324 int (*set_auto_gain
) (struct usb_sn9c20x
*dev
);
325 int (*set_auto_whitebalance
) (struct usb_sn9c20x
*dev
);
326 int (*set_contrast
) (struct usb_sn9c20x
*dev
);
327 int (*set_brightness
) (struct usb_sn9c20x
*dev
);
328 int (*set_gamma
) (struct usb_sn9c20x
*dev
);
329 int (*set_sharpness
) (struct usb_sn9c20x
*dev
);
330 int (*set_red_gain
) (struct usb_sn9c20x
*dev
);
331 int (*set_blue_gain
) (struct usb_sn9c20x
*dev
);
335 * @struct usb_sn9c20x
338 struct video_device
*vdev
; /**< Pointer on a V4L2 video device */
339 struct usb_device
*udev
; /**< Pointer on a USB device */
340 struct usb_interface
*interface
;/**< Pointer on a USB interface */
342 int release
; /**< Release of the device (bcdDevice) */
344 struct sn9c20x_video vsettings
; /**< Video settings (brightness, whiteness...) */
345 struct sn9c20x_debugfs debug
; /**< debugfs information structure */
347 struct kref vopen
; /**< Video status (Opened or Closed) */
348 struct file
*owner
; /**< file handler of stream owner */
349 enum sn9c20x_mode mode
; /**< camera mode */
351 int vframes_overflow
; /**< Buffer overflow frames */
352 int vframes_incomplete
; /**< Incomplete frames */
353 int vframes_dropped
; /**< Dropped frames */
355 struct sn9c20x_urb urbs
[MAX_URBS
];
359 unsigned int frozen
:1;
360 struct sn9c20x_video_queue queue
;
361 struct sn9c20x_camera camera
;
365 * @def SN9C20X_PERCENT
366 * Calculate a value from a percent
368 #define SN9C20X_PERCENT(x, y) (((int)x * (int)y) / 100)
371 extern struct mutex open_lock
;
374 int usb_sn9c20x_control_write(struct usb_sn9c20x
*, __u16
, __u8
*, __u16
);
375 int usb_sn9c20x_control_read(struct usb_sn9c20x
*, __u16
, __u8
*, __u16
);
377 int usb_sn9c20x_isoc_init(struct usb_sn9c20x
*,
378 struct usb_endpoint_descriptor
*);
379 void usb_sn9c20x_completion_handler(struct urb
*);
380 int usb_sn9c20x_init_urbs(struct usb_sn9c20x
*);
381 void usb_sn9c20x_uninit_urbs(struct usb_sn9c20x
*);
382 void usb_sn9c20x_delete(struct kref
*);
384 int sn9c20x_initialize(struct usb_sn9c20x
*dev
);
385 int sn9c20x_initialize_sensor(struct usb_sn9c20x
*dev
);
386 int sn9c20x_enable_video(struct usb_sn9c20x
*dev
, int enable
);
388 int dev_sn9c20x_call_constantly(struct usb_sn9c20x
*dev
);
389 int dev_sn9c20x_flip_detection(struct usb_sn9c20x
*dev
);
390 int dev_sn9c20x_button_detection(struct usb_sn9c20x
*dev
);
391 int dev_sn9c20x_camera_set_exposure(struct usb_sn9c20x
*);
392 int dev_sn9c20x_camera_set_gain(struct usb_sn9c20x
*);
393 int dev_sn9c20x_camera_set_hvflip(struct usb_sn9c20x
*);
394 int dev_sn9c20x_camera_set_auto_exposure(struct usb_sn9c20x
*dev
);
395 int dev_sn9c20x_camera_set_auto_gain(struct usb_sn9c20x
*dev
);
396 int dev_sn9c20x_camera_set_auto_whitebalance(struct usb_sn9c20x
*dev
);
397 int dev_sn9c20x_perform_soft_ae(struct usb_sn9c20x
*dev
);
399 void v4l2_set_control_default(struct usb_sn9c20x
*, __u32
, __u16
);
400 int v4l_sn9c20x_select_video_mode(struct usb_sn9c20x
*, int);
401 int v4l_sn9c20x_register_video_device(struct usb_sn9c20x
*);
402 int v4l_sn9c20x_unregister_video_device(struct usb_sn9c20x
*);
404 int sn9c20x_create_sysfs_files(struct video_device
*);
405 void sn9c20x_remove_sysfs_files(struct video_device
*);
407 #ifdef CONFIG_SN9C20X_DEBUGFS
408 void sn9c20x_init_debugfs(void);
409 void sn9c20x_uninit_debugfs(void);
410 int sn9c20x_create_debugfs_files(struct usb_sn9c20x
*);
411 int sn9c20x_remove_debugfs_files(struct usb_sn9c20x
*);
413 static inline void sn9c20x_init_debugfs(void) {}
414 static inline void sn9c20x_uninit_debugfs(void) {}
415 static inline int sn9c20x_create_debugfs_files(struct usb_sn9c20x
*dev
)
419 static inline int sn9c20x_remove_debugfs_files(struct usb_sn9c20x
*dev
)
425 void sn9c20x_queue_init(struct sn9c20x_video_queue
*);
426 int sn9c20x_alloc_buffers(struct sn9c20x_video_queue
*,
427 unsigned int, unsigned int);
428 int sn9c20x_free_buffers(struct sn9c20x_video_queue
*);
429 int sn9c20x_queue_enable(struct sn9c20x_video_queue
*, int);
430 void sn9c20x_queue_cancel(struct sn9c20x_video_queue
*, int);
431 unsigned int sn9c20x_queue_poll(struct sn9c20x_video_queue
*,
432 struct file
*, poll_table
*);
433 int sn9c20x_query_buffer(struct sn9c20x_video_queue
*, struct v4l2_buffer
*);
434 int sn9c20x_queue_buffer(struct sn9c20x_video_queue
*, struct v4l2_buffer
*);
435 int sn9c20x_dequeue_buffer(struct sn9c20x_video_queue
*,
436 struct v4l2_buffer
*, int);
437 struct sn9c20x_buffer
*sn9c20x_queue_next_buffer(
438 struct sn9c20x_video_queue
*, struct sn9c20x_buffer
*);
440 static inline int sn9c20x_queue_streaming(struct sn9c20x_video_queue
*queue
)
442 return queue
->flags
& SN9C20X_QUEUE_STREAMING
;
445 /* sensor hv7131r has no header-file of its own: */
446 extern struct sn9c20x_i2c_regs hv7131r_init
[];
447 int hv7131r_initialize(struct usb_sn9c20x
*dev
);
448 int hv7131r_set_exposure(struct usb_sn9c20x
*dev
);
449 int hv7131r_set_gain(struct usb_sn9c20x
*dev
);
450 int hv7131r_set_hvflip(struct usb_sn9c20x
*dev
);