usbh_ftdi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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[USB_ALIGN_UP(64, CONFIG_USB_ALIGN_SIZE)];
  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 const char *ftdi_chip_name[] = {
  14. [SIO] = "SIO", /* the serial part of FT8U100AX */
  15. [FT232A] = "FT232A",
  16. [FT232B] = "FT232B",
  17. [FT2232C] = "FT2232C/D",
  18. [FT232R] = "FT232R",
  19. [FT232H] = "FT232H",
  20. [FT2232H] = "FT2232H",
  21. [FT4232H] = "FT4232H",
  22. [FT4232HA] = "FT4232HA",
  23. [FT232HP] = "FT232HP",
  24. [FT233HP] = "FT233HP",
  25. [FT2232HP] = "FT2232HP",
  26. [FT2233HP] = "FT2233HP",
  27. [FT4232HP] = "FT4232HP",
  28. [FT4233HP] = "FT4233HP",
  29. [FTX] = "FT-X",
  30. };
  31. static struct usbh_ftdi *usbh_ftdi_class_alloc(void)
  32. {
  33. uint8_t devno;
  34. for (devno = 0; devno < CONFIG_USBHOST_MAX_FTDI_CLASS; devno++) {
  35. if ((g_devinuse & (1U << devno)) == 0) {
  36. g_devinuse |= (1U << devno);
  37. memset(&g_ftdi_class[devno], 0, sizeof(struct usbh_ftdi));
  38. g_ftdi_class[devno].minor = devno;
  39. return &g_ftdi_class[devno];
  40. }
  41. }
  42. return NULL;
  43. }
  44. static void usbh_ftdi_class_free(struct usbh_ftdi *ftdi_class)
  45. {
  46. uint8_t devno = ftdi_class->minor;
  47. if (devno < 32) {
  48. g_devinuse &= ~(1U << devno);
  49. }
  50. memset(ftdi_class, 0, sizeof(struct usbh_ftdi));
  51. }
  52. /*
  53. * Divide positive or negative dividend by positive or negative divisor
  54. * and round to closest integer. Result is undefined for negative
  55. * divisors if the dividend variable type is unsigned and for negative
  56. * dividends if the divisor variable type is unsigned.
  57. */
  58. #define DIV_ROUND_CLOSEST(x, divisor) ( \
  59. { \
  60. typeof(x) __x = x; \
  61. typeof(divisor) __d = divisor; \
  62. (((typeof(x))-1) > 0 || \
  63. ((typeof(divisor))-1) > 0 || \
  64. (((__x) > 0) == ((__d) > 0))) ? \
  65. (((__x) + ((__d) / 2)) / (__d)) : \
  66. (((__x) - ((__d) / 2)) / (__d)); \
  67. })
  68. static uint32_t ftdi_232bm_baud_base_to_divisor(uint32_t baud, int base)
  69. {
  70. static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
  71. uint32_t divisor;
  72. /* divisor shifted 3 bits to the left */
  73. int divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud);
  74. divisor = divisor3 >> 3;
  75. divisor |= (uint32_t)divfrac[divisor3 & 0x7] << 14;
  76. /* Deal with special cases for highest baud rates. */
  77. if (divisor == 1) /* 1.0 */
  78. divisor = 0;
  79. else if (divisor == 0x4001) /* 1.5 */
  80. divisor = 1;
  81. return divisor;
  82. }
  83. static uint32_t ftdi_232bm_baud_to_divisor(uint32_t baud)
  84. {
  85. return ftdi_232bm_baud_base_to_divisor(baud, 48000000);
  86. }
  87. static uint32_t ftdi_2232h_baud_base_to_divisor(uint32_t baud, int base)
  88. {
  89. static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
  90. uint32_t divisor;
  91. int divisor3;
  92. /* hi-speed baud rate is 10-bit sampling instead of 16-bit */
  93. divisor3 = DIV_ROUND_CLOSEST(8 * base, 10 * baud);
  94. divisor = divisor3 >> 3;
  95. divisor |= (uint32_t)divfrac[divisor3 & 0x7] << 14;
  96. /* Deal with special cases for highest baud rates. */
  97. if (divisor == 1) /* 1.0 */
  98. divisor = 0;
  99. else if (divisor == 0x4001) /* 1.5 */
  100. divisor = 1;
  101. /*
  102. * Set this bit to turn off a divide by 2.5 on baud rate generator
  103. * This enables baud rates up to 12Mbaud but cannot reach below 1200
  104. * baud with this bit set
  105. */
  106. divisor |= 0x00020000;
  107. return divisor;
  108. }
  109. static uint32_t ftdi_2232h_baud_to_divisor(uint32_t baud)
  110. {
  111. return ftdi_2232h_baud_base_to_divisor(baud, 120000000);
  112. }
  113. int usbh_ftdi_reset(struct usbh_ftdi *ftdi_class)
  114. {
  115. struct usb_setup_packet *setup;
  116. if (!ftdi_class || !ftdi_class->hport) {
  117. return -USB_ERR_INVAL;
  118. }
  119. setup = ftdi_class->hport->setup;
  120. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  121. setup->bRequest = SIO_RESET_REQUEST;
  122. setup->wValue = 0;
  123. setup->wIndex = ftdi_class->intf;
  124. setup->wLength = 0;
  125. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  126. }
  127. static int usbh_ftdi_set_modem(struct usbh_ftdi *ftdi_class, uint16_t value)
  128. {
  129. struct usb_setup_packet *setup;
  130. if (!ftdi_class || !ftdi_class->hport) {
  131. return -USB_ERR_INVAL;
  132. }
  133. setup = ftdi_class->hport->setup;
  134. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  135. setup->bRequest = SIO_SET_MODEM_CTRL_REQUEST;
  136. setup->wValue = value;
  137. setup->wIndex = ftdi_class->intf;
  138. setup->wLength = 0;
  139. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  140. }
  141. static int usbh_ftdi_set_baudrate(struct usbh_ftdi *ftdi_class, uint32_t baudrate)
  142. {
  143. struct usb_setup_packet *setup;
  144. uint32_t div_value;
  145. uint16_t value;
  146. uint8_t baudrate_high;
  147. if (!ftdi_class || !ftdi_class->hport) {
  148. return -USB_ERR_INVAL;
  149. }
  150. setup = ftdi_class->hport->setup;
  151. switch (ftdi_class->chip_type) {
  152. case FT232B:
  153. case FT2232C:
  154. case FT232R:
  155. if (baudrate > 3000000) {
  156. return -USB_ERR_INVAL;
  157. }
  158. div_value = ftdi_232bm_baud_to_divisor(baudrate);
  159. break;
  160. default:
  161. if ((baudrate <= 12000000) && (baudrate >= 1200)) {
  162. div_value = ftdi_2232h_baud_to_divisor(baudrate);
  163. } else {
  164. return -USB_ERR_INVAL;
  165. }
  166. break;
  167. }
  168. value = div_value & 0xFFFF;
  169. baudrate_high = (div_value >> 16) & 0xff;
  170. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  171. setup->bRequest = SIO_SET_BAUDRATE_REQUEST;
  172. setup->wValue = value;
  173. setup->wIndex = (baudrate_high << 8) | ftdi_class->intf;
  174. setup->wLength = 0;
  175. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  176. }
  177. static int usbh_ftdi_set_data_format(struct usbh_ftdi *ftdi_class, uint8_t databits, uint8_t parity, uint8_t stopbits, uint8_t isbreak)
  178. {
  179. /**
  180. * D0-D7 databits BITS_7=7, BITS_8=8
  181. * D8-D10 parity NONE=0, ODD=1, EVEN=2, MARK=3, SPACE=4
  182. * D11-D12 STOP_BIT_1=0, STOP_BIT_15=1, STOP_BIT_2=2
  183. * D14 BREAK_OFF=0, BREAK_ON=1
  184. **/
  185. struct usb_setup_packet *setup;
  186. uint16_t value;
  187. if (!ftdi_class || !ftdi_class->hport) {
  188. return -USB_ERR_INVAL;
  189. }
  190. setup = ftdi_class->hport->setup;
  191. value = ((isbreak & 0x01) << 14) | ((stopbits & 0x03) << 11) | ((parity & 0x0f) << 8) | (databits & 0x0f);
  192. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  193. setup->bRequest = SIO_SET_DATA_REQUEST;
  194. setup->wValue = value;
  195. setup->wIndex = ftdi_class->intf;
  196. setup->wLength = 0;
  197. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  198. }
  199. static int usbh_ftdi_set_latency_timer(struct usbh_ftdi *ftdi_class, uint16_t value)
  200. {
  201. struct usb_setup_packet *setup;
  202. if (!ftdi_class || !ftdi_class->hport) {
  203. return -USB_ERR_INVAL;
  204. }
  205. setup = ftdi_class->hport->setup;
  206. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  207. setup->bRequest = SIO_SET_LATENCY_TIMER_REQUEST;
  208. setup->wValue = value;
  209. setup->wIndex = ftdi_class->intf;
  210. setup->wLength = 0;
  211. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  212. }
  213. static int usbh_ftdi_set_flow_ctrl(struct usbh_ftdi *ftdi_class, uint16_t value)
  214. {
  215. struct usb_setup_packet *setup;
  216. if (!ftdi_class || !ftdi_class->hport) {
  217. return -USB_ERR_INVAL;
  218. }
  219. setup = ftdi_class->hport->setup;
  220. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  221. setup->bRequest = SIO_SET_FLOW_CTRL_REQUEST;
  222. setup->wValue = value;
  223. setup->wIndex = ftdi_class->intf;
  224. setup->wLength = 0;
  225. return usbh_control_transfer(ftdi_class->hport, setup, NULL);
  226. }
  227. static int usbh_ftdi_read_modem_status(struct usbh_ftdi *ftdi_class)
  228. {
  229. struct usb_setup_packet *setup;
  230. int ret;
  231. if (!ftdi_class || !ftdi_class->hport) {
  232. return -USB_ERR_INVAL;
  233. }
  234. setup = ftdi_class->hport->setup;
  235. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  236. setup->bRequest = SIO_POLL_MODEM_STATUS_REQUEST;
  237. setup->wValue = 0x0000;
  238. setup->wIndex = ftdi_class->intf;
  239. setup->wLength = 2;
  240. ret = usbh_control_transfer(ftdi_class->hport, setup, g_ftdi_buf);
  241. if (ret < 0) {
  242. return ret;
  243. }
  244. memcpy(ftdi_class->modem_status, g_ftdi_buf, 2);
  245. return ret;
  246. }
  247. int usbh_ftdi_set_line_coding(struct usbh_ftdi *ftdi_class, struct cdc_line_coding *line_coding)
  248. {
  249. memcpy((uint8_t *)&ftdi_class->line_coding, line_coding, sizeof(struct cdc_line_coding));
  250. int ret = usbh_ftdi_set_baudrate(ftdi_class, line_coding->dwDTERate);
  251. if (ret < 0) {
  252. return ret;
  253. }
  254. return usbh_ftdi_set_data_format(ftdi_class, line_coding->bDataBits, line_coding->bParityType, line_coding->bCharFormat, 0);
  255. }
  256. int usbh_ftdi_get_line_coding(struct usbh_ftdi *ftdi_class, struct cdc_line_coding *line_coding)
  257. {
  258. memcpy(line_coding, (uint8_t *)&ftdi_class->line_coding, sizeof(struct cdc_line_coding));
  259. return 0;
  260. }
  261. int usbh_ftdi_set_line_state(struct usbh_ftdi *ftdi_class, bool dtr, bool rts)
  262. {
  263. int ret;
  264. if (dtr) {
  265. usbh_ftdi_set_modem(ftdi_class, SIO_SET_DTR_HIGH);
  266. } else {
  267. usbh_ftdi_set_modem(ftdi_class, SIO_SET_DTR_LOW);
  268. }
  269. if (rts) {
  270. ret = usbh_ftdi_set_modem(ftdi_class, SIO_SET_RTS_HIGH);
  271. } else {
  272. ret = usbh_ftdi_set_modem(ftdi_class, SIO_SET_RTS_LOW);
  273. }
  274. return ret;
  275. }
  276. static int usbh_ftdi_connect(struct usbh_hubport *hport, uint8_t intf)
  277. {
  278. struct usb_endpoint_descriptor *ep_desc;
  279. int ret = 0;
  280. uint16_t version;
  281. struct usbh_ftdi *ftdi_class = usbh_ftdi_class_alloc();
  282. if (ftdi_class == NULL) {
  283. USB_LOG_ERR("Fail to alloc ftdi_class\r\n");
  284. return -USB_ERR_NOMEM;
  285. }
  286. ftdi_class->hport = hport;
  287. ftdi_class->intf = intf;
  288. hport->config.intf[intf].priv = ftdi_class;
  289. version = hport->device_desc.bcdDevice;
  290. switch (version) {
  291. case 0x400:
  292. ftdi_class->chip_type = FT232B;
  293. break;
  294. case 0x500:
  295. ftdi_class->chip_type = FT2232C;
  296. break;
  297. case 0x600:
  298. ftdi_class->chip_type = FT232R;
  299. break;
  300. case 0x700:
  301. ftdi_class->chip_type = FT2232H;
  302. break;
  303. case 0x800:
  304. ftdi_class->chip_type = FT4232H;
  305. break;
  306. case 0x900:
  307. ftdi_class->chip_type = FT232H;
  308. break;
  309. default:
  310. USB_LOG_ERR("Unknown FTDI chip version:%04x\r\n", version);
  311. return -USB_ERR_NOTSUPP;
  312. }
  313. USB_LOG_INFO("FTDI chip name:%s\r\n", ftdi_chip_name[ftdi_class->chip_type]);
  314. usbh_ftdi_reset(ftdi_class);
  315. usbh_ftdi_set_flow_ctrl(ftdi_class, SIO_DISABLE_FLOW_CTRL);
  316. usbh_ftdi_set_latency_timer(ftdi_class, 0x10);
  317. usbh_ftdi_read_modem_status(ftdi_class);
  318. USB_LOG_INFO("modem status:%02x:%02x\r\n", ftdi_class->modem_status[0], ftdi_class->modem_status[1]);
  319. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  320. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  321. if (ep_desc->bEndpointAddress & 0x80) {
  322. USBH_EP_INIT(ftdi_class->bulkin, ep_desc);
  323. } else {
  324. USBH_EP_INIT(ftdi_class->bulkout, ep_desc);
  325. }
  326. }
  327. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, ftdi_class->minor);
  328. USB_LOG_INFO("Register FTDI Class:%s\r\n", hport->config.intf[intf].devname);
  329. #if 0
  330. USB_LOG_INFO("Test ftdi rx and tx and rx for 5 times, baudrate is 115200\r\n");
  331. struct cdc_line_coding linecoding;
  332. uint8_t count = 5;
  333. linecoding.dwDTERate = 115200;
  334. linecoding.bDataBits = 8;
  335. linecoding.bParityType = 0;
  336. linecoding.bCharFormat = 0;
  337. usbh_ftdi_set_line_coding(ftdi_class, &linecoding);
  338. usbh_ftdi_set_line_state(ftdi_class, true, false);
  339. memset(g_ftdi_buf, 'a', sizeof(g_ftdi_buf));
  340. ret = usbh_ftdi_bulk_out_transfer(ftdi_class, g_ftdi_buf, sizeof(g_ftdi_buf), 0xfffffff);
  341. USB_LOG_RAW("out ret:%d\r\n", ret);
  342. while (count--) {
  343. ret = usbh_ftdi_bulk_in_transfer(ftdi_class, g_ftdi_buf, sizeof(g_ftdi_buf), 0xfffffff);
  344. USB_LOG_RAW("in ret:%d\r\n", ret);
  345. if (ret > 0) {
  346. for (uint32_t i = 0; i < ret; i++) {
  347. USB_LOG_RAW("%02x ", g_ftdi_buf[i]);
  348. }
  349. }
  350. USB_LOG_RAW("\r\n");
  351. }
  352. #endif
  353. usbh_ftdi_run(ftdi_class);
  354. return ret;
  355. }
  356. static int usbh_ftdi_disconnect(struct usbh_hubport *hport, uint8_t intf)
  357. {
  358. int ret = 0;
  359. struct usbh_ftdi *ftdi_class = (struct usbh_ftdi *)hport->config.intf[intf].priv;
  360. if (ftdi_class) {
  361. if (ftdi_class->bulkin) {
  362. usbh_kill_urb(&ftdi_class->bulkin_urb);
  363. }
  364. if (ftdi_class->bulkout) {
  365. usbh_kill_urb(&ftdi_class->bulkout_urb);
  366. }
  367. if (hport->config.intf[intf].devname[0] != '\0') {
  368. usb_osal_thread_schedule_other();
  369. USB_LOG_INFO("Unregister FTDI Class:%s\r\n", hport->config.intf[intf].devname);
  370. usbh_ftdi_stop(ftdi_class);
  371. }
  372. usbh_ftdi_class_free(ftdi_class);
  373. }
  374. return ret;
  375. }
  376. int usbh_ftdi_bulk_in_transfer(struct usbh_ftdi *ftdi_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
  377. {
  378. int ret;
  379. struct usbh_urb *urb = &ftdi_class->bulkin_urb;
  380. usbh_bulk_urb_fill(urb, ftdi_class->hport, ftdi_class->bulkin, buffer, buflen, timeout, NULL, NULL);
  381. ret = usbh_submit_urb(urb);
  382. if (ret == 0) {
  383. ret = urb->actual_length;
  384. }
  385. return ret;
  386. }
  387. int usbh_ftdi_bulk_out_transfer(struct usbh_ftdi *ftdi_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
  388. {
  389. int ret;
  390. struct usbh_urb *urb = &ftdi_class->bulkout_urb;
  391. usbh_bulk_urb_fill(urb, ftdi_class->hport, ftdi_class->bulkout, buffer, buflen, timeout, NULL, NULL);
  392. ret = usbh_submit_urb(urb);
  393. if (ret == 0) {
  394. ret = urb->actual_length;
  395. }
  396. return ret;
  397. }
  398. __WEAK void usbh_ftdi_run(struct usbh_ftdi *ftdi_class)
  399. {
  400. (void)ftdi_class;
  401. }
  402. __WEAK void usbh_ftdi_stop(struct usbh_ftdi *ftdi_class)
  403. {
  404. (void)ftdi_class;
  405. }
  406. static const uint16_t ftdi_id_table[][2] = {
  407. { 0x0403, 0x6001 },
  408. { 0x0403, 0x6010 },
  409. { 0, 0 },
  410. };
  411. const struct usbh_class_driver ftdi_class_driver = {
  412. .driver_name = "ftdi",
  413. .connect = usbh_ftdi_connect,
  414. .disconnect = usbh_ftdi_disconnect
  415. };
  416. CLASS_INFO_DEFINE const struct usbh_class_info ftdi_class_info = {
  417. .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
  418. .bInterfaceClass = 0xff,
  419. .bInterfaceSubClass = 0x00,
  420. .bInterfaceProtocol = 0x00,
  421. .id_table = ftdi_id_table,
  422. .class_driver = &ftdi_class_driver
  423. };