ch341.c 15 KB

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