2  * $Id: synclink_gt.c,v 4.22 2006/01/09 20:16:06 paulkf Exp $
 
   4  * Device driver for Microgate SyncLink GT serial adapters.
 
   6  * written by Paul Fulghum for Microgate Corporation
 
   9  * Microgate and SyncLink are trademarks of Microgate Corporation
 
  11  * This code is released under the GNU General Public License (GPL)
 
  13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 
  14  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
  15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
  16  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 
  17  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
  18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
  19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
  20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 
  21  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
  22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 
  23  * OF THE POSSIBILITY OF SUCH DAMAGE.
 
  27  * DEBUG OUTPUT DEFINITIONS
 
  29  * uncomment lines below to enable specific types of debug output
 
  31  * DBGINFO   information - most verbose output
 
  32  * DBGERR    serious errors
 
  33  * DBGBH     bottom half service routine debugging
 
  34  * DBGISR    interrupt service routine debugging
 
  35  * DBGDATA   output receive and transmit data
 
  36  * DBGTBUF   output transmit DMA buffers and registers
 
  37  * DBGRBUF   output receive DMA buffers and registers
 
  40 #define DBGINFO(fmt) if (debug_level >= DEBUG_LEVEL_INFO) printk fmt
 
  41 #define DBGERR(fmt) if (debug_level >= DEBUG_LEVEL_ERROR) printk fmt
 
  42 #define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt
 
  43 #define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt
 
  44 #define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label))
 
  45 //#define DBGTBUF(info) dump_tbufs(info)
 
  46 //#define DBGRBUF(info) dump_rbufs(info)
 
  49 #include <linux/config.h>
 
  50 #include <linux/module.h>
 
  51 #include <linux/version.h>
 
  52 #include <linux/errno.h>
 
  53 #include <linux/signal.h>
 
  54 #include <linux/sched.h>
 
  55 #include <linux/timer.h>
 
  56 #include <linux/interrupt.h>
 
  57 #include <linux/pci.h>
 
  58 #include <linux/tty.h>
 
  59 #include <linux/tty_flip.h>
 
  60 #include <linux/serial.h>
 
  61 #include <linux/major.h>
 
  62 #include <linux/string.h>
 
  63 #include <linux/fcntl.h>
 
  64 #include <linux/ptrace.h>
 
  65 #include <linux/ioport.h>
 
  67 #include <linux/slab.h>
 
  68 #include <linux/netdevice.h>
 
  69 #include <linux/vmalloc.h>
 
  70 #include <linux/init.h>
 
  71 #include <linux/delay.h>
 
  72 #include <linux/ioctl.h>
 
  73 #include <linux/termios.h>
 
  74 #include <linux/bitops.h>
 
  75 #include <linux/workqueue.h>
 
  76 #include <linux/hdlc.h>
 
  78 #include <asm/system.h>
 
  82 #include <asm/types.h>
 
  83 #include <asm/uaccess.h>
 
  85 #include "linux/synclink.h"
 
  87 #ifdef CONFIG_HDLC_MODULE
 
  92  * module identification
 
  94 static char *driver_name     = "SyncLink GT";
 
  95 static char *driver_version  = "$Revision: 4.22 $";
 
  96 static char *tty_driver_name = "synclink_gt";
 
  97 static char *tty_dev_prefix  = "ttySLG";
 
  98 MODULE_LICENSE("GPL");
 
  99 #define MGSL_MAGIC 0x5401
 
 100 #define MAX_DEVICES 12
 
 102 static struct pci_device_id pci_table[] = {
 
 103         {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
 
 104         {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
 
 105         {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
 
 106         {0,}, /* terminate list */
 
 108 MODULE_DEVICE_TABLE(pci, pci_table);
 
 110 static int  init_one(struct pci_dev *dev,const struct pci_device_id *ent);
 
 111 static void remove_one(struct pci_dev *dev);
 
 112 static struct pci_driver pci_driver = {
 
 113         .name           = "synclink_gt",
 
 114         .id_table       = pci_table,
 
 116         .remove         = __devexit_p(remove_one),
 
 119 static int pci_registered;
 
 122  * module configuration and status
 
 124 static struct slgt_info *slgt_device_list;
 
 125 static int slgt_device_count;
 
 128 static int debug_level;
 
 129 static int maxframe[MAX_DEVICES];
 
 130 static int dosyncppp[MAX_DEVICES];
 
 132 module_param(ttymajor, int, 0);
 
 133 module_param(debug_level, int, 0);
 
 134 module_param_array(maxframe, int, NULL, 0);
 
 135 module_param_array(dosyncppp, int, NULL, 0);
 
 137 MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
 
 138 MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
 
 139 MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
 
 140 MODULE_PARM_DESC(dosyncppp, "Enable synchronous net device, 0=disable 1=enable");
 
 143  * tty support and callbacks
 
 145 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
 
 147 static struct tty_driver *serial_driver;
 
 149 static int  open(struct tty_struct *tty, struct file * filp);
 
 150 static void close(struct tty_struct *tty, struct file * filp);
 
 151 static void hangup(struct tty_struct *tty);
 
 152 static void set_termios(struct tty_struct *tty, struct termios *old_termios);
 
 154 static int  write(struct tty_struct *tty, const unsigned char *buf, int count);
 
 155 static void put_char(struct tty_struct *tty, unsigned char ch);
 
 156 static void send_xchar(struct tty_struct *tty, char ch);
 
 157 static void wait_until_sent(struct tty_struct *tty, int timeout);
 
 158 static int  write_room(struct tty_struct *tty);
 
 159 static void flush_chars(struct tty_struct *tty);
 
 160 static void flush_buffer(struct tty_struct *tty);
 
 161 static void tx_hold(struct tty_struct *tty);
 
 162 static void tx_release(struct tty_struct *tty);
 
 164 static int  ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
 
 165 static int  read_proc(char *page, char **start, off_t off, int count,int *eof, void *data);
 
 166 static int  chars_in_buffer(struct tty_struct *tty);
 
 167 static void throttle(struct tty_struct * tty);
 
 168 static void unthrottle(struct tty_struct * tty);
 
 169 static void set_break(struct tty_struct *tty, int break_state);
 
 172  * generic HDLC support and callbacks
 
 175 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
 
 176 static void hdlcdev_tx_done(struct slgt_info *info);
 
 177 static void hdlcdev_rx(struct slgt_info *info, char *buf, int size);
 
 178 static int  hdlcdev_init(struct slgt_info *info);
 
 179 static void hdlcdev_exit(struct slgt_info *info);
 
 184  * device specific structures, macros and functions
 
 187 #define SLGT_MAX_PORTS 4
 
 188 #define SLGT_REG_SIZE  256
 
 191  * DMA buffer descriptor and access macros
 
 195         unsigned short count;
 
 196         unsigned short status;
 
 197         unsigned int pbuf;  /* physical address of data buffer */
 
 198         unsigned int next;  /* physical address of next descriptor */
 
 200         /* driver book keeping */
 
 201         char *buf;          /* virtual  address of data buffer */
 
 202         unsigned int pdesc; /* physical address of this descriptor */
 
 203         dma_addr_t buf_dma_addr;
 
 206 #define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
 
 207 #define set_desc_next(a,b) (a).next   = cpu_to_le32((unsigned int)(b))
 
 208 #define set_desc_count(a,b)(a).count  = cpu_to_le16((unsigned short)(b))
 
 209 #define set_desc_eof(a,b)  (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
 
 210 #define desc_count(a)      (le16_to_cpu((a).count))
 
 211 #define desc_status(a)     (le16_to_cpu((a).status))
 
 212 #define desc_complete(a)   (le16_to_cpu((a).status) & BIT15)
 
 213 #define desc_eof(a)        (le16_to_cpu((a).status) & BIT2)
 
 214 #define desc_crc_error(a)  (le16_to_cpu((a).status) & BIT1)
 
 215 #define desc_abort(a)      (le16_to_cpu((a).status) & BIT0)
 
 216 #define desc_residue(a)    ((le16_to_cpu((a).status) & 0x38) >> 3)
 
 218 struct _input_signal_events {
 
 230  * device instance data structure
 
 233         void *if_ptr;           /* General purpose pointer (used by SPPP) */
 
 235         struct slgt_info *next_device;  /* device list link */
 
 240         char device_name[25];
 
 241         struct pci_dev *pdev;
 
 243         int port_count;  /* count of ports on adapter */
 
 244         int adapter_num; /* adapter instance number */
 
 245         int port_num;    /* port instance number */
 
 247         /* array of pointers to port contexts on this adapter */
 
 248         struct slgt_info *port_array[SLGT_MAX_PORTS];
 
 250         int                     count;          /* count of opens */
 
 251         int                     line;           /* tty line instance number */
 
 252         unsigned short          close_delay;
 
 253         unsigned short          closing_wait;   /* time to wait before closing */
 
 255         struct mgsl_icount      icount;
 
 257         struct tty_struct       *tty;
 
 259         int                     x_char;         /* xon/xoff character */
 
 260         int                     blocked_open;   /* # of blocked opens */
 
 261         unsigned int            read_status_mask;
 
 262         unsigned int            ignore_status_mask;
 
 264         wait_queue_head_t       open_wait;
 
 265         wait_queue_head_t       close_wait;
 
 267         wait_queue_head_t       status_event_wait_q;
 
 268         wait_queue_head_t       event_wait_q;
 
 269         struct timer_list       tx_timer;
 
 270         struct timer_list       rx_timer;
 
 272         spinlock_t lock;        /* spinlock for synchronizing with ISR */
 
 274         struct work_struct task;
 
 280         int irq_requested;      /* nonzero if IRQ requested */
 
 281         int irq_occurred;       /* for diagnostics use */
 
 283         /* device configuration */
 
 285         unsigned int bus_type;
 
 286         unsigned int irq_level;
 
 287         unsigned long irq_flags;
 
 289         unsigned char __iomem * reg_addr;  /* memory mapped registers address */
 
 291         int reg_addr_requested;
 
 293         MGSL_PARAMS params;       /* communications parameters */
 
 295         u32 max_frame_size;       /* as set by device config */
 
 297         unsigned int raw_rx_size;
 
 298         unsigned int if_mode;
 
 308         unsigned char signals;    /* serial signal states */
 
 309         int init_error;  /* initialization error */
 
 311         unsigned char *tx_buf;
 
 314         char flag_buf[MAX_ASYNC_BUFFER_SIZE];
 
 315         char char_buf[MAX_ASYNC_BUFFER_SIZE];
 
 316         BOOLEAN drop_rts_on_tx_done;
 
 317         struct  _input_signal_events    input_signal_events;
 
 319         int dcd_chkcount;       /* check counts to prevent */
 
 320         int cts_chkcount;       /* too many IRQs if a signal */
 
 321         int dsr_chkcount;       /* is floating */
 
 324         char *bufs;             /* virtual address of DMA buffer lists */
 
 325         dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
 
 327         unsigned int rbuf_count;
 
 328         struct slgt_desc *rbufs;
 
 329         unsigned int rbuf_current;
 
 330         unsigned int rbuf_index;
 
 332         unsigned int tbuf_count;
 
 333         struct slgt_desc *tbufs;
 
 334         unsigned int tbuf_current;
 
 335         unsigned int tbuf_start;
 
 337         unsigned char *tmp_rbuf;
 
 338         unsigned int tmp_rbuf_count;
 
 340         /* SPPP/Cisco HDLC device parts */
 
 346         struct net_device *netdev;
 
 351 static MGSL_PARAMS default_params = {
 
 352         .mode            = MGSL_MODE_HDLC,
 
 354         .flags           = HDLC_FLAG_UNDERRUN_ABORT15,
 
 355         .encoding        = HDLC_ENCODING_NRZI_SPACE,
 
 358         .crc_type        = HDLC_CRC_16_CCITT,
 
 359         .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
 
 360         .preamble        = HDLC_PREAMBLE_PATTERN_NONE,
 
 364         .parity          = ASYNC_PARITY_NONE
 
 369 #define BH_TRANSMIT 2
 
 371 #define IO_PIN_SHUTDOWN_LIMIT 100
 
 373 #define DMABUFSIZE 256
 
 374 #define DESC_LIST_SIZE 4096
 
 376 #define MASK_PARITY  BIT1
 
 377 #define MASK_FRAMING BIT2
 
 378 #define MASK_BREAK   BIT3
 
 379 #define MASK_OVERRUN BIT4
 
 381 #define GSR   0x00 /* global status */
 
 382 #define TDR   0x80 /* tx data */
 
 383 #define RDR   0x80 /* rx data */
 
 384 #define TCR   0x82 /* tx control */
 
 385 #define TIR   0x84 /* tx idle */
 
 386 #define TPR   0x85 /* tx preamble */
 
 387 #define RCR   0x86 /* rx control */
 
 388 #define VCR   0x88 /* V.24 control */
 
 389 #define CCR   0x89 /* clock control */
 
 390 #define BDR   0x8a /* baud divisor */
 
 391 #define SCR   0x8c /* serial control */
 
 392 #define SSR   0x8e /* serial status */
 
 393 #define RDCSR 0x90 /* rx DMA control/status */
 
 394 #define TDCSR 0x94 /* tx DMA control/status */
 
 395 #define RDDAR 0x98 /* rx DMA descriptor address */
 
 396 #define TDDAR 0x9c /* tx DMA descriptor address */
 
 399 #define RXBREAK     BIT14
 
 400 #define IRQ_TXDATA  BIT13
 
 401 #define IRQ_TXIDLE  BIT12
 
 402 #define IRQ_TXUNDER BIT11 /* HDLC */
 
 403 #define IRQ_RXDATA  BIT10
 
 404 #define IRQ_RXIDLE  BIT9  /* HDLC */
 
 405 #define IRQ_RXBREAK BIT9  /* async */
 
 406 #define IRQ_RXOVER  BIT8
 
 411 #define IRQ_ALL     0x3ff0
 
 412 #define IRQ_MASTER  BIT0
 
 414 #define slgt_irq_on(info, mask) \
 
 415         wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
 
 416 #define slgt_irq_off(info, mask) \
 
 417         wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
 
 419 static __u8  rd_reg8(struct slgt_info *info, unsigned int addr);
 
 420 static void  wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
 
 421 static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
 
 422 static void  wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
 
 423 static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
 
 424 static void  wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
 
 426 static void  msc_set_vcr(struct slgt_info *info);
 
 428 static int  startup(struct slgt_info *info);
 
 429 static int  block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
 
 430 static void shutdown(struct slgt_info *info);
 
 431 static void program_hw(struct slgt_info *info);
 
 432 static void change_params(struct slgt_info *info);
 
 434 static int  register_test(struct slgt_info *info);
 
 435 static int  irq_test(struct slgt_info *info);
 
 436 static int  loopback_test(struct slgt_info *info);
 
 437 static int  adapter_test(struct slgt_info *info);
 
 439 static void reset_adapter(struct slgt_info *info);
 
 440 static void reset_port(struct slgt_info *info);
 
 441 static void async_mode(struct slgt_info *info);
 
 442 static void hdlc_mode(struct slgt_info *info);
 
 444 static void rx_stop(struct slgt_info *info);
 
 445 static void rx_start(struct slgt_info *info);
 
 446 static void reset_rbufs(struct slgt_info *info);
 
 447 static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
 
 448 static void rdma_reset(struct slgt_info *info);
 
 449 static int  rx_get_frame(struct slgt_info *info);
 
 450 static int  rx_get_buf(struct slgt_info *info);
 
 452 static void tx_start(struct slgt_info *info);
 
 453 static void tx_stop(struct slgt_info *info);
 
 454 static void tx_set_idle(struct slgt_info *info);
 
 455 static unsigned int free_tbuf_count(struct slgt_info *info);
 
 456 static void reset_tbufs(struct slgt_info *info);
 
 457 static void tdma_reset(struct slgt_info *info);
 
 458 static void tx_load(struct slgt_info *info, const char *buf, unsigned int count);
 
 460 static void get_signals(struct slgt_info *info);
 
 461 static void set_signals(struct slgt_info *info);
 
 462 static void enable_loopback(struct slgt_info *info);
 
 463 static void set_rate(struct slgt_info *info, u32 data_rate);
 
 465 static int  bh_action(struct slgt_info *info);
 
 466 static void bh_handler(void* context);
 
 467 static void bh_transmit(struct slgt_info *info);
 
 468 static void isr_serial(struct slgt_info *info);
 
 469 static void isr_rdma(struct slgt_info *info);
 
 470 static void isr_txeom(struct slgt_info *info, unsigned short status);
 
 471 static void isr_tdma(struct slgt_info *info);
 
 472 static irqreturn_t slgt_interrupt(int irq, void *dev_id, struct pt_regs * regs);
 
 474 static int  alloc_dma_bufs(struct slgt_info *info);
 
 475 static void free_dma_bufs(struct slgt_info *info);
 
 476 static int  alloc_desc(struct slgt_info *info);
 
 477 static void free_desc(struct slgt_info *info);
 
 478 static int  alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
 
 479 static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
 
 481 static int  alloc_tmp_rbuf(struct slgt_info *info);
 
 482 static void free_tmp_rbuf(struct slgt_info *info);
 
 484 static void tx_timeout(unsigned long context);
 
 485 static void rx_timeout(unsigned long context);
 
 490 static int  get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount);
 
 491 static int  get_params(struct slgt_info *info, MGSL_PARAMS __user *params);
 
 492 static int  set_params(struct slgt_info *info, MGSL_PARAMS __user *params);
 
 493 static int  get_txidle(struct slgt_info *info, int __user *idle_mode);
 
 494 static int  set_txidle(struct slgt_info *info, int idle_mode);
 
 495 static int  tx_enable(struct slgt_info *info, int enable);
 
 496 static int  tx_abort(struct slgt_info *info);
 
 497 static int  rx_enable(struct slgt_info *info, int enable);
 
 498 static int  modem_input_wait(struct slgt_info *info,int arg);
 
 499 static int  wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr);
 
 500 static int  tiocmget(struct tty_struct *tty, struct file *file);
 
 501 static int  tiocmset(struct tty_struct *tty, struct file *file,
 
 502                      unsigned int set, unsigned int clear);
 
 503 static void set_break(struct tty_struct *tty, int break_state);
 
 504 static int  get_interface(struct slgt_info *info, int __user *if_mode);
 
 505 static int  set_interface(struct slgt_info *info, int if_mode);
 
 510 static void add_device(struct slgt_info *info);
 
 511 static void device_init(int adapter_num, struct pci_dev *pdev);
 
 512 static int  claim_resources(struct slgt_info *info);
 
 513 static void release_resources(struct slgt_info *info);
 
 532 static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
 
 536         printk("%s %s data:\n",info->device_name, label);
 
 538                 linecount = (count > 16) ? 16 : count;
 
 539                 for(i=0; i < linecount; i++)
 
 540                         printk("%02X ",(unsigned char)data[i]);
 
 543                 for(i=0;i<linecount;i++) {
 
 544                         if (data[i]>=040 && data[i]<=0176)
 
 545                                 printk("%c",data[i]);
 
 555 #define DBGDATA(info, buf, size, label)
 
 559 static void dump_tbufs(struct slgt_info *info)
 
 562         printk("tbuf_current=%d\n", info->tbuf_current);
 
 563         for (i=0 ; i < info->tbuf_count ; i++) {
 
 564                 printk("%d: count=%04X status=%04X\n",
 
 565                         i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
 
 569 #define DBGTBUF(info)
 
 573 static void dump_rbufs(struct slgt_info *info)
 
 576         printk("rbuf_current=%d\n", info->rbuf_current);
 
 577         for (i=0 ; i < info->rbuf_count ; i++) {
 
 578                 printk("%d: count=%04X status=%04X\n",
 
 579                         i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
 
 583 #define DBGRBUF(info)
 
 586 static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
 
 590                 printk("null struct slgt_info for (%s) in %s\n", devname, name);
 
 593         if (info->magic != MGSL_MAGIC) {
 
 594                 printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
 
 605  * line discipline callback wrappers
 
 607  * The wrappers maintain line discipline references
 
 608  * while calling into the line discipline.
 
 610  * ldisc_receive_buf  - pass receive data to line discipline
 
 612 static void ldisc_receive_buf(struct tty_struct *tty,
 
 613                               const __u8 *data, char *flags, int count)
 
 615         struct tty_ldisc *ld;
 
 618         ld = tty_ldisc_ref(tty);
 
 621                         ld->receive_buf(tty, data, flags, count);
 
 628 static int open(struct tty_struct *tty, struct file *filp)
 
 630         struct slgt_info *info;
 
 635         if ((line < 0) || (line >= slgt_device_count)) {
 
 636                 DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
 
 640         info = slgt_device_list;
 
 641         while(info && info->line != line)
 
 642                 info = info->next_device;
 
 643         if (sanity_check(info, tty->name, "open"))
 
 645         if (info->init_error) {
 
 646                 DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
 
 650         tty->driver_data = info;
 
 653         DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->count));
 
 655         /* If port is closing, signal caller to try again */
 
 656         if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
 
 657                 if (info->flags & ASYNC_CLOSING)
 
 658                         interruptible_sleep_on(&info->close_wait);
 
 659                 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
 
 660                         -EAGAIN : -ERESTARTSYS);
 
 664         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
 
 666         spin_lock_irqsave(&info->netlock, flags);
 
 667         if (info->netcount) {
 
 669                 spin_unlock_irqrestore(&info->netlock, flags);
 
 673         spin_unlock_irqrestore(&info->netlock, flags);
 
 675         if (info->count == 1) {
 
 676                 /* 1st open on this device, init hardware */
 
 677                 retval = startup(info);
 
 682         retval = block_til_ready(tty, filp, info);
 
 684                 DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
 
 693                         info->tty = NULL; /* tty layer will release tty struct */
 
 698         DBGINFO(("%s open rc=%d\n", info->device_name, retval));
 
 702 static void close(struct tty_struct *tty, struct file *filp)
 
 704         struct slgt_info *info = tty->driver_data;
 
 706         if (sanity_check(info, tty->name, "close"))
 
 708         DBGINFO(("%s close entry, count=%d\n", info->device_name, info->count));
 
 713         if (tty_hung_up_p(filp))
 
 716         if ((tty->count == 1) && (info->count != 1)) {
 
 718                  * tty->count is 1 and the tty structure will be freed.
 
 719                  * info->count should be one in this case.
 
 720                  * if it's not, correct it so that the port is shutdown.
 
 722                 DBGERR(("%s close: bad refcount; tty->count=1, "
 
 723                        "info->count=%d\n", info->device_name, info->count));
 
 729         /* if at least one open remaining, leave hardware active */
 
 733         info->flags |= ASYNC_CLOSING;
 
 735         /* set tty->closing to notify line discipline to
 
 736          * only process XON/XOFF characters. Only the N_TTY
 
 737          * discipline appears to use this (ppp does not).
 
 741         /* wait for transmit data to clear all layers */
 
 743         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
 
 744                 DBGINFO(("%s call tty_wait_until_sent\n", info->device_name));
 
 745                 tty_wait_until_sent(tty, info->closing_wait);
 
 748         if (info->flags & ASYNC_INITIALIZED)
 
 749                 wait_until_sent(tty, info->timeout);
 
 750         if (tty->driver->flush_buffer)
 
 751                 tty->driver->flush_buffer(tty);
 
 752         tty_ldisc_flush(tty);
 
 759         if (info->blocked_open) {
 
 760                 if (info->close_delay) {
 
 761                         msleep_interruptible(jiffies_to_msecs(info->close_delay));
 
 763                 wake_up_interruptible(&info->open_wait);
 
 766         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
 
 768         wake_up_interruptible(&info->close_wait);
 
 771         DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->count));
 
 774 static void hangup(struct tty_struct *tty)
 
 776         struct slgt_info *info = tty->driver_data;
 
 778         if (sanity_check(info, tty->name, "hangup"))
 
 780         DBGINFO(("%s hangup\n", info->device_name));
 
 786         info->flags &= ~ASYNC_NORMAL_ACTIVE;
 
 789         wake_up_interruptible(&info->open_wait);
 
 792 static void set_termios(struct tty_struct *tty, struct termios *old_termios)
 
 794         struct slgt_info *info = tty->driver_data;
 
 797         DBGINFO(("%s set_termios\n", tty->driver->name));
 
 799         /* just return if nothing has changed */
 
 800         if ((tty->termios->c_cflag == old_termios->c_cflag)
 
 801             && (RELEVANT_IFLAG(tty->termios->c_iflag)
 
 802                 == RELEVANT_IFLAG(old_termios->c_iflag)))
 
 807         /* Handle transition to B0 status */
 
 808         if (old_termios->c_cflag & CBAUD &&
 
 809             !(tty->termios->c_cflag & CBAUD)) {
 
 810                 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
 
 811                 spin_lock_irqsave(&info->lock,flags);
 
 813                 spin_unlock_irqrestore(&info->lock,flags);
 
 816         /* Handle transition away from B0 status */
 
 817         if (!(old_termios->c_cflag & CBAUD) &&
 
 818             tty->termios->c_cflag & CBAUD) {
 
 819                 info->signals |= SerialSignal_DTR;
 
 820                 if (!(tty->termios->c_cflag & CRTSCTS) ||
 
 821                     !test_bit(TTY_THROTTLED, &tty->flags)) {
 
 822                         info->signals |= SerialSignal_RTS;
 
 824                 spin_lock_irqsave(&info->lock,flags);
 
 826                 spin_unlock_irqrestore(&info->lock,flags);
 
 829         /* Handle turning off CRTSCTS */
 
 830         if (old_termios->c_cflag & CRTSCTS &&
 
 831             !(tty->termios->c_cflag & CRTSCTS)) {
 
 837 static int write(struct tty_struct *tty,
 
 838                  const unsigned char *buf, int count)
 
 841         struct slgt_info *info = tty->driver_data;
 
 844         if (sanity_check(info, tty->name, "write"))
 
 846         DBGINFO(("%s write count=%d\n", info->device_name, count));
 
 848         if (!tty || !info->tx_buf)
 
 851         if (count > info->max_frame_size) {
 
 859         if (info->params.mode == MGSL_MODE_RAW) {
 
 860                 unsigned int bufs_needed = (count/DMABUFSIZE);
 
 861                 unsigned int bufs_free = free_tbuf_count(info);
 
 862                 if (count % DMABUFSIZE)
 
 864                 if (bufs_needed > bufs_free)
 
 869                 if (info->tx_count) {
 
 870                         /* send accumulated data from send_char() calls */
 
 871                         /* as frame and wait before accepting more data. */
 
 872                         tx_load(info, info->tx_buf, info->tx_count);
 
 877         ret = info->tx_count = count;
 
 878         tx_load(info, buf, count);
 
 882         if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
 
 883                 spin_lock_irqsave(&info->lock,flags);
 
 884                 if (!info->tx_active)
 
 886                 spin_unlock_irqrestore(&info->lock,flags);
 
 890         DBGINFO(("%s write rc=%d\n", info->device_name, ret));
 
 894 static void put_char(struct tty_struct *tty, unsigned char ch)
 
 896         struct slgt_info *info = tty->driver_data;
 
 899         if (sanity_check(info, tty->name, "put_char"))
 
 901         DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
 
 902         if (!tty || !info->tx_buf)
 
 904         spin_lock_irqsave(&info->lock,flags);
 
 905         if (!info->tx_active && (info->tx_count < info->max_frame_size))
 
 906                 info->tx_buf[info->tx_count++] = ch;
 
 907         spin_unlock_irqrestore(&info->lock,flags);
 
 910 static void send_xchar(struct tty_struct *tty, char ch)
 
 912         struct slgt_info *info = tty->driver_data;
 
 915         if (sanity_check(info, tty->name, "send_xchar"))
 
 917         DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
 
 920                 spin_lock_irqsave(&info->lock,flags);
 
 921                 if (!info->tx_enabled)
 
 923                 spin_unlock_irqrestore(&info->lock,flags);
 
 927 static void wait_until_sent(struct tty_struct *tty, int timeout)
 
 929         struct slgt_info *info = tty->driver_data;
 
 930         unsigned long orig_jiffies, char_time;
 
 934         if (sanity_check(info, tty->name, "wait_until_sent"))
 
 936         DBGINFO(("%s wait_until_sent entry\n", info->device_name));
 
 937         if (!(info->flags & ASYNC_INITIALIZED))
 
 940         orig_jiffies = jiffies;
 
 942         /* Set check interval to 1/5 of estimated time to
 
 943          * send a character, and make it at least 1. The check
 
 944          * interval should also be less than the timeout.
 
 945          * Note: use tight timings here to satisfy the NIST-PCTS.
 
 948         if (info->params.data_rate) {
 
 949                 char_time = info->timeout/(32 * 5);
 
 956                 char_time = min_t(unsigned long, char_time, timeout);
 
 958         while (info->tx_active) {
 
 959                 msleep_interruptible(jiffies_to_msecs(char_time));
 
 960                 if (signal_pending(current))
 
 962                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
 
 967         DBGINFO(("%s wait_until_sent exit\n", info->device_name));
 
 970 static int write_room(struct tty_struct *tty)
 
 972         struct slgt_info *info = tty->driver_data;
 
 975         if (sanity_check(info, tty->name, "write_room"))
 
 977         ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
 
 978         DBGINFO(("%s write_room=%d\n", info->device_name, ret));
 
 982 static void flush_chars(struct tty_struct *tty)
 
 984         struct slgt_info *info = tty->driver_data;
 
 987         if (sanity_check(info, tty->name, "flush_chars"))
 
 989         DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
 
 991         if (info->tx_count <= 0 || tty->stopped ||
 
 992             tty->hw_stopped || !info->tx_buf)
 
 995         DBGINFO(("%s flush_chars start transmit\n", info->device_name));
 
 997         spin_lock_irqsave(&info->lock,flags);
 
 998         if (!info->tx_active && info->tx_count) {
 
 999                 tx_load(info, info->tx_buf,info->tx_count);
 
1002         spin_unlock_irqrestore(&info->lock,flags);
 
1005 static void flush_buffer(struct tty_struct *tty)
 
1007         struct slgt_info *info = tty->driver_data;
 
1008         unsigned long flags;
 
1010         if (sanity_check(info, tty->name, "flush_buffer"))
 
1012         DBGINFO(("%s flush_buffer\n", info->device_name));
 
1014         spin_lock_irqsave(&info->lock,flags);
 
1015         if (!info->tx_active)
 
1017         spin_unlock_irqrestore(&info->lock,flags);
 
1019         wake_up_interruptible(&tty->write_wait);
 
1024  * throttle (stop) transmitter
 
1026 static void tx_hold(struct tty_struct *tty)
 
1028         struct slgt_info *info = tty->driver_data;
 
1029         unsigned long flags;
 
1031         if (sanity_check(info, tty->name, "tx_hold"))
 
1033         DBGINFO(("%s tx_hold\n", info->device_name));
 
1034         spin_lock_irqsave(&info->lock,flags);
 
1035         if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
 
1037         spin_unlock_irqrestore(&info->lock,flags);
 
1041  * release (start) transmitter
 
1043 static void tx_release(struct tty_struct *tty)
 
1045         struct slgt_info *info = tty->driver_data;
 
1046         unsigned long flags;
 
1048         if (sanity_check(info, tty->name, "tx_release"))
 
1050         DBGINFO(("%s tx_release\n", info->device_name));
 
1051         spin_lock_irqsave(&info->lock,flags);
 
1052         if (!info->tx_active && info->tx_count) {
 
1053                 tx_load(info, info->tx_buf, info->tx_count);
 
1056         spin_unlock_irqrestore(&info->lock,flags);
 
1060  * Service an IOCTL request
 
1064  *      tty     pointer to tty instance data
 
1065  *      file    pointer to associated file object for device
 
1066  *      cmd     IOCTL command code
 
1067  *      arg     command argument/context
 
1069  * Return 0 if success, otherwise error code
 
1071 static int ioctl(struct tty_struct *tty, struct file *file,
 
1072                  unsigned int cmd, unsigned long arg)
 
1074         struct slgt_info *info = tty->driver_data;
 
1075         struct mgsl_icount cnow;        /* kernel counter temps */
 
1076         struct serial_icounter_struct __user *p_cuser;  /* user space */
 
1077         unsigned long flags;
 
1078         void __user *argp = (void __user *)arg;
 
1080         if (sanity_check(info, tty->name, "ioctl"))
 
1082         DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
 
1084         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
 
1085             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
 
1086                 if (tty->flags & (1 << TTY_IO_ERROR))
 
1091         case MGSL_IOCGPARAMS:
 
1092                 return get_params(info, argp);
 
1093         case MGSL_IOCSPARAMS:
 
1094                 return set_params(info, argp);
 
1095         case MGSL_IOCGTXIDLE:
 
1096                 return get_txidle(info, argp);
 
1097         case MGSL_IOCSTXIDLE:
 
1098                 return set_txidle(info, (int)arg);
 
1099         case MGSL_IOCTXENABLE:
 
1100                 return tx_enable(info, (int)arg);
 
1101         case MGSL_IOCRXENABLE:
 
1102                 return rx_enable(info, (int)arg);
 
1103         case MGSL_IOCTXABORT:
 
1104                 return tx_abort(info);
 
1105         case MGSL_IOCGSTATS:
 
1106                 return get_stats(info, argp);
 
1107         case MGSL_IOCWAITEVENT:
 
1108                 return wait_mgsl_event(info, argp);
 
1110                 return modem_input_wait(info,(int)arg);
 
1112                 return get_interface(info, argp);
 
1114                 return set_interface(info,(int)arg);
 
1116                 spin_lock_irqsave(&info->lock,flags);
 
1117                 cnow = info->icount;
 
1118                 spin_unlock_irqrestore(&info->lock,flags);
 
1120                 if (put_user(cnow.cts, &p_cuser->cts) ||
 
1121                     put_user(cnow.dsr, &p_cuser->dsr) ||
 
1122                     put_user(cnow.rng, &p_cuser->rng) ||
 
1123                     put_user(cnow.dcd, &p_cuser->dcd) ||
 
1124                     put_user(cnow.rx, &p_cuser->rx) ||
 
1125                     put_user(cnow.tx, &p_cuser->tx) ||
 
1126                     put_user(cnow.frame, &p_cuser->frame) ||
 
1127                     put_user(cnow.overrun, &p_cuser->overrun) ||
 
1128                     put_user(cnow.parity, &p_cuser->parity) ||
 
1129                     put_user(cnow.brk, &p_cuser->brk) ||
 
1130                     put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
 
1134                 return -ENOIOCTLCMD;
 
1142 static inline int line_info(char *buf, struct slgt_info *info)
 
1146         unsigned long flags;
 
1148         ret = sprintf(buf, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
 
1149                       info->device_name, info->phys_reg_addr,
 
1150                       info->irq_level, info->max_frame_size);
 
1152         /* output current serial signal states */
 
1153         spin_lock_irqsave(&info->lock,flags);
 
1155         spin_unlock_irqrestore(&info->lock,flags);
 
1159         if (info->signals & SerialSignal_RTS)
 
1160                 strcat(stat_buf, "|RTS");
 
1161         if (info->signals & SerialSignal_CTS)
 
1162                 strcat(stat_buf, "|CTS");
 
1163         if (info->signals & SerialSignal_DTR)
 
1164                 strcat(stat_buf, "|DTR");
 
1165         if (info->signals & SerialSignal_DSR)
 
1166                 strcat(stat_buf, "|DSR");
 
1167         if (info->signals & SerialSignal_DCD)
 
1168                 strcat(stat_buf, "|CD");
 
1169         if (info->signals & SerialSignal_RI)
 
1170                 strcat(stat_buf, "|RI");
 
1172         if (info->params.mode != MGSL_MODE_ASYNC) {
 
1173                 ret += sprintf(buf+ret, "\tHDLC txok:%d rxok:%d",
 
1174                                info->icount.txok, info->icount.rxok);
 
1175                 if (info->icount.txunder)
 
1176                         ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
 
1177                 if (info->icount.txabort)
 
1178                         ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
 
1179                 if (info->icount.rxshort)
 
1180                         ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
 
1181                 if (info->icount.rxlong)
 
1182                         ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
 
1183                 if (info->icount.rxover)
 
1184                         ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
 
1185                 if (info->icount.rxcrc)
 
1186                         ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
 
1188                 ret += sprintf(buf+ret, "\tASYNC tx:%d rx:%d",
 
1189                                info->icount.tx, info->icount.rx);
 
1190                 if (info->icount.frame)
 
1191                         ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
 
1192                 if (info->icount.parity)
 
1193                         ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
 
1194                 if (info->icount.brk)
 
1195                         ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
 
1196                 if (info->icount.overrun)
 
1197                         ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
 
1200         /* Append serial signal status to end */
 
1201         ret += sprintf(buf+ret, " %s\n", stat_buf+1);
 
1203         ret += sprintf(buf+ret, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
 
1204                        info->tx_active,info->bh_requested,info->bh_running,
 
1210 /* Called to print information about devices
 
1212 static int read_proc(char *page, char **start, off_t off, int count,
 
1213                      int *eof, void *data)
 
1217         struct slgt_info *info;
 
1219         len += sprintf(page, "synclink_gt driver:%s\n", driver_version);
 
1221         info = slgt_device_list;
 
1223                 l = line_info(page + len, info);
 
1225                 if (len+begin > off+count)
 
1227                 if (len+begin < off) {
 
1231                 info = info->next_device;
 
1236         if (off >= len+begin)
 
1238         *start = page + (off-begin);
 
1239         return ((count < begin+len-off) ? count : begin+len-off);
 
1243  * return count of bytes in transmit buffer
 
1245 static int chars_in_buffer(struct tty_struct *tty)
 
1247         struct slgt_info *info = tty->driver_data;
 
1248         if (sanity_check(info, tty->name, "chars_in_buffer"))
 
1250         DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, info->tx_count));
 
1251         return info->tx_count;
 
1255  * signal remote device to throttle send data (our receive data)
 
1257 static void throttle(struct tty_struct * tty)
 
1259         struct slgt_info *info = tty->driver_data;
 
1260         unsigned long flags;
 
1262         if (sanity_check(info, tty->name, "throttle"))
 
1264         DBGINFO(("%s throttle\n", info->device_name));
 
1266                 send_xchar(tty, STOP_CHAR(tty));
 
1267         if (tty->termios->c_cflag & CRTSCTS) {
 
1268                 spin_lock_irqsave(&info->lock,flags);
 
1269                 info->signals &= ~SerialSignal_RTS;
 
1271                 spin_unlock_irqrestore(&info->lock,flags);
 
1276  * signal remote device to stop throttling send data (our receive data)
 
1278 static void unthrottle(struct tty_struct * tty)
 
1280         struct slgt_info *info = tty->driver_data;
 
1281         unsigned long flags;
 
1283         if (sanity_check(info, tty->name, "unthrottle"))
 
1285         DBGINFO(("%s unthrottle\n", info->device_name));
 
1290                         send_xchar(tty, START_CHAR(tty));
 
1292         if (tty->termios->c_cflag & CRTSCTS) {
 
1293                 spin_lock_irqsave(&info->lock,flags);
 
1294                 info->signals |= SerialSignal_RTS;
 
1296                 spin_unlock_irqrestore(&info->lock,flags);
 
1301  * set or clear transmit break condition
 
1302  * break_state  -1=set break condition, 0=clear
 
1304 static void set_break(struct tty_struct *tty, int break_state)
 
1306         struct slgt_info *info = tty->driver_data;
 
1307         unsigned short value;
 
1308         unsigned long flags;
 
1310         if (sanity_check(info, tty->name, "set_break"))
 
1312         DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
 
1314         spin_lock_irqsave(&info->lock,flags);
 
1315         value = rd_reg16(info, TCR);
 
1316         if (break_state == -1)
 
1320         wr_reg16(info, TCR, value);
 
1321         spin_unlock_irqrestore(&info->lock,flags);
 
1327  * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
 
1328  * set encoding and frame check sequence (FCS) options
 
1330  * dev       pointer to network device structure
 
1331  * encoding  serial encoding setting
 
1332  * parity    FCS setting
 
1334  * returns 0 if success, otherwise error code
 
1336 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
 
1337                           unsigned short parity)
 
1339         struct slgt_info *info = dev_to_port(dev);
 
1340         unsigned char  new_encoding;
 
1341         unsigned short new_crctype;
 
1343         /* return error if TTY interface open */
 
1347         DBGINFO(("%s hdlcdev_attach\n", info->device_name));
 
1351         case ENCODING_NRZ:        new_encoding = HDLC_ENCODING_NRZ; break;
 
1352         case ENCODING_NRZI:       new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
 
1353         case ENCODING_FM_MARK:    new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
 
1354         case ENCODING_FM_SPACE:   new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
 
1355         case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
 
1356         default: return -EINVAL;
 
1361         case PARITY_NONE:            new_crctype = HDLC_CRC_NONE; break;
 
1362         case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
 
1363         case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
 
1364         default: return -EINVAL;
 
1367         info->params.encoding = new_encoding;
 
1368         info->params.crc_type = new_crctype;
 
1370         /* if network interface up, reprogram hardware */
 
1378  * called by generic HDLC layer to send frame
 
1380  * skb  socket buffer containing HDLC frame
 
1381  * dev  pointer to network device structure
 
1383  * returns 0 if success, otherwise error code
 
1385 static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
 
1387         struct slgt_info *info = dev_to_port(dev);
 
1388         struct net_device_stats *stats = hdlc_stats(dev);
 
1389         unsigned long flags;
 
1391         DBGINFO(("%s hdlc_xmit\n", dev->name));
 
1393         /* stop sending until this frame completes */
 
1394         netif_stop_queue(dev);
 
1396         /* copy data to device buffers */
 
1397         info->tx_count = skb->len;
 
1398         tx_load(info, skb->data, skb->len);
 
1400         /* update network statistics */
 
1401         stats->tx_packets++;
 
1402         stats->tx_bytes += skb->len;
 
1404         /* done with socket buffer, so free it */
 
1407         /* save start time for transmit timeout detection */
 
1408         dev->trans_start = jiffies;
 
1410         /* start hardware transmitter if necessary */
 
1411         spin_lock_irqsave(&info->lock,flags);
 
1412         if (!info->tx_active)
 
1414         spin_unlock_irqrestore(&info->lock,flags);
 
1420  * called by network layer when interface enabled
 
1421  * claim resources and initialize hardware
 
1423  * dev  pointer to network device structure
 
1425  * returns 0 if success, otherwise error code
 
1427 static int hdlcdev_open(struct net_device *dev)
 
1429         struct slgt_info *info = dev_to_port(dev);
 
1431         unsigned long flags;
 
1433         DBGINFO(("%s hdlcdev_open\n", dev->name));
 
1435         /* generic HDLC layer open processing */
 
1436         if ((rc = hdlc_open(dev)))
 
1439         /* arbitrate between network and tty opens */
 
1440         spin_lock_irqsave(&info->netlock, flags);
 
1441         if (info->count != 0 || info->netcount != 0) {
 
1442                 DBGINFO(("%s hdlc_open busy\n", dev->name));
 
1443                 spin_unlock_irqrestore(&info->netlock, flags);
 
1447         spin_unlock_irqrestore(&info->netlock, flags);
 
1449         /* claim resources and init adapter */
 
1450         if ((rc = startup(info)) != 0) {
 
1451                 spin_lock_irqsave(&info->netlock, flags);
 
1453                 spin_unlock_irqrestore(&info->netlock, flags);
 
1457         /* assert DTR and RTS, apply hardware settings */
 
1458         info->signals |= SerialSignal_RTS + SerialSignal_DTR;
 
1461         /* enable network layer transmit */
 
1462         dev->trans_start = jiffies;
 
1463         netif_start_queue(dev);
 
1465         /* inform generic HDLC layer of current DCD status */
 
1466         spin_lock_irqsave(&info->lock, flags);
 
1468         spin_unlock_irqrestore(&info->lock, flags);
 
1469         hdlc_set_carrier(info->signals & SerialSignal_DCD, dev);
 
1475  * called by network layer when interface is disabled
 
1476  * shutdown hardware and release resources
 
1478  * dev  pointer to network device structure
 
1480  * returns 0 if success, otherwise error code
 
1482 static int hdlcdev_close(struct net_device *dev)
 
1484         struct slgt_info *info = dev_to_port(dev);
 
1485         unsigned long flags;
 
1487         DBGINFO(("%s hdlcdev_close\n", dev->name));
 
1489         netif_stop_queue(dev);
 
1491         /* shutdown adapter and release resources */
 
1496         spin_lock_irqsave(&info->netlock, flags);
 
1498         spin_unlock_irqrestore(&info->netlock, flags);
 
1504  * called by network layer to process IOCTL call to network device
 
1506  * dev  pointer to network device structure
 
1507  * ifr  pointer to network interface request structure
 
1508  * cmd  IOCTL command code
 
1510  * returns 0 if success, otherwise error code
 
1512 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 
1514         const size_t size = sizeof(sync_serial_settings);
 
1515         sync_serial_settings new_line;
 
1516         sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
 
1517         struct slgt_info *info = dev_to_port(dev);
 
1520         DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
 
1522         /* return error if TTY interface open */
 
1526         if (cmd != SIOCWANDEV)
 
1527                 return hdlc_ioctl(dev, ifr, cmd);
 
1529         switch(ifr->ifr_settings.type) {
 
1530         case IF_GET_IFACE: /* return current sync_serial_settings */
 
1532                 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
 
1533                 if (ifr->ifr_settings.size < size) {
 
1534                         ifr->ifr_settings.size = size; /* data size wanted */
 
1538                 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
 
1539                                               HDLC_FLAG_RXC_BRG    | HDLC_FLAG_RXC_TXCPIN |
 
1540                                               HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
 
1541                                               HDLC_FLAG_TXC_BRG    | HDLC_FLAG_TXC_RXCPIN);
 
1544                 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
 
1545                 case (HDLC_FLAG_RXC_BRG    | HDLC_FLAG_TXC_BRG):    new_line.clock_type = CLOCK_INT; break;
 
1546                 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG):    new_line.clock_type = CLOCK_TXINT; break;
 
1547                 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
 
1548                 default: new_line.clock_type = CLOCK_DEFAULT;
 
1551                 new_line.clock_rate = info->params.clock_speed;
 
1552                 new_line.loopback   = info->params.loopback ? 1:0;
 
1554                 if (copy_to_user(line, &new_line, size))
 
1558         case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
 
1560                 if(!capable(CAP_NET_ADMIN))
 
1562                 if (copy_from_user(&new_line, line, size))
 
1565                 switch (new_line.clock_type)
 
1567                 case CLOCK_EXT:      flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
 
1568                 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
 
1569                 case CLOCK_INT:      flags = HDLC_FLAG_RXC_BRG    | HDLC_FLAG_TXC_BRG;    break;
 
1570                 case CLOCK_TXINT:    flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG;    break;
 
1571                 case CLOCK_DEFAULT:  flags = info->params.flags &
 
1572                                              (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
 
1573                                               HDLC_FLAG_RXC_BRG    | HDLC_FLAG_RXC_TXCPIN |
 
1574                                               HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
 
1575                                               HDLC_FLAG_TXC_BRG    | HDLC_FLAG_TXC_RXCPIN); break;
 
1576                 default: return -EINVAL;
 
1579                 if (new_line.loopback != 0 && new_line.loopback != 1)
 
1582                 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
 
1583                                         HDLC_FLAG_RXC_BRG    | HDLC_FLAG_RXC_TXCPIN |
 
1584                                         HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
 
1585                                         HDLC_FLAG_TXC_BRG    | HDLC_FLAG_TXC_RXCPIN);
 
1586                 info->params.flags |= flags;
 
1588                 info->params.loopback = new_line.loopback;
 
1590                 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
 
1591                         info->params.clock_speed = new_line.clock_rate;
 
1593                         info->params.clock_speed = 0;
 
1595                 /* if network interface up, reprogram hardware */
 
1601                 return hdlc_ioctl(dev, ifr, cmd);
 
1606  * called by network layer when transmit timeout is detected
 
1608  * dev  pointer to network device structure
 
1610 static void hdlcdev_tx_timeout(struct net_device *dev)
 
1612         struct slgt_info *info = dev_to_port(dev);
 
1613         struct net_device_stats *stats = hdlc_stats(dev);
 
1614         unsigned long flags;
 
1616         DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
 
1619         stats->tx_aborted_errors++;
 
1621         spin_lock_irqsave(&info->lock,flags);
 
1623         spin_unlock_irqrestore(&info->lock,flags);
 
1625         netif_wake_queue(dev);
 
1629  * called by device driver when transmit completes
 
1630  * reenable network layer transmit if stopped
 
1632  * info  pointer to device instance information
 
1634 static void hdlcdev_tx_done(struct slgt_info *info)
 
1636         if (netif_queue_stopped(info->netdev))
 
1637                 netif_wake_queue(info->netdev);
 
1641  * called by device driver when frame received
 
1642  * pass frame to network layer
 
1644  * info  pointer to device instance information
 
1645  * buf   pointer to buffer contianing frame data
 
1646  * size  count of data bytes in buf
 
1648 static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
 
1650         struct sk_buff *skb = dev_alloc_skb(size);
 
1651         struct net_device *dev = info->netdev;
 
1652         struct net_device_stats *stats = hdlc_stats(dev);
 
1654         DBGINFO(("%s hdlcdev_rx\n", dev->name));
 
1657                 DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
 
1658                 stats->rx_dropped++;
 
1662         memcpy(skb_put(skb, size),buf,size);
 
1664         skb->protocol = hdlc_type_trans(skb, info->netdev);
 
1666         stats->rx_packets++;
 
1667         stats->rx_bytes += size;
 
1671         info->netdev->last_rx = jiffies;
 
1675  * called by device driver when adding device instance
 
1676  * do generic HDLC initialization
 
1678  * info  pointer to device instance information
 
1680  * returns 0 if success, otherwise error code
 
1682 static int hdlcdev_init(struct slgt_info *info)
 
1685         struct net_device *dev;
 
1688         /* allocate and initialize network and HDLC layer objects */
 
1690         if (!(dev = alloc_hdlcdev(info))) {
 
1691                 printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
 
1695         /* for network layer reporting purposes only */
 
1696         dev->mem_start = info->phys_reg_addr;
 
1697         dev->mem_end   = info->phys_reg_addr + SLGT_REG_SIZE - 1;
 
1698         dev->irq       = info->irq_level;
 
1700         /* network layer callbacks and settings */
 
1701         dev->do_ioctl       = hdlcdev_ioctl;
 
1702         dev->open           = hdlcdev_open;
 
1703         dev->stop           = hdlcdev_close;
 
1704         dev->tx_timeout     = hdlcdev_tx_timeout;
 
1705         dev->watchdog_timeo = 10*HZ;
 
1706         dev->tx_queue_len   = 50;
 
1708         /* generic HDLC layer callbacks and settings */
 
1709         hdlc         = dev_to_hdlc(dev);
 
1710         hdlc->attach = hdlcdev_attach;
 
1711         hdlc->xmit   = hdlcdev_xmit;
 
1713         /* register objects with HDLC layer */
 
1714         if ((rc = register_hdlc_device(dev))) {
 
1715                 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
 
1725  * called by device driver when removing device instance
 
1726  * do generic HDLC cleanup
 
1728  * info  pointer to device instance information
 
1730 static void hdlcdev_exit(struct slgt_info *info)
 
1732         unregister_hdlc_device(info->netdev);
 
1733         free_netdev(info->netdev);
 
1734         info->netdev = NULL;
 
1737 #endif /* ifdef CONFIG_HDLC */
 
1740  * get async data from rx DMA buffers
 
1742 static void rx_async(struct slgt_info *info)
 
1744         struct tty_struct *tty = info->tty;
 
1745         struct mgsl_icount *icount = &info->icount;
 
1746         unsigned int start, end;
 
1748         unsigned char status;
 
1749         struct slgt_desc *bufs = info->rbufs;
 
1755         start = end = info->rbuf_current;
 
1757         while(desc_complete(bufs[end])) {
 
1758                 count = desc_count(bufs[end]) - info->rbuf_index;
 
1759                 p     = bufs[end].buf + info->rbuf_index;
 
1761                 DBGISR(("%s rx_async count=%d\n", info->device_name, count));
 
1762                 DBGDATA(info, p, count, "rx");
 
1764                 for(i=0 ; i < count; i+=2, p+=2) {
 
1766                                 tty_flip_buffer_push(tty);
 
1774                         if ((status = *(p+1) & (BIT9 + BIT8))) {
 
1777                                 else if (status & BIT8)
 
1779                                 /* discard char if tty control flags say so */
 
1780                                 if (status & info->ignore_status_mask)
 
1784                                 else if (status & BIT8)
 
1788                                 tty_insert_flip_char(tty, ch, stat);
 
1794                         /* receive buffer not completed */
 
1795                         info->rbuf_index += i;
 
1796                         info->rx_timer.expires = jiffies + 1;
 
1797                         add_timer(&info->rx_timer);
 
1801                 info->rbuf_index = 0;
 
1802                 free_rbufs(info, end, end);
 
1804                 if (++end == info->rbuf_count)
 
1807                 /* if entire list searched then no frame available */
 
1813                 tty_flip_buffer_push(tty);
 
1817  * return next bottom half action to perform
 
1819 static int bh_action(struct slgt_info *info)
 
1821         unsigned long flags;
 
1824         spin_lock_irqsave(&info->lock,flags);
 
1826         if (info->pending_bh & BH_RECEIVE) {
 
1827                 info->pending_bh &= ~BH_RECEIVE;
 
1829         } else if (info->pending_bh & BH_TRANSMIT) {
 
1830                 info->pending_bh &= ~BH_TRANSMIT;
 
1832         } else if (info->pending_bh & BH_STATUS) {
 
1833                 info->pending_bh &= ~BH_STATUS;
 
1836                 /* Mark BH routine as complete */
 
1837                 info->bh_running   = 0;
 
1838                 info->bh_requested = 0;
 
1842         spin_unlock_irqrestore(&info->lock,flags);
 
1848  * perform bottom half processing
 
1850 static void bh_handler(void* context)
 
1852         struct slgt_info *info = context;
 
1857         info->bh_running = 1;
 
1859         while((action = bh_action(info))) {
 
1862                         DBGBH(("%s bh receive\n", info->device_name));
 
1863                         switch(info->params.mode) {
 
1864                         case MGSL_MODE_ASYNC:
 
1867                         case MGSL_MODE_HDLC:
 
1868                                 while(rx_get_frame(info));
 
1871                                 while(rx_get_buf(info));
 
1874                         /* restart receiver if rx DMA buffers exhausted */
 
1875                         if (info->rx_restart)
 
1882                         DBGBH(("%s bh status\n", info->device_name));
 
1883                         info->ri_chkcount = 0;
 
1884                         info->dsr_chkcount = 0;
 
1885                         info->dcd_chkcount = 0;
 
1886                         info->cts_chkcount = 0;
 
1889                         DBGBH(("%s unknown action\n", info->device_name));
 
1893         DBGBH(("%s bh_handler exit\n", info->device_name));
 
1896 static void bh_transmit(struct slgt_info *info)
 
1898         struct tty_struct *tty = info->tty;
 
1900         DBGBH(("%s bh_transmit\n", info->device_name));
 
1903                 wake_up_interruptible(&tty->write_wait);
 
1907 static void dsr_change(struct slgt_info *info)
 
1910         DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
 
1911         if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
 
1912                 slgt_irq_off(info, IRQ_DSR);
 
1916         if (info->signals & SerialSignal_DSR)
 
1917                 info->input_signal_events.dsr_up++;
 
1919                 info->input_signal_events.dsr_down++;
 
1920         wake_up_interruptible(&info->status_event_wait_q);
 
1921         wake_up_interruptible(&info->event_wait_q);
 
1922         info->pending_bh |= BH_STATUS;
 
1925 static void cts_change(struct slgt_info *info)
 
1928         DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
 
1929         if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
 
1930                 slgt_irq_off(info, IRQ_CTS);
 
1934         if (info->signals & SerialSignal_CTS)
 
1935                 info->input_signal_events.cts_up++;
 
1937                 info->input_signal_events.cts_down++;
 
1938         wake_up_interruptible(&info->status_event_wait_q);
 
1939         wake_up_interruptible(&info->event_wait_q);
 
1940         info->pending_bh |= BH_STATUS;
 
1942         if (info->flags & ASYNC_CTS_FLOW) {
 
1944                         if (info->tty->hw_stopped) {
 
1945                                 if (info->signals & SerialSignal_CTS) {
 
1946                                         info->tty->hw_stopped = 0;
 
1947                                         info->pending_bh |= BH_TRANSMIT;
 
1951                                 if (!(info->signals & SerialSignal_CTS))
 
1952                                         info->tty->hw_stopped = 1;
 
1958 static void dcd_change(struct slgt_info *info)
 
1961         DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
 
1962         if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
 
1963                 slgt_irq_off(info, IRQ_DCD);
 
1967         if (info->signals & SerialSignal_DCD) {
 
1968                 info->input_signal_events.dcd_up++;
 
1970                 info->input_signal_events.dcd_down++;
 
1974                 hdlc_set_carrier(info->signals & SerialSignal_DCD, info->netdev);
 
1976         wake_up_interruptible(&info->status_event_wait_q);
 
1977         wake_up_interruptible(&info->event_wait_q);
 
1978         info->pending_bh |= BH_STATUS;
 
1980         if (info->flags & ASYNC_CHECK_CD) {
 
1981                 if (info->signals & SerialSignal_DCD)
 
1982                         wake_up_interruptible(&info->open_wait);
 
1985                                 tty_hangup(info->tty);
 
1990 static void ri_change(struct slgt_info *info)
 
1993         DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
 
1994         if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
 
1995                 slgt_irq_off(info, IRQ_RI);
 
1999         if (info->signals & SerialSignal_RI) {
 
2000                 info->input_signal_events.ri_up++;
 
2002                 info->input_signal_events.ri_down++;
 
2004         wake_up_interruptible(&info->status_event_wait_q);
 
2005         wake_up_interruptible(&info->event_wait_q);
 
2006         info->pending_bh |= BH_STATUS;
 
2009 static void isr_serial(struct slgt_info *info)
 
2011         unsigned short status = rd_reg16(info, SSR);
 
2013         DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
 
2015         wr_reg16(info, SSR, status); /* clear pending */
 
2017         info->irq_occurred = 1;
 
2019         if (info->params.mode == MGSL_MODE_ASYNC) {
 
2020                 if (status & IRQ_TXIDLE) {
 
2022                                 isr_txeom(info, status);
 
2024                 if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
 
2026                         /* process break detection if tty control allows */
 
2028                                 if (!(status & info->ignore_status_mask)) {
 
2029                                         if (info->read_status_mask & MASK_BREAK) {
 
2030                                                 tty_insert_flip_char(info->tty, 0, TTY_BREAK);
 
2031                                                 if (info->flags & ASYNC_SAK)
 
2038                 if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
 
2039                         isr_txeom(info, status);
 
2041                 if (status & IRQ_RXIDLE) {
 
2042                         if (status & RXIDLE)
 
2043                                 info->icount.rxidle++;
 
2045                                 info->icount.exithunt++;
 
2046                         wake_up_interruptible(&info->event_wait_q);
 
2049                 if (status & IRQ_RXOVER)
 
2053         if (status & IRQ_DSR)
 
2055         if (status & IRQ_CTS)
 
2057         if (status & IRQ_DCD)
 
2059         if (status & IRQ_RI)
 
2063 static void isr_rdma(struct slgt_info *info)
 
2065         unsigned int status = rd_reg32(info, RDCSR);
 
2067         DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
 
2069         /* RDCSR (rx DMA control/status)
 
2072          * 06      save status byte to DMA buffer
 
2074          * 04      eol (end of list)
 
2075          * 03      eob (end of buffer)
 
2080         wr_reg32(info, RDCSR, status);  /* clear pending */
 
2082         if (status & (BIT5 + BIT4)) {
 
2083                 DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
 
2084                 info->rx_restart = 1;
 
2086         info->pending_bh |= BH_RECEIVE;
 
2089 static void isr_tdma(struct slgt_info *info)
 
2091         unsigned int status = rd_reg32(info, TDCSR);
 
2093         DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
 
2095         /* TDCSR (tx DMA control/status)
 
2099          * 04      eol (end of list)
 
2100          * 03      eob (end of buffer)
 
2105         wr_reg32(info, TDCSR, status);  /* clear pending */
 
2107         if (status & (BIT5 + BIT4 + BIT3)) {
 
2108                 // another transmit buffer has completed
 
2109                 // run bottom half to get more send data from user
 
2110                 info->pending_bh |= BH_TRANSMIT;
 
2114 static void isr_txeom(struct slgt_info *info, unsigned short status)
 
2116         DBGISR(("%s txeom status=%04x\n", info->device_name, status));
 
2118         slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
 
2121         if (status & IRQ_TXUNDER) {
 
2122                 unsigned short val = rd_reg16(info, TCR);
 
2123                 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
 
2124                 wr_reg16(info, TCR, val); /* clear reset bit */
 
2127         if (info->tx_active) {
 
2128                 if (info->params.mode != MGSL_MODE_ASYNC) {
 
2129                         if (status & IRQ_TXUNDER)
 
2130                                 info->icount.txunder++;
 
2131                         else if (status & IRQ_TXIDLE)
 
2132                                 info->icount.txok++;
 
2135                 info->tx_active = 0;
 
2138                 del_timer(&info->tx_timer);
 
2140                 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
 
2141                         info->signals &= ~SerialSignal_RTS;
 
2142                         info->drop_rts_on_tx_done = 0;
 
2148                         hdlcdev_tx_done(info);
 
2152                         if (info->tty && (info->tty->stopped || info->tty->hw_stopped)) {
 
2156                         info->pending_bh |= BH_TRANSMIT;
 
2161 /* interrupt service routine
 
2163  *      irq     interrupt number
 
2164  *      dev_id  device ID supplied during interrupt registration
 
2165  *      regs    interrupted processor context
 
2167 static irqreturn_t slgt_interrupt(int irq, void *dev_id, struct pt_regs * regs)
 
2169         struct slgt_info *info;
 
2173         DBGISR(("slgt_interrupt irq=%d entry\n", irq));
 
2179         spin_lock(&info->lock);
 
2181         while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
 
2182                 DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
 
2183                 info->irq_occurred = 1;
 
2184                 for(i=0; i < info->port_count ; i++) {
 
2185                         if (info->port_array[i] == NULL)
 
2187                         if (gsr & (BIT8 << i))
 
2188                                 isr_serial(info->port_array[i]);
 
2189                         if (gsr & (BIT16 << (i*2)))
 
2190                                 isr_rdma(info->port_array[i]);
 
2191                         if (gsr & (BIT17 << (i*2)))
 
2192                                 isr_tdma(info->port_array[i]);
 
2196         for(i=0; i < info->port_count ; i++) {
 
2197                 struct slgt_info *port = info->port_array[i];
 
2199                 if (port && (port->count || port->netcount) &&
 
2200                     port->pending_bh && !port->bh_running &&
 
2201                     !port->bh_requested) {
 
2202                         DBGISR(("%s bh queued\n", port->device_name));
 
2203                         schedule_work(&port->task);
 
2204                         port->bh_requested = 1;
 
2208         spin_unlock(&info->lock);
 
2210         DBGISR(("slgt_interrupt irq=%d exit\n", irq));
 
2214 static int startup(struct slgt_info *info)
 
2216         DBGINFO(("%s startup\n", info->device_name));
 
2218         if (info->flags & ASYNC_INITIALIZED)
 
2221         if (!info->tx_buf) {
 
2222                 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
 
2223                 if (!info->tx_buf) {
 
2224                         DBGERR(("%s can't allocate tx buffer\n", info->device_name));
 
2229         info->pending_bh = 0;
 
2231         memset(&info->icount, 0, sizeof(info->icount));
 
2233         /* program hardware for current parameters */
 
2234         change_params(info);
 
2237                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
 
2239         info->flags |= ASYNC_INITIALIZED;
 
2245  *  called by close() and hangup() to shutdown hardware
 
2247 static void shutdown(struct slgt_info *info)
 
2249         unsigned long flags;
 
2251         if (!(info->flags & ASYNC_INITIALIZED))
 
2254         DBGINFO(("%s shutdown\n", info->device_name));
 
2256         /* clear status wait queue because status changes */
 
2257         /* can't happen after shutting down the hardware */
 
2258         wake_up_interruptible(&info->status_event_wait_q);
 
2259         wake_up_interruptible(&info->event_wait_q);
 
2261         del_timer_sync(&info->tx_timer);
 
2262         del_timer_sync(&info->rx_timer);
 
2264         kfree(info->tx_buf);
 
2265         info->tx_buf = NULL;
 
2267         spin_lock_irqsave(&info->lock,flags);
 
2272         slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
 
2274         if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
 
2275                 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
 
2279         spin_unlock_irqrestore(&info->lock,flags);
 
2282                 set_bit(TTY_IO_ERROR, &info->tty->flags);
 
2284         info->flags &= ~ASYNC_INITIALIZED;
 
2287 static void program_hw(struct slgt_info *info)
 
2289         unsigned long flags;
 
2291         spin_lock_irqsave(&info->lock,flags);
 
2296         if (info->params.mode == MGSL_MODE_HDLC ||
 
2297             info->params.mode == MGSL_MODE_RAW ||
 
2305         info->dcd_chkcount = 0;
 
2306         info->cts_chkcount = 0;
 
2307         info->ri_chkcount = 0;
 
2308         info->dsr_chkcount = 0;
 
2310         slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR);
 
2313         if (info->netcount ||
 
2314             (info->tty && info->tty->termios->c_cflag & CREAD))
 
2317         spin_unlock_irqrestore(&info->lock,flags);
 
2321  * reconfigure adapter based on new parameters
 
2323 static void change_params(struct slgt_info *info)
 
2328         if (!info->tty || !info->tty->termios)
 
2330         DBGINFO(("%s change_params\n", info->device_name));
 
2332         cflag = info->tty->termios->c_cflag;
 
2334         /* if B0 rate (hangup) specified then negate DTR and RTS */
 
2335         /* otherwise assert DTR and RTS */
 
2337                 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
 
2339                 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
 
2341         /* byte size and parity */
 
2343         switch (cflag & CSIZE) {
 
2344         case CS5: info->params.data_bits = 5; break;
 
2345         case CS6: info->params.data_bits = 6; break;
 
2346         case CS7: info->params.data_bits = 7; break;
 
2347         case CS8: info->params.data_bits = 8; break;
 
2348         default:  info->params.data_bits = 7; break;
 
2351         info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
 
2354                 info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
 
2356                 info->params.parity = ASYNC_PARITY_NONE;
 
2358         /* calculate number of jiffies to transmit a full
 
2359          * FIFO (32 bytes) at specified data rate
 
2361         bits_per_char = info->params.data_bits +
 
2362                         info->params.stop_bits + 1;
 
2364         info->params.data_rate = tty_get_baud_rate(info->tty);
 
2366         if (info->params.data_rate) {
 
2367                 info->timeout = (32*HZ*bits_per_char) /
 
2368                                 info->params.data_rate;
 
2370         info->timeout += HZ/50;         /* Add .02 seconds of slop */
 
2372         if (cflag & CRTSCTS)
 
2373                 info->flags |= ASYNC_CTS_FLOW;
 
2375                 info->flags &= ~ASYNC_CTS_FLOW;
 
2378                 info->flags &= ~ASYNC_CHECK_CD;
 
2380                 info->flags |= ASYNC_CHECK_CD;
 
2382         /* process tty input control flags */
 
2384         info->read_status_mask = IRQ_RXOVER;
 
2385         if (I_INPCK(info->tty))
 
2386                 info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
 
2387         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
 
2388                 info->read_status_mask |= MASK_BREAK;
 
2389         if (I_IGNPAR(info->tty))
 
2390                 info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
 
2391         if (I_IGNBRK(info->tty)) {
 
2392                 info->ignore_status_mask |= MASK_BREAK;
 
2393                 /* If ignoring parity and break indicators, ignore
 
2394                  * overruns too.  (For real raw support).
 
2396                 if (I_IGNPAR(info->tty))
 
2397                         info->ignore_status_mask |= MASK_OVERRUN;
 
2403 static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
 
2405         DBGINFO(("%s get_stats\n",  info->device_name));
 
2407                 memset(&info->icount, 0, sizeof(info->icount));
 
2409                 if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
 
2415 static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
 
2417         DBGINFO(("%s get_params\n", info->device_name));
 
2418         if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
 
2423 static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
 
2425         unsigned long flags;
 
2426         MGSL_PARAMS tmp_params;
 
2428         DBGINFO(("%s set_params\n", info->device_name));
 
2429         if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
 
2432         spin_lock_irqsave(&info->lock, flags);
 
2433         memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
 
2434         spin_unlock_irqrestore(&info->lock, flags);
 
2436         change_params(info);
 
2441 static int get_txidle(struct slgt_info *info, int __user *idle_mode)
 
2443         DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
 
2444         if (put_user(info->idle_mode, idle_mode))
 
2449 static int set_txidle(struct slgt_info *info, int idle_mode)
 
2451         unsigned long flags;
 
2452         DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
 
2453         spin_lock_irqsave(&info->lock,flags);
 
2454         info->idle_mode = idle_mode;
 
2456         spin_unlock_irqrestore(&info->lock,flags);
 
2460 static int tx_enable(struct slgt_info *info, int enable)
 
2462         unsigned long flags;
 
2463         DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
 
2464         spin_lock_irqsave(&info->lock,flags);
 
2466                 if (!info->tx_enabled)
 
2469                 if (info->tx_enabled)
 
2472         spin_unlock_irqrestore(&info->lock,flags);
 
2477  * abort transmit HDLC frame
 
2479 static int tx_abort(struct slgt_info *info)
 
2481         unsigned long flags;
 
2482         DBGINFO(("%s tx_abort\n", info->device_name));
 
2483         spin_lock_irqsave(&info->lock,flags);
 
2485         spin_unlock_irqrestore(&info->lock,flags);
 
2489 static int rx_enable(struct slgt_info *info, int enable)
 
2491         unsigned long flags;
 
2492         DBGINFO(("%s rx_enable(%d)\n", info->device_name, enable));
 
2493         spin_lock_irqsave(&info->lock,flags);
 
2495                 if (!info->rx_enabled)
 
2498                 if (info->rx_enabled)
 
2501         spin_unlock_irqrestore(&info->lock,flags);
 
2506  *  wait for specified event to occur
 
2508 static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
 
2510         unsigned long flags;
 
2513         struct mgsl_icount cprev, cnow;
 
2516         struct  _input_signal_events oldsigs, newsigs;
 
2517         DECLARE_WAITQUEUE(wait, current);
 
2519         if (get_user(mask, mask_ptr))
 
2522         DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
 
2524         spin_lock_irqsave(&info->lock,flags);
 
2526         /* return immediately if state matches requested events */
 
2531                 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
 
2532                   ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
 
2533                   ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
 
2534                   ((s & SerialSignal_RI)  ? MgslEvent_RiActive :MgslEvent_RiInactive) );
 
2536                 spin_unlock_irqrestore(&info->lock,flags);
 
2540         /* save current irq counts */
 
2541         cprev = info->icount;
 
2542         oldsigs = info->input_signal_events;
 
2544         /* enable hunt and idle irqs if needed */
 
2545         if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
 
2546                 unsigned short val = rd_reg16(info, SCR);
 
2547                 if (!(val & IRQ_RXIDLE))
 
2548                         wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
 
2551         set_current_state(TASK_INTERRUPTIBLE);
 
2552         add_wait_queue(&info->event_wait_q, &wait);
 
2554         spin_unlock_irqrestore(&info->lock,flags);
 
2558                 if (signal_pending(current)) {
 
2563                 /* get current irq counts */
 
2564                 spin_lock_irqsave(&info->lock,flags);
 
2565                 cnow = info->icount;
 
2566                 newsigs = info->input_signal_events;
 
2567                 set_current_state(TASK_INTERRUPTIBLE);
 
2568                 spin_unlock_irqrestore(&info->lock,flags);
 
2570                 /* if no change, wait aborted for some reason */
 
2571                 if (newsigs.dsr_up   == oldsigs.dsr_up   &&
 
2572                     newsigs.dsr_down == oldsigs.dsr_down &&
 
2573                     newsigs.dcd_up   == oldsigs.dcd_up   &&
 
2574                     newsigs.dcd_down == oldsigs.dcd_down &&
 
2575                     newsigs.cts_up   == oldsigs.cts_up   &&
 
2576                     newsigs.cts_down == oldsigs.cts_down &&
 
2577                     newsigs.ri_up    == oldsigs.ri_up    &&
 
2578                     newsigs.ri_down  == oldsigs.ri_down  &&
 
2579                     cnow.exithunt    == cprev.exithunt   &&
 
2580                     cnow.rxidle      == cprev.rxidle) {
 
2586                         ( (newsigs.dsr_up   != oldsigs.dsr_up   ? MgslEvent_DsrActive:0)   +
 
2587                           (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
 
2588                           (newsigs.dcd_up   != oldsigs.dcd_up   ? MgslEvent_DcdActive:0)   +
 
2589                           (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
 
2590                           (newsigs.cts_up   != oldsigs.cts_up   ? MgslEvent_CtsActive:0)   +
 
2591                           (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
 
2592                           (newsigs.ri_up    != oldsigs.ri_up    ? MgslEvent_RiActive:0)    +
 
2593                           (newsigs.ri_down  != oldsigs.ri_down  ? MgslEvent_RiInactive:0)  +
 
2594                           (cnow.exithunt    != cprev.exithunt   ? MgslEvent_ExitHuntMode:0) +
 
2595                           (cnow.rxidle      != cprev.rxidle     ? MgslEvent_IdleReceived:0) );
 
2603         remove_wait_queue(&info->event_wait_q, &wait);
 
2604         set_current_state(TASK_RUNNING);
 
2607         if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
 
2608                 spin_lock_irqsave(&info->lock,flags);
 
2609                 if (!waitqueue_active(&info->event_wait_q)) {
 
2610                         /* disable enable exit hunt mode/idle rcvd IRQs */
 
2612                                 (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
 
2614                 spin_unlock_irqrestore(&info->lock,flags);
 
2618                 rc = put_user(events, mask_ptr);
 
2622 static int get_interface(struct slgt_info *info, int __user *if_mode)
 
2624         DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
 
2625         if (put_user(info->if_mode, if_mode))
 
2630 static int set_interface(struct slgt_info *info, int if_mode)
 
2632         unsigned long flags;
 
2635         DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
 
2636         spin_lock_irqsave(&info->lock,flags);
 
2637         info->if_mode = if_mode;
 
2641         /* TCR (tx control) 07  1=RTS driver control */
 
2642         val = rd_reg16(info, TCR);
 
2643         if (info->if_mode & MGSL_INTERFACE_RTS_EN)
 
2647         wr_reg16(info, TCR, val);
 
2649         spin_unlock_irqrestore(&info->lock,flags);
 
2653 static int modem_input_wait(struct slgt_info *info,int arg)
 
2655         unsigned long flags;
 
2657         struct mgsl_icount cprev, cnow;
 
2658         DECLARE_WAITQUEUE(wait, current);
 
2660         /* save current irq counts */
 
2661         spin_lock_irqsave(&info->lock,flags);
 
2662         cprev = info->icount;
 
2663         add_wait_queue(&info->status_event_wait_q, &wait);
 
2664         set_current_state(TASK_INTERRUPTIBLE);
 
2665         spin_unlock_irqrestore(&info->lock,flags);
 
2669                 if (signal_pending(current)) {
 
2674                 /* get new irq counts */
 
2675                 spin_lock_irqsave(&info->lock,flags);
 
2676                 cnow = info->icount;
 
2677                 set_current_state(TASK_INTERRUPTIBLE);
 
2678                 spin_unlock_irqrestore(&info->lock,flags);
 
2680                 /* if no change, wait aborted for some reason */
 
2681                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
 
2682                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
 
2687                 /* check for change in caller specified modem input */
 
2688                 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
 
2689                     (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
 
2690                     (arg & TIOCM_CD  && cnow.dcd != cprev.dcd) ||
 
2691                     (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
 
2698         remove_wait_queue(&info->status_event_wait_q, &wait);
 
2699         set_current_state(TASK_RUNNING);
 
2704  *  return state of serial control and status signals
 
2706 static int tiocmget(struct tty_struct *tty, struct file *file)
 
2708         struct slgt_info *info = tty->driver_data;
 
2709         unsigned int result;
 
2710         unsigned long flags;
 
2712         spin_lock_irqsave(&info->lock,flags);
 
2714         spin_unlock_irqrestore(&info->lock,flags);
 
2716         result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
 
2717                 ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
 
2718                 ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
 
2719                 ((info->signals & SerialSignal_RI)  ? TIOCM_RNG:0) +
 
2720                 ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
 
2721                 ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
 
2723         DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
 
2728  * set modem control signals (DTR/RTS)
 
2730  *      cmd     signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
 
2731  *              TIOCMSET = set/clear signal values
 
2732  *      value   bit mask for command
 
2734 static int tiocmset(struct tty_struct *tty, struct file *file,
 
2735                     unsigned int set, unsigned int clear)
 
2737         struct slgt_info *info = tty->driver_data;
 
2738         unsigned long flags;
 
2740         DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
 
2742         if (set & TIOCM_RTS)
 
2743                 info->signals |= SerialSignal_RTS;
 
2744         if (set & TIOCM_DTR)
 
2745                 info->signals |= SerialSignal_DTR;
 
2746         if (clear & TIOCM_RTS)
 
2747                 info->signals &= ~SerialSignal_RTS;
 
2748         if (clear & TIOCM_DTR)
 
2749                 info->signals &= ~SerialSignal_DTR;
 
2751         spin_lock_irqsave(&info->lock,flags);
 
2753         spin_unlock_irqrestore(&info->lock,flags);
 
2758  *  block current process until the device is ready to open
 
2760 static int block_til_ready(struct tty_struct *tty, struct file *filp,
 
2761                            struct slgt_info *info)
 
2763         DECLARE_WAITQUEUE(wait, current);
 
2765         int             do_clocal = 0, extra_count = 0;
 
2766         unsigned long   flags;
 
2768         DBGINFO(("%s block_til_ready\n", tty->driver->name));
 
2770         if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
 
2771                 /* nonblock mode is set or port is not enabled */
 
2772                 info->flags |= ASYNC_NORMAL_ACTIVE;
 
2776         if (tty->termios->c_cflag & CLOCAL)
 
2779         /* Wait for carrier detect and the line to become
 
2780          * free (i.e., not in use by the callout).  While we are in
 
2781          * this loop, info->count is dropped by one, so that
 
2782          * close() knows when to free things.  We restore it upon
 
2783          * exit, either normal or abnormal.
 
2787         add_wait_queue(&info->open_wait, &wait);
 
2789         spin_lock_irqsave(&info->lock, flags);
 
2790         if (!tty_hung_up_p(filp)) {
 
2794         spin_unlock_irqrestore(&info->lock, flags);
 
2795         info->blocked_open++;
 
2798                 if ((tty->termios->c_cflag & CBAUD)) {
 
2799                         spin_lock_irqsave(&info->lock,flags);
 
2800                         info->signals |= SerialSignal_RTS + SerialSignal_DTR;
 
2802                         spin_unlock_irqrestore(&info->lock,flags);
 
2805                 set_current_state(TASK_INTERRUPTIBLE);
 
2807                 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
 
2808                         retval = (info->flags & ASYNC_HUP_NOTIFY) ?
 
2809                                         -EAGAIN : -ERESTARTSYS;
 
2813                 spin_lock_irqsave(&info->lock,flags);
 
2815                 spin_unlock_irqrestore(&info->lock,flags);
 
2817                 if (!(info->flags & ASYNC_CLOSING) &&
 
2818                     (do_clocal || (info->signals & SerialSignal_DCD)) ) {
 
2822                 if (signal_pending(current)) {
 
2823                         retval = -ERESTARTSYS;
 
2827                 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
 
2831         set_current_state(TASK_RUNNING);
 
2832         remove_wait_queue(&info->open_wait, &wait);
 
2836         info->blocked_open--;
 
2839                 info->flags |= ASYNC_NORMAL_ACTIVE;
 
2841         DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
 
2845 static int alloc_tmp_rbuf(struct slgt_info *info)
 
2847         info->tmp_rbuf = kmalloc(info->max_frame_size, GFP_KERNEL);
 
2848         if (info->tmp_rbuf == NULL)
 
2853 static void free_tmp_rbuf(struct slgt_info *info)
 
2855         kfree(info->tmp_rbuf);
 
2856         info->tmp_rbuf = NULL;
 
2860  * allocate DMA descriptor lists.
 
2862 static int alloc_desc(struct slgt_info *info)
 
2867         /* allocate memory to hold descriptor lists */
 
2868         info->bufs = pci_alloc_consistent(info->pdev, DESC_LIST_SIZE, &info->bufs_dma_addr);
 
2869         if (info->bufs == NULL)
 
2872         memset(info->bufs, 0, DESC_LIST_SIZE);
 
2874         info->rbufs = (struct slgt_desc*)info->bufs;
 
2875         info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
 
2877         pbufs = (unsigned int)info->bufs_dma_addr;
 
2880          * Build circular lists of descriptors
 
2883         for (i=0; i < info->rbuf_count; i++) {
 
2884                 /* physical address of this descriptor */
 
2885                 info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
 
2887                 /* physical address of next descriptor */
 
2888                 if (i == info->rbuf_count - 1)
 
2889                         info->rbufs[i].next = cpu_to_le32(pbufs);
 
2891                         info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
 
2892                 set_desc_count(info->rbufs[i], DMABUFSIZE);
 
2895         for (i=0; i < info->tbuf_count; i++) {
 
2896                 /* physical address of this descriptor */
 
2897                 info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
 
2899                 /* physical address of next descriptor */
 
2900                 if (i == info->tbuf_count - 1)
 
2901                         info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
 
2903                         info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
 
2909 static void free_desc(struct slgt_info *info)
 
2911         if (info->bufs != NULL) {
 
2912                 pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
 
2919 static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
 
2922         for (i=0; i < count; i++) {
 
2923                 if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
 
2925                 bufs[i].pbuf  = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
 
2930 static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
 
2933         for (i=0; i < count; i++) {
 
2934                 if (bufs[i].buf == NULL)
 
2936                 pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
 
2941 static int alloc_dma_bufs(struct slgt_info *info)
 
2943         info->rbuf_count = 32;
 
2944         info->tbuf_count = 32;
 
2946         if (alloc_desc(info) < 0 ||
 
2947             alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
 
2948             alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
 
2949             alloc_tmp_rbuf(info) < 0) {
 
2950                 DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
 
2957 static void free_dma_bufs(struct slgt_info *info)
 
2960                 free_bufs(info, info->rbufs, info->rbuf_count);
 
2961                 free_bufs(info, info->tbufs, info->tbuf_count);
 
2964         free_tmp_rbuf(info);
 
2967 static int claim_resources(struct slgt_info *info)
 
2969         if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
 
2970                 DBGERR(("%s reg addr conflict, addr=%08X\n",
 
2971                         info->device_name, info->phys_reg_addr));
 
2972                 info->init_error = DiagStatus_AddressConflict;
 
2976                 info->reg_addr_requested = 1;
 
2978         info->reg_addr = ioremap(info->phys_reg_addr, SLGT_REG_SIZE);
 
2979         if (!info->reg_addr) {
 
2980                 DBGERR(("%s cant map device registers, addr=%08X\n",
 
2981                         info->device_name, info->phys_reg_addr));
 
2982                 info->init_error = DiagStatus_CantAssignPciResources;
 
2988         release_resources(info);
 
2992 static void release_resources(struct slgt_info *info)
 
2994         if (info->irq_requested) {
 
2995                 free_irq(info->irq_level, info);
 
2996                 info->irq_requested = 0;
 
2999         if (info->reg_addr_requested) {
 
3000                 release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
 
3001                 info->reg_addr_requested = 0;
 
3004         if (info->reg_addr) {
 
3005                 iounmap(info->reg_addr);
 
3006                 info->reg_addr = NULL;
 
3010 /* Add the specified device instance data structure to the
 
3011  * global linked list of devices and increment the device count.
 
3013 static void add_device(struct slgt_info *info)
 
3017         info->next_device = NULL;
 
3018         info->line = slgt_device_count;
 
3019         sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
 
3021         if (info->line < MAX_DEVICES) {
 
3022                 if (maxframe[info->line])
 
3023                         info->max_frame_size = maxframe[info->line];
 
3024                 info->dosyncppp = dosyncppp[info->line];
 
3027         slgt_device_count++;
 
3029         if (!slgt_device_list)
 
3030                 slgt_device_list = info;
 
3032                 struct slgt_info *current_dev = slgt_device_list;
 
3033                 while(current_dev->next_device)
 
3034                         current_dev = current_dev->next_device;
 
3035                 current_dev->next_device = info;
 
3038         if (info->max_frame_size < 4096)
 
3039                 info->max_frame_size = 4096;
 
3040         else if (info->max_frame_size > 65535)
 
3041                 info->max_frame_size = 65535;
 
3043         switch(info->pdev->device) {
 
3044         case SYNCLINK_GT_DEVICE_ID:
 
3047         case SYNCLINK_GT4_DEVICE_ID:
 
3050         case SYNCLINK_AC_DEVICE_ID:
 
3052                 info->params.mode = MGSL_MODE_ASYNC;
 
3055                 devstr = "(unknown model)";
 
3057         printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
 
3058                 devstr, info->device_name, info->phys_reg_addr,
 
3059                 info->irq_level, info->max_frame_size);
 
3067  *  allocate device instance structure, return NULL on failure
 
3069 static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
 
3071         struct slgt_info *info;
 
3073         info = kmalloc(sizeof(struct slgt_info), GFP_KERNEL);
 
3076                 DBGERR(("%s device alloc failed adapter=%d port=%d\n",
 
3077                         driver_name, adapter_num, port_num));
 
3079                 memset(info, 0, sizeof(struct slgt_info));
 
3080                 info->magic = MGSL_MAGIC;
 
3081                 INIT_WORK(&info->task, bh_handler, info);
 
3082                 info->max_frame_size = 4096;
 
3083                 info->raw_rx_size = DMABUFSIZE;
 
3084                 info->close_delay = 5*HZ/10;
 
3085                 info->closing_wait = 30*HZ;
 
3086                 init_waitqueue_head(&info->open_wait);
 
3087                 init_waitqueue_head(&info->close_wait);
 
3088                 init_waitqueue_head(&info->status_event_wait_q);
 
3089                 init_waitqueue_head(&info->event_wait_q);
 
3090                 spin_lock_init(&info->netlock);
 
3091                 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
 
3092                 info->idle_mode = HDLC_TXIDLE_FLAGS;
 
3093                 info->adapter_num = adapter_num;
 
3094                 info->port_num = port_num;
 
3096                 init_timer(&info->tx_timer);
 
3097                 info->tx_timer.data = (unsigned long)info;
 
3098                 info->tx_timer.function = tx_timeout;
 
3100                 init_timer(&info->rx_timer);
 
3101                 info->rx_timer.data = (unsigned long)info;
 
3102                 info->rx_timer.function = rx_timeout;
 
3104                 /* Copy configuration info to device instance data */
 
3106                 info->irq_level = pdev->irq;
 
3107                 info->phys_reg_addr = pci_resource_start(pdev,0);
 
3109                 info->bus_type = MGSL_BUS_TYPE_PCI;
 
3110                 info->irq_flags = SA_SHIRQ;
 
3112                 info->init_error = -1; /* assume error, set to 0 on successful init */
 
3118 static void device_init(int adapter_num, struct pci_dev *pdev)
 
3120         struct slgt_info *port_array[SLGT_MAX_PORTS];
 
3124         if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
 
3127         /* allocate device instances for all ports */
 
3128         for (i=0; i < port_count; ++i) {
 
3129                 port_array[i] = alloc_dev(adapter_num, i, pdev);
 
3130                 if (port_array[i] == NULL) {
 
3131                         for (--i; i >= 0; --i)
 
3132                                 kfree(port_array[i]);
 
3137         /* give copy of port_array to all ports and add to device list  */
 
3138         for (i=0; i < port_count; ++i) {
 
3139                 memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
 
3140                 add_device(port_array[i]);
 
3141                 port_array[i]->port_count = port_count;
 
3142                 spin_lock_init(&port_array[i]->lock);
 
3145         /* Allocate and claim adapter resources */
 
3146         if (!claim_resources(port_array[0])) {
 
3148                 alloc_dma_bufs(port_array[0]);
 
3150                 /* copy resource information from first port to others */
 
3151                 for (i = 1; i < port_count; ++i) {
 
3152                         port_array[i]->lock      = port_array[0]->lock;
 
3153                         port_array[i]->irq_level = port_array[0]->irq_level;
 
3154                         port_array[i]->reg_addr  = port_array[0]->reg_addr;
 
3155                         alloc_dma_bufs(port_array[i]);
 
3158                 if (request_irq(port_array[0]->irq_level,
 
3160                                         port_array[0]->irq_flags,
 
3161                                         port_array[0]->device_name,
 
3162                                         port_array[0]) < 0) {
 
3163                         DBGERR(("%s request_irq failed IRQ=%d\n",
 
3164                                 port_array[0]->device_name,
 
3165                                 port_array[0]->irq_level));
 
3167                         port_array[0]->irq_requested = 1;
 
3168                         adapter_test(port_array[0]);
 
3169                         for (i=1 ; i < port_count ; i++)
 
3170                                 port_array[i]->init_error = port_array[0]->init_error;
 
3175 static int __devinit init_one(struct pci_dev *dev,
 
3176                               const struct pci_device_id *ent)
 
3178         if (pci_enable_device(dev)) {
 
3179                 printk("error enabling pci device %p\n", dev);
 
3182         pci_set_master(dev);
 
3183         device_init(slgt_device_count, dev);
 
3187 static void __devexit remove_one(struct pci_dev *dev)
 
3191 static struct tty_operations ops = {
 
3195         .put_char = put_char,
 
3196         .flush_chars = flush_chars,
 
3197         .write_room = write_room,
 
3198         .chars_in_buffer = chars_in_buffer,
 
3199         .flush_buffer = flush_buffer,
 
3201         .throttle = throttle,
 
3202         .unthrottle = unthrottle,
 
3203         .send_xchar = send_xchar,
 
3204         .break_ctl = set_break,
 
3205         .wait_until_sent = wait_until_sent,
 
3206         .read_proc = read_proc,
 
3207         .set_termios = set_termios,
 
3209         .start = tx_release,
 
3211         .tiocmget = tiocmget,
 
3212         .tiocmset = tiocmset,
 
3215 static void slgt_cleanup(void)
 
3218         struct slgt_info *info;
 
3219         struct slgt_info *tmp;
 
3221         printk("unload %s %s\n", driver_name, driver_version);
 
3223         if (serial_driver) {
 
3224                 if ((rc = tty_unregister_driver(serial_driver)))
 
3225                         DBGERR(("tty_unregister_driver error=%d\n", rc));
 
3226                 put_tty_driver(serial_driver);
 
3230         info = slgt_device_list;
 
3233                 info = info->next_device;
 
3236         /* release devices */
 
3237         info = slgt_device_list;
 
3242                 free_dma_bufs(info);
 
3243                 free_tmp_rbuf(info);
 
3244                 if (info->port_num == 0)
 
3245                         release_resources(info);
 
3247                 info = info->next_device;
 
3252                 pci_unregister_driver(&pci_driver);
 
3256  *  Driver initialization entry point.
 
3258 static int __init slgt_init(void)
 
3262         printk("%s %s\n", driver_name, driver_version);
 
3264         slgt_device_count = 0;
 
3265         if ((rc = pci_register_driver(&pci_driver)) < 0) {
 
3266                 printk("%s pci_register_driver error=%d\n", driver_name, rc);
 
3271         if (!slgt_device_list) {
 
3272                 printk("%s no devices found\n",driver_name);
 
3276         serial_driver = alloc_tty_driver(MAX_DEVICES);
 
3277         if (!serial_driver) {
 
3282         /* Initialize the tty_driver structure */
 
3284         serial_driver->owner = THIS_MODULE;
 
3285         serial_driver->driver_name = tty_driver_name;
 
3286         serial_driver->name = tty_dev_prefix;
 
3287         serial_driver->major = ttymajor;
 
3288         serial_driver->minor_start = 64;
 
3289         serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
 
3290         serial_driver->subtype = SERIAL_TYPE_NORMAL;
 
3291         serial_driver->init_termios = tty_std_termios;
 
3292         serial_driver->init_termios.c_cflag =
 
3293                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
 
3294         serial_driver->flags = TTY_DRIVER_REAL_RAW;
 
3295         tty_set_operations(serial_driver, &ops);
 
3296         if ((rc = tty_register_driver(serial_driver)) < 0) {
 
3297                 DBGERR(("%s can't register serial driver\n", driver_name));
 
3298                 put_tty_driver(serial_driver);
 
3299                 serial_driver = NULL;
 
3303         printk("%s %s, tty major#%d\n",
 
3304                 driver_name, driver_version,
 
3305                 serial_driver->major);
 
3314 static void __exit slgt_exit(void)
 
3319 module_init(slgt_init);
 
3320 module_exit(slgt_exit);
 
3323  * register access routines
 
3326 #define CALC_REGADDR() \
 
3327         unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
 
3329                 reg_addr += (info->port_num) * 32;
 
3331 static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
 
3334         return readb((void __iomem *)reg_addr);
 
3337 static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
 
3340         writeb(value, (void __iomem *)reg_addr);
 
3343 static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
 
3346         return readw((void __iomem *)reg_addr);
 
3349 static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
 
3352         writew(value, (void __iomem *)reg_addr);
 
3355 static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
 
3358         return readl((void __iomem *)reg_addr);
 
3361 static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
 
3364         writel(value, (void __iomem *)reg_addr);
 
3367 static void rdma_reset(struct slgt_info *info)
 
3372         wr_reg32(info, RDCSR, BIT1);
 
3374         /* wait for enable bit cleared */
 
3375         for(i=0 ; i < 1000 ; i++)
 
3376                 if (!(rd_reg32(info, RDCSR) & BIT0))
 
3380 static void tdma_reset(struct slgt_info *info)
 
3385         wr_reg32(info, TDCSR, BIT1);
 
3387         /* wait for enable bit cleared */
 
3388         for(i=0 ; i < 1000 ; i++)
 
3389                 if (!(rd_reg32(info, TDCSR) & BIT0))
 
3394  * enable internal loopback
 
3395  * TxCLK and RxCLK are generated from BRG
 
3396  * and TxD is looped back to RxD internally.
 
3398 static void enable_loopback(struct slgt_info *info)
 
3400         /* SCR (serial control) BIT2=looopback enable */
 
3401         wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
 
3403         if (info->params.mode != MGSL_MODE_ASYNC) {
 
3404                 /* CCR (clock control)
 
3405                  * 07..05  tx clock source (010 = BRG)
 
3406                  * 04..02  rx clock source (010 = BRG)
 
3407                  * 01      auxclk enable   (0 = disable)
 
3408                  * 00      BRG enable      (1 = enable)
 
3412                 wr_reg8(info, CCR, 0x49);
 
3414                 /* set speed if available, otherwise use default */
 
3415                 if (info->params.clock_speed)
 
3416                         set_rate(info, info->params.clock_speed);
 
3418                         set_rate(info, 3686400);
 
3423  *  set baud rate generator to specified rate
 
3425 static void set_rate(struct slgt_info *info, u32 rate)
 
3428         static unsigned int osc = 14745600;
 
3430         /* div = osc/rate - 1
 
3432          * Round div up if osc/rate is not integer to
 
3433          * force to next slowest rate.
 
3438                 if (!(osc % rate) && div)
 
3440                 wr_reg16(info, BDR, (unsigned short)div);
 
3444 static void rx_stop(struct slgt_info *info)
 
3448         /* disable and reset receiver */
 
3449         val = rd_reg16(info, RCR) & ~BIT1;          /* clear enable bit */
 
3450         wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
 
3451         wr_reg16(info, RCR, val);                  /* clear reset bit */
 
3453         slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
 
3455         /* clear pending rx interrupts */
 
3456         wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
 
3460         info->rx_enabled = 0;
 
3461         info->rx_restart = 0;
 
3464 static void rx_start(struct slgt_info *info)
 
3468         slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
 
3470         /* clear pending rx overrun IRQ */
 
3471         wr_reg16(info, SSR, IRQ_RXOVER);
 
3473         /* reset and disable receiver */
 
3474         val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
 
3475         wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
 
3476         wr_reg16(info, RCR, val);                  /* clear reset bit */
 
3481         /* set 1st descriptor address */
 
3482         wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
 
3484         if (info->params.mode != MGSL_MODE_ASYNC) {
 
3485                 /* enable rx DMA and DMA interrupt */
 
3486                 wr_reg32(info, RDCSR, (BIT2 + BIT0));
 
3488                 /* enable saving of rx status, rx DMA and DMA interrupt */
 
3489                 wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
 
3492         slgt_irq_on(info, IRQ_RXOVER);
 
3494         /* enable receiver */
 
3495         wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
 
3497         info->rx_restart = 0;
 
3498         info->rx_enabled = 1;
 
3501 static void tx_start(struct slgt_info *info)
 
3503         if (!info->tx_enabled) {
 
3505                         (unsigned short)(rd_reg16(info, TCR) | BIT1));
 
3506                 info->tx_enabled = TRUE;
 
3509         if (info->tx_count) {
 
3510                 info->drop_rts_on_tx_done = 0;
 
3512                 if (info->params.mode != MGSL_MODE_ASYNC) {
 
3513                         if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
 
3515                                 if (!(info->signals & SerialSignal_RTS)) {
 
3516                                         info->signals |= SerialSignal_RTS;
 
3518                                         info->drop_rts_on_tx_done = 1;
 
3522                         slgt_irq_off(info, IRQ_TXDATA);
 
3523                         slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
 
3524                         /* clear tx idle and underrun status bits */
 
3525                         wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
 
3527                         if (!(rd_reg32(info, TDCSR) & BIT0)) {
 
3528                                 /* tx DMA stopped, restart tx DMA */
 
3530                                 /* set 1st descriptor address */
 
3531                                 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
 
3532                                 if (info->params.mode == MGSL_MODE_RAW)
 
3533                                         wr_reg32(info, TDCSR, BIT2 + BIT0); /* IRQ + DMA enable */
 
3535                                         wr_reg32(info, TDCSR, BIT0); /* DMA enable */
 
3538                         if (info->params.mode != MGSL_MODE_RAW) {
 
3539                                 info->tx_timer.expires = jiffies + msecs_to_jiffies(5000);
 
3540                                 add_timer(&info->tx_timer);
 
3544                         /* set 1st descriptor address */
 
3545                         wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
 
3547                         slgt_irq_off(info, IRQ_TXDATA);
 
3548                         slgt_irq_on(info, IRQ_TXIDLE);
 
3549                         /* clear tx idle status bit */
 
3550                         wr_reg16(info, SSR, IRQ_TXIDLE);
 
3553                         wr_reg32(info, TDCSR, BIT0);
 
3556                 info->tx_active = 1;
 
3560 static void tx_stop(struct slgt_info *info)
 
3564         del_timer(&info->tx_timer);
 
3568         /* reset and disable transmitter */
 
3569         val = rd_reg16(info, TCR) & ~BIT1;          /* clear enable bit */
 
3570         wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
 
3571         wr_reg16(info, TCR, val);                  /* clear reset */
 
3573         slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
 
3575         /* clear tx idle and underrun status bit */
 
3576         wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
 
3580         info->tx_enabled = 0;
 
3581         info->tx_active  = 0;
 
3584 static void reset_port(struct slgt_info *info)
 
3586         if (!info->reg_addr)
 
3592         info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
 
3595         slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
 
3598 static void reset_adapter(struct slgt_info *info)
 
3601         for (i=0; i < info->port_count; ++i) {
 
3602                 if (info->port_array[i])
 
3603                         reset_port(info->port_array[i]);
 
3607 static void async_mode(struct slgt_info *info)
 
3611         slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
 
3617          * 15..13  mode, 010=async
 
3618          * 12..10  encoding, 000=NRZ
 
3620          * 08      1=odd parity, 0=even parity
 
3621          * 07      1=RTS driver control
 
3623          * 05..04  character length
 
3628          * 03      0=1 stop bit, 1=2 stop bits
 
3631          * 00      auto-CTS enable
 
3635         if (info->if_mode & MGSL_INTERFACE_RTS_EN)
 
3638         if (info->params.parity != ASYNC_PARITY_NONE) {
 
3640                 if (info->params.parity == ASYNC_PARITY_ODD)
 
3644         switch (info->params.data_bits)
 
3646         case 6: val |= BIT4; break;
 
3647         case 7: val |= BIT5; break;
 
3648         case 8: val |= BIT5 + BIT4; break;
 
3651         if (info->params.stop_bits != 1)
 
3654         if (info->params.flags & HDLC_FLAG_AUTO_CTS)
 
3657         wr_reg16(info, TCR, val);
 
3661          * 15..13  mode, 010=async
 
3662          * 12..10  encoding, 000=NRZ
 
3664          * 08      1=odd parity, 0=even parity
 
3665          * 07..06  reserved, must be 0
 
3666          * 05..04  character length
 
3671          * 03      reserved, must be zero
 
3674          * 00      auto-DCD enable
 
3678         if (info->params.parity != ASYNC_PARITY_NONE) {
 
3680                 if (info->params.parity == ASYNC_PARITY_ODD)
 
3684         switch (info->params.data_bits)
 
3686         case 6: val |= BIT4; break;
 
3687         case 7: val |= BIT5; break;
 
3688         case 8: val |= BIT5 + BIT4; break;
 
3691         if (info->params.flags & HDLC_FLAG_AUTO_DCD)
 
3694         wr_reg16(info, RCR, val);
 
3696         /* CCR (clock control)
 
3698          * 07..05  011 = tx clock source is BRG/16
 
3699          * 04..02  010 = rx clock source is BRG
 
3700          * 01      0 = auxclk disabled
 
3701          * 00      1 = BRG enabled
 
3705         wr_reg8(info, CCR, 0x69);
 
3711         /* SCR (serial control)
 
3713          * 15  1=tx req on FIFO half empty
 
3714          * 14  1=rx req on FIFO half full
 
3715          * 13  tx data  IRQ enable
 
3716          * 12  tx idle  IRQ enable
 
3717          * 11  rx break on IRQ enable
 
3718          * 10  rx data  IRQ enable
 
3719          * 09  rx break off IRQ enable
 
3720          * 08  overrun  IRQ enable
 
3725          * 03  reserved, must be zero
 
3726          * 02  1=txd->rxd internal loopback enable
 
3727          * 01  reserved, must be zero
 
3728          * 00  1=master IRQ enable
 
3730         val = BIT15 + BIT14 + BIT0;
 
3731         wr_reg16(info, SCR, val);
 
3733         slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
 
3735         set_rate(info, info->params.data_rate * 16);
 
3737         if (info->params.loopback)
 
3738                 enable_loopback(info);
 
3741 static void hdlc_mode(struct slgt_info *info)
 
3745         slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
 
3751          * 15..13  mode, 000=HDLC 001=raw sync
 
3755          * 07      1=RTS driver control
 
3756          * 06      preamble enable
 
3757          * 05..04  preamble length
 
3758          * 03      share open/close flag
 
3761          * 00      auto-CTS enable
 
3765         if (info->params.mode == MGSL_MODE_RAW)
 
3767         if (info->if_mode & MGSL_INTERFACE_RTS_EN)
 
3770         switch(info->params.encoding)
 
3772         case HDLC_ENCODING_NRZB:          val |= BIT10; break;
 
3773         case HDLC_ENCODING_NRZI_MARK:     val |= BIT11; break;
 
3774         case HDLC_ENCODING_NRZI:          val |= BIT11 + BIT10; break;
 
3775         case HDLC_ENCODING_BIPHASE_MARK:  val |= BIT12; break;
 
3776         case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
 
3777         case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
 
3778         case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
 
3781         switch (info->params.crc_type)
 
3783         case HDLC_CRC_16_CCITT: val |= BIT9; break;
 
3784         case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
 
3787         if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
 
3790         switch (info->params.preamble_length)
 
3792         case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
 
3793         case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
 
3794         case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
 
3797         if (info->params.flags & HDLC_FLAG_AUTO_CTS)
 
3800         wr_reg16(info, TCR, val);
 
3802         /* TPR (transmit preamble) */
 
3804         switch (info->params.preamble)
 
3806         case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
 
3807         case HDLC_PREAMBLE_PATTERN_ONES:  val = 0xff; break;
 
3808         case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
 
3809         case HDLC_PREAMBLE_PATTERN_10:    val = 0x55; break;
 
3810         case HDLC_PREAMBLE_PATTERN_01:    val = 0xaa; break;
 
3811         default:                          val = 0x7e; break;
 
3813         wr_reg8(info, TPR, (unsigned char)val);
 
3817          * 15..13  mode, 000=HDLC 001=raw sync
 
3821          * 07..03  reserved, must be 0
 
3824          * 00      auto-DCD enable
 
3828         if (info->params.mode == MGSL_MODE_RAW)
 
3831         switch(info->params.encoding)
 
3833         case HDLC_ENCODING_NRZB:          val |= BIT10; break;
 
3834         case HDLC_ENCODING_NRZI_MARK:     val |= BIT11; break;
 
3835         case HDLC_ENCODING_NRZI:          val |= BIT11 + BIT10; break;
 
3836         case HDLC_ENCODING_BIPHASE_MARK:  val |= BIT12; break;
 
3837         case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
 
3838         case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
 
3839         case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
 
3842         switch (info->params.crc_type)
 
3844         case HDLC_CRC_16_CCITT: val |= BIT9; break;
 
3845         case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
 
3848         if (info->params.flags & HDLC_FLAG_AUTO_DCD)
 
3851         wr_reg16(info, RCR, val);
 
3853         /* CCR (clock control)
 
3855          * 07..05  tx clock source
 
3856          * 04..02  rx clock source
 
3862         if (info->params.flags & HDLC_FLAG_TXC_BRG)
 
3864                 // when RxC source is DPLL, BRG generates 16X DPLL
 
3865                 // reference clock, so take TxC from BRG/16 to get
 
3866                 // transmit clock at actual data rate
 
3867                 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
 
3868                         val |= BIT6 + BIT5;     /* 011, txclk = BRG/16 */
 
3870                         val |= BIT6;    /* 010, txclk = BRG */
 
3872         else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
 
3873                 val |= BIT7;    /* 100, txclk = DPLL Input */
 
3874         else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
 
3875                 val |= BIT5;    /* 001, txclk = RXC Input */
 
3877         if (info->params.flags & HDLC_FLAG_RXC_BRG)
 
3878                 val |= BIT3;    /* 010, rxclk = BRG */
 
3879         else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
 
3880                 val |= BIT4;    /* 100, rxclk = DPLL */
 
3881         else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
 
3882                 val |= BIT2;    /* 001, rxclk = TXC Input */
 
3884         if (info->params.clock_speed)
 
3887         wr_reg8(info, CCR, (unsigned char)val);
 
3889         if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
 
3891                 // program DPLL mode
 
3892                 switch(info->params.encoding)
 
3894                 case HDLC_ENCODING_BIPHASE_MARK:
 
3895                 case HDLC_ENCODING_BIPHASE_SPACE:
 
3897                 case HDLC_ENCODING_BIPHASE_LEVEL:
 
3898                 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
 
3899                         val = BIT7 + BIT6; break;
 
3900                 default: val = BIT6;    // NRZ encodings
 
3902                 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
 
3904                 // DPLL requires a 16X reference clock from BRG
 
3905                 set_rate(info, info->params.clock_speed * 16);
 
3908                 set_rate(info, info->params.clock_speed);
 
3914         /* SCR (serial control)
 
3916          * 15  1=tx req on FIFO half empty
 
3917          * 14  1=rx req on FIFO half full
 
3918          * 13  tx data  IRQ enable
 
3919          * 12  tx idle  IRQ enable
 
3920          * 11  underrun IRQ enable
 
3921          * 10  rx data  IRQ enable
 
3922          * 09  rx idle  IRQ enable
 
3923          * 08  overrun  IRQ enable
 
3928          * 03  reserved, must be zero
 
3929          * 02  1=txd->rxd internal loopback enable
 
3930          * 01  reserved, must be zero
 
3931          * 00  1=master IRQ enable
 
3933         wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
 
3935         if (info->params.loopback)
 
3936                 enable_loopback(info);
 
3940  *  set transmit idle mode
 
3942 static void tx_set_idle(struct slgt_info *info)
 
3944         unsigned char val = 0xff;
 
3946         switch(info->idle_mode)
 
3948         case HDLC_TXIDLE_FLAGS:          val = 0x7e; break;
 
3949         case HDLC_TXIDLE_ALT_ZEROS_ONES: val = 0xaa; break;
 
3950         case HDLC_TXIDLE_ZEROS:          val = 0x00; break;
 
3951         case HDLC_TXIDLE_ONES:           val = 0xff; break;
 
3952         case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
 
3953         case HDLC_TXIDLE_SPACE:          val = 0x00; break;
 
3954         case HDLC_TXIDLE_MARK:           val = 0xff; break;
 
3957         wr_reg8(info, TIR, val);
 
3961  * get state of V24 status (input) signals
 
3963 static void get_signals(struct slgt_info *info)
 
3965         unsigned short status = rd_reg16(info, SSR);
 
3967         /* clear all serial signals except DTR and RTS */
 
3968         info->signals &= SerialSignal_DTR + SerialSignal_RTS;
 
3971                 info->signals |= SerialSignal_DSR;
 
3973                 info->signals |= SerialSignal_CTS;
 
3975                 info->signals |= SerialSignal_DCD;
 
3977                 info->signals |= SerialSignal_RI;
 
3981  * set V.24 Control Register based on current configuration
 
3983 static void msc_set_vcr(struct slgt_info *info)
 
3985         unsigned char val = 0;
 
3987         /* VCR (V.24 control)
 
3989          * 07..04  serial IF select
 
3996         switch(info->if_mode & MGSL_INTERFACE_MASK)
 
3998         case MGSL_INTERFACE_RS232:
 
3999                 val |= BIT5; /* 0010 */
 
4001         case MGSL_INTERFACE_V35:
 
4002                 val |= BIT7 + BIT6 + BIT5; /* 1110 */
 
4004         case MGSL_INTERFACE_RS422:
 
4005                 val |= BIT6; /* 0100 */
 
4009         if (info->signals & SerialSignal_DTR)
 
4011         if (info->signals & SerialSignal_RTS)
 
4013         if (info->if_mode & MGSL_INTERFACE_LL)
 
4015         if (info->if_mode & MGSL_INTERFACE_RL)
 
4017         wr_reg8(info, VCR, val);
 
4021  * set state of V24 control (output) signals
 
4023 static void set_signals(struct slgt_info *info)
 
4025         unsigned char val = rd_reg8(info, VCR);
 
4026         if (info->signals & SerialSignal_DTR)
 
4030         if (info->signals & SerialSignal_RTS)
 
4034         wr_reg8(info, VCR, val);
 
4038  * free range of receive DMA buffers (i to last)
 
4040 static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
 
4045                 /* reset current buffer for reuse */
 
4046                 info->rbufs[i].status = 0;
 
4047                 if (info->params.mode == MGSL_MODE_RAW)
 
4048                         set_desc_count(info->rbufs[i], info->raw_rx_size);
 
4050                         set_desc_count(info->rbufs[i], DMABUFSIZE);
 
4054                 if (++i == info->rbuf_count)
 
4057         info->rbuf_current = i;
 
4061  * mark all receive DMA buffers as free
 
4063 static void reset_rbufs(struct slgt_info *info)
 
4065         free_rbufs(info, 0, info->rbuf_count - 1);
 
4069  * pass receive HDLC frame to upper layer
 
4071  * return 1 if frame available, otherwise 0
 
4073 static int rx_get_frame(struct slgt_info *info)
 
4075         unsigned int start, end;
 
4076         unsigned short status;
 
4077         unsigned int framesize = 0;
 
4079         unsigned long flags;
 
4080         struct tty_struct *tty = info->tty;
 
4081         unsigned char addr_field = 0xff;
 
4087         start = end = info->rbuf_current;
 
4090                 if (!desc_complete(info->rbufs[end]))
 
4093                 if (framesize == 0 && info->params.addr_filter != 0xff)
 
4094                         addr_field = info->rbufs[end].buf[0];
 
4096                 framesize += desc_count(info->rbufs[end]);
 
4098                 if (desc_eof(info->rbufs[end]))
 
4101                 if (++end == info->rbuf_count)
 
4104                 if (end == info->rbuf_current) {
 
4105                         if (info->rx_enabled){
 
4106                                 spin_lock_irqsave(&info->lock,flags);
 
4108                                 spin_unlock_irqrestore(&info->lock,flags);
 
4116          * 15      buffer complete
 
4119          * 02      eof (end of frame)
 
4123         status = desc_status(info->rbufs[end]);
 
4125         /* ignore CRC bit if not using CRC (bit is undefined) */
 
4126         if (info->params.crc_type == HDLC_CRC_NONE)
 
4129         if (framesize == 0 ||
 
4130                  (addr_field != 0xff && addr_field != info->params.addr_filter)) {
 
4131                 free_rbufs(info, start, end);
 
4135         if (framesize < 2 || status & (BIT1+BIT0)) {
 
4136                 if (framesize < 2 || (status & BIT0))
 
4137                         info->icount.rxshort++;
 
4139                         info->icount.rxcrc++;
 
4144                         struct net_device_stats *stats = hdlc_stats(info->netdev);
 
4146                         stats->rx_frame_errors++;
 
4150                 /* adjust frame size for CRC, if any */
 
4151                 if (info->params.crc_type == HDLC_CRC_16_CCITT)
 
4153                 else if (info->params.crc_type == HDLC_CRC_32_CCITT)
 
4157         DBGBH(("%s rx frame status=%04X size=%d\n",
 
4158                 info->device_name, status, framesize));
 
4159         DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, DMABUFSIZE), "rx");
 
4162                 if (framesize > info->max_frame_size)
 
4163                         info->icount.rxlong++;
 
4165                         /* copy dma buffer(s) to contiguous temp buffer */
 
4166                         int copy_count = framesize;
 
4168                         unsigned char *p = info->tmp_rbuf;
 
4169                         info->tmp_rbuf_count = framesize;
 
4171                         info->icount.rxok++;
 
4174                                 int partial_count = min(copy_count, DMABUFSIZE);
 
4175                                 memcpy(p, info->rbufs[i].buf, partial_count);
 
4177                                 copy_count -= partial_count;
 
4178                                 if (++i == info->rbuf_count)
 
4184                                 hdlcdev_rx(info,info->tmp_rbuf, framesize);
 
4187                                 ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
 
4190         free_rbufs(info, start, end);
 
4198  * pass receive buffer (RAW synchronous mode) to tty layer
 
4199  * return 1 if buffer available, otherwise 0
 
4201 static int rx_get_buf(struct slgt_info *info)
 
4203         unsigned int i = info->rbuf_current;
 
4205         if (!desc_complete(info->rbufs[i]))
 
4207         DBGDATA(info, info->rbufs[i].buf, desc_count(info->rbufs[i]), "rx");
 
4208         DBGINFO(("rx_get_buf size=%d\n", desc_count(info->rbufs[i])));
 
4209         ldisc_receive_buf(info->tty, info->rbufs[i].buf,
 
4210                           info->flag_buf, desc_count(info->rbufs[i]));
 
4211         free_rbufs(info, i, i);
 
4215 static void reset_tbufs(struct slgt_info *info)
 
4218         info->tbuf_current = 0;
 
4219         for (i=0 ; i < info->tbuf_count ; i++) {
 
4220                 info->tbufs[i].status = 0;
 
4221                 info->tbufs[i].count  = 0;
 
4226  * return number of free transmit DMA buffers
 
4228 static unsigned int free_tbuf_count(struct slgt_info *info)
 
4230         unsigned int count = 0;
 
4231         unsigned int i = info->tbuf_current;
 
4235                 if (desc_count(info->tbufs[i]))
 
4236                         break; /* buffer in use */
 
4238                 if (++i == info->tbuf_count)
 
4240         } while (i != info->tbuf_current);
 
4242         /* last buffer with zero count may be in use, assume it is */
 
4250  * load transmit DMA buffer(s) with data
 
4252 static void tx_load(struct slgt_info *info, const char *buf, unsigned int size)
 
4254         unsigned short count;
 
4256         struct slgt_desc *d;
 
4261         DBGDATA(info, buf, size, "tx");
 
4263         info->tbuf_start = i = info->tbuf_current;
 
4266                 d = &info->tbufs[i];
 
4267                 if (++i == info->tbuf_count)
 
4270                 count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
 
4271                 memcpy(d->buf, buf, count);
 
4276                 if (!size && info->params.mode != MGSL_MODE_RAW)
 
4277                         set_desc_eof(*d, 1); /* HDLC: set EOF of last desc */
 
4279                         set_desc_eof(*d, 0);
 
4281                 set_desc_count(*d, count);
 
4284         info->tbuf_current = i;
 
4287 static int register_test(struct slgt_info *info)
 
4289         static unsigned short patterns[] =
 
4290                 {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
 
4291         static unsigned int count = sizeof(patterns)/sizeof(patterns[0]);
 
4295         for (i=0 ; i < count ; i++) {
 
4296                 wr_reg16(info, TIR, patterns[i]);
 
4297                 wr_reg16(info, BDR, patterns[(i+1)%count]);
 
4298                 if ((rd_reg16(info, TIR) != patterns[i]) ||
 
4299                     (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
 
4305         info->init_error = rc ? 0 : DiagStatus_AddressFailure;
 
4309 static int irq_test(struct slgt_info *info)
 
4311         unsigned long timeout;
 
4312         unsigned long flags;
 
4313         struct tty_struct *oldtty = info->tty;
 
4314         u32 speed = info->params.data_rate;
 
4316         info->params.data_rate = 921600;
 
4319         spin_lock_irqsave(&info->lock, flags);
 
4321         slgt_irq_on(info, IRQ_TXIDLE);
 
4323         /* enable transmitter */
 
4325                 (unsigned short)(rd_reg16(info, TCR) | BIT1));
 
4327         /* write one byte and wait for tx idle */
 
4328         wr_reg16(info, TDR, 0);
 
4330         /* assume failure */
 
4331         info->init_error = DiagStatus_IrqFailure;
 
4332         info->irq_occurred = FALSE;
 
4334         spin_unlock_irqrestore(&info->lock, flags);
 
4337         while(timeout-- && !info->irq_occurred)
 
4338                 msleep_interruptible(10);
 
4340         spin_lock_irqsave(&info->lock,flags);
 
4342         spin_unlock_irqrestore(&info->lock,flags);
 
4344         info->params.data_rate = speed;
 
4347         info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
 
4348         return info->irq_occurred ? 0 : -ENODEV;
 
4351 static int loopback_test_rx(struct slgt_info *info)
 
4353         unsigned char *src, *dest;
 
4356         if (desc_complete(info->rbufs[0])) {
 
4357                 count = desc_count(info->rbufs[0]);
 
4358                 src   = info->rbufs[0].buf;
 
4359                 dest  = info->tmp_rbuf;
 
4361                 for( ; count ; count-=2, src+=2) {
 
4362                         /* src=data byte (src+1)=status byte */
 
4363                         if (!(*(src+1) & (BIT9 + BIT8))) {
 
4366                                 info->tmp_rbuf_count++;
 
4369                 DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
 
4375 static int loopback_test(struct slgt_info *info)
 
4377 #define TESTFRAMESIZE 20
 
4379         unsigned long timeout;
 
4380         u16 count = TESTFRAMESIZE;
 
4381         unsigned char buf[TESTFRAMESIZE];
 
4383         unsigned long flags;
 
4385         struct tty_struct *oldtty = info->tty;
 
4388         memcpy(¶ms, &info->params, sizeof(params));
 
4390         info->params.mode = MGSL_MODE_ASYNC;
 
4391         info->params.data_rate = 921600;
 
4392         info->params.loopback = 1;
 
4395         /* build and send transmit frame */
 
4396         for (count = 0; count < TESTFRAMESIZE; ++count)
 
4397                 buf[count] = (unsigned char)count;
 
4399         info->tmp_rbuf_count = 0;
 
4400         memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
 
4402         /* program hardware for HDLC and enabled receiver */
 
4403         spin_lock_irqsave(&info->lock,flags);
 
4406         info->tx_count = count;
 
4407         tx_load(info, buf, count);
 
4409         spin_unlock_irqrestore(&info->lock, flags);
 
4411         /* wait for receive complete */
 
4412         for (timeout = 100; timeout; --timeout) {
 
4413                 msleep_interruptible(10);
 
4414                 if (loopback_test_rx(info)) {
 
4420         /* verify received frame length and contents */
 
4421         if (!rc && (info->tmp_rbuf_count != count ||
 
4422                   memcmp(buf, info->tmp_rbuf, count))) {
 
4426         spin_lock_irqsave(&info->lock,flags);
 
4427         reset_adapter(info);
 
4428         spin_unlock_irqrestore(&info->lock,flags);
 
4430         memcpy(&info->params, ¶ms, sizeof(info->params));
 
4433         info->init_error = rc ? DiagStatus_DmaFailure : 0;
 
4437 static int adapter_test(struct slgt_info *info)
 
4439         DBGINFO(("testing %s\n", info->device_name));
 
4440         if ((info->init_error = register_test(info)) < 0) {
 
4441                 printk("register test failure %s addr=%08X\n",
 
4442                         info->device_name, info->phys_reg_addr);
 
4443         } else if ((info->init_error = irq_test(info)) < 0) {
 
4444                 printk("IRQ test failure %s IRQ=%d\n",
 
4445                         info->device_name, info->irq_level);
 
4446         } else if ((info->init_error = loopback_test(info)) < 0) {
 
4447                 printk("loopback test failure %s\n", info->device_name);
 
4449         return info->init_error;
 
4453  * transmit timeout handler
 
4455 static void tx_timeout(unsigned long context)
 
4457         struct slgt_info *info = (struct slgt_info*)context;
 
4458         unsigned long flags;
 
4460         DBGINFO(("%s tx_timeout\n", info->device_name));
 
4461         if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
 
4462                 info->icount.txtimeout++;
 
4464         spin_lock_irqsave(&info->lock,flags);
 
4465         info->tx_active = 0;
 
4467         spin_unlock_irqrestore(&info->lock,flags);
 
4471                 hdlcdev_tx_done(info);
 
4478  * receive buffer polling timer
 
4480 static void rx_timeout(unsigned long context)
 
4482         struct slgt_info *info = (struct slgt_info*)context;
 
4483         unsigned long flags;
 
4485         DBGINFO(("%s rx_timeout\n", info->device_name));
 
4486         spin_lock_irqsave(&info->lock, flags);
 
4487         info->pending_bh |= BH_RECEIVE;
 
4488         spin_unlock_irqrestore(&info->lock, flags);