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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [serial_txx927.c] - Blame information for rev 1774

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

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *  drivers/char/serial_txx927.c
3
 *  driver for TX[34]927 SIO
4
 *
5
 * Copyright 2001 MontaVista Software Inc.
6
 * Author: MontaVista Software, Inc.
7
 *                ahennessy@mvista.com
8
 *
9
 * Based on drivers/char/serial.c
10
 *
11
 * Copyright (C) 2000-2001 Toshiba Corporation
12
 *
13
 * This program is free software; you can redistribute  it and/or modify it
14
 * under  the terms of  the GNU General  Public License as published by the
15
 * Free Software Foundation;  either version 2 of the  License, or (at your
16
 * option) any later version.
17
 *
18
 * THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
19
 * WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
20
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
21
 * NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
24
 * USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
 * ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * You should have received a copy of the  GNU General Public License along
30
 * with this program; if not, write  to the Free Software Foundation, Inc.,
31
 * 675 Mass Ave, Cambridge, MA 02139, USA.
32
 */
33
 
34
#define SERIAL_DO_RESTART
35
 
36
/* Set of debugging defines */
37
 
38
#undef SERIAL_DEBUG_INTR
39
#undef SERIAL_DEBUG_OPEN
40
#undef SERIAL_DEBUG_FLOW
41
#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
42
#undef SERIAL_DEBUG_PCI
43
#undef SERIAL_DEBUG_AUTOCONF   
44
 
45
#ifdef MODULE
46
#undef CONFIG_TXX927_SERIAL_CONSOLE
47
#endif                               
48
 
49
#define CONFIG_SERIAL_RSA
50
 
51
#define RS_STROBE_TIME (10*HZ)
52
#define RS_ISR_PASS_LIMIT 256     
53
 
54
/*
55
 * End of serial driver configuration section.
56
 */
57
 
58
#include <linux/module.h>    
59
 
60
#include <linux/config.h>
61
#include <linux/module.h>
62
#include <linux/errno.h>
63
#include <linux/signal.h>
64
#include <linux/sched.h>
65
#include <linux/timer.h>
66
#include <linux/interrupt.h>
67
#include <linux/tty.h>
68
#include <linux/tty_flip.h>
69
#include <linux/serial.h>
70
#include <linux/circ_buf.h>
71
#include <linux/serial_reg.h>
72
#include <linux/major.h>
73
#include <linux/string.h>
74
#include <linux/fcntl.h>
75
#include <linux/ptrace.h>
76
#include <linux/ioport.h>
77
#include <linux/mm.h>
78
#include <linux/slab.h>
79
#include <linux/init.h>
80
#include <linux/serialP.h>
81
#include <linux/delay.h>
82
#ifdef CONFIG_TXX927_SERIAL_CONSOLE
83
#include <linux/console.h>
84
#endif
85
#ifdef CONFIG_MAGIC_SYSRQ
86
#include <linux/sysrq.h>
87
#endif            
88
 
89
#include <asm/system.h>
90
#include <asm/serial.h>
91
#include <asm/io.h>
92
#include <asm/irq.h>
93
#include <asm/uaccess.h>
94
#include <asm/bitops.h>
95
#include <asm/jmr3927/txx927.h>
96
#include <asm/bootinfo.h>
97
#ifdef CONFIG_TOSHIBA_JMR3927
98
#include <asm/jmr3927/jmr3927.h>
99
#endif
100
 
101
#define _INLINE_ inline
102
 
103
#ifdef CONFIG_MAC_SERIAL
104
#define SERIAL_DEV_OFFSET       2
105
#else
106
#define SERIAL_DEV_OFFSET       0
107
#endif
108
 
109
static char *serial_name = "TXx927 Serial driver";
110
static char *serial_version = "0.02";
111
 
112
static DECLARE_TASK_QUEUE(tq_serial);
113
 
114
static struct tty_driver serial_driver, callout_driver;
115
static int serial_refcount;
116
 
117
static struct timer_list serial_timer;
118
 
119
extern unsigned long get_txx927_uart_baud(void);
120
 
121
/* serial subtype definitions */
122
#ifndef SERIAL_TYPE_NORMAL
123
#define SERIAL_TYPE_NORMAL      1
124
#define SERIAL_TYPE_CALLOUT     2
125
#endif                          
126
 
127
/* number of characters left in xmit buffer before we ask for more */
128
#define WAKEUP_CHARS 256
129
 
130
/*
131
 * IRQ_timeout          - How long the timeout should be for each IRQ
132
 *                              should be after the IRQ has been active.
133
 */
134
 
135
static struct async_struct *IRQ_ports[NR_IRQS];
136
static int IRQ_timeout[NR_IRQS];
137
#ifdef CONFIG_TXX927_SERIAL_CONSOLE
138
static struct console sercons;
139
#endif
140
#if defined(CONFIG_TXX927_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
141
static unsigned long break_pressed; /* break, really ... */
142
#endif                                                        
143
 
144
static void change_speed(struct async_struct *info, struct termios *old);
145
static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
146
 
147
#ifndef PREPARE_FUNC
148
#define PREPARE_FUNC(dev)  (dev->prepare)
149
#define ACTIVATE_FUNC(dev)  (dev->activate)
150
#define DEACTIVATE_FUNC(dev)  (dev->deactivate)
151
#endif
152
 
153
#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
154
 
155
 
156
#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
157
#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
158
 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
159
#else
160
#define DBG_CNT(s)
161
#endif                                     
162
 
163
#define SERIAL_DRIVER_NAME "TXx927SIO"
164
 
165
#ifdef CONFIG_SERIAL
166
/* "ttyS","cua" is used for standard serial driver */
167
#define TXX927_TTY_NAME "ttySC"
168
#define TXX927_TTY_MINOR_START  (64 + 16)       /* ttySC0(80), ttySC1(81) */
169
#define TXX927_CU_NAME "cuac"
170
#define TXX927_SERIAL_BH        TXX927SERIAL_BH
171
#else
172
/* acts like standard serial driver */
173
#define TXX927_TTY_NAME "ttyS"
174
#define TXX927_TTY_MINOR_START  64
175
#define TXX927_CU_NAME "cua"
176
#define TXX927_SERIAL_BH        SERIAL_BH
177
#endif
178
#define TXX927_TTY_MAJOR        TTY_MAJOR
179
#define TXX927_TTYAUX_MAJOR     TTYAUX_MAJOR
180
 
181
#define ASYNC_HAVE_CTS_LINE             ASYNC_BOOT_AUTOCONF     /* reuse */
182
 
183
static struct serial_state rs_table[RS_TABLE_SIZE] = {
184
        SERIAL_PORT_DFNS        /* Defined in serial.h */
185
};
186
 
187
#define NR_PORTS        (sizeof(rs_table)/sizeof(struct serial_state))
188
 
189
static struct tty_struct *serial_table[NR_PORTS];
190
static struct termios *serial_termios[NR_PORTS];
191
static struct termios *serial_termios_locked[NR_PORTS];
192
 
193
#ifndef MIN
194
#define MIN(a,b)        ((a) < (b) ? (a) : (b))
195
#endif
196
 
197
/*
198
 * tmp_buf is used as a temporary buffer by serial_write.  We need to
199
 * lock it in case the copy_from_user blocks while swapping in a page,
200
 * and some other program tries to do a serial write at the same time.
201
 * Since the lock will only come under contention when the system is
202
 * swapping and available memory is low, it makes sense to share one
203
 * buffer across all the serial ports, since it significantly saves
204
 * memory if large numbers of serial ports are open.
205
 */
206
static unsigned char *tmp_buf;
207
#ifdef DECLARE_MUTEX
208
static DECLARE_MUTEX(tmp_buf_sem);
209
#else
210
static struct semaphore tmp_buf_sem = MUTEX;
211
#endif                                 
212
 
213
static inline int serial_paranoia_check(struct async_struct *info,
214
                                        kdev_t device, const char *routine)
215
{
216
#ifdef SERIAL_PARANOIA_CHECK
217
        static const char *badmagic =
218
                "Warning: bad magic number for serial struct (%s) in %s\n";
219
        static const char *badinfo =
220
                "Warning: null async_struct for (%s) in %s\n";
221
 
222
        if (!info) {
223
                printk(badinfo, kdevname(device), routine);
224
                return 1;
225
        }
226
        if (info->magic != SERIAL_MAGIC) {
227
                printk(badmagic, kdevname(device), routine);
228
                return 1;
229
        }
230
#endif
231
        return 0;
232
}
233
 
234
static inline struct txx927_sio_reg *sio_reg(struct async_struct *info)
235
{
236
        return (struct txx927_sio_reg *)info->port;
237
}
238
 
239
/*
240
 *      Wait for transmitter & holding register to empty
241
 */
242
static inline void wait_for_xmitr(struct async_struct *info)
243
{
244
        unsigned int tmout = 1000000;
245
 
246
        do {
247
                if (--tmout == 0) break;
248
        } while (!(sio_reg(info)->cisr & TXx927_SICISR_TXALS));
249
}
250
 
251
/*
252
 * ------------------------------------------------------------
253
 * rs_stop() and rs_start()
254
 *
255
 * This routines are called before setting or resetting tty->stopped.
256
 * They enable or disable transmitter interrupts, as necessary.
257
 * ------------------------------------------------------------
258
 */
259
static void rs_stop(struct tty_struct *tty)
260
{
261
        struct async_struct *info = (struct async_struct *)tty->driver_data;
262
        unsigned long flags;
263
 
264
        if (serial_paranoia_check(info, tty->device, "rs_stop"))
265
                return;
266
 
267
        save_flags(flags); cli();
268
        if (info->IER & UART_IER_THRI) {
269
                info->IER &= ~UART_IER_THRI;
270
                sio_reg(info)->dicr &= ~TXx927_SIDICR_TIE;
271
        }
272
        restore_flags(flags);
273
}
274
 
275
static void rs_start(struct tty_struct *tty)
276
{
277
        struct async_struct *info = (struct async_struct *)tty->driver_data;
278
        unsigned long flags;
279
 
280
        if (serial_paranoia_check(info, tty->device, "rs_start"))
281
                return;
282
 
283
        save_flags(flags); cli();
284
        if (info->xmit.head != info->xmit.tail
285
            && info->xmit.buf
286
            && !(info->IER & UART_IER_THRI)) {
287
                info->IER |= UART_IER_THRI;
288
                sio_reg(info)->dicr |= TXx927_SIDICR_TIE;
289
        }
290
        restore_flags(flags);
291
}
292
 
293
/*
294
 * ----------------------------------------------------------------------
295
 *
296
 * Here starts the interrupt handling routines.  All of the following
297
 * subroutines are declared as inline and are folded into
298
 * rs_interrupt().  They were separated out for readability's sake.
299
 *
300
 * Note: rs_interrupt() is a "fast" interrupt, which means that it
301
 * runs with interrupts turned off.  People who may want to modify
302
 * rs_interrupt() should try to keep the interrupt handler as fast as
303
 * possible.  After you are done making modifications, it is not a bad
304
 * idea to do:
305
 *
306
 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
307
 *
308
 * and look at the resulting assemble code in serial.s.
309
 *
310
 *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
311
 * -----------------------------------------------------------------------
312
 */
313
 
314
/*
315
 * This routine is used by the interrupt handler to schedule
316
 * processing in the software interrupt portion of the driver.
317
 */
318
static _INLINE_ void rs_sched_event(struct async_struct *info,
319
                                  int event)
320
{
321
        info->event |= 1 << event;
322
        queue_task(&info->tqueue, &tq_serial);
323
        mark_bh(TXX927_SERIAL_BH);
324
}
325
 
326
static _INLINE_ void receive_chars(struct async_struct *info,
327
                                 int *status)
328
{
329
        struct tty_struct *tty = info->tty;
330
        unsigned char ch;
331
        int ignored = 0;
332
        struct  async_icount *icount;
333
 
334
        icount = &info->state->icount;
335
        do {
336
                ch = sio_reg(info)->rfifo;
337
                if (tty->flip.count >= TTY_FLIPBUF_SIZE)
338
                        break;
339
                *tty->flip.char_buf_ptr = ch;
340
                icount->rx++;
341
 
342
#ifdef SERIAL_DEBUG_INTR
343
                printk("DR%02x:%02x...", ch, *status);
344
#endif
345
                *tty->flip.flag_buf_ptr = 0;
346
                if (*status & (TXx927_SIDISR_UBRK | TXx927_SIDISR_UPER |
347
                               TXx927_SIDISR_UFER | TXx927_SIDISR_UOER)) {
348
                        /*
349
                         * For statistics only
350
                         */
351
                        if (*status & TXx927_SIDISR_UBRK) {
352
                                *status &= ~(TXx927_SIDISR_UFER | TXx927_SIDISR_UPER);
353
                                icount->brk++;
354
                        } else if (*status & TXx927_SIDISR_UPER)
355
                                icount->parity++;
356
                        else if (*status & TXx927_SIDISR_UFER)
357
                                icount->frame++;
358
                        if (*status & TXx927_SIDISR_UOER)
359
                                icount->overrun++;
360
 
361
                        /*
362
                         * Now check to see if character should be
363
                         * ignored, and mask off conditions which
364
                         * should be ignored.
365
                         */
366
                        if (*status & info->ignore_status_mask) {
367
                                if (++ignored > 100)
368
                                        break;
369
                                goto ignore_char;
370
                        }
371
                        *status &= info->read_status_mask;
372
 
373
                        if (*status & (TXx927_SIDISR_UBRK)) {
374
#ifdef SERIAL_DEBUG_INTR
375
                                printk("handling break....");
376
#endif
377
                                *tty->flip.flag_buf_ptr = TTY_BREAK;
378
                                if (info->flags & ASYNC_SAK)
379
                                        do_SAK(tty);
380
                        } else if (*status & TXx927_SIDISR_UPER)
381
                                *tty->flip.flag_buf_ptr = TTY_PARITY;
382
                        else if (*status & TXx927_SIDISR_UFER)
383
                                *tty->flip.flag_buf_ptr = TTY_FRAME;
384
                        if (*status & TXx927_SIDISR_UOER) {
385
                                /*
386
                                 * Overrun is special, since it's
387
                                 * reported immediately, and doesn't
388
                                 * affect the current character
389
                                 */
390
                                if (tty->flip.count < TTY_FLIPBUF_SIZE) {
391
                                        tty->flip.count++;
392
                                        tty->flip.flag_buf_ptr++;
393
                                        tty->flip.char_buf_ptr++;
394
                                        *tty->flip.flag_buf_ptr = TTY_OVERRUN;
395
                                }
396
                        }
397
                }
398
                tty->flip.flag_buf_ptr++;
399
                tty->flip.char_buf_ptr++;
400
                tty->flip.count++;
401
        ignore_char:
402
                *status = sio_reg(info)->disr;
403
        } while (!(*status & TXx927_SIDISR_UVALID));
404
 
405
        tty_flip_buffer_push(tty);
406
}
407
 
408
static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
409
{
410
        int count;
411
 
412
        wait_for_xmitr(info);
413
 
414
        if (info->x_char) {
415
                sio_reg(info)->tfifo = info->x_char;
416
                info->state->icount.tx++;
417
                info->x_char = 0;
418
                if (intr_done)
419
                        *intr_done = 0;
420
                return;
421
        }
422
 
423
        if (info->xmit.head == info->xmit.tail
424
            || info->tty->stopped
425
            || info->tty->hw_stopped) {
426
                sio_reg(info)->dicr &= ~TXx927_SIDICR_TIE;
427
                return;
428
        }
429
 
430
        count = info->xmit_fifo_size;
431
        do {
432
                sio_reg(info)->tfifo = info->xmit.buf[info->xmit.tail++];
433
                info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
434
                info->state->icount.tx++;
435
                if (info->xmit.head == info->xmit.tail)
436
                        break;
437
        } while (--count > 0);
438
 
439
        if (CIRC_CNT(info->xmit.head,
440
                     info->xmit.tail,
441
                     SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
442
                rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
443
 
444
#ifdef SERIAL_DEBUG_INTR
445
        printk("THRE...");
446
#endif
447
        if (intr_done)
448
                *intr_done = 0;
449
 
450
        if (info->xmit.head == info->xmit.tail) {
451
                sio_reg(info)->dicr &= ~TXx927_SIDICR_TIE;
452
        }
453
}
454
 
455
static _INLINE_ void check_modem_status(struct async_struct *info)
456
{
457
        /* RTS/CTS are controled by HW. (if possible) */
458
}
459
 
460
/*
461
 * This is the serial driver's interrupt routine for a single port
462
 */
463
static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
464
{
465
        int status;
466
        int pass_counter = 0;
467
        struct async_struct * info;
468
 
469
#ifdef SERIAL_DEBUG_INTR
470
        printk("rs_interrupt_single(%d)...", irq);
471
#endif
472
 
473
        info = IRQ_ports[irq];
474
        if (!info || !info->tty)
475
                return;
476
 
477
        do {
478
                status = sio_reg(info)->disr;
479
#ifdef SERIAL_DEBUG_INTR
480
                printk("status = %x...", status);
481
#endif
482
                if (!(sio_reg(info)->dicr & TXx927_SIDICR_TIE))
483
                        status &= ~TXx927_SIDISR_TDIS;
484
                if (!(status & (TXx927_SIDISR_TDIS | TXx927_SIDISR_RDIS | TXx927_SIDISR_TOUT)))
485
                        break;
486
 
487
                if (status & TXx927_SIDISR_RDIS)
488
                        receive_chars(info, &status);
489
                check_modem_status(info);
490
                if (status & TXx927_SIDISR_TDIS)
491
                        transmit_chars(info, 0);
492
                /* Clear TX/RX Int. Status */
493
                sio_reg(info)->disr &= ~(TXx927_SIDISR_TDIS | TXx927_SIDISR_RDIS | TXx927_SIDISR_TOUT);
494
 
495
                if (pass_counter++ > RS_ISR_PASS_LIMIT) {
496
#ifdef SERIAL_DEBUG_INTR
497
                        printk("rs_single loop break.\n");
498
#endif
499
                        break;
500
                }
501
        } while (1);
502
        info->last_active = jiffies;
503
#ifdef SERIAL_DEBUG_INTR
504
        printk("end.\n");
505
#endif
506
}
507
 
508
/*
509
 * -------------------------------------------------------------------
510
 * Here ends the serial interrupt routines.
511
 * -------------------------------------------------------------------
512
 */
513
 
514
/*
515
 * This routine is used to handle the "bottom half" processing for the
516
 * serial driver, known also the "software interrupt" processing.
517
 * This processing is done at the kernel interrupt level, after the
518
 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
519
 * is where time-consuming activities which can not be done in the
520
 * interrupt driver proper are done; the interrupt driver schedules
521
 * them using rs_sched_event(), and they get done here.
522
 */
523
static void do_serial_bh(void)
524
{
525
        run_task_queue(&tq_serial);
526
}
527
 
528
static void do_softint(void *private_)
529
{
530
        struct async_struct     *info = (struct async_struct *) private_;
531
        struct tty_struct       *tty;
532
 
533
        tty = info->tty;
534
        if (!tty)
535
                return;
536
 
537
        if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
538
                if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
539
                    tty->ldisc.write_wakeup)
540
                        (tty->ldisc.write_wakeup)(tty);
541
                wake_up_interruptible(&tty->write_wait);
542
#ifdef SERIAL_HAVE_POLL_WAIT
543
                wake_up_interruptible(&tty->poll_wait);
544
#endif                                                    
545
        }
546
}
547
 
548
/*
549
 * This subroutine is called when the RS_TIMER goes off.  It is used
550
 * by the serial driver to handle ports that do not have an interrupt
551
 * (irq=0).  This doesn't work very well for 16450's, but gives barely
552
 * passable results for a 16550A.  (Although at the expense of much
553
 * CPU overhead).
554
 */
555
static void rs_timer(unsigned long dummy)
556
{
557
        static unsigned long last_strobe;
558
        struct async_struct *info;
559
        unsigned int    i;
560
        unsigned long flags;
561
 
562
        if ((jiffies - last_strobe) >= RS_STROBE_TIME) {
563
                for (i=0; i < NR_IRQS; i++) {
564
                        info = IRQ_ports[i];
565
                        if (!info)
566
                                continue;
567
                        save_flags(flags); cli();
568
                                rs_interrupt_single(i, NULL, NULL);
569
                        restore_flags(flags);
570
                }
571
        }
572
        last_strobe = jiffies;
573
        mod_timer(&serial_timer, jiffies + RS_STROBE_TIME);
574
 
575
#if 0
576
        if (IRQ_ports[0]) {
577
                save_flags(flags); cli();
578
                rs_interrupt_single(0, NULL, NULL);
579
                restore_flags(flags);
580
 
581
                mod_timer(&serial_timer, jiffies + IRQ_timeout[0]);
582
        }
583
#endif
584
}
585
 
586
/*
587
 * ---------------------------------------------------------------
588
 * Low level utility subroutines for the serial driver:  routines to
589
 * figure out the appropriate timeout for an interrupt chain, routines
590
 * to initialize and startup a serial port, and routines to shutdown a
591
 * serial port.  Useful stuff like that.
592
 * ---------------------------------------------------------------
593
 */
594
 
595
/*
596
 * This routine figures out the correct timeout for a particular IRQ.
597
 * It uses the smallest timeout of all of the serial ports in a
598
 * particular interrupt chain.  Now only used for IRQ 0....
599
 */
600
static void figure_IRQ_timeout(int irq)
601
{
602
        struct  async_struct    *info;
603
        int     timeout = 60*HZ;        /* 60 seconds === a long time :-) */
604
 
605
        info = IRQ_ports[irq];
606
        if (!info) {
607
                IRQ_timeout[irq] = 60*HZ;
608
                return;
609
        }
610
        while (info) {
611
                if (info->timeout < timeout)
612
                        timeout = info->timeout;
613
                info = info->next_port;
614
        }
615
        if (!irq)
616
                timeout = timeout / 2;
617
        IRQ_timeout[irq] = timeout ? timeout : 1;
618
}
619
 
620
static int startup(struct async_struct * info)
621
{
622
        unsigned long flags;
623
        int     retval=0;
624
        struct serial_state *state= info->state;
625
        unsigned long page;
626
 
627
        page = get_zeroed_page(GFP_KERNEL);
628
        if (!page)
629
                return -ENOMEM;
630
 
631
        save_flags(flags); cli();
632
 
633
        if (info->flags & ASYNC_INITIALIZED) {
634
                free_page(page);
635
                goto errout;
636
        }
637
 
638
        if (!state->port) {
639
                if (info->tty)
640
                        set_bit(TTY_IO_ERROR, &info->tty->flags);
641
                free_page(page);
642
                goto errout;
643
        }
644
        if (info->xmit.buf)
645
                free_page(page);
646
        else
647
                info->xmit.buf = (unsigned char *) page;
648
 
649
#ifdef SERIAL_DEBUG_OPEN
650
        printk("starting up ttys%d (irq %d)...", info->line, state->irq);
651
#endif
652
 
653
        /*
654
         * Clear the FIFO buffers and disable them
655
         * (they will be reenabled in change_speed())
656
         */
657
        sio_reg(info)->fcr |= TXx927_SIFCR_TFRST | TXx927_SIFCR_RFRST |
658
                TXx927_SIFCR_FRSTE;
659
        /* clear reset */
660
        sio_reg(info)->fcr &= ~(TXx927_SIFCR_TFRST | TXx927_SIFCR_RFRST |
661
                                TXx927_SIFCR_FRSTE);
662
 
663
        /*
664
         * Allocate the IRQ if necessary
665
         */
666
        if (state->irq && (!IRQ_ports[state->irq] ||
667
                          !IRQ_ports[state->irq]->next_port)) {
668
                if (IRQ_ports[state->irq]) {
669
                        retval = -EBUSY;
670
                        goto errout;
671
                }
672
 
673
                retval = request_irq(state->irq, rs_interrupt_single,
674
                                     SA_INTERRUPT,
675
                                     "txx927serial", NULL);
676
                if (retval) {
677
                        if (capable(CAP_SYS_ADMIN)) {
678
                                if (info->tty)
679
                                        set_bit(TTY_IO_ERROR,
680
                                                &info->tty->flags);
681
                                retval = 0;
682
                        }
683
                        goto errout;
684
                }
685
        }
686
 
687
        /*
688
         * Insert serial port into IRQ chain.
689
         */
690
        info->prev_port = 0;
691
        info->next_port = IRQ_ports[state->irq];
692
        if (info->next_port)
693
                info->next_port->prev_port = info;
694
        IRQ_ports[state->irq] = info;
695
        figure_IRQ_timeout(state->irq);
696
 
697
        /*
698
         * Clear the interrupt registers.
699
         */
700
        sio_reg(info)->disr = 0;
701
 
702
        /*
703
         * Now, initialize the UART
704
         */
705
        /* HW RTS/CTS control */
706
        if (state->flags & ASYNC_HAVE_CTS_LINE)
707
                sio_reg(info)->flcr = TXx927_SIFLCR_RCS | TXx927_SIFLCR_TES |
708
                        TXx927_SIFLCR_RTSTL_MAX /* 15 */;
709
        /* Enable RX/TX */
710
        sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RSDE | TXx927_SIFLCR_TSDE);
711
 
712
        /*
713
         * Finally, enable interrupts
714
         */
715
        sio_reg(info)->dicr = TXx927_SIDICR_RIE;
716
 
717
        if (info->tty)
718
                clear_bit(TTY_IO_ERROR, &info->tty->flags);
719
        info->xmit.head = info->xmit.tail = 0;
720
 
721
        /*
722
         * Set up the tty->alt_speed kludge
723
         */
724
        if (info->tty) {
725
                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
726
                        info->tty->alt_speed = 57600;
727
                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
728
                        info->tty->alt_speed = 115200;
729
                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
730
                        info->tty->alt_speed = 230400;
731
                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
732
                        info->tty->alt_speed = 460800;
733
        }
734
 
735
        /*
736
         * and set the speed of the serial port
737
         */
738
        change_speed(info, 0);
739
 
740
        info->flags |= ASYNC_INITIALIZED;
741
        restore_flags(flags);
742
        return 0;
743
 
744
errout:
745
        restore_flags(flags);
746
        return retval;
747
}
748
 
749
/*
750
 * This routine will shutdown a serial port; interrupts are disabled, and
751
 * DTR is dropped if the hangup on close termio flag is on.
752
 */
753
static void shutdown(struct async_struct * info)
754
{
755
        unsigned long   flags;
756
        struct serial_state *state;
757
        int             retval;
758
 
759
        if (!(info->flags & ASYNC_INITIALIZED))
760
                return;
761
 
762
        state = info->state;
763
 
764
#ifdef SERIAL_DEBUG_OPEN
765
        printk("Shutting down serial port %d (irq %d)....", info->line,
766
               state->irq);
767
#endif
768
 
769
        save_flags(flags); cli(); /* Disable interrupts */
770
 
771
        /*
772
         * First unlink the serial port from the IRQ chain...
773
         */
774
        if (info->next_port)
775
                info->next_port->prev_port = info->prev_port;
776
        if (info->prev_port)
777
                info->prev_port->next_port = info->next_port;
778
        else
779
                IRQ_ports[state->irq] = info->next_port;
780
        figure_IRQ_timeout(state->irq);
781
 
782
        /*
783
         * Free the IRQ, if necessary
784
         */
785
        if (state->irq && (!IRQ_ports[state->irq] ||
786
                          !IRQ_ports[state->irq]->next_port)) {
787
                if (IRQ_ports[state->irq]) {
788
                        free_irq(state->irq, NULL);
789
                        retval = request_irq(state->irq, rs_interrupt_single,
790
                                             SA_INTERRUPT, "txx927serial", NULL);
791
 
792
                        if (retval)
793
                                printk(KERN_WARNING "txx927serial shutdown: request_irq: error %d"
794
                                       "  Couldn't reacquire IRQ.\n", retval);
795
                } else
796
                        free_irq(state->irq, NULL);
797
        }
798
 
799
        if (info->xmit.buf) {
800
                free_page((unsigned long) info->xmit.buf);
801
                info->xmit.buf = 0;
802
        }
803
 
804
        sio_reg(info)->dicr = 0; /* disable all intrs */
805
 
806
        /* disable break condition */
807
        sio_reg(info)->flcr &= ~TXx927_SIFLCR_TBRK;
808
 
809
        if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
810
                /* drop RTS */
811
                sio_reg(info)->flcr |= TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE;
812
                /* TXx927-SIO can not control DTR... */
813
        }
814
 
815
        /* reset FIFO's */
816
        sio_reg(info)->fcr |= TXx927_SIFCR_TFRST | TXx927_SIFCR_RFRST |
817
                TXx927_SIFCR_FRSTE;
818
        /* clear reset */
819
        sio_reg(info)->fcr &= ~(TXx927_SIFCR_TFRST | TXx927_SIFCR_RFRST |
820
                                TXx927_SIFCR_FRSTE);
821
 
822
        /* DON'T disable Rx/Tx here, ie. DON'T set either
823
         * TXx927_SIFLCR_RSDE or TXx927_SIFLCR_TSDE in flcr
824
         */
825
 
826
        if (info->tty)
827
                set_bit(TTY_IO_ERROR, &info->tty->flags);
828
 
829
        info->flags &= ~ASYNC_INITIALIZED;
830
        restore_flags(flags);
831
}
832
 
833
/*
834
 * This routine is called to set the UART divisor registers to match
835
 * the specified baud rate for a serial port.
836
 */
837
static void change_speed(struct async_struct *info,
838
                         struct termios *old_termios)
839
{
840
        int     quot = 0, baud_base, baud;
841
        unsigned cflag, cval;
842
        int     bits;
843
        unsigned long   flags;
844
 
845
        if (!info->tty || !info->tty->termios)
846
                return;
847
        cflag = info->tty->termios->c_cflag;
848
        if (!info->port)
849
                return;
850
 
851
        cval = sio_reg(info)->lcr;
852
        /* byte size and parity */
853
        cval &= ~TXx927_SILCR_UMODE_MASK;
854
        switch (cflag & CSIZE) {
855
        case CS7:
856
                cval |= TXx927_SILCR_UMODE_7BIT;
857
                bits = 9;
858
                break;
859
        case CS5:       /* not supported */
860
        case CS6:       /* not supported */
861
        case CS8:
862
        default:
863
                cval |= TXx927_SILCR_UMODE_8BIT;
864
                bits = 10;
865
                break;
866
        }
867
        cval &= ~TXx927_SILCR_USBL_MASK;
868
        if (cflag & CSTOPB) {
869
                cval |= TXx927_SILCR_USBL_2BIT;
870
                bits++;
871
        } else {
872
                cval |= TXx927_SILCR_USBL_1BIT;
873
        }
874
 
875
        cval &= ~(TXx927_SILCR_UPEN|TXx927_SILCR_UEPS);
876
        if (cflag & PARENB) {
877
                cval |= TXx927_SILCR_UPEN;
878
                bits++;
879
        }
880
        if (!(cflag & PARODD))
881
                cval |= TXx927_SILCR_UEPS;
882
 
883
        /* Determine divisor based on baud rate */
884
        baud = tty_get_baud_rate(info->tty);
885
        if (!baud)
886
                baud = 9600;    /* B0 transition handled in rs_set_termios */
887
        baud_base = info->state->baud_base;
888
        quot = (baud_base + baud / 2) / baud;
889
        /* If the quotient is zero refuse the change */
890
        if (!quot && old_termios) {
891
                info->tty->termios->c_cflag &= ~CBAUD;
892
                info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
893
                baud = tty_get_baud_rate(info->tty);
894
                if (!baud)
895
                        baud = 9600;
896
                quot = (baud_base + baud / 2) / baud;
897
        }
898
        /* As a last resort, if the quotient is zero, default to 9600 bps */
899
        if (!quot)
900
                quot = (baud_base + 9600 / 2) / 9600;
901
        info->quot = quot;
902
        info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
903
        info->timeout += HZ/50;         /* Add .02 seconds of slop */
904
 
905
        /* CTS flow control flag */
906
        if (cflag & CRTSCTS) {
907
                info->flags |= ASYNC_CTS_FLOW;
908
                if (info->state->flags & ASYNC_HAVE_CTS_LINE)
909
                        sio_reg(info)->flcr = TXx927_SIFLCR_RCS | TXx927_SIFLCR_TES |
910
                                TXx927_SIFLCR_RTSTL_MAX /* 15 */;
911
        } else {
912
                info->flags &= ~ASYNC_CTS_FLOW;
913
                sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RCS | TXx927_SIFLCR_TES | TXx927_SIFLCR_RSDE | TXx927_SIFLCR_TSDE);
914
        }
915
 
916
        /*
917
         * Set up parity check flag
918
         */
919
#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
920
 
921
        info->read_status_mask = TXx927_SIDISR_UOER |
922
                TXx927_SIDISR_TDIS | TXx927_SIDISR_RDIS;
923
        if (I_INPCK(info->tty))
924
                info->read_status_mask |= TXx927_SIDISR_UFER | TXx927_SIDISR_UPER;
925
        if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
926
                info->read_status_mask |= TXx927_SIDISR_UBRK;
927
 
928
        /*
929
         * Characters to ignore
930
         */
931
        info->ignore_status_mask = 0;
932
        if (I_IGNPAR(info->tty))
933
                info->ignore_status_mask |= TXx927_SIDISR_UPER | TXx927_SIDISR_UFER;
934
        if (I_IGNBRK(info->tty)) {
935
                info->ignore_status_mask |= TXx927_SIDISR_UBRK;
936
                /*
937
                 * If we're ignore parity and break indicators, ignore
938
                 * overruns too.  (For real raw support).
939
                 */
940
                if (I_IGNPAR(info->tty))
941
                        info->ignore_status_mask |= TXx927_SIDISR_UOER;
942
        }
943
        /*
944
         * !!! ignore all characters if CREAD is not set
945
         */
946
        if ((cflag & CREAD) == 0)
947
                info->ignore_status_mask |= TXx927_SIDISR_RDIS;
948
        save_flags(flags); cli();
949
        sio_reg(info)->lcr = cval | TXx927_SILCR_SCS_IMCLK_BG;
950
        sio_reg(info)->bgr = quot | TXx927_SIBGR_BCLK_T0;
951
        restore_flags(flags);
952
}
953
 
954
static void rs_put_char(struct tty_struct *tty, unsigned char ch)
955
{
956
        struct async_struct *info = (struct async_struct *)tty->driver_data;
957
        unsigned long flags;
958
 
959
        if (serial_paranoia_check(info, tty->device, "rs_put_char"))
960
                return;
961
 
962
        if (!tty || !info->xmit.buf)
963
                return;
964
 
965
        save_flags(flags); cli();
966
        if (CIRC_SPACE(info->xmit.head,
967
                       info->xmit.tail,
968
                       SERIAL_XMIT_SIZE) == 0) {
969
                restore_flags(flags);
970
                return;
971
        }
972
 
973
        info->xmit.buf[info->xmit.head++] = ch;
974
        info->xmit.head &= SERIAL_XMIT_SIZE-1;
975
        restore_flags(flags);
976
}
977
 
978
static void rs_flush_chars(struct tty_struct *tty)
979
{
980
        struct async_struct *info = (struct async_struct *)tty->driver_data;
981
        unsigned long flags;
982
 
983
        if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
984
                return;
985
 
986
        if (info->xmit.head == info->xmit.tail
987
            || tty->stopped
988
            || tty->hw_stopped
989
            || !info->xmit.buf)
990
                return;
991
 
992
        save_flags(flags); cli();
993
        sio_reg(info)->dicr |= TXx927_SIDICR_TIE;
994
        restore_flags(flags);
995
}
996
 
997
static int rs_write(struct tty_struct * tty, int from_user,
998
                    const unsigned char *buf, int count)
999
{
1000
        int     c, ret = 0;
1001
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1002
        unsigned long flags;
1003
 
1004
        if (serial_paranoia_check(info, tty->device, "rs_write"))
1005
                return 0;
1006
 
1007
        if (!tty || !info->xmit.buf || !tmp_buf)
1008
                return 0;
1009
 
1010
        save_flags(flags);
1011
 
1012
        if (from_user) {
1013
                down(&tmp_buf_sem);
1014
                while (1) {
1015
                        int c1;
1016
                        c = CIRC_SPACE_TO_END(info->xmit.head,
1017
                                              info->xmit.tail,
1018
                                              SERIAL_XMIT_SIZE);
1019
 
1020
                        if (count < c)
1021
                                c = count;
1022
                        if (c <= 0)
1023
                                break;
1024
 
1025
                        c -= copy_from_user(tmp_buf, buf, c);
1026
                        if (!c) {
1027
                                if (!ret)
1028
                                        ret = -EFAULT;
1029
                                break;
1030
                        }
1031
                        cli();
1032
                        c1 = CIRC_SPACE_TO_END(info->xmit.head,
1033
                                               info->xmit.tail,
1034
                                               SERIAL_XMIT_SIZE);
1035
 
1036
                        if (c1 < c)
1037
                                c = c1;
1038
                        memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
1039
                        info->xmit.head = ((info->xmit.head + c) &
1040
                                           (SERIAL_XMIT_SIZE-1));
1041
                        restore_flags(flags);
1042
                        buf += c;
1043
                        count -= c;
1044
                        ret += c;
1045
                }
1046
                up(&tmp_buf_sem);
1047
        } else {
1048
                cli();
1049
                while (1) {
1050
                        c = CIRC_SPACE_TO_END(info->xmit.head,
1051
                                              info->xmit.tail,
1052
                                              SERIAL_XMIT_SIZE);
1053
 
1054
                        if (count < c)
1055
                                c = count;
1056
                        if (c <= 0) {
1057
                                break;
1058
                        }
1059
                        memcpy(info->xmit.buf + info->xmit.head, buf, c);
1060
                        info->xmit.head = ((info->xmit.head + c) &
1061
                                           (SERIAL_XMIT_SIZE-1));
1062
                        buf += c;
1063
                        count -= c;
1064
                        ret += c;
1065
                }
1066
                restore_flags(flags);
1067
        }
1068
        if (info->xmit.head != info->xmit.tail
1069
            && !tty->stopped
1070
            && !tty->hw_stopped
1071
            && !(info->IER & UART_IER_THRI))
1072
                sio_reg(info)->dicr |= TXx927_SIDICR_TIE;
1073
 
1074
        return ret;
1075
}
1076
 
1077
static int rs_write_room(struct tty_struct *tty)
1078
{
1079
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1080
 
1081
        if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1082
                return 0;
1083
 
1084
        return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1085
}
1086
 
1087
static int rs_chars_in_buffer(struct tty_struct *tty)
1088
{
1089
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1090
 
1091
        if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1092
                return 0;
1093
 
1094
        return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1095
}
1096
 
1097
static void rs_flush_buffer(struct tty_struct *tty)
1098
{
1099
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1100
        unsigned long flags;
1101
 
1102
        if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1103
                return;
1104
        save_flags(flags); cli();
1105
        info->xmit.head = info->xmit.tail = 0;
1106
        restore_flags(flags);
1107
        wake_up_interruptible(&tty->write_wait);
1108
#ifdef SERIAL_HAVE_POLL_WAIT
1109
        wake_up_interruptible(&tty->poll_wait);
1110
#endif
1111
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1112
            tty->ldisc.write_wakeup)
1113
                (tty->ldisc.write_wakeup)(tty);
1114
}
1115
 
1116
/*
1117
 * This function is used to send a high-priority XON/XOFF character to
1118
 * the device
1119
 */
1120
static void rs_send_xchar(struct tty_struct *tty, char ch)
1121
{
1122
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1123
 
1124
 
1125
        if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1126
                return;
1127
 
1128
        info->x_char = ch;
1129
        if (ch) {
1130
                /* Make sure transmit interrupts are on */
1131
                sio_reg(info)->dicr |= TXx927_SIDICR_TIE;
1132
        }
1133
}
1134
 
1135
/*
1136
 * ------------------------------------------------------------
1137
 * rs_throttle()
1138
 *
1139
 * This routine is called by the upper-layer tty layer to signal that
1140
 * incoming characters should be throttled.
1141
 * ------------------------------------------------------------
1142
 */
1143
static void rs_throttle(struct tty_struct * tty)
1144
{
1145
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1146
        unsigned long flags;
1147
#ifdef SERIAL_DEBUG_THROTTLE
1148
        char    buf[64];
1149
 
1150
        printk("throttle %s: %d....\n", tty_name(tty, buf),
1151
               tty->ldisc.chars_in_buffer(tty));
1152
#endif
1153
 
1154
        if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1155
                return;
1156
 
1157
        if (I_IXOFF(tty))
1158
                rs_send_xchar(tty, STOP_CHAR(tty));
1159
 
1160
        if (tty->termios->c_cflag & CRTSCTS) {
1161
                save_flags(flags); cli();
1162
                /* drop RTS */
1163
                sio_reg(info)->flcr |= TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE;
1164
                restore_flags(flags);
1165
        }
1166
}
1167
 
1168
static void rs_unthrottle(struct tty_struct * tty)
1169
{
1170
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1171
        unsigned long flags;
1172
#ifdef SERIAL_DEBUG_THROTTLE
1173
        char    buf[64];
1174
 
1175
        printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1176
               tty->ldisc.chars_in_buffer(tty));
1177
#endif
1178
 
1179
        if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1180
                return;
1181
 
1182
        if (I_IXOFF(tty)) {
1183
                if (info->x_char)
1184
                        info->x_char = 0;
1185
                else
1186
                        rs_send_xchar(tty, START_CHAR(tty));
1187
        }
1188
        if (tty->termios->c_cflag & CRTSCTS) {
1189
                save_flags(flags); cli();
1190
                sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE);
1191
                restore_flags(flags);
1192
        }
1193
}
1194
 
1195
/*
1196
 * ------------------------------------------------------------
1197
 * rs_ioctl() and friends
1198
 * ------------------------------------------------------------
1199
 */
1200
 
1201
static int get_modem_info(struct async_struct * info, unsigned int *value)
1202
{
1203
        unsigned int result;
1204
        unsigned long flags;
1205
 
1206
        save_flags(flags); cli();
1207
        result =  ((sio_reg(info)->flcr & TXx927_SIFLCR_RTSSC) ? 0 : TIOCM_RTS)
1208
                | ((sio_reg(info)->cisr & TXx927_SICISR_CTSS) ? 0 : TIOCM_CTS);
1209
        restore_flags(flags);
1210
        return put_user(result,value);
1211
}
1212
 
1213
static int set_modem_info(struct async_struct * info, unsigned int cmd,
1214
                          unsigned int *value)
1215
{
1216
        int error;
1217
        unsigned int arg;
1218
        unsigned long flags;
1219
 
1220
        error = get_user(arg, value);
1221
        if (error)
1222
                return error;
1223
        save_flags(flags); cli();
1224
        switch (cmd) {
1225
        case TIOCMBIS:
1226
                if (arg & TIOCM_RTS)
1227
                        sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE);
1228
                break;
1229
        case TIOCMBIC:
1230
                if (arg & TIOCM_RTS)
1231
                        sio_reg(info)->flcr |= TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE;
1232
                break;
1233
        case TIOCMSET:
1234
                sio_reg(info)->flcr =
1235
                        (sio_reg(info)->flcr & ~(TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE)) |
1236
                        ((arg & TIOCM_RTS) ? 0 : TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE);
1237
                break;
1238
        default:
1239
                error = -EINVAL;
1240
        }
1241
        restore_flags(flags);
1242
        return error;
1243
}
1244
 
1245
/*
1246
 * rs_break() --- routine which turns the break handling on or off
1247
 */
1248
static void rs_break(struct tty_struct *tty, int break_state)
1249
{
1250
        struct async_struct * info = (struct async_struct *)tty->driver_data;
1251
        unsigned long flags;
1252
 
1253
        if (serial_paranoia_check(info, tty->device, "rs_break"))
1254
                return;
1255
 
1256
        if (!info->port)
1257
                return;
1258
        save_flags(flags); cli();
1259
        if (break_state == -1)
1260
                sio_reg(info)->flcr |= TXx927_SIFLCR_TBRK;
1261
        else
1262
                sio_reg(info)->flcr &= ~TXx927_SIFLCR_TBRK;
1263
        restore_flags(flags);
1264
}
1265
 
1266
static int rs_ioctl(struct tty_struct *tty, struct file * file,
1267
                    unsigned int cmd, unsigned long arg)
1268
{
1269
        struct async_struct * info = (struct async_struct *)tty->driver_data;
1270
 
1271
        if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1272
                return -ENODEV;
1273
 
1274
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1275
            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1276
            (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1277
                if (tty->flags & (1 << TTY_IO_ERROR))
1278
                    return -EIO;
1279
        }
1280
 
1281
        switch (cmd) {
1282
                case TIOCMGET:
1283
                        return get_modem_info(info, (unsigned int *) arg);
1284
                case TIOCMBIS:
1285
                case TIOCMBIC:
1286
                case TIOCMSET:
1287
                        return set_modem_info(info, cmd, (unsigned int *) arg);
1288
                        return 0;
1289
                case TIOCGSERIAL:
1290
                        printk("TIOCGSERIAL\n");
1291
                        return 0;
1292
                case TIOCSSERIAL:
1293
                        printk("TIOCSSERIAL\n");
1294
                        return 0;
1295
                case TIOCSERCONFIG:
1296
                        printk("TIOCSERCONFIG\n");
1297
                        return 0;
1298
 
1299
                case TIOCSERGETLSR: /* Get line status register */
1300
                        printk("TIOCSERGETLSR\n");
1301
                        return 0;
1302
 
1303
                case TIOCSERGSTRUCT:
1304
                        printk("TIOCSERGSTRUCT\n");
1305
                        return 0;
1306
 
1307
                /*
1308
                 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1309
                 * - mask passed in arg for lines of interest
1310
                 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1311
                 * Caller should use TIOCGICOUNT to see which one it was
1312
                 */
1313
                case TIOCMIWAIT:
1314
                        printk("TIOCMIWAIT\n");
1315
                        return 0;
1316
 
1317
                /*
1318
                 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1319
                 * Return: write counters to the user passed counter struct
1320
                 * NB: both 1->0 and 0->1 transitions are counted except for
1321
                 *     RI where only 0->1 is counted.
1322
                 */
1323
                case TIOCGICOUNT:
1324
                        printk("TIOCGICOUNT\n");
1325
                        return 0;
1326
 
1327
                case TIOCSERGWILD:
1328
                case TIOCSERSWILD:
1329
                        /* "setserial -W" is called in Debian boot */
1330
                        printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1331
                        return 0;
1332
 
1333
                default:
1334
                        return -ENOIOCTLCMD;
1335
                }
1336
        return 0;
1337
}
1338
 
1339
static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1340
{
1341
        struct async_struct *info = (struct async_struct *)tty->driver_data;
1342
        unsigned long flags;
1343
        unsigned int cflag = tty->termios->c_cflag;
1344
 
1345
        if (   (cflag == old_termios->c_cflag)
1346
            && (   RELEVANT_IFLAG(tty->termios->c_iflag)
1347
                == RELEVANT_IFLAG(old_termios->c_iflag)))
1348
          return;
1349
 
1350
        change_speed(info, old_termios);
1351
 
1352
        /* Handle transition to B0 status */
1353
        if ((old_termios->c_cflag & CBAUD) &&
1354
            !(cflag & CBAUD)) {
1355
                save_flags(flags); cli();
1356
                sio_reg(info)->flcr |= TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE;
1357
                restore_flags(flags);
1358
        }
1359
 
1360
        /* Handle transition away from B0 status */
1361
        if (!(old_termios->c_cflag & CBAUD) &&
1362
            (cflag & CBAUD)) {
1363
                if (!(tty->termios->c_cflag & CRTSCTS) ||
1364
                    !test_bit(TTY_THROTTLED, &tty->flags)) {
1365
                        save_flags(flags); cli();
1366
                        sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE);
1367
                        restore_flags(flags);
1368
                }
1369
        }
1370
 
1371
        /* Handle turning off CRTSCTS */
1372
        if ((old_termios->c_cflag & CRTSCTS) &&
1373
            !(tty->termios->c_cflag & CRTSCTS)) {
1374
                tty->hw_stopped = 0;
1375
                rs_start(tty);
1376
        }
1377
}
1378
 
1379
/*
1380
 * ------------------------------------------------------------
1381
 * rs_close()
1382
 *
1383
 * This routine is called when the serial port gets closed.  First, we
1384
 * wait for the last remaining data to be sent.  Then, we unlink its
1385
 * async structure from the interrupt chain if necessary, and we free
1386
 * that IRQ if nothing is left in the chain.
1387
 * ------------------------------------------------------------
1388
 */
1389
static void rs_close(struct tty_struct *tty, struct file * filp)
1390
{
1391
        struct async_struct * info = (struct async_struct *)tty->driver_data;
1392
        struct serial_state *state;
1393
        unsigned long flags;
1394
 
1395
        if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1396
                return;
1397
 
1398
        state = info->state;
1399
 
1400
        save_flags(flags); cli();
1401
 
1402
        if (tty_hung_up_p(filp)) {
1403
                restore_flags(flags);
1404
                return;
1405
        }
1406
 
1407
#ifdef SERIAL_DEBUG_OPEN
1408
        printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1409
#endif
1410
        if ((tty->count == 1) && (state->count != 1)) {
1411
                /*
1412
                 * Uh, oh.  tty->count is 1, which means that the tty
1413
                 * structure will be freed.  state->count should always
1414
                 * be one in these conditions.  If it's greater than
1415
                 * one, we've got real problems, since it means the
1416
                 * serial port won't be shutdown.
1417
                 */
1418
                printk(KERN_WARNING "rs_close: bad serial port count; tty->count is 1, "
1419
                       "state->count is %d\n", state->count);
1420
                state->count = 1;
1421
        }
1422
        if (--state->count < 0) {
1423
                printk(KERN_WARNING "rs_close: bad serial port count for ttys%d: %d\n",
1424
                       info->line, state->count);
1425
                state->count = 0;
1426
        }
1427
        if (state->count) {
1428
                restore_flags(flags);
1429
                return;
1430
        }
1431
        info->flags |= ASYNC_CLOSING;
1432
        /*
1433
         * Save the termios structure, since this port may have
1434
         * separate termios for callout and dialin.
1435
         */
1436
        if (info->flags & ASYNC_NORMAL_ACTIVE)
1437
                info->state->normal_termios = *tty->termios;
1438
        if (info->flags & ASYNC_CALLOUT_ACTIVE)
1439
                info->state->callout_termios = *tty->termios;
1440
        /*
1441
         * Now we wait for the transmit buffer to clear; and we notify
1442
         * the line discipline to only process XON/XOFF characters.
1443
         */
1444
        tty->closing = 1;
1445
        if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1446
                tty_wait_until_sent(tty, info->closing_wait);
1447
        /*
1448
         * At this point we stop accepting input.  To do this, we
1449
         * disable the receive line status interrupts, and tell the
1450
         * interrupt driver to stop checking the data ready bit in the
1451
         * line status register.
1452
         */
1453
        info->read_status_mask &= ~TXx927_SIDISR_RDIS;
1454
        if (info->flags & ASYNC_INITIALIZED) {
1455
#if 0
1456
                sio_reg(info)->dicr &= ~TXx927_SIDICR_RIE;
1457
#endif
1458
                /*
1459
                 * Before we drop DTR, make sure the UART transmitter
1460
                 * has completely drained; this is especially
1461
                 * important if there is a transmit FIFO!
1462
                 */
1463
                rs_wait_until_sent(tty, info->timeout);
1464
        }
1465
        shutdown(info);
1466
        if (tty->driver.flush_buffer)
1467
                tty->driver.flush_buffer(tty);
1468
        if (tty->ldisc.flush_buffer)
1469
                tty->ldisc.flush_buffer(tty);
1470
        tty->closing = 0;
1471
        info->event = 0;
1472
        info->tty = 0;
1473
        if (info->blocked_open) {
1474
                if (info->close_delay) {
1475
                        current->state = TASK_INTERRUPTIBLE;
1476
                        schedule_timeout(info->close_delay);
1477
                }
1478
                wake_up_interruptible(&info->open_wait);
1479
        }
1480
        info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1481
                         ASYNC_CLOSING);
1482
        wake_up_interruptible(&info->close_wait);
1483
        restore_flags(flags);
1484
}
1485
 
1486
/*
1487
 * rs_wait_until_sent() --- wait until the transmitter is empty
1488
 */
1489
static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1490
{
1491
        struct async_struct * info = (struct async_struct *)tty->driver_data;
1492
        unsigned long orig_jiffies, char_time;
1493
        int cisr;
1494
 
1495
        if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1496
                return;
1497
 
1498
        if (info->xmit_fifo_size == 0)
1499
                return; /* Just in case.... */
1500
 
1501
        orig_jiffies = jiffies;
1502
        /*
1503
         * Set the check interval to be 1/5 of the estimated time to
1504
         * send a single character, and make it at least 1.  The check
1505
         * interval should also be less than the timeout.
1506
         *
1507
         * Note: we have to use pretty tight timings here to satisfy
1508
         * the NIST-PCTS.
1509
         */
1510
        char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1511
        char_time = char_time / 5;
1512
        if (char_time == 0)
1513
                char_time = 1;
1514
        if (timeout)
1515
          char_time = MIN(char_time, timeout);
1516
        /*
1517
         * If the transmitter hasn't cleared in twice the approximate
1518
         * amount of time to send the entire FIFO, it probably won't
1519
         * ever clear.  This assumes the UART isn't doing flow
1520
         * control, which is currently the case.  Hence, if it ever
1521
         * takes longer than info->timeout, this is probably due to a
1522
         * UART bug of some kind.  So, we clamp the timeout parameter at
1523
         * 2*info->timeout.
1524
         */
1525
        if (!timeout || timeout > 2*info->timeout)
1526
                timeout = 2*info->timeout;
1527
#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1528
        printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1529
        printk("jiff=%lu...", jiffies);
1530
#endif
1531
        while (!((cisr = sio_reg(info)->cisr) & TXx927_SICISR_TXALS)) {
1532
#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1533
                printk("cisr = %d (jiff=%lu)...", cisr, jiffies);
1534
#endif
1535
                current->state = TASK_INTERRUPTIBLE;
1536
                current->counter = 0;    /* make us low-priority */
1537
                schedule_timeout(char_time);
1538
                if (signal_pending(current))
1539
                        break;
1540
                if (timeout && time_after(jiffies, orig_jiffies + timeout))
1541
                        break;
1542
        }
1543
        current->state = TASK_RUNNING;
1544
#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1545
        printk("cisr = %d (jiff=%lu)...done\n", cisr, jiffies);
1546
#endif
1547
}
1548
 
1549
/*
1550
 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1551
 */
1552
static void rs_hangup(struct tty_struct *tty)
1553
{
1554
        struct async_struct * info = (struct async_struct *)tty->driver_data;
1555
        struct serial_state *state = info->state;
1556
 
1557
        if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1558
                return;
1559
 
1560
        state = info->state;
1561
 
1562
        rs_flush_buffer(tty);
1563
        shutdown(info);
1564
        info->event = 0;
1565
        state->count = 0;
1566
        info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1567
        info->tty = 0;
1568
        wake_up_interruptible(&info->open_wait);
1569
}
1570
 
1571
/*
1572
 * ------------------------------------------------------------
1573
 * rs_open() and friends
1574
 * ------------------------------------------------------------
1575
 */
1576
static int block_til_ready(struct tty_struct *tty, struct file * filp,
1577
                           struct async_struct *info)
1578
{
1579
        DECLARE_WAITQUEUE(wait, current);
1580
        struct serial_state *state = info->state;
1581
        int             retval;
1582
        int             do_clocal = 0, extra_count = 0;
1583
        unsigned long   flags;
1584
 
1585
        /*
1586
         * If the device is in the middle of being closed, then block
1587
         * until it's done, and then try again.
1588
         */
1589
        if (tty_hung_up_p(filp) ||
1590
            (info->flags & ASYNC_CLOSING)) {
1591
                if (info->flags & ASYNC_CLOSING)
1592
                        interruptible_sleep_on(&info->close_wait);
1593
#ifdef SERIAL_DO_RESTART
1594
                return ((info->flags & ASYNC_HUP_NOTIFY) ?
1595
                        -EAGAIN : -ERESTARTSYS);
1596
#else
1597
                return -EAGAIN;
1598
#endif
1599
        }
1600
 
1601
        /*
1602
         * If this is a callout device, then just make sure the normal
1603
         * device isn't being used.
1604
         */
1605
        if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1606
                if (info->flags & ASYNC_NORMAL_ACTIVE)
1607
                        return -EBUSY;
1608
                if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1609
                    (info->flags & ASYNC_SESSION_LOCKOUT) &&
1610
                    (info->session != current->session))
1611
                    return -EBUSY;
1612
                if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1613
                    (info->flags & ASYNC_PGRP_LOCKOUT) &&
1614
                    (info->pgrp != current->pgrp))
1615
                    return -EBUSY;
1616
                info->flags |= ASYNC_CALLOUT_ACTIVE;
1617
                return 0;
1618
        }
1619
 
1620
        /*
1621
         * If non-blocking mode is set, or the port is not enabled,
1622
         * then make the check up front and then exit.
1623
         */
1624
        if ((filp->f_flags & O_NONBLOCK) ||
1625
            (tty->flags & (1 << TTY_IO_ERROR))) {
1626
                if (info->flags & ASYNC_CALLOUT_ACTIVE)
1627
                        return -EBUSY;
1628
                info->flags |= ASYNC_NORMAL_ACTIVE;
1629
                return 0;
1630
        }
1631
 
1632
        if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1633
                if (state->normal_termios.c_cflag & CLOCAL)
1634
                        do_clocal = 1;
1635
        } else {
1636
                if (tty->termios->c_cflag & CLOCAL)
1637
                        do_clocal = 1;
1638
        }
1639
 
1640
        /*
1641
         * Block waiting for the carrier detect and the line to become
1642
         * free (i.e., not in use by the callout).  While we are in
1643
         * this loop, state->count is dropped by one, so that
1644
         * rs_close() knows when to free things.  We restore it upon
1645
         * exit, either normal or abnormal.
1646
         */
1647
        retval = 0;
1648
        add_wait_queue(&info->open_wait, &wait);
1649
#ifdef SERIAL_DEBUG_OPEN
1650
        printk("block_til_ready before block: ttys%d, count = %d\n",
1651
               state->line, state->count);
1652
#endif
1653
        save_flags(flags); cli();
1654
        if (!tty_hung_up_p(filp)) {
1655
                extra_count = 1;
1656
                state->count--;
1657
        }
1658
        restore_flags(flags);
1659
        info->blocked_open++;
1660
        while (1) {
1661
                save_flags(flags); cli();
1662
                if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1663
                    (tty->termios->c_cflag & CBAUD))
1664
                        sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RTSSC|TXx927_SIFLCR_RSDE);
1665
                restore_flags(flags);
1666
                current->state = TASK_INTERRUPTIBLE;
1667
                if (tty_hung_up_p(filp) ||
1668
                    !(info->flags & ASYNC_INITIALIZED)) {
1669
#ifdef SERIAL_DO_RESTART
1670
                        if (info->flags & ASYNC_HUP_NOTIFY)
1671
                                retval = -EAGAIN;
1672
                        else
1673
                                retval = -ERESTARTSYS;
1674
#else
1675
                        retval = -EAGAIN;
1676
#endif
1677
                        break;
1678
                }
1679
                if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1680
                    !(info->flags & ASYNC_CLOSING))
1681
                        break;
1682
                if (signal_pending(current)) {
1683
                        retval = -ERESTARTSYS;
1684
                        break;
1685
                }
1686
#ifdef SERIAL_DEBUG_OPEN
1687
                printk("block_til_ready blocking: ttys%d, count = %d\n",
1688
                       info->line, state->count);
1689
#endif
1690
                schedule();
1691
        }
1692
        current->state = TASK_RUNNING;
1693
        remove_wait_queue(&info->open_wait, &wait);
1694
        if (extra_count)
1695
                state->count++;
1696
        info->blocked_open--;
1697
#ifdef SERIAL_DEBUG_OPEN
1698
        printk("block_til_ready after blocking: ttys%d, count = %d\n",
1699
               info->line, state->count);
1700
#endif
1701
        if (retval)
1702
                return retval;
1703
        info->flags |= ASYNC_NORMAL_ACTIVE;
1704
        return 0;
1705
}
1706
 
1707
static int get_async_struct(int line, struct async_struct **ret_info)
1708
{
1709
        struct async_struct *info;
1710
        struct serial_state *sstate;
1711
 
1712
#ifdef REMOTE_DEBUG
1713
        if (kdb_port_info.state && line == kdb_port_info.line)
1714
                return -ENODEV;
1715
#endif
1716
        sstate = rs_table + line;
1717
        sstate->count++;
1718
        if (sstate->info) {
1719
                *ret_info = sstate->info;
1720
                return 0;
1721
        }
1722
        info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
1723
        if (!info) {
1724
                sstate->count--;
1725
                return -ENOMEM;
1726
        }
1727
        memset(info, 0, sizeof(struct async_struct));
1728
        init_waitqueue_head(&info->open_wait);
1729
        init_waitqueue_head(&info->close_wait);
1730
        init_waitqueue_head(&info->delta_msr_wait);
1731
        info->magic = SERIAL_MAGIC;
1732
        info->port = sstate->port;
1733
        info->flags = sstate->flags;
1734
        info->io_type = sstate->io_type;
1735
        info->xmit_fifo_size = sstate->xmit_fifo_size;
1736
        info->line = line;
1737
        info->tqueue.routine = do_softint;
1738
        info->tqueue.data = info;
1739
        info->state = sstate;
1740
        if (sstate->info) {
1741
                kfree(info);
1742
                *ret_info = sstate->info;
1743
                return 0;
1744
        }
1745
        *ret_info = sstate->info = info;
1746
        return 0;
1747
}
1748
 
1749
/*
1750
 * This routine is called whenever a serial port is opened.  It
1751
 * enables interrupts for a serial port, linking in its async structure into
1752
 * the IRQ chain.   It also performs the serial-specific
1753
 * initialization for the tty structure.
1754
 */
1755
static int rs_open(struct tty_struct *tty, struct file * filp)
1756
{
1757
        struct async_struct     *info;
1758
        int                     retval, line;
1759
        unsigned long           page;
1760
 
1761
        line = MINOR(tty->device) - tty->driver.minor_start;
1762
        if ((line < 0) || (line >= NR_PORTS)) {
1763
                return -ENODEV;
1764
        }
1765
        retval = get_async_struct(line, &info);
1766
        if (retval) {
1767
                return retval;
1768
        }
1769
        tty->driver_data = info;
1770
        info->tty = tty;
1771
 
1772
#ifdef SERIAL_DEBUG_OPEN
1773
        printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1774
               info->state->count);
1775
#endif
1776
        info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1777
 
1778
        if (!tmp_buf) {
1779
                page = get_free_page(GFP_KERNEL);
1780
                if (!page) {
1781
                        return -ENOMEM;
1782
                }
1783
                if (tmp_buf)
1784
                        free_page(page);
1785
                else
1786
                        tmp_buf = (unsigned char *) page;
1787
        }
1788
 
1789
        /*
1790
         * If the port is the middle of closing, bail out now
1791
         */
1792
        if (tty_hung_up_p(filp) ||
1793
            (info->flags & ASYNC_CLOSING)) {
1794
                if (info->flags & ASYNC_CLOSING)
1795
                        interruptible_sleep_on(&info->close_wait);
1796
#ifdef SERIAL_DO_RESTART
1797
                return ((info->flags & ASYNC_HUP_NOTIFY) ?
1798
                        -EAGAIN : -ERESTARTSYS);
1799
#else
1800
                return -EAGAIN;
1801
#endif
1802
        }
1803
 
1804
        /*
1805
         * Start up serial port
1806
         */
1807
        retval = startup(info);
1808
        if (retval) {
1809
                return retval;
1810
        }
1811
 
1812
        retval = block_til_ready(tty, filp, info);
1813
        if (retval) {
1814
#ifdef SERIAL_DEBUG_OPEN
1815
                printk("rs_open returning after block_til_ready with %d\n",
1816
                       retval);
1817
#endif
1818
                return retval;
1819
        }
1820
 
1821
        if ((info->state->count == 1) &&
1822
            (info->flags & ASYNC_SPLIT_TERMIOS)) {
1823
                if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1824
                        *tty->termios = info->state->normal_termios;
1825
                else
1826
                        *tty->termios = info->state->callout_termios;
1827
                change_speed(info, 0);
1828
        }
1829
#ifdef CONFIG_TXX927_SERIAL_CONSOLE
1830
        if (sercons.cflag && sercons.index == line) {
1831
                tty->termios->c_cflag = sercons.cflag;
1832
                sercons.cflag = 0;
1833
                change_speed(info, 0);
1834
        }
1835
#endif
1836
        info->session = current->session;
1837
        info->pgrp = current->pgrp;
1838
 
1839
#ifdef SERIAL_DEBUG_OPEN
1840
        printk("rs_open ttys%d successful...", info->line);
1841
#endif
1842
        return 0;
1843
}
1844
 
1845
/*
1846
 * /proc fs routines....
1847
 */
1848
 
1849
static inline int line_info(char *buf, struct serial_state *state)
1850
{
1851
        struct async_struct *info = state->info, scr_info;
1852
        char    stat_buf[30];
1853
        int     ret;
1854
        unsigned long flags;
1855
 
1856
        ret = sprintf(buf, "%d: uart:%s port:%lX irq:%d",
1857
                      state->line, SERIAL_DRIVER_NAME,
1858
                      state->port, state->irq);
1859
 
1860
        if (!state->port) {
1861
                ret += sprintf(buf+ret, "\n");
1862
                return ret;
1863
        }
1864
 
1865
        /*
1866
         * Figure out the current RS-232 lines
1867
         */
1868
        if (!info) {
1869
                info = &scr_info;       /* This is just for serial_{in,out} */
1870
 
1871
                info->magic = SERIAL_MAGIC;
1872
                info->port = state->port;
1873
                info->flags = state->flags;
1874
                info->quot = 0;
1875
                info->tty = 0;
1876
        }
1877
 
1878
        stat_buf[0] = 0;
1879
        stat_buf[1] = 0;
1880
        save_flags(flags); cli();
1881
        if (!(sio_reg(info)->flcr & TXx927_SIFLCR_RTSSC))
1882
                strcat(stat_buf, "|RTS");
1883
        if (!(sio_reg(info)->cisr & TXx927_SICISR_CTSS))
1884
                strcat(stat_buf, "|CTS");
1885
        restore_flags(flags);
1886
 
1887
        if (info->quot) {
1888
                ret += sprintf(buf+ret, " baud:%d",
1889
                               state->baud_base / info->quot);
1890
        }
1891
 
1892
        ret += sprintf(buf+ret, " tx:%d rx:%d",
1893
                      state->icount.tx, state->icount.rx);
1894
 
1895
        if (state->icount.frame)
1896
                ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
1897
 
1898
        if (state->icount.parity)
1899
                ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
1900
 
1901
        if (state->icount.brk)
1902
                ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
1903
 
1904
        if (state->icount.overrun)
1905
                ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
1906
 
1907
        /*
1908
         * Last thing is the RS-232 status lines
1909
         */
1910
        ret += sprintf(buf+ret, " %s\n", stat_buf+1);
1911
        return ret;
1912
}
1913
 
1914
static int rs_read_proc(char *page, char **start, off_t off, int count,
1915
                 int *eof, void *data)
1916
{
1917
        int i, len = 0, l;
1918
        off_t   begin = 0;
1919
 
1920
        len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
1921
        for (i = 0; i < NR_PORTS && len < 4000; i++) {
1922
                l = line_info(page + len, &rs_table[i]);
1923
                len += l;
1924
                if (len+begin > off+count)
1925
                        goto done;
1926
                if (len+begin < off) {
1927
                        begin += len;
1928
                        len = 0;
1929
                }
1930
        }
1931
        *eof = 1;
1932
done:
1933
        if (off >= len+begin)
1934
                return 0;
1935
        *start = page + (begin-off);
1936
        return ((count < begin+len-off) ? count : begin+len-off);
1937
}
1938
 
1939
/*
1940
 * ---------------------------------------------------------------------
1941
 * rs_init() and friends
1942
 *
1943
 * rs_init() is called at boot-time to initialize the serial driver.
1944
 * ---------------------------------------------------------------------
1945
 */
1946
 
1947
/*
1948
 * This routine prints out the appropriate serial driver version
1949
 * number, and identifies which options were configured into this
1950
 * driver.
1951
 */
1952
static _INLINE_ void show_serial_version(void)
1953
{
1954
        printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1955
}
1956
 
1957
/*
1958
 * The serial driver boot-time initialization code!
1959
 */
1960
static int __init rs_init(void)
1961
{
1962
        int i;
1963
        struct serial_state * state;
1964
 
1965
        if (rs_table[0].port == 0)
1966
                return -ENODEV;
1967
 
1968
        init_bh(TXX927_SERIAL_BH, do_serial_bh);
1969
        init_timer(&serial_timer);
1970
        serial_timer.function = rs_timer;
1971
        mod_timer(&serial_timer, jiffies + RS_STROBE_TIME);
1972
 
1973
        for (i = 0; i < NR_IRQS; i++) {
1974
                IRQ_ports[i] = 0;
1975
                IRQ_timeout[i] = 0;
1976
        }
1977
#ifdef CONFIG_TXX927_SERIAL_CONSOLE
1978
        /*
1979
         *      The interrupt of the serial console port
1980
         *      can't be shared.
1981
         */
1982
        if (sercons.flags & CON_CONSDEV) {
1983
                for(i = 0; i < NR_PORTS; i++)
1984
                        if (i != sercons.index &&
1985
                            rs_table[i].irq == rs_table[sercons.index].irq)
1986
                                rs_table[i].irq = 0;
1987
        }
1988
#endif
1989
        show_serial_version();
1990
 
1991
        /* Initialize the tty_driver structure */
1992
 
1993
        memset(&serial_driver, 0, sizeof(struct tty_driver));
1994
        serial_driver.magic = TTY_DRIVER_MAGIC;
1995
        serial_driver.driver_name = "txx927serial";
1996
#if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1997
        serial_driver.name = "tts/%d";
1998
#else
1999
        serial_driver.name = "ttyS";
2000
#endif
2001
        serial_driver.major = TXX927_TTY_MAJOR;
2002
        serial_driver.minor_start = TXX927_TTY_MINOR_START + SERIAL_DEV_OFFSET;
2003
        serial_driver.num = NR_PORTS;
2004
        serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2005
        serial_driver.subtype = SERIAL_TYPE_NORMAL;
2006
        serial_driver.init_termios = tty_std_termios;
2007
        serial_driver.init_termios.c_cflag =
2008
                B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2009
        serial_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2010
        serial_driver.refcount = &serial_refcount;
2011
        serial_driver.table = serial_table;
2012
        serial_driver.termios = serial_termios;
2013
        serial_driver.termios_locked = serial_termios_locked;
2014
 
2015
        serial_driver.open = rs_open;
2016
        serial_driver.close = rs_close;
2017
        serial_driver.write = rs_write;
2018
        serial_driver.put_char = rs_put_char;
2019
        serial_driver.flush_chars = rs_flush_chars;
2020
        serial_driver.write_room = rs_write_room;
2021
        serial_driver.chars_in_buffer = rs_chars_in_buffer;
2022
        serial_driver.flush_buffer = rs_flush_buffer;
2023
        serial_driver.ioctl = rs_ioctl;
2024
        serial_driver.throttle = rs_throttle;
2025
        serial_driver.unthrottle = rs_unthrottle;
2026
        serial_driver.send_xchar = rs_send_xchar;
2027
        serial_driver.set_termios = rs_set_termios;
2028
        serial_driver.stop = rs_stop;
2029
        serial_driver.start = rs_start;
2030
        serial_driver.hangup = rs_hangup;
2031
        serial_driver.break_ctl = rs_break;
2032
        serial_driver.wait_until_sent = rs_wait_until_sent;
2033
        serial_driver.read_proc = rs_read_proc;
2034
 
2035
        /*
2036
         * The callout device is just like normal device except for
2037
         * major number and the subtype code.
2038
         */
2039
        callout_driver = serial_driver;
2040
#if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
2041
                callout_driver.name = "cua/%d";
2042
#else
2043
                callout_driver.name = "cua";
2044
#endif                               
2045
        callout_driver.major = TTYAUX_MAJOR;
2046
        callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2047
        callout_driver.read_proc = 0;
2048
        callout_driver.proc_entry = 0;
2049
 
2050
        if (tty_register_driver(&serial_driver)){
2051
                panic("Couldn't register serial driver\n");
2052
        }
2053
        if (tty_register_driver(&callout_driver)) {
2054
                panic("Couldn't register callout driver\n");
2055
        }
2056
 
2057
        for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2058
                state->magic = SSTATE_MAGIC;
2059
                state->line = i;
2060
                state->type = PORT_UNKNOWN;
2061
                state->custom_divisor = 0;
2062
                state->close_delay = 5*HZ/10;
2063
                state->closing_wait = 30*HZ;
2064
                state->callout_termios = callout_driver.init_termios;
2065
                state->normal_termios = serial_driver.init_termios;
2066
                state->icount.cts = state->icount.dsr =
2067
                        state->icount.rng = state->icount.dcd = 0;
2068
                state->icount.rx = state->icount.tx = 0;
2069
                state->icount.frame = state->icount.parity = 0;
2070
                state->icount.overrun = state->icount.brk = 0;
2071
                state->irq = irq_cannonicalize(state->irq);
2072
                state->xmit_fifo_size = TXx927_SIO_TX_FIFO;
2073
                if (state->hub6)
2074
                        state->io_type = SERIAL_IO_HUB6;
2075
                if (state->port) {
2076
                        continue;
2077
                }
2078
        }
2079
 
2080
        for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2081
                if (state->type == PORT_UNKNOWN) {
2082
                       continue;
2083
                }
2084
                printk(KERN_INFO "%s%02d at 0x%04lx (irq = %d) is a %s\n",
2085
                       TXX927_TTY_NAME,
2086
                       state->line,
2087
                       state->port, state->irq,
2088
                       SERIAL_DRIVER_NAME);
2089
                tty_register_devfs(&serial_driver, 0,
2090
                                serial_driver.minor_start + state->line);
2091
                tty_register_devfs(&callout_driver, 0,
2092
                                callout_driver.minor_start + state->line);
2093
        }
2094
        return 0;
2095
}
2096
 
2097
static void __exit rs_fini(void)
2098
{
2099
        unsigned long flags;
2100
        int e1, e2;
2101
        int i;
2102
        struct async_struct *info;
2103
 
2104
        del_timer_sync(&serial_timer);
2105
        save_flags(flags); cli();
2106
        remove_bh(TXX927_SERIAL_BH);
2107
        if ((e1 = tty_unregister_driver(&serial_driver)))
2108
                printk(KERN_WARNING "serial: failed to unregister serial driver (%d)\n",
2109
                       e1);
2110
        if ((e2 = tty_unregister_driver(&callout_driver)))
2111
                printk(KERN_WARNING "serial: failed to unregister callout driver (%d)\n",
2112
                       e2);
2113
        restore_flags(flags);
2114
 
2115
        for (i = 0; i < NR_PORTS; i++) {
2116
                if ((info = rs_table[i].info)) {
2117
                        rs_table[i].info = NULL;
2118
                        kfree(info);
2119
                }
2120
        }
2121
        if (tmp_buf) {
2122
                unsigned long pg = (unsigned long) tmp_buf;
2123
                tmp_buf = NULL;
2124
                free_page(pg);
2125
        }
2126
}
2127
 
2128
module_init(rs_init);
2129
module_exit(rs_fini);
2130
MODULE_DESCRIPTION("TXX927 serial driver");
2131
 
2132
/*
2133
 * ------------------------------------------------------------
2134
 * Serial console driver
2135
 * ------------------------------------------------------------
2136
 */
2137
#ifdef CONFIG_TXX927_SERIAL_CONSOLE
2138
 
2139
static struct async_struct async_sercons;
2140
 
2141
/*
2142
 *      Print a string to the serial port trying not to disturb
2143
 *      any possible real use of the port...
2144
 */
2145
static void serial_console_write(struct console *co, const char *s,
2146
                                unsigned count)
2147
{
2148
        static struct async_struct *info = &async_sercons;
2149
        int ier;
2150
        unsigned i;
2151
 
2152
        /*
2153
         *      First save the IER then disable the interrupts
2154
         */
2155
        ier = sio_reg(info)->dicr;
2156
        sio_reg(info)->dicr = 0;
2157
 
2158
 
2159
        /*
2160
         *      Now, do each character
2161
         */
2162
        for (i = 0; i < count; i++, s++) {
2163
                wait_for_xmitr(info);
2164
 
2165
                /*
2166
                 *      Send the character out.
2167
                 *      If a LF, also do CR...
2168
                 */
2169
                sio_reg(info)->tfifo = *s;
2170
                if (*s == 10) {
2171
                        wait_for_xmitr(info);
2172
                        sio_reg(info)->tfifo = 13;
2173
                }
2174
        }
2175
 
2176
        /*
2177
         *      Finally, Wait for transmitter & holding register to empty
2178
         *      and restore the IER
2179
         */
2180
        wait_for_xmitr(info);
2181
        sio_reg(info)->dicr = ier;
2182
}
2183
 
2184
static kdev_t serial_console_device(struct console *c)
2185
{
2186
        return MKDEV(TXX927_TTY_MAJOR, TXX927_TTY_MINOR_START + c->index);
2187
}
2188
 
2189
/*
2190
 *      Setup initial baud/bits/parity. We do two things here:
2191
 *      - construct a cflag setting for the first rs_open()
2192
 *      - initialize the serial port
2193
 *      Return non-zero if we didn't find a serial port.
2194
 */
2195
static int serial_console_setup(struct console *co, char *options)
2196
{
2197
        static struct async_struct *info;
2198
        struct serial_state *state;
2199
        unsigned cval;
2200
        int     baud = 9600;
2201
        int     bits = 8;
2202
        int     parity = 'n';
2203
        int     cflag = CREAD | HUPCL | CLOCAL;
2204
        int     quot = 0;
2205
        char    *s;
2206
 
2207
        if (co->index < 0 || co->index >= NR_PORTS) {
2208
                return -1;
2209
        }
2210
        if (options) {
2211
                baud = simple_strtoul(options, NULL, 10);
2212
                s = options;
2213
                while(*s >= '0' && *s <= '9')
2214
                        s++;
2215
                if (*s) parity = *s++;
2216
                if (*s) bits   = *s - '0';
2217
        }
2218
 
2219
        /*
2220
         *      Now construct a cflag setting.
2221
         */
2222
        switch(baud) {
2223
                case 1200:
2224
                        cflag |= B1200;
2225
                        break;
2226
                case 2400:
2227
                        cflag |= B2400;
2228
                        break;
2229
                case 4800:
2230
                        cflag |= B4800;
2231
                        break;
2232
                case 19200:
2233
                        cflag |= B19200;
2234
                        break;
2235
                case 38400:
2236
                        cflag |= B38400;
2237
                        break;
2238
                case 57600:
2239
                        cflag |= B57600;
2240
                        break;
2241
                case 115200:
2242
                        cflag |= B115200;
2243
                        break;
2244
                case 9600:
2245
                default:
2246
                        cflag |= B9600;
2247
                        break;
2248
        }
2249
        switch(bits) {
2250
                case 7:
2251
                        cflag |= CS7;
2252
                        break;
2253
                default:
2254
                case 8:
2255
                        cflag |= CS8;
2256
                        break;
2257
        }
2258
        switch(parity) {
2259
                case 'o': case 'O':
2260
                        cflag |= PARODD;
2261
                        break;
2262
                case 'e': case 'E':
2263
                        cflag |= PARENB;
2264
                        break;
2265
        }
2266
        co->cflag = cflag;
2267
 
2268
        /*
2269
         *      Divisor, bytesize and parity
2270
         */
2271
        state = rs_table + co->index;
2272
        info = &async_sercons;
2273
        info->magic = SERIAL_MAGIC;
2274
        info->state = state;
2275
        info->port = state->port;
2276
        info->flags = state->flags;
2277
        info->io_type = state->io_type;
2278
        info->iomem_base = state->iomem_base;
2279
        info->iomem_reg_shift = state->iomem_reg_shift;
2280
        quot = state->baud_base / baud;
2281
 
2282
        switch (cflag & CSIZE) {
2283
        case CS7: cval = TXx927_SILCR_UMODE_7BIT; break;
2284
        default:
2285
        case CS8: cval = TXx927_SILCR_UMODE_8BIT; break;
2286
        }
2287
        if (cflag & CSTOPB)
2288
                cval |= TXx927_SILCR_USBL_2BIT;
2289
        else
2290
                cval |= TXx927_SILCR_USBL_1BIT;
2291
        if (cflag & PARENB)
2292
                cval |= TXx927_SILCR_UPEN;
2293
        if (!(cflag & PARODD))
2294
                cval |= TXx927_SILCR_UEPS;
2295
 
2296
        /*
2297
         *      Disable UART interrupts, set DTR and RTS high
2298
         *      and set speed.
2299
         */
2300
        sio_reg(info)->dicr = 0;
2301
        sio_reg(info)->lcr = cval | TXx927_SILCR_SCS_IMCLK_BG;
2302
        sio_reg(info)->bgr = quot | TXx927_SIBGR_BCLK_T0;
2303
        /* HW RTS/CTS control */
2304
        if (info->flags & ASYNC_HAVE_CTS_LINE)
2305
                sio_reg(info)->flcr = TXx927_SIFLCR_RCS | TXx927_SIFLCR_TES |
2306
                        TXx927_SIFLCR_RTSTL_MAX /* 15 */;
2307
        /* Enable RX/TX */
2308
        sio_reg(info)->flcr &= ~(TXx927_SIFLCR_RSDE | TXx927_SIFLCR_TSDE);
2309
 
2310
        return 0;
2311
}
2312
 
2313
static struct console sercons = {
2314
        name:           TXX927_TTY_NAME,
2315
        write:          serial_console_write,
2316
        device:         serial_console_device,
2317
        setup:          serial_console_setup,
2318
        flags:          CON_PRINTBUFFER,
2319
        index:          -1,
2320
};
2321
 
2322
/*
2323
 *      Register console.
2324
 */
2325
void __init txx927_console_init(void)
2326
{
2327
        register_console(&sercons);
2328
}
2329
#endif

powered by: WebSVN 2.1.0

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