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