adk.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * File : adk.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-12-12 Yi Qiu first version
  13. */
  14. #include <rtthread.h>
  15. #include <rtdevice.h>
  16. #include "driver.h"
  17. #include "core.h"
  18. #include "hcd.h"
  19. #include "adk.h"
  20. #ifdef RT_USB_CLASS_ADK
  21. static struct uclass_driver adk_driver;
  22. rt_err_t rt_usb_adk_read(uifinst_t ifinst, rt_uint8_t *buffer,
  23. rt_size_t size)
  24. {
  25. uadkinst_t adkinst;
  26. rt_size_t length;
  27. RT_ASSERT(buffer != RT_NULL);
  28. if(ifinst == RT_NULL)
  29. {
  30. rt_kprintf("the interface is not available\n");
  31. return -RT_EIO;
  32. }
  33. /* get storage instance from the interface instance */
  34. adkinst = (uadkinst_t)ifinst->user_data;
  35. /* send the cbw */
  36. length = rt_usb_hcd_bulk_xfer(ifinst->uinst->hcd, adkinst->pipe_in,
  37. buffer, size, 300);
  38. return length;
  39. }
  40. rt_err_t rt_usb_adk_write(uifinst_t ifinst, rt_uint8_t *buffer,
  41. rt_size_t size)
  42. {
  43. uadkinst_t adkinst;
  44. rt_size_t length;
  45. RT_ASSERT(buffer != RT_NULL);
  46. if(ifinst == RT_NULL)
  47. {
  48. rt_kprintf("the interface is not available\n");
  49. return -RT_EIO;
  50. }
  51. /* get storage instance from the interface instance */
  52. adkinst = (uadkinst_t)ifinst->user_data;
  53. /* send the cbw */
  54. length = rt_usb_hcd_bulk_xfer(ifinst->uinst->hcd, adkinst->pipe_out,
  55. buffer, size, 300);
  56. return length;
  57. }
  58. /**
  59. * This function will run adk class driver when usb device is detected and identified
  60. * as a adk class device, it will continue the enumulate process.
  61. *
  62. * @param arg the argument.
  63. *
  64. * @return the error code, RT_EOK on successfully.
  65. */
  66. static rt_err_t rt_usb_adk_run(void* arg)
  67. {
  68. int i = 0;
  69. uadkinst_t adkinst;
  70. uifinst_t ifinst = (uifinst_t)arg;
  71. rt_err_t ret;
  72. /* parameter check */
  73. if(ifinst == RT_NULL)
  74. {
  75. rt_kprintf("the interface is not available\n");
  76. return -RT_EIO;
  77. }
  78. RT_DEBUG_LOG(RT_DEBUG_USB,("rt_usb_adk_run\n"));
  79. adkinst = rt_malloc(sizeof(struct uadkinst));
  80. RT_ASSERT(adkinst != RT_NULL);
  81. /* initilize the data structure */
  82. rt_memset(adkinst, 0, sizeof(struct uadkinst));
  83. ifinst->user_data = (void*)adkinst;
  84. for(i=0; i<ifinst->intf_desc->bNumEndpoints; i++)
  85. {
  86. uep_desc_t ep_desc;
  87. /* get endpoint descriptor from interface descriptor */
  88. rt_usb_get_endpoint_descriptor(ifinst->intf_desc, i, &ep_desc);
  89. if(ep_desc == RT_NULL)
  90. {
  91. rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
  92. return -RT_ERROR;
  93. }
  94. /* the endpoint type of adk class should be BULK */
  95. if((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
  96. continue;
  97. /* allocate pipes according to the endpoint type */
  98. if(ep_desc->bEndpointAddress & USB_DIR_IN)
  99. {
  100. /* allocate an in pipe for the adk instance */
  101. ret = rt_usb_hcd_alloc_pipe(ifinst->uinst->hcd, &adkinst->pipe_in,
  102. ifinst, ep_desc, RT_NULL);
  103. if(ret != RT_EOK) return ret;
  104. }
  105. else
  106. {
  107. /* allocate an output pipe for the adk instance */
  108. ret = rt_usb_hcd_alloc_pipe(ifinst->uinst->hcd, &adkinst->pipe_out,
  109. ifinst, ep_desc, RT_NULL);
  110. if(ret != RT_EOK) return ret;
  111. }
  112. }
  113. /* check pipes infomation */
  114. if(adkinst->pipe_in == RT_NULL || adkinst->pipe_out == RT_NULL)
  115. {
  116. rt_kprintf("pipe error, unsupported device\n");
  117. return -RT_ERROR;
  118. }
  119. return RT_EOK;
  120. }
  121. /**
  122. * This function will be invoked when usb device plug out is detected and it would clean
  123. * and release all hub class related resources.
  124. *
  125. * @param arg the argument.
  126. *
  127. * @return the error code, RT_EOK on successfully.
  128. */
  129. static rt_err_t rt_usb_adk_stop(void* arg)
  130. {
  131. uadkinst_t adkinst;
  132. uifinst_t ifinst = (uifinst_t)arg;
  133. RT_ASSERT(ifinst != RT_NULL);
  134. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usb_adk_stop\n"));
  135. adkinst = (uadkinst_t)ifinst->user_data;
  136. if(adkinst->pipe_in != RT_NULL)
  137. rt_usb_hcd_free_pipe(ifinst->uinst->hcd, adkinst->pipe_in);
  138. /* free adk instance */
  139. if(adkinst != RT_NULL) rt_free(adkinst);
  140. /* free interface instance */
  141. if(ifinst != RT_NULL) rt_free(ifinst);
  142. return RT_EOK;
  143. }
  144. /**
  145. * This function will register adk class driver to the usb class driver manager.
  146. * and it should be invoked in the usb system initialization.
  147. *
  148. * @return the error code, RT_EOK on successfully.
  149. */
  150. ucd_t rt_usb_class_driver_adk(void)
  151. {
  152. adk_driver.class_code = USB_CLASS_ADK;
  153. adk_driver.run = rt_usb_adk_run;
  154. adk_driver.stop = rt_usb_adk_stop;
  155. return &adk_driver;
  156. }
  157. #endif