dma-example.c 3.4 KB

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