diff options
author | Stefani Seibold <stefani@seibold.net> | 2010-08-10 21:03:39 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2010-08-11 11:59:23 -0400 |
commit | 5bf2b19320ec31d094d7370fdf536f7fd91fd799 (patch) | |
tree | 47052fd9bdb0969d4a4a7d430695388094409964 /samples/kfifo/dma-example.c | |
parent | 2e956fb320568cc70861761483e2f0e2db75fd66 (diff) |
kfifo: add example files to the kernel sample directory
Add four examples to the kernel sample directory.
It shows how to handle:
- a byte stream fifo
- a integer type fifo
- a dynamic record sized fifo
- the fifo DMA functions
[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: Stefani Seibold <stefani@seibold.net>
Cc: Greg KH <greg@kroah.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'samples/kfifo/dma-example.c')
-rw-r--r-- | samples/kfifo/dma-example.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/samples/kfifo/dma-example.c b/samples/kfifo/dma-example.c new file mode 100644 index 000000000000..b9482c28b41a --- /dev/null +++ b/samples/kfifo/dma-example.c | |||
@@ -0,0 +1,115 @@ | |||
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>"); | ||