usbh_ftdi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_ftdi.h"
  8. #define DEV_FORMAT "/dev/ttyUSB%d"
  9. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_ftdi_buf[64];
  10. #define CONFIG_USBHOST_MAX_FTDI_CLASS 1
  11. static struct usbh_ftdi g_ftdi_class[CONFIG_USBHOST_MAX_FTDI_CLASS];
  12. static uint32_t g_devinuse = 0;
  13. static struct usbh_ftdi *usbh_ftdi_class_alloc(void)
  14. {
  15. uint8_t devno;
  16. for (devno = 0; devno < CONFIG_USBHOST_MAX_FTDI_CLASS; devno++) {
  17. if ((g_devinuse & (1U << devno)) == 0) {
  18. g_devinuse |= (1U << devno);
  19. memset(&g_ftdi_class[devno], 0, sizeof(struct usbh_ftdi));
  20. g_ftdi_class[devno].minor = devno;
  21. return &g_ftdi_class[devno];
  22. }
  23. }
  24. return NULL;
  25. }
  26. static void usbh_ftdi_class_free(struct usbh_ftdi *ftdi_class)
  27. {
  28. uint8_t devno = ftdi_class->minor;
  29. if (devno < 32) {
  30. g_devinuse &= ~(1U << devno);
  31. }
  32. memset(ftdi_class, 0, sizeof(struct usbh_ftdi));
  33. }
  34. static void usbh_ftdi_caculate_baudrate(uint32_t *itdf_divisor, uint32_t actual_baudrate)
  35. {
  36. #define FTDI_USB_CLK 48000000
  37. int baudrate;
  38. uint8_t frac[] = { 0, 8, 4, 2, 6, 10, 12, 14 };
  39. if (actual_baudrate == 2000000) {
  40. *itdf_divisor = 0x01;
  41. } else if (actual_baudrate == 3000000) {
  42. *itdf_divisor = 0x00;
  43. } else {
  44. baudrate = actual_baudrate;
  45. if (baudrate > 100000 && baudrate < 12000000) {
  46. baudrate = (baudrate / 100000) + 100000;
  47. }
  48. int divisor = FTDI_USB_CLK / baudrate;
  49. int frac_bits = 0;
  50. for (uint8_t i = 0; i < sizeof(frac) / sizeof(frac[0]); i++) {
  51. if ((divisor & 0xF) == frac[i]) {
  52. frac_bits = i;
  53. break;
  54. }
  55. }
  56. divisor >>= 4;
  57. divisor &= 0x3FFF;
  58. *itdf_divisor = (divisor << 14) | (frac_bits << 8);
  59. }
  60. }
  61. int usbh_ftdi_reset(struct usbh_ftdi *ftdi_class)
  62. {
  63. struct usb_setup_packet *setup;
  64. if (!ftdi_class || !ftdi_class->hport) {
  65. return -USB_ERR_INVAL;
  66. }
  67. setup = ftdi_class->hport->setup;
  68. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  69. setup->bRequest = SIO_RESET_REQUEST;
  70. setup->wValue = 0;
  71. setup->wIndex = ftdi_class->intf;
  72. setup->wLength = 0;
  73. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  74. }
  75. static int usbh_ftdi_set_modem(struct usbh_ftdi *ftdi_class, uint16_t value)
  76. {
  77. struct usb_setup_packet *setup;
  78. if (!ftdi_class || !ftdi_class->hport) {
  79. return -USB_ERR_INVAL;
  80. }
  81. setup = ftdi_class->hport->setup;
  82. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  83. setup->bRequest = SIO_SET_MODEM_CTRL_REQUEST;
  84. setup->wValue = value;
  85. setup->wIndex = ftdi_class->intf;
  86. setup->wLength = 0;
  87. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  88. }
  89. static int usbh_ftdi_set_baudrate(struct usbh_ftdi *ftdi_class, uint32_t baudrate)
  90. {
  91. struct usb_setup_packet *setup;
  92. uint32_t itdf_divisor;
  93. uint16_t value;
  94. uint8_t baudrate_high;
  95. if (!ftdi_class || !ftdi_class->hport) {
  96. return -USB_ERR_INVAL;
  97. }
  98. setup = ftdi_class->hport->setup;
  99. usbh_ftdi_caculate_baudrate(&itdf_divisor, baudrate);
  100. value = itdf_divisor & 0xFFFF;
  101. baudrate_high = (itdf_divisor >> 16) & 0xff;
  102. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  103. setup->bRequest = SIO_SET_BAUDRATE_REQUEST;
  104. setup->wValue = value;
  105. setup->wIndex = (baudrate_high << 8) | ftdi_class->intf;
  106. setup->wLength = 0;
  107. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  108. }
  109. static int usbh_ftdi_set_data_format(struct usbh_ftdi *ftdi_class, uint8_t databits, uint8_t parity, uint8_t stopbits, uint8_t isbreak)
  110. {
  111. /**
  112. * D0-D7 databits BITS_7=7, BITS_8=8
  113. * D8-D10 parity NONE=0, ODD=1, EVEN=2, MARK=3, SPACE=4
  114. * D11-D12 STOP_BIT_1=0, STOP_BIT_15=1, STOP_BIT_2=2
  115. * D14 BREAK_OFF=0, BREAK_ON=1
  116. **/
  117. struct usb_setup_packet *setup;
  118. uint16_t value;
  119. if (!ftdi_class || !ftdi_class->hport) {
  120. return -USB_ERR_INVAL;
  121. }
  122. setup = ftdi_class->hport->setup;
  123. value = ((isbreak & 0x01) << 14) | ((stopbits & 0x03) << 11) | ((parity & 0x0f) << 8) | (databits & 0x0f);
  124. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  125. setup->bRequest = SIO_SET_DATA_REQUEST;
  126. setup->wValue = value;
  127. setup->wIndex = ftdi_class->intf;
  128. setup->wLength = 0;
  129. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  130. }
  131. static int usbh_ftdi_set_latency_timer(struct usbh_ftdi *ftdi_class, uint16_t value)
  132. {
  133. struct usb_setup_packet *setup;
  134. if (!ftdi_class || !ftdi_class->hport) {
  135. return -USB_ERR_INVAL;
  136. }
  137. setup = ftdi_class->hport->setup;
  138. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  139. setup->bRequest = SIO_SET_LATENCY_TIMER_REQUEST;
  140. setup->wValue = value;
  141. setup->wIndex = ftdi_class->intf;
  142. setup->wLength = 0;
  143. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  144. }
  145. static int usbh_ftdi_set_flow_ctrl(struct usbh_ftdi *ftdi_class, uint16_t value)
  146. {
  147. struct usb_setup_packet *setup;
  148. if (!ftdi_class || !ftdi_class->hport) {
  149. return -USB_ERR_INVAL;
  150. }
  151. setup = ftdi_class->hport->setup;
  152. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  153. setup->bRequest = SIO_SET_FLOW_CTRL_REQUEST;
  154. setup->wValue = value;
  155. setup->wIndex = ftdi_class->intf;
  156. setup->wLength = 0;
  157. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  158. }
  159. static int usbh_ftdi_read_modem_status(struct usbh_ftdi *ftdi_class)
  160. {
  161. struct usb_setup_packet *setup;
  162. int ret;
  163. if (!ftdi_class || !ftdi_class->hport) {
  164. return -USB_ERR_INVAL;
  165. }
  166. setup = ftdi_class->hport->setup;
  167. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  168. setup->bRequest = SIO_POLL_MODEM_STATUS_REQUEST;
  169. setup->wValue = 0x0000;
  170. setup->wIndex = ftdi_class->intf;
  171. setup->wLength = 2;
  172. ret = usbh_control_transfer(ftdi_class->hport, setup, g_ftdi_buf);
  173. if (ret < 0) {
  174. return ret;
  175. }
  176. memcpy(ftdi_class->modem_status, g_ftdi_buf, 2);
  177. return ret;
  178. }
  179. int usbh_ftdi_set_line_coding(struct usbh_ftdi *ftdi_class, struct cdc_line_coding *line_coding)
  180. {
  181. memcpy((uint8_t *)&ftdi_class->line_coding, line_coding, sizeof(struct cdc_line_coding));
  182. usbh_ftdi_set_baudrate(ftdi_class, line_coding->dwDTERate);
  183. return usbh_ftdi_set_data_format(ftdi_class, line_coding->bDataBits, line_coding->bParityType, line_coding->bCharFormat, 0);
  184. }
  185. int usbh_ftdi_get_line_coding(struct usbh_ftdi *ftdi_class, struct cdc_line_coding *line_coding)
  186. {
  187. memcpy(line_coding, (uint8_t *)&ftdi_class->line_coding, sizeof(struct cdc_line_coding));
  188. return 0;
  189. }
  190. int usbh_ftdi_set_line_state(struct usbh_ftdi *ftdi_class, bool dtr, bool rts)
  191. {
  192. int ret;
  193. if (dtr) {
  194. usbh_ftdi_set_modem(ftdi_class, SIO_SET_DTR_HIGH);
  195. } else {
  196. usbh_ftdi_set_modem(ftdi_class, SIO_SET_DTR_LOW);
  197. }
  198. if (rts) {
  199. ret = usbh_ftdi_set_modem(ftdi_class, SIO_SET_RTS_HIGH);
  200. } else {
  201. ret = usbh_ftdi_set_modem(ftdi_class, SIO_SET_RTS_LOW);
  202. }
  203. return ret;
  204. }
  205. static int usbh_ftdi_connect(struct usbh_hubport *hport, uint8_t intf)
  206. {
  207. struct usb_endpoint_descriptor *ep_desc;
  208. int ret = 0;
  209. struct usbh_ftdi *ftdi_class = usbh_ftdi_class_alloc();
  210. if (ftdi_class == NULL) {
  211. USB_LOG_ERR("Fail to alloc ftdi_class\r\n");
  212. return -USB_ERR_NOMEM;
  213. }
  214. ftdi_class->hport = hport;
  215. ftdi_class->intf = intf;
  216. hport->config.intf[intf].priv = ftdi_class;
  217. usbh_ftdi_reset(ftdi_class);
  218. usbh_ftdi_set_flow_ctrl(ftdi_class, SIO_DISABLE_FLOW_CTRL);
  219. usbh_ftdi_set_latency_timer(ftdi_class, 0x10);
  220. usbh_ftdi_read_modem_status(ftdi_class);
  221. USB_LOG_INFO("modem status:%02x:%02x\r\n", ftdi_class->modem_status[0], ftdi_class->modem_status[1]);
  222. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  223. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  224. if (ep_desc->bEndpointAddress & 0x80) {
  225. USBH_EP_INIT(ftdi_class->bulkin, ep_desc);
  226. } else {
  227. USBH_EP_INIT(ftdi_class->bulkout, ep_desc);
  228. }
  229. }
  230. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, ftdi_class->minor);
  231. USB_LOG_INFO("Register FTDI Class:%s\r\n", hport->config.intf[intf].devname);
  232. #if 0
  233. USB_LOG_INFO("Test ftdi rx and tx and rx for 5 times, baudrate is 115200\r\n");
  234. struct cdc_line_coding linecoding;
  235. uint8_t count = 5;
  236. linecoding.dwDTERate = 115200;
  237. linecoding.bDataBits = 8;
  238. linecoding.bParityType = 0;
  239. linecoding.bCharFormat = 0;
  240. usbh_ftdi_set_line_coding(ftdi_class, &linecoding);
  241. usbh_ftdi_set_line_state(ftdi_class, true, false);
  242. memset(g_ftdi_buf, 'a', sizeof(g_ftdi_buf));
  243. ret = usbh_ftdi_bulk_out_transfer(ftdi_class, g_ftdi_buf, sizeof(g_ftdi_buf), 0xfffffff);
  244. USB_LOG_RAW("out ret:%d\r\n", ret);
  245. while (count--) {
  246. ret = usbh_ftdi_bulk_in_transfer(ftdi_class, g_ftdi_buf, sizeof(g_ftdi_buf), 0xfffffff);
  247. USB_LOG_RAW("in ret:%d\r\n", ret);
  248. if (ret > 0) {
  249. for (uint32_t i = 0; i < ret; i++) {
  250. USB_LOG_RAW("%02x ", g_ftdi_buf[i]);
  251. }
  252. }
  253. USB_LOG_RAW("\r\n");
  254. }
  255. #endif
  256. usbh_ftdi_run(ftdi_class);
  257. return ret;
  258. }
  259. static int usbh_ftdi_disconnect(struct usbh_hubport *hport, uint8_t intf)
  260. {
  261. int ret = 0;
  262. struct usbh_ftdi *ftdi_class = (struct usbh_ftdi *)hport->config.intf[intf].priv;
  263. if (ftdi_class) {
  264. if (ftdi_class->bulkin) {
  265. usbh_kill_urb(&ftdi_class->bulkin_urb);
  266. }
  267. if (ftdi_class->bulkout) {
  268. usbh_kill_urb(&ftdi_class->bulkout_urb);
  269. }
  270. if (hport->config.intf[intf].devname[0] != '\0') {
  271. USB_LOG_INFO("Unregister FTDI Class:%s\r\n", hport->config.intf[intf].devname);
  272. usbh_ftdi_stop(ftdi_class);
  273. }
  274. usbh_ftdi_class_free(ftdi_class);
  275. }
  276. return ret;
  277. }
  278. int usbh_ftdi_bulk_in_transfer(struct usbh_ftdi *ftdi_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
  279. {
  280. int ret;
  281. struct usbh_urb *urb = &ftdi_class->bulkin_urb;
  282. usbh_bulk_urb_fill(urb, ftdi_class->hport, ftdi_class->bulkin, buffer, buflen, timeout, NULL, NULL);
  283. ret = usbh_submit_urb(urb);
  284. if (ret == 0) {
  285. ret = urb->actual_length;
  286. }
  287. return ret;
  288. }
  289. int usbh_ftdi_bulk_out_transfer(struct usbh_ftdi *ftdi_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
  290. {
  291. int ret;
  292. struct usbh_urb *urb = &ftdi_class->bulkout_urb;
  293. usbh_bulk_urb_fill(urb, ftdi_class->hport, ftdi_class->bulkout, buffer, buflen, timeout, NULL, NULL);
  294. ret = usbh_submit_urb(urb);
  295. if (ret == 0) {
  296. ret = urb->actual_length;
  297. }
  298. return ret;
  299. }
  300. __WEAK void usbh_ftdi_run(struct usbh_ftdi *ftdi_class)
  301. {
  302. (void)ftdi_class;
  303. }
  304. __WEAK void usbh_ftdi_stop(struct usbh_ftdi *ftdi_class)
  305. {
  306. (void)ftdi_class;
  307. }
  308. static const uint16_t ftdi_id_table[][2] = {
  309. { 0x0403, 0x6001 },
  310. { 0x0403, 0x6010 },
  311. { 0, 0 },
  312. };
  313. const struct usbh_class_driver ftdi_class_driver = {
  314. .driver_name = "ftdi",
  315. .connect = usbh_ftdi_connect,
  316. .disconnect = usbh_ftdi_disconnect
  317. };
  318. CLASS_INFO_DEFINE const struct usbh_class_info ftdi_class_info = {
  319. .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
  320. .class = 0xff,
  321. .subclass = 0x00,
  322. .protocol = 0x00,
  323. .id_table = ftdi_id_table,
  324. .class_driver = &ftdi_class_driver
  325. };