driver.c 3.4 KB

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