2 * linux/drivers/char/pcmcia/synclink_cs.c
4 * $Id: synclink_cs.c,v 4.34 2005/09/08 13:20:54 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/time.h>
45 #include <linux/interrupt.h>
46 #include <linux/pci.h>
47 #include <linux/tty.h>
48 #include <linux/tty_flip.h>
49 #include <linux/serial.h>
50 #include <linux/major.h>
51 #include <linux/string.h>
52 #include <linux/fcntl.h>
53 #include <linux/ptrace.h>
54 #include <linux/ioport.h>
56 #include <linux/slab.h>
57 #include <linux/netdevice.h>
58 #include <linux/vmalloc.h>
59 #include <linux/init.h>
60 #include <asm/serial.h>
61 #include <linux/delay.h>
62 #include <linux/ioctl.h>
64 #include <asm/system.h>
68 #include <linux/bitops.h>
69 #include <asm/types.h>
70 #include <linux/termios.h>
71 #include <linux/workqueue.h>
72 #include <linux/hdlc.h>
74 #include <pcmcia/cs_types.h>
75 #include <pcmcia/cs.h>
76 #include <pcmcia/cistpl.h>
77 #include <pcmcia/cisreg.h>
78 #include <pcmcia/ds.h>
80 #ifdef CONFIG_HDLC_MODULE
84 #define GET_USER(error,value,addr) error = get_user(value,addr)
85 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
86 #define PUT_USER(error,value,addr) error = put_user(value,addr)
87 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
89 #include <asm/uaccess.h>
91 #include "linux/synclink.h"
93 static MGSL_PARAMS default_params = {
94 MGSL_MODE_HDLC, /* unsigned long mode */
95 0, /* unsigned char loopback; */
96 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
97 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
98 0, /* unsigned long clock_speed; */
99 0xff, /* unsigned char addr_filter; */
100 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
101 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
102 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
103 9600, /* unsigned long data_rate; */
104 8, /* unsigned char data_bits; */
105 1, /* unsigned char stop_bits; */
106 ASYNC_PARITY_NONE /* unsigned char parity; */
112 unsigned char status;
116 /* The queue of BH actions to be performed */
119 #define BH_TRANSMIT 2
122 #define IO_PIN_SHUTDOWN_LIMIT 100
124 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
126 struct _input_signal_events {
139 * Device instance data structure
142 typedef struct _mgslpc_info {
143 void *if_ptr; /* General purpose pointer (used by SPPP) */
146 int count; /* count of opens */
148 unsigned short close_delay;
149 unsigned short closing_wait; /* time to wait before closing */
151 struct mgsl_icount icount;
153 struct tty_struct *tty;
155 int x_char; /* xon/xoff character */
156 int blocked_open; /* # of blocked opens */
157 unsigned char read_status_mask;
158 unsigned char ignore_status_mask;
160 unsigned char *tx_buf;
165 /* circular list of fixed length rx buffers */
167 unsigned char *rx_buf; /* memory allocated for all rx buffers */
168 int rx_buf_total_size; /* size of memory allocated for rx buffers */
169 int rx_put; /* index of next empty rx buffer */
170 int rx_get; /* index of next full rx buffer */
171 int rx_buf_size; /* size in bytes of single rx buffer */
172 int rx_buf_count; /* total number of rx buffers */
173 int rx_frame_count; /* number of full rx buffers */
175 wait_queue_head_t open_wait;
176 wait_queue_head_t close_wait;
178 wait_queue_head_t status_event_wait_q;
179 wait_queue_head_t event_wait_q;
180 struct timer_list tx_timer; /* HDLC transmit timeout timer */
181 struct _mgslpc_info *next_device; /* device list link */
183 unsigned short imra_value;
184 unsigned short imrb_value;
185 unsigned char pim_value;
188 struct work_struct task; /* task structure for scheduling bh */
197 int dcd_chkcount; /* check counts to prevent */
198 int cts_chkcount; /* too many IRQs if a signal */
199 int dsr_chkcount; /* is floating */
210 int if_mode; /* serial interface selection (RS-232, v.35 etc) */
212 char device_name[25]; /* device instance name */
214 unsigned int io_base; /* base I/O address of adapter */
215 unsigned int irq_level;
217 MGSL_PARAMS params; /* communications parameters */
219 unsigned char serial_signals; /* current serial signal states */
221 char irq_occurred; /* for diagnostics use */
223 unsigned int init_error; /* startup error (DIAGS) */
225 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
226 BOOLEAN drop_rts_on_tx_done;
228 struct _input_signal_events input_signal_events;
235 /* SPPP/Cisco HDLC device parts */
241 struct net_device *netdev;
246 #define MGSLPC_MAGIC 0x5402
249 * The size of the serial xmit buffer is 1 page, or 4096 bytes
251 #define TXBUFSIZE 4096
254 #define CHA 0x00 /* channel A offset */
255 #define CHB 0x40 /* channel B offset */
258 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
305 #define IRQ_BREAK_ON BIT15 // rx break detected
306 #define IRQ_DATAOVERRUN BIT14 // receive data overflow
307 #define IRQ_ALLSENT BIT13 // all sent
308 #define IRQ_UNDERRUN BIT12 // transmit data underrun
309 #define IRQ_TIMER BIT11 // timer interrupt
310 #define IRQ_CTS BIT10 // CTS status change
311 #define IRQ_TXREPEAT BIT9 // tx message repeat
312 #define IRQ_TXFIFO BIT8 // transmit pool ready
313 #define IRQ_RXEOM BIT7 // receive message end
314 #define IRQ_EXITHUNT BIT6 // receive frame start
315 #define IRQ_RXTIME BIT6 // rx char timeout
316 #define IRQ_DCD BIT2 // carrier detect status change
317 #define IRQ_OVERRUN BIT1 // receive frame overflow
318 #define IRQ_RXFIFO BIT0 // receive pool full
322 #define XFW BIT6 // transmit FIFO write enable
323 #define CEC BIT2 // command executing
324 #define CTS BIT1 // CTS state
329 #define PVR_AUTOCTS BIT3
330 #define PVR_RS232 0x20 /* 0010b */
331 #define PVR_V35 0xe0 /* 1110b */
332 #define PVR_RS422 0x40 /* 0100b */
334 /* Register access functions */
336 #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
337 #define read_reg(info, reg) inb((info)->io_base + (reg))
339 #define read_reg16(info, reg) inw((info)->io_base + (reg))
340 #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
342 #define set_reg_bits(info, reg, mask) \
343 write_reg(info, (reg), \
344 (unsigned char) (read_reg(info, (reg)) | (mask)))
345 #define clear_reg_bits(info, reg, mask) \
346 write_reg(info, (reg), \
347 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
349 * interrupt enable/disable routines
351 static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
353 if (channel == CHA) {
354 info->imra_value |= mask;
355 write_reg16(info, CHA + IMR, info->imra_value);
357 info->imrb_value |= mask;
358 write_reg16(info, CHB + IMR, info->imrb_value);
361 static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
363 if (channel == CHA) {
364 info->imra_value &= ~mask;
365 write_reg16(info, CHA + IMR, info->imra_value);
367 info->imrb_value &= ~mask;
368 write_reg16(info, CHB + IMR, info->imrb_value);
372 #define port_irq_disable(info, mask) \
373 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
375 #define port_irq_enable(info, mask) \
376 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
378 static void rx_start(MGSLPC_INFO *info);
379 static void rx_stop(MGSLPC_INFO *info);
381 static void tx_start(MGSLPC_INFO *info);
382 static void tx_stop(MGSLPC_INFO *info);
383 static void tx_set_idle(MGSLPC_INFO *info);
385 static void get_signals(MGSLPC_INFO *info);
386 static void set_signals(MGSLPC_INFO *info);
388 static void reset_device(MGSLPC_INFO *info);
390 static void hdlc_mode(MGSLPC_INFO *info);
391 static void async_mode(MGSLPC_INFO *info);
393 static void tx_timeout(unsigned long context);
395 static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg);
398 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
399 static void hdlcdev_tx_done(MGSLPC_INFO *info);
400 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
401 static int hdlcdev_init(MGSLPC_INFO *info);
402 static void hdlcdev_exit(MGSLPC_INFO *info);
405 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
407 static BOOLEAN register_test(MGSLPC_INFO *info);
408 static BOOLEAN irq_test(MGSLPC_INFO *info);
409 static int adapter_test(MGSLPC_INFO *info);
411 static int claim_resources(MGSLPC_INFO *info);
412 static void release_resources(MGSLPC_INFO *info);
413 static void mgslpc_add_device(MGSLPC_INFO *info);
414 static void mgslpc_remove_device(MGSLPC_INFO *info);
416 static int rx_get_frame(MGSLPC_INFO *info);
417 static void rx_reset_buffers(MGSLPC_INFO *info);
418 static int rx_alloc_buffers(MGSLPC_INFO *info);
419 static void rx_free_buffers(MGSLPC_INFO *info);
421 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs);
424 * Bottom half interrupt handlers
426 static void bh_handler(void* Context);
427 static void bh_transmit(MGSLPC_INFO *info);
428 static void bh_status(MGSLPC_INFO *info);
433 static int tiocmget(struct tty_struct *tty, struct file *file);
434 static int tiocmset(struct tty_struct *tty, struct file *file,
435 unsigned int set, unsigned int clear);
436 static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
437 static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
438 static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params);
439 static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
440 static int set_txidle(MGSLPC_INFO *info, int idle_mode);
441 static int set_txenable(MGSLPC_INFO *info, int enable);
442 static int tx_abort(MGSLPC_INFO *info);
443 static int set_rxenable(MGSLPC_INFO *info, int enable);
444 static int wait_events(MGSLPC_INFO *info, int __user *mask);
446 static MGSLPC_INFO *mgslpc_device_list = NULL;
447 static int mgslpc_device_count = 0;
450 * Set this param to non-zero to load eax with the
451 * .text section address and breakpoint on module load.
452 * This is useful for use with gdb and add-symbol-file command.
454 static int break_on_load=0;
457 * Driver major number, defaults to zero to get auto
458 * assigned major number. May be forced as module parameter.
460 static int ttymajor=0;
462 static int debug_level = 0;
463 static int maxframe[MAX_DEVICE_COUNT] = {0,};
464 static int dosyncppp[MAX_DEVICE_COUNT] = {1,1,1,1};
466 module_param(break_on_load, bool, 0);
467 module_param(ttymajor, int, 0);
468 module_param(debug_level, int, 0);
469 module_param_array(maxframe, int, NULL, 0);
470 module_param_array(dosyncppp, int, NULL, 0);
472 MODULE_LICENSE("GPL");
474 static char *driver_name = "SyncLink PC Card driver";
475 static char *driver_version = "$Revision: 4.34 $";
477 static struct tty_driver *serial_driver;
479 /* number of characters left in xmit buffer before we ask for more */
480 #define WAKEUP_CHARS 256
482 static void mgslpc_change_params(MGSLPC_INFO *info);
483 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
485 /* PCMCIA prototypes */
487 static void mgslpc_config(dev_link_t *link);
488 static void mgslpc_release(u_long arg);
489 static void mgslpc_detach(struct pcmcia_device *p_dev);
492 * 1st function defined in .text section. Calling this function in
493 * init_module() followed by a breakpoint allows a remote debugger
494 * (gdb) to get the .text address for the add-symbol-file command.
495 * This allows remote debugging of dynamically loadable modules.
497 static void* mgslpc_get_text_ptr(void)
499 return mgslpc_get_text_ptr;
503 * line discipline callback wrappers
505 * The wrappers maintain line discipline references
506 * while calling into the line discipline.
508 * ldisc_flush_buffer - flush line discipline receive buffers
509 * ldisc_receive_buf - pass receive data to line discipline
512 static void ldisc_flush_buffer(struct tty_struct *tty)
514 struct tty_ldisc *ld = tty_ldisc_ref(tty);
516 if (ld->flush_buffer)
517 ld->flush_buffer(tty);
522 static void ldisc_receive_buf(struct tty_struct *tty,
523 const __u8 *data, char *flags, int count)
525 struct tty_ldisc *ld;
528 ld = tty_ldisc_ref(tty);
531 ld->receive_buf(tty, data, flags, count);
536 static int mgslpc_attach(struct pcmcia_device *p_dev)
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_LEVEL_ID;
576 link->irq.Handler = NULL;
578 link->conf.Attributes = 0;
580 link->conf.IntType = INT_MEMORY_AND_IO;
582 link->handle = p_dev;
583 p_dev->instance = link;
585 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
588 mgslpc_add_device(info);
593 /* Card has been inserted.
596 #define CS_CHECK(fn, ret) \
597 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
599 static void mgslpc_config(dev_link_t *link)
601 client_handle_t handle = link->handle;
602 MGSLPC_INFO *info = link->priv;
605 int last_fn, last_ret;
608 cistpl_cftable_entry_t dflt = { 0 };
609 cistpl_cftable_entry_t *cfg;
611 if (debug_level >= DEBUG_LEVEL_INFO)
612 printk("mgslpc_config(0x%p)\n", link);
614 /* read CONFIG tuple to find its configuration registers */
615 tuple.DesiredTuple = CISTPL_CONFIG;
616 tuple.Attributes = 0;
617 tuple.TupleData = buf;
618 tuple.TupleDataMax = sizeof(buf);
619 tuple.TupleOffset = 0;
620 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
621 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
622 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
623 link->conf.ConfigBase = parse.config.base;
624 link->conf.Present = parse.config.rmask[0];
627 link->state |= DEV_CONFIG;
629 /* Look up the current Vcc */
630 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
631 link->conf.Vcc = conf.Vcc;
633 /* get CIS configuration entry */
635 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
636 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
638 cfg = &(parse.cftable_entry);
639 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
640 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
642 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
646 link->conf.ConfigIndex = cfg->index;
647 link->conf.Attributes |= CONF_ENABLE_IRQ;
649 /* IO window settings */
650 link->io.NumPorts1 = 0;
651 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
652 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
653 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
654 if (!(io->flags & CISTPL_IO_8BIT))
655 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
656 if (!(io->flags & CISTPL_IO_16BIT))
657 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
658 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
659 link->io.BasePort1 = io->win[0].base;
660 link->io.NumPorts1 = io->win[0].len;
661 CS_CHECK(RequestIO, pcmcia_request_io(link->handle, &link->io));
664 link->conf.Attributes = CONF_ENABLE_IRQ;
666 link->conf.IntType = INT_MEMORY_AND_IO;
667 link->conf.ConfigIndex = 8;
668 link->conf.Present = PRESENT_OPTION;
670 link->irq.Attributes |= IRQ_HANDLE_PRESENT;
671 link->irq.Handler = mgslpc_isr;
672 link->irq.Instance = info;
673 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
675 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
677 info->io_base = link->io.BasePort1;
678 info->irq_level = link->irq.AssignedIRQ;
680 /* add to linked list of devices */
681 sprintf(info->node.dev_name, "mgslpc0");
682 info->node.major = info->node.minor = 0;
683 link->dev = &info->node;
685 printk(KERN_INFO "%s: index 0x%02x:",
686 info->node.dev_name, link->conf.ConfigIndex);
687 if (link->conf.Attributes & CONF_ENABLE_IRQ)
688 printk(", irq %d", link->irq.AssignedIRQ);
689 if (link->io.NumPorts1)
690 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
691 link->io.BasePort1+link->io.NumPorts1-1);
694 link->state &= ~DEV_CONFIG_PENDING;
698 cs_error(link->handle, last_fn, last_ret);
699 mgslpc_release((u_long)link);
702 /* Card has been removed.
703 * Unregister device and release PCMCIA configuration.
704 * If device is open, postpone until it is closed.
706 static void mgslpc_release(u_long arg)
708 dev_link_t *link = (dev_link_t *)arg;
710 if (debug_level >= DEBUG_LEVEL_INFO)
711 printk("mgslpc_release(0x%p)\n", link);
713 /* Unlink the device chain */
715 link->state &= ~DEV_CONFIG;
717 pcmcia_release_configuration(link->handle);
718 if (link->io.NumPorts1)
719 pcmcia_release_io(link->handle, &link->io);
720 if (link->irq.AssignedIRQ)
721 pcmcia_release_irq(link->handle, &link->irq);
724 static void mgslpc_detach(struct pcmcia_device *p_dev)
726 dev_link_t *link = dev_to_instance(p_dev);
728 if (debug_level >= DEBUG_LEVEL_INFO)
729 printk("mgslpc_detach(0x%p)\n", link);
731 if (link->state & DEV_CONFIG) {
732 ((MGSLPC_INFO *)link->priv)->stop = 1;
733 mgslpc_release((u_long)link);
736 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
739 static int mgslpc_suspend(struct pcmcia_device *dev)
741 dev_link_t *link = dev_to_instance(dev);
742 MGSLPC_INFO *info = link->priv;
744 link->state |= DEV_SUSPEND;
746 if (link->state & DEV_CONFIG)
747 pcmcia_release_configuration(link->handle);
752 static int mgslpc_resume(struct pcmcia_device *dev)
754 dev_link_t *link = dev_to_instance(dev);
755 MGSLPC_INFO *info = link->priv;
757 link->state &= ~DEV_SUSPEND;
758 if (link->state & DEV_CONFIG)
759 pcmcia_request_configuration(link->handle, &link->conf);
766 static inline int mgslpc_paranoia_check(MGSLPC_INFO *info,
767 char *name, const char *routine)
769 #ifdef MGSLPC_PARANOIA_CHECK
770 static const char *badmagic =
771 "Warning: bad magic number for mgsl struct (%s) in %s\n";
772 static const char *badinfo =
773 "Warning: null mgslpc_info for (%s) in %s\n";
776 printk(badinfo, name, routine);
779 if (info->magic != MGSLPC_MAGIC) {
780 printk(badmagic, name, routine);
791 #define CMD_RXFIFO BIT7 // release current rx FIFO
792 #define CMD_RXRESET BIT6 // receiver reset
793 #define CMD_RXFIFO_READ BIT5
794 #define CMD_START_TIMER BIT4
795 #define CMD_TXFIFO BIT3 // release current tx FIFO
796 #define CMD_TXEOM BIT1 // transmit end message
797 #define CMD_TXRESET BIT0 // transmit reset
799 static BOOLEAN wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
802 /* wait for command completion */
803 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
811 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
813 wait_command_complete(info, channel);
814 write_reg(info, (unsigned char) (channel + CMDR), cmd);
817 static void tx_pause(struct tty_struct *tty)
819 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
822 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
824 if (debug_level >= DEBUG_LEVEL_INFO)
825 printk("tx_pause(%s)\n",info->device_name);
827 spin_lock_irqsave(&info->lock,flags);
828 if (info->tx_enabled)
830 spin_unlock_irqrestore(&info->lock,flags);
833 static void tx_release(struct tty_struct *tty)
835 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
838 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
840 if (debug_level >= DEBUG_LEVEL_INFO)
841 printk("tx_release(%s)\n",info->device_name);
843 spin_lock_irqsave(&info->lock,flags);
844 if (!info->tx_enabled)
846 spin_unlock_irqrestore(&info->lock,flags);
849 /* Return next bottom half action to perform.
850 * or 0 if nothing to do.
852 static int bh_action(MGSLPC_INFO *info)
857 spin_lock_irqsave(&info->lock,flags);
859 if (info->pending_bh & BH_RECEIVE) {
860 info->pending_bh &= ~BH_RECEIVE;
862 } else if (info->pending_bh & BH_TRANSMIT) {
863 info->pending_bh &= ~BH_TRANSMIT;
865 } else if (info->pending_bh & BH_STATUS) {
866 info->pending_bh &= ~BH_STATUS;
871 /* Mark BH routine as complete */
872 info->bh_running = 0;
873 info->bh_requested = 0;
876 spin_unlock_irqrestore(&info->lock,flags);
881 void bh_handler(void* Context)
883 MGSLPC_INFO *info = (MGSLPC_INFO*)Context;
889 if (debug_level >= DEBUG_LEVEL_BH)
890 printk( "%s(%d):bh_handler(%s) entry\n",
891 __FILE__,__LINE__,info->device_name);
893 info->bh_running = 1;
895 while((action = bh_action(info)) != 0) {
897 /* Process work item */
898 if ( debug_level >= DEBUG_LEVEL_BH )
899 printk( "%s(%d):bh_handler() work item action=%d\n",
900 __FILE__,__LINE__,action);
905 while(rx_get_frame(info));
914 /* unknown work item ID */
915 printk("Unknown work item ID=%08X!\n", action);
920 if (debug_level >= DEBUG_LEVEL_BH)
921 printk( "%s(%d):bh_handler(%s) exit\n",
922 __FILE__,__LINE__,info->device_name);
925 void bh_transmit(MGSLPC_INFO *info)
927 struct tty_struct *tty = info->tty;
928 if (debug_level >= DEBUG_LEVEL_BH)
929 printk("bh_transmit() entry on %s\n", info->device_name);
933 wake_up_interruptible(&tty->write_wait);
937 void bh_status(MGSLPC_INFO *info)
939 info->ri_chkcount = 0;
940 info->dsr_chkcount = 0;
941 info->dcd_chkcount = 0;
942 info->cts_chkcount = 0;
945 /* eom: non-zero = end of frame */
946 static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
948 unsigned char data[2];
949 unsigned char fifo_count, read_count, i;
950 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
952 if (debug_level >= DEBUG_LEVEL_ISR)
953 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
955 if (!info->rx_enabled)
958 if (info->rx_frame_count >= info->rx_buf_count) {
959 /* no more free buffers */
960 issue_command(info, CHA, CMD_RXRESET);
961 info->pending_bh |= BH_RECEIVE;
962 info->rx_overflow = 1;
963 info->icount.buf_overrun++;
968 /* end of frame, get FIFO count from RBCL register */
969 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
975 if (fifo_count == 1) {
977 data[0] = read_reg(info, CHA + RXFIFO);
980 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
982 fifo_count -= read_count;
983 if (!fifo_count && eom)
984 buf->status = data[--read_count];
986 for (i = 0; i < read_count; i++) {
987 if (buf->count >= info->max_frame_size) {
988 /* frame too large, reset receiver and reset current buffer */
989 issue_command(info, CHA, CMD_RXRESET);
993 *(buf->data + buf->count) = data[i];
996 } while (fifo_count);
999 info->pending_bh |= BH_RECEIVE;
1000 info->rx_frame_count++;
1002 if (info->rx_put >= info->rx_buf_count)
1005 issue_command(info, CHA, CMD_RXFIFO);
1008 static void rx_ready_async(MGSLPC_INFO *info, int tcd)
1010 unsigned char data, status;
1012 struct tty_struct *tty = info->tty;
1013 struct mgsl_icount *icount = &info->icount;
1016 /* early termination, get FIFO count from RBCL register */
1017 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
1019 /* Zero fifo count could mean 0 or 32 bytes available.
1020 * If BIT5 of STAR is set then at least 1 byte is available.
1022 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
1027 /* Flush received async data to receive data buffer. */
1028 while (fifo_count) {
1029 data = read_reg(info, CHA + RXFIFO);
1030 status = read_reg(info, CHA + RXFIFO);
1033 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1036 *tty->flip.char_buf_ptr = data;
1039 *tty->flip.flag_buf_ptr = 0;
1041 // if no frameing/crc error then save data
1042 // BIT7:parity error
1043 // BIT6:framing error
1045 if (status & (BIT7 + BIT6)) {
1051 /* discard char if tty control flags say so */
1052 if (status & info->ignore_status_mask)
1055 status &= info->read_status_mask;
1058 *tty->flip.flag_buf_ptr = TTY_PARITY;
1059 else if (status & BIT6)
1060 *tty->flip.flag_buf_ptr = TTY_FRAME;
1063 tty->flip.flag_buf_ptr++;
1064 tty->flip.char_buf_ptr++;
1067 issue_command(info, CHA, CMD_RXFIFO);
1069 if (debug_level >= DEBUG_LEVEL_ISR) {
1070 printk("%s(%d):rx_ready_async count=%d\n",
1071 __FILE__,__LINE__,tty->flip.count);
1072 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1073 __FILE__,__LINE__,icount->rx,icount->brk,
1074 icount->parity,icount->frame,icount->overrun);
1077 if (tty->flip.count)
1078 tty_flip_buffer_push(tty);
1082 static void tx_done(MGSLPC_INFO *info)
1084 if (!info->tx_active)
1087 info->tx_active = 0;
1088 info->tx_aborting = 0;
1090 if (info->params.mode == MGSL_MODE_ASYNC)
1093 info->tx_count = info->tx_put = info->tx_get = 0;
1094 del_timer(&info->tx_timer);
1096 if (info->drop_rts_on_tx_done) {
1098 if (info->serial_signals & SerialSignal_RTS) {
1099 info->serial_signals &= ~SerialSignal_RTS;
1102 info->drop_rts_on_tx_done = 0;
1107 hdlcdev_tx_done(info);
1111 if (info->tty->stopped || info->tty->hw_stopped) {
1115 info->pending_bh |= BH_TRANSMIT;
1119 static void tx_ready(MGSLPC_INFO *info)
1121 unsigned char fifo_count = 32;
1124 if (debug_level >= DEBUG_LEVEL_ISR)
1125 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1127 if (info->params.mode == MGSL_MODE_HDLC) {
1128 if (!info->tx_active)
1131 if (info->tty->stopped || info->tty->hw_stopped) {
1135 if (!info->tx_count)
1136 info->tx_active = 0;
1139 if (!info->tx_count)
1142 while (info->tx_count && fifo_count) {
1143 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
1146 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1148 write_reg16(info, CHA + TXFIFO,
1149 *((unsigned short*)(info->tx_buf + info->tx_get)));
1151 info->tx_count -= c;
1152 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1156 if (info->params.mode == MGSL_MODE_ASYNC) {
1157 if (info->tx_count < WAKEUP_CHARS)
1158 info->pending_bh |= BH_TRANSMIT;
1159 issue_command(info, CHA, CMD_TXFIFO);
1162 issue_command(info, CHA, CMD_TXFIFO);
1164 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1168 static void cts_change(MGSLPC_INFO *info)
1171 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1172 irq_disable(info, CHB, IRQ_CTS);
1174 if (info->serial_signals & SerialSignal_CTS)
1175 info->input_signal_events.cts_up++;
1177 info->input_signal_events.cts_down++;
1178 wake_up_interruptible(&info->status_event_wait_q);
1179 wake_up_interruptible(&info->event_wait_q);
1181 if (info->flags & ASYNC_CTS_FLOW) {
1182 if (info->tty->hw_stopped) {
1183 if (info->serial_signals & SerialSignal_CTS) {
1184 if (debug_level >= DEBUG_LEVEL_ISR)
1185 printk("CTS tx start...");
1187 info->tty->hw_stopped = 0;
1189 info->pending_bh |= BH_TRANSMIT;
1193 if (!(info->serial_signals & SerialSignal_CTS)) {
1194 if (debug_level >= DEBUG_LEVEL_ISR)
1195 printk("CTS tx stop...");
1197 info->tty->hw_stopped = 1;
1202 info->pending_bh |= BH_STATUS;
1205 static void dcd_change(MGSLPC_INFO *info)
1208 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1209 irq_disable(info, CHB, IRQ_DCD);
1211 if (info->serial_signals & SerialSignal_DCD) {
1212 info->input_signal_events.dcd_up++;
1215 info->input_signal_events.dcd_down++;
1218 hdlc_set_carrier(info->serial_signals & SerialSignal_DCD, info->netdev);
1220 wake_up_interruptible(&info->status_event_wait_q);
1221 wake_up_interruptible(&info->event_wait_q);
1223 if (info->flags & ASYNC_CHECK_CD) {
1224 if (debug_level >= DEBUG_LEVEL_ISR)
1225 printk("%s CD now %s...", info->device_name,
1226 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1227 if (info->serial_signals & SerialSignal_DCD)
1228 wake_up_interruptible(&info->open_wait);
1230 if (debug_level >= DEBUG_LEVEL_ISR)
1231 printk("doing serial hangup...");
1233 tty_hangup(info->tty);
1236 info->pending_bh |= BH_STATUS;
1239 static void dsr_change(MGSLPC_INFO *info)
1242 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1243 port_irq_disable(info, PVR_DSR);
1245 if (info->serial_signals & SerialSignal_DSR)
1246 info->input_signal_events.dsr_up++;
1248 info->input_signal_events.dsr_down++;
1249 wake_up_interruptible(&info->status_event_wait_q);
1250 wake_up_interruptible(&info->event_wait_q);
1251 info->pending_bh |= BH_STATUS;
1254 static void ri_change(MGSLPC_INFO *info)
1257 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1258 port_irq_disable(info, PVR_RI);
1260 if (info->serial_signals & SerialSignal_RI)
1261 info->input_signal_events.ri_up++;
1263 info->input_signal_events.ri_down++;
1264 wake_up_interruptible(&info->status_event_wait_q);
1265 wake_up_interruptible(&info->event_wait_q);
1266 info->pending_bh |= BH_STATUS;
1269 /* Interrupt service routine entry point.
1273 * irq interrupt number that caused interrupt
1274 * dev_id device ID supplied during interrupt registration
1275 * regs interrupted processor context
1277 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs)
1279 MGSLPC_INFO * info = (MGSLPC_INFO *)dev_id;
1281 unsigned char gis, pis;
1284 if (debug_level >= DEBUG_LEVEL_ISR)
1285 printk("mgslpc_isr(%d) entry.\n", irq);
1289 if (!(info->link.state & DEV_CONFIG))
1292 spin_lock(&info->lock);
1294 while ((gis = read_reg(info, CHA + GIS))) {
1295 if (debug_level >= DEBUG_LEVEL_ISR)
1296 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1298 if ((gis & 0x70) || count > 1000) {
1299 printk("synclink_cs:hardware failed or ejected\n");
1304 if (gis & (BIT1 + BIT0)) {
1305 isr = read_reg16(info, CHB + ISR);
1311 if (gis & (BIT3 + BIT2))
1313 isr = read_reg16(info, CHA + ISR);
1314 if (isr & IRQ_TIMER) {
1315 info->irq_occurred = 1;
1316 irq_disable(info, CHA, IRQ_TIMER);
1320 if (isr & IRQ_EXITHUNT) {
1321 info->icount.exithunt++;
1322 wake_up_interruptible(&info->event_wait_q);
1324 if (isr & IRQ_BREAK_ON) {
1326 if (info->flags & ASYNC_SAK)
1329 if (isr & IRQ_RXTIME) {
1330 issue_command(info, CHA, CMD_RXFIFO_READ);
1332 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1333 if (info->params.mode == MGSL_MODE_HDLC)
1334 rx_ready_hdlc(info, isr & IRQ_RXEOM);
1336 rx_ready_async(info, isr & IRQ_RXEOM);
1340 if (isr & IRQ_UNDERRUN) {
1341 if (info->tx_aborting)
1342 info->icount.txabort++;
1344 info->icount.txunder++;
1347 else if (isr & IRQ_ALLSENT) {
1348 info->icount.txok++;
1351 else if (isr & IRQ_TXFIFO)
1355 pis = read_reg(info, CHA + PIS);
1363 /* Request bottom half processing if there's something
1364 * for it to do and the bh is not already running
1367 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1368 if ( debug_level >= DEBUG_LEVEL_ISR )
1369 printk("%s(%d):%s queueing bh task.\n",
1370 __FILE__,__LINE__,info->device_name);
1371 schedule_work(&info->task);
1372 info->bh_requested = 1;
1375 spin_unlock(&info->lock);
1377 if (debug_level >= DEBUG_LEVEL_ISR)
1378 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1379 __FILE__,__LINE__,irq);
1384 /* Initialize and start device.
1386 static int startup(MGSLPC_INFO * info)
1390 if (debug_level >= DEBUG_LEVEL_INFO)
1391 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1393 if (info->flags & ASYNC_INITIALIZED)
1396 if (!info->tx_buf) {
1397 /* allocate a page of memory for a transmit buffer */
1398 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1399 if (!info->tx_buf) {
1400 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1401 __FILE__,__LINE__,info->device_name);
1406 info->pending_bh = 0;
1408 memset(&info->icount, 0, sizeof(info->icount));
1410 init_timer(&info->tx_timer);
1411 info->tx_timer.data = (unsigned long)info;
1412 info->tx_timer.function = tx_timeout;
1414 /* Allocate and claim adapter resources */
1415 retval = claim_resources(info);
1417 /* perform existance check and diagnostics */
1419 retval = adapter_test(info);
1422 if (capable(CAP_SYS_ADMIN) && info->tty)
1423 set_bit(TTY_IO_ERROR, &info->tty->flags);
1424 release_resources(info);
1428 /* program hardware for current parameters */
1429 mgslpc_change_params(info);
1432 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1434 info->flags |= ASYNC_INITIALIZED;
1439 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1441 static void shutdown(MGSLPC_INFO * info)
1443 unsigned long flags;
1445 if (!(info->flags & ASYNC_INITIALIZED))
1448 if (debug_level >= DEBUG_LEVEL_INFO)
1449 printk("%s(%d):mgslpc_shutdown(%s)\n",
1450 __FILE__,__LINE__, info->device_name );
1452 /* clear status wait queue because status changes */
1453 /* can't happen after shutting down the hardware */
1454 wake_up_interruptible(&info->status_event_wait_q);
1455 wake_up_interruptible(&info->event_wait_q);
1457 del_timer(&info->tx_timer);
1460 free_page((unsigned long) info->tx_buf);
1461 info->tx_buf = NULL;
1464 spin_lock_irqsave(&info->lock,flags);
1469 /* TODO:disable interrupts instead of reset to preserve signal states */
1472 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1473 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1477 spin_unlock_irqrestore(&info->lock,flags);
1479 release_resources(info);
1482 set_bit(TTY_IO_ERROR, &info->tty->flags);
1484 info->flags &= ~ASYNC_INITIALIZED;
1487 static void mgslpc_program_hw(MGSLPC_INFO *info)
1489 unsigned long flags;
1491 spin_lock_irqsave(&info->lock,flags);
1495 info->tx_count = info->tx_put = info->tx_get = 0;
1497 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1504 info->dcd_chkcount = 0;
1505 info->cts_chkcount = 0;
1506 info->ri_chkcount = 0;
1507 info->dsr_chkcount = 0;
1509 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1510 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1513 if (info->netcount || info->tty->termios->c_cflag & CREAD)
1516 spin_unlock_irqrestore(&info->lock,flags);
1519 /* Reconfigure adapter based on new parameters
1521 static void mgslpc_change_params(MGSLPC_INFO *info)
1526 if (!info->tty || !info->tty->termios)
1529 if (debug_level >= DEBUG_LEVEL_INFO)
1530 printk("%s(%d):mgslpc_change_params(%s)\n",
1531 __FILE__,__LINE__, info->device_name );
1533 cflag = info->tty->termios->c_cflag;
1535 /* if B0 rate (hangup) specified then negate DTR and RTS */
1536 /* otherwise assert DTR and RTS */
1538 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1540 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1542 /* byte size and parity */
1544 switch (cflag & CSIZE) {
1545 case CS5: info->params.data_bits = 5; break;
1546 case CS6: info->params.data_bits = 6; break;
1547 case CS7: info->params.data_bits = 7; break;
1548 case CS8: info->params.data_bits = 8; break;
1549 default: info->params.data_bits = 7; break;
1553 info->params.stop_bits = 2;
1555 info->params.stop_bits = 1;
1557 info->params.parity = ASYNC_PARITY_NONE;
1558 if (cflag & PARENB) {
1560 info->params.parity = ASYNC_PARITY_ODD;
1562 info->params.parity = ASYNC_PARITY_EVEN;
1565 info->params.parity = ASYNC_PARITY_SPACE;
1569 /* calculate number of jiffies to transmit a full
1570 * FIFO (32 bytes) at specified data rate
1572 bits_per_char = info->params.data_bits +
1573 info->params.stop_bits + 1;
1575 /* if port data rate is set to 460800 or less then
1576 * allow tty settings to override, otherwise keep the
1577 * current data rate.
1579 if (info->params.data_rate <= 460800) {
1580 info->params.data_rate = tty_get_baud_rate(info->tty);
1583 if ( info->params.data_rate ) {
1584 info->timeout = (32*HZ*bits_per_char) /
1585 info->params.data_rate;
1587 info->timeout += HZ/50; /* Add .02 seconds of slop */
1589 if (cflag & CRTSCTS)
1590 info->flags |= ASYNC_CTS_FLOW;
1592 info->flags &= ~ASYNC_CTS_FLOW;
1595 info->flags &= ~ASYNC_CHECK_CD;
1597 info->flags |= ASYNC_CHECK_CD;
1599 /* process tty input control flags */
1601 info->read_status_mask = 0;
1602 if (I_INPCK(info->tty))
1603 info->read_status_mask |= BIT7 | BIT6;
1604 if (I_IGNPAR(info->tty))
1605 info->ignore_status_mask |= BIT7 | BIT6;
1607 mgslpc_program_hw(info);
1610 /* Add a character to the transmit buffer
1612 static void mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1614 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1615 unsigned long flags;
1617 if (debug_level >= DEBUG_LEVEL_INFO) {
1618 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1619 __FILE__,__LINE__,ch,info->device_name);
1622 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1625 if (!tty || !info->tx_buf)
1628 spin_lock_irqsave(&info->lock,flags);
1630 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1631 if (info->tx_count < TXBUFSIZE - 1) {
1632 info->tx_buf[info->tx_put++] = ch;
1633 info->tx_put &= TXBUFSIZE-1;
1638 spin_unlock_irqrestore(&info->lock,flags);
1641 /* Enable transmitter so remaining characters in the
1642 * transmit buffer are sent.
1644 static void mgslpc_flush_chars(struct tty_struct *tty)
1646 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1647 unsigned long flags;
1649 if (debug_level >= DEBUG_LEVEL_INFO)
1650 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1651 __FILE__,__LINE__,info->device_name,info->tx_count);
1653 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1656 if (info->tx_count <= 0 || tty->stopped ||
1657 tty->hw_stopped || !info->tx_buf)
1660 if (debug_level >= DEBUG_LEVEL_INFO)
1661 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1662 __FILE__,__LINE__,info->device_name);
1664 spin_lock_irqsave(&info->lock,flags);
1665 if (!info->tx_active)
1667 spin_unlock_irqrestore(&info->lock,flags);
1670 /* Send a block of data
1674 * tty pointer to tty information structure
1675 * buf pointer to buffer containing send data
1676 * count size of send data in bytes
1678 * Returns: number of characters written
1680 static int mgslpc_write(struct tty_struct * tty,
1681 const unsigned char *buf, int count)
1684 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1685 unsigned long flags;
1687 if (debug_level >= DEBUG_LEVEL_INFO)
1688 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1689 __FILE__,__LINE__,info->device_name,count);
1691 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1692 !tty || !info->tx_buf)
1695 if (info->params.mode == MGSL_MODE_HDLC) {
1696 if (count > TXBUFSIZE) {
1700 if (info->tx_active)
1702 else if (info->tx_count)
1708 min(TXBUFSIZE - info->tx_count - 1,
1709 TXBUFSIZE - info->tx_put));
1713 memcpy(info->tx_buf + info->tx_put, buf, c);
1715 spin_lock_irqsave(&info->lock,flags);
1716 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1717 info->tx_count += c;
1718 spin_unlock_irqrestore(&info->lock,flags);
1725 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1726 spin_lock_irqsave(&info->lock,flags);
1727 if (!info->tx_active)
1729 spin_unlock_irqrestore(&info->lock,flags);
1732 if (debug_level >= DEBUG_LEVEL_INFO)
1733 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1734 __FILE__,__LINE__,info->device_name,ret);
1738 /* Return the count of free bytes in transmit buffer
1740 static int mgslpc_write_room(struct tty_struct *tty)
1742 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1745 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1748 if (info->params.mode == MGSL_MODE_HDLC) {
1749 /* HDLC (frame oriented) mode */
1750 if (info->tx_active)
1753 return HDLC_MAX_FRAME_SIZE;
1755 ret = TXBUFSIZE - info->tx_count - 1;
1760 if (debug_level >= DEBUG_LEVEL_INFO)
1761 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1762 __FILE__,__LINE__, info->device_name, ret);
1766 /* Return the count of bytes in transmit buffer
1768 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1770 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1773 if (debug_level >= DEBUG_LEVEL_INFO)
1774 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1775 __FILE__,__LINE__, info->device_name );
1777 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1780 if (info->params.mode == MGSL_MODE_HDLC)
1781 rc = info->tx_active ? info->max_frame_size : 0;
1783 rc = info->tx_count;
1785 if (debug_level >= DEBUG_LEVEL_INFO)
1786 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1787 __FILE__,__LINE__, info->device_name, rc);
1792 /* Discard all data in the send buffer
1794 static void mgslpc_flush_buffer(struct tty_struct *tty)
1796 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1797 unsigned long flags;
1799 if (debug_level >= DEBUG_LEVEL_INFO)
1800 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1801 __FILE__,__LINE__, info->device_name );
1803 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1806 spin_lock_irqsave(&info->lock,flags);
1807 info->tx_count = info->tx_put = info->tx_get = 0;
1808 del_timer(&info->tx_timer);
1809 spin_unlock_irqrestore(&info->lock,flags);
1811 wake_up_interruptible(&tty->write_wait);
1815 /* Send a high-priority XON/XOFF character
1817 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1819 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1820 unsigned long flags;
1822 if (debug_level >= DEBUG_LEVEL_INFO)
1823 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1824 __FILE__,__LINE__, info->device_name, ch );
1826 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1831 spin_lock_irqsave(&info->lock,flags);
1832 if (!info->tx_enabled)
1834 spin_unlock_irqrestore(&info->lock,flags);
1838 /* Signal remote device to throttle send data (our receive data)
1840 static void mgslpc_throttle(struct tty_struct * tty)
1842 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1843 unsigned long flags;
1845 if (debug_level >= DEBUG_LEVEL_INFO)
1846 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1847 __FILE__,__LINE__, info->device_name );
1849 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1853 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1855 if (tty->termios->c_cflag & CRTSCTS) {
1856 spin_lock_irqsave(&info->lock,flags);
1857 info->serial_signals &= ~SerialSignal_RTS;
1859 spin_unlock_irqrestore(&info->lock,flags);
1863 /* Signal remote device to stop throttling send data (our receive data)
1865 static void mgslpc_unthrottle(struct tty_struct * tty)
1867 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1868 unsigned long flags;
1870 if (debug_level >= DEBUG_LEVEL_INFO)
1871 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1872 __FILE__,__LINE__, info->device_name );
1874 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1881 mgslpc_send_xchar(tty, START_CHAR(tty));
1884 if (tty->termios->c_cflag & CRTSCTS) {
1885 spin_lock_irqsave(&info->lock,flags);
1886 info->serial_signals |= SerialSignal_RTS;
1888 spin_unlock_irqrestore(&info->lock,flags);
1892 /* get the current serial statistics
1894 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1897 if (debug_level >= DEBUG_LEVEL_INFO)
1898 printk("get_params(%s)\n", info->device_name);
1900 memset(&info->icount, 0, sizeof(info->icount));
1902 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1909 /* get the current serial parameters
1911 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1914 if (debug_level >= DEBUG_LEVEL_INFO)
1915 printk("get_params(%s)\n", info->device_name);
1916 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1922 /* set the serial parameters
1926 * info pointer to device instance data
1927 * new_params user buffer containing new serial params
1929 * Returns: 0 if success, otherwise error code
1931 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params)
1933 unsigned long flags;
1934 MGSL_PARAMS tmp_params;
1937 if (debug_level >= DEBUG_LEVEL_INFO)
1938 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1939 info->device_name );
1940 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1942 if ( debug_level >= DEBUG_LEVEL_INFO )
1943 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1944 __FILE__,__LINE__,info->device_name);
1948 spin_lock_irqsave(&info->lock,flags);
1949 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1950 spin_unlock_irqrestore(&info->lock,flags);
1952 mgslpc_change_params(info);
1957 static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1960 if (debug_level >= DEBUG_LEVEL_INFO)
1961 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1962 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1968 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1970 unsigned long flags;
1971 if (debug_level >= DEBUG_LEVEL_INFO)
1972 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1973 spin_lock_irqsave(&info->lock,flags);
1974 info->idle_mode = idle_mode;
1976 spin_unlock_irqrestore(&info->lock,flags);
1980 static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1983 if (debug_level >= DEBUG_LEVEL_INFO)
1984 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1985 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1991 static int set_interface(MGSLPC_INFO * info, int if_mode)
1993 unsigned long flags;
1995 if (debug_level >= DEBUG_LEVEL_INFO)
1996 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1997 spin_lock_irqsave(&info->lock,flags);
1998 info->if_mode = if_mode;
2000 val = read_reg(info, PVR) & 0x0f;
2001 switch (info->if_mode)
2003 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
2004 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
2005 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
2007 write_reg(info, PVR, val);
2009 spin_unlock_irqrestore(&info->lock,flags);
2013 static int set_txenable(MGSLPC_INFO * info, int enable)
2015 unsigned long flags;
2017 if (debug_level >= DEBUG_LEVEL_INFO)
2018 printk("set_txenable(%s,%d)\n", info->device_name, enable);
2020 spin_lock_irqsave(&info->lock,flags);
2022 if (!info->tx_enabled)
2025 if (info->tx_enabled)
2028 spin_unlock_irqrestore(&info->lock,flags);
2032 static int tx_abort(MGSLPC_INFO * info)
2034 unsigned long flags;
2036 if (debug_level >= DEBUG_LEVEL_INFO)
2037 printk("tx_abort(%s)\n", info->device_name);
2039 spin_lock_irqsave(&info->lock,flags);
2040 if (info->tx_active && info->tx_count &&
2041 info->params.mode == MGSL_MODE_HDLC) {
2042 /* clear data count so FIFO is not filled on next IRQ.
2043 * This results in underrun and abort transmission.
2045 info->tx_count = info->tx_put = info->tx_get = 0;
2046 info->tx_aborting = TRUE;
2048 spin_unlock_irqrestore(&info->lock,flags);
2052 static int set_rxenable(MGSLPC_INFO * info, int enable)
2054 unsigned long flags;
2056 if (debug_level >= DEBUG_LEVEL_INFO)
2057 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
2059 spin_lock_irqsave(&info->lock,flags);
2061 if (!info->rx_enabled)
2064 if (info->rx_enabled)
2067 spin_unlock_irqrestore(&info->lock,flags);
2071 /* wait for specified event to occur
2073 * Arguments: info pointer to device instance data
2074 * mask pointer to bitmask of events to wait for
2075 * Return Value: 0 if successful and bit mask updated with
2076 * of events triggerred,
2077 * otherwise error code
2079 static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
2081 unsigned long flags;
2084 struct mgsl_icount cprev, cnow;
2087 struct _input_signal_events oldsigs, newsigs;
2088 DECLARE_WAITQUEUE(wait, current);
2090 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2094 if (debug_level >= DEBUG_LEVEL_INFO)
2095 printk("wait_events(%s,%d)\n", info->device_name, mask);
2097 spin_lock_irqsave(&info->lock,flags);
2099 /* return immediately if state matches requested events */
2101 s = info->serial_signals;
2103 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2104 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2105 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2106 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2108 spin_unlock_irqrestore(&info->lock,flags);
2112 /* save current irq counts */
2113 cprev = info->icount;
2114 oldsigs = info->input_signal_events;
2116 if ((info->params.mode == MGSL_MODE_HDLC) &&
2117 (mask & MgslEvent_ExitHuntMode))
2118 irq_enable(info, CHA, IRQ_EXITHUNT);
2120 set_current_state(TASK_INTERRUPTIBLE);
2121 add_wait_queue(&info->event_wait_q, &wait);
2123 spin_unlock_irqrestore(&info->lock,flags);
2128 if (signal_pending(current)) {
2133 /* get current irq counts */
2134 spin_lock_irqsave(&info->lock,flags);
2135 cnow = info->icount;
2136 newsigs = info->input_signal_events;
2137 set_current_state(TASK_INTERRUPTIBLE);
2138 spin_unlock_irqrestore(&info->lock,flags);
2140 /* if no change, wait aborted for some reason */
2141 if (newsigs.dsr_up == oldsigs.dsr_up &&
2142 newsigs.dsr_down == oldsigs.dsr_down &&
2143 newsigs.dcd_up == oldsigs.dcd_up &&
2144 newsigs.dcd_down == oldsigs.dcd_down &&
2145 newsigs.cts_up == oldsigs.cts_up &&
2146 newsigs.cts_down == oldsigs.cts_down &&
2147 newsigs.ri_up == oldsigs.ri_up &&
2148 newsigs.ri_down == oldsigs.ri_down &&
2149 cnow.exithunt == cprev.exithunt &&
2150 cnow.rxidle == cprev.rxidle) {
2156 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2157 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2158 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2159 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2160 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2161 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2162 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2163 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2164 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2165 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2173 remove_wait_queue(&info->event_wait_q, &wait);
2174 set_current_state(TASK_RUNNING);
2176 if (mask & MgslEvent_ExitHuntMode) {
2177 spin_lock_irqsave(&info->lock,flags);
2178 if (!waitqueue_active(&info->event_wait_q))
2179 irq_disable(info, CHA, IRQ_EXITHUNT);
2180 spin_unlock_irqrestore(&info->lock,flags);
2184 PUT_USER(rc, events, mask_ptr);
2188 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2190 unsigned long flags;
2192 struct mgsl_icount cprev, cnow;
2193 DECLARE_WAITQUEUE(wait, current);
2195 /* save current irq counts */
2196 spin_lock_irqsave(&info->lock,flags);
2197 cprev = info->icount;
2198 add_wait_queue(&info->status_event_wait_q, &wait);
2199 set_current_state(TASK_INTERRUPTIBLE);
2200 spin_unlock_irqrestore(&info->lock,flags);
2204 if (signal_pending(current)) {
2209 /* get new irq counts */
2210 spin_lock_irqsave(&info->lock,flags);
2211 cnow = info->icount;
2212 set_current_state(TASK_INTERRUPTIBLE);
2213 spin_unlock_irqrestore(&info->lock,flags);
2215 /* if no change, wait aborted for some reason */
2216 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2217 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2222 /* check for change in caller specified modem input */
2223 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2224 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2225 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2226 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2233 remove_wait_queue(&info->status_event_wait_q, &wait);
2234 set_current_state(TASK_RUNNING);
2238 /* return the state of the serial control and status signals
2240 static int tiocmget(struct tty_struct *tty, struct file *file)
2242 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2243 unsigned int result;
2244 unsigned long flags;
2246 spin_lock_irqsave(&info->lock,flags);
2248 spin_unlock_irqrestore(&info->lock,flags);
2250 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2251 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2252 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2253 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2254 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2255 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2257 if (debug_level >= DEBUG_LEVEL_INFO)
2258 printk("%s(%d):%s tiocmget() value=%08X\n",
2259 __FILE__,__LINE__, info->device_name, result );
2263 /* set modem control signals (DTR/RTS)
2265 static int tiocmset(struct tty_struct *tty, struct file *file,
2266 unsigned int set, unsigned int clear)
2268 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2269 unsigned long flags;
2271 if (debug_level >= DEBUG_LEVEL_INFO)
2272 printk("%s(%d):%s tiocmset(%x,%x)\n",
2273 __FILE__,__LINE__,info->device_name, set, clear);
2275 if (set & TIOCM_RTS)
2276 info->serial_signals |= SerialSignal_RTS;
2277 if (set & TIOCM_DTR)
2278 info->serial_signals |= SerialSignal_DTR;
2279 if (clear & TIOCM_RTS)
2280 info->serial_signals &= ~SerialSignal_RTS;
2281 if (clear & TIOCM_DTR)
2282 info->serial_signals &= ~SerialSignal_DTR;
2284 spin_lock_irqsave(&info->lock,flags);
2286 spin_unlock_irqrestore(&info->lock,flags);
2291 /* Set or clear transmit break condition
2293 * Arguments: tty pointer to tty instance data
2294 * break_state -1=set break condition, 0=clear
2296 static void mgslpc_break(struct tty_struct *tty, int break_state)
2298 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2299 unsigned long flags;
2301 if (debug_level >= DEBUG_LEVEL_INFO)
2302 printk("%s(%d):mgslpc_break(%s,%d)\n",
2303 __FILE__,__LINE__, info->device_name, break_state);
2305 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2308 spin_lock_irqsave(&info->lock,flags);
2309 if (break_state == -1)
2310 set_reg_bits(info, CHA+DAFO, BIT6);
2312 clear_reg_bits(info, CHA+DAFO, BIT6);
2313 spin_unlock_irqrestore(&info->lock,flags);
2316 /* Service an IOCTL request
2320 * tty pointer to tty instance data
2321 * file pointer to associated file object for device
2322 * cmd IOCTL command code
2323 * arg command argument/context
2325 * Return Value: 0 if success, otherwise error code
2327 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2328 unsigned int cmd, unsigned long arg)
2330 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2332 if (debug_level >= DEBUG_LEVEL_INFO)
2333 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2334 info->device_name, cmd );
2336 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2339 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2340 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2341 if (tty->flags & (1 << TTY_IO_ERROR))
2345 return ioctl_common(info, cmd, arg);
2348 int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
2351 struct mgsl_icount cnow; /* kernel counter temps */
2352 struct serial_icounter_struct __user *p_cuser; /* user space */
2353 void __user *argp = (void __user *)arg;
2354 unsigned long flags;
2357 case MGSL_IOCGPARAMS:
2358 return get_params(info, argp);
2359 case MGSL_IOCSPARAMS:
2360 return set_params(info, argp);
2361 case MGSL_IOCGTXIDLE:
2362 return get_txidle(info, argp);
2363 case MGSL_IOCSTXIDLE:
2364 return set_txidle(info, (int)arg);
2366 return get_interface(info, argp);
2368 return set_interface(info,(int)arg);
2369 case MGSL_IOCTXENABLE:
2370 return set_txenable(info,(int)arg);
2371 case MGSL_IOCRXENABLE:
2372 return set_rxenable(info,(int)arg);
2373 case MGSL_IOCTXABORT:
2374 return tx_abort(info);
2375 case MGSL_IOCGSTATS:
2376 return get_stats(info, argp);
2377 case MGSL_IOCWAITEVENT:
2378 return wait_events(info, argp);
2380 return modem_input_wait(info,(int)arg);
2382 spin_lock_irqsave(&info->lock,flags);
2383 cnow = info->icount;
2384 spin_unlock_irqrestore(&info->lock,flags);
2386 PUT_USER(error,cnow.cts, &p_cuser->cts);
2387 if (error) return error;
2388 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2389 if (error) return error;
2390 PUT_USER(error,cnow.rng, &p_cuser->rng);
2391 if (error) return error;
2392 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2393 if (error) return error;
2394 PUT_USER(error,cnow.rx, &p_cuser->rx);
2395 if (error) return error;
2396 PUT_USER(error,cnow.tx, &p_cuser->tx);
2397 if (error) return error;
2398 PUT_USER(error,cnow.frame, &p_cuser->frame);
2399 if (error) return error;
2400 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2401 if (error) return error;
2402 PUT_USER(error,cnow.parity, &p_cuser->parity);
2403 if (error) return error;
2404 PUT_USER(error,cnow.brk, &p_cuser->brk);
2405 if (error) return error;
2406 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2407 if (error) return error;
2410 return -ENOIOCTLCMD;
2415 /* Set new termios settings
2419 * tty pointer to tty structure
2420 * termios pointer to buffer to hold returned old termios
2422 static void mgslpc_set_termios(struct tty_struct *tty, struct termios *old_termios)
2424 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2425 unsigned long flags;
2427 if (debug_level >= DEBUG_LEVEL_INFO)
2428 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2429 tty->driver->name );
2431 /* just return if nothing has changed */
2432 if ((tty->termios->c_cflag == old_termios->c_cflag)
2433 && (RELEVANT_IFLAG(tty->termios->c_iflag)
2434 == RELEVANT_IFLAG(old_termios->c_iflag)))
2437 mgslpc_change_params(info);
2439 /* Handle transition to B0 status */
2440 if (old_termios->c_cflag & CBAUD &&
2441 !(tty->termios->c_cflag & CBAUD)) {
2442 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2443 spin_lock_irqsave(&info->lock,flags);
2445 spin_unlock_irqrestore(&info->lock,flags);
2448 /* Handle transition away from B0 status */
2449 if (!(old_termios->c_cflag & CBAUD) &&
2450 tty->termios->c_cflag & CBAUD) {
2451 info->serial_signals |= SerialSignal_DTR;
2452 if (!(tty->termios->c_cflag & CRTSCTS) ||
2453 !test_bit(TTY_THROTTLED, &tty->flags)) {
2454 info->serial_signals |= SerialSignal_RTS;
2456 spin_lock_irqsave(&info->lock,flags);
2458 spin_unlock_irqrestore(&info->lock,flags);
2461 /* Handle turning off CRTSCTS */
2462 if (old_termios->c_cflag & CRTSCTS &&
2463 !(tty->termios->c_cflag & CRTSCTS)) {
2464 tty->hw_stopped = 0;
2469 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2471 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2473 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2476 if (debug_level >= DEBUG_LEVEL_INFO)
2477 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2478 __FILE__,__LINE__, info->device_name, info->count);
2483 if (tty_hung_up_p(filp))
2486 if ((tty->count == 1) && (info->count != 1)) {
2488 * tty->count is 1 and the tty structure will be freed.
2489 * info->count should be one in this case.
2490 * if it's not, correct it so that the port is shutdown.
2492 printk("mgslpc_close: bad refcount; tty->count is 1, "
2493 "info->count is %d\n", info->count);
2499 /* if at least one open remaining, leave hardware active */
2503 info->flags |= ASYNC_CLOSING;
2505 /* set tty->closing to notify line discipline to
2506 * only process XON/XOFF characters. Only the N_TTY
2507 * discipline appears to use this (ppp does not).
2511 /* wait for transmit data to clear all layers */
2513 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
2514 if (debug_level >= DEBUG_LEVEL_INFO)
2515 printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sent\n",
2516 __FILE__,__LINE__, info->device_name );
2517 tty_wait_until_sent(tty, info->closing_wait);
2520 if (info->flags & ASYNC_INITIALIZED)
2521 mgslpc_wait_until_sent(tty, info->timeout);
2523 if (tty->driver->flush_buffer)
2524 tty->driver->flush_buffer(tty);
2526 ldisc_flush_buffer(tty);
2533 if (info->blocked_open) {
2534 if (info->close_delay) {
2535 msleep_interruptible(jiffies_to_msecs(info->close_delay));
2537 wake_up_interruptible(&info->open_wait);
2540 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2542 wake_up_interruptible(&info->close_wait);
2545 if (debug_level >= DEBUG_LEVEL_INFO)
2546 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2547 tty->driver->name, info->count);
2550 /* Wait until the transmitter is empty.
2552 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2554 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2555 unsigned long orig_jiffies, char_time;
2560 if (debug_level >= DEBUG_LEVEL_INFO)
2561 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2562 __FILE__,__LINE__, info->device_name );
2564 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2567 if (!(info->flags & ASYNC_INITIALIZED))
2570 orig_jiffies = jiffies;
2572 /* Set check interval to 1/5 of estimated time to
2573 * send a character, and make it at least 1. The check
2574 * interval should also be less than the timeout.
2575 * Note: use tight timings here to satisfy the NIST-PCTS.
2578 if ( info->params.data_rate ) {
2579 char_time = info->timeout/(32 * 5);
2586 char_time = min_t(unsigned long, char_time, timeout);
2588 if (info->params.mode == MGSL_MODE_HDLC) {
2589 while (info->tx_active) {
2590 msleep_interruptible(jiffies_to_msecs(char_time));
2591 if (signal_pending(current))
2593 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2597 while ((info->tx_count || info->tx_active) &&
2599 msleep_interruptible(jiffies_to_msecs(char_time));
2600 if (signal_pending(current))
2602 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2608 if (debug_level >= DEBUG_LEVEL_INFO)
2609 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2610 __FILE__,__LINE__, info->device_name );
2613 /* Called by tty_hangup() when a hangup is signaled.
2614 * This is the same as closing all open files for the port.
2616 static void mgslpc_hangup(struct tty_struct *tty)
2618 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2620 if (debug_level >= DEBUG_LEVEL_INFO)
2621 printk("%s(%d):mgslpc_hangup(%s)\n",
2622 __FILE__,__LINE__, info->device_name );
2624 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2627 mgslpc_flush_buffer(tty);
2631 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2634 wake_up_interruptible(&info->open_wait);
2637 /* Block the current process until the specified port
2638 * is ready to be opened.
2640 static int block_til_ready(struct tty_struct *tty, struct file *filp,
2643 DECLARE_WAITQUEUE(wait, current);
2645 int do_clocal = 0, extra_count = 0;
2646 unsigned long flags;
2648 if (debug_level >= DEBUG_LEVEL_INFO)
2649 printk("%s(%d):block_til_ready on %s\n",
2650 __FILE__,__LINE__, tty->driver->name );
2652 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
2653 /* nonblock mode is set or port is not enabled */
2654 /* just verify that callout device is not active */
2655 info->flags |= ASYNC_NORMAL_ACTIVE;
2659 if (tty->termios->c_cflag & CLOCAL)
2662 /* Wait for carrier detect and the line to become
2663 * free (i.e., not in use by the callout). While we are in
2664 * this loop, info->count is dropped by one, so that
2665 * mgslpc_close() knows when to free things. We restore it upon
2666 * exit, either normal or abnormal.
2670 add_wait_queue(&info->open_wait, &wait);
2672 if (debug_level >= DEBUG_LEVEL_INFO)
2673 printk("%s(%d):block_til_ready before block on %s count=%d\n",
2674 __FILE__,__LINE__, tty->driver->name, info->count );
2676 spin_lock_irqsave(&info->lock, flags);
2677 if (!tty_hung_up_p(filp)) {
2681 spin_unlock_irqrestore(&info->lock, flags);
2682 info->blocked_open++;
2685 if ((tty->termios->c_cflag & CBAUD)) {
2686 spin_lock_irqsave(&info->lock,flags);
2687 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2689 spin_unlock_irqrestore(&info->lock,flags);
2692 set_current_state(TASK_INTERRUPTIBLE);
2694 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
2695 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
2696 -EAGAIN : -ERESTARTSYS;
2700 spin_lock_irqsave(&info->lock,flags);
2702 spin_unlock_irqrestore(&info->lock,flags);
2704 if (!(info->flags & ASYNC_CLOSING) &&
2705 (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
2709 if (signal_pending(current)) {
2710 retval = -ERESTARTSYS;
2714 if (debug_level >= DEBUG_LEVEL_INFO)
2715 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
2716 __FILE__,__LINE__, tty->driver->name, info->count );
2721 set_current_state(TASK_RUNNING);
2722 remove_wait_queue(&info->open_wait, &wait);
2726 info->blocked_open--;
2728 if (debug_level >= DEBUG_LEVEL_INFO)
2729 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
2730 __FILE__,__LINE__, tty->driver->name, info->count );
2733 info->flags |= ASYNC_NORMAL_ACTIVE;
2738 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2742 unsigned long flags;
2744 /* verify range of specified line number */
2746 if ((line < 0) || (line >= mgslpc_device_count)) {
2747 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2748 __FILE__,__LINE__,line);
2752 /* find the info structure for the specified line */
2753 info = mgslpc_device_list;
2754 while(info && info->line != line)
2755 info = info->next_device;
2756 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2759 tty->driver_data = info;
2762 if (debug_level >= DEBUG_LEVEL_INFO)
2763 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2764 __FILE__,__LINE__,tty->driver->name, info->count);
2766 /* If port is closing, signal caller to try again */
2767 if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
2768 if (info->flags & ASYNC_CLOSING)
2769 interruptible_sleep_on(&info->close_wait);
2770 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2771 -EAGAIN : -ERESTARTSYS);
2775 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2777 spin_lock_irqsave(&info->netlock, flags);
2778 if (info->netcount) {
2780 spin_unlock_irqrestore(&info->netlock, flags);
2784 spin_unlock_irqrestore(&info->netlock, flags);
2786 if (info->count == 1) {
2787 /* 1st open on this device, init hardware */
2788 retval = startup(info);
2793 retval = block_til_ready(tty, filp, info);
2795 if (debug_level >= DEBUG_LEVEL_INFO)
2796 printk("%s(%d):block_til_ready(%s) returned %d\n",
2797 __FILE__,__LINE__, info->device_name, retval);
2801 if (debug_level >= DEBUG_LEVEL_INFO)
2802 printk("%s(%d):mgslpc_open(%s) success\n",
2803 __FILE__,__LINE__, info->device_name);
2808 if (tty->count == 1)
2809 info->tty = NULL; /* tty layer will release tty struct */
2818 * /proc fs routines....
2821 static inline int line_info(char *buf, MGSLPC_INFO *info)
2825 unsigned long flags;
2827 ret = sprintf(buf, "%s:io:%04X irq:%d",
2828 info->device_name, info->io_base, info->irq_level);
2830 /* output current serial signal states */
2831 spin_lock_irqsave(&info->lock,flags);
2833 spin_unlock_irqrestore(&info->lock,flags);
2837 if (info->serial_signals & SerialSignal_RTS)
2838 strcat(stat_buf, "|RTS");
2839 if (info->serial_signals & SerialSignal_CTS)
2840 strcat(stat_buf, "|CTS");
2841 if (info->serial_signals & SerialSignal_DTR)
2842 strcat(stat_buf, "|DTR");
2843 if (info->serial_signals & SerialSignal_DSR)
2844 strcat(stat_buf, "|DSR");
2845 if (info->serial_signals & SerialSignal_DCD)
2846 strcat(stat_buf, "|CD");
2847 if (info->serial_signals & SerialSignal_RI)
2848 strcat(stat_buf, "|RI");
2850 if (info->params.mode == MGSL_MODE_HDLC) {
2851 ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
2852 info->icount.txok, info->icount.rxok);
2853 if (info->icount.txunder)
2854 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
2855 if (info->icount.txabort)
2856 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
2857 if (info->icount.rxshort)
2858 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
2859 if (info->icount.rxlong)
2860 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
2861 if (info->icount.rxover)
2862 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
2863 if (info->icount.rxcrc)
2864 ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
2866 ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
2867 info->icount.tx, info->icount.rx);
2868 if (info->icount.frame)
2869 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2870 if (info->icount.parity)
2871 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2872 if (info->icount.brk)
2873 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2874 if (info->icount.overrun)
2875 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2878 /* Append serial signal status to end */
2879 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2881 ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2882 info->tx_active,info->bh_requested,info->bh_running,
2888 /* Called to print information about devices
2890 static int mgslpc_read_proc(char *page, char **start, off_t off, int count,
2891 int *eof, void *data)
2897 len += sprintf(page, "synclink driver:%s\n", driver_version);
2899 info = mgslpc_device_list;
2901 l = line_info(page + len, info);
2903 if (len+begin > off+count)
2905 if (len+begin < off) {
2909 info = info->next_device;
2914 if (off >= len+begin)
2916 *start = page + (off-begin);
2917 return ((count < begin+len-off) ? count : begin+len-off);
2920 int rx_alloc_buffers(MGSLPC_INFO *info)
2922 /* each buffer has header and data */
2923 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2925 /* calculate total allocation size for 8 buffers */
2926 info->rx_buf_total_size = info->rx_buf_size * 8;
2928 /* limit total allocated memory */
2929 if (info->rx_buf_total_size > 0x10000)
2930 info->rx_buf_total_size = 0x10000;
2932 /* calculate number of buffers */
2933 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2935 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2936 if (info->rx_buf == NULL)
2939 rx_reset_buffers(info);
2943 void rx_free_buffers(MGSLPC_INFO *info)
2945 kfree(info->rx_buf);
2946 info->rx_buf = NULL;
2949 int claim_resources(MGSLPC_INFO *info)
2951 if (rx_alloc_buffers(info) < 0 ) {
2952 printk( "Cant allocate rx buffer %s\n", info->device_name);
2953 release_resources(info);
2959 void release_resources(MGSLPC_INFO *info)
2961 if (debug_level >= DEBUG_LEVEL_INFO)
2962 printk("release_resources(%s)\n", info->device_name);
2963 rx_free_buffers(info);
2966 /* Add the specified device instance data structure to the
2967 * global linked list of devices and increment the device count.
2969 * Arguments: info pointer to device instance data
2971 void mgslpc_add_device(MGSLPC_INFO *info)
2973 info->next_device = NULL;
2974 info->line = mgslpc_device_count;
2975 sprintf(info->device_name,"ttySLP%d",info->line);
2977 if (info->line < MAX_DEVICE_COUNT) {
2978 if (maxframe[info->line])
2979 info->max_frame_size = maxframe[info->line];
2980 info->dosyncppp = dosyncppp[info->line];
2983 mgslpc_device_count++;
2985 if (!mgslpc_device_list)
2986 mgslpc_device_list = info;
2988 MGSLPC_INFO *current_dev = mgslpc_device_list;
2989 while( current_dev->next_device )
2990 current_dev = current_dev->next_device;
2991 current_dev->next_device = info;
2994 if (info->max_frame_size < 4096)
2995 info->max_frame_size = 4096;
2996 else if (info->max_frame_size > 65535)
2997 info->max_frame_size = 65535;
2999 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
3000 info->device_name, info->io_base, info->irq_level);
3007 void mgslpc_remove_device(MGSLPC_INFO *remove_info)
3009 MGSLPC_INFO *info = mgslpc_device_list;
3010 MGSLPC_INFO *last = NULL;
3013 if (info == remove_info) {
3015 last->next_device = info->next_device;
3017 mgslpc_device_list = info->next_device;
3021 release_resources(info);
3023 mgslpc_device_count--;
3027 info = info->next_device;
3031 static struct pcmcia_device_id mgslpc_ids[] = {
3032 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
3035 MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
3037 static struct pcmcia_driver mgslpc_driver = {
3038 .owner = THIS_MODULE,
3040 .name = "synclink_cs",
3042 .probe = mgslpc_attach,
3043 .remove = mgslpc_detach,
3044 .id_table = mgslpc_ids,
3045 .suspend = mgslpc_suspend,
3046 .resume = mgslpc_resume,
3049 static struct tty_operations mgslpc_ops = {
3050 .open = mgslpc_open,
3051 .close = mgslpc_close,
3052 .write = mgslpc_write,
3053 .put_char = mgslpc_put_char,
3054 .flush_chars = mgslpc_flush_chars,
3055 .write_room = mgslpc_write_room,
3056 .chars_in_buffer = mgslpc_chars_in_buffer,
3057 .flush_buffer = mgslpc_flush_buffer,
3058 .ioctl = mgslpc_ioctl,
3059 .throttle = mgslpc_throttle,
3060 .unthrottle = mgslpc_unthrottle,
3061 .send_xchar = mgslpc_send_xchar,
3062 .break_ctl = mgslpc_break,
3063 .wait_until_sent = mgslpc_wait_until_sent,
3064 .read_proc = mgslpc_read_proc,
3065 .set_termios = mgslpc_set_termios,
3067 .start = tx_release,
3068 .hangup = mgslpc_hangup,
3069 .tiocmget = tiocmget,
3070 .tiocmset = tiocmset,
3073 static void synclink_cs_cleanup(void)
3077 printk("Unloading %s: version %s\n", driver_name, driver_version);
3079 while(mgslpc_device_list)
3080 mgslpc_remove_device(mgslpc_device_list);
3082 if (serial_driver) {
3083 if ((rc = tty_unregister_driver(serial_driver)))
3084 printk("%s(%d) failed to unregister tty driver err=%d\n",
3085 __FILE__,__LINE__,rc);
3086 put_tty_driver(serial_driver);
3089 pcmcia_unregister_driver(&mgslpc_driver);
3092 static int __init synclink_cs_init(void)
3096 if (break_on_load) {
3097 mgslpc_get_text_ptr();
3101 printk("%s %s\n", driver_name, driver_version);
3103 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
3106 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
3107 if (!serial_driver) {
3112 /* Initialize the tty_driver structure */
3114 serial_driver->owner = THIS_MODULE;
3115 serial_driver->driver_name = "synclink_cs";
3116 serial_driver->name = "ttySLP";
3117 serial_driver->major = ttymajor;
3118 serial_driver->minor_start = 64;
3119 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3120 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3121 serial_driver->init_termios = tty_std_termios;
3122 serial_driver->init_termios.c_cflag =
3123 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3124 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3125 tty_set_operations(serial_driver, &mgslpc_ops);
3127 if ((rc = tty_register_driver(serial_driver)) < 0) {
3128 printk("%s(%d):Couldn't register serial driver\n",
3130 put_tty_driver(serial_driver);
3131 serial_driver = NULL;
3135 printk("%s %s, tty major#%d\n",
3136 driver_name, driver_version,
3137 serial_driver->major);
3142 synclink_cs_cleanup();
3146 static void __exit synclink_cs_exit(void)
3148 synclink_cs_cleanup();
3151 module_init(synclink_cs_init);
3152 module_exit(synclink_cs_exit);
3154 static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
3159 /* note:standard BRG mode is broken in V3.2 chip
3160 * so enhanced mode is always used
3168 for (M = 1; N > 64 && M < 16; M++)
3174 * BGR[7..0] contained in BGR register
3175 * BGR[9..8] contained in CCR2[7..6]
3176 * divisor = (N+1)*2^M
3178 * Note: M *must* not be zero (causes asymetric duty cycle)
3180 write_reg(info, (unsigned char) (channel + BGR),
3181 (unsigned char) ((M << 6) + N));
3182 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
3183 val |= ((M << 4) & 0xc0);
3184 write_reg(info, (unsigned char) (channel + CCR2), val);
3188 /* Enabled the AUX clock output at the specified frequency.
3190 static void enable_auxclk(MGSLPC_INFO *info)
3196 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3197 * 05 ADM Address Mode, 0 = no addr recognition
3198 * 04 TMD Timer Mode, 0 = external
3199 * 03 RAC Receiver Active, 0 = inactive
3200 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3201 * 01 TRS Timer Resolution, 1=512
3202 * 00 TLP Test Loop, 0 = no loop
3208 /* channel B RTS is used to enable AUXCLK driver on SP505 */
3209 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3211 write_reg(info, CHB + MODE, val);
3215 * 07 PU Power Up, 1=active, 0=power down
3216 * 06 MCE Master Clock Enable, 1=enabled
3218 * 04..02 SC[2..0] Encoding
3219 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3223 write_reg(info, CHB + CCR0, 0xc0);
3227 * 07 SFLG Shared Flag, 0 = disable shared flags
3228 * 06 GALP Go Active On Loop, 0 = not used
3229 * 05 GLP Go On Loop, 0 = not used
3230 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3231 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3232 * 02..00 CM[2..0] Clock Mode
3236 write_reg(info, CHB + CCR1, 0x17);
3240 * 07..06 BGR[9..8] Baud rate bits 9..8
3241 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3242 * 04 SSEL Clock source select, 1=submode b
3243 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3244 * 02 RWX Read/Write Exchange 0=disabled
3245 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3246 * 00 DIV, data inversion 0=disabled, 1=enabled
3250 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3251 write_reg(info, CHB + CCR2, 0x38);
3253 write_reg(info, CHB + CCR2, 0x30);
3257 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3258 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3259 * 05 TST1 Test Pin, 0=normal operation
3260 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3261 * 03..02 Reserved, must be 0
3262 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3266 write_reg(info, CHB + CCR4, 0x50);
3268 /* if auxclk not enabled, set internal BRG so
3269 * CTS transitions can be detected (requires TxC)
3271 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3272 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3274 mgslpc_set_rate(info, CHB, 921600);
3277 static void loopback_enable(MGSLPC_INFO *info)
3281 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3282 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3283 write_reg(info, CHA + CCR1, val);
3285 /* CCR2:04 SSEL Clock source select, 1=submode b */
3286 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3287 write_reg(info, CHA + CCR2, val);
3289 /* set LinkSpeed if available, otherwise default to 2Mbps */
3290 if (info->params.clock_speed)
3291 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3293 mgslpc_set_rate(info, CHA, 1843200);
3295 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3296 val = read_reg(info, CHA + MODE) | BIT0;
3297 write_reg(info, CHA + MODE, val);
3300 void hdlc_mode(MGSLPC_INFO *info)
3303 unsigned char clkmode, clksubmode;
3305 /* disable all interrupts */
3306 irq_disable(info, CHA, 0xffff);
3307 irq_disable(info, CHB, 0xffff);
3308 port_irq_disable(info, 0xff);
3310 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3311 clkmode = clksubmode = 0;
3312 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3313 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3314 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3316 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3317 && info->params.flags & HDLC_FLAG_TXC_BRG) {
3318 /* clock mode 7b, rcv = BRG, xmt = BRG */
3321 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3322 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3323 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3327 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3330 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3331 /* clock mode 0b, rcv = RxC, xmt = BRG */
3337 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3338 * 05 ADM Address Mode, 0 = no addr recognition
3339 * 04 TMD Timer Mode, 0 = external
3340 * 03 RAC Receiver Active, 0 = inactive
3341 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3342 * 01 TRS Timer Resolution, 1=512
3343 * 00 TLP Test Loop, 0 = no loop
3348 if (info->params.loopback)
3351 /* preserve RTS state */
3352 if (info->serial_signals & SerialSignal_RTS)
3354 write_reg(info, CHA + MODE, val);
3358 * 07 PU Power Up, 1=active, 0=power down
3359 * 06 MCE Master Clock Enable, 1=enabled
3361 * 04..02 SC[2..0] Encoding
3362 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3367 switch (info->params.encoding)
3369 case HDLC_ENCODING_NRZI:
3372 case HDLC_ENCODING_BIPHASE_SPACE:
3375 case HDLC_ENCODING_BIPHASE_MARK:
3378 case HDLC_ENCODING_BIPHASE_LEVEL:
3380 break; // Manchester
3382 write_reg(info, CHA + CCR0, val);
3386 * 07 SFLG Shared Flag, 0 = disable shared flags
3387 * 06 GALP Go Active On Loop, 0 = not used
3388 * 05 GLP Go On Loop, 0 = not used
3389 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3390 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3391 * 02..00 CM[2..0] Clock Mode
3395 val = 0x10 + clkmode;
3396 write_reg(info, CHA + CCR1, val);
3400 * 07..06 BGR[9..8] Baud rate bits 9..8
3401 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3402 * 04 SSEL Clock source select, 1=submode b
3403 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3404 * 02 RWX Read/Write Exchange 0=disabled
3405 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3406 * 00 DIV, data inversion 0=disabled, 1=enabled
3411 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3412 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3416 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3418 if (info->params.encoding == HDLC_ENCODING_NRZB)
3420 write_reg(info, CHA + CCR2, val);
3424 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3425 * 05 EPT Enable preamble transmission, 1=enabled
3426 * 04 RADD Receive address pushed to FIFO, 0=disabled
3427 * 03 CRL CRC Reset Level, 0=FFFF
3428 * 02 RCRC Rx CRC 0=On 1=Off
3429 * 01 TCRC Tx CRC 0=On 1=Off
3430 * 00 PSD DPLL Phase Shift Disable
3435 if (info->params.crc_type == HDLC_CRC_NONE)
3437 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3439 switch (info->params.preamble_length)
3441 case HDLC_PREAMBLE_LENGTH_16BITS:
3444 case HDLC_PREAMBLE_LENGTH_32BITS:
3447 case HDLC_PREAMBLE_LENGTH_64BITS:
3451 write_reg(info, CHA + CCR3, val);
3453 /* PRE - Preamble pattern */
3455 switch (info->params.preamble)
3457 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3458 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3459 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3460 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3462 write_reg(info, CHA + PRE, val);
3466 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3467 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3468 * 05 TST1 Test Pin, 0=normal operation
3469 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3470 * 03..02 Reserved, must be 0
3471 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3476 write_reg(info, CHA + CCR4, val);
3477 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3478 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3480 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3482 /* RLCR Receive length check register
3484 * 7 1=enable receive length check
3485 * 6..0 Max frame length = (RL + 1) * 32
3487 write_reg(info, CHA + RLCR, 0);
3489 /* XBCH Transmit Byte Count High
3491 * 07 DMA mode, 0 = interrupt driven
3492 * 06 NRM, 0=ABM (ignored)
3493 * 05 CAS Carrier Auto Start
3494 * 04 XC Transmit Continuously (ignored)
3495 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3500 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3502 write_reg(info, CHA + XBCH, val);
3503 enable_auxclk(info);
3504 if (info->params.loopback || info->testing_irq)
3505 loopback_enable(info);
3506 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3508 irq_enable(info, CHB, IRQ_CTS);
3509 /* PVR[3] 1=AUTO CTS active */
3510 set_reg_bits(info, CHA + PVR, BIT3);
3512 clear_reg_bits(info, CHA + PVR, BIT3);
3514 irq_enable(info, CHA,
3515 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3516 IRQ_UNDERRUN + IRQ_TXFIFO);
3517 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3518 wait_command_complete(info, CHA);
3519 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3521 /* Master clock mode enabled above to allow reset commands
3522 * to complete even if no data clocks are present.
3524 * Disable master clock mode for normal communications because
3525 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3526 * IRQ when in master clock mode.
3528 * Leave master clock mode enabled for IRQ test because the
3529 * timer IRQ used by the test can only happen in master clock mode.
3531 if (!info->testing_irq)
3532 clear_reg_bits(info, CHA + CCR0, BIT6);
3540 void rx_stop(MGSLPC_INFO *info)
3542 if (debug_level >= DEBUG_LEVEL_ISR)
3543 printk("%s(%d):rx_stop(%s)\n",
3544 __FILE__,__LINE__, info->device_name );
3546 /* MODE:03 RAC Receiver Active, 0=inactive */
3547 clear_reg_bits(info, CHA + MODE, BIT3);
3549 info->rx_enabled = 0;
3550 info->rx_overflow = 0;
3553 void rx_start(MGSLPC_INFO *info)
3555 if (debug_level >= DEBUG_LEVEL_ISR)
3556 printk("%s(%d):rx_start(%s)\n",
3557 __FILE__,__LINE__, info->device_name );
3559 rx_reset_buffers(info);
3560 info->rx_enabled = 0;
3561 info->rx_overflow = 0;
3563 /* MODE:03 RAC Receiver Active, 1=active */
3564 set_reg_bits(info, CHA + MODE, BIT3);
3566 info->rx_enabled = 1;
3569 void tx_start(MGSLPC_INFO *info)
3571 if (debug_level >= DEBUG_LEVEL_ISR)
3572 printk("%s(%d):tx_start(%s)\n",
3573 __FILE__,__LINE__, info->device_name );
3575 if (info->tx_count) {
3576 /* If auto RTS enabled and RTS is inactive, then assert */
3577 /* RTS and set a flag indicating that the driver should */
3578 /* negate RTS when the transmission completes. */
3579 info->drop_rts_on_tx_done = 0;
3581 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3583 if (!(info->serial_signals & SerialSignal_RTS)) {
3584 info->serial_signals |= SerialSignal_RTS;
3586 info->drop_rts_on_tx_done = 1;
3590 if (info->params.mode == MGSL_MODE_ASYNC) {
3591 if (!info->tx_active) {
3592 info->tx_active = 1;
3596 info->tx_active = 1;
3598 info->tx_timer.expires = jiffies + msecs_to_jiffies(5000);
3599 add_timer(&info->tx_timer);
3603 if (!info->tx_enabled)
3604 info->tx_enabled = 1;
3607 void tx_stop(MGSLPC_INFO *info)
3609 if (debug_level >= DEBUG_LEVEL_ISR)
3610 printk("%s(%d):tx_stop(%s)\n",
3611 __FILE__,__LINE__, info->device_name );
3613 del_timer(&info->tx_timer);
3615 info->tx_enabled = 0;
3616 info->tx_active = 0;
3619 /* Reset the adapter to a known state and prepare it for further use.
3621 void reset_device(MGSLPC_INFO *info)
3623 /* power up both channels (set BIT7) */
3624 write_reg(info, CHA + CCR0, 0x80);
3625 write_reg(info, CHB + CCR0, 0x80);
3626 write_reg(info, CHA + MODE, 0);
3627 write_reg(info, CHB + MODE, 0);
3629 /* disable all interrupts */
3630 irq_disable(info, CHA, 0xffff);
3631 irq_disable(info, CHB, 0xffff);
3632 port_irq_disable(info, 0xff);
3634 /* PCR Port Configuration Register
3636 * 07..04 DEC[3..0] Serial I/F select outputs
3637 * 03 output, 1=AUTO CTS control enabled
3638 * 02 RI Ring Indicator input 0=active
3639 * 01 DSR input 0=active
3640 * 00 DTR output 0=active
3644 write_reg(info, PCR, 0x06);
3646 /* PVR Port Value Register
3648 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3649 * 03 AUTO CTS output 1=enabled
3650 * 02 RI Ring Indicator input
3652 * 00 DTR output (1=inactive)
3656 // write_reg(info, PVR, PVR_DTR);
3658 /* IPC Interrupt Port Configuration
3660 * 07 VIS 1=Masked interrupts visible
3661 * 06..05 Reserved, 0
3662 * 04..03 SLA Slave address, 00 ignored
3663 * 02 CASM Cascading Mode, 1=daisy chain
3664 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3668 write_reg(info, IPC, 0x05);
3671 void async_mode(MGSLPC_INFO *info)
3675 /* disable all interrupts */
3676 irq_disable(info, CHA, 0xffff);
3677 irq_disable(info, CHB, 0xffff);
3678 port_irq_disable(info, 0xff);
3683 * 06 FRTS RTS State, 0=active
3684 * 05 FCTS Flow Control on CTS
3685 * 04 FLON Flow Control Enable
3686 * 03 RAC Receiver Active, 0 = inactive
3687 * 02 RTS 0=Auto RTS, 1=manual RTS
3688 * 01 TRS Timer Resolution, 1=512
3689 * 00 TLP Test Loop, 0 = no loop
3694 if (info->params.loopback)
3697 /* preserve RTS state */
3698 if (!(info->serial_signals & SerialSignal_RTS))
3700 write_reg(info, CHA + MODE, val);
3704 * 07 PU Power Up, 1=active, 0=power down
3705 * 06 MCE Master Clock Enable, 1=enabled
3707 * 04..02 SC[2..0] Encoding, 000=NRZ
3708 * 01..00 SM[1..0] Serial Mode, 11=Async
3712 write_reg(info, CHA + CCR0, 0x83);
3716 * 07..05 Reserved, 0
3717 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3718 * 03 BCR Bit Clock Rate, 1=16x
3719 * 02..00 CM[2..0] Clock Mode, 111=BRG
3723 write_reg(info, CHA + CCR1, 0x1f);
3727 * 07..06 BGR[9..8] Baud rate bits 9..8
3728 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3729 * 04 SSEL Clock source select, 1=submode b
3730 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3731 * 02 RWX Read/Write Exchange 0=disabled
3733 * 00 DIV, data inversion 0=disabled, 1=enabled
3737 write_reg(info, CHA + CCR2, 0x10);
3741 * 07..01 Reserved, 0
3742 * 00 PSD DPLL Phase Shift Disable
3746 write_reg(info, CHA + CCR3, 0);
3750 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3751 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3752 * 05 TST1 Test Pin, 0=normal operation
3753 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3754 * 03..00 Reserved, must be 0
3758 write_reg(info, CHA + CCR4, 0x50);
3759 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3764 * 06 XBRK transmit break, 0=normal operation
3765 * 05 Stop bits (0=1, 1=2)
3766 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3767 * 02 PAREN Parity Enable
3768 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3772 if (info->params.data_bits != 8)
3773 val |= BIT0; /* 7 bits */
3774 if (info->params.stop_bits != 1)
3776 if (info->params.parity != ASYNC_PARITY_NONE)
3778 val |= BIT2; /* Parity enable */
3779 if (info->params.parity == ASYNC_PARITY_ODD)
3784 write_reg(info, CHA + DAFO, val);
3786 /* RFC Rx FIFO Control
3789 * 06 DPS, 1=parity bit not stored in data byte
3790 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3791 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3792 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3794 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3798 write_reg(info, CHA + RFC, 0x5c);
3800 /* RLCR Receive length check register
3802 * Max frame length = (RL + 1) * 32
3804 write_reg(info, CHA + RLCR, 0);
3806 /* XBCH Transmit Byte Count High
3808 * 07 DMA mode, 0 = interrupt driven
3809 * 06 NRM, 0=ABM (ignored)
3810 * 05 CAS Carrier Auto Start
3811 * 04 XC Transmit Continuously (ignored)
3812 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3817 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3819 write_reg(info, CHA + XBCH, val);
3820 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3821 irq_enable(info, CHA, IRQ_CTS);
3823 /* MODE:03 RAC Receiver Active, 1=active */
3824 set_reg_bits(info, CHA + MODE, BIT3);
3825 enable_auxclk(info);
3826 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3827 irq_enable(info, CHB, IRQ_CTS);
3828 /* PVR[3] 1=AUTO CTS active */
3829 set_reg_bits(info, CHA + PVR, BIT3);
3831 clear_reg_bits(info, CHA + PVR, BIT3);
3832 irq_enable(info, CHA,
3833 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3834 IRQ_ALLSENT + IRQ_TXFIFO);
3835 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3836 wait_command_complete(info, CHA);
3837 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3840 /* Set the HDLC idle mode for the transmitter.
3842 void tx_set_idle(MGSLPC_INFO *info)
3844 /* Note: ESCC2 only supports flags and one idle modes */
3845 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3846 set_reg_bits(info, CHA + CCR1, BIT3);
3848 clear_reg_bits(info, CHA + CCR1, BIT3);
3851 /* get state of the V24 status (input) signals.
3853 void get_signals(MGSLPC_INFO *info)
3855 unsigned char status = 0;
3857 /* preserve DTR and RTS */
3858 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3860 if (read_reg(info, CHB + VSTR) & BIT7)
3861 info->serial_signals |= SerialSignal_DCD;
3862 if (read_reg(info, CHB + STAR) & BIT1)
3863 info->serial_signals |= SerialSignal_CTS;
3865 status = read_reg(info, CHA + PVR);
3866 if (!(status & PVR_RI))
3867 info->serial_signals |= SerialSignal_RI;
3868 if (!(status & PVR_DSR))
3869 info->serial_signals |= SerialSignal_DSR;
3872 /* Set the state of DTR and RTS based on contents of
3873 * serial_signals member of device extension.
3875 void set_signals(MGSLPC_INFO *info)
3879 val = read_reg(info, CHA + MODE);
3880 if (info->params.mode == MGSL_MODE_ASYNC) {
3881 if (info->serial_signals & SerialSignal_RTS)
3886 if (info->serial_signals & SerialSignal_RTS)
3891 write_reg(info, CHA + MODE, val);
3893 if (info->serial_signals & SerialSignal_DTR)
3894 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3896 set_reg_bits(info, CHA + PVR, PVR_DTR);
3899 void rx_reset_buffers(MGSLPC_INFO *info)
3906 info->rx_frame_count = 0;
3907 for (i=0 ; i < info->rx_buf_count ; i++) {
3908 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3909 buf->status = buf->count = 0;
3913 /* Attempt to return a received HDLC frame
3914 * Only frames received without errors are returned.
3916 * Returns 1 if frame returned, otherwise 0
3918 int rx_get_frame(MGSLPC_INFO *info)
3920 unsigned short status;
3922 unsigned int framesize = 0;
3923 unsigned long flags;
3924 struct tty_struct *tty = info->tty;
3925 int return_frame = 0;
3927 if (info->rx_frame_count == 0)
3930 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3932 status = buf->status;
3934 /* 07 VFR 1=valid frame
3935 * 06 RDO 1=data overrun
3936 * 05 CRC 1=OK, 0=error
3937 * 04 RAB 1=frame aborted
3939 if ((status & 0xf0) != 0xA0) {
3940 if (!(status & BIT7) || (status & BIT4))
3941 info->icount.rxabort++;
3942 else if (status & BIT6)
3943 info->icount.rxover++;
3944 else if (!(status & BIT5)) {
3945 info->icount.rxcrc++;
3946 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
3952 struct net_device_stats *stats = hdlc_stats(info->netdev);
3954 stats->rx_frame_errors++;
3961 framesize = buf->count;
3963 if (debug_level >= DEBUG_LEVEL_BH)
3964 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3965 __FILE__,__LINE__,info->device_name,status,framesize);
3967 if (debug_level >= DEBUG_LEVEL_DATA)
3968 trace_block(info, buf->data, framesize, 0);
3971 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3972 framesize+1 > info->max_frame_size) ||
3973 framesize > info->max_frame_size)
3974 info->icount.rxlong++;
3977 info->icount.rxok++;
3979 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3980 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3986 hdlcdev_rx(info, buf->data, framesize);
3989 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3993 spin_lock_irqsave(&info->lock,flags);
3994 buf->status = buf->count = 0;
3995 info->rx_frame_count--;
3997 if (info->rx_get >= info->rx_buf_count)
3999 spin_unlock_irqrestore(&info->lock,flags);
4004 BOOLEAN register_test(MGSLPC_INFO *info)
4006 static unsigned char patterns[] =
4007 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
4008 static unsigned int count = sizeof(patterns) / sizeof(patterns[0]);
4011 unsigned long flags;
4013 spin_lock_irqsave(&info->lock,flags);
4016 for (i = 0; i < count; i++) {
4017 write_reg(info, XAD1, patterns[i]);
4018 write_reg(info, XAD2, patterns[(i + 1) % count]);
4019 if ((read_reg(info, XAD1) != patterns[i]) ||
4020 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
4026 spin_unlock_irqrestore(&info->lock,flags);
4030 BOOLEAN irq_test(MGSLPC_INFO *info)
4032 unsigned long end_time;
4033 unsigned long flags;
4035 spin_lock_irqsave(&info->lock,flags);
4038 info->testing_irq = TRUE;
4041 info->irq_occurred = FALSE;
4043 /* init hdlc mode */
4045 irq_enable(info, CHA, IRQ_TIMER);
4046 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
4047 issue_command(info, CHA, CMD_START_TIMER);
4049 spin_unlock_irqrestore(&info->lock,flags);
4052 while(end_time-- && !info->irq_occurred) {
4053 msleep_interruptible(10);
4056 info->testing_irq = FALSE;
4058 spin_lock_irqsave(&info->lock,flags);
4060 spin_unlock_irqrestore(&info->lock,flags);
4062 return info->irq_occurred ? TRUE : FALSE;
4065 int adapter_test(MGSLPC_INFO *info)
4067 if (!register_test(info)) {
4068 info->init_error = DiagStatus_AddressFailure;
4069 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
4070 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
4074 if (!irq_test(info)) {
4075 info->init_error = DiagStatus_IrqFailure;
4076 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
4077 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
4081 if (debug_level >= DEBUG_LEVEL_INFO)
4082 printk("%s(%d):device %s passed diagnostics\n",
4083 __FILE__,__LINE__,info->device_name);
4087 void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
4092 printk("%s tx data:\n",info->device_name);
4094 printk("%s rx data:\n",info->device_name);
4102 for(i=0;i<linecount;i++)
4103 printk("%02X ",(unsigned char)data[i]);
4106 for(i=0;i<linecount;i++) {
4107 if (data[i]>=040 && data[i]<=0176)
4108 printk("%c",data[i]);
4119 /* HDLC frame time out
4120 * update stats and do tx completion processing
4122 void tx_timeout(unsigned long context)
4124 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
4125 unsigned long flags;
4127 if ( debug_level >= DEBUG_LEVEL_INFO )
4128 printk( "%s(%d):tx_timeout(%s)\n",
4129 __FILE__,__LINE__,info->device_name);
4130 if(info->tx_active &&
4131 info->params.mode == MGSL_MODE_HDLC) {
4132 info->icount.txtimeout++;
4134 spin_lock_irqsave(&info->lock,flags);
4135 info->tx_active = 0;
4136 info->tx_count = info->tx_put = info->tx_get = 0;
4138 spin_unlock_irqrestore(&info->lock,flags);
4142 hdlcdev_tx_done(info);
4151 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
4152 * set encoding and frame check sequence (FCS) options
4154 * dev pointer to network device structure
4155 * encoding serial encoding setting
4156 * parity FCS setting
4158 * returns 0 if success, otherwise error code
4160 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
4161 unsigned short parity)
4163 MGSLPC_INFO *info = dev_to_port(dev);
4164 unsigned char new_encoding;
4165 unsigned short new_crctype;
4167 /* return error if TTY interface open */
4173 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
4174 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
4175 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
4176 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
4177 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
4178 default: return -EINVAL;
4183 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
4184 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
4185 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
4186 default: return -EINVAL;
4189 info->params.encoding = new_encoding;
4190 info->params.crc_type = new_crctype;;
4192 /* if network interface up, reprogram hardware */
4194 mgslpc_program_hw(info);
4200 * called by generic HDLC layer to send frame
4202 * skb socket buffer containing HDLC frame
4203 * dev pointer to network device structure
4205 * returns 0 if success, otherwise error code
4207 static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
4209 MGSLPC_INFO *info = dev_to_port(dev);
4210 struct net_device_stats *stats = hdlc_stats(dev);
4211 unsigned long flags;
4213 if (debug_level >= DEBUG_LEVEL_INFO)
4214 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
4216 /* stop sending until this frame completes */
4217 netif_stop_queue(dev);
4219 /* copy data to device buffers */
4220 memcpy(info->tx_buf, skb->data, skb->len);
4222 info->tx_put = info->tx_count = skb->len;
4224 /* update network statistics */
4225 stats->tx_packets++;
4226 stats->tx_bytes += skb->len;
4228 /* done with socket buffer, so free it */
4231 /* save start time for transmit timeout detection */
4232 dev->trans_start = jiffies;
4234 /* start hardware transmitter if necessary */
4235 spin_lock_irqsave(&info->lock,flags);
4236 if (!info->tx_active)
4238 spin_unlock_irqrestore(&info->lock,flags);
4244 * called by network layer when interface enabled
4245 * claim resources and initialize hardware
4247 * dev pointer to network device structure
4249 * returns 0 if success, otherwise error code
4251 static int hdlcdev_open(struct net_device *dev)
4253 MGSLPC_INFO *info = dev_to_port(dev);
4255 unsigned long flags;
4257 if (debug_level >= DEBUG_LEVEL_INFO)
4258 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4260 /* generic HDLC layer open processing */
4261 if ((rc = hdlc_open(dev)))
4264 /* arbitrate between network and tty opens */
4265 spin_lock_irqsave(&info->netlock, flags);
4266 if (info->count != 0 || info->netcount != 0) {
4267 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4268 spin_unlock_irqrestore(&info->netlock, flags);
4272 spin_unlock_irqrestore(&info->netlock, flags);
4274 /* claim resources and init adapter */
4275 if ((rc = startup(info)) != 0) {
4276 spin_lock_irqsave(&info->netlock, flags);
4278 spin_unlock_irqrestore(&info->netlock, flags);
4282 /* assert DTR and RTS, apply hardware settings */
4283 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4284 mgslpc_program_hw(info);
4286 /* enable network layer transmit */
4287 dev->trans_start = jiffies;
4288 netif_start_queue(dev);
4290 /* inform generic HDLC layer of current DCD status */
4291 spin_lock_irqsave(&info->lock, flags);
4293 spin_unlock_irqrestore(&info->lock, flags);
4294 hdlc_set_carrier(info->serial_signals & SerialSignal_DCD, dev);
4300 * called by network layer when interface is disabled
4301 * shutdown hardware and release resources
4303 * dev pointer to network device structure
4305 * returns 0 if success, otherwise error code
4307 static int hdlcdev_close(struct net_device *dev)
4309 MGSLPC_INFO *info = dev_to_port(dev);
4310 unsigned long flags;
4312 if (debug_level >= DEBUG_LEVEL_INFO)
4313 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4315 netif_stop_queue(dev);
4317 /* shutdown adapter and release resources */
4322 spin_lock_irqsave(&info->netlock, flags);
4324 spin_unlock_irqrestore(&info->netlock, flags);
4330 * called by network layer to process IOCTL call to network device
4332 * dev pointer to network device structure
4333 * ifr pointer to network interface request structure
4334 * cmd IOCTL command code
4336 * returns 0 if success, otherwise error code
4338 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4340 const size_t size = sizeof(sync_serial_settings);
4341 sync_serial_settings new_line;
4342 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4343 MGSLPC_INFO *info = dev_to_port(dev);
4346 if (debug_level >= DEBUG_LEVEL_INFO)
4347 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4349 /* return error if TTY interface open */
4353 if (cmd != SIOCWANDEV)
4354 return hdlc_ioctl(dev, ifr, cmd);
4356 switch(ifr->ifr_settings.type) {
4357 case IF_GET_IFACE: /* return current sync_serial_settings */
4359 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4360 if (ifr->ifr_settings.size < size) {
4361 ifr->ifr_settings.size = size; /* data size wanted */
4365 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4366 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4367 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4368 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4371 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4372 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4373 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4374 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4375 default: new_line.clock_type = CLOCK_DEFAULT;
4378 new_line.clock_rate = info->params.clock_speed;
4379 new_line.loopback = info->params.loopback ? 1:0;
4381 if (copy_to_user(line, &new_line, size))
4385 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4387 if(!capable(CAP_NET_ADMIN))
4389 if (copy_from_user(&new_line, line, size))
4392 switch (new_line.clock_type)
4394 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4395 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4396 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4397 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4398 case CLOCK_DEFAULT: flags = info->params.flags &
4399 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4400 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4401 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4402 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4403 default: return -EINVAL;
4406 if (new_line.loopback != 0 && new_line.loopback != 1)
4409 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4410 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4411 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4412 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4413 info->params.flags |= flags;
4415 info->params.loopback = new_line.loopback;
4417 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4418 info->params.clock_speed = new_line.clock_rate;
4420 info->params.clock_speed = 0;
4422 /* if network interface up, reprogram hardware */
4424 mgslpc_program_hw(info);
4428 return hdlc_ioctl(dev, ifr, cmd);
4433 * called by network layer when transmit timeout is detected
4435 * dev pointer to network device structure
4437 static void hdlcdev_tx_timeout(struct net_device *dev)
4439 MGSLPC_INFO *info = dev_to_port(dev);
4440 struct net_device_stats *stats = hdlc_stats(dev);
4441 unsigned long flags;
4443 if (debug_level >= DEBUG_LEVEL_INFO)
4444 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4447 stats->tx_aborted_errors++;
4449 spin_lock_irqsave(&info->lock,flags);
4451 spin_unlock_irqrestore(&info->lock,flags);
4453 netif_wake_queue(dev);
4457 * called by device driver when transmit completes
4458 * reenable network layer transmit if stopped
4460 * info pointer to device instance information
4462 static void hdlcdev_tx_done(MGSLPC_INFO *info)
4464 if (netif_queue_stopped(info->netdev))
4465 netif_wake_queue(info->netdev);
4469 * called by device driver when frame received
4470 * pass frame to network layer
4472 * info pointer to device instance information
4473 * buf pointer to buffer contianing frame data
4474 * size count of data bytes in buf
4476 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4478 struct sk_buff *skb = dev_alloc_skb(size);
4479 struct net_device *dev = info->netdev;
4480 struct net_device_stats *stats = hdlc_stats(dev);
4482 if (debug_level >= DEBUG_LEVEL_INFO)
4483 printk("hdlcdev_rx(%s)\n",dev->name);
4486 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
4487 stats->rx_dropped++;
4491 memcpy(skb_put(skb, size),buf,size);
4493 skb->protocol = hdlc_type_trans(skb, info->netdev);
4495 stats->rx_packets++;
4496 stats->rx_bytes += size;
4500 info->netdev->last_rx = jiffies;
4504 * called by device driver when adding device instance
4505 * do generic HDLC initialization
4507 * info pointer to device instance information
4509 * returns 0 if success, otherwise error code
4511 static int hdlcdev_init(MGSLPC_INFO *info)
4514 struct net_device *dev;
4517 /* allocate and initialize network and HDLC layer objects */
4519 if (!(dev = alloc_hdlcdev(info))) {
4520 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4524 /* for network layer reporting purposes only */
4525 dev->base_addr = info->io_base;
4526 dev->irq = info->irq_level;
4528 /* network layer callbacks and settings */
4529 dev->do_ioctl = hdlcdev_ioctl;
4530 dev->open = hdlcdev_open;
4531 dev->stop = hdlcdev_close;
4532 dev->tx_timeout = hdlcdev_tx_timeout;
4533 dev->watchdog_timeo = 10*HZ;
4534 dev->tx_queue_len = 50;
4536 /* generic HDLC layer callbacks and settings */
4537 hdlc = dev_to_hdlc(dev);
4538 hdlc->attach = hdlcdev_attach;
4539 hdlc->xmit = hdlcdev_xmit;
4541 /* register objects with HDLC layer */
4542 if ((rc = register_hdlc_device(dev))) {
4543 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4553 * called by device driver when removing device instance
4554 * do generic HDLC cleanup
4556 * info pointer to device instance information
4558 static void hdlcdev_exit(MGSLPC_INFO *info)
4560 unregister_hdlc_device(info->netdev);
4561 free_netdev(info->netdev);
4562 info->netdev = NULL;
4565 #endif /* CONFIG_HDLC */