Commit | Line | Data |
---|---|---|
87b9ad07 MCC |
1 | /* |
2 | * helper functions for vmalloc 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 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 | |
14 | */ | |
15 | ||
16 | #include <linux/init.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/moduleparam.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/interrupt.h> | |
21 | ||
22 | #include <linux/pci.h> | |
23 | #include <linux/vmalloc.h> | |
24 | #include <linux/pagemap.h> | |
25 | #include <asm/page.h> | |
26 | #include <asm/pgtable.h> | |
27 | ||
28 | #include <media/videobuf-vmalloc.h> | |
29 | ||
30 | #define MAGIC_DMABUF 0x17760309 | |
31 | #define MAGIC_VMAL_MEM 0x18221223 | |
32 | ||
33 | #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \ | |
34 | { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); } | |
35 | ||
ff699e6b | 36 | static int debug; |
87b9ad07 MCC |
37 | module_param(debug, int, 0644); |
38 | ||
39 | MODULE_DESCRIPTION("helper module to manage video4linux vmalloc buffers"); | |
40 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); | |
41 | MODULE_LICENSE("GPL"); | |
42 | ||
43 | #define dprintk(level, fmt, arg...) if (debug >= level) \ | |
493977f0 | 44 | printk(KERN_DEBUG "vbuf-vmalloc: " fmt , ## arg) |
87b9ad07 MCC |
45 | |
46 | ||
47 | /***************************************************************************/ | |
48 | ||
49 | static void | |
50 | videobuf_vm_open(struct vm_area_struct *vma) | |
51 | { | |
52 | struct videobuf_mapping *map = vma->vm_private_data; | |
53 | ||
0b29669c | 54 | dprintk(2,"vm_open %p [count=%u,vma=%08lx-%08lx]\n",map, |
87b9ad07 MCC |
55 | map->count,vma->vm_start,vma->vm_end); |
56 | ||
57 | map->count++; | |
58 | } | |
59 | ||
60 | static void | |
61 | videobuf_vm_close(struct vm_area_struct *vma) | |
62 | { | |
63 | struct videobuf_mapping *map = vma->vm_private_data; | |
64 | struct videobuf_queue *q = map->q; | |
87b9ad07 MCC |
65 | int i; |
66 | ||
0b29669c | 67 | dprintk(2,"vm_close %p [count=%u,vma=%08lx-%08lx]\n",map, |
87b9ad07 MCC |
68 | map->count,vma->vm_start,vma->vm_end); |
69 | ||
70 | map->count--; | |
71 | if (0 == map->count) { | |
72 | dprintk(1,"munmap %p q=%p\n",map,q); | |
64f9477f | 73 | mutex_lock(&q->vb_lock); |
87b9ad07 MCC |
74 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
75 | if (NULL == q->bufs[i]) | |
76 | continue; | |
87b9ad07 | 77 | |
851c0c96 | 78 | if (q->bufs[i]->map != map) |
87b9ad07 | 79 | continue; |
123f8ef6 | 80 | |
851c0c96 | 81 | q->bufs[i]->map = NULL; |
87b9ad07 | 82 | q->bufs[i]->baddr = 0; |
87b9ad07 | 83 | } |
64f9477f | 84 | mutex_unlock(&q->vb_lock); |
87b9ad07 MCC |
85 | kfree(map); |
86 | } | |
87 | return; | |
88 | } | |
89 | ||
90 | static struct vm_operations_struct videobuf_vm_ops = | |
91 | { | |
92 | .open = videobuf_vm_open, | |
93 | .close = videobuf_vm_close, | |
94 | }; | |
95 | ||
96 | /* --------------------------------------------------------------------- | |
97 | * vmalloc handlers for the generic methods | |
98 | */ | |
99 | ||
100 | /* Allocated area consists on 3 parts: | |
101 | struct video_buffer | |
102 | struct <driver>_buffer (cx88_buffer, saa7134_buf, ...) | |
0705135e | 103 | struct videobuf_dma_sg_memory |
87b9ad07 MCC |
104 | */ |
105 | ||
106 | static void *__videobuf_alloc(size_t size) | |
107 | { | |
384b835a | 108 | struct videobuf_vmalloc_memory *mem; |
87b9ad07 MCC |
109 | struct videobuf_buffer *vb; |
110 | ||
111 | vb = kzalloc(size+sizeof(*mem),GFP_KERNEL); | |
112 | ||
113 | mem = vb->priv = ((char *)vb)+size; | |
114 | mem->magic=MAGIC_VMAL_MEM; | |
115 | ||
116 | dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n", | |
7e28adb2 | 117 | __func__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb), |
87b9ad07 MCC |
118 | mem,(long)sizeof(*mem)); |
119 | ||
120 | return vb; | |
121 | } | |
122 | ||
123 | static int __videobuf_iolock (struct videobuf_queue* q, | |
124 | struct videobuf_buffer *vb, | |
125 | struct v4l2_framebuffer *fbuf) | |
126 | { | |
968ced78 | 127 | struct videobuf_vmalloc_memory *mem = vb->priv; |
87b9ad07 MCC |
128 | |
129 | BUG_ON(!mem); | |
130 | ||
968ced78 | 131 | MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); |
87b9ad07 | 132 | |
968ced78 MCC |
133 | switch (vb->memory) { |
134 | case V4L2_MEMORY_MMAP: | |
135 | dprintk(1, "%s memory method MMAP\n", __func__); | |
87b9ad07 | 136 | |
968ced78 MCC |
137 | /* All handling should be done by __videobuf_mmap_mapper() */ |
138 | if (!mem->vmalloc) { | |
139 | printk(KERN_ERR "memory is not alloced/mmapped.\n"); | |
140 | return -EINVAL; | |
141 | } | |
142 | break; | |
143 | case V4L2_MEMORY_USERPTR: | |
144 | { | |
145 | int pages = PAGE_ALIGN(vb->size); | |
87b9ad07 | 146 | |
968ced78 MCC |
147 | dprintk(1, "%s memory method USERPTR\n", __func__); |
148 | ||
149 | #if 1 | |
150 | if (vb->baddr) { | |
151 | printk(KERN_ERR "USERPTR is currently not supported\n"); | |
152 | return -EINVAL; | |
153 | } | |
154 | #endif | |
87b9ad07 | 155 | |
968ced78 MCC |
156 | /* The only USERPTR currently supported is the one needed for |
157 | read() method. | |
158 | */ | |
159 | ||
160 | mem->vmalloc = vmalloc_user(pages); | |
161 | if (!mem->vmalloc) { | |
162 | printk(KERN_ERR "vmalloc (%d pages) failed\n", pages); | |
163 | return -ENOMEM; | |
87b9ad07 | 164 | } |
968ced78 MCC |
165 | dprintk(1, "vmalloc is at addr %p (%d pages)\n", |
166 | mem->vmalloc, pages); | |
167 | ||
168 | #if 0 | |
169 | int rc; | |
170 | /* Kernel userptr is used also by read() method. In this case, | |
171 | there's no need to remap, since data will be copied to user | |
172 | */ | |
173 | if (!vb->baddr) | |
174 | return 0; | |
175 | ||
176 | /* FIXME: to properly support USERPTR, remap should occur. | |
177 | The code bellow won't work, since mem->vma = NULL | |
178 | */ | |
179 | /* Try to remap memory */ | |
180 | rc = remap_vmalloc_range(mem->vma, (void *)vb->baddr, 0); | |
181 | if (rc < 0) { | |
182 | printk(KERN_ERR "mmap: remap failed with error %d. ", rc); | |
183 | return -ENOMEM; | |
184 | } | |
185 | #endif | |
186 | ||
187 | break; | |
188 | } | |
189 | case V4L2_MEMORY_OVERLAY: | |
190 | default: | |
191 | dprintk(1, "%s memory method OVERLAY/unknown\n", __func__); | |
192 | ||
193 | /* Currently, doesn't support V4L2_MEMORY_OVERLAY */ | |
194 | printk(KERN_ERR "Memory method currently unsupported.\n"); | |
195 | return -EINVAL; | |
87b9ad07 MCC |
196 | } |
197 | ||
198 | return 0; | |
199 | } | |
200 | ||
201 | static int __videobuf_sync(struct videobuf_queue *q, | |
202 | struct videobuf_buffer *buf) | |
203 | { | |
204 | return 0; | |
205 | } | |
206 | ||
207 | static int __videobuf_mmap_free(struct videobuf_queue *q) | |
208 | { | |
209 | unsigned int i; | |
210 | ||
968ced78 | 211 | dprintk(1, "%s\n", __func__); |
87b9ad07 MCC |
212 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
213 | if (q->bufs[i]) { | |
851c0c96 | 214 | if (q->bufs[i]->map) |
87b9ad07 MCC |
215 | return -EBUSY; |
216 | } | |
217 | } | |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
222 | static int __videobuf_mmap_mapper(struct videobuf_queue *q, | |
223 | struct vm_area_struct *vma) | |
224 | { | |
384b835a | 225 | struct videobuf_vmalloc_memory *mem; |
87b9ad07 MCC |
226 | struct videobuf_mapping *map; |
227 | unsigned int first; | |
968ced78 | 228 | int retval, pages; |
87b9ad07 MCC |
229 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; |
230 | ||
968ced78 MCC |
231 | dprintk(1, "%s\n", __func__); |
232 | if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) | |
87b9ad07 MCC |
233 | return -EINVAL; |
234 | ||
235 | /* look for first buffer to map */ | |
236 | for (first = 0; first < VIDEO_MAX_FRAME; first++) { | |
237 | if (NULL == q->bufs[first]) | |
238 | continue; | |
239 | ||
240 | if (V4L2_MEMORY_MMAP != q->bufs[first]->memory) | |
241 | continue; | |
242 | if (q->bufs[first]->boff == offset) | |
243 | break; | |
244 | } | |
245 | if (VIDEO_MAX_FRAME == first) { | |
246 | dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n", | |
247 | (vma->vm_pgoff << PAGE_SHIFT)); | |
248 | return -EINVAL; | |
249 | } | |
87b9ad07 MCC |
250 | |
251 | /* create mapping + update buffer list */ | |
968ced78 | 252 | map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL); |
87b9ad07 MCC |
253 | if (NULL == map) |
254 | return -ENOMEM; | |
255 | ||
968ced78 | 256 | q->bufs[first]->map = map; |
87b9ad07 MCC |
257 | map->start = vma->vm_start; |
258 | map->end = vma->vm_end; | |
259 | map->q = q; | |
260 | ||
261 | q->bufs[first]->baddr = vma->vm_start; | |
262 | ||
968ced78 MCC |
263 | mem = q->bufs[first]->priv; |
264 | BUG_ON(!mem); | |
265 | MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); | |
87b9ad07 | 266 | |
968ced78 MCC |
267 | pages = PAGE_ALIGN(vma->vm_end - vma->vm_start); |
268 | mem->vmalloc = vmalloc_user(pages); | |
269 | if (!mem->vmalloc) { | |
270 | printk(KERN_ERR "vmalloc (%d pages) failed\n", pages); | |
271 | goto error; | |
272 | } | |
273 | dprintk(1, "vmalloc is at addr %p (%d pages)\n", | |
274 | mem->vmalloc, pages); | |
851c0c96 | 275 | |
87b9ad07 | 276 | /* Try to remap memory */ |
968ced78 MCC |
277 | retval = remap_vmalloc_range(vma, mem->vmalloc, 0); |
278 | if (retval < 0) { | |
279 | printk(KERN_ERR "mmap: remap failed with error %d. ", retval); | |
280 | vfree(mem->vmalloc); | |
281 | goto error; | |
87b9ad07 MCC |
282 | } |
283 | ||
968ced78 MCC |
284 | vma->vm_ops = &videobuf_vm_ops; |
285 | vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED; | |
286 | vma->vm_private_data = map; | |
287 | ||
87b9ad07 | 288 | dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n", |
968ced78 | 289 | map, q, vma->vm_start, vma->vm_end, |
87b9ad07 | 290 | (long int) q->bufs[first]->bsize, |
968ced78 | 291 | vma->vm_pgoff, first); |
87b9ad07 MCC |
292 | |
293 | videobuf_vm_open(vma); | |
294 | ||
968ced78 MCC |
295 | return 0; |
296 | ||
297 | error: | |
298 | mem = NULL; | |
299 | kfree(map); | |
300 | return -ENOMEM; | |
87b9ad07 MCC |
301 | } |
302 | ||
87b9ad07 MCC |
303 | static int __videobuf_copy_to_user ( struct videobuf_queue *q, |
304 | char __user *data, size_t count, | |
305 | int nonblocking ) | |
306 | { | |
384b835a | 307 | struct videobuf_vmalloc_memory *mem=q->read_buf->priv; |
87b9ad07 MCC |
308 | BUG_ON (!mem); |
309 | MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); | |
310 | ||
311 | BUG_ON (!mem->vmalloc); | |
312 | ||
313 | /* copy to userspace */ | |
314 | if (count > q->read_buf->size - q->read_off) | |
315 | count = q->read_buf->size - q->read_off; | |
316 | ||
317 | if (copy_to_user(data, mem->vmalloc+q->read_off, count)) | |
318 | return -EFAULT; | |
319 | ||
320 | return count; | |
321 | } | |
322 | ||
323 | static int __videobuf_copy_stream ( struct videobuf_queue *q, | |
324 | char __user *data, size_t count, size_t pos, | |
325 | int vbihack, int nonblocking ) | |
326 | { | |
327 | unsigned int *fc; | |
384b835a | 328 | struct videobuf_vmalloc_memory *mem=q->read_buf->priv; |
87b9ad07 MCC |
329 | BUG_ON (!mem); |
330 | MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); | |
331 | ||
332 | if (vbihack) { | |
333 | /* dirty, undocumented hack -- pass the frame counter | |
334 | * within the last four bytes of each vbi data block. | |
335 | * We need that one to maintain backward compatibility | |
336 | * to all vbi decoding software out there ... */ | |
337 | fc = (unsigned int*)mem->vmalloc; | |
338 | fc += (q->read_buf->size>>2) -1; | |
339 | *fc = q->read_buf->field_count >> 1; | |
340 | dprintk(1,"vbihack: %d\n",*fc); | |
341 | } | |
342 | ||
343 | /* copy stuff using the common method */ | |
344 | count = __videobuf_copy_to_user (q,data,count,nonblocking); | |
345 | ||
346 | if ( (count==-EFAULT) && (0 == pos) ) | |
347 | return -EFAULT; | |
348 | ||
349 | return count; | |
350 | } | |
351 | ||
352 | static struct videobuf_qtype_ops qops = { | |
353 | .magic = MAGIC_QTYPE_OPS, | |
354 | ||
355 | .alloc = __videobuf_alloc, | |
356 | .iolock = __videobuf_iolock, | |
357 | .sync = __videobuf_sync, | |
358 | .mmap_free = __videobuf_mmap_free, | |
359 | .mmap_mapper = __videobuf_mmap_mapper, | |
13bcd5d0 | 360 | .video_copy_to_user = __videobuf_copy_to_user, |
87b9ad07 MCC |
361 | .copy_stream = __videobuf_copy_stream, |
362 | }; | |
363 | ||
364 | void videobuf_queue_vmalloc_init(struct videobuf_queue* q, | |
365 | struct videobuf_queue_ops *ops, | |
366 | void *dev, | |
367 | spinlock_t *irqlock, | |
368 | enum v4l2_buf_type type, | |
369 | enum v4l2_field field, | |
370 | unsigned int msize, | |
371 | void *priv) | |
372 | { | |
d4cae5a5 MCC |
373 | videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize, |
374 | priv, &qops); | |
87b9ad07 MCC |
375 | } |
376 | ||
377 | EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init); | |
378 | ||
379 | void *videobuf_to_vmalloc (struct videobuf_buffer *buf) | |
380 | { | |
384b835a | 381 | struct videobuf_vmalloc_memory *mem=buf->priv; |
87b9ad07 MCC |
382 | BUG_ON (!mem); |
383 | MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); | |
384 | ||
385 | return mem->vmalloc; | |
386 | } | |
387 | EXPORT_SYMBOL_GPL(videobuf_to_vmalloc); | |
388 | ||
389 | void videobuf_vmalloc_free (struct videobuf_buffer *buf) | |
390 | { | |
968ced78 | 391 | struct videobuf_vmalloc_memory *mem = buf->priv; |
87b9ad07 | 392 | |
968ced78 MCC |
393 | if (!mem) |
394 | return; | |
395 | ||
396 | MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); | |
87b9ad07 MCC |
397 | |
398 | vfree(mem->vmalloc); | |
968ced78 MCC |
399 | mem->vmalloc = NULL; |
400 | ||
401 | ||
402 | ||
403 | /* FIXME: need to do buf->priv = NULL? */ | |
87b9ad07 MCC |
404 | |
405 | return; | |
406 | } | |
407 | EXPORT_SYMBOL_GPL(videobuf_vmalloc_free); | |
408 | ||
409 | /* | |
410 | * Local variables: | |
411 | * c-basic-offset: 8 | |
412 | * End: | |
413 | */ |