2 * Sample fifo dma implementation
4 * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
6 * Released under the GPL version 2 only.
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/kfifo.h>
15 * This module shows how to handle fifo dma operations.
18 /* fifo size in elements (bytes) */
21 static struct kfifo fifo
;
23 static int __init
example_init(void)
28 struct scatterlist sg
[10];
30 printk(KERN_INFO
"DMA fifo test start\n");
32 if (kfifo_alloc(&fifo
, FIFO_SIZE
, GFP_KERNEL
)) {
33 printk(KERN_WARNING
"error kfifo_alloc\n");
37 printk(KERN_INFO
"queue size: %u\n", kfifo_size(&fifo
));
39 kfifo_in(&fifo
, "test", 4);
41 for (i
= 0; i
!= 9; i
++)
44 /* kick away first byte */
47 printk(KERN_INFO
"queue len: %u\n", kfifo_len(&fifo
));
50 * Configure the kfifo buffer to receive data from DMA input.
52 * .--------------------------------------.
53 * | 0 | 1 | 2 | ... | 12 | 13 | ... | 31 |
54 * |---|------------------|---------------|
55 * \_/ \________________/ \_____________/
57 * \ \_allocated data \
58 * \_*free space* \_*free space*
60 * We need two different SG entries: one for the free space area at the
61 * end of the kfifo buffer (19 bytes) and another for the first free
62 * byte at the beginning, after the kfifo_skip().
64 sg_init_table(sg
, ARRAY_SIZE(sg
));
65 nents
= kfifo_dma_in_prepare(&fifo
, sg
, ARRAY_SIZE(sg
), FIFO_SIZE
);
66 printk(KERN_INFO
"DMA sgl entries: %d\n", nents
);
68 /* fifo is full and no sgl was created */
69 printk(KERN_WARNING
"error kfifo_dma_in_prepare\n");
74 printk(KERN_INFO
"scatterlist for receive:\n");
75 for (i
= 0; i
< nents
; i
++) {
78 "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
79 i
, sg
[i
].page_link
, sg
[i
].offset
, sg
[i
].length
);
81 if (sg_is_last(&sg
[i
]))
85 /* put here your code to setup and exectute the dma operation */
88 /* example: zero bytes received */
91 /* finish the dma operation and update the received data */
92 kfifo_dma_in_finish(&fifo
, ret
);
94 /* Prepare to transmit data, example: 8 bytes */
95 nents
= kfifo_dma_out_prepare(&fifo
, sg
, ARRAY_SIZE(sg
), 8);
96 printk(KERN_INFO
"DMA sgl entries: %d\n", nents
);
98 /* no data was available and no sgl was created */
99 printk(KERN_WARNING
"error kfifo_dma_out_prepare\n");
103 printk(KERN_INFO
"scatterlist for transmit:\n");
104 for (i
= 0; i
< nents
; i
++) {
107 "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
108 i
, sg
[i
].page_link
, sg
[i
].offset
, sg
[i
].length
);
110 if (sg_is_last(&sg
[i
]))
114 /* put here your code to setup and exectute the dma operation */
117 /* example: 5 bytes transmitted */
120 /* finish the dma operation and update the transmitted data */
121 kfifo_dma_out_finish(&fifo
, ret
);
123 ret
= kfifo_len(&fifo
);
124 printk(KERN_INFO
"queue len: %u\n", kfifo_len(&fifo
));
127 printk(KERN_WARNING
"size mismatch: test failed");
130 printk(KERN_INFO
"test passed\n");
135 static void __exit
example_exit(void)
140 module_init(example_init
);
141 module_exit(example_exit
);
142 MODULE_LICENSE("GPL");
143 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");