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;
231 struct pcmcia_device *p_dev;
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)
539 dev_link_t *link = dev_to_instance(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;
579 link->conf.IntType = INT_MEMORY_AND_IO;
581 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
584 mgslpc_add_device(info);
589 /* Card has been inserted.
592 #define CS_CHECK(fn, ret) \
593 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
595 static void mgslpc_config(dev_link_t *link)
597 client_handle_t handle = link->handle;
598 MGSLPC_INFO *info = link->priv;
601 int last_fn, last_ret;
603 cistpl_cftable_entry_t dflt = { 0 };
604 cistpl_cftable_entry_t *cfg;
606 if (debug_level >= DEBUG_LEVEL_INFO)
607 printk("mgslpc_config(0x%p)\n", link);
609 /* read CONFIG tuple to find its configuration registers */
610 tuple.DesiredTuple = CISTPL_CONFIG;
611 tuple.Attributes = 0;
612 tuple.TupleData = buf;
613 tuple.TupleDataMax = sizeof(buf);
614 tuple.TupleOffset = 0;
615 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
616 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
617 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
618 link->conf.ConfigBase = parse.config.base;
619 link->conf.Present = parse.config.rmask[0];
622 link->state |= DEV_CONFIG;
624 /* get CIS configuration entry */
626 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
627 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
629 cfg = &(parse.cftable_entry);
630 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
631 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
633 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
637 link->conf.ConfigIndex = cfg->index;
638 link->conf.Attributes |= CONF_ENABLE_IRQ;
640 /* IO window settings */
641 link->io.NumPorts1 = 0;
642 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
643 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
644 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
645 if (!(io->flags & CISTPL_IO_8BIT))
646 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
647 if (!(io->flags & CISTPL_IO_16BIT))
648 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
649 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
650 link->io.BasePort1 = io->win[0].base;
651 link->io.NumPorts1 = io->win[0].len;
652 CS_CHECK(RequestIO, pcmcia_request_io(link->handle, &link->io));
655 link->conf.Attributes = CONF_ENABLE_IRQ;
656 link->conf.IntType = INT_MEMORY_AND_IO;
657 link->conf.ConfigIndex = 8;
658 link->conf.Present = PRESENT_OPTION;
660 link->irq.Attributes |= IRQ_HANDLE_PRESENT;
661 link->irq.Handler = mgslpc_isr;
662 link->irq.Instance = info;
663 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
665 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
667 info->io_base = link->io.BasePort1;
668 info->irq_level = link->irq.AssignedIRQ;
670 /* add to linked list of devices */
671 sprintf(info->node.dev_name, "mgslpc0");
672 info->node.major = info->node.minor = 0;
673 link->dev_node = &info->node;
675 printk(KERN_INFO "%s: index 0x%02x:",
676 info->node.dev_name, link->conf.ConfigIndex);
677 if (link->conf.Attributes & CONF_ENABLE_IRQ)
678 printk(", irq %d", link->irq.AssignedIRQ);
679 if (link->io.NumPorts1)
680 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
681 link->io.BasePort1+link->io.NumPorts1-1);
684 link->state &= ~DEV_CONFIG_PENDING;
688 cs_error(link->handle, last_fn, last_ret);
689 mgslpc_release((u_long)link);
692 /* Card has been removed.
693 * Unregister device and release PCMCIA configuration.
694 * If device is open, postpone until it is closed.
696 static void mgslpc_release(u_long arg)
698 dev_link_t *link = (dev_link_t *)arg;
700 if (debug_level >= DEBUG_LEVEL_INFO)
701 printk("mgslpc_release(0x%p)\n", link);
703 pcmcia_disable_device(link->handle);
706 static void mgslpc_detach(struct pcmcia_device *p_dev)
708 dev_link_t *link = dev_to_instance(p_dev);
710 if (debug_level >= DEBUG_LEVEL_INFO)
711 printk("mgslpc_detach(0x%p)\n", link);
713 if (link->state & DEV_CONFIG) {
714 ((MGSLPC_INFO *)link->priv)->stop = 1;
715 mgslpc_release((u_long)link);
718 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
721 static int mgslpc_suspend(struct pcmcia_device *dev)
723 dev_link_t *link = dev_to_instance(dev);
724 MGSLPC_INFO *info = link->priv;
731 static int mgslpc_resume(struct pcmcia_device *dev)
733 dev_link_t *link = dev_to_instance(dev);
734 MGSLPC_INFO *info = link->priv;
742 static inline int mgslpc_paranoia_check(MGSLPC_INFO *info,
743 char *name, const char *routine)
745 #ifdef MGSLPC_PARANOIA_CHECK
746 static const char *badmagic =
747 "Warning: bad magic number for mgsl struct (%s) in %s\n";
748 static const char *badinfo =
749 "Warning: null mgslpc_info for (%s) in %s\n";
752 printk(badinfo, name, routine);
755 if (info->magic != MGSLPC_MAGIC) {
756 printk(badmagic, name, routine);
767 #define CMD_RXFIFO BIT7 // release current rx FIFO
768 #define CMD_RXRESET BIT6 // receiver reset
769 #define CMD_RXFIFO_READ BIT5
770 #define CMD_START_TIMER BIT4
771 #define CMD_TXFIFO BIT3 // release current tx FIFO
772 #define CMD_TXEOM BIT1 // transmit end message
773 #define CMD_TXRESET BIT0 // transmit reset
775 static BOOLEAN wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
778 /* wait for command completion */
779 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
787 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
789 wait_command_complete(info, channel);
790 write_reg(info, (unsigned char) (channel + CMDR), cmd);
793 static void tx_pause(struct tty_struct *tty)
795 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
798 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
800 if (debug_level >= DEBUG_LEVEL_INFO)
801 printk("tx_pause(%s)\n",info->device_name);
803 spin_lock_irqsave(&info->lock,flags);
804 if (info->tx_enabled)
806 spin_unlock_irqrestore(&info->lock,flags);
809 static void tx_release(struct tty_struct *tty)
811 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
814 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
816 if (debug_level >= DEBUG_LEVEL_INFO)
817 printk("tx_release(%s)\n",info->device_name);
819 spin_lock_irqsave(&info->lock,flags);
820 if (!info->tx_enabled)
822 spin_unlock_irqrestore(&info->lock,flags);
825 /* Return next bottom half action to perform.
826 * or 0 if nothing to do.
828 static int bh_action(MGSLPC_INFO *info)
833 spin_lock_irqsave(&info->lock,flags);
835 if (info->pending_bh & BH_RECEIVE) {
836 info->pending_bh &= ~BH_RECEIVE;
838 } else if (info->pending_bh & BH_TRANSMIT) {
839 info->pending_bh &= ~BH_TRANSMIT;
841 } else if (info->pending_bh & BH_STATUS) {
842 info->pending_bh &= ~BH_STATUS;
847 /* Mark BH routine as complete */
848 info->bh_running = 0;
849 info->bh_requested = 0;
852 spin_unlock_irqrestore(&info->lock,flags);
857 void bh_handler(void* Context)
859 MGSLPC_INFO *info = (MGSLPC_INFO*)Context;
865 if (debug_level >= DEBUG_LEVEL_BH)
866 printk( "%s(%d):bh_handler(%s) entry\n",
867 __FILE__,__LINE__,info->device_name);
869 info->bh_running = 1;
871 while((action = bh_action(info)) != 0) {
873 /* Process work item */
874 if ( debug_level >= DEBUG_LEVEL_BH )
875 printk( "%s(%d):bh_handler() work item action=%d\n",
876 __FILE__,__LINE__,action);
881 while(rx_get_frame(info));
890 /* unknown work item ID */
891 printk("Unknown work item ID=%08X!\n", action);
896 if (debug_level >= DEBUG_LEVEL_BH)
897 printk( "%s(%d):bh_handler(%s) exit\n",
898 __FILE__,__LINE__,info->device_name);
901 void bh_transmit(MGSLPC_INFO *info)
903 struct tty_struct *tty = info->tty;
904 if (debug_level >= DEBUG_LEVEL_BH)
905 printk("bh_transmit() entry on %s\n", info->device_name);
909 wake_up_interruptible(&tty->write_wait);
913 void bh_status(MGSLPC_INFO *info)
915 info->ri_chkcount = 0;
916 info->dsr_chkcount = 0;
917 info->dcd_chkcount = 0;
918 info->cts_chkcount = 0;
921 /* eom: non-zero = end of frame */
922 static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
924 unsigned char data[2];
925 unsigned char fifo_count, read_count, i;
926 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
928 if (debug_level >= DEBUG_LEVEL_ISR)
929 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
931 if (!info->rx_enabled)
934 if (info->rx_frame_count >= info->rx_buf_count) {
935 /* no more free buffers */
936 issue_command(info, CHA, CMD_RXRESET);
937 info->pending_bh |= BH_RECEIVE;
938 info->rx_overflow = 1;
939 info->icount.buf_overrun++;
944 /* end of frame, get FIFO count from RBCL register */
945 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
951 if (fifo_count == 1) {
953 data[0] = read_reg(info, CHA + RXFIFO);
956 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
958 fifo_count -= read_count;
959 if (!fifo_count && eom)
960 buf->status = data[--read_count];
962 for (i = 0; i < read_count; i++) {
963 if (buf->count >= info->max_frame_size) {
964 /* frame too large, reset receiver and reset current buffer */
965 issue_command(info, CHA, CMD_RXRESET);
969 *(buf->data + buf->count) = data[i];
972 } while (fifo_count);
975 info->pending_bh |= BH_RECEIVE;
976 info->rx_frame_count++;
978 if (info->rx_put >= info->rx_buf_count)
981 issue_command(info, CHA, CMD_RXFIFO);
984 static void rx_ready_async(MGSLPC_INFO *info, int tcd)
986 unsigned char data, status, flag;
989 struct tty_struct *tty = info->tty;
990 struct mgsl_icount *icount = &info->icount;
993 /* early termination, get FIFO count from RBCL register */
994 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
996 /* Zero fifo count could mean 0 or 32 bytes available.
997 * If BIT5 of STAR is set then at least 1 byte is available.
999 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
1004 tty_buffer_request_room(tty, fifo_count);
1005 /* Flush received async data to receive data buffer. */
1006 while (fifo_count) {
1007 data = read_reg(info, CHA + RXFIFO);
1008 status = read_reg(info, CHA + RXFIFO);
1014 // if no frameing/crc error then save data
1015 // BIT7:parity error
1016 // BIT6:framing error
1018 if (status & (BIT7 + BIT6)) {
1024 /* discard char if tty control flags say so */
1025 if (status & info->ignore_status_mask)
1028 status &= info->read_status_mask;
1032 else if (status & BIT6)
1035 work += tty_insert_flip_char(tty, data, flag);
1037 issue_command(info, CHA, CMD_RXFIFO);
1039 if (debug_level >= DEBUG_LEVEL_ISR) {
1040 printk("%s(%d):rx_ready_async",
1042 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1043 __FILE__,__LINE__,icount->rx,icount->brk,
1044 icount->parity,icount->frame,icount->overrun);
1048 tty_flip_buffer_push(tty);
1052 static void tx_done(MGSLPC_INFO *info)
1054 if (!info->tx_active)
1057 info->tx_active = 0;
1058 info->tx_aborting = 0;
1060 if (info->params.mode == MGSL_MODE_ASYNC)
1063 info->tx_count = info->tx_put = info->tx_get = 0;
1064 del_timer(&info->tx_timer);
1066 if (info->drop_rts_on_tx_done) {
1068 if (info->serial_signals & SerialSignal_RTS) {
1069 info->serial_signals &= ~SerialSignal_RTS;
1072 info->drop_rts_on_tx_done = 0;
1077 hdlcdev_tx_done(info);
1081 if (info->tty->stopped || info->tty->hw_stopped) {
1085 info->pending_bh |= BH_TRANSMIT;
1089 static void tx_ready(MGSLPC_INFO *info)
1091 unsigned char fifo_count = 32;
1094 if (debug_level >= DEBUG_LEVEL_ISR)
1095 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1097 if (info->params.mode == MGSL_MODE_HDLC) {
1098 if (!info->tx_active)
1101 if (info->tty->stopped || info->tty->hw_stopped) {
1105 if (!info->tx_count)
1106 info->tx_active = 0;
1109 if (!info->tx_count)
1112 while (info->tx_count && fifo_count) {
1113 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
1116 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1118 write_reg16(info, CHA + TXFIFO,
1119 *((unsigned short*)(info->tx_buf + info->tx_get)));
1121 info->tx_count -= c;
1122 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1126 if (info->params.mode == MGSL_MODE_ASYNC) {
1127 if (info->tx_count < WAKEUP_CHARS)
1128 info->pending_bh |= BH_TRANSMIT;
1129 issue_command(info, CHA, CMD_TXFIFO);
1132 issue_command(info, CHA, CMD_TXFIFO);
1134 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1138 static void cts_change(MGSLPC_INFO *info)
1141 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1142 irq_disable(info, CHB, IRQ_CTS);
1144 if (info->serial_signals & SerialSignal_CTS)
1145 info->input_signal_events.cts_up++;
1147 info->input_signal_events.cts_down++;
1148 wake_up_interruptible(&info->status_event_wait_q);
1149 wake_up_interruptible(&info->event_wait_q);
1151 if (info->flags & ASYNC_CTS_FLOW) {
1152 if (info->tty->hw_stopped) {
1153 if (info->serial_signals & SerialSignal_CTS) {
1154 if (debug_level >= DEBUG_LEVEL_ISR)
1155 printk("CTS tx start...");
1157 info->tty->hw_stopped = 0;
1159 info->pending_bh |= BH_TRANSMIT;
1163 if (!(info->serial_signals & SerialSignal_CTS)) {
1164 if (debug_level >= DEBUG_LEVEL_ISR)
1165 printk("CTS tx stop...");
1167 info->tty->hw_stopped = 1;
1172 info->pending_bh |= BH_STATUS;
1175 static void dcd_change(MGSLPC_INFO *info)
1178 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1179 irq_disable(info, CHB, IRQ_DCD);
1181 if (info->serial_signals & SerialSignal_DCD) {
1182 info->input_signal_events.dcd_up++;
1185 info->input_signal_events.dcd_down++;
1188 hdlc_set_carrier(info->serial_signals & SerialSignal_DCD, info->netdev);
1190 wake_up_interruptible(&info->status_event_wait_q);
1191 wake_up_interruptible(&info->event_wait_q);
1193 if (info->flags & ASYNC_CHECK_CD) {
1194 if (debug_level >= DEBUG_LEVEL_ISR)
1195 printk("%s CD now %s...", info->device_name,
1196 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1197 if (info->serial_signals & SerialSignal_DCD)
1198 wake_up_interruptible(&info->open_wait);
1200 if (debug_level >= DEBUG_LEVEL_ISR)
1201 printk("doing serial hangup...");
1203 tty_hangup(info->tty);
1206 info->pending_bh |= BH_STATUS;
1209 static void dsr_change(MGSLPC_INFO *info)
1212 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1213 port_irq_disable(info, PVR_DSR);
1215 if (info->serial_signals & SerialSignal_DSR)
1216 info->input_signal_events.dsr_up++;
1218 info->input_signal_events.dsr_down++;
1219 wake_up_interruptible(&info->status_event_wait_q);
1220 wake_up_interruptible(&info->event_wait_q);
1221 info->pending_bh |= BH_STATUS;
1224 static void ri_change(MGSLPC_INFO *info)
1227 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1228 port_irq_disable(info, PVR_RI);
1230 if (info->serial_signals & SerialSignal_RI)
1231 info->input_signal_events.ri_up++;
1233 info->input_signal_events.ri_down++;
1234 wake_up_interruptible(&info->status_event_wait_q);
1235 wake_up_interruptible(&info->event_wait_q);
1236 info->pending_bh |= BH_STATUS;
1239 /* Interrupt service routine entry point.
1243 * irq interrupt number that caused interrupt
1244 * dev_id device ID supplied during interrupt registration
1245 * regs interrupted processor context
1247 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs)
1249 MGSLPC_INFO * info = (MGSLPC_INFO *)dev_id;
1251 unsigned char gis, pis;
1254 if (debug_level >= DEBUG_LEVEL_ISR)
1255 printk("mgslpc_isr(%d) entry.\n", irq);
1259 if (!(info->p_dev->state & DEV_CONFIG))
1262 spin_lock(&info->lock);
1264 while ((gis = read_reg(info, CHA + GIS))) {
1265 if (debug_level >= DEBUG_LEVEL_ISR)
1266 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1268 if ((gis & 0x70) || count > 1000) {
1269 printk("synclink_cs:hardware failed or ejected\n");
1274 if (gis & (BIT1 + BIT0)) {
1275 isr = read_reg16(info, CHB + ISR);
1281 if (gis & (BIT3 + BIT2))
1283 isr = read_reg16(info, CHA + ISR);
1284 if (isr & IRQ_TIMER) {
1285 info->irq_occurred = 1;
1286 irq_disable(info, CHA, IRQ_TIMER);
1290 if (isr & IRQ_EXITHUNT) {
1291 info->icount.exithunt++;
1292 wake_up_interruptible(&info->event_wait_q);
1294 if (isr & IRQ_BREAK_ON) {
1296 if (info->flags & ASYNC_SAK)
1299 if (isr & IRQ_RXTIME) {
1300 issue_command(info, CHA, CMD_RXFIFO_READ);
1302 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1303 if (info->params.mode == MGSL_MODE_HDLC)
1304 rx_ready_hdlc(info, isr & IRQ_RXEOM);
1306 rx_ready_async(info, isr & IRQ_RXEOM);
1310 if (isr & IRQ_UNDERRUN) {
1311 if (info->tx_aborting)
1312 info->icount.txabort++;
1314 info->icount.txunder++;
1317 else if (isr & IRQ_ALLSENT) {
1318 info->icount.txok++;
1321 else if (isr & IRQ_TXFIFO)
1325 pis = read_reg(info, CHA + PIS);
1333 /* Request bottom half processing if there's something
1334 * for it to do and the bh is not already running
1337 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1338 if ( debug_level >= DEBUG_LEVEL_ISR )
1339 printk("%s(%d):%s queueing bh task.\n",
1340 __FILE__,__LINE__,info->device_name);
1341 schedule_work(&info->task);
1342 info->bh_requested = 1;
1345 spin_unlock(&info->lock);
1347 if (debug_level >= DEBUG_LEVEL_ISR)
1348 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1349 __FILE__,__LINE__,irq);
1354 /* Initialize and start device.
1356 static int startup(MGSLPC_INFO * info)
1360 if (debug_level >= DEBUG_LEVEL_INFO)
1361 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1363 if (info->flags & ASYNC_INITIALIZED)
1366 if (!info->tx_buf) {
1367 /* allocate a page of memory for a transmit buffer */
1368 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1369 if (!info->tx_buf) {
1370 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1371 __FILE__,__LINE__,info->device_name);
1376 info->pending_bh = 0;
1378 memset(&info->icount, 0, sizeof(info->icount));
1380 init_timer(&info->tx_timer);
1381 info->tx_timer.data = (unsigned long)info;
1382 info->tx_timer.function = tx_timeout;
1384 /* Allocate and claim adapter resources */
1385 retval = claim_resources(info);
1387 /* perform existance check and diagnostics */
1389 retval = adapter_test(info);
1392 if (capable(CAP_SYS_ADMIN) && info->tty)
1393 set_bit(TTY_IO_ERROR, &info->tty->flags);
1394 release_resources(info);
1398 /* program hardware for current parameters */
1399 mgslpc_change_params(info);
1402 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1404 info->flags |= ASYNC_INITIALIZED;
1409 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1411 static void shutdown(MGSLPC_INFO * info)
1413 unsigned long flags;
1415 if (!(info->flags & ASYNC_INITIALIZED))
1418 if (debug_level >= DEBUG_LEVEL_INFO)
1419 printk("%s(%d):mgslpc_shutdown(%s)\n",
1420 __FILE__,__LINE__, info->device_name );
1422 /* clear status wait queue because status changes */
1423 /* can't happen after shutting down the hardware */
1424 wake_up_interruptible(&info->status_event_wait_q);
1425 wake_up_interruptible(&info->event_wait_q);
1427 del_timer(&info->tx_timer);
1430 free_page((unsigned long) info->tx_buf);
1431 info->tx_buf = NULL;
1434 spin_lock_irqsave(&info->lock,flags);
1439 /* TODO:disable interrupts instead of reset to preserve signal states */
1442 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1443 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1447 spin_unlock_irqrestore(&info->lock,flags);
1449 release_resources(info);
1452 set_bit(TTY_IO_ERROR, &info->tty->flags);
1454 info->flags &= ~ASYNC_INITIALIZED;
1457 static void mgslpc_program_hw(MGSLPC_INFO *info)
1459 unsigned long flags;
1461 spin_lock_irqsave(&info->lock,flags);
1465 info->tx_count = info->tx_put = info->tx_get = 0;
1467 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1474 info->dcd_chkcount = 0;
1475 info->cts_chkcount = 0;
1476 info->ri_chkcount = 0;
1477 info->dsr_chkcount = 0;
1479 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1480 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1483 if (info->netcount || info->tty->termios->c_cflag & CREAD)
1486 spin_unlock_irqrestore(&info->lock,flags);
1489 /* Reconfigure adapter based on new parameters
1491 static void mgslpc_change_params(MGSLPC_INFO *info)
1496 if (!info->tty || !info->tty->termios)
1499 if (debug_level >= DEBUG_LEVEL_INFO)
1500 printk("%s(%d):mgslpc_change_params(%s)\n",
1501 __FILE__,__LINE__, info->device_name );
1503 cflag = info->tty->termios->c_cflag;
1505 /* if B0 rate (hangup) specified then negate DTR and RTS */
1506 /* otherwise assert DTR and RTS */
1508 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1510 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1512 /* byte size and parity */
1514 switch (cflag & CSIZE) {
1515 case CS5: info->params.data_bits = 5; break;
1516 case CS6: info->params.data_bits = 6; break;
1517 case CS7: info->params.data_bits = 7; break;
1518 case CS8: info->params.data_bits = 8; break;
1519 default: info->params.data_bits = 7; break;
1523 info->params.stop_bits = 2;
1525 info->params.stop_bits = 1;
1527 info->params.parity = ASYNC_PARITY_NONE;
1528 if (cflag & PARENB) {
1530 info->params.parity = ASYNC_PARITY_ODD;
1532 info->params.parity = ASYNC_PARITY_EVEN;
1535 info->params.parity = ASYNC_PARITY_SPACE;
1539 /* calculate number of jiffies to transmit a full
1540 * FIFO (32 bytes) at specified data rate
1542 bits_per_char = info->params.data_bits +
1543 info->params.stop_bits + 1;
1545 /* if port data rate is set to 460800 or less then
1546 * allow tty settings to override, otherwise keep the
1547 * current data rate.
1549 if (info->params.data_rate <= 460800) {
1550 info->params.data_rate = tty_get_baud_rate(info->tty);
1553 if ( info->params.data_rate ) {
1554 info->timeout = (32*HZ*bits_per_char) /
1555 info->params.data_rate;
1557 info->timeout += HZ/50; /* Add .02 seconds of slop */
1559 if (cflag & CRTSCTS)
1560 info->flags |= ASYNC_CTS_FLOW;
1562 info->flags &= ~ASYNC_CTS_FLOW;
1565 info->flags &= ~ASYNC_CHECK_CD;
1567 info->flags |= ASYNC_CHECK_CD;
1569 /* process tty input control flags */
1571 info->read_status_mask = 0;
1572 if (I_INPCK(info->tty))
1573 info->read_status_mask |= BIT7 | BIT6;
1574 if (I_IGNPAR(info->tty))
1575 info->ignore_status_mask |= BIT7 | BIT6;
1577 mgslpc_program_hw(info);
1580 /* Add a character to the transmit buffer
1582 static void mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1584 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1585 unsigned long flags;
1587 if (debug_level >= DEBUG_LEVEL_INFO) {
1588 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1589 __FILE__,__LINE__,ch,info->device_name);
1592 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1595 if (!tty || !info->tx_buf)
1598 spin_lock_irqsave(&info->lock,flags);
1600 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1601 if (info->tx_count < TXBUFSIZE - 1) {
1602 info->tx_buf[info->tx_put++] = ch;
1603 info->tx_put &= TXBUFSIZE-1;
1608 spin_unlock_irqrestore(&info->lock,flags);
1611 /* Enable transmitter so remaining characters in the
1612 * transmit buffer are sent.
1614 static void mgslpc_flush_chars(struct tty_struct *tty)
1616 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1617 unsigned long flags;
1619 if (debug_level >= DEBUG_LEVEL_INFO)
1620 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1621 __FILE__,__LINE__,info->device_name,info->tx_count);
1623 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1626 if (info->tx_count <= 0 || tty->stopped ||
1627 tty->hw_stopped || !info->tx_buf)
1630 if (debug_level >= DEBUG_LEVEL_INFO)
1631 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1632 __FILE__,__LINE__,info->device_name);
1634 spin_lock_irqsave(&info->lock,flags);
1635 if (!info->tx_active)
1637 spin_unlock_irqrestore(&info->lock,flags);
1640 /* Send a block of data
1644 * tty pointer to tty information structure
1645 * buf pointer to buffer containing send data
1646 * count size of send data in bytes
1648 * Returns: number of characters written
1650 static int mgslpc_write(struct tty_struct * tty,
1651 const unsigned char *buf, int count)
1654 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1655 unsigned long flags;
1657 if (debug_level >= DEBUG_LEVEL_INFO)
1658 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1659 __FILE__,__LINE__,info->device_name,count);
1661 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1662 !tty || !info->tx_buf)
1665 if (info->params.mode == MGSL_MODE_HDLC) {
1666 if (count > TXBUFSIZE) {
1670 if (info->tx_active)
1672 else if (info->tx_count)
1678 min(TXBUFSIZE - info->tx_count - 1,
1679 TXBUFSIZE - info->tx_put));
1683 memcpy(info->tx_buf + info->tx_put, buf, c);
1685 spin_lock_irqsave(&info->lock,flags);
1686 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1687 info->tx_count += c;
1688 spin_unlock_irqrestore(&info->lock,flags);
1695 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1696 spin_lock_irqsave(&info->lock,flags);
1697 if (!info->tx_active)
1699 spin_unlock_irqrestore(&info->lock,flags);
1702 if (debug_level >= DEBUG_LEVEL_INFO)
1703 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1704 __FILE__,__LINE__,info->device_name,ret);
1708 /* Return the count of free bytes in transmit buffer
1710 static int mgslpc_write_room(struct tty_struct *tty)
1712 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1715 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1718 if (info->params.mode == MGSL_MODE_HDLC) {
1719 /* HDLC (frame oriented) mode */
1720 if (info->tx_active)
1723 return HDLC_MAX_FRAME_SIZE;
1725 ret = TXBUFSIZE - info->tx_count - 1;
1730 if (debug_level >= DEBUG_LEVEL_INFO)
1731 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1732 __FILE__,__LINE__, info->device_name, ret);
1736 /* Return the count of bytes in transmit buffer
1738 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1740 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1743 if (debug_level >= DEBUG_LEVEL_INFO)
1744 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1745 __FILE__,__LINE__, info->device_name );
1747 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1750 if (info->params.mode == MGSL_MODE_HDLC)
1751 rc = info->tx_active ? info->max_frame_size : 0;
1753 rc = info->tx_count;
1755 if (debug_level >= DEBUG_LEVEL_INFO)
1756 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1757 __FILE__,__LINE__, info->device_name, rc);
1762 /* Discard all data in the send buffer
1764 static void mgslpc_flush_buffer(struct tty_struct *tty)
1766 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1767 unsigned long flags;
1769 if (debug_level >= DEBUG_LEVEL_INFO)
1770 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1771 __FILE__,__LINE__, info->device_name );
1773 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1776 spin_lock_irqsave(&info->lock,flags);
1777 info->tx_count = info->tx_put = info->tx_get = 0;
1778 del_timer(&info->tx_timer);
1779 spin_unlock_irqrestore(&info->lock,flags);
1781 wake_up_interruptible(&tty->write_wait);
1785 /* Send a high-priority XON/XOFF character
1787 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1789 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1790 unsigned long flags;
1792 if (debug_level >= DEBUG_LEVEL_INFO)
1793 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1794 __FILE__,__LINE__, info->device_name, ch );
1796 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1801 spin_lock_irqsave(&info->lock,flags);
1802 if (!info->tx_enabled)
1804 spin_unlock_irqrestore(&info->lock,flags);
1808 /* Signal remote device to throttle send data (our receive data)
1810 static void mgslpc_throttle(struct tty_struct * tty)
1812 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1813 unsigned long flags;
1815 if (debug_level >= DEBUG_LEVEL_INFO)
1816 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1817 __FILE__,__LINE__, info->device_name );
1819 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1823 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1825 if (tty->termios->c_cflag & CRTSCTS) {
1826 spin_lock_irqsave(&info->lock,flags);
1827 info->serial_signals &= ~SerialSignal_RTS;
1829 spin_unlock_irqrestore(&info->lock,flags);
1833 /* Signal remote device to stop throttling send data (our receive data)
1835 static void mgslpc_unthrottle(struct tty_struct * tty)
1837 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1838 unsigned long flags;
1840 if (debug_level >= DEBUG_LEVEL_INFO)
1841 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1842 __FILE__,__LINE__, info->device_name );
1844 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1851 mgslpc_send_xchar(tty, START_CHAR(tty));
1854 if (tty->termios->c_cflag & CRTSCTS) {
1855 spin_lock_irqsave(&info->lock,flags);
1856 info->serial_signals |= SerialSignal_RTS;
1858 spin_unlock_irqrestore(&info->lock,flags);
1862 /* get the current serial statistics
1864 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1867 if (debug_level >= DEBUG_LEVEL_INFO)
1868 printk("get_params(%s)\n", info->device_name);
1870 memset(&info->icount, 0, sizeof(info->icount));
1872 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1879 /* get the current serial parameters
1881 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1884 if (debug_level >= DEBUG_LEVEL_INFO)
1885 printk("get_params(%s)\n", info->device_name);
1886 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1892 /* set the serial parameters
1896 * info pointer to device instance data
1897 * new_params user buffer containing new serial params
1899 * Returns: 0 if success, otherwise error code
1901 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params)
1903 unsigned long flags;
1904 MGSL_PARAMS tmp_params;
1907 if (debug_level >= DEBUG_LEVEL_INFO)
1908 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1909 info->device_name );
1910 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1912 if ( debug_level >= DEBUG_LEVEL_INFO )
1913 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1914 __FILE__,__LINE__,info->device_name);
1918 spin_lock_irqsave(&info->lock,flags);
1919 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1920 spin_unlock_irqrestore(&info->lock,flags);
1922 mgslpc_change_params(info);
1927 static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1930 if (debug_level >= DEBUG_LEVEL_INFO)
1931 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1932 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1938 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1940 unsigned long flags;
1941 if (debug_level >= DEBUG_LEVEL_INFO)
1942 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1943 spin_lock_irqsave(&info->lock,flags);
1944 info->idle_mode = idle_mode;
1946 spin_unlock_irqrestore(&info->lock,flags);
1950 static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1953 if (debug_level >= DEBUG_LEVEL_INFO)
1954 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1955 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1961 static int set_interface(MGSLPC_INFO * info, int if_mode)
1963 unsigned long flags;
1965 if (debug_level >= DEBUG_LEVEL_INFO)
1966 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1967 spin_lock_irqsave(&info->lock,flags);
1968 info->if_mode = if_mode;
1970 val = read_reg(info, PVR) & 0x0f;
1971 switch (info->if_mode)
1973 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
1974 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
1975 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
1977 write_reg(info, PVR, val);
1979 spin_unlock_irqrestore(&info->lock,flags);
1983 static int set_txenable(MGSLPC_INFO * info, int enable)
1985 unsigned long flags;
1987 if (debug_level >= DEBUG_LEVEL_INFO)
1988 printk("set_txenable(%s,%d)\n", info->device_name, enable);
1990 spin_lock_irqsave(&info->lock,flags);
1992 if (!info->tx_enabled)
1995 if (info->tx_enabled)
1998 spin_unlock_irqrestore(&info->lock,flags);
2002 static int tx_abort(MGSLPC_INFO * info)
2004 unsigned long flags;
2006 if (debug_level >= DEBUG_LEVEL_INFO)
2007 printk("tx_abort(%s)\n", info->device_name);
2009 spin_lock_irqsave(&info->lock,flags);
2010 if (info->tx_active && info->tx_count &&
2011 info->params.mode == MGSL_MODE_HDLC) {
2012 /* clear data count so FIFO is not filled on next IRQ.
2013 * This results in underrun and abort transmission.
2015 info->tx_count = info->tx_put = info->tx_get = 0;
2016 info->tx_aborting = TRUE;
2018 spin_unlock_irqrestore(&info->lock,flags);
2022 static int set_rxenable(MGSLPC_INFO * info, int enable)
2024 unsigned long flags;
2026 if (debug_level >= DEBUG_LEVEL_INFO)
2027 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
2029 spin_lock_irqsave(&info->lock,flags);
2031 if (!info->rx_enabled)
2034 if (info->rx_enabled)
2037 spin_unlock_irqrestore(&info->lock,flags);
2041 /* wait for specified event to occur
2043 * Arguments: info pointer to device instance data
2044 * mask pointer to bitmask of events to wait for
2045 * Return Value: 0 if successful and bit mask updated with
2046 * of events triggerred,
2047 * otherwise error code
2049 static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
2051 unsigned long flags;
2054 struct mgsl_icount cprev, cnow;
2057 struct _input_signal_events oldsigs, newsigs;
2058 DECLARE_WAITQUEUE(wait, current);
2060 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2064 if (debug_level >= DEBUG_LEVEL_INFO)
2065 printk("wait_events(%s,%d)\n", info->device_name, mask);
2067 spin_lock_irqsave(&info->lock,flags);
2069 /* return immediately if state matches requested events */
2071 s = info->serial_signals;
2073 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2074 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2075 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2076 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2078 spin_unlock_irqrestore(&info->lock,flags);
2082 /* save current irq counts */
2083 cprev = info->icount;
2084 oldsigs = info->input_signal_events;
2086 if ((info->params.mode == MGSL_MODE_HDLC) &&
2087 (mask & MgslEvent_ExitHuntMode))
2088 irq_enable(info, CHA, IRQ_EXITHUNT);
2090 set_current_state(TASK_INTERRUPTIBLE);
2091 add_wait_queue(&info->event_wait_q, &wait);
2093 spin_unlock_irqrestore(&info->lock,flags);
2098 if (signal_pending(current)) {
2103 /* get current irq counts */
2104 spin_lock_irqsave(&info->lock,flags);
2105 cnow = info->icount;
2106 newsigs = info->input_signal_events;
2107 set_current_state(TASK_INTERRUPTIBLE);
2108 spin_unlock_irqrestore(&info->lock,flags);
2110 /* if no change, wait aborted for some reason */
2111 if (newsigs.dsr_up == oldsigs.dsr_up &&
2112 newsigs.dsr_down == oldsigs.dsr_down &&
2113 newsigs.dcd_up == oldsigs.dcd_up &&
2114 newsigs.dcd_down == oldsigs.dcd_down &&
2115 newsigs.cts_up == oldsigs.cts_up &&
2116 newsigs.cts_down == oldsigs.cts_down &&
2117 newsigs.ri_up == oldsigs.ri_up &&
2118 newsigs.ri_down == oldsigs.ri_down &&
2119 cnow.exithunt == cprev.exithunt &&
2120 cnow.rxidle == cprev.rxidle) {
2126 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2127 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2128 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2129 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2130 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2131 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2132 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2133 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2134 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2135 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2143 remove_wait_queue(&info->event_wait_q, &wait);
2144 set_current_state(TASK_RUNNING);
2146 if (mask & MgslEvent_ExitHuntMode) {
2147 spin_lock_irqsave(&info->lock,flags);
2148 if (!waitqueue_active(&info->event_wait_q))
2149 irq_disable(info, CHA, IRQ_EXITHUNT);
2150 spin_unlock_irqrestore(&info->lock,flags);
2154 PUT_USER(rc, events, mask_ptr);
2158 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2160 unsigned long flags;
2162 struct mgsl_icount cprev, cnow;
2163 DECLARE_WAITQUEUE(wait, current);
2165 /* save current irq counts */
2166 spin_lock_irqsave(&info->lock,flags);
2167 cprev = info->icount;
2168 add_wait_queue(&info->status_event_wait_q, &wait);
2169 set_current_state(TASK_INTERRUPTIBLE);
2170 spin_unlock_irqrestore(&info->lock,flags);
2174 if (signal_pending(current)) {
2179 /* get new irq counts */
2180 spin_lock_irqsave(&info->lock,flags);
2181 cnow = info->icount;
2182 set_current_state(TASK_INTERRUPTIBLE);
2183 spin_unlock_irqrestore(&info->lock,flags);
2185 /* if no change, wait aborted for some reason */
2186 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2187 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2192 /* check for change in caller specified modem input */
2193 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2194 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2195 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2196 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2203 remove_wait_queue(&info->status_event_wait_q, &wait);
2204 set_current_state(TASK_RUNNING);
2208 /* return the state of the serial control and status signals
2210 static int tiocmget(struct tty_struct *tty, struct file *file)
2212 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2213 unsigned int result;
2214 unsigned long flags;
2216 spin_lock_irqsave(&info->lock,flags);
2218 spin_unlock_irqrestore(&info->lock,flags);
2220 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2221 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2222 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2223 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2224 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2225 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2227 if (debug_level >= DEBUG_LEVEL_INFO)
2228 printk("%s(%d):%s tiocmget() value=%08X\n",
2229 __FILE__,__LINE__, info->device_name, result );
2233 /* set modem control signals (DTR/RTS)
2235 static int tiocmset(struct tty_struct *tty, struct file *file,
2236 unsigned int set, unsigned int clear)
2238 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2239 unsigned long flags;
2241 if (debug_level >= DEBUG_LEVEL_INFO)
2242 printk("%s(%d):%s tiocmset(%x,%x)\n",
2243 __FILE__,__LINE__,info->device_name, set, clear);
2245 if (set & TIOCM_RTS)
2246 info->serial_signals |= SerialSignal_RTS;
2247 if (set & TIOCM_DTR)
2248 info->serial_signals |= SerialSignal_DTR;
2249 if (clear & TIOCM_RTS)
2250 info->serial_signals &= ~SerialSignal_RTS;
2251 if (clear & TIOCM_DTR)
2252 info->serial_signals &= ~SerialSignal_DTR;
2254 spin_lock_irqsave(&info->lock,flags);
2256 spin_unlock_irqrestore(&info->lock,flags);
2261 /* Set or clear transmit break condition
2263 * Arguments: tty pointer to tty instance data
2264 * break_state -1=set break condition, 0=clear
2266 static void mgslpc_break(struct tty_struct *tty, int break_state)
2268 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2269 unsigned long flags;
2271 if (debug_level >= DEBUG_LEVEL_INFO)
2272 printk("%s(%d):mgslpc_break(%s,%d)\n",
2273 __FILE__,__LINE__, info->device_name, break_state);
2275 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2278 spin_lock_irqsave(&info->lock,flags);
2279 if (break_state == -1)
2280 set_reg_bits(info, CHA+DAFO, BIT6);
2282 clear_reg_bits(info, CHA+DAFO, BIT6);
2283 spin_unlock_irqrestore(&info->lock,flags);
2286 /* Service an IOCTL request
2290 * tty pointer to tty instance data
2291 * file pointer to associated file object for device
2292 * cmd IOCTL command code
2293 * arg command argument/context
2295 * Return Value: 0 if success, otherwise error code
2297 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2298 unsigned int cmd, unsigned long arg)
2300 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2302 if (debug_level >= DEBUG_LEVEL_INFO)
2303 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2304 info->device_name, cmd );
2306 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2309 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2310 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2311 if (tty->flags & (1 << TTY_IO_ERROR))
2315 return ioctl_common(info, cmd, arg);
2318 int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
2321 struct mgsl_icount cnow; /* kernel counter temps */
2322 struct serial_icounter_struct __user *p_cuser; /* user space */
2323 void __user *argp = (void __user *)arg;
2324 unsigned long flags;
2327 case MGSL_IOCGPARAMS:
2328 return get_params(info, argp);
2329 case MGSL_IOCSPARAMS:
2330 return set_params(info, argp);
2331 case MGSL_IOCGTXIDLE:
2332 return get_txidle(info, argp);
2333 case MGSL_IOCSTXIDLE:
2334 return set_txidle(info, (int)arg);
2336 return get_interface(info, argp);
2338 return set_interface(info,(int)arg);
2339 case MGSL_IOCTXENABLE:
2340 return set_txenable(info,(int)arg);
2341 case MGSL_IOCRXENABLE:
2342 return set_rxenable(info,(int)arg);
2343 case MGSL_IOCTXABORT:
2344 return tx_abort(info);
2345 case MGSL_IOCGSTATS:
2346 return get_stats(info, argp);
2347 case MGSL_IOCWAITEVENT:
2348 return wait_events(info, argp);
2350 return modem_input_wait(info,(int)arg);
2352 spin_lock_irqsave(&info->lock,flags);
2353 cnow = info->icount;
2354 spin_unlock_irqrestore(&info->lock,flags);
2356 PUT_USER(error,cnow.cts, &p_cuser->cts);
2357 if (error) return error;
2358 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2359 if (error) return error;
2360 PUT_USER(error,cnow.rng, &p_cuser->rng);
2361 if (error) return error;
2362 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2363 if (error) return error;
2364 PUT_USER(error,cnow.rx, &p_cuser->rx);
2365 if (error) return error;
2366 PUT_USER(error,cnow.tx, &p_cuser->tx);
2367 if (error) return error;
2368 PUT_USER(error,cnow.frame, &p_cuser->frame);
2369 if (error) return error;
2370 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2371 if (error) return error;
2372 PUT_USER(error,cnow.parity, &p_cuser->parity);
2373 if (error) return error;
2374 PUT_USER(error,cnow.brk, &p_cuser->brk);
2375 if (error) return error;
2376 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2377 if (error) return error;
2380 return -ENOIOCTLCMD;
2385 /* Set new termios settings
2389 * tty pointer to tty structure
2390 * termios pointer to buffer to hold returned old termios
2392 static void mgslpc_set_termios(struct tty_struct *tty, struct termios *old_termios)
2394 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2395 unsigned long flags;
2397 if (debug_level >= DEBUG_LEVEL_INFO)
2398 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2399 tty->driver->name );
2401 /* just return if nothing has changed */
2402 if ((tty->termios->c_cflag == old_termios->c_cflag)
2403 && (RELEVANT_IFLAG(tty->termios->c_iflag)
2404 == RELEVANT_IFLAG(old_termios->c_iflag)))
2407 mgslpc_change_params(info);
2409 /* Handle transition to B0 status */
2410 if (old_termios->c_cflag & CBAUD &&
2411 !(tty->termios->c_cflag & CBAUD)) {
2412 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2413 spin_lock_irqsave(&info->lock,flags);
2415 spin_unlock_irqrestore(&info->lock,flags);
2418 /* Handle transition away from B0 status */
2419 if (!(old_termios->c_cflag & CBAUD) &&
2420 tty->termios->c_cflag & CBAUD) {
2421 info->serial_signals |= SerialSignal_DTR;
2422 if (!(tty->termios->c_cflag & CRTSCTS) ||
2423 !test_bit(TTY_THROTTLED, &tty->flags)) {
2424 info->serial_signals |= SerialSignal_RTS;
2426 spin_lock_irqsave(&info->lock,flags);
2428 spin_unlock_irqrestore(&info->lock,flags);
2431 /* Handle turning off CRTSCTS */
2432 if (old_termios->c_cflag & CRTSCTS &&
2433 !(tty->termios->c_cflag & CRTSCTS)) {
2434 tty->hw_stopped = 0;
2439 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2441 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2443 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2446 if (debug_level >= DEBUG_LEVEL_INFO)
2447 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2448 __FILE__,__LINE__, info->device_name, info->count);
2453 if (tty_hung_up_p(filp))
2456 if ((tty->count == 1) && (info->count != 1)) {
2458 * tty->count is 1 and the tty structure will be freed.
2459 * info->count should be one in this case.
2460 * if it's not, correct it so that the port is shutdown.
2462 printk("mgslpc_close: bad refcount; tty->count is 1, "
2463 "info->count is %d\n", info->count);
2469 /* if at least one open remaining, leave hardware active */
2473 info->flags |= ASYNC_CLOSING;
2475 /* set tty->closing to notify line discipline to
2476 * only process XON/XOFF characters. Only the N_TTY
2477 * discipline appears to use this (ppp does not).
2481 /* wait for transmit data to clear all layers */
2483 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
2484 if (debug_level >= DEBUG_LEVEL_INFO)
2485 printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sent\n",
2486 __FILE__,__LINE__, info->device_name );
2487 tty_wait_until_sent(tty, info->closing_wait);
2490 if (info->flags & ASYNC_INITIALIZED)
2491 mgslpc_wait_until_sent(tty, info->timeout);
2493 if (tty->driver->flush_buffer)
2494 tty->driver->flush_buffer(tty);
2496 ldisc_flush_buffer(tty);
2503 if (info->blocked_open) {
2504 if (info->close_delay) {
2505 msleep_interruptible(jiffies_to_msecs(info->close_delay));
2507 wake_up_interruptible(&info->open_wait);
2510 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2512 wake_up_interruptible(&info->close_wait);
2515 if (debug_level >= DEBUG_LEVEL_INFO)
2516 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2517 tty->driver->name, info->count);
2520 /* Wait until the transmitter is empty.
2522 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2524 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2525 unsigned long orig_jiffies, char_time;
2530 if (debug_level >= DEBUG_LEVEL_INFO)
2531 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2532 __FILE__,__LINE__, info->device_name );
2534 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2537 if (!(info->flags & ASYNC_INITIALIZED))
2540 orig_jiffies = jiffies;
2542 /* Set check interval to 1/5 of estimated time to
2543 * send a character, and make it at least 1. The check
2544 * interval should also be less than the timeout.
2545 * Note: use tight timings here to satisfy the NIST-PCTS.
2548 if ( info->params.data_rate ) {
2549 char_time = info->timeout/(32 * 5);
2556 char_time = min_t(unsigned long, char_time, timeout);
2558 if (info->params.mode == MGSL_MODE_HDLC) {
2559 while (info->tx_active) {
2560 msleep_interruptible(jiffies_to_msecs(char_time));
2561 if (signal_pending(current))
2563 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2567 while ((info->tx_count || info->tx_active) &&
2569 msleep_interruptible(jiffies_to_msecs(char_time));
2570 if (signal_pending(current))
2572 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2578 if (debug_level >= DEBUG_LEVEL_INFO)
2579 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2580 __FILE__,__LINE__, info->device_name );
2583 /* Called by tty_hangup() when a hangup is signaled.
2584 * This is the same as closing all open files for the port.
2586 static void mgslpc_hangup(struct tty_struct *tty)
2588 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2590 if (debug_level >= DEBUG_LEVEL_INFO)
2591 printk("%s(%d):mgslpc_hangup(%s)\n",
2592 __FILE__,__LINE__, info->device_name );
2594 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2597 mgslpc_flush_buffer(tty);
2601 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2604 wake_up_interruptible(&info->open_wait);
2607 /* Block the current process until the specified port
2608 * is ready to be opened.
2610 static int block_til_ready(struct tty_struct *tty, struct file *filp,
2613 DECLARE_WAITQUEUE(wait, current);
2615 int do_clocal = 0, extra_count = 0;
2616 unsigned long flags;
2618 if (debug_level >= DEBUG_LEVEL_INFO)
2619 printk("%s(%d):block_til_ready on %s\n",
2620 __FILE__,__LINE__, tty->driver->name );
2622 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
2623 /* nonblock mode is set or port is not enabled */
2624 /* just verify that callout device is not active */
2625 info->flags |= ASYNC_NORMAL_ACTIVE;
2629 if (tty->termios->c_cflag & CLOCAL)
2632 /* Wait for carrier detect and the line to become
2633 * free (i.e., not in use by the callout). While we are in
2634 * this loop, info->count is dropped by one, so that
2635 * mgslpc_close() knows when to free things. We restore it upon
2636 * exit, either normal or abnormal.
2640 add_wait_queue(&info->open_wait, &wait);
2642 if (debug_level >= DEBUG_LEVEL_INFO)
2643 printk("%s(%d):block_til_ready before block on %s count=%d\n",
2644 __FILE__,__LINE__, tty->driver->name, info->count );
2646 spin_lock_irqsave(&info->lock, flags);
2647 if (!tty_hung_up_p(filp)) {
2651 spin_unlock_irqrestore(&info->lock, flags);
2652 info->blocked_open++;
2655 if ((tty->termios->c_cflag & CBAUD)) {
2656 spin_lock_irqsave(&info->lock,flags);
2657 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2659 spin_unlock_irqrestore(&info->lock,flags);
2662 set_current_state(TASK_INTERRUPTIBLE);
2664 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
2665 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
2666 -EAGAIN : -ERESTARTSYS;
2670 spin_lock_irqsave(&info->lock,flags);
2672 spin_unlock_irqrestore(&info->lock,flags);
2674 if (!(info->flags & ASYNC_CLOSING) &&
2675 (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
2679 if (signal_pending(current)) {
2680 retval = -ERESTARTSYS;
2684 if (debug_level >= DEBUG_LEVEL_INFO)
2685 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
2686 __FILE__,__LINE__, tty->driver->name, info->count );
2691 set_current_state(TASK_RUNNING);
2692 remove_wait_queue(&info->open_wait, &wait);
2696 info->blocked_open--;
2698 if (debug_level >= DEBUG_LEVEL_INFO)
2699 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
2700 __FILE__,__LINE__, tty->driver->name, info->count );
2703 info->flags |= ASYNC_NORMAL_ACTIVE;
2708 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2712 unsigned long flags;
2714 /* verify range of specified line number */
2716 if ((line < 0) || (line >= mgslpc_device_count)) {
2717 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2718 __FILE__,__LINE__,line);
2722 /* find the info structure for the specified line */
2723 info = mgslpc_device_list;
2724 while(info && info->line != line)
2725 info = info->next_device;
2726 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2729 tty->driver_data = info;
2732 if (debug_level >= DEBUG_LEVEL_INFO)
2733 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2734 __FILE__,__LINE__,tty->driver->name, info->count);
2736 /* If port is closing, signal caller to try again */
2737 if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
2738 if (info->flags & ASYNC_CLOSING)
2739 interruptible_sleep_on(&info->close_wait);
2740 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2741 -EAGAIN : -ERESTARTSYS);
2745 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2747 spin_lock_irqsave(&info->netlock, flags);
2748 if (info->netcount) {
2750 spin_unlock_irqrestore(&info->netlock, flags);
2754 spin_unlock_irqrestore(&info->netlock, flags);
2756 if (info->count == 1) {
2757 /* 1st open on this device, init hardware */
2758 retval = startup(info);
2763 retval = block_til_ready(tty, filp, info);
2765 if (debug_level >= DEBUG_LEVEL_INFO)
2766 printk("%s(%d):block_til_ready(%s) returned %d\n",
2767 __FILE__,__LINE__, info->device_name, retval);
2771 if (debug_level >= DEBUG_LEVEL_INFO)
2772 printk("%s(%d):mgslpc_open(%s) success\n",
2773 __FILE__,__LINE__, info->device_name);
2778 if (tty->count == 1)
2779 info->tty = NULL; /* tty layer will release tty struct */
2788 * /proc fs routines....
2791 static inline int line_info(char *buf, MGSLPC_INFO *info)
2795 unsigned long flags;
2797 ret = sprintf(buf, "%s:io:%04X irq:%d",
2798 info->device_name, info->io_base, info->irq_level);
2800 /* output current serial signal states */
2801 spin_lock_irqsave(&info->lock,flags);
2803 spin_unlock_irqrestore(&info->lock,flags);
2807 if (info->serial_signals & SerialSignal_RTS)
2808 strcat(stat_buf, "|RTS");
2809 if (info->serial_signals & SerialSignal_CTS)
2810 strcat(stat_buf, "|CTS");
2811 if (info->serial_signals & SerialSignal_DTR)
2812 strcat(stat_buf, "|DTR");
2813 if (info->serial_signals & SerialSignal_DSR)
2814 strcat(stat_buf, "|DSR");
2815 if (info->serial_signals & SerialSignal_DCD)
2816 strcat(stat_buf, "|CD");
2817 if (info->serial_signals & SerialSignal_RI)
2818 strcat(stat_buf, "|RI");
2820 if (info->params.mode == MGSL_MODE_HDLC) {
2821 ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
2822 info->icount.txok, info->icount.rxok);
2823 if (info->icount.txunder)
2824 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
2825 if (info->icount.txabort)
2826 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
2827 if (info->icount.rxshort)
2828 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
2829 if (info->icount.rxlong)
2830 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
2831 if (info->icount.rxover)
2832 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
2833 if (info->icount.rxcrc)
2834 ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
2836 ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
2837 info->icount.tx, info->icount.rx);
2838 if (info->icount.frame)
2839 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2840 if (info->icount.parity)
2841 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2842 if (info->icount.brk)
2843 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2844 if (info->icount.overrun)
2845 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2848 /* Append serial signal status to end */
2849 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2851 ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2852 info->tx_active,info->bh_requested,info->bh_running,
2858 /* Called to print information about devices
2860 static int mgslpc_read_proc(char *page, char **start, off_t off, int count,
2861 int *eof, void *data)
2867 len += sprintf(page, "synclink driver:%s\n", driver_version);
2869 info = mgslpc_device_list;
2871 l = line_info(page + len, info);
2873 if (len+begin > off+count)
2875 if (len+begin < off) {
2879 info = info->next_device;
2884 if (off >= len+begin)
2886 *start = page + (off-begin);
2887 return ((count < begin+len-off) ? count : begin+len-off);
2890 int rx_alloc_buffers(MGSLPC_INFO *info)
2892 /* each buffer has header and data */
2893 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2895 /* calculate total allocation size for 8 buffers */
2896 info->rx_buf_total_size = info->rx_buf_size * 8;
2898 /* limit total allocated memory */
2899 if (info->rx_buf_total_size > 0x10000)
2900 info->rx_buf_total_size = 0x10000;
2902 /* calculate number of buffers */
2903 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2905 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2906 if (info->rx_buf == NULL)
2909 rx_reset_buffers(info);
2913 void rx_free_buffers(MGSLPC_INFO *info)
2915 kfree(info->rx_buf);
2916 info->rx_buf = NULL;
2919 int claim_resources(MGSLPC_INFO *info)
2921 if (rx_alloc_buffers(info) < 0 ) {
2922 printk( "Cant allocate rx buffer %s\n", info->device_name);
2923 release_resources(info);
2929 void release_resources(MGSLPC_INFO *info)
2931 if (debug_level >= DEBUG_LEVEL_INFO)
2932 printk("release_resources(%s)\n", info->device_name);
2933 rx_free_buffers(info);
2936 /* Add the specified device instance data structure to the
2937 * global linked list of devices and increment the device count.
2939 * Arguments: info pointer to device instance data
2941 void mgslpc_add_device(MGSLPC_INFO *info)
2943 info->next_device = NULL;
2944 info->line = mgslpc_device_count;
2945 sprintf(info->device_name,"ttySLP%d",info->line);
2947 if (info->line < MAX_DEVICE_COUNT) {
2948 if (maxframe[info->line])
2949 info->max_frame_size = maxframe[info->line];
2950 info->dosyncppp = dosyncppp[info->line];
2953 mgslpc_device_count++;
2955 if (!mgslpc_device_list)
2956 mgslpc_device_list = info;
2958 MGSLPC_INFO *current_dev = mgslpc_device_list;
2959 while( current_dev->next_device )
2960 current_dev = current_dev->next_device;
2961 current_dev->next_device = info;
2964 if (info->max_frame_size < 4096)
2965 info->max_frame_size = 4096;
2966 else if (info->max_frame_size > 65535)
2967 info->max_frame_size = 65535;
2969 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2970 info->device_name, info->io_base, info->irq_level);
2977 void mgslpc_remove_device(MGSLPC_INFO *remove_info)
2979 MGSLPC_INFO *info = mgslpc_device_list;
2980 MGSLPC_INFO *last = NULL;
2983 if (info == remove_info) {
2985 last->next_device = info->next_device;
2987 mgslpc_device_list = info->next_device;
2991 release_resources(info);
2993 mgslpc_device_count--;
2997 info = info->next_device;
3001 static struct pcmcia_device_id mgslpc_ids[] = {
3002 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
3005 MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
3007 static struct pcmcia_driver mgslpc_driver = {
3008 .owner = THIS_MODULE,
3010 .name = "synclink_cs",
3012 .probe = mgslpc_attach,
3013 .remove = mgslpc_detach,
3014 .id_table = mgslpc_ids,
3015 .suspend = mgslpc_suspend,
3016 .resume = mgslpc_resume,
3019 static struct tty_operations mgslpc_ops = {
3020 .open = mgslpc_open,
3021 .close = mgslpc_close,
3022 .write = mgslpc_write,
3023 .put_char = mgslpc_put_char,
3024 .flush_chars = mgslpc_flush_chars,
3025 .write_room = mgslpc_write_room,
3026 .chars_in_buffer = mgslpc_chars_in_buffer,
3027 .flush_buffer = mgslpc_flush_buffer,
3028 .ioctl = mgslpc_ioctl,
3029 .throttle = mgslpc_throttle,
3030 .unthrottle = mgslpc_unthrottle,
3031 .send_xchar = mgslpc_send_xchar,
3032 .break_ctl = mgslpc_break,
3033 .wait_until_sent = mgslpc_wait_until_sent,
3034 .read_proc = mgslpc_read_proc,
3035 .set_termios = mgslpc_set_termios,
3037 .start = tx_release,
3038 .hangup = mgslpc_hangup,
3039 .tiocmget = tiocmget,
3040 .tiocmset = tiocmset,
3043 static void synclink_cs_cleanup(void)
3047 printk("Unloading %s: version %s\n", driver_name, driver_version);
3049 while(mgslpc_device_list)
3050 mgslpc_remove_device(mgslpc_device_list);
3052 if (serial_driver) {
3053 if ((rc = tty_unregister_driver(serial_driver)))
3054 printk("%s(%d) failed to unregister tty driver err=%d\n",
3055 __FILE__,__LINE__,rc);
3056 put_tty_driver(serial_driver);
3059 pcmcia_unregister_driver(&mgslpc_driver);
3062 static int __init synclink_cs_init(void)
3066 if (break_on_load) {
3067 mgslpc_get_text_ptr();
3071 printk("%s %s\n", driver_name, driver_version);
3073 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
3076 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
3077 if (!serial_driver) {
3082 /* Initialize the tty_driver structure */
3084 serial_driver->owner = THIS_MODULE;
3085 serial_driver->driver_name = "synclink_cs";
3086 serial_driver->name = "ttySLP";
3087 serial_driver->major = ttymajor;
3088 serial_driver->minor_start = 64;
3089 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3090 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3091 serial_driver->init_termios = tty_std_termios;
3092 serial_driver->init_termios.c_cflag =
3093 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3094 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3095 tty_set_operations(serial_driver, &mgslpc_ops);
3097 if ((rc = tty_register_driver(serial_driver)) < 0) {
3098 printk("%s(%d):Couldn't register serial driver\n",
3100 put_tty_driver(serial_driver);
3101 serial_driver = NULL;
3105 printk("%s %s, tty major#%d\n",
3106 driver_name, driver_version,
3107 serial_driver->major);
3112 synclink_cs_cleanup();
3116 static void __exit synclink_cs_exit(void)
3118 synclink_cs_cleanup();
3121 module_init(synclink_cs_init);
3122 module_exit(synclink_cs_exit);
3124 static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
3129 /* note:standard BRG mode is broken in V3.2 chip
3130 * so enhanced mode is always used
3138 for (M = 1; N > 64 && M < 16; M++)
3144 * BGR[7..0] contained in BGR register
3145 * BGR[9..8] contained in CCR2[7..6]
3146 * divisor = (N+1)*2^M
3148 * Note: M *must* not be zero (causes asymetric duty cycle)
3150 write_reg(info, (unsigned char) (channel + BGR),
3151 (unsigned char) ((M << 6) + N));
3152 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
3153 val |= ((M << 4) & 0xc0);
3154 write_reg(info, (unsigned char) (channel + CCR2), val);
3158 /* Enabled the AUX clock output at the specified frequency.
3160 static void enable_auxclk(MGSLPC_INFO *info)
3166 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3167 * 05 ADM Address Mode, 0 = no addr recognition
3168 * 04 TMD Timer Mode, 0 = external
3169 * 03 RAC Receiver Active, 0 = inactive
3170 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3171 * 01 TRS Timer Resolution, 1=512
3172 * 00 TLP Test Loop, 0 = no loop
3178 /* channel B RTS is used to enable AUXCLK driver on SP505 */
3179 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3181 write_reg(info, CHB + MODE, val);
3185 * 07 PU Power Up, 1=active, 0=power down
3186 * 06 MCE Master Clock Enable, 1=enabled
3188 * 04..02 SC[2..0] Encoding
3189 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3193 write_reg(info, CHB + CCR0, 0xc0);
3197 * 07 SFLG Shared Flag, 0 = disable shared flags
3198 * 06 GALP Go Active On Loop, 0 = not used
3199 * 05 GLP Go On Loop, 0 = not used
3200 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3201 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3202 * 02..00 CM[2..0] Clock Mode
3206 write_reg(info, CHB + CCR1, 0x17);
3210 * 07..06 BGR[9..8] Baud rate bits 9..8
3211 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3212 * 04 SSEL Clock source select, 1=submode b
3213 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3214 * 02 RWX Read/Write Exchange 0=disabled
3215 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3216 * 00 DIV, data inversion 0=disabled, 1=enabled
3220 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3221 write_reg(info, CHB + CCR2, 0x38);
3223 write_reg(info, CHB + CCR2, 0x30);
3227 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3228 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3229 * 05 TST1 Test Pin, 0=normal operation
3230 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3231 * 03..02 Reserved, must be 0
3232 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3236 write_reg(info, CHB + CCR4, 0x50);
3238 /* if auxclk not enabled, set internal BRG so
3239 * CTS transitions can be detected (requires TxC)
3241 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3242 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3244 mgslpc_set_rate(info, CHB, 921600);
3247 static void loopback_enable(MGSLPC_INFO *info)
3251 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3252 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3253 write_reg(info, CHA + CCR1, val);
3255 /* CCR2:04 SSEL Clock source select, 1=submode b */
3256 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3257 write_reg(info, CHA + CCR2, val);
3259 /* set LinkSpeed if available, otherwise default to 2Mbps */
3260 if (info->params.clock_speed)
3261 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3263 mgslpc_set_rate(info, CHA, 1843200);
3265 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3266 val = read_reg(info, CHA + MODE) | BIT0;
3267 write_reg(info, CHA + MODE, val);
3270 void hdlc_mode(MGSLPC_INFO *info)
3273 unsigned char clkmode, clksubmode;
3275 /* disable all interrupts */
3276 irq_disable(info, CHA, 0xffff);
3277 irq_disable(info, CHB, 0xffff);
3278 port_irq_disable(info, 0xff);
3280 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3281 clkmode = clksubmode = 0;
3282 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3283 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3284 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3286 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3287 && info->params.flags & HDLC_FLAG_TXC_BRG) {
3288 /* clock mode 7b, rcv = BRG, xmt = BRG */
3291 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3292 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3293 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3297 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3300 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3301 /* clock mode 0b, rcv = RxC, xmt = BRG */
3307 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3308 * 05 ADM Address Mode, 0 = no addr recognition
3309 * 04 TMD Timer Mode, 0 = external
3310 * 03 RAC Receiver Active, 0 = inactive
3311 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3312 * 01 TRS Timer Resolution, 1=512
3313 * 00 TLP Test Loop, 0 = no loop
3318 if (info->params.loopback)
3321 /* preserve RTS state */
3322 if (info->serial_signals & SerialSignal_RTS)
3324 write_reg(info, CHA + MODE, val);
3328 * 07 PU Power Up, 1=active, 0=power down
3329 * 06 MCE Master Clock Enable, 1=enabled
3331 * 04..02 SC[2..0] Encoding
3332 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3337 switch (info->params.encoding)
3339 case HDLC_ENCODING_NRZI:
3342 case HDLC_ENCODING_BIPHASE_SPACE:
3345 case HDLC_ENCODING_BIPHASE_MARK:
3348 case HDLC_ENCODING_BIPHASE_LEVEL:
3350 break; // Manchester
3352 write_reg(info, CHA + CCR0, val);
3356 * 07 SFLG Shared Flag, 0 = disable shared flags
3357 * 06 GALP Go Active On Loop, 0 = not used
3358 * 05 GLP Go On Loop, 0 = not used
3359 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3360 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3361 * 02..00 CM[2..0] Clock Mode
3365 val = 0x10 + clkmode;
3366 write_reg(info, CHA + CCR1, val);
3370 * 07..06 BGR[9..8] Baud rate bits 9..8
3371 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3372 * 04 SSEL Clock source select, 1=submode b
3373 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3374 * 02 RWX Read/Write Exchange 0=disabled
3375 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3376 * 00 DIV, data inversion 0=disabled, 1=enabled
3381 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3382 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3386 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3388 if (info->params.encoding == HDLC_ENCODING_NRZB)
3390 write_reg(info, CHA + CCR2, val);
3394 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3395 * 05 EPT Enable preamble transmission, 1=enabled
3396 * 04 RADD Receive address pushed to FIFO, 0=disabled
3397 * 03 CRL CRC Reset Level, 0=FFFF
3398 * 02 RCRC Rx CRC 0=On 1=Off
3399 * 01 TCRC Tx CRC 0=On 1=Off
3400 * 00 PSD DPLL Phase Shift Disable
3405 if (info->params.crc_type == HDLC_CRC_NONE)
3407 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3409 switch (info->params.preamble_length)
3411 case HDLC_PREAMBLE_LENGTH_16BITS:
3414 case HDLC_PREAMBLE_LENGTH_32BITS:
3417 case HDLC_PREAMBLE_LENGTH_64BITS:
3421 write_reg(info, CHA + CCR3, val);
3423 /* PRE - Preamble pattern */
3425 switch (info->params.preamble)
3427 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3428 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3429 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3430 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3432 write_reg(info, CHA + PRE, val);
3436 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3437 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3438 * 05 TST1 Test Pin, 0=normal operation
3439 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3440 * 03..02 Reserved, must be 0
3441 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3446 write_reg(info, CHA + CCR4, val);
3447 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3448 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3450 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3452 /* RLCR Receive length check register
3454 * 7 1=enable receive length check
3455 * 6..0 Max frame length = (RL + 1) * 32
3457 write_reg(info, CHA + RLCR, 0);
3459 /* XBCH Transmit Byte Count High
3461 * 07 DMA mode, 0 = interrupt driven
3462 * 06 NRM, 0=ABM (ignored)
3463 * 05 CAS Carrier Auto Start
3464 * 04 XC Transmit Continuously (ignored)
3465 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3470 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3472 write_reg(info, CHA + XBCH, val);
3473 enable_auxclk(info);
3474 if (info->params.loopback || info->testing_irq)
3475 loopback_enable(info);
3476 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3478 irq_enable(info, CHB, IRQ_CTS);
3479 /* PVR[3] 1=AUTO CTS active */
3480 set_reg_bits(info, CHA + PVR, BIT3);
3482 clear_reg_bits(info, CHA + PVR, BIT3);
3484 irq_enable(info, CHA,
3485 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3486 IRQ_UNDERRUN + IRQ_TXFIFO);
3487 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3488 wait_command_complete(info, CHA);
3489 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3491 /* Master clock mode enabled above to allow reset commands
3492 * to complete even if no data clocks are present.
3494 * Disable master clock mode for normal communications because
3495 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3496 * IRQ when in master clock mode.
3498 * Leave master clock mode enabled for IRQ test because the
3499 * timer IRQ used by the test can only happen in master clock mode.
3501 if (!info->testing_irq)
3502 clear_reg_bits(info, CHA + CCR0, BIT6);
3510 void rx_stop(MGSLPC_INFO *info)
3512 if (debug_level >= DEBUG_LEVEL_ISR)
3513 printk("%s(%d):rx_stop(%s)\n",
3514 __FILE__,__LINE__, info->device_name );
3516 /* MODE:03 RAC Receiver Active, 0=inactive */
3517 clear_reg_bits(info, CHA + MODE, BIT3);
3519 info->rx_enabled = 0;
3520 info->rx_overflow = 0;
3523 void rx_start(MGSLPC_INFO *info)
3525 if (debug_level >= DEBUG_LEVEL_ISR)
3526 printk("%s(%d):rx_start(%s)\n",
3527 __FILE__,__LINE__, info->device_name );
3529 rx_reset_buffers(info);
3530 info->rx_enabled = 0;
3531 info->rx_overflow = 0;
3533 /* MODE:03 RAC Receiver Active, 1=active */
3534 set_reg_bits(info, CHA + MODE, BIT3);
3536 info->rx_enabled = 1;
3539 void tx_start(MGSLPC_INFO *info)
3541 if (debug_level >= DEBUG_LEVEL_ISR)
3542 printk("%s(%d):tx_start(%s)\n",
3543 __FILE__,__LINE__, info->device_name );
3545 if (info->tx_count) {
3546 /* If auto RTS enabled and RTS is inactive, then assert */
3547 /* RTS and set a flag indicating that the driver should */
3548 /* negate RTS when the transmission completes. */
3549 info->drop_rts_on_tx_done = 0;
3551 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3553 if (!(info->serial_signals & SerialSignal_RTS)) {
3554 info->serial_signals |= SerialSignal_RTS;
3556 info->drop_rts_on_tx_done = 1;
3560 if (info->params.mode == MGSL_MODE_ASYNC) {
3561 if (!info->tx_active) {
3562 info->tx_active = 1;
3566 info->tx_active = 1;
3568 info->tx_timer.expires = jiffies + msecs_to_jiffies(5000);
3569 add_timer(&info->tx_timer);
3573 if (!info->tx_enabled)
3574 info->tx_enabled = 1;
3577 void tx_stop(MGSLPC_INFO *info)
3579 if (debug_level >= DEBUG_LEVEL_ISR)
3580 printk("%s(%d):tx_stop(%s)\n",
3581 __FILE__,__LINE__, info->device_name );
3583 del_timer(&info->tx_timer);
3585 info->tx_enabled = 0;
3586 info->tx_active = 0;
3589 /* Reset the adapter to a known state and prepare it for further use.
3591 void reset_device(MGSLPC_INFO *info)
3593 /* power up both channels (set BIT7) */
3594 write_reg(info, CHA + CCR0, 0x80);
3595 write_reg(info, CHB + CCR0, 0x80);
3596 write_reg(info, CHA + MODE, 0);
3597 write_reg(info, CHB + MODE, 0);
3599 /* disable all interrupts */
3600 irq_disable(info, CHA, 0xffff);
3601 irq_disable(info, CHB, 0xffff);
3602 port_irq_disable(info, 0xff);
3604 /* PCR Port Configuration Register
3606 * 07..04 DEC[3..0] Serial I/F select outputs
3607 * 03 output, 1=AUTO CTS control enabled
3608 * 02 RI Ring Indicator input 0=active
3609 * 01 DSR input 0=active
3610 * 00 DTR output 0=active
3614 write_reg(info, PCR, 0x06);
3616 /* PVR Port Value Register
3618 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3619 * 03 AUTO CTS output 1=enabled
3620 * 02 RI Ring Indicator input
3622 * 00 DTR output (1=inactive)
3626 // write_reg(info, PVR, PVR_DTR);
3628 /* IPC Interrupt Port Configuration
3630 * 07 VIS 1=Masked interrupts visible
3631 * 06..05 Reserved, 0
3632 * 04..03 SLA Slave address, 00 ignored
3633 * 02 CASM Cascading Mode, 1=daisy chain
3634 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3638 write_reg(info, IPC, 0x05);
3641 void async_mode(MGSLPC_INFO *info)
3645 /* disable all interrupts */
3646 irq_disable(info, CHA, 0xffff);
3647 irq_disable(info, CHB, 0xffff);
3648 port_irq_disable(info, 0xff);
3653 * 06 FRTS RTS State, 0=active
3654 * 05 FCTS Flow Control on CTS
3655 * 04 FLON Flow Control Enable
3656 * 03 RAC Receiver Active, 0 = inactive
3657 * 02 RTS 0=Auto RTS, 1=manual RTS
3658 * 01 TRS Timer Resolution, 1=512
3659 * 00 TLP Test Loop, 0 = no loop
3664 if (info->params.loopback)
3667 /* preserve RTS state */
3668 if (!(info->serial_signals & SerialSignal_RTS))
3670 write_reg(info, CHA + MODE, val);
3674 * 07 PU Power Up, 1=active, 0=power down
3675 * 06 MCE Master Clock Enable, 1=enabled
3677 * 04..02 SC[2..0] Encoding, 000=NRZ
3678 * 01..00 SM[1..0] Serial Mode, 11=Async
3682 write_reg(info, CHA + CCR0, 0x83);
3686 * 07..05 Reserved, 0
3687 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3688 * 03 BCR Bit Clock Rate, 1=16x
3689 * 02..00 CM[2..0] Clock Mode, 111=BRG
3693 write_reg(info, CHA + CCR1, 0x1f);
3697 * 07..06 BGR[9..8] Baud rate bits 9..8
3698 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3699 * 04 SSEL Clock source select, 1=submode b
3700 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3701 * 02 RWX Read/Write Exchange 0=disabled
3703 * 00 DIV, data inversion 0=disabled, 1=enabled
3707 write_reg(info, CHA + CCR2, 0x10);
3711 * 07..01 Reserved, 0
3712 * 00 PSD DPLL Phase Shift Disable
3716 write_reg(info, CHA + CCR3, 0);
3720 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3721 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3722 * 05 TST1 Test Pin, 0=normal operation
3723 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3724 * 03..00 Reserved, must be 0
3728 write_reg(info, CHA + CCR4, 0x50);
3729 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3734 * 06 XBRK transmit break, 0=normal operation
3735 * 05 Stop bits (0=1, 1=2)
3736 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3737 * 02 PAREN Parity Enable
3738 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3742 if (info->params.data_bits != 8)
3743 val |= BIT0; /* 7 bits */
3744 if (info->params.stop_bits != 1)
3746 if (info->params.parity != ASYNC_PARITY_NONE)
3748 val |= BIT2; /* Parity enable */
3749 if (info->params.parity == ASYNC_PARITY_ODD)
3754 write_reg(info, CHA + DAFO, val);
3756 /* RFC Rx FIFO Control
3759 * 06 DPS, 1=parity bit not stored in data byte
3760 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3761 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3762 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3764 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3768 write_reg(info, CHA + RFC, 0x5c);
3770 /* RLCR Receive length check register
3772 * Max frame length = (RL + 1) * 32
3774 write_reg(info, CHA + RLCR, 0);
3776 /* XBCH Transmit Byte Count High
3778 * 07 DMA mode, 0 = interrupt driven
3779 * 06 NRM, 0=ABM (ignored)
3780 * 05 CAS Carrier Auto Start
3781 * 04 XC Transmit Continuously (ignored)
3782 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3787 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3789 write_reg(info, CHA + XBCH, val);
3790 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3791 irq_enable(info, CHA, IRQ_CTS);
3793 /* MODE:03 RAC Receiver Active, 1=active */
3794 set_reg_bits(info, CHA + MODE, BIT3);
3795 enable_auxclk(info);
3796 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3797 irq_enable(info, CHB, IRQ_CTS);
3798 /* PVR[3] 1=AUTO CTS active */
3799 set_reg_bits(info, CHA + PVR, BIT3);
3801 clear_reg_bits(info, CHA + PVR, BIT3);
3802 irq_enable(info, CHA,
3803 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3804 IRQ_ALLSENT + IRQ_TXFIFO);
3805 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3806 wait_command_complete(info, CHA);
3807 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3810 /* Set the HDLC idle mode for the transmitter.
3812 void tx_set_idle(MGSLPC_INFO *info)
3814 /* Note: ESCC2 only supports flags and one idle modes */
3815 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3816 set_reg_bits(info, CHA + CCR1, BIT3);
3818 clear_reg_bits(info, CHA + CCR1, BIT3);
3821 /* get state of the V24 status (input) signals.
3823 void get_signals(MGSLPC_INFO *info)
3825 unsigned char status = 0;
3827 /* preserve DTR and RTS */
3828 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3830 if (read_reg(info, CHB + VSTR) & BIT7)
3831 info->serial_signals |= SerialSignal_DCD;
3832 if (read_reg(info, CHB + STAR) & BIT1)
3833 info->serial_signals |= SerialSignal_CTS;
3835 status = read_reg(info, CHA + PVR);
3836 if (!(status & PVR_RI))
3837 info->serial_signals |= SerialSignal_RI;
3838 if (!(status & PVR_DSR))
3839 info->serial_signals |= SerialSignal_DSR;
3842 /* Set the state of DTR and RTS based on contents of
3843 * serial_signals member of device extension.
3845 void set_signals(MGSLPC_INFO *info)
3849 val = read_reg(info, CHA + MODE);
3850 if (info->params.mode == MGSL_MODE_ASYNC) {
3851 if (info->serial_signals & SerialSignal_RTS)
3856 if (info->serial_signals & SerialSignal_RTS)
3861 write_reg(info, CHA + MODE, val);
3863 if (info->serial_signals & SerialSignal_DTR)
3864 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3866 set_reg_bits(info, CHA + PVR, PVR_DTR);
3869 void rx_reset_buffers(MGSLPC_INFO *info)
3876 info->rx_frame_count = 0;
3877 for (i=0 ; i < info->rx_buf_count ; i++) {
3878 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3879 buf->status = buf->count = 0;
3883 /* Attempt to return a received HDLC frame
3884 * Only frames received without errors are returned.
3886 * Returns 1 if frame returned, otherwise 0
3888 int rx_get_frame(MGSLPC_INFO *info)
3890 unsigned short status;
3892 unsigned int framesize = 0;
3893 unsigned long flags;
3894 struct tty_struct *tty = info->tty;
3895 int return_frame = 0;
3897 if (info->rx_frame_count == 0)
3900 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3902 status = buf->status;
3904 /* 07 VFR 1=valid frame
3905 * 06 RDO 1=data overrun
3906 * 05 CRC 1=OK, 0=error
3907 * 04 RAB 1=frame aborted
3909 if ((status & 0xf0) != 0xA0) {
3910 if (!(status & BIT7) || (status & BIT4))
3911 info->icount.rxabort++;
3912 else if (status & BIT6)
3913 info->icount.rxover++;
3914 else if (!(status & BIT5)) {
3915 info->icount.rxcrc++;
3916 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
3922 struct net_device_stats *stats = hdlc_stats(info->netdev);
3924 stats->rx_frame_errors++;
3931 framesize = buf->count;
3933 if (debug_level >= DEBUG_LEVEL_BH)
3934 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3935 __FILE__,__LINE__,info->device_name,status,framesize);
3937 if (debug_level >= DEBUG_LEVEL_DATA)
3938 trace_block(info, buf->data, framesize, 0);
3941 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3942 framesize+1 > info->max_frame_size) ||
3943 framesize > info->max_frame_size)
3944 info->icount.rxlong++;
3947 info->icount.rxok++;
3949 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3950 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3956 hdlcdev_rx(info, buf->data, framesize);
3959 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3963 spin_lock_irqsave(&info->lock,flags);
3964 buf->status = buf->count = 0;
3965 info->rx_frame_count--;
3967 if (info->rx_get >= info->rx_buf_count)
3969 spin_unlock_irqrestore(&info->lock,flags);
3974 BOOLEAN register_test(MGSLPC_INFO *info)
3976 static unsigned char patterns[] =
3977 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
3978 static unsigned int count = ARRAY_SIZE(patterns);
3981 unsigned long flags;
3983 spin_lock_irqsave(&info->lock,flags);
3986 for (i = 0; i < count; i++) {
3987 write_reg(info, XAD1, patterns[i]);
3988 write_reg(info, XAD2, patterns[(i + 1) % count]);
3989 if ((read_reg(info, XAD1) != patterns[i]) ||
3990 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
3996 spin_unlock_irqrestore(&info->lock,flags);
4000 BOOLEAN irq_test(MGSLPC_INFO *info)
4002 unsigned long end_time;
4003 unsigned long flags;
4005 spin_lock_irqsave(&info->lock,flags);
4008 info->testing_irq = TRUE;
4011 info->irq_occurred = FALSE;
4013 /* init hdlc mode */
4015 irq_enable(info, CHA, IRQ_TIMER);
4016 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
4017 issue_command(info, CHA, CMD_START_TIMER);
4019 spin_unlock_irqrestore(&info->lock,flags);
4022 while(end_time-- && !info->irq_occurred) {
4023 msleep_interruptible(10);
4026 info->testing_irq = FALSE;
4028 spin_lock_irqsave(&info->lock,flags);
4030 spin_unlock_irqrestore(&info->lock,flags);
4032 return info->irq_occurred ? TRUE : FALSE;
4035 int adapter_test(MGSLPC_INFO *info)
4037 if (!register_test(info)) {
4038 info->init_error = DiagStatus_AddressFailure;
4039 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
4040 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
4044 if (!irq_test(info)) {
4045 info->init_error = DiagStatus_IrqFailure;
4046 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
4047 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
4051 if (debug_level >= DEBUG_LEVEL_INFO)
4052 printk("%s(%d):device %s passed diagnostics\n",
4053 __FILE__,__LINE__,info->device_name);
4057 void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
4062 printk("%s tx data:\n",info->device_name);
4064 printk("%s rx data:\n",info->device_name);
4072 for(i=0;i<linecount;i++)
4073 printk("%02X ",(unsigned char)data[i]);
4076 for(i=0;i<linecount;i++) {
4077 if (data[i]>=040 && data[i]<=0176)
4078 printk("%c",data[i]);
4089 /* HDLC frame time out
4090 * update stats and do tx completion processing
4092 void tx_timeout(unsigned long context)
4094 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
4095 unsigned long flags;
4097 if ( debug_level >= DEBUG_LEVEL_INFO )
4098 printk( "%s(%d):tx_timeout(%s)\n",
4099 __FILE__,__LINE__,info->device_name);
4100 if(info->tx_active &&
4101 info->params.mode == MGSL_MODE_HDLC) {
4102 info->icount.txtimeout++;
4104 spin_lock_irqsave(&info->lock,flags);
4105 info->tx_active = 0;
4106 info->tx_count = info->tx_put = info->tx_get = 0;
4108 spin_unlock_irqrestore(&info->lock,flags);
4112 hdlcdev_tx_done(info);
4121 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
4122 * set encoding and frame check sequence (FCS) options
4124 * dev pointer to network device structure
4125 * encoding serial encoding setting
4126 * parity FCS setting
4128 * returns 0 if success, otherwise error code
4130 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
4131 unsigned short parity)
4133 MGSLPC_INFO *info = dev_to_port(dev);
4134 unsigned char new_encoding;
4135 unsigned short new_crctype;
4137 /* return error if TTY interface open */
4143 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
4144 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
4145 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
4146 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
4147 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
4148 default: return -EINVAL;
4153 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
4154 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
4155 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
4156 default: return -EINVAL;
4159 info->params.encoding = new_encoding;
4160 info->params.crc_type = new_crctype;
4162 /* if network interface up, reprogram hardware */
4164 mgslpc_program_hw(info);
4170 * called by generic HDLC layer to send frame
4172 * skb socket buffer containing HDLC frame
4173 * dev pointer to network device structure
4175 * returns 0 if success, otherwise error code
4177 static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
4179 MGSLPC_INFO *info = dev_to_port(dev);
4180 struct net_device_stats *stats = hdlc_stats(dev);
4181 unsigned long flags;
4183 if (debug_level >= DEBUG_LEVEL_INFO)
4184 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
4186 /* stop sending until this frame completes */
4187 netif_stop_queue(dev);
4189 /* copy data to device buffers */
4190 memcpy(info->tx_buf, skb->data, skb->len);
4192 info->tx_put = info->tx_count = skb->len;
4194 /* update network statistics */
4195 stats->tx_packets++;
4196 stats->tx_bytes += skb->len;
4198 /* done with socket buffer, so free it */
4201 /* save start time for transmit timeout detection */
4202 dev->trans_start = jiffies;
4204 /* start hardware transmitter if necessary */
4205 spin_lock_irqsave(&info->lock,flags);
4206 if (!info->tx_active)
4208 spin_unlock_irqrestore(&info->lock,flags);
4214 * called by network layer when interface enabled
4215 * claim resources and initialize hardware
4217 * dev pointer to network device structure
4219 * returns 0 if success, otherwise error code
4221 static int hdlcdev_open(struct net_device *dev)
4223 MGSLPC_INFO *info = dev_to_port(dev);
4225 unsigned long flags;
4227 if (debug_level >= DEBUG_LEVEL_INFO)
4228 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4230 /* generic HDLC layer open processing */
4231 if ((rc = hdlc_open(dev)))
4234 /* arbitrate between network and tty opens */
4235 spin_lock_irqsave(&info->netlock, flags);
4236 if (info->count != 0 || info->netcount != 0) {
4237 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4238 spin_unlock_irqrestore(&info->netlock, flags);
4242 spin_unlock_irqrestore(&info->netlock, flags);
4244 /* claim resources and init adapter */
4245 if ((rc = startup(info)) != 0) {
4246 spin_lock_irqsave(&info->netlock, flags);
4248 spin_unlock_irqrestore(&info->netlock, flags);
4252 /* assert DTR and RTS, apply hardware settings */
4253 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4254 mgslpc_program_hw(info);
4256 /* enable network layer transmit */
4257 dev->trans_start = jiffies;
4258 netif_start_queue(dev);
4260 /* inform generic HDLC layer of current DCD status */
4261 spin_lock_irqsave(&info->lock, flags);
4263 spin_unlock_irqrestore(&info->lock, flags);
4264 hdlc_set_carrier(info->serial_signals & SerialSignal_DCD, dev);
4270 * called by network layer when interface is disabled
4271 * shutdown hardware and release resources
4273 * dev pointer to network device structure
4275 * returns 0 if success, otherwise error code
4277 static int hdlcdev_close(struct net_device *dev)
4279 MGSLPC_INFO *info = dev_to_port(dev);
4280 unsigned long flags;
4282 if (debug_level >= DEBUG_LEVEL_INFO)
4283 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4285 netif_stop_queue(dev);
4287 /* shutdown adapter and release resources */
4292 spin_lock_irqsave(&info->netlock, flags);
4294 spin_unlock_irqrestore(&info->netlock, flags);
4300 * called by network layer to process IOCTL call to network device
4302 * dev pointer to network device structure
4303 * ifr pointer to network interface request structure
4304 * cmd IOCTL command code
4306 * returns 0 if success, otherwise error code
4308 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4310 const size_t size = sizeof(sync_serial_settings);
4311 sync_serial_settings new_line;
4312 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4313 MGSLPC_INFO *info = dev_to_port(dev);
4316 if (debug_level >= DEBUG_LEVEL_INFO)
4317 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4319 /* return error if TTY interface open */
4323 if (cmd != SIOCWANDEV)
4324 return hdlc_ioctl(dev, ifr, cmd);
4326 switch(ifr->ifr_settings.type) {
4327 case IF_GET_IFACE: /* return current sync_serial_settings */
4329 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4330 if (ifr->ifr_settings.size < size) {
4331 ifr->ifr_settings.size = size; /* data size wanted */
4335 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4336 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4337 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4338 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4341 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4342 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4343 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4344 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4345 default: new_line.clock_type = CLOCK_DEFAULT;
4348 new_line.clock_rate = info->params.clock_speed;
4349 new_line.loopback = info->params.loopback ? 1:0;
4351 if (copy_to_user(line, &new_line, size))
4355 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4357 if(!capable(CAP_NET_ADMIN))
4359 if (copy_from_user(&new_line, line, size))
4362 switch (new_line.clock_type)
4364 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4365 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4366 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4367 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4368 case CLOCK_DEFAULT: flags = info->params.flags &
4369 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4370 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4371 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4372 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4373 default: return -EINVAL;
4376 if (new_line.loopback != 0 && new_line.loopback != 1)
4379 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4380 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4381 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4382 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4383 info->params.flags |= flags;
4385 info->params.loopback = new_line.loopback;
4387 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4388 info->params.clock_speed = new_line.clock_rate;
4390 info->params.clock_speed = 0;
4392 /* if network interface up, reprogram hardware */
4394 mgslpc_program_hw(info);
4398 return hdlc_ioctl(dev, ifr, cmd);
4403 * called by network layer when transmit timeout is detected
4405 * dev pointer to network device structure
4407 static void hdlcdev_tx_timeout(struct net_device *dev)
4409 MGSLPC_INFO *info = dev_to_port(dev);
4410 struct net_device_stats *stats = hdlc_stats(dev);
4411 unsigned long flags;
4413 if (debug_level >= DEBUG_LEVEL_INFO)
4414 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4417 stats->tx_aborted_errors++;
4419 spin_lock_irqsave(&info->lock,flags);
4421 spin_unlock_irqrestore(&info->lock,flags);
4423 netif_wake_queue(dev);
4427 * called by device driver when transmit completes
4428 * reenable network layer transmit if stopped
4430 * info pointer to device instance information
4432 static void hdlcdev_tx_done(MGSLPC_INFO *info)
4434 if (netif_queue_stopped(info->netdev))
4435 netif_wake_queue(info->netdev);
4439 * called by device driver when frame received
4440 * pass frame to network layer
4442 * info pointer to device instance information
4443 * buf pointer to buffer contianing frame data
4444 * size count of data bytes in buf
4446 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4448 struct sk_buff *skb = dev_alloc_skb(size);
4449 struct net_device *dev = info->netdev;
4450 struct net_device_stats *stats = hdlc_stats(dev);
4452 if (debug_level >= DEBUG_LEVEL_INFO)
4453 printk("hdlcdev_rx(%s)\n",dev->name);
4456 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
4457 stats->rx_dropped++;
4461 memcpy(skb_put(skb, size),buf,size);
4463 skb->protocol = hdlc_type_trans(skb, info->netdev);
4465 stats->rx_packets++;
4466 stats->rx_bytes += size;
4470 info->netdev->last_rx = jiffies;
4474 * called by device driver when adding device instance
4475 * do generic HDLC initialization
4477 * info pointer to device instance information
4479 * returns 0 if success, otherwise error code
4481 static int hdlcdev_init(MGSLPC_INFO *info)
4484 struct net_device *dev;
4487 /* allocate and initialize network and HDLC layer objects */
4489 if (!(dev = alloc_hdlcdev(info))) {
4490 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4494 /* for network layer reporting purposes only */
4495 dev->base_addr = info->io_base;
4496 dev->irq = info->irq_level;
4498 /* network layer callbacks and settings */
4499 dev->do_ioctl = hdlcdev_ioctl;
4500 dev->open = hdlcdev_open;
4501 dev->stop = hdlcdev_close;
4502 dev->tx_timeout = hdlcdev_tx_timeout;
4503 dev->watchdog_timeo = 10*HZ;
4504 dev->tx_queue_len = 50;
4506 /* generic HDLC layer callbacks and settings */
4507 hdlc = dev_to_hdlc(dev);
4508 hdlc->attach = hdlcdev_attach;
4509 hdlc->xmit = hdlcdev_xmit;
4511 /* register objects with HDLC layer */
4512 if ((rc = register_hdlc_device(dev))) {
4513 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4523 * called by device driver when removing device instance
4524 * do generic HDLC cleanup
4526 * info pointer to device instance information
4528 static void hdlcdev_exit(MGSLPC_INFO *info)
4530 unregister_hdlc_device(info->netdev);
4531 free_netdev(info->netdev);
4532 info->netdev = NULL;
4535 #endif /* CONFIG_HDLC */