1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas Electronics uPD78F0730 USB to serial converter driver
5 * Copyright (C) 2014,2016 Maksim Salau <maksim.salau@gmail.com>
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>
12 * The adaptor functionality is limited to the following:
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
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/tty.h>
24 #include <linux/usb.h>
25 #include <linux/usb/serial.h>
27 #define DRIVER_DESC "Renesas uPD78F0730 USB to serial converter driver"
29 #define DRIVER_AUTHOR "Maksim Salau <maksim.salau@gmail.com>"
31 static const struct usb_device_id id_table[] = {
32 { USB_DEVICE(0x0409, 0x0063) }, /* V850ESJX3-STICK */
33 { USB_DEVICE(0x045B, 0x0212) }, /* YRPBRL78G13, YRPBRL78G14 */
34 { USB_DEVICE(0x064B, 0x7825) }, /* Analog Devices EVAL-ADXL362Z-DB */
38 MODULE_DEVICE_TABLE(usb, id_table);
41 * Each adaptor is associated with a private structure, that holds the current
42 * state of control signals (DTR, RTS and BREAK).
44 struct upd78f0730_port_private {
45 struct mutex lock; /* mutex to protect line_signals */
49 /* Op-codes of control commands */
50 #define UPD78F0730_CMD_LINE_CONTROL 0x00
51 #define UPD78F0730_CMD_SET_DTR_RTS 0x01
52 #define UPD78F0730_CMD_SET_XON_XOFF_CHR 0x02
53 #define UPD78F0730_CMD_OPEN_CLOSE 0x03
54 #define UPD78F0730_CMD_SET_ERR_CHR 0x04
56 /* Data sizes in UPD78F0730_CMD_LINE_CONTROL command */
57 #define UPD78F0730_DATA_SIZE_7_BITS 0x00
58 #define UPD78F0730_DATA_SIZE_8_BITS 0x01
59 #define UPD78F0730_DATA_SIZE_MASK 0x01
61 /* Stop-bit modes in UPD78F0730_CMD_LINE_CONTROL command */
62 #define UPD78F0730_STOP_BIT_1_BIT 0x00
63 #define UPD78F0730_STOP_BIT_2_BIT 0x02
64 #define UPD78F0730_STOP_BIT_MASK 0x02
66 /* Parity modes in UPD78F0730_CMD_LINE_CONTROL command */
67 #define UPD78F0730_PARITY_NONE 0x00
68 #define UPD78F0730_PARITY_EVEN 0x04
69 #define UPD78F0730_PARITY_ODD 0x08
70 #define UPD78F0730_PARITY_MASK 0x0C
72 /* Flow control modes in UPD78F0730_CMD_LINE_CONTROL command */
73 #define UPD78F0730_FLOW_CONTROL_NONE 0x00
74 #define UPD78F0730_FLOW_CONTROL_HW 0x10
75 #define UPD78F0730_FLOW_CONTROL_SW 0x20
76 #define UPD78F0730_FLOW_CONTROL_MASK 0x30
78 /* Control signal bits in UPD78F0730_CMD_SET_DTR_RTS command */
79 #define UPD78F0730_RTS 0x01
80 #define UPD78F0730_DTR 0x02
81 #define UPD78F0730_BREAK 0x04
83 /* Port modes in UPD78F0730_CMD_OPEN_CLOSE command */
84 #define UPD78F0730_PORT_CLOSE 0x00
85 #define UPD78F0730_PORT_OPEN 0x01
87 /* Error character substitution modes in UPD78F0730_CMD_SET_ERR_CHR command */
88 #define UPD78F0730_ERR_CHR_DISABLED 0x00
89 #define UPD78F0730_ERR_CHR_ENABLED 0x01
92 * Declaration of command structures
95 /* UPD78F0730_CMD_LINE_CONTROL command */
96 struct upd78f0730_line_control {
102 /* UPD78F0730_CMD_SET_DTR_RTS command */
103 struct upd78f0730_set_dtr_rts {
108 /* UPD78F0730_CMD_SET_XON_OFF_CHR command */
109 struct upd78f0730_set_xon_xoff_chr {
115 /* UPD78F0730_CMD_OPEN_CLOSE command */
116 struct upd78f0730_open_close {
121 /* UPD78F0730_CMD_SET_ERR_CHR command */
122 struct upd78f0730_set_err_chr {
128 static int upd78f0730_send_ctl(struct usb_serial_port *port,
129 const void *data, int size)
131 struct usb_device *usbdev = port->serial->dev;
135 if (size <= 0 || !data)
138 buf = kmemdup(data, size, GFP_KERNEL);
142 res = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x00,
143 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
144 0x0000, 0x0000, buf, size, USB_CTRL_SET_TIMEOUT);
149 struct device *dev = &port->dev;
151 dev_err(dev, "failed to send control request %02x: %d\n",
160 static int upd78f0730_port_probe(struct usb_serial_port *port)
162 struct upd78f0730_port_private *private;
164 private = kzalloc(sizeof(*private), GFP_KERNEL);
168 mutex_init(&private->lock);
169 usb_set_serial_port_data(port, private);
174 static void upd78f0730_port_remove(struct usb_serial_port *port)
176 struct upd78f0730_port_private *private;
178 private = usb_get_serial_port_data(port);
179 mutex_destroy(&private->lock);
183 static int upd78f0730_tiocmget(struct tty_struct *tty)
185 struct upd78f0730_port_private *private;
186 struct usb_serial_port *port = tty->driver_data;
190 private = usb_get_serial_port_data(port);
192 mutex_lock(&private->lock);
193 signals = private->line_signals;
194 mutex_unlock(&private->lock);
196 res = ((signals & UPD78F0730_DTR) ? TIOCM_DTR : 0) |
197 ((signals & UPD78F0730_RTS) ? TIOCM_RTS : 0);
199 dev_dbg(&port->dev, "%s - res = %x\n", __func__, res);
204 static int upd78f0730_tiocmset(struct tty_struct *tty,
205 unsigned int set, unsigned int clear)
207 struct usb_serial_port *port = tty->driver_data;
208 struct upd78f0730_port_private *private;
209 struct upd78f0730_set_dtr_rts request;
210 struct device *dev = &port->dev;
213 private = usb_get_serial_port_data(port);
215 mutex_lock(&private->lock);
216 if (set & TIOCM_DTR) {
217 private->line_signals |= UPD78F0730_DTR;
218 dev_dbg(dev, "%s - set DTR\n", __func__);
220 if (set & TIOCM_RTS) {
221 private->line_signals |= UPD78F0730_RTS;
222 dev_dbg(dev, "%s - set RTS\n", __func__);
224 if (clear & TIOCM_DTR) {
225 private->line_signals &= ~UPD78F0730_DTR;
226 dev_dbg(dev, "%s - clear DTR\n", __func__);
228 if (clear & TIOCM_RTS) {
229 private->line_signals &= ~UPD78F0730_RTS;
230 dev_dbg(dev, "%s - clear RTS\n", __func__);
232 request.opcode = UPD78F0730_CMD_SET_DTR_RTS;
233 request.params = private->line_signals;
235 res = upd78f0730_send_ctl(port, &request, sizeof(request));
236 mutex_unlock(&private->lock);
241 static void upd78f0730_break_ctl(struct tty_struct *tty, int break_state)
243 struct upd78f0730_port_private *private;
244 struct usb_serial_port *port = tty->driver_data;
245 struct upd78f0730_set_dtr_rts request;
246 struct device *dev = &port->dev;
248 private = usb_get_serial_port_data(port);
250 mutex_lock(&private->lock);
252 private->line_signals |= UPD78F0730_BREAK;
253 dev_dbg(dev, "%s - set BREAK\n", __func__);
255 private->line_signals &= ~UPD78F0730_BREAK;
256 dev_dbg(dev, "%s - clear BREAK\n", __func__);
258 request.opcode = UPD78F0730_CMD_SET_DTR_RTS;
259 request.params = private->line_signals;
261 upd78f0730_send_ctl(port, &request, sizeof(request));
262 mutex_unlock(&private->lock);
265 static void upd78f0730_dtr_rts(struct usb_serial_port *port, int on)
267 struct tty_struct *tty = port->port.tty;
268 unsigned int set = 0;
269 unsigned int clear = 0;
272 set = TIOCM_DTR | TIOCM_RTS;
274 clear = TIOCM_DTR | TIOCM_RTS;
276 upd78f0730_tiocmset(tty, set, clear);
279 static speed_t upd78f0730_get_baud_rate(struct tty_struct *tty)
281 const speed_t baud_rate = tty_get_baud_rate(tty);
282 static const speed_t supported[] = {
283 0, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 153600
287 for (i = ARRAY_SIZE(supported) - 1; i >= 0; i--) {
288 if (baud_rate == supported[i])
292 /* If the baud rate is not supported, switch to the default one */
293 tty_encode_baud_rate(tty, 9600, 9600);
295 return tty_get_baud_rate(tty);
298 static void upd78f0730_set_termios(struct tty_struct *tty,
299 struct usb_serial_port *port,
300 struct ktermios *old_termios)
302 struct device *dev = &port->dev;
303 struct upd78f0730_line_control request;
306 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
309 if (C_BAUD(tty) == B0)
310 upd78f0730_dtr_rts(port, 0);
311 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
312 upd78f0730_dtr_rts(port, 1);
314 baud_rate = upd78f0730_get_baud_rate(tty);
315 request.opcode = UPD78F0730_CMD_LINE_CONTROL;
316 request.baud_rate = cpu_to_le32(baud_rate);
318 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud_rate);
320 switch (C_CSIZE(tty)) {
322 request.params |= UPD78F0730_DATA_SIZE_7_BITS;
323 dev_dbg(dev, "%s - 7 data bits\n", __func__);
326 tty->termios.c_cflag &= ~CSIZE;
327 tty->termios.c_cflag |= CS8;
328 dev_warn(dev, "data size is not supported, using 8 bits\n");
331 request.params |= UPD78F0730_DATA_SIZE_8_BITS;
332 dev_dbg(dev, "%s - 8 data bits\n", __func__);
338 request.params |= UPD78F0730_PARITY_ODD;
339 dev_dbg(dev, "%s - odd parity\n", __func__);
341 request.params |= UPD78F0730_PARITY_EVEN;
342 dev_dbg(dev, "%s - even parity\n", __func__);
346 tty->termios.c_cflag &= ~CMSPAR;
347 dev_warn(dev, "MARK/SPACE parity is not supported\n");
350 request.params |= UPD78F0730_PARITY_NONE;
351 dev_dbg(dev, "%s - no parity\n", __func__);
355 request.params |= UPD78F0730_STOP_BIT_2_BIT;
356 dev_dbg(dev, "%s - 2 stop bits\n", __func__);
358 request.params |= UPD78F0730_STOP_BIT_1_BIT;
359 dev_dbg(dev, "%s - 1 stop bit\n", __func__);
362 if (C_CRTSCTS(tty)) {
363 tty->termios.c_cflag &= ~CRTSCTS;
364 dev_warn(dev, "RTSCTS flow control is not supported\n");
366 if (I_IXOFF(tty) || I_IXON(tty)) {
367 tty->termios.c_iflag &= ~(IXOFF | IXON);
368 dev_warn(dev, "XON/XOFF flow control is not supported\n");
370 request.params |= UPD78F0730_FLOW_CONTROL_NONE;
371 dev_dbg(dev, "%s - no flow control\n", __func__);
373 upd78f0730_send_ctl(port, &request, sizeof(request));
376 static int upd78f0730_open(struct tty_struct *tty, struct usb_serial_port *port)
378 static const struct upd78f0730_open_close request = {
379 .opcode = UPD78F0730_CMD_OPEN_CLOSE,
380 .state = UPD78F0730_PORT_OPEN
384 res = upd78f0730_send_ctl(port, &request, sizeof(request));
389 upd78f0730_set_termios(tty, port, NULL);
391 return usb_serial_generic_open(tty, port);
394 static void upd78f0730_close(struct usb_serial_port *port)
396 static const struct upd78f0730_open_close request = {
397 .opcode = UPD78F0730_CMD_OPEN_CLOSE,
398 .state = UPD78F0730_PORT_CLOSE
401 usb_serial_generic_close(port);
402 upd78f0730_send_ctl(port, &request, sizeof(request));
405 static struct usb_serial_driver upd78f0730_device = {
407 .owner = THIS_MODULE,
408 .name = "upd78f0730",
410 .id_table = id_table,
412 .port_probe = upd78f0730_port_probe,
413 .port_remove = upd78f0730_port_remove,
414 .open = upd78f0730_open,
415 .close = upd78f0730_close,
416 .set_termios = upd78f0730_set_termios,
417 .tiocmget = upd78f0730_tiocmget,
418 .tiocmset = upd78f0730_tiocmset,
419 .dtr_rts = upd78f0730_dtr_rts,
420 .break_ctl = upd78f0730_break_ctl,
423 static struct usb_serial_driver * const serial_drivers[] = {
428 module_usb_serial_driver(serial_drivers, id_table);
430 MODULE_DESCRIPTION(DRIVER_DESC);
431 MODULE_AUTHOR(DRIVER_AUTHOR);
432 MODULE_LICENSE("GPL v2");