ch341.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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/init.h>
  19. #include <linux/tty.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/usb.h>
  23. #include <linux/usb/serial.h>
  24. #include <linux/serial.h>
  25. #include <asm/unaligned.h>
  26. #define DEFAULT_BAUD_RATE 9600
  27. #define DEFAULT_TIMEOUT 1000
  28. /* flags for IO-Bits */
  29. #define CH341_BIT_RTS (1 << 6)
  30. #define CH341_BIT_DTR (1 << 5)
  31. /******************************/
  32. /* interrupt pipe definitions */
  33. /******************************/
  34. /* always 4 interrupt bytes */
  35. /* first irq byte normally 0x08 */
  36. /* second irq byte base 0x7d + below */
  37. /* third irq byte base 0x94 + below */
  38. /* fourth irq byte normally 0xee */
  39. /* second interrupt byte */
  40. #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
  41. /* status returned in third interrupt answer byte, inverted in data
  42. from irq */
  43. #define CH341_BIT_CTS 0x01
  44. #define CH341_BIT_DSR 0x02
  45. #define CH341_BIT_RI 0x04
  46. #define CH341_BIT_DCD 0x08
  47. #define CH341_BITS_MODEM_STAT 0x0f /* all bits */
  48. /*******************************/
  49. /* baudrate calculation factor */
  50. /*******************************/
  51. #define CH341_BAUDBASE_FACTOR 1532620800
  52. #define CH341_BAUDBASE_DIVMAX 3
  53. /* Break support - the information used to implement this was gleaned from
  54. * the Net/FreeBSD uchcom.c driver by Takanori Watanabe. Domo arigato.
  55. */
  56. #define CH341_REQ_WRITE_REG 0x9A
  57. #define CH341_REQ_READ_REG 0x95
  58. #define CH341_REG_BREAK1 0x05
  59. #define CH341_REG_BREAK2 0x18
  60. #define CH341_NBREAK_BITS_REG1 0x01
  61. #define CH341_NBREAK_BITS_REG2 0x40
  62. static const struct usb_device_id id_table[] = {
  63. { USB_DEVICE(0x4348, 0x5523) },
  64. { USB_DEVICE(0x1a86, 0x7523) },
  65. { USB_DEVICE(0x1a86, 0x5523) },
  66. { },
  67. };
  68. MODULE_DEVICE_TABLE(usb, id_table);
  69. struct ch341_private {
  70. spinlock_t lock; /* access lock */
  71. unsigned baud_rate; /* set baud rate */
  72. u8 line_control; /* set line control value RTS/DTR */
  73. u8 line_status; /* active status of modem control inputs */
  74. u8 multi_status_change; /* status changed multiple since last call */
  75. };
  76. static int ch341_control_out(struct usb_device *dev, u8 request,
  77. u16 value, u16 index)
  78. {
  79. int r;
  80. dev_dbg(&dev->dev, "ch341_control_out(%02x,%02x,%04x,%04x)\n",
  81. USB_DIR_OUT|0x40, (int)request, (int)value, (int)index);
  82. r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
  83. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  84. value, index, NULL, 0, DEFAULT_TIMEOUT);
  85. return r;
  86. }
  87. static int ch341_control_in(struct usb_device *dev,
  88. u8 request, u16 value, u16 index,
  89. char *buf, unsigned bufsize)
  90. {
  91. int r;
  92. dev_dbg(&dev->dev, "ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)\n",
  93. USB_DIR_IN|0x40, (int)request, (int)value, (int)index, buf,
  94. (int)bufsize);
  95. r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
  96. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  97. value, index, buf, bufsize, DEFAULT_TIMEOUT);
  98. return r;
  99. }
  100. static int ch341_set_baudrate(struct usb_device *dev,
  101. struct ch341_private *priv)
  102. {
  103. short a, b;
  104. int r;
  105. unsigned long factor;
  106. short divisor;
  107. if (!priv->baud_rate)
  108. return -EINVAL;
  109. factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
  110. divisor = CH341_BAUDBASE_DIVMAX;
  111. while ((factor > 0xfff0) && divisor) {
  112. factor >>= 3;
  113. divisor--;
  114. }
  115. if (factor > 0xfff0)
  116. return -EINVAL;
  117. factor = 0x10000 - factor;
  118. a = (factor & 0xff00) | divisor;
  119. b = factor & 0xff;
  120. r = ch341_control_out(dev, 0x9a, 0x1312, a);
  121. if (!r)
  122. r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
  123. return r;
  124. }
  125. static int ch341_set_handshake(struct usb_device *dev, u8 control)
  126. {
  127. return ch341_control_out(dev, 0xa4, ~control, 0);
  128. }
  129. static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
  130. {
  131. char *buffer;
  132. int r;
  133. const unsigned size = 8;
  134. unsigned long flags;
  135. buffer = kmalloc(size, GFP_KERNEL);
  136. if (!buffer)
  137. return -ENOMEM;
  138. r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
  139. if (r < 0)
  140. goto out;
  141. /* setup the private status if available */
  142. if (r == 2) {
  143. r = 0;
  144. spin_lock_irqsave(&priv->lock, flags);
  145. priv->line_status = (~(*buffer)) & CH341_BITS_MODEM_STAT;
  146. priv->multi_status_change = 0;
  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. wake_up_interruptible(&port->delta_msr_wait);
  242. }
  243. static void ch341_close(struct usb_serial_port *port)
  244. {
  245. usb_serial_generic_close(port);
  246. usb_kill_urb(port->interrupt_in_urb);
  247. }
  248. /* open this device, set default parameters */
  249. static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
  250. {
  251. struct usb_serial *serial = port->serial;
  252. struct ch341_private *priv = usb_get_serial_port_data(port);
  253. int r;
  254. priv->baud_rate = DEFAULT_BAUD_RATE;
  255. r = ch341_configure(serial->dev, priv);
  256. if (r)
  257. goto out;
  258. r = ch341_set_handshake(serial->dev, priv->line_control);
  259. if (r)
  260. goto out;
  261. r = ch341_set_baudrate(serial->dev, priv);
  262. if (r)
  263. goto out;
  264. dev_dbg(&port->dev, "%s - submitting interrupt urb", __func__);
  265. r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  266. if (r) {
  267. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  268. " error %d\n", __func__, r);
  269. ch341_close(port);
  270. goto out;
  271. }
  272. r = usb_serial_generic_open(tty, port);
  273. out: return r;
  274. }
  275. /* Old_termios contains the original termios settings and
  276. * tty->termios contains the new setting to be used.
  277. */
  278. static void ch341_set_termios(struct tty_struct *tty,
  279. struct usb_serial_port *port, struct ktermios *old_termios)
  280. {
  281. struct ch341_private *priv = usb_get_serial_port_data(port);
  282. unsigned baud_rate;
  283. unsigned long flags;
  284. baud_rate = tty_get_baud_rate(tty);
  285. priv->baud_rate = baud_rate;
  286. if (baud_rate) {
  287. spin_lock_irqsave(&priv->lock, flags);
  288. priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
  289. spin_unlock_irqrestore(&priv->lock, flags);
  290. ch341_set_baudrate(port->serial->dev, priv);
  291. } else {
  292. spin_lock_irqsave(&priv->lock, flags);
  293. priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
  294. spin_unlock_irqrestore(&priv->lock, flags);
  295. }
  296. ch341_set_handshake(port->serial->dev, priv->line_control);
  297. /* Unimplemented:
  298. * (cflag & CSIZE) : data bits [5, 8]
  299. * (cflag & PARENB) : parity {NONE, EVEN, ODD}
  300. * (cflag & CSTOPB) : stop bits [1, 2]
  301. */
  302. }
  303. static void ch341_break_ctl(struct tty_struct *tty, int break_state)
  304. {
  305. const uint16_t ch341_break_reg =
  306. CH341_REG_BREAK1 | ((uint16_t) CH341_REG_BREAK2 << 8);
  307. struct usb_serial_port *port = tty->driver_data;
  308. int r;
  309. uint16_t reg_contents;
  310. uint8_t *break_reg;
  311. break_reg = kmalloc(2, GFP_KERNEL);
  312. if (!break_reg) {
  313. dev_err(&port->dev, "%s - kmalloc failed\n", __func__);
  314. return;
  315. }
  316. r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
  317. ch341_break_reg, 0, break_reg, 2);
  318. if (r < 0) {
  319. dev_err(&port->dev, "%s - USB control read error (%d)\n",
  320. __func__, r);
  321. goto out;
  322. }
  323. dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
  324. __func__, break_reg[0], break_reg[1]);
  325. if (break_state != 0) {
  326. dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
  327. break_reg[0] &= ~CH341_NBREAK_BITS_REG1;
  328. break_reg[1] &= ~CH341_NBREAK_BITS_REG2;
  329. } else {
  330. dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
  331. break_reg[0] |= CH341_NBREAK_BITS_REG1;
  332. break_reg[1] |= CH341_NBREAK_BITS_REG2;
  333. }
  334. dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
  335. __func__, break_reg[0], break_reg[1]);
  336. reg_contents = get_unaligned_le16(break_reg);
  337. r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
  338. ch341_break_reg, reg_contents);
  339. if (r < 0)
  340. dev_err(&port->dev, "%s - USB control write error (%d)\n",
  341. __func__, r);
  342. out:
  343. kfree(break_reg);
  344. }
  345. static int ch341_tiocmset(struct tty_struct *tty,
  346. unsigned int set, unsigned int clear)
  347. {
  348. struct usb_serial_port *port = tty->driver_data;
  349. struct ch341_private *priv = usb_get_serial_port_data(port);
  350. unsigned long flags;
  351. u8 control;
  352. spin_lock_irqsave(&priv->lock, flags);
  353. if (set & TIOCM_RTS)
  354. priv->line_control |= CH341_BIT_RTS;
  355. if (set & TIOCM_DTR)
  356. priv->line_control |= CH341_BIT_DTR;
  357. if (clear & TIOCM_RTS)
  358. priv->line_control &= ~CH341_BIT_RTS;
  359. if (clear & TIOCM_DTR)
  360. priv->line_control &= ~CH341_BIT_DTR;
  361. control = priv->line_control;
  362. spin_unlock_irqrestore(&priv->lock, flags);
  363. return ch341_set_handshake(port->serial->dev, control);
  364. }
  365. static void ch341_read_int_callback(struct urb *urb)
  366. {
  367. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  368. unsigned char *data = urb->transfer_buffer;
  369. unsigned int actual_length = urb->actual_length;
  370. int status;
  371. switch (urb->status) {
  372. case 0:
  373. /* success */
  374. break;
  375. case -ECONNRESET:
  376. case -ENOENT:
  377. case -ESHUTDOWN:
  378. /* this urb is terminated, clean up */
  379. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
  380. __func__, urb->status);
  381. return;
  382. default:
  383. dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
  384. __func__, urb->status);
  385. goto exit;
  386. }
  387. usb_serial_debug_data(&port->dev, __func__,
  388. urb->actual_length, urb->transfer_buffer);
  389. if (actual_length >= 4) {
  390. struct ch341_private *priv = usb_get_serial_port_data(port);
  391. unsigned long flags;
  392. u8 prev_line_status = priv->line_status;
  393. spin_lock_irqsave(&priv->lock, flags);
  394. priv->line_status = (~(data[2])) & CH341_BITS_MODEM_STAT;
  395. if ((data[1] & CH341_MULT_STAT))
  396. priv->multi_status_change = 1;
  397. spin_unlock_irqrestore(&priv->lock, flags);
  398. if ((priv->line_status ^ prev_line_status) & CH341_BIT_DCD) {
  399. struct tty_struct *tty = tty_port_tty_get(&port->port);
  400. if (tty)
  401. usb_serial_handle_dcd_change(port, tty,
  402. priv->line_status & CH341_BIT_DCD);
  403. tty_kref_put(tty);
  404. }
  405. wake_up_interruptible(&port->delta_msr_wait);
  406. }
  407. exit:
  408. status = usb_submit_urb(urb, GFP_ATOMIC);
  409. if (status)
  410. dev_err(&urb->dev->dev,
  411. "%s - usb_submit_urb failed with result %d\n",
  412. __func__, status);
  413. }
  414. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  415. {
  416. struct ch341_private *priv = usb_get_serial_port_data(port);
  417. unsigned long flags;
  418. u8 prevstatus;
  419. u8 status;
  420. u8 changed;
  421. u8 multi_change = 0;
  422. spin_lock_irqsave(&priv->lock, flags);
  423. prevstatus = priv->line_status;
  424. priv->multi_status_change = 0;
  425. spin_unlock_irqrestore(&priv->lock, flags);
  426. while (!multi_change) {
  427. interruptible_sleep_on(&port->delta_msr_wait);
  428. /* see if a signal did it */
  429. if (signal_pending(current))
  430. return -ERESTARTSYS;
  431. if (port->serial->disconnected)
  432. return -EIO;
  433. spin_lock_irqsave(&priv->lock, flags);
  434. status = priv->line_status;
  435. multi_change = priv->multi_status_change;
  436. spin_unlock_irqrestore(&priv->lock, flags);
  437. changed = prevstatus ^ status;
  438. if (((arg & TIOCM_RNG) && (changed & CH341_BIT_RI)) ||
  439. ((arg & TIOCM_DSR) && (changed & CH341_BIT_DSR)) ||
  440. ((arg & TIOCM_CD) && (changed & CH341_BIT_DCD)) ||
  441. ((arg & TIOCM_CTS) && (changed & CH341_BIT_CTS))) {
  442. return 0;
  443. }
  444. prevstatus = status;
  445. }
  446. return 0;
  447. }
  448. static int ch341_ioctl(struct tty_struct *tty,
  449. unsigned int cmd, unsigned long arg)
  450. {
  451. struct usb_serial_port *port = tty->driver_data;
  452. dev_dbg(&port->dev, "%s (%d) cmd = 0x%04x\n", __func__, port->number, cmd);
  453. switch (cmd) {
  454. case TIOCMIWAIT:
  455. dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
  456. return wait_modem_info(port, arg);
  457. default:
  458. dev_dbg(&port->dev, "%s not supported = 0x%04x\n", __func__, cmd);
  459. break;
  460. }
  461. return -ENOIOCTLCMD;
  462. }
  463. static int ch341_tiocmget(struct tty_struct *tty)
  464. {
  465. struct usb_serial_port *port = tty->driver_data;
  466. struct ch341_private *priv = usb_get_serial_port_data(port);
  467. unsigned long flags;
  468. u8 mcr;
  469. u8 status;
  470. unsigned int result;
  471. spin_lock_irqsave(&priv->lock, flags);
  472. mcr = priv->line_control;
  473. status = priv->line_status;
  474. spin_unlock_irqrestore(&priv->lock, flags);
  475. result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
  476. | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
  477. | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
  478. | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
  479. | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
  480. | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
  481. dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
  482. return result;
  483. }
  484. static int ch341_reset_resume(struct usb_serial *serial)
  485. {
  486. struct ch341_private *priv;
  487. priv = usb_get_serial_port_data(serial->port[0]);
  488. /* reconfigure ch341 serial port after bus-reset */
  489. ch341_configure(serial->dev, priv);
  490. return 0;
  491. }
  492. static struct usb_serial_driver ch341_device = {
  493. .driver = {
  494. .owner = THIS_MODULE,
  495. .name = "ch341-uart",
  496. },
  497. .id_table = id_table,
  498. .num_ports = 1,
  499. .open = ch341_open,
  500. .dtr_rts = ch341_dtr_rts,
  501. .carrier_raised = ch341_carrier_raised,
  502. .close = ch341_close,
  503. .ioctl = ch341_ioctl,
  504. .set_termios = ch341_set_termios,
  505. .break_ctl = ch341_break_ctl,
  506. .tiocmget = ch341_tiocmget,
  507. .tiocmset = ch341_tiocmset,
  508. .read_int_callback = ch341_read_int_callback,
  509. .port_probe = ch341_port_probe,
  510. .port_remove = ch341_port_remove,
  511. .reset_resume = ch341_reset_resume,
  512. };
  513. static struct usb_serial_driver * const serial_drivers[] = {
  514. &ch341_device, NULL
  515. };
  516. module_usb_serial_driver(serial_drivers, id_table);
  517. MODULE_LICENSE("GPL");