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