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)
27 struct scatterlist sg[10];
29 printk(KERN_INFO "DMA fifo test start\n");
31 if (kfifo_alloc(&fifo, FIFO_SIZE, GFP_KERNEL)) {
32 printk(KERN_ERR "error kfifo_alloc\n");
36 printk(KERN_INFO "queue size: %u\n", kfifo_size(&fifo));
38 kfifo_in(&fifo, "test", 4);
40 for (i = 0; i != 9; i++)
43 /* kick away first byte */
44 ret = kfifo_get(&fifo, &i);
46 printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
48 sg_init_table(sg, ARRAY_SIZE(sg));
49 ret = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE);
50 printk(KERN_INFO "DMA sgl entries: %d\n", ret);
52 /* if 0 was returned, fifo is full and no sgl was created */
54 printk(KERN_INFO "scatterlist for receive:\n");
55 for (i = 0; i < ARRAY_SIZE(sg); i++) {
58 "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
59 i, sg[i].page_link, sg[i].offset, sg[i].length);
61 if (sg_is_last(&sg[i]))
65 /* but here your code to setup and exectute the dma operation */
68 /* example: zero bytes received */
71 /* finish the dma operation and update the received data */
72 kfifo_dma_in_finish(&fifo, ret);
75 ret = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8);
76 printk(KERN_INFO "DMA sgl entries: %d\n", ret);
78 /* if 0 was returned, no data was available and no sgl was created */
80 printk(KERN_INFO "scatterlist for transmit:\n");
81 for (i = 0; i < ARRAY_SIZE(sg); i++) {
84 "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
85 i, sg[i].page_link, sg[i].offset, sg[i].length);
87 if (sg_is_last(&sg[i]))
91 /* but here your code to setup and exectute the dma operation */
94 /* example: 5 bytes transmitted */
97 /* finish the dma operation and update the transmitted data */
98 kfifo_dma_out_finish(&fifo, ret);
101 printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
106 static void __exit example_exit(void)
111 module_init(example_init);
112 module_exit(example_exit);
113 MODULE_LICENSE("GPL");
114 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");