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