driver.c 3.3 KB

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