ch341.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. ch341_close(port);
  264. goto out;
  265. }
  266. r = usb_serial_generic_open(tty, port);
  267. out: return r;
  268. }
  269. /* Old_termios contains the original termios settings and
  270. * tty->termios contains the new setting to be used.
  271. */
  272. static void ch341_set_termios(struct tty_struct *tty,
  273. struct usb_serial_port *port, struct ktermios *old_termios)
  274. {
  275. struct ch341_private *priv = usb_get_serial_port_data(port);
  276. unsigned baud_rate;
  277. unsigned long flags;
  278. baud_rate = tty_get_baud_rate(tty);
  279. priv->baud_rate = baud_rate;
  280. if (baud_rate) {
  281. spin_lock_irqsave(&priv->lock, flags);
  282. priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
  283. spin_unlock_irqrestore(&priv->lock, flags);
  284. ch341_set_baudrate(port->serial->dev, priv);
  285. } else {
  286. spin_lock_irqsave(&priv->lock, flags);
  287. priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
  288. spin_unlock_irqrestore(&priv->lock, flags);
  289. }
  290. ch341_set_handshake(port->serial->dev, priv->line_control);
  291. /* Unimplemented:
  292. * (cflag & CSIZE) : data bits [5, 8]
  293. * (cflag & PARENB) : parity {NONE, EVEN, ODD}
  294. * (cflag & CSTOPB) : stop bits [1, 2]
  295. */
  296. }
  297. static void ch341_break_ctl(struct tty_struct *tty, int break_state)
  298. {
  299. const uint16_t ch341_break_reg =
  300. CH341_REG_BREAK1 | ((uint16_t) CH341_REG_BREAK2 << 8);
  301. struct usb_serial_port *port = tty->driver_data;
  302. int r;
  303. uint16_t reg_contents;
  304. uint8_t *break_reg;
  305. break_reg = kmalloc(2, GFP_KERNEL);
  306. if (!break_reg)
  307. return;
  308. r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
  309. ch341_break_reg, 0, break_reg, 2);
  310. if (r < 0) {
  311. dev_err(&port->dev, "%s - USB control read error (%d)\n",
  312. __func__, r);
  313. goto out;
  314. }
  315. dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
  316. __func__, break_reg[0], break_reg[1]);
  317. if (break_state != 0) {
  318. dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
  319. break_reg[0] &= ~CH341_NBREAK_BITS_REG1;
  320. break_reg[1] &= ~CH341_NBREAK_BITS_REG2;
  321. } else {
  322. dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
  323. break_reg[0] |= CH341_NBREAK_BITS_REG1;
  324. break_reg[1] |= CH341_NBREAK_BITS_REG2;
  325. }
  326. dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
  327. __func__, break_reg[0], break_reg[1]);
  328. reg_contents = get_unaligned_le16(break_reg);
  329. r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
  330. ch341_break_reg, reg_contents);
  331. if (r < 0)
  332. dev_err(&port->dev, "%s - USB control write error (%d)\n",
  333. __func__, r);
  334. out:
  335. kfree(break_reg);
  336. }
  337. static int ch341_tiocmset(struct tty_struct *tty,
  338. unsigned int set, unsigned int clear)
  339. {
  340. struct usb_serial_port *port = tty->driver_data;
  341. struct ch341_private *priv = usb_get_serial_port_data(port);
  342. unsigned long flags;
  343. u8 control;
  344. spin_lock_irqsave(&priv->lock, flags);
  345. if (set & TIOCM_RTS)
  346. priv->line_control |= CH341_BIT_RTS;
  347. if (set & TIOCM_DTR)
  348. priv->line_control |= CH341_BIT_DTR;
  349. if (clear & TIOCM_RTS)
  350. priv->line_control &= ~CH341_BIT_RTS;
  351. if (clear & TIOCM_DTR)
  352. priv->line_control &= ~CH341_BIT_DTR;
  353. control = priv->line_control;
  354. spin_unlock_irqrestore(&priv->lock, flags);
  355. return ch341_set_handshake(port->serial->dev, control);
  356. }
  357. static void ch341_update_line_status(struct usb_serial_port *port,
  358. unsigned char *data, size_t len)
  359. {
  360. struct ch341_private *priv = usb_get_serial_port_data(port);
  361. struct tty_struct *tty;
  362. unsigned long flags;
  363. u8 status;
  364. u8 delta;
  365. if (len < 4)
  366. return;
  367. status = ~data[2] & CH341_BITS_MODEM_STAT;
  368. spin_lock_irqsave(&priv->lock, flags);
  369. delta = status ^ priv->line_status;
  370. priv->line_status = status;
  371. spin_unlock_irqrestore(&priv->lock, flags);
  372. if (data[1] & CH341_MULT_STAT)
  373. dev_dbg(&port->dev, "%s - multiple status change\n", __func__);
  374. if (!delta)
  375. return;
  376. if (delta & CH341_BIT_CTS)
  377. port->icount.cts++;
  378. if (delta & CH341_BIT_DSR)
  379. port->icount.dsr++;
  380. if (delta & CH341_BIT_RI)
  381. port->icount.rng++;
  382. if (delta & CH341_BIT_DCD) {
  383. port->icount.dcd++;
  384. tty = tty_port_tty_get(&port->port);
  385. if (tty) {
  386. usb_serial_handle_dcd_change(port, tty,
  387. status & CH341_BIT_DCD);
  388. tty_kref_put(tty);
  389. }
  390. }
  391. wake_up_interruptible(&port->port.delta_msr_wait);
  392. }
  393. static void ch341_read_int_callback(struct urb *urb)
  394. {
  395. struct usb_serial_port *port = urb->context;
  396. unsigned char *data = urb->transfer_buffer;
  397. unsigned int len = urb->actual_length;
  398. int status;
  399. switch (urb->status) {
  400. case 0:
  401. /* success */
  402. break;
  403. case -ECONNRESET:
  404. case -ENOENT:
  405. case -ESHUTDOWN:
  406. /* this urb is terminated, clean up */
  407. dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
  408. __func__, urb->status);
  409. return;
  410. default:
  411. dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
  412. __func__, urb->status);
  413. goto exit;
  414. }
  415. usb_serial_debug_data(&port->dev, __func__, len, data);
  416. ch341_update_line_status(port, data, len);
  417. exit:
  418. status = usb_submit_urb(urb, GFP_ATOMIC);
  419. if (status) {
  420. dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n",
  421. __func__, status);
  422. }
  423. }
  424. static int ch341_tiocmget(struct tty_struct *tty)
  425. {
  426. struct usb_serial_port *port = tty->driver_data;
  427. struct ch341_private *priv = usb_get_serial_port_data(port);
  428. unsigned long flags;
  429. u8 mcr;
  430. u8 status;
  431. unsigned int result;
  432. spin_lock_irqsave(&priv->lock, flags);
  433. mcr = priv->line_control;
  434. status = priv->line_status;
  435. spin_unlock_irqrestore(&priv->lock, flags);
  436. result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
  437. | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
  438. | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
  439. | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
  440. | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
  441. | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
  442. dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
  443. return result;
  444. }
  445. static int ch341_reset_resume(struct usb_serial *serial)
  446. {
  447. struct ch341_private *priv;
  448. priv = usb_get_serial_port_data(serial->port[0]);
  449. /* reconfigure ch341 serial port after bus-reset */
  450. ch341_configure(serial->dev, priv);
  451. return 0;
  452. }
  453. static struct usb_serial_driver ch341_device = {
  454. .driver = {
  455. .owner = THIS_MODULE,
  456. .name = "ch341-uart",
  457. },
  458. .id_table = id_table,
  459. .num_ports = 1,
  460. .open = ch341_open,
  461. .dtr_rts = ch341_dtr_rts,
  462. .carrier_raised = ch341_carrier_raised,
  463. .close = ch341_close,
  464. .set_termios = ch341_set_termios,
  465. .break_ctl = ch341_break_ctl,
  466. .tiocmget = ch341_tiocmget,
  467. .tiocmset = ch341_tiocmset,
  468. .tiocmiwait = usb_serial_generic_tiocmiwait,
  469. .read_int_callback = ch341_read_int_callback,
  470. .port_probe = ch341_port_probe,
  471. .port_remove = ch341_port_remove,
  472. .reset_resume = ch341_reset_resume,
  473. };
  474. static struct usb_serial_driver * const serial_drivers[] = {
  475. &ch341_device, NULL
  476. };
  477. module_usb_serial_driver(serial_drivers, id_table);
  478. MODULE_LICENSE("GPL");