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

Subversion Repositories or1k_old

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

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

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

powered by: WebSVN 2.1.0

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