driver.c 3.5 KB

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