driver.c 3.4 KB

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