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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [pcmcia/] [synclink_cs.c] - Blame information for rev 1275

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

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

powered by: WebSVN 2.1.0

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