driver.c 3.2 KB

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