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; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 142 | spinlock_t *irqlock; |
Guennadi Liakhovetski | e9bcf66 | 2008-04-22 14:46:02 -0300 | [diff] [blame] | 143 | struct device *dev; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 144 | |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 145 | wait_queue_head_t wait; /* wait if queue is empty */ |
| 146 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 147 | enum v4l2_buf_type type; |
| 148 | unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */ |
| 149 | unsigned int msize; |
| 150 | enum v4l2_field field; |
| 151 | enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */ |
| 152 | struct videobuf_buffer *bufs[VIDEO_MAX_FRAME]; |
Jonathan Corbet | 38a54f3 | 2009-11-17 19:43:41 -0300 | [diff] [blame] | 153 | const struct videobuf_queue_ops *ops; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 154 | struct videobuf_qtype_ops *int_ops; |
| 155 | |
Brandon Philips | d6964aa | 2007-11-06 20:23:08 -0300 | [diff] [blame] | 156 | unsigned int streaming:1; |
| 157 | unsigned int reading:1; |
Mauro Carvalho Chehab | d05051c | 2008-01-10 07:33:03 -0300 | [diff] [blame] | 158 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 159 | /* capture via mmap() + ioctl(QBUF/DQBUF) */ |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 160 | struct list_head stream; |
| 161 | |
| 162 | /* capture via read() */ |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 163 | unsigned int read_off; |
| 164 | struct videobuf_buffer *read_buf; |
| 165 | |
| 166 | /* driver private data */ |
| 167 | void *priv_data; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr); |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 171 | int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 172 | struct v4l2_framebuffer *fbuf); |
| 173 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 174 | struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 175 | |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 176 | /* Used on videobuf-dvb */ |
Hans Verkuil | f4fce60 | 2010-03-28 08:33:23 -0300 | [diff] [blame] | 177 | void *videobuf_queue_to_vaddr(struct videobuf_queue *q, |
| 178 | struct videobuf_buffer *buf); |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 179 | |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 180 | void videobuf_queue_core_init(struct videobuf_queue *q, |
Jonathan Corbet | 38a54f3 | 2009-11-17 19:43:41 -0300 | [diff] [blame] | 181 | const struct videobuf_queue_ops *ops, |
Guennadi Liakhovetski | e9bcf66 | 2008-04-22 14:46:02 -0300 | [diff] [blame] | 182 | struct device *dev, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 183 | spinlock_t *irqlock, |
| 184 | enum v4l2_buf_type type, |
| 185 | enum v4l2_field field, |
| 186 | unsigned int msize, |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 187 | void *priv, |
| 188 | struct videobuf_qtype_ops *int_ops); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 189 | int videobuf_queue_is_busy(struct videobuf_queue *q); |
| 190 | void videobuf_queue_cancel(struct videobuf_queue *q); |
| 191 | |
| 192 | enum v4l2_field videobuf_next_field(struct videobuf_queue *q); |
| 193 | int videobuf_reqbufs(struct videobuf_queue *q, |
| 194 | struct v4l2_requestbuffers *req); |
| 195 | int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b); |
| 196 | int videobuf_qbuf(struct videobuf_queue *q, |
| 197 | struct v4l2_buffer *b); |
| 198 | int videobuf_dqbuf(struct videobuf_queue *q, |
| 199 | struct v4l2_buffer *b, int nonblocking); |
| 200 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 201 | int videobuf_cgmbuf(struct videobuf_queue *q, |
| 202 | struct video_mbuf *mbuf, int count); |
| 203 | #endif |
| 204 | int videobuf_streamon(struct videobuf_queue *q); |
| 205 | int videobuf_streamoff(struct videobuf_queue *q); |
| 206 | |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 207 | void videobuf_stop(struct videobuf_queue *q); |
| 208 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 209 | int videobuf_read_start(struct videobuf_queue *q); |
| 210 | void videobuf_read_stop(struct videobuf_queue *q); |
| 211 | ssize_t videobuf_read_stream(struct videobuf_queue *q, |
| 212 | char __user *data, size_t count, loff_t *ppos, |
| 213 | int vbihack, int nonblocking); |
| 214 | ssize_t videobuf_read_one(struct videobuf_queue *q, |
| 215 | char __user *data, size_t count, loff_t *ppos, |
| 216 | int nonblocking); |
| 217 | unsigned int videobuf_poll_stream(struct file *file, |
| 218 | struct videobuf_queue *q, |
| 219 | poll_table *wait); |
| 220 | |
| 221 | int videobuf_mmap_setup(struct videobuf_queue *q, |
| 222 | unsigned int bcount, unsigned int bsize, |
| 223 | enum v4l2_memory memory); |
Arjan van de Ven | 81b2dbc | 2008-05-20 09:53:52 -0700 | [diff] [blame] | 224 | int __videobuf_mmap_setup(struct videobuf_queue *q, |
| 225 | unsigned int bcount, unsigned int bsize, |
| 226 | enum v4l2_memory memory); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 227 | int videobuf_mmap_free(struct videobuf_queue *q); |
| 228 | int videobuf_mmap_mapper(struct videobuf_queue *q, |
| 229 | struct vm_area_struct *vma); |
| 230 | |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 231 | #endif |