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