dma-example.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/kfifo.h>
  12. /*
  13. * This module shows how to handle fifo dma operations.
  14. */
  15. /* fifo size in elements (bytes) */
  16. #define FIFO_SIZE 32
  17. static struct kfifo fifo;
  18. static int __init example_init(void)
  19. {
  20. int i;
  21. unsigned int ret;
  22. struct scatterlist sg[10];
  23. printk(KERN_INFO "DMA fifo test start\n");
  24. if (kfifo_alloc(&fifo, FIFO_SIZE, GFP_KERNEL)) {
  25. printk(KERN_WARNING "error kfifo_alloc\n");
  26. return -ENOMEM;
  27. }
  28. printk(KERN_INFO "queue size: %u\n", kfifo_size(&fifo));
  29. kfifo_in(&fifo, "test", 4);
  30. for (i = 0; i != 9; i++)
  31. kfifo_put(&fifo, &i);
  32. /* kick away first byte */
  33. kfifo_skip(&fifo);
  34. printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
  35. /*
  36. * Configure the kfifo buffer to receive data from DMA input.
  37. *
  38. * .--------------------------------------.
  39. * | 0 | 1 | 2 | ... | 12 | 13 | ... | 31 |
  40. * |---|------------------|---------------|
  41. * \_/ \________________/ \_____________/
  42. * \ \ \
  43. * \ \_allocated data \
  44. * \_*free space* \_*free space*
  45. *
  46. * We need two different SG entries: one for the free space area at the
  47. * end of the kfifo buffer (19 bytes) and another for the first free
  48. * byte at the beginning, after the kfifo_skip().
  49. */
  50. sg_init_table(sg, ARRAY_SIZE(sg));
  51. ret = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE);
  52. printk(KERN_INFO "DMA sgl entries: %d\n", ret);
  53. if (!ret) {
  54. /* fifo is full and no sgl was created */
  55. printk(KERN_WARNING "error kfifo_dma_in_prepare\n");
  56. return -EIO;
  57. }
  58. /* receive data */
  59. printk(KERN_INFO "scatterlist for receive:\n");
  60. for (i = 0; i < ARRAY_SIZE(sg); i++) {
  61. printk(KERN_INFO
  62. "sg[%d] -> "
  63. "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
  64. i, sg[i].page_link, sg[i].offset, sg[i].length);
  65. if (sg_is_last(&sg[i]))
  66. break;
  67. }
  68. /* put here your code to setup and exectute the dma operation */
  69. /* ... */
  70. /* example: zero bytes received */
  71. ret = 0;
  72. /* finish the dma operation and update the received data */
  73. kfifo_dma_in_finish(&fifo, ret);
  74. /* Prepare to transmit data, example: 8 bytes */
  75. ret = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8);
  76. printk(KERN_INFO "DMA sgl entries: %d\n", ret);
  77. if (!ret) {
  78. /* no data was available and no sgl was created */
  79. printk(KERN_WARNING "error kfifo_dma_out_prepare\n");
  80. return -EIO;
  81. }
  82. printk(KERN_INFO "scatterlist for transmit:\n");
  83. for (i = 0; i < ARRAY_SIZE(sg); i++) {
  84. printk(KERN_INFO
  85. "sg[%d] -> "
  86. "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
  87. i, sg[i].page_link, sg[i].offset, sg[i].length);
  88. if (sg_is_last(&sg[i]))
  89. break;
  90. }
  91. /* put here your code to setup and exectute the dma operation */
  92. /* ... */
  93. /* example: 5 bytes transmitted */
  94. ret = 5;
  95. /* finish the dma operation and update the transmitted data */
  96. kfifo_dma_out_finish(&fifo, ret);
  97. ret = kfifo_len(&fifo);
  98. printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
  99. if (ret != 7) {
  100. printk(KERN_WARNING "size mismatch: test failed");
  101. return -EIO;
  102. }
  103. printk(KERN_INFO "test passed\n");
  104. return 0;
  105. }
  106. static void __exit example_exit(void)
  107. {
  108. kfifo_free(&fifo);
  109. }
  110. module_init(example_init);
  111. module_exit(example_exit);
  112. MODULE_LICENSE("GPL");
  113. MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");