videobuf-core.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * generic helper functions for handling video4linux capture buffers
  3. *
  4. * (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.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@kernel.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 contiguous 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. enum v4l2_field field;
  65. enum videobuf_state state;
  66. struct list_head stream; /* QBUF/DQBUF list */
  67. /* touched by irq handler */
  68. struct list_head queue;
  69. wait_queue_head_t done;
  70. unsigned int field_count;
  71. struct timeval ts;
  72. /* Memory type */
  73. enum v4l2_memory memory;
  74. /* buffer size */
  75. size_t bsize;
  76. /* buffer offset (mmap + overlay) */
  77. size_t boff;
  78. /* buffer addr (userland ptr!) */
  79. unsigned long baddr;
  80. /* for mmap'ed buffers */
  81. struct videobuf_mapping *map;
  82. /* Private pointer to allow specific methods to store their data */
  83. int privsize;
  84. void *priv;
  85. };
  86. struct videobuf_queue_ops {
  87. int (*buf_setup)(struct videobuf_queue *q,
  88. unsigned int *count, unsigned int *size);
  89. int (*buf_prepare)(struct videobuf_queue *q,
  90. struct videobuf_buffer *vb,
  91. enum v4l2_field field);
  92. void (*buf_queue)(struct videobuf_queue *q,
  93. struct videobuf_buffer *vb);
  94. void (*buf_release)(struct videobuf_queue *q,
  95. struct videobuf_buffer *vb);
  96. };
  97. #define MAGIC_QTYPE_OPS 0x12261003
  98. /* Helper operations - device type dependent */
  99. struct videobuf_qtype_ops {
  100. u32 magic;
  101. struct videobuf_buffer *(*alloc_vb)(size_t size);
  102. void *(*vaddr) (struct videobuf_buffer *buf);
  103. int (*iolock) (struct videobuf_queue *q,
  104. struct videobuf_buffer *vb,
  105. struct v4l2_framebuffer *fbuf);
  106. int (*sync) (struct videobuf_queue *q,
  107. struct videobuf_buffer *buf);
  108. int (*mmap_mapper) (struct videobuf_queue *q,
  109. struct videobuf_buffer *buf,
  110. struct vm_area_struct *vma);
  111. };
  112. struct videobuf_queue {
  113. struct mutex vb_lock;
  114. struct mutex *ext_lock;
  115. spinlock_t *irqlock;
  116. struct device *dev;
  117. wait_queue_head_t wait; /* wait if queue is empty */
  118. enum v4l2_buf_type type;
  119. unsigned int msize;
  120. enum v4l2_field field;
  121. enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
  122. struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
  123. const struct videobuf_queue_ops *ops;
  124. struct videobuf_qtype_ops *int_ops;
  125. unsigned int streaming:1;
  126. unsigned int reading:1;
  127. /* capture via mmap() + ioctl(QBUF/DQBUF) */
  128. struct list_head stream;
  129. /* capture via read() */
  130. unsigned int read_off;
  131. struct videobuf_buffer *read_buf;
  132. /* driver private data */
  133. void *priv_data;
  134. };
  135. static inline void videobuf_queue_lock(struct videobuf_queue *q)
  136. {
  137. if (!q->ext_lock)
  138. mutex_lock(&q->vb_lock);
  139. }
  140. static inline void videobuf_queue_unlock(struct videobuf_queue *q)
  141. {
  142. if (!q->ext_lock)
  143. mutex_unlock(&q->vb_lock);
  144. }
  145. int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb,
  146. int non_blocking, int intr);
  147. int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
  148. struct v4l2_framebuffer *fbuf);
  149. struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q);
  150. /* Used on videobuf-dvb */
  151. void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
  152. struct videobuf_buffer *buf);
  153. void videobuf_queue_core_init(struct videobuf_queue *q,
  154. const struct videobuf_queue_ops *ops,
  155. struct device *dev,
  156. spinlock_t *irqlock,
  157. enum v4l2_buf_type type,
  158. enum v4l2_field field,
  159. unsigned int msize,
  160. void *priv,
  161. struct videobuf_qtype_ops *int_ops,
  162. struct mutex *ext_lock);
  163. int videobuf_queue_is_busy(struct videobuf_queue *q);
  164. void videobuf_queue_cancel(struct videobuf_queue *q);
  165. enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
  166. int videobuf_reqbufs(struct videobuf_queue *q,
  167. struct v4l2_requestbuffers *req);
  168. int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
  169. int videobuf_qbuf(struct videobuf_queue *q,
  170. struct v4l2_buffer *b);
  171. int videobuf_dqbuf(struct videobuf_queue *q,
  172. struct v4l2_buffer *b, int nonblocking);
  173. int videobuf_streamon(struct videobuf_queue *q);
  174. int videobuf_streamoff(struct videobuf_queue *q);
  175. void videobuf_stop(struct videobuf_queue *q);
  176. int videobuf_read_start(struct videobuf_queue *q);
  177. void videobuf_read_stop(struct videobuf_queue *q);
  178. ssize_t videobuf_read_stream(struct videobuf_queue *q,
  179. char __user *data, size_t count, loff_t *ppos,
  180. int vbihack, int nonblocking);
  181. ssize_t videobuf_read_one(struct videobuf_queue *q,
  182. char __user *data, size_t count, loff_t *ppos,
  183. int nonblocking);
  184. __poll_t videobuf_poll_stream(struct file *file,
  185. struct videobuf_queue *q,
  186. poll_table *wait);
  187. int videobuf_mmap_setup(struct videobuf_queue *q,
  188. unsigned int bcount, unsigned int bsize,
  189. enum v4l2_memory memory);
  190. int __videobuf_mmap_setup(struct videobuf_queue *q,
  191. unsigned int bcount, unsigned int bsize,
  192. enum v4l2_memory memory);
  193. int videobuf_mmap_free(struct videobuf_queue *q);
  194. int videobuf_mmap_mapper(struct videobuf_queue *q,
  195. struct vm_area_struct *vma);
  196. #endif