ch341.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. return r;
  87. }
  88. static int ch341_control_in(struct usb_device *dev,
  89. u8 request, u16 value, u16 index,
  90. char *buf, unsigned bufsize)
  91. {
  92. int r;
  93. dev_dbg(&dev->dev, "ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)\n",
  94. USB_DIR_IN|0x40, (int)request, (int)value, (int)index, buf,
  95. (int)bufsize);
  96. r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
  97. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  98. value, index, buf, bufsize, DEFAULT_TIMEOUT);
  99. return r;
  100. }
  101. static int ch341_set_baudrate(struct usb_device *dev,
  102. struct ch341_private *priv)
  103. {
  104. short a, b;
  105. int r;
  106. unsigned long factor;
  107. short divisor;
  108. if (!priv->baud_rate)
  109. return -EINVAL;
  110. factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
  111. divisor = CH341_BAUDBASE_DIVMAX;
  112. while ((factor > 0xfff0) && divisor) {
  113. factor >>= 3;
  114. divisor--;
  115. }
  116. if (factor > 0xfff0)
  117. return -EINVAL;
  118. factor = 0x10000 - factor;
  119. a = (factor & 0xff00) | divisor;
  120. b = factor & 0xff;
  121. r = ch341_control_out(dev, 0x9a, 0x1312, a);
  122. if (!r)
  123. r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
  124. return r;
  125. }
  126. static int ch341_set_handshake(struct usb_device *dev, u8 control)
  127. {
  128. return ch341_control_out(dev, 0xa4, ~control, 0);
  129. }
  130. static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
  131. {
  132. char *buffer;
  133. int r;
  134. const unsigned size = 8;
  135. unsigned long flags;
  136. buffer = kmalloc(size, GFP_KERNEL);
  137. if (!buffer)
  138. return -ENOMEM;
  139. r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
  140. if (r < 0)
  141. goto out;
  142. /* setup the private status if available */
  143. if (r == 2) {
  144. r = 0;
  145. spin_lock_irqsave(&priv->lock, flags);
  146. priv->line_status = (~(*buffer)) & CH341_BITS_MODEM_STAT;
  147. spin_unlock_irqrestore(&priv->lock, flags);
  148. } else
  149. r = -EPROTO;
  150. out: kfree(buffer);
  151. return r;
  152. }
  153. /* -------------------------------------------------------------------------- */
  154. static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
  155. {
  156. char *buffer;
  157. int r;
  158. const unsigned size = 8;
  159. buffer = kmalloc(size, GFP_KERNEL);
  160. if (!buffer)
  161. return -ENOMEM;
  162. /* expect two bytes 0x27 0x00 */
  163. r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
  164. if (r < 0)
  165. goto out;
  166. r = ch341_control_out(dev, 0xa1, 0, 0);
  167. if (r < 0)
  168. goto out;
  169. r = ch341_set_baudrate(dev, priv);
  170. if (r < 0)
  171. goto out;
  172. /* expect two bytes 0x56 0x00 */
  173. r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
  174. if (r < 0)
  175. goto out;
  176. r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
  177. if (r < 0)
  178. goto out;
  179. /* expect 0xff 0xee */
  180. r = ch341_get_status(dev, priv);
  181. if (r < 0)
  182. goto out;
  183. r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
  184. if (r < 0)
  185. goto out;
  186. r = ch341_set_baudrate(dev, priv);
  187. if (r < 0)
  188. goto out;
  189. r = ch341_set_handshake(dev, priv->line_control);
  190. if (r < 0)
  191. goto out;
  192. /* expect 0x9f 0xee */
  193. r = ch341_get_status(dev, priv);
  194. out: kfree(buffer);
  195. return r;
  196. }
  197. static int ch341_port_probe(struct usb_serial_port *port)
  198. {
  199. struct ch341_private *priv;
  200. int r;
  201. priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
  202. if (!priv)
  203. return -ENOMEM;
  204. spin_lock_init(&priv->lock);
  205. priv->baud_rate = DEFAULT_BAUD_RATE;
  206. priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
  207. r = ch341_configure(port->serial->dev, priv);
  208. if (r < 0)
  209. goto error;
  210. usb_set_serial_port_data(port, priv);
  211. return 0;
  212. error: kfree(priv);
  213. return r;
  214. }
  215. static int ch341_port_remove(struct usb_serial_port *port)
  216. {
  217. struct ch341_private *priv;
  218. priv = usb_get_serial_port_data(port);
  219. kfree(priv);
  220. return 0;
  221. }
  222. static int ch341_carrier_raised(struct usb_serial_port *port)
  223. {
  224. struct ch341_private *priv = usb_get_serial_port_data(port);
  225. if (priv->line_status & CH341_BIT_DCD)
  226. return 1;
  227. return 0;
  228. }
  229. static void ch341_dtr_rts(struct usb_serial_port *port, int on)
  230. {
  231. struct ch341_private *priv = usb_get_serial_port_data(port);
  232. unsigned long flags;
  233. /* drop DTR and RTS */
  234. spin_lock_irqsave(&priv->lock, flags);
  235. if (on)
  236. priv->line_control |= CH341_BIT_RTS | CH341_BIT_DTR;
  237. else
  238. priv->line_control &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
  239. spin_unlock_irqrestore(&priv->lock, flags);
  240. ch341_set_handshake(port->serial->dev, priv->line_control);
  241. }
  242. static void ch341_close(struct usb_serial_port *port)
  243. {
  244. usb_serial_generic_close(port);
  245. usb_kill_urb(port->interrupt_in_urb);
  246. }
  247. /* open this device, set default parameters */
  248. static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
  249. {
  250. struct usb_serial *serial = port->serial;
  251. struct ch341_private *priv = usb_get_serial_port_data(port);
  252. int r;
  253. r = ch341_configure(serial->dev, priv);
  254. if (r)
  255. goto out;
  256. if (tty)
  257. ch341_set_termios(tty, port, NULL);
  258. dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__);
  259. r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  260. if (r) {
  261. dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n",
  262. __func__, r);
  263. goto out;
  264. }
  265. r = usb_serial_generic_open(tty, port);
  266. out: return r;
  267. }
  268. /* Old_termios contains the original termios settings and
  269. * tty->termios contains the new setting to be used.
  270. */
  271. static void ch341_set_termios(struct tty_struct *tty,
  272. struct usb_serial_port *port, struct ktermios *old_termios)
  273. {
  274. struct ch341_private *priv = usb_get_serial_port_data(port);
  275. unsigned baud_rate;
  276. unsigned long flags;
  277. baud_rate = tty_get_baud_rate(tty);
  278. priv->baud_rate = baud_rate;
  279. if (baud_rate) {
  280. spin_lock_irqsave(&priv->lock, flags);
  281. priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
  282. spin_unlock_irqrestore(&priv->lock, flags);
  283. ch341_set_baudrate(port->serial->dev, priv);
  284. } else {
  285. spin_lock_irqsave(&priv->lock, flags);
  286. priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
  287. spin_unlock_irqrestore(&priv->lock, flags);
  288. }
  289. ch341_set_handshake(port->serial->dev, priv->line_control);
  290. /* Unimplemented:
  291. * (cflag & CSIZE) : data bits [5, 8]
  292. * (cflag & PARENB) : parity {NONE, EVEN, ODD}
  293. * (cflag & CSTOPB) : stop bits [1, 2]
  294. */
  295. }
  296. static void ch341_break_ctl(struct tty_struct *tty, int break_state)
  297. {
  298. const uint16_t ch341_break_reg =
  299. ((uint16_t) CH341_REG_BREAK2 << 8) | CH341_REG_BREAK1;
  300. struct usb_serial_port *port = tty->driver_data;
  301. int r;
  302. uint16_t reg_contents;
  303. uint8_t *break_reg;
  304. break_reg = kmalloc(2, GFP_KERNEL);
  305. if (!break_reg)
  306. return;
  307. r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
  308. ch341_break_reg, 0, break_reg, 2);
  309. if (r < 0) {
  310. dev_err(&port->dev, "%s - USB control read error (%d)\n",
  311. __func__, r);
  312. goto out;
  313. }
  314. dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
  315. __func__, break_reg[0], break_reg[1]);
  316. if (break_state != 0) {
  317. dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
  318. break_reg[0] &= ~CH341_NBREAK_BITS_REG1;
  319. break_reg[1] &= ~CH341_NBREAK_BITS_REG2;
  320. } else {
  321. dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
  322. break_reg[0] |= CH341_NBREAK_BITS_REG1;
  323. break_reg[1] |= CH341_NBREAK_BITS_REG2;
  324. }
  325. dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
  326. __func__, break_reg[0], break_reg[1]);
  327. reg_contents = get_unaligned_le16(break_reg);
  328. r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
  329. ch341_break_reg, reg_contents);
  330. if (r < 0)
  331. dev_err(&port->dev, "%s - USB control write error (%d)\n",
  332. __func__, r);
  333. out:
  334. kfree(break_reg);
  335. }
  336. static int ch341_tiocmset(struct tty_struct *tty,
  337. unsigned int set, unsigned int clear)
  338. {
  339. struct usb_serial_port *port = tty->driver_data;
  340. struct ch341_private *priv = usb_get_serial_port_data(port);
  341. unsigned long flags;
  342. u8 control;
  343. spin_lock_irqsave(&priv->lock, flags);
  344. if (set & TIOCM_RTS)
  345. priv->line_control |= CH341_BIT_RTS;
  346. if (set & TIOCM_DTR)
  347. priv->line_control |= CH341_BIT_DTR;
  348. if (clear & TIOCM_RTS)
  349. priv->line_control &= ~CH341_BIT_RTS;
  350. if (clear & TIOCM_DTR)
  351. priv->line_control &= ~CH341_BIT_DTR;
  352. control = priv->line_control;
  353. spin_unlock_irqrestore(&priv->lock, flags);
  354. return ch341_set_handshake(port->serial->dev, control);
  355. }
  356. static void ch341_update_line_status(struct usb_serial_port *port,
  357. unsigned char *data, size_t len)
  358. {
  359. struct ch341_private *priv = usb_get_serial_port_data(port);
  360. struct tty_struct *tty;
  361. unsigned long flags;
  362. u8 status;
  363. u8 delta;
  364. if (len < 4)
  365. return;
  366. status = ~data[2] & CH341_BITS_MODEM_STAT;
  367. spin_lock_irqsave(&priv->lock, flags);
  368. delta = status ^ priv->line_status;
  369. priv->line_status = status;
  370. spin_unlock_irqrestore(&priv->lock, flags);
  371. if (data[1] & CH341_MULT_STAT)
  372. dev_dbg(&port->dev, "%s - multiple status change\n", __func__);
  373. if (!delta)
  374. return;
  375. if (delta & CH341_BIT_CTS)
  376. port->icount.cts++;
  377. if (delta & CH341_BIT_DSR)
  378. port->icount.dsr++;
  379. if (delta & CH341_BIT_RI)
  380. port->icount.rng++;
  381. if (delta & CH341_BIT_DCD) {
  382. port->icount.dcd++;
  383. tty = tty_port_tty_get(&port->port);
  384. if (tty) {
  385. usb_serial_handle_dcd_change(port, tty,
  386. status & CH341_BIT_DCD);
  387. tty_kref_put(tty);
  388. }
  389. }
  390. wake_up_interruptible(&port->port.delta_msr_wait);
  391. }
  392. static void ch341_read_int_callback(struct urb *urb)
  393. {
  394. struct usb_serial_port *port = urb->context;
  395. unsigned char *data = urb->transfer_buffer;
  396. unsigned int len = urb->actual_length;
  397. int status;
  398. switch (urb->status) {
  399. case 0:
  400. /* success */
  401. break;
  402. case -ECONNRESET:
  403. case -ENOENT:
  404. case -ESHUTDOWN:
  405. /* this urb is terminated, clean up */
  406. dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
  407. __func__, urb->status);
  408. return;
  409. default:
  410. dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
  411. __func__, urb->status);
  412. goto exit;
  413. }
  414. usb_serial_debug_data(&port->dev, __func__, len, data);
  415. ch341_update_line_status(port, data, len);
  416. exit:
  417. status = usb_submit_urb(urb, GFP_ATOMIC);
  418. if (status) {
  419. dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n",
  420. __func__, status);
  421. }
  422. }
  423. static int ch341_tiocmget(struct tty_struct *tty)
  424. {
  425. struct usb_serial_port *port = tty->driver_data;
  426. struct ch341_private *priv = usb_get_serial_port_data(port);
  427. unsigned long flags;
  428. u8 mcr;
  429. u8 status;
  430. unsigned int result;
  431. spin_lock_irqsave(&priv->lock, flags);
  432. mcr = priv->line_control;
  433. status = priv->line_status;
  434. spin_unlock_irqrestore(&priv->lock, flags);
  435. result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
  436. | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
  437. | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
  438. | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
  439. | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
  440. | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
  441. dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
  442. return result;
  443. }
  444. static int ch341_reset_resume(struct usb_serial *serial)
  445. {
  446. struct ch341_private *priv;
  447. priv = usb_get_serial_port_data(serial->port[0]);
  448. /* reconfigure ch341 serial port after bus-reset */
  449. ch341_configure(serial->dev, priv);
  450. return 0;
  451. }
  452. static struct usb_serial_driver ch341_device = {
  453. .driver = {
  454. .owner = THIS_MODULE,
  455. .name = "ch341-uart",
  456. },
  457. .id_table = id_table,
  458. .num_ports = 1,
  459. .open = ch341_open,
  460. .dtr_rts = ch341_dtr_rts,
  461. .carrier_raised = ch341_carrier_raised,
  462. .close = ch341_close,
  463. .set_termios = ch341_set_termios,
  464. .break_ctl = ch341_break_ctl,
  465. .tiocmget = ch341_tiocmget,
  466. .tiocmset = ch341_tiocmset,
  467. .tiocmiwait = usb_serial_generic_tiocmiwait,
  468. .read_int_callback = ch341_read_int_callback,
  469. .port_probe = ch341_port_probe,
  470. .port_remove = ch341_port_remove,
  471. .reset_resume = ch341_reset_resume,
  472. };
  473. static struct usb_serial_driver * const serial_drivers[] = {
  474. &ch341_device, NULL
  475. };
  476. module_usb_serial_driver(serial_drivers, id_table);
  477. MODULE_LICENSE("GPL");