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