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