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