Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * generic helper functions for video4linux capture buffers, to handle |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 4 | * memory management and PCI DMA. |
| 5 | * Right now, bttv, saa7134, saa7146 and cx88 use it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * The functions expect the hardware being able to scatter gatter |
| 8 | * (i.e. the buffers are not linear in physical memory, but fragmented |
| 9 | * into PAGE_SIZE chunks). They also assume the driver does not need |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 10 | * to touch the video data. |
| 11 | * |
| 12 | * device specific map/unmap/sync stuff now are mapped as operations |
| 13 | * to allow its usage by USB and virtual devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
| 15 | * (c) 2001-2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs] |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 16 | * (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org> |
| 17 | * (c) 2006 Ted Walther and John Sokol |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * |
| 19 | * This program is free software; you can redistribute it and/or modify |
| 20 | * it under the terms of the GNU General Public License as published by |
| 21 | * the Free Software Foundation; either version 2 of the License, or |
| 22 | * (at your option) any later version. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/vmalloc.h> |
| 29 | #include <linux/pagemap.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/pci.h> |
| 32 | #include <linux/interrupt.h> |
| 33 | #include <asm/page.h> |
| 34 | #include <asm/pgtable.h> |
| 35 | |
| 36 | #include <media/video-buf.h> |
| 37 | |
| 38 | #define MAGIC_DMABUF 0x19721112 |
| 39 | #define MAGIC_BUFFER 0x20040302 |
| 40 | #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \ |
| 41 | { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); } |
| 42 | |
| 43 | static int debug = 0; |
| 44 | module_param(debug, int, 0644); |
| 45 | |
| 46 | MODULE_DESCRIPTION("helper module to manage video4linux pci dma buffers"); |
| 47 | MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]"); |
| 48 | MODULE_LICENSE("GPL"); |
| 49 | |
| 50 | #define dprintk(level, fmt, arg...) if (debug >= level) \ |
| 51 | printk(KERN_DEBUG "vbuf: " fmt , ## arg) |
| 52 | |
| 53 | struct scatterlist* |
| 54 | videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages) |
| 55 | { |
| 56 | struct scatterlist *sglist; |
| 57 | struct page *pg; |
| 58 | int i; |
| 59 | |
Panagiotis Issaris | 7408187 | 2006-01-11 19:40:56 -0200 | [diff] [blame] | 60 | sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | if (NULL == sglist) |
| 62 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) { |
| 64 | pg = vmalloc_to_page(virt); |
| 65 | if (NULL == pg) |
| 66 | goto err; |
Eric Sesterhenn | ae24601 | 2006-03-13 13:17:11 -0300 | [diff] [blame] | 67 | BUG_ON(PageHighMem(pg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | sglist[i].page = pg; |
| 69 | sglist[i].length = PAGE_SIZE; |
| 70 | } |
| 71 | return sglist; |
| 72 | |
| 73 | err: |
| 74 | kfree(sglist); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | struct scatterlist* |
| 79 | videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset) |
| 80 | { |
| 81 | struct scatterlist *sglist; |
| 82 | int i = 0; |
| 83 | |
| 84 | if (NULL == pages[0]) |
| 85 | return NULL; |
Panagiotis Issaris | 7408187 | 2006-01-11 19:40:56 -0200 | [diff] [blame] | 86 | sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if (NULL == sglist) |
| 88 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | if (NULL == pages[0]) |
| 91 | goto nopage; |
| 92 | if (PageHighMem(pages[0])) |
| 93 | /* DMA to highmem pages might not work */ |
| 94 | goto highmem; |
| 95 | sglist[0].page = pages[0]; |
| 96 | sglist[0].offset = offset; |
| 97 | sglist[0].length = PAGE_SIZE - offset; |
| 98 | for (i = 1; i < nr_pages; i++) { |
| 99 | if (NULL == pages[i]) |
| 100 | goto nopage; |
| 101 | if (PageHighMem(pages[i])) |
| 102 | goto highmem; |
| 103 | sglist[i].page = pages[i]; |
| 104 | sglist[i].length = PAGE_SIZE; |
| 105 | } |
| 106 | return sglist; |
| 107 | |
| 108 | nopage: |
| 109 | dprintk(2,"sgl: oops - no page\n"); |
| 110 | kfree(sglist); |
| 111 | return NULL; |
| 112 | |
| 113 | highmem: |
| 114 | dprintk(2,"sgl: oops - highmem page\n"); |
| 115 | kfree(sglist); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | /* --------------------------------------------------------------------- */ |
| 120 | |
| 121 | void videobuf_dma_init(struct videobuf_dmabuf *dma) |
| 122 | { |
| 123 | memset(dma,0,sizeof(*dma)); |
| 124 | dma->magic = MAGIC_DMABUF; |
| 125 | } |
| 126 | |
| 127 | int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction, |
| 128 | unsigned long data, unsigned long size) |
| 129 | { |
| 130 | unsigned long first,last; |
| 131 | int err, rw = 0; |
| 132 | |
| 133 | dma->direction = direction; |
| 134 | switch (dma->direction) { |
| 135 | case PCI_DMA_FROMDEVICE: rw = READ; break; |
| 136 | case PCI_DMA_TODEVICE: rw = WRITE; break; |
| 137 | default: BUG(); |
| 138 | } |
| 139 | |
| 140 | first = (data & PAGE_MASK) >> PAGE_SHIFT; |
| 141 | last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT; |
| 142 | dma->offset = data & ~PAGE_MASK; |
| 143 | dma->nr_pages = last-first+1; |
| 144 | dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*), |
| 145 | GFP_KERNEL); |
| 146 | if (NULL == dma->pages) |
| 147 | return -ENOMEM; |
| 148 | dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n", |
| 149 | data,size,dma->nr_pages); |
| 150 | |
Mauro Carvalho Chehab | b50e7fe | 2007-01-25 05:00:01 -0300 | [diff] [blame] | 151 | dma->varea = (void *) data; |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | down_read(¤t->mm->mmap_sem); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 154 | err = get_user_pages(current,current->mm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | data & PAGE_MASK, dma->nr_pages, |
| 156 | rw == READ, 1, /* force */ |
| 157 | dma->pages, NULL); |
| 158 | up_read(¤t->mm->mmap_sem); |
| 159 | if (err != dma->nr_pages) { |
| 160 | dma->nr_pages = (err >= 0) ? err : 0; |
| 161 | dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages); |
| 162 | return err < 0 ? err : -EINVAL; |
| 163 | } |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction, |
| 168 | int nr_pages) |
| 169 | { |
| 170 | dprintk(1,"init kernel [%d pages]\n",nr_pages); |
| 171 | dma->direction = direction; |
| 172 | dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT); |
| 173 | if (NULL == dma->vmalloc) { |
| 174 | dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages); |
| 175 | return -ENOMEM; |
| 176 | } |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 177 | dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n", |
| 178 | (unsigned long)dma->vmalloc, |
| 179 | nr_pages << PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT); |
| 181 | dma->nr_pages = nr_pages; |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction, |
| 186 | dma_addr_t addr, int nr_pages) |
| 187 | { |
| 188 | dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n", |
| 189 | nr_pages,(unsigned long)addr); |
| 190 | dma->direction = direction; |
| 191 | if (0 == addr) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | dma->bus_addr = addr; |
| 195 | dma->nr_pages = nr_pages; |
| 196 | return 0; |
| 197 | } |
| 198 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 199 | int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 201 | void *dev=q->dev; |
| 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | MAGIC_CHECK(dma->magic,MAGIC_DMABUF); |
| 204 | BUG_ON(0 == dma->nr_pages); |
| 205 | |
| 206 | if (dma->pages) { |
| 207 | dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages, |
| 208 | dma->offset); |
| 209 | } |
| 210 | if (dma->vmalloc) { |
| 211 | dma->sglist = videobuf_vmalloc_to_sg |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 212 | (dma->vmalloc,dma->nr_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | if (dma->bus_addr) { |
| 215 | dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL); |
| 216 | if (NULL != dma->sglist) { |
| 217 | dma->sglen = 1; |
| 218 | sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK; |
| 219 | dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK; |
| 220 | sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE; |
| 221 | } |
| 222 | } |
| 223 | if (NULL == dma->sglist) { |
| 224 | dprintk(1,"scatterlist is NULL\n"); |
| 225 | return -ENOMEM; |
| 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | if (!dma->bus_addr) { |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 228 | if (q->ops->vb_map_sg) { |
| 229 | dma->sglen = q->ops->vb_map_sg(dev,dma->sglist, |
| 230 | dma->nr_pages, dma->direction); |
| 231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | if (0 == dma->sglen) { |
| 233 | printk(KERN_WARNING |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 234 | "%s: videobuf_map_sg failed\n",__FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | kfree(dma->sglist); |
| 236 | dma->sglist = NULL; |
| 237 | dma->sglen = 0; |
| 238 | return -EIO; |
| 239 | } |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 244 | int videobuf_dma_sync(struct videobuf_queue* q,struct videobuf_dmabuf *dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 246 | void *dev=q->dev; |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | MAGIC_CHECK(dma->magic,MAGIC_DMABUF); |
| 249 | BUG_ON(!dma->sglen); |
| 250 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 251 | if (!dma->bus_addr && q->ops->vb_dma_sync_sg) |
| 252 | q->ops->vb_dma_sync_sg(dev,dma->sglist,dma->nr_pages, |
| 253 | dma->direction); |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 258 | int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 260 | void *dev=q->dev; |
| 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | MAGIC_CHECK(dma->magic,MAGIC_DMABUF); |
| 263 | if (!dma->sglen) |
| 264 | return 0; |
| 265 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 266 | if (!dma->bus_addr && q->ops->vb_unmap_sg) |
| 267 | q->ops->vb_unmap_sg(dev,dma->sglist,dma->nr_pages, |
| 268 | dma->direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | kfree(dma->sglist); |
| 270 | dma->sglist = NULL; |
| 271 | dma->sglen = 0; |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | int videobuf_dma_free(struct videobuf_dmabuf *dma) |
| 276 | { |
| 277 | MAGIC_CHECK(dma->magic,MAGIC_DMABUF); |
| 278 | BUG_ON(dma->sglen); |
| 279 | |
| 280 | if (dma->pages) { |
| 281 | int i; |
| 282 | for (i=0; i < dma->nr_pages; i++) |
| 283 | page_cache_release(dma->pages[i]); |
| 284 | kfree(dma->pages); |
| 285 | dma->pages = NULL; |
| 286 | } |
Jesper Juhl | f9101210 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 287 | |
| 288 | vfree(dma->vmalloc); |
| 289 | dma->vmalloc = NULL; |
Mauro Carvalho Chehab | b50e7fe | 2007-01-25 05:00:01 -0300 | [diff] [blame] | 290 | dma->varea = NULL; |
Jesper Juhl | f9101210 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | if (dma->bus_addr) { |
| 293 | dma->bus_addr = 0; |
| 294 | } |
| 295 | dma->direction = PCI_DMA_NONE; |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | /* --------------------------------------------------------------------- */ |
| 300 | |
| 301 | void* videobuf_alloc(unsigned int size) |
| 302 | { |
| 303 | struct videobuf_buffer *vb; |
| 304 | |
Panagiotis Issaris | 7408187 | 2006-01-11 19:40:56 -0200 | [diff] [blame] | 305 | vb = kzalloc(size,GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | if (NULL != vb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | videobuf_dma_init(&vb->dma); |
| 308 | init_waitqueue_head(&vb->done); |
| 309 | vb->magic = MAGIC_BUFFER; |
| 310 | } |
| 311 | return vb; |
| 312 | } |
| 313 | |
| 314 | int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr) |
| 315 | { |
| 316 | int retval = 0; |
| 317 | DECLARE_WAITQUEUE(wait, current); |
| 318 | |
| 319 | MAGIC_CHECK(vb->magic,MAGIC_BUFFER); |
| 320 | add_wait_queue(&vb->done, &wait); |
| 321 | while (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) { |
| 322 | if (non_blocking) { |
| 323 | retval = -EAGAIN; |
| 324 | break; |
| 325 | } |
| 326 | set_current_state(intr ? TASK_INTERRUPTIBLE |
| 327 | : TASK_UNINTERRUPTIBLE); |
| 328 | if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) |
| 329 | schedule(); |
| 330 | set_current_state(TASK_RUNNING); |
| 331 | if (intr && signal_pending(current)) { |
| 332 | dprintk(1,"buffer waiton: -EINTR\n"); |
| 333 | retval = -EINTR; |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | remove_wait_queue(&vb->done, &wait); |
| 338 | return retval; |
| 339 | } |
| 340 | |
| 341 | int |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 342 | videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | struct v4l2_framebuffer *fbuf) |
| 344 | { |
| 345 | int err,pages; |
| 346 | dma_addr_t bus; |
| 347 | |
| 348 | MAGIC_CHECK(vb->magic,MAGIC_BUFFER); |
| 349 | switch (vb->memory) { |
| 350 | case V4L2_MEMORY_MMAP: |
| 351 | case V4L2_MEMORY_USERPTR: |
| 352 | if (0 == vb->baddr) { |
| 353 | /* no userspace addr -- kernel bounce buffer */ |
| 354 | pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; |
| 355 | err = videobuf_dma_init_kernel(&vb->dma,PCI_DMA_FROMDEVICE, |
| 356 | pages); |
| 357 | if (0 != err) |
| 358 | return err; |
| 359 | } else { |
| 360 | /* dma directly to userspace */ |
| 361 | err = videobuf_dma_init_user(&vb->dma,PCI_DMA_FROMDEVICE, |
| 362 | vb->baddr,vb->bsize); |
| 363 | if (0 != err) |
| 364 | return err; |
| 365 | } |
| 366 | break; |
| 367 | case V4L2_MEMORY_OVERLAY: |
| 368 | if (NULL == fbuf) |
| 369 | return -EINVAL; |
| 370 | /* FIXME: need sanity checks for vb->boff */ |
Sujoy Gupta | 6eb32a79 | 2006-09-30 06:55:29 -0300 | [diff] [blame] | 371 | /* |
| 372 | * Using a double cast to avoid compiler warnings when |
| 373 | * building for PAE. Compiler doesn't like direct casting |
| 374 | * of a 32 bit ptr to 64 bit integer. |
| 375 | */ |
| 376 | bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; |
| 378 | err = videobuf_dma_init_overlay(&vb->dma,PCI_DMA_FROMDEVICE, |
| 379 | bus, pages); |
| 380 | if (0 != err) |
| 381 | return err; |
| 382 | break; |
| 383 | default: |
| 384 | BUG(); |
| 385 | } |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 386 | err = videobuf_dma_map(q,&vb->dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (0 != err) |
| 388 | return err; |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | /* --------------------------------------------------------------------- */ |
| 394 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 395 | void videobuf_queue_pci(struct videobuf_queue* q) |
| 396 | { |
| 397 | /* If not specified, defaults to PCI map sg */ |
| 398 | if (!q->ops->vb_map_sg) |
| 399 | q->ops->vb_map_sg=(vb_map_sg_t *)pci_map_sg; |
| 400 | |
| 401 | if (!q->ops->vb_dma_sync_sg) |
| 402 | q->ops->vb_dma_sync_sg=(vb_map_sg_t *)pci_dma_sync_sg_for_cpu; |
| 403 | if (!q->ops->vb_unmap_sg) |
| 404 | q->ops->vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg; |
| 405 | } |
| 406 | |
| 407 | int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma) |
| 408 | { |
| 409 | struct videobuf_queue q; |
Ricardo Cerqueira | 731f902 | 2006-03-27 09:16:31 -0300 | [diff] [blame] | 410 | struct videobuf_queue_ops qops; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 411 | |
| 412 | q.dev=pci; |
Ricardo Cerqueira | 731f902 | 2006-03-27 09:16:31 -0300 | [diff] [blame] | 413 | qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg; |
| 414 | qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg; |
| 415 | q.ops = &qops; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 416 | |
Ricardo Cerqueira | 731f902 | 2006-03-27 09:16:31 -0300 | [diff] [blame] | 417 | return (videobuf_dma_map(&q,dma)); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma) |
| 421 | { |
| 422 | struct videobuf_queue q; |
Ricardo Cerqueira | 731f902 | 2006-03-27 09:16:31 -0300 | [diff] [blame] | 423 | struct videobuf_queue_ops qops; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 424 | |
| 425 | q.dev=pci; |
Ricardo Cerqueira | 731f902 | 2006-03-27 09:16:31 -0300 | [diff] [blame] | 426 | qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg; |
| 427 | qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg; |
| 428 | q.ops = &qops; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 429 | |
| 430 | return (videobuf_dma_unmap(&q,dma)); |
| 431 | } |
| 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | void videobuf_queue_init(struct videobuf_queue* q, |
| 434 | struct videobuf_queue_ops *ops, |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 435 | void *dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | spinlock_t *irqlock, |
| 437 | enum v4l2_buf_type type, |
| 438 | enum v4l2_field field, |
| 439 | unsigned int msize, |
| 440 | void *priv) |
| 441 | { |
| 442 | memset(q,0,sizeof(*q)); |
| 443 | q->irqlock = irqlock; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 444 | q->dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | q->type = type; |
| 446 | q->field = field; |
| 447 | q->msize = msize; |
| 448 | q->ops = ops; |
| 449 | q->priv_data = priv; |
| 450 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 451 | videobuf_queue_pci(q); |
| 452 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 453 | mutex_init(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | INIT_LIST_HEAD(&q->stream); |
| 455 | } |
| 456 | |
| 457 | int |
| 458 | videobuf_queue_is_busy(struct videobuf_queue *q) |
| 459 | { |
| 460 | int i; |
| 461 | |
| 462 | if (q->streaming) { |
| 463 | dprintk(1,"busy: streaming active\n"); |
| 464 | return 1; |
| 465 | } |
| 466 | if (q->reading) { |
| 467 | dprintk(1,"busy: pending read #1\n"); |
| 468 | return 1; |
| 469 | } |
| 470 | if (q->read_buf) { |
| 471 | dprintk(1,"busy: pending read #2\n"); |
| 472 | return 1; |
| 473 | } |
| 474 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 475 | if (NULL == q->bufs[i]) |
| 476 | continue; |
| 477 | if (q->bufs[i]->map) { |
| 478 | dprintk(1,"busy: buffer #%d mapped\n",i); |
| 479 | return 1; |
| 480 | } |
| 481 | if (q->bufs[i]->state == STATE_QUEUED) { |
| 482 | dprintk(1,"busy: buffer #%d queued\n",i); |
| 483 | return 1; |
| 484 | } |
| 485 | if (q->bufs[i]->state == STATE_ACTIVE) { |
| 486 | dprintk(1,"busy: buffer #%d avtive\n",i); |
| 487 | return 1; |
| 488 | } |
| 489 | } |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | void |
| 494 | videobuf_queue_cancel(struct videobuf_queue *q) |
| 495 | { |
Mauro Carvalho Chehab | 9b89f2d | 2006-03-15 17:52:14 -0300 | [diff] [blame] | 496 | unsigned long flags=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | int i; |
| 498 | |
| 499 | /* remove queued buffers from list */ |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 500 | if (q->irqlock) |
| 501 | spin_lock_irqsave(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 503 | if (NULL == q->bufs[i]) |
| 504 | continue; |
| 505 | if (q->bufs[i]->state == STATE_QUEUED) { |
| 506 | list_del(&q->bufs[i]->queue); |
| 507 | q->bufs[i]->state = STATE_ERROR; |
| 508 | } |
| 509 | } |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 510 | if (q->irqlock) |
| 511 | spin_unlock_irqrestore(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
| 513 | /* free all buffers + clear queue */ |
| 514 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 515 | if (NULL == q->bufs[i]) |
| 516 | continue; |
| 517 | q->ops->buf_release(q,q->bufs[i]); |
| 518 | } |
| 519 | INIT_LIST_HEAD(&q->stream); |
| 520 | } |
| 521 | |
| 522 | /* --------------------------------------------------------------------- */ |
| 523 | |
| 524 | enum v4l2_field |
| 525 | videobuf_next_field(struct videobuf_queue *q) |
| 526 | { |
| 527 | enum v4l2_field field = q->field; |
| 528 | |
| 529 | BUG_ON(V4L2_FIELD_ANY == field); |
| 530 | |
| 531 | if (V4L2_FIELD_ALTERNATE == field) { |
| 532 | if (V4L2_FIELD_TOP == q->last) { |
| 533 | field = V4L2_FIELD_BOTTOM; |
| 534 | q->last = V4L2_FIELD_BOTTOM; |
| 535 | } else { |
| 536 | field = V4L2_FIELD_TOP; |
| 537 | q->last = V4L2_FIELD_TOP; |
| 538 | } |
| 539 | } |
| 540 | return field; |
| 541 | } |
| 542 | |
| 543 | void |
| 544 | videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb, |
| 545 | enum v4l2_buf_type type) |
| 546 | { |
| 547 | MAGIC_CHECK(vb->magic,MAGIC_BUFFER); |
| 548 | |
| 549 | b->index = vb->i; |
| 550 | b->type = type; |
| 551 | |
| 552 | b->memory = vb->memory; |
| 553 | switch (b->memory) { |
| 554 | case V4L2_MEMORY_MMAP: |
| 555 | b->m.offset = vb->boff; |
| 556 | b->length = vb->bsize; |
| 557 | break; |
| 558 | case V4L2_MEMORY_USERPTR: |
| 559 | b->m.userptr = vb->baddr; |
| 560 | b->length = vb->bsize; |
| 561 | break; |
| 562 | case V4L2_MEMORY_OVERLAY: |
| 563 | b->m.offset = vb->boff; |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | b->flags = 0; |
| 568 | if (vb->map) |
| 569 | b->flags |= V4L2_BUF_FLAG_MAPPED; |
| 570 | |
| 571 | switch (vb->state) { |
| 572 | case STATE_PREPARED: |
| 573 | case STATE_QUEUED: |
| 574 | case STATE_ACTIVE: |
| 575 | b->flags |= V4L2_BUF_FLAG_QUEUED; |
| 576 | break; |
| 577 | case STATE_DONE: |
| 578 | case STATE_ERROR: |
| 579 | b->flags |= V4L2_BUF_FLAG_DONE; |
| 580 | break; |
| 581 | case STATE_NEEDS_INIT: |
| 582 | case STATE_IDLE: |
| 583 | /* nothing */ |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | if (vb->input != UNSET) { |
| 588 | b->flags |= V4L2_BUF_FLAG_INPUT; |
| 589 | b->input = vb->input; |
| 590 | } |
| 591 | |
| 592 | b->field = vb->field; |
| 593 | b->timestamp = vb->ts; |
| 594 | b->bytesused = vb->size; |
| 595 | b->sequence = vb->field_count >> 1; |
| 596 | } |
| 597 | |
| 598 | int |
| 599 | videobuf_reqbufs(struct videobuf_queue *q, |
| 600 | struct v4l2_requestbuffers *req) |
| 601 | { |
| 602 | unsigned int size,count; |
| 603 | int retval; |
| 604 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 605 | if (req->type != q->type) { |
| 606 | dprintk(1,"reqbufs: queue type invalid\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | return -EINVAL; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 608 | } |
| 609 | if (req->count < 1) { |
| 610 | dprintk(1,"reqbufs: count invalid (%d)\n",req->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | return -EINVAL; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 612 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | if (req->memory != V4L2_MEMORY_MMAP && |
| 614 | req->memory != V4L2_MEMORY_USERPTR && |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 615 | req->memory != V4L2_MEMORY_OVERLAY) { |
| 616 | dprintk(1,"reqbufs: memory type invalid\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | return -EINVAL; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 618 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 620 | if (q->streaming) { |
| 621 | dprintk(1,"reqbufs: streaming already exists\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | return -EBUSY; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 623 | } |
| 624 | if (!list_empty(&q->stream)) { |
| 625 | dprintk(1,"reqbufs: stream running\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | return -EBUSY; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 627 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 629 | mutex_lock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | count = req->count; |
| 631 | if (count > VIDEO_MAX_FRAME) |
| 632 | count = VIDEO_MAX_FRAME; |
| 633 | size = 0; |
| 634 | q->ops->buf_setup(q,&count,&size); |
| 635 | size = PAGE_ALIGN(size); |
| 636 | dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n", |
| 637 | count, size, (count*size)>>PAGE_SHIFT); |
| 638 | |
| 639 | retval = videobuf_mmap_setup(q,count,size,req->memory); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 640 | if (retval < 0) { |
| 641 | dprintk(1,"reqbufs: mmap setup returned %d\n",retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
| 645 | req->count = count; |
| 646 | |
| 647 | done: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 648 | mutex_unlock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | return retval; |
| 650 | } |
| 651 | |
| 652 | int |
| 653 | videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b) |
| 654 | { |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 655 | if (unlikely(b->type != q->type)) { |
| 656 | dprintk(1,"querybuf: Wrong type.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return -EINVAL; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 658 | } |
| 659 | if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) { |
| 660 | dprintk(1,"querybuf: index out of range.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | return -EINVAL; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 662 | } |
| 663 | if (unlikely(NULL == q->bufs[b->index])) { |
| 664 | dprintk(1,"querybuf: buffer is null.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | return -EINVAL; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 666 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | videobuf_status(b,q->bufs[b->index],q->type); |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | int |
| 672 | videobuf_qbuf(struct videobuf_queue *q, |
| 673 | struct v4l2_buffer *b) |
| 674 | { |
| 675 | struct videobuf_buffer *buf; |
| 676 | enum v4l2_field field; |
Mauro Carvalho Chehab | 9b89f2d | 2006-03-15 17:52:14 -0300 | [diff] [blame] | 677 | unsigned long flags=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | int retval; |
| 679 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 680 | mutex_lock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | retval = -EBUSY; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 682 | if (q->reading) { |
| 683 | dprintk(1,"qbuf: Reading running...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | retval = -EINVAL; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 687 | if (b->type != q->type) { |
| 688 | dprintk(1,"qbuf: Wrong type.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 690 | } |
| 691 | if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) { |
| 692 | dprintk(1,"qbuf: index out of range.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 694 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | buf = q->bufs[b->index]; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 696 | if (NULL == buf) { |
| 697 | dprintk(1,"qbuf: buffer is null.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | MAGIC_CHECK(buf->magic,MAGIC_BUFFER); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 701 | if (buf->memory != b->memory) { |
| 702 | dprintk(1,"qbuf: memory type is wrong.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 704 | } |
Sakari Ailus | 36a63ee | 2007-05-04 12:19:59 -0300 | [diff] [blame^] | 705 | if (buf->state != STATE_NEEDS_INIT && buf->state != STATE_IDLE) { |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 706 | dprintk(1,"qbuf: buffer is already queued or active.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 708 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
| 710 | if (b->flags & V4L2_BUF_FLAG_INPUT) { |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 711 | if (b->input >= q->inputs) { |
| 712 | dprintk(1,"qbuf: wrong input.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 714 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | buf->input = b->input; |
| 716 | } else { |
| 717 | buf->input = UNSET; |
| 718 | } |
| 719 | |
| 720 | switch (b->memory) { |
| 721 | case V4L2_MEMORY_MMAP: |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 722 | if (0 == buf->baddr) { |
| 723 | dprintk(1,"qbuf: mmap requested but buffer addr is zero!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | break; |
| 727 | case V4L2_MEMORY_USERPTR: |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 728 | if (b->length < buf->bsize) { |
| 729 | dprintk(1,"qbuf: buffer length is not enough\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr) |
| 733 | q->ops->buf_release(q,buf); |
| 734 | buf->baddr = b->m.userptr; |
| 735 | break; |
| 736 | case V4L2_MEMORY_OVERLAY: |
| 737 | buf->boff = b->m.offset; |
| 738 | break; |
| 739 | default: |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 740 | dprintk(1,"qbuf: wrong memory type\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | goto done; |
| 742 | } |
| 743 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 744 | dprintk(1,"qbuf: requesting next field\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | field = videobuf_next_field(q); |
| 746 | retval = q->ops->buf_prepare(q,buf,field); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 747 | if (0 != retval) { |
| 748 | dprintk(1,"qbuf: buffer_prepare returned %d\n",retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 750 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
| 752 | list_add_tail(&buf->stream,&q->stream); |
| 753 | if (q->streaming) { |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 754 | if (q->irqlock) |
| 755 | spin_lock_irqsave(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | q->ops->buf_queue(q,buf); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 757 | if (q->irqlock) |
| 758 | spin_unlock_irqrestore(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 760 | dprintk(1,"qbuf: succeded\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | retval = 0; |
| 762 | |
| 763 | done: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 764 | mutex_unlock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | return retval; |
| 766 | } |
| 767 | |
| 768 | int |
| 769 | videobuf_dqbuf(struct videobuf_queue *q, |
| 770 | struct v4l2_buffer *b, int nonblocking) |
| 771 | { |
| 772 | struct videobuf_buffer *buf; |
| 773 | int retval; |
| 774 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 775 | mutex_lock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | retval = -EBUSY; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 777 | if (q->reading) { |
| 778 | dprintk(1,"dqbuf: Reading running...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 780 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | retval = -EINVAL; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 782 | if (b->type != q->type) { |
| 783 | dprintk(1,"dqbuf: Wrong type.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 785 | } |
| 786 | if (list_empty(&q->stream)) { |
| 787 | dprintk(1,"dqbuf: stream running\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 789 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | buf = list_entry(q->stream.next, struct videobuf_buffer, stream); |
| 791 | retval = videobuf_waiton(buf, nonblocking, 1); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 792 | if (retval < 0) { |
| 793 | dprintk(1,"dqbuf: waiton returned %d\n",retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 795 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | switch (buf->state) { |
| 797 | case STATE_ERROR: |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 798 | dprintk(1,"dqbuf: state is error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | retval = -EIO; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 800 | videobuf_dma_sync(q,&buf->dma); |
| 801 | buf->state = STATE_IDLE; |
| 802 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | case STATE_DONE: |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 804 | dprintk(1,"dqbuf: state is done\n"); |
| 805 | videobuf_dma_sync(q,&buf->dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | buf->state = STATE_IDLE; |
| 807 | break; |
| 808 | default: |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 809 | dprintk(1,"dqbuf: state invalid\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | retval = -EINVAL; |
| 811 | goto done; |
| 812 | } |
| 813 | list_del(&buf->stream); |
| 814 | memset(b,0,sizeof(*b)); |
| 815 | videobuf_status(b,buf,q->type); |
| 816 | |
| 817 | done: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 818 | mutex_unlock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | return retval; |
| 820 | } |
| 821 | |
| 822 | int videobuf_streamon(struct videobuf_queue *q) |
| 823 | { |
| 824 | struct videobuf_buffer *buf; |
| 825 | struct list_head *list; |
Mauro Carvalho Chehab | 9b89f2d | 2006-03-15 17:52:14 -0300 | [diff] [blame] | 826 | unsigned long flags=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | int retval; |
| 828 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 829 | mutex_lock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | retval = -EBUSY; |
| 831 | if (q->reading) |
| 832 | goto done; |
| 833 | retval = 0; |
| 834 | if (q->streaming) |
| 835 | goto done; |
| 836 | q->streaming = 1; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 837 | if (q->irqlock) |
| 838 | spin_lock_irqsave(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | list_for_each(list,&q->stream) { |
| 840 | buf = list_entry(list, struct videobuf_buffer, stream); |
| 841 | if (buf->state == STATE_PREPARED) |
| 842 | q->ops->buf_queue(q,buf); |
| 843 | } |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 844 | if (q->irqlock) |
| 845 | spin_unlock_irqrestore(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
| 847 | done: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 848 | mutex_unlock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | return retval; |
| 850 | } |
| 851 | |
| 852 | int videobuf_streamoff(struct videobuf_queue *q) |
| 853 | { |
| 854 | int retval = -EINVAL; |
| 855 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 856 | mutex_lock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | if (!q->streaming) |
| 858 | goto done; |
| 859 | videobuf_queue_cancel(q); |
| 860 | q->streaming = 0; |
| 861 | retval = 0; |
| 862 | |
| 863 | done: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 864 | mutex_unlock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | return retval; |
| 866 | } |
| 867 | |
| 868 | static ssize_t |
| 869 | videobuf_read_zerocopy(struct videobuf_queue *q, char __user *data, |
| 870 | size_t count, loff_t *ppos) |
| 871 | { |
| 872 | enum v4l2_field field; |
Mauro Carvalho Chehab | 9b89f2d | 2006-03-15 17:52:14 -0300 | [diff] [blame] | 873 | unsigned long flags=0; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 874 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 876 | /* setup stuff */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | q->read_buf = videobuf_alloc(q->msize); |
| 878 | if (NULL == q->read_buf) |
Nickolay V. Shmyrev | 50ab5ed | 2005-12-01 00:51:32 -0800 | [diff] [blame] | 879 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
| 881 | q->read_buf->memory = V4L2_MEMORY_USERPTR; |
| 882 | q->read_buf->baddr = (unsigned long)data; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 883 | q->read_buf->bsize = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | field = videobuf_next_field(q); |
| 885 | retval = q->ops->buf_prepare(q,q->read_buf,field); |
| 886 | if (0 != retval) |
| 887 | goto done; |
| 888 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 889 | /* start capture & wait */ |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 890 | if (q->irqlock) |
| 891 | spin_lock_irqsave(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | q->ops->buf_queue(q,q->read_buf); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 893 | if (q->irqlock) |
| 894 | spin_unlock_irqrestore(q->irqlock,flags); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 895 | retval = videobuf_waiton(q->read_buf,0,0); |
| 896 | if (0 == retval) { |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 897 | videobuf_dma_sync(q,&q->read_buf->dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | if (STATE_ERROR == q->read_buf->state) |
| 899 | retval = -EIO; |
| 900 | else |
| 901 | retval = q->read_buf->size; |
| 902 | } |
| 903 | |
| 904 | done: |
| 905 | /* cleanup */ |
| 906 | q->ops->buf_release(q,q->read_buf); |
| 907 | kfree(q->read_buf); |
| 908 | q->read_buf = NULL; |
| 909 | return retval; |
| 910 | } |
| 911 | |
| 912 | ssize_t videobuf_read_one(struct videobuf_queue *q, |
| 913 | char __user *data, size_t count, loff_t *ppos, |
| 914 | int nonblocking) |
| 915 | { |
| 916 | enum v4l2_field field; |
Mauro Carvalho Chehab | 9b89f2d | 2006-03-15 17:52:14 -0300 | [diff] [blame] | 917 | unsigned long flags=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | unsigned size, nbufs, bytes; |
| 919 | int retval; |
| 920 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 921 | mutex_lock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
| 923 | nbufs = 1; size = 0; |
| 924 | q->ops->buf_setup(q,&nbufs,&size); |
| 925 | if (NULL == q->read_buf && |
| 926 | count >= size && |
| 927 | !nonblocking) { |
| 928 | retval = videobuf_read_zerocopy(q,data,count,ppos); |
| 929 | if (retval >= 0 || retval == -EIO) |
| 930 | /* ok, all done */ |
| 931 | goto done; |
| 932 | /* fallback to kernel bounce buffer on failures */ |
| 933 | } |
| 934 | |
| 935 | if (NULL == q->read_buf) { |
| 936 | /* need to capture a new frame */ |
| 937 | retval = -ENOMEM; |
| 938 | q->read_buf = videobuf_alloc(q->msize); |
Mauro Carvalho Chehab | 4ae5c2e | 2006-03-25 15:53:38 -0300 | [diff] [blame] | 939 | dprintk(1,"video alloc=0x%p\n", q->read_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | if (NULL == q->read_buf) |
| 941 | goto done; |
| 942 | q->read_buf->memory = V4L2_MEMORY_USERPTR; |
Michael H. Schimek | 13c7280 | 2005-12-01 00:51:37 -0800 | [diff] [blame] | 943 | q->read_buf->bsize = count; /* preferred size */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | field = videobuf_next_field(q); |
| 945 | retval = q->ops->buf_prepare(q,q->read_buf,field); |
Nickolay V. Shmyrev | 50ab5ed | 2005-12-01 00:51:32 -0800 | [diff] [blame] | 946 | if (0 != retval) { |
| 947 | kfree (q->read_buf); |
| 948 | q->read_buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | goto done; |
Nickolay V. Shmyrev | 50ab5ed | 2005-12-01 00:51:32 -0800 | [diff] [blame] | 950 | } |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 951 | if (q->irqlock) |
| 952 | spin_lock_irqsave(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | q->ops->buf_queue(q,q->read_buf); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 954 | if (q->irqlock) |
| 955 | spin_unlock_irqrestore(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | q->read_off = 0; |
| 957 | } |
| 958 | |
| 959 | /* wait until capture is done */ |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 960 | retval = videobuf_waiton(q->read_buf, nonblocking, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | if (0 != retval) |
| 962 | goto done; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 963 | videobuf_dma_sync(q,&q->read_buf->dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | if (STATE_ERROR == q->read_buf->state) { |
| 966 | /* catch I/O errors */ |
| 967 | q->ops->buf_release(q,q->read_buf); |
| 968 | kfree(q->read_buf); |
| 969 | q->read_buf = NULL; |
| 970 | retval = -EIO; |
| 971 | goto done; |
| 972 | } |
| 973 | |
| 974 | /* copy to userspace */ |
| 975 | bytes = count; |
| 976 | if (bytes > q->read_buf->size - q->read_off) |
| 977 | bytes = q->read_buf->size - q->read_off; |
| 978 | retval = -EFAULT; |
| 979 | if (copy_to_user(data, q->read_buf->dma.vmalloc+q->read_off, bytes)) |
| 980 | goto done; |
| 981 | |
| 982 | retval = bytes; |
| 983 | q->read_off += bytes; |
| 984 | if (q->read_off == q->read_buf->size) { |
| 985 | /* all data copied, cleanup */ |
| 986 | q->ops->buf_release(q,q->read_buf); |
| 987 | kfree(q->read_buf); |
| 988 | q->read_buf = NULL; |
| 989 | } |
| 990 | |
| 991 | done: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 992 | mutex_unlock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | return retval; |
| 994 | } |
| 995 | |
| 996 | int videobuf_read_start(struct videobuf_queue *q) |
| 997 | { |
| 998 | enum v4l2_field field; |
Mauro Carvalho Chehab | 9b89f2d | 2006-03-15 17:52:14 -0300 | [diff] [blame] | 999 | unsigned long flags=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | int count = 0, size = 0; |
| 1001 | int err, i; |
| 1002 | |
| 1003 | q->ops->buf_setup(q,&count,&size); |
| 1004 | if (count < 2) |
| 1005 | count = 2; |
| 1006 | if (count > VIDEO_MAX_FRAME) |
| 1007 | count = VIDEO_MAX_FRAME; |
| 1008 | size = PAGE_ALIGN(size); |
| 1009 | |
| 1010 | err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR); |
| 1011 | if (err) |
| 1012 | return err; |
| 1013 | for (i = 0; i < count; i++) { |
| 1014 | field = videobuf_next_field(q); |
| 1015 | err = q->ops->buf_prepare(q,q->bufs[i],field); |
| 1016 | if (err) |
| 1017 | return err; |
| 1018 | list_add_tail(&q->bufs[i]->stream, &q->stream); |
| 1019 | } |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 1020 | if (q->irqlock) |
| 1021 | spin_lock_irqsave(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | for (i = 0; i < count; i++) |
| 1023 | q->ops->buf_queue(q,q->bufs[i]); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 1024 | if (q->irqlock) |
| 1025 | spin_unlock_irqrestore(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | q->reading = 1; |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | void videobuf_read_stop(struct videobuf_queue *q) |
| 1031 | { |
| 1032 | int i; |
| 1033 | |
| 1034 | videobuf_queue_cancel(q); |
| 1035 | videobuf_mmap_free(q); |
| 1036 | INIT_LIST_HEAD(&q->stream); |
| 1037 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 1038 | if (NULL == q->bufs[i]) |
| 1039 | continue; |
| 1040 | kfree(q->bufs[i]); |
| 1041 | q->bufs[i] = NULL; |
| 1042 | } |
| 1043 | q->read_buf = NULL; |
| 1044 | q->reading = 0; |
| 1045 | } |
| 1046 | |
| 1047 | ssize_t videobuf_read_stream(struct videobuf_queue *q, |
| 1048 | char __user *data, size_t count, loff_t *ppos, |
| 1049 | int vbihack, int nonblocking) |
| 1050 | { |
| 1051 | unsigned int *fc, bytes; |
| 1052 | int err, retval; |
Mauro Carvalho Chehab | 9b89f2d | 2006-03-15 17:52:14 -0300 | [diff] [blame] | 1053 | unsigned long flags=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
| 1055 | dprintk(2,"%s\n",__FUNCTION__); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1056 | mutex_lock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | retval = -EBUSY; |
| 1058 | if (q->streaming) |
| 1059 | goto done; |
| 1060 | if (!q->reading) { |
| 1061 | retval = videobuf_read_start(q); |
| 1062 | if (retval < 0) |
| 1063 | goto done; |
| 1064 | } |
| 1065 | |
| 1066 | retval = 0; |
| 1067 | while (count > 0) { |
| 1068 | /* get / wait for data */ |
| 1069 | if (NULL == q->read_buf) { |
| 1070 | q->read_buf = list_entry(q->stream.next, |
| 1071 | struct videobuf_buffer, |
| 1072 | stream); |
| 1073 | list_del(&q->read_buf->stream); |
| 1074 | q->read_off = 0; |
| 1075 | } |
| 1076 | err = videobuf_waiton(q->read_buf, nonblocking, 1); |
| 1077 | if (err < 0) { |
| 1078 | if (0 == retval) |
| 1079 | retval = err; |
| 1080 | break; |
| 1081 | } |
| 1082 | |
| 1083 | if (q->read_buf->state == STATE_DONE) { |
| 1084 | if (vbihack) { |
| 1085 | /* dirty, undocumented hack -- pass the frame counter |
| 1086 | * within the last four bytes of each vbi data block. |
| 1087 | * We need that one to maintain backward compatibility |
| 1088 | * to all vbi decoding software out there ... */ |
| 1089 | fc = (unsigned int*)q->read_buf->dma.vmalloc; |
| 1090 | fc += (q->read_buf->size>>2) -1; |
| 1091 | *fc = q->read_buf->field_count >> 1; |
| 1092 | dprintk(1,"vbihack: %d\n",*fc); |
| 1093 | } |
| 1094 | |
| 1095 | /* copy stuff */ |
| 1096 | bytes = count; |
| 1097 | if (bytes > q->read_buf->size - q->read_off) |
| 1098 | bytes = q->read_buf->size - q->read_off; |
| 1099 | if (copy_to_user(data + retval, |
| 1100 | q->read_buf->dma.vmalloc + q->read_off, |
| 1101 | bytes)) { |
| 1102 | if (0 == retval) |
| 1103 | retval = -EFAULT; |
| 1104 | break; |
| 1105 | } |
| 1106 | count -= bytes; |
| 1107 | retval += bytes; |
| 1108 | q->read_off += bytes; |
| 1109 | } else { |
| 1110 | /* some error */ |
| 1111 | q->read_off = q->read_buf->size; |
| 1112 | if (0 == retval) |
| 1113 | retval = -EIO; |
| 1114 | } |
| 1115 | |
| 1116 | /* requeue buffer when done with copying */ |
| 1117 | if (q->read_off == q->read_buf->size) { |
| 1118 | list_add_tail(&q->read_buf->stream, |
| 1119 | &q->stream); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 1120 | if (q->irqlock) |
| 1121 | spin_lock_irqsave(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | q->ops->buf_queue(q,q->read_buf); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 1123 | if (q->irqlock) |
| 1124 | spin_unlock_irqrestore(q->irqlock,flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | q->read_buf = NULL; |
| 1126 | } |
| 1127 | if (retval < 0) |
| 1128 | break; |
| 1129 | } |
| 1130 | |
| 1131 | done: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1132 | mutex_unlock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | return retval; |
| 1134 | } |
| 1135 | |
| 1136 | unsigned int videobuf_poll_stream(struct file *file, |
| 1137 | struct videobuf_queue *q, |
| 1138 | poll_table *wait) |
| 1139 | { |
| 1140 | struct videobuf_buffer *buf = NULL; |
| 1141 | unsigned int rc = 0; |
| 1142 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1143 | mutex_lock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | if (q->streaming) { |
| 1145 | if (!list_empty(&q->stream)) |
| 1146 | buf = list_entry(q->stream.next, |
| 1147 | struct videobuf_buffer, stream); |
| 1148 | } else { |
| 1149 | if (!q->reading) |
| 1150 | videobuf_read_start(q); |
| 1151 | if (!q->reading) { |
| 1152 | rc = POLLERR; |
| 1153 | } else if (NULL == q->read_buf) { |
| 1154 | q->read_buf = list_entry(q->stream.next, |
| 1155 | struct videobuf_buffer, |
| 1156 | stream); |
| 1157 | list_del(&q->read_buf->stream); |
| 1158 | q->read_off = 0; |
| 1159 | } |
| 1160 | buf = q->read_buf; |
| 1161 | } |
| 1162 | if (!buf) |
| 1163 | rc = POLLERR; |
| 1164 | |
| 1165 | if (0 == rc) { |
| 1166 | poll_wait(file, &buf->done, wait); |
| 1167 | if (buf->state == STATE_DONE || |
| 1168 | buf->state == STATE_ERROR) |
| 1169 | rc = POLLIN|POLLRDNORM; |
| 1170 | } |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1171 | mutex_unlock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | return rc; |
| 1173 | } |
| 1174 | |
| 1175 | /* --------------------------------------------------------------------- */ |
| 1176 | |
| 1177 | static void |
| 1178 | videobuf_vm_open(struct vm_area_struct *vma) |
| 1179 | { |
| 1180 | struct videobuf_mapping *map = vma->vm_private_data; |
| 1181 | |
| 1182 | dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map, |
| 1183 | map->count,vma->vm_start,vma->vm_end); |
| 1184 | map->count++; |
| 1185 | } |
| 1186 | |
| 1187 | static void |
| 1188 | videobuf_vm_close(struct vm_area_struct *vma) |
| 1189 | { |
| 1190 | struct videobuf_mapping *map = vma->vm_private_data; |
| 1191 | struct videobuf_queue *q = map->q; |
| 1192 | int i; |
| 1193 | |
| 1194 | dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map, |
| 1195 | map->count,vma->vm_start,vma->vm_end); |
| 1196 | |
| 1197 | map->count--; |
| 1198 | if (0 == map->count) { |
| 1199 | dprintk(1,"munmap %p q=%p\n",map,q); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1200 | mutex_lock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 1202 | if (NULL == q->bufs[i]) |
| 1203 | continue; |
| 1204 | if (q->bufs[i]) |
| 1205 | ; |
| 1206 | if (q->bufs[i]->map != map) |
| 1207 | continue; |
| 1208 | q->bufs[i]->map = NULL; |
| 1209 | q->bufs[i]->baddr = 0; |
| 1210 | q->ops->buf_release(q,q->bufs[i]); |
| 1211 | } |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1212 | mutex_unlock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | kfree(map); |
| 1214 | } |
| 1215 | return; |
| 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | * Get a anonymous page for the mapping. Make sure we can DMA to that |
| 1220 | * memory location with 32bit PCI devices (i.e. don't use highmem for |
| 1221 | * now ...). Bounce buffers don't work very well for the data rates |
| 1222 | * video capture has. |
| 1223 | */ |
| 1224 | static struct page* |
| 1225 | videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr, |
| 1226 | int *type) |
| 1227 | { |
| 1228 | struct page *page; |
| 1229 | |
| 1230 | dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n", |
| 1231 | vaddr,vma->vm_start,vma->vm_end); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 1232 | if (vaddr > vma->vm_end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | return NOPAGE_SIGBUS; |
Gerd Hoffmann | 10329b9 | 2007-01-12 21:26:32 -0300 | [diff] [blame] | 1234 | page = alloc_page(GFP_USER | __GFP_DMA32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | if (!page) |
| 1236 | return NOPAGE_OOM; |
| 1237 | clear_user_page(page_address(page), vaddr, page); |
| 1238 | if (type) |
| 1239 | *type = VM_FAULT_MINOR; |
| 1240 | return page; |
| 1241 | } |
| 1242 | |
| 1243 | static struct vm_operations_struct videobuf_vm_ops = |
| 1244 | { |
| 1245 | .open = videobuf_vm_open, |
| 1246 | .close = videobuf_vm_close, |
| 1247 | .nopage = videobuf_vm_nopage, |
| 1248 | }; |
| 1249 | |
| 1250 | int videobuf_mmap_setup(struct videobuf_queue *q, |
| 1251 | unsigned int bcount, unsigned int bsize, |
| 1252 | enum v4l2_memory memory) |
| 1253 | { |
| 1254 | unsigned int i; |
| 1255 | int err; |
| 1256 | |
| 1257 | err = videobuf_mmap_free(q); |
| 1258 | if (0 != err) |
| 1259 | return err; |
| 1260 | |
| 1261 | for (i = 0; i < bcount; i++) { |
| 1262 | q->bufs[i] = videobuf_alloc(q->msize); |
| 1263 | q->bufs[i]->i = i; |
| 1264 | q->bufs[i]->input = UNSET; |
| 1265 | q->bufs[i]->memory = memory; |
| 1266 | q->bufs[i]->bsize = bsize; |
| 1267 | switch (memory) { |
| 1268 | case V4L2_MEMORY_MMAP: |
| 1269 | q->bufs[i]->boff = bsize * i; |
| 1270 | break; |
| 1271 | case V4L2_MEMORY_USERPTR: |
| 1272 | case V4L2_MEMORY_OVERLAY: |
| 1273 | /* nothing */ |
| 1274 | break; |
| 1275 | } |
| 1276 | } |
| 1277 | dprintk(1,"mmap setup: %d buffers, %d bytes each\n", |
| 1278 | bcount,bsize); |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
| 1282 | int videobuf_mmap_free(struct videobuf_queue *q) |
| 1283 | { |
| 1284 | int i; |
| 1285 | |
| 1286 | for (i = 0; i < VIDEO_MAX_FRAME; i++) |
| 1287 | if (q->bufs[i] && q->bufs[i]->map) |
| 1288 | return -EBUSY; |
| 1289 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 1290 | if (NULL == q->bufs[i]) |
| 1291 | continue; |
| 1292 | q->ops->buf_release(q,q->bufs[i]); |
| 1293 | kfree(q->bufs[i]); |
| 1294 | q->bufs[i] = NULL; |
| 1295 | } |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
| 1299 | int videobuf_mmap_mapper(struct videobuf_queue *q, |
| 1300 | struct vm_area_struct *vma) |
| 1301 | { |
| 1302 | struct videobuf_mapping *map; |
| 1303 | unsigned int first,last,size,i; |
| 1304 | int retval; |
| 1305 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1306 | mutex_lock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | retval = -EINVAL; |
| 1308 | if (!(vma->vm_flags & VM_WRITE)) { |
| 1309 | dprintk(1,"mmap app bug: PROT_WRITE please\n"); |
| 1310 | goto done; |
| 1311 | } |
| 1312 | if (!(vma->vm_flags & VM_SHARED)) { |
| 1313 | dprintk(1,"mmap app bug: MAP_SHARED please\n"); |
| 1314 | goto done; |
| 1315 | } |
| 1316 | |
| 1317 | /* look for first buffer to map */ |
| 1318 | for (first = 0; first < VIDEO_MAX_FRAME; first++) { |
| 1319 | if (NULL == q->bufs[first]) |
| 1320 | continue; |
| 1321 | if (V4L2_MEMORY_MMAP != q->bufs[first]->memory) |
| 1322 | continue; |
| 1323 | if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT)) |
| 1324 | break; |
| 1325 | } |
| 1326 | if (VIDEO_MAX_FRAME == first) { |
| 1327 | dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n", |
| 1328 | (vma->vm_pgoff << PAGE_SHIFT)); |
| 1329 | goto done; |
| 1330 | } |
| 1331 | |
| 1332 | /* look for last buffer to map */ |
| 1333 | for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) { |
| 1334 | if (NULL == q->bufs[last]) |
| 1335 | continue; |
| 1336 | if (V4L2_MEMORY_MMAP != q->bufs[last]->memory) |
| 1337 | continue; |
| 1338 | if (q->bufs[last]->map) { |
| 1339 | retval = -EBUSY; |
| 1340 | goto done; |
| 1341 | } |
| 1342 | size += q->bufs[last]->bsize; |
| 1343 | if (size == (vma->vm_end - vma->vm_start)) |
| 1344 | break; |
| 1345 | } |
| 1346 | if (VIDEO_MAX_FRAME == last) { |
| 1347 | dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n", |
| 1348 | (vma->vm_end - vma->vm_start)); |
| 1349 | goto done; |
| 1350 | } |
| 1351 | |
| 1352 | /* create mapping + update buffer list */ |
| 1353 | retval = -ENOMEM; |
| 1354 | map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL); |
| 1355 | if (NULL == map) |
| 1356 | goto done; |
| 1357 | for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) { |
| 1358 | q->bufs[i]->map = map; |
| 1359 | q->bufs[i]->baddr = vma->vm_start + size; |
| 1360 | } |
| 1361 | map->count = 1; |
| 1362 | map->start = vma->vm_start; |
| 1363 | map->end = vma->vm_end; |
| 1364 | map->q = q; |
| 1365 | vma->vm_ops = &videobuf_vm_ops; |
| 1366 | vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED; |
| 1367 | vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */ |
| 1368 | vma->vm_private_data = map; |
| 1369 | dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n", |
| 1370 | map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last); |
| 1371 | retval = 0; |
| 1372 | |
| 1373 | done: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1374 | mutex_unlock(&q->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | return retval; |
| 1376 | } |
| 1377 | |
| 1378 | /* --------------------------------------------------------------------- */ |
| 1379 | |
| 1380 | EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg); |
| 1381 | |
| 1382 | EXPORT_SYMBOL_GPL(videobuf_dma_init); |
| 1383 | EXPORT_SYMBOL_GPL(videobuf_dma_init_user); |
| 1384 | EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel); |
| 1385 | EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay); |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 1386 | EXPORT_SYMBOL_GPL(videobuf_dma_map); |
| 1387 | EXPORT_SYMBOL_GPL(videobuf_dma_sync); |
| 1388 | EXPORT_SYMBOL_GPL(videobuf_dma_unmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | EXPORT_SYMBOL_GPL(videobuf_dma_free); |
| 1390 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 1391 | EXPORT_SYMBOL_GPL(videobuf_pci_dma_map); |
| 1392 | EXPORT_SYMBOL_GPL(videobuf_pci_dma_unmap); |
| 1393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | EXPORT_SYMBOL_GPL(videobuf_alloc); |
| 1395 | EXPORT_SYMBOL_GPL(videobuf_waiton); |
| 1396 | EXPORT_SYMBOL_GPL(videobuf_iolock); |
| 1397 | |
| 1398 | EXPORT_SYMBOL_GPL(videobuf_queue_init); |
| 1399 | EXPORT_SYMBOL_GPL(videobuf_queue_cancel); |
| 1400 | EXPORT_SYMBOL_GPL(videobuf_queue_is_busy); |
| 1401 | |
| 1402 | EXPORT_SYMBOL_GPL(videobuf_next_field); |
| 1403 | EXPORT_SYMBOL_GPL(videobuf_status); |
| 1404 | EXPORT_SYMBOL_GPL(videobuf_reqbufs); |
| 1405 | EXPORT_SYMBOL_GPL(videobuf_querybuf); |
| 1406 | EXPORT_SYMBOL_GPL(videobuf_qbuf); |
| 1407 | EXPORT_SYMBOL_GPL(videobuf_dqbuf); |
| 1408 | EXPORT_SYMBOL_GPL(videobuf_streamon); |
| 1409 | EXPORT_SYMBOL_GPL(videobuf_streamoff); |
| 1410 | |
| 1411 | EXPORT_SYMBOL_GPL(videobuf_read_start); |
| 1412 | EXPORT_SYMBOL_GPL(videobuf_read_stop); |
| 1413 | EXPORT_SYMBOL_GPL(videobuf_read_stream); |
| 1414 | EXPORT_SYMBOL_GPL(videobuf_read_one); |
| 1415 | EXPORT_SYMBOL_GPL(videobuf_poll_stream); |
| 1416 | |
| 1417 | EXPORT_SYMBOL_GPL(videobuf_mmap_setup); |
| 1418 | EXPORT_SYMBOL_GPL(videobuf_mmap_free); |
| 1419 | EXPORT_SYMBOL_GPL(videobuf_mmap_mapper); |
| 1420 | |
| 1421 | /* |
| 1422 | * Local variables: |
| 1423 | * c-basic-offset: 8 |
| 1424 | * End: |
| 1425 | */ |