dma-example.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_ERR "error kfifo_alloc\n");
  26. return 1;
  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. ret = kfifo_get(&fifo, &i);
  34. printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
  35. ret = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE);
  36. printk(KERN_INFO "DMA sgl entries: %d\n", ret);
  37. /* if 0 was returned, fifo is full and no sgl was created */
  38. if (ret) {
  39. printk(KERN_INFO "scatterlist for receive:\n");
  40. for (i = 0; i < ARRAY_SIZE(sg); i++) {
  41. printk(KERN_INFO
  42. "sg[%d] -> "
  43. "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
  44. i, sg[i].page_link, sg[i].offset, sg[i].length);
  45. if (sg_is_last(&sg[i]))
  46. break;
  47. }
  48. /* but here your code to setup and exectute the dma operation */
  49. /* ... */
  50. /* example: zero bytes received */
  51. ret = 0;
  52. /* finish the dma operation and update the received data */
  53. kfifo_dma_in_finish(&fifo, ret);
  54. }
  55. ret = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8);
  56. printk(KERN_INFO "DMA sgl entries: %d\n", ret);
  57. /* if 0 was returned, no data was available and no sgl was created */
  58. if (ret) {
  59. printk(KERN_INFO "scatterlist for transmit:\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. /* but here your code to setup and exectute the dma operation */
  69. /* ... */
  70. /* example: 5 bytes transmitted */
  71. ret = 5;
  72. /* finish the dma operation and update the transmitted data */
  73. kfifo_dma_out_finish(&fifo, ret);
  74. }
  75. printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
  76. return 0;
  77. }
  78. static void __exit example_exit(void)
  79. {
  80. #ifdef DYNAMIC
  81. kfifo_free(&test);
  82. #endif
  83. }
  84. module_init(example_init);
  85. module_exit(example_exit);
  86. MODULE_LICENSE("GPL");
  87. MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");