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