ch341.c 7.3 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. { },
  28. };
  29. MODULE_DEVICE_TABLE(usb, id_table);
  30. struct ch341_private {
  31. unsigned baud_rate;
  32. u8 dtr;
  33. u8 rts;
  34. };
  35. static int ch341_control_out(struct usb_device *dev, u8 request,
  36. u16 value, u16 index)
  37. {
  38. int r;
  39. dbg("ch341_control_out(%02x,%02x,%04x,%04x)", USB_DIR_OUT|0x40,
  40. (int)request, (int)value, (int)index);
  41. r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
  42. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  43. value, index, NULL, 0, DEFAULT_TIMEOUT);
  44. return r;
  45. }
  46. static int ch341_control_in(struct usb_device *dev,
  47. u8 request, u16 value, u16 index,
  48. char *buf, unsigned bufsize)
  49. {
  50. int r;
  51. dbg("ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)", USB_DIR_IN|0x40,
  52. (int)request, (int)value, (int)index, buf, (int)bufsize);
  53. r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
  54. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  55. value, index, buf, bufsize, DEFAULT_TIMEOUT);
  56. return r;
  57. }
  58. static int ch341_set_baudrate(struct usb_device *dev,
  59. struct ch341_private *priv)
  60. {
  61. short a, b;
  62. int r;
  63. dbg("ch341_set_baudrate(%d)", priv->baud_rate);
  64. switch (priv->baud_rate) {
  65. case 2400:
  66. a = 0xd901;
  67. b = 0x0038;
  68. break;
  69. case 4800:
  70. a = 0x6402;
  71. b = 0x001f;
  72. break;
  73. case 9600:
  74. a = 0xb202;
  75. b = 0x0013;
  76. break;
  77. case 19200:
  78. a = 0xd902;
  79. b = 0x000d;
  80. break;
  81. case 38400:
  82. a = 0x6403;
  83. b = 0x000a;
  84. break;
  85. case 115200:
  86. a = 0xcc03;
  87. b = 0x0008;
  88. break;
  89. default:
  90. return -EINVAL;
  91. }
  92. r = ch341_control_out(dev, 0x9a, 0x1312, a);
  93. if (!r)
  94. r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
  95. return r;
  96. }
  97. static int ch341_set_handshake(struct usb_device *dev,
  98. struct ch341_private *priv)
  99. {
  100. dbg("ch341_set_handshake(%d,%d)", priv->dtr, priv->rts);
  101. return ch341_control_out(dev, 0xa4,
  102. ~((priv->dtr?1<<5:0)|(priv->rts?1<<6:0)), 0);
  103. }
  104. static int ch341_get_status(struct usb_device *dev)
  105. {
  106. char *buffer;
  107. int r;
  108. const unsigned size = 8;
  109. dbg("ch341_get_status()");
  110. buffer = kmalloc(size, GFP_KERNEL);
  111. if (!buffer)
  112. return -ENOMEM;
  113. r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
  114. if ( r < 0)
  115. goto out;
  116. /* Not having the datasheet for the CH341, we ignore the bytes returned
  117. * from the device. Return error if the device did not respond in time.
  118. */
  119. r = 0;
  120. out: kfree(buffer);
  121. return r;
  122. }
  123. /* -------------------------------------------------------------------------- */
  124. static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
  125. {
  126. char *buffer;
  127. int r;
  128. const unsigned size = 8;
  129. dbg("ch341_configure()");
  130. buffer = kmalloc(size, GFP_KERNEL);
  131. if (!buffer)
  132. return -ENOMEM;
  133. /* expect two bytes 0x27 0x00 */
  134. r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
  135. if (r < 0)
  136. goto out;
  137. r = ch341_control_out(dev, 0xa1, 0, 0);
  138. if (r < 0)
  139. goto out;
  140. r = ch341_set_baudrate(dev, priv);
  141. if (r < 0)
  142. goto out;
  143. /* expect two bytes 0x56 0x00 */
  144. r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
  145. if (r < 0)
  146. goto out;
  147. r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
  148. if (r < 0)
  149. goto out;
  150. /* expect 0xff 0xee */
  151. r = ch341_get_status(dev);
  152. if (r < 0)
  153. goto out;
  154. r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
  155. if (r < 0)
  156. goto out;
  157. r = ch341_set_baudrate(dev, priv);
  158. if (r < 0)
  159. goto out;
  160. r = ch341_set_handshake(dev, priv);
  161. if (r < 0)
  162. goto out;
  163. /* expect 0x9f 0xee */
  164. r = ch341_get_status(dev);
  165. out: kfree(buffer);
  166. return r;
  167. }
  168. /* allocate private data */
  169. static int ch341_attach(struct usb_serial *serial)
  170. {
  171. struct ch341_private *priv;
  172. int r;
  173. dbg("ch341_attach()");
  174. /* private data */
  175. priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
  176. if (!priv)
  177. return -ENOMEM;
  178. priv->baud_rate = DEFAULT_BAUD_RATE;
  179. priv->dtr = 1;
  180. priv->rts = 1;
  181. r = ch341_configure(serial->dev, priv);
  182. if (r < 0)
  183. goto error;
  184. usb_set_serial_port_data(serial->port[0], priv);
  185. return 0;
  186. error: kfree(priv);
  187. return r;
  188. }
  189. /* open this device, set default parameters */
  190. static int ch341_open(struct usb_serial_port *port, struct file *filp)
  191. {
  192. struct usb_serial *serial = port->serial;
  193. struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
  194. int r;
  195. dbg("ch341_open()");
  196. priv->baud_rate = DEFAULT_BAUD_RATE;
  197. priv->dtr = 1;
  198. priv->rts = 1;
  199. r = ch341_configure(serial->dev, priv);
  200. if (r)
  201. goto out;
  202. r = ch341_set_handshake(serial->dev, priv);
  203. if (r)
  204. goto out;
  205. r = ch341_set_baudrate(serial->dev, priv);
  206. if (r)
  207. goto out;
  208. r = usb_serial_generic_open(port, filp);
  209. out: return r;
  210. }
  211. /* Old_termios contains the original termios settings and
  212. * tty->termios contains the new setting to be used.
  213. */
  214. static void ch341_set_termios(struct usb_serial_port *port,
  215. struct ktermios *old_termios)
  216. {
  217. struct ch341_private *priv = usb_get_serial_port_data(port);
  218. struct tty_struct *tty = port->tty;
  219. unsigned baud_rate;
  220. dbg("ch341_set_termios()");
  221. if (!tty || !tty->termios)
  222. return;
  223. baud_rate = tty_get_baud_rate(tty);
  224. switch (baud_rate) {
  225. case 2400:
  226. case 4800:
  227. case 9600:
  228. case 19200:
  229. case 38400:
  230. case 115200:
  231. priv->baud_rate = baud_rate;
  232. break;
  233. default:
  234. dbg("Rate %d not supported, using %d",
  235. baud_rate, DEFAULT_BAUD_RATE);
  236. priv->baud_rate = DEFAULT_BAUD_RATE;
  237. }
  238. ch341_set_baudrate(port->serial->dev, priv);
  239. /* Unimplemented:
  240. * (cflag & CSIZE) : data bits [5, 8]
  241. * (cflag & PARENB) : parity {NONE, EVEN, ODD}
  242. * (cflag & CSTOPB) : stop bits [1, 2]
  243. */
  244. }
  245. static struct usb_driver ch341_driver = {
  246. .name = "ch341",
  247. .probe = usb_serial_probe,
  248. .disconnect = usb_serial_disconnect,
  249. .id_table = id_table,
  250. .no_dynamic_id = 1,
  251. };
  252. static struct usb_serial_driver ch341_device = {
  253. .driver = {
  254. .owner = THIS_MODULE,
  255. .name = "ch341-uart",
  256. },
  257. .id_table = id_table,
  258. .usb_driver = &ch341_driver,
  259. .num_interrupt_in = NUM_DONT_CARE,
  260. .num_bulk_in = 1,
  261. .num_bulk_out = 1,
  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 */