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