video-buf.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /* for overlay buffers (pci-pci dma) */
  70. dma_addr_t bus_addr;
  71. /* common */
  72. struct scatterlist *sglist;
  73. int sglen;
  74. int nr_pages;
  75. int direction;
  76. };
  77. void videobuf_dma_init(struct videobuf_dmabuf *dma);
  78. int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
  79. unsigned long data, unsigned long size);
  80. int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
  81. int nr_pages);
  82. int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
  83. dma_addr_t addr, int nr_pages);
  84. int videobuf_dma_free(struct videobuf_dmabuf *dma);
  85. int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma);
  86. int videobuf_dma_sync(struct videobuf_queue* q,struct videobuf_dmabuf *dma);
  87. int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma);
  88. /*FIXME: these variants are used only on *-alsa code, where videobuf is
  89. * used without queue
  90. */
  91. int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma);
  92. int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma);
  93. /* --------------------------------------------------------------------- */
  94. /*
  95. * A small set of helper functions to manage video4linux buffers.
  96. *
  97. * struct videobuf_buffer holds the data structures used by the helper
  98. * functions, additionally some commonly used fields for v4l buffers
  99. * (width, height, lists, waitqueue) are in there. That struct should
  100. * be used as first element in the drivers buffer struct.
  101. *
  102. * about the mmap helpers (videobuf_mmap_*):
  103. *
  104. * The mmaper function allows to map any subset of contingous buffers.
  105. * This includes one mmap() call for all buffers (which the original
  106. * video4linux API uses) as well as one mmap() for every single buffer
  107. * (which v4l2 uses).
  108. *
  109. * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
  110. * userspace address + size which can be feeded into the
  111. * videobuf_dma_init_user function listed above.
  112. *
  113. */
  114. struct videobuf_mapping {
  115. unsigned int count;
  116. unsigned long start;
  117. unsigned long end;
  118. struct videobuf_queue *q;
  119. };
  120. enum videobuf_state {
  121. STATE_NEEDS_INIT = 0,
  122. STATE_PREPARED = 1,
  123. STATE_QUEUED = 2,
  124. STATE_ACTIVE = 3,
  125. STATE_DONE = 4,
  126. STATE_ERROR = 5,
  127. STATE_IDLE = 6,
  128. };
  129. struct videobuf_buffer {
  130. unsigned int i;
  131. u32 magic;
  132. /* info about the buffer */
  133. unsigned int width;
  134. unsigned int height;
  135. unsigned int bytesperline; /* use only if != 0 */
  136. unsigned long size;
  137. unsigned int input;
  138. enum v4l2_field field;
  139. enum videobuf_state state;
  140. struct videobuf_dmabuf dma;
  141. struct list_head stream; /* QBUF/DQBUF list */
  142. /* for mmap'ed buffers */
  143. enum v4l2_memory memory;
  144. size_t boff; /* buffer offset (mmap + overlay) */
  145. size_t bsize; /* buffer size */
  146. unsigned long baddr; /* buffer addr (userland ptr!) */
  147. struct videobuf_mapping *map;
  148. /* touched by irq handler */
  149. struct list_head queue;
  150. wait_queue_head_t done;
  151. unsigned int field_count;
  152. struct timeval ts;
  153. };
  154. typedef int (vb_map_sg_t)(void *dev,struct scatterlist *sglist,int nr_pages,
  155. int direction);
  156. struct videobuf_queue_ops {
  157. int (*buf_setup)(struct videobuf_queue *q,
  158. unsigned int *count, unsigned int *size);
  159. int (*buf_prepare)(struct videobuf_queue *q,
  160. struct videobuf_buffer *vb,
  161. enum v4l2_field field);
  162. void (*buf_queue)(struct videobuf_queue *q,
  163. struct videobuf_buffer *vb);
  164. void (*buf_release)(struct videobuf_queue *q,
  165. struct videobuf_buffer *vb);
  166. /* Helper operations - device dependent.
  167. * If null, videobuf_init defaults all to PCI handling
  168. */
  169. vb_map_sg_t *vb_map_sg;
  170. vb_map_sg_t *vb_dma_sync_sg;
  171. vb_map_sg_t *vb_unmap_sg;
  172. };
  173. struct videobuf_queue {
  174. struct mutex lock;
  175. spinlock_t *irqlock;
  176. void *dev; /* on pci, points to struct pci_dev */
  177. enum v4l2_buf_type type;
  178. unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */
  179. unsigned int msize;
  180. enum v4l2_field field;
  181. enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
  182. struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
  183. struct videobuf_queue_ops *ops;
  184. /* capture via mmap() + ioctl(QBUF/DQBUF) */
  185. unsigned int streaming;
  186. struct list_head stream;
  187. /* capture via read() */
  188. unsigned int reading;
  189. unsigned int read_off;
  190. struct videobuf_buffer *read_buf;
  191. /* driver private data */
  192. void *priv_data;
  193. };
  194. void* videobuf_alloc(unsigned int size);
  195. int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
  196. int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
  197. struct v4l2_framebuffer *fbuf);
  198. /* Maps fops to PCI stuff */
  199. void videobuf_queue_pci(struct videobuf_queue* q);
  200. void videobuf_queue_init(struct videobuf_queue *q,
  201. struct videobuf_queue_ops *ops,
  202. void *dev,
  203. spinlock_t *irqlock,
  204. enum v4l2_buf_type type,
  205. enum v4l2_field field,
  206. unsigned int msize,
  207. void *priv);
  208. int videobuf_queue_is_busy(struct videobuf_queue *q);
  209. void videobuf_queue_cancel(struct videobuf_queue *q);
  210. enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
  211. void videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
  212. enum v4l2_buf_type type);
  213. int videobuf_reqbufs(struct videobuf_queue *q,
  214. struct v4l2_requestbuffers *req);
  215. int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
  216. int videobuf_qbuf(struct videobuf_queue *q,
  217. struct v4l2_buffer *b);
  218. int videobuf_dqbuf(struct videobuf_queue *q,
  219. struct v4l2_buffer *b, int nonblocking);
  220. int videobuf_streamon(struct videobuf_queue *q);
  221. int videobuf_streamoff(struct videobuf_queue *q);
  222. int videobuf_read_start(struct videobuf_queue *q);
  223. void videobuf_read_stop(struct videobuf_queue *q);
  224. ssize_t videobuf_read_stream(struct videobuf_queue *q,
  225. char __user *data, size_t count, loff_t *ppos,
  226. int vbihack, int nonblocking);
  227. ssize_t videobuf_read_one(struct videobuf_queue *q,
  228. char __user *data, size_t count, loff_t *ppos,
  229. int nonblocking);
  230. unsigned int videobuf_poll_stream(struct file *file,
  231. struct videobuf_queue *q,
  232. poll_table *wait);
  233. int videobuf_mmap_setup(struct videobuf_queue *q,
  234. unsigned int bcount, unsigned int bsize,
  235. enum v4l2_memory memory);
  236. int videobuf_mmap_free(struct videobuf_queue *q);
  237. int videobuf_mmap_mapper(struct videobuf_queue *q,
  238. struct vm_area_struct *vma);
  239. /* --------------------------------------------------------------------- */
  240. /*
  241. * Local variables:
  242. * c-basic-offset: 8
  243. * End:
  244. */