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