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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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