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