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 */
237 #if SYNCLINK_GENERIC_HDLC
238 struct net_device *netdev;
243 #define MGSLPC_MAGIC 0x5402
246 * The size of the serial xmit buffer is 1 page, or 4096 bytes
248 #define TXBUFSIZE 4096
251 #define CHA 0x00 /* channel A offset */
252 #define CHB 0x40 /* channel B offset */
255 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
302 #define IRQ_BREAK_ON BIT15 // rx break detected
303 #define IRQ_DATAOVERRUN BIT14 // receive data overflow
304 #define IRQ_ALLSENT BIT13 // all sent
305 #define IRQ_UNDERRUN BIT12 // transmit data underrun
306 #define IRQ_TIMER BIT11 // timer interrupt
307 #define IRQ_CTS BIT10 // CTS status change
308 #define IRQ_TXREPEAT BIT9 // tx message repeat
309 #define IRQ_TXFIFO BIT8 // transmit pool ready
310 #define IRQ_RXEOM BIT7 // receive message end
311 #define IRQ_EXITHUNT BIT6 // receive frame start
312 #define IRQ_RXTIME BIT6 // rx char timeout
313 #define IRQ_DCD BIT2 // carrier detect status change
314 #define IRQ_OVERRUN BIT1 // receive frame overflow
315 #define IRQ_RXFIFO BIT0 // receive pool full
319 #define XFW BIT6 // transmit FIFO write enable
320 #define CEC BIT2 // command executing
321 #define CTS BIT1 // CTS state
326 #define PVR_AUTOCTS BIT3
327 #define PVR_RS232 0x20 /* 0010b */
328 #define PVR_V35 0xe0 /* 1110b */
329 #define PVR_RS422 0x40 /* 0100b */
331 /* Register access functions */
333 #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
334 #define read_reg(info, reg) inb((info)->io_base + (reg))
336 #define read_reg16(info, reg) inw((info)->io_base + (reg))
337 #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
339 #define set_reg_bits(info, reg, mask) \
340 write_reg(info, (reg), \
341 (unsigned char) (read_reg(info, (reg)) | (mask)))
342 #define clear_reg_bits(info, reg, mask) \
343 write_reg(info, (reg), \
344 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
346 * interrupt enable/disable routines
348 static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
350 if (channel == CHA) {
351 info->imra_value |= mask;
352 write_reg16(info, CHA + IMR, info->imra_value);
354 info->imrb_value |= mask;
355 write_reg16(info, CHB + IMR, info->imrb_value);
358 static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
360 if (channel == CHA) {
361 info->imra_value &= ~mask;
362 write_reg16(info, CHA + IMR, info->imra_value);
364 info->imrb_value &= ~mask;
365 write_reg16(info, CHB + IMR, info->imrb_value);
369 #define port_irq_disable(info, mask) \
370 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
372 #define port_irq_enable(info, mask) \
373 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
375 static void rx_start(MGSLPC_INFO *info);
376 static void rx_stop(MGSLPC_INFO *info);
378 static void tx_start(MGSLPC_INFO *info);
379 static void tx_stop(MGSLPC_INFO *info);
380 static void tx_set_idle(MGSLPC_INFO *info);
382 static void get_signals(MGSLPC_INFO *info);
383 static void set_signals(MGSLPC_INFO *info);
385 static void reset_device(MGSLPC_INFO *info);
387 static void hdlc_mode(MGSLPC_INFO *info);
388 static void async_mode(MGSLPC_INFO *info);
390 static void tx_timeout(unsigned long context);
392 static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg);
394 #if SYNCLINK_GENERIC_HDLC
395 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
396 static void hdlcdev_tx_done(MGSLPC_INFO *info);
397 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
398 static int hdlcdev_init(MGSLPC_INFO *info);
399 static void hdlcdev_exit(MGSLPC_INFO *info);
402 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
404 static bool register_test(MGSLPC_INFO *info);
405 static bool irq_test(MGSLPC_INFO *info);
406 static int adapter_test(MGSLPC_INFO *info);
408 static int claim_resources(MGSLPC_INFO *info);
409 static void release_resources(MGSLPC_INFO *info);
410 static void mgslpc_add_device(MGSLPC_INFO *info);
411 static void mgslpc_remove_device(MGSLPC_INFO *info);
413 static bool rx_get_frame(MGSLPC_INFO *info);
414 static void rx_reset_buffers(MGSLPC_INFO *info);
415 static int rx_alloc_buffers(MGSLPC_INFO *info);
416 static void rx_free_buffers(MGSLPC_INFO *info);
418 static irqreturn_t mgslpc_isr(int irq, void *dev_id);
421 * Bottom half interrupt handlers
423 static void bh_handler(struct work_struct *work);
424 static void bh_transmit(MGSLPC_INFO *info);
425 static void bh_status(MGSLPC_INFO *info);
430 static int tiocmget(struct tty_struct *tty, struct file *file);
431 static int tiocmset(struct tty_struct *tty, struct file *file,
432 unsigned int set, unsigned int clear);
433 static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
434 static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
435 static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params);
436 static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
437 static int set_txidle(MGSLPC_INFO *info, int idle_mode);
438 static int set_txenable(MGSLPC_INFO *info, int enable);
439 static int tx_abort(MGSLPC_INFO *info);
440 static int set_rxenable(MGSLPC_INFO *info, int enable);
441 static int wait_events(MGSLPC_INFO *info, int __user *mask);
443 static MGSLPC_INFO *mgslpc_device_list = NULL;
444 static int mgslpc_device_count = 0;
447 * Set this param to non-zero to load eax with the
448 * .text section address and breakpoint on module load.
449 * This is useful for use with gdb and add-symbol-file command.
451 static int break_on_load=0;
454 * Driver major number, defaults to zero to get auto
455 * assigned major number. May be forced as module parameter.
457 static int ttymajor=0;
459 static int debug_level = 0;
460 static int maxframe[MAX_DEVICE_COUNT] = {0,};
462 module_param(break_on_load, bool, 0);
463 module_param(ttymajor, int, 0);
464 module_param(debug_level, int, 0);
465 module_param_array(maxframe, int, NULL, 0);
467 MODULE_LICENSE("GPL");
469 static char *driver_name = "SyncLink PC Card driver";
470 static char *driver_version = "$Revision: 4.34 $";
472 static struct tty_driver *serial_driver;
474 /* number of characters left in xmit buffer before we ask for more */
475 #define WAKEUP_CHARS 256
477 static void mgslpc_change_params(MGSLPC_INFO *info);
478 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
480 /* PCMCIA prototypes */
482 static int mgslpc_config(struct pcmcia_device *link);
483 static void mgslpc_release(u_long arg);
484 static void mgslpc_detach(struct pcmcia_device *p_dev);
487 * 1st function defined in .text section. Calling this function in
488 * init_module() followed by a breakpoint allows a remote debugger
489 * (gdb) to get the .text address for the add-symbol-file command.
490 * This allows remote debugging of dynamically loadable modules.
492 static void* mgslpc_get_text_ptr(void)
494 return mgslpc_get_text_ptr;
498 * line discipline callback wrappers
500 * The wrappers maintain line discipline references
501 * while calling into the line discipline.
503 * ldisc_receive_buf - pass receive data to line discipline
506 static void ldisc_receive_buf(struct tty_struct *tty,
507 const __u8 *data, char *flags, int count)
509 struct tty_ldisc *ld;
512 ld = tty_ldisc_ref(tty);
514 if (ld->ops->receive_buf)
515 ld->ops->receive_buf(tty, data, flags, count);
520 static int mgslpc_probe(struct pcmcia_device *link)
525 if (debug_level >= DEBUG_LEVEL_INFO)
526 printk("mgslpc_attach\n");
528 info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
530 printk("Error can't allocate device instance data\n");
534 info->magic = MGSLPC_MAGIC;
535 INIT_WORK(&info->task, bh_handler);
536 info->max_frame_size = 4096;
537 info->close_delay = 5*HZ/10;
538 info->closing_wait = 30*HZ;
539 init_waitqueue_head(&info->open_wait);
540 init_waitqueue_head(&info->close_wait);
541 init_waitqueue_head(&info->status_event_wait_q);
542 init_waitqueue_head(&info->event_wait_q);
543 spin_lock_init(&info->lock);
544 spin_lock_init(&info->netlock);
545 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
546 info->idle_mode = HDLC_TXIDLE_FLAGS;
547 info->imra_value = 0xffff;
548 info->imrb_value = 0xffff;
549 info->pim_value = 0xff;
554 /* Initialize the struct pcmcia_device structure */
556 /* Interrupt setup */
557 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
558 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
559 link->irq.Handler = NULL;
561 link->conf.Attributes = 0;
562 link->conf.IntType = INT_MEMORY_AND_IO;
564 ret = mgslpc_config(link);
568 mgslpc_add_device(info);
573 /* Card has been inserted.
576 #define CS_CHECK(fn, ret) \
577 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
579 static int mgslpc_config(struct pcmcia_device *link)
581 MGSLPC_INFO *info = link->priv;
584 int last_fn, last_ret;
586 cistpl_cftable_entry_t dflt = { 0 };
587 cistpl_cftable_entry_t *cfg;
589 if (debug_level >= DEBUG_LEVEL_INFO)
590 printk("mgslpc_config(0x%p)\n", link);
592 tuple.Attributes = 0;
593 tuple.TupleData = buf;
594 tuple.TupleDataMax = sizeof(buf);
595 tuple.TupleOffset = 0;
597 /* get CIS configuration entry */
599 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
600 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
602 cfg = &(parse.cftable_entry);
603 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
604 CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
606 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
610 link->conf.ConfigIndex = cfg->index;
611 link->conf.Attributes |= CONF_ENABLE_IRQ;
613 /* IO window settings */
614 link->io.NumPorts1 = 0;
615 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
616 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
617 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
618 if (!(io->flags & CISTPL_IO_8BIT))
619 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
620 if (!(io->flags & CISTPL_IO_16BIT))
621 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
622 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
623 link->io.BasePort1 = io->win[0].base;
624 link->io.NumPorts1 = io->win[0].len;
625 CS_CHECK(RequestIO, pcmcia_request_io(link, &link->io));
628 link->conf.Attributes = CONF_ENABLE_IRQ;
629 link->conf.IntType = INT_MEMORY_AND_IO;
630 link->conf.ConfigIndex = 8;
631 link->conf.Present = PRESENT_OPTION;
633 link->irq.Attributes |= IRQ_HANDLE_PRESENT;
634 link->irq.Handler = mgslpc_isr;
635 link->irq.Instance = info;
636 CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
638 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
640 info->io_base = link->io.BasePort1;
641 info->irq_level = link->irq.AssignedIRQ;
643 /* add to linked list of devices */
644 sprintf(info->node.dev_name, "mgslpc0");
645 info->node.major = info->node.minor = 0;
646 link->dev_node = &info->node;
648 printk(KERN_INFO "%s: index 0x%02x:",
649 info->node.dev_name, link->conf.ConfigIndex);
650 if (link->conf.Attributes & CONF_ENABLE_IRQ)
651 printk(", irq %d", link->irq.AssignedIRQ);
652 if (link->io.NumPorts1)
653 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
654 link->io.BasePort1+link->io.NumPorts1-1);
659 cs_error(link, last_fn, last_ret);
660 mgslpc_release((u_long)link);
664 /* Card has been removed.
665 * Unregister device and release PCMCIA configuration.
666 * If device is open, postpone until it is closed.
668 static void mgslpc_release(u_long arg)
670 struct pcmcia_device *link = (struct pcmcia_device *)arg;
672 if (debug_level >= DEBUG_LEVEL_INFO)
673 printk("mgslpc_release(0x%p)\n", link);
675 pcmcia_disable_device(link);
678 static void mgslpc_detach(struct pcmcia_device *link)
680 if (debug_level >= DEBUG_LEVEL_INFO)
681 printk("mgslpc_detach(0x%p)\n", link);
683 ((MGSLPC_INFO *)link->priv)->stop = 1;
684 mgslpc_release((u_long)link);
686 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
689 static int mgslpc_suspend(struct pcmcia_device *link)
691 MGSLPC_INFO *info = link->priv;
698 static int mgslpc_resume(struct pcmcia_device *link)
700 MGSLPC_INFO *info = link->priv;
708 static inline bool mgslpc_paranoia_check(MGSLPC_INFO *info,
709 char *name, const char *routine)
711 #ifdef MGSLPC_PARANOIA_CHECK
712 static const char *badmagic =
713 "Warning: bad magic number for mgsl struct (%s) in %s\n";
714 static const char *badinfo =
715 "Warning: null mgslpc_info for (%s) in %s\n";
718 printk(badinfo, name, routine);
721 if (info->magic != MGSLPC_MAGIC) {
722 printk(badmagic, name, routine);
733 #define CMD_RXFIFO BIT7 // release current rx FIFO
734 #define CMD_RXRESET BIT6 // receiver reset
735 #define CMD_RXFIFO_READ BIT5
736 #define CMD_START_TIMER BIT4
737 #define CMD_TXFIFO BIT3 // release current tx FIFO
738 #define CMD_TXEOM BIT1 // transmit end message
739 #define CMD_TXRESET BIT0 // transmit reset
741 static bool wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
744 /* wait for command completion */
745 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
753 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
755 wait_command_complete(info, channel);
756 write_reg(info, (unsigned char) (channel + CMDR), cmd);
759 static void tx_pause(struct tty_struct *tty)
761 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
764 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
766 if (debug_level >= DEBUG_LEVEL_INFO)
767 printk("tx_pause(%s)\n",info->device_name);
769 spin_lock_irqsave(&info->lock,flags);
770 if (info->tx_enabled)
772 spin_unlock_irqrestore(&info->lock,flags);
775 static void tx_release(struct tty_struct *tty)
777 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
780 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
782 if (debug_level >= DEBUG_LEVEL_INFO)
783 printk("tx_release(%s)\n",info->device_name);
785 spin_lock_irqsave(&info->lock,flags);
786 if (!info->tx_enabled)
788 spin_unlock_irqrestore(&info->lock,flags);
791 /* Return next bottom half action to perform.
792 * or 0 if nothing to do.
794 static int bh_action(MGSLPC_INFO *info)
799 spin_lock_irqsave(&info->lock,flags);
801 if (info->pending_bh & BH_RECEIVE) {
802 info->pending_bh &= ~BH_RECEIVE;
804 } else if (info->pending_bh & BH_TRANSMIT) {
805 info->pending_bh &= ~BH_TRANSMIT;
807 } else if (info->pending_bh & BH_STATUS) {
808 info->pending_bh &= ~BH_STATUS;
813 /* Mark BH routine as complete */
814 info->bh_running = false;
815 info->bh_requested = false;
818 spin_unlock_irqrestore(&info->lock,flags);
823 static void bh_handler(struct work_struct *work)
825 MGSLPC_INFO *info = container_of(work, MGSLPC_INFO, task);
831 if (debug_level >= DEBUG_LEVEL_BH)
832 printk( "%s(%d):bh_handler(%s) entry\n",
833 __FILE__,__LINE__,info->device_name);
835 info->bh_running = true;
837 while((action = bh_action(info)) != 0) {
839 /* Process work item */
840 if ( debug_level >= DEBUG_LEVEL_BH )
841 printk( "%s(%d):bh_handler() work item action=%d\n",
842 __FILE__,__LINE__,action);
847 while(rx_get_frame(info));
856 /* unknown work item ID */
857 printk("Unknown work item ID=%08X!\n", action);
862 if (debug_level >= DEBUG_LEVEL_BH)
863 printk( "%s(%d):bh_handler(%s) exit\n",
864 __FILE__,__LINE__,info->device_name);
867 static void bh_transmit(MGSLPC_INFO *info)
869 struct tty_struct *tty = info->tty;
870 if (debug_level >= DEBUG_LEVEL_BH)
871 printk("bh_transmit() entry on %s\n", info->device_name);
877 static void bh_status(MGSLPC_INFO *info)
879 info->ri_chkcount = 0;
880 info->dsr_chkcount = 0;
881 info->dcd_chkcount = 0;
882 info->cts_chkcount = 0;
885 /* eom: non-zero = end of frame */
886 static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
888 unsigned char data[2];
889 unsigned char fifo_count, read_count, i;
890 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
892 if (debug_level >= DEBUG_LEVEL_ISR)
893 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
895 if (!info->rx_enabled)
898 if (info->rx_frame_count >= info->rx_buf_count) {
899 /* no more free buffers */
900 issue_command(info, CHA, CMD_RXRESET);
901 info->pending_bh |= BH_RECEIVE;
902 info->rx_overflow = true;
903 info->icount.buf_overrun++;
908 /* end of frame, get FIFO count from RBCL register */
909 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
915 if (fifo_count == 1) {
917 data[0] = read_reg(info, CHA + RXFIFO);
920 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
922 fifo_count -= read_count;
923 if (!fifo_count && eom)
924 buf->status = data[--read_count];
926 for (i = 0; i < read_count; i++) {
927 if (buf->count >= info->max_frame_size) {
928 /* frame too large, reset receiver and reset current buffer */
929 issue_command(info, CHA, CMD_RXRESET);
933 *(buf->data + buf->count) = data[i];
936 } while (fifo_count);
939 info->pending_bh |= BH_RECEIVE;
940 info->rx_frame_count++;
942 if (info->rx_put >= info->rx_buf_count)
945 issue_command(info, CHA, CMD_RXFIFO);
948 static void rx_ready_async(MGSLPC_INFO *info, int tcd)
950 unsigned char data, status, flag;
953 struct tty_struct *tty = info->tty;
954 struct mgsl_icount *icount = &info->icount;
957 /* early termination, get FIFO count from RBCL register */
958 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
960 /* Zero fifo count could mean 0 or 32 bytes available.
961 * If BIT5 of STAR is set then at least 1 byte is available.
963 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
968 tty_buffer_request_room(tty, fifo_count);
969 /* Flush received async data to receive data buffer. */
971 data = read_reg(info, CHA + RXFIFO);
972 status = read_reg(info, CHA + RXFIFO);
978 // if no frameing/crc error then save data
980 // BIT6:framing error
982 if (status & (BIT7 + BIT6)) {
988 /* discard char if tty control flags say so */
989 if (status & info->ignore_status_mask)
992 status &= info->read_status_mask;
996 else if (status & BIT6)
999 work += tty_insert_flip_char(tty, data, flag);
1001 issue_command(info, CHA, CMD_RXFIFO);
1003 if (debug_level >= DEBUG_LEVEL_ISR) {
1004 printk("%s(%d):rx_ready_async",
1006 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1007 __FILE__,__LINE__,icount->rx,icount->brk,
1008 icount->parity,icount->frame,icount->overrun);
1012 tty_flip_buffer_push(tty);
1016 static void tx_done(MGSLPC_INFO *info)
1018 if (!info->tx_active)
1021 info->tx_active = false;
1022 info->tx_aborting = false;
1024 if (info->params.mode == MGSL_MODE_ASYNC)
1027 info->tx_count = info->tx_put = info->tx_get = 0;
1028 del_timer(&info->tx_timer);
1030 if (info->drop_rts_on_tx_done) {
1032 if (info->serial_signals & SerialSignal_RTS) {
1033 info->serial_signals &= ~SerialSignal_RTS;
1036 info->drop_rts_on_tx_done = false;
1039 #if SYNCLINK_GENERIC_HDLC
1041 hdlcdev_tx_done(info);
1045 if (info->tty->stopped || info->tty->hw_stopped) {
1049 info->pending_bh |= BH_TRANSMIT;
1053 static void tx_ready(MGSLPC_INFO *info)
1055 unsigned char fifo_count = 32;
1058 if (debug_level >= DEBUG_LEVEL_ISR)
1059 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1061 if (info->params.mode == MGSL_MODE_HDLC) {
1062 if (!info->tx_active)
1065 if (info->tty->stopped || info->tty->hw_stopped) {
1069 if (!info->tx_count)
1070 info->tx_active = false;
1073 if (!info->tx_count)
1076 while (info->tx_count && fifo_count) {
1077 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
1080 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1082 write_reg16(info, CHA + TXFIFO,
1083 *((unsigned short*)(info->tx_buf + info->tx_get)));
1085 info->tx_count -= c;
1086 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1090 if (info->params.mode == MGSL_MODE_ASYNC) {
1091 if (info->tx_count < WAKEUP_CHARS)
1092 info->pending_bh |= BH_TRANSMIT;
1093 issue_command(info, CHA, CMD_TXFIFO);
1096 issue_command(info, CHA, CMD_TXFIFO);
1098 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1102 static void cts_change(MGSLPC_INFO *info)
1105 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1106 irq_disable(info, CHB, IRQ_CTS);
1108 if (info->serial_signals & SerialSignal_CTS)
1109 info->input_signal_events.cts_up++;
1111 info->input_signal_events.cts_down++;
1112 wake_up_interruptible(&info->status_event_wait_q);
1113 wake_up_interruptible(&info->event_wait_q);
1115 if (info->flags & ASYNC_CTS_FLOW) {
1116 if (info->tty->hw_stopped) {
1117 if (info->serial_signals & SerialSignal_CTS) {
1118 if (debug_level >= DEBUG_LEVEL_ISR)
1119 printk("CTS tx start...");
1121 info->tty->hw_stopped = 0;
1123 info->pending_bh |= BH_TRANSMIT;
1127 if (!(info->serial_signals & SerialSignal_CTS)) {
1128 if (debug_level >= DEBUG_LEVEL_ISR)
1129 printk("CTS tx stop...");
1131 info->tty->hw_stopped = 1;
1136 info->pending_bh |= BH_STATUS;
1139 static void dcd_change(MGSLPC_INFO *info)
1142 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1143 irq_disable(info, CHB, IRQ_DCD);
1145 if (info->serial_signals & SerialSignal_DCD) {
1146 info->input_signal_events.dcd_up++;
1149 info->input_signal_events.dcd_down++;
1150 #if SYNCLINK_GENERIC_HDLC
1151 if (info->netcount) {
1152 if (info->serial_signals & SerialSignal_DCD)
1153 netif_carrier_on(info->netdev);
1155 netif_carrier_off(info->netdev);
1158 wake_up_interruptible(&info->status_event_wait_q);
1159 wake_up_interruptible(&info->event_wait_q);
1161 if (info->flags & ASYNC_CHECK_CD) {
1162 if (debug_level >= DEBUG_LEVEL_ISR)
1163 printk("%s CD now %s...", info->device_name,
1164 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1165 if (info->serial_signals & SerialSignal_DCD)
1166 wake_up_interruptible(&info->open_wait);
1168 if (debug_level >= DEBUG_LEVEL_ISR)
1169 printk("doing serial hangup...");
1171 tty_hangup(info->tty);
1174 info->pending_bh |= BH_STATUS;
1177 static void dsr_change(MGSLPC_INFO *info)
1180 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1181 port_irq_disable(info, PVR_DSR);
1183 if (info->serial_signals & SerialSignal_DSR)
1184 info->input_signal_events.dsr_up++;
1186 info->input_signal_events.dsr_down++;
1187 wake_up_interruptible(&info->status_event_wait_q);
1188 wake_up_interruptible(&info->event_wait_q);
1189 info->pending_bh |= BH_STATUS;
1192 static void ri_change(MGSLPC_INFO *info)
1195 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1196 port_irq_disable(info, PVR_RI);
1198 if (info->serial_signals & SerialSignal_RI)
1199 info->input_signal_events.ri_up++;
1201 info->input_signal_events.ri_down++;
1202 wake_up_interruptible(&info->status_event_wait_q);
1203 wake_up_interruptible(&info->event_wait_q);
1204 info->pending_bh |= BH_STATUS;
1207 /* Interrupt service routine entry point.
1211 * irq interrupt number that caused interrupt
1212 * dev_id device ID supplied during interrupt registration
1214 static irqreturn_t mgslpc_isr(int dummy, void *dev_id)
1216 MGSLPC_INFO *info = dev_id;
1218 unsigned char gis, pis;
1221 if (debug_level >= DEBUG_LEVEL_ISR)
1222 printk("mgslpc_isr(%d) entry.\n", info->irq_level);
1224 if (!(info->p_dev->_locked))
1227 spin_lock(&info->lock);
1229 while ((gis = read_reg(info, CHA + GIS))) {
1230 if (debug_level >= DEBUG_LEVEL_ISR)
1231 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1233 if ((gis & 0x70) || count > 1000) {
1234 printk("synclink_cs:hardware failed or ejected\n");
1239 if (gis & (BIT1 + BIT0)) {
1240 isr = read_reg16(info, CHB + ISR);
1246 if (gis & (BIT3 + BIT2))
1248 isr = read_reg16(info, CHA + ISR);
1249 if (isr & IRQ_TIMER) {
1250 info->irq_occurred = true;
1251 irq_disable(info, CHA, IRQ_TIMER);
1255 if (isr & IRQ_EXITHUNT) {
1256 info->icount.exithunt++;
1257 wake_up_interruptible(&info->event_wait_q);
1259 if (isr & IRQ_BREAK_ON) {
1261 if (info->flags & ASYNC_SAK)
1264 if (isr & IRQ_RXTIME) {
1265 issue_command(info, CHA, CMD_RXFIFO_READ);
1267 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1268 if (info->params.mode == MGSL_MODE_HDLC)
1269 rx_ready_hdlc(info, isr & IRQ_RXEOM);
1271 rx_ready_async(info, isr & IRQ_RXEOM);
1275 if (isr & IRQ_UNDERRUN) {
1276 if (info->tx_aborting)
1277 info->icount.txabort++;
1279 info->icount.txunder++;
1282 else if (isr & IRQ_ALLSENT) {
1283 info->icount.txok++;
1286 else if (isr & IRQ_TXFIFO)
1290 pis = read_reg(info, CHA + PIS);
1298 /* Request bottom half processing if there's something
1299 * for it to do and the bh is not already running
1302 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1303 if ( debug_level >= DEBUG_LEVEL_ISR )
1304 printk("%s(%d):%s queueing bh task.\n",
1305 __FILE__,__LINE__,info->device_name);
1306 schedule_work(&info->task);
1307 info->bh_requested = true;
1310 spin_unlock(&info->lock);
1312 if (debug_level >= DEBUG_LEVEL_ISR)
1313 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1314 __FILE__, __LINE__, info->irq_level);
1319 /* Initialize and start device.
1321 static int startup(MGSLPC_INFO * info)
1325 if (debug_level >= DEBUG_LEVEL_INFO)
1326 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1328 if (info->flags & ASYNC_INITIALIZED)
1331 if (!info->tx_buf) {
1332 /* allocate a page of memory for a transmit buffer */
1333 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1334 if (!info->tx_buf) {
1335 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1336 __FILE__,__LINE__,info->device_name);
1341 info->pending_bh = 0;
1343 memset(&info->icount, 0, sizeof(info->icount));
1345 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
1347 /* Allocate and claim adapter resources */
1348 retval = claim_resources(info);
1350 /* perform existance check and diagnostics */
1352 retval = adapter_test(info);
1355 if (capable(CAP_SYS_ADMIN) && info->tty)
1356 set_bit(TTY_IO_ERROR, &info->tty->flags);
1357 release_resources(info);
1361 /* program hardware for current parameters */
1362 mgslpc_change_params(info);
1365 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1367 info->flags |= ASYNC_INITIALIZED;
1372 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1374 static void shutdown(MGSLPC_INFO * info)
1376 unsigned long flags;
1378 if (!(info->flags & ASYNC_INITIALIZED))
1381 if (debug_level >= DEBUG_LEVEL_INFO)
1382 printk("%s(%d):mgslpc_shutdown(%s)\n",
1383 __FILE__,__LINE__, info->device_name );
1385 /* clear status wait queue because status changes */
1386 /* can't happen after shutting down the hardware */
1387 wake_up_interruptible(&info->status_event_wait_q);
1388 wake_up_interruptible(&info->event_wait_q);
1390 del_timer_sync(&info->tx_timer);
1393 free_page((unsigned long) info->tx_buf);
1394 info->tx_buf = NULL;
1397 spin_lock_irqsave(&info->lock,flags);
1402 /* TODO:disable interrupts instead of reset to preserve signal states */
1405 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1406 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1410 spin_unlock_irqrestore(&info->lock,flags);
1412 release_resources(info);
1415 set_bit(TTY_IO_ERROR, &info->tty->flags);
1417 info->flags &= ~ASYNC_INITIALIZED;
1420 static void mgslpc_program_hw(MGSLPC_INFO *info)
1422 unsigned long flags;
1424 spin_lock_irqsave(&info->lock,flags);
1428 info->tx_count = info->tx_put = info->tx_get = 0;
1430 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1437 info->dcd_chkcount = 0;
1438 info->cts_chkcount = 0;
1439 info->ri_chkcount = 0;
1440 info->dsr_chkcount = 0;
1442 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1443 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1446 if (info->netcount || info->tty->termios->c_cflag & CREAD)
1449 spin_unlock_irqrestore(&info->lock,flags);
1452 /* Reconfigure adapter based on new parameters
1454 static void mgslpc_change_params(MGSLPC_INFO *info)
1459 if (!info->tty || !info->tty->termios)
1462 if (debug_level >= DEBUG_LEVEL_INFO)
1463 printk("%s(%d):mgslpc_change_params(%s)\n",
1464 __FILE__,__LINE__, info->device_name );
1466 cflag = info->tty->termios->c_cflag;
1468 /* if B0 rate (hangup) specified then negate DTR and RTS */
1469 /* otherwise assert DTR and RTS */
1471 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1473 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1475 /* byte size and parity */
1477 switch (cflag & CSIZE) {
1478 case CS5: info->params.data_bits = 5; break;
1479 case CS6: info->params.data_bits = 6; break;
1480 case CS7: info->params.data_bits = 7; break;
1481 case CS8: info->params.data_bits = 8; break;
1482 default: info->params.data_bits = 7; break;
1486 info->params.stop_bits = 2;
1488 info->params.stop_bits = 1;
1490 info->params.parity = ASYNC_PARITY_NONE;
1491 if (cflag & PARENB) {
1493 info->params.parity = ASYNC_PARITY_ODD;
1495 info->params.parity = ASYNC_PARITY_EVEN;
1498 info->params.parity = ASYNC_PARITY_SPACE;
1502 /* calculate number of jiffies to transmit a full
1503 * FIFO (32 bytes) at specified data rate
1505 bits_per_char = info->params.data_bits +
1506 info->params.stop_bits + 1;
1508 /* if port data rate is set to 460800 or less then
1509 * allow tty settings to override, otherwise keep the
1510 * current data rate.
1512 if (info->params.data_rate <= 460800) {
1513 info->params.data_rate = tty_get_baud_rate(info->tty);
1516 if ( info->params.data_rate ) {
1517 info->timeout = (32*HZ*bits_per_char) /
1518 info->params.data_rate;
1520 info->timeout += HZ/50; /* Add .02 seconds of slop */
1522 if (cflag & CRTSCTS)
1523 info->flags |= ASYNC_CTS_FLOW;
1525 info->flags &= ~ASYNC_CTS_FLOW;
1528 info->flags &= ~ASYNC_CHECK_CD;
1530 info->flags |= ASYNC_CHECK_CD;
1532 /* process tty input control flags */
1534 info->read_status_mask = 0;
1535 if (I_INPCK(info->tty))
1536 info->read_status_mask |= BIT7 | BIT6;
1537 if (I_IGNPAR(info->tty))
1538 info->ignore_status_mask |= BIT7 | BIT6;
1540 mgslpc_program_hw(info);
1543 /* Add a character to the transmit buffer
1545 static int mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1547 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1548 unsigned long flags;
1550 if (debug_level >= DEBUG_LEVEL_INFO) {
1551 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1552 __FILE__,__LINE__,ch,info->device_name);
1555 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1561 spin_lock_irqsave(&info->lock,flags);
1563 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1564 if (info->tx_count < TXBUFSIZE - 1) {
1565 info->tx_buf[info->tx_put++] = ch;
1566 info->tx_put &= TXBUFSIZE-1;
1571 spin_unlock_irqrestore(&info->lock,flags);
1575 /* Enable transmitter so remaining characters in the
1576 * transmit buffer are sent.
1578 static void mgslpc_flush_chars(struct tty_struct *tty)
1580 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1581 unsigned long flags;
1583 if (debug_level >= DEBUG_LEVEL_INFO)
1584 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1585 __FILE__,__LINE__,info->device_name,info->tx_count);
1587 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1590 if (info->tx_count <= 0 || tty->stopped ||
1591 tty->hw_stopped || !info->tx_buf)
1594 if (debug_level >= DEBUG_LEVEL_INFO)
1595 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1596 __FILE__,__LINE__,info->device_name);
1598 spin_lock_irqsave(&info->lock,flags);
1599 if (!info->tx_active)
1601 spin_unlock_irqrestore(&info->lock,flags);
1604 /* Send a block of data
1608 * tty pointer to tty information structure
1609 * buf pointer to buffer containing send data
1610 * count size of send data in bytes
1612 * Returns: number of characters written
1614 static int mgslpc_write(struct tty_struct * tty,
1615 const unsigned char *buf, int count)
1618 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1619 unsigned long flags;
1621 if (debug_level >= DEBUG_LEVEL_INFO)
1622 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1623 __FILE__,__LINE__,info->device_name,count);
1625 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1629 if (info->params.mode == MGSL_MODE_HDLC) {
1630 if (count > TXBUFSIZE) {
1634 if (info->tx_active)
1636 else if (info->tx_count)
1642 min(TXBUFSIZE - info->tx_count - 1,
1643 TXBUFSIZE - info->tx_put));
1647 memcpy(info->tx_buf + info->tx_put, buf, c);
1649 spin_lock_irqsave(&info->lock,flags);
1650 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1651 info->tx_count += c;
1652 spin_unlock_irqrestore(&info->lock,flags);
1659 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1660 spin_lock_irqsave(&info->lock,flags);
1661 if (!info->tx_active)
1663 spin_unlock_irqrestore(&info->lock,flags);
1666 if (debug_level >= DEBUG_LEVEL_INFO)
1667 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1668 __FILE__,__LINE__,info->device_name,ret);
1672 /* Return the count of free bytes in transmit buffer
1674 static int mgslpc_write_room(struct tty_struct *tty)
1676 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1679 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1682 if (info->params.mode == MGSL_MODE_HDLC) {
1683 /* HDLC (frame oriented) mode */
1684 if (info->tx_active)
1687 return HDLC_MAX_FRAME_SIZE;
1689 ret = TXBUFSIZE - info->tx_count - 1;
1694 if (debug_level >= DEBUG_LEVEL_INFO)
1695 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1696 __FILE__,__LINE__, info->device_name, ret);
1700 /* Return the count of bytes in transmit buffer
1702 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1704 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1707 if (debug_level >= DEBUG_LEVEL_INFO)
1708 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1709 __FILE__,__LINE__, info->device_name );
1711 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1714 if (info->params.mode == MGSL_MODE_HDLC)
1715 rc = info->tx_active ? info->max_frame_size : 0;
1717 rc = info->tx_count;
1719 if (debug_level >= DEBUG_LEVEL_INFO)
1720 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1721 __FILE__,__LINE__, info->device_name, rc);
1726 /* Discard all data in the send buffer
1728 static void mgslpc_flush_buffer(struct tty_struct *tty)
1730 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1731 unsigned long flags;
1733 if (debug_level >= DEBUG_LEVEL_INFO)
1734 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1735 __FILE__,__LINE__, info->device_name );
1737 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1740 spin_lock_irqsave(&info->lock,flags);
1741 info->tx_count = info->tx_put = info->tx_get = 0;
1742 del_timer(&info->tx_timer);
1743 spin_unlock_irqrestore(&info->lock,flags);
1745 wake_up_interruptible(&tty->write_wait);
1749 /* Send a high-priority XON/XOFF character
1751 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1753 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1754 unsigned long flags;
1756 if (debug_level >= DEBUG_LEVEL_INFO)
1757 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1758 __FILE__,__LINE__, info->device_name, ch );
1760 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1765 spin_lock_irqsave(&info->lock,flags);
1766 if (!info->tx_enabled)
1768 spin_unlock_irqrestore(&info->lock,flags);
1772 /* Signal remote device to throttle send data (our receive data)
1774 static void mgslpc_throttle(struct tty_struct * tty)
1776 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1777 unsigned long flags;
1779 if (debug_level >= DEBUG_LEVEL_INFO)
1780 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1781 __FILE__,__LINE__, info->device_name );
1783 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1787 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1789 if (tty->termios->c_cflag & CRTSCTS) {
1790 spin_lock_irqsave(&info->lock,flags);
1791 info->serial_signals &= ~SerialSignal_RTS;
1793 spin_unlock_irqrestore(&info->lock,flags);
1797 /* Signal remote device to stop throttling send data (our receive data)
1799 static void mgslpc_unthrottle(struct tty_struct * tty)
1801 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1802 unsigned long flags;
1804 if (debug_level >= DEBUG_LEVEL_INFO)
1805 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1806 __FILE__,__LINE__, info->device_name );
1808 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1815 mgslpc_send_xchar(tty, START_CHAR(tty));
1818 if (tty->termios->c_cflag & CRTSCTS) {
1819 spin_lock_irqsave(&info->lock,flags);
1820 info->serial_signals |= SerialSignal_RTS;
1822 spin_unlock_irqrestore(&info->lock,flags);
1826 /* get the current serial statistics
1828 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1831 if (debug_level >= DEBUG_LEVEL_INFO)
1832 printk("get_params(%s)\n", info->device_name);
1834 memset(&info->icount, 0, sizeof(info->icount));
1836 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1843 /* get the current serial parameters
1845 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1848 if (debug_level >= DEBUG_LEVEL_INFO)
1849 printk("get_params(%s)\n", info->device_name);
1850 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1856 /* set the serial parameters
1860 * info pointer to device instance data
1861 * new_params user buffer containing new serial params
1863 * Returns: 0 if success, otherwise error code
1865 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params)
1867 unsigned long flags;
1868 MGSL_PARAMS tmp_params;
1871 if (debug_level >= DEBUG_LEVEL_INFO)
1872 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1873 info->device_name );
1874 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1876 if ( debug_level >= DEBUG_LEVEL_INFO )
1877 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1878 __FILE__,__LINE__,info->device_name);
1882 spin_lock_irqsave(&info->lock,flags);
1883 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1884 spin_unlock_irqrestore(&info->lock,flags);
1886 mgslpc_change_params(info);
1891 static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1894 if (debug_level >= DEBUG_LEVEL_INFO)
1895 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1896 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1902 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1904 unsigned long flags;
1905 if (debug_level >= DEBUG_LEVEL_INFO)
1906 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1907 spin_lock_irqsave(&info->lock,flags);
1908 info->idle_mode = idle_mode;
1910 spin_unlock_irqrestore(&info->lock,flags);
1914 static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1917 if (debug_level >= DEBUG_LEVEL_INFO)
1918 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1919 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1925 static int set_interface(MGSLPC_INFO * info, int if_mode)
1927 unsigned long flags;
1929 if (debug_level >= DEBUG_LEVEL_INFO)
1930 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1931 spin_lock_irqsave(&info->lock,flags);
1932 info->if_mode = if_mode;
1934 val = read_reg(info, PVR) & 0x0f;
1935 switch (info->if_mode)
1937 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
1938 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
1939 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
1941 write_reg(info, PVR, val);
1943 spin_unlock_irqrestore(&info->lock,flags);
1947 static int set_txenable(MGSLPC_INFO * info, int enable)
1949 unsigned long flags;
1951 if (debug_level >= DEBUG_LEVEL_INFO)
1952 printk("set_txenable(%s,%d)\n", info->device_name, enable);
1954 spin_lock_irqsave(&info->lock,flags);
1956 if (!info->tx_enabled)
1959 if (info->tx_enabled)
1962 spin_unlock_irqrestore(&info->lock,flags);
1966 static int tx_abort(MGSLPC_INFO * info)
1968 unsigned long flags;
1970 if (debug_level >= DEBUG_LEVEL_INFO)
1971 printk("tx_abort(%s)\n", info->device_name);
1973 spin_lock_irqsave(&info->lock,flags);
1974 if (info->tx_active && info->tx_count &&
1975 info->params.mode == MGSL_MODE_HDLC) {
1976 /* clear data count so FIFO is not filled on next IRQ.
1977 * This results in underrun and abort transmission.
1979 info->tx_count = info->tx_put = info->tx_get = 0;
1980 info->tx_aborting = true;
1982 spin_unlock_irqrestore(&info->lock,flags);
1986 static int set_rxenable(MGSLPC_INFO * info, int enable)
1988 unsigned long flags;
1990 if (debug_level >= DEBUG_LEVEL_INFO)
1991 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
1993 spin_lock_irqsave(&info->lock,flags);
1995 if (!info->rx_enabled)
1998 if (info->rx_enabled)
2001 spin_unlock_irqrestore(&info->lock,flags);
2005 /* wait for specified event to occur
2007 * Arguments: info pointer to device instance data
2008 * mask pointer to bitmask of events to wait for
2009 * Return Value: 0 if successful and bit mask updated with
2010 * of events triggerred,
2011 * otherwise error code
2013 static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
2015 unsigned long flags;
2018 struct mgsl_icount cprev, cnow;
2021 struct _input_signal_events oldsigs, newsigs;
2022 DECLARE_WAITQUEUE(wait, current);
2024 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2028 if (debug_level >= DEBUG_LEVEL_INFO)
2029 printk("wait_events(%s,%d)\n", info->device_name, mask);
2031 spin_lock_irqsave(&info->lock,flags);
2033 /* return immediately if state matches requested events */
2035 s = info->serial_signals;
2037 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2038 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2039 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2040 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2042 spin_unlock_irqrestore(&info->lock,flags);
2046 /* save current irq counts */
2047 cprev = info->icount;
2048 oldsigs = info->input_signal_events;
2050 if ((info->params.mode == MGSL_MODE_HDLC) &&
2051 (mask & MgslEvent_ExitHuntMode))
2052 irq_enable(info, CHA, IRQ_EXITHUNT);
2054 set_current_state(TASK_INTERRUPTIBLE);
2055 add_wait_queue(&info->event_wait_q, &wait);
2057 spin_unlock_irqrestore(&info->lock,flags);
2062 if (signal_pending(current)) {
2067 /* get current irq counts */
2068 spin_lock_irqsave(&info->lock,flags);
2069 cnow = info->icount;
2070 newsigs = info->input_signal_events;
2071 set_current_state(TASK_INTERRUPTIBLE);
2072 spin_unlock_irqrestore(&info->lock,flags);
2074 /* if no change, wait aborted for some reason */
2075 if (newsigs.dsr_up == oldsigs.dsr_up &&
2076 newsigs.dsr_down == oldsigs.dsr_down &&
2077 newsigs.dcd_up == oldsigs.dcd_up &&
2078 newsigs.dcd_down == oldsigs.dcd_down &&
2079 newsigs.cts_up == oldsigs.cts_up &&
2080 newsigs.cts_down == oldsigs.cts_down &&
2081 newsigs.ri_up == oldsigs.ri_up &&
2082 newsigs.ri_down == oldsigs.ri_down &&
2083 cnow.exithunt == cprev.exithunt &&
2084 cnow.rxidle == cprev.rxidle) {
2090 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2091 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2092 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2093 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2094 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2095 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2096 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2097 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2098 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2099 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2107 remove_wait_queue(&info->event_wait_q, &wait);
2108 set_current_state(TASK_RUNNING);
2110 if (mask & MgslEvent_ExitHuntMode) {
2111 spin_lock_irqsave(&info->lock,flags);
2112 if (!waitqueue_active(&info->event_wait_q))
2113 irq_disable(info, CHA, IRQ_EXITHUNT);
2114 spin_unlock_irqrestore(&info->lock,flags);
2118 PUT_USER(rc, events, mask_ptr);
2122 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2124 unsigned long flags;
2126 struct mgsl_icount cprev, cnow;
2127 DECLARE_WAITQUEUE(wait, current);
2129 /* save current irq counts */
2130 spin_lock_irqsave(&info->lock,flags);
2131 cprev = info->icount;
2132 add_wait_queue(&info->status_event_wait_q, &wait);
2133 set_current_state(TASK_INTERRUPTIBLE);
2134 spin_unlock_irqrestore(&info->lock,flags);
2138 if (signal_pending(current)) {
2143 /* get new irq counts */
2144 spin_lock_irqsave(&info->lock,flags);
2145 cnow = info->icount;
2146 set_current_state(TASK_INTERRUPTIBLE);
2147 spin_unlock_irqrestore(&info->lock,flags);
2149 /* if no change, wait aborted for some reason */
2150 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2151 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2156 /* check for change in caller specified modem input */
2157 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2158 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2159 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2160 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2167 remove_wait_queue(&info->status_event_wait_q, &wait);
2168 set_current_state(TASK_RUNNING);
2172 /* return the state of the serial control and status signals
2174 static int tiocmget(struct tty_struct *tty, struct file *file)
2176 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2177 unsigned int result;
2178 unsigned long flags;
2180 spin_lock_irqsave(&info->lock,flags);
2182 spin_unlock_irqrestore(&info->lock,flags);
2184 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2185 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2186 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2187 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2188 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2189 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2191 if (debug_level >= DEBUG_LEVEL_INFO)
2192 printk("%s(%d):%s tiocmget() value=%08X\n",
2193 __FILE__,__LINE__, info->device_name, result );
2197 /* set modem control signals (DTR/RTS)
2199 static int tiocmset(struct tty_struct *tty, struct file *file,
2200 unsigned int set, unsigned int clear)
2202 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2203 unsigned long flags;
2205 if (debug_level >= DEBUG_LEVEL_INFO)
2206 printk("%s(%d):%s tiocmset(%x,%x)\n",
2207 __FILE__,__LINE__,info->device_name, set, clear);
2209 if (set & TIOCM_RTS)
2210 info->serial_signals |= SerialSignal_RTS;
2211 if (set & TIOCM_DTR)
2212 info->serial_signals |= SerialSignal_DTR;
2213 if (clear & TIOCM_RTS)
2214 info->serial_signals &= ~SerialSignal_RTS;
2215 if (clear & TIOCM_DTR)
2216 info->serial_signals &= ~SerialSignal_DTR;
2218 spin_lock_irqsave(&info->lock,flags);
2220 spin_unlock_irqrestore(&info->lock,flags);
2225 /* Set or clear transmit break condition
2227 * Arguments: tty pointer to tty instance data
2228 * break_state -1=set break condition, 0=clear
2230 static void mgslpc_break(struct tty_struct *tty, int break_state)
2232 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2233 unsigned long flags;
2235 if (debug_level >= DEBUG_LEVEL_INFO)
2236 printk("%s(%d):mgslpc_break(%s,%d)\n",
2237 __FILE__,__LINE__, info->device_name, break_state);
2239 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2242 spin_lock_irqsave(&info->lock,flags);
2243 if (break_state == -1)
2244 set_reg_bits(info, CHA+DAFO, BIT6);
2246 clear_reg_bits(info, CHA+DAFO, BIT6);
2247 spin_unlock_irqrestore(&info->lock,flags);
2250 /* Service an IOCTL request
2254 * tty pointer to tty instance data
2255 * file pointer to associated file object for device
2256 * cmd IOCTL command code
2257 * arg command argument/context
2259 * Return Value: 0 if success, otherwise error code
2261 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2262 unsigned int cmd, unsigned long arg)
2264 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2266 if (debug_level >= DEBUG_LEVEL_INFO)
2267 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2268 info->device_name, cmd );
2270 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2273 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2274 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2275 if (tty->flags & (1 << TTY_IO_ERROR))
2279 return ioctl_common(info, cmd, arg);
2282 static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
2285 struct mgsl_icount cnow; /* kernel counter temps */
2286 struct serial_icounter_struct __user *p_cuser; /* user space */
2287 void __user *argp = (void __user *)arg;
2288 unsigned long flags;
2291 case MGSL_IOCGPARAMS:
2292 return get_params(info, argp);
2293 case MGSL_IOCSPARAMS:
2294 return set_params(info, argp);
2295 case MGSL_IOCGTXIDLE:
2296 return get_txidle(info, argp);
2297 case MGSL_IOCSTXIDLE:
2298 return set_txidle(info, (int)arg);
2300 return get_interface(info, argp);
2302 return set_interface(info,(int)arg);
2303 case MGSL_IOCTXENABLE:
2304 return set_txenable(info,(int)arg);
2305 case MGSL_IOCRXENABLE:
2306 return set_rxenable(info,(int)arg);
2307 case MGSL_IOCTXABORT:
2308 return tx_abort(info);
2309 case MGSL_IOCGSTATS:
2310 return get_stats(info, argp);
2311 case MGSL_IOCWAITEVENT:
2312 return wait_events(info, argp);
2314 return modem_input_wait(info,(int)arg);
2316 spin_lock_irqsave(&info->lock,flags);
2317 cnow = info->icount;
2318 spin_unlock_irqrestore(&info->lock,flags);
2320 PUT_USER(error,cnow.cts, &p_cuser->cts);
2321 if (error) return error;
2322 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2323 if (error) return error;
2324 PUT_USER(error,cnow.rng, &p_cuser->rng);
2325 if (error) return error;
2326 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2327 if (error) return error;
2328 PUT_USER(error,cnow.rx, &p_cuser->rx);
2329 if (error) return error;
2330 PUT_USER(error,cnow.tx, &p_cuser->tx);
2331 if (error) return error;
2332 PUT_USER(error,cnow.frame, &p_cuser->frame);
2333 if (error) return error;
2334 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2335 if (error) return error;
2336 PUT_USER(error,cnow.parity, &p_cuser->parity);
2337 if (error) return error;
2338 PUT_USER(error,cnow.brk, &p_cuser->brk);
2339 if (error) return error;
2340 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2341 if (error) return error;
2344 return -ENOIOCTLCMD;
2349 /* Set new termios settings
2353 * tty pointer to tty structure
2354 * termios pointer to buffer to hold returned old termios
2356 static void mgslpc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2358 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2359 unsigned long flags;
2361 if (debug_level >= DEBUG_LEVEL_INFO)
2362 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2363 tty->driver->name );
2365 /* just return if nothing has changed */
2366 if ((tty->termios->c_cflag == old_termios->c_cflag)
2367 && (RELEVANT_IFLAG(tty->termios->c_iflag)
2368 == RELEVANT_IFLAG(old_termios->c_iflag)))
2371 mgslpc_change_params(info);
2373 /* Handle transition to B0 status */
2374 if (old_termios->c_cflag & CBAUD &&
2375 !(tty->termios->c_cflag & CBAUD)) {
2376 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2377 spin_lock_irqsave(&info->lock,flags);
2379 spin_unlock_irqrestore(&info->lock,flags);
2382 /* Handle transition away from B0 status */
2383 if (!(old_termios->c_cflag & CBAUD) &&
2384 tty->termios->c_cflag & CBAUD) {
2385 info->serial_signals |= SerialSignal_DTR;
2386 if (!(tty->termios->c_cflag & CRTSCTS) ||
2387 !test_bit(TTY_THROTTLED, &tty->flags)) {
2388 info->serial_signals |= SerialSignal_RTS;
2390 spin_lock_irqsave(&info->lock,flags);
2392 spin_unlock_irqrestore(&info->lock,flags);
2395 /* Handle turning off CRTSCTS */
2396 if (old_termios->c_cflag & CRTSCTS &&
2397 !(tty->termios->c_cflag & CRTSCTS)) {
2398 tty->hw_stopped = 0;
2403 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2405 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2407 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2410 if (debug_level >= DEBUG_LEVEL_INFO)
2411 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2412 __FILE__,__LINE__, info->device_name, info->count);
2417 if (tty_hung_up_p(filp))
2420 if ((tty->count == 1) && (info->count != 1)) {
2422 * tty->count is 1 and the tty structure will be freed.
2423 * info->count should be one in this case.
2424 * if it's not, correct it so that the port is shutdown.
2426 printk("mgslpc_close: bad refcount; tty->count is 1, "
2427 "info->count is %d\n", info->count);
2433 /* if at least one open remaining, leave hardware active */
2437 info->flags |= ASYNC_CLOSING;
2439 /* set tty->closing to notify line discipline to
2440 * only process XON/XOFF characters. Only the N_TTY
2441 * discipline appears to use this (ppp does not).
2445 /* wait for transmit data to clear all layers */
2447 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
2448 if (debug_level >= DEBUG_LEVEL_INFO)
2449 printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sent\n",
2450 __FILE__,__LINE__, info->device_name );
2451 tty_wait_until_sent(tty, info->closing_wait);
2454 if (info->flags & ASYNC_INITIALIZED)
2455 mgslpc_wait_until_sent(tty, info->timeout);
2457 mgslpc_flush_buffer(tty);
2459 tty_ldisc_flush(tty);
2466 if (info->blocked_open) {
2467 if (info->close_delay) {
2468 msleep_interruptible(jiffies_to_msecs(info->close_delay));
2470 wake_up_interruptible(&info->open_wait);
2473 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2475 wake_up_interruptible(&info->close_wait);
2478 if (debug_level >= DEBUG_LEVEL_INFO)
2479 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2480 tty->driver->name, info->count);
2483 /* Wait until the transmitter is empty.
2485 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2487 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2488 unsigned long orig_jiffies, char_time;
2493 if (debug_level >= DEBUG_LEVEL_INFO)
2494 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2495 __FILE__,__LINE__, info->device_name );
2497 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2500 if (!(info->flags & ASYNC_INITIALIZED))
2503 orig_jiffies = jiffies;
2505 /* Set check interval to 1/5 of estimated time to
2506 * send a character, and make it at least 1. The check
2507 * interval should also be less than the timeout.
2508 * Note: use tight timings here to satisfy the NIST-PCTS.
2511 if ( info->params.data_rate ) {
2512 char_time = info->timeout/(32 * 5);
2519 char_time = min_t(unsigned long, char_time, timeout);
2521 if (info->params.mode == MGSL_MODE_HDLC) {
2522 while (info->tx_active) {
2523 msleep_interruptible(jiffies_to_msecs(char_time));
2524 if (signal_pending(current))
2526 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2530 while ((info->tx_count || info->tx_active) &&
2532 msleep_interruptible(jiffies_to_msecs(char_time));
2533 if (signal_pending(current))
2535 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2541 if (debug_level >= DEBUG_LEVEL_INFO)
2542 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2543 __FILE__,__LINE__, info->device_name );
2546 /* Called by tty_hangup() when a hangup is signaled.
2547 * This is the same as closing all open files for the port.
2549 static void mgslpc_hangup(struct tty_struct *tty)
2551 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2553 if (debug_level >= DEBUG_LEVEL_INFO)
2554 printk("%s(%d):mgslpc_hangup(%s)\n",
2555 __FILE__,__LINE__, info->device_name );
2557 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2560 mgslpc_flush_buffer(tty);
2564 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2567 wake_up_interruptible(&info->open_wait);
2570 /* Block the current process until the specified port
2571 * is ready to be opened.
2573 static int block_til_ready(struct tty_struct *tty, struct file *filp,
2576 DECLARE_WAITQUEUE(wait, current);
2578 bool do_clocal = false;
2579 bool extra_count = false;
2580 unsigned long flags;
2582 if (debug_level >= DEBUG_LEVEL_INFO)
2583 printk("%s(%d):block_til_ready on %s\n",
2584 __FILE__,__LINE__, tty->driver->name );
2586 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
2587 /* nonblock mode is set or port is not enabled */
2588 /* just verify that callout device is not active */
2589 info->flags |= ASYNC_NORMAL_ACTIVE;
2593 if (tty->termios->c_cflag & CLOCAL)
2596 /* Wait for carrier detect and the line to become
2597 * free (i.e., not in use by the callout). While we are in
2598 * this loop, info->count is dropped by one, so that
2599 * mgslpc_close() knows when to free things. We restore it upon
2600 * exit, either normal or abnormal.
2604 add_wait_queue(&info->open_wait, &wait);
2606 if (debug_level >= DEBUG_LEVEL_INFO)
2607 printk("%s(%d):block_til_ready before block on %s count=%d\n",
2608 __FILE__,__LINE__, tty->driver->name, info->count );
2610 spin_lock_irqsave(&info->lock, flags);
2611 if (!tty_hung_up_p(filp)) {
2615 spin_unlock_irqrestore(&info->lock, flags);
2616 info->blocked_open++;
2619 if ((tty->termios->c_cflag & CBAUD)) {
2620 spin_lock_irqsave(&info->lock,flags);
2621 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2623 spin_unlock_irqrestore(&info->lock,flags);
2626 set_current_state(TASK_INTERRUPTIBLE);
2628 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
2629 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
2630 -EAGAIN : -ERESTARTSYS;
2634 spin_lock_irqsave(&info->lock,flags);
2636 spin_unlock_irqrestore(&info->lock,flags);
2638 if (!(info->flags & ASYNC_CLOSING) &&
2639 (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
2643 if (signal_pending(current)) {
2644 retval = -ERESTARTSYS;
2648 if (debug_level >= DEBUG_LEVEL_INFO)
2649 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
2650 __FILE__,__LINE__, tty->driver->name, info->count );
2655 set_current_state(TASK_RUNNING);
2656 remove_wait_queue(&info->open_wait, &wait);
2660 info->blocked_open--;
2662 if (debug_level >= DEBUG_LEVEL_INFO)
2663 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
2664 __FILE__,__LINE__, tty->driver->name, info->count );
2667 info->flags |= ASYNC_NORMAL_ACTIVE;
2672 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2676 unsigned long flags;
2678 /* verify range of specified line number */
2680 if ((line < 0) || (line >= mgslpc_device_count)) {
2681 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2682 __FILE__,__LINE__,line);
2686 /* find the info structure for the specified line */
2687 info = mgslpc_device_list;
2688 while(info && info->line != line)
2689 info = info->next_device;
2690 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2693 tty->driver_data = info;
2696 if (debug_level >= DEBUG_LEVEL_INFO)
2697 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2698 __FILE__,__LINE__,tty->driver->name, info->count);
2700 /* If port is closing, signal caller to try again */
2701 if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
2702 if (info->flags & ASYNC_CLOSING)
2703 interruptible_sleep_on(&info->close_wait);
2704 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2705 -EAGAIN : -ERESTARTSYS);
2709 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2711 spin_lock_irqsave(&info->netlock, flags);
2712 if (info->netcount) {
2714 spin_unlock_irqrestore(&info->netlock, flags);
2718 spin_unlock_irqrestore(&info->netlock, flags);
2720 if (info->count == 1) {
2721 /* 1st open on this device, init hardware */
2722 retval = startup(info);
2727 retval = block_til_ready(tty, filp, info);
2729 if (debug_level >= DEBUG_LEVEL_INFO)
2730 printk("%s(%d):block_til_ready(%s) returned %d\n",
2731 __FILE__,__LINE__, info->device_name, retval);
2735 if (debug_level >= DEBUG_LEVEL_INFO)
2736 printk("%s(%d):mgslpc_open(%s) success\n",
2737 __FILE__,__LINE__, info->device_name);
2742 if (tty->count == 1)
2743 info->tty = NULL; /* tty layer will release tty struct */
2752 * /proc fs routines....
2755 static inline int line_info(char *buf, MGSLPC_INFO *info)
2759 unsigned long flags;
2761 ret = sprintf(buf, "%s:io:%04X irq:%d",
2762 info->device_name, info->io_base, info->irq_level);
2764 /* output current serial signal states */
2765 spin_lock_irqsave(&info->lock,flags);
2767 spin_unlock_irqrestore(&info->lock,flags);
2771 if (info->serial_signals & SerialSignal_RTS)
2772 strcat(stat_buf, "|RTS");
2773 if (info->serial_signals & SerialSignal_CTS)
2774 strcat(stat_buf, "|CTS");
2775 if (info->serial_signals & SerialSignal_DTR)
2776 strcat(stat_buf, "|DTR");
2777 if (info->serial_signals & SerialSignal_DSR)
2778 strcat(stat_buf, "|DSR");
2779 if (info->serial_signals & SerialSignal_DCD)
2780 strcat(stat_buf, "|CD");
2781 if (info->serial_signals & SerialSignal_RI)
2782 strcat(stat_buf, "|RI");
2784 if (info->params.mode == MGSL_MODE_HDLC) {
2785 ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
2786 info->icount.txok, info->icount.rxok);
2787 if (info->icount.txunder)
2788 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
2789 if (info->icount.txabort)
2790 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
2791 if (info->icount.rxshort)
2792 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
2793 if (info->icount.rxlong)
2794 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
2795 if (info->icount.rxover)
2796 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
2797 if (info->icount.rxcrc)
2798 ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
2800 ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
2801 info->icount.tx, info->icount.rx);
2802 if (info->icount.frame)
2803 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2804 if (info->icount.parity)
2805 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2806 if (info->icount.brk)
2807 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2808 if (info->icount.overrun)
2809 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2812 /* Append serial signal status to end */
2813 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2815 ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2816 info->tx_active,info->bh_requested,info->bh_running,
2822 /* Called to print information about devices
2824 static int mgslpc_read_proc(char *page, char **start, off_t off, int count,
2825 int *eof, void *data)
2831 len += sprintf(page, "synclink driver:%s\n", driver_version);
2833 info = mgslpc_device_list;
2835 l = line_info(page + len, info);
2837 if (len+begin > off+count)
2839 if (len+begin < off) {
2843 info = info->next_device;
2848 if (off >= len+begin)
2850 *start = page + (off-begin);
2851 return ((count < begin+len-off) ? count : begin+len-off);
2854 static int rx_alloc_buffers(MGSLPC_INFO *info)
2856 /* each buffer has header and data */
2857 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2859 /* calculate total allocation size for 8 buffers */
2860 info->rx_buf_total_size = info->rx_buf_size * 8;
2862 /* limit total allocated memory */
2863 if (info->rx_buf_total_size > 0x10000)
2864 info->rx_buf_total_size = 0x10000;
2866 /* calculate number of buffers */
2867 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2869 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2870 if (info->rx_buf == NULL)
2873 rx_reset_buffers(info);
2877 static void rx_free_buffers(MGSLPC_INFO *info)
2879 kfree(info->rx_buf);
2880 info->rx_buf = NULL;
2883 static int claim_resources(MGSLPC_INFO *info)
2885 if (rx_alloc_buffers(info) < 0 ) {
2886 printk( "Cant allocate rx buffer %s\n", info->device_name);
2887 release_resources(info);
2893 static void release_resources(MGSLPC_INFO *info)
2895 if (debug_level >= DEBUG_LEVEL_INFO)
2896 printk("release_resources(%s)\n", info->device_name);
2897 rx_free_buffers(info);
2900 /* Add the specified device instance data structure to the
2901 * global linked list of devices and increment the device count.
2903 * Arguments: info pointer to device instance data
2905 static void mgslpc_add_device(MGSLPC_INFO *info)
2907 info->next_device = NULL;
2908 info->line = mgslpc_device_count;
2909 sprintf(info->device_name,"ttySLP%d",info->line);
2911 if (info->line < MAX_DEVICE_COUNT) {
2912 if (maxframe[info->line])
2913 info->max_frame_size = maxframe[info->line];
2916 mgslpc_device_count++;
2918 if (!mgslpc_device_list)
2919 mgslpc_device_list = info;
2921 MGSLPC_INFO *current_dev = mgslpc_device_list;
2922 while( current_dev->next_device )
2923 current_dev = current_dev->next_device;
2924 current_dev->next_device = info;
2927 if (info->max_frame_size < 4096)
2928 info->max_frame_size = 4096;
2929 else if (info->max_frame_size > 65535)
2930 info->max_frame_size = 65535;
2932 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2933 info->device_name, info->io_base, info->irq_level);
2935 #if SYNCLINK_GENERIC_HDLC
2940 static void mgslpc_remove_device(MGSLPC_INFO *remove_info)
2942 MGSLPC_INFO *info = mgslpc_device_list;
2943 MGSLPC_INFO *last = NULL;
2946 if (info == remove_info) {
2948 last->next_device = info->next_device;
2950 mgslpc_device_list = info->next_device;
2951 #if SYNCLINK_GENERIC_HDLC
2954 release_resources(info);
2956 mgslpc_device_count--;
2960 info = info->next_device;
2964 static struct pcmcia_device_id mgslpc_ids[] = {
2965 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
2968 MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
2970 static struct pcmcia_driver mgslpc_driver = {
2971 .owner = THIS_MODULE,
2973 .name = "synclink_cs",
2975 .probe = mgslpc_probe,
2976 .remove = mgslpc_detach,
2977 .id_table = mgslpc_ids,
2978 .suspend = mgslpc_suspend,
2979 .resume = mgslpc_resume,
2982 static const struct tty_operations mgslpc_ops = {
2983 .open = mgslpc_open,
2984 .close = mgslpc_close,
2985 .write = mgslpc_write,
2986 .put_char = mgslpc_put_char,
2987 .flush_chars = mgslpc_flush_chars,
2988 .write_room = mgslpc_write_room,
2989 .chars_in_buffer = mgslpc_chars_in_buffer,
2990 .flush_buffer = mgslpc_flush_buffer,
2991 .ioctl = mgslpc_ioctl,
2992 .throttle = mgslpc_throttle,
2993 .unthrottle = mgslpc_unthrottle,
2994 .send_xchar = mgslpc_send_xchar,
2995 .break_ctl = mgslpc_break,
2996 .wait_until_sent = mgslpc_wait_until_sent,
2997 .read_proc = mgslpc_read_proc,
2998 .set_termios = mgslpc_set_termios,
3000 .start = tx_release,
3001 .hangup = mgslpc_hangup,
3002 .tiocmget = tiocmget,
3003 .tiocmset = tiocmset,
3006 static void synclink_cs_cleanup(void)
3010 printk("Unloading %s: version %s\n", driver_name, driver_version);
3012 while(mgslpc_device_list)
3013 mgslpc_remove_device(mgslpc_device_list);
3015 if (serial_driver) {
3016 if ((rc = tty_unregister_driver(serial_driver)))
3017 printk("%s(%d) failed to unregister tty driver err=%d\n",
3018 __FILE__,__LINE__,rc);
3019 put_tty_driver(serial_driver);
3022 pcmcia_unregister_driver(&mgslpc_driver);
3025 static int __init synclink_cs_init(void)
3029 if (break_on_load) {
3030 mgslpc_get_text_ptr();
3034 printk("%s %s\n", driver_name, driver_version);
3036 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
3039 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
3040 if (!serial_driver) {
3045 /* Initialize the tty_driver structure */
3047 serial_driver->owner = THIS_MODULE;
3048 serial_driver->driver_name = "synclink_cs";
3049 serial_driver->name = "ttySLP";
3050 serial_driver->major = ttymajor;
3051 serial_driver->minor_start = 64;
3052 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3053 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3054 serial_driver->init_termios = tty_std_termios;
3055 serial_driver->init_termios.c_cflag =
3056 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3057 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3058 tty_set_operations(serial_driver, &mgslpc_ops);
3060 if ((rc = tty_register_driver(serial_driver)) < 0) {
3061 printk("%s(%d):Couldn't register serial driver\n",
3063 put_tty_driver(serial_driver);
3064 serial_driver = NULL;
3068 printk("%s %s, tty major#%d\n",
3069 driver_name, driver_version,
3070 serial_driver->major);
3075 synclink_cs_cleanup();
3079 static void __exit synclink_cs_exit(void)
3081 synclink_cs_cleanup();
3084 module_init(synclink_cs_init);
3085 module_exit(synclink_cs_exit);
3087 static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
3092 /* note:standard BRG mode is broken in V3.2 chip
3093 * so enhanced mode is always used
3101 for (M = 1; N > 64 && M < 16; M++)
3107 * BGR[7..0] contained in BGR register
3108 * BGR[9..8] contained in CCR2[7..6]
3109 * divisor = (N+1)*2^M
3111 * Note: M *must* not be zero (causes asymetric duty cycle)
3113 write_reg(info, (unsigned char) (channel + BGR),
3114 (unsigned char) ((M << 6) + N));
3115 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
3116 val |= ((M << 4) & 0xc0);
3117 write_reg(info, (unsigned char) (channel + CCR2), val);
3121 /* Enabled the AUX clock output at the specified frequency.
3123 static void enable_auxclk(MGSLPC_INFO *info)
3129 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3130 * 05 ADM Address Mode, 0 = no addr recognition
3131 * 04 TMD Timer Mode, 0 = external
3132 * 03 RAC Receiver Active, 0 = inactive
3133 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3134 * 01 TRS Timer Resolution, 1=512
3135 * 00 TLP Test Loop, 0 = no loop
3141 /* channel B RTS is used to enable AUXCLK driver on SP505 */
3142 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3144 write_reg(info, CHB + MODE, val);
3148 * 07 PU Power Up, 1=active, 0=power down
3149 * 06 MCE Master Clock Enable, 1=enabled
3151 * 04..02 SC[2..0] Encoding
3152 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3156 write_reg(info, CHB + CCR0, 0xc0);
3160 * 07 SFLG Shared Flag, 0 = disable shared flags
3161 * 06 GALP Go Active On Loop, 0 = not used
3162 * 05 GLP Go On Loop, 0 = not used
3163 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3164 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3165 * 02..00 CM[2..0] Clock Mode
3169 write_reg(info, CHB + CCR1, 0x17);
3173 * 07..06 BGR[9..8] Baud rate bits 9..8
3174 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3175 * 04 SSEL Clock source select, 1=submode b
3176 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3177 * 02 RWX Read/Write Exchange 0=disabled
3178 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3179 * 00 DIV, data inversion 0=disabled, 1=enabled
3183 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3184 write_reg(info, CHB + CCR2, 0x38);
3186 write_reg(info, CHB + CCR2, 0x30);
3190 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3191 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3192 * 05 TST1 Test Pin, 0=normal operation
3193 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3194 * 03..02 Reserved, must be 0
3195 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3199 write_reg(info, CHB + CCR4, 0x50);
3201 /* if auxclk not enabled, set internal BRG so
3202 * CTS transitions can be detected (requires TxC)
3204 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3205 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3207 mgslpc_set_rate(info, CHB, 921600);
3210 static void loopback_enable(MGSLPC_INFO *info)
3214 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3215 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3216 write_reg(info, CHA + CCR1, val);
3218 /* CCR2:04 SSEL Clock source select, 1=submode b */
3219 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3220 write_reg(info, CHA + CCR2, val);
3222 /* set LinkSpeed if available, otherwise default to 2Mbps */
3223 if (info->params.clock_speed)
3224 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3226 mgslpc_set_rate(info, CHA, 1843200);
3228 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3229 val = read_reg(info, CHA + MODE) | BIT0;
3230 write_reg(info, CHA + MODE, val);
3233 static void hdlc_mode(MGSLPC_INFO *info)
3236 unsigned char clkmode, clksubmode;
3238 /* disable all interrupts */
3239 irq_disable(info, CHA, 0xffff);
3240 irq_disable(info, CHB, 0xffff);
3241 port_irq_disable(info, 0xff);
3243 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3244 clkmode = clksubmode = 0;
3245 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3246 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3247 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3249 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3250 && info->params.flags & HDLC_FLAG_TXC_BRG) {
3251 /* clock mode 7b, rcv = BRG, xmt = BRG */
3254 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3255 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3256 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3260 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3263 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3264 /* clock mode 0b, rcv = RxC, xmt = BRG */
3270 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3271 * 05 ADM Address Mode, 0 = no addr recognition
3272 * 04 TMD Timer Mode, 0 = external
3273 * 03 RAC Receiver Active, 0 = inactive
3274 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3275 * 01 TRS Timer Resolution, 1=512
3276 * 00 TLP Test Loop, 0 = no loop
3281 if (info->params.loopback)
3284 /* preserve RTS state */
3285 if (info->serial_signals & SerialSignal_RTS)
3287 write_reg(info, CHA + MODE, val);
3291 * 07 PU Power Up, 1=active, 0=power down
3292 * 06 MCE Master Clock Enable, 1=enabled
3294 * 04..02 SC[2..0] Encoding
3295 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3300 switch (info->params.encoding)
3302 case HDLC_ENCODING_NRZI:
3305 case HDLC_ENCODING_BIPHASE_SPACE:
3308 case HDLC_ENCODING_BIPHASE_MARK:
3311 case HDLC_ENCODING_BIPHASE_LEVEL:
3313 break; // Manchester
3315 write_reg(info, CHA + CCR0, val);
3319 * 07 SFLG Shared Flag, 0 = disable shared flags
3320 * 06 GALP Go Active On Loop, 0 = not used
3321 * 05 GLP Go On Loop, 0 = not used
3322 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3323 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3324 * 02..00 CM[2..0] Clock Mode
3328 val = 0x10 + clkmode;
3329 write_reg(info, CHA + CCR1, val);
3333 * 07..06 BGR[9..8] Baud rate bits 9..8
3334 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3335 * 04 SSEL Clock source select, 1=submode b
3336 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3337 * 02 RWX Read/Write Exchange 0=disabled
3338 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3339 * 00 DIV, data inversion 0=disabled, 1=enabled
3344 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3345 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3349 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3351 if (info->params.encoding == HDLC_ENCODING_NRZB)
3353 write_reg(info, CHA + CCR2, val);
3357 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3358 * 05 EPT Enable preamble transmission, 1=enabled
3359 * 04 RADD Receive address pushed to FIFO, 0=disabled
3360 * 03 CRL CRC Reset Level, 0=FFFF
3361 * 02 RCRC Rx CRC 0=On 1=Off
3362 * 01 TCRC Tx CRC 0=On 1=Off
3363 * 00 PSD DPLL Phase Shift Disable
3368 if (info->params.crc_type == HDLC_CRC_NONE)
3370 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3372 switch (info->params.preamble_length)
3374 case HDLC_PREAMBLE_LENGTH_16BITS:
3377 case HDLC_PREAMBLE_LENGTH_32BITS:
3380 case HDLC_PREAMBLE_LENGTH_64BITS:
3384 write_reg(info, CHA + CCR3, val);
3386 /* PRE - Preamble pattern */
3388 switch (info->params.preamble)
3390 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3391 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3392 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3393 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3395 write_reg(info, CHA + PRE, val);
3399 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3400 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3401 * 05 TST1 Test Pin, 0=normal operation
3402 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3403 * 03..02 Reserved, must be 0
3404 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3409 write_reg(info, CHA + CCR4, val);
3410 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3411 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3413 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3415 /* RLCR Receive length check register
3417 * 7 1=enable receive length check
3418 * 6..0 Max frame length = (RL + 1) * 32
3420 write_reg(info, CHA + RLCR, 0);
3422 /* XBCH Transmit Byte Count High
3424 * 07 DMA mode, 0 = interrupt driven
3425 * 06 NRM, 0=ABM (ignored)
3426 * 05 CAS Carrier Auto Start
3427 * 04 XC Transmit Continuously (ignored)
3428 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3433 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3435 write_reg(info, CHA + XBCH, val);
3436 enable_auxclk(info);
3437 if (info->params.loopback || info->testing_irq)
3438 loopback_enable(info);
3439 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3441 irq_enable(info, CHB, IRQ_CTS);
3442 /* PVR[3] 1=AUTO CTS active */
3443 set_reg_bits(info, CHA + PVR, BIT3);
3445 clear_reg_bits(info, CHA + PVR, BIT3);
3447 irq_enable(info, CHA,
3448 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3449 IRQ_UNDERRUN + IRQ_TXFIFO);
3450 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3451 wait_command_complete(info, CHA);
3452 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3454 /* Master clock mode enabled above to allow reset commands
3455 * to complete even if no data clocks are present.
3457 * Disable master clock mode for normal communications because
3458 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3459 * IRQ when in master clock mode.
3461 * Leave master clock mode enabled for IRQ test because the
3462 * timer IRQ used by the test can only happen in master clock mode.
3464 if (!info->testing_irq)
3465 clear_reg_bits(info, CHA + CCR0, BIT6);
3473 static void rx_stop(MGSLPC_INFO *info)
3475 if (debug_level >= DEBUG_LEVEL_ISR)
3476 printk("%s(%d):rx_stop(%s)\n",
3477 __FILE__,__LINE__, info->device_name );
3479 /* MODE:03 RAC Receiver Active, 0=inactive */
3480 clear_reg_bits(info, CHA + MODE, BIT3);
3482 info->rx_enabled = false;
3483 info->rx_overflow = false;
3486 static void rx_start(MGSLPC_INFO *info)
3488 if (debug_level >= DEBUG_LEVEL_ISR)
3489 printk("%s(%d):rx_start(%s)\n",
3490 __FILE__,__LINE__, info->device_name );
3492 rx_reset_buffers(info);
3493 info->rx_enabled = false;
3494 info->rx_overflow = false;
3496 /* MODE:03 RAC Receiver Active, 1=active */
3497 set_reg_bits(info, CHA + MODE, BIT3);
3499 info->rx_enabled = true;
3502 static void tx_start(MGSLPC_INFO *info)
3504 if (debug_level >= DEBUG_LEVEL_ISR)
3505 printk("%s(%d):tx_start(%s)\n",
3506 __FILE__,__LINE__, info->device_name );
3508 if (info->tx_count) {
3509 /* If auto RTS enabled and RTS is inactive, then assert */
3510 /* RTS and set a flag indicating that the driver should */
3511 /* negate RTS when the transmission completes. */
3512 info->drop_rts_on_tx_done = false;
3514 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3516 if (!(info->serial_signals & SerialSignal_RTS)) {
3517 info->serial_signals |= SerialSignal_RTS;
3519 info->drop_rts_on_tx_done = true;
3523 if (info->params.mode == MGSL_MODE_ASYNC) {
3524 if (!info->tx_active) {
3525 info->tx_active = true;
3529 info->tx_active = true;
3531 mod_timer(&info->tx_timer, jiffies +
3532 msecs_to_jiffies(5000));
3536 if (!info->tx_enabled)
3537 info->tx_enabled = true;
3540 static void tx_stop(MGSLPC_INFO *info)
3542 if (debug_level >= DEBUG_LEVEL_ISR)
3543 printk("%s(%d):tx_stop(%s)\n",
3544 __FILE__,__LINE__, info->device_name );
3546 del_timer(&info->tx_timer);
3548 info->tx_enabled = false;
3549 info->tx_active = false;
3552 /* Reset the adapter to a known state and prepare it for further use.
3554 static void reset_device(MGSLPC_INFO *info)
3556 /* power up both channels (set BIT7) */
3557 write_reg(info, CHA + CCR0, 0x80);
3558 write_reg(info, CHB + CCR0, 0x80);
3559 write_reg(info, CHA + MODE, 0);
3560 write_reg(info, CHB + MODE, 0);
3562 /* disable all interrupts */
3563 irq_disable(info, CHA, 0xffff);
3564 irq_disable(info, CHB, 0xffff);
3565 port_irq_disable(info, 0xff);
3567 /* PCR Port Configuration Register
3569 * 07..04 DEC[3..0] Serial I/F select outputs
3570 * 03 output, 1=AUTO CTS control enabled
3571 * 02 RI Ring Indicator input 0=active
3572 * 01 DSR input 0=active
3573 * 00 DTR output 0=active
3577 write_reg(info, PCR, 0x06);
3579 /* PVR Port Value Register
3581 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3582 * 03 AUTO CTS output 1=enabled
3583 * 02 RI Ring Indicator input
3585 * 00 DTR output (1=inactive)
3589 // write_reg(info, PVR, PVR_DTR);
3591 /* IPC Interrupt Port Configuration
3593 * 07 VIS 1=Masked interrupts visible
3594 * 06..05 Reserved, 0
3595 * 04..03 SLA Slave address, 00 ignored
3596 * 02 CASM Cascading Mode, 1=daisy chain
3597 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3601 write_reg(info, IPC, 0x05);
3604 static void async_mode(MGSLPC_INFO *info)
3608 /* disable all interrupts */
3609 irq_disable(info, CHA, 0xffff);
3610 irq_disable(info, CHB, 0xffff);
3611 port_irq_disable(info, 0xff);
3616 * 06 FRTS RTS State, 0=active
3617 * 05 FCTS Flow Control on CTS
3618 * 04 FLON Flow Control Enable
3619 * 03 RAC Receiver Active, 0 = inactive
3620 * 02 RTS 0=Auto RTS, 1=manual RTS
3621 * 01 TRS Timer Resolution, 1=512
3622 * 00 TLP Test Loop, 0 = no loop
3627 if (info->params.loopback)
3630 /* preserve RTS state */
3631 if (!(info->serial_signals & SerialSignal_RTS))
3633 write_reg(info, CHA + MODE, val);
3637 * 07 PU Power Up, 1=active, 0=power down
3638 * 06 MCE Master Clock Enable, 1=enabled
3640 * 04..02 SC[2..0] Encoding, 000=NRZ
3641 * 01..00 SM[1..0] Serial Mode, 11=Async
3645 write_reg(info, CHA + CCR0, 0x83);
3649 * 07..05 Reserved, 0
3650 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3651 * 03 BCR Bit Clock Rate, 1=16x
3652 * 02..00 CM[2..0] Clock Mode, 111=BRG
3656 write_reg(info, CHA + CCR1, 0x1f);
3660 * 07..06 BGR[9..8] Baud rate bits 9..8
3661 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3662 * 04 SSEL Clock source select, 1=submode b
3663 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3664 * 02 RWX Read/Write Exchange 0=disabled
3666 * 00 DIV, data inversion 0=disabled, 1=enabled
3670 write_reg(info, CHA + CCR2, 0x10);
3674 * 07..01 Reserved, 0
3675 * 00 PSD DPLL Phase Shift Disable
3679 write_reg(info, CHA + CCR3, 0);
3683 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3684 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3685 * 05 TST1 Test Pin, 0=normal operation
3686 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3687 * 03..00 Reserved, must be 0
3691 write_reg(info, CHA + CCR4, 0x50);
3692 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3697 * 06 XBRK transmit break, 0=normal operation
3698 * 05 Stop bits (0=1, 1=2)
3699 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3700 * 02 PAREN Parity Enable
3701 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3705 if (info->params.data_bits != 8)
3706 val |= BIT0; /* 7 bits */
3707 if (info->params.stop_bits != 1)
3709 if (info->params.parity != ASYNC_PARITY_NONE)
3711 val |= BIT2; /* Parity enable */
3712 if (info->params.parity == ASYNC_PARITY_ODD)
3717 write_reg(info, CHA + DAFO, val);
3719 /* RFC Rx FIFO Control
3722 * 06 DPS, 1=parity bit not stored in data byte
3723 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3724 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3725 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3727 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3731 write_reg(info, CHA + RFC, 0x5c);
3733 /* RLCR Receive length check register
3735 * Max frame length = (RL + 1) * 32
3737 write_reg(info, CHA + RLCR, 0);
3739 /* XBCH Transmit Byte Count High
3741 * 07 DMA mode, 0 = interrupt driven
3742 * 06 NRM, 0=ABM (ignored)
3743 * 05 CAS Carrier Auto Start
3744 * 04 XC Transmit Continuously (ignored)
3745 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3750 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3752 write_reg(info, CHA + XBCH, val);
3753 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3754 irq_enable(info, CHA, IRQ_CTS);
3756 /* MODE:03 RAC Receiver Active, 1=active */
3757 set_reg_bits(info, CHA + MODE, BIT3);
3758 enable_auxclk(info);
3759 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3760 irq_enable(info, CHB, IRQ_CTS);
3761 /* PVR[3] 1=AUTO CTS active */
3762 set_reg_bits(info, CHA + PVR, BIT3);
3764 clear_reg_bits(info, CHA + PVR, BIT3);
3765 irq_enable(info, CHA,
3766 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3767 IRQ_ALLSENT + IRQ_TXFIFO);
3768 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3769 wait_command_complete(info, CHA);
3770 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3773 /* Set the HDLC idle mode for the transmitter.
3775 static void tx_set_idle(MGSLPC_INFO *info)
3777 /* Note: ESCC2 only supports flags and one idle modes */
3778 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3779 set_reg_bits(info, CHA + CCR1, BIT3);
3781 clear_reg_bits(info, CHA + CCR1, BIT3);
3784 /* get state of the V24 status (input) signals.
3786 static void get_signals(MGSLPC_INFO *info)
3788 unsigned char status = 0;
3790 /* preserve DTR and RTS */
3791 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3793 if (read_reg(info, CHB + VSTR) & BIT7)
3794 info->serial_signals |= SerialSignal_DCD;
3795 if (read_reg(info, CHB + STAR) & BIT1)
3796 info->serial_signals |= SerialSignal_CTS;
3798 status = read_reg(info, CHA + PVR);
3799 if (!(status & PVR_RI))
3800 info->serial_signals |= SerialSignal_RI;
3801 if (!(status & PVR_DSR))
3802 info->serial_signals |= SerialSignal_DSR;
3805 /* Set the state of DTR and RTS based on contents of
3806 * serial_signals member of device extension.
3808 static void set_signals(MGSLPC_INFO *info)
3812 val = read_reg(info, CHA + MODE);
3813 if (info->params.mode == MGSL_MODE_ASYNC) {
3814 if (info->serial_signals & SerialSignal_RTS)
3819 if (info->serial_signals & SerialSignal_RTS)
3824 write_reg(info, CHA + MODE, val);
3826 if (info->serial_signals & SerialSignal_DTR)
3827 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3829 set_reg_bits(info, CHA + PVR, PVR_DTR);
3832 static void rx_reset_buffers(MGSLPC_INFO *info)
3839 info->rx_frame_count = 0;
3840 for (i=0 ; i < info->rx_buf_count ; i++) {
3841 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3842 buf->status = buf->count = 0;
3846 /* Attempt to return a received HDLC frame
3847 * Only frames received without errors are returned.
3849 * Returns true if frame returned, otherwise false
3851 static bool rx_get_frame(MGSLPC_INFO *info)
3853 unsigned short status;
3855 unsigned int framesize = 0;
3856 unsigned long flags;
3857 struct tty_struct *tty = info->tty;
3858 bool return_frame = false;
3860 if (info->rx_frame_count == 0)
3863 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3865 status = buf->status;
3867 /* 07 VFR 1=valid frame
3868 * 06 RDO 1=data overrun
3869 * 05 CRC 1=OK, 0=error
3870 * 04 RAB 1=frame aborted
3872 if ((status & 0xf0) != 0xA0) {
3873 if (!(status & BIT7) || (status & BIT4))
3874 info->icount.rxabort++;
3875 else if (status & BIT6)
3876 info->icount.rxover++;
3877 else if (!(status & BIT5)) {
3878 info->icount.rxcrc++;
3879 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
3880 return_frame = true;
3883 #if SYNCLINK_GENERIC_HDLC
3885 info->netdev->stats.rx_errors++;
3886 info->netdev->stats.rx_frame_errors++;
3890 return_frame = true;
3893 framesize = buf->count;
3895 if (debug_level >= DEBUG_LEVEL_BH)
3896 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3897 __FILE__,__LINE__,info->device_name,status,framesize);
3899 if (debug_level >= DEBUG_LEVEL_DATA)
3900 trace_block(info, buf->data, framesize, 0);
3903 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3904 framesize+1 > info->max_frame_size) ||
3905 framesize > info->max_frame_size)
3906 info->icount.rxlong++;
3909 info->icount.rxok++;
3911 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3912 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3916 #if SYNCLINK_GENERIC_HDLC
3918 hdlcdev_rx(info, buf->data, framesize);
3921 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3925 spin_lock_irqsave(&info->lock,flags);
3926 buf->status = buf->count = 0;
3927 info->rx_frame_count--;
3929 if (info->rx_get >= info->rx_buf_count)
3931 spin_unlock_irqrestore(&info->lock,flags);
3936 static bool register_test(MGSLPC_INFO *info)
3938 static unsigned char patterns[] =
3939 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
3940 static unsigned int count = ARRAY_SIZE(patterns);
3943 unsigned long flags;
3945 spin_lock_irqsave(&info->lock,flags);
3948 for (i = 0; i < count; i++) {
3949 write_reg(info, XAD1, patterns[i]);
3950 write_reg(info, XAD2, patterns[(i + 1) % count]);
3951 if ((read_reg(info, XAD1) != patterns[i]) ||
3952 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
3958 spin_unlock_irqrestore(&info->lock,flags);
3962 static bool irq_test(MGSLPC_INFO *info)
3964 unsigned long end_time;
3965 unsigned long flags;
3967 spin_lock_irqsave(&info->lock,flags);
3970 info->testing_irq = true;
3973 info->irq_occurred = false;
3975 /* init hdlc mode */
3977 irq_enable(info, CHA, IRQ_TIMER);
3978 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
3979 issue_command(info, CHA, CMD_START_TIMER);
3981 spin_unlock_irqrestore(&info->lock,flags);
3984 while(end_time-- && !info->irq_occurred) {
3985 msleep_interruptible(10);
3988 info->testing_irq = false;
3990 spin_lock_irqsave(&info->lock,flags);
3992 spin_unlock_irqrestore(&info->lock,flags);
3994 return info->irq_occurred;
3997 static int adapter_test(MGSLPC_INFO *info)
3999 if (!register_test(info)) {
4000 info->init_error = DiagStatus_AddressFailure;
4001 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
4002 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
4006 if (!irq_test(info)) {
4007 info->init_error = DiagStatus_IrqFailure;
4008 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
4009 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
4013 if (debug_level >= DEBUG_LEVEL_INFO)
4014 printk("%s(%d):device %s passed diagnostics\n",
4015 __FILE__,__LINE__,info->device_name);
4019 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
4024 printk("%s tx data:\n",info->device_name);
4026 printk("%s rx data:\n",info->device_name);
4034 for(i=0;i<linecount;i++)
4035 printk("%02X ",(unsigned char)data[i]);
4038 for(i=0;i<linecount;i++) {
4039 if (data[i]>=040 && data[i]<=0176)
4040 printk("%c",data[i]);
4051 /* HDLC frame time out
4052 * update stats and do tx completion processing
4054 static void tx_timeout(unsigned long context)
4056 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
4057 unsigned long flags;
4059 if ( debug_level >= DEBUG_LEVEL_INFO )
4060 printk( "%s(%d):tx_timeout(%s)\n",
4061 __FILE__,__LINE__,info->device_name);
4062 if(info->tx_active &&
4063 info->params.mode == MGSL_MODE_HDLC) {
4064 info->icount.txtimeout++;
4066 spin_lock_irqsave(&info->lock,flags);
4067 info->tx_active = false;
4068 info->tx_count = info->tx_put = info->tx_get = 0;
4070 spin_unlock_irqrestore(&info->lock,flags);
4072 #if SYNCLINK_GENERIC_HDLC
4074 hdlcdev_tx_done(info);
4080 #if SYNCLINK_GENERIC_HDLC
4083 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
4084 * set encoding and frame check sequence (FCS) options
4086 * dev pointer to network device structure
4087 * encoding serial encoding setting
4088 * parity FCS setting
4090 * returns 0 if success, otherwise error code
4092 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
4093 unsigned short parity)
4095 MGSLPC_INFO *info = dev_to_port(dev);
4096 unsigned char new_encoding;
4097 unsigned short new_crctype;
4099 /* return error if TTY interface open */
4105 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
4106 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
4107 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
4108 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
4109 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
4110 default: return -EINVAL;
4115 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
4116 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
4117 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
4118 default: return -EINVAL;
4121 info->params.encoding = new_encoding;
4122 info->params.crc_type = new_crctype;
4124 /* if network interface up, reprogram hardware */
4126 mgslpc_program_hw(info);
4132 * called by generic HDLC layer to send frame
4134 * skb socket buffer containing HDLC frame
4135 * dev pointer to network device structure
4137 * returns 0 if success, otherwise error code
4139 static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
4141 MGSLPC_INFO *info = dev_to_port(dev);
4142 unsigned long flags;
4144 if (debug_level >= DEBUG_LEVEL_INFO)
4145 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
4147 /* stop sending until this frame completes */
4148 netif_stop_queue(dev);
4150 /* copy data to device buffers */
4151 skb_copy_from_linear_data(skb, info->tx_buf, skb->len);
4153 info->tx_put = info->tx_count = skb->len;
4155 /* update network statistics */
4156 dev->stats.tx_packets++;
4157 dev->stats.tx_bytes += skb->len;
4159 /* done with socket buffer, so free it */
4162 /* save start time for transmit timeout detection */
4163 dev->trans_start = jiffies;
4165 /* start hardware transmitter if necessary */
4166 spin_lock_irqsave(&info->lock,flags);
4167 if (!info->tx_active)
4169 spin_unlock_irqrestore(&info->lock,flags);
4175 * called by network layer when interface enabled
4176 * claim resources and initialize hardware
4178 * dev pointer to network device structure
4180 * returns 0 if success, otherwise error code
4182 static int hdlcdev_open(struct net_device *dev)
4184 MGSLPC_INFO *info = dev_to_port(dev);
4186 unsigned long flags;
4188 if (debug_level >= DEBUG_LEVEL_INFO)
4189 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4191 /* generic HDLC layer open processing */
4192 if ((rc = hdlc_open(dev)))
4195 /* arbitrate between network and tty opens */
4196 spin_lock_irqsave(&info->netlock, flags);
4197 if (info->count != 0 || info->netcount != 0) {
4198 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4199 spin_unlock_irqrestore(&info->netlock, flags);
4203 spin_unlock_irqrestore(&info->netlock, flags);
4205 /* claim resources and init adapter */
4206 if ((rc = startup(info)) != 0) {
4207 spin_lock_irqsave(&info->netlock, flags);
4209 spin_unlock_irqrestore(&info->netlock, flags);
4213 /* assert DTR and RTS, apply hardware settings */
4214 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4215 mgslpc_program_hw(info);
4217 /* enable network layer transmit */
4218 dev->trans_start = jiffies;
4219 netif_start_queue(dev);
4221 /* inform generic HDLC layer of current DCD status */
4222 spin_lock_irqsave(&info->lock, flags);
4224 spin_unlock_irqrestore(&info->lock, flags);
4225 if (info->serial_signals & SerialSignal_DCD)
4226 netif_carrier_on(dev);
4228 netif_carrier_off(dev);
4233 * called by network layer when interface is disabled
4234 * shutdown hardware and release resources
4236 * dev pointer to network device structure
4238 * returns 0 if success, otherwise error code
4240 static int hdlcdev_close(struct net_device *dev)
4242 MGSLPC_INFO *info = dev_to_port(dev);
4243 unsigned long flags;
4245 if (debug_level >= DEBUG_LEVEL_INFO)
4246 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4248 netif_stop_queue(dev);
4250 /* shutdown adapter and release resources */
4255 spin_lock_irqsave(&info->netlock, flags);
4257 spin_unlock_irqrestore(&info->netlock, flags);
4263 * called by network layer to process IOCTL call to network device
4265 * dev pointer to network device structure
4266 * ifr pointer to network interface request structure
4267 * cmd IOCTL command code
4269 * returns 0 if success, otherwise error code
4271 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4273 const size_t size = sizeof(sync_serial_settings);
4274 sync_serial_settings new_line;
4275 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4276 MGSLPC_INFO *info = dev_to_port(dev);
4279 if (debug_level >= DEBUG_LEVEL_INFO)
4280 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4282 /* return error if TTY interface open */
4286 if (cmd != SIOCWANDEV)
4287 return hdlc_ioctl(dev, ifr, cmd);
4289 switch(ifr->ifr_settings.type) {
4290 case IF_GET_IFACE: /* return current sync_serial_settings */
4292 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4293 if (ifr->ifr_settings.size < size) {
4294 ifr->ifr_settings.size = size; /* data size wanted */
4298 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4299 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4300 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4301 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4304 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4305 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4306 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4307 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4308 default: new_line.clock_type = CLOCK_DEFAULT;
4311 new_line.clock_rate = info->params.clock_speed;
4312 new_line.loopback = info->params.loopback ? 1:0;
4314 if (copy_to_user(line, &new_line, size))
4318 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4320 if(!capable(CAP_NET_ADMIN))
4322 if (copy_from_user(&new_line, line, size))
4325 switch (new_line.clock_type)
4327 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4328 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4329 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4330 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4331 case CLOCK_DEFAULT: flags = info->params.flags &
4332 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4333 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4334 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4335 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4336 default: return -EINVAL;
4339 if (new_line.loopback != 0 && new_line.loopback != 1)
4342 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4343 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4344 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4345 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4346 info->params.flags |= flags;
4348 info->params.loopback = new_line.loopback;
4350 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4351 info->params.clock_speed = new_line.clock_rate;
4353 info->params.clock_speed = 0;
4355 /* if network interface up, reprogram hardware */
4357 mgslpc_program_hw(info);
4361 return hdlc_ioctl(dev, ifr, cmd);
4366 * called by network layer when transmit timeout is detected
4368 * dev pointer to network device structure
4370 static void hdlcdev_tx_timeout(struct net_device *dev)
4372 MGSLPC_INFO *info = dev_to_port(dev);
4373 unsigned long flags;
4375 if (debug_level >= DEBUG_LEVEL_INFO)
4376 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4378 dev->stats.tx_errors++;
4379 dev->stats.tx_aborted_errors++;
4381 spin_lock_irqsave(&info->lock,flags);
4383 spin_unlock_irqrestore(&info->lock,flags);
4385 netif_wake_queue(dev);
4389 * called by device driver when transmit completes
4390 * reenable network layer transmit if stopped
4392 * info pointer to device instance information
4394 static void hdlcdev_tx_done(MGSLPC_INFO *info)
4396 if (netif_queue_stopped(info->netdev))
4397 netif_wake_queue(info->netdev);
4401 * called by device driver when frame received
4402 * pass frame to network layer
4404 * info pointer to device instance information
4405 * buf pointer to buffer contianing frame data
4406 * size count of data bytes in buf
4408 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4410 struct sk_buff *skb = dev_alloc_skb(size);
4411 struct net_device *dev = info->netdev;
4413 if (debug_level >= DEBUG_LEVEL_INFO)
4414 printk("hdlcdev_rx(%s)\n",dev->name);
4417 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
4418 dev->stats.rx_dropped++;
4422 memcpy(skb_put(skb, size), buf, size);
4424 skb->protocol = hdlc_type_trans(skb, dev);
4426 dev->stats.rx_packets++;
4427 dev->stats.rx_bytes += size;
4431 dev->last_rx = jiffies;
4435 * called by device driver when adding device instance
4436 * do generic HDLC initialization
4438 * info pointer to device instance information
4440 * returns 0 if success, otherwise error code
4442 static int hdlcdev_init(MGSLPC_INFO *info)
4445 struct net_device *dev;
4448 /* allocate and initialize network and HDLC layer objects */
4450 if (!(dev = alloc_hdlcdev(info))) {
4451 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4455 /* for network layer reporting purposes only */
4456 dev->base_addr = info->io_base;
4457 dev->irq = info->irq_level;
4459 /* network layer callbacks and settings */
4460 dev->do_ioctl = hdlcdev_ioctl;
4461 dev->open = hdlcdev_open;
4462 dev->stop = hdlcdev_close;
4463 dev->tx_timeout = hdlcdev_tx_timeout;
4464 dev->watchdog_timeo = 10*HZ;
4465 dev->tx_queue_len = 50;
4467 /* generic HDLC layer callbacks and settings */
4468 hdlc = dev_to_hdlc(dev);
4469 hdlc->attach = hdlcdev_attach;
4470 hdlc->xmit = hdlcdev_xmit;
4472 /* register objects with HDLC layer */
4473 if ((rc = register_hdlc_device(dev))) {
4474 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4484 * called by device driver when removing device instance
4485 * do generic HDLC cleanup
4487 * info pointer to device instance information
4489 static void hdlcdev_exit(MGSLPC_INFO *info)
4491 unregister_hdlc_device(info->netdev);
4492 free_netdev(info->netdev);
4493 info->netdev = NULL;
4496 #endif /* CONFIG_HDLC */