]>
Commit | Line | Data |
---|---|---|
ea534e0b MS |
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 | |
89fd8ee8 | 20 | * - baud rates: 0, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 153600 |
ea534e0b MS |
21 | * - signals: DTR, RTS and BREAK |
22 | */ | |
23 | ||
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> | |
29 | ||
30 | #define DRIVER_DESC "Renesas uPD78F0730 USB to serial converter driver" | |
31 | ||
32 | #define DRIVER_AUTHOR "Maksim Salau <maksim.salau@gmail.com>" | |
33 | ||
34 | static const struct usb_device_id id_table[] = { | |
ea534e0b | 35 | { USB_DEVICE(0x0409, 0x0063) }, /* V850ESJX3-STICK */ |
d0c54f2f | 36 | { USB_DEVICE(0x045B, 0x0212) }, /* YRPBRL78G13, YRPBRL78G14 */ |
89fd8ee8 | 37 | { USB_DEVICE(0x064B, 0x7825) }, /* Analog Devices EVAL-ADXL362Z-DB */ |
ea534e0b MS |
38 | {} |
39 | }; | |
40 | ||
41 | MODULE_DEVICE_TABLE(usb, id_table); | |
42 | ||
43 | /* | |
44 | * Each adaptor is associated with a private structure, that holds the current | |
45 | * state of control signals (DTR, RTS and BREAK). | |
46 | */ | |
47 | struct upd78f0730_port_private { | |
48 | struct mutex lock; /* mutex to protect line_signals */ | |
49 | u8 line_signals; | |
50 | }; | |
51 | ||
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 | |
58 | ||
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 | |
63 | ||
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 | |
68 | ||
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 | |
74 | ||
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 | |
80 | ||
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 | |
85 | ||
86 | /* Port modes in UPD78F0730_CMD_OPEN_CLOSE command */ | |
87 | #define UPD78F0730_PORT_CLOSE 0x00 | |
88 | #define UPD78F0730_PORT_OPEN 0x01 | |
89 | ||
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 | |
93 | ||
94 | /* | |
95 | * Declaration of command structures | |
96 | */ | |
97 | ||
98 | /* UPD78F0730_CMD_LINE_CONTROL command */ | |
99 | struct upd78f0730_line_control { | |
100 | u8 opcode; | |
101 | __le32 baud_rate; | |
102 | u8 params; | |
103 | } __packed; | |
104 | ||
105 | /* UPD78F0730_CMD_SET_DTR_RTS command */ | |
106 | struct upd78f0730_set_dtr_rts { | |
107 | u8 opcode; | |
108 | u8 params; | |
109 | }; | |
110 | ||
111 | /* UPD78F0730_CMD_SET_XON_OFF_CHR command */ | |
112 | struct upd78f0730_set_xon_xoff_chr { | |
113 | u8 opcode; | |
114 | u8 xon; | |
115 | u8 xoff; | |
116 | }; | |
117 | ||
118 | /* UPD78F0730_CMD_OPEN_CLOSE command */ | |
119 | struct upd78f0730_open_close { | |
120 | u8 opcode; | |
121 | u8 state; | |
122 | }; | |
123 | ||
124 | /* UPD78F0730_CMD_SET_ERR_CHR command */ | |
125 | struct upd78f0730_set_err_chr { | |
126 | u8 opcode; | |
127 | u8 state; | |
128 | u8 err_char; | |
129 | }; | |
130 | ||
131 | static int upd78f0730_send_ctl(struct usb_serial_port *port, | |
132 | const void *data, int size) | |
133 | { | |
134 | struct usb_device *usbdev = port->serial->dev; | |
135 | void *buf; | |
136 | int res; | |
137 | ||
138 | if (size <= 0 || !data) | |
139 | return -EINVAL; | |
140 | ||
141 | buf = kmemdup(data, size, GFP_KERNEL); | |
142 | if (!buf) | |
143 | return -ENOMEM; | |
144 | ||
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); | |
148 | ||
149 | kfree(buf); | |
150 | ||
151 | if (res != size) { | |
152 | struct device *dev = &port->dev; | |
153 | ||
154 | dev_err(dev, "failed to send control request %02x: %d\n", | |
155 | *(u8 *)data, res); | |
156 | /* The maximum expected length of a transfer is 6 bytes */ | |
157 | if (res >= 0) | |
158 | res = -EIO; | |
159 | ||
160 | return res; | |
161 | } | |
162 | ||
163 | return 0; | |
164 | } | |
165 | ||
166 | static int upd78f0730_port_probe(struct usb_serial_port *port) | |
167 | { | |
168 | struct upd78f0730_port_private *private; | |
169 | ||
170 | private = kzalloc(sizeof(*private), GFP_KERNEL); | |
171 | if (!private) | |
172 | return -ENOMEM; | |
173 | ||
174 | mutex_init(&private->lock); | |
175 | usb_set_serial_port_data(port, private); | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
180 | static int upd78f0730_port_remove(struct usb_serial_port *port) | |
181 | { | |
182 | struct upd78f0730_port_private *private; | |
183 | ||
184 | private = usb_get_serial_port_data(port); | |
185 | mutex_destroy(&private->lock); | |
186 | kfree(private); | |
187 | ||
188 | return 0; | |
189 | } | |
190 | ||
191 | static int upd78f0730_tiocmget(struct tty_struct *tty) | |
192 | { | |
193 | struct device *dev = tty->dev; | |
194 | struct upd78f0730_port_private *private; | |
195 | struct usb_serial_port *port = tty->driver_data; | |
196 | int signals; | |
197 | int res; | |
198 | ||
199 | private = usb_get_serial_port_data(port); | |
200 | ||
201 | mutex_lock(&private->lock); | |
202 | signals = private->line_signals; | |
203 | mutex_unlock(&private->lock); | |
204 | ||
205 | res = ((signals & UPD78F0730_DTR) ? TIOCM_DTR : 0) | | |
206 | ((signals & UPD78F0730_RTS) ? TIOCM_RTS : 0); | |
207 | ||
208 | dev_dbg(dev, "%s - res = %x\n", __func__, res); | |
209 | ||
210 | return res; | |
211 | } | |
212 | ||
213 | static int upd78f0730_tiocmset(struct tty_struct *tty, | |
214 | unsigned int set, unsigned int clear) | |
215 | { | |
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; | |
220 | int res; | |
221 | ||
222 | private = usb_get_serial_port_data(port); | |
223 | ||
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__); | |
228 | } | |
229 | if (set & TIOCM_RTS) { | |
230 | private->line_signals |= UPD78F0730_RTS; | |
231 | dev_dbg(dev, "%s - set RTS\n", __func__); | |
232 | } | |
233 | if (clear & TIOCM_DTR) { | |
234 | private->line_signals &= ~UPD78F0730_DTR; | |
235 | dev_dbg(dev, "%s - clear DTR\n", __func__); | |
236 | } | |
237 | if (clear & TIOCM_RTS) { | |
238 | private->line_signals &= ~UPD78F0730_RTS; | |
239 | dev_dbg(dev, "%s - clear RTS\n", __func__); | |
240 | } | |
241 | request.opcode = UPD78F0730_CMD_SET_DTR_RTS; | |
242 | request.params = private->line_signals; | |
243 | ||
244 | res = upd78f0730_send_ctl(port, &request, sizeof(request)); | |
245 | mutex_unlock(&private->lock); | |
246 | ||
247 | return res; | |
248 | } | |
249 | ||
250 | static void upd78f0730_break_ctl(struct tty_struct *tty, int break_state) | |
251 | { | |
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; | |
256 | ||
257 | private = usb_get_serial_port_data(port); | |
258 | ||
259 | mutex_lock(&private->lock); | |
260 | if (break_state) { | |
261 | private->line_signals |= UPD78F0730_BREAK; | |
262 | dev_dbg(dev, "%s - set BREAK\n", __func__); | |
263 | } else { | |
264 | private->line_signals &= ~UPD78F0730_BREAK; | |
265 | dev_dbg(dev, "%s - clear BREAK\n", __func__); | |
266 | } | |
267 | request.opcode = UPD78F0730_CMD_SET_DTR_RTS; | |
268 | request.params = private->line_signals; | |
269 | ||
270 | upd78f0730_send_ctl(port, &request, sizeof(request)); | |
271 | mutex_unlock(&private->lock); | |
272 | } | |
273 | ||
274 | static void upd78f0730_dtr_rts(struct usb_serial_port *port, int on) | |
275 | { | |
276 | struct tty_struct *tty = port->port.tty; | |
277 | unsigned int set = 0; | |
278 | unsigned int clear = 0; | |
279 | ||
280 | if (on) | |
281 | set = TIOCM_DTR | TIOCM_RTS; | |
282 | else | |
283 | clear = TIOCM_DTR | TIOCM_RTS; | |
284 | ||
285 | upd78f0730_tiocmset(tty, set, clear); | |
286 | } | |
287 | ||
288 | static speed_t upd78f0730_get_baud_rate(struct tty_struct *tty) | |
289 | { | |
290 | const speed_t baud_rate = tty_get_baud_rate(tty); | |
291 | const speed_t supported[] = { | |
89fd8ee8 | 292 | 0, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 153600 |
ea534e0b MS |
293 | }; |
294 | int i; | |
295 | ||
296 | for (i = ARRAY_SIZE(supported) - 1; i >= 0; i--) { | |
297 | if (baud_rate == supported[i]) | |
298 | return baud_rate; | |
299 | } | |
300 | ||
301 | /* If the baud rate is not supported, switch to the default one */ | |
302 | tty_encode_baud_rate(tty, 9600, 9600); | |
303 | ||
304 | return tty_get_baud_rate(tty); | |
305 | } | |
306 | ||
307 | static void upd78f0730_set_termios(struct tty_struct *tty, | |
308 | struct usb_serial_port *port, | |
309 | struct ktermios *old_termios) | |
310 | { | |
311 | struct device *dev = &port->dev; | |
312 | struct upd78f0730_line_control request; | |
313 | speed_t baud_rate; | |
314 | ||
315 | if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios)) | |
316 | return; | |
317 | ||
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); | |
322 | ||
323 | baud_rate = upd78f0730_get_baud_rate(tty); | |
324 | request.opcode = UPD78F0730_CMD_LINE_CONTROL; | |
325 | request.baud_rate = cpu_to_le32(baud_rate); | |
326 | request.params = 0; | |
327 | dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud_rate); | |
328 | ||
329 | switch (C_CSIZE(tty)) { | |
330 | case CS7: | |
331 | request.params |= UPD78F0730_DATA_SIZE_7_BITS; | |
332 | dev_dbg(dev, "%s - 7 data bits\n", __func__); | |
333 | break; | |
334 | default: | |
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"); | |
338 | /* fall through */ | |
339 | case CS8: | |
340 | request.params |= UPD78F0730_DATA_SIZE_8_BITS; | |
341 | dev_dbg(dev, "%s - 8 data bits\n", __func__); | |
342 | break; | |
343 | } | |
344 | ||
345 | if (C_PARENB(tty)) { | |
346 | if (C_PARODD(tty)) { | |
347 | request.params |= UPD78F0730_PARITY_ODD; | |
348 | dev_dbg(dev, "%s - odd parity\n", __func__); | |
349 | } else { | |
350 | request.params |= UPD78F0730_PARITY_EVEN; | |
351 | dev_dbg(dev, "%s - even parity\n", __func__); | |
352 | } | |
353 | ||
354 | if (C_CMSPAR(tty)) { | |
355 | tty->termios.c_cflag &= ~CMSPAR; | |
356 | dev_warn(dev, "MARK/SPACE parity is not supported\n"); | |
357 | } | |
358 | } else { | |
359 | request.params |= UPD78F0730_PARITY_NONE; | |
360 | dev_dbg(dev, "%s - no parity\n", __func__); | |
361 | } | |
362 | ||
363 | if (C_CSTOPB(tty)) { | |
364 | request.params |= UPD78F0730_STOP_BIT_2_BIT; | |
365 | dev_dbg(dev, "%s - 2 stop bits\n", __func__); | |
366 | } else { | |
367 | request.params |= UPD78F0730_STOP_BIT_1_BIT; | |
368 | dev_dbg(dev, "%s - 1 stop bit\n", __func__); | |
369 | } | |
370 | ||
371 | if (C_CRTSCTS(tty)) { | |
372 | tty->termios.c_cflag &= ~CRTSCTS; | |
373 | dev_warn(dev, "RTSCTS flow control is not supported\n"); | |
374 | } | |
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"); | |
378 | } | |
379 | request.params |= UPD78F0730_FLOW_CONTROL_NONE; | |
380 | dev_dbg(dev, "%s - no flow control\n", __func__); | |
381 | ||
382 | upd78f0730_send_ctl(port, &request, sizeof(request)); | |
383 | } | |
384 | ||
385 | static int upd78f0730_open(struct tty_struct *tty, struct usb_serial_port *port) | |
386 | { | |
387 | struct upd78f0730_open_close request = { | |
388 | .opcode = UPD78F0730_CMD_OPEN_CLOSE, | |
389 | .state = UPD78F0730_PORT_OPEN | |
390 | }; | |
391 | int res; | |
392 | ||
393 | res = upd78f0730_send_ctl(port, &request, sizeof(request)); | |
394 | if (res) | |
395 | return res; | |
396 | ||
397 | if (tty) | |
398 | upd78f0730_set_termios(tty, port, NULL); | |
399 | ||
400 | return usb_serial_generic_open(tty, port); | |
401 | } | |
402 | ||
403 | static void upd78f0730_close(struct usb_serial_port *port) | |
404 | { | |
405 | struct upd78f0730_open_close request = { | |
406 | .opcode = UPD78F0730_CMD_OPEN_CLOSE, | |
407 | .state = UPD78F0730_PORT_CLOSE | |
408 | }; | |
409 | ||
410 | usb_serial_generic_close(port); | |
411 | upd78f0730_send_ctl(port, &request, sizeof(request)); | |
412 | } | |
413 | ||
414 | static struct usb_serial_driver upd78f0730_device = { | |
415 | .driver = { | |
416 | .owner = THIS_MODULE, | |
417 | .name = "upd78f0730", | |
418 | }, | |
419 | .id_table = id_table, | |
420 | .num_ports = 1, | |
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, | |
430 | }; | |
431 | ||
432 | static struct usb_serial_driver * const serial_drivers[] = { | |
433 | &upd78f0730_device, | |
434 | NULL | |
435 | }; | |
436 | ||
437 | module_usb_serial_driver(serial_drivers, id_table); | |
438 | ||
439 | MODULE_DESCRIPTION(DRIVER_DESC); | |
440 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
441 | MODULE_LICENSE("GPL v2"); |