This source file includes following definitions.
- example_init
- example_exit
1
2
3
4
5
6
7
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/kfifo.h>
11
12
13
14
15
16
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;
25 unsigned int nents;
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)) {
31 printk(KERN_WARNING "error kfifo_alloc\n");
32 return -ENOMEM;
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++)
40 kfifo_put(&fifo, i);
41
42
43 kfifo_skip(&fifo);
44
45 printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 sg_init_table(sg, ARRAY_SIZE(sg));
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) {
66
67 printk(KERN_WARNING "error kfifo_dma_in_prepare\n");
68 return -EIO;
69 }
70
71
72 printk(KERN_INFO "scatterlist for receive:\n");
73 for (i = 0; i < nents; i++) {
74 printk(KERN_INFO
75 "sg[%d] -> "
76 "page %p offset 0x%.8x length 0x%.8x\n",
77 i, sg_page(&sg[i]), sg[i].offset, sg[i].length);
78
79 if (sg_is_last(&sg[i]))
80 break;
81 }
82
83
84
85
86
87 ret = 0;
88
89
90 kfifo_dma_in_finish(&fifo, ret);
91
92
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) {
96
97 printk(KERN_WARNING "error kfifo_dma_out_prepare\n");
98 return -EIO;
99 }
100
101 printk(KERN_INFO "scatterlist for transmit:\n");
102 for (i = 0; i < nents; i++) {
103 printk(KERN_INFO
104 "sg[%d] -> "
105 "page %p offset 0x%.8x length 0x%.8x\n",
106 i, sg_page(&sg[i]), sg[i].offset, sg[i].length);
107
108 if (sg_is_last(&sg[i]))
109 break;
110 }
111
112
113
114
115
116 ret = 5;
117
118
119 kfifo_dma_out_finish(&fifo, ret);
120
121 ret = kfifo_len(&fifo);
122 printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
123
124 if (ret != 7) {
125 printk(KERN_WARNING "size mismatch: test failed");
126 return -EIO;
127 }
128 printk(KERN_INFO "test passed\n");
129
130 return 0;
131 }
132
133 static void __exit example_exit(void)
134 {
135 kfifo_free(&fifo);
136 }
137
138 module_init(example_init);
139 module_exit(example_exit);
140 MODULE_LICENSE("GPL");
141 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");