driver.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-03-12 Yi Qiu first version
  9. * 2021-02-23 Leslie Lee provide possibility for multi usb host
  10. */
  11. #include <rtthread.h>
  12. #include <rtservice.h>
  13. #include <drivers/usb_host.h>
  14. static rt_list_t _driver_list;
  15. static rt_bool_t _driver_list_created = RT_FALSE;
  16. /**
  17. * This function will initilize the usb class driver related data structure,
  18. * and it should be invoked in the usb system initialization.
  19. *
  20. * @return the error code, RT_EOK on successfully.
  21. */
  22. rt_err_t rt_usbh_class_driver_init(void)
  23. {
  24. if (_driver_list_created == RT_FALSE)
  25. {
  26. rt_list_init(&_driver_list);
  27. _driver_list_created = RT_TRUE;
  28. }
  29. return RT_EOK;
  30. }
  31. /**
  32. * This function will register an usb class driver to the class driver manager.
  33. *
  34. * @param drv the pointer of the usb class driver.
  35. *
  36. * @return the error code, RT_EOK on successfully.
  37. */
  38. rt_err_t rt_usbh_class_driver_register(ucd_t drv)
  39. {
  40. if (drv == RT_NULL) return -RT_ERROR;
  41. if (rt_usbh_class_driver_find(drv->class_code, drv->subclass_code) == RT_NULL)
  42. {
  43. /* insert class driver into driver list */
  44. rt_list_insert_after(&_driver_list, &(drv->list));
  45. }
  46. return RT_EOK;
  47. }
  48. /**
  49. * This function will removes a previously registed usb class driver.
  50. *
  51. * @param drv the pointer of the usb class driver structure.
  52. *
  53. * @return the error code, RT_EOK on successfully.
  54. */
  55. rt_err_t rt_usbh_class_driver_unregister(ucd_t drv)
  56. {
  57. RT_ASSERT(drv != RT_NULL);
  58. /* remove class driver from driver list */
  59. rt_list_remove(&(drv->list));
  60. return RT_EOK;
  61. }
  62. /**
  63. * This function will run an usb class driver.
  64. *
  65. * @param drv the pointer of usb class driver.
  66. * @param args the parameter of run function.
  67. *
  68. * @return the error code, RT_EOK on successfully.
  69. */
  70. rt_err_t rt_usbh_class_driver_enable(ucd_t drv, void* args)
  71. {
  72. RT_ASSERT(drv != RT_NULL);
  73. if(drv->enable != RT_NULL)
  74. drv->enable(args);
  75. return RT_EOK;
  76. }
  77. /**
  78. * This function will stop a usb class driver.
  79. *
  80. * @param drv the pointer of usb class driver structure.
  81. * @param args the argument of the stop function.
  82. *
  83. * @return the error code, RT_EOK on successfully.
  84. */
  85. rt_err_t rt_usbh_class_driver_disable(ucd_t drv, void* args)
  86. {
  87. RT_ASSERT(drv != RT_NULL);
  88. if(drv->disable != RT_NULL)
  89. drv->disable(args);
  90. return RT_EOK;
  91. }
  92. /**
  93. * This function finds a usb class driver by specified class code and subclass code.
  94. *
  95. * @param class_code the usb class driver's class code.
  96. * @param subclass_code the usb class driver's sub class code.
  97. *
  98. * @return the registered usb class driver on successful, or RT_NULL on failure.
  99. */
  100. ucd_t rt_usbh_class_driver_find(int class_code, int subclass_code)
  101. {
  102. struct rt_list_node *node;
  103. /* enter critical */
  104. if (rt_thread_self() != RT_NULL)
  105. rt_enter_critical();
  106. /* try to find driver object */
  107. for (node = _driver_list.next; node != &_driver_list; node = node->next)
  108. {
  109. ucd_t drv =
  110. (ucd_t)rt_list_entry(node, struct uclass_driver, list);
  111. if (drv->class_code == class_code)
  112. {
  113. /* leave critical */
  114. if (rt_thread_self() != RT_NULL)
  115. rt_exit_critical();
  116. return drv;
  117. }
  118. }
  119. /* leave critical */
  120. if (rt_thread_self() != RT_NULL)
  121. rt_exit_critical();
  122. /* not found */
  123. return RT_NULL;
  124. }