2 * linux/drivers/char/pcmcia/synclink_cs.c
4 * $Id: synclink_cs.c,v 4.22 2004/06/01 20:27:46 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 *user_icount);
442 static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
443 static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params);
444 static int get_txidle(MGSLPC_INFO *info, int __user *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 __user *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.22 $";
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)
526 return mgslpc_get_text_ptr;
529 static dev_link_t *mgslpc_attach(void)
533 client_reg_t client_reg;
536 if (debug_level >= DEBUG_LEVEL_INFO)
537 printk("mgslpc_attach\n");
539 info = (MGSLPC_INFO *)kmalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
541 printk("Error can't allocate device instance data\n");
545 memset(info, 0, sizeof(MGSLPC_INFO));
546 info->magic = MGSLPC_MAGIC;
547 INIT_WORK(&info->task, bh_handler, info);
548 info->max_frame_size = 4096;
549 info->close_delay = 5*HZ/10;
550 info->closing_wait = 30*HZ;
551 init_waitqueue_head(&info->open_wait);
552 init_waitqueue_head(&info->close_wait);
553 init_waitqueue_head(&info->status_event_wait_q);
554 init_waitqueue_head(&info->event_wait_q);
555 spin_lock_init(&info->lock);
556 spin_lock_init(&info->netlock);
557 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
558 info->idle_mode = HDLC_TXIDLE_FLAGS;
559 info->imra_value = 0xffff;
560 info->imrb_value = 0xffff;
561 info->pim_value = 0xff;
566 /* Initialize the dev_link_t structure */
568 /* Interrupt setup */
569 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
570 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
571 if (irq_list[0] == -1)
572 link->irq.IRQInfo2 = irq_mask;
574 for (i = 0; i < 4; i++)
575 link->irq.IRQInfo2 |= 1 << irq_list[i];
576 link->irq.Handler = NULL;
578 link->conf.Attributes = 0;
580 link->conf.IntType = INT_MEMORY_AND_IO;
582 /* Register with Card Services */
583 link->next = dev_list;
586 client_reg.dev_info = &dev_info;
587 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
588 client_reg.EventMask =
589 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
590 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
591 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
592 client_reg.event_handler = &mgslpc_event;
593 client_reg.Version = 0x0210;
594 client_reg.event_callback_args.client_data = link;
596 ret = pcmcia_register_client(&link->handle, &client_reg);
597 if (ret != CS_SUCCESS) {
598 cs_error(link->handle, RegisterClient, ret);
603 mgslpc_add_device(info);
608 /* Card has been inserted.
611 #define CS_CHECK(fn, ret) \
612 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
614 static void mgslpc_config(dev_link_t *link)
616 client_handle_t handle = link->handle;
617 MGSLPC_INFO *info = link->priv;
620 int last_fn, last_ret;
623 cistpl_cftable_entry_t dflt = { 0 };
624 cistpl_cftable_entry_t *cfg;
626 if (debug_level >= DEBUG_LEVEL_INFO)
627 printk("mgslpc_config(0x%p)\n", link);
629 /* read CONFIG tuple to find its configuration registers */
630 tuple.DesiredTuple = CISTPL_CONFIG;
631 tuple.Attributes = 0;
632 tuple.TupleData = buf;
633 tuple.TupleDataMax = sizeof(buf);
634 tuple.TupleOffset = 0;
635 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
636 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
637 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
638 link->conf.ConfigBase = parse.config.base;
639 link->conf.Present = parse.config.rmask[0];
642 link->state |= DEV_CONFIG;
644 /* Look up the current Vcc */
645 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
646 link->conf.Vcc = conf.Vcc;
648 /* get CIS configuration entry */
650 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
651 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
653 cfg = &(parse.cftable_entry);
654 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
655 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
657 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
661 link->conf.ConfigIndex = cfg->index;
662 link->conf.Attributes |= CONF_ENABLE_IRQ;
664 /* IO window settings */
665 link->io.NumPorts1 = 0;
666 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
667 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
668 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
669 if (!(io->flags & CISTPL_IO_8BIT))
670 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
671 if (!(io->flags & CISTPL_IO_16BIT))
672 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
673 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
674 link->io.BasePort1 = io->win[0].base;
675 link->io.NumPorts1 = io->win[0].len;
676 CS_CHECK(RequestIO, pcmcia_request_io(link->handle, &link->io));
679 link->conf.Attributes = CONF_ENABLE_IRQ;
681 link->conf.IntType = INT_MEMORY_AND_IO;
682 link->conf.ConfigIndex = 8;
683 link->conf.Present = PRESENT_OPTION;
685 link->irq.Attributes |= IRQ_HANDLE_PRESENT;
686 link->irq.Handler = mgslpc_isr;
687 link->irq.Instance = info;
688 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
690 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
692 info->io_base = link->io.BasePort1;
693 info->irq_level = link->irq.AssignedIRQ;
695 /* add to linked list of devices */
696 sprintf(info->node.dev_name, "mgslpc0");
697 info->node.major = info->node.minor = 0;
698 link->dev = &info->node;
700 printk(KERN_INFO "%s: index 0x%02x:",
701 info->node.dev_name, link->conf.ConfigIndex);
702 if (link->conf.Attributes & CONF_ENABLE_IRQ)
703 printk(", irq %d", link->irq.AssignedIRQ);
704 if (link->io.NumPorts1)
705 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
706 link->io.BasePort1+link->io.NumPorts1-1);
709 link->state &= ~DEV_CONFIG_PENDING;
713 cs_error(link->handle, last_fn, last_ret);
714 mgslpc_release((u_long)link);
717 /* Card has been removed.
718 * Unregister device and release PCMCIA configuration.
719 * If device is open, postpone until it is closed.
721 static void mgslpc_release(u_long arg)
723 dev_link_t *link = (dev_link_t *)arg;
725 if (debug_level >= DEBUG_LEVEL_INFO)
726 printk("mgslpc_release(0x%p)\n", link);
728 /* Unlink the device chain */
730 link->state &= ~DEV_CONFIG;
732 pcmcia_release_configuration(link->handle);
733 if (link->io.NumPorts1)
734 pcmcia_release_io(link->handle, &link->io);
735 if (link->irq.AssignedIRQ)
736 pcmcia_release_irq(link->handle, &link->irq);
737 if (link->state & DEV_STALE_LINK)
741 static void mgslpc_detach(dev_link_t *link)
745 if (debug_level >= DEBUG_LEVEL_INFO)
746 printk("mgslpc_detach(0x%p)\n", link);
749 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
750 if (*linkp == link) break;
754 if (link->state & DEV_CONFIG) {
755 /* device is configured/active, mark it so when
756 * release() is called a proper detach() occurs.
758 if (debug_level >= DEBUG_LEVEL_INFO)
759 printk(KERN_DEBUG "synclinkpc: detach postponed, '%s' "
760 "still locked\n", link->dev->dev_name);
761 link->state |= DEV_STALE_LINK;
765 /* Break the link with Card Services */
767 pcmcia_deregister_client(link->handle);
769 /* Unlink device structure, and free it */
771 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
774 static int mgslpc_event(event_t event, int priority,
775 event_callback_args_t *args)
777 dev_link_t *link = args->client_data;
778 MGSLPC_INFO *info = link->priv;
780 if (debug_level >= DEBUG_LEVEL_INFO)
781 printk("mgslpc_event(0x%06x)\n", event);
784 case CS_EVENT_CARD_REMOVAL:
785 link->state &= ~DEV_PRESENT;
786 if (link->state & DEV_CONFIG) {
787 ((MGSLPC_INFO *)link->priv)->stop = 1;
788 mgslpc_release((u_long)link);
791 case CS_EVENT_CARD_INSERTION:
792 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
795 case CS_EVENT_PM_SUSPEND:
796 link->state |= DEV_SUSPEND;
797 /* Fall through... */
798 case CS_EVENT_RESET_PHYSICAL:
799 /* Mark the device as stopped, to block IO until later */
801 if (link->state & DEV_CONFIG)
802 pcmcia_release_configuration(link->handle);
804 case CS_EVENT_PM_RESUME:
805 link->state &= ~DEV_SUSPEND;
806 /* Fall through... */
807 case CS_EVENT_CARD_RESET:
808 if (link->state & DEV_CONFIG)
809 pcmcia_request_configuration(link->handle, &link->conf);
816 static inline int mgslpc_paranoia_check(MGSLPC_INFO *info,
817 char *name, const char *routine)
819 #ifdef MGSLPC_PARANOIA_CHECK
820 static const char *badmagic =
821 "Warning: bad magic number for mgsl struct (%s) in %s\n";
822 static const char *badinfo =
823 "Warning: null mgslpc_info for (%s) in %s\n";
826 printk(badinfo, name, routine);
829 if (info->magic != MGSLPC_MAGIC) {
830 printk(badmagic, name, routine);
841 #define CMD_RXFIFO BIT7 // release current rx FIFO
842 #define CMD_RXRESET BIT6 // receiver reset
843 #define CMD_RXFIFO_READ BIT5
844 #define CMD_START_TIMER BIT4
845 #define CMD_TXFIFO BIT3 // release current tx FIFO
846 #define CMD_TXEOM BIT1 // transmit end message
847 #define CMD_TXRESET BIT0 // transmit reset
849 static BOOLEAN wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
852 unsigned char status;
853 /* wait for command completion */
854 while ((status = read_reg(info, (unsigned char)(channel+STAR)) & BIT2)) {
862 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
864 wait_command_complete(info, channel);
865 write_reg(info, (unsigned char) (channel + CMDR), cmd);
868 static void tx_pause(struct tty_struct *tty)
870 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
873 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
875 if (debug_level >= DEBUG_LEVEL_INFO)
876 printk("tx_pause(%s)\n",info->device_name);
878 spin_lock_irqsave(&info->lock,flags);
879 if (info->tx_enabled)
881 spin_unlock_irqrestore(&info->lock,flags);
884 static void tx_release(struct tty_struct *tty)
886 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
889 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
891 if (debug_level >= DEBUG_LEVEL_INFO)
892 printk("tx_release(%s)\n",info->device_name);
894 spin_lock_irqsave(&info->lock,flags);
895 if (!info->tx_enabled)
897 spin_unlock_irqrestore(&info->lock,flags);
900 /* Return next bottom half action to perform.
901 * or 0 if nothing to do.
903 int bh_action(MGSLPC_INFO *info)
908 spin_lock_irqsave(&info->lock,flags);
910 if (info->pending_bh & BH_RECEIVE) {
911 info->pending_bh &= ~BH_RECEIVE;
913 } else if (info->pending_bh & BH_TRANSMIT) {
914 info->pending_bh &= ~BH_TRANSMIT;
916 } else if (info->pending_bh & BH_STATUS) {
917 info->pending_bh &= ~BH_STATUS;
922 /* Mark BH routine as complete */
923 info->bh_running = 0;
924 info->bh_requested = 0;
927 spin_unlock_irqrestore(&info->lock,flags);
932 void bh_handler(void* Context)
934 MGSLPC_INFO *info = (MGSLPC_INFO*)Context;
940 if (debug_level >= DEBUG_LEVEL_BH)
941 printk( "%s(%d):bh_handler(%s) entry\n",
942 __FILE__,__LINE__,info->device_name);
944 info->bh_running = 1;
946 while((action = bh_action(info)) != 0) {
948 /* Process work item */
949 if ( debug_level >= DEBUG_LEVEL_BH )
950 printk( "%s(%d):bh_handler() work item action=%d\n",
951 __FILE__,__LINE__,action);
956 while(rx_get_frame(info));
965 /* unknown work item ID */
966 printk("Unknown work item ID=%08X!\n", action);
971 if (debug_level >= DEBUG_LEVEL_BH)
972 printk( "%s(%d):bh_handler(%s) exit\n",
973 __FILE__,__LINE__,info->device_name);
976 void bh_transmit(MGSLPC_INFO *info)
978 struct tty_struct *tty = info->tty;
979 if (debug_level >= DEBUG_LEVEL_BH)
980 printk("bh_transmit() entry on %s\n", info->device_name);
983 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
984 tty->ldisc.write_wakeup) {
985 if ( debug_level >= DEBUG_LEVEL_BH )
986 printk( "%s(%d):calling ldisc.write_wakeup on %s\n",
987 __FILE__,__LINE__,info->device_name);
988 (tty->ldisc.write_wakeup)(tty);
990 wake_up_interruptible(&tty->write_wait);
994 void bh_status(MGSLPC_INFO *info)
996 info->ri_chkcount = 0;
997 info->dsr_chkcount = 0;
998 info->dcd_chkcount = 0;
999 info->cts_chkcount = 0;
1002 /* eom: non-zero = end of frame */
1003 void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
1005 unsigned char data[2];
1006 unsigned char fifo_count, read_count, i;
1007 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
1009 if (debug_level >= DEBUG_LEVEL_ISR)
1010 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
1012 if (!info->rx_enabled)
1015 if (info->rx_frame_count >= info->rx_buf_count) {
1016 /* no more free buffers */
1017 issue_command(info, CHA, CMD_RXRESET);
1018 info->pending_bh |= BH_RECEIVE;
1019 info->rx_overflow = 1;
1020 info->icount.buf_overrun++;
1025 /* end of frame, get FIFO count from RBCL register */
1026 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
1032 if (fifo_count == 1) {
1034 data[0] = read_reg(info, CHA + RXFIFO);
1037 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
1039 fifo_count -= read_count;
1040 if (!fifo_count && eom)
1041 buf->status = data[--read_count];
1043 for (i = 0; i < read_count; i++) {
1044 if (buf->count >= info->max_frame_size) {
1045 /* frame too large, reset receiver and reset current buffer */
1046 issue_command(info, CHA, CMD_RXRESET);
1050 *(buf->data + buf->count) = data[i];
1053 } while (fifo_count);
1056 info->pending_bh |= BH_RECEIVE;
1057 info->rx_frame_count++;
1059 if (info->rx_put >= info->rx_buf_count)
1062 issue_command(info, CHA, CMD_RXFIFO);
1065 void rx_ready_async(MGSLPC_INFO *info, int tcd)
1067 unsigned char data, status;
1069 struct tty_struct *tty = info->tty;
1070 struct mgsl_icount *icount = &info->icount;
1073 /* early termination, get FIFO count from RBCL register */
1074 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
1076 /* Zero fifo count could mean 0 or 32 bytes available.
1077 * If BIT5 of STAR is set then at least 1 byte is available.
1079 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
1084 /* Flush received async data to receive data buffer. */
1085 while (fifo_count) {
1086 data = read_reg(info, CHA + RXFIFO);
1087 status = read_reg(info, CHA + RXFIFO);
1090 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1093 *tty->flip.char_buf_ptr = data;
1096 *tty->flip.flag_buf_ptr = 0;
1098 // if no frameing/crc error then save data
1099 // BIT7:parity error
1100 // BIT6:framing error
1102 if (status & (BIT7 + BIT6)) {
1108 /* discard char if tty control flags say so */
1109 if (status & info->ignore_status_mask)
1112 status &= info->read_status_mask;
1115 *tty->flip.flag_buf_ptr = TTY_PARITY;
1116 else if (status & BIT6)
1117 *tty->flip.flag_buf_ptr = TTY_FRAME;
1120 tty->flip.flag_buf_ptr++;
1121 tty->flip.char_buf_ptr++;
1124 issue_command(info, CHA, CMD_RXFIFO);
1126 if (debug_level >= DEBUG_LEVEL_ISR) {
1127 printk("%s(%d):rx_ready_async count=%d\n",
1128 __FILE__,__LINE__,tty->flip.count);
1129 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1130 __FILE__,__LINE__,icount->rx,icount->brk,
1131 icount->parity,icount->frame,icount->overrun);
1134 if (tty->flip.count)
1135 tty_flip_buffer_push(tty);
1139 void tx_done(MGSLPC_INFO *info)
1141 if (!info->tx_active)
1144 info->tx_active = 0;
1145 info->tx_aborting = 0;
1147 if (info->params.mode == MGSL_MODE_ASYNC)
1150 info->tx_count = info->tx_put = info->tx_get = 0;
1151 del_timer(&info->tx_timer);
1153 if (info->drop_rts_on_tx_done) {
1155 if (info->serial_signals & SerialSignal_RTS) {
1156 info->serial_signals &= ~SerialSignal_RTS;
1159 info->drop_rts_on_tx_done = 0;
1162 #ifdef CONFIG_SYNCLINK_SYNCPPP
1164 mgslpc_sppp_tx_done(info);
1168 if (info->tty->stopped || info->tty->hw_stopped) {
1172 info->pending_bh |= BH_TRANSMIT;
1176 void tx_ready(MGSLPC_INFO *info)
1178 unsigned char fifo_count = 32;
1181 if (debug_level >= DEBUG_LEVEL_ISR)
1182 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1184 if (info->params.mode == MGSL_MODE_HDLC) {
1185 if (!info->tx_active)
1188 if (info->tty->stopped || info->tty->hw_stopped) {
1192 if (!info->tx_count)
1193 info->tx_active = 0;
1196 if (!info->tx_count)
1199 while (info->tx_count && fifo_count) {
1200 c = MIN(2, MIN(fifo_count, MIN(info->tx_count, TXBUFSIZE - info->tx_get)));
1203 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1205 write_reg16(info, CHA + TXFIFO,
1206 *((unsigned short*)(info->tx_buf + info->tx_get)));
1208 info->tx_count -= c;
1209 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1213 if (info->params.mode == MGSL_MODE_ASYNC) {
1214 if (info->tx_count < WAKEUP_CHARS)
1215 info->pending_bh |= BH_TRANSMIT;
1216 issue_command(info, CHA, CMD_TXFIFO);
1219 issue_command(info, CHA, CMD_TXFIFO);
1221 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1225 void cts_change(MGSLPC_INFO *info)
1228 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1229 irq_disable(info, CHB, IRQ_CTS);
1231 if (info->serial_signals & SerialSignal_CTS)
1232 info->input_signal_events.cts_up++;
1234 info->input_signal_events.cts_down++;
1235 wake_up_interruptible(&info->status_event_wait_q);
1236 wake_up_interruptible(&info->event_wait_q);
1238 if (info->flags & ASYNC_CTS_FLOW) {
1239 if (info->tty->hw_stopped) {
1240 if (info->serial_signals & SerialSignal_CTS) {
1241 if (debug_level >= DEBUG_LEVEL_ISR)
1242 printk("CTS tx start...");
1244 info->tty->hw_stopped = 0;
1246 info->pending_bh |= BH_TRANSMIT;
1250 if (!(info->serial_signals & SerialSignal_CTS)) {
1251 if (debug_level >= DEBUG_LEVEL_ISR)
1252 printk("CTS tx stop...");
1254 info->tty->hw_stopped = 1;
1259 info->pending_bh |= BH_STATUS;
1262 void dcd_change(MGSLPC_INFO *info)
1265 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1266 irq_disable(info, CHB, IRQ_DCD);
1268 if (info->serial_signals & SerialSignal_DCD) {
1269 info->input_signal_events.dcd_up++;
1270 #ifdef CONFIG_SYNCLINK_SYNCPPP
1272 sppp_reopen(info->netdev);
1276 info->input_signal_events.dcd_down++;
1277 wake_up_interruptible(&info->status_event_wait_q);
1278 wake_up_interruptible(&info->event_wait_q);
1280 if (info->flags & ASYNC_CHECK_CD) {
1281 if (debug_level >= DEBUG_LEVEL_ISR)
1282 printk("%s CD now %s...", info->device_name,
1283 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1284 if (info->serial_signals & SerialSignal_DCD)
1285 wake_up_interruptible(&info->open_wait);
1287 if (debug_level >= DEBUG_LEVEL_ISR)
1288 printk("doing serial hangup...");
1290 tty_hangup(info->tty);
1293 info->pending_bh |= BH_STATUS;
1296 void dsr_change(MGSLPC_INFO *info)
1299 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1300 port_irq_disable(info, PVR_DSR);
1302 if (info->serial_signals & SerialSignal_DSR)
1303 info->input_signal_events.dsr_up++;
1305 info->input_signal_events.dsr_down++;
1306 wake_up_interruptible(&info->status_event_wait_q);
1307 wake_up_interruptible(&info->event_wait_q);
1308 info->pending_bh |= BH_STATUS;
1311 void ri_change(MGSLPC_INFO *info)
1314 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1315 port_irq_disable(info, PVR_RI);
1317 if (info->serial_signals & SerialSignal_RI)
1318 info->input_signal_events.ri_up++;
1320 info->input_signal_events.ri_down++;
1321 wake_up_interruptible(&info->status_event_wait_q);
1322 wake_up_interruptible(&info->event_wait_q);
1323 info->pending_bh |= BH_STATUS;
1326 /* Interrupt service routine entry point.
1330 * irq interrupt number that caused interrupt
1331 * dev_id device ID supplied during interrupt registration
1332 * regs interrupted processor context
1334 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs)
1336 MGSLPC_INFO * info = (MGSLPC_INFO *)dev_id;
1338 unsigned char gis, pis;
1341 if (debug_level >= DEBUG_LEVEL_ISR)
1342 printk("mgslpc_isr(%d) entry.\n", irq);
1346 if (!(info->link.state & DEV_CONFIG))
1349 spin_lock(&info->lock);
1351 while ((gis = read_reg(info, CHA + GIS))) {
1352 if (debug_level >= DEBUG_LEVEL_ISR)
1353 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1355 if ((gis & 0x70) || count > 1000) {
1356 printk("synclink_cs:hardware failed or ejected\n");
1361 if (gis & (BIT1 + BIT0)) {
1362 isr = read_reg16(info, CHB + ISR);
1368 if (gis & (BIT3 + BIT2))
1370 isr = read_reg16(info, CHA + ISR);
1371 if (isr & IRQ_TIMER) {
1372 info->irq_occurred = 1;
1373 irq_disable(info, CHA, IRQ_TIMER);
1377 if (isr & IRQ_EXITHUNT) {
1378 info->icount.exithunt++;
1379 wake_up_interruptible(&info->event_wait_q);
1381 if (isr & IRQ_BREAK_ON) {
1383 if (info->flags & ASYNC_SAK)
1386 if (isr & IRQ_RXTIME) {
1387 issue_command(info, CHA, CMD_RXFIFO_READ);
1389 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1390 if (info->params.mode == MGSL_MODE_HDLC)
1391 rx_ready_hdlc(info, isr & IRQ_RXEOM);
1393 rx_ready_async(info, isr & IRQ_RXEOM);
1397 if (isr & IRQ_UNDERRUN) {
1398 if (info->tx_aborting)
1399 info->icount.txabort++;
1401 info->icount.txunder++;
1404 else if (isr & IRQ_ALLSENT) {
1405 info->icount.txok++;
1408 else if (isr & IRQ_TXFIFO)
1412 pis = read_reg(info, CHA + PIS);
1420 /* Request bottom half processing if there's something
1421 * for it to do and the bh is not already running
1424 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1425 if ( debug_level >= DEBUG_LEVEL_ISR )
1426 printk("%s(%d):%s queueing bh task.\n",
1427 __FILE__,__LINE__,info->device_name);
1428 schedule_work(&info->task);
1429 info->bh_requested = 1;
1432 spin_unlock(&info->lock);
1434 if (debug_level >= DEBUG_LEVEL_ISR)
1435 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1436 __FILE__,__LINE__,irq);
1441 /* Initialize and start device.
1443 static int startup(MGSLPC_INFO * info)
1447 if (debug_level >= DEBUG_LEVEL_INFO)
1448 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1450 if (info->flags & ASYNC_INITIALIZED)
1453 if (!info->tx_buf) {
1454 /* allocate a page of memory for a transmit buffer */
1455 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1456 if (!info->tx_buf) {
1457 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1458 __FILE__,__LINE__,info->device_name);
1463 info->pending_bh = 0;
1465 init_timer(&info->tx_timer);
1466 info->tx_timer.data = (unsigned long)info;
1467 info->tx_timer.function = tx_timeout;
1469 /* Allocate and claim adapter resources */
1470 retval = claim_resources(info);
1472 /* perform existance check and diagnostics */
1474 retval = adapter_test(info);
1477 if (capable(CAP_SYS_ADMIN) && info->tty)
1478 set_bit(TTY_IO_ERROR, &info->tty->flags);
1479 release_resources(info);
1483 /* program hardware for current parameters */
1484 mgslpc_change_params(info);
1487 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1489 info->flags |= ASYNC_INITIALIZED;
1494 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1496 static void shutdown(MGSLPC_INFO * info)
1498 unsigned long flags;
1500 if (!(info->flags & ASYNC_INITIALIZED))
1503 if (debug_level >= DEBUG_LEVEL_INFO)
1504 printk("%s(%d):mgslpc_shutdown(%s)\n",
1505 __FILE__,__LINE__, info->device_name );
1507 /* clear status wait queue because status changes */
1508 /* can't happen after shutting down the hardware */
1509 wake_up_interruptible(&info->status_event_wait_q);
1510 wake_up_interruptible(&info->event_wait_q);
1512 del_timer(&info->tx_timer);
1515 free_page((unsigned long) info->tx_buf);
1519 spin_lock_irqsave(&info->lock,flags);
1524 /* TODO:disable interrupts instead of reset to preserve signal states */
1527 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1528 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1532 spin_unlock_irqrestore(&info->lock,flags);
1534 release_resources(info);
1537 set_bit(TTY_IO_ERROR, &info->tty->flags);
1539 info->flags &= ~ASYNC_INITIALIZED;
1542 static void mgslpc_program_hw(MGSLPC_INFO *info)
1544 unsigned long flags;
1546 spin_lock_irqsave(&info->lock,flags);
1550 info->tx_count = info->tx_put = info->tx_get = 0;
1552 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1559 info->dcd_chkcount = 0;
1560 info->cts_chkcount = 0;
1561 info->ri_chkcount = 0;
1562 info->dsr_chkcount = 0;
1564 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1565 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1568 if (info->netcount || info->tty->termios->c_cflag & CREAD)
1571 spin_unlock_irqrestore(&info->lock,flags);
1574 /* Reconfigure adapter based on new parameters
1576 static void mgslpc_change_params(MGSLPC_INFO *info)
1581 if (!info->tty || !info->tty->termios)
1584 if (debug_level >= DEBUG_LEVEL_INFO)
1585 printk("%s(%d):mgslpc_change_params(%s)\n",
1586 __FILE__,__LINE__, info->device_name );
1588 cflag = info->tty->termios->c_cflag;
1590 /* if B0 rate (hangup) specified then negate DTR and RTS */
1591 /* otherwise assert DTR and RTS */
1593 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1595 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1597 /* byte size and parity */
1599 switch (cflag & CSIZE) {
1600 case CS5: info->params.data_bits = 5; break;
1601 case CS6: info->params.data_bits = 6; break;
1602 case CS7: info->params.data_bits = 7; break;
1603 case CS8: info->params.data_bits = 8; break;
1604 default: info->params.data_bits = 7; break;
1608 info->params.stop_bits = 2;
1610 info->params.stop_bits = 1;
1612 info->params.parity = ASYNC_PARITY_NONE;
1613 if (cflag & PARENB) {
1615 info->params.parity = ASYNC_PARITY_ODD;
1617 info->params.parity = ASYNC_PARITY_EVEN;
1620 info->params.parity = ASYNC_PARITY_SPACE;
1624 /* calculate number of jiffies to transmit a full
1625 * FIFO (32 bytes) at specified data rate
1627 bits_per_char = info->params.data_bits +
1628 info->params.stop_bits + 1;
1630 /* if port data rate is set to 460800 or less then
1631 * allow tty settings to override, otherwise keep the
1632 * current data rate.
1634 if (info->params.data_rate <= 460800) {
1635 info->params.data_rate = tty_get_baud_rate(info->tty);
1638 if ( info->params.data_rate ) {
1639 info->timeout = (32*HZ*bits_per_char) /
1640 info->params.data_rate;
1642 info->timeout += HZ/50; /* Add .02 seconds of slop */
1644 if (cflag & CRTSCTS)
1645 info->flags |= ASYNC_CTS_FLOW;
1647 info->flags &= ~ASYNC_CTS_FLOW;
1650 info->flags &= ~ASYNC_CHECK_CD;
1652 info->flags |= ASYNC_CHECK_CD;
1654 /* process tty input control flags */
1656 info->read_status_mask = 0;
1657 if (I_INPCK(info->tty))
1658 info->read_status_mask |= BIT7 | BIT6;
1659 if (I_IGNPAR(info->tty))
1660 info->ignore_status_mask |= BIT7 | BIT6;
1662 mgslpc_program_hw(info);
1665 /* Add a character to the transmit buffer
1667 static void mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1669 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1670 unsigned long flags;
1672 if (debug_level >= DEBUG_LEVEL_INFO) {
1673 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1674 __FILE__,__LINE__,ch,info->device_name);
1677 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1680 if (!tty || !info->tx_buf)
1683 spin_lock_irqsave(&info->lock,flags);
1685 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1686 if (info->tx_count < TXBUFSIZE - 1) {
1687 info->tx_buf[info->tx_put++] = ch;
1688 info->tx_put &= TXBUFSIZE-1;
1693 spin_unlock_irqrestore(&info->lock,flags);
1696 /* Enable transmitter so remaining characters in the
1697 * transmit buffer are sent.
1699 static void mgslpc_flush_chars(struct tty_struct *tty)
1701 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1702 unsigned long flags;
1704 if (debug_level >= DEBUG_LEVEL_INFO)
1705 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1706 __FILE__,__LINE__,info->device_name,info->tx_count);
1708 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1711 if (info->tx_count <= 0 || tty->stopped ||
1712 tty->hw_stopped || !info->tx_buf)
1715 if (debug_level >= DEBUG_LEVEL_INFO)
1716 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1717 __FILE__,__LINE__,info->device_name);
1719 spin_lock_irqsave(&info->lock,flags);
1720 if (!info->tx_active)
1722 spin_unlock_irqrestore(&info->lock,flags);
1725 /* Send a block of data
1729 * tty pointer to tty information structure
1730 * from_user flag: 1 = from user process
1731 * buf pointer to buffer containing send data
1732 * count size of send data in bytes
1734 * Returns: number of characters written
1736 static int mgslpc_write(struct tty_struct * tty, int from_user,
1737 const unsigned char *buf, int count)
1739 int c, ret = 0, err;
1740 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1741 unsigned long flags;
1743 if (debug_level >= DEBUG_LEVEL_INFO)
1744 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1745 __FILE__,__LINE__,info->device_name,count);
1747 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1748 !tty || !info->tx_buf)
1751 if (info->params.mode == MGSL_MODE_HDLC) {
1752 if (count > TXBUFSIZE) {
1756 if (info->tx_active)
1758 else if (info->tx_count)
1764 MIN(TXBUFSIZE - info->tx_count - 1,
1765 TXBUFSIZE - info->tx_put));
1770 COPY_FROM_USER(err, info->tx_buf + info->tx_put, buf, c);
1777 memcpy(info->tx_buf + info->tx_put, buf, c);
1779 spin_lock_irqsave(&info->lock,flags);
1780 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1781 info->tx_count += c;
1782 spin_unlock_irqrestore(&info->lock,flags);
1789 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1790 spin_lock_irqsave(&info->lock,flags);
1791 if (!info->tx_active)
1793 spin_unlock_irqrestore(&info->lock,flags);
1796 if (debug_level >= DEBUG_LEVEL_INFO)
1797 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1798 __FILE__,__LINE__,info->device_name,ret);
1802 /* Return the count of free bytes in transmit buffer
1804 static int mgslpc_write_room(struct tty_struct *tty)
1806 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1809 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1812 if (info->params.mode == MGSL_MODE_HDLC) {
1813 /* HDLC (frame oriented) mode */
1814 if (info->tx_active)
1817 return HDLC_MAX_FRAME_SIZE;
1819 ret = TXBUFSIZE - info->tx_count - 1;
1824 if (debug_level >= DEBUG_LEVEL_INFO)
1825 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1826 __FILE__,__LINE__, info->device_name, ret);
1830 /* Return the count of bytes in transmit buffer
1832 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1834 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1837 if (debug_level >= DEBUG_LEVEL_INFO)
1838 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1839 __FILE__,__LINE__, info->device_name );
1841 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1844 if (info->params.mode == MGSL_MODE_HDLC)
1845 rc = info->tx_active ? info->max_frame_size : 0;
1847 rc = info->tx_count;
1849 if (debug_level >= DEBUG_LEVEL_INFO)
1850 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1851 __FILE__,__LINE__, info->device_name, rc);
1856 /* Discard all data in the send buffer
1858 static void mgslpc_flush_buffer(struct tty_struct *tty)
1860 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1861 unsigned long flags;
1863 if (debug_level >= DEBUG_LEVEL_INFO)
1864 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1865 __FILE__,__LINE__, info->device_name );
1867 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1870 spin_lock_irqsave(&info->lock,flags);
1871 info->tx_count = info->tx_put = info->tx_get = 0;
1872 del_timer(&info->tx_timer);
1873 spin_unlock_irqrestore(&info->lock,flags);
1875 wake_up_interruptible(&tty->write_wait);
1876 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1877 tty->ldisc.write_wakeup)
1878 (tty->ldisc.write_wakeup)(tty);
1881 /* Send a high-priority XON/XOFF character
1883 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1885 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1886 unsigned long flags;
1888 if (debug_level >= DEBUG_LEVEL_INFO)
1889 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1890 __FILE__,__LINE__, info->device_name, ch );
1892 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1897 spin_lock_irqsave(&info->lock,flags);
1898 if (!info->tx_enabled)
1900 spin_unlock_irqrestore(&info->lock,flags);
1904 /* Signal remote device to throttle send data (our receive data)
1906 static void mgslpc_throttle(struct tty_struct * tty)
1908 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1909 unsigned long flags;
1911 if (debug_level >= DEBUG_LEVEL_INFO)
1912 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1913 __FILE__,__LINE__, info->device_name );
1915 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1919 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1921 if (tty->termios->c_cflag & CRTSCTS) {
1922 spin_lock_irqsave(&info->lock,flags);
1923 info->serial_signals &= ~SerialSignal_RTS;
1925 spin_unlock_irqrestore(&info->lock,flags);
1929 /* Signal remote device to stop throttling send data (our receive data)
1931 static void mgslpc_unthrottle(struct tty_struct * tty)
1933 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1934 unsigned long flags;
1936 if (debug_level >= DEBUG_LEVEL_INFO)
1937 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1938 __FILE__,__LINE__, info->device_name );
1940 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1947 mgslpc_send_xchar(tty, START_CHAR(tty));
1950 if (tty->termios->c_cflag & CRTSCTS) {
1951 spin_lock_irqsave(&info->lock,flags);
1952 info->serial_signals |= SerialSignal_RTS;
1954 spin_unlock_irqrestore(&info->lock,flags);
1958 /* get the current serial statistics
1960 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1963 if (debug_level >= DEBUG_LEVEL_INFO)
1964 printk("get_params(%s)\n", info->device_name);
1965 COPY_TO_USER(err,user_icount, &info->icount, sizeof(struct mgsl_icount));
1971 /* get the current serial parameters
1973 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1976 if (debug_level >= DEBUG_LEVEL_INFO)
1977 printk("get_params(%s)\n", info->device_name);
1978 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1984 /* set the serial parameters
1988 * info pointer to device instance data
1989 * new_params user buffer containing new serial params
1991 * Returns: 0 if success, otherwise error code
1993 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params)
1995 unsigned long flags;
1996 MGSL_PARAMS tmp_params;
1999 if (debug_level >= DEBUG_LEVEL_INFO)
2000 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
2001 info->device_name );
2002 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2004 if ( debug_level >= DEBUG_LEVEL_INFO )
2005 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
2006 __FILE__,__LINE__,info->device_name);
2010 spin_lock_irqsave(&info->lock,flags);
2011 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2012 spin_unlock_irqrestore(&info->lock,flags);
2014 mgslpc_change_params(info);
2019 static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
2022 if (debug_level >= DEBUG_LEVEL_INFO)
2023 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
2024 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2030 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
2032 unsigned long flags;
2033 if (debug_level >= DEBUG_LEVEL_INFO)
2034 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
2035 spin_lock_irqsave(&info->lock,flags);
2036 info->idle_mode = idle_mode;
2038 spin_unlock_irqrestore(&info->lock,flags);
2042 static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
2045 if (debug_level >= DEBUG_LEVEL_INFO)
2046 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
2047 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
2053 static int set_interface(MGSLPC_INFO * info, int if_mode)
2055 unsigned long flags;
2057 if (debug_level >= DEBUG_LEVEL_INFO)
2058 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
2059 spin_lock_irqsave(&info->lock,flags);
2060 info->if_mode = if_mode;
2062 val = read_reg(info, PVR) & 0x0f;
2063 switch (info->if_mode)
2065 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
2066 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
2067 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
2069 write_reg(info, PVR, val);
2071 spin_unlock_irqrestore(&info->lock,flags);
2075 static int set_txenable(MGSLPC_INFO * info, int enable)
2077 unsigned long flags;
2079 if (debug_level >= DEBUG_LEVEL_INFO)
2080 printk("set_txenable(%s,%d)\n", info->device_name, enable);
2082 spin_lock_irqsave(&info->lock,flags);
2084 if (!info->tx_enabled)
2087 if (info->tx_enabled)
2090 spin_unlock_irqrestore(&info->lock,flags);
2094 static int tx_abort(MGSLPC_INFO * info)
2096 unsigned long flags;
2098 if (debug_level >= DEBUG_LEVEL_INFO)
2099 printk("tx_abort(%s)\n", info->device_name);
2101 spin_lock_irqsave(&info->lock,flags);
2102 if (info->tx_active && info->tx_count &&
2103 info->params.mode == MGSL_MODE_HDLC) {
2104 /* clear data count so FIFO is not filled on next IRQ.
2105 * This results in underrun and abort transmission.
2107 info->tx_count = info->tx_put = info->tx_get = 0;
2108 info->tx_aborting = TRUE;
2110 spin_unlock_irqrestore(&info->lock,flags);
2114 static int set_rxenable(MGSLPC_INFO * info, int enable)
2116 unsigned long flags;
2118 if (debug_level >= DEBUG_LEVEL_INFO)
2119 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
2121 spin_lock_irqsave(&info->lock,flags);
2123 if (!info->rx_enabled)
2126 if (info->rx_enabled)
2129 spin_unlock_irqrestore(&info->lock,flags);
2133 /* wait for specified event to occur
2135 * Arguments: info pointer to device instance data
2136 * mask pointer to bitmask of events to wait for
2137 * Return Value: 0 if successful and bit mask updated with
2138 * of events triggerred,
2139 * otherwise error code
2141 static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
2143 unsigned long flags;
2146 struct mgsl_icount cprev, cnow;
2149 struct _input_signal_events oldsigs, newsigs;
2150 DECLARE_WAITQUEUE(wait, current);
2152 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2156 if (debug_level >= DEBUG_LEVEL_INFO)
2157 printk("wait_events(%s,%d)\n", info->device_name, mask);
2159 spin_lock_irqsave(&info->lock,flags);
2161 /* return immediately if state matches requested events */
2163 s = info->serial_signals;
2165 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2166 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2167 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2168 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2170 spin_unlock_irqrestore(&info->lock,flags);
2174 /* save current irq counts */
2175 cprev = info->icount;
2176 oldsigs = info->input_signal_events;
2178 if ((info->params.mode == MGSL_MODE_HDLC) &&
2179 (mask & MgslEvent_ExitHuntMode))
2180 irq_enable(info, CHA, IRQ_EXITHUNT);
2182 set_current_state(TASK_INTERRUPTIBLE);
2183 add_wait_queue(&info->event_wait_q, &wait);
2185 spin_unlock_irqrestore(&info->lock,flags);
2190 if (signal_pending(current)) {
2195 /* get current irq counts */
2196 spin_lock_irqsave(&info->lock,flags);
2197 cnow = info->icount;
2198 newsigs = info->input_signal_events;
2199 set_current_state(TASK_INTERRUPTIBLE);
2200 spin_unlock_irqrestore(&info->lock,flags);
2202 /* if no change, wait aborted for some reason */
2203 if (newsigs.dsr_up == oldsigs.dsr_up &&
2204 newsigs.dsr_down == oldsigs.dsr_down &&
2205 newsigs.dcd_up == oldsigs.dcd_up &&
2206 newsigs.dcd_down == oldsigs.dcd_down &&
2207 newsigs.cts_up == oldsigs.cts_up &&
2208 newsigs.cts_down == oldsigs.cts_down &&
2209 newsigs.ri_up == oldsigs.ri_up &&
2210 newsigs.ri_down == oldsigs.ri_down &&
2211 cnow.exithunt == cprev.exithunt &&
2212 cnow.rxidle == cprev.rxidle) {
2218 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2219 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2220 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2221 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2222 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2223 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2224 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2225 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2226 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2227 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2235 remove_wait_queue(&info->event_wait_q, &wait);
2236 set_current_state(TASK_RUNNING);
2238 if (mask & MgslEvent_ExitHuntMode) {
2239 spin_lock_irqsave(&info->lock,flags);
2240 if (!waitqueue_active(&info->event_wait_q))
2241 irq_disable(info, CHA, IRQ_EXITHUNT);
2242 spin_unlock_irqrestore(&info->lock,flags);
2246 PUT_USER(rc, events, mask_ptr);
2250 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2252 unsigned long flags;
2254 struct mgsl_icount cprev, cnow;
2255 DECLARE_WAITQUEUE(wait, current);
2257 /* save current irq counts */
2258 spin_lock_irqsave(&info->lock,flags);
2259 cprev = info->icount;
2260 add_wait_queue(&info->status_event_wait_q, &wait);
2261 set_current_state(TASK_INTERRUPTIBLE);
2262 spin_unlock_irqrestore(&info->lock,flags);
2266 if (signal_pending(current)) {
2271 /* get new irq counts */
2272 spin_lock_irqsave(&info->lock,flags);
2273 cnow = info->icount;
2274 set_current_state(TASK_INTERRUPTIBLE);
2275 spin_unlock_irqrestore(&info->lock,flags);
2277 /* if no change, wait aborted for some reason */
2278 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2279 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2284 /* check for change in caller specified modem input */
2285 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2286 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2287 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2288 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2295 remove_wait_queue(&info->status_event_wait_q, &wait);
2296 set_current_state(TASK_RUNNING);
2300 /* return the state of the serial control and status signals
2302 static int tiocmget(struct tty_struct *tty, struct file *file)
2304 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2305 unsigned int result;
2306 unsigned long flags;
2308 spin_lock_irqsave(&info->lock,flags);
2310 spin_unlock_irqrestore(&info->lock,flags);
2312 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2313 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2314 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2315 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2316 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2317 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2319 if (debug_level >= DEBUG_LEVEL_INFO)
2320 printk("%s(%d):%s tiocmget() value=%08X\n",
2321 __FILE__,__LINE__, info->device_name, result );
2325 /* set modem control signals (DTR/RTS)
2327 static int tiocmset(struct tty_struct *tty, struct file *file,
2328 unsigned int set, unsigned int clear)
2330 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2331 unsigned long flags;
2333 if (debug_level >= DEBUG_LEVEL_INFO)
2334 printk("%s(%d):%s tiocmset(%x,%x)\n",
2335 __FILE__,__LINE__,info->device_name, set, clear);
2337 if (set & TIOCM_RTS)
2338 info->serial_signals |= SerialSignal_RTS;
2339 if (set & TIOCM_DTR)
2340 info->serial_signals |= SerialSignal_DTR;
2341 if (clear & TIOCM_RTS)
2342 info->serial_signals &= ~SerialSignal_RTS;
2343 if (clear & TIOCM_DTR)
2344 info->serial_signals &= ~SerialSignal_DTR;
2346 spin_lock_irqsave(&info->lock,flags);
2348 spin_unlock_irqrestore(&info->lock,flags);
2353 /* Set or clear transmit break condition
2355 * Arguments: tty pointer to tty instance data
2356 * break_state -1=set break condition, 0=clear
2358 static void mgslpc_break(struct tty_struct *tty, int break_state)
2360 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2361 unsigned long flags;
2363 if (debug_level >= DEBUG_LEVEL_INFO)
2364 printk("%s(%d):mgslpc_break(%s,%d)\n",
2365 __FILE__,__LINE__, info->device_name, break_state);
2367 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2370 spin_lock_irqsave(&info->lock,flags);
2371 if (break_state == -1)
2372 set_reg_bits(info, CHA+DAFO, BIT6);
2374 clear_reg_bits(info, CHA+DAFO, BIT6);
2375 spin_unlock_irqrestore(&info->lock,flags);
2378 /* Service an IOCTL request
2382 * tty pointer to tty instance data
2383 * file pointer to associated file object for device
2384 * cmd IOCTL command code
2385 * arg command argument/context
2387 * Return Value: 0 if success, otherwise error code
2389 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2390 unsigned int cmd, unsigned long arg)
2392 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2394 if (debug_level >= DEBUG_LEVEL_INFO)
2395 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2396 info->device_name, cmd );
2398 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2401 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2402 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2403 if (tty->flags & (1 << TTY_IO_ERROR))
2407 return ioctl_common(info, cmd, arg);
2410 int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
2413 struct mgsl_icount cnow; /* kernel counter temps */
2414 struct serial_icounter_struct __user *p_cuser; /* user space */
2415 void __user *argp = (void __user *)arg;
2416 unsigned long flags;
2419 case MGSL_IOCGPARAMS:
2420 return get_params(info, argp);
2421 case MGSL_IOCSPARAMS:
2422 return set_params(info, argp);
2423 case MGSL_IOCGTXIDLE:
2424 return get_txidle(info, argp);
2425 case MGSL_IOCSTXIDLE:
2426 return set_txidle(info, (int)arg);
2428 return get_interface(info, argp);
2430 return set_interface(info,(int)arg);
2431 case MGSL_IOCTXENABLE:
2432 return set_txenable(info,(int)arg);
2433 case MGSL_IOCRXENABLE:
2434 return set_rxenable(info,(int)arg);
2435 case MGSL_IOCTXABORT:
2436 return tx_abort(info);
2437 case MGSL_IOCGSTATS:
2438 return get_stats(info, argp);
2439 case MGSL_IOCWAITEVENT:
2440 return wait_events(info, argp);
2442 return modem_input_wait(info,(int)arg);
2444 spin_lock_irqsave(&info->lock,flags);
2445 cnow = info->icount;
2446 spin_unlock_irqrestore(&info->lock,flags);
2448 PUT_USER(error,cnow.cts, &p_cuser->cts);
2449 if (error) return error;
2450 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2451 if (error) return error;
2452 PUT_USER(error,cnow.rng, &p_cuser->rng);
2453 if (error) return error;
2454 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2455 if (error) return error;
2456 PUT_USER(error,cnow.rx, &p_cuser->rx);
2457 if (error) return error;
2458 PUT_USER(error,cnow.tx, &p_cuser->tx);
2459 if (error) return error;
2460 PUT_USER(error,cnow.frame, &p_cuser->frame);
2461 if (error) return error;
2462 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2463 if (error) return error;
2464 PUT_USER(error,cnow.parity, &p_cuser->parity);
2465 if (error) return error;
2466 PUT_USER(error,cnow.brk, &p_cuser->brk);
2467 if (error) return error;
2468 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2469 if (error) return error;
2472 return -ENOIOCTLCMD;
2477 /* Set new termios settings
2481 * tty pointer to tty structure
2482 * termios pointer to buffer to hold returned old termios
2484 static void mgslpc_set_termios(struct tty_struct *tty, struct termios *old_termios)
2486 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2487 unsigned long flags;
2489 if (debug_level >= DEBUG_LEVEL_INFO)
2490 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2491 tty->driver->name );
2493 /* just return if nothing has changed */
2494 if ((tty->termios->c_cflag == old_termios->c_cflag)
2495 && (RELEVANT_IFLAG(tty->termios->c_iflag)
2496 == RELEVANT_IFLAG(old_termios->c_iflag)))
2499 mgslpc_change_params(info);
2501 /* Handle transition to B0 status */
2502 if (old_termios->c_cflag & CBAUD &&
2503 !(tty->termios->c_cflag & CBAUD)) {
2504 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2505 spin_lock_irqsave(&info->lock,flags);
2507 spin_unlock_irqrestore(&info->lock,flags);
2510 /* Handle transition away from B0 status */
2511 if (!(old_termios->c_cflag & CBAUD) &&
2512 tty->termios->c_cflag & CBAUD) {
2513 info->serial_signals |= SerialSignal_DTR;
2514 if (!(tty->termios->c_cflag & CRTSCTS) ||
2515 !test_bit(TTY_THROTTLED, &tty->flags)) {
2516 info->serial_signals |= SerialSignal_RTS;
2518 spin_lock_irqsave(&info->lock,flags);
2520 spin_unlock_irqrestore(&info->lock,flags);
2523 /* Handle turning off CRTSCTS */
2524 if (old_termios->c_cflag & CRTSCTS &&
2525 !(tty->termios->c_cflag & CRTSCTS)) {
2526 tty->hw_stopped = 0;
2531 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2533 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2535 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2538 if (debug_level >= DEBUG_LEVEL_INFO)
2539 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2540 __FILE__,__LINE__, info->device_name, info->count);
2545 if (tty_hung_up_p(filp))
2548 if ((tty->count == 1) && (info->count != 1)) {
2550 * tty->count is 1 and the tty structure will be freed.
2551 * info->count should be one in this case.
2552 * if it's not, correct it so that the port is shutdown.
2554 printk("mgslpc_close: bad refcount; tty->count is 1, "
2555 "info->count is %d\n", info->count);
2561 /* if at least one open remaining, leave hardware active */
2565 info->flags |= ASYNC_CLOSING;
2567 /* set tty->closing to notify line discipline to
2568 * only process XON/XOFF characters. Only the N_TTY
2569 * discipline appears to use this (ppp does not).
2573 /* wait for transmit data to clear all layers */
2575 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
2576 if (debug_level >= DEBUG_LEVEL_INFO)
2577 printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sent\n",
2578 __FILE__,__LINE__, info->device_name );
2579 tty_wait_until_sent(tty, info->closing_wait);
2582 if (info->flags & ASYNC_INITIALIZED)
2583 mgslpc_wait_until_sent(tty, info->timeout);
2585 if (tty->driver->flush_buffer)
2586 tty->driver->flush_buffer(tty);
2588 if (tty->ldisc.flush_buffer)
2589 tty->ldisc.flush_buffer(tty);
2596 if (info->blocked_open) {
2597 if (info->close_delay) {
2598 set_current_state(TASK_INTERRUPTIBLE);
2599 schedule_timeout(info->close_delay);
2601 wake_up_interruptible(&info->open_wait);
2604 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2606 wake_up_interruptible(&info->close_wait);
2609 if (debug_level >= DEBUG_LEVEL_INFO)
2610 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2611 tty->driver->name, info->count);
2614 /* Wait until the transmitter is empty.
2616 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2618 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2619 unsigned long orig_jiffies, char_time;
2624 if (debug_level >= DEBUG_LEVEL_INFO)
2625 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2626 __FILE__,__LINE__, info->device_name );
2628 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2631 if (!(info->flags & ASYNC_INITIALIZED))
2634 orig_jiffies = jiffies;
2636 /* Set check interval to 1/5 of estimated time to
2637 * send a character, and make it at least 1. The check
2638 * interval should also be less than the timeout.
2639 * Note: use tight timings here to satisfy the NIST-PCTS.
2642 if ( info->params.data_rate ) {
2643 char_time = info->timeout/(32 * 5);
2650 char_time = MIN(char_time, timeout);
2652 if (info->params.mode == MGSL_MODE_HDLC) {
2653 while (info->tx_active) {
2654 set_current_state(TASK_INTERRUPTIBLE);
2655 schedule_timeout(char_time);
2656 if (signal_pending(current))
2658 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2662 while ((info->tx_count || info->tx_active) &&
2664 set_current_state(TASK_INTERRUPTIBLE);
2665 schedule_timeout(char_time);
2666 if (signal_pending(current))
2668 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2674 if (debug_level >= DEBUG_LEVEL_INFO)
2675 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2676 __FILE__,__LINE__, info->device_name );
2679 /* Called by tty_hangup() when a hangup is signaled.
2680 * This is the same as closing all open files for the port.
2682 static void mgslpc_hangup(struct tty_struct *tty)
2684 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2686 if (debug_level >= DEBUG_LEVEL_INFO)
2687 printk("%s(%d):mgslpc_hangup(%s)\n",
2688 __FILE__,__LINE__, info->device_name );
2690 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2693 mgslpc_flush_buffer(tty);
2697 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2700 wake_up_interruptible(&info->open_wait);
2703 /* Block the current process until the specified port
2704 * is ready to be opened.
2706 static int block_til_ready(struct tty_struct *tty, struct file *filp,
2709 DECLARE_WAITQUEUE(wait, current);
2711 int do_clocal = 0, extra_count = 0;
2712 unsigned long flags;
2714 if (debug_level >= DEBUG_LEVEL_INFO)
2715 printk("%s(%d):block_til_ready on %s\n",
2716 __FILE__,__LINE__, tty->driver->name );
2718 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
2719 /* nonblock mode is set or port is not enabled */
2720 /* just verify that callout device is not active */
2721 info->flags |= ASYNC_NORMAL_ACTIVE;
2725 if (tty->termios->c_cflag & CLOCAL)
2728 /* Wait for carrier detect and the line to become
2729 * free (i.e., not in use by the callout). While we are in
2730 * this loop, info->count is dropped by one, so that
2731 * mgslpc_close() knows when to free things. We restore it upon
2732 * exit, either normal or abnormal.
2736 add_wait_queue(&info->open_wait, &wait);
2738 if (debug_level >= DEBUG_LEVEL_INFO)
2739 printk("%s(%d):block_til_ready before block on %s count=%d\n",
2740 __FILE__,__LINE__, tty->driver->name, info->count );
2742 spin_lock_irqsave(&info->lock, flags);
2743 if (!tty_hung_up_p(filp)) {
2747 spin_unlock_irqrestore(&info->lock, flags);
2748 info->blocked_open++;
2751 if ((tty->termios->c_cflag & CBAUD)) {
2752 spin_lock_irqsave(&info->lock,flags);
2753 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2755 spin_unlock_irqrestore(&info->lock,flags);
2758 set_current_state(TASK_INTERRUPTIBLE);
2760 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
2761 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
2762 -EAGAIN : -ERESTARTSYS;
2766 spin_lock_irqsave(&info->lock,flags);
2768 spin_unlock_irqrestore(&info->lock,flags);
2770 if (!(info->flags & ASYNC_CLOSING) &&
2771 (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
2775 if (signal_pending(current)) {
2776 retval = -ERESTARTSYS;
2780 if (debug_level >= DEBUG_LEVEL_INFO)
2781 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
2782 __FILE__,__LINE__, tty->driver->name, info->count );
2787 set_current_state(TASK_RUNNING);
2788 remove_wait_queue(&info->open_wait, &wait);
2792 info->blocked_open--;
2794 if (debug_level >= DEBUG_LEVEL_INFO)
2795 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
2796 __FILE__,__LINE__, tty->driver->name, info->count );
2799 info->flags |= ASYNC_NORMAL_ACTIVE;
2804 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2808 unsigned long flags;
2810 /* verify range of specified line number */
2812 if ((line < 0) || (line >= mgslpc_device_count)) {
2813 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2814 __FILE__,__LINE__,line);
2818 /* find the info structure for the specified line */
2819 info = mgslpc_device_list;
2820 while(info && info->line != line)
2821 info = info->next_device;
2822 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2825 tty->driver_data = info;
2828 if (debug_level >= DEBUG_LEVEL_INFO)
2829 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2830 __FILE__,__LINE__,tty->driver->name, info->count);
2832 /* If port is closing, signal caller to try again */
2833 if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
2834 if (info->flags & ASYNC_CLOSING)
2835 interruptible_sleep_on(&info->close_wait);
2836 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2837 -EAGAIN : -ERESTARTSYS);
2841 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2843 spin_lock_irqsave(&info->netlock, flags);
2844 if (info->netcount) {
2846 spin_unlock_irqrestore(&info->netlock, flags);
2850 spin_unlock_irqrestore(&info->netlock, flags);
2852 if (info->count == 1) {
2853 /* 1st open on this device, init hardware */
2854 retval = startup(info);
2859 retval = block_til_ready(tty, filp, info);
2861 if (debug_level >= DEBUG_LEVEL_INFO)
2862 printk("%s(%d):block_til_ready(%s) returned %d\n",
2863 __FILE__,__LINE__, info->device_name, retval);
2867 if (debug_level >= DEBUG_LEVEL_INFO)
2868 printk("%s(%d):mgslpc_open(%s) success\n",
2869 __FILE__,__LINE__, info->device_name);
2874 if (tty->count == 1)
2875 info->tty = 0; /* tty layer will release tty struct */
2884 * /proc fs routines....
2887 static inline int line_info(char *buf, MGSLPC_INFO *info)
2891 unsigned long flags;
2893 ret = sprintf(buf, "%s:io:%04X irq:%d",
2894 info->device_name, info->io_base, info->irq_level);
2896 /* output current serial signal states */
2897 spin_lock_irqsave(&info->lock,flags);
2899 spin_unlock_irqrestore(&info->lock,flags);
2903 if (info->serial_signals & SerialSignal_RTS)
2904 strcat(stat_buf, "|RTS");
2905 if (info->serial_signals & SerialSignal_CTS)
2906 strcat(stat_buf, "|CTS");
2907 if (info->serial_signals & SerialSignal_DTR)
2908 strcat(stat_buf, "|DTR");
2909 if (info->serial_signals & SerialSignal_DSR)
2910 strcat(stat_buf, "|DSR");
2911 if (info->serial_signals & SerialSignal_DCD)
2912 strcat(stat_buf, "|CD");
2913 if (info->serial_signals & SerialSignal_RI)
2914 strcat(stat_buf, "|RI");
2916 if (info->params.mode == MGSL_MODE_HDLC) {
2917 ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
2918 info->icount.txok, info->icount.rxok);
2919 if (info->icount.txunder)
2920 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
2921 if (info->icount.txabort)
2922 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
2923 if (info->icount.rxshort)
2924 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
2925 if (info->icount.rxlong)
2926 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
2927 if (info->icount.rxover)
2928 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
2929 if (info->icount.rxcrc)
2930 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxcrc);
2932 ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
2933 info->icount.tx, info->icount.rx);
2934 if (info->icount.frame)
2935 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2936 if (info->icount.parity)
2937 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2938 if (info->icount.brk)
2939 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2940 if (info->icount.overrun)
2941 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2944 /* Append serial signal status to end */
2945 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2947 ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2948 info->tx_active,info->bh_requested,info->bh_running,
2954 /* Called to print information about devices
2956 int mgslpc_read_proc(char *page, char **start, off_t off, int count,
2957 int *eof, void *data)
2963 len += sprintf(page, "synclink driver:%s\n", driver_version);
2965 info = mgslpc_device_list;
2967 l = line_info(page + len, info);
2969 if (len+begin > off+count)
2971 if (len+begin < off) {
2975 info = info->next_device;
2980 if (off >= len+begin)
2982 *start = page + (off-begin);
2983 return ((count < begin+len-off) ? count : begin+len-off);
2986 int rx_alloc_buffers(MGSLPC_INFO *info)
2988 /* each buffer has header and data */
2989 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2991 /* calculate total allocation size for 8 buffers */
2992 info->rx_buf_total_size = info->rx_buf_size * 8;
2994 /* limit total allocated memory */
2995 if (info->rx_buf_total_size > 0x10000)
2996 info->rx_buf_total_size = 0x10000;
2998 /* calculate number of buffers */
2999 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
3001 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
3002 if (info->rx_buf == NULL)
3005 rx_reset_buffers(info);
3009 void rx_free_buffers(MGSLPC_INFO *info)
3012 kfree(info->rx_buf);
3013 info->rx_buf = NULL;
3016 int claim_resources(MGSLPC_INFO *info)
3018 if (rx_alloc_buffers(info) < 0 ) {
3019 printk( "Cant allocate rx buffer %s\n", info->device_name);
3020 release_resources(info);
3026 void release_resources(MGSLPC_INFO *info)
3028 if (debug_level >= DEBUG_LEVEL_INFO)
3029 printk("release_resources(%s)\n", info->device_name);
3030 rx_free_buffers(info);
3033 /* Add the specified device instance data structure to the
3034 * global linked list of devices and increment the device count.
3036 * Arguments: info pointer to device instance data
3038 void mgslpc_add_device(MGSLPC_INFO *info)
3040 info->next_device = NULL;
3041 info->line = mgslpc_device_count;
3042 sprintf(info->device_name,"ttySLP%d",info->line);
3044 if (info->line < MAX_DEVICE_COUNT) {
3045 if (maxframe[info->line])
3046 info->max_frame_size = maxframe[info->line];
3047 info->dosyncppp = dosyncppp[info->line];
3050 mgslpc_device_count++;
3052 if (!mgslpc_device_list)
3053 mgslpc_device_list = info;
3055 MGSLPC_INFO *current_dev = mgslpc_device_list;
3056 while( current_dev->next_device )
3057 current_dev = current_dev->next_device;
3058 current_dev->next_device = info;
3061 if (info->max_frame_size < 4096)
3062 info->max_frame_size = 4096;
3063 else if (info->max_frame_size > 65535)
3064 info->max_frame_size = 65535;
3066 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
3067 info->device_name, info->io_base, info->irq_level);
3070 #ifdef CONFIG_SYNCLINK_SYNCPPP
3072 if (info->dosyncppp)
3074 mgslpc_sppp_init(info);
3078 void mgslpc_remove_device(MGSLPC_INFO *remove_info)
3080 MGSLPC_INFO *info = mgslpc_device_list;
3081 MGSLPC_INFO *last = NULL;
3084 if (info == remove_info) {
3086 last->next_device = info->next_device;
3088 mgslpc_device_list = info->next_device;
3089 #ifdef CONFIG_SYNCLINK_SYNCPPP
3090 if (info->dosyncppp)
3091 mgslpc_sppp_delete(info);
3093 release_resources(info);
3095 mgslpc_device_count--;
3099 info = info->next_device;
3103 static struct pcmcia_driver mgslpc_driver = {
3104 .owner = THIS_MODULE,
3106 .name = "synclink_cs",
3108 .attach = mgslpc_attach,
3109 .detach = mgslpc_detach,
3112 static struct tty_operations mgslpc_ops = {
3113 .open = mgslpc_open,
3114 .close = mgslpc_close,
3115 .write = mgslpc_write,
3116 .put_char = mgslpc_put_char,
3117 .flush_chars = mgslpc_flush_chars,
3118 .write_room = mgslpc_write_room,
3119 .chars_in_buffer = mgslpc_chars_in_buffer,
3120 .flush_buffer = mgslpc_flush_buffer,
3121 .ioctl = mgslpc_ioctl,
3122 .throttle = mgslpc_throttle,
3123 .unthrottle = mgslpc_unthrottle,
3124 .send_xchar = mgslpc_send_xchar,
3125 .break_ctl = mgslpc_break,
3126 .wait_until_sent = mgslpc_wait_until_sent,
3127 .read_proc = mgslpc_read_proc,
3128 .set_termios = mgslpc_set_termios,
3130 .start = tx_release,
3131 .hangup = mgslpc_hangup,
3132 .tiocmget = tiocmget,
3133 .tiocmset = tiocmset,
3136 static void synclink_cs_cleanup(void)
3140 printk("Unloading %s: version %s\n", driver_name, driver_version);
3142 while(mgslpc_device_list)
3143 mgslpc_remove_device(mgslpc_device_list);
3145 if (serial_driver) {
3146 if ((rc = tty_unregister_driver(serial_driver)))
3147 printk("%s(%d) failed to unregister tty driver err=%d\n",
3148 __FILE__,__LINE__,rc);
3149 put_tty_driver(serial_driver);
3152 pcmcia_unregister_driver(&mgslpc_driver);
3154 /* XXX: this really needs to move into generic code.. */
3155 while (dev_list != NULL) {
3156 if (dev_list->state & DEV_CONFIG)
3157 mgslpc_release((u_long)dev_list);
3158 mgslpc_detach(dev_list);
3162 static int __init synclink_cs_init(void)
3166 if (break_on_load) {
3167 mgslpc_get_text_ptr();
3171 printk("%s %s\n", driver_name, driver_version);
3173 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
3176 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
3177 if (!serial_driver) {
3182 /* Initialize the tty_driver structure */
3184 serial_driver->owner = THIS_MODULE;
3185 serial_driver->driver_name = "synclink_cs";
3186 serial_driver->name = "ttySLP";
3187 serial_driver->major = ttymajor;
3188 serial_driver->minor_start = 64;
3189 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3190 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3191 serial_driver->init_termios = tty_std_termios;
3192 serial_driver->init_termios.c_cflag =
3193 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3194 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3195 tty_set_operations(serial_driver, &mgslpc_ops);
3197 if ((rc = tty_register_driver(serial_driver)) < 0) {
3198 printk("%s(%d):Couldn't register serial driver\n",
3200 put_tty_driver(serial_driver);
3201 serial_driver = NULL;
3205 printk("%s %s, tty major#%d\n",
3206 driver_name, driver_version,
3207 serial_driver->major);
3212 synclink_cs_cleanup();
3216 static void __exit synclink_cs_exit(void)
3218 synclink_cs_cleanup();
3221 module_init(synclink_cs_init);
3222 module_exit(synclink_cs_exit);
3224 void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
3229 /* note:standard BRG mode is broken in V3.2 chip
3230 * so enhanced mode is always used
3238 for (M = 1; N > 64 && M < 16; M++)
3244 * BGR[7..0] contained in BGR register
3245 * BGR[9..8] contained in CCR2[7..6]
3246 * divisor = (N+1)*2^M
3248 * Note: M *must* not be zero (causes asymetric duty cycle)
3250 write_reg(info, (unsigned char) (channel + BGR),
3251 (unsigned char) ((M << 6) + N));
3252 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
3253 val |= ((M << 4) & 0xc0);
3254 write_reg(info, (unsigned char) (channel + CCR2), val);
3258 /* Enabled the AUX clock output at the specified frequency.
3260 void enable_auxclk(MGSLPC_INFO *info)
3266 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3267 * 05 ADM Address Mode, 0 = no addr recognition
3268 * 04 TMD Timer Mode, 0 = external
3269 * 03 RAC Receiver Active, 0 = inactive
3270 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3271 * 01 TRS Timer Resolution, 1=512
3272 * 00 TLP Test Loop, 0 = no loop
3278 /* channel B RTS is used to enable AUXCLK driver on SP505 */
3279 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3281 write_reg(info, CHB + MODE, val);
3285 * 07 PU Power Up, 1=active, 0=power down
3286 * 06 MCE Master Clock Enable, 1=enabled
3288 * 04..02 SC[2..0] Encoding
3289 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3293 write_reg(info, CHB + CCR0, 0xc0);
3297 * 07 SFLG Shared Flag, 0 = disable shared flags
3298 * 06 GALP Go Active On Loop, 0 = not used
3299 * 05 GLP Go On Loop, 0 = not used
3300 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3301 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3302 * 02..00 CM[2..0] Clock Mode
3306 write_reg(info, CHB + CCR1, 0x17);
3310 * 07..06 BGR[9..8] Baud rate bits 9..8
3311 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3312 * 04 SSEL Clock source select, 1=submode b
3313 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3314 * 02 RWX Read/Write Exchange 0=disabled
3315 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3316 * 00 DIV, data inversion 0=disabled, 1=enabled
3320 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3321 write_reg(info, CHB + CCR2, 0x38);
3323 write_reg(info, CHB + CCR2, 0x30);
3327 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3328 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3329 * 05 TST1 Test Pin, 0=normal operation
3330 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3331 * 03..02 Reserved, must be 0
3332 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3336 write_reg(info, CHB + CCR4, 0x50);
3338 /* if auxclk not enabled, set internal BRG so
3339 * CTS transitions can be detected (requires TxC)
3341 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3342 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3344 mgslpc_set_rate(info, CHB, 921600);
3347 static void loopback_enable(MGSLPC_INFO *info)
3351 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3352 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3353 write_reg(info, CHA + CCR1, val);
3355 /* CCR2:04 SSEL Clock source select, 1=submode b */
3356 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3357 write_reg(info, CHA + CCR2, val);
3359 /* set LinkSpeed if available, otherwise default to 2Mbps */
3360 if (info->params.clock_speed)
3361 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3363 mgslpc_set_rate(info, CHA, 1843200);
3365 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3366 val = read_reg(info, CHA + MODE) | BIT0;
3367 write_reg(info, CHA + MODE, val);
3370 void hdlc_mode(MGSLPC_INFO *info)
3373 unsigned char clkmode, clksubmode;
3375 /* disable all interrupts */
3376 irq_disable(info, CHA, 0xffff);
3377 irq_disable(info, CHB, 0xffff);
3378 port_irq_disable(info, 0xff);
3380 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3381 clkmode = clksubmode = 0;
3382 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3383 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3384 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3386 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3387 && info->params.flags & HDLC_FLAG_TXC_BRG) {
3388 /* clock mode 7b, rcv = BRG, xmt = BRG */
3391 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3392 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3393 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3397 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3400 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3401 /* clock mode 0b, rcv = RxC, xmt = BRG */
3407 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3408 * 05 ADM Address Mode, 0 = no addr recognition
3409 * 04 TMD Timer Mode, 0 = external
3410 * 03 RAC Receiver Active, 0 = inactive
3411 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3412 * 01 TRS Timer Resolution, 1=512
3413 * 00 TLP Test Loop, 0 = no loop
3418 if (info->params.loopback)
3421 /* preserve RTS state */
3422 if (info->serial_signals & SerialSignal_RTS)
3424 write_reg(info, CHA + MODE, val);
3428 * 07 PU Power Up, 1=active, 0=power down
3429 * 06 MCE Master Clock Enable, 1=enabled
3431 * 04..02 SC[2..0] Encoding
3432 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3437 switch (info->params.encoding)
3439 case HDLC_ENCODING_NRZI:
3442 case HDLC_ENCODING_BIPHASE_SPACE:
3445 case HDLC_ENCODING_BIPHASE_MARK:
3448 case HDLC_ENCODING_BIPHASE_LEVEL:
3450 break; // Manchester
3452 write_reg(info, CHA + CCR0, val);
3456 * 07 SFLG Shared Flag, 0 = disable shared flags
3457 * 06 GALP Go Active On Loop, 0 = not used
3458 * 05 GLP Go On Loop, 0 = not used
3459 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3460 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3461 * 02..00 CM[2..0] Clock Mode
3465 val = 0x10 + clkmode;
3466 write_reg(info, CHA + CCR1, val);
3470 * 07..06 BGR[9..8] Baud rate bits 9..8
3471 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3472 * 04 SSEL Clock source select, 1=submode b
3473 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3474 * 02 RWX Read/Write Exchange 0=disabled
3475 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3476 * 00 DIV, data inversion 0=disabled, 1=enabled
3481 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3482 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3486 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3488 if (info->params.encoding == HDLC_ENCODING_NRZB)
3490 write_reg(info, CHA + CCR2, val);
3494 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3495 * 05 EPT Enable preamble transmission, 1=enabled
3496 * 04 RADD Receive address pushed to FIFO, 0=disabled
3497 * 03 CRL CRC Reset Level, 0=FFFF
3498 * 02 RCRC Rx CRC 0=On 1=Off
3499 * 01 TCRC Tx CRC 0=On 1=Off
3500 * 00 PSD DPLL Phase Shift Disable
3505 if (info->params.crc_type == HDLC_CRC_NONE)
3507 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3509 switch (info->params.preamble_length)
3511 case HDLC_PREAMBLE_LENGTH_16BITS:
3514 case HDLC_PREAMBLE_LENGTH_32BITS:
3517 case HDLC_PREAMBLE_LENGTH_64BITS:
3521 write_reg(info, CHA + CCR3, val);
3523 /* PRE - Preamble pattern */
3525 switch (info->params.preamble)
3527 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3528 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3529 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3530 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3532 write_reg(info, CHA + PRE, val);
3536 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3537 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3538 * 05 TST1 Test Pin, 0=normal operation
3539 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3540 * 03..02 Reserved, must be 0
3541 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3546 write_reg(info, CHA + CCR4, val);
3547 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3548 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3550 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3552 /* RLCR Receive length check register
3554 * 7 1=enable receive length check
3555 * 6..0 Max frame length = (RL + 1) * 32
3557 write_reg(info, CHA + RLCR, 0);
3559 /* XBCH Transmit Byte Count High
3561 * 07 DMA mode, 0 = interrupt driven
3562 * 06 NRM, 0=ABM (ignored)
3563 * 05 CAS Carrier Auto Start
3564 * 04 XC Transmit Continuously (ignored)
3565 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3570 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3572 write_reg(info, CHA + XBCH, val);
3573 enable_auxclk(info);
3574 if (info->params.loopback || info->testing_irq)
3575 loopback_enable(info);
3576 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3578 irq_enable(info, CHB, IRQ_CTS);
3579 /* PVR[3] 1=AUTO CTS active */
3580 set_reg_bits(info, CHA + PVR, BIT3);
3582 clear_reg_bits(info, CHA + PVR, BIT3);
3584 irq_enable(info, CHA,
3585 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3586 IRQ_UNDERRUN + IRQ_TXFIFO);
3587 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3588 wait_command_complete(info, CHA);
3589 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3591 /* Master clock mode enabled above to allow reset commands
3592 * to complete even if no data clocks are present.
3594 * Disable master clock mode for normal communications because
3595 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3596 * IRQ when in master clock mode.
3598 * Leave master clock mode enabled for IRQ test because the
3599 * timer IRQ used by the test can only happen in master clock mode.
3601 if (!info->testing_irq)
3602 clear_reg_bits(info, CHA + CCR0, BIT6);
3610 void rx_stop(MGSLPC_INFO *info)
3612 if (debug_level >= DEBUG_LEVEL_ISR)
3613 printk("%s(%d):rx_stop(%s)\n",
3614 __FILE__,__LINE__, info->device_name );
3616 /* MODE:03 RAC Receiver Active, 0=inactive */
3617 clear_reg_bits(info, CHA + MODE, BIT3);
3619 info->rx_enabled = 0;
3620 info->rx_overflow = 0;
3623 void rx_start(MGSLPC_INFO *info)
3625 if (debug_level >= DEBUG_LEVEL_ISR)
3626 printk("%s(%d):rx_start(%s)\n",
3627 __FILE__,__LINE__, info->device_name );
3629 rx_reset_buffers(info);
3630 info->rx_enabled = 0;
3631 info->rx_overflow = 0;
3633 /* MODE:03 RAC Receiver Active, 1=active */
3634 set_reg_bits(info, CHA + MODE, BIT3);
3636 info->rx_enabled = 1;
3639 void tx_start(MGSLPC_INFO *info)
3641 if (debug_level >= DEBUG_LEVEL_ISR)
3642 printk("%s(%d):tx_start(%s)\n",
3643 __FILE__,__LINE__, info->device_name );
3645 if (info->tx_count) {
3646 /* If auto RTS enabled and RTS is inactive, then assert */
3647 /* RTS and set a flag indicating that the driver should */
3648 /* negate RTS when the transmission completes. */
3649 info->drop_rts_on_tx_done = 0;
3651 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3653 if (!(info->serial_signals & SerialSignal_RTS)) {
3654 info->serial_signals |= SerialSignal_RTS;
3656 info->drop_rts_on_tx_done = 1;
3660 if (info->params.mode == MGSL_MODE_ASYNC) {
3661 if (!info->tx_active) {
3662 info->tx_active = 1;
3666 info->tx_active = 1;
3668 info->tx_timer.expires = jiffies + jiffies_from_ms(5000);
3669 add_timer(&info->tx_timer);
3673 if (!info->tx_enabled)
3674 info->tx_enabled = 1;
3677 void tx_stop(MGSLPC_INFO *info)
3679 if (debug_level >= DEBUG_LEVEL_ISR)
3680 printk("%s(%d):tx_stop(%s)\n",
3681 __FILE__,__LINE__, info->device_name );
3683 del_timer(&info->tx_timer);
3685 info->tx_enabled = 0;
3686 info->tx_active = 0;
3689 /* Reset the adapter to a known state and prepare it for further use.
3691 void reset_device(MGSLPC_INFO *info)
3693 /* power up both channels (set BIT7) */
3694 write_reg(info, CHA + CCR0, 0x80);
3695 write_reg(info, CHB + CCR0, 0x80);
3696 write_reg(info, CHA + MODE, 0);
3697 write_reg(info, CHB + MODE, 0);
3699 /* disable all interrupts */
3700 irq_disable(info, CHA, 0xffff);
3701 irq_disable(info, CHB, 0xffff);
3702 port_irq_disable(info, 0xff);
3704 /* PCR Port Configuration Register
3706 * 07..04 DEC[3..0] Serial I/F select outputs
3707 * 03 output, 1=AUTO CTS control enabled
3708 * 02 RI Ring Indicator input 0=active
3709 * 01 DSR input 0=active
3710 * 00 DTR output 0=active
3714 write_reg(info, PCR, 0x06);
3716 /* PVR Port Value Register
3718 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3719 * 03 AUTO CTS output 1=enabled
3720 * 02 RI Ring Indicator input
3722 * 00 DTR output (1=inactive)
3726 // write_reg(info, PVR, PVR_DTR);
3728 /* IPC Interrupt Port Configuration
3730 * 07 VIS 1=Masked interrupts visible
3731 * 06..05 Reserved, 0
3732 * 04..03 SLA Slave address, 00 ignored
3733 * 02 CASM Cascading Mode, 1=daisy chain
3734 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3738 write_reg(info, IPC, 0x05);
3741 void async_mode(MGSLPC_INFO *info)
3745 /* disable all interrupts */
3746 irq_disable(info, CHA, 0xffff);
3747 irq_disable(info, CHB, 0xffff);
3748 port_irq_disable(info, 0xff);
3753 * 06 FRTS RTS State, 0=active
3754 * 05 FCTS Flow Control on CTS
3755 * 04 FLON Flow Control Enable
3756 * 03 RAC Receiver Active, 0 = inactive
3757 * 02 RTS 0=Auto RTS, 1=manual RTS
3758 * 01 TRS Timer Resolution, 1=512
3759 * 00 TLP Test Loop, 0 = no loop
3764 if (info->params.loopback)
3767 /* preserve RTS state */
3768 if (!(info->serial_signals & SerialSignal_RTS))
3770 write_reg(info, CHA + MODE, val);
3774 * 07 PU Power Up, 1=active, 0=power down
3775 * 06 MCE Master Clock Enable, 1=enabled
3777 * 04..02 SC[2..0] Encoding, 000=NRZ
3778 * 01..00 SM[1..0] Serial Mode, 11=Async
3782 write_reg(info, CHA + CCR0, 0x83);
3786 * 07..05 Reserved, 0
3787 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3788 * 03 BCR Bit Clock Rate, 1=16x
3789 * 02..00 CM[2..0] Clock Mode, 111=BRG
3793 write_reg(info, CHA + CCR1, 0x1f);
3797 * 07..06 BGR[9..8] Baud rate bits 9..8
3798 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3799 * 04 SSEL Clock source select, 1=submode b
3800 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3801 * 02 RWX Read/Write Exchange 0=disabled
3803 * 00 DIV, data inversion 0=disabled, 1=enabled
3807 write_reg(info, CHA + CCR2, 0x10);
3811 * 07..01 Reserved, 0
3812 * 00 PSD DPLL Phase Shift Disable
3816 write_reg(info, CHA + CCR3, 0);
3820 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3821 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3822 * 05 TST1 Test Pin, 0=normal operation
3823 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3824 * 03..00 Reserved, must be 0
3828 write_reg(info, CHA + CCR4, 0x50);
3829 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3834 * 06 XBRK transmit break, 0=normal operation
3835 * 05 Stop bits (0=1, 1=2)
3836 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3837 * 02 PAREN Parity Enable
3838 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3842 if (info->params.data_bits != 8)
3843 val |= BIT0; /* 7 bits */
3844 if (info->params.stop_bits != 1)
3846 if (info->params.parity != ASYNC_PARITY_NONE)
3848 val |= BIT2; /* Parity enable */
3849 if (info->params.parity == ASYNC_PARITY_ODD)
3854 write_reg(info, CHA + DAFO, val);
3856 /* RFC Rx FIFO Control
3859 * 06 DPS, 1=parity bit not stored in data byte
3860 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3861 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3862 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3864 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3868 write_reg(info, CHA + RFC, 0x5c);
3870 /* RLCR Receive length check register
3872 * Max frame length = (RL + 1) * 32
3874 write_reg(info, CHA + RLCR, 0);
3876 /* XBCH Transmit Byte Count High
3878 * 07 DMA mode, 0 = interrupt driven
3879 * 06 NRM, 0=ABM (ignored)
3880 * 05 CAS Carrier Auto Start
3881 * 04 XC Transmit Continuously (ignored)
3882 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3887 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3889 write_reg(info, CHA + XBCH, val);
3890 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3891 irq_enable(info, CHA, IRQ_CTS);
3893 /* MODE:03 RAC Receiver Active, 1=active */
3894 set_reg_bits(info, CHA + MODE, BIT3);
3895 enable_auxclk(info);
3896 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3897 irq_enable(info, CHB, IRQ_CTS);
3898 /* PVR[3] 1=AUTO CTS active */
3899 set_reg_bits(info, CHA + PVR, BIT3);
3901 clear_reg_bits(info, CHA + PVR, BIT3);
3902 irq_enable(info, CHA,
3903 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3904 IRQ_ALLSENT + IRQ_TXFIFO);
3905 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3906 wait_command_complete(info, CHA);
3907 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3910 /* Set the HDLC idle mode for the transmitter.
3912 void tx_set_idle(MGSLPC_INFO *info)
3914 /* Note: ESCC2 only supports flags and one idle modes */
3915 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3916 set_reg_bits(info, CHA + CCR1, BIT3);
3918 clear_reg_bits(info, CHA + CCR1, BIT3);
3921 /* get state of the V24 status (input) signals.
3923 void get_signals(MGSLPC_INFO *info)
3925 unsigned char status = 0;
3927 /* preserve DTR and RTS */
3928 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3930 if (read_reg(info, CHB + VSTR) & BIT7)
3931 info->serial_signals |= SerialSignal_DCD;
3932 if (read_reg(info, CHB + STAR) & BIT1)
3933 info->serial_signals |= SerialSignal_CTS;
3935 status = read_reg(info, CHA + PVR);
3936 if (!(status & PVR_RI))
3937 info->serial_signals |= SerialSignal_RI;
3938 if (!(status & PVR_DSR))
3939 info->serial_signals |= SerialSignal_DSR;
3942 /* Set the state of DTR and RTS based on contents of
3943 * serial_signals member of device extension.
3945 void set_signals(MGSLPC_INFO *info)
3949 val = read_reg(info, CHA + MODE);
3950 if (info->params.mode == MGSL_MODE_ASYNC) {
3951 if (info->serial_signals & SerialSignal_RTS)
3956 if (info->serial_signals & SerialSignal_RTS)
3961 write_reg(info, CHA + MODE, val);
3963 if (info->serial_signals & SerialSignal_DTR)
3964 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3966 set_reg_bits(info, CHA + PVR, PVR_DTR);
3969 void rx_reset_buffers(MGSLPC_INFO *info)
3976 info->rx_frame_count = 0;
3977 for (i=0 ; i < info->rx_buf_count ; i++) {
3978 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3979 buf->status = buf->count = 0;
3983 /* Attempt to return a received HDLC frame
3984 * Only frames received without errors are returned.
3986 * Returns 1 if frame returned, otherwise 0
3988 int rx_get_frame(MGSLPC_INFO *info)
3990 unsigned short status;
3992 unsigned int framesize = 0;
3993 unsigned long flags;
3994 struct tty_struct *tty = info->tty;
3995 int return_frame = 0;
3997 if (info->rx_frame_count == 0)
4000 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
4002 status = buf->status;
4004 /* 07 VFR 1=valid frame
4005 * 06 RDO 1=data overrun
4006 * 05 CRC 1=OK, 0=error
4007 * 04 RAB 1=frame aborted
4009 if ((status & 0xf0) != 0xA0) {
4010 if (!(status & BIT7) || (status & BIT4))
4011 info->icount.rxabort++;
4012 else if (status & BIT6)
4013 info->icount.rxover++;
4014 else if (!(status & BIT5)) {
4015 info->icount.rxcrc++;
4016 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
4020 #ifdef CONFIG_SYNCLINK_SYNCPPP
4021 info->netstats.rx_errors++;
4022 info->netstats.rx_frame_errors++;
4028 framesize = buf->count;
4030 if (debug_level >= DEBUG_LEVEL_BH)
4031 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
4032 __FILE__,__LINE__,info->device_name,status,framesize);
4034 if (debug_level >= DEBUG_LEVEL_DATA)
4035 trace_block(info, buf->data, framesize, 0);
4038 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
4039 framesize+1 > info->max_frame_size) ||
4040 framesize > info->max_frame_size)
4041 info->icount.rxlong++;
4044 info->icount.rxok++;
4046 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4047 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
4051 #ifdef CONFIG_SYNCLINK_SYNCPPP
4052 if (info->netcount) {
4053 /* pass frame to syncppp device */
4054 mgslpc_sppp_rx_done(info, buf->data, framesize);
4059 /* Call the line discipline receive callback directly. */
4060 if (tty && tty->ldisc.receive_buf)
4061 tty->ldisc.receive_buf(tty, buf->data, info->flag_buf, framesize);
4066 spin_lock_irqsave(&info->lock,flags);
4067 buf->status = buf->count = 0;
4068 info->rx_frame_count--;
4070 if (info->rx_get >= info->rx_buf_count)
4072 spin_unlock_irqrestore(&info->lock,flags);
4077 BOOLEAN register_test(MGSLPC_INFO *info)
4079 static unsigned char patterns[] =
4080 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
4081 static unsigned int count = sizeof(patterns) / sizeof(patterns[0]);
4084 unsigned long flags;
4086 spin_lock_irqsave(&info->lock,flags);
4089 for (i = 0; i < count; i++) {
4090 write_reg(info, XAD1, patterns[i]);
4091 write_reg(info, XAD2, patterns[(i + 1) % count]);
4092 if ((read_reg(info, XAD1) != patterns[i]) ||
4093 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
4099 spin_unlock_irqrestore(&info->lock,flags);
4103 BOOLEAN irq_test(MGSLPC_INFO *info)
4105 unsigned long end_time;
4106 unsigned long flags;
4108 spin_lock_irqsave(&info->lock,flags);
4111 info->testing_irq = TRUE;
4114 info->irq_occurred = FALSE;
4116 /* init hdlc mode */
4118 irq_enable(info, CHA, IRQ_TIMER);
4119 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
4120 issue_command(info, CHA, CMD_START_TIMER);
4122 spin_unlock_irqrestore(&info->lock,flags);
4125 while(end_time-- && !info->irq_occurred) {
4126 set_current_state(TASK_INTERRUPTIBLE);
4127 schedule_timeout(jiffies_from_ms(10));
4130 info->testing_irq = FALSE;
4132 spin_lock_irqsave(&info->lock,flags);
4134 spin_unlock_irqrestore(&info->lock,flags);
4136 return info->irq_occurred ? TRUE : FALSE;
4139 int adapter_test(MGSLPC_INFO *info)
4141 if (!register_test(info)) {
4142 info->init_error = DiagStatus_AddressFailure;
4143 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
4144 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
4148 if (!irq_test(info)) {
4149 info->init_error = DiagStatus_IrqFailure;
4150 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
4151 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
4155 if (debug_level >= DEBUG_LEVEL_INFO)
4156 printk("%s(%d):device %s passed diagnostics\n",
4157 __FILE__,__LINE__,info->device_name);
4161 void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
4166 printk("%s tx data:\n",info->device_name);
4168 printk("%s rx data:\n",info->device_name);
4176 for(i=0;i<linecount;i++)
4177 printk("%02X ",(unsigned char)data[i]);
4180 for(i=0;i<linecount;i++) {
4181 if (data[i]>=040 && data[i]<=0176)
4182 printk("%c",data[i]);
4193 /* HDLC frame time out
4194 * update stats and do tx completion processing
4196 void tx_timeout(unsigned long context)
4198 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
4199 unsigned long flags;
4201 if ( debug_level >= DEBUG_LEVEL_INFO )
4202 printk( "%s(%d):tx_timeout(%s)\n",
4203 __FILE__,__LINE__,info->device_name);
4204 if(info->tx_active &&
4205 info->params.mode == MGSL_MODE_HDLC) {
4206 info->icount.txtimeout++;
4208 spin_lock_irqsave(&info->lock,flags);
4209 info->tx_active = 0;
4210 info->tx_count = info->tx_put = info->tx_get = 0;
4212 spin_unlock_irqrestore(&info->lock,flags);
4214 #ifdef CONFIG_SYNCLINK_SYNCPPP
4216 mgslpc_sppp_tx_done(info);
4222 #ifdef CONFIG_SYNCLINK_SYNCPPP
4223 /* syncppp net device routines
4226 static void mgslpc_setup(struct net_device *dev)
4228 dev->open = mgslpc_sppp_open;
4229 dev->stop = mgslpc_sppp_close;
4230 dev->hard_start_xmit = mgslpc_sppp_tx;
4231 dev->do_ioctl = mgslpc_sppp_ioctl;
4232 dev->get_stats = mgslpc_net_stats;
4233 dev->tx_timeout = mgslpc_sppp_tx_timeout;
4234 dev->watchdog_timeo = 10*HZ;
4237 void mgslpc_sppp_init(MGSLPC_INFO *info)
4239 struct net_device *d;
4241 sprintf(info->netname,"mgslp%d",info->line);
4243 d = alloc_netdev(0, info->netname, mgslpc_setup);
4245 printk(KERN_WARNING "%s: alloc_netdev failed.\n",
4250 info->if_ptr = &info->pppdev;
4251 info->netdev = info->pppdev.dev = d;
4253 d->base_addr = info->io_base;
4254 d->irq = info->irq_level;
4257 sppp_attach(&info->pppdev);
4260 if (register_netdev(d)) {
4261 printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
4262 sppp_detach(info->netdev);
4263 info->netdev = NULL;
4264 info->pppdev.dev = NULL;
4269 if (debug_level >= DEBUG_LEVEL_INFO)
4270 printk("mgslpc_sppp_init()\n");
4273 void mgslpc_sppp_delete(MGSLPC_INFO *info)
4275 if (debug_level >= DEBUG_LEVEL_INFO)
4276 printk("mgslpc_sppp_delete(%s)\n",info->netname);
4277 unregister_netdev(info->netdev);
4278 sppp_detach(info->netdev);
4279 free_netdev(info->netdev);
4280 info->netdev = NULL;
4281 info->pppdev.dev = NULL;
4284 int mgslpc_sppp_open(struct net_device *d)
4286 MGSLPC_INFO *info = d->priv;
4288 unsigned long flags;
4290 if (debug_level >= DEBUG_LEVEL_INFO)
4291 printk("mgslpc_sppp_open(%s)\n",info->netname);
4293 spin_lock_irqsave(&info->netlock, flags);
4294 if (info->count != 0 || info->netcount != 0) {
4295 printk(KERN_WARNING "%s: sppp_open returning busy\n", info->netname);
4296 spin_unlock_irqrestore(&info->netlock, flags);
4300 spin_unlock_irqrestore(&info->netlock, flags);
4302 /* claim resources and init adapter */
4303 if ((err = startup(info)) != 0)
4306 /* allow syncppp module to do open processing */
4307 if ((err = sppp_open(d)) != 0) {
4312 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4313 mgslpc_program_hw(info);
4315 d->trans_start = jiffies;
4316 netif_start_queue(d);
4320 spin_lock_irqsave(&info->netlock, flags);
4322 spin_unlock_irqrestore(&info->netlock, flags);
4326 void mgslpc_sppp_tx_timeout(struct net_device *dev)
4328 MGSLPC_INFO *info = dev->priv;
4329 unsigned long flags;
4331 if (debug_level >= DEBUG_LEVEL_INFO)
4332 printk("mgslpc_sppp_tx_timeout(%s)\n",info->netname);
4334 info->netstats.tx_errors++;
4335 info->netstats.tx_aborted_errors++;
4337 spin_lock_irqsave(&info->lock,flags);
4339 spin_unlock_irqrestore(&info->lock,flags);
4341 netif_wake_queue(dev);
4344 int mgslpc_sppp_tx(struct sk_buff *skb, struct net_device *dev)
4346 MGSLPC_INFO *info = dev->priv;
4347 unsigned long flags;
4349 if (debug_level >= DEBUG_LEVEL_INFO)
4350 printk("mgslpc_sppp_tx(%s)\n",info->netname);
4352 netif_stop_queue(dev);
4354 info->tx_count = skb->len;
4356 memcpy(info->tx_buf, skb->data, skb->len);
4358 info->tx_put = info->tx_count = skb->len;
4360 info->netstats.tx_packets++;
4361 info->netstats.tx_bytes += skb->len;
4364 dev->trans_start = jiffies;
4366 spin_lock_irqsave(&info->lock,flags);
4367 if (!info->tx_active)
4369 spin_unlock_irqrestore(&info->lock,flags);
4374 int mgslpc_sppp_close(struct net_device *d)
4376 MGSLPC_INFO *info = d->priv;
4377 unsigned long flags;
4379 if (debug_level >= DEBUG_LEVEL_INFO)
4380 printk("mgslpc_sppp_close(%s)\n",info->netname);
4382 /* shutdown adapter and release resources */
4385 /* allow syncppp to do close processing */
4387 netif_stop_queue(d);
4389 spin_lock_irqsave(&info->netlock, flags);
4391 spin_unlock_irqrestore(&info->netlock, flags);
4395 void mgslpc_sppp_rx_done(MGSLPC_INFO *info, char *buf, int size)
4397 struct sk_buff *skb = dev_alloc_skb(size);
4398 if (debug_level >= DEBUG_LEVEL_INFO)
4399 printk("mgslpc_sppp_rx_done(%s)\n",info->netname);
4401 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n",
4403 info->netstats.rx_dropped++;
4407 memcpy(skb_put(skb, size),buf,size);
4409 skb->protocol = htons(ETH_P_WAN_PPP);
4410 skb->dev = info->netdev;
4411 skb->mac.raw = skb->data;
4412 info->netstats.rx_packets++;
4413 info->netstats.rx_bytes += size;
4415 info->netdev->trans_start = jiffies;
4418 void mgslpc_sppp_tx_done(MGSLPC_INFO *info)
4420 if (netif_queue_stopped(info->netdev))
4421 netif_wake_queue(info->netdev);
4424 struct net_device_stats *mgslpc_net_stats(struct net_device *dev)
4426 MGSLPC_INFO *info = dev->priv;
4427 if (debug_level >= DEBUG_LEVEL_INFO)
4428 printk("mgslpc_net_stats(%s)\n",info->netname);
4429 return &info->netstats;
4432 int mgslpc_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4434 MGSLPC_INFO *info = dev->priv;
4435 if (debug_level >= DEBUG_LEVEL_INFO)
4436 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
4437 info->netname, cmd );
4438 return sppp_do_ioctl(dev, ifr, cmd);
4441 #endif /* ifdef CONFIG_SYNCLINK_SYNCPPP */