* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / char / synclink.c
blobd07532a177efa12c71eb9b7bee1eb80b27eb5a03
1 /*
2 * linux/drivers/char/synclink.c
4 * ==FILEDATE 19990610==
6 * Device driver for Microgate SyncLink ISA and PCI
7 * high speed multiprotocol serial adapters.
9 * written by Paul Fulghum for Microgate Corporation
10 * paulkf@microgate.com
12 * Microgate and SyncLink are trademarks of Microgate Corporation
14 * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
16 * Original release 01/11/99
18 * This code is released under the GNU General Public License (GPL)
20 * This driver is primarily intended for use in synchronous
21 * HDLC mode. Asynchronous mode is also provided.
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.
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.
33 * This driver has been tested with a slightly modified ppp.c driver
34 * for synchronous PPP.
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
40 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
49 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
50 #define BREAKPOINT() asm(" int $3");
52 #define MAX_ISA_DEVICES 10
54 #include <linux/config.h>
55 #include <linux/module.h>
56 #include <linux/version.h>
57 #include <linux/errno.h>
58 #include <linux/signal.h>
59 #include <linux/sched.h>
60 #include <linux/timer.h>
61 #include <linux/interrupt.h>
62 #include <linux/pci.h>
63 #include <linux/tty.h>
64 #include <linux/tty_flip.h>
65 #include <linux/serial.h>
66 #include <linux/major.h>
67 #include <linux/string.h>
68 #include <linux/fcntl.h>
69 #include <linux/ptrace.h>
70 #include <linux/ioport.h>
71 #include <linux/mm.h>
72 #include <linux/malloc.h>
74 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
75 #include <linux/vmalloc.h>
76 #include <linux/init.h>
77 #include <asm/serial.h>
78 #else
79 #include <linux/bios32.h>
80 #endif
82 #include <linux/delay.h>
83 #include <linux/ioctl.h>
85 #include <asm/system.h>
86 #include <asm/io.h>
87 #include <asm/irq.h>
88 #include <asm/dma.h>
89 #include <asm/bitops.h>
90 #include <asm/types.h>
91 #include <linux/termios.h>
92 #include <linux/tqueue.h>
94 #if LINUX_VERSION_CODE >= VERSION(2,1,4)
95 #include <asm/segment.h>
96 #define GET_USER(error,value,addr) error = get_user(value,addr)
97 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
98 #define PUT_USER(error,value,addr) error = put_user(value,addr)
99 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
101 #if LINUX_VERSION_CODE >= VERSION(2,1,5)
102 #include <asm/uaccess.h>
103 #endif
105 #else /* 2.0.x and 2.1.x before 2.1.4 */
107 #define GET_USER(error,value,addr) \
108 do { \
109 error = verify_area (VERIFY_READ, (void *) addr, sizeof (value)); \
110 if (error == 0) \
111 value = get_user(addr); \
112 } while (0)
114 #define COPY_FROM_USER(error,dest,src,size) \
115 do { \
116 error = verify_area (VERIFY_READ, (void *) src, size); \
117 if (error == 0) \
118 memcpy_fromfs (dest, src, size); \
119 } while (0)
121 #define PUT_USER(error,value,addr) \
122 do { \
123 error = verify_area (VERIFY_WRITE, (void *) addr, sizeof (value)); \
124 if (error == 0) \
125 put_user (value, addr); \
126 } while (0)
128 #define COPY_TO_USER(error,dest,src,size) \
129 do { \
130 error = verify_area (VERIFY_WRITE, (void *) dest, size); \
131 if (error == 0) \
132 memcpy_tofs (dest, src, size); \
133 } while (0)
135 #endif
137 #if LINUX_VERSION_CODE < VERSION(2,1,0)
139 * This is used to figure out the divisor speeds and the timeouts
141 static int baud_table[] = {
142 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
143 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };
145 #define __init
146 #define ioremap(a,b) vremap((a),(b))
147 #define iounmap(a) vfree((a))
148 #define SERIAL_TYPE_NORMAL 1
149 #define SERIAL_TYPE_CALLOUT 2
150 typedef int spinlock_t;
151 #define spin_lock_irqsave(a,b) {save_flags((b));cli();}
152 #define spin_unlock_irqrestore(a,b) {restore_flags((b));}
153 #define spin_lock(a)
154 #define spin_unlock(a)
155 #define schedule_timeout(a){current->timeout = jiffies + (a); schedule();}
156 #define signal_pending(a) ((a)->signal & ~(a)->blocked)
157 #endif
161 #include "linux/synclink.h"
163 #define RCLRVALUE 0xffff
165 MGSL_PARAMS default_params = {
166 MGSL_MODE_HDLC, /* unsigned long mode */
167 0, /* unsigned char loopback; */
168 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
169 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
170 0, /* unsigned long clock_speed; */
171 0xff, /* unsigned char addr_filter; */
172 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
173 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
174 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
175 9600, /* unsigned long data_rate; */
176 8, /* unsigned char data_bits; */
177 1, /* unsigned char stop_bits; */
178 ASYNC_PARITY_NONE /* unsigned char parity; */
181 #define SHARED_MEM_ADDRESS_SIZE 0x40000
182 #define BUFFERLISTSIZE (PAGE_SIZE)
183 #define DMABUFFERSIZE (PAGE_SIZE)
184 #define MAXRXFRAMES 7
186 typedef struct _DMABUFFERENTRY
188 u32 phys_addr; /* 32-bit flat physical address of data buffer */
189 u16 count; /* buffer size/data count */
190 u16 status; /* Control/status field */
191 u16 rcc; /* character count field */
192 u16 reserved; /* padding required by 16C32 */
193 u32 link; /* 32-bit flat link to next buffer entry */
194 char *virt_addr; /* virtual address of data buffer */
195 u32 phys_entry; /* physical address of this buffer entry */
196 } DMABUFFERENTRY, *DMAPBUFFERENTRY;
198 /* The queue of BH actions to be performed */
200 #define BH_TYPE_RECEIVE_DATA 1
201 #define BH_TYPE_RECEIVE_STATUS 2
202 #define BH_TYPE_RECEIVE_DMA 3
203 #define BH_TYPE_TRANSMIT_DATA 4
204 #define BH_TYPE_TRANSMIT_STATUS 5
205 #define BH_TYPE_STATUS 6
207 typedef struct _BH_EVENT {
208 unsigned char type; /* Set by interrupt routines to reqst */
209 u16 status;
210 struct _BH_EVENT *link;
212 } BH_EVENT, *BH_QUEUE; /* Queue of BH actions to be done. */
214 #define MAX_BH_QUEUE_ENTRIES 200
215 #define IO_PIN_SHUTDOWN_LIMIT (MAX_BH_QUEUE_ENTRIES/4)
217 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
219 struct _input_signal_events {
220 int ri_up;
221 int ri_down;
222 int dsr_up;
223 int dsr_down;
224 int dcd_up;
225 int dcd_down;
226 int cts_up;
227 int cts_down;
231 * Device instance data structure
234 struct mgsl_struct {
235 int magic;
236 int flags;
237 int count; /* count of opens */
238 int line;
239 unsigned short close_delay;
240 unsigned short closing_wait; /* time to wait before closing */
242 struct mgsl_icount icount;
244 struct termios normal_termios;
245 struct termios callout_termios;
247 struct tty_struct *tty;
248 int timeout;
249 int x_char; /* xon/xoff character */
250 int blocked_open; /* # of blocked opens */
251 long session; /* Session of opening process */
252 long pgrp; /* pgrp of opening process */
253 u16 read_status_mask;
254 u16 ignore_status_mask;
255 unsigned char *xmit_buf;
256 int xmit_head;
257 int xmit_tail;
258 int xmit_cnt;
260 wait_queue_head_t open_wait;
261 wait_queue_head_t close_wait;
263 wait_queue_head_t status_event_wait_q;
264 wait_queue_head_t event_wait_q;
265 struct timer_list tx_timer; /* HDLC transmit timeout timer */
266 struct mgsl_struct *next_device; /* device list link */
268 spinlock_t irq_spinlock; /* spinlock for synchronizing with ISR */
269 struct tq_struct task; /* task structure for scheduling bh */
271 u32 EventMask; /* event trigger mask */
272 u32 RecordedEvents; /* pending events */
274 u32 max_frame_size; /* as set by device config */
276 BH_EVENT bh_queue[MAX_BH_QUEUE_ENTRIES]; /* Pointer to alloc'ed block */
277 BH_QUEUE bh_queue_head; /* Queue of BH actions */
278 BH_QUEUE bh_queue_tail; /* Tail of above for perf. */
279 BH_QUEUE free_bh_queue_head; /* Queue of Free BH */
280 BH_QUEUE free_bh_queue_tail; /* Tail of above for perf. */
281 BH_QUEUE bh_action; /* Action for BH */
282 int bh_running; /* Protection from multiple */
283 int isr_overflow;
284 int bh_requested;
286 int dcd_chkcount; /* check counts to prevent */
287 int cts_chkcount; /* too many IRQs if a signal */
288 int dsr_chkcount; /* is floating */
289 int ri_chkcount;
291 char *buffer_list; /* virtual address of Rx & Tx buffer lists */
292 unsigned long buffer_list_phys;
294 unsigned int rx_buffer_count; /* count of total allocated Rx buffers */
295 DMABUFFERENTRY *rx_buffer_list; /* list of receive buffer entries */
296 unsigned int current_rx_buffer;
298 unsigned int tx_buffer_count; /* count of total allocated Tx buffers */
299 DMABUFFERENTRY *tx_buffer_list; /* list of transmit buffer entries */
301 int rx_enabled;
302 int rx_overflow;
304 int tx_enabled;
305 int tx_active;
306 u32 idle_mode;
308 u16 cmr_value;
310 char device_name[25]; /* device instance name */
312 unsigned int bus_type; /* expansion bus type (ISA,EISA,PCI) */
313 unsigned char bus; /* expansion bus number (zero based) */
314 unsigned char function; /* PCI device number */
316 unsigned int io_base; /* base I/O address of adapter */
317 unsigned int io_addr_size; /* size of the I/O address range */
318 int io_addr_requested; /* nonzero if I/O address requested */
320 unsigned int irq_level; /* interrupt level */
321 unsigned long irq_flags;
322 int irq_requested; /* nonzero if IRQ requested */
324 unsigned int dma_level; /* DMA channel */
325 int dma_requested; /* nonzero if dma channel requested */
327 u16 mbre_bit;
328 u16 loopback_bits;
329 u16 usc_idle_mode;
331 MGSL_PARAMS params; /* communications parameters */
333 unsigned char serial_signals; /* current serial signal states */
335 int irq_occurred; /* for diagnostics use */
336 unsigned int init_error; /* Initialization startup error (DIAGS) */
337 int fDiagnosticsmode; /* Driver in Diagnostic mode? (DIAGS) */
339 u32 last_mem_alloc;
340 unsigned char* memory_base; /* shared memory address (PCI only) */
341 u32 phys_memory_base;
343 unsigned char* lcr_base; /* local config registers (PCI only) */
344 u32 phys_lcr_base;
345 u32 lcr_offset;
347 u32 misc_ctrl_value;
348 char flag_buf[HDLC_MAX_FRAME_SIZE];
349 char char_buf[HDLC_MAX_FRAME_SIZE];
350 BOOLEAN drop_rts_on_tx_done;
352 BOOLEAN loopmode_insert_requested;
353 BOOLEAN loopmode_send_done_requested;
355 struct _input_signal_events input_signal_events;
358 #define MGSL_MAGIC 0x5401
361 * The size of the serial xmit buffer is 1 page, or 4096 bytes
363 #define SERIAL_XMIT_SIZE 4096
367 * These macros define the offsets used in calculating the
368 * I/O address of the specified USC registers.
372 #define DCPIN 2 /* Bit 1 of I/O address */
373 #define SDPIN 4 /* Bit 2 of I/O address */
375 #define DCAR 0 /* DMA command/address register */
376 #define CCAR SDPIN /* channel command/address register */
377 #define DATAREG DCPIN + SDPIN /* serial data register */
378 #define MSBONLY 0x41
379 #define LSBONLY 0x40
382 * These macros define the register address (ordinal number)
383 * used for writing address/value pairs to the USC.
386 #define CMR 0x02 /* Channel mode Register */
387 #define CCSR 0x04 /* Channel Command/status Register */
388 #define CCR 0x06 /* Channel Control Register */
389 #define PSR 0x08 /* Port status Register */
390 #define PCR 0x0a /* Port Control Register */
391 #define TMDR 0x0c /* Test mode Data Register */
392 #define TMCR 0x0e /* Test mode Control Register */
393 #define CMCR 0x10 /* Clock mode Control Register */
394 #define HCR 0x12 /* Hardware Configuration Register */
395 #define IVR 0x14 /* Interrupt Vector Register */
396 #define IOCR 0x16 /* Input/Output Control Register */
397 #define ICR 0x18 /* Interrupt Control Register */
398 #define DCCR 0x1a /* Daisy Chain Control Register */
399 #define MISR 0x1c /* Misc Interrupt status Register */
400 #define SICR 0x1e /* status Interrupt Control Register */
401 #define RDR 0x20 /* Receive Data Register */
402 #define RMR 0x22 /* Receive mode Register */
403 #define RCSR 0x24 /* Receive Command/status Register */
404 #define RICR 0x26 /* Receive Interrupt Control Register */
405 #define RSR 0x28 /* Receive Sync Register */
406 #define RCLR 0x2a /* Receive count Limit Register */
407 #define RCCR 0x2c /* Receive Character count Register */
408 #define TC0R 0x2e /* Time Constant 0 Register */
409 #define TDR 0x30 /* Transmit Data Register */
410 #define TMR 0x32 /* Transmit mode Register */
411 #define TCSR 0x34 /* Transmit Command/status Register */
412 #define TICR 0x36 /* Transmit Interrupt Control Register */
413 #define TSR 0x38 /* Transmit Sync Register */
414 #define TCLR 0x3a /* Transmit count Limit Register */
415 #define TCCR 0x3c /* Transmit Character count Register */
416 #define TC1R 0x3e /* Time Constant 1 Register */
420 * MACRO DEFINITIONS FOR DMA REGISTERS
423 #define DCR 0x06 /* DMA Control Register (shared) */
424 #define DACR 0x08 /* DMA Array count Register (shared) */
425 #define BDCR 0x12 /* Burst/Dwell Control Register (shared) */
426 #define DIVR 0x14 /* DMA Interrupt Vector Register (shared) */
427 #define DICR 0x18 /* DMA Interrupt Control Register (shared) */
428 #define CDIR 0x1a /* Clear DMA Interrupt Register (shared) */
429 #define SDIR 0x1c /* Set DMA Interrupt Register (shared) */
431 #define TDMR 0x02 /* Transmit DMA mode Register */
432 #define TDIAR 0x1e /* Transmit DMA Interrupt Arm Register */
433 #define TBCR 0x2a /* Transmit Byte count Register */
434 #define TARL 0x2c /* Transmit Address Register (low) */
435 #define TARU 0x2e /* Transmit Address Register (high) */
436 #define NTBCR 0x3a /* Next Transmit Byte count Register */
437 #define NTARL 0x3c /* Next Transmit Address Register (low) */
438 #define NTARU 0x3e /* Next Transmit Address Register (high) */
440 #define RDMR 0x82 /* Receive DMA mode Register (non-shared) */
441 #define RDIAR 0x9e /* Receive DMA Interrupt Arm Register */
442 #define RBCR 0xaa /* Receive Byte count Register */
443 #define RARL 0xac /* Receive Address Register (low) */
444 #define RARU 0xae /* Receive Address Register (high) */
445 #define NRBCR 0xba /* Next Receive Byte count Register */
446 #define NRARL 0xbc /* Next Receive Address Register (low) */
447 #define NRARU 0xbe /* Next Receive Address Register (high) */
451 * MACRO DEFINITIONS FOR MODEM STATUS BITS
454 #define MODEMSTATUS_DTR 0x80
455 #define MODEMSTATUS_DSR 0x40
456 #define MODEMSTATUS_RTS 0x20
457 #define MODEMSTATUS_CTS 0x10
458 #define MODEMSTATUS_RI 0x04
459 #define MODEMSTATUS_DCD 0x01
463 * Channel Command/Address Register (CCAR) Command Codes
466 #define RTCmd_Null 0x0000
467 #define RTCmd_ResetHighestIus 0x1000
468 #define RTCmd_TriggerChannelLoadDma 0x2000
469 #define RTCmd_TriggerRxDma 0x2800
470 #define RTCmd_TriggerTxDma 0x3000
471 #define RTCmd_TriggerRxAndTxDma 0x3800
472 #define RTCmd_PurgeRxFifo 0x4800
473 #define RTCmd_PurgeTxFifo 0x5000
474 #define RTCmd_PurgeRxAndTxFifo 0x5800
475 #define RTCmd_LoadRcc 0x6800
476 #define RTCmd_LoadTcc 0x7000
477 #define RTCmd_LoadRccAndTcc 0x7800
478 #define RTCmd_LoadTC0 0x8800
479 #define RTCmd_LoadTC1 0x9000
480 #define RTCmd_LoadTC0AndTC1 0x9800
481 #define RTCmd_SerialDataLSBFirst 0xa000
482 #define RTCmd_SerialDataMSBFirst 0xa800
483 #define RTCmd_SelectBigEndian 0xb000
484 #define RTCmd_SelectLittleEndian 0xb800
488 * DMA Command/Address Register (DCAR) Command Codes
491 #define DmaCmd_Null 0x0000
492 #define DmaCmd_ResetTxChannel 0x1000
493 #define DmaCmd_ResetRxChannel 0x1200
494 #define DmaCmd_StartTxChannel 0x2000
495 #define DmaCmd_StartRxChannel 0x2200
496 #define DmaCmd_ContinueTxChannel 0x3000
497 #define DmaCmd_ContinueRxChannel 0x3200
498 #define DmaCmd_PauseTxChannel 0x4000
499 #define DmaCmd_PauseRxChannel 0x4200
500 #define DmaCmd_AbortTxChannel 0x5000
501 #define DmaCmd_AbortRxChannel 0x5200
502 #define DmaCmd_InitTxChannel 0x7000
503 #define DmaCmd_InitRxChannel 0x7200
504 #define DmaCmd_ResetHighestDmaIus 0x8000
505 #define DmaCmd_ResetAllChannels 0x9000
506 #define DmaCmd_StartAllChannels 0xa000
507 #define DmaCmd_ContinueAllChannels 0xb000
508 #define DmaCmd_PauseAllChannels 0xc000
509 #define DmaCmd_AbortAllChannels 0xd000
510 #define DmaCmd_InitAllChannels 0xf000
512 #define TCmd_Null 0x0000
513 #define TCmd_ClearTxCRC 0x2000
514 #define TCmd_SelectTicrTtsaData 0x4000
515 #define TCmd_SelectTicrTxFifostatus 0x5000
516 #define TCmd_SelectTicrIntLevel 0x6000
517 #define TCmd_SelectTicrdma_level 0x7000
518 #define TCmd_SendFrame 0x8000
519 #define TCmd_SendAbort 0x9000
520 #define TCmd_EnableDleInsertion 0xc000
521 #define TCmd_DisableDleInsertion 0xd000
522 #define TCmd_ClearEofEom 0xe000
523 #define TCmd_SetEofEom 0xf000
525 #define RCmd_Null 0x0000
526 #define RCmd_ClearRxCRC 0x2000
527 #define RCmd_EnterHuntmode 0x3000
528 #define RCmd_SelectRicrRtsaData 0x4000
529 #define RCmd_SelectRicrRxFifostatus 0x5000
530 #define RCmd_SelectRicrIntLevel 0x6000
531 #define RCmd_SelectRicrdma_level 0x7000
534 * Bits for enabling and disabling IRQs in Interrupt Control Register (ICR)
537 #define RECEIVE_STATUS BIT5
538 #define RECEIVE_DATA BIT4
539 #define TRANSMIT_STATUS BIT3
540 #define TRANSMIT_DATA BIT2
541 #define IO_PIN BIT1
542 #define MISC BIT0
546 * Receive status Bits in Receive Command/status Register RCSR
549 #define RXSTATUS_SHORT_FRAME BIT8
550 #define RXSTATUS_CODE_VIOLATION BIT8
551 #define RXSTATUS_EXITED_HUNT BIT7
552 #define RXSTATUS_IDLE_RECEIVED BIT6
553 #define RXSTATUS_BREAK_RECEIVED BIT5
554 #define RXSTATUS_ABORT_RECEIVED BIT5
555 #define RXSTATUS_RXBOUND BIT4
556 #define RXSTATUS_CRC_ERROR BIT3
557 #define RXSTATUS_FRAMING_ERROR BIT3
558 #define RXSTATUS_ABORT BIT2
559 #define RXSTATUS_PARITY_ERROR BIT2
560 #define RXSTATUS_OVERRUN BIT1
561 #define RXSTATUS_DATA_AVAILABLE BIT0
562 #define RXSTATUS_ALL 0x01f6
563 #define usc_UnlatchRxstatusBits(a,b) usc_OutReg( (a), RCSR, (u16)((b) & RXSTATUS_ALL) )
566 * Values for setting transmit idle mode in
567 * Transmit Control/status Register (TCSR)
569 #define IDLEMODE_FLAGS 0x0000
570 #define IDLEMODE_ALT_ONE_ZERO 0x0100
571 #define IDLEMODE_ZERO 0x0200
572 #define IDLEMODE_ONE 0x0300
573 #define IDLEMODE_ALT_MARK_SPACE 0x0500
574 #define IDLEMODE_SPACE 0x0600
575 #define IDLEMODE_MARK 0x0700
578 * Transmit status Bits in Transmit Command/status Register (TCSR)
581 #define TCSR_PRESERVE 0x0700
583 #define TXSTATUS_PREAMBLE_SENT BIT7
584 #define TXSTATUS_IDLE_SENT BIT6
585 #define TXSTATUS_ABORT_SENT BIT5
586 #define TXSTATUS_EOF_SENT BIT4
587 #define TXSTATUS_EOM_SENT BIT4
588 #define TXSTATUS_CRC_SENT BIT3
589 #define TXSTATUS_ALL_SENT BIT2
590 #define TXSTATUS_UNDERRUN BIT1
591 #define TXSTATUS_FIFO_EMPTY BIT0
592 #define TXSTATUS_ALL 0x00fa
593 #define usc_UnlatchTxstatusBits(a,b) usc_OutReg( (a), TCSR, (u16)((a)->usc_idle_mode + ((b) & 0x00FF)) )
596 #define MISCSTATUS_RXC_LATCHED BIT15
597 #define MISCSTATUS_RXC BIT14
598 #define MISCSTATUS_TXC_LATCHED BIT13
599 #define MISCSTATUS_TXC BIT12
600 #define MISCSTATUS_RI_LATCHED BIT11
601 #define MISCSTATUS_RI BIT10
602 #define MISCSTATUS_DSR_LATCHED BIT9
603 #define MISCSTATUS_DSR BIT8
604 #define MISCSTATUS_DCD_LATCHED BIT7
605 #define MISCSTATUS_DCD BIT6
606 #define MISCSTATUS_CTS_LATCHED BIT5
607 #define MISCSTATUS_CTS BIT4
608 #define MISCSTATUS_RCC_UNDERRUN BIT3
609 #define MISCSTATUS_DPLL_NO_SYNC BIT2
610 #define MISCSTATUS_BRG1_ZERO BIT1
611 #define MISCSTATUS_BRG0_ZERO BIT0
613 #define usc_UnlatchIostatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0xaaa0))
614 #define usc_UnlatchMiscstatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0x000f))
616 #define SICR_RXC_ACTIVE BIT15
617 #define SICR_RXC_INACTIVE BIT14
618 #define SICR_RXC (BIT15+BIT14)
619 #define SICR_TXC_ACTIVE BIT13
620 #define SICR_TXC_INACTIVE BIT12
621 #define SICR_TXC (BIT13+BIT12)
622 #define SICR_RI_ACTIVE BIT11
623 #define SICR_RI_INACTIVE BIT10
624 #define SICR_RI (BIT11+BIT10)
625 #define SICR_DSR_ACTIVE BIT9
626 #define SICR_DSR_INACTIVE BIT8
627 #define SICR_DSR (BIT9+BIT8)
628 #define SICR_DCD_ACTIVE BIT7
629 #define SICR_DCD_INACTIVE BIT6
630 #define SICR_DCD (BIT7+BIT6)
631 #define SICR_CTS_ACTIVE BIT5
632 #define SICR_CTS_INACTIVE BIT4
633 #define SICR_CTS (BIT5+BIT4)
634 #define SICR_RCC_UNDERFLOW BIT3
635 #define SICR_DPLL_NO_SYNC BIT2
636 #define SICR_BRG1_ZERO BIT1
637 #define SICR_BRG0_ZERO BIT0
639 void usc_DisableMasterIrqBit( struct mgsl_struct *info );
640 void usc_EnableMasterIrqBit( struct mgsl_struct *info );
641 void usc_EnableInterrupts( struct mgsl_struct *info, u16 IrqMask );
642 void usc_DisableInterrupts( struct mgsl_struct *info, u16 IrqMask );
643 void usc_ClearIrqPendingBits( struct mgsl_struct *info, u16 IrqMask );
645 #define usc_EnableInterrupts( a, b ) \
646 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0xc0 + (b)) )
648 #define usc_DisableInterrupts( a, b ) \
649 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0x80 + (b)) )
651 #define usc_EnableMasterIrqBit(a) \
652 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0x0f00) + 0xb000) )
654 #define usc_DisableMasterIrqBit(a) \
655 usc_OutReg( (a), ICR, (u16)(usc_InReg((a),ICR) & 0x7f00) )
657 #define usc_ClearIrqPendingBits( a, b ) usc_OutReg( (a), DCCR, 0x40 + (b) )
660 * Transmit status Bits in Transmit Control status Register (TCSR)
661 * and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0)
664 #define TXSTATUS_PREAMBLE_SENT BIT7
665 #define TXSTATUS_IDLE_SENT BIT6
666 #define TXSTATUS_ABORT_SENT BIT5
667 #define TXSTATUS_EOF BIT4
668 #define TXSTATUS_CRC_SENT BIT3
669 #define TXSTATUS_ALL_SENT BIT2
670 #define TXSTATUS_UNDERRUN BIT1
671 #define TXSTATUS_FIFO_EMPTY BIT0
673 #define DICR_MASTER BIT15
674 #define DICR_TRANSMIT BIT0
675 #define DICR_RECEIVE BIT1
677 #define usc_EnableDmaInterrupts(a,b) \
678 usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) | (b)) )
680 #define usc_DisableDmaInterrupts(a,b) \
681 usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) & ~(b)) )
683 #define usc_EnableStatusIrqs(a,b) \
684 usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) | (b)) )
686 #define usc_DisablestatusIrqs(a,b) \
687 usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) & ~(b)) )
689 /* Transmit status Bits in Transmit Control status Register (TCSR) */
690 /* and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0) */
693 #define DISABLE_UNCONDITIONAL 0
694 #define DISABLE_END_OF_FRAME 1
695 #define ENABLE_UNCONDITIONAL 2
696 #define ENABLE_AUTO_CTS 3
697 #define ENABLE_AUTO_DCD 3
698 #define usc_EnableTransmitter(a,b) \
699 usc_OutReg( (a), TMR, (u16)((usc_InReg((a),TMR) & 0xfffc) | (b)) )
700 #define usc_EnableReceiver(a,b) \
701 usc_OutReg( (a), RMR, (u16)((usc_InReg((a),RMR) & 0xfffc) | (b)) )
703 u16 usc_InDmaReg( struct mgsl_struct *info, u16 Port );
704 void usc_OutDmaReg( struct mgsl_struct *info, u16 Port, u16 Value );
705 void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd );
707 u16 usc_InReg( struct mgsl_struct *info, u16 Port );
708 void usc_OutReg( struct mgsl_struct *info, u16 Port, u16 Value );
709 void usc_RTCmd( struct mgsl_struct *info, u16 Cmd );
710 void usc_RCmd( struct mgsl_struct *info, u16 Cmd );
711 void usc_TCmd( struct mgsl_struct *info, u16 Cmd );
713 #define usc_TCmd(a,b) usc_OutReg((a), TCSR, (u16)((a)->usc_idle_mode + (b)))
714 #define usc_RCmd(a,b) usc_OutReg((a), RCSR, (b))
716 void usc_start_receiver( struct mgsl_struct *info );
717 void usc_stop_receiver( struct mgsl_struct *info );
719 void usc_start_transmitter( struct mgsl_struct *info );
720 void usc_stop_transmitter( struct mgsl_struct *info );
721 void usc_set_txidle( struct mgsl_struct *info );
722 void usc_load_txfifo( struct mgsl_struct *info );
724 void usc_enable_aux_clock( struct mgsl_struct *info, u32 DataRate );
725 void usc_enable_loopback( struct mgsl_struct *info, int enable );
727 void usc_get_serial_signals( struct mgsl_struct *info );
728 void usc_set_serial_signals( struct mgsl_struct *info );
730 void usc_reset( struct mgsl_struct *info );
732 void usc_set_sync_mode( struct mgsl_struct *info );
733 void usc_set_sdlc_mode( struct mgsl_struct *info );
734 void usc_set_async_mode( struct mgsl_struct *info );
735 void usc_enable_async_clock( struct mgsl_struct *info, u32 DataRate );
737 void usc_loopback_frame( struct mgsl_struct *info );
739 void mgsl_tx_timeout(unsigned long context);
742 void usc_loopmode_cancel_transmit( struct mgsl_struct * info );
743 void usc_loopmode_insert_request( struct mgsl_struct * info );
744 int usc_loopmode_active( struct mgsl_struct * info);
745 void usc_loopmode_send_done( struct mgsl_struct * info );
746 int usc_loopmode_send_active( struct mgsl_struct * info );
749 * Defines a BUS descriptor value for the PCI adapter
750 * local bus address ranges.
753 #define BUS_DESCRIPTOR( WrHold, WrDly, RdDly, Nwdd, Nwad, Nxda, Nrdd, Nrad ) \
754 (0x00400020 + \
755 ((WrHold) << 30) + \
756 ((WrDly) << 28) + \
757 ((RdDly) << 26) + \
758 ((Nwdd) << 20) + \
759 ((Nwad) << 15) + \
760 ((Nxda) << 13) + \
761 ((Nrdd) << 11) + \
762 ((Nrad) << 6) )
764 void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit);
767 * Adapter diagnostic routines
769 BOOLEAN mgsl_register_test( struct mgsl_struct *info );
770 BOOLEAN mgsl_irq_test( struct mgsl_struct *info );
771 BOOLEAN mgsl_dma_test( struct mgsl_struct *info );
772 BOOLEAN mgsl_memory_test( struct mgsl_struct *info );
773 int mgsl_adapter_test( struct mgsl_struct *info );
776 * device and resource management routines
778 int mgsl_claim_resources(struct mgsl_struct *info);
779 void mgsl_release_resources(struct mgsl_struct *info);
780 void mgsl_add_device(struct mgsl_struct *info);
781 struct mgsl_struct* mgsl_allocate_device(void);
782 int mgsl_enumerate_devices(void);
785 * DMA buffer manupulation functions.
787 void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex );
788 int mgsl_get_rx_frame( struct mgsl_struct *info );
789 void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info );
790 void mgsl_load_tx_dma_buffer( struct mgsl_struct *info, const char *Buffer, unsigned int BufferSize);
791 void mgsl_load_pci_memory(char* TargetPtr, const char* SourcePtr, unsigned short count);
794 * DMA and Shared Memory buffer allocation and formatting
796 int mgsl_allocate_dma_buffers(struct mgsl_struct *info);
797 void mgsl_free_dma_buffers(struct mgsl_struct *info);
798 int mgsl_alloc_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
799 void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
800 int mgsl_alloc_buffer_list_memory(struct mgsl_struct *info);
801 void mgsl_free_buffer_list_memory(struct mgsl_struct *info);
804 * Bottom half interrupt handlers
806 void mgsl_bh_handler(void* Context);
807 void mgsl_bh_receive_dma( struct mgsl_struct *info, unsigned short status );
808 void mgsl_bh_transmit_data( struct mgsl_struct *info, unsigned short Datacount );
809 void mgsl_bh_status_handler( struct mgsl_struct *info, unsigned short status );
811 void mgsl_format_bh_queue( struct mgsl_struct *info );
812 void mgsl_bh_queue_put( struct mgsl_struct *info, unsigned char type, unsigned short status );
813 int mgsl_bh_queue_get( struct mgsl_struct *info );
817 * Interrupt handler routines and dispatch table.
819 void mgsl_isr_null( struct mgsl_struct *info );
820 void mgsl_isr_transmit_data( struct mgsl_struct *info );
821 void mgsl_isr_receive_data( struct mgsl_struct *info );
822 void mgsl_isr_receive_status( struct mgsl_struct *info );
823 void mgsl_isr_transmit_status( struct mgsl_struct *info );
824 void mgsl_isr_io_pin( struct mgsl_struct *info );
825 void mgsl_isr_misc( struct mgsl_struct *info );
826 void mgsl_isr_receive_dma( struct mgsl_struct *info );
828 typedef void (*isr_dispatch_func)(struct mgsl_struct *);
830 isr_dispatch_func UscIsrTable[7] =
832 mgsl_isr_null,
833 mgsl_isr_misc,
834 mgsl_isr_io_pin,
835 mgsl_isr_transmit_data,
836 mgsl_isr_transmit_status,
837 mgsl_isr_receive_data,
838 mgsl_isr_receive_status
842 * ioctl call handlers
844 static int set_modem_info(struct mgsl_struct * info, unsigned int cmd,
845 unsigned int *value);
846 static int get_modem_info(struct mgsl_struct * info, unsigned int *value);
847 static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount
848 *user_icount);
849 static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS *user_params);
850 static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS *new_params);
851 static int mgsl_get_txidle(struct mgsl_struct * info, int*idle_mode);
852 static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode);
853 static int mgsl_txenable(struct mgsl_struct * info, int enable);
854 static int mgsl_txabort(struct mgsl_struct * info);
855 static int mgsl_rxenable(struct mgsl_struct * info, int enable);
856 static int mgsl_wait_event(struct mgsl_struct * info, int * mask);
857 static int mgsl_loopmode_send_done( struct mgsl_struct * info );
859 #define jiffies_from_ms(a) ((((a) * HZ)/1000)+1)
862 * Global linked list of SyncLink devices
864 struct mgsl_struct *mgsl_device_list = NULL;
865 int mgsl_device_count = 0;
868 * Set this param to non-zero to load eax with the
869 * .text section address and breakpoint on module load.
870 * This is useful for use with gdb and add-symbol-file command.
872 int break_on_load=0;
875 * Driver major number, defaults to zero to get auto
876 * assigned major number. May be forced as module parameter.
878 int ttymajor=0;
880 int cuamajor=0;
883 * Array of user specified options for ISA adapters.
885 static int io[MAX_ISA_DEVICES] = {0,};
886 static int irq[MAX_ISA_DEVICES] = {0,};
887 static int dma[MAX_ISA_DEVICES] = {0,};
888 static int debug_level = 0;
891 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
892 MODULE_PARM(break_on_load,"i");
893 MODULE_PARM(ttymajor,"i");
894 MODULE_PARM(cuamajor,"i");
895 MODULE_PARM(io,"1-" __MODULE_STRING(MAX_ISA_DEVICES) "i");
896 MODULE_PARM(irq,"1-" __MODULE_STRING(MAX_ISA_DEVICES) "i");
897 MODULE_PARM(dma,"1-" __MODULE_STRING(MAX_ISA_DEVICES) "i");
898 MODULE_PARM(debug_level,"i");
899 #endif
901 static char *driver_name = "SyncLink serial driver";
902 static char *driver_version = "1.7";
904 static struct tty_driver serial_driver, callout_driver;
905 static int serial_refcount;
907 /* number of characters left in xmit buffer before we ask for more */
908 #define WAKEUP_CHARS 256
911 static void mgsl_change_params(struct mgsl_struct *info);
912 static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout);
914 static struct tty_struct **serial_table = NULL;
915 static struct termios **serial_termios = NULL;
916 static struct termios **serial_termios_locked = NULL;
918 #ifndef MIN
919 #define MIN(a,b) ((a) < (b) ? (a) : (b))
920 #endif
923 * 1st function defined in .text section. Calling this function in
924 * init_module() followed by a breakpoint allows a remote debugger
925 * (gdb) to get the .text address for the add-symbol-file command.
926 * This allows remote debugging of dynamically loadable modules.
928 void* mgsl_get_text_ptr(void);
929 void* mgsl_get_text_ptr() {return mgsl_get_text_ptr;}
932 * tmp_buf is used as a temporary buffer by mgsl_write. We need to
933 * lock it in case the COPY_FROM_USER blocks while swapping in a page,
934 * and some other program tries to do a serial write at the same time.
935 * Since the lock will only come under contention when the system is
936 * swapping and available memory is low, it makes sense to share one
937 * buffer across all the serial ioports, since it significantly saves
938 * memory if large numbers of serial ports are open.
940 static unsigned char *tmp_buf;
941 static DECLARE_MUTEX(tmp_buf_sem);
943 static inline int mgsl_paranoia_check(struct mgsl_struct *info,
944 kdev_t device, const char *routine)
946 #ifdef MGSL_PARANOIA_CHECK
947 static const char *badmagic =
948 "Warning: bad magic number for mgsl struct (%s) in %s\n";
949 static const char *badinfo =
950 "Warning: null mgsl_struct for (%s) in %s\n";
952 if (!info) {
953 printk(badinfo, kdevname(device), routine);
954 return 1;
956 if (info->magic != MGSL_MAGIC) {
957 printk(badmagic, kdevname(device), routine);
958 return 1;
960 #endif
961 return 0;
964 /* mgsl_stop() throttle (stop) transmitter
966 * Arguments: tty pointer to tty info structure
967 * Return Value: None
969 static void mgsl_stop(struct tty_struct *tty)
971 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
972 unsigned long flags;
974 if (mgsl_paranoia_check(info, tty->device, "mgsl_stop"))
975 return;
977 if ( debug_level >= DEBUG_LEVEL_INFO )
978 printk("mgsl_stop(%s)\n",info->device_name);
980 spin_lock_irqsave(&info->irq_spinlock,flags);
981 if (info->tx_enabled)
982 usc_stop_transmitter(info);
983 spin_unlock_irqrestore(&info->irq_spinlock,flags);
985 } /* end of mgsl_stop() */
987 /* mgsl_start() release (start) transmitter
989 * Arguments: tty pointer to tty info structure
990 * Return Value: None
992 static void mgsl_start(struct tty_struct *tty)
994 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
995 unsigned long flags;
997 if (mgsl_paranoia_check(info, tty->device, "mgsl_start"))
998 return;
1000 if ( debug_level >= DEBUG_LEVEL_INFO )
1001 printk("mgsl_start(%s)\n",info->device_name);
1003 spin_lock_irqsave(&info->irq_spinlock,flags);
1004 if (!info->tx_enabled)
1005 usc_start_transmitter(info);
1006 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1008 } /* end of mgsl_start() */
1011 * Bottom half work queue access functions
1014 /* mgsl_format_bh_queue()
1016 * Initialize the bottom half processing queue
1018 * Arguments: info pointer to device instance data
1019 * Return Value: None
1021 void mgsl_format_bh_queue( struct mgsl_struct *info )
1023 BH_QUEUE bh_queue = info->bh_queue;
1024 int i;
1026 /* go through sequentially tacking the little bits together */
1028 for ( i=0; i < MAX_BH_QUEUE_ENTRIES; i++ ) {
1029 if ( info->free_bh_queue_tail == NULL )
1030 info->free_bh_queue_head = bh_queue;
1031 else
1032 info->free_bh_queue_tail->link = bh_queue;
1033 info->free_bh_queue_tail = bh_queue++;
1036 /* As a safety measure, mark the end of the chain with a NULL */
1037 info->free_bh_queue_tail->link = NULL;
1038 info->isr_overflow=0;
1040 } /* end of mgsl_format_bh_queue() */
1042 /* mgsl_bh_queue_put()
1044 * Add a BH event to the BH queue
1046 * Arguments: info pointer to device instance data
1047 * type BH event type
1048 * status BH event status
1050 * Return Value: None
1052 void mgsl_bh_queue_put( struct mgsl_struct *info, unsigned char type, unsigned short status )
1054 BH_EVENT *event = info->free_bh_queue_head;
1056 if ( event != NULL ) {
1057 /* remove free element from head of free list */
1058 info->free_bh_queue_head = event->link;
1059 event->link = NULL;
1061 /* file out new BH event */
1062 event->type = type;
1063 event->status = status;
1065 /* add element to tail of pending list */
1066 if ( info->bh_queue_head != NULL ){
1067 /* BH queue is not empty, add current element to tail */
1068 info->bh_queue_tail->link = event;
1069 } else {
1070 /* the BH queue is empty so this element becomes the head of queue */
1071 info->bh_queue_head = event;
1074 /* the new element becomes tail of queue */
1075 info->bh_queue_tail = event;
1076 } else {
1077 /* No more free BH action elements in queue. */
1078 /* This happens when too many interrupts are occuring */
1079 /* for the mgsl_bh_handler to process so set a flag. */
1081 info->isr_overflow = 1;
1084 } /* end of mgsl_bh_queue_put() */
1086 /* mgsl_bh_queue_get()
1088 * Free the current work item (if any) and get the
1089 * next work item from the head of the pending work item queue.
1091 * Effects:
1093 * If a BH action element is available on the BH action queue
1094 * then the head of the queue is removed and bh_action
1095 * is set to point to the removed element.
1097 * Arguments: info pointer to device instance data
1098 * Return Value: 1 if BH action removed from queue
1100 int mgsl_bh_queue_get( struct mgsl_struct *info )
1102 unsigned long flags;
1104 spin_lock_irqsave(&info->irq_spinlock,flags);
1106 if ( info->bh_action ) {
1107 /* free the current work item */
1108 if ( info->free_bh_queue_head != NULL ){
1109 /* free queue is not empty, add current element to tail */
1110 info->free_bh_queue_tail->link = info->bh_action;
1111 } else {
1112 /* free queue is empty so this element becomes the head of queue */
1113 info->free_bh_queue_head = info->bh_action;
1116 /* add element to tail of free queue */
1117 info->free_bh_queue_tail = info->bh_action;
1118 info->free_bh_queue_tail->link = NULL;
1121 /* attempt to remove element from head of queue */
1122 info->bh_action = info->bh_queue_head;
1124 if ( info->bh_action != NULL ){
1125 /* BH queue is not empty, remove element from queue head */
1126 info->bh_queue_head = info->bh_action->link;
1127 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1128 return 1;
1131 if ( info->isr_overflow ) {
1132 if (debug_level >= DEBUG_LEVEL_BH)
1133 printk("ISR overflow cleared.\n");
1134 info->isr_overflow=0;
1135 usc_EnableMasterIrqBit(info);
1136 usc_EnableDmaInterrupts(info,DICR_MASTER);
1139 /* Mark BH routine as complete */
1140 info->bh_running = 0;
1141 info->bh_requested = 0;
1143 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1145 return 0;
1147 } /* end of mgsl_bh_queue_get() */
1149 /* mgsl_bh_handler()
1151 * Perform bottom half processing of work items queued by ISR.
1153 * Arguments: Context pointer to device instance data
1154 * Return Value: None
1156 void mgsl_bh_handler(void* Context)
1158 struct mgsl_struct *info = (struct mgsl_struct*)Context;
1160 if (!info)
1161 return;
1163 if ( debug_level >= DEBUG_LEVEL_BH )
1164 printk( "%s(%d):mgsl_bh_handler(%s) entry\n",
1165 __FILE__,__LINE__,info->device_name);
1167 info->bh_running = 1;
1169 /* Attempt to clear out the BH queue */
1171 while( mgsl_bh_queue_get(info) ) {
1173 /* Process work item */
1174 if ( debug_level >= DEBUG_LEVEL_BH )
1175 printk( "%s(%d):mgsl_bh_handler() work item action=%d\n",
1176 __FILE__,__LINE__,info->bh_action->type);
1178 switch ( info->bh_action->type ) {
1180 case BH_TYPE_RECEIVE_DMA:
1181 mgsl_bh_receive_dma( info, info->bh_action->status );
1182 break;
1184 case BH_TYPE_TRANSMIT_STATUS:
1185 case BH_TYPE_TRANSMIT_DATA:
1186 mgsl_bh_transmit_data( info, info->bh_action->status );
1187 break;
1189 case BH_TYPE_STATUS:
1190 mgsl_bh_status_handler( info, info->bh_action->status );
1191 break;
1193 default:
1194 /* unknown work item ID */
1195 printk("Unknown work item ID=%08X!\n",
1196 info->bh_action->type );
1197 break;
1201 if ( debug_level >= DEBUG_LEVEL_BH )
1202 printk( "%s(%d):mgsl_bh_handler(%s) exit\n",
1203 __FILE__,__LINE__,info->device_name);
1205 } /* end of mgsl_bh_handler() */
1207 /* mgsl_bh_receive_dma()
1209 * Perform bottom half processing for a receive DMA interrupt
1210 * This occurs in HDLC mode after a DMA buffer has terminated
1211 * or the DMA buffers have been exhausted.
1213 * Arguments:
1215 * info pointer to device instance data
1216 * status status word
1218 * Return Value: None
1220 void mgsl_bh_receive_dma( struct mgsl_struct *info, unsigned short status )
1222 if ( debug_level >= DEBUG_LEVEL_BH )
1223 printk( "%s(%d):mgsl_bh_receive_dma(%s)\n",
1224 __FILE__,__LINE__,info->device_name);
1226 while( mgsl_get_rx_frame(info) );
1228 } /* end of mgsl_bh_receive_dma() */
1230 /* mgsl_bh_transmit_data()
1232 * Process a transmit data interrupt event
1233 * This occurs in asynchronous communications mode.
1235 * Arguments: info pointer to device instance data
1236 * Return Value: None
1238 void mgsl_bh_transmit_data( struct mgsl_struct *info, unsigned short Datacount )
1240 struct tty_struct *tty = info->tty;
1241 unsigned long flags;
1243 if ( debug_level >= DEBUG_LEVEL_BH )
1244 printk( "%s(%d):mgsl_bh_transmit_data() entry on %s\n",
1245 __FILE__,__LINE__,info->device_name);
1247 /* wakeup any waiting write requests */
1248 if (tty) {
1249 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1250 tty->ldisc.write_wakeup) {
1251 if ( debug_level >= DEBUG_LEVEL_BH )
1252 printk( "%s(%d):calling ldisc.write_wakeup on %s\n",
1253 __FILE__,__LINE__,info->device_name);
1254 (tty->ldisc.write_wakeup)(tty);
1256 wake_up_interruptible(&tty->write_wait);
1259 /* if transmitter idle and loopmode_send_done_requested
1260 * then start echoing RxD to TxD
1262 spin_lock_irqsave(&info->irq_spinlock,flags);
1263 if ( !info->tx_active && info->loopmode_send_done_requested )
1264 usc_loopmode_send_done( info );
1265 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1267 } /* End Of mgsl_bh_transmit_data() */
1269 /* mgsl_bh_status_handler()
1271 * Peform bottom half processing for a status interrupt
1273 * This event is generated when a I/O pin (serial signal)
1274 * has a transition. If there is a pending WaitEvent call
1275 * and the status transition is identified in the EventMast
1276 * of the pending call then complete the pending call.
1278 * Arguments:
1280 * info pointer to device instance data
1281 * status status word
1283 * Return Value: None
1285 void mgsl_bh_status_handler( struct mgsl_struct *info, unsigned short status )
1287 if ( debug_level >= DEBUG_LEVEL_BH )
1288 printk( "%s(%d):mgsl_bh_status_handler() entry on %s\n",
1289 __FILE__,__LINE__,info->device_name);
1291 if (status & MISCSTATUS_RI_LATCHED) {
1292 if (info->ri_chkcount)
1293 (info->ri_chkcount)--;
1295 if (status & MISCSTATUS_DSR_LATCHED) {
1296 if (info->dsr_chkcount)
1297 (info->dsr_chkcount)--;
1299 if (status & MISCSTATUS_DCD_LATCHED) {
1300 if (info->dcd_chkcount)
1301 (info->dcd_chkcount)--;
1303 if (status & MISCSTATUS_CTS_LATCHED) {
1304 if (info->cts_chkcount)
1305 (info->cts_chkcount)--;
1308 } /* End Of mgsl_bh_status_handler() */
1310 /* mgsl_isr_receive_status()
1312 * Service a receive status interrupt. The type of status
1313 * interrupt is indicated by the state of the RCSR.
1314 * This is only used for HDLC mode.
1316 * Arguments: info pointer to device instance data
1317 * Return Value: None
1319 void mgsl_isr_receive_status( struct mgsl_struct *info )
1321 u16 status = usc_InReg( info, RCSR );
1323 if ( debug_level >= DEBUG_LEVEL_ISR )
1324 printk("%s(%d):mgsl_isr_receive_status status=%04X\n",
1325 __FILE__,__LINE__,status);
1327 if ( (status & RXSTATUS_ABORT_RECEIVED) &&
1328 info->loopmode_insert_requested &&
1329 usc_loopmode_active(info) )
1331 ++info->icount.rxabort;
1332 info->loopmode_insert_requested = FALSE;
1334 /* clear CMR:13 to start echoing RxD to TxD */
1335 info->cmr_value &= ~BIT13;
1336 usc_OutReg(info, CMR, info->cmr_value);
1338 /* disable received abort irq (no longer required) */
1339 usc_OutReg(info, RICR,
1340 (usc_InReg(info, RICR) & ~RXSTATUS_ABORT_RECEIVED));
1343 if (status & (RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED)) {
1344 if (status & RXSTATUS_EXITED_HUNT)
1345 info->icount.exithunt++;
1346 if (status & RXSTATUS_IDLE_RECEIVED)
1347 info->icount.rxidle++;
1348 wake_up_interruptible(&info->event_wait_q);
1351 if (status & RXSTATUS_OVERRUN){
1352 /* Purge receive FIFO to allow DMA buffer completion
1353 * with overrun status stored in the receive status block.
1355 usc_RCmd( info, RCmd_EnterHuntmode );
1356 usc_RTCmd( info, RTCmd_PurgeRxFifo );
1359 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
1360 usc_UnlatchRxstatusBits( info, status );
1362 } /* end of mgsl_isr_receive_status() */
1364 /* mgsl_isr_transmit_status()
1366 * Service a transmit status interrupt
1367 * HDLC mode :end of transmit frame
1368 * Async mode:all data is sent
1369 * transmit status is indicated by bits in the TCSR.
1371 * Arguments: info pointer to device instance data
1372 * Return Value: None
1374 void mgsl_isr_transmit_status( struct mgsl_struct *info )
1376 u16 status = usc_InReg( info, TCSR );
1378 if ( debug_level >= DEBUG_LEVEL_ISR )
1379 printk("%s(%d):mgsl_isr_transmit_status status=%04X\n",
1380 __FILE__,__LINE__,status);
1382 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
1383 usc_UnlatchTxstatusBits( info, status );
1385 if ( status & TXSTATUS_EOF_SENT )
1386 info->icount.txok++;
1387 else if ( status & TXSTATUS_UNDERRUN )
1388 info->icount.txunder++;
1389 else if ( status & TXSTATUS_ABORT_SENT )
1390 info->icount.txabort++;
1391 else
1392 info->icount.txunder++;
1394 info->tx_active = 0;
1395 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1396 del_timer(&info->tx_timer);
1398 if ( info->drop_rts_on_tx_done ) {
1399 usc_get_serial_signals( info );
1400 if ( info->serial_signals & SerialSignal_RTS ) {
1401 info->serial_signals &= ~SerialSignal_RTS;
1402 usc_set_serial_signals( info );
1404 info->drop_rts_on_tx_done = 0;
1407 if (info->tty->stopped || info->tty->hw_stopped) {
1408 usc_stop_transmitter(info);
1409 return;
1412 mgsl_bh_queue_put(info, BH_TYPE_TRANSMIT_STATUS, status);
1414 } /* end of mgsl_isr_transmit_status() */
1416 /* mgsl_isr_io_pin()
1418 * Service an Input/Output pin interrupt. The type of
1419 * interrupt is indicated by bits in the MISR
1421 * Arguments: info pointer to device instance data
1422 * Return Value: None
1424 void mgsl_isr_io_pin( struct mgsl_struct *info )
1426 struct mgsl_icount *icount;
1427 u16 status = usc_InReg( info, MISR );
1429 if ( debug_level >= DEBUG_LEVEL_ISR )
1430 printk("%s(%d):mgsl_isr_io_pin status=%04X\n",
1431 __FILE__,__LINE__,status);
1433 usc_ClearIrqPendingBits( info, IO_PIN );
1434 usc_UnlatchIostatusBits( info, status );
1436 if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
1437 MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
1438 icount = &info->icount;
1439 /* update input line counters */
1440 if (status & MISCSTATUS_RI_LATCHED) {
1441 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1442 usc_DisablestatusIrqs(info,SICR_RI);
1443 icount->rng++;
1444 if ( status & MISCSTATUS_RI )
1445 info->input_signal_events.ri_up++;
1446 else
1447 info->input_signal_events.ri_down++;
1449 if (status & MISCSTATUS_DSR_LATCHED) {
1450 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1451 usc_DisablestatusIrqs(info,SICR_DSR);
1452 icount->dsr++;
1453 if ( status & MISCSTATUS_DSR )
1454 info->input_signal_events.dsr_up++;
1455 else
1456 info->input_signal_events.dsr_down++;
1458 if (status & MISCSTATUS_DCD_LATCHED) {
1459 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1460 usc_DisablestatusIrqs(info,SICR_DCD);
1461 icount->dcd++;
1462 if ( status & MISCSTATUS_DCD )
1463 info->input_signal_events.dcd_up++;
1464 else
1465 info->input_signal_events.dcd_down++;
1466 #ifdef CONFIG_HARD_PPS
1467 if ((info->flags & ASYNC_HARDPPS_CD) &&
1468 (status & MISCSTATUS_DCD_LATCHED))
1469 hardpps();
1470 #endif
1472 if (status & MISCSTATUS_CTS_LATCHED)
1474 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1475 usc_DisablestatusIrqs(info,SICR_CTS);
1476 icount->cts++;
1477 if ( status & MISCSTATUS_CTS )
1478 info->input_signal_events.cts_up++;
1479 else
1480 info->input_signal_events.cts_down++;
1482 wake_up_interruptible(&info->status_event_wait_q);
1483 wake_up_interruptible(&info->event_wait_q);
1485 if ( (info->flags & ASYNC_CHECK_CD) &&
1486 (status & MISCSTATUS_DCD_LATCHED) ) {
1487 if ( debug_level >= DEBUG_LEVEL_ISR )
1488 printk("%s CD now %s...", info->device_name,
1489 (status & MISCSTATUS_DCD) ? "on" : "off");
1490 if (status & MISCSTATUS_DCD)
1491 wake_up_interruptible(&info->open_wait);
1492 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1493 (info->flags & ASYNC_CALLOUT_NOHUP))) {
1494 if ( debug_level >= DEBUG_LEVEL_ISR )
1495 printk("doing serial hangup...");
1496 if (info->tty)
1497 tty_hangup(info->tty);
1501 if ( (info->flags & ASYNC_CTS_FLOW) &&
1502 (status & MISCSTATUS_CTS_LATCHED) ) {
1503 if (info->tty->hw_stopped) {
1504 if (status & MISCSTATUS_CTS) {
1505 if ( debug_level >= DEBUG_LEVEL_ISR )
1506 printk("CTS tx start...");
1507 info->tty->hw_stopped = 0;
1508 usc_start_transmitter(info);
1509 mgsl_bh_queue_put( info, BH_TYPE_TRANSMIT_DATA, status );
1510 return;
1512 } else {
1513 if (!(status & MISCSTATUS_CTS)) {
1514 if ( debug_level >= DEBUG_LEVEL_ISR )
1515 printk("CTS tx stop...");
1516 info->tty->hw_stopped = 1;
1517 usc_stop_transmitter(info);
1523 mgsl_bh_queue_put(info, BH_TYPE_STATUS, status);
1525 /* for diagnostics set IRQ flag */
1526 if ( status & MISCSTATUS_TXC_LATCHED ){
1527 usc_OutReg( info, SICR,
1528 (unsigned short)(usc_InReg(info,SICR) & ~(SICR_TXC_ACTIVE+SICR_TXC_INACTIVE)) );
1529 usc_UnlatchIostatusBits( info, MISCSTATUS_TXC_LATCHED );
1530 info->irq_occurred = 1;
1533 } /* end of mgsl_isr_io_pin() */
1535 /* mgsl_isr_transmit_data()
1537 * Service a transmit data interrupt (async mode only).
1539 * Arguments: info pointer to device instance data
1540 * Return Value: None
1542 void mgsl_isr_transmit_data( struct mgsl_struct *info )
1544 if ( debug_level >= DEBUG_LEVEL_ISR )
1545 printk("%s(%d):mgsl_isr_transmit_data xmit_cnt=%d\n",
1546 __FILE__,__LINE__,info->xmit_cnt);
1548 usc_ClearIrqPendingBits( info, TRANSMIT_DATA );
1550 if (info->tty->stopped || info->tty->hw_stopped) {
1551 usc_stop_transmitter(info);
1552 return;
1555 if ( info->xmit_cnt )
1556 usc_load_txfifo( info );
1557 else
1558 info->tx_active = 0;
1560 if (info->xmit_cnt < WAKEUP_CHARS)
1561 mgsl_bh_queue_put(info, BH_TYPE_TRANSMIT_DATA, (unsigned short)(info->xmit_cnt));
1563 } /* end of mgsl_isr_transmit_data() */
1565 /* mgsl_isr_receive_data()
1567 * Service a receive data interrupt. This occurs
1568 * when operating in asynchronous interrupt transfer mode.
1569 * The receive data FIFO is flushed to the receive data buffers.
1571 * Arguments: info pointer to device instance data
1572 * Return Value: None
1574 void mgsl_isr_receive_data( struct mgsl_struct *info )
1576 int Fifocount;
1577 u16 status;
1578 unsigned char DataByte;
1579 struct tty_struct *tty = info->tty;
1580 struct mgsl_icount *icount = &info->icount;
1582 if ( debug_level >= DEBUG_LEVEL_ISR )
1583 printk("%s(%d):mgsl_isr_receive_data\n",
1584 __FILE__,__LINE__);
1586 usc_ClearIrqPendingBits( info, RECEIVE_DATA );
1588 /* select FIFO status for RICR readback */
1589 usc_RCmd( info, RCmd_SelectRicrRxFifostatus );
1591 /* clear the Wordstatus bit so that status readback */
1592 /* only reflects the status of this byte */
1593 usc_OutReg( info, RICR+LSBONLY, (u16)(usc_InReg(info, RICR+LSBONLY) & ~BIT3 ));
1595 /* flush the receive FIFO */
1597 while( (Fifocount = (usc_InReg(info,RICR) >> 8)) ) {
1598 /* read one byte from RxFIFO */
1599 outw( (inw(info->io_base + CCAR) & 0x0780) | (RDR+LSBONLY),
1600 info->io_base + CCAR );
1601 DataByte = inb( info->io_base + CCAR );
1603 /* get the status of the received byte */
1604 status = usc_InReg(info, RCSR);
1605 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
1606 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) )
1607 usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
1609 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1610 continue;
1612 *tty->flip.char_buf_ptr = DataByte;
1613 icount->rx++;
1615 *tty->flip.flag_buf_ptr = 0;
1616 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
1617 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) ) {
1618 printk("rxerr=%04X\n",status);
1619 /* update error statistics */
1620 if ( status & RXSTATUS_BREAK_RECEIVED ) {
1621 status &= ~(RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR);
1622 icount->brk++;
1623 } else if (status & RXSTATUS_PARITY_ERROR)
1624 icount->parity++;
1625 else if (status & RXSTATUS_FRAMING_ERROR)
1626 icount->frame++;
1627 else if (status & RXSTATUS_OVERRUN) {
1628 /* must issue purge fifo cmd before */
1629 /* 16C32 accepts more receive chars */
1630 usc_RTCmd(info,RTCmd_PurgeRxFifo);
1631 icount->overrun++;
1634 /* discard char if tty control flags say so */
1635 if (status & info->ignore_status_mask)
1636 continue;
1638 status &= info->read_status_mask;
1640 if (status & RXSTATUS_BREAK_RECEIVED) {
1641 *tty->flip.flag_buf_ptr = TTY_BREAK;
1642 if (info->flags & ASYNC_SAK)
1643 do_SAK(tty);
1644 } else if (status & RXSTATUS_PARITY_ERROR)
1645 *tty->flip.flag_buf_ptr = TTY_PARITY;
1646 else if (status & RXSTATUS_FRAMING_ERROR)
1647 *tty->flip.flag_buf_ptr = TTY_FRAME;
1648 if (status & RXSTATUS_OVERRUN) {
1649 /* Overrun is special, since it's
1650 * reported immediately, and doesn't
1651 * affect the current character
1653 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
1654 tty->flip.count++;
1655 tty->flip.flag_buf_ptr++;
1656 tty->flip.char_buf_ptr++;
1657 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
1660 } /* end of if (error) */
1662 tty->flip.flag_buf_ptr++;
1663 tty->flip.char_buf_ptr++;
1664 tty->flip.count++;
1667 if ( debug_level >= DEBUG_LEVEL_ISR ) {
1668 printk("%s(%d):mgsl_isr_receive_data flip count=%d\n",
1669 __FILE__,__LINE__,tty->flip.count);
1670 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1671 __FILE__,__LINE__,icount->rx,icount->brk,
1672 icount->parity,icount->frame,icount->overrun);
1675 if ( tty->flip.count ) {
1676 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
1677 tty_flip_buffer_push(tty);
1678 #else
1679 queue_task(&tty->flip.tqueue, &tq_timer);
1680 #endif
1684 } /* end of mgsl_isr_receive_data() */
1686 /* mgsl_isr_misc()
1688 * Service a miscellaneos interrupt source.
1690 * Arguments: info pointer to device extension (instance data)
1691 * Return Value: None
1693 void mgsl_isr_misc( struct mgsl_struct *info )
1695 u16 status = usc_InReg( info, MISR );
1697 if ( debug_level >= DEBUG_LEVEL_ISR )
1698 printk("%s(%d):mgsl_isr_misc status=%04X\n",
1699 __FILE__,__LINE__,status);
1701 usc_ClearIrqPendingBits( info, MISC );
1702 usc_UnlatchMiscstatusBits( info, status );
1704 } /* end of mgsl_isr_misc() */
1706 /* mgsl_isr_null()
1708 * Services undefined interrupt vectors from the
1709 * USC. (hence this function SHOULD never be called)
1711 * Arguments: info pointer to device extension (instance data)
1712 * Return Value: None
1714 void mgsl_isr_null( struct mgsl_struct *info )
1717 } /* end of mgsl_isr_null() */
1719 /* mgsl_isr_receive_dma()
1721 * Service a receive DMA channel interrupt.
1722 * For this driver there are two sources of receive DMA interrupts
1723 * as identified in the Receive DMA mode Register (RDMR):
1725 * BIT3 EOA/EOL End of List, all receive buffers in receive
1726 * buffer list have been filled (no more free buffers
1727 * available). The DMA controller has shut down.
1729 * BIT2 EOB End of Buffer. This interrupt occurs when a receive
1730 * DMA buffer is terminated in response to completion
1731 * of a good frame or a frame with errors. The status
1732 * of the frame is stored in the buffer entry in the
1733 * list of receive buffer entries.
1735 * Arguments: info pointer to device instance data
1736 * Return Value: None
1738 void mgsl_isr_receive_dma( struct mgsl_struct *info )
1740 u16 status;
1742 /* clear interrupt pending and IUS bit for Rx DMA IRQ */
1743 usc_OutDmaReg( info, CDIR, BIT9+BIT1 );
1745 /* Read the receive DMA status to identify interrupt type. */
1746 /* This also clears the status bits. */
1747 status = usc_InDmaReg( info, RDMR );
1749 if ( debug_level >= DEBUG_LEVEL_ISR )
1750 printk("%s(%d):mgsl_isr_receive_dma(%s) status=%04X\n",
1751 __FILE__,__LINE__,info->device_name,status);
1753 /* Post a receive event for BH processing. */
1754 mgsl_bh_queue_put( info, BH_TYPE_RECEIVE_DMA, status );
1756 if ( status & BIT3 ) {
1757 info->rx_overflow = 1;
1758 info->icount.buf_overrun++;
1761 } /* end of mgsl_isr_receive_dma() */
1763 /* mgsl_interrupt()
1765 * Interrupt service routine entry point.
1767 * Arguments:
1769 * irq interrupt number that caused interrupt
1770 * dev_id device ID supplied during interrupt registration
1771 * regs interrupted processor context
1773 * Return Value: None
1775 static void mgsl_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1777 struct mgsl_struct * info;
1778 u16 UscVector;
1779 u16 DmaVector;
1781 if ( debug_level >= DEBUG_LEVEL_ISR )
1782 printk("%s(%d):mgsl_interrupt(%d)entry.\n",
1783 __FILE__,__LINE__,irq);
1785 info = (struct mgsl_struct *)dev_id;
1786 if (!info)
1787 return;
1789 spin_lock(&info->irq_spinlock);
1791 for(;;) {
1792 /* Read the interrupt vectors from hardware. */
1793 UscVector = usc_InReg(info, IVR) >> 9;
1794 DmaVector = usc_InDmaReg(info, DIVR);
1796 if ( debug_level >= DEBUG_LEVEL_ISR )
1797 printk("%s(%d):%s UscVector=%08X DmaVector=%08X\n",
1798 __FILE__,__LINE__,info->device_name,UscVector,DmaVector);
1800 if ( !UscVector && !DmaVector )
1801 break;
1803 /* Dispatch interrupt vector */
1804 if ( UscVector )
1805 (*UscIsrTable[UscVector])(info);
1806 else
1807 mgsl_isr_receive_dma(info);
1809 if ( info->isr_overflow ) {
1810 printk(KERN_ERR"%s(%d):%s isr overflow irq=%d\n",
1811 __FILE__,__LINE__,info->device_name, irq);
1812 usc_DisableMasterIrqBit(info);
1813 usc_DisableDmaInterrupts(info,DICR_MASTER);
1814 break;
1818 /* Request bottom half processing if there's something
1819 * for it to do and the bh is not already running
1822 if ( info->bh_queue_head && !info->bh_running && !info->bh_requested ) {
1823 if ( debug_level >= DEBUG_LEVEL_ISR )
1824 printk("%s(%d):%s queueing bh task.\n",
1825 __FILE__,__LINE__,info->device_name);
1826 queue_task(&info->task, &tq_immediate);
1827 mark_bh(IMMEDIATE_BH);
1828 info->bh_requested = 1;
1831 spin_unlock(&info->irq_spinlock);
1833 if ( debug_level >= DEBUG_LEVEL_ISR )
1834 printk("%s(%d):mgsl_interrupt(%d)exit.\n",
1835 __FILE__,__LINE__,irq);
1837 } /* end of mgsl_interrupt() */
1839 /* startup()
1841 * Initialize and start device.
1843 * Arguments: info pointer to device instance data
1844 * Return Value: 0 if success, otherwise error code
1846 static int startup(struct mgsl_struct * info)
1848 int retval = 0;
1850 if ( debug_level >= DEBUG_LEVEL_INFO )
1851 printk("%s(%d):mgsl_startup(%s)\n",__FILE__,__LINE__,info->device_name);
1853 if (info->flags & ASYNC_INITIALIZED)
1854 return 0;
1856 if (!info->xmit_buf) {
1857 /* allocate a page of memory for a transmit buffer */
1858 info->xmit_buf = (unsigned char *)get_free_page(GFP_KERNEL);
1859 if (!info->xmit_buf) {
1860 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1861 __FILE__,__LINE__,info->device_name);
1862 return -ENOMEM;
1866 mgsl_format_bh_queue(info);
1868 init_timer(&info->tx_timer);
1869 info->tx_timer.data = (unsigned long)info;
1870 info->tx_timer.function = mgsl_tx_timeout;
1872 /* Allocate and claim adapter resources */
1873 retval = mgsl_claim_resources(info);
1875 /* perform existance check and diagnostics */
1876 if ( !retval )
1877 retval = mgsl_adapter_test(info);
1879 if ( retval ) {
1880 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
1881 if (capable(CAP_SYS_ADMIN) && info->tty)
1882 #else
1883 if (suser() && info->tty)
1884 #endif
1885 set_bit(TTY_IO_ERROR, &info->tty->flags);
1886 mgsl_release_resources(info);
1887 return retval;
1890 /* program hardware for current parameters */
1891 mgsl_change_params(info);
1893 if (info->tty)
1894 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1896 info->flags |= ASYNC_INITIALIZED;
1898 return 0;
1900 } /* end of startup() */
1902 /* shutdown()
1904 * Called by mgsl_close() and mgsl_hangup() to shutdown hardware
1906 * Arguments: info pointer to device instance data
1907 * Return Value: None
1909 static void shutdown(struct mgsl_struct * info)
1911 unsigned long flags;
1913 if (!(info->flags & ASYNC_INITIALIZED))
1914 return;
1916 if (debug_level >= DEBUG_LEVEL_INFO)
1917 printk("%s(%d):mgsl_shutdown(%s)\n",
1918 __FILE__,__LINE__, info->device_name );
1920 /* clear status wait queue because status changes */
1921 /* can't happen after shutting down the hardware */
1922 wake_up_interruptible(&info->status_event_wait_q);
1923 wake_up_interruptible(&info->event_wait_q);
1925 if (info->xmit_buf) {
1926 free_page((unsigned long) info->xmit_buf);
1927 info->xmit_buf = 0;
1930 spin_lock_irqsave(&info->irq_spinlock,flags);
1931 usc_DisableMasterIrqBit(info);
1932 usc_stop_receiver(info);
1933 usc_stop_transmitter(info);
1934 usc_DisableInterrupts(info,RECEIVE_DATA + RECEIVE_STATUS +
1935 TRANSMIT_DATA + TRANSMIT_STATUS + IO_PIN + MISC );
1936 usc_DisableDmaInterrupts(info,DICR_MASTER + DICR_TRANSMIT + DICR_RECEIVE);
1938 /* Disable DMAEN (Port 7, Bit 14) */
1939 /* This disconnects the DMA request signal from the ISA bus */
1940 /* on the ISA adapter. This has no effect for the PCI adapter */
1941 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) | BIT14));
1943 /* Disable INTEN (Port 6, Bit12) */
1944 /* This disconnects the IRQ request signal to the ISA bus */
1945 /* on the ISA adapter. This has no effect for the PCI adapter */
1946 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) | BIT12));
1948 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1949 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1950 usc_set_serial_signals(info);
1953 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1955 mgsl_release_resources(info);
1957 if (info->tty)
1958 set_bit(TTY_IO_ERROR, &info->tty->flags);
1960 info->flags &= ~ASYNC_INITIALIZED;
1962 } /* end of shutdown() */
1964 /* mgsl_change_params()
1966 * Reconfigure adapter based on new parameters
1968 * Arguments: info pointer to device instance data
1969 * Return Value: None
1971 static void mgsl_change_params(struct mgsl_struct *info)
1973 unsigned cflag;
1974 unsigned long flags;
1975 int bits_per_char;
1977 if (!info->tty || !info->tty->termios)
1978 return;
1980 if (debug_level >= DEBUG_LEVEL_INFO)
1981 printk("%s(%d):mgsl_change_params(%s)\n",
1982 __FILE__,__LINE__, info->device_name );
1984 cflag = info->tty->termios->c_cflag;
1986 /* if B0 rate (hangup) specified then negate DTR and RTS */
1987 /* otherwise assert DTR and RTS */
1988 if (cflag & CBAUD)
1989 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1990 else
1991 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1993 /* byte size and parity */
1995 switch (cflag & CSIZE) {
1996 case CS5: info->params.data_bits = 5; break;
1997 case CS6: info->params.data_bits = 6; break;
1998 case CS7: info->params.data_bits = 7; break;
1999 case CS8: info->params.data_bits = 8; break;
2000 /* Never happens, but GCC is too dumb to figure it out */
2001 default: info->params.data_bits = 7; break;
2004 if (cflag & CSTOPB)
2005 info->params.stop_bits = 2;
2006 else
2007 info->params.stop_bits = 1;
2009 info->params.parity = ASYNC_PARITY_NONE;
2010 if (cflag & PARENB) {
2011 if (cflag & PARODD)
2012 info->params.parity = ASYNC_PARITY_ODD;
2013 else
2014 info->params.parity = ASYNC_PARITY_EVEN;
2015 #ifdef CMSPAR
2016 if (cflag & CMSPAR)
2017 info->params.parity = ASYNC_PARITY_SPACE;
2018 #endif
2021 /* calculate number of jiffies to transmit a full
2022 * FIFO (32 bytes) at specified data rate
2024 bits_per_char = info->params.data_bits +
2025 info->params.stop_bits + 1;
2027 /* if port data rate is set to 460800 or less then
2028 * allow tty settings to override, otherwise keep the
2029 * current data rate.
2031 if (info->params.data_rate <= 460800) {
2032 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
2033 info->params.data_rate = tty_get_baud_rate(info->tty);
2034 #else
2035 int i = cflag & CBAUD;
2036 if (i & CBAUDEX) {
2037 i &= ~CBAUDEX;
2038 if (i < 1 || i > 4)
2039 info->tty->termios->c_cflag &= ~CBAUDEX;
2040 else
2041 i += 15;
2043 info->params.data_rate = baud_table[i];
2044 #endif
2047 if ( info->params.data_rate ) {
2048 info->timeout = (32*HZ*bits_per_char) /
2049 info->params.data_rate;
2051 info->timeout += HZ/50; /* Add .02 seconds of slop */
2053 if (cflag & CRTSCTS)
2054 info->flags |= ASYNC_CTS_FLOW;
2055 else
2056 info->flags &= ~ASYNC_CTS_FLOW;
2058 if (cflag & CLOCAL)
2059 info->flags &= ~ASYNC_CHECK_CD;
2060 else
2061 info->flags |= ASYNC_CHECK_CD;
2063 /* process tty input control flags */
2065 info->read_status_mask = RXSTATUS_OVERRUN;
2066 if (I_INPCK(info->tty))
2067 info->read_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
2068 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2069 info->read_status_mask |= RXSTATUS_BREAK_RECEIVED;
2071 if (I_IGNPAR(info->tty))
2072 info->ignore_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
2073 if (I_IGNBRK(info->tty)) {
2074 info->ignore_status_mask |= RXSTATUS_BREAK_RECEIVED;
2075 /* If ignoring parity and break indicators, ignore
2076 * overruns too. (For real raw support).
2078 if (I_IGNPAR(info->tty))
2079 info->ignore_status_mask |= RXSTATUS_OVERRUN;
2082 /* reprogram the hardware */
2084 spin_lock_irqsave(&info->irq_spinlock,flags);
2086 usc_stop_receiver(info);
2087 usc_stop_transmitter(info);
2088 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2090 if ( info->params.mode == MGSL_MODE_HDLC )
2091 usc_set_sync_mode(info);
2092 else
2093 usc_set_async_mode(info);
2095 usc_set_serial_signals(info);
2097 info->dcd_chkcount = 0;
2098 info->cts_chkcount = 0;
2099 info->ri_chkcount = 0;
2100 info->dsr_chkcount = 0;
2102 /* enable modem signal IRQs and read initial signal states */
2103 usc_EnableStatusIrqs(info,SICR_CTS+SICR_DSR+SICR_DCD+SICR_RI);
2104 usc_EnableInterrupts(info, IO_PIN);
2105 usc_get_serial_signals(info);
2107 if ( cflag & CREAD )
2108 usc_start_receiver(info);
2110 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2112 } /* end of mgsl_change_params() */
2114 /* mgsl_put_char()
2116 * Add a character to the transmit buffer.
2118 * Arguments: tty pointer to tty information structure
2119 * ch character to add to transmit buffer
2121 * Return Value: None
2123 static void mgsl_put_char(struct tty_struct *tty, unsigned char ch)
2125 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2126 unsigned long flags;
2128 if ( debug_level >= DEBUG_LEVEL_INFO ) {
2129 printk( "%s(%d):mgsl_put_char(%d) on %s\n",
2130 __FILE__,__LINE__,ch,info->device_name);
2133 if (mgsl_paranoia_check(info, tty->device, "mgsl_put_char"))
2134 return;
2136 if (!tty || !info->xmit_buf)
2137 return;
2139 spin_lock_irqsave(&info->irq_spinlock,flags);
2141 if ( (info->params.mode != MGSL_MODE_HDLC) ||
2142 !info->tx_active ) {
2144 if (info->xmit_cnt < SERIAL_XMIT_SIZE - 1) {
2145 info->xmit_buf[info->xmit_head++] = ch;
2146 info->xmit_head &= SERIAL_XMIT_SIZE-1;
2147 info->xmit_cnt++;
2151 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2153 } /* end of mgsl_put_char() */
2155 /* mgsl_flush_chars()
2157 * Enable transmitter so remaining characters in the
2158 * transmit buffer are sent.
2160 * Arguments: tty pointer to tty information structure
2161 * Return Value: None
2163 static void mgsl_flush_chars(struct tty_struct *tty)
2165 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2166 unsigned long flags;
2168 if ( debug_level >= DEBUG_LEVEL_INFO )
2169 printk( "%s(%d):mgsl_flush_chars() entry on %s xmit_cnt=%d\n",
2170 __FILE__,__LINE__,info->device_name,info->xmit_cnt);
2172 if (mgsl_paranoia_check(info, tty->device, "mgsl_flush_chars"))
2173 return;
2175 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
2176 !info->xmit_buf)
2177 return;
2179 if ( debug_level >= DEBUG_LEVEL_INFO )
2180 printk( "%s(%d):mgsl_flush_chars() entry on %s starting transmitter\n",
2181 __FILE__,__LINE__,info->device_name );
2183 spin_lock_irqsave(&info->irq_spinlock,flags);
2185 if (!info->tx_active) {
2186 if ( (info->params.mode == MGSL_MODE_HDLC) &&
2187 info->xmit_cnt ) {
2188 /* operating in synchronous (frame oriented) mode */
2189 /* copy data from circular xmit_buf to */
2190 /* transmit DMA buffer. */
2191 mgsl_load_tx_dma_buffer(info,
2192 info->xmit_buf,info->xmit_cnt);
2194 usc_start_transmitter(info);
2197 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2199 } /* end of mgsl_flush_chars() */
2201 /* mgsl_write()
2203 * Send a block of data
2205 * Arguments:
2207 * tty pointer to tty information structure
2208 * from_user flag: 1 = from user process
2209 * buf pointer to buffer containing send data
2210 * count size of send data in bytes
2212 * Return Value: number of characters written
2214 static int mgsl_write(struct tty_struct * tty, int from_user,
2215 const unsigned char *buf, int count)
2217 int c, ret = 0, err;
2218 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2219 unsigned long flags;
2221 if ( debug_level >= DEBUG_LEVEL_INFO )
2222 printk( "%s(%d):mgsl_write(%s) count=%d\n",
2223 __FILE__,__LINE__,info->device_name,count);
2225 if (mgsl_paranoia_check(info, tty->device, "mgsl_write"))
2226 goto cleanup;
2228 if (!tty || !info->xmit_buf || !tmp_buf)
2229 goto cleanup;
2231 if ( info->params.mode == MGSL_MODE_HDLC ) {
2232 /* operating in synchronous (frame oriented) mode */
2234 if (info->tx_active) {
2235 ret = 0; goto cleanup;
2238 /* if operating in HDLC LoopMode and the adapter */
2239 /* has yet to be inserted into the loop, we can't */
2240 /* transmit */
2242 if ( (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) &&
2243 !usc_loopmode_active(info) )
2245 ret = 0;
2246 goto cleanup;
2249 if ( info->xmit_cnt ) {
2250 /* Send accumulated from send_char() calls */
2251 /* as frame and wait before accepting more data. */
2252 ret = 0;
2254 /* copy data from circular xmit_buf to */
2255 /* transmit DMA buffer. */
2256 mgsl_load_tx_dma_buffer(info,
2257 info->xmit_buf,info->xmit_cnt);
2258 if ( debug_level >= DEBUG_LEVEL_INFO )
2259 printk( "%s(%d):mgsl_write(%s) sync xmit_cnt flushing\n",
2260 __FILE__,__LINE__,info->device_name);
2261 } else {
2262 if ( debug_level >= DEBUG_LEVEL_INFO )
2263 printk( "%s(%d):mgsl_write(%s) sync transmit accepted\n",
2264 __FILE__,__LINE__,info->device_name);
2265 ret = count;
2266 info->xmit_cnt = count;
2267 if (from_user) {
2268 down(&tmp_buf_sem);
2269 COPY_FROM_USER(err,tmp_buf, buf, count);
2270 if (err) {
2271 if ( debug_level >= DEBUG_LEVEL_INFO )
2272 printk( "%s(%d):mgsl_write(%s) sync user buf copy failed\n",
2273 __FILE__,__LINE__,info->device_name);
2274 ret = -EFAULT;
2275 } else
2276 mgsl_load_tx_dma_buffer(info,tmp_buf,count);
2277 up(&tmp_buf_sem);
2279 else
2280 mgsl_load_tx_dma_buffer(info,buf,count);
2282 } else {
2283 if (from_user) {
2284 down(&tmp_buf_sem);
2285 while (1) {
2286 c = MIN(count,
2287 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
2288 SERIAL_XMIT_SIZE - info->xmit_head));
2289 if (c <= 0)
2290 break;
2292 COPY_FROM_USER(err,tmp_buf, buf, c);
2293 c -= err;
2294 if (!c) {
2295 if (!ret)
2296 ret = -EFAULT;
2297 break;
2299 spin_lock_irqsave(&info->irq_spinlock,flags);
2300 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
2301 SERIAL_XMIT_SIZE - info->xmit_head));
2302 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
2303 info->xmit_head = ((info->xmit_head + c) &
2304 (SERIAL_XMIT_SIZE-1));
2305 info->xmit_cnt += c;
2306 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2307 buf += c;
2308 count -= c;
2309 ret += c;
2311 up(&tmp_buf_sem);
2312 } else {
2313 while (1) {
2314 spin_lock_irqsave(&info->irq_spinlock,flags);
2315 c = MIN(count,
2316 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
2317 SERIAL_XMIT_SIZE - info->xmit_head));
2318 if (c <= 0) {
2319 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2320 break;
2322 memcpy(info->xmit_buf + info->xmit_head, buf, c);
2323 info->xmit_head = ((info->xmit_head + c) &
2324 (SERIAL_XMIT_SIZE-1));
2325 info->xmit_cnt += c;
2326 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2327 buf += c;
2328 count -= c;
2329 ret += c;
2334 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
2335 spin_lock_irqsave(&info->irq_spinlock,flags);
2336 if (!info->tx_active)
2337 usc_start_transmitter(info);
2338 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2340 cleanup:
2341 if ( debug_level >= DEBUG_LEVEL_INFO )
2342 printk( "%s(%d):mgsl_write(%s) returning=%d\n",
2343 __FILE__,__LINE__,info->device_name,ret);
2345 return ret;
2347 } /* end of mgsl_write() */
2349 /* mgsl_write_room()
2351 * Return the count of free bytes in transmit buffer
2353 * Arguments: tty pointer to tty info structure
2354 * Return Value: None
2356 static int mgsl_write_room(struct tty_struct *tty)
2358 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2359 int ret;
2361 if (mgsl_paranoia_check(info, tty->device, "mgsl_write_room"))
2362 return 0;
2363 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
2364 if (ret < 0)
2365 ret = 0;
2367 if (debug_level >= DEBUG_LEVEL_INFO)
2368 printk("%s(%d):mgsl_write_room(%s)=%d\n",
2369 __FILE__,__LINE__, info->device_name,ret );
2371 if ( info->params.mode == MGSL_MODE_HDLC ) {
2372 /* operating in synchronous (frame oriented) mode */
2373 if ( info->tx_active )
2374 return 0;
2375 else
2376 return HDLC_MAX_FRAME_SIZE;
2379 return ret;
2381 } /* end of mgsl_write_room() */
2383 /* mgsl_chars_in_buffer()
2385 * Return the count of bytes in transmit buffer
2387 * Arguments: tty pointer to tty info structure
2388 * Return Value: None
2390 static int mgsl_chars_in_buffer(struct tty_struct *tty)
2392 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2394 if (debug_level >= DEBUG_LEVEL_INFO)
2395 printk("%s(%d):mgsl_chars_in_buffer(%s)\n",
2396 __FILE__,__LINE__, info->device_name );
2398 if (mgsl_paranoia_check(info, tty->device, "mgsl_chars_in_buffer"))
2399 return 0;
2401 if (debug_level >= DEBUG_LEVEL_INFO)
2402 printk("%s(%d):mgsl_chars_in_buffer(%s)=%d\n",
2403 __FILE__,__LINE__, info->device_name,info->xmit_cnt );
2405 if ( info->params.mode == MGSL_MODE_HDLC ) {
2406 /* operating in synchronous (frame oriented) mode */
2407 if ( info->tx_active )
2408 return info->tx_buffer_list[0].rcc;
2409 else
2410 return 0;
2413 return info->xmit_cnt;
2414 } /* end of mgsl_chars_in_buffer() */
2416 /* mgsl_flush_buffer()
2418 * Discard all data in the send buffer
2420 * Arguments: tty pointer to tty info structure
2421 * Return Value: None
2423 static void mgsl_flush_buffer(struct tty_struct *tty)
2425 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2426 unsigned long flags;
2428 if (debug_level >= DEBUG_LEVEL_INFO)
2429 printk("%s(%d):mgsl_flush_buffer(%s) entry\n",
2430 __FILE__,__LINE__, info->device_name );
2432 if (mgsl_paranoia_check(info, tty->device, "mgsl_flush_buffer"))
2433 return;
2435 spin_lock_irqsave(&info->irq_spinlock,flags);
2436 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2437 del_timer(&info->tx_timer);
2438 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2440 wake_up_interruptible(&tty->write_wait);
2441 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2442 tty->ldisc.write_wakeup)
2443 (tty->ldisc.write_wakeup)(tty);
2445 } /* end of mgsl_flush_buffer() */
2447 /* mgsl_send_xchar()
2449 * Send a high-priority XON/XOFF character
2451 * Arguments: tty pointer to tty info structure
2452 * ch character to send
2453 * Return Value: None
2455 static void mgsl_send_xchar(struct tty_struct *tty, char ch)
2457 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2458 unsigned long flags;
2460 if (debug_level >= DEBUG_LEVEL_INFO)
2461 printk("%s(%d):mgsl_send_xchar(%s,%d)\n",
2462 __FILE__,__LINE__, info->device_name, ch );
2464 if (mgsl_paranoia_check(info, tty->device, "mgsl_send_xchar"))
2465 return;
2467 info->x_char = ch;
2468 if (ch) {
2469 /* Make sure transmit interrupts are on */
2470 spin_lock_irqsave(&info->irq_spinlock,flags);
2471 if (!info->tx_enabled)
2472 usc_start_transmitter(info);
2473 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2475 } /* end of mgsl_send_xchar() */
2477 /* mgsl_throttle()
2479 * Signal remote device to throttle send data (our receive data)
2481 * Arguments: tty pointer to tty info structure
2482 * Return Value: None
2484 static void mgsl_throttle(struct tty_struct * tty)
2486 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2487 unsigned long flags;
2489 if (debug_level >= DEBUG_LEVEL_INFO)
2490 printk("%s(%d):mgsl_throttle(%s) entry\n",
2491 __FILE__,__LINE__, info->device_name );
2493 if (mgsl_paranoia_check(info, tty->device, "mgsl_throttle"))
2494 return;
2496 if (I_IXOFF(tty))
2497 mgsl_send_xchar(tty, STOP_CHAR(tty));
2499 if (tty->termios->c_cflag & CRTSCTS) {
2500 spin_lock_irqsave(&info->irq_spinlock,flags);
2501 info->serial_signals &= ~SerialSignal_RTS;
2502 usc_set_serial_signals(info);
2503 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2505 } /* end of mgsl_throttle() */
2507 /* mgsl_unthrottle()
2509 * Signal remote device to stop throttling send data (our receive data)
2511 * Arguments: tty pointer to tty info structure
2512 * Return Value: None
2514 static void mgsl_unthrottle(struct tty_struct * tty)
2516 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
2517 unsigned long flags;
2519 if (debug_level >= DEBUG_LEVEL_INFO)
2520 printk("%s(%d):mgsl_unthrottle(%s) entry\n",
2521 __FILE__,__LINE__, info->device_name );
2523 if (mgsl_paranoia_check(info, tty->device, "mgsl_unthrottle"))
2524 return;
2526 if (I_IXOFF(tty)) {
2527 if (info->x_char)
2528 info->x_char = 0;
2529 else
2530 mgsl_send_xchar(tty, START_CHAR(tty));
2533 if (tty->termios->c_cflag & CRTSCTS) {
2534 spin_lock_irqsave(&info->irq_spinlock,flags);
2535 info->serial_signals |= SerialSignal_RTS;
2536 usc_set_serial_signals(info);
2537 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2540 } /* end of mgsl_unthrottle() */
2542 /* mgsl_get_stats()
2544 * get the current serial parameters information
2546 * Arguments: info pointer to device instance data
2547 * user_icount pointer to buffer to hold returned stats
2549 * Return Value: 0 if success, otherwise error code
2551 static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount *user_icount)
2553 int err;
2555 if (debug_level >= DEBUG_LEVEL_INFO)
2556 printk("%s(%d):mgsl_get_params(%s)\n",
2557 __FILE__,__LINE__, info->device_name);
2559 COPY_TO_USER(err,user_icount, &info->icount, sizeof(struct mgsl_icount));
2560 if (err) {
2561 if ( debug_level >= DEBUG_LEVEL_INFO )
2562 printk( "%s(%d):mgsl_get_stats(%s) user buffer copy failed\n",
2563 __FILE__,__LINE__,info->device_name);
2564 return -EFAULT;
2567 return 0;
2569 } /* end of mgsl_get_stats() */
2571 /* mgsl_get_params()
2573 * get the current serial parameters information
2575 * Arguments: info pointer to device instance data
2576 * user_params pointer to buffer to hold returned params
2578 * Return Value: 0 if success, otherwise error code
2580 static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS *user_params)
2582 int err;
2583 if (debug_level >= DEBUG_LEVEL_INFO)
2584 printk("%s(%d):mgsl_get_params(%s)\n",
2585 __FILE__,__LINE__, info->device_name);
2587 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
2588 if (err) {
2589 if ( debug_level >= DEBUG_LEVEL_INFO )
2590 printk( "%s(%d):mgsl_get_params(%s) user buffer copy failed\n",
2591 __FILE__,__LINE__,info->device_name);
2592 return -EFAULT;
2595 return 0;
2597 } /* end of mgsl_get_params() */
2599 /* mgsl_set_params()
2601 * set the serial parameters
2603 * Arguments:
2605 * info pointer to device instance data
2606 * new_params user buffer containing new serial params
2608 * Return Value: 0 if success, otherwise error code
2610 static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS *new_params)
2612 unsigned long flags;
2613 MGSL_PARAMS tmp_params;
2614 int err;
2616 if (debug_level >= DEBUG_LEVEL_INFO)
2617 printk("%s(%d):mgsl_set_params %s\n", __FILE__,__LINE__,
2618 info->device_name );
2619 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2620 if (err) {
2621 if ( debug_level >= DEBUG_LEVEL_INFO )
2622 printk( "%s(%d):mgsl_set_params(%s) user buffer copy failed\n",
2623 __FILE__,__LINE__,info->device_name);
2624 return -EFAULT;
2627 spin_lock_irqsave(&info->irq_spinlock,flags);
2628 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2629 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2631 mgsl_change_params(info);
2633 return 0;
2635 } /* end of mgsl_set_params() */
2637 /* mgsl_get_txidle()
2639 * get the current transmit idle mode
2641 * Arguments: info pointer to device instance data
2642 * idle_mode pointer to buffer to hold returned idle mode
2644 * Return Value: 0 if success, otherwise error code
2646 static int mgsl_get_txidle(struct mgsl_struct * info, int*idle_mode)
2648 int err;
2650 if (debug_level >= DEBUG_LEVEL_INFO)
2651 printk("%s(%d):mgsl_get_txidle(%s)=%d\n",
2652 __FILE__,__LINE__, info->device_name, info->idle_mode);
2654 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2655 if (err) {
2656 if ( debug_level >= DEBUG_LEVEL_INFO )
2657 printk( "%s(%d):mgsl_get_txidle(%s) user buffer copy failed\n",
2658 __FILE__,__LINE__,info->device_name);
2659 return -EFAULT;
2662 return 0;
2664 } /* end of mgsl_get_txidle() */
2666 /* mgsl_set_txidle() service ioctl to set transmit idle mode
2668 * Arguments: info pointer to device instance data
2669 * idle_mode new idle mode
2671 * Return Value: 0 if success, otherwise error code
2673 static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode)
2675 unsigned long flags;
2677 if (debug_level >= DEBUG_LEVEL_INFO)
2678 printk("%s(%d):mgsl_set_txidle(%s,%d)\n", __FILE__,__LINE__,
2679 info->device_name, idle_mode );
2681 spin_lock_irqsave(&info->irq_spinlock,flags);
2682 info->idle_mode = idle_mode;
2683 usc_set_txidle( info );
2684 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2685 return 0;
2687 } /* end of mgsl_set_txidle() */
2689 /* mgsl_txenable()
2691 * enable or disable the transmitter
2693 * Arguments:
2695 * info pointer to device instance data
2696 * enable 1 = enable, 0 = disable
2698 * Return Value: 0 if success, otherwise error code
2700 static int mgsl_txenable(struct mgsl_struct * info, int enable)
2702 unsigned long flags;
2704 if (debug_level >= DEBUG_LEVEL_INFO)
2705 printk("%s(%d):mgsl_txenable(%s,%d)\n", __FILE__,__LINE__,
2706 info->device_name, enable);
2708 spin_lock_irqsave(&info->irq_spinlock,flags);
2709 if ( enable ) {
2710 if ( !info->tx_enabled ) {
2712 usc_start_transmitter(info);
2713 /*--------------------------------------------------
2714 * if HDLC/SDLC Loop mode, attempt to insert the
2715 * station in the 'loop' by setting CMR:13. Upon
2716 * receipt of the next GoAhead (RxAbort) sequence,
2717 * the OnLoop indicator (CCSR:7) should go active
2718 * to indicate that we are on the loop
2719 *--------------------------------------------------*/
2720 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
2721 usc_loopmode_insert_request( info );
2723 } else {
2724 if ( info->tx_enabled )
2725 usc_stop_transmitter(info);
2727 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2728 return 0;
2730 } /* end of mgsl_txenable() */
2732 /* mgsl_txabort() abort send HDLC frame
2734 * Arguments: info pointer to device instance data
2735 * Return Value: 0 if success, otherwise error code
2737 static int mgsl_txabort(struct mgsl_struct * info)
2739 unsigned long flags;
2741 if (debug_level >= DEBUG_LEVEL_INFO)
2742 printk("%s(%d):mgsl_txabort(%s)\n", __FILE__,__LINE__,
2743 info->device_name);
2745 spin_lock_irqsave(&info->irq_spinlock,flags);
2746 if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC )
2748 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
2749 usc_loopmode_cancel_transmit( info );
2750 else
2751 usc_TCmd(info,TCmd_SendAbort);
2753 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2754 return 0;
2756 } /* end of mgsl_txabort() */
2758 /* mgsl_rxenable() enable or disable the receiver
2760 * Arguments: info pointer to device instance data
2761 * enable 1 = enable, 0 = disable
2762 * Return Value: 0 if success, otherwise error code
2764 static int mgsl_rxenable(struct mgsl_struct * info, int enable)
2766 unsigned long flags;
2768 if (debug_level >= DEBUG_LEVEL_INFO)
2769 printk("%s(%d):mgsl_rxenable(%s,%d)\n", __FILE__,__LINE__,
2770 info->device_name, enable);
2772 spin_lock_irqsave(&info->irq_spinlock,flags);
2773 if ( enable ) {
2774 if ( !info->rx_enabled )
2775 usc_start_receiver(info);
2776 } else {
2777 if ( info->rx_enabled )
2778 usc_stop_receiver(info);
2780 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2781 return 0;
2783 } /* end of mgsl_rxenable() */
2785 /* mgsl_wait_event() wait for specified event to occur
2787 * Arguments: info pointer to device instance data
2788 * mask pointer to bitmask of events to wait for
2789 * Return Value: 0 if successful and bit mask updated with
2790 * of events triggerred,
2791 * otherwise error code
2793 static int mgsl_wait_event(struct mgsl_struct * info, int * mask_ptr)
2795 unsigned long flags;
2796 int s;
2797 int rc=0;
2798 u16 regval;
2799 struct mgsl_icount cprev, cnow;
2800 int events = 0;
2801 int mask;
2802 struct _input_signal_events signal_events_prev, signal_events_now;
2804 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2805 if (rc) {
2806 return -EFAULT;
2809 if (debug_level >= DEBUG_LEVEL_INFO)
2810 printk("%s(%d):mgsl_wait_event(%s,%d)\n", __FILE__,__LINE__,
2811 info->device_name, mask);
2813 spin_lock_irqsave(&info->irq_spinlock,flags);
2815 usc_get_serial_signals(info);
2816 s = info->serial_signals;
2818 /* note the counters on entry */
2819 cprev = info->icount;
2820 signal_events_prev = info->input_signal_events;
2822 if (mask & MgslEvent_ExitHuntMode) {
2823 /* enable exit hunt mode IRQ */
2824 regval = usc_InReg(info,RICR);
2825 if (!(regval & RXSTATUS_EXITED_HUNT))
2826 usc_OutReg(info, RICR, regval | RXSTATUS_EXITED_HUNT);
2829 if (mask & MgslEvent_IdleReceived) {
2830 /* enable idle mode received IRQ */
2831 regval = usc_InReg(info,RICR);
2832 if (!(regval & RXSTATUS_IDLE_RECEIVED))
2833 usc_OutReg(info, RICR, regval | RXSTATUS_IDLE_RECEIVED);
2836 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2838 /* Determine if any user requested events for input signals is currently TRUE */
2840 events |= (mask & ((s & SerialSignal_DSR) ?
2841 MgslEvent_DsrActive:MgslEvent_DsrInactive));
2843 events |= (mask & ((s & SerialSignal_DCD) ?
2844 MgslEvent_DcdActive:MgslEvent_DcdInactive));
2846 events |= (mask & ((s & SerialSignal_CTS) ?
2847 MgslEvent_CtsActive:MgslEvent_CtsInactive));
2849 events |= (mask & ((s & SerialSignal_RI) ?
2850 MgslEvent_RiActive:MgslEvent_RiInactive));
2853 while(!events) {
2854 /* sleep until event occurs */
2855 interruptible_sleep_on(&info->event_wait_q);
2857 /* see if a signal woke us */
2858 if (signal_pending(current)) {
2859 rc = -ERESTARTSYS;
2860 break;
2863 spin_lock_irqsave(&info->irq_spinlock,flags);
2865 /* get icount and serial signal states */
2866 cnow = info->icount;
2867 signal_events_now = info->input_signal_events;
2868 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2870 if (signal_events_now.dsr_up != signal_events_prev.dsr_up &&
2871 mask & MgslEvent_DsrActive )
2872 events |= MgslEvent_DsrActive;
2874 if (signal_events_now.dsr_down != signal_events_prev.dsr_down &&
2875 mask & MgslEvent_DsrInactive )
2876 events |= MgslEvent_DsrInactive;
2878 if (signal_events_now.dcd_up != signal_events_prev.dcd_up &&
2879 mask & MgslEvent_DcdActive )
2880 events |= MgslEvent_DcdActive;
2882 if (signal_events_now.dcd_down != signal_events_prev.dcd_down &&
2883 mask & MgslEvent_DcdInactive )
2884 events |= MgslEvent_DcdInactive;
2886 if (signal_events_now.cts_up != signal_events_prev.cts_up &&
2887 mask & MgslEvent_CtsActive )
2888 events |= MgslEvent_CtsActive;
2890 if (signal_events_now.cts_down != signal_events_prev.cts_down &&
2891 mask & MgslEvent_CtsInactive )
2892 events |= MgslEvent_CtsInactive;
2894 if (signal_events_now.ri_up != signal_events_prev.ri_up &&
2895 mask & MgslEvent_RiActive )
2896 events |= MgslEvent_RiActive;
2898 if (signal_events_now.ri_down != signal_events_prev.ri_down &&
2899 mask & MgslEvent_RiInactive )
2900 events |= MgslEvent_RiInactive;
2902 if (cnow.exithunt != cprev.exithunt)
2903 events |= (mask & MgslEvent_ExitHuntMode);
2905 if (cnow.rxidle != cprev.rxidle)
2906 events |= (mask & MgslEvent_IdleReceived);
2908 cprev = cnow;
2909 signal_events_prev = signal_events_now;
2912 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2913 spin_lock_irqsave(&info->irq_spinlock,flags);
2914 if (!waitqueue_active(&info->event_wait_q)) {
2915 /* disable enable exit hunt mode/idle rcvd IRQs */
2916 regval = usc_InReg(info,RICR);
2917 usc_OutReg(info, RICR, regval &
2918 ~(RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED));
2920 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2923 if ( rc == 0 )
2924 PUT_USER(rc, events, mask_ptr);
2926 return rc;
2928 } /* end of mgsl_wait_event() */
2930 /* get_modem_info()
2932 * Read the state of the serial control and
2933 * status signals and return to caller.
2935 * Arguments: info pointer to device instance data
2936 * value pointer to int to hold returned info
2938 * Return Value: 0 if success, otherwise error code
2940 static int get_modem_info(struct mgsl_struct * info, unsigned int *value)
2942 unsigned int result = 0;
2943 unsigned long flags;
2944 int err;
2946 spin_lock_irqsave(&info->irq_spinlock,flags);
2947 usc_get_serial_signals(info);
2948 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2950 if (info->serial_signals & SerialSignal_RTS)
2951 result |= TIOCM_RTS;
2952 if (info->serial_signals & SerialSignal_DTR)
2953 result |= TIOCM_DTR;
2954 if (info->serial_signals & SerialSignal_DCD)
2955 result |= TIOCM_CAR;
2956 if (info->serial_signals & SerialSignal_RI)
2957 result |= TIOCM_RNG;
2958 if (info->serial_signals & SerialSignal_DSR)
2959 result |= TIOCM_DSR;
2960 if (info->serial_signals & SerialSignal_CTS)
2961 result |= TIOCM_CTS;
2963 if (debug_level >= DEBUG_LEVEL_INFO)
2964 printk("%s(%d):mgsl_get_modem_info %s value=%08X\n",
2965 __FILE__,__LINE__, info->device_name, result );
2967 PUT_USER(err,result,value);
2968 return err;
2969 } /* end of get_modem_info() */
2971 /* set_modem_info()
2973 * Set the state of the modem control signals (DTR/RTS)
2975 * Arguments:
2977 * info pointer to device instance data
2978 * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
2979 * TIOCMSET = set/clear signal values
2980 * value bit mask for command
2982 * Return Value: 0 if success, otherwise error code
2984 static int set_modem_info(struct mgsl_struct * info, unsigned int cmd,
2985 unsigned int *value)
2987 int error;
2988 unsigned int arg;
2989 unsigned long flags;
2991 if (debug_level >= DEBUG_LEVEL_INFO)
2992 printk("%s(%d):mgsl_set_modem_info %s\n", __FILE__,__LINE__,
2993 info->device_name );
2995 GET_USER(error,arg,value);
2996 if (error)
2997 return error;
2999 switch (cmd) {
3000 case TIOCMBIS:
3001 if (arg & TIOCM_RTS)
3002 info->serial_signals |= SerialSignal_RTS;
3003 if (arg & TIOCM_DTR)
3004 info->serial_signals |= SerialSignal_DTR;
3005 break;
3006 case TIOCMBIC:
3007 if (arg & TIOCM_RTS)
3008 info->serial_signals &= ~SerialSignal_RTS;
3009 if (arg & TIOCM_DTR)
3010 info->serial_signals &= ~SerialSignal_DTR;
3011 break;
3012 case TIOCMSET:
3013 if (arg & TIOCM_RTS)
3014 info->serial_signals |= SerialSignal_RTS;
3015 else
3016 info->serial_signals &= ~SerialSignal_RTS;
3018 if (arg & TIOCM_DTR)
3019 info->serial_signals |= SerialSignal_DTR;
3020 else
3021 info->serial_signals &= ~SerialSignal_DTR;
3022 break;
3023 default:
3024 return -EINVAL;
3027 spin_lock_irqsave(&info->irq_spinlock,flags);
3028 usc_set_serial_signals(info);
3029 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3031 return 0;
3033 } /* end of set_modem_info() */
3035 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
3036 /* mgsl_break() Set or clear transmit break condition
3038 * Arguments: tty pointer to tty instance data
3039 * break_state -1=set break condition, 0=clear
3040 * Return Value: None
3042 static void mgsl_break(struct tty_struct *tty, int break_state)
3044 struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
3045 unsigned long flags;
3047 if (debug_level >= DEBUG_LEVEL_INFO)
3048 printk("%s(%d):mgsl_break(%s,%d)\n",
3049 __FILE__,__LINE__, info->device_name, break_state);
3051 if (mgsl_paranoia_check(info, tty->device, "mgsl_break"))
3052 return;
3054 spin_lock_irqsave(&info->irq_spinlock,flags);
3055 if (break_state == -1)
3056 usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) | BIT7));
3057 else
3058 usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) & ~BIT7));
3059 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3061 } /* end of mgsl_break() */
3062 #endif
3064 /* mgsl_ioctl() Service an IOCTL request
3066 * Arguments:
3068 * tty pointer to tty instance data
3069 * file pointer to associated file object for device
3070 * cmd IOCTL command code
3071 * arg command argument/context
3073 * Return Value: 0 if success, otherwise error code
3075 static int mgsl_ioctl(struct tty_struct *tty, struct file * file,
3076 unsigned int cmd, unsigned long arg)
3078 int error;
3079 struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
3080 struct mgsl_icount cprev, cnow; /* kernel counter temps */
3081 struct serial_icounter_struct *p_cuser; /* user space */
3082 unsigned long flags;
3084 if (debug_level >= DEBUG_LEVEL_INFO)
3085 printk("%s(%d):mgsl_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
3086 info->device_name, cmd );
3088 if (mgsl_paranoia_check(info, tty->device, "mgsl_ioctl"))
3089 return -ENODEV;
3091 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
3092 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
3093 if (tty->flags & (1 << TTY_IO_ERROR))
3094 return -EIO;
3097 switch (cmd) {
3098 case TIOCMGET:
3099 return get_modem_info(info, (unsigned int *) arg);
3100 case TIOCMBIS:
3101 case TIOCMBIC:
3102 case TIOCMSET:
3103 return set_modem_info(info, cmd, (unsigned int *) arg);
3104 case MGSL_IOCGPARAMS:
3105 return mgsl_get_params(info,(MGSL_PARAMS *)arg);
3106 case MGSL_IOCSPARAMS:
3107 return mgsl_set_params(info,(MGSL_PARAMS *)arg);
3108 case MGSL_IOCGTXIDLE:
3109 return mgsl_get_txidle(info,(int*)arg);
3110 case MGSL_IOCSTXIDLE:
3111 return mgsl_set_txidle(info,(int)arg);
3112 case MGSL_IOCTXENABLE:
3113 return mgsl_txenable(info,(int)arg);
3114 case MGSL_IOCRXENABLE:
3115 return mgsl_rxenable(info,(int)arg);
3116 case MGSL_IOCTXABORT:
3117 return mgsl_txabort(info);
3118 case MGSL_IOCGSTATS:
3119 return mgsl_get_stats(info,(struct mgsl_icount*)arg);
3120 case MGSL_IOCWAITEVENT:
3121 return mgsl_wait_event(info,(int*)arg);
3122 case MGSL_IOCLOOPTXDONE:
3123 return mgsl_loopmode_send_done(info);
3124 case MGSL_IOCCLRMODCOUNT:
3125 while(MOD_IN_USE)
3126 MOD_DEC_USE_COUNT;
3127 return 0;
3129 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
3130 * - mask passed in arg for lines of interest
3131 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
3132 * Caller should use TIOCGICOUNT to see which one it was
3134 case TIOCMIWAIT:
3135 spin_lock_irqsave(&info->irq_spinlock,flags);
3136 /* note the counters on entry */
3137 cprev = info->icount;
3138 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3139 while (1) {
3140 interruptible_sleep_on(&info->status_event_wait_q);
3141 /* see if a signal did it */
3142 if (signal_pending(current))
3143 return -ERESTARTSYS;
3144 save_flags(flags); cli();
3145 cnow = info->icount; /* atomic copy */
3146 restore_flags(flags);
3147 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
3148 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
3149 return -EIO; /* no change => error */
3150 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
3151 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
3152 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
3153 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
3154 return 0;
3156 cprev = cnow;
3158 /* NOTREACHED */
3161 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
3162 * Return: write counters to the user passed counter struct
3163 * NB: both 1->0 and 0->1 transitions are counted except for
3164 * RI where only 0->1 is counted.
3166 case TIOCGICOUNT:
3167 spin_lock_irqsave(&info->irq_spinlock,flags);
3168 cnow = info->icount;
3169 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3170 p_cuser = (struct serial_icounter_struct *) arg;
3171 PUT_USER(error,cnow.cts, &p_cuser->cts);
3172 if (error) return error;
3173 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
3174 if (error) return error;
3175 PUT_USER(error,cnow.rng, &p_cuser->rng);
3176 if (error) return error;
3177 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
3178 if (error) return error;
3179 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
3180 PUT_USER(error,cnow.rx, &p_cuser->rx);
3181 if (error) return error;
3182 PUT_USER(error,cnow.tx, &p_cuser->tx);
3183 if (error) return error;
3184 PUT_USER(error,cnow.frame, &p_cuser->frame);
3185 if (error) return error;
3186 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
3187 if (error) return error;
3188 PUT_USER(error,cnow.parity, &p_cuser->parity);
3189 if (error) return error;
3190 PUT_USER(error,cnow.brk, &p_cuser->brk);
3191 if (error) return error;
3192 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
3193 if (error) return error;
3194 #endif
3195 return 0;
3197 default:
3198 return -ENOIOCTLCMD;
3200 return 0;
3203 /* mgsl_set_termios()
3205 * Set new termios settings
3207 * Arguments:
3209 * tty pointer to tty structure
3210 * termios pointer to buffer to hold returned old termios
3212 * Return Value: None
3214 static void mgsl_set_termios(struct tty_struct *tty, struct termios *old_termios)
3216 struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
3217 unsigned long flags;
3219 if (debug_level >= DEBUG_LEVEL_INFO)
3220 printk("%s(%d):mgsl_set_termios %s\n", __FILE__,__LINE__,
3221 tty->driver.name );
3223 /* just return if nothing has changed */
3224 if ((tty->termios->c_cflag == old_termios->c_cflag)
3225 && (RELEVANT_IFLAG(tty->termios->c_iflag)
3226 == RELEVANT_IFLAG(old_termios->c_iflag)))
3227 return;
3229 mgsl_change_params(info);
3231 /* Handle transition to B0 status */
3232 if (old_termios->c_cflag & CBAUD &&
3233 !(tty->termios->c_cflag & CBAUD)) {
3234 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
3235 spin_lock_irqsave(&info->irq_spinlock,flags);
3236 usc_set_serial_signals(info);
3237 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3240 /* Handle transition away from B0 status */
3241 if (!(old_termios->c_cflag & CBAUD) &&
3242 tty->termios->c_cflag & CBAUD) {
3243 info->serial_signals |= SerialSignal_DTR;
3244 if (!(tty->termios->c_cflag & CRTSCTS) ||
3245 !test_bit(TTY_THROTTLED, &tty->flags)) {
3246 info->serial_signals |= SerialSignal_RTS;
3248 spin_lock_irqsave(&info->irq_spinlock,flags);
3249 usc_set_serial_signals(info);
3250 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3253 /* Handle turning off CRTSCTS */
3254 if (old_termios->c_cflag & CRTSCTS &&
3255 !(tty->termios->c_cflag & CRTSCTS)) {
3256 tty->hw_stopped = 0;
3257 mgsl_start(tty);
3260 } /* end of mgsl_set_termios() */
3262 /* mgsl_close()
3264 * Called when port is closed. Wait for remaining data to be
3265 * sent. Disable port and free resources.
3267 * Arguments:
3269 * tty pointer to open tty structure
3270 * filp pointer to open file object
3272 * Return Value: None
3274 static void mgsl_close(struct tty_struct *tty, struct file * filp)
3276 struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
3278 if (!info || mgsl_paranoia_check(info, tty->device, "mgsl_close"))
3279 return;
3281 if (debug_level >= DEBUG_LEVEL_INFO)
3282 printk("%s(%d):mgsl_close(%s) entry, count=%d\n",
3283 __FILE__,__LINE__, info->device_name, info->count);
3285 if (!info->count || tty_hung_up_p(filp))
3286 goto cleanup;
3288 if ((tty->count == 1) && (info->count != 1)) {
3290 * tty->count is 1 and the tty structure will be freed.
3291 * info->count should be one in this case.
3292 * if it's not, correct it so that the port is shutdown.
3294 printk("mgsl_close: bad refcount; tty->count is 1, "
3295 "info->count is %d\n", info->count);
3296 info->count = 1;
3299 info->count--;
3301 /* if at least one open remaining, leave hardware active */
3302 if (info->count)
3303 goto cleanup;
3305 info->flags |= ASYNC_CLOSING;
3307 /* Save the termios structure, since this port may have
3308 * separate termios for callout and dialin.
3310 if (info->flags & ASYNC_NORMAL_ACTIVE)
3311 info->normal_termios = *tty->termios;
3312 if (info->flags & ASYNC_CALLOUT_ACTIVE)
3313 info->callout_termios = *tty->termios;
3315 /* set tty->closing to notify line discipline to
3316 * only process XON/XOFF characters. Only the N_TTY
3317 * discipline appears to use this (ppp does not).
3319 tty->closing = 1;
3321 /* wait for transmit data to clear all layers */
3323 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
3324 if (debug_level >= DEBUG_LEVEL_INFO)
3325 printk("%s(%d):mgsl_close(%s) calling tty_wait_until_sent\n",
3326 __FILE__,__LINE__, info->device_name );
3327 tty_wait_until_sent(tty, info->closing_wait);
3330 if (info->flags & ASYNC_INITIALIZED)
3331 mgsl_wait_until_sent(tty, info->timeout);
3333 if (tty->driver.flush_buffer)
3334 tty->driver.flush_buffer(tty);
3336 if (tty->ldisc.flush_buffer)
3337 tty->ldisc.flush_buffer(tty);
3339 shutdown(info);
3341 tty->closing = 0;
3342 info->tty = 0;
3344 if (info->blocked_open) {
3345 if (info->close_delay) {
3346 current->state = TASK_INTERRUPTIBLE;
3347 schedule_timeout(info->close_delay);
3349 wake_up_interruptible(&info->open_wait);
3352 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
3353 ASYNC_CLOSING);
3355 wake_up_interruptible(&info->close_wait);
3357 cleanup:
3358 if (debug_level >= DEBUG_LEVEL_INFO)
3359 printk("%s(%d):mgsl_close(%s) exit, count=%d\n", __FILE__,__LINE__,
3360 tty->driver.name, info->count);
3361 if(MOD_IN_USE)
3362 MOD_DEC_USE_COUNT;
3364 } /* end of mgsl_close() */
3366 /* mgsl_wait_until_sent()
3368 * Wait until the transmitter is empty.
3370 * Arguments:
3372 * tty pointer to tty info structure
3373 * timeout time to wait for send completion
3375 * Return Value: None
3377 static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout)
3379 struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
3380 unsigned long orig_jiffies, char_time;
3382 if (!info )
3383 return;
3385 if (debug_level >= DEBUG_LEVEL_INFO)
3386 printk("%s(%d):mgsl_wait_until_sent(%s) entry\n",
3387 __FILE__,__LINE__, info->device_name );
3389 if (mgsl_paranoia_check(info, tty->device, "mgsl_wait_until_sent"))
3390 return;
3392 if (!(info->flags & ASYNC_INITIALIZED))
3393 goto exit;
3395 orig_jiffies = jiffies;
3397 /* Set check interval to 1/5 of estimated time to
3398 * send a character, and make it at least 1. The check
3399 * interval should also be less than the timeout.
3400 * Note: use tight timings here to satisfy the NIST-PCTS.
3403 if ( info->params.data_rate ) {
3404 char_time = info->timeout/(32 * 5);
3405 if (!char_time)
3406 char_time++;
3407 } else
3408 char_time = 1;
3410 if (timeout)
3411 char_time = MIN(char_time, timeout);
3413 if ( info->params.mode == MGSL_MODE_HDLC ) {
3414 while (info->tx_active) {
3415 current->state = TASK_INTERRUPTIBLE;
3416 schedule_timeout(char_time);
3417 if (signal_pending(current))
3418 break;
3419 if (timeout && ((orig_jiffies + timeout) < jiffies))
3420 break;
3422 } else {
3423 while (!(usc_InReg(info,TCSR) & TXSTATUS_ALL_SENT) &&
3424 info->tx_enabled) {
3425 current->state = TASK_INTERRUPTIBLE;
3426 schedule_timeout(char_time);
3427 if (signal_pending(current))
3428 break;
3429 if (timeout && ((orig_jiffies + timeout) < jiffies))
3430 break;
3434 current->state = TASK_RUNNING;
3435 exit:
3436 if (debug_level >= DEBUG_LEVEL_INFO)
3437 printk("%s(%d):mgsl_wait_until_sent(%s) exit\n",
3438 __FILE__,__LINE__, info->device_name );
3440 } /* end of mgsl_wait_until_sent() */
3442 /* mgsl_hangup()
3444 * Called by tty_hangup() when a hangup is signaled.
3445 * This is the same as to closing all open files for the port.
3447 * Arguments: tty pointer to associated tty object
3448 * Return Value: None
3450 static void mgsl_hangup(struct tty_struct *tty)
3452 struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
3454 if (debug_level >= DEBUG_LEVEL_INFO)
3455 printk("%s(%d):mgsl_hangup(%s)\n",
3456 __FILE__,__LINE__, info->device_name );
3458 if (mgsl_paranoia_check(info, tty->device, "mgsl_hangup"))
3459 return;
3461 mgsl_flush_buffer(tty);
3462 shutdown(info);
3464 info->count = 0;
3465 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
3466 info->tty = 0;
3468 wake_up_interruptible(&info->open_wait);
3470 } /* end of mgsl_hangup() */
3472 /* block_til_ready()
3474 * Block the current process until the specified port
3475 * is ready to be opened.
3477 * Arguments:
3479 * tty pointer to tty info structure
3480 * filp pointer to open file object
3481 * info pointer to device instance data
3483 * Return Value: 0 if success, otherwise error code
3485 static int block_til_ready(struct tty_struct *tty, struct file * filp,
3486 struct mgsl_struct *info)
3488 DECLARE_WAITQUEUE(wait, current);
3489 int retval;
3490 int do_clocal = 0, extra_count = 0;
3491 unsigned long flags;
3493 if (debug_level >= DEBUG_LEVEL_INFO)
3494 printk("%s(%d):block_til_ready on %s\n",
3495 __FILE__,__LINE__, tty->driver.name );
3497 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
3498 /* this is a callout device */
3499 /* just verify that normal device is not in use */
3500 if (info->flags & ASYNC_NORMAL_ACTIVE)
3501 return -EBUSY;
3502 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
3503 (info->flags & ASYNC_SESSION_LOCKOUT) &&
3504 (info->session != current->session))
3505 return -EBUSY;
3506 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
3507 (info->flags & ASYNC_PGRP_LOCKOUT) &&
3508 (info->pgrp != current->pgrp))
3509 return -EBUSY;
3510 info->flags |= ASYNC_CALLOUT_ACTIVE;
3511 return 0;
3514 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3515 /* nonblock mode is set or port is not enabled */
3516 /* just verify that callout device is not active */
3517 if (info->flags & ASYNC_CALLOUT_ACTIVE)
3518 return -EBUSY;
3519 info->flags |= ASYNC_NORMAL_ACTIVE;
3520 return 0;
3523 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
3524 if (info->normal_termios.c_cflag & CLOCAL)
3525 do_clocal = 1;
3526 } else {
3527 if (tty->termios->c_cflag & CLOCAL)
3528 do_clocal = 1;
3531 /* Wait for carrier detect and the line to become
3532 * free (i.e., not in use by the callout). While we are in
3533 * this loop, info->count is dropped by one, so that
3534 * mgsl_close() knows when to free things. We restore it upon
3535 * exit, either normal or abnormal.
3538 retval = 0;
3539 add_wait_queue(&info->open_wait, &wait);
3541 if (debug_level >= DEBUG_LEVEL_INFO)
3542 printk("%s(%d):block_til_ready before block on %s count=%d\n",
3543 __FILE__,__LINE__, tty->driver.name, info->count );
3545 save_flags(flags); cli();
3546 if (!tty_hung_up_p(filp)) {
3547 extra_count = 1;
3548 info->count--;
3550 restore_flags(flags);
3551 info->blocked_open++;
3553 while (1) {
3554 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
3555 (tty->termios->c_cflag & CBAUD)) {
3556 spin_lock_irqsave(&info->irq_spinlock,flags);
3557 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
3558 usc_set_serial_signals(info);
3559 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3562 set_current_state(TASK_INTERRUPTIBLE);
3564 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
3565 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
3566 -EAGAIN : -ERESTARTSYS;
3567 break;
3570 spin_lock_irqsave(&info->irq_spinlock,flags);
3571 usc_get_serial_signals(info);
3572 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3574 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
3575 !(info->flags & ASYNC_CLOSING) &&
3576 (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
3577 break;
3580 if (signal_pending(current)) {
3581 retval = -ERESTARTSYS;
3582 break;
3585 if (debug_level >= DEBUG_LEVEL_INFO)
3586 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
3587 __FILE__,__LINE__, tty->driver.name, info->count );
3589 schedule();
3592 current->state = TASK_RUNNING;
3593 remove_wait_queue(&info->open_wait, &wait);
3595 if (extra_count)
3596 info->count++;
3597 info->blocked_open--;
3599 if (debug_level >= DEBUG_LEVEL_INFO)
3600 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
3601 __FILE__,__LINE__, tty->driver.name, info->count );
3603 if (!retval)
3604 info->flags |= ASYNC_NORMAL_ACTIVE;
3606 return retval;
3608 } /* end of block_til_ready() */
3610 /* mgsl_open()
3612 * Called when a port is opened. Init and enable port.
3613 * Perform serial-specific initialization for the tty structure.
3615 * Arguments: tty pointer to tty info structure
3616 * filp associated file pointer
3618 * Return Value: 0 if success, otherwise error code
3620 static int mgsl_open(struct tty_struct *tty, struct file * filp)
3622 struct mgsl_struct *info;
3623 int retval, line;
3624 unsigned long page;
3626 /* verify range of specified line number */
3627 line = MINOR(tty->device) - tty->driver.minor_start;
3628 if ((line < 0) || (line >= mgsl_device_count)) {
3629 printk("%s(%d):mgsl_open with illegal line #%d.\n",
3630 __FILE__,__LINE__,line);
3631 return -ENODEV;
3634 /* find the info structure for the specified line */
3635 info = mgsl_device_list;
3636 while(info && info->line != line)
3637 info = info->next_device;
3638 if ( !info ){
3639 printk("%s(%d):Can't find specified device on open (line=%d)\n",
3640 __FILE__,__LINE__,line);
3641 return -ENODEV;
3644 tty->driver_data = info;
3645 info->tty = tty;
3646 if (mgsl_paranoia_check(info, tty->device, "mgsl_open"))
3647 return -ENODEV;
3649 if (debug_level >= DEBUG_LEVEL_INFO)
3650 printk("%s(%d):mgsl_open(%s), old ref count = %d\n",
3651 __FILE__,__LINE__,tty->driver.name, info->count);
3653 MOD_INC_USE_COUNT;
3655 /* If port is closing, signal caller to try again */
3656 if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
3657 if (info->flags & ASYNC_CLOSING)
3658 interruptible_sleep_on(&info->close_wait);
3659 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
3660 -EAGAIN : -ERESTARTSYS);
3661 goto cleanup;
3664 if (!tmp_buf) {
3665 page = get_free_page(GFP_KERNEL);
3666 if (!page) {
3667 retval = -ENOMEM;
3668 goto cleanup;
3670 if (tmp_buf)
3671 free_page(page);
3672 else
3673 tmp_buf = (unsigned char *) page;
3676 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
3677 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
3678 #endif
3680 info->count++;
3681 if (info->count == 1) {
3682 /* 1st open on this device, init hardware */
3683 retval = startup(info);
3684 if (retval < 0)
3685 goto cleanup;
3688 retval = block_til_ready(tty, filp, info);
3689 if (retval) {
3690 if (debug_level >= DEBUG_LEVEL_INFO)
3691 printk("%s(%d):block_til_ready(%s) returned %d\n",
3692 __FILE__,__LINE__, info->device_name, retval);
3693 goto cleanup;
3696 if ((info->count == 1) &&
3697 info->flags & ASYNC_SPLIT_TERMIOS) {
3698 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
3699 *tty->termios = info->normal_termios;
3700 else
3701 *tty->termios = info->callout_termios;
3702 mgsl_change_params(info);
3705 info->session = current->session;
3706 info->pgrp = current->pgrp;
3708 if (debug_level >= DEBUG_LEVEL_INFO)
3709 printk("%s(%d):mgsl_open(%s) success\n",
3710 __FILE__,__LINE__, info->device_name);
3711 retval = 0;
3713 cleanup:
3714 if (retval) {
3715 if(MOD_IN_USE)
3716 MOD_DEC_USE_COUNT;
3717 if(info->count)
3718 info->count--;
3721 return retval;
3723 } /* end of mgsl_open() */
3726 * /proc fs routines....
3729 static inline int line_info(char *buf, struct mgsl_struct *info)
3731 char stat_buf[30];
3732 int ret;
3733 unsigned long flags;
3735 if (info->bus_type == MGSL_BUS_TYPE_PCI) {
3736 ret = sprintf(buf, "%s:PCI io:%04X irq:%d mem:%08X lcr:%08X",
3737 info->device_name, info->io_base, info->irq_level,
3738 info->phys_memory_base, info->phys_lcr_base);
3739 } else {
3740 ret = sprintf(buf, "%s:(E)ISA io:%04X irq:%d dma:%d",
3741 info->device_name, info->io_base,
3742 info->irq_level, info->dma_level);
3745 /* output current serial signal states */
3746 spin_lock_irqsave(&info->irq_spinlock,flags);
3747 usc_get_serial_signals(info);
3748 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3750 stat_buf[0] = 0;
3751 stat_buf[1] = 0;
3752 if (info->serial_signals & SerialSignal_RTS)
3753 strcat(stat_buf, "|RTS");
3754 if (info->serial_signals & SerialSignal_CTS)
3755 strcat(stat_buf, "|CTS");
3756 if (info->serial_signals & SerialSignal_DTR)
3757 strcat(stat_buf, "|DTR");
3758 if (info->serial_signals & SerialSignal_DSR)
3759 strcat(stat_buf, "|DSR");
3760 if (info->serial_signals & SerialSignal_DCD)
3761 strcat(stat_buf, "|CD");
3762 if (info->serial_signals & SerialSignal_RI)
3763 strcat(stat_buf, "|RI");
3765 if (info->params.mode == MGSL_MODE_HDLC) {
3766 ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
3767 info->icount.txok, info->icount.rxok);
3768 if (info->icount.txunder)
3769 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
3770 if (info->icount.txabort)
3771 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
3772 if (info->icount.rxshort)
3773 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
3774 if (info->icount.rxlong)
3775 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
3776 if (info->icount.rxover)
3777 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
3778 if (info->icount.rxcrc)
3779 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxcrc);
3780 } else {
3781 ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
3782 info->icount.tx, info->icount.rx);
3783 if (info->icount.frame)
3784 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
3785 if (info->icount.parity)
3786 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
3787 if (info->icount.brk)
3788 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
3789 if (info->icount.overrun)
3790 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
3793 /* Append serial signal status to end */
3794 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
3796 ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d bh_q=%p\n",
3797 info->tx_active,info->bh_requested,info->bh_running,
3798 info->bh_queue_head);
3800 spin_lock_irqsave(&info->irq_spinlock,flags);
3802 u16 Tscr = usc_InReg( info, TCSR );
3803 u16 Tdmr = usc_InDmaReg( info, TDMR );
3804 u16 Ticr = usc_InReg( info, TICR );
3805 u16 Rscr = usc_InReg( info, RCSR );
3806 u16 Rdmr = usc_InDmaReg( info, RDMR );
3807 u16 Ricr = usc_InReg( info, RICR );
3808 u16 Icr = usc_InReg( info, ICR );
3809 u16 Dccr = usc_InReg( info, DCCR );
3810 u16 Tmr = usc_InReg( info, TMR );
3811 u16 Tccr = usc_InReg( info, TCCR );
3812 u16 Ccar = inw( info->io_base + CCAR );
3813 ret += sprintf(buf+ret, "tcsr=%04X tdmr=%04X ticr=%04X rcsr=%04X rdmr=%04X\n"
3814 "ricr=%04X icr =%04X dccr=%04X tmr=%04X tccr=%04X ccar=%04X\n",
3815 Tscr,Tdmr,Ticr,Rscr,Rdmr,Ricr,Icr,Dccr,Tmr,Tccr,Ccar );
3817 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3819 return ret;
3821 } /* end of line_info() */
3823 /* mgsl_read_proc()
3825 * Called to print information about devices
3827 * Arguments:
3828 * page page of memory to hold returned info
3829 * start
3830 * off
3831 * count
3832 * eof
3833 * data
3835 * Return Value:
3837 int mgsl_read_proc(char *page, char **start, off_t off, int count,
3838 int *eof, void *data)
3840 int len = 0, l;
3841 off_t begin = 0;
3842 struct mgsl_struct *info;
3844 len += sprintf(page, "synclink driver:%s\n", driver_version);
3846 info = mgsl_device_list;
3847 while( info ) {
3848 l = line_info(page + len, info);
3849 len += l;
3850 if (len+begin > off+count)
3851 goto done;
3852 if (len+begin < off) {
3853 begin += len;
3854 len = 0;
3856 info = info->next_device;
3859 *eof = 1;
3860 done:
3861 if (off >= len+begin)
3862 return 0;
3863 *start = page + (begin-off);
3864 return ((count < begin+len-off) ? count : begin+len-off);
3866 } /* end of mgsl_read_proc() */
3868 /* mgsl_allocate_dma_buffers()
3870 * Allocate and format DMA buffers (ISA adapter)
3871 * or format shared memory buffers (PCI adapter).
3873 * Arguments: info pointer to device instance data
3874 * Return Value: 0 if success, otherwise error
3876 int mgsl_allocate_dma_buffers(struct mgsl_struct *info)
3878 unsigned short BuffersPerFrame;
3880 info->last_mem_alloc = 0;
3882 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
3884 * The PCI adapter has 256KBytes of shared memory to use.
3885 * This is 64 PAGE_SIZE buffers. 1 is used for the buffer
3886 * list. 2 are used for the transmit and one is left as
3887 * a spare. The 4K buffer list can hold 128 DMA_BUFFER
3888 * structures at 32bytes each.
3891 info->rx_buffer_count = 60;
3892 info->tx_buffer_count = 2;
3893 } else {
3894 /* Calculate the number of PAGE_SIZE buffers needed for */
3895 /* receive and transmit DMA buffers. */
3897 /* Calculate the number of DMA buffers necessary to hold the */
3898 /* largest allowable frame size. Note: If the max frame size is */
3899 /* not an even multiple of the DMA buffer size then we need to */
3900 /* round the buffer count per frame up one. */
3902 BuffersPerFrame = (unsigned short)(info->max_frame_size/DMABUFFERSIZE);
3903 if ( info->max_frame_size % DMABUFFERSIZE )
3904 BuffersPerFrame++;
3906 /* Calculate the number of DMA buffers necessary to */
3907 /* hold 7 max size receive frames and one max size transmit frame. */
3908 /* The receive buffer count is bumped by one so we avoid an */
3909 /* End of List condition if all receive buffers are used when */
3910 /* using linked list DMA buffers. */
3912 info->rx_buffer_count = (BuffersPerFrame * MAXRXFRAMES) + 6;
3913 info->tx_buffer_count = BuffersPerFrame;
3916 if ( debug_level >= DEBUG_LEVEL_INFO )
3917 printk("%s(%d):Allocating %d TX and %d RX DMA buffers.\n",
3918 __FILE__,__LINE__, info->tx_buffer_count,info->rx_buffer_count);
3920 if ( mgsl_alloc_buffer_list_memory( info ) < 0 ||
3921 mgsl_alloc_frame_memory(info, info->rx_buffer_list, info->rx_buffer_count) < 0 ||
3922 mgsl_alloc_frame_memory(info, info->tx_buffer_list, info->tx_buffer_count) < 0) {
3923 printk("%s(%d):Can't allocate DMA buffer memory\n",__FILE__,__LINE__);
3924 return -ENOMEM;
3927 mgsl_reset_rx_dma_buffers( info );
3929 return 0;
3931 } /* end of mgsl_allocate_dma_buffers() */
3934 * mgsl_alloc_buffer_list_memory()
3936 * Allocate a common DMA buffer for use as the
3937 * receive and transmit buffer lists.
3939 * A buffer list is a set of buffer entries where each entry contains
3940 * a pointer to an actual buffer and a pointer to the next buffer entry
3941 * (plus some other info about the buffer).
3943 * The buffer entries for a list are built to form a circular list so
3944 * that when the entire list has been traversed you start back at the
3945 * beginning.
3947 * This function allocates memory for just the buffer entries.
3948 * The links (pointer to next entry) are filled in with the physical
3949 * address of the next entry so the adapter can navigate the list
3950 * using bus master DMA. The pointers to the actual buffers are filled
3951 * out later when the actual buffers are allocated.
3953 * Arguments: info pointer to device instance data
3954 * Return Value: 0 if success, otherwise error
3956 int mgsl_alloc_buffer_list_memory( struct mgsl_struct *info )
3958 unsigned int i;
3960 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
3961 /* PCI adapter uses shared memory. */
3962 info->buffer_list = info->memory_base + info->last_mem_alloc;
3963 info->buffer_list_phys = info->last_mem_alloc;
3964 info->last_mem_alloc += BUFFERLISTSIZE;
3965 } else {
3966 /* ISA adapter uses system memory. */
3967 /* The buffer lists are allocated as a common buffer that both */
3968 /* the processor and adapter can access. This allows the driver to */
3969 /* inspect portions of the buffer while other portions are being */
3970 /* updated by the adapter using Bus Master DMA. */
3972 info->buffer_list = kmalloc(BUFFERLISTSIZE, GFP_KERNEL | GFP_DMA);
3973 if ( info->buffer_list == NULL )
3974 return -ENOMEM;
3976 info->buffer_list_phys = virt_to_bus(info->buffer_list);
3979 /* We got the memory for the buffer entry lists. */
3980 /* Initialize the memory block to all zeros. */
3981 memset( info->buffer_list, 0, BUFFERLISTSIZE );
3983 /* Save virtual address pointers to the receive and */
3984 /* transmit buffer lists. (Receive 1st). These pointers will */
3985 /* be used by the processor to access the lists. */
3986 info->rx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
3987 info->tx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
3988 info->tx_buffer_list += info->rx_buffer_count;
3991 * Build the links for the buffer entry lists such that
3992 * two circular lists are built. (Transmit and Receive).
3994 * Note: the links are physical addresses
3995 * which are read by the adapter to determine the next
3996 * buffer entry to use.
3999 for ( i = 0; i < info->rx_buffer_count; i++ ) {
4000 /* calculate and store physical address of this buffer entry */
4001 info->rx_buffer_list[i].phys_entry =
4002 info->buffer_list_phys + (i * sizeof(DMABUFFERENTRY));
4004 /* calculate and store physical address of */
4005 /* next entry in cirular list of entries */
4007 info->rx_buffer_list[i].link = info->buffer_list_phys;
4009 if ( i < info->rx_buffer_count - 1 )
4010 info->rx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
4013 for ( i = 0; i < info->tx_buffer_count; i++ ) {
4014 /* calculate and store physical address of this buffer entry */
4015 info->tx_buffer_list[i].phys_entry = info->buffer_list_phys +
4016 ((info->rx_buffer_count + i) * sizeof(DMABUFFERENTRY));
4018 /* calculate and store physical address of */
4019 /* next entry in cirular list of entries */
4021 info->tx_buffer_list[i].link = info->buffer_list_phys +
4022 info->rx_buffer_count * sizeof(DMABUFFERENTRY);
4024 if ( i < info->tx_buffer_count - 1 )
4025 info->tx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
4028 return 0;
4030 } /* end of mgsl_alloc_buffer_list_memory() */
4033 * mgsl_free_buffer_list_memory()
4035 * Free the common DMA buffer allocated for use as the
4036 * receive and transmit buffer lists. The associated Memory
4037 * Descriptor List (MDL) is also freed.
4039 * Warning:
4041 * The data transfer buffers associated with the buffer list
4042 * MUST be freed before freeing the buffer list itself because
4043 * the buffer list contains the information necessary to free
4044 * the individual buffers!
4046 * Arguments: info pointer to device extension
4047 * Return Value: None
4049 void mgsl_free_buffer_list_memory( struct mgsl_struct *info )
4051 if ( info->buffer_list && info->bus_type != MGSL_BUS_TYPE_PCI )
4052 kfree_s(info->buffer_list, BUFFERLISTSIZE);
4054 info->buffer_list = NULL;
4055 info->rx_buffer_list = NULL;
4056 info->tx_buffer_list = NULL;
4058 } /* end of mgsl_free_buffer_list_memory() */
4061 * mgsl_alloc_frame_memory()
4063 * Allocate the frame DMA buffers used by the specified buffer list.
4064 * Each DMA buffer will be one memory page in size. This is necessary
4065 * because memory can fragment enough that it may be impossible
4066 * contiguous pages.
4068 * Arguments:
4070 * info pointer to device instance data
4071 * BufferList pointer to list of buffer entries
4072 * Buffercount count of buffer entries in buffer list
4074 * Return Value: 0 if success, otherwise -ENOMEM
4076 int mgsl_alloc_frame_memory(struct mgsl_struct *info,DMABUFFERENTRY *BufferList,int Buffercount)
4078 int i;
4079 unsigned long phys_addr;
4081 /* Allocate page sized buffers for the receive buffer list */
4083 for ( i = 0; i < Buffercount; i++ ) {
4084 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
4085 /* PCI adapter uses shared memory buffers. */
4086 BufferList[i].virt_addr = info->memory_base + info->last_mem_alloc;
4087 phys_addr = info->last_mem_alloc;
4088 info->last_mem_alloc += DMABUFFERSIZE;
4089 } else {
4090 /* ISA adapter uses system memory. */
4091 BufferList[i].virt_addr =
4092 kmalloc(DMABUFFERSIZE, GFP_KERNEL | GFP_DMA);
4093 if ( BufferList[i].virt_addr == NULL )
4094 return -ENOMEM;
4095 phys_addr = virt_to_bus(BufferList[i].virt_addr);
4097 BufferList[i].phys_addr = phys_addr;
4100 return 0;
4102 } /* end of mgsl_alloc_frame_memory() */
4105 * mgsl_free_frame_memory()
4107 * Free the buffers associated with
4108 * each buffer entry of a buffer list.
4110 * Arguments:
4112 * info pointer to device instance data
4113 * BufferList pointer to list of buffer entries
4114 * Buffercount count of buffer entries in buffer list
4116 * Return Value: None
4118 void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList, int Buffercount)
4120 int i;
4122 if ( BufferList ) {
4123 for ( i = 0 ; i < Buffercount ; i++ ) {
4124 if ( BufferList[i].virt_addr ) {
4125 if ( info->bus_type != MGSL_BUS_TYPE_PCI )
4126 kfree_s(BufferList[i].virt_addr, DMABUFFERSIZE);
4127 BufferList[i].virt_addr = NULL;
4132 } /* end of mgsl_free_frame_memory() */
4134 /* mgsl_free_dma_buffers()
4136 * Free DMA buffers
4138 * Arguments: info pointer to device instance data
4139 * Return Value: None
4141 void mgsl_free_dma_buffers( struct mgsl_struct *info )
4143 mgsl_free_frame_memory( info, info->rx_buffer_list, info->rx_buffer_count );
4144 mgsl_free_frame_memory( info, info->tx_buffer_list, info->tx_buffer_count );
4145 mgsl_free_buffer_list_memory( info );
4147 } /* end of mgsl_free_dma_buffers() */
4149 /* mgsl_claim_resources()
4151 * Claim all resources used by a device
4153 * Arguments: info pointer to device instance data
4154 * Return Value: 0 if success, otherwise -ENODEV
4156 int mgsl_claim_resources(struct mgsl_struct *info)
4158 /* claim 16C32 I/O base address */
4160 if ( check_region(info->io_base,info->io_addr_size) < 0 ) {
4161 printk( "%s(%d):I/O address conflict on device %s Addr=%08X\n",
4162 __FILE__,__LINE__,info->device_name, info->io_base );
4163 return -ENODEV;
4165 request_region(info->io_base,info->io_addr_size,"synclink.o");
4166 info->io_addr_requested = 1;
4168 /* claim interrupt level */
4170 if ( request_irq(info->irq_level,mgsl_interrupt,info->irq_flags,
4171 info->device_name, info ) < 0 ) {
4172 printk( "%s(%d):Cant request interrupt on device %s IRQ=%d\n",
4173 __FILE__,__LINE__,info->device_name, info->irq_level );
4174 mgsl_release_resources( info );
4175 return -ENODEV;
4177 info->irq_requested = 1;
4179 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
4180 /* claim shared memory range */
4181 info->memory_base = ioremap(info->phys_memory_base,0x40000);
4182 if (!info->memory_base) {
4183 printk( "%s(%d):Cant map shared memory on device %s MemAddr=%08X\n",
4184 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
4185 mgsl_release_resources( info );
4186 return -ENODEV;
4189 /* test the shared memory range */
4190 if ( !mgsl_memory_test(info) ) {
4191 printk( "%s(%d):Failed shared memory test %s MemAddr=%08X\n",
4192 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
4193 mgsl_release_resources( info );
4194 return -ENODEV;
4197 /* claim LCR memory range */
4198 info->lcr_base = ioremap(info->phys_lcr_base,PAGE_SIZE) + info->lcr_offset;
4199 if (!info->lcr_base) {
4200 printk( "%s(%d):Cant map LCR memory on device %s MemAddr=%08X\n",
4201 __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
4202 mgsl_release_resources( info );
4203 return -ENODEV;
4206 } else {
4207 /* claim DMA channel */
4209 if (request_dma(info->dma_level,info->device_name) < 0){
4210 printk( "%s(%d):Cant request DMA channel on device %s DMA=%d\n",
4211 __FILE__,__LINE__,info->device_name, info->dma_level );
4212 mgsl_release_resources( info );
4213 return -ENODEV;
4215 info->dma_requested = 1;
4217 /* ISA adapter uses bus master DMA */
4218 set_dma_mode(info->dma_level,DMA_MODE_CASCADE);
4219 enable_dma(info->dma_level);
4222 if ( mgsl_allocate_dma_buffers(info) < 0 ) {
4223 printk( "%s(%d):Cant allocate DMA buffers on device %s DMA=%d\n",
4224 __FILE__,__LINE__,info->device_name, info->dma_level );
4225 mgsl_release_resources( info );
4226 return -ENODEV;
4229 return 0;
4231 } /* end of mgsl_claim_resources() */
4233 /* mgsl_release_resources()
4235 * Release all resources used by a device
4237 * Arguments: info pointer to device instance data
4238 * Return Value: None
4240 void mgsl_release_resources(struct mgsl_struct *info)
4242 if ( debug_level >= DEBUG_LEVEL_INFO )
4243 printk( "%s(%d):mgsl_release_resources(%s) entry\n",
4244 __FILE__,__LINE__,info->device_name );
4246 if ( info->irq_requested ) {
4247 free_irq(info->irq_level, info);
4248 info->irq_requested = 0;
4251 if ( info->dma_requested ) {
4252 disable_dma(info->dma_level);
4253 free_dma(info->dma_level);
4254 info->dma_requested = 0;
4256 mgsl_free_dma_buffers(info);
4258 if ( info->io_addr_requested ) {
4259 release_region(info->io_base,info->io_addr_size);
4260 info->io_addr_requested = 0;
4263 if (info->memory_base){
4264 iounmap(info->memory_base);
4265 info->memory_base = 0;
4268 if (info->lcr_base){
4269 iounmap(info->lcr_base - info->lcr_offset);
4270 info->lcr_base = 0;
4273 if ( debug_level >= DEBUG_LEVEL_INFO )
4274 printk( "%s(%d):mgsl_release_resources(%s) exit\n",
4275 __FILE__,__LINE__,info->device_name );
4277 } /* end of mgsl_release_resources() */
4279 /* mgsl_add_device()
4281 * Add the specified device instance data structure to the
4282 * global linked list of devices and increment the device count.
4284 * Arguments: info pointer to device instance data
4285 * Return Value: None
4287 void mgsl_add_device( struct mgsl_struct *info )
4289 info->next_device = NULL;
4290 info->line = mgsl_device_count;
4291 sprintf(info->device_name,"ttySL%d",info->line);
4293 mgsl_device_count++;
4295 if ( !mgsl_device_list )
4296 mgsl_device_list = info;
4297 else {
4298 struct mgsl_struct *current_dev = mgsl_device_list;
4299 while( current_dev->next_device )
4300 current_dev = current_dev->next_device;
4301 current_dev->next_device = info;
4304 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
4305 printk( "SyncLink device %s added:PCI bus IO=%04X IRQ=%d Mem=%08X LCR=%08X\n",
4306 info->device_name, info->io_base, info->irq_level,
4307 info->phys_memory_base, info->phys_lcr_base );
4308 } else {
4309 printk( "SyncLink device %s added:ISA bus IO=%04X IRQ=%d DMA=%d\n",
4310 info->device_name, info->io_base, info->irq_level, info->dma_level );
4313 } /* end of mgsl_add_device() */
4315 /* mgsl_allocate_device()
4317 * Allocate and initialize a device instance structure
4319 * Arguments: None
4320 * Return Value: pointer to mgsl_struct if success, otherwise NULL
4322 struct mgsl_struct* mgsl_allocate_device()
4324 struct mgsl_struct *info;
4326 info = (struct mgsl_struct *)kmalloc(sizeof(struct mgsl_struct),
4327 GFP_KERNEL);
4329 if (!info) {
4330 printk("Error can't allocate device instance data\n");
4331 } else {
4332 memset(info, 0, sizeof(struct mgsl_struct));
4333 info->magic = MGSL_MAGIC;
4334 info->task.sync = 0;
4335 info->task.routine = mgsl_bh_handler;
4336 info->task.data = info;
4337 info->max_frame_size = 4096;
4338 info->close_delay = 5*HZ/10;
4339 info->closing_wait = 30*HZ;
4340 init_waitqueue_head(&info->open_wait);
4341 init_waitqueue_head(&info->close_wait);
4342 init_waitqueue_head(&info->status_event_wait_q);
4343 init_waitqueue_head(&info->event_wait_q);
4345 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
4346 info->idle_mode = HDLC_TXIDLE_FLAGS;
4349 return info;
4351 } /* end of mgsl_allocate_device()*/
4353 /* mgsl_enumerate_devices()
4355 * Enumerate SyncLink serial devices based on user specified
4356 * options for ISA adapters and autodetected PCI adapters.
4358 * Arguments: None
4359 * Return Value: 0 if success, otherwise error code
4361 int mgsl_enumerate_devices()
4363 struct mgsl_struct *info;
4364 int i;
4366 /* Check for user specified ISA devices */
4368 for (i=0 ;(i < MAX_ISA_DEVICES) && io[i] && irq[i]; i++){
4369 if ( debug_level >= DEBUG_LEVEL_INFO )
4370 printk("ISA device specified io=%04X,irq=%d,dma=%d\n",
4371 io[i], irq[i], dma[i] );
4373 info = mgsl_allocate_device();
4374 if ( !info ) {
4375 /* error allocating device instance data */
4376 if ( debug_level >= DEBUG_LEVEL_ERROR )
4377 printk( "can't allocate device instance data.\n");
4378 continue;
4381 /* Copy user configuration info to device instance data */
4382 info->io_base = (unsigned int)io[i];
4383 info->irq_level = (unsigned int)irq[i];
4384 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
4385 info->irq_level = irq_cannonicalize(info->irq_level);
4386 #else
4387 if (info->irq_level == 2)
4388 info->irq_level = 9;
4389 #endif
4390 info->dma_level = (unsigned int)dma[i];
4391 info->bus_type = MGSL_BUS_TYPE_ISA;
4392 info->io_addr_size = 16;
4393 info->irq_flags = 0;
4395 /* add new device to device list */
4396 mgsl_add_device( info );
4400 #ifdef CONFIG_PCI
4401 /* Auto detect PCI adapters */
4403 if ( pcibios_present() ) {
4404 unsigned char bus;
4405 unsigned char func;
4406 unsigned int shared_mem_base;
4407 unsigned int lcr_mem_base;
4408 unsigned int io_base;
4409 unsigned char irq_line;
4411 for(i=0;;i++){
4412 if ( PCIBIOS_SUCCESSFUL == pcibios_find_device(
4413 MICROGATE_VENDOR_ID, SYNCLINK_DEVICE_ID, i, &bus, &func) ) {
4415 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
4416 struct pci_dev *pdev = pci_find_slot(bus,func);
4417 irq_line = pdev->irq;
4418 #else
4419 if (pcibios_read_config_byte(bus,func,
4420 PCI_INTERRUPT_LINE,&irq_line) ) {
4421 printk( "%s(%d):USC I/O addr not set.\n",
4422 __FILE__,__LINE__);
4423 continue;
4425 #endif
4427 if (pcibios_read_config_dword(bus,func,
4428 PCI_BASE_ADDRESS_3,&shared_mem_base) ) {
4429 printk( "%s(%d):Shared mem addr not set.\n",
4430 __FILE__,__LINE__);
4431 continue;
4434 if (pcibios_read_config_dword(bus,func,
4435 PCI_BASE_ADDRESS_0,&lcr_mem_base) ) {
4436 printk( "%s(%d):LCR mem addr not set.\n",
4437 __FILE__,__LINE__);
4438 continue;
4441 if (pcibios_read_config_dword(bus,func,
4442 PCI_BASE_ADDRESS_2,&io_base) ) {
4443 printk( "%s(%d):USC I/O addr not set.\n",
4444 __FILE__,__LINE__);
4445 continue;
4448 info = mgsl_allocate_device();
4449 if ( !info ) {
4450 /* error allocating device instance data */
4451 if ( debug_level >= DEBUG_LEVEL_ERROR )
4452 printk( "can't allocate device instance data.\n");
4453 continue;
4456 /* Copy user configuration info to device instance data */
4458 info->io_base = io_base & PCI_BASE_ADDRESS_IO_MASK;
4459 info->irq_level = (unsigned int)irq_line;
4460 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
4461 info->irq_level = irq_cannonicalize(info->irq_level);
4462 #else
4463 if (info->irq_level == 2)
4464 info->irq_level = 9;
4465 #endif
4466 info->phys_memory_base = shared_mem_base & PCI_BASE_ADDRESS_MEM_MASK;
4468 /* Because veremap only works on page boundaries we must map
4469 * a larger area than is actually implemented for the LCR
4470 * memory range. We map a full page starting at the page boundary.
4472 info->phys_lcr_base = lcr_mem_base & PCI_BASE_ADDRESS_MEM_MASK;
4473 info->lcr_offset = info->phys_lcr_base & (PAGE_SIZE-1);
4474 info->phys_lcr_base &= ~(PAGE_SIZE-1);
4476 info->bus_type = MGSL_BUS_TYPE_PCI;
4477 info->io_addr_size = 8;
4478 info->irq_flags = SA_SHIRQ;
4479 info->bus = bus;
4480 info->function = func;
4482 /* Store the PCI9050 misc control register value because a flaw
4483 * in the PCI9050 prevents LCR registers from being read if
4484 * BIOS assigns an LCR base address with bit 7 set.
4486 * Only the misc control register is accessed for which only
4487 * write access is needed, so set an initial value and change
4488 * bits to the device instance data as we write the value
4489 * to the actual misc control register.
4491 info->misc_ctrl_value = 0x087e4546;
4493 /* add new device to device list */
4494 mgsl_add_device( info );
4495 } else {
4496 break;
4500 #endif
4503 * Allocate memory to hold the following tty/termios arrays
4504 * with an element for each enumerated device.
4507 serial_table = (struct tty_struct**)kmalloc(sizeof(struct tty_struct*)*mgsl_device_count, GFP_KERNEL);
4508 serial_termios = (struct termios**)kmalloc(sizeof(struct termios*)*mgsl_device_count, GFP_KERNEL);
4509 serial_termios_locked = (struct termios**)kmalloc(sizeof(struct termios*)*mgsl_device_count, GFP_KERNEL);
4511 if (!serial_table || !serial_termios || !serial_termios_locked){
4512 printk("%s(%d):Can't allocate tty/termios arrays.\n",
4513 __FILE__,__LINE__);
4514 return -ENOMEM;
4517 memset(serial_table,0,sizeof(struct tty_struct*)*mgsl_device_count);
4518 memset(serial_termios,0,sizeof(struct termios*)*mgsl_device_count);
4519 memset(serial_termios_locked,0,sizeof(struct termios*)*mgsl_device_count);
4521 return 0;
4523 } /* end of mgsl_enumerate_devices() */
4525 /* mgsl_init()
4527 * Driver initialization entry point.
4529 * Arguments: None
4530 * Return Value: 0 if success, otherwise error code
4532 int __init mgsl_init(void)
4534 struct mgsl_struct *info;
4536 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
4537 EXPORT_NO_SYMBOLS;
4538 #else
4539 register_symtab(NULL);
4540 #endif
4542 printk("%s version %s\n", driver_name, driver_version);
4544 /* determine how many SyncLink devices are installed */
4545 mgsl_enumerate_devices();
4546 if ( !mgsl_device_list ) {
4547 printk("%s(%d):No SyncLink devices found.\n",__FILE__,__LINE__);
4548 return -ENODEV;
4551 /* Initialize the tty_driver structure */
4553 memset(&serial_driver, 0, sizeof(struct tty_driver));
4554 serial_driver.magic = TTY_DRIVER_MAGIC;
4555 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
4556 serial_driver.driver_name = "synclink";
4557 #endif
4558 serial_driver.name = "ttySL";
4559 serial_driver.major = ttymajor;
4560 serial_driver.minor_start = 64;
4561 serial_driver.num = mgsl_device_count;
4562 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
4563 serial_driver.subtype = SERIAL_TYPE_NORMAL;
4564 serial_driver.init_termios = tty_std_termios;
4565 serial_driver.init_termios.c_cflag =
4566 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
4567 serial_driver.flags = TTY_DRIVER_REAL_RAW;
4568 serial_driver.refcount = &serial_refcount;
4569 serial_driver.table = serial_table;
4570 serial_driver.termios = serial_termios;
4571 serial_driver.termios_locked = serial_termios_locked;
4573 serial_driver.open = mgsl_open;
4574 serial_driver.close = mgsl_close;
4575 serial_driver.write = mgsl_write;
4576 serial_driver.put_char = mgsl_put_char;
4577 serial_driver.flush_chars = mgsl_flush_chars;
4578 serial_driver.write_room = mgsl_write_room;
4579 serial_driver.chars_in_buffer = mgsl_chars_in_buffer;
4580 serial_driver.flush_buffer = mgsl_flush_buffer;
4581 serial_driver.ioctl = mgsl_ioctl;
4582 serial_driver.throttle = mgsl_throttle;
4583 serial_driver.unthrottle = mgsl_unthrottle;
4584 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
4585 serial_driver.send_xchar = mgsl_send_xchar;
4586 serial_driver.break_ctl = mgsl_break;
4587 serial_driver.wait_until_sent = mgsl_wait_until_sent;
4588 serial_driver.read_proc = mgsl_read_proc;
4589 #endif
4590 serial_driver.set_termios = mgsl_set_termios;
4591 serial_driver.stop = mgsl_stop;
4592 serial_driver.start = mgsl_start;
4593 serial_driver.hangup = mgsl_hangup;
4596 * The callout device is just like normal device except for
4597 * major number and the subtype code.
4599 callout_driver = serial_driver;
4600 callout_driver.name = "cuaSL";
4601 callout_driver.major = cuamajor;
4602 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
4603 #if LINUX_VERSION_CODE >= VERSION(2,1,0)
4604 callout_driver.read_proc = 0;
4605 callout_driver.proc_entry = 0;
4606 #endif
4608 if (tty_register_driver(&serial_driver) < 0)
4609 printk("%s(%d):Couldn't register serial driver\n",
4610 __FILE__,__LINE__);
4612 if (tty_register_driver(&callout_driver) < 0)
4613 printk("%s(%d):Couldn't register callout driver\n",
4614 __FILE__,__LINE__);
4616 printk("%s version %s, tty major#%d callout major#%d\n",
4617 driver_name, driver_version,
4618 serial_driver.major, callout_driver.major);
4620 /* Propagate these values to all device instances */
4622 info = mgsl_device_list;
4623 while(info){
4624 info->callout_termios = callout_driver.init_termios;
4625 info->normal_termios = serial_driver.init_termios;
4626 info = info->next_device;
4629 return 0;
4631 } /* end of mgsl_init() */
4633 #ifdef MODULE
4634 int init_module(void)
4636 /* Uncomment this to kernel debug module.
4637 * mgsl_get_text_ptr() leaves the .text address in eax
4638 * which can be used with add-symbol-file with gdb.
4640 if (break_on_load) {
4641 mgsl_get_text_ptr();
4642 BREAKPOINT();
4645 return mgsl_init();
4648 void cleanup_module(void)
4650 unsigned long flags;
4651 int rc;
4652 struct mgsl_struct *info;
4654 printk("Unloading %s: version %s\n", driver_name, driver_version);
4655 save_flags(flags);
4656 cli();
4657 if ((rc = tty_unregister_driver(&serial_driver)))
4658 printk("%s(%d) failed to unregister tty driver err=%d\n",
4659 __FILE__,__LINE__,rc);
4660 if ((rc = tty_unregister_driver(&callout_driver)))
4661 printk("%s(%d) failed to unregister callout driver err=%d\n",
4662 __FILE__,__LINE__,rc);
4663 restore_flags(flags);
4665 info = mgsl_device_list;
4666 while(info) {
4667 mgsl_release_resources(info);
4668 info = info->next_device;
4671 if (tmp_buf) {
4672 free_page((unsigned long) tmp_buf);
4673 tmp_buf = NULL;
4676 if (serial_table)
4677 kfree_s(serial_table,sizeof(struct tty_struct*)*mgsl_device_count);
4679 if (serial_termios)
4680 kfree_s(serial_termios,sizeof(struct termios*)*mgsl_device_count);
4682 if (serial_termios_locked)
4683 kfree_s(serial_termios_locked,sizeof(struct termios*)*mgsl_device_count);
4685 } /* end of cleanup_module() */
4687 #endif /* MODULE */
4691 * usc_RTCmd()
4693 * Issue a USC Receive/Transmit command to the
4694 * Channel Command/Address Register (CCAR).
4696 * Notes:
4698 * The command is encoded in the most significant 5 bits <15..11>
4699 * of the CCAR value. Bits <10..7> of the CCAR must be preserved
4700 * and Bits <6..0> must be written as zeros.
4702 * Arguments:
4704 * info pointer to device information structure
4705 * Cmd command mask (use symbolic macros)
4707 * Return Value:
4709 * None
4711 void usc_RTCmd( struct mgsl_struct *info, u16 Cmd )
4713 /* output command to CCAR in bits <15..11> */
4714 /* preserve bits <10..7>, bits <6..0> must be zero */
4716 outw( Cmd + info->loopback_bits, info->io_base + CCAR );
4718 /* Read to flush write to CCAR */
4719 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4720 inw( info->io_base + CCAR );
4722 } /* end of usc_RTCmd() */
4725 * usc_DmaCmd()
4727 * Issue a DMA command to the DMA Command/Address Register (DCAR).
4729 * Arguments:
4731 * info pointer to device information structure
4732 * Cmd DMA command mask (usc_DmaCmd_XX Macros)
4734 * Return Value:
4736 * None
4738 void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd )
4740 /* write command mask to DCAR */
4741 outw( Cmd + info->mbre_bit, info->io_base );
4743 /* Read to flush write to DCAR */
4744 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4745 inw( info->io_base );
4747 } /* end of usc_DmaCmd() */
4750 * usc_OutDmaReg()
4752 * Write a 16-bit value to a USC DMA register
4754 * Arguments:
4756 * info pointer to device info structure
4757 * RegAddr register address (number) for write
4758 * RegValue 16-bit value to write to register
4760 * Return Value:
4762 * None
4765 void usc_OutDmaReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
4767 /* Note: The DCAR is located at the adapter base address */
4768 /* Note: must preserve state of BIT8 in DCAR */
4770 outw( RegAddr + info->mbre_bit, info->io_base );
4771 outw( RegValue, info->io_base );
4773 /* Read to flush write to DCAR */
4774 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4775 inw( info->io_base );
4777 } /* end of usc_OutDmaReg() */
4780 * usc_InDmaReg()
4782 * Read a 16-bit value from a DMA register
4784 * Arguments:
4786 * info pointer to device info structure
4787 * RegAddr register address (number) to read from
4789 * Return Value:
4791 * The 16-bit value read from register
4794 u16 usc_InDmaReg( struct mgsl_struct *info, u16 RegAddr )
4796 /* Note: The DCAR is located at the adapter base address */
4797 /* Note: must preserve state of BIT8 in DCAR */
4799 outw( RegAddr + info->mbre_bit, info->io_base );
4800 return inw( info->io_base );
4802 } /* end of usc_InDmaReg() */
4806 * usc_OutReg()
4808 * Write a 16-bit value to a USC serial channel register
4810 * Arguments:
4812 * info pointer to device info structure
4813 * RegAddr register address (number) to write to
4814 * RegValue 16-bit value to write to register
4816 * Return Value:
4818 * None
4821 void usc_OutReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
4823 outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
4824 outw( RegValue, info->io_base + CCAR );
4826 /* Read to flush write to CCAR */
4827 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4828 inw( info->io_base + CCAR );
4830 } /* end of usc_OutReg() */
4833 * usc_InReg()
4835 * Reads a 16-bit value from a USC serial channel register
4837 * Arguments:
4839 * info pointer to device extension
4840 * RegAddr register address (number) to read from
4842 * Return Value:
4844 * 16-bit value read from register
4846 u16 usc_InReg( struct mgsl_struct *info, u16 RegAddr )
4848 outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
4849 return inw( info->io_base + CCAR );
4851 } /* end of usc_InReg() */
4853 /* usc_set_sdlc_mode()
4855 * Set up the adapter for SDLC DMA communications.
4857 * Arguments: info pointer to device instance data
4858 * Return Value: NONE
4860 void usc_set_sdlc_mode( struct mgsl_struct *info )
4862 u16 RegValue;
4864 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
4867 ** Channel Mode Register (CMR)
4869 ** <15..14> 10 Tx Sub Modes, Send Flag on Underrun
4870 ** <13> 0 0 = Transmit Disabled (initially)
4871 ** <12> 0 1 = Consecutive Idles share common 0
4872 ** <11..8> 1110 Transmitter Mode = HDLC/SDLC Loop
4873 ** <7..4> 0000 Rx Sub Modes, addr/ctrl field handling
4874 ** <3..0> 0110 Receiver Mode = HDLC/SDLC
4876 ** 1000 1110 0000 0110 = 0x8e06
4878 RegValue = 0x8e06;
4880 /*--------------------------------------------------
4881 * ignore user options for UnderRun Actions and
4882 * preambles
4883 *--------------------------------------------------*/
4885 else
4887 /* Channel mode Register (CMR)
4889 * <15..14> 00 Tx Sub modes, Underrun Action
4890 * <13> 0 1 = Send Preamble before opening flag
4891 * <12> 0 1 = Consecutive Idles share common 0
4892 * <11..8> 0110 Transmitter mode = HDLC/SDLC
4893 * <7..4> 0000 Rx Sub modes, addr/ctrl field handling
4894 * <3..0> 0110 Receiver mode = HDLC/SDLC
4896 * 0000 0110 0000 0110 = 0x0606
4899 RegValue = 0x0606;
4901 if ( info->params.flags & HDLC_FLAG_UNDERRUN_ABORT15 )
4902 RegValue |= BIT14;
4903 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_FLAG )
4904 RegValue |= BIT15;
4905 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_CRC )
4906 RegValue |= BIT15 + BIT14;
4908 if ( info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE )
4909 RegValue |= BIT13;
4912 if ( info->params.flags & HDLC_FLAG_SHARE_ZERO )
4913 RegValue |= BIT12;
4915 if ( info->params.addr_filter != 0xff )
4917 /* set up receive address filtering */
4918 usc_OutReg( info, RSR, info->params.addr_filter );
4919 RegValue |= BIT4;
4922 usc_OutReg( info, CMR, RegValue );
4923 info->cmr_value = RegValue;
4925 /* Receiver mode Register (RMR)
4927 * <15..13> 000 encoding
4928 * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
4929 * <10> 1 1 = Set CRC to all 1s (use for SDLC/HDLC)
4930 * <9> 0 1 = Include Receive chars in CRC
4931 * <8> 1 1 = Use Abort/PE bit as abort indicator
4932 * <7..6> 00 Even parity
4933 * <5> 0 parity disabled
4934 * <4..2> 000 Receive Char Length = 8 bits
4935 * <1..0> 00 Disable Receiver
4937 * 0000 0101 0000 0000 = 0x0500
4940 RegValue = 0x0500;
4942 switch ( info->params.encoding ) {
4943 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
4944 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
4945 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break;
4946 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
4947 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break;
4948 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break;
4949 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break;
4952 if ( info->params.crc_type == HDLC_CRC_16_CCITT )
4953 RegValue |= BIT9;
4955 usc_OutReg( info, RMR, RegValue );
4959 /* Set the Receive count Limit Register (RCLR) to 0xffff. */
4960 /* When an opening flag of an SDLC frame is recognized the */
4961 /* Receive Character count (RCC) is loaded with the value in */
4962 /* RCLR. The RCC is decremented for each received byte. The */
4963 /* value of RCC is stored after the closing flag of the frame */
4964 /* allowing the frame size to be computed. */
4966 usc_OutReg( info, RCLR, RCLRVALUE );
4968 usc_RCmd( info, RCmd_SelectRicrdma_level );
4970 /* Receive Interrupt Control Register (RICR)
4972 * <15..8> ? RxFIFO DMA Request Level
4973 * <7> 0 Exited Hunt IA (Interrupt Arm)
4974 * <6> 0 Idle Received IA
4975 * <5> 0 Break/Abort IA
4976 * <4> 0 Rx Bound IA
4977 * <3> 1 Queued status reflects oldest 2 bytes in FIFO
4978 * <2> 0 Abort/PE IA
4979 * <1> 1 Rx Overrun IA
4980 * <0> 0 Select TC0 value for readback
4982 * 0000 0000 0000 1000 = 0x000a
4985 /* Carry over the Exit Hunt and Idle Received bits */
4986 /* in case they have been armed by usc_ArmEvents. */
4988 RegValue = usc_InReg( info, RICR ) & 0xc0;
4990 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4991 usc_OutReg( info, RICR, (u16)(0x030a | RegValue) );
4992 else
4993 usc_OutReg( info, RICR, (u16)(0x140a | RegValue) );
4995 /* Unlatch all Rx status bits and clear Rx status IRQ Pending */
4997 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
4998 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
5000 /* Transmit mode Register (TMR)
5002 * <15..13> 000 encoding
5003 * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
5004 * <10> 1 1 = Start CRC as all 1s (use for SDLC/HDLC)
5005 * <9> 0 1 = Tx CRC Enabled
5006 * <8> 0 1 = Append CRC to end of transmit frame
5007 * <7..6> 00 Transmit parity Even
5008 * <5> 0 Transmit parity Disabled
5009 * <4..2> 000 Tx Char Length = 8 bits
5010 * <1..0> 00 Disable Transmitter
5012 * 0000 0100 0000 0000 = 0x0400
5015 RegValue = 0x0400;
5017 switch ( info->params.encoding ) {
5018 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
5019 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
5020 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break;
5021 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
5022 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break;
5023 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break;
5024 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break;
5027 if ( info->params.crc_type == HDLC_CRC_16_CCITT )
5028 RegValue |= BIT9 + BIT8;
5030 usc_OutReg( info, TMR, RegValue );
5032 usc_set_txidle( info );
5035 usc_TCmd( info, TCmd_SelectTicrdma_level );
5037 /* Transmit Interrupt Control Register (TICR)
5039 * <15..8> ? Transmit FIFO DMA Level
5040 * <7> 0 Present IA (Interrupt Arm)
5041 * <6> 0 Idle Sent IA
5042 * <5> 1 Abort Sent IA
5043 * <4> 1 EOF/EOM Sent IA
5044 * <3> 0 CRC Sent IA
5045 * <2> 1 1 = Wait for SW Trigger to Start Frame
5046 * <1> 1 Tx Underrun IA
5047 * <0> 0 TC0 constant on read back
5049 * 0000 0000 0011 0110 = 0x0036
5052 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
5053 usc_OutReg( info, TICR, 0x0736 );
5054 else
5055 usc_OutReg( info, TICR, 0x1436 );
5057 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
5058 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
5060 /* Clock mode Control Register (CMCR)
5062 * <15..14> 00 counter 1 Source = Disabled
5063 * <13..12> 00 counter 0 Source = Disabled
5064 * <11..10> 11 BRG1 Input is TxC Pin
5065 * <9..8> 11 BRG0 Input is TxC Pin
5066 * <7..6> 01 DPLL Input is BRG1 Output
5067 * <5..3> XXX TxCLK comes from Port 0
5068 * <2..0> XXX RxCLK comes from Port 1
5070 * 0000 1111 0111 0111 = 0x0f77
5073 RegValue = 0x0f40;
5075 if ( info->params.flags & HDLC_FLAG_RXC_DPLL )
5076 RegValue |= 0x0003; /* RxCLK from DPLL */
5077 else if ( info->params.flags & HDLC_FLAG_RXC_BRG )
5078 RegValue |= 0x0004; /* RxCLK from BRG0 */
5079 else if ( info->params.flags & HDLC_FLAG_RXC_TXCPIN)
5080 RegValue |= 0x0006; /* RxCLK from TXC Input */
5081 else
5082 RegValue |= 0x0007; /* RxCLK from Port1 */
5084 if ( info->params.flags & HDLC_FLAG_TXC_DPLL )
5085 RegValue |= 0x0018; /* TxCLK from DPLL */
5086 else if ( info->params.flags & HDLC_FLAG_TXC_BRG )
5087 RegValue |= 0x0020; /* TxCLK from BRG0 */
5088 else if ( info->params.flags & HDLC_FLAG_TXC_RXCPIN)
5089 RegValue |= 0x0038; /* RxCLK from TXC Input */
5090 else
5091 RegValue |= 0x0030; /* TxCLK from Port0 */
5093 usc_OutReg( info, CMCR, RegValue );
5096 /* Hardware Configuration Register (HCR)
5098 * <15..14> 00 CTR0 Divisor:00=32,01=16,10=8,11=4
5099 * <13> 0 CTR1DSel:0=CTR0Div determines CTR0Div
5100 * <12> 0 CVOK:0=report code violation in biphase
5101 * <11..10> 00 DPLL Divisor:00=32,01=16,10=8,11=4
5102 * <9..8> XX DPLL mode:00=disable,01=NRZ,10=Biphase,11=Biphase Level
5103 * <7..6> 00 reserved
5104 * <5> 0 BRG1 mode:0=continuous,1=single cycle
5105 * <4> X BRG1 Enable
5106 * <3..2> 00 reserved
5107 * <1> 0 BRG0 mode:0=continuous,1=single cycle
5108 * <0> 0 BRG0 Enable
5111 RegValue = 0x0000;
5113 if ( info->params.flags & (HDLC_FLAG_RXC_DPLL + HDLC_FLAG_TXC_DPLL) ) {
5114 u32 XtalSpeed;
5115 u32 DpllDivisor;
5116 u16 Tc;
5118 /* DPLL is enabled. Use BRG1 to provide continuous reference clock */
5119 /* for DPLL. DPLL mode in HCR is dependent on the encoding used. */
5121 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
5122 XtalSpeed = 11059200;
5123 else
5124 XtalSpeed = 14745600;
5126 if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
5127 DpllDivisor = 16;
5128 RegValue |= BIT10;
5130 else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
5131 DpllDivisor = 8;
5132 RegValue |= BIT11;
5134 else
5135 DpllDivisor = 32;
5137 /* Tc = (Xtal/Speed) - 1 */
5138 /* If twice the remainder of (Xtal/Speed) is greater than Speed */
5139 /* then rounding up gives a more precise time constant. Instead */
5140 /* of rounding up and then subtracting 1 we just don't subtract */
5141 /* the one in this case. */
5143 /*--------------------------------------------------
5144 * ejz: for DPLL mode, application should use the
5145 * same clock speed as the partner system, even
5146 * though clocking is derived from the input RxData.
5147 * In case the user uses a 0 for the clock speed,
5148 * default to 0xffffffff and don't try to divide by
5149 * zero
5150 *--------------------------------------------------*/
5151 if ( info->params.clock_speed )
5153 Tc = (u16)((XtalSpeed/DpllDivisor)/info->params.clock_speed);
5154 if ( !((((XtalSpeed/DpllDivisor) % info->params.clock_speed) * 2)
5155 / info->params.clock_speed) )
5156 Tc--;
5158 else
5159 Tc = -1;
5162 /* Write 16-bit Time Constant for BRG1 */
5163 usc_OutReg( info, TC1R, Tc );
5165 RegValue |= BIT4; /* enable BRG1 */
5167 switch ( info->params.encoding ) {
5168 case HDLC_ENCODING_NRZ:
5169 case HDLC_ENCODING_NRZB:
5170 case HDLC_ENCODING_NRZI_MARK:
5171 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT8; break;
5172 case HDLC_ENCODING_BIPHASE_MARK:
5173 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT9; break;
5174 case HDLC_ENCODING_BIPHASE_LEVEL:
5175 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT9 + BIT8; break;
5179 usc_OutReg( info, HCR, RegValue );
5182 /* Channel Control/status Register (CCSR)
5184 * <15> X RCC FIFO Overflow status (RO)
5185 * <14> X RCC FIFO Not Empty status (RO)
5186 * <13> 0 1 = Clear RCC FIFO (WO)
5187 * <12> X DPLL Sync (RW)
5188 * <11> X DPLL 2 Missed Clocks status (RO)
5189 * <10> X DPLL 1 Missed Clock status (RO)
5190 * <9..8> 00 DPLL Resync on rising and falling edges (RW)
5191 * <7> X SDLC Loop On status (RO)
5192 * <6> X SDLC Loop Send status (RO)
5193 * <5> 1 Bypass counters for TxClk and RxClk (RW)
5194 * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
5195 * <1..0> 00 reserved
5197 * 0000 0000 0010 0000 = 0x0020
5200 usc_OutReg( info, CCSR, 0x1020 );
5203 if ( info->params.flags & HDLC_FLAG_AUTO_CTS ) {
5204 usc_OutReg( info, SICR,
5205 (u16)(usc_InReg(info,SICR) | SICR_CTS_INACTIVE) );
5209 /* enable Master Interrupt Enable bit (MIE) */
5210 usc_EnableMasterIrqBit( info );
5212 usc_ClearIrqPendingBits( info, RECEIVE_STATUS + RECEIVE_DATA +
5213 TRANSMIT_STATUS + TRANSMIT_DATA );
5215 info->mbre_bit = 0;
5216 outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
5217 usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
5218 info->mbre_bit = BIT8;
5219 outw( BIT8, info->io_base ); /* set Master Bus Enable (DCAR) */
5221 /* Enable DMAEN (Port 7, Bit 14) */
5222 /* This connects the DMA request signal to the ISA bus */
5223 /* on the ISA adapter. This has no effect for the PCI adapter */
5224 usc_OutReg( info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) & ~BIT14) );
5226 /* DMA Control Register (DCR)
5228 * <15..14> 10 Priority mode = Alternating Tx/Rx
5229 * 01 Rx has priority
5230 * 00 Tx has priority
5232 * <13> 1 Enable Priority Preempt per DCR<15..14>
5233 * (WARNING DCR<11..10> must be 00 when this is 1)
5234 * 0 Choose activate channel per DCR<11..10>
5236 * <12> 0 Little Endian for Array/List
5237 * <11..10> 00 Both Channels can use each bus grant
5238 * <9..6> 0000 reserved
5239 * <5> 0 7 CLK - Minimum Bus Re-request Interval
5240 * <4> 0 1 = drive D/C and S/D pins
5241 * <3> 1 1 = Add one wait state to all DMA cycles.
5242 * <2> 0 1 = Strobe /UAS on every transfer.
5243 * <1..0> 11 Addr incrementing only affects LS24 bits
5245 * 0110 0000 0000 1011 = 0x600b
5248 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5249 /* PCI adapter does not need DMA wait state */
5250 usc_OutDmaReg( info, DCR, 0xa00b );
5252 else
5253 usc_OutDmaReg( info, DCR, 0x800b );
5256 /* Receive DMA mode Register (RDMR)
5258 * <15..14> 11 DMA mode = Linked List Buffer mode
5259 * <13> 1 RSBinA/L = store Rx status Block in Arrary/List entry
5260 * <12> 1 Clear count of List Entry after fetching
5261 * <11..10> 00 Address mode = Increment
5262 * <9> 1 Terminate Buffer on RxBound
5263 * <8> 0 Bus Width = 16bits
5264 * <7..0> ? status Bits (write as 0s)
5266 * 1111 0010 0000 0000 = 0xf200
5269 usc_OutDmaReg( info, RDMR, 0xf200 );
5272 /* Transmit DMA mode Register (TDMR)
5274 * <15..14> 11 DMA mode = Linked List Buffer mode
5275 * <13> 1 TCBinA/L = fetch Tx Control Block from List entry
5276 * <12> 1 Clear count of List Entry after fetching
5277 * <11..10> 00 Address mode = Increment
5278 * <9> 1 Terminate Buffer on end of frame
5279 * <8> 0 Bus Width = 16bits
5280 * <7..0> ? status Bits (Read Only so write as 0)
5282 * 1111 0010 0000 0000 = 0xf200
5285 usc_OutDmaReg( info, TDMR, 0xf200 );
5288 /* DMA Interrupt Control Register (DICR)
5290 * <15> 1 DMA Interrupt Enable
5291 * <14> 0 1 = Disable IEO from USC
5292 * <13> 0 1 = Don't provide vector during IntAck
5293 * <12> 1 1 = Include status in Vector
5294 * <10..2> 0 reserved, Must be 0s
5295 * <1> 0 1 = Rx DMA Interrupt Enabled
5296 * <0> 0 1 = Tx DMA Interrupt Enabled
5298 * 1001 0000 0000 0000 = 0x9000
5301 usc_OutDmaReg( info, DICR, 0x9000 );
5303 usc_InDmaReg( info, RDMR ); /* clear pending receive DMA IRQ bits */
5304 usc_InDmaReg( info, TDMR ); /* clear pending transmit DMA IRQ bits */
5305 usc_OutDmaReg( info, CDIR, 0x0303 ); /* clear IUS and Pending for Tx and Rx */
5307 /* Channel Control Register (CCR)
5309 * <15..14> 10 Use 32-bit Tx Control Blocks (TCBs)
5310 * <13> 0 Trigger Tx on SW Command Disabled
5311 * <12> 0 Flag Preamble Disabled
5312 * <11..10> 00 Preamble Length
5313 * <9..8> 00 Preamble Pattern
5314 * <7..6> 10 Use 32-bit Rx status Blocks (RSBs)
5315 * <5> 0 Trigger Rx on SW Command Disabled
5316 * <4..0> 0 reserved
5318 * 1000 0000 1000 0000 = 0x8080
5321 RegValue = 0x8080;
5323 switch ( info->params.preamble_length ) {
5324 case HDLC_PREAMBLE_LENGTH_16BITS: RegValue |= BIT10; break;
5325 case HDLC_PREAMBLE_LENGTH_32BITS: RegValue |= BIT11; break;
5326 case HDLC_PREAMBLE_LENGTH_64BITS: RegValue |= BIT11 + BIT10; break;
5329 switch ( info->params.preamble ) {
5330 case HDLC_PREAMBLE_PATTERN_FLAGS: RegValue |= BIT8 + BIT12; break;
5331 case HDLC_PREAMBLE_PATTERN_ONES: RegValue |= BIT8; break;
5332 case HDLC_PREAMBLE_PATTERN_10: RegValue |= BIT9; break;
5333 case HDLC_PREAMBLE_PATTERN_01: RegValue |= BIT9 + BIT8; break;
5336 usc_OutReg( info, CCR, RegValue );
5340 * Burst/Dwell Control Register
5342 * <15..8> 0x20 Maximum number of transfers per bus grant
5343 * <7..0> 0x00 Maximum number of clock cycles per bus grant
5346 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5347 /* don't limit bus occupancy on PCI adapter */
5348 usc_OutDmaReg( info, BDCR, 0x0000 );
5350 else
5351 usc_OutDmaReg( info, BDCR, 0x2000 );
5353 usc_stop_transmitter(info);
5354 usc_stop_receiver(info);
5356 } /* end of usc_set_sdlc_mode() */
5358 /* usc_enable_loopback()
5360 * Set the 16C32 for internal loopback mode.
5361 * The TxCLK and RxCLK signals are generated from the BRG0 and
5362 * the TxD is looped back to the RxD internally.
5364 * Arguments: info pointer to device instance data
5365 * enable 1 = enable loopback, 0 = disable
5366 * Return Value: None
5368 void usc_enable_loopback(struct mgsl_struct *info, int enable)
5370 if (enable) {
5371 /* blank external TXD output */
5372 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) | (BIT7+BIT6));
5374 /* Clock mode Control Register (CMCR)
5376 * <15..14> 00 counter 1 Disabled
5377 * <13..12> 00 counter 0 Disabled
5378 * <11..10> 11 BRG1 Input is TxC Pin
5379 * <9..8> 11 BRG0 Input is TxC Pin
5380 * <7..6> 01 DPLL Input is BRG1 Output
5381 * <5..3> 100 TxCLK comes from BRG0
5382 * <2..0> 100 RxCLK comes from BRG0
5384 * 0000 1111 0110 0100 = 0x0f64
5387 usc_OutReg( info, CMCR, 0x0f64 );
5389 /* Write 16-bit Time Constant for BRG0 */
5390 /* use clock speed if available, otherwise use 8 for diagnostics */
5391 if (info->params.clock_speed) {
5392 if (info->bus_type == MGSL_BUS_TYPE_PCI)
5393 usc_OutReg(info, TC0R, (u16)((11059200/info->params.clock_speed)-1));
5394 else
5395 usc_OutReg(info, TC0R, (u16)((14745600/info->params.clock_speed)-1));
5396 } else
5397 usc_OutReg(info, TC0R, (u16)8);
5399 /* Hardware Configuration Register (HCR) Clear Bit 1, BRG0
5400 mode = Continuous Set Bit 0 to enable BRG0. */
5401 usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
5403 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
5404 usc_OutReg(info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004));
5406 /* set Internal Data loopback mode */
5407 info->loopback_bits = 0x300;
5408 outw( 0x0300, info->io_base + CCAR );
5409 } else {
5410 /* enable external TXD output */
5411 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) & ~(BIT7+BIT6));
5413 /* clear Internal Data loopback mode */
5414 info->loopback_bits = 0;
5415 outw( 0,info->io_base + CCAR );
5418 } /* end of usc_enable_loopback() */
5420 /* usc_enable_aux_clock()
5422 * Enabled the AUX clock output at the specified frequency.
5424 * Arguments:
5426 * info pointer to device extension
5427 * data_rate data rate of clock in bits per second
5428 * A data rate of 0 disables the AUX clock.
5430 * Return Value: None
5432 void usc_enable_aux_clock( struct mgsl_struct *info, u32 data_rate )
5434 u32 XtalSpeed;
5435 u16 Tc;
5437 if ( data_rate ) {
5438 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
5439 XtalSpeed = 11059200;
5440 else
5441 XtalSpeed = 14745600;
5444 /* Tc = (Xtal/Speed) - 1 */
5445 /* If twice the remainder of (Xtal/Speed) is greater than Speed */
5446 /* then rounding up gives a more precise time constant. Instead */
5447 /* of rounding up and then subtracting 1 we just don't subtract */
5448 /* the one in this case. */
5451 Tc = (u16)(XtalSpeed/data_rate);
5452 if ( !(((XtalSpeed % data_rate) * 2) / data_rate) )
5453 Tc--;
5455 /* Write 16-bit Time Constant for BRG0 */
5456 usc_OutReg( info, TC0R, Tc );
5459 * Hardware Configuration Register (HCR)
5460 * Clear Bit 1, BRG0 mode = Continuous
5461 * Set Bit 0 to enable BRG0.
5464 usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
5466 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
5467 usc_OutReg( info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
5468 } else {
5469 /* data rate == 0 so turn off BRG0 */
5470 usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
5473 } /* end of usc_enable_aux_clock() */
5475 /* usc_stop_receiver()
5477 * Disable USC receiver
5479 * Arguments: info pointer to device instance data
5480 * Return Value: None
5482 void usc_stop_receiver( struct mgsl_struct *info )
5484 if (debug_level >= DEBUG_LEVEL_ISR)
5485 printk("%s(%d):usc_stop_receiver(%s)\n",
5486 __FILE__,__LINE__, info->device_name );
5488 /* Disable receive DMA channel. */
5489 /* This also disables receive DMA channel interrupts */
5490 usc_DmaCmd( info, DmaCmd_ResetRxChannel );
5492 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5493 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
5494 usc_DisableInterrupts( info, RECEIVE_DATA + RECEIVE_STATUS );
5496 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
5498 /* This empties the receive FIFO and loads the RCC with RCLR */
5499 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5500 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5502 info->rx_enabled = 0;
5503 info->rx_overflow = 0;
5505 } /* end of stop_receiver() */
5507 /* usc_start_receiver()
5509 * Enable the USC receiver
5511 * Arguments: info pointer to device instance data
5512 * Return Value: None
5514 void usc_start_receiver( struct mgsl_struct *info )
5516 u32 phys_addr;
5518 if (debug_level >= DEBUG_LEVEL_ISR)
5519 printk("%s(%d):usc_start_receiver(%s)\n",
5520 __FILE__,__LINE__, info->device_name );
5522 mgsl_reset_rx_dma_buffers( info );
5523 usc_stop_receiver( info );
5525 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5526 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5528 if ( info->params.mode == MGSL_MODE_HDLC ) {
5529 /* DMA mode Transfers */
5530 /* Program the DMA controller. */
5531 /* Enable the DMA controller end of buffer interrupt. */
5533 /* program 16C32 with physical address of 1st DMA buffer entry */
5534 phys_addr = info->rx_buffer_list[0].phys_entry;
5535 usc_OutDmaReg( info, NRARL, (u16)phys_addr );
5536 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
5538 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5539 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
5540 usc_EnableInterrupts( info, RECEIVE_STATUS );
5542 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
5543 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
5545 usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 );
5546 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
5547 usc_DmaCmd( info, DmaCmd_InitRxChannel );
5548 if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
5549 usc_EnableReceiver(info,ENABLE_AUTO_DCD);
5550 else
5551 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5552 } else {
5553 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
5554 usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS);
5555 usc_EnableInterrupts(info, RECEIVE_DATA);
5557 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5558 usc_RCmd( info, RCmd_EnterHuntmode );
5560 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5563 usc_OutReg( info, CCSR, 0x1020 );
5565 info->rx_enabled = 1;
5567 } /* end of usc_start_receiver() */
5569 /* usc_start_transmitter()
5571 * Enable the USC transmitter and send a transmit frame if
5572 * one is loaded in the DMA buffers.
5574 * Arguments: info pointer to device instance data
5575 * Return Value: None
5577 void usc_start_transmitter( struct mgsl_struct *info )
5579 u32 phys_addr;
5580 unsigned int FrameSize;
5582 if (debug_level >= DEBUG_LEVEL_ISR)
5583 printk("%s(%d):usc_start_transmitter(%s)\n",
5584 __FILE__,__LINE__, info->device_name );
5586 if ( info->xmit_cnt ) {
5588 /* If auto RTS enabled and RTS is inactive, then assert */
5589 /* RTS and set a flag indicating that the driver should */
5590 /* negate RTS when the transmission completes. */
5592 info->drop_rts_on_tx_done = 0;
5594 if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
5595 usc_get_serial_signals( info );
5596 if ( !(info->serial_signals & SerialSignal_RTS) ) {
5597 info->serial_signals |= SerialSignal_RTS;
5598 usc_set_serial_signals( info );
5599 info->drop_rts_on_tx_done = 1;
5604 if ( info->params.mode == MGSL_MODE_ASYNC ) {
5605 if ( !info->tx_active ) {
5606 usc_UnlatchTxstatusBits(info, TXSTATUS_ALL);
5607 usc_ClearIrqPendingBits(info, TRANSMIT_STATUS + TRANSMIT_DATA);
5608 usc_EnableInterrupts(info, TRANSMIT_DATA);
5609 usc_load_txfifo(info);
5611 } else {
5612 /* Disable transmit DMA controller while programming. */
5613 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
5615 /* Transmit DMA buffer is loaded, so program USC */
5616 /* to send the frame contained in the buffers. */
5619 FrameSize = info->tx_buffer_list[0].rcc;
5621 /* Program the Transmit Character Length Register (TCLR) */
5622 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
5623 usc_OutReg( info, TCLR, (u16)FrameSize );
5625 usc_RTCmd( info, RTCmd_PurgeTxFifo );
5627 /* Program the address of the 1st DMA Buffer Entry in linked list */
5628 phys_addr = info->tx_buffer_list[0].phys_entry;
5629 usc_OutDmaReg( info, NTARL, (u16)phys_addr );
5630 usc_OutDmaReg( info, NTARU, (u16)(phys_addr >> 16) );
5632 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
5633 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
5634 usc_EnableInterrupts( info, TRANSMIT_STATUS );
5636 /* Initialize Transmit DMA Channel */
5637 usc_DmaCmd( info, DmaCmd_InitTxChannel );
5639 usc_TCmd( info, TCmd_SendFrame );
5641 info->tx_timer.expires = jiffies + jiffies_from_ms(5000);
5642 add_timer(&info->tx_timer);
5644 info->tx_active = 1;
5647 if ( !info->tx_enabled ) {
5648 info->tx_enabled = 1;
5649 if ( info->params.flags & HDLC_FLAG_AUTO_CTS )
5650 usc_EnableTransmitter(info,ENABLE_AUTO_CTS);
5651 else
5652 usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
5655 } /* end of usc_start_transmitter() */
5657 /* usc_stop_transmitter()
5659 * Stops the transmitter and DMA
5661 * Arguments: info pointer to device isntance data
5662 * Return Value: None
5664 void usc_stop_transmitter( struct mgsl_struct *info )
5666 if (debug_level >= DEBUG_LEVEL_ISR)
5667 printk("%s(%d):usc_stop_transmitter(%s)\n",
5668 __FILE__,__LINE__, info->device_name );
5670 del_timer(&info->tx_timer);
5672 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
5673 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA );
5674 usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA );
5676 usc_EnableTransmitter(info,DISABLE_UNCONDITIONAL);
5677 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
5678 usc_RTCmd( info, RTCmd_PurgeTxFifo );
5680 info->tx_enabled = 0;
5681 info->tx_active = 0;
5683 } /* end of usc_stop_transmitter() */
5685 /* usc_load_txfifo()
5687 * Fill the transmit FIFO until the FIFO is full or
5688 * there is no more data to load.
5690 * Arguments: info pointer to device extension (instance data)
5691 * Return Value: None
5693 void usc_load_txfifo( struct mgsl_struct *info )
5695 int Fifocount;
5696 u8 TwoBytes[2];
5698 if ( !info->xmit_cnt && !info->x_char )
5699 return;
5701 /* Select transmit FIFO status readback in TICR */
5702 usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
5704 /* load the Transmit FIFO until FIFOs full or all data sent */
5706 while( (Fifocount = usc_InReg(info, TICR) >> 8) && info->xmit_cnt ) {
5707 /* there is more space in the transmit FIFO and */
5708 /* there is more data in transmit buffer */
5710 if ( (info->xmit_cnt > 1) && (Fifocount > 1) && !info->x_char ) {
5711 /* write a 16-bit word from transmit buffer to 16C32 */
5713 TwoBytes[0] = info->xmit_buf[info->xmit_tail++];
5714 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5715 TwoBytes[1] = info->xmit_buf[info->xmit_tail++];
5716 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5718 outw( *((u16 *)TwoBytes), info->io_base + DATAREG);
5720 info->xmit_cnt -= 2;
5721 info->icount.tx += 2;
5722 } else {
5723 /* only 1 byte left to transmit or 1 FIFO slot left */
5725 outw( (inw( info->io_base + CCAR) & 0x0780) | (TDR+LSBONLY),
5726 info->io_base + CCAR );
5728 if (info->x_char) {
5729 /* transmit pending high priority char */
5730 outw( info->x_char,info->io_base + CCAR );
5731 info->x_char = 0;
5732 } else {
5733 outw( info->xmit_buf[info->xmit_tail++],info->io_base + CCAR );
5734 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5735 info->xmit_cnt--;
5737 info->icount.tx++;
5741 } /* end of usc_load_txfifo() */
5743 /* usc_reset()
5745 * Reset the adapter to a known state and prepare it for further use.
5747 * Arguments: info pointer to device instance data
5748 * Return Value: None
5750 void usc_reset( struct mgsl_struct *info )
5752 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5753 int i;
5754 volatile u32 readval;
5756 /* Set BIT30 of Misc Control Register */
5757 /* (Local Control Register 0x50) to force reset of USC. */
5759 u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
5760 u32 *LCR0BRDR = (u32 *)(info->lcr_base + 0x28);
5762 info->misc_ctrl_value |= BIT30;
5763 *MiscCtrl = info->misc_ctrl_value;
5766 * Force at least 170ns delay before clearing
5767 * reset bit. Each read from LCR takes at least
5768 * 30ns so 10 times for 300ns to be safe.
5770 for(i=0;i<10;i++)
5771 readval = *MiscCtrl;
5773 info->misc_ctrl_value &= ~BIT30;
5774 *MiscCtrl = info->misc_ctrl_value;
5776 *LCR0BRDR = BUS_DESCRIPTOR(
5777 1, // Write Strobe Hold (0-3)
5778 2, // Write Strobe Delay (0-3)
5779 2, // Read Strobe Delay (0-3)
5780 0, // NWDD (Write data-data) (0-3)
5781 4, // NWAD (Write Addr-data) (0-31)
5782 0, // NXDA (Read/Write Data-Addr) (0-3)
5783 0, // NRDD (Read Data-Data) (0-3)
5784 5 // NRAD (Read Addr-Data) (0-31)
5786 } else {
5787 /* do HW reset */
5788 outb( 0,info->io_base + 8 );
5791 info->mbre_bit = 0;
5792 info->loopback_bits = 0;
5793 info->usc_idle_mode = 0;
5796 * Program the Bus Configuration Register (BCR)
5798 * <15> 0 Don't use seperate address
5799 * <14..6> 0 reserved
5800 * <5..4> 00 IAckmode = Default, don't care
5801 * <3> 1 Bus Request Totem Pole output
5802 * <2> 1 Use 16 Bit data bus
5803 * <1> 0 IRQ Totem Pole output
5804 * <0> 0 Don't Shift Right Addr
5806 * 0000 0000 0000 1100 = 0x000c
5808 * By writing to io_base + SDPIN the Wait/Ack pin is
5809 * programmed to work as a Wait pin.
5812 outw( 0x000c,info->io_base + SDPIN );
5815 outw( 0,info->io_base );
5816 outw( 0,info->io_base + CCAR );
5818 /* select little endian byte ordering */
5819 usc_RTCmd( info, RTCmd_SelectLittleEndian );
5822 /* Port Control Register (PCR)
5824 * <15..14> 11 Port 7 is Output (~DMAEN, Bit 14 : 0 = Enabled)
5825 * <13..12> 11 Port 6 is Output (~INTEN, Bit 12 : 0 = Enabled)
5826 * <11..10> 00 Port 5 is Input (No Connect, Don't Care)
5827 * <9..8> 00 Port 4 is Input (No Connect, Don't Care)
5828 * <7..6> 11 Port 3 is Output (~RTS, Bit 6 : 0 = Enabled )
5829 * <5..4> 11 Port 2 is Output (~DTR, Bit 4 : 0 = Enabled )
5830 * <3..2> 01 Port 1 is Input (Dedicated RxC)
5831 * <1..0> 01 Port 0 is Input (Dedicated TxC)
5833 * 1111 0000 1111 0101 = 0xf0f5
5836 usc_OutReg( info, PCR, 0xf0f5 );
5840 * Input/Output Control Register
5842 * <15..14> 00 CTS is active low input
5843 * <13..12> 00 DCD is active low input
5844 * <11..10> 00 TxREQ pin is input (DSR)
5845 * <9..8> 00 RxREQ pin is input (RI)
5846 * <7..6> 00 TxD is output (Transmit Data)
5847 * <5..3> 000 TxC Pin in Input (14.7456MHz Clock)
5848 * <2..0> 100 RxC is Output (drive with BRG0)
5850 * 0000 0000 0000 0100 = 0x0004
5853 usc_OutReg( info, IOCR, 0x0004 );
5855 } /* end of usc_reset() */
5857 /* usc_set_async_mode()
5859 * Program adapter for asynchronous communications.
5861 * Arguments: info pointer to device instance data
5862 * Return Value: None
5864 void usc_set_async_mode( struct mgsl_struct *info )
5866 u16 RegValue;
5868 /* disable interrupts while programming USC */
5869 usc_DisableMasterIrqBit( info );
5871 outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
5872 usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
5874 usc_loopback_frame( info );
5876 /* Channel mode Register (CMR)
5878 * <15..14> 00 Tx Sub modes, 00 = 1 Stop Bit
5879 * <13..12> 00 00 = 16X Clock
5880 * <11..8> 0000 Transmitter mode = Asynchronous
5881 * <7..6> 00 reserved?
5882 * <5..4> 00 Rx Sub modes, 00 = 16X Clock
5883 * <3..0> 0000 Receiver mode = Asynchronous
5885 * 0000 0000 0000 0000 = 0x0
5888 RegValue = 0;
5889 if ( info->params.stop_bits != 1 )
5890 RegValue |= BIT14;
5891 usc_OutReg( info, CMR, RegValue );
5894 /* Receiver mode Register (RMR)
5896 * <15..13> 000 encoding = None
5897 * <12..08> 00000 reserved (Sync Only)
5898 * <7..6> 00 Even parity
5899 * <5> 0 parity disabled
5900 * <4..2> 000 Receive Char Length = 8 bits
5901 * <1..0> 00 Disable Receiver
5903 * 0000 0000 0000 0000 = 0x0
5906 RegValue = 0;
5908 if ( info->params.data_bits != 8 )
5909 RegValue |= BIT4+BIT3+BIT2;
5911 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5912 RegValue |= BIT5;
5913 if ( info->params.parity != ASYNC_PARITY_ODD )
5914 RegValue |= BIT6;
5917 usc_OutReg( info, RMR, RegValue );
5920 /* Set IRQ trigger level */
5922 usc_RCmd( info, RCmd_SelectRicrIntLevel );
5925 /* Receive Interrupt Control Register (RICR)
5927 * <15..8> ? RxFIFO IRQ Request Level
5929 * Note: For async mode the receive FIFO level must be set
5930 * to 0 to aviod the situation where the FIFO contains fewer bytes
5931 * than the trigger level and no more data is expected.
5933 * <7> 0 Exited Hunt IA (Interrupt Arm)
5934 * <6> 0 Idle Received IA
5935 * <5> 0 Break/Abort IA
5936 * <4> 0 Rx Bound IA
5937 * <3> 0 Queued status reflects oldest byte in FIFO
5938 * <2> 0 Abort/PE IA
5939 * <1> 0 Rx Overrun IA
5940 * <0> 0 Select TC0 value for readback
5942 * 0000 0000 0100 0000 = 0x0000 + (FIFOLEVEL in MSB)
5945 usc_OutReg( info, RICR, 0x0000 );
5947 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5948 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
5951 /* Transmit mode Register (TMR)
5953 * <15..13> 000 encoding = None
5954 * <12..08> 00000 reserved (Sync Only)
5955 * <7..6> 00 Transmit parity Even
5956 * <5> 0 Transmit parity Disabled
5957 * <4..2> 000 Tx Char Length = 8 bits
5958 * <1..0> 00 Disable Transmitter
5960 * 0000 0000 0000 0000 = 0x0
5963 RegValue = 0;
5965 if ( info->params.data_bits != 8 )
5966 RegValue |= BIT4+BIT3+BIT2;
5968 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5969 RegValue |= BIT5;
5970 if ( info->params.parity != ASYNC_PARITY_ODD )
5971 RegValue |= BIT6;
5974 usc_OutReg( info, TMR, RegValue );
5976 usc_set_txidle( info );
5979 /* Set IRQ trigger level */
5981 usc_TCmd( info, TCmd_SelectTicrIntLevel );
5984 /* Transmit Interrupt Control Register (TICR)
5986 * <15..8> ? Transmit FIFO IRQ Level
5987 * <7> 0 Present IA (Interrupt Arm)
5988 * <6> 1 Idle Sent IA
5989 * <5> 0 Abort Sent IA
5990 * <4> 0 EOF/EOM Sent IA
5991 * <3> 0 CRC Sent IA
5992 * <2> 0 1 = Wait for SW Trigger to Start Frame
5993 * <1> 0 Tx Underrun IA
5994 * <0> 0 TC0 constant on read back
5996 * 0000 0000 0100 0000 = 0x0040
5999 usc_OutReg( info, TICR, 0x1f40 );
6001 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
6002 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
6004 usc_enable_async_clock( info, info->params.data_rate );
6007 /* Channel Control/status Register (CCSR)
6009 * <15> X RCC FIFO Overflow status (RO)
6010 * <14> X RCC FIFO Not Empty status (RO)
6011 * <13> 0 1 = Clear RCC FIFO (WO)
6012 * <12> X DPLL in Sync status (RO)
6013 * <11> X DPLL 2 Missed Clocks status (RO)
6014 * <10> X DPLL 1 Missed Clock status (RO)
6015 * <9..8> 00 DPLL Resync on rising and falling edges (RW)
6016 * <7> X SDLC Loop On status (RO)
6017 * <6> X SDLC Loop Send status (RO)
6018 * <5> 1 Bypass counters for TxClk and RxClk (RW)
6019 * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
6020 * <1..0> 00 reserved
6022 * 0000 0000 0010 0000 = 0x0020
6025 usc_OutReg( info, CCSR, 0x0020 );
6027 usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA +
6028 RECEIVE_DATA + RECEIVE_STATUS );
6030 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA +
6031 RECEIVE_DATA + RECEIVE_STATUS );
6033 usc_EnableMasterIrqBit( info );
6035 /* Enable INTEN (Port 6, Bit12) */
6036 /* This connects the IRQ request signal to the ISA bus */
6037 /* on the ISA adapter. This has no effect for the PCI adapter */
6038 usc_OutReg( info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12) );
6040 } /* end of usc_set_async_mode() */
6042 /* usc_loopback_frame()
6044 * Loop back a small (2 byte) dummy SDLC frame.
6045 * Interrupts and DMA are NOT used. The purpose of this is to
6046 * clear any 'stale' status info left over from running in async mode.
6048 * The 16C32 shows the strange behaviour of marking the 1st
6049 * received SDLC frame with a CRC error even when there is no
6050 * CRC error. To get around this a small dummy from of 2 bytes
6051 * is looped back when switching from async to sync mode.
6053 * Arguments: info pointer to device instance data
6054 * Return Value: None
6056 void usc_loopback_frame( struct mgsl_struct *info )
6058 int i;
6060 usc_DisableMasterIrqBit( info );
6062 usc_set_sdlc_mode( info );
6063 usc_enable_loopback( info, 1 );
6065 /* Write 16-bit Time Constant for BRG0 */
6066 usc_OutReg( info, TC0R, 0 );
6068 /* Channel Control Register (CCR)
6070 * <15..14> 00 Don't use 32-bit Tx Control Blocks (TCBs)
6071 * <13> 0 Trigger Tx on SW Command Disabled
6072 * <12> 0 Flag Preamble Disabled
6073 * <11..10> 00 Preamble Length = 8-Bits
6074 * <9..8> 01 Preamble Pattern = flags
6075 * <7..6> 10 Don't use 32-bit Rx status Blocks (RSBs)
6076 * <5> 0 Trigger Rx on SW Command Disabled
6077 * <4..0> 0 reserved
6079 * 0000 0001 0000 0000 = 0x0100
6082 usc_OutReg( info, CCR, 0x0100 );
6084 /* SETUP RECEIVER */
6085 usc_RTCmd( info, RTCmd_PurgeRxFifo );
6086 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
6088 /* SETUP TRANSMITTER */
6089 /* Program the Transmit Character Length Register (TCLR) */
6090 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
6091 usc_OutReg( info, TCLR, 2 );
6092 usc_RTCmd( info, RTCmd_PurgeTxFifo );
6094 /* unlatch Tx status bits, and start transmit channel. */
6095 usc_UnlatchTxstatusBits(info,TXSTATUS_ALL);
6096 outw(0,info->io_base + DATAREG);
6098 /* ENABLE TRANSMITTER */
6099 usc_TCmd( info, TCmd_SendFrame );
6100 usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
6102 /* WAIT FOR RECEIVE COMPLETE */
6103 for (i=0 ; i<1000 ; i++)
6104 if (usc_InReg( info, RCSR ) & (BIT8 + BIT4 + BIT3 + BIT1))
6105 break;
6107 /* clear Internal Data loopback mode */
6108 usc_enable_loopback(info, 0);
6110 usc_EnableMasterIrqBit(info);
6112 } /* end of usc_loopback_frame() */
6114 /* usc_set_sync_mode() Programs the USC for SDLC communications.
6116 * Arguments: info pointer to adapter info structure
6117 * Return Value: None
6119 void usc_set_sync_mode( struct mgsl_struct *info )
6121 usc_loopback_frame( info );
6122 usc_set_sdlc_mode( info );
6124 /* Enable INTEN (Port 6, Bit12) */
6125 /* This connects the IRQ request signal to the ISA bus */
6126 /* on the ISA adapter. This has no effect for the PCI adapter */
6127 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12));
6129 usc_enable_aux_clock(info, info->params.clock_speed);
6131 if (info->params.loopback)
6132 usc_enable_loopback(info,1);
6134 } /* end of mgsl_set_sync_mode() */
6136 /* usc_set_txidle() Set the HDLC idle mode for the transmitter.
6138 * Arguments: info pointer to device instance data
6139 * Return Value: None
6141 void usc_set_txidle( struct mgsl_struct *info )
6143 u16 usc_idle_mode = IDLEMODE_FLAGS;
6145 /* Map API idle mode to USC register bits */
6147 switch( info->idle_mode ){
6148 case HDLC_TXIDLE_FLAGS: usc_idle_mode = IDLEMODE_FLAGS; break;
6149 case HDLC_TXIDLE_ALT_ZEROS_ONES: usc_idle_mode = IDLEMODE_ALT_ONE_ZERO; break;
6150 case HDLC_TXIDLE_ZEROS: usc_idle_mode = IDLEMODE_ZERO; break;
6151 case HDLC_TXIDLE_ONES: usc_idle_mode = IDLEMODE_ONE; break;
6152 case HDLC_TXIDLE_ALT_MARK_SPACE: usc_idle_mode = IDLEMODE_ALT_MARK_SPACE; break;
6153 case HDLC_TXIDLE_SPACE: usc_idle_mode = IDLEMODE_SPACE; break;
6154 case HDLC_TXIDLE_MARK: usc_idle_mode = IDLEMODE_MARK; break;
6157 info->usc_idle_mode = usc_idle_mode;
6158 usc_OutReg(info, TCSR, usc_idle_mode);
6160 } /* end of usc_set_txidle() */
6162 /* usc_get_serial_signals()
6164 * Query the adapter for the state of the V24 status (input) signals.
6166 * Arguments: info pointer to device instance data
6167 * Return Value: None
6169 void usc_get_serial_signals( struct mgsl_struct *info )
6171 u16 status;
6173 /* clear all serial signals except DTR and RTS */
6174 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
6176 /* Read the Misc Interrupt status Register (MISR) to get */
6177 /* the V24 status signals. */
6179 status = usc_InReg( info, MISR );
6181 /* set serial signal bits to reflect MISR */
6183 if ( status & MISCSTATUS_CTS )
6184 info->serial_signals |= SerialSignal_CTS;
6186 if ( status & MISCSTATUS_DCD )
6187 info->serial_signals |= SerialSignal_DCD;
6189 if ( status & MISCSTATUS_RI )
6190 info->serial_signals |= SerialSignal_RI;
6192 if ( status & MISCSTATUS_DSR )
6193 info->serial_signals |= SerialSignal_DSR;
6195 } /* end of usc_get_serial_signals() */
6197 /* usc_set_serial_signals()
6199 * Set the state of DTR and RTS based on contents of
6200 * serial_signals member of device extension.
6202 * Arguments: info pointer to device instance data
6203 * Return Value: None
6205 void usc_set_serial_signals( struct mgsl_struct *info )
6207 u16 Control;
6208 unsigned char V24Out = info->serial_signals;
6210 /* get the current value of the Port Control Register (PCR) */
6212 Control = usc_InReg( info, PCR );
6214 if ( V24Out & SerialSignal_RTS )
6215 Control &= ~(BIT6);
6216 else
6217 Control |= BIT6;
6219 if ( V24Out & SerialSignal_DTR )
6220 Control &= ~(BIT4);
6221 else
6222 Control |= BIT4;
6224 usc_OutReg( info, PCR, Control );
6226 } /* end of usc_set_serial_signals() */
6228 /* usc_enable_async_clock()
6230 * Enable the async clock at the specified frequency.
6232 * Arguments: info pointer to device instance data
6233 * data_rate data rate of clock in bps
6234 * 0 disables the AUX clock.
6235 * Return Value: None
6237 void usc_enable_async_clock( struct mgsl_struct *info, u32 data_rate )
6239 if ( data_rate ) {
6241 * Clock mode Control Register (CMCR)
6243 * <15..14> 00 counter 1 Disabled
6244 * <13..12> 00 counter 0 Disabled
6245 * <11..10> 11 BRG1 Input is TxC Pin
6246 * <9..8> 11 BRG0 Input is TxC Pin
6247 * <7..6> 01 DPLL Input is BRG1 Output
6248 * <5..3> 100 TxCLK comes from BRG0
6249 * <2..0> 100 RxCLK comes from BRG0
6251 * 0000 1111 0110 0100 = 0x0f64
6254 usc_OutReg( info, CMCR, 0x0f64 );
6258 * Write 16-bit Time Constant for BRG0
6259 * Time Constant = (ClkSpeed / data_rate) - 1
6260 * ClkSpeed = 921600 (ISA), 691200 (PCI)
6263 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
6264 usc_OutReg( info, TC0R, (u16)((691200/data_rate) - 1) );
6265 else
6266 usc_OutReg( info, TC0R, (u16)((921600/data_rate) - 1) );
6270 * Hardware Configuration Register (HCR)
6271 * Clear Bit 1, BRG0 mode = Continuous
6272 * Set Bit 0 to enable BRG0.
6275 usc_OutReg( info, HCR,
6276 (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
6279 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
6281 usc_OutReg( info, IOCR,
6282 (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
6283 } else {
6284 /* data rate == 0 so turn off BRG0 */
6285 usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
6288 } /* end of usc_enable_async_clock() */
6291 * Buffer Structures:
6293 * Normal memory access uses virtual addresses that can make discontiguous
6294 * physical memory pages appear to be contiguous in the virtual address
6295 * space (the processors memory mapping handles the conversions).
6297 * DMA transfers require physically contiguous memory. This is because
6298 * the DMA system controller and DMA bus masters deal with memory using
6299 * only physical addresses.
6301 * This causes a problem under Windows NT when large DMA buffers are
6302 * needed. Fragmentation of the nonpaged pool prevents allocations of
6303 * physically contiguous buffers larger than the PAGE_SIZE.
6305 * However the 16C32 supports Bus Master Scatter/Gather DMA which
6306 * allows DMA transfers to physically discontiguous buffers. Information
6307 * about each data transfer buffer is contained in a memory structure
6308 * called a 'buffer entry'. A list of buffer entries is maintained
6309 * to track and control the use of the data transfer buffers.
6311 * To support this strategy we will allocate sufficient PAGE_SIZE
6312 * contiguous memory buffers to allow for the total required buffer
6313 * space.
6315 * The 16C32 accesses the list of buffer entries using Bus Master
6316 * DMA. Control information is read from the buffer entries by the
6317 * 16C32 to control data transfers. status information is written to
6318 * the buffer entries by the 16C32 to indicate the status of completed
6319 * transfers.
6321 * The CPU writes control information to the buffer entries to control
6322 * the 16C32 and reads status information from the buffer entries to
6323 * determine information about received and transmitted frames.
6325 * Because the CPU and 16C32 (adapter) both need simultaneous access
6326 * to the buffer entries, the buffer entry memory is allocated with
6327 * HalAllocateCommonBuffer(). This restricts the size of the buffer
6328 * entry list to PAGE_SIZE.
6330 * The actual data buffers on the other hand will only be accessed
6331 * by the CPU or the adapter but not by both simultaneously. This allows
6332 * Scatter/Gather packet based DMA procedures for using physically
6333 * discontiguous pages.
6337 * mgsl_reset_rx_dma_buffers()
6339 * Set the count for all receive buffers to DMABUFFERSIZE
6340 * and set the current buffer to the first buffer. This effectively
6341 * makes all buffers free and discards any data in buffers.
6343 * Arguments: info pointer to device instance data
6344 * Return Value: None
6346 void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info )
6348 unsigned int i;
6350 for ( i = 0; i < info->rx_buffer_count; i++ ) {
6351 *((unsigned long *)&(info->rx_buffer_list[i].count)) = DMABUFFERSIZE;
6352 // info->rx_buffer_list[i].count = DMABUFFERSIZE;
6353 // info->rx_buffer_list[i].status = 0;
6356 info->current_rx_buffer = 0;
6358 } /* end of mgsl_reset_rx_dma_buffers() */
6361 * mgsl_free_rx_frame_buffers()
6363 * Free the receive buffers used by a received SDLC
6364 * frame such that the buffers can be reused.
6366 * Arguments:
6368 * info pointer to device instance data
6369 * StartIndex index of 1st receive buffer of frame
6370 * EndIndex index of last receive buffer of frame
6372 * Return Value: None
6374 void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex )
6376 int Done = 0;
6377 DMABUFFERENTRY *pBufEntry;
6378 unsigned int Index;
6380 /* Starting with 1st buffer entry of the frame clear the status */
6381 /* field and set the count field to DMA Buffer Size. */
6383 Index = StartIndex;
6385 while( !Done ) {
6386 pBufEntry = &(info->rx_buffer_list[Index]);
6388 if ( Index == EndIndex ) {
6389 /* This is the last buffer of the frame! */
6390 Done = 1;
6393 /* reset current buffer for reuse */
6394 // pBufEntry->status = 0;
6395 // pBufEntry->count = DMABUFFERSIZE;
6396 *((unsigned long *)&(pBufEntry->count)) = DMABUFFERSIZE;
6398 /* advance to next buffer entry in linked list */
6399 Index++;
6400 if ( Index == info->rx_buffer_count )
6401 Index = 0;
6404 /* set current buffer to next buffer after last buffer of frame */
6405 info->current_rx_buffer = Index;
6407 } /* end of free_rx_frame_buffers() */
6409 /* mgsl_get_rx_frame()
6411 * This function attempts to return a received SDLC frame from the
6412 * receive DMA buffers. Only frames received without errors are returned.
6414 * Arguments: info pointer to device extension
6415 * Return Value: 1 if frame returned, otherwise 0
6417 int mgsl_get_rx_frame(struct mgsl_struct *info)
6419 unsigned int StartIndex, EndIndex; /* index of 1st and last buffers of Rx frame */
6420 unsigned short status;
6421 DMABUFFERENTRY *pBufEntry;
6422 unsigned int framesize;
6423 int ReturnCode = 0;
6424 unsigned long flags;
6425 struct tty_struct *tty = info->tty;
6428 * current_rx_buffer points to the 1st buffer of the next available
6429 * receive frame. To find the last buffer of the frame look for
6430 * a non-zero status field in the buffer entries. (The status
6431 * field is set by the 16C32 after completing a receive frame.
6434 StartIndex = EndIndex = info->current_rx_buffer;
6436 while( !info->rx_buffer_list[EndIndex].status ) {
6438 * If the count field of the buffer entry is non-zero then
6439 * this buffer has not been used. (The 16C32 clears the count
6440 * field when it starts using the buffer.) If an unused buffer
6441 * is encountered then there are no frames available.
6444 if ( info->rx_buffer_list[EndIndex].count )
6445 goto Cleanup;
6447 /* advance to next buffer entry in linked list */
6448 EndIndex++;
6449 if ( EndIndex == info->rx_buffer_count )
6450 EndIndex = 0;
6452 /* if entire list searched then no frame available */
6453 if ( EndIndex == StartIndex ) {
6454 /* If this occurs then something bad happened,
6455 * all buffers have been 'used' but none mark
6456 * the end of a frame. Reset buffers and receiver.
6459 if ( info->rx_enabled ){
6460 spin_lock_irqsave(&info->irq_spinlock,flags);
6461 usc_start_receiver(info);
6462 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6464 goto Cleanup;
6469 /* check status of receive frame */
6471 status = info->rx_buffer_list[EndIndex].status;
6473 if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN +
6474 RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) {
6475 if ( status & RXSTATUS_SHORT_FRAME )
6476 info->icount.rxshort++;
6477 else if ( status & RXSTATUS_ABORT )
6478 info->icount.rxabort++;
6479 else if ( status & RXSTATUS_OVERRUN )
6480 info->icount.rxover++;
6481 else
6482 info->icount.rxcrc++;
6483 framesize = 0;
6484 } else {
6485 /* receive frame has no errors, get frame size.
6486 * The frame size is the starting value of the RCC (which was
6487 * set to 0xffff) minus the ending value of the RCC (decremented
6488 * once for each receive character) minus 2 for the 16-bit CRC.
6491 framesize = RCLRVALUE - info->rx_buffer_list[EndIndex].rcc;
6493 /* adjust frame size for CRC if any */
6494 if ( info->params.crc_type == HDLC_CRC_16_CCITT )
6495 framesize -= 2;
6498 if ( debug_level >= DEBUG_LEVEL_BH )
6499 printk("%s(%d):mgsl_get_rx_frame(%s) status=%04X size=%d\n",
6500 __FILE__,__LINE__,info->device_name,status,framesize);
6502 if ( debug_level >= DEBUG_LEVEL_DATA )
6503 mgsl_trace_block(info,info->rx_buffer_list[StartIndex].virt_addr,
6504 framesize,0);
6506 if (framesize) {
6507 if (framesize > HDLC_MAX_FRAME_SIZE)
6508 info->icount.rxlong++;
6509 else {
6510 info->icount.rxok++;
6511 pBufEntry = &(info->rx_buffer_list[StartIndex]);
6512 /* Call the line discipline receive callback directly. */
6513 tty->ldisc.receive_buf(tty, pBufEntry->virt_addr, info->flag_buf, framesize);
6516 /* Free the buffers used by this frame. */
6517 mgsl_free_rx_frame_buffers( info, StartIndex, EndIndex );
6519 ReturnCode = 1;
6521 Cleanup:
6523 if ( info->rx_enabled && info->rx_overflow ) {
6524 /* The receiver needs to restarted because of
6525 * a receive overflow (buffer or FIFO). If the
6526 * receive buffers are now empty, then restart receiver.
6529 if ( !info->rx_buffer_list[info->current_rx_buffer].status &&
6530 info->rx_buffer_list[info->current_rx_buffer].count ) {
6531 spin_lock_irqsave(&info->irq_spinlock,flags);
6532 usc_start_receiver(info);
6533 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6537 return ReturnCode;
6539 } /* end of mgsl_get_rx_frame() */
6541 /* mgsl_load_tx_dma_buffer()
6543 * Load the transmit DMA buffer with the specified data.
6545 * Arguments:
6547 * info pointer to device extension
6548 * Buffer pointer to buffer containing frame to load
6549 * BufferSize size in bytes of frame in Buffer
6551 * Return Value: None
6553 void mgsl_load_tx_dma_buffer(struct mgsl_struct *info, const char *Buffer,
6554 unsigned int BufferSize)
6556 unsigned short Copycount;
6557 unsigned int i = 0;
6558 DMABUFFERENTRY *pBufEntry;
6560 if ( debug_level >= DEBUG_LEVEL_DATA )
6561 mgsl_trace_block(info,Buffer,BufferSize,1);
6563 if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
6564 /* set CMR:13 to start transmit when
6565 * next GoAhead (abort) is received
6567 info->cmr_value |= BIT13;
6570 /* Setup the status and RCC (Frame Size) fields of the 1st */
6571 /* buffer entry in the transmit DMA buffer list. */
6573 info->tx_buffer_list[0].status = info->cmr_value & 0xf000;
6574 info->tx_buffer_list[0].rcc = BufferSize;
6575 info->tx_buffer_list[0].count = BufferSize;
6577 /* Copy frame data from 1st source buffer to the DMA buffers. */
6578 /* The frame data may span multiple DMA buffers. */
6580 while( BufferSize ){
6581 /* Get a pointer to next DMA buffer entry. */
6582 pBufEntry = &info->tx_buffer_list[i++];
6584 /* Calculate the number of bytes that can be copied from */
6585 /* the source buffer to this DMA buffer. */
6586 if ( BufferSize > DMABUFFERSIZE )
6587 Copycount = DMABUFFERSIZE;
6588 else
6589 Copycount = BufferSize;
6591 /* Actually copy data from source buffer to DMA buffer. */
6592 /* Also set the data count for this individual DMA buffer. */
6593 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
6594 mgsl_load_pci_memory(pBufEntry->virt_addr, Buffer,Copycount);
6595 else
6596 memcpy(pBufEntry->virt_addr, Buffer, Copycount);
6598 pBufEntry->count = Copycount;
6600 /* Advance source pointer and reduce remaining data count. */
6601 Buffer += Copycount;
6602 BufferSize -= Copycount;
6605 } /* end of mgsl_load_tx_dma_buffer() */
6608 * mgsl_register_test()
6610 * Performs a register test of the 16C32.
6612 * Arguments: info pointer to device instance data
6613 * Return Value: TRUE if test passed, otherwise FALSE
6615 BOOLEAN mgsl_register_test( struct mgsl_struct *info )
6617 static unsigned short BitPatterns[] =
6618 { 0x0000, 0xffff, 0xaaaa, 0x5555, 0x1234, 0x6969, 0x9696, 0x0f0f };
6619 static unsigned int Patterncount = sizeof(BitPatterns)/sizeof(unsigned short);
6620 unsigned int i;
6621 BOOLEAN rc = TRUE;
6622 unsigned long flags;
6624 spin_lock_irqsave(&info->irq_spinlock,flags);
6625 usc_reset(info);
6626 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6628 /* Verify the reset state of some registers. */
6630 if ( (usc_InReg( info, SICR ) != 0) ||
6631 (usc_InReg( info, IVR ) != 0) ||
6632 (usc_InDmaReg( info, DIVR ) != 0) ){
6633 rc = FALSE;
6636 if ( rc == TRUE ){
6637 /* Write bit patterns to various registers but do it out of */
6638 /* sync, then read back and verify values. */
6640 for ( i = 0 ; i < Patterncount ; i++ ) {
6641 usc_OutReg( info, TC0R, BitPatterns[i] );
6642 usc_OutReg( info, TC1R, BitPatterns[(i+1)%Patterncount] );
6643 usc_OutReg( info, TCLR, BitPatterns[(i+2)%Patterncount] );
6644 usc_OutReg( info, RCLR, BitPatterns[(i+3)%Patterncount] );
6645 usc_OutReg( info, RSR, BitPatterns[(i+4)%Patterncount] );
6646 usc_OutDmaReg( info, TBCR, BitPatterns[(i+5)%Patterncount] );
6648 if ( (usc_InReg( info, TC0R ) != BitPatterns[i]) ||
6649 (usc_InReg( info, TC1R ) != BitPatterns[(i+1)%Patterncount]) ||
6650 (usc_InReg( info, TCLR ) != BitPatterns[(i+2)%Patterncount]) ||
6651 (usc_InReg( info, RCLR ) != BitPatterns[(i+3)%Patterncount]) ||
6652 (usc_InReg( info, RSR ) != BitPatterns[(i+4)%Patterncount]) ||
6653 (usc_InDmaReg( info, TBCR ) != BitPatterns[(i+5)%Patterncount]) ){
6654 rc = FALSE;
6655 break;
6660 spin_lock_irqsave(&info->irq_spinlock,flags);
6661 usc_reset(info);
6662 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6664 return rc;
6666 } /* end of mgsl_register_test() */
6668 /* mgsl_irq_test() Perform interrupt test of the 16C32.
6670 * Arguments: info pointer to device instance data
6671 * Return Value: TRUE if test passed, otherwise FALSE
6673 BOOLEAN mgsl_irq_test( struct mgsl_struct *info )
6675 unsigned long EndTime;
6676 unsigned long flags;
6678 spin_lock_irqsave(&info->irq_spinlock,flags);
6679 usc_reset(info);
6680 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6683 * Setup 16C32 to interrupt on TxC pin (14MHz clock) transition.
6684 * The ISR sets irq_occurred to 1.
6687 info->irq_occurred = FALSE;
6689 /* Enable INTEN gate for ISA adapter (Port 6, Bit12) */
6690 /* Enable INTEN (Port 6, Bit12) */
6691 /* This connects the IRQ request signal to the ISA bus */
6692 /* on the ISA adapter. This has no effect for the PCI adapter */
6693 usc_OutReg( info, PCR, (unsigned short)((usc_InReg(info, PCR) | BIT13) & ~BIT12) );
6695 usc_EnableMasterIrqBit(info);
6696 usc_EnableInterrupts(info, IO_PIN);
6697 usc_ClearIrqPendingBits(info, IO_PIN);
6699 usc_UnlatchIostatusBits(info, MISCSTATUS_TXC_LATCHED);
6700 usc_EnableStatusIrqs(info, SICR_TXC_ACTIVE + SICR_TXC_INACTIVE);
6702 EndTime=100;
6703 while( EndTime-- && !info->irq_occurred ) {
6704 current->state = TASK_INTERRUPTIBLE;
6705 schedule_timeout(jiffies_from_ms(10));
6706 current->state = TASK_RUNNING;
6709 spin_lock_irqsave(&info->irq_spinlock,flags);
6710 usc_reset(info);
6711 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6713 if ( !info->irq_occurred )
6714 return FALSE;
6715 else
6716 return TRUE;
6718 } /* end of mgsl_irq_test() */
6720 /* mgsl_dma_test()
6722 * Perform a DMA test of the 16C32. A small frame is
6723 * transmitted via DMA from a transmit buffer to a receive buffer
6724 * using single buffer DMA mode.
6726 * Arguments: info pointer to device instance data
6727 * Return Value: TRUE if test passed, otherwise FALSE
6729 BOOLEAN mgsl_dma_test( struct mgsl_struct *info )
6731 unsigned short FifoLevel;
6732 unsigned long phys_addr;
6733 unsigned int FrameSize;
6734 unsigned int i;
6735 char *TmpPtr;
6736 BOOLEAN rc = TRUE;
6737 volatile unsigned short status;
6738 volatile unsigned long EndTime;
6739 unsigned long flags;
6740 MGSL_PARAMS tmp_params;
6742 /* save current port options */
6743 memcpy(&tmp_params,&info->params,sizeof(MGSL_PARAMS));
6744 /* load default port options */
6745 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
6747 #define TESTFRAMESIZE 40
6749 spin_lock_irqsave(&info->irq_spinlock,flags);
6751 /* setup 16C32 for SDLC DMA transfer mode */
6753 usc_reset(info);
6754 usc_set_sdlc_mode(info);
6755 usc_enable_loopback(info,1);
6757 /* Reprogram the RDMR so that the 16C32 does NOT clear the count
6758 * field of the buffer entry after fetching buffer address. This
6759 * way we can detect a DMA failure for a DMA read (which should be
6760 * non-destructive to system memory) before we try and write to
6761 * memory (where a failure could corrupt system memory).
6764 /* Receive DMA mode Register (RDMR)
6766 * <15..14> 11 DMA mode = Linked List Buffer mode
6767 * <13> 1 RSBinA/L = store Rx status Block in List entry
6768 * <12> 0 1 = Clear count of List Entry after fetching
6769 * <11..10> 00 Address mode = Increment
6770 * <9> 1 Terminate Buffer on RxBound
6771 * <8> 0 Bus Width = 16bits
6772 * <7..0> ? status Bits (write as 0s)
6774 * 1110 0010 0000 0000 = 0xe200
6777 usc_OutDmaReg( info, RDMR, 0xe200 );
6779 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6782 /* SETUP TRANSMIT AND RECEIVE DMA BUFFERS */
6784 FrameSize = TESTFRAMESIZE;
6786 /* setup 1st transmit buffer entry: */
6787 /* with frame size and transmit control word */
6789 info->tx_buffer_list[0].count = FrameSize;
6790 info->tx_buffer_list[0].rcc = FrameSize;
6791 info->tx_buffer_list[0].status = 0x4000;
6793 /* build a transmit frame in 1st transmit DMA buffer */
6795 TmpPtr = info->tx_buffer_list[0].virt_addr;
6796 for (i = 0; i < FrameSize; i++ )
6797 *TmpPtr++ = i;
6799 /* setup 1st receive buffer entry: */
6800 /* clear status, set max receive buffer size */
6802 info->rx_buffer_list[0].status = 0;
6803 info->rx_buffer_list[0].count = FrameSize + 4;
6805 /* zero out the 1st receive buffer */
6807 memset( info->rx_buffer_list[0].virt_addr, 0, FrameSize + 4 );
6809 /* Set count field of next buffer entries to prevent */
6810 /* 16C32 from using buffers after the 1st one. */
6812 info->tx_buffer_list[1].count = 0;
6813 info->rx_buffer_list[1].count = 0;
6816 /***************************/
6817 /* Program 16C32 receiver. */
6818 /***************************/
6820 spin_lock_irqsave(&info->irq_spinlock,flags);
6822 /* setup DMA transfers */
6823 usc_RTCmd( info, RTCmd_PurgeRxFifo );
6825 /* program 16C32 receiver with physical address of 1st DMA buffer entry */
6826 phys_addr = info->rx_buffer_list[0].phys_entry;
6827 usc_OutDmaReg( info, NRARL, (unsigned short)phys_addr );
6828 usc_OutDmaReg( info, NRARU, (unsigned short)(phys_addr >> 16) );
6830 /* Clear the Rx DMA status bits (read RDMR) and start channel */
6831 usc_InDmaReg( info, RDMR );
6832 usc_DmaCmd( info, DmaCmd_InitRxChannel );
6834 /* Enable Receiver (RMR <1..0> = 10) */
6835 usc_OutReg( info, RMR, (unsigned short)((usc_InReg(info, RMR) & 0xfffc) | 0x0002) );
6837 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6840 /*************************************************************/
6841 /* WAIT FOR RECEIVER TO DMA ALL PARAMETERS FROM BUFFER ENTRY */
6842 /*************************************************************/
6844 /* Wait 100ms for interrupt. */
6845 EndTime = jiffies + jiffies_from_ms(100);
6847 for(;;) {
6848 if ( jiffies > EndTime ) {
6849 rc = FALSE;
6850 break;
6853 spin_lock_irqsave(&info->irq_spinlock,flags);
6854 status = usc_InDmaReg( info, RDMR );
6855 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6857 if ( !(status & BIT4) && (status & BIT5) ) {
6858 /* INITG (BIT 4) is inactive (no entry read in progress) AND */
6859 /* BUSY (BIT 5) is active (channel still active). */
6860 /* This means the buffer entry read has completed. */
6861 break;
6866 /******************************/
6867 /* Program 16C32 transmitter. */
6868 /******************************/
6870 spin_lock_irqsave(&info->irq_spinlock,flags);
6872 /* Program the Transmit Character Length Register (TCLR) */
6873 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
6875 usc_OutReg( info, TCLR, (unsigned short)info->tx_buffer_list[0].count );
6876 usc_RTCmd( info, RTCmd_PurgeTxFifo );
6878 /* Program the address of the 1st DMA Buffer Entry in linked list */
6880 phys_addr = info->tx_buffer_list[0].phys_entry;
6881 usc_OutDmaReg( info, NTARL, (unsigned short)phys_addr );
6882 usc_OutDmaReg( info, NTARU, (unsigned short)(phys_addr >> 16) );
6884 /* unlatch Tx status bits, and start transmit channel. */
6886 usc_OutReg( info, TCSR, (unsigned short)(( usc_InReg(info, TCSR) & 0x0700) | 0xfa) );
6887 usc_DmaCmd( info, DmaCmd_InitTxChannel );
6889 /* wait for DMA controller to fill transmit FIFO */
6891 usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
6893 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6896 /**********************************/
6897 /* WAIT FOR TRANSMIT FIFO TO FILL */
6898 /**********************************/
6900 /* Wait 100ms */
6901 EndTime = jiffies + jiffies_from_ms(100);
6903 for(;;) {
6904 if ( jiffies > EndTime ) {
6905 rc = FALSE;
6906 break;
6909 spin_lock_irqsave(&info->irq_spinlock,flags);
6910 FifoLevel = usc_InReg(info, TICR) >> 8;
6911 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6913 if ( FifoLevel < 16 )
6914 break;
6915 else
6916 if ( FrameSize < 32 ) {
6917 /* This frame is smaller than the entire transmit FIFO */
6918 /* so wait for the entire frame to be loaded. */
6919 if ( FifoLevel <= (32 - FrameSize) )
6920 break;
6925 if ( rc == TRUE )
6927 /* Enable 16C32 transmitter. */
6929 spin_lock_irqsave(&info->irq_spinlock,flags);
6931 /* Transmit mode Register (TMR), <1..0> = 10, Enable Transmitter */
6932 usc_TCmd( info, TCmd_SendFrame );
6933 usc_OutReg( info, TMR, (unsigned short)((usc_InReg(info, TMR) & 0xfffc) | 0x0002) );
6935 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6938 /******************************/
6939 /* WAIT FOR TRANSMIT COMPLETE */
6940 /******************************/
6942 /* Wait 100ms */
6943 EndTime = jiffies + jiffies_from_ms(100);
6945 /* While timer not expired wait for transmit complete */
6947 spin_lock_irqsave(&info->irq_spinlock,flags);
6948 status = usc_InReg( info, TCSR );
6949 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6951 while ( !(status & (BIT6+BIT5+BIT4+BIT2+BIT1)) ) {
6952 if ( jiffies > EndTime ) {
6953 rc = FALSE;
6954 break;
6957 spin_lock_irqsave(&info->irq_spinlock,flags);
6958 status = usc_InReg( info, TCSR );
6959 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6964 if ( rc == TRUE ){
6965 /* CHECK FOR TRANSMIT ERRORS */
6966 if ( status & (BIT5 + BIT1) )
6967 rc = FALSE;
6970 if ( rc == TRUE ) {
6971 /* WAIT FOR RECEIVE COMPLETE */
6973 /* Wait 100ms */
6974 EndTime = jiffies + jiffies_from_ms(100);
6976 /* Wait for 16C32 to write receive status to buffer entry. */
6977 status=info->rx_buffer_list[0].status;
6978 while ( status == 0 ) {
6979 if ( jiffies > EndTime ) {
6980 printk(KERN_ERR"mark 4\n");
6981 rc = FALSE;
6982 break;
6984 status=info->rx_buffer_list[0].status;
6989 if ( rc == TRUE ) {
6990 /* CHECK FOR RECEIVE ERRORS */
6991 status = info->rx_buffer_list[0].status;
6993 if ( status & (BIT8 + BIT3 + BIT1) ) {
6994 /* receive error has occured */
6995 rc = FALSE;
6996 } else {
6997 if ( memcmp( info->tx_buffer_list[0].virt_addr ,
6998 info->rx_buffer_list[0].virt_addr, FrameSize ) ){
6999 rc = FALSE;
7004 usc_reset( info );
7006 /* restore current port options */
7007 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
7009 return rc;
7011 } /* end of mgsl_dma_test() */
7013 /* mgsl_adapter_test()
7015 * Perform the register, IRQ, and DMA tests for the 16C32.
7017 * Arguments: info pointer to device instance data
7018 * Return Value: 0 if success, otherwise -ENODEV
7020 int mgsl_adapter_test( struct mgsl_struct *info )
7022 if ( debug_level >= DEBUG_LEVEL_INFO )
7023 printk( "%s(%d):Testing device %s\n",
7024 __FILE__,__LINE__,info->device_name );
7026 if ( !mgsl_register_test( info ) ) {
7027 info->init_error = DiagStatus_AddressFailure;
7028 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
7029 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
7030 return -ENODEV;
7033 if ( !mgsl_irq_test( info ) ) {
7034 info->init_error = DiagStatus_IrqFailure;
7035 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
7036 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
7037 return -ENODEV;
7040 if ( !mgsl_dma_test( info ) ) {
7041 info->init_error = DiagStatus_DmaFailure;
7042 printk( "%s(%d):DMA test failure for device %s DMA=%d\n",
7043 __FILE__,__LINE__,info->device_name, (unsigned short)(info->dma_level) );
7044 return -ENODEV;
7047 if ( debug_level >= DEBUG_LEVEL_INFO )
7048 printk( "%s(%d):device %s passed diagnostics\n",
7049 __FILE__,__LINE__,info->device_name );
7051 return 0;
7053 } /* end of mgsl_adapter_test() */
7055 /* mgsl_memory_test()
7057 * Test the shared memory on a PCI adapter.
7059 * Arguments: info pointer to device instance data
7060 * Return Value: TRUE if test passed, otherwise FALSE
7062 BOOLEAN mgsl_memory_test( struct mgsl_struct *info )
7064 static unsigned long BitPatterns[] = { 0x0, 0x55555555, 0xaaaaaaaa,
7065 0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
7066 unsigned long Patterncount = sizeof(BitPatterns)/sizeof(unsigned long);
7067 unsigned long i;
7068 unsigned long TestLimit = SHARED_MEM_ADDRESS_SIZE/sizeof(unsigned long);
7069 unsigned long * TestAddr;
7071 if ( info->bus_type != MGSL_BUS_TYPE_PCI )
7072 return TRUE;
7074 TestAddr = (unsigned long *)info->memory_base;
7076 /* Test data lines with test pattern at one location. */
7078 for ( i = 0 ; i < Patterncount ; i++ ) {
7079 *TestAddr = BitPatterns[i];
7080 if ( *TestAddr != BitPatterns[i] )
7081 return FALSE;
7084 /* Test address lines with incrementing pattern over */
7085 /* entire address range. */
7087 for ( i = 0 ; i < TestLimit ; i++ ) {
7088 *TestAddr = i * 4;
7089 TestAddr++;
7092 TestAddr = (unsigned long *)info->memory_base;
7094 for ( i = 0 ; i < TestLimit ; i++ ) {
7095 if ( *TestAddr != i * 4 )
7096 return FALSE;
7097 TestAddr++;
7100 memset( info->memory_base, 0, SHARED_MEM_ADDRESS_SIZE );
7102 return TRUE;
7104 } /* End Of mgsl_memory_test() */
7107 #pragma optimize( "", off )
7108 /* mgsl_load_pci_memory()
7110 * Load a large block of data into the PCI shared memory.
7111 * Use this instead of memcpy() or memmove() to move data
7112 * into the PCI shared memory.
7114 * Notes:
7116 * This function prevents the PCI9050 interface chip from hogging
7117 * the adapter local bus, which can starve the 16C32 by preventing
7118 * 16C32 bus master cycles.
7120 * The PCI9050 documentation says that the 9050 will always release
7121 * control of the local bus after completing the current read
7122 * or write operation.
7124 * It appears that as long as the PCI9050 write FIFO is full, the
7125 * PCI9050 treats all of the writes as a single burst transaction
7126 * and will not release the bus. This causes DMA latency problems
7127 * at high speeds when copying large data blocks to the shared
7128 * memory.
7130 * This function in effect, breaks the a large shared memory write
7131 * into multiple transations by interleaving a shared memory read
7132 * which will flush the write FIFO and 'complete' the write
7133 * transation. This allows any pending DMA request to gain control
7134 * of the local bus in a timely fasion.
7136 * Arguments:
7138 * TargetPtr pointer to target address in PCI shared memory
7139 * SourcePtr pointer to source buffer for data
7140 * count count in bytes of data to copy
7142 * Return Value: None
7144 void mgsl_load_pci_memory( char* TargetPtr, const char* SourcePtr,
7145 unsigned short count )
7147 /*******************************************************/
7148 /* A load interval of 16 allows for 4 32-bit writes at */
7149 /* 60ns each for a maximum latency of 240ns on the */
7150 /* local bus. */
7151 /*******************************************************/
7153 #define PCI_LOAD_INTERVAL 64
7155 unsigned short Intervalcount = count / PCI_LOAD_INTERVAL;
7156 unsigned short Index;
7157 unsigned long Dummy;
7159 for ( Index = 0 ; Index < Intervalcount ; Index++ )
7161 memcpy(TargetPtr, SourcePtr, PCI_LOAD_INTERVAL);
7162 Dummy = *((unsigned long *)TargetPtr);
7163 TargetPtr += PCI_LOAD_INTERVAL;
7164 SourcePtr += PCI_LOAD_INTERVAL;
7167 memcpy( TargetPtr, SourcePtr, count % PCI_LOAD_INTERVAL );
7169 } /* End Of mgsl_load_pci_memory() */
7170 #pragma optimize( "", on )
7172 void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit)
7174 int i;
7175 int linecount;
7176 if (xmit)
7177 printk("%s tx data:\n",info->device_name);
7178 else
7179 printk("%s rx data:\n",info->device_name);
7181 while(count) {
7182 if (count > 16)
7183 linecount = 16;
7184 else
7185 linecount = count;
7187 for(i=0;i<linecount;i++)
7188 printk("%02X ",(unsigned char)data[i]);
7189 for(;i<17;i++)
7190 printk(" ");
7191 for(i=0;i<linecount;i++) {
7192 if (data[i]>=040 && data[i]<=0176)
7193 printk("%c",data[i]);
7194 else
7195 printk(".");
7197 printk("\n");
7199 data += linecount;
7200 count -= linecount;
7202 } /* end of mgsl_trace_block() */
7204 /* mgsl_tx_timeout()
7206 * called when HDLC frame times out
7207 * update stats and do tx completion processing
7209 * Arguments: context pointer to device instance data
7210 * Return Value: None
7212 void mgsl_tx_timeout(unsigned long context)
7214 struct mgsl_struct *info = (struct mgsl_struct*)context;
7215 unsigned long flags;
7217 if ( debug_level >= DEBUG_LEVEL_INFO )
7218 printk( "%s(%d):mgsl_tx_timeout(%s)\n",
7219 __FILE__,__LINE__,info->device_name);
7220 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
7221 info->icount.txtimeout++;
7223 spin_lock_irqsave(&info->irq_spinlock,flags);
7224 info->tx_active = 0;
7225 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
7227 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
7228 usc_loopmode_cancel_transmit( info );
7230 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7232 mgsl_bh_transmit_data(info,0);
7234 } /* end of mgsl_tx_timeout() */
7236 /* signal that there are no more frames to send, so that
7237 * line is 'released' by echoing RxD to TxD when current
7238 * transmission is complete (or immediately if no tx in progress).
7240 static int mgsl_loopmode_send_done( struct mgsl_struct * info )
7242 unsigned long flags;
7244 spin_lock_irqsave(&info->irq_spinlock,flags);
7245 if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
7246 if (info->tx_active)
7247 info->loopmode_send_done_requested = TRUE;
7248 else
7249 usc_loopmode_send_done(info);
7251 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7253 return 0;
7256 /* release the line by echoing RxD to TxD
7257 * upon completion of a transmit frame
7259 void usc_loopmode_send_done( struct mgsl_struct * info )
7261 info->loopmode_send_done_requested = FALSE;
7262 /* clear CMR:13 to 0 to start echoing RxData to TxData */
7263 info->cmr_value &= ~BIT13;
7264 usc_OutReg(info, CMR, info->cmr_value);
7267 /* abort a transmit in progress while in HDLC LoopMode
7269 void usc_loopmode_cancel_transmit( struct mgsl_struct * info )
7271 /* reset tx dma channel and purge TxFifo */
7272 usc_RTCmd( info, RTCmd_PurgeTxFifo );
7273 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
7274 usc_loopmode_send_done( info );
7277 /* for HDLC/SDLC LoopMode, setting CMR:13 after the transmitter is enabled
7278 * is an Insert Into Loop action. Upon receipt of a GoAhead sequence (RxAbort)
7279 * we must clear CMR:13 to begin repeating TxData to RxData
7281 void usc_loopmode_insert_request( struct mgsl_struct * info )
7283 info->loopmode_insert_requested = TRUE;
7285 /* enable RxAbort irq. On next RxAbort, clear CMR:13 to
7286 * begin repeating TxData on RxData (complete insertion)
7288 usc_OutReg( info, RICR,
7289 (usc_InReg( info, RICR ) | RXSTATUS_ABORT_RECEIVED ) );
7291 /* set CMR:13 to insert into loop on next GoAhead (RxAbort) */
7292 info->cmr_value |= BIT13;
7293 usc_OutReg(info, CMR, info->cmr_value);
7296 /* return 1 if station is inserted into the loop, otherwise 0
7298 int usc_loopmode_active( struct mgsl_struct * info)
7300 return usc_InReg( info, CCSR ) & BIT7 ? 1 : 0 ;
7303 /* return 1 if USC is in loop send mode, otherwise 0
7305 int usc_loopmode_send_active( struct mgsl_struct * info )
7307 return usc_InReg( info, CCSR ) & BIT6 ? 1 : 0 ;