hcd.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * File : hcd.h
  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. #ifndef __HCD_H__
  15. #define __HCD_H__
  16. #include <rtthread.h>
  17. #include "usbspec.h"
  18. struct uhcd_ops
  19. {
  20. int (*ctl_xfer)(uinst_t inst, ureq_t setup, void* buffer, int nbytes,
  21. int timeout);
  22. int (*bulk_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  23. int (*int_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  24. int (*iso_xfer)(upipe_t pipe, void* buffer, int nbytes, int timeout);
  25. rt_err_t (*alloc_pipe)(struct upipe** pipe, uifinst_t ifinst, uep_desc_t ep,
  26. func_callback callback);
  27. rt_err_t (*free_pipe)(upipe_t pipe);
  28. rt_err_t (*hub_ctrl)(rt_uint16_t port, rt_uint8_t cmd, void *args);
  29. };
  30. struct uhcd
  31. {
  32. struct rt_device parent;
  33. struct uhcd_ops* ops;
  34. };
  35. typedef struct uhcd* uhcd_t;
  36. rt_inline rt_err_t rt_usb_hcd_alloc_pipe(uhcd_t hcd, upipe_t* pipe,
  37. uifinst_t ifinst, uep_desc_t ep, func_callback callback)
  38. {
  39. if(ifinst == RT_NULL) return -RT_EIO;
  40. /* parameter check */
  41. RT_ASSERT(hcd != RT_NULL);
  42. RT_ASSERT(hcd->ops != RT_NULL);
  43. RT_ASSERT(hcd->ops->alloc_pipe!= RT_NULL);
  44. return hcd->ops->alloc_pipe(pipe, ifinst, ep, callback);
  45. }
  46. rt_inline rt_err_t rt_usb_hcd_free_pipe(uhcd_t hcd, upipe_t pipe)
  47. {
  48. RT_ASSERT(pipe != RT_NULL);
  49. /* parameter check */
  50. RT_ASSERT(hcd != RT_NULL);
  51. RT_ASSERT(hcd->ops != RT_NULL);
  52. RT_ASSERT(hcd->ops->free_pipe!= RT_NULL);
  53. return hcd->ops->free_pipe(pipe);
  54. }
  55. rt_inline int rt_usb_hcd_bulk_xfer(uhcd_t hcd, upipe_t pipe, void* buffer,
  56. int nbytes, int timeout)
  57. {
  58. if(pipe == RT_NULL) return -1;
  59. if(pipe->ifinst == RT_NULL) return -1;
  60. if(pipe->ifinst->uinst == RT_NULL) return -1;
  61. if(pipe->ifinst->uinst->status == UINST_STATUS_IDLE)
  62. return -1;
  63. /* parameter check */
  64. RT_ASSERT(hcd != RT_NULL);
  65. RT_ASSERT(hcd->ops != RT_NULL);
  66. RT_ASSERT(hcd->ops->bulk_xfer!= RT_NULL);
  67. return hcd->ops->bulk_xfer(pipe, buffer, nbytes, timeout);
  68. }
  69. rt_inline int rt_usb_hcd_control_xfer(uhcd_t hcd, uinst_t uinst, ureq_t setup,
  70. void* buffer, int nbytes, int timeout)
  71. {
  72. if(uinst->status == UINST_STATUS_IDLE) return -1;
  73. /* parameter check */
  74. RT_ASSERT(hcd != RT_NULL);
  75. RT_ASSERT(hcd->ops != RT_NULL);
  76. RT_ASSERT(hcd->ops->ctl_xfer!= RT_NULL);
  77. return hcd->ops->ctl_xfer(uinst, setup, buffer, nbytes, timeout);
  78. }
  79. rt_inline int rt_usb_hcd_int_xfer(uhcd_t hcd, upipe_t pipe, void* buffer,
  80. int nbytes, int timeout)
  81. {
  82. if(pipe == RT_NULL) return -1;
  83. if(pipe->ifinst == RT_NULL) return -1;
  84. if(pipe->ifinst->uinst == RT_NULL) return -1;
  85. if(pipe->ifinst->uinst->status == UINST_STATUS_IDLE)
  86. return -1;
  87. RT_ASSERT(hcd != RT_NULL);
  88. RT_ASSERT(hcd->ops != RT_NULL);
  89. RT_ASSERT(hcd->ops->int_xfer!= RT_NULL);
  90. return hcd->ops->int_xfer(pipe, buffer, nbytes, timeout);
  91. }
  92. rt_inline rt_err_t rt_usb_hcd_hub_control(uhcd_t hcd, rt_uint16_t port,
  93. rt_uint8_t cmd, void *args)
  94. {
  95. RT_ASSERT(hcd != RT_NULL);
  96. RT_ASSERT(hcd->ops != RT_NULL);
  97. RT_ASSERT(hcd->ops->hub_ctrl != RT_NULL);
  98. return hcd->ops->hub_ctrl(port, cmd, args);
  99. }
  100. #endif