2 * linux/drivers/char/pcmcia/synclink_cs.c
4 * $Id: synclink_cs.c,v 4.34 2005/09/08 13:20:54 paulkf Exp $
6 * Device driver for Microgate SyncLink PC Card
7 * multiprotocol serial adapter.
9 * written by Paul Fulghum for Microgate Corporation
10 * paulkf@microgate.com
12 * Microgate and SyncLink are trademarks of Microgate Corporation
14 * This code is released under the GNU General Public License (GPL)
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
31 # define BREAKPOINT() asm(" int $3");
33 # define BREAKPOINT() { }
36 #define MAX_DEVICE_COUNT 4
38 #include <linux/config.h>
39 #include <linux/module.h>
40 #include <linux/errno.h>
41 #include <linux/signal.h>
42 #include <linux/sched.h>
43 #include <linux/timer.h>
44 #include <linux/time.h>
45 #include <linux/interrupt.h>
46 #include <linux/pci.h>
47 #include <linux/tty.h>
48 #include <linux/tty_flip.h>
49 #include <linux/serial.h>
50 #include <linux/major.h>
51 #include <linux/string.h>
52 #include <linux/fcntl.h>
53 #include <linux/ptrace.h>
54 #include <linux/ioport.h>
56 #include <linux/slab.h>
57 #include <linux/netdevice.h>
58 #include <linux/vmalloc.h>
59 #include <linux/init.h>
60 #include <asm/serial.h>
61 #include <linux/delay.h>
62 #include <linux/ioctl.h>
64 #include <asm/system.h>
68 #include <linux/bitops.h>
69 #include <asm/types.h>
70 #include <linux/termios.h>
71 #include <linux/workqueue.h>
72 #include <linux/hdlc.h>
74 #include <pcmcia/cs_types.h>
75 #include <pcmcia/cs.h>
76 #include <pcmcia/cistpl.h>
77 #include <pcmcia/cisreg.h>
78 #include <pcmcia/ds.h>
80 #ifdef CONFIG_HDLC_MODULE
84 #define GET_USER(error,value,addr) error = get_user(value,addr)
85 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
86 #define PUT_USER(error,value,addr) error = put_user(value,addr)
87 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
89 #include <asm/uaccess.h>
91 #include "linux/synclink.h"
93 static MGSL_PARAMS default_params = {
94 MGSL_MODE_HDLC, /* unsigned long mode */
95 0, /* unsigned char loopback; */
96 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
97 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
98 0, /* unsigned long clock_speed; */
99 0xff, /* unsigned char addr_filter; */
100 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
101 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
102 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
103 9600, /* unsigned long data_rate; */
104 8, /* unsigned char data_bits; */
105 1, /* unsigned char stop_bits; */
106 ASYNC_PARITY_NONE /* unsigned char parity; */
112 unsigned char status;
116 /* The queue of BH actions to be performed */
119 #define BH_TRANSMIT 2
122 #define IO_PIN_SHUTDOWN_LIMIT 100
124 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
126 struct _input_signal_events {
139 * Device instance data structure
142 typedef struct _mgslpc_info {
143 void *if_ptr; /* General purpose pointer (used by SPPP) */
146 int count; /* count of opens */
148 unsigned short close_delay;
149 unsigned short closing_wait; /* time to wait before closing */
151 struct mgsl_icount icount;
153 struct tty_struct *tty;
155 int x_char; /* xon/xoff character */
156 int blocked_open; /* # of blocked opens */
157 unsigned char read_status_mask;
158 unsigned char ignore_status_mask;
160 unsigned char *tx_buf;
165 /* circular list of fixed length rx buffers */
167 unsigned char *rx_buf; /* memory allocated for all rx buffers */
168 int rx_buf_total_size; /* size of memory allocated for rx buffers */
169 int rx_put; /* index of next empty rx buffer */
170 int rx_get; /* index of next full rx buffer */
171 int rx_buf_size; /* size in bytes of single rx buffer */
172 int rx_buf_count; /* total number of rx buffers */
173 int rx_frame_count; /* number of full rx buffers */
175 wait_queue_head_t open_wait;
176 wait_queue_head_t close_wait;
178 wait_queue_head_t status_event_wait_q;
179 wait_queue_head_t event_wait_q;
180 struct timer_list tx_timer; /* HDLC transmit timeout timer */
181 struct _mgslpc_info *next_device; /* device list link */
183 unsigned short imra_value;
184 unsigned short imrb_value;
185 unsigned char pim_value;
188 struct work_struct task; /* task structure for scheduling bh */
197 int dcd_chkcount; /* check counts to prevent */
198 int cts_chkcount; /* too many IRQs if a signal */
199 int dsr_chkcount; /* is floating */
210 int if_mode; /* serial interface selection (RS-232, v.35 etc) */
212 char device_name[25]; /* device instance name */
214 unsigned int io_base; /* base I/O address of adapter */
215 unsigned int irq_level;
217 MGSL_PARAMS params; /* communications parameters */
219 unsigned char serial_signals; /* current serial signal states */
221 char irq_occurred; /* for diagnostics use */
223 unsigned int init_error; /* startup error (DIAGS) */
225 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
226 BOOLEAN drop_rts_on_tx_done;
228 struct _input_signal_events input_signal_events;
235 /* SPPP/Cisco HDLC device parts */
241 struct net_device *netdev;
246 #define MGSLPC_MAGIC 0x5402
249 * The size of the serial xmit buffer is 1 page, or 4096 bytes
251 #define TXBUFSIZE 4096
254 #define CHA 0x00 /* channel A offset */
255 #define CHB 0x40 /* channel B offset */
258 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
305 #define IRQ_BREAK_ON BIT15 // rx break detected
306 #define IRQ_DATAOVERRUN BIT14 // receive data overflow
307 #define IRQ_ALLSENT BIT13 // all sent
308 #define IRQ_UNDERRUN BIT12 // transmit data underrun
309 #define IRQ_TIMER BIT11 // timer interrupt
310 #define IRQ_CTS BIT10 // CTS status change
311 #define IRQ_TXREPEAT BIT9 // tx message repeat
312 #define IRQ_TXFIFO BIT8 // transmit pool ready
313 #define IRQ_RXEOM BIT7 // receive message end
314 #define IRQ_EXITHUNT BIT6 // receive frame start
315 #define IRQ_RXTIME BIT6 // rx char timeout
316 #define IRQ_DCD BIT2 // carrier detect status change
317 #define IRQ_OVERRUN BIT1 // receive frame overflow
318 #define IRQ_RXFIFO BIT0 // receive pool full
322 #define XFW BIT6 // transmit FIFO write enable
323 #define CEC BIT2 // command executing
324 #define CTS BIT1 // CTS state
329 #define PVR_AUTOCTS BIT3
330 #define PVR_RS232 0x20 /* 0010b */
331 #define PVR_V35 0xe0 /* 1110b */
332 #define PVR_RS422 0x40 /* 0100b */
334 /* Register access functions */
336 #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
337 #define read_reg(info, reg) inb((info)->io_base + (reg))
339 #define read_reg16(info, reg) inw((info)->io_base + (reg))
340 #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
342 #define set_reg_bits(info, reg, mask) \
343 write_reg(info, (reg), \
344 (unsigned char) (read_reg(info, (reg)) | (mask)))
345 #define clear_reg_bits(info, reg, mask) \
346 write_reg(info, (reg), \
347 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
349 * interrupt enable/disable routines
351 static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
353 if (channel == CHA) {
354 info->imra_value |= mask;
355 write_reg16(info, CHA + IMR, info->imra_value);
357 info->imrb_value |= mask;
358 write_reg16(info, CHB + IMR, info->imrb_value);
361 static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
363 if (channel == CHA) {
364 info->imra_value &= ~mask;
365 write_reg16(info, CHA + IMR, info->imra_value);
367 info->imrb_value &= ~mask;
368 write_reg16(info, CHB + IMR, info->imrb_value);
372 #define port_irq_disable(info, mask) \
373 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
375 #define port_irq_enable(info, mask) \
376 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
378 static void rx_start(MGSLPC_INFO *info);
379 static void rx_stop(MGSLPC_INFO *info);
381 static void tx_start(MGSLPC_INFO *info);
382 static void tx_stop(MGSLPC_INFO *info);
383 static void tx_set_idle(MGSLPC_INFO *info);
385 static void get_signals(MGSLPC_INFO *info);
386 static void set_signals(MGSLPC_INFO *info);
388 static void reset_device(MGSLPC_INFO *info);
390 static void hdlc_mode(MGSLPC_INFO *info);
391 static void async_mode(MGSLPC_INFO *info);
393 static void tx_timeout(unsigned long context);
395 static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg);
398 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
399 static void hdlcdev_tx_done(MGSLPC_INFO *info);
400 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
401 static int hdlcdev_init(MGSLPC_INFO *info);
402 static void hdlcdev_exit(MGSLPC_INFO *info);
405 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
407 static BOOLEAN register_test(MGSLPC_INFO *info);
408 static BOOLEAN irq_test(MGSLPC_INFO *info);
409 static int adapter_test(MGSLPC_INFO *info);
411 static int claim_resources(MGSLPC_INFO *info);
412 static void release_resources(MGSLPC_INFO *info);
413 static void mgslpc_add_device(MGSLPC_INFO *info);
414 static void mgslpc_remove_device(MGSLPC_INFO *info);
416 static int rx_get_frame(MGSLPC_INFO *info);
417 static void rx_reset_buffers(MGSLPC_INFO *info);
418 static int rx_alloc_buffers(MGSLPC_INFO *info);
419 static void rx_free_buffers(MGSLPC_INFO *info);
421 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs);
424 * Bottom half interrupt handlers
426 static void bh_handler(void* Context);
427 static void bh_transmit(MGSLPC_INFO *info);
428 static void bh_status(MGSLPC_INFO *info);
433 static int tiocmget(struct tty_struct *tty, struct file *file);
434 static int tiocmset(struct tty_struct *tty, struct file *file,
435 unsigned int set, unsigned int clear);
436 static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
437 static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
438 static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params);
439 static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
440 static int set_txidle(MGSLPC_INFO *info, int idle_mode);
441 static int set_txenable(MGSLPC_INFO *info, int enable);
442 static int tx_abort(MGSLPC_INFO *info);
443 static int set_rxenable(MGSLPC_INFO *info, int enable);
444 static int wait_events(MGSLPC_INFO *info, int __user *mask);
446 static MGSLPC_INFO *mgslpc_device_list = NULL;
447 static int mgslpc_device_count = 0;
450 * Set this param to non-zero to load eax with the
451 * .text section address and breakpoint on module load.
452 * This is useful for use with gdb and add-symbol-file command.
454 static int break_on_load=0;
457 * Driver major number, defaults to zero to get auto
458 * assigned major number. May be forced as module parameter.
460 static int ttymajor=0;
462 static int debug_level = 0;
463 static int maxframe[MAX_DEVICE_COUNT] = {0,};
464 static int dosyncppp[MAX_DEVICE_COUNT] = {1,1,1,1};
466 module_param(break_on_load, bool, 0);
467 module_param(ttymajor, int, 0);
468 module_param(debug_level, int, 0);
469 module_param_array(maxframe, int, NULL, 0);
470 module_param_array(dosyncppp, int, NULL, 0);
472 MODULE_LICENSE("GPL");
474 static char *driver_name = "SyncLink PC Card driver";
475 static char *driver_version = "$Revision: 4.34 $";
477 static struct tty_driver *serial_driver;
479 /* number of characters left in xmit buffer before we ask for more */
480 #define WAKEUP_CHARS 256
482 static void mgslpc_change_params(MGSLPC_INFO *info);
483 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
485 /* PCMCIA prototypes */
487 static void mgslpc_config(dev_link_t *link);
488 static void mgslpc_release(u_long arg);
489 static void mgslpc_detach(struct pcmcia_device *p_dev);
492 * 1st function defined in .text section. Calling this function in
493 * init_module() followed by a breakpoint allows a remote debugger
494 * (gdb) to get the .text address for the add-symbol-file command.
495 * This allows remote debugging of dynamically loadable modules.
497 static void* mgslpc_get_text_ptr(void)
499 return mgslpc_get_text_ptr;
503 * line discipline callback wrappers
505 * The wrappers maintain line discipline references
506 * while calling into the line discipline.
508 * ldisc_flush_buffer - flush line discipline receive buffers
509 * ldisc_receive_buf - pass receive data to line discipline
512 static void ldisc_flush_buffer(struct tty_struct *tty)
514 struct tty_ldisc *ld = tty_ldisc_ref(tty);
516 if (ld->flush_buffer)
517 ld->flush_buffer(tty);
522 static void ldisc_receive_buf(struct tty_struct *tty,
523 const __u8 *data, char *flags, int count)
525 struct tty_ldisc *ld;
528 ld = tty_ldisc_ref(tty);
531 ld->receive_buf(tty, data, flags, count);
536 static int mgslpc_attach(struct pcmcia_device *p_dev)
541 if (debug_level >= DEBUG_LEVEL_INFO)
542 printk("mgslpc_attach\n");
544 info = (MGSLPC_INFO *)kmalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
546 printk("Error can't allocate device instance data\n");
550 memset(info, 0, sizeof(MGSLPC_INFO));
551 info->magic = MGSLPC_MAGIC;
552 INIT_WORK(&info->task, bh_handler, info);
553 info->max_frame_size = 4096;
554 info->close_delay = 5*HZ/10;
555 info->closing_wait = 30*HZ;
556 init_waitqueue_head(&info->open_wait);
557 init_waitqueue_head(&info->close_wait);
558 init_waitqueue_head(&info->status_event_wait_q);
559 init_waitqueue_head(&info->event_wait_q);
560 spin_lock_init(&info->lock);
561 spin_lock_init(&info->netlock);
562 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
563 info->idle_mode = HDLC_TXIDLE_FLAGS;
564 info->imra_value = 0xffff;
565 info->imrb_value = 0xffff;
566 info->pim_value = 0xff;
571 /* Initialize the dev_link_t structure */
573 /* Interrupt setup */
574 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
575 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
576 link->irq.Handler = NULL;
578 link->conf.Attributes = 0;
579 link->conf.IntType = INT_MEMORY_AND_IO;
581 link->handle = p_dev;
582 p_dev->instance = link;
584 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
587 mgslpc_add_device(info);
592 /* Card has been inserted.
595 #define CS_CHECK(fn, ret) \
596 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
598 static void mgslpc_config(dev_link_t *link)
600 client_handle_t handle = link->handle;
601 MGSLPC_INFO *info = link->priv;
604 int last_fn, last_ret;
606 cistpl_cftable_entry_t dflt = { 0 };
607 cistpl_cftable_entry_t *cfg;
609 if (debug_level >= DEBUG_LEVEL_INFO)
610 printk("mgslpc_config(0x%p)\n", link);
612 /* read CONFIG tuple to find its configuration registers */
613 tuple.DesiredTuple = CISTPL_CONFIG;
614 tuple.Attributes = 0;
615 tuple.TupleData = buf;
616 tuple.TupleDataMax = sizeof(buf);
617 tuple.TupleOffset = 0;
618 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
619 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
620 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
621 link->conf.ConfigBase = parse.config.base;
622 link->conf.Present = parse.config.rmask[0];
625 link->state |= DEV_CONFIG;
627 /* get CIS configuration entry */
629 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
630 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
632 cfg = &(parse.cftable_entry);
633 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
634 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
636 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
640 link->conf.ConfigIndex = cfg->index;
641 link->conf.Attributes |= CONF_ENABLE_IRQ;
643 /* IO window settings */
644 link->io.NumPorts1 = 0;
645 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
646 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
647 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
648 if (!(io->flags & CISTPL_IO_8BIT))
649 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
650 if (!(io->flags & CISTPL_IO_16BIT))
651 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
652 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
653 link->io.BasePort1 = io->win[0].base;
654 link->io.NumPorts1 = io->win[0].len;
655 CS_CHECK(RequestIO, pcmcia_request_io(link->handle, &link->io));
658 link->conf.Attributes = CONF_ENABLE_IRQ;
659 link->conf.IntType = INT_MEMORY_AND_IO;
660 link->conf.ConfigIndex = 8;
661 link->conf.Present = PRESENT_OPTION;
663 link->irq.Attributes |= IRQ_HANDLE_PRESENT;
664 link->irq.Handler = mgslpc_isr;
665 link->irq.Instance = info;
666 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
668 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
670 info->io_base = link->io.BasePort1;
671 info->irq_level = link->irq.AssignedIRQ;
673 /* add to linked list of devices */
674 sprintf(info->node.dev_name, "mgslpc0");
675 info->node.major = info->node.minor = 0;
676 link->dev = &info->node;
678 printk(KERN_INFO "%s: index 0x%02x:",
679 info->node.dev_name, link->conf.ConfigIndex);
680 if (link->conf.Attributes & CONF_ENABLE_IRQ)
681 printk(", irq %d", link->irq.AssignedIRQ);
682 if (link->io.NumPorts1)
683 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
684 link->io.BasePort1+link->io.NumPorts1-1);
687 link->state &= ~DEV_CONFIG_PENDING;
691 cs_error(link->handle, last_fn, last_ret);
692 mgslpc_release((u_long)link);
695 /* Card has been removed.
696 * Unregister device and release PCMCIA configuration.
697 * If device is open, postpone until it is closed.
699 static void mgslpc_release(u_long arg)
701 dev_link_t *link = (dev_link_t *)arg;
703 if (debug_level >= DEBUG_LEVEL_INFO)
704 printk("mgslpc_release(0x%p)\n", link);
706 pcmcia_disable_device(link->handle);
709 static void mgslpc_detach(struct pcmcia_device *p_dev)
711 dev_link_t *link = dev_to_instance(p_dev);
713 if (debug_level >= DEBUG_LEVEL_INFO)
714 printk("mgslpc_detach(0x%p)\n", link);
716 if (link->state & DEV_CONFIG) {
717 ((MGSLPC_INFO *)link->priv)->stop = 1;
718 mgslpc_release((u_long)link);
721 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
724 static int mgslpc_suspend(struct pcmcia_device *dev)
726 dev_link_t *link = dev_to_instance(dev);
727 MGSLPC_INFO *info = link->priv;
734 static int mgslpc_resume(struct pcmcia_device *dev)
736 dev_link_t *link = dev_to_instance(dev);
737 MGSLPC_INFO *info = link->priv;
745 static inline int mgslpc_paranoia_check(MGSLPC_INFO *info,
746 char *name, const char *routine)
748 #ifdef MGSLPC_PARANOIA_CHECK
749 static const char *badmagic =
750 "Warning: bad magic number for mgsl struct (%s) in %s\n";
751 static const char *badinfo =
752 "Warning: null mgslpc_info for (%s) in %s\n";
755 printk(badinfo, name, routine);
758 if (info->magic != MGSLPC_MAGIC) {
759 printk(badmagic, name, routine);
770 #define CMD_RXFIFO BIT7 // release current rx FIFO
771 #define CMD_RXRESET BIT6 // receiver reset
772 #define CMD_RXFIFO_READ BIT5
773 #define CMD_START_TIMER BIT4
774 #define CMD_TXFIFO BIT3 // release current tx FIFO
775 #define CMD_TXEOM BIT1 // transmit end message
776 #define CMD_TXRESET BIT0 // transmit reset
778 static BOOLEAN wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
781 /* wait for command completion */
782 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
790 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
792 wait_command_complete(info, channel);
793 write_reg(info, (unsigned char) (channel + CMDR), cmd);
796 static void tx_pause(struct tty_struct *tty)
798 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
801 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
803 if (debug_level >= DEBUG_LEVEL_INFO)
804 printk("tx_pause(%s)\n",info->device_name);
806 spin_lock_irqsave(&info->lock,flags);
807 if (info->tx_enabled)
809 spin_unlock_irqrestore(&info->lock,flags);
812 static void tx_release(struct tty_struct *tty)
814 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
817 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
819 if (debug_level >= DEBUG_LEVEL_INFO)
820 printk("tx_release(%s)\n",info->device_name);
822 spin_lock_irqsave(&info->lock,flags);
823 if (!info->tx_enabled)
825 spin_unlock_irqrestore(&info->lock,flags);
828 /* Return next bottom half action to perform.
829 * or 0 if nothing to do.
831 static int bh_action(MGSLPC_INFO *info)
836 spin_lock_irqsave(&info->lock,flags);
838 if (info->pending_bh & BH_RECEIVE) {
839 info->pending_bh &= ~BH_RECEIVE;
841 } else if (info->pending_bh & BH_TRANSMIT) {
842 info->pending_bh &= ~BH_TRANSMIT;
844 } else if (info->pending_bh & BH_STATUS) {
845 info->pending_bh &= ~BH_STATUS;
850 /* Mark BH routine as complete */
851 info->bh_running = 0;
852 info->bh_requested = 0;
855 spin_unlock_irqrestore(&info->lock,flags);
860 void bh_handler(void* Context)
862 MGSLPC_INFO *info = (MGSLPC_INFO*)Context;
868 if (debug_level >= DEBUG_LEVEL_BH)
869 printk( "%s(%d):bh_handler(%s) entry\n",
870 __FILE__,__LINE__,info->device_name);
872 info->bh_running = 1;
874 while((action = bh_action(info)) != 0) {
876 /* Process work item */
877 if ( debug_level >= DEBUG_LEVEL_BH )
878 printk( "%s(%d):bh_handler() work item action=%d\n",
879 __FILE__,__LINE__,action);
884 while(rx_get_frame(info));
893 /* unknown work item ID */
894 printk("Unknown work item ID=%08X!\n", action);
899 if (debug_level >= DEBUG_LEVEL_BH)
900 printk( "%s(%d):bh_handler(%s) exit\n",
901 __FILE__,__LINE__,info->device_name);
904 void bh_transmit(MGSLPC_INFO *info)
906 struct tty_struct *tty = info->tty;
907 if (debug_level >= DEBUG_LEVEL_BH)
908 printk("bh_transmit() entry on %s\n", info->device_name);
912 wake_up_interruptible(&tty->write_wait);
916 void bh_status(MGSLPC_INFO *info)
918 info->ri_chkcount = 0;
919 info->dsr_chkcount = 0;
920 info->dcd_chkcount = 0;
921 info->cts_chkcount = 0;
924 /* eom: non-zero = end of frame */
925 static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
927 unsigned char data[2];
928 unsigned char fifo_count, read_count, i;
929 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
931 if (debug_level >= DEBUG_LEVEL_ISR)
932 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
934 if (!info->rx_enabled)
937 if (info->rx_frame_count >= info->rx_buf_count) {
938 /* no more free buffers */
939 issue_command(info, CHA, CMD_RXRESET);
940 info->pending_bh |= BH_RECEIVE;
941 info->rx_overflow = 1;
942 info->icount.buf_overrun++;
947 /* end of frame, get FIFO count from RBCL register */
948 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
954 if (fifo_count == 1) {
956 data[0] = read_reg(info, CHA + RXFIFO);
959 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
961 fifo_count -= read_count;
962 if (!fifo_count && eom)
963 buf->status = data[--read_count];
965 for (i = 0; i < read_count; i++) {
966 if (buf->count >= info->max_frame_size) {
967 /* frame too large, reset receiver and reset current buffer */
968 issue_command(info, CHA, CMD_RXRESET);
972 *(buf->data + buf->count) = data[i];
975 } while (fifo_count);
978 info->pending_bh |= BH_RECEIVE;
979 info->rx_frame_count++;
981 if (info->rx_put >= info->rx_buf_count)
984 issue_command(info, CHA, CMD_RXFIFO);
987 static void rx_ready_async(MGSLPC_INFO *info, int tcd)
989 unsigned char data, status, flag;
992 struct tty_struct *tty = info->tty;
993 struct mgsl_icount *icount = &info->icount;
996 /* early termination, get FIFO count from RBCL register */
997 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
999 /* Zero fifo count could mean 0 or 32 bytes available.
1000 * If BIT5 of STAR is set then at least 1 byte is available.
1002 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
1007 tty_buffer_request_room(tty, fifo_count);
1008 /* Flush received async data to receive data buffer. */
1009 while (fifo_count) {
1010 data = read_reg(info, CHA + RXFIFO);
1011 status = read_reg(info, CHA + RXFIFO);
1017 // if no frameing/crc error then save data
1018 // BIT7:parity error
1019 // BIT6:framing error
1021 if (status & (BIT7 + BIT6)) {
1027 /* discard char if tty control flags say so */
1028 if (status & info->ignore_status_mask)
1031 status &= info->read_status_mask;
1035 else if (status & BIT6)
1038 work += tty_insert_flip_char(tty, data, flag);
1040 issue_command(info, CHA, CMD_RXFIFO);
1042 if (debug_level >= DEBUG_LEVEL_ISR) {
1043 printk("%s(%d):rx_ready_async",
1045 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1046 __FILE__,__LINE__,icount->rx,icount->brk,
1047 icount->parity,icount->frame,icount->overrun);
1051 tty_flip_buffer_push(tty);
1055 static void tx_done(MGSLPC_INFO *info)
1057 if (!info->tx_active)
1060 info->tx_active = 0;
1061 info->tx_aborting = 0;
1063 if (info->params.mode == MGSL_MODE_ASYNC)
1066 info->tx_count = info->tx_put = info->tx_get = 0;
1067 del_timer(&info->tx_timer);
1069 if (info->drop_rts_on_tx_done) {
1071 if (info->serial_signals & SerialSignal_RTS) {
1072 info->serial_signals &= ~SerialSignal_RTS;
1075 info->drop_rts_on_tx_done = 0;
1080 hdlcdev_tx_done(info);
1084 if (info->tty->stopped || info->tty->hw_stopped) {
1088 info->pending_bh |= BH_TRANSMIT;
1092 static void tx_ready(MGSLPC_INFO *info)
1094 unsigned char fifo_count = 32;
1097 if (debug_level >= DEBUG_LEVEL_ISR)
1098 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1100 if (info->params.mode == MGSL_MODE_HDLC) {
1101 if (!info->tx_active)
1104 if (info->tty->stopped || info->tty->hw_stopped) {
1108 if (!info->tx_count)
1109 info->tx_active = 0;
1112 if (!info->tx_count)
1115 while (info->tx_count && fifo_count) {
1116 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
1119 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1121 write_reg16(info, CHA + TXFIFO,
1122 *((unsigned short*)(info->tx_buf + info->tx_get)));
1124 info->tx_count -= c;
1125 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1129 if (info->params.mode == MGSL_MODE_ASYNC) {
1130 if (info->tx_count < WAKEUP_CHARS)
1131 info->pending_bh |= BH_TRANSMIT;
1132 issue_command(info, CHA, CMD_TXFIFO);
1135 issue_command(info, CHA, CMD_TXFIFO);
1137 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1141 static void cts_change(MGSLPC_INFO *info)
1144 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1145 irq_disable(info, CHB, IRQ_CTS);
1147 if (info->serial_signals & SerialSignal_CTS)
1148 info->input_signal_events.cts_up++;
1150 info->input_signal_events.cts_down++;
1151 wake_up_interruptible(&info->status_event_wait_q);
1152 wake_up_interruptible(&info->event_wait_q);
1154 if (info->flags & ASYNC_CTS_FLOW) {
1155 if (info->tty->hw_stopped) {
1156 if (info->serial_signals & SerialSignal_CTS) {
1157 if (debug_level >= DEBUG_LEVEL_ISR)
1158 printk("CTS tx start...");
1160 info->tty->hw_stopped = 0;
1162 info->pending_bh |= BH_TRANSMIT;
1166 if (!(info->serial_signals & SerialSignal_CTS)) {
1167 if (debug_level >= DEBUG_LEVEL_ISR)
1168 printk("CTS tx stop...");
1170 info->tty->hw_stopped = 1;
1175 info->pending_bh |= BH_STATUS;
1178 static void dcd_change(MGSLPC_INFO *info)
1181 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1182 irq_disable(info, CHB, IRQ_DCD);
1184 if (info->serial_signals & SerialSignal_DCD) {
1185 info->input_signal_events.dcd_up++;
1188 info->input_signal_events.dcd_down++;
1191 hdlc_set_carrier(info->serial_signals & SerialSignal_DCD, info->netdev);
1193 wake_up_interruptible(&info->status_event_wait_q);
1194 wake_up_interruptible(&info->event_wait_q);
1196 if (info->flags & ASYNC_CHECK_CD) {
1197 if (debug_level >= DEBUG_LEVEL_ISR)
1198 printk("%s CD now %s...", info->device_name,
1199 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1200 if (info->serial_signals & SerialSignal_DCD)
1201 wake_up_interruptible(&info->open_wait);
1203 if (debug_level >= DEBUG_LEVEL_ISR)
1204 printk("doing serial hangup...");
1206 tty_hangup(info->tty);
1209 info->pending_bh |= BH_STATUS;
1212 static void dsr_change(MGSLPC_INFO *info)
1215 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1216 port_irq_disable(info, PVR_DSR);
1218 if (info->serial_signals & SerialSignal_DSR)
1219 info->input_signal_events.dsr_up++;
1221 info->input_signal_events.dsr_down++;
1222 wake_up_interruptible(&info->status_event_wait_q);
1223 wake_up_interruptible(&info->event_wait_q);
1224 info->pending_bh |= BH_STATUS;
1227 static void ri_change(MGSLPC_INFO *info)
1230 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1231 port_irq_disable(info, PVR_RI);
1233 if (info->serial_signals & SerialSignal_RI)
1234 info->input_signal_events.ri_up++;
1236 info->input_signal_events.ri_down++;
1237 wake_up_interruptible(&info->status_event_wait_q);
1238 wake_up_interruptible(&info->event_wait_q);
1239 info->pending_bh |= BH_STATUS;
1242 /* Interrupt service routine entry point.
1246 * irq interrupt number that caused interrupt
1247 * dev_id device ID supplied during interrupt registration
1248 * regs interrupted processor context
1250 static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs)
1252 MGSLPC_INFO * info = (MGSLPC_INFO *)dev_id;
1254 unsigned char gis, pis;
1257 if (debug_level >= DEBUG_LEVEL_ISR)
1258 printk("mgslpc_isr(%d) entry.\n", irq);
1262 if (!(info->link.state & DEV_CONFIG))
1265 spin_lock(&info->lock);
1267 while ((gis = read_reg(info, CHA + GIS))) {
1268 if (debug_level >= DEBUG_LEVEL_ISR)
1269 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1271 if ((gis & 0x70) || count > 1000) {
1272 printk("synclink_cs:hardware failed or ejected\n");
1277 if (gis & (BIT1 + BIT0)) {
1278 isr = read_reg16(info, CHB + ISR);
1284 if (gis & (BIT3 + BIT2))
1286 isr = read_reg16(info, CHA + ISR);
1287 if (isr & IRQ_TIMER) {
1288 info->irq_occurred = 1;
1289 irq_disable(info, CHA, IRQ_TIMER);
1293 if (isr & IRQ_EXITHUNT) {
1294 info->icount.exithunt++;
1295 wake_up_interruptible(&info->event_wait_q);
1297 if (isr & IRQ_BREAK_ON) {
1299 if (info->flags & ASYNC_SAK)
1302 if (isr & IRQ_RXTIME) {
1303 issue_command(info, CHA, CMD_RXFIFO_READ);
1305 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1306 if (info->params.mode == MGSL_MODE_HDLC)
1307 rx_ready_hdlc(info, isr & IRQ_RXEOM);
1309 rx_ready_async(info, isr & IRQ_RXEOM);
1313 if (isr & IRQ_UNDERRUN) {
1314 if (info->tx_aborting)
1315 info->icount.txabort++;
1317 info->icount.txunder++;
1320 else if (isr & IRQ_ALLSENT) {
1321 info->icount.txok++;
1324 else if (isr & IRQ_TXFIFO)
1328 pis = read_reg(info, CHA + PIS);
1336 /* Request bottom half processing if there's something
1337 * for it to do and the bh is not already running
1340 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1341 if ( debug_level >= DEBUG_LEVEL_ISR )
1342 printk("%s(%d):%s queueing bh task.\n",
1343 __FILE__,__LINE__,info->device_name);
1344 schedule_work(&info->task);
1345 info->bh_requested = 1;
1348 spin_unlock(&info->lock);
1350 if (debug_level >= DEBUG_LEVEL_ISR)
1351 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1352 __FILE__,__LINE__,irq);
1357 /* Initialize and start device.
1359 static int startup(MGSLPC_INFO * info)
1363 if (debug_level >= DEBUG_LEVEL_INFO)
1364 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1366 if (info->flags & ASYNC_INITIALIZED)
1369 if (!info->tx_buf) {
1370 /* allocate a page of memory for a transmit buffer */
1371 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1372 if (!info->tx_buf) {
1373 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1374 __FILE__,__LINE__,info->device_name);
1379 info->pending_bh = 0;
1381 memset(&info->icount, 0, sizeof(info->icount));
1383 init_timer(&info->tx_timer);
1384 info->tx_timer.data = (unsigned long)info;
1385 info->tx_timer.function = tx_timeout;
1387 /* Allocate and claim adapter resources */
1388 retval = claim_resources(info);
1390 /* perform existance check and diagnostics */
1392 retval = adapter_test(info);
1395 if (capable(CAP_SYS_ADMIN) && info->tty)
1396 set_bit(TTY_IO_ERROR, &info->tty->flags);
1397 release_resources(info);
1401 /* program hardware for current parameters */
1402 mgslpc_change_params(info);
1405 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1407 info->flags |= ASYNC_INITIALIZED;
1412 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1414 static void shutdown(MGSLPC_INFO * info)
1416 unsigned long flags;
1418 if (!(info->flags & ASYNC_INITIALIZED))
1421 if (debug_level >= DEBUG_LEVEL_INFO)
1422 printk("%s(%d):mgslpc_shutdown(%s)\n",
1423 __FILE__,__LINE__, info->device_name );
1425 /* clear status wait queue because status changes */
1426 /* can't happen after shutting down the hardware */
1427 wake_up_interruptible(&info->status_event_wait_q);
1428 wake_up_interruptible(&info->event_wait_q);
1430 del_timer(&info->tx_timer);
1433 free_page((unsigned long) info->tx_buf);
1434 info->tx_buf = NULL;
1437 spin_lock_irqsave(&info->lock,flags);
1442 /* TODO:disable interrupts instead of reset to preserve signal states */
1445 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1446 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1450 spin_unlock_irqrestore(&info->lock,flags);
1452 release_resources(info);
1455 set_bit(TTY_IO_ERROR, &info->tty->flags);
1457 info->flags &= ~ASYNC_INITIALIZED;
1460 static void mgslpc_program_hw(MGSLPC_INFO *info)
1462 unsigned long flags;
1464 spin_lock_irqsave(&info->lock,flags);
1468 info->tx_count = info->tx_put = info->tx_get = 0;
1470 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1477 info->dcd_chkcount = 0;
1478 info->cts_chkcount = 0;
1479 info->ri_chkcount = 0;
1480 info->dsr_chkcount = 0;
1482 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1483 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1486 if (info->netcount || info->tty->termios->c_cflag & CREAD)
1489 spin_unlock_irqrestore(&info->lock,flags);
1492 /* Reconfigure adapter based on new parameters
1494 static void mgslpc_change_params(MGSLPC_INFO *info)
1499 if (!info->tty || !info->tty->termios)
1502 if (debug_level >= DEBUG_LEVEL_INFO)
1503 printk("%s(%d):mgslpc_change_params(%s)\n",
1504 __FILE__,__LINE__, info->device_name );
1506 cflag = info->tty->termios->c_cflag;
1508 /* if B0 rate (hangup) specified then negate DTR and RTS */
1509 /* otherwise assert DTR and RTS */
1511 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1513 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1515 /* byte size and parity */
1517 switch (cflag & CSIZE) {
1518 case CS5: info->params.data_bits = 5; break;
1519 case CS6: info->params.data_bits = 6; break;
1520 case CS7: info->params.data_bits = 7; break;
1521 case CS8: info->params.data_bits = 8; break;
1522 default: info->params.data_bits = 7; break;
1526 info->params.stop_bits = 2;
1528 info->params.stop_bits = 1;
1530 info->params.parity = ASYNC_PARITY_NONE;
1531 if (cflag & PARENB) {
1533 info->params.parity = ASYNC_PARITY_ODD;
1535 info->params.parity = ASYNC_PARITY_EVEN;
1538 info->params.parity = ASYNC_PARITY_SPACE;
1542 /* calculate number of jiffies to transmit a full
1543 * FIFO (32 bytes) at specified data rate
1545 bits_per_char = info->params.data_bits +
1546 info->params.stop_bits + 1;
1548 /* if port data rate is set to 460800 or less then
1549 * allow tty settings to override, otherwise keep the
1550 * current data rate.
1552 if (info->params.data_rate <= 460800) {
1553 info->params.data_rate = tty_get_baud_rate(info->tty);
1556 if ( info->params.data_rate ) {
1557 info->timeout = (32*HZ*bits_per_char) /
1558 info->params.data_rate;
1560 info->timeout += HZ/50; /* Add .02 seconds of slop */
1562 if (cflag & CRTSCTS)
1563 info->flags |= ASYNC_CTS_FLOW;
1565 info->flags &= ~ASYNC_CTS_FLOW;
1568 info->flags &= ~ASYNC_CHECK_CD;
1570 info->flags |= ASYNC_CHECK_CD;
1572 /* process tty input control flags */
1574 info->read_status_mask = 0;
1575 if (I_INPCK(info->tty))
1576 info->read_status_mask |= BIT7 | BIT6;
1577 if (I_IGNPAR(info->tty))
1578 info->ignore_status_mask |= BIT7 | BIT6;
1580 mgslpc_program_hw(info);
1583 /* Add a character to the transmit buffer
1585 static void mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1587 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1588 unsigned long flags;
1590 if (debug_level >= DEBUG_LEVEL_INFO) {
1591 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1592 __FILE__,__LINE__,ch,info->device_name);
1595 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1598 if (!tty || !info->tx_buf)
1601 spin_lock_irqsave(&info->lock,flags);
1603 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1604 if (info->tx_count < TXBUFSIZE - 1) {
1605 info->tx_buf[info->tx_put++] = ch;
1606 info->tx_put &= TXBUFSIZE-1;
1611 spin_unlock_irqrestore(&info->lock,flags);
1614 /* Enable transmitter so remaining characters in the
1615 * transmit buffer are sent.
1617 static void mgslpc_flush_chars(struct tty_struct *tty)
1619 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1620 unsigned long flags;
1622 if (debug_level >= DEBUG_LEVEL_INFO)
1623 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1624 __FILE__,__LINE__,info->device_name,info->tx_count);
1626 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1629 if (info->tx_count <= 0 || tty->stopped ||
1630 tty->hw_stopped || !info->tx_buf)
1633 if (debug_level >= DEBUG_LEVEL_INFO)
1634 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1635 __FILE__,__LINE__,info->device_name);
1637 spin_lock_irqsave(&info->lock,flags);
1638 if (!info->tx_active)
1640 spin_unlock_irqrestore(&info->lock,flags);
1643 /* Send a block of data
1647 * tty pointer to tty information structure
1648 * buf pointer to buffer containing send data
1649 * count size of send data in bytes
1651 * Returns: number of characters written
1653 static int mgslpc_write(struct tty_struct * tty,
1654 const unsigned char *buf, int count)
1657 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1658 unsigned long flags;
1660 if (debug_level >= DEBUG_LEVEL_INFO)
1661 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1662 __FILE__,__LINE__,info->device_name,count);
1664 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1665 !tty || !info->tx_buf)
1668 if (info->params.mode == MGSL_MODE_HDLC) {
1669 if (count > TXBUFSIZE) {
1673 if (info->tx_active)
1675 else if (info->tx_count)
1681 min(TXBUFSIZE - info->tx_count - 1,
1682 TXBUFSIZE - info->tx_put));
1686 memcpy(info->tx_buf + info->tx_put, buf, c);
1688 spin_lock_irqsave(&info->lock,flags);
1689 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1690 info->tx_count += c;
1691 spin_unlock_irqrestore(&info->lock,flags);
1698 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1699 spin_lock_irqsave(&info->lock,flags);
1700 if (!info->tx_active)
1702 spin_unlock_irqrestore(&info->lock,flags);
1705 if (debug_level >= DEBUG_LEVEL_INFO)
1706 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1707 __FILE__,__LINE__,info->device_name,ret);
1711 /* Return the count of free bytes in transmit buffer
1713 static int mgslpc_write_room(struct tty_struct *tty)
1715 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1718 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1721 if (info->params.mode == MGSL_MODE_HDLC) {
1722 /* HDLC (frame oriented) mode */
1723 if (info->tx_active)
1726 return HDLC_MAX_FRAME_SIZE;
1728 ret = TXBUFSIZE - info->tx_count - 1;
1733 if (debug_level >= DEBUG_LEVEL_INFO)
1734 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1735 __FILE__,__LINE__, info->device_name, ret);
1739 /* Return the count of bytes in transmit buffer
1741 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1743 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1746 if (debug_level >= DEBUG_LEVEL_INFO)
1747 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1748 __FILE__,__LINE__, info->device_name );
1750 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1753 if (info->params.mode == MGSL_MODE_HDLC)
1754 rc = info->tx_active ? info->max_frame_size : 0;
1756 rc = info->tx_count;
1758 if (debug_level >= DEBUG_LEVEL_INFO)
1759 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1760 __FILE__,__LINE__, info->device_name, rc);
1765 /* Discard all data in the send buffer
1767 static void mgslpc_flush_buffer(struct tty_struct *tty)
1769 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1770 unsigned long flags;
1772 if (debug_level >= DEBUG_LEVEL_INFO)
1773 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1774 __FILE__,__LINE__, info->device_name );
1776 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1779 spin_lock_irqsave(&info->lock,flags);
1780 info->tx_count = info->tx_put = info->tx_get = 0;
1781 del_timer(&info->tx_timer);
1782 spin_unlock_irqrestore(&info->lock,flags);
1784 wake_up_interruptible(&tty->write_wait);
1788 /* Send a high-priority XON/XOFF character
1790 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1792 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1793 unsigned long flags;
1795 if (debug_level >= DEBUG_LEVEL_INFO)
1796 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1797 __FILE__,__LINE__, info->device_name, ch );
1799 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1804 spin_lock_irqsave(&info->lock,flags);
1805 if (!info->tx_enabled)
1807 spin_unlock_irqrestore(&info->lock,flags);
1811 /* Signal remote device to throttle send data (our receive data)
1813 static void mgslpc_throttle(struct tty_struct * tty)
1815 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1816 unsigned long flags;
1818 if (debug_level >= DEBUG_LEVEL_INFO)
1819 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1820 __FILE__,__LINE__, info->device_name );
1822 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1826 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1828 if (tty->termios->c_cflag & CRTSCTS) {
1829 spin_lock_irqsave(&info->lock,flags);
1830 info->serial_signals &= ~SerialSignal_RTS;
1832 spin_unlock_irqrestore(&info->lock,flags);
1836 /* Signal remote device to stop throttling send data (our receive data)
1838 static void mgslpc_unthrottle(struct tty_struct * tty)
1840 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1841 unsigned long flags;
1843 if (debug_level >= DEBUG_LEVEL_INFO)
1844 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1845 __FILE__,__LINE__, info->device_name );
1847 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1854 mgslpc_send_xchar(tty, START_CHAR(tty));
1857 if (tty->termios->c_cflag & CRTSCTS) {
1858 spin_lock_irqsave(&info->lock,flags);
1859 info->serial_signals |= SerialSignal_RTS;
1861 spin_unlock_irqrestore(&info->lock,flags);
1865 /* get the current serial statistics
1867 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1870 if (debug_level >= DEBUG_LEVEL_INFO)
1871 printk("get_params(%s)\n", info->device_name);
1873 memset(&info->icount, 0, sizeof(info->icount));
1875 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1882 /* get the current serial parameters
1884 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1887 if (debug_level >= DEBUG_LEVEL_INFO)
1888 printk("get_params(%s)\n", info->device_name);
1889 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1895 /* set the serial parameters
1899 * info pointer to device instance data
1900 * new_params user buffer containing new serial params
1902 * Returns: 0 if success, otherwise error code
1904 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params)
1906 unsigned long flags;
1907 MGSL_PARAMS tmp_params;
1910 if (debug_level >= DEBUG_LEVEL_INFO)
1911 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1912 info->device_name );
1913 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1915 if ( debug_level >= DEBUG_LEVEL_INFO )
1916 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1917 __FILE__,__LINE__,info->device_name);
1921 spin_lock_irqsave(&info->lock,flags);
1922 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1923 spin_unlock_irqrestore(&info->lock,flags);
1925 mgslpc_change_params(info);
1930 static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1933 if (debug_level >= DEBUG_LEVEL_INFO)
1934 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1935 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1941 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1943 unsigned long flags;
1944 if (debug_level >= DEBUG_LEVEL_INFO)
1945 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1946 spin_lock_irqsave(&info->lock,flags);
1947 info->idle_mode = idle_mode;
1949 spin_unlock_irqrestore(&info->lock,flags);
1953 static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1956 if (debug_level >= DEBUG_LEVEL_INFO)
1957 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1958 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1964 static int set_interface(MGSLPC_INFO * info, int if_mode)
1966 unsigned long flags;
1968 if (debug_level >= DEBUG_LEVEL_INFO)
1969 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1970 spin_lock_irqsave(&info->lock,flags);
1971 info->if_mode = if_mode;
1973 val = read_reg(info, PVR) & 0x0f;
1974 switch (info->if_mode)
1976 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
1977 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
1978 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
1980 write_reg(info, PVR, val);
1982 spin_unlock_irqrestore(&info->lock,flags);
1986 static int set_txenable(MGSLPC_INFO * info, int enable)
1988 unsigned long flags;
1990 if (debug_level >= DEBUG_LEVEL_INFO)
1991 printk("set_txenable(%s,%d)\n", info->device_name, enable);
1993 spin_lock_irqsave(&info->lock,flags);
1995 if (!info->tx_enabled)
1998 if (info->tx_enabled)
2001 spin_unlock_irqrestore(&info->lock,flags);
2005 static int tx_abort(MGSLPC_INFO * info)
2007 unsigned long flags;
2009 if (debug_level >= DEBUG_LEVEL_INFO)
2010 printk("tx_abort(%s)\n", info->device_name);
2012 spin_lock_irqsave(&info->lock,flags);
2013 if (info->tx_active && info->tx_count &&
2014 info->params.mode == MGSL_MODE_HDLC) {
2015 /* clear data count so FIFO is not filled on next IRQ.
2016 * This results in underrun and abort transmission.
2018 info->tx_count = info->tx_put = info->tx_get = 0;
2019 info->tx_aborting = TRUE;
2021 spin_unlock_irqrestore(&info->lock,flags);
2025 static int set_rxenable(MGSLPC_INFO * info, int enable)
2027 unsigned long flags;
2029 if (debug_level >= DEBUG_LEVEL_INFO)
2030 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
2032 spin_lock_irqsave(&info->lock,flags);
2034 if (!info->rx_enabled)
2037 if (info->rx_enabled)
2040 spin_unlock_irqrestore(&info->lock,flags);
2044 /* wait for specified event to occur
2046 * Arguments: info pointer to device instance data
2047 * mask pointer to bitmask of events to wait for
2048 * Return Value: 0 if successful and bit mask updated with
2049 * of events triggerred,
2050 * otherwise error code
2052 static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
2054 unsigned long flags;
2057 struct mgsl_icount cprev, cnow;
2060 struct _input_signal_events oldsigs, newsigs;
2061 DECLARE_WAITQUEUE(wait, current);
2063 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2067 if (debug_level >= DEBUG_LEVEL_INFO)
2068 printk("wait_events(%s,%d)\n", info->device_name, mask);
2070 spin_lock_irqsave(&info->lock,flags);
2072 /* return immediately if state matches requested events */
2074 s = info->serial_signals;
2076 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2077 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2078 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2079 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2081 spin_unlock_irqrestore(&info->lock,flags);
2085 /* save current irq counts */
2086 cprev = info->icount;
2087 oldsigs = info->input_signal_events;
2089 if ((info->params.mode == MGSL_MODE_HDLC) &&
2090 (mask & MgslEvent_ExitHuntMode))
2091 irq_enable(info, CHA, IRQ_EXITHUNT);
2093 set_current_state(TASK_INTERRUPTIBLE);
2094 add_wait_queue(&info->event_wait_q, &wait);
2096 spin_unlock_irqrestore(&info->lock,flags);
2101 if (signal_pending(current)) {
2106 /* get current irq counts */
2107 spin_lock_irqsave(&info->lock,flags);
2108 cnow = info->icount;
2109 newsigs = info->input_signal_events;
2110 set_current_state(TASK_INTERRUPTIBLE);
2111 spin_unlock_irqrestore(&info->lock,flags);
2113 /* if no change, wait aborted for some reason */
2114 if (newsigs.dsr_up == oldsigs.dsr_up &&
2115 newsigs.dsr_down == oldsigs.dsr_down &&
2116 newsigs.dcd_up == oldsigs.dcd_up &&
2117 newsigs.dcd_down == oldsigs.dcd_down &&
2118 newsigs.cts_up == oldsigs.cts_up &&
2119 newsigs.cts_down == oldsigs.cts_down &&
2120 newsigs.ri_up == oldsigs.ri_up &&
2121 newsigs.ri_down == oldsigs.ri_down &&
2122 cnow.exithunt == cprev.exithunt &&
2123 cnow.rxidle == cprev.rxidle) {
2129 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2130 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2131 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2132 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2133 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2134 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2135 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2136 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2137 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2138 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2146 remove_wait_queue(&info->event_wait_q, &wait);
2147 set_current_state(TASK_RUNNING);
2149 if (mask & MgslEvent_ExitHuntMode) {
2150 spin_lock_irqsave(&info->lock,flags);
2151 if (!waitqueue_active(&info->event_wait_q))
2152 irq_disable(info, CHA, IRQ_EXITHUNT);
2153 spin_unlock_irqrestore(&info->lock,flags);
2157 PUT_USER(rc, events, mask_ptr);
2161 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2163 unsigned long flags;
2165 struct mgsl_icount cprev, cnow;
2166 DECLARE_WAITQUEUE(wait, current);
2168 /* save current irq counts */
2169 spin_lock_irqsave(&info->lock,flags);
2170 cprev = info->icount;
2171 add_wait_queue(&info->status_event_wait_q, &wait);
2172 set_current_state(TASK_INTERRUPTIBLE);
2173 spin_unlock_irqrestore(&info->lock,flags);
2177 if (signal_pending(current)) {
2182 /* get new irq counts */
2183 spin_lock_irqsave(&info->lock,flags);
2184 cnow = info->icount;
2185 set_current_state(TASK_INTERRUPTIBLE);
2186 spin_unlock_irqrestore(&info->lock,flags);
2188 /* if no change, wait aborted for some reason */
2189 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2190 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2195 /* check for change in caller specified modem input */
2196 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2197 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2198 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2199 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2206 remove_wait_queue(&info->status_event_wait_q, &wait);
2207 set_current_state(TASK_RUNNING);
2211 /* return the state of the serial control and status signals
2213 static int tiocmget(struct tty_struct *tty, struct file *file)
2215 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2216 unsigned int result;
2217 unsigned long flags;
2219 spin_lock_irqsave(&info->lock,flags);
2221 spin_unlock_irqrestore(&info->lock,flags);
2223 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2224 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2225 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2226 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2227 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2228 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2230 if (debug_level >= DEBUG_LEVEL_INFO)
2231 printk("%s(%d):%s tiocmget() value=%08X\n",
2232 __FILE__,__LINE__, info->device_name, result );
2236 /* set modem control signals (DTR/RTS)
2238 static int tiocmset(struct tty_struct *tty, struct file *file,
2239 unsigned int set, unsigned int clear)
2241 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2242 unsigned long flags;
2244 if (debug_level >= DEBUG_LEVEL_INFO)
2245 printk("%s(%d):%s tiocmset(%x,%x)\n",
2246 __FILE__,__LINE__,info->device_name, set, clear);
2248 if (set & TIOCM_RTS)
2249 info->serial_signals |= SerialSignal_RTS;
2250 if (set & TIOCM_DTR)
2251 info->serial_signals |= SerialSignal_DTR;
2252 if (clear & TIOCM_RTS)
2253 info->serial_signals &= ~SerialSignal_RTS;
2254 if (clear & TIOCM_DTR)
2255 info->serial_signals &= ~SerialSignal_DTR;
2257 spin_lock_irqsave(&info->lock,flags);
2259 spin_unlock_irqrestore(&info->lock,flags);
2264 /* Set or clear transmit break condition
2266 * Arguments: tty pointer to tty instance data
2267 * break_state -1=set break condition, 0=clear
2269 static void mgslpc_break(struct tty_struct *tty, int break_state)
2271 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2272 unsigned long flags;
2274 if (debug_level >= DEBUG_LEVEL_INFO)
2275 printk("%s(%d):mgslpc_break(%s,%d)\n",
2276 __FILE__,__LINE__, info->device_name, break_state);
2278 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2281 spin_lock_irqsave(&info->lock,flags);
2282 if (break_state == -1)
2283 set_reg_bits(info, CHA+DAFO, BIT6);
2285 clear_reg_bits(info, CHA+DAFO, BIT6);
2286 spin_unlock_irqrestore(&info->lock,flags);
2289 /* Service an IOCTL request
2293 * tty pointer to tty instance data
2294 * file pointer to associated file object for device
2295 * cmd IOCTL command code
2296 * arg command argument/context
2298 * Return Value: 0 if success, otherwise error code
2300 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2301 unsigned int cmd, unsigned long arg)
2303 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2305 if (debug_level >= DEBUG_LEVEL_INFO)
2306 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2307 info->device_name, cmd );
2309 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2312 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2313 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2314 if (tty->flags & (1 << TTY_IO_ERROR))
2318 return ioctl_common(info, cmd, arg);
2321 int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
2324 struct mgsl_icount cnow; /* kernel counter temps */
2325 struct serial_icounter_struct __user *p_cuser; /* user space */
2326 void __user *argp = (void __user *)arg;
2327 unsigned long flags;
2330 case MGSL_IOCGPARAMS:
2331 return get_params(info, argp);
2332 case MGSL_IOCSPARAMS:
2333 return set_params(info, argp);
2334 case MGSL_IOCGTXIDLE:
2335 return get_txidle(info, argp);
2336 case MGSL_IOCSTXIDLE:
2337 return set_txidle(info, (int)arg);
2339 return get_interface(info, argp);
2341 return set_interface(info,(int)arg);
2342 case MGSL_IOCTXENABLE:
2343 return set_txenable(info,(int)arg);
2344 case MGSL_IOCRXENABLE:
2345 return set_rxenable(info,(int)arg);
2346 case MGSL_IOCTXABORT:
2347 return tx_abort(info);
2348 case MGSL_IOCGSTATS:
2349 return get_stats(info, argp);
2350 case MGSL_IOCWAITEVENT:
2351 return wait_events(info, argp);
2353 return modem_input_wait(info,(int)arg);
2355 spin_lock_irqsave(&info->lock,flags);
2356 cnow = info->icount;
2357 spin_unlock_irqrestore(&info->lock,flags);
2359 PUT_USER(error,cnow.cts, &p_cuser->cts);
2360 if (error) return error;
2361 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2362 if (error) return error;
2363 PUT_USER(error,cnow.rng, &p_cuser->rng);
2364 if (error) return error;
2365 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2366 if (error) return error;
2367 PUT_USER(error,cnow.rx, &p_cuser->rx);
2368 if (error) return error;
2369 PUT_USER(error,cnow.tx, &p_cuser->tx);
2370 if (error) return error;
2371 PUT_USER(error,cnow.frame, &p_cuser->frame);
2372 if (error) return error;
2373 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2374 if (error) return error;
2375 PUT_USER(error,cnow.parity, &p_cuser->parity);
2376 if (error) return error;
2377 PUT_USER(error,cnow.brk, &p_cuser->brk);
2378 if (error) return error;
2379 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2380 if (error) return error;
2383 return -ENOIOCTLCMD;
2388 /* Set new termios settings
2392 * tty pointer to tty structure
2393 * termios pointer to buffer to hold returned old termios
2395 static void mgslpc_set_termios(struct tty_struct *tty, struct termios *old_termios)
2397 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2398 unsigned long flags;
2400 if (debug_level >= DEBUG_LEVEL_INFO)
2401 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2402 tty->driver->name );
2404 /* just return if nothing has changed */
2405 if ((tty->termios->c_cflag == old_termios->c_cflag)
2406 && (RELEVANT_IFLAG(tty->termios->c_iflag)
2407 == RELEVANT_IFLAG(old_termios->c_iflag)))
2410 mgslpc_change_params(info);
2412 /* Handle transition to B0 status */
2413 if (old_termios->c_cflag & CBAUD &&
2414 !(tty->termios->c_cflag & CBAUD)) {
2415 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2416 spin_lock_irqsave(&info->lock,flags);
2418 spin_unlock_irqrestore(&info->lock,flags);
2421 /* Handle transition away from B0 status */
2422 if (!(old_termios->c_cflag & CBAUD) &&
2423 tty->termios->c_cflag & CBAUD) {
2424 info->serial_signals |= SerialSignal_DTR;
2425 if (!(tty->termios->c_cflag & CRTSCTS) ||
2426 !test_bit(TTY_THROTTLED, &tty->flags)) {
2427 info->serial_signals |= SerialSignal_RTS;
2429 spin_lock_irqsave(&info->lock,flags);
2431 spin_unlock_irqrestore(&info->lock,flags);
2434 /* Handle turning off CRTSCTS */
2435 if (old_termios->c_cflag & CRTSCTS &&
2436 !(tty->termios->c_cflag & CRTSCTS)) {
2437 tty->hw_stopped = 0;
2442 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2444 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2446 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2449 if (debug_level >= DEBUG_LEVEL_INFO)
2450 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2451 __FILE__,__LINE__, info->device_name, info->count);
2456 if (tty_hung_up_p(filp))
2459 if ((tty->count == 1) && (info->count != 1)) {
2461 * tty->count is 1 and the tty structure will be freed.
2462 * info->count should be one in this case.
2463 * if it's not, correct it so that the port is shutdown.
2465 printk("mgslpc_close: bad refcount; tty->count is 1, "
2466 "info->count is %d\n", info->count);
2472 /* if at least one open remaining, leave hardware active */
2476 info->flags |= ASYNC_CLOSING;
2478 /* set tty->closing to notify line discipline to
2479 * only process XON/XOFF characters. Only the N_TTY
2480 * discipline appears to use this (ppp does not).
2484 /* wait for transmit data to clear all layers */
2486 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
2487 if (debug_level >= DEBUG_LEVEL_INFO)
2488 printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sent\n",
2489 __FILE__,__LINE__, info->device_name );
2490 tty_wait_until_sent(tty, info->closing_wait);
2493 if (info->flags & ASYNC_INITIALIZED)
2494 mgslpc_wait_until_sent(tty, info->timeout);
2496 if (tty->driver->flush_buffer)
2497 tty->driver->flush_buffer(tty);
2499 ldisc_flush_buffer(tty);
2506 if (info->blocked_open) {
2507 if (info->close_delay) {
2508 msleep_interruptible(jiffies_to_msecs(info->close_delay));
2510 wake_up_interruptible(&info->open_wait);
2513 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2515 wake_up_interruptible(&info->close_wait);
2518 if (debug_level >= DEBUG_LEVEL_INFO)
2519 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2520 tty->driver->name, info->count);
2523 /* Wait until the transmitter is empty.
2525 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2527 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2528 unsigned long orig_jiffies, char_time;
2533 if (debug_level >= DEBUG_LEVEL_INFO)
2534 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2535 __FILE__,__LINE__, info->device_name );
2537 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2540 if (!(info->flags & ASYNC_INITIALIZED))
2543 orig_jiffies = jiffies;
2545 /* Set check interval to 1/5 of estimated time to
2546 * send a character, and make it at least 1. The check
2547 * interval should also be less than the timeout.
2548 * Note: use tight timings here to satisfy the NIST-PCTS.
2551 if ( info->params.data_rate ) {
2552 char_time = info->timeout/(32 * 5);
2559 char_time = min_t(unsigned long, char_time, timeout);
2561 if (info->params.mode == MGSL_MODE_HDLC) {
2562 while (info->tx_active) {
2563 msleep_interruptible(jiffies_to_msecs(char_time));
2564 if (signal_pending(current))
2566 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2570 while ((info->tx_count || info->tx_active) &&
2572 msleep_interruptible(jiffies_to_msecs(char_time));
2573 if (signal_pending(current))
2575 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2581 if (debug_level >= DEBUG_LEVEL_INFO)
2582 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2583 __FILE__,__LINE__, info->device_name );
2586 /* Called by tty_hangup() when a hangup is signaled.
2587 * This is the same as closing all open files for the port.
2589 static void mgslpc_hangup(struct tty_struct *tty)
2591 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2593 if (debug_level >= DEBUG_LEVEL_INFO)
2594 printk("%s(%d):mgslpc_hangup(%s)\n",
2595 __FILE__,__LINE__, info->device_name );
2597 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2600 mgslpc_flush_buffer(tty);
2604 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2607 wake_up_interruptible(&info->open_wait);
2610 /* Block the current process until the specified port
2611 * is ready to be opened.
2613 static int block_til_ready(struct tty_struct *tty, struct file *filp,
2616 DECLARE_WAITQUEUE(wait, current);
2618 int do_clocal = 0, extra_count = 0;
2619 unsigned long flags;
2621 if (debug_level >= DEBUG_LEVEL_INFO)
2622 printk("%s(%d):block_til_ready on %s\n",
2623 __FILE__,__LINE__, tty->driver->name );
2625 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
2626 /* nonblock mode is set or port is not enabled */
2627 /* just verify that callout device is not active */
2628 info->flags |= ASYNC_NORMAL_ACTIVE;
2632 if (tty->termios->c_cflag & CLOCAL)
2635 /* Wait for carrier detect and the line to become
2636 * free (i.e., not in use by the callout). While we are in
2637 * this loop, info->count is dropped by one, so that
2638 * mgslpc_close() knows when to free things. We restore it upon
2639 * exit, either normal or abnormal.
2643 add_wait_queue(&info->open_wait, &wait);
2645 if (debug_level >= DEBUG_LEVEL_INFO)
2646 printk("%s(%d):block_til_ready before block on %s count=%d\n",
2647 __FILE__,__LINE__, tty->driver->name, info->count );
2649 spin_lock_irqsave(&info->lock, flags);
2650 if (!tty_hung_up_p(filp)) {
2654 spin_unlock_irqrestore(&info->lock, flags);
2655 info->blocked_open++;
2658 if ((tty->termios->c_cflag & CBAUD)) {
2659 spin_lock_irqsave(&info->lock,flags);
2660 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2662 spin_unlock_irqrestore(&info->lock,flags);
2665 set_current_state(TASK_INTERRUPTIBLE);
2667 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
2668 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
2669 -EAGAIN : -ERESTARTSYS;
2673 spin_lock_irqsave(&info->lock,flags);
2675 spin_unlock_irqrestore(&info->lock,flags);
2677 if (!(info->flags & ASYNC_CLOSING) &&
2678 (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
2682 if (signal_pending(current)) {
2683 retval = -ERESTARTSYS;
2687 if (debug_level >= DEBUG_LEVEL_INFO)
2688 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
2689 __FILE__,__LINE__, tty->driver->name, info->count );
2694 set_current_state(TASK_RUNNING);
2695 remove_wait_queue(&info->open_wait, &wait);
2699 info->blocked_open--;
2701 if (debug_level >= DEBUG_LEVEL_INFO)
2702 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
2703 __FILE__,__LINE__, tty->driver->name, info->count );
2706 info->flags |= ASYNC_NORMAL_ACTIVE;
2711 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2715 unsigned long flags;
2717 /* verify range of specified line number */
2719 if ((line < 0) || (line >= mgslpc_device_count)) {
2720 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2721 __FILE__,__LINE__,line);
2725 /* find the info structure for the specified line */
2726 info = mgslpc_device_list;
2727 while(info && info->line != line)
2728 info = info->next_device;
2729 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2732 tty->driver_data = info;
2735 if (debug_level >= DEBUG_LEVEL_INFO)
2736 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2737 __FILE__,__LINE__,tty->driver->name, info->count);
2739 /* If port is closing, signal caller to try again */
2740 if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
2741 if (info->flags & ASYNC_CLOSING)
2742 interruptible_sleep_on(&info->close_wait);
2743 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2744 -EAGAIN : -ERESTARTSYS);
2748 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2750 spin_lock_irqsave(&info->netlock, flags);
2751 if (info->netcount) {
2753 spin_unlock_irqrestore(&info->netlock, flags);
2757 spin_unlock_irqrestore(&info->netlock, flags);
2759 if (info->count == 1) {
2760 /* 1st open on this device, init hardware */
2761 retval = startup(info);
2766 retval = block_til_ready(tty, filp, info);
2768 if (debug_level >= DEBUG_LEVEL_INFO)
2769 printk("%s(%d):block_til_ready(%s) returned %d\n",
2770 __FILE__,__LINE__, info->device_name, retval);
2774 if (debug_level >= DEBUG_LEVEL_INFO)
2775 printk("%s(%d):mgslpc_open(%s) success\n",
2776 __FILE__,__LINE__, info->device_name);
2781 if (tty->count == 1)
2782 info->tty = NULL; /* tty layer will release tty struct */
2791 * /proc fs routines....
2794 static inline int line_info(char *buf, MGSLPC_INFO *info)
2798 unsigned long flags;
2800 ret = sprintf(buf, "%s:io:%04X irq:%d",
2801 info->device_name, info->io_base, info->irq_level);
2803 /* output current serial signal states */
2804 spin_lock_irqsave(&info->lock,flags);
2806 spin_unlock_irqrestore(&info->lock,flags);
2810 if (info->serial_signals & SerialSignal_RTS)
2811 strcat(stat_buf, "|RTS");
2812 if (info->serial_signals & SerialSignal_CTS)
2813 strcat(stat_buf, "|CTS");
2814 if (info->serial_signals & SerialSignal_DTR)
2815 strcat(stat_buf, "|DTR");
2816 if (info->serial_signals & SerialSignal_DSR)
2817 strcat(stat_buf, "|DSR");
2818 if (info->serial_signals & SerialSignal_DCD)
2819 strcat(stat_buf, "|CD");
2820 if (info->serial_signals & SerialSignal_RI)
2821 strcat(stat_buf, "|RI");
2823 if (info->params.mode == MGSL_MODE_HDLC) {
2824 ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
2825 info->icount.txok, info->icount.rxok);
2826 if (info->icount.txunder)
2827 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
2828 if (info->icount.txabort)
2829 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
2830 if (info->icount.rxshort)
2831 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
2832 if (info->icount.rxlong)
2833 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
2834 if (info->icount.rxover)
2835 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
2836 if (info->icount.rxcrc)
2837 ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
2839 ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
2840 info->icount.tx, info->icount.rx);
2841 if (info->icount.frame)
2842 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2843 if (info->icount.parity)
2844 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2845 if (info->icount.brk)
2846 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2847 if (info->icount.overrun)
2848 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2851 /* Append serial signal status to end */
2852 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2854 ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2855 info->tx_active,info->bh_requested,info->bh_running,
2861 /* Called to print information about devices
2863 static int mgslpc_read_proc(char *page, char **start, off_t off, int count,
2864 int *eof, void *data)
2870 len += sprintf(page, "synclink driver:%s\n", driver_version);
2872 info = mgslpc_device_list;
2874 l = line_info(page + len, info);
2876 if (len+begin > off+count)
2878 if (len+begin < off) {
2882 info = info->next_device;
2887 if (off >= len+begin)
2889 *start = page + (off-begin);
2890 return ((count < begin+len-off) ? count : begin+len-off);
2893 int rx_alloc_buffers(MGSLPC_INFO *info)
2895 /* each buffer has header and data */
2896 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2898 /* calculate total allocation size for 8 buffers */
2899 info->rx_buf_total_size = info->rx_buf_size * 8;
2901 /* limit total allocated memory */
2902 if (info->rx_buf_total_size > 0x10000)
2903 info->rx_buf_total_size = 0x10000;
2905 /* calculate number of buffers */
2906 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2908 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2909 if (info->rx_buf == NULL)
2912 rx_reset_buffers(info);
2916 void rx_free_buffers(MGSLPC_INFO *info)
2918 kfree(info->rx_buf);
2919 info->rx_buf = NULL;
2922 int claim_resources(MGSLPC_INFO *info)
2924 if (rx_alloc_buffers(info) < 0 ) {
2925 printk( "Cant allocate rx buffer %s\n", info->device_name);
2926 release_resources(info);
2932 void release_resources(MGSLPC_INFO *info)
2934 if (debug_level >= DEBUG_LEVEL_INFO)
2935 printk("release_resources(%s)\n", info->device_name);
2936 rx_free_buffers(info);
2939 /* Add the specified device instance data structure to the
2940 * global linked list of devices and increment the device count.
2942 * Arguments: info pointer to device instance data
2944 void mgslpc_add_device(MGSLPC_INFO *info)
2946 info->next_device = NULL;
2947 info->line = mgslpc_device_count;
2948 sprintf(info->device_name,"ttySLP%d",info->line);
2950 if (info->line < MAX_DEVICE_COUNT) {
2951 if (maxframe[info->line])
2952 info->max_frame_size = maxframe[info->line];
2953 info->dosyncppp = dosyncppp[info->line];
2956 mgslpc_device_count++;
2958 if (!mgslpc_device_list)
2959 mgslpc_device_list = info;
2961 MGSLPC_INFO *current_dev = mgslpc_device_list;
2962 while( current_dev->next_device )
2963 current_dev = current_dev->next_device;
2964 current_dev->next_device = info;
2967 if (info->max_frame_size < 4096)
2968 info->max_frame_size = 4096;
2969 else if (info->max_frame_size > 65535)
2970 info->max_frame_size = 65535;
2972 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2973 info->device_name, info->io_base, info->irq_level);
2980 void mgslpc_remove_device(MGSLPC_INFO *remove_info)
2982 MGSLPC_INFO *info = mgslpc_device_list;
2983 MGSLPC_INFO *last = NULL;
2986 if (info == remove_info) {
2988 last->next_device = info->next_device;
2990 mgslpc_device_list = info->next_device;
2994 release_resources(info);
2996 mgslpc_device_count--;
3000 info = info->next_device;
3004 static struct pcmcia_device_id mgslpc_ids[] = {
3005 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
3008 MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
3010 static struct pcmcia_driver mgslpc_driver = {
3011 .owner = THIS_MODULE,
3013 .name = "synclink_cs",
3015 .probe = mgslpc_attach,
3016 .remove = mgslpc_detach,
3017 .id_table = mgslpc_ids,
3018 .suspend = mgslpc_suspend,
3019 .resume = mgslpc_resume,
3022 static struct tty_operations mgslpc_ops = {
3023 .open = mgslpc_open,
3024 .close = mgslpc_close,
3025 .write = mgslpc_write,
3026 .put_char = mgslpc_put_char,
3027 .flush_chars = mgslpc_flush_chars,
3028 .write_room = mgslpc_write_room,
3029 .chars_in_buffer = mgslpc_chars_in_buffer,
3030 .flush_buffer = mgslpc_flush_buffer,
3031 .ioctl = mgslpc_ioctl,
3032 .throttle = mgslpc_throttle,
3033 .unthrottle = mgslpc_unthrottle,
3034 .send_xchar = mgslpc_send_xchar,
3035 .break_ctl = mgslpc_break,
3036 .wait_until_sent = mgslpc_wait_until_sent,
3037 .read_proc = mgslpc_read_proc,
3038 .set_termios = mgslpc_set_termios,
3040 .start = tx_release,
3041 .hangup = mgslpc_hangup,
3042 .tiocmget = tiocmget,
3043 .tiocmset = tiocmset,
3046 static void synclink_cs_cleanup(void)
3050 printk("Unloading %s: version %s\n", driver_name, driver_version);
3052 while(mgslpc_device_list)
3053 mgslpc_remove_device(mgslpc_device_list);
3055 if (serial_driver) {
3056 if ((rc = tty_unregister_driver(serial_driver)))
3057 printk("%s(%d) failed to unregister tty driver err=%d\n",
3058 __FILE__,__LINE__,rc);
3059 put_tty_driver(serial_driver);
3062 pcmcia_unregister_driver(&mgslpc_driver);
3065 static int __init synclink_cs_init(void)
3069 if (break_on_load) {
3070 mgslpc_get_text_ptr();
3074 printk("%s %s\n", driver_name, driver_version);
3076 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
3079 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
3080 if (!serial_driver) {
3085 /* Initialize the tty_driver structure */
3087 serial_driver->owner = THIS_MODULE;
3088 serial_driver->driver_name = "synclink_cs";
3089 serial_driver->name = "ttySLP";
3090 serial_driver->major = ttymajor;
3091 serial_driver->minor_start = 64;
3092 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3093 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3094 serial_driver->init_termios = tty_std_termios;
3095 serial_driver->init_termios.c_cflag =
3096 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3097 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3098 tty_set_operations(serial_driver, &mgslpc_ops);
3100 if ((rc = tty_register_driver(serial_driver)) < 0) {
3101 printk("%s(%d):Couldn't register serial driver\n",
3103 put_tty_driver(serial_driver);
3104 serial_driver = NULL;
3108 printk("%s %s, tty major#%d\n",
3109 driver_name, driver_version,
3110 serial_driver->major);
3115 synclink_cs_cleanup();
3119 static void __exit synclink_cs_exit(void)
3121 synclink_cs_cleanup();
3124 module_init(synclink_cs_init);
3125 module_exit(synclink_cs_exit);
3127 static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
3132 /* note:standard BRG mode is broken in V3.2 chip
3133 * so enhanced mode is always used
3141 for (M = 1; N > 64 && M < 16; M++)
3147 * BGR[7..0] contained in BGR register
3148 * BGR[9..8] contained in CCR2[7..6]
3149 * divisor = (N+1)*2^M
3151 * Note: M *must* not be zero (causes asymetric duty cycle)
3153 write_reg(info, (unsigned char) (channel + BGR),
3154 (unsigned char) ((M << 6) + N));
3155 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
3156 val |= ((M << 4) & 0xc0);
3157 write_reg(info, (unsigned char) (channel + CCR2), val);
3161 /* Enabled the AUX clock output at the specified frequency.
3163 static void enable_auxclk(MGSLPC_INFO *info)
3169 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3170 * 05 ADM Address Mode, 0 = no addr recognition
3171 * 04 TMD Timer Mode, 0 = external
3172 * 03 RAC Receiver Active, 0 = inactive
3173 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3174 * 01 TRS Timer Resolution, 1=512
3175 * 00 TLP Test Loop, 0 = no loop
3181 /* channel B RTS is used to enable AUXCLK driver on SP505 */
3182 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3184 write_reg(info, CHB + MODE, val);
3188 * 07 PU Power Up, 1=active, 0=power down
3189 * 06 MCE Master Clock Enable, 1=enabled
3191 * 04..02 SC[2..0] Encoding
3192 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3196 write_reg(info, CHB + CCR0, 0xc0);
3200 * 07 SFLG Shared Flag, 0 = disable shared flags
3201 * 06 GALP Go Active On Loop, 0 = not used
3202 * 05 GLP Go On Loop, 0 = not used
3203 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3204 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3205 * 02..00 CM[2..0] Clock Mode
3209 write_reg(info, CHB + CCR1, 0x17);
3213 * 07..06 BGR[9..8] Baud rate bits 9..8
3214 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3215 * 04 SSEL Clock source select, 1=submode b
3216 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3217 * 02 RWX Read/Write Exchange 0=disabled
3218 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3219 * 00 DIV, data inversion 0=disabled, 1=enabled
3223 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3224 write_reg(info, CHB + CCR2, 0x38);
3226 write_reg(info, CHB + CCR2, 0x30);
3230 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3231 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3232 * 05 TST1 Test Pin, 0=normal operation
3233 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3234 * 03..02 Reserved, must be 0
3235 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3239 write_reg(info, CHB + CCR4, 0x50);
3241 /* if auxclk not enabled, set internal BRG so
3242 * CTS transitions can be detected (requires TxC)
3244 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3245 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3247 mgslpc_set_rate(info, CHB, 921600);
3250 static void loopback_enable(MGSLPC_INFO *info)
3254 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3255 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3256 write_reg(info, CHA + CCR1, val);
3258 /* CCR2:04 SSEL Clock source select, 1=submode b */
3259 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3260 write_reg(info, CHA + CCR2, val);
3262 /* set LinkSpeed if available, otherwise default to 2Mbps */
3263 if (info->params.clock_speed)
3264 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3266 mgslpc_set_rate(info, CHA, 1843200);
3268 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3269 val = read_reg(info, CHA + MODE) | BIT0;
3270 write_reg(info, CHA + MODE, val);
3273 void hdlc_mode(MGSLPC_INFO *info)
3276 unsigned char clkmode, clksubmode;
3278 /* disable all interrupts */
3279 irq_disable(info, CHA, 0xffff);
3280 irq_disable(info, CHB, 0xffff);
3281 port_irq_disable(info, 0xff);
3283 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3284 clkmode = clksubmode = 0;
3285 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3286 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3287 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3289 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3290 && info->params.flags & HDLC_FLAG_TXC_BRG) {
3291 /* clock mode 7b, rcv = BRG, xmt = BRG */
3294 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3295 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3296 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3300 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3303 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3304 /* clock mode 0b, rcv = RxC, xmt = BRG */
3310 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3311 * 05 ADM Address Mode, 0 = no addr recognition
3312 * 04 TMD Timer Mode, 0 = external
3313 * 03 RAC Receiver Active, 0 = inactive
3314 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3315 * 01 TRS Timer Resolution, 1=512
3316 * 00 TLP Test Loop, 0 = no loop
3321 if (info->params.loopback)
3324 /* preserve RTS state */
3325 if (info->serial_signals & SerialSignal_RTS)
3327 write_reg(info, CHA + MODE, val);
3331 * 07 PU Power Up, 1=active, 0=power down
3332 * 06 MCE Master Clock Enable, 1=enabled
3334 * 04..02 SC[2..0] Encoding
3335 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3340 switch (info->params.encoding)
3342 case HDLC_ENCODING_NRZI:
3345 case HDLC_ENCODING_BIPHASE_SPACE:
3348 case HDLC_ENCODING_BIPHASE_MARK:
3351 case HDLC_ENCODING_BIPHASE_LEVEL:
3353 break; // Manchester
3355 write_reg(info, CHA + CCR0, val);
3359 * 07 SFLG Shared Flag, 0 = disable shared flags
3360 * 06 GALP Go Active On Loop, 0 = not used
3361 * 05 GLP Go On Loop, 0 = not used
3362 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3363 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3364 * 02..00 CM[2..0] Clock Mode
3368 val = 0x10 + clkmode;
3369 write_reg(info, CHA + CCR1, val);
3373 * 07..06 BGR[9..8] Baud rate bits 9..8
3374 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3375 * 04 SSEL Clock source select, 1=submode b
3376 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3377 * 02 RWX Read/Write Exchange 0=disabled
3378 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3379 * 00 DIV, data inversion 0=disabled, 1=enabled
3384 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3385 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3389 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3391 if (info->params.encoding == HDLC_ENCODING_NRZB)
3393 write_reg(info, CHA + CCR2, val);
3397 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3398 * 05 EPT Enable preamble transmission, 1=enabled
3399 * 04 RADD Receive address pushed to FIFO, 0=disabled
3400 * 03 CRL CRC Reset Level, 0=FFFF
3401 * 02 RCRC Rx CRC 0=On 1=Off
3402 * 01 TCRC Tx CRC 0=On 1=Off
3403 * 00 PSD DPLL Phase Shift Disable
3408 if (info->params.crc_type == HDLC_CRC_NONE)
3410 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3412 switch (info->params.preamble_length)
3414 case HDLC_PREAMBLE_LENGTH_16BITS:
3417 case HDLC_PREAMBLE_LENGTH_32BITS:
3420 case HDLC_PREAMBLE_LENGTH_64BITS:
3424 write_reg(info, CHA + CCR3, val);
3426 /* PRE - Preamble pattern */
3428 switch (info->params.preamble)
3430 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3431 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3432 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3433 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3435 write_reg(info, CHA + PRE, val);
3439 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3440 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3441 * 05 TST1 Test Pin, 0=normal operation
3442 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3443 * 03..02 Reserved, must be 0
3444 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3449 write_reg(info, CHA + CCR4, val);
3450 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3451 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3453 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3455 /* RLCR Receive length check register
3457 * 7 1=enable receive length check
3458 * 6..0 Max frame length = (RL + 1) * 32
3460 write_reg(info, CHA + RLCR, 0);
3462 /* XBCH Transmit Byte Count High
3464 * 07 DMA mode, 0 = interrupt driven
3465 * 06 NRM, 0=ABM (ignored)
3466 * 05 CAS Carrier Auto Start
3467 * 04 XC Transmit Continuously (ignored)
3468 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3473 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3475 write_reg(info, CHA + XBCH, val);
3476 enable_auxclk(info);
3477 if (info->params.loopback || info->testing_irq)
3478 loopback_enable(info);
3479 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3481 irq_enable(info, CHB, IRQ_CTS);
3482 /* PVR[3] 1=AUTO CTS active */
3483 set_reg_bits(info, CHA + PVR, BIT3);
3485 clear_reg_bits(info, CHA + PVR, BIT3);
3487 irq_enable(info, CHA,
3488 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3489 IRQ_UNDERRUN + IRQ_TXFIFO);
3490 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3491 wait_command_complete(info, CHA);
3492 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3494 /* Master clock mode enabled above to allow reset commands
3495 * to complete even if no data clocks are present.
3497 * Disable master clock mode for normal communications because
3498 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3499 * IRQ when in master clock mode.
3501 * Leave master clock mode enabled for IRQ test because the
3502 * timer IRQ used by the test can only happen in master clock mode.
3504 if (!info->testing_irq)
3505 clear_reg_bits(info, CHA + CCR0, BIT6);
3513 void rx_stop(MGSLPC_INFO *info)
3515 if (debug_level >= DEBUG_LEVEL_ISR)
3516 printk("%s(%d):rx_stop(%s)\n",
3517 __FILE__,__LINE__, info->device_name );
3519 /* MODE:03 RAC Receiver Active, 0=inactive */
3520 clear_reg_bits(info, CHA + MODE, BIT3);
3522 info->rx_enabled = 0;
3523 info->rx_overflow = 0;
3526 void rx_start(MGSLPC_INFO *info)
3528 if (debug_level >= DEBUG_LEVEL_ISR)
3529 printk("%s(%d):rx_start(%s)\n",
3530 __FILE__,__LINE__, info->device_name );
3532 rx_reset_buffers(info);
3533 info->rx_enabled = 0;
3534 info->rx_overflow = 0;
3536 /* MODE:03 RAC Receiver Active, 1=active */
3537 set_reg_bits(info, CHA + MODE, BIT3);
3539 info->rx_enabled = 1;
3542 void tx_start(MGSLPC_INFO *info)
3544 if (debug_level >= DEBUG_LEVEL_ISR)
3545 printk("%s(%d):tx_start(%s)\n",
3546 __FILE__,__LINE__, info->device_name );
3548 if (info->tx_count) {
3549 /* If auto RTS enabled and RTS is inactive, then assert */
3550 /* RTS and set a flag indicating that the driver should */
3551 /* negate RTS when the transmission completes. */
3552 info->drop_rts_on_tx_done = 0;
3554 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3556 if (!(info->serial_signals & SerialSignal_RTS)) {
3557 info->serial_signals |= SerialSignal_RTS;
3559 info->drop_rts_on_tx_done = 1;
3563 if (info->params.mode == MGSL_MODE_ASYNC) {
3564 if (!info->tx_active) {
3565 info->tx_active = 1;
3569 info->tx_active = 1;
3571 info->tx_timer.expires = jiffies + msecs_to_jiffies(5000);
3572 add_timer(&info->tx_timer);
3576 if (!info->tx_enabled)
3577 info->tx_enabled = 1;
3580 void tx_stop(MGSLPC_INFO *info)
3582 if (debug_level >= DEBUG_LEVEL_ISR)
3583 printk("%s(%d):tx_stop(%s)\n",
3584 __FILE__,__LINE__, info->device_name );
3586 del_timer(&info->tx_timer);
3588 info->tx_enabled = 0;
3589 info->tx_active = 0;
3592 /* Reset the adapter to a known state and prepare it for further use.
3594 void reset_device(MGSLPC_INFO *info)
3596 /* power up both channels (set BIT7) */
3597 write_reg(info, CHA + CCR0, 0x80);
3598 write_reg(info, CHB + CCR0, 0x80);
3599 write_reg(info, CHA + MODE, 0);
3600 write_reg(info, CHB + MODE, 0);
3602 /* disable all interrupts */
3603 irq_disable(info, CHA, 0xffff);
3604 irq_disable(info, CHB, 0xffff);
3605 port_irq_disable(info, 0xff);
3607 /* PCR Port Configuration Register
3609 * 07..04 DEC[3..0] Serial I/F select outputs
3610 * 03 output, 1=AUTO CTS control enabled
3611 * 02 RI Ring Indicator input 0=active
3612 * 01 DSR input 0=active
3613 * 00 DTR output 0=active
3617 write_reg(info, PCR, 0x06);
3619 /* PVR Port Value Register
3621 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3622 * 03 AUTO CTS output 1=enabled
3623 * 02 RI Ring Indicator input
3625 * 00 DTR output (1=inactive)
3629 // write_reg(info, PVR, PVR_DTR);
3631 /* IPC Interrupt Port Configuration
3633 * 07 VIS 1=Masked interrupts visible
3634 * 06..05 Reserved, 0
3635 * 04..03 SLA Slave address, 00 ignored
3636 * 02 CASM Cascading Mode, 1=daisy chain
3637 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3641 write_reg(info, IPC, 0x05);
3644 void async_mode(MGSLPC_INFO *info)
3648 /* disable all interrupts */
3649 irq_disable(info, CHA, 0xffff);
3650 irq_disable(info, CHB, 0xffff);
3651 port_irq_disable(info, 0xff);
3656 * 06 FRTS RTS State, 0=active
3657 * 05 FCTS Flow Control on CTS
3658 * 04 FLON Flow Control Enable
3659 * 03 RAC Receiver Active, 0 = inactive
3660 * 02 RTS 0=Auto RTS, 1=manual RTS
3661 * 01 TRS Timer Resolution, 1=512
3662 * 00 TLP Test Loop, 0 = no loop
3667 if (info->params.loopback)
3670 /* preserve RTS state */
3671 if (!(info->serial_signals & SerialSignal_RTS))
3673 write_reg(info, CHA + MODE, val);
3677 * 07 PU Power Up, 1=active, 0=power down
3678 * 06 MCE Master Clock Enable, 1=enabled
3680 * 04..02 SC[2..0] Encoding, 000=NRZ
3681 * 01..00 SM[1..0] Serial Mode, 11=Async
3685 write_reg(info, CHA + CCR0, 0x83);
3689 * 07..05 Reserved, 0
3690 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3691 * 03 BCR Bit Clock Rate, 1=16x
3692 * 02..00 CM[2..0] Clock Mode, 111=BRG
3696 write_reg(info, CHA + CCR1, 0x1f);
3700 * 07..06 BGR[9..8] Baud rate bits 9..8
3701 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3702 * 04 SSEL Clock source select, 1=submode b
3703 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3704 * 02 RWX Read/Write Exchange 0=disabled
3706 * 00 DIV, data inversion 0=disabled, 1=enabled
3710 write_reg(info, CHA + CCR2, 0x10);
3714 * 07..01 Reserved, 0
3715 * 00 PSD DPLL Phase Shift Disable
3719 write_reg(info, CHA + CCR3, 0);
3723 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3724 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3725 * 05 TST1 Test Pin, 0=normal operation
3726 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3727 * 03..00 Reserved, must be 0
3731 write_reg(info, CHA + CCR4, 0x50);
3732 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3737 * 06 XBRK transmit break, 0=normal operation
3738 * 05 Stop bits (0=1, 1=2)
3739 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3740 * 02 PAREN Parity Enable
3741 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3745 if (info->params.data_bits != 8)
3746 val |= BIT0; /* 7 bits */
3747 if (info->params.stop_bits != 1)
3749 if (info->params.parity != ASYNC_PARITY_NONE)
3751 val |= BIT2; /* Parity enable */
3752 if (info->params.parity == ASYNC_PARITY_ODD)
3757 write_reg(info, CHA + DAFO, val);
3759 /* RFC Rx FIFO Control
3762 * 06 DPS, 1=parity bit not stored in data byte
3763 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3764 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3765 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3767 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3771 write_reg(info, CHA + RFC, 0x5c);
3773 /* RLCR Receive length check register
3775 * Max frame length = (RL + 1) * 32
3777 write_reg(info, CHA + RLCR, 0);
3779 /* XBCH Transmit Byte Count High
3781 * 07 DMA mode, 0 = interrupt driven
3782 * 06 NRM, 0=ABM (ignored)
3783 * 05 CAS Carrier Auto Start
3784 * 04 XC Transmit Continuously (ignored)
3785 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3790 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3792 write_reg(info, CHA + XBCH, val);
3793 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3794 irq_enable(info, CHA, IRQ_CTS);
3796 /* MODE:03 RAC Receiver Active, 1=active */
3797 set_reg_bits(info, CHA + MODE, BIT3);
3798 enable_auxclk(info);
3799 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3800 irq_enable(info, CHB, IRQ_CTS);
3801 /* PVR[3] 1=AUTO CTS active */
3802 set_reg_bits(info, CHA + PVR, BIT3);
3804 clear_reg_bits(info, CHA + PVR, BIT3);
3805 irq_enable(info, CHA,
3806 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3807 IRQ_ALLSENT + IRQ_TXFIFO);
3808 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3809 wait_command_complete(info, CHA);
3810 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3813 /* Set the HDLC idle mode for the transmitter.
3815 void tx_set_idle(MGSLPC_INFO *info)
3817 /* Note: ESCC2 only supports flags and one idle modes */
3818 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3819 set_reg_bits(info, CHA + CCR1, BIT3);
3821 clear_reg_bits(info, CHA + CCR1, BIT3);
3824 /* get state of the V24 status (input) signals.
3826 void get_signals(MGSLPC_INFO *info)
3828 unsigned char status = 0;
3830 /* preserve DTR and RTS */
3831 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3833 if (read_reg(info, CHB + VSTR) & BIT7)
3834 info->serial_signals |= SerialSignal_DCD;
3835 if (read_reg(info, CHB + STAR) & BIT1)
3836 info->serial_signals |= SerialSignal_CTS;
3838 status = read_reg(info, CHA + PVR);
3839 if (!(status & PVR_RI))
3840 info->serial_signals |= SerialSignal_RI;
3841 if (!(status & PVR_DSR))
3842 info->serial_signals |= SerialSignal_DSR;
3845 /* Set the state of DTR and RTS based on contents of
3846 * serial_signals member of device extension.
3848 void set_signals(MGSLPC_INFO *info)
3852 val = read_reg(info, CHA + MODE);
3853 if (info->params.mode == MGSL_MODE_ASYNC) {
3854 if (info->serial_signals & SerialSignal_RTS)
3859 if (info->serial_signals & SerialSignal_RTS)
3864 write_reg(info, CHA + MODE, val);
3866 if (info->serial_signals & SerialSignal_DTR)
3867 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3869 set_reg_bits(info, CHA + PVR, PVR_DTR);
3872 void rx_reset_buffers(MGSLPC_INFO *info)
3879 info->rx_frame_count = 0;
3880 for (i=0 ; i < info->rx_buf_count ; i++) {
3881 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3882 buf->status = buf->count = 0;
3886 /* Attempt to return a received HDLC frame
3887 * Only frames received without errors are returned.
3889 * Returns 1 if frame returned, otherwise 0
3891 int rx_get_frame(MGSLPC_INFO *info)
3893 unsigned short status;
3895 unsigned int framesize = 0;
3896 unsigned long flags;
3897 struct tty_struct *tty = info->tty;
3898 int return_frame = 0;
3900 if (info->rx_frame_count == 0)
3903 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3905 status = buf->status;
3907 /* 07 VFR 1=valid frame
3908 * 06 RDO 1=data overrun
3909 * 05 CRC 1=OK, 0=error
3910 * 04 RAB 1=frame aborted
3912 if ((status & 0xf0) != 0xA0) {
3913 if (!(status & BIT7) || (status & BIT4))
3914 info->icount.rxabort++;
3915 else if (status & BIT6)
3916 info->icount.rxover++;
3917 else if (!(status & BIT5)) {
3918 info->icount.rxcrc++;
3919 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
3925 struct net_device_stats *stats = hdlc_stats(info->netdev);
3927 stats->rx_frame_errors++;
3934 framesize = buf->count;
3936 if (debug_level >= DEBUG_LEVEL_BH)
3937 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3938 __FILE__,__LINE__,info->device_name,status,framesize);
3940 if (debug_level >= DEBUG_LEVEL_DATA)
3941 trace_block(info, buf->data, framesize, 0);
3944 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3945 framesize+1 > info->max_frame_size) ||
3946 framesize > info->max_frame_size)
3947 info->icount.rxlong++;
3950 info->icount.rxok++;
3952 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3953 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3959 hdlcdev_rx(info, buf->data, framesize);
3962 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3966 spin_lock_irqsave(&info->lock,flags);
3967 buf->status = buf->count = 0;
3968 info->rx_frame_count--;
3970 if (info->rx_get >= info->rx_buf_count)
3972 spin_unlock_irqrestore(&info->lock,flags);
3977 BOOLEAN register_test(MGSLPC_INFO *info)
3979 static unsigned char patterns[] =
3980 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
3981 static unsigned int count = ARRAY_SIZE(patterns);
3984 unsigned long flags;
3986 spin_lock_irqsave(&info->lock,flags);
3989 for (i = 0; i < count; i++) {
3990 write_reg(info, XAD1, patterns[i]);
3991 write_reg(info, XAD2, patterns[(i + 1) % count]);
3992 if ((read_reg(info, XAD1) != patterns[i]) ||
3993 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
3999 spin_unlock_irqrestore(&info->lock,flags);
4003 BOOLEAN irq_test(MGSLPC_INFO *info)
4005 unsigned long end_time;
4006 unsigned long flags;
4008 spin_lock_irqsave(&info->lock,flags);
4011 info->testing_irq = TRUE;
4014 info->irq_occurred = FALSE;
4016 /* init hdlc mode */
4018 irq_enable(info, CHA, IRQ_TIMER);
4019 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
4020 issue_command(info, CHA, CMD_START_TIMER);
4022 spin_unlock_irqrestore(&info->lock,flags);
4025 while(end_time-- && !info->irq_occurred) {
4026 msleep_interruptible(10);
4029 info->testing_irq = FALSE;
4031 spin_lock_irqsave(&info->lock,flags);
4033 spin_unlock_irqrestore(&info->lock,flags);
4035 return info->irq_occurred ? TRUE : FALSE;
4038 int adapter_test(MGSLPC_INFO *info)
4040 if (!register_test(info)) {
4041 info->init_error = DiagStatus_AddressFailure;
4042 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
4043 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
4047 if (!irq_test(info)) {
4048 info->init_error = DiagStatus_IrqFailure;
4049 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
4050 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
4054 if (debug_level >= DEBUG_LEVEL_INFO)
4055 printk("%s(%d):device %s passed diagnostics\n",
4056 __FILE__,__LINE__,info->device_name);
4060 void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
4065 printk("%s tx data:\n",info->device_name);
4067 printk("%s rx data:\n",info->device_name);
4075 for(i=0;i<linecount;i++)
4076 printk("%02X ",(unsigned char)data[i]);
4079 for(i=0;i<linecount;i++) {
4080 if (data[i]>=040 && data[i]<=0176)
4081 printk("%c",data[i]);
4092 /* HDLC frame time out
4093 * update stats and do tx completion processing
4095 void tx_timeout(unsigned long context)
4097 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
4098 unsigned long flags;
4100 if ( debug_level >= DEBUG_LEVEL_INFO )
4101 printk( "%s(%d):tx_timeout(%s)\n",
4102 __FILE__,__LINE__,info->device_name);
4103 if(info->tx_active &&
4104 info->params.mode == MGSL_MODE_HDLC) {
4105 info->icount.txtimeout++;
4107 spin_lock_irqsave(&info->lock,flags);
4108 info->tx_active = 0;
4109 info->tx_count = info->tx_put = info->tx_get = 0;
4111 spin_unlock_irqrestore(&info->lock,flags);
4115 hdlcdev_tx_done(info);
4124 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
4125 * set encoding and frame check sequence (FCS) options
4127 * dev pointer to network device structure
4128 * encoding serial encoding setting
4129 * parity FCS setting
4131 * returns 0 if success, otherwise error code
4133 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
4134 unsigned short parity)
4136 MGSLPC_INFO *info = dev_to_port(dev);
4137 unsigned char new_encoding;
4138 unsigned short new_crctype;
4140 /* return error if TTY interface open */
4146 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
4147 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
4148 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
4149 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
4150 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
4151 default: return -EINVAL;
4156 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
4157 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
4158 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
4159 default: return -EINVAL;
4162 info->params.encoding = new_encoding;
4163 info->params.crc_type = new_crctype;
4165 /* if network interface up, reprogram hardware */
4167 mgslpc_program_hw(info);
4173 * called by generic HDLC layer to send frame
4175 * skb socket buffer containing HDLC frame
4176 * dev pointer to network device structure
4178 * returns 0 if success, otherwise error code
4180 static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
4182 MGSLPC_INFO *info = dev_to_port(dev);
4183 struct net_device_stats *stats = hdlc_stats(dev);
4184 unsigned long flags;
4186 if (debug_level >= DEBUG_LEVEL_INFO)
4187 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
4189 /* stop sending until this frame completes */
4190 netif_stop_queue(dev);
4192 /* copy data to device buffers */
4193 memcpy(info->tx_buf, skb->data, skb->len);
4195 info->tx_put = info->tx_count = skb->len;
4197 /* update network statistics */
4198 stats->tx_packets++;
4199 stats->tx_bytes += skb->len;
4201 /* done with socket buffer, so free it */
4204 /* save start time for transmit timeout detection */
4205 dev->trans_start = jiffies;
4207 /* start hardware transmitter if necessary */
4208 spin_lock_irqsave(&info->lock,flags);
4209 if (!info->tx_active)
4211 spin_unlock_irqrestore(&info->lock,flags);
4217 * called by network layer when interface enabled
4218 * claim resources and initialize hardware
4220 * dev pointer to network device structure
4222 * returns 0 if success, otherwise error code
4224 static int hdlcdev_open(struct net_device *dev)
4226 MGSLPC_INFO *info = dev_to_port(dev);
4228 unsigned long flags;
4230 if (debug_level >= DEBUG_LEVEL_INFO)
4231 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4233 /* generic HDLC layer open processing */
4234 if ((rc = hdlc_open(dev)))
4237 /* arbitrate between network and tty opens */
4238 spin_lock_irqsave(&info->netlock, flags);
4239 if (info->count != 0 || info->netcount != 0) {
4240 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4241 spin_unlock_irqrestore(&info->netlock, flags);
4245 spin_unlock_irqrestore(&info->netlock, flags);
4247 /* claim resources and init adapter */
4248 if ((rc = startup(info)) != 0) {
4249 spin_lock_irqsave(&info->netlock, flags);
4251 spin_unlock_irqrestore(&info->netlock, flags);
4255 /* assert DTR and RTS, apply hardware settings */
4256 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4257 mgslpc_program_hw(info);
4259 /* enable network layer transmit */
4260 dev->trans_start = jiffies;
4261 netif_start_queue(dev);
4263 /* inform generic HDLC layer of current DCD status */
4264 spin_lock_irqsave(&info->lock, flags);
4266 spin_unlock_irqrestore(&info->lock, flags);
4267 hdlc_set_carrier(info->serial_signals & SerialSignal_DCD, dev);
4273 * called by network layer when interface is disabled
4274 * shutdown hardware and release resources
4276 * dev pointer to network device structure
4278 * returns 0 if success, otherwise error code
4280 static int hdlcdev_close(struct net_device *dev)
4282 MGSLPC_INFO *info = dev_to_port(dev);
4283 unsigned long flags;
4285 if (debug_level >= DEBUG_LEVEL_INFO)
4286 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4288 netif_stop_queue(dev);
4290 /* shutdown adapter and release resources */
4295 spin_lock_irqsave(&info->netlock, flags);
4297 spin_unlock_irqrestore(&info->netlock, flags);
4303 * called by network layer to process IOCTL call to network device
4305 * dev pointer to network device structure
4306 * ifr pointer to network interface request structure
4307 * cmd IOCTL command code
4309 * returns 0 if success, otherwise error code
4311 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4313 const size_t size = sizeof(sync_serial_settings);
4314 sync_serial_settings new_line;
4315 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4316 MGSLPC_INFO *info = dev_to_port(dev);
4319 if (debug_level >= DEBUG_LEVEL_INFO)
4320 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4322 /* return error if TTY interface open */
4326 if (cmd != SIOCWANDEV)
4327 return hdlc_ioctl(dev, ifr, cmd);
4329 switch(ifr->ifr_settings.type) {
4330 case IF_GET_IFACE: /* return current sync_serial_settings */
4332 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4333 if (ifr->ifr_settings.size < size) {
4334 ifr->ifr_settings.size = size; /* data size wanted */
4338 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4339 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4340 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4341 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4344 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4345 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4346 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4347 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4348 default: new_line.clock_type = CLOCK_DEFAULT;
4351 new_line.clock_rate = info->params.clock_speed;
4352 new_line.loopback = info->params.loopback ? 1:0;
4354 if (copy_to_user(line, &new_line, size))
4358 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4360 if(!capable(CAP_NET_ADMIN))
4362 if (copy_from_user(&new_line, line, size))
4365 switch (new_line.clock_type)
4367 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4368 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4369 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4370 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4371 case CLOCK_DEFAULT: flags = info->params.flags &
4372 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4373 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4374 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4375 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4376 default: return -EINVAL;
4379 if (new_line.loopback != 0 && new_line.loopback != 1)
4382 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4383 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4384 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4385 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4386 info->params.flags |= flags;
4388 info->params.loopback = new_line.loopback;
4390 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4391 info->params.clock_speed = new_line.clock_rate;
4393 info->params.clock_speed = 0;
4395 /* if network interface up, reprogram hardware */
4397 mgslpc_program_hw(info);
4401 return hdlc_ioctl(dev, ifr, cmd);
4406 * called by network layer when transmit timeout is detected
4408 * dev pointer to network device structure
4410 static void hdlcdev_tx_timeout(struct net_device *dev)
4412 MGSLPC_INFO *info = dev_to_port(dev);
4413 struct net_device_stats *stats = hdlc_stats(dev);
4414 unsigned long flags;
4416 if (debug_level >= DEBUG_LEVEL_INFO)
4417 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4420 stats->tx_aborted_errors++;
4422 spin_lock_irqsave(&info->lock,flags);
4424 spin_unlock_irqrestore(&info->lock,flags);
4426 netif_wake_queue(dev);
4430 * called by device driver when transmit completes
4431 * reenable network layer transmit if stopped
4433 * info pointer to device instance information
4435 static void hdlcdev_tx_done(MGSLPC_INFO *info)
4437 if (netif_queue_stopped(info->netdev))
4438 netif_wake_queue(info->netdev);
4442 * called by device driver when frame received
4443 * pass frame to network layer
4445 * info pointer to device instance information
4446 * buf pointer to buffer contianing frame data
4447 * size count of data bytes in buf
4449 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4451 struct sk_buff *skb = dev_alloc_skb(size);
4452 struct net_device *dev = info->netdev;
4453 struct net_device_stats *stats = hdlc_stats(dev);
4455 if (debug_level >= DEBUG_LEVEL_INFO)
4456 printk("hdlcdev_rx(%s)\n",dev->name);
4459 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
4460 stats->rx_dropped++;
4464 memcpy(skb_put(skb, size),buf,size);
4466 skb->protocol = hdlc_type_trans(skb, info->netdev);
4468 stats->rx_packets++;
4469 stats->rx_bytes += size;
4473 info->netdev->last_rx = jiffies;
4477 * called by device driver when adding device instance
4478 * do generic HDLC initialization
4480 * info pointer to device instance information
4482 * returns 0 if success, otherwise error code
4484 static int hdlcdev_init(MGSLPC_INFO *info)
4487 struct net_device *dev;
4490 /* allocate and initialize network and HDLC layer objects */
4492 if (!(dev = alloc_hdlcdev(info))) {
4493 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4497 /* for network layer reporting purposes only */
4498 dev->base_addr = info->io_base;
4499 dev->irq = info->irq_level;
4501 /* network layer callbacks and settings */
4502 dev->do_ioctl = hdlcdev_ioctl;
4503 dev->open = hdlcdev_open;
4504 dev->stop = hdlcdev_close;
4505 dev->tx_timeout = hdlcdev_tx_timeout;
4506 dev->watchdog_timeo = 10*HZ;
4507 dev->tx_queue_len = 50;
4509 /* generic HDLC layer callbacks and settings */
4510 hdlc = dev_to_hdlc(dev);
4511 hdlc->attach = hdlcdev_attach;
4512 hdlc->xmit = hdlcdev_xmit;
4514 /* register objects with HDLC layer */
4515 if ((rc = register_hdlc_device(dev))) {
4516 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4526 * called by device driver when removing device instance
4527 * do generic HDLC cleanup
4529 * info pointer to device instance information
4531 static void hdlcdev_exit(MGSLPC_INFO *info)
4533 unregister_hdlc_device(info->netdev);
4534 free_netdev(info->netdev);
4535 info->netdev = NULL;
4538 #endif /* CONFIG_HDLC */