2 * Renesas Electronics uPD78F0730 USB to serial converter driver
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.
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>
15 * The adaptor functionality is limited to the following:
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
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/usb.h>
28 #include <linux/usb/serial.h>
30 #define DRIVER_DESC "Renesas uPD78F0730 USB to serial converter driver"
34 static const struct usb_device_id id_table[] = {
35 { USB_DEVICE(0x0409, 0x0063) }, /* V850ESJX3-STICK */
36 { USB_DEVICE(0x045B, 0x0212) }, /* YRPBRL78G13, YRPBRL78G14 */
37 { USB_DEVICE(0x064B, 0x7825) }, /* Analog Devices EVAL-ADXL362Z-DB */
41 MODULE_DEVICE_TABLE(usb, id_table);
44 * Each adaptor is associated with a private structure, that holds the current
45 * state of control signals (DTR, RTS and BREAK).
47 struct upd78f0730_port_private {
48 struct mutex lock; /* mutex to protect line_signals */
52 /* Op-codes of control commands */
53 #define UPD78F0730_CMD_LINE_CONTROL 0x00
54 #define UPD78F0730_CMD_SET_DTR_RTS 0x01
55 #define UPD78F0730_CMD_SET_XON_XOFF_CHR 0x02
56 #define UPD78F0730_CMD_OPEN_CLOSE 0x03
57 #define UPD78F0730_CMD_SET_ERR_CHR 0x04
59 /* Data sizes in UPD78F0730_CMD_LINE_CONTROL command */
60 #define UPD78F0730_DATA_SIZE_7_BITS 0x00
61 #define UPD78F0730_DATA_SIZE_8_BITS 0x01
62 #define UPD78F0730_DATA_SIZE_MASK 0x01
64 /* Stop-bit modes in UPD78F0730_CMD_LINE_CONTROL command */
65 #define UPD78F0730_STOP_BIT_1_BIT 0x00
66 #define UPD78F0730_STOP_BIT_2_BIT 0x02
67 #define UPD78F0730_STOP_BIT_MASK 0x02
69 /* Parity modes in UPD78F0730_CMD_LINE_CONTROL command */
70 #define UPD78F0730_PARITY_NONE 0x00
71 #define UPD78F0730_PARITY_EVEN 0x04
72 #define UPD78F0730_PARITY_ODD 0x08
73 #define UPD78F0730_PARITY_MASK 0x0C
75 /* Flow control modes in UPD78F0730_CMD_LINE_CONTROL command */
76 #define UPD78F0730_FLOW_CONTROL_NONE 0x00
77 #define UPD78F0730_FLOW_CONTROL_HW 0x10
78 #define UPD78F0730_FLOW_CONTROL_SW 0x20
79 #define UPD78F0730_FLOW_CONTROL_MASK 0x30
81 /* Control signal bits in UPD78F0730_CMD_SET_DTR_RTS command */
82 #define UPD78F0730_RTS 0x01
83 #define UPD78F0730_DTR 0x02
84 #define UPD78F0730_BREAK 0x04
86 /* Port modes in UPD78F0730_CMD_OPEN_CLOSE command */
87 #define UPD78F0730_PORT_CLOSE 0x00
88 #define UPD78F0730_PORT_OPEN 0x01
90 /* Error character substitution modes in UPD78F0730_CMD_SET_ERR_CHR command */
91 #define UPD78F0730_ERR_CHR_DISABLED 0x00
92 #define UPD78F0730_ERR_CHR_ENABLED 0x01
95 * Declaration of command structures
98 /* UPD78F0730_CMD_LINE_CONTROL command */
99 struct upd78f0730_line_control {
105 /* UPD78F0730_CMD_SET_DTR_RTS command */
106 struct upd78f0730_set_dtr_rts {
111 /* UPD78F0730_CMD_SET_XON_OFF_CHR command */
112 struct upd78f0730_set_xon_xoff_chr {
118 /* UPD78F0730_CMD_OPEN_CLOSE command */
119 struct upd78f0730_open_close {
124 /* UPD78F0730_CMD_SET_ERR_CHR command */
125 struct upd78f0730_set_err_chr {
131 static int upd78f0730_send_ctl(struct usb_serial_port *port,
132 const void *data, int size)
134 struct usb_device *usbdev = port->serial->dev;
138 if (size <= 0 || !data)
141 buf = kmemdup(data, size, GFP_KERNEL);
145 res = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x00,
146 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
147 0x0000, 0x0000, buf, size, USB_CTRL_SET_TIMEOUT);
152 struct device *dev = &port->dev;
154 dev_err(dev, "failed to send control request %02x: %d\n",
156 /* The maximum expected length of a transfer is 6 bytes */
166 static int upd78f0730_port_probe(struct usb_serial_port *port)
168 struct upd78f0730_port_private *private;
170 private = kzalloc(sizeof(*private), GFP_KERNEL);
174 mutex_init(&private->lock);
175 usb_set_serial_port_data(port, private);
180 static int upd78f0730_port_remove(struct usb_serial_port *port)
182 struct upd78f0730_port_private *private;
184 private = usb_get_serial_port_data(port);
185 mutex_destroy(&private->lock);
191 static int upd78f0730_tiocmget(struct tty_struct *tty)
193 struct device *dev = tty->dev;
194 struct upd78f0730_port_private *private;
195 struct usb_serial_port *port = tty->driver_data;
199 private = usb_get_serial_port_data(port);
201 mutex_lock(&private->lock);
202 signals = private->line_signals;
203 mutex_unlock(&private->lock);
205 res = ((signals & UPD78F0730_DTR) ? TIOCM_DTR : 0) |
206 ((signals & UPD78F0730_RTS) ? TIOCM_RTS : 0);
208 dev_dbg(dev, "%s - res = %x\n", __func__, res);
213 static int upd78f0730_tiocmset(struct tty_struct *tty,
214 unsigned int set, unsigned int clear)
216 struct device *dev = tty->dev;
217 struct usb_serial_port *port = tty->driver_data;
218 struct upd78f0730_port_private *private;
219 struct upd78f0730_set_dtr_rts request;
222 private = usb_get_serial_port_data(port);
224 mutex_lock(&private->lock);
225 if (set & TIOCM_DTR) {
226 private->line_signals |= UPD78F0730_DTR;
227 dev_dbg(dev, "%s - set DTR\n", __func__);
229 if (set & TIOCM_RTS) {
230 private->line_signals |= UPD78F0730_RTS;
231 dev_dbg(dev, "%s - set RTS\n", __func__);
233 if (clear & TIOCM_DTR) {
234 private->line_signals &= ~UPD78F0730_DTR;
235 dev_dbg(dev, "%s - clear DTR\n", __func__);
237 if (clear & TIOCM_RTS) {
238 private->line_signals &= ~UPD78F0730_RTS;
239 dev_dbg(dev, "%s - clear RTS\n", __func__);
241 request.opcode = UPD78F0730_CMD_SET_DTR_RTS;
242 request.params = private->line_signals;
244 res = upd78f0730_send_ctl(port, &request, sizeof(request));
245 mutex_unlock(&private->lock);
250 static void upd78f0730_break_ctl(struct tty_struct *tty, int break_state)
252 struct device *dev = tty->dev;
253 struct upd78f0730_port_private *private;
254 struct usb_serial_port *port = tty->driver_data;
255 struct upd78f0730_set_dtr_rts request;
257 private = usb_get_serial_port_data(port);
259 mutex_lock(&private->lock);
261 private->line_signals |= UPD78F0730_BREAK;
262 dev_dbg(dev, "%s - set BREAK\n", __func__);
264 private->line_signals &= ~UPD78F0730_BREAK;
265 dev_dbg(dev, "%s - clear BREAK\n", __func__);
267 request.opcode = UPD78F0730_CMD_SET_DTR_RTS;
268 request.params = private->line_signals;
270 upd78f0730_send_ctl(port, &request, sizeof(request));
271 mutex_unlock(&private->lock);
274 static void upd78f0730_dtr_rts(struct usb_serial_port *port, int on)
276 struct tty_struct *tty = port->port.tty;
277 unsigned int set = 0;
278 unsigned int clear = 0;
281 set = TIOCM_DTR | TIOCM_RTS;
283 clear = TIOCM_DTR | TIOCM_RTS;
285 upd78f0730_tiocmset(tty, set, clear);
288 static speed_t upd78f0730_get_baud_rate(struct tty_struct *tty)
290 const speed_t baud_rate = tty_get_baud_rate(tty);
291 const speed_t supported[] = {
292 0, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 153600
296 for (i = ARRAY_SIZE(supported) - 1; i >= 0; i--) {
297 if (baud_rate == supported[i])
301 /* If the baud rate is not supported, switch to the default one */
302 tty_encode_baud_rate(tty, 9600, 9600);
304 return tty_get_baud_rate(tty);
307 static void upd78f0730_set_termios(struct tty_struct *tty,
308 struct usb_serial_port *port,
309 struct ktermios *old_termios)
311 struct device *dev = &port->dev;
312 struct upd78f0730_line_control request;
315 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
318 if (C_BAUD(tty) == B0)
319 upd78f0730_dtr_rts(port, 0);
320 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
321 upd78f0730_dtr_rts(port, 1);
323 baud_rate = upd78f0730_get_baud_rate(tty);
324 request.opcode = UPD78F0730_CMD_LINE_CONTROL;
325 request.baud_rate = cpu_to_le32(baud_rate);
327 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud_rate);
329 switch (C_CSIZE(tty)) {
331 request.params |= UPD78F0730_DATA_SIZE_7_BITS;
332 dev_dbg(dev, "%s - 7 data bits\n", __func__);
335 tty->termios.c_cflag &= ~CSIZE;
336 tty->termios.c_cflag |= CS8;
337 dev_warn(dev, "data size is not supported, using 8 bits\n");
340 request.params |= UPD78F0730_DATA_SIZE_8_BITS;
341 dev_dbg(dev, "%s - 8 data bits\n", __func__);
347 request.params |= UPD78F0730_PARITY_ODD;
348 dev_dbg(dev, "%s - odd parity\n", __func__);
350 request.params |= UPD78F0730_PARITY_EVEN;
351 dev_dbg(dev, "%s - even parity\n", __func__);
355 tty->termios.c_cflag &= ~CMSPAR;
356 dev_warn(dev, "MARK/SPACE parity is not supported\n");
359 request.params |= UPD78F0730_PARITY_NONE;
360 dev_dbg(dev, "%s - no parity\n", __func__);
364 request.params |= UPD78F0730_STOP_BIT_2_BIT;
365 dev_dbg(dev, "%s - 2 stop bits\n", __func__);
367 request.params |= UPD78F0730_STOP_BIT_1_BIT;
368 dev_dbg(dev, "%s - 1 stop bit\n", __func__);
371 if (C_CRTSCTS(tty)) {
372 tty->termios.c_cflag &= ~CRTSCTS;
373 dev_warn(dev, "RTSCTS flow control is not supported\n");
375 if (I_IXOFF(tty) || I_IXON(tty)) {
376 tty->termios.c_iflag &= ~(IXOFF | IXON);
377 dev_warn(dev, "XON/XOFF flow control is not supported\n");
379 request.params |= UPD78F0730_FLOW_CONTROL_NONE;
380 dev_dbg(dev, "%s - no flow control\n", __func__);
382 upd78f0730_send_ctl(port, &request, sizeof(request));
385 static int upd78f0730_open(struct tty_struct *tty, struct usb_serial_port *port)
387 struct upd78f0730_open_close request = {
388 .opcode = UPD78F0730_CMD_OPEN_CLOSE,
389 .state = UPD78F0730_PORT_OPEN
393 res = upd78f0730_send_ctl(port, &request, sizeof(request));
398 upd78f0730_set_termios(tty, port, NULL);
400 return usb_serial_generic_open(tty, port);
403 static void upd78f0730_close(struct usb_serial_port *port)
405 struct upd78f0730_open_close request = {
406 .opcode = UPD78F0730_CMD_OPEN_CLOSE,
407 .state = UPD78F0730_PORT_CLOSE
410 usb_serial_generic_close(port);
411 upd78f0730_send_ctl(port, &request, sizeof(request));
414 static struct usb_serial_driver upd78f0730_device = {
416 .owner = THIS_MODULE,
417 .name = "upd78f0730",
419 .id_table = id_table,
421 .port_probe = upd78f0730_port_probe,
422 .port_remove = upd78f0730_port_remove,
423 .open = upd78f0730_open,
424 .close = upd78f0730_close,
425 .set_termios = upd78f0730_set_termios,
426 .tiocmget = upd78f0730_tiocmget,
427 .tiocmset = upd78f0730_tiocmset,
428 .dtr_rts = upd78f0730_dtr_rts,
429 .break_ctl = upd78f0730_break_ctl,
432 static struct usb_serial_driver * const serial_drivers[] = {
437 module_usb_serial_driver(serial_drivers, id_table);
439 MODULE_DESCRIPTION(DRIVER_DESC);
440 MODULE_AUTHOR(DRIVER_AUTHOR);
441 MODULE_LICENSE("GPL v2");