videobuf-core.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #ifndef _VIDEOBUF_CORE_H
  16. #define _VIDEOBUF_CORE_H
  17. #include <linux/poll.h>
  18. #include <linux/videodev2.h>
  19. #define UNSET (-1U)
  20. struct videobuf_buffer;
  21. struct videobuf_queue;
  22. /* --------------------------------------------------------------------- */
  23. /*
  24. * A small set of helper functions to manage video4linux buffers.
  25. *
  26. * struct videobuf_buffer holds the data structures used by the helper
  27. * functions, additionally some commonly used fields for v4l buffers
  28. * (width, height, lists, waitqueue) are in there. That struct should
  29. * be used as first element in the drivers buffer struct.
  30. *
  31. * about the mmap helpers (videobuf_mmap_*):
  32. *
  33. * The mmaper function allows to map any subset of contingous buffers.
  34. * This includes one mmap() call for all buffers (which the original
  35. * video4linux API uses) as well as one mmap() for every single buffer
  36. * (which v4l2 uses).
  37. *
  38. * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
  39. * userspace address + size which can be feeded into the
  40. * videobuf_dma_init_user function listed above.
  41. *
  42. */
  43. struct videobuf_mapping {
  44. unsigned int count;
  45. struct videobuf_queue *q;
  46. };
  47. enum videobuf_state {
  48. VIDEOBUF_NEEDS_INIT = 0,
  49. VIDEOBUF_PREPARED = 1,
  50. VIDEOBUF_QUEUED = 2,
  51. VIDEOBUF_ACTIVE = 3,
  52. VIDEOBUF_DONE = 4,
  53. VIDEOBUF_ERROR = 5,
  54. VIDEOBUF_IDLE = 6,
  55. };
  56. struct videobuf_buffer {
  57. unsigned int i;
  58. u32 magic;
  59. /* info about the buffer */
  60. unsigned int width;
  61. unsigned int height;
  62. unsigned int bytesperline; /* use only if != 0 */
  63. unsigned long size;
  64. unsigned int input;
  65. enum v4l2_field field;
  66. enum videobuf_state state;
  67. struct list_head stream; /* QBUF/DQBUF list */
  68. /* touched by irq handler */
  69. struct list_head queue;
  70. wait_queue_head_t done;
  71. unsigned int field_count;
  72. struct timeval ts;
  73. /* Memory type */
  74. enum v4l2_memory memory;
  75. /* buffer size */
  76. size_t bsize;
  77. /* buffer offset (mmap + overlay) */
  78. size_t boff;
  79. /* buffer addr (userland ptr!) */
  80. unsigned long baddr;
  81. /* for mmap'ed buffers */
  82. struct videobuf_mapping *map;
  83. /* Private pointer to allow specific methods to store their data */
  84. int privsize;
  85. void *priv;
  86. };
  87. struct videobuf_queue_ops {
  88. int (*buf_setup)(struct videobuf_queue *q,
  89. unsigned int *count, unsigned int *size);
  90. int (*buf_prepare)(struct videobuf_queue *q,
  91. struct videobuf_buffer *vb,
  92. enum v4l2_field field);
  93. void (*buf_queue)(struct videobuf_queue *q,
  94. struct videobuf_buffer *vb);
  95. void (*buf_release)(struct videobuf_queue *q,
  96. struct videobuf_buffer *vb);
  97. };
  98. #define MAGIC_QTYPE_OPS 0x12261003
  99. /* Helper operations - device type dependent */
  100. struct videobuf_qtype_ops {
  101. u32 magic;
  102. struct videobuf_buffer *(*alloc_vb)(size_t size);
  103. void *(*vaddr) (struct videobuf_buffer *buf);
  104. int (*iolock) (struct videobuf_queue *q,
  105. struct videobuf_buffer *vb,
  106. struct v4l2_framebuffer *fbuf);
  107. int (*sync) (struct videobuf_queue *q,
  108. struct videobuf_buffer *buf);
  109. int (*mmap_mapper) (struct videobuf_queue *q,
  110. struct videobuf_buffer *buf,
  111. struct vm_area_struct *vma);
  112. };
  113. struct videobuf_queue {
  114. struct mutex vb_lock;
  115. struct mutex *ext_lock;
  116. spinlock_t *irqlock;
  117. struct device *dev;
  118. wait_queue_head_t wait; /* wait if queue is empty */
  119. enum v4l2_buf_type type;
  120. unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */
  121. unsigned int msize;
  122. enum v4l2_field field;
  123. enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
  124. struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
  125. const struct videobuf_queue_ops *ops;
  126. struct videobuf_qtype_ops *int_ops;
  127. unsigned int streaming:1;
  128. unsigned int reading:1;
  129. /* capture via mmap() + ioctl(QBUF/DQBUF) */
  130. struct list_head stream;
  131. /* capture via read() */
  132. unsigned int read_off;
  133. struct videobuf_buffer *read_buf;
  134. /* driver private data */
  135. void *priv_data;
  136. };
  137. static inline void videobuf_queue_lock(struct videobuf_queue *q)
  138. {
  139. if (!q->ext_lock)
  140. mutex_lock(&q->vb_lock);
  141. }
  142. static inline void videobuf_queue_unlock(struct videobuf_queue *q)
  143. {
  144. if (!q->ext_lock)
  145. mutex_unlock(&q->vb_lock);
  146. }
  147. int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb,
  148. int non_blocking, int intr);
  149. int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
  150. struct v4l2_framebuffer *fbuf);
  151. struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q);
  152. /* Used on videobuf-dvb */
  153. void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
  154. struct videobuf_buffer *buf);
  155. void videobuf_queue_core_init(struct videobuf_queue *q,
  156. const struct videobuf_queue_ops *ops,
  157. struct device *dev,
  158. spinlock_t *irqlock,
  159. enum v4l2_buf_type type,
  160. enum v4l2_field field,
  161. unsigned int msize,
  162. void *priv,
  163. struct videobuf_qtype_ops *int_ops,
  164. struct mutex *ext_lock);
  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. int videobuf_streamon(struct videobuf_queue *q);
  176. int videobuf_streamoff(struct videobuf_queue *q);
  177. void videobuf_stop(struct videobuf_queue *q);
  178. int videobuf_read_start(struct videobuf_queue *q);
  179. void videobuf_read_stop(struct videobuf_queue *q);
  180. ssize_t videobuf_read_stream(struct videobuf_queue *q,
  181. char __user *data, size_t count, loff_t *ppos,
  182. int vbihack, int nonblocking);
  183. ssize_t videobuf_read_one(struct videobuf_queue *q,
  184. char __user *data, size_t count, loff_t *ppos,
  185. int nonblocking);
  186. unsigned int videobuf_poll_stream(struct file *file,
  187. struct videobuf_queue *q,
  188. poll_table *wait);
  189. int videobuf_mmap_setup(struct videobuf_queue *q,
  190. unsigned int bcount, unsigned int bsize,
  191. enum v4l2_memory memory);
  192. int __videobuf_mmap_setup(struct videobuf_queue *q,
  193. unsigned int bcount, unsigned int bsize,
  194. enum v4l2_memory memory);
  195. int videobuf_mmap_free(struct videobuf_queue *q);
  196. int videobuf_mmap_mapper(struct videobuf_queue *q,
  197. struct vm_area_struct *vma);
  198. #endif