Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Serial driver for the amiga builtin port. |
| 3 | * |
| 4 | * This code was created by taking serial.c version 4.30 from kernel |
| 5 | * release 2.3.22, replacing all hardware related stuff with the |
| 6 | * corresponding amiga hardware actions, and removing all irrelevant |
| 7 | * code. As a consequence, it uses many of the constants and names |
| 8 | * associated with the registers and bits of 16550 compatible UARTS - |
| 9 | * but only to keep track of status, etc in the state variables. It |
| 10 | * was done this was to make it easier to keep the code in line with |
| 11 | * (non hardware specific) changes to serial.c. |
| 12 | * |
| 13 | * The port is registered with the tty driver as minor device 64, and |
| 14 | * therefore other ports should should only use 65 upwards. |
| 15 | * |
| 16 | * Richard Lucock 28/12/99 |
| 17 | * |
| 18 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 19 | * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, |
| 20 | * 1998, 1999 Theodore Ts'o |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Serial driver configuration section. Here are the various options: |
| 26 | * |
| 27 | * SERIAL_PARANOIA_CHECK |
| 28 | * Check the magic number for the async_structure where |
| 29 | * ever possible. |
| 30 | */ |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/delay.h> |
| 33 | |
| 34 | #undef SERIAL_PARANOIA_CHECK |
| 35 | #define SERIAL_DO_RESTART |
| 36 | |
| 37 | /* Set of debugging defines */ |
| 38 | |
| 39 | #undef SERIAL_DEBUG_INTR |
| 40 | #undef SERIAL_DEBUG_OPEN |
| 41 | #undef SERIAL_DEBUG_FLOW |
| 42 | #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT |
| 43 | |
| 44 | /* Sanity checks */ |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT) |
| 47 | #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \ |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 48 | tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #else |
| 50 | #define DBG_CNT(s) |
| 51 | #endif |
| 52 | |
| 53 | /* |
| 54 | * End of serial driver configuration section. |
| 55 | */ |
| 56 | |
| 57 | #include <linux/module.h> |
| 58 | |
| 59 | #include <linux/types.h> |
| 60 | #include <linux/serial.h> |
| 61 | #include <linux/serialP.h> |
| 62 | #include <linux/serial_reg.h> |
| 63 | static char *serial_version = "4.30"; |
| 64 | |
| 65 | #include <linux/errno.h> |
| 66 | #include <linux/signal.h> |
| 67 | #include <linux/sched.h> |
| 68 | #include <linux/kernel.h> |
| 69 | #include <linux/timer.h> |
| 70 | #include <linux/interrupt.h> |
| 71 | #include <linux/tty.h> |
| 72 | #include <linux/tty_flip.h> |
| 73 | #include <linux/console.h> |
| 74 | #include <linux/major.h> |
| 75 | #include <linux/string.h> |
| 76 | #include <linux/fcntl.h> |
| 77 | #include <linux/ptrace.h> |
| 78 | #include <linux/ioport.h> |
| 79 | #include <linux/mm.h> |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 80 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #include <linux/slab.h> |
| 82 | #include <linux/init.h> |
| 83 | #include <linux/bitops.h> |
Geert Uytterhoeven | 826e8c8 | 2009-04-05 13:12:30 +0200 | [diff] [blame] | 84 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | #include <asm/setup.h> |
| 87 | |
| 88 | #include <asm/system.h> |
| 89 | |
| 90 | #include <asm/irq.h> |
| 91 | |
| 92 | #include <asm/amigahw.h> |
| 93 | #include <asm/amigaints.h> |
| 94 | |
Al Viro | b4290a2 | 2006-01-12 01:06:12 -0800 | [diff] [blame] | 95 | #define custom amiga_custom |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | static char *serial_name = "Amiga-builtin serial driver"; |
| 97 | |
| 98 | static struct tty_driver *serial_driver; |
| 99 | |
| 100 | /* number of characters left in xmit buffer before we ask for more */ |
| 101 | #define WAKEUP_CHARS 256 |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | static unsigned char current_ctl_bits; |
| 104 | |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 105 | static void change_speed(struct tty_struct *tty, struct serial_state *info, |
| 106 | struct ktermios *old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | static void rs_wait_until_sent(struct tty_struct *tty, int timeout); |
| 108 | |
| 109 | |
| 110 | static struct serial_state rs_table[1]; |
| 111 | |
Tobias Klauser | fe97107 | 2006-01-09 20:54:02 -0800 | [diff] [blame] | 112 | #define NR_PORTS ARRAY_SIZE(rs_table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | #include <asm/uaccess.h> |
| 115 | |
| 116 | #define serial_isroot() (capable(CAP_SYS_ADMIN)) |
| 117 | |
| 118 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 119 | static inline int serial_paranoia_check(struct serial_state *info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | char *name, const char *routine) |
| 121 | { |
| 122 | #ifdef SERIAL_PARANOIA_CHECK |
| 123 | static const char *badmagic = |
| 124 | "Warning: bad magic number for serial struct (%s) in %s\n"; |
| 125 | static const char *badinfo = |
| 126 | "Warning: null async_struct for (%s) in %s\n"; |
| 127 | |
| 128 | if (!info) { |
| 129 | printk(badinfo, name, routine); |
| 130 | return 1; |
| 131 | } |
| 132 | if (info->magic != SERIAL_MAGIC) { |
| 133 | printk(badmagic, name, routine); |
| 134 | return 1; |
| 135 | } |
| 136 | #endif |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /* some serial hardware definitions */ |
| 141 | #define SDR_OVRUN (1<<15) |
| 142 | #define SDR_RBF (1<<14) |
| 143 | #define SDR_TBE (1<<13) |
| 144 | #define SDR_TSRE (1<<12) |
| 145 | |
| 146 | #define SERPER_PARENB (1<<15) |
| 147 | |
| 148 | #define AC_SETCLR (1<<15) |
| 149 | #define AC_UARTBRK (1<<11) |
| 150 | |
| 151 | #define SER_DTR (1<<7) |
| 152 | #define SER_RTS (1<<6) |
| 153 | #define SER_DCD (1<<5) |
| 154 | #define SER_CTS (1<<4) |
| 155 | #define SER_DSR (1<<3) |
| 156 | |
| 157 | static __inline__ void rtsdtr_ctrl(int bits) |
| 158 | { |
| 159 | ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR)); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * ------------------------------------------------------------ |
| 164 | * rs_stop() and rs_start() |
| 165 | * |
| 166 | * This routines are called before setting or resetting tty->stopped. |
| 167 | * They enable or disable transmitter interrupts, as necessary. |
| 168 | * ------------------------------------------------------------ |
| 169 | */ |
| 170 | static void rs_stop(struct tty_struct *tty) |
| 171 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 172 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | unsigned long flags; |
| 174 | |
| 175 | if (serial_paranoia_check(info, tty->name, "rs_stop")) |
| 176 | return; |
| 177 | |
| 178 | local_irq_save(flags); |
| 179 | if (info->IER & UART_IER_THRI) { |
| 180 | info->IER &= ~UART_IER_THRI; |
| 181 | /* disable Tx interrupt and remove any pending interrupts */ |
| 182 | custom.intena = IF_TBE; |
| 183 | mb(); |
| 184 | custom.intreq = IF_TBE; |
| 185 | mb(); |
| 186 | } |
| 187 | local_irq_restore(flags); |
| 188 | } |
| 189 | |
| 190 | static void rs_start(struct tty_struct *tty) |
| 191 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 192 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | unsigned long flags; |
| 194 | |
| 195 | if (serial_paranoia_check(info, tty->name, "rs_start")) |
| 196 | return; |
| 197 | |
| 198 | local_irq_save(flags); |
| 199 | if (info->xmit.head != info->xmit.tail |
| 200 | && info->xmit.buf |
| 201 | && !(info->IER & UART_IER_THRI)) { |
| 202 | info->IER |= UART_IER_THRI; |
| 203 | custom.intena = IF_SETCLR | IF_TBE; |
| 204 | mb(); |
| 205 | /* set a pending Tx Interrupt, transmitter should restart now */ |
| 206 | custom.intreq = IF_SETCLR | IF_TBE; |
| 207 | mb(); |
| 208 | } |
| 209 | local_irq_restore(flags); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * ---------------------------------------------------------------------- |
| 214 | * |
| 215 | * Here starts the interrupt handling routines. All of the following |
| 216 | * subroutines are declared as inline and are folded into |
| 217 | * rs_interrupt(). They were separated out for readability's sake. |
| 218 | * |
| 219 | * Note: rs_interrupt() is a "fast" interrupt, which means that it |
| 220 | * runs with interrupts turned off. People who may want to modify |
| 221 | * rs_interrupt() should try to keep the interrupt handler as fast as |
| 222 | * possible. After you are done making modifications, it is not a bad |
| 223 | * idea to do: |
| 224 | * |
| 225 | * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c |
| 226 | * |
| 227 | * and look at the resulting assemble code in serial.s. |
| 228 | * |
| 229 | * - Ted Ts'o (tytso@mit.edu), 7-Mar-93 |
| 230 | * ----------------------------------------------------------------------- |
| 231 | */ |
| 232 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 233 | static void receive_chars(struct serial_state *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
| 235 | int status; |
| 236 | int serdatr; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 237 | struct tty_struct *tty = info->tport.tty; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 238 | unsigned char ch, flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | struct async_icount *icount; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 240 | int oe = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 242 | icount = &info->icount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | status = UART_LSR_DR; /* We obviously have a character! */ |
| 245 | serdatr = custom.serdatr; |
| 246 | mb(); |
| 247 | custom.intreq = IF_RBF; |
| 248 | mb(); |
| 249 | |
| 250 | if((serdatr & 0x1ff) == 0) |
| 251 | status |= UART_LSR_BI; |
| 252 | if(serdatr & SDR_OVRUN) |
| 253 | status |= UART_LSR_OE; |
| 254 | |
| 255 | ch = serdatr & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | icount->rx++; |
| 257 | |
| 258 | #ifdef SERIAL_DEBUG_INTR |
| 259 | printk("DR%02x:%02x...", ch, status); |
| 260 | #endif |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 261 | flag = TTY_NORMAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * We don't handle parity or frame errors - but I have left |
| 265 | * the code in, since I'm not sure that the errors can't be |
| 266 | * detected. |
| 267 | */ |
| 268 | |
| 269 | if (status & (UART_LSR_BI | UART_LSR_PE | |
| 270 | UART_LSR_FE | UART_LSR_OE)) { |
| 271 | /* |
| 272 | * For statistics only |
| 273 | */ |
| 274 | if (status & UART_LSR_BI) { |
| 275 | status &= ~(UART_LSR_FE | UART_LSR_PE); |
| 276 | icount->brk++; |
| 277 | } else if (status & UART_LSR_PE) |
| 278 | icount->parity++; |
| 279 | else if (status & UART_LSR_FE) |
| 280 | icount->frame++; |
| 281 | if (status & UART_LSR_OE) |
| 282 | icount->overrun++; |
| 283 | |
| 284 | /* |
| 285 | * Now check to see if character should be |
| 286 | * ignored, and mask off conditions which |
| 287 | * should be ignored. |
| 288 | */ |
| 289 | if (status & info->ignore_status_mask) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 290 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | status &= info->read_status_mask; |
| 293 | |
| 294 | if (status & (UART_LSR_BI)) { |
| 295 | #ifdef SERIAL_DEBUG_INTR |
| 296 | printk("handling break...."); |
| 297 | #endif |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 298 | flag = TTY_BREAK; |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 299 | if (info->flags & ASYNC_SAK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | do_SAK(tty); |
| 301 | } else if (status & UART_LSR_PE) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 302 | flag = TTY_PARITY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | else if (status & UART_LSR_FE) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 304 | flag = TTY_FRAME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | if (status & UART_LSR_OE) { |
| 306 | /* |
| 307 | * Overrun is special, since it's |
| 308 | * reported immediately, and doesn't |
| 309 | * affect the current character |
| 310 | */ |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 311 | oe = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 314 | tty_insert_flip_char(tty, ch, flag); |
| 315 | if (oe == 1) |
| 316 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | tty_flip_buffer_push(tty); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 318 | out: |
| 319 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 322 | static void transmit_chars(struct serial_state *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
| 324 | custom.intreq = IF_TBE; |
| 325 | mb(); |
| 326 | if (info->x_char) { |
| 327 | custom.serdat = info->x_char | 0x100; |
| 328 | mb(); |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 329 | info->icount.tx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | info->x_char = 0; |
| 331 | return; |
| 332 | } |
| 333 | if (info->xmit.head == info->xmit.tail |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 334 | || info->tport.tty->stopped |
| 335 | || info->tport.tty->hw_stopped) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | info->IER &= ~UART_IER_THRI; |
| 337 | custom.intena = IF_TBE; |
| 338 | mb(); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100; |
| 343 | mb(); |
| 344 | info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1); |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 345 | info->icount.tx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
| 347 | if (CIRC_CNT(info->xmit.head, |
| 348 | info->xmit.tail, |
| 349 | SERIAL_XMIT_SIZE) < WAKEUP_CHARS) |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 350 | tty_wakeup(info->tport.tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | #ifdef SERIAL_DEBUG_INTR |
| 353 | printk("THRE..."); |
| 354 | #endif |
| 355 | if (info->xmit.head == info->xmit.tail) { |
| 356 | custom.intena = IF_TBE; |
| 357 | mb(); |
| 358 | info->IER &= ~UART_IER_THRI; |
| 359 | } |
| 360 | } |
| 361 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 362 | static void check_modem_status(struct serial_state *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
| 364 | unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR); |
| 365 | unsigned char dstatus; |
| 366 | struct async_icount *icount; |
| 367 | |
| 368 | /* Determine bits that have changed */ |
| 369 | dstatus = status ^ current_ctl_bits; |
| 370 | current_ctl_bits = status; |
| 371 | |
| 372 | if (dstatus) { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 373 | icount = &info->icount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | /* update input line counters */ |
| 375 | if (dstatus & SER_DSR) |
| 376 | icount->dsr++; |
| 377 | if (dstatus & SER_DCD) { |
| 378 | icount->dcd++; |
| 379 | #ifdef CONFIG_HARD_PPS |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 380 | if ((info->flags & ASYNC_HARDPPS_CD) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | !(status & SER_DCD)) |
| 382 | hardpps(); |
| 383 | #endif |
| 384 | } |
| 385 | if (dstatus & SER_CTS) |
| 386 | icount->cts++; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 387 | wake_up_interruptible(&info->tport.delta_msr_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 390 | if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR)) |
| 392 | printk("ttyS%d CD now %s...", info->line, |
| 393 | (!(status & SER_DCD)) ? "on" : "off"); |
| 394 | #endif |
| 395 | if (!(status & SER_DCD)) |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 396 | wake_up_interruptible(&info->tport.open_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | else { |
| 398 | #ifdef SERIAL_DEBUG_OPEN |
| 399 | printk("doing serial hangup..."); |
| 400 | #endif |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 401 | if (info->tport.tty) |
| 402 | tty_hangup(info->tport.tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 405 | if (info->flags & ASYNC_CTS_FLOW) { |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 406 | if (info->tport.tty->hw_stopped) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | if (!(status & SER_CTS)) { |
| 408 | #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW)) |
| 409 | printk("CTS tx start..."); |
| 410 | #endif |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 411 | info->tport.tty->hw_stopped = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | info->IER |= UART_IER_THRI; |
| 413 | custom.intena = IF_SETCLR | IF_TBE; |
| 414 | mb(); |
| 415 | /* set a pending Tx Interrupt, transmitter should restart now */ |
| 416 | custom.intreq = IF_SETCLR | IF_TBE; |
| 417 | mb(); |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 418 | tty_wakeup(info->tport.tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | } else { |
| 422 | if ((status & SER_CTS)) { |
| 423 | #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW)) |
| 424 | printk("CTS tx stop..."); |
| 425 | #endif |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 426 | info->tport.tty->hw_stopped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | info->IER &= ~UART_IER_THRI; |
| 428 | /* disable Tx interrupt and remove any pending interrupts */ |
| 429 | custom.intena = IF_TBE; |
| 430 | mb(); |
| 431 | custom.intreq = IF_TBE; |
| 432 | mb(); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 438 | static irqreturn_t ser_vbl_int( int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
| 440 | /* vbl is just a periodic interrupt we tie into to update modem status */ |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 441 | struct serial_state *info = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | /* |
| 443 | * TBD - is it better to unregister from this interrupt or to |
| 444 | * ignore it if MSI is clear ? |
| 445 | */ |
| 446 | if(info->IER & UART_IER_MSI) |
| 447 | check_modem_status(info); |
| 448 | return IRQ_HANDLED; |
| 449 | } |
| 450 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 451 | static irqreturn_t ser_rx_int(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 453 | struct serial_state *info = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | #ifdef SERIAL_DEBUG_INTR |
| 456 | printk("ser_rx_int..."); |
| 457 | #endif |
| 458 | |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 459 | if (!info->tport.tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | return IRQ_NONE; |
| 461 | |
| 462 | receive_chars(info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | #ifdef SERIAL_DEBUG_INTR |
| 464 | printk("end.\n"); |
| 465 | #endif |
| 466 | return IRQ_HANDLED; |
| 467 | } |
| 468 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 469 | static irqreturn_t ser_tx_int(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 471 | struct serial_state *info = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
| 473 | if (custom.serdatr & SDR_TBE) { |
| 474 | #ifdef SERIAL_DEBUG_INTR |
| 475 | printk("ser_tx_int..."); |
| 476 | #endif |
| 477 | |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 478 | if (!info->tport.tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | return IRQ_NONE; |
| 480 | |
| 481 | transmit_chars(info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | #ifdef SERIAL_DEBUG_INTR |
| 483 | printk("end.\n"); |
| 484 | #endif |
| 485 | } |
| 486 | return IRQ_HANDLED; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * ------------------------------------------------------------------- |
| 491 | * Here ends the serial interrupt routines. |
| 492 | * ------------------------------------------------------------------- |
| 493 | */ |
| 494 | |
| 495 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | * --------------------------------------------------------------- |
| 497 | * Low level utility subroutines for the serial driver: routines to |
| 498 | * figure out the appropriate timeout for an interrupt chain, routines |
| 499 | * to initialize and startup a serial port, and routines to shutdown a |
| 500 | * serial port. Useful stuff like that. |
| 501 | * --------------------------------------------------------------- |
| 502 | */ |
| 503 | |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 504 | static int startup(struct tty_struct *tty, struct serial_state *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
| 506 | unsigned long flags; |
| 507 | int retval=0; |
| 508 | unsigned long page; |
| 509 | |
| 510 | page = get_zeroed_page(GFP_KERNEL); |
| 511 | if (!page) |
| 512 | return -ENOMEM; |
| 513 | |
| 514 | local_irq_save(flags); |
| 515 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 516 | if (info->flags & ASYNC_INITIALIZED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | free_page(page); |
| 518 | goto errout; |
| 519 | } |
| 520 | |
| 521 | if (info->xmit.buf) |
| 522 | free_page(page); |
| 523 | else |
| 524 | info->xmit.buf = (unsigned char *) page; |
| 525 | |
| 526 | #ifdef SERIAL_DEBUG_OPEN |
| 527 | printk("starting up ttys%d ...", info->line); |
| 528 | #endif |
| 529 | |
| 530 | /* Clear anything in the input buffer */ |
| 531 | |
| 532 | custom.intreq = IF_RBF; |
| 533 | mb(); |
| 534 | |
| 535 | retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info); |
| 536 | if (retval) { |
| 537 | if (serial_isroot()) { |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 538 | set_bit(TTY_IO_ERROR, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | retval = 0; |
| 540 | } |
| 541 | goto errout; |
| 542 | } |
| 543 | |
| 544 | /* enable both Rx and Tx interrupts */ |
| 545 | custom.intena = IF_SETCLR | IF_RBF | IF_TBE; |
| 546 | mb(); |
| 547 | info->IER = UART_IER_MSI; |
| 548 | |
| 549 | /* remember current state of the DCD and CTS bits */ |
| 550 | current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR); |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | info->MCR = 0; |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 553 | if (C_BAUD(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | info->MCR = SER_DTR | SER_RTS; |
| 555 | rtsdtr_ctrl(info->MCR); |
| 556 | |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 557 | clear_bit(TTY_IO_ERROR, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | info->xmit.head = info->xmit.tail = 0; |
| 559 | |
| 560 | /* |
| 561 | * Set up the tty->alt_speed kludge |
| 562 | */ |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 563 | if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) |
| 564 | tty->alt_speed = 57600; |
| 565 | if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) |
| 566 | tty->alt_speed = 115200; |
| 567 | if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) |
| 568 | tty->alt_speed = 230400; |
| 569 | if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) |
| 570 | tty->alt_speed = 460800; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
| 572 | /* |
| 573 | * and set the speed of the serial port |
| 574 | */ |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 575 | change_speed(tty, info, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 577 | info->flags |= ASYNC_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | local_irq_restore(flags); |
| 579 | return 0; |
| 580 | |
| 581 | errout: |
| 582 | local_irq_restore(flags); |
| 583 | return retval; |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * This routine will shutdown a serial port; interrupts are disabled, and |
| 588 | * DTR is dropped if the hangup on close termio flag is on. |
| 589 | */ |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 590 | static void shutdown(struct tty_struct *tty, struct serial_state *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
| 592 | unsigned long flags; |
| 593 | struct serial_state *state; |
| 594 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 595 | if (!(info->flags & ASYNC_INITIALIZED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | return; |
| 597 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 598 | state = info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | #ifdef SERIAL_DEBUG_OPEN |
| 601 | printk("Shutting down serial port %d ....\n", info->line); |
| 602 | #endif |
| 603 | |
| 604 | local_irq_save(flags); /* Disable interrupts */ |
| 605 | |
| 606 | /* |
| 607 | * clear delta_msr_wait queue to avoid mem leaks: we may free the irq |
| 608 | * here so the queue might never be waken up |
| 609 | */ |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 610 | wake_up_interruptible(&info->tport.delta_msr_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | /* |
| 613 | * Free the IRQ, if necessary |
| 614 | */ |
| 615 | free_irq(IRQ_AMIGA_VERTB, info); |
| 616 | |
| 617 | if (info->xmit.buf) { |
| 618 | free_page((unsigned long) info->xmit.buf); |
| 619 | info->xmit.buf = NULL; |
| 620 | } |
| 621 | |
| 622 | info->IER = 0; |
| 623 | custom.intena = IF_RBF | IF_TBE; |
| 624 | mb(); |
| 625 | |
| 626 | /* disable break condition */ |
| 627 | custom.adkcon = AC_UARTBRK; |
| 628 | mb(); |
| 629 | |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 630 | if (tty->termios->c_cflag & HUPCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | info->MCR &= ~(SER_DTR|SER_RTS); |
| 632 | rtsdtr_ctrl(info->MCR); |
| 633 | |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 634 | set_bit(TTY_IO_ERROR, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 636 | info->flags &= ~ASYNC_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | local_irq_restore(flags); |
| 638 | } |
| 639 | |
| 640 | |
| 641 | /* |
| 642 | * This routine is called to set the UART divisor registers to match |
| 643 | * the specified baud rate for a serial port. |
| 644 | */ |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 645 | static void change_speed(struct tty_struct *tty, struct serial_state *info, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 646 | struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
| 648 | int quot = 0, baud_base, baud; |
| 649 | unsigned cflag, cval = 0; |
| 650 | int bits; |
| 651 | unsigned long flags; |
| 652 | |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 653 | cflag = tty->termios->c_cflag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
| 655 | /* Byte size is always 8 bits plus parity bit if requested */ |
| 656 | |
| 657 | cval = 3; bits = 10; |
| 658 | if (cflag & CSTOPB) { |
| 659 | cval |= 0x04; |
| 660 | bits++; |
| 661 | } |
| 662 | if (cflag & PARENB) { |
| 663 | cval |= UART_LCR_PARITY; |
| 664 | bits++; |
| 665 | } |
| 666 | if (!(cflag & PARODD)) |
| 667 | cval |= UART_LCR_EPAR; |
| 668 | #ifdef CMSPAR |
| 669 | if (cflag & CMSPAR) |
| 670 | cval |= UART_LCR_SPAR; |
| 671 | #endif |
| 672 | |
| 673 | /* Determine divisor based on baud rate */ |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 674 | baud = tty_get_baud_rate(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | if (!baud) |
| 676 | baud = 9600; /* B0 transition handled in rs_set_termios */ |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 677 | baud_base = info->baud_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (baud == 38400 && |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 679 | ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) |
| 680 | quot = info->custom_divisor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | else { |
| 682 | if (baud == 134) |
| 683 | /* Special case since 134 is really 134.5 */ |
| 684 | quot = (2*baud_base / 269); |
| 685 | else if (baud) |
| 686 | quot = baud_base / baud; |
| 687 | } |
| 688 | /* If the quotient is zero refuse the change */ |
| 689 | if (!quot && old_termios) { |
Alan Cox | db0ef08 | 2007-07-15 23:41:47 -0700 | [diff] [blame] | 690 | /* FIXME: Will need updating for new tty in the end */ |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 691 | tty->termios->c_cflag &= ~CBAUD; |
| 692 | tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD); |
| 693 | baud = tty_get_baud_rate(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | if (!baud) |
| 695 | baud = 9600; |
| 696 | if (baud == 38400 && |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 697 | ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) |
| 698 | quot = info->custom_divisor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | else { |
| 700 | if (baud == 134) |
| 701 | /* Special case since 134 is really 134.5 */ |
| 702 | quot = (2*baud_base / 269); |
| 703 | else if (baud) |
| 704 | quot = baud_base / baud; |
| 705 | } |
| 706 | } |
| 707 | /* As a last resort, if the quotient is zero, default to 9600 bps */ |
| 708 | if (!quot) |
| 709 | quot = baud_base / 9600; |
| 710 | info->quot = quot; |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 711 | info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | info->timeout += HZ/50; /* Add .02 seconds of slop */ |
| 713 | |
| 714 | /* CTS flow control flag and modem status interrupts */ |
| 715 | info->IER &= ~UART_IER_MSI; |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 716 | if (info->flags & ASYNC_HARDPPS_CD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | info->IER |= UART_IER_MSI; |
| 718 | if (cflag & CRTSCTS) { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 719 | info->flags |= ASYNC_CTS_FLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | info->IER |= UART_IER_MSI; |
| 721 | } else |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 722 | info->flags &= ~ASYNC_CTS_FLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | if (cflag & CLOCAL) |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 724 | info->flags &= ~ASYNC_CHECK_CD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | else { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 726 | info->flags |= ASYNC_CHECK_CD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | info->IER |= UART_IER_MSI; |
| 728 | } |
| 729 | /* TBD: |
Thadeu Lima de Souza Cascardo | 4b512d2 | 2009-04-14 23:14:10 -0300 | [diff] [blame] | 730 | * Does clearing IER_MSI imply that we should disable the VBL interrupt ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | */ |
| 732 | |
| 733 | /* |
| 734 | * Set up parity check flag |
| 735 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
| 737 | info->read_status_mask = UART_LSR_OE | UART_LSR_DR; |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 738 | if (I_INPCK(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | info->read_status_mask |= UART_LSR_FE | UART_LSR_PE; |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 740 | if (I_BRKINT(tty) || I_PARMRK(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | info->read_status_mask |= UART_LSR_BI; |
| 742 | |
| 743 | /* |
| 744 | * Characters to ignore |
| 745 | */ |
| 746 | info->ignore_status_mask = 0; |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 747 | if (I_IGNPAR(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE; |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 749 | if (I_IGNBRK(tty)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | info->ignore_status_mask |= UART_LSR_BI; |
| 751 | /* |
| 752 | * If we're ignore parity and break indicators, ignore |
| 753 | * overruns too. (For real raw support). |
| 754 | */ |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 755 | if (I_IGNPAR(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | info->ignore_status_mask |= UART_LSR_OE; |
| 757 | } |
| 758 | /* |
| 759 | * !!! ignore all characters if CREAD is not set |
| 760 | */ |
| 761 | if ((cflag & CREAD) == 0) |
| 762 | info->ignore_status_mask |= UART_LSR_DR; |
| 763 | local_irq_save(flags); |
| 764 | |
| 765 | { |
| 766 | short serper; |
| 767 | |
| 768 | /* Set up the baud rate */ |
| 769 | serper = quot - 1; |
| 770 | |
| 771 | /* Enable or disable parity bit */ |
| 772 | |
| 773 | if(cval & UART_LCR_PARITY) |
| 774 | serper |= (SERPER_PARENB); |
| 775 | |
| 776 | custom.serper = serper; |
| 777 | mb(); |
| 778 | } |
| 779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | local_irq_restore(flags); |
| 781 | } |
| 782 | |
Alan Cox | 257afa3 | 2008-04-30 00:54:02 -0700 | [diff] [blame] | 783 | static int rs_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 785 | struct serial_state *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | unsigned long flags; |
| 787 | |
TINNES Julien RD-MAPS-ISS | d9ad7ef | 2005-06-23 00:10:08 -0700 | [diff] [blame] | 788 | info = tty->driver_data; |
| 789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | if (serial_paranoia_check(info, tty->name, "rs_put_char")) |
Alan Cox | 257afa3 | 2008-04-30 00:54:02 -0700 | [diff] [blame] | 791 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
TINNES Julien RD-MAPS-ISS | d9ad7ef | 2005-06-23 00:10:08 -0700 | [diff] [blame] | 793 | if (!info->xmit.buf) |
Alan Cox | 257afa3 | 2008-04-30 00:54:02 -0700 | [diff] [blame] | 794 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
| 796 | local_irq_save(flags); |
| 797 | if (CIRC_SPACE(info->xmit.head, |
| 798 | info->xmit.tail, |
| 799 | SERIAL_XMIT_SIZE) == 0) { |
| 800 | local_irq_restore(flags); |
Alan Cox | 257afa3 | 2008-04-30 00:54:02 -0700 | [diff] [blame] | 801 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | info->xmit.buf[info->xmit.head++] = ch; |
| 805 | info->xmit.head &= SERIAL_XMIT_SIZE-1; |
| 806 | local_irq_restore(flags); |
Alan Cox | 257afa3 | 2008-04-30 00:54:02 -0700 | [diff] [blame] | 807 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | static void rs_flush_chars(struct tty_struct *tty) |
| 811 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 812 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | unsigned long flags; |
| 814 | |
| 815 | if (serial_paranoia_check(info, tty->name, "rs_flush_chars")) |
| 816 | return; |
| 817 | |
| 818 | if (info->xmit.head == info->xmit.tail |
| 819 | || tty->stopped |
| 820 | || tty->hw_stopped |
| 821 | || !info->xmit.buf) |
| 822 | return; |
| 823 | |
| 824 | local_irq_save(flags); |
| 825 | info->IER |= UART_IER_THRI; |
| 826 | custom.intena = IF_SETCLR | IF_TBE; |
| 827 | mb(); |
| 828 | /* set a pending Tx Interrupt, transmitter should restart now */ |
| 829 | custom.intreq = IF_SETCLR | IF_TBE; |
| 830 | mb(); |
| 831 | local_irq_restore(flags); |
| 832 | } |
| 833 | |
| 834 | static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count) |
| 835 | { |
| 836 | int c, ret = 0; |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 837 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | unsigned long flags; |
| 839 | |
| 840 | if (serial_paranoia_check(info, tty->name, "rs_write")) |
| 841 | return 0; |
| 842 | |
Jiri Slaby | b3218a7 | 2006-10-04 02:15:27 -0700 | [diff] [blame] | 843 | if (!info->xmit.buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | return 0; |
| 845 | |
Jiri Kosina | 3abf3be | 2007-02-10 01:46:48 -0800 | [diff] [blame] | 846 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | while (1) { |
| 848 | c = CIRC_SPACE_TO_END(info->xmit.head, |
| 849 | info->xmit.tail, |
| 850 | SERIAL_XMIT_SIZE); |
| 851 | if (count < c) |
| 852 | c = count; |
| 853 | if (c <= 0) { |
| 854 | break; |
| 855 | } |
| 856 | memcpy(info->xmit.buf + info->xmit.head, buf, c); |
| 857 | info->xmit.head = ((info->xmit.head + c) & |
| 858 | (SERIAL_XMIT_SIZE-1)); |
| 859 | buf += c; |
| 860 | count -= c; |
| 861 | ret += c; |
| 862 | } |
| 863 | local_irq_restore(flags); |
| 864 | |
| 865 | if (info->xmit.head != info->xmit.tail |
| 866 | && !tty->stopped |
| 867 | && !tty->hw_stopped |
| 868 | && !(info->IER & UART_IER_THRI)) { |
| 869 | info->IER |= UART_IER_THRI; |
| 870 | local_irq_disable(); |
| 871 | custom.intena = IF_SETCLR | IF_TBE; |
| 872 | mb(); |
| 873 | /* set a pending Tx Interrupt, transmitter should restart now */ |
| 874 | custom.intreq = IF_SETCLR | IF_TBE; |
| 875 | mb(); |
| 876 | local_irq_restore(flags); |
| 877 | } |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | static int rs_write_room(struct tty_struct *tty) |
| 882 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 883 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | if (serial_paranoia_check(info, tty->name, "rs_write_room")) |
| 886 | return 0; |
| 887 | return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); |
| 888 | } |
| 889 | |
| 890 | static int rs_chars_in_buffer(struct tty_struct *tty) |
| 891 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 892 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | |
| 894 | if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer")) |
| 895 | return 0; |
| 896 | return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); |
| 897 | } |
| 898 | |
| 899 | static void rs_flush_buffer(struct tty_struct *tty) |
| 900 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 901 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | unsigned long flags; |
| 903 | |
| 904 | if (serial_paranoia_check(info, tty->name, "rs_flush_buffer")) |
| 905 | return; |
| 906 | local_irq_save(flags); |
| 907 | info->xmit.head = info->xmit.tail = 0; |
| 908 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | tty_wakeup(tty); |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * This function is used to send a high-priority XON/XOFF character to |
| 914 | * the device |
| 915 | */ |
| 916 | static void rs_send_xchar(struct tty_struct *tty, char ch) |
| 917 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 918 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | unsigned long flags; |
| 920 | |
| 921 | if (serial_paranoia_check(info, tty->name, "rs_send_char")) |
| 922 | return; |
| 923 | |
| 924 | info->x_char = ch; |
| 925 | if (ch) { |
| 926 | /* Make sure transmit interrupts are on */ |
| 927 | |
| 928 | /* Check this ! */ |
| 929 | local_irq_save(flags); |
| 930 | if(!(custom.intenar & IF_TBE)) { |
| 931 | custom.intena = IF_SETCLR | IF_TBE; |
| 932 | mb(); |
| 933 | /* set a pending Tx Interrupt, transmitter should restart now */ |
| 934 | custom.intreq = IF_SETCLR | IF_TBE; |
| 935 | mb(); |
| 936 | } |
| 937 | local_irq_restore(flags); |
| 938 | |
| 939 | info->IER |= UART_IER_THRI; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * ------------------------------------------------------------ |
| 945 | * rs_throttle() |
| 946 | * |
| 947 | * This routine is called by the upper-layer tty layer to signal that |
| 948 | * incoming characters should be throttled. |
| 949 | * ------------------------------------------------------------ |
| 950 | */ |
| 951 | static void rs_throttle(struct tty_struct * tty) |
| 952 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 953 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | unsigned long flags; |
| 955 | #ifdef SERIAL_DEBUG_THROTTLE |
| 956 | char buf[64]; |
| 957 | |
| 958 | printk("throttle %s: %d....\n", tty_name(tty, buf), |
| 959 | tty->ldisc.chars_in_buffer(tty)); |
| 960 | #endif |
| 961 | |
| 962 | if (serial_paranoia_check(info, tty->name, "rs_throttle")) |
| 963 | return; |
| 964 | |
| 965 | if (I_IXOFF(tty)) |
| 966 | rs_send_xchar(tty, STOP_CHAR(tty)); |
| 967 | |
| 968 | if (tty->termios->c_cflag & CRTSCTS) |
| 969 | info->MCR &= ~SER_RTS; |
| 970 | |
| 971 | local_irq_save(flags); |
| 972 | rtsdtr_ctrl(info->MCR); |
| 973 | local_irq_restore(flags); |
| 974 | } |
| 975 | |
| 976 | static void rs_unthrottle(struct tty_struct * tty) |
| 977 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 978 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | unsigned long flags; |
| 980 | #ifdef SERIAL_DEBUG_THROTTLE |
| 981 | char buf[64]; |
| 982 | |
| 983 | printk("unthrottle %s: %d....\n", tty_name(tty, buf), |
| 984 | tty->ldisc.chars_in_buffer(tty)); |
| 985 | #endif |
| 986 | |
| 987 | if (serial_paranoia_check(info, tty->name, "rs_unthrottle")) |
| 988 | return; |
| 989 | |
| 990 | if (I_IXOFF(tty)) { |
| 991 | if (info->x_char) |
| 992 | info->x_char = 0; |
| 993 | else |
| 994 | rs_send_xchar(tty, START_CHAR(tty)); |
| 995 | } |
| 996 | if (tty->termios->c_cflag & CRTSCTS) |
| 997 | info->MCR |= SER_RTS; |
| 998 | local_irq_save(flags); |
| 999 | rtsdtr_ctrl(info->MCR); |
| 1000 | local_irq_restore(flags); |
| 1001 | } |
| 1002 | |
| 1003 | /* |
| 1004 | * ------------------------------------------------------------ |
| 1005 | * rs_ioctl() and friends |
| 1006 | * ------------------------------------------------------------ |
| 1007 | */ |
| 1008 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1009 | static int get_serial_info(struct serial_state *state, |
Al Viro | ab14cae | 2006-01-12 01:06:30 -0800 | [diff] [blame] | 1010 | struct serial_struct __user * retinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | { |
| 1012 | struct serial_struct tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
| 1014 | if (!retinfo) |
| 1015 | return -EFAULT; |
| 1016 | memset(&tmp, 0, sizeof(tmp)); |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 1017 | tty_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | tmp.type = state->type; |
| 1019 | tmp.line = state->line; |
| 1020 | tmp.port = state->port; |
| 1021 | tmp.irq = state->irq; |
| 1022 | tmp.flags = state->flags; |
| 1023 | tmp.xmit_fifo_size = state->xmit_fifo_size; |
| 1024 | tmp.baud_base = state->baud_base; |
Jiri Slaby | 799be6f | 2012-03-05 14:52:25 +0100 | [diff] [blame] | 1025 | tmp.close_delay = state->tport.close_delay; |
| 1026 | tmp.closing_wait = state->tport.closing_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | tmp.custom_divisor = state->custom_divisor; |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 1028 | tty_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | if (copy_to_user(retinfo,&tmp,sizeof(*retinfo))) |
| 1030 | return -EFAULT; |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1034 | static int set_serial_info(struct tty_struct *tty, struct serial_state *state, |
Al Viro | ab14cae | 2006-01-12 01:06:30 -0800 | [diff] [blame] | 1035 | struct serial_struct __user * new_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | { |
| 1037 | struct serial_struct new_serial; |
Jiri Slaby | 0f9b968 | 2012-03-05 14:52:21 +0100 | [diff] [blame] | 1038 | bool change_spd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | int retval = 0; |
| 1040 | |
| 1041 | if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) |
| 1042 | return -EFAULT; |
Alan Cox | e18ce49 | 2008-04-30 00:53:16 -0700 | [diff] [blame] | 1043 | |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 1044 | tty_lock(); |
Jiri Slaby | 0f9b968 | 2012-03-05 14:52:21 +0100 | [diff] [blame] | 1045 | change_spd = ((new_serial.flags ^ state->flags) & ASYNC_SPD_MASK) || |
| 1046 | new_serial.custom_divisor != state->custom_divisor; |
| 1047 | if (new_serial.irq != state->irq || new_serial.port != state->port || |
| 1048 | new_serial.xmit_fifo_size != state->xmit_fifo_size) { |
| 1049 | tty_unlock(); |
| 1050 | return -EINVAL; |
Alan Cox | e18ce49 | 2008-04-30 00:53:16 -0700 | [diff] [blame] | 1051 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
| 1053 | if (!serial_isroot()) { |
| 1054 | if ((new_serial.baud_base != state->baud_base) || |
Jiri Slaby | 799be6f | 2012-03-05 14:52:25 +0100 | [diff] [blame] | 1055 | (new_serial.close_delay != state->tport.close_delay) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | (new_serial.xmit_fifo_size != state->xmit_fifo_size) || |
| 1057 | ((new_serial.flags & ~ASYNC_USR_MASK) != |
| 1058 | (state->flags & ~ASYNC_USR_MASK))) |
| 1059 | return -EPERM; |
| 1060 | state->flags = ((state->flags & ~ASYNC_USR_MASK) | |
| 1061 | (new_serial.flags & ASYNC_USR_MASK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | state->custom_divisor = new_serial.custom_divisor; |
| 1063 | goto check_and_exit; |
| 1064 | } |
| 1065 | |
Alan Cox | e18ce49 | 2008-04-30 00:53:16 -0700 | [diff] [blame] | 1066 | if (new_serial.baud_base < 9600) { |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 1067 | tty_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | return -EINVAL; |
Alan Cox | e18ce49 | 2008-04-30 00:53:16 -0700 | [diff] [blame] | 1069 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | /* |
| 1072 | * OK, past this point, all the error checking has been done. |
| 1073 | * At this point, we start making changes..... |
| 1074 | */ |
| 1075 | |
| 1076 | state->baud_base = new_serial.baud_base; |
| 1077 | state->flags = ((state->flags & ~ASYNC_FLAGS) | |
| 1078 | (new_serial.flags & ASYNC_FLAGS)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | state->custom_divisor = new_serial.custom_divisor; |
Jiri Slaby | 799be6f | 2012-03-05 14:52:25 +0100 | [diff] [blame] | 1080 | state->tport.close_delay = new_serial.close_delay * HZ/100; |
| 1081 | state->tport.closing_wait = new_serial.closing_wait * HZ/100; |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1082 | tty->low_latency = (state->flags & ASYNC_LOW_LATENCY) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | |
| 1084 | check_and_exit: |
Jiri Slaby | fef2107 | 2012-03-05 14:52:14 +0100 | [diff] [blame] | 1085 | if (state->flags & ASYNC_INITIALIZED) { |
Jiri Slaby | 0f9b968 | 2012-03-05 14:52:21 +0100 | [diff] [blame] | 1086 | if (change_spd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1088 | tty->alt_speed = 57600; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1090 | tty->alt_speed = 115200; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1092 | tty->alt_speed = 230400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1094 | tty->alt_speed = 460800; |
| 1095 | change_speed(tty, state, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | } |
| 1097 | } else |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1098 | retval = startup(tty, state); |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 1099 | tty_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | return retval; |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | /* |
| 1105 | * get_lsr_info - get line status register info |
| 1106 | * |
| 1107 | * Purpose: Let user call ioctl() to get info when the UART physically |
| 1108 | * is emptied. On bus types like RS485, the transmitter must |
| 1109 | * release the bus after transmitting. This must be done when |
| 1110 | * the transmit shift register is empty, not be done when the |
| 1111 | * transmit holding register is empty. This functionality |
| 1112 | * allows an RS485 driver to be written in user space. |
| 1113 | */ |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1114 | static int get_lsr_info(struct serial_state *info, unsigned int __user *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | { |
| 1116 | unsigned char status; |
| 1117 | unsigned int result; |
| 1118 | unsigned long flags; |
| 1119 | |
| 1120 | local_irq_save(flags); |
| 1121 | status = custom.serdatr; |
| 1122 | mb(); |
| 1123 | local_irq_restore(flags); |
| 1124 | result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0); |
| 1125 | if (copy_to_user(value, &result, sizeof(int))) |
| 1126 | return -EFAULT; |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 1131 | static int rs_tiocmget(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1133 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | unsigned char control, status; |
| 1135 | unsigned long flags; |
| 1136 | |
| 1137 | if (serial_paranoia_check(info, tty->name, "rs_ioctl")) |
| 1138 | return -ENODEV; |
| 1139 | if (tty->flags & (1 << TTY_IO_ERROR)) |
| 1140 | return -EIO; |
| 1141 | |
| 1142 | control = info->MCR; |
| 1143 | local_irq_save(flags); |
| 1144 | status = ciab.pra; |
| 1145 | local_irq_restore(flags); |
| 1146 | return ((control & SER_RTS) ? TIOCM_RTS : 0) |
| 1147 | | ((control & SER_DTR) ? TIOCM_DTR : 0) |
| 1148 | | (!(status & SER_DCD) ? TIOCM_CAR : 0) |
| 1149 | | (!(status & SER_DSR) ? TIOCM_DSR : 0) |
| 1150 | | (!(status & SER_CTS) ? TIOCM_CTS : 0); |
| 1151 | } |
| 1152 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 1153 | static int rs_tiocmset(struct tty_struct *tty, unsigned int set, |
| 1154 | unsigned int clear) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1156 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | unsigned long flags; |
| 1158 | |
| 1159 | if (serial_paranoia_check(info, tty->name, "rs_ioctl")) |
| 1160 | return -ENODEV; |
| 1161 | if (tty->flags & (1 << TTY_IO_ERROR)) |
| 1162 | return -EIO; |
| 1163 | |
| 1164 | local_irq_save(flags); |
| 1165 | if (set & TIOCM_RTS) |
| 1166 | info->MCR |= SER_RTS; |
| 1167 | if (set & TIOCM_DTR) |
| 1168 | info->MCR |= SER_DTR; |
| 1169 | if (clear & TIOCM_RTS) |
| 1170 | info->MCR &= ~SER_RTS; |
| 1171 | if (clear & TIOCM_DTR) |
| 1172 | info->MCR &= ~SER_DTR; |
| 1173 | rtsdtr_ctrl(info->MCR); |
| 1174 | local_irq_restore(flags); |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | * rs_break() --- routine which turns the break handling on or off |
| 1180 | */ |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 1181 | static int rs_break(struct tty_struct *tty, int break_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1183 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | unsigned long flags; |
| 1185 | |
| 1186 | if (serial_paranoia_check(info, tty->name, "rs_break")) |
Geert Uytterhoeven | 970a8a5 | 2008-08-06 22:19:39 +0200 | [diff] [blame] | 1187 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | |
| 1189 | local_irq_save(flags); |
| 1190 | if (break_state == -1) |
| 1191 | custom.adkcon = AC_SETCLR | AC_UARTBRK; |
| 1192 | else |
| 1193 | custom.adkcon = AC_UARTBRK; |
| 1194 | mb(); |
| 1195 | local_irq_restore(flags); |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 1196 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Alan Cox | 0587102 | 2010-09-16 18:21:52 +0100 | [diff] [blame] | 1199 | /* |
| 1200 | * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) |
| 1201 | * Return: write counters to the user passed counter struct |
| 1202 | * NB: both 1->0 and 0->1 transitions are counted except for |
| 1203 | * RI where only 0->1 is counted. |
| 1204 | */ |
| 1205 | static int rs_get_icount(struct tty_struct *tty, |
| 1206 | struct serial_icounter_struct *icount) |
| 1207 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1208 | struct serial_state *info = tty->driver_data; |
Alan Cox | 0587102 | 2010-09-16 18:21:52 +0100 | [diff] [blame] | 1209 | struct async_icount cnow; |
| 1210 | unsigned long flags; |
| 1211 | |
| 1212 | local_irq_save(flags); |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1213 | cnow = info->icount; |
Alan Cox | 0587102 | 2010-09-16 18:21:52 +0100 | [diff] [blame] | 1214 | local_irq_restore(flags); |
| 1215 | icount->cts = cnow.cts; |
| 1216 | icount->dsr = cnow.dsr; |
| 1217 | icount->rng = cnow.rng; |
| 1218 | icount->dcd = cnow.dcd; |
| 1219 | icount->rx = cnow.rx; |
| 1220 | icount->tx = cnow.tx; |
| 1221 | icount->frame = cnow.frame; |
| 1222 | icount->overrun = cnow.overrun; |
| 1223 | icount->parity = cnow.parity; |
| 1224 | icount->brk = cnow.brk; |
| 1225 | icount->buf_overrun = cnow.buf_overrun; |
| 1226 | |
| 1227 | return 0; |
| 1228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 1230 | static int rs_ioctl(struct tty_struct *tty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | unsigned int cmd, unsigned long arg) |
| 1232 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1233 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | struct async_icount cprev, cnow; /* kernel counter temps */ |
Al Viro | ab14cae | 2006-01-12 01:06:30 -0800 | [diff] [blame] | 1235 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | unsigned long flags; |
| 1237 | |
| 1238 | if (serial_paranoia_check(info, tty->name, "rs_ioctl")) |
| 1239 | return -ENODEV; |
| 1240 | |
| 1241 | if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && |
| 1242 | (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) && |
| 1243 | (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) { |
| 1244 | if (tty->flags & (1 << TTY_IO_ERROR)) |
| 1245 | return -EIO; |
| 1246 | } |
| 1247 | |
| 1248 | switch (cmd) { |
| 1249 | case TIOCGSERIAL: |
Al Viro | ab14cae | 2006-01-12 01:06:30 -0800 | [diff] [blame] | 1250 | return get_serial_info(info, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | case TIOCSSERIAL: |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1252 | return set_serial_info(tty, info, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | case TIOCSERCONFIG: |
| 1254 | return 0; |
| 1255 | |
| 1256 | case TIOCSERGETLSR: /* Get line status register */ |
Al Viro | ab14cae | 2006-01-12 01:06:30 -0800 | [diff] [blame] | 1257 | return get_lsr_info(info, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
| 1259 | case TIOCSERGSTRUCT: |
Al Viro | ab14cae | 2006-01-12 01:06:30 -0800 | [diff] [blame] | 1260 | if (copy_to_user(argp, |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1261 | info, sizeof(struct serial_state))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | return -EFAULT; |
| 1263 | return 0; |
| 1264 | |
| 1265 | /* |
| 1266 | * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change |
| 1267 | * - mask passed in arg for lines of interest |
| 1268 | * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) |
| 1269 | * Caller should use TIOCGICOUNT to see which one it was |
| 1270 | */ |
| 1271 | case TIOCMIWAIT: |
| 1272 | local_irq_save(flags); |
| 1273 | /* note the counters on entry */ |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1274 | cprev = info->icount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | local_irq_restore(flags); |
| 1276 | while (1) { |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1277 | interruptible_sleep_on(&info->tport.delta_msr_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | /* see if a signal did it */ |
| 1279 | if (signal_pending(current)) |
| 1280 | return -ERESTARTSYS; |
| 1281 | local_irq_save(flags); |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1282 | cnow = info->icount; /* atomic copy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | local_irq_restore(flags); |
| 1284 | if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && |
| 1285 | cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) |
| 1286 | return -EIO; /* no change => error */ |
| 1287 | if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || |
| 1288 | ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || |
| 1289 | ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || |
| 1290 | ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) { |
| 1291 | return 0; |
| 1292 | } |
| 1293 | cprev = cnow; |
| 1294 | } |
| 1295 | /* NOTREACHED */ |
| 1296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | case TIOCSERGWILD: |
| 1298 | case TIOCSERSWILD: |
| 1299 | /* "setserial -W" is called in Debian boot */ |
| 1300 | printk ("TIOCSER?WILD ioctl obsolete, ignored.\n"); |
| 1301 | return 0; |
| 1302 | |
| 1303 | default: |
| 1304 | return -ENOIOCTLCMD; |
| 1305 | } |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1309 | static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1311 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | unsigned long flags; |
| 1313 | unsigned int cflag = tty->termios->c_cflag; |
| 1314 | |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1315 | change_speed(tty, info, old_termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
| 1317 | /* Handle transition to B0 status */ |
| 1318 | if ((old_termios->c_cflag & CBAUD) && |
| 1319 | !(cflag & CBAUD)) { |
| 1320 | info->MCR &= ~(SER_DTR|SER_RTS); |
| 1321 | local_irq_save(flags); |
| 1322 | rtsdtr_ctrl(info->MCR); |
| 1323 | local_irq_restore(flags); |
| 1324 | } |
| 1325 | |
| 1326 | /* Handle transition away from B0 status */ |
| 1327 | if (!(old_termios->c_cflag & CBAUD) && |
| 1328 | (cflag & CBAUD)) { |
| 1329 | info->MCR |= SER_DTR; |
| 1330 | if (!(tty->termios->c_cflag & CRTSCTS) || |
| 1331 | !test_bit(TTY_THROTTLED, &tty->flags)) { |
| 1332 | info->MCR |= SER_RTS; |
| 1333 | } |
| 1334 | local_irq_save(flags); |
| 1335 | rtsdtr_ctrl(info->MCR); |
| 1336 | local_irq_restore(flags); |
| 1337 | } |
| 1338 | |
| 1339 | /* Handle turning off CRTSCTS */ |
| 1340 | if ((old_termios->c_cflag & CRTSCTS) && |
| 1341 | !(tty->termios->c_cflag & CRTSCTS)) { |
| 1342 | tty->hw_stopped = 0; |
| 1343 | rs_start(tty); |
| 1344 | } |
| 1345 | |
| 1346 | #if 0 |
| 1347 | /* |
| 1348 | * No need to wake up processes in open wait, since they |
| 1349 | * sample the CLOCAL flag once, and don't recheck it. |
| 1350 | * XXX It's not clear whether the current behavior is correct |
| 1351 | * or not. Hence, this may change..... |
| 1352 | */ |
| 1353 | if (!(old_termios->c_cflag & CLOCAL) && |
| 1354 | (tty->termios->c_cflag & CLOCAL)) |
| 1355 | wake_up_interruptible(&info->open_wait); |
| 1356 | #endif |
| 1357 | } |
| 1358 | |
| 1359 | /* |
| 1360 | * ------------------------------------------------------------ |
| 1361 | * rs_close() |
| 1362 | * |
| 1363 | * This routine is called when the serial port gets closed. First, we |
| 1364 | * wait for the last remaining data to be sent. Then, we unlink its |
| 1365 | * async structure from the interrupt chain if necessary, and we free |
| 1366 | * that IRQ if nothing is left in the chain. |
| 1367 | * ------------------------------------------------------------ |
| 1368 | */ |
| 1369 | static void rs_close(struct tty_struct *tty, struct file * filp) |
| 1370 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1371 | struct serial_state *state = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | unsigned long flags; |
| 1373 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1374 | if (!state || serial_paranoia_check(state, tty->name, "rs_close")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | return; |
| 1376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | local_irq_save(flags); |
| 1378 | |
| 1379 | if (tty_hung_up_p(filp)) { |
| 1380 | DBG_CNT("before DEC-hung"); |
| 1381 | local_irq_restore(flags); |
| 1382 | return; |
| 1383 | } |
| 1384 | |
| 1385 | #ifdef SERIAL_DEBUG_OPEN |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1386 | printk("rs_close ttys%d, count = %d\n", state->line, state->tport.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | #endif |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1388 | if ((tty->count == 1) && (state->tport.count != 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | /* |
| 1390 | * Uh, oh. tty->count is 1, which means that the tty |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1391 | * structure will be freed. state->tport.count should always |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | * be one in these conditions. If it's greater than |
| 1393 | * one, we've got real problems, since it means the |
| 1394 | * serial port won't be shutdown. |
| 1395 | */ |
| 1396 | printk("rs_close: bad serial port count; tty->count is 1, " |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1397 | "state->tport.count is %d\n", state->tport.count); |
| 1398 | state->tport.count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | } |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1400 | if (--state->tport.count < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | printk("rs_close: bad serial port count for ttys%d: %d\n", |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1402 | state->line, state->tport.count); |
| 1403 | state->tport.count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | } |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1405 | if (state->tport.count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | DBG_CNT("before DEC-2"); |
| 1407 | local_irq_restore(flags); |
| 1408 | return; |
| 1409 | } |
Jiri Slaby | fef2107 | 2012-03-05 14:52:14 +0100 | [diff] [blame] | 1410 | state->flags |= ASYNC_CLOSING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | /* |
| 1412 | * Now we wait for the transmit buffer to clear; and we notify |
| 1413 | * the line discipline to only process XON/XOFF characters. |
| 1414 | */ |
| 1415 | tty->closing = 1; |
Jiri Slaby | 799be6f | 2012-03-05 14:52:25 +0100 | [diff] [blame] | 1416 | if (state->tport.closing_wait != ASYNC_CLOSING_WAIT_NONE) |
| 1417 | tty_wait_until_sent(tty, state->tport.closing_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | /* |
| 1419 | * At this point we stop accepting input. To do this, we |
| 1420 | * disable the receive line status interrupts, and tell the |
| 1421 | * interrupt driver to stop checking the data ready bit in the |
| 1422 | * line status register. |
| 1423 | */ |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1424 | state->read_status_mask &= ~UART_LSR_DR; |
Jiri Slaby | fef2107 | 2012-03-05 14:52:14 +0100 | [diff] [blame] | 1425 | if (state->flags & ASYNC_INITIALIZED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | /* disable receive interrupts */ |
| 1427 | custom.intena = IF_RBF; |
| 1428 | mb(); |
| 1429 | /* clear any pending receive interrupt */ |
| 1430 | custom.intreq = IF_RBF; |
| 1431 | mb(); |
| 1432 | |
| 1433 | /* |
| 1434 | * Before we drop DTR, make sure the UART transmitter |
| 1435 | * has completely drained; this is especially |
| 1436 | * important if there is a transmit FIFO! |
| 1437 | */ |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1438 | rs_wait_until_sent(tty, state->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | } |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1440 | shutdown(tty, state); |
Alan Cox | 978e595 | 2008-04-30 00:53:59 -0700 | [diff] [blame] | 1441 | rs_flush_buffer(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | |
| 1443 | tty_ldisc_flush(tty); |
| 1444 | tty->closing = 0; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1445 | state->tport.tty = NULL; |
| 1446 | if (state->tport.blocked_open) { |
Jiri Slaby | 799be6f | 2012-03-05 14:52:25 +0100 | [diff] [blame] | 1447 | if (state->tport.close_delay) { |
| 1448 | msleep_interruptible(jiffies_to_msecs(state->tport.close_delay)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | } |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1450 | wake_up_interruptible(&state->tport.open_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | } |
Jiri Slaby | fef2107 | 2012-03-05 14:52:14 +0100 | [diff] [blame] | 1452 | state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING); |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1453 | wake_up_interruptible(&state->tport.close_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | local_irq_restore(flags); |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * rs_wait_until_sent() --- wait until the transmitter is empty |
| 1459 | */ |
| 1460 | static void rs_wait_until_sent(struct tty_struct *tty, int timeout) |
| 1461 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1462 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | unsigned long orig_jiffies, char_time; |
| 1464 | int lsr; |
| 1465 | |
| 1466 | if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent")) |
| 1467 | return; |
| 1468 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1469 | if (info->xmit_fifo_size == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | return; /* Just in case.... */ |
| 1471 | |
| 1472 | orig_jiffies = jiffies; |
Alan Cox | 978e595 | 2008-04-30 00:53:59 -0700 | [diff] [blame] | 1473 | |
Arnd Bergmann | 2036521 | 2010-06-01 22:53:07 +0200 | [diff] [blame] | 1474 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | * Set the check interval to be 1/5 of the estimated time to |
| 1476 | * send a single character, and make it at least 1. The check |
| 1477 | * interval should also be less than the timeout. |
| 1478 | * |
| 1479 | * Note: we have to use pretty tight timings here to satisfy |
| 1480 | * the NIST-PCTS. |
| 1481 | */ |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1482 | char_time = (info->timeout - HZ/50) / info->xmit_fifo_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | char_time = char_time / 5; |
| 1484 | if (char_time == 0) |
| 1485 | char_time = 1; |
| 1486 | if (timeout) |
| 1487 | char_time = min_t(unsigned long, char_time, timeout); |
| 1488 | /* |
| 1489 | * If the transmitter hasn't cleared in twice the approximate |
| 1490 | * amount of time to send the entire FIFO, it probably won't |
| 1491 | * ever clear. This assumes the UART isn't doing flow |
| 1492 | * control, which is currently the case. Hence, if it ever |
| 1493 | * takes longer than info->timeout, this is probably due to a |
| 1494 | * UART bug of some kind. So, we clamp the timeout parameter at |
| 1495 | * 2*info->timeout. |
| 1496 | */ |
| 1497 | if (!timeout || timeout > 2*info->timeout) |
| 1498 | timeout = 2*info->timeout; |
| 1499 | #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT |
| 1500 | printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time); |
| 1501 | printk("jiff=%lu...", jiffies); |
| 1502 | #endif |
| 1503 | while(!((lsr = custom.serdatr) & SDR_TSRE)) { |
| 1504 | #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT |
| 1505 | printk("serdatr = %d (jiff=%lu)...", lsr, jiffies); |
| 1506 | #endif |
| 1507 | msleep_interruptible(jiffies_to_msecs(char_time)); |
| 1508 | if (signal_pending(current)) |
| 1509 | break; |
| 1510 | if (timeout && time_after(jiffies, orig_jiffies + timeout)) |
| 1511 | break; |
| 1512 | } |
Milind Arun Choudhary | cc0a8fb | 2007-05-08 00:30:52 -0700 | [diff] [blame] | 1513 | __set_current_state(TASK_RUNNING); |
Jiri Slaby | eff4b0b | 2011-07-14 14:35:13 +0200 | [diff] [blame] | 1514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT |
| 1516 | printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies); |
| 1517 | #endif |
| 1518 | } |
| 1519 | |
| 1520 | /* |
| 1521 | * rs_hangup() --- called by tty_hangup() when a hangup is signaled. |
| 1522 | */ |
| 1523 | static void rs_hangup(struct tty_struct *tty) |
| 1524 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1525 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | |
| 1527 | if (serial_paranoia_check(info, tty->name, "rs_hangup")) |
| 1528 | return; |
| 1529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | rs_flush_buffer(tty); |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1531 | shutdown(tty, info); |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1532 | info->tport.count = 0; |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1533 | info->flags &= ~ASYNC_NORMAL_ACTIVE; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1534 | info->tport.tty = NULL; |
| 1535 | wake_up_interruptible(&info->tport.open_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | * ------------------------------------------------------------ |
| 1540 | * rs_open() and friends |
| 1541 | * ------------------------------------------------------------ |
| 1542 | */ |
| 1543 | static int block_til_ready(struct tty_struct *tty, struct file * filp, |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1544 | struct serial_state *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | { |
| 1546 | #ifdef DECLARE_WAITQUEUE |
| 1547 | DECLARE_WAITQUEUE(wait, current); |
| 1548 | #else |
| 1549 | struct wait_queue wait = { current, NULL }; |
| 1550 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | int retval; |
| 1552 | int do_clocal = 0, extra_count = 0; |
| 1553 | unsigned long flags; |
| 1554 | |
| 1555 | /* |
| 1556 | * If the device is in the middle of being closed, then block |
| 1557 | * until it's done, and then try again. |
| 1558 | */ |
| 1559 | if (tty_hung_up_p(filp) || |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1560 | (info->flags & ASYNC_CLOSING)) { |
| 1561 | if (info->flags & ASYNC_CLOSING) |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1562 | interruptible_sleep_on(&info->tport.close_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | #ifdef SERIAL_DO_RESTART |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1564 | return ((info->flags & ASYNC_HUP_NOTIFY) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | -EAGAIN : -ERESTARTSYS); |
| 1566 | #else |
| 1567 | return -EAGAIN; |
| 1568 | #endif |
| 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | * If non-blocking mode is set, or the port is not enabled, |
| 1573 | * then make the check up front and then exit. |
| 1574 | */ |
| 1575 | if ((filp->f_flags & O_NONBLOCK) || |
| 1576 | (tty->flags & (1 << TTY_IO_ERROR))) { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1577 | info->flags |= ASYNC_NORMAL_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | return 0; |
| 1579 | } |
| 1580 | |
| 1581 | if (tty->termios->c_cflag & CLOCAL) |
| 1582 | do_clocal = 1; |
| 1583 | |
| 1584 | /* |
| 1585 | * Block waiting for the carrier detect and the line to become |
| 1586 | * free (i.e., not in use by the callout). While we are in |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1587 | * this loop, info->tport.count is dropped by one, so that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | * rs_close() knows when to free things. We restore it upon |
| 1589 | * exit, either normal or abnormal. |
| 1590 | */ |
| 1591 | retval = 0; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1592 | add_wait_queue(&info->tport.open_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | #ifdef SERIAL_DEBUG_OPEN |
| 1594 | printk("block_til_ready before block: ttys%d, count = %d\n", |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1595 | info->line, info->tport.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | #endif |
| 1597 | local_irq_save(flags); |
| 1598 | if (!tty_hung_up_p(filp)) { |
| 1599 | extra_count = 1; |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1600 | info->tport.count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | } |
| 1602 | local_irq_restore(flags); |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1603 | info->tport.blocked_open++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | while (1) { |
| 1605 | local_irq_save(flags); |
| 1606 | if (tty->termios->c_cflag & CBAUD) |
| 1607 | rtsdtr_ctrl(SER_DTR|SER_RTS); |
| 1608 | local_irq_restore(flags); |
| 1609 | set_current_state(TASK_INTERRUPTIBLE); |
| 1610 | if (tty_hung_up_p(filp) || |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1611 | !(info->flags & ASYNC_INITIALIZED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | #ifdef SERIAL_DO_RESTART |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1613 | if (info->flags & ASYNC_HUP_NOTIFY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | retval = -EAGAIN; |
| 1615 | else |
| 1616 | retval = -ERESTARTSYS; |
| 1617 | #else |
| 1618 | retval = -EAGAIN; |
| 1619 | #endif |
| 1620 | break; |
| 1621 | } |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1622 | if (!(info->flags & ASYNC_CLOSING) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | (do_clocal || (!(ciab.pra & SER_DCD)) )) |
| 1624 | break; |
| 1625 | if (signal_pending(current)) { |
| 1626 | retval = -ERESTARTSYS; |
| 1627 | break; |
| 1628 | } |
| 1629 | #ifdef SERIAL_DEBUG_OPEN |
| 1630 | printk("block_til_ready blocking: ttys%d, count = %d\n", |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1631 | info->line, info->tport.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | #endif |
Arnd Bergmann | e142a31 | 2010-06-01 22:53:10 +0200 | [diff] [blame] | 1633 | tty_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | schedule(); |
Arnd Bergmann | e142a31 | 2010-06-01 22:53:10 +0200 | [diff] [blame] | 1635 | tty_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | } |
Milind Arun Choudhary | cc0a8fb | 2007-05-08 00:30:52 -0700 | [diff] [blame] | 1637 | __set_current_state(TASK_RUNNING); |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1638 | remove_wait_queue(&info->tport.open_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | if (extra_count) |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1640 | info->tport.count++; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1641 | info->tport.blocked_open--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | #ifdef SERIAL_DEBUG_OPEN |
| 1643 | printk("block_til_ready after blocking: ttys%d, count = %d\n", |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1644 | info->line, info->tport.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | #endif |
| 1646 | if (retval) |
| 1647 | return retval; |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1648 | info->flags |= ASYNC_NORMAL_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | return 0; |
| 1650 | } |
| 1651 | |
| 1652 | /* |
| 1653 | * This routine is called whenever a serial port is opened. It |
| 1654 | * enables interrupts for a serial port, linking in its async structure into |
| 1655 | * the IRQ chain. It also performs the serial-specific |
| 1656 | * initialization for the tty structure. |
| 1657 | */ |
| 1658 | static int rs_open(struct tty_struct *tty, struct file * filp) |
| 1659 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1660 | struct serial_state *info = rs_table + tty->index; |
Jiri Slaby | 410235f | 2012-03-05 14:52:01 +0100 | [diff] [blame] | 1661 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame^] | 1663 | info->tport.count++; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1664 | info->tport.tty = tty; |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1665 | tty->driver_data = info; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1666 | tty->port = &info->tport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | if (serial_paranoia_check(info, tty->name, "rs_open")) |
| 1668 | return -ENODEV; |
| 1669 | |
| 1670 | #ifdef SERIAL_DEBUG_OPEN |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1671 | printk("rs_open %s, count = %d\n", tty->name, info->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | #endif |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1673 | tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | /* |
| 1676 | * If the port is the middle of closing, bail out now |
| 1677 | */ |
| 1678 | if (tty_hung_up_p(filp) || |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1679 | (info->flags & ASYNC_CLOSING)) { |
| 1680 | if (info->flags & ASYNC_CLOSING) |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1681 | interruptible_sleep_on(&info->tport.close_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | #ifdef SERIAL_DO_RESTART |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1683 | return ((info->flags & ASYNC_HUP_NOTIFY) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | -EAGAIN : -ERESTARTSYS); |
| 1685 | #else |
| 1686 | return -EAGAIN; |
| 1687 | #endif |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * Start up serial port |
| 1692 | */ |
Jiri Slaby | 588993d | 2012-03-05 14:52:22 +0100 | [diff] [blame] | 1693 | retval = startup(tty, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | if (retval) { |
| 1695 | return retval; |
| 1696 | } |
| 1697 | |
| 1698 | retval = block_til_ready(tty, filp, info); |
| 1699 | if (retval) { |
| 1700 | #ifdef SERIAL_DEBUG_OPEN |
| 1701 | printk("rs_open returning after block_til_ready with %d\n", |
| 1702 | retval); |
| 1703 | #endif |
| 1704 | return retval; |
| 1705 | } |
| 1706 | |
| 1707 | #ifdef SERIAL_DEBUG_OPEN |
| 1708 | printk("rs_open %s successful...", tty->name); |
| 1709 | #endif |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
| 1713 | /* |
| 1714 | * /proc fs routines.... |
| 1715 | */ |
| 1716 | |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1717 | static inline void line_info(struct seq_file *m, struct serial_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | char stat_buf[30], control, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | unsigned long flags; |
| 1721 | |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1722 | seq_printf(m, "%d: uart:amiga_builtin",state->line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | local_irq_save(flags); |
| 1725 | status = ciab.pra; |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1726 | control = (state->flags & ASYNC_INITIALIZED) ? state->MCR : status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | local_irq_restore(flags); |
| 1728 | |
| 1729 | stat_buf[0] = 0; |
| 1730 | stat_buf[1] = 0; |
| 1731 | if(!(control & SER_RTS)) |
| 1732 | strcat(stat_buf, "|RTS"); |
| 1733 | if(!(status & SER_CTS)) |
| 1734 | strcat(stat_buf, "|CTS"); |
| 1735 | if(!(control & SER_DTR)) |
| 1736 | strcat(stat_buf, "|DTR"); |
| 1737 | if(!(status & SER_DSR)) |
| 1738 | strcat(stat_buf, "|DSR"); |
| 1739 | if(!(status & SER_DCD)) |
| 1740 | strcat(stat_buf, "|CD"); |
| 1741 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1742 | if (state->quot) |
| 1743 | seq_printf(m, " baud:%d", state->baud_base / state->quot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1745 | seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | |
| 1747 | if (state->icount.frame) |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1748 | seq_printf(m, " fe:%d", state->icount.frame); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | |
| 1750 | if (state->icount.parity) |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1751 | seq_printf(m, " pe:%d", state->icount.parity); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
| 1753 | if (state->icount.brk) |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1754 | seq_printf(m, " brk:%d", state->icount.brk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | |
| 1756 | if (state->icount.overrun) |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1757 | seq_printf(m, " oe:%d", state->icount.overrun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | |
| 1759 | /* |
| 1760 | * Last thing is the RS-232 status lines |
| 1761 | */ |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1762 | seq_printf(m, " %s\n", stat_buf+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1765 | static int rs_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | { |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1767 | seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version); |
| 1768 | line_info(m, &rs_table[0]); |
| 1769 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1772 | static int rs_proc_open(struct inode *inode, struct file *file) |
| 1773 | { |
| 1774 | return single_open(file, rs_proc_show, NULL); |
| 1775 | } |
| 1776 | |
| 1777 | static const struct file_operations rs_proc_fops = { |
| 1778 | .owner = THIS_MODULE, |
| 1779 | .open = rs_proc_open, |
| 1780 | .read = seq_read, |
| 1781 | .llseek = seq_lseek, |
| 1782 | .release = single_release, |
| 1783 | }; |
| 1784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | /* |
| 1786 | * --------------------------------------------------------------------- |
| 1787 | * rs_init() and friends |
| 1788 | * |
| 1789 | * rs_init() is called at boot-time to initialize the serial driver. |
| 1790 | * --------------------------------------------------------------------- |
| 1791 | */ |
| 1792 | |
| 1793 | /* |
| 1794 | * This routine prints out the appropriate serial driver version |
| 1795 | * number, and identifies which options were configured into this |
| 1796 | * driver. |
| 1797 | */ |
Adrian Bunk | 41c28ff | 2006-03-23 03:00:56 -0800 | [diff] [blame] | 1798 | static void show_serial_version(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | { |
| 1800 | printk(KERN_INFO "%s version %s\n", serial_name, serial_version); |
| 1801 | } |
| 1802 | |
| 1803 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 1804 | static const struct tty_operations serial_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | .open = rs_open, |
| 1806 | .close = rs_close, |
| 1807 | .write = rs_write, |
| 1808 | .put_char = rs_put_char, |
| 1809 | .flush_chars = rs_flush_chars, |
| 1810 | .write_room = rs_write_room, |
| 1811 | .chars_in_buffer = rs_chars_in_buffer, |
| 1812 | .flush_buffer = rs_flush_buffer, |
| 1813 | .ioctl = rs_ioctl, |
| 1814 | .throttle = rs_throttle, |
| 1815 | .unthrottle = rs_unthrottle, |
| 1816 | .set_termios = rs_set_termios, |
| 1817 | .stop = rs_stop, |
| 1818 | .start = rs_start, |
| 1819 | .hangup = rs_hangup, |
| 1820 | .break_ctl = rs_break, |
| 1821 | .send_xchar = rs_send_xchar, |
| 1822 | .wait_until_sent = rs_wait_until_sent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | .tiocmget = rs_tiocmget, |
| 1824 | .tiocmset = rs_tiocmset, |
Alan Cox | 0587102 | 2010-09-16 18:21:52 +0100 | [diff] [blame] | 1825 | .get_icount = rs_get_icount, |
Alexey Dobriyan | d594027 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 1826 | .proc_fops = &rs_proc_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | }; |
| 1828 | |
| 1829 | /* |
| 1830 | * The serial driver boot-time initialization code! |
| 1831 | */ |
Geert Uytterhoeven | 826e8c8 | 2009-04-05 13:12:30 +0200 | [diff] [blame] | 1832 | static int __init amiga_serial_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | { |
| 1834 | unsigned long flags; |
| 1835 | struct serial_state * state; |
Geert Uytterhoeven | 5edc304 | 2008-12-30 14:13:41 +0100 | [diff] [blame] | 1836 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | |
Jiri Slaby | 410235f | 2012-03-05 14:52:01 +0100 | [diff] [blame] | 1838 | serial_driver = alloc_tty_driver(NR_PORTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | if (!serial_driver) |
| 1840 | return -ENOMEM; |
| 1841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | show_serial_version(); |
| 1843 | |
| 1844 | /* Initialize the tty_driver structure */ |
| 1845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | serial_driver->driver_name = "amiserial"; |
| 1847 | serial_driver->name = "ttyS"; |
| 1848 | serial_driver->major = TTY_MAJOR; |
| 1849 | serial_driver->minor_start = 64; |
| 1850 | serial_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 1851 | serial_driver->subtype = SERIAL_TYPE_NORMAL; |
| 1852 | serial_driver->init_termios = tty_std_termios; |
| 1853 | serial_driver->init_termios.c_cflag = |
| 1854 | B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 1855 | serial_driver->flags = TTY_DRIVER_REAL_RAW; |
| 1856 | tty_set_operations(serial_driver, &serial_ops); |
| 1857 | |
Geert Uytterhoeven | 5edc304 | 2008-12-30 14:13:41 +0100 | [diff] [blame] | 1858 | error = tty_register_driver(serial_driver); |
| 1859 | if (error) |
Geert Uytterhoeven | 826e8c8 | 2009-04-05 13:12:30 +0200 | [diff] [blame] | 1860 | goto fail_put_tty_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | |
| 1862 | state = rs_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | state->port = (int)&custom.serdatr; /* Just to give it a value */ |
| 1864 | state->line = 0; |
| 1865 | state->custom_divisor = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | state->icount.cts = state->icount.dsr = |
| 1867 | state->icount.rng = state->icount.dcd = 0; |
| 1868 | state->icount.rx = state->icount.tx = 0; |
| 1869 | state->icount.frame = state->icount.parity = 0; |
| 1870 | state->icount.overrun = state->icount.brk = 0; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 1871 | tty_port_init(&state->tport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | |
| 1873 | printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n", |
| 1874 | state->line); |
| 1875 | |
| 1876 | /* Hardware set up */ |
| 1877 | |
| 1878 | state->baud_base = amiga_colorclock; |
| 1879 | state->xmit_fifo_size = 1; |
| 1880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | /* set ISRs, and then disable the rx interrupts */ |
Geert Uytterhoeven | 5edc304 | 2008-12-30 14:13:41 +0100 | [diff] [blame] | 1882 | error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state); |
| 1883 | if (error) |
| 1884 | goto fail_unregister; |
| 1885 | |
Yong Zhang | 9cfb5c0 | 2011-09-22 16:59:15 +0800 | [diff] [blame] | 1886 | error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0, |
Geert Uytterhoeven | 5edc304 | 2008-12-30 14:13:41 +0100 | [diff] [blame] | 1887 | "serial RX", state); |
| 1888 | if (error) |
| 1889 | goto fail_free_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | |
Julia Lawall | c70c036 | 2010-04-06 14:34:46 -0700 | [diff] [blame] | 1891 | local_irq_save(flags); |
| 1892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | /* turn off Rx and Tx interrupts */ |
| 1894 | custom.intena = IF_RBF | IF_TBE; |
| 1895 | mb(); |
| 1896 | |
| 1897 | /* clear any pending interrupt */ |
| 1898 | custom.intreq = IF_RBF | IF_TBE; |
| 1899 | mb(); |
| 1900 | |
| 1901 | local_irq_restore(flags); |
| 1902 | |
| 1903 | /* |
| 1904 | * set the appropriate directions for the modem control flags, |
| 1905 | * and clear RTS and DTR |
| 1906 | */ |
| 1907 | ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */ |
| 1908 | ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */ |
| 1909 | |
Geert Uytterhoeven | 826e8c8 | 2009-04-05 13:12:30 +0200 | [diff] [blame] | 1910 | platform_set_drvdata(pdev, state); |
| 1911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | return 0; |
Geert Uytterhoeven | 5edc304 | 2008-12-30 14:13:41 +0100 | [diff] [blame] | 1913 | |
| 1914 | fail_free_irq: |
| 1915 | free_irq(IRQ_AMIGA_TBE, state); |
| 1916 | fail_unregister: |
| 1917 | tty_unregister_driver(serial_driver); |
Geert Uytterhoeven | 5edc304 | 2008-12-30 14:13:41 +0100 | [diff] [blame] | 1918 | fail_put_tty_driver: |
| 1919 | put_tty_driver(serial_driver); |
| 1920 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
Geert Uytterhoeven | 826e8c8 | 2009-04-05 13:12:30 +0200 | [diff] [blame] | 1923 | static int __exit amiga_serial_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | { |
| 1925 | int error; |
Geert Uytterhoeven | 826e8c8 | 2009-04-05 13:12:30 +0200 | [diff] [blame] | 1926 | struct serial_state *state = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | |
| 1928 | /* printk("Unloading %s: version %s\n", serial_name, serial_version); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | if ((error = tty_unregister_driver(serial_driver))) |
| 1930 | printk("SERIAL: failed to unregister serial driver (%d)\n", |
| 1931 | error); |
| 1932 | put_tty_driver(serial_driver); |
| 1933 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 1934 | free_irq(IRQ_AMIGA_TBE, state); |
| 1935 | free_irq(IRQ_AMIGA_RBF, state); |
Geert Uytterhoeven | 5edc304 | 2008-12-30 14:13:41 +0100 | [diff] [blame] | 1936 | |
Geert Uytterhoeven | 826e8c8 | 2009-04-05 13:12:30 +0200 | [diff] [blame] | 1937 | platform_set_drvdata(pdev, NULL); |
| 1938 | |
| 1939 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
Geert Uytterhoeven | 826e8c8 | 2009-04-05 13:12:30 +0200 | [diff] [blame] | 1942 | static struct platform_driver amiga_serial_driver = { |
| 1943 | .remove = __exit_p(amiga_serial_remove), |
| 1944 | .driver = { |
| 1945 | .name = "amiga-serial", |
| 1946 | .owner = THIS_MODULE, |
| 1947 | }, |
| 1948 | }; |
| 1949 | |
| 1950 | static int __init amiga_serial_init(void) |
| 1951 | { |
| 1952 | return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe); |
| 1953 | } |
| 1954 | |
| 1955 | module_init(amiga_serial_init); |
| 1956 | |
| 1957 | static void __exit amiga_serial_exit(void) |
| 1958 | { |
| 1959 | platform_driver_unregister(&amiga_serial_driver); |
| 1960 | } |
| 1961 | |
| 1962 | module_exit(amiga_serial_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | |
| 1964 | |
Geert Uytterhoeven | d1a35e4 | 2008-10-26 19:51:14 +0100 | [diff] [blame] | 1965 | #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE) |
| 1966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | /* |
| 1968 | * ------------------------------------------------------------ |
| 1969 | * Serial console driver |
| 1970 | * ------------------------------------------------------------ |
| 1971 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | |
| 1973 | static void amiga_serial_putc(char c) |
| 1974 | { |
| 1975 | custom.serdat = (unsigned char)c | 0x100; |
| 1976 | while (!(custom.serdatr & 0x2000)) |
| 1977 | barrier(); |
| 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | * Print a string to the serial port trying not to disturb |
| 1982 | * any possible real use of the port... |
| 1983 | * |
| 1984 | * The console must be locked when we get here. |
| 1985 | */ |
| 1986 | static void serial_console_write(struct console *co, const char *s, |
| 1987 | unsigned count) |
| 1988 | { |
| 1989 | unsigned short intena = custom.intenar; |
| 1990 | |
| 1991 | custom.intena = IF_TBE; |
| 1992 | |
| 1993 | while (count--) { |
| 1994 | if (*s == '\n') |
| 1995 | amiga_serial_putc('\r'); |
| 1996 | amiga_serial_putc(*s++); |
| 1997 | } |
| 1998 | |
| 1999 | custom.intena = IF_SETCLR | (intena & IF_TBE); |
| 2000 | } |
| 2001 | |
| 2002 | static struct tty_driver *serial_console_device(struct console *c, int *index) |
| 2003 | { |
| 2004 | *index = 0; |
| 2005 | return serial_driver; |
| 2006 | } |
| 2007 | |
| 2008 | static struct console sercons = { |
| 2009 | .name = "ttyS", |
| 2010 | .write = serial_console_write, |
| 2011 | .device = serial_console_device, |
| 2012 | .flags = CON_PRINTBUFFER, |
| 2013 | .index = -1, |
| 2014 | }; |
| 2015 | |
| 2016 | /* |
| 2017 | * Register console. |
| 2018 | */ |
| 2019 | static int __init amiserial_console_init(void) |
| 2020 | { |
| 2021 | register_console(&sercons); |
| 2022 | return 0; |
| 2023 | } |
| 2024 | console_initcall(amiserial_console_init); |
Geert Uytterhoeven | d1a35e4 | 2008-10-26 19:51:14 +0100 | [diff] [blame] | 2025 | |
| 2026 | #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | |
| 2028 | MODULE_LICENSE("GPL"); |
Geert Uytterhoeven | 826e8c8 | 2009-04-05 13:12:30 +0200 | [diff] [blame] | 2029 | MODULE_ALIAS("platform:amiga-serial"); |