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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [char/] [serial.c] - Blame information for rev 1765

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

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

powered by: WebSVN 2.1.0

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