ch341.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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/usb.h>
  22. #include <linux/usb/serial.h>
  23. #include <linux/serial.h>
  24. #define DEFAULT_BAUD_RATE 9600
  25. #define DEFAULT_TIMEOUT 1000
  26. /* flags for IO-Bits */
  27. #define CH341_BIT_RTS (1 << 6)
  28. #define CH341_BIT_DTR (1 << 5)
  29. /******************************/
  30. /* interrupt pipe definitions */
  31. /******************************/
  32. /* always 4 interrupt bytes */
  33. /* first irq byte normally 0x08 */
  34. /* second irq byte base 0x7d + below */
  35. /* third irq byte base 0x94 + below */
  36. /* fourth irq byte normally 0xee */
  37. /* second interrupt byte */
  38. #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
  39. /* status returned in third interrupt answer byte, inverted in data
  40. from irq */
  41. #define CH341_BIT_CTS 0x01
  42. #define CH341_BIT_DSR 0x02
  43. #define CH341_BIT_RI 0x04
  44. #define CH341_BIT_DCD 0x08
  45. #define CH341_BITS_MODEM_STAT 0x0f /* all bits */
  46. /*******************************/
  47. /* baudrate calculation factor */
  48. /*******************************/
  49. #define CH341_BAUDBASE_FACTOR 1532620800
  50. #define CH341_BAUDBASE_DIVMAX 3
  51. static int debug;
  52. static struct usb_device_id id_table [] = {
  53. { USB_DEVICE(0x4348, 0x5523) },
  54. { USB_DEVICE(0x1a86, 0x7523) },
  55. { },
  56. };
  57. MODULE_DEVICE_TABLE(usb, id_table);
  58. struct ch341_private {
  59. spinlock_t lock; /* access lock */
  60. wait_queue_head_t delta_msr_wait; /* wait queue for modem status */
  61. unsigned baud_rate; /* set baud rate */
  62. u8 line_control; /* set line control value RTS/DTR */
  63. u8 line_status; /* active status of modem control inputs */
  64. u8 multi_status_change; /* status changed multiple since last call */
  65. };
  66. static int ch341_control_out(struct usb_device *dev, u8 request,
  67. u16 value, u16 index)
  68. {
  69. int r;
  70. dbg("ch341_control_out(%02x,%02x,%04x,%04x)", USB_DIR_OUT|0x40,
  71. (int)request, (int)value, (int)index);
  72. r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
  73. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  74. value, index, NULL, 0, DEFAULT_TIMEOUT);
  75. return r;
  76. }
  77. static int ch341_control_in(struct usb_device *dev,
  78. u8 request, u16 value, u16 index,
  79. char *buf, unsigned bufsize)
  80. {
  81. int r;
  82. dbg("ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)", USB_DIR_IN|0x40,
  83. (int)request, (int)value, (int)index, buf, (int)bufsize);
  84. r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
  85. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  86. value, index, buf, bufsize, DEFAULT_TIMEOUT);
  87. return r;
  88. }
  89. static int ch341_set_baudrate(struct usb_device *dev,
  90. struct ch341_private *priv)
  91. {
  92. short a, b;
  93. int r;
  94. unsigned long factor;
  95. short divisor;
  96. dbg("ch341_set_baudrate(%d)", priv->baud_rate);
  97. if (!priv->baud_rate)
  98. return -EINVAL;
  99. factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
  100. divisor = CH341_BAUDBASE_DIVMAX;
  101. while ((factor > 0xfff0) && divisor) {
  102. factor >>= 3;
  103. divisor--;
  104. }
  105. if (factor > 0xfff0)
  106. return -EINVAL;
  107. factor = 0x10000 - factor;
  108. a = (factor & 0xff00) | divisor;
  109. b = factor & 0xff;
  110. r = ch341_control_out(dev, 0x9a, 0x1312, a);
  111. if (!r)
  112. r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
  113. return r;
  114. }
  115. static int ch341_set_handshake(struct usb_device *dev, u8 control)
  116. {
  117. dbg("ch341_set_handshake(0x%02x)", control);
  118. return ch341_control_out(dev, 0xa4, ~control, 0);
  119. }
  120. static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
  121. {
  122. char *buffer;
  123. int r;
  124. const unsigned size = 8;
  125. unsigned long flags;
  126. dbg("ch341_get_status()");
  127. buffer = kmalloc(size, GFP_KERNEL);
  128. if (!buffer)
  129. return -ENOMEM;
  130. r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
  131. if (r < 0)
  132. goto out;
  133. /* setup the private status if available */
  134. if (r == 2) {
  135. r = 0;
  136. spin_lock_irqsave(&priv->lock, flags);
  137. priv->line_status = (~(*buffer)) & CH341_BITS_MODEM_STAT;
  138. priv->multi_status_change = 0;
  139. spin_unlock_irqrestore(&priv->lock, flags);
  140. } else
  141. r = -EPROTO;
  142. out: kfree(buffer);
  143. return r;
  144. }
  145. /* -------------------------------------------------------------------------- */
  146. static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
  147. {
  148. char *buffer;
  149. int r;
  150. const unsigned size = 8;
  151. dbg("ch341_configure()");
  152. buffer = kmalloc(size, GFP_KERNEL);
  153. if (!buffer)
  154. return -ENOMEM;
  155. /* expect two bytes 0x27 0x00 */
  156. r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
  157. if (r < 0)
  158. goto out;
  159. r = ch341_control_out(dev, 0xa1, 0, 0);
  160. if (r < 0)
  161. goto out;
  162. r = ch341_set_baudrate(dev, priv);
  163. if (r < 0)
  164. goto out;
  165. /* expect two bytes 0x56 0x00 */
  166. r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
  167. if (r < 0)
  168. goto out;
  169. r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
  170. if (r < 0)
  171. goto out;
  172. /* expect 0xff 0xee */
  173. r = ch341_get_status(dev, priv);
  174. if (r < 0)
  175. goto out;
  176. r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
  177. if (r < 0)
  178. goto out;
  179. r = ch341_set_baudrate(dev, priv);
  180. if (r < 0)
  181. goto out;
  182. r = ch341_set_handshake(dev, priv->line_control);
  183. if (r < 0)
  184. goto out;
  185. /* expect 0x9f 0xee */
  186. r = ch341_get_status(dev, priv);
  187. out: kfree(buffer);
  188. return r;
  189. }
  190. /* allocate private data */
  191. static int ch341_attach(struct usb_serial *serial)
  192. {
  193. struct ch341_private *priv;
  194. int r;
  195. dbg("ch341_attach()");
  196. /* private data */
  197. priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
  198. if (!priv)
  199. return -ENOMEM;
  200. spin_lock_init(&priv->lock);
  201. init_waitqueue_head(&priv->delta_msr_wait);
  202. priv->baud_rate = DEFAULT_BAUD_RATE;
  203. priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
  204. r = ch341_configure(serial->dev, priv);
  205. if (r < 0)
  206. goto error;
  207. usb_set_serial_port_data(serial->port[0], priv);
  208. return 0;
  209. error: kfree(priv);
  210. return r;
  211. }
  212. static void ch341_close(struct tty_struct *tty, struct usb_serial_port *port,
  213. struct file *filp)
  214. {
  215. struct ch341_private *priv = usb_get_serial_port_data(port);
  216. unsigned long flags;
  217. unsigned int c_cflag;
  218. dbg("%s - port %d", __func__, port->number);
  219. /* shutdown our urbs */
  220. dbg("%s - shutting down urbs", __func__);
  221. usb_kill_urb(port->write_urb);
  222. usb_kill_urb(port->read_urb);
  223. usb_kill_urb(port->interrupt_in_urb);
  224. if (tty) {
  225. c_cflag = tty->termios->c_cflag;
  226. if (c_cflag & HUPCL) {
  227. /* drop DTR and RTS */
  228. spin_lock_irqsave(&priv->lock, flags);
  229. priv->line_control = 0;
  230. spin_unlock_irqrestore(&priv->lock, flags);
  231. ch341_set_handshake(port->serial->dev, 0);
  232. }
  233. }
  234. wake_up_interruptible(&priv->delta_msr_wait);
  235. }
  236. /* open this device, set default parameters */
  237. static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
  238. struct file *filp)
  239. {
  240. struct usb_serial *serial = port->serial;
  241. struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
  242. int r;
  243. dbg("ch341_open()");
  244. priv->baud_rate = DEFAULT_BAUD_RATE;
  245. priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
  246. r = ch341_configure(serial->dev, priv);
  247. if (r)
  248. goto out;
  249. r = ch341_set_handshake(serial->dev, priv->line_control);
  250. if (r)
  251. goto out;
  252. r = ch341_set_baudrate(serial->dev, priv);
  253. if (r)
  254. goto out;
  255. dbg("%s - submitting interrupt urb", __func__);
  256. port->interrupt_in_urb->dev = serial->dev;
  257. r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  258. if (r) {
  259. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  260. " error %d\n", __func__, r);
  261. ch341_close(tty, port, NULL);
  262. return -EPROTO;
  263. }
  264. r = usb_serial_generic_open(tty, port, filp);
  265. out: return r;
  266. }
  267. /* Old_termios contains the original termios settings and
  268. * tty->termios contains the new setting to be used.
  269. */
  270. static void ch341_set_termios(struct tty_struct *tty,
  271. struct usb_serial_port *port, struct ktermios *old_termios)
  272. {
  273. struct ch341_private *priv = usb_get_serial_port_data(port);
  274. unsigned baud_rate;
  275. unsigned long flags;
  276. dbg("ch341_set_termios()");
  277. if (!tty || !tty->termios)
  278. return;
  279. baud_rate = tty_get_baud_rate(tty);
  280. priv->baud_rate = baud_rate;
  281. if (baud_rate) {
  282. spin_lock_irqsave(&priv->lock, flags);
  283. priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
  284. spin_unlock_irqrestore(&priv->lock, flags);
  285. ch341_set_baudrate(port->serial->dev, priv);
  286. } else {
  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. }
  291. ch341_set_handshake(port->serial->dev, priv->line_control);
  292. /* Unimplemented:
  293. * (cflag & CSIZE) : data bits [5, 8]
  294. * (cflag & PARENB) : parity {NONE, EVEN, ODD}
  295. * (cflag & CSTOPB) : stop bits [1, 2]
  296. */
  297. }
  298. static int ch341_tiocmset(struct tty_struct *tty, struct file *file,
  299. unsigned int set, unsigned int clear)
  300. {
  301. struct usb_serial_port *port = tty->driver_data;
  302. struct ch341_private *priv = usb_get_serial_port_data(port);
  303. unsigned long flags;
  304. u8 control;
  305. spin_lock_irqsave(&priv->lock, flags);
  306. if (set & TIOCM_RTS)
  307. priv->line_control |= CH341_BIT_RTS;
  308. if (set & TIOCM_DTR)
  309. priv->line_control |= CH341_BIT_DTR;
  310. if (clear & TIOCM_RTS)
  311. priv->line_control &= ~CH341_BIT_RTS;
  312. if (clear & TIOCM_DTR)
  313. priv->line_control &= ~CH341_BIT_DTR;
  314. control = priv->line_control;
  315. spin_unlock_irqrestore(&priv->lock, flags);
  316. return ch341_set_handshake(port->serial->dev, control);
  317. }
  318. static void ch341_read_int_callback(struct urb *urb)
  319. {
  320. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  321. unsigned char *data = urb->transfer_buffer;
  322. unsigned int actual_length = urb->actual_length;
  323. int status;
  324. dbg("%s (%d)", __func__, port->number);
  325. switch (urb->status) {
  326. case 0:
  327. /* success */
  328. break;
  329. case -ECONNRESET:
  330. case -ENOENT:
  331. case -ESHUTDOWN:
  332. /* this urb is terminated, clean up */
  333. dbg("%s - urb shutting down with status: %d", __func__,
  334. urb->status);
  335. return;
  336. default:
  337. dbg("%s - nonzero urb status received: %d", __func__,
  338. urb->status);
  339. goto exit;
  340. }
  341. usb_serial_debug_data(debug, &port->dev, __func__,
  342. urb->actual_length, urb->transfer_buffer);
  343. if (actual_length >= 4) {
  344. struct ch341_private *priv = usb_get_serial_port_data(port);
  345. unsigned long flags;
  346. spin_lock_irqsave(&priv->lock, flags);
  347. priv->line_status = (~(data[2])) & CH341_BITS_MODEM_STAT;
  348. if ((data[1] & CH341_MULT_STAT))
  349. priv->multi_status_change = 1;
  350. spin_unlock_irqrestore(&priv->lock, flags);
  351. wake_up_interruptible(&priv->delta_msr_wait);
  352. }
  353. exit:
  354. status = usb_submit_urb(urb, GFP_ATOMIC);
  355. if (status)
  356. dev_err(&urb->dev->dev,
  357. "%s - usb_submit_urb failed with result %d\n",
  358. __func__, status);
  359. }
  360. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  361. {
  362. struct ch341_private *priv = usb_get_serial_port_data(port);
  363. unsigned long flags;
  364. u8 prevstatus;
  365. u8 status;
  366. u8 changed;
  367. u8 multi_change = 0;
  368. spin_lock_irqsave(&priv->lock, flags);
  369. prevstatus = priv->line_status;
  370. priv->multi_status_change = 0;
  371. spin_unlock_irqrestore(&priv->lock, flags);
  372. while (!multi_change) {
  373. interruptible_sleep_on(&priv->delta_msr_wait);
  374. /* see if a signal did it */
  375. if (signal_pending(current))
  376. return -ERESTARTSYS;
  377. spin_lock_irqsave(&priv->lock, flags);
  378. status = priv->line_status;
  379. multi_change = priv->multi_status_change;
  380. spin_unlock_irqrestore(&priv->lock, flags);
  381. changed = prevstatus ^ status;
  382. if (((arg & TIOCM_RNG) && (changed & CH341_BIT_RI)) ||
  383. ((arg & TIOCM_DSR) && (changed & CH341_BIT_DSR)) ||
  384. ((arg & TIOCM_CD) && (changed & CH341_BIT_DCD)) ||
  385. ((arg & TIOCM_CTS) && (changed & CH341_BIT_CTS))) {
  386. return 0;
  387. }
  388. prevstatus = status;
  389. }
  390. return 0;
  391. }
  392. /*static int ch341_ioctl(struct usb_serial_port *port, struct file *file,*/
  393. static int ch341_ioctl(struct tty_struct *tty, struct file *file,
  394. unsigned int cmd, unsigned long arg)
  395. {
  396. struct usb_serial_port *port = tty->driver_data;
  397. dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
  398. switch (cmd) {
  399. case TIOCMIWAIT:
  400. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  401. return wait_modem_info(port, arg);
  402. default:
  403. dbg("%s not supported = 0x%04x", __func__, cmd);
  404. break;
  405. }
  406. return -ENOIOCTLCMD;
  407. }
  408. static int ch341_tiocmget(struct tty_struct *tty, struct file *file)
  409. {
  410. struct usb_serial_port *port = tty->driver_data;
  411. struct ch341_private *priv = usb_get_serial_port_data(port);
  412. unsigned long flags;
  413. u8 mcr;
  414. u8 status;
  415. unsigned int result;
  416. dbg("%s (%d)", __func__, port->number);
  417. spin_lock_irqsave(&priv->lock, flags);
  418. mcr = priv->line_control;
  419. status = priv->line_status;
  420. spin_unlock_irqrestore(&priv->lock, flags);
  421. result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
  422. | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
  423. | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
  424. | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
  425. | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
  426. | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
  427. dbg("%s - result = %x", __func__, result);
  428. return result;
  429. }
  430. static int ch341_reset_resume(struct usb_interface *intf)
  431. {
  432. struct usb_device *dev = interface_to_usbdev(intf);
  433. struct usb_serial *serial = NULL;
  434. struct ch341_private *priv;
  435. serial = usb_get_intfdata(intf);
  436. priv = usb_get_serial_port_data(serial->port[0]);
  437. /*reconfigure ch341 serial port after bus-reset*/
  438. ch341_configure(dev, priv);
  439. usb_serial_resume(intf);
  440. return 0;
  441. }
  442. static struct usb_driver ch341_driver = {
  443. .name = "ch341",
  444. .probe = usb_serial_probe,
  445. .disconnect = usb_serial_disconnect,
  446. .suspend = usb_serial_suspend,
  447. .resume = usb_serial_resume,
  448. .reset_resume = ch341_reset_resume,
  449. .id_table = id_table,
  450. .no_dynamic_id = 1,
  451. .supports_autosuspend = 1,
  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. .usb_driver = &ch341_driver,
  460. .num_ports = 1,
  461. .open = ch341_open,
  462. .close = ch341_close,
  463. .ioctl = ch341_ioctl,
  464. .set_termios = ch341_set_termios,
  465. .tiocmget = ch341_tiocmget,
  466. .tiocmset = ch341_tiocmset,
  467. .read_int_callback = ch341_read_int_callback,
  468. .attach = ch341_attach,
  469. };
  470. static int __init ch341_init(void)
  471. {
  472. int retval;
  473. retval = usb_serial_register(&ch341_device);
  474. if (retval)
  475. return retval;
  476. retval = usb_register(&ch341_driver);
  477. if (retval)
  478. usb_serial_deregister(&ch341_device);
  479. return retval;
  480. }
  481. static void __exit ch341_exit(void)
  482. {
  483. usb_deregister(&ch341_driver);
  484. usb_serial_deregister(&ch341_device);
  485. }
  486. module_init(ch341_init);
  487. module_exit(ch341_exit);
  488. MODULE_LICENSE("GPL");
  489. module_param(debug, bool, S_IRUGO | S_IWUSR);
  490. MODULE_PARM_DESC(debug, "Debug enabled or not");
  491. /* EOF ch341.c */