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