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