usbvideo.h 14 KB

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