[PATCH] Typo fixes
[sfrench/cifs-2.6.git] / drivers / char / synclink.c
1 /*
2  * linux/drivers/char/synclink.c
3  *
4  * $Id: synclink.c,v 4.38 2005/11/07 16:30:34 paulkf Exp $
5  *
6  * Device driver for Microgate SyncLink ISA and PCI
7  * high speed multiprotocol serial adapters.
8  *
9  * written by Paul Fulghum for Microgate Corporation
10  * paulkf@microgate.com
11  *
12  * Microgate and SyncLink are trademarks of Microgate Corporation
13  *
14  * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
15  *
16  * Original release 01/11/99
17  *
18  * This code is released under the GNU General Public License (GPL)
19  *
20  * This driver is primarily intended for use in synchronous
21  * HDLC mode. Asynchronous mode is also provided.
22  *
23  * When operating in synchronous mode, each call to mgsl_write()
24  * contains exactly one complete HDLC frame. Calling mgsl_put_char
25  * will start assembling an HDLC frame that will not be sent until
26  * mgsl_flush_chars or mgsl_write is called.
27  * 
28  * Synchronous receive data is reported as complete frames. To accomplish
29  * this, the TTY flip buffer is bypassed (too small to hold largest
30  * frame and may fragment frames) and the line discipline
31  * receive entry point is called directly.
32  *
33  * This driver has been tested with a slightly modified ppp.c driver
34  * for synchronous PPP.
35  *
36  * 2000/02/16
37  * Added interface for syncppp.c driver (an alternate synchronous PPP
38  * implementation that also supports Cisco HDLC). Each device instance
39  * registers as a tty device AND a network device (if dosyncppp option
40  * is set for the device). The functionality is determined by which
41  * device interface is opened.
42  *
43  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
44  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
45  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
46  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
47  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
48  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
49  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53  * OF THE POSSIBILITY OF SUCH DAMAGE.
54  */
55
56 #if defined(__i386__)
57 #  define BREAKPOINT() asm("   int $3");
58 #else
59 #  define BREAKPOINT() { }
60 #endif
61
62 #define MAX_ISA_DEVICES 10
63 #define MAX_PCI_DEVICES 10
64 #define MAX_TOTAL_DEVICES 20
65
66 #include <linux/config.h>       
67 #include <linux/module.h>
68 #include <linux/errno.h>
69 #include <linux/signal.h>
70 #include <linux/sched.h>
71 #include <linux/timer.h>
72 #include <linux/interrupt.h>
73 #include <linux/pci.h>
74 #include <linux/tty.h>
75 #include <linux/tty_flip.h>
76 #include <linux/serial.h>
77 #include <linux/major.h>
78 #include <linux/string.h>
79 #include <linux/fcntl.h>
80 #include <linux/ptrace.h>
81 #include <linux/ioport.h>
82 #include <linux/mm.h>
83 #include <linux/slab.h>
84 #include <linux/delay.h>
85
86 #include <linux/netdevice.h>
87
88 #include <linux/vmalloc.h>
89 #include <linux/init.h>
90 #include <asm/serial.h>
91
92 #include <linux/delay.h>
93 #include <linux/ioctl.h>
94
95 #include <asm/system.h>
96 #include <asm/io.h>
97 #include <asm/irq.h>
98 #include <asm/dma.h>
99 #include <linux/bitops.h>
100 #include <asm/types.h>
101 #include <linux/termios.h>
102 #include <linux/workqueue.h>
103 #include <linux/hdlc.h>
104 #include <linux/dma-mapping.h>
105
106 #ifdef CONFIG_HDLC_MODULE
107 #define CONFIG_HDLC 1
108 #endif
109
110 #define GET_USER(error,value,addr) error = get_user(value,addr)
111 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
112 #define PUT_USER(error,value,addr) error = put_user(value,addr)
113 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
114
115 #include <asm/uaccess.h>
116
117 #include "linux/synclink.h"
118
119 #define RCLRVALUE 0xffff
120
121 static MGSL_PARAMS default_params = {
122         MGSL_MODE_HDLC,                 /* unsigned long mode */
123         0,                              /* unsigned char loopback; */
124         HDLC_FLAG_UNDERRUN_ABORT15,     /* unsigned short flags; */
125         HDLC_ENCODING_NRZI_SPACE,       /* unsigned char encoding; */
126         0,                              /* unsigned long clock_speed; */
127         0xff,                           /* unsigned char addr_filter; */
128         HDLC_CRC_16_CCITT,              /* unsigned short crc_type; */
129         HDLC_PREAMBLE_LENGTH_8BITS,     /* unsigned char preamble_length; */
130         HDLC_PREAMBLE_PATTERN_NONE,     /* unsigned char preamble; */
131         9600,                           /* unsigned long data_rate; */
132         8,                              /* unsigned char data_bits; */
133         1,                              /* unsigned char stop_bits; */
134         ASYNC_PARITY_NONE               /* unsigned char parity; */
135 };
136
137 #define SHARED_MEM_ADDRESS_SIZE 0x40000
138 #define BUFFERLISTSIZE (PAGE_SIZE)
139 #define DMABUFFERSIZE (PAGE_SIZE)
140 #define MAXRXFRAMES 7
141
142 typedef struct _DMABUFFERENTRY
143 {
144         u32 phys_addr;  /* 32-bit flat physical address of data buffer */
145         volatile u16 count;     /* buffer size/data count */
146         volatile u16 status;    /* Control/status field */
147         volatile u16 rcc;       /* character count field */
148         u16 reserved;   /* padding required by 16C32 */
149         u32 link;       /* 32-bit flat link to next buffer entry */
150         char *virt_addr;        /* virtual address of data buffer */
151         u32 phys_entry; /* physical address of this buffer entry */
152         dma_addr_t dma_addr;
153 } DMABUFFERENTRY, *DMAPBUFFERENTRY;
154
155 /* The queue of BH actions to be performed */
156
157 #define BH_RECEIVE  1
158 #define BH_TRANSMIT 2
159 #define BH_STATUS   4
160
161 #define IO_PIN_SHUTDOWN_LIMIT 100
162
163 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
164
165 struct  _input_signal_events {
166         int     ri_up;  
167         int     ri_down;
168         int     dsr_up;
169         int     dsr_down;
170         int     dcd_up;
171         int     dcd_down;
172         int     cts_up;
173         int     cts_down;
174 };
175
176 /* transmit holding buffer definitions*/
177 #define MAX_TX_HOLDING_BUFFERS 5
178 struct tx_holding_buffer {
179         int     buffer_size;
180         unsigned char * buffer;
181 };
182
183
184 /*
185  * Device instance data structure
186  */
187  
188 struct mgsl_struct {
189         int                     magic;
190         int                     flags;
191         int                     count;          /* count of opens */
192         int                     line;
193         int                     hw_version;
194         unsigned short          close_delay;
195         unsigned short          closing_wait;   /* time to wait before closing */
196         
197         struct mgsl_icount      icount;
198         
199         struct tty_struct       *tty;
200         int                     timeout;
201         int                     x_char;         /* xon/xoff character */
202         int                     blocked_open;   /* # of blocked opens */
203         u16                     read_status_mask;
204         u16                     ignore_status_mask;     
205         unsigned char           *xmit_buf;
206         int                     xmit_head;
207         int                     xmit_tail;
208         int                     xmit_cnt;
209         
210         wait_queue_head_t       open_wait;
211         wait_queue_head_t       close_wait;
212         
213         wait_queue_head_t       status_event_wait_q;
214         wait_queue_head_t       event_wait_q;
215         struct timer_list       tx_timer;       /* HDLC transmit timeout timer */
216         struct mgsl_struct      *next_device;   /* device list link */
217         
218         spinlock_t irq_spinlock;                /* spinlock for synchronizing with ISR */
219         struct work_struct task;                /* task structure for scheduling bh */
220
221         u32 EventMask;                  /* event trigger mask */
222         u32 RecordedEvents;             /* pending events */
223
224         u32 max_frame_size;             /* as set by device config */
225
226         u32 pending_bh;
227
228         int bh_running;         /* Protection from multiple */
229         int isr_overflow;
230         int bh_requested;
231         
232         int dcd_chkcount;               /* check counts to prevent */
233         int cts_chkcount;               /* too many IRQs if a signal */
234         int dsr_chkcount;               /* is floating */
235         int ri_chkcount;
236
237         char *buffer_list;              /* virtual address of Rx & Tx buffer lists */
238         u32 buffer_list_phys;
239         dma_addr_t buffer_list_dma_addr;
240
241         unsigned int rx_buffer_count;   /* count of total allocated Rx buffers */
242         DMABUFFERENTRY *rx_buffer_list; /* list of receive buffer entries */
243         unsigned int current_rx_buffer;
244
245         int num_tx_dma_buffers;         /* number of tx dma frames required */
246         int tx_dma_buffers_used;
247         unsigned int tx_buffer_count;   /* count of total allocated Tx buffers */
248         DMABUFFERENTRY *tx_buffer_list; /* list of transmit buffer entries */
249         int start_tx_dma_buffer;        /* tx dma buffer to start tx dma operation */
250         int current_tx_buffer;          /* next tx dma buffer to be loaded */
251         
252         unsigned char *intermediate_rxbuffer;
253
254         int num_tx_holding_buffers;     /* number of tx holding buffer allocated */
255         int get_tx_holding_index;       /* next tx holding buffer for adapter to load */
256         int put_tx_holding_index;       /* next tx holding buffer to store user request */
257         int tx_holding_count;           /* number of tx holding buffers waiting */
258         struct tx_holding_buffer tx_holding_buffers[MAX_TX_HOLDING_BUFFERS];
259
260         int rx_enabled;
261         int rx_overflow;
262         int rx_rcc_underrun;
263
264         int tx_enabled;
265         int tx_active;
266         u32 idle_mode;
267
268         u16 cmr_value;
269         u16 tcsr_value;
270
271         char device_name[25];           /* device instance name */
272
273         unsigned int bus_type;  /* expansion bus type (ISA,EISA,PCI) */
274         unsigned char bus;              /* expansion bus number (zero based) */
275         unsigned char function;         /* PCI device number */
276
277         unsigned int io_base;           /* base I/O address of adapter */
278         unsigned int io_addr_size;      /* size of the I/O address range */
279         int io_addr_requested;          /* nonzero if I/O address requested */
280         
281         unsigned int irq_level;         /* interrupt level */
282         unsigned long irq_flags;
283         int irq_requested;              /* nonzero if IRQ requested */
284         
285         unsigned int dma_level;         /* DMA channel */
286         int dma_requested;              /* nonzero if dma channel requested */
287
288         u16 mbre_bit;
289         u16 loopback_bits;
290         u16 usc_idle_mode;
291
292         MGSL_PARAMS params;             /* communications parameters */
293
294         unsigned char serial_signals;   /* current serial signal states */
295
296         int irq_occurred;               /* for diagnostics use */
297         unsigned int init_error;        /* Initialization startup error                 (DIAGS) */
298         int     fDiagnosticsmode;       /* Driver in Diagnostic mode?                   (DIAGS) */
299
300         u32 last_mem_alloc;
301         unsigned char* memory_base;     /* shared memory address (PCI only) */
302         u32 phys_memory_base;
303         int shared_mem_requested;
304
305         unsigned char* lcr_base;        /* local config registers (PCI only) */
306         u32 phys_lcr_base;
307         u32 lcr_offset;
308         int lcr_mem_requested;
309
310         u32 misc_ctrl_value;
311         char flag_buf[MAX_ASYNC_BUFFER_SIZE];
312         char char_buf[MAX_ASYNC_BUFFER_SIZE];   
313         BOOLEAN drop_rts_on_tx_done;
314
315         BOOLEAN loopmode_insert_requested;
316         BOOLEAN loopmode_send_done_requested;
317         
318         struct  _input_signal_events    input_signal_events;
319
320         /* generic HDLC device parts */
321         int netcount;
322         int dosyncppp;
323         spinlock_t netlock;
324
325 #ifdef CONFIG_HDLC
326         struct net_device *netdev;
327 #endif
328 };
329
330 #define MGSL_MAGIC 0x5401
331
332 /*
333  * The size of the serial xmit buffer is 1 page, or 4096 bytes
334  */
335 #ifndef SERIAL_XMIT_SIZE
336 #define SERIAL_XMIT_SIZE 4096
337 #endif
338
339 /*
340  * These macros define the offsets used in calculating the
341  * I/O address of the specified USC registers.
342  */
343
344
345 #define DCPIN 2         /* Bit 1 of I/O address */
346 #define SDPIN 4         /* Bit 2 of I/O address */
347
348 #define DCAR 0          /* DMA command/address register */
349 #define CCAR SDPIN              /* channel command/address register */
350 #define DATAREG DCPIN + SDPIN   /* serial data register */
351 #define MSBONLY 0x41
352 #define LSBONLY 0x40
353
354 /*
355  * These macros define the register address (ordinal number)
356  * used for writing address/value pairs to the USC.
357  */
358
359 #define CMR     0x02    /* Channel mode Register */
360 #define CCSR    0x04    /* Channel Command/status Register */
361 #define CCR     0x06    /* Channel Control Register */
362 #define PSR     0x08    /* Port status Register */
363 #define PCR     0x0a    /* Port Control Register */
364 #define TMDR    0x0c    /* Test mode Data Register */
365 #define TMCR    0x0e    /* Test mode Control Register */
366 #define CMCR    0x10    /* Clock mode Control Register */
367 #define HCR     0x12    /* Hardware Configuration Register */
368 #define IVR     0x14    /* Interrupt Vector Register */
369 #define IOCR    0x16    /* Input/Output Control Register */
370 #define ICR     0x18    /* Interrupt Control Register */
371 #define DCCR    0x1a    /* Daisy Chain Control Register */
372 #define MISR    0x1c    /* Misc Interrupt status Register */
373 #define SICR    0x1e    /* status Interrupt Control Register */
374 #define RDR     0x20    /* Receive Data Register */
375 #define RMR     0x22    /* Receive mode Register */
376 #define RCSR    0x24    /* Receive Command/status Register */
377 #define RICR    0x26    /* Receive Interrupt Control Register */
378 #define RSR     0x28    /* Receive Sync Register */
379 #define RCLR    0x2a    /* Receive count Limit Register */
380 #define RCCR    0x2c    /* Receive Character count Register */
381 #define TC0R    0x2e    /* Time Constant 0 Register */
382 #define TDR     0x30    /* Transmit Data Register */
383 #define TMR     0x32    /* Transmit mode Register */
384 #define TCSR    0x34    /* Transmit Command/status Register */
385 #define TICR    0x36    /* Transmit Interrupt Control Register */
386 #define TSR     0x38    /* Transmit Sync Register */
387 #define TCLR    0x3a    /* Transmit count Limit Register */
388 #define TCCR    0x3c    /* Transmit Character count Register */
389 #define TC1R    0x3e    /* Time Constant 1 Register */
390
391
392 /*
393  * MACRO DEFINITIONS FOR DMA REGISTERS
394  */
395
396 #define DCR     0x06    /* DMA Control Register (shared) */
397 #define DACR    0x08    /* DMA Array count Register (shared) */
398 #define BDCR    0x12    /* Burst/Dwell Control Register (shared) */
399 #define DIVR    0x14    /* DMA Interrupt Vector Register (shared) */    
400 #define DICR    0x18    /* DMA Interrupt Control Register (shared) */
401 #define CDIR    0x1a    /* Clear DMA Interrupt Register (shared) */
402 #define SDIR    0x1c    /* Set DMA Interrupt Register (shared) */
403
404 #define TDMR    0x02    /* Transmit DMA mode Register */
405 #define TDIAR   0x1e    /* Transmit DMA Interrupt Arm Register */
406 #define TBCR    0x2a    /* Transmit Byte count Register */
407 #define TARL    0x2c    /* Transmit Address Register (low) */
408 #define TARU    0x2e    /* Transmit Address Register (high) */
409 #define NTBCR   0x3a    /* Next Transmit Byte count Register */
410 #define NTARL   0x3c    /* Next Transmit Address Register (low) */
411 #define NTARU   0x3e    /* Next Transmit Address Register (high) */
412
413 #define RDMR    0x82    /* Receive DMA mode Register (non-shared) */
414 #define RDIAR   0x9e    /* Receive DMA Interrupt Arm Register */
415 #define RBCR    0xaa    /* Receive Byte count Register */
416 #define RARL    0xac    /* Receive Address Register (low) */
417 #define RARU    0xae    /* Receive Address Register (high) */
418 #define NRBCR   0xba    /* Next Receive Byte count Register */
419 #define NRARL   0xbc    /* Next Receive Address Register (low) */
420 #define NRARU   0xbe    /* Next Receive Address Register (high) */
421
422
423 /*
424  * MACRO DEFINITIONS FOR MODEM STATUS BITS
425  */
426
427 #define MODEMSTATUS_DTR 0x80
428 #define MODEMSTATUS_DSR 0x40
429 #define MODEMSTATUS_RTS 0x20
430 #define MODEMSTATUS_CTS 0x10
431 #define MODEMSTATUS_RI  0x04
432 #define MODEMSTATUS_DCD 0x01
433
434
435 /*
436  * Channel Command/Address Register (CCAR) Command Codes
437  */
438
439 #define RTCmd_Null                      0x0000
440 #define RTCmd_ResetHighestIus           0x1000
441 #define RTCmd_TriggerChannelLoadDma     0x2000
442 #define RTCmd_TriggerRxDma              0x2800
443 #define RTCmd_TriggerTxDma              0x3000
444 #define RTCmd_TriggerRxAndTxDma         0x3800
445 #define RTCmd_PurgeRxFifo               0x4800
446 #define RTCmd_PurgeTxFifo               0x5000
447 #define RTCmd_PurgeRxAndTxFifo          0x5800
448 #define RTCmd_LoadRcc                   0x6800
449 #define RTCmd_LoadTcc                   0x7000
450 #define RTCmd_LoadRccAndTcc             0x7800
451 #define RTCmd_LoadTC0                   0x8800
452 #define RTCmd_LoadTC1                   0x9000
453 #define RTCmd_LoadTC0AndTC1             0x9800
454 #define RTCmd_SerialDataLSBFirst        0xa000
455 #define RTCmd_SerialDataMSBFirst        0xa800
456 #define RTCmd_SelectBigEndian           0xb000
457 #define RTCmd_SelectLittleEndian        0xb800
458
459
460 /*
461  * DMA Command/Address Register (DCAR) Command Codes
462  */
463
464 #define DmaCmd_Null                     0x0000
465 #define DmaCmd_ResetTxChannel           0x1000
466 #define DmaCmd_ResetRxChannel           0x1200
467 #define DmaCmd_StartTxChannel           0x2000
468 #define DmaCmd_StartRxChannel           0x2200
469 #define DmaCmd_ContinueTxChannel        0x3000
470 #define DmaCmd_ContinueRxChannel        0x3200
471 #define DmaCmd_PauseTxChannel           0x4000
472 #define DmaCmd_PauseRxChannel           0x4200
473 #define DmaCmd_AbortTxChannel           0x5000
474 #define DmaCmd_AbortRxChannel           0x5200
475 #define DmaCmd_InitTxChannel            0x7000
476 #define DmaCmd_InitRxChannel            0x7200
477 #define DmaCmd_ResetHighestDmaIus       0x8000
478 #define DmaCmd_ResetAllChannels         0x9000
479 #define DmaCmd_StartAllChannels         0xa000
480 #define DmaCmd_ContinueAllChannels      0xb000
481 #define DmaCmd_PauseAllChannels         0xc000
482 #define DmaCmd_AbortAllChannels         0xd000
483 #define DmaCmd_InitAllChannels          0xf000
484
485 #define TCmd_Null                       0x0000
486 #define TCmd_ClearTxCRC                 0x2000
487 #define TCmd_SelectTicrTtsaData         0x4000
488 #define TCmd_SelectTicrTxFifostatus     0x5000
489 #define TCmd_SelectTicrIntLevel         0x6000
490 #define TCmd_SelectTicrdma_level                0x7000
491 #define TCmd_SendFrame                  0x8000
492 #define TCmd_SendAbort                  0x9000
493 #define TCmd_EnableDleInsertion         0xc000
494 #define TCmd_DisableDleInsertion        0xd000
495 #define TCmd_ClearEofEom                0xe000
496 #define TCmd_SetEofEom                  0xf000
497
498 #define RCmd_Null                       0x0000
499 #define RCmd_ClearRxCRC                 0x2000
500 #define RCmd_EnterHuntmode              0x3000
501 #define RCmd_SelectRicrRtsaData         0x4000
502 #define RCmd_SelectRicrRxFifostatus     0x5000
503 #define RCmd_SelectRicrIntLevel         0x6000
504 #define RCmd_SelectRicrdma_level                0x7000
505
506 /*
507  * Bits for enabling and disabling IRQs in Interrupt Control Register (ICR)
508  */
509  
510 #define RECEIVE_STATUS          BIT5
511 #define RECEIVE_DATA            BIT4
512 #define TRANSMIT_STATUS         BIT3
513 #define TRANSMIT_DATA           BIT2
514 #define IO_PIN                  BIT1
515 #define MISC                    BIT0
516
517
518 /*
519  * Receive status Bits in Receive Command/status Register RCSR
520  */
521
522 #define RXSTATUS_SHORT_FRAME            BIT8
523 #define RXSTATUS_CODE_VIOLATION         BIT8
524 #define RXSTATUS_EXITED_HUNT            BIT7
525 #define RXSTATUS_IDLE_RECEIVED          BIT6
526 #define RXSTATUS_BREAK_RECEIVED         BIT5
527 #define RXSTATUS_ABORT_RECEIVED         BIT5
528 #define RXSTATUS_RXBOUND                BIT4
529 #define RXSTATUS_CRC_ERROR              BIT3
530 #define RXSTATUS_FRAMING_ERROR          BIT3
531 #define RXSTATUS_ABORT                  BIT2
532 #define RXSTATUS_PARITY_ERROR           BIT2
533 #define RXSTATUS_OVERRUN                BIT1
534 #define RXSTATUS_DATA_AVAILABLE         BIT0
535 #define RXSTATUS_ALL                    0x01f6
536 #define usc_UnlatchRxstatusBits(a,b) usc_OutReg( (a), RCSR, (u16)((b) & RXSTATUS_ALL) )
537
538 /*
539  * Values for setting transmit idle mode in 
540  * Transmit Control/status Register (TCSR)
541  */
542 #define IDLEMODE_FLAGS                  0x0000
543 #define IDLEMODE_ALT_ONE_ZERO           0x0100
544 #define IDLEMODE_ZERO                   0x0200
545 #define IDLEMODE_ONE                    0x0300
546 #define IDLEMODE_ALT_MARK_SPACE         0x0500
547 #define IDLEMODE_SPACE                  0x0600
548 #define IDLEMODE_MARK                   0x0700
549 #define IDLEMODE_MASK                   0x0700
550
551 /*
552  * IUSC revision identifiers
553  */
554 #define IUSC_SL1660                     0x4d44
555 #define IUSC_PRE_SL1660                 0x4553
556
557 /*
558  * Transmit status Bits in Transmit Command/status Register (TCSR)
559  */
560
561 #define TCSR_PRESERVE                   0x0F00
562
563 #define TCSR_UNDERWAIT                  BIT11
564 #define TXSTATUS_PREAMBLE_SENT          BIT7
565 #define TXSTATUS_IDLE_SENT              BIT6
566 #define TXSTATUS_ABORT_SENT             BIT5
567 #define TXSTATUS_EOF_SENT               BIT4
568 #define TXSTATUS_EOM_SENT               BIT4
569 #define TXSTATUS_CRC_SENT               BIT3
570 #define TXSTATUS_ALL_SENT               BIT2
571 #define TXSTATUS_UNDERRUN               BIT1
572 #define TXSTATUS_FIFO_EMPTY             BIT0
573 #define TXSTATUS_ALL                    0x00fa
574 #define usc_UnlatchTxstatusBits(a,b) usc_OutReg( (a), TCSR, (u16)((a)->tcsr_value + ((b) & 0x00FF)) )
575                                 
576
577 #define MISCSTATUS_RXC_LATCHED          BIT15
578 #define MISCSTATUS_RXC                  BIT14
579 #define MISCSTATUS_TXC_LATCHED          BIT13
580 #define MISCSTATUS_TXC                  BIT12
581 #define MISCSTATUS_RI_LATCHED           BIT11
582 #define MISCSTATUS_RI                   BIT10
583 #define MISCSTATUS_DSR_LATCHED          BIT9
584 #define MISCSTATUS_DSR                  BIT8
585 #define MISCSTATUS_DCD_LATCHED          BIT7
586 #define MISCSTATUS_DCD                  BIT6
587 #define MISCSTATUS_CTS_LATCHED          BIT5
588 #define MISCSTATUS_CTS                  BIT4
589 #define MISCSTATUS_RCC_UNDERRUN         BIT3
590 #define MISCSTATUS_DPLL_NO_SYNC         BIT2
591 #define MISCSTATUS_BRG1_ZERO            BIT1
592 #define MISCSTATUS_BRG0_ZERO            BIT0
593
594 #define usc_UnlatchIostatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0xaaa0))
595 #define usc_UnlatchMiscstatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0x000f))
596
597 #define SICR_RXC_ACTIVE                 BIT15
598 #define SICR_RXC_INACTIVE               BIT14
599 #define SICR_RXC                        (BIT15+BIT14)
600 #define SICR_TXC_ACTIVE                 BIT13
601 #define SICR_TXC_INACTIVE               BIT12
602 #define SICR_TXC                        (BIT13+BIT12)
603 #define SICR_RI_ACTIVE                  BIT11
604 #define SICR_RI_INACTIVE                BIT10
605 #define SICR_RI                         (BIT11+BIT10)
606 #define SICR_DSR_ACTIVE                 BIT9
607 #define SICR_DSR_INACTIVE               BIT8
608 #define SICR_DSR                        (BIT9+BIT8)
609 #define SICR_DCD_ACTIVE                 BIT7
610 #define SICR_DCD_INACTIVE               BIT6
611 #define SICR_DCD                        (BIT7+BIT6)
612 #define SICR_CTS_ACTIVE                 BIT5
613 #define SICR_CTS_INACTIVE               BIT4
614 #define SICR_CTS                        (BIT5+BIT4)
615 #define SICR_RCC_UNDERFLOW              BIT3
616 #define SICR_DPLL_NO_SYNC               BIT2
617 #define SICR_BRG1_ZERO                  BIT1
618 #define SICR_BRG0_ZERO                  BIT0
619
620 void usc_DisableMasterIrqBit( struct mgsl_struct *info );
621 void usc_EnableMasterIrqBit( struct mgsl_struct *info );
622 void usc_EnableInterrupts( struct mgsl_struct *info, u16 IrqMask );
623 void usc_DisableInterrupts( struct mgsl_struct *info, u16 IrqMask );
624 void usc_ClearIrqPendingBits( struct mgsl_struct *info, u16 IrqMask );
625
626 #define usc_EnableInterrupts( a, b ) \
627         usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0xc0 + (b)) )
628
629 #define usc_DisableInterrupts( a, b ) \
630         usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0x80 + (b)) )
631
632 #define usc_EnableMasterIrqBit(a) \
633         usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0x0f00) + 0xb000) )
634
635 #define usc_DisableMasterIrqBit(a) \
636         usc_OutReg( (a), ICR, (u16)(usc_InReg((a),ICR) & 0x7f00) )
637
638 #define usc_ClearIrqPendingBits( a, b ) usc_OutReg( (a), DCCR, 0x40 + (b) )
639
640 /*
641  * Transmit status Bits in Transmit Control status Register (TCSR)
642  * and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0)
643  */
644
645 #define TXSTATUS_PREAMBLE_SENT  BIT7
646 #define TXSTATUS_IDLE_SENT      BIT6
647 #define TXSTATUS_ABORT_SENT     BIT5
648 #define TXSTATUS_EOF            BIT4
649 #define TXSTATUS_CRC_SENT       BIT3
650 #define TXSTATUS_ALL_SENT       BIT2
651 #define TXSTATUS_UNDERRUN       BIT1
652 #define TXSTATUS_FIFO_EMPTY     BIT0
653
654 #define DICR_MASTER             BIT15
655 #define DICR_TRANSMIT           BIT0
656 #define DICR_RECEIVE            BIT1
657
658 #define usc_EnableDmaInterrupts(a,b) \
659         usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) | (b)) )
660
661 #define usc_DisableDmaInterrupts(a,b) \
662         usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) & ~(b)) )
663
664 #define usc_EnableStatusIrqs(a,b) \
665         usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) | (b)) )
666
667 #define usc_DisablestatusIrqs(a,b) \
668         usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) & ~(b)) )
669
670 /* Transmit status Bits in Transmit Control status Register (TCSR) */
671 /* and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0) */
672
673
674 #define DISABLE_UNCONDITIONAL    0
675 #define DISABLE_END_OF_FRAME     1
676 #define ENABLE_UNCONDITIONAL     2
677 #define ENABLE_AUTO_CTS          3
678 #define ENABLE_AUTO_DCD          3
679 #define usc_EnableTransmitter(a,b) \
680         usc_OutReg( (a), TMR, (u16)((usc_InReg((a),TMR) & 0xfffc) | (b)) )
681 #define usc_EnableReceiver(a,b) \
682         usc_OutReg( (a), RMR, (u16)((usc_InReg((a),RMR) & 0xfffc) | (b)) )
683
684 static u16  usc_InDmaReg( struct mgsl_struct *info, u16 Port );
685 static void usc_OutDmaReg( struct mgsl_struct *info, u16 Port, u16 Value );
686 static void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd );
687
688 static u16  usc_InReg( struct mgsl_struct *info, u16 Port );
689 static void usc_OutReg( struct mgsl_struct *info, u16 Port, u16 Value );
690 static void usc_RTCmd( struct mgsl_struct *info, u16 Cmd );
691 void usc_RCmd( struct mgsl_struct *info, u16 Cmd );
692 void usc_TCmd( struct mgsl_struct *info, u16 Cmd );
693
694 #define usc_TCmd(a,b) usc_OutReg((a), TCSR, (u16)((a)->tcsr_value + (b)))
695 #define usc_RCmd(a,b) usc_OutReg((a), RCSR, (b))
696
697 #define usc_SetTransmitSyncChars(a,s0,s1) usc_OutReg((a), TSR, (u16)(((u16)s0<<8)|(u16)s1))
698
699 static void usc_process_rxoverrun_sync( struct mgsl_struct *info );
700 static void usc_start_receiver( struct mgsl_struct *info );
701 static void usc_stop_receiver( struct mgsl_struct *info );
702
703 static void usc_start_transmitter( struct mgsl_struct *info );
704 static void usc_stop_transmitter( struct mgsl_struct *info );
705 static void usc_set_txidle( struct mgsl_struct *info );
706 static void usc_load_txfifo( struct mgsl_struct *info );
707
708 static void usc_enable_aux_clock( struct mgsl_struct *info, u32 DataRate );
709 static void usc_enable_loopback( struct mgsl_struct *info, int enable );
710
711 static void usc_get_serial_signals( struct mgsl_struct *info );
712 static void usc_set_serial_signals( struct mgsl_struct *info );
713
714 static void usc_reset( struct mgsl_struct *info );
715
716 static void usc_set_sync_mode( struct mgsl_struct *info );
717 static void usc_set_sdlc_mode( struct mgsl_struct *info );
718 static void usc_set_async_mode( struct mgsl_struct *info );
719 static void usc_enable_async_clock( struct mgsl_struct *info, u32 DataRate );
720
721 static void usc_loopback_frame( struct mgsl_struct *info );
722
723 static void mgsl_tx_timeout(unsigned long context);
724
725
726 static void usc_loopmode_cancel_transmit( struct mgsl_struct * info );
727 static void usc_loopmode_insert_request( struct mgsl_struct * info );
728 static int usc_loopmode_active( struct mgsl_struct * info);
729 static void usc_loopmode_send_done( struct mgsl_struct * info );
730
731 static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg);
732
733 #ifdef CONFIG_HDLC
734 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
735 static void hdlcdev_tx_done(struct mgsl_struct *info);
736 static void hdlcdev_rx(struct mgsl_struct *info, char *buf, int size);
737 static int  hdlcdev_init(struct mgsl_struct *info);
738 static void hdlcdev_exit(struct mgsl_struct *info);
739 #endif
740
741 /*
742  * Defines a BUS descriptor value for the PCI adapter
743  * local bus address ranges.
744  */
745
746 #define BUS_DESCRIPTOR( WrHold, WrDly, RdDly, Nwdd, Nwad, Nxda, Nrdd, Nrad ) \
747 (0x00400020 + \
748 ((WrHold) << 30) + \
749 ((WrDly)  << 28) + \
750 ((RdDly)  << 26) + \
751 ((Nwdd)   << 20) + \
752 ((Nwad)   << 15) + \
753 ((Nxda)   << 13) + \
754 ((Nrdd)   << 11) + \
755 ((Nrad)   <<  6) )
756
757 static void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit);
758
759 /*
760  * Adapter diagnostic routines
761  */
762 static BOOLEAN mgsl_register_test( struct mgsl_struct *info );
763 static BOOLEAN mgsl_irq_test( struct mgsl_struct *info );
764 static BOOLEAN mgsl_dma_test( struct mgsl_struct *info );
765 static BOOLEAN mgsl_memory_test( struct mgsl_struct *info );
766 static int mgsl_adapter_test( struct mgsl_struct *info );
767
768 /*
769  * device and resource management routines
770  */
771 static int mgsl_claim_resources(struct mgsl_struct *info);
772 static void mgsl_release_resources(struct mgsl_struct *info);
773 static void mgsl_add_device(struct mgsl_struct *info);
774 static struct mgsl_struct* mgsl_allocate_device(void);
775
776 /*
777  * DMA buffer manupulation functions.
778  */
779 static void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex );
780 static int  mgsl_get_rx_frame( struct mgsl_struct *info );
781 static int  mgsl_get_raw_rx_frame( struct mgsl_struct *info );
782 static void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info );
783 static void mgsl_reset_tx_dma_buffers( struct mgsl_struct *info );
784 static int num_free_tx_dma_buffers(struct mgsl_struct *info);
785 static void mgsl_load_tx_dma_buffer( struct mgsl_struct *info, const char *Buffer, unsigned int BufferSize);
786 static void mgsl_load_pci_memory(char* TargetPtr, const char* SourcePtr, unsigned short count);
787
788 /*
789  * DMA and Shared Memory buffer allocation and formatting
790  */
791 static int  mgsl_allocate_dma_buffers(struct mgsl_struct *info);
792 static void mgsl_free_dma_buffers(struct mgsl_struct *info);
793 static int  mgsl_alloc_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
794 static void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
795 static int  mgsl_alloc_buffer_list_memory(struct mgsl_struct *info);
796 static void mgsl_free_buffer_list_memory(struct mgsl_struct *info);
797 static int mgsl_alloc_intermediate_rxbuffer_memory(struct mgsl_struct *info);
798 static void mgsl_free_intermediate_rxbuffer_memory(struct mgsl_struct *info);
799 static int mgsl_alloc_intermediate_txbuffer_memory(struct mgsl_struct *info);
800 static void mgsl_free_intermediate_txbuffer_memory(struct mgsl_struct *info);
801 static int load_next_tx_holding_buffer(struct mgsl_struct *info);
802 static int save_tx_buffer_request(struct mgsl_struct *info,const char *Buffer, unsigned int BufferSize);
803
804 /*
805  * Bottom half interrupt handlers
806  */
807 static void mgsl_bh_handler(void* Context);
808 static void mgsl_bh_receive(struct mgsl_struct *info);
809 static void mgsl_bh_transmit(struct mgsl_struct *info);
810 static void mgsl_bh_status(struct mgsl_struct *info);
811
812 /*
813  * Interrupt handler routines and dispatch table.
814  */
815 static void mgsl_isr_null( struct mgsl_struct *info );
816 static void mgsl_isr_transmit_data( struct mgsl_struct *info );
817 static void mgsl_isr_receive_data( struct mgsl_struct *info );
818 static void mgsl_isr_receive_status( struct mgsl_struct *info );
819 static void mgsl_isr_transmit_status( struct mgsl_struct *info );
820 static void mgsl_isr_io_pin( struct mgsl_struct *info );
821 static void mgsl_isr_misc( struct mgsl_struct *info );
822 static void mgsl_isr_receive_dma( struct mgsl_struct *info );
823 static void mgsl_isr_transmit_dma( struct mgsl_struct *info );
824
825 typedef void (*isr_dispatch_func)(struct mgsl_struct *);
826
827 static isr_dispatch_func UscIsrTable[7] =
828 {
829         mgsl_isr_null,
830         mgsl_isr_misc,
831         mgsl_isr_io_pin,
832         mgsl_isr_transmit_data,
833         mgsl_isr_transmit_status,
834         mgsl_isr_receive_data,
835         mgsl_isr_receive_status
836 };
837
838 /*
839  * ioctl call handlers
840  */
841 static int tiocmget(struct tty_struct *tty, struct file *file);
842 static int tiocmset(struct tty_struct *tty, struct file *file,
843                     unsigned int set, unsigned int clear);
844 static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount
845         __user *user_icount);
846 static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS  __user *user_params);
847 static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS  __user *new_params);
848 static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode);
849 static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode);
850 static int mgsl_txenable(struct mgsl_struct * info, int enable);
851 static int mgsl_txabort(struct mgsl_struct * info);
852 static int mgsl_rxenable(struct mgsl_struct * info, int enable);
853 static int mgsl_wait_event(struct mgsl_struct * info, int __user *mask);
854 static int mgsl_loopmode_send_done( struct mgsl_struct * info );
855
856 /* set non-zero on successful registration with PCI subsystem */
857 static int pci_registered;
858
859 /*
860  * Global linked list of SyncLink devices
861  */
862 static struct mgsl_struct *mgsl_device_list;
863 static int mgsl_device_count;
864
865 /*
866  * Set this param to non-zero to load eax with the
867  * .text section address and breakpoint on module load.
868  * This is useful for use with gdb and add-symbol-file command.
869  */
870 static int break_on_load;
871
872 /*
873  * Driver major number, defaults to zero to get auto
874  * assigned major number. May be forced as module parameter.
875  */
876 static int ttymajor;
877
878 /*
879  * Array of user specified options for ISA adapters.
880  */
881 static int io[MAX_ISA_DEVICES];
882 static int irq[MAX_ISA_DEVICES];
883 static int dma[MAX_ISA_DEVICES];
884 static int debug_level;
885 static int maxframe[MAX_TOTAL_DEVICES];
886 static int dosyncppp[MAX_TOTAL_DEVICES];
887 static int txdmabufs[MAX_TOTAL_DEVICES];
888 static int txholdbufs[MAX_TOTAL_DEVICES];
889         
890 module_param(break_on_load, bool, 0);
891 module_param(ttymajor, int, 0);
892 module_param_array(io, int, NULL, 0);
893 module_param_array(irq, int, NULL, 0);
894 module_param_array(dma, int, NULL, 0);
895 module_param(debug_level, int, 0);
896 module_param_array(maxframe, int, NULL, 0);
897 module_param_array(dosyncppp, int, NULL, 0);
898 module_param_array(txdmabufs, int, NULL, 0);
899 module_param_array(txholdbufs, int, NULL, 0);
900
901 static char *driver_name = "SyncLink serial driver";
902 static char *driver_version = "$Revision: 4.38 $";
903
904 static int synclink_init_one (struct pci_dev *dev,
905                                      const struct pci_device_id *ent);
906 static void synclink_remove_one (struct pci_dev *dev);
907
908 static struct pci_device_id synclink_pci_tbl[] = {
909         { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_USC, PCI_ANY_ID, PCI_ANY_ID, },
910         { PCI_VENDOR_ID_MICROGATE, 0x0210, PCI_ANY_ID, PCI_ANY_ID, },
911         { 0, }, /* terminate list */
912 };
913 MODULE_DEVICE_TABLE(pci, synclink_pci_tbl);
914
915 MODULE_LICENSE("GPL");
916
917 static struct pci_driver synclink_pci_driver = {
918         .name           = "synclink",
919         .id_table       = synclink_pci_tbl,
920         .probe          = synclink_init_one,
921         .remove         = __devexit_p(synclink_remove_one),
922 };
923
924 static struct tty_driver *serial_driver;
925
926 /* number of characters left in xmit buffer before we ask for more */
927 #define WAKEUP_CHARS 256
928
929
930 static void mgsl_change_params(struct mgsl_struct *info);
931 static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout);
932
933 /*
934  * 1st function defined in .text section. Calling this function in
935  * init_module() followed by a breakpoint allows a remote debugger
936  * (gdb) to get the .text address for the add-symbol-file command.
937  * This allows remote debugging of dynamically loadable modules.
938  */
939 static void* mgsl_get_text_ptr(void)
940 {
941         return mgsl_get_text_ptr;
942 }
943
944 static inline int mgsl_paranoia_check(struct mgsl_struct *info,
945                                         char *name, const char *routine)
946 {
947 #ifdef MGSL_PARANOIA_CHECK
948         static const char *badmagic =
949                 "Warning: bad magic number for mgsl struct (%s) in %s\n";
950         static const char *badinfo =
951                 "Warning: null mgsl_struct for (%s) in %s\n";
952
953         if (!info) {
954                 printk(badinfo, name, routine);
955                 return 1;
956         }
957         if (info->magic != MGSL_MAGIC) {
958                 printk(badmagic, name, routine);
959                 return 1;
960         }
961 #else
962         if (!info)
963                 return 1;
964 #endif
965         return 0;
966 }
967
968 /**
969  * line discipline callback wrappers
970  *
971  * The wrappers maintain line discipline references
972  * while calling into the line discipline.
973  *
974  * ldisc_receive_buf  - pass receive data to line discipline
975  */
976
977 static void ldisc_receive_buf(struct tty_struct *tty,
978                               const __u8 *data, char *flags, int count)
979 {
980         struct tty_ldisc *ld;
981         if (!tty)
982                 return;
983         ld = tty_ldisc_ref(tty);
984         if (ld) {
985                 if (ld->receive_buf)
986                         ld->receive_buf(tty, data, flags, count);
987                 tty_ldisc_deref(ld);
988         }
989 }
990
991 /* mgsl_stop()          throttle (stop) transmitter
992  *      
993  * Arguments:           tty     pointer to tty info structure
994  * Return Value:        None
995  */
996 static void mgsl_stop(struct tty_struct *tty)
997 {
998         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
999         unsigned long flags;
1000         
1001         if (mgsl_paranoia_check(info, tty->name, "mgsl_stop"))
1002                 return;
1003         
1004         if ( debug_level >= DEBUG_LEVEL_INFO )
1005                 printk("mgsl_stop(%s)\n",info->device_name);    
1006                 
1007         spin_lock_irqsave(&info->irq_spinlock,flags);
1008         if (info->tx_enabled)
1009                 usc_stop_transmitter(info);
1010         spin_unlock_irqrestore(&info->irq_spinlock,flags);
1011         
1012 }       /* end of mgsl_stop() */
1013
1014 /* mgsl_start()         release (start) transmitter
1015  *      
1016  * Arguments:           tty     pointer to tty info structure
1017  * Return Value:        None
1018  */
1019 static void mgsl_start(struct tty_struct *tty)
1020 {
1021         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
1022         unsigned long flags;
1023         
1024         if (mgsl_paranoia_check(info, tty->name, "mgsl_start"))
1025                 return;
1026         
1027         if ( debug_level >= DEBUG_LEVEL_INFO )
1028                 printk("mgsl_start(%s)\n",info->device_name);   
1029                 
1030         spin_lock_irqsave(&info->irq_spinlock,flags);
1031         if (!info->tx_enabled)
1032                 usc_start_transmitter(info);
1033         spin_unlock_irqrestore(&info->irq_spinlock,flags);
1034         
1035 }       /* end of mgsl_start() */
1036
1037 /*
1038  * Bottom half work queue access functions
1039  */
1040
1041 /* mgsl_bh_action()     Return next bottom half action to perform.
1042  * Return Value:        BH action code or 0 if nothing to do.
1043  */
1044 static int mgsl_bh_action(struct mgsl_struct *info)
1045 {
1046         unsigned long flags;
1047         int rc = 0;
1048         
1049         spin_lock_irqsave(&info->irq_spinlock,flags);
1050
1051         if (info->pending_bh & BH_RECEIVE) {
1052                 info->pending_bh &= ~BH_RECEIVE;
1053                 rc = BH_RECEIVE;
1054         } else if (info->pending_bh & BH_TRANSMIT) {
1055                 info->pending_bh &= ~BH_TRANSMIT;
1056                 rc = BH_TRANSMIT;
1057         } else if (info->pending_bh & BH_STATUS) {
1058                 info->pending_bh &= ~BH_STATUS;
1059                 rc = BH_STATUS;
1060         }
1061
1062         if (!rc) {
1063                 /* Mark BH routine as complete */
1064                 info->bh_running   = 0;
1065                 info->bh_requested = 0;
1066         }
1067         
1068         spin_unlock_irqrestore(&info->irq_spinlock,flags);
1069         
1070         return rc;
1071 }
1072
1073 /*
1074  *      Perform bottom half processing of work items queued by ISR.
1075  */
1076 static void mgsl_bh_handler(void* Context)
1077 {
1078         struct mgsl_struct *info = (struct mgsl_struct*)Context;
1079         int action;
1080
1081         if (!info)
1082                 return;
1083                 
1084         if ( debug_level >= DEBUG_LEVEL_BH )
1085                 printk( "%s(%d):mgsl_bh_handler(%s) entry\n",
1086                         __FILE__,__LINE__,info->device_name);
1087         
1088         info->bh_running = 1;
1089
1090         while((action = mgsl_bh_action(info)) != 0) {
1091         
1092                 /* Process work item */
1093                 if ( debug_level >= DEBUG_LEVEL_BH )
1094                         printk( "%s(%d):mgsl_bh_handler() work item action=%d\n",
1095                                 __FILE__,__LINE__,action);
1096
1097                 switch (action) {
1098                 
1099                 case BH_RECEIVE:
1100                         mgsl_bh_receive(info);
1101                         break;
1102                 case BH_TRANSMIT:
1103                         mgsl_bh_transmit(info);
1104                         break;
1105                 case BH_STATUS:
1106                         mgsl_bh_status(info);
1107                         break;
1108                 default:
1109                         /* unknown work item ID */
1110                         printk("Unknown work item ID=%08X!\n", action);
1111                         break;
1112                 }
1113         }
1114
1115         if ( debug_level >= DEBUG_LEVEL_BH )
1116                 printk( "%s(%d):mgsl_bh_handler(%s) exit\n",
1117                         __FILE__,__LINE__,info->device_name);
1118 }
1119
1120 static void mgsl_bh_receive(struct mgsl_struct *info)
1121 {
1122         int (*get_rx_frame)(struct mgsl_struct *info) =
1123                 (info->params.mode == MGSL_MODE_HDLC ? mgsl_get_rx_frame : mgsl_get_raw_rx_frame);
1124
1125         if ( debug_level >= DEBUG_LEVEL_BH )
1126                 printk( "%s(%d):mgsl_bh_receive(%s)\n",
1127                         __FILE__,__LINE__,info->device_name);
1128         
1129         do
1130         {
1131                 if (info->rx_rcc_underrun) {
1132                         unsigned long flags;
1133                         spin_lock_irqsave(&info->irq_spinlock,flags);
1134                         usc_start_receiver(info);
1135                         spin_unlock_irqrestore(&info->irq_spinlock,flags);
1136                         return;
1137                 }
1138         } while(get_rx_frame(info));
1139 }
1140
1141 static void mgsl_bh_transmit(struct mgsl_struct *info)
1142 {
1143         struct tty_struct *tty = info->tty;
1144         unsigned long flags;
1145         
1146         if ( debug_level >= DEBUG_LEVEL_BH )
1147                 printk( "%s(%d):mgsl_bh_transmit() entry on %s\n",
1148                         __FILE__,__LINE__,info->device_name);
1149
1150         if (tty) {
1151                 tty_wakeup(tty);
1152                 wake_up_interruptible(&tty->write_wait);
1153         }
1154
1155         /* if transmitter idle and loopmode_send_done_requested
1156          * then start echoing RxD to TxD
1157          */
1158         spin_lock_irqsave(&info->irq_spinlock,flags);
1159         if ( !info->tx_active && info->loopmode_send_done_requested )
1160                 usc_loopmode_send_done( info );
1161         spin_unlock_irqrestore(&info->irq_spinlock,flags);
1162 }
1163
1164 static void mgsl_bh_status(struct mgsl_struct *info)
1165 {
1166         if ( debug_level >= DEBUG_LEVEL_BH )
1167                 printk( "%s(%d):mgsl_bh_status() entry on %s\n",
1168                         __FILE__,__LINE__,info->device_name);
1169
1170         info->ri_chkcount = 0;
1171         info->dsr_chkcount = 0;
1172         info->dcd_chkcount = 0;
1173         info->cts_chkcount = 0;
1174 }
1175
1176 /* mgsl_isr_receive_status()
1177  * 
1178  *      Service a receive status interrupt. The type of status
1179  *      interrupt is indicated by the state of the RCSR.
1180  *      This is only used for HDLC mode.
1181  *
1182  * Arguments:           info    pointer to device instance data
1183  * Return Value:        None
1184  */
1185 static void mgsl_isr_receive_status( struct mgsl_struct *info )
1186 {
1187         u16 status = usc_InReg( info, RCSR );
1188
1189         if ( debug_level >= DEBUG_LEVEL_ISR )   
1190                 printk("%s(%d):mgsl_isr_receive_status status=%04X\n",
1191                         __FILE__,__LINE__,status);
1192                         
1193         if ( (status & RXSTATUS_ABORT_RECEIVED) && 
1194                 info->loopmode_insert_requested &&
1195                 usc_loopmode_active(info) )
1196         {
1197                 ++info->icount.rxabort;
1198                 info->loopmode_insert_requested = FALSE;
1199  
1200                 /* clear CMR:13 to start echoing RxD to TxD */
1201                 info->cmr_value &= ~BIT13;
1202                 usc_OutReg(info, CMR, info->cmr_value);
1203  
1204                 /* disable received abort irq (no longer required) */
1205                 usc_OutReg(info, RICR,
1206                         (usc_InReg(info, RICR) & ~RXSTATUS_ABORT_RECEIVED));
1207         }
1208
1209         if (status & (RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED)) {
1210                 if (status & RXSTATUS_EXITED_HUNT)
1211                         info->icount.exithunt++;
1212                 if (status & RXSTATUS_IDLE_RECEIVED)
1213                         info->icount.rxidle++;
1214                 wake_up_interruptible(&info->event_wait_q);
1215         }
1216
1217         if (status & RXSTATUS_OVERRUN){
1218                 info->icount.rxover++;
1219                 usc_process_rxoverrun_sync( info );
1220         }
1221
1222         usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
1223         usc_UnlatchRxstatusBits( info, status );
1224
1225 }       /* end of mgsl_isr_receive_status() */
1226
1227 /* mgsl_isr_transmit_status()
1228  * 
1229  *      Service a transmit status interrupt
1230  *      HDLC mode :end of transmit frame
1231  *      Async mode:all data is sent
1232  *      transmit status is indicated by bits in the TCSR.
1233  * 
1234  * Arguments:           info           pointer to device instance data
1235  * Return Value:        None
1236  */
1237 static void mgsl_isr_transmit_status( struct mgsl_struct *info )
1238 {
1239         u16 status = usc_InReg( info, TCSR );
1240
1241         if ( debug_level >= DEBUG_LEVEL_ISR )   
1242                 printk("%s(%d):mgsl_isr_transmit_status status=%04X\n",
1243                         __FILE__,__LINE__,status);
1244         
1245         usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
1246         usc_UnlatchTxstatusBits( info, status );
1247         
1248         if ( status & (TXSTATUS_UNDERRUN | TXSTATUS_ABORT_SENT) )
1249         {
1250                 /* finished sending HDLC abort. This may leave  */
1251                 /* the TxFifo with data from the aborted frame  */
1252                 /* so purge the TxFifo. Also shutdown the DMA   */
1253                 /* channel in case there is data remaining in   */
1254                 /* the DMA buffer                               */
1255                 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
1256                 usc_RTCmd( info, RTCmd_PurgeTxFifo );
1257         }
1258  
1259         if ( status & TXSTATUS_EOF_SENT )
1260                 info->icount.txok++;
1261         else if ( status & TXSTATUS_UNDERRUN )
1262                 info->icount.txunder++;
1263         else if ( status & TXSTATUS_ABORT_SENT )
1264                 info->icount.txabort++;
1265         else
1266                 info->icount.txunder++;
1267                         
1268         info->tx_active = 0;
1269         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1270         del_timer(&info->tx_timer);     
1271         
1272         if ( info->drop_rts_on_tx_done ) {
1273                 usc_get_serial_signals( info );
1274                 if ( info->serial_signals & SerialSignal_RTS ) {
1275                         info->serial_signals &= ~SerialSignal_RTS;
1276                         usc_set_serial_signals( info );
1277                 }
1278                 info->drop_rts_on_tx_done = 0;
1279         }
1280
1281 #ifdef CONFIG_HDLC
1282         if (info->netcount)
1283                 hdlcdev_tx_done(info);
1284         else 
1285 #endif
1286         {
1287                 if (info->tty->stopped || info->tty->hw_stopped) {
1288                         usc_stop_transmitter(info);
1289                         return;
1290                 }
1291                 info->pending_bh |= BH_TRANSMIT;
1292         }
1293
1294 }       /* end of mgsl_isr_transmit_status() */
1295
1296 /* mgsl_isr_io_pin()
1297  * 
1298  *      Service an Input/Output pin interrupt. The type of
1299  *      interrupt is indicated by bits in the MISR
1300  *      
1301  * Arguments:           info           pointer to device instance data
1302  * Return Value:        None
1303  */
1304 static void mgsl_isr_io_pin( struct mgsl_struct *info )
1305 {
1306         struct  mgsl_icount *icount;
1307         u16 status = usc_InReg( info, MISR );
1308
1309         if ( debug_level >= DEBUG_LEVEL_ISR )   
1310                 printk("%s(%d):mgsl_isr_io_pin status=%04X\n",
1311                         __FILE__,__LINE__,status);
1312                         
1313         usc_ClearIrqPendingBits( info, IO_PIN );
1314         usc_UnlatchIostatusBits( info, status );
1315
1316         if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
1317                       MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
1318                 icount = &info->icount;
1319                 /* update input line counters */
1320                 if (status & MISCSTATUS_RI_LATCHED) {
1321                         if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1322                                 usc_DisablestatusIrqs(info,SICR_RI);
1323                         icount->rng++;
1324                         if ( status & MISCSTATUS_RI )
1325                                 info->input_signal_events.ri_up++;      
1326                         else
1327                                 info->input_signal_events.ri_down++;    
1328                 }
1329                 if (status & MISCSTATUS_DSR_LATCHED) {
1330                         if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1331                                 usc_DisablestatusIrqs(info,SICR_DSR);
1332                         icount->dsr++;
1333                         if ( status & MISCSTATUS_DSR )
1334                                 info->input_signal_events.dsr_up++;
1335                         else
1336                                 info->input_signal_events.dsr_down++;
1337                 }
1338                 if (status & MISCSTATUS_DCD_LATCHED) {
1339                         if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1340                                 usc_DisablestatusIrqs(info,SICR_DCD);
1341                         icount->dcd++;
1342                         if (status & MISCSTATUS_DCD) {
1343                                 info->input_signal_events.dcd_up++;
1344                         } else
1345                                 info->input_signal_events.dcd_down++;
1346 #ifdef CONFIG_HDLC
1347                         if (info->netcount)
1348                                 hdlc_set_carrier(status & MISCSTATUS_DCD, info->netdev);
1349 #endif
1350                 }
1351                 if (status & MISCSTATUS_CTS_LATCHED)
1352                 {
1353                         if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1354                                 usc_DisablestatusIrqs(info,SICR_CTS);
1355                         icount->cts++;
1356                         if ( status & MISCSTATUS_CTS )
1357                                 info->input_signal_events.cts_up++;
1358                         else
1359                                 info->input_signal_events.cts_down++;
1360                 }
1361                 wake_up_interruptible(&info->status_event_wait_q);
1362                 wake_up_interruptible(&info->event_wait_q);
1363
1364                 if ( (info->flags & ASYNC_CHECK_CD) && 
1365                      (status & MISCSTATUS_DCD_LATCHED) ) {
1366                         if ( debug_level >= DEBUG_LEVEL_ISR )
1367                                 printk("%s CD now %s...", info->device_name,
1368                                        (status & MISCSTATUS_DCD) ? "on" : "off");
1369                         if (status & MISCSTATUS_DCD)
1370                                 wake_up_interruptible(&info->open_wait);
1371                         else {
1372                                 if ( debug_level >= DEBUG_LEVEL_ISR )
1373                                         printk("doing serial hangup...");
1374                                 if (info->tty)
1375                                         tty_hangup(info->tty);
1376                         }
1377                 }
1378         
1379                 if ( (info->flags & ASYNC_CTS_FLOW) && 
1380                      (status & MISCSTATUS_CTS_LATCHED) ) {
1381                         if (info->tty->hw_stopped) {
1382                                 if (status & MISCSTATUS_CTS) {
1383                                         if ( debug_level >= DEBUG_LEVEL_ISR )
1384                                                 printk("CTS tx start...");
1385                                         if (info->tty)
1386                                                 info->tty->hw_stopped = 0;
1387                                         usc_start_transmitter(info);
1388                                         info->pending_bh |= BH_TRANSMIT;
1389                                         return;
1390                                 }
1391                         } else {
1392                                 if (!(status & MISCSTATUS_CTS)) {
1393                                         if ( debug_level >= DEBUG_LEVEL_ISR )
1394                                                 printk("CTS tx stop...");
1395                                         if (info->tty)
1396                                                 info->tty->hw_stopped = 1;
1397                                         usc_stop_transmitter(info);
1398                                 }
1399                         }
1400                 }
1401         }
1402
1403         info->pending_bh |= BH_STATUS;
1404         
1405         /* for diagnostics set IRQ flag */
1406         if ( status & MISCSTATUS_TXC_LATCHED ){
1407                 usc_OutReg( info, SICR,
1408                         (unsigned short)(usc_InReg(info,SICR) & ~(SICR_TXC_ACTIVE+SICR_TXC_INACTIVE)) );
1409                 usc_UnlatchIostatusBits( info, MISCSTATUS_TXC_LATCHED );
1410                 info->irq_occurred = 1;
1411         }
1412
1413 }       /* end of mgsl_isr_io_pin() */
1414
1415 /* mgsl_isr_transmit_data()
1416  * 
1417  *      Service a transmit data interrupt (async mode only).
1418  * 
1419  * Arguments:           info    pointer to device instance data
1420  * Return Value:        None
1421  */
1422 static void mgsl_isr_transmit_data( struct mgsl_struct *info )
1423 {
1424         if ( debug_level >= DEBUG_LEVEL_ISR )   
1425                 printk("%s(%d):mgsl_isr_transmit_data xmit_cnt=%d\n",
1426                         __FILE__,__LINE__,info->xmit_cnt);
1427                         
1428         usc_ClearIrqPendingBits( info, TRANSMIT_DATA );
1429         
1430         if (info->tty->stopped || info->tty->hw_stopped) {
1431                 usc_stop_transmitter(info);
1432                 return;
1433         }
1434         
1435         if ( info->xmit_cnt )
1436                 usc_load_txfifo( info );
1437         else
1438                 info->tx_active = 0;
1439                 
1440         if (info->xmit_cnt < WAKEUP_CHARS)
1441                 info->pending_bh |= BH_TRANSMIT;
1442
1443 }       /* end of mgsl_isr_transmit_data() */
1444
1445 /* mgsl_isr_receive_data()
1446  * 
1447  *      Service a receive data interrupt. This occurs
1448  *      when operating in asynchronous interrupt transfer mode.
1449  *      The receive data FIFO is flushed to the receive data buffers. 
1450  * 
1451  * Arguments:           info            pointer to device instance data
1452  * Return Value:        None
1453  */
1454 static void mgsl_isr_receive_data( struct mgsl_struct *info )
1455 {
1456         int Fifocount;
1457         u16 status;
1458         int work = 0;
1459         unsigned char DataByte;
1460         struct tty_struct *tty = info->tty;
1461         struct  mgsl_icount *icount = &info->icount;
1462         
1463         if ( debug_level >= DEBUG_LEVEL_ISR )   
1464                 printk("%s(%d):mgsl_isr_receive_data\n",
1465                         __FILE__,__LINE__);
1466
1467         usc_ClearIrqPendingBits( info, RECEIVE_DATA );
1468         
1469         /* select FIFO status for RICR readback */
1470         usc_RCmd( info, RCmd_SelectRicrRxFifostatus );
1471
1472         /* clear the Wordstatus bit so that status readback */
1473         /* only reflects the status of this byte */
1474         usc_OutReg( info, RICR+LSBONLY, (u16)(usc_InReg(info, RICR+LSBONLY) & ~BIT3 ));
1475
1476         /* flush the receive FIFO */
1477
1478         while( (Fifocount = (usc_InReg(info,RICR) >> 8)) ) {
1479                 int flag;
1480
1481                 /* read one byte from RxFIFO */
1482                 outw( (inw(info->io_base + CCAR) & 0x0780) | (RDR+LSBONLY),
1483                       info->io_base + CCAR );
1484                 DataByte = inb( info->io_base + CCAR );
1485
1486                 /* get the status of the received byte */
1487                 status = usc_InReg(info, RCSR);
1488                 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
1489                                 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) )
1490                         usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
1491                 
1492                 icount->rx++;
1493                 
1494                 flag = 0;
1495                 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
1496                                 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) ) {
1497                         printk("rxerr=%04X\n",status);                                  
1498                         /* update error statistics */
1499                         if ( status & RXSTATUS_BREAK_RECEIVED ) {
1500                                 status &= ~(RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR);
1501                                 icount->brk++;
1502                         } else if (status & RXSTATUS_PARITY_ERROR) 
1503                                 icount->parity++;
1504                         else if (status & RXSTATUS_FRAMING_ERROR)
1505                                 icount->frame++;
1506                         else if (status & RXSTATUS_OVERRUN) {
1507                                 /* must issue purge fifo cmd before */
1508                                 /* 16C32 accepts more receive chars */
1509                                 usc_RTCmd(info,RTCmd_PurgeRxFifo);
1510                                 icount->overrun++;
1511                         }
1512
1513                         /* discard char if tty control flags say so */                                  
1514                         if (status & info->ignore_status_mask)
1515                                 continue;
1516                                 
1517                         status &= info->read_status_mask;
1518                 
1519                         if (status & RXSTATUS_BREAK_RECEIVED) {
1520                                 flag = TTY_BREAK;
1521                                 if (info->flags & ASYNC_SAK)
1522                                         do_SAK(tty);
1523                         } else if (status & RXSTATUS_PARITY_ERROR)
1524                                 flag = TTY_PARITY;
1525                         else if (status & RXSTATUS_FRAMING_ERROR)
1526                                 flag = TTY_FRAME;
1527                 }       /* end of if (error) */
1528                 tty_insert_flip_char(tty, DataByte, flag);
1529                 if (status & RXSTATUS_OVERRUN) {
1530                         /* Overrun is special, since it's
1531                          * reported immediately, and doesn't
1532                          * affect the current character
1533                          */
1534                         work += tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1535                 }
1536         }
1537
1538         if ( debug_level >= DEBUG_LEVEL_ISR ) {
1539                 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1540                         __FILE__,__LINE__,icount->rx,icount->brk,
1541                         icount->parity,icount->frame,icount->overrun);
1542         }
1543                         
1544         if(work)
1545                 tty_flip_buffer_push(tty);
1546 }
1547
1548 /* mgsl_isr_misc()
1549  * 
1550  *      Service a miscellaneos interrupt source.
1551  *      
1552  * Arguments:           info            pointer to device extension (instance data)
1553  * Return Value:        None
1554  */
1555 static void mgsl_isr_misc( struct mgsl_struct *info )
1556 {
1557         u16 status = usc_InReg( info, MISR );
1558
1559         if ( debug_level >= DEBUG_LEVEL_ISR )   
1560                 printk("%s(%d):mgsl_isr_misc status=%04X\n",
1561                         __FILE__,__LINE__,status);
1562                         
1563         if ((status & MISCSTATUS_RCC_UNDERRUN) &&
1564             (info->params.mode == MGSL_MODE_HDLC)) {
1565
1566                 /* turn off receiver and rx DMA */
1567                 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
1568                 usc_DmaCmd(info, DmaCmd_ResetRxChannel);
1569                 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
1570                 usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS);
1571                 usc_DisableInterrupts(info, RECEIVE_DATA + RECEIVE_STATUS);
1572
1573                 /* schedule BH handler to restart receiver */
1574                 info->pending_bh |= BH_RECEIVE;
1575                 info->rx_rcc_underrun = 1;
1576         }
1577
1578         usc_ClearIrqPendingBits( info, MISC );
1579         usc_UnlatchMiscstatusBits( info, status );
1580
1581 }       /* end of mgsl_isr_misc() */
1582
1583 /* mgsl_isr_null()
1584  *
1585  *      Services undefined interrupt vectors from the
1586  *      USC. (hence this function SHOULD never be called)
1587  * 
1588  * Arguments:           info            pointer to device extension (instance data)
1589  * Return Value:        None
1590  */
1591 static void mgsl_isr_null( struct mgsl_struct *info )
1592 {
1593
1594 }       /* end of mgsl_isr_null() */
1595
1596 /* mgsl_isr_receive_dma()
1597  * 
1598  *      Service a receive DMA channel interrupt.
1599  *      For this driver there are two sources of receive DMA interrupts
1600  *      as identified in the Receive DMA mode Register (RDMR):
1601  * 
1602  *      BIT3    EOA/EOL         End of List, all receive buffers in receive
1603  *                              buffer list have been filled (no more free buffers
1604  *                              available). The DMA controller has shut down.
1605  * 
1606  *      BIT2    EOB             End of Buffer. This interrupt occurs when a receive
1607  *                              DMA buffer is terminated in response to completion
1608  *                              of a good frame or a frame with errors. The status
1609  *                              of the frame is stored in the buffer entry in the
1610  *                              list of receive buffer entries.
1611  * 
1612  * Arguments:           info            pointer to device instance data
1613  * Return Value:        None
1614  */
1615 static void mgsl_isr_receive_dma( struct mgsl_struct *info )
1616 {
1617         u16 status;
1618         
1619         /* clear interrupt pending and IUS bit for Rx DMA IRQ */
1620         usc_OutDmaReg( info, CDIR, BIT9+BIT1 );
1621
1622         /* Read the receive DMA status to identify interrupt type. */
1623         /* This also clears the status bits. */
1624         status = usc_InDmaReg( info, RDMR );
1625
1626         if ( debug_level >= DEBUG_LEVEL_ISR )   
1627                 printk("%s(%d):mgsl_isr_receive_dma(%s) status=%04X\n",
1628                         __FILE__,__LINE__,info->device_name,status);
1629                         
1630         info->pending_bh |= BH_RECEIVE;
1631         
1632         if ( status & BIT3 ) {
1633                 info->rx_overflow = 1;
1634                 info->icount.buf_overrun++;
1635         }
1636
1637 }       /* end of mgsl_isr_receive_dma() */
1638
1639 /* mgsl_isr_transmit_dma()
1640  *
1641  *      This function services a transmit DMA channel interrupt.
1642  *
1643  *      For this driver there is one source of transmit DMA interrupts
1644  *      as identified in the Transmit DMA Mode Register (TDMR):
1645  *
1646  *      BIT2  EOB       End of Buffer. This interrupt occurs when a
1647  *                      transmit DMA buffer has been emptied.
1648  *
1649  *      The driver maintains enough transmit DMA buffers to hold at least
1650  *      one max frame size transmit frame. When operating in a buffered
1651  *      transmit mode, there may be enough transmit DMA buffers to hold at
1652  *      least two or more max frame size frames. On an EOB condition,
1653  *      determine if there are any queued transmit buffers and copy into
1654  *      transmit DMA buffers if we have room.
1655  *
1656  * Arguments:           info            pointer to device instance data
1657  * Return Value:        None
1658  */
1659 static void mgsl_isr_transmit_dma( struct mgsl_struct *info )
1660 {
1661         u16 status;
1662
1663         /* clear interrupt pending and IUS bit for Tx DMA IRQ */
1664         usc_OutDmaReg(info, CDIR, BIT8+BIT0 );
1665
1666         /* Read the transmit DMA status to identify interrupt type. */
1667         /* This also clears the status bits. */
1668
1669         status = usc_InDmaReg( info, TDMR );
1670
1671         if ( debug_level >= DEBUG_LEVEL_ISR )
1672                 printk("%s(%d):mgsl_isr_transmit_dma(%s) status=%04X\n",
1673                         __FILE__,__LINE__,info->device_name,status);
1674
1675         if ( status & BIT2 ) {
1676                 --info->tx_dma_buffers_used;
1677
1678                 /* if there are transmit frames queued,
1679                  *  try to load the next one
1680                  */
1681                 if ( load_next_tx_holding_buffer(info) ) {
1682                         /* if call returns non-zero value, we have
1683                          * at least one free tx holding buffer
1684                          */
1685                         info->pending_bh |= BH_TRANSMIT;
1686                 }
1687         }
1688
1689 }       /* end of mgsl_isr_transmit_dma() */
1690
1691 /* mgsl_interrupt()
1692  * 
1693  *      Interrupt service routine entry point.
1694  *      
1695  * Arguments:
1696  * 
1697  *      irq             interrupt number that caused interrupt
1698  *      dev_id          device ID supplied during interrupt registration
1699  *      regs            interrupted processor context
1700  *      
1701  * Return Value: None
1702  */
1703 static irqreturn_t mgsl_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1704 {
1705         struct mgsl_struct * info;
1706         u16 UscVector;
1707         u16 DmaVector;
1708
1709         if ( debug_level >= DEBUG_LEVEL_ISR )   
1710                 printk("%s(%d):mgsl_interrupt(%d)entry.\n",
1711                         __FILE__,__LINE__,irq);
1712
1713         info = (struct mgsl_struct *)dev_id;    
1714         if (!info)
1715                 return IRQ_NONE;
1716                 
1717         spin_lock(&info->irq_spinlock);
1718
1719         for(;;) {
1720                 /* Read the interrupt vectors from hardware. */
1721                 UscVector = usc_InReg(info, IVR) >> 9;
1722                 DmaVector = usc_InDmaReg(info, DIVR);
1723                 
1724                 if ( debug_level >= DEBUG_LEVEL_ISR )   
1725                         printk("%s(%d):%s UscVector=%08X DmaVector=%08X\n",
1726                                 __FILE__,__LINE__,info->device_name,UscVector,DmaVector);
1727                         
1728                 if ( !UscVector && !DmaVector )
1729                         break;
1730                         
1731                 /* Dispatch interrupt vector */
1732                 if ( UscVector )
1733                         (*UscIsrTable[UscVector])(info);
1734                 else if ( (DmaVector&(BIT10|BIT9)) == BIT10)
1735                         mgsl_isr_transmit_dma(info);
1736                 else
1737                         mgsl_isr_receive_dma(info);
1738
1739                 if ( info->isr_overflow ) {
1740                         printk(KERN_ERR"%s(%d):%s isr overflow irq=%d\n",
1741                                 __FILE__,__LINE__,info->device_name, irq);
1742                         usc_DisableMasterIrqBit(info);
1743                         usc_DisableDmaInterrupts(info,DICR_MASTER);
1744                         break;
1745                 }
1746         }
1747         
1748         /* Request bottom half processing if there's something 
1749          * for it to do and the bh is not already running
1750          */
1751
1752         if ( info->pending_bh && !info->bh_running && !info->bh_requested ) {
1753                 if ( debug_level >= DEBUG_LEVEL_ISR )   
1754                         printk("%s(%d):%s queueing bh task.\n",
1755                                 __FILE__,__LINE__,info->device_name);
1756                 schedule_work(&info->task);
1757                 info->bh_requested = 1;
1758         }
1759
1760         spin_unlock(&info->irq_spinlock);
1761         
1762         if ( debug_level >= DEBUG_LEVEL_ISR )   
1763                 printk("%s(%d):mgsl_interrupt(%d)exit.\n",
1764                         __FILE__,__LINE__,irq);
1765         return IRQ_HANDLED;
1766 }       /* end of mgsl_interrupt() */
1767
1768 /* startup()
1769  * 
1770  *      Initialize and start device.
1771  *      
1772  * Arguments:           info    pointer to device instance data
1773  * Return Value:        0 if success, otherwise error code
1774  */
1775 static int startup(struct mgsl_struct * info)
1776 {
1777         int retval = 0;
1778         
1779         if ( debug_level >= DEBUG_LEVEL_INFO )
1780                 printk("%s(%d):mgsl_startup(%s)\n",__FILE__,__LINE__,info->device_name);
1781                 
1782         if (info->flags & ASYNC_INITIALIZED)
1783                 return 0;
1784         
1785         if (!info->xmit_buf) {
1786                 /* allocate a page of memory for a transmit buffer */
1787                 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1788                 if (!info->xmit_buf) {
1789                         printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1790                                 __FILE__,__LINE__,info->device_name);
1791                         return -ENOMEM;
1792                 }
1793         }
1794
1795         info->pending_bh = 0;
1796         
1797         memset(&info->icount, 0, sizeof(info->icount));
1798
1799         init_timer(&info->tx_timer);
1800         info->tx_timer.data = (unsigned long)info;
1801         info->tx_timer.function = mgsl_tx_timeout;
1802         
1803         /* Allocate and claim adapter resources */
1804         retval = mgsl_claim_resources(info);
1805         
1806         /* perform existence check and diagnostics */
1807         if ( !retval )
1808                 retval = mgsl_adapter_test(info);
1809                 
1810         if ( retval ) {
1811                 if (capable(CAP_SYS_ADMIN) && info->tty)
1812                         set_bit(TTY_IO_ERROR, &info->tty->flags);
1813                 mgsl_release_resources(info);
1814                 return retval;
1815         }
1816
1817         /* program hardware for current parameters */
1818         mgsl_change_params(info);
1819         
1820         if (info->tty)
1821                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1822
1823         info->flags |= ASYNC_INITIALIZED;
1824         
1825         return 0;
1826         
1827 }       /* end of startup() */
1828
1829 /* shutdown()
1830  *
1831  * Called by mgsl_close() and mgsl_hangup() to shutdown hardware
1832  *
1833  * Arguments:           info    pointer to device instance data
1834  * Return Value:        None
1835  */
1836 static void shutdown(struct mgsl_struct * info)
1837 {
1838         unsigned long flags;
1839         
1840         if (!(info->flags & ASYNC_INITIALIZED))
1841                 return;
1842
1843         if (debug_level >= DEBUG_LEVEL_INFO)
1844                 printk("%s(%d):mgsl_shutdown(%s)\n",
1845                          __FILE__,__LINE__, info->device_name );
1846
1847         /* clear status wait queue because status changes */
1848         /* can't happen after shutting down the hardware */
1849         wake_up_interruptible(&info->status_event_wait_q);
1850         wake_up_interruptible(&info->event_wait_q);
1851
1852         del_timer(&info->tx_timer);     
1853
1854         if (info->xmit_buf) {
1855                 free_page((unsigned long) info->xmit_buf);
1856                 info->xmit_buf = NULL;
1857         }
1858
1859         spin_lock_irqsave(&info->irq_spinlock,flags);
1860         usc_DisableMasterIrqBit(info);
1861         usc_stop_receiver(info);
1862         usc_stop_transmitter(info);
1863         usc_DisableInterrupts(info,RECEIVE_DATA + RECEIVE_STATUS +
1864                 TRANSMIT_DATA + TRANSMIT_STATUS + IO_PIN + MISC );
1865         usc_DisableDmaInterrupts(info,DICR_MASTER + DICR_TRANSMIT + DICR_RECEIVE);
1866         
1867         /* Disable DMAEN (Port 7, Bit 14) */
1868         /* This disconnects the DMA request signal from the ISA bus */
1869         /* on the ISA adapter. This has no effect for the PCI adapter */
1870         usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) | BIT14));
1871         
1872         /* Disable INTEN (Port 6, Bit12) */
1873         /* This disconnects the IRQ request signal to the ISA bus */
1874         /* on the ISA adapter. This has no effect for the PCI adapter */
1875         usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) | BIT12));
1876         
1877         if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1878                 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1879                 usc_set_serial_signals(info);
1880         }
1881         
1882         spin_unlock_irqrestore(&info->irq_spinlock,flags);
1883
1884         mgsl_release_resources(info);   
1885         
1886         if (info->tty)
1887                 set_bit(TTY_IO_ERROR, &info->tty->flags);
1888
1889         info->flags &= ~ASYNC_INITIALIZED;
1890         
1891 }       /* end of shutdown() */
1892
1893 static void mgsl_program_hw(struct mgsl_struct *info)
1894 {
1895         unsigned long flags;
1896
1897         spin_lock_irqsave(&info->irq_spinlock,flags);
1898         
1899         usc_stop_receiver(info);
1900         usc_stop_transmitter(info);
1901         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1902         
1903         if (info->params.mode == MGSL_MODE_HDLC ||
1904             info->params.mode == MGSL_MODE_RAW ||
1905             info->netcount)
1906                 usc_set_sync_mode(info);
1907         else
1908                 usc_set_async_mode(info);
1909                 
1910         usc_set_serial_signals(info);
1911         
1912         info->dcd_chkcount = 0;
1913         info->cts_chkcount = 0;
1914         info->ri_chkcount = 0;
1915         info->dsr_chkcount = 0;
1916
1917         usc_EnableStatusIrqs(info,SICR_CTS+SICR_DSR+SICR_DCD+SICR_RI);          
1918         usc_EnableInterrupts(info, IO_PIN);
1919         usc_get_serial_signals(info);
1920                 
1921         if (info->netcount || info->tty->termios->c_cflag & CREAD)
1922                 usc_start_receiver(info);
1923                 
1924         spin_unlock_irqrestore(&info->irq_spinlock,flags);
1925 }
1926
1927 /* Reconfigure adapter based on new parameters
1928  */
1929 static void mgsl_change_params(struct mgsl_struct *info)
1930 {
1931         unsigned cflag;
1932         int bits_per_char;
1933
1934         if (!info->tty || !info->tty->termios)
1935                 return;
1936                 
1937         if (debug_level >= DEBUG_LEVEL_INFO)
1938                 printk("%s(%d):mgsl_change_params(%s)\n",
1939                          __FILE__,__LINE__, info->device_name );
1940                          
1941         cflag = info->tty->termios->c_cflag;
1942
1943         /* if B0 rate (hangup) specified then negate DTR and RTS */
1944         /* otherwise assert DTR and RTS */
1945         if (cflag & CBAUD)
1946                 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1947         else
1948                 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1949         
1950         /* byte size and parity */
1951         
1952         switch (cflag & CSIZE) {
1953               case CS5: info->params.data_bits = 5; break;
1954               case CS6: info->params.data_bits = 6; break;
1955               case CS7: info->params.data_bits = 7; break;
1956               case CS8: info->params.data_bits = 8; break;
1957               /* Never happens, but GCC is too dumb to figure it out */
1958               default:  info->params.data_bits = 7; break;
1959               }
1960               
1961         if (cflag & CSTOPB)
1962                 info->params.stop_bits = 2;
1963         else
1964                 info->params.stop_bits = 1;
1965
1966         info->params.parity = ASYNC_PARITY_NONE;
1967         if (cflag & PARENB) {
1968                 if (cflag & PARODD)
1969                         info->params.parity = ASYNC_PARITY_ODD;
1970                 else
1971                         info->params.parity = ASYNC_PARITY_EVEN;
1972 #ifdef CMSPAR
1973                 if (cflag & CMSPAR)
1974                         info->params.parity = ASYNC_PARITY_SPACE;
1975 #endif
1976         }
1977
1978         /* calculate number of jiffies to transmit a full
1979          * FIFO (32 bytes) at specified data rate
1980          */
1981         bits_per_char = info->params.data_bits + 
1982                         info->params.stop_bits + 1;
1983
1984         /* if port data rate is set to 460800 or less then
1985          * allow tty settings to override, otherwise keep the
1986          * current data rate.
1987          */
1988         if (info->params.data_rate <= 460800)
1989                 info->params.data_rate = tty_get_baud_rate(info->tty);
1990         
1991         if ( info->params.data_rate ) {
1992                 info->timeout = (32*HZ*bits_per_char) / 
1993                                 info->params.data_rate;
1994         }
1995         info->timeout += HZ/50;         /* Add .02 seconds of slop */
1996
1997         if (cflag & CRTSCTS)
1998                 info->flags |= ASYNC_CTS_FLOW;
1999         else
2000                 info->flags &= ~ASYNC_CTS_FLOW;
2001                 
2002         if (cflag & CLOCAL)
2003                 info->flags &= ~ASYNC_CHECK_CD;
2004         else
2005                 info->flags |= ASYNC_CHECK_CD;
2006
2007         /* process tty input control flags */
2008         
2009         info->read_status_mask = RXSTATUS_OVERRUN;
2010         if (I_INPCK(info->tty))
2011                 info->read_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
2012         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2013                 info->read_status_mask |= RXSTATUS_BREAK_RECEIVED;
2014         
2015         if (I_IGNPAR(info->tty))
2016                 info->ignore_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
2017         if (I_IGNBRK(info->tty)) {
2018                 info->ignore_status_mask |= RXSTATUS_BREAK_RECEIVED;
2019                 /* If ignoring parity and break indicators, ignore 
2020                  * overruns too.  (For real raw support).
2021                  */
2022                 if (I_IGNPAR(info->tty))
2023                         info->ignore_status_mask |= RXSTATUS_OVERRUN;
2024         }
2025
2026         mgsl_program_hw(info);
2027
2028 }       /* end of mgsl_change_params() */
2029
2030 /* mgsl_put_char()
2031  * 
2032  *      Add a character to the transmit buffer.
2033  *      
2034  * Arguments:           tty     pointer to tty information structure
2035  *                      ch      character to add to transmit buffer
2036  *              
2037  * Return Value:        None
2038  */
2039 static void mgsl_put_char(struct tty_struct *tty, unsigned char ch)
2040 {
2041         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2042         unsigned long flags;
2043
2044         if ( debug_level >= DEBUG_LEVEL_INFO ) {
2045                 printk( "%s(%d):mgsl_put_char(%d) on %s\n",
2046                         __FILE__,__LINE__,ch,info->device_name);
2047         }               
2048         
2049         if (mgsl_paranoia_check(info, tty->name, "mgsl_put_char"))
2050                 return;
2051
2052         if (!tty || !info->xmit_buf)
2053                 return;
2054
2055         spin_lock_irqsave(&info->irq_spinlock,flags);
2056         
2057         if ( (info->params.mode == MGSL_MODE_ASYNC ) || !info->tx_active ) {
2058         
2059                 if (info->xmit_cnt < SERIAL_XMIT_SIZE - 1) {
2060                         info->xmit_buf[info->xmit_head++] = ch;
2061                         info->xmit_head &= SERIAL_XMIT_SIZE-1;
2062                         info->xmit_cnt++;
2063                 }
2064         }
2065         
2066         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2067         
2068 }       /* end of mgsl_put_char() */
2069
2070 /* mgsl_flush_chars()
2071  * 
2072  *      Enable transmitter so remaining characters in the
2073  *      transmit buffer are sent.
2074  *      
2075  * Arguments:           tty     pointer to tty information structure
2076  * Return Value:        None
2077  */
2078 static void mgsl_flush_chars(struct tty_struct *tty)
2079 {
2080         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2081         unsigned long flags;
2082                                 
2083         if ( debug_level >= DEBUG_LEVEL_INFO )
2084                 printk( "%s(%d):mgsl_flush_chars() entry on %s xmit_cnt=%d\n",
2085                         __FILE__,__LINE__,info->device_name,info->xmit_cnt);
2086         
2087         if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_chars"))
2088                 return;
2089
2090         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
2091             !info->xmit_buf)
2092                 return;
2093
2094         if ( debug_level >= DEBUG_LEVEL_INFO )
2095                 printk( "%s(%d):mgsl_flush_chars() entry on %s starting transmitter\n",
2096                         __FILE__,__LINE__,info->device_name );
2097
2098         spin_lock_irqsave(&info->irq_spinlock,flags);
2099         
2100         if (!info->tx_active) {
2101                 if ( (info->params.mode == MGSL_MODE_HDLC ||
2102                         info->params.mode == MGSL_MODE_RAW) && info->xmit_cnt ) {
2103                         /* operating in synchronous (frame oriented) mode */
2104                         /* copy data from circular xmit_buf to */
2105                         /* transmit DMA buffer. */
2106                         mgsl_load_tx_dma_buffer(info,
2107                                  info->xmit_buf,info->xmit_cnt);
2108                 }
2109                 usc_start_transmitter(info);
2110         }
2111         
2112         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2113         
2114 }       /* end of mgsl_flush_chars() */
2115
2116 /* mgsl_write()
2117  * 
2118  *      Send a block of data
2119  *      
2120  * Arguments:
2121  * 
2122  *      tty             pointer to tty information structure
2123  *      buf             pointer to buffer containing send data
2124  *      count           size of send data in bytes
2125  *      
2126  * Return Value:        number of characters written
2127  */
2128 static int mgsl_write(struct tty_struct * tty,
2129                     const unsigned char *buf, int count)
2130 {
2131         int     c, ret = 0;
2132         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2133         unsigned long flags;
2134         
2135         if ( debug_level >= DEBUG_LEVEL_INFO )
2136                 printk( "%s(%d):mgsl_write(%s) count=%d\n",
2137                         __FILE__,__LINE__,info->device_name,count);
2138         
2139         if (mgsl_paranoia_check(info, tty->name, "mgsl_write"))
2140                 goto cleanup;
2141
2142         if (!tty || !info->xmit_buf)
2143                 goto cleanup;
2144
2145         if ( info->params.mode == MGSL_MODE_HDLC ||
2146                         info->params.mode == MGSL_MODE_RAW ) {
2147                 /* operating in synchronous (frame oriented) mode */
2148                 /* operating in synchronous (frame oriented) mode */
2149                 if (info->tx_active) {
2150
2151                         if ( info->params.mode == MGSL_MODE_HDLC ) {
2152                                 ret = 0;
2153                                 goto cleanup;
2154                         }
2155                         /* transmitter is actively sending data -
2156                          * if we have multiple transmit dma and
2157                          * holding buffers, attempt to queue this
2158                          * frame for transmission at a later time.
2159                          */
2160                         if (info->tx_holding_count >= info->num_tx_holding_buffers ) {
2161                                 /* no tx holding buffers available */
2162                                 ret = 0;
2163                                 goto cleanup;
2164                         }
2165
2166                         /* queue transmit frame request */
2167                         ret = count;
2168                         save_tx_buffer_request(info,buf,count);
2169
2170                         /* if we have sufficient tx dma buffers,
2171                          * load the next buffered tx request
2172                          */
2173                         spin_lock_irqsave(&info->irq_spinlock,flags);
2174                         load_next_tx_holding_buffer(info);
2175                         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2176                         goto cleanup;
2177                 }
2178         
2179                 /* if operating in HDLC LoopMode and the adapter  */
2180                 /* has yet to be inserted into the loop, we can't */
2181                 /* transmit                                       */
2182
2183                 if ( (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) &&
2184                         !usc_loopmode_active(info) )
2185                 {
2186                         ret = 0;
2187                         goto cleanup;
2188                 }
2189
2190                 if ( info->xmit_cnt ) {
2191                         /* Send accumulated from send_char() calls */
2192                         /* as frame and wait before accepting more data. */
2193                         ret = 0;
2194                         
2195                         /* copy data from circular xmit_buf to */
2196                         /* transmit DMA buffer. */
2197                         mgsl_load_tx_dma_buffer(info,
2198                                 info->xmit_buf,info->xmit_cnt);
2199                         if ( debug_level >= DEBUG_LEVEL_INFO )
2200                                 printk( "%s(%d):mgsl_write(%s) sync xmit_cnt flushing\n",
2201                                         __FILE__,__LINE__,info->device_name);
2202                 } else {
2203                         if ( debug_level >= DEBUG_LEVEL_INFO )
2204                                 printk( "%s(%d):mgsl_write(%s) sync transmit accepted\n",
2205                                         __FILE__,__LINE__,info->device_name);
2206                         ret = count;
2207                         info->xmit_cnt = count;
2208                         mgsl_load_tx_dma_buffer(info,buf,count);
2209                 }
2210         } else {
2211                 while (1) {
2212                         spin_lock_irqsave(&info->irq_spinlock,flags);
2213                         c = min_t(int, count,
2214                                 min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
2215                                     SERIAL_XMIT_SIZE - info->xmit_head));
2216                         if (c <= 0) {
2217                                 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2218                                 break;
2219                         }
2220                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
2221                         info->xmit_head = ((info->xmit_head + c) &
2222                                            (SERIAL_XMIT_SIZE-1));
2223                         info->xmit_cnt += c;
2224                         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2225                         buf += c;
2226                         count -= c;
2227                         ret += c;
2228                 }
2229         }       
2230         
2231         if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
2232                 spin_lock_irqsave(&info->irq_spinlock,flags);
2233                 if (!info->tx_active)
2234                         usc_start_transmitter(info);
2235                 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2236         }
2237 cleanup:        
2238         if ( debug_level >= DEBUG_LEVEL_INFO )
2239                 printk( "%s(%d):mgsl_write(%s) returning=%d\n",
2240                         __FILE__,__LINE__,info->device_name,ret);
2241                         
2242         return ret;
2243         
2244 }       /* end of mgsl_write() */
2245
2246 /* mgsl_write_room()
2247  *
2248  *      Return the count of free bytes in transmit buffer
2249  *      
2250  * Arguments:           tty     pointer to tty info structure
2251  * Return Value:        None
2252  */
2253 static int mgsl_write_room(struct tty_struct *tty)
2254 {
2255         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2256         int     ret;
2257                                 
2258         if (mgsl_paranoia_check(info, tty->name, "mgsl_write_room"))
2259                 return 0;
2260         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
2261         if (ret < 0)
2262                 ret = 0;
2263                 
2264         if (debug_level >= DEBUG_LEVEL_INFO)
2265                 printk("%s(%d):mgsl_write_room(%s)=%d\n",
2266                          __FILE__,__LINE__, info->device_name,ret );
2267                          
2268         if ( info->params.mode == MGSL_MODE_HDLC ||
2269                 info->params.mode == MGSL_MODE_RAW ) {
2270                 /* operating in synchronous (frame oriented) mode */
2271                 if ( info->tx_active )
2272                         return 0;
2273                 else
2274                         return HDLC_MAX_FRAME_SIZE;
2275         }
2276         
2277         return ret;
2278         
2279 }       /* end of mgsl_write_room() */
2280
2281 /* mgsl_chars_in_buffer()
2282  *
2283  *      Return the count of bytes in transmit buffer
2284  *      
2285  * Arguments:           tty     pointer to tty info structure
2286  * Return Value:        None
2287  */
2288 static int mgsl_chars_in_buffer(struct tty_struct *tty)
2289 {
2290         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2291                          
2292         if (debug_level >= DEBUG_LEVEL_INFO)
2293                 printk("%s(%d):mgsl_chars_in_buffer(%s)\n",
2294                          __FILE__,__LINE__, info->device_name );
2295                          
2296         if (mgsl_paranoia_check(info, tty->name, "mgsl_chars_in_buffer"))
2297                 return 0;
2298                 
2299         if (debug_level >= DEBUG_LEVEL_INFO)
2300                 printk("%s(%d):mgsl_chars_in_buffer(%s)=%d\n",
2301                          __FILE__,__LINE__, info->device_name,info->xmit_cnt );
2302                          
2303         if ( info->params.mode == MGSL_MODE_HDLC ||
2304                 info->params.mode == MGSL_MODE_RAW ) {
2305                 /* operating in synchronous (frame oriented) mode */
2306                 if ( info->tx_active )
2307                         return info->max_frame_size;
2308                 else
2309                         return 0;
2310         }
2311                          
2312         return info->xmit_cnt;
2313 }       /* end of mgsl_chars_in_buffer() */
2314
2315 /* mgsl_flush_buffer()
2316  *
2317  *      Discard all data in the send buffer
2318  *      
2319  * Arguments:           tty     pointer to tty info structure
2320  * Return Value:        None
2321  */
2322 static void mgsl_flush_buffer(struct tty_struct *tty)
2323 {
2324         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2325         unsigned long flags;
2326         
2327         if (debug_level >= DEBUG_LEVEL_INFO)
2328                 printk("%s(%d):mgsl_flush_buffer(%s) entry\n",
2329                          __FILE__,__LINE__, info->device_name );
2330         
2331         if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_buffer"))
2332                 return;
2333                 
2334         spin_lock_irqsave(&info->irq_spinlock,flags); 
2335         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2336         del_timer(&info->tx_timer);     
2337         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2338         
2339         wake_up_interruptible(&tty->write_wait);
2340         tty_wakeup(tty);
2341 }
2342
2343 /* mgsl_send_xchar()
2344  *
2345  *      Send a high-priority XON/XOFF character
2346  *      
2347  * Arguments:           tty     pointer to tty info structure
2348  *                      ch      character to send
2349  * Return Value:        None
2350  */
2351 static void mgsl_send_xchar(struct tty_struct *tty, char ch)
2352 {
2353         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2354         unsigned long flags;
2355
2356         if (debug_level >= DEBUG_LEVEL_INFO)
2357                 printk("%s(%d):mgsl_send_xchar(%s,%d)\n",
2358                          __FILE__,__LINE__, info->device_name, ch );
2359                          
2360         if (mgsl_paranoia_check(info, tty->name, "mgsl_send_xchar"))
2361                 return;
2362
2363         info->x_char = ch;
2364         if (ch) {
2365                 /* Make sure transmit interrupts are on */
2366                 spin_lock_irqsave(&info->irq_spinlock,flags);
2367                 if (!info->tx_enabled)
2368                         usc_start_transmitter(info);
2369                 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2370         }
2371 }       /* end of mgsl_send_xchar() */
2372
2373 /* mgsl_throttle()
2374  * 
2375  *      Signal remote device to throttle send data (our receive data)
2376  *      
2377  * Arguments:           tty     pointer to tty info structure
2378  * Return Value:        None
2379  */
2380 static void mgsl_throttle(struct tty_struct * tty)
2381 {
2382         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2383         unsigned long flags;
2384         
2385         if (debug_level >= DEBUG_LEVEL_INFO)
2386                 printk("%s(%d):mgsl_throttle(%s) entry\n",
2387                          __FILE__,__LINE__, info->device_name );
2388
2389         if (mgsl_paranoia_check(info, tty->name, "mgsl_throttle"))
2390                 return;
2391         
2392         if (I_IXOFF(tty))
2393                 mgsl_send_xchar(tty, STOP_CHAR(tty));
2394  
2395         if (tty->termios->c_cflag & CRTSCTS) {
2396                 spin_lock_irqsave(&info->irq_spinlock,flags);
2397                 info->serial_signals &= ~SerialSignal_RTS;
2398                 usc_set_serial_signals(info);
2399                 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2400         }
2401 }       /* end of mgsl_throttle() */
2402
2403 /* mgsl_unthrottle()
2404  * 
2405  *      Signal remote device to stop throttling send data (our receive data)
2406  *      
2407  * Arguments:           tty     pointer to tty info structure
2408  * Return Value:        None
2409  */
2410 static void mgsl_unthrottle(struct tty_struct * tty)
2411 {
2412         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2413         unsigned long flags;
2414         
2415         if (debug_level >= DEBUG_LEVEL_INFO)
2416                 printk("%s(%d):mgsl_unthrottle(%s) entry\n",
2417                          __FILE__,__LINE__, info->device_name );
2418
2419         if (mgsl_paranoia_check(info, tty->name, "mgsl_unthrottle"))
2420                 return;
2421         
2422         if (I_IXOFF(tty)) {
2423                 if (info->x_char)
2424                         info->x_char = 0;
2425                 else
2426                         mgsl_send_xchar(tty, START_CHAR(tty));
2427         }
2428         
2429         if (tty->termios->c_cflag & CRTSCTS) {
2430                 spin_lock_irqsave(&info->irq_spinlock,flags);
2431                 info->serial_signals |= SerialSignal_RTS;
2432                 usc_set_serial_signals(info);
2433                 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2434         }
2435         
2436 }       /* end of mgsl_unthrottle() */
2437
2438 /* mgsl_get_stats()
2439  * 
2440  *      get the current serial parameters information
2441  *
2442  * Arguments:   info            pointer to device instance data
2443  *              user_icount     pointer to buffer to hold returned stats
2444  *      
2445  * Return Value:        0 if success, otherwise error code
2446  */
2447 static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount __user *user_icount)
2448 {
2449         int err;
2450         
2451         if (debug_level >= DEBUG_LEVEL_INFO)
2452                 printk("%s(%d):mgsl_get_params(%s)\n",
2453                          __FILE__,__LINE__, info->device_name);
2454                         
2455         if (!user_icount) {
2456                 memset(&info->icount, 0, sizeof(info->icount));
2457         } else {
2458                 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
2459                 if (err)
2460                         return -EFAULT;
2461         }
2462         
2463         return 0;
2464         
2465 }       /* end of mgsl_get_stats() */
2466
2467 /* mgsl_get_params()
2468  * 
2469  *      get the current serial parameters information
2470  *
2471  * Arguments:   info            pointer to device instance data
2472  *              user_params     pointer to buffer to hold returned params
2473  *      
2474  * Return Value:        0 if success, otherwise error code
2475  */
2476 static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_params)
2477 {
2478         int err;
2479         if (debug_level >= DEBUG_LEVEL_INFO)
2480                 printk("%s(%d):mgsl_get_params(%s)\n",
2481                          __FILE__,__LINE__, info->device_name);
2482                         
2483         COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
2484         if (err) {
2485                 if ( debug_level >= DEBUG_LEVEL_INFO )
2486                         printk( "%s(%d):mgsl_get_params(%s) user buffer copy failed\n",
2487                                 __FILE__,__LINE__,info->device_name);
2488                 return -EFAULT;
2489         }
2490         
2491         return 0;
2492         
2493 }       /* end of mgsl_get_params() */
2494
2495 /* mgsl_set_params()
2496  * 
2497  *      set the serial parameters
2498  *      
2499  * Arguments:
2500  * 
2501  *      info            pointer to device instance data
2502  *      new_params      user buffer containing new serial params
2503  *
2504  * Return Value:        0 if success, otherwise error code
2505  */
2506 static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_params)
2507 {
2508         unsigned long flags;
2509         MGSL_PARAMS tmp_params;
2510         int err;
2511  
2512         if (debug_level >= DEBUG_LEVEL_INFO)
2513                 printk("%s(%d):mgsl_set_params %s\n", __FILE__,__LINE__,
2514                         info->device_name );
2515         COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2516         if (err) {
2517                 if ( debug_level >= DEBUG_LEVEL_INFO )
2518                         printk( "%s(%d):mgsl_set_params(%s) user buffer copy failed\n",
2519                                 __FILE__,__LINE__,info->device_name);
2520                 return -EFAULT;
2521         }
2522         
2523         spin_lock_irqsave(&info->irq_spinlock,flags);
2524         memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2525         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2526         
2527         mgsl_change_params(info);
2528         
2529         return 0;
2530         
2531 }       /* end of mgsl_set_params() */
2532
2533 /* mgsl_get_txidle()
2534  * 
2535  *      get the current transmit idle mode
2536  *
2537  * Arguments:   info            pointer to device instance data
2538  *              idle_mode       pointer to buffer to hold returned idle mode
2539  *      
2540  * Return Value:        0 if success, otherwise error code
2541  */
2542 static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode)
2543 {
2544         int err;
2545         
2546         if (debug_level >= DEBUG_LEVEL_INFO)
2547                 printk("%s(%d):mgsl_get_txidle(%s)=%d\n",
2548                          __FILE__,__LINE__, info->device_name, info->idle_mode);
2549                         
2550         COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2551         if (err) {
2552                 if ( debug_level >= DEBUG_LEVEL_INFO )
2553                         printk( "%s(%d):mgsl_get_txidle(%s) user buffer copy failed\n",
2554                                 __FILE__,__LINE__,info->device_name);
2555                 return -EFAULT;
2556         }
2557         
2558         return 0;
2559         
2560 }       /* end of mgsl_get_txidle() */
2561
2562 /* mgsl_set_txidle()    service ioctl to set transmit idle mode
2563  *      
2564  * Arguments:           info            pointer to device instance data
2565  *                      idle_mode       new idle mode
2566  *
2567  * Return Value:        0 if success, otherwise error code
2568  */
2569 static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode)
2570 {
2571         unsigned long flags;
2572  
2573         if (debug_level >= DEBUG_LEVEL_INFO)
2574                 printk("%s(%d):mgsl_set_txidle(%s,%d)\n", __FILE__,__LINE__,
2575                         info->device_name, idle_mode );
2576                         
2577         spin_lock_irqsave(&info->irq_spinlock,flags);
2578         info->idle_mode = idle_mode;
2579         usc_set_txidle( info );
2580         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2581         return 0;
2582         
2583 }       /* end of mgsl_set_txidle() */
2584
2585 /* mgsl_txenable()
2586  * 
2587  *      enable or disable the transmitter
2588  *      
2589  * Arguments:
2590  * 
2591  *      info            pointer to device instance data
2592  *      enable          1 = enable, 0 = disable
2593  *
2594  * Return Value:        0 if success, otherwise error code
2595  */
2596 static int mgsl_txenable(struct mgsl_struct * info, int enable)
2597 {
2598         unsigned long flags;
2599  
2600         if (debug_level >= DEBUG_LEVEL_INFO)
2601                 printk("%s(%d):mgsl_txenable(%s,%d)\n", __FILE__,__LINE__,
2602                         info->device_name, enable);
2603                         
2604         spin_lock_irqsave(&info->irq_spinlock,flags);
2605         if ( enable ) {
2606                 if ( !info->tx_enabled ) {
2607
2608                         usc_start_transmitter(info);
2609                         /*--------------------------------------------------
2610                          * if HDLC/SDLC Loop mode, attempt to insert the
2611                          * station in the 'loop' by setting CMR:13. Upon
2612                          * receipt of the next GoAhead (RxAbort) sequence,
2613                          * the OnLoop indicator (CCSR:7) should go active
2614                          * to indicate that we are on the loop
2615                          *--------------------------------------------------*/
2616                         if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
2617                                 usc_loopmode_insert_request( info );
2618                 }
2619         } else {
2620                 if ( info->tx_enabled )
2621                         usc_stop_transmitter(info);
2622         }
2623         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2624         return 0;
2625         
2626 }       /* end of mgsl_txenable() */
2627
2628 /* mgsl_txabort()       abort send HDLC frame
2629  *      
2630  * Arguments:           info            pointer to device instance data
2631  * Return Value:        0 if success, otherwise error code
2632  */
2633 static int mgsl_txabort(struct mgsl_struct * info)
2634 {
2635         unsigned long flags;
2636  
2637         if (debug_level >= DEBUG_LEVEL_INFO)
2638                 printk("%s(%d):mgsl_txabort(%s)\n", __FILE__,__LINE__,
2639                         info->device_name);
2640                         
2641         spin_lock_irqsave(&info->irq_spinlock,flags);
2642         if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC )
2643         {
2644                 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
2645                         usc_loopmode_cancel_transmit( info );
2646                 else
2647                         usc_TCmd(info,TCmd_SendAbort);
2648         }
2649         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2650         return 0;
2651         
2652 }       /* end of mgsl_txabort() */
2653
2654 /* mgsl_rxenable()      enable or disable the receiver
2655  *      
2656  * Arguments:           info            pointer to device instance data
2657  *                      enable          1 = enable, 0 = disable
2658  * Return Value:        0 if success, otherwise error code
2659  */
2660 static int mgsl_rxenable(struct mgsl_struct * info, int enable)
2661 {
2662         unsigned long flags;
2663  
2664         if (debug_level >= DEBUG_LEVEL_INFO)
2665                 printk("%s(%d):mgsl_rxenable(%s,%d)\n", __FILE__,__LINE__,
2666                         info->device_name, enable);
2667                         
2668         spin_lock_irqsave(&info->irq_spinlock,flags);
2669         if ( enable ) {
2670                 if ( !info->rx_enabled )
2671                         usc_start_receiver(info);
2672         } else {
2673                 if ( info->rx_enabled )
2674                         usc_stop_receiver(info);
2675         }
2676         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2677         return 0;
2678         
2679 }       /* end of mgsl_rxenable() */
2680
2681 /* mgsl_wait_event()    wait for specified event to occur
2682  *      
2683  * Arguments:           info    pointer to device instance data
2684  *                      mask    pointer to bitmask of events to wait for
2685  * Return Value:        0       if successful and bit mask updated with
2686  *                              of events triggerred,
2687  *                      otherwise error code
2688  */
2689 static int mgsl_wait_event(struct mgsl_struct * info, int __user * mask_ptr)
2690 {
2691         unsigned long flags;
2692         int s;
2693         int rc=0;
2694         struct mgsl_icount cprev, cnow;
2695         int events;
2696         int mask;
2697         struct  _input_signal_events oldsigs, newsigs;
2698         DECLARE_WAITQUEUE(wait, current);
2699
2700         COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2701         if (rc) {
2702                 return  -EFAULT;
2703         }
2704                  
2705         if (debug_level >= DEBUG_LEVEL_INFO)
2706                 printk("%s(%d):mgsl_wait_event(%s,%d)\n", __FILE__,__LINE__,
2707                         info->device_name, mask);
2708
2709         spin_lock_irqsave(&info->irq_spinlock,flags);
2710
2711         /* return immediately if state matches requested events */
2712         usc_get_serial_signals(info);
2713         s = info->serial_signals;
2714         events = mask &
2715                 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2716                   ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2717                   ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2718                   ((s & SerialSignal_RI)  ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2719         if (events) {
2720                 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2721                 goto exit;
2722         }
2723
2724         /* save current irq counts */
2725         cprev = info->icount;
2726         oldsigs = info->input_signal_events;
2727         
2728         /* enable hunt and idle irqs if needed */
2729         if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2730                 u16 oldreg = usc_InReg(info,RICR);
2731                 u16 newreg = oldreg +
2732                          (mask & MgslEvent_ExitHuntMode ? RXSTATUS_EXITED_HUNT:0) +
2733                          (mask & MgslEvent_IdleReceived ? RXSTATUS_IDLE_RECEIVED:0);
2734                 if (oldreg != newreg)
2735                         usc_OutReg(info, RICR, newreg);
2736         }
2737         
2738         set_current_state(TASK_INTERRUPTIBLE);
2739         add_wait_queue(&info->event_wait_q, &wait);
2740         
2741         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2742         
2743
2744         for(;;) {
2745                 schedule();
2746                 if (signal_pending(current)) {
2747                         rc = -ERESTARTSYS;
2748                         break;
2749                 }
2750                         
2751                 /* get current irq counts */
2752                 spin_lock_irqsave(&info->irq_spinlock,flags);
2753                 cnow = info->icount;
2754                 newsigs = info->input_signal_events;
2755                 set_current_state(TASK_INTERRUPTIBLE);
2756                 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2757
2758                 /* if no change, wait aborted for some reason */
2759                 if (newsigs.dsr_up   == oldsigs.dsr_up   &&
2760                     newsigs.dsr_down == oldsigs.dsr_down &&
2761                     newsigs.dcd_up   == oldsigs.dcd_up   &&
2762                     newsigs.dcd_down == oldsigs.dcd_down &&
2763                     newsigs.cts_up   == oldsigs.cts_up   &&
2764                     newsigs.cts_down == oldsigs.cts_down &&
2765                     newsigs.ri_up    == oldsigs.ri_up    &&
2766                     newsigs.ri_down  == oldsigs.ri_down  &&
2767                     cnow.exithunt    == cprev.exithunt   &&
2768                     cnow.rxidle      == cprev.rxidle) {
2769                         rc = -EIO;
2770                         break;
2771                 }
2772
2773                 events = mask &
2774                         ( (newsigs.dsr_up   != oldsigs.dsr_up   ? MgslEvent_DsrActive:0)   +
2775                         (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2776                         (newsigs.dcd_up   != oldsigs.dcd_up   ? MgslEvent_DcdActive:0)   +
2777                         (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2778                         (newsigs.cts_up   != oldsigs.cts_up   ? MgslEvent_CtsActive:0)   +
2779                         (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2780                         (newsigs.ri_up    != oldsigs.ri_up    ? MgslEvent_RiActive:0)    +
2781                         (newsigs.ri_down  != oldsigs.ri_down  ? MgslEvent_RiInactive:0)  +
2782                         (cnow.exithunt    != cprev.exithunt   ? MgslEvent_ExitHuntMode:0) +
2783                           (cnow.rxidle      != cprev.rxidle     ? MgslEvent_IdleReceived:0) );
2784                 if (events)
2785                         break;
2786                 
2787                 cprev = cnow;
2788                 oldsigs = newsigs;
2789         }
2790         
2791         remove_wait_queue(&info->event_wait_q, &wait);
2792         set_current_state(TASK_RUNNING);
2793
2794         if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2795                 spin_lock_irqsave(&info->irq_spinlock,flags);
2796                 if (!waitqueue_active(&info->event_wait_q)) {
2797                         /* disable enable exit hunt mode/idle rcvd IRQs */
2798                         usc_OutReg(info, RICR, usc_InReg(info,RICR) &
2799                                 ~(RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED));
2800                 }
2801                 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2802         }
2803 exit:
2804         if ( rc == 0 )
2805                 PUT_USER(rc, events, mask_ptr);
2806                 
2807         return rc;
2808         
2809 }       /* end of mgsl_wait_event() */
2810
2811 static int modem_input_wait(struct mgsl_struct *info,int arg)
2812 {
2813         unsigned long flags;
2814         int rc;
2815         struct mgsl_icount cprev, cnow;
2816         DECLARE_WAITQUEUE(wait, current);
2817
2818         /* save current irq counts */
2819         spin_lock_irqsave(&info->irq_spinlock,flags);
2820         cprev = info->icount;
2821         add_wait_queue(&info->status_event_wait_q, &wait);
2822         set_current_state(TASK_INTERRUPTIBLE);
2823         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2824
2825         for(;;) {
2826                 schedule();
2827                 if (signal_pending(current)) {
2828                         rc = -ERESTARTSYS;
2829                         break;
2830                 }
2831
2832                 /* get new irq counts */
2833                 spin_lock_irqsave(&info->irq_spinlock,flags);
2834                 cnow = info->icount;
2835                 set_current_state(TASK_INTERRUPTIBLE);
2836                 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2837
2838                 /* if no change, wait aborted for some reason */
2839                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2840                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2841                         rc = -EIO;
2842                         break;
2843                 }
2844
2845                 /* check for change in caller specified modem input */
2846                 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2847                     (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2848                     (arg & TIOCM_CD  && cnow.dcd != cprev.dcd) ||
2849                     (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2850                         rc = 0;
2851                         break;
2852                 }
2853
2854                 cprev = cnow;
2855         }
2856         remove_wait_queue(&info->status_event_wait_q, &wait);
2857         set_current_state(TASK_RUNNING);
2858         return rc;
2859 }
2860
2861 /* return the state of the serial control and status signals
2862  */
2863 static int tiocmget(struct tty_struct *tty, struct file *file)
2864 {
2865         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2866         unsigned int result;
2867         unsigned long flags;
2868
2869         spin_lock_irqsave(&info->irq_spinlock,flags);
2870         usc_get_serial_signals(info);
2871         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2872
2873         result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2874                 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2875                 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2876                 ((info->serial_signals & SerialSignal_RI)  ? TIOCM_RNG:0) +
2877                 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2878                 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2879
2880         if (debug_level >= DEBUG_LEVEL_INFO)
2881                 printk("%s(%d):%s tiocmget() value=%08X\n",
2882                          __FILE__,__LINE__, info->device_name, result );
2883         return result;
2884 }
2885
2886 /* set modem control signals (DTR/RTS)
2887  */
2888 static int tiocmset(struct tty_struct *tty, struct file *file,
2889                     unsigned int set, unsigned int clear)
2890 {
2891         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2892         unsigned long flags;
2893
2894         if (debug_level >= DEBUG_LEVEL_INFO)
2895                 printk("%s(%d):%s tiocmset(%x,%x)\n",
2896                         __FILE__,__LINE__,info->device_name, set, clear);
2897
2898         if (set & TIOCM_RTS)
2899                 info->serial_signals |= SerialSignal_RTS;
2900         if (set & TIOCM_DTR)
2901                 info->serial_signals |= SerialSignal_DTR;
2902         if (clear & TIOCM_RTS)
2903                 info->serial_signals &= ~SerialSignal_RTS;
2904         if (clear & TIOCM_DTR)
2905                 info->serial_signals &= ~SerialSignal_DTR;
2906
2907         spin_lock_irqsave(&info->irq_spinlock,flags);
2908         usc_set_serial_signals(info);
2909         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2910
2911         return 0;
2912 }
2913
2914 /* mgsl_break()         Set or clear transmit break condition
2915  *
2916  * Arguments:           tty             pointer to tty instance data
2917  *                      break_state     -1=set break condition, 0=clear
2918  * Return Value:        None
2919  */
2920 static void mgsl_break(struct tty_struct *tty, int break_state)
2921 {
2922         struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
2923         unsigned long flags;
2924         
2925         if (debug_level >= DEBUG_LEVEL_INFO)
2926                 printk("%s(%d):mgsl_break(%s,%d)\n",
2927                          __FILE__,__LINE__, info->device_name, break_state);
2928                          
2929         if (mgsl_paranoia_check(info, tty->name, "mgsl_break"))
2930                 return;
2931
2932         spin_lock_irqsave(&info->irq_spinlock,flags);
2933         if (break_state == -1)
2934                 usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) | BIT7));
2935         else 
2936                 usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) & ~BIT7));
2937         spin_unlock_irqrestore(&info->irq_spinlock,flags);
2938         
2939 }       /* end of mgsl_break() */
2940
2941 /* mgsl_ioctl() Service an IOCTL request
2942  *      
2943  * Arguments:
2944  * 
2945  *      tty     pointer to tty instance data
2946  *      file    pointer to associated file object for device
2947  *      cmd     IOCTL command code
2948  *      arg     command argument/context
2949  *      
2950  * Return Value:        0 if success, otherwise error code
2951  */
2952 static int mgsl_ioctl(struct tty_struct *tty, struct file * file,
2953                     unsigned int cmd, unsigned long arg)
2954 {
2955         struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
2956         
2957         if (debug_level >= DEBUG_LEVEL_INFO)
2958                 printk("%s(%d):mgsl_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2959                         info->device_name, cmd );
2960         
2961         if (mgsl_paranoia_check(info, tty->name, "mgsl_ioctl"))
2962                 return -ENODEV;
2963
2964         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2965             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2966                 if (tty->flags & (1 << TTY_IO_ERROR))
2967                     return -EIO;
2968         }
2969
2970         return mgsl_ioctl_common(info, cmd, arg);
2971 }
2972
2973 static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg)
2974 {
2975         int error;
2976         struct mgsl_icount cnow;        /* kernel counter temps */
2977         void __user *argp = (void __user *)arg;
2978         struct serial_icounter_struct __user *p_cuser;  /* user space */
2979         unsigned long flags;
2980         
2981         switch (cmd) {
2982                 case MGSL_IOCGPARAMS:
2983                         return mgsl_get_params(info, argp);
2984                 case MGSL_IOCSPARAMS:
2985                         return mgsl_set_params(info, argp);
2986                 case MGSL_IOCGTXIDLE:
2987                         return mgsl_get_txidle(info, argp);
2988                 case MGSL_IOCSTXIDLE:
2989                         return mgsl_set_txidle(info,(int)arg);
2990                 case MGSL_IOCTXENABLE:
2991                         return mgsl_txenable(info,(int)arg);
2992                 case MGSL_IOCRXENABLE:
2993                         return mgsl_rxenable(info,(int)arg);
2994                 case MGSL_IOCTXABORT:
2995                         return mgsl_txabort(info);
2996                 case MGSL_IOCGSTATS:
2997                         return mgsl_get_stats(info, argp);
2998                 case MGSL_IOCWAITEVENT:
2999                         return mgsl_wait_event(info, argp);
3000                 case MGSL_IOCLOOPTXDONE:
3001                         return mgsl_loopmode_send_done(info);
3002                 /* Wait for modem input (DCD,RI,DSR,CTS) change
3003                  * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
3004                  */
3005                 case TIOCMIWAIT:
3006                         return modem_input_wait(info,(int)arg);
3007
3008                 /* 
3009                  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
3010                  * Return: write counters to the user passed counter struct
3011                  * NB: both 1->0 and 0->1 transitions are counted except for
3012                  *     RI where only 0->1 is counted.
3013                  */
3014                 case TIOCGICOUNT:
3015                         spin_lock_irqsave(&info->irq_spinlock,flags);
3016                         cnow = info->icount;
3017                         spin_unlock_irqrestore(&info->irq_spinlock,flags);
3018                         p_cuser = argp;
3019                         PUT_USER(error,cnow.cts, &p_cuser->cts);
3020                         if (error) return error;
3021                         PUT_USER(error,cnow.dsr, &p_cuser->dsr);
3022                         if (error) return error;
3023                         PUT_USER(error,cnow.rng, &p_cuser->rng);
3024                         if (error) return error;
3025                         PUT_USER(error,cnow.dcd, &p_cuser->dcd);
3026                         if (error) return error;
3027                         PUT_USER(error,cnow.rx, &p_cuser->rx);
3028                         if (error) return error;
3029                         PUT_USER(error,cnow.tx, &p_cuser->tx);
3030                         if (error) return error;
3031                         PUT_USER(error,cnow.frame, &p_cuser->frame);
3032                         if (error) return error;
3033                         PUT_USER(error,cnow.overrun, &p_cuser->overrun);
3034                         if (error) return error;
3035                         PUT_USER(error,cnow.parity, &p_cuser->parity);
3036                         if (error) return error;
3037                         PUT_USER(error,cnow.brk, &p_cuser->brk);
3038                         if (error) return error;
3039                         PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
3040                         if (error) return error;
3041                         return 0;
3042                 default:
3043                         return -ENOIOCTLCMD;
3044         }
3045         return 0;
3046 }
3047
3048 /* mgsl_set_termios()
3049  * 
3050  *      Set new termios settings
3051  *      
3052  * Arguments:
3053  * 
3054  *      tty             pointer to tty structure
3055  *      termios         pointer to buffer to hold returned old termios
3056  *      
3057  * Return Value:                None
3058  */
3059 static void mgsl_set_termios(struct tty_struct *tty, struct termios *old_termios)
3060 {
3061         struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
3062         unsigned long flags;
3063         
3064         if (debug_level >= DEBUG_LEVEL_INFO)
3065                 printk("%s(%d):mgsl_set_termios %s\n", __FILE__,__LINE__,
3066                         tty->driver->name );
3067         
3068         /* just return if nothing has changed */
3069         if ((tty->termios->c_cflag == old_termios->c_cflag)
3070             && (RELEVANT_IFLAG(tty->termios->c_iflag) 
3071                 == RELEVANT_IFLAG(old_termios->c_iflag)))
3072           return;
3073
3074         mgsl_change_params(info);
3075
3076         /* Handle transition to B0 status */
3077         if (old_termios->c_cflag & CBAUD &&
3078             !(tty->termios->c_cflag & CBAUD)) {
3079                 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
3080                 spin_lock_irqsave(&info->irq_spinlock,flags);
3081                 usc_set_serial_signals(info);
3082                 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3083         }
3084         
3085         /* Handle transition away from B0 status */
3086         if (!(old_termios->c_cflag & CBAUD) &&
3087             tty->termios->c_cflag & CBAUD) {
3088                 info->serial_signals |= SerialSignal_DTR;
3089                 if (!(tty->termios->c_cflag & CRTSCTS) || 
3090                     !test_bit(TTY_THROTTLED, &tty->flags)) {
3091                         info->serial_signals |= SerialSignal_RTS;
3092                 }
3093                 spin_lock_irqsave(&info->irq_spinlock,flags);
3094                 usc_set_serial_signals(info);
3095                 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3096         }
3097         
3098         /* Handle turning off CRTSCTS */
3099         if (old_termios->c_cflag & CRTSCTS &&
3100             !(tty->termios->c_cflag & CRTSCTS)) {
3101                 tty->hw_stopped = 0;
3102                 mgsl_start(tty);
3103         }
3104
3105 }       /* end of mgsl_set_termios() */
3106
3107 /* mgsl_close()
3108  * 
3109  *      Called when port is closed. Wait for remaining data to be
3110  *      sent. Disable port and free resources.
3111  *      
3112  * Arguments:
3113  * 
3114  *      tty     pointer to open tty structure
3115  *      filp    pointer to open file object
3116  *      
3117  * Return Value:        None
3118  */
3119 static void mgsl_close(struct tty_struct *tty, struct file * filp)
3120 {
3121         struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
3122
3123         if (mgsl_paranoia_check(info, tty->name, "mgsl_close"))
3124                 return;
3125         
3126         if (debug_level >= DEBUG_LEVEL_INFO)
3127                 printk("%s(%d):mgsl_close(%s) entry, count=%d\n",
3128                          __FILE__,__LINE__, info->device_name, info->count);
3129                          
3130         if (!info->count)
3131                 return;
3132
3133         if (tty_hung_up_p(filp))
3134                 goto cleanup;
3135                         
3136         if ((tty->count == 1) && (info->count != 1)) {
3137                 /*
3138                  * tty->count is 1 and the tty structure will be freed.
3139                  * info->count should be one in this case.
3140                  * if it's not, correct it so that the port is shutdown.
3141                  */
3142                 printk("mgsl_close: bad refcount; tty->count is 1, "
3143                        "info->count is %d\n", info->count);
3144                 info->count = 1;
3145         }
3146         
3147         info->count--;
3148         
3149         /* if at least one open remaining, leave hardware active */
3150         if (info->count)
3151                 goto cleanup;
3152         
3153         info->flags |= ASYNC_CLOSING;
3154         
3155         /* set tty->closing to notify line discipline to 
3156          * only process XON/XOFF characters. Only the N_TTY
3157          * discipline appears to use this (ppp does not).
3158          */
3159         tty->closing = 1;
3160         
3161         /* wait for transmit data to clear all layers */
3162         
3163         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
3164                 if (debug_level >= DEBUG_LEVEL_INFO)
3165                         printk("%s(%d):mgsl_close(%s) calling tty_wait_until_sent\n",