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