ch341.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk>
  3. *
  4. * ch341.c implements a serial port driver for the Winchiphead CH341.
  5. *
  6. * The CH341 device can be used to implement an RS232 asynchronous
  7. * serial port, an IEEE-1284 parallel printer port or a memory-like
  8. * interface. In all cases the CH341 supports an I2C interface as well.
  9. * This driver only supports the asynchronous serial interface.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License version
  13. * 2 as published by the Free Software Foundation.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/tty.h>
  18. #include <linux/module.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/serial.h>
  21. #include <linux/serial.h>
  22. #define DEFAULT_BAUD_RATE 2400
  23. #define DEFAULT_TIMEOUT 1000
  24. static int debug;
  25. static struct usb_device_id id_table [] = {
  26. { USB_DEVICE(0x4348, 0x5523) },
  27. { USB_DEVICE(0x1a86, 0x7523) },
  28. { },
  29. };
  30. MODULE_DEVICE_TABLE(usb, id_table);
  31. struct ch341_private {
  32. unsigned baud_rate;
  33. u8 dtr;
  34. u8 rts;
  35. };
  36. static int ch341_control_out(struct usb_device *dev, u8 request,
  37. u16 value, u16 index)
  38. {
  39. int r;
  40. dbg("ch341_control_out(%02x,%02x,%04x,%04x)", USB_DIR_OUT|0x40,
  41. (int)request, (int)value, (int)index);
  42. r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
  43. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  44. value, index, NULL, 0, DEFAULT_TIMEOUT);
  45. return r;
  46. }
  47. static int ch341_control_in(struct usb_device *dev,
  48. u8 request, u16 value, u16 index,
  49. char *buf, unsigned bufsize)
  50. {
  51. int r;
  52. dbg("ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)", USB_DIR_IN|0x40,
  53. (int)request, (int)value, (int)index, buf, (int)bufsize);
  54. r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
  55. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  56. value, index, buf, bufsize, DEFAULT_TIMEOUT);
  57. return r;
  58. }
  59. static int ch341_set_baudrate(struct usb_device *dev,
  60. struct ch341_private *priv)
  61. {
  62. short a, b;
  63. int r;
  64. dbg("ch341_set_baudrate(%d)", priv->baud_rate);
  65. switch (priv->baud_rate) {
  66. case 2400:
  67. a = 0xd901;
  68. b = 0x0038;
  69. break;
  70. case 4800:
  71. a = 0x6402;
  72. b = 0x001f;
  73. break;
  74. case 9600:
  75. a = 0xb202;
  76. b = 0x0013;
  77. break;
  78. case 19200:
  79. a = 0xd902;
  80. b = 0x000d;
  81. break;
  82. case 38400:
  83. a = 0x6403;
  84. b = 0x000a;
  85. break;
  86. case 115200:
  87. a = 0xcc03;
  88. b = 0x0008;
  89. break;
  90. default:
  91. return -EINVAL;
  92. }
  93. r = ch341_control_out(dev, 0x9a, 0x1312, a);
  94. if (!r)
  95. r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
  96. return r;
  97. }
  98. static int ch341_set_handshake(struct usb_device *dev,
  99. struct ch341_private *priv)
  100. {
  101. dbg("ch341_set_handshake(%d,%d)", priv->dtr, priv->rts);
  102. return ch341_control_out(dev, 0xa4,
  103. ~((priv->dtr?1<<5:0)|(priv->rts?1<<6:0)), 0);
  104. }
  105. static int ch341_get_status(struct usb_device *dev)
  106. {
  107. char *buffer;
  108. int r;
  109. const unsigned size = 8;
  110. dbg("ch341_get_status()");
  111. buffer = kmalloc(size, GFP_KERNEL);
  112. if (!buffer)
  113. return -ENOMEM;
  114. r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
  115. if (r < 0)
  116. goto out;
  117. /* Not having the datasheet for the CH341, we ignore the bytes returned
  118. * from the device. Return error if the device did not respond in time.
  119. */
  120. r = 0;
  121. out: kfree(buffer);
  122. return r;
  123. }
  124. /* -------------------------------------------------------------------------- */
  125. static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
  126. {
  127. char *buffer;
  128. int r;
  129. const unsigned size = 8;
  130. dbg("ch341_configure()");
  131. buffer = kmalloc(size, GFP_KERNEL);
  132. if (!buffer)
  133. return -ENOMEM;
  134. /* expect two bytes 0x27 0x00 */
  135. r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
  136. if (r < 0)
  137. goto out;
  138. r = ch341_control_out(dev, 0xa1, 0, 0);
  139. if (r < 0)
  140. goto out;
  141. r = ch341_set_baudrate(dev, priv);
  142. if (r < 0)
  143. goto out;
  144. /* expect two bytes 0x56 0x00 */
  145. r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
  146. if (r < 0)
  147. goto out;
  148. r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
  149. if (r < 0)
  150. goto out;
  151. /* expect 0xff 0xee */
  152. r = ch341_get_status(dev);
  153. if (r < 0)
  154. goto out;
  155. r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
  156. if (r < 0)
  157. goto out;
  158. r = ch341_set_baudrate(dev, priv);
  159. if (r < 0)
  160. goto out;
  161. r = ch341_set_handshake(dev, priv);
  162. if (r < 0)
  163. goto out;
  164. /* expect 0x9f 0xee */
  165. r = ch341_get_status(dev);
  166. out: kfree(buffer);
  167. return r;
  168. }
  169. /* allocate private data */
  170. static int ch341_attach(struct usb_serial *serial)
  171. {
  172. struct ch341_private *priv;
  173. int r;
  174. dbg("ch341_attach()");
  175. /* private data */
  176. priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
  177. if (!priv)
  178. return -ENOMEM;
  179. priv->baud_rate = DEFAULT_BAUD_RATE;
  180. priv->dtr = 1;
  181. priv->rts = 1;
  182. r = ch341_configure(serial->dev, priv);
  183. if (r < 0)
  184. goto error;
  185. usb_set_serial_port_data(serial->port[0], priv);
  186. return 0;
  187. error: kfree(priv);
  188. return r;
  189. }
  190. /* open this device, set default parameters */
  191. static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
  192. struct file *filp)
  193. {
  194. struct usb_serial *serial = port->serial;
  195. struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
  196. int r;
  197. dbg("ch341_open()");
  198. priv->baud_rate = DEFAULT_BAUD_RATE;
  199. priv->dtr = 1;
  200. priv->rts = 1;
  201. r = ch341_configure(serial->dev, priv);
  202. if (r)
  203. goto out;
  204. r = ch341_set_handshake(serial->dev, priv);
  205. if (r)
  206. goto out;
  207. r = ch341_set_baudrate(serial->dev, priv);
  208. if (r)
  209. goto out;
  210. r = usb_serial_generic_open(tty, port, filp);
  211. out: return r;
  212. }
  213. /* Old_termios contains the original termios settings and
  214. * tty->termios contains the new setting to be used.
  215. */
  216. static void ch341_set_termios(struct tty_struct *tty,
  217. struct usb_serial_port *port, struct ktermios *old_termios)
  218. {
  219. struct ch341_private *priv = usb_get_serial_port_data(port);
  220. unsigned baud_rate;
  221. dbg("ch341_set_termios()");
  222. baud_rate = tty_get_baud_rate(tty);
  223. switch (baud_rate) {
  224. case 2400:
  225. case 4800:
  226. case 9600:
  227. case 19200:
  228. case 38400:
  229. case 115200:
  230. priv->baud_rate = baud_rate;
  231. break;
  232. default:
  233. dbg("Rate %d not supported, using %d",
  234. baud_rate, DEFAULT_BAUD_RATE);
  235. priv->baud_rate = DEFAULT_BAUD_RATE;
  236. }
  237. ch341_set_baudrate(port->serial->dev, priv);
  238. /* Unimplemented:
  239. * (cflag & CSIZE) : data bits [5, 8]
  240. * (cflag & PARENB) : parity {NONE, EVEN, ODD}
  241. * (cflag & CSTOPB) : stop bits [1, 2]
  242. */
  243. /* Copy back the old hardware settings */
  244. tty_termios_copy_hw(tty->termios, old_termios);
  245. /* And re-encode with the new baud */
  246. tty_encode_baud_rate(tty, baud_rate, baud_rate);
  247. }
  248. static struct usb_driver ch341_driver = {
  249. .name = "ch341",
  250. .probe = usb_serial_probe,
  251. .disconnect = usb_serial_disconnect,
  252. .id_table = id_table,
  253. .no_dynamic_id = 1,
  254. };
  255. static struct usb_serial_driver ch341_device = {
  256. .driver = {
  257. .owner = THIS_MODULE,
  258. .name = "ch341-uart",
  259. },
  260. .id_table = id_table,
  261. .usb_driver = &ch341_driver,
  262. .num_ports = 1,
  263. .open = ch341_open,
  264. .set_termios = ch341_set_termios,
  265. .attach = ch341_attach,
  266. };
  267. static int __init ch341_init(void)
  268. {
  269. int retval;
  270. retval = usb_serial_register(&ch341_device);
  271. if (retval)
  272. return retval;
  273. retval = usb_register(&ch341_driver);
  274. if (retval)
  275. usb_serial_deregister(&ch341_device);
  276. return retval;
  277. }
  278. static void __exit ch341_exit(void)
  279. {
  280. usb_deregister(&ch341_driver);
  281. usb_serial_deregister(&ch341_device);
  282. }
  283. module_init(ch341_init);
  284. module_exit(ch341_exit);
  285. MODULE_LICENSE("GPL");
  286. module_param(debug, bool, S_IRUGO | S_IWUSR);
  287. MODULE_PARM_DESC(debug, "Debug enabled or not");
  288. /* EOF ch341.c */