video-buf.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. *
  3. * generic helper functions for video4linux capture buffers, to handle
  4. * memory management and PCI DMA.
  5. * Right now, bttv, saa7134, saa7146 and cx88 use it.
  6. *
  7. * The functions expect the hardware being able to scatter gatter
  8. * (i.e. the buffers are not linear in physical memory, but fragmented
  9. * into PAGE_SIZE chunks). They also assume the driver does not need
  10. * to touch the video data.
  11. *
  12. * device specific map/unmap/sync stuff now are mapped as file operations
  13. * to allow its usage by USB and virtual devices.
  14. *
  15. * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
  16. * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
  17. * (c) 2006 Ted Walther and John Sokol
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. */
  24. #include <linux/videodev2.h>
  25. #include <linux/poll.h>
  26. #define UNSET (-1U)
  27. /* --------------------------------------------------------------------- */
  28. /*
  29. * Return a scatterlist for some page-aligned vmalloc()'ed memory
  30. * block (NULL on errors). Memory for the scatterlist is allocated
  31. * using kmalloc. The caller must free the memory.
  32. */
  33. struct scatterlist* videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages);
  34. /*
  35. * Return a scatterlist for a an array of userpages (NULL on errors).
  36. * Memory for the scatterlist is allocated using kmalloc. The caller
  37. * must free the memory.
  38. */
  39. struct scatterlist* videobuf_pages_to_sg(struct page **pages, int nr_pages,
  40. int offset);
  41. struct videobuf_buffer;
  42. struct videobuf_queue;
  43. /* --------------------------------------------------------------------- */
  44. /*
  45. * A small set of helper functions to manage buffers (both userland
  46. * and kernel) for DMA.
  47. *
  48. * videobuf_dma_init_*()
  49. * creates a buffer. The userland version takes a userspace
  50. * pointer + length. The kernel version just wants the size and
  51. * does memory allocation too using vmalloc_32().
  52. *
  53. * videobuf_dma_*()
  54. * see Documentation/DMA-mapping.txt, these functions to
  55. * basically the same. The map function does also build a
  56. * scatterlist for the buffer (and unmap frees it ...)
  57. *
  58. * videobuf_dma_free()
  59. * no comment ...
  60. *
  61. */
  62. struct videobuf_dmabuf {
  63. u32 magic;
  64. /* for userland buffer */
  65. int offset;
  66. struct page **pages;
  67. /* for kernel buffers */
  68. void *vmalloc;
  69. /* Stores the userspace pointer to vmalloc area */
  70. void *varea;
  71. /* for overlay buffers (pci-pci dma) */
  72. dma_addr_t bus_addr;
  73. /* common */
  74. struct scatterlist *sglist;
  75. int sglen;
  76. int nr_pages;
  77. int direction;
  78. };
  79. void videobuf_dma_init(struct videobuf_dmabuf *dma);
  80. int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
  81. unsigned long data, unsigned long size);
  82. int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
  83. int nr_pages);
  84. int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
  85. dma_addr_t addr, int nr_pages);
  86. int videobuf_dma_free(struct videobuf_dmabuf *dma);
  87. int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma);
  88. int videobuf_dma_sync(struct videobuf_queue* q,struct videobuf_dmabuf *dma);
  89. int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma);
  90. /*FIXME: these variants are used only on *-alsa code, where videobuf is
  91. * used without queue
  92. */
  93. int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma);
  94. int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma);
  95. /* --------------------------------------------------------------------- */
  96. /*
  97. * A small set of helper functions to manage video4linux buffers.
  98. *
  99. * struct videobuf_buffer holds the data structures used by the helper
  100. * functions, additionally some commonly used fields for v4l buffers
  101. * (width, height, lists, waitqueue) are in there. That struct should
  102. * be used as first element in the drivers buffer struct.
  103. *
  104. * about the mmap helpers (videobuf_mmap_*):
  105. *
  106. * The mmaper function allows to map any subset of contingous buffers.
  107. * This includes one mmap() call for all buffers (which the original
  108. * video4linux API uses) as well as one mmap() for every single buffer
  109. * (which v4l2 uses).
  110. *
  111. * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
  112. * userspace address + size which can be feeded into the
  113. * videobuf_dma_init_user function listed above.
  114. *
  115. */
  116. struct videobuf_mapping {
  117. unsigned int count;
  118. unsigned long start;
  119. unsigned long end;
  120. struct videobuf_queue *q;
  121. };
  122. enum videobuf_state {
  123. STATE_NEEDS_INIT = 0,
  124. STATE_PREPARED = 1,
  125. STATE_QUEUED = 2,
  126. STATE_ACTIVE = 3,
  127. STATE_DONE = 4,
  128. STATE_ERROR = 5,
  129. STATE_IDLE = 6,
  130. };
  131. struct videobuf_buffer {
  132. unsigned int i;
  133. u32 magic;
  134. /* info about the buffer */
  135. unsigned int width;
  136. unsigned int height;
  137. unsigned int bytesperline; /* use only if != 0 */
  138. unsigned long size;
  139. unsigned int input;
  140. enum v4l2_field field;
  141. enum videobuf_state state;
  142. struct videobuf_dmabuf dma;
  143. struct list_head stream; /* QBUF/DQBUF list */
  144. /* for mmap'ed buffers */
  145. enum v4l2_memory memory;
  146. size_t boff; /* buffer offset (mmap + overlay) */
  147. size_t bsize; /* buffer size */
  148. unsigned long baddr; /* buffer addr (userland ptr!) */
  149. struct videobuf_mapping *map;
  150. /* touched by irq handler */
  151. struct list_head queue;
  152. wait_queue_head_t done;
  153. unsigned int field_count;
  154. struct timeval ts;
  155. };
  156. typedef int (vb_map_sg_t)(void *dev,struct scatterlist *sglist,int nr_pages,
  157. int direction);
  158. struct videobuf_queue_ops {
  159. int (*buf_setup)(struct videobuf_queue *q,
  160. unsigned int *count, unsigned int *size);
  161. int (*buf_prepare)(struct videobuf_queue *q,
  162. struct videobuf_buffer *vb,
  163. enum v4l2_field field);
  164. void (*buf_queue)(struct videobuf_queue *q,
  165. struct videobuf_buffer *vb);
  166. void (*buf_release)(struct videobuf_queue *q,
  167. struct videobuf_buffer *vb);
  168. /* Helper operations - device dependent.
  169. * If null, videobuf_init defaults all to PCI handling
  170. */
  171. vb_map_sg_t *vb_map_sg;
  172. vb_map_sg_t *vb_dma_sync_sg;
  173. vb_map_sg_t *vb_unmap_sg;
  174. };
  175. struct videobuf_queue {
  176. struct mutex lock;
  177. spinlock_t *irqlock;
  178. void *dev; /* on pci, points to struct pci_dev */
  179. enum v4l2_buf_type type;
  180. unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */
  181. unsigned int msize;
  182. enum v4l2_field field;
  183. enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
  184. struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
  185. struct videobuf_queue_ops *ops;
  186. /* capture via mmap() + ioctl(QBUF/DQBUF) */
  187. unsigned int streaming;
  188. struct list_head stream;
  189. /* capture via read() */
  190. unsigned int reading;
  191. unsigned int read_off;
  192. struct videobuf_buffer *read_buf;
  193. /* driver private data */
  194. void *priv_data;
  195. };
  196. void* videobuf_alloc(unsigned int size);
  197. int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
  198. int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
  199. struct v4l2_framebuffer *fbuf);
  200. /* Maps fops to PCI stuff */
  201. void videobuf_queue_pci(struct videobuf_queue* q);
  202. void videobuf_queue_init(struct videobuf_queue *q,
  203. struct videobuf_queue_ops *ops,
  204. void *dev,
  205. spinlock_t *irqlock,
  206. enum v4l2_buf_type type,
  207. enum v4l2_field field,
  208. unsigned int msize,
  209. void *priv);
  210. int videobuf_queue_is_busy(struct videobuf_queue *q);
  211. void videobuf_queue_cancel(struct videobuf_queue *q);
  212. enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
  213. void videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
  214. enum v4l2_buf_type type);
  215. int videobuf_reqbufs(struct videobuf_queue *q,
  216. struct v4l2_requestbuffers *req);
  217. int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
  218. int videobuf_qbuf(struct videobuf_queue *q,
  219. struct v4l2_buffer *b);
  220. int videobuf_dqbuf(struct videobuf_queue *q,
  221. struct v4l2_buffer *b, int nonblocking);
  222. int videobuf_streamon(struct videobuf_queue *q);
  223. int videobuf_streamoff(struct videobuf_queue *q);
  224. int videobuf_read_start(struct videobuf_queue *q);
  225. void videobuf_read_stop(struct videobuf_queue *q);
  226. ssize_t videobuf_read_stream(struct videobuf_queue *q,
  227. char __user *data, size_t count, loff_t *ppos,
  228. int vbihack, int nonblocking);
  229. ssize_t videobuf_read_one(struct videobuf_queue *q,
  230. char __user *data, size_t count, loff_t *ppos,
  231. int nonblocking);
  232. unsigned int videobuf_poll_stream(struct file *file,
  233. struct videobuf_queue *q,
  234. poll_table *wait);
  235. int videobuf_mmap_setup(struct videobuf_queue *q,
  236. unsigned int bcount, unsigned int bsize,
  237. enum v4l2_memory memory);
  238. int videobuf_mmap_free(struct videobuf_queue *q);
  239. int videobuf_mmap_mapper(struct videobuf_queue *q,
  240. struct vm_area_struct *vma);
  241. /* --------------------------------------------------------------------- */
  242. /*
  243. * Local variables:
  244. * c-basic-offset: 8
  245. * End:
  246. */