Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1 | /* |
| 2 | * helper functions for PCI DMA video4linux capture buffers |
| 3 | * |
| 4 | * The functions expect the hardware being able to scatter gatter |
| 5 | * (i.e. the buffers are not linear in physical memory, but fragmented |
| 6 | * into PAGE_SIZE chunks). They also assume the driver does not need |
| 7 | * to touch the video data. |
| 8 | * |
| 9 | * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org> |
| 10 | * |
| 11 | * Highly based on video-buf written originally by: |
| 12 | * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> |
| 13 | * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org> |
| 14 | * (c) 2006 Ted Walther and John Sokol |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 |
| 19 | */ |
| 20 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/moduleparam.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | |
| 27 | #include <linux/pci.h> |
| 28 | #include <linux/vmalloc.h> |
| 29 | #include <linux/pagemap.h> |
| 30 | #include <asm/page.h> |
| 31 | #include <asm/pgtable.h> |
| 32 | |
| 33 | #include <media/videobuf-dma-sg.h> |
| 34 | |
| 35 | #define MAGIC_DMABUF 0x19721112 |
| 36 | #define MAGIC_SG_MEM 0x17890714 |
| 37 | |
| 38 | #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \ |
| 39 | { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); } |
| 40 | |
| 41 | static int debug = 0; |
| 42 | module_param(debug, int, 0644); |
| 43 | |
| 44 | MODULE_DESCRIPTION("helper module to manage video4linux pci dma sg buffers"); |
| 45 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); |
| 46 | MODULE_LICENSE("GPL"); |
| 47 | |
| 48 | #define dprintk(level, fmt, arg...) if (debug >= level) \ |
| 49 | printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg) |
| 50 | |
| 51 | /* --------------------------------------------------------------------- */ |
| 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 | |
| 60 | sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL); |
| 61 | if (NULL == sglist) |
| 62 | return NULL; |
| 63 | for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) { |
| 64 | pg = vmalloc_to_page(virt); |
| 65 | if (NULL == pg) |
| 66 | goto err; |
| 67 | BUG_ON(PageHighMem(pg)); |
| 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; |
| 86 | sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL); |
| 87 | if (NULL == sglist) |
| 88 | return NULL; |
| 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 | struct videobuf_dmabuf *videobuf_to_dma (struct videobuf_buffer *buf) |
| 122 | { |
| 123 | struct videbuf_pci_sg_memory *mem=buf->priv; |
| 124 | BUG_ON (!mem); |
| 125 | |
| 126 | MAGIC_CHECK(mem->magic,MAGIC_SG_MEM); |
| 127 | |
| 128 | return &mem->dma; |
| 129 | } |
| 130 | |
| 131 | void videobuf_dma_init(struct videobuf_dmabuf *dma) |
| 132 | { |
| 133 | memset(dma,0,sizeof(*dma)); |
| 134 | dma->magic = MAGIC_DMABUF; |
| 135 | } |
| 136 | |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 137 | static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma, |
| 138 | int direction, unsigned long data, unsigned long size) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 139 | { |
| 140 | unsigned long first,last; |
| 141 | int err, rw = 0; |
| 142 | |
| 143 | dma->direction = direction; |
| 144 | switch (dma->direction) { |
| 145 | case PCI_DMA_FROMDEVICE: rw = READ; break; |
| 146 | case PCI_DMA_TODEVICE: rw = WRITE; break; |
| 147 | default: BUG(); |
| 148 | } |
| 149 | |
| 150 | first = (data & PAGE_MASK) >> PAGE_SHIFT; |
| 151 | last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT; |
| 152 | dma->offset = data & ~PAGE_MASK; |
| 153 | dma->nr_pages = last-first+1; |
| 154 | dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*), |
| 155 | GFP_KERNEL); |
| 156 | if (NULL == dma->pages) |
| 157 | return -ENOMEM; |
| 158 | dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n", |
| 159 | data,size,dma->nr_pages); |
| 160 | |
| 161 | dma->varea = (void *) data; |
| 162 | |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 163 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 164 | err = get_user_pages(current,current->mm, |
| 165 | data & PAGE_MASK, dma->nr_pages, |
| 166 | rw == READ, 1, /* force */ |
| 167 | dma->pages, NULL); |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 168 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 169 | if (err != dma->nr_pages) { |
| 170 | dma->nr_pages = (err >= 0) ? err : 0; |
| 171 | dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages); |
| 172 | return err < 0 ? err : -EINVAL; |
| 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 177 | int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction, |
| 178 | unsigned long data, unsigned long size) |
| 179 | { |
| 180 | int ret; |
| 181 | down_read(¤t->mm->mmap_sem); |
| 182 | ret = videobuf_dma_init_user_locked(dma, direction, data, size); |
| 183 | up_read(¤t->mm->mmap_sem); |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 188 | int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction, |
| 189 | int nr_pages) |
| 190 | { |
| 191 | dprintk(1,"init kernel [%d pages]\n",nr_pages); |
| 192 | dma->direction = direction; |
| 193 | dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT); |
| 194 | if (NULL == dma->vmalloc) { |
| 195 | dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages); |
| 196 | return -ENOMEM; |
| 197 | } |
| 198 | dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n", |
| 199 | (unsigned long)dma->vmalloc, |
| 200 | nr_pages << PAGE_SHIFT); |
| 201 | memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT); |
| 202 | dma->nr_pages = nr_pages; |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction, |
| 207 | dma_addr_t addr, int nr_pages) |
| 208 | { |
| 209 | dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n", |
| 210 | nr_pages,(unsigned long)addr); |
| 211 | dma->direction = direction; |
| 212 | if (0 == addr) |
| 213 | return -EINVAL; |
| 214 | |
| 215 | dma->bus_addr = addr; |
| 216 | dma->nr_pages = nr_pages; |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma) |
| 221 | { |
| 222 | void *dev=q->dev; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 223 | |
| 224 | MAGIC_CHECK(dma->magic,MAGIC_DMABUF); |
| 225 | BUG_ON(0 == dma->nr_pages); |
| 226 | |
| 227 | if (dma->pages) { |
| 228 | dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages, |
| 229 | dma->offset); |
| 230 | } |
| 231 | if (dma->vmalloc) { |
| 232 | dma->sglist = videobuf_vmalloc_to_sg |
| 233 | (dma->vmalloc,dma->nr_pages); |
| 234 | } |
| 235 | if (dma->bus_addr) { |
| 236 | dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL); |
| 237 | if (NULL != dma->sglist) { |
| 238 | dma->sglen = 1; |
| 239 | sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK; |
| 240 | dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK; |
| 241 | sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE; |
| 242 | } |
| 243 | } |
| 244 | if (NULL == dma->sglist) { |
| 245 | dprintk(1,"scatterlist is NULL\n"); |
| 246 | return -ENOMEM; |
| 247 | } |
| 248 | if (!dma->bus_addr) { |
Mauro Carvalho Chehab | 5ddff43 | 2007-10-08 11:43:49 -0300 | [diff] [blame] | 249 | dma->sglen = pci_map_sg(dev,dma->sglist, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 250 | dma->nr_pages, dma->direction); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 251 | if (0 == dma->sglen) { |
| 252 | printk(KERN_WARNING |
| 253 | "%s: videobuf_map_sg failed\n",__FUNCTION__); |
| 254 | kfree(dma->sglist); |
| 255 | dma->sglist = NULL; |
| 256 | dma->sglen = 0; |
| 257 | return -EIO; |
| 258 | } |
| 259 | } |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | int videobuf_dma_sync(struct videobuf_queue *q,struct videobuf_dmabuf *dma) |
| 264 | { |
| 265 | void *dev=q->dev; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 266 | |
| 267 | MAGIC_CHECK(dma->magic,MAGIC_DMABUF); |
| 268 | BUG_ON(!dma->sglen); |
| 269 | |
Mauro Carvalho Chehab | 5ddff43 | 2007-10-08 11:43:49 -0300 | [diff] [blame] | 270 | pci_dma_sync_sg_for_cpu (dev,dma->sglist,dma->nr_pages,dma->direction); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma) |
| 275 | { |
| 276 | void *dev=q->dev; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 277 | |
| 278 | MAGIC_CHECK(dma->magic,MAGIC_DMABUF); |
| 279 | if (!dma->sglen) |
| 280 | return 0; |
| 281 | |
Mauro Carvalho Chehab | 5ddff43 | 2007-10-08 11:43:49 -0300 | [diff] [blame] | 282 | pci_unmap_sg (dev,dma->sglist,dma->nr_pages,dma->direction); |
| 283 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 284 | kfree(dma->sglist); |
| 285 | dma->sglist = NULL; |
| 286 | dma->sglen = 0; |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | int videobuf_dma_free(struct videobuf_dmabuf *dma) |
| 291 | { |
| 292 | MAGIC_CHECK(dma->magic,MAGIC_DMABUF); |
| 293 | BUG_ON(dma->sglen); |
| 294 | |
| 295 | if (dma->pages) { |
| 296 | int i; |
| 297 | for (i=0; i < dma->nr_pages; i++) |
| 298 | page_cache_release(dma->pages[i]); |
| 299 | kfree(dma->pages); |
| 300 | dma->pages = NULL; |
| 301 | } |
| 302 | |
| 303 | vfree(dma->vmalloc); |
| 304 | dma->vmalloc = NULL; |
| 305 | dma->varea = NULL; |
| 306 | |
| 307 | if (dma->bus_addr) { |
| 308 | dma->bus_addr = 0; |
| 309 | } |
| 310 | dma->direction = PCI_DMA_NONE; |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /* --------------------------------------------------------------------- */ |
| 315 | |
| 316 | int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma) |
| 317 | { |
| 318 | struct videobuf_queue q; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 319 | |
| 320 | q.dev=pci; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 321 | |
| 322 | return (videobuf_dma_map(&q,dma)); |
| 323 | } |
| 324 | |
| 325 | int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma) |
| 326 | { |
| 327 | struct videobuf_queue q; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 328 | |
| 329 | q.dev=pci; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 330 | |
| 331 | return (videobuf_dma_unmap(&q,dma)); |
| 332 | } |
| 333 | |
| 334 | /* --------------------------------------------------------------------- */ |
| 335 | |
| 336 | static void |
| 337 | videobuf_vm_open(struct vm_area_struct *vma) |
| 338 | { |
| 339 | struct videobuf_mapping *map = vma->vm_private_data; |
| 340 | |
| 341 | dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map, |
| 342 | map->count,vma->vm_start,vma->vm_end); |
| 343 | map->count++; |
| 344 | } |
| 345 | |
| 346 | static void |
| 347 | videobuf_vm_close(struct vm_area_struct *vma) |
| 348 | { |
| 349 | struct videobuf_mapping *map = vma->vm_private_data; |
| 350 | struct videobuf_queue *q = map->q; |
| 351 | struct videbuf_pci_sg_memory *mem; |
| 352 | int i; |
| 353 | |
| 354 | dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map, |
| 355 | map->count,vma->vm_start,vma->vm_end); |
| 356 | |
| 357 | map->count--; |
| 358 | if (0 == map->count) { |
| 359 | dprintk(1,"munmap %p q=%p\n",map,q); |
| 360 | mutex_lock(&q->lock); |
| 361 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 362 | if (NULL == q->bufs[i]) |
| 363 | continue; |
| 364 | mem=q->bufs[i]->priv; |
| 365 | |
| 366 | if (!mem) |
| 367 | continue; |
| 368 | |
| 369 | MAGIC_CHECK(mem->magic,MAGIC_SG_MEM); |
| 370 | |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 371 | if (q->bufs[i]->map != map) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 372 | continue; |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 373 | q->bufs[i]->map = NULL; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 374 | q->bufs[i]->baddr = 0; |
| 375 | q->ops->buf_release(q,q->bufs[i]); |
| 376 | } |
| 377 | mutex_unlock(&q->lock); |
| 378 | kfree(map); |
| 379 | } |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Get a anonymous page for the mapping. Make sure we can DMA to that |
| 385 | * memory location with 32bit PCI devices (i.e. don't use highmem for |
| 386 | * now ...). Bounce buffers don't work very well for the data rates |
| 387 | * video capture has. |
| 388 | */ |
| 389 | static struct page* |
| 390 | videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr, |
| 391 | int *type) |
| 392 | { |
| 393 | struct page *page; |
| 394 | |
| 395 | dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n", |
| 396 | vaddr,vma->vm_start,vma->vm_end); |
| 397 | if (vaddr > vma->vm_end) |
| 398 | return NOPAGE_SIGBUS; |
| 399 | page = alloc_page(GFP_USER | __GFP_DMA32); |
| 400 | if (!page) |
| 401 | return NOPAGE_OOM; |
| 402 | clear_user_page(page_address(page), vaddr, page); |
| 403 | if (type) |
| 404 | *type = VM_FAULT_MINOR; |
| 405 | return page; |
| 406 | } |
| 407 | |
| 408 | static struct vm_operations_struct videobuf_vm_ops = |
| 409 | { |
| 410 | .open = videobuf_vm_open, |
| 411 | .close = videobuf_vm_close, |
| 412 | .nopage = videobuf_vm_nopage, |
| 413 | }; |
| 414 | |
| 415 | /* --------------------------------------------------------------------- |
| 416 | * PCI handlers for the generic methods |
| 417 | */ |
| 418 | |
| 419 | /* Allocated area consists on 3 parts: |
| 420 | struct video_buffer |
| 421 | struct <driver>_buffer (cx88_buffer, saa7134_buf, ...) |
| 422 | struct videobuf_pci_sg_memory |
| 423 | */ |
| 424 | |
| 425 | static void *__videobuf_alloc(size_t size) |
| 426 | { |
| 427 | struct videbuf_pci_sg_memory *mem; |
| 428 | struct videobuf_buffer *vb; |
| 429 | |
| 430 | vb = kzalloc(size+sizeof(*mem),GFP_KERNEL); |
| 431 | |
| 432 | mem = vb->priv = ((char *)vb)+size; |
| 433 | mem->magic=MAGIC_SG_MEM; |
| 434 | |
| 435 | videobuf_dma_init(&mem->dma); |
| 436 | |
| 437 | dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n", |
| 438 | __FUNCTION__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb), |
| 439 | mem,(long)sizeof(*mem)); |
| 440 | |
| 441 | return vb; |
| 442 | } |
| 443 | |
| 444 | static int __videobuf_iolock (struct videobuf_queue* q, |
| 445 | struct videobuf_buffer *vb, |
| 446 | struct v4l2_framebuffer *fbuf) |
| 447 | { |
| 448 | int err,pages; |
| 449 | dma_addr_t bus; |
| 450 | struct videbuf_pci_sg_memory *mem=vb->priv; |
| 451 | BUG_ON(!mem); |
| 452 | |
| 453 | MAGIC_CHECK(mem->magic,MAGIC_SG_MEM); |
| 454 | |
| 455 | switch (vb->memory) { |
| 456 | case V4L2_MEMORY_MMAP: |
| 457 | case V4L2_MEMORY_USERPTR: |
| 458 | if (0 == vb->baddr) { |
| 459 | /* no userspace addr -- kernel bounce buffer */ |
| 460 | pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; |
| 461 | err = videobuf_dma_init_kernel( &mem->dma, |
| 462 | PCI_DMA_FROMDEVICE, |
| 463 | pages ); |
| 464 | if (0 != err) |
| 465 | return err; |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 466 | } else if (vb->memory == V4L2_MEMORY_USERPTR) { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 467 | /* dma directly to userspace */ |
| 468 | err = videobuf_dma_init_user( &mem->dma, |
| 469 | PCI_DMA_FROMDEVICE, |
| 470 | vb->baddr,vb->bsize ); |
| 471 | if (0 != err) |
| 472 | return err; |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 473 | } else { |
| 474 | /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP |
| 475 | buffers can only be called from videobuf_qbuf |
| 476 | we take current->mm->mmap_sem there, to prevent |
| 477 | locking inversion, so don't take it here */ |
| 478 | |
| 479 | err = videobuf_dma_init_user_locked(&mem->dma, |
| 480 | PCI_DMA_FROMDEVICE, |
| 481 | vb->baddr, vb->bsize); |
| 482 | if (0 != err) |
| 483 | return err; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 484 | } |
| 485 | break; |
| 486 | case V4L2_MEMORY_OVERLAY: |
| 487 | if (NULL == fbuf) |
| 488 | return -EINVAL; |
| 489 | /* FIXME: need sanity checks for vb->boff */ |
| 490 | /* |
| 491 | * Using a double cast to avoid compiler warnings when |
| 492 | * building for PAE. Compiler doesn't like direct casting |
| 493 | * of a 32 bit ptr to 64 bit integer. |
| 494 | */ |
| 495 | bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff; |
| 496 | pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; |
| 497 | err = videobuf_dma_init_overlay(&mem->dma,PCI_DMA_FROMDEVICE, |
| 498 | bus, pages); |
| 499 | if (0 != err) |
| 500 | return err; |
| 501 | break; |
| 502 | default: |
| 503 | BUG(); |
| 504 | } |
| 505 | err = videobuf_dma_map(q,&mem->dma); |
| 506 | if (0 != err) |
| 507 | return err; |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int __videobuf_sync(struct videobuf_queue *q, |
| 513 | struct videobuf_buffer *buf) |
| 514 | { |
| 515 | struct videbuf_pci_sg_memory *mem=buf->priv; |
| 516 | BUG_ON (!mem); |
| 517 | MAGIC_CHECK(mem->magic,MAGIC_SG_MEM); |
| 518 | |
| 519 | return videobuf_dma_sync(q,&mem->dma); |
| 520 | } |
| 521 | |
| 522 | static int __videobuf_mmap_free(struct videobuf_queue *q) |
| 523 | { |
| 524 | int i; |
| 525 | |
| 526 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 527 | if (q->bufs[i]) { |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 528 | if (q->bufs[i]->map) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 529 | return -EBUSY; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static int __videobuf_mmap_mapper(struct videobuf_queue *q, |
| 537 | struct vm_area_struct *vma) |
| 538 | { |
| 539 | struct videbuf_pci_sg_memory *mem; |
| 540 | struct videobuf_mapping *map; |
| 541 | unsigned int first,last,size,i; |
| 542 | int retval; |
| 543 | |
| 544 | retval = -EINVAL; |
| 545 | if (!(vma->vm_flags & VM_WRITE)) { |
| 546 | dprintk(1,"mmap app bug: PROT_WRITE please\n"); |
| 547 | goto done; |
| 548 | } |
| 549 | if (!(vma->vm_flags & VM_SHARED)) { |
| 550 | dprintk(1,"mmap app bug: MAP_SHARED please\n"); |
| 551 | goto done; |
| 552 | } |
| 553 | |
| 554 | /* look for first buffer to map */ |
| 555 | for (first = 0; first < VIDEO_MAX_FRAME; first++) { |
| 556 | if (NULL == q->bufs[first]) |
| 557 | continue; |
| 558 | mem=q->bufs[first]->priv; |
| 559 | BUG_ON (!mem); |
| 560 | MAGIC_CHECK(mem->magic,MAGIC_SG_MEM); |
| 561 | |
| 562 | if (V4L2_MEMORY_MMAP != q->bufs[first]->memory) |
| 563 | continue; |
| 564 | if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT)) |
| 565 | break; |
| 566 | } |
| 567 | if (VIDEO_MAX_FRAME == first) { |
| 568 | dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n", |
| 569 | (vma->vm_pgoff << PAGE_SHIFT)); |
| 570 | goto done; |
| 571 | } |
| 572 | |
| 573 | /* look for last buffer to map */ |
| 574 | for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) { |
| 575 | if (NULL == q->bufs[last]) |
| 576 | continue; |
| 577 | if (V4L2_MEMORY_MMAP != q->bufs[last]->memory) |
| 578 | continue; |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 579 | if (q->bufs[last]->map) { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 580 | retval = -EBUSY; |
| 581 | goto done; |
| 582 | } |
| 583 | size += q->bufs[last]->bsize; |
| 584 | if (size == (vma->vm_end - vma->vm_start)) |
| 585 | break; |
| 586 | } |
| 587 | if (VIDEO_MAX_FRAME == last) { |
| 588 | dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n", |
| 589 | (vma->vm_end - vma->vm_start)); |
| 590 | goto done; |
| 591 | } |
| 592 | |
| 593 | /* create mapping + update buffer list */ |
| 594 | retval = -ENOMEM; |
| 595 | map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL); |
| 596 | if (NULL == map) |
| 597 | goto done; |
| 598 | for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) { |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 599 | q->bufs[i]->map = map; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 600 | q->bufs[i]->baddr = vma->vm_start + size; |
| 601 | } |
| 602 | map->count = 1; |
| 603 | map->start = vma->vm_start; |
| 604 | map->end = vma->vm_end; |
| 605 | map->q = q; |
| 606 | vma->vm_ops = &videobuf_vm_ops; |
| 607 | vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED; |
| 608 | vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */ |
| 609 | vma->vm_private_data = map; |
| 610 | dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n", |
| 611 | map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last); |
| 612 | retval = 0; |
| 613 | |
| 614 | done: |
| 615 | return retval; |
| 616 | } |
| 617 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 618 | static int __videobuf_copy_to_user ( struct videobuf_queue *q, |
| 619 | char __user *data, size_t count, |
| 620 | int nonblocking ) |
| 621 | { |
| 622 | struct videbuf_pci_sg_memory *mem=q->read_buf->priv; |
| 623 | BUG_ON (!mem); |
| 624 | MAGIC_CHECK(mem->magic,MAGIC_SG_MEM); |
| 625 | |
| 626 | /* copy to userspace */ |
| 627 | if (count > q->read_buf->size - q->read_off) |
| 628 | count = q->read_buf->size - q->read_off; |
| 629 | |
| 630 | if (copy_to_user(data, mem->dma.vmalloc+q->read_off, count)) |
| 631 | return -EFAULT; |
| 632 | |
| 633 | return count; |
| 634 | } |
| 635 | |
| 636 | static int __videobuf_copy_stream ( struct videobuf_queue *q, |
| 637 | char __user *data, size_t count, size_t pos, |
| 638 | int vbihack, int nonblocking ) |
| 639 | { |
| 640 | unsigned int *fc; |
| 641 | struct videbuf_pci_sg_memory *mem=q->read_buf->priv; |
| 642 | BUG_ON (!mem); |
| 643 | MAGIC_CHECK(mem->magic,MAGIC_SG_MEM); |
| 644 | |
| 645 | if (vbihack) { |
| 646 | /* dirty, undocumented hack -- pass the frame counter |
| 647 | * within the last four bytes of each vbi data block. |
| 648 | * We need that one to maintain backward compatibility |
| 649 | * to all vbi decoding software out there ... */ |
| 650 | fc = (unsigned int*)mem->dma.vmalloc; |
| 651 | fc += (q->read_buf->size>>2) -1; |
| 652 | *fc = q->read_buf->field_count >> 1; |
| 653 | dprintk(1,"vbihack: %d\n",*fc); |
| 654 | } |
| 655 | |
| 656 | /* copy stuff using the common method */ |
| 657 | count = __videobuf_copy_to_user (q,data,count,nonblocking); |
| 658 | |
| 659 | if ( (count==-EFAULT) && (0 == pos) ) |
| 660 | return -EFAULT; |
| 661 | |
| 662 | return count; |
| 663 | } |
| 664 | |
| 665 | static struct videobuf_qtype_ops pci_ops = { |
| 666 | .magic = MAGIC_QTYPE_OPS, |
| 667 | |
| 668 | .alloc = __videobuf_alloc, |
| 669 | .iolock = __videobuf_iolock, |
| 670 | .sync = __videobuf_sync, |
| 671 | .mmap_free = __videobuf_mmap_free, |
| 672 | .mmap_mapper = __videobuf_mmap_mapper, |
Al Viro | 13bcd5d | 2007-10-13 08:25:24 +0100 | [diff] [blame^] | 673 | .video_copy_to_user = __videobuf_copy_to_user, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 674 | .copy_stream = __videobuf_copy_stream, |
| 675 | }; |
| 676 | |
| 677 | void *videobuf_pci_alloc (size_t size) |
| 678 | { |
| 679 | struct videobuf_queue q; |
| 680 | |
| 681 | /* Required to make generic handler to call __videobuf_alloc */ |
| 682 | q.int_ops=&pci_ops; |
| 683 | |
| 684 | q.msize=size; |
| 685 | |
| 686 | return videobuf_alloc (&q); |
| 687 | } |
| 688 | |
| 689 | void videobuf_queue_pci_init(struct videobuf_queue* q, |
| 690 | struct videobuf_queue_ops *ops, |
| 691 | void *dev, |
| 692 | spinlock_t *irqlock, |
| 693 | enum v4l2_buf_type type, |
| 694 | enum v4l2_field field, |
| 695 | unsigned int msize, |
| 696 | void *priv) |
| 697 | { |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 698 | videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize, |
| 699 | priv, &pci_ops); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 700 | } |
| 701 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 702 | /* --------------------------------------------------------------------- */ |
| 703 | |
| 704 | EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg); |
| 705 | |
| 706 | EXPORT_SYMBOL_GPL(videobuf_to_dma); |
| 707 | EXPORT_SYMBOL_GPL(videobuf_dma_init); |
| 708 | EXPORT_SYMBOL_GPL(videobuf_dma_init_user); |
| 709 | EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel); |
| 710 | EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay); |
| 711 | EXPORT_SYMBOL_GPL(videobuf_dma_map); |
| 712 | EXPORT_SYMBOL_GPL(videobuf_dma_sync); |
| 713 | EXPORT_SYMBOL_GPL(videobuf_dma_unmap); |
| 714 | EXPORT_SYMBOL_GPL(videobuf_dma_free); |
| 715 | |
| 716 | EXPORT_SYMBOL_GPL(videobuf_pci_dma_map); |
| 717 | EXPORT_SYMBOL_GPL(videobuf_pci_dma_unmap); |
| 718 | EXPORT_SYMBOL_GPL(videobuf_pci_alloc); |
| 719 | |
| 720 | EXPORT_SYMBOL_GPL(videobuf_queue_pci_init); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 721 | |
| 722 | /* |
| 723 | * Local variables: |
| 724 | * c-basic-offset: 8 |
| 725 | * End: |
| 726 | */ |