adk.c 4.6 KB

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