patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / usb / media / w9968cf.h
1 /***************************************************************************
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/spinlock.h>
28 #include <linux/list.h>
29 #include <linux/wait.h>
30 #include <linux/config.h>
31 #include <linux/param.h>
32 #include <asm/semaphore.h>
33 #include <asm/types.h>
34
35 #include "w9968cf_externaldef.h"
36
37
38 /****************************************************************************
39  * Default values                                                           *
40  ****************************************************************************/
41
42 #define W9968CF_VPPMOD_LOAD     1  /* automatic 'w9968cf-vpp' module loading */
43
44 /* Comment/uncomment the following line to enable/disable debugging messages */
45 #define W9968CF_DEBUG
46
47 /* These have effect only if W9968CF_DEBUG is defined */
48 #define W9968CF_DEBUG_LEVEL    2 /* from 0 to 6. 0 for no debug informations */
49 #define W9968CF_SPECIFIC_DEBUG 0 /* 0 or 1 */
50
51 #define W9968CF_MAX_DEVICES    32
52 #define W9968CF_SIMCAMS        W9968CF_MAX_DEVICES /* simultaneous cameras */
53
54 #define W9968CF_MAX_BUFFERS   32
55 #define W9968CF_BUFFERS       2 /* n. of frame buffers from 2 to MAX_BUFFERS */
56
57 /* Maximum data payload sizes in bytes for alternate settings */
58 static const u16 wMaxPacketSize[] = {1023, 959, 895, 831, 767, 703, 639, 575,
59                                       511, 447, 383, 319, 255, 191, 127,  63};
60 #define W9968CF_PACKET_SIZE      1023 /* according to wMaxPacketSizes[] */
61 #define W9968CF_MIN_PACKET_SIZE  63 /* minimum value */
62 #define W9968CF_ISO_PACKETS      5 /* n.of packets for isochronous transfers */
63 #define W9968CF_USB_CTRL_TIMEOUT HZ /* timeout for usb control commands */
64 #define W9968CF_URBS             2 /* n. of scheduled URBs for ISO transfer */
65
66 #define W9968CF_I2C_BUS_DELAY    4 /* delay in us for I2C bit r/w operations */
67 #define W9968CF_I2C_RW_RETRIES   15 /* number of max I2C r/w retries */
68
69 /* Available video formats */
70 struct w9968cf_format {
71         const u16 palette;
72         const u16 depth;
73         const u8 compression;
74 };
75
76 static const struct w9968cf_format w9968cf_formatlist[] = {
77         { VIDEO_PALETTE_UYVY,    16, 0 }, /* original video */
78         { VIDEO_PALETTE_YUV422P, 16, 1 }, /* with JPEG compression */
79         { VIDEO_PALETTE_YUV420P, 12, 1 }, /* with JPEG compression */
80         { VIDEO_PALETTE_YUV420,  12, 1 }, /* same as YUV420P */
81         { VIDEO_PALETTE_YUYV,    16, 0 }, /* software conversion */
82         { VIDEO_PALETTE_YUV422,  16, 0 }, /* software conversion */
83         { VIDEO_PALETTE_GREY,     8, 0 }, /* software conversion */
84         { VIDEO_PALETTE_RGB555,  16, 0 }, /* software conversion */
85         { VIDEO_PALETTE_RGB565,  16, 0 }, /* software conversion */
86         { VIDEO_PALETTE_RGB24,   24, 0 }, /* software conversion */
87         { VIDEO_PALETTE_RGB32,   32, 0 }, /* software conversion */
88         { 0,                      0, 0 }  /* 0 is a terminating entry */
89 };
90
91 #define W9968CF_DECOMPRESSION    2 /* decomp:0=disable,1=force,2=any formats */
92 #define W9968CF_PALETTE_DECOMP_OFF   VIDEO_PALETTE_UYVY    /* when decomp=0 */
93 #define W9968CF_PALETTE_DECOMP_FORCE VIDEO_PALETTE_YUV420P /* when decomp=1 */
94 #define W9968CF_PALETTE_DECOMP_ON    VIDEO_PALETTE_UYVY    /* when decomp=2 */
95
96 #define W9968CF_FORCE_RGB        0  /* read RGB instead of BGR, yes=1/no=0 */
97
98 #define W9968CF_MAX_WIDTH      800 /* should be >= 640 */
99 #define W9968CF_MAX_HEIGHT     600 /* should be >= 480 */
100 #define W9968CF_WIDTH          320 /* from 128 to 352, multiple of 16 */
101 #define W9968CF_HEIGHT         240 /* from  96 to 288, multiple of 16 */
102
103 #define W9968CF_CLAMPING       0 /* 0 disable, 1 enable video data clamping */
104 #define W9968CF_FILTER_TYPE    0 /* 0 disable  1 (1-2-1), 2 (2-3-6-3-2) */
105 #define W9968CF_DOUBLE_BUFFER  1 /* 0 disable, 1 enable double buffer */
106 #define W9968CF_LARGEVIEW      1 /* 0 disable, 1 enable */
107 #define W9968CF_UPSCALING      0 /* 0 disable, 1 enable */
108
109 #define W9968CF_MONOCHROME     0 /* 0 not monochrome, 1 monochrome sensor */
110 #define W9968CF_BRIGHTNESS     31000 /* from 0 to 65535 */
111 #define W9968CF_HUE            32768 /* from 0 to 65535 */
112 #define W9968CF_COLOUR         32768 /* from 0 to 65535 */
113 #define W9968CF_CONTRAST       50000 /* from 0 to 65535 */
114 #define W9968CF_WHITENESS      32768 /* from 0 to 65535 */
115
116 #define W9968CF_AUTOBRIGHT     0 /* 0 disable, 1 enable automatic brightness */
117 #define W9968CF_AUTOEXP        1 /* 0 disable, 1 enable automatic exposure */
118 #define W9968CF_LIGHTFREQ      50 /* light frequency. 50Hz (Europe) or 60Hz */
119 #define W9968CF_BANDINGFILTER  0 /* 0 disable, 1 enable banding filter */
120 #define W9968CF_BACKLIGHT      0 /* 0 or 1, 1=object is lit from behind */
121 #define W9968CF_MIRROR         0 /* 0 or 1 [don't] reverse image horizontally*/
122
123 #define W9968CF_CLOCKDIV         -1 /* -1 = automatic clock divisor */
124 #define W9968CF_DEF_CLOCKDIVISOR  0 /* default sensor clock divisor value */
125
126
127 /****************************************************************************
128  * Globals                                                                  *
129  ****************************************************************************/
130
131 #define W9968CF_MODULE_NAME     "V4L driver for W996[87]CF JPEG USB " \
132                                 "Dual Mode Camera Chip"
133 #define W9968CF_MODULE_VERSION  "v1.25-basic"
134 #define W9968CF_MODULE_AUTHOR   "(C) 2002-2004 Luca Risolia"
135 #define W9968CF_AUTHOR_EMAIL    "<luca.risolia@studio.unibo.it>"
136 #define W9968CF_MODULE_LICENSE  "GPL"
137
138 static u8 w9968cf_vppmod_present; /* status flag: yes=1, no=0 */
139
140 static const struct usb_device_id winbond_id_table[] = {
141         {
142                 /* Creative Labs Video Blaster WebCam Go Plus */
143                 USB_DEVICE(0x041e, 0x4003),
144                 .driver_info = (unsigned long)"w9968cf",
145         },
146         {
147                 /* Generic W996[87]CF JPEG USB Dual Mode Camera */
148                 USB_DEVICE(0x1046, 0x9967),
149                 .driver_info = (unsigned long)"w9968cf",
150         },
151         { } /* terminating entry */
152 };
153
154 MODULE_DEVICE_TABLE(usb, winbond_id_table);
155
156 /* W996[87]CF camera models, internal ids: */
157 enum 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_ADPA5R = 21, /* Aroma Digi Pen ADG-5000 Refurbished */
161         W9986CF_MOD_AU = 31,     /* AVerTV USB */
162         W9968CF_MOD_CLVBWG = 34, /* Creative Labs Video Blaster WebCam Go */
163         W9968CF_MOD_DLLDK = 37,  /* Die Lebon LDC-D35A Digital Kamera */
164         W9968CF_MOD_EEEMC = 40,   /* Ezonics EZ-802 EZMega Cam */
165         W9968CF_MOD_ODPVDMPC = 43,/* OPCOM Digi Pen VGA Dual Mode Pen Camera */
166 };
167
168 enum w9968cf_frame_status {
169         F_READY,            /* finished grabbing & ready to be read/synced */
170         F_GRABBING,         /* in the process of being grabbed into */
171         F_ERROR,            /* something bad happened while processing */
172         F_UNUSED            /* unused (no VIDIOCMCAPTURE) */
173 };
174
175 struct w9968cf_frame_t {
176         #define W9968CF_HW_BUF_SIZE 640*480*2 /* buf.size of original frames */
177         void* buffer;
178         u32 length;
179         enum w9968cf_frame_status status;
180         struct w9968cf_frame_t* next;
181         u8 queued;
182 };
183
184 enum w9968cf_vpp_flag {
185         VPP_NONE = 0x00,
186         VPP_UPSCALE = 0x01,
187         VPP_SWAP_YUV_BYTES = 0x02,
188         VPP_DECOMPRESSION = 0x04,
189         VPP_UYVY_TO_RGBX = 0x08,
190 };
191
192 static struct list_head w9968cf_dev_list; /* head of V4L registered cameras list */
193 static LIST_HEAD(w9968cf_dev_list);
194 struct semaphore w9968cf_devlist_sem; /* semaphore for list traversal */
195
196 /* Main device driver structure */
197 struct w9968cf_device {
198         enum w9968cf_model_id id;   /* private device identifier */
199
200         struct video_device* v4ldev; /* -> V4L structure */
201         struct list_head v4llist;    /* entry of the list of V4L cameras */
202
203         struct usb_device* usbdev;           /* -> main USB structure */
204         struct urb* urb[W9968CF_URBS];       /* -> USB request block structs */
205         void* transfer_buffer[W9968CF_URBS]; /* -> ISO transfer buffers */
206         u16* control_buffer;                 /* -> buffer for control req.*/
207         u16* data_buffer;                    /* -> data to send to the FSB */
208
209         struct w9968cf_frame_t frame[W9968CF_MAX_BUFFERS];
210         struct w9968cf_frame_t frame_tmp;  /* temporary frame */
211         struct w9968cf_frame_t* frame_current; /* -> frame being grabbed */
212         struct w9968cf_frame_t* requested_frame[W9968CF_MAX_BUFFERS];
213         void* vpp_buffer; /*-> helper buf.for video post-processing routines */
214
215         u8 max_buffers,   /* number of requested buffers */
216            force_palette, /* yes=1/no=0 */
217            force_rgb,     /* read RGB instead of BGR, yes=1, no=0 */
218            double_buffer, /* hardware double buffering yes=1/no=0 */
219            clamping,      /* video data clamping yes=1/no=0 */
220            filter_type,   /* 0=disabled, 1=3 tap, 2=5 tap filter */
221            capture,       /* 0=disabled, 1=enabled */
222            largeview,     /* 0=disabled, 1=enabled */
223            decompression, /* 0=disabled, 1=forced, 2=allowed */
224            upscaling;     /* software image scaling, 0=enabled, 1=disabled */
225
226         struct video_picture picture; /* current picture settings */
227         struct video_window window;   /* current window settings */
228
229         u16 hw_depth,    /* depth (used by the chip) */
230             hw_palette,  /* palette (used by the chip) */
231             hw_width,    /* width (used by the chip) */
232             hw_height,   /* height (used by the chip) */
233             hs_polarity, /* 0=negative sync pulse, 1=positive sync pulse */
234             vs_polarity, /* 0=negative sync pulse, 1=positive sync pulse */
235             start_cropx, /* pixels from HS inactive edge to 1st cropped pixel*/
236             start_cropy; /* pixels from VS incative edge to 1st cropped pixel*/
237
238         enum w9968cf_vpp_flag vpp_flag; /* post-processing routines in use */
239
240         u8 nbuffers,      /* number of allocated frame buffers */
241            altsetting,    /* camera alternate setting */
242            disconnected,  /* flag: yes=1, no=0 */
243            misconfigured, /* flag: yes=1, no=0 */
244            users,         /* flag: number of users holding the device */
245            streaming;     /* flag: yes=1, no=0 */
246
247         u8 sensor_initialized; /* flag: yes=1, no=0 */
248
249         /* Determined by CMOS sensor type: */
250         int sensor,       /* type of image sensor chip (CC_*) */
251             monochrome;   /* CMOS sensor is (probably) monochrome */
252         u16 maxwidth,     /* maximum width supported by the CMOS sensor */
253             maxheight,    /* maximum height supported by the CMOS sensor */
254             minwidth,     /* minimum width supported by the CMOS sensor */
255             minheight;    /* minimum height supported by the CMOS sensor */
256         u8  auto_brt,     /* auto brightness enabled flag */
257             auto_exp,     /* auto exposure enabled flag */
258             backlight,    /* backlight exposure algorithm flag */
259             mirror,       /* image is reversed horizontally */
260             lightfreq,    /* power (lighting) frequency */
261             bandfilt;     /* banding filter enabled flag */
262         s8  clockdiv;     /* clock divisor */
263
264         /* I2C interface to kernel */
265         struct i2c_adapter i2c_adapter;
266         struct i2c_client* sensor_client;
267
268         /* Locks */
269         struct semaphore dev_sem,    /* for probe, disconnect,open and close */
270                          fileop_sem; /* for read and ioctl */
271         spinlock_t urb_lock,   /* for submit_urb() and unlink_urb() */
272                    flist_lock; /* for requested frame list accesses */
273         char command[16]; /* name of the program holding the device */
274         wait_queue_head_t open, wait_queue;
275 };
276
277
278 /****************************************************************************
279  * Macros for debugging                                                     *
280  ****************************************************************************/
281
282 #undef DBG
283 #ifdef W9968CF_DEBUG
284 #       define DBG(level, fmt, args...) \
285 { \
286 if ( ((specific_debug) && (debug == (level))) || \
287      ((!specific_debug) && (debug >= (level))) ) { \
288         if ((level) == 1) \
289                 err(fmt, ## args); \
290         else if ((level) == 2 || (level) == 3) \
291                 info(fmt, ## args); \
292         else if ((level) == 4) \
293                 warn(fmt, ## args); \
294         else if ((level) >= 5) \
295                 info("[%s:%d] " fmt, \
296                      __PRETTY_FUNCTION__, __LINE__ , ## args); \
297 } \
298 }
299 #else
300         /* Not debugging: nothing */
301 #       define DBG(level, fmt, args...) do {;} while(0);
302 #endif
303
304 #undef PDBG
305 #undef PDBGG
306 #define PDBG(fmt, args...) info("[%s:%d] "fmt, \
307                                 __PRETTY_FUNCTION__, __LINE__ , ## args);
308 #define PDBGG(fmt, args...) do {;} while(0); /* nothing: it's a placeholder */
309
310 #endif /* _W9968CF_H_ */