blob: ec7696e8f1fac3eaf3b47725c37db3ec1dde8154 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/***************************************************************************
2 * Video4Linux driver for W996[87]CF JPEG USB Dual Mode Camera Chip. *
3 * *
4 * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#ifndef _W9968CF_H_
22#define _W9968CF_H_
23
24#include <linux/videodev.h>
25#include <linux/usb.h>
26#include <linux/i2c.h>
27#include <linux/device.h>
28#include <linux/spinlock.h>
29#include <linux/list.h>
30#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/param.h>
32#include <linux/types.h>
33#include <linux/rwsem.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010034#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <media/ovcamchip.h>
37
38#include "w9968cf_vpp.h"
39
40
41/****************************************************************************
42 * Default values *
43 ****************************************************************************/
44
45#define W9968CF_OVMOD_LOAD 1 /* automatic 'ovcamchip' module loading */
46#define W9968CF_VPPMOD_LOAD 1 /* automatic 'w9968cf-vpp' module loading */
47
48/* Comment/uncomment the following line to enable/disable debugging messages */
49#define W9968CF_DEBUG
50
51/* These have effect only if W9968CF_DEBUG is defined */
52#define W9968CF_DEBUG_LEVEL 2 /* from 0 to 6. 0 for no debug informations */
53#define W9968CF_SPECIFIC_DEBUG 0 /* 0 or 1 */
54
55#define W9968CF_MAX_DEVICES 32
56#define W9968CF_SIMCAMS W9968CF_MAX_DEVICES /* simultaneous cameras */
57
58#define W9968CF_MAX_BUFFERS 32
59#define W9968CF_BUFFERS 2 /* n. of frame buffers from 2 to MAX_BUFFERS */
60
61/* Maximum data payload sizes in bytes for alternate settings */
62static const u16 wMaxPacketSize[] = {1023, 959, 895, 831, 767, 703, 639, 575,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030063 511, 447, 383, 319, 255, 191, 127, 63};
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define W9968CF_PACKET_SIZE 1023 /* according to wMaxPacketSizes[] */
65#define W9968CF_MIN_PACKET_SIZE 63 /* minimum value */
66#define W9968CF_ISO_PACKETS 5 /* n.of packets for isochronous transfers */
67#define W9968CF_USB_CTRL_TIMEOUT 1000 /* timeout (ms) for usb control commands */
68#define W9968CF_URBS 2 /* n. of scheduled URBs for ISO transfer */
69
70#define W9968CF_I2C_BUS_DELAY 4 /* delay in us for I2C bit r/w operations */
71#define W9968CF_I2C_RW_RETRIES 15 /* number of max I2C r/w retries */
72
73/* Available video formats */
74struct w9968cf_format {
75 const u16 palette;
76 const u16 depth;
77 const u8 compression;
78};
79
80static const struct w9968cf_format w9968cf_formatlist[] = {
81 { VIDEO_PALETTE_UYVY, 16, 0 }, /* original video */
82 { VIDEO_PALETTE_YUV422P, 16, 1 }, /* with JPEG compression */
83 { VIDEO_PALETTE_YUV420P, 12, 1 }, /* with JPEG compression */
84 { VIDEO_PALETTE_YUV420, 12, 1 }, /* same as YUV420P */
85 { VIDEO_PALETTE_YUYV, 16, 0 }, /* software conversion */
86 { VIDEO_PALETTE_YUV422, 16, 0 }, /* software conversion */
87 { VIDEO_PALETTE_GREY, 8, 0 }, /* software conversion */
88 { VIDEO_PALETTE_RGB555, 16, 0 }, /* software conversion */
89 { VIDEO_PALETTE_RGB565, 16, 0 }, /* software conversion */
90 { VIDEO_PALETTE_RGB24, 24, 0 }, /* software conversion */
91 { VIDEO_PALETTE_RGB32, 32, 0 }, /* software conversion */
92 { 0, 0, 0 } /* 0 is a terminating entry */
93};
94
95#define W9968CF_DECOMPRESSION 2 /* decomp:0=disable,1=force,2=any formats */
96#define W9968CF_PALETTE_DECOMP_OFF VIDEO_PALETTE_UYVY /* when decomp=0 */
97#define W9968CF_PALETTE_DECOMP_FORCE VIDEO_PALETTE_YUV420P /* when decomp=1 */
98#define W9968CF_PALETTE_DECOMP_ON VIDEO_PALETTE_UYVY /* when decomp=2 */
99
100#define W9968CF_FORCE_RGB 0 /* read RGB instead of BGR, yes=1/no=0 */
101
102#define W9968CF_MAX_WIDTH 800 /* Has effect if up-scaling is on */
103#define W9968CF_MAX_HEIGHT 600 /* Has effect if up-scaling is on */
104#define W9968CF_WIDTH 320 /* from 128 to 352, multiple of 16 */
105#define W9968CF_HEIGHT 240 /* from 96 to 288, multiple of 16 */
106
107#define W9968CF_CLAMPING 0 /* 0 disable, 1 enable video data clamping */
108#define W9968CF_FILTER_TYPE 0 /* 0 disable 1 (1-2-1), 2 (2-3-6-3-2) */
109#define W9968CF_DOUBLE_BUFFER 1 /* 0 disable, 1 enable double buffer */
110#define W9968CF_LARGEVIEW 1 /* 0 disable, 1 enable */
111#define W9968CF_UPSCALING 0 /* 0 disable, 1 enable */
112
113#define W9968CF_MONOCHROME 0 /* 0 not monochrome, 1 monochrome sensor */
114#define W9968CF_BRIGHTNESS 31000 /* from 0 to 65535 */
115#define W9968CF_HUE 32768 /* from 0 to 65535 */
116#define W9968CF_COLOUR 32768 /* from 0 to 65535 */
117#define W9968CF_CONTRAST 50000 /* from 0 to 65535 */
118#define W9968CF_WHITENESS 32768 /* from 0 to 65535 */
119
120#define W9968CF_AUTOBRIGHT 0 /* 0 disable, 1 enable automatic brightness */
121#define W9968CF_AUTOEXP 1 /* 0 disable, 1 enable automatic exposure */
122#define W9968CF_LIGHTFREQ 50 /* light frequency. 50Hz (Europe) or 60Hz */
123#define W9968CF_BANDINGFILTER 0 /* 0 disable, 1 enable banding filter */
124#define W9968CF_BACKLIGHT 0 /* 0 or 1, 1=object is lit from behind */
125#define W9968CF_MIRROR 0 /* 0 or 1 [don't] reverse image horizontally*/
126
127#define W9968CF_CLOCKDIV -1 /* -1 = automatic clock divisor */
128#define W9968CF_DEF_CLOCKDIVISOR 0 /* default sensor clock divisor value */
129
130
131/****************************************************************************
132 * Globals *
133 ****************************************************************************/
134
135#define W9968CF_MODULE_NAME "V4L driver for W996[87]CF JPEG USB " \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300136 "Dual Mode Camera Chip"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define W9968CF_MODULE_VERSION "1:1.33-basic"
138#define W9968CF_MODULE_AUTHOR "(C) 2002-2004 Luca Risolia"
139#define W9968CF_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
140#define W9968CF_MODULE_LICENSE "GPL"
141
142static const struct usb_device_id winbond_id_table[] = {
143 {
144 /* Creative Labs Video Blaster WebCam Go Plus */
145 USB_DEVICE(0x041e, 0x4003),
146 .driver_info = (unsigned long)"w9968cf",
147 },
148 {
149 /* Generic W996[87]CF JPEG USB Dual Mode Camera */
150 USB_DEVICE(0x1046, 0x9967),
151 .driver_info = (unsigned long)"w9968cf",
152 },
153 { } /* terminating entry */
154};
155
156/* W996[87]CF camera models, internal ids: */
157enum w9968cf_model_id {
158 W9968CF_MOD_GENERIC = 1, /* Generic W996[87]CF based device */
159 W9968CF_MOD_CLVBWGP = 11,/*Creative Labs Video Blaster WebCam Go Plus*/
160 W9968CF_MOD_ADPVDMA = 21, /* Aroma Digi Pen VGA Dual Mode ADG-5000 */
161 W9986CF_MOD_AAU = 31, /* AVerMedia AVerTV USB */
162 W9968CF_MOD_CLVBWG = 34, /* Creative Labs Video Blaster WebCam Go */
163 W9968CF_MOD_LL = 37, /* Lebon LDC-035A */
164 W9968CF_MOD_EEEMC = 40, /* Ezonics EZ-802 EZMega Cam */
165 W9968CF_MOD_OOE = 42, /* OmniVision OV8610-EDE */
166 W9968CF_MOD_ODPVDMPC = 43,/* OPCOM Digi Pen VGA Dual Mode Pen Camera */
167 W9968CF_MOD_PDPII = 46, /* Pretec Digi Pen-II */
168 W9968CF_MOD_PDP480 = 49, /* Pretec DigiPen-480 */
169};
170
171enum w9968cf_frame_status {
172 F_READY, /* finished grabbing & ready to be read/synced */
173 F_GRABBING, /* in the process of being grabbed into */
174 F_ERROR, /* something bad happened while processing */
175 F_UNUSED /* unused (no VIDIOCMCAPTURE) */
176};
177
178struct w9968cf_frame_t {
179 void* buffer;
180 unsigned long size;
181 u32 length;
182 int number;
183 enum w9968cf_frame_status status;
184 struct w9968cf_frame_t* next;
185 u8 queued;
186};
187
188enum w9968cf_vpp_flag {
189 VPP_NONE = 0x00,
190 VPP_UPSCALE = 0x01,
191 VPP_SWAP_YUV_BYTES = 0x02,
192 VPP_DECOMPRESSION = 0x04,
193 VPP_UYVY_TO_RGBX = 0x08,
194};
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* Main device driver structure */
197struct w9968cf_device {
198 struct device dev; /* device structure */
199
200 enum w9968cf_model_id id; /* private device identifier */
201
202 struct video_device* v4ldev; /* -> V4L structure */
203 struct list_head v4llist; /* entry of the list of V4L cameras */
204
205 struct usb_device* usbdev; /* -> main USB structure */
206 struct urb* urb[W9968CF_URBS]; /* -> USB request block structs */
207 void* transfer_buffer[W9968CF_URBS]; /* -> ISO transfer buffers */
208 u16* control_buffer; /* -> buffer for control req.*/
209 u16* data_buffer; /* -> data to send to the FSB */
210
211 struct w9968cf_frame_t frame[W9968CF_MAX_BUFFERS];
212 struct w9968cf_frame_t frame_tmp; /* temporary frame */
213 struct w9968cf_frame_t frame_vpp; /* helper frame.*/
214 struct w9968cf_frame_t* frame_current; /* -> frame being grabbed */
215 struct w9968cf_frame_t* requested_frame[W9968CF_MAX_BUFFERS];
216
217 u8 max_buffers, /* number of requested buffers */
218 force_palette, /* yes=1/no=0 */
219 force_rgb, /* read RGB instead of BGR, yes=1, no=0 */
220 double_buffer, /* hardware double buffering yes=1/no=0 */
221 clamping, /* video data clamping yes=1/no=0 */
222 filter_type, /* 0=disabled, 1=3 tap, 2=5 tap filter */
223 capture, /* 0=disabled, 1=enabled */
224 largeview, /* 0=disabled, 1=enabled */
225 decompression, /* 0=disabled, 1=forced, 2=allowed */
226 upscaling; /* software image scaling, 0=enabled, 1=disabled */
227
228 struct video_picture picture; /* current picture settings */
229 struct video_window window; /* current window settings */
230
231 u16 hw_depth, /* depth (used by the chip) */
232 hw_palette, /* palette (used by the chip) */
233 hw_width, /* width (used by the chip) */
234 hw_height, /* height (used by the chip) */
235 hs_polarity, /* 0=negative sync pulse, 1=positive sync pulse */
236 vs_polarity, /* 0=negative sync pulse, 1=positive sync pulse */
237 start_cropx, /* pixels from HS inactive edge to 1st cropped pixel*/
238 start_cropy; /* pixels from VS inactive edge to 1st cropped pixel*/
239
240 enum w9968cf_vpp_flag vpp_flag; /* post-processing routines in use */
241
242 u8 nbuffers, /* number of allocated frame buffers */
243 altsetting, /* camera alternate setting */
244 disconnected, /* flag: yes=1, no=0 */
245 misconfigured, /* flag: yes=1, no=0 */
246 users, /* flag: number of users holding the device */
247 streaming; /* flag: yes=1, no=0 */
248
249 u8 sensor_initialized; /* flag: yes=1, no=0 */
250
251 /* Determined by the image sensor type: */
252 int sensor, /* type of image sensor chip (CC_*) */
253 monochrome; /* image sensor is (probably) monochrome */
254 u16 maxwidth, /* maximum width supported by the image sensor */
255 maxheight, /* maximum height supported by the image sensor */
256 minwidth, /* minimum width supported by the image sensor */
257 minheight; /* minimum height supported by the image sensor */
258 u8 auto_brt, /* auto brightness enabled flag */
259 auto_exp, /* auto exposure enabled flag */
260 backlight, /* backlight exposure algorithm flag */
261 mirror, /* image is reversed horizontally */
262 lightfreq, /* power (lighting) frequency */
263 bandfilt; /* banding filter enabled flag */
264 s8 clockdiv; /* clock divisor */
265
266 /* I2C interface to kernel */
267 struct i2c_adapter i2c_adapter;
268 struct i2c_client* sensor_client;
269
270 /* Locks */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100271 struct mutex dev_mutex, /* for probe, disconnect,open and close */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300272 fileop_mutex; /* for read and ioctl */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 spinlock_t urb_lock, /* for submit_urb() and unlink_urb() */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300274 flist_lock; /* for requested frame list accesses */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 wait_queue_head_t open, wait_queue;
276
277 char command[16]; /* name of the program holding the device */
278};
279
280
281/****************************************************************************
282 * Macros for debugging *
283 ****************************************************************************/
284
285#undef DBG
286#undef KDBG
287#ifdef W9968CF_DEBUG
288/* For device specific debugging messages */
289# define DBG(level, fmt, args...) \
290{ \
291 if ( ((specific_debug) && (debug == (level))) || \
292 ((!specific_debug) && (debug >= (level))) ) { \
293 if ((level) == 1) \
294 dev_err(&cam->dev, fmt "\n", ## args); \
295 else if ((level) == 2 || (level) == 3) \
296 dev_info(&cam->dev, fmt "\n", ## args); \
297 else if ((level) == 4) \
298 dev_warn(&cam->dev, fmt "\n", ## args); \
299 else if ((level) >= 5) \
300 dev_info(&cam->dev, "[%s:%d] " fmt "\n", \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300301 __FUNCTION__, __LINE__ , ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 } \
303}
304/* For generic kernel (not device specific) messages */
305# define KDBG(level, fmt, args...) \
306{ \
307 if ( ((specific_debug) && (debug == (level))) || \
308 ((!specific_debug) && (debug >= (level))) ) { \
309 if ((level) >= 1 && (level) <= 4) \
310 pr_info("w9968cf: " fmt "\n", ## args); \
311 else if ((level) >= 5) \
312 pr_debug("w9968cf: [%s:%d] " fmt "\n", __FUNCTION__, \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300313 __LINE__ , ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 } \
315}
316#else
317 /* Not debugging: nothing */
318# define DBG(level, fmt, args...) do {;} while(0);
319# define KDBG(level, fmt, args...) do {;} while(0);
320#endif
321
322#undef PDBG
323#define PDBG(fmt, args...) \
324dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args);
325
326#undef PDBGG
327#define PDBGG(fmt, args...) do {;} while(0); /* nothing: it's a placeholder */
328
329#endif /* _W9968CF_H_ */