diff options
Diffstat (limited to 'samples')
| -rw-r--r-- | samples/kfifo/bytestream-example.c | 42 | ||||
| -rw-r--r-- | samples/kfifo/dma-example.c | 111 | ||||
| -rw-r--r-- | samples/kfifo/inttype-example.c | 43 | ||||
| -rw-r--r-- | samples/kfifo/record-example.c | 39 |
4 files changed, 176 insertions, 59 deletions
diff --git a/samples/kfifo/bytestream-example.c b/samples/kfifo/bytestream-example.c index 642eef3f6336..178061e87ffe 100644 --- a/samples/kfifo/bytestream-example.c +++ b/samples/kfifo/bytestream-example.c | |||
| @@ -44,10 +44,17 @@ static struct kfifo test; | |||
| 44 | static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE); | 44 | static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE); |
| 45 | #endif | 45 | #endif |
| 46 | 46 | ||
| 47 | static const unsigned char expected_result[FIFO_SIZE] = { | ||
| 48 | 3, 4, 5, 6, 7, 8, 9, 0, | ||
| 49 | 1, 20, 21, 22, 23, 24, 25, 26, | ||
| 50 | 27, 28, 29, 30, 31, 32, 33, 34, | ||
| 51 | 35, 36, 37, 38, 39, 40, 41, 42, | ||
| 52 | }; | ||
| 53 | |||
| 47 | static int __init testfunc(void) | 54 | static int __init testfunc(void) |
| 48 | { | 55 | { |
| 49 | unsigned char buf[6]; | 56 | unsigned char buf[6]; |
| 50 | unsigned char i; | 57 | unsigned char i, j; |
| 51 | unsigned int ret; | 58 | unsigned int ret; |
| 52 | 59 | ||
| 53 | printk(KERN_INFO "byte stream fifo test start\n"); | 60 | printk(KERN_INFO "byte stream fifo test start\n"); |
| @@ -73,16 +80,34 @@ static int __init testfunc(void) | |||
| 73 | ret = kfifo_in(&test, buf, ret); | 80 | ret = kfifo_in(&test, buf, ret); |
| 74 | printk(KERN_INFO "ret: %d\n", ret); | 81 | printk(KERN_INFO "ret: %d\n", ret); |
| 75 | 82 | ||
| 83 | /* skip first element of the fifo */ | ||
| 84 | printk(KERN_INFO "skip 1st element\n"); | ||
| 85 | kfifo_skip(&test); | ||
| 86 | |||
| 76 | /* put values into the fifo until is full */ | 87 | /* put values into the fifo until is full */ |
| 77 | for (i = 20; kfifo_put(&test, &i); i++) | 88 | for (i = 20; kfifo_put(&test, &i); i++) |
| 78 | ; | 89 | ; |
| 79 | 90 | ||
| 80 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&test)); | 91 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&test)); |
| 81 | 92 | ||
| 82 | /* print out all values in the fifo */ | 93 | /* show the first value without removing from the fifo */ |
| 83 | while (kfifo_get(&test, &i)) | 94 | if (kfifo_peek(&test, &i)) |
| 84 | printk("%d ", i); | 95 | printk(KERN_INFO "%d\n", i); |
| 85 | printk("\n"); | 96 | |
| 97 | /* check the correctness of all values in the fifo */ | ||
| 98 | j = 0; | ||
| 99 | while (kfifo_get(&test, &i)) { | ||
| 100 | printk(KERN_INFO "item = %d\n", i); | ||
| 101 | if (i != expected_result[j++]) { | ||
| 102 | printk(KERN_WARNING "value mismatch: test failed\n"); | ||
| 103 | return -EIO; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | if (j != ARRAY_SIZE(expected_result)) { | ||
| 107 | printk(KERN_WARNING "size mismatch: test failed\n"); | ||
| 108 | return -EIO; | ||
| 109 | } | ||
| 110 | printk(KERN_INFO "test passed\n"); | ||
| 86 | 111 | ||
| 87 | return 0; | 112 | return 0; |
| 88 | } | 113 | } |
| @@ -138,7 +163,12 @@ static int __init example_init(void) | |||
| 138 | #else | 163 | #else |
| 139 | INIT_KFIFO(test); | 164 | INIT_KFIFO(test); |
| 140 | #endif | 165 | #endif |
| 141 | testfunc(); | 166 | if (testfunc() < 0) { |
| 167 | #ifdef DYNAMIC | ||
| 168 | kfifo_free(&test); | ||
| 169 | #endif | ||
| 170 | return -EIO; | ||
| 171 | } | ||
| 142 | 172 | ||
| 143 | if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { | 173 | if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { |
| 144 | #ifdef DYNAMIC | 174 | #ifdef DYNAMIC |
diff --git a/samples/kfifo/dma-example.c b/samples/kfifo/dma-example.c index b9482c28b41a..ee03a4f0b64f 100644 --- a/samples/kfifo/dma-example.c +++ b/samples/kfifo/dma-example.c | |||
| @@ -29,8 +29,8 @@ static int __init example_init(void) | |||
| 29 | printk(KERN_INFO "DMA fifo test start\n"); | 29 | printk(KERN_INFO "DMA fifo test start\n"); |
| 30 | 30 | ||
| 31 | if (kfifo_alloc(&fifo, FIFO_SIZE, GFP_KERNEL)) { | 31 | if (kfifo_alloc(&fifo, FIFO_SIZE, GFP_KERNEL)) { |
| 32 | printk(KERN_ERR "error kfifo_alloc\n"); | 32 | printk(KERN_WARNING "error kfifo_alloc\n"); |
| 33 | return 1; | 33 | return -ENOMEM; |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | printk(KERN_INFO "queue size: %u\n", kfifo_size(&fifo)); | 36 | printk(KERN_INFO "queue size: %u\n", kfifo_size(&fifo)); |
| @@ -41,72 +41,99 @@ static int __init example_init(void) | |||
| 41 | kfifo_put(&fifo, &i); | 41 | kfifo_put(&fifo, &i); |
| 42 | 42 | ||
| 43 | /* kick away first byte */ | 43 | /* kick away first byte */ |
| 44 | ret = kfifo_get(&fifo, &i); | 44 | kfifo_skip(&fifo); |
| 45 | 45 | ||
| 46 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo)); | 46 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo)); |
| 47 | 47 | ||
| 48 | /* | ||
| 49 | * Configure the kfifo buffer to receive data from DMA input. | ||
| 50 | * | ||
| 51 | * .--------------------------------------. | ||
| 52 | * | 0 | 1 | 2 | ... | 12 | 13 | ... | 31 | | ||
| 53 | * |---|------------------|---------------| | ||
| 54 | * \_/ \________________/ \_____________/ | ||
| 55 | * \ \ \ | ||
| 56 | * \ \_allocated data \ | ||
| 57 | * \_*free space* \_*free space* | ||
| 58 | * | ||
| 59 | * We need two different SG entries: one for the free space area at the | ||
| 60 | * end of the kfifo buffer (19 bytes) and another for the first free | ||
| 61 | * byte at the beginning, after the kfifo_skip(). | ||
| 62 | */ | ||
| 63 | sg_init_table(sg, ARRAY_SIZE(sg)); | ||
| 48 | ret = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE); | 64 | ret = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE); |
| 49 | printk(KERN_INFO "DMA sgl entries: %d\n", ret); | 65 | printk(KERN_INFO "DMA sgl entries: %d\n", ret); |
| 66 | if (!ret) { | ||
| 67 | /* fifo is full and no sgl was created */ | ||
| 68 | printk(KERN_WARNING "error kfifo_dma_in_prepare\n"); | ||
| 69 | return -EIO; | ||
| 70 | } | ||
| 50 | 71 | ||
| 51 | /* if 0 was returned, fifo is full and no sgl was created */ | 72 | /* receive data */ |
| 52 | if (ret) { | 73 | printk(KERN_INFO "scatterlist for receive:\n"); |
| 53 | printk(KERN_INFO "scatterlist for receive:\n"); | 74 | for (i = 0; i < ARRAY_SIZE(sg); i++) { |
| 54 | for (i = 0; i < ARRAY_SIZE(sg); i++) { | 75 | printk(KERN_INFO |
| 55 | printk(KERN_INFO | 76 | "sg[%d] -> " |
| 56 | "sg[%d] -> " | 77 | "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n", |
| 57 | "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n", | 78 | i, sg[i].page_link, sg[i].offset, sg[i].length); |
| 58 | i, sg[i].page_link, sg[i].offset, sg[i].length); | ||
| 59 | 79 | ||
| 60 | if (sg_is_last(&sg[i])) | 80 | if (sg_is_last(&sg[i])) |
| 61 | break; | 81 | break; |
| 62 | } | 82 | } |
| 63 | 83 | ||
| 64 | /* but here your code to setup and exectute the dma operation */ | 84 | /* put here your code to setup and exectute the dma operation */ |
| 65 | /* ... */ | 85 | /* ... */ |
| 66 | 86 | ||
| 67 | /* example: zero bytes received */ | 87 | /* example: zero bytes received */ |
| 68 | ret = 0; | 88 | ret = 0; |
| 69 | 89 | ||
| 70 | /* finish the dma operation and update the received data */ | 90 | /* finish the dma operation and update the received data */ |
| 71 | kfifo_dma_in_finish(&fifo, ret); | 91 | kfifo_dma_in_finish(&fifo, ret); |
| 72 | } | ||
| 73 | 92 | ||
| 93 | /* Prepare to transmit data, example: 8 bytes */ | ||
| 74 | ret = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8); | 94 | ret = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8); |
| 75 | printk(KERN_INFO "DMA sgl entries: %d\n", ret); | 95 | printk(KERN_INFO "DMA sgl entries: %d\n", ret); |
| 96 | if (!ret) { | ||
| 97 | /* no data was available and no sgl was created */ | ||
| 98 | printk(KERN_WARNING "error kfifo_dma_out_prepare\n"); | ||
| 99 | return -EIO; | ||
| 100 | } | ||
| 76 | 101 | ||
| 77 | /* if 0 was returned, no data was available and no sgl was created */ | 102 | printk(KERN_INFO "scatterlist for transmit:\n"); |
| 78 | if (ret) { | 103 | for (i = 0; i < ARRAY_SIZE(sg); i++) { |
| 79 | printk(KERN_INFO "scatterlist for transmit:\n"); | 104 | printk(KERN_INFO |
| 80 | for (i = 0; i < ARRAY_SIZE(sg); i++) { | 105 | "sg[%d] -> " |
| 81 | printk(KERN_INFO | 106 | "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n", |
| 82 | "sg[%d] -> " | 107 | i, sg[i].page_link, sg[i].offset, sg[i].length); |
| 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 | 108 | ||
| 86 | if (sg_is_last(&sg[i])) | 109 | if (sg_is_last(&sg[i])) |
| 87 | break; | 110 | break; |
| 88 | } | 111 | } |
| 89 | 112 | ||
| 90 | /* but here your code to setup and exectute the dma operation */ | 113 | /* put here your code to setup and exectute the dma operation */ |
| 91 | /* ... */ | 114 | /* ... */ |
| 92 | 115 | ||
| 93 | /* example: 5 bytes transmitted */ | 116 | /* example: 5 bytes transmitted */ |
| 94 | ret = 5; | 117 | ret = 5; |
| 95 | 118 | ||
| 96 | /* finish the dma operation and update the transmitted data */ | 119 | /* finish the dma operation and update the transmitted data */ |
| 97 | kfifo_dma_out_finish(&fifo, ret); | 120 | kfifo_dma_out_finish(&fifo, ret); |
| 98 | } | ||
| 99 | 121 | ||
| 122 | ret = kfifo_len(&fifo); | ||
| 100 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo)); | 123 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo)); |
| 101 | 124 | ||
| 125 | if (ret != 7) { | ||
| 126 | printk(KERN_WARNING "size mismatch: test failed"); | ||
| 127 | return -EIO; | ||
| 128 | } | ||
| 129 | printk(KERN_INFO "test passed\n"); | ||
| 130 | |||
| 102 | return 0; | 131 | return 0; |
| 103 | } | 132 | } |
| 104 | 133 | ||
| 105 | static void __exit example_exit(void) | 134 | static void __exit example_exit(void) |
| 106 | { | 135 | { |
| 107 | #ifdef DYNAMIC | 136 | kfifo_free(&fifo); |
| 108 | kfifo_free(&test); | ||
| 109 | #endif | ||
| 110 | } | 137 | } |
| 111 | 138 | ||
| 112 | module_init(example_init); | 139 | module_init(example_init); |
diff --git a/samples/kfifo/inttype-example.c b/samples/kfifo/inttype-example.c index d6c5b7d9df64..71b2aabca96a 100644 --- a/samples/kfifo/inttype-example.c +++ b/samples/kfifo/inttype-example.c | |||
| @@ -44,10 +44,17 @@ static DECLARE_KFIFO_PTR(test, int); | |||
| 44 | static DEFINE_KFIFO(test, int, FIFO_SIZE); | 44 | static DEFINE_KFIFO(test, int, FIFO_SIZE); |
| 45 | #endif | 45 | #endif |
| 46 | 46 | ||
| 47 | static const int expected_result[FIFO_SIZE] = { | ||
| 48 | 3, 4, 5, 6, 7, 8, 9, 0, | ||
| 49 | 1, 20, 21, 22, 23, 24, 25, 26, | ||
| 50 | 27, 28, 29, 30, 31, 32, 33, 34, | ||
| 51 | 35, 36, 37, 38, 39, 40, 41, 42, | ||
| 52 | }; | ||
| 53 | |||
| 47 | static int __init testfunc(void) | 54 | static int __init testfunc(void) |
| 48 | { | 55 | { |
| 49 | int buf[6]; | 56 | int buf[6]; |
| 50 | int i; | 57 | int i, j; |
| 51 | unsigned int ret; | 58 | unsigned int ret; |
| 52 | 59 | ||
| 53 | printk(KERN_INFO "int fifo test start\n"); | 60 | printk(KERN_INFO "int fifo test start\n"); |
| @@ -66,8 +73,13 @@ static int __init testfunc(void) | |||
| 66 | ret = kfifo_in(&test, buf, ret); | 73 | ret = kfifo_in(&test, buf, ret); |
| 67 | printk(KERN_INFO "ret: %d\n", ret); | 74 | printk(KERN_INFO "ret: %d\n", ret); |
| 68 | 75 | ||
| 69 | for (i = 20; i != 30; i++) | 76 | /* skip first element of the fifo */ |
| 70 | kfifo_put(&test, &i); | 77 | printk(KERN_INFO "skip 1st element\n"); |
| 78 | kfifo_skip(&test); | ||
| 79 | |||
| 80 | /* put values into the fifo until is full */ | ||
| 81 | for (i = 20; kfifo_put(&test, &i); i++) | ||
| 82 | ; | ||
| 71 | 83 | ||
| 72 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&test)); | 84 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&test)); |
| 73 | 85 | ||
| @@ -75,10 +87,20 @@ static int __init testfunc(void) | |||
| 75 | if (kfifo_peek(&test, &i)) | 87 | if (kfifo_peek(&test, &i)) |
| 76 | printk(KERN_INFO "%d\n", i); | 88 | printk(KERN_INFO "%d\n", i); |
| 77 | 89 | ||
| 78 | /* print out all values in the fifo */ | 90 | /* check the correctness of all values in the fifo */ |
| 79 | while (kfifo_get(&test, &i)) | 91 | j = 0; |
| 80 | printk("%d ", i); | 92 | while (kfifo_get(&test, &i)) { |
| 81 | printk("\n"); | 93 | printk(KERN_INFO "item = %d\n", i); |
| 94 | if (i != expected_result[j++]) { | ||
| 95 | printk(KERN_WARNING "value mismatch: test failed\n"); | ||
| 96 | return -EIO; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | if (j != ARRAY_SIZE(expected_result)) { | ||
| 100 | printk(KERN_WARNING "size mismatch: test failed\n"); | ||
| 101 | return -EIO; | ||
| 102 | } | ||
| 103 | printk(KERN_INFO "test passed\n"); | ||
| 82 | 104 | ||
| 83 | return 0; | 105 | return 0; |
| 84 | } | 106 | } |
| @@ -132,7 +154,12 @@ static int __init example_init(void) | |||
| 132 | return ret; | 154 | return ret; |
| 133 | } | 155 | } |
| 134 | #endif | 156 | #endif |
| 135 | testfunc(); | 157 | if (testfunc() < 0) { |
| 158 | #ifdef DYNAMIC | ||
| 159 | kfifo_free(&test); | ||
| 160 | #endif | ||
| 161 | return -EIO; | ||
| 162 | } | ||
| 136 | 163 | ||
| 137 | if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { | 164 | if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { |
| 138 | #ifdef DYNAMIC | 165 | #ifdef DYNAMIC |
diff --git a/samples/kfifo/record-example.c b/samples/kfifo/record-example.c index 32c6e0bda744..e68bd16a5da4 100644 --- a/samples/kfifo/record-example.c +++ b/samples/kfifo/record-example.c | |||
| @@ -55,6 +55,19 @@ typedef STRUCT_KFIFO_REC_1(FIFO_SIZE) mytest; | |||
| 55 | static mytest test; | 55 | static mytest test; |
| 56 | #endif | 56 | #endif |
| 57 | 57 | ||
| 58 | static const char *expected_result[] = { | ||
| 59 | "a", | ||
| 60 | "bb", | ||
| 61 | "ccc", | ||
| 62 | "dddd", | ||
| 63 | "eeeee", | ||
| 64 | "ffffff", | ||
| 65 | "ggggggg", | ||
| 66 | "hhhhhhhh", | ||
| 67 | "iiiiiiiii", | ||
| 68 | "jjjjjjjjjj", | ||
| 69 | }; | ||
| 70 | |||
| 58 | static int __init testfunc(void) | 71 | static int __init testfunc(void) |
| 59 | { | 72 | { |
| 60 | char buf[100]; | 73 | char buf[100]; |
| @@ -75,6 +88,10 @@ static int __init testfunc(void) | |||
| 75 | kfifo_in(&test, buf, i + 1); | 88 | kfifo_in(&test, buf, i + 1); |
| 76 | } | 89 | } |
| 77 | 90 | ||
| 91 | /* skip first element of the fifo */ | ||
| 92 | printk(KERN_INFO "skip 1st element\n"); | ||
| 93 | kfifo_skip(&test); | ||
| 94 | |||
| 78 | printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test)); | 95 | printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test)); |
| 79 | 96 | ||
| 80 | /* show the first record without removing from the fifo */ | 97 | /* show the first record without removing from the fifo */ |
| @@ -82,11 +99,22 @@ static int __init testfunc(void) | |||
| 82 | if (ret) | 99 | if (ret) |
| 83 | printk(KERN_INFO "%.*s\n", ret, buf); | 100 | printk(KERN_INFO "%.*s\n", ret, buf); |
| 84 | 101 | ||
| 85 | /* print out all records in the fifo */ | 102 | /* check the correctness of all values in the fifo */ |
| 103 | i = 0; | ||
| 86 | while (!kfifo_is_empty(&test)) { | 104 | while (!kfifo_is_empty(&test)) { |
| 87 | ret = kfifo_out(&test, buf, sizeof(buf)); | 105 | ret = kfifo_out(&test, buf, sizeof(buf)); |
| 88 | printk(KERN_INFO "%.*s\n", ret, buf); | 106 | buf[ret] = '\0'; |
| 107 | printk(KERN_INFO "item = %.*s\n", ret, buf); | ||
| 108 | if (strcmp(buf, expected_result[i++])) { | ||
| 109 | printk(KERN_WARNING "value mismatch: test failed\n"); | ||
| 110 | return -EIO; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | if (i != ARRAY_SIZE(expected_result)) { | ||
| 114 | printk(KERN_WARNING "size mismatch: test failed\n"); | ||
| 115 | return -EIO; | ||
| 89 | } | 116 | } |
| 117 | printk(KERN_INFO "test passed\n"); | ||
| 90 | 118 | ||
| 91 | return 0; | 119 | return 0; |
| 92 | } | 120 | } |
| @@ -142,7 +170,12 @@ static int __init example_init(void) | |||
| 142 | #else | 170 | #else |
| 143 | INIT_KFIFO(test); | 171 | INIT_KFIFO(test); |
| 144 | #endif | 172 | #endif |
| 145 | testfunc(); | 173 | if (testfunc() < 0) { |
| 174 | #ifdef DYNAMIC | ||
| 175 | kfifo_free(&test); | ||
| 176 | #endif | ||
| 177 | return -EIO; | ||
| 178 | } | ||
| 146 | 179 | ||
| 147 | if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { | 180 | if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { |
| 148 | #ifdef DYNAMIC | 181 | #ifdef DYNAMIC |
