OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [char/] [serial.c] - Blame information for rev 862

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *  linux/drivers/char/serial.c
3
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
5
 *
6
 *  Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92.  Now
7
 *  much more extensible to support other serial cards based on the
8
 *  16450/16550A UART's.  Added support for the AST FourPort and the
9
 *  Accent Async board.
10
 *
11
 *  set_serial_info fixed to set the flags, custom divisor, and uart
12
 *      type fields.  Fix suggested by Michael K. Johnson 12/12/92.
13
 *
14
 *  11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
15
 *
16
 *  03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
17
 *
18
 *  rs_set_termios fixed to look also for changes of the input
19
 *      flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
20
 *                                            Bernd Anhäupl 05/17/96.
21
 *
22
 * Added Support for PCI serial boards which contain 16x50 Chips
23
 * 31.10.1998 Henning P. Schmiedehausen <hps@tanstaafl.de>
24
 *
25
 * This module exports the following rs232 io functions:
26
 *
27
 *      int rs_init(void);
28
 *      int rs_open(struct tty_struct * tty, struct file * filp)
29
 */
30
 
31
#include <linux/module.h>
32
#include <linux/errno.h>
33
#include <linux/signal.h>
34
#include <linux/sched.h>
35
#include <linux/timer.h>
36
#include <linux/interrupt.h>
37
#include <linux/tty.h>
38
#include <linux/tty_flip.h>
39
#include <linux/serial.h>
40
#include <linux/serial_reg.h>
41
#include <linux/config.h>
42
#include <linux/major.h>
43
#include <linux/string.h>
44
#include <linux/fcntl.h>
45
#include <linux/ptrace.h>
46
#include <linux/ioport.h>
47
#include <linux/mm.h>
48
 
49
#ifdef CONFIG_SERIAL_PCI
50
#include <linux/pci.h>
51
#include <linux/bios32.h>
52
#endif
53
 
54
#include <asm/system.h>
55
 
56
#if defined(CONFIG_eLIA) && defined(CONFIG_PCI)
57
#include <asm/mcfpci.h>
58
#else
59
#include <asm/io.h>
60
#endif
61
 
62
#ifdef CONFIG_OR32
63
#include <asm/irq.h>
64
#endif
65
 
66
#include <asm/segment.h>
67
#include <asm/bitops.h>
68
 
69
static char *serial_name = "Serial driver";
70
static char *serial_version = "4.13p1";
71
 
72
DECLARE_TASK_QUEUE(tq_serial);
73
 
74
struct tty_driver serial_driver, callout_driver;
75
static int serial_refcount;
76
 
77
/* serial subtype definitions */
78
#define SERIAL_TYPE_NORMAL      1
79
#define SERIAL_TYPE_CALLOUT     2
80
 
81
/* number of characters left in xmit buffer before we ask for more */
82
#define WAKEUP_CHARS 256
83
 
84
#ifdef CONFIG_COLDFIRE
85
#undef  TTY_MAJOR
86
#define TTY_MAJOR       24
87
#undef  TTYAUX_MAJOR
88
#define TTYAUX_MAJOR    25
89
#endif
90
 
91
/*
92
 * Serial driver configuration section.  Here are the various options:
93
 *
94
 * CONFIG_HUB6
95
 *              Enables support for the venerable Bell Technologies
96
 *              HUB6 card.
97
 *
98
 * SERIAL_PARANOIA_CHECK
99
 *              Check the magic number for the async_structure where
100
 *              ever possible.
101
 */
102
 
103
#define SERIAL_PARANOIA_CHECK
104
#define CONFIG_SERIAL_NOPAUSE_IO
105
#define SERIAL_DO_RESTART
106
 
107
#undef SERIAL_DEBUG_INTR
108
#undef SERIAL_DEBUG_OPEN
109
#undef SERIAL_DEBUG_FLOW
110
 
111 582 simons
#if 0
112 199 simons
#define SERIAL_DEBUG_INTR       1
113
#endif
114
 
115
#ifdef CONFIG_SERIAL_PCI
116
# undef SERIAL_DEBUG_PCI
117
#endif
118
 
119
#define RS_STROBE_TIME (10*HZ)
120
#define RS_ISR_PASS_LIMIT 256
121
 
122
#define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
123
 
124
#define _INLINE_ inline
125
 
126
#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
127
#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
128
 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
129
#else
130
#define DBG_CNT(s)
131
#endif
132
 
133
/*
134
 * IRQ_timeout          - How long the timeout should be for each IRQ
135
 *                              should be after the IRQ has been active.
136
 */
137 862 simons
#ifdef CONFIG_OR32
138
static struct async_struct *IRQ_ports[32];
139
static struct rs_multiport_struct rs_multiport[32];
140
static int IRQ_timeout[32];
141
#else
142 199 simons
static struct async_struct *IRQ_ports[16];
143
static struct rs_multiport_struct rs_multiport[16];
144
static int IRQ_timeout[16];
145 862 simons
#endif
146 199 simons
static volatile int rs_irq_triggered;
147
static volatile int rs_triggered;
148
static int rs_wild_int_mask;
149
 
150
#ifdef CONFIG_COLDFIRE
151
#define IRQMASK(i)      ((i) & 0x0f)
152
#endif
153
 
154
#ifdef CONFIG_OR32
155
#define IRQMASK(i)      ((i) & 0x1f)
156
#endif
157
 
158
static void autoconfig(struct async_struct * info);
159
static void change_speed(struct async_struct *info);
160
 
161
/*
162
 * This assumes you have a 1.8432 MHz clock for your UART.
163
 *
164
 * It'd be nice if someone built a serial card with a 24.576 MHz
165
 * clock, since the 16550A is capable of handling a top speed of 1.5
166
 * megabits/second; but this requires the faster clock.
167
 */
168
#define BASE_BAUD ( 1843200 / 16 )
169
 
170 582 simons
/* Base boud for or1k */
171 764 simons
#define BASE_BAUD_OR1K ( SYS_CLK / 16 )
172 582 simons
 
173 199 simons
/*
174
 * Well, it is not a 24,756 MHz clock but it is at least a start.
175
 * This PCI board here has a 14,7456 MHz crystal oscillator which is
176
 * eight times as fast as the standard serial clock...
177
 */
178
 
179
#define PCI_BAUD  ( 14745600 / 16 )
180
 
181
/* Standard COM flags (except for COM4, because of the 8514 problem) */
182
#define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST )
183
#define STD_COM4_FLAGS ASYNC_BOOT_AUTOCONF
184
 
185
#define FOURPORT_FLAGS ASYNC_FOURPORT
186
#define ACCENT_FLAGS 0
187
#define BOCA_FLAGS 0
188
#define HUB6_FLAGS 0
189
 
190
#ifdef CONFIG_SERIAL_PCI
191
 
192
#define PCI_FLAGS (ASYNC_PCI|ASYNC_BOOT_AUTOCONF)
193
 
194
#ifndef PCI_DEVICE_ID_PLX_SPCOM200
195
#define PCI_DEVICE_ID_PLX_SPCOM200 0x1103
196
#endif
197
 
198
/*
199
 * The chips we know about
200
 */
201
 
202
#define PCISER_PLX9050  0    /* PLX 9050 local bus bridge as serial card */
203
#define PCISER_PCCOM4   1    /* "PC COM PCI Bus 4 port serial Adapter" -- from Alvin Sim <alvin@alloycp.com.au> */
204
 
205
struct pci_serial_boards pci_serial_tbl[] = {
206
  { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_SPCOM200, "SPCom 200", PCISER_PLX9050, pci_space_0|pci_space_1, 1, 0, 128, PCI_BAUD },
207
  { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM4,   "PC COM 4",  PCISER_PCCOM4,  pci_space_0,             4, 8, 128, BASE_BAUD },
208
  {     0, 0, 0, 0, 0, 0, 0, 0, 0 }
209
};
210
 
211
#endif
212
 
213
/*
214
 * The following define the access methods for the HUB6 card. All
215
 * access is through two ports for all 24 possible chips. The card is
216
 * selected through the high 2 bits, the port on that card with the
217
 * "middle" 3 bits, and the register on that port with the bottom
218
 * 3 bits.
219
 *
220
 * While the access port and interrupt is configurable, the default
221
 * port locations are 0x302 for the port control register, and 0x303
222
 * for the data read/write register. Normally, the interrupt is at irq3
223
 * but can be anything from 3 to 7 inclusive. Note that using 3 will
224
 * require disabling com2.
225
 */
226
 
227
#define C_P(card,port) (((card)<<6|(port)<<3) + 1)
228
 
229
struct async_struct rs_table[] = {
230
        /* UART CLK   PORT IRQ     FLAGS        */
231
#if defined(CONFIG_NETtel) && defined(CONFIG_M5206e)
232
        { 0, BASE_BAUD, 0x40000000, 28, STD_COM_FLAGS },/* ttyS0 */
233
#elif defined(CONFIG_eLIA) && defined(CONFIG_PCI)
234
        { 0, BASE_BAUD, 0x200, 25, STD_COM_FLAGS },      /* ttyS0 */
235
        { 0, BASE_BAUD, 0x208, 25, STD_COM_FLAGS },      /* ttyS1 */
236
        { 0, BASE_BAUD, 0x210, 25, STD_COM_FLAGS },      /* ttyS2 */
237
        { 0, BASE_BAUD, 0x218, 25, STD_COM_FLAGS },      /* ttyS3 */
238
#elif defined(CONFIG_OR32)
239 582 simons
        { 0, BASE_BAUD_OR1K, UART_BASE_ADD, IRQ_UART_0, STD_COM_FLAGS }, /* ttyS0 */
240 199 simons
#else
241
        { 0, BASE_BAUD, 0x3F8, 4, STD_COM_FLAGS },       /* ttyS0 */
242
        { 0, BASE_BAUD, 0x2F8, 3, STD_COM_FLAGS },       /* ttyS1 */
243
        { 0, BASE_BAUD, 0x3E8, 4, STD_COM_FLAGS },       /* ttyS2 */
244
        { 0, BASE_BAUD, 0x2E8, 3, STD_COM4_FLAGS },      /* ttyS3 */
245
 
246
        { 0, BASE_BAUD, 0x1A0, 9, FOURPORT_FLAGS },      /* ttyS4 */
247
        { 0, BASE_BAUD, 0x1A8, 9, FOURPORT_FLAGS },      /* ttyS5 */
248
        { 0, BASE_BAUD, 0x1B0, 9, FOURPORT_FLAGS },      /* ttyS6 */
249
        { 0, BASE_BAUD, 0x1B8, 9, FOURPORT_FLAGS },      /* ttyS7 */
250
 
251
        { 0, BASE_BAUD, 0x2A0, 5, FOURPORT_FLAGS },      /* ttyS8 */
252
        { 0, BASE_BAUD, 0x2A8, 5, FOURPORT_FLAGS },      /* ttyS9 */
253
        { 0, BASE_BAUD, 0x2B0, 5, FOURPORT_FLAGS },      /* ttyS10 */
254
        { 0, BASE_BAUD, 0x2B8, 5, FOURPORT_FLAGS },      /* ttyS11 */
255
 
256
        { 0, BASE_BAUD, 0x330, 4, ACCENT_FLAGS },        /* ttyS12 */
257
        { 0, BASE_BAUD, 0x338, 4, ACCENT_FLAGS },        /* ttyS13 */
258
        { 0, BASE_BAUD, 0x000, 0, 0 },     /* ttyS14 (spare; user configurable) */
259
        { 0, BASE_BAUD, 0x000, 0, 0 },     /* ttyS15 (spare; user configurable) */
260
 
261
        { 0, BASE_BAUD, 0x100, 12, BOCA_FLAGS }, /* ttyS16 */
262
        { 0, BASE_BAUD, 0x108, 12, BOCA_FLAGS }, /* ttyS17 */
263
        { 0, BASE_BAUD, 0x110, 12, BOCA_FLAGS }, /* ttyS18 */
264
        { 0, BASE_BAUD, 0x118, 12, BOCA_FLAGS }, /* ttyS19 */
265
        { 0, BASE_BAUD, 0x120, 12, BOCA_FLAGS }, /* ttyS20 */
266
        { 0, BASE_BAUD, 0x128, 12, BOCA_FLAGS }, /* ttyS21 */
267
        { 0, BASE_BAUD, 0x130, 12, BOCA_FLAGS }, /* ttyS22 */
268
        { 0, BASE_BAUD, 0x138, 12, BOCA_FLAGS }, /* ttyS23 */
269
        { 0, BASE_BAUD, 0x140, 12, BOCA_FLAGS }, /* ttyS24 */
270
        { 0, BASE_BAUD, 0x148, 12, BOCA_FLAGS }, /* ttyS25 */
271
        { 0, BASE_BAUD, 0x150, 12, BOCA_FLAGS }, /* ttyS26 */
272
        { 0, BASE_BAUD, 0x158, 12, BOCA_FLAGS }, /* ttyS27 */
273
        { 0, BASE_BAUD, 0x160, 12, BOCA_FLAGS }, /* ttyS28 */
274
        { 0, BASE_BAUD, 0x168, 12, BOCA_FLAGS }, /* ttyS29 */
275
        { 0, BASE_BAUD, 0x170, 12, BOCA_FLAGS }, /* ttyS30 */
276
        { 0, BASE_BAUD, 0x178, 12, BOCA_FLAGS }, /* ttyS31 */
277
 
278
/* You can have up to four HUB6's in the system, but I've only
279
 * included two cards here for a total of twelve ports.
280
 */
281
#ifdef CONFIG_HUB6
282
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,0) },  /* ttyS32 */
283
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,1) }, /* ttyS33 */
284
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,2) }, /* ttyS34 */
285
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,3) }, /* ttyS35 */
286
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,4) }, /* ttyS36 */
287
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,5) }, /* ttyS37 */
288
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,0) }, /* ttyS38 */
289
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,1) },        /* ttyS39 */
290
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,2) },        /* ttyS40 */
291
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,3) },        /* ttyS41 */
292
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,4) },        /* ttyS42 */
293
        { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,5) },        /* ttyS43 */
294
#endif
295
 
296
#ifdef CONFIG_SERIAL_PCI
297
                { 0, BASE_BAUD, 0x0, 0, 0 },       /* ttyS32 or bigger... */
298
                { 0, BASE_BAUD, 0x0, 0, 0 },       /* ttyS33 */
299
                { 0, BASE_BAUD, 0x0, 0, 0 },       /* ttyS34 */
300
                { 0, BASE_BAUD, 0x0, 0, 0 },       /* ttyS35 */
301
                { 0, BASE_BAUD, 0x0, 0, 0 },       /* ttyS36 */
302
                { 0, BASE_BAUD, 0x0, 0, 0 },       /* ttyS37 */
303
                { 0, BASE_BAUD, 0x0, 0, 0 },       /* ttyS38 */
304
                { 0, BASE_BAUD, 0x0, 0, 0 },       /* ttyS39 */
305
#endif
306
#endif /* CONFIG_COLDFIRE */
307
};
308
 
309
 
310
 
311
#define NR_PORTS        (sizeof(rs_table)/sizeof(struct async_struct))
312
 
313
#ifdef CONFIG_SERIAL_PCI
314
 
315
/*
316
 * currently you can have up to four PCI serial boards in your
317
 * system. Increase the size of this structure to have more
318
 */
319
 
320
struct pci_struct pci_rs_chips[] = {
321
  {0, 0,},
322
  {0, 0,},
323
  {0, 0,},
324
  {0, 0,},
325
};
326
 
327
#define PCI_NR_BOARDS   (sizeof(pci_rs_chips)/sizeof(struct pci_struct))
328
#define PCI_NR_PORTS    8
329
 
330
#define PCI_PORT_START (NR_PORTS - PCI_NR_PORTS)
331
 
332
#endif
333
 
334
static struct tty_struct *serial_table[NR_PORTS];
335
static struct termios *serial_termios[NR_PORTS];
336
static struct termios *serial_termios_locked[NR_PORTS];
337
 
338
#ifndef MIN
339
#define MIN(a,b)        ((a) < (b) ? (a) : (b))
340
#endif
341
 
342
#ifdef CONFIG_OR32
343 582 simons
#define REG8(x) (*(volatile unsigned char *)(x))
344
#define IN_CLK  20000000
345
 
346
#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
347
#define WAIT_FOR_XMITR \
348
 do { \
349
       lsr = REG8(UART_BASE_ADD + UART_LSR); \
350
 } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
351
 
352 199 simons
int rs_console_inited = 0;
353
int rs_console_port = 0;
354
int rs_console_baud = 9600;
355
 
356
/*
357
 * or32_console_print is registered for printk.
358
 */
359
void console_print_or32(const char *p)
360
{
361
        char c;
362
        return;
363
}
364
 
365
void rs_console_init(void)
366
{
367
 
368 582 simons
  int devisor;
369
 
370
  if(rs_console_port != 0)
371
          return;
372
 
373
   /* Reset receiver and transmiter */
374
  REG8(UART_BASE_ADD + UART_FCR) = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
375
 
376
  /* Disable all interrupts */
377
  REG8(UART_BASE_ADD + UART_IER) = 0x00;
378
 
379
  /* Set 8 bit char, 1 stop bit, no parity */
380
  REG8(UART_BASE_ADD + UART_LCR) = UART_LCR_WLEN8 & ~(UART_LCR_STOP | UART_LCR_PARITY);
381
 
382
  /* Set baud rate */
383
  devisor = BASE_BAUD_OR1K/rs_console_baud;
384
  REG8(UART_BASE_ADD + UART_LCR) |= UART_LCR_DLAB;
385
  REG8(UART_BASE_ADD + UART_DLL) = devisor & 0x000000ff;
386
  REG8(UART_BASE_ADD + UART_DLM) = (devisor >> 8) & 0x000000ff;
387
  REG8(UART_BASE_ADD + UART_LCR) &= ~(UART_LCR_DLAB);
388
 
389 199 simons
        rs_console_inited++;
390
        return;
391
}
392
 
393
/*
394
 * rs_console_print is registered for printk output.
395
 */
396
 
397
void rs_console_print(const char *p)
398
{
399 582 simons
 
400
  char c;
401
  unsigned char ier, lsr;
402
 
403
extern void putc(char);
404
#if 1
405
  ier = REG8(UART_BASE_ADD + UART_IER);
406
  REG8(UART_BASE_ADD + UART_IER) = 0x00;
407
#endif
408
 
409
  while ((c = *(p++)) != 0) {
410
#if 1
411
    WAIT_FOR_XMITR;
412
    REG8(UART_BASE_ADD + UART_TX) = c;
413
 
414
    if (c == '\n') {
415
      WAIT_FOR_XMITR;
416
      REG8(UART_BASE_ADD + UART_TX) = '\r';
417
    }
418
#else
419
    putc(c);
420
#endif
421
  }
422
 
423
#if 1
424
  WAIT_FOR_XMITR;
425
  REG8(UART_BASE_ADD + UART_IER) = ier;
426
 
427
  /* Dummy read to clear all pending interrupt */
428
  REG8(UART_BASE_ADD + UART_IIR);
429
  REG8(UART_BASE_ADD + UART_LSR);
430
#endif 
431
 
432 199 simons
        return;
433
}
434
 
435
/*
436
 *      Setup for console. Argument comes from the boot command line.
437
 */
438
int rs_console_setup(char *arg)
439
{
440
        int     rc = 0;
441
 
442
        if (!strncmp(arg, "/dev/ttyS", 9)) {
443
                rs_console_port = arg[9] - '0';
444
                arg += 10;
445
                rc = 1;
446
        } else if (!strncmp(arg, "/dev/cua", 8)) {
447
                rs_console_port = arg[8] - '0';
448
                arg += 9;
449
                rc = 1;
450
        }
451
        if (*arg == ',')
452
                rs_console_baud = simple_strtoul(arg+1,NULL,0);
453
        return(rc);
454
}
455
#endif
456
 
457
/*
458
 * tmp_buf is used as a temporary buffer by serial_write.  We need to
459
 * lock it in case the memcpy_fromfs blocks while swapping in a page,
460
 * and some other program tries to do a serial write at the same time.
461
 * Since the lock will only come under contention when the system is
462
 * swapping and available memory is low, it makes sense to share one
463
 * buffer across all the serial ports, since it significantly saves
464
 * memory if large numbers of serial ports are open.
465
 */
466
static unsigned char *tmp_buf = 0;
467
static struct semaphore tmp_buf_sem = MUTEX;
468
 
469
static inline int serial_paranoia_check(struct async_struct *info,
470
                                        kdev_t device, const char *routine)
471
{
472
#ifdef SERIAL_PARANOIA_CHECK
473
        static const char *badmagic =
474
                "Warning: bad magic number for serial struct (%s) in %s\n";
475
        static const char *badinfo =
476
                "Warning: null async_struct for (%s) in %s\n";
477
 
478
        if (!info) {
479
                printk(badinfo, kdevname(device), routine);
480
                return 1;
481
        }
482
        if (info->magic != SERIAL_MAGIC) {
483
                printk(badmagic, kdevname(device), routine);
484
                return 1;
485
        }
486
#endif
487
        return 0;
488
}
489
 
490
/*
491
 * This is used to figure out the divisor speeds and the timeouts
492
 */
493
static int baud_table[] = {
494
        0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
495
        9600, 19200, 38400, 57600, 115200, 0 };
496
 
497
static inline unsigned int serial_in(struct async_struct *info, int offset)
498
{
499
#ifdef CONFIG_HUB6
500
    if (info->hub6) {
501
        outb(info->hub6 - 1 + offset, info->port);
502
        return inb(info->port+1);
503
    } else
504
#endif
505
        return inb(info->port + offset);
506
}
507
 
508
static inline unsigned int serial_inp(struct async_struct *info, int offset)
509
{
510
#ifdef CONFIG_HUB6
511
    if (info->hub6) {
512
        outb(info->hub6 - 1 + offset, info->port);
513
        return inb_p(info->port+1);
514
    } else
515
#endif
516
#ifdef CONFIG_SERIAL_NOPAUSE_IO
517
        return inb(info->port + offset);
518
#else
519
        return inb_p(info->port + offset);
520
#endif
521
}
522
 
523
static inline void serial_out(struct async_struct *info, int offset, int value)
524
{
525
#ifdef CONFIG_HUB6
526
    if (info->hub6) {
527
        outb(info->hub6 - 1 + offset, info->port);
528
        outb(value, info->port+1);
529
    } else
530
#endif
531
        outb(value, info->port+offset);
532
}
533
 
534
static inline void serial_outp(struct async_struct *info, int offset,
535
                               int value)
536
{
537
#ifdef CONFIG_HUB6
538
    if (info->hub6) {
539
        outb(info->hub6 - 1 + offset, info->port);
540
        outb_p(value, info->port+1);
541
    } else
542
#endif
543
#ifdef CONFIG_SERIAL_NOPAUSE_IO
544
        outb(value, info->port+offset);
545
#else
546
        outb_p(value, info->port+offset);
547
#endif
548
}
549
 
550
/*
551
 * ------------------------------------------------------------
552
 * rs_stop() and rs_start()
553
 *
554
 * This routines are called before setting or resetting tty->stopped.
555
 * They enable or disable transmitter interrupts, as necessary.
556
 * ------------------------------------------------------------
557
 */
558
static void rs_stop(struct tty_struct *tty)
559
{
560
        struct async_struct *info = (struct async_struct *)tty->driver_data;
561
        unsigned long flags;
562
 
563
        if (serial_paranoia_check(info, tty->device, "rs_stop"))
564
                return;
565
 
566
        save_flags(flags); cli();
567
        if (info->IER & UART_IER_THRI) {
568
                info->IER &= ~UART_IER_THRI;
569
                serial_out(info, UART_IER, info->IER);
570
        }
571
        restore_flags(flags);
572
}
573
 
574
static void rs_start(struct tty_struct *tty)
575
{
576
        struct async_struct *info = (struct async_struct *)tty->driver_data;
577
        unsigned long flags;
578
 
579
        if (serial_paranoia_check(info, tty->device, "rs_start"))
580
                return;
581
 
582
        save_flags(flags); cli();
583
        if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
584
                info->IER |= UART_IER_THRI;
585
                serial_out(info, UART_IER, info->IER);
586
        }
587
        restore_flags(flags);
588
}
589
 
590
/*
591
 * ----------------------------------------------------------------------
592
 *
593
 * Here starts the interrupt handling routines.  All of the following
594
 * subroutines are declared as inline and are folded into
595
 * rs_interrupt().  They were separated out for readability's sake.
596
 *
597
 * Note: rs_interrupt() is a "fast" interrupt, which means that it
598
 * runs with interrupts turned off.  People who may want to modify
599
 * rs_interrupt() should try to keep the interrupt handler as fast as
600
 * possible.  After you are done making modifications, it is not a bad
601
 * idea to do:
602
 *
603
 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
604
 *
605
 * and look at the resulting assemble code in serial.s.
606
 *
607
 *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
608
 * -----------------------------------------------------------------------
609
 */
610
 
611
/*
612
 * This is the serial driver's interrupt routine while we are probing
613
 * for submarines.
614
 */
615
static void rs_probe(int irq, void *dev_id, struct pt_regs * regs)
616
{
617
        rs_irq_triggered = IRQMASK(irq);
618
        rs_triggered |= 1 << IRQMASK(irq);
619
        return;
620
}
621
 
622
/*
623
 * This routine is used by the interrupt handler to schedule
624
 * processing in the software interrupt portion of the driver.
625
 */
626
static _INLINE_ void rs_sched_event(struct async_struct *info,
627
                                  int event)
628
{
629
        info->event |= 1 << event;
630
        queue_task_irq_off(&info->tqueue, &tq_serial);
631
        mark_bh(SERIAL_BH);
632
}
633
 
634
static _INLINE_ void receive_chars(struct async_struct *info,
635
                                 int *status)
636
{
637
        struct tty_struct *tty = info->tty;
638
        unsigned char ch;
639
        int ignored = 0;
640
 
641
        do {
642
                ch = serial_inp(info, UART_RX);
643
                if (*status & info->ignore_status_mask) {
644
                        if (++ignored > 100)
645
                                break;
646
                        goto ignore_char;
647
                }
648
                if (tty->flip.count >= TTY_FLIPBUF_SIZE)
649
                        break;
650
                tty->flip.count++;
651
                if (*status & (UART_LSR_BI)) {
652
#ifdef SERIAL_DEBUG_INTR
653
                        printk("handling break....");
654
#endif
655
                        *tty->flip.flag_buf_ptr++ = TTY_BREAK;
656
                        if (info->flags & ASYNC_SAK)
657
                                do_SAK(tty);
658
                } else if (*status & UART_LSR_PE)
659
                        *tty->flip.flag_buf_ptr++ = TTY_PARITY;
660
                else if (*status & UART_LSR_FE)
661
                        *tty->flip.flag_buf_ptr++ = TTY_FRAME;
662
                else if (*status & UART_LSR_OE)
663
                        *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
664
                else
665
                        *tty->flip.flag_buf_ptr++ = 0;
666
                *tty->flip.char_buf_ptr++ = ch;
667
        ignore_char:
668
                *status = serial_inp(info, UART_LSR) & info->read_status_mask;
669
        } while (*status & UART_LSR_DR);
670
        queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
671
#ifdef SERIAL_DEBUG_INTR
672
        printk("DR...");
673
#endif
674
}
675
 
676
static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
677
{
678
        int count;
679
 
680
        if (info->x_char) {
681
                serial_outp(info, UART_TX, info->x_char);
682
                info->x_char = 0;
683
                if (intr_done)
684
                        *intr_done = 0;
685
                return;
686
        }
687
        if ((info->xmit_cnt <= 0) || info->tty->stopped ||
688
            info->tty->hw_stopped) {
689
                info->IER &= ~UART_IER_THRI;
690
                serial_out(info, UART_IER, info->IER);
691
                return;
692
        }
693
 
694
        count = info->xmit_fifo_size;
695
        do {
696
                serial_out(info, UART_TX, info->xmit_buf[info->xmit_tail++]);
697
                info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
698
                if (--info->xmit_cnt <= 0)
699
                        break;
700
        } while (--count > 0);
701
 
702
        if (info->xmit_cnt < WAKEUP_CHARS)
703
                rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
704
 
705
#ifdef SERIAL_DEBUG_INTR
706
        printk("THRE...");
707
#endif
708
        if (intr_done)
709
                *intr_done = 0;
710
 
711
        if (info->xmit_cnt <= 0) {
712
                info->IER &= ~UART_IER_THRI;
713
                serial_out(info, UART_IER, info->IER);
714
        }
715
}
716
 
717
static _INLINE_ void check_modem_status(struct async_struct *info)
718
{
719
        int     status;
720
 
721
        status = serial_in(info, UART_MSR);
722
 
723
        if (status & UART_MSR_ANY_DELTA) {
724
                /* update input line counters */
725
                if (status & UART_MSR_TERI)
726
                        info->icount.rng++;
727
                if (status & UART_MSR_DDSR)
728
                        info->icount.dsr++;
729
                if (status & UART_MSR_DDCD)
730
                        info->icount.dcd++;
731
                if (status & UART_MSR_DCTS)
732
                        info->icount.cts++;
733
                wake_up_interruptible(&info->delta_msr_wait);
734
        }
735
 
736
        if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
737
#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
738
                printk("ttys%d CD now %s...", info->line,
739
                       (status & UART_MSR_DCD) ? "on" : "off");
740
#endif          
741
                if (status & UART_MSR_DCD)
742
                        wake_up_interruptible(&info->open_wait);
743
                else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
744
                           (info->flags & ASYNC_CALLOUT_NOHUP))) {
745
#ifdef SERIAL_DEBUG_OPEN
746
                        printk("scheduling hangup...");
747
#endif
748
                        queue_task_irq_off(&info->tqueue_hangup,
749
                                           &tq_scheduler);
750
                }
751
        }
752
        if (info->flags & ASYNC_CTS_FLOW) {
753
                if (info->tty->hw_stopped) {
754
                        if (status & UART_MSR_CTS) {
755
#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
756
                                printk("CTS tx start...");
757
#endif
758
                                info->tty->hw_stopped = 0;
759
                                info->IER |= UART_IER_THRI;
760
                                serial_out(info, UART_IER, info->IER);
761
                                rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
762
                                return;
763
                        }
764
                } else {
765
                        if (!(status & UART_MSR_CTS)) {
766
#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
767
                                printk("CTS tx stop...");
768
#endif
769
                                info->tty->hw_stopped = 1;
770
                                info->IER &= ~UART_IER_THRI;
771
                                serial_out(info, UART_IER, info->IER);
772
                        }
773
                }
774
        }
775
}
776
 
777
/*
778
 * This is the serial driver's generic interrupt routine
779
 */
780
static void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
781
{
782
        int status;
783
        struct async_struct * info;
784
        int pass_counter = 0;
785
        struct async_struct *end_mark = 0;
786
        int first_multi = 0;
787
        struct rs_multiport_struct *multi;
788
 
789
#ifdef SERIAL_DEBUG_INTR
790
        printk("rs_interrupt(%d)...", irq);
791
#endif
792
 
793
        info = IRQ_ports[IRQMASK(irq)];
794
        if (!info)
795
                return;
796
 
797
        multi = &rs_multiport[IRQMASK(irq)];
798
        if (multi->port_monitor)
799
                first_multi = inb(multi->port_monitor);
800
 
801
        do {
802
                if (!info->tty ||
803
                    (serial_in(info, UART_IIR) & UART_IIR_NO_INT)) {
804
                        if (!end_mark)
805
                                end_mark = info;
806
                        goto next;
807
                }
808
                end_mark = 0;
809
 
810
                info->last_active = jiffies;
811
 
812
                status = serial_inp(info, UART_LSR) & info->read_status_mask;
813
#ifdef SERIAL_DEBUG_INTR
814
                printk("status = %x...", status);
815
#endif
816
                if (status & UART_LSR_DR)
817
                        receive_chars(info, &status);
818
                check_modem_status(info);
819
                if (status & UART_LSR_THRE)
820
                        transmit_chars(info, 0);
821
 
822
        next:
823
                info = info->next_port;
824
                if (!info) {
825
                        info = IRQ_ports[IRQMASK(irq)];
826
                        if (pass_counter++ > RS_ISR_PASS_LIMIT) {
827
#if 0
828
                                printk("rs loop break\n");
829
#endif
830
                                break;  /* Prevent infinite loops */
831
                        }
832
                        continue;
833
                }
834
        } while (end_mark != info);
835
        if (multi->port_monitor)
836
                printk("rs port monitor (normal) irq %d: 0x%x, 0x%x\n",
837
                       info->irq, first_multi, inb(multi->port_monitor));
838
#ifdef SERIAL_DEBUG_INTR
839
        printk("end.\n");
840
#endif
841
}
842
 
843
/*
844
 * This is the serial driver's interrupt routine for a single port
845
 */
846
static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
847
{
848
        int status;
849
        int pass_counter = 0;
850
        int first_multi = 0;
851
        struct async_struct * info;
852
        struct rs_multiport_struct *multi;
853
 
854
#ifdef SERIAL_DEBUG_INTR
855
        printk("rs_interrupt_single(%d)...", irq);
856
#endif
857
 
858
        info = IRQ_ports[IRQMASK(irq)];
859
        if (!info || !info->tty)
860
                return;
861
 
862
        multi = &rs_multiport[IRQMASK(irq)];
863
        if (multi->port_monitor)
864
                first_multi = inb(multi->port_monitor);
865
 
866
        do {
867
                status = serial_inp(info, UART_LSR) & info->read_status_mask;
868
#ifdef SERIAL_DEBUG_INTR
869
                printk("status = %x...", status);
870
#endif
871
                if (status & UART_LSR_DR)
872
                        receive_chars(info, &status);
873
                check_modem_status(info);
874
                if (status & UART_LSR_THRE)
875
                        transmit_chars(info, 0);
876
                if (pass_counter++ > RS_ISR_PASS_LIMIT) {
877
#if 0
878
                        printk("rs_single loop break.\n");
879
#endif
880
                        break;
881
                }
882
        } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
883
        info->last_active = jiffies;
884
        if (multi->port_monitor)
885
                printk("rs port monitor (single) irq %d: 0x%x, 0x%x\n",
886
                       info->irq, first_multi, inb(multi->port_monitor));
887
#ifdef SERIAL_DEBUG_INTR
888
        printk("end.\n");
889
#endif
890
}
891
 
892
/*
893
 * This is the serial driver's for multiport boards
894
 */
895
static void rs_interrupt_multi(int irq, void *dev_id, struct pt_regs * regs)
896
{
897
        int status;
898
        struct async_struct * info;
899
        int pass_counter = 0;
900
        int first_multi= 0;
901
        struct rs_multiport_struct *multi;
902
 
903
#ifdef SERIAL_DEBUG_INTR
904
        printk("rs_interrupt_multi(%d)...", irq);
905
#endif
906
 
907
        info = IRQ_ports[IRQMASK(irq)];
908
        if (!info)
909
                return;
910
        multi = &rs_multiport[IRQMASK(irq)];
911
        if (!multi->port1) {
912
                /* Should never happen */
913
                printk("rs_interrupt_multi: NULL port1!\n");
914
                return;
915
        }
916
        if (multi->port_monitor)
917
                first_multi = inb(multi->port_monitor);
918
 
919
        while (1) {
920
                if (!info->tty ||
921
                    (serial_in(info, UART_IIR) & UART_IIR_NO_INT))
922
                        goto next;
923
 
924
                info->last_active = jiffies;
925
 
926
                status = serial_inp(info, UART_LSR) & info->read_status_mask;
927
#ifdef SERIAL_DEBUG_INTR
928
                printk("status = %x...", status);
929
#endif
930
                if (status & UART_LSR_DR)
931
                        receive_chars(info, &status);
932
                check_modem_status(info);
933
                if (status & UART_LSR_THRE)
934
                        transmit_chars(info, 0);
935
 
936
        next:
937
                info = info->next_port;
938
                if (info)
939
                        continue;
940
 
941
                info = IRQ_ports[IRQMASK(irq)];
942
                if (pass_counter++ > RS_ISR_PASS_LIMIT) {
943
#if 1
944
                        printk("rs_multi loop break\n");
945
#endif
946
                        break;  /* Prevent infinite loops */
947
                }
948
                if (multi->port_monitor)
949
                        printk("rs port monitor irq %d: 0x%x, 0x%x\n",
950
                               info->irq, first_multi,
951
                               inb(multi->port_monitor));
952
                if ((inb(multi->port1) & multi->mask1) != multi->match1)
953
                        continue;
954
                if (!multi->port2)
955
                        break;
956
                if ((inb(multi->port2) & multi->mask2) != multi->match2)
957
                        continue;
958
                if (!multi->port3)
959
                        break;
960
                if ((inb(multi->port3) & multi->mask3) != multi->match3)
961
                        continue;
962
                if (!multi->port4)
963
                        break;
964
                if ((inb(multi->port4) & multi->mask4) == multi->match4)
965
                        continue;
966
                break;
967
        }
968
#ifdef SERIAL_DEBUG_INTR
969
        printk("end.\n");
970
#endif
971
}
972
 
973
 
974
/*
975
 * -------------------------------------------------------------------
976
 * Here ends the serial interrupt routines.
977
 * -------------------------------------------------------------------
978
 */
979
 
980
/*
981
 * This routine is used to handle the "bottom half" processing for the
982
 * serial driver, known also the "software interrupt" processing.
983
 * This processing is done at the kernel interrupt level, after the
984
 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
985
 * is where time-consuming activities which can not be done in the
986
 * interrupt driver proper are done; the interrupt driver schedules
987
 * them using rs_sched_event(), and they get done here.
988
 */
989
static void do_serial_bh(void)
990
{
991
        run_task_queue(&tq_serial);
992
}
993
 
994
static void do_softint(void *private_)
995
{
996
        struct async_struct     *info = (struct async_struct *) private_;
997
        struct tty_struct       *tty;
998
 
999
        tty = info->tty;
1000
        if (!tty)
1001
                return;
1002
 
1003
        if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
1004
                if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1005
                    tty->ldisc.write_wakeup)
1006
                        (tty->ldisc.write_wakeup)(tty);
1007
                wake_up_interruptible(&tty->write_wait);
1008
        }
1009
}
1010
 
1011
/*
1012
 * This routine is called from the scheduler tqueue when the interrupt
1013
 * routine has signalled that a hangup has occurred.  The path of
1014
 * hangup processing is:
1015
 *
1016
 *      serial interrupt routine -> (scheduler tqueue) ->
1017
 *      do_serial_hangup() -> tty->hangup() -> rs_hangup()
1018
 *
1019
 */
1020
static void do_serial_hangup(void *private_)
1021
{
1022
        struct async_struct     *info = (struct async_struct *) private_;
1023
        struct tty_struct       *tty;
1024
 
1025
        tty = info->tty;
1026
        if (!tty)
1027
                return;
1028
 
1029
        tty_hangup(tty);
1030
}
1031
 
1032
 
1033
/*
1034
 * This subroutine is called when the RS_TIMER goes off.  It is used
1035
 * by the serial driver to handle ports that do not have an interrupt
1036
 * (irq=0).  This doesn't work very well for 16450's, but gives barely
1037
 * passable results for a 16550A.  (Although at the expense of much
1038
 * CPU overhead).
1039
 */
1040
static void rs_timer(void)
1041
{
1042
        static unsigned long last_strobe = 0;
1043
        struct async_struct *info;
1044
        unsigned int    i;
1045
 
1046
        if ((jiffies - last_strobe) >= RS_STROBE_TIME) {
1047
                for (i=1; i < 16; i++) {
1048
                        info = IRQ_ports[IRQMASK(i)];
1049
                        if (!info)
1050
                                continue;
1051
                        cli();
1052
                        if (info->next_port) {
1053
                                do {
1054
                                        serial_out(info, UART_IER, 0);
1055
                                        info->IER |= UART_IER_THRI;
1056
                                        serial_out(info, UART_IER, info->IER);
1057
                                        info = info->next_port;
1058
                                } while (info);
1059
                                if (rs_multiport[IRQMASK(i)].port1)
1060
                                        rs_interrupt_multi(i, NULL, NULL);
1061
                                else
1062
                                        rs_interrupt(i, NULL, NULL);
1063
                        } else
1064
                                rs_interrupt_single(i, NULL, NULL);
1065
                        sti();
1066
                }
1067
        }
1068
        last_strobe = jiffies;
1069
        timer_table[RS_TIMER].expires = jiffies + RS_STROBE_TIME;
1070
        timer_active |= 1 << RS_TIMER;
1071
 
1072
        if (IRQ_ports[IRQMASK(0)]) {
1073
                cli();
1074
                rs_interrupt(0, NULL, NULL);
1075
                sti();
1076
 
1077
                timer_table[RS_TIMER].expires = jiffies + IRQ_timeout[0] - 2;
1078
        }
1079
}
1080
 
1081
/*
1082
 * ---------------------------------------------------------------
1083
 * Low level utility subroutines for the serial driver:  routines to
1084
 * figure out the appropriate timeout for an interrupt chain, routines
1085
 * to initialize and startup a serial port, and routines to shutdown a
1086
 * serial port.  Useful stuff like that.
1087
 * ---------------------------------------------------------------
1088
 */
1089
 
1090
#ifndef CONFIG_COLDFIRE
1091
/*
1092
 * Grab all interrupts in preparation for doing an automatic irq
1093
 * detection.  dontgrab is a mask of irq's _not_ to grab.  Returns a
1094
 * mask of irq's which were grabbed and should therefore be freed
1095
 * using free_all_interrupts().
1096
 */
1097
static int grab_all_interrupts(int dontgrab)
1098
{
1099
        int                     irq_lines = 0;
1100
        int                     i, mask;
1101
 
1102
        for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1103
                if (!(mask & dontgrab) && !request_irq(i, rs_probe, SA_INTERRUPT, "serial probe", NULL)) {
1104
                        irq_lines |= mask;
1105
                }
1106
        }
1107
        return irq_lines;
1108
}
1109
 
1110
/*
1111
 * Release all interrupts grabbed by grab_all_interrupts
1112
 */
1113
static void free_all_interrupts(int irq_lines)
1114
{
1115
        int     i;
1116
 
1117
        for (i = 0; i < 16; i++) {
1118
                if (irq_lines & (1 << i))
1119
                        free_irq(i, NULL);
1120
        }
1121
}
1122
#endif /* CONFIG_COLDFIRE */
1123
 
1124
/*
1125
 * This routine figures out the correct timeout for a particular IRQ.
1126
 * It uses the smallest timeout of all of the serial ports in a
1127
 * particular interrupt chain.  Now only used for IRQ 0....
1128
 */
1129
static void figure_IRQ_timeout(int irq)
1130
{
1131
        struct  async_struct    *info;
1132
        int     timeout = 60*HZ;        /* 60 seconds === a long time :-) */
1133
 
1134
        info = IRQ_ports[IRQMASK(irq)];
1135
        if (!info) {
1136
                IRQ_timeout[IRQMASK(irq)] = 60*HZ;
1137
                return;
1138
        }
1139
        while (info) {
1140
                if (info->timeout < timeout)
1141
                        timeout = info->timeout;
1142
                info = info->next_port;
1143
        }
1144
        if (!irq)
1145
                timeout = timeout / 2;
1146
        IRQ_timeout[IRQMASK(irq)] = timeout ? timeout : 1;
1147
}
1148
 
1149
static int startup(struct async_struct * info)
1150
{
1151
        unsigned int ICP;
1152
        unsigned long flags;
1153
        int     retval;
1154
        void (*handler)(int, void *, struct pt_regs *);
1155
        unsigned long page;
1156
 
1157
        page = get_free_page(GFP_KERNEL);
1158
        if (!page)
1159
                return -ENOMEM;
1160
 
1161
 
1162
        save_flags(flags); cli();
1163
 
1164
        if (info->flags & ASYNC_INITIALIZED) {
1165
                free_page(page);
1166
                restore_flags(flags);
1167
                return 0;
1168
        }
1169
 
1170
        if (!info->port || !info->type) {
1171
                if (info->tty)
1172
                        set_bit(TTY_IO_ERROR, &info->tty->flags);
1173
                free_page(page);
1174
                restore_flags(flags);
1175
                return 0;
1176
        }
1177
        if (info->xmit_buf)
1178
                free_page(page);
1179
        else
1180
                info->xmit_buf = (unsigned char *) page;
1181
 
1182
#ifdef SERIAL_DEBUG_OPEN
1183
        printk("starting up ttys%d (irq %d)...", info->line, info->irq);
1184
#endif
1185
 
1186
        /*
1187
         * Clear the FIFO buffers and disable them
1188
         * (they will be reenabled in change_speed())
1189
         */
1190
        if (info->type == PORT_16650) {
1191
                serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1192
                                             UART_FCR_CLEAR_XMIT));
1193
                info->xmit_fifo_size = 1; /* disabled for now */
1194
        } else if (info->type == PORT_16550A) {
1195
                serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1196
                                             UART_FCR_CLEAR_XMIT));
1197
                info->xmit_fifo_size = 16;
1198
        } else
1199
                info->xmit_fifo_size = 1;
1200
 
1201
        /*
1202
         * At this point there's no way the LSR could still be 0xFF;
1203
         * if it is, then bail out, because there's likely no UART
1204
         * here.
1205
         */
1206
        if (serial_inp(info, UART_LSR) == 0xff) {
1207
                restore_flags(flags);
1208
                if (suser()) {
1209
                        if (info->tty)
1210
                                set_bit(TTY_IO_ERROR, &info->tty->flags);
1211
                        return 0;
1212
                } else
1213
                        return -ENODEV;
1214
        }
1215
 
1216
#ifndef CONFIG_COLDFIRE
1217
        /*
1218
         * Allocate the IRQ if necessary
1219
         */
1220
        if (info->irq && (!IRQ_ports[IRQMASK(info->irq)] ||
1221
                          !IRQ_ports[IRQMASK(info->irq)]->next_port)) {
1222
                if (IRQ_ports[IRQMASK(info->irq)]) {
1223
                        free_irq(info->irq, NULL);
1224
                        if (rs_multiport[IRQMASK(info->irq)].port1)
1225
                                handler = rs_interrupt_multi;
1226
                        else
1227
                                handler = rs_interrupt;
1228
                } else
1229
                        handler = rs_interrupt_single;
1230
 
1231
                retval = request_irq(info->irq, handler, IRQ_T(info),
1232
                                     "serial", NULL);
1233
                if (retval) {
1234
                        restore_flags(flags);
1235
                        if (suser()) {
1236
                                if (info->tty)
1237
                                        set_bit(TTY_IO_ERROR,
1238
                                                &info->tty->flags);
1239
                                return 0;
1240
                        } else
1241
                                return retval;
1242
                }
1243
        }
1244
#endif
1245
 
1246
        /*
1247
         * Clear the interrupt registers.
1248
         */
1249
     /* (void) serial_inp(info, UART_LSR); */   /* (see above) */
1250
        (void) serial_inp(info, UART_RX);
1251
        (void) serial_inp(info, UART_IIR);
1252
        (void) serial_inp(info, UART_MSR);
1253
 
1254
        /*
1255
         * Now, initialize the UART
1256
         */
1257
        serial_outp(info, UART_LCR, UART_LCR_WLEN8);    /* reset DLAB */
1258
        if (info->flags & ASYNC_FOURPORT) {
1259
                info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1260
                info->MCR_noint = UART_MCR_DTR | UART_MCR_OUT1;
1261
        } else {
1262
                info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
1263
                info->MCR_noint = UART_MCR_DTR | UART_MCR_RTS;
1264
        }
1265
#if defined(__alpha__) && !defined(CONFIG_PCI)
1266
        info->MCR |= UART_MCR_OUT1 | UART_MCR_OUT2;
1267
        info->MCR_noint |= UART_MCR_OUT1 | UART_MCR_OUT2;
1268
#endif
1269
        if (info->irq == 0)
1270
                info->MCR = info->MCR_noint;
1271
        serial_outp(info, UART_MCR, info->MCR);
1272
 
1273
        /*
1274
         * Finally, enable interrupts
1275
         */
1276
        info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1277
        serial_outp(info, UART_IER, info->IER); /* enable interrupts */
1278
 
1279
        if (info->flags & ASYNC_FOURPORT) {
1280
                /* Enable interrupts on the AST Fourport board */
1281
                ICP = (info->port & 0xFE0) | 0x01F;
1282
                outb_p(0x80, ICP);
1283
                (void) inb_p(ICP);
1284
        }
1285
 
1286
        /*
1287
         * And clear the interrupt registers again for luck.
1288
         */
1289
        (void)serial_inp(info, UART_LSR);
1290
        (void)serial_inp(info, UART_RX);
1291
        (void)serial_inp(info, UART_IIR);
1292
        (void)serial_inp(info, UART_MSR);
1293
 
1294
        if (info->tty)
1295
                clear_bit(TTY_IO_ERROR, &info->tty->flags);
1296
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1297
 
1298
        /*
1299
         * Insert serial port into IRQ chain.
1300
         */
1301
        info->prev_port = 0;
1302
        info->next_port = IRQ_ports[IRQMASK(info->irq)];
1303
        if (info->next_port)
1304
                info->next_port->prev_port = info;
1305
        IRQ_ports[IRQMASK(info->irq)] = info;
1306
        figure_IRQ_timeout(info->irq);
1307
 
1308
        /*
1309
         * Set up serial timers...
1310
         */
1311
        timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
1312
        timer_active |= 1 << RS_TIMER;
1313
 
1314
        /*
1315
         * and set the speed of the serial port
1316
         */
1317
        change_speed(info);
1318
 
1319
        info->flags |= ASYNC_INITIALIZED;
1320
        restore_flags(flags);
1321
        return 0;
1322
}
1323
 
1324
/*
1325
 * This routine will shutdown a serial port; interrupts are disabled, and
1326
 * DTR is dropped if the hangup on close termio flag is on.
1327
 */
1328
static void shutdown(struct async_struct * info)
1329
{
1330
        unsigned long   flags;
1331
        int             retval;
1332
 
1333
        if (!(info->flags & ASYNC_INITIALIZED))
1334
                return;
1335
 
1336
#ifdef SERIAL_DEBUG_OPEN
1337
        printk("Shutting down serial port %d (irq %d)....", info->line,
1338
               info->irq);
1339
#endif
1340
 
1341
        save_flags(flags); cli(); /* Disable interrupts */
1342
 
1343
        /*
1344
         * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1345
         * here so the queue might never be waken up
1346
         */
1347
        wake_up_interruptible(&info->delta_msr_wait);
1348
 
1349
        /*
1350
         * First unlink the serial port from the IRQ chain...
1351
         */
1352
        if (info->next_port)
1353
                info->next_port->prev_port = info->prev_port;
1354
        if (info->prev_port)
1355
                info->prev_port->next_port = info->next_port;
1356
        else
1357
                IRQ_ports[IRQMASK(info->irq)] = info->next_port;
1358
        figure_IRQ_timeout(info->irq);
1359
 
1360
#ifndef CONFIG_COLDFIRE
1361
        /*
1362
         * Free the IRQ, if necessary
1363
         */
1364
        if (info->irq && (!IRQ_ports[IRQMASK(info->irq)] ||
1365
                          !IRQ_ports[IRQMASK(info->irq)]->next_port)) {
1366
                if (IRQ_ports[IRQMASK(info->irq)]) {
1367
                        free_irq(info->irq, NULL);
1368
                        retval = request_irq(info->irq, rs_interrupt_single,
1369
                                             IRQ_T(info), "serial", NULL);
1370
 
1371
                        if (retval)
1372
                                printk("serial shutdown: request_irq: error %d"
1373
                                       "  Couldn't reacquire IRQ.\n", retval);
1374
                } else
1375
                        free_irq(info->irq, NULL);
1376
        }
1377
#endif /* CONFIG_COLDFIRE */
1378
 
1379
        if (info->xmit_buf) {
1380
                free_page((unsigned long) info->xmit_buf);
1381
                info->xmit_buf = 0;
1382
        }
1383
 
1384
        info->IER = 0;
1385
        serial_outp(info, UART_IER, 0x00);      /* disable all intrs */
1386
        if (info->flags & ASYNC_FOURPORT) {
1387
                /* reset interrupts on the AST Fourport board */
1388
                (void) inb((info->port & 0xFE0) | 0x01F);
1389
        }
1390
 
1391
        if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1392
                info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1393
                info->MCR_noint &= ~(UART_MCR_DTR|UART_MCR_RTS);
1394
        }
1395
        serial_outp(info, UART_MCR, info->MCR_noint);
1396
 
1397
        /* disable FIFO's */
1398
        serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1399
                                     UART_FCR_CLEAR_XMIT));
1400
        (void)serial_in(info, UART_RX);    /* read data port to reset things */
1401
 
1402
        if (info->tty)
1403
                set_bit(TTY_IO_ERROR, &info->tty->flags);
1404
 
1405
        info->flags &= ~ASYNC_INITIALIZED;
1406
        restore_flags(flags);
1407
}
1408
 
1409
/*
1410
 * This routine is called to set the UART divisor registers to match
1411
 * the specified baud rate for a serial port.
1412
 */
1413
static void change_speed(struct async_struct *info)
1414
{
1415
        unsigned int port;
1416
        int     quot = 0;
1417
        unsigned cflag,cval,fcr;
1418
        int     i;
1419
        unsigned long flags;
1420
 
1421
        if (!info->tty || !info->tty->termios)
1422
                return;
1423
        cflag = info->tty->termios->c_cflag;
1424
        if (!(port = info->port))
1425
                return;
1426
        i = cflag & CBAUD;
1427
        if (i & CBAUDEX) {
1428
                i &= ~CBAUDEX;
1429
                if (i < 1 || i > 2)
1430
                        info->tty->termios->c_cflag &= ~CBAUDEX;
1431
                else
1432
                        i += 15;
1433
        }
1434
        if (i == 15) {
1435
                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1436
                        i += 1;
1437
                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1438
                        i += 2;
1439
                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
1440
                        quot = info->custom_divisor;
1441
        }
1442
        if (quot) {
1443
                info->timeout = ((info->xmit_fifo_size*HZ*15*quot) /
1444
                                 info->baud_base) + 2;
1445
        } else if (baud_table[i] == 134) {
1446
                quot = (2*info->baud_base / 269);
1447
                info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
1448
        } else if (baud_table[i]) {
1449
                quot = info->baud_base / baud_table[i];
1450
                info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
1451
        } else {
1452
                quot = 0;
1453
                info->timeout = 0;
1454
        }
1455
        if (quot) {
1456
                info->MCR |= UART_MCR_DTR;
1457
                info->MCR_noint |= UART_MCR_DTR;
1458
                save_flags(flags); cli();
1459
                serial_out(info, UART_MCR, info->MCR);
1460
                restore_flags(flags);
1461
        } else {
1462
                info->MCR &= ~UART_MCR_DTR;
1463
                info->MCR_noint &= ~UART_MCR_DTR;
1464
                save_flags(flags); cli();
1465
                serial_out(info, UART_MCR, info->MCR);
1466
                restore_flags(flags);
1467
                return;
1468
        }
1469
        /* byte size and parity */
1470
        switch (cflag & CSIZE) {
1471
              case CS5: cval = 0x00; break;
1472
              case CS6: cval = 0x01; break;
1473
              case CS7: cval = 0x02; break;
1474
              case CS8: cval = 0x03; break;
1475
              default:  cval = 0x00; break;     /* too keep GCC shut... */
1476
        }
1477
        if (cflag & CSTOPB) {
1478
                cval |= 0x04;
1479
        }
1480
        if (cflag & PARENB)
1481
                cval |= UART_LCR_PARITY;
1482
        if (!(cflag & PARODD))
1483
                cval |= UART_LCR_EPAR;
1484
        if (info->type == PORT_16550A) {
1485
                if ((info->baud_base / quot) < 2400)
1486
                        fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1487
                else
1488
                        fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1489
        } else if (info->type == PORT_16650) {
1490
                /*
1491
                 * On the 16650, we disable the FIFOs altogether
1492
                 * because of a design bug in how the implement
1493
                 * things.  We could support it by completely changing
1494
                 * how we handle the interrupt driver, but not today....
1495
                 *
1496
                 * N.B.  Because there's no way to set a FIFO trigger
1497
                 * at 1 char, we'd probably disable at speed below
1498
                 * 2400 baud anyway...
1499
                 */
1500
                fcr = 0;
1501
        } else
1502
                fcr = 0;
1503
 
1504
        /* CTS flow control flag and modem status interrupts */
1505
        info->IER &= ~UART_IER_MSI;
1506
        if (cflag & CRTSCTS) {
1507
                info->flags |= ASYNC_CTS_FLOW;
1508
                info->IER |= UART_IER_MSI;
1509
        } else
1510
                info->flags &= ~ASYNC_CTS_FLOW;
1511
        if (cflag & CLOCAL)
1512
                info->flags &= ~ASYNC_CHECK_CD;
1513
        else {
1514
                info->flags |= ASYNC_CHECK_CD;
1515
                info->IER |= UART_IER_MSI;
1516
        }
1517
        serial_out(info, UART_IER, info->IER);
1518
 
1519
        /*
1520
         * Set up parity check flag
1521
         */
1522
#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1523
 
1524
        info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1525
        if (I_INPCK(info->tty))
1526
                info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1527
        if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1528
                info->read_status_mask |= UART_LSR_BI;
1529
 
1530
        info->ignore_status_mask = 0;
1531
#if 0
1532
        /* This should be safe, but for some broken bits of hardware... */
1533
        if (I_IGNPAR(info->tty)) {
1534
                info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1535
                info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1536
        }
1537
#endif
1538
        if (I_IGNBRK(info->tty)) {
1539
                info->ignore_status_mask |= UART_LSR_BI;
1540
                info->read_status_mask |= UART_LSR_BI;
1541
                /*
1542
                 * If we're ignore parity and break indicators, ignore
1543
                 * overruns too.  (For real raw support).
1544
                 */
1545
                if (I_IGNPAR(info->tty)) {
1546
                        info->ignore_status_mask |= UART_LSR_OE | \
1547
                                UART_LSR_PE | UART_LSR_FE;
1548
                        info->read_status_mask |= UART_LSR_OE | \
1549
                                UART_LSR_PE | UART_LSR_FE;
1550
                }
1551
        }
1552
        save_flags(flags); cli();
1553
        serial_outp(info, UART_LCR, cval | UART_LCR_DLAB);      /* set DLAB */
1554
        serial_outp(info, UART_DLL, quot & 0xff);       /* LS of divisor */
1555
        serial_outp(info, UART_DLM, quot >> 8);         /* MS of divisor */
1556
        serial_outp(info, UART_LCR, cval);              /* reset DLAB */
1557
        serial_outp(info, UART_FCR, fcr);       /* set fcr */
1558
        restore_flags(flags);
1559
}
1560
 
1561
static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1562
{
1563
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1564
        unsigned long flags;
1565
 
1566
        if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1567
                return;
1568
 
1569
        if (!tty || !info->xmit_buf)
1570
                return;
1571
 
1572
        save_flags(flags); cli();
1573
        if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1574
                restore_flags(flags);
1575
                return;
1576
        }
1577
 
1578
        info->xmit_buf[info->xmit_head++] = ch;
1579
        info->xmit_head &= SERIAL_XMIT_SIZE-1;
1580
        info->xmit_cnt++;
1581
        restore_flags(flags);
1582
}
1583
 
1584
static void rs_flush_chars(struct tty_struct *tty)
1585
{
1586
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1587
        unsigned long flags;
1588
 
1589
        if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1590
                return;
1591
 
1592
        if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1593
            !info->xmit_buf)
1594
                return;
1595
 
1596
        save_flags(flags); cli();
1597
        info->IER |= UART_IER_THRI;
1598
        serial_out(info, UART_IER, info->IER);
1599
        restore_flags(flags);
1600
}
1601
 
1602
static int rs_write(struct tty_struct * tty, int from_user,
1603
                    const unsigned char *buf, int count)
1604
{
1605
        int     c, total = 0;
1606
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1607
        unsigned long flags;
1608
 
1609
        if (serial_paranoia_check(info, tty->device, "rs_write"))
1610
                return 0;
1611
 
1612
        if (!tty || !info->xmit_buf || !tmp_buf)
1613
                return 0;
1614
 
1615
        if (from_user)
1616
                down(&tmp_buf_sem);
1617
        save_flags(flags);
1618
        while (1) {
1619
                cli();
1620
                c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1621
                                   SERIAL_XMIT_SIZE - info->xmit_head));
1622
                if (c <= 0)
1623
                        break;
1624
 
1625
                if (from_user) {
1626
                        memcpy_fromfs(tmp_buf, buf, c);
1627
                        c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1628
                                       SERIAL_XMIT_SIZE - info->xmit_head));
1629
                        memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1630
                } else
1631
                        memcpy(info->xmit_buf + info->xmit_head, buf, c);
1632
                info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1633
                info->xmit_cnt += c;
1634
                restore_flags(flags);
1635
                buf += c;
1636
                count -= c;
1637
                total += c;
1638
        }
1639
        if (from_user)
1640
                up(&tmp_buf_sem);
1641
        if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1642
            !(info->IER & UART_IER_THRI)) {
1643
                info->IER |= UART_IER_THRI;
1644
                serial_out(info, UART_IER, info->IER);
1645
        }
1646
        restore_flags(flags);
1647
        return total;
1648
}
1649
 
1650
static int rs_write_room(struct tty_struct *tty)
1651
{
1652
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1653
        int     ret;
1654
 
1655
        if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1656
                return 0;
1657
        ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1658
        if (ret < 0)
1659
                ret = 0;
1660
        return ret;
1661
}
1662
 
1663
static int rs_chars_in_buffer(struct tty_struct *tty)
1664
{
1665
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1666
 
1667
        if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1668
                return 0;
1669
        return info->xmit_cnt;
1670
}
1671
 
1672
static void rs_flush_buffer(struct tty_struct *tty)
1673
{
1674
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1675
 
1676
        if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1677
                return;
1678
        cli();
1679
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1680
        sti();
1681
        wake_up_interruptible(&tty->write_wait);
1682
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1683
            tty->ldisc.write_wakeup)
1684
                (tty->ldisc.write_wakeup)(tty);
1685
}
1686
 
1687
/*
1688
 * ------------------------------------------------------------
1689
 * rs_throttle()
1690
 *
1691
 * This routine is called by the upper-layer tty layer to signal that
1692
 * incoming characters should be throttled.
1693
 * ------------------------------------------------------------
1694
 */
1695
static void rs_throttle(struct tty_struct * tty)
1696
{
1697
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1698
#ifdef SERIAL_DEBUG_THROTTLE
1699
        char    buf[64];
1700
 
1701
        printk("throttle %s: %d....\n", _tty_name(tty, buf),
1702
               tty->ldisc.chars_in_buffer(tty));
1703
#endif
1704
 
1705
        if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1706
                return;
1707
 
1708
        if (I_IXOFF(tty))
1709
                info->x_char = STOP_CHAR(tty);
1710
 
1711
        info->MCR &= ~UART_MCR_RTS;
1712
        info->MCR_noint &= ~UART_MCR_RTS;
1713
        cli();
1714
        serial_out(info, UART_MCR, info->MCR);
1715
        sti();
1716
}
1717
 
1718
static void rs_unthrottle(struct tty_struct * tty)
1719
{
1720
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1721
#ifdef SERIAL_DEBUG_THROTTLE
1722
        char    buf[64];
1723
 
1724
        printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1725
               tty->ldisc.chars_in_buffer(tty));
1726
#endif
1727
 
1728
        if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1729
                return;
1730
 
1731
        if (I_IXOFF(tty)) {
1732
                if (info->x_char)
1733
                        info->x_char = 0;
1734
                else
1735
                        info->x_char = START_CHAR(tty);
1736
        }
1737
        info->MCR |= UART_MCR_RTS;
1738
        info->MCR_noint |= UART_MCR_RTS;
1739
        cli();
1740
        serial_out(info, UART_MCR, info->MCR);
1741
        sti();
1742
}
1743
 
1744
/*
1745
 * ------------------------------------------------------------
1746
 * rs_ioctl() and friends
1747
 * ------------------------------------------------------------
1748
 */
1749
 
1750
static int get_serial_info(struct async_struct * info,
1751
                           struct serial_struct * retinfo)
1752
{
1753
        struct serial_struct tmp;
1754
 
1755
        if (!retinfo)
1756
                return -EFAULT;
1757
        memset(&tmp, 0, sizeof(tmp));
1758
        tmp.type = info->type;
1759
        tmp.line = info->line;
1760
        tmp.port = info->port;
1761
        tmp.irq = info->irq;
1762
        tmp.flags = info->flags;
1763
        tmp.baud_base = info->baud_base;
1764
        tmp.close_delay = info->close_delay;
1765
        tmp.closing_wait = info->closing_wait;
1766
        tmp.custom_divisor = info->custom_divisor;
1767
        tmp.hub6 = info->hub6;
1768
        memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
1769
        return 0;
1770
}
1771
 
1772
static int set_serial_info(struct async_struct * info,
1773
                           struct serial_struct * new_info)
1774
{
1775
        struct serial_struct new_serial;
1776
        struct async_struct old_info;
1777
        unsigned int            i,change_irq,change_port;
1778
        int                     retval = 0;
1779
 
1780
        if (!new_info)
1781
                return -EFAULT;
1782
        memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
1783
        old_info = *info;
1784
 
1785
        change_irq = new_serial.irq != info->irq;
1786
        change_port = (new_serial.port != info->port) || (new_serial.hub6 != info->hub6);
1787
 
1788
        if (!suser()) {
1789
                if (change_irq || change_port ||
1790
                    (new_serial.baud_base != info->baud_base) ||
1791
                    (new_serial.type != info->type) ||
1792
                    (new_serial.close_delay != info->close_delay) ||
1793
                    ((new_serial.flags & ~ASYNC_USR_MASK) !=
1794
                     (info->flags & ~ASYNC_USR_MASK)))
1795
                        return -EPERM;
1796
                info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1797
                               (new_serial.flags & ASYNC_USR_MASK));
1798
                info->custom_divisor = new_serial.custom_divisor;
1799
                goto check_and_exit;
1800
        }
1801
/* SIMON */
1802
#if 0
1803
#ifndef CONFIG_COLDFIRE
1804
        if (new_serial.irq == 2)
1805
                new_serial.irq = 9;
1806
 
1807
        if ((new_serial.irq > 15) || (new_serial.port > 0xffff) ||
1808
            (new_serial.type < PORT_UNKNOWN) || (new_serial.type > PORT_MAX)) {
1809
                return -EINVAL;
1810
        }
1811
#endif
1812
#endif
1813
 
1814
        /* Make sure address is not already in use */
1815
        if (new_serial.type) {
1816
                for (i = 0 ; i < NR_PORTS; i++)
1817
                        if ((info != &rs_table[i]) &&
1818
                            (rs_table[i].port == new_serial.port) &&
1819
                            rs_table[i].type)
1820
                                return -EADDRINUSE;
1821
        }
1822
 
1823
        if ((change_port || change_irq) && (info->count > 1))
1824
                return -EBUSY;
1825
 
1826
        /*
1827
         * OK, past this point, all the error checking has been done.
1828
         * At this point, we start making changes.....
1829
         */
1830
 
1831
        info->baud_base = new_serial.baud_base;
1832
        info->flags = ((info->flags & ~ASYNC_FLAGS) |
1833
                        (new_serial.flags & ASYNC_FLAGS));
1834
        info->custom_divisor = new_serial.custom_divisor;
1835
        info->type = new_serial.type;
1836
        info->close_delay = new_serial.close_delay * HZ/100;
1837
        info->closing_wait = new_serial.closing_wait * HZ/100;
1838
 
1839
        release_region(info->port,8);
1840
        if (change_port || change_irq) {
1841
                /*
1842
                 * We need to shutdown the serial port at the old
1843
                 * port/irq combination.
1844
                 */
1845
                shutdown(info);
1846
                info->irq = new_serial.irq;
1847
                info->port = new_serial.port;
1848
                info->hub6 = new_serial.hub6;
1849
        }
1850
        if(info->type != PORT_UNKNOWN)
1851
                request_region(info->port,8,"serial(set)");
1852
 
1853
 
1854
check_and_exit:
1855
        if (!info->port || !info->type)
1856
                return 0;
1857
        if (info->flags & ASYNC_INITIALIZED) {
1858
                if (((old_info.flags & ASYNC_SPD_MASK) !=
1859
                     (info->flags & ASYNC_SPD_MASK)) ||
1860
                    (old_info.custom_divisor != info->custom_divisor))
1861
                        change_speed(info);
1862
        } else
1863
                retval = startup(info);
1864
        return retval;
1865
}
1866
 
1867
 
1868
/*
1869
 * get_lsr_info - get line status register info
1870
 *
1871
 * Purpose: Let user call ioctl() to get info when the UART physically
1872
 *          is emptied.  On bus types like RS485, the transmitter must
1873
 *          release the bus after transmitting. This must be done when
1874
 *          the transmit shift register is empty, not be done when the
1875
 *          transmit holding register is empty.  This functionality
1876
 *          allows an RS485 driver to be written in user space.
1877
 */
1878
static int get_lsr_info(struct async_struct * info, unsigned int *value)
1879
{
1880
        unsigned char status;
1881
        unsigned int result;
1882
 
1883
        cli();
1884
        status = serial_in(info, UART_LSR);
1885
        sti();
1886
        result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1887
        put_user(result,value);
1888
        return 0;
1889
}
1890
 
1891
 
1892
static int get_modem_info(struct async_struct * info, unsigned int *value)
1893
{
1894
        unsigned char control, status;
1895
        unsigned int result;
1896
 
1897
        control = info->MCR;
1898
        cli();
1899
        status = serial_in(info, UART_MSR);
1900
        sti();
1901
        result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1902
                | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1903
                | ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1904
                | ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1905
                | ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1906
                | ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1907
        put_user(result,value);
1908
        return 0;
1909
}
1910
 
1911
static int set_modem_info(struct async_struct * info, unsigned int cmd,
1912
                          unsigned int *value)
1913
{
1914
        int error;
1915
        unsigned int arg;
1916
 
1917
        error = verify_area(VERIFY_READ, value, sizeof(int));
1918
        if (error)
1919
                return error;
1920
        arg = get_user(value);
1921
        switch (cmd) {
1922
        case TIOCMBIS:
1923
                if (arg & TIOCM_RTS) {
1924
                        info->MCR |= UART_MCR_RTS;
1925
                        info->MCR_noint |= UART_MCR_RTS;
1926
                }
1927
                if (arg & TIOCM_DTR) {
1928
                        info->MCR |= UART_MCR_DTR;
1929
                        info->MCR_noint |= UART_MCR_DTR;
1930
                }
1931
                break;
1932
        case TIOCMBIC:
1933
                if (arg & TIOCM_RTS) {
1934
                        info->MCR &= ~UART_MCR_RTS;
1935
                        info->MCR_noint &= ~UART_MCR_RTS;
1936
                }
1937
                if (arg & TIOCM_DTR) {
1938
                        info->MCR &= ~UART_MCR_DTR;
1939
                        info->MCR_noint &= ~UART_MCR_DTR;
1940
                }
1941
                break;
1942
        case TIOCMSET:
1943
                info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR))
1944
                             | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1945
                             | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1946
                info->MCR_noint = ((info->MCR_noint
1947
                                    & ~(UART_MCR_RTS | UART_MCR_DTR))
1948
                                   | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1949
                                   | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1950
                break;
1951
        default:
1952
                return -EINVAL;
1953
        }
1954
        cli();
1955
        serial_out(info, UART_MCR, info->MCR);
1956
        sti();
1957
        return 0;
1958
}
1959
 
1960
static int do_autoconfig(struct async_struct * info)
1961
{
1962
        int                     retval;
1963
 
1964
        if (!suser())
1965
                return -EPERM;
1966
 
1967
        if (info->count > 1)
1968
                return -EBUSY;
1969
 
1970
        shutdown(info);
1971
 
1972
        cli();
1973
        autoconfig(info);
1974
        sti();
1975
 
1976
        retval = startup(info);
1977
        if (retval)
1978
                return retval;
1979
        return 0;
1980
}
1981
 
1982
 
1983
/*
1984
 * rs_break() --- routine which turns the break handling on or off
1985
 * adapted from 2.1.124
1986
 */
1987
static void rs_break(struct async_struct * info, int break_state)
1988
{
1989
        unsigned long flags;
1990
 
1991
        if (!info->port)
1992
                return;
1993
        save_flags(flags);cli();
1994
        if (break_state == -1)
1995
                serial_out(info, UART_LCR,
1996
                           serial_inp(info, UART_LCR) | UART_LCR_SBC);
1997
        else
1998
                serial_out(info, UART_LCR,
1999
                           serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
2000
        restore_flags(flags);
2001
}
2002
 
2003
/*
2004
 * This routine sends a break character out the serial port.
2005
 */
2006
static void send_break( struct async_struct * info, int duration)
2007
{
2008
        if (!info->port)
2009
                return;
2010
        current->state = TASK_INTERRUPTIBLE;
2011
        current->timeout = jiffies + duration;
2012
        cli();
2013
        serial_out(info, UART_LCR, serial_inp(info, UART_LCR) | UART_LCR_SBC);
2014
        schedule();
2015
        serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
2016
        sti();
2017
}
2018
 
2019
#ifndef CONFIG_COLDFIRE
2020
/*
2021
 * This routine returns a bitfield of "wild interrupts".  Basically,
2022
 * any unclaimed interrupts which is flapping around.
2023
 */
2024
static int check_wild_interrupts(int doprint)
2025
{
2026
        int     i, mask;
2027
        int     wild_interrupts = 0;
2028
        int     irq_lines;
2029
        unsigned long timeout;
2030
        unsigned long flags;
2031
 
2032
        /* Turn on interrupts (they may be off) */
2033
        save_flags(flags); sti();
2034
 
2035
        irq_lines = grab_all_interrupts(0);
2036
 
2037
        /*
2038
         * Delay for 0.1 seconds -- we use a busy loop since this may
2039
         * occur during the bootup sequence
2040
         */
2041
        timeout = jiffies+HZ/10;
2042
        while (timeout >= jiffies)
2043
                ;
2044
 
2045
        rs_triggered = 0;        /* Reset after letting things settle */
2046
 
2047
        timeout = jiffies+HZ/10;
2048
        while (timeout >= jiffies)
2049
                ;
2050
 
2051
        for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
2052
                if ((rs_triggered & (1 << i)) &&
2053
                    (irq_lines & (1 << i))) {
2054
                        wild_interrupts |= mask;
2055
                        if (doprint)
2056
                                printk("Wild interrupt?  (IRQ %d)\n", i);
2057
                }
2058
        }
2059
        free_all_interrupts(irq_lines);
2060
        restore_flags(flags);
2061
        return wild_interrupts;
2062
}
2063
 
2064
static int get_multiport_struct(struct async_struct * info,
2065
                                struct serial_multiport_struct *retinfo)
2066
{
2067
        struct serial_multiport_struct ret;
2068
        struct rs_multiport_struct *multi;
2069
 
2070
        multi = &rs_multiport[IRQMASK(info->irq)];
2071
 
2072
        ret.port_monitor = multi->port_monitor;
2073
 
2074
        ret.port1 = multi->port1;
2075
        ret.mask1 = multi->mask1;
2076
        ret.match1 = multi->match1;
2077
 
2078
        ret.port2 = multi->port2;
2079
        ret.mask2 = multi->mask2;
2080
        ret.match2 = multi->match2;
2081
 
2082
        ret.port3 = multi->port3;
2083
        ret.mask3 = multi->mask3;
2084
        ret.match3 = multi->match3;
2085
 
2086
        ret.port4 = multi->port4;
2087
        ret.mask4 = multi->mask4;
2088
        ret.match4 = multi->match4;
2089
 
2090
        ret.irq = info->irq;
2091
 
2092
        memcpy_tofs(retinfo,&ret,sizeof(*retinfo));
2093
        return 0;
2094
 
2095
}
2096
 
2097
static int set_multiport_struct(struct async_struct * info,
2098
                                struct serial_multiport_struct *in_multi)
2099
{
2100
        struct serial_multiport_struct new_multi;
2101
        struct rs_multiport_struct *multi;
2102
        int     was_multi, now_multi;
2103
        int     retval;
2104
        void (*handler)(int, void *, struct pt_regs *);
2105
 
2106
        if (!suser())
2107
                return -EPERM;
2108
        if (!in_multi)
2109
                return -EFAULT;
2110
        memcpy_fromfs(&new_multi, in_multi,
2111
                      sizeof(struct serial_multiport_struct));
2112
 
2113
        if (new_multi.irq != info->irq || info->irq == 0 ||
2114
            !IRQ_ports[IRQMASK(info->irq)])
2115
                return -EINVAL;
2116
 
2117
        multi = &rs_multiport[IRQMASK(info->irq)];
2118
        was_multi = (multi->port1 != 0);
2119
 
2120
        multi->port_monitor = new_multi.port_monitor;
2121
 
2122
        if (multi->port1)
2123
                release_region(multi->port1,1);
2124
        multi->port1 = new_multi.port1;
2125
        multi->mask1 = new_multi.mask1;
2126
        multi->match1 = new_multi.match1;
2127
        if (multi->port1)
2128
                request_region(multi->port1,1,"serial(multiport1)");
2129
 
2130
        if (multi->port2)
2131
                release_region(multi->port2,1);
2132
        multi->port2 = new_multi.port2;
2133
        multi->mask2 = new_multi.mask2;
2134
        multi->match2 = new_multi.match2;
2135
        if (multi->port2)
2136
                request_region(multi->port2,1,"serial(multiport2)");
2137
 
2138
        if (multi->port3)
2139
                release_region(multi->port3,1);
2140
        multi->port3 = new_multi.port3;
2141
        multi->mask3 = new_multi.mask3;
2142
        multi->match3 = new_multi.match3;
2143
        if (multi->port3)
2144
                request_region(multi->port3,1,"serial(multiport3)");
2145
 
2146
        if (multi->port4)
2147
                release_region(multi->port4,1);
2148
        multi->port4 = new_multi.port4;
2149
        multi->mask4 = new_multi.mask4;
2150
        multi->match4 = new_multi.match4;
2151
        if (multi->port4)
2152
                request_region(multi->port4,1,"serial(multiport4)");
2153
 
2154
        now_multi = (multi->port1 != 0);
2155
 
2156
        if (IRQ_ports[IRQMASK(info->irq)]->next_port &&
2157
            (was_multi != now_multi)) {
2158
                free_irq(info->irq, NULL);
2159
                if (now_multi)
2160
                        handler = rs_interrupt_multi;
2161
                else
2162
                        handler = rs_interrupt;
2163
 
2164
                retval = request_irq(info->irq, handler, IRQ_T(info),
2165
                                     "serial", NULL);
2166
                if (retval) {
2167
                        printk("Couldn't reallocate serial interrupt "
2168
                               "driver!!\n");
2169
                }
2170
        }
2171
 
2172
        return 0;
2173
}
2174
#endif /* CONFIG_COLDFIRE */
2175
 
2176
static int rs_ioctl(struct tty_struct *tty, struct file * file,
2177
                    unsigned int cmd, unsigned long arg)
2178
{
2179
        int error;
2180
        struct async_struct * info = (struct async_struct *)tty->driver_data;
2181
        int retval;
2182
        struct async_icount cprev, cnow;        /* kernel counter temps */
2183
        struct serial_icounter_struct *p_cuser; /* user space */
2184
 
2185
        if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
2186
                return -ENODEV;
2187
 
2188
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2189
            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
2190
            (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
2191
            (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2192
                if (tty->flags & (1 << TTY_IO_ERROR))
2193
                    return -EIO;
2194
        }
2195
 
2196
        switch (cmd) {
2197
                case TIOCSBRK:  /* Turn break on, unconditionally */
2198
                        retval = tty_check_change(tty);
2199
                        if (retval)
2200
                                return retval;
2201
                        tty_wait_until_sent(tty, 0);
2202
                        rs_break(info,-1);
2203
                        return 0;
2204
                case TIOCCBRK:  /* Turn break off, unconditionally */
2205
                        retval = tty_check_change(tty);
2206
                        if (retval)
2207
                                return retval;
2208
                        tty_wait_until_sent(tty, 0);
2209
                        rs_break(info,0);
2210
                        return 0;
2211
                case TCSBRK:    /* SVID version: non-zero arg --> no break */
2212
                        retval = tty_check_change(tty);
2213
                        if (retval)
2214
                                return retval;
2215
                        tty_wait_until_sent(tty, 0);
2216
                        if (!arg)
2217
                                send_break(info, HZ/4); /* 1/4 second */
2218
                        return 0;
2219
                case TCSBRKP:   /* support for POSIX tcsendbreak() */
2220
                        retval = tty_check_change(tty);
2221
                        if (retval)
2222
                                return retval;
2223
                        tty_wait_until_sent(tty, 0);
2224
                        send_break(info, arg ? arg*(HZ/10) : HZ/4);
2225
                        return 0;
2226
                case TIOCGSOFTCAR:
2227
                        error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
2228
                        if (error)
2229
                                return error;
2230
                        put_fs_long(C_CLOCAL(tty) ? 1 : 0,
2231
                                    (unsigned long *) arg);
2232
                        return 0;
2233
                case TIOCSSOFTCAR:
2234
                        error = verify_area(VERIFY_READ, (void *) arg,sizeof(long));
2235
                        if (error)
2236
                                return error;
2237
                        arg = get_fs_long((unsigned long *) arg);
2238
                        tty->termios->c_cflag =
2239
                                ((tty->termios->c_cflag & ~CLOCAL) |
2240
                                 (arg ? CLOCAL : 0));
2241
                        return 0;
2242
                case TIOCMGET:
2243
                        error = verify_area(VERIFY_WRITE, (void *) arg,
2244
                                sizeof(unsigned int));
2245
                        if (error)
2246
                                return error;
2247
                        return get_modem_info(info, (unsigned int *) arg);
2248
                case TIOCMBIS:
2249
                case TIOCMBIC:
2250
                case TIOCMSET:
2251
                        return set_modem_info(info, cmd, (unsigned int *) arg);
2252
                case TIOCGSERIAL:
2253
                        error = verify_area(VERIFY_WRITE, (void *) arg,
2254
                                                sizeof(struct serial_struct));
2255
                        if (error)
2256
                                return error;
2257
                        return get_serial_info(info,
2258
                                               (struct serial_struct *) arg);
2259
                case TIOCSSERIAL:
2260
                        error = verify_area(VERIFY_READ, (void *) arg,
2261
                                                sizeof(struct serial_struct));
2262
                        if (error)
2263
                                return error;
2264
                        return set_serial_info(info,
2265
                                               (struct serial_struct *) arg);
2266
                case TIOCSERCONFIG:
2267
                        return do_autoconfig(info);
2268
 
2269
#ifndef CONFIG_COLDFIRE
2270
                case TIOCSERGWILD:
2271
                        error = verify_area(VERIFY_WRITE, (void *) arg,
2272
                                            sizeof(int));
2273
                        if (error)
2274
                                return error;
2275
                        put_fs_long(rs_wild_int_mask, (unsigned long *) arg);
2276
                        return 0;
2277
#endif /* CONFIG_COLDFIRE */
2278
                case TIOCSERGETLSR: /* Get line status register */
2279
                        error = verify_area(VERIFY_WRITE, (void *) arg,
2280
                                sizeof(unsigned int));
2281
                        if (error)
2282
                                return error;
2283
                        else
2284
                            return get_lsr_info(info, (unsigned int *) arg);
2285
#ifndef CONFIG_COLDFIRE
2286
                case TIOCSERSWILD:
2287
                        if (!suser())
2288
                                return -EPERM;
2289
                        error = verify_area(VERIFY_READ, (void *) arg,sizeof(long));
2290
                        if (error)
2291
                                return error;
2292
                        rs_wild_int_mask = get_fs_long((unsigned long *) arg);
2293
                        if (rs_wild_int_mask < 0)
2294
                                rs_wild_int_mask = check_wild_interrupts(0);
2295
#endif /* CONFIG_COLDFIRE */
2296
                        return 0;
2297
 
2298
                case TIOCSERGSTRUCT:
2299
                        error = verify_area(VERIFY_WRITE, (void *) arg,
2300
                                                sizeof(struct async_struct));
2301
                        if (error)
2302
                                return error;
2303
                        memcpy_tofs((struct async_struct *) arg,
2304
                                    info, sizeof(struct async_struct));
2305
                        return 0;
2306
 
2307
#ifndef CONFIG_COLDFIRE
2308
                case TIOCSERGETMULTI:
2309
                        error = verify_area(VERIFY_WRITE, (void *) arg,
2310
                                    sizeof(struct serial_multiport_struct));
2311
                        if (error)
2312
                                return error;
2313
                        return get_multiport_struct(info,
2314
                                       (struct serial_multiport_struct *) arg);
2315
                case TIOCSERSETMULTI:
2316
                        error = verify_area(VERIFY_READ, (void *) arg,
2317
                                    sizeof(struct serial_multiport_struct));
2318
                        if (error)
2319
                                return error;
2320
                        return set_multiport_struct(info,
2321
                                       (struct serial_multiport_struct *) arg);
2322
#endif /* CONFIG_COLDFIRE */
2323
                /*
2324
                 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
2325
                 * - mask passed in arg for lines of interest
2326
                 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
2327
                 * Caller should use TIOCGICOUNT to see which one it was
2328
                 */
2329
                 case TIOCMIWAIT:
2330
                        cli();
2331
                        cprev = info->icount;   /* note the counters on entry */
2332
                        sti();
2333
                        while (1) {
2334
                                interruptible_sleep_on(&info->delta_msr_wait);
2335
                                /* see if a signal did it */
2336
                                if (current->signal & ~current->blocked)
2337
                                        return -ERESTARTSYS;
2338
                                cli();
2339
                                cnow = info->icount;    /* atomic copy */
2340
                                sti();
2341
                                if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2342
                                    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2343
                                        return -EIO; /* no change => error */
2344
                                if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2345
                                     ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2346
                                     ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
2347
                                     ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2348
                                        return 0;
2349
                                }
2350
                                cprev = cnow;
2351
                        }
2352
                        /* NOTREACHED */
2353
 
2354
                /*
2355
                 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2356
                 * Return: write counters to the user passed counter struct
2357
                 * NB: both 1->0 and 0->1 transitions are counted except for
2358
                 *     RI where only 0->1 is counted.
2359
                 */
2360
                case TIOCGICOUNT:
2361
                        error = verify_area(VERIFY_WRITE, (void *) arg,
2362
                                sizeof(struct serial_icounter_struct));
2363
                        if (error)
2364
                                return error;
2365
                        cli();
2366
                        cnow = info->icount;
2367
                        sti();
2368
                        p_cuser = (struct serial_icounter_struct *) arg;
2369
                        put_user(cnow.cts, &p_cuser->cts);
2370
                        put_user(cnow.dsr, &p_cuser->dsr);
2371
                        put_user(cnow.rng, &p_cuser->rng);
2372
                        put_user(cnow.dcd, &p_cuser->dcd);
2373
                        return 0;
2374
 
2375
                default:
2376
                        return -ENOIOCTLCMD;
2377
                }
2378
        return 0;
2379
}
2380
 
2381
static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
2382
{
2383
        struct async_struct *info = (struct async_struct *)tty->driver_data;
2384
 
2385
        if (   (tty->termios->c_cflag == old_termios->c_cflag)
2386
            && (   RELEVANT_IFLAG(tty->termios->c_iflag)
2387
                == RELEVANT_IFLAG(old_termios->c_iflag)))
2388
          return;
2389
 
2390
        change_speed(info);
2391
 
2392
        if ((old_termios->c_cflag & CRTSCTS) &&
2393
            !(tty->termios->c_cflag & CRTSCTS)) {
2394
                tty->hw_stopped = 0;
2395
                rs_start(tty);
2396
        }
2397
 
2398
#if 0
2399
        /*
2400
         * No need to wake up processes in open wait, since they
2401
         * sample the CLOCAL flag once, and don't recheck it.
2402
         * XXX  It's not clear whether the current behavior is correct
2403
         * or not.  Hence, this may change.....
2404
         */
2405
        if (!(old_termios->c_cflag & CLOCAL) &&
2406
            (tty->termios->c_cflag & CLOCAL))
2407
                wake_up_interruptible(&info->open_wait);
2408
#endif
2409
}
2410
 
2411
/*
2412
 * ------------------------------------------------------------
2413
 * rs_close()
2414
 *
2415
 * This routine is called when the serial port gets closed.  First, we
2416
 * wait for the last remaining data to be sent.  Then, we unlink its
2417
 * async structure from the interrupt chain if necessary, and we free
2418
 * that IRQ if nothing is left in the chain.
2419
 * ------------------------------------------------------------
2420
 */
2421
static void rs_close(struct tty_struct *tty, struct file * filp)
2422
{
2423
        struct async_struct * info = (struct async_struct *)tty->driver_data;
2424
        unsigned long flags;
2425
        unsigned long timeout;
2426
 
2427
        if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
2428
                return;
2429
 
2430
        save_flags(flags); cli();
2431
 
2432
        if (tty_hung_up_p(filp)) {
2433
                DBG_CNT("before DEC-hung");
2434
                MOD_DEC_USE_COUNT;
2435
                restore_flags(flags);
2436
                return;
2437
        }
2438
 
2439
#ifdef SERIAL_DEBUG_OPEN
2440
        printk("rs_close ttys%d, count = %d\n", info->line, info->count);
2441
#endif
2442
        if ((tty->count == 1) && (info->count != 1)) {
2443
                /*
2444
                 * Uh, oh.  tty->count is 1, which means that the tty
2445
                 * structure will be freed.  Info->count should always
2446
                 * be one in these conditions.  If it's greater than
2447
                 * one, we've got real problems, since it means the
2448
                 * serial port won't be shutdown.
2449
                 */
2450
                printk("rs_close: bad serial port count; tty->count is 1, "
2451
                       "info->count is %d\n", info->count);
2452
                info->count = 1;
2453
        }
2454
        if (--info->count < 0) {
2455
                printk("rs_close: bad serial port count for ttys%d: %d\n",
2456
                       info->line, info->count);
2457
                info->count = 0;
2458
        }
2459
        if (info->count) {
2460
                DBG_CNT("before DEC-2");
2461
                MOD_DEC_USE_COUNT;
2462
                restore_flags(flags);
2463
                return;
2464
        }
2465
        info->flags |= ASYNC_CLOSING;
2466
        /*
2467
         * Save the termios structure, since this port may have
2468
         * separate termios for callout and dialin.
2469
         */
2470
        if (info->flags & ASYNC_NORMAL_ACTIVE)
2471
                info->normal_termios = *tty->termios;
2472
        if (info->flags & ASYNC_CALLOUT_ACTIVE)
2473
                info->callout_termios = *tty->termios;
2474
        /*
2475
         * Now we wait for the transmit buffer to clear; and we notify
2476
         * the line discipline to only process XON/XOFF characters.
2477
         */
2478
        tty->closing = 1;
2479
        if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2480
                tty_wait_until_sent(tty, info->closing_wait);
2481
        /*
2482
         * At this point we stop accepting input.  To do this, we
2483
         * disable the receive line status interrupts, and tell the
2484
         * interrupt driver to stop checking the data ready bit in the
2485
         * line status register.
2486
         */
2487
        info->IER &= ~UART_IER_RLSI;
2488
        info->read_status_mask &= ~UART_LSR_DR;
2489
        if (info->flags & ASYNC_INITIALIZED) {
2490
                serial_out(info, UART_IER, info->IER);
2491
                /*
2492
                 * Before we drop DTR, make sure the UART transmitter
2493
                 * has completely drained; this is especially
2494
                 * important if there is a transmit FIFO!
2495
                 */
2496
                timeout = jiffies+HZ;
2497
                while (!(serial_inp(info, UART_LSR) & UART_LSR_TEMT)) {
2498
                        current->state = TASK_INTERRUPTIBLE;
2499
                        current->timeout = jiffies + info->timeout;
2500
                        schedule();
2501
                        if (jiffies > timeout)
2502
                                break;
2503
                }
2504
        }
2505
        shutdown(info);
2506
        if (tty->driver.flush_buffer)
2507
                tty->driver.flush_buffer(tty);
2508
        if (tty->ldisc.flush_buffer)
2509
                tty->ldisc.flush_buffer(tty);
2510
        tty->closing = 0;
2511
        info->event = 0;
2512
        info->tty = 0;
2513
        if (info->blocked_open) {
2514
                if (info->close_delay) {
2515
                        current->state = TASK_INTERRUPTIBLE;
2516
                        current->timeout = jiffies + info->close_delay;
2517
                        schedule();
2518
                }
2519
                wake_up_interruptible(&info->open_wait);
2520
        }
2521
        info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2522
                         ASYNC_CLOSING);
2523
        wake_up_interruptible(&info->close_wait);
2524
        MOD_DEC_USE_COUNT;
2525
        restore_flags(flags);
2526
}
2527
 
2528
/*
2529
 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2530
 */
2531
void rs_hangup(struct tty_struct *tty)
2532
{
2533
        struct async_struct * info = (struct async_struct *)tty->driver_data;
2534
 
2535
        if (serial_paranoia_check(info, tty->device, "rs_hangup"))
2536
                return;
2537
 
2538
        rs_flush_buffer(tty);
2539
        shutdown(info);
2540
        info->event = 0;
2541
        info->count = 0;
2542
        info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2543
        info->tty = 0;
2544
        wake_up_interruptible(&info->open_wait);
2545
}
2546
 
2547
/*
2548
 * ------------------------------------------------------------
2549
 * rs_open() and friends
2550
 * ------------------------------------------------------------
2551
 */
2552
static int block_til_ready(struct tty_struct *tty, struct file * filp,
2553
                           struct async_struct *info)
2554
{
2555
        struct wait_queue wait = { current, NULL };
2556
        int             retval;
2557
        int             do_clocal = 0;
2558
 
2559
        /*
2560
         * If the device is in the middle of being closed, then block
2561
         * until it's done, and then try again.
2562
         */
2563
        if (tty_hung_up_p(filp) ||
2564
            (info->flags & ASYNC_CLOSING)) {
2565
                if (info->flags & ASYNC_CLOSING)
2566
                        interruptible_sleep_on(&info->close_wait);
2567
#ifdef SERIAL_DO_RESTART
2568
                if (info->flags & ASYNC_HUP_NOTIFY)
2569
                        return -EAGAIN;
2570
                else
2571
                        return -ERESTARTSYS;
2572
#else
2573
                return -EAGAIN;
2574
#endif
2575
        }
2576
 
2577
        /*
2578
         * If this is a callout device, then just make sure the normal
2579
         * device isn't being used.
2580
         */
2581
        if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2582
                if (info->flags & ASYNC_NORMAL_ACTIVE)
2583
                        return -EBUSY;
2584
                if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2585
                    (info->flags & ASYNC_SESSION_LOCKOUT) &&
2586
                    (info->session != current->session))
2587
                    return -EBUSY;
2588
                if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2589
                    (info->flags & ASYNC_PGRP_LOCKOUT) &&
2590
                    (info->pgrp != current->pgrp))
2591
                    return -EBUSY;
2592
                info->flags |= ASYNC_CALLOUT_ACTIVE;
2593
                return 0;
2594
        }
2595
 
2596
        /*
2597
         * If non-blocking mode is set, or the port is not enabled,
2598
         * then make the check up front and then exit.
2599
         */
2600
        if ((filp->f_flags & O_NONBLOCK) ||
2601
            (tty->flags & (1 << TTY_IO_ERROR))) {
2602
                if (info->flags & ASYNC_CALLOUT_ACTIVE)
2603
                        return -EBUSY;
2604
                info->flags |= ASYNC_NORMAL_ACTIVE;
2605
                return 0;
2606
        }
2607
 
2608
        if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2609
                if (info->normal_termios.c_cflag & CLOCAL)
2610
                        do_clocal = 1;
2611
        } else {
2612
                if (tty->termios->c_cflag & CLOCAL)
2613
                        do_clocal = 1;
2614
        }
2615
 
2616
        /*
2617
         * Block waiting for the carrier detect and the line to become
2618
         * free (i.e., not in use by the callout).  While we are in
2619
         * this loop, info->count is dropped by one, so that
2620
         * rs_close() knows when to free things.  We restore it upon
2621
         * exit, either normal or abnormal.
2622
         */
2623
        retval = 0;
2624
        add_wait_queue(&info->open_wait, &wait);
2625
#ifdef SERIAL_DEBUG_OPEN
2626
        printk("block_til_ready before block: ttys%d, count = %d\n",
2627
               info->line, info->count);
2628
#endif
2629
        cli();
2630
        if (!tty_hung_up_p(filp))
2631
                info->count--;
2632
        sti();
2633
        info->blocked_open++;
2634
        while (1) {
2635
                cli();
2636
                if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
2637
                        serial_out(info, UART_MCR,
2638
                                   serial_inp(info, UART_MCR) |
2639
                                   (UART_MCR_DTR | UART_MCR_RTS));
2640
                sti();
2641
                current->state = TASK_INTERRUPTIBLE;
2642
                if (tty_hung_up_p(filp) ||
2643
                    !(info->flags & ASYNC_INITIALIZED)) {
2644
#ifdef SERIAL_DO_RESTART
2645
                        if (info->flags & ASYNC_HUP_NOTIFY)
2646
                                retval = -EAGAIN;
2647
                        else
2648
                                retval = -ERESTARTSYS;
2649
#else
2650
                        retval = -EAGAIN;
2651
#endif
2652
                        break;
2653
                }
2654
                if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2655
                    !(info->flags & ASYNC_CLOSING) &&
2656
                    (do_clocal || (serial_in(info, UART_MSR) &
2657
                                   UART_MSR_DCD)))
2658
                        break;
2659
                if (current->signal & ~current->blocked) {
2660
                        retval = -ERESTARTSYS;
2661
                        break;
2662
                }
2663
#ifdef SERIAL_DEBUG_OPEN
2664
                printk("block_til_ready blocking: ttys%d, count = %d\n",
2665
                       info->line, info->count);
2666
#endif
2667
                schedule();
2668
        }
2669
        current->state = TASK_RUNNING;
2670
        remove_wait_queue(&info->open_wait, &wait);
2671
        if (!tty_hung_up_p(filp))
2672
                info->count++;
2673
        info->blocked_open--;
2674
#ifdef SERIAL_DEBUG_OPEN
2675
        printk("block_til_ready after blocking: ttys%d, count = %d\n",
2676
               info->line, info->count);
2677
#endif
2678
        if (retval)
2679
                return retval;
2680
        info->flags |= ASYNC_NORMAL_ACTIVE;
2681
        return 0;
2682
}
2683
 
2684
/*
2685
 * This routine is called whenever a serial port is opened.  It
2686
 * enables interrupts for a serial port, linking in its async structure into
2687
 * the IRQ chain.   It also performs the serial-specific
2688
 * initialization for the tty structure.
2689
 */
2690
int rs_open(struct tty_struct *tty, struct file * filp)
2691
{
2692
        struct async_struct     *info;
2693
        int                     retval, line;
2694
        unsigned long           page;
2695
 
2696
        line = MINOR(tty->device) - tty->driver.minor_start;
2697
        if ((line < 0) || (line >= NR_PORTS))
2698
                return -ENODEV;
2699
        info = rs_table + line;
2700
        if (serial_paranoia_check(info, tty->device, "rs_open"))
2701
                return -ENODEV;
2702
 
2703
#ifdef SERIAL_DEBUG_OPEN
2704
        printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2705
               info->count);
2706
#endif
2707
        info->count++;
2708
        tty->driver_data = info;
2709
        info->tty = tty;
2710
 
2711
        if (!tmp_buf) {
2712
                page = get_free_page(GFP_KERNEL);
2713
                if (!page)
2714
                        return -ENOMEM;
2715
                if (tmp_buf)
2716
                        free_page(page);
2717
                else
2718
                        tmp_buf = (unsigned char *) page;
2719
        }
2720
 
2721
        /*
2722
         * Start up serial port
2723
         */
2724
        retval = startup(info);
2725
        if (retval)
2726
                return retval;
2727
 
2728
        MOD_INC_USE_COUNT;
2729
        retval = block_til_ready(tty, filp, info);
2730
        if (retval) {
2731
#ifdef SERIAL_DEBUG_OPEN
2732
                printk("rs_open returning after block_til_ready with %d\n",
2733
                       retval);
2734
#endif
2735
                return retval;
2736
        }
2737
 
2738
        if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2739
                if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2740
                        *tty->termios = info->normal_termios;
2741
                else
2742
                        *tty->termios = info->callout_termios;
2743
                change_speed(info);
2744
        }
2745
 
2746
        info->session = current->session;
2747
        info->pgrp = current->pgrp;
2748
 
2749
#ifdef SERIAL_DEBUG_OPEN
2750
        printk("rs_open ttys%d successful...", info->line);
2751
#endif
2752
        return 0;
2753
}
2754
 
2755
/*
2756
 * ---------------------------------------------------------------------
2757
 * rs_init() and friends
2758
 *
2759
 * rs_init() is called at boot-time to initialize the serial driver.
2760
 * ---------------------------------------------------------------------
2761
 */
2762
 
2763
/*
2764
 * This routine prints out the appropriate serial driver version
2765
 * number, and identifies which options were configured into this
2766
 * driver.
2767
 */
2768
static void show_serial_version(void)
2769
{
2770
        printk(KERN_INFO "%s version %s with", serial_name, serial_version);
2771
#ifdef CONFIG_SERIAL_PCI
2772
  printk(" PCI");
2773
#define SERIAL_OPT
2774
#endif
2775
#ifdef CONFIG_HUB6
2776
        printk(" HUB-6");
2777
#define SERIAL_OPT
2778
#endif
2779
#ifdef SERIAL_OPT
2780
        printk(" enabled\n");
2781
#else
2782
        printk(" no serial options enabled\n");
2783
#endif
2784
#undef SERIAL_OPT
2785
}
2786
 
2787
#ifndef CONFIG_COLDFIRE
2788
/*
2789
 * This routine is called by do_auto_irq(); it attempts to determine
2790
 * which interrupt a serial port is configured to use.  It is not
2791
 * fool-proof, but it works a large part of the time.
2792
 */
2793
static int get_auto_irq(struct async_struct *info)
2794
{
2795
        unsigned char save_MCR, save_IER, save_ICP=0;
2796
        unsigned int ICP=0, port = info->port;
2797
        unsigned long timeout;
2798
 
2799
        /*
2800
         * Enable interrupts and see who answers
2801
         */
2802
        rs_irq_triggered = 0;
2803
        cli();
2804
        save_IER = serial_inp(info, UART_IER);
2805
        save_MCR = serial_inp(info, UART_MCR);
2806
        if (info->flags & ASYNC_FOURPORT)  {
2807
                serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2808
                serial_outp(info, UART_IER, 0x0f);      /* enable all intrs */
2809
                ICP = (port & 0xFE0) | 0x01F;
2810
                save_ICP = inb_p(ICP);
2811
                outb_p(0x80, ICP);
2812
                (void) inb_p(ICP);
2813
        } else {
2814
                serial_outp(info, UART_MCR,
2815
                            UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
2816
                serial_outp(info, UART_IER, 0x0f);      /* enable all intrs */
2817
        }
2818
        sti();
2819
        /*
2820
         * Next, clear the interrupt registers.
2821
         */
2822
        (void)serial_inp(info, UART_LSR);
2823
        (void)serial_inp(info, UART_RX);
2824
        (void)serial_inp(info, UART_IIR);
2825
        (void)serial_inp(info, UART_MSR);
2826
 
2827
        timeout = jiffies+2*HZ/100;
2828
        while (timeout >= jiffies) {
2829
                if (rs_irq_triggered)
2830
                        break;
2831
        }
2832
        /*
2833
         * Now check to see if we got any business, and clean up.
2834
         */
2835
        cli();
2836
        serial_outp(info, UART_IER, save_IER);
2837
        serial_outp(info, UART_MCR, save_MCR);
2838
        if (info->flags & ASYNC_FOURPORT)
2839
                outb_p(save_ICP, ICP);
2840
        sti();
2841
        return(rs_irq_triggered);
2842
}
2843
 
2844
/*
2845
 * Calls get_auto_irq() multiple times, to make sure we don't get
2846
 * faked out by random interrupts
2847
 */
2848
static int do_auto_irq(struct async_struct * info)
2849
{
2850
        unsigned                port = info->port;
2851
        int                     irq_lines = 0;
2852
        int                     irq_try_1 = 0, irq_try_2 = 0;
2853
        int                     retries;
2854
        unsigned long flags;
2855
 
2856
        if (!port)
2857
                return 0;
2858
 
2859
        /* Turn on interrupts (they may be off) */
2860
        save_flags(flags); sti();
2861
 
2862
        irq_lines = grab_all_interrupts(rs_wild_int_mask);
2863
 
2864
        for (retries = 0; retries < 5; retries++) {
2865
                if (!irq_try_1)
2866
                        irq_try_1 = get_auto_irq(info);
2867
                if (!irq_try_2)
2868
                        irq_try_2 = get_auto_irq(info);
2869
                if (irq_try_1 && irq_try_2) {
2870
                        if (irq_try_1 == irq_try_2)
2871
                                break;
2872
                        irq_try_1 = irq_try_2 = 0;
2873
                }
2874
        }
2875
        restore_flags(flags);
2876
        free_all_interrupts(irq_lines);
2877
        return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
2878
}
2879
#endif /* CONFIG_COLDFIRE */
2880
 
2881
/*
2882
 * This routine is called by rs_init() to initialize a specific serial
2883
 * port.  It determines what type of UART chip this serial port is
2884
 * using: 8250, 16450, 16550, 16550A.  The important question is
2885
 * whether or not this UART is a 16550A or not, since this will
2886
 * determine whether or not we can use its FIFO features or not.
2887
 */
2888
static void autoconfig(struct async_struct * info)
2889
{
2890
        unsigned char status1, status2, scratch, scratch2;
2891
        unsigned port = info->port;
2892
        unsigned long flags;
2893
 
2894
        info->type = PORT_UNKNOWN;
2895
 
2896
        if (!port)
2897
                return;
2898
 
2899
        save_flags(flags); cli();
2900
 
2901
        /*
2902
         * Do a simple existence test first; if we fail this, there's
2903
         * no point trying anything else.
2904
         *
2905
         * 0x80 is used as a nonsense port to prevent against false
2906
         * positives due to ISA bus float.  The assumption is that
2907
         * 0x80 is a non-existent port; which should be safe since
2908
         * include/asm/io.h also makes this assumption.
2909
         */
2910
        scratch = serial_inp(info, UART_IER);
2911
        serial_outp(info, UART_IER, 0);
2912
#ifndef CONFIG_COLDFIRE
2913
        outb(0xff, 0x080);
2914
#endif
2915
        scratch2 = serial_inp(info, UART_IER);
2916
        serial_outp(info, UART_IER, scratch);
2917
        if (scratch2) {
2918
                restore_flags(flags);
2919
                return;         /* We failed; there's nothing here */
2920
        }
2921
 
2922
        /*
2923
         * Check to see if a UART is really there.  Certain broken
2924
         * internal modems based on the Rockwell chipset fail this
2925
         * test, because they apparently don't implement the loopback
2926
         * test mode.  So this test is skipped on the COM 1 through
2927
         * COM 4 ports.  This *should* be safe, since no board
2928
         * manufacturer would be stupid enough to design a board
2929
         * that conflicts with COM 1-4 --- we hope!
2930
         */
2931
        if (!(info->flags & ASYNC_SKIP_TEST)) {
2932
                scratch = serial_inp(info, UART_MCR);
2933
                serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2934
                scratch2 = serial_inp(info, UART_MSR);
2935
                serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2936
                status1 = serial_inp(info, UART_MSR) & 0xF0;
2937
                serial_outp(info, UART_MCR, scratch);
2938
                serial_outp(info, UART_MSR, scratch2);
2939
                if (status1 != 0x90) {
2940
                        restore_flags(flags);
2941
                        return;
2942
                }
2943
        }
2944
 
2945
        /*
2946
         * If the AUTO_IRQ flag is set, try to do the automatic IRQ
2947
         * detection.
2948
         */
2949
        if (info->flags & ASYNC_AUTO_IRQ)
2950
#ifdef CONFIG_COLDFIRE
2951
                printk("SERIAL: auto-irq not supported\n");
2952
#else
2953
                info->irq = do_auto_irq(info);
2954
#endif
2955
 
2956
        scratch2 = serial_in(info, UART_LCR);
2957
        serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2958
        serial_outp(info, UART_EFR, 0);  /* EFR is the same as FCR */
2959
        serial_outp(info, UART_LCR, scratch2);
2960
        serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2961
        scratch = serial_in(info, UART_IIR) >> 6;
2962
        info->xmit_fifo_size = 1;
2963
        switch (scratch) {
2964
                case 0:
2965
                        info->type = PORT_16450;
2966
                        break;
2967
                case 1:
2968
                        info->type = PORT_UNKNOWN;
2969
                        break;
2970
                case 2:
2971
                        info->type = PORT_16550;
2972
                        break;
2973
                case 3:
2974
                        serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2975
                        if (serial_in(info, UART_EFR) == 0) {
2976
                                info->type = PORT_16650;
2977
                                info->xmit_fifo_size = 32;
2978
                        } else {
2979
                                info->type = PORT_16550A;
2980
                                info->xmit_fifo_size = 16;
2981
                        }
2982
                        serial_outp(info, UART_LCR, scratch2);
2983
                        break;
2984
        }
2985
        if (info->type == PORT_16450) {
2986
                scratch = serial_in(info, UART_SCR);
2987
                serial_outp(info, UART_SCR, 0xa5);
2988
                status1 = serial_in(info, UART_SCR);
2989
                serial_outp(info, UART_SCR, 0x5a);
2990
                status2 = serial_in(info, UART_SCR);
2991
                serial_outp(info, UART_SCR, scratch);
2992
 
2993
                if ((status1 != 0xa5) || (status2 != 0x5a)) {
2994
                        info->type = PORT_8250;
2995
                }
2996
        }
2997
        request_region(info->port,8,"serial(auto)");
2998
 
2999
        /*
3000
         * Reset the UART.
3001
         */
3002
#if defined(__alpha__) && !defined(CONFIG_PCI)
3003
        /*
3004
         * I wonder what DEC did to the OUT1 and OUT2 lines?
3005
         * clearing them results in endless interrupts.
3006
         */
3007
        serial_outp(info, UART_MCR, 0x0c);
3008
#else
3009
        serial_outp(info, UART_MCR, 0x00);
3010
#endif
3011
        serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
3012
                                     UART_FCR_CLEAR_XMIT));
3013
        (void)serial_in(info, UART_RX);
3014
 
3015
        restore_flags(flags);
3016
}
3017
 
3018
void display_uart_type(int type)
3019
{
3020
  switch (type) {
3021
        case PORT_8250:
3022
          printk(" is a 8250\n");
3023
          break;
3024
        case PORT_16450:
3025
          printk(" is a 16450\n");
3026
          break;
3027
        case PORT_16550:
3028
          printk(" is a 16550\n");
3029
          break;
3030
        case PORT_16550A:
3031
          printk(" is a 16550A\n");
3032
          break;
3033
        case PORT_16650:
3034
          printk(" is a 16650\n");
3035
          break;
3036
        default:
3037
          printk("\n");
3038
          break;
3039
  }
3040
}
3041
 
3042
void init_port(struct async_struct *info, int num)
3043
{
3044
  info->magic = SERIAL_MAGIC;
3045
  info->line = num;
3046
  info->tty = 0;
3047
  info->type = PORT_UNKNOWN;
3048
  info->custom_divisor = 0;
3049
  info->close_delay = 5*HZ/10;
3050
  info->closing_wait = 30*HZ;
3051
  info->x_char = 0;
3052
  info->event = 0;
3053
  info->count = 0;
3054
  info->blocked_open = 0;
3055
  info->tqueue.routine = do_softint;
3056
  info->tqueue.data = info;
3057
  info->tqueue_hangup.routine = do_serial_hangup;
3058
  info->tqueue_hangup.data = info;
3059
  info->callout_termios =callout_driver.init_termios;
3060
  info->normal_termios = serial_driver.init_termios;
3061
  info->open_wait = 0;
3062
  info->close_wait = 0;
3063
  info->delta_msr_wait = 0;
3064
  info->icount.cts = info->icount.dsr =
3065
        info->icount.rng = info->icount.dcd = 0;
3066
  info->next_port = 0;
3067
  info->prev_port = 0;
3068
#ifdef CONFIG_COLDFIRE
3069
{
3070
  int retval;
3071
#if defined(CONFIG_NETtel)
3072
  mcf_autovector(info->irq);
3073
#endif
3074
  retval = request_irq(info->irq, rs_interrupt, IRQ_T(info), "serial", NULL);
3075
  if (retval)
3076
    printk("SERIAL: failed to register interrup %d\n", info->irq);
3077
}
3078
#else
3079
/* SIMON */
3080
/*  if (info->irq == 2)
3081
        info->irq = 9;
3082
*/
3083
#endif
3084
 
3085
  if (info->type == PORT_UNKNOWN) {
3086
        if (!(info->flags & ASYNC_BOOT_AUTOCONF))
3087
          return;
3088
        autoconfig(info);
3089
        if (info->type == PORT_UNKNOWN)
3090
          return;
3091
  }
3092
  printk(KERN_INFO "ttyS%02d%s%s at 0x%04x (irq = %d)", info->line,
3093
                 (info->flags & ASYNC_FOURPORT) ? " FourPort" : "",
3094
                 (info->flags & ASYNC_PCI)      ? " PCI" : "",
3095
                 info->port, info->irq);
3096
  display_uart_type(info->type);
3097
}
3098
 
3099
int register_serial(struct serial_struct *req);
3100
void unregister_serial(int line);
3101
 
3102
static struct symbol_table serial_syms = {
3103
#include <linux/symtab_begin.h>
3104
        X(register_serial),
3105
        X(unregister_serial),
3106
#include <linux/symtab_end.h>
3107
};
3108
 
3109
#ifdef CONFIG_SERIAL_PCI
3110
 
3111
/*
3112
 * Query PCI space for known serial boards
3113
 * If found, add them to the PCI device space in rs_table[]
3114
 *
3115
 * Accept a maximum of eight boards
3116
 *
3117
 */
3118
 
3119
static void probe_serial_pci(void)
3120
{
3121
  u16 vendor, device;
3122
  static int pci_index  = 0;
3123
  unsigned char pci_bus, pci_device_fn;
3124
  struct async_struct *pci_boards = &rs_table[PCI_PORT_START];
3125
  unsigned int port_num = 0;
3126
  unsigned int card_num = 0;
3127
 
3128
  u32 device_ioaddr;
3129
  u8  device_irq;
3130
 
3131
  enum pci_spc pci_space        = pci_space_0;
3132
  unsigned int pci_space_offset = 0;
3133
 
3134
 
3135
#ifdef SERIAL_DEBUG_PCI
3136
  printk(KERN_DEBUG "Entered probe_serial_pci()\n");
3137
#endif
3138
 
3139
  if (! pcibios_present()) {
3140
#ifdef SERIAL_DEBUG_PCI
3141
        printk(KERN_DEBUG "Leaving probe_serial_pci() (no pcibios)\n");
3142
#endif
3143
        return;
3144
  }
3145
 
3146
/*
3147
 * Start scanning the PCI bus for serial controllers ...
3148
 *
3149
 */
3150
 
3151
  for (;pci_index < 0xff; pci_index++) {
3152
        int i = 0;
3153
 
3154
        if (pcibios_find_class(PCI_CLASS_COMMUNICATION_SERIAL << 8,
3155
                                                   pci_index,
3156
                                                   &pci_bus,
3157
                                                   &pci_device_fn) != PCIBIOS_SUCCESSFUL)
3158
          break; /* for (; pci_index ... */
3159
 
3160
        pcibios_read_config_word(pci_bus, pci_device_fn, PCI_VENDOR_ID, &vendor);
3161
        pcibios_read_config_word(pci_bus, pci_device_fn, PCI_DEVICE_ID, &device);
3162
 
3163
        for (i = 0; pci_serial_tbl[i].board_name; i++) {
3164
          if (vendor == pci_serial_tbl[i].vendor_id  &&
3165
                  device == pci_serial_tbl[i].device_id)
3166
                break; /* for(i=0... */
3167
        }
3168
 
3169
        if (pci_serial_tbl[i].board_name == 0) {
3170
#ifdef SERIAL_DEBUG_PCI
3171
          printk(KERN_DEBUG "Found Board (%x/%x) (not one of us)\n", vendor, device);
3172
#endif
3173
          continue;        /* Found a serial communication controller but not one we know */
3174
        }
3175
 
3176
/*
3177
 * At this point we found a serial board which we know
3178
 */
3179
 
3180
        if(card_num >= PCI_NR_BOARDS) {
3181
          printk(KERN_ERR "Already %d boards configured, skipping\n", PCI_NR_BOARDS);
3182
          continue; /* for (;pci_index < 0xff */
3183
        }
3184
 
3185
        pcibios_read_config_byte(pci_bus, pci_device_fn,
3186
                                                         PCI_INTERRUPT_LINE, &device_irq);
3187
        pcibios_read_config_dword(pci_bus, pci_device_fn,
3188
                                                          PCI_BASE_ADDRESS_1, &device_ioaddr);
3189
 
3190
#ifdef SERIAL_DEBUG_PCI
3191
                printk(KERN_DEBUG "Device %s at #%x found\n", pci_serial_tbl[i].board_name, device_ioaddr);
3192
#endif
3193
 
3194
        if (check_region(device_ioaddr, pci_serial_tbl[i].io_size)) {
3195
          printk(KERN_ERR "Could not reserve %d bytes of I/O Space at %x\n", pci_serial_tbl[i].io_size, device_ioaddr);
3196
          continue; /* for (;pci_index < 0xff */
3197
        }
3198
 
3199
/*
3200
 * Every PCI device brings 128 bytes (at least) of IO-Space with it
3201
 * reserve a region for it. It is not exactly necessary as PCI will
3202
 * ensure that no other device will be mapped onto this space (LOL)
3203
 * but we do it nevertheless so it will show up nicely on
3204
 * /proc/ioports -- hps
3205
 */
3206
 
3207
          if((device_ioaddr & 1) == 0) {
3208
#ifdef SERIAL_DEBUG_PCI
3209
                device_ioaddr &= ~0x7f;
3210
                printk(KERN_DEBUG "%s has its config registers memory-mapped at #%x (ignoring)\n",
3211
                           pci_serial_tbl[i].board_name, device_ioaddr);
3212
#endif
3213
                continue; /* for (;pci_index < 0xff */
3214
          }
3215
 
3216
        device_ioaddr &= ~0x7f;         /* Mask out the flag bits
3217
                                                                         * from this register. At least on the PLX9050
3218
                                                                         * they're always 0 but this is here nevertheless
3219
                                                                         * for sanity's sake
3220
                                                                         */
3221
 
3222
        request_region(device_ioaddr, pci_serial_tbl[i].io_size, "serial (PCI Controller)");
3223
 
3224
        pci_rs_chips[card_num].start = device_ioaddr;
3225
        pci_rs_chips[card_num].type  = &pci_serial_tbl[i];
3226
 
3227
 
3228
/*
3229
 * Every PCI device can bring up to four PCI memory or IO spaces (at
3230
 * least according to the documentation I have. So we will now check
3231
 * with our config whether this device has one of these spaces and we
3232
 * should configure UARTs inside -- hps
3233
 */
3234
 
3235
        for(; pci_space <= pci_space_3; pci_space <<= 1, pci_space_offset+= 4) {
3236
          u32 uart_chip_base;
3237
          u32 uart_chip_count;
3238
 
3239
          if((pci_serial_tbl[i].pci_space & pci_space) == 0)
3240
                continue; /* for(;pci_space... */
3241
 
3242
          pcibios_read_config_dword(pci_bus, pci_device_fn,
3243
                                                                PCI_BASE_ADDRESS_2+pci_space_offset, &uart_chip_base);
3244
 
3245
          if((uart_chip_base & 1) == 0) {
3246
#ifdef SERIAL_DEBUG_PCI
3247
                chip_base &= ~0x0f;
3248
                printk(KERN_DEBUG "%s has a memory-mapped IO Chip at #%x (ignoring)\n",
3249
                           pci_serial_tbl[i].board_name, chip_base);
3250
#endif
3251
                continue; /* for(;pci_space... */
3252
          }
3253
 
3254
          uart_chip_base &= ~0x0f;
3255
 
3256
/*
3257
 * uart_chip_base now points to the IO-Space.
3258
 *
3259
 * Alvin Sim <alvin@alloycp.com.au> told me the following thing:
3260
 *
3261
 * UARTS can be "setserial"d by kernel 2.0.35, but ports needed to be
3262
 * manually specified. 4 ports start at 0x6100, in increments of 8
3263
 * addresses.
3264
 *
3265
 * so there is at least one board out there which can do more than one
3266
 * UART in a single PCI config space. My trustworthy SPCom 200 PCI has
3267
 * just one UART in one config space. So I added a check for more than
3268
 * one chip in a config space -- hps
3269
 *
3270
 */
3271
 
3272
          for(uart_chip_count=0;uart_chip_count < pci_serial_tbl[i].dev_per_space; uart_chip_count++) {
3273
#ifdef SERIAL_DEBUG_PCI
3274
                printk(KERN_DEBUG "%s has an IO Chip at #%x\n",
3275
                           pci_serial_tbl[i].board_name, uart_chip_base);
3276
#endif
3277
 
3278
                if(port_num >= PCI_NR_PORTS) {
3279
                  printk(KERN_ERR "Already %d ports configured, skipping\n", PCI_NR_PORTS);
3280
                  break; /* for(;uart_chip_count... */
3281
                }
3282
 
3283
                if (check_region(uart_chip_base, 8)) {
3284
                  printk(KERN_ERR "Could not reserve %d bytes of I/O Space at %x\n", 8, uart_chip_base);
3285
                  break; /* for(;uart_chip_count... */
3286
                }
3287
 
3288
                request_region(uart_chip_base, 8, "serial (PCI)");
3289
                pci_boards[port_num].port  = uart_chip_base;
3290
                pci_boards[port_num].irq   = device_irq;
3291
                pci_boards[port_num].flags = PCI_FLAGS;
3292
                pci_boards[port_num].baud_base = pci_serial_tbl[i].baud_base;
3293
 
3294
                port_num++;
3295
                uart_chip_base += pci_serial_tbl[i].dev_spacing;
3296
 
3297
          }  /* for(uart_chip_count... */
3298
        } /* for(pci_space ... */
3299
 
3300
        card_num++;
3301
  }  /* for */
3302
 
3303
#ifdef SERIAL_DEBUG_PCI
3304
  printk(KERN_DEBUG "Leaving probe_serial_pci() (probe finished)\n");
3305
#endif
3306
  return;
3307
}
3308
 
3309
#endif /* CONFIG_SERIAL_PCI */
3310
 
3311
/*
3312
 * The serial driver boot-time initialization code!
3313
 */
3314
 
3315
int rs_init(void)
3316
{
3317
        int i;
3318
        struct async_struct * info;
3319
 
3320
        init_bh(SERIAL_BH, do_serial_bh);
3321
        timer_table[RS_TIMER].fn = rs_timer;
3322
        timer_table[RS_TIMER].expires = 0;
3323
#ifdef CONFIG_AUTO_IRQ
3324
        rs_wild_int_mask = check_wild_interrupts(1);
3325
#endif
3326
 
3327
        for (i = 0; i < 16; i++) {
3328
                IRQ_ports[IRQMASK(i)] = 0;
3329
                IRQ_timeout[IRQMASK(i)] = 0;
3330
                memset(&rs_multiport[IRQMASK(i)], 0, sizeof(struct rs_multiport_struct));
3331
        }
3332
 
3333
        show_serial_version();
3334
#ifdef CONFIG_SERIAL_PCI
3335
                probe_serial_pci();
3336
#endif  
3337
 
3338
        /* Initialize the tty_driver structure */
3339
 
3340
        memset(&serial_driver, 0, sizeof(struct tty_driver));
3341
        serial_driver.magic = TTY_DRIVER_MAGIC;
3342
        serial_driver.name = "ttyS";
3343
        serial_driver.major = TTY_MAJOR;
3344
        serial_driver.minor_start = 64;
3345
        serial_driver.num = NR_PORTS;
3346
        serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
3347
        serial_driver.subtype = SERIAL_TYPE_NORMAL;
3348
        serial_driver.init_termios = tty_std_termios;
3349
        serial_driver.init_termios.c_cflag =
3350
                B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3351
        serial_driver.flags = TTY_DRIVER_REAL_RAW;
3352
        serial_driver.refcount = &serial_refcount;
3353
        serial_driver.table = serial_table;
3354
        serial_driver.termios = serial_termios;
3355
        serial_driver.termios_locked = serial_termios_locked;
3356
 
3357
        serial_driver.open = rs_open;
3358
        serial_driver.close = rs_close;
3359
        serial_driver.write = rs_write;
3360
        serial_driver.put_char = rs_put_char;
3361
        serial_driver.flush_chars = rs_flush_chars;
3362
        serial_driver.write_room = rs_write_room;
3363
        serial_driver.chars_in_buffer = rs_chars_in_buffer;
3364
        serial_driver.flush_buffer = rs_flush_buffer;
3365
        serial_driver.ioctl = rs_ioctl;
3366
        serial_driver.throttle = rs_throttle;
3367
        serial_driver.unthrottle = rs_unthrottle;
3368
        serial_driver.set_termios = rs_set_termios;
3369
        serial_driver.stop = rs_stop;
3370
        serial_driver.start = rs_start;
3371
        serial_driver.hangup = rs_hangup;
3372
 
3373
        /*
3374
         * The callout device is just like normal device except for
3375
         * major number and the subtype code.
3376
         */
3377
        callout_driver = serial_driver;
3378
        callout_driver.name = "cua";
3379
        callout_driver.major = TTYAUX_MAJOR;
3380
        callout_driver.subtype = SERIAL_TYPE_CALLOUT;
3381
 
3382
        if (tty_register_driver(&serial_driver))
3383
                panic("Couldn't register serial driver\n");
3384
        if (tty_register_driver(&callout_driver))
3385
                panic("Couldn't register callout driver\n");
3386
 
3387
        for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
3388
                                init_port(info, i);
3389
                };
3390
 
3391
        register_symtab(&serial_syms);
3392
        return 0;
3393
}
3394
 
3395
 
3396
 
3397
/*
3398
 * register_serial and unregister_serial allows for serial ports to be
3399
 * configured at run-time, to support PCMCIA modems.
3400
 */
3401
int register_serial(struct serial_struct *req)
3402
{
3403
        int i;
3404
        unsigned long flags;
3405
        struct async_struct *info;
3406
 
3407
        save_flags(flags);
3408
        cli();
3409
        for (i = 0; i < NR_PORTS; i++) {
3410
                if (rs_table[i].port == req->port)
3411
                        break;
3412
        }
3413
        if (i == NR_PORTS) {
3414
                for (i = 0; i < NR_PORTS; i++)
3415
                        if ((rs_table[i].type == PORT_UNKNOWN) &&
3416
                            (rs_table[i].count == 0))
3417
                                break;
3418
        }
3419
        if (i == NR_PORTS) {
3420
                restore_flags(flags);
3421
                return -1;
3422
        }
3423
        info = &rs_table[i];
3424
        if (rs_table[i].count) {
3425
                restore_flags(flags);
3426
                printk("Couldn't configure serial #%d (port=%d,irq=%d): "
3427
                       "device already open\n", i, req->port, req->irq);
3428
                return -1;
3429
        }
3430
        info->irq = req->irq;
3431
        info->port = req->port;
3432
        info->flags = req->flags;
3433
        autoconfig(info);
3434
        if (info->type == PORT_UNKNOWN) {
3435
                restore_flags(flags);
3436
                printk("register_serial(): autoconfig failed\n");
3437
                return -1;
3438
        }
3439
                printk(KERN_INFO "ttyS%02d at 0x%04x (irq = %d)", info->line,
3440
               info->port, info->irq);
3441
                display_uart_type(info->type);
3442
        restore_flags(flags);
3443
        return info->line;
3444
}
3445
 
3446
void unregister_serial(int line)
3447
{
3448
        unsigned long flags;
3449
        struct async_struct *info = &rs_table[line];
3450
 
3451
        save_flags(flags);
3452
        cli();
3453
        if (info->tty)
3454
                tty_hangup(info->tty);
3455
        info->type = PORT_UNKNOWN;
3456
                printk(KERN_INFO "ttyS%02d unloaded\n", info->line);
3457
        restore_flags(flags);
3458
}
3459
 
3460
#ifdef MODULE
3461
int init_module(void)
3462
{
3463
        return rs_init();
3464
}
3465
 
3466
void cleanup_module(void)
3467
{
3468
        unsigned long flags;
3469
        int e1, e2;
3470
        int i;
3471
 
3472
        /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
3473
        save_flags(flags);
3474
        cli();
3475
        timer_active &= ~(1 << RS_TIMER);
3476
        timer_table[RS_TIMER].fn = NULL;
3477
        timer_table[RS_TIMER].expires = 0;
3478
        if ((e1 = tty_unregister_driver(&serial_driver)))
3479
                printk("SERIAL: failed to unregister serial driver (%d)\n",
3480
                       e1);
3481
        if ((e2 = tty_unregister_driver(&callout_driver)))
3482
                printk("SERIAL: failed to unregister callout driver (%d)\n",
3483
                       e2);
3484
        restore_flags(flags);
3485
 
3486
        for (i = 0; i < NR_PORTS; i++) {
3487
                if (rs_table[i].type != PORT_UNKNOWN)
3488
                        release_region(rs_table[i].port, 8);
3489
        }
3490
 
3491
#ifdef CONFIG_SERIAL_PCI
3492
                for (i = 0; i < PCI_NR_BOARDS; i++) {
3493
                  if (pci_rs_chips[i].start != 0x0) {
3494
#ifdef SERIAL_DEBUG_PCI
3495
                        printk(KERN_DEBUG "Releasing %d Bytes at #%x\n", pci_rs_chips[i].type->io_size, pci_rs_chips[i].start);
3496
#endif
3497
                        release_region(pci_rs_chips[i].start, pci_rs_chips[i].type->io_size);
3498
                  }
3499
                }
3500
#endif
3501
 
3502
        if (tmp_buf) {
3503
                free_page((unsigned long) tmp_buf);
3504
                tmp_buf = NULL;
3505
        }
3506
}
3507
#endif /* MODULE */

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.