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 */
262 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
309 #define IRQ_BREAK_ON BIT15 // rx break detected
310 #define IRQ_DATAOVERRUN BIT14 // receive data overflow
311 #define IRQ_ALLSENT BIT13 // all sent
312 #define IRQ_UNDERRUN BIT12 // transmit data underrun
313 #define IRQ_TIMER BIT11 // timer interrupt
314 #define IRQ_CTS BIT10 // CTS status change
315 #define IRQ_TXREPEAT BIT9 // tx message repeat
316 #define IRQ_TXFIFO BIT8 // transmit pool ready
317 #define IRQ_RXEOM BIT7 // receive message end
318 #define IRQ_EXITHUNT BIT6 // receive frame start
319 #define IRQ_RXTIME BIT6 // rx char timeout
320 #define IRQ_DCD BIT2 // carrier detect status change
321 #define IRQ_OVERRUN BIT1 // receive frame overflow
322 #define IRQ_RXFIFO BIT0 // receive pool full
326 #define XFW BIT6 // transmit FIFO write enable
327 #define CEC BIT2 // command executing
328 #define CTS BIT1 // CTS state
333 #define PVR_AUTOCTS BIT3
334 #define PVR_RS232 0x20 /* 0010b */
335 #define PVR_V35 0xe0 /* 1110b */
336 #define PVR_RS422 0x40 /* 0100b */
338 /* Register access functions */
340 #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
341 #define read_reg(info, reg) inb((info)->io_base + (reg))
343 #define read_reg16(info, reg) inw((info)->io_base + (reg))
344 #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
346 #define set_reg_bits(info, reg, mask) \
347 write_reg(info, (reg), \
348 (unsigned char) (read_reg(info, (reg)) | (mask)))
349 #define clear_reg_bits(info, reg, mask) \
350 write_reg(info, (reg), \
351 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
353 * interrupt enable/disable routines
355 static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
357 if (channel == CHA) {
358 info->imra_value |= mask;
359 write_reg16(info, CHA + IMR, info->imra_value);
361 info->imrb_value |= mask;
362 write_reg16(info, CHB + IMR, info->imrb_value);
365 static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
367 if (channel == CHA) {
368 info->imra_value &= ~mask;
369 write_reg16(info, CHA + IMR, info->imra_value);
371 info->imrb_value &= ~mask;
372 write_reg16(info, CHB + IMR, info->imrb_value);
376 #define port_irq_disable(info, mask) \
377 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
379 #define port_irq_enable(info, mask) \
380 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
382 static void rx_start(MGSLPC_INFO *info);
383 static void rx_stop(MGSLPC_INFO *info);
385 static void tx_start(MGSLPC_INFO *info);
386 static void tx_stop(MGSLPC_INFO *info);
387 static void tx_set_idle(MGSLPC_INFO *info);
389 static void get_signals(MGSLPC_INFO *info);
390 static void set_signals(MGSLPC_INFO *info);
392 static void reset_device(MGSLPC_INFO *info);
394 static void hdlc_mode(MGSLPC_INFO *info);
395 static void async_mode(MGSLPC_INFO *info);
397 static void tx_timeout(unsigned long context);
399 static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg);
401 #ifdef CONFIG_SYNCLINK_SYNCPPP
402 /* SPPP/HDLC stuff */
403 static void mgslpc_sppp_init(MGSLPC_INFO *info);
404 static void mgslpc_sppp_delete(MGSLPC_INFO *info);
405 static int mgslpc_sppp_open(struct net_device *d);
406 static int mgslpc_sppp_close(struct net_device *d);
407 static void mgslpc_sppp_tx_timeout(struct net_device *d);
408 static int mgslpc_sppp_tx(struct sk_buff *skb, struct net_device *d);
409 static void mgslpc_sppp_rx_done(MGSLPC_INFO *info, char *buf, int size);
410 static void mgslpc_sppp_tx_done(MGSLPC_INFO *info);
411 static int mgslpc_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
412 struct net_device_stats *mgslpc_net_stats(struct net_device *dev);
415 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
417 static BOOLEAN register_test(MGSLPC_INFO *info);
418 static BOOLEAN irq_test(MGSLPC_INFO *info);
419 static int adapter_test(MGSLPC_INFO *info);
421 static int claim_resources(MGSLPC_INFO *info);
422 static void release_resources(MGSLPC_INFO *info);
423 static void mgslpc_add_device(MGSLPC_INFO *info);
424 static void mgslpc_remove_device(MGSLPC_INFO *info);
426 static int rx_get_frame(MGSLPC_INFO *info);
427 static void rx_reset_buffers(MGSLPC_INFO *info);
428 static int rx_alloc_buffers(MGSLPC_INFO *info);
429 static void rx_free_buffers(MGSLPC_INFO *info);
431 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs);
434 * Bottom half interrupt handlers
436 static void bh_handler(void* Context);
437 static void bh_transmit(MGSLPC_INFO *info);
438 static void bh_status(MGSLPC_INFO *info);
443 static int tiocmget(struct tty_struct *tty, struct file *file);
444 static int tiocmset(struct tty_struct *tty, struct file *file,
445 unsigned int set, unsigned int clear);
446 static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
447 static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
448 static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params);
449 static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
450 static int set_txidle(MGSLPC_INFO *info, int idle_mode);
451 static int set_txenable(MGSLPC_INFO *info, int enable);
452 static int tx_abort(MGSLPC_INFO *info);
453 static int set_rxenable(MGSLPC_INFO *info, int enable);
454 static int wait_events(MGSLPC_INFO *info, int __user *mask);
456 #define jiffies_from_ms(a) ((((a) * HZ)/1000)+1)
458 static MGSLPC_INFO *mgslpc_device_list = NULL;
459 static int mgslpc_device_count = 0;
462 * Set this param to non-zero to load eax with the
463 * .text section address and breakpoint on module load.
464 * This is useful for use with gdb and add-symbol-file command.
466 static int break_on_load=0;
469 * Driver major number, defaults to zero to get auto
470 * assigned major number. May be forced as module parameter.
472 static int ttymajor=0;
474 static int debug_level = 0;
475 static int maxframe[MAX_DEVICE_COUNT] = {0,};
476 static int dosyncppp[MAX_DEVICE_COUNT] = {1,1,1,1};
478 /* The old way: bit map of interrupts to choose from */
479 /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, and 3 */
480 static u_int irq_mask = 0xdeb8;
482 /* Newer, simpler way of listing specific interrupts */
483 static int irq_list[4] = { -1 };
485 MODULE_PARM(irq_mask, "i");
486 MODULE_PARM(irq_list, "1-4i");
488 MODULE_PARM(break_on_load,"i");
489 MODULE_PARM(ttymajor,"i");
490 MODULE_PARM(debug_level,"i");
491 MODULE_PARM(maxframe,"1-" __MODULE_STRING(MAX_DEVICE_COUNT) "i");
492 MODULE_PARM(dosyncppp,"1-" __MODULE_STRING(MAX_DEVICE_COUNT) "i");
494 MODULE_LICENSE("GPL");
496 static char *driver_name = "SyncLink PC Card driver";
497 static char *driver_version = "$Revision: 4.22 $";
499 static struct tty_driver *serial_driver;
501 /* number of characters left in xmit buffer before we ask for more */
502 #define WAKEUP_CHARS 256
504 static void mgslpc_change_params(MGSLPC_INFO *info);
505 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
508 #define MIN(a,b) ((a) < (b) ? (a) : (b))
511 /* PCMCIA prototypes */
513 static void mgslpc_config(dev_link_t *link);
514 static void mgslpc_release(u_long arg);
515 static int mgslpc_event(event_t event, int priority,
516 event_callback_args_t *args);
517 static dev_link_t *mgslpc_attach(void);
518 static void mgslpc_detach(dev_link_t *);
520 static dev_info_t dev_info = "synclink_cs";
521 static dev_link_t *dev_list = NULL;
524 * 1st function defined in .text section. Calling this function in
525 * init_module() followed by a breakpoint allows a remote debugger
526 * (gdb) to get the .text address for the add-symbol-file command.
527 * This allows remote debugging of dynamically loadable modules.
529 static void* mgslpc_get_text_ptr(void)
531 return mgslpc_get_text_ptr;
534 static dev_link_t *mgslpc_attach(void)
538 client_reg_t client_reg;
541 if (debug_level >= DEBUG_LEVEL_INFO)
542 printk("mgslpc_attach\n");
544 info = (MGSLPC_INFO *)kmalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
546 printk("Error can't allocate device instance data\n");
550 memset(info, 0, sizeof(MGSLPC_INFO));
551 info->magic = MGSLPC_MAGIC;
552 INIT_WORK(&info->task, bh_handler, info);
553 info->max_frame_size = 4096;
554 info->close_delay = 5*HZ/10;
555 info->closing_wait = 30*HZ;
556 init_waitqueue_head(&info->open_wait);
557 init_waitqueue_head(&info->close_wait);
558 init_waitqueue_head(&info->status_event_wait_q);
559 init_waitqueue_head(&info->event_wait_q);
560 spin_lock_init(&info->lock);
561 spin_lock_init(&info->netlock);
562 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
563 info->idle_mode = HDLC_TXIDLE_FLAGS;
564 info->imra_value = 0xffff;
565 info->imrb_value = 0xffff;
566 info->pim_value = 0xff;
571 /* Initialize the dev_link_t structure */
573 /* Interrupt setup */
574 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
575 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
576 if (irq_list[0] == -1)
577 link->irq.IRQInfo2 = irq_mask;
579 for (i = 0; i < 4; i++)
580 link->irq.IRQInfo2 |= 1 << irq_list[i];
581 link->irq.Handler = NULL;
583 link->conf.Attributes = 0;
585 link->conf.IntType = INT_MEMORY_AND_IO;
587 /* Register with Card Services */
588 link->next = dev_list;
591 client_reg.dev_info = &dev_info;
592 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
593 client_reg.EventMask =
594 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
595 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
596 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
597 client_reg.event_handler = &mgslpc_event;
598 client_reg.Version = 0x0210;
599 client_reg.event_callback_args.client_data = link;
601 ret = pcmcia_register_client(&link->handle, &client_reg);
602 if (ret != CS_SUCCESS) {
603 cs_error(link->handle, RegisterClient, ret);
608 mgslpc_add_device(info);
613 /* Card has been inserted.
616 #define CS_CHECK(fn, ret) \
617 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
619 static void mgslpc_config(dev_link_t *link)
621 client_handle_t handle = link->handle;
622 MGSLPC_INFO *info = link->priv;
625 int last_fn, last_ret;
628 cistpl_cftable_entry_t dflt = { 0 };
629 cistpl_cftable_entry_t *cfg;
631 if (debug_level >= DEBUG_LEVEL_INFO)
632 printk("mgslpc_config(0x%p)\n", link);
634 /* read CONFIG tuple to find its configuration registers */
635 tuple.DesiredTuple = CISTPL_CONFIG;
636 tuple.Attributes = 0;
637 tuple.TupleData = buf;
638 tuple.TupleDataMax = sizeof(buf);
639 tuple.TupleOffset = 0;
640 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
641 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
642 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
643 link->conf.ConfigBase = parse.config.base;
644 link->conf.Present = parse.config.rmask[0];
647 link->state |= DEV_CONFIG;
649 /* Look up the current Vcc */
650 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
651 link->conf.Vcc = conf.Vcc;
653 /* get CIS configuration entry */
655 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
656 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
658 cfg = &(parse.cftable_entry);
659 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
660 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
662 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
666 link->conf.ConfigIndex = cfg->index;
667 link->conf.Attributes |= CONF_ENABLE_IRQ;
669 /* IO window settings */
670 link->io.NumPorts1 = 0;
671 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
672 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
673 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
674 if (!(io->flags & CISTPL_IO_8BIT))
675 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
676 if (!(io->flags & CISTPL_IO_16BIT))
677 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
678 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
679 link->io.BasePort1 = io->win[0].base;
680 link->io.NumPorts1 = io->win[0].len;
681 CS_CHECK(RequestIO, pcmcia_request_io(link->handle, &link->io));
684 link->conf.Attributes = CONF_ENABLE_IRQ;
686 link->conf.IntType = INT_MEMORY_AND_IO;
687 link->conf.ConfigIndex = 8;
688 link->conf.Present = PRESENT_OPTION;
690 link->irq.Attributes |= IRQ_HANDLE_PRESENT;
691 link->irq.Handler = mgslpc_isr;
692 link->irq.Instance = info;
693 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
695 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
697 info->io_base = link->io.BasePort1;
698 info->irq_level = link->irq.AssignedIRQ;
700 /* add to linked list of devices */
701 sprintf(info->node.dev_name, "mgslpc0");
702 info->node.major = info->node.minor = 0;
703 link->dev = &info->node;
705 printk(KERN_INFO "%s: index 0x%02x:",
706 info->node.dev_name, link->conf.ConfigIndex);
707 if (link->conf.Attributes & CONF_ENABLE_IRQ)
708 printk(", irq %d", link->irq.AssignedIRQ);
709 if (link->io.NumPorts1)
710 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
711 link->io.BasePort1+link->io.NumPorts1-1);
714 link->state &= ~DEV_CONFIG_PENDING;
718 cs_error(link->handle, last_fn, last_ret);
719 mgslpc_release((u_long)link);
722 /* Card has been removed.
723 * Unregister device and release PCMCIA configuration.
724 * If device is open, postpone until it is closed.
726 static void mgslpc_release(u_long arg)
728 dev_link_t *link = (dev_link_t *)arg;
730 if (debug_level >= DEBUG_LEVEL_INFO)
731 printk("mgslpc_release(0x%p)\n", link);
733 /* Unlink the device chain */
735 link->state &= ~DEV_CONFIG;
737 pcmcia_release_configuration(link->handle);
738 if (link->io.NumPorts1)
739 pcmcia_release_io(link->handle, &link->io);
740 if (link->irq.AssignedIRQ)
741 pcmcia_release_irq(link->handle, &link->irq);
742 if (link->state & DEV_STALE_LINK)
746 static void mgslpc_detach(dev_link_t *link)
750 if (debug_level >= DEBUG_LEVEL_INFO)
751 printk("mgslpc_detach(0x%p)\n", link);
754 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
755 if (*linkp == link) break;
759 if (link->state & DEV_CONFIG) {
760 /* device is configured/active, mark it so when
761 * release() is called a proper detach() occurs.
763 if (debug_level >= DEBUG_LEVEL_INFO)
764 printk(KERN_DEBUG "synclinkpc: detach postponed, '%s' "
765 "still locked\n", link->dev->dev_name);
766 link->state |= DEV_STALE_LINK;
770 /* Break the link with Card Services */
772 pcmcia_deregister_client(link->handle);
774 /* Unlink device structure, and free it */
776 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
779 static int mgslpc_event(event_t event, int priority,
780 event_callback_args_t *args)
782 dev_link_t *link = args->client_data;
783 MGSLPC_INFO *info = link->priv;
785 if (debug_level >= DEBUG_LEVEL_INFO)
786 printk("mgslpc_event(0x%06x)\n", event);
789 case CS_EVENT_CARD_REMOVAL:
790 link->state &= ~DEV_PRESENT;
791 if (link->state & DEV_CONFIG) {
792 ((MGSLPC_INFO *)link->priv)->stop = 1;
793 mgslpc_release((u_long)link);
796 case CS_EVENT_CARD_INSERTION:
797 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
800 case CS_EVENT_PM_SUSPEND:
801 link->state |= DEV_SUSPEND;
802 /* Fall through... */
803 case CS_EVENT_RESET_PHYSICAL:
804 /* Mark the device as stopped, to block IO until later */
806 if (link->state & DEV_CONFIG)
807 pcmcia_release_configuration(link->handle);
809 case CS_EVENT_PM_RESUME:
810 link->state &= ~DEV_SUSPEND;
811 /* Fall through... */
812 case CS_EVENT_CARD_RESET:
813 if (link->state & DEV_CONFIG)
814 pcmcia_request_configuration(link->handle, &link->conf);
821 static inline int mgslpc_paranoia_check(MGSLPC_INFO *info,
822 char *name, const char *routine)
824 #ifdef MGSLPC_PARANOIA_CHECK
825 static const char *badmagic =
826 "Warning: bad magic number for mgsl struct (%s) in %s\n";
827 static const char *badinfo =
828 "Warning: null mgslpc_info for (%s) in %s\n";
831 printk(badinfo, name, routine);
834 if (info->magic != MGSLPC_MAGIC) {
835 printk(badmagic, name, routine);
846 #define CMD_RXFIFO BIT7 // release current rx FIFO
847 #define CMD_RXRESET BIT6 // receiver reset
848 #define CMD_RXFIFO_READ BIT5
849 #define CMD_START_TIMER BIT4
850 #define CMD_TXFIFO BIT3 // release current tx FIFO
851 #define CMD_TXEOM BIT1 // transmit end message
852 #define CMD_TXRESET BIT0 // transmit reset
854 static BOOLEAN wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
857 /* wait for command completion */
858 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
866 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
868 wait_command_complete(info, channel);
869 write_reg(info, (unsigned char) (channel + CMDR), cmd);
872 static void tx_pause(struct tty_struct *tty)
874 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
877 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
879 if (debug_level >= DEBUG_LEVEL_INFO)
880 printk("tx_pause(%s)\n",info->device_name);
882 spin_lock_irqsave(&info->lock,flags);
883 if (info->tx_enabled)
885 spin_unlock_irqrestore(&info->lock,flags);
888 static void tx_release(struct tty_struct *tty)
890 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
893 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
895 if (debug_level >= DEBUG_LEVEL_INFO)
896 printk("tx_release(%s)\n",info->device_name);
898 spin_lock_irqsave(&info->lock,flags);
899 if (!info->tx_enabled)
901 spin_unlock_irqrestore(&info->lock,flags);
904 /* Return next bottom half action to perform.
905 * or 0 if nothing to do.
907 int bh_action(MGSLPC_INFO *info)
912 spin_lock_irqsave(&info->lock,flags);
914 if (info->pending_bh & BH_RECEIVE) {
915 info->pending_bh &= ~BH_RECEIVE;
917 } else if (info->pending_bh & BH_TRANSMIT) {
918 info->pending_bh &= ~BH_TRANSMIT;
920 } else if (info->pending_bh & BH_STATUS) {
921 info->pending_bh &= ~BH_STATUS;
926 /* Mark BH routine as complete */
927 info->bh_running = 0;
928 info->bh_requested = 0;
931 spin_unlock_irqrestore(&info->lock,flags);
936 void bh_handler(void* Context)
938 MGSLPC_INFO *info = (MGSLPC_INFO*)Context;
944 if (debug_level >= DEBUG_LEVEL_BH)
945 printk( "%s(%d):bh_handler(%s) entry\n",
946 __FILE__,__LINE__,info->device_name);
948 info->bh_running = 1;
950 while((action = bh_action(info)) != 0) {
952 /* Process work item */
953 if ( debug_level >= DEBUG_LEVEL_BH )
954 printk( "%s(%d):bh_handler() work item action=%d\n",
955 __FILE__,__LINE__,action);
960 while(rx_get_frame(info));
969 /* unknown work item ID */
970 printk("Unknown work item ID=%08X!\n", action);
975 if (debug_level >= DEBUG_LEVEL_BH)
976 printk( "%s(%d):bh_handler(%s) exit\n",
977 __FILE__,__LINE__,info->device_name);
980 void bh_transmit(MGSLPC_INFO *info)
982 struct tty_struct *tty = info->tty;
983 if (debug_level >= DEBUG_LEVEL_BH)
984 printk("bh_transmit() entry on %s\n", info->device_name);
987 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
988 tty->ldisc.write_wakeup) {
989 if ( debug_level >= DEBUG_LEVEL_BH )
990 printk( "%s(%d):calling ldisc.write_wakeup on %s\n",
991 __FILE__,__LINE__,info->device_name);
992 (tty->ldisc.write_wakeup)(tty);
994 wake_up_interruptible(&tty->write_wait);
998 void bh_status(MGSLPC_INFO *info)
1000 info->ri_chkcount = 0;
1001 info->dsr_chkcount = 0;
1002 info->dcd_chkcount = 0;
1003 info->cts_chkcount = 0;
1006 /* eom: non-zero = end of frame */
1007 void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
1009 unsigned char data[2];
1010 unsigned char fifo_count, read_count, i;
1011 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
1013 if (debug_level >= DEBUG_LEVEL_ISR)
1014 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
1016 if (!info->rx_enabled)
1019 if (info->rx_frame_count >= info->rx_buf_count) {
1020 /* no more free buffers */
1021 issue_command(info, CHA, CMD_RXRESET);
1022 info->pending_bh |= BH_RECEIVE;
1023 info->rx_overflow = 1;
1024 info->icount.buf_overrun++;
1029 /* end of frame, get FIFO count from RBCL register */
1030 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
1036 if (fifo_count == 1) {
1038 data[0] = read_reg(info, CHA + RXFIFO);
1041 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
1043 fifo_count -= read_count;
1044 if (!fifo_count && eom)
1045 buf->status = data[--read_count];
1047 for (i = 0; i < read_count; i++) {
1048 if (buf->count >= info->max_frame_size) {
1049 /* frame too large, reset receiver and reset current buffer */
1050 issue_command(info, CHA, CMD_RXRESET);
1054 *(buf->data + buf->count) = data[i];
1057 } while (fifo_count);
1060 info->pending_bh |= BH_RECEIVE;
1061 info->rx_frame_count++;
1063 if (info->rx_put >= info->rx_buf_count)
1066 issue_command(info, CHA, CMD_RXFIFO);
1069 void rx_ready_async(MGSLPC_INFO *info, int tcd)
1071 unsigned char data, status;
1073 struct tty_struct *tty = info->tty;
1074 struct mgsl_icount *icount = &info->icount;
1077 /* early termination, get FIFO count from RBCL register */
1078 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
1080 /* Zero fifo count could mean 0 or 32 bytes available.
1081 * If BIT5 of STAR is set then at least 1 byte is available.
1083 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
1088 /* Flush received async data to receive data buffer. */
1089 while (fifo_count) {
1090 data = read_reg(info, CHA + RXFIFO);
1091 status = read_reg(info, CHA + RXFIFO);
1094 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1097 *tty->flip.char_buf_ptr = data;
1100 *tty->flip.flag_buf_ptr = 0;
1102 // if no frameing/crc error then save data
1103 // BIT7:parity error
1104 // BIT6:framing error
1106 if (status & (BIT7 + BIT6)) {
1112 /* discard char if tty control flags say so */
1113 if (status & info->ignore_status_mask)
1116 status &= info->read_status_mask;
1119 *tty->flip.flag_buf_ptr = TTY_PARITY;
1120 else if (status & BIT6)
1121 *tty->flip.flag_buf_ptr = TTY_FRAME;
1124 tty->flip.flag_buf_ptr++;
1125 tty->flip.char_buf_ptr++;
1128 issue_command(info, CHA, CMD_RXFIFO);
1130 if (debug_level >= DEBUG_LEVEL_ISR) {
1131 printk("%s(%d):rx_ready_async count=%d\n",
1132 __FILE__,__LINE__,tty->flip.count);
1133 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1134 __FILE__,__LINE__,icount->rx,icount->brk,
1135 icount->parity,icount->frame,icount->overrun);
1138 if (tty->flip.count)
1139 tty_flip_buffer_push(tty);
1143 void tx_done(MGSLPC_INFO *info)
1145 if (!info->tx_active)
1148 info->tx_active = 0;
1149 info->tx_aborting = 0;
1151 if (info->params.mode == MGSL_MODE_ASYNC)
1154 info->tx_count = info->tx_put = info->tx_get = 0;
1155 del_timer(&info->tx_timer);
1157 if (info->drop_rts_on_tx_done) {
1159 if (info->serial_signals & SerialSignal_RTS) {
1160 info->serial_signals &= ~SerialSignal_RTS;
1163 info->drop_rts_on_tx_done = 0;
1166 #ifdef CONFIG_SYNCLINK_SYNCPPP
1168 mgslpc_sppp_tx_done(info);
1172 if (info->tty->stopped || info->tty->hw_stopped) {
1176 info->pending_bh |= BH_TRANSMIT;
1180 void tx_ready(MGSLPC_INFO *info)
1182 unsigned char fifo_count = 32;
1185 if (debug_level >= DEBUG_LEVEL_ISR)
1186 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1188 if (info->params.mode == MGSL_MODE_HDLC) {
1189 if (!info->tx_active)
1192 if (info->tty->stopped || info->tty->hw_stopped) {
1196 if (!info->tx_count)
1197 info->tx_active = 0;
1200 if (!info->tx_count)
1203 while (info->tx_count && fifo_count) {
1204 c = MIN(2, MIN(fifo_count, MIN(info->tx_count, TXBUFSIZE - info->tx_get)));
1207 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1209 write_reg16(info, CHA + TXFIFO,
1210 *((unsigned short*)(info->tx_buf + info->tx_get)));
1212 info->tx_count -= c;
1213 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1217 if (info->params.mode == MGSL_MODE_ASYNC) {
1218 if (info->tx_count < WAKEUP_CHARS)
1219 info->pending_bh |= BH_TRANSMIT;
1220 issue_command(info, CHA, CMD_TXFIFO);
1223 issue_command(info, CHA, CMD_TXFIFO);
1225 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1229 void cts_change(MGSLPC_INFO *info)
1232 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1233 irq_disable(info, CHB, IRQ_CTS);
1235 if (info->serial_signals & SerialSignal_CTS)
1236 info->input_signal_events.cts_up++;
1238 info->input_signal_events.cts_down++;
1239 wake_up_interruptible(&info->status_event_wait_q);
1240 wake_up_interruptible(&info->event_wait_q);
1242 if (info->flags & ASYNC_CTS_FLOW) {
1243 if (info->tty->hw_stopped) {
1244 if (info->serial_signals & SerialSignal_CTS) {
1245 if (debug_level >= DEBUG_LEVEL_ISR)
1246 printk("CTS tx start...");
1248 info->tty->hw_stopped = 0;
1250 info->pending_bh |= BH_TRANSMIT;
1254 if (!(info->serial_signals & SerialSignal_CTS)) {
1255 if (debug_level >= DEBUG_LEVEL_ISR)
1256 printk("CTS tx stop...");
1258 info->tty->hw_stopped = 1;
1263 info->pending_bh |= BH_STATUS;
1266 void dcd_change(MGSLPC_INFO *info)
1269 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1270 irq_disable(info, CHB, IRQ_DCD);
1272 if (info->serial_signals & SerialSignal_DCD) {
1273 info->input_signal_events.dcd_up++;
1274 #ifdef CONFIG_SYNCLINK_SYNCPPP
1276 sppp_reopen(info->netdev);
1280 info->input_signal_events.dcd_down++;
1281 wake_up_interruptible(&info->status_event_wait_q);
1282 wake_up_interruptible(&info->event_wait_q);
1284 if (info->flags & ASYNC_CHECK_CD) {
1285 if (debug_level >= DEBUG_LEVEL_ISR)
1286 printk("%s CD now %s...", info->device_name,
1287 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1288 if (info->serial_signals & SerialSignal_DCD)
1289 wake_up_interruptible(&info->open_wait);
1291 if (debug_level >= DEBUG_LEVEL_ISR)
1292 printk("doing serial hangup...");
1294 tty_hangup(info->tty);
1297 info->pending_bh |= BH_STATUS;
1300 void dsr_change(MGSLPC_INFO *info)
1303 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1304 port_irq_disable(info, PVR_DSR);
1306 if (info->serial_signals & SerialSignal_DSR)
1307 info->input_signal_events.dsr_up++;
1309 info->input_signal_events.dsr_down++;
1310 wake_up_interruptible(&info->status_event_wait_q);
1311 wake_up_interruptible(&info->event_wait_q);
1312 info->pending_bh |= BH_STATUS;
1315 void ri_change(MGSLPC_INFO *info)
1318 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1319 port_irq_disable(info, PVR_RI);
1321 if (info->serial_signals & SerialSignal_RI)
1322 info->input_signal_events.ri_up++;
1324 info->input_signal_events.ri_down++;
1325 wake_up_interruptible(&info->status_event_wait_q);
1326 wake_up_interruptible(&info->event_wait_q);
1327 info->pending_bh |= BH_STATUS;
1330 /* Interrupt service routine entry point.
1334 * irq interrupt number that caused interrupt
1335 * dev_id device ID supplied during interrupt registration
1336 * regs interrupted processor context
1338 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs)
1340 MGSLPC_INFO * info = (MGSLPC_INFO *)dev_id;
1342 unsigned char gis, pis;
1345 if (debug_level >= DEBUG_LEVEL_ISR)
1346 printk("mgslpc_isr(%d) entry.\n", irq);
1350 if (!(info->link.state & DEV_CONFIG))
1353 spin_lock(&info->lock);
1355 while ((gis = read_reg(info, CHA + GIS))) {
1356 if (debug_level >= DEBUG_LEVEL_ISR)
1357 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1359 if ((gis & 0x70) || count > 1000) {
1360 printk("synclink_cs:hardware failed or ejected\n");
1365 if (gis & (BIT1 + BIT0)) {
1366 isr = read_reg16(info, CHB + ISR);
1372 if (gis & (BIT3 + BIT2))
1374 isr = read_reg16(info, CHA + ISR);
1375 if (isr & IRQ_TIMER) {
1376 info->irq_occurred = 1;
1377 irq_disable(info, CHA, IRQ_TIMER);
1381 if (isr & IRQ_EXITHUNT) {
1382 info->icount.exithunt++;
1383 wake_up_interruptible(&info->event_wait_q);
1385 if (isr & IRQ_BREAK_ON) {
1387 if (info->flags & ASYNC_SAK)
1390 if (isr & IRQ_RXTIME) {
1391 issue_command(info, CHA, CMD_RXFIFO_READ);
1393 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1394 if (info->params.mode == MGSL_MODE_HDLC)
1395 rx_ready_hdlc(info, isr & IRQ_RXEOM);
1397 rx_ready_async(info, isr & IRQ_RXEOM);
1401 if (isr & IRQ_UNDERRUN) {
1402 if (info->tx_aborting)
1403 info->icount.txabort++;
1405 info->icount.txunder++;
1408 else if (isr & IRQ_ALLSENT) {
1409 info->icount.txok++;
1412 else if (isr & IRQ_TXFIFO)
1416 pis = read_reg(info, CHA + PIS);
1424 /* Request bottom half processing if there's something
1425 * for it to do and the bh is not already running
1428 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1429 if ( debug_level >= DEBUG_LEVEL_ISR )
1430 printk("%s(%d):%s queueing bh task.\n",
1431 __FILE__,__LINE__,info->device_name);
1432 schedule_work(&info->task);
1433 info->bh_requested = 1;
1436 spin_unlock(&info->lock);
1438 if (debug_level >= DEBUG_LEVEL_ISR)
1439 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1440 __FILE__,__LINE__,irq);
1445 /* Initialize and start device.
1447 static int startup(MGSLPC_INFO * info)
1451 if (debug_level >= DEBUG_LEVEL_INFO)
1452 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1454 if (info->flags & ASYNC_INITIALIZED)
1457 if (!info->tx_buf) {
1458 /* allocate a page of memory for a transmit buffer */
1459 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1460 if (!info->tx_buf) {
1461 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1462 __FILE__,__LINE__,info->device_name);
1467 info->pending_bh = 0;
1469 init_timer(&info->tx_timer);
1470 info->tx_timer.data = (unsigned long)info;
1471 info->tx_timer.function = tx_timeout;
1473 /* Allocate and claim adapter resources */
1474 retval = claim_resources(info);
1476 /* perform existance check and diagnostics */
1478 retval = adapter_test(info);
1481 if (capable(CAP_SYS_ADMIN) && info->tty)
1482 set_bit(TTY_IO_ERROR, &info->tty->flags);
1483 release_resources(info);
1487 /* program hardware for current parameters */
1488 mgslpc_change_params(info);
1491 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1493 info->flags |= ASYNC_INITIALIZED;
1498 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1500 static void shutdown(MGSLPC_INFO * info)
1502 unsigned long flags;
1504 if (!(info->flags & ASYNC_INITIALIZED))
1507 if (debug_level >= DEBUG_LEVEL_INFO)
1508 printk("%s(%d):mgslpc_shutdown(%s)\n",
1509 __FILE__,__LINE__, info->device_name );
1511 /* clear status wait queue because status changes */
1512 /* can't happen after shutting down the hardware */
1513 wake_up_interruptible(&info->status_event_wait_q);
1514 wake_up_interruptible(&info->event_wait_q);
1516 del_timer(&info->tx_timer);
1519 free_page((unsigned long) info->tx_buf);
1520 info->tx_buf = NULL;
1523 spin_lock_irqsave(&info->lock,flags);
1528 /* TODO:disable interrupts instead of reset to preserve signal states */
1531 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1532 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1536 spin_unlock_irqrestore(&info->lock,flags);
1538 release_resources(info);
1541 set_bit(TTY_IO_ERROR, &info->tty->flags);
1543 info->flags &= ~ASYNC_INITIALIZED;
1546 static void mgslpc_program_hw(MGSLPC_INFO *info)
1548 unsigned long flags;
1550 spin_lock_irqsave(&info->lock,flags);
1554 info->tx_count = info->tx_put = info->tx_get = 0;
1556 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1563 info->dcd_chkcount = 0;
1564 info->cts_chkcount = 0;
1565 info->ri_chkcount = 0;
1566 info->dsr_chkcount = 0;
1568 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1569 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1572 if (info->netcount || info->tty->termios->c_cflag & CREAD)
1575 spin_unlock_irqrestore(&info->lock,flags);
1578 /* Reconfigure adapter based on new parameters
1580 static void mgslpc_change_params(MGSLPC_INFO *info)
1585 if (!info->tty || !info->tty->termios)
1588 if (debug_level >= DEBUG_LEVEL_INFO)
1589 printk("%s(%d):mgslpc_change_params(%s)\n",
1590 __FILE__,__LINE__, info->device_name );
1592 cflag = info->tty->termios->c_cflag;
1594 /* if B0 rate (hangup) specified then negate DTR and RTS */
1595 /* otherwise assert DTR and RTS */
1597 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1599 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1601 /* byte size and parity */
1603 switch (cflag & CSIZE) {
1604 case CS5: info->params.data_bits = 5; break;
1605 case CS6: info->params.data_bits = 6; break;
1606 case CS7: info->params.data_bits = 7; break;
1607 case CS8: info->params.data_bits = 8; break;
1608 default: info->params.data_bits = 7; break;
1612 info->params.stop_bits = 2;
1614 info->params.stop_bits = 1;
1616 info->params.parity = ASYNC_PARITY_NONE;
1617 if (cflag & PARENB) {
1619 info->params.parity = ASYNC_PARITY_ODD;
1621 info->params.parity = ASYNC_PARITY_EVEN;
1624 info->params.parity = ASYNC_PARITY_SPACE;
1628 /* calculate number of jiffies to transmit a full
1629 * FIFO (32 bytes) at specified data rate
1631 bits_per_char = info->params.data_bits +
1632 info->params.stop_bits + 1;
1634 /* if port data rate is set to 460800 or less then
1635 * allow tty settings to override, otherwise keep the
1636 * current data rate.
1638 if (info->params.data_rate <= 460800) {
1639 info->params.data_rate = tty_get_baud_rate(info->tty);
1642 if ( info->params.data_rate ) {
1643 info->timeout = (32*HZ*bits_per_char) /
1644 info->params.data_rate;
1646 info->timeout += HZ/50; /* Add .02 seconds of slop */
1648 if (cflag & CRTSCTS)
1649 info->flags |= ASYNC_CTS_FLOW;
1651 info->flags &= ~ASYNC_CTS_FLOW;
1654 info->flags &= ~ASYNC_CHECK_CD;
1656 info->flags |= ASYNC_CHECK_CD;
1658 /* process tty input control flags */
1660 info->read_status_mask = 0;
1661 if (I_INPCK(info->tty))
1662 info->read_status_mask |= BIT7 | BIT6;
1663 if (I_IGNPAR(info->tty))
1664 info->ignore_status_mask |= BIT7 | BIT6;
1666 mgslpc_program_hw(info);
1669 /* Add a character to the transmit buffer
1671 static void mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1673 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1674 unsigned long flags;
1676 if (debug_level >= DEBUG_LEVEL_INFO) {
1677 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1678 __FILE__,__LINE__,ch,info->device_name);
1681 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1684 if (!tty || !info->tx_buf)
1687 spin_lock_irqsave(&info->lock,flags);
1689 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1690 if (info->tx_count < TXBUFSIZE - 1) {
1691 info->tx_buf[info->tx_put++] = ch;
1692 info->tx_put &= TXBUFSIZE-1;
1697 spin_unlock_irqrestore(&info->lock,flags);
1700 /* Enable transmitter so remaining characters in the
1701 * transmit buffer are sent.
1703 static void mgslpc_flush_chars(struct tty_struct *tty)
1705 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1706 unsigned long flags;
1708 if (debug_level >= DEBUG_LEVEL_INFO)
1709 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1710 __FILE__,__LINE__,info->device_name,info->tx_count);
1712 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1715 if (info->tx_count <= 0 || tty->stopped ||
1716 tty->hw_stopped || !info->tx_buf)
1719 if (debug_level >= DEBUG_LEVEL_INFO)
1720 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1721 __FILE__,__LINE__,info->device_name);
1723 spin_lock_irqsave(&info->lock,flags);
1724 if (!info->tx_active)
1726 spin_unlock_irqrestore(&info->lock,flags);
1729 /* Send a block of data
1733 * tty pointer to tty information structure
1734 * from_user flag: 1 = from user process
1735 * buf pointer to buffer containing send data
1736 * count size of send data in bytes
1738 * Returns: number of characters written
1740 static int mgslpc_write(struct tty_struct * tty, int from_user,
1741 const unsigned char *buf, int count)
1743 int c, ret = 0, err;
1744 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1745 unsigned long flags;
1747 if (debug_level >= DEBUG_LEVEL_INFO)
1748 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1749 __FILE__,__LINE__,info->device_name,count);
1751 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1752 !tty || !info->tx_buf)
1755 if (info->params.mode == MGSL_MODE_HDLC) {
1756 if (count > TXBUFSIZE) {
1760 if (info->tx_active)
1762 else if (info->tx_count)
1768 MIN(TXBUFSIZE - info->tx_count - 1,
1769 TXBUFSIZE - info->tx_put));
1774 COPY_FROM_USER(err, info->tx_buf + info->tx_put, buf, c);
1781 memcpy(info->tx_buf + info->tx_put, buf, c);
1783 spin_lock_irqsave(&info->lock,flags);
1784 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1785 info->tx_count += c;
1786 spin_unlock_irqrestore(&info->lock,flags);
1793 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1794 spin_lock_irqsave(&info->lock,flags);
1795 if (!info->tx_active)
1797 spin_unlock_irqrestore(&info->lock,flags);
1800 if (debug_level >= DEBUG_LEVEL_INFO)
1801 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1802 __FILE__,__LINE__,info->device_name,ret);
1806 /* Return the count of free bytes in transmit buffer
1808 static int mgslpc_write_room(struct tty_struct *tty)
1810 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1813 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1816 if (info->params.mode == MGSL_MODE_HDLC) {
1817 /* HDLC (frame oriented) mode */
1818 if (info->tx_active)
1821 return HDLC_MAX_FRAME_SIZE;
1823 ret = TXBUFSIZE - info->tx_count - 1;
1828 if (debug_level >= DEBUG_LEVEL_INFO)
1829 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1830 __FILE__,__LINE__, info->device_name, ret);
1834 /* Return the count of bytes in transmit buffer
1836 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1838 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1841 if (debug_level >= DEBUG_LEVEL_INFO)
1842 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1843 __FILE__,__LINE__, info->device_name );
1845 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1848 if (info->params.mode == MGSL_MODE_HDLC)
1849 rc = info->tx_active ? info->max_frame_size : 0;
1851 rc = info->tx_count;
1853 if (debug_level >= DEBUG_LEVEL_INFO)
1854 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1855 __FILE__,__LINE__, info->device_name, rc);
1860 /* Discard all data in the send buffer
1862 static void mgslpc_flush_buffer(struct tty_struct *tty)
1864 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1865 unsigned long flags;
1867 if (debug_level >= DEBUG_LEVEL_INFO)
1868 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1869 __FILE__,__LINE__, info->device_name );
1871 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1874 spin_lock_irqsave(&info->lock,flags);
1875 info->tx_count = info->tx_put = info->tx_get = 0;
1876 del_timer(&info->tx_timer);
1877 spin_unlock_irqrestore(&info->lock,flags);
1879 wake_up_interruptible(&tty->write_wait);
1880 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1881 tty->ldisc.write_wakeup)
1882 (tty->ldisc.write_wakeup)(tty);
1885 /* Send a high-priority XON/XOFF character
1887 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1889 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1890 unsigned long flags;
1892 if (debug_level >= DEBUG_LEVEL_INFO)
1893 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1894 __FILE__,__LINE__, info->device_name, ch );
1896 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1901 spin_lock_irqsave(&info->lock,flags);
1902 if (!info->tx_enabled)
1904 spin_unlock_irqrestore(&info->lock,flags);
1908 /* Signal remote device to throttle send data (our receive data)
1910 static void mgslpc_throttle(struct tty_struct * tty)
1912 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1913 unsigned long flags;
1915 if (debug_level >= DEBUG_LEVEL_INFO)
1916 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1917 __FILE__,__LINE__, info->device_name );
1919 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1923 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1925 if (tty->termios->c_cflag & CRTSCTS) {
1926 spin_lock_irqsave(&info->lock,flags);
1927 info->serial_signals &= ~SerialSignal_RTS;
1929 spin_unlock_irqrestore(&info->lock,flags);
1933 /* Signal remote device to stop throttling send data (our receive data)
1935 static void mgslpc_unthrottle(struct tty_struct * tty)
1937 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1938 unsigned long flags;
1940 if (debug_level >= DEBUG_LEVEL_INFO)
1941 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1942 __FILE__,__LINE__, info->device_name );
1944 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1951 mgslpc_send_xchar(tty, START_CHAR(tty));
1954 if (tty->termios->c_cflag & CRTSCTS) {
1955 spin_lock_irqsave(&info->lock,flags);
1956 info->serial_signals |= SerialSignal_RTS;
1958 spin_unlock_irqrestore(&info->lock,flags);
1962 /* get the current serial statistics
1964 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1967 if (debug_level >= DEBUG_LEVEL_INFO)
1968 printk("get_params(%s)\n", info->device_name);
1969 COPY_TO_USER(err,user_icount, &info->icount, sizeof(struct mgsl_icount));
1975 /* get the current serial parameters
1977 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1980 if (debug_level >= DEBUG_LEVEL_INFO)
1981 printk("get_params(%s)\n", info->device_name);
1982 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1988 /* set the serial parameters
1992 * info pointer to device instance data
1993 * new_params user buffer containing new serial params
1995 * Returns: 0 if success, otherwise error code
1997 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params)
1999 unsigned long flags;
2000 MGSL_PARAMS tmp_params;
2003 if (debug_level >= DEBUG_LEVEL_INFO)
2004 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
2005 info->device_name );
2006 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2008 if ( debug_level >= DEBUG_LEVEL_INFO )
2009 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
2010 __FILE__,__LINE__,info->device_name);
2014 spin_lock_irqsave(&info->lock,flags);
2015 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2016 spin_unlock_irqrestore(&info->lock,flags);
2018 mgslpc_change_params(info);
2023 static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
2026 if (debug_level >= DEBUG_LEVEL_INFO)
2027 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
2028 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2034 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
2036 unsigned long flags;
2037 if (debug_level >= DEBUG_LEVEL_INFO)
2038 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
2039 spin_lock_irqsave(&info->lock,flags);
2040 info->idle_mode = idle_mode;
2042 spin_unlock_irqrestore(&info->lock,flags);
2046 static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
2049 if (debug_level >= DEBUG_LEVEL_INFO)
2050 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
2051 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
2057 static int set_interface(MGSLPC_INFO * info, int if_mode)
2059 unsigned long flags;
2061 if (debug_level >= DEBUG_LEVEL_INFO)
2062 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
2063 spin_lock_irqsave(&info->lock,flags);
2064 info->if_mode = if_mode;
2066 val = read_reg(info, PVR) & 0x0f;
2067 switch (info->if_mode)
2069 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
2070 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
2071 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
2073 write_reg(info, PVR, val);
2075 spin_unlock_irqrestore(&info->lock,flags);
2079 static int set_txenable(MGSLPC_INFO * info, int enable)
2081 unsigned long flags;
2083 if (debug_level >= DEBUG_LEVEL_INFO)
2084 printk("set_txenable(%s,%d)\n", info->device_name, enable);
2086 spin_lock_irqsave(&info->lock,flags);
2088 if (!info->tx_enabled)
2091 if (info->tx_enabled)
2094 spin_unlock_irqrestore(&info->lock,flags);
2098 static int tx_abort(MGSLPC_INFO * info)
2100 unsigned long flags;
2102 if (debug_level >= DEBUG_LEVEL_INFO)
2103 printk("tx_abort(%s)\n", info->device_name);
2105 spin_lock_irqsave(&info->lock,flags);
2106 if (info->tx_active && info->tx_count &&
2107 info->params.mode == MGSL_MODE_HDLC) {
2108 /* clear data count so FIFO is not filled on next IRQ.
2109 * This results in underrun and abort transmission.
2111 info->tx_count = info->tx_put = info->tx_get = 0;
2112 info->tx_aborting = TRUE;
2114 spin_unlock_irqrestore(&info->lock,flags);
2118 static int set_rxenable(MGSLPC_INFO * info, int enable)
2120 unsigned long flags;
2122 if (debug_level >= DEBUG_LEVEL_INFO)
2123 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
2125 spin_lock_irqsave(&info->lock,flags);
2127 if (!info->rx_enabled)
2130 if (info->rx_enabled)
2133 spin_unlock_irqrestore(&info->lock,flags);
2137 /* wait for specified event to occur
2139 * Arguments: info pointer to device instance data
2140 * mask pointer to bitmask of events to wait for
2141 * Return Value: 0 if successful and bit mask updated with
2142 * of events triggerred,
2143 * otherwise error code
2145 static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
2147 unsigned long flags;
2150 struct mgsl_icount cprev, cnow;
2153 struct _input_signal_events oldsigs, newsigs;
2154 DECLARE_WAITQUEUE(wait, current);
2156 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2160 if (debug_level >= DEBUG_LEVEL_INFO)
2161 printk("wait_events(%s,%d)\n", info->device_name, mask);
2163 spin_lock_irqsave(&info->lock,flags);
2165 /* return immediately if state matches requested events */
2167 s = info->serial_signals;
2169 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2170 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2171 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2172 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2174 spin_unlock_irqrestore(&info->lock,flags);
2178 /* save current irq counts */
2179 cprev = info->icount;
2180 oldsigs = info->input_signal_events;
2182 if ((info->params.mode == MGSL_MODE_HDLC) &&
2183 (mask & MgslEvent_ExitHuntMode))
2184 irq_enable(info, CHA, IRQ_EXITHUNT);
2186 set_current_state(TASK_INTERRUPTIBLE);
2187 add_wait_queue(&info->event_wait_q, &wait);
2189 spin_unlock_irqrestore(&info->lock,flags);
2194 if (signal_pending(current)) {
2199 /* get current irq counts */
2200 spin_lock_irqsave(&info->lock,flags);
2201 cnow = info->icount;
2202 newsigs = info->input_signal_events;
2203 set_current_state(TASK_INTERRUPTIBLE);
2204 spin_unlock_irqrestore(&info->lock,flags);
2206 /* if no change, wait aborted for some reason */
2207 if (newsigs.dsr_up == oldsigs.dsr_up &&
2208 newsigs.dsr_down == oldsigs.dsr_down &&
2209 newsigs.dcd_up == oldsigs.dcd_up &&
2210 newsigs.dcd_down == oldsigs.dcd_down &&
2211 newsigs.cts_up == oldsigs.cts_up &&
2212 newsigs.cts_down == oldsigs.cts_down &&
2213 newsigs.ri_up == oldsigs.ri_up &&
2214 newsigs.ri_down == oldsigs.ri_down &&
2215 cnow.exithunt == cprev.exithunt &&
2216 cnow.rxidle == cprev.rxidle) {
2222 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2223 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2224 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2225 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2226 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2227 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2228 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2229 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2230 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2231 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2239 remove_wait_queue(&info->event_wait_q, &wait);
2240 set_current_state(TASK_RUNNING);
2242 if (mask & MgslEvent_ExitHuntMode) {
2243 spin_lock_irqsave(&info->lock,flags);
2244 if (!waitqueue_active(&info->event_wait_q))
2245 irq_disable(info, CHA, IRQ_EXITHUNT);
2246 spin_unlock_irqrestore(&info->lock,flags);
2250 PUT_USER(rc, events, mask_ptr);
2254 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2256 unsigned long flags;
2258 struct mgsl_icount cprev, cnow;
2259 DECLARE_WAITQUEUE(wait, current);
2261 /* save current irq counts */
2262 spin_lock_irqsave(&info->lock,flags);
2263 cprev = info->icount;
2264 add_wait_queue(&info->status_event_wait_q, &wait);
2265 set_current_state(TASK_INTERRUPTIBLE);
2266 spin_unlock_irqrestore(&info->lock,flags);
2270 if (signal_pending(current)) {
2275 /* get new irq counts */
2276 spin_lock_irqsave(&info->lock,flags);
2277 cnow = info->icount;
2278 set_current_state(TASK_INTERRUPTIBLE);
2279 spin_unlock_irqrestore(&info->lock,flags);
2281 /* if no change, wait aborted for some reason */
2282 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2283 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2288 /* check for change in caller specified modem input */
2289 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2290 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2291 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2292 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2299 remove_wait_queue(&info->status_event_wait_q, &wait);
2300 set_current_state(TASK_RUNNING);
2304 /* return the state of the serial control and status signals
2306 static int tiocmget(struct tty_struct *tty, struct file *file)
2308 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2309 unsigned int result;
2310 unsigned long flags;
2312 spin_lock_irqsave(&info->lock,flags);
2314 spin_unlock_irqrestore(&info->lock,flags);
2316 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2317 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2318 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2319 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2320 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2321 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2323 if (debug_level >= DEBUG_LEVEL_INFO)
2324 printk("%s(%d):%s tiocmget() value=%08X\n",
2325 __FILE__,__LINE__, info->device_name, result );
2329 /* set modem control signals (DTR/RTS)
2331 static int tiocmset(struct tty_struct *tty, struct file *file,
2332 unsigned int set, unsigned int clear)
2334 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2335 unsigned long flags;
2337 if (debug_level >= DEBUG_LEVEL_INFO)
2338 printk("%s(%d):%s tiocmset(%x,%x)\n",
2339 __FILE__,__LINE__,info->device_name, set, clear);
2341 if (set & TIOCM_RTS)
2342 info->serial_signals |= SerialSignal_RTS;
2343 if (set & TIOCM_DTR)
2344 info->serial_signals |= SerialSignal_DTR;
2345 if (clear & TIOCM_RTS)
2346 info->serial_signals &= ~SerialSignal_RTS;
2347 if (clear & TIOCM_DTR)
2348 info->serial_signals &= ~SerialSignal_DTR;
2350 spin_lock_irqsave(&info->lock,flags);
2352 spin_unlock_irqrestore(&info->lock,flags);
2357 /* Set or clear transmit break condition
2359 * Arguments: tty pointer to tty instance data
2360 * break_state -1=set break condition, 0=clear
2362 static void mgslpc_break(struct tty_struct *tty, int break_state)
2364 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2365 unsigned long flags;
2367 if (debug_level >= DEBUG_LEVEL_INFO)
2368 printk("%s(%d):mgslpc_break(%s,%d)\n",
2369 __FILE__,__LINE__, info->device_name, break_state);
2371 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2374 spin_lock_irqsave(&info->lock,flags);
2375 if (break_state == -1)
2376 set_reg_bits(info, CHA+DAFO, BIT6);
2378 clear_reg_bits(info, CHA+DAFO, BIT6);
2379 spin_unlock_irqrestore(&info->lock,flags);
2382 /* Service an IOCTL request
2386 * tty pointer to tty instance data
2387 * file pointer to associated file object for device
2388 * cmd IOCTL command code
2389 * arg command argument/context
2391 * Return Value: 0 if success, otherwise error code
2393 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2394 unsigned int cmd, unsigned long arg)
2396 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2398 if (debug_level >= DEBUG_LEVEL_INFO)
2399 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2400 info->device_name, cmd );
2402 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2405 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2406 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2407 if (tty->flags & (1 << TTY_IO_ERROR))
2411 return ioctl_common(info, cmd, arg);
2414 int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
2417 struct mgsl_icount cnow; /* kernel counter temps */
2418 struct serial_icounter_struct __user *p_cuser; /* user space */
2419 void __user *argp = (void __user *)arg;
2420 unsigned long flags;
2423 case MGSL_IOCGPARAMS:
2424 return get_params(info, argp);
2425 case MGSL_IOCSPARAMS:
2426 return set_params(info, argp);
2427 case MGSL_IOCGTXIDLE:
2428 return get_txidle(info, argp);
2429 case MGSL_IOCSTXIDLE:
2430 return set_txidle(info, (int)arg);
2432 return get_interface(info, argp);
2434 return set_interface(info,(int)arg);
2435 case MGSL_IOCTXENABLE:
2436 return set_txenable(info,(int)arg);
2437 case MGSL_IOCRXENABLE:
2438 return set_rxenable(info,(int)arg);
2439 case MGSL_IOCTXABORT:
2440 return tx_abort(info);
2441 case MGSL_IOCGSTATS:
2442 return get_stats(info, argp);
2443 case MGSL_IOCWAITEVENT:
2444 return wait_events(info, argp);
2446 return modem_input_wait(info,(int)arg);
2448 spin_lock_irqsave(&info->lock,flags);
2449 cnow = info->icount;
2450 spin_unlock_irqrestore(&info->lock,flags);
2452 PUT_USER(error,cnow.cts, &p_cuser->cts);
2453 if (error) return error;
2454 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2455 if (error) return error;
2456 PUT_USER(error,cnow.rng, &p_cuser->rng);
2457 if (error) return error;
2458 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2459 if (error) return error;
2460 PUT_USER(error,cnow.rx, &p_cuser->rx);
2461 if (error) return error;
2462 PUT_USER(error,cnow.tx, &p_cuser->tx);
2463 if (error) return error;
2464 PUT_USER(error,cnow.frame, &p_cuser->frame);
2465 if (error) return error;
2466 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2467 if (error) return error;
2468 PUT_USER(error,cnow.parity, &p_cuser->parity);
2469 if (error) return error;
2470 PUT_USER(error,cnow.brk, &p_cuser->brk);
2471 if (error) return error;
2472 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2473 if (error) return error;
2476 return -ENOIOCTLCMD;
2481 /* Set new termios settings
2485 * tty pointer to tty structure
2486 * termios pointer to buffer to hold returned old termios
2488 static void mgslpc_set_termios(struct tty_struct *tty, struct termios *old_termios)
2490 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2491 unsigned long flags;
2493 if (debug_level >= DEBUG_LEVEL_INFO)
2494 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2495 tty->driver->name );
2497 /* just return if nothing has changed */
2498 if ((tty->termios->c_cflag == old_termios->c_cflag)
2499 && (RELEVANT_IFLAG(tty->termios->c_iflag)
2500 == RELEVANT_IFLAG(old_termios->c_iflag)))
2503 mgslpc_change_params(info);
2505 /* Handle transition to B0 status */
2506 if (old_termios->c_cflag & CBAUD &&
2507 !(tty->termios->c_cflag & CBAUD)) {
2508 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2509 spin_lock_irqsave(&info->lock,flags);
2511 spin_unlock_irqrestore(&info->lock,flags);
2514 /* Handle transition away from B0 status */
2515 if (!(old_termios->c_cflag & CBAUD) &&
2516 tty->termios->c_cflag & CBAUD) {
2517 info->serial_signals |= SerialSignal_DTR;
2518 if (!(tty->termios->c_cflag & CRTSCTS) ||
2519 !test_bit(TTY_THROTTLED, &tty->flags)) {
2520 info->serial_signals |= SerialSignal_RTS;
2522 spin_lock_irqsave(&info->lock,flags);
2524 spin_unlock_irqrestore(&info->lock,flags);
2527 /* Handle turning off CRTSCTS */
2528 if (old_termios->c_cflag & CRTSCTS &&
2529 !(tty->termios->c_cflag & CRTSCTS)) {
2530 tty->hw_stopped = 0;
2535 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2537 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2539 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2542 if (debug_level >= DEBUG_LEVEL_INFO)
2543 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2544 __FILE__,__LINE__, info->device_name, info->count);
2549 if (tty_hung_up_p(filp))
2552 if ((tty->count == 1) && (info->count != 1)) {
2554 * tty->count is 1 and the tty structure will be freed.
2555 * info->count should be one in this case.
2556 * if it's not, correct it so that the port is shutdown.
2558 printk("mgslpc_close: bad refcount; tty->count is 1, "
2559 "info->count is %d\n", info->count);
2565 /* if at least one open remaining, leave hardware active */
2569 info->flags |= ASYNC_CLOSING;
2571 /* set tty->closing to notify line discipline to
2572 * only process XON/XOFF characters. Only the N_TTY
2573 * discipline appears to use this (ppp does not).
2577 /* wait for transmit data to clear all layers */
2579 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
2580 if (debug_level >= DEBUG_LEVEL_INFO)
2581 printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sent\n",
2582 __FILE__,__LINE__, info->device_name );
2583 tty_wait_until_sent(tty, info->closing_wait);
2586 if (info->flags & ASYNC_INITIALIZED)
2587 mgslpc_wait_until_sent(tty, info->timeout);
2589 if (tty->driver->flush_buffer)
2590 tty->driver->flush_buffer(tty);
2592 if (tty->ldisc.flush_buffer)
2593 tty->ldisc.flush_buffer(tty);
2600 if (info->blocked_open) {
2601 if (info->close_delay) {
2602 set_current_state(TASK_INTERRUPTIBLE);
2603 schedule_timeout(info->close_delay);
2605 wake_up_interruptible(&info->open_wait);
2608 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2610 wake_up_interruptible(&info->close_wait);
2613 if (debug_level >= DEBUG_LEVEL_INFO)
2614 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2615 tty->driver->name, info->count);
2618 /* Wait until the transmitter is empty.
2620 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2622 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2623 unsigned long orig_jiffies, char_time;
2628 if (debug_level >= DEBUG_LEVEL_INFO)
2629 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2630 __FILE__,__LINE__, info->device_name );
2632 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2635 if (!(info->flags & ASYNC_INITIALIZED))
2638 orig_jiffies = jiffies;
2640 /* Set check interval to 1/5 of estimated time to
2641 * send a character, and make it at least 1. The check
2642 * interval should also be less than the timeout.
2643 * Note: use tight timings here to satisfy the NIST-PCTS.
2646 if ( info->params.data_rate ) {
2647 char_time = info->timeout/(32 * 5);
2654 char_time = MIN(char_time, timeout);
2656 if (info->params.mode == MGSL_MODE_HDLC) {
2657 while (info->tx_active) {
2658 set_current_state(TASK_INTERRUPTIBLE);
2659 schedule_timeout(char_time);
2660 if (signal_pending(current))
2662 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2666 while ((info->tx_count || info->tx_active) &&
2668 set_current_state(TASK_INTERRUPTIBLE);
2669 schedule_timeout(char_time);
2670 if (signal_pending(current))
2672 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2678 if (debug_level >= DEBUG_LEVEL_INFO)
2679 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2680 __FILE__,__LINE__, info->device_name );
2683 /* Called by tty_hangup() when a hangup is signaled.
2684 * This is the same as closing all open files for the port.
2686 static void mgslpc_hangup(struct tty_struct *tty)
2688 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2690 if (debug_level >= DEBUG_LEVEL_INFO)
2691 printk("%s(%d):mgslpc_hangup(%s)\n",
2692 __FILE__,__LINE__, info->device_name );
2694 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2697 mgslpc_flush_buffer(tty);
2701 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2704 wake_up_interruptible(&info->open_wait);
2707 /* Block the current process until the specified port
2708 * is ready to be opened.
2710 static int block_til_ready(struct tty_struct *tty, struct file *filp,
2713 DECLARE_WAITQUEUE(wait, current);
2715 int do_clocal = 0, extra_count = 0;
2716 unsigned long flags;
2718 if (debug_level >= DEBUG_LEVEL_INFO)
2719 printk("%s(%d):block_til_ready on %s\n",
2720 __FILE__,__LINE__, tty->driver->name );
2722 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
2723 /* nonblock mode is set or port is not enabled */
2724 /* just verify that callout device is not active */
2725 info->flags |= ASYNC_NORMAL_ACTIVE;
2729 if (tty->termios->c_cflag & CLOCAL)
2732 /* Wait for carrier detect and the line to become
2733 * free (i.e., not in use by the callout). While we are in
2734 * this loop, info->count is dropped by one, so that
2735 * mgslpc_close() knows when to free things. We restore it upon
2736 * exit, either normal or abnormal.
2740 add_wait_queue(&info->open_wait, &wait);
2742 if (debug_level >= DEBUG_LEVEL_INFO)
2743 printk("%s(%d):block_til_ready before block on %s count=%d\n",
2744 __FILE__,__LINE__, tty->driver->name, info->count );
2746 spin_lock_irqsave(&info->lock, flags);
2747 if (!tty_hung_up_p(filp)) {
2751 spin_unlock_irqrestore(&info->lock, flags);
2752 info->blocked_open++;
2755 if ((tty->termios->c_cflag & CBAUD)) {
2756 spin_lock_irqsave(&info->lock,flags);
2757 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2759 spin_unlock_irqrestore(&info->lock,flags);
2762 set_current_state(TASK_INTERRUPTIBLE);
2764 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
2765 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
2766 -EAGAIN : -ERESTARTSYS;
2770 spin_lock_irqsave(&info->lock,flags);
2772 spin_unlock_irqrestore(&info->lock,flags);
2774 if (!(info->flags & ASYNC_CLOSING) &&
2775 (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
2779 if (signal_pending(current)) {
2780 retval = -ERESTARTSYS;
2784 if (debug_level >= DEBUG_LEVEL_INFO)
2785 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
2786 __FILE__,__LINE__, tty->driver->name, info->count );
2791 set_current_state(TASK_RUNNING);
2792 remove_wait_queue(&info->open_wait, &wait);
2796 info->blocked_open--;
2798 if (debug_level >= DEBUG_LEVEL_INFO)
2799 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
2800 __FILE__,__LINE__, tty->driver->name, info->count );
2803 info->flags |= ASYNC_NORMAL_ACTIVE;
2808 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2812 unsigned long flags;
2814 /* verify range of specified line number */
2816 if ((line < 0) || (line >= mgslpc_device_count)) {
2817 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2818 __FILE__,__LINE__,line);
2822 /* find the info structure for the specified line */
2823 info = mgslpc_device_list;
2824 while(info && info->line != line)
2825 info = info->next_device;
2826 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2829 tty->driver_data = info;
2832 if (debug_level >= DEBUG_LEVEL_INFO)
2833 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2834 __FILE__,__LINE__,tty->driver->name, info->count);
2836 /* If port is closing, signal caller to try again */
2837 if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
2838 if (info->flags & ASYNC_CLOSING)
2839 interruptible_sleep_on(&info->close_wait);
2840 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2841 -EAGAIN : -ERESTARTSYS);
2845 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2847 spin_lock_irqsave(&info->netlock, flags);
2848 if (info->netcount) {
2850 spin_unlock_irqrestore(&info->netlock, flags);
2854 spin_unlock_irqrestore(&info->netlock, flags);
2856 if (info->count == 1) {
2857 /* 1st open on this device, init hardware */
2858 retval = startup(info);
2863 retval = block_til_ready(tty, filp, info);
2865 if (debug_level >= DEBUG_LEVEL_INFO)
2866 printk("%s(%d):block_til_ready(%s) returned %d\n",
2867 __FILE__,__LINE__, info->device_name, retval);
2871 if (debug_level >= DEBUG_LEVEL_INFO)
2872 printk("%s(%d):mgslpc_open(%s) success\n",
2873 __FILE__,__LINE__, info->device_name);
2878 if (tty->count == 1)
2879 info->tty = NULL;/* tty layer will release tty struct */
2888 * /proc fs routines....
2891 static inline int line_info(char *buf, MGSLPC_INFO *info)
2895 unsigned long flags;
2897 ret = sprintf(buf, "%s:io:%04X irq:%d",
2898 info->device_name, info->io_base, info->irq_level);
2900 /* output current serial signal states */
2901 spin_lock_irqsave(&info->lock,flags);
2903 spin_unlock_irqrestore(&info->lock,flags);
2907 if (info->serial_signals & SerialSignal_RTS)
2908 strcat(stat_buf, "|RTS");
2909 if (info->serial_signals & SerialSignal_CTS)
2910 strcat(stat_buf, "|CTS");
2911 if (info->serial_signals & SerialSignal_DTR)
2912 strcat(stat_buf, "|DTR");
2913 if (info->serial_signals & SerialSignal_DSR)
2914 strcat(stat_buf, "|DSR");
2915 if (info->serial_signals & SerialSignal_DCD)
2916 strcat(stat_buf, "|CD");
2917 if (info->serial_signals & SerialSignal_RI)
2918 strcat(stat_buf, "|RI");
2920 if (info->params.mode == MGSL_MODE_HDLC) {
2921 ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
2922 info->icount.txok, info->icount.rxok);
2923 if (info->icount.txunder)
2924 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
2925 if (info->icount.txabort)
2926 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
2927 if (info->icount.rxshort)
2928 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
2929 if (info->icount.rxlong)
2930 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
2931 if (info->icount.rxover)
2932 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
2933 if (info->icount.rxcrc)
2934 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxcrc);
2936 ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
2937 info->icount.tx, info->icount.rx);
2938 if (info->icount.frame)
2939 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2940 if (info->icount.parity)
2941 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2942 if (info->icount.brk)
2943 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2944 if (info->icount.overrun)
2945 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2948 /* Append serial signal status to end */
2949 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2951 ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2952 info->tx_active,info->bh_requested,info->bh_running,
2958 /* Called to print information about devices
2960 int mgslpc_read_proc(char *page, char **start, off_t off, int count,
2961 int *eof, void *data)
2967 len += sprintf(page, "synclink driver:%s\n", driver_version);
2969 info = mgslpc_device_list;
2971 l = line_info(page + len, info);
2973 if (len+begin > off+count)
2975 if (len+begin < off) {
2979 info = info->next_device;
2984 if (off >= len+begin)
2986 *start = page + (off-begin);
2987 return ((count < begin+len-off) ? count : begin+len-off);
2990 int rx_alloc_buffers(MGSLPC_INFO *info)
2992 /* each buffer has header and data */
2993 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2995 /* calculate total allocation size for 8 buffers */
2996 info->rx_buf_total_size = info->rx_buf_size * 8;
2998 /* limit total allocated memory */
2999 if (info->rx_buf_total_size > 0x10000)
3000 info->rx_buf_total_size = 0x10000;
3002 /* calculate number of buffers */
3003 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
3005 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
3006 if (info->rx_buf == NULL)
3009 rx_reset_buffers(info);
3013 void rx_free_buffers(MGSLPC_INFO *info)
3016 kfree(info->rx_buf);
3017 info->rx_buf = NULL;
3020 int claim_resources(MGSLPC_INFO *info)
3022 if (rx_alloc_buffers(info) < 0 ) {
3023 printk( "Cant allocate rx buffer %s\n", info->device_name);
3024 release_resources(info);
3030 void release_resources(MGSLPC_INFO *info)
3032 if (debug_level >= DEBUG_LEVEL_INFO)
3033 printk("release_resources(%s)\n", info->device_name);
3034 rx_free_buffers(info);
3037 /* Add the specified device instance data structure to the
3038 * global linked list of devices and increment the device count.
3040 * Arguments: info pointer to device instance data
3042 void mgslpc_add_device(MGSLPC_INFO *info)
3044 info->next_device = NULL;
3045 info->line = mgslpc_device_count;
3046 sprintf(info->device_name,"ttySLP%d",info->line);
3048 if (info->line < MAX_DEVICE_COUNT) {
3049 if (maxframe[info->line])
3050 info->max_frame_size = maxframe[info->line];
3051 info->dosyncppp = dosyncppp[info->line];
3054 mgslpc_device_count++;
3056 if (!mgslpc_device_list)
3057 mgslpc_device_list = info;
3059 MGSLPC_INFO *current_dev = mgslpc_device_list;
3060 while( current_dev->next_device )
3061 current_dev = current_dev->next_device;
3062 current_dev->next_device = info;
3065 if (info->max_frame_size < 4096)
3066 info->max_frame_size = 4096;
3067 else if (info->max_frame_size > 65535)
3068 info->max_frame_size = 65535;
3070 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
3071 info->device_name, info->io_base, info->irq_level);
3074 #ifdef CONFIG_SYNCLINK_SYNCPPP
3076 if (info->dosyncppp)
3078 mgslpc_sppp_init(info);
3082 void mgslpc_remove_device(MGSLPC_INFO *remove_info)
3084 MGSLPC_INFO *info = mgslpc_device_list;
3085 MGSLPC_INFO *last = NULL;
3088 if (info == remove_info) {
3090 last->next_device = info->next_device;
3092 mgslpc_device_list = info->next_device;
3093 #ifdef CONFIG_SYNCLINK_SYNCPPP
3094 if (info->dosyncppp)
3095 mgslpc_sppp_delete(info);
3097 release_resources(info);
3099 mgslpc_device_count--;
3103 info = info->next_device;
3107 static struct pcmcia_driver mgslpc_driver = {
3108 .owner = THIS_MODULE,
3110 .name = "synclink_cs",
3112 .attach = mgslpc_attach,
3113 .detach = mgslpc_detach,
3116 static struct tty_operations mgslpc_ops = {
3117 .open = mgslpc_open,
3118 .close = mgslpc_close,
3119 .write = mgslpc_write,
3120 .put_char = mgslpc_put_char,
3121 .flush_chars = mgslpc_flush_chars,
3122 .write_room = mgslpc_write_room,
3123 .chars_in_buffer = mgslpc_chars_in_buffer,
3124 .flush_buffer = mgslpc_flush_buffer,
3125 .ioctl = mgslpc_ioctl,
3126 .throttle = mgslpc_throttle,
3127 .unthrottle = mgslpc_unthrottle,
3128 .send_xchar = mgslpc_send_xchar,
3129 .break_ctl = mgslpc_break,
3130 .wait_until_sent = mgslpc_wait_until_sent,
3131 .read_proc = mgslpc_read_proc,
3132 .set_termios = mgslpc_set_termios,
3134 .start = tx_release,
3135 .hangup = mgslpc_hangup,
3136 .tiocmget = tiocmget,
3137 .tiocmset = tiocmset,
3140 static void synclink_cs_cleanup(void)
3144 printk("Unloading %s: version %s\n", driver_name, driver_version);
3146 while(mgslpc_device_list)
3147 mgslpc_remove_device(mgslpc_device_list);
3149 if (serial_driver) {
3150 if ((rc = tty_unregister_driver(serial_driver)))
3151 printk("%s(%d) failed to unregister tty driver err=%d\n",
3152 __FILE__,__LINE__,rc);
3153 put_tty_driver(serial_driver);
3156 pcmcia_unregister_driver(&mgslpc_driver);
3158 /* XXX: this really needs to move into generic code.. */
3159 while (dev_list != NULL) {
3160 if (dev_list->state & DEV_CONFIG)
3161 mgslpc_release((u_long)dev_list);
3162 mgslpc_detach(dev_list);
3166 static int __init synclink_cs_init(void)
3170 if (break_on_load) {
3171 mgslpc_get_text_ptr();
3175 printk("%s %s\n", driver_name, driver_version);
3177 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
3180 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
3181 if (!serial_driver) {
3186 /* Initialize the tty_driver structure */
3188 serial_driver->owner = THIS_MODULE;
3189 serial_driver->driver_name = "synclink_cs";
3190 serial_driver->name = "ttySLP";
3191 serial_driver->major = ttymajor;
3192 serial_driver->minor_start = 64;
3193 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3194 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3195 serial_driver->init_termios = tty_std_termios;
3196 serial_driver->init_termios.c_cflag =
3197 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3198 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3199 tty_set_operations(serial_driver, &mgslpc_ops);
3201 if ((rc = tty_register_driver(serial_driver)) < 0) {
3202 printk("%s(%d):Couldn't register serial driver\n",
3204 put_tty_driver(serial_driver);
3205 serial_driver = NULL;
3209 printk("%s %s, tty major#%d\n",
3210 driver_name, driver_version,
3211 serial_driver->major);
3216 synclink_cs_cleanup();
3220 static void __exit synclink_cs_exit(void)
3222 synclink_cs_cleanup();
3225 module_init(synclink_cs_init);
3226 module_exit(synclink_cs_exit);
3228 void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
3233 /* note:standard BRG mode is broken in V3.2 chip
3234 * so enhanced mode is always used
3242 for (M = 1; N > 64 && M < 16; M++)
3248 * BGR[7..0] contained in BGR register
3249 * BGR[9..8] contained in CCR2[7..6]
3250 * divisor = (N+1)*2^M
3252 * Note: M *must* not be zero (causes asymetric duty cycle)
3254 write_reg(info, (unsigned char) (channel + BGR),
3255 (unsigned char) ((M << 6) + N));
3256 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
3257 val |= ((M << 4) & 0xc0);
3258 write_reg(info, (unsigned char) (channel + CCR2), val);
3262 /* Enabled the AUX clock output at the specified frequency.
3264 void enable_auxclk(MGSLPC_INFO *info)
3270 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3271 * 05 ADM Address Mode, 0 = no addr recognition
3272 * 04 TMD Timer Mode, 0 = external
3273 * 03 RAC Receiver Active, 0 = inactive
3274 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3275 * 01 TRS Timer Resolution, 1=512
3276 * 00 TLP Test Loop, 0 = no loop
3282 /* channel B RTS is used to enable AUXCLK driver on SP505 */
3283 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3285 write_reg(info, CHB + MODE, val);
3289 * 07 PU Power Up, 1=active, 0=power down
3290 * 06 MCE Master Clock Enable, 1=enabled
3292 * 04..02 SC[2..0] Encoding
3293 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3297 write_reg(info, CHB + CCR0, 0xc0);
3301 * 07 SFLG Shared Flag, 0 = disable shared flags
3302 * 06 GALP Go Active On Loop, 0 = not used
3303 * 05 GLP Go On Loop, 0 = not used
3304 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3305 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3306 * 02..00 CM[2..0] Clock Mode
3310 write_reg(info, CHB + CCR1, 0x17);
3314 * 07..06 BGR[9..8] Baud rate bits 9..8
3315 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3316 * 04 SSEL Clock source select, 1=submode b
3317 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3318 * 02 RWX Read/Write Exchange 0=disabled
3319 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3320 * 00 DIV, data inversion 0=disabled, 1=enabled
3324 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3325 write_reg(info, CHB + CCR2, 0x38);
3327 write_reg(info, CHB + CCR2, 0x30);
3331 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3332 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3333 * 05 TST1 Test Pin, 0=normal operation
3334 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3335 * 03..02 Reserved, must be 0
3336 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3340 write_reg(info, CHB + CCR4, 0x50);
3342 /* if auxclk not enabled, set internal BRG so
3343 * CTS transitions can be detected (requires TxC)
3345 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3346 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3348 mgslpc_set_rate(info, CHB, 921600);
3351 static void loopback_enable(MGSLPC_INFO *info)
3355 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3356 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3357 write_reg(info, CHA + CCR1, val);
3359 /* CCR2:04 SSEL Clock source select, 1=submode b */
3360 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3361 write_reg(info, CHA + CCR2, val);
3363 /* set LinkSpeed if available, otherwise default to 2Mbps */
3364 if (info->params.clock_speed)
3365 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3367 mgslpc_set_rate(info, CHA, 1843200);
3369 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3370 val = read_reg(info, CHA + MODE) | BIT0;
3371 write_reg(info, CHA + MODE, val);
3374 void hdlc_mode(MGSLPC_INFO *info)
3377 unsigned char clkmode, clksubmode;
3379 /* disable all interrupts */
3380 irq_disable(info, CHA, 0xffff);
3381 irq_disable(info, CHB, 0xffff);
3382 port_irq_disable(info, 0xff);
3384 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3385 clkmode = clksubmode = 0;
3386 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3387 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3388 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3390 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3391 && info->params.flags & HDLC_FLAG_TXC_BRG) {
3392 /* clock mode 7b, rcv = BRG, xmt = BRG */
3395 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3396 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3397 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3401 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3404 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3405 /* clock mode 0b, rcv = RxC, xmt = BRG */
3411 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3412 * 05 ADM Address Mode, 0 = no addr recognition
3413 * 04 TMD Timer Mode, 0 = external
3414 * 03 RAC Receiver Active, 0 = inactive
3415 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3416 * 01 TRS Timer Resolution, 1=512
3417 * 00 TLP Test Loop, 0 = no loop
3422 if (info->params.loopback)
3425 /* preserve RTS state */
3426 if (info->serial_signals & SerialSignal_RTS)
3428 write_reg(info, CHA + MODE, val);
3432 * 07 PU Power Up, 1=active, 0=power down
3433 * 06 MCE Master Clock Enable, 1=enabled
3435 * 04..02 SC[2..0] Encoding
3436 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3441 switch (info->params.encoding)
3443 case HDLC_ENCODING_NRZI:
3446 case HDLC_ENCODING_BIPHASE_SPACE:
3449 case HDLC_ENCODING_BIPHASE_MARK:
3452 case HDLC_ENCODING_BIPHASE_LEVEL:
3454 break; // Manchester
3456 write_reg(info, CHA + CCR0, val);
3460 * 07 SFLG Shared Flag, 0 = disable shared flags
3461 * 06 GALP Go Active On Loop, 0 = not used
3462 * 05 GLP Go On Loop, 0 = not used
3463 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3464 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3465 * 02..00 CM[2..0] Clock Mode
3469 val = 0x10 + clkmode;
3470 write_reg(info, CHA + CCR1, val);
3474 * 07..06 BGR[9..8] Baud rate bits 9..8
3475 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3476 * 04 SSEL Clock source select, 1=submode b
3477 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3478 * 02 RWX Read/Write Exchange 0=disabled
3479 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3480 * 00 DIV, data inversion 0=disabled, 1=enabled
3485 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3486 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3490 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3492 if (info->params.encoding == HDLC_ENCODING_NRZB)
3494 write_reg(info, CHA + CCR2, val);
3498 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3499 * 05 EPT Enable preamble transmission, 1=enabled
3500 * 04 RADD Receive address pushed to FIFO, 0=disabled
3501 * 03 CRL CRC Reset Level, 0=FFFF
3502 * 02 RCRC Rx CRC 0=On 1=Off
3503 * 01 TCRC Tx CRC 0=On 1=Off
3504 * 00 PSD DPLL Phase Shift Disable
3509 if (info->params.crc_type == HDLC_CRC_NONE)
3511 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3513 switch (info->params.preamble_length)
3515 case HDLC_PREAMBLE_LENGTH_16BITS:
3518 case HDLC_PREAMBLE_LENGTH_32BITS:
3521 case HDLC_PREAMBLE_LENGTH_64BITS:
3525 write_reg(info, CHA + CCR3, val);
3527 /* PRE - Preamble pattern */
3529 switch (info->params.preamble)
3531 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3532 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3533 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3534 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3536 write_reg(info, CHA + PRE, val);
3540 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3541 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3542 * 05 TST1 Test Pin, 0=normal operation
3543 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3544 * 03..02 Reserved, must be 0
3545 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3550 write_reg(info, CHA + CCR4, val);
3551 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3552 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3554 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3556 /* RLCR Receive length check register
3558 * 7 1=enable receive length check
3559 * 6..0 Max frame length = (RL + 1) * 32
3561 write_reg(info, CHA + RLCR, 0);
3563 /* XBCH Transmit Byte Count High
3565 * 07 DMA mode, 0 = interrupt driven
3566 * 06 NRM, 0=ABM (ignored)
3567 * 05 CAS Carrier Auto Start
3568 * 04 XC Transmit Continuously (ignored)
3569 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3574 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3576 write_reg(info, CHA + XBCH, val);
3577 enable_auxclk(info);
3578 if (info->params.loopback || info->testing_irq)
3579 loopback_enable(info);
3580 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3582 irq_enable(info, CHB, IRQ_CTS);
3583 /* PVR[3] 1=AUTO CTS active */
3584 set_reg_bits(info, CHA + PVR, BIT3);
3586 clear_reg_bits(info, CHA + PVR, BIT3);
3588 irq_enable(info, CHA,
3589 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3590 IRQ_UNDERRUN + IRQ_TXFIFO);
3591 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3592 wait_command_complete(info, CHA);
3593 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3595 /* Master clock mode enabled above to allow reset commands
3596 * to complete even if no data clocks are present.
3598 * Disable master clock mode for normal communications because
3599 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3600 * IRQ when in master clock mode.
3602 * Leave master clock mode enabled for IRQ test because the
3603 * timer IRQ used by the test can only happen in master clock mode.
3605 if (!info->testing_irq)
3606 clear_reg_bits(info, CHA + CCR0, BIT6);
3614 void rx_stop(MGSLPC_INFO *info)
3616 if (debug_level >= DEBUG_LEVEL_ISR)
3617 printk("%s(%d):rx_stop(%s)\n",
3618 __FILE__,__LINE__, info->device_name );
3620 /* MODE:03 RAC Receiver Active, 0=inactive */
3621 clear_reg_bits(info, CHA + MODE, BIT3);
3623 info->rx_enabled = 0;
3624 info->rx_overflow = 0;
3627 void rx_start(MGSLPC_INFO *info)
3629 if (debug_level >= DEBUG_LEVEL_ISR)
3630 printk("%s(%d):rx_start(%s)\n",
3631 __FILE__,__LINE__, info->device_name );
3633 rx_reset_buffers(info);
3634 info->rx_enabled = 0;
3635 info->rx_overflow = 0;
3637 /* MODE:03 RAC Receiver Active, 1=active */
3638 set_reg_bits(info, CHA + MODE, BIT3);
3640 info->rx_enabled = 1;
3643 void tx_start(MGSLPC_INFO *info)
3645 if (debug_level >= DEBUG_LEVEL_ISR)
3646 printk("%s(%d):tx_start(%s)\n",
3647 __FILE__,__LINE__, info->device_name );
3649 if (info->tx_count) {
3650 /* If auto RTS enabled and RTS is inactive, then assert */
3651 /* RTS and set a flag indicating that the driver should */
3652 /* negate RTS when the transmission completes. */
3653 info->drop_rts_on_tx_done = 0;
3655 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3657 if (!(info->serial_signals & SerialSignal_RTS)) {
3658 info->serial_signals |= SerialSignal_RTS;
3660 info->drop_rts_on_tx_done = 1;
3664 if (info->params.mode == MGSL_MODE_ASYNC) {
3665 if (!info->tx_active) {
3666 info->tx_active = 1;
3670 info->tx_active = 1;
3672 info->tx_timer.expires = jiffies + jiffies_from_ms(5000);
3673 add_timer(&info->tx_timer);
3677 if (!info->tx_enabled)
3678 info->tx_enabled = 1;
3681 void tx_stop(MGSLPC_INFO *info)
3683 if (debug_level >= DEBUG_LEVEL_ISR)
3684 printk("%s(%d):tx_stop(%s)\n",
3685 __FILE__,__LINE__, info->device_name );
3687 del_timer(&info->tx_timer);
3689 info->tx_enabled = 0;
3690 info->tx_active = 0;
3693 /* Reset the adapter to a known state and prepare it for further use.
3695 void reset_device(MGSLPC_INFO *info)
3697 /* power up both channels (set BIT7) */
3698 write_reg(info, CHA + CCR0, 0x80);
3699 write_reg(info, CHB + CCR0, 0x80);
3700 write_reg(info, CHA + MODE, 0);
3701 write_reg(info, CHB + MODE, 0);
3703 /* disable all interrupts */
3704 irq_disable(info, CHA, 0xffff);
3705 irq_disable(info, CHB, 0xffff);
3706 port_irq_disable(info, 0xff);
3708 /* PCR Port Configuration Register
3710 * 07..04 DEC[3..0] Serial I/F select outputs
3711 * 03 output, 1=AUTO CTS control enabled
3712 * 02 RI Ring Indicator input 0=active
3713 * 01 DSR input 0=active
3714 * 00 DTR output 0=active
3718 write_reg(info, PCR, 0x06);
3720 /* PVR Port Value Register
3722 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3723 * 03 AUTO CTS output 1=enabled
3724 * 02 RI Ring Indicator input
3726 * 00 DTR output (1=inactive)
3730 // write_reg(info, PVR, PVR_DTR);
3732 /* IPC Interrupt Port Configuration
3734 * 07 VIS 1=Masked interrupts visible
3735 * 06..05 Reserved, 0
3736 * 04..03 SLA Slave address, 00 ignored
3737 * 02 CASM Cascading Mode, 1=daisy chain
3738 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3742 write_reg(info, IPC, 0x05);
3745 void async_mode(MGSLPC_INFO *info)
3749 /* disable all interrupts */
3750 irq_disable(info, CHA, 0xffff);
3751 irq_disable(info, CHB, 0xffff);
3752 port_irq_disable(info, 0xff);
3757 * 06 FRTS RTS State, 0=active
3758 * 05 FCTS Flow Control on CTS
3759 * 04 FLON Flow Control Enable
3760 * 03 RAC Receiver Active, 0 = inactive
3761 * 02 RTS 0=Auto RTS, 1=manual RTS
3762 * 01 TRS Timer Resolution, 1=512
3763 * 00 TLP Test Loop, 0 = no loop
3768 if (info->params.loopback)
3771 /* preserve RTS state */
3772 if (!(info->serial_signals & SerialSignal_RTS))
3774 write_reg(info, CHA + MODE, val);
3778 * 07 PU Power Up, 1=active, 0=power down
3779 * 06 MCE Master Clock Enable, 1=enabled
3781 * 04..02 SC[2..0] Encoding, 000=NRZ
3782 * 01..00 SM[1..0] Serial Mode, 11=Async
3786 write_reg(info, CHA + CCR0, 0x83);
3790 * 07..05 Reserved, 0
3791 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3792 * 03 BCR Bit Clock Rate, 1=16x
3793 * 02..00 CM[2..0] Clock Mode, 111=BRG
3797 write_reg(info, CHA + CCR1, 0x1f);
3801 * 07..06 BGR[9..8] Baud rate bits 9..8
3802 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3803 * 04 SSEL Clock source select, 1=submode b
3804 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3805 * 02 RWX Read/Write Exchange 0=disabled
3807 * 00 DIV, data inversion 0=disabled, 1=enabled
3811 write_reg(info, CHA + CCR2, 0x10);
3815 * 07..01 Reserved, 0
3816 * 00 PSD DPLL Phase Shift Disable
3820 write_reg(info, CHA + CCR3, 0);
3824 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3825 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3826 * 05 TST1 Test Pin, 0=normal operation
3827 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3828 * 03..00 Reserved, must be 0
3832 write_reg(info, CHA + CCR4, 0x50);
3833 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3838 * 06 XBRK transmit break, 0=normal operation
3839 * 05 Stop bits (0=1, 1=2)
3840 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3841 * 02 PAREN Parity Enable
3842 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3846 if (info->params.data_bits != 8)
3847 val |= BIT0; /* 7 bits */
3848 if (info->params.stop_bits != 1)
3850 if (info->params.parity != ASYNC_PARITY_NONE)
3852 val |= BIT2; /* Parity enable */
3853 if (info->params.parity == ASYNC_PARITY_ODD)
3858 write_reg(info, CHA + DAFO, val);
3860 /* RFC Rx FIFO Control
3863 * 06 DPS, 1=parity bit not stored in data byte
3864 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3865 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3866 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3868 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3872 write_reg(info, CHA + RFC, 0x5c);
3874 /* RLCR Receive length check register
3876 * Max frame length = (RL + 1) * 32
3878 write_reg(info, CHA + RLCR, 0);
3880 /* XBCH Transmit Byte Count High
3882 * 07 DMA mode, 0 = interrupt driven
3883 * 06 NRM, 0=ABM (ignored)
3884 * 05 CAS Carrier Auto Start
3885 * 04 XC Transmit Continuously (ignored)
3886 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3891 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3893 write_reg(info, CHA + XBCH, val);
3894 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3895 irq_enable(info, CHA, IRQ_CTS);
3897 /* MODE:03 RAC Receiver Active, 1=active */
3898 set_reg_bits(info, CHA + MODE, BIT3);
3899 enable_auxclk(info);
3900 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3901 irq_enable(info, CHB, IRQ_CTS);
3902 /* PVR[3] 1=AUTO CTS active */
3903 set_reg_bits(info, CHA + PVR, BIT3);
3905 clear_reg_bits(info, CHA + PVR, BIT3);
3906 irq_enable(info, CHA,
3907 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3908 IRQ_ALLSENT + IRQ_TXFIFO);
3909 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3910 wait_command_complete(info, CHA);
3911 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3914 /* Set the HDLC idle mode for the transmitter.
3916 void tx_set_idle(MGSLPC_INFO *info)
3918 /* Note: ESCC2 only supports flags and one idle modes */
3919 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3920 set_reg_bits(info, CHA + CCR1, BIT3);
3922 clear_reg_bits(info, CHA + CCR1, BIT3);
3925 /* get state of the V24 status (input) signals.
3927 void get_signals(MGSLPC_INFO *info)
3929 unsigned char status = 0;
3931 /* preserve DTR and RTS */
3932 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3934 if (read_reg(info, CHB + VSTR) & BIT7)
3935 info->serial_signals |= SerialSignal_DCD;
3936 if (read_reg(info, CHB + STAR) & BIT1)
3937 info->serial_signals |= SerialSignal_CTS;
3939 status = read_reg(info, CHA + PVR);
3940 if (!(status & PVR_RI))
3941 info->serial_signals |= SerialSignal_RI;
3942 if (!(status & PVR_DSR))
3943 info->serial_signals |= SerialSignal_DSR;
3946 /* Set the state of DTR and RTS based on contents of
3947 * serial_signals member of device extension.
3949 void set_signals(MGSLPC_INFO *info)
3953 val = read_reg(info, CHA + MODE);
3954 if (info->params.mode == MGSL_MODE_ASYNC) {
3955 if (info->serial_signals & SerialSignal_RTS)
3960 if (info->serial_signals & SerialSignal_RTS)
3965 write_reg(info, CHA + MODE, val);
3967 if (info->serial_signals & SerialSignal_DTR)
3968 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3970 set_reg_bits(info, CHA + PVR, PVR_DTR);
3973 void rx_reset_buffers(MGSLPC_INFO *info)
3980 info->rx_frame_count = 0;
3981 for (i=0 ; i < info->rx_buf_count ; i++) {
3982 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3983 buf->status = buf->count = 0;
3987 /* Attempt to return a received HDLC frame
3988 * Only frames received without errors are returned.
3990 * Returns 1 if frame returned, otherwise 0
3992 int rx_get_frame(MGSLPC_INFO *info)
3994 unsigned short status;
3996 unsigned int framesize = 0;
3997 unsigned long flags;
3998 struct tty_struct *tty = info->tty;
3999 int return_frame = 0;
4001 if (info->rx_frame_count == 0)
4004 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
4006 status = buf->status;
4008 /* 07 VFR 1=valid frame
4009 * 06 RDO 1=data overrun
4010 * 05 CRC 1=OK, 0=error
4011 * 04 RAB 1=frame aborted
4013 if ((status & 0xf0) != 0xA0) {
4014 if (!(status & BIT7) || (status & BIT4))
4015 info->icount.rxabort++;
4016 else if (status & BIT6)
4017 info->icount.rxover++;
4018 else if (!(status & BIT5)) {
4019 info->icount.rxcrc++;
4020 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
4024 #ifdef CONFIG_SYNCLINK_SYNCPPP
4025 info->netstats.rx_errors++;
4026 info->netstats.rx_frame_errors++;
4032 framesize = buf->count;
4034 if (debug_level >= DEBUG_LEVEL_BH)
4035 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
4036 __FILE__,__LINE__,info->device_name,status,framesize);
4038 if (debug_level >= DEBUG_LEVEL_DATA)
4039 trace_block(info, buf->data, framesize, 0);
4042 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
4043 framesize+1 > info->max_frame_size) ||
4044 framesize > info->max_frame_size)
4045 info->icount.rxlong++;
4048 info->icount.rxok++;
4050 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4051 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
4055 #ifdef CONFIG_SYNCLINK_SYNCPPP
4056 if (info->netcount) {
4057 /* pass frame to syncppp device */
4058 mgslpc_sppp_rx_done(info, buf->data, framesize);
4063 /* Call the line discipline receive callback directly. */
4064 if (tty && tty->ldisc.receive_buf)
4065 tty->ldisc.receive_buf(tty, buf->data, info->flag_buf, framesize);
4070 spin_lock_irqsave(&info->lock,flags);
4071 buf->status = buf->count = 0;
4072 info->rx_frame_count--;
4074 if (info->rx_get >= info->rx_buf_count)
4076 spin_unlock_irqrestore(&info->lock,flags);
4081 BOOLEAN register_test(MGSLPC_INFO *info)
4083 static unsigned char patterns[] =
4084 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
4085 static unsigned int count = sizeof(patterns) / sizeof(patterns[0]);
4088 unsigned long flags;
4090 spin_lock_irqsave(&info->lock,flags);
4093 for (i = 0; i < count; i++) {
4094 write_reg(info, XAD1, patterns[i]);
4095 write_reg(info, XAD2, patterns[(i + 1) % count]);
4096 if ((read_reg(info, XAD1) != patterns[i]) ||
4097 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
4103 spin_unlock_irqrestore(&info->lock,flags);
4107 BOOLEAN irq_test(MGSLPC_INFO *info)
4109 unsigned long end_time;
4110 unsigned long flags;
4112 spin_lock_irqsave(&info->lock,flags);
4115 info->testing_irq = TRUE;
4118 info->irq_occurred = FALSE;
4120 /* init hdlc mode */
4122 irq_enable(info, CHA, IRQ_TIMER);
4123 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
4124 issue_command(info, CHA, CMD_START_TIMER);
4126 spin_unlock_irqrestore(&info->lock,flags);
4129 while(end_time-- && !info->irq_occurred) {
4130 set_current_state(TASK_INTERRUPTIBLE);
4131 schedule_timeout(jiffies_from_ms(10));
4134 info->testing_irq = FALSE;
4136 spin_lock_irqsave(&info->lock,flags);
4138 spin_unlock_irqrestore(&info->lock,flags);
4140 return info->irq_occurred ? TRUE : FALSE;
4143 int adapter_test(MGSLPC_INFO *info)
4145 if (!register_test(info)) {
4146 info->init_error = DiagStatus_AddressFailure;
4147 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
4148 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
4152 if (!irq_test(info)) {
4153 info->init_error = DiagStatus_IrqFailure;
4154 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
4155 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
4159 if (debug_level >= DEBUG_LEVEL_INFO)
4160 printk("%s(%d):device %s passed diagnostics\n",
4161 __FILE__,__LINE__,info->device_name);
4165 void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
4170 printk("%s tx data:\n",info->device_name);
4172 printk("%s rx data:\n",info->device_name);
4180 for(i=0;i<linecount;i++)
4181 printk("%02X ",(unsigned char)data[i]);
4184 for(i=0;i<linecount;i++) {
4185 if (data[i]>=040 && data[i]<=0176)
4186 printk("%c",data[i]);
4197 /* HDLC frame time out
4198 * update stats and do tx completion processing
4200 void tx_timeout(unsigned long context)
4202 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
4203 unsigned long flags;
4205 if ( debug_level >= DEBUG_LEVEL_INFO )
4206 printk( "%s(%d):tx_timeout(%s)\n",
4207 __FILE__,__LINE__,info->device_name);
4208 if(info->tx_active &&
4209 info->params.mode == MGSL_MODE_HDLC) {
4210 info->icount.txtimeout++;
4212 spin_lock_irqsave(&info->lock,flags);
4213 info->tx_active = 0;
4214 info->tx_count = info->tx_put = info->tx_get = 0;
4216 spin_unlock_irqrestore(&info->lock,flags);
4218 #ifdef CONFIG_SYNCLINK_SYNCPPP
4220 mgslpc_sppp_tx_done(info);
4226 #ifdef CONFIG_SYNCLINK_SYNCPPP
4227 /* syncppp net device routines
4230 static void mgslpc_setup(struct net_device *dev)
4232 dev->open = mgslpc_sppp_open;
4233 dev->stop = mgslpc_sppp_close;
4234 dev->hard_start_xmit = mgslpc_sppp_tx;
4235 dev->do_ioctl = mgslpc_sppp_ioctl;
4236 dev->get_stats = mgslpc_net_stats;
4237 dev->tx_timeout = mgslpc_sppp_tx_timeout;
4238 dev->watchdog_timeo = 10*HZ;
4241 void mgslpc_sppp_init(MGSLPC_INFO *info)
4243 struct net_device *d;
4245 sprintf(info->netname,"mgslp%d",info->line);
4247 d = alloc_netdev(0, info->netname, mgslpc_setup);
4249 printk(KERN_WARNING "%s: alloc_netdev failed.\n",
4254 info->if_ptr = &info->pppdev;
4255 info->netdev = info->pppdev.dev = d;
4257 d->base_addr = info->io_base;
4258 d->irq = info->irq_level;
4261 sppp_attach(&info->pppdev);
4264 if (register_netdev(d)) {
4265 printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
4266 sppp_detach(info->netdev);
4267 info->netdev = NULL;
4268 info->pppdev.dev = NULL;
4273 if (debug_level >= DEBUG_LEVEL_INFO)
4274 printk("mgslpc_sppp_init()\n");
4277 void mgslpc_sppp_delete(MGSLPC_INFO *info)
4279 if (debug_level >= DEBUG_LEVEL_INFO)
4280 printk("mgslpc_sppp_delete(%s)\n",info->netname);
4281 unregister_netdev(info->netdev);
4282 sppp_detach(info->netdev);
4283 free_netdev(info->netdev);
4284 info->netdev = NULL;
4285 info->pppdev.dev = NULL;
4288 int mgslpc_sppp_open(struct net_device *d)
4290 MGSLPC_INFO *info = d->priv;
4292 unsigned long flags;
4294 if (debug_level >= DEBUG_LEVEL_INFO)
4295 printk("mgslpc_sppp_open(%s)\n",info->netname);
4297 spin_lock_irqsave(&info->netlock, flags);
4298 if (info->count != 0 || info->netcount != 0) {
4299 printk(KERN_WARNING "%s: sppp_open returning busy\n", info->netname);
4300 spin_unlock_irqrestore(&info->netlock, flags);
4304 spin_unlock_irqrestore(&info->netlock, flags);
4306 /* claim resources and init adapter */
4307 if ((err = startup(info)) != 0)
4310 /* allow syncppp module to do open processing */
4311 if ((err = sppp_open(d)) != 0) {
4316 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4317 mgslpc_program_hw(info);
4319 d->trans_start = jiffies;
4320 netif_start_queue(d);
4324 spin_lock_irqsave(&info->netlock, flags);
4326 spin_unlock_irqrestore(&info->netlock, flags);
4330 void mgslpc_sppp_tx_timeout(struct net_device *dev)
4332 MGSLPC_INFO *info = dev->priv;
4333 unsigned long flags;
4335 if (debug_level >= DEBUG_LEVEL_INFO)
4336 printk("mgslpc_sppp_tx_timeout(%s)\n",info->netname);
4338 info->netstats.tx_errors++;
4339 info->netstats.tx_aborted_errors++;
4341 spin_lock_irqsave(&info->lock,flags);
4343 spin_unlock_irqrestore(&info->lock,flags);
4345 netif_wake_queue(dev);
4348 int mgslpc_sppp_tx(struct sk_buff *skb, struct net_device *dev)
4350 MGSLPC_INFO *info = dev->priv;
4351 unsigned long flags;
4353 if (debug_level >= DEBUG_LEVEL_INFO)
4354 printk("mgslpc_sppp_tx(%s)\n",info->netname);
4356 netif_stop_queue(dev);
4358 info->tx_count = skb->len;
4360 memcpy(info->tx_buf, skb->data, skb->len);
4362 info->tx_put = info->tx_count = skb->len;
4364 info->netstats.tx_packets++;
4365 info->netstats.tx_bytes += skb->len;
4368 dev->trans_start = jiffies;
4370 spin_lock_irqsave(&info->lock,flags);
4371 if (!info->tx_active)
4373 spin_unlock_irqrestore(&info->lock,flags);
4378 int mgslpc_sppp_close(struct net_device *d)
4380 MGSLPC_INFO *info = d->priv;
4381 unsigned long flags;
4383 if (debug_level >= DEBUG_LEVEL_INFO)
4384 printk("mgslpc_sppp_close(%s)\n",info->netname);
4386 /* shutdown adapter and release resources */
4389 /* allow syncppp to do close processing */
4391 netif_stop_queue(d);
4393 spin_lock_irqsave(&info->netlock, flags);
4395 spin_unlock_irqrestore(&info->netlock, flags);
4399 void mgslpc_sppp_rx_done(MGSLPC_INFO *info, char *buf, int size)
4401 struct sk_buff *skb = dev_alloc_skb(size);
4402 if (debug_level >= DEBUG_LEVEL_INFO)
4403 printk("mgslpc_sppp_rx_done(%s)\n",info->netname);
4405 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n",
4407 info->netstats.rx_dropped++;
4411 memcpy(skb_put(skb, size),buf,size);
4413 skb->protocol = htons(ETH_P_WAN_PPP);
4414 skb->dev = info->netdev;
4415 skb->mac.raw = skb->data;
4416 info->netstats.rx_packets++;
4417 info->netstats.rx_bytes += size;
4419 info->netdev->trans_start = jiffies;
4422 void mgslpc_sppp_tx_done(MGSLPC_INFO *info)
4424 if (netif_queue_stopped(info->netdev))
4425 netif_wake_queue(info->netdev);
4428 struct net_device_stats *mgslpc_net_stats(struct net_device *dev)
4430 MGSLPC_INFO *info = dev->priv;
4431 if (debug_level >= DEBUG_LEVEL_INFO)
4432 printk("mgslpc_net_stats(%s)\n",info->netname);
4433 return &info->netstats;
4436 int mgslpc_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4438 MGSLPC_INFO *info = dev->priv;
4439 if (debug_level >= DEBUG_LEVEL_INFO)
4440 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
4441 info->netname, cmd );
4442 return sppp_do_ioctl(dev, ifr, cmd);
4445 #endif /* ifdef CONFIG_SYNCLINK_SYNCPPP */