dma-example.c 3.4 KB

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