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/module.h>
39 #include <linux/errno.h>
40 #include <linux/signal.h>
41 #include <linux/sched.h>
42 #include <linux/timer.h>
43 #include <linux/time.h>
44 #include <linux/interrupt.h>
45 #include <linux/tty.h>
46 #include <linux/tty_flip.h>
47 #include <linux/serial.h>
48 #include <linux/major.h>
49 #include <linux/string.h>
50 #include <linux/fcntl.h>
51 #include <linux/ptrace.h>
52 #include <linux/ioport.h>
54 #include <linux/slab.h>
55 #include <linux/netdevice.h>
56 #include <linux/vmalloc.h>
57 #include <linux/init.h>
58 #include <linux/delay.h>
59 #include <linux/ioctl.h>
60 #include <linux/synclink.h>
62 #include <asm/system.h>
66 #include <linux/bitops.h>
67 #include <asm/types.h>
68 #include <linux/termios.h>
69 #include <linux/workqueue.h>
70 #include <linux/hdlc.h>
72 #include <pcmcia/cs_types.h>
73 #include <pcmcia/cs.h>
74 #include <pcmcia/cistpl.h>
75 #include <pcmcia/cisreg.h>
76 #include <pcmcia/ds.h>
78 #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_CS_MODULE))
79 #define SYNCLINK_GENERIC_HDLC 1
81 #define SYNCLINK_GENERIC_HDLC 0
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 static MGSL_PARAMS default_params = {
92 MGSL_MODE_HDLC, /* unsigned long mode */
93 0, /* unsigned char loopback; */
94 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
95 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
96 0, /* unsigned long clock_speed; */
97 0xff, /* unsigned char addr_filter; */
98 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
99 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
100 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
101 9600, /* unsigned long data_rate; */
102 8, /* unsigned char data_bits; */
103 1, /* unsigned char stop_bits; */
104 ASYNC_PARITY_NONE /* unsigned char parity; */
110 unsigned char status;
114 /* The queue of BH actions to be performed */
117 #define BH_TRANSMIT 2
120 #define IO_PIN_SHUTDOWN_LIMIT 100
122 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
124 struct _input_signal_events {
137 * Device instance data structure
140 typedef struct _mgslpc_info {
141 void *if_ptr; /* General purpose pointer (used by SPPP) */
144 int count; /* count of opens */
146 unsigned short close_delay;
147 unsigned short closing_wait; /* time to wait before closing */
149 struct mgsl_icount icount;
151 struct tty_struct *tty;
153 int x_char; /* xon/xoff character */
154 int blocked_open; /* # of blocked opens */
155 unsigned char read_status_mask;
156 unsigned char ignore_status_mask;
158 unsigned char *tx_buf;
163 /* circular list of fixed length rx buffers */
165 unsigned char *rx_buf; /* memory allocated for all rx buffers */
166 int rx_buf_total_size; /* size of memory allocated for rx buffers */
167 int rx_put; /* index of next empty rx buffer */
168 int rx_get; /* index of next full rx buffer */
169 int rx_buf_size; /* size in bytes of single rx buffer */
170 int rx_buf_count; /* total number of rx buffers */
171 int rx_frame_count; /* number of full rx buffers */
173 wait_queue_head_t open_wait;
174 wait_queue_head_t close_wait;
176 wait_queue_head_t status_event_wait_q;
177 wait_queue_head_t event_wait_q;
178 struct timer_list tx_timer; /* HDLC transmit timeout timer */
179 struct _mgslpc_info *next_device; /* device list link */
181 unsigned short imra_value;
182 unsigned short imrb_value;
183 unsigned char pim_value;
186 struct work_struct task; /* task structure for scheduling bh */
195 int dcd_chkcount; /* check counts to prevent */
196 int cts_chkcount; /* too many IRQs if a signal */
197 int dsr_chkcount; /* is floating */
208 int if_mode; /* serial interface selection (RS-232, v.35 etc) */
210 char device_name[25]; /* device instance name */
212 unsigned int io_base; /* base I/O address of adapter */
213 unsigned int irq_level;
215 MGSL_PARAMS params; /* communications parameters */
217 unsigned char serial_signals; /* current serial signal states */
219 bool irq_occurred; /* for diagnostics use */
221 unsigned int init_error; /* startup error (DIAGS) */
223 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
224 bool drop_rts_on_tx_done;
226 struct _input_signal_events input_signal_events;
229 struct pcmcia_device *p_dev;
233 /* SPPP/Cisco HDLC device parts */
238 #if SYNCLINK_GENERIC_HDLC
239 struct net_device *netdev;
244 #define MGSLPC_MAGIC 0x5402
247 * The size of the serial xmit buffer is 1 page, or 4096 bytes
249 #define TXBUFSIZE 4096
252 #define CHA 0x00 /* channel A offset */
253 #define CHB 0x40 /* channel B offset */
256 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
303 #define IRQ_BREAK_ON BIT15 // rx break detected
304 #define IRQ_DATAOVERRUN BIT14 // receive data overflow
305 #define IRQ_ALLSENT BIT13 // all sent
306 #define IRQ_UNDERRUN BIT12 // transmit data underrun
307 #define IRQ_TIMER BIT11 // timer interrupt
308 #define IRQ_CTS BIT10 // CTS status change
309 #define IRQ_TXREPEAT BIT9 // tx message repeat
310 #define IRQ_TXFIFO BIT8 // transmit pool ready
311 #define IRQ_RXEOM BIT7 // receive message end
312 #define IRQ_EXITHUNT BIT6 // receive frame start
313 #define IRQ_RXTIME BIT6 // rx char timeout
314 #define IRQ_DCD BIT2 // carrier detect status change
315 #define IRQ_OVERRUN BIT1 // receive frame overflow
316 #define IRQ_RXFIFO BIT0 // receive pool full
320 #define XFW BIT6 // transmit FIFO write enable
321 #define CEC BIT2 // command executing
322 #define CTS BIT1 // CTS state
327 #define PVR_AUTOCTS BIT3
328 #define PVR_RS232 0x20 /* 0010b */
329 #define PVR_V35 0xe0 /* 1110b */
330 #define PVR_RS422 0x40 /* 0100b */
332 /* Register access functions */
334 #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
335 #define read_reg(info, reg) inb((info)->io_base + (reg))
337 #define read_reg16(info, reg) inw((info)->io_base + (reg))
338 #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
340 #define set_reg_bits(info, reg, mask) \
341 write_reg(info, (reg), \
342 (unsigned char) (read_reg(info, (reg)) | (mask)))
343 #define clear_reg_bits(info, reg, mask) \
344 write_reg(info, (reg), \
345 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
347 * interrupt enable/disable routines
349 static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
351 if (channel == CHA) {
352 info->imra_value |= mask;
353 write_reg16(info, CHA + IMR, info->imra_value);
355 info->imrb_value |= mask;
356 write_reg16(info, CHB + IMR, info->imrb_value);
359 static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
361 if (channel == CHA) {
362 info->imra_value &= ~mask;
363 write_reg16(info, CHA + IMR, info->imra_value);
365 info->imrb_value &= ~mask;
366 write_reg16(info, CHB + IMR, info->imrb_value);
370 #define port_irq_disable(info, mask) \
371 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
373 #define port_irq_enable(info, mask) \
374 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
376 static void rx_start(MGSLPC_INFO *info);
377 static void rx_stop(MGSLPC_INFO *info);
379 static void tx_start(MGSLPC_INFO *info);
380 static void tx_stop(MGSLPC_INFO *info);
381 static void tx_set_idle(MGSLPC_INFO *info);
383 static void get_signals(MGSLPC_INFO *info);
384 static void set_signals(MGSLPC_INFO *info);
386 static void reset_device(MGSLPC_INFO *info);
388 static void hdlc_mode(MGSLPC_INFO *info);
389 static void async_mode(MGSLPC_INFO *info);
391 static void tx_timeout(unsigned long context);
393 static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg);
395 #if SYNCLINK_GENERIC_HDLC
396 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
397 static void hdlcdev_tx_done(MGSLPC_INFO *info);
398 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
399 static int hdlcdev_init(MGSLPC_INFO *info);
400 static void hdlcdev_exit(MGSLPC_INFO *info);
403 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
405 static bool register_test(MGSLPC_INFO *info);
406 static bool irq_test(MGSLPC_INFO *info);
407 static int adapter_test(MGSLPC_INFO *info);
409 static int claim_resources(MGSLPC_INFO *info);
410 static void release_resources(MGSLPC_INFO *info);
411 static void mgslpc_add_device(MGSLPC_INFO *info);
412 static void mgslpc_remove_device(MGSLPC_INFO *info);
414 static bool rx_get_frame(MGSLPC_INFO *info);
415 static void rx_reset_buffers(MGSLPC_INFO *info);
416 static int rx_alloc_buffers(MGSLPC_INFO *info);
417 static void rx_free_buffers(MGSLPC_INFO *info);
419 static irqreturn_t mgslpc_isr(int irq, void *dev_id);
422 * Bottom half interrupt handlers
424 static void bh_handler(struct work_struct *work);
425 static void bh_transmit(MGSLPC_INFO *info);
426 static void bh_status(MGSLPC_INFO *info);
431 static int tiocmget(struct tty_struct *tty, struct file *file);
432 static int tiocmset(struct tty_struct *tty, struct file *file,
433 unsigned int set, unsigned int clear);
434 static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
435 static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
436 static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params);
437 static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
438 static int set_txidle(MGSLPC_INFO *info, int idle_mode);
439 static int set_txenable(MGSLPC_INFO *info, int enable);
440 static int tx_abort(MGSLPC_INFO *info);
441 static int set_rxenable(MGSLPC_INFO *info, int enable);
442 static int wait_events(MGSLPC_INFO *info, int __user *mask);
444 static MGSLPC_INFO *mgslpc_device_list = NULL;
445 static int mgslpc_device_count = 0;
448 * Set this param to non-zero to load eax with the
449 * .text section address and breakpoint on module load.
450 * This is useful for use with gdb and add-symbol-file command.
452 static int break_on_load=0;
455 * Driver major number, defaults to zero to get auto
456 * assigned major number. May be forced as module parameter.
458 static int ttymajor=0;
460 static int debug_level = 0;
461 static int maxframe[MAX_DEVICE_COUNT] = {0,};
462 static int dosyncppp[MAX_DEVICE_COUNT] = {1,1,1,1};
464 module_param(break_on_load, bool, 0);
465 module_param(ttymajor, int, 0);
466 module_param(debug_level, int, 0);
467 module_param_array(maxframe, int, NULL, 0);
468 module_param_array(dosyncppp, int, NULL, 0);
470 MODULE_LICENSE("GPL");
472 static char *driver_name = "SyncLink PC Card driver";
473 static char *driver_version = "$Revision: 4.34 $";
475 static struct tty_driver *serial_driver;
477 /* number of characters left in xmit buffer before we ask for more */
478 #define WAKEUP_CHARS 256
480 static void mgslpc_change_params(MGSLPC_INFO *info);
481 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
483 /* PCMCIA prototypes */
485 static int mgslpc_config(struct pcmcia_device *link);
486 static void mgslpc_release(u_long arg);
487 static void mgslpc_detach(struct pcmcia_device *p_dev);
490 * 1st function defined in .text section. Calling this function in
491 * init_module() followed by a breakpoint allows a remote debugger
492 * (gdb) to get the .text address for the add-symbol-file command.
493 * This allows remote debugging of dynamically loadable modules.
495 static void* mgslpc_get_text_ptr(void)
497 return mgslpc_get_text_ptr;
501 * line discipline callback wrappers
503 * The wrappers maintain line discipline references
504 * while calling into the line discipline.
506 * ldisc_receive_buf - pass receive data to line discipline
509 static void ldisc_receive_buf(struct tty_struct *tty,
510 const __u8 *data, char *flags, int count)
512 struct tty_ldisc *ld;
515 ld = tty_ldisc_ref(tty);
517 if (ld->ops->receive_buf)
518 ld->ops->receive_buf(tty, data, flags, count);
523 static int mgslpc_probe(struct pcmcia_device *link)
528 if (debug_level >= DEBUG_LEVEL_INFO)
529 printk("mgslpc_attach\n");
531 info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
533 printk("Error can't allocate device instance data\n");
537 info->magic = MGSLPC_MAGIC;
538 INIT_WORK(&info->task, bh_handler);
539 info->max_frame_size = 4096;
540 info->close_delay = 5*HZ/10;
541 info->closing_wait = 30*HZ;
542 init_waitqueue_head(&info->open_wait);
543 init_waitqueue_head(&info->close_wait);
544 init_waitqueue_head(&info->status_event_wait_q);
545 init_waitqueue_head(&info->event_wait_q);
546 spin_lock_init(&info->lock);
547 spin_lock_init(&info->netlock);
548 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
549 info->idle_mode = HDLC_TXIDLE_FLAGS;
550 info->imra_value = 0xffff;
551 info->imrb_value = 0xffff;
552 info->pim_value = 0xff;
557 /* Initialize the struct pcmcia_device structure */
559 /* Interrupt setup */
560 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
561 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
562 link->irq.Handler = NULL;
564 link->conf.Attributes = 0;
565 link->conf.IntType = INT_MEMORY_AND_IO;
567 ret = mgslpc_config(link);
571 mgslpc_add_device(info);
576 /* Card has been inserted.
579 #define CS_CHECK(fn, ret) \
580 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
582 static int mgslpc_config(struct pcmcia_device *link)
584 MGSLPC_INFO *info = link->priv;
587 int last_fn, last_ret;
589 cistpl_cftable_entry_t dflt = { 0 };
590 cistpl_cftable_entry_t *cfg;
592 if (debug_level >= DEBUG_LEVEL_INFO)
593 printk("mgslpc_config(0x%p)\n", link);
595 tuple.Attributes = 0;
596 tuple.TupleData = buf;
597 tuple.TupleDataMax = sizeof(buf);
598 tuple.TupleOffset = 0;
600 /* get CIS configuration entry */
602 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
603 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
605 cfg = &(parse.cftable_entry);
606 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
607 CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
609 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
613 link->conf.ConfigIndex = cfg->index;
614 link->conf.Attributes |= CONF_ENABLE_IRQ;
616 /* IO window settings */
617 link->io.NumPorts1 = 0;
618 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
619 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
620 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
621 if (!(io->flags & CISTPL_IO_8BIT))
622 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
623 if (!(io->flags & CISTPL_IO_16BIT))
624 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
625 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
626 link->io.BasePort1 = io->win[0].base;
627 link->io.NumPorts1 = io->win[0].len;
628 CS_CHECK(RequestIO, pcmcia_request_io(link, &link->io));
631 link->conf.Attributes = CONF_ENABLE_IRQ;
632 link->conf.IntType = INT_MEMORY_AND_IO;
633 link->conf.ConfigIndex = 8;
634 link->conf.Present = PRESENT_OPTION;
636 link->irq.Attributes |= IRQ_HANDLE_PRESENT;
637 link->irq.Handler = mgslpc_isr;
638 link->irq.Instance = info;
639 CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
641 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
643 info->io_base = link->io.BasePort1;
644 info->irq_level = link->irq.AssignedIRQ;
646 /* add to linked list of devices */
647 sprintf(info->node.dev_name, "mgslpc0");
648 info->node.major = info->node.minor = 0;
649 link->dev_node = &info->node;
651 printk(KERN_INFO "%s: index 0x%02x:",
652 info->node.dev_name, link->conf.ConfigIndex);
653 if (link->conf.Attributes & CONF_ENABLE_IRQ)
654 printk(", irq %d", link->irq.AssignedIRQ);
655 if (link->io.NumPorts1)
656 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
657 link->io.BasePort1+link->io.NumPorts1-1);
662 cs_error(link, last_fn, last_ret);
663 mgslpc_release((u_long)link);
667 /* Card has been removed.
668 * Unregister device and release PCMCIA configuration.
669 * If device is open, postpone until it is closed.
671 static void mgslpc_release(u_long arg)
673 struct pcmcia_device *link = (struct pcmcia_device *)arg;
675 if (debug_level >= DEBUG_LEVEL_INFO)
676 printk("mgslpc_release(0x%p)\n", link);
678 pcmcia_disable_device(link);
681 static void mgslpc_detach(struct pcmcia_device *link)
683 if (debug_level >= DEBUG_LEVEL_INFO)
684 printk("mgslpc_detach(0x%p)\n", link);
686 ((MGSLPC_INFO *)link->priv)->stop = 1;
687 mgslpc_release((u_long)link);
689 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
692 static int mgslpc_suspend(struct pcmcia_device *link)
694 MGSLPC_INFO *info = link->priv;
701 static int mgslpc_resume(struct pcmcia_device *link)
703 MGSLPC_INFO *info = link->priv;
711 static inline bool mgslpc_paranoia_check(MGSLPC_INFO *info,
712 char *name, const char *routine)
714 #ifdef MGSLPC_PARANOIA_CHECK
715 static const char *badmagic =
716 "Warning: bad magic number for mgsl struct (%s) in %s\n";
717 static const char *badinfo =
718 "Warning: null mgslpc_info for (%s) in %s\n";
721 printk(badinfo, name, routine);
724 if (info->magic != MGSLPC_MAGIC) {
725 printk(badmagic, name, routine);
736 #define CMD_RXFIFO BIT7 // release current rx FIFO
737 #define CMD_RXRESET BIT6 // receiver reset
738 #define CMD_RXFIFO_READ BIT5
739 #define CMD_START_TIMER BIT4
740 #define CMD_TXFIFO BIT3 // release current tx FIFO
741 #define CMD_TXEOM BIT1 // transmit end message
742 #define CMD_TXRESET BIT0 // transmit reset
744 static bool wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
747 /* wait for command completion */
748 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
756 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
758 wait_command_complete(info, channel);
759 write_reg(info, (unsigned char) (channel + CMDR), cmd);
762 static void tx_pause(struct tty_struct *tty)
764 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
767 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
769 if (debug_level >= DEBUG_LEVEL_INFO)
770 printk("tx_pause(%s)\n",info->device_name);
772 spin_lock_irqsave(&info->lock,flags);
773 if (info->tx_enabled)
775 spin_unlock_irqrestore(&info->lock,flags);
778 static void tx_release(struct tty_struct *tty)
780 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
783 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
785 if (debug_level >= DEBUG_LEVEL_INFO)
786 printk("tx_release(%s)\n",info->device_name);
788 spin_lock_irqsave(&info->lock,flags);
789 if (!info->tx_enabled)
791 spin_unlock_irqrestore(&info->lock,flags);
794 /* Return next bottom half action to perform.
795 * or 0 if nothing to do.
797 static int bh_action(MGSLPC_INFO *info)
802 spin_lock_irqsave(&info->lock,flags);
804 if (info->pending_bh & BH_RECEIVE) {
805 info->pending_bh &= ~BH_RECEIVE;
807 } else if (info->pending_bh & BH_TRANSMIT) {
808 info->pending_bh &= ~BH_TRANSMIT;
810 } else if (info->pending_bh & BH_STATUS) {
811 info->pending_bh &= ~BH_STATUS;
816 /* Mark BH routine as complete */
817 info->bh_running = false;
818 info->bh_requested = false;
821 spin_unlock_irqrestore(&info->lock,flags);
826 static void bh_handler(struct work_struct *work)
828 MGSLPC_INFO *info = container_of(work, MGSLPC_INFO, task);
834 if (debug_level >= DEBUG_LEVEL_BH)
835 printk( "%s(%d):bh_handler(%s) entry\n",
836 __FILE__,__LINE__,info->device_name);
838 info->bh_running = true;
840 while((action = bh_action(info)) != 0) {
842 /* Process work item */
843 if ( debug_level >= DEBUG_LEVEL_BH )
844 printk( "%s(%d):bh_handler() work item action=%d\n",
845 __FILE__,__LINE__,action);
850 while(rx_get_frame(info));
859 /* unknown work item ID */
860 printk("Unknown work item ID=%08X!\n", action);
865 if (debug_level >= DEBUG_LEVEL_BH)
866 printk( "%s(%d):bh_handler(%s) exit\n",
867 __FILE__,__LINE__,info->device_name);
870 static void bh_transmit(MGSLPC_INFO *info)
872 struct tty_struct *tty = info->tty;
873 if (debug_level >= DEBUG_LEVEL_BH)
874 printk("bh_transmit() entry on %s\n", info->device_name);
880 static void bh_status(MGSLPC_INFO *info)
882 info->ri_chkcount = 0;
883 info->dsr_chkcount = 0;
884 info->dcd_chkcount = 0;
885 info->cts_chkcount = 0;
888 /* eom: non-zero = end of frame */
889 static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
891 unsigned char data[2];
892 unsigned char fifo_count, read_count, i;
893 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
895 if (debug_level >= DEBUG_LEVEL_ISR)
896 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
898 if (!info->rx_enabled)
901 if (info->rx_frame_count >= info->rx_buf_count) {
902 /* no more free buffers */
903 issue_command(info, CHA, CMD_RXRESET);
904 info->pending_bh |= BH_RECEIVE;
905 info->rx_overflow = true;
906 info->icount.buf_overrun++;
911 /* end of frame, get FIFO count from RBCL register */
912 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
918 if (fifo_count == 1) {
920 data[0] = read_reg(info, CHA + RXFIFO);
923 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
925 fifo_count -= read_count;
926 if (!fifo_count && eom)
927 buf->status = data[--read_count];
929 for (i = 0; i < read_count; i++) {
930 if (buf->count >= info->max_frame_size) {
931 /* frame too large, reset receiver and reset current buffer */
932 issue_command(info, CHA, CMD_RXRESET);
936 *(buf->data + buf->count) = data[i];
939 } while (fifo_count);
942 info->pending_bh |= BH_RECEIVE;
943 info->rx_frame_count++;
945 if (info->rx_put >= info->rx_buf_count)
948 issue_command(info, CHA, CMD_RXFIFO);
951 static void rx_ready_async(MGSLPC_INFO *info, int tcd)
953 unsigned char data, status, flag;
956 struct tty_struct *tty = info->tty;
957 struct mgsl_icount *icount = &info->icount;
960 /* early termination, get FIFO count from RBCL register */
961 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
963 /* Zero fifo count could mean 0 or 32 bytes available.
964 * If BIT5 of STAR is set then at least 1 byte is available.
966 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
971 tty_buffer_request_room(tty, fifo_count);
972 /* Flush received async data to receive data buffer. */
974 data = read_reg(info, CHA + RXFIFO);
975 status = read_reg(info, CHA + RXFIFO);
981 // if no frameing/crc error then save data
983 // BIT6:framing error
985 if (status & (BIT7 + BIT6)) {
991 /* discard char if tty control flags say so */
992 if (status & info->ignore_status_mask)
995 status &= info->read_status_mask;
999 else if (status & BIT6)
1002 work += tty_insert_flip_char(tty, data, flag);
1004 issue_command(info, CHA, CMD_RXFIFO);
1006 if (debug_level >= DEBUG_LEVEL_ISR) {
1007 printk("%s(%d):rx_ready_async",
1009 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1010 __FILE__,__LINE__,icount->rx,icount->brk,
1011 icount->parity,icount->frame,icount->overrun);
1015 tty_flip_buffer_push(tty);
1019 static void tx_done(MGSLPC_INFO *info)
1021 if (!info->tx_active)
1024 info->tx_active = false;
1025 info->tx_aborting = false;
1027 if (info->params.mode == MGSL_MODE_ASYNC)
1030 info->tx_count = info->tx_put = info->tx_get = 0;
1031 del_timer(&info->tx_timer);
1033 if (info->drop_rts_on_tx_done) {
1035 if (info->serial_signals & SerialSignal_RTS) {
1036 info->serial_signals &= ~SerialSignal_RTS;
1039 info->drop_rts_on_tx_done = false;
1042 #if SYNCLINK_GENERIC_HDLC
1044 hdlcdev_tx_done(info);
1048 if (info->tty->stopped || info->tty->hw_stopped) {
1052 info->pending_bh |= BH_TRANSMIT;
1056 static void tx_ready(MGSLPC_INFO *info)
1058 unsigned char fifo_count = 32;
1061 if (debug_level >= DEBUG_LEVEL_ISR)
1062 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1064 if (info->params.mode == MGSL_MODE_HDLC) {
1065 if (!info->tx_active)
1068 if (info->tty->stopped || info->tty->hw_stopped) {
1072 if (!info->tx_count)
1073 info->tx_active = false;
1076 if (!info->tx_count)
1079 while (info->tx_count && fifo_count) {
1080 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
1083 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1085 write_reg16(info, CHA + TXFIFO,
1086 *((unsigned short*)(info->tx_buf + info->tx_get)));
1088 info->tx_count -= c;
1089 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1093 if (info->params.mode == MGSL_MODE_ASYNC) {
1094 if (info->tx_count < WAKEUP_CHARS)
1095 info->pending_bh |= BH_TRANSMIT;
1096 issue_command(info, CHA, CMD_TXFIFO);
1099 issue_command(info, CHA, CMD_TXFIFO);
1101 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1105 static void cts_change(MGSLPC_INFO *info)
1108 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1109 irq_disable(info, CHB, IRQ_CTS);
1111 if (info->serial_signals & SerialSignal_CTS)
1112 info->input_signal_events.cts_up++;
1114 info->input_signal_events.cts_down++;
1115 wake_up_interruptible(&info->status_event_wait_q);
1116 wake_up_interruptible(&info->event_wait_q);
1118 if (info->flags & ASYNC_CTS_FLOW) {
1119 if (info->tty->hw_stopped) {
1120 if (info->serial_signals & SerialSignal_CTS) {
1121 if (debug_level >= DEBUG_LEVEL_ISR)
1122 printk("CTS tx start...");
1124 info->tty->hw_stopped = 0;
1126 info->pending_bh |= BH_TRANSMIT;
1130 if (!(info->serial_signals & SerialSignal_CTS)) {
1131 if (debug_level >= DEBUG_LEVEL_ISR)
1132 printk("CTS tx stop...");
1134 info->tty->hw_stopped = 1;
1139 info->pending_bh |= BH_STATUS;
1142 static void dcd_change(MGSLPC_INFO *info)
1145 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1146 irq_disable(info, CHB, IRQ_DCD);
1148 if (info->serial_signals & SerialSignal_DCD) {
1149 info->input_signal_events.dcd_up++;
1152 info->input_signal_events.dcd_down++;
1153 #if SYNCLINK_GENERIC_HDLC
1154 if (info->netcount) {
1155 if (info->serial_signals & SerialSignal_DCD)
1156 netif_carrier_on(info->netdev);
1158 netif_carrier_off(info->netdev);
1161 wake_up_interruptible(&info->status_event_wait_q);
1162 wake_up_interruptible(&info->event_wait_q);
1164 if (info->flags & ASYNC_CHECK_CD) {
1165 if (debug_level >= DEBUG_LEVEL_ISR)
1166 printk("%s CD now %s...", info->device_name,
1167 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1168 if (info->serial_signals & SerialSignal_DCD)
1169 wake_up_interruptible(&info->open_wait);
1171 if (debug_level >= DEBUG_LEVEL_ISR)
1172 printk("doing serial hangup...");
1174 tty_hangup(info->tty);
1177 info->pending_bh |= BH_STATUS;
1180 static void dsr_change(MGSLPC_INFO *info)
1183 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1184 port_irq_disable(info, PVR_DSR);
1186 if (info->serial_signals & SerialSignal_DSR)
1187 info->input_signal_events.dsr_up++;
1189 info->input_signal_events.dsr_down++;
1190 wake_up_interruptible(&info->status_event_wait_q);
1191 wake_up_interruptible(&info->event_wait_q);
1192 info->pending_bh |= BH_STATUS;
1195 static void ri_change(MGSLPC_INFO *info)
1198 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1199 port_irq_disable(info, PVR_RI);
1201 if (info->serial_signals & SerialSignal_RI)
1202 info->input_signal_events.ri_up++;
1204 info->input_signal_events.ri_down++;
1205 wake_up_interruptible(&info->status_event_wait_q);
1206 wake_up_interruptible(&info->event_wait_q);
1207 info->pending_bh |= BH_STATUS;
1210 /* Interrupt service routine entry point.
1214 * irq interrupt number that caused interrupt
1215 * dev_id device ID supplied during interrupt registration
1217 static irqreturn_t mgslpc_isr(int dummy, void *dev_id)
1219 MGSLPC_INFO *info = dev_id;
1221 unsigned char gis, pis;
1224 if (debug_level >= DEBUG_LEVEL_ISR)
1225 printk("mgslpc_isr(%d) entry.\n", info->irq_level);
1227 if (!(info->p_dev->_locked))
1230 spin_lock(&info->lock);
1232 while ((gis = read_reg(info, CHA + GIS))) {
1233 if (debug_level >= DEBUG_LEVEL_ISR)
1234 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1236 if ((gis & 0x70) || count > 1000) {
1237 printk("synclink_cs:hardware failed or ejected\n");
1242 if (gis & (BIT1 + BIT0)) {
1243 isr = read_reg16(info, CHB + ISR);
1249 if (gis & (BIT3 + BIT2))
1251 isr = read_reg16(info, CHA + ISR);
1252 if (isr & IRQ_TIMER) {
1253 info->irq_occurred = true;
1254 irq_disable(info, CHA, IRQ_TIMER);
1258 if (isr & IRQ_EXITHUNT) {
1259 info->icount.exithunt++;
1260 wake_up_interruptible(&info->event_wait_q);
1262 if (isr & IRQ_BREAK_ON) {
1264 if (info->flags & ASYNC_SAK)
1267 if (isr & IRQ_RXTIME) {
1268 issue_command(info, CHA, CMD_RXFIFO_READ);
1270 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1271 if (info->params.mode == MGSL_MODE_HDLC)
1272 rx_ready_hdlc(info, isr & IRQ_RXEOM);
1274 rx_ready_async(info, isr & IRQ_RXEOM);
1278 if (isr & IRQ_UNDERRUN) {
1279 if (info->tx_aborting)
1280 info->icount.txabort++;
1282 info->icount.txunder++;
1285 else if (isr & IRQ_ALLSENT) {
1286 info->icount.txok++;
1289 else if (isr & IRQ_TXFIFO)
1293 pis = read_reg(info, CHA + PIS);
1301 /* Request bottom half processing if there's something
1302 * for it to do and the bh is not already running
1305 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1306 if ( debug_level >= DEBUG_LEVEL_ISR )
1307 printk("%s(%d):%s queueing bh task.\n",
1308 __FILE__,__LINE__,info->device_name);
1309 schedule_work(&info->task);
1310 info->bh_requested = true;
1313 spin_unlock(&info->lock);
1315 if (debug_level >= DEBUG_LEVEL_ISR)
1316 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1317 __FILE__, __LINE__, info->irq_level);
1322 /* Initialize and start device.
1324 static int startup(MGSLPC_INFO * info)
1328 if (debug_level >= DEBUG_LEVEL_INFO)
1329 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1331 if (info->flags & ASYNC_INITIALIZED)
1334 if (!info->tx_buf) {
1335 /* allocate a page of memory for a transmit buffer */
1336 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1337 if (!info->tx_buf) {
1338 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1339 __FILE__,__LINE__,info->device_name);
1344 info->pending_bh = 0;
1346 memset(&info->icount, 0, sizeof(info->icount));
1348 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
1350 /* Allocate and claim adapter resources */
1351 retval = claim_resources(info);
1353 /* perform existance check and diagnostics */
1355 retval = adapter_test(info);
1358 if (capable(CAP_SYS_ADMIN) && info->tty)
1359 set_bit(TTY_IO_ERROR, &info->tty->flags);
1360 release_resources(info);
1364 /* program hardware for current parameters */
1365 mgslpc_change_params(info);
1368 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1370 info->flags |= ASYNC_INITIALIZED;
1375 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1377 static void shutdown(MGSLPC_INFO * info)
1379 unsigned long flags;
1381 if (!(info->flags & ASYNC_INITIALIZED))
1384 if (debug_level >= DEBUG_LEVEL_INFO)
1385 printk("%s(%d):mgslpc_shutdown(%s)\n",
1386 __FILE__,__LINE__, info->device_name );
1388 /* clear status wait queue because status changes */
1389 /* can't happen after shutting down the hardware */
1390 wake_up_interruptible(&info->status_event_wait_q);
1391 wake_up_interruptible(&info->event_wait_q);
1393 del_timer_sync(&info->tx_timer);
1396 free_page((unsigned long) info->tx_buf);
1397 info->tx_buf = NULL;
1400 spin_lock_irqsave(&info->lock,flags);
1405 /* TODO:disable interrupts instead of reset to preserve signal states */
1408 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1409 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1413 spin_unlock_irqrestore(&info->lock,flags);
1415 release_resources(info);
1418 set_bit(TTY_IO_ERROR, &info->tty->flags);
1420 info->flags &= ~ASYNC_INITIALIZED;
1423 static void mgslpc_program_hw(MGSLPC_INFO *info)
1425 unsigned long flags;
1427 spin_lock_irqsave(&info->lock,flags);
1431 info->tx_count = info->tx_put = info->tx_get = 0;
1433 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1440 info->dcd_chkcount = 0;
1441 info->cts_chkcount = 0;
1442 info->ri_chkcount = 0;
1443 info->dsr_chkcount = 0;
1445 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1446 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1449 if (info->netcount || info->tty->termios->c_cflag & CREAD)
1452 spin_unlock_irqrestore(&info->lock,flags);
1455 /* Reconfigure adapter based on new parameters
1457 static void mgslpc_change_params(MGSLPC_INFO *info)
1462 if (!info->tty || !info->tty->termios)
1465 if (debug_level >= DEBUG_LEVEL_INFO)
1466 printk("%s(%d):mgslpc_change_params(%s)\n",
1467 __FILE__,__LINE__, info->device_name );
1469 cflag = info->tty->termios->c_cflag;
1471 /* if B0 rate (hangup) specified then negate DTR and RTS */
1472 /* otherwise assert DTR and RTS */
1474 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1476 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1478 /* byte size and parity */
1480 switch (cflag & CSIZE) {
1481 case CS5: info->params.data_bits = 5; break;
1482 case CS6: info->params.data_bits = 6; break;
1483 case CS7: info->params.data_bits = 7; break;
1484 case CS8: info->params.data_bits = 8; break;
1485 default: info->params.data_bits = 7; break;
1489 info->params.stop_bits = 2;
1491 info->params.stop_bits = 1;
1493 info->params.parity = ASYNC_PARITY_NONE;
1494 if (cflag & PARENB) {
1496 info->params.parity = ASYNC_PARITY_ODD;
1498 info->params.parity = ASYNC_PARITY_EVEN;
1501 info->params.parity = ASYNC_PARITY_SPACE;
1505 /* calculate number of jiffies to transmit a full
1506 * FIFO (32 bytes) at specified data rate
1508 bits_per_char = info->params.data_bits +
1509 info->params.stop_bits + 1;
1511 /* if port data rate is set to 460800 or less then
1512 * allow tty settings to override, otherwise keep the
1513 * current data rate.
1515 if (info->params.data_rate <= 460800) {
1516 info->params.data_rate = tty_get_baud_rate(info->tty);
1519 if ( info->params.data_rate ) {
1520 info->timeout = (32*HZ*bits_per_char) /
1521 info->params.data_rate;
1523 info->timeout += HZ/50; /* Add .02 seconds of slop */
1525 if (cflag & CRTSCTS)
1526 info->flags |= ASYNC_CTS_FLOW;
1528 info->flags &= ~ASYNC_CTS_FLOW;
1531 info->flags &= ~ASYNC_CHECK_CD;
1533 info->flags |= ASYNC_CHECK_CD;
1535 /* process tty input control flags */
1537 info->read_status_mask = 0;
1538 if (I_INPCK(info->tty))
1539 info->read_status_mask |= BIT7 | BIT6;
1540 if (I_IGNPAR(info->tty))
1541 info->ignore_status_mask |= BIT7 | BIT6;
1543 mgslpc_program_hw(info);
1546 /* Add a character to the transmit buffer
1548 static int mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1550 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1551 unsigned long flags;
1553 if (debug_level >= DEBUG_LEVEL_INFO) {
1554 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1555 __FILE__,__LINE__,ch,info->device_name);
1558 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1564 spin_lock_irqsave(&info->lock,flags);
1566 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1567 if (info->tx_count < TXBUFSIZE - 1) {
1568 info->tx_buf[info->tx_put++] = ch;
1569 info->tx_put &= TXBUFSIZE-1;
1574 spin_unlock_irqrestore(&info->lock,flags);
1578 /* Enable transmitter so remaining characters in the
1579 * transmit buffer are sent.
1581 static void mgslpc_flush_chars(struct tty_struct *tty)
1583 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1584 unsigned long flags;
1586 if (debug_level >= DEBUG_LEVEL_INFO)
1587 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1588 __FILE__,__LINE__,info->device_name,info->tx_count);
1590 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1593 if (info->tx_count <= 0 || tty->stopped ||
1594 tty->hw_stopped || !info->tx_buf)
1597 if (debug_level >= DEBUG_LEVEL_INFO)
1598 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1599 __FILE__,__LINE__,info->device_name);
1601 spin_lock_irqsave(&info->lock,flags);
1602 if (!info->tx_active)
1604 spin_unlock_irqrestore(&info->lock,flags);
1607 /* Send a block of data
1611 * tty pointer to tty information structure
1612 * buf pointer to buffer containing send data
1613 * count size of send data in bytes
1615 * Returns: number of characters written
1617 static int mgslpc_write(struct tty_struct * tty,
1618 const unsigned char *buf, int count)
1621 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1622 unsigned long flags;
1624 if (debug_level >= DEBUG_LEVEL_INFO)
1625 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1626 __FILE__,__LINE__,info->device_name,count);
1628 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1632 if (info->params.mode == MGSL_MODE_HDLC) {
1633 if (count > TXBUFSIZE) {
1637 if (info->tx_active)
1639 else if (info->tx_count)
1645 min(TXBUFSIZE - info->tx_count - 1,
1646 TXBUFSIZE - info->tx_put));
1650 memcpy(info->tx_buf + info->tx_put, buf, c);
1652 spin_lock_irqsave(&info->lock,flags);
1653 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1654 info->tx_count += c;
1655 spin_unlock_irqrestore(&info->lock,flags);
1662 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1663 spin_lock_irqsave(&info->lock,flags);
1664 if (!info->tx_active)
1666 spin_unlock_irqrestore(&info->lock,flags);
1669 if (debug_level >= DEBUG_LEVEL_INFO)
1670 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1671 __FILE__,__LINE__,info->device_name,ret);
1675 /* Return the count of free bytes in transmit buffer
1677 static int mgslpc_write_room(struct tty_struct *tty)
1679 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1682 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1685 if (info->params.mode == MGSL_MODE_HDLC) {
1686 /* HDLC (frame oriented) mode */
1687 if (info->tx_active)
1690 return HDLC_MAX_FRAME_SIZE;
1692 ret = TXBUFSIZE - info->tx_count - 1;
1697 if (debug_level >= DEBUG_LEVEL_INFO)
1698 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1699 __FILE__,__LINE__, info->device_name, ret);
1703 /* Return the count of bytes in transmit buffer
1705 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1707 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1710 if (debug_level >= DEBUG_LEVEL_INFO)
1711 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1712 __FILE__,__LINE__, info->device_name );
1714 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1717 if (info->params.mode == MGSL_MODE_HDLC)
1718 rc = info->tx_active ? info->max_frame_size : 0;
1720 rc = info->tx_count;
1722 if (debug_level >= DEBUG_LEVEL_INFO)
1723 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1724 __FILE__,__LINE__, info->device_name, rc);
1729 /* Discard all data in the send buffer
1731 static void mgslpc_flush_buffer(struct tty_struct *tty)
1733 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1734 unsigned long flags;
1736 if (debug_level >= DEBUG_LEVEL_INFO)
1737 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1738 __FILE__,__LINE__, info->device_name );
1740 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1743 spin_lock_irqsave(&info->lock,flags);
1744 info->tx_count = info->tx_put = info->tx_get = 0;
1745 del_timer(&info->tx_timer);
1746 spin_unlock_irqrestore(&info->lock,flags);
1748 wake_up_interruptible(&tty->write_wait);
1752 /* Send a high-priority XON/XOFF character
1754 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1756 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1757 unsigned long flags;
1759 if (debug_level >= DEBUG_LEVEL_INFO)
1760 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1761 __FILE__,__LINE__, info->device_name, ch );
1763 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1768 spin_lock_irqsave(&info->lock,flags);
1769 if (!info->tx_enabled)
1771 spin_unlock_irqrestore(&info->lock,flags);
1775 /* Signal remote device to throttle send data (our receive data)
1777 static void mgslpc_throttle(struct tty_struct * tty)
1779 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1780 unsigned long flags;
1782 if (debug_level >= DEBUG_LEVEL_INFO)
1783 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1784 __FILE__,__LINE__, info->device_name );
1786 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1790 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1792 if (tty->termios->c_cflag & CRTSCTS) {
1793 spin_lock_irqsave(&info->lock,flags);
1794 info->serial_signals &= ~SerialSignal_RTS;
1796 spin_unlock_irqrestore(&info->lock,flags);
1800 /* Signal remote device to stop throttling send data (our receive data)
1802 static void mgslpc_unthrottle(struct tty_struct * tty)
1804 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1805 unsigned long flags;
1807 if (debug_level >= DEBUG_LEVEL_INFO)
1808 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1809 __FILE__,__LINE__, info->device_name );
1811 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1818 mgslpc_send_xchar(tty, START_CHAR(tty));
1821 if (tty->termios->c_cflag & CRTSCTS) {
1822 spin_lock_irqsave(&info->lock,flags);
1823 info->serial_signals |= SerialSignal_RTS;
1825 spin_unlock_irqrestore(&info->lock,flags);
1829 /* get the current serial statistics
1831 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1834 if (debug_level >= DEBUG_LEVEL_INFO)
1835 printk("get_params(%s)\n", info->device_name);
1837 memset(&info->icount, 0, sizeof(info->icount));
1839 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1846 /* get the current serial parameters
1848 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1851 if (debug_level >= DEBUG_LEVEL_INFO)
1852 printk("get_params(%s)\n", info->device_name);
1853 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1859 /* set the serial parameters
1863 * info pointer to device instance data
1864 * new_params user buffer containing new serial params
1866 * Returns: 0 if success, otherwise error code
1868 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params)
1870 unsigned long flags;
1871 MGSL_PARAMS tmp_params;
1874 if (debug_level >= DEBUG_LEVEL_INFO)
1875 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1876 info->device_name );
1877 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1879 if ( debug_level >= DEBUG_LEVEL_INFO )
1880 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1881 __FILE__,__LINE__,info->device_name);
1885 spin_lock_irqsave(&info->lock,flags);
1886 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1887 spin_unlock_irqrestore(&info->lock,flags);
1889 mgslpc_change_params(info);
1894 static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1897 if (debug_level >= DEBUG_LEVEL_INFO)
1898 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1899 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1905 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1907 unsigned long flags;
1908 if (debug_level >= DEBUG_LEVEL_INFO)
1909 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1910 spin_lock_irqsave(&info->lock,flags);
1911 info->idle_mode = idle_mode;
1913 spin_unlock_irqrestore(&info->lock,flags);
1917 static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1920 if (debug_level >= DEBUG_LEVEL_INFO)
1921 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1922 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1928 static int set_interface(MGSLPC_INFO * info, int if_mode)
1930 unsigned long flags;
1932 if (debug_level >= DEBUG_LEVEL_INFO)
1933 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1934 spin_lock_irqsave(&info->lock,flags);
1935 info->if_mode = if_mode;
1937 val = read_reg(info, PVR) & 0x0f;
1938 switch (info->if_mode)
1940 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
1941 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
1942 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
1944 write_reg(info, PVR, val);
1946 spin_unlock_irqrestore(&info->lock,flags);
1950 static int set_txenable(MGSLPC_INFO * info, int enable)
1952 unsigned long flags;
1954 if (debug_level >= DEBUG_LEVEL_INFO)
1955 printk("set_txenable(%s,%d)\n", info->device_name, enable);
1957 spin_lock_irqsave(&info->lock,flags);
1959 if (!info->tx_enabled)
1962 if (info->tx_enabled)
1965 spin_unlock_irqrestore(&info->lock,flags);
1969 static int tx_abort(MGSLPC_INFO * info)
1971 unsigned long flags;
1973 if (debug_level >= DEBUG_LEVEL_INFO)
1974 printk("tx_abort(%s)\n", info->device_name);
1976 spin_lock_irqsave(&info->lock,flags);
1977 if (info->tx_active && info->tx_count &&
1978 info->params.mode == MGSL_MODE_HDLC) {
1979 /* clear data count so FIFO is not filled on next IRQ.
1980 * This results in underrun and abort transmission.
1982 info->tx_count = info->tx_put = info->tx_get = 0;
1983 info->tx_aborting = true;
1985 spin_unlock_irqrestore(&info->lock,flags);
1989 static int set_rxenable(MGSLPC_INFO * info, int enable)
1991 unsigned long flags;
1993 if (debug_level >= DEBUG_LEVEL_INFO)
1994 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
1996 spin_lock_irqsave(&info->lock,flags);
1998 if (!info->rx_enabled)
2001 if (info->rx_enabled)
2004 spin_unlock_irqrestore(&info->lock,flags);
2008 /* wait for specified event to occur
2010 * Arguments: info pointer to device instance data
2011 * mask pointer to bitmask of events to wait for
2012 * Return Value: 0 if successful and bit mask updated with
2013 * of events triggerred,
2014 * otherwise error code
2016 static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
2018 unsigned long flags;
2021 struct mgsl_icount cprev, cnow;
2024 struct _input_signal_events oldsigs, newsigs;
2025 DECLARE_WAITQUEUE(wait, current);
2027 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2031 if (debug_level >= DEBUG_LEVEL_INFO)
2032 printk("wait_events(%s,%d)\n", info->device_name, mask);
2034 spin_lock_irqsave(&info->lock,flags);
2036 /* return immediately if state matches requested events */
2038 s = info->serial_signals;
2040 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2041 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2042 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2043 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2045 spin_unlock_irqrestore(&info->lock,flags);
2049 /* save current irq counts */
2050 cprev = info->icount;
2051 oldsigs = info->input_signal_events;
2053 if ((info->params.mode == MGSL_MODE_HDLC) &&
2054 (mask & MgslEvent_ExitHuntMode))
2055 irq_enable(info, CHA, IRQ_EXITHUNT);
2057 set_current_state(TASK_INTERRUPTIBLE);
2058 add_wait_queue(&info->event_wait_q, &wait);
2060 spin_unlock_irqrestore(&info->lock,flags);
2065 if (signal_pending(current)) {
2070 /* get current irq counts */
2071 spin_lock_irqsave(&info->lock,flags);
2072 cnow = info->icount;
2073 newsigs = info->input_signal_events;
2074 set_current_state(TASK_INTERRUPTIBLE);
2075 spin_unlock_irqrestore(&info->lock,flags);
2077 /* if no change, wait aborted for some reason */
2078 if (newsigs.dsr_up == oldsigs.dsr_up &&
2079 newsigs.dsr_down == oldsigs.dsr_down &&
2080 newsigs.dcd_up == oldsigs.dcd_up &&
2081 newsigs.dcd_down == oldsigs.dcd_down &&
2082 newsigs.cts_up == oldsigs.cts_up &&
2083 newsigs.cts_down == oldsigs.cts_down &&
2084 newsigs.ri_up == oldsigs.ri_up &&
2085 newsigs.ri_down == oldsigs.ri_down &&
2086 cnow.exithunt == cprev.exithunt &&
2087 cnow.rxidle == cprev.rxidle) {
2093 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2094 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2095 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2096 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2097 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2098 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2099 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2100 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2101 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2102 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2110 remove_wait_queue(&info->event_wait_q, &wait);
2111 set_current_state(TASK_RUNNING);
2113 if (mask & MgslEvent_ExitHuntMode) {
2114 spin_lock_irqsave(&info->lock,flags);
2115 if (!waitqueue_active(&info->event_wait_q))
2116 irq_disable(info, CHA, IRQ_EXITHUNT);
2117 spin_unlock_irqrestore(&info->lock,flags);
2121 PUT_USER(rc, events, mask_ptr);
2125 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2127 unsigned long flags;
2129 struct mgsl_icount cprev, cnow;
2130 DECLARE_WAITQUEUE(wait, current);
2132 /* save current irq counts */
2133 spin_lock_irqsave(&info->lock,flags);
2134 cprev = info->icount;
2135 add_wait_queue(&info->status_event_wait_q, &wait);
2136 set_current_state(TASK_INTERRUPTIBLE);
2137 spin_unlock_irqrestore(&info->lock,flags);
2141 if (signal_pending(current)) {
2146 /* get new irq counts */
2147 spin_lock_irqsave(&info->lock,flags);
2148 cnow = info->icount;
2149 set_current_state(TASK_INTERRUPTIBLE);
2150 spin_unlock_irqrestore(&info->lock,flags);
2152 /* if no change, wait aborted for some reason */
2153 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2154 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2159 /* check for change in caller specified modem input */
2160 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2161 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2162 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2163 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2170 remove_wait_queue(&info->status_event_wait_q, &wait);
2171 set_current_state(TASK_RUNNING);
2175 /* return the state of the serial control and status signals
2177 static int tiocmget(struct tty_struct *tty, struct file *file)
2179 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2180 unsigned int result;
2181 unsigned long flags;
2183 spin_lock_irqsave(&info->lock,flags);
2185 spin_unlock_irqrestore(&info->lock,flags);
2187 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2188 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2189 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2190 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2191 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2192 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2194 if (debug_level >= DEBUG_LEVEL_INFO)
2195 printk("%s(%d):%s tiocmget() value=%08X\n",
2196 __FILE__,__LINE__, info->device_name, result );
2200 /* set modem control signals (DTR/RTS)
2202 static int tiocmset(struct tty_struct *tty, struct file *file,
2203 unsigned int set, unsigned int clear)
2205 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2206 unsigned long flags;
2208 if (debug_level >= DEBUG_LEVEL_INFO)
2209 printk("%s(%d):%s tiocmset(%x,%x)\n",
2210 __FILE__,__LINE__,info->device_name, set, clear);
2212 if (set & TIOCM_RTS)
2213 info->serial_signals |= SerialSignal_RTS;
2214 if (set & TIOCM_DTR)
2215 info->serial_signals |= SerialSignal_DTR;
2216 if (clear & TIOCM_RTS)
2217 info->serial_signals &= ~SerialSignal_RTS;
2218 if (clear & TIOCM_DTR)
2219 info->serial_signals &= ~SerialSignal_DTR;
2221 spin_lock_irqsave(&info->lock,flags);
2223 spin_unlock_irqrestore(&info->lock,flags);
2228 /* Set or clear transmit break condition
2230 * Arguments: tty pointer to tty instance data
2231 * break_state -1=set break condition, 0=clear
2233 static int mgslpc_break(struct tty_struct *tty, int break_state)
2235 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2236 unsigned long flags;
2238 if (debug_level >= DEBUG_LEVEL_INFO)
2239 printk("%s(%d):mgslpc_break(%s,%d)\n",
2240 __FILE__,__LINE__, info->device_name, break_state);
2242 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2245 spin_lock_irqsave(&info->lock,flags);
2246 if (break_state == -1)
2247 set_reg_bits(info, CHA+DAFO, BIT6);
2249 clear_reg_bits(info, CHA+DAFO, BIT6);
2250 spin_unlock_irqrestore(&info->lock,flags);
2254 /* Service an IOCTL request
2258 * tty pointer to tty instance data
2259 * file pointer to associated file object for device
2260 * cmd IOCTL command code
2261 * arg command argument/context
2263 * Return Value: 0 if success, otherwise error code
2265 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2266 unsigned int cmd, unsigned long arg)
2268 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2270 if (debug_level >= DEBUG_LEVEL_INFO)
2271 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2272 info->device_name, cmd );
2274 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2277 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2278 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2279 if (tty->flags & (1 << TTY_IO_ERROR))
2283 return ioctl_common(info, cmd, arg);
2286 static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
2289 struct mgsl_icount cnow; /* kernel counter temps */
2290 struct serial_icounter_struct __user *p_cuser; /* user space */
2291 void __user *argp = (void __user *)arg;
2292 unsigned long flags;
2295 case MGSL_IOCGPARAMS:
2296 return get_params(info, argp);
2297 case MGSL_IOCSPARAMS:
2298 return set_params(info, argp);
2299 case MGSL_IOCGTXIDLE:
2300 return get_txidle(info, argp);
2301 case MGSL_IOCSTXIDLE:
2302 return set_txidle(info, (int)arg);
2304 return get_interface(info, argp);
2306 return set_interface(info,(int)arg);
2307 case MGSL_IOCTXENABLE:
2308 return set_txenable(info,(int)arg);
2309 case MGSL_IOCRXENABLE:
2310 return set_rxenable(info,(int)arg);
2311 case MGSL_IOCTXABORT:
2312 return tx_abort(info);
2313 case MGSL_IOCGSTATS:
2314 return get_stats(info, argp);
2315 case MGSL_IOCWAITEVENT:
2316 return wait_events(info, argp);
2318 return modem_input_wait(info,(int)arg);
2320 spin_lock_irqsave(&info->lock,flags);
2321 cnow = info->icount;
2322 spin_unlock_irqrestore(&info->lock,flags);
2324 PUT_USER(error,cnow.cts, &p_cuser->cts);
2325 if (error) return error;
2326 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2327 if (error) return error;
2328 PUT_USER(error,cnow.rng, &p_cuser->rng);
2329 if (error) return error;
2330 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2331 if (error) return error;
2332 PUT_USER(error,cnow.rx, &p_cuser->rx);
2333 if (error) return error;
2334 PUT_USER(error,cnow.tx, &p_cuser->tx);
2335 if (error) return error;
2336 PUT_USER(error,cnow.frame, &p_cuser->frame);
2337 if (error) return error;
2338 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2339 if (error) return error;
2340 PUT_USER(error,cnow.parity, &p_cuser->parity);
2341 if (error) return error;
2342 PUT_USER(error,cnow.brk, &p_cuser->brk);
2343 if (error) return error;
2344 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2345 if (error) return error;
2348 return -ENOIOCTLCMD;
2353 /* Set new termios settings
2357 * tty pointer to tty structure
2358 * termios pointer to buffer to hold returned old termios
2360 static void mgslpc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2362 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2363 unsigned long flags;
2365 if (debug_level >= DEBUG_LEVEL_INFO)
2366 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2367 tty->driver->name );
2369 /* just return if nothing has changed */
2370 if ((tty->termios->c_cflag == old_termios->c_cflag)
2371 && (RELEVANT_IFLAG(tty->termios->c_iflag)
2372 == RELEVANT_IFLAG(old_termios->c_iflag)))
2375 mgslpc_change_params(info);
2377 /* Handle transition to B0 status */
2378 if (old_termios->c_cflag & CBAUD &&
2379 !(tty->termios->c_cflag & CBAUD)) {
2380 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2381 spin_lock_irqsave(&info->lock,flags);
2383 spin_unlock_irqrestore(&info->lock,flags);
2386 /* Handle transition away from B0 status */
2387 if (!(old_termios->c_cflag & CBAUD) &&
2388 tty->termios->c_cflag & CBAUD) {
2389 info->serial_signals |= SerialSignal_DTR;
2390 if (!(tty->termios->c_cflag & CRTSCTS) ||
2391 !test_bit(TTY_THROTTLED, &tty->flags)) {
2392 info->serial_signals |= SerialSignal_RTS;
2394 spin_lock_irqsave(&info->lock,flags);
2396 spin_unlock_irqrestore(&info->lock,flags);
2399 /* Handle turning off CRTSCTS */
2400 if (old_termios->c_cflag & CRTSCTS &&
2401 !(tty->termios->c_cflag & CRTSCTS)) {
2402 tty->hw_stopped = 0;
2407 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2409 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2411 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2414 if (debug_level >= DEBUG_LEVEL_INFO)
2415 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2416 __FILE__,__LINE__, info->device_name, info->count);
2421 if (tty_hung_up_p(filp))
2424 if ((tty->count == 1) && (info->count != 1)) {
2426 * tty->count is 1 and the tty structure will be freed.
2427 * info->count should be one in this case.
2428 * if it's not, correct it so that the port is shutdown.
2430 printk("mgslpc_close: bad refcount; tty->count is 1, "
2431 "info->count is %d\n", info->count);
2437 /* if at least one open remaining, leave hardware active */
2441 info->flags |= ASYNC_CLOSING;
2443 /* set tty->closing to notify line discipline to
2444 * only process XON/XOFF characters. Only the N_TTY
2445 * discipline appears to use this (ppp does not).
2449 /* wait for transmit data to clear all layers */
2451 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
2452 if (debug_level >= DEBUG_LEVEL_INFO)
2453 printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sent\n",
2454 __FILE__,__LINE__, info->device_name );
2455 tty_wait_until_sent(tty, info->closing_wait);
2458 if (info->flags & ASYNC_INITIALIZED)
2459 mgslpc_wait_until_sent(tty, info->timeout);
2461 mgslpc_flush_buffer(tty);
2463 tty_ldisc_flush(tty);
2470 if (info->blocked_open) {
2471 if (info->close_delay) {
2472 msleep_interruptible(jiffies_to_msecs(info->close_delay));
2474 wake_up_interruptible(&info->open_wait);
2477 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2479 wake_up_interruptible(&info->close_wait);
2482 if (debug_level >= DEBUG_LEVEL_INFO)
2483 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2484 tty->driver->name, info->count);
2487 /* Wait until the transmitter is empty.
2489 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2491 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2492 unsigned long orig_jiffies, char_time;
2497 if (debug_level >= DEBUG_LEVEL_INFO)
2498 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2499 __FILE__,__LINE__, info->device_name );
2501 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2504 if (!(info->flags & ASYNC_INITIALIZED))
2507 orig_jiffies = jiffies;
2509 /* Set check interval to 1/5 of estimated time to
2510 * send a character, and make it at least 1. The check
2511 * interval should also be less than the timeout.
2512 * Note: use tight timings here to satisfy the NIST-PCTS.
2515 if ( info->params.data_rate ) {
2516 char_time = info->timeout/(32 * 5);
2523 char_time = min_t(unsigned long, char_time, timeout);
2525 if (info->params.mode == MGSL_MODE_HDLC) {
2526 while (info->tx_active) {
2527 msleep_interruptible(jiffies_to_msecs(char_time));
2528 if (signal_pending(current))
2530 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2534 while ((info->tx_count || info->tx_active) &&
2536 msleep_interruptible(jiffies_to_msecs(char_time));
2537 if (signal_pending(current))
2539 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2545 if (debug_level >= DEBUG_LEVEL_INFO)
2546 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2547 __FILE__,__LINE__, info->device_name );
2550 /* Called by tty_hangup() when a hangup is signaled.
2551 * This is the same as closing all open files for the port.
2553 static void mgslpc_hangup(struct tty_struct *tty)
2555 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2557 if (debug_level >= DEBUG_LEVEL_INFO)
2558 printk("%s(%d):mgslpc_hangup(%s)\n",
2559 __FILE__,__LINE__, info->device_name );
2561 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2564 mgslpc_flush_buffer(tty);
2568 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2571 wake_up_interruptible(&info->open_wait);
2574 /* Block the current process until the specified port
2575 * is ready to be opened.
2577 static int block_til_ready(struct tty_struct *tty, struct file *filp,
2580 DECLARE_WAITQUEUE(wait, current);
2582 bool do_clocal = false;
2583 bool extra_count = false;
2584 unsigned long flags;
2586 if (debug_level >= DEBUG_LEVEL_INFO)
2587 printk("%s(%d):block_til_ready on %s\n",
2588 __FILE__,__LINE__, tty->driver->name );
2590 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
2591 /* nonblock mode is set or port is not enabled */
2592 /* just verify that callout device is not active */
2593 info->flags |= ASYNC_NORMAL_ACTIVE;
2597 if (tty->termios->c_cflag & CLOCAL)
2600 /* Wait for carrier detect and the line to become
2601 * free (i.e., not in use by the callout). While we are in
2602 * this loop, info->count is dropped by one, so that
2603 * mgslpc_close() knows when to free things. We restore it upon
2604 * exit, either normal or abnormal.
2608 add_wait_queue(&info->open_wait, &wait);
2610 if (debug_level >= DEBUG_LEVEL_INFO)
2611 printk("%s(%d):block_til_ready before block on %s count=%d\n",
2612 __FILE__,__LINE__, tty->driver->name, info->count );
2614 spin_lock_irqsave(&info->lock, flags);
2615 if (!tty_hung_up_p(filp)) {
2619 spin_unlock_irqrestore(&info->lock, flags);
2620 info->blocked_open++;
2623 if ((tty->termios->c_cflag & CBAUD)) {
2624 spin_lock_irqsave(&info->lock,flags);
2625 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2627 spin_unlock_irqrestore(&info->lock,flags);
2630 set_current_state(TASK_INTERRUPTIBLE);
2632 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
2633 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
2634 -EAGAIN : -ERESTARTSYS;
2638 spin_lock_irqsave(&info->lock,flags);
2640 spin_unlock_irqrestore(&info->lock,flags);
2642 if (!(info->flags & ASYNC_CLOSING) &&
2643 (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
2647 if (signal_pending(current)) {
2648 retval = -ERESTARTSYS;
2652 if (debug_level >= DEBUG_LEVEL_INFO)
2653 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
2654 __FILE__,__LINE__, tty->driver->name, info->count );
2659 set_current_state(TASK_RUNNING);
2660 remove_wait_queue(&info->open_wait, &wait);
2664 info->blocked_open--;
2666 if (debug_level >= DEBUG_LEVEL_INFO)
2667 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
2668 __FILE__,__LINE__, tty->driver->name, info->count );
2671 info->flags |= ASYNC_NORMAL_ACTIVE;
2676 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2680 unsigned long flags;
2682 /* verify range of specified line number */
2684 if ((line < 0) || (line >= mgslpc_device_count)) {
2685 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2686 __FILE__,__LINE__,line);
2690 /* find the info structure for the specified line */
2691 info = mgslpc_device_list;
2692 while(info && info->line != line)
2693 info = info->next_device;
2694 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2697 tty->driver_data = info;
2700 if (debug_level >= DEBUG_LEVEL_INFO)
2701 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2702 __FILE__,__LINE__,tty->driver->name, info->count);
2704 /* If port is closing, signal caller to try again */
2705 if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
2706 if (info->flags & ASYNC_CLOSING)
2707 interruptible_sleep_on(&info->close_wait);
2708 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2709 -EAGAIN : -ERESTARTSYS);
2713 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2715 spin_lock_irqsave(&info->netlock, flags);
2716 if (info->netcount) {
2718 spin_unlock_irqrestore(&info->netlock, flags);
2722 spin_unlock_irqrestore(&info->netlock, flags);
2724 if (info->count == 1) {
2725 /* 1st open on this device, init hardware */
2726 retval = startup(info);
2731 retval = block_til_ready(tty, filp, info);
2733 if (debug_level >= DEBUG_LEVEL_INFO)
2734 printk("%s(%d):block_til_ready(%s) returned %d\n",
2735 __FILE__,__LINE__, info->device_name, retval);
2739 if (debug_level >= DEBUG_LEVEL_INFO)
2740 printk("%s(%d):mgslpc_open(%s) success\n",
2741 __FILE__,__LINE__, info->device_name);
2746 if (tty->count == 1)
2747 info->tty = NULL; /* tty layer will release tty struct */
2756 * /proc fs routines....
2759 static inline int line_info(char *buf, MGSLPC_INFO *info)
2763 unsigned long flags;
2765 ret = sprintf(buf, "%s:io:%04X irq:%d",
2766 info->device_name, info->io_base, info->irq_level);
2768 /* output current serial signal states */
2769 spin_lock_irqsave(&info->lock,flags);
2771 spin_unlock_irqrestore(&info->lock,flags);
2775 if (info->serial_signals & SerialSignal_RTS)
2776 strcat(stat_buf, "|RTS");
2777 if (info->serial_signals & SerialSignal_CTS)
2778 strcat(stat_buf, "|CTS");
2779 if (info->serial_signals & SerialSignal_DTR)
2780 strcat(stat_buf, "|DTR");
2781 if (info->serial_signals & SerialSignal_DSR)
2782 strcat(stat_buf, "|DSR");
2783 if (info->serial_signals & SerialSignal_DCD)
2784 strcat(stat_buf, "|CD");
2785 if (info->serial_signals & SerialSignal_RI)
2786 strcat(stat_buf, "|RI");
2788 if (info->params.mode == MGSL_MODE_HDLC) {
2789 ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
2790 info->icount.txok, info->icount.rxok);
2791 if (info->icount.txunder)
2792 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
2793 if (info->icount.txabort)
2794 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
2795 if (info->icount.rxshort)
2796 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
2797 if (info->icount.rxlong)
2798 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
2799 if (info->icount.rxover)
2800 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
2801 if (info->icount.rxcrc)
2802 ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
2804 ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
2805 info->icount.tx, info->icount.rx);
2806 if (info->icount.frame)
2807 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2808 if (info->icount.parity)
2809 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2810 if (info->icount.brk)
2811 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2812 if (info->icount.overrun)
2813 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2816 /* Append serial signal status to end */
2817 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2819 ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2820 info->tx_active,info->bh_requested,info->bh_running,
2826 /* Called to print information about devices
2828 static int mgslpc_read_proc(char *page, char **start, off_t off, int count,
2829 int *eof, void *data)
2835 len += sprintf(page, "synclink driver:%s\n", driver_version);
2837 info = mgslpc_device_list;
2839 l = line_info(page + len, info);
2841 if (len+begin > off+count)
2843 if (len+begin < off) {
2847 info = info->next_device;
2852 if (off >= len+begin)
2854 *start = page + (off-begin);
2855 return ((count < begin+len-off) ? count : begin+len-off);
2858 static int rx_alloc_buffers(MGSLPC_INFO *info)
2860 /* each buffer has header and data */
2861 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2863 /* calculate total allocation size for 8 buffers */
2864 info->rx_buf_total_size = info->rx_buf_size * 8;
2866 /* limit total allocated memory */
2867 if (info->rx_buf_total_size > 0x10000)
2868 info->rx_buf_total_size = 0x10000;
2870 /* calculate number of buffers */
2871 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2873 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2874 if (info->rx_buf == NULL)
2877 rx_reset_buffers(info);
2881 static void rx_free_buffers(MGSLPC_INFO *info)
2883 kfree(info->rx_buf);
2884 info->rx_buf = NULL;
2887 static int claim_resources(MGSLPC_INFO *info)
2889 if (rx_alloc_buffers(info) < 0 ) {
2890 printk( "Cant allocate rx buffer %s\n", info->device_name);
2891 release_resources(info);
2897 static void release_resources(MGSLPC_INFO *info)
2899 if (debug_level >= DEBUG_LEVEL_INFO)
2900 printk("release_resources(%s)\n", info->device_name);
2901 rx_free_buffers(info);
2904 /* Add the specified device instance data structure to the
2905 * global linked list of devices and increment the device count.
2907 * Arguments: info pointer to device instance data
2909 static void mgslpc_add_device(MGSLPC_INFO *info)
2911 info->next_device = NULL;
2912 info->line = mgslpc_device_count;
2913 sprintf(info->device_name,"ttySLP%d",info->line);
2915 if (info->line < MAX_DEVICE_COUNT) {
2916 if (maxframe[info->line])
2917 info->max_frame_size = maxframe[info->line];
2918 info->dosyncppp = dosyncppp[info->line];
2921 mgslpc_device_count++;
2923 if (!mgslpc_device_list)
2924 mgslpc_device_list = info;
2926 MGSLPC_INFO *current_dev = mgslpc_device_list;
2927 while( current_dev->next_device )
2928 current_dev = current_dev->next_device;
2929 current_dev->next_device = info;
2932 if (info->max_frame_size < 4096)
2933 info->max_frame_size = 4096;
2934 else if (info->max_frame_size > 65535)
2935 info->max_frame_size = 65535;
2937 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2938 info->device_name, info->io_base, info->irq_level);
2940 #if SYNCLINK_GENERIC_HDLC
2945 static void mgslpc_remove_device(MGSLPC_INFO *remove_info)
2947 MGSLPC_INFO *info = mgslpc_device_list;
2948 MGSLPC_INFO *last = NULL;
2951 if (info == remove_info) {
2953 last->next_device = info->next_device;
2955 mgslpc_device_list = info->next_device;
2956 #if SYNCLINK_GENERIC_HDLC
2959 release_resources(info);
2961 mgslpc_device_count--;
2965 info = info->next_device;
2969 static struct pcmcia_device_id mgslpc_ids[] = {
2970 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
2973 MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
2975 static struct pcmcia_driver mgslpc_driver = {
2976 .owner = THIS_MODULE,
2978 .name = "synclink_cs",
2980 .probe = mgslpc_probe,
2981 .remove = mgslpc_detach,
2982 .id_table = mgslpc_ids,
2983 .suspend = mgslpc_suspend,
2984 .resume = mgslpc_resume,
2987 static const struct tty_operations mgslpc_ops = {
2988 .open = mgslpc_open,
2989 .close = mgslpc_close,
2990 .write = mgslpc_write,
2991 .put_char = mgslpc_put_char,
2992 .flush_chars = mgslpc_flush_chars,
2993 .write_room = mgslpc_write_room,
2994 .chars_in_buffer = mgslpc_chars_in_buffer,
2995 .flush_buffer = mgslpc_flush_buffer,
2996 .ioctl = mgslpc_ioctl,
2997 .throttle = mgslpc_throttle,
2998 .unthrottle = mgslpc_unthrottle,
2999 .send_xchar = mgslpc_send_xchar,
3000 .break_ctl = mgslpc_break,
3001 .wait_until_sent = mgslpc_wait_until_sent,
3002 .read_proc = mgslpc_read_proc,
3003 .set_termios = mgslpc_set_termios,
3005 .start = tx_release,
3006 .hangup = mgslpc_hangup,
3007 .tiocmget = tiocmget,
3008 .tiocmset = tiocmset,
3011 static void synclink_cs_cleanup(void)
3015 printk("Unloading %s: version %s\n", driver_name, driver_version);
3017 while(mgslpc_device_list)
3018 mgslpc_remove_device(mgslpc_device_list);
3020 if (serial_driver) {
3021 if ((rc = tty_unregister_driver(serial_driver)))
3022 printk("%s(%d) failed to unregister tty driver err=%d\n",
3023 __FILE__,__LINE__,rc);
3024 put_tty_driver(serial_driver);
3027 pcmcia_unregister_driver(&mgslpc_driver);
3030 static int __init synclink_cs_init(void)
3034 if (break_on_load) {
3035 mgslpc_get_text_ptr();
3039 printk("%s %s\n", driver_name, driver_version);
3041 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
3044 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
3045 if (!serial_driver) {
3050 /* Initialize the tty_driver structure */
3052 serial_driver->owner = THIS_MODULE;
3053 serial_driver->driver_name = "synclink_cs";
3054 serial_driver->name = "ttySLP";
3055 serial_driver->major = ttymajor;
3056 serial_driver->minor_start = 64;
3057 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3058 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3059 serial_driver->init_termios = tty_std_termios;
3060 serial_driver->init_termios.c_cflag =
3061 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3062 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3063 tty_set_operations(serial_driver, &mgslpc_ops);
3065 if ((rc = tty_register_driver(serial_driver)) < 0) {
3066 printk("%s(%d):Couldn't register serial driver\n",
3068 put_tty_driver(serial_driver);
3069 serial_driver = NULL;
3073 printk("%s %s, tty major#%d\n",
3074 driver_name, driver_version,
3075 serial_driver->major);
3080 synclink_cs_cleanup();
3084 static void __exit synclink_cs_exit(void)
3086 synclink_cs_cleanup();
3089 module_init(synclink_cs_init);
3090 module_exit(synclink_cs_exit);
3092 static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
3097 /* note:standard BRG mode is broken in V3.2 chip
3098 * so enhanced mode is always used
3106 for (M = 1; N > 64 && M < 16; M++)
3112 * BGR[7..0] contained in BGR register
3113 * BGR[9..8] contained in CCR2[7..6]
3114 * divisor = (N+1)*2^M
3116 * Note: M *must* not be zero (causes asymetric duty cycle)
3118 write_reg(info, (unsigned char) (channel + BGR),
3119 (unsigned char) ((M << 6) + N));
3120 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
3121 val |= ((M << 4) & 0xc0);
3122 write_reg(info, (unsigned char) (channel + CCR2), val);
3126 /* Enabled the AUX clock output at the specified frequency.
3128 static void enable_auxclk(MGSLPC_INFO *info)
3134 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3135 * 05 ADM Address Mode, 0 = no addr recognition
3136 * 04 TMD Timer Mode, 0 = external
3137 * 03 RAC Receiver Active, 0 = inactive
3138 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3139 * 01 TRS Timer Resolution, 1=512
3140 * 00 TLP Test Loop, 0 = no loop
3146 /* channel B RTS is used to enable AUXCLK driver on SP505 */
3147 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3149 write_reg(info, CHB + MODE, val);
3153 * 07 PU Power Up, 1=active, 0=power down
3154 * 06 MCE Master Clock Enable, 1=enabled
3156 * 04..02 SC[2..0] Encoding
3157 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3161 write_reg(info, CHB + CCR0, 0xc0);
3165 * 07 SFLG Shared Flag, 0 = disable shared flags
3166 * 06 GALP Go Active On Loop, 0 = not used
3167 * 05 GLP Go On Loop, 0 = not used
3168 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3169 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3170 * 02..00 CM[2..0] Clock Mode
3174 write_reg(info, CHB + CCR1, 0x17);
3178 * 07..06 BGR[9..8] Baud rate bits 9..8
3179 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3180 * 04 SSEL Clock source select, 1=submode b
3181 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3182 * 02 RWX Read/Write Exchange 0=disabled
3183 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3184 * 00 DIV, data inversion 0=disabled, 1=enabled
3188 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3189 write_reg(info, CHB + CCR2, 0x38);
3191 write_reg(info, CHB + CCR2, 0x30);
3195 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3196 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3197 * 05 TST1 Test Pin, 0=normal operation
3198 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3199 * 03..02 Reserved, must be 0
3200 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3204 write_reg(info, CHB + CCR4, 0x50);
3206 /* if auxclk not enabled, set internal BRG so
3207 * CTS transitions can be detected (requires TxC)
3209 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3210 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3212 mgslpc_set_rate(info, CHB, 921600);
3215 static void loopback_enable(MGSLPC_INFO *info)
3219 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3220 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3221 write_reg(info, CHA + CCR1, val);
3223 /* CCR2:04 SSEL Clock source select, 1=submode b */
3224 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3225 write_reg(info, CHA + CCR2, val);
3227 /* set LinkSpeed if available, otherwise default to 2Mbps */
3228 if (info->params.clock_speed)
3229 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3231 mgslpc_set_rate(info, CHA, 1843200);
3233 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3234 val = read_reg(info, CHA + MODE) | BIT0;
3235 write_reg(info, CHA + MODE, val);
3238 static void hdlc_mode(MGSLPC_INFO *info)
3241 unsigned char clkmode, clksubmode;
3243 /* disable all interrupts */
3244 irq_disable(info, CHA, 0xffff);
3245 irq_disable(info, CHB, 0xffff);
3246 port_irq_disable(info, 0xff);
3248 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3249 clkmode = clksubmode = 0;
3250 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3251 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3252 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3254 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3255 && info->params.flags & HDLC_FLAG_TXC_BRG) {
3256 /* clock mode 7b, rcv = BRG, xmt = BRG */
3259 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3260 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3261 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3265 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3268 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3269 /* clock mode 0b, rcv = RxC, xmt = BRG */
3275 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3276 * 05 ADM Address Mode, 0 = no addr recognition
3277 * 04 TMD Timer Mode, 0 = external
3278 * 03 RAC Receiver Active, 0 = inactive
3279 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3280 * 01 TRS Timer Resolution, 1=512
3281 * 00 TLP Test Loop, 0 = no loop
3286 if (info->params.loopback)
3289 /* preserve RTS state */
3290 if (info->serial_signals & SerialSignal_RTS)
3292 write_reg(info, CHA + MODE, val);
3296 * 07 PU Power Up, 1=active, 0=power down
3297 * 06 MCE Master Clock Enable, 1=enabled
3299 * 04..02 SC[2..0] Encoding
3300 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3305 switch (info->params.encoding)
3307 case HDLC_ENCODING_NRZI:
3310 case HDLC_ENCODING_BIPHASE_SPACE:
3313 case HDLC_ENCODING_BIPHASE_MARK:
3316 case HDLC_ENCODING_BIPHASE_LEVEL:
3318 break; // Manchester
3320 write_reg(info, CHA + CCR0, val);
3324 * 07 SFLG Shared Flag, 0 = disable shared flags
3325 * 06 GALP Go Active On Loop, 0 = not used
3326 * 05 GLP Go On Loop, 0 = not used
3327 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3328 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3329 * 02..00 CM[2..0] Clock Mode
3333 val = 0x10 + clkmode;
3334 write_reg(info, CHA + CCR1, val);
3338 * 07..06 BGR[9..8] Baud rate bits 9..8
3339 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3340 * 04 SSEL Clock source select, 1=submode b
3341 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3342 * 02 RWX Read/Write Exchange 0=disabled
3343 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3344 * 00 DIV, data inversion 0=disabled, 1=enabled
3349 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3350 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3354 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3356 if (info->params.encoding == HDLC_ENCODING_NRZB)
3358 write_reg(info, CHA + CCR2, val);
3362 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3363 * 05 EPT Enable preamble transmission, 1=enabled
3364 * 04 RADD Receive address pushed to FIFO, 0=disabled
3365 * 03 CRL CRC Reset Level, 0=FFFF
3366 * 02 RCRC Rx CRC 0=On 1=Off
3367 * 01 TCRC Tx CRC 0=On 1=Off
3368 * 00 PSD DPLL Phase Shift Disable
3373 if (info->params.crc_type == HDLC_CRC_NONE)
3375 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3377 switch (info->params.preamble_length)
3379 case HDLC_PREAMBLE_LENGTH_16BITS:
3382 case HDLC_PREAMBLE_LENGTH_32BITS:
3385 case HDLC_PREAMBLE_LENGTH_64BITS:
3389 write_reg(info, CHA + CCR3, val);
3391 /* PRE - Preamble pattern */
3393 switch (info->params.preamble)
3395 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3396 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3397 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3398 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3400 write_reg(info, CHA + PRE, val);
3404 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3405 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3406 * 05 TST1 Test Pin, 0=normal operation
3407 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3408 * 03..02 Reserved, must be 0
3409 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3414 write_reg(info, CHA + CCR4, val);
3415 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3416 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3418 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3420 /* RLCR Receive length check register
3422 * 7 1=enable receive length check
3423 * 6..0 Max frame length = (RL + 1) * 32
3425 write_reg(info, CHA + RLCR, 0);
3427 /* XBCH Transmit Byte Count High
3429 * 07 DMA mode, 0 = interrupt driven
3430 * 06 NRM, 0=ABM (ignored)
3431 * 05 CAS Carrier Auto Start
3432 * 04 XC Transmit Continuously (ignored)
3433 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3438 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3440 write_reg(info, CHA + XBCH, val);
3441 enable_auxclk(info);
3442 if (info->params.loopback || info->testing_irq)
3443 loopback_enable(info);
3444 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3446 irq_enable(info, CHB, IRQ_CTS);
3447 /* PVR[3] 1=AUTO CTS active */
3448 set_reg_bits(info, CHA + PVR, BIT3);
3450 clear_reg_bits(info, CHA + PVR, BIT3);
3452 irq_enable(info, CHA,
3453 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3454 IRQ_UNDERRUN + IRQ_TXFIFO);
3455 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3456 wait_command_complete(info, CHA);
3457 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3459 /* Master clock mode enabled above to allow reset commands
3460 * to complete even if no data clocks are present.
3462 * Disable master clock mode for normal communications because
3463 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3464 * IRQ when in master clock mode.
3466 * Leave master clock mode enabled for IRQ test because the
3467 * timer IRQ used by the test can only happen in master clock mode.
3469 if (!info->testing_irq)
3470 clear_reg_bits(info, CHA + CCR0, BIT6);
3478 static void rx_stop(MGSLPC_INFO *info)
3480 if (debug_level >= DEBUG_LEVEL_ISR)
3481 printk("%s(%d):rx_stop(%s)\n",
3482 __FILE__,__LINE__, info->device_name );
3484 /* MODE:03 RAC Receiver Active, 0=inactive */
3485 clear_reg_bits(info, CHA + MODE, BIT3);
3487 info->rx_enabled = false;
3488 info->rx_overflow = false;
3491 static void rx_start(MGSLPC_INFO *info)
3493 if (debug_level >= DEBUG_LEVEL_ISR)
3494 printk("%s(%d):rx_start(%s)\n",
3495 __FILE__,__LINE__, info->device_name );
3497 rx_reset_buffers(info);
3498 info->rx_enabled = false;
3499 info->rx_overflow = false;
3501 /* MODE:03 RAC Receiver Active, 1=active */
3502 set_reg_bits(info, CHA + MODE, BIT3);
3504 info->rx_enabled = true;
3507 static void tx_start(MGSLPC_INFO *info)
3509 if (debug_level >= DEBUG_LEVEL_ISR)
3510 printk("%s(%d):tx_start(%s)\n",
3511 __FILE__,__LINE__, info->device_name );
3513 if (info->tx_count) {
3514 /* If auto RTS enabled and RTS is inactive, then assert */
3515 /* RTS and set a flag indicating that the driver should */
3516 /* negate RTS when the transmission completes. */
3517 info->drop_rts_on_tx_done = false;
3519 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3521 if (!(info->serial_signals & SerialSignal_RTS)) {
3522 info->serial_signals |= SerialSignal_RTS;
3524 info->drop_rts_on_tx_done = true;
3528 if (info->params.mode == MGSL_MODE_ASYNC) {
3529 if (!info->tx_active) {
3530 info->tx_active = true;
3534 info->tx_active = true;
3536 mod_timer(&info->tx_timer, jiffies +
3537 msecs_to_jiffies(5000));
3541 if (!info->tx_enabled)
3542 info->tx_enabled = true;
3545 static void tx_stop(MGSLPC_INFO *info)
3547 if (debug_level >= DEBUG_LEVEL_ISR)
3548 printk("%s(%d):tx_stop(%s)\n",
3549 __FILE__,__LINE__, info->device_name );
3551 del_timer(&info->tx_timer);
3553 info->tx_enabled = false;
3554 info->tx_active = false;
3557 /* Reset the adapter to a known state and prepare it for further use.
3559 static void reset_device(MGSLPC_INFO *info)
3561 /* power up both channels (set BIT7) */
3562 write_reg(info, CHA + CCR0, 0x80);
3563 write_reg(info, CHB + CCR0, 0x80);
3564 write_reg(info, CHA + MODE, 0);
3565 write_reg(info, CHB + MODE, 0);
3567 /* disable all interrupts */
3568 irq_disable(info, CHA, 0xffff);
3569 irq_disable(info, CHB, 0xffff);
3570 port_irq_disable(info, 0xff);
3572 /* PCR Port Configuration Register
3574 * 07..04 DEC[3..0] Serial I/F select outputs
3575 * 03 output, 1=AUTO CTS control enabled
3576 * 02 RI Ring Indicator input 0=active
3577 * 01 DSR input 0=active
3578 * 00 DTR output 0=active
3582 write_reg(info, PCR, 0x06);
3584 /* PVR Port Value Register
3586 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3587 * 03 AUTO CTS output 1=enabled
3588 * 02 RI Ring Indicator input
3590 * 00 DTR output (1=inactive)
3594 // write_reg(info, PVR, PVR_DTR);
3596 /* IPC Interrupt Port Configuration
3598 * 07 VIS 1=Masked interrupts visible
3599 * 06..05 Reserved, 0
3600 * 04..03 SLA Slave address, 00 ignored
3601 * 02 CASM Cascading Mode, 1=daisy chain
3602 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3606 write_reg(info, IPC, 0x05);
3609 static void async_mode(MGSLPC_INFO *info)
3613 /* disable all interrupts */
3614 irq_disable(info, CHA, 0xffff);
3615 irq_disable(info, CHB, 0xffff);
3616 port_irq_disable(info, 0xff);
3621 * 06 FRTS RTS State, 0=active
3622 * 05 FCTS Flow Control on CTS
3623 * 04 FLON Flow Control Enable
3624 * 03 RAC Receiver Active, 0 = inactive
3625 * 02 RTS 0=Auto RTS, 1=manual RTS
3626 * 01 TRS Timer Resolution, 1=512
3627 * 00 TLP Test Loop, 0 = no loop
3632 if (info->params.loopback)
3635 /* preserve RTS state */
3636 if (!(info->serial_signals & SerialSignal_RTS))
3638 write_reg(info, CHA + MODE, val);
3642 * 07 PU Power Up, 1=active, 0=power down
3643 * 06 MCE Master Clock Enable, 1=enabled
3645 * 04..02 SC[2..0] Encoding, 000=NRZ
3646 * 01..00 SM[1..0] Serial Mode, 11=Async
3650 write_reg(info, CHA + CCR0, 0x83);
3654 * 07..05 Reserved, 0
3655 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3656 * 03 BCR Bit Clock Rate, 1=16x
3657 * 02..00 CM[2..0] Clock Mode, 111=BRG
3661 write_reg(info, CHA + CCR1, 0x1f);
3665 * 07..06 BGR[9..8] Baud rate bits 9..8
3666 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3667 * 04 SSEL Clock source select, 1=submode b
3668 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3669 * 02 RWX Read/Write Exchange 0=disabled
3671 * 00 DIV, data inversion 0=disabled, 1=enabled
3675 write_reg(info, CHA + CCR2, 0x10);
3679 * 07..01 Reserved, 0
3680 * 00 PSD DPLL Phase Shift Disable
3684 write_reg(info, CHA + CCR3, 0);
3688 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3689 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3690 * 05 TST1 Test Pin, 0=normal operation
3691 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3692 * 03..00 Reserved, must be 0
3696 write_reg(info, CHA + CCR4, 0x50);
3697 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3702 * 06 XBRK transmit break, 0=normal operation
3703 * 05 Stop bits (0=1, 1=2)
3704 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3705 * 02 PAREN Parity Enable
3706 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3710 if (info->params.data_bits != 8)
3711 val |= BIT0; /* 7 bits */
3712 if (info->params.stop_bits != 1)
3714 if (info->params.parity != ASYNC_PARITY_NONE)
3716 val |= BIT2; /* Parity enable */
3717 if (info->params.parity == ASYNC_PARITY_ODD)
3722 write_reg(info, CHA + DAFO, val);
3724 /* RFC Rx FIFO Control
3727 * 06 DPS, 1=parity bit not stored in data byte
3728 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3729 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3730 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3732 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3736 write_reg(info, CHA + RFC, 0x5c);
3738 /* RLCR Receive length check register
3740 * Max frame length = (RL + 1) * 32
3742 write_reg(info, CHA + RLCR, 0);
3744 /* XBCH Transmit Byte Count High
3746 * 07 DMA mode, 0 = interrupt driven
3747 * 06 NRM, 0=ABM (ignored)
3748 * 05 CAS Carrier Auto Start
3749 * 04 XC Transmit Continuously (ignored)
3750 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3755 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3757 write_reg(info, CHA + XBCH, val);
3758 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3759 irq_enable(info, CHA, IRQ_CTS);
3761 /* MODE:03 RAC Receiver Active, 1=active */
3762 set_reg_bits(info, CHA + MODE, BIT3);
3763 enable_auxclk(info);
3764 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3765 irq_enable(info, CHB, IRQ_CTS);
3766 /* PVR[3] 1=AUTO CTS active */
3767 set_reg_bits(info, CHA + PVR, BIT3);
3769 clear_reg_bits(info, CHA + PVR, BIT3);
3770 irq_enable(info, CHA,
3771 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3772 IRQ_ALLSENT + IRQ_TXFIFO);
3773 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3774 wait_command_complete(info, CHA);
3775 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3778 /* Set the HDLC idle mode for the transmitter.
3780 static void tx_set_idle(MGSLPC_INFO *info)
3782 /* Note: ESCC2 only supports flags and one idle modes */
3783 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3784 set_reg_bits(info, CHA + CCR1, BIT3);
3786 clear_reg_bits(info, CHA + CCR1, BIT3);
3789 /* get state of the V24 status (input) signals.
3791 static void get_signals(MGSLPC_INFO *info)
3793 unsigned char status = 0;
3795 /* preserve DTR and RTS */
3796 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3798 if (read_reg(info, CHB + VSTR) & BIT7)
3799 info->serial_signals |= SerialSignal_DCD;
3800 if (read_reg(info, CHB + STAR) & BIT1)
3801 info->serial_signals |= SerialSignal_CTS;
3803 status = read_reg(info, CHA + PVR);
3804 if (!(status & PVR_RI))
3805 info->serial_signals |= SerialSignal_RI;
3806 if (!(status & PVR_DSR))
3807 info->serial_signals |= SerialSignal_DSR;
3810 /* Set the state of DTR and RTS based on contents of
3811 * serial_signals member of device extension.
3813 static void set_signals(MGSLPC_INFO *info)
3817 val = read_reg(info, CHA + MODE);
3818 if (info->params.mode == MGSL_MODE_ASYNC) {
3819 if (info->serial_signals & SerialSignal_RTS)
3824 if (info->serial_signals & SerialSignal_RTS)
3829 write_reg(info, CHA + MODE, val);
3831 if (info->serial_signals & SerialSignal_DTR)
3832 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3834 set_reg_bits(info, CHA + PVR, PVR_DTR);
3837 static void rx_reset_buffers(MGSLPC_INFO *info)
3844 info->rx_frame_count = 0;
3845 for (i=0 ; i < info->rx_buf_count ; i++) {
3846 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3847 buf->status = buf->count = 0;
3851 /* Attempt to return a received HDLC frame
3852 * Only frames received without errors are returned.
3854 * Returns true if frame returned, otherwise false
3856 static bool rx_get_frame(MGSLPC_INFO *info)
3858 unsigned short status;
3860 unsigned int framesize = 0;
3861 unsigned long flags;
3862 struct tty_struct *tty = info->tty;
3863 bool return_frame = false;
3865 if (info->rx_frame_count == 0)
3868 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3870 status = buf->status;
3872 /* 07 VFR 1=valid frame
3873 * 06 RDO 1=data overrun
3874 * 05 CRC 1=OK, 0=error
3875 * 04 RAB 1=frame aborted
3877 if ((status & 0xf0) != 0xA0) {
3878 if (!(status & BIT7) || (status & BIT4))
3879 info->icount.rxabort++;
3880 else if (status & BIT6)
3881 info->icount.rxover++;
3882 else if (!(status & BIT5)) {
3883 info->icount.rxcrc++;
3884 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
3885 return_frame = true;
3888 #if SYNCLINK_GENERIC_HDLC
3890 info->netdev->stats.rx_errors++;
3891 info->netdev->stats.rx_frame_errors++;
3895 return_frame = true;
3898 framesize = buf->count;
3900 if (debug_level >= DEBUG_LEVEL_BH)
3901 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3902 __FILE__,__LINE__,info->device_name,status,framesize);
3904 if (debug_level >= DEBUG_LEVEL_DATA)
3905 trace_block(info, buf->data, framesize, 0);
3908 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3909 framesize+1 > info->max_frame_size) ||
3910 framesize > info->max_frame_size)
3911 info->icount.rxlong++;
3914 info->icount.rxok++;
3916 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3917 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3921 #if SYNCLINK_GENERIC_HDLC
3923 hdlcdev_rx(info, buf->data, framesize);
3926 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3930 spin_lock_irqsave(&info->lock,flags);
3931 buf->status = buf->count = 0;
3932 info->rx_frame_count--;
3934 if (info->rx_get >= info->rx_buf_count)
3936 spin_unlock_irqrestore(&info->lock,flags);
3941 static bool register_test(MGSLPC_INFO *info)
3943 static unsigned char patterns[] =
3944 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
3945 static unsigned int count = ARRAY_SIZE(patterns);
3948 unsigned long flags;
3950 spin_lock_irqsave(&info->lock,flags);
3953 for (i = 0; i < count; i++) {
3954 write_reg(info, XAD1, patterns[i]);
3955 write_reg(info, XAD2, patterns[(i + 1) % count]);
3956 if ((read_reg(info, XAD1) != patterns[i]) ||
3957 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
3963 spin_unlock_irqrestore(&info->lock,flags);
3967 static bool irq_test(MGSLPC_INFO *info)
3969 unsigned long end_time;
3970 unsigned long flags;
3972 spin_lock_irqsave(&info->lock,flags);
3975 info->testing_irq = true;
3978 info->irq_occurred = false;
3980 /* init hdlc mode */
3982 irq_enable(info, CHA, IRQ_TIMER);
3983 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
3984 issue_command(info, CHA, CMD_START_TIMER);
3986 spin_unlock_irqrestore(&info->lock,flags);
3989 while(end_time-- && !info->irq_occurred) {
3990 msleep_interruptible(10);
3993 info->testing_irq = false;
3995 spin_lock_irqsave(&info->lock,flags);
3997 spin_unlock_irqrestore(&info->lock,flags);
3999 return info->irq_occurred;
4002 static int adapter_test(MGSLPC_INFO *info)
4004 if (!register_test(info)) {
4005 info->init_error = DiagStatus_AddressFailure;
4006 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
4007 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
4011 if (!irq_test(info)) {
4012 info->init_error = DiagStatus_IrqFailure;
4013 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
4014 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
4018 if (debug_level >= DEBUG_LEVEL_INFO)
4019 printk("%s(%d):device %s passed diagnostics\n",
4020 __FILE__,__LINE__,info->device_name);
4024 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
4029 printk("%s tx data:\n",info->device_name);
4031 printk("%s rx data:\n",info->device_name);
4039 for(i=0;i<linecount;i++)
4040 printk("%02X ",(unsigned char)data[i]);
4043 for(i=0;i<linecount;i++) {
4044 if (data[i]>=040 && data[i]<=0176)
4045 printk("%c",data[i]);
4056 /* HDLC frame time out
4057 * update stats and do tx completion processing
4059 static void tx_timeout(unsigned long context)
4061 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
4062 unsigned long flags;
4064 if ( debug_level >= DEBUG_LEVEL_INFO )
4065 printk( "%s(%d):tx_timeout(%s)\n",
4066 __FILE__,__LINE__,info->device_name);
4067 if(info->tx_active &&
4068 info->params.mode == MGSL_MODE_HDLC) {
4069 info->icount.txtimeout++;
4071 spin_lock_irqsave(&info->lock,flags);
4072 info->tx_active = false;
4073 info->tx_count = info->tx_put = info->tx_get = 0;
4075 spin_unlock_irqrestore(&info->lock,flags);
4077 #if SYNCLINK_GENERIC_HDLC
4079 hdlcdev_tx_done(info);
4085 #if SYNCLINK_GENERIC_HDLC
4088 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
4089 * set encoding and frame check sequence (FCS) options
4091 * dev pointer to network device structure
4092 * encoding serial encoding setting
4093 * parity FCS setting
4095 * returns 0 if success, otherwise error code
4097 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
4098 unsigned short parity)
4100 MGSLPC_INFO *info = dev_to_port(dev);
4101 unsigned char new_encoding;
4102 unsigned short new_crctype;
4104 /* return error if TTY interface open */
4110 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
4111 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
4112 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
4113 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
4114 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
4115 default: return -EINVAL;
4120 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
4121 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
4122 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
4123 default: return -EINVAL;
4126 info->params.encoding = new_encoding;
4127 info->params.crc_type = new_crctype;
4129 /* if network interface up, reprogram hardware */
4131 mgslpc_program_hw(info);
4137 * called by generic HDLC layer to send frame
4139 * skb socket buffer containing HDLC frame
4140 * dev pointer to network device structure
4142 * returns 0 if success, otherwise error code
4144 static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
4146 MGSLPC_INFO *info = dev_to_port(dev);
4147 unsigned long flags;
4149 if (debug_level >= DEBUG_LEVEL_INFO)
4150 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
4152 /* stop sending until this frame completes */
4153 netif_stop_queue(dev);
4155 /* copy data to device buffers */
4156 skb_copy_from_linear_data(skb, info->tx_buf, skb->len);
4158 info->tx_put = info->tx_count = skb->len;
4160 /* update network statistics */
4161 dev->stats.tx_packets++;
4162 dev->stats.tx_bytes += skb->len;
4164 /* done with socket buffer, so free it */
4167 /* save start time for transmit timeout detection */
4168 dev->trans_start = jiffies;
4170 /* start hardware transmitter if necessary */
4171 spin_lock_irqsave(&info->lock,flags);
4172 if (!info->tx_active)
4174 spin_unlock_irqrestore(&info->lock,flags);
4180 * called by network layer when interface enabled
4181 * claim resources and initialize hardware
4183 * dev pointer to network device structure
4185 * returns 0 if success, otherwise error code
4187 static int hdlcdev_open(struct net_device *dev)
4189 MGSLPC_INFO *info = dev_to_port(dev);
4191 unsigned long flags;
4193 if (debug_level >= DEBUG_LEVEL_INFO)
4194 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4196 /* generic HDLC layer open processing */
4197 if ((rc = hdlc_open(dev)))
4200 /* arbitrate between network and tty opens */
4201 spin_lock_irqsave(&info->netlock, flags);
4202 if (info->count != 0 || info->netcount != 0) {
4203 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4204 spin_unlock_irqrestore(&info->netlock, flags);
4208 spin_unlock_irqrestore(&info->netlock, flags);
4210 /* claim resources and init adapter */
4211 if ((rc = startup(info)) != 0) {
4212 spin_lock_irqsave(&info->netlock, flags);
4214 spin_unlock_irqrestore(&info->netlock, flags);
4218 /* assert DTR and RTS, apply hardware settings */
4219 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4220 mgslpc_program_hw(info);
4222 /* enable network layer transmit */
4223 dev->trans_start = jiffies;
4224 netif_start_queue(dev);
4226 /* inform generic HDLC layer of current DCD status */
4227 spin_lock_irqsave(&info->lock, flags);
4229 spin_unlock_irqrestore(&info->lock, flags);
4230 if (info->serial_signals & SerialSignal_DCD)
4231 netif_carrier_on(dev);
4233 netif_carrier_off(dev);
4238 * called by network layer when interface is disabled
4239 * shutdown hardware and release resources
4241 * dev pointer to network device structure
4243 * returns 0 if success, otherwise error code
4245 static int hdlcdev_close(struct net_device *dev)
4247 MGSLPC_INFO *info = dev_to_port(dev);
4248 unsigned long flags;
4250 if (debug_level >= DEBUG_LEVEL_INFO)
4251 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4253 netif_stop_queue(dev);
4255 /* shutdown adapter and release resources */
4260 spin_lock_irqsave(&info->netlock, flags);
4262 spin_unlock_irqrestore(&info->netlock, flags);
4268 * called by network layer to process IOCTL call to network device
4270 * dev pointer to network device structure
4271 * ifr pointer to network interface request structure
4272 * cmd IOCTL command code
4274 * returns 0 if success, otherwise error code
4276 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4278 const size_t size = sizeof(sync_serial_settings);
4279 sync_serial_settings new_line;
4280 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4281 MGSLPC_INFO *info = dev_to_port(dev);
4284 if (debug_level >= DEBUG_LEVEL_INFO)
4285 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4287 /* return error if TTY interface open */
4291 if (cmd != SIOCWANDEV)
4292 return hdlc_ioctl(dev, ifr, cmd);
4294 switch(ifr->ifr_settings.type) {
4295 case IF_GET_IFACE: /* return current sync_serial_settings */
4297 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4298 if (ifr->ifr_settings.size < size) {
4299 ifr->ifr_settings.size = size; /* data size wanted */
4303 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4304 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4305 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4306 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4309 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4310 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4311 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4312 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4313 default: new_line.clock_type = CLOCK_DEFAULT;
4316 new_line.clock_rate = info->params.clock_speed;
4317 new_line.loopback = info->params.loopback ? 1:0;
4319 if (copy_to_user(line, &new_line, size))
4323 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4325 if(!capable(CAP_NET_ADMIN))
4327 if (copy_from_user(&new_line, line, size))
4330 switch (new_line.clock_type)
4332 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4333 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4334 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4335 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4336 case CLOCK_DEFAULT: flags = info->params.flags &
4337 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4338 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4339 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4340 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4341 default: return -EINVAL;
4344 if (new_line.loopback != 0 && new_line.loopback != 1)
4347 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4348 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4349 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4350 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4351 info->params.flags |= flags;
4353 info->params.loopback = new_line.loopback;
4355 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4356 info->params.clock_speed = new_line.clock_rate;
4358 info->params.clock_speed = 0;
4360 /* if network interface up, reprogram hardware */
4362 mgslpc_program_hw(info);
4366 return hdlc_ioctl(dev, ifr, cmd);
4371 * called by network layer when transmit timeout is detected
4373 * dev pointer to network device structure
4375 static void hdlcdev_tx_timeout(struct net_device *dev)
4377 MGSLPC_INFO *info = dev_to_port(dev);
4378 unsigned long flags;
4380 if (debug_level >= DEBUG_LEVEL_INFO)
4381 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4383 dev->stats.tx_errors++;
4384 dev->stats.tx_aborted_errors++;
4386 spin_lock_irqsave(&info->lock,flags);
4388 spin_unlock_irqrestore(&info->lock,flags);
4390 netif_wake_queue(dev);
4394 * called by device driver when transmit completes
4395 * reenable network layer transmit if stopped
4397 * info pointer to device instance information
4399 static void hdlcdev_tx_done(MGSLPC_INFO *info)
4401 if (netif_queue_stopped(info->netdev))
4402 netif_wake_queue(info->netdev);
4406 * called by device driver when frame received
4407 * pass frame to network layer
4409 * info pointer to device instance information
4410 * buf pointer to buffer contianing frame data
4411 * size count of data bytes in buf
4413 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4415 struct sk_buff *skb = dev_alloc_skb(size);
4416 struct net_device *dev = info->netdev;
4418 if (debug_level >= DEBUG_LEVEL_INFO)
4419 printk("hdlcdev_rx(%s)\n",dev->name);
4422 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
4423 dev->stats.rx_dropped++;
4427 memcpy(skb_put(skb, size), buf, size);
4429 skb->protocol = hdlc_type_trans(skb, dev);
4431 dev->stats.rx_packets++;
4432 dev->stats.rx_bytes += size;
4436 dev->last_rx = jiffies;
4440 * called by device driver when adding device instance
4441 * do generic HDLC initialization
4443 * info pointer to device instance information
4445 * returns 0 if success, otherwise error code
4447 static int hdlcdev_init(MGSLPC_INFO *info)
4450 struct net_device *dev;
4453 /* allocate and initialize network and HDLC layer objects */
4455 if (!(dev = alloc_hdlcdev(info))) {
4456 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4460 /* for network layer reporting purposes only */
4461 dev->base_addr = info->io_base;
4462 dev->irq = info->irq_level;
4464 /* network layer callbacks and settings */
4465 dev->do_ioctl = hdlcdev_ioctl;
4466 dev->open = hdlcdev_open;
4467 dev->stop = hdlcdev_close;
4468 dev->tx_timeout = hdlcdev_tx_timeout;
4469 dev->watchdog_timeo = 10*HZ;
4470 dev->tx_queue_len = 50;
4472 /* generic HDLC layer callbacks and settings */
4473 hdlc = dev_to_hdlc(dev);
4474 hdlc->attach = hdlcdev_attach;
4475 hdlc->xmit = hdlcdev_xmit;
4477 /* register objects with HDLC layer */
4478 if ((rc = register_hdlc_device(dev))) {
4479 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4489 * called by device driver when removing device instance
4490 * do generic HDLC cleanup
4492 * info pointer to device instance information
4494 static void hdlcdev_exit(MGSLPC_INFO *info)
4496 unregister_hdlc_device(info->netdev);
4497 free_netdev(info->netdev);
4498 info->netdev = NULL;
4501 #endif /* CONFIG_HDLC */