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