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