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