ch341.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk>
  3. * Copyright 2007, Werner Cornelius <werner@cornelius-consult.de>
  4. * Copyright 2009, Boris Hajduk <boris@hajduk.org>
  5. *
  6. * ch341.c implements a serial port driver for the Winchiphead CH341.
  7. *
  8. * The CH341 device can be used to implement an RS232 asynchronous
  9. * serial port, an IEEE-1284 parallel printer port or a memory-like
  10. * interface. In all cases the CH341 supports an I2C interface as well.
  11. * This driver only supports the asynchronous serial interface.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License version
  15. * 2 as published by the Free Software Foundation.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/tty.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/serial.h>
  23. #include <linux/serial.h>
  24. #include <asm/unaligned.h>
  25. #define DEFAULT_BAUD_RATE 9600
  26. #define DEFAULT_TIMEOUT 1000
  27. /* flags for IO-Bits */
  28. #define CH341_BIT_RTS (1 << 6)
  29. #define CH341_BIT_DTR (1 << 5)
  30. /******************************/
  31. /* interrupt pipe definitions */
  32. /******************************/
  33. /* always 4 interrupt bytes */
  34. /* first irq byte normally 0x08 */
  35. /* second irq byte base 0x7d + below */
  36. /* third irq byte base 0x94 + below */
  37. /* fourth irq byte normally 0xee */
  38. /* second interrupt byte */
  39. #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
  40. /* status returned in third interrupt answer byte, inverted in data
  41. from irq */
  42. #define CH341_BIT_CTS 0x01
  43. #define CH341_BIT_DSR 0x02
  44. #define CH341_BIT_RI 0x04
  45. #define CH341_BIT_DCD 0x08
  46. #define CH341_BITS_MODEM_STAT 0x0f /* all bits */
  47. /*******************************/
  48. /* baudrate calculation factor */
  49. /*******************************/
  50. #define CH341_BAUDBASE_FACTOR 1532620800
  51. #define CH341_BAUDBASE_DIVMAX 3
  52. /* Break support - the information used to implement this was gleaned from
  53. * the Net/FreeBSD uchcom.c driver by Takanori Watanabe. Domo arigato.
  54. */
  55. #define CH341_REQ_WRITE_REG 0x9A
  56. #define CH341_REQ_READ_REG 0x95
  57. #define CH341_REG_BREAK1 0x05
  58. #define CH341_REG_BREAK2 0x18
  59. #define CH341_NBREAK_BITS_REG1 0x01
  60. #define CH341_NBREAK_BITS_REG2 0x40
  61. static const struct usb_device_id id_table[] = {
  62. { USB_DEVICE(0x4348, 0x5523) },
  63. { USB_DEVICE(0x1a86, 0x7523) },
  64. { USB_DEVICE(0x1a86, 0x5523) },
  65. { },
  66. };
  67. MODULE_DEVICE_TABLE(usb, id_table);
  68. struct ch341_private {
  69. spinlock_t lock; /* access lock */
  70. unsigned baud_rate; /* set baud rate */
  71. u8 line_control; /* set line control value RTS/DTR */
  72. u8 line_status; /* active status of modem control inputs */
  73. };
  74. static void ch341_set_termios(struct tty_struct *tty,
  75. struct usb_serial_port *port,
  76. struct ktermios *old_termios);
  77. static int ch341_control_out(struct usb_device *dev, u8 request,
  78. u16 value, u16 index)
  79. {
  80. int r;
  81. dev_dbg(&dev->dev, "ch341_control_out(%02x,%02x,%04x,%04x)\n",
  82. USB_DIR_OUT|0x40, (int)request, (int)value, (int)index);
  83. r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
  84. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  85. value, index, NULL, 0, DEFAULT_TIMEOUT);
  86. if (r < 0)
  87. dev_err(&dev->dev, "failed to send control message: %d\n", r);
  88. return r;
  89. }
  90. static int ch341_control_in(struct usb_device *dev,
  91. u8 request, u16 value, u16 index,
  92. char *buf, unsigned bufsize)
  93. {
  94. int r;
  95. dev_dbg(&dev->dev, "ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)\n",
  96. USB_DIR_IN|0x40, (int)request, (int)value, (int)index, buf,
  97. (int)bufsize);
  98. r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
  99. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  100. value, index, buf, bufsize, DEFAULT_TIMEOUT);
  101. if (r < (int)bufsize) {
  102. if (r >= 0) {
  103. dev_err(&dev->dev,
  104. "short control message received (%d < %u)\n",
  105. r, bufsize);
  106. r = -EIO;
  107. }
  108. dev_err(&dev->dev, "failed to receive control message: %d\n",
  109. r);
  110. return r;
  111. }
  112. return 0;
  113. }
  114. static int ch341_set_baudrate(struct usb_device *dev,
  115. struct ch341_private *priv)
  116. {
  117. short a, b;
  118. int r;
  119. unsigned long factor;
  120. short divisor;
  121. if (!priv->baud_rate)
  122. return -EINVAL;
  123. factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
  124. divisor = CH341_BAUDBASE_DIVMAX;
  125. while ((factor > 0xfff0) && divisor) {
  126. factor >>= 3;
  127. divisor--;
  128. }
  129. if (factor > 0xfff0)
  130. return -EINVAL;
  131. factor = 0x10000 - factor;
  132. a = (factor & 0xff00) | divisor;
  133. b = factor & 0xff;
  134. r = ch341_control_out(dev, 0x9a, 0x1312, a);
  135. if (!r)
  136. r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
  137. return r;
  138. }
  139. static int ch341_set_handshake(struct usb_device *dev, u8 control)
  140. {
  141. return ch341_control_out(dev, 0xa4, ~control, 0);
  142. }
  143. static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
  144. {
  145. const unsigned int size = 2;
  146. char *buffer;
  147. int r;
  148. unsigned long flags;
  149. buffer = kmalloc(size, GFP_KERNEL);
  150. if (!buffer)
  151. return -ENOMEM;
  152. r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
  153. if (r < 0)
  154. goto out;
  155. spin_lock_irqsave(&priv->lock, flags);
  156. priv->line_status = (~(*buffer)) & CH341_BITS_MODEM_STAT;
  157. spin_unlock_irqrestore(&priv->lock, flags);
  158. out: kfree(buffer);
  159. return r;
  160. }
  161. /* -------------------------------------------------------------------------- */
  162. static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
  163. {
  164. const unsigned int size = 2;
  165. char *buffer;
  166. int r;
  167. buffer = kmalloc(size, GFP_KERNEL);
  168. if (!buffer)
  169. return -ENOMEM;
  170. /* expect two bytes 0x27 0x00 */
  171. r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
  172. if (r < 0)
  173. goto out;
  174. r = ch341_control_out(dev, 0xa1, 0, 0);
  175. if (r < 0)
  176. goto out;
  177. r = ch341_set_baudrate(dev, priv);
  178. if (r < 0)
  179. goto out;
  180. /* expect two bytes 0x56 0x00 */
  181. r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
  182. if (r < 0)
  183. goto out;
  184. r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
  185. if (r < 0)
  186. goto out;
  187. /* expect 0xff 0xee */
  188. r = ch341_get_status(dev, priv);
  189. if (r < 0)
  190. goto out;
  191. r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
  192. if (r < 0)
  193. goto out;
  194. r = ch341_set_baudrate(dev, priv);
  195. if (r < 0)
  196. goto out;
  197. r = ch341_set_handshake(dev, priv->line_control);
  198. if (r < 0)
  199. goto out;
  200. /* expect 0x9f 0xee */
  201. r = ch341_get_status(dev, priv);
  202. out: kfree(buffer);
  203. return r;
  204. }
  205. static int ch341_port_probe(struct usb_serial_port *port)
  206. {
  207. struct ch341_private *priv;
  208. int r;
  209. priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
  210. if (!priv)
  211. return -ENOMEM;
  212. spin_lock_init(&priv->lock);
  213. priv->baud_rate = DEFAULT_BAUD_RATE;
  214. r = ch341_configure(port->serial->dev, priv);
  215. if (r < 0)
  216. goto error;
  217. usb_set_serial_port_data(port, priv);
  218. return 0;
  219. error: kfree(priv);
  220. return r;
  221. }
  222. static int ch341_port_remove(struct usb_serial_port *port)
  223. {
  224. struct ch341_private *priv;
  225. priv = usb_get_serial_port_data(port);
  226. kfree(priv);
  227. return 0;
  228. }
  229. static int ch341_carrier_raised(struct usb_serial_port *port)
  230. {
  231. struct ch341_private *priv = usb_get_serial_port_data(port);
  232. if (priv->line_status & CH341_BIT_DCD)
  233. return 1;
  234. return 0;
  235. }
  236. static void ch341_dtr_rts(struct usb_serial_port *port, int on)
  237. {
  238. struct ch341_private *priv = usb_get_serial_port_data(port);
  239. unsigned long flags;
  240. /* drop DTR and RTS */
  241. spin_lock_irqsave(&priv->lock, flags);
  242. if (on)
  243. priv->line_control |= CH341_BIT_RTS | CH341_BIT_DTR;
  244. else
  245. priv->line_control &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
  246. spin_unlock_irqrestore(&priv->lock, flags);
  247. ch341_set_handshake(port->serial->dev, priv->line_control);
  248. }
  249. static void ch341_close(struct usb_serial_port *port)
  250. {
  251. usb_serial_generic_close(port);
  252. usb_kill_urb(port->interrupt_in_urb);
  253. }
  254. /* open this device, set default parameters */
  255. static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
  256. {
  257. struct usb_serial *serial = port->serial;
  258. struct ch341_private *priv = usb_get_serial_port_data(port);
  259. int r;
  260. r = ch341_configure(serial->dev, priv);
  261. if (r)
  262. return r;
  263. if (tty)
  264. ch341_set_termios(tty, port, NULL);
  265. dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__);
  266. r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  267. if (r) {
  268. dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n",
  269. __func__, r);
  270. return r;
  271. }
  272. r = usb_serial_generic_open(tty, port);
  273. if (r)
  274. goto err_kill_interrupt_urb;
  275. return 0;
  276. err_kill_interrupt_urb:
  277. usb_kill_urb(port->interrupt_in_urb);
  278. return r;
  279. }
  280. /* Old_termios contains the original termios settings and
  281. * tty->termios contains the new setting to be used.
  282. */
  283. static void ch341_set_termios(struct tty_struct *tty,
  284. struct usb_serial_port *port, struct ktermios *old_termios)
  285. {
  286. struct ch341_private *priv = usb_get_serial_port_data(port);
  287. unsigned baud_rate;
  288. unsigned long flags;
  289. baud_rate = tty_get_baud_rate(tty);
  290. if (baud_rate) {
  291. priv->baud_rate = baud_rate;
  292. ch341_set_baudrate(port->serial->dev, priv);
  293. }
  294. /* Unimplemented:
  295. * (cflag & CSIZE) : data bits [5, 8]
  296. * (cflag & PARENB) : parity {NONE, EVEN, ODD}
  297. * (cflag & CSTOPB) : stop bits [1, 2]
  298. */
  299. spin_lock_irqsave(&priv->lock, flags);
  300. if (C_BAUD(tty) == B0)
  301. priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
  302. else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
  303. priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
  304. spin_unlock_irqrestore(&priv->lock, flags);
  305. ch341_set_handshake(port->serial->dev, priv->line_control);
  306. }
  307. static void ch341_break_ctl(struct tty_struct *tty, int break_state)
  308. {
  309. const uint16_t ch341_break_reg =
  310. CH341_REG_BREAK1 | ((uint16_t) CH341_REG_BREAK2 << 8);
  311. struct usb_serial_port *port = tty->driver_data;
  312. int r;
  313. uint16_t reg_contents;
  314. uint8_t *break_reg;
  315. break_reg = kmalloc(2, GFP_KERNEL);
  316. if (!break_reg)
  317. return;
  318. r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
  319. ch341_break_reg, 0, break_reg, 2);
  320. if (r < 0) {
  321. dev_err(&port->dev, "%s - USB control read error (%d)\n",
  322. __func__, r);
  323. goto out;
  324. }
  325. dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
  326. __func__, break_reg[0], break_reg[1]);
  327. if (break_state != 0) {
  328. dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
  329. break_reg[0] &= ~CH341_NBREAK_BITS_REG1;
  330. break_reg[1] &= ~CH341_NBREAK_BITS_REG2;
  331. } else {
  332. dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
  333. break_reg[0] |= CH341_NBREAK_BITS_REG1;
  334. break_reg[1] |= CH341_NBREAK_BITS_REG2;
  335. }
  336. dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
  337. __func__, break_reg[0], break_reg[1]);
  338. reg_contents = get_unaligned_le16(break_reg);
  339. r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
  340. ch341_break_reg, reg_contents);
  341. if (r < 0)
  342. dev_err(&port->dev, "%s - USB control write error (%d)\n",
  343. __func__, r);
  344. out:
  345. kfree(break_reg);
  346. }
  347. static int ch341_tiocmset(struct tty_struct *tty,
  348. unsigned int set, unsigned int clear)
  349. {
  350. struct usb_serial_port *port = tty->driver_data;
  351. struct ch341_private *priv = usb_get_serial_port_data(port);
  352. unsigned long flags;
  353. u8 control;
  354. spin_lock_irqsave(&priv->lock, flags);
  355. if (set & TIOCM_RTS)
  356. priv->line_control |= CH341_BIT_RTS;
  357. if (set & TIOCM_DTR)
  358. priv->line_control |= CH341_BIT_DTR;
  359. if (clear & TIOCM_RTS)
  360. priv->line_control &= ~CH341_BIT_RTS;
  361. if (clear & TIOCM_DTR)
  362. priv->line_control &= ~CH341_BIT_DTR;
  363. control = priv->line_control;
  364. spin_unlock_irqrestore(&priv->lock, flags);
  365. return ch341_set_handshake(port->serial->dev, control);
  366. }
  367. static void ch341_update_line_status(struct usb_serial_port *port,
  368. unsigned char *data, size_t len)
  369. {
  370. struct ch341_private *priv = usb_get_serial_port_data(port);
  371. struct tty_struct *tty;
  372. unsigned long flags;
  373. u8 status;
  374. u8 delta;
  375. if (len < 4)
  376. return;
  377. status = ~data[2] & CH341_BITS_MODEM_STAT;
  378. spin_lock_irqsave(&priv->lock, flags);
  379. delta = status ^ priv->line_status;
  380. priv->line_status = status;
  381. spin_unlock_irqrestore(&priv->lock, flags);
  382. if (data[1] & CH341_MULT_STAT)
  383. dev_dbg(&port->dev, "%s - multiple status change\n", __func__);
  384. if (!delta)
  385. return;
  386. if (delta & CH341_BIT_CTS)
  387. port->icount.cts++;
  388. if (delta & CH341_BIT_DSR)
  389. port->icount.dsr++;
  390. if (delta & CH341_BIT_RI)
  391. port->icount.rng++;
  392. if (delta & CH341_BIT_DCD) {
  393. port->icount.dcd++;
  394. tty = tty_port_tty_get(&port->port);
  395. if (tty) {
  396. usb_serial_handle_dcd_change(port, tty,
  397. status & CH341_BIT_DCD);
  398. tty_kref_put(tty);
  399. }
  400. }
  401. wake_up_interruptible(&port->port.delta_msr_wait);
  402. }
  403. static void ch341_read_int_callback(struct urb *urb)
  404. {
  405. struct usb_serial_port *port = urb->context;
  406. unsigned char *data = urb->transfer_buffer;
  407. unsigned int len = urb->actual_length;
  408. int status;
  409. switch (urb->status) {
  410. case 0:
  411. /* success */
  412. break;
  413. case -ECONNRESET:
  414. case -ENOENT:
  415. case -ESHUTDOWN:
  416. /* this urb is terminated, clean up */
  417. dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
  418. __func__, urb->status);
  419. return;
  420. default:
  421. dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
  422. __func__, urb->status);
  423. goto exit;
  424. }
  425. usb_serial_debug_data(&port->dev, __func__, len, data);
  426. ch341_update_line_status(port, data, len);
  427. exit:
  428. status = usb_submit_urb(urb, GFP_ATOMIC);
  429. if (status) {
  430. dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n",
  431. __func__, status);
  432. }
  433. }
  434. static int ch341_tiocmget(struct tty_struct *tty)
  435. {
  436. struct usb_serial_port *port = tty->driver_data;
  437. struct ch341_private *priv = usb_get_serial_port_data(port);
  438. unsigned long flags;
  439. u8 mcr;
  440. u8 status;
  441. unsigned int result;
  442. spin_lock_irqsave(&priv->lock, flags);
  443. mcr = priv->line_control;
  444. status = priv->line_status;
  445. spin_unlock_irqrestore(&priv->lock, flags);
  446. result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
  447. | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
  448. | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
  449. | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
  450. | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
  451. | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
  452. dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
  453. return result;
  454. }
  455. static int ch341_reset_resume(struct usb_serial *serial)
  456. {
  457. struct usb_serial_port *port = serial->port[0];
  458. struct ch341_private *priv = usb_get_serial_port_data(port);
  459. int ret;
  460. /* reconfigure ch341 serial port after bus-reset */
  461. ch341_configure(serial->dev, priv);
  462. if (test_bit(ASYNCB_INITIALIZED, &port->port.flags)) {
  463. ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
  464. if (ret) {
  465. dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
  466. ret);
  467. return ret;
  468. }
  469. }
  470. return usb_serial_generic_resume(serial);
  471. }
  472. static struct usb_serial_driver ch341_device = {
  473. .driver = {
  474. .owner = THIS_MODULE,
  475. .name = "ch341-uart",
  476. },
  477. .id_table = id_table,
  478. .num_ports = 1,
  479. .open = ch341_open,
  480. .dtr_rts = ch341_dtr_rts,
  481. .carrier_raised = ch341_carrier_raised,
  482. .close = ch341_close,
  483. .set_termios = ch341_set_termios,
  484. .break_ctl = ch341_break_ctl,
  485. .tiocmget = ch341_tiocmget,
  486. .tiocmset = ch341_tiocmset,
  487. .tiocmiwait = usb_serial_generic_tiocmiwait,
  488. .read_int_callback = ch341_read_int_callback,
  489. .port_probe = ch341_port_probe,
  490. .port_remove = ch341_port_remove,
  491. .reset_resume = ch341_reset_resume,
  492. };
  493. static struct usb_serial_driver * const serial_drivers[] = {
  494. &ch341_device, NULL
  495. };
  496. module_usb_serial_driver(serial_drivers, id_table);
  497. MODULE_LICENSE("GPL");