video-buf.h 8.4 KB

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