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

Subversion Repositories or1k

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

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

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * $Id: synclinkmp.c,v 1.1.1.1 2004-04-15 01:58:16 phoenix Exp $
3
 *
4
 * Device driver for Microgate SyncLink Multiport
5
 * high speed multiprotocol serial adapter.
6
 *
7
 * written by Paul Fulghum for Microgate Corporation
8
 * paulkf@microgate.com
9
 *
10
 * Microgate and SyncLink are trademarks of Microgate Corporation
11
 *
12
 * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
13
 * This code is released under the GNU General Public License (GPL)
14
 *
15
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
16
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
25
 * OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
 
28
#define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
29
#if defined(__i386__)
30
#  define BREAKPOINT() asm("   int $3");
31
#else
32
#  define BREAKPOINT() { }
33
#endif
34
 
35
#define MAX_DEVICES 12
36
 
37
#include <linux/config.h>
38
#include <linux/module.h>
39
#include <linux/version.h>
40
#include <linux/errno.h>
41
#include <linux/signal.h>
42
#include <linux/sched.h>
43
#include <linux/timer.h>
44
#include <linux/interrupt.h>
45
#include <linux/pci.h>
46
#include <linux/tty.h>
47
#include <linux/tty_flip.h>
48
#include <linux/serial.h>
49
#include <linux/major.h>
50
#include <linux/string.h>
51
#include <linux/fcntl.h>
52
#include <linux/ptrace.h>
53
#include <linux/ioport.h>
54
#include <linux/mm.h>
55
#include <linux/slab.h>
56
#include <linux/netdevice.h>
57
#include <linux/vmalloc.h>
58
#include <linux/init.h>
59
#include <asm/serial.h>
60
#include <linux/delay.h>
61
#include <linux/ioctl.h>
62
 
63
#include <asm/system.h>
64
#include <asm/io.h>
65
#include <asm/irq.h>
66
#include <asm/dma.h>
67
#include <asm/bitops.h>
68
#include <asm/types.h>
69
#include <linux/termios.h>
70
#include <linux/tqueue.h>
71
 
72
#ifdef CONFIG_SYNCLINK_SYNCPPP_MODULE
73
#define CONFIG_SYNCLINK_SYNCPPP 1
74
#endif
75
 
76
#ifdef CONFIG_SYNCLINK_SYNCPPP
77
#include <net/syncppp.h>
78
#endif
79
 
80
#include <asm/segment.h>
81
#define GET_USER(error,value,addr) error = get_user(value,addr)
82
#define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
83
#define PUT_USER(error,value,addr) error = put_user(value,addr)
84
#define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
85
 
86
#include <asm/uaccess.h>
87
 
88
#include "linux/synclink.h"
89
 
90
static MGSL_PARAMS default_params = {
91
        MGSL_MODE_HDLC,                 /* unsigned long mode */
92
        0,                               /* unsigned char loopback; */
93
        HDLC_FLAG_UNDERRUN_ABORT15,     /* unsigned short flags; */
94
        HDLC_ENCODING_NRZI_SPACE,       /* unsigned char encoding; */
95
        0,                               /* unsigned long clock_speed; */
96
        0xff,                           /* unsigned char addr_filter; */
97
        HDLC_CRC_16_CCITT,              /* unsigned short crc_type; */
98
        HDLC_PREAMBLE_LENGTH_8BITS,     /* unsigned char preamble_length; */
99
        HDLC_PREAMBLE_PATTERN_NONE,     /* unsigned char preamble; */
100
        9600,                           /* unsigned long data_rate; */
101
        8,                              /* unsigned char data_bits; */
102
        1,                              /* unsigned char stop_bits; */
103
        ASYNC_PARITY_NONE               /* unsigned char parity; */
104
};
105
 
106
/* size in bytes of DMA data buffers */
107
#define SCABUFSIZE      1024
108
#define SCA_MEM_SIZE    0x40000
109
#define SCA_BASE_SIZE   512
110
#define SCA_REG_SIZE    16
111
#define SCA_MAX_PORTS   4
112
#define SCAMAXDESC      128
113
 
114
#define BUFFERLISTSIZE  4096
115
 
116
/* SCA-I style DMA buffer descriptor */
117
typedef struct _SCADESC
118
{
119
        u16     next;           /* lower l6 bits of next descriptor addr */
120
        u16     buf_ptr;        /* lower 16 bits of buffer addr */
121
        u8      buf_base;       /* upper 8 bits of buffer addr */
122
        u8      pad1;
123
        u16     length;         /* length of buffer */
124
        u8      status;         /* status of buffer */
125
        u8      pad2;
126
} SCADESC, *PSCADESC;
127
 
128
typedef struct _SCADESC_EX
129
{
130
        /* device driver bookkeeping section */
131
        char    *virt_addr;     /* virtual address of data buffer */
132
        u16     phys_entry;     /* lower 16-bits of physical address of this descriptor */
133
} SCADESC_EX, *PSCADESC_EX;
134
 
135
/* The queue of BH actions to be performed */
136
 
137
#define BH_RECEIVE  1
138
#define BH_TRANSMIT 2
139
#define BH_STATUS   4
140
 
141
#define IO_PIN_SHUTDOWN_LIMIT 100
142
 
143
#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
144
 
145
struct  _input_signal_events {
146
        int     ri_up;
147
        int     ri_down;
148
        int     dsr_up;
149
        int     dsr_down;
150
        int     dcd_up;
151
        int     dcd_down;
152
        int     cts_up;
153
        int     cts_down;
154
};
155
 
156
/*
157
 * Device instance data structure
158
 */
159
typedef struct _synclinkmp_info {
160
        void *if_ptr;                           /* General purpose pointer (used by SPPP) */
161
        int                     magic;
162
        int                     flags;
163
        int                     count;          /* count of opens */
164
        int                     line;
165
        unsigned short          close_delay;
166
        unsigned short          closing_wait;   /* time to wait before closing */
167
 
168
        struct mgsl_icount      icount;
169
 
170
        struct termios          normal_termios;
171
        struct termios          callout_termios;
172
 
173
        struct tty_struct       *tty;
174
        int                     timeout;
175
        int                     x_char;         /* xon/xoff character */
176
        int                     blocked_open;   /* # of blocked opens */
177
        long                    session;        /* Session of opening process */
178
        long                    pgrp;           /* pgrp of opening process */
179
        u16                     read_status_mask1;  /* break detection (SR1 indications) */
180
        u16                     read_status_mask2;  /* parity/framing/overun (SR2 indications) */
181
        unsigned char           ignore_status_mask1;  /* break detection (SR1 indications) */
182
        unsigned char           ignore_status_mask2;  /* parity/framing/overun (SR2 indications) */
183
        unsigned char           *tx_buf;
184
        int                     tx_put;
185
        int                     tx_get;
186
        int                     tx_count;
187
 
188
        wait_queue_head_t       open_wait;
189
        wait_queue_head_t       close_wait;
190
 
191
        wait_queue_head_t       status_event_wait_q;
192
        wait_queue_head_t       event_wait_q;
193
        struct timer_list       tx_timer;       /* HDLC transmit timeout timer */
194
        struct _synclinkmp_info *next_device;   /* device list link */
195
        struct timer_list       status_timer;   /* input signal status check timer */
196
 
197
        spinlock_t lock;                /* spinlock for synchronizing with ISR */
198
        struct tq_struct task;                  /* task structure for scheduling bh */
199
 
200
        u32 max_frame_size;                     /* as set by device config */
201
 
202
        u32 pending_bh;
203
 
204
        int bh_running;                         /* Protection from multiple */
205
        int isr_overflow;
206
        int bh_requested;
207
 
208
        int dcd_chkcount;                       /* check counts to prevent */
209
        int cts_chkcount;                       /* too many IRQs if a signal */
210
        int dsr_chkcount;                       /* is floating */
211
        int ri_chkcount;
212
 
213
        char *buffer_list;                      /* virtual address of Rx & Tx buffer lists */
214
        unsigned long buffer_list_phys;
215
 
216
        unsigned int rx_buf_count;              /* count of total allocated Rx buffers */
217
        SCADESC *rx_buf_list;                   /* list of receive buffer entries */
218
        SCADESC_EX rx_buf_list_ex[SCAMAXDESC]; /* list of receive buffer entries */
219
        unsigned int current_rx_buf;
220
 
221
        unsigned int tx_buf_count;              /* count of total allocated Tx buffers */
222
        SCADESC *tx_buf_list;           /* list of transmit buffer entries */
223
        SCADESC_EX tx_buf_list_ex[SCAMAXDESC]; /* list of transmit buffer entries */
224
        unsigned int last_tx_buf;
225
 
226
        unsigned char *tmp_rx_buf;
227
        unsigned int tmp_rx_buf_count;
228
 
229
        int rx_enabled;
230
        int rx_overflow;
231
 
232
        int tx_enabled;
233
        int tx_active;
234
        u32 idle_mode;
235
 
236
        unsigned char ie0_value;
237
        unsigned char ie1_value;
238
        unsigned char ie2_value;
239
        unsigned char ctrlreg_value;
240
        unsigned char old_signals;
241
 
242
        char device_name[25];                   /* device instance name */
243
 
244
        int port_count;
245
        int adapter_num;
246
        int port_num;
247
 
248
        struct _synclinkmp_info *port_array[SCA_MAX_PORTS];
249
 
250
        unsigned int bus_type;                  /* expansion bus type (ISA,EISA,PCI) */
251
 
252
        unsigned int irq_level;                 /* interrupt level */
253
        unsigned long irq_flags;
254
        int irq_requested;                      /* nonzero if IRQ requested */
255
 
256
        MGSL_PARAMS params;                     /* communications parameters */
257
 
258
        unsigned char serial_signals;           /* current serial signal states */
259
 
260
        int irq_occurred;                       /* for diagnostics use */
261
        unsigned int init_error;                /* Initialization startup error */
262
 
263
        u32 last_mem_alloc;
264
        unsigned char* memory_base;             /* shared memory address (PCI only) */
265
        u32 phys_memory_base;
266
        int shared_mem_requested;
267
 
268
        unsigned char* sca_base;                /* HD64570 SCA Memory address */
269
        u32 phys_sca_base;
270
        u32 sca_offset;
271
        int sca_base_requested;
272
 
273
        unsigned char* lcr_base;                /* local config registers (PCI only) */
274
        u32 phys_lcr_base;
275
        u32 lcr_offset;
276
        int lcr_mem_requested;
277
 
278
        unsigned char* statctrl_base;           /* status/control register memory */
279
        u32 phys_statctrl_base;
280
        u32 statctrl_offset;
281
        int sca_statctrl_requested;
282
 
283
        u32 misc_ctrl_value;
284
        char flag_buf[MAX_ASYNC_BUFFER_SIZE];
285
        char char_buf[MAX_ASYNC_BUFFER_SIZE];
286
        BOOLEAN drop_rts_on_tx_done;
287
 
288
        struct  _input_signal_events    input_signal_events;
289
 
290
        /* SPPP/Cisco HDLC device parts */
291
        int netcount;
292
        int dosyncppp;
293
        spinlock_t netlock;
294
#ifdef CONFIG_SYNCLINK_SYNCPPP
295
        struct ppp_device pppdev;
296
        char netname[10];
297
        struct net_device *netdev;
298
        struct net_device_stats netstats;
299
        struct net_device netdevice;
300
#endif
301
} SLMP_INFO;
302
 
303
#define MGSL_MAGIC 0x5401
304
 
305
/*
306
 * define serial signal status change macros
307
 */
308
#define MISCSTATUS_DCD_LATCHED  (SerialSignal_DCD<<8)   /* indicates change in DCD */
309
#define MISCSTATUS_RI_LATCHED   (SerialSignal_RI<<8)    /* indicates change in RI */
310
#define MISCSTATUS_CTS_LATCHED  (SerialSignal_CTS<<8)   /* indicates change in CTS */
311
#define MISCSTATUS_DSR_LATCHED  (SerialSignal_DSR<<8)   /* change in DSR */
312
 
313
/* Common Register macros */
314
#define LPR     0x00
315
#define PABR0   0x02
316
#define PABR1   0x03
317
#define WCRL    0x04
318
#define WCRM    0x05
319
#define WCRH    0x06
320
#define DPCR    0x08
321
#define DMER    0x09
322
#define ISR0    0x10
323
#define ISR1    0x11
324
#define ISR2    0x12
325
#define IER0    0x14
326
#define IER1    0x15
327
#define IER2    0x16
328
#define ITCR    0x18
329
#define INTVR   0x1a
330
#define IMVR    0x1c
331
 
332
/* MSCI Register macros */
333
#define TRB     0x20
334
#define TRBL    0x20
335
#define TRBH    0x21
336
#define SR0     0x22
337
#define SR1     0x23
338
#define SR2     0x24
339
#define SR3     0x25
340
#define FST     0x26
341
#define IE0     0x28
342
#define IE1     0x29
343
#define IE2     0x2a
344
#define FIE     0x2b
345
#define CMD     0x2c
346
#define MD0     0x2e
347
#define MD1     0x2f
348
#define MD2     0x30
349
#define CTL     0x31
350
#define SA0     0x32
351
#define SA1     0x33
352
#define IDL     0x34
353
#define TMC     0x35
354
#define RXS     0x36
355
#define TXS     0x37
356
#define TRC0    0x38
357
#define TRC1    0x39
358
#define RRC     0x3a
359
#define CST0    0x3c
360
#define CST1    0x3d
361
 
362
/* Timer Register Macros */
363
#define TCNT    0x60
364
#define TCNTL   0x60
365
#define TCNTH   0x61
366
#define TCONR   0x62
367
#define TCONRL  0x62
368
#define TCONRH  0x63
369
#define TMCS    0x64
370
#define TEPR    0x65
371
 
372
/* DMA Controller Register macros */
373
#define DAR     0x80
374
#define DARL    0x80
375
#define DARH    0x81
376
#define DARB    0x82
377
#define BAR     0x80
378
#define BARL    0x80
379
#define BARH    0x81
380
#define BARB    0x82
381
#define SAR     0x84
382
#define SARL    0x84
383
#define SARH    0x85
384
#define SARB    0x86
385
#define CPB     0x86
386
#define CDA     0x88
387
#define CDAL    0x88
388
#define CDAH    0x89
389
#define EDA     0x8a
390
#define EDAL    0x8a
391
#define EDAH    0x8b
392
#define BFL     0x8c
393
#define BFLL    0x8c
394
#define BFLH    0x8d
395
#define BCR     0x8e
396
#define BCRL    0x8e
397
#define BCRH    0x8f
398
#define DSR     0x90
399
#define DMR     0x91
400
#define FCT     0x93
401
#define DIR     0x94
402
#define DCMD    0x95
403
 
404
/* combine with timer or DMA register address */
405
#define TIMER0  0x00
406
#define TIMER1  0x08
407
#define TIMER2  0x10
408
#define TIMER3  0x18
409
#define RXDMA   0x00
410
#define TXDMA   0x20
411
 
412
/* SCA Command Codes */
413
#define NOOP            0x00
414
#define TXRESET         0x01
415
#define TXENABLE        0x02
416
#define TXDISABLE       0x03
417
#define TXCRCINIT       0x04
418
#define TXCRCEXCL       0x05
419
#define TXEOM           0x06
420
#define TXABORT         0x07
421
#define MPON            0x08
422
#define TXBUFCLR        0x09
423
#define RXRESET         0x11
424
#define RXENABLE        0x12
425
#define RXDISABLE       0x13
426
#define RXCRCINIT       0x14
427
#define RXREJECT        0x15
428
#define SEARCHMP        0x16
429
#define RXCRCEXCL       0x17
430
#define RXCRCCALC       0x18
431
#define CHRESET         0x21
432
#define HUNT            0x31
433
 
434
/* DMA command codes */
435
#define SWABORT         0x01
436
#define FEICLEAR        0x02
437
 
438
/* IE0 */
439
#define TXINTE          BIT7
440
#define RXINTE          BIT6
441
#define TXRDYE          BIT1
442
#define RXRDYE          BIT0
443
 
444
/* IE1 & SR1 */
445
#define UDRN    BIT7
446
#define IDLE    BIT6
447
#define SYNCD   BIT4
448
#define FLGD    BIT4
449
#define CCTS    BIT3
450
#define CDCD    BIT2
451
#define BRKD    BIT1
452
#define ABTD    BIT1
453
#define GAPD    BIT1
454
#define BRKE    BIT0
455
#define IDLD    BIT0
456
 
457
/* IE2 & SR2 */
458
#define EOM     BIT7
459
#define PMP     BIT6
460
#define SHRT    BIT6
461
#define PE      BIT5
462
#define ABT     BIT5
463
#define FRME    BIT4
464
#define RBIT    BIT4
465
#define OVRN    BIT3
466
#define CRCE    BIT2
467
 
468
 
469
#define jiffies_from_ms(a) ((((a) * HZ)/1000)+1)
470
 
471
/*
472
 * Global linked list of SyncLink devices
473
 */
474
static SLMP_INFO *synclinkmp_device_list = NULL;
475
static int synclinkmp_adapter_count = -1;
476
static int synclinkmp_device_count = 0;
477
 
478
/*
479
 * Set this param to non-zero to load eax with the
480
 * .text section address and breakpoint on module load.
481
 * This is useful for use with gdb and add-symbol-file command.
482
 */
483
static int break_on_load=0;
484
 
485
/*
486
 * Driver major number, defaults to zero to get auto
487
 * assigned major number. May be forced as module parameter.
488
 */
489
static int ttymajor=0;
490
static int cuamajor=0;
491
 
492
/*
493
 * Array of user specified options for ISA adapters.
494
 */
495
static int debug_level = 0;
496
static int maxframe[MAX_DEVICES] = {0,};
497
static int dosyncppp[MAX_DEVICES] = {0,};
498
 
499
MODULE_PARM(break_on_load,"i");
500
MODULE_PARM(ttymajor,"i");
501
MODULE_PARM(cuamajor,"i");
502
MODULE_PARM(debug_level,"i");
503
MODULE_PARM(maxframe,"1-" __MODULE_STRING(MAX_DEVICES) "i");
504
MODULE_PARM(dosyncppp,"1-" __MODULE_STRING(MAX_DEVICES) "i");
505
 
506
static char *driver_name = "SyncLink MultiPort driver";
507
static char *driver_version = "$Revision: 1.1.1.1 $";
508
 
509
static int __devinit synclinkmp_init_one(struct pci_dev *dev,const struct pci_device_id *ent);
510
static void __devexit synclinkmp_remove_one(struct pci_dev *dev);
511
 
512
static struct pci_device_id synclinkmp_pci_tbl[] __devinitdata = {
513
        { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_SCA, PCI_ANY_ID, PCI_ANY_ID, },
514
        { 0, }, /* terminate list */
515
};
516
MODULE_DEVICE_TABLE(pci, synclinkmp_pci_tbl);
517
 
518
#ifdef MODULE_LICENSE
519
MODULE_LICENSE("GPL");
520
#endif
521
 
522
static struct pci_driver synclinkmp_pci_driver = {
523
        name:           "synclinkmp",
524
        id_table:       synclinkmp_pci_tbl,
525
        probe:          synclinkmp_init_one,
526
        remove:         __devexit_p(synclinkmp_remove_one),
527
};
528
 
529
 
530
static struct tty_driver serial_driver, callout_driver;
531
static int serial_refcount;
532
 
533
/* number of characters left in xmit buffer before we ask for more */
534
#define WAKEUP_CHARS 256
535
 
536
static struct tty_struct *serial_table[MAX_DEVICES];
537
static struct termios *serial_termios[MAX_DEVICES];
538
static struct termios *serial_termios_locked[MAX_DEVICES];
539
 
540
#ifndef MIN
541
#define MIN(a,b) ((a) < (b) ? (a) : (b))
542
#endif
543
 
544
 
545
/* tty callbacks */
546
 
547
static int  open(struct tty_struct *tty, struct file * filp);
548
static void close(struct tty_struct *tty, struct file * filp);
549
static void hangup(struct tty_struct *tty);
550
static void set_termios(struct tty_struct *tty, struct termios *old_termios);
551
 
552
static int  write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
553
static void put_char(struct tty_struct *tty, unsigned char ch);
554
static void send_xchar(struct tty_struct *tty, char ch);
555
static void wait_until_sent(struct tty_struct *tty, int timeout);
556
static int  write_room(struct tty_struct *tty);
557
static void flush_chars(struct tty_struct *tty);
558
static void flush_buffer(struct tty_struct *tty);
559
static void tx_hold(struct tty_struct *tty);
560
static void tx_release(struct tty_struct *tty);
561
 
562
static int  ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
563
static int  read_proc(char *page, char **start, off_t off, int count,int *eof, void *data);
564
static int  chars_in_buffer(struct tty_struct *tty);
565
static void throttle(struct tty_struct * tty);
566
static void unthrottle(struct tty_struct * tty);
567
static void set_break(struct tty_struct *tty, int break_state);
568
 
569
/* sppp support and callbacks */
570
 
571
#ifdef CONFIG_SYNCLINK_SYNCPPP
572
static void sppp_init(SLMP_INFO *info);
573
static void sppp_delete(SLMP_INFO *info);
574
static void sppp_rx_done(SLMP_INFO *info, char *buf, int size);
575
static void sppp_tx_done(SLMP_INFO *info);
576
 
577
static int  sppp_cb_open(struct net_device *d);
578
static int  sppp_cb_close(struct net_device *d);
579
static int  sppp_cb_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
580
static int  sppp_cb_tx(struct sk_buff *skb, struct net_device *dev);
581
static void sppp_cb_tx_timeout(struct net_device *dev);
582
static struct net_device_stats *sppp_cb_net_stats(struct net_device *dev);
583
#endif
584
 
585
/* ioctl handlers */
586
 
587
static int  get_stats(SLMP_INFO *info, struct mgsl_icount *user_icount);
588
static int  get_params(SLMP_INFO *info, MGSL_PARAMS *params);
589
static int  set_params(SLMP_INFO *info, MGSL_PARAMS *params);
590
static int  get_txidle(SLMP_INFO *info, int*idle_mode);
591
static int  set_txidle(SLMP_INFO *info, int idle_mode);
592
static int  tx_enable(SLMP_INFO *info, int enable);
593
static int  tx_abort(SLMP_INFO *info);
594
static int  rx_enable(SLMP_INFO *info, int enable);
595
static int  map_status(int signals);
596
static int  modem_input_wait(SLMP_INFO *info,int arg);
597
static int  wait_mgsl_event(SLMP_INFO *info, int *mask_ptr);
598
static int  get_modem_info(SLMP_INFO *info, unsigned int *value);
599
static int  set_modem_info(SLMP_INFO *info, unsigned int cmd,unsigned int *value);
600
static void set_break(struct tty_struct *tty, int break_state);
601
 
602
static void add_device(SLMP_INFO *info);
603
static void device_init(int adapter_num, struct pci_dev *pdev);
604
static int  claim_resources(SLMP_INFO *info);
605
static void release_resources(SLMP_INFO *info);
606
 
607
static int  startup(SLMP_INFO *info);
608
static int  block_til_ready(struct tty_struct *tty, struct file * filp,SLMP_INFO *info);
609
static void shutdown(SLMP_INFO *info);
610
static void program_hw(SLMP_INFO *info);
611
static void change_params(SLMP_INFO *info);
612
 
613
static int  init_adapter(SLMP_INFO *info);
614
static int  register_test(SLMP_INFO *info);
615
static int  irq_test(SLMP_INFO *info);
616
static int  loopback_test(SLMP_INFO *info);
617
static int  adapter_test(SLMP_INFO *info);
618
static int  memory_test(SLMP_INFO *info);
619
 
620
static void reset_adapter(SLMP_INFO *info);
621
static void reset_port(SLMP_INFO *info);
622
static void async_mode(SLMP_INFO *info);
623
static void hdlc_mode(SLMP_INFO *info);
624
 
625
static void rx_stop(SLMP_INFO *info);
626
static void rx_start(SLMP_INFO *info);
627
static void rx_reset_buffers(SLMP_INFO *info);
628
static void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last);
629
static int  rx_get_frame(SLMP_INFO *info);
630
 
631
static void tx_start(SLMP_INFO *info);
632
static void tx_stop(SLMP_INFO *info);
633
static void tx_load_fifo(SLMP_INFO *info);
634
static void tx_set_idle(SLMP_INFO *info);
635
static void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count);
636
 
637
static void get_signals(SLMP_INFO *info);
638
static void set_signals(SLMP_INFO *info);
639
static void enable_loopback(SLMP_INFO *info, int enable);
640
static void set_rate(SLMP_INFO *info, u32 data_rate);
641
 
642
static int  bh_action(SLMP_INFO *info);
643
static void bh_handler(void* Context);
644
static void bh_receive(SLMP_INFO *info);
645
static void bh_transmit(SLMP_INFO *info);
646
static void bh_status(SLMP_INFO *info);
647
static void isr_timer(SLMP_INFO *info);
648
static void isr_rxint(SLMP_INFO *info);
649
static void isr_rxrdy(SLMP_INFO *info);
650
static void isr_txint(SLMP_INFO *info);
651
static void isr_txrdy(SLMP_INFO *info);
652
static void isr_rxdmaok(SLMP_INFO *info);
653
static void isr_rxdmaerror(SLMP_INFO *info);
654
static void isr_txdmaok(SLMP_INFO *info);
655
static void isr_txdmaerror(SLMP_INFO *info);
656
static void isr_io_pin(SLMP_INFO *info, u16 status);
657
static void synclinkmp_interrupt(int irq, void *dev_id, struct pt_regs * regs);
658
 
659
static int  alloc_dma_bufs(SLMP_INFO *info);
660
static void free_dma_bufs(SLMP_INFO *info);
661
static int  alloc_buf_list(SLMP_INFO *info);
662
static int  alloc_frame_bufs(SLMP_INFO *info, SCADESC *list, SCADESC_EX *list_ex,int count);
663
static int  alloc_tmp_rx_buf(SLMP_INFO *info);
664
static void free_tmp_rx_buf(SLMP_INFO *info);
665
 
666
static void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count);
667
static void trace_block(SLMP_INFO *info, const char* data, int count, int xmit);
668
static void tx_timeout(unsigned long context);
669
static void status_timeout(unsigned long context);
670
 
671
static unsigned char read_reg(SLMP_INFO *info, unsigned char addr);
672
static void write_reg(SLMP_INFO *info, unsigned char addr, unsigned char val);
673
static u16 read_reg16(SLMP_INFO *info, unsigned char addr);
674
static void write_reg16(SLMP_INFO *info, unsigned char addr, u16 val);
675
static unsigned char read_status_reg(SLMP_INFO * info);
676
static void write_control_reg(SLMP_INFO * info);
677
 
678
 
679
static unsigned char rx_active_fifo_level = 16; // rx request FIFO activation level in bytes
680
static unsigned char tx_active_fifo_level = 16; // tx request FIFO activation level in bytes
681
static unsigned char tx_negate_fifo_level = 32; // tx request FIFO negation level in bytes
682
 
683
static u32 misc_ctrl_value = 0x007e4040;
684
static u32 lcr1_brdr_value = 0x0080002d;
685
 
686
static u32 read_ahead_count = 8;
687
 
688
/* DPCR, DMA Priority Control
689
 *
690
 * 07..05  Not used, must be 0
691
 * 04      BRC, bus release condition: 0=all transfers complete
692
 *              1=release after 1 xfer on all channels
693
 * 03      CCC, channel change condition: 0=every cycle
694
 *              1=after each channel completes all xfers
695
 * 02..00  PR<2..0>, priority 100=round robin
696
 *
697
 * 00000100 = 0x00
698
 */
699
static unsigned char dma_priority = 0x04;
700
 
701
// Number of bytes that can be written to shared RAM
702
// in a single write operation
703
static u32 sca_pci_load_interval = 64;
704
 
705
/*
706
 * 1st function defined in .text section. Calling this function in
707
 * init_module() followed by a breakpoint allows a remote debugger
708
 * (gdb) to get the .text address for the add-symbol-file command.
709
 * This allows remote debugging of dynamically loadable modules.
710
 */
711
static void* synclinkmp_get_text_ptr(void);
712
static void* synclinkmp_get_text_ptr() {return synclinkmp_get_text_ptr;}
713
 
714
static inline int sanity_check(SLMP_INFO *info,
715
                               kdev_t device, const char *routine)
716
{
717
#ifdef SANITY_CHECK
718
        static const char *badmagic =
719
                "Warning: bad magic number for synclinkmp_struct (%s) in %s\n";
720
        static const char *badinfo =
721
                "Warning: null synclinkmp_struct for (%s) in %s\n";
722
 
723
        if (!info) {
724
                printk(badinfo, kdevname(device), routine);
725
                return 1;
726
        }
727
        if (info->magic != MGSL_MAGIC) {
728
                printk(badmagic, kdevname(device), routine);
729
                return 1;
730
        }
731
#else
732
        if (!info)
733
                return 1;
734
#endif
735
        return 0;
736
}
737
 
738
/* tty callbacks */
739
 
740
/* Called when a port is opened.  Init and enable port.
741
 */
742
static int open(struct tty_struct *tty, struct file *filp)
743
{
744
        SLMP_INFO *info;
745
        int retval, line;
746
        unsigned long flags;
747
 
748
        line = MINOR(tty->device) - tty->driver.minor_start;
749
        if ((line < 0) || (line >= synclinkmp_device_count)) {
750
                printk("%s(%d): open with illegal line #%d.\n",
751
                        __FILE__,__LINE__,line);
752
                return -ENODEV;
753
        }
754
 
755
        info = synclinkmp_device_list;
756
        while(info && info->line != line)
757
                info = info->next_device;
758
        if (sanity_check(info, tty->device, "open"))
759
                return -ENODEV;
760
        if ( info->init_error ) {
761
                printk("%s(%d):%s device is not allocated, init error=%d\n",
762
                        __FILE__,__LINE__,info->device_name,info->init_error);
763
                return -ENODEV;
764
        }
765
 
766
        tty->driver_data = info;
767
        info->tty = tty;
768
 
769
        if (debug_level >= DEBUG_LEVEL_INFO)
770
                printk("%s(%d):%s open(), old ref count = %d\n",
771
                         __FILE__,__LINE__,tty->driver.name, info->count);
772
 
773
        MOD_INC_USE_COUNT;
774
 
775
        /* If port is closing, signal caller to try again */
776
        if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
777
                if (info->flags & ASYNC_CLOSING)
778
                        interruptible_sleep_on(&info->close_wait);
779
                retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
780
                        -EAGAIN : -ERESTARTSYS);
781
                goto cleanup;
782
        }
783
 
784
        info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
785
 
786
        spin_lock_irqsave(&info->netlock, flags);
787
        if (info->netcount) {
788
                retval = -EBUSY;
789
                spin_unlock_irqrestore(&info->netlock, flags);
790
                goto cleanup;
791
        }
792
        info->count++;
793
        spin_unlock_irqrestore(&info->netlock, flags);
794
 
795
        if (info->count == 1) {
796
                /* 1st open on this device, init hardware */
797
                retval = startup(info);
798
                if (retval < 0)
799
                        goto cleanup;
800
        }
801
 
802
        retval = block_til_ready(tty, filp, info);
803
        if (retval) {
804
                if (debug_level >= DEBUG_LEVEL_INFO)
805
                        printk("%s(%d):%s block_til_ready() returned %d\n",
806
                                 __FILE__,__LINE__, info->device_name, retval);
807
                goto cleanup;
808
        }
809
 
810
        if ((info->count == 1) &&
811
            info->flags & ASYNC_SPLIT_TERMIOS) {
812
                if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
813
                        *tty->termios = info->normal_termios;
814
                else
815
                        *tty->termios = info->callout_termios;
816
                change_params(info);
817
        }
818
 
819
        info->session = current->session;
820
        info->pgrp    = current->pgrp;
821
 
822
        if (debug_level >= DEBUG_LEVEL_INFO)
823
                printk("%s(%d):%s open() success\n",
824
                         __FILE__,__LINE__, info->device_name);
825
        retval = 0;
826
 
827
cleanup:
828
        if (retval) {
829
                if (tty->count == 1)
830
                        info->tty = 0; /* tty layer will release tty struct */
831
                if(MOD_IN_USE)
832
                        MOD_DEC_USE_COUNT;
833
                if(info->count)
834
                        info->count--;
835
        }
836
 
837
        return retval;
838
}
839
 
840
/* Called when port is closed. Wait for remaining data to be
841
 * sent. Disable port and free resources.
842
 */
843
static void close(struct tty_struct *tty, struct file *filp)
844
{
845
        SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
846
 
847
        if (sanity_check(info, tty->device, "close"))
848
                return;
849
 
850
        if (debug_level >= DEBUG_LEVEL_INFO)
851
                printk("%s(%d):%s close() entry, count=%d\n",
852
                         __FILE__,__LINE__, info->device_name, info->count);
853
 
854
        if (!info->count)
855
                return;
856
 
857
        if (tty_hung_up_p(filp))
858
                goto cleanup;
859
 
860
        if ((tty->count == 1) && (info->count != 1)) {
861
                /*
862
                 * tty->count is 1 and the tty structure will be freed.
863
                 * info->count should be one in this case.
864
                 * if it's not, correct it so that the port is shutdown.
865
                 */
866
                printk("%s(%d):%s close: bad refcount; tty->count is 1, "
867
                       "info->count is %d\n",
868
                         __FILE__,__LINE__, info->device_name, info->count);
869
                info->count = 1;
870
        }
871
 
872
        info->count--;
873
 
874
        /* if at least one open remaining, leave hardware active */
875
        if (info->count)
876
                goto cleanup;
877
 
878
        info->flags |= ASYNC_CLOSING;
879
 
880
        /* Save the termios structure, since this port may have
881
         * separate termios for callout and dialin.
882
         */
883
        if (info->flags & ASYNC_NORMAL_ACTIVE)
884
                info->normal_termios = *tty->termios;
885
        if (info->flags & ASYNC_CALLOUT_ACTIVE)
886
                info->callout_termios = *tty->termios;
887
 
888
        /* set tty->closing to notify line discipline to
889
         * only process XON/XOFF characters. Only the N_TTY
890
         * discipline appears to use this (ppp does not).
891
         */
892
        tty->closing = 1;
893
 
894
        /* wait for transmit data to clear all layers */
895
 
896
        if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
897
                if (debug_level >= DEBUG_LEVEL_INFO)
898
                        printk("%s(%d):%s close() calling tty_wait_until_sent\n",
899
                                 __FILE__,__LINE__, info->device_name );
900
                tty_wait_until_sent(tty, info->closing_wait);
901
        }
902
 
903
        if (info->flags & ASYNC_INITIALIZED)
904
                wait_until_sent(tty, info->timeout);
905
 
906
        if (tty->driver.flush_buffer)
907
                tty->driver.flush_buffer(tty);
908
 
909
        if (tty->ldisc.flush_buffer)
910
                tty->ldisc.flush_buffer(tty);
911
 
912
        shutdown(info);
913
 
914
        tty->closing = 0;
915
        info->tty = 0;
916
 
917
        if (info->blocked_open) {
918
                if (info->close_delay) {
919
                        set_current_state(TASK_INTERRUPTIBLE);
920
                        schedule_timeout(info->close_delay);
921
                }
922
                wake_up_interruptible(&info->open_wait);
923
        }
924
 
925
        info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
926
                         ASYNC_CLOSING);
927
 
928
        wake_up_interruptible(&info->close_wait);
929
 
930
cleanup:
931
        if (debug_level >= DEBUG_LEVEL_INFO)
932
                printk("%s(%d):%s close() exit, count=%d\n", __FILE__,__LINE__,
933
                        tty->driver.name, info->count);
934
        if(MOD_IN_USE)
935
                MOD_DEC_USE_COUNT;
936
}
937
 
938
/* Called by tty_hangup() when a hangup is signaled.
939
 * This is the same as closing all open descriptors for the port.
940
 */
941
static void hangup(struct tty_struct *tty)
942
{
943
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
944
 
945
        if (debug_level >= DEBUG_LEVEL_INFO)
946
                printk("%s(%d):%s hangup()\n",
947
                         __FILE__,__LINE__, info->device_name );
948
 
949
        if (sanity_check(info, tty->device, "hangup"))
950
                return;
951
 
952
        flush_buffer(tty);
953
        shutdown(info);
954
 
955
        info->count = 0;
956
        info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
957
        info->tty = 0;
958
 
959
        wake_up_interruptible(&info->open_wait);
960
}
961
 
962
/* Set new termios settings
963
 */
964
static void set_termios(struct tty_struct *tty, struct termios *old_termios)
965
{
966
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
967
        unsigned long flags;
968
 
969
        if (debug_level >= DEBUG_LEVEL_INFO)
970
                printk("%s(%d):%s set_termios()\n", __FILE__,__LINE__,
971
                        tty->driver.name );
972
 
973
        /* just return if nothing has changed */
974
        if ((tty->termios->c_cflag == old_termios->c_cflag)
975
            && (RELEVANT_IFLAG(tty->termios->c_iflag)
976
                == RELEVANT_IFLAG(old_termios->c_iflag)))
977
          return;
978
 
979
        change_params(info);
980
 
981
        /* Handle transition to B0 status */
982
        if (old_termios->c_cflag & CBAUD &&
983
            !(tty->termios->c_cflag & CBAUD)) {
984
                info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
985
                spin_lock_irqsave(&info->lock,flags);
986
                set_signals(info);
987
                spin_unlock_irqrestore(&info->lock,flags);
988
        }
989
 
990
        /* Handle transition away from B0 status */
991
        if (!(old_termios->c_cflag & CBAUD) &&
992
            tty->termios->c_cflag & CBAUD) {
993
                info->serial_signals |= SerialSignal_DTR;
994
                if (!(tty->termios->c_cflag & CRTSCTS) ||
995
                    !test_bit(TTY_THROTTLED, &tty->flags)) {
996
                        info->serial_signals |= SerialSignal_RTS;
997
                }
998
                spin_lock_irqsave(&info->lock,flags);
999
                set_signals(info);
1000
                spin_unlock_irqrestore(&info->lock,flags);
1001
        }
1002
 
1003
        /* Handle turning off CRTSCTS */
1004
        if (old_termios->c_cflag & CRTSCTS &&
1005
            !(tty->termios->c_cflag & CRTSCTS)) {
1006
                tty->hw_stopped = 0;
1007
                tx_release(tty);
1008
        }
1009
}
1010
 
1011
/* Send a block of data
1012
 *
1013
 * Arguments:
1014
 *
1015
 *      tty             pointer to tty information structure
1016
 *      from_user       flag: 1 = from user process
1017
 *      buf             pointer to buffer containing send data
1018
 *      count           size of send data in bytes
1019
 *
1020
 * Return Value:        number of characters written
1021
 */
1022
static int write(struct tty_struct *tty, int from_user,
1023
                 const unsigned char *buf, int count)
1024
{
1025
        int     c, ret = 0, err;
1026
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1027
        unsigned long flags;
1028
 
1029
        if (debug_level >= DEBUG_LEVEL_INFO)
1030
                printk("%s(%d):%s write() count=%d\n",
1031
                       __FILE__,__LINE__,info->device_name,count);
1032
 
1033
        if (sanity_check(info, tty->device, "write"))
1034
                goto cleanup;
1035
 
1036
        if (!tty || !info->tx_buf)
1037
                goto cleanup;
1038
 
1039
        if (info->params.mode == MGSL_MODE_HDLC) {
1040
                if (count > info->max_frame_size) {
1041
                        ret = -EIO;
1042
                        goto cleanup;
1043
                }
1044
                if (info->tx_active)
1045
                        goto cleanup;
1046
                if (info->tx_count) {
1047
                        /* send accumulated data from send_char() calls */
1048
                        /* as frame and wait before accepting more data. */
1049
                        tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
1050
                        goto start;
1051
                }
1052
                if (!from_user) {
1053
                        ret = info->tx_count = count;
1054
                        tx_load_dma_buffer(info, buf, count);
1055
                        goto start;
1056
                }
1057
        }
1058
 
1059
        for (;;) {
1060
                c = MIN(count,
1061
                        MIN(info->max_frame_size - info->tx_count - 1,
1062
                            info->max_frame_size - info->tx_put));
1063
                if (c <= 0)
1064
                        break;
1065
 
1066
                if (from_user) {
1067
                        COPY_FROM_USER(err, info->tx_buf + info->tx_put, buf, c);
1068
                        if (err) {
1069
                                if (!ret)
1070
                                        ret = -EFAULT;
1071
                                break;
1072
                        }
1073
                } else
1074
                        memcpy(info->tx_buf + info->tx_put, buf, c);
1075
 
1076
                spin_lock_irqsave(&info->lock,flags);
1077
                info->tx_put += c;
1078
                if (info->tx_put >= info->max_frame_size)
1079
                        info->tx_put -= info->max_frame_size;
1080
                info->tx_count += c;
1081
                spin_unlock_irqrestore(&info->lock,flags);
1082
 
1083
                buf += c;
1084
                count -= c;
1085
                ret += c;
1086
        }
1087
 
1088
        if (info->params.mode == MGSL_MODE_HDLC) {
1089
                if (count) {
1090
                        ret = info->tx_count = 0;
1091
                        goto cleanup;
1092
                }
1093
                tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
1094
        }
1095
start:
1096
        if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1097
                spin_lock_irqsave(&info->lock,flags);
1098
                if (!info->tx_active)
1099
                        tx_start(info);
1100
                spin_unlock_irqrestore(&info->lock,flags);
1101
        }
1102
 
1103
cleanup:
1104
        if (debug_level >= DEBUG_LEVEL_INFO)
1105
                printk( "%s(%d):%s write() returning=%d\n",
1106
                        __FILE__,__LINE__,info->device_name,ret);
1107
        return ret;
1108
}
1109
 
1110
/* Add a character to the transmit buffer.
1111
 */
1112
static void put_char(struct tty_struct *tty, unsigned char ch)
1113
{
1114
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1115
        unsigned long flags;
1116
 
1117
        if ( debug_level >= DEBUG_LEVEL_INFO ) {
1118
                printk( "%s(%d):%s put_char(%d)\n",
1119
                        __FILE__,__LINE__,info->device_name,ch);
1120
        }
1121
 
1122
        if (sanity_check(info, tty->device, "put_char"))
1123
                return;
1124
 
1125
        if (!tty || !info->tx_buf)
1126
                return;
1127
 
1128
        spin_lock_irqsave(&info->lock,flags);
1129
 
1130
        if ( (info->params.mode != MGSL_MODE_HDLC) ||
1131
             !info->tx_active ) {
1132
 
1133
                if (info->tx_count < info->max_frame_size - 1) {
1134
                        info->tx_buf[info->tx_put++] = ch;
1135
                        if (info->tx_put >= info->max_frame_size)
1136
                                info->tx_put -= info->max_frame_size;
1137
                        info->tx_count++;
1138
                }
1139
        }
1140
 
1141
        spin_unlock_irqrestore(&info->lock,flags);
1142
}
1143
 
1144
/* Send a high-priority XON/XOFF character
1145
 */
1146
static void send_xchar(struct tty_struct *tty, char ch)
1147
{
1148
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1149
        unsigned long flags;
1150
 
1151
        if (debug_level >= DEBUG_LEVEL_INFO)
1152
                printk("%s(%d):%s send_xchar(%d)\n",
1153
                         __FILE__,__LINE__, info->device_name, ch );
1154
 
1155
        if (sanity_check(info, tty->device, "send_xchar"))
1156
                return;
1157
 
1158
        info->x_char = ch;
1159
        if (ch) {
1160
                /* Make sure transmit interrupts are on */
1161
                spin_lock_irqsave(&info->lock,flags);
1162
                if (!info->tx_enabled)
1163
                        tx_start(info);
1164
                spin_unlock_irqrestore(&info->lock,flags);
1165
        }
1166
}
1167
 
1168
/* Wait until the transmitter is empty.
1169
 */
1170
static void wait_until_sent(struct tty_struct *tty, int timeout)
1171
{
1172
        SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
1173
        unsigned long orig_jiffies, char_time;
1174
 
1175
        if (!info )
1176
                return;
1177
 
1178
        if (debug_level >= DEBUG_LEVEL_INFO)
1179
                printk("%s(%d):%s wait_until_sent() entry\n",
1180
                         __FILE__,__LINE__, info->device_name );
1181
 
1182
        if (sanity_check(info, tty->device, "wait_until_sent"))
1183
                return;
1184
 
1185
        if (!(info->flags & ASYNC_INITIALIZED))
1186
                goto exit;
1187
 
1188
        orig_jiffies = jiffies;
1189
 
1190
        /* Set check interval to 1/5 of estimated time to
1191
         * send a character, and make it at least 1. The check
1192
         * interval should also be less than the timeout.
1193
         * Note: use tight timings here to satisfy the NIST-PCTS.
1194
         */
1195
 
1196
        if ( info->params.data_rate ) {
1197
                char_time = info->timeout/(32 * 5);
1198
                if (!char_time)
1199
                        char_time++;
1200
        } else
1201
                char_time = 1;
1202
 
1203
        if (timeout)
1204
                char_time = MIN(char_time, timeout);
1205
 
1206
        if ( info->params.mode == MGSL_MODE_HDLC ) {
1207
                while (info->tx_active) {
1208
                        set_current_state(TASK_INTERRUPTIBLE);
1209
                        schedule_timeout(char_time);
1210
                        if (signal_pending(current))
1211
                                break;
1212
                        if (timeout && time_after(jiffies, orig_jiffies + timeout))
1213
                                break;
1214
                }
1215
        } else {
1216
                //TODO: determine if there is something similar to USC16C32
1217
                //      TXSTATUS_ALL_SENT status
1218
                while ( info->tx_active && info->tx_enabled) {
1219
                        set_current_state(TASK_INTERRUPTIBLE);
1220
                        schedule_timeout(char_time);
1221
                        if (signal_pending(current))
1222
                                break;
1223
                        if (timeout && time_after(jiffies, orig_jiffies + timeout))
1224
                                break;
1225
                }
1226
        }
1227
 
1228
exit:
1229
        if (debug_level >= DEBUG_LEVEL_INFO)
1230
                printk("%s(%d):%s wait_until_sent() exit\n",
1231
                         __FILE__,__LINE__, info->device_name );
1232
}
1233
 
1234
/* Return the count of free bytes in transmit buffer
1235
 */
1236
static int write_room(struct tty_struct *tty)
1237
{
1238
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1239
        int ret;
1240
 
1241
        if (sanity_check(info, tty->device, "write_room"))
1242
                return 0;
1243
 
1244
        if (info->params.mode == MGSL_MODE_HDLC) {
1245
                ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
1246
        } else {
1247
                ret = info->max_frame_size - info->tx_count - 1;
1248
                if (ret < 0)
1249
                        ret = 0;
1250
        }
1251
 
1252
        if (debug_level >= DEBUG_LEVEL_INFO)
1253
                printk("%s(%d):%s write_room()=%d\n",
1254
                       __FILE__, __LINE__, info->device_name, ret);
1255
 
1256
        return ret;
1257
}
1258
 
1259
/* enable transmitter and send remaining buffered characters
1260
 */
1261
static void flush_chars(struct tty_struct *tty)
1262
{
1263
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1264
        unsigned long flags;
1265
 
1266
        if ( debug_level >= DEBUG_LEVEL_INFO )
1267
                printk( "%s(%d):%s flush_chars() entry tx_count=%d\n",
1268
                        __FILE__,__LINE__,info->device_name,info->tx_count);
1269
 
1270
        if (sanity_check(info, tty->device, "flush_chars"))
1271
                return;
1272
 
1273
        if (info->tx_count <= 0 || tty->stopped || tty->hw_stopped ||
1274
            !info->tx_buf)
1275
                return;
1276
 
1277
        if ( debug_level >= DEBUG_LEVEL_INFO )
1278
                printk( "%s(%d):%s flush_chars() entry, starting transmitter\n",
1279
                        __FILE__,__LINE__,info->device_name );
1280
 
1281
        spin_lock_irqsave(&info->lock,flags);
1282
 
1283
        if (!info->tx_active) {
1284
                if ( (info->params.mode == MGSL_MODE_HDLC) &&
1285
                        info->tx_count ) {
1286
                        /* operating in synchronous (frame oriented) mode */
1287
                        /* copy data from circular tx_buf to */
1288
                        /* transmit DMA buffer. */
1289
                        tx_load_dma_buffer(info,
1290
                                 info->tx_buf,info->tx_count);
1291
                }
1292
                tx_start(info);
1293
        }
1294
 
1295
        spin_unlock_irqrestore(&info->lock,flags);
1296
}
1297
 
1298
/* Discard all data in the send buffer
1299
 */
1300
static void flush_buffer(struct tty_struct *tty)
1301
{
1302
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1303
        unsigned long flags;
1304
 
1305
        if (debug_level >= DEBUG_LEVEL_INFO)
1306
                printk("%s(%d):%s flush_buffer() entry\n",
1307
                         __FILE__,__LINE__, info->device_name );
1308
 
1309
        if (sanity_check(info, tty->device, "flush_buffer"))
1310
                return;
1311
 
1312
        spin_lock_irqsave(&info->lock,flags);
1313
        info->tx_count = info->tx_put = info->tx_get = 0;
1314
        del_timer(&info->tx_timer);
1315
        spin_unlock_irqrestore(&info->lock,flags);
1316
 
1317
        wake_up_interruptible(&tty->write_wait);
1318
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1319
            tty->ldisc.write_wakeup)
1320
                (tty->ldisc.write_wakeup)(tty);
1321
}
1322
 
1323
/* throttle (stop) transmitter
1324
 */
1325
static void tx_hold(struct tty_struct *tty)
1326
{
1327
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1328
        unsigned long flags;
1329
 
1330
        if (sanity_check(info, tty->device, "tx_hold"))
1331
                return;
1332
 
1333
        if ( debug_level >= DEBUG_LEVEL_INFO )
1334
                printk("%s(%d):%s tx_hold()\n",
1335
                        __FILE__,__LINE__,info->device_name);
1336
 
1337
        spin_lock_irqsave(&info->lock,flags);
1338
        if (info->tx_enabled)
1339
                tx_stop(info);
1340
        spin_unlock_irqrestore(&info->lock,flags);
1341
}
1342
 
1343
/* release (start) transmitter
1344
 */
1345
static void tx_release(struct tty_struct *tty)
1346
{
1347
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1348
        unsigned long flags;
1349
 
1350
        if (sanity_check(info, tty->device, "tx_release"))
1351
                return;
1352
 
1353
        if ( debug_level >= DEBUG_LEVEL_INFO )
1354
                printk("%s(%d):%s tx_release()\n",
1355
                        __FILE__,__LINE__,info->device_name);
1356
 
1357
        spin_lock_irqsave(&info->lock,flags);
1358
        if (!info->tx_enabled)
1359
                tx_start(info);
1360
        spin_unlock_irqrestore(&info->lock,flags);
1361
}
1362
 
1363
/* Service an IOCTL request
1364
 *
1365
 * Arguments:
1366
 *
1367
 *      tty     pointer to tty instance data
1368
 *      file    pointer to associated file object for device
1369
 *      cmd     IOCTL command code
1370
 *      arg     command argument/context
1371
 *
1372
 * Return Value:        0 if success, otherwise error code
1373
 */
1374
static int ioctl(struct tty_struct *tty, struct file *file,
1375
                 unsigned int cmd, unsigned long arg)
1376
{
1377
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1378
        int error;
1379
        struct mgsl_icount cnow;        /* kernel counter temps */
1380
        struct serial_icounter_struct *p_cuser; /* user space */
1381
        unsigned long flags;
1382
 
1383
        if (debug_level >= DEBUG_LEVEL_INFO)
1384
                printk("%s(%d):%s ioctl() cmd=%08X\n", __FILE__,__LINE__,
1385
                        info->device_name, cmd );
1386
 
1387
        if (sanity_check(info, tty->device, "ioctl"))
1388
                return -ENODEV;
1389
 
1390
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1391
            (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1392
                if (tty->flags & (1 << TTY_IO_ERROR))
1393
                    return -EIO;
1394
        }
1395
 
1396
        switch (cmd) {
1397
        case TIOCMGET:
1398
                return get_modem_info(info, (unsigned int *) arg);
1399
        case TIOCMBIS:
1400
        case TIOCMBIC:
1401
        case TIOCMSET:
1402
                return set_modem_info(info, cmd, (unsigned int *) arg);
1403
        case MGSL_IOCGPARAMS:
1404
                return get_params(info,(MGSL_PARAMS *)arg);
1405
        case MGSL_IOCSPARAMS:
1406
                return set_params(info,(MGSL_PARAMS *)arg);
1407
        case MGSL_IOCGTXIDLE:
1408
                return get_txidle(info,(int*)arg);
1409
        case MGSL_IOCSTXIDLE:
1410
                return set_txidle(info,(int)arg);
1411
        case MGSL_IOCTXENABLE:
1412
                return tx_enable(info,(int)arg);
1413
        case MGSL_IOCRXENABLE:
1414
                return rx_enable(info,(int)arg);
1415
        case MGSL_IOCTXABORT:
1416
                return tx_abort(info);
1417
        case MGSL_IOCGSTATS:
1418
                return get_stats(info,(struct mgsl_icount*)arg);
1419
        case MGSL_IOCWAITEVENT:
1420
                return wait_mgsl_event(info,(int*)arg);
1421
        case MGSL_IOCLOOPTXDONE:
1422
                return 0; // TODO: Not supported, need to document
1423
        case MGSL_IOCCLRMODCOUNT:
1424
                while(MOD_IN_USE)
1425
                        MOD_DEC_USE_COUNT;
1426
                return 0;
1427
                /* Wait for modem input (DCD,RI,DSR,CTS) change
1428
                 * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
1429
                 */
1430
        case TIOCMIWAIT:
1431
                return modem_input_wait(info,(int)arg);
1432
 
1433
                /*
1434
                 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1435
                 * Return: write counters to the user passed counter struct
1436
                 * NB: both 1->0 and 0->1 transitions are counted except for
1437
                 *     RI where only 0->1 is counted.
1438
                 */
1439
        case TIOCGICOUNT:
1440
                spin_lock_irqsave(&info->lock,flags);
1441
                cnow = info->icount;
1442
                spin_unlock_irqrestore(&info->lock,flags);
1443
                p_cuser = (struct serial_icounter_struct *) arg;
1444
                PUT_USER(error,cnow.cts, &p_cuser->cts);
1445
                if (error) return error;
1446
                PUT_USER(error,cnow.dsr, &p_cuser->dsr);
1447
                if (error) return error;
1448
                PUT_USER(error,cnow.rng, &p_cuser->rng);
1449
                if (error) return error;
1450
                PUT_USER(error,cnow.dcd, &p_cuser->dcd);
1451
                if (error) return error;
1452
                PUT_USER(error,cnow.rx, &p_cuser->rx);
1453
                if (error) return error;
1454
                PUT_USER(error,cnow.tx, &p_cuser->tx);
1455
                if (error) return error;
1456
                PUT_USER(error,cnow.frame, &p_cuser->frame);
1457
                if (error) return error;
1458
                PUT_USER(error,cnow.overrun, &p_cuser->overrun);
1459
                if (error) return error;
1460
                PUT_USER(error,cnow.parity, &p_cuser->parity);
1461
                if (error) return error;
1462
                PUT_USER(error,cnow.brk, &p_cuser->brk);
1463
                if (error) return error;
1464
                PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
1465
                if (error) return error;
1466
                return 0;
1467
        default:
1468
                return -ENOIOCTLCMD;
1469
        }
1470
        return 0;
1471
}
1472
 
1473
/*
1474
 * /proc fs routines....
1475
 */
1476
 
1477
static inline int line_info(char *buf, SLMP_INFO *info)
1478
{
1479
        char    stat_buf[30];
1480
        int     ret;
1481
        unsigned long flags;
1482
 
1483
        ret = sprintf(buf, "%s: SCABase=%08x Mem=%08X StatusControl=%08x LCR=%08X\n"
1484
                       "\tIRQ=%d MaxFrameSize=%u\n",
1485
                info->device_name,
1486
                info->phys_sca_base,
1487
                info->phys_memory_base,
1488
                info->phys_statctrl_base,
1489
                info->phys_lcr_base,
1490
                info->irq_level,
1491
                info->max_frame_size );
1492
 
1493
        /* output current serial signal states */
1494
        spin_lock_irqsave(&info->lock,flags);
1495
        get_signals(info);
1496
        spin_unlock_irqrestore(&info->lock,flags);
1497
 
1498
        stat_buf[0] = 0;
1499
        stat_buf[1] = 0;
1500
        if (info->serial_signals & SerialSignal_RTS)
1501
                strcat(stat_buf, "|RTS");
1502
        if (info->serial_signals & SerialSignal_CTS)
1503
                strcat(stat_buf, "|CTS");
1504
        if (info->serial_signals & SerialSignal_DTR)
1505
                strcat(stat_buf, "|DTR");
1506
        if (info->serial_signals & SerialSignal_DSR)
1507
                strcat(stat_buf, "|DSR");
1508
        if (info->serial_signals & SerialSignal_DCD)
1509
                strcat(stat_buf, "|CD");
1510
        if (info->serial_signals & SerialSignal_RI)
1511
                strcat(stat_buf, "|RI");
1512
 
1513
        if (info->params.mode == MGSL_MODE_HDLC) {
1514
                ret += sprintf(buf+ret, "\tHDLC txok:%d rxok:%d",
1515
                              info->icount.txok, info->icount.rxok);
1516
                if (info->icount.txunder)
1517
                        ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
1518
                if (info->icount.txabort)
1519
                        ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
1520
                if (info->icount.rxshort)
1521
                        ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
1522
                if (info->icount.rxlong)
1523
                        ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
1524
                if (info->icount.rxover)
1525
                        ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
1526
                if (info->icount.rxcrc)
1527
                        ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxcrc);
1528
        } else {
1529
                ret += sprintf(buf+ret, "\tASYNC tx:%d rx:%d",
1530
                              info->icount.tx, info->icount.rx);
1531
                if (info->icount.frame)
1532
                        ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
1533
                if (info->icount.parity)
1534
                        ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
1535
                if (info->icount.brk)
1536
                        ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
1537
                if (info->icount.overrun)
1538
                        ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
1539
        }
1540
 
1541
        /* Append serial signal status to end */
1542
        ret += sprintf(buf+ret, " %s\n", stat_buf+1);
1543
 
1544
        ret += sprintf(buf+ret, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1545
         info->tx_active,info->bh_requested,info->bh_running,
1546
         info->pending_bh);
1547
 
1548
        return ret;
1549
}
1550
 
1551
/* Called to print information about devices
1552
 */
1553
int read_proc(char *page, char **start, off_t off, int count,
1554
              int *eof, void *data)
1555
{
1556
        int len = 0, l;
1557
        off_t   begin = 0;
1558
        SLMP_INFO *info;
1559
 
1560
        len += sprintf(page, "synclinkmp driver:%s\n", driver_version);
1561
 
1562
        info = synclinkmp_device_list;
1563
        while( info ) {
1564
                l = line_info(page + len, info);
1565
                len += l;
1566
                if (len+begin > off+count)
1567
                        goto done;
1568
                if (len+begin < off) {
1569
                        begin += len;
1570
                        len = 0;
1571
                }
1572
                info = info->next_device;
1573
        }
1574
 
1575
        *eof = 1;
1576
done:
1577
        if (off >= len+begin)
1578
                return 0;
1579
        *start = page + (off-begin);
1580
        return ((count < begin+len-off) ? count : begin+len-off);
1581
}
1582
 
1583
/* Return the count of bytes in transmit buffer
1584
 */
1585
static int chars_in_buffer(struct tty_struct *tty)
1586
{
1587
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1588
 
1589
        if (sanity_check(info, tty->device, "chars_in_buffer"))
1590
                return 0;
1591
 
1592
        if (debug_level >= DEBUG_LEVEL_INFO)
1593
                printk("%s(%d):%s chars_in_buffer()=%d\n",
1594
                       __FILE__, __LINE__, info->device_name, info->tx_count);
1595
 
1596
        return info->tx_count;
1597
}
1598
 
1599
/* Signal remote device to throttle send data (our receive data)
1600
 */
1601
static void throttle(struct tty_struct * tty)
1602
{
1603
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1604
        unsigned long flags;
1605
 
1606
        if (debug_level >= DEBUG_LEVEL_INFO)
1607
                printk("%s(%d):%s throttle() entry\n",
1608
                         __FILE__,__LINE__, info->device_name );
1609
 
1610
        if (sanity_check(info, tty->device, "throttle"))
1611
                return;
1612
 
1613
        if (I_IXOFF(tty))
1614
                send_xchar(tty, STOP_CHAR(tty));
1615
 
1616
        if (tty->termios->c_cflag & CRTSCTS) {
1617
                spin_lock_irqsave(&info->lock,flags);
1618
                info->serial_signals &= ~SerialSignal_RTS;
1619
                set_signals(info);
1620
                spin_unlock_irqrestore(&info->lock,flags);
1621
        }
1622
}
1623
 
1624
/* Signal remote device to stop throttling send data (our receive data)
1625
 */
1626
static void unthrottle(struct tty_struct * tty)
1627
{
1628
        SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
1629
        unsigned long flags;
1630
 
1631
        if (debug_level >= DEBUG_LEVEL_INFO)
1632
                printk("%s(%d):%s unthrottle() entry\n",
1633
                         __FILE__,__LINE__, info->device_name );
1634
 
1635
        if (sanity_check(info, tty->device, "unthrottle"))
1636
                return;
1637
 
1638
        if (I_IXOFF(tty)) {
1639
                if (info->x_char)
1640
                        info->x_char = 0;
1641
                else
1642
                        send_xchar(tty, START_CHAR(tty));
1643
        }
1644
 
1645
        if (tty->termios->c_cflag & CRTSCTS) {
1646
                spin_lock_irqsave(&info->lock,flags);
1647
                info->serial_signals |= SerialSignal_RTS;
1648
                set_signals(info);
1649
                spin_unlock_irqrestore(&info->lock,flags);
1650
        }
1651
}
1652
 
1653
/* set or clear transmit break condition
1654
 * break_state  -1=set break condition, 0=clear
1655
 */
1656
static void set_break(struct tty_struct *tty, int break_state)
1657
{
1658
        unsigned char RegValue;
1659
        SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
1660
        unsigned long flags;
1661
 
1662
        if (debug_level >= DEBUG_LEVEL_INFO)
1663
                printk("%s(%d):%s set_break(%d)\n",
1664
                         __FILE__,__LINE__, info->device_name, break_state);
1665
 
1666
        if (sanity_check(info, tty->device, "set_break"))
1667
                return;
1668
 
1669
        spin_lock_irqsave(&info->lock,flags);
1670
        RegValue = read_reg(info, CTL);
1671
        if (break_state == -1)
1672
                RegValue |= BIT3;
1673
        else
1674
                RegValue &= ~BIT3;
1675
        write_reg(info, CTL, RegValue);
1676
        spin_unlock_irqrestore(&info->lock,flags);
1677
}
1678
 
1679
#ifdef CONFIG_SYNCLINK_SYNCPPP
1680
 
1681
/* syncppp support and callbacks */
1682
 
1683
static void sppp_init(SLMP_INFO *info)
1684
{
1685
        struct net_device *d;
1686
 
1687
        sprintf(info->netname,"mgslm%dp%d",info->adapter_num,info->port_num);
1688
 
1689
        info->if_ptr = &info->pppdev;
1690
        info->netdev = info->pppdev.dev = &info->netdevice;
1691
 
1692
        sppp_attach(&info->pppdev);
1693
 
1694
        d = info->netdev;
1695
        strcpy(d->name,info->netname);
1696
        d->base_addr = 0;
1697
        d->irq = info->irq_level;
1698
        d->dma = 0;
1699
        d->priv = info;
1700
        d->init = NULL;
1701
        d->open = sppp_cb_open;
1702
        d->stop = sppp_cb_close;
1703
        d->hard_start_xmit = sppp_cb_tx;
1704
        d->do_ioctl = sppp_cb_ioctl;
1705
        d->get_stats = sppp_cb_net_stats;
1706
        d->tx_timeout = sppp_cb_tx_timeout;
1707
        d->watchdog_timeo = 10*HZ;
1708
 
1709
#if LINUX_VERSION_CODE < VERSION(2,4,4) 
1710
        dev_init_buffers(d);
1711
#endif
1712
 
1713
        if (register_netdev(d) == -1) {
1714
                printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
1715
                sppp_detach(info->netdev);
1716
                return;
1717
        }
1718
 
1719
        if (debug_level >= DEBUG_LEVEL_INFO)
1720
                printk("sppp_init(%s)\n",info->netname);
1721
}
1722
 
1723
static void sppp_delete(SLMP_INFO *info)
1724
{
1725
        if (debug_level >= DEBUG_LEVEL_INFO)
1726
                printk("sppp_delete(%s)\n",info->netname);
1727
        sppp_detach(info->netdev);
1728
        unregister_netdev(info->netdev);
1729
}
1730
 
1731
static int sppp_cb_open(struct net_device *d)
1732
{
1733
        SLMP_INFO *info = d->priv;
1734
        int err, flags;
1735
 
1736
        if (debug_level >= DEBUG_LEVEL_INFO)
1737
                printk("sppp_cb_open(%s)\n",info->netname);
1738
 
1739
        spin_lock_irqsave(&info->netlock, flags);
1740
        if (info->count != 0 || info->netcount != 0) {
1741
                printk(KERN_WARNING "%s: sppp_cb_open returning busy\n", info->netname);
1742
                spin_unlock_irqrestore(&info->netlock, flags);
1743
                return -EBUSY;
1744
        }
1745
        info->netcount=1;
1746
        MOD_INC_USE_COUNT;
1747
        spin_unlock_irqrestore(&info->netlock, flags);
1748
 
1749
        /* claim resources and init adapter */
1750
        if ((err = startup(info)) != 0)
1751
                goto open_fail;
1752
 
1753
        /* allow syncppp module to do open processing */
1754
        if ((err = sppp_open(d)) != 0) {
1755
                shutdown(info);
1756
                goto open_fail;
1757
        }
1758
 
1759
        info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1760
        program_hw(info);
1761
 
1762
        d->trans_start = jiffies;
1763
        netif_start_queue(d);
1764
        return 0;
1765
 
1766
open_fail:
1767
        spin_lock_irqsave(&info->netlock, flags);
1768
        info->netcount=0;
1769
        MOD_DEC_USE_COUNT;
1770
        spin_unlock_irqrestore(&info->netlock, flags);
1771
        return err;
1772
}
1773
 
1774
static void sppp_cb_tx_timeout(struct net_device *dev)
1775
{
1776
        SLMP_INFO *info = dev->priv;
1777
        int flags;
1778
 
1779
        if (debug_level >= DEBUG_LEVEL_INFO)
1780
                printk("sppp_tx_timeout(%s)\n",info->netname);
1781
 
1782
        info->netstats.tx_errors++;
1783
        info->netstats.tx_aborted_errors++;
1784
 
1785
        spin_lock_irqsave(&info->lock,flags);
1786
        tx_stop(info);
1787
        spin_unlock_irqrestore(&info->lock,flags);
1788
 
1789
        netif_wake_queue(dev);
1790
}
1791
 
1792
static int sppp_cb_tx(struct sk_buff *skb, struct net_device *dev)
1793
{
1794
        SLMP_INFO *info = dev->priv;
1795
        unsigned long flags;
1796
 
1797
        if (debug_level >= DEBUG_LEVEL_INFO)
1798
                printk("sppp_tx(%s)\n",info->netname);
1799
 
1800
        netif_stop_queue(dev);
1801
 
1802
        info->tx_count = skb->len;
1803
        tx_load_dma_buffer(info, skb->data, skb->len);
1804
        info->netstats.tx_packets++;
1805
        info->netstats.tx_bytes += skb->len;
1806
        dev_kfree_skb(skb);
1807
 
1808
        dev->trans_start = jiffies;
1809
 
1810
        spin_lock_irqsave(&info->lock,flags);
1811
        if (!info->tx_active)
1812
                tx_start(info);
1813
        spin_unlock_irqrestore(&info->lock,flags);
1814
 
1815
        return 0;
1816
}
1817
 
1818
static int sppp_cb_close(struct net_device *d)
1819
{
1820
        SLMP_INFO *info = d->priv;
1821
        unsigned long flags;
1822
 
1823
        if (debug_level >= DEBUG_LEVEL_INFO)
1824
                printk("sppp_cb_close(%s)\n",info->netname);
1825
 
1826
        /* shutdown adapter and release resources */
1827
        shutdown(info);
1828
 
1829
        /* allow syncppp to do close processing */
1830
        sppp_close(d);
1831
        netif_stop_queue(d);
1832
 
1833
        spin_lock_irqsave(&info->netlock, flags);
1834
        info->netcount=0;
1835
        MOD_DEC_USE_COUNT;
1836
        spin_unlock_irqrestore(&info->netlock, flags);
1837
        return 0;
1838
}
1839
 
1840
static void sppp_rx_done(SLMP_INFO *info, char *buf, int size)
1841
{
1842
        struct sk_buff *skb = dev_alloc_skb(size);
1843
        if (debug_level >= DEBUG_LEVEL_INFO)
1844
                printk("sppp_rx_done(%s)\n",info->netname);
1845
        if (skb == NULL) {
1846
                printk(KERN_NOTICE "%s: cant alloc skb, dropping packet\n",
1847
                        info->netname);
1848
                info->netstats.rx_dropped++;
1849
                return;
1850
        }
1851
 
1852
        memcpy(skb_put(skb, size),buf,size);
1853
 
1854
        skb->protocol = htons(ETH_P_WAN_PPP);
1855
        skb->dev = info->netdev;
1856
        skb->mac.raw = skb->data;
1857
        info->netstats.rx_packets++;
1858
        info->netstats.rx_bytes += size;
1859
        netif_rx(skb);
1860
        info->netdev->trans_start = jiffies;
1861
}
1862
 
1863
static void sppp_tx_done(SLMP_INFO *info)
1864
{
1865
        if (netif_queue_stopped(info->netdev))
1866
            netif_wake_queue(info->netdev);
1867
}
1868
 
1869
static struct net_device_stats *sppp_cb_net_stats(struct net_device *dev)
1870
{
1871
        SLMP_INFO *info = dev->priv;
1872
        if (debug_level >= DEBUG_LEVEL_INFO)
1873
                printk("net_stats(%s)\n",info->netname);
1874
        return &info->netstats;
1875
}
1876
 
1877
static int sppp_cb_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1878
{
1879
        SLMP_INFO *info = (SLMP_INFO *)dev->priv;
1880
        if (debug_level >= DEBUG_LEVEL_INFO)
1881
                printk("%s(%d):ioctl %s cmd=%08X\n", __FILE__,__LINE__,
1882
                        info->netname, cmd );
1883
        return sppp_do_ioctl(dev, ifr, cmd);
1884
}
1885
 
1886
#endif /* ifdef CONFIG_SYNCLINK_SYNCPPP */
1887
 
1888
 
1889
/* Return next bottom half action to perform.
1890
 * Return Value:        BH action code or 0 if nothing to do.
1891
 */
1892
int bh_action(SLMP_INFO *info)
1893
{
1894
        unsigned long flags;
1895
        int rc = 0;
1896
 
1897
        spin_lock_irqsave(&info->lock,flags);
1898
 
1899
        if (info->pending_bh & BH_RECEIVE) {
1900
                info->pending_bh &= ~BH_RECEIVE;
1901
                rc = BH_RECEIVE;
1902
        } else if (info->pending_bh & BH_TRANSMIT) {
1903
                info->pending_bh &= ~BH_TRANSMIT;
1904
                rc = BH_TRANSMIT;
1905
        } else if (info->pending_bh & BH_STATUS) {
1906
                info->pending_bh &= ~BH_STATUS;
1907
                rc = BH_STATUS;
1908
        }
1909
 
1910
        if (!rc) {
1911
                /* Mark BH routine as complete */
1912
                info->bh_running   = 0;
1913
                info->bh_requested = 0;
1914
        }
1915
 
1916
        spin_unlock_irqrestore(&info->lock,flags);
1917
 
1918
        return rc;
1919
}
1920
 
1921
/* Perform bottom half processing of work items queued by ISR.
1922
 */
1923
void bh_handler(void* Context)
1924
{
1925
        SLMP_INFO *info = (SLMP_INFO*)Context;
1926
        int action;
1927
 
1928
        if (!info)
1929
                return;
1930
 
1931
        if ( debug_level >= DEBUG_LEVEL_BH )
1932
                printk( "%s(%d):%s bh_handler() entry\n",
1933
                        __FILE__,__LINE__,info->device_name);
1934
 
1935
        info->bh_running = 1;
1936
 
1937
        while((action = bh_action(info)) != 0) {
1938
 
1939
                /* Process work item */
1940
                if ( debug_level >= DEBUG_LEVEL_BH )
1941
                        printk( "%s(%d):%s bh_handler() work item action=%d\n",
1942
                                __FILE__,__LINE__,info->device_name, action);
1943
 
1944
                switch (action) {
1945
 
1946
                case BH_RECEIVE:
1947
                        bh_receive(info);
1948
                        break;
1949
                case BH_TRANSMIT:
1950
                        bh_transmit(info);
1951
                        break;
1952
                case BH_STATUS:
1953
                        bh_status(info);
1954
                        break;
1955
                default:
1956
                        /* unknown work item ID */
1957
                        printk("%s(%d):%s Unknown work item ID=%08X!\n",
1958
                                __FILE__,__LINE__,info->device_name,action);
1959
                        break;
1960
                }
1961
        }
1962
 
1963
        if ( debug_level >= DEBUG_LEVEL_BH )
1964
                printk( "%s(%d):%s bh_handler() exit\n",
1965
                        __FILE__,__LINE__,info->device_name);
1966
}
1967
 
1968
void bh_receive(SLMP_INFO *info)
1969
{
1970
        if ( debug_level >= DEBUG_LEVEL_BH )
1971
                printk( "%s(%d):%s bh_receive()\n",
1972
                        __FILE__,__LINE__,info->device_name);
1973
 
1974
        while( rx_get_frame(info) );
1975
}
1976
 
1977
void bh_transmit(SLMP_INFO *info)
1978
{
1979
        struct tty_struct *tty = info->tty;
1980
 
1981
        if ( debug_level >= DEBUG_LEVEL_BH )
1982
                printk( "%s(%d):%s bh_transmit() entry\n",
1983
                        __FILE__,__LINE__,info->device_name);
1984
 
1985
        if (tty) {
1986
                if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1987
                    tty->ldisc.write_wakeup) {
1988
                        if ( debug_level >= DEBUG_LEVEL_BH )
1989
                                printk( "%s(%d):%s calling ldisc.write_wakeup\n",
1990
                                        __FILE__,__LINE__,info->device_name);
1991
                        (tty->ldisc.write_wakeup)(tty);
1992
                }
1993
                wake_up_interruptible(&tty->write_wait);
1994
        }
1995
}
1996
 
1997
void bh_status(SLMP_INFO *info)
1998
{
1999
        if ( debug_level >= DEBUG_LEVEL_BH )
2000
                printk( "%s(%d):%s bh_status() entry\n",
2001
                        __FILE__,__LINE__,info->device_name);
2002
 
2003
        info->ri_chkcount = 0;
2004
        info->dsr_chkcount = 0;
2005
        info->dcd_chkcount = 0;
2006
        info->cts_chkcount = 0;
2007
}
2008
 
2009
void isr_timer(SLMP_INFO * info)
2010
{
2011
        unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
2012
 
2013
        /* IER2<7..4> = timer<3..0> interrupt enables (0=disabled) */
2014
        write_reg(info, IER2, 0);
2015
 
2016
        /* TMCS, Timer Control/Status Register
2017
         *
2018
         * 07      CMF, Compare match flag (read only) 1=match
2019
         * 06      ECMI, CMF Interrupt Enable: 0=disabled
2020
         * 05      Reserved, must be 0
2021
         * 04      TME, Timer Enable
2022
         * 03..00  Reserved, must be 0
2023
         *
2024
         * 0000 0000
2025
         */
2026
        write_reg(info, (unsigned char)(timer + TMCS), 0);
2027
 
2028
        info->irq_occurred = TRUE;
2029
 
2030
        if ( debug_level >= DEBUG_LEVEL_ISR )
2031
                printk("%s(%d):%s isr_timer()\n",
2032
                        __FILE__,__LINE__,info->device_name);
2033
}
2034
 
2035
void isr_rxint(SLMP_INFO * info)
2036
{
2037
        struct tty_struct *tty = info->tty;
2038
        struct  mgsl_icount *icount = &info->icount;
2039
        unsigned char status = read_reg(info, SR1);
2040
        unsigned char status2 = read_reg(info, SR2);
2041
 
2042
        /* clear status bits */
2043
        if ( status & (FLGD + IDLD + CDCD + BRKD) )
2044
                write_reg(info, SR1,
2045
                                (unsigned char)(status & (FLGD + IDLD + CDCD + BRKD)));
2046
 
2047
        if ( status2 & OVRN )
2048
                write_reg(info, SR2, (unsigned char)(status2 & OVRN));
2049
 
2050
        if ( debug_level >= DEBUG_LEVEL_ISR )
2051
                printk("%s(%d):%s isr_rxint status=%02X %02x\n",
2052
                        __FILE__,__LINE__,info->device_name,status,status2);
2053
 
2054
        if (info->params.mode == MGSL_MODE_ASYNC) {
2055
                if (status & BRKD) {
2056
                        icount->brk++;
2057
 
2058
                        /* process break detection if tty control
2059
                         * is not set to ignore it
2060
                         */
2061
                        if ( tty ) {
2062
                                if (!(status & info->ignore_status_mask1)) {
2063
                                        if (info->read_status_mask1 & BRKD) {
2064
                                                *tty->flip.flag_buf_ptr = TTY_BREAK;
2065
                                                if (info->flags & ASYNC_SAK)
2066
                                                        do_SAK(tty);
2067
                                        }
2068
                                }
2069
                        }
2070
                }
2071
        }
2072
        else {
2073
                if (status & (FLGD|IDLD)) {
2074
                        if (status & FLGD)
2075
                                info->icount.exithunt++;
2076
                        else if (status & IDLD)
2077
                                info->icount.rxidle++;
2078
                        wake_up_interruptible(&info->event_wait_q);
2079
                }
2080
        }
2081
 
2082
        if (status & CDCD) {
2083
                /* simulate a common modem status change interrupt
2084
                 * for our handler
2085
                 */
2086
                get_signals( info );
2087
                isr_io_pin(info,
2088
                        MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD));
2089
        }
2090
}
2091
 
2092
/*
2093
 * handle async rx data interrupts
2094
 */
2095
void isr_rxrdy(SLMP_INFO * info)
2096
{
2097
        u16 status;
2098
        unsigned char DataByte;
2099
        struct tty_struct *tty = info->tty;
2100
        struct  mgsl_icount *icount = &info->icount;
2101
 
2102
        if ( debug_level >= DEBUG_LEVEL_ISR )
2103
                printk("%s(%d):%s isr_rxrdy\n",
2104
                        __FILE__,__LINE__,info->device_name);
2105
 
2106
        while((status = read_reg(info,CST0)) & BIT0)
2107
        {
2108
                DataByte = read_reg(info,TRB);
2109
 
2110
                if ( tty ) {
2111
                        if (tty->flip.count >= TTY_FLIPBUF_SIZE)
2112
                                continue;
2113
 
2114
                        *tty->flip.char_buf_ptr = DataByte;
2115
                        *tty->flip.flag_buf_ptr = 0;
2116
                }
2117
 
2118
                icount->rx++;
2119
 
2120
                if ( status & (PE + FRME + OVRN) ) {
2121
                        printk("%s(%d):%s rxerr=%04X\n",
2122
                                __FILE__,__LINE__,info->device_name,status);
2123
 
2124
                        /* update error statistics */
2125
                        if (status & PE)
2126
                                icount->parity++;
2127
                        else if (status & FRME)
2128
                                icount->frame++;
2129
                        else if (status & OVRN)
2130
                                icount->overrun++;
2131
 
2132
                        /* discard char if tty control flags say so */
2133
                        if (status & info->ignore_status_mask2)
2134
                                continue;
2135
 
2136
                        status &= info->read_status_mask2;
2137
 
2138
                        if ( tty ) {
2139
                                if (status & PE)
2140
                                        *tty->flip.flag_buf_ptr = TTY_PARITY;
2141
                                else if (status & FRME)
2142
                                        *tty->flip.flag_buf_ptr = TTY_FRAME;
2143
                                if (status & OVRN) {
2144
                                        /* Overrun is special, since it's
2145
                                         * reported immediately, and doesn't
2146
                                         * affect the current character
2147
                                         */
2148
                                        if (tty->flip.count < TTY_FLIPBUF_SIZE) {
2149
                                                tty->flip.count++;
2150
                                                tty->flip.flag_buf_ptr++;
2151
                                                tty->flip.char_buf_ptr++;
2152
                                                *tty->flip.flag_buf_ptr = TTY_OVERRUN;
2153
                                        }
2154
                                }
2155
                        }
2156
                }       /* end of if (error) */
2157
 
2158
                if ( tty ) {
2159
                        tty->flip.flag_buf_ptr++;
2160
                        tty->flip.char_buf_ptr++;
2161
                        tty->flip.count++;
2162
                }
2163
        }
2164
 
2165
        if ( debug_level >= DEBUG_LEVEL_ISR ) {
2166
                printk("%s(%d):%s isr_rxrdy() flip count=%d\n",
2167
                        __FILE__,__LINE__,info->device_name,
2168
                        tty ? tty->flip.count : 0);
2169
                printk("%s(%d):%s rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
2170
                        __FILE__,__LINE__,info->device_name,
2171
                        icount->rx,icount->brk,icount->parity,
2172
                        icount->frame,icount->overrun);
2173
        }
2174
 
2175
        if ( tty && tty->flip.count )
2176
                tty_flip_buffer_push(tty);
2177
}
2178
 
2179
void isr_txeom(SLMP_INFO * info, unsigned char status)
2180
{
2181
        if ( debug_level >= DEBUG_LEVEL_ISR )
2182
                printk("%s(%d):%s isr_txeom status=%02x\n",
2183
                        __FILE__,__LINE__,info->device_name,status);
2184
 
2185
        /* disable and clear MSCI interrupts */
2186
        info->ie1_value &= ~(IDLE + UDRN);
2187
        write_reg(info, IE1, info->ie1_value);
2188
        write_reg(info, SR1, (unsigned char)(UDRN + IDLE));
2189
 
2190
        write_reg(info, TXDMA + DIR, 0x00); /* disable Tx DMA IRQs */
2191
        write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
2192
        write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
2193
 
2194
        if ( info->tx_active ) {
2195
                if (info->params.mode != MGSL_MODE_ASYNC) {
2196
                        if (status & UDRN)
2197
                                info->icount.txunder++;
2198
                        else if (status & IDLE)
2199
                                info->icount.txok++;
2200
                }
2201
 
2202
                info->tx_active = 0;
2203
                info->tx_count = info->tx_put = info->tx_get = 0;
2204
 
2205
                del_timer(&info->tx_timer);
2206
 
2207
                if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done ) {
2208
                        info->serial_signals &= ~SerialSignal_RTS;
2209
                        info->drop_rts_on_tx_done = 0;
2210
                        set_signals(info);
2211
                }
2212
 
2213
#ifdef CONFIG_SYNCLINK_SYNCPPP
2214
                if (info->netcount)
2215
                        sppp_tx_done(info);
2216
                else
2217
#endif
2218
                {
2219
                        if (info->tty && (info->tty->stopped || info->tty->hw_stopped)) {
2220
                                tx_stop(info);
2221
                                return;
2222
                        }
2223
                        info->pending_bh |= BH_TRANSMIT;
2224
                }
2225
        }
2226
}
2227
 
2228
 
2229
/*
2230
 * handle tx status interrupts
2231
 */
2232
void isr_txint(SLMP_INFO * info)
2233
{
2234
        unsigned char status = read_reg(info, SR1);
2235
 
2236
        /* clear status bits */
2237
        write_reg(info, SR1, (unsigned char)(status & (UDRN + IDLE + CCTS)));
2238
 
2239
        if ( debug_level >= DEBUG_LEVEL_ISR )
2240
                printk("%s(%d):%s isr_txint status=%02x\n",
2241
                        __FILE__,__LINE__,info->device_name,status);
2242
 
2243
        if (status & (UDRN + IDLE))
2244
                isr_txeom(info, status);
2245
 
2246
        if (status & CCTS) {
2247
                /* simulate a common modem status change interrupt
2248
                 * for our handler
2249
                 */
2250
                get_signals( info );
2251
                isr_io_pin(info,
2252
                        MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS));
2253
 
2254
        }
2255
}
2256
 
2257
/*
2258
 * handle async tx data interrupts
2259
 */
2260
void isr_txrdy(SLMP_INFO * info)
2261
{
2262
        if ( debug_level >= DEBUG_LEVEL_ISR )
2263
                printk("%s(%d):%s isr_txrdy() tx_count=%d\n",
2264
                        __FILE__,__LINE__,info->device_name,info->tx_count);
2265
 
2266
        if (info->tty && (info->tty->stopped || info->tty->hw_stopped)) {
2267
                tx_stop(info);
2268
                return;
2269
        }
2270
 
2271
        if ( info->tx_count )
2272
                tx_load_fifo( info );
2273
        else {
2274
                info->tx_active = 0;
2275
                info->ie0_value &= ~TXRDYE;
2276
                write_reg(info, IE0, info->ie0_value);
2277
        }
2278
 
2279
        if (info->tx_count < WAKEUP_CHARS)
2280
                info->pending_bh |= BH_TRANSMIT;
2281
}
2282
 
2283
void isr_rxdmaok(SLMP_INFO * info)
2284
{
2285
        /* BIT7 = EOT (end of transfer)
2286
         * BIT6 = EOM (end of message/frame)
2287
         */
2288
        unsigned char status = read_reg(info,RXDMA + DSR) & 0xc0;
2289
 
2290
        /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
2291
        write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
2292
 
2293
        if ( debug_level >= DEBUG_LEVEL_ISR )
2294
                printk("%s(%d):%s isr_rxdmaok(), status=%02x\n",
2295
                        __FILE__,__LINE__,info->device_name,status);
2296
 
2297
        info->pending_bh |= BH_RECEIVE;
2298
}
2299
 
2300
void isr_rxdmaerror(SLMP_INFO * info)
2301
{
2302
        /* BIT5 = BOF (buffer overflow)
2303
         * BIT4 = COF (counter overflow)
2304
         */
2305
        unsigned char status = read_reg(info,RXDMA + DSR) & 0x30;
2306
 
2307
        /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
2308
        write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
2309
 
2310
        if ( debug_level >= DEBUG_LEVEL_ISR )
2311
                printk("%s(%d):%s isr_rxdmaerror(), status=%02x\n",
2312
                        __FILE__,__LINE__,info->device_name,status);
2313
 
2314
        info->rx_overflow = TRUE;
2315
        info->pending_bh |= BH_RECEIVE;
2316
}
2317
 
2318
void isr_txdmaok(SLMP_INFO * info)
2319
{
2320
        /* BIT7 = EOT (end of transfer, used for async mode)
2321
         * BIT6 = EOM (end of message/frame, used for sync mode)
2322
         *
2323
         * We don't look at DMA status because only EOT is enabled
2324
         * and we always clear and disable all tx DMA IRQs.
2325
         */
2326
//      unsigned char dma_status = read_reg(info,TXDMA + DSR) & 0xc0;
2327
        unsigned char status_reg1 = read_reg(info, SR1);
2328
 
2329
        write_reg(info, TXDMA + DIR, 0x00);     /* disable Tx DMA IRQs */
2330
        write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
2331
        write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
2332
 
2333
        if ( debug_level >= DEBUG_LEVEL_ISR )
2334
                printk("%s(%d):%s isr_txdmaok(), status=%02x\n",
2335
                        __FILE__,__LINE__,info->device_name,status_reg1);
2336
 
2337
        /* If transmitter already idle, do end of frame processing,
2338
         * otherwise enable interrupt for tx IDLE.
2339
         */
2340
        if (status_reg1 & IDLE)
2341
                isr_txeom(info, IDLE);
2342
        else {
2343
                /* disable and clear underrun IRQ, enable IDLE interrupt */
2344
                info->ie1_value |= IDLE;
2345
                info->ie1_value &= ~UDRN;
2346
                write_reg(info, IE1, info->ie1_value);
2347
 
2348
                write_reg(info, SR1, UDRN);
2349
        }
2350
}
2351
 
2352
void isr_txdmaerror(SLMP_INFO * info)
2353
{
2354
        /* BIT5 = BOF (buffer overflow)
2355
         * BIT4 = COF (counter overflow)
2356
         */
2357
        unsigned char status = read_reg(info,TXDMA + DSR) & 0x30;
2358
 
2359
        /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
2360
        write_reg(info, TXDMA + DSR, (unsigned char)(status | 1));
2361
 
2362
        if ( debug_level >= DEBUG_LEVEL_ISR )
2363
                printk("%s(%d):%s isr_txdmaerror(), status=%02x\n",
2364
                        __FILE__,__LINE__,info->device_name,status);
2365
}
2366
 
2367
/* handle input serial signal changes
2368
 */
2369
void isr_io_pin( SLMP_INFO *info, u16 status )
2370
{
2371
        struct  mgsl_icount *icount;
2372
 
2373
        if ( debug_level >= DEBUG_LEVEL_ISR )
2374
                printk("%s(%d):isr_io_pin status=%04X\n",
2375
                        __FILE__,__LINE__,status);
2376
 
2377
        if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
2378
                      MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
2379
                icount = &info->icount;
2380
                /* update input line counters */
2381
                if (status & MISCSTATUS_RI_LATCHED) {
2382
                        icount->rng++;
2383
                        if ( status & SerialSignal_RI )
2384
                                info->input_signal_events.ri_up++;
2385
                        else
2386
                                info->input_signal_events.ri_down++;
2387
                }
2388
                if (status & MISCSTATUS_DSR_LATCHED) {
2389
                        icount->dsr++;
2390
                        if ( status & SerialSignal_DSR )
2391
                                info->input_signal_events.dsr_up++;
2392
                        else
2393
                                info->input_signal_events.dsr_down++;
2394
                }
2395
                if (status & MISCSTATUS_DCD_LATCHED) {
2396
                        if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
2397
                                info->ie1_value &= ~CDCD;
2398
                                write_reg(info, IE1, info->ie1_value);
2399
                        }
2400
                        icount->dcd++;
2401
                        if (status & SerialSignal_DCD) {
2402
                                info->input_signal_events.dcd_up++;
2403
#ifdef CONFIG_SYNCLINK_SYNCPPP
2404
                                if (info->netcount)
2405
                                        sppp_reopen(info->netdev);
2406
#endif
2407
                        } else
2408
                                info->input_signal_events.dcd_down++;
2409
                }
2410
                if (status & MISCSTATUS_CTS_LATCHED)
2411
                {
2412
                        if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
2413
                                info->ie1_value &= ~CCTS;
2414
                                write_reg(info, IE1, info->ie1_value);
2415
                        }
2416
                        icount->cts++;
2417
                        if ( status & SerialSignal_CTS )
2418
                                info->input_signal_events.cts_up++;
2419
                        else
2420
                                info->input_signal_events.cts_down++;
2421
                }
2422
                wake_up_interruptible(&info->status_event_wait_q);
2423
                wake_up_interruptible(&info->event_wait_q);
2424
 
2425
                if ( (info->flags & ASYNC_CHECK_CD) &&
2426
                     (status & MISCSTATUS_DCD_LATCHED) ) {
2427
                        if ( debug_level >= DEBUG_LEVEL_ISR )
2428
                                printk("%s CD now %s...", info->device_name,
2429
                                       (status & SerialSignal_DCD) ? "on" : "off");
2430
                        if (status & SerialSignal_DCD)
2431
                                wake_up_interruptible(&info->open_wait);
2432
                        else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2433
                                   (info->flags & ASYNC_CALLOUT_NOHUP))) {
2434
                                if ( debug_level >= DEBUG_LEVEL_ISR )
2435
                                        printk("doing serial hangup...");
2436
                                if (info->tty)
2437
                                        tty_hangup(info->tty);
2438
                        }
2439
                }
2440
 
2441
                if ( (info->flags & ASYNC_CTS_FLOW) &&
2442
                     (status & MISCSTATUS_CTS_LATCHED) ) {
2443
                        if ( info->tty ) {
2444
                                if (info->tty->hw_stopped) {
2445
                                        if (status & SerialSignal_CTS) {
2446
                                                if ( debug_level >= DEBUG_LEVEL_ISR )
2447
                                                        printk("CTS tx start...");
2448
                                                info->tty->hw_stopped = 0;
2449
                                                tx_start(info);
2450
                                                info->pending_bh |= BH_TRANSMIT;
2451
                                                return;
2452
                                        }
2453
                                } else {
2454
                                        if (!(status & SerialSignal_CTS)) {
2455
                                                if ( debug_level >= DEBUG_LEVEL_ISR )
2456
                                                        printk("CTS tx stop...");
2457
                                                info->tty->hw_stopped = 1;
2458
                                                tx_stop(info);
2459
                                        }
2460
                                }
2461
                        }
2462
                }
2463
        }
2464
 
2465
        info->pending_bh |= BH_STATUS;
2466
}
2467
 
2468
/* Interrupt service routine entry point.
2469
 *
2470
 * Arguments:
2471
 *      irq             interrupt number that caused interrupt
2472
 *      dev_id          device ID supplied during interrupt registration
2473
 *      regs            interrupted processor context
2474
 */
2475
static void synclinkmp_interrupt(int irq, void *dev_id, struct pt_regs * regs)
2476
{
2477
        SLMP_INFO * info;
2478
        unsigned char status, status0, status1=0;
2479
        unsigned char dmastatus, dmastatus0, dmastatus1=0;
2480
        unsigned char timerstatus0, timerstatus1=0;
2481
        unsigned char shift;
2482
        unsigned int i;
2483
        unsigned short tmp;
2484
 
2485
        if ( debug_level >= DEBUG_LEVEL_ISR )
2486
                printk("%s(%d): synclinkmp_interrupt(%d)entry.\n",
2487
                        __FILE__,__LINE__,irq);
2488
 
2489
        info = (SLMP_INFO *)dev_id;
2490
        if (!info)
2491
                return;
2492
 
2493
        spin_lock(&info->lock);
2494
 
2495
        for(;;) {
2496
 
2497
                /* get status for SCA0 (ports 0-1) */
2498
                tmp = read_reg16(info, ISR0);   /* get ISR0 and ISR1 in one read */
2499
                status0 = (unsigned char)tmp;
2500
                dmastatus0 = (unsigned char)(tmp>>8);
2501
                timerstatus0 = read_reg(info, ISR2);
2502
 
2503
                if ( debug_level >= DEBUG_LEVEL_ISR )
2504
                        printk("%s(%d):%s status0=%02x, dmastatus0=%02x, timerstatus0=%02x\n",
2505
                                __FILE__,__LINE__,info->device_name,
2506
                                status0,dmastatus0,timerstatus0);
2507
 
2508
                if (info->port_count == 4) {
2509
                        /* get status for SCA1 (ports 2-3) */
2510
                        tmp = read_reg16(info->port_array[2], ISR0);
2511
                        status1 = (unsigned char)tmp;
2512
                        dmastatus1 = (unsigned char)(tmp>>8);
2513
                        timerstatus1 = read_reg(info->port_array[2], ISR2);
2514
 
2515
                        if ( debug_level >= DEBUG_LEVEL_ISR )
2516
                                printk("%s(%d):%s status1=%02x, dmastatus1=%02x, timerstatus1=%02x\n",
2517
                                        __FILE__,__LINE__,info->device_name,
2518
                                        status1,dmastatus1,timerstatus1);
2519
                }
2520
 
2521
                if (!status0 && !dmastatus0 && !timerstatus0 &&
2522
                         !status1 && !dmastatus1 && !timerstatus1)
2523
                        break;
2524
 
2525
                for(i=0; i < info->port_count ; i++) {
2526
                        if (info->port_array[i] == NULL)
2527
                                continue;
2528
                        if (i < 2) {
2529
                                status = status0;
2530
                                dmastatus = dmastatus0;
2531
                        } else {
2532
                                status = status1;
2533
                                dmastatus = dmastatus1;
2534
                        }
2535
 
2536
                        shift = i & 1 ? 4 :0;
2537
 
2538
                        if (status & BIT0 << shift)
2539
                                isr_rxrdy(info->port_array[i]);
2540
                        if (status & BIT1 << shift)
2541
                                isr_txrdy(info->port_array[i]);
2542
                        if (status & BIT2 << shift)
2543
                                isr_rxint(info->port_array[i]);
2544
                        if (status & BIT3 << shift)
2545
                                isr_txint(info->port_array[i]);
2546
 
2547
                        if (dmastatus & BIT0 << shift)
2548
                                isr_rxdmaerror(info->port_array[i]);
2549
                        if (dmastatus & BIT1 << shift)
2550
                                isr_rxdmaok(info->port_array[i]);
2551
                        if (dmastatus & BIT2 << shift)
2552
                                isr_txdmaerror(info->port_array[i]);
2553
                        if (dmastatus & BIT3 << shift)
2554
                                isr_txdmaok(info->port_array[i]);
2555
                }
2556
 
2557
                if (timerstatus0 & (BIT5 | BIT4))
2558
                        isr_timer(info->port_array[0]);
2559
                if (timerstatus0 & (BIT7 | BIT6))
2560
                        isr_timer(info->port_array[1]);
2561
                if (timerstatus1 & (BIT5 | BIT4))
2562
                        isr_timer(info->port_array[2]);
2563
                if (timerstatus1 & (BIT7 | BIT6))
2564
                        isr_timer(info->port_array[3]);
2565
        }
2566
 
2567
        for(i=0; i < info->port_count ; i++) {
2568
                SLMP_INFO * port = info->port_array[i];
2569
 
2570
                /* Request bottom half processing if there's something
2571
                 * for it to do and the bh is not already running.
2572
                 *
2573
                 * Note: startup adapter diags require interrupts.
2574
                 * do not request bottom half processing if the
2575
                 * device is not open in a normal mode.
2576
                 */
2577
                if ( port && (port->count || port->netcount) &&
2578
                     port->pending_bh && !port->bh_running &&
2579
                     !port->bh_requested ) {
2580
                        if ( debug_level >= DEBUG_LEVEL_ISR )
2581
                                printk("%s(%d):%s queueing bh task.\n",
2582
                                        __FILE__,__LINE__,port->device_name);
2583
                        queue_task(&port->task, &tq_immediate);
2584
                        mark_bh(IMMEDIATE_BH);
2585
                        port->bh_requested = 1;
2586
                }
2587
        }
2588
 
2589
        spin_unlock(&info->lock);
2590
 
2591
        if ( debug_level >= DEBUG_LEVEL_ISR )
2592
                printk("%s(%d):synclinkmp_interrupt(%d)exit.\n",
2593
                        __FILE__,__LINE__,irq);
2594
}
2595
 
2596
/* Initialize and start device.
2597
 */
2598
static int startup(SLMP_INFO * info)
2599
{
2600
        if ( debug_level >= DEBUG_LEVEL_INFO )
2601
                printk("%s(%d):%s tx_releaseup()\n",__FILE__,__LINE__,info->device_name);
2602
 
2603
        if (info->flags & ASYNC_INITIALIZED)
2604
                return 0;
2605
 
2606
        if (!info->tx_buf) {
2607
                info->tx_buf = (unsigned char *)kmalloc(info->max_frame_size, GFP_KERNEL);
2608
                if (!info->tx_buf) {
2609
                        printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
2610
                                __FILE__,__LINE__,info->device_name);
2611
                        return -ENOMEM;
2612
                }
2613
        }
2614
 
2615
        info->pending_bh = 0;
2616
 
2617
        init_timer(&info->tx_timer);
2618
        info->tx_timer.data = (unsigned long)info;
2619
        info->tx_timer.function = tx_timeout;
2620
 
2621
        /* program hardware for current parameters */
2622
        reset_port(info);
2623
 
2624
        change_params(info);
2625
 
2626
        init_timer(&info->status_timer);
2627
        info->status_timer.data = (unsigned long)info;
2628
        info->status_timer.function = status_timeout;
2629
        info->status_timer.expires = jiffies + jiffies_from_ms(10);
2630
        add_timer(&info->status_timer);
2631
 
2632
        if (info->tty)
2633
                clear_bit(TTY_IO_ERROR, &info->tty->flags);
2634
 
2635
        info->flags |= ASYNC_INITIALIZED;
2636
 
2637
        return 0;
2638
}
2639
 
2640
/* Called by close() and hangup() to shutdown hardware
2641
 */
2642
static void shutdown(SLMP_INFO * info)
2643
{
2644
        unsigned long flags;
2645
 
2646
        if (!(info->flags & ASYNC_INITIALIZED))
2647
                return;
2648
 
2649
        if (debug_level >= DEBUG_LEVEL_INFO)
2650
                printk("%s(%d):%s synclinkmp_shutdown()\n",
2651
                         __FILE__,__LINE__, info->device_name );
2652
 
2653
        /* clear status wait queue because status changes */
2654
        /* can't happen after shutting down the hardware */
2655
        wake_up_interruptible(&info->status_event_wait_q);
2656
        wake_up_interruptible(&info->event_wait_q);
2657
 
2658
        del_timer(&info->tx_timer);
2659
        del_timer(&info->status_timer);
2660
 
2661
        if (info->tx_buf) {
2662
                free_page((unsigned long) info->tx_buf);
2663
                info->tx_buf = 0;
2664
        }
2665
 
2666
        spin_lock_irqsave(&info->lock,flags);
2667
 
2668
        reset_port(info);
2669
 
2670
        if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
2671
                info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2672
                set_signals(info);
2673
        }
2674
 
2675
        spin_unlock_irqrestore(&info->lock,flags);
2676
 
2677
        if (info->tty)
2678
                set_bit(TTY_IO_ERROR, &info->tty->flags);
2679
 
2680
        info->flags &= ~ASYNC_INITIALIZED;
2681
}
2682
 
2683
static void program_hw(SLMP_INFO *info)
2684
{
2685
        unsigned long flags;
2686
 
2687
        spin_lock_irqsave(&info->lock,flags);
2688
 
2689
        rx_stop(info);
2690
        tx_stop(info);
2691
 
2692
        info->tx_count = info->tx_put = info->tx_get = 0;
2693
 
2694
        if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
2695
                hdlc_mode(info);
2696
        else
2697
                async_mode(info);
2698
 
2699
        set_signals(info);
2700
 
2701
        info->dcd_chkcount = 0;
2702
        info->cts_chkcount = 0;
2703
        info->ri_chkcount = 0;
2704
        info->dsr_chkcount = 0;
2705
 
2706
        info->ie1_value |= (CDCD|CCTS);
2707
        write_reg(info, IE1, info->ie1_value);
2708
 
2709
        get_signals(info);
2710
 
2711
        if (info->netcount || (info->tty && info->tty->termios->c_cflag & CREAD) )
2712
                rx_start(info);
2713
 
2714
        spin_unlock_irqrestore(&info->lock,flags);
2715
}
2716
 
2717
/* Reconfigure adapter based on new parameters
2718
 */
2719
static void change_params(SLMP_INFO *info)
2720
{
2721
        unsigned cflag;
2722
        int bits_per_char;
2723
 
2724
        if (!info->tty || !info->tty->termios)
2725
                return;
2726
 
2727
        if (debug_level >= DEBUG_LEVEL_INFO)
2728
                printk("%s(%d):%s change_params()\n",
2729
                         __FILE__,__LINE__, info->device_name );
2730
 
2731
        cflag = info->tty->termios->c_cflag;
2732
 
2733
        /* if B0 rate (hangup) specified then negate DTR and RTS */
2734
        /* otherwise assert DTR and RTS */
2735
        if (cflag & CBAUD)
2736
                info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2737
        else
2738
                info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2739
 
2740
        /* byte size and parity */
2741
 
2742
        switch (cflag & CSIZE) {
2743
              case CS5: info->params.data_bits = 5; break;
2744
              case CS6: info->params.data_bits = 6; break;
2745
              case CS7: info->params.data_bits = 7; break;
2746
              case CS8: info->params.data_bits = 8; break;
2747
              /* Never happens, but GCC is too dumb to figure it out */
2748
              default:  info->params.data_bits = 7; break;
2749
              }
2750
 
2751
        if (cflag & CSTOPB)
2752
                info->params.stop_bits = 2;
2753
        else
2754
                info->params.stop_bits = 1;
2755
 
2756
        info->params.parity = ASYNC_PARITY_NONE;
2757
        if (cflag & PARENB) {
2758
                if (cflag & PARODD)
2759
                        info->params.parity = ASYNC_PARITY_ODD;
2760
                else
2761
                        info->params.parity = ASYNC_PARITY_EVEN;
2762
#ifdef CMSPAR
2763
                if (cflag & CMSPAR)
2764
                        info->params.parity = ASYNC_PARITY_SPACE;
2765
#endif
2766
        }
2767
 
2768
        /* calculate number of jiffies to transmit a full
2769
         * FIFO (32 bytes) at specified data rate
2770
         */
2771
        bits_per_char = info->params.data_bits +
2772
                        info->params.stop_bits + 1;
2773
 
2774
        /* if port data rate is set to 460800 or less then
2775
         * allow tty settings to override, otherwise keep the
2776
         * current data rate.
2777
         */
2778
        if (info->params.data_rate <= 460800) {
2779
                info->params.data_rate = tty_get_baud_rate(info->tty);
2780
        }
2781
 
2782
        if ( info->params.data_rate ) {
2783
                info->timeout = (32*HZ*bits_per_char) /
2784
                                info->params.data_rate;
2785
        }
2786
        info->timeout += HZ/50;         /* Add .02 seconds of slop */
2787
 
2788
        if (cflag & CRTSCTS)
2789
                info->flags |= ASYNC_CTS_FLOW;
2790
        else
2791
                info->flags &= ~ASYNC_CTS_FLOW;
2792
 
2793
        if (cflag & CLOCAL)
2794
                info->flags &= ~ASYNC_CHECK_CD;
2795
        else
2796
                info->flags |= ASYNC_CHECK_CD;
2797
 
2798
        /* process tty input control flags */
2799
 
2800
        info->read_status_mask2 = OVRN;
2801
        if (I_INPCK(info->tty))
2802
                info->read_status_mask2 |= PE | FRME;
2803
        if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2804
                info->read_status_mask1 |= BRKD;
2805
        if (I_IGNPAR(info->tty))
2806
                info->ignore_status_mask2 |= PE | FRME;
2807
        if (I_IGNBRK(info->tty)) {
2808
                info->ignore_status_mask1 |= BRKD;
2809
                /* If ignoring parity and break indicators, ignore
2810
                 * overruns too.  (For real raw support).
2811
                 */
2812
                if (I_IGNPAR(info->tty))
2813
                        info->ignore_status_mask2 |= OVRN;
2814
        }
2815
 
2816
        program_hw(info);
2817
}
2818
 
2819
static int get_stats(SLMP_INFO * info, struct mgsl_icount *user_icount)
2820
{
2821
        int err;
2822
 
2823
        if (debug_level >= DEBUG_LEVEL_INFO)
2824
                printk("%s(%d):%s get_params()\n",
2825
                         __FILE__,__LINE__, info->device_name);
2826
 
2827
        COPY_TO_USER(err,user_icount, &info->icount, sizeof(struct mgsl_icount));
2828
        if (err) {
2829
                if ( debug_level >= DEBUG_LEVEL_INFO )
2830
                        printk( "%s(%d):%s get_stats() user buffer copy failed\n",
2831
                                __FILE__,__LINE__,info->device_name);
2832
                return -EFAULT;
2833
        }
2834
 
2835
        return 0;
2836
}
2837
 
2838
static int get_params(SLMP_INFO * info, MGSL_PARAMS *user_params)
2839
{
2840
        int err;
2841
        if (debug_level >= DEBUG_LEVEL_INFO)
2842
                printk("%s(%d):%s get_params()\n",
2843
                         __FILE__,__LINE__, info->device_name);
2844
 
2845
        COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
2846
        if (err) {
2847
                if ( debug_level >= DEBUG_LEVEL_INFO )
2848
                        printk( "%s(%d):%s get_params() user buffer copy failed\n",
2849
                                __FILE__,__LINE__,info->device_name);
2850
                return -EFAULT;
2851
        }
2852
 
2853
        return 0;
2854
}
2855
 
2856
static int set_params(SLMP_INFO * info, MGSL_PARAMS *new_params)
2857
{
2858
        unsigned long flags;
2859
        MGSL_PARAMS tmp_params;
2860
        int err;
2861
 
2862
        if (debug_level >= DEBUG_LEVEL_INFO)
2863
                printk("%s(%d):%s set_params\n",
2864
                        __FILE__,__LINE__,info->device_name );
2865
        COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2866
        if (err) {
2867
                if ( debug_level >= DEBUG_LEVEL_INFO )
2868
                        printk( "%s(%d):%s set_params() user buffer copy failed\n",
2869
                                __FILE__,__LINE__,info->device_name);
2870
                return -EFAULT;
2871
        }
2872
 
2873
        spin_lock_irqsave(&info->lock,flags);
2874
        memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2875
        spin_unlock_irqrestore(&info->lock,flags);
2876
 
2877
        change_params(info);
2878
 
2879
        return 0;
2880
}
2881
 
2882
static int get_txidle(SLMP_INFO * info, int*idle_mode)
2883
{
2884
        int err;
2885
 
2886
        if (debug_level >= DEBUG_LEVEL_INFO)
2887
                printk("%s(%d):%s get_txidle()=%d\n",
2888
                         __FILE__,__LINE__, info->device_name, info->idle_mode);
2889
 
2890
        COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2891
        if (err) {
2892
                if ( debug_level >= DEBUG_LEVEL_INFO )
2893
                        printk( "%s(%d):%s get_txidle() user buffer copy failed\n",
2894
                                __FILE__,__LINE__,info->device_name);
2895
                return -EFAULT;
2896
        }
2897
 
2898
        return 0;
2899
}
2900
 
2901
static int set_txidle(SLMP_INFO * info, int idle_mode)
2902
{
2903
        unsigned long flags;
2904
 
2905
        if (debug_level >= DEBUG_LEVEL_INFO)
2906
                printk("%s(%d):%s set_txidle(%d)\n",
2907
                        __FILE__,__LINE__,info->device_name, idle_mode );
2908
 
2909
        spin_lock_irqsave(&info->lock,flags);
2910
        info->idle_mode = idle_mode;
2911
        tx_set_idle( info );
2912
        spin_unlock_irqrestore(&info->lock,flags);
2913
        return 0;
2914
}
2915
 
2916
static int tx_enable(SLMP_INFO * info, int enable)
2917
{
2918
        unsigned long flags;
2919
 
2920
        if (debug_level >= DEBUG_LEVEL_INFO)
2921
                printk("%s(%d):%s tx_enable(%d)\n",
2922
                        __FILE__,__LINE__,info->device_name, enable);
2923
 
2924
        spin_lock_irqsave(&info->lock,flags);
2925
        if ( enable ) {
2926
                if ( !info->tx_enabled ) {
2927
                        tx_start(info);
2928
                }
2929
        } else {
2930
                if ( info->tx_enabled )
2931
                        tx_stop(info);
2932
        }
2933
        spin_unlock_irqrestore(&info->lock,flags);
2934
        return 0;
2935
}
2936
 
2937
/* abort send HDLC frame
2938
 */
2939
static int tx_abort(SLMP_INFO * info)
2940
{
2941
        unsigned long flags;
2942
 
2943
        if (debug_level >= DEBUG_LEVEL_INFO)
2944
                printk("%s(%d):%s tx_abort()\n",
2945
                        __FILE__,__LINE__,info->device_name);
2946
 
2947
        spin_lock_irqsave(&info->lock,flags);
2948
        if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC ) {
2949
                info->ie1_value &= ~UDRN;
2950
                info->ie1_value |= IDLE;
2951
                write_reg(info, IE1, info->ie1_value);  /* disable tx status interrupts */
2952
                write_reg(info, SR1, (unsigned char)(IDLE + UDRN));     /* clear pending */
2953
 
2954
                write_reg(info, TXDMA + DSR, 0);         /* disable DMA channel */
2955
                write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
2956
 
2957
                write_reg(info, CMD, TXABORT);
2958
        }
2959
        spin_unlock_irqrestore(&info->lock,flags);
2960
        return 0;
2961
}
2962
 
2963
static int rx_enable(SLMP_INFO * info, int enable)
2964
{
2965
        unsigned long flags;
2966
 
2967
        if (debug_level >= DEBUG_LEVEL_INFO)
2968
                printk("%s(%d):%s rx_enable(%d)\n",
2969
                        __FILE__,__LINE__,info->device_name,enable);
2970
 
2971
        spin_lock_irqsave(&info->lock,flags);
2972
        if ( enable ) {
2973
                if ( !info->rx_enabled )
2974
                        rx_start(info);
2975
        } else {
2976
                if ( info->rx_enabled )
2977
                        rx_stop(info);
2978
        }
2979
        spin_unlock_irqrestore(&info->lock,flags);
2980
        return 0;
2981
}
2982
 
2983
static int map_status(int signals)
2984
{
2985
        /* Map status bits to API event bits */
2986
 
2987
        return ((signals & SerialSignal_DSR) ? MgslEvent_DsrActive : MgslEvent_DsrInactive) +
2988
               ((signals & SerialSignal_CTS) ? MgslEvent_CtsActive : MgslEvent_CtsInactive) +
2989
               ((signals & SerialSignal_DCD) ? MgslEvent_DcdActive : MgslEvent_DcdInactive) +
2990
               ((signals & SerialSignal_RI)  ? MgslEvent_RiActive : MgslEvent_RiInactive);
2991
}
2992
 
2993
/* wait for specified event to occur
2994
 */
2995
static int wait_mgsl_event(SLMP_INFO * info, int * mask_ptr)
2996
{
2997
        unsigned long flags;
2998
        int s;
2999
        int rc=0;
3000
        struct mgsl_icount cprev, cnow;
3001
        int events;
3002
        int mask;
3003
        struct  _input_signal_events oldsigs, newsigs;
3004
        DECLARE_WAITQUEUE(wait, current);
3005
 
3006
        COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
3007
        if (rc) {
3008
                return  -EFAULT;
3009
        }
3010
 
3011
        if (debug_level >= DEBUG_LEVEL_INFO)
3012
                printk("%s(%d):%s wait_mgsl_event(%d)\n",
3013
                        __FILE__,__LINE__,info->device_name,mask);
3014
 
3015
        spin_lock_irqsave(&info->lock,flags);
3016
 
3017
        /* return immediately if state matches requested events */
3018
        get_signals(info);
3019
        s = map_status(info->serial_signals);
3020
 
3021
        events = mask &
3022
                ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
3023
                  ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
3024
                  ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
3025
                  ((s & SerialSignal_RI)  ? MgslEvent_RiActive :MgslEvent_RiInactive) );
3026
        if (events) {
3027
                spin_unlock_irqrestore(&info->lock,flags);
3028
                goto exit;
3029
        }
3030
 
3031
        /* save current irq counts */
3032
        cprev = info->icount;
3033
        oldsigs = info->input_signal_events;
3034
 
3035
        /* enable hunt and idle irqs if needed */
3036
        if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
3037
                unsigned char oldval = info->ie1_value;
3038
                unsigned char newval = oldval +
3039
                         (mask & MgslEvent_ExitHuntMode ? FLGD:0) +
3040
                         (mask & MgslEvent_IdleReceived ? IDLE:0);
3041
                if ( oldval != newval ) {
3042
                        info->ie1_value = newval;
3043
                        write_reg(info, IE1, info->ie1_value);
3044
                }
3045
        }
3046
 
3047
        set_current_state(TASK_INTERRUPTIBLE);
3048
        add_wait_queue(&info->event_wait_q, &wait);
3049
 
3050
        spin_unlock_irqrestore(&info->lock,flags);
3051
 
3052
        for(;;) {
3053
                schedule();
3054
                if (signal_pending(current)) {
3055
                        rc = -ERESTARTSYS;
3056
                        break;
3057
                }
3058
 
3059
                /* get current irq counts */
3060
                spin_lock_irqsave(&info->lock,flags);
3061
                cnow = info->icount;
3062
                newsigs = info->input_signal_events;
3063
                set_current_state(TASK_INTERRUPTIBLE);
3064
                spin_unlock_irqrestore(&info->lock,flags);
3065
 
3066
                /* if no change, wait aborted for some reason */
3067
                if (newsigs.dsr_up   == oldsigs.dsr_up   &&
3068
                    newsigs.dsr_down == oldsigs.dsr_down &&
3069
                    newsigs.dcd_up   == oldsigs.dcd_up   &&
3070
                    newsigs.dcd_down == oldsigs.dcd_down &&
3071
                    newsigs.cts_up   == oldsigs.cts_up   &&
3072
                    newsigs.cts_down == oldsigs.cts_down &&
3073
                    newsigs.ri_up    == oldsigs.ri_up    &&
3074
                    newsigs.ri_down  == oldsigs.ri_down  &&
3075
                    cnow.exithunt    == cprev.exithunt   &&
3076
                    cnow.rxidle      == cprev.rxidle) {
3077
                        rc = -EIO;
3078
                        break;
3079
                }
3080
 
3081
                events = mask &
3082
                        ( (newsigs.dsr_up   != oldsigs.dsr_up   ? MgslEvent_DsrActive:0)   +
3083
                          (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
3084
                          (newsigs.dcd_up   != oldsigs.dcd_up   ? MgslEvent_DcdActive:0)   +
3085
                          (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
3086
                          (newsigs.cts_up   != oldsigs.cts_up   ? MgslEvent_CtsActive:0)   +
3087
                          (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
3088
                          (newsigs.ri_up    != oldsigs.ri_up    ? MgslEvent_RiActive:0)    +
3089
                          (newsigs.ri_down  != oldsigs.ri_down  ? MgslEvent_RiInactive:0)  +
3090
                          (cnow.exithunt    != cprev.exithunt   ? MgslEvent_ExitHuntMode:0) +
3091
                          (cnow.rxidle      != cprev.rxidle     ? MgslEvent_IdleReceived:0) );
3092
                if (events)
3093
                        break;
3094
 
3095
                cprev = cnow;
3096
                oldsigs = newsigs;
3097
        }
3098
 
3099
        remove_wait_queue(&info->event_wait_q, &wait);
3100
        set_current_state(TASK_RUNNING);
3101
 
3102
 
3103
        if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
3104
                spin_lock_irqsave(&info->lock,flags);
3105
                if (!waitqueue_active(&info->event_wait_q)) {
3106
                        /* disable enable exit hunt mode/idle rcvd IRQs */
3107
                        info->ie1_value &= ~(FLGD|IDLE);
3108
                        write_reg(info, IE1, info->ie1_value);
3109
                }
3110
                spin_unlock_irqrestore(&info->lock,flags);
3111
        }
3112
exit:
3113
        if ( rc == 0 )
3114
                PUT_USER(rc, events, mask_ptr);
3115
 
3116
        return rc;
3117
}
3118
 
3119
static int modem_input_wait(SLMP_INFO *info,int arg)
3120
{
3121
        unsigned long flags;
3122
        int rc;
3123
        struct mgsl_icount cprev, cnow;
3124
        DECLARE_WAITQUEUE(wait, current);
3125
 
3126
        /* save current irq counts */
3127
        spin_lock_irqsave(&info->lock,flags);
3128
        cprev = info->icount;
3129
        add_wait_queue(&info->status_event_wait_q, &wait);
3130
        set_current_state(TASK_INTERRUPTIBLE);
3131
        spin_unlock_irqrestore(&info->lock,flags);
3132
 
3133
        for(;;) {
3134
                schedule();
3135
                if (signal_pending(current)) {
3136
                        rc = -ERESTARTSYS;
3137
                        break;
3138
                }
3139
 
3140
                /* get new irq counts */
3141
                spin_lock_irqsave(&info->lock,flags);
3142
                cnow = info->icount;
3143
                set_current_state(TASK_INTERRUPTIBLE);
3144
                spin_unlock_irqrestore(&info->lock,flags);
3145
 
3146
                /* if no change, wait aborted for some reason */
3147
                if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
3148
                    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
3149
                        rc = -EIO;
3150
                        break;
3151
                }
3152
 
3153
                /* check for change in caller specified modem input */
3154
                if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
3155
                    (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
3156
                    (arg & TIOCM_CD  && cnow.dcd != cprev.dcd) ||
3157
                    (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
3158
                        rc = 0;
3159
                        break;
3160
                }
3161
 
3162
                cprev = cnow;
3163
        }
3164
        remove_wait_queue(&info->status_event_wait_q, &wait);
3165
        set_current_state(TASK_RUNNING);
3166
        return rc;
3167
}
3168
 
3169
/* return the state of the serial control and status signals
3170
 */
3171
static int get_modem_info(SLMP_INFO * info, unsigned int *value)
3172
{
3173
        unsigned int result;
3174
        unsigned long flags;
3175
        int err;
3176
 
3177
        spin_lock_irqsave(&info->lock,flags);
3178
        get_signals(info);
3179
        spin_unlock_irqrestore(&info->lock,flags);
3180
 
3181
        result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
3182
                ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
3183
                ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
3184
                ((info->serial_signals & SerialSignal_RI)  ? TIOCM_RNG:0) +
3185
                ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
3186
                ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
3187
 
3188
        if (debug_level >= DEBUG_LEVEL_INFO)
3189
                printk("%s(%d):%s synclinkmp_get_modem_info() value=%08X\n",
3190
                         __FILE__,__LINE__, info->device_name, result );
3191
 
3192
        PUT_USER(err,result,value);
3193
        return err;
3194
}
3195
 
3196
/* set modem control signals (DTR/RTS)
3197
 *
3198
 *      cmd     signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
3199
 *              TIOCMSET = set/clear signal values
3200
 *      value   bit mask for command
3201
 */
3202
static int set_modem_info(SLMP_INFO * info, unsigned int cmd,
3203
                          unsigned int *value)
3204
{
3205
        int error;
3206
        unsigned int arg;
3207
        unsigned long flags;
3208
 
3209
        if (debug_level >= DEBUG_LEVEL_INFO)
3210
                printk("%s(%d):%s synclinkmp_set_modem_info()\n",
3211
                        __FILE__,__LINE__,info->device_name );
3212
 
3213
        GET_USER(error,arg,value);
3214
        if (error)
3215
                return error;
3216
 
3217
        switch (cmd) {
3218
        case TIOCMBIS:
3219
                if (arg & TIOCM_RTS)
3220
                        info->serial_signals |= SerialSignal_RTS;
3221
                if (arg & TIOCM_DTR)
3222
                        info->serial_signals |= SerialSignal_DTR;
3223
                break;
3224
        case TIOCMBIC:
3225
                if (arg & TIOCM_RTS)
3226
                        info->serial_signals &= ~SerialSignal_RTS;
3227
                if (arg & TIOCM_DTR)
3228
                        info->serial_signals &= ~SerialSignal_DTR;
3229
                break;
3230
        case TIOCMSET:
3231
                if (arg & TIOCM_RTS)
3232
                        info->serial_signals |= SerialSignal_RTS;
3233
                else
3234
                        info->serial_signals &= ~SerialSignal_RTS;
3235
 
3236
                if (arg & TIOCM_DTR)
3237
                        info->serial_signals |= SerialSignal_DTR;
3238
                else
3239
                        info->serial_signals &= ~SerialSignal_DTR;
3240
                break;
3241
        default:
3242
                return -EINVAL;
3243
        }
3244
 
3245
        spin_lock_irqsave(&info->lock,flags);
3246
        set_signals(info);
3247
        spin_unlock_irqrestore(&info->lock,flags);
3248
 
3249
        return 0;
3250
}
3251
 
3252
 
3253
 
3254
/* Block the current process until the specified port is ready to open.
3255
 */
3256
static int block_til_ready(struct tty_struct *tty, struct file *filp,
3257
                           SLMP_INFO *info)
3258
{
3259
        DECLARE_WAITQUEUE(wait, current);
3260
        int             retval;
3261
        int             do_clocal = 0, extra_count = 0;
3262
        unsigned long   flags;
3263
 
3264
        if (debug_level >= DEBUG_LEVEL_INFO)
3265
                printk("%s(%d):%s block_til_ready()\n",
3266
                         __FILE__,__LINE__, tty->driver.name );
3267
 
3268
        if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
3269
                /* this is a callout device */
3270
                /* just verify that normal device is not in use */
3271
                if (info->flags & ASYNC_NORMAL_ACTIVE)
3272
                        return -EBUSY;
3273
                if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
3274
                    (info->flags & ASYNC_SESSION_LOCKOUT) &&
3275
                    (info->session != current->session))
3276
                    return -EBUSY;
3277
                if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
3278
                    (info->flags & ASYNC_PGRP_LOCKOUT) &&
3279
                    (info->pgrp != current->pgrp))
3280
                    return -EBUSY;
3281
                info->flags |= ASYNC_CALLOUT_ACTIVE;
3282
                return 0;
3283
        }
3284
 
3285
        if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3286
                /* nonblock mode is set or port is not enabled */
3287
                /* just verify that callout device is not active */
3288
                if (info->flags & ASYNC_CALLOUT_ACTIVE)
3289
                        return -EBUSY;
3290
                info->flags |= ASYNC_NORMAL_ACTIVE;
3291
                return 0;
3292
        }
3293
 
3294
        if (info->flags & ASYNC_CALLOUT_ACTIVE) {
3295
                if (info->normal_termios.c_cflag & CLOCAL)
3296
                        do_clocal = 1;
3297
        } else {
3298
                if (tty->termios->c_cflag & CLOCAL)
3299
                        do_clocal = 1;
3300
        }
3301
 
3302
        /* Wait for carrier detect and the line to become
3303
         * free (i.e., not in use by the callout).  While we are in
3304
         * this loop, info->count is dropped by one, so that
3305
         * close() knows when to free things.  We restore it upon
3306
         * exit, either normal or abnormal.
3307
         */
3308
 
3309
        retval = 0;
3310
        add_wait_queue(&info->open_wait, &wait);
3311
 
3312
        if (debug_level >= DEBUG_LEVEL_INFO)
3313
                printk("%s(%d):%s block_til_ready() before block, count=%d\n",
3314
                         __FILE__,__LINE__, tty->driver.name, info->count );
3315
 
3316
        save_flags(flags); cli();
3317
        if (!tty_hung_up_p(filp)) {
3318
                extra_count = 1;
3319
                info->count--;
3320
        }
3321
        restore_flags(flags);
3322
        info->blocked_open++;
3323
 
3324
        while (1) {
3325
                if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
3326
                    (tty->termios->c_cflag & CBAUD)) {
3327
                        spin_lock_irqsave(&info->lock,flags);
3328
                        info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
3329
                        set_signals(info);
3330
                        spin_unlock_irqrestore(&info->lock,flags);
3331
                }
3332
 
3333
                set_current_state(TASK_INTERRUPTIBLE);
3334
 
3335
                if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
3336
                        retval = (info->flags & ASYNC_HUP_NOTIFY) ?
3337
                                        -EAGAIN : -ERESTARTSYS;
3338
                        break;
3339
                }
3340
 
3341
                spin_lock_irqsave(&info->lock,flags);
3342
                get_signals(info);
3343
                spin_unlock_irqrestore(&info->lock,flags);
3344
 
3345
                if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
3346
                    !(info->flags & ASYNC_CLOSING) &&
3347
                    (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
3348
                        break;
3349
                }
3350
 
3351
                if (signal_pending(current)) {
3352
                        retval = -ERESTARTSYS;
3353
                        break;
3354
                }
3355
 
3356
                if (debug_level >= DEBUG_LEVEL_INFO)
3357
                        printk("%s(%d):%s block_til_ready() count=%d\n",
3358
                                 __FILE__,__LINE__, tty->driver.name, info->count );
3359
 
3360
                schedule();
3361
        }
3362
 
3363
        set_current_state(TASK_RUNNING);
3364
        remove_wait_queue(&info->open_wait, &wait);
3365
 
3366
        if (extra_count)
3367
                info->count++;
3368
        info->blocked_open--;
3369
 
3370
        if (debug_level >= DEBUG_LEVEL_INFO)
3371
                printk("%s(%d):%s block_til_ready() after, count=%d\n",
3372
                         __FILE__,__LINE__, tty->driver.name, info->count );
3373
 
3374
        if (!retval)
3375
                info->flags |= ASYNC_NORMAL_ACTIVE;
3376
 
3377
        return retval;
3378
}
3379
 
3380
int alloc_dma_bufs(SLMP_INFO *info)
3381
{
3382
        unsigned short BuffersPerFrame;
3383
        unsigned short BufferCount;
3384
 
3385
        // Force allocation to start at 64K boundary for each port.
3386
        // This is necessary because *all* buffer descriptors for a port
3387
        // *must* be in the same 64K block. All descriptors on a port
3388
        // share a common 'base' address (upper 8 bits of 24 bits) programmed
3389
        // into the CBP register.
3390
        info->port_array[0]->last_mem_alloc = (SCA_MEM_SIZE/4) * info->port_num;
3391
 
3392
        /* Calculate the number of DMA buffers necessary to hold the */
3393
        /* largest allowable frame size. Note: If the max frame size is */
3394
        /* not an even multiple of the DMA buffer size then we need to */
3395
        /* round the buffer count per frame up one. */
3396
 
3397
        BuffersPerFrame = (unsigned short)(info->max_frame_size/SCABUFSIZE);
3398
        if ( info->max_frame_size % SCABUFSIZE )
3399
                BuffersPerFrame++;
3400
 
3401
        /* calculate total number of data buffers (SCABUFSIZE) possible
3402
         * in one ports memory (SCA_MEM_SIZE/4) after allocating memory
3403
         * for the descriptor list (BUFFERLISTSIZE).
3404
         */
3405
        BufferCount = (SCA_MEM_SIZE/4 - BUFFERLISTSIZE)/SCABUFSIZE;
3406
 
3407
        /* limit number of buffers to maximum amount of descriptors */
3408
        if (BufferCount > BUFFERLISTSIZE/sizeof(SCADESC))
3409
                BufferCount = BUFFERLISTSIZE/sizeof(SCADESC);
3410
 
3411
        /* use enough buffers to transmit one max size frame */
3412
        info->tx_buf_count = BuffersPerFrame + 1;
3413
 
3414
        /* never use more than half the available buffers for transmit */
3415
        if (info->tx_buf_count > (BufferCount/2))
3416
                info->tx_buf_count = BufferCount/2;
3417
 
3418
        if (info->tx_buf_count > SCAMAXDESC)
3419
                info->tx_buf_count = SCAMAXDESC;
3420
 
3421
        /* use remaining buffers for receive */
3422
        info->rx_buf_count = BufferCount - info->tx_buf_count;
3423
 
3424
        if (info->rx_buf_count > SCAMAXDESC)
3425
                info->rx_buf_count = SCAMAXDESC;
3426
 
3427
        if ( debug_level >= DEBUG_LEVEL_INFO )
3428
                printk("%s(%d):%s Allocating %d TX and %d RX DMA buffers.\n",
3429
                        __FILE__,__LINE__, info->device_name,
3430
                        info->tx_buf_count,info->rx_buf_count);
3431
 
3432
        if ( alloc_buf_list( info ) < 0 ||
3433
                alloc_frame_bufs(info,
3434
                                        info->rx_buf_list,
3435
                                        info->rx_buf_list_ex,
3436
                                        info->rx_buf_count) < 0 ||
3437
                alloc_frame_bufs(info,
3438
                                        info->tx_buf_list,
3439
                                        info->tx_buf_list_ex,
3440
                                        info->tx_buf_count) < 0 ||
3441
                alloc_tmp_rx_buf(info) < 0 ) {
3442
                printk("%s(%d):%s Can't allocate DMA buffer memory\n",
3443
                        __FILE__,__LINE__, info->device_name);
3444
                return -ENOMEM;
3445
        }
3446
 
3447
        rx_reset_buffers( info );
3448
 
3449
        return 0;
3450
}
3451
 
3452
/* Allocate DMA buffers for the transmit and receive descriptor lists.
3453
 */
3454
int alloc_buf_list(SLMP_INFO *info)
3455
{
3456
        unsigned int i;
3457
 
3458
        /* build list in adapter shared memory */
3459
        info->buffer_list = info->memory_base + info->port_array[0]->last_mem_alloc;
3460
        info->buffer_list_phys = info->port_array[0]->last_mem_alloc;
3461
        info->port_array[0]->last_mem_alloc += BUFFERLISTSIZE;
3462
 
3463
        memset(info->buffer_list, 0, BUFFERLISTSIZE);
3464
 
3465
        /* Save virtual address pointers to the receive and */
3466
        /* transmit buffer lists. (Receive 1st). These pointers will */
3467
        /* be used by the processor to access the lists. */
3468
        info->rx_buf_list = (SCADESC *)info->buffer_list;
3469
 
3470
        info->tx_buf_list = (SCADESC *)info->buffer_list;
3471
        info->tx_buf_list += info->rx_buf_count;
3472
 
3473
        /* Build links for circular buffer entry lists (tx and rx)
3474
         *
3475
         * Note: links are physical addresses read by the SCA device
3476
         * to determine the next buffer entry to use.
3477
         */
3478
 
3479
        for ( i = 0; i < info->rx_buf_count; i++ ) {
3480
                /* calculate and store physical address of this buffer entry */
3481
                info->rx_buf_list_ex[i].phys_entry =
3482
                        info->buffer_list_phys + (i * sizeof(SCABUFSIZE));
3483
 
3484
                /* calculate and store physical address of */
3485
                /* next entry in cirular list of entries */
3486
                info->rx_buf_list[i].next = info->buffer_list_phys;
3487
                if ( i < info->rx_buf_count - 1 )
3488
                        info->rx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
3489
 
3490
                info->rx_buf_list[i].length = SCABUFSIZE;
3491
        }
3492
 
3493
        for ( i = 0; i < info->tx_buf_count; i++ ) {
3494
                /* calculate and store physical address of this buffer entry */
3495
                info->tx_buf_list_ex[i].phys_entry = info->buffer_list_phys +
3496
                        ((info->rx_buf_count + i) * sizeof(SCADESC));
3497
 
3498
                /* calculate and store physical address of */
3499
                /* next entry in cirular list of entries */
3500
 
3501
                info->tx_buf_list[i].next = info->buffer_list_phys +
3502
                        info->rx_buf_count * sizeof(SCADESC);
3503
 
3504
                if ( i < info->tx_buf_count - 1 )
3505
                        info->tx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
3506
        }
3507
 
3508
        return 0;
3509
}
3510
 
3511
/* Allocate the frame DMA buffers used by the specified buffer list.
3512
 */
3513
int alloc_frame_bufs(SLMP_INFO *info, SCADESC *buf_list,SCADESC_EX *buf_list_ex,int count)
3514
{
3515
        int i;
3516
        unsigned long phys_addr;
3517
 
3518
        for ( i = 0; i < count; i++ ) {
3519
                buf_list_ex[i].virt_addr = info->memory_base + info->port_array[0]->last_mem_alloc;
3520
                phys_addr = info->port_array[0]->last_mem_alloc;
3521
                info->port_array[0]->last_mem_alloc += SCABUFSIZE;
3522
 
3523
                buf_list[i].buf_ptr  = (unsigned short)phys_addr;
3524
                buf_list[i].buf_base = (unsigned char)(phys_addr >> 16);
3525
        }
3526
 
3527
        return 0;
3528
}
3529
 
3530
void free_dma_bufs(SLMP_INFO *info)
3531
{
3532
        info->buffer_list = NULL;
3533
        info->rx_buf_list = NULL;
3534
        info->tx_buf_list = NULL;
3535
}
3536
 
3537
/* allocate buffer large enough to hold max_frame_size.
3538
 * This buffer is used to pass an assembled frame to the line discipline.
3539
 */
3540
int alloc_tmp_rx_buf(SLMP_INFO *info)
3541
{
3542
        info->tmp_rx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
3543
        if (info->tmp_rx_buf == NULL)
3544
                return -ENOMEM;
3545
        return 0;
3546
}
3547
 
3548
void free_tmp_rx_buf(SLMP_INFO *info)
3549
{
3550
        if (info->tmp_rx_buf)
3551
                kfree(info->tmp_rx_buf);
3552
        info->tmp_rx_buf = NULL;
3553
}
3554
 
3555
int claim_resources(SLMP_INFO *info)
3556
{
3557
        if (request_mem_region(info->phys_memory_base,0x40000,"synclinkmp") == NULL) {
3558
                printk( "%s(%d):%s mem addr conflict, Addr=%08X\n",
3559
                        __FILE__,__LINE__,info->device_name, info->phys_memory_base);
3560
                goto errout;
3561
        }
3562
        else
3563
                info->shared_mem_requested = 1;
3564
 
3565
        if (request_mem_region(info->phys_lcr_base + info->lcr_offset,128,"synclinkmp") == NULL) {
3566
                printk( "%s(%d):%s lcr mem addr conflict, Addr=%08X\n",
3567
                        __FILE__,__LINE__,info->device_name, info->phys_lcr_base);
3568
                goto errout;
3569
        }
3570
        else
3571
                info->lcr_mem_requested = 1;
3572
 
3573
        if (request_mem_region(info->phys_sca_base + info->sca_offset,512,"synclinkmp") == NULL) {
3574
                printk( "%s(%d):%s sca mem addr conflict, Addr=%08X\n",
3575
                        __FILE__,__LINE__,info->device_name, info->phys_sca_base);
3576
                goto errout;
3577
        }
3578
        else
3579
                info->sca_base_requested = 1;
3580
 
3581
        if (request_mem_region(info->phys_statctrl_base + info->statctrl_offset,16,"synclinkmp") == NULL) {
3582
                printk( "%s(%d):%s stat/ctrl mem addr conflict, Addr=%08X\n",
3583
                        __FILE__,__LINE__,info->device_name, info->phys_statctrl_base);
3584
                goto errout;
3585
        }
3586
        else
3587
                info->sca_statctrl_requested = 1;
3588
 
3589
        info->memory_base = ioremap(info->phys_memory_base,SCA_MEM_SIZE);
3590
        if (!info->memory_base) {
3591
                printk( "%s(%d):%s Cant map shared memory, MemAddr=%08X\n",
3592
                        __FILE__,__LINE__,info->device_name, info->phys_memory_base );
3593
                goto errout;
3594
        }
3595
 
3596
        if ( !memory_test(info) ) {
3597
                printk( "%s(%d):Shared Memory Test failed for device %s MemAddr=%08X\n",
3598
                        __FILE__,__LINE__,info->device_name, info->phys_memory_base );
3599
                goto errout;
3600
        }
3601
 
3602
        info->lcr_base = ioremap(info->phys_lcr_base,PAGE_SIZE) + info->lcr_offset;
3603
        if (!info->lcr_base) {
3604
                printk( "%s(%d):%s Cant map LCR memory, MemAddr=%08X\n",
3605
                        __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
3606
                goto errout;
3607
        }
3608
 
3609
        info->sca_base = ioremap(info->phys_sca_base,PAGE_SIZE) + info->sca_offset;
3610
        if (!info->sca_base) {
3611
                printk( "%s(%d):%s Cant map SCA memory, MemAddr=%08X\n",
3612
                        __FILE__,__LINE__,info->device_name, info->phys_sca_base );
3613
                goto errout;
3614
        }
3615
 
3616
        info->statctrl_base = ioremap(info->phys_statctrl_base,PAGE_SIZE) + info->statctrl_offset;
3617
        if (!info->statctrl_base) {
3618
                printk( "%s(%d):%s Cant map SCA Status/Control memory, MemAddr=%08X\n",
3619
                        __FILE__,__LINE__,info->device_name, info->phys_statctrl_base );
3620
                goto errout;
3621
        }
3622
 
3623
        return 0;
3624
 
3625
errout:
3626
        release_resources( info );
3627
        return -ENODEV;
3628
}
3629
 
3630
void release_resources(SLMP_INFO *info)
3631
{
3632
        if ( debug_level >= DEBUG_LEVEL_INFO )
3633
                printk( "%s(%d):%s release_resources() entry\n",
3634
                        __FILE__,__LINE__,info->device_name );
3635
 
3636
        if ( info->irq_requested ) {
3637
                free_irq(info->irq_level, info);
3638
                info->irq_requested = 0;
3639
        }
3640
 
3641
        if ( info->shared_mem_requested ) {
3642
                release_mem_region(info->phys_memory_base,0x40000);
3643
                info->shared_mem_requested = 0;
3644
        }
3645
        if ( info->lcr_mem_requested ) {
3646
                release_mem_region(info->phys_lcr_base + info->lcr_offset,128);
3647
                info->lcr_mem_requested = 0;
3648
        }
3649
        if ( info->sca_base_requested ) {
3650
                release_mem_region(info->phys_sca_base + info->sca_offset,512);
3651
                info->sca_base_requested = 0;
3652
        }
3653
        if ( info->sca_statctrl_requested ) {
3654
                release_mem_region(info->phys_statctrl_base + info->statctrl_offset,16);
3655
                info->sca_statctrl_requested = 0;
3656
        }
3657
 
3658
        if (info->memory_base){
3659
                iounmap(info->memory_base);
3660
                info->memory_base = 0;
3661
        }
3662
 
3663
        if (info->sca_base) {
3664
                iounmap(info->sca_base - info->sca_offset);
3665
                info->sca_base=0;
3666
        }
3667
 
3668
        if (info->statctrl_base) {
3669
                iounmap(info->statctrl_base - info->statctrl_offset);
3670
                info->statctrl_base=0;
3671
        }
3672
 
3673
        if (info->lcr_base){
3674
                iounmap(info->lcr_base - info->lcr_offset);
3675
                info->lcr_base = 0;
3676
        }
3677
 
3678
        if ( debug_level >= DEBUG_LEVEL_INFO )
3679
                printk( "%s(%d):%s release_resources() exit\n",
3680
                        __FILE__,__LINE__,info->device_name );
3681
}
3682
 
3683
/* Add the specified device instance data structure to the
3684
 * global linked list of devices and increment the device count.
3685
 */
3686
void add_device(SLMP_INFO *info)
3687
{
3688
        info->next_device = NULL;
3689
        info->line = synclinkmp_device_count;
3690
        sprintf(info->device_name,"ttySLM%dp%d",info->adapter_num,info->port_num);
3691
 
3692
        if (info->line < MAX_DEVICES) {
3693
                if (maxframe[info->line])
3694
                        info->max_frame_size = maxframe[info->line];
3695
                info->dosyncppp = dosyncppp[info->line];
3696
        }
3697
 
3698
        synclinkmp_device_count++;
3699
 
3700
        if ( !synclinkmp_device_list )
3701
                synclinkmp_device_list = info;
3702
        else {
3703
                SLMP_INFO *current_dev = synclinkmp_device_list;
3704
                while( current_dev->next_device )
3705
                        current_dev = current_dev->next_device;
3706
                current_dev->next_device = info;
3707
        }
3708
 
3709
        if ( info->max_frame_size < 4096 )
3710
                info->max_frame_size = 4096;
3711
        else if ( info->max_frame_size > 65535 )
3712
                info->max_frame_size = 65535;
3713
 
3714
        printk( "SyncLink MultiPort %s: "
3715
                "Mem=(%08x %08X %08x %08X) IRQ=%d MaxFrameSize=%u\n",
3716
                info->device_name,
3717
                info->phys_sca_base,
3718
                info->phys_memory_base,
3719
                info->phys_statctrl_base,
3720
                info->phys_lcr_base,
3721
                info->irq_level,
3722
                info->max_frame_size );
3723
 
3724
#ifdef CONFIG_SYNCLINK_SYNCPPP
3725
        if (info->dosyncppp)
3726
                sppp_init(info);
3727
#endif
3728
}
3729
 
3730
/* Allocate and initialize a device instance structure
3731
 *
3732
 * Return Value:        pointer to SLMP_INFO if success, otherwise NULL
3733
 */
3734
SLMP_INFO *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
3735
{
3736
        SLMP_INFO *info;
3737
 
3738
        info = (SLMP_INFO *)kmalloc(sizeof(SLMP_INFO),
3739
                 GFP_KERNEL);
3740
 
3741
        if (!info) {
3742
                printk("%s(%d) Error can't allocate device instance data for adapter %d, port %d\n",
3743
                        __FILE__,__LINE__, adapter_num, port_num);
3744
        } else {
3745
                memset(info, 0, sizeof(SLMP_INFO));
3746
                info->magic = MGSL_MAGIC;
3747
                info->task.sync = 0;
3748
                info->task.routine = bh_handler;
3749
                info->task.data    = info;
3750
                info->max_frame_size = 4096;
3751
                info->close_delay = 5*HZ/10;
3752
                info->closing_wait = 30*HZ;
3753
                init_waitqueue_head(&info->open_wait);
3754
                init_waitqueue_head(&info->close_wait);
3755
                init_waitqueue_head(&info->status_event_wait_q);
3756
                init_waitqueue_head(&info->event_wait_q);
3757
                spin_lock_init(&info->netlock);
3758
                memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
3759
                info->idle_mode = HDLC_TXIDLE_FLAGS;
3760
                info->adapter_num = adapter_num;
3761
                info->port_num = port_num;
3762
 
3763
                /* Copy configuration info to device instance data */
3764
                info->irq_level = pdev->irq;
3765
                info->phys_lcr_base = pci_resource_start(pdev,0);
3766
                info->phys_sca_base = pci_resource_start(pdev,2);
3767
                info->phys_memory_base = pci_resource_start(pdev,3);
3768
                info->phys_statctrl_base = pci_resource_start(pdev,4);
3769
 
3770
                /* Because veremap only works on page boundaries we must map
3771
                 * a larger area than is actually implemented for the LCR
3772
                 * memory range. We map a full page starting at the page boundary.
3773
                 */
3774
                info->lcr_offset    = info->phys_lcr_base & (PAGE_SIZE-1);
3775
                info->phys_lcr_base &= ~(PAGE_SIZE-1);
3776
 
3777
                info->sca_offset    = info->phys_sca_base & (PAGE_SIZE-1);
3778
                info->phys_sca_base &= ~(PAGE_SIZE-1);
3779
 
3780
                info->statctrl_offset    = info->phys_statctrl_base & (PAGE_SIZE-1);
3781
                info->phys_statctrl_base &= ~(PAGE_SIZE-1);
3782
 
3783
                info->bus_type = MGSL_BUS_TYPE_PCI;
3784
                info->irq_flags = SA_SHIRQ;
3785
 
3786
                /* Store the PCI9050 misc control register value because a flaw
3787
                 * in the PCI9050 prevents LCR registers from being read if
3788
                 * BIOS assigns an LCR base address with bit 7 set.
3789
                 *
3790
                 * Only the misc control register is accessed for which only
3791
                 * write access is needed, so set an initial value and change
3792
                 * bits to the device instance data as we write the value
3793
                 * to the actual misc control register.
3794
                 */
3795
                info->misc_ctrl_value = 0x087e4546;
3796
 
3797
                /* initial port state is unknown - if startup errors
3798
                 * occur, init_error will be set to indicate the
3799
                 * problem. Once the port is fully initialized,
3800
                 * this value will be set to 0 to indicate the
3801
                 * port is available.
3802
                 */
3803
                info->init_error = -1;
3804
        }
3805
 
3806
        return info;
3807
}
3808
 
3809
void device_init(int adapter_num, struct pci_dev *pdev)
3810
{
3811
        SLMP_INFO *port_array[SCA_MAX_PORTS];
3812
        int port;
3813
 
3814
        /* allocate device instances for up to SCA_MAX_PORTS devices */
3815
        for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
3816
                port_array[port] = alloc_dev(adapter_num,port,pdev);
3817
                if( port_array[port] == NULL ) {
3818
                        for ( --port; port >= 0; --port )
3819
                                kfree(port_array[port]);
3820
                        return;
3821
                }
3822
        }
3823
 
3824
        /* give copy of port_array to all ports and add to device list  */
3825
        for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
3826
                memcpy(port_array[port]->port_array,port_array,sizeof(port_array));
3827
                add_device( port_array[port] );
3828
                spin_lock_init(&port_array[port]->lock);
3829
        }
3830
 
3831
        /* Allocate and claim adapter resources */
3832
        if ( !claim_resources(port_array[0]) ) {
3833
 
3834
                alloc_dma_bufs(port_array[0]);
3835
 
3836
                /* copy resource information from first port to others */
3837
                for ( port = 1; port < SCA_MAX_PORTS; ++port ) {
3838
                        port_array[port]->lock  = port_array[0]->lock;
3839
                        port_array[port]->irq_level     = port_array[0]->irq_level;
3840
                        port_array[port]->memory_base   = port_array[0]->memory_base;
3841
                        port_array[port]->sca_base      = port_array[0]->sca_base;
3842
                        port_array[port]->statctrl_base = port_array[0]->statctrl_base;
3843
                        port_array[port]->lcr_base      = port_array[0]->lcr_base;
3844
                        alloc_dma_bufs(port_array[port]);
3845
                }
3846
 
3847
                if ( request_irq(port_array[0]->irq_level,
3848
                                        synclinkmp_interrupt,
3849
                                        port_array[0]->irq_flags,
3850
                                        port_array[0]->device_name,
3851
                                        port_array[0]) < 0 ) {
3852
                        printk( "%s(%d):%s Cant request interrupt, IRQ=%d\n",
3853
                                __FILE__,__LINE__,
3854
                                port_array[0]->device_name,
3855
                                port_array[0]->irq_level );
3856
                }
3857
                else {
3858
                        port_array[0]->irq_requested = 1;
3859
                        adapter_test(port_array[0]);
3860
                }
3861
        }
3862
}
3863
 
3864
/* Driver initialization entry point.
3865
 */
3866
 
3867
static int __init synclinkmp_init(void)
3868
{
3869
        SLMP_INFO *info;
3870
 
3871
        EXPORT_NO_SYMBOLS;
3872
 
3873
        if (break_on_load) {
3874
                synclinkmp_get_text_ptr();
3875
                BREAKPOINT();
3876
        }
3877
 
3878
        printk("%s %s\n", driver_name, driver_version);
3879
 
3880
        synclinkmp_adapter_count = -1;
3881
        pci_register_driver(&synclinkmp_pci_driver);
3882
 
3883
        if ( !synclinkmp_device_list ) {
3884
                printk("%s(%d):No SyncLink devices found.\n",__FILE__,__LINE__);
3885
                return -ENODEV;
3886
        }
3887
 
3888
        memset(serial_table,0,sizeof(struct tty_struct*)*MAX_DEVICES);
3889
        memset(serial_termios,0,sizeof(struct termios*)*MAX_DEVICES);
3890
        memset(serial_termios_locked,0,sizeof(struct termios*)*MAX_DEVICES);
3891
 
3892
        /* Initialize the tty_driver structure */
3893
 
3894
        memset(&serial_driver, 0, sizeof(struct tty_driver));
3895
        serial_driver.magic = TTY_DRIVER_MAGIC;
3896
        serial_driver.driver_name = "synclinkmp";
3897
        serial_driver.name = "ttySLM";
3898
        serial_driver.major = ttymajor;
3899
        serial_driver.minor_start = 64;
3900
        serial_driver.num = synclinkmp_device_count;
3901
        serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
3902
        serial_driver.subtype = SERIAL_TYPE_NORMAL;
3903
        serial_driver.init_termios = tty_std_termios;
3904
        serial_driver.init_termios.c_cflag =
3905
                B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3906
        serial_driver.flags = TTY_DRIVER_REAL_RAW;
3907
        serial_driver.refcount = &serial_refcount;
3908
        serial_driver.table = serial_table;
3909
        serial_driver.termios = serial_termios;
3910
        serial_driver.termios_locked = serial_termios_locked;
3911
 
3912
        serial_driver.open = open;
3913
        serial_driver.close = close;
3914
        serial_driver.write = write;
3915
        serial_driver.put_char = put_char;
3916
        serial_driver.flush_chars = flush_chars;
3917
        serial_driver.write_room = write_room;
3918
        serial_driver.chars_in_buffer = chars_in_buffer;
3919
        serial_driver.flush_buffer = flush_buffer;
3920
        serial_driver.ioctl = ioctl;
3921
        serial_driver.throttle = throttle;
3922
        serial_driver.unthrottle = unthrottle;
3923
        serial_driver.send_xchar = send_xchar;
3924
        serial_driver.break_ctl = set_break;
3925
        serial_driver.wait_until_sent = wait_until_sent;
3926
        serial_driver.read_proc = read_proc;
3927
        serial_driver.set_termios = set_termios;
3928
        serial_driver.stop = tx_hold;
3929
        serial_driver.start = tx_release;
3930
        serial_driver.hangup = hangup;
3931
 
3932
        /*
3933
         * The callout device is just like normal device except for
3934
         * major number and the subtype code.
3935
         */
3936
        callout_driver = serial_driver;
3937
        callout_driver.name = "cuaSLM";
3938
        callout_driver.major = cuamajor;
3939
        callout_driver.subtype = SERIAL_TYPE_CALLOUT;
3940
        callout_driver.read_proc = 0;
3941
        callout_driver.proc_entry = 0;
3942
 
3943
        if (tty_register_driver(&serial_driver) < 0)
3944
                printk("%s(%d):Couldn't register serial driver\n",
3945
                        __FILE__,__LINE__);
3946
 
3947
        if (tty_register_driver(&callout_driver) < 0)
3948
                printk("%s(%d):Couldn't register callout driver\n",
3949
                        __FILE__,__LINE__);
3950
 
3951
        printk("%s %s, tty major#%d callout major#%d\n",
3952
                driver_name, driver_version,
3953
                serial_driver.major, callout_driver.major);
3954
 
3955
        /* Propagate these values to all device instances */
3956
 
3957
        info = synclinkmp_device_list;
3958
        while(info){
3959
                info->callout_termios = callout_driver.init_termios;
3960
                info->normal_termios  = serial_driver.init_termios;
3961
                info = info->next_device;
3962
        }
3963
 
3964
        return 0;
3965
}
3966
 
3967
static void __exit synclinkmp_exit(void)
3968
{
3969
        unsigned long flags;
3970
        int rc;
3971
        SLMP_INFO *info;
3972
        SLMP_INFO *tmp;
3973
 
3974
        printk("Unloading %s %s\n", driver_name, driver_version);
3975
        save_flags(flags);
3976
        cli();
3977
        if ((rc = tty_unregister_driver(&serial_driver)))
3978
                printk("%s(%d) failed to unregister tty driver err=%d\n",
3979
                       __FILE__,__LINE__,rc);
3980
        if ((rc = tty_unregister_driver(&callout_driver)))
3981
                printk("%s(%d) failed to unregister callout driver err=%d\n",
3982
                       __FILE__,__LINE__,rc);
3983
        restore_flags(flags);
3984
 
3985
        info = synclinkmp_device_list;
3986
        while(info) {
3987
#ifdef CONFIG_SYNCLINK_SYNCPPP
3988
                if (info->dosyncppp)
3989
                        sppp_delete(info);
3990
#endif
3991
                reset_port(info);
3992
                if ( info->port_num == 0 ) {
3993
                        if ( info->irq_requested ) {
3994
                                free_irq(info->irq_level, info);
3995
                                info->irq_requested = 0;
3996
                        }
3997
                }
3998
                info = info->next_device;
3999
        }
4000
 
4001
        /* port 0 of each adapter originally claimed
4002
         * all resources, release those now
4003
         */
4004
        info = synclinkmp_device_list;
4005
        while(info) {
4006
                free_dma_bufs(info);
4007
                free_tmp_rx_buf(info);
4008
                if ( info->port_num == 0 ) {
4009
                        spin_lock_irqsave(&info->lock,flags);
4010
                        reset_adapter(info);
4011
                        write_reg(info, LPR, 1);                /* set low power mode */
4012
                        spin_unlock_irqrestore(&info->lock,flags);
4013
                        release_resources(info);
4014
                }
4015
                tmp = info;
4016
                info = info->next_device;
4017
                kfree(tmp);
4018
        }
4019
 
4020
        pci_unregister_driver(&synclinkmp_pci_driver);
4021
}
4022
 
4023
module_init(synclinkmp_init);
4024
module_exit(synclinkmp_exit);
4025
 
4026
/* Set the port for internal loopback mode.
4027
 * The TxCLK and RxCLK signals are generated from the BRG and
4028
 * the TxD is looped back to the RxD internally.
4029
 */
4030
void enable_loopback(SLMP_INFO *info, int enable)
4031
{
4032
        if (enable) {
4033
                /* MD2 (Mode Register 2)
4034
                 * 01..00  CNCT<1..0> Channel Connection 11=Local Loopback
4035
                 */
4036
                write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) | (BIT1 + BIT0)));
4037
 
4038
                /* degate external TxC clock source */
4039
                info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
4040
                write_control_reg(info);
4041
 
4042
                /* RXS/TXS (Rx/Tx clock source)
4043
                 * 07      Reserved, must be 0
4044
                 * 06..04  Clock Source, 100=BRG
4045
                 * 03..00  Clock Divisor, 0000=1
4046
                 */
4047
                write_reg(info, RXS, 0x40);
4048
                write_reg(info, TXS, 0x40);
4049
 
4050
        } else {
4051
                /* MD2 (Mode Register 2)
4052
                 * 01..00  CNCT<1..0> Channel connection, 0=normal
4053
                 */
4054
                write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) & ~(BIT1 + BIT0)));
4055
 
4056
                /* RXS/TXS (Rx/Tx clock source)
4057
                 * 07      Reserved, must be 0
4058
                 * 06..04  Clock Source, 000=RxC/TxC Pin
4059
                 * 03..00  Clock Divisor, 0000=1
4060
                 */
4061
                write_reg(info, RXS, 0x00);
4062
                write_reg(info, TXS, 0x00);
4063
        }
4064
 
4065
        /* set LinkSpeed if available, otherwise default to 2Mbps */
4066
        if (info->params.clock_speed)
4067
                set_rate(info, info->params.clock_speed);
4068
        else
4069
                set_rate(info, 3686400);
4070
}
4071
 
4072
/* Set the baud rate register to the desired speed
4073
 *
4074
 *      data_rate       data rate of clock in bits per second
4075
 *                      A data rate of 0 disables the AUX clock.
4076
 */
4077
void set_rate( SLMP_INFO *info, u32 data_rate )
4078
{
4079
        u32 TMCValue;
4080
        unsigned char BRValue;
4081
        u32 Divisor=0;
4082
 
4083
        /* fBRG = fCLK/(TMC * 2^BR)
4084
         */
4085
        if (data_rate != 0) {
4086
                Divisor = 14745600/data_rate;
4087
                if (!Divisor)
4088
                        Divisor = 1;
4089
 
4090
                TMCValue = Divisor;
4091
 
4092
                BRValue = 0;
4093
                if (TMCValue != 1 && TMCValue != 2) {
4094
                        /* BRValue of 0 provides 50/50 duty cycle *only* when
4095
                         * TMCValue is 1 or 2. BRValue of 1 to 9 always provides
4096
                         * 50/50 duty cycle.
4097
                         */
4098
                        BRValue = 1;
4099
                        TMCValue >>= 1;
4100
                }
4101
 
4102
                /* while TMCValue is too big for TMC register, divide
4103
                 * by 2 and increment BR exponent.
4104
                 */
4105
                for(; TMCValue > 256 && BRValue < 10; BRValue++)
4106
                        TMCValue >>= 1;
4107
 
4108
                write_reg(info, TXS,
4109
                        (unsigned char)((read_reg(info, TXS) & 0xf0) | BRValue));
4110
                write_reg(info, RXS,
4111
                        (unsigned char)((read_reg(info, RXS) & 0xf0) | BRValue));
4112
                write_reg(info, TMC, (unsigned char)TMCValue);
4113
        }
4114
        else {
4115
                write_reg(info, TXS,0);
4116
                write_reg(info, RXS,0);
4117
                write_reg(info, TMC, 0);
4118
        }
4119
}
4120
 
4121
/* Disable receiver
4122
 */
4123
void rx_stop(SLMP_INFO *info)
4124
{
4125
        if (debug_level >= DEBUG_LEVEL_ISR)
4126
                printk("%s(%d):%s rx_stop()\n",
4127
                         __FILE__,__LINE__, info->device_name );
4128
 
4129
        write_reg(info, CMD, RXRESET);
4130
 
4131
        info->ie0_value &= ~RXRDYE;
4132
        write_reg(info, IE0, info->ie0_value);  /* disable Rx data interrupts */
4133
 
4134
        write_reg(info, RXDMA + DSR, 0); /* disable Rx DMA */
4135
        write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
4136
        write_reg(info, RXDMA + DIR, 0); /* disable Rx DMA interrupts */
4137
 
4138
        info->rx_enabled = 0;
4139
        info->rx_overflow = 0;
4140
}
4141
 
4142
/* enable the receiver
4143
 */
4144
void rx_start(SLMP_INFO *info)
4145
{
4146
        int i;
4147
 
4148
        if (debug_level >= DEBUG_LEVEL_ISR)
4149
                printk("%s(%d):%s rx_start()\n",
4150
                         __FILE__,__LINE__, info->device_name );
4151
 
4152
        write_reg(info, CMD, RXRESET);
4153
 
4154
        if ( info->params.mode == MGSL_MODE_HDLC ) {
4155
                /* HDLC, disabe IRQ on rxdata */
4156
                info->ie0_value &= ~RXRDYE;
4157
                write_reg(info, IE0, info->ie0_value);
4158
 
4159
                /* Reset all Rx DMA buffers and program rx dma */
4160
                write_reg(info, RXDMA + DSR, 0);         /* disable Rx DMA */
4161
                write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
4162
 
4163
                for (i = 0; i < info->rx_buf_count; i++) {
4164
                        info->rx_buf_list[i].status = 0xff;
4165
 
4166
                        // throttle to 4 shared memory writes at a time to prevent
4167
                        // hogging local bus (keep latency time for DMA requests low).
4168
                        if (!(i % 4))
4169
                                read_status_reg(info);
4170
                }
4171
                info->current_rx_buf = 0;
4172
 
4173
                /* set current/1st descriptor address */
4174
                write_reg16(info, RXDMA + CDA,
4175
                        info->rx_buf_list_ex[0].phys_entry);
4176
 
4177
                /* set new last rx descriptor address */
4178
                write_reg16(info, RXDMA + EDA,
4179
                        info->rx_buf_list_ex[info->rx_buf_count - 1].phys_entry);
4180
 
4181
                /* set buffer length (shared by all rx dma data buffers) */
4182
                write_reg16(info, RXDMA + BFL, SCABUFSIZE);
4183
 
4184
                write_reg(info, RXDMA + DIR, 0x60);     /* enable Rx DMA interrupts (EOM/BOF) */
4185
                write_reg(info, RXDMA + DSR, 0xf2);     /* clear Rx DMA IRQs, enable Rx DMA */
4186
        } else {
4187
                /* async, enable IRQ on rxdata */
4188
                info->ie0_value |= RXRDYE;
4189
                write_reg(info, IE0, info->ie0_value);
4190
        }
4191
 
4192
        write_reg(info, CMD, RXENABLE);
4193
 
4194
        info->rx_overflow = FALSE;
4195
        info->rx_enabled = 1;
4196
}
4197
 
4198
/* Enable the transmitter and send a transmit frame if
4199
 * one is loaded in the DMA buffers.
4200
 */
4201
void tx_start(SLMP_INFO *info)
4202
{
4203
        if (debug_level >= DEBUG_LEVEL_ISR)
4204
                printk("%s(%d):%s tx_start() tx_count=%d\n",
4205
                         __FILE__,__LINE__, info->device_name,info->tx_count );
4206
 
4207
        if (!info->tx_enabled ) {
4208
                write_reg(info, CMD, TXRESET);
4209
                write_reg(info, CMD, TXENABLE);
4210
                info->tx_enabled = TRUE;
4211
        }
4212
 
4213
        if ( info->tx_count ) {
4214
 
4215
                /* If auto RTS enabled and RTS is inactive, then assert */
4216
                /* RTS and set a flag indicating that the driver should */
4217
                /* negate RTS when the transmission completes. */
4218
 
4219
                info->drop_rts_on_tx_done = 0;
4220
 
4221
                if (info->params.mode != MGSL_MODE_ASYNC) {
4222
 
4223
                        if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
4224
                                get_signals( info );
4225
                                if ( !(info->serial_signals & SerialSignal_RTS) ) {
4226
                                        info->serial_signals |= SerialSignal_RTS;
4227
                                        set_signals( info );
4228
                                        info->drop_rts_on_tx_done = 1;
4229
                                }
4230
                        }
4231
 
4232
                        write_reg(info, TXDMA + DSR, 0);                 /* disable DMA channel */
4233
                        write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
4234
 
4235
                        /* set TX CDA (current descriptor address) */
4236
                        write_reg16(info, TXDMA + CDA,
4237
                                info->tx_buf_list_ex[0].phys_entry);
4238
 
4239
                        /* set TX EDA (last descriptor address) */
4240
                        write_reg16(info, TXDMA + EDA,
4241
                                info->tx_buf_list_ex[info->last_tx_buf].phys_entry);
4242
 
4243
                        /* clear IDLE and UDRN status bit */
4244
                        info->ie1_value &= ~(IDLE + UDRN);
4245
                        if (info->params.mode != MGSL_MODE_ASYNC)
4246
                                info->ie1_value |= UDRN;                /* HDLC, IRQ on underrun */
4247
                        write_reg(info, IE1, info->ie1_value);  /* enable MSCI interrupts */
4248
                        write_reg(info, SR1, (unsigned char)(IDLE + UDRN));
4249
 
4250
                        write_reg(info, TXDMA + DIR, 0x40);             /* enable Tx DMA interrupts (EOM) */
4251
                        write_reg(info, TXDMA + DSR, 0xf2);             /* clear Tx DMA IRQs, enable Tx DMA */
4252
 
4253
                        info->tx_timer.expires = jiffies + jiffies_from_ms(5000);
4254
                        add_timer(&info->tx_timer);
4255
                }
4256
                else {
4257
                        tx_load_fifo(info);
4258
                        /* async, enable IRQ on txdata */
4259
                        info->ie0_value |= TXRDYE;
4260
                        write_reg(info, IE0, info->ie0_value);
4261
                }
4262
 
4263
                info->tx_active = 1;
4264
        }
4265
}
4266
 
4267
/* stop the transmitter and DMA
4268
 */
4269
void tx_stop( SLMP_INFO *info )
4270
{
4271
        if (debug_level >= DEBUG_LEVEL_ISR)
4272
                printk("%s(%d):%s tx_stop()\n",
4273
                         __FILE__,__LINE__, info->device_name );
4274
 
4275
        del_timer(&info->tx_timer);
4276
 
4277
        write_reg(info, TXDMA + DSR, 0);         /* disable DMA channel */
4278
        write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
4279
 
4280
        write_reg(info, CMD, TXRESET);
4281
 
4282
        info->ie1_value &= ~(UDRN + IDLE);
4283
        write_reg(info, IE1, info->ie1_value);  /* disable tx status interrupts */
4284
        write_reg(info, SR1, (unsigned char)(IDLE + UDRN));     /* clear pending */
4285
 
4286
        info->ie0_value &= ~TXRDYE;
4287
        write_reg(info, IE0, info->ie0_value);  /* disable tx data interrupts */
4288
 
4289
        info->tx_enabled = 0;
4290
        info->tx_active  = 0;
4291
}
4292
 
4293
/* Fill the transmit FIFO until the FIFO is full or
4294
 * there is no more data to load.
4295
 */
4296
void tx_load_fifo(SLMP_INFO *info)
4297
{
4298
        u8 TwoBytes[2];
4299
 
4300
        /* do nothing is now tx data available and no XON/XOFF pending */
4301
 
4302
        if ( !info->tx_count && !info->x_char )
4303
                return;
4304
 
4305
        /* load the Transmit FIFO until FIFOs full or all data sent */
4306
 
4307
        while( info->tx_count && (read_reg(info,SR0) & BIT1) ) {
4308
 
4309
                /* there is more space in the transmit FIFO and */
4310
                /* there is more data in transmit buffer */
4311
 
4312
                if ( (info->tx_count > 1) && !info->x_char ) {
4313
                        /* write 16-bits */
4314
                        TwoBytes[0] = info->tx_buf[info->tx_get++];
4315
                        if (info->tx_get >= info->max_frame_size)
4316
                                info->tx_get -= info->max_frame_size;
4317
                        TwoBytes[1] = info->tx_buf[info->tx_get++];
4318
                        if (info->tx_get >= info->max_frame_size)
4319
                                info->tx_get -= info->max_frame_size;
4320
 
4321
                        write_reg16(info, TRB, *((u16 *)TwoBytes));
4322
 
4323
                        info->tx_count -= 2;
4324
                        info->icount.tx += 2;
4325
                } else {
4326
                        /* only 1 byte left to transmit or 1 FIFO slot left */
4327
 
4328
                        if (info->x_char) {
4329
                                /* transmit pending high priority char */
4330
                                write_reg(info, TRB, info->x_char);
4331
                                info->x_char = 0;
4332
                        } else {
4333
                                write_reg(info, TRB, info->tx_buf[info->tx_get++]);
4334
                                if (info->tx_get >= info->max_frame_size)
4335
                                        info->tx_get -= info->max_frame_size;
4336
                                info->tx_count--;
4337
                        }
4338
                        info->icount.tx++;
4339
                }
4340
        }
4341
}
4342
 
4343
/* Reset a port to a known state
4344
 */
4345
void reset_port(SLMP_INFO *info)
4346
{
4347
        if (info->sca_base) {
4348
 
4349
                tx_stop(info);
4350
                rx_stop(info);
4351
 
4352
                info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
4353
                set_signals(info);
4354
 
4355
                /* disable all port interrupts */
4356
                info->ie0_value = 0;
4357
                info->ie1_value = 0;
4358
                info->ie2_value = 0;
4359
                write_reg(info, IE0, info->ie0_value);
4360
                write_reg(info, IE1, info->ie1_value);
4361
                write_reg(info, IE2, info->ie2_value);
4362
 
4363
                write_reg(info, CMD, CHRESET);
4364
        }
4365
}
4366
 
4367
/* Reset all the ports to a known state.
4368
 */
4369
void reset_adapter(SLMP_INFO *info)
4370
{
4371
        int i;
4372
 
4373
        for ( i=0; i < SCA_MAX_PORTS; ++i) {
4374
                if (info->port_array[i])
4375
                        reset_port(info->port_array[i]);
4376
        }
4377
}
4378
 
4379
/* Program port for asynchronous communications.
4380
 */
4381
void async_mode(SLMP_INFO *info)
4382
{
4383
 
4384
        unsigned char RegValue;
4385
 
4386
        tx_stop(info);
4387
        rx_stop(info);
4388
 
4389
        /* MD0, Mode Register 0
4390
         *
4391
         * 07..05  PRCTL<2..0>, Protocol Mode, 000=async
4392
         * 04      AUTO, Auto-enable (RTS/CTS/DCD)
4393
         * 03      Reserved, must be 0
4394
         * 02      CRCCC, CRC Calculation, 0=disabled
4395
         * 01..00  STOP<1..0> Stop bits (00=1,10=2)
4396
         *
4397
         * 0000 0000
4398
         */
4399
        RegValue = 0x00;
4400
        if (info->params.stop_bits != 1)
4401
                RegValue |= BIT1;
4402
        write_reg(info, MD0, RegValue);
4403
 
4404
        /* MD1, Mode Register 1
4405
         *
4406
         * 07..06  BRATE<1..0>, bit rate, 00=1/1 01=1/16 10=1/32 11=1/64
4407
         * 05..04  TXCHR<1..0>, tx char size, 00=8 bits,01=7,10=6,11=5
4408
         * 03..02  RXCHR<1..0>, rx char size
4409
         * 01..00  PMPM<1..0>, Parity mode, 00=none 10=even 11=odd
4410
         *
4411
         * 0100 0000
4412
         */
4413
        RegValue = 0x40;
4414
        switch (info->params.data_bits) {
4415
        case 7: RegValue |= BIT4 + BIT2; break;
4416
        case 6: RegValue |= BIT5 + BIT3; break;
4417
        case 5: RegValue |= BIT5 + BIT4 + BIT3 + BIT2; break;
4418
        }
4419
        if (info->params.parity != ASYNC_PARITY_NONE) {
4420
                RegValue |= BIT1;
4421
                if (info->params.parity == ASYNC_PARITY_ODD)
4422
                        RegValue |= BIT0;
4423
        }
4424
        write_reg(info, MD1, RegValue);
4425
 
4426
        /* MD2, Mode Register 2
4427
         *
4428
         * 07..02  Reserved, must be 0
4429
         * 01..00  CNCT<1..0> Channel connection, 0=normal
4430
         *
4431
         * 0000 0000
4432
         */
4433
        RegValue = 0x00;
4434
        write_reg(info, MD2, RegValue);
4435
 
4436
        /* RXS, Receive clock source
4437
         *
4438
         * 07      Reserved, must be 0
4439
         * 06..04  RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
4440
         * 03..00  RXBR<3..0>, rate divisor, 0000=1
4441
         */
4442
        RegValue=BIT6;
4443
        write_reg(info, RXS, RegValue);
4444
 
4445
        /* TXS, Transmit clock source
4446
         *
4447
         * 07      Reserved, must be 0
4448
         * 06..04  RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
4449
         * 03..00  RXBR<3..0>, rate divisor, 0000=1
4450
         */
4451
        RegValue=BIT6;
4452
        write_reg(info, TXS, RegValue);
4453
 
4454
        /* Control Register
4455
         *
4456
         * 6,4,2,0  CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
4457
         */
4458
        info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
4459
        write_control_reg(info);
4460
 
4461
        tx_set_idle(info);
4462
 
4463
        /* RRC Receive Ready Control 0
4464
         *
4465
         * 07..05  Reserved, must be 0
4466
         * 04..00  RRC<4..0> Rx FIFO trigger active 0x00 = 1 byte
4467
         */
4468
        write_reg(info, TRC0, 0x00);
4469
 
4470
        /* TRC0 Transmit Ready Control 0
4471
         *
4472
         * 07..05  Reserved, must be 0
4473
         * 04..00  TRC<4..0> Tx FIFO trigger active 0x10 = 16 bytes
4474
         */
4475
        write_reg(info, TRC0, 0x10);
4476
 
4477
        /* TRC1 Transmit Ready Control 1
4478
         *
4479
         * 07..05  Reserved, must be 0
4480
         * 04..00  TRC<4..0> Tx FIFO trigger inactive 0x1e = 31 bytes (full-1)
4481
         */
4482
        write_reg(info, TRC1, 0x1e);
4483
 
4484
        /* CTL, MSCI control register
4485
         *
4486
         * 07..06  Reserved, set to 0
4487
         * 05      UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
4488
         * 04      IDLC, idle control, 0=mark 1=idle register
4489
         * 03      BRK, break, 0=off 1 =on (async)
4490
         * 02      SYNCLD, sync char load enable (BSC) 1=enabled
4491
         * 01      GOP, go active on poll (LOOP mode) 1=enabled
4492
         * 00      RTS, RTS output control, 0=active 1=inactive
4493
         *
4494
         * 0001 0001
4495
         */
4496
        RegValue = 0x10;
4497
        if (!(info->serial_signals & SerialSignal_RTS))
4498
                RegValue |= 0x01;
4499
        write_reg(info, CTL, RegValue);
4500
 
4501
        /* enable status interrupts */
4502
        info->ie0_value |= TXINTE + RXINTE;
4503
        write_reg(info, IE0, info->ie0_value);
4504
 
4505
        /* enable break detect interrupt */
4506
        info->ie1_value = BRKD;
4507
        write_reg(info, IE1, info->ie1_value);
4508
 
4509
        /* enable rx overrun interrupt */
4510
        info->ie2_value = OVRN;
4511
        write_reg(info, IE2, info->ie2_value);
4512
 
4513
        set_rate( info, info->params.data_rate * 16 );
4514
 
4515
        if (info->params.loopback)
4516
                enable_loopback(info,1);
4517
}
4518
 
4519
/* Program the SCA for HDLC communications.
4520
 */
4521
void hdlc_mode(SLMP_INFO *info)
4522
{
4523
        unsigned char RegValue;
4524
        u32 DpllDivisor;
4525
 
4526
        // Can't use DPLL because SCA outputs recovered clock on RxC when
4527
        // DPLL mode selected. This causes output contention with RxC receiver.
4528
        // Use of DPLL would require external hardware to disable RxC receiver
4529
        // when DPLL mode selected.
4530
        info->params.flags &= ~(HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL);
4531
 
4532
        /* disable DMA interrupts */
4533
        write_reg(info, TXDMA + DIR, 0);
4534
        write_reg(info, RXDMA + DIR, 0);
4535
 
4536
        /* MD0, Mode Register 0
4537
         *
4538
         * 07..05  PRCTL<2..0>, Protocol Mode, 100=HDLC
4539
         * 04      AUTO, Auto-enable (RTS/CTS/DCD)
4540
         * 03      Reserved, must be 0
4541
         * 02      CRCCC, CRC Calculation, 1=enabled
4542
         * 01      CRC1, CRC selection, 0=CRC-16,1=CRC-CCITT-16
4543
         * 00      CRC0, CRC initial value, 1 = all 1s
4544
         *
4545
         * 1000 0001
4546
         */
4547
        RegValue = 0x81;
4548
        if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4549
                RegValue |= BIT4;
4550
        if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4551
                RegValue |= BIT4;
4552
        if (info->params.crc_type == HDLC_CRC_16_CCITT)
4553
                RegValue |= BIT2 + BIT1;
4554
        write_reg(info, MD0, RegValue);
4555
 
4556
        /* MD1, Mode Register 1
4557
         *
4558
         * 07..06  ADDRS<1..0>, Address detect, 00=no addr check
4559
         * 05..04  TXCHR<1..0>, tx char size, 00=8 bits
4560
         * 03..02  RXCHR<1..0>, rx char size, 00=8 bits
4561
         * 01..00  PMPM<1..0>, Parity mode, 00=no parity
4562
         *
4563
         * 0000 0000
4564
         */
4565
        RegValue = 0x00;
4566
        write_reg(info, MD1, RegValue);
4567
 
4568
        /* MD2, Mode Register 2
4569
         *
4570
         * 07      NRZFM, 0=NRZ, 1=FM
4571
         * 06..05  CODE<1..0> Encoding, 00=NRZ
4572
         * 04..03  DRATE<1..0> DPLL Divisor, 00=8
4573
         * 02      Reserved, must be 0
4574
         * 01..00  CNCT<1..0> Channel connection, 0=normal
4575
         *
4576
         * 0000 0000
4577
         */
4578
        RegValue = 0x00;
4579
        switch(info->params.encoding) {
4580
        case HDLC_ENCODING_NRZI:          RegValue |= BIT5; break;
4581
        case HDLC_ENCODING_BIPHASE_MARK:  RegValue |= BIT7 + BIT5; break; /* aka FM1 */
4582
        case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT7 + BIT6; break; /* aka FM0 */
4583
        case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT7; break;      /* aka Manchester */
4584
#if 0
4585
        case HDLC_ENCODING_NRZB:                                        /* not supported */
4586
        case HDLC_ENCODING_NRZI_MARK:                                   /* not supported */
4587
        case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:                          /* not supported */
4588
#endif
4589
        }
4590
        if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
4591
                DpllDivisor = 16;
4592
                RegValue |= BIT3;
4593
        } else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
4594
                DpllDivisor = 8;
4595
        } else {
4596
                DpllDivisor = 32;
4597
                RegValue |= BIT4;
4598
        }
4599
        write_reg(info, MD2, RegValue);
4600
 
4601
 
4602
        /* RXS, Receive clock source
4603
         *
4604
         * 07      Reserved, must be 0
4605
         * 06..04  RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
4606
         * 03..00  RXBR<3..0>, rate divisor, 0000=1
4607
         */
4608
        RegValue=0;
4609
        if (info->params.flags & HDLC_FLAG_RXC_BRG)
4610
                RegValue |= BIT6;
4611
        if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4612
                RegValue |= BIT6 + BIT5;
4613
        write_reg(info, RXS, RegValue);
4614
 
4615
        /* TXS, Transmit clock source
4616
         *
4617
         * 07      Reserved, must be 0
4618
         * 06..04  RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
4619
         * 03..00  RXBR<3..0>, rate divisor, 0000=1
4620
         */
4621
        RegValue=0;
4622
        if (info->params.flags & HDLC_FLAG_TXC_BRG)
4623
                RegValue |= BIT6;
4624
        if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4625
                RegValue |= BIT6 + BIT5;
4626
        write_reg(info, TXS, RegValue);
4627
 
4628
        if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4629
                set_rate(info, info->params.clock_speed * DpllDivisor);
4630
        else
4631
                set_rate(info, info->params.clock_speed);
4632
 
4633
        /* GPDATA (General Purpose I/O Data Register)
4634
         *
4635
         * 6,4,2,0  CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
4636
         */
4637
        if (info->params.flags & HDLC_FLAG_TXC_BRG)
4638
                info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
4639
        else
4640
                info->port_array[0]->ctrlreg_value &= ~(BIT0 << (info->port_num * 2));
4641
        write_control_reg(info);
4642
 
4643
        /* RRC Receive Ready Control 0
4644
         *
4645
         * 07..05  Reserved, must be 0
4646
         * 04..00  RRC<4..0> Rx FIFO trigger active
4647
         */
4648
        write_reg(info, RRC, rx_active_fifo_level);
4649
 
4650
        /* TRC0 Transmit Ready Control 0
4651
         *
4652
         * 07..05  Reserved, must be 0
4653
         * 04..00  TRC<4..0> Tx FIFO trigger active
4654
         */
4655
        write_reg(info, TRC0, tx_active_fifo_level);
4656
 
4657
        /* TRC1 Transmit Ready Control 1
4658
         *
4659
         * 07..05  Reserved, must be 0
4660
         * 04..00  TRC<4..0> Tx FIFO trigger inactive 0x1f = 32 bytes (full)
4661
         */
4662
        write_reg(info, TRC1, (unsigned char)(tx_negate_fifo_level - 1));
4663
 
4664
        /* DMR, DMA Mode Register
4665
         *
4666
         * 07..05  Reserved, must be 0
4667
         * 04      TMOD, Transfer Mode: 1=chained-block
4668
         * 03      Reserved, must be 0
4669
         * 02      NF, Number of Frames: 1=multi-frame
4670
         * 01      CNTE, Frame End IRQ Counter enable: 0=disabled
4671
         * 00      Reserved, must be 0
4672
         *
4673
         * 0001 0100
4674
         */
4675
        write_reg(info, TXDMA + DMR, 0x14);
4676
        write_reg(info, RXDMA + DMR, 0x14);
4677
 
4678
        /* Set chain pointer base (upper 8 bits of 24 bit addr) */
4679
        write_reg(info, RXDMA + CPB,
4680
                (unsigned char)(info->buffer_list_phys >> 16));
4681
 
4682
        /* Set chain pointer base (upper 8 bits of 24 bit addr) */
4683
        write_reg(info, TXDMA + CPB,
4684
                (unsigned char)(info->buffer_list_phys >> 16));
4685
 
4686
        /* enable status interrupts. other code enables/disables
4687
         * the individual sources for these two interrupt classes.
4688
         */
4689
        info->ie0_value |= TXINTE + RXINTE;
4690
        write_reg(info, IE0, info->ie0_value);
4691
 
4692
        /* CTL, MSCI control register
4693
         *
4694
         * 07..06  Reserved, set to 0
4695
         * 05      UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
4696
         * 04      IDLC, idle control, 0=mark 1=idle register
4697
         * 03      BRK, break, 0=off 1 =on (async)
4698
         * 02      SYNCLD, sync char load enable (BSC) 1=enabled
4699
         * 01      GOP, go active on poll (LOOP mode) 1=enabled
4700
         * 00      RTS, RTS output control, 0=active 1=inactive
4701
         *
4702
         * 0001 0001
4703
         */
4704
        RegValue = 0x10;
4705
        if (!(info->serial_signals & SerialSignal_RTS))
4706
                RegValue |= 0x01;
4707
        write_reg(info, CTL, RegValue);
4708
 
4709
        /* preamble not supported ! */
4710
 
4711
        tx_set_idle(info);
4712
        tx_stop(info);
4713
        rx_stop(info);
4714
 
4715
        set_rate(info, info->params.clock_speed);
4716
 
4717
        if (info->params.loopback)
4718
                enable_loopback(info,1);
4719
}
4720
 
4721
/* Set the transmit HDLC idle mode
4722
 */
4723
void tx_set_idle(SLMP_INFO *info)
4724
{
4725
        unsigned char RegValue = 0xff;
4726
 
4727
        /* Map API idle mode to SCA register bits */
4728
        switch(info->idle_mode) {
4729
        case HDLC_TXIDLE_FLAGS:                 RegValue = 0x7e; break;
4730
        case HDLC_TXIDLE_ALT_ZEROS_ONES:        RegValue = 0xaa; break;
4731
        case HDLC_TXIDLE_ZEROS:                 RegValue = 0x00; break;
4732
        case HDLC_TXIDLE_ONES:                  RegValue = 0xff; break;
4733
        case HDLC_TXIDLE_ALT_MARK_SPACE:        RegValue = 0xaa; break;
4734
        case HDLC_TXIDLE_SPACE:                 RegValue = 0x00; break;
4735
        case HDLC_TXIDLE_MARK:                  RegValue = 0xff; break;
4736
        }
4737
 
4738
        write_reg(info, IDL, RegValue);
4739
}
4740
 
4741
/* Query the adapter for the state of the V24 status (input) signals.
4742
 */
4743
void get_signals(SLMP_INFO *info)
4744
{
4745
        u16 status = read_reg(info, SR3);
4746
        u16 gpstatus = read_status_reg(info);
4747
        u16 testbit;
4748
 
4749
        /* clear all serial signals except DTR and RTS */
4750
        info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
4751
 
4752
        /* set serial signal bits to reflect MISR */
4753
 
4754
        if (!(status & BIT3))
4755
                info->serial_signals |= SerialSignal_CTS;
4756
 
4757
        if ( !(status & BIT2))
4758
                info->serial_signals |= SerialSignal_DCD;
4759
 
4760
        testbit = BIT1 << (info->port_num * 2); // Port 0..3 RI is GPDATA<1,3,5,7>
4761
        if (!(gpstatus & testbit))
4762
                info->serial_signals |= SerialSignal_RI;
4763
 
4764
        testbit = BIT0 << (info->port_num * 2); // Port 0..3 DSR is GPDATA<0,2,4,6>
4765
        if (!(gpstatus & testbit))
4766
                info->serial_signals |= SerialSignal_DSR;
4767
}
4768
 
4769
/* Set the state of DTR and RTS based on contents of
4770
 * serial_signals member of device context.
4771
 */
4772
void set_signals(SLMP_INFO *info)
4773
{
4774
        unsigned char RegValue;
4775
        u16 EnableBit;
4776
 
4777
        RegValue = read_reg(info, CTL);
4778
        if (info->serial_signals & SerialSignal_RTS)
4779
                RegValue &= ~BIT0;
4780
        else
4781
                RegValue |= BIT0;
4782
        write_reg(info, CTL, RegValue);
4783
 
4784
        // Port 0..3 DTR is ctrl reg <1,3,5,7>
4785
        EnableBit = BIT1 << (info->port_num*2);
4786
        if (info->serial_signals & SerialSignal_DTR)
4787
                info->port_array[0]->ctrlreg_value &= ~EnableBit;
4788
        else
4789
                info->port_array[0]->ctrlreg_value |= EnableBit;
4790
        write_control_reg(info);
4791
}
4792
 
4793
/*******************/
4794
/* DMA Buffer Code */
4795
/*******************/
4796
 
4797
/* Set the count for all receive buffers to SCABUFSIZE
4798
 * and set the current buffer to the first buffer. This effectively
4799
 * makes all buffers free and discards any data in buffers.
4800
 */
4801
void rx_reset_buffers(SLMP_INFO *info)
4802
{
4803
        rx_free_frame_buffers(info, 0, info->rx_buf_count - 1);
4804
}
4805
 
4806
/* Free the buffers used by a received frame
4807
 *
4808
 * info   pointer to device instance data
4809
 * first  index of 1st receive buffer of frame
4810
 * last   index of last receive buffer of frame
4811
 */
4812
void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last)
4813
{
4814
        int done = 0;
4815
 
4816
        while(!done) {
4817
                /* reset current buffer for reuse */
4818
                info->rx_buf_list[first].status = 0xff;
4819
 
4820
                if (first == last) {
4821
                        done = 1;
4822
                        /* set new last rx descriptor address */
4823
                        write_reg16(info, RXDMA + EDA, info->rx_buf_list_ex[first].phys_entry);
4824
                }
4825
 
4826
                first++;
4827
                if (first == info->rx_buf_count)
4828
                        first = 0;
4829
        }
4830
 
4831
        /* set current buffer to next buffer after last buffer of frame */
4832
        info->current_rx_buf = first;
4833
}
4834
 
4835
/* Return a received frame from the receive DMA buffers.
4836
 * Only frames received without errors are returned.
4837
 *
4838
 * Return Value:        1 if frame returned, otherwise 0
4839
 */
4840
int rx_get_frame(SLMP_INFO *info)
4841
{
4842
        unsigned int StartIndex, EndIndex;      /* index of 1st and last buffers of Rx frame */
4843
        unsigned short status;
4844
        unsigned int framesize = 0;
4845
        int ReturnCode = 0;
4846
        unsigned long flags;
4847
        struct tty_struct *tty = info->tty;
4848
        unsigned char addr_field = 0xff;
4849
        SCADESC *desc;
4850
        SCADESC_EX *desc_ex;
4851
 
4852
CheckAgain:
4853
        /* assume no frame returned, set zero length */
4854
        framesize = 0;
4855
        addr_field = 0xff;
4856
 
4857
        /*
4858
         * current_rx_buf points to the 1st buffer of the next available
4859
         * receive frame. To find the last buffer of the frame look for
4860
         * a non-zero status field in the buffer entries. (The status
4861
         * field is set by the 16C32 after completing a receive frame.
4862
         */
4863
        StartIndex = EndIndex = info->current_rx_buf;
4864
 
4865
        for ( ;; ) {
4866
                desc = &info->rx_buf_list[EndIndex];
4867
                desc_ex = &info->rx_buf_list_ex[EndIndex];
4868
 
4869
                if (desc->status == 0xff)
4870
                        goto Cleanup;   /* current desc still in use, no frames available */
4871
 
4872
                if (framesize == 0 && info->params.addr_filter != 0xff)
4873
                        addr_field = desc_ex->virt_addr[0];
4874
 
4875
                framesize += desc->length;
4876
 
4877
                /* Status != 0 means last buffer of frame */
4878
                if (desc->status)
4879
                        break;
4880
 
4881
                EndIndex++;
4882
                if (EndIndex == info->rx_buf_count)
4883
                        EndIndex = 0;
4884
 
4885
                if (EndIndex == info->current_rx_buf) {
4886
                        /* all buffers have been 'used' but none mark      */
4887
                        /* the end of a frame. Reset buffers and receiver. */
4888
                        if ( info->rx_enabled ){
4889
                                spin_lock_irqsave(&info->lock,flags);
4890
                                rx_start(info);
4891
                                spin_unlock_irqrestore(&info->lock,flags);
4892
                        }
4893
                        goto Cleanup;
4894
                }
4895
 
4896
        }
4897
 
4898
        /* check status of receive frame */
4899
 
4900
        /* frame status is byte stored after frame data
4901
         *
4902
         * 7 EOM (end of msg), 1 = last buffer of frame
4903
         * 6 Short Frame, 1 = short frame
4904
         * 5 Abort, 1 = frame aborted
4905
         * 4 Residue, 1 = last byte is partial
4906
         * 3 Overrun, 1 = overrun occurred during frame reception
4907
         * 2 CRC,     1 = CRC error detected
4908
         *
4909
         */
4910
        status = desc->status;
4911
 
4912
        /* ignore CRC bit if not using CRC (bit is undefined) */
4913
        /* Note:CRC is not save to data buffer */
4914
        if (info->params.crc_type == HDLC_CRC_NONE)
4915
                status &= ~BIT2;
4916
 
4917
        if (framesize == 0 ||
4918
                 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4919
                /* discard 0 byte frames, this seems to occur sometime
4920
                 * when remote is idling flags.
4921
                 */
4922
                rx_free_frame_buffers(info, StartIndex, EndIndex);
4923
                goto CheckAgain;
4924
        }
4925
 
4926
        if (framesize < 2)
4927
                status |= BIT6;
4928
 
4929
        if (status & (BIT6+BIT5+BIT3+BIT2)) {
4930
                /* received frame has errors,
4931
                 * update counts and mark frame size as 0
4932
                 */
4933
                if (status & BIT6)
4934
                        info->icount.rxshort++;
4935
                else if (status & BIT5)
4936
                        info->icount.rxabort++;
4937
                else if (status & BIT3)
4938
                        info->icount.rxover++;
4939
                else
4940
                        info->icount.rxcrc++;
4941
 
4942
                framesize = 0;
4943
 
4944
#ifdef CONFIG_SYNCLINK_SYNCPPP
4945
                info->netstats.rx_errors++;
4946
                info->netstats.rx_frame_errors++;
4947
#endif
4948
        }
4949
 
4950
        if ( debug_level >= DEBUG_LEVEL_BH )
4951
                printk("%s(%d):%s rx_get_frame() status=%04X size=%d\n",
4952
                        __FILE__,__LINE__,info->device_name,status,framesize);
4953
 
4954
        if ( debug_level >= DEBUG_LEVEL_DATA )
4955
                trace_block(info,info->rx_buf_list_ex[StartIndex].virt_addr,
4956
                        MIN(framesize,SCABUFSIZE),0);
4957
 
4958
        if (framesize) {
4959
                if (framesize > info->max_frame_size)
4960
                        info->icount.rxlong++;
4961
                else {
4962
                        /* copy dma buffer(s) to contiguous intermediate buffer */
4963
                        int copy_count = framesize;
4964
                        int index = StartIndex;
4965
                        unsigned char *ptmp = info->tmp_rx_buf;
4966
                        info->tmp_rx_buf_count = framesize;
4967
 
4968
                        info->icount.rxok++;
4969
 
4970
                        while(copy_count) {
4971
                                int partial_count = MIN(copy_count,SCABUFSIZE);
4972
                                memcpy( ptmp,
4973
                                        info->rx_buf_list_ex[index].virt_addr,
4974
                                        partial_count );
4975
                                ptmp += partial_count;
4976
                                copy_count -= partial_count;
4977
 
4978
                                if ( ++index == info->rx_buf_count )
4979
                                        index = 0;
4980
                        }
4981
 
4982
#ifdef CONFIG_SYNCLINK_SYNCPPP
4983
                        if (info->netcount) {
4984
                                /* pass frame to syncppp device */
4985
                                sppp_rx_done(info,info->tmp_rx_buf,framesize);
4986
                        }
4987
                        else
4988
#endif
4989
                        {
4990
                                if ( tty && tty->ldisc.receive_buf ) {
4991
                                        /* Call the line discipline receive callback directly. */
4992
                                        tty->ldisc.receive_buf(tty,
4993
                                                info->tmp_rx_buf,
4994
                                                info->flag_buf,
4995
                                                framesize);
4996
                                }
4997
                        }
4998
                }
4999
        }
5000
        /* Free the buffers used by this frame. */
5001
        rx_free_frame_buffers( info, StartIndex, EndIndex );
5002
 
5003
        ReturnCode = 1;
5004
 
5005
Cleanup:
5006
        if ( info->rx_enabled && info->rx_overflow ) {
5007
                /* Receiver is enabled, but needs to restarted due to
5008
                 * rx buffer overflow. If buffers are empty, restart receiver.
5009
                 */
5010
                if (info->rx_buf_list[EndIndex].status == 0xff) {
5011
                        spin_lock_irqsave(&info->lock,flags);
5012
                        rx_start(info);
5013
                        spin_unlock_irqrestore(&info->lock,flags);
5014
                }
5015
        }
5016
 
5017
        return ReturnCode;
5018
}
5019
 
5020
/* load the transmit DMA buffer with data
5021
 */
5022
void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count)
5023
{
5024
        unsigned short copy_count;
5025
        unsigned int i = 0;
5026
        SCADESC *desc;
5027
        SCADESC_EX *desc_ex;
5028
 
5029
        if ( debug_level >= DEBUG_LEVEL_DATA )
5030
                trace_block(info,buf, MIN(count,SCABUFSIZE), 1);
5031
 
5032
        /* Copy source buffer to one or more DMA buffers, starting with
5033
         * the first transmit dma buffer.
5034
         */
5035
        for(i=0;;)
5036
        {
5037
                copy_count = MIN(count,SCABUFSIZE);
5038
 
5039
                desc = &info->tx_buf_list[i];
5040
                desc_ex = &info->tx_buf_list_ex[i];
5041
 
5042
                load_pci_memory(info, desc_ex->virt_addr,buf,copy_count);
5043
 
5044
                desc->length = copy_count;
5045
                desc->status = 0;
5046
 
5047
                buf += copy_count;
5048
                count -= copy_count;
5049
 
5050
                if (!count)
5051
                        break;
5052
 
5053
                i++;
5054
                if (i >= info->tx_buf_count)
5055
                        i = 0;
5056
        }
5057
 
5058
        info->tx_buf_list[i].status = 0x81;     /* set EOM and EOT status */
5059
        info->last_tx_buf = ++i;
5060
}
5061
 
5062
int register_test(SLMP_INFO *info)
5063
{
5064
        static unsigned char testval[] = {0x00, 0xff, 0xaa, 0x55, 0x69, 0x96};
5065
        static unsigned int count = sizeof(testval)/sizeof(unsigned char);
5066
        unsigned int i;
5067
        int rc = TRUE;
5068
        unsigned long flags;
5069
 
5070
        spin_lock_irqsave(&info->lock,flags);
5071
        reset_port(info);
5072
 
5073
        /* assume failure */
5074
        info->init_error = DiagStatus_AddressFailure;
5075
 
5076
        /* Write bit patterns to various registers but do it out of */
5077
        /* sync, then read back and verify values. */
5078
 
5079
        for (i = 0 ; i < count ; i++) {
5080
                write_reg(info, TMC, testval[i]);
5081
                write_reg(info, IDL, testval[(i+1)%count]);
5082
                write_reg(info, SA0, testval[(i+2)%count]);
5083
                write_reg(info, SA1, testval[(i+3)%count]);
5084
 
5085
                if ( (read_reg(info, TMC) != testval[i]) ||
5086
                          (read_reg(info, IDL) != testval[(i+1)%count]) ||
5087
                          (read_reg(info, SA0) != testval[(i+2)%count]) ||
5088
                          (read_reg(info, SA1) != testval[(i+3)%count]) )
5089
                {
5090
                        rc = FALSE;
5091
                        break;
5092
                }
5093
        }
5094
 
5095
        reset_port(info);
5096
        spin_unlock_irqrestore(&info->lock,flags);
5097
 
5098
        return rc;
5099
}
5100
 
5101
int irq_test(SLMP_INFO *info)
5102
{
5103
        unsigned long timeout;
5104
        unsigned long flags;
5105
 
5106
        unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
5107
 
5108
        spin_lock_irqsave(&info->lock,flags);
5109
        reset_port(info);
5110
 
5111
        /* assume failure */
5112
        info->init_error = DiagStatus_IrqFailure;
5113
        info->irq_occurred = FALSE;
5114
 
5115
        /* setup timer0 on SCA0 to interrupt */
5116
 
5117
        /* IER2<7..4> = timer<3..0> interrupt enables (1=enabled) */
5118
        write_reg(info, IER2, (unsigned char)((info->port_num & 1) ? BIT6 : BIT4));
5119
 
5120
        write_reg(info, (unsigned char)(timer + TEPR), 0);       /* timer expand prescale */
5121
        write_reg16(info, (unsigned char)(timer + TCONR), 1);   /* timer constant */
5122
 
5123
 
5124
        /* TMCS, Timer Control/Status Register
5125
         *
5126
         * 07      CMF, Compare match flag (read only) 1=match
5127
         * 06      ECMI, CMF Interrupt Enable: 1=enabled
5128
         * 05      Reserved, must be 0
5129
         * 04      TME, Timer Enable
5130
         * 03..00  Reserved, must be 0
5131
         *
5132
         * 0101 0000
5133
         */
5134
        write_reg(info, (unsigned char)(timer + TMCS), 0x50);
5135
 
5136
        spin_unlock_irqrestore(&info->lock,flags);
5137
 
5138
        timeout=100;
5139
        while( timeout-- && !info->irq_occurred ) {
5140
                set_current_state(TASK_INTERRUPTIBLE);
5141
                schedule_timeout(jiffies_from_ms(10));
5142
        }
5143
 
5144
        spin_lock_irqsave(&info->lock,flags);
5145
        reset_port(info);
5146
        spin_unlock_irqrestore(&info->lock,flags);
5147
 
5148
        return info->irq_occurred;
5149
}
5150
 
5151
/* initialize individual SCA device (2 ports)
5152
 */
5153
int sca_init(SLMP_INFO *info)
5154
{
5155
        /* set wait controller to single mem partition (low), no wait states */
5156
        write_reg(info, PABR0, 0);       /* wait controller addr boundary 0 */
5157
        write_reg(info, PABR1, 0);       /* wait controller addr boundary 1 */
5158
        write_reg(info, WCRL, 0);        /* wait controller low range */
5159
        write_reg(info, WCRM, 0);        /* wait controller mid range */
5160
        write_reg(info, WCRH, 0);        /* wait controller high range */
5161
 
5162
        /* DPCR, DMA Priority Control
5163
         *
5164
         * 07..05  Not used, must be 0
5165
         * 04      BRC, bus release condition: 0=all transfers complete
5166
         * 03      CCC, channel change condition: 0=every cycle
5167
         * 02..00  PR<2..0>, priority 100=round robin
5168
         *
5169
         * 00000100 = 0x04
5170
         */
5171
        write_reg(info, DPCR, dma_priority);
5172
 
5173
        /* DMA Master Enable, BIT7: 1=enable all channels */
5174
        write_reg(info, DMER, 0x80);
5175
 
5176
        /* enable all interrupt classes */
5177
        write_reg(info, IER0, 0xff);    /* TxRDY,RxRDY,TxINT,RxINT (ports 0-1) */
5178
        write_reg(info, IER1, 0xff);    /* DMIB,DMIA (channels 0-3) */
5179
        write_reg(info, IER2, 0xf0);    /* TIRQ (timers 0-3) */
5180
 
5181
        /* ITCR, interrupt control register
5182
         * 07      IPC, interrupt priority, 0=MSCI->DMA
5183
         * 06..05  IAK<1..0>, Acknowledge cycle, 00=non-ack cycle
5184
         * 04      VOS, Vector Output, 0=unmodified vector
5185
         * 03..00  Reserved, must be 0
5186
         */
5187
        write_reg(info, ITCR, 0);
5188
 
5189
        return TRUE;
5190
}
5191
 
5192
/* initialize adapter hardware
5193
 */
5194
int init_adapter(SLMP_INFO *info)
5195
{
5196
        int i;
5197
 
5198
        /* Set BIT30 of Local Control Reg 0x50 to reset SCA */
5199
        volatile u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
5200
        u32 readval;
5201
 
5202
        info->misc_ctrl_value |= BIT30;
5203
        *MiscCtrl = info->misc_ctrl_value;
5204
 
5205
        /*
5206
         * Force at least 170ns delay before clearing
5207
         * reset bit. Each read from LCR takes at least
5208
         * 30ns so 10 times for 300ns to be safe.
5209
         */
5210
        for(i=0;i<10;i++)
5211
                readval = *MiscCtrl;
5212
 
5213
        info->misc_ctrl_value &= ~BIT30;
5214
        *MiscCtrl = info->misc_ctrl_value;
5215
 
5216
        /* init control reg (all DTRs off, all clksel=input) */
5217
        info->ctrlreg_value = 0xaa;
5218
        write_control_reg(info);
5219
 
5220
        {
5221
                volatile u32 *LCR1BRDR = (u32 *)(info->lcr_base + 0x2c);
5222
                lcr1_brdr_value &= ~(BIT5 + BIT4 + BIT3);
5223
 
5224
                switch(read_ahead_count)
5225
                {
5226
                case 16:
5227
                        lcr1_brdr_value |= BIT5 + BIT4 + BIT3;
5228
                        break;
5229
                case 8:
5230
                        lcr1_brdr_value |= BIT5 + BIT4;
5231
                        break;
5232
                case 4:
5233
                        lcr1_brdr_value |= BIT5 + BIT3;
5234
                        break;
5235
                case 0:
5236
                        lcr1_brdr_value |= BIT5;
5237
                        break;
5238
                }
5239
 
5240
                *LCR1BRDR = lcr1_brdr_value;
5241
                *MiscCtrl = misc_ctrl_value;
5242
        }
5243
 
5244
        sca_init(info->port_array[0]);
5245
        sca_init(info->port_array[2]);
5246
 
5247
        return TRUE;
5248
}
5249
 
5250
/* Loopback an HDLC frame to test the hardware
5251
 * interrupt and DMA functions.
5252
 */
5253
int loopback_test(SLMP_INFO *info)
5254
{
5255
#define TESTFRAMESIZE 20
5256
 
5257
        unsigned long timeout;
5258
        u16 count = TESTFRAMESIZE;
5259
        unsigned char buf[TESTFRAMESIZE];
5260
        int rc = FALSE;
5261
        unsigned long flags;
5262
 
5263
        struct tty_struct *oldtty = info->tty;
5264
        u32 speed = info->params.clock_speed;
5265
 
5266
        info->params.clock_speed = 3686400;
5267
        info->tty = 0;
5268
 
5269
        /* assume failure */
5270
        info->init_error = DiagStatus_DmaFailure;
5271
 
5272
        /* build and send transmit frame */
5273
        for (count = 0; count < TESTFRAMESIZE;++count)
5274
                buf[count] = (unsigned char)count;
5275
 
5276
        memset(info->tmp_rx_buf,0,TESTFRAMESIZE);
5277
 
5278
        /* program hardware for HDLC and enabled receiver */
5279
        spin_lock_irqsave(&info->lock,flags);
5280
        hdlc_mode(info);
5281
        enable_loopback(info,1);
5282
        rx_start(info);
5283
        info->tx_count = count;
5284
        tx_load_dma_buffer(info,buf,count);
5285
        tx_start(info);
5286
        spin_unlock_irqrestore(&info->lock,flags);
5287
 
5288
        /* wait for receive complete */
5289
        /* Set a timeout for waiting for interrupt. */
5290
        for ( timeout = 100; timeout; --timeout ) {
5291
                set_current_state(TASK_INTERRUPTIBLE);
5292
                schedule_timeout(jiffies_from_ms(10));
5293
 
5294
                if (rx_get_frame(info)) {
5295
                        rc = TRUE;
5296
                        break;
5297
                }
5298
        }
5299
 
5300
        /* verify received frame length and contents */
5301
        if (rc == TRUE &&
5302
                ( info->tmp_rx_buf_count != count ||
5303
                  memcmp(buf, info->tmp_rx_buf,count))) {
5304
                rc = FALSE;
5305
        }
5306
 
5307
        spin_lock_irqsave(&info->lock,flags);
5308
        reset_adapter(info);
5309
        spin_unlock_irqrestore(&info->lock,flags);
5310
 
5311
        info->params.clock_speed = speed;
5312
        info->tty = oldtty;
5313
 
5314
        return rc;
5315
}
5316
 
5317
/* Perform diagnostics on hardware
5318
 */
5319
int adapter_test( SLMP_INFO *info )
5320
{
5321
        unsigned long flags;
5322
        if ( debug_level >= DEBUG_LEVEL_INFO )
5323
                printk( "%s(%d):Testing device %s\n",
5324
                        __FILE__,__LINE__,info->device_name );
5325
 
5326
        spin_lock_irqsave(&info->lock,flags);
5327
        init_adapter(info);
5328
        spin_unlock_irqrestore(&info->lock,flags);
5329
 
5330
        info->port_array[0]->port_count = 0;
5331
 
5332
        if ( register_test(info->port_array[0]) &&
5333
                register_test(info->port_array[1])) {
5334
 
5335
                info->port_array[0]->port_count = 2;
5336
 
5337
                if ( register_test(info->port_array[2]) &&
5338
                        register_test(info->port_array[3]) )
5339
                        info->port_array[0]->port_count += 2;
5340
        }
5341
        else {
5342
                printk( "%s(%d):Register test failure for device %s Addr=%08lX\n",
5343
                        __FILE__,__LINE__,info->device_name, (unsigned long)(info->phys_sca_base));
5344
                return -ENODEV;
5345
        }
5346
 
5347
        if ( !irq_test(info->port_array[0]) ||
5348
                !irq_test(info->port_array[1]) ||
5349
                 (info->port_count == 4 && !irq_test(info->port_array[2])) ||
5350
                 (info->port_count == 4 && !irq_test(info->port_array[3]))) {
5351
                printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
5352
                        __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
5353
                return -ENODEV;
5354
        }
5355
 
5356
        if (!loopback_test(info->port_array[0]) ||
5357
                !loopback_test(info->port_array[1]) ||
5358
                 (info->port_count == 4 && !loopback_test(info->port_array[2])) ||
5359
                 (info->port_count == 4 && !loopback_test(info->port_array[3]))) {
5360
                printk( "%s(%d):DMA test failure for device %s\n",
5361
                        __FILE__,__LINE__,info->device_name);
5362
                return -ENODEV;
5363
        }
5364
 
5365
        if ( debug_level >= DEBUG_LEVEL_INFO )
5366
                printk( "%s(%d):device %s passed diagnostics\n",
5367
                        __FILE__,__LINE__,info->device_name );
5368
 
5369
        info->port_array[0]->init_error = 0;
5370
        info->port_array[1]->init_error = 0;
5371
        if ( info->port_count > 2 ) {
5372
                info->port_array[2]->init_error = 0;
5373
                info->port_array[3]->init_error = 0;
5374
        }
5375
 
5376
        return 0;
5377
}
5378
 
5379
/* Test the shared memory on a PCI adapter.
5380
 */
5381
int memory_test(SLMP_INFO *info)
5382
{
5383
        static unsigned long testval[] = { 0x0, 0x55555555, 0xaaaaaaaa,
5384
                0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
5385
        unsigned long count = sizeof(testval)/sizeof(unsigned long);
5386
        unsigned long i;
5387
        unsigned long limit = SCA_MEM_SIZE/sizeof(unsigned long);
5388
        unsigned long * addr = (unsigned long *)info->memory_base;
5389
 
5390
        /* Test data lines with test pattern at one location. */
5391
 
5392
        for ( i = 0 ; i < count ; i++ ) {
5393
                *addr = testval[i];
5394
                if ( *addr != testval[i] )
5395
                        return FALSE;
5396
        }
5397
 
5398
        /* Test address lines with incrementing pattern over */
5399
        /* entire address range. */
5400
 
5401
        for ( i = 0 ; i < limit ; i++ ) {
5402
                *addr = i * 4;
5403
                addr++;
5404
        }
5405
 
5406
        addr = (unsigned long *)info->memory_base;
5407
 
5408
        for ( i = 0 ; i < limit ; i++ ) {
5409
                if ( *addr != i * 4 )
5410
                        return FALSE;
5411
                addr++;
5412
        }
5413
 
5414
        memset( info->memory_base, 0, SCA_MEM_SIZE );
5415
        return TRUE;
5416
}
5417
 
5418
/* Load data into PCI adapter shared memory.
5419
 *
5420
 * The PCI9050 releases control of the local bus
5421
 * after completing the current read or write operation.
5422
 *
5423
 * While the PCI9050 write FIFO not empty, the
5424
 * PCI9050 treats all of the writes as a single transaction
5425
 * and does not release the bus. This causes DMA latency problems
5426
 * at high speeds when copying large data blocks to the shared memory.
5427
 *
5428
 * This function breaks a write into multiple transations by
5429
 * interleaving a read which flushes the write FIFO and 'completes'
5430
 * the write transation. This allows any pending DMA request to gain control
5431
 * of the local bus in a timely fasion.
5432
 */
5433
void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count)
5434
{
5435
        /* A load interval of 16 allows for 4 32-bit writes at */
5436
        /* 136ns each for a maximum latency of 542ns on the local bus.*/
5437
 
5438
        unsigned short interval = count / sca_pci_load_interval;
5439
        unsigned short i;
5440
 
5441
        for ( i = 0 ; i < interval ; i++ )
5442
        {
5443
                memcpy(dest, src, sca_pci_load_interval);
5444
                read_status_reg(info);
5445
                dest += sca_pci_load_interval;
5446
                src += sca_pci_load_interval;
5447
        }
5448
 
5449
        memcpy(dest, src, count % sca_pci_load_interval);
5450
}
5451
 
5452
void trace_block(SLMP_INFO *info,const char* data, int count, int xmit)
5453
{
5454
        int i;
5455
        int linecount;
5456
        if (xmit)
5457
                printk("%s tx data:\n",info->device_name);
5458
        else
5459
                printk("%s rx data:\n",info->device_name);
5460
 
5461
        while(count) {
5462
                if (count > 16)
5463
                        linecount = 16;
5464
                else
5465
                        linecount = count;
5466
 
5467
                for(i=0;i<linecount;i++)
5468
                        printk("%02X ",(unsigned char)data[i]);
5469
                for(;i<17;i++)
5470
                        printk("   ");
5471
                for(i=0;i<linecount;i++) {
5472
                        if (data[i]>=040 && data[i]<=0176)
5473
                                printk("%c",data[i]);
5474
                        else
5475
                                printk(".");
5476
                }
5477
                printk("\n");
5478
 
5479
                data  += linecount;
5480
                count -= linecount;
5481
        }
5482
}       /* end of trace_block() */
5483
 
5484
/* called when HDLC frame times out
5485
 * update stats and do tx completion processing
5486
 */
5487
void tx_timeout(unsigned long context)
5488
{
5489
        SLMP_INFO *info = (SLMP_INFO*)context;
5490
        unsigned long flags;
5491
 
5492
        if ( debug_level >= DEBUG_LEVEL_INFO )
5493
                printk( "%s(%d):%s tx_timeout()\n",
5494
                        __FILE__,__LINE__,info->device_name);
5495
        if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
5496
                info->icount.txtimeout++;
5497
        }
5498
        spin_lock_irqsave(&info->lock,flags);
5499
        info->tx_active = 0;
5500
        info->tx_count = info->tx_put = info->tx_get = 0;
5501
 
5502
        spin_unlock_irqrestore(&info->lock,flags);
5503
 
5504
#ifdef CONFIG_SYNCLINK_SYNCPPP
5505
        if (info->netcount)
5506
                sppp_tx_done(info);
5507
        else
5508
#endif
5509
                bh_transmit(info);
5510
}
5511
 
5512
/* called to periodically check the DSR/RI modem signal input status
5513
 */
5514
void status_timeout(unsigned long context)
5515
{
5516
        u16 status = 0;
5517
        SLMP_INFO *info = (SLMP_INFO*)context;
5518
        unsigned long flags;
5519
        unsigned char delta;
5520
 
5521
 
5522
        spin_lock_irqsave(&info->lock,flags);
5523
        get_signals(info);
5524
        spin_unlock_irqrestore(&info->lock,flags);
5525
 
5526
        /* check for DSR/RI state change */
5527
 
5528
        delta = info->old_signals ^ info->serial_signals;
5529
        info->old_signals = info->serial_signals;
5530
 
5531
        if (delta & SerialSignal_DSR)
5532
                status |= MISCSTATUS_DSR_LATCHED|(info->serial_signals&SerialSignal_DSR);
5533
 
5534
        if (delta & SerialSignal_RI)
5535
                status |= MISCSTATUS_RI_LATCHED|(info->serial_signals&SerialSignal_RI);
5536
 
5537
        if (delta & SerialSignal_DCD)
5538
                status |= MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD);
5539
 
5540
        if (delta & SerialSignal_CTS)
5541
                status |= MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS);
5542
 
5543
        if (status)
5544
                isr_io_pin(info,status);
5545
 
5546
        info->status_timer.data = (unsigned long)info;
5547
        info->status_timer.function = status_timeout;
5548
        info->status_timer.expires = jiffies + jiffies_from_ms(10);
5549
        add_timer(&info->status_timer);
5550
}
5551
 
5552
 
5553
/* Register Access Routines -
5554
 * All registers are memory mapped
5555
 */
5556
#define CALC_REGADDR() \
5557
        unsigned char * RegAddr = (unsigned char*)(info->sca_base + Addr); \
5558
        if (info->port_num > 1) \
5559
                RegAddr += 256;                 /* port 0-1 SCA0, 2-3 SCA1 */ \
5560
        if ( info->port_num & 1) { \
5561
                if (Addr > 0x7f) \
5562
                        RegAddr += 0x40;        /* DMA access */ \
5563
                else if (Addr > 0x1f && Addr < 0x60) \
5564
                        RegAddr += 0x20;        /* MSCI access */ \
5565
        }
5566
 
5567
 
5568
unsigned char read_reg(SLMP_INFO * info, unsigned char Addr)
5569
{
5570
        CALC_REGADDR();
5571
        return *RegAddr;
5572
}
5573
void write_reg(SLMP_INFO * info, unsigned char Addr, unsigned char Value)
5574
{
5575
        CALC_REGADDR();
5576
        *RegAddr = Value;
5577
}
5578
 
5579
u16 read_reg16(SLMP_INFO * info, unsigned char Addr)
5580
{
5581
        CALC_REGADDR();
5582
        return *((u16 *)RegAddr);
5583
}
5584
 
5585
void write_reg16(SLMP_INFO * info, unsigned char Addr, u16 Value)
5586
{
5587
        CALC_REGADDR();
5588
        *((u16 *)RegAddr) = Value;
5589
}
5590
 
5591
unsigned char read_status_reg(SLMP_INFO * info)
5592
{
5593
        unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
5594
        return *RegAddr;
5595
}
5596
 
5597
void write_control_reg(SLMP_INFO * info)
5598
{
5599
        unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
5600
        *RegAddr = info->port_array[0]->ctrlreg_value;
5601
}
5602
 
5603
 
5604
static int __devinit synclinkmp_init_one (struct pci_dev *dev,
5605
                                          const struct pci_device_id *ent)
5606
{
5607
        if (pci_enable_device(dev)) {
5608
                printk("error enabling pci device %p\n", dev);
5609
                return -EIO;
5610
        }
5611
        device_init( ++synclinkmp_adapter_count, dev );
5612
        return 0;
5613
}
5614
 
5615
static void __devexit synclinkmp_remove_one (struct pci_dev *dev)
5616
{
5617
}

powered by: WebSVN 2.1.0

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