Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1 | /* |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 2 | * helper functions for SG DMA video4linux capture buffers |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 3 | * |
Magnus Damm | 5d6aaf5 | 2008-07-16 21:27:49 -0300 | [diff] [blame] | 4 | * The functions expect the hardware being able to scatter gather |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame^] | 24 | #include <linux/sched.h> |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 25 | #include <linux/slab.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 28 | #include <linux/dma-mapping.h> |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 29 | #include <linux/vmalloc.h> |
| 30 | #include <linux/pagemap.h> |
Ralf Baechle | 1176360 | 2007-10-23 20:42:11 +0200 | [diff] [blame] | 31 | #include <linux/scatterlist.h> |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 32 | #include <asm/page.h> |
| 33 | #include <asm/pgtable.h> |
| 34 | |
| 35 | #include <media/videobuf-dma-sg.h> |
| 36 | |
| 37 | #define MAGIC_DMABUF 0x19721112 |
| 38 | #define MAGIC_SG_MEM 0x17890714 |
| 39 | |
| 40 | #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \ |
| 41 | { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); } |
| 42 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 43 | static int debug; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 44 | module_param(debug, int, 0644); |
| 45 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 46 | MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 47 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); |
| 48 | MODULE_LICENSE("GPL"); |
| 49 | |
| 50 | #define dprintk(level, fmt, arg...) if (debug >= level) \ |
| 51 | printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg) |
| 52 | |
| 53 | /* --------------------------------------------------------------------- */ |
| 54 | |
| 55 | struct scatterlist* |
| 56 | videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages) |
| 57 | { |
| 58 | struct scatterlist *sglist; |
| 59 | struct page *pg; |
| 60 | int i; |
| 61 | |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 62 | sglist = vmalloc(nr_pages * sizeof(*sglist)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 63 | if (NULL == sglist) |
| 64 | return NULL; |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 65 | memset(sglist, 0, nr_pages * sizeof(*sglist)); |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 66 | sg_init_table(sglist, nr_pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 67 | for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) { |
| 68 | pg = vmalloc_to_page(virt); |
| 69 | if (NULL == pg) |
| 70 | goto err; |
| 71 | BUG_ON(PageHighMem(pg)); |
Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 72 | sg_set_page(&sglist[i], pg, PAGE_SIZE, 0); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 73 | } |
| 74 | return sglist; |
| 75 | |
| 76 | err: |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 77 | vfree(sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | struct scatterlist* |
| 82 | videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset) |
| 83 | { |
| 84 | struct scatterlist *sglist; |
Christophe Jaillet | a47cacb | 2008-07-04 06:33:22 -0300 | [diff] [blame] | 85 | int i; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 86 | |
| 87 | if (NULL == pages[0]) |
| 88 | return NULL; |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 89 | sglist = vmalloc(nr_pages * sizeof(*sglist)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 90 | if (NULL == sglist) |
| 91 | return NULL; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 92 | sg_init_table(sglist, nr_pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 93 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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"); |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 109 | vfree(sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 110 | return NULL; |
| 111 | |
| 112 | highmem: |
| 113 | dprintk(2,"sgl: oops - highmem page\n"); |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 114 | vfree(sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | /* --------------------------------------------------------------------- */ |
| 119 | |
| 120 | struct videobuf_dmabuf *videobuf_to_dma (struct videobuf_buffer *buf) |
| 121 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 122 | struct videobuf_dma_sg_memory *mem = buf->priv; |
| 123 | BUG_ON(!mem); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 124 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 125 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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) { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 144 | case DMA_FROM_DEVICE: |
| 145 | rw = READ; |
| 146 | break; |
| 147 | case DMA_TO_DEVICE: |
| 148 | rw = WRITE; |
| 149 | break; |
| 150 | default: |
| 151 | BUG(); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | first = (data & PAGE_MASK) >> PAGE_SHIFT; |
| 155 | last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT; |
| 156 | dma->offset = data & ~PAGE_MASK; |
| 157 | dma->nr_pages = last-first+1; |
| 158 | dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*), |
| 159 | GFP_KERNEL); |
| 160 | if (NULL == dma->pages) |
| 161 | return -ENOMEM; |
| 162 | dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n", |
| 163 | data,size,dma->nr_pages); |
| 164 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 165 | err = get_user_pages(current,current->mm, |
| 166 | data & PAGE_MASK, dma->nr_pages, |
| 167 | rw == READ, 1, /* force */ |
| 168 | dma->pages, NULL); |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 169 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 170 | if (err != dma->nr_pages) { |
| 171 | dma->nr_pages = (err >= 0) ? err : 0; |
| 172 | dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages); |
| 173 | return err < 0 ? err : -EINVAL; |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 178 | int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction, |
| 179 | unsigned long data, unsigned long size) |
| 180 | { |
| 181 | int ret; |
| 182 | down_read(¤t->mm->mmap_sem); |
| 183 | ret = videobuf_dma_init_user_locked(dma, direction, data, size); |
| 184 | up_read(¤t->mm->mmap_sem); |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 189 | int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction, |
| 190 | int nr_pages) |
| 191 | { |
| 192 | dprintk(1,"init kernel [%d pages]\n",nr_pages); |
| 193 | dma->direction = direction; |
| 194 | dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT); |
| 195 | if (NULL == dma->vmalloc) { |
| 196 | dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages); |
| 197 | return -ENOMEM; |
| 198 | } |
| 199 | dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n", |
| 200 | (unsigned long)dma->vmalloc, |
| 201 | nr_pages << PAGE_SHIFT); |
| 202 | memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT); |
| 203 | dma->nr_pages = nr_pages; |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction, |
| 208 | dma_addr_t addr, int nr_pages) |
| 209 | { |
| 210 | dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n", |
| 211 | nr_pages,(unsigned long)addr); |
| 212 | dma->direction = direction; |
| 213 | if (0 == addr) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | dma->bus_addr = addr; |
| 217 | dma->nr_pages = nr_pages; |
| 218 | return 0; |
| 219 | } |
| 220 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 221 | int videobuf_dma_map(struct videobuf_queue* q, struct videobuf_dmabuf *dma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 222 | { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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) { |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 235 | dma->sglist = vmalloc(sizeof(*dma->sglist)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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) { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 248 | dma->sglen = dma_map_sg(q->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 |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 252 | "%s: videobuf_map_sg failed\n",__func__); |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 253 | vfree(dma->sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 254 | dma->sglist = NULL; |
| 255 | dma->sglen = 0; |
Figo.zhang | 92051b2 | 2009-06-10 23:17:27 -0300 | [diff] [blame] | 256 | return -ENOMEM; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | return 0; |
| 260 | } |
| 261 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 262 | int videobuf_dma_sync(struct videobuf_queue *q, struct videobuf_dmabuf *dma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 263 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 264 | MAGIC_CHECK(dma->magic, MAGIC_DMABUF); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 265 | BUG_ON(!dma->sglen); |
| 266 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 267 | dma_sync_sg_for_cpu(q->dev, dma->sglist, dma->nr_pages, dma->direction); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma) |
| 272 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 273 | MAGIC_CHECK(dma->magic, MAGIC_DMABUF); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 274 | if (!dma->sglen) |
| 275 | return 0; |
| 276 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 277 | dma_unmap_sg(q->dev, dma->sglist, dma->nr_pages, dma->direction); |
Mauro Carvalho Chehab | 5ddff43 | 2007-10-08 11:43:49 -0300 | [diff] [blame] | 278 | |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 279 | vfree(dma->sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 280 | dma->sglist = NULL; |
| 281 | dma->sglen = 0; |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | int videobuf_dma_free(struct videobuf_dmabuf *dma) |
| 286 | { |
| 287 | MAGIC_CHECK(dma->magic,MAGIC_DMABUF); |
| 288 | BUG_ON(dma->sglen); |
| 289 | |
| 290 | if (dma->pages) { |
| 291 | int i; |
| 292 | for (i=0; i < dma->nr_pages; i++) |
| 293 | page_cache_release(dma->pages[i]); |
| 294 | kfree(dma->pages); |
| 295 | dma->pages = NULL; |
| 296 | } |
| 297 | |
| 298 | vfree(dma->vmalloc); |
| 299 | dma->vmalloc = NULL; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 300 | |
| 301 | if (dma->bus_addr) { |
| 302 | dma->bus_addr = 0; |
| 303 | } |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 304 | dma->direction = DMA_NONE; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /* --------------------------------------------------------------------- */ |
| 309 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 310 | int videobuf_sg_dma_map(struct device *dev, struct videobuf_dmabuf *dma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 311 | { |
| 312 | struct videobuf_queue q; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 313 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 314 | q.dev = dev; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 315 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 316 | return videobuf_dma_map(&q, dma); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 317 | } |
| 318 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 319 | int videobuf_sg_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 320 | { |
| 321 | struct videobuf_queue q; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 322 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 323 | q.dev = dev; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 324 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 325 | return videobuf_dma_unmap(&q, dma); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* --------------------------------------------------------------------- */ |
| 329 | |
| 330 | static void |
| 331 | videobuf_vm_open(struct vm_area_struct *vma) |
| 332 | { |
| 333 | struct videobuf_mapping *map = vma->vm_private_data; |
| 334 | |
| 335 | dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map, |
| 336 | map->count,vma->vm_start,vma->vm_end); |
| 337 | map->count++; |
| 338 | } |
| 339 | |
| 340 | static void |
| 341 | videobuf_vm_close(struct vm_area_struct *vma) |
| 342 | { |
| 343 | struct videobuf_mapping *map = vma->vm_private_data; |
| 344 | struct videobuf_queue *q = map->q; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 345 | struct videobuf_dma_sg_memory *mem; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 346 | int i; |
| 347 | |
| 348 | dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map, |
| 349 | map->count,vma->vm_start,vma->vm_end); |
| 350 | |
| 351 | map->count--; |
| 352 | if (0 == map->count) { |
| 353 | dprintk(1,"munmap %p q=%p\n",map,q); |
Mauro Carvalho Chehab | 64f9477 | 2008-01-31 13:57:53 -0300 | [diff] [blame] | 354 | mutex_lock(&q->vb_lock); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 355 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 356 | if (NULL == q->bufs[i]) |
| 357 | continue; |
| 358 | mem=q->bufs[i]->priv; |
| 359 | |
| 360 | if (!mem) |
| 361 | continue; |
| 362 | |
| 363 | MAGIC_CHECK(mem->magic,MAGIC_SG_MEM); |
| 364 | |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 365 | if (q->bufs[i]->map != map) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 366 | continue; |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 367 | q->bufs[i]->map = NULL; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 368 | q->bufs[i]->baddr = 0; |
| 369 | q->ops->buf_release(q,q->bufs[i]); |
| 370 | } |
Mauro Carvalho Chehab | 64f9477 | 2008-01-31 13:57:53 -0300 | [diff] [blame] | 371 | mutex_unlock(&q->vb_lock); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 372 | kfree(map); |
| 373 | } |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Get a anonymous page for the mapping. Make sure we can DMA to that |
| 379 | * memory location with 32bit PCI devices (i.e. don't use highmem for |
| 380 | * now ...). Bounce buffers don't work very well for the data rates |
| 381 | * video capture has. |
| 382 | */ |
Nick Piggin | 105354a | 2007-12-07 17:57:38 -0300 | [diff] [blame] | 383 | static int |
| 384 | 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] | 385 | { |
| 386 | struct page *page; |
| 387 | |
Nick Piggin | 105354a | 2007-12-07 17:57:38 -0300 | [diff] [blame] | 388 | dprintk(3,"fault: fault @ %08lx [vma %08lx-%08lx]\n", |
| 389 | (unsigned long)vmf->virtual_address,vma->vm_start,vma->vm_end); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 390 | page = alloc_page(GFP_USER | __GFP_DMA32); |
| 391 | if (!page) |
Nick Piggin | 105354a | 2007-12-07 17:57:38 -0300 | [diff] [blame] | 392 | return VM_FAULT_OOM; |
Guennadi Liakhovetski | c0cd501 | 2009-01-03 18:20:04 -0300 | [diff] [blame] | 393 | clear_user_highpage(page, (unsigned long)vmf->virtual_address); |
Nick Piggin | 105354a | 2007-12-07 17:57:38 -0300 | [diff] [blame] | 394 | vmf->page = page; |
| 395 | return 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 396 | } |
| 397 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 398 | static const struct vm_operations_struct videobuf_vm_ops = |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 399 | { |
| 400 | .open = videobuf_vm_open, |
| 401 | .close = videobuf_vm_close, |
Nick Piggin | 105354a | 2007-12-07 17:57:38 -0300 | [diff] [blame] | 402 | .fault = videobuf_vm_fault, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 403 | }; |
| 404 | |
| 405 | /* --------------------------------------------------------------------- |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 406 | * SG handlers for the generic methods |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 407 | */ |
| 408 | |
| 409 | /* Allocated area consists on 3 parts: |
| 410 | struct video_buffer |
| 411 | struct <driver>_buffer (cx88_buffer, saa7134_buf, ...) |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 412 | struct videobuf_dma_sg_memory |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 413 | */ |
| 414 | |
| 415 | static void *__videobuf_alloc(size_t size) |
| 416 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 417 | struct videobuf_dma_sg_memory *mem; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 418 | struct videobuf_buffer *vb; |
| 419 | |
| 420 | vb = kzalloc(size+sizeof(*mem),GFP_KERNEL); |
| 421 | |
| 422 | mem = vb->priv = ((char *)vb)+size; |
| 423 | mem->magic=MAGIC_SG_MEM; |
| 424 | |
| 425 | videobuf_dma_init(&mem->dma); |
| 426 | |
| 427 | dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n", |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 428 | __func__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb), |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 429 | mem,(long)sizeof(*mem)); |
| 430 | |
| 431 | return vb; |
| 432 | } |
| 433 | |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 434 | static void *__videobuf_to_vmalloc (struct videobuf_buffer *buf) |
| 435 | { |
| 436 | struct videobuf_dma_sg_memory *mem = buf->priv; |
| 437 | BUG_ON(!mem); |
| 438 | |
| 439 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
| 440 | |
| 441 | return mem->dma.vmalloc; |
| 442 | } |
| 443 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 450 | struct videobuf_dma_sg_memory *mem = vb->priv; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 451 | BUG_ON(!mem); |
| 452 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 453 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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, |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 462 | DMA_FROM_DEVICE, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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, |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 469 | DMA_FROM_DEVICE, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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, |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 480 | DMA_FROM_DEVICE, |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 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; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 497 | err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 498 | bus, pages); |
| 499 | if (0 != err) |
| 500 | return err; |
| 501 | break; |
| 502 | default: |
| 503 | BUG(); |
| 504 | } |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 505 | err = videobuf_dma_map(q, &mem->dma); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 515 | struct videobuf_dma_sg_memory *mem = buf->priv; |
| 516 | BUG_ON(!mem); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 539 | struct videobuf_dma_sg_memory *mem; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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 | |
Brandon Philips | ce54093 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 554 | /* This function maintains backwards compatibility with V4L1 and will |
| 555 | * map more than one buffer if the vma length is equal to the combined |
| 556 | * size of multiple buffers than it will map them together. See |
| 557 | * VIDIOCGMBUF in the v4l spec |
| 558 | * |
| 559 | * TODO: Allow drivers to specify if they support this mode |
| 560 | */ |
| 561 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 562 | /* look for first buffer to map */ |
| 563 | for (first = 0; first < VIDEO_MAX_FRAME; first++) { |
| 564 | if (NULL == q->bufs[first]) |
| 565 | continue; |
| 566 | mem=q->bufs[first]->priv; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 567 | BUG_ON(!mem); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 568 | MAGIC_CHECK(mem->magic,MAGIC_SG_MEM); |
| 569 | |
| 570 | if (V4L2_MEMORY_MMAP != q->bufs[first]->memory) |
| 571 | continue; |
| 572 | if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT)) |
| 573 | break; |
| 574 | } |
| 575 | if (VIDEO_MAX_FRAME == first) { |
| 576 | dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n", |
| 577 | (vma->vm_pgoff << PAGE_SHIFT)); |
| 578 | goto done; |
| 579 | } |
| 580 | |
| 581 | /* look for last buffer to map */ |
| 582 | for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) { |
| 583 | if (NULL == q->bufs[last]) |
| 584 | continue; |
| 585 | if (V4L2_MEMORY_MMAP != q->bufs[last]->memory) |
| 586 | continue; |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 587 | if (q->bufs[last]->map) { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 588 | retval = -EBUSY; |
| 589 | goto done; |
| 590 | } |
| 591 | size += q->bufs[last]->bsize; |
| 592 | if (size == (vma->vm_end - vma->vm_start)) |
| 593 | break; |
| 594 | } |
| 595 | if (VIDEO_MAX_FRAME == last) { |
| 596 | dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n", |
| 597 | (vma->vm_end - vma->vm_start)); |
| 598 | goto done; |
| 599 | } |
| 600 | |
| 601 | /* create mapping + update buffer list */ |
| 602 | retval = -ENOMEM; |
| 603 | map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL); |
| 604 | if (NULL == map) |
| 605 | goto done; |
Brandon Philips | ce54093 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 606 | |
| 607 | size = 0; |
| 608 | for (i = first; i <= last; i++) { |
| 609 | if (NULL == q->bufs[i]) |
| 610 | continue; |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 611 | q->bufs[i]->map = map; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 612 | q->bufs[i]->baddr = vma->vm_start + size; |
Brandon Philips | ce54093 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 613 | size += q->bufs[i]->bsize; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 614 | } |
Brandon Philips | ce54093 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 615 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 616 | map->count = 1; |
| 617 | map->start = vma->vm_start; |
| 618 | map->end = vma->vm_end; |
| 619 | map->q = q; |
| 620 | vma->vm_ops = &videobuf_vm_ops; |
| 621 | vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED; |
| 622 | vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */ |
| 623 | vma->vm_private_data = map; |
| 624 | dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n", |
| 625 | map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last); |
| 626 | retval = 0; |
| 627 | |
| 628 | done: |
| 629 | return retval; |
| 630 | } |
| 631 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 632 | static int __videobuf_copy_to_user ( struct videobuf_queue *q, |
| 633 | char __user *data, size_t count, |
| 634 | int nonblocking ) |
| 635 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 636 | struct videobuf_dma_sg_memory *mem = q->read_buf->priv; |
| 637 | BUG_ON(!mem); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 638 | MAGIC_CHECK(mem->magic,MAGIC_SG_MEM); |
| 639 | |
| 640 | /* copy to userspace */ |
| 641 | if (count > q->read_buf->size - q->read_off) |
| 642 | count = q->read_buf->size - q->read_off; |
| 643 | |
| 644 | if (copy_to_user(data, mem->dma.vmalloc+q->read_off, count)) |
| 645 | return -EFAULT; |
| 646 | |
| 647 | return count; |
| 648 | } |
| 649 | |
| 650 | static int __videobuf_copy_stream ( struct videobuf_queue *q, |
| 651 | char __user *data, size_t count, size_t pos, |
| 652 | int vbihack, int nonblocking ) |
| 653 | { |
| 654 | unsigned int *fc; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 655 | struct videobuf_dma_sg_memory *mem = q->read_buf->priv; |
| 656 | BUG_ON(!mem); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 657 | MAGIC_CHECK(mem->magic,MAGIC_SG_MEM); |
| 658 | |
| 659 | if (vbihack) { |
| 660 | /* dirty, undocumented hack -- pass the frame counter |
| 661 | * within the last four bytes of each vbi data block. |
| 662 | * We need that one to maintain backward compatibility |
| 663 | * to all vbi decoding software out there ... */ |
| 664 | fc = (unsigned int*)mem->dma.vmalloc; |
| 665 | fc += (q->read_buf->size>>2) -1; |
| 666 | *fc = q->read_buf->field_count >> 1; |
| 667 | dprintk(1,"vbihack: %d\n",*fc); |
| 668 | } |
| 669 | |
| 670 | /* copy stuff using the common method */ |
| 671 | count = __videobuf_copy_to_user (q,data,count,nonblocking); |
| 672 | |
| 673 | if ( (count==-EFAULT) && (0 == pos) ) |
| 674 | return -EFAULT; |
| 675 | |
| 676 | return count; |
| 677 | } |
| 678 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 679 | static struct videobuf_qtype_ops sg_ops = { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 680 | .magic = MAGIC_QTYPE_OPS, |
| 681 | |
| 682 | .alloc = __videobuf_alloc, |
| 683 | .iolock = __videobuf_iolock, |
| 684 | .sync = __videobuf_sync, |
| 685 | .mmap_free = __videobuf_mmap_free, |
| 686 | .mmap_mapper = __videobuf_mmap_mapper, |
Al Viro | 13bcd5d | 2007-10-13 08:25:24 +0100 | [diff] [blame] | 687 | .video_copy_to_user = __videobuf_copy_to_user, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 688 | .copy_stream = __videobuf_copy_stream, |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 689 | .vmalloc = __videobuf_to_vmalloc, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 690 | }; |
| 691 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 692 | void *videobuf_sg_alloc(size_t size) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 693 | { |
| 694 | struct videobuf_queue q; |
| 695 | |
| 696 | /* Required to make generic handler to call __videobuf_alloc */ |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 697 | q.int_ops = &sg_ops; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 698 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 699 | q.msize = size; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 700 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 701 | return videobuf_alloc(&q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 702 | } |
| 703 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 704 | void videobuf_queue_sg_init(struct videobuf_queue* q, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 705 | struct videobuf_queue_ops *ops, |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 706 | struct device *dev, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 707 | spinlock_t *irqlock, |
| 708 | enum v4l2_buf_type type, |
| 709 | enum v4l2_field field, |
| 710 | unsigned int msize, |
| 711 | void *priv) |
| 712 | { |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 713 | videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize, |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 714 | priv, &sg_ops); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 715 | } |
| 716 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 717 | /* --------------------------------------------------------------------- */ |
| 718 | |
| 719 | EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg); |
| 720 | |
| 721 | EXPORT_SYMBOL_GPL(videobuf_to_dma); |
| 722 | EXPORT_SYMBOL_GPL(videobuf_dma_init); |
| 723 | EXPORT_SYMBOL_GPL(videobuf_dma_init_user); |
| 724 | EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel); |
| 725 | EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay); |
| 726 | EXPORT_SYMBOL_GPL(videobuf_dma_map); |
| 727 | EXPORT_SYMBOL_GPL(videobuf_dma_sync); |
| 728 | EXPORT_SYMBOL_GPL(videobuf_dma_unmap); |
| 729 | EXPORT_SYMBOL_GPL(videobuf_dma_free); |
| 730 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 731 | EXPORT_SYMBOL_GPL(videobuf_sg_dma_map); |
| 732 | EXPORT_SYMBOL_GPL(videobuf_sg_dma_unmap); |
| 733 | EXPORT_SYMBOL_GPL(videobuf_sg_alloc); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 734 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 735 | EXPORT_SYMBOL_GPL(videobuf_queue_sg_init); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * Local variables: |
| 739 | * c-basic-offset: 8 |
| 740 | * End: |
| 741 | */ |