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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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