synclink_cs.c 110 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*
 * linux/drivers/char/pcmcia/synclink_cs.c
 *
4
 * $Id: synclink_cs.c,v 4.34 2005/09/08 13:20:54 paulkf Exp $
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 *
 * Device driver for Microgate SyncLink PC Card
 * multiprotocol serial adapter.
 *
 * written by Paul Fulghum for Microgate Corporation
 * paulkf@microgate.com
 *
 * Microgate and SyncLink are trademarks of Microgate Corporation
 *
 * This code is released under the GNU General Public License (GPL)
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
#if defined(__i386__)
#  define BREAKPOINT() asm("   int $3");
#else
#  define BREAKPOINT() { }
#endif

#define MAX_DEVICE_COUNT 4

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/time.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/serial.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/mm.h>
54
#include <linux/seq_file.h>
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58 59 60
#include <linux/slab.h>
#include <linux/netdevice.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/ioctl.h>
61
#include <linux/synclink.h>
Linus Torvalds's avatar
Linus Torvalds committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/dma.h>
#include <linux/bitops.h>
#include <asm/types.h>
#include <linux/termios.h>
#include <linux/workqueue.h>
#include <linux/hdlc.h>

#include <pcmcia/cistpl.h>
#include <pcmcia/cisreg.h>
#include <pcmcia/ds.h>

77 78 79 80
#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_CS_MODULE))
#define SYNCLINK_GENERIC_HDLC 1
#else
#define SYNCLINK_GENERIC_HDLC 0
Linus Torvalds's avatar
Linus Torvalds committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#endif

#define GET_USER(error,value,addr) error = get_user(value,addr)
#define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
#define PUT_USER(error,value,addr) error = put_user(value,addr)
#define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0

#include <asm/uaccess.h>

static MGSL_PARAMS default_params = {
	MGSL_MODE_HDLC,			/* unsigned long mode */
	0,				/* unsigned char loopback; */
	HDLC_FLAG_UNDERRUN_ABORT15,	/* unsigned short flags; */
	HDLC_ENCODING_NRZI_SPACE,	/* unsigned char encoding; */
	0,				/* unsigned long clock_speed; */
	0xff,				/* unsigned char addr_filter; */
	HDLC_CRC_16_CCITT,		/* unsigned short crc_type; */
	HDLC_PREAMBLE_LENGTH_8BITS,	/* unsigned char preamble_length; */
	HDLC_PREAMBLE_PATTERN_NONE,	/* unsigned char preamble; */
	9600,				/* unsigned long data_rate; */
	8,				/* unsigned char data_bits; */
	1,				/* unsigned char stop_bits; */
	ASYNC_PARITY_NONE		/* unsigned char parity; */
};

typedef struct
{
	int count;
	unsigned char status;
	char data[1];
} RXBUF;

/* The queue of BH actions to be performed */

#define BH_RECEIVE  1
#define BH_TRANSMIT 2
#define BH_STATUS   4

#define IO_PIN_SHUTDOWN_LIMIT 100

#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))

struct _input_signal_events {
124
	int	ri_up;
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128 129 130 131 132 133 134 135 136 137
	int	ri_down;
	int	dsr_up;
	int	dsr_down;
	int	dcd_up;
	int	dcd_down;
	int	cts_up;
	int	cts_down;
};


/*
 * Device instance data structure
 */
138

Linus Torvalds's avatar
Linus Torvalds committed
139
typedef struct _mgslpc_info {
140
	struct tty_port		port;
Linus Torvalds's avatar
Linus Torvalds committed
141 142 143
	void *if_ptr;	/* General purpose pointer (used by SPPP) */
	int			magic;
	int			line;
144

Linus Torvalds's avatar
Linus Torvalds committed
145
	struct mgsl_icount	icount;
146

Linus Torvalds's avatar
Linus Torvalds committed
147 148 149
	int			timeout;
	int			x_char;		/* xon/xoff character */
	unsigned char		read_status_mask;
150
	unsigned char		ignore_status_mask;
Linus Torvalds's avatar
Linus Torvalds committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

	unsigned char *tx_buf;
	int            tx_put;
	int            tx_get;
	int            tx_count;

	/* circular list of fixed length rx buffers */

	unsigned char  *rx_buf;        /* memory allocated for all rx buffers */
	int            rx_buf_total_size; /* size of memory allocated for rx buffers */
	int            rx_put;         /* index of next empty rx buffer */
	int            rx_get;         /* index of next full rx buffer */
	int            rx_buf_size;    /* size in bytes of single rx buffer */
	int            rx_buf_count;   /* total number of rx buffers */
	int            rx_frame_count; /* number of full rx buffers */
166

Linus Torvalds's avatar
Linus Torvalds committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	wait_queue_head_t	status_event_wait_q;
	wait_queue_head_t	event_wait_q;
	struct timer_list	tx_timer;	/* HDLC transmit timeout timer */
	struct _mgslpc_info	*next_device;	/* device list link */

	unsigned short imra_value;
	unsigned short imrb_value;
	unsigned char  pim_value;

	spinlock_t lock;
	struct work_struct task;		/* task structure for scheduling bh */

	u32 max_frame_size;

	u32 pending_bh;

183 184
	bool bh_running;
	bool bh_requested;
185

Linus Torvalds's avatar
Linus Torvalds committed
186 187 188 189 190
	int dcd_chkcount; /* check counts to prevent */
	int cts_chkcount; /* too many IRQs if a signal */
	int dsr_chkcount; /* is floating */
	int ri_chkcount;

191 192
	bool rx_enabled;
	bool rx_overflow;
Linus Torvalds's avatar
Linus Torvalds committed
193

194 195 196
	bool tx_enabled;
	bool tx_active;
	bool tx_aborting;
Linus Torvalds's avatar
Linus Torvalds committed
197 198 199 200 201 202 203 204
	u32 idle_mode;

	int if_mode; /* serial interface selection (RS-232, v.35 etc) */

	char device_name[25];		/* device instance name */

	unsigned int io_base;	/* base I/O address of adapter */
	unsigned int irq_level;
205

Linus Torvalds's avatar
Linus Torvalds committed
206 207 208 209
	MGSL_PARAMS params;		/* communications parameters */

	unsigned char serial_signals;	/* current serial signal states */

210
	bool irq_occurred;		/* for diagnostics use */
Linus Torvalds's avatar
Linus Torvalds committed
211 212 213 214
	char testing_irq;
	unsigned int init_error;	/* startup error (DIAGS)	*/

	char flag_buf[MAX_ASYNC_BUFFER_SIZE];
215
	bool drop_rts_on_tx_done;
Linus Torvalds's avatar
Linus Torvalds committed
216 217 218 219

	struct	_input_signal_events	input_signal_events;

	/* PCMCIA support */
220
	struct pcmcia_device	*p_dev;
Linus Torvalds's avatar
Linus Torvalds committed
221 222 223 224 225 226
	int		      stop;

	/* SPPP/Cisco HDLC device parts */
	int netcount;
	spinlock_t netlock;

227
#if SYNCLINK_GENERIC_HDLC
Linus Torvalds's avatar
Linus Torvalds committed
228 229 230 231 232 233 234 235 236 237 238 239
	struct net_device *netdev;
#endif

} MGSLPC_INFO;

#define MGSLPC_MAGIC 0x5402

/*
 * The size of the serial xmit buffer is 1 page, or 4096 bytes
 */
#define TXBUFSIZE 4096

240

Linus Torvalds's avatar
Linus Torvalds committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
#define CHA     0x00   /* channel A offset */
#define CHB     0x40   /* channel B offset */

/*
 *  FIXME: PPC has PVR defined in asm/reg.h.  For now we just undef it.
 */
#undef PVR

#define RXFIFO  0
#define TXFIFO  0
#define STAR    0x20
#define CMDR    0x20
#define RSTA    0x21
#define PRE     0x21
#define MODE    0x22
#define TIMR    0x23
#define XAD1    0x24
#define XAD2    0x25
#define RAH1    0x26
#define RAH2    0x27
#define DAFO    0x27
#define RAL1    0x28
#define RFC     0x28
#define RHCR    0x29
#define RAL2    0x29
#define RBCL    0x2a
#define XBCL    0x2a
#define RBCH    0x2b
#define XBCH    0x2b
#define CCR0    0x2c
#define CCR1    0x2d
#define CCR2    0x2e
#define CCR3    0x2f
#define VSTR    0x34
#define BGR     0x34
#define RLCR    0x35
#define AML     0x36
#define AMH     0x37
#define GIS     0x38
#define IVA     0x38
#define IPC     0x39
#define ISR     0x3a
#define IMR     0x3a
#define PVR     0x3c
#define PIS     0x3d
#define PIM     0x3d
#define PCR     0x3e
#define CCR4    0x3f
289

Linus Torvalds's avatar
Linus Torvalds committed
290
// IMR/ISR
291

Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305
#define IRQ_BREAK_ON    BIT15   // rx break detected
#define IRQ_DATAOVERRUN BIT14	// receive data overflow
#define IRQ_ALLSENT     BIT13	// all sent
#define IRQ_UNDERRUN    BIT12	// transmit data underrun
#define IRQ_TIMER       BIT11	// timer interrupt
#define IRQ_CTS         BIT10	// CTS status change
#define IRQ_TXREPEAT    BIT9	// tx message repeat
#define IRQ_TXFIFO      BIT8	// transmit pool ready
#define IRQ_RXEOM       BIT7	// receive message end
#define IRQ_EXITHUNT    BIT6	// receive frame start
#define IRQ_RXTIME      BIT6    // rx char timeout
#define IRQ_DCD         BIT2	// carrier detect status change
#define IRQ_OVERRUN     BIT1	// receive frame overflow
#define IRQ_RXFIFO      BIT0	// receive pool full
306

Linus Torvalds's avatar
Linus Torvalds committed
307
// STAR
308

Linus Torvalds's avatar
Linus Torvalds committed
309 310 311
#define XFW   BIT6		// transmit FIFO write enable
#define CEC   BIT2		// command executing
#define CTS   BIT1		// CTS state
312

Linus Torvalds's avatar
Linus Torvalds committed
313 314 315 316 317 318 319
#define PVR_DTR      BIT0
#define PVR_DSR      BIT1
#define PVR_RI       BIT2
#define PVR_AUTOCTS  BIT3
#define PVR_RS232    0x20   /* 0010b */
#define PVR_V35      0xe0   /* 1110b */
#define PVR_RS422    0x40   /* 0100b */
320 321 322

/* Register access functions */

Linus Torvalds's avatar
Linus Torvalds committed
323 324 325
#define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
#define read_reg(info, reg) inb((info)->io_base + (reg))

326
#define read_reg16(info, reg) inw((info)->io_base + (reg))
Linus Torvalds's avatar
Linus Torvalds committed
327
#define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
328

Linus Torvalds's avatar
Linus Torvalds committed
329 330
#define set_reg_bits(info, reg, mask) \
    write_reg(info, (reg), \
331
		 (unsigned char) (read_reg(info, (reg)) | (mask)))
Linus Torvalds's avatar
Linus Torvalds committed
332 333
#define clear_reg_bits(info, reg, mask) \
    write_reg(info, (reg), \
334
		 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
Linus Torvalds's avatar
Linus Torvalds committed
335 336
/*
 * interrupt enable/disable routines
337 338
 */
static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
Linus Torvalds's avatar
Linus Torvalds committed
339 340 341 342 343 344 345 346 347
{
	if (channel == CHA) {
		info->imra_value |= mask;
		write_reg16(info, CHA + IMR, info->imra_value);
	} else {
		info->imrb_value |= mask;
		write_reg16(info, CHB + IMR, info->imrb_value);
	}
}
348
static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
Linus Torvalds's avatar
Linus Torvalds committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
{
	if (channel == CHA) {
		info->imra_value &= ~mask;
		write_reg16(info, CHA + IMR, info->imra_value);
	} else {
		info->imrb_value &= ~mask;
		write_reg16(info, CHB + IMR, info->imrb_value);
	}
}

#define port_irq_disable(info, mask) \
  { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }

#define port_irq_enable(info, mask) \
  { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }

static void rx_start(MGSLPC_INFO *info);
static void rx_stop(MGSLPC_INFO *info);

368
static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds's avatar
Linus Torvalds committed
369 370 371 372 373 374 375 376 377 378 379 380 381
static void tx_stop(MGSLPC_INFO *info);
static void tx_set_idle(MGSLPC_INFO *info);

static void get_signals(MGSLPC_INFO *info);
static void set_signals(MGSLPC_INFO *info);

static void reset_device(MGSLPC_INFO *info);

static void hdlc_mode(MGSLPC_INFO *info);
static void async_mode(MGSLPC_INFO *info);

static void tx_timeout(unsigned long context);

382
static int carrier_raised(struct tty_port *port);
383
static void dtr_rts(struct tty_port *port, int onoff);
Linus Torvalds's avatar
Linus Torvalds committed
384

385
#if SYNCLINK_GENERIC_HDLC
Linus Torvalds's avatar
Linus Torvalds committed
386 387 388 389 390 391 392 393 394
#define dev_to_port(D) (dev_to_hdlc(D)->priv)
static void hdlcdev_tx_done(MGSLPC_INFO *info);
static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
static int  hdlcdev_init(MGSLPC_INFO *info);
static void hdlcdev_exit(MGSLPC_INFO *info);
#endif

static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);

395 396
static bool register_test(MGSLPC_INFO *info);
static bool irq_test(MGSLPC_INFO *info);
Linus Torvalds's avatar
Linus Torvalds committed
397 398 399 400 401 402 403
static int adapter_test(MGSLPC_INFO *info);

static int claim_resources(MGSLPC_INFO *info);
static void release_resources(MGSLPC_INFO *info);
static void mgslpc_add_device(MGSLPC_INFO *info);
static void mgslpc_remove_device(MGSLPC_INFO *info);

404
static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds's avatar
Linus Torvalds committed
405 406 407 408
static void rx_reset_buffers(MGSLPC_INFO *info);
static int  rx_alloc_buffers(MGSLPC_INFO *info);
static void rx_free_buffers(MGSLPC_INFO *info);

409
static irqreturn_t mgslpc_isr(int irq, void *dev_id);
Linus Torvalds's avatar
Linus Torvalds committed
410 411 412 413

/*
 * Bottom half interrupt handlers
 */
414
static void bh_handler(struct work_struct *work);
415
static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds's avatar
Linus Torvalds committed
416 417 418 419 420 421 422 423 424 425
static void bh_status(MGSLPC_INFO *info);

/*
 * ioctl handlers
 */
static int tiocmget(struct tty_struct *tty, struct file *file);
static int tiocmset(struct tty_struct *tty, struct file *file,
		    unsigned int set, unsigned int clear);
static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
426
static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params, struct tty_struct *tty);
Linus Torvalds's avatar
Linus Torvalds committed
427 428
static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
static int set_txidle(MGSLPC_INFO *info, int idle_mode);
429
static int set_txenable(MGSLPC_INFO *info, int enable, struct tty_struct *tty);
Linus Torvalds's avatar
Linus Torvalds committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
static int tx_abort(MGSLPC_INFO *info);
static int set_rxenable(MGSLPC_INFO *info, int enable);
static int wait_events(MGSLPC_INFO *info, int __user *mask);

static MGSLPC_INFO *mgslpc_device_list = NULL;
static int mgslpc_device_count = 0;

/*
 * Set this param to non-zero to load eax with the
 * .text section address and breakpoint on module load.
 * This is useful for use with gdb and add-symbol-file command.
 */
static int break_on_load=0;

/*
 * Driver major number, defaults to zero to get auto
 * assigned major number. May be forced as module parameter.
 */
static int ttymajor=0;

static int debug_level = 0;
static int maxframe[MAX_DEVICE_COUNT] = {0,};

module_param(break_on_load, bool, 0);
module_param(ttymajor, int, 0);
module_param(debug_level, int, 0);
module_param_array(maxframe, int, NULL, 0);

MODULE_LICENSE("GPL");

static char *driver_name = "SyncLink PC Card driver";
461
static char *driver_version = "$Revision: 4.34 $";
Linus Torvalds's avatar
Linus Torvalds committed
462 463 464 465 466 467

static struct tty_driver *serial_driver;

/* number of characters left in xmit buffer before we ask for more */
#define WAKEUP_CHARS 256

468
static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds's avatar
Linus Torvalds committed
469 470 471 472
static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);

/* PCMCIA prototypes */

473
static int mgslpc_config(struct pcmcia_device *link);
Linus Torvalds's avatar
Linus Torvalds committed
474
static void mgslpc_release(u_long arg);
475
static void mgslpc_detach(struct pcmcia_device *p_dev);
Linus Torvalds's avatar
Linus Torvalds committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

/*
 * 1st function defined in .text section. Calling this function in
 * init_module() followed by a breakpoint allows a remote debugger
 * (gdb) to get the .text address for the add-symbol-file command.
 * This allows remote debugging of dynamically loadable modules.
 */
static void* mgslpc_get_text_ptr(void)
{
	return mgslpc_get_text_ptr;
}

/**
 * line discipline callback wrappers
 *
 * The wrappers maintain line discipline references
 * while calling into the line discipline.
 *
 * ldisc_receive_buf  - pass receive data to line discipline
 */

static void ldisc_receive_buf(struct tty_struct *tty,
			      const __u8 *data, char *flags, int count)
{
	struct tty_ldisc *ld;
	if (!tty)
		return;
	ld = tty_ldisc_ref(tty);
	if (ld) {
Alan Cox's avatar
Alan Cox committed
505 506
		if (ld->ops->receive_buf)
			ld->ops->receive_buf(tty, data, flags, count);
Linus Torvalds's avatar
Linus Torvalds committed
507 508 509 510
		tty_ldisc_deref(ld);
	}
}

511 512
static const struct tty_port_operations mgslpc_port_ops = {
	.carrier_raised = carrier_raised,
513
	.dtr_rts = dtr_rts
514 515
};

516
static int mgslpc_probe(struct pcmcia_device *link)
Linus Torvalds's avatar
Linus Torvalds committed
517 518
{
    MGSLPC_INFO *info;
519
    int ret;
520

Linus Torvalds's avatar
Linus Torvalds committed
521 522
    if (debug_level >= DEBUG_LEVEL_INFO)
	    printk("mgslpc_attach\n");
523

524
    info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
525 526
    if (!info) {
	    printk("Error can't allocate device instance data\n");
527
	    return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
528 529 530
    }

    info->magic = MGSLPC_MAGIC;
531 532
    tty_port_init(&info->port);
    info->port.ops = &mgslpc_port_ops;
533
    INIT_WORK(&info->task, bh_handler);
Linus Torvalds's avatar
Linus Torvalds committed
534
    info->max_frame_size = 4096;
535 536
    info->port.close_delay = 5*HZ/10;
    info->port.closing_wait = 30*HZ;
Linus Torvalds's avatar
Linus Torvalds committed
537 538 539 540 541
    init_waitqueue_head(&info->status_event_wait_q);
    init_waitqueue_head(&info->event_wait_q);
    spin_lock_init(&info->lock);
    spin_lock_init(&info->netlock);
    memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
542
    info->idle_mode = HDLC_TXIDLE_FLAGS;
Linus Torvalds's avatar
Linus Torvalds committed
543 544 545 546
    info->imra_value = 0xffff;
    info->imrb_value = 0xffff;
    info->pim_value = 0xff;

547
    info->p_dev = link;
Linus Torvalds's avatar
Linus Torvalds committed
548
    link->priv = info;
549

550
    /* Initialize the struct pcmcia_device structure */
Linus Torvalds's avatar
Linus Torvalds committed
551

552 553 554
    ret = mgslpc_config(link);
    if (ret)
	    return ret;
Linus Torvalds's avatar
Linus Torvalds committed
555 556 557

    mgslpc_add_device(info);

558
    return 0;
Linus Torvalds's avatar
Linus Torvalds committed
559 560 561 562 563
}

/* Card has been inserted.
 */

564 565 566 567 568 569
static int mgslpc_ioprobe(struct pcmcia_device *p_dev,
			  cistpl_cftable_entry_t *cfg,
			  cistpl_cftable_entry_t *dflt,
			  unsigned int vcc,
			  void *priv_data)
{
570 571 572 573 574 575 576 577 578
	if (!cfg->io.nwin)
		return -ENODEV;

	p_dev->resource[0]->start = cfg->io.win[0].base;
	p_dev->resource[0]->end = cfg->io.win[0].len;
	p_dev->resource[0]->flags |= pcmcia_io_cfg_data_width(cfg->io.flags);
	p_dev->io_lines = cfg->io.flags & CISTPL_IO_LINES_MASK;

	return pcmcia_request_io(p_dev);
579 580
}

581
static int mgslpc_config(struct pcmcia_device *link)
Linus Torvalds's avatar
Linus Torvalds committed
582 583
{
    MGSLPC_INFO *info = link->priv;
584
    int ret;
585

Linus Torvalds's avatar
Linus Torvalds committed
586 587 588
    if (debug_level >= DEBUG_LEVEL_INFO)
	    printk("mgslpc_config(0x%p)\n", link);

589 590 591
    ret = pcmcia_loop_config(link, mgslpc_ioprobe, NULL);
    if (ret != 0)
	    goto failed;
Linus Torvalds's avatar
Linus Torvalds committed
592

593
    link->config_flags |= CONF_ENABLE_IRQ;
594 595
    link->config_index = 8;
    link->config_regs = PRESENT_OPTION;
596

597
    ret = pcmcia_request_irq(link, mgslpc_isr);
598 599
    if (ret)
	    goto failed;
600
    ret = pcmcia_enable_device(link);
601 602
    if (ret)
	    goto failed;
Linus Torvalds's avatar
Linus Torvalds committed
603

604
    info->io_base = link->resource[0]->start;
605
    info->irq_level = link->irq;
Linus Torvalds's avatar
Linus Torvalds committed
606

607
    dev_info(&link->dev, "index 0x%02x:",
608
	    link->config_index);
609
    printk(", irq %d", link->irq);
610 611
    if (link->resource[0])
	    printk(", io %pR", link->resource[0]);
Linus Torvalds's avatar
Linus Torvalds committed
612
    printk("\n");
613
    return 0;
Linus Torvalds's avatar
Linus Torvalds committed
614

615
failed:
Linus Torvalds's avatar
Linus Torvalds committed
616
    mgslpc_release((u_long)link);
617
    return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
618 619 620 621 622 623 624 625
}

/* Card has been removed.
 * Unregister device and release PCMCIA configuration.
 * If device is open, postpone until it is closed.
 */
static void mgslpc_release(u_long arg)
{
626
	struct pcmcia_device *link = (struct pcmcia_device *)arg;
Linus Torvalds's avatar
Linus Torvalds committed
627

628 629
	if (debug_level >= DEBUG_LEVEL_INFO)
		printk("mgslpc_release(0x%p)\n", link);
Linus Torvalds's avatar
Linus Torvalds committed
630

631
	pcmcia_disable_device(link);
Linus Torvalds's avatar
Linus Torvalds committed
632 633
}

634
static void mgslpc_detach(struct pcmcia_device *link)
Linus Torvalds's avatar
Linus Torvalds committed
635
{
636 637
	if (debug_level >= DEBUG_LEVEL_INFO)
		printk("mgslpc_detach(0x%p)\n", link);
638

639 640
	((MGSLPC_INFO *)link->priv)->stop = 1;
	mgslpc_release((u_long)link);
Linus Torvalds's avatar
Linus Torvalds committed
641

642
	mgslpc_remove_device((MGSLPC_INFO *)link->priv);
Linus Torvalds's avatar
Linus Torvalds committed
643 644
}

645
static int mgslpc_suspend(struct pcmcia_device *link)
646 647 648 649 650 651 652 653
{
	MGSLPC_INFO *info = link->priv;

	info->stop = 1;

	return 0;
}

654
static int mgslpc_resume(struct pcmcia_device *link)
655 656 657 658 659 660 661 662 663
{
	MGSLPC_INFO *info = link->priv;

	info->stop = 0;

	return 0;
}


664
static inline bool mgslpc_paranoia_check(MGSLPC_INFO *info,
Linus Torvalds's avatar
Linus Torvalds committed
665 666 667 668 669 670 671 672 673 674
					char *name, const char *routine)
{
#ifdef MGSLPC_PARANOIA_CHECK
	static const char *badmagic =
		"Warning: bad magic number for mgsl struct (%s) in %s\n";
	static const char *badinfo =
		"Warning: null mgslpc_info for (%s) in %s\n";

	if (!info) {
		printk(badinfo, name, routine);
675
		return true;
Linus Torvalds's avatar
Linus Torvalds committed
676 677 678
	}
	if (info->magic != MGSLPC_MAGIC) {
		printk(badmagic, name, routine);
679
		return true;
Linus Torvalds's avatar
Linus Torvalds committed
680 681 682
	}
#else
	if (!info)
683
		return true;
Linus Torvalds's avatar
Linus Torvalds committed
684
#endif
685
	return false;
Linus Torvalds's avatar
Linus Torvalds committed
686 687 688 689 690 691 692 693 694 695 696
}


#define CMD_RXFIFO      BIT7	// release current rx FIFO
#define CMD_RXRESET     BIT6	// receiver reset
#define CMD_RXFIFO_READ BIT5
#define CMD_START_TIMER BIT4
#define CMD_TXFIFO      BIT3	// release current tx FIFO
#define CMD_TXEOM       BIT1	// transmit end message
#define CMD_TXRESET     BIT0	// transmit reset

697
static bool wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
Linus Torvalds's avatar
Linus Torvalds committed
698 699
{
	int i = 0;
700
	/* wait for command completion */
Linus Torvalds's avatar
Linus Torvalds committed
701 702 703
	while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
		udelay(1);
		if (i++ == 1000)
704
			return false;
Linus Torvalds's avatar
Linus Torvalds committed
705
	}
706
	return true;
Linus Torvalds's avatar
Linus Torvalds committed
707 708
}

709
static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
Linus Torvalds's avatar
Linus Torvalds committed
710 711 712 713 714 715 716 717 718
{
	wait_command_complete(info, channel);
	write_reg(info, (unsigned char) (channel + CMDR), cmd);
}

static void tx_pause(struct tty_struct *tty)
{
	MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
	unsigned long flags;
719

Linus Torvalds's avatar
Linus Torvalds committed
720 721 722
	if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
		return;
	if (debug_level >= DEBUG_LEVEL_INFO)
723 724
		printk("tx_pause(%s)\n",info->device_name);

Linus Torvalds's avatar
Linus Torvalds committed
725 726 727 728 729 730 731 732 733 734
	spin_lock_irqsave(&info->lock,flags);
	if (info->tx_enabled)
	 	tx_stop(info);
	spin_unlock_irqrestore(&info->lock,flags);
}

static void tx_release(struct tty_struct *tty)
{
	MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
	unsigned long flags;
735

Linus Torvalds's avatar
Linus Torvalds committed
736 737 738
	if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
		return;
	if (debug_level >= DEBUG_LEVEL_INFO)
739 740
		printk("tx_release(%s)\n",info->device_name);

Linus Torvalds's avatar
Linus Torvalds committed
741 742
	spin_lock_irqsave(&info->lock,flags);
	if (!info->tx_enabled)
743
	 	tx_start(info, tty);
Linus Torvalds's avatar
Linus Torvalds committed
744 745 746 747 748 749 750 751 752 753
	spin_unlock_irqrestore(&info->lock,flags);
}

/* Return next bottom half action to perform.
 * or 0 if nothing to do.
 */
static int bh_action(MGSLPC_INFO *info)
{
	unsigned long flags;
	int rc = 0;
754

Linus Torvalds's avatar
Linus Torvalds committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	spin_lock_irqsave(&info->lock,flags);

	if (info->pending_bh & BH_RECEIVE) {
		info->pending_bh &= ~BH_RECEIVE;
		rc = BH_RECEIVE;
	} else if (info->pending_bh & BH_TRANSMIT) {
		info->pending_bh &= ~BH_TRANSMIT;
		rc = BH_TRANSMIT;
	} else if (info->pending_bh & BH_STATUS) {
		info->pending_bh &= ~BH_STATUS;
		rc = BH_STATUS;
	}

	if (!rc) {
		/* Mark BH routine as complete */
770 771
		info->bh_running = false;
		info->bh_requested = false;
Linus Torvalds's avatar
Linus Torvalds committed
772
	}
773

Linus Torvalds's avatar
Linus Torvalds committed
774
	spin_unlock_irqrestore(&info->lock,flags);
775

Linus Torvalds's avatar
Linus Torvalds committed
776 777 778
	return rc;
}

779
static void bh_handler(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
780
{
781
	MGSLPC_INFO *info = container_of(work, MGSLPC_INFO, task);
782
	struct tty_struct *tty;
Linus Torvalds's avatar
Linus Torvalds committed
783 784 785 786
	int action;

	if (!info)
		return;
787

Linus Torvalds's avatar
Linus Torvalds committed
788 789 790
	if (debug_level >= DEBUG_LEVEL_BH)
		printk( "%s(%d):bh_handler(%s) entry\n",
			__FILE__,__LINE__,info->device_name);
791

792
	info->bh_running = true;
793
	tty = tty_port_tty_get(&info->port);
Linus Torvalds's avatar
Linus Torvalds committed
794 795

	while((action = bh_action(info)) != 0) {
796

Linus Torvalds's avatar
Linus Torvalds committed
797 798 799 800 801 802
		/* Process work item */
		if ( debug_level >= DEBUG_LEVEL_BH )
			printk( "%s(%d):bh_handler() work item action=%d\n",
				__FILE__,__LINE__,action);

		switch (action) {
803

Linus Torvalds's avatar
Linus Torvalds committed
804
		case BH_RECEIVE:
805
			while(rx_get_frame(info, tty));
Linus Torvalds's avatar
Linus Torvalds committed
806 807
			break;
		case BH_TRANSMIT:
808
			bh_transmit(info, tty);
Linus Torvalds's avatar
Linus Torvalds committed
809 810 811 812 813 814 815 816 817 818 819
			break;
		case BH_STATUS:
			bh_status(info);
			break;
		default:
			/* unknown work item ID */
			printk("Unknown work item ID=%08X!\n", action);
			break;
		}
	}

820
	tty_kref_put(tty);
Linus Torvalds's avatar
Linus Torvalds committed
821 822 823 824 825
	if (debug_level >= DEBUG_LEVEL_BH)
		printk( "%s(%d):bh_handler(%s) exit\n",
			__FILE__,__LINE__,info->device_name);
}

826
static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
827 828 829 830
{
	if (debug_level >= DEBUG_LEVEL_BH)
		printk("bh_transmit() entry on %s\n", info->device_name);

831
	if (tty)
Linus Torvalds's avatar
Linus Torvalds committed
832 833 834
		tty_wakeup(tty);
}

835
static void bh_status(MGSLPC_INFO *info)
Linus Torvalds's avatar
Linus Torvalds committed
836 837 838 839 840 841 842
{
	info->ri_chkcount = 0;
	info->dsr_chkcount = 0;
	info->dcd_chkcount = 0;
	info->cts_chkcount = 0;
}

843
/* eom: non-zero = end of frame */
Linus Torvalds's avatar
Linus Torvalds committed
844 845 846 847 848 849 850 851
static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
{
	unsigned char data[2];
	unsigned char fifo_count, read_count, i;
	RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));

	if (debug_level >= DEBUG_LEVEL_ISR)
		printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
852

Linus Torvalds's avatar
Linus Torvalds committed
853 854 855 856 857 858 859
	if (!info->rx_enabled)
		return;

	if (info->rx_frame_count >= info->rx_buf_count) {
		/* no more free buffers */
		issue_command(info, CHA, CMD_RXRESET);
		info->pending_bh |= BH_RECEIVE;
860
		info->rx_overflow = true;
Linus Torvalds's avatar
Linus Torvalds committed
861 862 863 864 865
		info->icount.buf_overrun++;
		return;
	}

	if (eom) {
866
		/* end of frame, get FIFO count from RBCL register */
Linus Torvalds's avatar
Linus Torvalds committed
867 868 869 870
		if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
			fifo_count = 32;
	} else
		fifo_count = 32;
871

Linus Torvalds's avatar
Linus Torvalds committed
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
	do {
		if (fifo_count == 1) {
			read_count = 1;
			data[0] = read_reg(info, CHA + RXFIFO);
		} else {
			read_count = 2;
			*((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
		}
		fifo_count -= read_count;
		if (!fifo_count && eom)
			buf->status = data[--read_count];

		for (i = 0; i < read_count; i++) {
			if (buf->count >= info->max_frame_size) {
				/* frame too large, reset receiver and reset current buffer */
				issue_command(info, CHA, CMD_RXRESET);
				buf->count = 0;
				return;
			}
			*(buf->data + buf->count) = data[i];
			buf->count++;
		}
	} while (fifo_count);

	if (eom) {
		info->pending_bh |= BH_RECEIVE;
		info->rx_frame_count++;
		info->rx_put++;
		if (info->rx_put >= info->rx_buf_count)
			info->rx_put = 0;
	}
	issue_command(info, CHA, CMD_RXFIFO);
}

906
static void rx_ready_async(MGSLPC_INFO *info, int tcd, struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
907
{
908
	unsigned char data, status, flag;
Linus Torvalds's avatar
Linus Torvalds committed
909
	int fifo_count;
910
	int work = 0;
Linus Torvalds's avatar
Linus Torvalds committed
911 912 913
 	struct mgsl_icount *icount = &info->icount;

	if (tcd) {
914
		/* early termination, get FIFO count from RBCL register */
Linus Torvalds's avatar
Linus Torvalds committed
915 916 917 918 919 920 921 922 923
		fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);

		/* Zero fifo count could mean 0 or 32 bytes available.
		 * If BIT5 of STAR is set then at least 1 byte is available.
		 */
		if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
			fifo_count = 32;
	} else
		fifo_count = 32;
924 925

	tty_buffer_request_room(tty, fifo_count);
926
	/* Flush received async data to receive data buffer. */
Linus Torvalds's avatar
Linus Torvalds committed
927 928 929 930 931 932
	while (fifo_count) {
		data   = read_reg(info, CHA + RXFIFO);
		status = read_reg(info, CHA + RXFIFO);
		fifo_count -= 2;

		icount->rx++;
933
		flag = TTY_NORMAL;
Linus Torvalds's avatar
Linus Torvalds committed
934 935 936 937 938 939

		// if no frameing/crc error then save data
		// BIT7:parity error
		// BIT6:framing error

		if (status & (BIT7 + BIT6)) {
940
			if (status & BIT7)
Linus Torvalds's avatar
Linus Torvalds committed
941 942 943 944 945 946 947
				icount->parity++;
			else
				icount->frame++;

			/* discard char if tty control flags say so */
			if (status & info->ignore_status_mask)
				continue;
948

Linus Torvalds's avatar
Linus Torvalds committed
949 950 951
			status &= info->read_status_mask;

			if (status & BIT7)
952
				flag = TTY_PARITY;
Linus Torvalds's avatar
Linus Torvalds committed
953
			else if (status & BIT6)
954
				flag = TTY_FRAME;
Linus Torvalds's avatar
Linus Torvalds committed
955
		}
956
		work += tty_insert_flip_char(tty, data, flag);
Linus Torvalds's avatar
Linus Torvalds committed
957 958 959 960
	}
	issue_command(info, CHA, CMD_RXFIFO);

	if (debug_level >= DEBUG_LEVEL_ISR) {
961 962
		printk("%s(%d):rx_ready_async",
			__FILE__,__LINE__);
Linus Torvalds's avatar
Linus Torvalds committed
963 964 965 966
		printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
			__FILE__,__LINE__,icount->rx,icount->brk,
			icount->parity,icount->frame,icount->overrun);
	}
967

968
	if (work)
Linus Torvalds's avatar
Linus Torvalds committed
969 970 971 972
		tty_flip_buffer_push(tty);
}


973
static void tx_done(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
974 975 976
{
	if (!info->tx_active)
		return;
977

978 979
	info->tx_active = false;
	info->tx_aborting = false;
Linus Torvalds's avatar
Linus Torvalds committed
980 981 982 983 984

	if (info->params.mode == MGSL_MODE_ASYNC)
		return;

	info->tx_count = info->tx_put = info->tx_get = 0;
985 986
	del_timer(&info->tx_timer);

Linus Torvalds's avatar
Linus Torvalds committed
987 988 989 990 991 992
	if (info->drop_rts_on_tx_done) {
		get_signals(info);
		if (info->serial_signals & SerialSignal_RTS) {
			info->serial_signals &= ~SerialSignal_RTS;
			set_signals(info);
		}
993
		info->drop_rts_on_tx_done = false;
Linus Torvalds's avatar
Linus Torvalds committed
994 995
	}

996
#if SYNCLINK_GENERIC_HDLC
Linus Torvalds's avatar
Linus Torvalds committed
997 998
	if (info->netcount)
		hdlcdev_tx_done(info);
999
	else
Linus Torvalds's avatar
Linus Torvalds committed
1000 1001
#endif
	{
1002
		if (tty->stopped || tty->hw_stopped) {
Linus Torvalds's avatar
Linus Torvalds committed
1003 1004 1005 1006 1007 1008 1009
			tx_stop(info);
			return;
		}
		info->pending_bh |= BH_TRANSMIT;
	}
}

1010
static void tx_ready(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
{
	unsigned char fifo_count = 32;
	int c;

	if (debug_level >= DEBUG_LEVEL_ISR)
		printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);

	if (info->params.mode == MGSL_MODE_HDLC) {
		if (!info->tx_active)
			return;
	} else {
1022
		if (tty->stopped || tty->hw_stopped) {
Linus Torvalds's avatar
Linus Torvalds committed
1023 1024 1025 1026
			tx_stop(info);
			return;
		}
		if (!info->tx_count)
1027
			info->tx_active = false;
Linus Torvalds's avatar
Linus Torvalds committed
1028 1029 1030 1031 1032 1033 1034
	}

	if (!info->tx_count)
		return;

	while (info->tx_count && fifo_count) {
		c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
1035

Linus Torvalds's avatar
Linus Torvalds committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
		if (c == 1) {
			write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
		} else {
			write_reg16(info, CHA + TXFIFO,
					  *((unsigned short*)(info->tx_buf + info->tx_get)));
		}
		info->tx_count -= c;
		info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
		fifo_count -= c;
	}

	if (info->params.mode == MGSL_MODE_ASYNC) {
		if (info->tx_count < WAKEUP_CHARS)
			info->pending_bh |= BH_TRANSMIT;
		issue_command(info, CHA, CMD_TXFIFO);
	} else {
		if (info->tx_count)
			issue_command(info, CHA, CMD_TXFIFO);
		else
			issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
	}
}

1059
static void cts_change(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
{
	get_signals(info);
	if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
		irq_disable(info, CHB, IRQ_CTS);
	info->icount.cts++;
	if (info->serial_signals & SerialSignal_CTS)
		info->input_signal_events.cts_up++;
	else
		info->input_signal_events.cts_down++;
	wake_up_interruptible(&info->status_event_wait_q);
	wake_up_interruptible(&info->event_wait_q);

1072 1073
	if (info->port.flags & ASYNC_CTS_FLOW) {
		if (tty->hw_stopped) {
Linus Torvalds's avatar
Linus Torvalds committed
1074 1075 1076
			if (info->serial_signals & SerialSignal_CTS) {
				if (debug_level >= DEBUG_LEVEL_ISR)
					printk("CTS tx start...");
1077 1078 1079
				if (tty)
					tty->hw_stopped = 0;
				tx_start(info, tty);
Linus Torvalds's avatar
Linus Torvalds committed
1080 1081 1082 1083 1084 1085 1086
				info->pending_bh |= BH_TRANSMIT;
				return;
			}
		} else {
			if (!(info->serial_signals & SerialSignal_CTS)) {
				if (debug_level >= DEBUG_LEVEL_ISR)
					printk("CTS tx stop...");
1087 1088
				if (tty)
					tty->hw_stopped = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1089 1090 1091 1092 1093 1094 1095
				tx_stop(info);
			}
		}
	}
	info->pending_bh |= BH_STATUS;
}

1096
static void dcd_change(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
{
	get_signals(info);
	if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
		irq_disable(info, CHB, IRQ_DCD);
	info->icount.dcd++;
	if (info->serial_signals & SerialSignal_DCD) {
		info->input_signal_events.dcd_up++;
	}
	else
		info->input_signal_events.dcd_down++;
1107
#if SYNCLINK_GENERIC_HDLC
1108 1109 1110 1111 1112 1113
	if (info->netcount) {
		if (info->serial_signals & SerialSignal_DCD)
			netif_carrier_on(info->netdev);
		else
			netif_carrier_off(info->netdev);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1114 1115 1116 1117
#endif
	wake_up_interruptible(&info->status_event_wait_q);
	wake_up_interruptible(&info->event_wait_q);

1118
	if (info->port.flags & ASYNC_CHECK_CD) {
Linus Torvalds's avatar
Linus Torvalds committed
1119 1120 1121 1122
		if (debug_level >= DEBUG_LEVEL_ISR)
			printk("%s CD now %s...", info->device_name,
			       (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
		if (info->serial_signals & SerialSignal_DCD)
1123
			wake_up_interruptible(&info->port.open_wait);
Linus Torvalds's avatar
Linus Torvalds committed
1124 1125 1126
		else {
			if (debug_level >= DEBUG_LEVEL_ISR)
				printk("doing serial hangup...");
1127 1128
			if (tty)
				tty_hangup(tty);
Linus Torvalds's avatar
Linus Torvalds committed
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
		}
	}
	info->pending_bh |= BH_STATUS;
}

static void dsr_change(MGSLPC_INFO *info)
{
	get_signals(info);
	if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
		port_irq_disable(info, PVR_DSR);
	info->icount.dsr++;
	if (info->serial_signals & SerialSignal_DSR)
		info->input_signal_events.dsr_up++;
	else
		info->input_signal_events.dsr_down++;
	wake_up_interruptible(&info->status_event_wait_q);
	wake_up_interruptible(&info->event_wait_q);
	info->pending_bh |= BH_STATUS;
}

static void ri_change(MGSLPC_INFO *info)
{
	get_signals(info);
	if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
		port_irq_disable(info, PVR_RI);
	info->icount.rng++;
	if (info->serial_signals & SerialSignal_RI)
		info->input_signal_events.ri_up++;
	else
		info->input_signal_events.ri_down++;
	wake_up_interruptible(&info->status_event_wait_q);
	wake_up_interruptible(&info->event_wait_q);
	info->pending_bh |= BH_STATUS;
}

/* Interrupt service routine entry point.
1165
 *
Linus Torvalds's avatar
Linus Torvalds committed
1166
 * Arguments:
1167
 *
Linus Torvalds's avatar
Linus Torvalds committed
1168 1169 1170
 * irq     interrupt number that caused interrupt
 * dev_id  device ID supplied during interrupt registration
 */
1171
static irqreturn_t mgslpc_isr(int dummy, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
1172
{
1173
	MGSLPC_INFO *info = dev_id;
1174
	struct tty_struct *tty;
Linus Torvalds's avatar
Linus Torvalds committed
1175 1176 1177 1178
	unsigned short isr;
	unsigned char gis, pis;
	int count=0;

1179
	if (debug_level >= DEBUG_LEVEL_ISR)
1180
		printk("mgslpc_isr(%d) entry.\n", info->irq_level);
1181

1182
	if (!(info->p_dev->_locked))
Linus Torvalds's avatar
Linus Torvalds committed
1183 1184
		return IRQ_HANDLED;

1185 1186
	tty = tty_port_tty_get(&info->port);

Linus Torvalds's avatar
Linus Torvalds committed
1187 1188 1189
	spin_lock(&info->lock);

	while ((gis = read_reg(info, CHA + GIS))) {
1190
		if (debug_level >= DEBUG_LEVEL_ISR)
Linus Torvalds's avatar
Linus Torvalds committed
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
			printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);

		if ((gis & 0x70) || count > 1000) {
			printk("synclink_cs:hardware failed or ejected\n");
			break;
		}
		count++;

		if (gis & (BIT1 + BIT0)) {
			isr = read_reg16(info, CHB + ISR);
			if (isr & IRQ_DCD)
1202
				dcd_change(info, tty);
Linus Torvalds's avatar
Linus Torvalds committed
1203
			if (isr & IRQ_CTS)
1204
				cts_change(info, tty);
Linus Torvalds's avatar
Linus Torvalds committed
1205 1206 1207 1208 1209
		}
		if (gis & (BIT3 + BIT2))
		{
			isr = read_reg16(info, CHA + ISR);
			if (isr & IRQ_TIMER) {
1210
				info->irq_occurred = true;
Linus Torvalds's avatar
Linus Torvalds committed
1211 1212 1213
				irq_disable(info, CHA, IRQ_TIMER);
			}

1214
			/* receive IRQs */
Linus Torvalds's avatar
Linus Torvalds committed
1215 1216 1217 1218 1219 1220
			if (isr & IRQ_EXITHUNT) {
				info->icount.exithunt++;
				wake_up_interruptible(&info->event_wait_q);
			}
			if (isr & IRQ_BREAK_ON) {
				info->icount.brk++;
1221 1222
				if (info->port.flags & ASYNC_SAK)
					do_SAK(tty);
Linus Torvalds's avatar
Linus Torvalds committed
1223 1224 1225 1226 1227 1228
			}
			if (isr & IRQ_RXTIME) {
				issue_command(info, CHA, CMD_RXFIFO_READ);
			}
			if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
				if (info->params.mode == MGSL_MODE_HDLC)
1229
					rx_ready_hdlc(info, isr & IRQ_RXEOM);
Linus Torvalds's avatar
Linus Torvalds committed
1230
				else
1231
					rx_ready_async(info, isr & IRQ_RXEOM, tty);
Linus Torvalds's avatar
Linus Torvalds committed
1232 1233
			}

1234
			/* transmit IRQs */
Linus Torvalds's avatar
Linus Torvalds committed
1235 1236 1237 1238 1239
			if (isr & IRQ_UNDERRUN) {
				if (info->tx_aborting)
					info->icount.txabort++;
				else
					info->icount.txunder++;
1240
				tx_done(info, tty);
Linus Torvalds's avatar
Linus Torvalds committed
1241 1242 1243
			}
			else if (isr & IRQ_ALLSENT) {
				info->icount.txok++;
1244
				tx_done(info, tty);
Linus Torvalds's avatar
Linus Torvalds committed
1245 1246
			}
			else if (isr & IRQ_TXFIFO)
1247
				tx_ready(info, tty);
Linus Torvalds's avatar
Linus Torvalds committed
1248 1249 1250 1251 1252 1253 1254 1255 1256
		}
		if (gis & BIT7) {
			pis = read_reg(info, CHA + PIS);
			if (pis & BIT1)
				dsr_change(info);
			if (pis & BIT2)
				ri_change(info);
		}
	}
1257 1258

	/* Request bottom half processing if there's something
Linus Torvalds's avatar
Linus Torvalds committed
1259 1260 1261 1262
	 * for it to do and the bh is not already running
	 */

	if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1263
		if ( debug_level >= DEBUG_LEVEL_ISR )
Linus Torvalds's avatar
Linus Torvalds committed
1264 1265 1266
			printk("%s(%d):%s queueing bh task.\n",
				__FILE__,__LINE__,info->device_name);
		schedule_work(&info->task);
1267
		info->bh_requested = true;
Linus Torvalds's avatar
Linus Torvalds committed
1268 1269 1270
	}

	spin_unlock(&info->lock);
1271
	tty_kref_put(tty);
1272 1273

	if (debug_level >= DEBUG_LEVEL_ISR)
Linus Torvalds's avatar
Linus Torvalds committed
1274
		printk("%s(%d):mgslpc_isr(%d)exit.\n",
1275
		       __FILE__, __LINE__, info->irq_level);
Linus Torvalds's avatar
Linus Torvalds committed
1276 1277 1278 1279 1280 1281

	return IRQ_HANDLED;
}

/* Initialize and start device.
 */
1282
static int startup(MGSLPC_INFO * info, struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1283 1284
{
	int retval = 0;
1285

Linus Torvalds's avatar
Linus Torvalds committed
1286 1287
	if (debug_level >= DEBUG_LEVEL_INFO)
		printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1288

1289
	if (info->port.flags & ASYNC_INITIALIZED)
Linus Torvalds's avatar
Linus Torvalds committed
1290
		return 0;
1291

Linus Torvalds's avatar
Linus Torvalds committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
	if (!info->tx_buf) {
		/* allocate a page of memory for a transmit buffer */
		info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
		if (!info->tx_buf) {
			printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
				__FILE__,__LINE__,info->device_name);
			return -ENOMEM;
		}
	}

	info->pending_bh = 0;
1303

1304 1305
	memset(&info->icount, 0, sizeof(info->icount));

1306
	setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
Linus Torvalds's avatar
Linus Torvalds committed
1307 1308 1309

	/* Allocate and claim adapter resources */
	retval = claim_resources(info);
1310

Linus Torvalds's avatar
Linus Torvalds committed
1311 1312 1313
	/* perform existance check and diagnostics */
	if ( !retval )
		retval = adapter_test(info);
1314

Linus Torvalds's avatar
Linus Torvalds committed
1315
	if ( retval ) {
1316 1317
  		if (capable(CAP_SYS_ADMIN) && tty)
			set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
1318 1319 1320 1321 1322
		release_resources(info);
  		return retval;
  	}

	/* program hardware for current parameters */
1323
	mgslpc_change_params(info, tty);
1324

1325 1326
	if (tty)
		clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
1327

1328
	info->port.flags |= ASYNC_INITIALIZED;
1329

Linus Torvalds's avatar
Linus Torvalds committed
1330 1331 1332 1333 1334
	return 0;
}

/* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
 */
1335
static void shutdown(MGSLPC_INFO * info, struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1336 1337
{
	unsigned long flags;
1338

1339
	if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds's avatar
Linus Torvalds committed
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
		return;

	if (debug_level >= DEBUG_LEVEL_INFO)
		printk("%s(%d):mgslpc_shutdown(%s)\n",
			 __FILE__,__LINE__, info->device_name );

	/* clear status wait queue because status changes */
	/* can't happen after shutting down the hardware */
	wake_up_interruptible(&info->status_event_wait_q);
	wake_up_interruptible(&info->event_wait_q);

1351
	del_timer_sync(&info->tx_timer);
Linus Torvalds's avatar
Linus Torvalds committed
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364

	if (info->tx_buf) {
		free_page((unsigned long) info->tx_buf);
		info->tx_buf = NULL;
	}

	spin_lock_irqsave(&info->lock,flags);

	rx_stop(info);
	tx_stop(info);

	/* TODO:disable interrupts instead of reset to preserve signal states */
	reset_device(info);
1365

1366
 	if (!tty || tty->termios->c_cflag & HUPCL) {
Linus Torvalds's avatar
Linus Torvalds committed
1367 1368 1369
 		info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
		set_signals(info);
	}
1370

Linus Torvalds's avatar
Linus Torvalds committed
1371 1372
	spin_unlock_irqrestore(&info->lock,flags);

1373 1374
	release_resources(info);

1375 1376
	if (tty)
		set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
1377

1378
	info->port.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds's avatar
Linus Torvalds committed
1379 1380
}

1381
static void mgslpc_program_hw(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1382 1383 1384 1385
{
	unsigned long flags;

	spin_lock_irqsave(&info->lock,flags);
1386

Linus Torvalds's avatar
Linus Torvalds committed
1387 1388 1389
	rx_stop(info);
	tx_stop(info);
	info->tx_count = info->tx_put = info->tx_get = 0;
1390

Linus Torvalds's avatar
Linus Torvalds committed
1391 1392 1393 1394
	if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
		hdlc_mode(info);
	else
		async_mode(info);
1395

Linus Torvalds's avatar
Linus Torvalds committed
1396
	set_signals(info);
1397

Linus Torvalds's avatar
Linus Torvalds committed
1398 1399 1400 1401 1402 1403 1404 1405
	info->dcd_chkcount = 0;
	info->cts_chkcount = 0;
	info->ri_chkcount = 0;
	info->dsr_chkcount = 0;

	irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
	port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
	get_signals(info);
1406

1407
	if (info->netcount || (tty && (tty->termios->c_cflag & CREAD)))
Linus Torvalds's avatar
Linus Torvalds committed
1408
		rx_start(info);
1409

Linus Torvalds's avatar
Linus Torvalds committed
1410 1411 1412 1413 1414
	spin_unlock_irqrestore(&info->lock,flags);
}

/* Reconfigure adapter based on new parameters
 */
1415
static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
1416 1417 1418 1419
{
	unsigned cflag;
	int bits_per_char;

1420
	if (!tty || !tty->termios)
Linus Torvalds's avatar
Linus Torvalds committed
1421
		return;
1422

Linus Torvalds's avatar
Linus Torvalds committed
1423 1424 1425
	if (debug_level >= DEBUG_LEVEL_INFO)
		printk("%s(%d):mgslpc_change_params(%s)\n",
			 __FILE__,__LINE__, info->device_name );
1426

1427
	cflag = tty->termios->c_cflag;
Linus Torvalds's avatar
Linus Torvalds committed
1428 1429 1430 1431 1432 1433 1434

	/* if B0 rate (hangup) specified then negate DTR and RTS */
	/* otherwise assert DTR and RTS */
 	if (cflag & CBAUD)
		info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
	else
		info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1435

Linus Torvalds's avatar
Linus Torvalds committed
1436
	/* byte size and parity */
1437

Linus Torvalds's avatar
Linus Torvalds committed
1438 1439 1440 1441 1442 1443 1444
	switch (cflag & CSIZE) {
	case CS5: info->params.data_bits = 5; break;
	case CS6: info->params.data_bits = 6; break;
	case CS7: info->params.data_bits = 7; break;
	case CS8: info->params.data_bits = 8; break;
	default:  info->params.data_bits = 7; break;
	}
1445

Linus Torvalds's avatar
Linus Torvalds committed
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
	if (cflag & CSTOPB)
		info->params.stop_bits = 2;
	else
		info->params.stop_bits = 1;

	info->params.parity = ASYNC_PARITY_NONE;
	if (cflag & PARENB) {
		if (cflag & PARODD)
			info->params.parity = ASYNC_PARITY_ODD;
		else
			info->params.parity = ASYNC_PARITY_EVEN;
#ifdef CMSPAR
		if (cflag & CMSPAR)
			info->params.parity = ASYNC_PARITY_SPACE;
#endif
	}

	/* calculate number of jiffies to transmit a full
	 * FIFO (32 bytes) at specified data rate
	 */
1466
	bits_per_char = info->params.data_bits +
Linus Torvalds's avatar
Linus Torvalds committed
1467 1468 1469 1470 1471 1472 1473
			info->params.stop_bits + 1;

	/* if port data rate is set to 460800 or less then
	 * allow tty settings to override, otherwise keep the
	 * current data rate.
	 */
	if (info->params.data_rate <= 460800) {
1474
		info->params.data_rate = tty_get_baud_rate(tty);
Linus Torvalds's avatar
Linus Torvalds committed
1475
	}
1476

Linus Torvalds's avatar
Linus Torvalds committed
1477
	if ( info->params.data_rate ) {
1478
		info->timeout = (32*HZ*bits_per_char) /
Linus Torvalds's avatar
Linus Torvalds committed
1479 1480 1481 1482 1483
				info->params.data_rate;
	}
	info->timeout += HZ/50;		/* Add .02 seconds of slop */

	if (cflag & CRTSCTS)
1484
		info->port.flags |= ASYNC_CTS_FLOW;
Linus Torvalds's avatar
Linus Torvalds committed
1485
	else
1486
		info->port.flags &= ~ASYNC_CTS_FLOW;
1487

Linus Torvalds's avatar
Linus Torvalds committed
1488
	if (cflag & CLOCAL)
1489
		info->port.flags &= ~ASYNC_CHECK_CD;
Linus Torvalds's avatar
Linus Torvalds committed
1490
	else
1491
		info->port.flags |= ASYNC_CHECK_CD;
Linus Torvalds's avatar
Linus Torvalds committed
1492 1493

	/* process tty input control flags */
1494

Linus Torvalds's avatar
Linus Torvalds committed
1495
	info->read_status_mask = 0;
1496
	if (I_INPCK(tty))
Linus Torvalds's avatar
Linus Torvalds committed
1497
		info->read_status_mask |= BIT7 | BIT6;
1498
	if (I_IGNPAR(tty))
Linus Torvalds's avatar
Linus Torvalds committed
1499 1500
		info->ignore_status_mask |= BIT7 | BIT6;

1501
	mgslpc_program_hw(info, tty);
Linus Torvalds's avatar
Linus Torvalds committed
1502 1503 1504 1505
}

/* Add a character to the transmit buffer
 */
1506
static int mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds's avatar
Linus Torvalds committed
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
{
	MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
	unsigned long flags;

	if (debug_level >= DEBUG_LEVEL_INFO) {
		printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
			__FILE__,__LINE__,ch,info->device_name);
	}

	if