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

Subversion Repositories or1k

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

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

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *  esp.c - driver for Hayes ESP serial cards
3
 *
4
 *  --- Notices from serial.c, upon which this driver is based ---
5
 *
6
 *  Copyright (C) 1991, 1992  Linus Torvalds
7
 *
8
 *  Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92.  Now
9
 *  much more extensible to support other serial cards based on the
10
 *  16450/16550A UART's.  Added support for the AST FourPort and the
11
 *  Accent Async board.
12
 *
13
 *  set_serial_info fixed to set the flags, custom divisor, and uart
14
 *      type fields.  Fix suggested by Michael K. Johnson 12/12/92.
15
 *
16
 *  11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
17
 *
18
 *  03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
19
 *
20
 *  rs_set_termios fixed to look also for changes of the input
21
 *      flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
22
 *                                            Bernd Anhäupl 05/17/96.
23
 *
24
 * --- End of notices from serial.c ---
25
 *
26
 * Support for the ESP serial card by Andrew J. Robinson
27
 *     <arobinso@nyx.net> (Card detection routine taken from a patch
28
 *     by Dennis J. Boylan).  Patches to allow use with 2.1.x contributed
29
 *     by Chris Faylor.
30
 *
31
 * Most recent changes: (Andrew J. Robinson)
32
 *   Support for PIO mode.  This allows the driver to work properly with
33
 *     multiport cards.
34
 *
35
 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> -
36
 * several cleanups, use module_init/module_exit, etc
37
 *
38
 * This module exports the following rs232 io functions:
39
 *
40
 *      int espserial_init(void);
41
 */
42
 
43
#include <linux/module.h>
44
#include <linux/errno.h>
45
#include <linux/signal.h>
46
#include <linux/sched.h>
47
#include <linux/interrupt.h>
48
#include <linux/tty.h>
49
#include <linux/tty_flip.h>
50
#include <linux/serial.h>
51
#include <linux/serialP.h>
52
#include <linux/serial_reg.h>
53
#include <linux/major.h>
54
#include <linux/string.h>
55
#include <linux/fcntl.h>
56
#include <linux/ptrace.h>
57
#include <linux/ioport.h>
58
#include <linux/mm.h>
59
#include <linux/init.h>
60
 
61
#include <asm/system.h>
62
#include <asm/io.h>
63
#include <asm/segment.h>
64
#include <asm/bitops.h>
65
 
66
#include <asm/dma.h>
67
#include <linux/slab.h>
68
#include <asm/uaccess.h>
69
 
70
#include <linux/hayesesp.h>
71
 
72
#define NR_PORTS 64     /* maximum number of ports */
73
#define NR_PRIMARY 8    /* maximum number of primary ports */
74
 
75
/* The following variables can be set by giving module options */
76
static int irq[NR_PRIMARY];     /* IRQ for each base port */
77
static unsigned int divisor[NR_PRIMARY]; /* custom divisor for each port */
78
static unsigned int dma = ESP_DMA_CHANNEL; /* DMA channel */
79
static unsigned int rx_trigger = ESP_RX_TRIGGER;
80
static unsigned int tx_trigger = ESP_TX_TRIGGER;
81
static unsigned int flow_off = ESP_FLOW_OFF;
82
static unsigned int flow_on = ESP_FLOW_ON;
83
static unsigned int rx_timeout = ESP_RX_TMOUT;
84
static unsigned int pio_threshold = ESP_PIO_THRESHOLD;
85
 
86
MODULE_LICENSE("GPL");
87
 
88
MODULE_PARM(irq, "1-8i");
89
MODULE_PARM(divisor, "1-8i");
90
MODULE_PARM(dma, "i");
91
MODULE_PARM(rx_trigger, "i");
92
MODULE_PARM(tx_trigger, "i");
93
MODULE_PARM(flow_off, "i");
94
MODULE_PARM(flow_on, "i");
95
MODULE_PARM(rx_timeout, "i");
96
MODULE_PARM(pio_threshold, "i");
97
 
98
/* END */
99
 
100
static char *dma_buffer;
101
static int dma_bytes;
102
static struct esp_pio_buffer *free_pio_buf;
103
 
104
#define DMA_BUFFER_SZ 1024
105
 
106
#define WAKEUP_CHARS 1024
107
 
108
static char serial_name[] __initdata = "ESP serial driver";
109
static char serial_version[] __initdata = "2.2";
110
 
111
static DECLARE_TASK_QUEUE(tq_esp);
112
 
113
static struct tty_driver esp_driver, esp_callout_driver;
114
static int serial_refcount;
115
 
116
/* serial subtype definitions */
117
#define SERIAL_TYPE_NORMAL      1
118
#define SERIAL_TYPE_CALLOUT     2
119
 
120
/*
121
 * Serial driver configuration section.  Here are the various options:
122
 *
123
 * SERIAL_PARANOIA_CHECK
124
 *              Check the magic number for the esp_structure where
125
 *              ever possible.
126
 */
127
 
128
#undef SERIAL_PARANOIA_CHECK
129
#define SERIAL_DO_RESTART
130
 
131
#undef SERIAL_DEBUG_INTR
132
#undef SERIAL_DEBUG_OPEN
133
#undef SERIAL_DEBUG_FLOW
134
 
135
#define _INLINE_ inline
136
 
137
#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
138
#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
139
 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
140
#else
141
#define DBG_CNT(s)
142
#endif
143
 
144
static struct esp_struct *ports;
145
 
146
static void change_speed(struct esp_struct *info);
147
static void rs_wait_until_sent(struct tty_struct *, int);
148
 
149
/*
150
 * The ESP card has a clock rate of 14.7456 MHz (that is, 2**ESPC_SCALE
151
 * times the normal 1.8432 Mhz clock of most serial boards).
152
 */
153
#define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
154
 
155
/* Standard COM flags (except for COM4, because of the 8514 problem) */
156
#define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
157
 
158
static struct tty_struct *serial_table[NR_PORTS];
159
static struct termios *serial_termios[NR_PORTS];
160
static struct termios *serial_termios_locked[NR_PORTS];
161
 
162
#ifndef MIN
163
#define MIN(a,b)        ((a) < (b) ? (a) : (b))
164
#endif
165
 
166
/*
167
 * tmp_buf is used as a temporary buffer by serial_write.  We need to
168
 * lock it in case the memcpy_fromfs blocks while swapping in a page,
169
 * and some other program tries to do a serial write at the same time.
170
 * Since the lock will only come under contention when the system is
171
 * swapping and available memory is low, it makes sense to share one
172
 * buffer across all the serial ports, since it significantly saves
173
 * memory if large numbers of serial ports are open.
174
 */
175
static unsigned char *tmp_buf;
176
static DECLARE_MUTEX(tmp_buf_sem);
177
 
178
static inline int serial_paranoia_check(struct esp_struct *info,
179
                                        kdev_t device, const char *routine)
180
{
181
#ifdef SERIAL_PARANOIA_CHECK
182
        static const char badmagic[] = KERN_WARNING
183
                "Warning: bad magic number for serial struct (%s) in %s\n";
184
        static const char badinfo[] = KERN_WARNING
185
                "Warning: null esp_struct for (%s) in %s\n";
186
 
187
        if (!info) {
188
                printk(badinfo, kdevname(device), routine);
189
                return 1;
190
        }
191
        if (info->magic != ESP_MAGIC) {
192
                printk(badmagic, kdevname(device), routine);
193
                return 1;
194
        }
195
#endif
196
        return 0;
197
}
198
 
199
static inline unsigned int serial_in(struct esp_struct *info, int offset)
200
{
201
        return inb(info->port + offset);
202
}
203
 
204
static inline void serial_out(struct esp_struct *info, int offset,
205
                              unsigned char value)
206
{
207
        outb(value, info->port+offset);
208
}
209
 
210
/*
211
 * ------------------------------------------------------------
212
 * rs_stop() and rs_start()
213
 *
214
 * This routines are called before setting or resetting tty->stopped.
215
 * They enable or disable transmitter interrupts, as necessary.
216
 * ------------------------------------------------------------
217
 */
218
static void rs_stop(struct tty_struct *tty)
219
{
220
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
221
        unsigned long flags;
222
 
223
        if (serial_paranoia_check(info, tty->device, "rs_stop"))
224
                return;
225
 
226
        save_flags(flags); cli();
227
        if (info->IER & UART_IER_THRI) {
228
                info->IER &= ~UART_IER_THRI;
229
                serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
230
                serial_out(info, UART_ESI_CMD2, info->IER);
231
        }
232
 
233
        restore_flags(flags);
234
}
235
 
236
static void rs_start(struct tty_struct *tty)
237
{
238
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
239
        unsigned long flags;
240
 
241
        if (serial_paranoia_check(info, tty->device, "rs_start"))
242
                return;
243
 
244
        save_flags(flags); cli();
245
        if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
246
                info->IER |= UART_IER_THRI;
247
                serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
248
                serial_out(info, UART_ESI_CMD2, info->IER);
249
        }
250
        restore_flags(flags);
251
}
252
 
253
/*
254
 * ----------------------------------------------------------------------
255
 *
256
 * Here starts the interrupt handling routines.  All of the following
257
 * subroutines are declared as inline and are folded into
258
 * rs_interrupt().  They were separated out for readability's sake.
259
 *
260
 * Note: rs_interrupt() is a "fast" interrupt, which means that it
261
 * runs with interrupts turned off.  People who may want to modify
262
 * rs_interrupt() should try to keep the interrupt handler as fast as
263
 * possible.  After you are done making modifications, it is not a bad
264
 * idea to do:
265
 *
266
 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
267
 *
268
 * and look at the resulting assemble code in serial.s.
269
 *
270
 *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
271
 * -----------------------------------------------------------------------
272
 */
273
 
274
/*
275
 * This routine is used by the interrupt handler to schedule
276
 * processing in the software interrupt portion of the driver.
277
 */
278
static _INLINE_ void rs_sched_event(struct esp_struct *info,
279
                                  int event)
280
{
281
        info->event |= 1 << event;
282
        queue_task(&info->tqueue, &tq_esp);
283
        mark_bh(ESP_BH);
284
}
285
static _INLINE_ struct esp_pio_buffer *get_pio_buffer(void)
286
{
287
        struct esp_pio_buffer *buf;
288
 
289
        if (free_pio_buf) {
290
                buf = free_pio_buf;
291
                free_pio_buf = buf->next;
292
        } else {
293
                buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
294
        }
295
 
296
        return buf;
297
}
298
 
299
static _INLINE_ void release_pio_buffer(struct esp_pio_buffer *buf)
300
{
301
        buf->next = free_pio_buf;
302
        free_pio_buf = buf;
303
}
304
 
305
static _INLINE_ void receive_chars_pio(struct esp_struct *info, int num_bytes)
306
{
307
        struct tty_struct *tty = info->tty;
308
        int i;
309
        struct esp_pio_buffer *pio_buf;
310
        struct esp_pio_buffer *err_buf;
311
        unsigned char status_mask;
312
 
313
        pio_buf = get_pio_buffer();
314
 
315
        if (!pio_buf)
316
                return;
317
 
318
        err_buf = get_pio_buffer();
319
 
320
        if (!err_buf) {
321
                release_pio_buffer(pio_buf);
322
                return;
323
        }
324
 
325
        sti();
326
 
327
        status_mask = (info->read_status_mask >> 2) & 0x07;
328
 
329
        for (i = 0; i < num_bytes - 1; i += 2) {
330
                *((unsigned short *)(pio_buf->data + i)) =
331
                        inw(info->port + UART_ESI_RX);
332
                err_buf->data[i] = serial_in(info, UART_ESI_RWS);
333
                err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
334
                err_buf->data[i] &= status_mask;
335
        }
336
 
337
        if (num_bytes & 0x0001) {
338
                pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
339
                err_buf->data[num_bytes - 1] =
340
                        (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
341
        }
342
 
343
        cli();
344
 
345
        /* make sure everything is still ok since interrupts were enabled */
346
        tty = info->tty;
347
 
348
        if (!tty) {
349
                release_pio_buffer(pio_buf);
350
                release_pio_buffer(err_buf);
351
                info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
352
                return;
353
        }
354
 
355
        status_mask = (info->ignore_status_mask >> 2) & 0x07;
356
 
357
        for (i = 0; i < num_bytes; i++) {
358
                if (!(err_buf->data[i] & status_mask)) {
359
                        *(tty->flip.char_buf_ptr++) = pio_buf->data[i];
360
 
361
                        if (err_buf->data[i] & 0x04) {
362
                                *(tty->flip.flag_buf_ptr++) = TTY_BREAK;
363
 
364
                                if (info->flags & ASYNC_SAK)
365
                                        do_SAK(tty);
366
                        }
367
                        else if (err_buf->data[i] & 0x02)
368
                                *(tty->flip.flag_buf_ptr++) = TTY_FRAME;
369
                        else if (err_buf->data[i] & 0x01)
370
                                *(tty->flip.flag_buf_ptr++) = TTY_PARITY;
371
                        else
372
                                *(tty->flip.flag_buf_ptr++) = 0;
373
 
374
                        tty->flip.count++;
375
                }
376
        }
377
 
378
        queue_task(&tty->flip.tqueue, &tq_timer);
379
 
380
        info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
381
        release_pio_buffer(pio_buf);
382
        release_pio_buffer(err_buf);
383
}
384
 
385
static _INLINE_ void receive_chars_dma(struct esp_struct *info, int num_bytes)
386
{
387
        unsigned long flags;
388
        info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
389
        dma_bytes = num_bytes;
390
        info->stat_flags |= ESP_STAT_DMA_RX;
391
 
392
        flags=claim_dma_lock();
393
        disable_dma(dma);
394
        clear_dma_ff(dma);
395
        set_dma_mode(dma, DMA_MODE_READ);
396
        set_dma_addr(dma, virt_to_bus(dma_buffer));
397
        set_dma_count(dma, dma_bytes);
398
        enable_dma(dma);
399
        release_dma_lock(flags);
400
 
401
        serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
402
}
403
 
404
static _INLINE_ void receive_chars_dma_done(struct esp_struct *info,
405
                                            int status)
406
{
407
        struct tty_struct *tty = info->tty;
408
        int num_bytes;
409
        unsigned long flags;
410
 
411
 
412
        flags=claim_dma_lock();
413
        disable_dma(dma);
414
        clear_dma_ff(dma);
415
 
416
        info->stat_flags &= ~ESP_STAT_DMA_RX;
417
        num_bytes = dma_bytes - get_dma_residue(dma);
418
        release_dma_lock(flags);
419
 
420
        info->icount.rx += num_bytes;
421
 
422
        memcpy(tty->flip.char_buf_ptr, dma_buffer, num_bytes);
423
        tty->flip.char_buf_ptr += num_bytes;
424
        tty->flip.count += num_bytes;
425
        memset(tty->flip.flag_buf_ptr, 0, num_bytes);
426
        tty->flip.flag_buf_ptr += num_bytes;
427
 
428
        if (num_bytes > 0) {
429
                tty->flip.flag_buf_ptr--;
430
 
431
                status &= (0x1c & info->read_status_mask);
432
 
433
                if (status & info->ignore_status_mask) {
434
                        tty->flip.count--;
435
                        tty->flip.char_buf_ptr--;
436
                        tty->flip.flag_buf_ptr--;
437
                } else if (status & 0x10) {
438
                        *tty->flip.flag_buf_ptr = TTY_BREAK;
439
                        (info->icount.brk)++;
440
                        if (info->flags & ASYNC_SAK)
441
                                do_SAK(tty);
442
                } else if (status & 0x08) {
443
                        *tty->flip.flag_buf_ptr = TTY_FRAME;
444
                        (info->icount.frame)++;
445
                }
446
                else if (status & 0x04) {
447
                        *tty->flip.flag_buf_ptr = TTY_PARITY;
448
                        (info->icount.parity)++;
449
                }
450
 
451
                tty->flip.flag_buf_ptr++;
452
 
453
                queue_task(&tty->flip.tqueue, &tq_timer);
454
        }
455
 
456
        if (dma_bytes != num_bytes) {
457
                num_bytes = dma_bytes - num_bytes;
458
                dma_bytes = 0;
459
                receive_chars_dma(info, num_bytes);
460
        } else
461
                dma_bytes = 0;
462
}
463
 
464
static _INLINE_ void transmit_chars_pio(struct esp_struct *info,
465
                                        int space_avail)
466
{
467
        int i;
468
        struct esp_pio_buffer *pio_buf;
469
 
470
        pio_buf = get_pio_buffer();
471
 
472
        if (!pio_buf)
473
                return;
474
 
475
        while (space_avail && info->xmit_cnt) {
476
                if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
477
                        memcpy(pio_buf->data,
478
                               &(info->xmit_buf[info->xmit_tail]),
479
                               space_avail);
480
                } else {
481
                        i = ESP_XMIT_SIZE - info->xmit_tail;
482
                        memcpy(pio_buf->data,
483
                               &(info->xmit_buf[info->xmit_tail]), i);
484
                        memcpy(&(pio_buf->data[i]), info->xmit_buf,
485
                               space_avail - i);
486
                }
487
 
488
                info->xmit_cnt -= space_avail;
489
                info->xmit_tail = (info->xmit_tail + space_avail) &
490
                        (ESP_XMIT_SIZE - 1);
491
 
492
                sti();
493
 
494
                for (i = 0; i < space_avail - 1; i += 2) {
495
                        outw(*((unsigned short *)(pio_buf->data + i)),
496
                             info->port + UART_ESI_TX);
497
                }
498
 
499
                if (space_avail & 0x0001)
500
                        serial_out(info, UART_ESI_TX,
501
                                   pio_buf->data[space_avail - 1]);
502
 
503
                cli();
504
 
505
                if (info->xmit_cnt) {
506
                        serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
507
                        serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
508
                        space_avail = serial_in(info, UART_ESI_STAT1) << 8;
509
                        space_avail |= serial_in(info, UART_ESI_STAT2);
510
 
511
                        if (space_avail > info->xmit_cnt)
512
                                space_avail = info->xmit_cnt;
513
                }
514
        }
515
 
516
        if (info->xmit_cnt < WAKEUP_CHARS) {
517
                rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
518
 
519
#ifdef SERIAL_DEBUG_INTR
520
                printk("THRE...");
521
#endif
522
 
523
                if (info->xmit_cnt <= 0) {
524
                        info->IER &= ~UART_IER_THRI;
525
                        serial_out(info, UART_ESI_CMD1,
526
                                   ESI_SET_SRV_MASK);
527
                        serial_out(info, UART_ESI_CMD2, info->IER);
528
                }
529
        }
530
 
531
        release_pio_buffer(pio_buf);
532
}
533
 
534
static _INLINE_ void transmit_chars_dma(struct esp_struct *info, int num_bytes)
535
{
536
        unsigned long flags;
537
 
538
        dma_bytes = num_bytes;
539
 
540
        if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
541
                memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
542
                       dma_bytes);
543
        } else {
544
                int i = ESP_XMIT_SIZE - info->xmit_tail;
545
                memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
546
                        i);
547
                memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
548
        }
549
 
550
        info->xmit_cnt -= dma_bytes;
551
        info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
552
 
553
        if (info->xmit_cnt < WAKEUP_CHARS) {
554
                rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
555
 
556
#ifdef SERIAL_DEBUG_INTR
557
                printk("THRE...");
558
#endif
559
 
560
                if (info->xmit_cnt <= 0) {
561
                        info->IER &= ~UART_IER_THRI;
562
                        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
563
                        serial_out(info, UART_ESI_CMD2, info->IER);
564
                }
565
        }
566
 
567
        info->stat_flags |= ESP_STAT_DMA_TX;
568
 
569
        flags=claim_dma_lock();
570
        disable_dma(dma);
571
        clear_dma_ff(dma);
572
        set_dma_mode(dma, DMA_MODE_WRITE);
573
        set_dma_addr(dma, virt_to_bus(dma_buffer));
574
        set_dma_count(dma, dma_bytes);
575
        enable_dma(dma);
576
        release_dma_lock(flags);
577
 
578
        serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
579
}
580
 
581
static _INLINE_ void transmit_chars_dma_done(struct esp_struct *info)
582
{
583
        int num_bytes;
584
        unsigned long flags;
585
 
586
 
587
        flags=claim_dma_lock();
588
        disable_dma(dma);
589
        clear_dma_ff(dma);
590
 
591
        num_bytes = dma_bytes - get_dma_residue(dma);
592
        info->icount.tx += dma_bytes;
593
        release_dma_lock(flags);
594
 
595
        if (dma_bytes != num_bytes) {
596
                dma_bytes -= num_bytes;
597
                memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
598
 
599
                flags=claim_dma_lock();
600
                disable_dma(dma);
601
                clear_dma_ff(dma);
602
                set_dma_mode(dma, DMA_MODE_WRITE);
603
                set_dma_addr(dma, virt_to_bus(dma_buffer));
604
                set_dma_count(dma, dma_bytes);
605
                enable_dma(dma);
606
                release_dma_lock(flags);
607
 
608
                serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
609
        } else {
610
                dma_bytes = 0;
611
                info->stat_flags &= ~ESP_STAT_DMA_TX;
612
        }
613
}
614
 
615
static _INLINE_ void check_modem_status(struct esp_struct *info)
616
{
617
        int     status;
618
 
619
        serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
620
        status = serial_in(info, UART_ESI_STAT2);
621
 
622
        if (status & UART_MSR_ANY_DELTA) {
623
                /* update input line counters */
624
                if (status & UART_MSR_TERI)
625
                        info->icount.rng++;
626
                if (status & UART_MSR_DDSR)
627
                        info->icount.dsr++;
628
                if (status & UART_MSR_DDCD)
629
                        info->icount.dcd++;
630
                if (status & UART_MSR_DCTS)
631
                        info->icount.cts++;
632
                wake_up_interruptible(&info->delta_msr_wait);
633
        }
634
 
635
        if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
636
#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
637
                printk("ttys%d CD now %s...", info->line,
638
                       (status & UART_MSR_DCD) ? "on" : "off");
639
#endif          
640
                if (status & UART_MSR_DCD)
641
                        wake_up_interruptible(&info->open_wait);
642
                else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
643
                           (info->flags & ASYNC_CALLOUT_NOHUP))) {
644
#ifdef SERIAL_DEBUG_OPEN
645
                        printk("scheduling hangup...");
646
#endif
647
                        MOD_INC_USE_COUNT;
648
                        if (schedule_task(&info->tqueue_hangup) == 0)
649
                                MOD_DEC_USE_COUNT;
650
                }
651
        }
652
}
653
 
654
/*
655
 * This is the serial driver's interrupt routine
656
 */
657
static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
658
{
659
        struct esp_struct * info;
660
        unsigned err_status;
661
        unsigned int scratch;
662
 
663
#ifdef SERIAL_DEBUG_INTR
664
        printk("rs_interrupt_single(%d)...", irq);
665
#endif
666
        info = (struct esp_struct *)dev_id;
667
        err_status = 0;
668
        scratch = serial_in(info, UART_ESI_SID);
669
 
670
        cli();
671
 
672
        if (!info->tty) {
673
                sti();
674
                return;
675
        }
676
 
677
        if (scratch & 0x04) { /* error */
678
                serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
679
                err_status = serial_in(info, UART_ESI_STAT1);
680
                serial_in(info, UART_ESI_STAT2);
681
 
682
                if (err_status & 0x01)
683
                        info->stat_flags |= ESP_STAT_RX_TIMEOUT;
684
 
685
                if (err_status & 0x20) /* UART status */
686
                        check_modem_status(info);
687
 
688
                if (err_status & 0x80) /* Start break */
689
                        wake_up_interruptible(&info->break_wait);
690
        }
691
 
692
        if ((scratch & 0x88) || /* DMA completed or timed out */
693
            (err_status & 0x1c) /* receive error */) {
694
                if (info->stat_flags & ESP_STAT_DMA_RX)
695
                        receive_chars_dma_done(info, err_status);
696
                else if (info->stat_flags & ESP_STAT_DMA_TX)
697
                        transmit_chars_dma_done(info);
698
        }
699
 
700
        if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
701
            ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
702
            (info->IER & UART_IER_RDI)) {
703
                int num_bytes;
704
 
705
                serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
706
                serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
707
                num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
708
                num_bytes |= serial_in(info, UART_ESI_STAT2);
709
 
710
                if (num_bytes > (TTY_FLIPBUF_SIZE - info->tty->flip.count))
711
                  num_bytes = TTY_FLIPBUF_SIZE - info->tty->flip.count;
712
 
713
                if (num_bytes) {
714
                        if (dma_bytes ||
715
                            (info->stat_flags & ESP_STAT_USE_PIO) ||
716
                            (num_bytes <= info->config.pio_threshold))
717
                                receive_chars_pio(info, num_bytes);
718
                        else
719
                                receive_chars_dma(info, num_bytes);
720
                }
721
        }
722
 
723
        if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
724
            (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
725
                if ((info->xmit_cnt <= 0) || info->tty->stopped) {
726
                        info->IER &= ~UART_IER_THRI;
727
                        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
728
                        serial_out(info, UART_ESI_CMD2, info->IER);
729
                } else {
730
                        int num_bytes;
731
 
732
                        serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
733
                        serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
734
                        num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
735
                        num_bytes |= serial_in(info, UART_ESI_STAT2);
736
 
737
                        if (num_bytes > info->xmit_cnt)
738
                                num_bytes = info->xmit_cnt;
739
 
740
                        if (num_bytes) {
741
                                if (dma_bytes ||
742
                                    (info->stat_flags & ESP_STAT_USE_PIO) ||
743
                                    (num_bytes <= info->config.pio_threshold))
744
                                        transmit_chars_pio(info, num_bytes);
745
                                else
746
                                        transmit_chars_dma(info, num_bytes);
747
                        }
748
                }
749
        }
750
 
751
        info->last_active = jiffies;
752
 
753
#ifdef SERIAL_DEBUG_INTR
754
        printk("end.\n");
755
#endif
756
        sti();
757
}
758
 
759
/*
760
 * -------------------------------------------------------------------
761
 * Here ends the serial interrupt routines.
762
 * -------------------------------------------------------------------
763
 */
764
 
765
/*
766
 * This routine is used to handle the "bottom half" processing for the
767
 * serial driver, known also the "software interrupt" processing.
768
 * This processing is done at the kernel interrupt level, after the
769
 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
770
 * is where time-consuming activities which can not be done in the
771
 * interrupt driver proper are done; the interrupt driver schedules
772
 * them using rs_sched_event(), and they get done here.
773
 */
774
static void do_serial_bh(void)
775
{
776
        run_task_queue(&tq_esp);
777
}
778
 
779
static void do_softint(void *private_)
780
{
781
        struct esp_struct       *info = (struct esp_struct *) private_;
782
        struct tty_struct       *tty;
783
 
784
        tty = info->tty;
785
        if (!tty)
786
                return;
787
 
788
        if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
789
                if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
790
                    tty->ldisc.write_wakeup)
791
                        (tty->ldisc.write_wakeup)(tty);
792
                wake_up_interruptible(&tty->write_wait);
793
        }
794
}
795
 
796
/*
797
 * This routine is called from the scheduler tqueue when the interrupt
798
 * routine has signalled that a hangup has occurred.  The path of
799
 * hangup processing is:
800
 *
801
 *      serial interrupt routine -> (scheduler tqueue) ->
802
 *      do_serial_hangup() -> tty->hangup() -> esp_hangup()
803
 *
804
 */
805
static void do_serial_hangup(void *private_)
806
{
807
        struct esp_struct       *info = (struct esp_struct *) private_;
808
        struct tty_struct       *tty;
809
 
810
        tty = info->tty;
811
        if (tty)
812
                tty_hangup(tty);
813
        MOD_DEC_USE_COUNT;
814
}
815
 
816
/*
817
 * ---------------------------------------------------------------
818
 * Low level utility subroutines for the serial driver:  routines to
819
 * figure out the appropriate timeout for an interrupt chain, routines
820
 * to initialize and startup a serial port, and routines to shutdown a
821
 * serial port.  Useful stuff like that.
822
 * ---------------------------------------------------------------
823
 */
824
 
825
static _INLINE_ void esp_basic_init(struct esp_struct * info)
826
{
827
        /* put ESPC in enhanced mode */
828
        serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
829
 
830
        if (info->stat_flags & ESP_STAT_NEVER_DMA)
831
                serial_out(info, UART_ESI_CMD2, 0x01);
832
        else
833
                serial_out(info, UART_ESI_CMD2, 0x31);
834
 
835
        /* disable interrupts for now */
836
        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
837
        serial_out(info, UART_ESI_CMD2, 0x00);
838
 
839
        /* set interrupt and DMA channel */
840
        serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
841
 
842
        if (info->stat_flags & ESP_STAT_NEVER_DMA)
843
                serial_out(info, UART_ESI_CMD2, 0x01);
844
        else
845
                serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
846
 
847
        serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
848
 
849
        if (info->line % 8)     /* secondary port */
850
                serial_out(info, UART_ESI_CMD2, 0x0d);  /* shared */
851
        else if (info->irq == 9)
852
                serial_out(info, UART_ESI_CMD2, 0x02);
853
        else
854
                serial_out(info, UART_ESI_CMD2, info->irq);
855
 
856
        /* set error status mask (check this) */
857
        serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
858
 
859
        if (info->stat_flags & ESP_STAT_NEVER_DMA)
860
                serial_out(info, UART_ESI_CMD2, 0xa1);
861
        else
862
                serial_out(info, UART_ESI_CMD2, 0xbd);
863
 
864
        serial_out(info, UART_ESI_CMD2, 0x00);
865
 
866
        /* set DMA timeout */
867
        serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
868
        serial_out(info, UART_ESI_CMD2, 0xff);
869
 
870
        /* set FIFO trigger levels */
871
        serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
872
        serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
873
        serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
874
        serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
875
        serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
876
 
877
        /* Set clock scaling and wait states */
878
        serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
879
        serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
880
 
881
        /* set reinterrupt pacing */
882
        serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
883
        serial_out(info, UART_ESI_CMD2, 0xff);
884
}
885
 
886
static int startup(struct esp_struct * info)
887
{
888
        unsigned long flags;
889
        int     retval=0;
890
        unsigned int num_chars;
891
 
892
        save_flags(flags); cli();
893
 
894
        if (info->flags & ASYNC_INITIALIZED)
895
                goto out;
896
 
897
        if (!info->xmit_buf) {
898
                info->xmit_buf = (unsigned char *)get_free_page(GFP_KERNEL);
899
                retval = -ENOMEM;
900
                if (!info->xmit_buf)
901
                        goto out;
902
        }
903
 
904
#ifdef SERIAL_DEBUG_OPEN
905
        printk("starting up ttys%d (irq %d)...", info->line, info->irq);
906
#endif
907
 
908
        /* Flush the RX buffer.  Using the ESI flush command may cause */
909
        /* wild interrupts, so read all the data instead. */
910
 
911
        serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
912
        serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
913
        num_chars = serial_in(info, UART_ESI_STAT1) << 8;
914
        num_chars |= serial_in(info, UART_ESI_STAT2);
915
 
916
        while (num_chars > 1) {
917
                inw(info->port + UART_ESI_RX);
918
                num_chars -= 2;
919
        }
920
 
921
        if (num_chars)
922
                serial_in(info, UART_ESI_RX);
923
 
924
        /* set receive character timeout */
925
        serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
926
        serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
927
 
928
        /* clear all flags except the "never DMA" flag */
929
        info->stat_flags &= ESP_STAT_NEVER_DMA;
930
 
931
        if (info->stat_flags & ESP_STAT_NEVER_DMA)
932
                info->stat_flags |= ESP_STAT_USE_PIO;
933
 
934
        /*
935
         * Allocate the IRQ
936
         */
937
 
938
        retval = request_irq(info->irq, rs_interrupt_single, SA_SHIRQ,
939
                             "esp serial", info);
940
 
941
        if (retval) {
942
                if (capable(CAP_SYS_ADMIN)) {
943
                        if (info->tty)
944
                                set_bit(TTY_IO_ERROR,
945
                                        &info->tty->flags);
946
                        retval = 0;
947
                }
948
                goto out;
949
        }
950
 
951
        if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
952
                dma_buffer = (char *)__get_dma_pages(
953
                        GFP_KERNEL, get_order(DMA_BUFFER_SZ));
954
 
955
                /* use PIO mode if DMA buf/chan cannot be allocated */
956
                if (!dma_buffer)
957
                        info->stat_flags |= ESP_STAT_USE_PIO;
958
                else if (request_dma(dma, "esp serial")) {
959
                        free_pages((unsigned long)dma_buffer,
960
                                   get_order(DMA_BUFFER_SZ));
961
                        dma_buffer = 0;
962
                        info->stat_flags |= ESP_STAT_USE_PIO;
963
                }
964
 
965
        }
966
 
967
        info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
968
        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
969
        serial_out(info, UART_ESI_CMD2, UART_MCR);
970
        serial_out(info, UART_ESI_CMD2, info->MCR);
971
 
972
        /*
973
         * Finally, enable interrupts
974
         */
975
        /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
976
        info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
977
                        UART_IER_DMA_TC;
978
        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
979
        serial_out(info, UART_ESI_CMD2, info->IER);
980
 
981
        if (info->tty)
982
                clear_bit(TTY_IO_ERROR, &info->tty->flags);
983
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
984
 
985
        /*
986
         * Set up the tty->alt_speed kludge
987
         */
988
        if (info->tty) {
989
                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
990
                        info->tty->alt_speed = 57600;
991
                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
992
                        info->tty->alt_speed = 115200;
993
                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
994
                        info->tty->alt_speed = 230400;
995
                if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
996
                        info->tty->alt_speed = 460800;
997
        }
998
 
999
        /*
1000
         * set the speed of the serial port
1001
         */
1002
        change_speed(info);
1003
 
1004
        info->flags |= ASYNC_INITIALIZED;
1005
        retval = 0;
1006
out:    restore_flags(flags);
1007
        return retval;
1008
}
1009
 
1010
/*
1011
 * This routine will shutdown a serial port; interrupts are disabled, and
1012
 * DTR is dropped if the hangup on close termio flag is on.
1013
 */
1014
static void shutdown(struct esp_struct * info)
1015
{
1016
        unsigned long   flags, f;
1017
 
1018
        if (!(info->flags & ASYNC_INITIALIZED))
1019
                return;
1020
 
1021
#ifdef SERIAL_DEBUG_OPEN
1022
        printk("Shutting down serial port %d (irq %d)....", info->line,
1023
               info->irq);
1024
#endif
1025
 
1026
        save_flags(flags); cli(); /* Disable interrupts */
1027
 
1028
        /*
1029
         * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1030
         * here so the queue might never be waken up
1031
         */
1032
        wake_up_interruptible(&info->delta_msr_wait);
1033
        wake_up_interruptible(&info->break_wait);
1034
 
1035
        /* stop a DMA transfer on the port being closed */
1036
 
1037
        if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
1038
                f=claim_dma_lock();
1039
                disable_dma(dma);
1040
                clear_dma_ff(dma);
1041
                release_dma_lock(f);
1042
 
1043
                dma_bytes = 0;
1044
        }
1045
 
1046
        /*
1047
         * Free the IRQ
1048
         */
1049
        free_irq(info->irq, info);
1050
 
1051
        if (dma_buffer) {
1052
                struct esp_struct *current_port = ports;
1053
 
1054
                while (current_port) {
1055
                        if ((current_port != info) &&
1056
                            (current_port->flags & ASYNC_INITIALIZED))
1057
                                break;
1058
 
1059
                        current_port = current_port->next_port;
1060
                }
1061
 
1062
                if (!current_port) {
1063
                        free_dma(dma);
1064
                        free_pages((unsigned long)dma_buffer,
1065
                                   get_order(DMA_BUFFER_SZ));
1066
                        dma_buffer = 0;
1067
                }
1068
        }
1069
 
1070
        if (info->xmit_buf) {
1071
                free_page((unsigned long) info->xmit_buf);
1072
                info->xmit_buf = 0;
1073
        }
1074
 
1075
        info->IER = 0;
1076
        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1077
        serial_out(info, UART_ESI_CMD2, 0x00);
1078
 
1079
        if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1080
                info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1081
 
1082
        info->MCR &= ~UART_MCR_OUT2;
1083
        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1084
        serial_out(info, UART_ESI_CMD2, UART_MCR);
1085
        serial_out(info, UART_ESI_CMD2, info->MCR);
1086
 
1087
        if (info->tty)
1088
                set_bit(TTY_IO_ERROR, &info->tty->flags);
1089
 
1090
        info->flags &= ~ASYNC_INITIALIZED;
1091
        restore_flags(flags);
1092
}
1093
 
1094
/*
1095
 * This routine is called to set the UART divisor registers to match
1096
 * the specified baud rate for a serial port.
1097
 */
1098
static void change_speed(struct esp_struct *info)
1099
{
1100
        unsigned short port;
1101
        int     quot = 0;
1102
        unsigned cflag,cval;
1103
        int     baud, bits;
1104
        unsigned char flow1 = 0, flow2 = 0;
1105
        unsigned long flags;
1106
 
1107
        if (!info->tty || !info->tty->termios)
1108
                return;
1109
        cflag = info->tty->termios->c_cflag;
1110
        port = info->port;
1111
 
1112
        /* byte size and parity */
1113
        switch (cflag & CSIZE) {
1114
              case CS5: cval = 0x00; bits = 7; break;
1115
              case CS6: cval = 0x01; bits = 8; break;
1116
              case CS7: cval = 0x02; bits = 9; break;
1117
              case CS8: cval = 0x03; bits = 10; break;
1118
              default:  cval = 0x00; bits = 7; break;
1119
        }
1120
        if (cflag & CSTOPB) {
1121
                cval |= 0x04;
1122
                bits++;
1123
        }
1124
        if (cflag & PARENB) {
1125
                cval |= UART_LCR_PARITY;
1126
                bits++;
1127
        }
1128
        if (!(cflag & PARODD))
1129
                cval |= UART_LCR_EPAR;
1130
#ifdef CMSPAR
1131
        if (cflag & CMSPAR)
1132
                cval |= UART_LCR_SPAR;
1133
#endif
1134
 
1135
        baud = tty_get_baud_rate(info->tty);
1136
        if (baud == 38400 &&
1137
            ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1138
                quot = info->custom_divisor;
1139
        else {
1140
                if (baud == 134)
1141
                        /* Special case since 134 is really 134.5 */
1142
                        quot = (2*BASE_BAUD / 269);
1143
                else if (baud)
1144
                        quot = BASE_BAUD / baud;
1145
        }
1146
        /* If the quotient is ever zero, default to 9600 bps */
1147
        if (!quot)
1148
                quot = BASE_BAUD / 9600;
1149
 
1150
        info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1151
 
1152
        /* CTS flow control flag and modem status interrupts */
1153
        /* info->IER &= ~UART_IER_MSI; */
1154
        if (cflag & CRTSCTS) {
1155
                info->flags |= ASYNC_CTS_FLOW;
1156
                /* info->IER |= UART_IER_MSI; */
1157
                flow1 = 0x04;
1158
                flow2 = 0x10;
1159
        } else
1160
                info->flags &= ~ASYNC_CTS_FLOW;
1161
        if (cflag & CLOCAL)
1162
                info->flags &= ~ASYNC_CHECK_CD;
1163
        else {
1164
                info->flags |= ASYNC_CHECK_CD;
1165
                /* info->IER |= UART_IER_MSI; */
1166
        }
1167
 
1168
        /*
1169
         * Set up parity check flag
1170
         */
1171
#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1172
 
1173
        info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1174
        if (I_INPCK(info->tty))
1175
                info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1176
        if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1177
                info->read_status_mask |= UART_LSR_BI;
1178
 
1179
        info->ignore_status_mask = 0;
1180
#if 0
1181
        /* This should be safe, but for some broken bits of hardware... */
1182
        if (I_IGNPAR(info->tty)) {
1183
                info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1184
                info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1185
        }
1186
#endif
1187
        if (I_IGNBRK(info->tty)) {
1188
                info->ignore_status_mask |= UART_LSR_BI;
1189
                info->read_status_mask |= UART_LSR_BI;
1190
                /*
1191
                 * If we're ignore parity and break indicators, ignore
1192
                 * overruns too.  (For real raw support).
1193
                 */
1194
                if (I_IGNPAR(info->tty)) {
1195
                        info->ignore_status_mask |= UART_LSR_OE | \
1196
                                UART_LSR_PE | UART_LSR_FE;
1197
                        info->read_status_mask |= UART_LSR_OE | \
1198
                                UART_LSR_PE | UART_LSR_FE;
1199
                }
1200
        }
1201
 
1202
        if (I_IXOFF(info->tty))
1203
                flow1 |= 0x81;
1204
 
1205
        save_flags(flags); cli();
1206
        /* set baud */
1207
        serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1208
        serial_out(info, UART_ESI_CMD2, quot >> 8);
1209
        serial_out(info, UART_ESI_CMD2, quot & 0xff);
1210
 
1211
        /* set data bits, parity, etc. */
1212
        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1213
        serial_out(info, UART_ESI_CMD2, UART_LCR);
1214
        serial_out(info, UART_ESI_CMD2, cval);
1215
 
1216
        /* Enable flow control */
1217
        serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1218
        serial_out(info, UART_ESI_CMD2, flow1);
1219
        serial_out(info, UART_ESI_CMD2, flow2);
1220
 
1221
        /* set flow control characters (XON/XOFF only) */
1222
        if (I_IXOFF(info->tty)) {
1223
                serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1224
                serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
1225
                serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
1226
                serial_out(info, UART_ESI_CMD2, 0x10);
1227
                serial_out(info, UART_ESI_CMD2, 0x21);
1228
                switch (cflag & CSIZE) {
1229
                        case CS5:
1230
                                serial_out(info, UART_ESI_CMD2, 0x1f);
1231
                                break;
1232
                        case CS6:
1233
                                serial_out(info, UART_ESI_CMD2, 0x3f);
1234
                                break;
1235
                        case CS7:
1236
                        case CS8:
1237
                                serial_out(info, UART_ESI_CMD2, 0x7f);
1238
                                break;
1239
                        default:
1240
                                serial_out(info, UART_ESI_CMD2, 0xff);
1241
                                break;
1242
                }
1243
        }
1244
 
1245
        /* Set high/low water */
1246
        serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1247
        serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1248
        serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1249
        serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1250
        serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1251
 
1252
        restore_flags(flags);
1253
}
1254
 
1255
static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1256
{
1257
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1258
        unsigned long flags;
1259
 
1260
        if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1261
                return;
1262
 
1263
        if (!tty || !info->xmit_buf)
1264
                return;
1265
 
1266
        save_flags(flags); cli();
1267
        if (info->xmit_cnt >= ESP_XMIT_SIZE - 1) {
1268
                restore_flags(flags);
1269
                return;
1270
        }
1271
 
1272
        info->xmit_buf[info->xmit_head++] = ch;
1273
        info->xmit_head &= ESP_XMIT_SIZE-1;
1274
        info->xmit_cnt++;
1275
        restore_flags(flags);
1276
}
1277
 
1278
static void rs_flush_chars(struct tty_struct *tty)
1279
{
1280
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1281
        unsigned long flags;
1282
 
1283
        if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1284
                return;
1285
 
1286
        if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1287
                return;
1288
 
1289
        save_flags(flags); cli();
1290
        if (!(info->IER & UART_IER_THRI)) {
1291
                info->IER |= UART_IER_THRI;
1292
                serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1293
                serial_out(info, UART_ESI_CMD2, info->IER);
1294
        }
1295
        restore_flags(flags);
1296
}
1297
 
1298
static int rs_write(struct tty_struct * tty, int from_user,
1299
                    const unsigned char *buf, int count)
1300
{
1301
        int     c, t, ret = 0;
1302
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1303
        unsigned long flags;
1304
 
1305
        if (serial_paranoia_check(info, tty->device, "rs_write"))
1306
                return 0;
1307
 
1308
        if (!tty || !info->xmit_buf || !tmp_buf)
1309
                return 0;
1310
 
1311
        if (from_user)
1312
                down(&tmp_buf_sem);
1313
 
1314
        while (1) {
1315
                /* Thanks to R. Wolff for suggesting how to do this with */
1316
                /* interrupts enabled */
1317
 
1318
                c = count;
1319
                t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1320
 
1321
                if (t < c)
1322
                        c = t;
1323
 
1324
                t = ESP_XMIT_SIZE - info->xmit_head;
1325
 
1326
                if (t < c)
1327
                        c = t;
1328
 
1329
                if (c <= 0)
1330
                        break;
1331
 
1332
                if (from_user) {
1333
                        c -= copy_from_user(tmp_buf, buf, c);
1334
 
1335
                        if (!c) {
1336
                                if (!ret)
1337
                                        ret = -EFAULT;
1338
                                break;
1339
                        }
1340
 
1341
                        memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1342
                } else
1343
                        memcpy(info->xmit_buf + info->xmit_head, buf, c);
1344
 
1345
                info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1346
                info->xmit_cnt += c;
1347
                buf += c;
1348
                count -= c;
1349
                ret += c;
1350
        }
1351
 
1352
        if (from_user)
1353
                up(&tmp_buf_sem);
1354
 
1355
        save_flags(flags); cli();
1356
 
1357
        if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1358
                info->IER |= UART_IER_THRI;
1359
                serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1360
                serial_out(info, UART_ESI_CMD2, info->IER);
1361
        }
1362
 
1363
        restore_flags(flags);
1364
        return ret;
1365
}
1366
 
1367
static int rs_write_room(struct tty_struct *tty)
1368
{
1369
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1370
        int     ret;
1371
 
1372
        if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1373
                return 0;
1374
        ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1375
        if (ret < 0)
1376
                ret = 0;
1377
        return ret;
1378
}
1379
 
1380
static int rs_chars_in_buffer(struct tty_struct *tty)
1381
{
1382
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1383
 
1384
        if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1385
                return 0;
1386
        return info->xmit_cnt;
1387
}
1388
 
1389
static void rs_flush_buffer(struct tty_struct *tty)
1390
{
1391
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1392
 
1393
        if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1394
                return;
1395
        cli();
1396
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1397
        sti();
1398
        wake_up_interruptible(&tty->write_wait);
1399
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1400
            tty->ldisc.write_wakeup)
1401
                (tty->ldisc.write_wakeup)(tty);
1402
}
1403
 
1404
/*
1405
 * ------------------------------------------------------------
1406
 * rs_throttle()
1407
 *
1408
 * This routine is called by the upper-layer tty layer to signal that
1409
 * incoming characters should be throttled.
1410
 * ------------------------------------------------------------
1411
 */
1412
static void rs_throttle(struct tty_struct * tty)
1413
{
1414
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1415
#ifdef SERIAL_DEBUG_THROTTLE
1416
        char    buf[64];
1417
 
1418
        printk("throttle %s: %d....\n", tty_name(tty, buf),
1419
               tty->ldisc.chars_in_buffer(tty));
1420
#endif
1421
 
1422
        if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1423
                return;
1424
 
1425
        cli();
1426
        info->IER &= ~UART_IER_RDI;
1427
        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1428
        serial_out(info, UART_ESI_CMD2, info->IER);
1429
        serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1430
        serial_out(info, UART_ESI_CMD2, 0x00);
1431
        sti();
1432
}
1433
 
1434
static void rs_unthrottle(struct tty_struct * tty)
1435
{
1436
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1437
#ifdef SERIAL_DEBUG_THROTTLE
1438
        char    buf[64];
1439
 
1440
        printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1441
               tty->ldisc.chars_in_buffer(tty));
1442
#endif
1443
 
1444
        if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1445
                return;
1446
 
1447
        cli();
1448
        info->IER |= UART_IER_RDI;
1449
        serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1450
        serial_out(info, UART_ESI_CMD2, info->IER);
1451
        serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1452
        serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1453
        sti();
1454
}
1455
 
1456
/*
1457
 * ------------------------------------------------------------
1458
 * rs_ioctl() and friends
1459
 * ------------------------------------------------------------
1460
 */
1461
 
1462
static int get_serial_info(struct esp_struct * info,
1463
                           struct serial_struct * retinfo)
1464
{
1465
        struct serial_struct tmp;
1466
 
1467
        if (!retinfo)
1468
                return -EFAULT;
1469
        memset(&tmp, 0, sizeof(tmp));
1470
        tmp.type = PORT_16550A;
1471
        tmp.line = info->line;
1472
        tmp.port = info->port;
1473
        tmp.irq = info->irq;
1474
        tmp.flags = info->flags;
1475
        tmp.xmit_fifo_size = 1024;
1476
        tmp.baud_base = BASE_BAUD;
1477
        tmp.close_delay = info->close_delay;
1478
        tmp.closing_wait = info->closing_wait;
1479
        tmp.custom_divisor = info->custom_divisor;
1480
        tmp.hub6 = 0;
1481
        if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1482
                return -EFAULT;
1483
        return 0;
1484
}
1485
 
1486
static int get_esp_config(struct esp_struct * info,
1487
                          struct hayes_esp_config * retinfo)
1488
{
1489
        struct hayes_esp_config tmp;
1490
 
1491
        if (!retinfo)
1492
                return -EFAULT;
1493
 
1494
        memset(&tmp, 0, sizeof(tmp));
1495
        tmp.rx_timeout = info->config.rx_timeout;
1496
        tmp.rx_trigger = info->config.rx_trigger;
1497
        tmp.tx_trigger = info->config.tx_trigger;
1498
        tmp.flow_off = info->config.flow_off;
1499
        tmp.flow_on = info->config.flow_on;
1500
        tmp.pio_threshold = info->config.pio_threshold;
1501
        tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1502
 
1503
        return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1504
}
1505
 
1506
static int set_serial_info(struct esp_struct * info,
1507
                           struct serial_struct * new_info)
1508
{
1509
        struct serial_struct new_serial;
1510
        struct esp_struct old_info;
1511
        unsigned int change_irq;
1512
        int retval = 0;
1513
        struct esp_struct *current_async;
1514
 
1515
        if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1516
                return -EFAULT;
1517
        old_info = *info;
1518
 
1519
        if ((new_serial.type != PORT_16550A) ||
1520
            (new_serial.hub6) ||
1521
            (info->port != new_serial.port) ||
1522
            (new_serial.baud_base != BASE_BAUD) ||
1523
            (new_serial.irq > 15) ||
1524
            (new_serial.irq < 2) ||
1525
            (new_serial.irq == 6) ||
1526
            (new_serial.irq == 8) ||
1527
            (new_serial.irq == 13))
1528
                return -EINVAL;
1529
 
1530
        change_irq = new_serial.irq != info->irq;
1531
 
1532
        if (change_irq && (info->line % 8))
1533
                return -EINVAL;
1534
 
1535
        if (!capable(CAP_SYS_ADMIN)) {
1536
                if (change_irq ||
1537
                    (new_serial.close_delay != info->close_delay) ||
1538
                    ((new_serial.flags & ~ASYNC_USR_MASK) !=
1539
                     (info->flags & ~ASYNC_USR_MASK)))
1540
                        return -EPERM;
1541
                info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1542
                               (new_serial.flags & ASYNC_USR_MASK));
1543
                info->custom_divisor = new_serial.custom_divisor;
1544
        } else {
1545
                if (new_serial.irq == 2)
1546
                        new_serial.irq = 9;
1547
 
1548
                if (change_irq) {
1549
                        current_async = ports;
1550
 
1551
                        while (current_async) {
1552
                                if ((current_async->line >= info->line) &&
1553
                                    (current_async->line < (info->line + 8))) {
1554
                                        if (current_async == info) {
1555
                                                if (current_async->count > 1)
1556
                                                        return -EBUSY;
1557
                                        } else if (current_async->count)
1558
                                                return -EBUSY;
1559
                                }
1560
 
1561
                                current_async = current_async->next_port;
1562
                        }
1563
                }
1564
 
1565
                /*
1566
                 * OK, past this point, all the error checking has been done.
1567
                 * At this point, we start making changes.....
1568
                 */
1569
 
1570
                info->flags = ((info->flags & ~ASYNC_FLAGS) |
1571
                               (new_serial.flags & ASYNC_FLAGS));
1572
                info->custom_divisor = new_serial.custom_divisor;
1573
                info->close_delay = new_serial.close_delay * HZ/100;
1574
                info->closing_wait = new_serial.closing_wait * HZ/100;
1575
 
1576
                if (change_irq) {
1577
                        /*
1578
                         * We need to shutdown the serial port at the old
1579
                         * port/irq combination.
1580
                         */
1581
                        shutdown(info);
1582
 
1583
                        current_async = ports;
1584
 
1585
                        while (current_async) {
1586
                                if ((current_async->line >= info->line) &&
1587
                                    (current_async->line < (info->line + 8)))
1588
                                        current_async->irq = new_serial.irq;
1589
 
1590
                                current_async = current_async->next_port;
1591
                        }
1592
 
1593
                        serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1594
                        if (info->irq == 9)
1595
                                serial_out(info, UART_ESI_CMD2, 0x02);
1596
                        else
1597
                                serial_out(info, UART_ESI_CMD2, info->irq);
1598
                }
1599
        }
1600
 
1601
        if (info->flags & ASYNC_INITIALIZED) {
1602
                if (((old_info.flags & ASYNC_SPD_MASK) !=
1603
                     (info->flags & ASYNC_SPD_MASK)) ||
1604
                    (old_info.custom_divisor != info->custom_divisor)) {
1605
                        if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1606
                                info->tty->alt_speed = 57600;
1607
                        if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1608
                                info->tty->alt_speed = 115200;
1609
                        if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1610
                                info->tty->alt_speed = 230400;
1611
                        if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1612
                                info->tty->alt_speed = 460800;
1613
                        change_speed(info);
1614
                }
1615
        } else
1616
                retval = startup(info);
1617
 
1618
        return retval;
1619
}
1620
 
1621
static int set_esp_config(struct esp_struct * info,
1622
                          struct hayes_esp_config * new_info)
1623
{
1624
        struct hayes_esp_config new_config;
1625
        unsigned int change_dma;
1626
        int retval = 0;
1627
        struct esp_struct *current_async;
1628
 
1629
        /* Perhaps a non-sysadmin user should be able to do some of these */
1630
        /* operations.  I haven't decided yet. */
1631
 
1632
        if (!capable(CAP_SYS_ADMIN))
1633
                return -EPERM;
1634
 
1635
        if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1636
                return -EFAULT;
1637
 
1638
        if ((new_config.flow_on >= new_config.flow_off) ||
1639
            (new_config.rx_trigger < 1) ||
1640
            (new_config.tx_trigger < 1) ||
1641
            (new_config.flow_off < 1) ||
1642
            (new_config.flow_on < 1) ||
1643
            (new_config.rx_trigger > 1023) ||
1644
            (new_config.tx_trigger > 1023) ||
1645
            (new_config.flow_off > 1023) ||
1646
            (new_config.flow_on > 1023) ||
1647
            (new_config.pio_threshold < 0) ||
1648
            (new_config.pio_threshold > 1024))
1649
                return -EINVAL;
1650
 
1651
        if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1652
                new_config.dma_channel = 0;
1653
 
1654
        if (info->stat_flags & ESP_STAT_NEVER_DMA)
1655
                change_dma = new_config.dma_channel;
1656
        else
1657
                change_dma = (new_config.dma_channel != dma);
1658
 
1659
        if (change_dma) {
1660
                if (new_config.dma_channel) {
1661
                        /* PIO mode to DMA mode transition OR */
1662
                        /* change current DMA channel */
1663
 
1664
                        current_async = ports;
1665
 
1666
                        while (current_async) {
1667
                                if (current_async == info) {
1668
                                        if (current_async->count > 1)
1669
                                                return -EBUSY;
1670
                                } else if (current_async->count)
1671
                                        return -EBUSY;
1672
 
1673
                                current_async =
1674
                                        current_async->next_port;
1675
                        }
1676
 
1677
                        shutdown(info);
1678
                        dma = new_config.dma_channel;
1679
                        info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1680
 
1681
                        /* all ports must use the same DMA channel */
1682
 
1683
                        current_async = ports;
1684
 
1685
                        while (current_async) {
1686
                                esp_basic_init(current_async);
1687
                                current_async = current_async->next_port;
1688
                        }
1689
                } else {
1690
                        /* DMA mode to PIO mode only */
1691
 
1692
                        if (info->count > 1)
1693
                                return -EBUSY;
1694
 
1695
                        shutdown(info);
1696
                        info->stat_flags |= ESP_STAT_NEVER_DMA;
1697
                        esp_basic_init(info);
1698
                }
1699
        }
1700
 
1701
        info->config.pio_threshold = new_config.pio_threshold;
1702
 
1703
        if ((new_config.flow_off != info->config.flow_off) ||
1704
            (new_config.flow_on != info->config.flow_on)) {
1705
                unsigned long flags;
1706
 
1707
                info->config.flow_off = new_config.flow_off;
1708
                info->config.flow_on = new_config.flow_on;
1709
                save_flags(flags); cli();
1710
                serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1711
                serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1712
                serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1713
                serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1714
                serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1715
                restore_flags(flags);
1716
        }
1717
 
1718
        if ((new_config.rx_trigger != info->config.rx_trigger) ||
1719
            (new_config.tx_trigger != info->config.tx_trigger)) {
1720
                unsigned long flags;
1721
 
1722
                info->config.rx_trigger = new_config.rx_trigger;
1723
                info->config.tx_trigger = new_config.tx_trigger;
1724
                save_flags(flags); cli();
1725
                serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1726
                serial_out(info, UART_ESI_CMD2,
1727
                           new_config.rx_trigger >> 8);
1728
                serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1729
                serial_out(info, UART_ESI_CMD2,
1730
                           new_config.tx_trigger >> 8);
1731
                serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1732
                restore_flags(flags);
1733
        }
1734
 
1735
        if (new_config.rx_timeout != info->config.rx_timeout) {
1736
                unsigned long flags;
1737
 
1738
                info->config.rx_timeout = new_config.rx_timeout;
1739
                save_flags(flags); cli();
1740
 
1741
                if (info->IER & UART_IER_RDI) {
1742
                        serial_out(info, UART_ESI_CMD1,
1743
                                   ESI_SET_RX_TIMEOUT);
1744
                        serial_out(info, UART_ESI_CMD2,
1745
                                   new_config.rx_timeout);
1746
                }
1747
 
1748
                restore_flags(flags);
1749
        }
1750
 
1751
        if (!(info->flags & ASYNC_INITIALIZED))
1752
                retval = startup(info);
1753
 
1754
        return retval;
1755
}
1756
 
1757
/*
1758
 * get_lsr_info - get line status register info
1759
 *
1760
 * Purpose: Let user call ioctl() to get info when the UART physically
1761
 *          is emptied.  On bus types like RS485, the transmitter must
1762
 *          release the bus after transmitting. This must be done when
1763
 *          the transmit shift register is empty, not be done when the
1764
 *          transmit holding register is empty.  This functionality
1765
 *          allows an RS485 driver to be written in user space.
1766
 */
1767
static int get_lsr_info(struct esp_struct * info, unsigned int *value)
1768
{
1769
        unsigned char status;
1770
        unsigned int result;
1771
 
1772
        cli();
1773
        serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1774
        status = serial_in(info, UART_ESI_STAT1);
1775
        sti();
1776
        result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1777
        return put_user(result,value);
1778
}
1779
 
1780
 
1781
static int get_modem_info(struct esp_struct * info, unsigned int *value)
1782
{
1783
        unsigned char control, status;
1784
        unsigned int result;
1785
 
1786
        control = info->MCR;
1787
        cli();
1788
        serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1789
        status = serial_in(info, UART_ESI_STAT2);
1790
        sti();
1791
        result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1792
                | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1793
                | ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1794
                | ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1795
                | ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1796
                | ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1797
        return put_user(result,value);
1798
}
1799
 
1800
static int set_modem_info(struct esp_struct * info, unsigned int cmd,
1801
                          unsigned int *value)
1802
{
1803
        unsigned int arg;
1804
 
1805
        if (get_user(arg, value))
1806
                return -EFAULT;
1807
 
1808
        switch (cmd) {
1809
        case TIOCMBIS:
1810
                if (arg & TIOCM_RTS)
1811
                        info->MCR |= UART_MCR_RTS;
1812
                if (arg & TIOCM_DTR)
1813
                        info->MCR |= UART_MCR_DTR;
1814
                break;
1815
        case TIOCMBIC:
1816
                if (arg & TIOCM_RTS)
1817
                        info->MCR &= ~UART_MCR_RTS;
1818
                if (arg & TIOCM_DTR)
1819
                        info->MCR &= ~UART_MCR_DTR;
1820
                break;
1821
        case TIOCMSET:
1822
                info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR))
1823
                             | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1824
                             | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1825
                break;
1826
        default:
1827
                return -EINVAL;
1828
        }
1829
        cli();
1830
        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1831
        serial_out(info, UART_ESI_CMD2, UART_MCR);
1832
        serial_out(info, UART_ESI_CMD2, info->MCR);
1833
        sti();
1834
        return 0;
1835
}
1836
 
1837
/*
1838
 * rs_break() --- routine which turns the break handling on or off
1839
 */
1840
static void esp_break(struct tty_struct *tty, int break_state)
1841
{
1842
        struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1843
        unsigned long flags;
1844
 
1845
        if (serial_paranoia_check(info, tty->device, "esp_break"))
1846
                return;
1847
 
1848
        save_flags(flags); cli();
1849
        if (break_state == -1) {
1850
                serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1851
                serial_out(info, UART_ESI_CMD2, 0x01);
1852
 
1853
                interruptible_sleep_on(&info->break_wait);
1854
        } else {
1855
                serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1856
                serial_out(info, UART_ESI_CMD2, 0x00);
1857
        }
1858
        restore_flags(flags);
1859
}
1860
 
1861
static int rs_ioctl(struct tty_struct *tty, struct file * file,
1862
                    unsigned int cmd, unsigned long arg)
1863
{
1864
        struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1865
        struct async_icount cprev, cnow;        /* kernel counter temps */
1866
        struct serial_icounter_struct *p_cuser; /* user space */
1867
 
1868
        if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1869
                return -ENODEV;
1870
 
1871
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1872
            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1873
            (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1874
            (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1875
            (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1876
                if (tty->flags & (1 << TTY_IO_ERROR))
1877
                    return -EIO;
1878
        }
1879
 
1880
        switch (cmd) {
1881
                case TIOCMGET:
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
                        return get_serial_info(info,
1889
                                               (struct serial_struct *) arg);
1890
                case TIOCSSERIAL:
1891
                        return set_serial_info(info,
1892
                                               (struct serial_struct *) arg);
1893
                case TIOCSERCONFIG:
1894
                        /* do not reconfigure after initial configuration */
1895
                        return 0;
1896
 
1897
                case TIOCSERGWILD:
1898
                        return put_user(0L, (unsigned long *) arg);
1899
 
1900
                case TIOCSERGETLSR: /* Get line status register */
1901
                            return get_lsr_info(info, (unsigned int *) arg);
1902
 
1903
                case TIOCSERSWILD:
1904
                        if (!capable(CAP_SYS_ADMIN))
1905
                                return -EPERM;
1906
                        return 0;
1907
 
1908
                /*
1909
                 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1910
                 * - mask passed in arg for lines of interest
1911
                 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1912
                 * Caller should use TIOCGICOUNT to see which one it was
1913
                 */
1914
                 case TIOCMIWAIT:
1915
                        cli();
1916
                        cprev = info->icount;   /* note the counters on entry */
1917
                        sti();
1918
                        while (1) {
1919
                                interruptible_sleep_on(&info->delta_msr_wait);
1920
                                /* see if a signal did it */
1921
                                if (signal_pending(current))
1922
                                        return -ERESTARTSYS;
1923
                                cli();
1924
                                cnow = info->icount;    /* atomic copy */
1925
                                sti();
1926
                                if (cnow.rng == cprev.rng &&
1927
                                    cnow.dsr == cprev.dsr &&
1928
                                    cnow.dcd == cprev.dcd &&
1929
                                    cnow.cts == cprev.cts)
1930
                                        return -EIO; /* no change => error */
1931
                                if (((arg & TIOCM_RNG) &&
1932
                                     (cnow.rng != cprev.rng)) ||
1933
                                     ((arg & TIOCM_DSR) &&
1934
                                      (cnow.dsr != cprev.dsr)) ||
1935
                                     ((arg & TIOCM_CD) &&
1936
                                      (cnow.dcd != cprev.dcd)) ||
1937
                                     ((arg & TIOCM_CTS) &&
1938
                                      (cnow.cts != cprev.cts)) ) {
1939
                                        return 0;
1940
                                }
1941
                                cprev = cnow;
1942
                        }
1943
                        /* NOTREACHED */
1944
 
1945
                /*
1946
                 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1947
                 * Return: write counters to the user passed counter struct
1948
                 * NB: both 1->0 and 0->1 transitions are counted except for
1949
                 *     RI where only 0->1 is counted.
1950
                 */
1951
                case TIOCGICOUNT:
1952
                        cli();
1953
                        cnow = info->icount;
1954
                        sti();
1955
                        p_cuser = (struct serial_icounter_struct *) arg;
1956
                        if (put_user(cnow.cts, &p_cuser->cts) ||
1957
                            put_user(cnow.dsr, &p_cuser->dsr) ||
1958
                            put_user(cnow.rng, &p_cuser->rng) ||
1959
                            put_user(cnow.dcd, &p_cuser->dcd))
1960
                                return -EFAULT;
1961
 
1962
                        return 0;
1963
        case TIOCGHAYESESP:
1964
                return (get_esp_config(info, (struct hayes_esp_config *)arg));
1965
        case TIOCSHAYESESP:
1966
                return (set_esp_config(info, (struct hayes_esp_config *)arg));
1967
 
1968
                default:
1969
                        return -ENOIOCTLCMD;
1970
                }
1971
        return 0;
1972
}
1973
 
1974
static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1975
{
1976
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1977
 
1978
        if (   (tty->termios->c_cflag == old_termios->c_cflag)
1979
            && (   RELEVANT_IFLAG(tty->termios->c_iflag)
1980
                == RELEVANT_IFLAG(old_termios->c_iflag)))
1981
          return;
1982
 
1983
        change_speed(info);
1984
 
1985
        /* Handle transition to B0 status */
1986
        if ((old_termios->c_cflag & CBAUD) &&
1987
                !(tty->termios->c_cflag & CBAUD)) {
1988
                info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1989
                cli();
1990
                serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1991
                serial_out(info, UART_ESI_CMD2, UART_MCR);
1992
                serial_out(info, UART_ESI_CMD2, info->MCR);
1993
                sti();
1994
        }
1995
 
1996
        /* Handle transition away from B0 status */
1997
        if (!(old_termios->c_cflag & CBAUD) &&
1998
                (tty->termios->c_cflag & CBAUD)) {
1999
                info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
2000
                cli();
2001
                serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2002
                serial_out(info, UART_ESI_CMD2, UART_MCR);
2003
                serial_out(info, UART_ESI_CMD2, info->MCR);
2004
                sti();
2005
        }
2006
 
2007
        /* Handle turning of CRTSCTS */
2008
        if ((old_termios->c_cflag & CRTSCTS) &&
2009
            !(tty->termios->c_cflag & CRTSCTS)) {
2010
                rs_start(tty);
2011
        }
2012
 
2013
#if 0
2014
        /*
2015
         * No need to wake up processes in open wait, since they
2016
         * sample the CLOCAL flag once, and don't recheck it.
2017
         * XXX  It's not clear whether the current behavior is correct
2018
         * or not.  Hence, this may change.....
2019
         */
2020
        if (!(old_termios->c_cflag & CLOCAL) &&
2021
            (tty->termios->c_cflag & CLOCAL))
2022
                wake_up_interruptible(&info->open_wait);
2023
#endif
2024
}
2025
 
2026
/*
2027
 * ------------------------------------------------------------
2028
 * rs_close()
2029
 *
2030
 * This routine is called when the serial port gets closed.  First, we
2031
 * wait for the last remaining data to be sent.  Then, we unlink its
2032
 * async structure from the interrupt chain if necessary, and we free
2033
 * that IRQ if nothing is left in the chain.
2034
 * ------------------------------------------------------------
2035
 */
2036
static void rs_close(struct tty_struct *tty, struct file * filp)
2037
{
2038
        struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2039
        unsigned long flags;
2040
 
2041
        if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
2042
                return;
2043
 
2044
        save_flags(flags); cli();
2045
 
2046
        if (tty_hung_up_p(filp)) {
2047
                DBG_CNT("before DEC-hung");
2048
                goto out;
2049
        }
2050
 
2051
#ifdef SERIAL_DEBUG_OPEN
2052
        printk("rs_close ttys%d, count = %d\n", info->line, info->count);
2053
#endif
2054
        if ((tty->count == 1) && (info->count != 1)) {
2055
                /*
2056
                 * Uh, oh.  tty->count is 1, which means that the tty
2057
                 * structure will be freed.  Info->count should always
2058
                 * be one in these conditions.  If it's greater than
2059
                 * one, we've got real problems, since it means the
2060
                 * serial port won't be shutdown.
2061
                 */
2062
                printk("rs_close: bad serial port count; tty->count is 1, "
2063
                       "info->count is %d\n", info->count);
2064
                info->count = 1;
2065
        }
2066
        if (--info->count < 0) {
2067
                printk("rs_close: bad serial port count for ttys%d: %d\n",
2068
                       info->line, info->count);
2069
                info->count = 0;
2070
        }
2071
        if (info->count) {
2072
                DBG_CNT("before DEC-2");
2073
                goto out;
2074
        }
2075
        info->flags |= ASYNC_CLOSING;
2076
        /*
2077
         * Save the termios structure, since this port may have
2078
         * separate termios for callout and dialin.
2079
         */
2080
        if (info->flags & ASYNC_NORMAL_ACTIVE)
2081
                info->normal_termios = *tty->termios;
2082
        if (info->flags & ASYNC_CALLOUT_ACTIVE)
2083
                info->callout_termios = *tty->termios;
2084
        /*
2085
         * Now we wait for the transmit buffer to clear; and we notify
2086
         * the line discipline to only process XON/XOFF characters.
2087
         */
2088
        tty->closing = 1;
2089
        if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2090
                tty_wait_until_sent(tty, info->closing_wait);
2091
        /*
2092
         * At this point we stop accepting input.  To do this, we
2093
         * disable the receive line status interrupts, and tell the
2094
         * interrupt driver to stop checking the data ready bit in the
2095
         * line status register.
2096
         */
2097
        /* info->IER &= ~UART_IER_RLSI; */
2098
        info->IER &= ~UART_IER_RDI;
2099
        info->read_status_mask &= ~UART_LSR_DR;
2100
        if (info->flags & ASYNC_INITIALIZED) {
2101
                serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2102
                serial_out(info, UART_ESI_CMD2, info->IER);
2103
 
2104
                /* disable receive timeout */
2105
                serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2106
                serial_out(info, UART_ESI_CMD2, 0x00);
2107
 
2108
                /*
2109
                 * Before we drop DTR, make sure the UART transmitter
2110
                 * has completely drained; this is especially
2111
                 * important if there is a transmit FIFO!
2112
                 */
2113
                rs_wait_until_sent(tty, info->timeout);
2114
        }
2115
        shutdown(info);
2116
        if (tty->driver.flush_buffer)
2117
                tty->driver.flush_buffer(tty);
2118
        if (tty->ldisc.flush_buffer)
2119
                tty->ldisc.flush_buffer(tty);
2120
        tty->closing = 0;
2121
        info->event = 0;
2122
        info->tty = 0;
2123
 
2124
        if (info->blocked_open) {
2125
                if (info->close_delay) {
2126
                        set_current_state(TASK_INTERRUPTIBLE);
2127
                        schedule_timeout(info->close_delay);
2128
                }
2129
                wake_up_interruptible(&info->open_wait);
2130
        }
2131
        info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2132
                         ASYNC_CLOSING);
2133
        wake_up_interruptible(&info->close_wait);
2134
out:    MOD_DEC_USE_COUNT;
2135
        restore_flags(flags);
2136
}
2137
 
2138
static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2139
{
2140
        struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2141
        unsigned long orig_jiffies, char_time;
2142
        unsigned long flags;
2143
 
2144
        if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
2145
                return;
2146
 
2147
        orig_jiffies = jiffies;
2148
        char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2149
 
2150
        if (!char_time)
2151
                char_time = 1;
2152
 
2153
        save_flags(flags); cli();
2154
        serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2155
        serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2156
 
2157
        while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2158
                (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2159
                set_current_state(TASK_INTERRUPTIBLE);
2160
                schedule_timeout(char_time);
2161
 
2162
                if (signal_pending(current))
2163
                        break;
2164
 
2165
                if (timeout && time_after(jiffies, orig_jiffies + timeout))
2166
                        break;
2167
 
2168
                serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2169
                serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2170
        }
2171
 
2172
        restore_flags(flags);
2173
        set_current_state(TASK_RUNNING);
2174
}
2175
 
2176
/*
2177
 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2178
 */
2179
static void esp_hangup(struct tty_struct *tty)
2180
{
2181
        struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2182
 
2183
        if (serial_paranoia_check(info, tty->device, "esp_hangup"))
2184
                return;
2185
 
2186
        rs_flush_buffer(tty);
2187
        shutdown(info);
2188
        info->event = 0;
2189
        info->count = 0;
2190
        info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2191
        info->tty = 0;
2192
        wake_up_interruptible(&info->open_wait);
2193
}
2194
 
2195
/*
2196
 * ------------------------------------------------------------
2197
 * esp_open() and friends
2198
 * ------------------------------------------------------------
2199
 */
2200
static int block_til_ready(struct tty_struct *tty, struct file * filp,
2201
                           struct esp_struct *info)
2202
{
2203
        DECLARE_WAITQUEUE(wait, current);
2204
        int             retval;
2205
        int             do_clocal = 0;
2206
        unsigned long   flags;
2207
 
2208
        /*
2209
         * If the device is in the middle of being closed, then block
2210
         * until it's done, and then try again.
2211
         */
2212
        if (tty_hung_up_p(filp) ||
2213
            (info->flags & ASYNC_CLOSING)) {
2214
                if (info->flags & ASYNC_CLOSING)
2215
                        interruptible_sleep_on(&info->close_wait);
2216
#ifdef SERIAL_DO_RESTART
2217
                if (info->flags & ASYNC_HUP_NOTIFY)
2218
                        return -EAGAIN;
2219
                else
2220
                        return -ERESTARTSYS;
2221
#else
2222
                return -EAGAIN;
2223
#endif
2224
        }
2225
 
2226
        /*
2227
         * If this is a callout device, then just make sure the normal
2228
         * device isn't being used.
2229
         */
2230
        if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2231
                if (info->flags & ASYNC_NORMAL_ACTIVE)
2232
                        return -EBUSY;
2233
                if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2234
                    (info->flags & ASYNC_SESSION_LOCKOUT) &&
2235
                    (info->session != current->session))
2236
                    return -EBUSY;
2237
                if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2238
                    (info->flags & ASYNC_PGRP_LOCKOUT) &&
2239
                    (info->pgrp != current->pgrp))
2240
                    return -EBUSY;
2241
                info->flags |= ASYNC_CALLOUT_ACTIVE;
2242
                return 0;
2243
        }
2244
 
2245
        /*
2246
         * If non-blocking mode is set, or the port is not enabled,
2247
         * then make the check up front and then exit.
2248
         */
2249
        if ((filp->f_flags & O_NONBLOCK) ||
2250
            (tty->flags & (1 << TTY_IO_ERROR))) {
2251
                if (info->flags & ASYNC_CALLOUT_ACTIVE)
2252
                        return -EBUSY;
2253
                info->flags |= ASYNC_NORMAL_ACTIVE;
2254
                return 0;
2255
        }
2256
 
2257
        if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2258
                if (info->normal_termios.c_cflag & CLOCAL)
2259
                        do_clocal = 1;
2260
        } else {
2261
                if (tty->termios->c_cflag & CLOCAL)
2262
                        do_clocal = 1;
2263
        }
2264
 
2265
        /*
2266
         * Block waiting for the carrier detect and the line to become
2267
         * free (i.e., not in use by the callout).  While we are in
2268
         * this loop, info->count is dropped by one, so that
2269
         * rs_close() knows when to free things.  We restore it upon
2270
         * exit, either normal or abnormal.
2271
         */
2272
        retval = 0;
2273
        add_wait_queue(&info->open_wait, &wait);
2274
#ifdef SERIAL_DEBUG_OPEN
2275
        printk("block_til_ready before block: ttys%d, count = %d\n",
2276
               info->line, info->count);
2277
#endif
2278
        save_flags(flags);
2279
        cli();
2280
        if (!tty_hung_up_p(filp))
2281
                info->count--;
2282
        restore_flags(flags);
2283
        info->blocked_open++;
2284
        while (1) {
2285
                save_flags(flags);
2286
                cli();
2287
                if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2288
                        (tty->termios->c_cflag & CBAUD)) {
2289
                        unsigned int scratch;
2290
 
2291
                        serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2292
                        serial_out(info, UART_ESI_CMD2, UART_MCR);
2293
                        scratch = serial_in(info, UART_ESI_STAT1);
2294
                        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2295
                        serial_out(info, UART_ESI_CMD2, UART_MCR);
2296
                        serial_out(info, UART_ESI_CMD2,
2297
                                scratch | UART_MCR_DTR | UART_MCR_RTS);
2298
                }
2299
                restore_flags(flags);
2300
                set_current_state(TASK_INTERRUPTIBLE);
2301
                if (tty_hung_up_p(filp) ||
2302
                    !(info->flags & ASYNC_INITIALIZED)) {
2303
#ifdef SERIAL_DO_RESTART
2304
                        if (info->flags & ASYNC_HUP_NOTIFY)
2305
                                retval = -EAGAIN;
2306
                        else
2307
                                retval = -ERESTARTSYS;
2308
#else
2309
                        retval = -EAGAIN;
2310
#endif
2311
                        break;
2312
                }
2313
 
2314
                serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2315
                if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2316
                        do_clocal = 1;
2317
 
2318
                if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2319
                    !(info->flags & ASYNC_CLOSING) &&
2320
                    (do_clocal))
2321
                        break;
2322
                if (signal_pending(current)) {
2323
                        retval = -ERESTARTSYS;
2324
                        break;
2325
                }
2326
#ifdef SERIAL_DEBUG_OPEN
2327
                printk("block_til_ready blocking: ttys%d, count = %d\n",
2328
                       info->line, info->count);
2329
#endif
2330
                schedule();
2331
        }
2332
        set_current_state(TASK_RUNNING);
2333
        remove_wait_queue(&info->open_wait, &wait);
2334
        if (!tty_hung_up_p(filp))
2335
                info->count++;
2336
        info->blocked_open--;
2337
#ifdef SERIAL_DEBUG_OPEN
2338
        printk("block_til_ready after blocking: ttys%d, count = %d\n",
2339
               info->line, info->count);
2340
#endif
2341
        if (retval)
2342
                return retval;
2343
        info->flags |= ASYNC_NORMAL_ACTIVE;
2344
        return 0;
2345
}
2346
 
2347
/*
2348
 * This routine is called whenever a serial port is opened.  It
2349
 * enables interrupts for a serial port, linking in its async structure into
2350
 * the IRQ chain.   It also performs the serial-specific
2351
 * initialization for the tty structure.
2352
 */
2353
static int esp_open(struct tty_struct *tty, struct file * filp)
2354
{
2355
        struct esp_struct       *info;
2356
        int                     retval, line;
2357
 
2358
        line = MINOR(tty->device) - tty->driver.minor_start;
2359
        if ((line < 0) || (line >= NR_PORTS))
2360
                return -ENODEV;
2361
 
2362
        /* find the port in the chain */
2363
 
2364
        info = ports;
2365
 
2366
        while (info && (info->line != line))
2367
                info = info->next_port;
2368
 
2369
        if (!info) {
2370
                serial_paranoia_check(info, tty->device, "esp_open");
2371
                return -ENODEV;
2372
        }
2373
 
2374
#ifdef SERIAL_DEBUG_OPEN
2375
        printk("esp_open %s%d, count = %d\n", tty->driver.name, info->line,
2376
               info->count);
2377
#endif
2378
        MOD_INC_USE_COUNT;
2379
        info->count++;
2380
        tty->driver_data = info;
2381
        info->tty = tty;
2382
 
2383
        if (!tmp_buf) {
2384
                tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
2385
                if (!tmp_buf)
2386
                        return -ENOMEM;
2387
        }
2388
 
2389
        /*
2390
         * Start up serial port
2391
         */
2392
        retval = startup(info);
2393
        if (retval)
2394
                return retval;
2395
 
2396
        retval = block_til_ready(tty, filp, info);
2397
        if (retval) {
2398
#ifdef SERIAL_DEBUG_OPEN
2399
                printk("esp_open returning after block_til_ready with %d\n",
2400
                       retval);
2401
#endif
2402
                return retval;
2403
        }
2404
 
2405
        if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2406
                if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2407
                        *tty->termios = info->normal_termios;
2408
                else
2409
                        *tty->termios = info->callout_termios;
2410
                change_speed(info);
2411
        }
2412
 
2413
        info->session = current->session;
2414
        info->pgrp = current->pgrp;
2415
 
2416
#ifdef SERIAL_DEBUG_OPEN
2417
        printk("esp_open ttys%d successful...", info->line);
2418
#endif
2419
        return 0;
2420
}
2421
 
2422
/*
2423
 * ---------------------------------------------------------------------
2424
 * espserial_init() and friends
2425
 *
2426
 * espserial_init() is called at boot-time to initialize the serial driver.
2427
 * ---------------------------------------------------------------------
2428
 */
2429
 
2430
/*
2431
 * This routine prints out the appropriate serial driver version
2432
 * number, and identifies which options were configured into this
2433
 * driver.
2434
 */
2435
 
2436
static _INLINE_ void show_serial_version(void)
2437
{
2438
        printk(KERN_INFO "%s version %s (DMA %u)\n",
2439
                serial_name, serial_version, dma);
2440
}
2441
 
2442
/*
2443
 * This routine is called by espserial_init() to initialize a specific serial
2444
 * port.
2445
 */
2446
static _INLINE_ int autoconfig(struct esp_struct * info, int *region_start)
2447
{
2448
        int port_detected = 0;
2449
        unsigned long flags;
2450
 
2451
        save_flags(flags); cli();
2452
 
2453
        /*
2454
         * Check for ESP card
2455
         */
2456
 
2457
        if (!check_region(info->port, 8) &&
2458
            serial_in(info, UART_ESI_BASE) == 0xf3) {
2459
                serial_out(info, UART_ESI_CMD1, 0x00);
2460
                serial_out(info, UART_ESI_CMD1, 0x01);
2461
 
2462
                if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2463
                        port_detected = 1;
2464
 
2465
                        if (!(info->irq)) {
2466
                                serial_out(info, UART_ESI_CMD1, 0x02);
2467
 
2468
                                if (serial_in(info, UART_ESI_STAT1) & 0x01)
2469
                                        info->irq = 3;
2470
                                else
2471
                                        info->irq = 4;
2472
                        }
2473
 
2474
                        if (ports && (ports->port == (info->port - 8))) {
2475
                                release_region(*region_start,
2476
                                               info->port - *region_start);
2477
                        } else
2478
                                *region_start = info->port;
2479
 
2480
                        request_region(*region_start,
2481
                                       info->port - *region_start + 8,
2482
                                       "esp serial");
2483
 
2484
                        /* put card in enhanced mode */
2485
                        /* this prevents access through */
2486
                        /* the "old" IO ports */
2487
                        esp_basic_init(info);
2488
 
2489
                        /* clear out MCR */
2490
                        serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2491
                        serial_out(info, UART_ESI_CMD2, UART_MCR);
2492
                        serial_out(info, UART_ESI_CMD2, 0x00);
2493
                }
2494
        }
2495
 
2496
        restore_flags(flags);
2497
        return (port_detected);
2498
}
2499
 
2500
/*
2501
 * The serial driver boot-time initialization code!
2502
 */
2503
int __init espserial_init(void)
2504
{
2505
        int i, offset;
2506
        int region_start;
2507
        struct esp_struct * info;
2508
        struct esp_struct *last_primary = 0;
2509
        int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2510
 
2511
        init_bh(ESP_BH, do_serial_bh);
2512
 
2513
        for (i = 0; i < NR_PRIMARY; i++) {
2514
                if (irq[i] != 0) {
2515
                        if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2516
                            (irq[i] == 8) || (irq[i] == 13))
2517
                                irq[i] = 0;
2518
                        else if (irq[i] == 2)
2519
                                irq[i] = 9;
2520
                }
2521
        }
2522
 
2523
        if ((dma != 1) && (dma != 3))
2524
                dma = 0;
2525
 
2526
        if ((rx_trigger < 1) || (rx_trigger > 1023))
2527
                rx_trigger = 768;
2528
 
2529
        if ((tx_trigger < 1) || (tx_trigger > 1023))
2530
                tx_trigger = 768;
2531
 
2532
        if ((flow_off < 1) || (flow_off > 1023))
2533
                flow_off = 1016;
2534
 
2535
        if ((flow_on < 1) || (flow_on > 1023))
2536
                flow_on = 944;
2537
 
2538
        if ((rx_timeout < 0) || (rx_timeout > 255))
2539
                rx_timeout = 128;
2540
 
2541
        if (flow_on >= flow_off)
2542
                flow_on = flow_off - 1;
2543
 
2544
        show_serial_version();
2545
 
2546
        /* Initialize the tty_driver structure */
2547
 
2548
        memset(&esp_driver, 0, sizeof(struct tty_driver));
2549
        esp_driver.magic = TTY_DRIVER_MAGIC;
2550
        esp_driver.name = "ttyP";
2551
        esp_driver.major = ESP_IN_MAJOR;
2552
        esp_driver.minor_start = 0;
2553
        esp_driver.num = NR_PORTS;
2554
        esp_driver.type = TTY_DRIVER_TYPE_SERIAL;
2555
        esp_driver.subtype = SERIAL_TYPE_NORMAL;
2556
        esp_driver.init_termios = tty_std_termios;
2557
        esp_driver.init_termios.c_cflag =
2558
                B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2559
        esp_driver.flags = TTY_DRIVER_REAL_RAW;
2560
        esp_driver.refcount = &serial_refcount;
2561
        esp_driver.table = serial_table;
2562
        esp_driver.termios = serial_termios;
2563
        esp_driver.termios_locked = serial_termios_locked;
2564
 
2565
        esp_driver.open = esp_open;
2566
        esp_driver.close = rs_close;
2567
        esp_driver.write = rs_write;
2568
        esp_driver.put_char = rs_put_char;
2569
        esp_driver.flush_chars = rs_flush_chars;
2570
        esp_driver.write_room = rs_write_room;
2571
        esp_driver.chars_in_buffer = rs_chars_in_buffer;
2572
        esp_driver.flush_buffer = rs_flush_buffer;
2573
        esp_driver.ioctl = rs_ioctl;
2574
        esp_driver.throttle = rs_throttle;
2575
        esp_driver.unthrottle = rs_unthrottle;
2576
        esp_driver.set_termios = rs_set_termios;
2577
        esp_driver.stop = rs_stop;
2578
        esp_driver.start = rs_start;
2579
        esp_driver.hangup = esp_hangup;
2580
        esp_driver.break_ctl = esp_break;
2581
        esp_driver.wait_until_sent = rs_wait_until_sent;
2582
 
2583
        /*
2584
         * The callout device is just like normal device except for
2585
         * major number and the subtype code.
2586
         */
2587
        esp_callout_driver = esp_driver;
2588
        esp_callout_driver.name = "cup";
2589
        esp_callout_driver.major = ESP_OUT_MAJOR;
2590
        esp_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2591
 
2592
        if (tty_register_driver(&esp_driver))
2593
        {
2594
                printk(KERN_ERR "Couldn't register esp serial driver");
2595
                return 1;
2596
        }
2597
 
2598
        if (tty_register_driver(&esp_callout_driver))
2599
        {
2600
                printk(KERN_ERR "Couldn't register esp callout driver");
2601
                tty_unregister_driver(&esp_driver);
2602
                return 1;
2603
        }
2604
 
2605
        info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2606
 
2607
        if (!info)
2608
        {
2609
                printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2610
                tty_unregister_driver(&esp_driver);
2611
                tty_unregister_driver(&esp_callout_driver);
2612
                return 1;
2613
        }
2614
 
2615
        memset((void *)info, 0, sizeof(struct esp_struct));
2616
        /* rx_trigger, tx_trigger are needed by autoconfig */
2617
        info->config.rx_trigger = rx_trigger;
2618
        info->config.tx_trigger = tx_trigger;
2619
 
2620
        i = 0;
2621
        offset = 0;
2622
 
2623
        do {
2624
                info->port = esp[i] + offset;
2625
                info->irq = irq[i];
2626
                info->line = (i * 8) + (offset / 8);
2627
 
2628
                if (!autoconfig(info, &region_start)) {
2629
                        i++;
2630
                        offset = 0;
2631
                        continue;
2632
                }
2633
 
2634
                info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2635
                info->flags = STD_COM_FLAGS;
2636
                if (info->custom_divisor)
2637
                        info->flags |= ASYNC_SPD_CUST;
2638
                info->magic = ESP_MAGIC;
2639
                info->close_delay = 5*HZ/10;
2640
                info->closing_wait = 30*HZ;
2641
                info->tqueue.routine = do_softint;
2642
                info->tqueue.data = info;
2643
                info->tqueue_hangup.routine = do_serial_hangup;
2644
                info->tqueue_hangup.data = info;
2645
                info->callout_termios = esp_callout_driver.init_termios;
2646
                info->normal_termios = esp_driver.init_termios;
2647
                info->config.rx_timeout = rx_timeout;
2648
                info->config.flow_on = flow_on;
2649
                info->config.flow_off = flow_off;
2650
                info->config.pio_threshold = pio_threshold;
2651
                info->next_port = ports;
2652
                init_waitqueue_head(&info->open_wait);
2653
                init_waitqueue_head(&info->close_wait);
2654
                init_waitqueue_head(&info->delta_msr_wait);
2655
                init_waitqueue_head(&info->break_wait);
2656
                ports = info;
2657
                printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2658
                        info->line, info->port, info->irq);
2659
 
2660
                if (info->line % 8) {
2661
                        printk("secondary port\n");
2662
                        /* 8 port cards can't do DMA */
2663
                        info->stat_flags |= ESP_STAT_NEVER_DMA;
2664
 
2665
                        if (last_primary)
2666
                                last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2667
                } else {
2668
                        printk("primary port\n");
2669
                        last_primary = info;
2670
                        irq[i] = info->irq;
2671
                }
2672
 
2673
                if (!dma)
2674
                        info->stat_flags |= ESP_STAT_NEVER_DMA;
2675
 
2676
                info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2677
                if (!info)
2678
                {
2679
                        printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2680
 
2681
                        /* allow use of the already detected ports */
2682
                        return 0;
2683
                }
2684
 
2685
                memset((void *)info, 0, sizeof(struct esp_struct));
2686
                /* rx_trigger, tx_trigger are needed by autoconfig */
2687
                info->config.rx_trigger = rx_trigger;
2688
                info->config.tx_trigger = tx_trigger;
2689
 
2690
                if (offset == 56) {
2691
                        i++;
2692
                        offset = 0;
2693
                } else {
2694
                        offset += 8;
2695
                }
2696
        } while (i < NR_PRIMARY);
2697
 
2698
        /* free the last port memory allocation */
2699
        kfree(info);
2700
 
2701
        return 0;
2702
}
2703
 
2704
static void __exit espserial_exit(void)
2705
{
2706
        unsigned long flags;
2707
        int e1, e2;
2708
        unsigned int region_start, region_end;
2709
        struct esp_struct *temp_async;
2710
        struct esp_pio_buffer *pio_buf;
2711
 
2712
        /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2713
        save_flags(flags);
2714
        cli();
2715
        remove_bh(ESP_BH);
2716
        if ((e1 = tty_unregister_driver(&esp_driver)))
2717
                printk("SERIAL: failed to unregister serial driver (%d)\n",
2718
                       e1);
2719
        if ((e2 = tty_unregister_driver(&esp_callout_driver)))
2720
                printk("SERIAL: failed to unregister callout driver (%d)\n",
2721
                       e2);
2722
        restore_flags(flags);
2723
 
2724
        while (ports) {
2725
                if (ports->port) {
2726
                        region_start = region_end = ports->port;
2727
                        temp_async = ports;
2728
 
2729
                        while (temp_async) {
2730
                                if ((region_start - temp_async->port) == 8) {
2731
                                        region_start = temp_async->port;
2732
                                        temp_async->port = 0;
2733
                                        temp_async = ports;
2734
                                } else if ((temp_async->port - region_end)
2735
                                           == 8) {
2736
                                        region_end = temp_async->port;
2737
                                        temp_async->port = 0;
2738
                                        temp_async = ports;
2739
                                } else
2740
                                        temp_async = temp_async->next_port;
2741
                        }
2742
 
2743
                        release_region(region_start,
2744
                                       region_end - region_start + 8);
2745
                }
2746
 
2747
                temp_async = ports->next_port;
2748
                kfree(ports);
2749
                ports = temp_async;
2750
        }
2751
 
2752
        if (dma_buffer)
2753
                free_pages((unsigned long)dma_buffer,
2754
                        get_order(DMA_BUFFER_SZ));
2755
 
2756
        if (tmp_buf)
2757
                free_page((unsigned long)tmp_buf);
2758
 
2759
        while (free_pio_buf) {
2760
                pio_buf = free_pio_buf->next;
2761
                kfree(free_pio_buf);
2762
                free_pio_buf = pio_buf;
2763
        }
2764
}
2765
 
2766
module_init(espserial_init);
2767
module_exit(espserial_exit);

powered by: WebSVN 2.1.0

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