Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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 | |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 16 | #ifndef _VIDEOBUF_CORE_H |
| 17 | #define _VIDEOBUF_CORE_H |
| 18 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 19 | #include <linux/poll.h> |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 20 | #include <linux/videodev2.h> |
| 21 | |
| 22 | #define UNSET (-1U) |
| 23 | |
| 24 | |
| 25 | struct videobuf_buffer; |
| 26 | struct videobuf_queue; |
| 27 | |
| 28 | /* --------------------------------------------------------------------- */ |
| 29 | |
| 30 | /* |
| 31 | * A small set of helper functions to manage video4linux buffers. |
| 32 | * |
| 33 | * struct videobuf_buffer holds the data structures used by the helper |
| 34 | * functions, additionally some commonly used fields for v4l buffers |
| 35 | * (width, height, lists, waitqueue) are in there. That struct should |
| 36 | * be used as first element in the drivers buffer struct. |
| 37 | * |
| 38 | * about the mmap helpers (videobuf_mmap_*): |
| 39 | * |
Masahiro Yamada | e1c0506 | 2015-07-07 10:14:59 +0900 | [diff] [blame] | 40 | * The mmaper function allows to map any subset of contiguous buffers. |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 41 | * This includes one mmap() call for all buffers (which the original |
| 42 | * video4linux API uses) as well as one mmap() for every single buffer |
| 43 | * (which v4l2 uses). |
| 44 | * |
| 45 | * If there is a valid mapping for a buffer, buffer->baddr/bsize holds |
| 46 | * userspace address + size which can be feeded into the |
| 47 | * videobuf_dma_init_user function listed above. |
| 48 | * |
| 49 | */ |
| 50 | |
| 51 | struct videobuf_mapping { |
| 52 | unsigned int count; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 53 | struct videobuf_queue *q; |
| 54 | }; |
| 55 | |
| 56 | enum videobuf_state { |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 57 | VIDEOBUF_NEEDS_INIT = 0, |
| 58 | VIDEOBUF_PREPARED = 1, |
| 59 | VIDEOBUF_QUEUED = 2, |
| 60 | VIDEOBUF_ACTIVE = 3, |
| 61 | VIDEOBUF_DONE = 4, |
| 62 | VIDEOBUF_ERROR = 5, |
| 63 | VIDEOBUF_IDLE = 6, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | struct videobuf_buffer { |
| 67 | unsigned int i; |
| 68 | u32 magic; |
| 69 | |
| 70 | /* info about the buffer */ |
| 71 | unsigned int width; |
| 72 | unsigned int height; |
| 73 | unsigned int bytesperline; /* use only if != 0 */ |
| 74 | unsigned long size; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 75 | enum v4l2_field field; |
| 76 | enum videobuf_state state; |
| 77 | struct list_head stream; /* QBUF/DQBUF list */ |
| 78 | |
| 79 | /* touched by irq handler */ |
| 80 | struct list_head queue; |
| 81 | wait_queue_head_t done; |
| 82 | unsigned int field_count; |
| 83 | struct timeval ts; |
| 84 | |
| 85 | /* Memory type */ |
| 86 | enum v4l2_memory memory; |
| 87 | |
| 88 | /* buffer size */ |
| 89 | size_t bsize; |
| 90 | |
| 91 | /* buffer offset (mmap + overlay) */ |
| 92 | size_t boff; |
| 93 | |
| 94 | /* buffer addr (userland ptr!) */ |
| 95 | unsigned long baddr; |
| 96 | |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 97 | /* for mmap'ed buffers */ |
| 98 | struct videobuf_mapping *map; |
| 99 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 100 | /* Private pointer to allow specific methods to store their data */ |
| 101 | int privsize; |
| 102 | void *priv; |
| 103 | }; |
| 104 | |
| 105 | struct videobuf_queue_ops { |
| 106 | int (*buf_setup)(struct videobuf_queue *q, |
| 107 | unsigned int *count, unsigned int *size); |
| 108 | int (*buf_prepare)(struct videobuf_queue *q, |
| 109 | struct videobuf_buffer *vb, |
| 110 | enum v4l2_field field); |
| 111 | void (*buf_queue)(struct videobuf_queue *q, |
| 112 | struct videobuf_buffer *vb); |
| 113 | void (*buf_release)(struct videobuf_queue *q, |
| 114 | struct videobuf_buffer *vb); |
| 115 | }; |
| 116 | |
| 117 | #define MAGIC_QTYPE_OPS 0x12261003 |
| 118 | |
| 119 | /* Helper operations - device type dependent */ |
| 120 | struct videobuf_qtype_ops { |
| 121 | u32 magic; |
| 122 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 123 | struct videobuf_buffer *(*alloc_vb)(size_t size); |
Hans Verkuil | 037c75e | 2010-03-28 08:18:37 -0300 | [diff] [blame] | 124 | void *(*vaddr) (struct videobuf_buffer *buf); |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 125 | int (*iolock) (struct videobuf_queue *q, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 126 | struct videobuf_buffer *vb, |
| 127 | struct v4l2_framebuffer *fbuf); |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 128 | int (*sync) (struct videobuf_queue *q, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 129 | struct videobuf_buffer *buf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 130 | int (*mmap_mapper) (struct videobuf_queue *q, |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 131 | struct videobuf_buffer *buf, |
| 132 | struct vm_area_struct *vma); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | struct videobuf_queue { |
Mauro Carvalho Chehab | 64f9477 | 2008-01-31 13:57:53 -0300 | [diff] [blame] | 136 | struct mutex vb_lock; |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 137 | struct mutex *ext_lock; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 138 | spinlock_t *irqlock; |
Guennadi Liakhovetski | e9bcf66 | 2008-04-22 14:46:02 -0300 | [diff] [blame] | 139 | struct device *dev; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 140 | |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 141 | wait_queue_head_t wait; /* wait if queue is empty */ |
| 142 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 143 | enum v4l2_buf_type type; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 144 | unsigned int msize; |
| 145 | enum v4l2_field field; |
| 146 | enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */ |
| 147 | struct videobuf_buffer *bufs[VIDEO_MAX_FRAME]; |
Jonathan Corbet | 38a54f3 | 2009-11-17 19:43:41 -0300 | [diff] [blame] | 148 | const struct videobuf_queue_ops *ops; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 149 | struct videobuf_qtype_ops *int_ops; |
| 150 | |
Brandon Philips | d6964aa | 2007-11-06 20:23:08 -0300 | [diff] [blame] | 151 | unsigned int streaming:1; |
| 152 | unsigned int reading:1; |
Mauro Carvalho Chehab | d05051c | 2008-01-10 07:33:03 -0300 | [diff] [blame] | 153 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 154 | /* capture via mmap() + ioctl(QBUF/DQBUF) */ |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 155 | struct list_head stream; |
| 156 | |
| 157 | /* capture via read() */ |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 158 | unsigned int read_off; |
| 159 | struct videobuf_buffer *read_buf; |
| 160 | |
| 161 | /* driver private data */ |
| 162 | void *priv_data; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 163 | }; |
| 164 | |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 165 | static inline void videobuf_queue_lock(struct videobuf_queue *q) |
| 166 | { |
| 167 | if (!q->ext_lock) |
| 168 | mutex_lock(&q->vb_lock); |
| 169 | } |
| 170 | |
| 171 | static inline void videobuf_queue_unlock(struct videobuf_queue *q) |
| 172 | { |
| 173 | if (!q->ext_lock) |
| 174 | mutex_unlock(&q->vb_lock); |
| 175 | } |
| 176 | |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 177 | int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb, |
| 178 | int non_blocking, int intr); |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 179 | int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 180 | struct v4l2_framebuffer *fbuf); |
| 181 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 182 | struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 183 | |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 184 | /* Used on videobuf-dvb */ |
Hans Verkuil | f4fce60 | 2010-03-28 08:33:23 -0300 | [diff] [blame] | 185 | void *videobuf_queue_to_vaddr(struct videobuf_queue *q, |
| 186 | struct videobuf_buffer *buf); |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 187 | |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 188 | void videobuf_queue_core_init(struct videobuf_queue *q, |
Jonathan Corbet | 38a54f3 | 2009-11-17 19:43:41 -0300 | [diff] [blame] | 189 | const struct videobuf_queue_ops *ops, |
Guennadi Liakhovetski | e9bcf66 | 2008-04-22 14:46:02 -0300 | [diff] [blame] | 190 | struct device *dev, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 191 | spinlock_t *irqlock, |
| 192 | enum v4l2_buf_type type, |
| 193 | enum v4l2_field field, |
| 194 | unsigned int msize, |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 195 | void *priv, |
Hans Verkuil | 08bff03 | 2010-09-20 17:39:46 -0300 | [diff] [blame] | 196 | struct videobuf_qtype_ops *int_ops, |
| 197 | struct mutex *ext_lock); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 198 | int videobuf_queue_is_busy(struct videobuf_queue *q); |
| 199 | void videobuf_queue_cancel(struct videobuf_queue *q); |
| 200 | |
| 201 | enum v4l2_field videobuf_next_field(struct videobuf_queue *q); |
| 202 | int videobuf_reqbufs(struct videobuf_queue *q, |
| 203 | struct v4l2_requestbuffers *req); |
| 204 | int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b); |
| 205 | int videobuf_qbuf(struct videobuf_queue *q, |
| 206 | struct v4l2_buffer *b); |
| 207 | int videobuf_dqbuf(struct videobuf_queue *q, |
| 208 | struct v4l2_buffer *b, int nonblocking); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 209 | int videobuf_streamon(struct videobuf_queue *q); |
| 210 | int videobuf_streamoff(struct videobuf_queue *q); |
| 211 | |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 212 | void videobuf_stop(struct videobuf_queue *q); |
| 213 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 214 | int videobuf_read_start(struct videobuf_queue *q); |
| 215 | void videobuf_read_stop(struct videobuf_queue *q); |
| 216 | ssize_t videobuf_read_stream(struct videobuf_queue *q, |
| 217 | char __user *data, size_t count, loff_t *ppos, |
| 218 | int vbihack, int nonblocking); |
| 219 | ssize_t videobuf_read_one(struct videobuf_queue *q, |
| 220 | char __user *data, size_t count, loff_t *ppos, |
| 221 | int nonblocking); |
| 222 | unsigned int videobuf_poll_stream(struct file *file, |
| 223 | struct videobuf_queue *q, |
| 224 | poll_table *wait); |
| 225 | |
| 226 | int videobuf_mmap_setup(struct videobuf_queue *q, |
| 227 | unsigned int bcount, unsigned int bsize, |
| 228 | enum v4l2_memory memory); |
Arjan van de Ven | 81b2dbc | 2008-05-20 09:53:52 -0700 | [diff] [blame] | 229 | int __videobuf_mmap_setup(struct videobuf_queue *q, |
| 230 | unsigned int bcount, unsigned int bsize, |
| 231 | enum v4l2_memory memory); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 232 | int videobuf_mmap_free(struct videobuf_queue *q); |
| 233 | int videobuf_mmap_mapper(struct videobuf_queue *q, |
| 234 | struct vm_area_struct *vma); |
| 235 | |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 236 | #endif |