winusb.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * File : winusb.c
  3. * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2017-11-16 ZYH first version
  8. */
  9. #include <rthw.h>
  10. #include <rtthread.h>
  11. #include <rtservice.h>
  12. #include <rtdevice.h>
  13. #include <drivers/usb_device.h>
  14. #include "winusb.h"
  15. struct winusb_device
  16. {
  17. uep_t ep_out;
  18. uep_t ep_in;
  19. };
  20. typedef struct winusb_device * winusb_device_t;
  21. static struct udevice_descriptor dev_desc =
  22. {
  23. USB_DESC_LENGTH_DEVICE, //bLength;
  24. USB_DESC_TYPE_DEVICE, //type;
  25. USB_BCD_VERSION, //bcdUSB;
  26. 0x00, //bDeviceClass;
  27. 0x00, //bDeviceSubClass;
  28. 0x00, //bDeviceProtocol;
  29. 0x40, //bMaxPacketSize0;
  30. _VENDOR_ID, //idVendor;
  31. _PRODUCT_ID, //idProduct;
  32. USB_BCD_DEVICE, //bcdDevice;
  33. USB_STRING_MANU_INDEX, //iManufacturer;
  34. USB_STRING_PRODUCT_INDEX, //iProduct;
  35. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  36. USB_DYNAMIC, //bNumConfigurations;
  37. };
  38. //FS and HS needed
  39. static struct usb_qualifier_descriptor dev_qualifier =
  40. {
  41. sizeof(dev_qualifier),
  42. USB_DESC_TYPE_DEVICEQUALIFIER,
  43. 0x0200,
  44. 0x00,
  45. 0x00,
  46. 64,
  47. 0x01,
  48. 0,
  49. };
  50. struct winusb_descriptor _winusb_desc =
  51. {
  52. #ifdef RT_USB_DEVICE_COMPOSITE
  53. /* Interface Association Descriptor */
  54. USB_DESC_LENGTH_IAD,
  55. USB_DESC_TYPE_IAD,
  56. USB_DYNAMIC,
  57. 0x01,
  58. 0xFF,
  59. 0x00,
  60. 0x00,
  61. 0x00,
  62. #endif
  63. /*interface descriptor*/
  64. USB_DESC_LENGTH_INTERFACE, //bLength;
  65. USB_DESC_TYPE_INTERFACE, //type;
  66. USB_DYNAMIC, //bInterfaceNumber;
  67. 0x00, //bAlternateSetting;
  68. 0x02, //bNumEndpoints
  69. 0xFF, //bInterfaceClass;
  70. 0x00, //bInterfaceSubClass;
  71. 0x00, //bInterfaceProtocol;
  72. 0x00, //iInterface;
  73. /*endpoint descriptor*/
  74. USB_DESC_LENGTH_ENDPOINT,
  75. USB_DESC_TYPE_ENDPOINT,
  76. USB_DYNAMIC | USB_DIR_OUT,
  77. USB_EP_ATTR_BULK,
  78. 0x40,
  79. 0x00,
  80. /*endpoint descriptor*/
  81. USB_DESC_LENGTH_ENDPOINT,
  82. USB_DESC_TYPE_ENDPOINT,
  83. USB_DYNAMIC | USB_DIR_IN,
  84. USB_EP_ATTR_BULK,
  85. 0x40,
  86. 0x00,
  87. };
  88. const static char* _ustring[] =
  89. {
  90. "Language",
  91. "RT-Thread Team.",
  92. "RTT Win USB",
  93. "32021919830108",
  94. "Configuration",
  95. "Interface",
  96. USB_STRING_OS//must be
  97. };
  98. struct usb_os_function_comp_id_descriptor winusb_func_comp_id_desc =
  99. {
  100. .bFirstInterfaceNumber = USB_DYNAMIC,
  101. .reserved1 = 0x01,
  102. .compatibleID = {'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00},
  103. .subCompatibleID = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  104. .reserved2 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  105. };
  106. static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
  107. {
  108. struct winusb_device* data = (struct winusb_device*)func->user_data;
  109. rt_kprintf("recev:%s",data->ep_out->buffer);
  110. data->ep_in->request.buffer = data->ep_out->buffer;
  111. data->ep_in->request.size = EP_MAXPACKET(data->ep_out);
  112. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  113. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  114. return RT_EOK;
  115. }
  116. static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
  117. {
  118. return RT_EOK;
  119. }
  120. static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
  121. {
  122. return RT_EOK;
  123. }
  124. static rt_err_t _function_enable(ufunction_t func)
  125. {
  126. RT_ASSERT(func != RT_NULL);
  127. struct winusb_device* data = (struct winusb_device*)func->user_data;
  128. data->ep_out->buffer = rt_malloc(0x40);
  129. data->ep_out->request.buffer = data->ep_out->buffer;
  130. data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
  131. data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  132. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  133. return RT_EOK;
  134. }
  135. static rt_err_t _function_disable(ufunction_t func)
  136. {
  137. RT_ASSERT(func != RT_NULL);
  138. struct winusb_device* data = (struct winusb_device*)func->user_data;
  139. if(data->ep_out->buffer != RT_NULL)
  140. {
  141. rt_free(data->ep_out->buffer);
  142. data->ep_out->buffer = RT_NULL;
  143. }
  144. return RT_EOK;
  145. }
  146. static struct ufunction_ops ops =
  147. {
  148. _function_enable,
  149. _function_disable,
  150. RT_NULL,
  151. };
  152. static rt_err_t _winusb_descriptor_config(winusb_desc_t winusb, rt_uint8_t cintf_nr)
  153. {
  154. #ifdef RT_USB_DEVICE_COMPOSITE
  155. winusb->iad_desc.bFirstInterface = cintf_nr;
  156. #endif
  157. winusb_func_comp_id_desc.bFirstInterfaceNumber = cintf_nr;
  158. return RT_EOK;
  159. }
  160. static rt_err_t rt_usb_winusb_init(ufunction_t func)
  161. {
  162. return RT_EOK;
  163. }
  164. ufunction_t rt_usbd_function_winusb_create(udevice_t device)
  165. {
  166. ufunction_t func;
  167. winusb_device_t winusb_device;
  168. uintf_t winusb_intf;
  169. ualtsetting_t winusb_setting;
  170. winusb_desc_t winusb_desc;
  171. /* parameter check */
  172. RT_ASSERT(device != RT_NULL);
  173. /* set usb device string description */
  174. rt_usbd_device_set_string(device, _ustring);
  175. /* create a cdc function */
  176. func = rt_usbd_function_new(device, &dev_desc, &ops);
  177. rt_usbd_device_set_qualifier(device, &dev_qualifier);
  178. /* allocate memory for cdc vcom data */
  179. winusb_device = (winusb_device_t)rt_malloc(sizeof(struct winusb_device));
  180. rt_memset((void *)winusb_device, 0, sizeof(struct winusb_device));
  181. func->user_data = (void*)winusb_device;
  182. /* create an interface object */
  183. winusb_intf = rt_usbd_interface_new(device, _interface_handler);
  184. /* create an alternate setting object */
  185. winusb_setting = rt_usbd_altsetting_new(sizeof(struct winusb_descriptor));
  186. /* config desc in alternate setting */
  187. rt_usbd_altsetting_config_descriptor(winusb_setting, &_winusb_desc, (rt_off_t)&((winusb_desc_t)0)->intf_desc);
  188. /* configure the hid interface descriptor */
  189. _winusb_descriptor_config(winusb_setting->desc, winusb_intf->intf_num);
  190. /* create endpoint */
  191. winusb_desc = (winusb_desc_t)winusb_setting->desc;
  192. winusb_device->ep_out = rt_usbd_endpoint_new(&winusb_desc->ep_out_desc, _ep_out_handler);
  193. winusb_device->ep_in = rt_usbd_endpoint_new(&winusb_desc->ep_in_desc, _ep_in_handler);
  194. /* add the int out and int in endpoint to the alternate setting */
  195. rt_usbd_altsetting_add_endpoint(winusb_setting, winusb_device->ep_out);
  196. rt_usbd_altsetting_add_endpoint(winusb_setting, winusb_device->ep_in);
  197. /* add the alternate setting to the interface, then set default setting */
  198. rt_usbd_interface_add_altsetting(winusb_intf, winusb_setting);
  199. rt_usbd_set_altsetting(winusb_intf, 0);
  200. /* add the interface to the mass storage function */
  201. rt_usbd_function_add_interface(func, winusb_intf);
  202. rt_usbd_os_comp_id_desc_add_os_func_comp_id_desc(device->os_comp_id_desc, &winusb_func_comp_id_desc);
  203. /* initilize hid */
  204. rt_usb_winusb_init(func);
  205. return func;
  206. }