usbh_pl2303.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. * Copyright (c) 2024, Derek Konigsberg
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include "usbh_core.h"
  8. #include "usbh_pl2303.h"
  9. #undef USB_DBG_TAG
  10. #define USB_DBG_TAG "usbh_pl2303"
  11. #include "usb_log.h"
  12. #define DEV_FORMAT "/dev/ttyUSB%d"
  13. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_pl2303_buf[64];
  14. #define CONFIG_USBHOST_MAX_PL2303_CLASS 1
  15. #define UT_WRITE_VENDOR_DEVICE (USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE)
  16. #define UT_READ_VENDOR_DEVICE (USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE)
  17. static struct usbh_pl2303 g_pl2303_class[CONFIG_USBHOST_MAX_PL2303_CLASS];
  18. static uint32_t g_devinuse = 0;
  19. static struct usbh_pl2303 *usbh_pl2303_class_alloc(void)
  20. {
  21. uint8_t devno;
  22. for (devno = 0; devno < CONFIG_USBHOST_MAX_PL2303_CLASS; devno++) {
  23. if ((g_devinuse & (1U << devno)) == 0) {
  24. g_devinuse |= (1U << devno);
  25. memset(&g_pl2303_class[devno], 0, sizeof(struct usbh_pl2303));
  26. g_pl2303_class[devno].minor = devno;
  27. return &g_pl2303_class[devno];
  28. }
  29. }
  30. return NULL;
  31. }
  32. static void usbh_pl2303_class_free(struct usbh_pl2303 *pl2303_class)
  33. {
  34. uint8_t devno = pl2303_class->minor;
  35. if (devno < 32) {
  36. g_devinuse &= ~(1U << devno);
  37. }
  38. memset(pl2303_class, 0, sizeof(struct usbh_pl2303));
  39. }
  40. static int usbh_pl2303_get_chiptype(struct usbh_pl2303 *pl2303_class)
  41. {
  42. int ret = 0;
  43. switch (pl2303_class->hport->device_desc.bcdDevice) {
  44. case 0x0300:
  45. pl2303_class->chiptype = USBH_PL2303_TYPE_PL2303HX;
  46. /* or TA, that is HX with external crystal */
  47. break;
  48. case 0x0400:
  49. pl2303_class->chiptype = USBH_PL2303_TYPE_PL2303HXD;
  50. /* or EA, that is HXD with ESD protection */
  51. /* or RA, that has internal voltage level converter that works only up to 1Mbaud (!) */
  52. break;
  53. case 0x0500:
  54. pl2303_class->chiptype = USBH_PL2303_TYPE_PL2303HXD;
  55. /* in fact it's TB, that is HXD with external crystal */
  56. break;
  57. default:
  58. /* NOTE: I have no info about the bcdDevice for the base PL2303 (up to 1.2Mbaud,
  59. only fixed rates) and for PL2303SA (8-pin chip, up to 115200 baud */
  60. /* Determine the chip type. This algorithm is taken from Linux. */
  61. if (pl2303_class->hport->device_desc.bDeviceClass == 0x02) {
  62. pl2303_class->chiptype = USBH_PL2303_TYPE_PL2303;
  63. } else if (pl2303_class->hport->device_desc.bMaxPacketSize0 == 0x40) {
  64. pl2303_class->chiptype = USBH_PL2303_TYPE_PL2303HX;
  65. } else {
  66. pl2303_class->chiptype = USBH_PL2303_TYPE_PL2303;
  67. }
  68. break;
  69. }
  70. /*
  71. * The new chip revision PL2303HXN is only compatible with the new
  72. * PLCOM_SET_REQUEST_PL2303HXN command. Issuing the old command
  73. * PLCOM_SET_REQUEST to the new chip raises an error. Thus, PL2303HX
  74. * and PL2303HXN can be distinguished by issuing an old-style request
  75. * (on a status register) to the new chip and checking the error.
  76. */
  77. if (pl2303_class->chiptype == USBH_PL2303_TYPE_PL2303HX) {
  78. struct usb_setup_packet *setup = pl2303_class->hport->setup;
  79. setup->bmRequestType = UT_READ_VENDOR_DEVICE;
  80. setup->bRequest = PL2303_SET_REQUEST;
  81. setup->wValue = PL2303_STATUS_REG_PL2303HX;
  82. setup->wIndex = 0;
  83. setup->wLength = 1;
  84. ret = usbh_control_transfer(pl2303_class->hport, setup, g_pl2303_buf);
  85. if (ret == -USB_ERR_STALL) {
  86. pl2303_class->chiptype = USBH_PL2303_TYPE_PL2303HXN;
  87. ret = 0;
  88. } else if (ret < 0) {
  89. USB_LOG_WRN("Error checking chip type: %d\r\n", ret);
  90. return ret;
  91. }
  92. }
  93. switch (pl2303_class->chiptype) {
  94. case USBH_PL2303_TYPE_PL2303:
  95. USB_LOG_INFO("chiptype = 2303\r\n");
  96. break;
  97. case USBH_PL2303_TYPE_PL2303HX:
  98. USB_LOG_INFO("chiptype = 2303HX/TA\r\n");
  99. break;
  100. case USBH_PL2303_TYPE_PL2303HXN:
  101. USB_LOG_INFO("chiptype = 2303HXN\r\n");
  102. break;
  103. case USBH_PL2303_TYPE_PL2303HXD:
  104. USB_LOG_INFO("chiptype = 2303HXD/TB/RA/EA\r\n");
  105. break;
  106. default:
  107. USB_LOG_INFO("chiptype = [%d]\r\n", pl2303_class->chiptype);
  108. break;
  109. }
  110. return ret;
  111. }
  112. static int usbh_pl2303_do(struct usbh_pl2303 *pl2303_class,
  113. uint8_t req_type, uint8_t request, uint16_t value, uint16_t index,
  114. uint16_t length)
  115. {
  116. struct usb_setup_packet *setup;
  117. if (!pl2303_class || !pl2303_class->hport) {
  118. return -USB_ERR_INVAL;
  119. }
  120. setup = pl2303_class->hport->setup;
  121. setup->bmRequestType = req_type;
  122. setup->bRequest = request;
  123. setup->wValue = value;
  124. setup->wIndex = index;
  125. setup->wLength = length;
  126. return usbh_control_transfer(pl2303_class->hport, setup, g_pl2303_buf);
  127. }
  128. int usbh_pl2303_set_line_coding(struct usbh_pl2303 *pl2303_class, struct cdc_line_coding *line_coding)
  129. {
  130. struct usb_setup_packet *setup;
  131. if (!pl2303_class || !pl2303_class->hport) {
  132. return -USB_ERR_INVAL;
  133. }
  134. setup = pl2303_class->hport->setup;
  135. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  136. setup->bRequest = CDC_REQUEST_SET_LINE_CODING;
  137. setup->wValue = 0;
  138. setup->wIndex = pl2303_class->intf;
  139. setup->wLength = 7;
  140. memcpy(g_pl2303_buf, line_coding, sizeof(struct cdc_line_coding));
  141. return usbh_control_transfer(pl2303_class->hport, setup, g_pl2303_buf);
  142. }
  143. int usbh_pl2303_get_line_coding(struct usbh_pl2303 *pl2303_class, struct cdc_line_coding *line_coding)
  144. {
  145. struct usb_setup_packet *setup;
  146. int ret;
  147. if (!pl2303_class || !pl2303_class->hport) {
  148. return -USB_ERR_INVAL;
  149. }
  150. setup = pl2303_class->hport->setup;
  151. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  152. setup->bRequest = CDC_REQUEST_GET_LINE_CODING;
  153. setup->wValue = 0;
  154. setup->wIndex = pl2303_class->intf;
  155. setup->wLength = 7;
  156. ret = usbh_control_transfer(pl2303_class->hport, setup, g_pl2303_buf);
  157. if (ret < 0) {
  158. return ret;
  159. }
  160. memcpy(line_coding, g_pl2303_buf, sizeof(struct cdc_line_coding));
  161. return ret;
  162. }
  163. int usbh_pl2303_set_line_state(struct usbh_pl2303 *pl2303_class, bool dtr, bool rts)
  164. {
  165. struct usb_setup_packet *setup;
  166. if (!pl2303_class || !pl2303_class->hport) {
  167. return -USB_ERR_INVAL;
  168. }
  169. setup = pl2303_class->hport->setup;
  170. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  171. setup->bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE;
  172. setup->wValue = (dtr << 0) | (rts << 1);
  173. setup->wIndex = pl2303_class->intf;
  174. setup->wLength = 0;
  175. return usbh_control_transfer(pl2303_class->hport, setup, NULL);
  176. }
  177. static int usbh_pl2303_connect(struct usbh_hubport *hport, uint8_t intf)
  178. {
  179. struct usb_endpoint_descriptor *ep_desc;
  180. int ret = 0;
  181. struct usbh_pl2303 *pl2303_class = usbh_pl2303_class_alloc();
  182. if (pl2303_class == NULL) {
  183. USB_LOG_ERR("Fail to alloc pl2303_class\r\n");
  184. return -USB_ERR_NOMEM;
  185. }
  186. pl2303_class->hport = hport;
  187. pl2303_class->intf = intf;
  188. hport->config.intf[intf].priv = pl2303_class;
  189. do {
  190. ret = usbh_pl2303_get_chiptype(pl2303_class);
  191. if (ret < 0) {
  192. break;
  193. }
  194. /* Startup reset sequence, if necessary for the chip type */
  195. if (pl2303_class->chiptype != USBH_PL2303_TYPE_PL2303HXN) {
  196. struct usb_setup_packet *setup = pl2303_class->hport->setup;
  197. setup->bmRequestType = UT_WRITE_VENDOR_DEVICE;
  198. setup->bRequest = PL2303_SET_REQUEST;
  199. setup->wValue = 0;
  200. setup->wIndex = pl2303_class->intf;
  201. setup->wLength = 0;
  202. ret = usbh_control_transfer(pl2303_class->hport, setup, g_pl2303_buf);
  203. if (ret < 0) {
  204. USB_LOG_WRN("Initialization reset failed: %d\r\n", ret);
  205. break;
  206. }
  207. }
  208. if (pl2303_class->chiptype == USBH_PL2303_TYPE_PL2303) {
  209. /* HX variants seem to lock up after a clear stall request. */
  210. /*
  211. * The FreeBSD code sets the stall flags on the in and out pipes
  212. * here. Have no idea exactly how to do this, or if it is necessary.
  213. * May just leave this code unwritten until test hardware is available.
  214. */
  215. } else if (pl2303_class->chiptype == USBH_PL2303_TYPE_PL2303HX || pl2303_class->chiptype == USBH_PL2303_TYPE_PL2303HXD) {
  216. /* Reset upstream data pipes */
  217. ret = usbh_pl2303_do(pl2303_class, UT_WRITE_VENDOR_DEVICE, PL2303_SET_REQUEST, 8, 0, 0);
  218. if (ret < 0) {
  219. USB_LOG_WRN("Could not reset upstream data pipes (8,0): %d\r\n", ret);
  220. break;
  221. }
  222. ret = usbh_pl2303_do(pl2303_class, UT_WRITE_VENDOR_DEVICE, PL2303_SET_REQUEST, 9, 0, 0);
  223. if (ret < 0) {
  224. USB_LOG_WRN("Could not reset upstream data pipes (9,0): %d\r\n", ret);
  225. break;
  226. }
  227. } else if (pl2303_class->chiptype == USBH_PL2303_TYPE_PL2303HXN) {
  228. /* Reset upstream data pipes */
  229. ret = usbh_pl2303_do(pl2303_class, UT_WRITE_VENDOR_DEVICE, PL2303_SET_REQUEST_PL2303HXN, 0x07, 0x03, 0);
  230. if (ret < 0) {
  231. USB_LOG_WRN("Could not reset upstream data pipes (7,3): %d\r\n", ret);
  232. break;
  233. }
  234. }
  235. /* Final device initialization, if necessary for the chip type */
  236. if (pl2303_class->chiptype != USBH_PL2303_TYPE_PL2303HXN) {
  237. if (usbh_pl2303_do(pl2303_class, UT_READ_VENDOR_DEVICE, PL2303_SET_REQUEST, 0x8484, 0, 1) < 0 ||
  238. usbh_pl2303_do(pl2303_class, UT_WRITE_VENDOR_DEVICE, PL2303_SET_REQUEST, 0x0404, 0, 0) < 0 ||
  239. usbh_pl2303_do(pl2303_class, UT_READ_VENDOR_DEVICE, PL2303_SET_REQUEST, 0x8484, 0, 1) < 0 ||
  240. usbh_pl2303_do(pl2303_class, UT_READ_VENDOR_DEVICE, PL2303_SET_REQUEST, 0x8383, 0, 1) < 0 ||
  241. usbh_pl2303_do(pl2303_class, UT_READ_VENDOR_DEVICE, PL2303_SET_REQUEST, 0x8484, 0, 1) < 0 ||
  242. usbh_pl2303_do(pl2303_class, UT_WRITE_VENDOR_DEVICE, PL2303_SET_REQUEST, 0x0404, 1, 0) < 0 ||
  243. usbh_pl2303_do(pl2303_class, UT_READ_VENDOR_DEVICE, PL2303_SET_REQUEST, 0x8484, 0, 1) < 0 ||
  244. usbh_pl2303_do(pl2303_class, UT_READ_VENDOR_DEVICE, PL2303_SET_REQUEST, 0x8383, 0, 1) < 0 ||
  245. usbh_pl2303_do(pl2303_class, UT_WRITE_VENDOR_DEVICE, PL2303_SET_REQUEST, 0, 1, 0) < 0 ||
  246. usbh_pl2303_do(pl2303_class, UT_WRITE_VENDOR_DEVICE, PL2303_SET_REQUEST, 1, 0, 0) < 0) {
  247. USB_LOG_WRN("Could not complete init sequence\r\n");
  248. ret = -USB_ERR_INVAL;
  249. break;
  250. }
  251. if (pl2303_class->chiptype != USBH_PL2303_TYPE_PL2303) {
  252. ret = usbh_pl2303_do(pl2303_class, UT_WRITE_VENDOR_DEVICE, PL2303_SET_REQUEST, 2, 0x44, 0);
  253. } else {
  254. ret = usbh_pl2303_do(pl2303_class, UT_WRITE_VENDOR_DEVICE, PL2303_SET_REQUEST, 2, 0x24, 0);
  255. }
  256. if (ret < 0) {
  257. USB_LOG_WRN("Could not complete final init request: %d\r\n", ret);
  258. break;
  259. }
  260. }
  261. } while (0);
  262. if (ret < 0) {
  263. USB_LOG_ERR("Failed to initialize PL2303 device: %d\r\n", ret);
  264. return ret;
  265. }
  266. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  267. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  268. if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) {
  269. continue;
  270. } else {
  271. if (ep_desc->bEndpointAddress & 0x80) {
  272. USBH_EP_INIT(pl2303_class->bulkin, ep_desc);
  273. } else {
  274. USBH_EP_INIT(pl2303_class->bulkout, ep_desc);
  275. }
  276. }
  277. }
  278. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, pl2303_class->minor);
  279. USB_LOG_INFO("Register PL2303 Class:%s\r\n", hport->config.intf[intf].devname);
  280. #if 0
  281. USB_LOG_INFO("Test pl2303 rx and tx and rx for 5 times, baudrate is 115200\r\n");
  282. struct cdc_line_coding linecoding;
  283. uint8_t count = 5;
  284. linecoding.dwDTERate = 115200;
  285. linecoding.bDataBits = 8;
  286. linecoding.bParityType = 0;
  287. linecoding.bCharFormat = 0;
  288. usbh_pl2303_set_line_coding(pl2303_class, &linecoding);
  289. usbh_pl2303_set_line_state(pl2303_class, true, false);
  290. memset(g_pl2303_buf, 'a', sizeof(g_pl2303_buf));
  291. ret = usbh_pl2303_bulk_out_transfer(pl2303_class, g_pl2303_buf, sizeof(g_pl2303_buf), 0xfffffff);
  292. USB_LOG_RAW("out ret:%d\r\n", ret);
  293. while (count--) {
  294. ret = usbh_pl2303_bulk_in_transfer(pl2303_class, g_pl2303_buf, sizeof(g_pl2303_buf), 0xfffffff);
  295. USB_LOG_RAW("in ret:%d\r\n", ret);
  296. if (ret > 0) {
  297. for (uint32_t i = 0; i < ret; i++) {
  298. USB_LOG_RAW("%02x ", g_pl2303_buf[i]);
  299. }
  300. }
  301. USB_LOG_RAW("\r\n");
  302. }
  303. #endif
  304. usbh_pl2303_run(pl2303_class);
  305. return ret;
  306. }
  307. static int usbh_pl2303_disconnect(struct usbh_hubport *hport, uint8_t intf)
  308. {
  309. int ret = 0;
  310. struct usbh_pl2303 *pl2303_class = (struct usbh_pl2303 *)hport->config.intf[intf].priv;
  311. if (pl2303_class) {
  312. if (pl2303_class->bulkin) {
  313. usbh_kill_urb(&pl2303_class->bulkin_urb);
  314. }
  315. if (pl2303_class->bulkout) {
  316. usbh_kill_urb(&pl2303_class->bulkout_urb);
  317. }
  318. if (hport->config.intf[intf].devname[0] != '\0') {
  319. USB_LOG_INFO("Unregister PL2303 Class:%s\r\n", hport->config.intf[intf].devname);
  320. usbh_pl2303_stop(pl2303_class);
  321. }
  322. usbh_pl2303_class_free(pl2303_class);
  323. }
  324. return ret;
  325. }
  326. int usbh_pl2303_bulk_in_transfer(struct usbh_pl2303 *pl2303_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
  327. {
  328. int ret;
  329. struct usbh_urb *urb = &pl2303_class->bulkin_urb;
  330. usbh_bulk_urb_fill(urb, pl2303_class->hport, pl2303_class->bulkin, buffer, buflen, timeout, NULL, NULL);
  331. ret = usbh_submit_urb(urb);
  332. if (ret == 0) {
  333. ret = urb->actual_length;
  334. }
  335. return ret;
  336. }
  337. int usbh_pl2303_bulk_out_transfer(struct usbh_pl2303 *pl2303_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
  338. {
  339. int ret;
  340. struct usbh_urb *urb = &pl2303_class->bulkout_urb;
  341. usbh_bulk_urb_fill(urb, pl2303_class->hport, pl2303_class->bulkout, buffer, buflen, timeout, NULL, NULL);
  342. ret = usbh_submit_urb(urb);
  343. if (ret == 0) {
  344. ret = urb->actual_length;
  345. }
  346. return ret;
  347. }
  348. __WEAK void usbh_pl2303_run(struct usbh_pl2303 *pl2303_class)
  349. {
  350. (void)pl2303_class;
  351. }
  352. __WEAK void usbh_pl2303_stop(struct usbh_pl2303 *pl2303_class)
  353. {
  354. (void)pl2303_class;
  355. }
  356. static const uint16_t pl2303_id_table[][2] = {
  357. { 0x067B, 0x2303 }, // PL2303 Serial (ATEN/IOGEAR UC232A)
  358. { 0x067B, 0x23A3 }, // PL2303HXN Serial, type GC
  359. { 0x067B, 0x23B3 }, // PL2303HXN Serial, type GB
  360. { 0x067B, 0x23C3 }, // PL2303HXN Serial, type GT
  361. { 0x067B, 0x23D3 }, // PL2303HXN Serial, type GL
  362. { 0x067B, 0x23E3 }, // PL2303HXN Serial, type GE
  363. { 0x067B, 0x23F3 }, // PL2303HXN Serial, type GS
  364. { 0, 0 },
  365. };
  366. const struct usbh_class_driver pl2303_class_driver = {
  367. .driver_name = "pl2303",
  368. .connect = usbh_pl2303_connect,
  369. .disconnect = usbh_pl2303_disconnect
  370. };
  371. CLASS_INFO_DEFINE const struct usbh_class_info pl2303_class_info = {
  372. .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
  373. .bInterfaceClass = 0xff,
  374. .bInterfaceSubClass = 0x00,
  375. .bInterfaceProtocol = 0x00,
  376. .id_table = pl2303_id_table,
  377. .class_driver = &pl2303_class_driver
  378. };