Linux-2.6.12-rc2
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / media / usbvideo.h
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2, or (at your option)
5 * any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16 #ifndef usbvideo_h
17 #define usbvideo_h
18
19 #include <linux/config.h>
20 #include <linux/videodev.h>
21 #include <linux/usb.h>
22
23 /* Most helpful debugging aid */
24 #define assert(expr) ((void) ((expr) ? 0 : (err("assert failed at line %d",__LINE__))))
25
26 #define USBVIDEO_REPORT_STATS 1 /* Set to 0 to block statistics on close */
27
28 /* Bit flags (options) */
29 #define FLAGS_RETRY_VIDIOCSYNC (1 << 0)
30 #define FLAGS_MONOCHROME (1 << 1)
31 #define FLAGS_DISPLAY_HINTS (1 << 2)
32 #define FLAGS_OVERLAY_STATS (1 << 3)
33 #define FLAGS_FORCE_TESTPATTERN (1 << 4)
34 #define FLAGS_SEPARATE_FRAMES (1 << 5)
35 #define FLAGS_CLEAN_FRAMES (1 << 6)
36 #define FLAGS_NO_DECODING (1 << 7)
37
38 /* Bit flags for frames (apply to the frame where they are specified) */
39 #define USBVIDEO_FRAME_FLAG_SOFTWARE_CONTRAST (1 << 0)
40
41 /* Camera capabilities (maximum) */
42 #define CAMERA_URB_FRAMES 32
43 #define CAMERA_MAX_ISO_PACKET 1023 /* 1022 actually sent by camera */
44 #define FRAMES_PER_DESC (CAMERA_URB_FRAMES)
45 #define FRAME_SIZE_PER_DESC (CAMERA_MAX_ISO_PACKET)
46
47 /* This macro restricts an int variable to an inclusive range */
48 #define RESTRICT_TO_RANGE(v,mi,ma) { if ((v) < (mi)) (v) = (mi); else if ((v) > (ma)) (v) = (ma); }
49
50 #define V4L_BYTES_PER_PIXEL 3 /* Because we produce RGB24 */
51
52 /*
53 * Use this macro to construct constants for different video sizes.
54 * We have to deal with different video sizes that have to be
55 * configured in the device or compared against when we receive
56 * a data. Normally one would define a bunch of VIDEOSIZE_x_by_y
57 * #defines and that's the end of story. However this solution
58 * does not allow to convert between real pixel sizes and the
59 * constant (integer) value that may be used to tag a frame or
60 * whatever. The set of macros below constructs videosize constants
61 * from the pixel size and allows to reconstruct the pixel size
62 * from the combined value later.
63 */
64 #define VIDEOSIZE(x,y) (((x) & 0xFFFFL) | (((y) & 0xFFFFL) << 16))
65 #define VIDEOSIZE_X(vs) ((vs) & 0xFFFFL)
66 #define VIDEOSIZE_Y(vs) (((vs) >> 16) & 0xFFFFL)
67 typedef unsigned long videosize_t;
68
69 /*
70 * This macro checks if the camera is still operational. The 'uvd'
71 * pointer must be valid, uvd->dev must be valid, we are not
72 * removing the device and the device has not erred on us.
73 */
74 #define CAMERA_IS_OPERATIONAL(uvd) (\
75 (uvd != NULL) && \
76 ((uvd)->dev != NULL) && \
77 ((uvd)->last_error == 0) && \
78 (!(uvd)->remove_pending))
79
80 /*
81 * We use macros to do YUV -> RGB conversion because this is
82 * very important for speed and totally unimportant for size.
83 *
84 * YUV -> RGB Conversion
85 * ---------------------
86 *
87 * B = 1.164*(Y-16) + 2.018*(V-128)
88 * G = 1.164*(Y-16) - 0.813*(U-128) - 0.391*(V-128)
89 * R = 1.164*(Y-16) + 1.596*(U-128)
90 *
91 * If you fancy integer arithmetics (as you should), hear this:
92 *
93 * 65536*B = 76284*(Y-16) + 132252*(V-128)
94 * 65536*G = 76284*(Y-16) - 53281*(U-128) - 25625*(V-128)
95 * 65536*R = 76284*(Y-16) + 104595*(U-128)
96 *
97 * Make sure the output values are within [0..255] range.
98 */
99 #define LIMIT_RGB(x) (((x) < 0) ? 0 : (((x) > 255) ? 255 : (x)))
100 #define YUV_TO_RGB_BY_THE_BOOK(my,mu,mv,mr,mg,mb) { \
101 int mm_y, mm_yc, mm_u, mm_v, mm_r, mm_g, mm_b; \
102 mm_y = (my) - 16; \
103 mm_u = (mu) - 128; \
104 mm_v = (mv) - 128; \
105 mm_yc= mm_y * 76284; \
106 mm_b = (mm_yc + 132252*mm_v ) >> 16; \
107 mm_g = (mm_yc - 53281*mm_u - 25625*mm_v ) >> 16; \
108 mm_r = (mm_yc + 104595*mm_u ) >> 16; \
109 mb = LIMIT_RGB(mm_b); \
110 mg = LIMIT_RGB(mm_g); \
111 mr = LIMIT_RGB(mm_r); \
112 }
113
114 #define RING_QUEUE_SIZE (128*1024) /* Must be a power of 2 */
115 #define RING_QUEUE_ADVANCE_INDEX(rq,ind,n) (rq)->ind = ((rq)->ind + (n)) & ((rq)->length-1)
116 #define RING_QUEUE_DEQUEUE_BYTES(rq,n) RING_QUEUE_ADVANCE_INDEX(rq,ri,n)
117 #define RING_QUEUE_PEEK(rq,ofs) ((rq)->queue[((ofs) + (rq)->ri) & ((rq)->length-1)])
118
119 struct RingQueue {
120 unsigned char *queue; /* Data from the Isoc data pump */
121 int length; /* How many bytes allocated for the queue */
122 int wi; /* That's where we write */
123 int ri; /* Read from here until you hit write index */
124 wait_queue_head_t wqh; /* Processes waiting */
125 };
126
127 enum ScanState {
128 ScanState_Scanning, /* Scanning for header */
129 ScanState_Lines /* Parsing lines */
130 };
131
132 /* Completion states of the data parser */
133 enum ParseState {
134 scan_Continue, /* Just parse next item */
135 scan_NextFrame, /* Frame done, send it to V4L */
136 scan_Out, /* Not enough data for frame */
137 scan_EndParse /* End parsing */
138 };
139
140 enum FrameState {
141 FrameState_Unused, /* Unused (no MCAPTURE) */
142 FrameState_Ready, /* Ready to start grabbing */
143 FrameState_Grabbing, /* In the process of being grabbed into */
144 FrameState_Done, /* Finished grabbing, but not been synced yet */
145 FrameState_Done_Hold, /* Are syncing or reading */
146 FrameState_Error, /* Something bad happened while processing */
147 };
148
149 /*
150 * Some frames may contain only even or odd lines. This type
151 * specifies what type of deinterlacing is required.
152 */
153 enum Deinterlace {
154 Deinterlace_None=0,
155 Deinterlace_FillOddLines,
156 Deinterlace_FillEvenLines
157 };
158
159 #define USBVIDEO_NUMFRAMES 2 /* How many frames we work with */
160 #define USBVIDEO_NUMSBUF 2 /* How many URBs linked in a ring */
161
162 /* This structure represents one Isoc request - URB and buffer */
163 struct usbvideo_sbuf {
164 char *data;
165 struct urb *urb;
166 };
167
168 struct usbvideo_frame {
169 char *data; /* Frame buffer */
170 unsigned long header; /* Significant bits from the header */
171
172 videosize_t canvas; /* The canvas (max. image) allocated */
173 videosize_t request; /* That's what the application asked for */
174 unsigned short palette; /* The desired format */
175
176 enum FrameState frameState;/* State of grabbing */
177 enum ScanState scanstate; /* State of scanning */
178 enum Deinterlace deinterlace;
179 int flags; /* USBVIDEO_FRAME_FLAG_xxx bit flags */
180
181 int curline; /* Line of frame we're working on */
182
183 long seqRead_Length; /* Raw data length of frame */
184 long seqRead_Index; /* Amount of data that has been already read */
185
186 void *user; /* Additional data that user may need */
187 };
188
189 /* Statistics that can be overlaid on screen */
190 struct usbvideo_statistics {
191 unsigned long frame_num; /* Sequential number of the frame */
192 unsigned long urb_count; /* How many URBs we received so far */
193 unsigned long urb_length; /* Length of last URB */
194 unsigned long data_count; /* How many bytes we received */
195 unsigned long header_count; /* How many frame headers we found */
196 unsigned long iso_skip_count; /* How many empty ISO packets received */
197 unsigned long iso_err_count; /* How many bad ISO packets received */
198 };
199
200 struct usbvideo;
201
202 struct uvd {
203 struct video_device vdev; /* Must be the first field! */
204 struct usb_device *dev;
205 struct usbvideo *handle; /* Points back to the struct usbvideo */
206 void *user_data; /* Camera-dependent data */
207 int user_size; /* Size of that camera-dependent data */
208 int debug; /* Debug level for usbvideo */
209 unsigned char iface; /* Video interface number */
210 unsigned char video_endp;
211 unsigned char ifaceAltActive;
212 unsigned char ifaceAltInactive; /* Alt settings */
213 unsigned long flags; /* FLAGS_USBVIDEO_xxx */
214 unsigned long paletteBits; /* Which palettes we accept? */
215 unsigned short defaultPalette; /* What palette to use for read() */
216 struct semaphore lock;
217 int user; /* user count for exclusive use */
218
219 videosize_t videosize; /* Current setting */
220 videosize_t canvas; /* This is the width,height of the V4L canvas */
221 int max_frame_size; /* Bytes in one video frame */
222
223 int uvd_used; /* Is this structure in use? */
224 int streaming; /* Are we streaming Isochronous? */
225 int grabbing; /* Are we grabbing? */
226 int settingsAdjusted; /* Have we adjusted contrast etc.? */
227 int last_error; /* What calamity struck us? */
228
229 char *fbuf; /* Videodev buffer area */
230 int fbuf_size; /* Videodev buffer size */
231
232 int curframe;
233 int iso_packet_len; /* Videomode-dependent, saves bus bandwidth */
234
235 struct RingQueue dp; /* Isoc data pump */
236 struct usbvideo_frame frame[USBVIDEO_NUMFRAMES];
237 struct usbvideo_sbuf sbuf[USBVIDEO_NUMSBUF];
238
239 volatile int remove_pending; /* If set then about to exit */
240
241 struct video_picture vpic, vpic_old; /* Picture settings */
242 struct video_capability vcap; /* Video capabilities */
243 struct video_channel vchan; /* May be used for tuner support */
244 struct usbvideo_statistics stats;
245 char videoName[32]; /* Holds name like "video7" */
246 };
247
248 /*
249 * usbvideo callbacks (virtual methods). They are set when usbvideo
250 * services are registered. All of these default to NULL, except those
251 * that default to usbvideo-provided methods.
252 */
253 struct usbvideo_cb {
254 int (*probe)(struct usb_interface *, const struct usb_device_id *);
255 void (*userFree)(struct uvd *);
256 void (*disconnect)(struct usb_interface *);
257 int (*setupOnOpen)(struct uvd *);
258 void (*videoStart)(struct uvd *);
259 void (*videoStop)(struct uvd *);
260 void (*processData)(struct uvd *, struct usbvideo_frame *);
261 void (*postProcess)(struct uvd *, struct usbvideo_frame *);
262 void (*adjustPicture)(struct uvd *);
263 int (*getFPS)(struct uvd *);
264 int (*overlayHook)(struct uvd *, struct usbvideo_frame *);
265 int (*getFrame)(struct uvd *, int);
266 int (*startDataPump)(struct uvd *uvd);
267 void (*stopDataPump)(struct uvd *uvd);
268 int (*setVideoMode)(struct uvd *uvd, struct video_window *vw);
269 };
270
271 struct usbvideo {
272 int num_cameras; /* As allocated */
273 struct usb_driver usbdrv; /* Interface to the USB stack */
274 char drvName[80]; /* Driver name */
275 struct semaphore lock; /* Mutex protecting camera structures */
276 struct usbvideo_cb cb; /* Table of callbacks (virtual methods) */
277 struct video_device vdt; /* Video device template */
278 struct uvd *cam; /* Array of camera structures */
279 struct module *md_module; /* Minidriver module */
280 };
281
282
283 /*
284 * This macro retrieves callback address from the struct uvd object.
285 * No validity checks are done here, so be sure to check the
286 * callback beforehand with VALID_CALLBACK.
287 */
288 #define GET_CALLBACK(uvd,cbName) ((uvd)->handle->cb.cbName)
289
290 /*
291 * This macro returns either callback pointer or NULL. This is safe
292 * macro, meaning that most of components of data structures involved
293 * may be NULL - this only results in NULL being returned. You may
294 * wish to use this macro to make sure that the callback is callable.
295 * However keep in mind that those checks take time.
296 */
297 #define VALID_CALLBACK(uvd,cbName) ((((uvd) != NULL) && \
298 ((uvd)->handle != NULL)) ? GET_CALLBACK(uvd,cbName) : NULL)
299
300 int RingQueue_Dequeue(struct RingQueue *rq, unsigned char *dst, int len);
301 int RingQueue_Enqueue(struct RingQueue *rq, const unsigned char *cdata, int n);
302 void RingQueue_WakeUpInterruptible(struct RingQueue *rq);
303 void RingQueue_Flush(struct RingQueue *rq);
304
305 static inline int RingQueue_GetLength(const struct RingQueue *rq)
306 {
307 return (rq->wi - rq->ri + rq->length) & (rq->length-1);
308 }
309
310 static inline int RingQueue_GetFreeSpace(const struct RingQueue *rq)
311 {
312 return rq->length - RingQueue_GetLength(rq);
313 }
314
315 void usbvideo_DrawLine(
316 struct usbvideo_frame *frame,
317 int x1, int y1,
318 int x2, int y2,
319 unsigned char cr, unsigned char cg, unsigned char cb);
320 void usbvideo_HexDump(const unsigned char *data, int len);
321 void usbvideo_SayAndWait(const char *what);
322 void usbvideo_TestPattern(struct uvd *uvd, int fullframe, int pmode);
323
324 /* Memory allocation routines */
325 unsigned long usbvideo_kvirt_to_pa(unsigned long adr);
326
327 int usbvideo_register(
328 struct usbvideo **pCams,
329 const int num_cams,
330 const int num_extra,
331 const char *driverName,
332 const struct usbvideo_cb *cbTable,
333 struct module *md,
334 const struct usb_device_id *id_table);
335 struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams);
336 int usbvideo_RegisterVideoDevice(struct uvd *uvd);
337 void usbvideo_Deregister(struct usbvideo **uvt);
338
339 int usbvideo_v4l_initialize(struct video_device *dev);
340
341 void usbvideo_DeinterlaceFrame(struct uvd *uvd, struct usbvideo_frame *frame);
342
343 /*
344 * This code performs bounds checking - use it when working with
345 * new formats, or else you may get oopses all over the place.
346 * If pixel falls out of bounds then it gets shoved back (as close
347 * to place of offence as possible) and is painted bright red.
348 *
349 * There are two important concepts: frame width, height and
350 * V4L canvas width, height. The former is the area requested by
351 * the application -for this very frame-. The latter is the largest
352 * possible frame that we can serve (we advertise that via V4L ioctl).
353 * The frame data is expected to be formatted as lines of length
354 * VIDEOSIZE_X(fr->request), total VIDEOSIZE_Y(frame->request) lines.
355 */
356 static inline void RGB24_PUTPIXEL(
357 struct usbvideo_frame *fr,
358 int ix, int iy,
359 unsigned char vr,
360 unsigned char vg,
361 unsigned char vb)
362 {
363 register unsigned char *pf;
364 int limiter = 0, mx, my;
365 mx = ix;
366 my = iy;
367 if (mx < 0) {
368 mx=0;
369 limiter++;
370 } else if (mx >= VIDEOSIZE_X((fr)->request)) {
371 mx= VIDEOSIZE_X((fr)->request) - 1;
372 limiter++;
373 }
374 if (my < 0) {
375 my = 0;
376 limiter++;
377 } else if (my >= VIDEOSIZE_Y((fr)->request)) {
378 my = VIDEOSIZE_Y((fr)->request) - 1;
379 limiter++;
380 }
381 pf = (fr)->data + V4L_BYTES_PER_PIXEL*((iy)*VIDEOSIZE_X((fr)->request) + (ix));
382 if (limiter) {
383 *pf++ = 0;
384 *pf++ = 0;
385 *pf++ = 0xFF;
386 } else {
387 *pf++ = (vb);
388 *pf++ = (vg);
389 *pf++ = (vr);
390 }
391 }
392
393 #endif /* usbvideo_h */