ch341.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 bool debug;
  63. static const struct usb_device_id id_table[] = {
  64. { USB_DEVICE(0x4348, 0x5523) },
  65. { USB_DEVICE(0x1a86, 0x7523) },
  66. { USB_DEVICE(0x1a86, 0x5523) },
  67. { },
  68. };
  69. MODULE_DEVICE_TABLE(usb, id_table);
  70. struct ch341_private {
  71. spinlock_t lock; /* access lock */
  72. wait_queue_head_t delta_msr_wait; /* wait queue for modem status */
  73. unsigned baud_rate; /* set baud rate */
  74. u8 line_control; /* set line control value RTS/DTR */
  75. u8 line_status; /* active status of modem control inputs */
  76. u8 multi_status_change; /* status changed multiple since last call */
  77. };
  78. static int ch341_control_out(struct usb_device *dev, u8 request,
  79. u16 value, u16 index)
  80. {
  81. int r;
  82. dbg("ch341_control_out(%02x,%02x,%04x,%04x)", USB_DIR_OUT|0x40,
  83. (int)request, (int)value, (int)index);
  84. r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
  85. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  86. value, index, NULL, 0, DEFAULT_TIMEOUT);
  87. return r;
  88. }
  89. static int ch341_control_in(struct usb_device *dev,
  90. u8 request, u16 value, u16 index,
  91. char *buf, unsigned bufsize)
  92. {
  93. int r;
  94. dbg("ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)", USB_DIR_IN|0x40,
  95. (int)request, (int)value, (int)index, buf, (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. priv->multi_status_change = 0;
  148. spin_unlock_irqrestore(&priv->lock, flags);
  149. } else
  150. r = -EPROTO;
  151. out: kfree(buffer);
  152. return r;
  153. }
  154. /* -------------------------------------------------------------------------- */
  155. static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
  156. {
  157. char *buffer;
  158. int r;
  159. const unsigned size = 8;
  160. buffer = kmalloc(size, GFP_KERNEL);
  161. if (!buffer)
  162. return -ENOMEM;
  163. /* expect two bytes 0x27 0x00 */
  164. r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
  165. if (r < 0)
  166. goto out;
  167. r = ch341_control_out(dev, 0xa1, 0, 0);
  168. if (r < 0)
  169. goto out;
  170. r = ch341_set_baudrate(dev, priv);
  171. if (r < 0)
  172. goto out;
  173. /* expect two bytes 0x56 0x00 */
  174. r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
  175. if (r < 0)
  176. goto out;
  177. r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
  178. if (r < 0)
  179. goto out;
  180. /* expect 0xff 0xee */
  181. r = ch341_get_status(dev, priv);
  182. if (r < 0)
  183. goto out;
  184. r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
  185. if (r < 0)
  186. goto out;
  187. r = ch341_set_baudrate(dev, priv);
  188. if (r < 0)
  189. goto out;
  190. r = ch341_set_handshake(dev, priv->line_control);
  191. if (r < 0)
  192. goto out;
  193. /* expect 0x9f 0xee */
  194. r = ch341_get_status(dev, priv);
  195. out: kfree(buffer);
  196. return r;
  197. }
  198. /* allocate private data */
  199. static int ch341_attach(struct usb_serial *serial)
  200. {
  201. struct ch341_private *priv;
  202. int r;
  203. /* private data */
  204. priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
  205. if (!priv)
  206. return -ENOMEM;
  207. spin_lock_init(&priv->lock);
  208. init_waitqueue_head(&priv->delta_msr_wait);
  209. priv->baud_rate = DEFAULT_BAUD_RATE;
  210. priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
  211. r = ch341_configure(serial->dev, priv);
  212. if (r < 0)
  213. goto error;
  214. usb_set_serial_port_data(serial->port[0], priv);
  215. return 0;
  216. error: kfree(priv);
  217. return r;
  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. wake_up_interruptible(&priv->delta_msr_wait);
  239. }
  240. static void ch341_close(struct usb_serial_port *port)
  241. {
  242. usb_serial_generic_close(port);
  243. usb_kill_urb(port->interrupt_in_urb);
  244. }
  245. /* open this device, set default parameters */
  246. static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
  247. {
  248. struct usb_serial *serial = port->serial;
  249. struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
  250. int r;
  251. priv->baud_rate = DEFAULT_BAUD_RATE;
  252. r = ch341_configure(serial->dev, priv);
  253. if (r)
  254. goto out;
  255. r = ch341_set_handshake(serial->dev, priv->line_control);
  256. if (r)
  257. goto out;
  258. r = ch341_set_baudrate(serial->dev, priv);
  259. if (r)
  260. goto out;
  261. dbg("%s - submitting interrupt urb", __func__);
  262. r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  263. if (r) {
  264. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  265. " error %d\n", __func__, r);
  266. ch341_close(port);
  267. goto out;
  268. }
  269. r = usb_serial_generic_open(tty, port);
  270. out: return r;
  271. }
  272. /* Old_termios contains the original termios settings and
  273. * tty->termios contains the new setting to be used.
  274. */
  275. static void ch341_set_termios(struct tty_struct *tty,
  276. struct usb_serial_port *port, struct ktermios *old_termios)
  277. {
  278. struct ch341_private *priv = usb_get_serial_port_data(port);
  279. unsigned baud_rate;
  280. unsigned long flags;
  281. baud_rate = tty_get_baud_rate(tty);
  282. priv->baud_rate = baud_rate;
  283. if (baud_rate) {
  284. spin_lock_irqsave(&priv->lock, flags);
  285. priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
  286. spin_unlock_irqrestore(&priv->lock, flags);
  287. ch341_set_baudrate(port->serial->dev, priv);
  288. } else {
  289. spin_lock_irqsave(&priv->lock, flags);
  290. priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
  291. spin_unlock_irqrestore(&priv->lock, flags);
  292. }
  293. ch341_set_handshake(port->serial->dev, priv->line_control);
  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. }
  300. static void ch341_break_ctl(struct tty_struct *tty, int break_state)
  301. {
  302. const uint16_t ch341_break_reg =
  303. CH341_REG_BREAK1 | ((uint16_t) CH341_REG_BREAK2 << 8);
  304. struct usb_serial_port *port = tty->driver_data;
  305. int r;
  306. uint16_t reg_contents;
  307. uint8_t *break_reg;
  308. break_reg = kmalloc(2, GFP_KERNEL);
  309. if (!break_reg) {
  310. dev_err(&port->dev, "%s - kmalloc failed\n", __func__);
  311. return;
  312. }
  313. r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
  314. ch341_break_reg, 0, break_reg, 2);
  315. if (r < 0) {
  316. dev_err(&port->dev, "%s - USB control read error (%d)\n",
  317. __func__, r);
  318. goto out;
  319. }
  320. dbg("%s - initial ch341 break register contents - reg1: %x, reg2: %x",
  321. __func__, break_reg[0], break_reg[1]);
  322. if (break_state != 0) {
  323. dbg("%s - Enter break state requested", __func__);
  324. break_reg[0] &= ~CH341_NBREAK_BITS_REG1;
  325. break_reg[1] &= ~CH341_NBREAK_BITS_REG2;
  326. } else {
  327. dbg("%s - Leave break state requested", __func__);
  328. break_reg[0] |= CH341_NBREAK_BITS_REG1;
  329. break_reg[1] |= CH341_NBREAK_BITS_REG2;
  330. }
  331. dbg("%s - New ch341 break register contents - reg1: %x, reg2: %x",
  332. __func__, break_reg[0], break_reg[1]);
  333. reg_contents = get_unaligned_le16(break_reg);
  334. r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
  335. ch341_break_reg, reg_contents);
  336. if (r < 0)
  337. dev_err(&port->dev, "%s - USB control write error (%d)\n",
  338. __func__, r);
  339. out:
  340. kfree(break_reg);
  341. }
  342. static int ch341_tiocmset(struct tty_struct *tty,
  343. unsigned int set, unsigned int clear)
  344. {
  345. struct usb_serial_port *port = tty->driver_data;
  346. struct ch341_private *priv = usb_get_serial_port_data(port);
  347. unsigned long flags;
  348. u8 control;
  349. spin_lock_irqsave(&priv->lock, flags);
  350. if (set & TIOCM_RTS)
  351. priv->line_control |= CH341_BIT_RTS;
  352. if (set & TIOCM_DTR)
  353. priv->line_control |= CH341_BIT_DTR;
  354. if (clear & TIOCM_RTS)
  355. priv->line_control &= ~CH341_BIT_RTS;
  356. if (clear & TIOCM_DTR)
  357. priv->line_control &= ~CH341_BIT_DTR;
  358. control = priv->line_control;
  359. spin_unlock_irqrestore(&priv->lock, flags);
  360. return ch341_set_handshake(port->serial->dev, control);
  361. }
  362. static void ch341_read_int_callback(struct urb *urb)
  363. {
  364. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  365. unsigned char *data = urb->transfer_buffer;
  366. unsigned int actual_length = urb->actual_length;
  367. int status;
  368. switch (urb->status) {
  369. case 0:
  370. /* success */
  371. break;
  372. case -ECONNRESET:
  373. case -ENOENT:
  374. case -ESHUTDOWN:
  375. /* this urb is terminated, clean up */
  376. dbg("%s - urb shutting down with status: %d", __func__,
  377. urb->status);
  378. return;
  379. default:
  380. dbg("%s - nonzero urb status received: %d", __func__,
  381. urb->status);
  382. goto exit;
  383. }
  384. usb_serial_debug_data(debug, &port->dev, __func__,
  385. urb->actual_length, urb->transfer_buffer);
  386. if (actual_length >= 4) {
  387. struct ch341_private *priv = usb_get_serial_port_data(port);
  388. unsigned long flags;
  389. u8 prev_line_status = priv->line_status;
  390. spin_lock_irqsave(&priv->lock, flags);
  391. priv->line_status = (~(data[2])) & CH341_BITS_MODEM_STAT;
  392. if ((data[1] & CH341_MULT_STAT))
  393. priv->multi_status_change = 1;
  394. spin_unlock_irqrestore(&priv->lock, flags);
  395. if ((priv->line_status ^ prev_line_status) & CH341_BIT_DCD) {
  396. struct tty_struct *tty = tty_port_tty_get(&port->port);
  397. if (tty)
  398. usb_serial_handle_dcd_change(port, tty,
  399. priv->line_status & CH341_BIT_DCD);
  400. tty_kref_put(tty);
  401. }
  402. wake_up_interruptible(&priv->delta_msr_wait);
  403. }
  404. exit:
  405. status = usb_submit_urb(urb, GFP_ATOMIC);
  406. if (status)
  407. dev_err(&urb->dev->dev,
  408. "%s - usb_submit_urb failed with result %d\n",
  409. __func__, status);
  410. }
  411. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  412. {
  413. struct ch341_private *priv = usb_get_serial_port_data(port);
  414. unsigned long flags;
  415. u8 prevstatus;
  416. u8 status;
  417. u8 changed;
  418. u8 multi_change = 0;
  419. spin_lock_irqsave(&priv->lock, flags);
  420. prevstatus = priv->line_status;
  421. priv->multi_status_change = 0;
  422. spin_unlock_irqrestore(&priv->lock, flags);
  423. while (!multi_change) {
  424. interruptible_sleep_on(&priv->delta_msr_wait);
  425. /* see if a signal did it */
  426. if (signal_pending(current))
  427. return -ERESTARTSYS;
  428. spin_lock_irqsave(&priv->lock, flags);
  429. status = priv->line_status;
  430. multi_change = priv->multi_status_change;
  431. spin_unlock_irqrestore(&priv->lock, flags);
  432. changed = prevstatus ^ status;
  433. if (((arg & TIOCM_RNG) && (changed & CH341_BIT_RI)) ||
  434. ((arg & TIOCM_DSR) && (changed & CH341_BIT_DSR)) ||
  435. ((arg & TIOCM_CD) && (changed & CH341_BIT_DCD)) ||
  436. ((arg & TIOCM_CTS) && (changed & CH341_BIT_CTS))) {
  437. return 0;
  438. }
  439. prevstatus = status;
  440. }
  441. return 0;
  442. }
  443. static int ch341_ioctl(struct tty_struct *tty,
  444. unsigned int cmd, unsigned long arg)
  445. {
  446. struct usb_serial_port *port = tty->driver_data;
  447. dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
  448. switch (cmd) {
  449. case TIOCMIWAIT:
  450. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  451. return wait_modem_info(port, arg);
  452. default:
  453. dbg("%s not supported = 0x%04x", __func__, cmd);
  454. break;
  455. }
  456. return -ENOIOCTLCMD;
  457. }
  458. static int ch341_tiocmget(struct tty_struct *tty)
  459. {
  460. struct usb_serial_port *port = tty->driver_data;
  461. struct ch341_private *priv = usb_get_serial_port_data(port);
  462. unsigned long flags;
  463. u8 mcr;
  464. u8 status;
  465. unsigned int result;
  466. spin_lock_irqsave(&priv->lock, flags);
  467. mcr = priv->line_control;
  468. status = priv->line_status;
  469. spin_unlock_irqrestore(&priv->lock, flags);
  470. result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
  471. | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
  472. | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
  473. | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
  474. | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
  475. | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
  476. dbg("%s - result = %x", __func__, result);
  477. return result;
  478. }
  479. static int ch341_reset_resume(struct usb_serial *serial)
  480. {
  481. struct ch341_private *priv;
  482. priv = usb_get_serial_port_data(serial->port[0]);
  483. /* reconfigure ch341 serial port after bus-reset */
  484. ch341_configure(serial->dev, priv);
  485. return 0;
  486. }
  487. static struct usb_serial_driver ch341_device = {
  488. .driver = {
  489. .owner = THIS_MODULE,
  490. .name = "ch341-uart",
  491. },
  492. .id_table = id_table,
  493. .num_ports = 1,
  494. .open = ch341_open,
  495. .dtr_rts = ch341_dtr_rts,
  496. .carrier_raised = ch341_carrier_raised,
  497. .close = ch341_close,
  498. .ioctl = ch341_ioctl,
  499. .set_termios = ch341_set_termios,
  500. .break_ctl = ch341_break_ctl,
  501. .tiocmget = ch341_tiocmget,
  502. .tiocmset = ch341_tiocmset,
  503. .read_int_callback = ch341_read_int_callback,
  504. .attach = ch341_attach,
  505. .reset_resume = ch341_reset_resume,
  506. };
  507. static struct usb_serial_driver * const serial_drivers[] = {
  508. &ch341_device, NULL
  509. };
  510. module_usb_serial_driver(serial_drivers, id_table);
  511. MODULE_LICENSE("GPL");
  512. module_param(debug, bool, S_IRUGO | S_IWUSR);
  513. MODULE_PARM_DESC(debug, "Debug enabled or not");