ch341.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 int ch341_carrier_raised(struct usb_serial_port *port)
  213. {
  214. struct ch341_private *priv = usb_get_serial_port_data(port);
  215. if (priv->line_status & CH341_BIT_DCD)
  216. return 1;
  217. return 0;
  218. }
  219. static void ch341_dtr_rts(struct usb_serial_port *port, int on)
  220. {
  221. struct ch341_private *priv = usb_get_serial_port_data(port);
  222. unsigned long flags;
  223. dbg("%s - port %d", __func__, port->number);
  224. /* drop DTR and RTS */
  225. spin_lock_irqsave(&priv->lock, flags);
  226. if (on)
  227. priv->line_control |= CH341_BIT_RTS | CH341_BIT_DTR;
  228. else
  229. priv->line_control &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
  230. spin_unlock_irqrestore(&priv->lock, flags);
  231. ch341_set_handshake(port->serial->dev, priv->line_control);
  232. wake_up_interruptible(&priv->delta_msr_wait);
  233. }
  234. static void ch341_close(struct usb_serial_port *port)
  235. {
  236. dbg("%s - port %d", __func__, port->number);
  237. /* shutdown our urbs */
  238. dbg("%s - shutting down urbs", __func__);
  239. usb_kill_urb(port->write_urb);
  240. usb_kill_urb(port->read_urb);
  241. usb_kill_urb(port->interrupt_in_urb);
  242. }
  243. /* open this device, set default parameters */
  244. static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
  245. struct file *filp)
  246. {
  247. struct usb_serial *serial = port->serial;
  248. struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
  249. int r;
  250. dbg("ch341_open()");
  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. port->interrupt_in_urb->dev = serial->dev;
  263. r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  264. if (r) {
  265. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  266. " error %d\n", __func__, r);
  267. ch341_close(port);
  268. return -EPROTO;
  269. }
  270. r = usb_serial_generic_open(tty, port, filp);
  271. out: return r;
  272. }
  273. /* Old_termios contains the original termios settings and
  274. * tty->termios contains the new setting to be used.
  275. */
  276. static void ch341_set_termios(struct tty_struct *tty,
  277. struct usb_serial_port *port, struct ktermios *old_termios)
  278. {
  279. struct ch341_private *priv = usb_get_serial_port_data(port);
  280. unsigned baud_rate;
  281. unsigned long flags;
  282. dbg("ch341_set_termios()");
  283. baud_rate = tty_get_baud_rate(tty);
  284. priv->baud_rate = baud_rate;
  285. if (baud_rate) {
  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. ch341_set_baudrate(port->serial->dev, priv);
  290. } else {
  291. spin_lock_irqsave(&priv->lock, flags);
  292. priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
  293. spin_unlock_irqrestore(&priv->lock, flags);
  294. }
  295. ch341_set_handshake(port->serial->dev, priv->line_control);
  296. /* Unimplemented:
  297. * (cflag & CSIZE) : data bits [5, 8]
  298. * (cflag & PARENB) : parity {NONE, EVEN, ODD}
  299. * (cflag & CSTOPB) : stop bits [1, 2]
  300. */
  301. }
  302. static int ch341_tiocmset(struct tty_struct *tty, struct file *file,
  303. unsigned int set, unsigned int clear)
  304. {
  305. struct usb_serial_port *port = tty->driver_data;
  306. struct ch341_private *priv = usb_get_serial_port_data(port);
  307. unsigned long flags;
  308. u8 control;
  309. spin_lock_irqsave(&priv->lock, flags);
  310. if (set & TIOCM_RTS)
  311. priv->line_control |= CH341_BIT_RTS;
  312. if (set & TIOCM_DTR)
  313. priv->line_control |= CH341_BIT_DTR;
  314. if (clear & TIOCM_RTS)
  315. priv->line_control &= ~CH341_BIT_RTS;
  316. if (clear & TIOCM_DTR)
  317. priv->line_control &= ~CH341_BIT_DTR;
  318. control = priv->line_control;
  319. spin_unlock_irqrestore(&priv->lock, flags);
  320. return ch341_set_handshake(port->serial->dev, control);
  321. }
  322. static void ch341_read_int_callback(struct urb *urb)
  323. {
  324. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  325. unsigned char *data = urb->transfer_buffer;
  326. unsigned int actual_length = urb->actual_length;
  327. int status;
  328. dbg("%s (%d)", __func__, port->number);
  329. switch (urb->status) {
  330. case 0:
  331. /* success */
  332. break;
  333. case -ECONNRESET:
  334. case -ENOENT:
  335. case -ESHUTDOWN:
  336. /* this urb is terminated, clean up */
  337. dbg("%s - urb shutting down with status: %d", __func__,
  338. urb->status);
  339. return;
  340. default:
  341. dbg("%s - nonzero urb status received: %d", __func__,
  342. urb->status);
  343. goto exit;
  344. }
  345. usb_serial_debug_data(debug, &port->dev, __func__,
  346. urb->actual_length, urb->transfer_buffer);
  347. if (actual_length >= 4) {
  348. struct ch341_private *priv = usb_get_serial_port_data(port);
  349. unsigned long flags;
  350. spin_lock_irqsave(&priv->lock, flags);
  351. priv->line_status = (~(data[2])) & CH341_BITS_MODEM_STAT;
  352. if ((data[1] & CH341_MULT_STAT))
  353. priv->multi_status_change = 1;
  354. spin_unlock_irqrestore(&priv->lock, flags);
  355. wake_up_interruptible(&priv->delta_msr_wait);
  356. }
  357. exit:
  358. status = usb_submit_urb(urb, GFP_ATOMIC);
  359. if (status)
  360. dev_err(&urb->dev->dev,
  361. "%s - usb_submit_urb failed with result %d\n",
  362. __func__, status);
  363. }
  364. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  365. {
  366. struct ch341_private *priv = usb_get_serial_port_data(port);
  367. unsigned long flags;
  368. u8 prevstatus;
  369. u8 status;
  370. u8 changed;
  371. u8 multi_change = 0;
  372. spin_lock_irqsave(&priv->lock, flags);
  373. prevstatus = priv->line_status;
  374. priv->multi_status_change = 0;
  375. spin_unlock_irqrestore(&priv->lock, flags);
  376. while (!multi_change) {
  377. interruptible_sleep_on(&priv->delta_msr_wait);
  378. /* see if a signal did it */
  379. if (signal_pending(current))
  380. return -ERESTARTSYS;
  381. spin_lock_irqsave(&priv->lock, flags);
  382. status = priv->line_status;
  383. multi_change = priv->multi_status_change;
  384. spin_unlock_irqrestore(&priv->lock, flags);
  385. changed = prevstatus ^ status;
  386. if (((arg & TIOCM_RNG) && (changed & CH341_BIT_RI)) ||
  387. ((arg & TIOCM_DSR) && (changed & CH341_BIT_DSR)) ||
  388. ((arg & TIOCM_CD) && (changed & CH341_BIT_DCD)) ||
  389. ((arg & TIOCM_CTS) && (changed & CH341_BIT_CTS))) {
  390. return 0;
  391. }
  392. prevstatus = status;
  393. }
  394. return 0;
  395. }
  396. /*static int ch341_ioctl(struct usb_serial_port *port, struct file *file,*/
  397. static int ch341_ioctl(struct tty_struct *tty, struct file *file,
  398. unsigned int cmd, unsigned long arg)
  399. {
  400. struct usb_serial_port *port = tty->driver_data;
  401. dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
  402. switch (cmd) {
  403. case TIOCMIWAIT:
  404. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  405. return wait_modem_info(port, arg);
  406. default:
  407. dbg("%s not supported = 0x%04x", __func__, cmd);
  408. break;
  409. }
  410. return -ENOIOCTLCMD;
  411. }
  412. static int ch341_tiocmget(struct tty_struct *tty, struct file *file)
  413. {
  414. struct usb_serial_port *port = tty->driver_data;
  415. struct ch341_private *priv = usb_get_serial_port_data(port);
  416. unsigned long flags;
  417. u8 mcr;
  418. u8 status;
  419. unsigned int result;
  420. dbg("%s (%d)", __func__, port->number);
  421. spin_lock_irqsave(&priv->lock, flags);
  422. mcr = priv->line_control;
  423. status = priv->line_status;
  424. spin_unlock_irqrestore(&priv->lock, flags);
  425. result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
  426. | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
  427. | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
  428. | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
  429. | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
  430. | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
  431. dbg("%s - result = %x", __func__, result);
  432. return result;
  433. }
  434. static int ch341_reset_resume(struct usb_interface *intf)
  435. {
  436. struct usb_device *dev = interface_to_usbdev(intf);
  437. struct usb_serial *serial = NULL;
  438. struct ch341_private *priv;
  439. serial = usb_get_intfdata(intf);
  440. priv = usb_get_serial_port_data(serial->port[0]);
  441. /*reconfigure ch341 serial port after bus-reset*/
  442. ch341_configure(dev, priv);
  443. usb_serial_resume(intf);
  444. return 0;
  445. }
  446. static struct usb_driver ch341_driver = {
  447. .name = "ch341",
  448. .probe = usb_serial_probe,
  449. .disconnect = usb_serial_disconnect,
  450. .suspend = usb_serial_suspend,
  451. .resume = usb_serial_resume,
  452. .reset_resume = ch341_reset_resume,
  453. .id_table = id_table,
  454. .no_dynamic_id = 1,
  455. .supports_autosuspend = 1,
  456. };
  457. static struct usb_serial_driver ch341_device = {
  458. .driver = {
  459. .owner = THIS_MODULE,
  460. .name = "ch341-uart",
  461. },
  462. .id_table = id_table,
  463. .usb_driver = &ch341_driver,
  464. .num_ports = 1,
  465. .open = ch341_open,
  466. .dtr_rts = ch341_dtr_rts,
  467. .carrier_raised = ch341_carrier_raised,
  468. .close = ch341_close,
  469. .ioctl = ch341_ioctl,
  470. .set_termios = ch341_set_termios,
  471. .tiocmget = ch341_tiocmget,
  472. .tiocmset = ch341_tiocmset,
  473. .read_int_callback = ch341_read_int_callback,
  474. .attach = ch341_attach,
  475. };
  476. static int __init ch341_init(void)
  477. {
  478. int retval;
  479. retval = usb_serial_register(&ch341_device);
  480. if (retval)
  481. return retval;
  482. retval = usb_register(&ch341_driver);
  483. if (retval)
  484. usb_serial_deregister(&ch341_device);
  485. return retval;
  486. }
  487. static void __exit ch341_exit(void)
  488. {
  489. usb_deregister(&ch341_driver);
  490. usb_serial_deregister(&ch341_device);
  491. }
  492. module_init(ch341_init);
  493. module_exit(ch341_exit);
  494. MODULE_LICENSE("GPL");
  495. module_param(debug, bool, S_IRUGO | S_IWUSR);
  496. MODULE_PARM_DESC(debug, "Debug enabled or not");
  497. /* EOF ch341.c */