2 * linux/drivers/char/pcmcia/synclink_cs.c
4 * $Id: synclink_cs.c,v 4.21 2004/03/08 15:29:23 paulkf Exp $
6 * Device driver for Microgate SyncLink PC Card
7 * multiprotocol serial adapter.
9 * written by Paul Fulghum for Microgate Corporation
10 * paulkf@microgate.com
12 * Microgate and SyncLink are trademarks of Microgate Corporation
14 * This code is released under the GNU General Public License (GPL)
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.
29 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
31 # define BREAKPOINT() asm(" int $3");
33 # define BREAKPOINT() { }
36 #define MAX_DEVICE_COUNT 4
38 #include <linux/config.h>
39 #include <linux/module.h>
40 #include <linux/errno.h>
41 #include <linux/signal.h>
42 #include <linux/sched.h>
43 #include <linux/timer.h>
44 #include <linux/interrupt.h>
45 #include <linux/pci.h>
46 #include <linux/tty.h>
47 #include <linux/tty_flip.h>
48 #include <linux/serial.h>
49 #include <linux/major.h>
50 #include <linux/string.h>
51 #include <linux/fcntl.h>
52 #include <linux/ptrace.h>
53 #include <linux/ioport.h>
55 #include <linux/slab.h>
56 #include <linux/netdevice.h>
57 #include <linux/vmalloc.h>
58 #include <linux/init.h>
59 #include <asm/serial.h>
60 #include <linux/delay.h>
61 #include <linux/ioctl.h>
63 #include <asm/system.h>
67 #include <asm/bitops.h>
68 #include <asm/types.h>
69 #include <linux/termios.h>
70 #include <linux/workqueue.h>
72 #include <pcmcia/version.h>
73 #include <pcmcia/cs_types.h>
74 #include <pcmcia/cs.h>
75 #include <pcmcia/cistpl.h>
76 #include <pcmcia/cisreg.h>
77 #include <pcmcia/ds.h>
79 #ifdef CONFIG_SYNCLINK_SYNCPPP_MODULE
80 #define CONFIG_SYNCLINK_SYNCPPP 1
83 #ifdef CONFIG_SYNCLINK_SYNCPPP
84 #include <net/syncppp.h>
87 #define GET_USER(error,value,addr) error = get_user(value,addr)
88 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
89 #define PUT_USER(error,value,addr) error = put_user(value,addr)
90 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
92 #include <asm/uaccess.h>
94 #include "linux/synclink.h"
96 static MGSL_PARAMS default_params = {
97 MGSL_MODE_HDLC, /* unsigned long mode */
98 0, /* unsigned char loopback; */
99 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
100 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
101 0, /* unsigned long clock_speed; */
102 0xff, /* unsigned char addr_filter; */
103 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
104 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
105 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
106 9600, /* unsigned long data_rate; */
107 8, /* unsigned char data_bits; */
108 1, /* unsigned char stop_bits; */
109 ASYNC_PARITY_NONE /* unsigned char parity; */
115 unsigned char status;
119 /* The queue of BH actions to be performed */
122 #define BH_TRANSMIT 2
125 #define IO_PIN_SHUTDOWN_LIMIT 100
127 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
129 struct _input_signal_events {
142 * Device instance data structure
145 typedef struct _mgslpc_info {
146 void *if_ptr; /* General purpose pointer (used by SPPP) */
149 int count; /* count of opens */
151 unsigned short close_delay;
152 unsigned short closing_wait; /* time to wait before closing */
154 struct mgsl_icount icount;
156 struct tty_struct *tty;
158 int x_char; /* xon/xoff character */
159 int blocked_open; /* # of blocked opens */
160 unsigned char read_status_mask;
161 unsigned char ignore_status_mask;
163 unsigned char *tx_buf;
168 /* circular list of fixed length rx buffers */
170 unsigned char *rx_buf; /* memory allocated for all rx buffers */
171 int rx_buf_total_size; /* size of memory allocated for rx buffers */
172 int rx_put; /* index of next empty rx buffer */
173 int rx_get; /* index of next full rx buffer */
174 int rx_buf_size; /* size in bytes of single rx buffer */
175 int rx_buf_count; /* total number of rx buffers */
176 int rx_frame_count; /* number of full rx buffers */
178 wait_queue_head_t open_wait;
179 wait_queue_head_t close_wait;
181 wait_queue_head_t status_event_wait_q;
182 wait_queue_head_t event_wait_q;
183 struct timer_list tx_timer; /* HDLC transmit timeout timer */
184 struct _mgslpc_info *next_device; /* device list link */
186 unsigned short imra_value;
187 unsigned short imrb_value;
188 unsigned char pim_value;
191 struct work_struct task; /* task structure for scheduling bh */
200 int dcd_chkcount; /* check counts to prevent */
201 int cts_chkcount; /* too many IRQs if a signal */
202 int dsr_chkcount; /* is floating */
213 int if_mode; /* serial interface selection (RS-232, v.35 etc) */
215 char device_name[25]; /* device instance name */
217 unsigned int io_base; /* base I/O address of adapter */
218 unsigned int irq_level;
220 MGSL_PARAMS params; /* communications parameters */
222 unsigned char serial_signals; /* current serial signal states */
224 char irq_occurred; /* for diagnostics use */
226 unsigned int init_error; /* startup error (DIAGS) */
228 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
229 BOOLEAN drop_rts_on_tx_done;
231 struct _input_signal_events input_signal_events;
238 /* SPPP/Cisco HDLC device parts */
242 #ifdef CONFIG_SYNCLINK_SYNCPPP
243 struct ppp_device pppdev;
245 struct net_device *netdev;
246 struct net_device_stats netstats;
250 #define MGSLPC_MAGIC 0x5402
253 * The size of the serial xmit buffer is 1 page, or 4096 bytes
255 #define TXBUFSIZE 4096
258 #define CHA 0x00 /* channel A offset */
259 #define CHB 0x40 /* channel B offset */
304 #define IRQ_BREAK_ON BIT15 // rx break detected
305 #define IRQ_DATAOVERRUN BIT14 // receive data overflow
306 #define IRQ_ALLSENT BIT13 // all sent
307 #define IRQ_UNDERRUN BIT12 // transmit data underrun
308 #define IRQ_TIMER BIT11 // timer interrupt
309 #define IRQ_CTS BIT10 // CTS status change
310 #define IRQ_TXREPEAT BIT9 // tx message repeat
311 #define IRQ_TXFIFO BIT8 // transmit pool ready
312 #define IRQ_RXEOM BIT7 // receive message end
313 #define IRQ_EXITHUNT BIT6 // receive frame start
314 #define IRQ_RXTIME BIT6 // rx char timeout
315 #define IRQ_DCD BIT2 // carrier detect status change
316 #define IRQ_OVERRUN BIT1 // receive frame overflow
317 #define IRQ_RXFIFO BIT0 // receive pool full
321 #define XFW BIT6 // transmit FIFO write enable
322 #define CEC BIT2 // command executing
323 #define CTS BIT1 // CTS state
328 #define PVR_AUTOCTS BIT3
329 #define PVR_RS232 0x20 /* 0010b */
330 #define PVR_V35 0xe0 /* 1110b */
331 #define PVR_RS422 0x40 /* 0100b */
333 /* Register access functions */
335 #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
336 #define read_reg(info, reg) inb((info)->io_base + (reg))
338 #define read_reg16(info, reg) inw((info)->io_base + (reg))
339 #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
341 #define set_reg_bits(info, reg, mask) \
342 write_reg(info, (reg), \
343 (unsigned char) (read_reg(info, (reg)) | (mask)))
344 #define clear_reg_bits(info, reg, mask) \
345 write_reg(info, (reg), \
346 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
348 * interrupt enable/disable routines
350 static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
352 if (channel == CHA) {
353 info->imra_value |= mask;
354 write_reg16(info, CHA + IMR, info->imra_value);
356 info->imrb_value |= mask;
357 write_reg16(info, CHB + IMR, info->imrb_value);
360 static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
362 if (channel == CHA) {
363 info->imra_value &= ~mask;
364 write_reg16(info, CHA + IMR, info->imra_value);
366 info->imrb_value &= ~mask;
367 write_reg16(info, CHB + IMR, info->imrb_value);
371 #define port_irq_disable(info, mask) \
372 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
374 #define port_irq_enable(info, mask) \
375 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
377 static void rx_start(MGSLPC_INFO *info);
378 static void rx_stop(MGSLPC_INFO *info);
380 static void tx_start(MGSLPC_INFO *info);
381 static void tx_stop(MGSLPC_INFO *info);
382 static void tx_set_idle(MGSLPC_INFO *info);
384 static void get_signals(MGSLPC_INFO *info);
385 static void set_signals(MGSLPC_INFO *info);
387 static void reset_device(MGSLPC_INFO *info);
389 static void hdlc_mode(MGSLPC_INFO *info);
390 static void async_mode(MGSLPC_INFO *info);
392 static void tx_timeout(unsigned long context);
394 static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg);
396 #ifdef CONFIG_SYNCLINK_SYNCPPP
397 /* SPPP/HDLC stuff */
398 static void mgslpc_sppp_init(MGSLPC_INFO *info);
399 static void mgslpc_sppp_delete(MGSLPC_INFO *info);
400 static int mgslpc_sppp_open(struct net_device *d);
401 static int mgslpc_sppp_close(struct net_device *d);
402 static void mgslpc_sppp_tx_timeout(struct net_device *d);
403 static int mgslpc_sppp_tx(struct sk_buff *skb, struct net_device *d);
404 static void mgslpc_sppp_rx_done(MGSLPC_INFO *info, char *buf, int size);
405 static void mgslpc_sppp_tx_done(MGSLPC_INFO *info);
406 static int mgslpc_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
407 struct net_device_stats *mgslpc_net_stats(struct net_device *dev);
410 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
412 static BOOLEAN register_test(MGSLPC_INFO *info);
413 static BOOLEAN irq_test(MGSLPC_INFO *info);
414 static int adapter_test(MGSLPC_INFO *info);
416 static int claim_resources(MGSLPC_INFO *info);
417 static void release_resources(MGSLPC_INFO *info);
418 static void mgslpc_add_device(MGSLPC_INFO *info);
419 static void mgslpc_remove_device(MGSLPC_INFO *info);
421 static int rx_get_frame(MGSLPC_INFO *info);
422 static void rx_reset_buffers(MGSLPC_INFO *info);
423 static int rx_alloc_buffers(MGSLPC_INFO *info);
424 static void rx_free_buffers(MGSLPC_INFO *info);
426 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs);
429 * Bottom half interrupt handlers
431 static void bh_handler(void* Context);
432 static void bh_transmit(MGSLPC_INFO *info);
433 static void bh_status(MGSLPC_INFO *info);
438 static int tiocmget(struct tty_struct *tty, struct file *file);
439 static int tiocmset(struct tty_struct *tty, struct file *file,
440 unsigned int set, unsigned int clear);
441 static int get_stats(MGSLPC_INFO *info, struct mgsl_icount *user_icount);
442 static int get_params(MGSLPC_INFO *info, MGSL_PARAMS *user_params);
443 static int set_params(MGSLPC_INFO *info, MGSL_PARAMS *new_params);
444 static int get_txidle(MGSLPC_INFO *info, int*idle_mode);
445 static int set_txidle(MGSLPC_INFO *info, int idle_mode);
446 static int set_txenable(MGSLPC_INFO *info, int enable);
447 static int tx_abort(MGSLPC_INFO *info);
448 static int set_rxenable(MGSLPC_INFO *info, int enable);
449 static int wait_events(MGSLPC_INFO *info, int *mask);
451 #define jiffies_from_ms(a) ((((a) * HZ)/1000)+1)
453 static MGSLPC_INFO *mgslpc_device_list = NULL;
454 static int mgslpc_device_count = 0;
457 * Set this param to non-zero to load eax with the
458 * .text section address and breakpoint on module load.
459 * This is useful for use with gdb and add-symbol-file command.
461 static int break_on_load=0;
464 * Driver major number, defaults to zero to get auto
465 * assigned major number. May be forced as module parameter.
467 static int ttymajor=0;
469 static int debug_level = 0;
470 static int maxframe[MAX_DEVICE_COUNT] = {0,};
471 static int dosyncppp[MAX_DEVICE_COUNT] = {1,1,1,1};
473 /* The old way: bit map of interrupts to choose from */
474 /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, and 3 */
475 static u_int irq_mask = 0xdeb8;
477 /* Newer, simpler way of listing specific interrupts */
478 static int irq_list[4] = { -1 };
480 MODULE_PARM(irq_mask, "i");
481 MODULE_PARM(irq_list, "1-4i");
483 MODULE_PARM(break_on_load,"i");
484 MODULE_PARM(ttymajor,"i");
485 MODULE_PARM(debug_level,"i");
486 MODULE_PARM(maxframe,"1-" __MODULE_STRING(MAX_DEVICE_COUNT) "i");
487 MODULE_PARM(dosyncppp,"1-" __MODULE_STRING(MAX_DEVICE_COUNT) "i");
489 MODULE_LICENSE("GPL");
491 static char *driver_name = "SyncLink PC Card driver";
492 static char *driver_version = "$Revision: 4.21 $";
494 static struct tty_driver *serial_driver;
496 /* number of characters left in xmit buffer before we ask for more */
497 #define WAKEUP_CHARS 256
499 static void mgslpc_change_params(MGSLPC_INFO *info);
500 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
503 #define MIN(a,b) ((a) < (b) ? (a) : (b))
506 /* PCMCIA prototypes */
508 static void mgslpc_config(dev_link_t *link);
509 static void mgslpc_release(u_long arg);
510 static int mgslpc_event(event_t event, int priority,
511 event_callback_args_t *args);
512 static dev_link_t *mgslpc_attach(void);
513 static void mgslpc_detach(dev_link_t *);
515 static dev_info_t dev_info = "synclink_cs";
516 static dev_link_t *dev_list = NULL;
519 * 1st function defined in .text section. Calling this function in
520 * init_module() followed by a breakpoint allows a remote debugger
521 * (gdb) to get the .text address for the add-symbol-file command.
522 * This allows remote debugging of dynamically loadable modules.
524 static void* mgslpc_get_text_ptr(void);
525 static void* mgslpc_get_text_ptr() {return mgslpc_get_text_ptr;}
527 static dev_link_t *mgslpc_attach(void)
531 client_reg_t client_reg;
534 if (debug_level >= DEBUG_LEVEL_INFO)
535 printk("mgslpc_attach\n");
537 info = (MGSLPC_INFO *)kmalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
539 printk("Error can't allocate device instance data\n");
543 memset(info, 0, sizeof(MGSLPC_INFO));
544 info->magic = MGSLPC_MAGIC;
545 INIT_WORK(&info->task, bh_handler, info);
546 info->max_frame_size = 4096;
547 info->close_delay = 5*HZ/10;
548 info->closing_wait = 30*HZ;
549 init_waitqueue_head(&info->open_wait);
550 init_waitqueue_head(&info->close_wait);
551 init_waitqueue_head(&info->status_event_wait_q);
552 init_waitqueue_head(&info->event_wait_q);
553 spin_lock_init(&info->lock);
554 spin_lock_init(&info->netlock);
555 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
556 info->idle_mode = HDLC_TXIDLE_FLAGS;
557 info->imra_value = 0xffff;
558 info->imrb_value = 0xffff;
559 info->pim_value = 0xff;
564 /* Initialize the dev_link_t structure */
566 /* Interrupt setup */
567 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
568 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
569 if (irq_list[0] == -1)
570 link->irq.IRQInfo2 = irq_mask;
572 for (i = 0; i < 4; i++)
573 link->irq.IRQInfo2 |= 1 << irq_list[i];
574 link->irq.Handler = NULL;
576 link->conf.Attributes = 0;
578 link->conf.IntType = INT_MEMORY_AND_IO;
580 /* Register with Card Services */
581 link->next = dev_list;
584 client_reg.dev_info = &dev_info;
585 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
586 client_reg.EventMask =
587 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
588 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
589 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
590 client_reg.event_handler = &mgslpc_event;
591 client_reg.Version = 0x0210;
592 client_reg.event_callback_args.client_data = link;
594 ret = pcmcia_register_client(&link->handle, &client_reg);
595 if (ret != CS_SUCCESS) {
596 cs_error(link->handle, RegisterClient, ret);
601 mgslpc_add_device(info);
606 /* Card has been inserted.
609 #define CS_CHECK(fn, ret) \
610 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
612 static void mgslpc_config(dev_link_t *link)
614 client_handle_t handle = link->handle;
615 MGSLPC_INFO *info = link->priv;
618 int last_fn, last_ret;
621 cistpl_cftable_entry_t dflt = { 0 };
622 cistpl_cftable_entry_t *cfg;
624 if (debug_level >= DEBUG_LEVEL_INFO)
625 printk("mgslpc_config(0x%p)\n", link);
627 /* read CONFIG tuple to find its configuration registers */
628 tuple.DesiredTuple = CISTPL_CONFIG;
629 tuple.Attributes = 0;
630 tuple.TupleData = buf;
631 tuple.TupleDataMax = sizeof(buf);
632 tuple.TupleOffset = 0;
633 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
634 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
635 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
636 link->conf.ConfigBase = parse.config.base;
637 link->conf.Present = parse.config.rmask[0];
640 link->state |= DEV_CONFIG;
642 /* Look up the current Vcc */
643 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
644 link->conf.Vcc = conf.Vcc;
646 /* get CIS configuration entry */
648 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
649 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
651 cfg = &(parse.cftable_entry);
652 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
653 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
655 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
659 link->conf.ConfigIndex = cfg->index;
660 link->conf.Attributes |= CONF_ENABLE_IRQ;
662 /* IO window settings */
663 link->io.NumPorts1 = 0;
664 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
665 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
666 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
667 if (!(io->flags & CISTPL_IO_8BIT))
668 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
669 if (!(io->flags & CISTPL_IO_16BIT))
670 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
671 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
672 link->io.BasePort1 = io->win[0].base;
673 link->io.NumPorts1 = io->win[0].len;
674 CS_CHECK(RequestIO, pcmcia_request_io(link->handle, &link->io));
677 link->conf.Attributes = CONF_ENABLE_IRQ;
679 link->conf.IntType = INT_MEMORY_AND_IO;
680 link->conf.ConfigIndex = 8;
681 link->conf.Present = PRESENT_OPTION;
683 link->irq.Attributes |= IRQ_HANDLE_PRESENT;
684 link->irq.Handler = mgslpc_isr;
685 link->irq.Instance = info;
686 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
688 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
690 info->io_base = link->io.BasePort1;
691 info->irq_level = link->irq.AssignedIRQ;
693 /* add to linked list of devices */
694 sprintf(info->node.dev_name, "mgslpc0");
695 info->node.major = info->node.minor = 0;
696 link->dev = &info->node;
698 printk(KERN_INFO "%s: index 0x%02x:",
699 info->node.dev_name, link->conf.ConfigIndex);
700 if (link->conf.Attributes & CONF_ENABLE_IRQ)
701 printk(", irq %d", link->irq.AssignedIRQ);
702 if (link->io.NumPorts1)
703 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
704 link->io.BasePort1+link->io.NumPorts1-1);
707 link->state &= ~DEV_CONFIG_PENDING;
711 cs_error(link->handle, last_fn, last_ret);
712 mgslpc_release((u_long)link);
715 /* Card has been removed.
716 * Unregister device and release PCMCIA configuration.
717 * If device is open, postpone until it is closed.
719 static void mgslpc_release(u_long arg)
721 dev_link_t *link = (dev_link_t *)arg;
723 if (debug_level >= DEBUG_LEVEL_INFO)
724 printk("mgslpc_release(0x%p)\n", link);
726 /* Unlink the device chain */
728 link->state &= ~DEV_CONFIG;
730 pcmcia_release_configuration(link->handle);
731 if (link->io.NumPorts1)
732 pcmcia_release_io(link->handle, &link->io);
733 if (link->irq.AssignedIRQ)
734 pcmcia_release_irq(link->handle, &link->irq);
735 if (link->state & DEV_STALE_LINK)
739 static void mgslpc_detach(dev_link_t *link)
743 if (debug_level >= DEBUG_LEVEL_INFO)
744 printk("mgslpc_detach(0x%p)\n", link);
747 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
748 if (*linkp == link) break;
752 if (link->state & DEV_CONFIG) {
753 /* device is configured/active, mark it so when
754 * release() is called a proper detach() occurs.
756 if (debug_level >= DEBUG_LEVEL_INFO)
757 printk(KERN_DEBUG "synclinkpc: detach postponed, '%s' "
758 "still locked\n", link->dev->dev_name);
759 link->state |= DEV_STALE_LINK;
763 /* Break the link with Card Services */
765 pcmcia_deregister_client(link->handle);
767 /* Unlink device structure, and free it */
769 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
772 static int mgslpc_event(event_t event, int priority,
773 event_callback_args_t *args)
775 dev_link_t *link = args->client_data;
776 MGSLPC_INFO *info = link->priv;
778 if (debug_level >= DEBUG_LEVEL_INFO)
779 printk("mgslpc_event(0x%06x)\n", event);
782 case CS_EVENT_CARD_REMOVAL:
783 link->state &= ~DEV_PRESENT;
784 if (link->state & DEV_CONFIG) {
785 ((MGSLPC_INFO *)link->priv)->stop = 1;
786 mgslpc_release((u_long)link);
789 case CS_EVENT_CARD_INSERTION:
790 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
793 case CS_EVENT_PM_SUSPEND:
794 link->state |= DEV_SUSPEND;
795 /* Fall through... */
796 case CS_EVENT_RESET_PHYSICAL:
797 /* Mark the device as stopped, to block IO until later */
799 if (link->state & DEV_CONFIG)
800 pcmcia_release_configuration(link->handle);
802 case CS_EVENT_PM_RESUME:
803 link->state &= ~DEV_SUSPEND;
804 /* Fall through... */
805 case CS_EVENT_CARD_RESET:
806 if (link->state & DEV_CONFIG)
807 pcmcia_request_configuration(link->handle, &link->conf);
814 static inline int mgslpc_paranoia_check(MGSLPC_INFO *info,
815 char *name, const char *routine)
817 #ifdef MGSLPC_PARANOIA_CHECK
818 static const char *badmagic =
819 "Warning: bad magic number for mgsl struct (%s) in %s\n";
820 static const char *badinfo =
821 "Warning: null mgslpc_info for (%s) in %s\n";
824 printk(badinfo, name, routine);
827 if (info->magic != MGSLPC_MAGIC) {
828 printk(badmagic, name, routine);
839 #define CMD_RXFIFO BIT7 // release current rx FIFO
840 #define CMD_RXRESET BIT6 // receiver reset
841 #define CMD_RXFIFO_READ BIT5
842 #define CMD_START_TIMER BIT4
843 #define CMD_TXFIFO BIT3 // release current tx FIFO
844 #define CMD_TXEOM BIT1 // transmit end message
845 #define CMD_TXRESET BIT0 // transmit reset
847 static BOOLEAN wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
850 unsigned char status;
851 /* wait for command completion */
852 while ((status = read_reg(info, (unsigned char)(channel+STAR)) & BIT2)) {
860 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
862 wait_command_complete(info, channel);
863 write_reg(info, (unsigned char) (channel + CMDR), cmd);
866 static void tx_pause(struct tty_struct *tty)
868 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
871 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
873 if (debug_level >= DEBUG_LEVEL_INFO)
874 printk("tx_pause(%s)\n",info->device_name);
876 spin_lock_irqsave(&info->lock,flags);
877 if (info->tx_enabled)
879 spin_unlock_irqrestore(&info->lock,flags);
882 static void tx_release(struct tty_struct *tty)
884 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
887 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
889 if (debug_level >= DEBUG_LEVEL_INFO)
890 printk("tx_release(%s)\n",info->device_name);
892 spin_lock_irqsave(&info->lock,flags);
893 if (!info->tx_enabled)
895 spin_unlock_irqrestore(&info->lock,flags);
898 /* Return next bottom half action to perform.
899 * or 0 if nothing to do.
901 int bh_action(MGSLPC_INFO *info)
906 spin_lock_irqsave(&info->lock,flags);
908 if (info->pending_bh & BH_RECEIVE) {
909 info->pending_bh &= ~BH_RECEIVE;
911 } else if (info->pending_bh & BH_TRANSMIT) {
912 info->pending_bh &= ~BH_TRANSMIT;
914 } else if (info->pending_bh & BH_STATUS) {
915 info->pending_bh &= ~BH_STATUS;
920 /* Mark BH routine as complete */
921 info->bh_running = 0;
922 info->bh_requested = 0;
925 spin_unlock_irqrestore(&info->lock,flags);
930 void bh_handler(void* Context)
932 MGSLPC_INFO *info = (MGSLPC_INFO*)Context;
938 if (debug_level >= DEBUG_LEVEL_BH)
939 printk( "%s(%d):bh_handler(%s) entry\n",
940 __FILE__,__LINE__,info->device_name);
942 info->bh_running = 1;
944 while((action = bh_action(info)) != 0) {
946 /* Process work item */
947 if ( debug_level >= DEBUG_LEVEL_BH )
948 printk( "%s(%d):bh_handler() work item action=%d\n",
949 __FILE__,__LINE__,action);
954 while(rx_get_frame(info));
963 /* unknown work item ID */
964 printk("Unknown work item ID=%08X!\n", action);
969 if (debug_level >= DEBUG_LEVEL_BH)
970 printk( "%s(%d):bh_handler(%s) exit\n",
971 __FILE__,__LINE__,info->device_name);
974 void bh_transmit(MGSLPC_INFO *info)
976 struct tty_struct *tty = info->tty;
977 if (debug_level >= DEBUG_LEVEL_BH)
978 printk("bh_transmit() entry on %s\n", info->device_name);
981 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
982 tty->ldisc.write_wakeup) {
983 if ( debug_level >= DEBUG_LEVEL_BH )
984 printk( "%s(%d):calling ldisc.write_wakeup on %s\n",
985 __FILE__,__LINE__,info->device_name);
986 (tty->ldisc.write_wakeup)(tty);
988 wake_up_interruptible(&tty->write_wait);
992 void bh_status(MGSLPC_INFO *info)
994 info->ri_chkcount = 0;
995 info->dsr_chkcount = 0;
996 info->dcd_chkcount = 0;
997 info->cts_chkcount = 0;
1000 /* eom: non-zero = end of frame */
1001 void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
1003 unsigned char data[2];
1004 unsigned char fifo_count, read_count, i;
1005 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
1007 if (debug_level >= DEBUG_LEVEL_ISR)
1008 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
1010 if (!info->rx_enabled)
1013 if (info->rx_frame_count >= info->rx_buf_count) {
1014 /* no more free buffers */
1015 issue_command(info, CHA, CMD_RXRESET);
1016 info->pending_bh |= BH_RECEIVE;
1017 info->rx_overflow = 1;
1018 info->icount.buf_overrun++;
1023 /* end of frame, get FIFO count from RBCL register */
1024 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
1030 if (fifo_count == 1) {
1032 data[0] = read_reg(info, CHA + RXFIFO);
1035 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
1037 fifo_count -= read_count;
1038 if (!fifo_count && eom)
1039 buf->status = data[--read_count];
1041 for (i = 0; i < read_count; i++) {
1042 if (buf->count >= info->max_frame_size) {
1043 /* frame too large, reset receiver and reset current buffer */
1044 issue_command(info, CHA, CMD_RXRESET);
1048 *(buf->data + buf->count) = data[i];
1051 } while (fifo_count);
1054 info->pending_bh |= BH_RECEIVE;
1055 info->rx_frame_count++;
1057 if (info->rx_put >= info->rx_buf_count)
1060 issue_command(info, CHA, CMD_RXFIFO);
1063 void rx_ready_async(MGSLPC_INFO *info, int tcd)
1065 unsigned char data, status;
1067 struct tty_struct *tty = info->tty;
1068 struct mgsl_icount *icount = &info->icount;
1071 /* early termination, get FIFO count from RBCL register */
1072 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
1074 /* Zero fifo count could mean 0 or 32 bytes available.
1075 * If BIT5 of STAR is set then at least 1 byte is available.
1077 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
1082 /* Flush received async data to receive data buffer. */
1083 while (fifo_count) {
1084 data = read_reg(info, CHA + RXFIFO);
1085 status = read_reg(info, CHA + RXFIFO);
1088 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1091 *tty->flip.char_buf_ptr = data;
1094 *tty->flip.flag_buf_ptr = 0;
1096 // if no frameing/crc error then save data
1097 // BIT7:parity error
1098 // BIT6:framing error
1100 if (status & (BIT7 + BIT6)) {
1106 /* discard char if tty control flags say so */
1107 if (status & info->ignore_status_mask)
1110 status &= info->read_status_mask;
1113 *tty->flip.flag_buf_ptr = TTY_PARITY;
1114 else if (status & BIT6)
1115 *tty->flip.flag_buf_ptr = TTY_FRAME;
1118 tty->flip.flag_buf_ptr++;
1119 tty->flip.char_buf_ptr++;
1122 issue_command(info, CHA, CMD_RXFIFO);
1124 if (debug_level >= DEBUG_LEVEL_ISR) {
1125 printk("%s(%d):rx_ready_async count=%d\n",
1126 __FILE__,__LINE__,tty->flip.count);
1127 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1128 __FILE__,__LINE__,icount->rx,icount->brk,
1129 icount->parity,icount->frame,icount->overrun);
1132 if (tty->flip.count)
1133 tty_flip_buffer_push(tty);
1137 void tx_done(MGSLPC_INFO *info)
1139 if (!info->tx_active)
1142 info->tx_active = 0;
1143 info->tx_aborting = 0;
1145 if (info->params.mode == MGSL_MODE_ASYNC)
1148 info->tx_count = info->tx_put = info->tx_get = 0;
1149 del_timer(&info->tx_timer);
1151 if (info->drop_rts_on_tx_done) {
1153 if (info->serial_signals & SerialSignal_RTS) {
1154 info->serial_signals &= ~SerialSignal_RTS;
1157 info->drop_rts_on_tx_done = 0;
1160 #ifdef CONFIG_SYNCLINK_SYNCPPP
1162 mgslpc_sppp_tx_done(info);
1166 if (info->tty->stopped || info->tty->hw_stopped) {
1170 info->pending_bh |= BH_TRANSMIT;
1174 void tx_ready(MGSLPC_INFO *info)
1176 unsigned char fifo_count = 32;
1179 if (debug_level >= DEBUG_LEVEL_ISR)
1180 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1182 if (info->params.mode == MGSL_MODE_HDLC) {
1183 if (!info->tx_active)
1186 if (info->tty->stopped || info->tty->hw_stopped) {
1190 if (!info->tx_count)
1191 info->tx_active = 0;
1194 if (!info->tx_count)
1197 while (info->tx_count && fifo_count) {
1198 c = MIN(2, MIN(fifo_count, MIN(info->tx_count, TXBUFSIZE - info->tx_get)));
1201 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1203 write_reg16(info, CHA + TXFIFO,
1204 *((unsigned short*)(info->tx_buf + info->tx_get)));
1206 info->tx_count -= c;
1207 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1211 if (info->params.mode == MGSL_MODE_ASYNC) {
1212 if (info->tx_count < WAKEUP_CHARS)
1213 info->pending_bh |= BH_TRANSMIT;
1214 issue_command(info, CHA, CMD_TXFIFO);
1217 issue_command(info, CHA, CMD_TXFIFO);
1219 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1223 void cts_change(MGSLPC_INFO *info)
1226 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1227 irq_disable(info, CHB, IRQ_CTS);
1229 if (info->serial_signals & SerialSignal_CTS)
1230 info->input_signal_events.cts_up++;
1232 info->input_signal_events.cts_down++;
1233 wake_up_interruptible(&info->status_event_wait_q);
1234 wake_up_interruptible(&info->event_wait_q);
1236 if (info->flags & ASYNC_CTS_FLOW) {
1237 if (info->tty->hw_stopped) {
1238 if (info->serial_signals & SerialSignal_CTS) {
1239 if (debug_level >= DEBUG_LEVEL_ISR)
1240 printk("CTS tx start...");
1242 info->tty->hw_stopped = 0;
1244 info->pending_bh |= BH_TRANSMIT;
1248 if (!(info->serial_signals & SerialSignal_CTS)) {
1249 if (debug_level >= DEBUG_LEVEL_ISR)
1250 printk("CTS tx stop...");
1252 info->tty->hw_stopped = 1;
1257 info->pending_bh |= BH_STATUS;
1260 void dcd_change(MGSLPC_INFO *info)
1263 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1264 irq_disable(info, CHB, IRQ_DCD);
1266 if (info->serial_signals & SerialSignal_DCD) {
1267 info->input_signal_events.dcd_up++;
1268 #ifdef CONFIG_SYNCLINK_SYNCPPP
1270 sppp_reopen(info->netdev);
1274 info->input_signal_events.dcd_down++;
1275 wake_up_interruptible(&info->status_event_wait_q);
1276 wake_up_interruptible(&info->event_wait_q);
1278 if (info->flags & ASYNC_CHECK_CD) {
1279 if (debug_level >= DEBUG_LEVEL_ISR)
1280 printk("%s CD now %s...", info->device_name,
1281 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1282 if (info->serial_signals & SerialSignal_DCD)
1283 wake_up_interruptible(&info->open_wait);
1285 if (debug_level >= DEBUG_LEVEL_ISR)
1286 printk("doing serial hangup...");
1288 tty_hangup(info->tty);
1291 info->pending_bh |= BH_STATUS;
1294 void dsr_change(MGSLPC_INFO *info)
1297 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1298 port_irq_disable(info, PVR_DSR);
1300 if (info->serial_signals & SerialSignal_DSR)
1301 info->input_signal_events.dsr_up++;
1303 info->input_signal_events.dsr_down++;
1304 wake_up_interruptible(&info->status_event_wait_q);
1305 wake_up_interruptible(&info->event_wait_q);
1306 info->pending_bh |= BH_STATUS;
1309 void ri_change(MGSLPC_INFO *info)
1312 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1313 port_irq_disable(info, PVR_RI);
1315 if (info->serial_signals & SerialSignal_RI)
1316 info->input_signal_events.ri_up++;
1318 info->input_signal_events.ri_down++;
1319 wake_up_interruptible(&info->status_event_wait_q);
1320 wake_up_interruptible(&info->event_wait_q);
1321 info->pending_bh |= BH_STATUS;
1324 /* Interrupt service routine entry point.
1328 * irq interrupt number that caused interrupt
1329 * dev_id device ID supplied during interrupt registration
1330 * regs interrupted processor context
1332 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs)
1334 MGSLPC_INFO * info = (MGSLPC_INFO *)dev_id;
1336 unsigned char gis, pis;
1339 if (debug_level >= DEBUG_LEVEL_ISR)
1340 printk("mgslpc_isr(%d) entry.\n", irq);
1344 if (!(info->link.state & DEV_CONFIG))
1347 spin_lock(&info->lock);
1349 while ((gis = read_reg(info, CHA + GIS))) {
1350 if (debug_level >= DEBUG_LEVEL_ISR)
1351 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1353 if ((gis & 0x70) || count > 1000) {
1354 printk("synclink_cs:hardware failed or ejected\n");
1359 if (gis & (BIT1 + BIT0)) {
1360 isr = read_reg16(info, CHB + ISR);
1366 if (gis & (BIT3 + BIT2))
1368 isr = read_reg16(info, CHA + ISR);
1369 if (isr & IRQ_TIMER) {
1370 info->irq_occurred = 1;
1371 irq_disable(info, CHA, IRQ_TIMER);
1375 if (isr & IRQ_EXITHUNT) {
1376 info->icount.exithunt++;
1377 wake_up_interruptible(&info->event_wait_q);
1379 if (isr & IRQ_BREAK_ON) {
1381 if (info->flags & ASYNC_SAK)
1384 if (isr & IRQ_RXTIME) {
1385 issue_command(info, CHA, CMD_RXFIFO_READ);
1387 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1388 if (info->params.mode == MGSL_MODE_HDLC)
1389 rx_ready_hdlc(info, isr & IRQ_RXEOM);
1391 rx_ready_async(info, isr & IRQ_RXEOM);
1395 if (isr & IRQ_UNDERRUN) {
1396 if (info->tx_aborting)
1397 info->icount.txabort++;
1399 info->icount.txunder++;
1402 else if (isr & IRQ_ALLSENT) {
1403 info->icount.txok++;
1406 else if (isr & IRQ_TXFIFO)
1410 pis = read_reg(info, CHA + PIS);
1418 /* Request bottom half processing if there's something
1419 * for it to do and the bh is not already running
1422 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1423 if ( debug_level >= DEBUG_LEVEL_ISR )
1424 printk("%s(%d):%s queueing bh task.\n",
1425 __FILE__,__LINE__,info->device_name);
1426 schedule_work(&info->task);
1427 info->bh_requested = 1;
1430 spin_unlock(&info->lock);
1432 if (debug_level >= DEBUG_LEVEL_ISR)
1433 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1434 __FILE__,__LINE__,irq);
1439 /* Initialize and start device.
1441 static int startup(MGSLPC_INFO * info)
1445 if (debug_level >= DEBUG_LEVEL_INFO)
1446 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1448 if (info->flags & ASYNC_INITIALIZED)
1451 if (!info->tx_buf) {
1452 /* allocate a page of memory for a transmit buffer */
1453 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1454 if (!info->tx_buf) {
1455 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1456 __FILE__,__LINE__,info->device_name);
1461 info->pending_bh = 0;
1463 init_timer(&info->tx_timer);
1464 info->tx_timer.data = (unsigned long)info;
1465 info->tx_timer.function = tx_timeout;
1467 /* Allocate and claim adapter resources */
1468 retval = claim_resources(info);
1470 /* perform existance check and diagnostics */
1472 retval = adapter_test(info);
1475 if (capable(CAP_SYS_ADMIN) && info->tty)
1476 set_bit(TTY_IO_ERROR, &info->tty->flags);
1477 release_resources(info);
1481 /* program hardware for current parameters */
1482 mgslpc_change_params(info);
1485 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1487 info->flags |= ASYNC_INITIALIZED;
1492 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1494 static void shutdown(MGSLPC_INFO * info)
1496 unsigned long flags;
1498 if (!(info->flags & ASYNC_INITIALIZED))
1501 if (debug_level >= DEBUG_LEVEL_INFO)
1502 printk("%s(%d):mgslpc_shutdown(%s)\n",
1503 __FILE__,__LINE__, info->device_name );
1505 /* clear status wait queue because status changes */
1506 /* can't happen after shutting down the hardware */
1507 wake_up_interruptible(&info->status_event_wait_q);
1508 wake_up_interruptible(&info->event_wait_q);
1510 del_timer(&info->tx_timer);
1513 free_page((unsigned long) info->tx_buf);
1517 spin_lock_irqsave(&info->lock,flags);
1522 /* TODO:disable interrupts instead of reset to preserve signal states */
1525 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1526 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1530 spin_unlock_irqrestore(&info->lock,flags);
1532 release_resources(info);
1535 set_bit(TTY_IO_ERROR, &info->tty->flags);
1537 info->flags &= ~ASYNC_INITIALIZED;
1540 static void mgslpc_program_hw(MGSLPC_INFO *info)
1542 unsigned long flags;
1544 spin_lock_irqsave(&info->lock,flags);
1548 info->tx_count = info->tx_put = info->tx_get = 0;
1550 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1557 info->dcd_chkcount = 0;
1558 info->cts_chkcount = 0;
1559 info->ri_chkcount = 0;
1560 info->dsr_chkcount = 0;
1562 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1563 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1566 if (info->netcount || info->tty->termios->c_cflag & CREAD)
1569 spin_unlock_irqrestore(&info->lock,flags);
1572 /* Reconfigure adapter based on new parameters
1574 static void mgslpc_change_params(MGSLPC_INFO *info)
1579 if (!info->tty || !info->tty->termios)
1582 if (debug_level >= DEBUG_LEVEL_INFO)
1583 printk("%s(%d):mgslpc_change_params(%s)\n",
1584 __FILE__,__LINE__, info->device_name );
1586 cflag = info->tty->termios->c_cflag;
1588 /* if B0 rate (hangup) specified then negate DTR and RTS */
1589 /* otherwise assert DTR and RTS */
1591 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1593 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1595 /* byte size and parity */
1597 switch (cflag & CSIZE) {
1598 case CS5: info->params.data_bits = 5; break;
1599 case CS6: info->params.data_bits = 6; break;
1600 case CS7: info->params.data_bits = 7; break;
1601 case CS8: info->params.data_bits = 8; break;
1602 default: info->params.data_bits = 7; break;
1606 info->params.stop_bits = 2;
1608 info->params.stop_bits = 1;
1610 info->params.parity = ASYNC_PARITY_NONE;
1611 if (cflag & PARENB) {
1613 info->params.parity = ASYNC_PARITY_ODD;
1615 info->params.parity = ASYNC_PARITY_EVEN;
1618 info->params.parity = ASYNC_PARITY_SPACE;
1622 /* calculate number of jiffies to transmit a full
1623 * FIFO (32 bytes) at specified data rate
1625 bits_per_char = info->params.data_bits +
1626 info->params.stop_bits + 1;
1628 /* if port data rate is set to 460800 or less then
1629 * allow tty settings to override, otherwise keep the
1630 * current data rate.
1632 if (info->params.data_rate <= 460800) {
1633 info->params.data_rate = tty_get_baud_rate(info->tty);
1636 if ( info->params.data_rate ) {
1637 info->timeout = (32*HZ*bits_per_char) /
1638 info->params.data_rate;
1640 info->timeout += HZ/50; /* Add .02 seconds of slop */
1642 if (cflag & CRTSCTS)
1643 info->flags |= ASYNC_CTS_FLOW;
1645 info->flags &= ~ASYNC_CTS_FLOW;
1648 info->flags &= ~ASYNC_CHECK_CD;
1650 info->flags |= ASYNC_CHECK_CD;
1652 /* process tty input control flags */
1654 info->read_status_mask = 0;
1655 if (I_INPCK(info->tty))
1656 info->read_status_mask |= BIT7 | BIT6;
1657 if (I_IGNPAR(info->tty))
1658 info->ignore_status_mask |= BIT7 | BIT6;
1660 mgslpc_program_hw(info);
1663 /* Add a character to the transmit buffer
1665 static void mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1667 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1668 unsigned long flags;
1670 if (debug_level >= DEBUG_LEVEL_INFO) {
1671 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1672 __FILE__,__LINE__,ch,info->device_name);
1675 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1678 if (!tty || !info->tx_buf)
1681 spin_lock_irqsave(&info->lock,flags);
1683 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1684 if (info->tx_count < TXBUFSIZE - 1) {
1685 info->tx_buf[info->tx_put++] = ch;
1686 info->tx_put &= TXBUFSIZE-1;
1691 spin_unlock_irqrestore(&info->lock,flags);
1694 /* Enable transmitter so remaining characters in the
1695 * transmit buffer are sent.
1697 static void mgslpc_flush_chars(struct tty_struct *tty)
1699 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1700 unsigned long flags;
1702 if (debug_level >= DEBUG_LEVEL_INFO)
1703 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1704 __FILE__,__LINE__,info->device_name,info->tx_count);
1706 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1709 if (info->tx_count <= 0 || tty->stopped ||
1710 tty->hw_stopped || !info->tx_buf)
1713 if (debug_level >= DEBUG_LEVEL_INFO)
1714 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1715 __FILE__,__LINE__,info->device_name);
1717 spin_lock_irqsave(&info->lock,flags);
1718 if (!info->tx_active)
1720 spin_unlock_irqrestore(&info->lock,flags);
1723 /* Send a block of data
1727 * tty pointer to tty information structure
1728 * from_user flag: 1 = from user process
1729 * buf pointer to buffer containing send data
1730 * count size of send data in bytes
1732 * Returns: number of characters written
1734 static int mgslpc_write(struct tty_struct * tty, int from_user,
1735 const unsigned char *buf, int count)
1737 int c, ret = 0, err;
1738 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1739 unsigned long flags;
1741 if (debug_level >= DEBUG_LEVEL_INFO)
1742 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1743 __FILE__,__LINE__,info->device_name,count);
1745 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1746 !tty || !info->tx_buf)
1749 if (info->params.mode == MGSL_MODE_HDLC) {
1750 if (count > TXBUFSIZE) {
1754 if (info->tx_active)
1756 else if (info->tx_count)
1762 MIN(TXBUFSIZE - info->tx_count - 1,
1763 TXBUFSIZE - info->tx_put));
1768 COPY_FROM_USER(err, info->tx_buf + info->tx_put, buf, c);
1775 memcpy(info->tx_buf + info->tx_put, buf, c);
1777 spin_lock_irqsave(&info->lock,flags);
1778 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1779 info->tx_count += c;
1780 spin_unlock_irqrestore(&info->lock,flags);
1787 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1788 spin_lock_irqsave(&info->lock,flags);
1789 if (!info->tx_active)
1791 spin_unlock_irqrestore(&info->lock,flags);
1794 if (debug_level >= DEBUG_LEVEL_INFO)
1795 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1796 __FILE__,__LINE__,info->device_name,ret);
1800 /* Return the count of free bytes in transmit buffer
1802 static int mgslpc_write_room(struct tty_struct *tty)
1804 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1807 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1810 if (info->params.mode == MGSL_MODE_HDLC) {
1811 /* HDLC (frame oriented) mode */
1812 if (info->tx_active)
1815 return HDLC_MAX_FRAME_SIZE;
1817 ret = TXBUFSIZE - info->tx_count - 1;
1822 if (debug_level >= DEBUG_LEVEL_INFO)
1823 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1824 __FILE__,__LINE__, info->device_name, ret);
1828 /* Return the count of bytes in transmit buffer
1830 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1832 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1835 if (debug_level >= DEBUG_LEVEL_INFO)
1836 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1837 __FILE__,__LINE__, info->device_name );
1839 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1842 if (info->params.mode == MGSL_MODE_HDLC)
1843 rc = info->tx_active ? info->max_frame_size : 0;
1845 rc = info->tx_count;
1847 if (debug_level >= DEBUG_LEVEL_INFO)
1848 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1849 __FILE__,__LINE__, info->device_name, rc);
1854 /* Discard all data in the send buffer
1856 static void mgslpc_flush_buffer(struct tty_struct *tty)
1858 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1859 unsigned long flags;
1861 if (debug_level >= DEBUG_LEVEL_INFO)
1862 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1863 __FILE__,__LINE__, info->device_name );
1865 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1868 spin_lock_irqsave(&info->lock,flags);
1869 info->tx_count = info->tx_put = info->tx_get = 0;
1870 del_timer(&info->tx_timer);
1871 spin_unlock_irqrestore(&info->lock,flags);
1873 wake_up_interruptible(&tty->write_wait);
1874 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1875 tty->ldisc.write_wakeup)
1876 (tty->ldisc.write_wakeup)(tty);
1879 /* Send a high-priority XON/XOFF character
1881 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1883 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1884 unsigned long flags;
1886 if (debug_level >= DEBUG_LEVEL_INFO)
1887 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1888 __FILE__,__LINE__, info->device_name, ch );
1890 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1895 spin_lock_irqsave(&info->lock,flags);
1896 if (!info->tx_enabled)
1898 spin_unlock_irqrestore(&info->lock,flags);
1902 /* Signal remote device to throttle send data (our receive data)
1904 static void mgslpc_throttle(struct tty_struct * tty)
1906 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1907 unsigned long flags;
1909 if (debug_level >= DEBUG_LEVEL_INFO)
1910 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1911 __FILE__,__LINE__, info->device_name );
1913 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1917 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1919 if (tty->termios->c_cflag & CRTSCTS) {
1920 spin_lock_irqsave(&info->lock,flags);
1921 info->serial_signals &= ~SerialSignal_RTS;
1923 spin_unlock_irqrestore(&info->lock,flags);
1927 /* Signal remote device to stop throttling send data (our receive data)
1929 static void mgslpc_unthrottle(struct tty_struct * tty)
1931 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1932 unsigned long flags;
1934 if (debug_level >= DEBUG_LEVEL_INFO)
1935 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1936 __FILE__,__LINE__, info->device_name );
1938 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1945 mgslpc_send_xchar(tty, START_CHAR(tty));
1948 if (tty->termios->c_cflag & CRTSCTS) {
1949 spin_lock_irqsave(&info->lock,flags);
1950 info->serial_signals |= SerialSignal_RTS;
1952 spin_unlock_irqrestore(&info->lock,flags);
1956 /* get the current serial statistics
1958 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount *user_icount)
1961 if (debug_level >= DEBUG_LEVEL_INFO)
1962 printk("get_params(%s)\n", info->device_name);
1963 COPY_TO_USER(err,user_icount, &info->icount, sizeof(struct mgsl_icount));
1969 /* get the current serial parameters
1971 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS *user_params)
1974 if (debug_level >= DEBUG_LEVEL_INFO)
1975 printk("get_params(%s)\n", info->device_name);
1976 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1982 /* set the serial parameters
1986 * info pointer to device instance data
1987 * new_params user buffer containing new serial params
1989 * Returns: 0 if success, otherwise error code
1991 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS *new_params)
1993 unsigned long flags;
1994 MGSL_PARAMS tmp_params;
1997 if (debug_level >= DEBUG_LEVEL_INFO)
1998 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1999 info->device_name );
2000 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2002 if ( debug_level >= DEBUG_LEVEL_INFO )
2003 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
2004 __FILE__,__LINE__,info->device_name);
2008 spin_lock_irqsave(&info->lock,flags);
2009 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2010 spin_unlock_irqrestore(&info->lock,flags);
2012 mgslpc_change_params(info);
2017 static int get_txidle(MGSLPC_INFO * info, int*idle_mode)
2020 if (debug_level >= DEBUG_LEVEL_INFO)
2021 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
2022 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2028 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
2030 unsigned long flags;
2031 if (debug_level >= DEBUG_LEVEL_INFO)
2032 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
2033 spin_lock_irqsave(&info->lock,flags);
2034 info->idle_mode = idle_mode;
2036 spin_unlock_irqrestore(&info->lock,flags);
2040 static int get_interface(MGSLPC_INFO * info, int*if_mode)
2043 if (debug_level >= DEBUG_LEVEL_INFO)
2044 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
2045 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
2051 static int set_interface(MGSLPC_INFO * info, int if_mode)
2053 unsigned long flags;
2055 if (debug_level >= DEBUG_LEVEL_INFO)
2056 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
2057 spin_lock_irqsave(&info->lock,flags);
2058 info->if_mode = if_mode;
2060 val = read_reg(info, PVR) & 0x0f;
2061 switch (info->if_mode)
2063 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
2064 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
2065 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
2067 write_reg(info, PVR, val);
2069 spin_unlock_irqrestore(&info->lock,flags);
2073 static int set_txenable(MGSLPC_INFO * info, int enable)
2075 unsigned long flags;
2077 if (debug_level >= DEBUG_LEVEL_INFO)
2078 printk("set_txenable(%s,%d)\n", info->device_name, enable);
2080 spin_lock_irqsave(&info->lock,flags);
2082 if (!info->tx_enabled)
2085 if (info->tx_enabled)
2088 spin_unlock_irqrestore(&info->lock,flags);
2092 static int tx_abort(MGSLPC_INFO * info)
2094 unsigned long flags;
2096 if (debug_level >= DEBUG_LEVEL_INFO)
2097 printk("tx_abort(%s)\n", info->device_name);
2099 spin_lock_irqsave(&info->lock,flags);
2100 if (info->tx_active && info->tx_count &&
2101 info->params.mode == MGSL_MODE_HDLC) {
2102 /* clear data count so FIFO is not filled on next IRQ.
2103 * This results in underrun and abort transmission.
2105 info->tx_count = info->tx_put = info->tx_get = 0;
2106 info->tx_aborting = TRUE;
2108 spin_unlock_irqrestore(&info->lock,flags);
2112 static int set_rxenable(MGSLPC_INFO * info, int enable)
2114 unsigned long flags;
2116 if (debug_level >= DEBUG_LEVEL_INFO)
2117 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
2119 spin_lock_irqsave(&info->lock,flags);
2121 if (!info->rx_enabled)
2124 if (info->rx_enabled)
2127 spin_unlock_irqrestore(&info->lock,flags);
2131 /* wait for specified event to occur
2133 * Arguments: info pointer to device instance data
2134 * mask pointer to bitmask of events to wait for
2135 * Return Value: 0 if successful and bit mask updated with
2136 * of events triggerred,
2137 * otherwise error code
2139 static int wait_events(MGSLPC_INFO * info, int * mask_ptr)
2141 unsigned long flags;
2144 struct mgsl_icount cprev, cnow;
2147 struct _input_signal_events oldsigs, newsigs;
2148 DECLARE_WAITQUEUE(wait, current);
2150 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2154 if (debug_level >= DEBUG_LEVEL_INFO)
2155 printk("wait_events(%s,%d)\n", info->device_name, mask);
2157 spin_lock_irqsave(&info->lock,flags);
2159 /* return immediately if state matches requested events */
2161 s = info->serial_signals;
2163 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2164 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2165 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2166 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2168 spin_unlock_irqrestore(&info->lock,flags);
2172 /* save current irq counts */
2173 cprev = info->icount;
2174 oldsigs = info->input_signal_events;
2176 if ((info->params.mode == MGSL_MODE_HDLC) &&
2177 (mask & MgslEvent_ExitHuntMode))
2178 irq_enable(info, CHA, IRQ_EXITHUNT);
2180 set_current_state(TASK_INTERRUPTIBLE);
2181 add_wait_queue(&info->event_wait_q, &wait);
2183 spin_unlock_irqrestore(&info->lock,flags);
2188 if (signal_pending(current)) {
2193 /* get current irq counts */
2194 spin_lock_irqsave(&info->lock,flags);
2195 cnow = info->icount;
2196 newsigs = info->input_signal_events;
2197 set_current_state(TASK_INTERRUPTIBLE);
2198 spin_unlock_irqrestore(&info->lock,flags);
2200 /* if no change, wait aborted for some reason */
2201 if (newsigs.dsr_up == oldsigs.dsr_up &&
2202 newsigs.dsr_down == oldsigs.dsr_down &&
2203 newsigs.dcd_up == oldsigs.dcd_up &&
2204 newsigs.dcd_down == oldsigs.dcd_down &&
2205 newsigs.cts_up == oldsigs.cts_up &&
2206 newsigs.cts_down == oldsigs.cts_down &&
2207 newsigs.ri_up == oldsigs.ri_up &&
2208 newsigs.ri_down == oldsigs.ri_down &&
2209 cnow.exithunt == cprev.exithunt &&
2210 cnow.rxidle == cprev.rxidle) {
2216 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2217 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2218 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2219 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2220 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2221 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2222 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2223 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2224 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2225 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2233 remove_wait_queue(&info->event_wait_q, &wait);
2234 set_current_state(TASK_RUNNING);
2236 if (mask & MgslEvent_ExitHuntMode) {
2237 spin_lock_irqsave(&info->lock,flags);
2238 if (!waitqueue_active(&info->event_wait_q))
2239 irq_disable(info, CHA, IRQ_EXITHUNT);
2240 spin_unlock_irqrestore(&info->lock,flags);
2244 PUT_USER(rc, events, mask_ptr);
2248 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2250 unsigned long flags;
2252 struct mgsl_icount cprev, cnow;
2253 DECLARE_WAITQUEUE(wait, current);
2255 /* save current irq counts */
2256 spin_lock_irqsave(&info->lock,flags);
2257 cprev = info->icount;
2258 add_wait_queue(&info->status_event_wait_q, &wait);
2259 set_current_state(TASK_INTERRUPTIBLE);
2260 spin_unlock_irqrestore(&info->lock,flags);
2264 if (signal_pending(current)) {
2269 /* get new irq counts */
2270 spin_lock_irqsave(&info->lock,flags);
2271 cnow = info->icount;
2272 set_current_state(TASK_INTERRUPTIBLE);
2273 spin_unlock_irqrestore(&info->lock,flags);
2275 /* if no change, wait aborted for some reason */
2276 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2277 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2282 /* check for change in caller specified modem input */
2283 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2284 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2285 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2286 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2293 remove_wait_queue(&info->status_event_wait_q, &wait);
2294 set_current_state(TASK_RUNNING);
2298 /* return the state of the serial control and status signals
2300 static int tiocmget(struct tty_struct *tty, struct file *file)
2302 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2303 unsigned int result;
2304 unsigned long flags;
2306 spin_lock_irqsave(&info->lock,flags);
2308 spin_unlock_irqrestore(&info->lock,flags);
2310 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2311 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2312 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2313 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2314 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2315 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2317 if (debug_level >= DEBUG_LEVEL_INFO)
2318 printk("%s(%d):%s tiocmget() value=%08X\n",
2319 __FILE__,__LINE__, info->device_name, result );
2323 /* set modem control signals (DTR/RTS)
2325 static int tiocmset(struct tty_struct *tty, struct file *file,
2326 unsigned int set, unsigned int clear)
2328 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2329 unsigned long flags;
2331 if (debug_level >= DEBUG_LEVEL_INFO)
2332 printk("%s(%d):%s tiocmset(%x,%x)\n",
2333 __FILE__,__LINE__,info->device_name, set, clear);
2335 if (set & TIOCM_RTS)
2336 info->serial_signals |= SerialSignal_RTS;
2337 if (set & TIOCM_DTR)
2338 info->serial_signals |= SerialSignal_DTR;
2339 if (clear & TIOCM_RTS)
2340 info->serial_signals &= ~SerialSignal_RTS;
2341 if (clear & TIOCM_DTR)
2342 info->serial_signals &= ~SerialSignal_DTR;
2344 spin_lock_irqsave(&info->lock,flags);
2346 spin_unlock_irqrestore(&info->lock,flags);
2351 /* Set or clear transmit break condition
2353 * Arguments: tty pointer to tty instance data
2354 * break_state -1=set break condition, 0=clear
2356 static void mgslpc_break(struct tty_struct *tty, int break_state)
2358 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2359 unsigned long flags;
2361 if (debug_level >= DEBUG_LEVEL_INFO)
2362 printk("%s(%d):mgslpc_break(%s,%d)\n",
2363 __FILE__,__LINE__, info->device_name, break_state);
2365 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2368 spin_lock_irqsave(&info->lock,flags);
2369 if (break_state == -1)
2370 set_reg_bits(info, CHA+DAFO, BIT6);
2372 clear_reg_bits(info, CHA+DAFO, BIT6);
2373 spin_unlock_irqrestore(&info->lock,flags);
2376 /* Service an IOCTL request
2380 * tty pointer to tty instance data
2381 * file pointer to associated file object for device
2382 * cmd IOCTL command code
2383 * arg command argument/context
2385 * Return Value: 0 if success, otherwise error code
2387 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2388 unsigned int cmd, unsigned long arg)
2390 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2392 if (debug_level >= DEBUG_LEVEL_INFO)
2393 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2394 info->device_name, cmd );
2396 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2399 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2400 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2401 if (tty->flags & (1 << TTY_IO_ERROR))
2405 return ioctl_common(info, cmd, arg);
2408 int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
2411 struct mgsl_icount cnow; /* kernel counter temps */
2412 struct serial_icounter_struct *p_cuser; /* user space */
2413 unsigned long flags;
2416 case MGSL_IOCGPARAMS:
2417 return get_params(info,(MGSL_PARAMS *)arg);
2418 case MGSL_IOCSPARAMS:
2419 return set_params(info,(MGSL_PARAMS *)arg);
2420 case MGSL_IOCGTXIDLE:
2421 return get_txidle(info,(int*)arg);
2422 case MGSL_IOCSTXIDLE:
2423 return set_txidle(info,(int)arg);
2425 return get_interface(info,(int*)arg);
2427 return set_interface(info,(int)arg);
2428 case MGSL_IOCTXENABLE:
2429 return set_txenable(info,(int)arg);
2430 case MGSL_IOCRXENABLE:
2431 return set_rxenable(info,(int)arg);
2432 case MGSL_IOCTXABORT:
2433 return tx_abort(info);
2434 case MGSL_IOCGSTATS:
2435 return get_stats(info,(struct mgsl_icount*)arg);
2436 case MGSL_IOCWAITEVENT:
2437 return wait_events(info,(int*)arg);
2439 return modem_input_wait(info,(int)arg);
2441 spin_lock_irqsave(&info->lock,flags);
2442 cnow = info->icount;
2443 spin_unlock_irqrestore(&info->lock,flags);
2444 p_cuser = (struct serial_icounter_struct *) arg;
2445 PUT_USER(error,cnow.cts, &p_cuser->cts);
2446 if (error) return error;
2447 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2448 if (error) return error;
2449 PUT_USER(error,cnow.rng, &p_cuser->rng);
2450 if (error) return error;
2451 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2452 if (error) return error;
2453 PUT_USER(error,cnow.rx, &p_cuser->rx);
2454 if (error) return error;
2455 PUT_USER(error,cnow.tx, &p_cuser->tx);
2456 if (error) return error;
2457 PUT_USER(error,cnow.frame, &p_cuser->frame);
2458 if (error) return error;
2459 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2460 if (error) return error;
2461 PUT_USER(error,cnow.parity, &p_cuser->parity);
2462 if (error) return error;
2463 PUT_USER(error,cnow.brk, &p_cuser->brk);
2464 if (error) return error;
2465 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2466 if (error) return error;
2469 return -ENOIOCTLCMD;
2474 /* Set new termios settings
2478 * tty pointer to tty structure
2479 * termios pointer to buffer to hold returned old termios
2481 static void mgslpc_set_termios(struct tty_struct *tty, struct termios *old_termios)
2483 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2484 unsigned long flags;
2486 if (debug_level >= DEBUG_LEVEL_INFO)
2487 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2488 tty->driver->name );
2490 /* just return if nothing has changed */
2491 if ((tty->termios->c_cflag == old_termios->c_cflag)
2492 && (RELEVANT_IFLAG(tty->termios->c_iflag)
2493 == RELEVANT_IFLAG(old_termios->c_iflag)))
2496 mgslpc_change_params(info);
2498 /* Handle transition to B0 status */
2499 if (old_termios->c_cflag & CBAUD &&
2500 !(tty->termios->c_cflag & CBAUD)) {
2501 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2502 spin_lock_irqsave(&info->lock,flags);
2504 spin_unlock_irqrestore(&info->lock,flags);
2507 /* Handle transition away from B0 status */
2508 if (!(old_termios->c_cflag & CBAUD) &&
2509 tty->termios->c_cflag & CBAUD) {
2510 info->serial_signals |= SerialSignal_DTR;
2511 if (!(tty->termios->c_cflag & CRTSCTS) ||
2512 !test_bit(TTY_THROTTLED, &tty->flags)) {
2513 info->serial_signals |= SerialSignal_RTS;
2515 spin_lock_irqsave(&info->lock,flags);
2517 spin_unlock_irqrestore(&info->lock,flags);
2520 /* Handle turning off CRTSCTS */
2521 if (old_termios->c_cflag & CRTSCTS &&
2522 !(tty->termios->c_cflag & CRTSCTS)) {
2523 tty->hw_stopped = 0;
2528 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2530 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2532 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2535 if (debug_level >= DEBUG_LEVEL_INFO)
2536 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2537 __FILE__,__LINE__, info->device_name, info->count);
2542 if (tty_hung_up_p(filp))
2545 if ((tty->count == 1) && (info->count != 1)) {
2547 * tty->count is 1 and the tty structure will be freed.
2548 * info->count should be one in this case.
2549 * if it's not, correct it so that the port is shutdown.
2551 printk("mgslpc_close: bad refcount; tty->count is 1, "
2552 "info->count is %d\n", info->count);
2558 /* if at least one open remaining, leave hardware active */
2562 info->flags |= ASYNC_CLOSING;
2564 /* set tty->closing to notify line discipline to
2565 * only process XON/XOFF characters. Only the N_TTY
2566 * discipline appears to use this (ppp does not).
2570 /* wait for transmit data to clear all layers */
2572 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
2573 if (debug_level >= DEBUG_LEVEL_INFO)
2574 printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sent\n",
2575 __FILE__,__LINE__, info->device_name );
2576 tty_wait_until_sent(tty, info->closing_wait);
2579 if (info->flags & ASYNC_INITIALIZED)
2580 mgslpc_wait_until_sent(tty, info->timeout);
2582 if (tty->driver->flush_buffer)
2583 tty->driver->flush_buffer(tty);
2585 if (tty->ldisc.flush_buffer)
2586 tty->ldisc.flush_buffer(tty);
2593 if (info->blocked_open) {
2594 if (info->close_delay) {
2595 set_current_state(TASK_INTERRUPTIBLE);
2596 schedule_timeout(info->close_delay);
2598 wake_up_interruptible(&info->open_wait);
2601 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2603 wake_up_interruptible(&info->close_wait);
2606 if (debug_level >= DEBUG_LEVEL_INFO)
2607 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2608 tty->driver->name, info->count);
2611 /* Wait until the transmitter is empty.
2613 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2615 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2616 unsigned long orig_jiffies, char_time;
2621 if (debug_level >= DEBUG_LEVEL_INFO)
2622 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2623 __FILE__,__LINE__, info->device_name );
2625 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2628 if (!(info->flags & ASYNC_INITIALIZED))
2631 orig_jiffies = jiffies;
2633 /* Set check interval to 1/5 of estimated time to
2634 * send a character, and make it at least 1. The check
2635 * interval should also be less than the timeout.
2636 * Note: use tight timings here to satisfy the NIST-PCTS.
2639 if ( info->params.data_rate ) {
2640 char_time = info->timeout/(32 * 5);
2647 char_time = MIN(char_time, timeout);
2649 if (info->params.mode == MGSL_MODE_HDLC) {
2650 while (info->tx_active) {
2651 set_current_state(TASK_INTERRUPTIBLE);
2652 schedule_timeout(char_time);
2653 if (signal_pending(current))
2655 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2659 while ((info->tx_count || info->tx_active) &&
2661 set_current_state(TASK_INTERRUPTIBLE);
2662 schedule_timeout(char_time);
2663 if (signal_pending(current))
2665 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2671 if (debug_level >= DEBUG_LEVEL_INFO)
2672 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2673 __FILE__,__LINE__, info->device_name );
2676 /* Called by tty_hangup() when a hangup is signaled.
2677 * This is the same as closing all open files for the port.
2679 static void mgslpc_hangup(struct tty_struct *tty)
2681 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2683 if (debug_level >= DEBUG_LEVEL_INFO)
2684 printk("%s(%d):mgslpc_hangup(%s)\n",
2685 __FILE__,__LINE__, info->device_name );
2687 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2690 mgslpc_flush_buffer(tty);
2694 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2697 wake_up_interruptible(&info->open_wait);
2700 /* Block the current process until the specified port
2701 * is ready to be opened.
2703 static int block_til_ready(struct tty_struct *tty, struct file *filp,
2706 DECLARE_WAITQUEUE(wait, current);
2708 int do_clocal = 0, extra_count = 0;
2709 unsigned long flags;
2711 if (debug_level >= DEBUG_LEVEL_INFO)
2712 printk("%s(%d):block_til_ready on %s\n",
2713 __FILE__,__LINE__, tty->driver->name );
2715 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
2716 /* nonblock mode is set or port is not enabled */
2717 /* just verify that callout device is not active */
2718 info->flags |= ASYNC_NORMAL_ACTIVE;
2722 if (tty->termios->c_cflag & CLOCAL)
2725 /* Wait for carrier detect and the line to become
2726 * free (i.e., not in use by the callout). While we are in
2727 * this loop, info->count is dropped by one, so that
2728 * mgslpc_close() knows when to free things. We restore it upon
2729 * exit, either normal or abnormal.
2733 add_wait_queue(&info->open_wait, &wait);
2735 if (debug_level >= DEBUG_LEVEL_INFO)
2736 printk("%s(%d):block_til_ready before block on %s count=%d\n",
2737 __FILE__,__LINE__, tty->driver->name, info->count );
2739 spin_lock_irqsave(&info->lock, flags);
2740 if (!tty_hung_up_p(filp)) {
2744 spin_unlock_irqrestore(&info->lock, flags);
2745 info->blocked_open++;
2748 if ((tty->termios->c_cflag & CBAUD)) {
2749 spin_lock_irqsave(&info->lock,flags);
2750 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2752 spin_unlock_irqrestore(&info->lock,flags);
2755 set_current_state(TASK_INTERRUPTIBLE);
2757 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
2758 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
2759 -EAGAIN : -ERESTARTSYS;
2763 spin_lock_irqsave(&info->lock,flags);
2765 spin_unlock_irqrestore(&info->lock,flags);
2767 if (!(info->flags & ASYNC_CLOSING) &&
2768 (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
2772 if (signal_pending(current)) {
2773 retval = -ERESTARTSYS;
2777 if (debug_level >= DEBUG_LEVEL_INFO)
2778 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
2779 __FILE__,__LINE__, tty->driver->name, info->count );
2784 set_current_state(TASK_RUNNING);
2785 remove_wait_queue(&info->open_wait, &wait);
2789 info->blocked_open--;
2791 if (debug_level >= DEBUG_LEVEL_INFO)
2792 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
2793 __FILE__,__LINE__, tty->driver->name, info->count );
2796 info->flags |= ASYNC_NORMAL_ACTIVE;
2801 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2805 unsigned long flags;
2807 /* verify range of specified line number */
2809 if ((line < 0) || (line >= mgslpc_device_count)) {
2810 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2811 __FILE__,__LINE__,line);
2815 /* find the info structure for the specified line */
2816 info = mgslpc_device_list;
2817 while(info && info->line != line)
2818 info = info->next_device;
2819 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2822 tty->driver_data = info;
2825 if (debug_level >= DEBUG_LEVEL_INFO)
2826 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2827 __FILE__,__LINE__,tty->driver->name, info->count);
2829 /* If port is closing, signal caller to try again */
2830 if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
2831 if (info->flags & ASYNC_CLOSING)
2832 interruptible_sleep_on(&info->close_wait);
2833 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2834 -EAGAIN : -ERESTARTSYS);
2838 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2840 spin_lock_irqsave(&info->netlock, flags);
2841 if (info->netcount) {
2843 spin_unlock_irqrestore(&info->netlock, flags);
2847 spin_unlock_irqrestore(&info->netlock, flags);
2849 if (info->count == 1) {
2850 /* 1st open on this device, init hardware */
2851 retval = startup(info);
2856 retval = block_til_ready(tty, filp, info);
2858 if (debug_level >= DEBUG_LEVEL_INFO)
2859 printk("%s(%d):block_til_ready(%s) returned %d\n",
2860 __FILE__,__LINE__, info->device_name, retval);
2864 if (debug_level >= DEBUG_LEVEL_INFO)
2865 printk("%s(%d):mgslpc_open(%s) success\n",
2866 __FILE__,__LINE__, info->device_name);
2871 if (tty->count == 1)
2872 info->tty = 0; /* tty layer will release tty struct */
2881 * /proc fs routines....
2884 static inline int line_info(char *buf, MGSLPC_INFO *info)
2888 unsigned long flags;
2890 ret = sprintf(buf, "%s:io:%04X irq:%d",
2891 info->device_name, info->io_base, info->irq_level);
2893 /* output current serial signal states */
2894 spin_lock_irqsave(&info->lock,flags);
2896 spin_unlock_irqrestore(&info->lock,flags);
2900 if (info->serial_signals & SerialSignal_RTS)
2901 strcat(stat_buf, "|RTS");
2902 if (info->serial_signals & SerialSignal_CTS)
2903 strcat(stat_buf, "|CTS");
2904 if (info->serial_signals & SerialSignal_DTR)
2905 strcat(stat_buf, "|DTR");
2906 if (info->serial_signals & SerialSignal_DSR)
2907 strcat(stat_buf, "|DSR");
2908 if (info->serial_signals & SerialSignal_DCD)
2909 strcat(stat_buf, "|CD");
2910 if (info->serial_signals & SerialSignal_RI)
2911 strcat(stat_buf, "|RI");
2913 if (info->params.mode == MGSL_MODE_HDLC) {
2914 ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
2915 info->icount.txok, info->icount.rxok);
2916 if (info->icount.txunder)
2917 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
2918 if (info->icount.txabort)
2919 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
2920 if (info->icount.rxshort)
2921 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
2922 if (info->icount.rxlong)
2923 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
2924 if (info->icount.rxover)
2925 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
2926 if (info->icount.rxcrc)
2927 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxcrc);
2929 ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
2930 info->icount.tx, info->icount.rx);
2931 if (info->icount.frame)
2932 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2933 if (info->icount.parity)
2934 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2935 if (info->icount.brk)
2936 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2937 if (info->icount.overrun)
2938 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2941 /* Append serial signal status to end */
2942 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2944 ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2945 info->tx_active,info->bh_requested,info->bh_running,
2951 /* Called to print information about devices
2953 int mgslpc_read_proc(char *page, char **start, off_t off, int count,
2954 int *eof, void *data)
2960 len += sprintf(page, "synclink driver:%s\n", driver_version);
2962 info = mgslpc_device_list;
2964 l = line_info(page + len, info);
2966 if (len+begin > off+count)
2968 if (len+begin < off) {
2972 info = info->next_device;
2977 if (off >= len+begin)
2979 *start = page + (off-begin);
2980 return ((count < begin+len-off) ? count : begin+len-off);
2983 int rx_alloc_buffers(MGSLPC_INFO *info)
2985 /* each buffer has header and data */
2986 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2988 /* calculate total allocation size for 8 buffers */
2989 info->rx_buf_total_size = info->rx_buf_size * 8;
2991 /* limit total allocated memory */
2992 if (info->rx_buf_total_size > 0x10000)
2993 info->rx_buf_total_size = 0x10000;
2995 /* calculate number of buffers */
2996 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2998 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2999 if (info->rx_buf == NULL)
3002 rx_reset_buffers(info);
3006 void rx_free_buffers(MGSLPC_INFO *info)
3009 kfree(info->rx_buf);
3010 info->rx_buf = NULL;
3013 int claim_resources(MGSLPC_INFO *info)
3015 if (rx_alloc_buffers(info) < 0 ) {
3016 printk( "Cant allocate rx buffer %s\n", info->device_name);
3017 release_resources(info);
3023 void release_resources(MGSLPC_INFO *info)
3025 if (debug_level >= DEBUG_LEVEL_INFO)
3026 printk("release_resources(%s)\n", info->device_name);
3027 rx_free_buffers(info);
3030 /* Add the specified device instance data structure to the
3031 * global linked list of devices and increment the device count.
3033 * Arguments: info pointer to device instance data
3035 void mgslpc_add_device(MGSLPC_INFO *info)
3037 info->next_device = NULL;
3038 info->line = mgslpc_device_count;
3039 sprintf(info->device_name,"ttySLP%d",info->line);
3041 if (info->line < MAX_DEVICE_COUNT) {
3042 if (maxframe[info->line])
3043 info->max_frame_size = maxframe[info->line];
3044 info->dosyncppp = dosyncppp[info->line];
3047 mgslpc_device_count++;
3049 if (!mgslpc_device_list)
3050 mgslpc_device_list = info;
3052 MGSLPC_INFO *current_dev = mgslpc_device_list;
3053 while( current_dev->next_device )
3054 current_dev = current_dev->next_device;
3055 current_dev->next_device = info;
3058 if (info->max_frame_size < 4096)
3059 info->max_frame_size = 4096;
3060 else if (info->max_frame_size > 65535)
3061 info->max_frame_size = 65535;
3063 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
3064 info->device_name, info->io_base, info->irq_level);
3067 #ifdef CONFIG_SYNCLINK_SYNCPPP
3069 if (info->dosyncppp)
3071 mgslpc_sppp_init(info);
3075 void mgslpc_remove_device(MGSLPC_INFO *remove_info)
3077 MGSLPC_INFO *info = mgslpc_device_list;
3078 MGSLPC_INFO *last = NULL;
3081 if (info == remove_info) {
3083 last->next_device = info->next_device;
3085 mgslpc_device_list = info->next_device;
3086 #ifdef CONFIG_SYNCLINK_SYNCPPP
3087 if (info->dosyncppp)
3088 mgslpc_sppp_delete(info);
3090 release_resources(info);
3092 mgslpc_device_count--;
3096 info = info->next_device;
3100 static struct pcmcia_driver mgslpc_driver = {
3101 .owner = THIS_MODULE,
3103 .name = "synclink_cs",
3105 .attach = mgslpc_attach,
3106 .detach = mgslpc_detach,
3109 static struct tty_operations mgslpc_ops = {
3110 .open = mgslpc_open,
3111 .close = mgslpc_close,
3112 .write = mgslpc_write,
3113 .put_char = mgslpc_put_char,
3114 .flush_chars = mgslpc_flush_chars,
3115 .write_room = mgslpc_write_room,
3116 .chars_in_buffer = mgslpc_chars_in_buffer,
3117 .flush_buffer = mgslpc_flush_buffer,
3118 .ioctl = mgslpc_ioctl,
3119 .throttle = mgslpc_throttle,
3120 .unthrottle = mgslpc_unthrottle,
3121 .send_xchar = mgslpc_send_xchar,
3122 .break_ctl = mgslpc_break,
3123 .wait_until_sent = mgslpc_wait_until_sent,
3124 .read_proc = mgslpc_read_proc,
3125 .set_termios = mgslpc_set_termios,
3127 .start = tx_release,
3128 .hangup = mgslpc_hangup,
3129 .tiocmget = tiocmget,
3130 .tiocmset = tiocmset,
3133 static int __init synclink_cs_init(void)
3137 if (break_on_load) {
3138 mgslpc_get_text_ptr();
3142 printk("%s %s\n", driver_name, driver_version);
3144 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
3148 error = pcmcia_register_driver(&mgslpc_driver);
3150 put_tty_driver(serial_driver);
3154 /* Initialize the tty_driver structure */
3156 serial_driver->owner = THIS_MODULE;
3157 serial_driver->driver_name = "synclink_cs";
3158 serial_driver->name = "ttySLP";
3159 serial_driver->major = ttymajor;
3160 serial_driver->minor_start = 64;
3161 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3162 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3163 serial_driver->init_termios = tty_std_termios;
3164 serial_driver->init_termios.c_cflag =
3165 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3166 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3167 tty_set_operations(serial_driver, &mgslpc_ops);
3169 if (tty_register_driver(serial_driver) < 0)
3170 printk("%s(%d):Couldn't register serial driver\n",
3173 printk("%s %s, tty major#%d\n",
3174 driver_name, driver_version,
3175 serial_driver->major);
3180 static void __exit synclink_cs_exit(void)
3184 printk("Unloading %s: version %s\n", driver_name, driver_version);
3186 while(mgslpc_device_list)
3187 mgslpc_remove_device(mgslpc_device_list);
3189 if ((rc = tty_unregister_driver(serial_driver)))
3190 printk("%s(%d) failed to unregister tty driver err=%d\n",
3191 __FILE__,__LINE__,rc);
3192 put_tty_driver(serial_driver);
3194 pcmcia_unregister_driver(&mgslpc_driver);
3196 /* XXX: this really needs to move into generic code.. */
3197 while (dev_list != NULL) {
3198 if (dev_list->state & DEV_CONFIG)
3199 mgslpc_release((u_long)dev_list);
3200 mgslpc_detach(dev_list);
3204 module_init(synclink_cs_init);
3205 module_exit(synclink_cs_exit);
3207 void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
3212 /* note:standard BRG mode is broken in V3.2 chip
3213 * so enhanced mode is always used
3221 for (M = 1; N > 64 && M < 16; M++)
3227 * BGR[7..0] contained in BGR register
3228 * BGR[9..8] contained in CCR2[7..6]
3229 * divisor = (N+1)*2^M
3231 * Note: M *must* not be zero (causes asymetric duty cycle)
3233 write_reg(info, (unsigned char) (channel + BGR),
3234 (unsigned char) ((M << 6) + N));
3235 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
3236 val |= ((M << 4) & 0xc0);
3237 write_reg(info, (unsigned char) (channel + CCR2), val);
3241 /* Enabled the AUX clock output at the specified frequency.
3243 void enable_auxclk(MGSLPC_INFO *info)
3249 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3250 * 05 ADM Address Mode, 0 = no addr recognition
3251 * 04 TMD Timer Mode, 0 = external
3252 * 03 RAC Receiver Active, 0 = inactive
3253 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3254 * 01 TRS Timer Resolution, 1=512
3255 * 00 TLP Test Loop, 0 = no loop
3261 /* channel B RTS is used to enable AUXCLK driver on SP505 */
3262 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3264 write_reg(info, CHB + MODE, val);
3268 * 07 PU Power Up, 1=active, 0=power down
3269 * 06 MCE Master Clock Enable, 1=enabled
3271 * 04..02 SC[2..0] Encoding
3272 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3276 write_reg(info, CHB + CCR0, 0xc0);
3280 * 07 SFLG Shared Flag, 0 = disable shared flags
3281 * 06 GALP Go Active On Loop, 0 = not used
3282 * 05 GLP Go On Loop, 0 = not used
3283 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3284 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3285 * 02..00 CM[2..0] Clock Mode
3289 write_reg(info, CHB + CCR1, 0x17);
3293 * 07..06 BGR[9..8] Baud rate bits 9..8
3294 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3295 * 04 SSEL Clock source select, 1=submode b
3296 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3297 * 02 RWX Read/Write Exchange 0=disabled
3298 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3299 * 00 DIV, data inversion 0=disabled, 1=enabled
3303 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3304 write_reg(info, CHB + CCR2, 0x38);
3306 write_reg(info, CHB + CCR2, 0x30);
3310 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3311 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3312 * 05 TST1 Test Pin, 0=normal operation
3313 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3314 * 03..02 Reserved, must be 0
3315 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3319 write_reg(info, CHB + CCR4, 0x50);
3321 /* if auxclk not enabled, set internal BRG so
3322 * CTS transitions can be detected (requires TxC)
3324 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3325 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3327 mgslpc_set_rate(info, CHB, 921600);
3330 static void loopback_enable(MGSLPC_INFO *info)
3334 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3335 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3336 write_reg(info, CHA + CCR1, val);
3338 /* CCR2:04 SSEL Clock source select, 1=submode b */
3339 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3340 write_reg(info, CHA + CCR2, val);
3342 /* set LinkSpeed if available, otherwise default to 2Mbps */
3343 if (info->params.clock_speed)
3344 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3346 mgslpc_set_rate(info, CHA, 1843200);
3348 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3349 val = read_reg(info, CHA + MODE) | BIT0;
3350 write_reg(info, CHA + MODE, val);
3353 void hdlc_mode(MGSLPC_INFO *info)
3356 unsigned char clkmode, clksubmode;
3358 /* disable all interrupts */
3359 irq_disable(info, CHA, 0xffff);
3360 irq_disable(info, CHB, 0xffff);
3361 port_irq_disable(info, 0xff);
3363 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3364 clkmode = clksubmode = 0;
3365 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3366 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3367 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3369 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3370 && info->params.flags & HDLC_FLAG_TXC_BRG) {
3371 /* clock mode 7b, rcv = BRG, xmt = BRG */
3374 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3375 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3376 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3380 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3383 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3384 /* clock mode 0b, rcv = RxC, xmt = BRG */
3390 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3391 * 05 ADM Address Mode, 0 = no addr recognition
3392 * 04 TMD Timer Mode, 0 = external
3393 * 03 RAC Receiver Active, 0 = inactive
3394 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3395 * 01 TRS Timer Resolution, 1=512
3396 * 00 TLP Test Loop, 0 = no loop
3401 if (info->params.loopback)
3404 /* preserve RTS state */
3405 if (info->serial_signals & SerialSignal_RTS)
3407 write_reg(info, CHA + MODE, val);
3411 * 07 PU Power Up, 1=active, 0=power down
3412 * 06 MCE Master Clock Enable, 1=enabled
3414 * 04..02 SC[2..0] Encoding
3415 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3420 switch (info->params.encoding)
3422 case HDLC_ENCODING_NRZI:
3425 case HDLC_ENCODING_BIPHASE_SPACE:
3428 case HDLC_ENCODING_BIPHASE_MARK:
3431 case HDLC_ENCODING_BIPHASE_LEVEL:
3433 break; // Manchester
3435 write_reg(info, CHA + CCR0, val);
3439 * 07 SFLG Shared Flag, 0 = disable shared flags
3440 * 06 GALP Go Active On Loop, 0 = not used
3441 * 05 GLP Go On Loop, 0 = not used
3442 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3443 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3444 * 02..00 CM[2..0] Clock Mode
3448 val = 0x10 + clkmode;
3449 write_reg(info, CHA + CCR1, val);
3453 * 07..06 BGR[9..8] Baud rate bits 9..8
3454 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3455 * 04 SSEL Clock source select, 1=submode b
3456 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3457 * 02 RWX Read/Write Exchange 0=disabled
3458 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3459 * 00 DIV, data inversion 0=disabled, 1=enabled
3464 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3465 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3469 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3471 if (info->params.encoding == HDLC_ENCODING_NRZB)
3473 write_reg(info, CHA + CCR2, val);
3477 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3478 * 05 EPT Enable preamble transmission, 1=enabled
3479 * 04 RADD Receive address pushed to FIFO, 0=disabled
3480 * 03 CRL CRC Reset Level, 0=FFFF
3481 * 02 RCRC Rx CRC 0=On 1=Off
3482 * 01 TCRC Tx CRC 0=On 1=Off
3483 * 00 PSD DPLL Phase Shift Disable
3488 if (info->params.crc_type == HDLC_CRC_NONE)
3490 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3492 switch (info->params.preamble_length)
3494 case HDLC_PREAMBLE_LENGTH_16BITS:
3497 case HDLC_PREAMBLE_LENGTH_32BITS:
3500 case HDLC_PREAMBLE_LENGTH_64BITS:
3504 write_reg(info, CHA + CCR3, val);
3506 /* PRE - Preamble pattern */
3508 switch (info->params.preamble)
3510 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3511 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3512 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3513 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3515 write_reg(info, CHA + PRE, val);
3519 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3520 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3521 * 05 TST1 Test Pin, 0=normal operation
3522 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3523 * 03..02 Reserved, must be 0
3524 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3529 write_reg(info, CHA + CCR4, val);
3530 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3531 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3533 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3535 /* RLCR Receive length check register
3537 * 7 1=enable receive length check
3538 * 6..0 Max frame length = (RL + 1) * 32
3540 write_reg(info, CHA + RLCR, 0);
3542 /* XBCH Transmit Byte Count High
3544 * 07 DMA mode, 0 = interrupt driven
3545 * 06 NRM, 0=ABM (ignored)
3546 * 05 CAS Carrier Auto Start
3547 * 04 XC Transmit Continuously (ignored)
3548 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3553 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3555 write_reg(info, CHA + XBCH, val);
3556 enable_auxclk(info);
3557 if (info->params.loopback || info->testing_irq)
3558 loopback_enable(info);
3559 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3561 irq_enable(info, CHB, IRQ_CTS);
3562 /* PVR[3] 1=AUTO CTS active */
3563 set_reg_bits(info, CHA + PVR, BIT3);
3565 clear_reg_bits(info, CHA + PVR, BIT3);
3567 irq_enable(info, CHA,
3568 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3569 IRQ_UNDERRUN + IRQ_TXFIFO);
3570 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3571 wait_command_complete(info, CHA);
3572 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3574 /* Master clock mode enabled above to allow reset commands
3575 * to complete even if no data clocks are present.
3577 * Disable master clock mode for normal communications because
3578 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3579 * IRQ when in master clock mode.
3581 * Leave master clock mode enabled for IRQ test because the
3582 * timer IRQ used by the test can only happen in master clock mode.
3584 if (!info->testing_irq)
3585 clear_reg_bits(info, CHA + CCR0, BIT6);
3593 void rx_stop(MGSLPC_INFO *info)
3595 if (debug_level >= DEBUG_LEVEL_ISR)
3596 printk("%s(%d):rx_stop(%s)\n",
3597 __FILE__,__LINE__, info->device_name );
3599 /* MODE:03 RAC Receiver Active, 0=inactive */
3600 clear_reg_bits(info, CHA + MODE, BIT3);
3602 info->rx_enabled = 0;
3603 info->rx_overflow = 0;
3606 void rx_start(MGSLPC_INFO *info)
3608 if (debug_level >= DEBUG_LEVEL_ISR)
3609 printk("%s(%d):rx_start(%s)\n",
3610 __FILE__,__LINE__, info->device_name );
3612 rx_reset_buffers(info);
3613 info->rx_enabled = 0;
3614 info->rx_overflow = 0;
3616 /* MODE:03 RAC Receiver Active, 1=active */
3617 set_reg_bits(info, CHA + MODE, BIT3);
3619 info->rx_enabled = 1;
3622 void tx_start(MGSLPC_INFO *info)
3624 if (debug_level >= DEBUG_LEVEL_ISR)
3625 printk("%s(%d):tx_start(%s)\n",
3626 __FILE__,__LINE__, info->device_name );
3628 if (info->tx_count) {
3629 /* If auto RTS enabled and RTS is inactive, then assert */
3630 /* RTS and set a flag indicating that the driver should */
3631 /* negate RTS when the transmission completes. */
3632 info->drop_rts_on_tx_done = 0;
3634 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3636 if (!(info->serial_signals & SerialSignal_RTS)) {
3637 info->serial_signals |= SerialSignal_RTS;
3639 info->drop_rts_on_tx_done = 1;
3643 if (info->params.mode == MGSL_MODE_ASYNC) {
3644 if (!info->tx_active) {
3645 info->tx_active = 1;
3649 info->tx_active = 1;
3651 info->tx_timer.expires = jiffies + jiffies_from_ms(5000);
3652 add_timer(&info->tx_timer);
3656 if (!info->tx_enabled)
3657 info->tx_enabled = 1;
3660 void tx_stop(MGSLPC_INFO *info)
3662 if (debug_level >= DEBUG_LEVEL_ISR)
3663 printk("%s(%d):tx_stop(%s)\n",
3664 __FILE__,__LINE__, info->device_name );
3666 del_timer(&info->tx_timer);
3668 info->tx_enabled = 0;
3669 info->tx_active = 0;
3672 /* Reset the adapter to a known state and prepare it for further use.
3674 void reset_device(MGSLPC_INFO *info)
3676 /* power up both channels (set BIT7) */
3677 write_reg(info, CHA + CCR0, 0x80);
3678 write_reg(info, CHB + CCR0, 0x80);
3679 write_reg(info, CHA + MODE, 0);
3680 write_reg(info, CHB + MODE, 0);
3682 /* disable all interrupts */
3683 irq_disable(info, CHA, 0xffff);
3684 irq_disable(info, CHB, 0xffff);
3685 port_irq_disable(info, 0xff);
3687 /* PCR Port Configuration Register
3689 * 07..04 DEC[3..0] Serial I/F select outputs
3690 * 03 output, 1=AUTO CTS control enabled
3691 * 02 RI Ring Indicator input 0=active
3692 * 01 DSR input 0=active
3693 * 00 DTR output 0=active
3697 write_reg(info, PCR, 0x06);
3699 /* PVR Port Value Register
3701 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3702 * 03 AUTO CTS output 1=enabled
3703 * 02 RI Ring Indicator input
3705 * 00 DTR output (1=inactive)
3709 // write_reg(info, PVR, PVR_DTR);
3711 /* IPC Interrupt Port Configuration
3713 * 07 VIS 1=Masked interrupts visible
3714 * 06..05 Reserved, 0
3715 * 04..03 SLA Slave address, 00 ignored
3716 * 02 CASM Cascading Mode, 1=daisy chain
3717 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3721 write_reg(info, IPC, 0x05);
3724 void async_mode(MGSLPC_INFO *info)
3728 /* disable all interrupts */
3729 irq_disable(info, CHA, 0xffff);
3730 irq_disable(info, CHB, 0xffff);
3731 port_irq_disable(info, 0xff);
3736 * 06 FRTS RTS State, 0=active
3737 * 05 FCTS Flow Control on CTS
3738 * 04 FLON Flow Control Enable
3739 * 03 RAC Receiver Active, 0 = inactive
3740 * 02 RTS 0=Auto RTS, 1=manual RTS
3741 * 01 TRS Timer Resolution, 1=512
3742 * 00 TLP Test Loop, 0 = no loop
3747 if (info->params.loopback)
3750 /* preserve RTS state */
3751 if (!(info->serial_signals & SerialSignal_RTS))
3753 write_reg(info, CHA + MODE, val);
3757 * 07 PU Power Up, 1=active, 0=power down
3758 * 06 MCE Master Clock Enable, 1=enabled
3760 * 04..02 SC[2..0] Encoding, 000=NRZ
3761 * 01..00 SM[1..0] Serial Mode, 11=Async
3765 write_reg(info, CHA + CCR0, 0x83);
3769 * 07..05 Reserved, 0
3770 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3771 * 03 BCR Bit Clock Rate, 1=16x
3772 * 02..00 CM[2..0] Clock Mode, 111=BRG
3776 write_reg(info, CHA + CCR1, 0x1f);
3780 * 07..06 BGR[9..8] Baud rate bits 9..8
3781 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3782 * 04 SSEL Clock source select, 1=submode b
3783 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3784 * 02 RWX Read/Write Exchange 0=disabled
3786 * 00 DIV, data inversion 0=disabled, 1=enabled
3790 write_reg(info, CHA + CCR2, 0x10);
3794 * 07..01 Reserved, 0
3795 * 00 PSD DPLL Phase Shift Disable
3799 write_reg(info, CHA + CCR3, 0);
3803 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3804 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3805 * 05 TST1 Test Pin, 0=normal operation
3806 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3807 * 03..00 Reserved, must be 0
3811 write_reg(info, CHA + CCR4, 0x50);
3812 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3817 * 06 XBRK transmit break, 0=normal operation
3818 * 05 Stop bits (0=1, 1=2)
3819 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3820 * 02 PAREN Parity Enable
3821 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3825 if (info->params.data_bits != 8)
3826 val |= BIT0; /* 7 bits */
3827 if (info->params.stop_bits != 1)
3829 if (info->params.parity != ASYNC_PARITY_NONE)
3831 val |= BIT2; /* Parity enable */
3832 if (info->params.parity == ASYNC_PARITY_ODD)
3837 write_reg(info, CHA + DAFO, val);
3839 /* RFC Rx FIFO Control
3842 * 06 DPS, 1=parity bit not stored in data byte
3843 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3844 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3845 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3847 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3851 write_reg(info, CHA + RFC, 0x5c);
3853 /* RLCR Receive length check register
3855 * Max frame length = (RL + 1) * 32
3857 write_reg(info, CHA + RLCR, 0);
3859 /* XBCH Transmit Byte Count High
3861 * 07 DMA mode, 0 = interrupt driven
3862 * 06 NRM, 0=ABM (ignored)
3863 * 05 CAS Carrier Auto Start
3864 * 04 XC Transmit Continuously (ignored)
3865 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3870 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3872 write_reg(info, CHA + XBCH, val);
3873 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3874 irq_enable(info, CHA, IRQ_CTS);
3876 /* MODE:03 RAC Receiver Active, 1=active */
3877 set_reg_bits(info, CHA + MODE, BIT3);
3878 enable_auxclk(info);
3879 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3880 irq_enable(info, CHB, IRQ_CTS);
3881 /* PVR[3] 1=AUTO CTS active */
3882 set_reg_bits(info, CHA + PVR, BIT3);
3884 clear_reg_bits(info, CHA + PVR, BIT3);
3885 irq_enable(info, CHA,
3886 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3887 IRQ_ALLSENT + IRQ_TXFIFO);
3888 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3889 wait_command_complete(info, CHA);
3890 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3893 /* Set the HDLC idle mode for the transmitter.
3895 void tx_set_idle(MGSLPC_INFO *info)
3897 /* Note: ESCC2 only supports flags and one idle modes */
3898 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3899 set_reg_bits(info, CHA + CCR1, BIT3);
3901 clear_reg_bits(info, CHA + CCR1, BIT3);
3904 /* get state of the V24 status (input) signals.
3906 void get_signals(MGSLPC_INFO *info)
3908 unsigned char status = 0;
3910 /* preserve DTR and RTS */
3911 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3913 if (read_reg(info, CHB + VSTR) & BIT7)
3914 info->serial_signals |= SerialSignal_DCD;
3915 if (read_reg(info, CHB + STAR) & BIT1)
3916 info->serial_signals |= SerialSignal_CTS;
3918 status = read_reg(info, CHA + PVR);
3919 if (!(status & PVR_RI))
3920 info->serial_signals |= SerialSignal_RI;
3921 if (!(status & PVR_DSR))
3922 info->serial_signals |= SerialSignal_DSR;
3925 /* Set the state of DTR and RTS based on contents of
3926 * serial_signals member of device extension.
3928 void set_signals(MGSLPC_INFO *info)
3932 val = read_reg(info, CHA + MODE);
3933 if (info->params.mode == MGSL_MODE_ASYNC) {
3934 if (info->serial_signals & SerialSignal_RTS)
3939 if (info->serial_signals & SerialSignal_RTS)
3944 write_reg(info, CHA + MODE, val);
3946 if (info->serial_signals & SerialSignal_DTR)
3947 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3949 set_reg_bits(info, CHA + PVR, PVR_DTR);
3952 void rx_reset_buffers(MGSLPC_INFO *info)
3959 info->rx_frame_count = 0;
3960 for (i=0 ; i < info->rx_buf_count ; i++) {
3961 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3962 buf->status = buf->count = 0;
3966 /* Attempt to return a received HDLC frame
3967 * Only frames received without errors are returned.
3969 * Returns 1 if frame returned, otherwise 0
3971 int rx_get_frame(MGSLPC_INFO *info)
3973 unsigned short status;
3975 unsigned int framesize = 0;
3976 unsigned long flags;
3977 struct tty_struct *tty = info->tty;
3978 int return_frame = 0;
3980 if (info->rx_frame_count == 0)
3983 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3985 status = buf->status;
3987 /* 07 VFR 1=valid frame
3988 * 06 RDO 1=data overrun
3989 * 05 CRC 1=OK, 0=error
3990 * 04 RAB 1=frame aborted
3992 if ((status & 0xf0) != 0xA0) {
3993 if (!(status & BIT7) || (status & BIT4))
3994 info->icount.rxabort++;
3995 else if (status & BIT6)
3996 info->icount.rxover++;
3997 else if (!(status & BIT5)) {
3998 info->icount.rxcrc++;
3999 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
4003 #ifdef CONFIG_SYNCLINK_SYNCPPP
4004 info->netstats.rx_errors++;
4005 info->netstats.rx_frame_errors++;
4011 framesize = buf->count;
4013 if (debug_level >= DEBUG_LEVEL_BH)
4014 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
4015 __FILE__,__LINE__,info->device_name,status,framesize);
4017 if (debug_level >= DEBUG_LEVEL_DATA)
4018 trace_block(info, buf->data, framesize, 0);
4021 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
4022 framesize+1 > info->max_frame_size) ||
4023 framesize > info->max_frame_size)
4024 info->icount.rxlong++;
4027 info->icount.rxok++;
4029 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4030 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
4034 #ifdef CONFIG_SYNCLINK_SYNCPPP
4035 if (info->netcount) {
4036 /* pass frame to syncppp device */
4037 mgslpc_sppp_rx_done(info, buf->data, framesize);
4042 /* Call the line discipline receive callback directly. */
4043 if (tty && tty->ldisc.receive_buf)
4044 tty->ldisc.receive_buf(tty, buf->data, info->flag_buf, framesize);
4049 spin_lock_irqsave(&info->lock,flags);
4050 buf->status = buf->count = 0;
4051 info->rx_frame_count--;
4053 if (info->rx_get >= info->rx_buf_count)
4055 spin_unlock_irqrestore(&info->lock,flags);
4060 BOOLEAN register_test(MGSLPC_INFO *info)
4062 static unsigned char patterns[] =
4063 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
4064 static unsigned int count = sizeof(patterns) / sizeof(patterns[0]);
4067 unsigned long flags;
4069 spin_lock_irqsave(&info->lock,flags);
4072 for (i = 0; i < count; i++) {
4073 write_reg(info, XAD1, patterns[i]);
4074 write_reg(info, XAD2, patterns[(i + 1) % count]);
4075 if ((read_reg(info, XAD1) != patterns[i]) ||
4076 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
4082 spin_unlock_irqrestore(&info->lock,flags);
4086 BOOLEAN irq_test(MGSLPC_INFO *info)
4088 unsigned long end_time;
4089 unsigned long flags;
4091 spin_lock_irqsave(&info->lock,flags);
4094 info->testing_irq = TRUE;
4097 info->irq_occurred = FALSE;
4099 /* init hdlc mode */
4101 irq_enable(info, CHA, IRQ_TIMER);
4102 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
4103 issue_command(info, CHA, CMD_START_TIMER);
4105 spin_unlock_irqrestore(&info->lock,flags);
4108 while(end_time-- && !info->irq_occurred) {
4109 set_current_state(TASK_INTERRUPTIBLE);
4110 schedule_timeout(jiffies_from_ms(10));
4113 info->testing_irq = FALSE;
4115 spin_lock_irqsave(&info->lock,flags);
4117 spin_unlock_irqrestore(&info->lock,flags);
4119 return info->irq_occurred ? TRUE : FALSE;
4122 int adapter_test(MGSLPC_INFO *info)
4124 if (!register_test(info)) {
4125 info->init_error = DiagStatus_AddressFailure;
4126 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
4127 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
4131 if (!irq_test(info)) {
4132 info->init_error = DiagStatus_IrqFailure;
4133 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
4134 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
4138 if (debug_level >= DEBUG_LEVEL_INFO)
4139 printk("%s(%d):device %s passed diagnostics\n",
4140 __FILE__,__LINE__,info->device_name);
4144 void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
4149 printk("%s tx data:\n",info->device_name);
4151 printk("%s rx data:\n",info->device_name);
4159 for(i=0;i<linecount;i++)
4160 printk("%02X ",(unsigned char)data[i]);
4163 for(i=0;i<linecount;i++) {
4164 if (data[i]>=040 && data[i]<=0176)
4165 printk("%c",data[i]);
4176 /* HDLC frame time out
4177 * update stats and do tx completion processing
4179 void tx_timeout(unsigned long context)
4181 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
4182 unsigned long flags;
4184 if ( debug_level >= DEBUG_LEVEL_INFO )
4185 printk( "%s(%d):tx_timeout(%s)\n",
4186 __FILE__,__LINE__,info->device_name);
4187 if(info->tx_active &&
4188 info->params.mode == MGSL_MODE_HDLC) {
4189 info->icount.txtimeout++;
4191 spin_lock_irqsave(&info->lock,flags);
4192 info->tx_active = 0;
4193 info->tx_count = info->tx_put = info->tx_get = 0;
4195 spin_unlock_irqrestore(&info->lock,flags);
4197 #ifdef CONFIG_SYNCLINK_SYNCPPP
4199 mgslpc_sppp_tx_done(info);
4205 #ifdef CONFIG_SYNCLINK_SYNCPPP
4206 /* syncppp net device routines
4209 static void mgslpc_setup(struct net_device *dev)
4211 dev->open = mgslpc_sppp_open;
4212 dev->stop = mgslpc_sppp_close;
4213 dev->hard_start_xmit = mgslpc_sppp_tx;
4214 dev->do_ioctl = mgslpc_sppp_ioctl;
4215 dev->get_stats = mgslpc_net_stats;
4216 dev->tx_timeout = mgslpc_sppp_tx_timeout;
4217 dev->watchdog_timeo = 10*HZ;
4220 void mgslpc_sppp_init(MGSLPC_INFO *info)
4222 struct net_device *d;
4224 sprintf(info->netname,"mgslp%d",info->line);
4226 d = alloc_netdev(0, info->netname, mgslpc_setup);
4228 printk(KERN_WARNING "%s: alloc_netdev failed.\n",
4233 info->if_ptr = &info->pppdev;
4234 info->netdev = info->pppdev.dev = d;
4236 d->base_addr = info->io_base;
4237 d->irq = info->irq_level;
4240 sppp_attach(&info->pppdev);
4243 if (register_netdev(d)) {
4244 printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
4245 sppp_detach(info->netdev);
4246 info->netdev = NULL;
4247 info->pppdev.dev = NULL;
4252 if (debug_level >= DEBUG_LEVEL_INFO)
4253 printk("mgslpc_sppp_init()\n");
4256 void mgslpc_sppp_delete(MGSLPC_INFO *info)
4258 if (debug_level >= DEBUG_LEVEL_INFO)
4259 printk("mgslpc_sppp_delete(%s)\n",info->netname);
4260 unregister_netdev(info->netdev);
4261 sppp_detach(info->netdev);
4262 free_netdev(info->netdev);
4263 info->netdev = NULL;
4264 info->pppdev.dev = NULL;
4267 int mgslpc_sppp_open(struct net_device *d)
4269 MGSLPC_INFO *info = d->priv;
4271 unsigned long flags;
4273 if (debug_level >= DEBUG_LEVEL_INFO)
4274 printk("mgslpc_sppp_open(%s)\n",info->netname);
4276 spin_lock_irqsave(&info->netlock, flags);
4277 if (info->count != 0 || info->netcount != 0) {
4278 printk(KERN_WARNING "%s: sppp_open returning busy\n", info->netname);
4279 spin_unlock_irqrestore(&info->netlock, flags);
4283 spin_unlock_irqrestore(&info->netlock, flags);
4285 /* claim resources and init adapter */
4286 if ((err = startup(info)) != 0)
4289 /* allow syncppp module to do open processing */
4290 if ((err = sppp_open(d)) != 0) {
4295 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4296 mgslpc_program_hw(info);
4298 d->trans_start = jiffies;
4299 netif_start_queue(d);
4303 spin_lock_irqsave(&info->netlock, flags);
4305 spin_unlock_irqrestore(&info->netlock, flags);
4309 void mgslpc_sppp_tx_timeout(struct net_device *dev)
4311 MGSLPC_INFO *info = dev->priv;
4312 unsigned long flags;
4314 if (debug_level >= DEBUG_LEVEL_INFO)
4315 printk("mgslpc_sppp_tx_timeout(%s)\n",info->netname);
4317 info->netstats.tx_errors++;
4318 info->netstats.tx_aborted_errors++;
4320 spin_lock_irqsave(&info->lock,flags);
4322 spin_unlock_irqrestore(&info->lock,flags);
4324 netif_wake_queue(dev);
4327 int mgslpc_sppp_tx(struct sk_buff *skb, struct net_device *dev)
4329 MGSLPC_INFO *info = dev->priv;
4330 unsigned long flags;
4332 if (debug_level >= DEBUG_LEVEL_INFO)
4333 printk("mgslpc_sppp_tx(%s)\n",info->netname);
4335 netif_stop_queue(dev);
4337 info->tx_count = skb->len;
4339 memcpy(info->tx_buf, skb->data, skb->len);
4341 info->tx_put = info->tx_count = skb->len;
4343 info->netstats.tx_packets++;
4344 info->netstats.tx_bytes += skb->len;
4347 dev->trans_start = jiffies;
4349 spin_lock_irqsave(&info->lock,flags);
4350 if (!info->tx_active)
4352 spin_unlock_irqrestore(&info->lock,flags);
4357 int mgslpc_sppp_close(struct net_device *d)
4359 MGSLPC_INFO *info = d->priv;
4360 unsigned long flags;
4362 if (debug_level >= DEBUG_LEVEL_INFO)
4363 printk("mgslpc_sppp_close(%s)\n",info->netname);
4365 /* shutdown adapter and release resources */
4368 /* allow syncppp to do close processing */
4370 netif_stop_queue(d);
4372 spin_lock_irqsave(&info->netlock, flags);
4374 spin_unlock_irqrestore(&info->netlock, flags);
4378 void mgslpc_sppp_rx_done(MGSLPC_INFO *info, char *buf, int size)
4380 struct sk_buff *skb = dev_alloc_skb(size);
4381 if (debug_level >= DEBUG_LEVEL_INFO)
4382 printk("mgslpc_sppp_rx_done(%s)\n",info->netname);
4384 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n",
4386 info->netstats.rx_dropped++;
4390 memcpy(skb_put(skb, size),buf,size);
4392 skb->protocol = htons(ETH_P_WAN_PPP);
4393 skb->dev = info->netdev;
4394 skb->mac.raw = skb->data;
4395 info->netstats.rx_packets++;
4396 info->netstats.rx_bytes += size;
4398 info->netdev->trans_start = jiffies;
4401 void mgslpc_sppp_tx_done(MGSLPC_INFO *info)
4403 if (netif_queue_stopped(info->netdev))
4404 netif_wake_queue(info->netdev);
4407 struct net_device_stats *mgslpc_net_stats(struct net_device *dev)
4409 MGSLPC_INFO *info = dev->priv;
4410 if (debug_level >= DEBUG_LEVEL_INFO)
4411 printk("mgslpc_net_stats(%s)\n",info->netname);
4412 return &info->netstats;
4415 int mgslpc_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4417 MGSLPC_INFO *info = dev->priv;
4418 if (debug_level >= DEBUG_LEVEL_INFO)
4419 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
4420 info->netname, cmd );
4421 return sppp_do_ioctl(dev, ifr, cmd);
4424 #endif /* ifdef CONFIG_SYNCLINK_SYNCPPP */