upd78f0730.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 <maksim.salau@gmail.com>
  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 <maksim.salau@gmail.com>"
  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 != size) {
  123. struct device *dev = &port->dev;
  124. dev_err(dev, "failed to send control request %02x: %d\n",
  125. *(u8 *)data, res);
  126. /* The maximum expected length of a transfer is 6 bytes */
  127. if (res >= 0)
  128. res = -EIO;
  129. return res;
  130. }
  131. return 0;
  132. }
  133. static int upd78f0730_port_probe(struct usb_serial_port *port)
  134. {
  135. struct upd78f0730_port_private *private;
  136. private = kzalloc(sizeof(*private), GFP_KERNEL);
  137. if (!private)
  138. return -ENOMEM;
  139. mutex_init(&private->lock);
  140. usb_set_serial_port_data(port, private);
  141. return 0;
  142. }
  143. static int upd78f0730_port_remove(struct usb_serial_port *port)
  144. {
  145. struct upd78f0730_port_private *private;
  146. private = usb_get_serial_port_data(port);
  147. mutex_destroy(&private->lock);
  148. kfree(private);
  149. return 0;
  150. }
  151. static int upd78f0730_tiocmget(struct tty_struct *tty)
  152. {
  153. struct device *dev = tty->dev;
  154. struct upd78f0730_port_private *private;
  155. struct usb_serial_port *port = tty->driver_data;
  156. int signals;
  157. int res;
  158. private = usb_get_serial_port_data(port);
  159. mutex_lock(&private->lock);
  160. signals = private->line_signals;
  161. mutex_unlock(&private->lock);
  162. res = ((signals & UPD78F0730_DTR) ? TIOCM_DTR : 0) |
  163. ((signals & UPD78F0730_RTS) ? TIOCM_RTS : 0);
  164. dev_dbg(dev, "%s - res = %x\n", __func__, res);
  165. return res;
  166. }
  167. static int upd78f0730_tiocmset(struct tty_struct *tty,
  168. unsigned int set, unsigned int clear)
  169. {
  170. struct device *dev = tty->dev;
  171. struct usb_serial_port *port = tty->driver_data;
  172. struct upd78f0730_port_private *private;
  173. struct upd78f0730_set_dtr_rts request;
  174. int res;
  175. private = usb_get_serial_port_data(port);
  176. mutex_lock(&private->lock);
  177. if (set & TIOCM_DTR) {
  178. private->line_signals |= UPD78F0730_DTR;
  179. dev_dbg(dev, "%s - set DTR\n", __func__);
  180. }
  181. if (set & TIOCM_RTS) {
  182. private->line_signals |= UPD78F0730_RTS;
  183. dev_dbg(dev, "%s - set RTS\n", __func__);
  184. }
  185. if (clear & TIOCM_DTR) {
  186. private->line_signals &= ~UPD78F0730_DTR;
  187. dev_dbg(dev, "%s - clear DTR\n", __func__);
  188. }
  189. if (clear & TIOCM_RTS) {
  190. private->line_signals &= ~UPD78F0730_RTS;
  191. dev_dbg(dev, "%s - clear RTS\n", __func__);
  192. }
  193. request.opcode = UPD78F0730_CMD_SET_DTR_RTS;
  194. request.params = private->line_signals;
  195. res = upd78f0730_send_ctl(port, &request, sizeof(request));
  196. mutex_unlock(&private->lock);
  197. return res;
  198. }
  199. static void upd78f0730_break_ctl(struct tty_struct *tty, int break_state)
  200. {
  201. struct device *dev = tty->dev;
  202. struct upd78f0730_port_private *private;
  203. struct usb_serial_port *port = tty->driver_data;
  204. struct upd78f0730_set_dtr_rts request;
  205. private = usb_get_serial_port_data(port);
  206. mutex_lock(&private->lock);
  207. if (break_state) {
  208. private->line_signals |= UPD78F0730_BREAK;
  209. dev_dbg(dev, "%s - set BREAK\n", __func__);
  210. } else {
  211. private->line_signals &= ~UPD78F0730_BREAK;
  212. dev_dbg(dev, "%s - clear BREAK\n", __func__);
  213. }
  214. request.opcode = UPD78F0730_CMD_SET_DTR_RTS;
  215. request.params = private->line_signals;
  216. upd78f0730_send_ctl(port, &request, sizeof(request));
  217. mutex_unlock(&private->lock);
  218. }
  219. static void upd78f0730_dtr_rts(struct usb_serial_port *port, int on)
  220. {
  221. struct tty_struct *tty = port->port.tty;
  222. unsigned int set = 0;
  223. unsigned int clear = 0;
  224. if (on)
  225. set = TIOCM_DTR | TIOCM_RTS;
  226. else
  227. clear = TIOCM_DTR | TIOCM_RTS;
  228. upd78f0730_tiocmset(tty, set, clear);
  229. }
  230. static speed_t upd78f0730_get_baud_rate(struct tty_struct *tty)
  231. {
  232. const speed_t baud_rate = tty_get_baud_rate(tty);
  233. static const speed_t supported[] = {
  234. 0, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 153600
  235. };
  236. int i;
  237. for (i = ARRAY_SIZE(supported) - 1; i >= 0; i--) {
  238. if (baud_rate == supported[i])
  239. return baud_rate;
  240. }
  241. /* If the baud rate is not supported, switch to the default one */
  242. tty_encode_baud_rate(tty, 9600, 9600);
  243. return tty_get_baud_rate(tty);
  244. }
  245. static void upd78f0730_set_termios(struct tty_struct *tty,
  246. struct usb_serial_port *port,
  247. struct ktermios *old_termios)
  248. {
  249. struct device *dev = &port->dev;
  250. struct upd78f0730_line_control request;
  251. speed_t baud_rate;
  252. if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
  253. return;
  254. if (C_BAUD(tty) == B0)
  255. upd78f0730_dtr_rts(port, 0);
  256. else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
  257. upd78f0730_dtr_rts(port, 1);
  258. baud_rate = upd78f0730_get_baud_rate(tty);
  259. request.opcode = UPD78F0730_CMD_LINE_CONTROL;
  260. request.baud_rate = cpu_to_le32(baud_rate);
  261. request.params = 0;
  262. dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud_rate);
  263. switch (C_CSIZE(tty)) {
  264. case CS7:
  265. request.params |= UPD78F0730_DATA_SIZE_7_BITS;
  266. dev_dbg(dev, "%s - 7 data bits\n", __func__);
  267. break;
  268. default:
  269. tty->termios.c_cflag &= ~CSIZE;
  270. tty->termios.c_cflag |= CS8;
  271. dev_warn(dev, "data size is not supported, using 8 bits\n");
  272. fallthrough;
  273. case CS8:
  274. request.params |= UPD78F0730_DATA_SIZE_8_BITS;
  275. dev_dbg(dev, "%s - 8 data bits\n", __func__);
  276. break;
  277. }
  278. if (C_PARENB(tty)) {
  279. if (C_PARODD(tty)) {
  280. request.params |= UPD78F0730_PARITY_ODD;
  281. dev_dbg(dev, "%s - odd parity\n", __func__);
  282. } else {
  283. request.params |= UPD78F0730_PARITY_EVEN;
  284. dev_dbg(dev, "%s - even parity\n", __func__);
  285. }
  286. if (C_CMSPAR(tty)) {
  287. tty->termios.c_cflag &= ~CMSPAR;
  288. dev_warn(dev, "MARK/SPACE parity is not supported\n");
  289. }
  290. } else {
  291. request.params |= UPD78F0730_PARITY_NONE;
  292. dev_dbg(dev, "%s - no parity\n", __func__);
  293. }
  294. if (C_CSTOPB(tty)) {
  295. request.params |= UPD78F0730_STOP_BIT_2_BIT;
  296. dev_dbg(dev, "%s - 2 stop bits\n", __func__);
  297. } else {
  298. request.params |= UPD78F0730_STOP_BIT_1_BIT;
  299. dev_dbg(dev, "%s - 1 stop bit\n", __func__);
  300. }
  301. if (C_CRTSCTS(tty)) {
  302. tty->termios.c_cflag &= ~CRTSCTS;
  303. dev_warn(dev, "RTSCTS flow control is not supported\n");
  304. }
  305. if (I_IXOFF(tty) || I_IXON(tty)) {
  306. tty->termios.c_iflag &= ~(IXOFF | IXON);
  307. dev_warn(dev, "XON/XOFF flow control is not supported\n");
  308. }
  309. request.params |= UPD78F0730_FLOW_CONTROL_NONE;
  310. dev_dbg(dev, "%s - no flow control\n", __func__);
  311. upd78f0730_send_ctl(port, &request, sizeof(request));
  312. }
  313. static int upd78f0730_open(struct tty_struct *tty, struct usb_serial_port *port)
  314. {
  315. static const struct upd78f0730_open_close request = {
  316. .opcode = UPD78F0730_CMD_OPEN_CLOSE,
  317. .state = UPD78F0730_PORT_OPEN
  318. };
  319. int res;
  320. res = upd78f0730_send_ctl(port, &request, sizeof(request));
  321. if (res)
  322. return res;
  323. if (tty)
  324. upd78f0730_set_termios(tty, port, NULL);
  325. return usb_serial_generic_open(tty, port);
  326. }
  327. static void upd78f0730_close(struct usb_serial_port *port)
  328. {
  329. static const struct upd78f0730_open_close request = {
  330. .opcode = UPD78F0730_CMD_OPEN_CLOSE,
  331. .state = UPD78F0730_PORT_CLOSE
  332. };
  333. usb_serial_generic_close(port);
  334. upd78f0730_send_ctl(port, &request, sizeof(request));
  335. }
  336. static struct usb_serial_driver upd78f0730_device = {
  337. .driver = {
  338. .owner = THIS_MODULE,
  339. .name = "upd78f0730",
  340. },
  341. .id_table = id_table,
  342. .num_ports = 1,
  343. .port_probe = upd78f0730_port_probe,
  344. .port_remove = upd78f0730_port_remove,
  345. .open = upd78f0730_open,
  346. .close = upd78f0730_close,
  347. .set_termios = upd78f0730_set_termios,
  348. .tiocmget = upd78f0730_tiocmget,
  349. .tiocmset = upd78f0730_tiocmset,
  350. .dtr_rts = upd78f0730_dtr_rts,
  351. .break_ctl = upd78f0730_break_ctl,
  352. };
  353. static struct usb_serial_driver * const serial_drivers[] = {
  354. &upd78f0730_device,
  355. NULL
  356. };
  357. module_usb_serial_driver(serial_drivers, id_table);
  358. MODULE_DESCRIPTION(DRIVER_DESC);
  359. MODULE_AUTHOR(DRIVER_AUTHOR);
  360. MODULE_LICENSE("GPL v2");