video-buf.h 8.3 KB

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