driver.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * File : driver.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2011-03-12 Yi Qiu first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtservice.h>
  26. #include <rtdevice.h>
  27. static rt_list_t _driver_list;
  28. /**
  29. * This function will initilize the usb class driver related data structure,
  30. * and it should be invoked in the usb system initialization.
  31. *
  32. * @return the error code, RT_EOK on successfully.
  33. */
  34. rt_err_t rt_usbh_class_driver_init(void)
  35. {
  36. rt_list_init(&_driver_list);
  37. return RT_EOK;
  38. }
  39. /**
  40. * This function will register an usb class driver to the class driver manager.
  41. *
  42. * @param drv the pointer of the usb class driver.
  43. *
  44. * @return the error code, RT_EOK on successfully.
  45. */
  46. rt_err_t rt_usbh_class_driver_register(ucd_t drv)
  47. {
  48. if (drv == RT_NULL) return -RT_ERROR;
  49. /* insert class driver into driver list */
  50. rt_list_insert_after(&_driver_list, &(drv->list));
  51. return RT_EOK;
  52. }
  53. /**
  54. * This function will removes a previously registed usb class driver.
  55. *
  56. * @param drv the pointer of the usb class driver structure.
  57. *
  58. * @return the error code, RT_EOK on successfully.
  59. */
  60. rt_err_t rt_usbh_class_driver_unregister(ucd_t drv)
  61. {
  62. RT_ASSERT(drv != RT_NULL);
  63. /* remove class driver from driver list */
  64. rt_list_remove(&(drv->list));
  65. return RT_EOK;
  66. }
  67. /**
  68. * This function will run an usb class driver.
  69. *
  70. * @param drv the pointer of usb class driver.
  71. * @param args the parameter of run function.
  72. *
  73. * @return the error code, RT_EOK on successfully.
  74. */
  75. rt_err_t rt_usbh_class_driver_enable(ucd_t drv, void* args)
  76. {
  77. RT_ASSERT(drv != RT_NULL);
  78. if(drv->enable != RT_NULL)
  79. drv->enable(args);
  80. return RT_EOK;
  81. }
  82. /**
  83. * This function will stop a usb class driver.
  84. *
  85. * @param drv the pointer of usb class driver structure.
  86. * @param args the argument of the stop function.
  87. *
  88. * @return the error code, RT_EOK on successfully.
  89. */
  90. rt_err_t rt_usbh_class_driver_disable(ucd_t drv, void* args)
  91. {
  92. RT_ASSERT(drv != RT_NULL);
  93. if(drv->disable != RT_NULL)
  94. drv->disable(args);
  95. return RT_EOK;
  96. }
  97. /**
  98. * This function finds a usb class driver by specified class code and subclass code.
  99. *
  100. * @param class_code the usb class driver's class code.
  101. * @param subclass_code the usb class driver's sub class code.
  102. *
  103. * @return the registered usb class driver on successful, or RT_NULL on failure.
  104. */
  105. ucd_t rt_usbh_class_driver_find(int class_code, int subclass_code)
  106. {
  107. struct rt_list_node *node;
  108. /* enter critical */
  109. if (rt_thread_self() != RT_NULL)
  110. rt_enter_critical();
  111. /* try to find driver object */
  112. for (node = _driver_list.next; node != &_driver_list; node = node->next)
  113. {
  114. ucd_t drv =
  115. (ucd_t)rt_list_entry(node, struct uclass_driver, list);
  116. if (drv->class_code == class_code)
  117. {
  118. /* leave critical */
  119. if (rt_thread_self() != RT_NULL)
  120. rt_exit_critical();
  121. return drv;
  122. }
  123. }
  124. /* leave critical */
  125. if (rt_thread_self() != RT_NULL)
  126. rt_exit_critical();
  127. /* not found */
  128. return RT_NULL;
  129. }