videobuf-core.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * generic helper functions for handling video4linux capture buffers
  3. *
  4. * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
  5. *
  6. * Highly based on video-buf written originally by:
  7. * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
  8. * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
  9. * (c) 2006 Ted Walther and John Sokol
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2
  14. */
  15. #include <linux/poll.h>
  16. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  17. #include <linux/videodev.h>
  18. #endif
  19. #include <linux/videodev2.h>
  20. #define UNSET (-1U)
  21. struct videobuf_buffer;
  22. struct videobuf_queue;
  23. /* --------------------------------------------------------------------- */
  24. /*
  25. * A small set of helper functions to manage video4linux buffers.
  26. *
  27. * struct videobuf_buffer holds the data structures used by the helper
  28. * functions, additionally some commonly used fields for v4l buffers
  29. * (width, height, lists, waitqueue) are in there. That struct should
  30. * be used as first element in the drivers buffer struct.
  31. *
  32. * about the mmap helpers (videobuf_mmap_*):
  33. *
  34. * The mmaper function allows to map any subset of contingous buffers.
  35. * This includes one mmap() call for all buffers (which the original
  36. * video4linux API uses) as well as one mmap() for every single buffer
  37. * (which v4l2 uses).
  38. *
  39. * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
  40. * userspace address + size which can be feeded into the
  41. * videobuf_dma_init_user function listed above.
  42. *
  43. */
  44. struct videobuf_mapping {
  45. unsigned int count;
  46. unsigned long start;
  47. unsigned long end;
  48. struct videobuf_queue *q;
  49. };
  50. enum videobuf_state {
  51. VIDEOBUF_NEEDS_INIT = 0,
  52. VIDEOBUF_PREPARED = 1,
  53. VIDEOBUF_QUEUED = 2,
  54. VIDEOBUF_ACTIVE = 3,
  55. VIDEOBUF_DONE = 4,
  56. VIDEOBUF_ERROR = 5,
  57. VIDEOBUF_IDLE = 6,
  58. };
  59. struct videobuf_buffer {
  60. unsigned int i;
  61. u32 magic;
  62. /* info about the buffer */
  63. unsigned int width;
  64. unsigned int height;
  65. unsigned int bytesperline; /* use only if != 0 */
  66. unsigned long size;
  67. unsigned int input;
  68. enum v4l2_field field;
  69. enum videobuf_state state;
  70. struct list_head stream; /* QBUF/DQBUF list */
  71. /* touched by irq handler */
  72. struct list_head queue;
  73. wait_queue_head_t done;
  74. unsigned int field_count;
  75. struct timeval ts;
  76. /* Memory type */
  77. enum v4l2_memory memory;
  78. /* buffer size */
  79. size_t bsize;
  80. /* buffer offset (mmap + overlay) */
  81. size_t boff;
  82. /* buffer addr (userland ptr!) */
  83. unsigned long baddr;
  84. /* for mmap'ed buffers */
  85. struct videobuf_mapping *map;
  86. /* Private pointer to allow specific methods to store their data */
  87. int privsize;
  88. void *priv;
  89. };
  90. struct videobuf_queue_ops {
  91. int (*buf_setup)(struct videobuf_queue *q,
  92. unsigned int *count, unsigned int *size);
  93. int (*buf_prepare)(struct videobuf_queue *q,
  94. struct videobuf_buffer *vb,
  95. enum v4l2_field field);
  96. void (*buf_queue)(struct videobuf_queue *q,
  97. struct videobuf_buffer *vb);
  98. void (*buf_release)(struct videobuf_queue *q,
  99. struct videobuf_buffer *vb);
  100. };
  101. #define MAGIC_QTYPE_OPS 0x12261003
  102. /* Helper operations - device type dependent */
  103. struct videobuf_qtype_ops {
  104. u32 magic;
  105. void* (*alloc) (size_t size);
  106. int (*iolock) (struct videobuf_queue* q,
  107. struct videobuf_buffer *vb,
  108. struct v4l2_framebuffer *fbuf);
  109. int (*mmap) (struct videobuf_queue *q,
  110. unsigned int *count,
  111. unsigned int *size,
  112. enum v4l2_memory memory);
  113. int (*sync) (struct videobuf_queue* q,
  114. struct videobuf_buffer *buf);
  115. int (*video_copy_to_user)(struct videobuf_queue *q,
  116. char __user *data,
  117. size_t count,
  118. int nonblocking);
  119. int (*copy_stream) (struct videobuf_queue *q,
  120. char __user *data,
  121. size_t count,
  122. size_t pos,
  123. int vbihack,
  124. int nonblocking);
  125. int (*mmap_free) (struct videobuf_queue *q);
  126. int (*mmap_mapper) (struct videobuf_queue *q,
  127. struct vm_area_struct *vma);
  128. };
  129. struct videobuf_queue {
  130. struct mutex lock;
  131. spinlock_t *irqlock;
  132. void *dev; /* on pci, points to struct pci_dev */
  133. enum v4l2_buf_type type;
  134. unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */
  135. unsigned int msize;
  136. enum v4l2_field field;
  137. enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
  138. struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
  139. struct videobuf_queue_ops *ops;
  140. struct videobuf_qtype_ops *int_ops;
  141. unsigned int streaming:1;
  142. unsigned int reading:1;
  143. unsigned int is_mmapped:1;
  144. /* capture via mmap() + ioctl(QBUF/DQBUF) */
  145. struct list_head stream;
  146. /* capture via read() */
  147. unsigned int read_off;
  148. struct videobuf_buffer *read_buf;
  149. /* driver private data */
  150. void *priv_data;
  151. };
  152. int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
  153. int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
  154. struct v4l2_framebuffer *fbuf);
  155. void *videobuf_alloc(struct videobuf_queue* q);
  156. void videobuf_queue_core_init(struct videobuf_queue *q,
  157. struct videobuf_queue_ops *ops,
  158. void *dev,
  159. spinlock_t *irqlock,
  160. enum v4l2_buf_type type,
  161. enum v4l2_field field,
  162. unsigned int msize,
  163. void *priv,
  164. struct videobuf_qtype_ops *int_ops);
  165. int videobuf_queue_is_busy(struct videobuf_queue *q);
  166. void videobuf_queue_cancel(struct videobuf_queue *q);
  167. enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
  168. int videobuf_reqbufs(struct videobuf_queue *q,
  169. struct v4l2_requestbuffers *req);
  170. int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
  171. int videobuf_qbuf(struct videobuf_queue *q,
  172. struct v4l2_buffer *b);
  173. int videobuf_dqbuf(struct videobuf_queue *q,
  174. struct v4l2_buffer *b, int nonblocking);
  175. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  176. int videobuf_cgmbuf(struct videobuf_queue *q,
  177. struct video_mbuf *mbuf, int count);
  178. #endif
  179. int videobuf_streamon(struct videobuf_queue *q);
  180. int videobuf_streamoff(struct videobuf_queue *q);
  181. void videobuf_stop(struct videobuf_queue *q);
  182. int videobuf_read_start(struct videobuf_queue *q);
  183. void videobuf_read_stop(struct videobuf_queue *q);
  184. ssize_t videobuf_read_stream(struct videobuf_queue *q,
  185. char __user *data, size_t count, loff_t *ppos,
  186. int vbihack, int nonblocking);
  187. ssize_t videobuf_read_one(struct videobuf_queue *q,
  188. char __user *data, size_t count, loff_t *ppos,
  189. int nonblocking);
  190. unsigned int videobuf_poll_stream(struct file *file,
  191. struct videobuf_queue *q,
  192. poll_table *wait);
  193. int videobuf_mmap_setup(struct videobuf_queue *q,
  194. unsigned int bcount, unsigned int bsize,
  195. enum v4l2_memory memory);
  196. int videobuf_mmap_free(struct videobuf_queue *q);
  197. int videobuf_mmap_mapper(struct videobuf_queue *q,
  198. struct vm_area_struct *vma);
  199. /* --------------------------------------------------------------------- */
  200. /*
  201. * Local variables:
  202. * c-basic-offset: 8
  203. * End:
  204. */