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 ret = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE);
49 printk(KERN_INFO "DMA sgl entries: %d\n", ret);
51 /* if 0 was returned, fifo is full and no sgl was created */
53 printk(KERN_INFO "scatterlist for receive:\n");
54 for (i = 0; i < ARRAY_SIZE(sg); i++) {
57 "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
58 i, sg[i].page_link, sg[i].offset, sg[i].length);
60 if (sg_is_last(&sg[i]))
64 /* but here your code to setup and exectute the dma operation */
67 /* example: zero bytes received */
70 /* finish the dma operation and update the received data */
71 kfifo_dma_in_finish(&fifo, ret);
74 ret = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8);
75 printk(KERN_INFO "DMA sgl entries: %d\n", ret);
77 /* if 0 was returned, no data was available and no sgl was created */
79 printk(KERN_INFO "scatterlist for transmit:\n");
80 for (i = 0; i < ARRAY_SIZE(sg); i++) {
83 "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
84 i, sg[i].page_link, sg[i].offset, sg[i].length);
86 if (sg_is_last(&sg[i]))
90 /* but here your code to setup and exectute the dma operation */
93 /* example: 5 bytes transmitted */
96 /* finish the dma operation and update the transmitted data */
97 kfifo_dma_out_finish(&fifo, ret);
100 printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
105 static void __exit example_exit(void)
112 module_init(example_init);
113 module_exit(example_exit);
114 MODULE_LICENSE("GPL");
115 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");