upd78f0730.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Renesas Electronics uPD78F0730 USB to serial converter driver
  3. *
  4. * Copyright (C) 2014,2016 Maksim Salau <maksim.salau@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * Protocol of the adaptor is described in the application note U19660EJ1V0AN00
  11. * μPD78F0730 8-bit Single-Chip Microcontroller
  12. * USB-to-Serial Conversion Software
  13. * <https://www.renesas.com/en-eu/doc/DocumentServer/026/U19660EJ1V0AN00.pdf>
  14. *
  15. * The adaptor functionality is limited to the following:
  16. * - data bits: 7 or 8
  17. * - stop bits: 1 or 2
  18. * - parity: even, odd or none
  19. * - flow control: none
  20. * - baud rates: 0, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 153600
  21. * - signals: DTR, RTS and BREAK
  22. */
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/tty.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/serial.h>
  28. #define DRIVER_DESC "Renesas uPD78F0730 USB to serial converter driver"
  29. #define DRIVER_AUTHOR "Maksim Salau <maksim.salau@gmail.com>"
  30. static const struct usb_device_id id_table[] = {
  31. { USB_DEVICE(0x0409, 0x0063) }, /* V850ESJX3-STICK */
  32. { USB_DEVICE(0x045B, 0x0212) }, /* YRPBRL78G13, YRPBRL78G14 */
  33. { USB_DEVICE(0x064B, 0x7825) }, /* Analog Devices EVAL-ADXL362Z-DB */
  34. {}
  35. };
  36. MODULE_DEVICE_TABLE(usb, id_table);
  37. /*
  38. * Each adaptor is associated with a private structure, that holds the current
  39. * state of control signals (DTR, RTS and BREAK).
  40. */
  41. struct upd78f0730_port_private {
  42. struct mutex lock; /* mutex to protect line_signals */
  43. u8 line_signals;
  44. };
  45. /* Op-codes of control commands */
  46. #define UPD78F0730_CMD_LINE_CONTROL 0x00
  47. #define UPD78F0730_CMD_SET_DTR_RTS 0x01
  48. #define UPD78F0730_CMD_SET_XON_XOFF_CHR 0x02
  49. #define UPD78F0730_CMD_OPEN_CLOSE 0x03
  50. #define UPD78F0730_CMD_SET_ERR_CHR 0x04
  51. /* Data sizes in UPD78F0730_CMD_LINE_CONTROL command */
  52. #define UPD78F0730_DATA_SIZE_7_BITS 0x00
  53. #define UPD78F0730_DATA_SIZE_8_BITS 0x01
  54. #define UPD78F0730_DATA_SIZE_MASK 0x01
  55. /* Stop-bit modes in UPD78F0730_CMD_LINE_CONTROL command */
  56. #define UPD78F0730_STOP_BIT_1_BIT 0x00
  57. #define UPD78F0730_STOP_BIT_2_BIT 0x02
  58. #define UPD78F0730_STOP_BIT_MASK 0x02
  59. /* Parity modes in UPD78F0730_CMD_LINE_CONTROL command */
  60. #define UPD78F0730_PARITY_NONE 0x00
  61. #define UPD78F0730_PARITY_EVEN 0x04
  62. #define UPD78F0730_PARITY_ODD 0x08
  63. #define UPD78F0730_PARITY_MASK 0x0C
  64. /* Flow control modes in UPD78F0730_CMD_LINE_CONTROL command */
  65. #define UPD78F0730_FLOW_CONTROL_NONE 0x00
  66. #define UPD78F0730_FLOW_CONTROL_HW 0x10
  67. #define UPD78F0730_FLOW_CONTROL_SW 0x20
  68. #define UPD78F0730_FLOW_CONTROL_MASK 0x30
  69. /* Control signal bits in UPD78F0730_CMD_SET_DTR_RTS command */
  70. #define UPD78F0730_RTS 0x01
  71. #define UPD78F0730_DTR 0x02
  72. #define UPD78F0730_BREAK 0x04
  73. /* Port modes in UPD78F0730_CMD_OPEN_CLOSE command */
  74. #define UPD78F0730_PORT_CLOSE 0x00
  75. #define UPD78F0730_PORT_OPEN 0x01
  76. /* Error character substitution modes in UPD78F0730_CMD_SET_ERR_CHR command */
  77. #define UPD78F0730_ERR_CHR_DISABLED 0x00
  78. #define UPD78F0730_ERR_CHR_ENABLED 0x01
  79. /*
  80. * Declaration of command structures
  81. */
  82. /* UPD78F0730_CMD_LINE_CONTROL command */
  83. struct upd78f0730_line_control {
  84. u8 opcode;
  85. __le32 baud_rate;
  86. u8 params;
  87. } __packed;
  88. /* UPD78F0730_CMD_SET_DTR_RTS command */
  89. struct upd78f0730_set_dtr_rts {
  90. u8 opcode;
  91. u8 params;
  92. };
  93. /* UPD78F0730_CMD_SET_XON_OFF_CHR command */
  94. struct upd78f0730_set_xon_xoff_chr {
  95. u8 opcode;
  96. u8 xon;
  97. u8 xoff;
  98. };
  99. /* UPD78F0730_CMD_OPEN_CLOSE command */
  100. struct upd78f0730_open_close {
  101. u8 opcode;
  102. u8 state;
  103. };
  104. /* UPD78F0730_CMD_SET_ERR_CHR command */
  105. struct upd78f0730_set_err_chr {
  106. u8 opcode;
  107. u8 state;
  108. u8 err_char;
  109. };
  110. static int upd78f0730_send_ctl(struct usb_serial_port *port,
  111. const void *data, int size)
  112. {
  113. struct usb_device *usbdev = port->serial->dev;
  114. void *buf;
  115. int res;
  116. if (size <= 0 || !data)
  117. return -EINVAL;
  118. buf = kmemdup(data, size, GFP_KERNEL);
  119. if (!buf)
  120. return -ENOMEM;
  121. res = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x00,
  122. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  123. 0x0000, 0x0000, buf, size, USB_CTRL_SET_TIMEOUT);
  124. kfree(buf);
  125. if (res != size) {
  126. struct device *dev = &port->dev;
  127. dev_err(dev, "failed to send control request %02x: %d\n",
  128. *(u8 *)data, res);
  129. /* The maximum expected length of a transfer is 6 bytes */
  130. if (res >= 0)
  131. res = -EIO;
  132. return res;
  133. }
  134. return 0;
  135. }
  136. static int upd78f0730_port_probe(struct usb_serial_port *port)
  137. {
  138. struct upd78f0730_port_private *private;
  139. private = kzalloc(sizeof(*private), GFP_KERNEL);
  140. if (!private)
  141. return -ENOMEM;
  142. mutex_init(&private->lock);
  143. usb_set_serial_port_data(port, private);
  144. return 0;
  145. }
  146. static int upd78f0730_port_remove(struct usb_serial_port *port)
  147. {
  148. struct upd78f0730_port_private *private;
  149. private = usb_get_serial_port_data(port);
  150. mutex_destroy(&private->lock);
  151. kfree(private);
  152. return 0;
  153. }
  154. static int upd78f0730_tiocmget(struct tty_struct *tty)
  155. {
  156. struct device *dev = tty->dev;
  157. struct upd78f0730_port_private *private;
  158. struct usb_serial_port *port = tty->driver_data;
  159. int signals;
  160. int res;
  161. private = usb_get_serial_port_data(port);
  162. mutex_lock(&private->lock);
  163. signals = private->line_signals;
  164. mutex_unlock(&private->lock);
  165. res = ((signals & UPD78F0730_DTR) ? TIOCM_DTR : 0) |
  166. ((signals & UPD78F0730_RTS) ? TIOCM_RTS : 0);
  167. dev_dbg(dev, "%s - res = %x\n", __func__, res);
  168. return res;
  169. }
  170. static int upd78f0730_tiocmset(struct tty_struct *tty,
  171. unsigned int set, unsigned int clear)
  172. {
  173. struct device *dev = tty->dev;
  174. struct usb_serial_port *port = tty->driver_data;
  175. struct upd78f0730_port_private *private;
  176. struct upd78f0730_set_dtr_rts request;
  177. int res;
  178. private = usb_get_serial_port_data(port);
  179. mutex_lock(&private->lock);
  180. if (set & TIOCM_DTR) {
  181. private->line_signals |= UPD78F0730_DTR;
  182. dev_dbg(dev, "%s - set DTR\n", __func__);
  183. }
  184. if (set & TIOCM_RTS) {
  185. private->line_signals |= UPD78F0730_RTS;
  186. dev_dbg(dev, "%s - set RTS\n", __func__);
  187. }
  188. if (clear & TIOCM_DTR) {
  189. private->line_signals &= ~UPD78F0730_DTR;
  190. dev_dbg(dev, "%s - clear DTR\n", __func__);
  191. }
  192. if (clear & TIOCM_RTS) {
  193. private->line_signals &= ~UPD78F0730_RTS;
  194. dev_dbg(dev, "%s - clear RTS\n", __func__);
  195. }
  196. request.opcode = UPD78F0730_CMD_SET_DTR_RTS;
  197. request.params = private->line_signals;
  198. res = upd78f0730_send_ctl(port, &request, sizeof(request));
  199. mutex_unlock(&private->lock);
  200. return res;
  201. }
  202. static void upd78f0730_break_ctl(struct tty_struct *tty, int break_state)
  203. {
  204. struct device *dev = tty->dev;
  205. struct upd78f0730_port_private *private;
  206. struct usb_serial_port *port = tty->driver_data;
  207. struct upd78f0730_set_dtr_rts request;
  208. private = usb_get_serial_port_data(port);
  209. mutex_lock(&private->lock);
  210. if (break_state) {
  211. private->line_signals |= UPD78F0730_BREAK;
  212. dev_dbg(dev, "%s - set BREAK\n", __func__);
  213. } else {
  214. private->line_signals &= ~UPD78F0730_BREAK;
  215. dev_dbg(dev, "%s - clear BREAK\n", __func__);
  216. }
  217. request.opcode = UPD78F0730_CMD_SET_DTR_RTS;
  218. request.params = private->line_signals;
  219. upd78f0730_send_ctl(port, &request, sizeof(request));
  220. mutex_unlock(&private->lock);
  221. }
  222. static void upd78f0730_dtr_rts(struct usb_serial_port *port, int on)
  223. {
  224. struct tty_struct *tty = port->port.tty;
  225. unsigned int set = 0;
  226. unsigned int clear = 0;
  227. if (on)
  228. set = TIOCM_DTR | TIOCM_RTS;
  229. else
  230. clear = TIOCM_DTR | TIOCM_RTS;
  231. upd78f0730_tiocmset(tty, set, clear);
  232. }
  233. static speed_t upd78f0730_get_baud_rate(struct tty_struct *tty)
  234. {
  235. const speed_t baud_rate = tty_get_baud_rate(tty);
  236. const speed_t supported[] = {
  237. 0, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 153600
  238. };
  239. int i;
  240. for (i = ARRAY_SIZE(supported) - 1; i >= 0; i--) {
  241. if (baud_rate == supported[i])
  242. return baud_rate;
  243. }
  244. /* If the baud rate is not supported, switch to the default one */
  245. tty_encode_baud_rate(tty, 9600, 9600);
  246. return tty_get_baud_rate(tty);
  247. }
  248. static void upd78f0730_set_termios(struct tty_struct *tty,
  249. struct usb_serial_port *port,
  250. struct ktermios *old_termios)
  251. {
  252. struct device *dev = &port->dev;
  253. struct upd78f0730_line_control request;
  254. speed_t baud_rate;
  255. if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
  256. return;
  257. if (C_BAUD(tty) == B0)
  258. upd78f0730_dtr_rts(port, 0);
  259. else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
  260. upd78f0730_dtr_rts(port, 1);
  261. baud_rate = upd78f0730_get_baud_rate(tty);
  262. request.opcode = UPD78F0730_CMD_LINE_CONTROL;
  263. request.baud_rate = cpu_to_le32(baud_rate);
  264. request.params = 0;
  265. dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud_rate);
  266. switch (C_CSIZE(tty)) {
  267. case CS7:
  268. request.params |= UPD78F0730_DATA_SIZE_7_BITS;
  269. dev_dbg(dev, "%s - 7 data bits\n", __func__);
  270. break;
  271. default:
  272. tty->termios.c_cflag &= ~CSIZE;
  273. tty->termios.c_cflag |= CS8;
  274. dev_warn(dev, "data size is not supported, using 8 bits\n");
  275. /* fall through */
  276. case CS8:
  277. request.params |= UPD78F0730_DATA_SIZE_8_BITS;
  278. dev_dbg(dev, "%s - 8 data bits\n", __func__);
  279. break;
  280. }
  281. if (C_PARENB(tty)) {
  282. if (C_PARODD(tty)) {
  283. request.params |= UPD78F0730_PARITY_ODD;
  284. dev_dbg(dev, "%s - odd parity\n", __func__);
  285. } else {
  286. request.params |= UPD78F0730_PARITY_EVEN;
  287. dev_dbg(dev, "%s - even parity\n", __func__);
  288. }
  289. if (C_CMSPAR(tty)) {
  290. tty->termios.c_cflag &= ~CMSPAR;
  291. dev_warn(dev, "MARK/SPACE parity is not supported\n");
  292. }
  293. } else {
  294. request.params |= UPD78F0730_PARITY_NONE;
  295. dev_dbg(dev, "%s - no parity\n", __func__);
  296. }
  297. if (C_CSTOPB(tty)) {
  298. request.params |= UPD78F0730_STOP_BIT_2_BIT;
  299. dev_dbg(dev, "%s - 2 stop bits\n", __func__);
  300. } else {
  301. request.params |= UPD78F0730_STOP_BIT_1_BIT;
  302. dev_dbg(dev, "%s - 1 stop bit\n", __func__);
  303. }
  304. if (C_CRTSCTS(tty)) {
  305. tty->termios.c_cflag &= ~CRTSCTS;
  306. dev_warn(dev, "RTSCTS flow control is not supported\n");
  307. }
  308. if (I_IXOFF(tty) || I_IXON(tty)) {
  309. tty->termios.c_iflag &= ~(IXOFF | IXON);
  310. dev_warn(dev, "XON/XOFF flow control is not supported\n");
  311. }
  312. request.params |= UPD78F0730_FLOW_CONTROL_NONE;
  313. dev_dbg(dev, "%s - no flow control\n", __func__);
  314. upd78f0730_send_ctl(port, &request, sizeof(request));
  315. }
  316. static int upd78f0730_open(struct tty_struct *tty, struct usb_serial_port *port)
  317. {
  318. struct upd78f0730_open_close request = {
  319. .opcode = UPD78F0730_CMD_OPEN_CLOSE,
  320. .state = UPD78F0730_PORT_OPEN
  321. };
  322. int res;
  323. res = upd78f0730_send_ctl(port, &request, sizeof(request));
  324. if (res)
  325. return res;
  326. if (tty)
  327. upd78f0730_set_termios(tty, port, NULL);
  328. return usb_serial_generic_open(tty, port);
  329. }
  330. static void upd78f0730_close(struct usb_serial_port *port)
  331. {
  332. struct upd78f0730_open_close request = {
  333. .opcode = UPD78F0730_CMD_OPEN_CLOSE,
  334. .state = UPD78F0730_PORT_CLOSE
  335. };
  336. usb_serial_generic_close(port);
  337. upd78f0730_send_ctl(port, &request, sizeof(request));
  338. }
  339. static struct usb_serial_driver upd78f0730_device = {
  340. .driver = {
  341. .owner = THIS_MODULE,
  342. .name = "upd78f0730",
  343. },
  344. .id_table = id_table,
  345. .num_ports = 1,
  346. .port_probe = upd78f0730_port_probe,
  347. .port_remove = upd78f0730_port_remove,
  348. .open = upd78f0730_open,
  349. .close = upd78f0730_close,
  350. .set_termios = upd78f0730_set_termios,
  351. .tiocmget = upd78f0730_tiocmget,
  352. .tiocmset = upd78f0730_tiocmset,
  353. .dtr_rts = upd78f0730_dtr_rts,
  354. .break_ctl = upd78f0730_break_ctl,
  355. };
  356. static struct usb_serial_driver * const serial_drivers[] = {
  357. &upd78f0730_device,
  358. NULL
  359. };
  360. module_usb_serial_driver(serial_drivers, id_table);
  361. MODULE_DESCRIPTION(DRIVER_DESC);
  362. MODULE_AUTHOR(DRIVER_AUTHOR);
  363. MODULE_LICENSE("GPL v2");