upd78f0730.c 11 KB

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