usbh_cdc_acm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * @file usbh_cdc_acm.c
  3. * @brief
  4. *
  5. * Copyright (c) 2022 sakumisu
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #include "usbh_core.h"
  24. #include "usbh_cdc_acm.h"
  25. #define DEV_FORMAT "/dev/ttyACM%d"
  26. static uint32_t g_devinuse = 0;
  27. /****************************************************************************
  28. * Name: usbh_cdc_acm_devno_alloc
  29. *
  30. * Description:
  31. * Allocate a unique /dev/ttyACM[n] minor number in the range 0-31.
  32. *
  33. ****************************************************************************/
  34. static int usbh_cdc_acm_devno_alloc(struct usbh_cdc_acm *cdc_acm_class)
  35. {
  36. size_t flags;
  37. int devno;
  38. flags = usb_osal_enter_critical_section();
  39. for (devno = 0; devno < 32; devno++) {
  40. uint32_t bitno = 1 << devno;
  41. if ((g_devinuse & bitno) == 0) {
  42. g_devinuse |= bitno;
  43. cdc_acm_class->minor = devno;
  44. usb_osal_leave_critical_section(flags);
  45. return 0;
  46. }
  47. }
  48. usb_osal_leave_critical_section(flags);
  49. return -EMFILE;
  50. }
  51. /****************************************************************************
  52. * Name: usbh_cdc_acm_devno_free
  53. *
  54. * Description:
  55. * Free a /dev/ttyACM[n] minor number so that it can be used.
  56. *
  57. ****************************************************************************/
  58. static void usbh_cdc_acm_devno_free(struct usbh_cdc_acm *cdc_acm_class)
  59. {
  60. int devno = cdc_acm_class->minor;
  61. if (devno >= 0 && devno < 32) {
  62. size_t flags = usb_osal_enter_critical_section();
  63. g_devinuse &= ~(1 << devno);
  64. usb_osal_leave_critical_section(flags);
  65. }
  66. }
  67. int usbh_cdc_acm_set_line_coding(struct usbh_cdc_acm *cdc_acm_class, struct cdc_line_coding *line_coding)
  68. {
  69. struct usb_setup_packet *setup = cdc_acm_class->hport->setup;
  70. int ret;
  71. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  72. setup->bRequest = CDC_REQUEST_SET_LINE_CODING;
  73. setup->wValue = 0;
  74. setup->wIndex = cdc_acm_class->ctrl_intf;
  75. setup->wLength = 7;
  76. ret = usbh_control_transfer(cdc_acm_class->hport->ep0, setup, (uint8_t *)line_coding);
  77. if (ret < 0) {
  78. return ret;
  79. }
  80. memcpy(cdc_acm_class->linecoding, line_coding, sizeof(struct cdc_line_coding));
  81. return 0;
  82. }
  83. int usbh_cdc_acm_get_line_coding(struct usbh_cdc_acm *cdc_acm_class, struct cdc_line_coding *line_coding)
  84. {
  85. struct usb_setup_packet *setup = cdc_acm_class->hport->setup;
  86. int ret;
  87. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  88. setup->bRequest = CDC_REQUEST_GET_LINE_CODING;
  89. setup->wValue = 0;
  90. setup->wIndex = cdc_acm_class->ctrl_intf;
  91. setup->wLength = 7;
  92. ret = usbh_control_transfer(cdc_acm_class->hport->ep0, setup, (uint8_t *)line_coding);
  93. if (ret < 0) {
  94. return ret;
  95. }
  96. memcpy(cdc_acm_class->linecoding, line_coding, sizeof(struct cdc_line_coding));
  97. return 0;
  98. }
  99. int usbh_cdc_acm_set_line_state(struct usbh_cdc_acm *cdc_acm_class, bool dtr, bool rts)
  100. {
  101. struct usb_setup_packet *setup = cdc_acm_class->hport->setup;
  102. int ret;
  103. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  104. setup->bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE;
  105. setup->wValue = (dtr << 0) | (rts << 1);
  106. setup->wIndex = cdc_acm_class->ctrl_intf;
  107. setup->wLength = 0;
  108. ret = usbh_control_transfer(cdc_acm_class->hport->ep0, setup, NULL);
  109. if (ret < 0) {
  110. return ret;
  111. }
  112. cdc_acm_class->dtr = dtr;
  113. cdc_acm_class->rts = rts;
  114. return 0;
  115. }
  116. static int usbh_cdc_acm_connect(struct usbh_hubport *hport, uint8_t intf)
  117. {
  118. struct usbh_endpoint_cfg ep_cfg = { 0 };
  119. struct usb_endpoint_descriptor *ep_desc;
  120. int ret;
  121. struct usbh_cdc_acm *cdc_acm_class = usb_malloc(sizeof(struct usbh_cdc_acm));
  122. if (cdc_acm_class == NULL) {
  123. USB_LOG_ERR("Fail to alloc cdc_acm_class\r\n");
  124. return -ENOMEM;
  125. }
  126. memset(cdc_acm_class, 0, sizeof(struct usbh_cdc_acm));
  127. usbh_cdc_acm_devno_alloc(cdc_acm_class);
  128. cdc_acm_class->hport = hport;
  129. cdc_acm_class->ctrl_intf = intf;
  130. cdc_acm_class->data_intf = intf + 1;
  131. hport->config.intf[intf].priv = cdc_acm_class;
  132. hport->config.intf[intf + 1].priv = NULL;
  133. cdc_acm_class->linecoding = usb_iomalloc(sizeof(struct cdc_line_coding));
  134. if (cdc_acm_class->linecoding == NULL) {
  135. USB_LOG_ERR("Fail to alloc linecoding\r\n");
  136. return -ENOMEM;
  137. }
  138. cdc_acm_class->linecoding->dwDTERate = 115200;
  139. cdc_acm_class->linecoding->bDataBits = 8;
  140. cdc_acm_class->linecoding->bParityType = 0;
  141. cdc_acm_class->linecoding->bCharFormat = 0;
  142. ret = usbh_cdc_acm_set_line_coding(cdc_acm_class, cdc_acm_class->linecoding);
  143. if (ret < 0) {
  144. USB_LOG_ERR("Fail to set linecoding\r\n");
  145. return ret;
  146. }
  147. ret = usbh_cdc_acm_set_line_state(cdc_acm_class, true, true);
  148. if (ret < 0) {
  149. USB_LOG_ERR("Fail to set line state\r\n");
  150. return ret;
  151. }
  152. #ifdef CONFIG_USBHOST_CDC_ACM_NOTIFY
  153. ep_desc = &hport->config.intf[intf].ep[0].ep_desc;
  154. ep_cfg.ep_addr = ep_desc->bEndpointAddress;
  155. ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
  156. ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
  157. ep_cfg.ep_interval = ep_desc->bInterval;
  158. ep_cfg.hport = hport;
  159. usbh_ep_alloc(&cdc_acm_class->intin, &ep_cfg);
  160. #endif
  161. for (uint8_t i = 0; i < hport->config.intf[intf + 1].intf_desc.bNumEndpoints; i++) {
  162. ep_desc = &hport->config.intf[intf + 1].ep[i].ep_desc;
  163. ep_cfg.ep_addr = ep_desc->bEndpointAddress;
  164. ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
  165. ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
  166. ep_cfg.ep_interval = ep_desc->bInterval;
  167. ep_cfg.hport = hport;
  168. if (ep_desc->bEndpointAddress & 0x80) {
  169. usbh_ep_alloc(&cdc_acm_class->bulkin, &ep_cfg);
  170. } else {
  171. usbh_ep_alloc(&cdc_acm_class->bulkout, &ep_cfg);
  172. }
  173. }
  174. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, cdc_acm_class->minor);
  175. USB_LOG_INFO("Register CDC ACM Class:%s\r\n", hport->config.intf[intf].devname);
  176. extern int cdc_acm_test();
  177. cdc_acm_test();
  178. return ret;
  179. }
  180. static int usbh_cdc_acm_disconnect(struct usbh_hubport *hport, uint8_t intf)
  181. {
  182. int ret = 0;
  183. struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
  184. if (cdc_acm_class) {
  185. usbh_cdc_acm_devno_free(cdc_acm_class);
  186. if (cdc_acm_class->bulkin) {
  187. ret = usb_ep_cancel(cdc_acm_class->bulkin);
  188. if (ret < 0) {
  189. }
  190. usbh_ep_free(cdc_acm_class->bulkin);
  191. }
  192. if (cdc_acm_class->bulkout) {
  193. ret = usb_ep_cancel(cdc_acm_class->bulkout);
  194. if (ret < 0) {
  195. }
  196. usbh_ep_free(cdc_acm_class->bulkout);
  197. }
  198. if (cdc_acm_class->linecoding)
  199. usb_iofree(cdc_acm_class->linecoding);
  200. usb_free(cdc_acm_class);
  201. if (hport->config.intf[intf].devname[0] != '\0')
  202. USB_LOG_INFO("Unregister CDC ACM Class:%s\r\n", hport->config.intf[intf].devname);
  203. memset(hport->config.intf[intf].devname, 0, CONFIG_USBHOST_DEV_NAMELEN);
  204. hport->config.intf[intf].priv = NULL;
  205. hport->config.intf[intf + 1].priv = NULL;
  206. }
  207. return ret;
  208. }
  209. const struct usbh_class_driver cdc_acm_class_driver = {
  210. .driver_name = "cdc_acm",
  211. .connect = usbh_cdc_acm_connect,
  212. .disconnect = usbh_cdc_acm_disconnect
  213. };
  214. CLASS_INFO_DEFINE const struct usbh_class_info cdc_acm_class_info = {
  215. .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
  216. .class = USB_DEVICE_CLASS_CDC,
  217. .subclass = CDC_ABSTRACT_CONTROL_MODEL,
  218. .protocol = CDC_COMMON_PROTOCOL_AT_COMMANDS,
  219. .vid = 0x00,
  220. .pid = 0x00,
  221. .class_driver = &cdc_acm_class_driver
  222. };