This source file includes following definitions.
- iio_buffer_block_release
- iio_buffer_block_get
- iio_buffer_block_put
- iio_dma_buffer_cleanup_worker
- iio_buffer_block_release_atomic
- iio_buffer_block_put_atomic
- iio_buffer_to_queue
- iio_dma_buffer_alloc_block
- _iio_dma_buffer_block_done
- iio_dma_buffer_block_done
- iio_dma_buffer_block_list_abort
- iio_dma_block_reusable
- iio_dma_buffer_request_update
- iio_dma_buffer_submit_block
- iio_dma_buffer_enable
- iio_dma_buffer_disable
- iio_dma_buffer_enqueue
- iio_dma_buffer_dequeue
- iio_dma_buffer_read
- iio_dma_buffer_data_available
- iio_dma_buffer_set_bytes_per_datum
- iio_dma_buffer_set_length
- iio_dma_buffer_init
- iio_dma_buffer_exit
- iio_dma_buffer_release
1
2
3
4
5
6
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/workqueue.h>
12 #include <linux/mutex.h>
13 #include <linux/sched.h>
14 #include <linux/poll.h>
15 #include <linux/iio/buffer.h>
16 #include <linux/iio/buffer_impl.h>
17 #include <linux/iio/buffer-dma.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/sizes.h>
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 static void iio_buffer_block_release(struct kref *kref)
95 {
96 struct iio_dma_buffer_block *block = container_of(kref,
97 struct iio_dma_buffer_block, kref);
98
99 WARN_ON(block->state != IIO_BLOCK_STATE_DEAD);
100
101 dma_free_coherent(block->queue->dev, PAGE_ALIGN(block->size),
102 block->vaddr, block->phys_addr);
103
104 iio_buffer_put(&block->queue->buffer);
105 kfree(block);
106 }
107
108 static void iio_buffer_block_get(struct iio_dma_buffer_block *block)
109 {
110 kref_get(&block->kref);
111 }
112
113 static void iio_buffer_block_put(struct iio_dma_buffer_block *block)
114 {
115 kref_put(&block->kref, iio_buffer_block_release);
116 }
117
118
119
120
121
122 static LIST_HEAD(iio_dma_buffer_dead_blocks);
123 static DEFINE_SPINLOCK(iio_dma_buffer_dead_blocks_lock);
124
125 static void iio_dma_buffer_cleanup_worker(struct work_struct *work)
126 {
127 struct iio_dma_buffer_block *block, *_block;
128 LIST_HEAD(block_list);
129
130 spin_lock_irq(&iio_dma_buffer_dead_blocks_lock);
131 list_splice_tail_init(&iio_dma_buffer_dead_blocks, &block_list);
132 spin_unlock_irq(&iio_dma_buffer_dead_blocks_lock);
133
134 list_for_each_entry_safe(block, _block, &block_list, head)
135 iio_buffer_block_release(&block->kref);
136 }
137 static DECLARE_WORK(iio_dma_buffer_cleanup_work, iio_dma_buffer_cleanup_worker);
138
139 static void iio_buffer_block_release_atomic(struct kref *kref)
140 {
141 struct iio_dma_buffer_block *block;
142 unsigned long flags;
143
144 block = container_of(kref, struct iio_dma_buffer_block, kref);
145
146 spin_lock_irqsave(&iio_dma_buffer_dead_blocks_lock, flags);
147 list_add_tail(&block->head, &iio_dma_buffer_dead_blocks);
148 spin_unlock_irqrestore(&iio_dma_buffer_dead_blocks_lock, flags);
149
150 schedule_work(&iio_dma_buffer_cleanup_work);
151 }
152
153
154
155
156 static void iio_buffer_block_put_atomic(struct iio_dma_buffer_block *block)
157 {
158 kref_put(&block->kref, iio_buffer_block_release_atomic);
159 }
160
161 static struct iio_dma_buffer_queue *iio_buffer_to_queue(struct iio_buffer *buf)
162 {
163 return container_of(buf, struct iio_dma_buffer_queue, buffer);
164 }
165
166 static struct iio_dma_buffer_block *iio_dma_buffer_alloc_block(
167 struct iio_dma_buffer_queue *queue, size_t size)
168 {
169 struct iio_dma_buffer_block *block;
170
171 block = kzalloc(sizeof(*block), GFP_KERNEL);
172 if (!block)
173 return NULL;
174
175 block->vaddr = dma_alloc_coherent(queue->dev, PAGE_ALIGN(size),
176 &block->phys_addr, GFP_KERNEL);
177 if (!block->vaddr) {
178 kfree(block);
179 return NULL;
180 }
181
182 block->size = size;
183 block->state = IIO_BLOCK_STATE_DEQUEUED;
184 block->queue = queue;
185 INIT_LIST_HEAD(&block->head);
186 kref_init(&block->kref);
187
188 iio_buffer_get(&queue->buffer);
189
190 return block;
191 }
192
193 static void _iio_dma_buffer_block_done(struct iio_dma_buffer_block *block)
194 {
195 struct iio_dma_buffer_queue *queue = block->queue;
196
197
198
199
200
201 if (block->state != IIO_BLOCK_STATE_DEAD) {
202 block->state = IIO_BLOCK_STATE_DONE;
203 list_add_tail(&block->head, &queue->outgoing);
204 }
205 }
206
207
208
209
210
211
212
213
214 void iio_dma_buffer_block_done(struct iio_dma_buffer_block *block)
215 {
216 struct iio_dma_buffer_queue *queue = block->queue;
217 unsigned long flags;
218
219 spin_lock_irqsave(&queue->list_lock, flags);
220 _iio_dma_buffer_block_done(block);
221 spin_unlock_irqrestore(&queue->list_lock, flags);
222
223 iio_buffer_block_put_atomic(block);
224 wake_up_interruptible_poll(&queue->buffer.pollq, EPOLLIN | EPOLLRDNORM);
225 }
226 EXPORT_SYMBOL_GPL(iio_dma_buffer_block_done);
227
228
229
230
231
232
233
234
235
236
237
238 void iio_dma_buffer_block_list_abort(struct iio_dma_buffer_queue *queue,
239 struct list_head *list)
240 {
241 struct iio_dma_buffer_block *block, *_block;
242 unsigned long flags;
243
244 spin_lock_irqsave(&queue->list_lock, flags);
245 list_for_each_entry_safe(block, _block, list, head) {
246 list_del(&block->head);
247 block->bytes_used = 0;
248 _iio_dma_buffer_block_done(block);
249 iio_buffer_block_put_atomic(block);
250 }
251 spin_unlock_irqrestore(&queue->list_lock, flags);
252
253 wake_up_interruptible_poll(&queue->buffer.pollq, EPOLLIN | EPOLLRDNORM);
254 }
255 EXPORT_SYMBOL_GPL(iio_dma_buffer_block_list_abort);
256
257 static bool iio_dma_block_reusable(struct iio_dma_buffer_block *block)
258 {
259
260
261
262
263
264 switch (block->state) {
265 case IIO_BLOCK_STATE_DEQUEUED:
266 case IIO_BLOCK_STATE_QUEUED:
267 case IIO_BLOCK_STATE_DONE:
268 return true;
269 default:
270 return false;
271 }
272 }
273
274
275
276
277
278
279
280
281 int iio_dma_buffer_request_update(struct iio_buffer *buffer)
282 {
283 struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
284 struct iio_dma_buffer_block *block;
285 bool try_reuse = false;
286 size_t size;
287 int ret = 0;
288 int i;
289
290
291
292
293
294
295 size = DIV_ROUND_UP(queue->buffer.bytes_per_datum *
296 queue->buffer.length, 2);
297
298 mutex_lock(&queue->lock);
299
300
301 if (PAGE_ALIGN(queue->fileio.block_size) == PAGE_ALIGN(size))
302 try_reuse = true;
303
304 queue->fileio.block_size = size;
305 queue->fileio.active_block = NULL;
306
307 spin_lock_irq(&queue->list_lock);
308 for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
309 block = queue->fileio.blocks[i];
310
311
312 if (block && (!iio_dma_block_reusable(block) || !try_reuse))
313 block->state = IIO_BLOCK_STATE_DEAD;
314 }
315
316
317
318
319
320
321 INIT_LIST_HEAD(&queue->outgoing);
322 spin_unlock_irq(&queue->list_lock);
323
324 INIT_LIST_HEAD(&queue->incoming);
325
326 for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
327 if (queue->fileio.blocks[i]) {
328 block = queue->fileio.blocks[i];
329 if (block->state == IIO_BLOCK_STATE_DEAD) {
330
331 iio_buffer_block_put(block);
332 block = NULL;
333 } else {
334 block->size = size;
335 }
336 } else {
337 block = NULL;
338 }
339
340 if (!block) {
341 block = iio_dma_buffer_alloc_block(queue, size);
342 if (!block) {
343 ret = -ENOMEM;
344 goto out_unlock;
345 }
346 queue->fileio.blocks[i] = block;
347 }
348
349 block->state = IIO_BLOCK_STATE_QUEUED;
350 list_add_tail(&block->head, &queue->incoming);
351 }
352
353 out_unlock:
354 mutex_unlock(&queue->lock);
355
356 return ret;
357 }
358 EXPORT_SYMBOL_GPL(iio_dma_buffer_request_update);
359
360 static void iio_dma_buffer_submit_block(struct iio_dma_buffer_queue *queue,
361 struct iio_dma_buffer_block *block)
362 {
363 int ret;
364
365
366
367
368
369
370 if (!queue->ops)
371 return;
372
373 block->state = IIO_BLOCK_STATE_ACTIVE;
374 iio_buffer_block_get(block);
375 ret = queue->ops->submit(queue, block);
376 if (ret) {
377
378
379
380
381
382
383
384
385
386
387 iio_buffer_block_put(block);
388 }
389 }
390
391
392
393
394
395
396
397
398
399
400
401 int iio_dma_buffer_enable(struct iio_buffer *buffer,
402 struct iio_dev *indio_dev)
403 {
404 struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
405 struct iio_dma_buffer_block *block, *_block;
406
407 mutex_lock(&queue->lock);
408 queue->active = true;
409 list_for_each_entry_safe(block, _block, &queue->incoming, head) {
410 list_del(&block->head);
411 iio_dma_buffer_submit_block(queue, block);
412 }
413 mutex_unlock(&queue->lock);
414
415 return 0;
416 }
417 EXPORT_SYMBOL_GPL(iio_dma_buffer_enable);
418
419
420
421
422
423
424
425
426
427 int iio_dma_buffer_disable(struct iio_buffer *buffer,
428 struct iio_dev *indio_dev)
429 {
430 struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
431
432 mutex_lock(&queue->lock);
433 queue->active = false;
434
435 if (queue->ops && queue->ops->abort)
436 queue->ops->abort(queue);
437 mutex_unlock(&queue->lock);
438
439 return 0;
440 }
441 EXPORT_SYMBOL_GPL(iio_dma_buffer_disable);
442
443 static void iio_dma_buffer_enqueue(struct iio_dma_buffer_queue *queue,
444 struct iio_dma_buffer_block *block)
445 {
446 if (block->state == IIO_BLOCK_STATE_DEAD) {
447 iio_buffer_block_put(block);
448 } else if (queue->active) {
449 iio_dma_buffer_submit_block(queue, block);
450 } else {
451 block->state = IIO_BLOCK_STATE_QUEUED;
452 list_add_tail(&block->head, &queue->incoming);
453 }
454 }
455
456 static struct iio_dma_buffer_block *iio_dma_buffer_dequeue(
457 struct iio_dma_buffer_queue *queue)
458 {
459 struct iio_dma_buffer_block *block;
460
461 spin_lock_irq(&queue->list_lock);
462 block = list_first_entry_or_null(&queue->outgoing, struct
463 iio_dma_buffer_block, head);
464 if (block != NULL) {
465 list_del(&block->head);
466 block->state = IIO_BLOCK_STATE_DEQUEUED;
467 }
468 spin_unlock_irq(&queue->list_lock);
469
470 return block;
471 }
472
473
474
475
476
477
478
479
480
481
482 int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n,
483 char __user *user_buffer)
484 {
485 struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
486 struct iio_dma_buffer_block *block;
487 int ret;
488
489 if (n < buffer->bytes_per_datum)
490 return -EINVAL;
491
492 mutex_lock(&queue->lock);
493
494 if (!queue->fileio.active_block) {
495 block = iio_dma_buffer_dequeue(queue);
496 if (block == NULL) {
497 ret = 0;
498 goto out_unlock;
499 }
500 queue->fileio.pos = 0;
501 queue->fileio.active_block = block;
502 } else {
503 block = queue->fileio.active_block;
504 }
505
506 n = rounddown(n, buffer->bytes_per_datum);
507 if (n > block->bytes_used - queue->fileio.pos)
508 n = block->bytes_used - queue->fileio.pos;
509
510 if (copy_to_user(user_buffer, block->vaddr + queue->fileio.pos, n)) {
511 ret = -EFAULT;
512 goto out_unlock;
513 }
514
515 queue->fileio.pos += n;
516
517 if (queue->fileio.pos == block->bytes_used) {
518 queue->fileio.active_block = NULL;
519 iio_dma_buffer_enqueue(queue, block);
520 }
521
522 ret = n;
523
524 out_unlock:
525 mutex_unlock(&queue->lock);
526
527 return ret;
528 }
529 EXPORT_SYMBOL_GPL(iio_dma_buffer_read);
530
531
532
533
534
535
536
537
538 size_t iio_dma_buffer_data_available(struct iio_buffer *buf)
539 {
540 struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buf);
541 struct iio_dma_buffer_block *block;
542 size_t data_available = 0;
543
544
545
546
547
548
549
550
551 mutex_lock(&queue->lock);
552 if (queue->fileio.active_block)
553 data_available += queue->fileio.active_block->size;
554
555 spin_lock_irq(&queue->list_lock);
556 list_for_each_entry(block, &queue->outgoing, head)
557 data_available += block->size;
558 spin_unlock_irq(&queue->list_lock);
559 mutex_unlock(&queue->lock);
560
561 return data_available;
562 }
563 EXPORT_SYMBOL_GPL(iio_dma_buffer_data_available);
564
565
566
567
568
569
570
571
572
573 int iio_dma_buffer_set_bytes_per_datum(struct iio_buffer *buffer, size_t bpd)
574 {
575 buffer->bytes_per_datum = bpd;
576
577 return 0;
578 }
579 EXPORT_SYMBOL_GPL(iio_dma_buffer_set_bytes_per_datum);
580
581
582
583
584
585
586
587
588
589 int iio_dma_buffer_set_length(struct iio_buffer *buffer, unsigned int length)
590 {
591
592 if (length < 2)
593 length = 2;
594 buffer->length = length;
595 buffer->watermark = length / 2;
596
597 return 0;
598 }
599 EXPORT_SYMBOL_GPL(iio_dma_buffer_set_length);
600
601
602
603
604
605
606
607
608
609
610
611 int iio_dma_buffer_init(struct iio_dma_buffer_queue *queue,
612 struct device *dev, const struct iio_dma_buffer_ops *ops)
613 {
614 iio_buffer_init(&queue->buffer);
615 queue->buffer.length = PAGE_SIZE;
616 queue->buffer.watermark = queue->buffer.length / 2;
617 queue->dev = dev;
618 queue->ops = ops;
619
620 INIT_LIST_HEAD(&queue->incoming);
621 INIT_LIST_HEAD(&queue->outgoing);
622
623 mutex_init(&queue->lock);
624 spin_lock_init(&queue->list_lock);
625
626 return 0;
627 }
628 EXPORT_SYMBOL_GPL(iio_dma_buffer_init);
629
630
631
632
633
634
635
636
637 void iio_dma_buffer_exit(struct iio_dma_buffer_queue *queue)
638 {
639 unsigned int i;
640
641 mutex_lock(&queue->lock);
642
643 spin_lock_irq(&queue->list_lock);
644 for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
645 if (!queue->fileio.blocks[i])
646 continue;
647 queue->fileio.blocks[i]->state = IIO_BLOCK_STATE_DEAD;
648 }
649 INIT_LIST_HEAD(&queue->outgoing);
650 spin_unlock_irq(&queue->list_lock);
651
652 INIT_LIST_HEAD(&queue->incoming);
653
654 for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
655 if (!queue->fileio.blocks[i])
656 continue;
657 iio_buffer_block_put(queue->fileio.blocks[i]);
658 queue->fileio.blocks[i] = NULL;
659 }
660 queue->fileio.active_block = NULL;
661 queue->ops = NULL;
662
663 mutex_unlock(&queue->lock);
664 }
665 EXPORT_SYMBOL_GPL(iio_dma_buffer_exit);
666
667
668
669
670
671
672
673
674
675 void iio_dma_buffer_release(struct iio_dma_buffer_queue *queue)
676 {
677 mutex_destroy(&queue->lock);
678 }
679 EXPORT_SYMBOL_GPL(iio_dma_buffer_release);
680
681 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
682 MODULE_DESCRIPTION("DMA buffer for the IIO framework");
683 MODULE_LICENSE("GPL v2");