usbh_pl2303.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * Copyright (c) 2024 ~ 2025, sakumisu
  3. * Copyright (c) 2024, Derek Konigsberg
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include "usbh_core.h"
  8. #include "usbh_serial.h"
  9. #include "usbh_pl2303.h"
  10. #undef USB_DBG_TAG
  11. #define USB_DBG_TAG "usbh_pl2303"
  12. #include "usb_log.h"
  13. #define UART_STATE_INDEX 8
  14. #define UART_STATE_MSR_MASK 0x8b
  15. #define UART_STATE_TRANSIENT_MASK 0x74
  16. #define UART_DCD 0x01
  17. #define UART_DSR 0x02
  18. #define UART_BREAK_ERROR 0x04
  19. #define UART_RING 0x08
  20. #define UART_FRAME_ERROR 0x10
  21. #define UART_PARITY_ERROR 0x20
  22. #define UART_OVERRUN_ERROR 0x40
  23. #define UART_CTS 0x80
  24. struct pl2303_type_data {
  25. const char *name;
  26. uint32_t max_baud_rate;
  27. unsigned long quirks;
  28. unsigned int no_autoxonxoff : 1;
  29. unsigned int no_divisors : 1;
  30. unsigned int alt_divisors : 1;
  31. };
  32. enum pl2303_type {
  33. TYPE_H,
  34. TYPE_HX,
  35. TYPE_TA,
  36. TYPE_TB,
  37. TYPE_HXD,
  38. TYPE_HXN,
  39. TYPE_COUNT
  40. };
  41. struct usbh_pl2303 {
  42. enum pl2303_type chip_type;
  43. uint32_t quirks;
  44. struct usb_endpoint_descriptor *intin;
  45. struct usbh_urb intin_urb;
  46. struct usb_osal_timer *modem_timer;
  47. uint16_t modem_status;
  48. };
  49. static const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = {
  50. [TYPE_H] = {
  51. .name = "PL2303H",
  52. .max_baud_rate = 1228800,
  53. .quirks = PL2303_QUIRK_LEGACY,
  54. .no_autoxonxoff = true,
  55. },
  56. [TYPE_HX] = {
  57. .name = "PL2303HX",
  58. .max_baud_rate = 6000000,
  59. },
  60. [TYPE_TA] = {
  61. .name = "PL2303TA",
  62. .max_baud_rate = 6000000,
  63. .alt_divisors = true,
  64. },
  65. [TYPE_TB] = {
  66. .name = "PL2303TB",
  67. .max_baud_rate = 12000000,
  68. .alt_divisors = true,
  69. },
  70. [TYPE_HXD] = {
  71. .name = "PL2303HXD",
  72. .max_baud_rate = 12000000,
  73. },
  74. [TYPE_HXN] = {
  75. .name = "PL2303G",
  76. .max_baud_rate = 12000000,
  77. .no_divisors = true,
  78. },
  79. };
  80. /*
  81. * Returns the nearest supported baud rate that can be set directly without
  82. * using divisors.
  83. */
  84. static uint32_t pl2303_get_supported_baud_rate(uint32_t baud)
  85. {
  86. static const uint32_t baud_sup[] = {
  87. 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600,
  88. 14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800,
  89. 614400, 921600, 1228800, 2457600, 3000000, 6000000
  90. };
  91. unsigned i;
  92. for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
  93. if (baud_sup[i] > baud)
  94. break;
  95. }
  96. if (i == ARRAY_SIZE(baud_sup))
  97. baud = baud_sup[i - 1];
  98. else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
  99. baud = baud_sup[i - 1];
  100. else
  101. baud = baud_sup[i];
  102. return baud;
  103. }
  104. /*
  105. * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
  106. * use 9600 baud.
  107. */
  108. static uint32_t pl2303_encode_baud_rate_direct(unsigned char buf[4],
  109. uint32_t baud)
  110. {
  111. memcpy(buf, &baud, 4);
  112. return baud;
  113. }
  114. static uint32_t pl2303_encode_baud_rate_divisor_alt(unsigned char buf[4],
  115. uint32_t baud)
  116. {
  117. unsigned int baseline, mantissa, exponent;
  118. /*
  119. * Apparently, for the TA version the formula is:
  120. * baudrate = 12M * 32 / (mantissa * 2^exponent)
  121. * where
  122. * mantissa = buf[10:0]
  123. * exponent = buf[15:13 16]
  124. */
  125. baseline = 12000000 * 32;
  126. mantissa = baseline / baud;
  127. if (mantissa == 0)
  128. mantissa = 1; /* Avoid dividing by zero if baud > 32*12M. */
  129. exponent = 0;
  130. while (mantissa >= 2048) {
  131. if (exponent < 15) {
  132. mantissa >>= 1; /* divide by 2 */
  133. exponent++;
  134. } else {
  135. /* Exponent is maxed. Trim mantissa and leave. */
  136. mantissa = 2047;
  137. break;
  138. }
  139. }
  140. buf[3] = 0x80;
  141. buf[2] = exponent & 0x01;
  142. buf[1] = (exponent & ~0x01) << 4 | mantissa >> 8;
  143. buf[0] = mantissa & 0xff;
  144. /* Calculate and return the exact baud rate. */
  145. baud = (baseline / mantissa) >> exponent;
  146. return baud;
  147. }
  148. static uint32_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
  149. uint32_t baud)
  150. {
  151. unsigned int baseline, mantissa, exponent;
  152. /*
  153. * Apparently the formula is:
  154. * baudrate = 12M * 32 / (mantissa * 4^exponent)
  155. * where
  156. * mantissa = buf[8:0]
  157. * exponent = buf[11:9]
  158. */
  159. baseline = 12000000 * 32;
  160. mantissa = baseline / baud;
  161. if (mantissa == 0)
  162. mantissa = 1; /* Avoid dividing by zero if baud > 32*12M. */
  163. exponent = 0;
  164. while (mantissa >= 512) {
  165. if (exponent < 7) {
  166. mantissa >>= 2; /* divide by 4 */
  167. exponent++;
  168. } else {
  169. /* Exponent is maxed. Trim mantissa and leave. */
  170. mantissa = 511;
  171. break;
  172. }
  173. }
  174. buf[3] = 0x80;
  175. buf[2] = 0;
  176. buf[1] = exponent << 1 | mantissa >> 8;
  177. buf[0] = mantissa & 0xff;
  178. /* Calculate and return the exact baud rate. */
  179. baud = (baseline / mantissa) >> (exponent << 1);
  180. return baud;
  181. }
  182. static int pl2303_vendor_write(struct usbh_serial *serial, uint16_t wValue, uint16_t wIndex)
  183. {
  184. struct usb_setup_packet *setup;
  185. struct usbh_pl2303 *pl2303_class;
  186. if (!serial || !serial->hport || !serial->priv) {
  187. return -USB_ERR_INVAL;
  188. }
  189. setup = serial->hport->setup;
  190. pl2303_class = (struct usbh_pl2303 *)serial->priv;
  191. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  192. setup->bRequest = pl2303_class->chip_type == TYPE_HXN ? PL2303_VENDOR_WRITE_NREQUEST : PL2303_VENDOR_WRITE_REQUEST;
  193. setup->wValue = wValue;
  194. setup->wIndex = wIndex;
  195. setup->wLength = 0;
  196. return usbh_control_transfer(serial->hport, setup, NULL);
  197. }
  198. static int pl2303_vendor_read(struct usbh_serial *serial, uint16_t wValue, uint8_t *data)
  199. {
  200. struct usb_setup_packet *setup;
  201. struct usbh_pl2303 *pl2303_class;
  202. int ret;
  203. if (!serial || !serial->hport || !serial->priv) {
  204. return -USB_ERR_INVAL;
  205. }
  206. setup = serial->hport->setup;
  207. pl2303_class = (struct usbh_pl2303 *)serial->priv;
  208. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  209. setup->bRequest = pl2303_class->chip_type == TYPE_HXN ? PL2303_VENDOR_READ_NREQUEST : PL2303_VENDOR_READ_REQUEST;
  210. setup->wValue = wValue;
  211. setup->wIndex = 0;
  212. setup->wLength = 1;
  213. ret = usbh_control_transfer(serial->hport, setup, serial->iobuffer);
  214. if (ret < 0) {
  215. return ret;
  216. }
  217. memcpy(data, serial->iobuffer, 1);
  218. return ret;
  219. }
  220. static bool pl2303_supports_hx_status(struct usbh_serial *serial)
  221. {
  222. int ret;
  223. uint8_t buf;
  224. ret = pl2303_vendor_read(serial, PL2303_READ_TYPE_HX_STATUS, &buf);
  225. if (ret < 0) {
  226. return false;
  227. }
  228. return true;
  229. }
  230. static bool pl2303_is_hxd_clone(struct usbh_serial *serial)
  231. {
  232. struct usb_setup_packet *setup;
  233. int ret;
  234. if (!serial || !serial->hport) {
  235. return -USB_ERR_INVAL;
  236. }
  237. setup = serial->hport->setup;
  238. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
  239. setup->bRequest = CDC_REQUEST_GET_LINE_CODING;
  240. setup->wValue = 0;
  241. setup->wIndex = 0;
  242. setup->wLength = 7;
  243. ret = usbh_control_transfer(serial->hport, setup, serial->iobuffer);
  244. if (ret < 0) {
  245. return false;
  246. }
  247. return true;
  248. }
  249. static int pl2303_update_reg(struct usbh_serial *serial, uint8_t reg, uint8_t mask, uint8_t val)
  250. {
  251. int ret;
  252. uint8_t buf[1];
  253. struct usbh_pl2303 *pl2303_class;
  254. if (!serial || !serial->hport || !serial->priv) {
  255. return -USB_ERR_INVAL;
  256. }
  257. pl2303_class = (struct usbh_pl2303 *)serial->priv;
  258. if (pl2303_class->chip_type == TYPE_HXN)
  259. ret = pl2303_vendor_read(serial, reg, buf);
  260. else
  261. ret = pl2303_vendor_read(serial, reg | 0x80, buf);
  262. if (ret < 0) {
  263. return ret;
  264. }
  265. *buf &= ~mask;
  266. *buf |= val & mask;
  267. return pl2303_vendor_write(serial, reg, *buf);
  268. }
  269. static int usbh_pl2303_get_chiptype(struct usbh_serial *serial)
  270. {
  271. if (serial->hport->device_desc.bDeviceClass == 0x02) {
  272. return TYPE_H; /* variant 0 */
  273. }
  274. if (serial->hport->device_desc.bMaxPacketSize0 != 0x40) {
  275. if (serial->hport->device_desc.bDeviceClass == 0x00 || serial->hport->device_desc.bDeviceClass == 0xff)
  276. return TYPE_H; /* variant 1 */
  277. return TYPE_H; /* variant 0 */
  278. }
  279. switch (serial->hport->device_desc.bcdUSB) {
  280. case 0x101:
  281. /* USB 1.0.1? Let's assume they meant 1.1... */
  282. case 0x110:
  283. switch (serial->hport->device_desc.bcdDevice) {
  284. case 0x300:
  285. return TYPE_HX;
  286. case 0x400:
  287. return TYPE_HXD;
  288. default:
  289. return TYPE_HX;
  290. }
  291. break;
  292. case 0x200:
  293. switch (serial->hport->device_desc.bcdDevice) {
  294. case 0x100: /* GC */
  295. case 0x105:
  296. return TYPE_HXN;
  297. case 0x300: /* GT / TA */
  298. if (pl2303_supports_hx_status(serial))
  299. return TYPE_TA;
  300. __attribute__((fallthrough));
  301. case 0x305:
  302. case 0x400: /* GL */
  303. case 0x405:
  304. return TYPE_HXN;
  305. case 0x500: /* GE / TB */
  306. if (pl2303_supports_hx_status(serial))
  307. return TYPE_TB;
  308. __attribute__((fallthrough));
  309. case 0x505:
  310. case 0x600: /* GS */
  311. case 0x605:
  312. case 0x700: /* GR */
  313. case 0x705:
  314. case 0x905: /* GT-2AB */
  315. case 0x1005: /* GC-Q20 */
  316. return TYPE_HXN;
  317. }
  318. break;
  319. }
  320. USB_LOG_ERR("Unsupported PL2303 Device\r\n");
  321. return -USB_ERR_NOTSUPP;
  322. }
  323. static int usbh_pl2303_attach(struct usbh_serial *serial)
  324. {
  325. struct usbh_pl2303 *pl2303_class;
  326. struct usb_endpoint_descriptor *ep_desc;
  327. uint8_t type;
  328. int ret;
  329. ret = usbh_pl2303_get_chiptype(serial);
  330. if (ret < 0) {
  331. return ret;
  332. }
  333. pl2303_class = usb_osal_malloc(sizeof(struct usbh_pl2303));
  334. if (pl2303_class == NULL) {
  335. USB_LOG_ERR("Fail to alloc pl2303_class\r\n");
  336. return -USB_ERR_NOMEM;
  337. }
  338. memset(pl2303_class, 0, sizeof(struct usbh_pl2303));
  339. serial->priv = pl2303_class;
  340. for (uint8_t i = 0; i < serial->hport->config.intf[serial->intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  341. ep_desc = &serial->hport->config.intf[serial->intf].altsetting[0].ep[i].ep_desc;
  342. if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) {
  343. if (ep_desc->bEndpointAddress & 0x80) {
  344. USBH_EP_INIT(pl2303_class->intin, ep_desc);
  345. break;
  346. } else {
  347. }
  348. }
  349. }
  350. if (!pl2303_class->intin) {
  351. USB_LOG_ERR("Failed to find interrupt endpoint\r\n");
  352. ret = -USB_ERR_NODEV;
  353. goto errout;
  354. }
  355. type = (uint8_t)ret;
  356. pl2303_class->chip_type = type;
  357. pl2303_class->quirks = pl2303_type_data[pl2303_class->chip_type].quirks;
  358. USB_LOG_INFO("chip type: %s\r\n", pl2303_type_data[pl2303_class->chip_type].name);
  359. if (type == TYPE_HXD && pl2303_is_hxd_clone(serial)) {
  360. pl2303_class->quirks |= PL2303_QUIRK_NO_BREAK_GETLINE;
  361. }
  362. if (type != TYPE_HXN) {
  363. uint8_t buf[1];
  364. ret = pl2303_vendor_read(serial, 0x8484, buf);
  365. ret |= pl2303_vendor_write(serial, 0x0404, 0);
  366. ret |= pl2303_vendor_read(serial, 0x8484, buf);
  367. ret |= pl2303_vendor_read(serial, 0x8383, buf);
  368. ret |= pl2303_vendor_read(serial, 0x8484, buf);
  369. ret |= pl2303_vendor_write(serial, 0x0404, 1);
  370. ret |= pl2303_vendor_read(serial, 0x8484, buf);
  371. ret |= pl2303_vendor_read(serial, 0x8383, buf);
  372. ret |= pl2303_vendor_write(serial, 0, 1);
  373. ret |= pl2303_vendor_write(serial, 1, 0);
  374. if (pl2303_class->quirks & PL2303_QUIRK_LEGACY)
  375. ret |= pl2303_vendor_write(serial, 2, 0x24);
  376. else
  377. ret |= pl2303_vendor_write(serial, 2, 0x44);
  378. } else {
  379. ret = 0;
  380. }
  381. if (ret < 0) {
  382. USB_LOG_ERR("pl2303 init failed\r\n");
  383. goto errout;
  384. }
  385. return 0;
  386. errout:
  387. serial->priv = NULL;
  388. usb_osal_free(pl2303_class);
  389. return ret;
  390. }
  391. static void usbh_pl2303_detach(struct usbh_serial *serial)
  392. {
  393. struct usbh_pl2303 *pl2303_class;
  394. if (!serial || !serial->priv) {
  395. return;
  396. }
  397. pl2303_class = (struct usbh_pl2303 *)serial->priv;
  398. if (pl2303_class->intin) {
  399. usbh_kill_urb(&pl2303_class->intin_urb);
  400. }
  401. serial->priv = NULL;
  402. usb_osal_free(pl2303_class);
  403. }
  404. static int usbh_pl2303_set_flow_ctrl(struct usbh_serial *serial, bool hardctrl)
  405. {
  406. struct usbh_pl2303 *pl2303_class;
  407. if (!serial || !serial->hport || !serial->priv) {
  408. return -USB_ERR_INVAL;
  409. }
  410. pl2303_class = (struct usbh_pl2303 *)serial->priv;
  411. if (hardctrl) {
  412. if (pl2303_class->quirks & PL2303_QUIRK_LEGACY) {
  413. return pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x40);
  414. } else if (pl2303_class->chip_type == TYPE_HXN) {
  415. return pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
  416. PL2303_HXN_FLOWCTRL_MASK,
  417. PL2303_HXN_FLOWCTRL_RTS_CTS);
  418. } else {
  419. return pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x60);
  420. }
  421. } else {
  422. if (pl2303_class->chip_type == TYPE_HXN) {
  423. return pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
  424. PL2303_HXN_FLOWCTRL_MASK,
  425. PL2303_HXN_FLOWCTRL_NONE);
  426. } else {
  427. return pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0);
  428. }
  429. }
  430. }
  431. static int usbh_pl2303_set_line_coding(struct usbh_serial *serial, struct cdc_line_coding *line_coding)
  432. {
  433. struct usb_setup_packet *setup;
  434. struct usbh_pl2303 *pl2303_class;
  435. uint32_t baud;
  436. uint32_t baud_sup;
  437. uint8_t buf[7];
  438. if (!serial || !serial->hport || !serial->priv) {
  439. return -USB_ERR_INVAL;
  440. }
  441. setup = serial->hport->setup;
  442. pl2303_class = (struct usbh_pl2303 *)serial->priv;
  443. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  444. setup->bRequest = CDC_REQUEST_SET_LINE_CODING;
  445. setup->wValue = 0;
  446. setup->wIndex = serial->intf;
  447. setup->wLength = 7;
  448. baud = line_coding->dwDTERate;
  449. if (pl2303_type_data[pl2303_class->chip_type].max_baud_rate) {
  450. baud = MIN(baud, pl2303_type_data[pl2303_class->chip_type].max_baud_rate);
  451. }
  452. /*
  453. * Use direct method for supported baud rates, otherwise use divisors.
  454. * Newer chip types do not support divisor encoding.
  455. */
  456. if (pl2303_type_data[pl2303_class->chip_type].no_divisors)
  457. baud_sup = baud;
  458. else
  459. baud_sup = pl2303_get_supported_baud_rate(baud);
  460. if (baud == baud_sup)
  461. baud = pl2303_encode_baud_rate_direct(buf, baud);
  462. else if (pl2303_type_data[pl2303_class->chip_type].alt_divisors)
  463. baud = pl2303_encode_baud_rate_divisor_alt(buf, baud);
  464. else
  465. baud = pl2303_encode_baud_rate_divisor(buf, baud);
  466. buf[4] = line_coding->bCharFormat;
  467. buf[5] = line_coding->bParityType;
  468. buf[6] = line_coding->bDataBits;
  469. memcpy(serial->iobuffer, buf, sizeof(struct cdc_line_coding));
  470. return usbh_control_transfer(serial->hport, setup, serial->iobuffer);
  471. }
  472. static int usbh_pl2303_get_line_coding(struct usbh_serial *serial, struct cdc_line_coding *line_coding)
  473. {
  474. struct usb_setup_packet *setup;
  475. int ret;
  476. if (!serial || !serial->hport) {
  477. return -USB_ERR_INVAL;
  478. }
  479. setup = serial->hport->setup;
  480. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  481. setup->bRequest = CDC_REQUEST_GET_LINE_CODING;
  482. setup->wValue = 0;
  483. setup->wIndex = serial->intf;
  484. setup->wLength = 7;
  485. ret = usbh_control_transfer(serial->hport, setup, serial->iobuffer);
  486. if (ret < 0) {
  487. return ret;
  488. }
  489. memcpy(line_coding, serial->iobuffer, sizeof(struct cdc_line_coding));
  490. return ret;
  491. }
  492. static int usbh_pl2303_set_line_state(struct usbh_serial *serial, bool dtr, bool rts)
  493. {
  494. struct usb_setup_packet *setup;
  495. if (!serial || !serial->hport) {
  496. return -USB_ERR_INVAL;
  497. }
  498. setup = serial->hport->setup;
  499. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  500. setup->bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE;
  501. setup->wValue = (dtr << 0) | (rts << 1);
  502. setup->wIndex = serial->intf;
  503. setup->wLength = 0;
  504. return usbh_control_transfer(serial->hport, setup, NULL);
  505. }
  506. static int usbh_pl2303_get_modem_status(struct usbh_serial *serial)
  507. {
  508. struct usbh_pl2303 *pl2303_class;
  509. uintptr_t flags;
  510. uint16_t status;
  511. if (!serial || !serial->hport || !serial->priv) {
  512. return -USB_ERR_INVAL;
  513. }
  514. flags = usb_osal_enter_critical_section();
  515. pl2303_class = (struct usbh_pl2303 *)serial->priv;
  516. status = (pl2303_class->modem_status & UART_DSR ? USBH_SERIAL_TIOCM_DSR : 0) |
  517. (pl2303_class->modem_status & UART_CTS ? USBH_SERIAL_TIOCM_CTS : 0) |
  518. (pl2303_class->modem_status & UART_RING ? USBH_SERIAL_TIOCM_RI : 0) |
  519. (pl2303_class->modem_status & UART_DCD ? USBH_SERIAL_TIOCM_CD : 0) |
  520. (serial->line_state & USBH_SERIAL_TIOCM_DTR ? USBH_SERIAL_TIOCM_DTR : 0) |
  521. (serial->line_state & USBH_SERIAL_TIOCM_RTS ? USBH_SERIAL_TIOCM_RTS : 0);
  522. usb_osal_leave_critical_section(flags);
  523. return status;
  524. }
  525. #ifdef CONFIG_USBH_SERIAL_GET_MODEM_STATUS
  526. static int __usbh_pl2303_get_modem_status(struct usbh_serial *serial)
  527. {
  528. struct usbh_pl2303 *pl2303_class;
  529. uint8_t status = 0;
  530. uint16_t difference;
  531. uintptr_t flags;
  532. int ret;
  533. if (!serial || !serial->hport || !serial->priv) {
  534. return -USB_ERR_INVAL;
  535. }
  536. pl2303_class = (struct usbh_pl2303 *)serial->priv;
  537. usbh_int_urb_fill(&pl2303_class->intin_urb, serial->hport, pl2303_class->intin, &serial->iobuffer[USBH_SERIAL_INT_NOCACHE_OFFSET], pl2303_class->intin->wMaxPacketSize, 0xffffffff, NULL, NULL);
  538. ret = usbh_submit_urb(&pl2303_class->intin_urb);
  539. if (ret < 0) {
  540. return ret;
  541. }
  542. if (ret < 1) {
  543. return -USB_ERR_INVAL;
  544. }
  545. flags = usb_osal_enter_critical_section();
  546. status = serial->iobuffer[USBH_SERIAL_INT_NOCACHE_OFFSET];
  547. difference = pl2303_class->modem_status ^ status;
  548. pl2303_class->modem_status = status;
  549. if (status & UART_BREAK_ERROR)
  550. serial->iocount.brk++;
  551. if (difference & UART_STATE_MSR_MASK) {
  552. if (difference & UART_CTS)
  553. serial->iocount.cts++;
  554. if (difference & UART_DSR)
  555. serial->iocount.dsr++;
  556. if (difference & UART_RING)
  557. serial->iocount.rng++;
  558. if (difference & UART_DCD) {
  559. serial->iocount.dcd++;
  560. }
  561. }
  562. usb_osal_leave_critical_section(flags);
  563. return ret;
  564. }
  565. #endif
  566. static const struct usbh_serial_driver pl2303_driver = {
  567. .driver_name = "pl2303",
  568. .ignore_rx_header = 0,
  569. .ignore_tx_header = 0,
  570. .attach = usbh_pl2303_attach,
  571. .detach = usbh_pl2303_detach,
  572. .set_flow_control = usbh_pl2303_set_flow_ctrl,
  573. .set_line_coding = usbh_pl2303_set_line_coding,
  574. .get_line_coding = usbh_pl2303_get_line_coding,
  575. .set_line_state = usbh_pl2303_set_line_state,
  576. .get_modem_status = usbh_pl2303_get_modem_status,
  577. };
  578. static int usbh_pl2303_connect(struct usbh_hubport *hport, uint8_t intf)
  579. {
  580. return usbh_serial_probe(hport, intf, &pl2303_driver) ? 0 : -USB_ERR_NOMEM;
  581. }
  582. static int usbh_pl2303_disconnect(struct usbh_hubport *hport, uint8_t intf)
  583. {
  584. struct usbh_serial *serial = (struct usbh_serial *)hport->config.intf[intf].priv;
  585. if (serial) {
  586. usbh_serial_remove(serial);
  587. }
  588. return 0;
  589. }
  590. static const uint16_t pl2303_id_table[][2] = {
  591. { 0x067B, 0x2303 }, // PL2303 Serial (ATEN/IOGEAR UC232A)
  592. { 0x067B, 0x2304 }, // PL2303HXN Serial, type TB
  593. { 0x067B, 0x23A3 }, // PL2303HXN Serial, type GC
  594. { 0x067B, 0x23B3 }, // PL2303HXN Serial, type GB
  595. { 0x067B, 0x23C3 }, // PL2303HXN Serial, type GT
  596. { 0x067B, 0x23D3 }, // PL2303HXN Serial, type GL
  597. { 0x067B, 0x23E3 }, // PL2303HXN Serial, type GE
  598. { 0x067B, 0x23F3 }, // PL2303HXN Serial, type GS
  599. { 0, 0 },
  600. };
  601. const struct usbh_class_driver pl2303_class_driver = {
  602. .driver_name = "pl2303",
  603. .connect = usbh_pl2303_connect,
  604. .disconnect = usbh_pl2303_disconnect
  605. };
  606. CLASS_INFO_DEFINE const struct usbh_class_info pl2303_class_info = {
  607. .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
  608. .bInterfaceClass = 0xff,
  609. .bInterfaceSubClass = 0x00,
  610. .bInterfaceProtocol = 0x00,
  611. .id_table = pl2303_id_table,
  612. .class_driver = &pl2303_class_driver
  613. };