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