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/proc_fs.h> 21#include <linux/videodev.h> 22#include <linux/usb.h> 23 24/* Most helpful debugging aid */ 25#define assert(expr) ((void) ((expr) ? 0 : (err("assert failed at line %d",__LINE__)))) 26 27#define USES_PROC_FS (defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)) 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) 69typedef 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 121struct 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 129enum ScanState { 130 ScanState_Scanning, /* Scanning for header */ 131 ScanState_Lines /* Parsing lines */ 132}; 133 134/* Completion states of the data parser */ 135enum 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 142enum 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 */ 155enum 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 */ 165struct usbvideo_sbuf { 166 char *data; 167 struct urb *urb; 168}; 169 170struct 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 */ 192struct 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 202struct usbvideo; 203 204struct 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 semaphore 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 struct proc_dir_entry *procfs_vEntry; /* /proc/video/MYDRIVER/video2 */ 248 char videoName[32]; /* Holds name like "video7" */ 249}; 250 251/* 252 * usbvideo callbacks (virtual methods). They are set when usbvideo 253 * services are registered. All of these default to NULL, except those 254 * that default to usbvideo-provided methods. 255 */ 256struct usbvideo_cb { 257 void *(*probe)(struct usb_device *, unsigned int,const struct usb_device_id *); 258 void (*userFree)(struct uvd *); 259 void (*disconnect)(struct usb_device *, void *); 260 int (*setupOnOpen)(struct uvd *); 261 void (*videoStart)(struct uvd *); 262 void (*videoStop)(struct uvd *); 263 void (*processData)(struct uvd *, struct usbvideo_frame *); 264 void (*postProcess)(struct uvd *, struct usbvideo_frame *); 265 void (*adjustPicture)(struct uvd *); 266 int (*getFPS)(struct uvd *); 267 int (*overlayHook)(struct uvd *, struct usbvideo_frame *); 268 int (*getFrame)(struct uvd *, int); 269 int (*procfs_read)(char *page,char **start,off_t off,int count,int *eof,void *data); 270 int (*procfs_write)(struct file *file,const char *buffer,unsigned long count,void *data); 271 int (*startDataPump)(struct uvd *uvd); 272 void (*stopDataPump)(struct uvd *uvd); 273 int (*setVideoMode)(struct uvd *uvd, struct video_window *vw); 274}; 275 276struct usbvideo { 277 int num_cameras; /* As allocated */ 278 struct usb_driver usbdrv; /* Interface to the USB stack */ 279 char drvName[80]; /* Driver name */ 280 struct semaphore lock; /* Mutex protecting camera structures */ 281 struct usbvideo_cb cb; /* Table of callbacks (virtual methods) */ 282 struct video_device vdt; /* Video device template */ 283 struct uvd *cam; /* Array of camera structures */ 284 int uses_procfs; /* Non-zero if we create /proc entries */ 285 struct proc_dir_entry *procfs_dEntry; /* /proc/video/MYDRIVER */ 286 struct module *md_module; /* Minidriver module */ 287}; 288 289 290/* 291 * This macro retrieves callback address from the struct uvd object. 292 * No validity checks are done here, so be sure to check the 293 * callback beforehand with VALID_CALLBACK. 294 */ 295#define GET_CALLBACK(uvd,cbName) ((uvd)->handle->cb.cbName) 296 297/* 298 * This macro returns either callback pointer or NULL. This is safe 299 * macro, meaning that most of components of data structures involved 300 * may be NULL - this only results in NULL being returned. You may 301 * wish to use this macro to make sure that the callback is callable. 302 * However keep in mind that those checks take time. 303 */ 304#define VALID_CALLBACK(uvd,cbName) ((((uvd) != NULL) && \ 305 ((uvd)->handle != NULL)) ? GET_CALLBACK(uvd,cbName) : NULL) 306 307int RingQueue_Dequeue(struct RingQueue *rq, unsigned char *dst, int len); 308int RingQueue_Enqueue(struct RingQueue *rq, const unsigned char *cdata, int n); 309void RingQueue_WakeUpInterruptible(struct RingQueue *rq); 310void RingQueue_Flush(struct RingQueue *rq); 311 312static inline int RingQueue_GetLength(const struct RingQueue *rq) 313{ 314 return (rq->wi - rq->ri + rq->length) & (rq->length-1); 315} 316 317static inline int RingQueue_GetFreeSpace(const struct RingQueue *rq) 318{ 319 return rq->length - RingQueue_GetLength(rq); 320} 321 322void usbvideo_CollectRawData(struct uvd *uvd, struct usbvideo_frame *frame); 323void usbvideo_DrawLine( 324 struct usbvideo_frame *frame, 325 int x1, int y1, 326 int x2, int y2, 327 unsigned char cr, unsigned char cg, unsigned char cb); 328void usbvideo_HexDump(const unsigned char *data, int len); 329void usbvideo_OverlayChar(struct uvd *uvd, struct usbvideo_frame *frame, int x, int y, int ch); 330void usbvideo_OverlayString(struct uvd *uvd, struct usbvideo_frame *frame, int x, int y, const char *str); 331void usbvideo_OverlayStats(struct uvd *uvd, struct usbvideo_frame *frame); 332void usbvideo_ReportStatistics(const struct uvd *uvd); 333void usbvideo_SayAndWait(const char *what); 334void usbvideo_TestPattern(struct uvd *uvd, int fullframe, int pmode); 335void usbvideo_VideosizeToString(char *buf, int bufLen, videosize_t vs); 336 337/* Memory allocation routines */ 338unsigned long usbvideo_uvirt_to_kva(pgd_t *pgd, unsigned long adr); 339unsigned long usbvideo_kvirt_to_pa(unsigned long adr); 340void *usbvideo_rvmalloc(unsigned long size); 341void usbvideo_rvfree(void *mem, unsigned long size); 342 343int usbvideo_register( 344 struct usbvideo **pCams, 345 const int num_cams, 346 const int num_extra, 347 const char *driverName, 348 const struct usbvideo_cb *cbTable, 349 struct module *md, 350 const struct usb_device_id *id_table); 351struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams); 352int usbvideo_RegisterVideoDevice(struct uvd *uvd); 353void usbvideo_Deregister(struct usbvideo **uvt); 354void usbvideo_Disconnect(struct usb_device *dev, void *ptr); 355void usbvideo_CameraRelease(struct uvd *uvd); 356 357void usbvideo_v4l_close(struct video_device *dev); 358int usbvideo_v4l_initialize(struct video_device *dev); 359int usbvideo_v4l_ioctl(struct video_device *dev, unsigned int cmd, void *arg); 360int usbvideo_v4l_mmap(struct video_device *dev, const char *adr, unsigned long size); 361int usbvideo_v4l_open(struct video_device *dev, int flags); 362long usbvideo_v4l_read(struct video_device *dev, char *buf, 363 unsigned long count, int noblock); 364long usbvideo_v4l_write(struct video_device *dev, const char *buf, 365 unsigned long count, int noblock); 366 367int usbvideo_GetFrame(struct uvd *uvd, int frameNum); 368int usbvideo_NewFrame(struct uvd *uvd, int framenum); 369int usbvideo_StartDataPump(struct uvd *uvd); 370void usbvideo_StopDataPump(struct uvd *uvd); 371void usbvideo_DeinterlaceFrame(struct uvd *uvd, struct usbvideo_frame *frame); 372void usbvideo_SoftwareContrastAdjustment(struct uvd *uvd, struct usbvideo_frame *frame); 373 374/* 375 * This code performs bounds checking - use it when working with 376 * new formats, or else you may get oopses all over the place. 377 * If pixel falls out of bounds then it gets shoved back (as close 378 * to place of offence as possible) and is painted bright red. 379 * 380 * There are two important concepts: frame width, height and 381 * V4L canvas width, height. The former is the area requested by 382 * the application -for this very frame-. The latter is the largest 383 * possible frame that we can serve (we advertise that via V4L ioctl). 384 * The frame data is expected to be formatted as lines of length 385 * VIDEOSIZE_X(fr->request), total VIDEOSIZE_Y(frame->request) lines. 386 */ 387static inline void RGB24_PUTPIXEL( 388 struct usbvideo_frame *fr, 389 int ix, int iy, 390 unsigned char vr, 391 unsigned char vg, 392 unsigned char vb) 393{ 394 register unsigned char *pf; 395 int limiter = 0, mx, my; 396 mx = ix; 397 my = iy; 398 if (mx < 0) { 399 mx=0; 400 limiter++; 401 } else if (mx >= VIDEOSIZE_X((fr)->request)) { 402 mx= VIDEOSIZE_X((fr)->request) - 1; 403 limiter++; 404 } 405 if (my < 0) { 406 my = 0; 407 limiter++; 408 } else if (my >= VIDEOSIZE_Y((fr)->request)) { 409 my = VIDEOSIZE_Y((fr)->request) - 1; 410 limiter++; 411 } 412 pf = (fr)->data + V4L_BYTES_PER_PIXEL*((iy)*VIDEOSIZE_X((fr)->request) + (ix)); 413 if (limiter) { 414 *pf++ = 0; 415 *pf++ = 0; 416 *pf++ = 0xFF; 417 } else { 418 *pf++ = (vb); 419 *pf++ = (vg); 420 *pf++ = (vr); 421 } 422} 423 424#endif /* usbvideo_h */ 425