ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / pcmcia / synclink_cs.c
1 /*
2  * linux/drivers/char/pcmcia/synclink_cs.c
3  *
4  * $Id: synclink_cs.c,v 4.21 2004/03/08 15:29:23 paulkf Exp $
5  *
6  * Device driver for Microgate SyncLink PC Card
7  * multiprotocol serial adapter.
8  *
9  * written by Paul Fulghum for Microgate Corporation
10  * paulkf@microgate.com
11  *
12  * Microgate and SyncLink are trademarks of Microgate Corporation
13  *
14  * This code is released under the GNU General Public License (GPL)
15  *
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26  * OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
30 #if defined(__i386__)
31 #  define BREAKPOINT() asm("   int $3");
32 #else
33 #  define BREAKPOINT() { }
34 #endif
35
36 #define MAX_DEVICE_COUNT 4
37
38 #include <linux/config.h>       
39 #include <linux/module.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/workqueue.h>
71
72 #include <pcmcia/version.h>
73 #include <pcmcia/cs_types.h>
74 #include <pcmcia/cs.h>
75 #include <pcmcia/cistpl.h>
76 #include <pcmcia/cisreg.h>
77 #include <pcmcia/ds.h>
78
79 #ifdef CONFIG_SYNCLINK_SYNCPPP_MODULE
80 #define CONFIG_SYNCLINK_SYNCPPP 1
81 #endif
82
83 #ifdef CONFIG_SYNCLINK_SYNCPPP
84 #include <net/syncppp.h>
85 #endif
86
87 #define GET_USER(error,value,addr) error = get_user(value,addr)
88 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
89 #define PUT_USER(error,value,addr) error = put_user(value,addr)
90 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
91
92 #include <asm/uaccess.h>
93
94 #include "linux/synclink.h"
95
96 static MGSL_PARAMS default_params = {
97         MGSL_MODE_HDLC,                 /* unsigned long mode */
98         0,                              /* unsigned char loopback; */
99         HDLC_FLAG_UNDERRUN_ABORT15,     /* unsigned short flags; */
100         HDLC_ENCODING_NRZI_SPACE,       /* unsigned char encoding; */
101         0,                              /* unsigned long clock_speed; */
102         0xff,                           /* unsigned char addr_filter; */
103         HDLC_CRC_16_CCITT,              /* unsigned short crc_type; */
104         HDLC_PREAMBLE_LENGTH_8BITS,     /* unsigned char preamble_length; */
105         HDLC_PREAMBLE_PATTERN_NONE,     /* unsigned char preamble; */
106         9600,                           /* unsigned long data_rate; */
107         8,                              /* unsigned char data_bits; */
108         1,                              /* unsigned char stop_bits; */
109         ASYNC_PARITY_NONE               /* unsigned char parity; */
110 };
111
112 typedef struct
113 {
114         int count;
115         unsigned char status;
116         char data[1];
117 } RXBUF;
118
119 /* The queue of BH actions to be performed */
120
121 #define BH_RECEIVE  1
122 #define BH_TRANSMIT 2
123 #define BH_STATUS   4
124
125 #define IO_PIN_SHUTDOWN_LIMIT 100
126
127 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
128
129 struct _input_signal_events {
130         int     ri_up;  
131         int     ri_down;
132         int     dsr_up;
133         int     dsr_down;
134         int     dcd_up;
135         int     dcd_down;
136         int     cts_up;
137         int     cts_down;
138 };
139
140
141 /*
142  * Device instance data structure
143  */
144  
145 typedef struct _mgslpc_info {
146         void *if_ptr;   /* General purpose pointer (used by SPPP) */
147         int                     magic;
148         int                     flags;
149         int                     count;          /* count of opens */
150         int                     line;
151         unsigned short          close_delay;
152         unsigned short          closing_wait;   /* time to wait before closing */
153         
154         struct mgsl_icount      icount;
155         
156         struct tty_struct       *tty;
157         int                     timeout;
158         int                     x_char;         /* xon/xoff character */
159         int                     blocked_open;   /* # of blocked opens */
160         unsigned char           read_status_mask;
161         unsigned char           ignore_status_mask;     
162
163         unsigned char *tx_buf;
164         int            tx_put;
165         int            tx_get;
166         int            tx_count;
167
168         /* circular list of fixed length rx buffers */
169
170         unsigned char  *rx_buf;        /* memory allocated for all rx buffers */
171         int            rx_buf_total_size; /* size of memory allocated for rx buffers */
172         int            rx_put;         /* index of next empty rx buffer */
173         int            rx_get;         /* index of next full rx buffer */
174         int            rx_buf_size;    /* size in bytes of single rx buffer */
175         int            rx_buf_count;   /* total number of rx buffers */
176         int            rx_frame_count; /* number of full rx buffers */
177         
178         wait_queue_head_t       open_wait;
179         wait_queue_head_t       close_wait;
180         
181         wait_queue_head_t       status_event_wait_q;
182         wait_queue_head_t       event_wait_q;
183         struct timer_list       tx_timer;       /* HDLC transmit timeout timer */
184         struct _mgslpc_info     *next_device;   /* device list link */
185
186         unsigned short imra_value;
187         unsigned short imrb_value;
188         unsigned char  pim_value;
189
190         spinlock_t lock;
191         struct work_struct task;                /* task structure for scheduling bh */
192
193         u32 max_frame_size;
194
195         u32 pending_bh;
196
197         int bh_running;
198         int bh_requested;
199         
200         int dcd_chkcount; /* check counts to prevent */
201         int cts_chkcount; /* too many IRQs if a signal */
202         int dsr_chkcount; /* is floating */
203         int ri_chkcount;
204
205         int rx_enabled;
206         int rx_overflow;
207
208         int tx_enabled;
209         int tx_active;
210         int tx_aborting;
211         u32 idle_mode;
212
213         int if_mode; /* serial interface selection (RS-232, v.35 etc) */
214
215         char device_name[25];           /* device instance name */
216
217         unsigned int io_base;   /* base I/O address of adapter */
218         unsigned int irq_level;
219         
220         MGSL_PARAMS params;             /* communications parameters */
221
222         unsigned char serial_signals;   /* current serial signal states */
223
224         char irq_occurred;              /* for diagnostics use */
225         char testing_irq;
226         unsigned int init_error;        /* startup error (DIAGS)        */
227
228         char flag_buf[MAX_ASYNC_BUFFER_SIZE];
229         BOOLEAN drop_rts_on_tx_done;
230
231         struct  _input_signal_events    input_signal_events;
232
233         /* PCMCIA support */
234         dev_link_t            link;
235         dev_node_t            node;
236         int                   stop;
237
238         /* SPPP/Cisco HDLC device parts */
239         int netcount;
240         int dosyncppp;
241         spinlock_t netlock;
242 #ifdef CONFIG_SYNCLINK_SYNCPPP
243         struct ppp_device pppdev;
244         char netname[10];
245         struct net_device *netdev;
246         struct net_device_stats netstats;
247 #endif
248 } MGSLPC_INFO;
249
250 #define MGSLPC_MAGIC 0x5402
251
252 /*
253  * The size of the serial xmit buffer is 1 page, or 4096 bytes
254  */
255 #define TXBUFSIZE 4096
256
257     
258 #define CHA     0x00   /* channel A offset */
259 #define CHB     0x40   /* channel B offset */
260     
261 #define RXFIFO  0
262 #define TXFIFO  0
263 #define STAR    0x20
264 #define CMDR    0x20
265 #define RSTA    0x21
266 #define PRE     0x21
267 #define MODE    0x22
268 #define TIMR    0x23
269 #define XAD1    0x24
270 #define XAD2    0x25
271 #define RAH1    0x26
272 #define RAH2    0x27
273 #define DAFO    0x27
274 #define RAL1    0x28
275 #define RFC     0x28
276 #define RHCR    0x29
277 #define RAL2    0x29
278 #define RBCL    0x2a
279 #define XBCL    0x2a
280 #define RBCH    0x2b
281 #define XBCH    0x2b
282 #define CCR0    0x2c
283 #define CCR1    0x2d
284 #define CCR2    0x2e
285 #define CCR3    0x2f
286 #define VSTR    0x34
287 #define BGR     0x34
288 #define RLCR    0x35
289 #define AML     0x36
290 #define AMH     0x37
291 #define GIS     0x38
292 #define IVA     0x38
293 #define IPC     0x39
294 #define ISR     0x3a
295 #define IMR     0x3a
296 #define PVR     0x3c
297 #define PIS     0x3d
298 #define PIM     0x3d
299 #define PCR     0x3e
300 #define CCR4    0x3f
301     
302 // IMR/ISR
303     
304 #define IRQ_BREAK_ON    BIT15   // rx break detected
305 #define IRQ_DATAOVERRUN BIT14   // receive data overflow
306 #define IRQ_ALLSENT     BIT13   // all sent
307 #define IRQ_UNDERRUN    BIT12   // transmit data underrun
308 #define IRQ_TIMER       BIT11   // timer interrupt
309 #define IRQ_CTS         BIT10   // CTS status change
310 #define IRQ_TXREPEAT    BIT9    // tx message repeat
311 #define IRQ_TXFIFO      BIT8    // transmit pool ready
312 #define IRQ_RXEOM       BIT7    // receive message end
313 #define IRQ_EXITHUNT    BIT6    // receive frame start
314 #define IRQ_RXTIME      BIT6    // rx char timeout
315 #define IRQ_DCD         BIT2    // carrier detect status change
316 #define IRQ_OVERRUN     BIT1    // receive frame overflow
317 #define IRQ_RXFIFO      BIT0    // receive pool full
318     
319 // STAR
320     
321 #define XFW   BIT6              // transmit FIFO write enable
322 #define CEC   BIT2              // command executing
323 #define CTS   BIT1              // CTS state
324     
325 #define PVR_DTR      BIT0
326 #define PVR_DSR      BIT1
327 #define PVR_RI       BIT2
328 #define PVR_AUTOCTS  BIT3
329 #define PVR_RS232    0x20   /* 0010b */
330 #define PVR_V35      0xe0   /* 1110b */
331 #define PVR_RS422    0x40   /* 0100b */
332     
333 /* Register access functions */ 
334     
335 #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
336 #define read_reg(info, reg) inb((info)->io_base + (reg))
337
338 #define read_reg16(info, reg) inw((info)->io_base + (reg))  
339 #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
340     
341 #define set_reg_bits(info, reg, mask) \
342     write_reg(info, (reg), \
343                  (unsigned char) (read_reg(info, (reg)) | (mask)))  
344 #define clear_reg_bits(info, reg, mask) \
345     write_reg(info, (reg), \
346                  (unsigned char) (read_reg(info, (reg)) & ~(mask)))  
347 /*
348  * interrupt enable/disable routines
349  */ 
350 static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask) 
351 {
352         if (channel == CHA) {
353                 info->imra_value |= mask;
354                 write_reg16(info, CHA + IMR, info->imra_value);
355         } else {
356                 info->imrb_value |= mask;
357                 write_reg16(info, CHB + IMR, info->imrb_value);
358         }
359 }
360 static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask) 
361 {
362         if (channel == CHA) {
363                 info->imra_value &= ~mask;
364                 write_reg16(info, CHA + IMR, info->imra_value);
365         } else {
366                 info->imrb_value &= ~mask;
367                 write_reg16(info, CHB + IMR, info->imrb_value);
368         }
369 }
370
371 #define port_irq_disable(info, mask) \
372   { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
373
374 #define port_irq_enable(info, mask) \
375   { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
376
377 static void rx_start(MGSLPC_INFO *info);
378 static void rx_stop(MGSLPC_INFO *info);
379
380 static void tx_start(MGSLPC_INFO *info);
381 static void tx_stop(MGSLPC_INFO *info);
382 static void tx_set_idle(MGSLPC_INFO *info);
383
384 static void get_signals(MGSLPC_INFO *info);
385 static void set_signals(MGSLPC_INFO *info);
386
387 static void reset_device(MGSLPC_INFO *info);
388
389 static void hdlc_mode(MGSLPC_INFO *info);
390 static void async_mode(MGSLPC_INFO *info);
391
392 static void tx_timeout(unsigned long context);
393
394 static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg);
395
396 #ifdef CONFIG_SYNCLINK_SYNCPPP
397 /* SPPP/HDLC stuff */
398 static void mgslpc_sppp_init(MGSLPC_INFO *info);
399 static void mgslpc_sppp_delete(MGSLPC_INFO *info);
400 static int  mgslpc_sppp_open(struct net_device *d);
401 static int  mgslpc_sppp_close(struct net_device *d);
402 static void mgslpc_sppp_tx_timeout(struct net_device *d);
403 static int  mgslpc_sppp_tx(struct sk_buff *skb, struct net_device *d);
404 static void mgslpc_sppp_rx_done(MGSLPC_INFO *info, char *buf, int size);
405 static void mgslpc_sppp_tx_done(MGSLPC_INFO *info);
406 static int  mgslpc_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
407 struct net_device_stats *mgslpc_net_stats(struct net_device *dev);
408 #endif
409
410 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
411
412 static BOOLEAN register_test(MGSLPC_INFO *info);
413 static BOOLEAN irq_test(MGSLPC_INFO *info);
414 static int adapter_test(MGSLPC_INFO *info);
415
416 static int claim_resources(MGSLPC_INFO *info);
417 static void release_resources(MGSLPC_INFO *info);
418 static void mgslpc_add_device(MGSLPC_INFO *info);
419 static void mgslpc_remove_device(MGSLPC_INFO *info);
420
421 static int  rx_get_frame(MGSLPC_INFO *info);
422 static void rx_reset_buffers(MGSLPC_INFO *info);
423 static int  rx_alloc_buffers(MGSLPC_INFO *info);
424 static void rx_free_buffers(MGSLPC_INFO *info);
425
426 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs);
427
428 /*
429  * Bottom half interrupt handlers
430  */
431 static void bh_handler(void* Context);
432 static void bh_transmit(MGSLPC_INFO *info);
433 static void bh_status(MGSLPC_INFO *info);
434
435 /*
436  * ioctl handlers
437  */
438 static int tiocmget(struct tty_struct *tty, struct file *file);
439 static int tiocmset(struct tty_struct *tty, struct file *file,
440                     unsigned int set, unsigned int clear);
441 static int get_stats(MGSLPC_INFO *info, struct mgsl_icount *user_icount);
442 static int get_params(MGSLPC_INFO *info, MGSL_PARAMS *user_params);
443 static int set_params(MGSLPC_INFO *info, MGSL_PARAMS *new_params);
444 static int get_txidle(MGSLPC_INFO *info, int*idle_mode);
445 static int set_txidle(MGSLPC_INFO *info, int idle_mode);
446 static int set_txenable(MGSLPC_INFO *info, int enable);
447 static int tx_abort(MGSLPC_INFO *info);
448 static int set_rxenable(MGSLPC_INFO *info, int enable);
449 static int wait_events(MGSLPC_INFO *info, int *mask);
450
451 #define jiffies_from_ms(a) ((((a) * HZ)/1000)+1)
452
453 static MGSLPC_INFO *mgslpc_device_list = NULL;
454 static int mgslpc_device_count = 0;
455
456 /*
457  * Set this param to non-zero to load eax with the
458  * .text section address and breakpoint on module load.
459  * This is useful for use with gdb and add-symbol-file command.
460  */
461 static int break_on_load=0;
462
463 /*
464  * Driver major number, defaults to zero to get auto
465  * assigned major number. May be forced as module parameter.
466  */
467 static int ttymajor=0;
468
469 static int debug_level = 0;
470 static int maxframe[MAX_DEVICE_COUNT] = {0,};
471 static int dosyncppp[MAX_DEVICE_COUNT] = {1,1,1,1};
472
473 /* The old way: bit map of interrupts to choose from */
474 /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, and 3 */
475 static u_int irq_mask = 0xdeb8;
476
477 /* Newer, simpler way of listing specific interrupts */
478 static int irq_list[4] = { -1 };
479
480 MODULE_PARM(irq_mask, "i");
481 MODULE_PARM(irq_list, "1-4i");
482
483 MODULE_PARM(break_on_load,"i");
484 MODULE_PARM(ttymajor,"i");
485 MODULE_PARM(debug_level,"i");
486 MODULE_PARM(maxframe,"1-" __MODULE_STRING(MAX_DEVICE_COUNT) "i");
487 MODULE_PARM(dosyncppp,"1-" __MODULE_STRING(MAX_DEVICE_COUNT) "i");
488
489 MODULE_LICENSE("GPL");
490
491 static char *driver_name = "SyncLink PC Card driver";
492 static char *driver_version = "$Revision: 4.21 $";
493
494 static struct tty_driver *serial_driver;
495
496 /* number of characters left in xmit buffer before we ask for more */
497 #define WAKEUP_CHARS 256
498
499 static void mgslpc_change_params(MGSLPC_INFO *info);
500 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
501
502 #ifndef MIN
503 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
504 #endif
505
506 /* PCMCIA prototypes */
507
508 static void mgslpc_config(dev_link_t *link);
509 static void mgslpc_release(u_long arg);
510 static int  mgslpc_event(event_t event, int priority,
511                          event_callback_args_t *args);
512 static dev_link_t *mgslpc_attach(void);
513 static void mgslpc_detach(dev_link_t *);
514
515 static dev_info_t dev_info = "synclink_cs";
516 static dev_link_t *dev_list = NULL;
517
518 /*
519  * 1st function defined in .text section. Calling this function in
520  * init_module() followed by a breakpoint allows a remote debugger
521  * (gdb) to get the .text address for the add-symbol-file command.
522  * This allows remote debugging of dynamically loadable modules.
523  */
524 static void* mgslpc_get_text_ptr(void);
525 static void* mgslpc_get_text_ptr() {return mgslpc_get_text_ptr;}
526
527 static dev_link_t *mgslpc_attach(void)
528 {
529     MGSLPC_INFO *info;
530     dev_link_t *link;
531     client_reg_t client_reg;
532     int ret, i;
533     
534     if (debug_level >= DEBUG_LEVEL_INFO)
535             printk("mgslpc_attach\n");
536         
537     info = (MGSLPC_INFO *)kmalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
538     if (!info) {
539             printk("Error can't allocate device instance data\n");
540             return NULL;
541     }
542
543     memset(info, 0, sizeof(MGSLPC_INFO));
544     info->magic = MGSLPC_MAGIC;
545     INIT_WORK(&info->task, bh_handler, info);
546     info->max_frame_size = 4096;
547     info->close_delay = 5*HZ/10;
548     info->closing_wait = 30*HZ;
549     init_waitqueue_head(&info->open_wait);
550     init_waitqueue_head(&info->close_wait);
551     init_waitqueue_head(&info->status_event_wait_q);
552     init_waitqueue_head(&info->event_wait_q);
553     spin_lock_init(&info->lock);
554     spin_lock_init(&info->netlock);
555     memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
556     info->idle_mode = HDLC_TXIDLE_FLAGS;                
557     info->imra_value = 0xffff;
558     info->imrb_value = 0xffff;
559     info->pim_value = 0xff;
560
561     link = &info->link;
562     link->priv = info;
563     
564     /* Initialize the dev_link_t structure */
565
566     /* Interrupt setup */
567     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
568     link->irq.IRQInfo1   = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
569     if (irq_list[0] == -1)
570             link->irq.IRQInfo2 = irq_mask;
571     else
572             for (i = 0; i < 4; i++)
573                     link->irq.IRQInfo2 |= 1 << irq_list[i];
574     link->irq.Handler = NULL;
575     
576     link->conf.Attributes = 0;
577     link->conf.Vcc = 50;
578     link->conf.IntType = INT_MEMORY_AND_IO;
579
580     /* Register with Card Services */
581     link->next = dev_list;
582     dev_list = link;
583
584     client_reg.dev_info = &dev_info;
585     client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
586     client_reg.EventMask =
587             CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
588             CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
589             CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
590     client_reg.event_handler = &mgslpc_event;
591     client_reg.Version = 0x0210;
592     client_reg.event_callback_args.client_data = link;
593
594     ret = pcmcia_register_client(&link->handle, &client_reg);
595     if (ret != CS_SUCCESS) {
596             cs_error(link->handle, RegisterClient, ret);
597             mgslpc_detach(link);
598             return NULL;
599     }
600
601     mgslpc_add_device(info);
602
603     return link;
604 }
605
606 /* Card has been inserted.
607  */
608
609 #define CS_CHECK(fn, ret) \
610 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
611
612 static void mgslpc_config(dev_link_t *link)
613 {
614     client_handle_t handle = link->handle;
615     MGSLPC_INFO *info = link->priv;
616     tuple_t tuple;
617     cisparse_t parse;
618     int last_fn, last_ret;
619     u_char buf[64];
620     config_info_t conf;
621     cistpl_cftable_entry_t dflt = { 0 };
622     cistpl_cftable_entry_t *cfg;
623     
624     if (debug_level >= DEBUG_LEVEL_INFO)
625             printk("mgslpc_config(0x%p)\n", link);
626
627     /* read CONFIG tuple to find its configuration registers */
628     tuple.DesiredTuple = CISTPL_CONFIG;
629     tuple.Attributes = 0;
630     tuple.TupleData = buf;
631     tuple.TupleDataMax = sizeof(buf);
632     tuple.TupleOffset = 0;
633     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
634     CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
635     CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
636     link->conf.ConfigBase = parse.config.base;
637     link->conf.Present = parse.config.rmask[0];
638     
639     /* Configure card */
640     link->state |= DEV_CONFIG;
641
642     /* Look up the current Vcc */
643     CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
644     link->conf.Vcc = conf.Vcc;
645
646     /* get CIS configuration entry */
647
648     tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
649     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
650
651     cfg = &(parse.cftable_entry);
652     CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
653     CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
654
655     if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
656     if (cfg->index == 0)
657             goto cs_failed;
658
659     link->conf.ConfigIndex = cfg->index;
660     link->conf.Attributes |= CONF_ENABLE_IRQ;
661         
662     /* IO window settings */
663     link->io.NumPorts1 = 0;
664     if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
665             cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
666             link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
667             if (!(io->flags & CISTPL_IO_8BIT))
668                     link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
669             if (!(io->flags & CISTPL_IO_16BIT))
670                     link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
671             link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
672             link->io.BasePort1 = io->win[0].base;
673             link->io.NumPorts1 = io->win[0].len;
674             CS_CHECK(RequestIO, pcmcia_request_io(link->handle, &link->io));
675     }
676
677     link->conf.Attributes = CONF_ENABLE_IRQ;
678     link->conf.Vcc = 50;
679     link->conf.IntType = INT_MEMORY_AND_IO;
680     link->conf.ConfigIndex = 8;
681     link->conf.Present = PRESENT_OPTION;
682     
683     link->irq.Attributes |= IRQ_HANDLE_PRESENT;
684     link->irq.Handler     = mgslpc_isr;
685     link->irq.Instance    = info;
686     CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
687
688     CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
689
690     info->io_base = link->io.BasePort1;
691     info->irq_level = link->irq.AssignedIRQ;
692
693     /* add to linked list of devices */
694     sprintf(info->node.dev_name, "mgslpc0");
695     info->node.major = info->node.minor = 0;
696     link->dev = &info->node;
697
698     printk(KERN_INFO "%s: index 0x%02x:",
699            info->node.dev_name, link->conf.ConfigIndex);
700     if (link->conf.Attributes & CONF_ENABLE_IRQ)
701             printk(", irq %d", link->irq.AssignedIRQ);
702     if (link->io.NumPorts1)
703             printk(", io 0x%04x-0x%04x", link->io.BasePort1,
704                    link->io.BasePort1+link->io.NumPorts1-1);
705     printk("\n");
706     
707     link->state &= ~DEV_CONFIG_PENDING;
708     return;
709
710 cs_failed:
711     cs_error(link->handle, last_fn, last_ret);
712     mgslpc_release((u_long)link);
713 }
714
715 /* Card has been removed.
716  * Unregister device and release PCMCIA configuration.
717  * If device is open, postpone until it is closed.
718  */
719 static void mgslpc_release(u_long arg)
720 {
721     dev_link_t *link = (dev_link_t *)arg;
722
723     if (debug_level >= DEBUG_LEVEL_INFO)
724             printk("mgslpc_release(0x%p)\n", link);
725
726     /* Unlink the device chain */
727     link->dev = NULL;
728     link->state &= ~DEV_CONFIG;
729
730     pcmcia_release_configuration(link->handle);
731     if (link->io.NumPorts1)
732             pcmcia_release_io(link->handle, &link->io);
733     if (link->irq.AssignedIRQ)
734             pcmcia_release_irq(link->handle, &link->irq);
735     if (link->state & DEV_STALE_LINK)
736             mgslpc_detach(link);
737 }
738
739 static void mgslpc_detach(dev_link_t *link)
740 {
741     dev_link_t **linkp;
742
743     if (debug_level >= DEBUG_LEVEL_INFO)
744             printk("mgslpc_detach(0x%p)\n", link);
745     
746     /* find device */
747     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
748             if (*linkp == link) break;
749     if (*linkp == NULL)
750             return;
751
752     if (link->state & DEV_CONFIG) {
753             /* device is configured/active, mark it so when
754              * release() is called a proper detach() occurs.
755              */
756             if (debug_level >= DEBUG_LEVEL_INFO)
757                     printk(KERN_DEBUG "synclinkpc: detach postponed, '%s' "
758                            "still locked\n", link->dev->dev_name);
759             link->state |= DEV_STALE_LINK;
760             return;
761     }
762
763     /* Break the link with Card Services */
764     if (link->handle)
765             pcmcia_deregister_client(link->handle);
766     
767     /* Unlink device structure, and free it */
768     *linkp = link->next;
769     mgslpc_remove_device((MGSLPC_INFO *)link->priv);
770 }
771
772 static int mgslpc_event(event_t event, int priority,
773                         event_callback_args_t *args)
774 {
775     dev_link_t *link = args->client_data;
776     MGSLPC_INFO *info = link->priv;
777     
778     if (debug_level >= DEBUG_LEVEL_INFO)
779             printk("mgslpc_event(0x%06x)\n", event);
780     
781     switch (event) {
782     case CS_EVENT_CARD_REMOVAL:
783             link->state &= ~DEV_PRESENT;
784             if (link->state & DEV_CONFIG) {
785                     ((MGSLPC_INFO *)link->priv)->stop = 1;
786                     mgslpc_release((u_long)link);
787             }
788             break;
789     case CS_EVENT_CARD_INSERTION:
790             link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
791             mgslpc_config(link);
792             break;
793     case CS_EVENT_PM_SUSPEND:
794             link->state |= DEV_SUSPEND;
795             /* Fall through... */
796     case CS_EVENT_RESET_PHYSICAL:
797             /* Mark the device as stopped, to block IO until later */
798             info->stop = 1;
799             if (link->state & DEV_CONFIG)
800                     pcmcia_release_configuration(link->handle);
801             break;
802     case CS_EVENT_PM_RESUME:
803             link->state &= ~DEV_SUSPEND;
804             /* Fall through... */
805     case CS_EVENT_CARD_RESET:
806             if (link->state & DEV_CONFIG)
807                     pcmcia_request_configuration(link->handle, &link->conf);
808             info->stop = 0;
809             break;
810     }
811     return 0;
812 }
813
814 static inline int mgslpc_paranoia_check(MGSLPC_INFO *info,
815                                         char *name, const char *routine)
816 {
817 #ifdef MGSLPC_PARANOIA_CHECK
818         static const char *badmagic =
819                 "Warning: bad magic number for mgsl struct (%s) in %s\n";
820         static const char *badinfo =
821                 "Warning: null mgslpc_info for (%s) in %s\n";
822
823         if (!info) {
824                 printk(badinfo, name, routine);
825                 return 1;
826         }
827         if (info->magic != MGSLPC_MAGIC) {
828                 printk(badmagic, name, routine);
829                 return 1;
830         }
831 #else
832         if (!info)
833                 return 1;
834 #endif
835         return 0;
836 }
837
838
839 #define CMD_RXFIFO      BIT7    // release current rx FIFO
840 #define CMD_RXRESET     BIT6    // receiver reset
841 #define CMD_RXFIFO_READ BIT5
842 #define CMD_START_TIMER BIT4
843 #define CMD_TXFIFO      BIT3    // release current tx FIFO
844 #define CMD_TXEOM       BIT1    // transmit end message
845 #define CMD_TXRESET     BIT0    // transmit reset
846
847 static BOOLEAN wait_command_complete(MGSLPC_INFO *info, unsigned char channel) 
848 {
849         int i = 0;
850         unsigned char status;
851         /* wait for command completion */ 
852         while ((status = read_reg(info, (unsigned char)(channel+STAR)) & BIT2)) {
853                 udelay(1);
854                 if (i++ == 1000)
855                         return FALSE;
856         }
857         return TRUE;
858 }
859
860 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd) 
861 {
862         wait_command_complete(info, channel);
863         write_reg(info, (unsigned char) (channel + CMDR), cmd);
864 }
865
866 static void tx_pause(struct tty_struct *tty)
867 {
868         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
869         unsigned long flags;
870         
871         if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
872                 return;
873         if (debug_level >= DEBUG_LEVEL_INFO)
874                 printk("tx_pause(%s)\n",info->device_name);     
875                 
876         spin_lock_irqsave(&info->lock,flags);
877         if (info->tx_enabled)
878                 tx_stop(info);
879         spin_unlock_irqrestore(&info->lock,flags);
880 }
881
882 static void tx_release(struct tty_struct *tty)
883 {
884         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
885         unsigned long flags;
886         
887         if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
888                 return;
889         if (debug_level >= DEBUG_LEVEL_INFO)
890                 printk("tx_release(%s)\n",info->device_name);   
891                 
892         spin_lock_irqsave(&info->lock,flags);
893         if (!info->tx_enabled)
894                 tx_start(info);
895         spin_unlock_irqrestore(&info->lock,flags);
896 }
897
898 /* Return next bottom half action to perform.
899  * or 0 if nothing to do.
900  */
901 int bh_action(MGSLPC_INFO *info)
902 {
903         unsigned long flags;
904         int rc = 0;
905         
906         spin_lock_irqsave(&info->lock,flags);
907
908         if (info->pending_bh & BH_RECEIVE) {
909                 info->pending_bh &= ~BH_RECEIVE;
910                 rc = BH_RECEIVE;
911         } else if (info->pending_bh & BH_TRANSMIT) {
912                 info->pending_bh &= ~BH_TRANSMIT;
913                 rc = BH_TRANSMIT;
914         } else if (info->pending_bh & BH_STATUS) {
915                 info->pending_bh &= ~BH_STATUS;
916                 rc = BH_STATUS;
917         }
918
919         if (!rc) {
920                 /* Mark BH routine as complete */
921                 info->bh_running   = 0;
922                 info->bh_requested = 0;
923         }
924         
925         spin_unlock_irqrestore(&info->lock,flags);
926         
927         return rc;
928 }
929
930 void bh_handler(void* Context)
931 {
932         MGSLPC_INFO *info = (MGSLPC_INFO*)Context;
933         int action;
934
935         if (!info)
936                 return;
937                 
938         if (debug_level >= DEBUG_LEVEL_BH)
939                 printk( "%s(%d):bh_handler(%s) entry\n",
940                         __FILE__,__LINE__,info->device_name);
941         
942         info->bh_running = 1;
943
944         while((action = bh_action(info)) != 0) {
945         
946                 /* Process work item */
947                 if ( debug_level >= DEBUG_LEVEL_BH )
948                         printk( "%s(%d):bh_handler() work item action=%d\n",
949                                 __FILE__,__LINE__,action);
950
951                 switch (action) {
952                 
953                 case BH_RECEIVE:
954                         while(rx_get_frame(info));
955                         break;
956                 case BH_TRANSMIT:
957                         bh_transmit(info);
958                         break;
959                 case BH_STATUS:
960                         bh_status(info);
961                         break;
962                 default:
963                         /* unknown work item ID */
964                         printk("Unknown work item ID=%08X!\n", action);
965                         break;
966                 }
967         }
968
969         if (debug_level >= DEBUG_LEVEL_BH)
970                 printk( "%s(%d):bh_handler(%s) exit\n",
971                         __FILE__,__LINE__,info->device_name);
972 }
973
974 void bh_transmit(MGSLPC_INFO *info)
975 {
976         struct tty_struct *tty = info->tty;
977         if (debug_level >= DEBUG_LEVEL_BH)
978                 printk("bh_transmit() entry on %s\n", info->device_name);
979
980         if (tty) {
981                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
982                     tty->ldisc.write_wakeup) {
983                         if ( debug_level >= DEBUG_LEVEL_BH )
984                                 printk( "%s(%d):calling ldisc.write_wakeup on %s\n",
985                                         __FILE__,__LINE__,info->device_name);
986                         (tty->ldisc.write_wakeup)(tty);
987                 }
988                 wake_up_interruptible(&tty->write_wait);
989         }
990 }
991
992 void bh_status(MGSLPC_INFO *info)
993 {
994         info->ri_chkcount = 0;
995         info->dsr_chkcount = 0;
996         info->dcd_chkcount = 0;
997         info->cts_chkcount = 0;
998 }
999
1000 /* eom: non-zero = end of frame */ 
1001 void rx_ready_hdlc(MGSLPC_INFO *info, int eom) 
1002 {
1003         unsigned char data[2];
1004         unsigned char fifo_count, read_count, i;
1005         RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
1006
1007         if (debug_level >= DEBUG_LEVEL_ISR)
1008                 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
1009         
1010         if (!info->rx_enabled)
1011                 return;
1012
1013         if (info->rx_frame_count >= info->rx_buf_count) {
1014                 /* no more free buffers */
1015                 issue_command(info, CHA, CMD_RXRESET);
1016                 info->pending_bh |= BH_RECEIVE;
1017                 info->rx_overflow = 1;
1018                 info->icount.buf_overrun++;
1019                 return;
1020         }
1021
1022         if (eom) {
1023                 /* end of frame, get FIFO count from RBCL register */ 
1024                 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
1025                         fifo_count = 32;
1026         } else
1027                 fifo_count = 32;
1028         
1029         do {
1030                 if (fifo_count == 1) {
1031                         read_count = 1;
1032                         data[0] = read_reg(info, CHA + RXFIFO);
1033                 } else {
1034                         read_count = 2;
1035                         *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
1036                 }
1037                 fifo_count -= read_count;
1038                 if (!fifo_count && eom)
1039                         buf->status = data[--read_count];
1040
1041                 for (i = 0; i < read_count; i++) {
1042                         if (buf->count >= info->max_frame_size) {
1043                                 /* frame too large, reset receiver and reset current buffer */
1044                                 issue_command(info, CHA, CMD_RXRESET);
1045                                 buf->count = 0;
1046                                 return;
1047                         }
1048                         *(buf->data + buf->count) = data[i];
1049                         buf->count++;
1050                 }
1051         } while (fifo_count);
1052
1053         if (eom) {
1054                 info->pending_bh |= BH_RECEIVE;
1055                 info->rx_frame_count++;
1056                 info->rx_put++;
1057                 if (info->rx_put >= info->rx_buf_count)
1058                         info->rx_put = 0;
1059         }
1060         issue_command(info, CHA, CMD_RXFIFO);
1061 }
1062
1063 void rx_ready_async(MGSLPC_INFO *info, int tcd) 
1064 {
1065         unsigned char data, status;
1066         int fifo_count;
1067         struct tty_struct *tty = info->tty;
1068         struct mgsl_icount *icount = &info->icount;
1069
1070         if (tcd) {
1071                 /* early termination, get FIFO count from RBCL register */ 
1072                 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
1073
1074                 /* Zero fifo count could mean 0 or 32 bytes available.
1075                  * If BIT5 of STAR is set then at least 1 byte is available.
1076                  */
1077                 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
1078                         fifo_count = 32;
1079         } else
1080                 fifo_count = 32;
1081         
1082         /* Flush received async data to receive data buffer. */ 
1083         while (fifo_count) {
1084                 data   = read_reg(info, CHA + RXFIFO);
1085                 status = read_reg(info, CHA + RXFIFO);
1086                 fifo_count -= 2;
1087
1088                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1089                         break;
1090                         
1091                 *tty->flip.char_buf_ptr = data;
1092                 icount->rx++;
1093                 
1094                 *tty->flip.flag_buf_ptr = 0;
1095
1096                 // if no frameing/crc error then save data
1097                 // BIT7:parity error
1098                 // BIT6:framing error
1099
1100                 if (status & (BIT7 + BIT6)) {
1101                         if (status & BIT7) 
1102                                 icount->parity++;
1103                         else
1104                                 icount->frame++;
1105
1106                         /* discard char if tty control flags say so */
1107                         if (status & info->ignore_status_mask)
1108                                 continue;
1109                                 
1110                         status &= info->read_status_mask;
1111
1112                         if (status & BIT7)
1113                                 *tty->flip.flag_buf_ptr = TTY_PARITY;
1114                         else if (status & BIT6)
1115                                 *tty->flip.flag_buf_ptr = TTY_FRAME;
1116                 }
1117                 
1118                 tty->flip.flag_buf_ptr++;
1119                 tty->flip.char_buf_ptr++;
1120                 tty->flip.count++;
1121         }
1122         issue_command(info, CHA, CMD_RXFIFO);
1123
1124         if (debug_level >= DEBUG_LEVEL_ISR) {
1125                 printk("%s(%d):rx_ready_async count=%d\n",
1126                         __FILE__,__LINE__,tty->flip.count);
1127                 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1128                         __FILE__,__LINE__,icount->rx,icount->brk,
1129                         icount->parity,icount->frame,icount->overrun);
1130         }
1131                         
1132         if (tty->flip.count)
1133                 tty_flip_buffer_push(tty);
1134 }
1135
1136
1137 void tx_done(MGSLPC_INFO *info) 
1138 {
1139         if (!info->tx_active)
1140                 return;
1141                         
1142         info->tx_active = 0;
1143         info->tx_aborting = 0;
1144
1145         if (info->params.mode == MGSL_MODE_ASYNC)
1146                 return;
1147
1148         info->tx_count = info->tx_put = info->tx_get = 0;
1149         del_timer(&info->tx_timer);     
1150         
1151         if (info->drop_rts_on_tx_done) {
1152                 get_signals(info);
1153                 if (info->serial_signals & SerialSignal_RTS) {
1154                         info->serial_signals &= ~SerialSignal_RTS;
1155                         set_signals(info);
1156                 }
1157                 info->drop_rts_on_tx_done = 0;
1158         }
1159
1160 #ifdef CONFIG_SYNCLINK_SYNCPPP  
1161         if (info->netcount)
1162                 mgslpc_sppp_tx_done(info);
1163         else 
1164 #endif
1165         {
1166                 if (info->tty->stopped || info->tty->hw_stopped) {
1167                         tx_stop(info);
1168                         return;
1169                 }
1170                 info->pending_bh |= BH_TRANSMIT;
1171         }
1172 }
1173
1174 void tx_ready(MGSLPC_INFO *info) 
1175 {
1176         unsigned char fifo_count = 32;
1177         int c;
1178
1179         if (debug_level >= DEBUG_LEVEL_ISR)
1180                 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1181
1182         if (info->params.mode == MGSL_MODE_HDLC) {
1183                 if (!info->tx_active)
1184                         return;
1185         } else {
1186                 if (info->tty->stopped || info->tty->hw_stopped) {
1187                         tx_stop(info);
1188                         return;
1189                 }
1190                 if (!info->tx_count)
1191                         info->tx_active = 0;
1192         }
1193
1194         if (!info->tx_count)
1195                 return;
1196
1197         while (info->tx_count && fifo_count) {
1198                 c = MIN(2, MIN(fifo_count, MIN(info->tx_count, TXBUFSIZE - info->tx_get)));
1199                 
1200                 if (c == 1) {
1201                         write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1202                 } else {
1203                         write_reg16(info, CHA + TXFIFO,
1204                                           *((unsigned short*)(info->tx_buf + info->tx_get)));
1205                 }
1206                 info->tx_count -= c;
1207                 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1208                 fifo_count -= c;
1209         }
1210
1211         if (info->params.mode == MGSL_MODE_ASYNC) {
1212                 if (info->tx_count < WAKEUP_CHARS)
1213                         info->pending_bh |= BH_TRANSMIT;
1214                 issue_command(info, CHA, CMD_TXFIFO);
1215         } else {
1216                 if (info->tx_count)
1217                         issue_command(info, CHA, CMD_TXFIFO);
1218                 else
1219                         issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1220         }
1221 }
1222
1223 void cts_change(MGSLPC_INFO *info) 
1224 {
1225         get_signals(info);
1226         if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1227                 irq_disable(info, CHB, IRQ_CTS);
1228         info->icount.cts++;
1229         if (info->serial_signals & SerialSignal_CTS)
1230                 info->input_signal_events.cts_up++;
1231         else
1232                 info->input_signal_events.cts_down++;
1233         wake_up_interruptible(&info->status_event_wait_q);
1234         wake_up_interruptible(&info->event_wait_q);
1235
1236         if (info->flags & ASYNC_CTS_FLOW) {
1237                 if (info->tty->hw_stopped) {
1238                         if (info->serial_signals & SerialSignal_CTS) {
1239                                 if (debug_level >= DEBUG_LEVEL_ISR)
1240                                         printk("CTS tx start...");
1241                                 if (info->tty)
1242                                         info->tty->hw_stopped = 0;
1243                                 tx_start(info);
1244                                 info->pending_bh |= BH_TRANSMIT;
1245                                 return;
1246                         }
1247                 } else {
1248                         if (!(info->serial_signals & SerialSignal_CTS)) {
1249                                 if (debug_level >= DEBUG_LEVEL_ISR)
1250                                         printk("CTS tx stop...");
1251                                 if (info->tty)
1252                                         info->tty->hw_stopped = 1;
1253                                 tx_stop(info);
1254                         }
1255                 }
1256         }
1257         info->pending_bh |= BH_STATUS;
1258 }
1259
1260 void dcd_change(MGSLPC_INFO *info) 
1261 {
1262         get_signals(info);
1263         if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1264                 irq_disable(info, CHB, IRQ_DCD);
1265         info->icount.dcd++;
1266         if (info->serial_signals & SerialSignal_DCD) {
1267                 info->input_signal_events.dcd_up++;
1268 #ifdef CONFIG_SYNCLINK_SYNCPPP  
1269                 if (info->netcount)
1270                         sppp_reopen(info->netdev);
1271 #endif
1272         }
1273         else
1274                 info->input_signal_events.dcd_down++;
1275         wake_up_interruptible(&info->status_event_wait_q);
1276         wake_up_interruptible(&info->event_wait_q);
1277
1278         if (info->flags & ASYNC_CHECK_CD) {
1279                 if (debug_level >= DEBUG_LEVEL_ISR)
1280                         printk("%s CD now %s...", info->device_name,
1281                                (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1282                 if (info->serial_signals & SerialSignal_DCD)
1283                         wake_up_interruptible(&info->open_wait);
1284                 else {
1285                         if (debug_level >= DEBUG_LEVEL_ISR)
1286                                 printk("doing serial hangup...");
1287                         if (info->tty)
1288                                 tty_hangup(info->tty);
1289                 }
1290         }
1291         info->pending_bh |= BH_STATUS;
1292 }
1293
1294 void dsr_change(MGSLPC_INFO *info) 
1295 {
1296         get_signals(info);
1297         if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1298                 port_irq_disable(info, PVR_DSR);
1299         info->icount.dsr++;
1300         if (info->serial_signals & SerialSignal_DSR)
1301                 info->input_signal_events.dsr_up++;
1302         else
1303                 info->input_signal_events.dsr_down++;
1304         wake_up_interruptible(&info->status_event_wait_q);
1305         wake_up_interruptible(&info->event_wait_q);
1306         info->pending_bh |= BH_STATUS;
1307 }
1308
1309 void ri_change(MGSLPC_INFO *info) 
1310 {
1311         get_signals(info);
1312         if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1313                 port_irq_disable(info, PVR_RI);
1314         info->icount.rng++;
1315         if (info->serial_signals & SerialSignal_RI)
1316                 info->input_signal_events.ri_up++;
1317         else
1318                 info->input_signal_events.ri_down++;
1319         wake_up_interruptible(&info->status_event_wait_q);
1320         wake_up_interruptible(&info->event_wait_q);
1321         info->pending_bh |= BH_STATUS;
1322 }
1323
1324 /* Interrupt service routine entry point.
1325  *      
1326  * Arguments:
1327  * 
1328  * irq     interrupt number that caused interrupt
1329  * dev_id  device ID supplied during interrupt registration
1330  * regs    interrupted processor context
1331  */
1332 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs)
1333 {
1334         MGSLPC_INFO * info = (MGSLPC_INFO *)dev_id;
1335         unsigned short isr;
1336         unsigned char gis, pis;
1337         int count=0;
1338
1339         if (debug_level >= DEBUG_LEVEL_ISR)     
1340                 printk("mgslpc_isr(%d) entry.\n", irq);
1341         if (!info)
1342                 return IRQ_NONE;
1343                 
1344         if (!(info->link.state & DEV_CONFIG))
1345                 return IRQ_HANDLED;
1346
1347         spin_lock(&info->lock);
1348
1349         while ((gis = read_reg(info, CHA + GIS))) {
1350                 if (debug_level >= DEBUG_LEVEL_ISR)     
1351                         printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1352
1353                 if ((gis & 0x70) || count > 1000) {
1354                         printk("synclink_cs:hardware failed or ejected\n");
1355                         break;
1356                 }
1357                 count++;
1358
1359                 if (gis & (BIT1 + BIT0)) {
1360                         isr = read_reg16(info, CHB + ISR);
1361                         if (isr & IRQ_DCD)
1362                                 dcd_change(info);
1363                         if (isr & IRQ_CTS)
1364                                 cts_change(info);
1365                 }
1366                 if (gis & (BIT3 + BIT2))
1367                 {
1368                         isr = read_reg16(info, CHA + ISR);
1369                         if (isr & IRQ_TIMER) {
1370                                 info->irq_occurred = 1;
1371                                 irq_disable(info, CHA, IRQ_TIMER);
1372                         }
1373
1374                         /* receive IRQs */ 
1375                         if (isr & IRQ_EXITHUNT) {
1376                                 info->icount.exithunt++;
1377                                 wake_up_interruptible(&info->event_wait_q);
1378                         }
1379                         if (isr & IRQ_BREAK_ON) {
1380                                 info->icount.brk++;
1381                                 if (info->flags & ASYNC_SAK)
1382                                         do_SAK(info->tty);
1383                         }
1384                         if (isr & IRQ_RXTIME) {
1385                                 issue_command(info, CHA, CMD_RXFIFO_READ);
1386                         }
1387                         if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1388                                 if (info->params.mode == MGSL_MODE_HDLC)
1389                                         rx_ready_hdlc(info, isr & IRQ_RXEOM); 
1390                                 else
1391                                         rx_ready_async(info, isr & IRQ_RXEOM);
1392                         }
1393
1394                         /* transmit IRQs */ 
1395                         if (isr & IRQ_UNDERRUN) {
1396                                 if (info->tx_aborting)
1397                                         info->icount.txabort++;
1398                                 else
1399                                         info->icount.txunder++;
1400                                 tx_done(info);
1401                         }
1402                         else if (isr & IRQ_ALLSENT) {
1403                                 info->icount.txok++;
1404                                 tx_done(info);
1405                         }
1406                         else if (isr & IRQ_TXFIFO)
1407                                 tx_ready(info);
1408                 }
1409                 if (gis & BIT7) {
1410                         pis = read_reg(info, CHA + PIS);
1411                         if (pis & BIT1)
1412                                 dsr_change(info);
1413                         if (pis & BIT2)
1414                                 ri_change(info);
1415                 }
1416         }
1417         
1418         /* Request bottom half processing if there's something 
1419          * for it to do and the bh is not already running
1420          */
1421
1422         if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1423                 if ( debug_level >= DEBUG_LEVEL_ISR )   
1424                         printk("%s(%d):%s queueing bh task.\n",
1425                                 __FILE__,__LINE__,info->device_name);
1426                 schedule_work(&info->task);
1427                 info->bh_requested = 1;
1428         }
1429
1430         spin_unlock(&info->lock);
1431         
1432         if (debug_level >= DEBUG_LEVEL_ISR)     
1433                 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1434                        __FILE__,__LINE__,irq);
1435
1436         return IRQ_HANDLED;
1437 }
1438
1439 /* Initialize and start device.
1440  */
1441 static int startup(MGSLPC_INFO * info)
1442 {
1443         int retval = 0;
1444         
1445         if (debug_level >= DEBUG_LEVEL_INFO)
1446                 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1447                 
1448         if (info->flags & ASYNC_INITIALIZED)
1449                 return 0;
1450         
1451         if (!info->tx_buf) {
1452                 /* allocate a page of memory for a transmit buffer */
1453                 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1454                 if (!info->tx_buf) {
1455                         printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1456                                 __FILE__,__LINE__,info->device_name);
1457                         return -ENOMEM;
1458                 }
1459         }
1460
1461         info->pending_bh = 0;
1462         
1463         init_timer(&info->tx_timer);
1464         info->tx_timer.data = (unsigned long)info;
1465         info->tx_timer.function = tx_timeout;
1466
1467         /* Allocate and claim adapter resources */
1468         retval = claim_resources(info);
1469         
1470         /* perform existance check and diagnostics */
1471         if ( !retval )
1472                 retval = adapter_test(info);
1473                 
1474         if ( retval ) {
1475                 if (capable(CAP_SYS_ADMIN) && info->tty)
1476                         set_bit(TTY_IO_ERROR, &info->tty->flags);
1477                 release_resources(info);
1478                 return retval;
1479         }
1480
1481         /* program hardware for current parameters */
1482         mgslpc_change_params(info);
1483         
1484         if (info->tty)
1485                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1486
1487         info->flags |= ASYNC_INITIALIZED;
1488         
1489         return 0;
1490 }
1491
1492 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1493  */
1494 static void shutdown(MGSLPC_INFO * info)
1495 {
1496         unsigned long flags;
1497         
1498         if (!(info->flags & ASYNC_INITIALIZED))
1499                 return;
1500
1501         if (debug_level >= DEBUG_LEVEL_INFO)
1502                 printk("%s(%d):mgslpc_shutdown(%s)\n",
1503                          __FILE__,__LINE__, info->device_name );
1504
1505         /* clear status wait queue because status changes */
1506         /* can't happen after shutting down the hardware */
1507         wake_up_interruptible(&info->status_event_wait_q);
1508         wake_up_interruptible(&info->event_wait_q);
1509
1510         del_timer(&info->tx_timer);     
1511
1512         if (info->tx_buf) {
1513                 free_page((unsigned long) info->tx_buf);
1514                 info->tx_buf = 0;
1515         }
1516
1517         spin_lock_irqsave(&info->lock,flags);
1518
1519         rx_stop(info);
1520         tx_stop(info);
1521
1522         /* TODO:disable interrupts instead of reset to preserve signal states */
1523         reset_device(info);
1524         
1525         if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1526                 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1527                 set_signals(info);
1528         }
1529         
1530         spin_unlock_irqrestore(&info->lock,flags);
1531
1532         release_resources(info);        
1533         
1534         if (info->tty)
1535                 set_bit(TTY_IO_ERROR, &info->tty->flags);
1536
1537         info->flags &= ~ASYNC_INITIALIZED;
1538 }
1539
1540 static void mgslpc_program_hw(MGSLPC_INFO *info)
1541 {
1542         unsigned long flags;
1543
1544         spin_lock_irqsave(&info->lock,flags);
1545         
1546         rx_stop(info);
1547         tx_stop(info);
1548         info->tx_count = info->tx_put = info->tx_get = 0;
1549         
1550         if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1551                 hdlc_mode(info);
1552         else
1553                 async_mode(info);
1554                 
1555         set_signals(info);
1556         
1557         info->dcd_chkcount = 0;
1558         info->cts_chkcount = 0;
1559         info->ri_chkcount = 0;
1560         info->dsr_chkcount = 0;
1561
1562         irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1563         port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1564         get_signals(info);
1565                 
1566         if (info->netcount || info->tty->termios->c_cflag & CREAD)
1567                 rx_start(info);
1568                 
1569         spin_unlock_irqrestore(&info->lock,flags);
1570 }
1571
1572 /* Reconfigure adapter based on new parameters
1573  */
1574 static void mgslpc_change_params(MGSLPC_INFO *info)
1575 {
1576         unsigned cflag;
1577         int bits_per_char;
1578
1579         if (!info->tty || !info->tty->termios)
1580                 return;
1581                 
1582         if (debug_level >= DEBUG_LEVEL_INFO)
1583                 printk("%s(%d):mgslpc_change_params(%s)\n",
1584                          __FILE__,__LINE__, info->device_name );
1585                          
1586         cflag = info->tty->termios->c_cflag;
1587
1588         /* if B0 rate (hangup) specified then negate DTR and RTS */
1589         /* otherwise assert DTR and RTS */
1590         if (cflag & CBAUD)
1591                 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1592         else
1593                 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1594         
1595         /* byte size and parity */
1596         
1597         switch (cflag & CSIZE) {
1598         case CS5: info->params.data_bits = 5; break;
1599         case CS6: info->params.data_bits = 6; break;
1600         case CS7: info->params.data_bits = 7; break;
1601         case CS8: info->params.data_bits = 8; break;
1602         default:  info->params.data_bits = 7; break;
1603         }
1604               
1605         if (cflag & CSTOPB)
1606                 info->params.stop_bits = 2;
1607         else
1608                 info->params.stop_bits = 1;
1609
1610         info->params.parity = ASYNC_PARITY_NONE;
1611         if (cflag & PARENB) {
1612                 if (cflag & PARODD)
1613                         info->params.parity = ASYNC_PARITY_ODD;
1614                 else
1615                         info->params.parity = ASYNC_PARITY_EVEN;
1616 #ifdef CMSPAR
1617                 if (cflag & CMSPAR)
1618                         info->params.parity = ASYNC_PARITY_SPACE;
1619 #endif
1620         }
1621
1622         /* calculate number of jiffies to transmit a full
1623          * FIFO (32 bytes) at specified data rate
1624          */
1625         bits_per_char = info->params.data_bits + 
1626                         info->params.stop_bits + 1;
1627
1628         /* if port data rate is set to 460800 or less then
1629          * allow tty settings to override, otherwise keep the
1630          * current data rate.
1631          */
1632         if (info->params.data_rate <= 460800) {
1633                 info->params.data_rate = tty_get_baud_rate(info->tty);
1634         }
1635         
1636         if ( info->params.data_rate ) {
1637                 info->timeout = (32*HZ*bits_per_char) / 
1638                                 info->params.data_rate;
1639         }
1640         info->timeout += HZ/50;         /* Add .02 seconds of slop */
1641
1642         if (cflag & CRTSCTS)
1643                 info->flags |= ASYNC_CTS_FLOW;
1644         else
1645                 info->flags &= ~ASYNC_CTS_FLOW;
1646                 
1647         if (cflag & CLOCAL)
1648                 info->flags &= ~ASYNC_CHECK_CD;
1649         else
1650                 info->flags |= ASYNC_CHECK_CD;
1651
1652         /* process tty input control flags */
1653         
1654         info->read_status_mask = 0;
1655         if (I_INPCK(info->tty))
1656                 info->read_status_mask |= BIT7 | BIT6;
1657         if (I_IGNPAR(info->tty))
1658                 info->ignore_status_mask |= BIT7 | BIT6;
1659
1660         mgslpc_program_hw(info);
1661 }
1662
1663 /* Add a character to the transmit buffer
1664  */
1665 static void mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1666 {
1667         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1668         unsigned long flags;
1669
1670         if (debug_level >= DEBUG_LEVEL_INFO) {
1671                 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1672                         __FILE__,__LINE__,ch,info->device_name);
1673         }
1674
1675         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1676                 return;
1677
1678         if (!tty || !info->tx_buf)
1679                 return;
1680
1681         spin_lock_irqsave(&info->lock,flags);
1682         
1683         if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1684                 if (info->tx_count < TXBUFSIZE - 1) {
1685                         info->tx_buf[info->tx_put++] = ch;
1686                         info->tx_put &= TXBUFSIZE-1;
1687                         info->tx_count++;
1688                 }
1689         }
1690         
1691         spin_unlock_irqrestore(&info->lock,flags);
1692 }
1693
1694 /* Enable transmitter so remaining characters in the
1695  * transmit buffer are sent.
1696  */
1697 static void mgslpc_flush_chars(struct tty_struct *tty)
1698 {
1699         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1700         unsigned long flags;
1701                                 
1702         if (debug_level >= DEBUG_LEVEL_INFO)
1703                 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1704                         __FILE__,__LINE__,info->device_name,info->tx_count);
1705         
1706         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1707                 return;
1708
1709         if (info->tx_count <= 0 || tty->stopped ||
1710             tty->hw_stopped || !info->tx_buf)
1711                 return;
1712
1713         if (debug_level >= DEBUG_LEVEL_INFO)
1714                 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1715                         __FILE__,__LINE__,info->device_name);
1716
1717         spin_lock_irqsave(&info->lock,flags);
1718         if (!info->tx_active)
1719                 tx_start(info);
1720         spin_unlock_irqrestore(&info->lock,flags);
1721 }
1722
1723 /* Send a block of data
1724  *      
1725  * Arguments:
1726  * 
1727  * tty        pointer to tty information structure
1728  * from_user  flag: 1 = from user process
1729  * buf        pointer to buffer containing send data
1730  * count      size of send data in bytes
1731  *      
1732  * Returns: number of characters written
1733  */
1734 static int mgslpc_write(struct tty_struct * tty, int from_user,
1735                         const unsigned char *buf, int count)
1736 {
1737         int c, ret = 0, err;
1738         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1739         unsigned long flags;
1740         
1741         if (debug_level >= DEBUG_LEVEL_INFO)
1742                 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1743                         __FILE__,__LINE__,info->device_name,count);
1744         
1745         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1746             !tty || !info->tx_buf)
1747                 goto cleanup;
1748
1749         if (info->params.mode == MGSL_MODE_HDLC) {
1750                 if (count > TXBUFSIZE) {
1751                         ret = -EIO;
1752                         goto cleanup;
1753                 }
1754                 if (info->tx_active)
1755                         goto cleanup;
1756                 else if (info->tx_count)
1757                         goto start;
1758         }
1759
1760         for (;;) {
1761                 c = MIN(count,
1762                         MIN(TXBUFSIZE - info->tx_count - 1,
1763                             TXBUFSIZE - info->tx_put));
1764                 if (c <= 0)
1765                         break;
1766                         
1767                 if (from_user) {
1768                         COPY_FROM_USER(err, info->tx_buf + info->tx_put, buf, c);
1769                         if (err) {
1770                                 if (!ret)
1771                                         ret = -EFAULT;
1772                                 break;
1773                         }
1774                 } else
1775                         memcpy(info->tx_buf + info->tx_put, buf, c);
1776
1777                 spin_lock_irqsave(&info->lock,flags);
1778                 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1779                 info->tx_count += c;
1780                 spin_unlock_irqrestore(&info->lock,flags);
1781
1782                 buf += c;
1783                 count -= c;
1784                 ret += c;
1785         }
1786 start:
1787         if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1788                 spin_lock_irqsave(&info->lock,flags);
1789                 if (!info->tx_active)
1790                         tx_start(info);
1791                 spin_unlock_irqrestore(&info->lock,flags);
1792         }
1793 cleanup:        
1794         if (debug_level >= DEBUG_LEVEL_INFO)
1795                 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1796                         __FILE__,__LINE__,info->device_name,ret);
1797         return ret;
1798 }
1799
1800 /* Return the count of free bytes in transmit buffer
1801  */
1802 static int mgslpc_write_room(struct tty_struct *tty)
1803 {
1804         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1805         int ret;
1806                                 
1807         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1808                 return 0;
1809
1810         if (info->params.mode == MGSL_MODE_HDLC) {
1811                 /* HDLC (frame oriented) mode */
1812                 if (info->tx_active)
1813                         return 0;
1814                 else
1815                         return HDLC_MAX_FRAME_SIZE;
1816         } else {
1817                 ret = TXBUFSIZE - info->tx_count - 1;
1818                 if (ret < 0)
1819                         ret = 0;
1820         }
1821         
1822         if (debug_level >= DEBUG_LEVEL_INFO)
1823                 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1824                          __FILE__,__LINE__, info->device_name, ret);
1825         return ret;
1826 }
1827
1828 /* Return the count of bytes in transmit buffer
1829  */
1830 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1831 {
1832         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1833         int rc;
1834                  
1835         if (debug_level >= DEBUG_LEVEL_INFO)
1836                 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1837                          __FILE__,__LINE__, info->device_name );
1838                          
1839         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1840                 return 0;
1841                 
1842         if (info->params.mode == MGSL_MODE_HDLC)
1843                 rc = info->tx_active ? info->max_frame_size : 0;
1844         else
1845                 rc = info->tx_count;
1846
1847         if (debug_level >= DEBUG_LEVEL_INFO)
1848                 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1849                          __FILE__,__LINE__, info->device_name, rc);
1850                          
1851         return rc;
1852 }
1853
1854 /* Discard all data in the send buffer
1855  */
1856 static void mgslpc_flush_buffer(struct tty_struct *tty)
1857 {
1858         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1859         unsigned long flags;
1860         
1861         if (debug_level >= DEBUG_LEVEL_INFO)
1862                 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1863                          __FILE__,__LINE__, info->device_name );
1864         
1865         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1866                 return;
1867                 
1868         spin_lock_irqsave(&info->lock,flags); 
1869         info->tx_count = info->tx_put = info->tx_get = 0;
1870         del_timer(&info->tx_timer);     
1871         spin_unlock_irqrestore(&info->lock,flags);
1872         
1873         wake_up_interruptible(&tty->write_wait);
1874         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1875             tty->ldisc.write_wakeup)
1876                 (tty->ldisc.write_wakeup)(tty);
1877 }
1878
1879 /* Send a high-priority XON/XOFF character
1880  */
1881 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1882 {
1883         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1884         unsigned long flags;
1885
1886         if (debug_level >= DEBUG_LEVEL_INFO)
1887                 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1888                          __FILE__,__LINE__, info->device_name, ch );
1889                          
1890         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1891                 return;
1892
1893         info->x_char = ch;
1894         if (ch) {
1895                 spin_lock_irqsave(&info->lock,flags);
1896                 if (!info->tx_enabled)
1897                         tx_start(info);
1898                 spin_unlock_irqrestore(&info->lock,flags);
1899         }
1900 }
1901
1902 /* Signal remote device to throttle send data (our receive data)
1903  */
1904 static void mgslpc_throttle(struct tty_struct * tty)
1905 {
1906         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1907         unsigned long flags;
1908         
1909         if (debug_level >= DEBUG_LEVEL_INFO)
1910                 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1911                          __FILE__,__LINE__, info->device_name );
1912
1913         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1914                 return;
1915         
1916         if (I_IXOFF(tty))
1917                 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1918  
1919         if (tty->termios->c_cflag & CRTSCTS) {
1920                 spin_lock_irqsave(&info->lock,flags);
1921                 info->serial_signals &= ~SerialSignal_RTS;
1922                 set_signals(info);
1923                 spin_unlock_irqrestore(&info->lock,flags);
1924         }
1925 }
1926
1927 /* Signal remote device to stop throttling send data (our receive data)
1928  */
1929 static void mgslpc_unthrottle(struct tty_struct * tty)
1930 {
1931         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1932         unsigned long flags;
1933         
1934         if (debug_level >= DEBUG_LEVEL_INFO)
1935                 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1936                          __FILE__,__LINE__, info->device_name );
1937
1938         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1939                 return;
1940         
1941         if (I_IXOFF(tty)) {
1942                 if (info->x_char)
1943                         info->x_char = 0;
1944                 else
1945                         mgslpc_send_xchar(tty, START_CHAR(tty));
1946         }
1947         
1948         if (tty->termios->c_cflag & CRTSCTS) {
1949                 spin_lock_irqsave(&info->lock,flags);
1950                 info->serial_signals |= SerialSignal_RTS;
1951                 set_signals(info);
1952                 spin_unlock_irqrestore(&info->lock,flags);
1953         }
1954 }
1955
1956 /* get the current serial statistics
1957  */
1958 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount *user_icount)
1959 {
1960         int err;
1961         if (debug_level >= DEBUG_LEVEL_INFO)
1962                 printk("get_params(%s)\n", info->device_name);
1963         COPY_TO_USER(err,user_icount, &info->icount, sizeof(struct mgsl_icount));
1964         if (err)
1965                 return -EFAULT;
1966         return 0;
1967 }
1968
1969 /* get the current serial parameters
1970  */
1971 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS *user_params)
1972 {
1973         int err;
1974         if (debug_level >= DEBUG_LEVEL_INFO)
1975                 printk("get_params(%s)\n", info->device_name);
1976         COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1977         if (err)
1978                 return -EFAULT;
1979         return 0;
1980 }
1981
1982 /* set the serial parameters
1983  *      
1984  * Arguments:
1985  * 
1986  *      info            pointer to device instance data
1987  *      new_params      user buffer containing new serial params
1988  *
1989  * Returns:     0 if success, otherwise error code
1990  */
1991 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS *new_params)
1992 {
1993         unsigned long flags;
1994         MGSL_PARAMS tmp_params;
1995         int err;
1996  
1997         if (debug_level >= DEBUG_LEVEL_INFO)
1998                 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1999                         info->device_name );
2000         COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2001         if (err) {
2002                 if ( debug_level >= DEBUG_LEVEL_INFO )
2003                         printk( "%s(%d):set_params(%s) user buffer copy failed\n",
2004                                 __FILE__,__LINE__,info->device_name);
2005                 return -EFAULT;
2006         }
2007         
2008         spin_lock_irqsave(&info->lock,flags);
2009         memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2010         spin_unlock_irqrestore(&info->lock,flags);
2011         
2012         mgslpc_change_params(info);
2013         
2014         return 0;
2015 }
2016
2017 static int get_txidle(MGSLPC_INFO * info, int*idle_mode)
2018 {
2019         int err;
2020         if (debug_level >= DEBUG_LEVEL_INFO)
2021                 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
2022         COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2023         if (err)
2024                 return -EFAULT;
2025         return 0;
2026 }
2027
2028 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
2029 {
2030         unsigned long flags;
2031         if (debug_level >= DEBUG_LEVEL_INFO)
2032                 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
2033         spin_lock_irqsave(&info->lock,flags);
2034         info->idle_mode = idle_mode;
2035         tx_set_idle(info);
2036         spin_unlock_irqrestore(&info->lock,flags);
2037         return 0;
2038 }
2039
2040 static int get_interface(MGSLPC_INFO * info, int*if_mode)
2041 {
2042         int err;
2043         if (debug_level >= DEBUG_LEVEL_INFO)
2044                 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
2045         COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
2046         if (err)
2047                 return -EFAULT;
2048         return 0;
2049 }
2050
2051 static int set_interface(MGSLPC_INFO * info, int if_mode)
2052 {
2053         unsigned long flags;
2054         unsigned char val;
2055         if (debug_level >= DEBUG_LEVEL_INFO)
2056                 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
2057         spin_lock_irqsave(&info->lock,flags);
2058         info->if_mode = if_mode;
2059
2060         val = read_reg(info, PVR) & 0x0f;
2061         switch (info->if_mode)
2062         {
2063         case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
2064         case MGSL_INTERFACE_V35:   val |= PVR_V35;   break;
2065         case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
2066         }
2067         write_reg(info, PVR, val);
2068
2069         spin_unlock_irqrestore(&info->lock,flags);
2070         return 0;
2071 }
2072
2073 static int set_txenable(MGSLPC_INFO * info, int enable)
2074 {
2075         unsigned long flags;
2076  
2077         if (debug_level >= DEBUG_LEVEL_INFO)
2078                 printk("set_txenable(%s,%d)\n", info->device_name, enable);
2079                         
2080         spin_lock_irqsave(&info->lock,flags);
2081         if (enable) {
2082                 if (!info->tx_enabled)
2083                         tx_start(info);
2084         } else {
2085                 if (info->tx_enabled)
2086                         tx_stop(info);
2087         }
2088         spin_unlock_irqrestore(&info->lock,flags);
2089         return 0;
2090 }
2091
2092 static int tx_abort(MGSLPC_INFO * info)
2093 {
2094         unsigned long flags;
2095  
2096         if (debug_level >= DEBUG_LEVEL_INFO)
2097                 printk("tx_abort(%s)\n", info->device_name);
2098                         
2099         spin_lock_irqsave(&info->lock,flags);
2100         if (info->tx_active && info->tx_count &&
2101             info->params.mode == MGSL_MODE_HDLC) {
2102                 /* clear data count so FIFO is not filled on next IRQ.
2103                  * This results in underrun and abort transmission.
2104                  */
2105                 info->tx_count = info->tx_put = info->tx_get = 0;
2106                 info->tx_aborting = TRUE;
2107         }
2108         spin_unlock_irqrestore(&info->lock,flags);
2109         return 0;
2110 }
2111
2112 static int set_rxenable(MGSLPC_INFO * info, int enable)
2113 {
2114         unsigned long flags;
2115  
2116         if (debug_level >= DEBUG_LEVEL_INFO)
2117                 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
2118                         
2119         spin_lock_irqsave(&info->lock,flags);
2120         if (enable) {
2121                 if (!info->rx_enabled)
2122                         rx_start(info);
2123         } else {
2124                 if (info->rx_enabled)
2125                         rx_stop(info);
2126         }
2127         spin_unlock_irqrestore(&info->lock,flags);
2128         return 0;
2129 }
2130
2131 /* wait for specified event to occur
2132  *      
2133  * Arguments:           info    pointer to device instance data
2134  *                      mask    pointer to bitmask of events to wait for
2135  * Return Value:        0       if successful and bit mask updated with
2136  *                              of events triggerred,
2137  *                      otherwise error code
2138  */
2139 static int wait_events(MGSLPC_INFO * info, int * mask_ptr)
2140 {
2141         unsigned long flags;
2142         int s;
2143         int rc=0;
2144         struct mgsl_icount cprev, cnow;
2145         int events;
2146         int mask;
2147         struct  _input_signal_events oldsigs, newsigs;
2148         DECLARE_WAITQUEUE(wait, current);
2149
2150         COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2151         if (rc)
2152                 return  -EFAULT;
2153                  
2154         if (debug_level >= DEBUG_LEVEL_INFO)
2155                 printk("wait_events(%s,%d)\n", info->device_name, mask);
2156
2157         spin_lock_irqsave(&info->lock,flags);
2158
2159         /* return immediately if state matches requested events */
2160         get_signals(info);
2161         s = info->serial_signals;
2162         events = mask &
2163                 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2164                   ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2165                   ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2166                   ((s & SerialSignal_RI)  ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2167         if (events) {
2168                 spin_unlock_irqrestore(&info->lock,flags);
2169                 goto exit;
2170         }
2171
2172         /* save current irq counts */
2173         cprev = info->icount;
2174         oldsigs = info->input_signal_events;
2175         
2176         if ((info->params.mode == MGSL_MODE_HDLC) &&
2177             (mask & MgslEvent_ExitHuntMode))
2178                 irq_enable(info, CHA, IRQ_EXITHUNT);
2179         
2180         set_current_state(TASK_INTERRUPTIBLE);
2181         add_wait_queue(&info->event_wait_q, &wait);
2182         
2183         spin_unlock_irqrestore(&info->lock,flags);
2184         
2185         
2186         for(;;) {
2187                 schedule();
2188                 if (signal_pending(current)) {
2189                         rc = -ERESTARTSYS;
2190                         break;
2191                 }
2192                         
2193                 /* get current irq counts */
2194                 spin_lock_irqsave(&info->lock,flags);
2195                 cnow = info->icount;
2196                 newsigs = info->input_signal_events;
2197                 set_current_state(TASK_INTERRUPTIBLE);
2198                 spin_unlock_irqrestore(&info->lock,flags);
2199
2200                 /* if no change, wait aborted for some reason */
2201                 if (newsigs.dsr_up   == oldsigs.dsr_up   &&
2202                     newsigs.dsr_down == oldsigs.dsr_down &&
2203                     newsigs.dcd_up   == oldsigs.dcd_up   &&
2204                     newsigs.dcd_down == oldsigs.dcd_down &&
2205                     newsigs.cts_up   == oldsigs.cts_up   &&
2206                     newsigs.cts_down == oldsigs.cts_down &&
2207                     newsigs.ri_up    == oldsigs.ri_up    &&
2208                     newsigs.ri_down  == oldsigs.ri_down  &&
2209                     cnow.exithunt    == cprev.exithunt   &&
2210                     cnow.rxidle      == cprev.rxidle) {
2211                         rc = -EIO;
2212                         break;
2213                 }
2214
2215                 events = mask &
2216                         ( (newsigs.dsr_up   != oldsigs.dsr_up   ? MgslEvent_DsrActive:0)   +
2217                           (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2218                           (newsigs.dcd_up   != oldsigs.dcd_up   ? MgslEvent_DcdActive:0)   +
2219                           (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2220                           (newsigs.cts_up   != oldsigs.cts_up   ? MgslEvent_CtsActive:0)   +
2221                           (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2222                           (newsigs.ri_up    != oldsigs.ri_up    ? MgslEvent_RiActive:0)    +
2223                           (newsigs.ri_down  != oldsigs.ri_down  ? MgslEvent_RiInactive:0)  +
2224                           (cnow.exithunt    != cprev.exithunt   ? MgslEvent_ExitHuntMode:0) +
2225                           (cnow.rxidle      != cprev.rxidle     ? MgslEvent_IdleReceived:0) );
2226                 if (events)
2227                         break;
2228                 
2229                 cprev = cnow;
2230                 oldsigs = newsigs;
2231         }
2232         
2233         remove_wait_queue(&info->event_wait_q, &wait);
2234         set_current_state(TASK_RUNNING);
2235
2236         if (mask & MgslEvent_ExitHuntMode) {
2237                 spin_lock_irqsave(&info->lock,flags);
2238                 if (!waitqueue_active(&info->event_wait_q))
2239                         irq_disable(info, CHA, IRQ_EXITHUNT);
2240                 spin_unlock_irqrestore(&info->lock,flags);
2241         }
2242 exit:
2243         if (rc == 0)
2244                 PUT_USER(rc, events, mask_ptr);
2245         return rc;
2246 }
2247
2248 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2249 {
2250         unsigned long flags;
2251         int rc;
2252         struct mgsl_icount cprev, cnow;
2253         DECLARE_WAITQUEUE(wait, current);
2254
2255         /* save current irq counts */
2256         spin_lock_irqsave(&info->lock,flags);
2257         cprev = info->icount;
2258         add_wait_queue(&info->status_event_wait_q, &wait);
2259         set_current_state(TASK_INTERRUPTIBLE);
2260         spin_unlock_irqrestore(&info->lock,flags);
2261
2262         for(;;) {
2263                 schedule();
2264                 if (signal_pending(current)) {
2265                         rc = -ERESTARTSYS;
2266                         break;
2267                 }
2268
2269                 /* get new irq counts */
2270                 spin_lock_irqsave(&info->lock,flags);
2271                 cnow = info->icount;
2272                 set_current_state(TASK_INTERRUPTIBLE);
2273                 spin_unlock_irqrestore(&info->lock,flags);
2274
2275                 /* if no change, wait aborted for some reason */
2276                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2277                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2278                         rc = -EIO;
2279                         break;
2280                 }
2281
2282                 /* check for change in caller specified modem input */
2283                 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2284                     (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2285                     (arg & TIOCM_CD  && cnow.dcd != cprev.dcd) ||
2286                     (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2287                         rc = 0;
2288                         break;
2289                 }
2290
2291                 cprev = cnow;
2292         }
2293         remove_wait_queue(&info->status_event_wait_q, &wait);
2294         set_current_state(TASK_RUNNING);
2295         return rc;
2296 }
2297
2298 /* return the state of the serial control and status signals
2299  */
2300 static int tiocmget(struct tty_struct *tty, struct file *file)
2301 {
2302         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2303         unsigned int result;
2304         unsigned long flags;
2305
2306         spin_lock_irqsave(&info->lock,flags);
2307         get_signals(info);
2308         spin_unlock_irqrestore(&info->lock,flags);
2309
2310         result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2311                 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2312                 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2313                 ((info->serial_signals & SerialSignal_RI)  ? TIOCM_RNG:0) +
2314                 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2315                 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2316
2317         if (debug_level >= DEBUG_LEVEL_INFO)
2318                 printk("%s(%d):%s tiocmget() value=%08X\n",
2319                          __FILE__,__LINE__, info->device_name, result );
2320         return result;
2321 }
2322
2323 /* set modem control signals (DTR/RTS)
2324  */
2325 static int tiocmset(struct tty_struct *tty, struct file *file,
2326                     unsigned int set, unsigned int clear)
2327 {
2328         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2329         unsigned long flags;
2330
2331         if (debug_level >= DEBUG_LEVEL_INFO)
2332                 printk("%s(%d):%s tiocmset(%x,%x)\n",
2333                         __FILE__,__LINE__,info->device_name, set, clear);
2334
2335         if (set & TIOCM_RTS)
2336                 info->serial_signals |= SerialSignal_RTS;
2337         if (set & TIOCM_DTR)
2338                 info->serial_signals |= SerialSignal_DTR;
2339         if (clear & TIOCM_RTS)
2340                 info->serial_signals &= ~SerialSignal_RTS;
2341         if (clear & TIOCM_DTR)
2342                 info->serial_signals &= ~SerialSignal_DTR;
2343
2344         spin_lock_irqsave(&info->lock,flags);
2345         set_signals(info);
2346         spin_unlock_irqrestore(&info->lock,flags);
2347
2348         return 0;
2349 }
2350
2351 /* Set or clear transmit break condition
2352  *
2353  * Arguments:           tty             pointer to tty instance data
2354  *                      break_state     -1=set break condition, 0=clear
2355  */
2356 static void mgslpc_break(struct tty_struct *tty, int break_state)
2357 {
2358         MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2359         unsigned long flags;
2360         
2361         if (debug_level >= DEBUG_LEVEL_INFO)
2362                 printk("%s(%d):mgslpc_break(%s,%d)\n",
2363                          __FILE__,__LINE__, info->device_name, break_state);
2364                          
2365         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2366                 return;
2367
2368         spin_lock_irqsave(&info->lock,flags);
2369         if (break_state == -1)
2370                 set_reg_bits(info, CHA+DAFO, BIT6);
2371         else 
2372                 clear_reg_bits(info, CHA+DAFO, BIT6);
2373         spin_unlock_irqrestore(&info->lock,flags);
2374 }
2375
2376 /* Service an IOCTL request
2377  *      
2378  * Arguments:
2379  * 
2380  *      tty     pointer to tty instance data
2381  *      file    pointer to associated file object for device
2382  *      cmd     IOCTL command code
2383  *      arg     command argument/context
2384  *      
2385  * Return Value:        0 if success, otherwise error code
2386  */
2387 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2388                         unsigned int cmd, unsigned long arg)
2389 {
2390         MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2391         
2392         if (debug_level >= DEBUG_LEVEL_INFO)
2393                 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2394                         info->device_name, cmd );
2395         
2396         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2397                 return -ENODEV;
2398
2399         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2400             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2401                 if (tty->flags & (1 << TTY_IO_ERROR))
2402                     return -EIO;
2403         }
2404
2405         return ioctl_common(info, cmd, arg);
2406 }
2407
2408 int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
2409 {
2410         int error;
2411         struct mgsl_icount cnow;        /* kernel counter temps */
2412         struct serial_icounter_struct *p_cuser; /* user space */
2413         unsigned long flags;
2414         
2415         switch (cmd) {
2416         case MGSL_IOCGPARAMS:
2417                 return get_params(info,(MGSL_PARAMS *)arg);
2418         case MGSL_IOCSPARAMS:
2419                 return set_params(info,(MGSL_PARAMS *)arg);
2420         case MGSL_IOCGTXIDLE:
2421                 return get_txidle(info,(int*)arg);
2422         case MGSL_IOCSTXIDLE:
2423                 return set_txidle(info,(int)arg);
2424         case MGSL_IOCGIF:
2425                 return get_interface(info,(int*)arg);
2426         case MGSL_IOCSIF:
2427                 return set_interface(info,(int)arg);
2428         case MGSL_IOCTXENABLE:
2429                 return set_txenable(info,(int)arg);
2430         case MGSL_IOCRXENABLE:
2431                 return set_rxenable(info,(int)arg);
2432         case MGSL_IOCTXABORT:
2433                 return tx_abort(info);
2434         case MGSL_IOCGSTATS:
2435                 return get_stats(info,(struct mgsl_icount*)arg);
2436         case MGSL_IOCWAITEVENT:
2437                 return wait_events(info,(int*)arg);
2438         case TIOCMIWAIT:
2439                 return modem_input_wait(info,(int)arg);
2440         case TIOCGICOUNT:
2441                 spin_lock_irqsave(&info->lock,flags);
2442                 cnow = info->icount;
2443                 spin_unlock_irqrestore(&info->lock,flags);
2444                 p_cuser = (struct serial_icounter_struct *) arg;
2445                 PUT_USER(error,cnow.cts, &p_cuser->cts);
2446                 if (error) return error;
2447                 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2448                 if (error) return error;
2449                 PUT_USER(error,cnow.rng, &p_cuser->rng);
2450                 if (error) return error;
2451                 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2452                 if (error) return error;
2453                 PUT_USER(error,cnow.rx, &p_cuser->rx);
2454                 if (error) return error;
2455                 PUT_USER(error,cnow.tx, &p_cuser->tx);
2456                 if (error) return error;
2457                 PUT_USER(error,cnow.frame, &p_cuser->frame);
2458                 if (error) return error;
2459                 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2460                 if (error) return error;
2461                 PUT_USER(error,cnow.parity, &p_cuser->parity);
2462                 if (error) return error;
2463                 PUT_USER(error,cnow.brk, &p_cuser->brk);
2464                 if (error) return error;
2465                 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2466                 if (error) return error;
2467                 return 0;
2468         default:
2469                 return -ENOIOCTLCMD;
2470         }
2471         return 0;
2472 }
2473
2474 /* Set new termios settings
2475  *      
2476  * Arguments:
2477  * 
2478  *      tty             pointer to tty structure
2479  *      termios         pointer to buffer to hold returned old termios
2480  */
2481 static void mgslpc_set_termios(struct tty_struct *tty, struct termios *old_termios)
2482 {
2483         MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2484         unsigned long flags;
2485         
2486         if (debug_level >= DEBUG_LEVEL_INFO)
2487                 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2488                         tty->driver->name );
2489         
2490         /* just return if nothing has changed */
2491         if ((tty->termios->c_cflag == old_termios->c_cflag)
2492             && (RELEVANT_IFLAG(tty->termios->c_iflag) 
2493                 == RELEVANT_IFLAG(old_termios->c_iflag)))
2494           return;
2495
2496         mgslpc_change_params(info);
2497
2498         /* Handle transition to B0 status */
2499         if (old_termios->c_cflag & CBAUD &&
2500             !(tty->termios->c_cflag & CBAUD)) {
2501                 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2502                 spin_lock_irqsave(&info->lock,flags);
2503                 set_signals(info);
2504                 spin_unlock_irqrestore(&info->lock,flags);
2505         }
2506         
2507         /* Handle transition away from B0 status */
2508         if (!(old_termios->c_cflag & CBAUD) &&
2509             tty->termios->c_cflag & CBAUD) {
2510                 info->serial_signals |= SerialSignal_DTR;
2511                 if (!(tty->termios->c_cflag & CRTSCTS) || 
2512                     !test_bit(TTY_THROTTLED, &tty->flags)) {
2513                         info->serial_signals |= SerialSignal_RTS;
2514                 }
2515                 spin_lock_irqsave(&info->lock,flags);
2516                 set_signals(info);
2517                 spin_unlock_irqrestore(&info->lock,flags);
2518         }
2519         
2520         /* Handle turning off CRTSCTS */
2521         if (old_termios->c_cflag & CRTSCTS &&
2522             !(tty->termios->c_cflag & CRTSCTS)) {
2523                 tty->hw_stopped = 0;
2524                 tx_release(tty);
2525         }
2526 }
2527
2528 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2529 {
2530         MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2531
2532         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2533                 return;
2534         
2535         if (debug_level >= DEBUG_LEVEL_INFO)
2536                 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2537                          __FILE__,__LINE__, info->device_name, info->count);
2538                          
2539         if (!info->count)
2540                 return;
2541
2542         if (tty_hung_up_p(filp))
2543                 goto cleanup;
2544                         
2545         if ((tty->count == 1) && (info->count != 1)) {
2546                 /*
2547                  * tty->count is 1 and the tty structure will be freed.
2548                  * info->count should be one in this case.
2549                  * if it's not, correct it so that the port is shutdown.
2550                  */
2551                 printk("mgslpc_close: bad refcount; tty->count is 1, "
2552                        "info->count is %d\n", info->count);
2553                 info->count = 1;
2554         }
2555         
2556         info->count--;
2557         
2558         /* if at least one open remaining, leave hardware active */
2559         if (info->count)
2560                 goto cleanup;
2561         
2562         info->flags |= ASYNC_CLOSING;
2563         
2564         /* set tty->closing to notify line discipline to 
2565          * only process XON/XOFF characters. Only the N_TTY
2566          * discipline appears to use this (ppp does not).
2567          */
2568         tty->closing = 1;
2569         
2570         /* wait for transmit data to clear all layers */
2571         
2572         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
2573                 if (debug_level >= DEBUG_LEVEL_INFO)
2574                         printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sent\n",
2575                                  __FILE__,__LINE__, info->device_name );
2576                 tty_wait_until_sent(tty, info->closing_wait);
2577         }
2578                 
2579         if (info->flags & ASYNC_INITIALIZED)
2580                 mgslpc_wait_until_sent(tty, info->timeout);
2581
2582         if (tty->driver->flush_buffer)
2583                 tty->driver->flush_buffer(tty);
2584                 
2585         if (tty->ldisc.flush_buffer)
2586                 tty->ldisc.flush_buffer(tty);
2587                 
2588         shutdown(info);
2589         
2590         tty->closing = 0;
2591         info->tty = 0;
2592         
2593         if (info->blocked_open) {
2594                 if (info->close_delay) {
2595                         set_current_state(TASK_INTERRUPTIBLE);
2596                         schedule_timeout(info->close_delay);
2597                 }
2598                 wake_up_interruptible(&info->open_wait);
2599         }
2600         
2601         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2602                          
2603         wake_up_interruptible(&info->close_wait);
2604         
2605 cleanup:                        
2606         if (debug_level >= DEBUG_LEVEL_INFO)
2607                 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2608                         tty->driver->name, info->count);
2609 }
2610
2611 /* Wait until the transmitter is empty.
2612  */
2613 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2614 {
2615         MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2616         unsigned long orig_jiffies, char_time;
2617
2618         if (!info )
2619                 return;
2620
2621         if (debug_level >= DEBUG_LEVEL_INFO)
2622                 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2623                          __FILE__,__LINE__, info->device_name );
2624       
2625         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2626                 return;
2627
2628         if (!(info->flags & ASYNC_INITIALIZED))
2629                 goto exit;
2630          
2631         orig_jiffies = jiffies;
2632       
2633         /* Set check interval to 1/5 of estimated time to
2634          * send a character, and make it at least 1. The check
2635          * interval should also be less than the timeout.
2636          * Note: use tight timings here to satisfy the NIST-PCTS.
2637          */ 
2638        
2639         if ( info->params.data_rate ) {
2640                 char_time = info->timeout/(32 * 5);
2641                 if (!char_time)
2642                         char_time++;
2643         } else
2644                 char_time = 1;
2645                 
2646         if (timeout)
2647                 char_time = MIN(char_time, timeout);
2648                 
2649         if (info->params.mode == MGSL_MODE_HDLC) {
2650                 while (info->tx_active) {
2651                         set_current_state(TASK_INTERRUPTIBLE);
2652                         schedule_timeout(char_time);
2653                         if (signal_pending(current))
2654                                 break;
2655                         if (timeout && time_after(jiffies, orig_jiffies + timeout))
2656                                 break;
2657                 }
2658         } else {
2659                 while ((info->tx_count || info->tx_active) &&
2660                         info->tx_enabled) {
2661                         set_current_state(TASK_INTERRUPTIBLE);
2662                         schedule_timeout(char_time);
2663                         if (signal_pending(current))
2664                                 break;
2665                         if (timeout && time_after(jiffies, orig_jiffies + timeout))
2666                                 break;
2667                 }
2668         }
2669       
2670 exit:
2671         if (debug_level >= DEBUG_LEVEL_INFO)
2672                 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2673                          __FILE__,__LINE__, info->device_name );
2674 }
2675
2676 /* Called by tty_hangup() when a hangup is signaled.
2677  * This is the same as closing all open files for the port.
2678  */
2679 static void mgslpc_hangup(struct tty_struct *tty)
2680 {
2681         MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2682         
2683         if (debug_level >= DEBUG_LEVEL_INFO)
2684                 printk("%s(%d):mgslpc_hangup(%s)\n",
2685                          __FILE__,__LINE__, info->device_name );
2686                          
2687         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2688                 return;
2689
2690         mgslpc_flush_buffer(tty);
2691         shutdown(info);
2692         
2693         info->count = 0;        
2694         info->flags &= ~ASYNC_NORMAL_ACTIVE;
2695         info->tty = 0;
2696
2697         wake_up_interruptible(&info->open_wait);
2698 }
2699
2700 /* Block the current process until the specified port
2701  * is ready to be opened.
2702  */
2703 static int block_til_ready(struct tty_struct *tty, struct file *filp,
2704                            MGSLPC_INFO *info)
2705 {
2706         DECLARE_WAITQUEUE(wait, current);
2707         int             retval;
2708         int             do_clocal = 0, extra_count = 0;
2709         unsigned long   flags;
2710         
2711         if (debug_level >= DEBUG_LEVEL_INFO)
2712                 printk("%s(%d):block_til_ready on %s\n",
2713                          __FILE__,__LINE__, tty->driver->name );
2714
2715         if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
2716                 /* nonblock mode is set or port is not enabled */
2717                 /* just verify that callout device is not active */
2718                 info->flags |= ASYNC_NORMAL_ACTIVE;
2719                 return 0;
2720         }
2721
2722         if (tty->termios->c_cflag & CLOCAL)
2723                 do_clocal = 1;
2724
2725         /* Wait for carrier detect and the line to become
2726          * free (i.e., not in use by the callout).  While we are in
2727          * this loop, info->count is dropped by one, so that
2728          * mgslpc_close() knows when to free things.  We restore it upon
2729          * exit, either normal or abnormal.
2730          */
2731          
2732         retval = 0;
2733         add_wait_queue(&info->open_wait, &wait);
2734         
2735         if (debug_level >= DEBUG_LEVEL_INFO)
2736                 printk("%s(%d):block_til_ready before block on %s count=%d\n",
2737                          __FILE__,__LINE__, tty->driver->name, info->count );
2738
2739         spin_lock_irqsave(&info->lock, flags);
2740         if (!tty_hung_up_p(filp)) {
2741                 extra_count = 1;
2742                 info->count--;
2743         }
2744         spin_unlock_irqrestore(&info->lock, flags);
2745         info->blocked_open++;
2746         
2747         while (1) {
2748                 if ((tty->termios->c_cflag & CBAUD)) {
2749                         spin_lock_irqsave(&info->lock,flags);
2750                         info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2751                         set_signals(info);
2752                         spin_unlock_irqrestore(&info->lock,flags);
2753                 }
2754                 
2755                 set_current_state(TASK_INTERRUPTIBLE);
2756                 
2757                 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
2758                         retval = (info->flags & ASYNC_HUP_NOTIFY) ?
2759                                         -EAGAIN : -ERESTARTSYS;
2760                         break;
2761                 }
2762                 
2763                 spin_lock_irqsave(&info->lock,flags);
2764                 get_signals(info);
2765                 spin_unlock_irqrestore(&info->lock,flags);
2766                 
2767                 if (!(info->flags & ASYNC_CLOSING) &&
2768                     (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
2769                         break;
2770                 }
2771                         
2772                 if (signal_pending(current)) {
2773                         retval = -ERESTARTSYS;
2774                         break;
2775                 }
2776                 
2777                 if (debug_level >= DEBUG_LEVEL_INFO)
2778                         printk("%s(%d):block_til_ready blocking on %s count=%d\n",
2779                                  __FILE__,__LINE__, tty->driver->name, info->count );
2780                                  
2781                 schedule();
2782         }
2783         
2784         set_current_state(TASK_RUNNING);
2785         remove_wait_queue(&info->open_wait, &wait);
2786         
2787         if (extra_count)
2788                 info->count++;
2789         info->blocked_open--;
2790         
2791         if (debug_level >= DEBUG_LEVEL_INFO)
2792                 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
2793                          __FILE__,__LINE__, tty->driver->name, info->count );
2794                          
2795         if (!retval)
2796                 info->flags |= ASYNC_NORMAL_ACTIVE;
2797                 
2798         return retval;
2799 }
2800
2801 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2802 {
2803         MGSLPC_INFO     *info;
2804         int                     retval, line;
2805         unsigned long flags;
2806
2807         /* verify range of specified line number */     
2808         line = tty->index;
2809         if ((line < 0) || (line >= mgslpc_device_count)) {
2810                 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2811                         __FILE__,__LINE__,line);
2812                 return -ENODEV;
2813         }
2814
2815         /* find the info structure for the specified line */
2816         info = mgslpc_device_list;
2817         while(info && info->line != line)
2818                 info = info->next_device;
2819         if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2820                 return -ENODEV;
2821         
2822         tty->driver_data = info;
2823         info->tty = tty;
2824                 
2825         if (debug_level >= DEBUG_LEVEL_INFO)
2826                 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2827                          __FILE__,__LINE__,tty->driver->name, info->count);
2828
2829         /* If port is closing, signal caller to try again */
2830         if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
2831                 if (info->flags & ASYNC_CLOSING)
2832                         interruptible_sleep_on(&info->close_wait);
2833                 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2834                         -EAGAIN : -ERESTARTSYS);
2835                 goto cleanup;
2836         }
2837         
2838         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2839
2840         spin_lock_irqsave(&info->netlock, flags);
2841         if (info->netcount) {
2842                 retval = -EBUSY;
2843                 spin_unlock_irqrestore(&info->netlock, flags);
2844                 goto cleanup;
2845         }
2846         info->count++;
2847         spin_unlock_irqrestore(&info->netlock, flags);
2848
2849         if (info->count == 1) {
2850                 /* 1st open on this device, init hardware */
2851                 retval = startup(info);
2852                 if (retval < 0)
2853                         goto cleanup;
2854         }
2855
2856         retval = block_til_ready(tty, filp, info);
2857         if (retval) {
2858                 if (debug_level >= DEBUG_LEVEL_INFO)
2859                         printk("%s(%d):block_til_ready(%s) returned %d\n",
2860                                  __FILE__,__LINE__, info->device_name, retval);
2861                 goto cleanup;
2862         }
2863
2864         if (debug_level >= DEBUG_LEVEL_INFO)
2865                 printk("%s(%d):mgslpc_open(%s) success\n",
2866                          __FILE__,__LINE__, info->device_name);
2867         retval = 0;
2868         
2869 cleanup:                        
2870         if (retval) {
2871                 if (tty->count == 1)
2872                         info->tty = 0; /* tty layer will release tty struct */
2873                 if(info->count)
2874                         info->count--;
2875         }
2876         
2877         return retval;
2878 }
2879
2880 /*
2881  * /proc fs routines....
2882  */
2883
2884 static inline int line_info(char *buf, MGSLPC_INFO *info)
2885 {
2886         char    stat_buf[30];
2887         int     ret;
2888         unsigned long flags;
2889
2890         ret = sprintf(buf, "%s:io:%04X irq:%d",
2891                       info->device_name, info->io_base, info->irq_level);
2892
2893         /* output current serial signal states */
2894         spin_lock_irqsave(&info->lock,flags);
2895         get_signals(info);
2896         spin_unlock_irqrestore(&info->lock,flags);
2897         
2898         stat_buf[0] = 0;
2899         stat_buf[1] = 0;
2900         if (info->serial_signals & SerialSignal_RTS)
2901                 strcat(stat_buf, "|RTS");
2902         if (info->serial_signals & SerialSignal_CTS)
2903                 strcat(stat_buf, "|CTS");
2904         if (info->serial_signals & SerialSignal_DTR)
2905                 strcat(stat_buf, "|DTR");
2906         if (info->serial_signals & SerialSignal_DSR)
2907                 strcat(stat_buf, "|DSR");
2908         if (info->serial_signals & SerialSignal_DCD)
2909                 strcat(stat_buf, "|CD");
2910         if (info->serial_signals & SerialSignal_RI)
2911                 strcat(stat_buf, "|RI");
2912
2913         if (info->params.mode == MGSL_MODE_HDLC) {
2914                 ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
2915                               info->icount.txok, info->icount.rxok);
2916                 if (info->icount.txunder)
2917                         ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
2918                 if (info->icount.txabort)
2919                         ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
2920                 if (info->icount.rxshort)
2921                         ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);   
2922                 if (info->icount.rxlong)
2923                         ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
2924                 if (info->icount.rxover)
2925                         ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
2926                 if (info->icount.rxcrc)
2927                         ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxcrc);
2928         } else {
2929                 ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
2930                               info->icount.tx, info->icount.rx);
2931                 if (info->icount.frame)
2932                         ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2933                 if (info->icount.parity)
2934                         ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2935                 if (info->icount.brk)
2936                         ret += sprintf(buf+ret, " brk:%d", info->icount.brk);   
2937                 if (info->icount.overrun)
2938                         ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2939         }
2940         
2941         /* Append serial signal status to end */
2942         ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2943         
2944         ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2945                        info->tx_active,info->bh_requested,info->bh_running,
2946                        info->pending_bh);
2947         
2948         return ret;
2949 }
2950
2951 /* Called to print information about devices
2952  */
2953 int mgslpc_read_proc(char *page, char **start, off_t off, int count,
2954                  int *eof, void *data)
2955 {
2956         int len = 0, l;
2957         off_t   begin = 0;
2958         MGSLPC_INFO *info;
2959         
2960         len += sprintf(page, "synclink driver:%s\n", driver_version);
2961         
2962         info = mgslpc_device_list;
2963         while( info ) {
2964                 l = line_info(page + len, info);
2965                 len += l;
2966                 if (len+begin > off+count)
2967                         goto done;
2968                 if (len+begin < off) {
2969                         begin += len;
2970                         len = 0;
2971                 }
2972                 info = info->next_device;
2973         }
2974
2975         *eof = 1;
2976 done:
2977         if (off >= len+begin)
2978                 return 0;
2979         *start = page + (off-begin);
2980         return ((count < begin+len-off) ? count : begin+len-off);
2981 }
2982
2983 int rx_alloc_buffers(MGSLPC_INFO *info)
2984 {
2985         /* each buffer has header and data */
2986         info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2987
2988         /* calculate total allocation size for 8 buffers */
2989         info->rx_buf_total_size = info->rx_buf_size * 8;
2990
2991         /* limit total allocated memory */
2992         if (info->rx_buf_total_size > 0x10000)
2993                 info->rx_buf_total_size = 0x10000;
2994
2995         /* calculate number of buffers */
2996         info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2997
2998         info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2999         if (info->rx_buf == NULL)
3000                 return -ENOMEM;
3001
3002         rx_reset_buffers(info);
3003         return 0;
3004 }
3005
3006 void rx_free_buffers(MGSLPC_INFO *info)
3007 {
3008         if (info->rx_buf)
3009                 kfree(info->rx_buf);
3010         info->rx_buf = NULL;
3011 }
3012
3013 int claim_resources(MGSLPC_INFO *info)
3014 {
3015         if (rx_alloc_buffers(info) < 0 ) {
3016                 printk( "Cant allocate rx buffer %s\n", info->device_name);
3017                 release_resources(info);
3018                 return -ENODEV;
3019         }       
3020         return 0;
3021 }
3022
3023 void release_resources(MGSLPC_INFO *info)
3024 {
3025         if (debug_level >= DEBUG_LEVEL_INFO)
3026                 printk("release_resources(%s)\n", info->device_name);
3027         rx_free_buffers(info);
3028 }
3029
3030 /* Add the specified device instance data structure to the
3031  * global linked list of devices and increment the device count.
3032  *      
3033  * Arguments:           info    pointer to device instance data
3034  */
3035 void mgslpc_add_device(MGSLPC_INFO *info)
3036 {
3037         info->next_device = NULL;
3038         info->line = mgslpc_device_count;
3039         sprintf(info->device_name,"ttySLP%d",info->line);
3040         
3041         if (info->line < MAX_DEVICE_COUNT) {
3042                 if (maxframe[info->line])
3043                         info->max_frame_size = maxframe[info->line];
3044                 info->dosyncppp = dosyncppp[info->line];
3045         }
3046
3047         mgslpc_device_count++;
3048         
3049         if (!mgslpc_device_list)
3050                 mgslpc_device_list = info;
3051         else {  
3052                 MGSLPC_INFO *current_dev = mgslpc_device_list;
3053                 while( current_dev->next_device )
3054                         current_dev = current_dev->next_device;
3055                 current_dev->next_device = info;
3056         }
3057         
3058         if (info->max_frame_size < 4096)
3059                 info->max_frame_size = 4096;
3060         else if (info->max_frame_size > 65535)
3061                 info->max_frame_size = 65535;
3062         
3063         printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
3064                 info->device_name, info->io_base, info->irq_level);
3065
3066
3067 #ifdef CONFIG_SYNCLINK_SYNCPPP
3068 #ifdef MODULE
3069         if (info->dosyncppp)
3070 #endif
3071                 mgslpc_sppp_init(info);
3072 #endif
3073 }
3074
3075 void mgslpc_remove_device(MGSLPC_INFO *remove_info)
3076 {
3077         MGSLPC_INFO *info = mgslpc_device_list;
3078         MGSLPC_INFO *last = NULL;
3079
3080         while(info) {
3081                 if (info == remove_info) {
3082                         if (last)
3083                                 last->next_device = info->next_device;
3084                         else
3085                                 mgslpc_device_list = info->next_device;
3086 #ifdef CONFIG_SYNCLINK_SYNCPPP
3087                         if (info->dosyncppp)
3088                                 mgslpc_sppp_delete(info);
3089 #endif
3090                         release_resources(info);
3091                         kfree(info);
3092                         mgslpc_device_count--;
3093                         return;
3094                 }
3095                 last = info;
3096                 info = info->next_device;
3097         }
3098 }
3099
3100 static struct pcmcia_driver mgslpc_driver = {
3101         .owner          = THIS_MODULE,
3102         .drv            = {
3103                 .name   = "synclink_cs",
3104         },
3105         .attach         = mgslpc_attach,
3106         .detach         = mgslpc_detach,
3107 };
3108
3109 static struct tty_operations mgslpc_ops = {
3110         .open = mgslpc_open,
3111         .close = mgslpc_close,
3112         .write = mgslpc_write,
3113         .put_char = mgslpc_put_char,
3114         .flush_chars = mgslpc_flush_chars,
3115         .write_room = mgslpc_write_room,
3116         .chars_in_buffer = mgslpc_chars_in_buffer,
3117         .flush_buffer = mgslpc_flush_buffer,
3118         .ioctl = mgslpc_ioctl,
3119         .throttle = mgslpc_throttle,
3120         .unthrottle = mgslpc_unthrottle,
3121         .send_xchar = mgslpc_send_xchar,
3122         .break_ctl = mgslpc_break,
3123         .wait_until_sent = mgslpc_wait_until_sent,
3124         .read_proc = mgslpc_read_proc,
3125         .set_termios = mgslpc_set_termios,
3126         .stop = tx_pause,
3127         .start = tx_release,
3128         .hangup = mgslpc_hangup,
3129         .tiocmget = tiocmget,
3130         .tiocmset = tiocmset,
3131 };
3132
3133 static int __init synclink_cs_init(void)
3134 {
3135     int error;
3136
3137     if (break_on_load) {
3138             mgslpc_get_text_ptr();
3139             BREAKPOINT();
3140     }
3141
3142     printk("%s %s\n", driver_name, driver_version);
3143
3144     serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
3145     if (!serial_driver)
3146             return -ENOMEM;
3147
3148     error = pcmcia_register_driver(&mgslpc_driver);
3149     if (error) {
3150             put_tty_driver(serial_driver);
3151             return error;
3152     }
3153
3154     /* Initialize the tty_driver structure */
3155         
3156     serial_driver->owner = THIS_MODULE;
3157     serial_driver->driver_name = "synclink_cs";
3158     serial_driver->name = "ttySLP";
3159     serial_driver->major = ttymajor;
3160     serial_driver->minor_start = 64;
3161     serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3162     serial_driver->subtype = SERIAL_TYPE_NORMAL;
3163     serial_driver->init_termios = tty_std_termios;
3164     serial_driver->init_termios.c_cflag =
3165             B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3166     serial_driver->flags = TTY_DRIVER_REAL_RAW;
3167     tty_set_operations(serial_driver, &mgslpc_ops);
3168
3169     if (tty_register_driver(serial_driver) < 0)
3170             printk("%s(%d):Couldn't register serial driver\n",
3171                    __FILE__,__LINE__);
3172                         
3173     printk("%s %s, tty major#%d\n",
3174            driver_name, driver_version,
3175            serial_driver->major);
3176         
3177     return 0;
3178 }
3179
3180 static void __exit synclink_cs_exit(void) 
3181 {
3182         int rc;
3183
3184         printk("Unloading %s: version %s\n", driver_name, driver_version);
3185
3186         while(mgslpc_device_list)
3187                 mgslpc_remove_device(mgslpc_device_list);
3188
3189         if ((rc = tty_unregister_driver(serial_driver)))
3190                 printk("%s(%d) failed to unregister tty driver err=%d\n",
3191                        __FILE__,__LINE__,rc);
3192         put_tty_driver(serial_driver);
3193
3194         pcmcia_unregister_driver(&mgslpc_driver);
3195
3196         /* XXX: this really needs to move into generic code.. */
3197         while (dev_list != NULL) {
3198                 if (dev_list->state & DEV_CONFIG)
3199                         mgslpc_release((u_long)dev_list);
3200                 mgslpc_detach(dev_list);
3201         }
3202 }
3203
3204 module_init(synclink_cs_init);
3205 module_exit(synclink_cs_exit);
3206
3207 void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate) 
3208 {
3209         unsigned int M, N;
3210         unsigned char val;
3211
3212         /* note:standard BRG mode is broken in V3.2 chip 
3213          * so enhanced mode is always used 
3214          */
3215
3216         if (rate) {
3217                 N = 3686400 / rate;
3218                 if (!N)
3219                         N = 1;
3220                 N >>= 1;
3221                 for (M = 1; N > 64 && M < 16; M++)
3222                         N >>= 1;
3223                 N--;
3224
3225                 /* BGR[5..0] = N
3226                  * BGR[9..6] = M
3227                  * BGR[7..0] contained in BGR register
3228                  * BGR[9..8] contained in CCR2[7..6]
3229                  * divisor = (N+1)*2^M
3230                  *
3231                  * Note: M *must* not be zero (causes asymetric duty cycle)
3232                  */ 
3233                 write_reg(info, (unsigned char) (channel + BGR),
3234                                   (unsigned char) ((M << 6) + N));
3235                 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
3236                 val |= ((M << 4) & 0xc0);
3237                 write_reg(info, (unsigned char) (channel + CCR2), val);
3238         }
3239 }
3240
3241 /* Enabled the AUX clock output at the specified frequency.
3242  */
3243 void enable_auxclk(MGSLPC_INFO *info)
3244 {
3245         unsigned char val;
3246         
3247         /* MODE
3248          *
3249          * 07..06  MDS[1..0] 10 = transparent HDLC mode
3250          * 05      ADM Address Mode, 0 = no addr recognition
3251          * 04      TMD Timer Mode, 0 = external
3252          * 03      RAC Receiver Active, 0 = inactive
3253          * 02      RTS 0=RTS active during xmit, 1=RTS always active
3254          * 01      TRS Timer Resolution, 1=512
3255          * 00      TLP Test Loop, 0 = no loop
3256          *
3257          * 1000 0010
3258          */ 
3259         val = 0x82;
3260         
3261         /* channel B RTS is used to enable AUXCLK driver on SP505 */ 
3262         if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3263                 val |= BIT2;
3264         write_reg(info, CHB + MODE, val);
3265         
3266         /* CCR0
3267          *
3268          * 07      PU Power Up, 1=active, 0=power down
3269          * 06      MCE Master Clock Enable, 1=enabled
3270          * 05      Reserved, 0
3271          * 04..02  SC[2..0] Encoding
3272          * 01..00  SM[1..0] Serial Mode, 00=HDLC
3273          *
3274          * 11000000
3275          */ 
3276         write_reg(info, CHB + CCR0, 0xc0);
3277         
3278         /* CCR1
3279          *
3280          * 07      SFLG Shared Flag, 0 = disable shared flags
3281          * 06      GALP Go Active On Loop, 0 = not used
3282          * 05      GLP Go On Loop, 0 = not used
3283          * 04      ODS Output Driver Select, 1=TxD is push-pull output
3284          * 03      ITF Interframe Time Fill, 0=mark, 1=flag
3285          * 02..00  CM[2..0] Clock Mode
3286          *
3287          * 0001 0111
3288          */ 
3289         write_reg(info, CHB + CCR1, 0x17);
3290         
3291         /* CCR2 (Channel B)
3292          *
3293          * 07..06  BGR[9..8] Baud rate bits 9..8
3294          * 05      BDF Baud rate divisor factor, 0=1, 1=BGR value
3295          * 04      SSEL Clock source select, 1=submode b
3296          * 03      TOE 0=TxCLK is input, 1=TxCLK is output
3297          * 02      RWX Read/Write Exchange 0=disabled
3298          * 01      C32, CRC select, 0=CRC-16, 1=CRC-32
3299          * 00      DIV, data inversion 0=disabled, 1=enabled
3300          *
3301          * 0011 1000
3302          */ 
3303         if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3304                 write_reg(info, CHB + CCR2, 0x38);
3305         else
3306                 write_reg(info, CHB + CCR2, 0x30);
3307         
3308         /* CCR4
3309          *
3310          * 07      MCK4 Master Clock Divide by 4, 1=enabled
3311          * 06      EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3312          * 05      TST1 Test Pin, 0=normal operation
3313          * 04      ICD Ivert Carrier Detect, 1=enabled (active low)
3314          * 03..02  Reserved, must be 0
3315          * 01..00  RFT[1..0] RxFIFO Threshold 00=32 bytes
3316          *
3317          * 0101 0000
3318          */ 
3319         write_reg(info, CHB + CCR4, 0x50);
3320         
3321         /* if auxclk not enabled, set internal BRG so
3322          * CTS transitions can be detected (requires TxC)
3323          */ 
3324         if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3325                 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3326         else
3327                 mgslpc_set_rate(info, CHB, 921600);
3328 }
3329
3330 static void loopback_enable(MGSLPC_INFO *info) 
3331 {
3332         unsigned char val;
3333         
3334         /* CCR1:02..00  CM[2..0] Clock Mode = 111 (clock mode 7) */ 
3335         val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3336         write_reg(info, CHA + CCR1, val);
3337         
3338         /* CCR2:04 SSEL Clock source select, 1=submode b */ 
3339         val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3340         write_reg(info, CHA + CCR2, val);
3341         
3342         /* set LinkSpeed if available, otherwise default to 2Mbps */ 
3343         if (info->params.clock_speed)
3344                 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3345         else
3346                 mgslpc_set_rate(info, CHA, 1843200);
3347         
3348         /* MODE:00 TLP Test Loop, 1=loopback enabled */ 
3349         val = read_reg(info, CHA + MODE) | BIT0;
3350         write_reg(info, CHA + MODE, val);
3351 }
3352
3353 void hdlc_mode(MGSLPC_INFO *info)
3354 {
3355         unsigned char val;
3356         unsigned char clkmode, clksubmode;
3357
3358         /* disable all interrupts */ 
3359         irq_disable(info, CHA, 0xffff);
3360         irq_disable(info, CHB, 0xffff);
3361         port_irq_disable(info, 0xff);
3362         
3363         /* assume clock mode 0a, rcv=RxC xmt=TxC */ 
3364         clkmode = clksubmode = 0;
3365         if (info->params.flags & HDLC_FLAG_RXC_DPLL
3366             && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3367                 /* clock mode 7a, rcv = DPLL, xmt = DPLL */ 
3368                 clkmode = 7;
3369         } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3370                  && info->params.flags & HDLC_FLAG_TXC_BRG) {
3371                 /* clock mode 7b, rcv = BRG, xmt = BRG */ 
3372                 clkmode = 7;
3373                 clksubmode = 1;
3374         } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3375                 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3376                         /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */ 
3377                         clkmode = 6;
3378                         clksubmode = 1;
3379                 } else {
3380                         /* clock mode 6a, rcv = DPLL, xmt = TxC */ 
3381                         clkmode = 6;
3382                 }
3383         } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3384                 /* clock mode 0b, rcv = RxC, xmt = BRG */ 
3385                 clksubmode = 1;
3386         }
3387         
3388         /* MODE
3389          *
3390          * 07..06  MDS[1..0] 10 = transparent HDLC mode
3391          * 05      ADM Address Mode, 0 = no addr recognition
3392          * 04      TMD Timer Mode, 0 = external
3393          * 03      RAC Receiver Active, 0 = inactive
3394          * 02      RTS 0=RTS active during xmit, 1=RTS always active
3395          * 01      TRS Timer Resolution, 1=512
3396          * 00      TLP Test Loop, 0 = no loop
3397          *
3398          * 1000 0010
3399          */ 
3400         val = 0x82;
3401         if (info->params.loopback)
3402                 val |= BIT0;
3403         
3404         /* preserve RTS state */ 
3405         if (info->serial_signals & SerialSignal_RTS)
3406                 val |= BIT2;
3407         write_reg(info, CHA + MODE, val);
3408         
3409         /* CCR0
3410          *
3411          * 07      PU Power Up, 1=active, 0=power down
3412          * 06      MCE Master Clock Enable, 1=enabled
3413          * 05      Reserved, 0
3414          * 04..02  SC[2..0] Encoding
3415          * 01..00  SM[1..0] Serial Mode, 00=HDLC
3416          *
3417          * 11000000
3418          */ 
3419         val = 0xc0;
3420         switch (info->params.encoding)
3421         {
3422         case HDLC_ENCODING_NRZI:
3423                 val |= BIT3;
3424                 break;
3425         case HDLC_ENCODING_BIPHASE_SPACE:
3426                 val |= BIT4;
3427                 break;          // FM0
3428         case HDLC_ENCODING_BIPHASE_MARK:
3429                 val |= BIT4 + BIT2;
3430                 break;          // FM1
3431         case HDLC_ENCODING_BIPHASE_LEVEL:
3432                 val |= BIT4 + BIT3;
3433                 break;          // Manchester
3434         }
3435         write_reg(info, CHA + CCR0, val);
3436         
3437         /* CCR1
3438          *
3439          * 07      SFLG Shared Flag, 0 = disable shared flags
3440          * 06      GALP Go Active On Loop, 0 = not used
3441          * 05      GLP Go On Loop, 0 = not used
3442          * 04      ODS Output Driver Select, 1=TxD is push-pull output
3443          * 03      ITF Interframe Time Fill, 0=mark, 1=flag
3444          * 02..00  CM[2..0] Clock Mode
3445          *
3446          * 0001 0000
3447          */ 
3448         val = 0x10 + clkmode;
3449         write_reg(info, CHA + CCR1, val);
3450         
3451         /* CCR2
3452          *
3453          * 07..06  BGR[9..8] Baud rate bits 9..8
3454          * 05      BDF Baud rate divisor factor, 0=1, 1=BGR value
3455          * 04      SSEL Clock source select, 1=submode b
3456          * 03      TOE 0=TxCLK is input, 0=TxCLK is input
3457          * 02      RWX Read/Write Exchange 0=disabled
3458          * 01      C32, CRC select, 0=CRC-16, 1=CRC-32
3459          * 00      DIV, data inversion 0=disabled, 1=enabled
3460          *
3461          * 0000 0000
3462          */ 
3463         val = 0x00;
3464         if (clkmode == 2 || clkmode == 3 || clkmode == 6
3465             || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3466                 val |= BIT5;
3467         if (clksubmode)
3468                 val |= BIT4;
3469         if (info->params.crc_type == HDLC_CRC_32_CCITT)
3470                 val |= BIT1;
3471         if (info->params.encoding == HDLC_ENCODING_NRZB)
3472                 val |= BIT0;
3473         write_reg(info, CHA + CCR2, val);
3474         
3475         /* CCR3
3476          *
3477          * 07..06  PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3478          * 05      EPT Enable preamble transmission, 1=enabled
3479          * 04      RADD Receive address pushed to FIFO, 0=disabled
3480          * 03      CRL CRC Reset Level, 0=FFFF
3481          * 02      RCRC Rx CRC 0=On 1=Off
3482          * 01      TCRC Tx CRC 0=On 1=Off
3483          * 00      PSD DPLL Phase Shift Disable
3484          *
3485          * 0000 0000
3486          */ 
3487         val = 0x00;
3488         if (info->params.crc_type == HDLC_CRC_NONE)
3489                 val |= BIT2 + BIT1;
3490         if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3491                 val |= BIT5;
3492         switch (info->params.preamble_length)
3493         {
3494         case HDLC_PREAMBLE_LENGTH_16BITS:
3495                 val |= BIT6;
3496                 break;
3497         case HDLC_PREAMBLE_LENGTH_32BITS:
3498                 val |= BIT6;
3499                 break;
3500         case HDLC_PREAMBLE_LENGTH_64BITS:
3501                 val |= BIT7 + BIT6;
3502                 break;
3503         }
3504         write_reg(info, CHA + CCR3, val);
3505         
3506         /* PRE - Preamble pattern */ 
3507         val = 0;
3508         switch (info->params.preamble)
3509         {
3510         case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3511         case HDLC_PREAMBLE_PATTERN_10:    val = 0xaa; break;
3512         case HDLC_PREAMBLE_PATTERN_01:    val = 0x55; break;
3513         case HDLC_PREAMBLE_PATTERN_ONES:  val = 0xff; break;
3514         }
3515         write_reg(info, CHA + PRE, val);
3516         
3517         /* CCR4
3518          *
3519          * 07      MCK4 Master Clock Divide by 4, 1=enabled
3520          * 06      EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3521          * 05      TST1 Test Pin, 0=normal operation
3522          * 04      ICD Ivert Carrier Detect, 1=enabled (active low)
3523          * 03..02  Reserved, must be 0
3524          * 01..00  RFT[1..0] RxFIFO Threshold 00=32 bytes
3525          *
3526          * 0101 0000
3527          */ 
3528         val = 0x50;
3529         write_reg(info, CHA + CCR4, val);
3530         if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3531                 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3532         else
3533                 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3534         
3535         /* RLCR Receive length check register
3536          *
3537          * 7     1=enable receive length check
3538          * 6..0  Max frame length = (RL + 1) * 32
3539          */ 
3540         write_reg(info, CHA + RLCR, 0);
3541         
3542         /* XBCH Transmit Byte Count High
3543          *
3544          * 07      DMA mode, 0 = interrupt driven
3545          * 06      NRM, 0=ABM (ignored)
3546          * 05      CAS Carrier Auto Start
3547          * 04      XC Transmit Continuously (ignored)
3548          * 03..00  XBC[10..8] Transmit byte count bits 10..8
3549          *
3550          * 0000 0000
3551          */ 
3552         val = 0x00;
3553         if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3554                 val |= BIT5;
3555         write_reg(info, CHA + XBCH, val);
3556         enable_auxclk(info);
3557         if (info->params.loopback || info->testing_irq)
3558                 loopback_enable(info);
3559         if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3560         {
3561                 irq_enable(info, CHB, IRQ_CTS);
3562                 /* PVR[3] 1=AUTO CTS active */ 
3563                 set_reg_bits(info, CHA + PVR, BIT3);
3564         } else
3565                 clear_reg_bits(info, CHA + PVR, BIT3);
3566
3567         irq_enable(info, CHA,
3568                          IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3569                          IRQ_UNDERRUN + IRQ_TXFIFO);
3570         issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3571         wait_command_complete(info, CHA);
3572         read_reg16(info, CHA + ISR);    /* clear pending IRQs */
3573         
3574         /* Master clock mode enabled above to allow reset commands
3575          * to complete even if no data clocks are present.
3576          *
3577          * Disable master clock mode for normal communications because
3578          * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3579          * IRQ when in master clock mode.
3580          *
3581          * Leave master clock mode enabled for IRQ test because the
3582          * timer IRQ used by the test can only happen in master clock mode.
3583          */ 
3584         if (!info->testing_irq)
3585                 clear_reg_bits(info, CHA + CCR0, BIT6);
3586
3587         tx_set_idle(info);
3588
3589         tx_stop(info);
3590         rx_stop(info);
3591 }
3592
3593 void rx_stop(MGSLPC_INFO *info)
3594 {
3595         if (debug_level >= DEBUG_LEVEL_ISR)
3596                 printk("%s(%d):rx_stop(%s)\n",
3597                          __FILE__,__LINE__, info->device_name );
3598                          
3599         /* MODE:03 RAC Receiver Active, 0=inactive */ 
3600         clear_reg_bits(info, CHA + MODE, BIT3);
3601
3602         info->rx_enabled = 0;
3603         info->rx_overflow = 0;
3604 }
3605
3606 void rx_start(MGSLPC_INFO *info)
3607 {
3608         if (debug_level >= DEBUG_LEVEL_ISR)
3609                 printk("%s(%d):rx_start(%s)\n",
3610                          __FILE__,__LINE__, info->device_name );
3611
3612         rx_reset_buffers(info);
3613         info->rx_enabled = 0;
3614         info->rx_overflow = 0;
3615
3616         /* MODE:03 RAC Receiver Active, 1=active */ 
3617         set_reg_bits(info, CHA + MODE, BIT3);
3618
3619         info->rx_enabled = 1;
3620 }
3621
3622 void tx_start(MGSLPC_INFO *info)
3623 {
3624         if (debug_level >= DEBUG_LEVEL_ISR)
3625                 printk("%s(%d):tx_start(%s)\n",
3626                          __FILE__,__LINE__, info->device_name );
3627                          
3628         if (info->tx_count) {
3629                 /* If auto RTS enabled and RTS is inactive, then assert */
3630                 /* RTS and set a flag indicating that the driver should */
3631                 /* negate RTS when the transmission completes. */
3632                 info->drop_rts_on_tx_done = 0;
3633
3634                 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3635                         get_signals(info);
3636                         if (!(info->serial_signals & SerialSignal_RTS)) {
3637                                 info->serial_signals |= SerialSignal_RTS;
3638                                 set_signals(info);
3639                                 info->drop_rts_on_tx_done = 1;
3640                         }
3641                 }
3642
3643                 if (info->params.mode == MGSL_MODE_ASYNC) {
3644                         if (!info->tx_active) {
3645                                 info->tx_active = 1;
3646                                 tx_ready(info);
3647                         }
3648                 } else {
3649                         info->tx_active = 1;
3650                         tx_ready(info);
3651                         info->tx_timer.expires = jiffies + jiffies_from_ms(5000);
3652                         add_timer(&info->tx_timer);     
3653                 }
3654         }
3655
3656         if (!info->tx_enabled)
3657                 info->tx_enabled = 1;
3658 }
3659
3660 void tx_stop(MGSLPC_INFO *info)
3661 {
3662         if (debug_level >= DEBUG_LEVEL_ISR)
3663                 printk("%s(%d):tx_stop(%s)\n",
3664                          __FILE__,__LINE__, info->device_name );
3665                          
3666         del_timer(&info->tx_timer);     
3667
3668         info->tx_enabled = 0;
3669         info->tx_active  = 0;
3670 }
3671
3672 /* Reset the adapter to a known state and prepare it for further use.
3673  */
3674 void reset_device(MGSLPC_INFO *info)
3675 {
3676         /* power up both channels (set BIT7) */ 
3677         write_reg(info, CHA + CCR0, 0x80);
3678         write_reg(info, CHB + CCR0, 0x80);
3679         write_reg(info, CHA + MODE, 0);
3680         write_reg(info, CHB + MODE, 0);
3681         
3682         /* disable all interrupts */ 
3683         irq_disable(info, CHA, 0xffff);
3684         irq_disable(info, CHB, 0xffff);
3685         port_irq_disable(info, 0xff);
3686         
3687         /* PCR Port Configuration Register
3688          *
3689          * 07..04  DEC[3..0] Serial I/F select outputs
3690          * 03      output, 1=AUTO CTS control enabled
3691          * 02      RI Ring Indicator input 0=active
3692          * 01      DSR input 0=active
3693          * 00      DTR output 0=active
3694          *
3695          * 0000 0110
3696          */ 
3697         write_reg(info, PCR, 0x06);
3698         
3699         /* PVR Port Value Register
3700          *
3701          * 07..04  DEC[3..0] Serial I/F select (0000=disabled)
3702          * 03      AUTO CTS output 1=enabled
3703          * 02      RI Ring Indicator input
3704          * 01      DSR input
3705          * 00      DTR output (1=inactive)
3706          *
3707          * 0000 0001
3708          */
3709 //      write_reg(info, PVR, PVR_DTR);
3710         
3711         /* IPC Interrupt Port Configuration
3712          *
3713          * 07      VIS 1=Masked interrupts visible
3714          * 06..05  Reserved, 0
3715          * 04..03  SLA Slave address, 00 ignored
3716          * 02      CASM Cascading Mode, 1=daisy chain
3717          * 01..00  IC[1..0] Interrupt Config, 01=push-pull output, active low
3718          *
3719          * 0000 0101
3720          */ 
3721         write_reg(info, IPC, 0x05);
3722 }
3723
3724 void async_mode(MGSLPC_INFO *info)
3725 {
3726         unsigned char val;
3727
3728         /* disable all interrupts */ 
3729         irq_disable(info, CHA, 0xffff);
3730         irq_disable(info, CHB, 0xffff);
3731         port_irq_disable(info, 0xff);
3732         
3733         /* MODE
3734          *
3735          * 07      Reserved, 0
3736          * 06      FRTS RTS State, 0=active
3737          * 05      FCTS Flow Control on CTS
3738          * 04      FLON Flow Control Enable
3739          * 03      RAC Receiver Active, 0 = inactive
3740          * 02      RTS 0=Auto RTS, 1=manual RTS
3741          * 01      TRS Timer Resolution, 1=512
3742          * 00      TLP Test Loop, 0 = no loop
3743          *
3744          * 0000 0110
3745          */ 
3746         val = 0x06;
3747         if (info->params.loopback)
3748                 val |= BIT0;
3749         
3750         /* preserve RTS state */ 
3751         if (!(info->serial_signals & SerialSignal_RTS))
3752                 val |= BIT6;
3753         write_reg(info, CHA + MODE, val);
3754         
3755         /* CCR0
3756          *
3757          * 07      PU Power Up, 1=active, 0=power down
3758          * 06      MCE Master Clock Enable, 1=enabled
3759          * 05      Reserved, 0
3760          * 04..02  SC[2..0] Encoding, 000=NRZ
3761          * 01..00  SM[1..0] Serial Mode, 11=Async
3762          *
3763          * 1000 0011
3764          */ 
3765         write_reg(info, CHA + CCR0, 0x83);
3766         
3767         /* CCR1
3768          *
3769          * 07..05  Reserved, 0
3770          * 04      ODS Output Driver Select, 1=TxD is push-pull output
3771          * 03      BCR Bit Clock Rate, 1=16x
3772          * 02..00  CM[2..0] Clock Mode, 111=BRG
3773          *
3774          * 0001 1111
3775          */ 
3776         write_reg(info, CHA + CCR1, 0x1f);
3777         
3778         /* CCR2 (channel A)
3779          *
3780          * 07..06  BGR[9..8] Baud rate bits 9..8
3781          * 05      BDF Baud rate divisor factor, 0=1, 1=BGR value
3782          * 04      SSEL Clock source select, 1=submode b
3783          * 03      TOE 0=TxCLK is input, 0=TxCLK is input
3784          * 02      RWX Read/Write Exchange 0=disabled
3785          * 01      Reserved, 0
3786          * 00      DIV, data inversion 0=disabled, 1=enabled
3787          *
3788          * 0001 0000
3789          */ 
3790         write_reg(info, CHA + CCR2, 0x10);
3791         
3792         /* CCR3
3793          *
3794          * 07..01  Reserved, 0
3795          * 00      PSD DPLL Phase Shift Disable
3796          *
3797          * 0000 0000
3798          */ 
3799         write_reg(info, CHA + CCR3, 0);
3800         
3801         /* CCR4
3802          *
3803          * 07      MCK4 Master Clock Divide by 4, 1=enabled
3804          * 06      EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3805          * 05      TST1 Test Pin, 0=normal operation
3806          * 04      ICD Ivert Carrier Detect, 1=enabled (active low)
3807          * 03..00  Reserved, must be 0
3808          *
3809          * 0101 0000
3810          */ 
3811         write_reg(info, CHA + CCR4, 0x50);
3812         mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3813         
3814         /* DAFO Data Format
3815          *
3816          * 07      Reserved, 0
3817          * 06      XBRK transmit break, 0=normal operation
3818          * 05      Stop bits (0=1, 1=2)
3819          * 04..03  PAR[1..0] Parity (01=odd, 10=even)
3820          * 02      PAREN Parity Enable
3821          * 01..00  CHL[1..0] Character Length (00=8, 01=7)
3822          *
3823          */ 
3824         val = 0x00;
3825         if (info->params.data_bits != 8)
3826                 val |= BIT0;    /* 7 bits */
3827         if (info->params.stop_bits != 1)
3828                 val |= BIT5;
3829         if (info->params.parity != ASYNC_PARITY_NONE)
3830         {
3831                 val |= BIT2;    /* Parity enable */
3832                 if (info->params.parity == ASYNC_PARITY_ODD)
3833                         val |= BIT3;
3834                 else
3835                         val |= BIT4;
3836         }
3837         write_reg(info, CHA + DAFO, val);
3838         
3839         /* RFC Rx FIFO Control
3840          *
3841          * 07      Reserved, 0
3842          * 06      DPS, 1=parity bit not stored in data byte
3843          * 05      DXS, 0=all data stored in FIFO (including XON/XOFF)
3844          * 04      RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3845          * 03..02  RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3846          * 01      Reserved, 0
3847          * 00      TCDE Terminate Char Detect Enable, 0=disabled
3848          *
3849          * 0101 1100
3850          */ 
3851         write_reg(info, CHA + RFC, 0x5c);
3852         
3853         /* RLCR Receive length check register
3854          *
3855          * Max frame length = (RL + 1) * 32
3856          */ 
3857         write_reg(info, CHA + RLCR, 0);
3858         
3859         /* XBCH Transmit Byte Count High
3860          *
3861          * 07      DMA mode, 0 = interrupt driven
3862          * 06      NRM, 0=ABM (ignored)
3863          * 05      CAS Carrier Auto Start
3864          * 04      XC Transmit Continuously (ignored)
3865          * 03..00  XBC[10..8] Transmit byte count bits 10..8
3866          *
3867          * 0000 0000
3868          */ 
3869         val = 0x00;
3870         if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3871                 val |= BIT5;
3872         write_reg(info, CHA + XBCH, val);
3873         if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3874                 irq_enable(info, CHA, IRQ_CTS);
3875         
3876         /* MODE:03 RAC Receiver Active, 1=active */ 
3877         set_reg_bits(info, CHA + MODE, BIT3);
3878         enable_auxclk(info);
3879         if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3880                 irq_enable(info, CHB, IRQ_CTS);
3881                 /* PVR[3] 1=AUTO CTS active */ 
3882                 set_reg_bits(info, CHA + PVR, BIT3);
3883         } else
3884                 clear_reg_bits(info, CHA + PVR, BIT3);
3885         irq_enable(info, CHA,
3886                           IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3887                           IRQ_ALLSENT + IRQ_TXFIFO);
3888         issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3889         wait_command_complete(info, CHA);
3890         read_reg16(info, CHA + ISR);    /* clear pending IRQs */
3891 }
3892
3893 /* Set the HDLC idle mode for the transmitter.
3894  */
3895 void tx_set_idle(MGSLPC_INFO *info)
3896 {
3897         /* Note: ESCC2 only supports flags and one idle modes */ 
3898         if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3899                 set_reg_bits(info, CHA + CCR1, BIT3);
3900         else
3901                 clear_reg_bits(info, CHA + CCR1, BIT3);
3902 }
3903
3904 /* get state of the V24 status (input) signals.
3905  */
3906 void get_signals(MGSLPC_INFO *info)
3907 {
3908         unsigned char status = 0;
3909         
3910         /* preserve DTR and RTS */ 
3911         info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3912
3913         if (read_reg(info, CHB + VSTR) & BIT7)
3914                 info->serial_signals |= SerialSignal_DCD;
3915         if (read_reg(info, CHB + STAR) & BIT1)
3916                 info->serial_signals |= SerialSignal_CTS;
3917
3918         status = read_reg(info, CHA + PVR);
3919         if (!(status & PVR_RI))
3920                 info->serial_signals |= SerialSignal_RI;
3921         if (!(status & PVR_DSR))
3922                 info->serial_signals |= SerialSignal_DSR;
3923 }
3924
3925 /* Set the state of DTR and RTS based on contents of
3926  * serial_signals member of device extension.
3927  */
3928 void set_signals(MGSLPC_INFO *info)
3929 {
3930         unsigned char val;
3931
3932         val = read_reg(info, CHA + MODE);
3933         if (info->params.mode == MGSL_MODE_ASYNC) {
3934                 if (info->serial_signals & SerialSignal_RTS)
3935                         val &= ~BIT6;
3936                 else
3937                         val |= BIT6;
3938         } else {
3939                 if (info->serial_signals & SerialSignal_RTS)
3940                         val |= BIT2;
3941                 else
3942                         val &= ~BIT2;
3943         }
3944         write_reg(info, CHA + MODE, val);
3945
3946         if (info->serial_signals & SerialSignal_DTR)
3947                 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3948         else
3949                 set_reg_bits(info, CHA + PVR, PVR_DTR);
3950 }
3951
3952 void rx_reset_buffers(MGSLPC_INFO *info)
3953 {
3954         RXBUF *buf;
3955         int i;
3956
3957         info->rx_put = 0;
3958         info->rx_get = 0;
3959         info->rx_frame_count = 0;
3960         for (i=0 ; i < info->rx_buf_count ; i++) {
3961                 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3962                 buf->status = buf->count = 0;
3963         }
3964 }
3965
3966 /* Attempt to return a received HDLC frame
3967  * Only frames received without errors are returned.
3968  *
3969  * Returns 1 if frame returned, otherwise 0
3970  */
3971 int rx_get_frame(MGSLPC_INFO *info)
3972 {
3973         unsigned short status;
3974         RXBUF *buf;
3975         unsigned int framesize = 0;
3976         unsigned long flags;
3977         struct tty_struct *tty = info->tty;
3978         int return_frame = 0;
3979         
3980         if (info->rx_frame_count == 0)
3981                 return 0;
3982
3983         buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3984
3985         status = buf->status;
3986
3987         /* 07  VFR  1=valid frame
3988          * 06  RDO  1=data overrun
3989          * 05  CRC  1=OK, 0=error
3990          * 04  RAB  1=frame aborted
3991          */
3992         if ((status & 0xf0) != 0xA0) {
3993                 if (!(status & BIT7) || (status & BIT4))
3994                         info->icount.rxabort++;
3995                 else if (status & BIT6)
3996                         info->icount.rxover++;
3997                 else if (!(status & BIT5)) {
3998                         info->icount.rxcrc++;
3999                         if (info->params.crc_type & HDLC_CRC_RETURN_EX)
4000                                 return_frame = 1;
4001                 }
4002                 framesize = 0;
4003 #ifdef CONFIG_SYNCLINK_SYNCPPP
4004                 info->netstats.rx_errors++;
4005                 info->netstats.rx_frame_errors++;
4006 #endif
4007         } else
4008                 return_frame = 1;
4009
4010         if (return_frame)
4011                 framesize = buf->count;
4012
4013         if (debug_level >= DEBUG_LEVEL_BH)
4014                 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
4015                         __FILE__,__LINE__,info->device_name,status,framesize);
4016                         
4017         if (debug_level >= DEBUG_LEVEL_DATA)
4018                 trace_block(info, buf->data, framesize, 0);     
4019                 
4020         if (framesize) {
4021                 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
4022                       framesize+1 > info->max_frame_size) ||
4023                     framesize > info->max_frame_size)
4024                         info->icount.rxlong++;
4025                 else {
4026                         if (status & BIT5)
4027                                 info->icount.rxok++;
4028
4029                         if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4030                                 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
4031                                 ++framesize;
4032                         }
4033
4034 #ifdef CONFIG_SYNCLINK_SYNCPPP
4035                         if (info->netcount) {
4036                                 /* pass frame to syncppp device */
4037                                 mgslpc_sppp_rx_done(info, buf->data, framesize);
4038                         } 
4039                         else
4040 #endif
4041                         {
4042                                 /* Call the line discipline receive callback directly. */
4043                                 if (tty && tty->ldisc.receive_buf)
4044                                         tty->ldisc.receive_buf(tty, buf->data, info->flag_buf, framesize);
4045                         }
4046                 }
4047         }
4048
4049         spin_lock_irqsave(&info->lock,flags);
4050         buf->status = buf->count = 0;
4051         info->rx_frame_count--;
4052         info->rx_get++;
4053         if (info->rx_get >= info->rx_buf_count)
4054                 info->rx_get = 0;
4055         spin_unlock_irqrestore(&info->lock,flags);
4056
4057         return 1;
4058 }
4059
4060 BOOLEAN register_test(MGSLPC_INFO *info)
4061 {
4062         static unsigned char patterns[] = 
4063             { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
4064         static unsigned int count = sizeof(patterns) / sizeof(patterns[0]);
4065         unsigned int i;
4066         BOOLEAN rc = TRUE;
4067         unsigned long flags;
4068
4069         spin_lock_irqsave(&info->lock,flags);
4070         reset_device(info);
4071
4072         for (i = 0; i < count; i++) {
4073                 write_reg(info, XAD1, patterns[i]);
4074                 write_reg(info, XAD2, patterns[(i + 1) % count]);
4075                 if ((read_reg(info, XAD1) != patterns[i]) || 
4076                     (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
4077                         rc = FALSE;
4078                         break;
4079                 }
4080         }
4081
4082         spin_unlock_irqrestore(&info->lock,flags);
4083         return rc;
4084 }
4085
4086 BOOLEAN irq_test(MGSLPC_INFO *info)
4087 {
4088         unsigned long end_time;
4089         unsigned long flags;
4090
4091         spin_lock_irqsave(&info->lock,flags);
4092         reset_device(info);
4093
4094         info->testing_irq = TRUE;
4095         hdlc_mode(info);
4096
4097         info->irq_occurred = FALSE;
4098
4099         /* init hdlc mode */
4100
4101         irq_enable(info, CHA, IRQ_TIMER);
4102         write_reg(info, CHA + TIMR, 0); /* 512 cycles */
4103         issue_command(info, CHA, CMD_START_TIMER);
4104
4105         spin_unlock_irqrestore(&info->lock,flags);
4106
4107         end_time=100;
4108         while(end_time-- && !info->irq_occurred) {
4109                 set_current_state(TASK_INTERRUPTIBLE);
4110                 schedule_timeout(jiffies_from_ms(10));
4111         }
4112         
4113         info->testing_irq = FALSE;
4114
4115         spin_lock_irqsave(&info->lock,flags);
4116         reset_device(info);
4117         spin_unlock_irqrestore(&info->lock,flags);
4118         
4119         return info->irq_occurred ? TRUE : FALSE;
4120 }
4121
4122 int adapter_test(MGSLPC_INFO *info)
4123 {
4124         if (!register_test(info)) {
4125                 info->init_error = DiagStatus_AddressFailure;
4126                 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
4127                         __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
4128                 return -ENODEV;
4129         }
4130
4131         if (!irq_test(info)) {
4132                 info->init_error = DiagStatus_IrqFailure;
4133                 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
4134                         __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
4135                 return -ENODEV;
4136         }
4137
4138         if (debug_level >= DEBUG_LEVEL_INFO)
4139                 printk("%s(%d):device %s passed diagnostics\n",
4140                         __FILE__,__LINE__,info->device_name);
4141         return 0;
4142 }
4143
4144 void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
4145 {
4146         int i;
4147         int linecount;
4148         if (xmit)
4149                 printk("%s tx data:\n",info->device_name);
4150         else
4151                 printk("%s rx data:\n",info->device_name);
4152                 
4153         while(count) {
4154                 if (count > 16)
4155                         linecount = 16;
4156                 else
4157                         linecount = count;
4158                         
4159                 for(i=0;i<linecount;i++)
4160                         printk("%02X ",(unsigned char)data[i]);
4161                 for(;i<17;i++)
4162                         printk("   ");
4163                 for(i=0;i<linecount;i++) {
4164                         if (data[i]>=040 && data[i]<=0176)
4165                                 printk("%c",data[i]);
4166                         else
4167                                 printk(".");
4168                 }
4169                 printk("\n");
4170                 
4171                 data  += linecount;
4172                 count -= linecount;
4173         }
4174 }
4175
4176 /* HDLC frame time out
4177  * update stats and do tx completion processing
4178  */
4179 void tx_timeout(unsigned long context)
4180 {
4181         MGSLPC_INFO *info = (MGSLPC_INFO*)context;
4182         unsigned long flags;
4183         
4184         if ( debug_level >= DEBUG_LEVEL_INFO )
4185                 printk( "%s(%d):tx_timeout(%s)\n",
4186                         __FILE__,__LINE__,info->device_name);
4187         if(info->tx_active &&
4188            info->params.mode == MGSL_MODE_HDLC) {
4189                 info->icount.txtimeout++;
4190         }
4191         spin_lock_irqsave(&info->lock,flags);
4192         info->tx_active = 0;
4193         info->tx_count = info->tx_put = info->tx_get = 0;
4194
4195         spin_unlock_irqrestore(&info->lock,flags);
4196         
4197 #ifdef CONFIG_SYNCLINK_SYNCPPP
4198         if (info->netcount)
4199                 mgslpc_sppp_tx_done(info);
4200         else
4201 #endif
4202                 bh_transmit(info);
4203 }
4204
4205 #ifdef CONFIG_SYNCLINK_SYNCPPP
4206 /* syncppp net device routines
4207  */
4208  
4209 static void mgslpc_setup(struct net_device *dev)
4210 {
4211         dev->open = mgslpc_sppp_open;
4212         dev->stop = mgslpc_sppp_close;
4213         dev->hard_start_xmit = mgslpc_sppp_tx;
4214         dev->do_ioctl = mgslpc_sppp_ioctl;
4215         dev->get_stats = mgslpc_net_stats;
4216         dev->tx_timeout = mgslpc_sppp_tx_timeout;
4217         dev->watchdog_timeo = 10*HZ;
4218 }
4219
4220 void mgslpc_sppp_init(MGSLPC_INFO *info)
4221 {
4222         struct net_device *d;
4223
4224         sprintf(info->netname,"mgslp%d",info->line);
4225  
4226         d = alloc_netdev(0, info->netname, mgslpc_setup);
4227         if (!d) {
4228                 printk(KERN_WARNING "%s: alloc_netdev failed.\n",
4229                                                 info->netname);
4230                 return;
4231         }
4232
4233         info->if_ptr = &info->pppdev;
4234         info->netdev = info->pppdev.dev = d;
4235
4236         d->base_addr = info->io_base;
4237         d->irq = info->irq_level;
4238         d->priv = info;
4239
4240         sppp_attach(&info->pppdev);
4241         mgslpc_setup(d);
4242
4243         if (register_netdev(d)) {
4244                 printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
4245                 sppp_detach(info->netdev);
4246                 info->netdev = NULL;
4247                 info->pppdev.dev = NULL;
4248                 free_netdev(d);
4249                 return;
4250         }
4251
4252         if (debug_level >= DEBUG_LEVEL_INFO)
4253                 printk("mgslpc_sppp_init()\n"); 
4254 }
4255
4256 void mgslpc_sppp_delete(MGSLPC_INFO *info)
4257 {
4258         if (debug_level >= DEBUG_LEVEL_INFO)
4259                 printk("mgslpc_sppp_delete(%s)\n",info->netname);       
4260         unregister_netdev(info->netdev);
4261         sppp_detach(info->netdev);
4262         free_netdev(info->netdev);
4263         info->netdev = NULL;
4264         info->pppdev.dev = NULL;
4265 }
4266
4267 int mgslpc_sppp_open(struct net_device *d)
4268 {
4269         MGSLPC_INFO *info = d->priv;
4270         int err;
4271         unsigned long flags;
4272
4273         if (debug_level >= DEBUG_LEVEL_INFO)
4274                 printk("mgslpc_sppp_open(%s)\n",info->netname); 
4275
4276         spin_lock_irqsave(&info->netlock, flags);
4277         if (info->count != 0 || info->netcount != 0) {
4278                 printk(KERN_WARNING "%s: sppp_open returning busy\n", info->netname);
4279                 spin_unlock_irqrestore(&info->netlock, flags);
4280                 return -EBUSY;
4281         }
4282         info->netcount=1;
4283         spin_unlock_irqrestore(&info->netlock, flags);
4284
4285         /* claim resources and init adapter */
4286         if ((err = startup(info)) != 0)
4287                 goto open_fail;
4288
4289         /* allow syncppp module to do open processing */
4290         if ((err = sppp_open(d)) != 0) {
4291                 shutdown(info);
4292                 goto open_fail;
4293         }
4294
4295         info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4296         mgslpc_program_hw(info);
4297
4298         d->trans_start = jiffies;
4299         netif_start_queue(d);
4300         return 0;
4301
4302 open_fail:
4303         spin_lock_irqsave(&info->netlock, flags);
4304         info->netcount=0;
4305         spin_unlock_irqrestore(&info->netlock, flags);
4306         return err;
4307 }
4308
4309 void mgslpc_sppp_tx_timeout(struct net_device *dev)
4310 {
4311         MGSLPC_INFO *info = dev->priv;
4312         unsigned long flags;
4313
4314         if (debug_level >= DEBUG_LEVEL_INFO)
4315                 printk("mgslpc_sppp_tx_timeout(%s)\n",info->netname);   
4316
4317         info->netstats.tx_errors++;
4318         info->netstats.tx_aborted_errors++;
4319
4320         spin_lock_irqsave(&info->lock,flags);
4321         tx_stop(info);
4322         spin_unlock_irqrestore(&info->lock,flags);
4323
4324         netif_wake_queue(dev);
4325 }
4326
4327 int mgslpc_sppp_tx(struct sk_buff *skb, struct net_device *dev)
4328 {
4329         MGSLPC_INFO *info = dev->priv;
4330         unsigned long flags;
4331
4332         if (debug_level >= DEBUG_LEVEL_INFO)
4333                 printk("mgslpc_sppp_tx(%s)\n",info->netname);   
4334
4335         netif_stop_queue(dev);
4336
4337         info->tx_count = skb->len;
4338
4339         memcpy(info->tx_buf, skb->data, skb->len);
4340         info->tx_get = 0;
4341         info->tx_put = info->tx_count = skb->len;
4342
4343         info->netstats.tx_packets++;
4344         info->netstats.tx_bytes += skb->len;
4345         dev_kfree_skb(skb);
4346
4347         dev->trans_start = jiffies;
4348
4349         spin_lock_irqsave(&info->lock,flags);
4350         if (!info->tx_active)
4351                 tx_start(info);
4352         spin_unlock_irqrestore(&info->lock,flags);
4353
4354         return 0;
4355 }
4356
4357 int mgslpc_sppp_close(struct net_device *d)
4358 {
4359         MGSLPC_INFO *info = d->priv;
4360         unsigned long flags;
4361
4362         if (debug_level >= DEBUG_LEVEL_INFO)
4363                 printk("mgslpc_sppp_close(%s)\n",info->netname);        
4364
4365         /* shutdown adapter and release resources */
4366         shutdown(info);
4367
4368         /* allow syncppp to do close processing */
4369         sppp_close(d);
4370         netif_stop_queue(d);
4371
4372         spin_lock_irqsave(&info->netlock, flags);
4373         info->netcount=0;
4374         spin_unlock_irqrestore(&info->netlock, flags);
4375         return 0;
4376 }
4377
4378 void mgslpc_sppp_rx_done(MGSLPC_INFO *info, char *buf, int size)
4379 {
4380         struct sk_buff *skb = dev_alloc_skb(size);
4381         if (debug_level >= DEBUG_LEVEL_INFO)
4382                 printk("mgslpc_sppp_rx_done(%s)\n",info->netname);      
4383         if (skb == NULL) {
4384                 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n",
4385                         info->netname);
4386                 info->netstats.rx_dropped++;
4387                 return;
4388         }
4389
4390         memcpy(skb_put(skb, size),buf,size);
4391
4392         skb->protocol = htons(ETH_P_WAN_PPP);
4393         skb->dev = info->netdev;
4394         skb->mac.raw = skb->data;
4395         info->netstats.rx_packets++;
4396         info->netstats.rx_bytes += size;
4397         netif_rx(skb);
4398         info->netdev->trans_start = jiffies;
4399 }
4400
4401 void mgslpc_sppp_tx_done(MGSLPC_INFO *info)
4402 {
4403         if (netif_queue_stopped(info->netdev))
4404             netif_wake_queue(info->netdev);
4405 }
4406
4407 struct net_device_stats *mgslpc_net_stats(struct net_device *dev)
4408 {
4409         MGSLPC_INFO *info = dev->priv;
4410         if (debug_level >= DEBUG_LEVEL_INFO)
4411                 printk("mgslpc_net_stats(%s)\n",info->netname); 
4412         return &info->netstats;
4413 }
4414
4415 int mgslpc_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4416 {
4417         MGSLPC_INFO *info = dev->priv;
4418         if (debug_level >= DEBUG_LEVEL_INFO)
4419                 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
4420                         info->netname, cmd );
4421         return sppp_do_ioctl(dev, ifr, cmd);
4422 }
4423
4424 #endif /* ifdef CONFIG_SYNCLINK_SYNCPPP */