Thomas Gleixner | 912d0f0b | 2019-06-04 10:10:58 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Stefani Seibold | 5bf2b19 | 2010-08-10 18:03:39 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Sample fifo dma implementation |
| 4 | * |
| 5 | * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net> |
Stefani Seibold | 5bf2b19 | 2010-08-10 18:03:39 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/kfifo.h> |
| 11 | |
| 12 | /* |
| 13 | * This module shows how to handle fifo dma operations. |
| 14 | */ |
| 15 | |
| 16 | /* fifo size in elements (bytes) */ |
| 17 | #define FIFO_SIZE 32 |
| 18 | |
| 19 | static struct kfifo fifo; |
| 20 | |
| 21 | static int __init example_init(void) |
| 22 | { |
| 23 | int i; |
| 24 | unsigned int ret; |
Ira W. Snyder | 399f1e3 | 2010-09-30 15:15:27 -0700 | [diff] [blame] | 25 | unsigned int nents; |
Stefani Seibold | 5bf2b19 | 2010-08-10 18:03:39 -0700 | [diff] [blame] | 26 | struct scatterlist sg[10]; |
| 27 | |
| 28 | printk(KERN_INFO "DMA fifo test start\n"); |
| 29 | |
| 30 | if (kfifo_alloc(&fifo, FIFO_SIZE, GFP_KERNEL)) { |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 31 | printk(KERN_WARNING "error kfifo_alloc\n"); |
| 32 | return -ENOMEM; |
Stefani Seibold | 5bf2b19 | 2010-08-10 18:03:39 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | printk(KERN_INFO "queue size: %u\n", kfifo_size(&fifo)); |
| 36 | |
| 37 | kfifo_in(&fifo, "test", 4); |
| 38 | |
| 39 | for (i = 0; i != 9; i++) |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 40 | kfifo_put(&fifo, i); |
Stefani Seibold | 5bf2b19 | 2010-08-10 18:03:39 -0700 | [diff] [blame] | 41 | |
| 42 | /* kick away first byte */ |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 43 | kfifo_skip(&fifo); |
Stefani Seibold | 5bf2b19 | 2010-08-10 18:03:39 -0700 | [diff] [blame] | 44 | |
| 45 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo)); |
| 46 | |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Configure the kfifo buffer to receive data from DMA input. |
| 49 | * |
| 50 | * .--------------------------------------. |
| 51 | * | 0 | 1 | 2 | ... | 12 | 13 | ... | 31 | |
| 52 | * |---|------------------|---------------| |
| 53 | * \_/ \________________/ \_____________/ |
| 54 | * \ \ \ |
| 55 | * \ \_allocated data \ |
| 56 | * \_*free space* \_*free space* |
| 57 | * |
| 58 | * We need two different SG entries: one for the free space area at the |
| 59 | * end of the kfifo buffer (19 bytes) and another for the first free |
| 60 | * byte at the beginning, after the kfifo_skip(). |
| 61 | */ |
Andrea Righi | 7b34d52 | 2010-08-19 14:13:29 -0700 | [diff] [blame] | 62 | sg_init_table(sg, ARRAY_SIZE(sg)); |
Ira W. Snyder | 399f1e3 | 2010-09-30 15:15:27 -0700 | [diff] [blame] | 63 | nents = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE); |
| 64 | printk(KERN_INFO "DMA sgl entries: %d\n", nents); |
| 65 | if (!nents) { |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 66 | /* fifo is full and no sgl was created */ |
| 67 | printk(KERN_WARNING "error kfifo_dma_in_prepare\n"); |
| 68 | return -EIO; |
Stefani Seibold | 5bf2b19 | 2010-08-10 18:03:39 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 71 | /* receive data */ |
| 72 | printk(KERN_INFO "scatterlist for receive:\n"); |
Ira W. Snyder | 399f1e3 | 2010-09-30 15:15:27 -0700 | [diff] [blame] | 73 | for (i = 0; i < nents; i++) { |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 74 | printk(KERN_INFO |
| 75 | "sg[%d] -> " |
Logan Gunthorpe | 9263969 | 2017-07-12 14:34:22 -0700 | [diff] [blame] | 76 | "page %p offset 0x%.8x length 0x%.8x\n", |
| 77 | i, sg_page(&sg[i]), sg[i].offset, sg[i].length); |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 78 | |
| 79 | if (sg_is_last(&sg[i])) |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | /* put here your code to setup and exectute the dma operation */ |
| 84 | /* ... */ |
| 85 | |
| 86 | /* example: zero bytes received */ |
| 87 | ret = 0; |
| 88 | |
| 89 | /* finish the dma operation and update the received data */ |
| 90 | kfifo_dma_in_finish(&fifo, ret); |
| 91 | |
| 92 | /* Prepare to transmit data, example: 8 bytes */ |
Ira W. Snyder | 399f1e3 | 2010-09-30 15:15:27 -0700 | [diff] [blame] | 93 | nents = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8); |
| 94 | printk(KERN_INFO "DMA sgl entries: %d\n", nents); |
| 95 | if (!nents) { |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 96 | /* no data was available and no sgl was created */ |
| 97 | printk(KERN_WARNING "error kfifo_dma_out_prepare\n"); |
| 98 | return -EIO; |
Stefani Seibold | 5bf2b19 | 2010-08-10 18:03:39 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 101 | printk(KERN_INFO "scatterlist for transmit:\n"); |
Ira W. Snyder | 399f1e3 | 2010-09-30 15:15:27 -0700 | [diff] [blame] | 102 | for (i = 0; i < nents; i++) { |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 103 | printk(KERN_INFO |
| 104 | "sg[%d] -> " |
Logan Gunthorpe | 9263969 | 2017-07-12 14:34:22 -0700 | [diff] [blame] | 105 | "page %p offset 0x%.8x length 0x%.8x\n", |
| 106 | i, sg_page(&sg[i]), sg[i].offset, sg[i].length); |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 107 | |
| 108 | if (sg_is_last(&sg[i])) |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | /* put here your code to setup and exectute the dma operation */ |
| 113 | /* ... */ |
| 114 | |
| 115 | /* example: 5 bytes transmitted */ |
| 116 | ret = 5; |
| 117 | |
| 118 | /* finish the dma operation and update the transmitted data */ |
| 119 | kfifo_dma_out_finish(&fifo, ret); |
| 120 | |
| 121 | ret = kfifo_len(&fifo); |
Stefani Seibold | 5bf2b19 | 2010-08-10 18:03:39 -0700 | [diff] [blame] | 122 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo)); |
| 123 | |
Andrea Righi | a25effa | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 124 | if (ret != 7) { |
| 125 | printk(KERN_WARNING "size mismatch: test failed"); |
| 126 | return -EIO; |
| 127 | } |
| 128 | printk(KERN_INFO "test passed\n"); |
| 129 | |
Stefani Seibold | 5bf2b19 | 2010-08-10 18:03:39 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static void __exit example_exit(void) |
| 134 | { |
Andrea Righi | d83a71c | 2010-08-19 14:13:30 -0700 | [diff] [blame] | 135 | kfifo_free(&fifo); |
Stefani Seibold | 5bf2b19 | 2010-08-10 18:03:39 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | module_init(example_init); |
| 139 | module_exit(example_exit); |
| 140 | MODULE_LICENSE("GPL"); |
| 141 | MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>"); |