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