xref: /linux/samples/kfifo/dma-example.c (revision d39d0ed196aa1685bb24771e92f78633c66ac9cb)
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)) {
32 		printk(KERN_ERR "error kfifo_alloc\n");
33 		return 1;
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 */
44 	ret = kfifo_get(&fifo, &i);
45 
46 	printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
47 
48 	ret = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE);
49 	printk(KERN_INFO "DMA sgl entries: %d\n", ret);
50 
51 	/* if 0 was returned, fifo is full and no sgl was created */
52 	if (ret) {
53 		printk(KERN_INFO "scatterlist for receive:\n");
54 		for (i = 0; i < ARRAY_SIZE(sg); i++) {
55 			printk(KERN_INFO
56 			"sg[%d] -> "
57 			"page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
58 				i, sg[i].page_link, sg[i].offset, sg[i].length);
59 
60 			if (sg_is_last(&sg[i]))
61 				break;
62 		}
63 
64 		/* but here your code to setup and exectute the dma operation */
65 		/* ... */
66 
67 		/* example: zero bytes received */
68 		ret = 0;
69 
70 		/* finish the dma operation and update the received data */
71 		kfifo_dma_in_finish(&fifo, ret);
72 	}
73 
74 	ret = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8);
75 	printk(KERN_INFO "DMA sgl entries: %d\n", ret);
76 
77 	/* if 0 was returned, no data was available and no sgl was created */
78 	if (ret) {
79 		printk(KERN_INFO "scatterlist for transmit:\n");
80 		for (i = 0; i < ARRAY_SIZE(sg); i++) {
81 			printk(KERN_INFO
82 			"sg[%d] -> "
83 			"page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
84 				i, sg[i].page_link, sg[i].offset, sg[i].length);
85 
86 			if (sg_is_last(&sg[i]))
87 				break;
88 		}
89 
90 		/* but here your code to setup and exectute the dma operation */
91 		/* ... */
92 
93 		/* example: 5 bytes transmitted */
94 		ret = 5;
95 
96 		/* finish the dma operation and update the transmitted data */
97 		kfifo_dma_out_finish(&fifo, ret);
98 	}
99 
100 	printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
101 
102 	return 0;
103 }
104 
105 static void __exit example_exit(void)
106 {
107 #ifdef DYNAMIC
108 	kfifo_free(&test);
109 #endif
110 }
111 
112 module_init(example_init);
113 module_exit(example_exit);
114 MODULE_LICENSE("GPL");
115 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");
116