Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Driver core for serial ports |
3 | * | |
4 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. | |
5 | * | |
6 | * Copyright 1999 ARM Limited | |
7 | * Copyright (C) 2000-2001 Deep Blue Solutions Ltd. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | */ | |
1da177e4 LT |
23 | #include <linux/module.h> |
24 | #include <linux/tty.h> | |
027d7dac | 25 | #include <linux/tty_flip.h> |
1da177e4 LT |
26 | #include <linux/slab.h> |
27 | #include <linux/init.h> | |
28 | #include <linux/console.h> | |
a208ffd2 | 29 | #include <linux/of.h> |
d196a949 AD |
30 | #include <linux/proc_fs.h> |
31 | #include <linux/seq_file.h> | |
1da177e4 LT |
32 | #include <linux/device.h> |
33 | #include <linux/serial.h> /* for serial_state and serial_icounter_struct */ | |
ccce6deb | 34 | #include <linux/serial_core.h> |
1da177e4 | 35 | #include <linux/delay.h> |
f392ecfa | 36 | #include <linux/mutex.h> |
1da177e4 LT |
37 | |
38 | #include <asm/irq.h> | |
39 | #include <asm/uaccess.h> | |
40 | ||
1da177e4 LT |
41 | /* |
42 | * This is used to lock changes in serial line configuration. | |
43 | */ | |
f392ecfa | 44 | static DEFINE_MUTEX(port_mutex); |
1da177e4 | 45 | |
13e83599 IM |
46 | /* |
47 | * lockdep: port->lock is initialized in two places, but we | |
48 | * want only one lock-class: | |
49 | */ | |
50 | static struct lock_class_key port_lock_key; | |
51 | ||
1da177e4 LT |
52 | #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8) |
53 | ||
19225135 | 54 | static void uart_change_speed(struct tty_struct *tty, struct uart_state *state, |
a46c9994 | 55 | struct ktermios *old_termios); |
1f33a51d | 56 | static void uart_wait_until_sent(struct tty_struct *tty, int timeout); |
6f538fe3 LW |
57 | static void uart_change_pm(struct uart_state *state, |
58 | enum uart_pm_state pm_state); | |
1da177e4 | 59 | |
b922e19d JS |
60 | static void uart_port_shutdown(struct tty_port *port); |
61 | ||
299245a1 PH |
62 | static int uart_dcd_enabled(struct uart_port *uport) |
63 | { | |
d4260b51 | 64 | return !!(uport->status & UPSTAT_DCD_ENABLE); |
299245a1 PH |
65 | } |
66 | ||
1da177e4 LT |
67 | /* |
68 | * This routine is used by the interrupt handler to schedule processing in | |
69 | * the software interrupt portion of the driver. | |
70 | */ | |
71 | void uart_write_wakeup(struct uart_port *port) | |
72 | { | |
ebd2c8f6 | 73 | struct uart_state *state = port->state; |
d5f735e5 PM |
74 | /* |
75 | * This means you called this function _after_ the port was | |
76 | * closed. No cookie for you. | |
77 | */ | |
ebd2c8f6 | 78 | BUG_ON(!state); |
6a3e492b | 79 | tty_wakeup(state->port.tty); |
1da177e4 LT |
80 | } |
81 | ||
82 | static void uart_stop(struct tty_struct *tty) | |
83 | { | |
84 | struct uart_state *state = tty->driver_data; | |
ebd2c8f6 | 85 | struct uart_port *port = state->uart_port; |
1da177e4 LT |
86 | unsigned long flags; |
87 | ||
88 | spin_lock_irqsave(&port->lock, flags); | |
b129a8cc | 89 | port->ops->stop_tx(port); |
1da177e4 LT |
90 | spin_unlock_irqrestore(&port->lock, flags); |
91 | } | |
92 | ||
93 | static void __uart_start(struct tty_struct *tty) | |
94 | { | |
95 | struct uart_state *state = tty->driver_data; | |
ebd2c8f6 | 96 | struct uart_port *port = state->uart_port; |
1da177e4 | 97 | |
d01f4d18 | 98 | if (!uart_tx_stopped(port)) |
b129a8cc | 99 | port->ops->start_tx(port); |
1da177e4 LT |
100 | } |
101 | ||
102 | static void uart_start(struct tty_struct *tty) | |
103 | { | |
104 | struct uart_state *state = tty->driver_data; | |
ebd2c8f6 | 105 | struct uart_port *port = state->uart_port; |
1da177e4 LT |
106 | unsigned long flags; |
107 | ||
108 | spin_lock_irqsave(&port->lock, flags); | |
109 | __uart_start(tty); | |
110 | spin_unlock_irqrestore(&port->lock, flags); | |
111 | } | |
112 | ||
1da177e4 LT |
113 | static inline void |
114 | uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear) | |
115 | { | |
116 | unsigned long flags; | |
117 | unsigned int old; | |
118 | ||
119 | spin_lock_irqsave(&port->lock, flags); | |
120 | old = port->mctrl; | |
121 | port->mctrl = (old & ~clear) | set; | |
122 | if (old != port->mctrl) | |
123 | port->ops->set_mctrl(port, port->mctrl); | |
124 | spin_unlock_irqrestore(&port->lock, flags); | |
125 | } | |
126 | ||
a46c9994 AC |
127 | #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0) |
128 | #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear) | |
1da177e4 LT |
129 | |
130 | /* | |
131 | * Startup the port. This will be called once per open. All calls | |
df4f4dd4 | 132 | * will be serialised by the per-port mutex. |
1da177e4 | 133 | */ |
c0d92be6 JS |
134 | static int uart_port_startup(struct tty_struct *tty, struct uart_state *state, |
135 | int init_hw) | |
1da177e4 | 136 | { |
46d57a44 | 137 | struct uart_port *uport = state->uart_port; |
1da177e4 LT |
138 | unsigned long page; |
139 | int retval = 0; | |
140 | ||
46d57a44 | 141 | if (uport->type == PORT_UNKNOWN) |
c0d92be6 | 142 | return 1; |
1da177e4 | 143 | |
7deb39ed TP |
144 | /* |
145 | * Make sure the device is in D0 state. | |
146 | */ | |
147 | uart_change_pm(state, UART_PM_STATE_ON); | |
148 | ||
1da177e4 LT |
149 | /* |
150 | * Initialise and allocate the transmit and temporary | |
151 | * buffer. | |
152 | */ | |
ebd2c8f6 | 153 | if (!state->xmit.buf) { |
df4f4dd4 | 154 | /* This is protected by the per port mutex */ |
1da177e4 LT |
155 | page = get_zeroed_page(GFP_KERNEL); |
156 | if (!page) | |
157 | return -ENOMEM; | |
158 | ||
ebd2c8f6 AC |
159 | state->xmit.buf = (unsigned char *) page; |
160 | uart_circ_clear(&state->xmit); | |
1da177e4 LT |
161 | } |
162 | ||
46d57a44 | 163 | retval = uport->ops->startup(uport); |
1da177e4 | 164 | if (retval == 0) { |
c7d7abff | 165 | if (uart_console(uport) && uport->cons->cflag) { |
adc8d746 | 166 | tty->termios.c_cflag = uport->cons->cflag; |
c7d7abff JS |
167 | uport->cons->cflag = 0; |
168 | } | |
169 | /* | |
170 | * Initialise the hardware port settings. | |
171 | */ | |
172 | uart_change_speed(tty, state, NULL); | |
1da177e4 | 173 | |
c7d7abff | 174 | if (init_hw) { |
1da177e4 LT |
175 | /* |
176 | * Setup the RTS and DTR signals once the | |
177 | * port is open and ready to respond. | |
178 | */ | |
adc8d746 | 179 | if (tty->termios.c_cflag & CBAUD) |
46d57a44 | 180 | uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR); |
1da177e4 | 181 | } |
1da177e4 LT |
182 | } |
183 | ||
0055197e JS |
184 | /* |
185 | * This is to allow setserial on this port. People may want to set | |
186 | * port/irq/type and then reconfigure the port properly if it failed | |
187 | * now. | |
188 | */ | |
1da177e4 | 189 | if (retval && capable(CAP_SYS_ADMIN)) |
c0d92be6 JS |
190 | return 1; |
191 | ||
192 | return retval; | |
193 | } | |
194 | ||
195 | static int uart_startup(struct tty_struct *tty, struct uart_state *state, | |
196 | int init_hw) | |
197 | { | |
198 | struct tty_port *port = &state->port; | |
199 | int retval; | |
200 | ||
201 | if (port->flags & ASYNC_INITIALIZED) | |
202 | return 0; | |
203 | ||
204 | /* | |
205 | * Set the TTY IO error marker - we will only clear this | |
206 | * once we have successfully opened the port. | |
207 | */ | |
208 | set_bit(TTY_IO_ERROR, &tty->flags); | |
209 | ||
210 | retval = uart_port_startup(tty, state, init_hw); | |
211 | if (!retval) { | |
212 | set_bit(ASYNCB_INITIALIZED, &port->flags); | |
213 | clear_bit(TTY_IO_ERROR, &tty->flags); | |
214 | } else if (retval > 0) | |
1da177e4 LT |
215 | retval = 0; |
216 | ||
217 | return retval; | |
218 | } | |
219 | ||
220 | /* | |
221 | * This routine will shutdown a serial port; interrupts are disabled, and | |
222 | * DTR is dropped if the hangup on close termio flag is on. Calls to | |
223 | * uart_shutdown are serialised by the per-port semaphore. | |
224 | */ | |
19225135 | 225 | static void uart_shutdown(struct tty_struct *tty, struct uart_state *state) |
1da177e4 | 226 | { |
ccce6deb | 227 | struct uart_port *uport = state->uart_port; |
bdc04e31 | 228 | struct tty_port *port = &state->port; |
1da177e4 | 229 | |
1da177e4 | 230 | /* |
ee31b337 | 231 | * Set the TTY IO error marker |
1da177e4 | 232 | */ |
f751928e AC |
233 | if (tty) |
234 | set_bit(TTY_IO_ERROR, &tty->flags); | |
1da177e4 | 235 | |
bdc04e31 | 236 | if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) { |
ee31b337 RK |
237 | /* |
238 | * Turn off DTR and RTS early. | |
239 | */ | |
ae84db96 PH |
240 | if (uart_console(uport) && tty) |
241 | uport->cons->cflag = tty->termios.c_cflag; | |
242 | ||
adc8d746 | 243 | if (!tty || (tty->termios.c_cflag & HUPCL)) |
ccce6deb | 244 | uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS); |
ee31b337 | 245 | |
b922e19d | 246 | uart_port_shutdown(port); |
ee31b337 | 247 | } |
1da177e4 LT |
248 | |
249 | /* | |
d208a3bf DA |
250 | * It's possible for shutdown to be called after suspend if we get |
251 | * a DCD drop (hangup) at just the right time. Clear suspended bit so | |
252 | * we don't try to resume a port that has been shutdown. | |
1da177e4 | 253 | */ |
d208a3bf | 254 | clear_bit(ASYNCB_SUSPENDED, &port->flags); |
1da177e4 LT |
255 | |
256 | /* | |
257 | * Free the transmit buffer page. | |
258 | */ | |
ebd2c8f6 AC |
259 | if (state->xmit.buf) { |
260 | free_page((unsigned long)state->xmit.buf); | |
261 | state->xmit.buf = NULL; | |
1da177e4 | 262 | } |
1da177e4 LT |
263 | } |
264 | ||
265 | /** | |
266 | * uart_update_timeout - update per-port FIFO timeout. | |
267 | * @port: uart_port structure describing the port | |
268 | * @cflag: termios cflag value | |
269 | * @baud: speed of the port | |
270 | * | |
271 | * Set the port FIFO timeout value. The @cflag value should | |
272 | * reflect the actual hardware settings. | |
273 | */ | |
274 | void | |
275 | uart_update_timeout(struct uart_port *port, unsigned int cflag, | |
276 | unsigned int baud) | |
277 | { | |
278 | unsigned int bits; | |
279 | ||
280 | /* byte size and parity */ | |
281 | switch (cflag & CSIZE) { | |
282 | case CS5: | |
283 | bits = 7; | |
284 | break; | |
285 | case CS6: | |
286 | bits = 8; | |
287 | break; | |
288 | case CS7: | |
289 | bits = 9; | |
290 | break; | |
291 | default: | |
292 | bits = 10; | |
a46c9994 | 293 | break; /* CS8 */ |
1da177e4 LT |
294 | } |
295 | ||
296 | if (cflag & CSTOPB) | |
297 | bits++; | |
298 | if (cflag & PARENB) | |
299 | bits++; | |
300 | ||
301 | /* | |
302 | * The total number of bits to be transmitted in the fifo. | |
303 | */ | |
304 | bits = bits * port->fifosize; | |
305 | ||
306 | /* | |
307 | * Figure the timeout to send the above number of bits. | |
308 | * Add .02 seconds of slop | |
309 | */ | |
310 | port->timeout = (HZ * bits) / baud + HZ/50; | |
311 | } | |
312 | ||
313 | EXPORT_SYMBOL(uart_update_timeout); | |
314 | ||
315 | /** | |
316 | * uart_get_baud_rate - return baud rate for a particular port | |
317 | * @port: uart_port structure describing the port in question. | |
318 | * @termios: desired termios settings. | |
319 | * @old: old termios (or NULL) | |
320 | * @min: minimum acceptable baud rate | |
321 | * @max: maximum acceptable baud rate | |
322 | * | |
323 | * Decode the termios structure into a numeric baud rate, | |
324 | * taking account of the magic 38400 baud rate (with spd_* | |
325 | * flags), and mapping the %B0 rate to 9600 baud. | |
326 | * | |
327 | * If the new baud rate is invalid, try the old termios setting. | |
328 | * If it's still invalid, we try 9600 baud. | |
329 | * | |
330 | * Update the @termios structure to reflect the baud rate | |
eb424fd2 AC |
331 | * we're actually going to be using. Don't do this for the case |
332 | * where B0 is requested ("hang up"). | |
1da177e4 LT |
333 | */ |
334 | unsigned int | |
606d099c AC |
335 | uart_get_baud_rate(struct uart_port *port, struct ktermios *termios, |
336 | struct ktermios *old, unsigned int min, unsigned int max) | |
1da177e4 | 337 | { |
f10a2233 JN |
338 | unsigned int try; |
339 | unsigned int baud; | |
340 | unsigned int altbaud; | |
eb424fd2 | 341 | int hung_up = 0; |
0077d45e | 342 | upf_t flags = port->flags & UPF_SPD_MASK; |
1da177e4 | 343 | |
f10a2233 JN |
344 | switch (flags) { |
345 | case UPF_SPD_HI: | |
1da177e4 | 346 | altbaud = 57600; |
f10a2233 JN |
347 | break; |
348 | case UPF_SPD_VHI: | |
1da177e4 | 349 | altbaud = 115200; |
f10a2233 JN |
350 | break; |
351 | case UPF_SPD_SHI: | |
1da177e4 | 352 | altbaud = 230400; |
f10a2233 JN |
353 | break; |
354 | case UPF_SPD_WARP: | |
1da177e4 | 355 | altbaud = 460800; |
f10a2233 JN |
356 | break; |
357 | default: | |
358 | altbaud = 38400; | |
359 | break; | |
360 | } | |
1da177e4 LT |
361 | |
362 | for (try = 0; try < 2; try++) { | |
363 | baud = tty_termios_baud_rate(termios); | |
364 | ||
365 | /* | |
366 | * The spd_hi, spd_vhi, spd_shi, spd_warp kludge... | |
367 | * Die! Die! Die! | |
368 | */ | |
547039ec | 369 | if (try == 0 && baud == 38400) |
1da177e4 LT |
370 | baud = altbaud; |
371 | ||
372 | /* | |
373 | * Special case: B0 rate. | |
374 | */ | |
eb424fd2 AC |
375 | if (baud == 0) { |
376 | hung_up = 1; | |
1da177e4 | 377 | baud = 9600; |
eb424fd2 | 378 | } |
1da177e4 LT |
379 | |
380 | if (baud >= min && baud <= max) | |
381 | return baud; | |
382 | ||
383 | /* | |
384 | * Oops, the quotient was zero. Try again with | |
385 | * the old baud rate if possible. | |
386 | */ | |
387 | termios->c_cflag &= ~CBAUD; | |
388 | if (old) { | |
6d4d67be | 389 | baud = tty_termios_baud_rate(old); |
eb424fd2 AC |
390 | if (!hung_up) |
391 | tty_termios_encode_baud_rate(termios, | |
392 | baud, baud); | |
1da177e4 LT |
393 | old = NULL; |
394 | continue; | |
395 | } | |
396 | ||
397 | /* | |
16ae2a87 AC |
398 | * As a last resort, if the range cannot be met then clip to |
399 | * the nearest chip supported rate. | |
1da177e4 | 400 | */ |
16ae2a87 AC |
401 | if (!hung_up) { |
402 | if (baud <= min) | |
403 | tty_termios_encode_baud_rate(termios, | |
404 | min + 1, min + 1); | |
405 | else | |
406 | tty_termios_encode_baud_rate(termios, | |
407 | max - 1, max - 1); | |
408 | } | |
1da177e4 | 409 | } |
16ae2a87 AC |
410 | /* Should never happen */ |
411 | WARN_ON(1); | |
1da177e4 LT |
412 | return 0; |
413 | } | |
414 | ||
415 | EXPORT_SYMBOL(uart_get_baud_rate); | |
416 | ||
417 | /** | |
418 | * uart_get_divisor - return uart clock divisor | |
419 | * @port: uart_port structure describing the port. | |
420 | * @baud: desired baud rate | |
421 | * | |
422 | * Calculate the uart clock divisor for the port. | |
423 | */ | |
424 | unsigned int | |
425 | uart_get_divisor(struct uart_port *port, unsigned int baud) | |
426 | { | |
427 | unsigned int quot; | |
428 | ||
429 | /* | |
430 | * Old custom speed handling. | |
431 | */ | |
432 | if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) | |
433 | quot = port->custom_divisor; | |
434 | else | |
97d24634 | 435 | quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud); |
1da177e4 LT |
436 | |
437 | return quot; | |
438 | } | |
439 | ||
440 | EXPORT_SYMBOL(uart_get_divisor); | |
441 | ||
7c8ab967 | 442 | /* Caller holds port mutex */ |
19225135 AC |
443 | static void uart_change_speed(struct tty_struct *tty, struct uart_state *state, |
444 | struct ktermios *old_termios) | |
1da177e4 | 445 | { |
ccce6deb | 446 | struct uart_port *uport = state->uart_port; |
606d099c | 447 | struct ktermios *termios; |
391f93f2 | 448 | int hw_stopped; |
1da177e4 LT |
449 | |
450 | /* | |
451 | * If we have no tty, termios, or the port does not exist, | |
452 | * then we can't set the parameters for this port. | |
453 | */ | |
adc8d746 | 454 | if (!tty || uport->type == PORT_UNKNOWN) |
1da177e4 LT |
455 | return; |
456 | ||
adc8d746 | 457 | termios = &tty->termios; |
c18b55fd | 458 | uport->ops->set_termios(uport, termios, old_termios); |
1da177e4 LT |
459 | |
460 | /* | |
299245a1 | 461 | * Set modem status enables based on termios cflag |
1da177e4 | 462 | */ |
299245a1 | 463 | spin_lock_irq(&uport->lock); |
1da177e4 | 464 | if (termios->c_cflag & CRTSCTS) |
299245a1 | 465 | uport->status |= UPSTAT_CTS_ENABLE; |
1da177e4 | 466 | else |
299245a1 | 467 | uport->status &= ~UPSTAT_CTS_ENABLE; |
1da177e4 LT |
468 | |
469 | if (termios->c_cflag & CLOCAL) | |
299245a1 | 470 | uport->status &= ~UPSTAT_DCD_ENABLE; |
1da177e4 | 471 | else |
299245a1 | 472 | uport->status |= UPSTAT_DCD_ENABLE; |
391f93f2 PH |
473 | |
474 | /* reset sw-assisted CTS flow control based on (possibly) new mode */ | |
475 | hw_stopped = uport->hw_stopped; | |
476 | uport->hw_stopped = uart_softcts_mode(uport) && | |
477 | !(uport->ops->get_mctrl(uport) & TIOCM_CTS); | |
478 | if (uport->hw_stopped) { | |
479 | if (!hw_stopped) | |
480 | uport->ops->stop_tx(uport); | |
481 | } else { | |
482 | if (hw_stopped) | |
483 | __uart_start(tty); | |
484 | } | |
299245a1 | 485 | spin_unlock_irq(&uport->lock); |
1da177e4 LT |
486 | } |
487 | ||
19225135 AC |
488 | static inline int __uart_put_char(struct uart_port *port, |
489 | struct circ_buf *circ, unsigned char c) | |
1da177e4 LT |
490 | { |
491 | unsigned long flags; | |
23d22cea | 492 | int ret = 0; |
1da177e4 LT |
493 | |
494 | if (!circ->buf) | |
23d22cea | 495 | return 0; |
1da177e4 LT |
496 | |
497 | spin_lock_irqsave(&port->lock, flags); | |
498 | if (uart_circ_chars_free(circ) != 0) { | |
499 | circ->buf[circ->head] = c; | |
500 | circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1); | |
23d22cea | 501 | ret = 1; |
1da177e4 LT |
502 | } |
503 | spin_unlock_irqrestore(&port->lock, flags); | |
23d22cea | 504 | return ret; |
1da177e4 LT |
505 | } |
506 | ||
23d22cea | 507 | static int uart_put_char(struct tty_struct *tty, unsigned char ch) |
1da177e4 LT |
508 | { |
509 | struct uart_state *state = tty->driver_data; | |
510 | ||
ebd2c8f6 | 511 | return __uart_put_char(state->uart_port, &state->xmit, ch); |
1da177e4 LT |
512 | } |
513 | ||
514 | static void uart_flush_chars(struct tty_struct *tty) | |
515 | { | |
516 | uart_start(tty); | |
517 | } | |
518 | ||
19225135 AC |
519 | static int uart_write(struct tty_struct *tty, |
520 | const unsigned char *buf, int count) | |
1da177e4 LT |
521 | { |
522 | struct uart_state *state = tty->driver_data; | |
d5f735e5 PM |
523 | struct uart_port *port; |
524 | struct circ_buf *circ; | |
1da177e4 LT |
525 | unsigned long flags; |
526 | int c, ret = 0; | |
527 | ||
d5f735e5 PM |
528 | /* |
529 | * This means you called this function _after_ the port was | |
530 | * closed. No cookie for you. | |
531 | */ | |
f751928e | 532 | if (!state) { |
d5f735e5 PM |
533 | WARN_ON(1); |
534 | return -EL3HLT; | |
535 | } | |
536 | ||
ebd2c8f6 AC |
537 | port = state->uart_port; |
538 | circ = &state->xmit; | |
d5f735e5 | 539 | |
1da177e4 LT |
540 | if (!circ->buf) |
541 | return 0; | |
542 | ||
543 | spin_lock_irqsave(&port->lock, flags); | |
544 | while (1) { | |
545 | c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE); | |
546 | if (count < c) | |
547 | c = count; | |
548 | if (c <= 0) | |
549 | break; | |
550 | memcpy(circ->buf + circ->head, buf, c); | |
551 | circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1); | |
552 | buf += c; | |
553 | count -= c; | |
554 | ret += c; | |
555 | } | |
64dbee31 PH |
556 | |
557 | __uart_start(tty); | |
1da177e4 LT |
558 | spin_unlock_irqrestore(&port->lock, flags); |
559 | ||
1da177e4 LT |
560 | return ret; |
561 | } | |
562 | ||
563 | static int uart_write_room(struct tty_struct *tty) | |
564 | { | |
565 | struct uart_state *state = tty->driver_data; | |
f34d7a5b AC |
566 | unsigned long flags; |
567 | int ret; | |
1da177e4 | 568 | |
ebd2c8f6 AC |
569 | spin_lock_irqsave(&state->uart_port->lock, flags); |
570 | ret = uart_circ_chars_free(&state->xmit); | |
571 | spin_unlock_irqrestore(&state->uart_port->lock, flags); | |
f34d7a5b | 572 | return ret; |
1da177e4 LT |
573 | } |
574 | ||
575 | static int uart_chars_in_buffer(struct tty_struct *tty) | |
576 | { | |
577 | struct uart_state *state = tty->driver_data; | |
f34d7a5b AC |
578 | unsigned long flags; |
579 | int ret; | |
1da177e4 | 580 | |
ebd2c8f6 AC |
581 | spin_lock_irqsave(&state->uart_port->lock, flags); |
582 | ret = uart_circ_chars_pending(&state->xmit); | |
583 | spin_unlock_irqrestore(&state->uart_port->lock, flags); | |
f34d7a5b | 584 | return ret; |
1da177e4 LT |
585 | } |
586 | ||
587 | static void uart_flush_buffer(struct tty_struct *tty) | |
588 | { | |
589 | struct uart_state *state = tty->driver_data; | |
55d7b689 | 590 | struct uart_port *port; |
1da177e4 LT |
591 | unsigned long flags; |
592 | ||
d5f735e5 PM |
593 | /* |
594 | * This means you called this function _after_ the port was | |
595 | * closed. No cookie for you. | |
596 | */ | |
f751928e | 597 | if (!state) { |
d5f735e5 PM |
598 | WARN_ON(1); |
599 | return; | |
600 | } | |
601 | ||
ebd2c8f6 | 602 | port = state->uart_port; |
eb3a1e11 | 603 | pr_debug("uart_flush_buffer(%d) called\n", tty->index); |
1da177e4 LT |
604 | |
605 | spin_lock_irqsave(&port->lock, flags); | |
ebd2c8f6 | 606 | uart_circ_clear(&state->xmit); |
6bb0e3a5 HS |
607 | if (port->ops->flush_buffer) |
608 | port->ops->flush_buffer(port); | |
1da177e4 LT |
609 | spin_unlock_irqrestore(&port->lock, flags); |
610 | tty_wakeup(tty); | |
611 | } | |
612 | ||
613 | /* | |
614 | * This function is used to send a high-priority XON/XOFF character to | |
615 | * the device | |
616 | */ | |
617 | static void uart_send_xchar(struct tty_struct *tty, char ch) | |
618 | { | |
619 | struct uart_state *state = tty->driver_data; | |
ebd2c8f6 | 620 | struct uart_port *port = state->uart_port; |
1da177e4 LT |
621 | unsigned long flags; |
622 | ||
623 | if (port->ops->send_xchar) | |
624 | port->ops->send_xchar(port, ch); | |
625 | else { | |
c235ccc1 | 626 | spin_lock_irqsave(&port->lock, flags); |
1da177e4 | 627 | port->x_char = ch; |
c235ccc1 | 628 | if (ch) |
b129a8cc | 629 | port->ops->start_tx(port); |
c235ccc1 | 630 | spin_unlock_irqrestore(&port->lock, flags); |
1da177e4 LT |
631 | } |
632 | } | |
633 | ||
634 | static void uart_throttle(struct tty_struct *tty) | |
635 | { | |
636 | struct uart_state *state = tty->driver_data; | |
9aba8d5b | 637 | struct uart_port *port = state->uart_port; |
391f93f2 | 638 | upstat_t mask = 0; |
1da177e4 LT |
639 | |
640 | if (I_IXOFF(tty)) | |
391f93f2 | 641 | mask |= UPSTAT_AUTOXOFF; |
9aba8d5b | 642 | if (tty->termios.c_cflag & CRTSCTS) |
391f93f2 | 643 | mask |= UPSTAT_AUTORTS; |
9aba8d5b | 644 | |
391f93f2 | 645 | if (port->status & mask) { |
9aba8d5b | 646 | port->ops->throttle(port); |
391f93f2 | 647 | mask &= ~port->status; |
9aba8d5b RK |
648 | } |
649 | ||
391f93f2 | 650 | if (mask & UPSTAT_AUTOXOFF) |
1da177e4 LT |
651 | uart_send_xchar(tty, STOP_CHAR(tty)); |
652 | ||
391f93f2 | 653 | if (mask & UPSTAT_AUTORTS) |
9aba8d5b | 654 | uart_clear_mctrl(port, TIOCM_RTS); |
1da177e4 LT |
655 | } |
656 | ||
657 | static void uart_unthrottle(struct tty_struct *tty) | |
658 | { | |
659 | struct uart_state *state = tty->driver_data; | |
ebd2c8f6 | 660 | struct uart_port *port = state->uart_port; |
391f93f2 | 661 | upstat_t mask = 0; |
1da177e4 | 662 | |
9aba8d5b | 663 | if (I_IXOFF(tty)) |
391f93f2 | 664 | mask |= UPSTAT_AUTOXOFF; |
9aba8d5b | 665 | if (tty->termios.c_cflag & CRTSCTS) |
391f93f2 | 666 | mask |= UPSTAT_AUTORTS; |
9aba8d5b | 667 | |
391f93f2 | 668 | if (port->status & mask) { |
9aba8d5b | 669 | port->ops->unthrottle(port); |
391f93f2 | 670 | mask &= ~port->status; |
9aba8d5b | 671 | } |
1da177e4 | 672 | |
391f93f2 | 673 | if (mask & UPSTAT_AUTOXOFF) |
fba594a8 | 674 | uart_send_xchar(tty, START_CHAR(tty)); |
1da177e4 | 675 | |
391f93f2 | 676 | if (mask & UPSTAT_AUTORTS) |
1da177e4 LT |
677 | uart_set_mctrl(port, TIOCM_RTS); |
678 | } | |
679 | ||
9f109694 | 680 | static void do_uart_get_info(struct tty_port *port, |
7ba2e769 | 681 | struct serial_struct *retinfo) |
1da177e4 | 682 | { |
9f109694 | 683 | struct uart_state *state = container_of(port, struct uart_state, port); |
a2bceae0 | 684 | struct uart_port *uport = state->uart_port; |
f34d7a5b | 685 | |
37cd0c99 | 686 | memset(retinfo, 0, sizeof(*retinfo)); |
f34d7a5b | 687 | |
7ba2e769 AC |
688 | retinfo->type = uport->type; |
689 | retinfo->line = uport->line; | |
690 | retinfo->port = uport->iobase; | |
1da177e4 | 691 | if (HIGH_BITS_OFFSET) |
7ba2e769 AC |
692 | retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET; |
693 | retinfo->irq = uport->irq; | |
694 | retinfo->flags = uport->flags; | |
695 | retinfo->xmit_fifo_size = uport->fifosize; | |
696 | retinfo->baud_base = uport->uartclk / 16; | |
697 | retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10; | |
698 | retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ? | |
1da177e4 | 699 | ASYNC_CLOSING_WAIT_NONE : |
4cb0fbfd | 700 | jiffies_to_msecs(port->closing_wait) / 10; |
7ba2e769 AC |
701 | retinfo->custom_divisor = uport->custom_divisor; |
702 | retinfo->hub6 = uport->hub6; | |
703 | retinfo->io_type = uport->iotype; | |
704 | retinfo->iomem_reg_shift = uport->regshift; | |
705 | retinfo->iomem_base = (void *)(unsigned long)uport->mapbase; | |
706 | } | |
707 | ||
9f109694 AC |
708 | static void uart_get_info(struct tty_port *port, |
709 | struct serial_struct *retinfo) | |
7ba2e769 | 710 | { |
7ba2e769 AC |
711 | /* Ensure the state we copy is consistent and no hardware changes |
712 | occur as we go */ | |
713 | mutex_lock(&port->mutex); | |
9f109694 | 714 | do_uart_get_info(port, retinfo); |
a2bceae0 | 715 | mutex_unlock(&port->mutex); |
9f109694 AC |
716 | } |
717 | ||
718 | static int uart_get_info_user(struct tty_port *port, | |
719 | struct serial_struct __user *retinfo) | |
720 | { | |
721 | struct serial_struct tmp; | |
722 | uart_get_info(port, &tmp); | |
f34d7a5b | 723 | |
1da177e4 LT |
724 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
725 | return -EFAULT; | |
726 | return 0; | |
727 | } | |
728 | ||
7ba2e769 AC |
729 | static int uart_set_info(struct tty_struct *tty, struct tty_port *port, |
730 | struct uart_state *state, | |
731 | struct serial_struct *new_info) | |
1da177e4 | 732 | { |
46d57a44 | 733 | struct uart_port *uport = state->uart_port; |
1da177e4 | 734 | unsigned long new_port; |
0077d45e | 735 | unsigned int change_irq, change_port, closing_wait; |
1da177e4 | 736 | unsigned int old_custom_divisor, close_delay; |
0077d45e | 737 | upf_t old_flags, new_flags; |
1da177e4 LT |
738 | int retval = 0; |
739 | ||
7ba2e769 | 740 | new_port = new_info->port; |
1da177e4 | 741 | if (HIGH_BITS_OFFSET) |
7ba2e769 | 742 | new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET; |
1da177e4 | 743 | |
7ba2e769 AC |
744 | new_info->irq = irq_canonicalize(new_info->irq); |
745 | close_delay = msecs_to_jiffies(new_info->close_delay * 10); | |
746 | closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ? | |
4cb0fbfd | 747 | ASYNC_CLOSING_WAIT_NONE : |
7ba2e769 | 748 | msecs_to_jiffies(new_info->closing_wait * 10); |
1da177e4 | 749 | |
1da177e4 | 750 | |
46d57a44 | 751 | change_irq = !(uport->flags & UPF_FIXED_PORT) |
7ba2e769 | 752 | && new_info->irq != uport->irq; |
1da177e4 LT |
753 | |
754 | /* | |
755 | * Since changing the 'type' of the port changes its resource | |
756 | * allocations, we should treat type changes the same as | |
757 | * IO port changes. | |
758 | */ | |
46d57a44 AC |
759 | change_port = !(uport->flags & UPF_FIXED_PORT) |
760 | && (new_port != uport->iobase || | |
7ba2e769 AC |
761 | (unsigned long)new_info->iomem_base != uport->mapbase || |
762 | new_info->hub6 != uport->hub6 || | |
763 | new_info->io_type != uport->iotype || | |
764 | new_info->iomem_reg_shift != uport->regshift || | |
765 | new_info->type != uport->type); | |
46d57a44 AC |
766 | |
767 | old_flags = uport->flags; | |
7ba2e769 | 768 | new_flags = new_info->flags; |
46d57a44 | 769 | old_custom_divisor = uport->custom_divisor; |
1da177e4 LT |
770 | |
771 | if (!capable(CAP_SYS_ADMIN)) { | |
772 | retval = -EPERM; | |
773 | if (change_irq || change_port || | |
7ba2e769 | 774 | (new_info->baud_base != uport->uartclk / 16) || |
46d57a44 AC |
775 | (close_delay != port->close_delay) || |
776 | (closing_wait != port->closing_wait) || | |
7ba2e769 AC |
777 | (new_info->xmit_fifo_size && |
778 | new_info->xmit_fifo_size != uport->fifosize) || | |
0077d45e | 779 | (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0)) |
1da177e4 | 780 | goto exit; |
46d57a44 | 781 | uport->flags = ((uport->flags & ~UPF_USR_MASK) | |
0077d45e | 782 | (new_flags & UPF_USR_MASK)); |
7ba2e769 | 783 | uport->custom_divisor = new_info->custom_divisor; |
1da177e4 LT |
784 | goto check_and_exit; |
785 | } | |
786 | ||
787 | /* | |
788 | * Ask the low level driver to verify the settings. | |
789 | */ | |
46d57a44 | 790 | if (uport->ops->verify_port) |
7ba2e769 | 791 | retval = uport->ops->verify_port(uport, new_info); |
1da177e4 | 792 | |
7ba2e769 AC |
793 | if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) || |
794 | (new_info->baud_base < 9600)) | |
1da177e4 LT |
795 | retval = -EINVAL; |
796 | ||
797 | if (retval) | |
798 | goto exit; | |
799 | ||
800 | if (change_port || change_irq) { | |
801 | retval = -EBUSY; | |
802 | ||
803 | /* | |
804 | * Make sure that we are the sole user of this port. | |
805 | */ | |
b58d13a0 | 806 | if (tty_port_users(port) > 1) |
1da177e4 LT |
807 | goto exit; |
808 | ||
809 | /* | |
810 | * We need to shutdown the serial port at the old | |
811 | * port/type/irq combination. | |
812 | */ | |
19225135 | 813 | uart_shutdown(tty, state); |
1da177e4 LT |
814 | } |
815 | ||
816 | if (change_port) { | |
817 | unsigned long old_iobase, old_mapbase; | |
818 | unsigned int old_type, old_iotype, old_hub6, old_shift; | |
819 | ||
46d57a44 AC |
820 | old_iobase = uport->iobase; |
821 | old_mapbase = uport->mapbase; | |
822 | old_type = uport->type; | |
823 | old_hub6 = uport->hub6; | |
824 | old_iotype = uport->iotype; | |
825 | old_shift = uport->regshift; | |
1da177e4 LT |
826 | |
827 | /* | |
828 | * Free and release old regions | |
829 | */ | |
830 | if (old_type != PORT_UNKNOWN) | |
46d57a44 | 831 | uport->ops->release_port(uport); |
1da177e4 | 832 | |
46d57a44 | 833 | uport->iobase = new_port; |
7ba2e769 AC |
834 | uport->type = new_info->type; |
835 | uport->hub6 = new_info->hub6; | |
836 | uport->iotype = new_info->io_type; | |
837 | uport->regshift = new_info->iomem_reg_shift; | |
838 | uport->mapbase = (unsigned long)new_info->iomem_base; | |
1da177e4 LT |
839 | |
840 | /* | |
841 | * Claim and map the new regions | |
842 | */ | |
46d57a44 AC |
843 | if (uport->type != PORT_UNKNOWN) { |
844 | retval = uport->ops->request_port(uport); | |
1da177e4 LT |
845 | } else { |
846 | /* Always success - Jean II */ | |
847 | retval = 0; | |
848 | } | |
849 | ||
850 | /* | |
851 | * If we fail to request resources for the | |
852 | * new port, try to restore the old settings. | |
853 | */ | |
7deb39ed | 854 | if (retval) { |
46d57a44 AC |
855 | uport->iobase = old_iobase; |
856 | uport->type = old_type; | |
857 | uport->hub6 = old_hub6; | |
858 | uport->iotype = old_iotype; | |
859 | uport->regshift = old_shift; | |
860 | uport->mapbase = old_mapbase; | |
1da177e4 | 861 | |
7deb39ed TP |
862 | if (old_type != PORT_UNKNOWN) { |
863 | retval = uport->ops->request_port(uport); | |
864 | /* | |
865 | * If we failed to restore the old settings, | |
866 | * we fail like this. | |
867 | */ | |
868 | if (retval) | |
869 | uport->type = PORT_UNKNOWN; | |
870 | ||
871 | /* | |
872 | * We failed anyway. | |
873 | */ | |
874 | retval = -EBUSY; | |
875 | } | |
876 | ||
a46c9994 AC |
877 | /* Added to return the correct error -Ram Gupta */ |
878 | goto exit; | |
1da177e4 LT |
879 | } |
880 | } | |
881 | ||
abb4a239 | 882 | if (change_irq) |
7ba2e769 | 883 | uport->irq = new_info->irq; |
46d57a44 | 884 | if (!(uport->flags & UPF_FIXED_PORT)) |
7ba2e769 | 885 | uport->uartclk = new_info->baud_base * 16; |
46d57a44 | 886 | uport->flags = (uport->flags & ~UPF_CHANGE_MASK) | |
0077d45e | 887 | (new_flags & UPF_CHANGE_MASK); |
7ba2e769 | 888 | uport->custom_divisor = new_info->custom_divisor; |
46d57a44 AC |
889 | port->close_delay = close_delay; |
890 | port->closing_wait = closing_wait; | |
7ba2e769 AC |
891 | if (new_info->xmit_fifo_size) |
892 | uport->fifosize = new_info->xmit_fifo_size; | |
d6c53c0e | 893 | port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0; |
1da177e4 LT |
894 | |
895 | check_and_exit: | |
896 | retval = 0; | |
46d57a44 | 897 | if (uport->type == PORT_UNKNOWN) |
1da177e4 | 898 | goto exit; |
ccce6deb | 899 | if (port->flags & ASYNC_INITIALIZED) { |
46d57a44 AC |
900 | if (((old_flags ^ uport->flags) & UPF_SPD_MASK) || |
901 | old_custom_divisor != uport->custom_divisor) { | |
1da177e4 LT |
902 | /* |
903 | * If they're setting up a custom divisor or speed, | |
904 | * instead of clearing it, then bitch about it. No | |
905 | * need to rate-limit; it's CAP_SYS_ADMIN only. | |
906 | */ | |
46d57a44 | 907 | if (uport->flags & UPF_SPD_MASK) { |
2f2dafe7 SM |
908 | dev_notice(uport->dev, |
909 | "%s sets custom speed on %s. This is deprecated.\n", | |
910 | current->comm, | |
429b4749 | 911 | tty_name(port->tty)); |
1da177e4 | 912 | } |
19225135 | 913 | uart_change_speed(tty, state, NULL); |
1da177e4 LT |
914 | } |
915 | } else | |
19225135 | 916 | retval = uart_startup(tty, state, 1); |
1da177e4 | 917 | exit: |
7ba2e769 AC |
918 | return retval; |
919 | } | |
920 | ||
921 | static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state, | |
922 | struct serial_struct __user *newinfo) | |
923 | { | |
924 | struct serial_struct new_serial; | |
925 | struct tty_port *port = &state->port; | |
926 | int retval; | |
927 | ||
928 | if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) | |
929 | return -EFAULT; | |
930 | ||
931 | /* | |
932 | * This semaphore protects port->count. It is also | |
933 | * very useful to prevent opens. Also, take the | |
934 | * port configuration semaphore to make sure that a | |
935 | * module insertion/removal doesn't change anything | |
936 | * under us. | |
937 | */ | |
938 | mutex_lock(&port->mutex); | |
939 | retval = uart_set_info(tty, port, state, &new_serial); | |
a2bceae0 | 940 | mutex_unlock(&port->mutex); |
1da177e4 LT |
941 | return retval; |
942 | } | |
943 | ||
19225135 AC |
944 | /** |
945 | * uart_get_lsr_info - get line status register info | |
946 | * @tty: tty associated with the UART | |
947 | * @state: UART being queried | |
948 | * @value: returned modem value | |
949 | * | |
950 | * Note: uart_ioctl protects us against hangups. | |
1da177e4 | 951 | */ |
19225135 AC |
952 | static int uart_get_lsr_info(struct tty_struct *tty, |
953 | struct uart_state *state, unsigned int __user *value) | |
1da177e4 | 954 | { |
46d57a44 | 955 | struct uart_port *uport = state->uart_port; |
1da177e4 LT |
956 | unsigned int result; |
957 | ||
46d57a44 | 958 | result = uport->ops->tx_empty(uport); |
1da177e4 LT |
959 | |
960 | /* | |
961 | * If we're about to load something into the transmit | |
962 | * register, we'll pretend the transmitter isn't empty to | |
963 | * avoid a race condition (depending on when the transmit | |
964 | * interrupt happens). | |
965 | */ | |
46d57a44 | 966 | if (uport->x_char || |
ebd2c8f6 | 967 | ((uart_circ_chars_pending(&state->xmit) > 0) && |
d01f4d18 | 968 | !uart_tx_stopped(uport))) |
1da177e4 | 969 | result &= ~TIOCSER_TEMT; |
a46c9994 | 970 | |
1da177e4 LT |
971 | return put_user(result, value); |
972 | } | |
973 | ||
60b33c13 | 974 | static int uart_tiocmget(struct tty_struct *tty) |
1da177e4 LT |
975 | { |
976 | struct uart_state *state = tty->driver_data; | |
a2bceae0 | 977 | struct tty_port *port = &state->port; |
46d57a44 | 978 | struct uart_port *uport = state->uart_port; |
1da177e4 LT |
979 | int result = -EIO; |
980 | ||
a2bceae0 | 981 | mutex_lock(&port->mutex); |
60b33c13 | 982 | if (!(tty->flags & (1 << TTY_IO_ERROR))) { |
46d57a44 | 983 | result = uport->mctrl; |
46d57a44 AC |
984 | spin_lock_irq(&uport->lock); |
985 | result |= uport->ops->get_mctrl(uport); | |
986 | spin_unlock_irq(&uport->lock); | |
1da177e4 | 987 | } |
a2bceae0 | 988 | mutex_unlock(&port->mutex); |
1da177e4 LT |
989 | |
990 | return result; | |
991 | } | |
992 | ||
993 | static int | |
20b9d177 | 994 | uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear) |
1da177e4 LT |
995 | { |
996 | struct uart_state *state = tty->driver_data; | |
46d57a44 | 997 | struct uart_port *uport = state->uart_port; |
a2bceae0 | 998 | struct tty_port *port = &state->port; |
1da177e4 LT |
999 | int ret = -EIO; |
1000 | ||
a2bceae0 | 1001 | mutex_lock(&port->mutex); |
20b9d177 | 1002 | if (!(tty->flags & (1 << TTY_IO_ERROR))) { |
46d57a44 | 1003 | uart_update_mctrl(uport, set, clear); |
1da177e4 LT |
1004 | ret = 0; |
1005 | } | |
a2bceae0 | 1006 | mutex_unlock(&port->mutex); |
1da177e4 LT |
1007 | return ret; |
1008 | } | |
1009 | ||
9e98966c | 1010 | static int uart_break_ctl(struct tty_struct *tty, int break_state) |
1da177e4 LT |
1011 | { |
1012 | struct uart_state *state = tty->driver_data; | |
a2bceae0 | 1013 | struct tty_port *port = &state->port; |
46d57a44 | 1014 | struct uart_port *uport = state->uart_port; |
1da177e4 | 1015 | |
a2bceae0 | 1016 | mutex_lock(&port->mutex); |
1da177e4 | 1017 | |
46d57a44 AC |
1018 | if (uport->type != PORT_UNKNOWN) |
1019 | uport->ops->break_ctl(uport, break_state); | |
1da177e4 | 1020 | |
a2bceae0 | 1021 | mutex_unlock(&port->mutex); |
9e98966c | 1022 | return 0; |
1da177e4 LT |
1023 | } |
1024 | ||
19225135 | 1025 | static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state) |
1da177e4 | 1026 | { |
46d57a44 | 1027 | struct uart_port *uport = state->uart_port; |
a2bceae0 | 1028 | struct tty_port *port = &state->port; |
1da177e4 LT |
1029 | int flags, ret; |
1030 | ||
1031 | if (!capable(CAP_SYS_ADMIN)) | |
1032 | return -EPERM; | |
1033 | ||
1034 | /* | |
1035 | * Take the per-port semaphore. This prevents count from | |
1036 | * changing, and hence any extra opens of the port while | |
1037 | * we're auto-configuring. | |
1038 | */ | |
a2bceae0 | 1039 | if (mutex_lock_interruptible(&port->mutex)) |
1da177e4 LT |
1040 | return -ERESTARTSYS; |
1041 | ||
1042 | ret = -EBUSY; | |
b58d13a0 | 1043 | if (tty_port_users(port) == 1) { |
19225135 | 1044 | uart_shutdown(tty, state); |
1da177e4 LT |
1045 | |
1046 | /* | |
1047 | * If we already have a port type configured, | |
1048 | * we must release its resources. | |
1049 | */ | |
46d57a44 AC |
1050 | if (uport->type != PORT_UNKNOWN) |
1051 | uport->ops->release_port(uport); | |
1da177e4 LT |
1052 | |
1053 | flags = UART_CONFIG_TYPE; | |
46d57a44 | 1054 | if (uport->flags & UPF_AUTO_IRQ) |
1da177e4 LT |
1055 | flags |= UART_CONFIG_IRQ; |
1056 | ||
1057 | /* | |
1058 | * This will claim the ports resources if | |
1059 | * a port is found. | |
1060 | */ | |
46d57a44 | 1061 | uport->ops->config_port(uport, flags); |
1da177e4 | 1062 | |
19225135 | 1063 | ret = uart_startup(tty, state, 1); |
1da177e4 | 1064 | } |
a2bceae0 | 1065 | mutex_unlock(&port->mutex); |
1da177e4 LT |
1066 | return ret; |
1067 | } | |
1068 | ||
1fdc3106 AS |
1069 | static void uart_enable_ms(struct uart_port *uport) |
1070 | { | |
1071 | /* | |
1072 | * Force modem status interrupts on | |
1073 | */ | |
1074 | if (uport->ops->enable_ms) | |
1075 | uport->ops->enable_ms(uport); | |
1076 | } | |
1077 | ||
1da177e4 LT |
1078 | /* |
1079 | * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change | |
1080 | * - mask passed in arg for lines of interest | |
1081 | * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) | |
1082 | * Caller should use TIOCGICOUNT to see which one it was | |
bdc04e31 AC |
1083 | * |
1084 | * FIXME: This wants extracting into a common all driver implementation | |
1085 | * of TIOCMWAIT using tty_port. | |
1da177e4 LT |
1086 | */ |
1087 | static int | |
1088 | uart_wait_modem_status(struct uart_state *state, unsigned long arg) | |
1089 | { | |
46d57a44 | 1090 | struct uart_port *uport = state->uart_port; |
bdc04e31 | 1091 | struct tty_port *port = &state->port; |
1da177e4 LT |
1092 | DECLARE_WAITQUEUE(wait, current); |
1093 | struct uart_icount cprev, cnow; | |
1094 | int ret; | |
1095 | ||
1096 | /* | |
1097 | * note the counters on entry | |
1098 | */ | |
46d57a44 AC |
1099 | spin_lock_irq(&uport->lock); |
1100 | memcpy(&cprev, &uport->icount, sizeof(struct uart_icount)); | |
1fdc3106 | 1101 | uart_enable_ms(uport); |
46d57a44 | 1102 | spin_unlock_irq(&uport->lock); |
1da177e4 | 1103 | |
bdc04e31 | 1104 | add_wait_queue(&port->delta_msr_wait, &wait); |
1da177e4 | 1105 | for (;;) { |
46d57a44 AC |
1106 | spin_lock_irq(&uport->lock); |
1107 | memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); | |
1108 | spin_unlock_irq(&uport->lock); | |
1da177e4 LT |
1109 | |
1110 | set_current_state(TASK_INTERRUPTIBLE); | |
1111 | ||
1112 | if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || | |
1113 | ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || | |
1114 | ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || | |
1115 | ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { | |
a46c9994 AC |
1116 | ret = 0; |
1117 | break; | |
1da177e4 LT |
1118 | } |
1119 | ||
1120 | schedule(); | |
1121 | ||
1122 | /* see if a signal did it */ | |
1123 | if (signal_pending(current)) { | |
1124 | ret = -ERESTARTSYS; | |
1125 | break; | |
1126 | } | |
1127 | ||
1128 | cprev = cnow; | |
1129 | } | |
97f9f707 | 1130 | __set_current_state(TASK_RUNNING); |
bdc04e31 | 1131 | remove_wait_queue(&port->delta_msr_wait, &wait); |
1da177e4 LT |
1132 | |
1133 | return ret; | |
1134 | } | |
1135 | ||
1136 | /* | |
1137 | * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) | |
1138 | * Return: write counters to the user passed counter struct | |
1139 | * NB: both 1->0 and 0->1 transitions are counted except for | |
1140 | * RI where only 0->1 is counted. | |
1141 | */ | |
d281da7f AC |
1142 | static int uart_get_icount(struct tty_struct *tty, |
1143 | struct serial_icounter_struct *icount) | |
1da177e4 | 1144 | { |
d281da7f | 1145 | struct uart_state *state = tty->driver_data; |
1da177e4 | 1146 | struct uart_icount cnow; |
46d57a44 | 1147 | struct uart_port *uport = state->uart_port; |
1da177e4 | 1148 | |
46d57a44 AC |
1149 | spin_lock_irq(&uport->lock); |
1150 | memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); | |
1151 | spin_unlock_irq(&uport->lock); | |
1da177e4 | 1152 | |
d281da7f AC |
1153 | icount->cts = cnow.cts; |
1154 | icount->dsr = cnow.dsr; | |
1155 | icount->rng = cnow.rng; | |
1156 | icount->dcd = cnow.dcd; | |
1157 | icount->rx = cnow.rx; | |
1158 | icount->tx = cnow.tx; | |
1159 | icount->frame = cnow.frame; | |
1160 | icount->overrun = cnow.overrun; | |
1161 | icount->parity = cnow.parity; | |
1162 | icount->brk = cnow.brk; | |
1163 | icount->buf_overrun = cnow.buf_overrun; | |
1164 | ||
1165 | return 0; | |
1da177e4 LT |
1166 | } |
1167 | ||
a5f276f1 RRD |
1168 | static int uart_get_rs485_config(struct uart_port *port, |
1169 | struct serial_rs485 __user *rs485) | |
1170 | { | |
bd737f87 RRD |
1171 | unsigned long flags; |
1172 | struct serial_rs485 aux; | |
1173 | ||
1174 | spin_lock_irqsave(&port->lock, flags); | |
1175 | aux = port->rs485; | |
1176 | spin_unlock_irqrestore(&port->lock, flags); | |
1177 | ||
1178 | if (copy_to_user(rs485, &aux, sizeof(aux))) | |
a5f276f1 | 1179 | return -EFAULT; |
bd737f87 | 1180 | |
a5f276f1 RRD |
1181 | return 0; |
1182 | } | |
1183 | ||
1184 | static int uart_set_rs485_config(struct uart_port *port, | |
1185 | struct serial_rs485 __user *rs485_user) | |
1186 | { | |
1187 | struct serial_rs485 rs485; | |
1188 | int ret; | |
bd737f87 | 1189 | unsigned long flags; |
a5f276f1 RRD |
1190 | |
1191 | if (!port->rs485_config) | |
1192 | return -ENOIOCTLCMD; | |
1193 | ||
1194 | if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user))) | |
1195 | return -EFAULT; | |
1196 | ||
bd737f87 | 1197 | spin_lock_irqsave(&port->lock, flags); |
a5f276f1 | 1198 | ret = port->rs485_config(port, &rs485); |
bd737f87 | 1199 | spin_unlock_irqrestore(&port->lock, flags); |
a5f276f1 RRD |
1200 | if (ret) |
1201 | return ret; | |
1202 | ||
1203 | if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485))) | |
1204 | return -EFAULT; | |
1205 | ||
1206 | return 0; | |
1207 | } | |
1208 | ||
1da177e4 | 1209 | /* |
e5238442 | 1210 | * Called via sys_ioctl. We can use spin_lock_irq() here. |
1da177e4 LT |
1211 | */ |
1212 | static int | |
6caa76b7 | 1213 | uart_ioctl(struct tty_struct *tty, unsigned int cmd, |
1da177e4 LT |
1214 | unsigned long arg) |
1215 | { | |
1216 | struct uart_state *state = tty->driver_data; | |
a2bceae0 | 1217 | struct tty_port *port = &state->port; |
1da177e4 LT |
1218 | void __user *uarg = (void __user *)arg; |
1219 | int ret = -ENOIOCTLCMD; | |
1220 | ||
1da177e4 LT |
1221 | |
1222 | /* | |
1223 | * These ioctls don't rely on the hardware to be present. | |
1224 | */ | |
1225 | switch (cmd) { | |
1226 | case TIOCGSERIAL: | |
9f109694 | 1227 | ret = uart_get_info_user(port, uarg); |
1da177e4 LT |
1228 | break; |
1229 | ||
1230 | case TIOCSSERIAL: | |
7c8ab967 | 1231 | down_write(&tty->termios_rwsem); |
7ba2e769 | 1232 | ret = uart_set_info_user(tty, state, uarg); |
7c8ab967 | 1233 | up_write(&tty->termios_rwsem); |
1da177e4 LT |
1234 | break; |
1235 | ||
1236 | case TIOCSERCONFIG: | |
7c8ab967 | 1237 | down_write(&tty->termios_rwsem); |
19225135 | 1238 | ret = uart_do_autoconfig(tty, state); |
7c8ab967 | 1239 | up_write(&tty->termios_rwsem); |
1da177e4 LT |
1240 | break; |
1241 | ||
1242 | case TIOCSERGWILD: /* obsolete */ | |
1243 | case TIOCSERSWILD: /* obsolete */ | |
1244 | ret = 0; | |
1245 | break; | |
1246 | } | |
1247 | ||
1248 | if (ret != -ENOIOCTLCMD) | |
1249 | goto out; | |
1250 | ||
1251 | if (tty->flags & (1 << TTY_IO_ERROR)) { | |
1252 | ret = -EIO; | |
1253 | goto out; | |
1254 | } | |
1255 | ||
1256 | /* | |
1257 | * The following should only be used when hardware is present. | |
1258 | */ | |
1259 | switch (cmd) { | |
1260 | case TIOCMIWAIT: | |
1261 | ret = uart_wait_modem_status(state, arg); | |
1262 | break; | |
1da177e4 LT |
1263 | } |
1264 | ||
1265 | if (ret != -ENOIOCTLCMD) | |
1266 | goto out; | |
1267 | ||
a2bceae0 | 1268 | mutex_lock(&port->mutex); |
1da177e4 | 1269 | |
6caa76b7 | 1270 | if (tty->flags & (1 << TTY_IO_ERROR)) { |
1da177e4 LT |
1271 | ret = -EIO; |
1272 | goto out_up; | |
1273 | } | |
1274 | ||
1275 | /* | |
1276 | * All these rely on hardware being present and need to be | |
1277 | * protected against the tty being hung up. | |
1278 | */ | |
a9c20a9c | 1279 | |
1da177e4 | 1280 | switch (cmd) { |
a9c20a9c RRD |
1281 | case TIOCSERGETLSR: /* Get line status register */ |
1282 | ret = uart_get_lsr_info(tty, state, uarg); | |
1283 | break; | |
1284 | ||
a5f276f1 RRD |
1285 | case TIOCGRS485: |
1286 | ret = uart_get_rs485_config(state->uart_port, uarg); | |
1287 | break; | |
1288 | ||
1289 | case TIOCSRS485: | |
1290 | ret = uart_set_rs485_config(state->uart_port, uarg); | |
1291 | break; | |
1da177e4 | 1292 | default: { |
46d57a44 AC |
1293 | struct uart_port *uport = state->uart_port; |
1294 | if (uport->ops->ioctl) | |
1295 | ret = uport->ops->ioctl(uport, cmd, arg); | |
1da177e4 LT |
1296 | break; |
1297 | } | |
1298 | } | |
f34d7a5b | 1299 | out_up: |
a2bceae0 | 1300 | mutex_unlock(&port->mutex); |
f34d7a5b | 1301 | out: |
1da177e4 LT |
1302 | return ret; |
1303 | } | |
1304 | ||
edeb280e | 1305 | static void uart_set_ldisc(struct tty_struct *tty) |
64e9159f AC |
1306 | { |
1307 | struct uart_state *state = tty->driver_data; | |
46d57a44 | 1308 | struct uart_port *uport = state->uart_port; |
64e9159f | 1309 | |
db1b9dfc PH |
1310 | if (uport->ops->set_ldisc) { |
1311 | mutex_lock(&state->port.mutex); | |
732a84a0 | 1312 | uport->ops->set_ldisc(uport, &tty->termios); |
db1b9dfc PH |
1313 | mutex_unlock(&state->port.mutex); |
1314 | } | |
64e9159f AC |
1315 | } |
1316 | ||
a46c9994 AC |
1317 | static void uart_set_termios(struct tty_struct *tty, |
1318 | struct ktermios *old_termios) | |
1da177e4 LT |
1319 | { |
1320 | struct uart_state *state = tty->driver_data; | |
dec94e70 | 1321 | struct uart_port *uport = state->uart_port; |
adc8d746 | 1322 | unsigned int cflag = tty->termios.c_cflag; |
2cbacafd RK |
1323 | unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK; |
1324 | bool sw_changed = false; | |
1da177e4 | 1325 | |
2cbacafd RK |
1326 | /* |
1327 | * Drivers doing software flow control also need to know | |
1328 | * about changes to these input settings. | |
1329 | */ | |
1330 | if (uport->flags & UPF_SOFT_FLOW) { | |
1331 | iflag_mask |= IXANY|IXON|IXOFF; | |
1332 | sw_changed = | |
1333 | tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] || | |
1334 | tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP]; | |
1335 | } | |
1da177e4 LT |
1336 | |
1337 | /* | |
1338 | * These are the bits that are used to setup various | |
20620d68 DW |
1339 | * flags in the low level driver. We can ignore the Bfoo |
1340 | * bits in c_cflag; c_[io]speed will always be set | |
1341 | * appropriately by set_termios() in tty_ioctl.c | |
1da177e4 | 1342 | */ |
1da177e4 | 1343 | if ((cflag ^ old_termios->c_cflag) == 0 && |
adc8d746 AC |
1344 | tty->termios.c_ospeed == old_termios->c_ospeed && |
1345 | tty->termios.c_ispeed == old_termios->c_ispeed && | |
2cbacafd RK |
1346 | ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 && |
1347 | !sw_changed) { | |
1da177e4 | 1348 | return; |
e5238442 | 1349 | } |
1da177e4 | 1350 | |
7c8ab967 | 1351 | mutex_lock(&state->port.mutex); |
19225135 | 1352 | uart_change_speed(tty, state, old_termios); |
7c8ab967 | 1353 | mutex_unlock(&state->port.mutex); |
c18b55fd PH |
1354 | /* reload cflag from termios; port driver may have overriden flags */ |
1355 | cflag = tty->termios.c_cflag; | |
1da177e4 LT |
1356 | |
1357 | /* Handle transition to B0 status */ | |
1358 | if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) | |
dec94e70 | 1359 | uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR); |
1da177e4 | 1360 | /* Handle transition away from B0 status */ |
82cb7ba1 | 1361 | else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) { |
1da177e4 | 1362 | unsigned int mask = TIOCM_DTR; |
99abf3b9 | 1363 | if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags)) |
1da177e4 | 1364 | mask |= TIOCM_RTS; |
dec94e70 | 1365 | uart_set_mctrl(uport, mask); |
1da177e4 | 1366 | } |
1da177e4 LT |
1367 | } |
1368 | ||
1369 | /* | |
ef4f527c KC |
1370 | * Calls to uart_close() are serialised via the tty_lock in |
1371 | * drivers/tty/tty_io.c:tty_release() | |
1372 | * drivers/tty/tty_io.c:do_tty_hangup() | |
1373 | * This runs from a workqueue and can sleep for a _short_ time only. | |
1da177e4 LT |
1374 | */ |
1375 | static void uart_close(struct tty_struct *tty, struct file *filp) | |
1376 | { | |
1377 | struct uart_state *state = tty->driver_data; | |
46d57a44 AC |
1378 | struct tty_port *port; |
1379 | struct uart_port *uport; | |
61cd8a21 | 1380 | unsigned long flags; |
a46c9994 | 1381 | |
91b32f54 PH |
1382 | if (!state) { |
1383 | struct uart_driver *drv = tty->driver->driver_state; | |
1384 | ||
1385 | state = drv->state + tty->index; | |
1386 | port = &state->port; | |
1387 | spin_lock_irq(&port->lock); | |
1388 | --port->count; | |
1389 | spin_unlock_irq(&port->lock); | |
eea7e17e | 1390 | return; |
91b32f54 | 1391 | } |
eea7e17e | 1392 | |
46d57a44 AC |
1393 | uport = state->uart_port; |
1394 | port = &state->port; | |
1da177e4 | 1395 | |
4ed94cd4 | 1396 | pr_debug("uart_close(%d) called\n", uport ? uport->line : -1); |
1da177e4 | 1397 | |
4ed94cd4 | 1398 | if (!port->count || tty_port_close_start(port, tty, filp) == 0) |
55956216 | 1399 | return; |
1da177e4 LT |
1400 | |
1401 | /* | |
1402 | * At this point, we stop accepting input. To do this, we | |
1403 | * disable the receive line status interrupts. | |
1404 | */ | |
ccce6deb | 1405 | if (port->flags & ASYNC_INITIALIZED) { |
1da177e4 | 1406 | unsigned long flags; |
eea7e17e | 1407 | spin_lock_irqsave(&uport->lock, flags); |
46d57a44 | 1408 | uport->ops->stop_rx(uport); |
eea7e17e | 1409 | spin_unlock_irqrestore(&uport->lock, flags); |
1da177e4 LT |
1410 | /* |
1411 | * Before we drop DTR, make sure the UART transmitter | |
1412 | * has completely drained; this is especially | |
1413 | * important if there is a transmit FIFO! | |
1414 | */ | |
1f33a51d | 1415 | uart_wait_until_sent(tty, uport->timeout); |
1da177e4 LT |
1416 | } |
1417 | ||
bafb0bd2 | 1418 | mutex_lock(&port->mutex); |
19225135 | 1419 | uart_shutdown(tty, state); |
7b01478f | 1420 | tty_port_tty_set(port, NULL); |
61cd8a21 | 1421 | tty->closing = 0; |
ddc7b758 | 1422 | spin_lock_irqsave(&port->lock, flags); |
1da177e4 | 1423 | |
46d57a44 | 1424 | if (port->blocked_open) { |
61cd8a21 | 1425 | spin_unlock_irqrestore(&port->lock, flags); |
46d57a44 | 1426 | if (port->close_delay) |
74866e75 | 1427 | msleep_interruptible(jiffies_to_msecs(port->close_delay)); |
61cd8a21 | 1428 | spin_lock_irqsave(&port->lock, flags); |
46d57a44 | 1429 | } else if (!uart_console(uport)) { |
61cd8a21 | 1430 | spin_unlock_irqrestore(&port->lock, flags); |
6f538fe3 | 1431 | uart_change_pm(state, UART_PM_STATE_OFF); |
61cd8a21 | 1432 | spin_lock_irqsave(&port->lock, flags); |
1da177e4 LT |
1433 | } |
1434 | ||
1435 | /* | |
1436 | * Wake up anyone trying to open this port. | |
1437 | */ | |
ccce6deb | 1438 | clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags); |
426929f8 | 1439 | clear_bit(ASYNCB_CLOSING, &port->flags); |
61cd8a21 | 1440 | spin_unlock_irqrestore(&port->lock, flags); |
46d57a44 | 1441 | wake_up_interruptible(&port->open_wait); |
426929f8 | 1442 | wake_up_interruptible(&port->close_wait); |
1da177e4 | 1443 | |
a2bceae0 | 1444 | mutex_unlock(&port->mutex); |
2e758910 PH |
1445 | |
1446 | tty_ldisc_flush(tty); | |
1da177e4 LT |
1447 | } |
1448 | ||
1f33a51d | 1449 | static void uart_wait_until_sent(struct tty_struct *tty, int timeout) |
1da177e4 | 1450 | { |
1f33a51d JS |
1451 | struct uart_state *state = tty->driver_data; |
1452 | struct uart_port *port = state->uart_port; | |
1da177e4 LT |
1453 | unsigned long char_time, expire; |
1454 | ||
1da177e4 LT |
1455 | if (port->type == PORT_UNKNOWN || port->fifosize == 0) |
1456 | return; | |
1457 | ||
1458 | /* | |
1459 | * Set the check interval to be 1/5 of the estimated time to | |
1460 | * send a single character, and make it at least 1. The check | |
1461 | * interval should also be less than the timeout. | |
1462 | * | |
1463 | * Note: we have to use pretty tight timings here to satisfy | |
1464 | * the NIST-PCTS. | |
1465 | */ | |
1466 | char_time = (port->timeout - HZ/50) / port->fifosize; | |
1467 | char_time = char_time / 5; | |
1468 | if (char_time == 0) | |
1469 | char_time = 1; | |
1470 | if (timeout && timeout < char_time) | |
1471 | char_time = timeout; | |
1472 | ||
1473 | /* | |
1474 | * If the transmitter hasn't cleared in twice the approximate | |
1475 | * amount of time to send the entire FIFO, it probably won't | |
1476 | * ever clear. This assumes the UART isn't doing flow | |
1477 | * control, which is currently the case. Hence, if it ever | |
1478 | * takes longer than port->timeout, this is probably due to a | |
1479 | * UART bug of some kind. So, we clamp the timeout parameter at | |
1480 | * 2*port->timeout. | |
1481 | */ | |
1482 | if (timeout == 0 || timeout > 2 * port->timeout) | |
1483 | timeout = 2 * port->timeout; | |
1484 | ||
1485 | expire = jiffies + timeout; | |
1486 | ||
eb3a1e11 | 1487 | pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n", |
a46c9994 | 1488 | port->line, jiffies, expire); |
1da177e4 LT |
1489 | |
1490 | /* | |
1491 | * Check whether the transmitter is empty every 'char_time'. | |
1492 | * 'timeout' / 'expire' give us the maximum amount of time | |
1493 | * we wait. | |
1494 | */ | |
1495 | while (!port->ops->tx_empty(port)) { | |
1496 | msleep_interruptible(jiffies_to_msecs(char_time)); | |
1497 | if (signal_pending(current)) | |
1498 | break; | |
1499 | if (time_after(jiffies, expire)) | |
1500 | break; | |
1501 | } | |
20365219 AB |
1502 | } |
1503 | ||
1da177e4 | 1504 | /* |
ef4f527c KC |
1505 | * Calls to uart_hangup() are serialised by the tty_lock in |
1506 | * drivers/tty/tty_io.c:do_tty_hangup() | |
1507 | * This runs from a workqueue and can sleep for a _short_ time only. | |
1da177e4 LT |
1508 | */ |
1509 | static void uart_hangup(struct tty_struct *tty) | |
1510 | { | |
1511 | struct uart_state *state = tty->driver_data; | |
46d57a44 | 1512 | struct tty_port *port = &state->port; |
61cd8a21 | 1513 | unsigned long flags; |
1da177e4 | 1514 | |
ebd2c8f6 | 1515 | pr_debug("uart_hangup(%d)\n", state->uart_port->line); |
1da177e4 | 1516 | |
a2bceae0 | 1517 | mutex_lock(&port->mutex); |
ccce6deb | 1518 | if (port->flags & ASYNC_NORMAL_ACTIVE) { |
1da177e4 | 1519 | uart_flush_buffer(tty); |
19225135 | 1520 | uart_shutdown(tty, state); |
61cd8a21 | 1521 | spin_lock_irqsave(&port->lock, flags); |
91312cdb | 1522 | port->count = 0; |
ccce6deb | 1523 | clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags); |
61cd8a21 | 1524 | spin_unlock_irqrestore(&port->lock, flags); |
7b01478f | 1525 | tty_port_tty_set(port, NULL); |
bf903c0c GU |
1526 | if (!uart_console(state->uart_port)) |
1527 | uart_change_pm(state, UART_PM_STATE_OFF); | |
46d57a44 | 1528 | wake_up_interruptible(&port->open_wait); |
bdc04e31 | 1529 | wake_up_interruptible(&port->delta_msr_wait); |
1da177e4 | 1530 | } |
a2bceae0 | 1531 | mutex_unlock(&port->mutex); |
1da177e4 LT |
1532 | } |
1533 | ||
0b1db830 JS |
1534 | static int uart_port_activate(struct tty_port *port, struct tty_struct *tty) |
1535 | { | |
1536 | return 0; | |
1537 | } | |
1538 | ||
1539 | static void uart_port_shutdown(struct tty_port *port) | |
1540 | { | |
b922e19d JS |
1541 | struct uart_state *state = container_of(port, struct uart_state, port); |
1542 | struct uart_port *uport = state->uart_port; | |
1543 | ||
1544 | /* | |
1545 | * clear delta_msr_wait queue to avoid mem leaks: we may free | |
1546 | * the irq here so the queue might never be woken up. Note | |
1547 | * that we won't end up waiting on delta_msr_wait again since | |
1548 | * any outstanding file descriptors should be pointing at | |
1549 | * hung_up_tty_fops now. | |
1550 | */ | |
1551 | wake_up_interruptible(&port->delta_msr_wait); | |
1552 | ||
1553 | /* | |
1554 | * Free the IRQ and disable the port. | |
1555 | */ | |
1556 | uport->ops->shutdown(uport); | |
1557 | ||
1558 | /* | |
1559 | * Ensure that the IRQ handler isn't running on another CPU. | |
1560 | */ | |
1561 | synchronize_irq(uport->irq); | |
0b1db830 JS |
1562 | } |
1563 | ||
de0c8cb3 AC |
1564 | static int uart_carrier_raised(struct tty_port *port) |
1565 | { | |
1566 | struct uart_state *state = container_of(port, struct uart_state, port); | |
1567 | struct uart_port *uport = state->uart_port; | |
1568 | int mctrl; | |
de0c8cb3 | 1569 | spin_lock_irq(&uport->lock); |
1fdc3106 | 1570 | uart_enable_ms(uport); |
de0c8cb3 AC |
1571 | mctrl = uport->ops->get_mctrl(uport); |
1572 | spin_unlock_irq(&uport->lock); | |
de0c8cb3 AC |
1573 | if (mctrl & TIOCM_CAR) |
1574 | return 1; | |
1575 | return 0; | |
1576 | } | |
1577 | ||
1578 | static void uart_dtr_rts(struct tty_port *port, int onoff) | |
1579 | { | |
1580 | struct uart_state *state = container_of(port, struct uart_state, port); | |
1581 | struct uart_port *uport = state->uart_port; | |
24fcc7c8 | 1582 | |
6f5c24ad | 1583 | if (onoff) |
de0c8cb3 AC |
1584 | uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS); |
1585 | else | |
1586 | uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS); | |
de0c8cb3 AC |
1587 | } |
1588 | ||
1da177e4 | 1589 | /* |
ef4f527c KC |
1590 | * Calls to uart_open are serialised by the tty_lock in |
1591 | * drivers/tty/tty_io.c:tty_open() | |
1da177e4 LT |
1592 | * Note that if this fails, then uart_close() _will_ be called. |
1593 | * | |
1594 | * In time, we want to scrap the "opening nonpresent ports" | |
1595 | * behaviour and implement an alternative way for setserial | |
1596 | * to set base addresses/ports/types. This will allow us to | |
1597 | * get rid of a certain amount of extra tests. | |
1598 | */ | |
1599 | static int uart_open(struct tty_struct *tty, struct file *filp) | |
1600 | { | |
1601 | struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state; | |
1da177e4 | 1602 | int retval, line = tty->index; |
1c7b13c4 JS |
1603 | struct uart_state *state = drv->state + line; |
1604 | struct tty_port *port = &state->port; | |
1da177e4 | 1605 | |
eb3a1e11 | 1606 | pr_debug("uart_open(%d) called\n", line); |
1da177e4 | 1607 | |
91b32f54 PH |
1608 | spin_lock_irq(&port->lock); |
1609 | ++port->count; | |
1610 | spin_unlock_irq(&port->lock); | |
1611 | ||
1da177e4 | 1612 | /* |
1c7b13c4 JS |
1613 | * We take the semaphore here to guarantee that we won't be re-entered |
1614 | * while allocating the state structure, or while we request any IRQs | |
1615 | * that the driver may need. This also has the nice side-effect that | |
1616 | * it delays the action of uart_hangup, so we can guarantee that | |
1617 | * state->port.tty will always contain something reasonable. | |
1da177e4 | 1618 | */ |
1c7b13c4 JS |
1619 | if (mutex_lock_interruptible(&port->mutex)) { |
1620 | retval = -ERESTARTSYS; | |
1621 | goto end; | |
1622 | } | |
1623 | ||
1c7b13c4 JS |
1624 | if (!state->uart_port || state->uart_port->flags & UPF_DEAD) { |
1625 | retval = -ENXIO; | |
91b32f54 | 1626 | goto err_unlock; |
1da177e4 LT |
1627 | } |
1628 | ||
1da177e4 | 1629 | tty->driver_data = state; |
ebd2c8f6 | 1630 | state->uart_port->state = state; |
d6c53c0e JS |
1631 | state->port.low_latency = |
1632 | (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0; | |
7b01478f | 1633 | tty_port_tty_set(port, tty); |
1da177e4 | 1634 | |
1da177e4 LT |
1635 | /* |
1636 | * Start up the serial port. | |
1637 | */ | |
19225135 | 1638 | retval = uart_startup(tty, state, 0); |
1da177e4 LT |
1639 | |
1640 | /* | |
1641 | * If we succeeded, wait until the port is ready. | |
1642 | */ | |
61cd8a21 | 1643 | mutex_unlock(&port->mutex); |
1da177e4 | 1644 | if (retval == 0) |
74c21077 | 1645 | retval = tty_port_block_til_ready(port, tty, filp); |
1da177e4 | 1646 | |
1c7b13c4 | 1647 | end: |
1da177e4 | 1648 | return retval; |
91b32f54 | 1649 | err_unlock: |
1c7b13c4 JS |
1650 | mutex_unlock(&port->mutex); |
1651 | goto end; | |
1da177e4 LT |
1652 | } |
1653 | ||
1654 | static const char *uart_type(struct uart_port *port) | |
1655 | { | |
1656 | const char *str = NULL; | |
1657 | ||
1658 | if (port->ops->type) | |
1659 | str = port->ops->type(port); | |
1660 | ||
1661 | if (!str) | |
1662 | str = "unknown"; | |
1663 | ||
1664 | return str; | |
1665 | } | |
1666 | ||
1667 | #ifdef CONFIG_PROC_FS | |
1668 | ||
d196a949 | 1669 | static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i) |
1da177e4 LT |
1670 | { |
1671 | struct uart_state *state = drv->state + i; | |
a2bceae0 | 1672 | struct tty_port *port = &state->port; |
6f538fe3 | 1673 | enum uart_pm_state pm_state; |
a2bceae0 | 1674 | struct uart_port *uport = state->uart_port; |
1da177e4 LT |
1675 | char stat_buf[32]; |
1676 | unsigned int status; | |
d196a949 | 1677 | int mmio; |
1da177e4 | 1678 | |
a2bceae0 | 1679 | if (!uport) |
d196a949 | 1680 | return; |
1da177e4 | 1681 | |
a2bceae0 | 1682 | mmio = uport->iotype >= UPIO_MEM; |
d196a949 | 1683 | seq_printf(m, "%d: uart:%s %s%08llX irq:%d", |
a2bceae0 | 1684 | uport->line, uart_type(uport), |
6c6a2334 | 1685 | mmio ? "mmio:0x" : "port:", |
a2bceae0 AC |
1686 | mmio ? (unsigned long long)uport->mapbase |
1687 | : (unsigned long long)uport->iobase, | |
1688 | uport->irq); | |
1da177e4 | 1689 | |
a2bceae0 | 1690 | if (uport->type == PORT_UNKNOWN) { |
d196a949 AD |
1691 | seq_putc(m, '\n'); |
1692 | return; | |
1da177e4 LT |
1693 | } |
1694 | ||
a46c9994 | 1695 | if (capable(CAP_SYS_ADMIN)) { |
a2bceae0 | 1696 | mutex_lock(&port->mutex); |
3689a0ec | 1697 | pm_state = state->pm_state; |
6f538fe3 LW |
1698 | if (pm_state != UART_PM_STATE_ON) |
1699 | uart_change_pm(state, UART_PM_STATE_ON); | |
a2bceae0 AC |
1700 | spin_lock_irq(&uport->lock); |
1701 | status = uport->ops->get_mctrl(uport); | |
1702 | spin_unlock_irq(&uport->lock); | |
6f538fe3 | 1703 | if (pm_state != UART_PM_STATE_ON) |
3689a0ec | 1704 | uart_change_pm(state, pm_state); |
a2bceae0 | 1705 | mutex_unlock(&port->mutex); |
1da177e4 | 1706 | |
d196a949 | 1707 | seq_printf(m, " tx:%d rx:%d", |
a2bceae0 AC |
1708 | uport->icount.tx, uport->icount.rx); |
1709 | if (uport->icount.frame) | |
d196a949 | 1710 | seq_printf(m, " fe:%d", |
a2bceae0 AC |
1711 | uport->icount.frame); |
1712 | if (uport->icount.parity) | |
d196a949 | 1713 | seq_printf(m, " pe:%d", |
a2bceae0 AC |
1714 | uport->icount.parity); |
1715 | if (uport->icount.brk) | |
d196a949 | 1716 | seq_printf(m, " brk:%d", |
a2bceae0 AC |
1717 | uport->icount.brk); |
1718 | if (uport->icount.overrun) | |
d196a949 | 1719 | seq_printf(m, " oe:%d", |
a2bceae0 | 1720 | uport->icount.overrun); |
a46c9994 AC |
1721 | |
1722 | #define INFOBIT(bit, str) \ | |
a2bceae0 | 1723 | if (uport->mctrl & (bit)) \ |
1da177e4 LT |
1724 | strncat(stat_buf, (str), sizeof(stat_buf) - \ |
1725 | strlen(stat_buf) - 2) | |
a46c9994 | 1726 | #define STATBIT(bit, str) \ |
1da177e4 LT |
1727 | if (status & (bit)) \ |
1728 | strncat(stat_buf, (str), sizeof(stat_buf) - \ | |
1729 | strlen(stat_buf) - 2) | |
1730 | ||
1731 | stat_buf[0] = '\0'; | |
1732 | stat_buf[1] = '\0'; | |
1733 | INFOBIT(TIOCM_RTS, "|RTS"); | |
1734 | STATBIT(TIOCM_CTS, "|CTS"); | |
1735 | INFOBIT(TIOCM_DTR, "|DTR"); | |
1736 | STATBIT(TIOCM_DSR, "|DSR"); | |
1737 | STATBIT(TIOCM_CAR, "|CD"); | |
1738 | STATBIT(TIOCM_RNG, "|RI"); | |
1739 | if (stat_buf[0]) | |
1740 | stat_buf[0] = ' '; | |
a46c9994 | 1741 | |
d196a949 | 1742 | seq_puts(m, stat_buf); |
1da177e4 | 1743 | } |
d196a949 | 1744 | seq_putc(m, '\n'); |
1da177e4 LT |
1745 | #undef STATBIT |
1746 | #undef INFOBIT | |
1da177e4 LT |
1747 | } |
1748 | ||
d196a949 | 1749 | static int uart_proc_show(struct seq_file *m, void *v) |
1da177e4 | 1750 | { |
833bb304 | 1751 | struct tty_driver *ttydrv = m->private; |
1da177e4 | 1752 | struct uart_driver *drv = ttydrv->driver_state; |
d196a949 | 1753 | int i; |
1da177e4 | 1754 | |
d196a949 | 1755 | seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", |
1da177e4 | 1756 | "", "", ""); |
d196a949 AD |
1757 | for (i = 0; i < drv->nr; i++) |
1758 | uart_line_info(m, drv, i); | |
1759 | return 0; | |
1da177e4 | 1760 | } |
d196a949 AD |
1761 | |
1762 | static int uart_proc_open(struct inode *inode, struct file *file) | |
1763 | { | |
d9dda78b | 1764 | return single_open(file, uart_proc_show, PDE_DATA(inode)); |
d196a949 AD |
1765 | } |
1766 | ||
1767 | static const struct file_operations uart_proc_fops = { | |
1768 | .owner = THIS_MODULE, | |
1769 | .open = uart_proc_open, | |
1770 | .read = seq_read, | |
1771 | .llseek = seq_lseek, | |
1772 | .release = single_release, | |
1773 | }; | |
1da177e4 LT |
1774 | #endif |
1775 | ||
4a1b5502 | 1776 | #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) |
1cfe42b7 | 1777 | /** |
d358788f RK |
1778 | * uart_console_write - write a console message to a serial port |
1779 | * @port: the port to write the message | |
1780 | * @s: array of characters | |
1781 | * @count: number of characters in string to write | |
10afbe34 | 1782 | * @putchar: function to write character to port |
d358788f RK |
1783 | */ |
1784 | void uart_console_write(struct uart_port *port, const char *s, | |
1785 | unsigned int count, | |
1786 | void (*putchar)(struct uart_port *, int)) | |
1787 | { | |
1788 | unsigned int i; | |
1789 | ||
1790 | for (i = 0; i < count; i++, s++) { | |
1791 | if (*s == '\n') | |
1792 | putchar(port, '\r'); | |
1793 | putchar(port, *s); | |
1794 | } | |
1795 | } | |
1796 | EXPORT_SYMBOL_GPL(uart_console_write); | |
1797 | ||
1da177e4 LT |
1798 | /* |
1799 | * Check whether an invalid uart number has been specified, and | |
1800 | * if so, search for the first available port that does have | |
1801 | * console support. | |
1802 | */ | |
1803 | struct uart_port * __init | |
1804 | uart_get_console(struct uart_port *ports, int nr, struct console *co) | |
1805 | { | |
1806 | int idx = co->index; | |
1807 | ||
1808 | if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 && | |
1809 | ports[idx].membase == NULL)) | |
1810 | for (idx = 0; idx < nr; idx++) | |
1811 | if (ports[idx].iobase != 0 || | |
1812 | ports[idx].membase != NULL) | |
1813 | break; | |
1814 | ||
1815 | co->index = idx; | |
1816 | ||
1817 | return ports + idx; | |
1818 | } | |
1819 | ||
73abaf87 PH |
1820 | /** |
1821 | * uart_parse_earlycon - Parse earlycon options | |
1822 | * @p: ptr to 2nd field (ie., just beyond '<name>,') | |
1823 | * @iotype: ptr for decoded iotype (out) | |
1824 | * @addr: ptr for decoded mapbase/iobase (out) | |
1825 | * @options: ptr for <options> field; NULL if not present (out) | |
1826 | * | |
1827 | * Decodes earlycon kernel command line parameters of the form | |
6e63be3f NC |
1828 | * earlycon=<name>,io|mmio|mmio32|mmio32be,<addr>,<options> |
1829 | * console=<name>,io|mmio|mmio32|mmio32be,<addr>,<options> | |
73abaf87 PH |
1830 | * |
1831 | * The optional form | |
1832 | * earlycon=<name>,0x<addr>,<options> | |
1833 | * console=<name>,0x<addr>,<options> | |
1834 | * is also accepted; the returned @iotype will be UPIO_MEM. | |
1835 | * | |
1836 | * Returns 0 on success or -EINVAL on failure | |
1837 | */ | |
1838 | int uart_parse_earlycon(char *p, unsigned char *iotype, unsigned long *addr, | |
1839 | char **options) | |
1840 | { | |
1841 | if (strncmp(p, "mmio,", 5) == 0) { | |
1842 | *iotype = UPIO_MEM; | |
1843 | p += 5; | |
1844 | } else if (strncmp(p, "mmio32,", 7) == 0) { | |
1845 | *iotype = UPIO_MEM32; | |
1846 | p += 7; | |
6e63be3f NC |
1847 | } else if (strncmp(p, "mmio32be,", 9) == 0) { |
1848 | *iotype = UPIO_MEM32BE; | |
1849 | p += 9; | |
73abaf87 PH |
1850 | } else if (strncmp(p, "io,", 3) == 0) { |
1851 | *iotype = UPIO_PORT; | |
1852 | p += 3; | |
1853 | } else if (strncmp(p, "0x", 2) == 0) { | |
1854 | *iotype = UPIO_MEM; | |
1855 | } else { | |
1856 | return -EINVAL; | |
1857 | } | |
1858 | ||
1859 | *addr = simple_strtoul(p, NULL, 0); | |
1860 | p = strchr(p, ','); | |
1861 | if (p) | |
1862 | p++; | |
1863 | ||
1864 | *options = p; | |
1865 | return 0; | |
1866 | } | |
1867 | EXPORT_SYMBOL_GPL(uart_parse_earlycon); | |
1868 | ||
1da177e4 | 1869 | /** |
02088ca6 | 1870 | * uart_parse_options - Parse serial port baud/parity/bits/flow control. |
1da177e4 LT |
1871 | * @options: pointer to option string |
1872 | * @baud: pointer to an 'int' variable for the baud rate. | |
1873 | * @parity: pointer to an 'int' variable for the parity. | |
1874 | * @bits: pointer to an 'int' variable for the number of data bits. | |
1875 | * @flow: pointer to an 'int' variable for the flow control character. | |
1876 | * | |
1877 | * uart_parse_options decodes a string containing the serial console | |
1878 | * options. The format of the string is <baud><parity><bits><flow>, | |
1879 | * eg: 115200n8r | |
1880 | */ | |
f2d937f3 | 1881 | void |
1da177e4 LT |
1882 | uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow) |
1883 | { | |
1884 | char *s = options; | |
1885 | ||
1886 | *baud = simple_strtoul(s, NULL, 10); | |
1887 | while (*s >= '0' && *s <= '9') | |
1888 | s++; | |
1889 | if (*s) | |
1890 | *parity = *s++; | |
1891 | if (*s) | |
1892 | *bits = *s++ - '0'; | |
1893 | if (*s) | |
1894 | *flow = *s; | |
1895 | } | |
f2d937f3 | 1896 | EXPORT_SYMBOL_GPL(uart_parse_options); |
1da177e4 LT |
1897 | |
1898 | struct baud_rates { | |
1899 | unsigned int rate; | |
1900 | unsigned int cflag; | |
1901 | }; | |
1902 | ||
cb3592be | 1903 | static const struct baud_rates baud_rates[] = { |
1da177e4 LT |
1904 | { 921600, B921600 }, |
1905 | { 460800, B460800 }, | |
1906 | { 230400, B230400 }, | |
1907 | { 115200, B115200 }, | |
1908 | { 57600, B57600 }, | |
1909 | { 38400, B38400 }, | |
1910 | { 19200, B19200 }, | |
1911 | { 9600, B9600 }, | |
1912 | { 4800, B4800 }, | |
1913 | { 2400, B2400 }, | |
1914 | { 1200, B1200 }, | |
1915 | { 0, B38400 } | |
1916 | }; | |
1917 | ||
1918 | /** | |
1919 | * uart_set_options - setup the serial console parameters | |
1920 | * @port: pointer to the serial ports uart_port structure | |
1921 | * @co: console pointer | |
1922 | * @baud: baud rate | |
1923 | * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even) | |
1924 | * @bits: number of data bits | |
1925 | * @flow: flow control character - 'r' (rts) | |
1926 | */ | |
f2d937f3 | 1927 | int |
1da177e4 LT |
1928 | uart_set_options(struct uart_port *port, struct console *co, |
1929 | int baud, int parity, int bits, int flow) | |
1930 | { | |
606d099c | 1931 | struct ktermios termios; |
149b36ea | 1932 | static struct ktermios dummy; |
1da177e4 LT |
1933 | int i; |
1934 | ||
976ecd12 RK |
1935 | /* |
1936 | * Ensure that the serial console lock is initialised | |
1937 | * early. | |
42b6a1ba RW |
1938 | * If this port is a console, then the spinlock is already |
1939 | * initialised. | |
976ecd12 | 1940 | */ |
42b6a1ba RW |
1941 | if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) { |
1942 | spin_lock_init(&port->lock); | |
1943 | lockdep_set_class(&port->lock, &port_lock_key); | |
1944 | } | |
976ecd12 | 1945 | |
606d099c | 1946 | memset(&termios, 0, sizeof(struct ktermios)); |
1da177e4 LT |
1947 | |
1948 | termios.c_cflag = CREAD | HUPCL | CLOCAL; | |
1949 | ||
1950 | /* | |
1951 | * Construct a cflag setting. | |
1952 | */ | |
1953 | for (i = 0; baud_rates[i].rate; i++) | |
1954 | if (baud_rates[i].rate <= baud) | |
1955 | break; | |
1956 | ||
1957 | termios.c_cflag |= baud_rates[i].cflag; | |
1958 | ||
1959 | if (bits == 7) | |
1960 | termios.c_cflag |= CS7; | |
1961 | else | |
1962 | termios.c_cflag |= CS8; | |
1963 | ||
1964 | switch (parity) { | |
1965 | case 'o': case 'O': | |
1966 | termios.c_cflag |= PARODD; | |
1967 | /*fall through*/ | |
1968 | case 'e': case 'E': | |
1969 | termios.c_cflag |= PARENB; | |
1970 | break; | |
1971 | } | |
1972 | ||
1973 | if (flow == 'r') | |
1974 | termios.c_cflag |= CRTSCTS; | |
1975 | ||
79492689 YL |
1976 | /* |
1977 | * some uarts on other side don't support no flow control. | |
1978 | * So we set * DTR in host uart to make them happy | |
1979 | */ | |
1980 | port->mctrl |= TIOCM_DTR; | |
1981 | ||
149b36ea | 1982 | port->ops->set_termios(port, &termios, &dummy); |
f2d937f3 JW |
1983 | /* |
1984 | * Allow the setting of the UART parameters with a NULL console | |
1985 | * too: | |
1986 | */ | |
1987 | if (co) | |
1988 | co->cflag = termios.c_cflag; | |
1da177e4 LT |
1989 | |
1990 | return 0; | |
1991 | } | |
f2d937f3 | 1992 | EXPORT_SYMBOL_GPL(uart_set_options); |
1da177e4 LT |
1993 | #endif /* CONFIG_SERIAL_CORE_CONSOLE */ |
1994 | ||
cf75525f JS |
1995 | /** |
1996 | * uart_change_pm - set power state of the port | |
1997 | * | |
1998 | * @state: port descriptor | |
1999 | * @pm_state: new state | |
2000 | * | |
2001 | * Locking: port->mutex has to be held | |
2002 | */ | |
6f538fe3 LW |
2003 | static void uart_change_pm(struct uart_state *state, |
2004 | enum uart_pm_state pm_state) | |
1da177e4 | 2005 | { |
ebd2c8f6 | 2006 | struct uart_port *port = state->uart_port; |
1281e360 AV |
2007 | |
2008 | if (state->pm_state != pm_state) { | |
2009 | if (port->ops->pm) | |
2010 | port->ops->pm(port, pm_state, state->pm_state); | |
2011 | state->pm_state = pm_state; | |
2012 | } | |
1da177e4 LT |
2013 | } |
2014 | ||
b3b708fa GL |
2015 | struct uart_match { |
2016 | struct uart_port *port; | |
2017 | struct uart_driver *driver; | |
2018 | }; | |
2019 | ||
2020 | static int serial_match_port(struct device *dev, void *data) | |
2021 | { | |
2022 | struct uart_match *match = data; | |
7ca796f4 GL |
2023 | struct tty_driver *tty_drv = match->driver->tty_driver; |
2024 | dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) + | |
2025 | match->port->line; | |
b3b708fa GL |
2026 | |
2027 | return dev->devt == devt; /* Actually, only one tty per port */ | |
2028 | } | |
2029 | ||
ccce6deb | 2030 | int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport) |
1da177e4 | 2031 | { |
ccce6deb AC |
2032 | struct uart_state *state = drv->state + uport->line; |
2033 | struct tty_port *port = &state->port; | |
b3b708fa | 2034 | struct device *tty_dev; |
ccce6deb | 2035 | struct uart_match match = {uport, drv}; |
1da177e4 | 2036 | |
a2bceae0 | 2037 | mutex_lock(&port->mutex); |
1da177e4 | 2038 | |
ccce6deb | 2039 | tty_dev = device_find_child(uport->dev, &match, serial_match_port); |
b3b708fa | 2040 | if (device_may_wakeup(tty_dev)) { |
3f960dbb G |
2041 | if (!enable_irq_wake(uport->irq)) |
2042 | uport->irq_wake = 1; | |
b3b708fa | 2043 | put_device(tty_dev); |
a2bceae0 | 2044 | mutex_unlock(&port->mutex); |
b3b708fa GL |
2045 | return 0; |
2046 | } | |
5a65dcc0 FV |
2047 | put_device(tty_dev); |
2048 | ||
b164c972 PH |
2049 | /* Nothing to do if the console is not suspending */ |
2050 | if (!console_suspend_enabled && uart_console(uport)) | |
2051 | goto unlock; | |
2052 | ||
2053 | uport->suspended = 1; | |
b3b708fa | 2054 | |
ccce6deb AC |
2055 | if (port->flags & ASYNC_INITIALIZED) { |
2056 | const struct uart_ops *ops = uport->ops; | |
c8c6bfa3 | 2057 | int tries; |
1da177e4 | 2058 | |
b164c972 PH |
2059 | set_bit(ASYNCB_SUSPENDED, &port->flags); |
2060 | clear_bit(ASYNCB_INITIALIZED, &port->flags); | |
a6b93a90 | 2061 | |
b164c972 PH |
2062 | spin_lock_irq(&uport->lock); |
2063 | ops->stop_tx(uport); | |
2064 | ops->set_mctrl(uport, 0); | |
2065 | ops->stop_rx(uport); | |
2066 | spin_unlock_irq(&uport->lock); | |
1da177e4 LT |
2067 | |
2068 | /* | |
2069 | * Wait for the transmitter to empty. | |
2070 | */ | |
ccce6deb | 2071 | for (tries = 3; !ops->tx_empty(uport) && tries; tries--) |
1da177e4 | 2072 | msleep(10); |
c8c6bfa3 | 2073 | if (!tries) |
2f2dafe7 SM |
2074 | dev_err(uport->dev, "%s%d: Unable to drain transmitter\n", |
2075 | drv->dev_name, | |
2076 | drv->tty_driver->name_base + uport->line); | |
1da177e4 | 2077 | |
b164c972 | 2078 | ops->shutdown(uport); |
1da177e4 LT |
2079 | } |
2080 | ||
2081 | /* | |
2082 | * Disable the console device before suspending. | |
2083 | */ | |
b164c972 | 2084 | if (uart_console(uport)) |
ccce6deb | 2085 | console_stop(uport->cons); |
1da177e4 | 2086 | |
b164c972 PH |
2087 | uart_change_pm(state, UART_PM_STATE_OFF); |
2088 | unlock: | |
a2bceae0 | 2089 | mutex_unlock(&port->mutex); |
1da177e4 LT |
2090 | |
2091 | return 0; | |
2092 | } | |
2093 | ||
ccce6deb | 2094 | int uart_resume_port(struct uart_driver *drv, struct uart_port *uport) |
1da177e4 | 2095 | { |
ccce6deb AC |
2096 | struct uart_state *state = drv->state + uport->line; |
2097 | struct tty_port *port = &state->port; | |
03a74dcc | 2098 | struct device *tty_dev; |
ccce6deb | 2099 | struct uart_match match = {uport, drv}; |
ba15ab0e | 2100 | struct ktermios termios; |
1da177e4 | 2101 | |
a2bceae0 | 2102 | mutex_lock(&port->mutex); |
1da177e4 | 2103 | |
ccce6deb AC |
2104 | tty_dev = device_find_child(uport->dev, &match, serial_match_port); |
2105 | if (!uport->suspended && device_may_wakeup(tty_dev)) { | |
3f960dbb G |
2106 | if (uport->irq_wake) { |
2107 | disable_irq_wake(uport->irq); | |
2108 | uport->irq_wake = 0; | |
2109 | } | |
5a65dcc0 | 2110 | put_device(tty_dev); |
a2bceae0 | 2111 | mutex_unlock(&port->mutex); |
b3b708fa GL |
2112 | return 0; |
2113 | } | |
5a65dcc0 | 2114 | put_device(tty_dev); |
ccce6deb | 2115 | uport->suspended = 0; |
b3b708fa | 2116 | |
1da177e4 LT |
2117 | /* |
2118 | * Re-enable the console device after suspending. | |
2119 | */ | |
5933a161 | 2120 | if (uart_console(uport)) { |
891b9dd1 JW |
2121 | /* |
2122 | * First try to use the console cflag setting. | |
2123 | */ | |
2124 | memset(&termios, 0, sizeof(struct ktermios)); | |
2125 | termios.c_cflag = uport->cons->cflag; | |
2126 | ||
2127 | /* | |
2128 | * If that's unset, use the tty termios setting. | |
2129 | */ | |
adc8d746 AC |
2130 | if (port->tty && termios.c_cflag == 0) |
2131 | termios = port->tty->termios; | |
891b9dd1 | 2132 | |
94abc56f | 2133 | if (console_suspend_enabled) |
6f538fe3 | 2134 | uart_change_pm(state, UART_PM_STATE_ON); |
ccce6deb | 2135 | uport->ops->set_termios(uport, &termios, NULL); |
5933a161 YK |
2136 | if (console_suspend_enabled) |
2137 | console_start(uport->cons); | |
1da177e4 LT |
2138 | } |
2139 | ||
ccce6deb AC |
2140 | if (port->flags & ASYNC_SUSPENDED) { |
2141 | const struct uart_ops *ops = uport->ops; | |
ee31b337 | 2142 | int ret; |
1da177e4 | 2143 | |
6f538fe3 | 2144 | uart_change_pm(state, UART_PM_STATE_ON); |
ccce6deb AC |
2145 | spin_lock_irq(&uport->lock); |
2146 | ops->set_mctrl(uport, 0); | |
2147 | spin_unlock_irq(&uport->lock); | |
4547be78 | 2148 | if (console_suspend_enabled || !uart_console(uport)) { |
19225135 AC |
2149 | /* Protected by port mutex for now */ |
2150 | struct tty_struct *tty = port->tty; | |
4547be78 SB |
2151 | ret = ops->startup(uport); |
2152 | if (ret == 0) { | |
19225135 AC |
2153 | if (tty) |
2154 | uart_change_speed(tty, state, NULL); | |
4547be78 SB |
2155 | spin_lock_irq(&uport->lock); |
2156 | ops->set_mctrl(uport, uport->mctrl); | |
2157 | ops->start_tx(uport); | |
2158 | spin_unlock_irq(&uport->lock); | |
2159 | set_bit(ASYNCB_INITIALIZED, &port->flags); | |
2160 | } else { | |
2161 | /* | |
2162 | * Failed to resume - maybe hardware went away? | |
2163 | * Clear the "initialized" flag so we won't try | |
2164 | * to call the low level drivers shutdown method. | |
2165 | */ | |
19225135 | 2166 | uart_shutdown(tty, state); |
4547be78 | 2167 | } |
ee31b337 | 2168 | } |
a6b93a90 | 2169 | |
ccce6deb | 2170 | clear_bit(ASYNCB_SUSPENDED, &port->flags); |
1da177e4 LT |
2171 | } |
2172 | ||
a2bceae0 | 2173 | mutex_unlock(&port->mutex); |
1da177e4 LT |
2174 | |
2175 | return 0; | |
2176 | } | |
2177 | ||
2178 | static inline void | |
2179 | uart_report_port(struct uart_driver *drv, struct uart_port *port) | |
2180 | { | |
30b7a3bc RK |
2181 | char address[64]; |
2182 | ||
1da177e4 LT |
2183 | switch (port->iotype) { |
2184 | case UPIO_PORT: | |
9bde10a4 | 2185 | snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase); |
1da177e4 LT |
2186 | break; |
2187 | case UPIO_HUB6: | |
30b7a3bc | 2188 | snprintf(address, sizeof(address), |
9bde10a4 | 2189 | "I/O 0x%lx offset 0x%x", port->iobase, port->hub6); |
1da177e4 LT |
2190 | break; |
2191 | case UPIO_MEM: | |
2192 | case UPIO_MEM32: | |
3ffb1a81 | 2193 | case UPIO_MEM32BE: |
21c614a7 | 2194 | case UPIO_AU: |
3be91ec7 | 2195 | case UPIO_TSI: |
30b7a3bc | 2196 | snprintf(address, sizeof(address), |
4f640efb | 2197 | "MMIO 0x%llx", (unsigned long long)port->mapbase); |
30b7a3bc RK |
2198 | break; |
2199 | default: | |
2200 | strlcpy(address, "*unknown*", sizeof(address)); | |
1da177e4 LT |
2201 | break; |
2202 | } | |
30b7a3bc | 2203 | |
68ed7e1c JB |
2204 | printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n", |
2205 | port->dev ? dev_name(port->dev) : "", | |
2206 | port->dev ? ": " : "", | |
8440838b DM |
2207 | drv->dev_name, |
2208 | drv->tty_driver->name_base + port->line, | |
7d12b976 | 2209 | address, port->irq, port->uartclk / 16, uart_type(port)); |
1da177e4 LT |
2210 | } |
2211 | ||
2212 | static void | |
2213 | uart_configure_port(struct uart_driver *drv, struct uart_state *state, | |
2214 | struct uart_port *port) | |
2215 | { | |
2216 | unsigned int flags; | |
2217 | ||
2218 | /* | |
2219 | * If there isn't a port here, don't do anything further. | |
2220 | */ | |
2221 | if (!port->iobase && !port->mapbase && !port->membase) | |
2222 | return; | |
2223 | ||
2224 | /* | |
2225 | * Now do the auto configuration stuff. Note that config_port | |
2226 | * is expected to claim the resources and map the port for us. | |
2227 | */ | |
8e23fcc8 | 2228 | flags = 0; |
1da177e4 LT |
2229 | if (port->flags & UPF_AUTO_IRQ) |
2230 | flags |= UART_CONFIG_IRQ; | |
2231 | if (port->flags & UPF_BOOT_AUTOCONF) { | |
8e23fcc8 DD |
2232 | if (!(port->flags & UPF_FIXED_TYPE)) { |
2233 | port->type = PORT_UNKNOWN; | |
2234 | flags |= UART_CONFIG_TYPE; | |
2235 | } | |
1da177e4 LT |
2236 | port->ops->config_port(port, flags); |
2237 | } | |
2238 | ||
2239 | if (port->type != PORT_UNKNOWN) { | |
2240 | unsigned long flags; | |
2241 | ||
2242 | uart_report_port(drv, port); | |
2243 | ||
3689a0ec | 2244 | /* Power up port for set_mctrl() */ |
6f538fe3 | 2245 | uart_change_pm(state, UART_PM_STATE_ON); |
3689a0ec | 2246 | |
1da177e4 LT |
2247 | /* |
2248 | * Ensure that the modem control lines are de-activated. | |
c3e4642b | 2249 | * keep the DTR setting that is set in uart_set_options() |
1da177e4 LT |
2250 | * We probably don't need a spinlock around this, but |
2251 | */ | |
2252 | spin_lock_irqsave(&port->lock, flags); | |
c3e4642b | 2253 | port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR); |
1da177e4 LT |
2254 | spin_unlock_irqrestore(&port->lock, flags); |
2255 | ||
97d97224 RK |
2256 | /* |
2257 | * If this driver supports console, and it hasn't been | |
2258 | * successfully registered yet, try to re-register it. | |
2259 | * It may be that the port was not available. | |
2260 | */ | |
2261 | if (port->cons && !(port->cons->flags & CON_ENABLED)) | |
2262 | register_console(port->cons); | |
2263 | ||
1da177e4 LT |
2264 | /* |
2265 | * Power down all ports by default, except the | |
2266 | * console if we have one. | |
2267 | */ | |
2268 | if (!uart_console(port)) | |
6f538fe3 | 2269 | uart_change_pm(state, UART_PM_STATE_OFF); |
1da177e4 LT |
2270 | } |
2271 | } | |
2272 | ||
f2d937f3 JW |
2273 | #ifdef CONFIG_CONSOLE_POLL |
2274 | ||
2275 | static int uart_poll_init(struct tty_driver *driver, int line, char *options) | |
2276 | { | |
2277 | struct uart_driver *drv = driver->driver_state; | |
2278 | struct uart_state *state = drv->state + line; | |
2279 | struct uart_port *port; | |
2280 | int baud = 9600; | |
2281 | int bits = 8; | |
2282 | int parity = 'n'; | |
2283 | int flow = 'n'; | |
c7f3e708 | 2284 | int ret; |
f2d937f3 | 2285 | |
ebd2c8f6 | 2286 | if (!state || !state->uart_port) |
f2d937f3 JW |
2287 | return -1; |
2288 | ||
ebd2c8f6 | 2289 | port = state->uart_port; |
f2d937f3 JW |
2290 | if (!(port->ops->poll_get_char && port->ops->poll_put_char)) |
2291 | return -1; | |
2292 | ||
c7f3e708 AV |
2293 | if (port->ops->poll_init) { |
2294 | struct tty_port *tport = &state->port; | |
2295 | ||
2296 | ret = 0; | |
2297 | mutex_lock(&tport->mutex); | |
2298 | /* | |
2299 | * We don't set ASYNCB_INITIALIZED as we only initialized the | |
2300 | * hw, e.g. state->xmit is still uninitialized. | |
2301 | */ | |
2302 | if (!test_bit(ASYNCB_INITIALIZED, &tport->flags)) | |
2303 | ret = port->ops->poll_init(port); | |
2304 | mutex_unlock(&tport->mutex); | |
2305 | if (ret) | |
2306 | return ret; | |
2307 | } | |
2308 | ||
f2d937f3 JW |
2309 | if (options) { |
2310 | uart_parse_options(options, &baud, &parity, &bits, &flow); | |
2311 | return uart_set_options(port, NULL, baud, parity, bits, flow); | |
2312 | } | |
2313 | ||
2314 | return 0; | |
2315 | } | |
2316 | ||
2317 | static int uart_poll_get_char(struct tty_driver *driver, int line) | |
2318 | { | |
2319 | struct uart_driver *drv = driver->driver_state; | |
2320 | struct uart_state *state = drv->state + line; | |
2321 | struct uart_port *port; | |
2322 | ||
ebd2c8f6 | 2323 | if (!state || !state->uart_port) |
f2d937f3 JW |
2324 | return -1; |
2325 | ||
ebd2c8f6 | 2326 | port = state->uart_port; |
f2d937f3 JW |
2327 | return port->ops->poll_get_char(port); |
2328 | } | |
2329 | ||
2330 | static void uart_poll_put_char(struct tty_driver *driver, int line, char ch) | |
2331 | { | |
2332 | struct uart_driver *drv = driver->driver_state; | |
2333 | struct uart_state *state = drv->state + line; | |
2334 | struct uart_port *port; | |
2335 | ||
ebd2c8f6 | 2336 | if (!state || !state->uart_port) |
f2d937f3 JW |
2337 | return; |
2338 | ||
ebd2c8f6 | 2339 | port = state->uart_port; |
c7d44a02 DA |
2340 | |
2341 | if (ch == '\n') | |
2342 | port->ops->poll_put_char(port, '\r'); | |
f2d937f3 JW |
2343 | port->ops->poll_put_char(port, ch); |
2344 | } | |
2345 | #endif | |
2346 | ||
b68e31d0 | 2347 | static const struct tty_operations uart_ops = { |
1da177e4 LT |
2348 | .open = uart_open, |
2349 | .close = uart_close, | |
2350 | .write = uart_write, | |
2351 | .put_char = uart_put_char, | |
2352 | .flush_chars = uart_flush_chars, | |
2353 | .write_room = uart_write_room, | |
2354 | .chars_in_buffer= uart_chars_in_buffer, | |
2355 | .flush_buffer = uart_flush_buffer, | |
2356 | .ioctl = uart_ioctl, | |
2357 | .throttle = uart_throttle, | |
2358 | .unthrottle = uart_unthrottle, | |
2359 | .send_xchar = uart_send_xchar, | |
2360 | .set_termios = uart_set_termios, | |
64e9159f | 2361 | .set_ldisc = uart_set_ldisc, |
1da177e4 LT |
2362 | .stop = uart_stop, |
2363 | .start = uart_start, | |
2364 | .hangup = uart_hangup, | |
2365 | .break_ctl = uart_break_ctl, | |
2366 | .wait_until_sent= uart_wait_until_sent, | |
2367 | #ifdef CONFIG_PROC_FS | |
d196a949 | 2368 | .proc_fops = &uart_proc_fops, |
1da177e4 LT |
2369 | #endif |
2370 | .tiocmget = uart_tiocmget, | |
2371 | .tiocmset = uart_tiocmset, | |
d281da7f | 2372 | .get_icount = uart_get_icount, |
f2d937f3 JW |
2373 | #ifdef CONFIG_CONSOLE_POLL |
2374 | .poll_init = uart_poll_init, | |
2375 | .poll_get_char = uart_poll_get_char, | |
2376 | .poll_put_char = uart_poll_put_char, | |
2377 | #endif | |
1da177e4 LT |
2378 | }; |
2379 | ||
de0c8cb3 | 2380 | static const struct tty_port_operations uart_port_ops = { |
0b1db830 JS |
2381 | .activate = uart_port_activate, |
2382 | .shutdown = uart_port_shutdown, | |
de0c8cb3 AC |
2383 | .carrier_raised = uart_carrier_raised, |
2384 | .dtr_rts = uart_dtr_rts, | |
2385 | }; | |
2386 | ||
1da177e4 LT |
2387 | /** |
2388 | * uart_register_driver - register a driver with the uart core layer | |
2389 | * @drv: low level driver structure | |
2390 | * | |
2391 | * Register a uart driver with the core driver. We in turn register | |
2392 | * with the tty layer, and initialise the core driver per-port state. | |
2393 | * | |
2394 | * We have a proc file in /proc/tty/driver which is named after the | |
2395 | * normal driver. | |
2396 | * | |
2397 | * drv->port should be NULL, and the per-port structures should be | |
2398 | * registered using uart_add_one_port after this call has succeeded. | |
2399 | */ | |
2400 | int uart_register_driver(struct uart_driver *drv) | |
2401 | { | |
9e845abf | 2402 | struct tty_driver *normal; |
1da177e4 LT |
2403 | int i, retval; |
2404 | ||
2405 | BUG_ON(drv->state); | |
2406 | ||
2407 | /* | |
2408 | * Maybe we should be using a slab cache for this, especially if | |
2409 | * we have a large number of ports to handle. | |
2410 | */ | |
8f31bb39 | 2411 | drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL); |
1da177e4 LT |
2412 | if (!drv->state) |
2413 | goto out; | |
2414 | ||
9e845abf | 2415 | normal = alloc_tty_driver(drv->nr); |
1da177e4 | 2416 | if (!normal) |
9e845abf | 2417 | goto out_kfree; |
1da177e4 LT |
2418 | |
2419 | drv->tty_driver = normal; | |
2420 | ||
1da177e4 | 2421 | normal->driver_name = drv->driver_name; |
1da177e4 LT |
2422 | normal->name = drv->dev_name; |
2423 | normal->major = drv->major; | |
2424 | normal->minor_start = drv->minor; | |
2425 | normal->type = TTY_DRIVER_TYPE_SERIAL; | |
2426 | normal->subtype = SERIAL_TYPE_NORMAL; | |
2427 | normal->init_termios = tty_std_termios; | |
2428 | normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; | |
606d099c | 2429 | normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600; |
331b8319 | 2430 | normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
1da177e4 LT |
2431 | normal->driver_state = drv; |
2432 | tty_set_operations(normal, &uart_ops); | |
2433 | ||
2434 | /* | |
2435 | * Initialise the UART state(s). | |
2436 | */ | |
2437 | for (i = 0; i < drv->nr; i++) { | |
2438 | struct uart_state *state = drv->state + i; | |
a2bceae0 | 2439 | struct tty_port *port = &state->port; |
1da177e4 | 2440 | |
a2bceae0 | 2441 | tty_port_init(port); |
de0c8cb3 | 2442 | port->ops = &uart_port_ops; |
1da177e4 LT |
2443 | } |
2444 | ||
2445 | retval = tty_register_driver(normal); | |
9e845abf AGR |
2446 | if (retval >= 0) |
2447 | return retval; | |
2448 | ||
191c5f10 JS |
2449 | for (i = 0; i < drv->nr; i++) |
2450 | tty_port_destroy(&drv->state[i].port); | |
9e845abf AGR |
2451 | put_tty_driver(normal); |
2452 | out_kfree: | |
2453 | kfree(drv->state); | |
2454 | out: | |
2455 | return -ENOMEM; | |
1da177e4 LT |
2456 | } |
2457 | ||
2458 | /** | |
2459 | * uart_unregister_driver - remove a driver from the uart core layer | |
2460 | * @drv: low level driver structure | |
2461 | * | |
2462 | * Remove all references to a driver from the core driver. The low | |
2463 | * level driver must have removed all its ports via the | |
2464 | * uart_remove_one_port() if it registered them with uart_add_one_port(). | |
2465 | * (ie, drv->port == NULL) | |
2466 | */ | |
2467 | void uart_unregister_driver(struct uart_driver *drv) | |
2468 | { | |
2469 | struct tty_driver *p = drv->tty_driver; | |
191c5f10 JS |
2470 | unsigned int i; |
2471 | ||
1da177e4 LT |
2472 | tty_unregister_driver(p); |
2473 | put_tty_driver(p); | |
191c5f10 JS |
2474 | for (i = 0; i < drv->nr; i++) |
2475 | tty_port_destroy(&drv->state[i].port); | |
1da177e4 | 2476 | kfree(drv->state); |
1e66cded | 2477 | drv->state = NULL; |
1da177e4 LT |
2478 | drv->tty_driver = NULL; |
2479 | } | |
2480 | ||
2481 | struct tty_driver *uart_console_device(struct console *co, int *index) | |
2482 | { | |
2483 | struct uart_driver *p = co->data; | |
2484 | *index = co->index; | |
2485 | return p->tty_driver; | |
2486 | } | |
2487 | ||
6915c0e4 TH |
2488 | static ssize_t uart_get_attr_uartclk(struct device *dev, |
2489 | struct device_attribute *attr, char *buf) | |
2490 | { | |
bebe73e3 | 2491 | struct serial_struct tmp; |
6915c0e4 | 2492 | struct tty_port *port = dev_get_drvdata(dev); |
6915c0e4 | 2493 | |
9f109694 | 2494 | uart_get_info(port, &tmp); |
bebe73e3 | 2495 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16); |
6915c0e4 TH |
2496 | } |
2497 | ||
373bac4c AC |
2498 | static ssize_t uart_get_attr_type(struct device *dev, |
2499 | struct device_attribute *attr, char *buf) | |
2500 | { | |
2501 | struct serial_struct tmp; | |
2502 | struct tty_port *port = dev_get_drvdata(dev); | |
2503 | ||
2504 | uart_get_info(port, &tmp); | |
2505 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type); | |
2506 | } | |
2507 | static ssize_t uart_get_attr_line(struct device *dev, | |
2508 | struct device_attribute *attr, char *buf) | |
2509 | { | |
2510 | struct serial_struct tmp; | |
2511 | struct tty_port *port = dev_get_drvdata(dev); | |
2512 | ||
2513 | uart_get_info(port, &tmp); | |
2514 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line); | |
2515 | } | |
2516 | ||
2517 | static ssize_t uart_get_attr_port(struct device *dev, | |
2518 | struct device_attribute *attr, char *buf) | |
2519 | { | |
2520 | struct serial_struct tmp; | |
2521 | struct tty_port *port = dev_get_drvdata(dev); | |
fd985e1d | 2522 | unsigned long ioaddr; |
373bac4c AC |
2523 | |
2524 | uart_get_info(port, &tmp); | |
fd985e1d AM |
2525 | ioaddr = tmp.port; |
2526 | if (HIGH_BITS_OFFSET) | |
2527 | ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET; | |
2528 | return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr); | |
373bac4c AC |
2529 | } |
2530 | ||
2531 | static ssize_t uart_get_attr_irq(struct device *dev, | |
2532 | struct device_attribute *attr, char *buf) | |
2533 | { | |
2534 | struct serial_struct tmp; | |
2535 | struct tty_port *port = dev_get_drvdata(dev); | |
2536 | ||
2537 | uart_get_info(port, &tmp); | |
2538 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq); | |
2539 | } | |
2540 | ||
2541 | static ssize_t uart_get_attr_flags(struct device *dev, | |
2542 | struct device_attribute *attr, char *buf) | |
2543 | { | |
2544 | struct serial_struct tmp; | |
2545 | struct tty_port *port = dev_get_drvdata(dev); | |
2546 | ||
2547 | uart_get_info(port, &tmp); | |
2548 | return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags); | |
2549 | } | |
2550 | ||
2551 | static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev, | |
2552 | struct device_attribute *attr, char *buf) | |
2553 | { | |
2554 | struct serial_struct tmp; | |
2555 | struct tty_port *port = dev_get_drvdata(dev); | |
2556 | ||
2557 | uart_get_info(port, &tmp); | |
2558 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size); | |
2559 | } | |
2560 | ||
2561 | ||
2562 | static ssize_t uart_get_attr_close_delay(struct device *dev, | |
2563 | struct device_attribute *attr, char *buf) | |
2564 | { | |
2565 | struct serial_struct tmp; | |
2566 | struct tty_port *port = dev_get_drvdata(dev); | |
2567 | ||
2568 | uart_get_info(port, &tmp); | |
2569 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay); | |
2570 | } | |
2571 | ||
2572 | ||
2573 | static ssize_t uart_get_attr_closing_wait(struct device *dev, | |
2574 | struct device_attribute *attr, char *buf) | |
2575 | { | |
2576 | struct serial_struct tmp; | |
2577 | struct tty_port *port = dev_get_drvdata(dev); | |
2578 | ||
2579 | uart_get_info(port, &tmp); | |
2580 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait); | |
2581 | } | |
2582 | ||
2583 | static ssize_t uart_get_attr_custom_divisor(struct device *dev, | |
2584 | struct device_attribute *attr, char *buf) | |
2585 | { | |
2586 | struct serial_struct tmp; | |
2587 | struct tty_port *port = dev_get_drvdata(dev); | |
2588 | ||
2589 | uart_get_info(port, &tmp); | |
2590 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor); | |
2591 | } | |
2592 | ||
2593 | static ssize_t uart_get_attr_io_type(struct device *dev, | |
2594 | struct device_attribute *attr, char *buf) | |
2595 | { | |
2596 | struct serial_struct tmp; | |
2597 | struct tty_port *port = dev_get_drvdata(dev); | |
2598 | ||
2599 | uart_get_info(port, &tmp); | |
2600 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type); | |
2601 | } | |
2602 | ||
2603 | static ssize_t uart_get_attr_iomem_base(struct device *dev, | |
2604 | struct device_attribute *attr, char *buf) | |
2605 | { | |
2606 | struct serial_struct tmp; | |
2607 | struct tty_port *port = dev_get_drvdata(dev); | |
2608 | ||
2609 | uart_get_info(port, &tmp); | |
2610 | return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base); | |
2611 | } | |
2612 | ||
2613 | static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev, | |
2614 | struct device_attribute *attr, char *buf) | |
2615 | { | |
2616 | struct serial_struct tmp; | |
2617 | struct tty_port *port = dev_get_drvdata(dev); | |
2618 | ||
2619 | uart_get_info(port, &tmp); | |
2620 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift); | |
2621 | } | |
2622 | ||
2623 | static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL); | |
2624 | static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL); | |
2625 | static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL); | |
2626 | static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL); | |
2627 | static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL); | |
2628 | static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL); | |
6915c0e4 | 2629 | static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL); |
373bac4c AC |
2630 | static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL); |
2631 | static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL); | |
2632 | static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL); | |
2633 | static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL); | |
2634 | static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL); | |
2635 | static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL); | |
6915c0e4 TH |
2636 | |
2637 | static struct attribute *tty_dev_attrs[] = { | |
373bac4c AC |
2638 | &dev_attr_type.attr, |
2639 | &dev_attr_line.attr, | |
2640 | &dev_attr_port.attr, | |
2641 | &dev_attr_irq.attr, | |
2642 | &dev_attr_flags.attr, | |
2643 | &dev_attr_xmit_fifo_size.attr, | |
6915c0e4 | 2644 | &dev_attr_uartclk.attr, |
373bac4c AC |
2645 | &dev_attr_close_delay.attr, |
2646 | &dev_attr_closing_wait.attr, | |
2647 | &dev_attr_custom_divisor.attr, | |
2648 | &dev_attr_io_type.attr, | |
2649 | &dev_attr_iomem_base.attr, | |
2650 | &dev_attr_iomem_reg_shift.attr, | |
6915c0e4 TH |
2651 | NULL, |
2652 | }; | |
2653 | ||
b1b79916 | 2654 | static const struct attribute_group tty_dev_attr_group = { |
6915c0e4 TH |
2655 | .attrs = tty_dev_attrs, |
2656 | }; | |
2657 | ||
1da177e4 LT |
2658 | /** |
2659 | * uart_add_one_port - attach a driver-defined port structure | |
2660 | * @drv: pointer to the uart low level driver structure for this port | |
1b9894f3 | 2661 | * @uport: uart port structure to use for this port. |
1da177e4 LT |
2662 | * |
2663 | * This allows the driver to register its own uart_port structure | |
2664 | * with the core driver. The main purpose is to allow the low | |
2665 | * level uart drivers to expand uart_port, rather than having yet | |
2666 | * more levels of structures. | |
2667 | */ | |
a2bceae0 | 2668 | int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport) |
1da177e4 LT |
2669 | { |
2670 | struct uart_state *state; | |
a2bceae0 | 2671 | struct tty_port *port; |
1da177e4 | 2672 | int ret = 0; |
b3b708fa | 2673 | struct device *tty_dev; |
266dcff0 | 2674 | int num_groups; |
1da177e4 LT |
2675 | |
2676 | BUG_ON(in_interrupt()); | |
2677 | ||
a2bceae0 | 2678 | if (uport->line >= drv->nr) |
1da177e4 LT |
2679 | return -EINVAL; |
2680 | ||
a2bceae0 AC |
2681 | state = drv->state + uport->line; |
2682 | port = &state->port; | |
1da177e4 | 2683 | |
f392ecfa | 2684 | mutex_lock(&port_mutex); |
a2bceae0 | 2685 | mutex_lock(&port->mutex); |
ebd2c8f6 | 2686 | if (state->uart_port) { |
1da177e4 LT |
2687 | ret = -EINVAL; |
2688 | goto out; | |
2689 | } | |
2690 | ||
2b702b9b | 2691 | /* Link the port to the driver state table and vice versa */ |
a2bceae0 | 2692 | state->uart_port = uport; |
2b702b9b | 2693 | uport->state = state; |
1da177e4 | 2694 | |
2b702b9b | 2695 | state->pm_state = UART_PM_STATE_UNDEFINED; |
a2bceae0 | 2696 | uport->cons = drv->cons; |
959801fe | 2697 | uport->minor = drv->tty_driver->minor_start + uport->line; |
1da177e4 | 2698 | |
976ecd12 RK |
2699 | /* |
2700 | * If this port is a console, then the spinlock is already | |
2701 | * initialised. | |
2702 | */ | |
a2bceae0 AC |
2703 | if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) { |
2704 | spin_lock_init(&uport->lock); | |
2705 | lockdep_set_class(&uport->lock, &port_lock_key); | |
13e83599 | 2706 | } |
a208ffd2 GL |
2707 | if (uport->cons && uport->dev) |
2708 | of_console_check(uport->dev->of_node, uport->cons->name, uport->line); | |
976ecd12 | 2709 | |
a2bceae0 | 2710 | uart_configure_port(drv, state, uport); |
1da177e4 | 2711 | |
266dcff0 GKH |
2712 | num_groups = 2; |
2713 | if (uport->attr_group) | |
2714 | num_groups++; | |
2715 | ||
c2b703b8 | 2716 | uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups), |
266dcff0 GKH |
2717 | GFP_KERNEL); |
2718 | if (!uport->tty_groups) { | |
2719 | ret = -ENOMEM; | |
2720 | goto out; | |
2721 | } | |
2722 | uport->tty_groups[0] = &tty_dev_attr_group; | |
2723 | if (uport->attr_group) | |
2724 | uport->tty_groups[1] = uport->attr_group; | |
2725 | ||
1da177e4 LT |
2726 | /* |
2727 | * Register the port whether it's detected or not. This allows | |
015355b7 | 2728 | * setserial to be used to alter this port's parameters. |
1da177e4 | 2729 | */ |
b1b79916 | 2730 | tty_dev = tty_port_register_device_attr(port, drv->tty_driver, |
266dcff0 | 2731 | uport->line, uport->dev, port, uport->tty_groups); |
b3b708fa | 2732 | if (likely(!IS_ERR(tty_dev))) { |
77359835 SG |
2733 | device_set_wakeup_capable(tty_dev, 1); |
2734 | } else { | |
2f2dafe7 | 2735 | dev_err(uport->dev, "Cannot register tty device on line %d\n", |
a2bceae0 | 2736 | uport->line); |
77359835 | 2737 | } |
1da177e4 | 2738 | |
68ac64cd RK |
2739 | /* |
2740 | * Ensure UPF_DEAD is not set. | |
2741 | */ | |
a2bceae0 | 2742 | uport->flags &= ~UPF_DEAD; |
68ac64cd | 2743 | |
1da177e4 | 2744 | out: |
a2bceae0 | 2745 | mutex_unlock(&port->mutex); |
f392ecfa | 2746 | mutex_unlock(&port_mutex); |
1da177e4 LT |
2747 | |
2748 | return ret; | |
2749 | } | |
2750 | ||
2751 | /** | |
2752 | * uart_remove_one_port - detach a driver defined port structure | |
2753 | * @drv: pointer to the uart low level driver structure for this port | |
1b9894f3 | 2754 | * @uport: uart port structure for this port |
1da177e4 LT |
2755 | * |
2756 | * This unhooks (and hangs up) the specified port structure from the | |
2757 | * core driver. No further calls will be made to the low-level code | |
2758 | * for this port. | |
2759 | */ | |
a2bceae0 | 2760 | int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport) |
1da177e4 | 2761 | { |
a2bceae0 AC |
2762 | struct uart_state *state = drv->state + uport->line; |
2763 | struct tty_port *port = &state->port; | |
4c6d5b4d | 2764 | struct tty_struct *tty; |
b342dd51 | 2765 | int ret = 0; |
1da177e4 LT |
2766 | |
2767 | BUG_ON(in_interrupt()); | |
2768 | ||
a2bceae0 | 2769 | if (state->uart_port != uport) |
2f2dafe7 | 2770 | dev_alert(uport->dev, "Removing wrong port: %p != %p\n", |
a2bceae0 | 2771 | state->uart_port, uport); |
1da177e4 | 2772 | |
f392ecfa | 2773 | mutex_lock(&port_mutex); |
1da177e4 | 2774 | |
68ac64cd RK |
2775 | /* |
2776 | * Mark the port "dead" - this prevents any opens from | |
2777 | * succeeding while we shut down the port. | |
2778 | */ | |
a2bceae0 | 2779 | mutex_lock(&port->mutex); |
b342dd51 CG |
2780 | if (!state->uart_port) { |
2781 | mutex_unlock(&port->mutex); | |
2782 | ret = -EINVAL; | |
2783 | goto out; | |
2784 | } | |
a2bceae0 AC |
2785 | uport->flags |= UPF_DEAD; |
2786 | mutex_unlock(&port->mutex); | |
68ac64cd | 2787 | |
1da177e4 | 2788 | /* |
aa4148cf | 2789 | * Remove the devices from the tty layer |
1da177e4 | 2790 | */ |
a2bceae0 | 2791 | tty_unregister_device(drv->tty_driver, uport->line); |
1da177e4 | 2792 | |
4c6d5b4d GU |
2793 | tty = tty_port_tty_get(port); |
2794 | if (tty) { | |
a2bceae0 | 2795 | tty_vhangup(port->tty); |
4c6d5b4d GU |
2796 | tty_kref_put(tty); |
2797 | } | |
68ac64cd | 2798 | |
5f5c9ae5 GU |
2799 | /* |
2800 | * If the port is used as a console, unregister it | |
2801 | */ | |
2802 | if (uart_console(uport)) | |
2803 | unregister_console(uport->cons); | |
2804 | ||
68ac64cd RK |
2805 | /* |
2806 | * Free the port IO and memory resources, if any. | |
2807 | */ | |
a2bceae0 AC |
2808 | if (uport->type != PORT_UNKNOWN) |
2809 | uport->ops->release_port(uport); | |
266dcff0 | 2810 | kfree(uport->tty_groups); |
68ac64cd RK |
2811 | |
2812 | /* | |
2813 | * Indicate that there isn't a port here anymore. | |
2814 | */ | |
a2bceae0 | 2815 | uport->type = PORT_UNKNOWN; |
68ac64cd | 2816 | |
ebd2c8f6 | 2817 | state->uart_port = NULL; |
b342dd51 | 2818 | out: |
f392ecfa | 2819 | mutex_unlock(&port_mutex); |
1da177e4 | 2820 | |
b342dd51 | 2821 | return ret; |
1da177e4 LT |
2822 | } |
2823 | ||
2824 | /* | |
2825 | * Are the two ports equivalent? | |
2826 | */ | |
2827 | int uart_match_port(struct uart_port *port1, struct uart_port *port2) | |
2828 | { | |
2829 | if (port1->iotype != port2->iotype) | |
2830 | return 0; | |
2831 | ||
2832 | switch (port1->iotype) { | |
2833 | case UPIO_PORT: | |
2834 | return (port1->iobase == port2->iobase); | |
2835 | case UPIO_HUB6: | |
2836 | return (port1->iobase == port2->iobase) && | |
2837 | (port1->hub6 == port2->hub6); | |
2838 | case UPIO_MEM: | |
d21b55d3 | 2839 | case UPIO_MEM32: |
3ffb1a81 | 2840 | case UPIO_MEM32BE: |
d21b55d3 SS |
2841 | case UPIO_AU: |
2842 | case UPIO_TSI: | |
1624f003 | 2843 | return (port1->mapbase == port2->mapbase); |
1da177e4 LT |
2844 | } |
2845 | return 0; | |
2846 | } | |
2847 | EXPORT_SYMBOL(uart_match_port); | |
2848 | ||
027d7dac JS |
2849 | /** |
2850 | * uart_handle_dcd_change - handle a change of carrier detect state | |
2851 | * @uport: uart_port structure for the open port | |
2852 | * @status: new carrier detect status, nonzero if active | |
4d90bb14 PH |
2853 | * |
2854 | * Caller must hold uport->lock | |
027d7dac JS |
2855 | */ |
2856 | void uart_handle_dcd_change(struct uart_port *uport, unsigned int status) | |
2857 | { | |
42381572 | 2858 | struct tty_port *port = &uport->state->port; |
43eca0ae | 2859 | struct tty_struct *tty = port->tty; |
c993257b | 2860 | struct tty_ldisc *ld; |
027d7dac | 2861 | |
4d90bb14 PH |
2862 | lockdep_assert_held_once(&uport->lock); |
2863 | ||
c993257b PH |
2864 | if (tty) { |
2865 | ld = tty_ldisc_ref(tty); | |
2866 | if (ld) { | |
2867 | if (ld->ops->dcd_change) | |
2868 | ld->ops->dcd_change(tty, status); | |
2869 | tty_ldisc_deref(ld); | |
2870 | } | |
42381572 | 2871 | } |
027d7dac JS |
2872 | |
2873 | uport->icount.dcd++; | |
027d7dac | 2874 | |
299245a1 | 2875 | if (uart_dcd_enabled(uport)) { |
027d7dac JS |
2876 | if (status) |
2877 | wake_up_interruptible(&port->open_wait); | |
43eca0ae AC |
2878 | else if (tty) |
2879 | tty_hangup(tty); | |
027d7dac | 2880 | } |
027d7dac JS |
2881 | } |
2882 | EXPORT_SYMBOL_GPL(uart_handle_dcd_change); | |
2883 | ||
2884 | /** | |
2885 | * uart_handle_cts_change - handle a change of clear-to-send state | |
2886 | * @uport: uart_port structure for the open port | |
2887 | * @status: new clear to send status, nonzero if active | |
4d90bb14 PH |
2888 | * |
2889 | * Caller must hold uport->lock | |
027d7dac JS |
2890 | */ |
2891 | void uart_handle_cts_change(struct uart_port *uport, unsigned int status) | |
2892 | { | |
4d90bb14 PH |
2893 | lockdep_assert_held_once(&uport->lock); |
2894 | ||
027d7dac JS |
2895 | uport->icount.cts++; |
2896 | ||
391f93f2 | 2897 | if (uart_softcts_mode(uport)) { |
d01f4d18 | 2898 | if (uport->hw_stopped) { |
027d7dac | 2899 | if (status) { |
d01f4d18 | 2900 | uport->hw_stopped = 0; |
027d7dac JS |
2901 | uport->ops->start_tx(uport); |
2902 | uart_write_wakeup(uport); | |
2903 | } | |
2904 | } else { | |
2905 | if (!status) { | |
d01f4d18 | 2906 | uport->hw_stopped = 1; |
027d7dac JS |
2907 | uport->ops->stop_tx(uport); |
2908 | } | |
2909 | } | |
391f93f2 | 2910 | |
027d7dac JS |
2911 | } |
2912 | } | |
2913 | EXPORT_SYMBOL_GPL(uart_handle_cts_change); | |
2914 | ||
cf75525f JS |
2915 | /** |
2916 | * uart_insert_char - push a char to the uart layer | |
2917 | * | |
2918 | * User is responsible to call tty_flip_buffer_push when they are done with | |
2919 | * insertion. | |
2920 | * | |
2921 | * @port: corresponding port | |
2922 | * @status: state of the serial port RX buffer (LSR for 8250) | |
2923 | * @overrun: mask of overrun bits in @status | |
2924 | * @ch: character to push | |
2925 | * @flag: flag for the character (see TTY_NORMAL and friends) | |
2926 | */ | |
027d7dac JS |
2927 | void uart_insert_char(struct uart_port *port, unsigned int status, |
2928 | unsigned int overrun, unsigned int ch, unsigned int flag) | |
2929 | { | |
92a19f9c | 2930 | struct tty_port *tport = &port->state->port; |
027d7dac JS |
2931 | |
2932 | if ((status & port->ignore_status_mask & ~overrun) == 0) | |
92a19f9c | 2933 | if (tty_insert_flip_char(tport, ch, flag) == 0) |
dabfb351 | 2934 | ++port->icount.buf_overrun; |
027d7dac JS |
2935 | |
2936 | /* | |
2937 | * Overrun is special. Since it's reported immediately, | |
2938 | * it doesn't affect the current character. | |
2939 | */ | |
2940 | if (status & ~port->ignore_status_mask & overrun) | |
92a19f9c | 2941 | if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0) |
dabfb351 | 2942 | ++port->icount.buf_overrun; |
027d7dac JS |
2943 | } |
2944 | EXPORT_SYMBOL_GPL(uart_insert_char); | |
2945 | ||
1da177e4 LT |
2946 | EXPORT_SYMBOL(uart_write_wakeup); |
2947 | EXPORT_SYMBOL(uart_register_driver); | |
2948 | EXPORT_SYMBOL(uart_unregister_driver); | |
2949 | EXPORT_SYMBOL(uart_suspend_port); | |
2950 | EXPORT_SYMBOL(uart_resume_port); | |
1da177e4 LT |
2951 | EXPORT_SYMBOL(uart_add_one_port); |
2952 | EXPORT_SYMBOL(uart_remove_one_port); | |
2953 | ||
2954 | MODULE_DESCRIPTION("Serial driver core"); | |
2955 | MODULE_LICENSE("GPL"); |