ch341.c 15 KB

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