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 | |
| 16 | #include <linux/poll.h> |
| 17 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 18 | #include <linux/videodev.h> |
| 19 | #endif |
| 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 | * |
| 40 | * The mmaper function allows to map any subset of contingous buffers. |
| 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; |
| 53 | unsigned long start; |
| 54 | unsigned long end; |
| 55 | struct videobuf_queue *q; |
| 56 | }; |
| 57 | |
| 58 | enum videobuf_state { |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 59 | VIDEOBUF_NEEDS_INIT = 0, |
| 60 | VIDEOBUF_PREPARED = 1, |
| 61 | VIDEOBUF_QUEUED = 2, |
| 62 | VIDEOBUF_ACTIVE = 3, |
| 63 | VIDEOBUF_DONE = 4, |
| 64 | VIDEOBUF_ERROR = 5, |
| 65 | VIDEOBUF_IDLE = 6, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | struct videobuf_buffer { |
| 69 | unsigned int i; |
| 70 | u32 magic; |
| 71 | |
| 72 | /* info about the buffer */ |
| 73 | unsigned int width; |
| 74 | unsigned int height; |
| 75 | unsigned int bytesperline; /* use only if != 0 */ |
| 76 | unsigned long size; |
| 77 | unsigned int input; |
| 78 | enum v4l2_field field; |
| 79 | enum videobuf_state state; |
| 80 | struct list_head stream; /* QBUF/DQBUF list */ |
| 81 | |
| 82 | /* touched by irq handler */ |
| 83 | struct list_head queue; |
| 84 | wait_queue_head_t done; |
| 85 | unsigned int field_count; |
| 86 | struct timeval ts; |
| 87 | |
| 88 | /* Memory type */ |
| 89 | enum v4l2_memory memory; |
| 90 | |
| 91 | /* buffer size */ |
| 92 | size_t bsize; |
| 93 | |
| 94 | /* buffer offset (mmap + overlay) */ |
| 95 | size_t boff; |
| 96 | |
| 97 | /* buffer addr (userland ptr!) */ |
| 98 | unsigned long baddr; |
| 99 | |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 100 | /* for mmap'ed buffers */ |
| 101 | struct videobuf_mapping *map; |
| 102 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 103 | /* Private pointer to allow specific methods to store their data */ |
| 104 | int privsize; |
| 105 | void *priv; |
| 106 | }; |
| 107 | |
| 108 | struct videobuf_queue_ops { |
| 109 | int (*buf_setup)(struct videobuf_queue *q, |
| 110 | unsigned int *count, unsigned int *size); |
| 111 | int (*buf_prepare)(struct videobuf_queue *q, |
| 112 | struct videobuf_buffer *vb, |
| 113 | enum v4l2_field field); |
| 114 | void (*buf_queue)(struct videobuf_queue *q, |
| 115 | struct videobuf_buffer *vb); |
| 116 | void (*buf_release)(struct videobuf_queue *q, |
| 117 | struct videobuf_buffer *vb); |
| 118 | }; |
| 119 | |
| 120 | #define MAGIC_QTYPE_OPS 0x12261003 |
| 121 | |
| 122 | /* Helper operations - device type dependent */ |
| 123 | struct videobuf_qtype_ops { |
| 124 | u32 magic; |
| 125 | |
| 126 | void* (*alloc) (size_t size); |
| 127 | int (*iolock) (struct videobuf_queue* q, |
| 128 | struct videobuf_buffer *vb, |
| 129 | struct v4l2_framebuffer *fbuf); |
| 130 | int (*mmap) (struct videobuf_queue *q, |
| 131 | unsigned int *count, |
| 132 | unsigned int *size, |
| 133 | enum v4l2_memory memory); |
| 134 | int (*sync) (struct videobuf_queue* q, |
| 135 | struct videobuf_buffer *buf); |
Al Viro | 13bcd5d | 2007-10-13 08:25:24 +0100 | [diff] [blame] | 136 | int (*video_copy_to_user)(struct videobuf_queue *q, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 137 | char __user *data, |
| 138 | size_t count, |
| 139 | int nonblocking); |
| 140 | int (*copy_stream) (struct videobuf_queue *q, |
| 141 | char __user *data, |
| 142 | size_t count, |
| 143 | size_t pos, |
| 144 | int vbihack, |
| 145 | int nonblocking); |
| 146 | int (*mmap_free) (struct videobuf_queue *q); |
| 147 | int (*mmap_mapper) (struct videobuf_queue *q, |
| 148 | struct vm_area_struct *vma); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | struct videobuf_queue { |
| 152 | struct mutex lock; |
| 153 | spinlock_t *irqlock; |
| 154 | void *dev; /* on pci, points to struct pci_dev */ |
| 155 | |
| 156 | enum v4l2_buf_type type; |
| 157 | unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */ |
| 158 | unsigned int msize; |
| 159 | enum v4l2_field field; |
| 160 | enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */ |
| 161 | struct videobuf_buffer *bufs[VIDEO_MAX_FRAME]; |
| 162 | struct videobuf_queue_ops *ops; |
| 163 | struct videobuf_qtype_ops *int_ops; |
| 164 | |
Brandon Philips | d6964aa | 2007-11-06 20:23:08 -0300 | [diff] [blame] | 165 | unsigned int streaming:1; |
| 166 | unsigned int reading:1; |
Mauro Carvalho Chehab | d05051c | 2008-01-10 07:33:03 -0300 | [diff] [blame] | 167 | unsigned int is_mmapped:1; |
| 168 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 169 | /* capture via mmap() + ioctl(QBUF/DQBUF) */ |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 170 | struct list_head stream; |
| 171 | |
| 172 | /* capture via read() */ |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 173 | unsigned int read_off; |
| 174 | struct videobuf_buffer *read_buf; |
| 175 | |
| 176 | /* driver private data */ |
| 177 | void *priv_data; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr); |
| 181 | int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb, |
| 182 | struct v4l2_framebuffer *fbuf); |
| 183 | |
| 184 | void *videobuf_alloc(struct videobuf_queue* q); |
| 185 | |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 186 | void videobuf_queue_core_init(struct videobuf_queue *q, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 187 | struct videobuf_queue_ops *ops, |
| 188 | void *dev, |
| 189 | spinlock_t *irqlock, |
| 190 | enum v4l2_buf_type type, |
| 191 | enum v4l2_field field, |
| 192 | unsigned int msize, |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 193 | void *priv, |
| 194 | struct videobuf_qtype_ops *int_ops); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 195 | int videobuf_queue_is_busy(struct videobuf_queue *q); |
| 196 | void videobuf_queue_cancel(struct videobuf_queue *q); |
| 197 | |
| 198 | enum v4l2_field videobuf_next_field(struct videobuf_queue *q); |
| 199 | int videobuf_reqbufs(struct videobuf_queue *q, |
| 200 | struct v4l2_requestbuffers *req); |
| 201 | int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b); |
| 202 | int videobuf_qbuf(struct videobuf_queue *q, |
| 203 | struct v4l2_buffer *b); |
| 204 | int videobuf_dqbuf(struct videobuf_queue *q, |
| 205 | struct v4l2_buffer *b, int nonblocking); |
| 206 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 207 | int videobuf_cgmbuf(struct videobuf_queue *q, |
| 208 | struct video_mbuf *mbuf, int count); |
| 209 | #endif |
| 210 | int videobuf_streamon(struct videobuf_queue *q); |
| 211 | int videobuf_streamoff(struct videobuf_queue *q); |
| 212 | |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 213 | void videobuf_stop(struct videobuf_queue *q); |
| 214 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 215 | int videobuf_read_start(struct videobuf_queue *q); |
| 216 | void videobuf_read_stop(struct videobuf_queue *q); |
| 217 | ssize_t videobuf_read_stream(struct videobuf_queue *q, |
| 218 | char __user *data, size_t count, loff_t *ppos, |
| 219 | int vbihack, int nonblocking); |
| 220 | ssize_t videobuf_read_one(struct videobuf_queue *q, |
| 221 | char __user *data, size_t count, loff_t *ppos, |
| 222 | int nonblocking); |
| 223 | unsigned int videobuf_poll_stream(struct file *file, |
| 224 | struct videobuf_queue *q, |
| 225 | poll_table *wait); |
| 226 | |
| 227 | int videobuf_mmap_setup(struct videobuf_queue *q, |
| 228 | unsigned int bcount, unsigned int bsize, |
| 229 | enum v4l2_memory memory); |
| 230 | int videobuf_mmap_free(struct videobuf_queue *q); |
| 231 | int videobuf_mmap_mapper(struct videobuf_queue *q, |
| 232 | struct vm_area_struct *vma); |
| 233 | |
| 234 | /* --------------------------------------------------------------------- */ |
| 235 | |
| 236 | /* |
| 237 | * Local variables: |
| 238 | * c-basic-offset: 8 |
| 239 | * End: |
| 240 | */ |