2 * $Id: synclink_gt.c,v 4.36 2006/08/28 20:47:14 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/module.h>
50 #include <linux/version.h>
51 #include <linux/errno.h>
52 #include <linux/signal.h>
53 #include <linux/sched.h>
54 #include <linux/timer.h>
55 #include <linux/interrupt.h>
56 #include <linux/pci.h>
57 #include <linux/tty.h>
58 #include <linux/tty_flip.h>
59 #include <linux/serial.h>
60 #include <linux/major.h>
61 #include <linux/string.h>
62 #include <linux/fcntl.h>
63 #include <linux/ptrace.h>
64 #include <linux/ioport.h>
66 #include <linux/slab.h>
67 #include <linux/netdevice.h>
68 #include <linux/vmalloc.h>
69 #include <linux/init.h>
70 #include <linux/delay.h>
71 #include <linux/ioctl.h>
72 #include <linux/termios.h>
73 #include <linux/bitops.h>
74 #include <linux/workqueue.h>
75 #include <linux/hdlc.h>
77 #include <asm/system.h>
81 #include <asm/types.h>
82 #include <asm/uaccess.h>
84 #include "linux/synclink.h"
86 #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_GT_MODULE))
87 #define SYNCLINK_GENERIC_HDLC 1
89 #define SYNCLINK_GENERIC_HDLC 0
93 * module identification
95 static char *driver_name = "SyncLink GT";
96 static char *driver_version = "$Revision: 4.36 $";
97 static char *tty_driver_name = "synclink_gt";
98 static char *tty_dev_prefix = "ttySLG";
99 MODULE_LICENSE("GPL");
100 #define MGSL_MAGIC 0x5401
101 #define MAX_DEVICES 32
103 static struct pci_device_id pci_table[] = {
104 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
105 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT2_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
106 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
107 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
108 {0,}, /* terminate list */
110 MODULE_DEVICE_TABLE(pci, pci_table);
112 static int init_one(struct pci_dev *dev,const struct pci_device_id *ent);
113 static void remove_one(struct pci_dev *dev);
114 static struct pci_driver pci_driver = {
115 .name = "synclink_gt",
116 .id_table = pci_table,
118 .remove = __devexit_p(remove_one),
121 static int pci_registered;
124 * module configuration and status
126 static struct slgt_info *slgt_device_list;
127 static int slgt_device_count;
130 static int debug_level;
131 static int maxframe[MAX_DEVICES];
132 static int dosyncppp[MAX_DEVICES];
134 module_param(ttymajor, int, 0);
135 module_param(debug_level, int, 0);
136 module_param_array(maxframe, int, NULL, 0);
137 module_param_array(dosyncppp, int, NULL, 0);
139 MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
140 MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
141 MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
142 MODULE_PARM_DESC(dosyncppp, "Enable synchronous net device, 0=disable 1=enable");
145 * tty support and callbacks
147 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
149 static struct tty_driver *serial_driver;
151 static int open(struct tty_struct *tty, struct file * filp);
152 static void close(struct tty_struct *tty, struct file * filp);
153 static void hangup(struct tty_struct *tty);
154 static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
156 static int write(struct tty_struct *tty, const unsigned char *buf, int count);
157 static void put_char(struct tty_struct *tty, unsigned char ch);
158 static void send_xchar(struct tty_struct *tty, char ch);
159 static void wait_until_sent(struct tty_struct *tty, int timeout);
160 static int write_room(struct tty_struct *tty);
161 static void flush_chars(struct tty_struct *tty);
162 static void flush_buffer(struct tty_struct *tty);
163 static void tx_hold(struct tty_struct *tty);
164 static void tx_release(struct tty_struct *tty);
166 static int ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
167 static int read_proc(char *page, char **start, off_t off, int count,int *eof, void *data);
168 static int chars_in_buffer(struct tty_struct *tty);
169 static void throttle(struct tty_struct * tty);
170 static void unthrottle(struct tty_struct * tty);
171 static void set_break(struct tty_struct *tty, int break_state);
174 * generic HDLC support and callbacks
176 #if SYNCLINK_GENERIC_HDLC
177 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
178 static void hdlcdev_tx_done(struct slgt_info *info);
179 static void hdlcdev_rx(struct slgt_info *info, char *buf, int size);
180 static int hdlcdev_init(struct slgt_info *info);
181 static void hdlcdev_exit(struct slgt_info *info);
186 * device specific structures, macros and functions
189 #define SLGT_MAX_PORTS 4
190 #define SLGT_REG_SIZE 256
193 * conditional wait facility
196 struct cond_wait *next;
201 static void init_cond_wait(struct cond_wait *w, unsigned int data);
202 static void add_cond_wait(struct cond_wait **head, struct cond_wait *w);
203 static void remove_cond_wait(struct cond_wait **head, struct cond_wait *w);
204 static void flush_cond_wait(struct cond_wait **head);
207 * DMA buffer descriptor and access macros
211 unsigned short count;
212 unsigned short status;
213 unsigned int pbuf; /* physical address of data buffer */
214 unsigned int next; /* physical address of next descriptor */
216 /* driver book keeping */
217 char *buf; /* virtual address of data buffer */
218 unsigned int pdesc; /* physical address of this descriptor */
219 dma_addr_t buf_dma_addr;
222 #define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
223 #define set_desc_next(a,b) (a).next = cpu_to_le32((unsigned int)(b))
224 #define set_desc_count(a,b)(a).count = cpu_to_le16((unsigned short)(b))
225 #define set_desc_eof(a,b) (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
226 #define desc_count(a) (le16_to_cpu((a).count))
227 #define desc_status(a) (le16_to_cpu((a).status))
228 #define desc_complete(a) (le16_to_cpu((a).status) & BIT15)
229 #define desc_eof(a) (le16_to_cpu((a).status) & BIT2)
230 #define desc_crc_error(a) (le16_to_cpu((a).status) & BIT1)
231 #define desc_abort(a) (le16_to_cpu((a).status) & BIT0)
232 #define desc_residue(a) ((le16_to_cpu((a).status) & 0x38) >> 3)
234 struct _input_signal_events {
246 * device instance data structure
249 void *if_ptr; /* General purpose pointer (used by SPPP) */
251 struct slgt_info *next_device; /* device list link */
256 char device_name[25];
257 struct pci_dev *pdev;
259 int port_count; /* count of ports on adapter */
260 int adapter_num; /* adapter instance number */
261 int port_num; /* port instance number */
263 /* array of pointers to port contexts on this adapter */
264 struct slgt_info *port_array[SLGT_MAX_PORTS];
266 int count; /* count of opens */
267 int line; /* tty line instance number */
268 unsigned short close_delay;
269 unsigned short closing_wait; /* time to wait before closing */
271 struct mgsl_icount icount;
273 struct tty_struct *tty;
275 int x_char; /* xon/xoff character */
276 int blocked_open; /* # of blocked opens */
277 unsigned int read_status_mask;
278 unsigned int ignore_status_mask;
280 wait_queue_head_t open_wait;
281 wait_queue_head_t close_wait;
283 wait_queue_head_t status_event_wait_q;
284 wait_queue_head_t event_wait_q;
285 struct timer_list tx_timer;
286 struct timer_list rx_timer;
288 unsigned int gpio_present;
289 struct cond_wait *gpio_wait_q;
291 spinlock_t lock; /* spinlock for synchronizing with ISR */
293 struct work_struct task;
299 int irq_requested; /* nonzero if IRQ requested */
300 int irq_occurred; /* for diagnostics use */
302 /* device configuration */
304 unsigned int bus_type;
305 unsigned int irq_level;
306 unsigned long irq_flags;
308 unsigned char __iomem * reg_addr; /* memory mapped registers address */
310 int reg_addr_requested;
312 MGSL_PARAMS params; /* communications parameters */
314 u32 max_frame_size; /* as set by device config */
316 unsigned int raw_rx_size;
317 unsigned int if_mode;
327 unsigned char signals; /* serial signal states */
328 int init_error; /* initialization error */
330 unsigned char *tx_buf;
333 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
334 char char_buf[MAX_ASYNC_BUFFER_SIZE];
335 BOOLEAN drop_rts_on_tx_done;
336 struct _input_signal_events input_signal_events;
338 int dcd_chkcount; /* check counts to prevent */
339 int cts_chkcount; /* too many IRQs if a signal */
340 int dsr_chkcount; /* is floating */
343 char *bufs; /* virtual address of DMA buffer lists */
344 dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
346 unsigned int rbuf_count;
347 struct slgt_desc *rbufs;
348 unsigned int rbuf_current;
349 unsigned int rbuf_index;
351 unsigned int tbuf_count;
352 struct slgt_desc *tbufs;
353 unsigned int tbuf_current;
354 unsigned int tbuf_start;
356 unsigned char *tmp_rbuf;
357 unsigned int tmp_rbuf_count;
359 /* SPPP/Cisco HDLC device parts */
364 #if SYNCLINK_GENERIC_HDLC
365 struct net_device *netdev;
370 static MGSL_PARAMS default_params = {
371 .mode = MGSL_MODE_HDLC,
373 .flags = HDLC_FLAG_UNDERRUN_ABORT15,
374 .encoding = HDLC_ENCODING_NRZI_SPACE,
377 .crc_type = HDLC_CRC_16_CCITT,
378 .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
379 .preamble = HDLC_PREAMBLE_PATTERN_NONE,
383 .parity = ASYNC_PARITY_NONE
388 #define BH_TRANSMIT 2
390 #define IO_PIN_SHUTDOWN_LIMIT 100
392 #define DMABUFSIZE 256
393 #define DESC_LIST_SIZE 4096
395 #define MASK_PARITY BIT1
396 #define MASK_FRAMING BIT0
397 #define MASK_BREAK BIT14
398 #define MASK_OVERRUN BIT4
400 #define GSR 0x00 /* global status */
401 #define JCR 0x04 /* JTAG control */
402 #define IODR 0x08 /* GPIO direction */
403 #define IOER 0x0c /* GPIO interrupt enable */
404 #define IOVR 0x10 /* GPIO value */
405 #define IOSR 0x14 /* GPIO interrupt status */
406 #define TDR 0x80 /* tx data */
407 #define RDR 0x80 /* rx data */
408 #define TCR 0x82 /* tx control */
409 #define TIR 0x84 /* tx idle */
410 #define TPR 0x85 /* tx preamble */
411 #define RCR 0x86 /* rx control */
412 #define VCR 0x88 /* V.24 control */
413 #define CCR 0x89 /* clock control */
414 #define BDR 0x8a /* baud divisor */
415 #define SCR 0x8c /* serial control */
416 #define SSR 0x8e /* serial status */
417 #define RDCSR 0x90 /* rx DMA control/status */
418 #define TDCSR 0x94 /* tx DMA control/status */
419 #define RDDAR 0x98 /* rx DMA descriptor address */
420 #define TDDAR 0x9c /* tx DMA descriptor address */
423 #define RXBREAK BIT14
424 #define IRQ_TXDATA BIT13
425 #define IRQ_TXIDLE BIT12
426 #define IRQ_TXUNDER BIT11 /* HDLC */
427 #define IRQ_RXDATA BIT10
428 #define IRQ_RXIDLE BIT9 /* HDLC */
429 #define IRQ_RXBREAK BIT9 /* async */
430 #define IRQ_RXOVER BIT8
435 #define IRQ_ALL 0x3ff0
436 #define IRQ_MASTER BIT0
438 #define slgt_irq_on(info, mask) \
439 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
440 #define slgt_irq_off(info, mask) \
441 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
443 static __u8 rd_reg8(struct slgt_info *info, unsigned int addr);
444 static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
445 static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
446 static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
447 static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
448 static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
450 static void msc_set_vcr(struct slgt_info *info);
452 static int startup(struct slgt_info *info);
453 static int block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
454 static void shutdown(struct slgt_info *info);
455 static void program_hw(struct slgt_info *info);
456 static void change_params(struct slgt_info *info);
458 static int register_test(struct slgt_info *info);
459 static int irq_test(struct slgt_info *info);
460 static int loopback_test(struct slgt_info *info);
461 static int adapter_test(struct slgt_info *info);
463 static void reset_adapter(struct slgt_info *info);
464 static void reset_port(struct slgt_info *info);
465 static void async_mode(struct slgt_info *info);
466 static void sync_mode(struct slgt_info *info);
468 static void rx_stop(struct slgt_info *info);
469 static void rx_start(struct slgt_info *info);
470 static void reset_rbufs(struct slgt_info *info);
471 static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
472 static void rdma_reset(struct slgt_info *info);
473 static int rx_get_frame(struct slgt_info *info);
474 static int rx_get_buf(struct slgt_info *info);
476 static void tx_start(struct slgt_info *info);
477 static void tx_stop(struct slgt_info *info);
478 static void tx_set_idle(struct slgt_info *info);
479 static unsigned int free_tbuf_count(struct slgt_info *info);
480 static void reset_tbufs(struct slgt_info *info);
481 static void tdma_reset(struct slgt_info *info);
482 static void tx_load(struct slgt_info *info, const char *buf, unsigned int count);
484 static void get_signals(struct slgt_info *info);
485 static void set_signals(struct slgt_info *info);
486 static void enable_loopback(struct slgt_info *info);
487 static void set_rate(struct slgt_info *info, u32 data_rate);
489 static int bh_action(struct slgt_info *info);
490 static void bh_handler(struct work_struct *work);
491 static void bh_transmit(struct slgt_info *info);
492 static void isr_serial(struct slgt_info *info);
493 static void isr_rdma(struct slgt_info *info);
494 static void isr_txeom(struct slgt_info *info, unsigned short status);
495 static void isr_tdma(struct slgt_info *info);
496 static irqreturn_t slgt_interrupt(int irq, void *dev_id);
498 static int alloc_dma_bufs(struct slgt_info *info);
499 static void free_dma_bufs(struct slgt_info *info);
500 static int alloc_desc(struct slgt_info *info);
501 static void free_desc(struct slgt_info *info);
502 static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
503 static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
505 static int alloc_tmp_rbuf(struct slgt_info *info);
506 static void free_tmp_rbuf(struct slgt_info *info);
508 static void tx_timeout(unsigned long context);
509 static void rx_timeout(unsigned long context);
514 static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount);
515 static int get_params(struct slgt_info *info, MGSL_PARAMS __user *params);
516 static int set_params(struct slgt_info *info, MGSL_PARAMS __user *params);
517 static int get_txidle(struct slgt_info *info, int __user *idle_mode);
518 static int set_txidle(struct slgt_info *info, int idle_mode);
519 static int tx_enable(struct slgt_info *info, int enable);
520 static int tx_abort(struct slgt_info *info);
521 static int rx_enable(struct slgt_info *info, int enable);
522 static int modem_input_wait(struct slgt_info *info,int arg);
523 static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr);
524 static int tiocmget(struct tty_struct *tty, struct file *file);
525 static int tiocmset(struct tty_struct *tty, struct file *file,
526 unsigned int set, unsigned int clear);
527 static void set_break(struct tty_struct *tty, int break_state);
528 static int get_interface(struct slgt_info *info, int __user *if_mode);
529 static int set_interface(struct slgt_info *info, int if_mode);
530 static int set_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
531 static int get_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
532 static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
537 static void add_device(struct slgt_info *info);
538 static void device_init(int adapter_num, struct pci_dev *pdev);
539 static int claim_resources(struct slgt_info *info);
540 static void release_resources(struct slgt_info *info);
559 static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
563 printk("%s %s data:\n",info->device_name, label);
565 linecount = (count > 16) ? 16 : count;
566 for(i=0; i < linecount; i++)
567 printk("%02X ",(unsigned char)data[i]);
570 for(i=0;i<linecount;i++) {
571 if (data[i]>=040 && data[i]<=0176)
572 printk("%c",data[i]);
582 #define DBGDATA(info, buf, size, label)
586 static void dump_tbufs(struct slgt_info *info)
589 printk("tbuf_current=%d\n", info->tbuf_current);
590 for (i=0 ; i < info->tbuf_count ; i++) {
591 printk("%d: count=%04X status=%04X\n",
592 i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
596 #define DBGTBUF(info)
600 static void dump_rbufs(struct slgt_info *info)
603 printk("rbuf_current=%d\n", info->rbuf_current);
604 for (i=0 ; i < info->rbuf_count ; i++) {
605 printk("%d: count=%04X status=%04X\n",
606 i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
610 #define DBGRBUF(info)
613 static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
617 printk("null struct slgt_info for (%s) in %s\n", devname, name);
620 if (info->magic != MGSL_MAGIC) {
621 printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
632 * line discipline callback wrappers
634 * The wrappers maintain line discipline references
635 * while calling into the line discipline.
637 * ldisc_receive_buf - pass receive data to line discipline
639 static void ldisc_receive_buf(struct tty_struct *tty,
640 const __u8 *data, char *flags, int count)
642 struct tty_ldisc *ld;
645 ld = tty_ldisc_ref(tty);
648 ld->receive_buf(tty, data, flags, count);
655 static int open(struct tty_struct *tty, struct file *filp)
657 struct slgt_info *info;
662 if ((line < 0) || (line >= slgt_device_count)) {
663 DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
667 info = slgt_device_list;
668 while(info && info->line != line)
669 info = info->next_device;
670 if (sanity_check(info, tty->name, "open"))
672 if (info->init_error) {
673 DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
677 tty->driver_data = info;
680 DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->count));
682 /* If port is closing, signal caller to try again */
683 if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
684 if (info->flags & ASYNC_CLOSING)
685 interruptible_sleep_on(&info->close_wait);
686 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
687 -EAGAIN : -ERESTARTSYS);
691 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
693 spin_lock_irqsave(&info->netlock, flags);
694 if (info->netcount) {
696 spin_unlock_irqrestore(&info->netlock, flags);
700 spin_unlock_irqrestore(&info->netlock, flags);
702 if (info->count == 1) {
703 /* 1st open on this device, init hardware */
704 retval = startup(info);
709 retval = block_til_ready(tty, filp, info);
711 DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
720 info->tty = NULL; /* tty layer will release tty struct */
725 DBGINFO(("%s open rc=%d\n", info->device_name, retval));
729 static void close(struct tty_struct *tty, struct file *filp)
731 struct slgt_info *info = tty->driver_data;
733 if (sanity_check(info, tty->name, "close"))
735 DBGINFO(("%s close entry, count=%d\n", info->device_name, info->count));
740 if (tty_hung_up_p(filp))
743 if ((tty->count == 1) && (info->count != 1)) {
745 * tty->count is 1 and the tty structure will be freed.
746 * info->count should be one in this case.
747 * if it's not, correct it so that the port is shutdown.
749 DBGERR(("%s close: bad refcount; tty->count=1, "
750 "info->count=%d\n", info->device_name, info->count));
756 /* if at least one open remaining, leave hardware active */
760 info->flags |= ASYNC_CLOSING;
762 /* set tty->closing to notify line discipline to
763 * only process XON/XOFF characters. Only the N_TTY
764 * discipline appears to use this (ppp does not).
768 /* wait for transmit data to clear all layers */
770 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
771 DBGINFO(("%s call tty_wait_until_sent\n", info->device_name));
772 tty_wait_until_sent(tty, info->closing_wait);
775 if (info->flags & ASYNC_INITIALIZED)
776 wait_until_sent(tty, info->timeout);
777 if (tty->driver->flush_buffer)
778 tty->driver->flush_buffer(tty);
779 tty_ldisc_flush(tty);
786 if (info->blocked_open) {
787 if (info->close_delay) {
788 msleep_interruptible(jiffies_to_msecs(info->close_delay));
790 wake_up_interruptible(&info->open_wait);
793 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
795 wake_up_interruptible(&info->close_wait);
798 DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->count));
801 static void hangup(struct tty_struct *tty)
803 struct slgt_info *info = tty->driver_data;
805 if (sanity_check(info, tty->name, "hangup"))
807 DBGINFO(("%s hangup\n", info->device_name));
813 info->flags &= ~ASYNC_NORMAL_ACTIVE;
816 wake_up_interruptible(&info->open_wait);
819 static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
821 struct slgt_info *info = tty->driver_data;
824 DBGINFO(("%s set_termios\n", tty->driver->name));
826 /* just return if nothing has changed */
827 if ((tty->termios->c_cflag == old_termios->c_cflag)
828 && (RELEVANT_IFLAG(tty->termios->c_iflag)
829 == RELEVANT_IFLAG(old_termios->c_iflag)))
834 /* Handle transition to B0 status */
835 if (old_termios->c_cflag & CBAUD &&
836 !(tty->termios->c_cflag & CBAUD)) {
837 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
838 spin_lock_irqsave(&info->lock,flags);
840 spin_unlock_irqrestore(&info->lock,flags);
843 /* Handle transition away from B0 status */
844 if (!(old_termios->c_cflag & CBAUD) &&
845 tty->termios->c_cflag & CBAUD) {
846 info->signals |= SerialSignal_DTR;
847 if (!(tty->termios->c_cflag & CRTSCTS) ||
848 !test_bit(TTY_THROTTLED, &tty->flags)) {
849 info->signals |= SerialSignal_RTS;
851 spin_lock_irqsave(&info->lock,flags);
853 spin_unlock_irqrestore(&info->lock,flags);
856 /* Handle turning off CRTSCTS */
857 if (old_termios->c_cflag & CRTSCTS &&
858 !(tty->termios->c_cflag & CRTSCTS)) {
864 static int write(struct tty_struct *tty,
865 const unsigned char *buf, int count)
868 struct slgt_info *info = tty->driver_data;
871 if (sanity_check(info, tty->name, "write"))
873 DBGINFO(("%s write count=%d\n", info->device_name, count));
878 if (count > info->max_frame_size) {
886 if (info->params.mode == MGSL_MODE_RAW ||
887 info->params.mode == MGSL_MODE_MONOSYNC ||
888 info->params.mode == MGSL_MODE_BISYNC) {
889 unsigned int bufs_needed = (count/DMABUFSIZE);
890 unsigned int bufs_free = free_tbuf_count(info);
891 if (count % DMABUFSIZE)
893 if (bufs_needed > bufs_free)
898 if (info->tx_count) {
899 /* send accumulated data from send_char() calls */
900 /* as frame and wait before accepting more data. */
901 tx_load(info, info->tx_buf, info->tx_count);
906 ret = info->tx_count = count;
907 tx_load(info, buf, count);
911 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
912 spin_lock_irqsave(&info->lock,flags);
913 if (!info->tx_active)
915 spin_unlock_irqrestore(&info->lock,flags);
919 DBGINFO(("%s write rc=%d\n", info->device_name, ret));
923 static void put_char(struct tty_struct *tty, unsigned char ch)
925 struct slgt_info *info = tty->driver_data;
928 if (sanity_check(info, tty->name, "put_char"))
930 DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
933 spin_lock_irqsave(&info->lock,flags);
934 if (!info->tx_active && (info->tx_count < info->max_frame_size))
935 info->tx_buf[info->tx_count++] = ch;
936 spin_unlock_irqrestore(&info->lock,flags);
939 static void send_xchar(struct tty_struct *tty, char ch)
941 struct slgt_info *info = tty->driver_data;
944 if (sanity_check(info, tty->name, "send_xchar"))
946 DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
949 spin_lock_irqsave(&info->lock,flags);
950 if (!info->tx_enabled)
952 spin_unlock_irqrestore(&info->lock,flags);
956 static void wait_until_sent(struct tty_struct *tty, int timeout)
958 struct slgt_info *info = tty->driver_data;
959 unsigned long orig_jiffies, char_time;
963 if (sanity_check(info, tty->name, "wait_until_sent"))
965 DBGINFO(("%s wait_until_sent entry\n", info->device_name));
966 if (!(info->flags & ASYNC_INITIALIZED))
969 orig_jiffies = jiffies;
971 /* Set check interval to 1/5 of estimated time to
972 * send a character, and make it at least 1. The check
973 * interval should also be less than the timeout.
974 * Note: use tight timings here to satisfy the NIST-PCTS.
977 if (info->params.data_rate) {
978 char_time = info->timeout/(32 * 5);
985 char_time = min_t(unsigned long, char_time, timeout);
987 while (info->tx_active) {
988 msleep_interruptible(jiffies_to_msecs(char_time));
989 if (signal_pending(current))
991 if (timeout && time_after(jiffies, orig_jiffies + timeout))
996 DBGINFO(("%s wait_until_sent exit\n", info->device_name));
999 static int write_room(struct tty_struct *tty)
1001 struct slgt_info *info = tty->driver_data;
1004 if (sanity_check(info, tty->name, "write_room"))
1006 ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
1007 DBGINFO(("%s write_room=%d\n", info->device_name, ret));
1011 static void flush_chars(struct tty_struct *tty)
1013 struct slgt_info *info = tty->driver_data;
1014 unsigned long flags;
1016 if (sanity_check(info, tty->name, "flush_chars"))
1018 DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
1020 if (info->tx_count <= 0 || tty->stopped ||
1021 tty->hw_stopped || !info->tx_buf)
1024 DBGINFO(("%s flush_chars start transmit\n", info->device_name));
1026 spin_lock_irqsave(&info->lock,flags);
1027 if (!info->tx_active && info->tx_count) {
1028 tx_load(info, info->tx_buf,info->tx_count);
1031 spin_unlock_irqrestore(&info->lock,flags);
1034 static void flush_buffer(struct tty_struct *tty)
1036 struct slgt_info *info = tty->driver_data;
1037 unsigned long flags;
1039 if (sanity_check(info, tty->name, "flush_buffer"))
1041 DBGINFO(("%s flush_buffer\n", info->device_name));
1043 spin_lock_irqsave(&info->lock,flags);
1044 if (!info->tx_active)
1046 spin_unlock_irqrestore(&info->lock,flags);
1052 * throttle (stop) transmitter
1054 static void tx_hold(struct tty_struct *tty)
1056 struct slgt_info *info = tty->driver_data;
1057 unsigned long flags;
1059 if (sanity_check(info, tty->name, "tx_hold"))
1061 DBGINFO(("%s tx_hold\n", info->device_name));
1062 spin_lock_irqsave(&info->lock,flags);
1063 if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
1065 spin_unlock_irqrestore(&info->lock,flags);
1069 * release (start) transmitter
1071 static void tx_release(struct tty_struct *tty)
1073 struct slgt_info *info = tty->driver_data;
1074 unsigned long flags;
1076 if (sanity_check(info, tty->name, "tx_release"))
1078 DBGINFO(("%s tx_release\n", info->device_name));
1079 spin_lock_irqsave(&info->lock,flags);
1080 if (!info->tx_active && info->tx_count) {
1081 tx_load(info, info->tx_buf, info->tx_count);
1084 spin_unlock_irqrestore(&info->lock,flags);
1088 * Service an IOCTL request
1092 * tty pointer to tty instance data
1093 * file pointer to associated file object for device
1094 * cmd IOCTL command code
1095 * arg command argument/context
1097 * Return 0 if success, otherwise error code
1099 static int ioctl(struct tty_struct *tty, struct file *file,
1100 unsigned int cmd, unsigned long arg)
1102 struct slgt_info *info = tty->driver_data;
1103 struct mgsl_icount cnow; /* kernel counter temps */
1104 struct serial_icounter_struct __user *p_cuser; /* user space */
1105 unsigned long flags;
1106 void __user *argp = (void __user *)arg;
1108 if (sanity_check(info, tty->name, "ioctl"))
1110 DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
1112 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1113 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1114 if (tty->flags & (1 << TTY_IO_ERROR))
1119 case MGSL_IOCGPARAMS:
1120 return get_params(info, argp);
1121 case MGSL_IOCSPARAMS:
1122 return set_params(info, argp);
1123 case MGSL_IOCGTXIDLE:
1124 return get_txidle(info, argp);
1125 case MGSL_IOCSTXIDLE:
1126 return set_txidle(info, (int)arg);
1127 case MGSL_IOCTXENABLE:
1128 return tx_enable(info, (int)arg);
1129 case MGSL_IOCRXENABLE:
1130 return rx_enable(info, (int)arg);
1131 case MGSL_IOCTXABORT:
1132 return tx_abort(info);
1133 case MGSL_IOCGSTATS:
1134 return get_stats(info, argp);
1135 case MGSL_IOCWAITEVENT:
1136 return wait_mgsl_event(info, argp);
1138 return modem_input_wait(info,(int)arg);
1140 return get_interface(info, argp);
1142 return set_interface(info,(int)arg);
1144 return set_gpio(info, argp);
1146 return get_gpio(info, argp);
1147 case MGSL_IOCWAITGPIO:
1148 return wait_gpio(info, argp);
1150 spin_lock_irqsave(&info->lock,flags);
1151 cnow = info->icount;
1152 spin_unlock_irqrestore(&info->lock,flags);
1154 if (put_user(cnow.cts, &p_cuser->cts) ||
1155 put_user(cnow.dsr, &p_cuser->dsr) ||
1156 put_user(cnow.rng, &p_cuser->rng) ||
1157 put_user(cnow.dcd, &p_cuser->dcd) ||
1158 put_user(cnow.rx, &p_cuser->rx) ||
1159 put_user(cnow.tx, &p_cuser->tx) ||
1160 put_user(cnow.frame, &p_cuser->frame) ||
1161 put_user(cnow.overrun, &p_cuser->overrun) ||
1162 put_user(cnow.parity, &p_cuser->parity) ||
1163 put_user(cnow.brk, &p_cuser->brk) ||
1164 put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1168 return -ENOIOCTLCMD;
1176 static inline int line_info(char *buf, struct slgt_info *info)
1180 unsigned long flags;
1182 ret = sprintf(buf, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
1183 info->device_name, info->phys_reg_addr,
1184 info->irq_level, info->max_frame_size);
1186 /* output current serial signal states */
1187 spin_lock_irqsave(&info->lock,flags);
1189 spin_unlock_irqrestore(&info->lock,flags);
1193 if (info->signals & SerialSignal_RTS)
1194 strcat(stat_buf, "|RTS");
1195 if (info->signals & SerialSignal_CTS)
1196 strcat(stat_buf, "|CTS");
1197 if (info->signals & SerialSignal_DTR)
1198 strcat(stat_buf, "|DTR");
1199 if (info->signals & SerialSignal_DSR)
1200 strcat(stat_buf, "|DSR");
1201 if (info->signals & SerialSignal_DCD)
1202 strcat(stat_buf, "|CD");
1203 if (info->signals & SerialSignal_RI)
1204 strcat(stat_buf, "|RI");
1206 if (info->params.mode != MGSL_MODE_ASYNC) {
1207 ret += sprintf(buf+ret, "\tHDLC txok:%d rxok:%d",
1208 info->icount.txok, info->icount.rxok);
1209 if (info->icount.txunder)
1210 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
1211 if (info->icount.txabort)
1212 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
1213 if (info->icount.rxshort)
1214 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
1215 if (info->icount.rxlong)
1216 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
1217 if (info->icount.rxover)
1218 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
1219 if (info->icount.rxcrc)
1220 ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
1222 ret += sprintf(buf+ret, "\tASYNC tx:%d rx:%d",
1223 info->icount.tx, info->icount.rx);
1224 if (info->icount.frame)
1225 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
1226 if (info->icount.parity)
1227 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
1228 if (info->icount.brk)
1229 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
1230 if (info->icount.overrun)
1231 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
1234 /* Append serial signal status to end */
1235 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
1237 ret += sprintf(buf+ret, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1238 info->tx_active,info->bh_requested,info->bh_running,
1244 /* Called to print information about devices
1246 static int read_proc(char *page, char **start, off_t off, int count,
1247 int *eof, void *data)
1251 struct slgt_info *info;
1253 len += sprintf(page, "synclink_gt driver:%s\n", driver_version);
1255 info = slgt_device_list;
1257 l = line_info(page + len, info);
1259 if (len+begin > off+count)
1261 if (len+begin < off) {
1265 info = info->next_device;
1270 if (off >= len+begin)
1272 *start = page + (off-begin);
1273 return ((count < begin+len-off) ? count : begin+len-off);
1277 * return count of bytes in transmit buffer
1279 static int chars_in_buffer(struct tty_struct *tty)
1281 struct slgt_info *info = tty->driver_data;
1282 if (sanity_check(info, tty->name, "chars_in_buffer"))
1284 DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, info->tx_count));
1285 return info->tx_count;
1289 * signal remote device to throttle send data (our receive data)
1291 static void throttle(struct tty_struct * tty)
1293 struct slgt_info *info = tty->driver_data;
1294 unsigned long flags;
1296 if (sanity_check(info, tty->name, "throttle"))
1298 DBGINFO(("%s throttle\n", info->device_name));
1300 send_xchar(tty, STOP_CHAR(tty));
1301 if (tty->termios->c_cflag & CRTSCTS) {
1302 spin_lock_irqsave(&info->lock,flags);
1303 info->signals &= ~SerialSignal_RTS;
1305 spin_unlock_irqrestore(&info->lock,flags);
1310 * signal remote device to stop throttling send data (our receive data)
1312 static void unthrottle(struct tty_struct * tty)
1314 struct slgt_info *info = tty->driver_data;
1315 unsigned long flags;
1317 if (sanity_check(info, tty->name, "unthrottle"))
1319 DBGINFO(("%s unthrottle\n", info->device_name));
1324 send_xchar(tty, START_CHAR(tty));
1326 if (tty->termios->c_cflag & CRTSCTS) {
1327 spin_lock_irqsave(&info->lock,flags);
1328 info->signals |= SerialSignal_RTS;
1330 spin_unlock_irqrestore(&info->lock,flags);
1335 * set or clear transmit break condition
1336 * break_state -1=set break condition, 0=clear
1338 static void set_break(struct tty_struct *tty, int break_state)
1340 struct slgt_info *info = tty->driver_data;
1341 unsigned short value;
1342 unsigned long flags;
1344 if (sanity_check(info, tty->name, "set_break"))
1346 DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
1348 spin_lock_irqsave(&info->lock,flags);
1349 value = rd_reg16(info, TCR);
1350 if (break_state == -1)
1354 wr_reg16(info, TCR, value);
1355 spin_unlock_irqrestore(&info->lock,flags);
1358 #if SYNCLINK_GENERIC_HDLC
1361 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1362 * set encoding and frame check sequence (FCS) options
1364 * dev pointer to network device structure
1365 * encoding serial encoding setting
1366 * parity FCS setting
1368 * returns 0 if success, otherwise error code
1370 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
1371 unsigned short parity)
1373 struct slgt_info *info = dev_to_port(dev);
1374 unsigned char new_encoding;
1375 unsigned short new_crctype;
1377 /* return error if TTY interface open */
1381 DBGINFO(("%s hdlcdev_attach\n", info->device_name));
1385 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
1386 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
1387 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
1388 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
1389 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
1390 default: return -EINVAL;
1395 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
1396 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
1397 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
1398 default: return -EINVAL;
1401 info->params.encoding = new_encoding;
1402 info->params.crc_type = new_crctype;
1404 /* if network interface up, reprogram hardware */
1412 * called by generic HDLC layer to send frame
1414 * skb socket buffer containing HDLC frame
1415 * dev pointer to network device structure
1417 * returns 0 if success, otherwise error code
1419 static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
1421 struct slgt_info *info = dev_to_port(dev);
1422 struct net_device_stats *stats = hdlc_stats(dev);
1423 unsigned long flags;
1425 DBGINFO(("%s hdlc_xmit\n", dev->name));
1427 /* stop sending until this frame completes */
1428 netif_stop_queue(dev);
1430 /* copy data to device buffers */
1431 info->tx_count = skb->len;
1432 tx_load(info, skb->data, skb->len);
1434 /* update network statistics */
1435 stats->tx_packets++;
1436 stats->tx_bytes += skb->len;
1438 /* done with socket buffer, so free it */
1441 /* save start time for transmit timeout detection */
1442 dev->trans_start = jiffies;
1444 /* start hardware transmitter if necessary */
1445 spin_lock_irqsave(&info->lock,flags);
1446 if (!info->tx_active)
1448 spin_unlock_irqrestore(&info->lock,flags);
1454 * called by network layer when interface enabled
1455 * claim resources and initialize hardware
1457 * dev pointer to network device structure
1459 * returns 0 if success, otherwise error code
1461 static int hdlcdev_open(struct net_device *dev)
1463 struct slgt_info *info = dev_to_port(dev);
1465 unsigned long flags;
1467 DBGINFO(("%s hdlcdev_open\n", dev->name));
1469 /* generic HDLC layer open processing */
1470 if ((rc = hdlc_open(dev)))
1473 /* arbitrate between network and tty opens */
1474 spin_lock_irqsave(&info->netlock, flags);
1475 if (info->count != 0 || info->netcount != 0) {
1476 DBGINFO(("%s hdlc_open busy\n", dev->name));
1477 spin_unlock_irqrestore(&info->netlock, flags);
1481 spin_unlock_irqrestore(&info->netlock, flags);
1483 /* claim resources and init adapter */
1484 if ((rc = startup(info)) != 0) {
1485 spin_lock_irqsave(&info->netlock, flags);
1487 spin_unlock_irqrestore(&info->netlock, flags);
1491 /* assert DTR and RTS, apply hardware settings */
1492 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
1495 /* enable network layer transmit */
1496 dev->trans_start = jiffies;
1497 netif_start_queue(dev);
1499 /* inform generic HDLC layer of current DCD status */
1500 spin_lock_irqsave(&info->lock, flags);
1502 spin_unlock_irqrestore(&info->lock, flags);
1503 if (info->signals & SerialSignal_DCD)
1504 netif_carrier_on(dev);
1506 netif_carrier_off(dev);
1511 * called by network layer when interface is disabled
1512 * shutdown hardware and release resources
1514 * dev pointer to network device structure
1516 * returns 0 if success, otherwise error code
1518 static int hdlcdev_close(struct net_device *dev)
1520 struct slgt_info *info = dev_to_port(dev);
1521 unsigned long flags;
1523 DBGINFO(("%s hdlcdev_close\n", dev->name));
1525 netif_stop_queue(dev);
1527 /* shutdown adapter and release resources */
1532 spin_lock_irqsave(&info->netlock, flags);
1534 spin_unlock_irqrestore(&info->netlock, flags);
1540 * called by network layer to process IOCTL call to network device
1542 * dev pointer to network device structure
1543 * ifr pointer to network interface request structure
1544 * cmd IOCTL command code
1546 * returns 0 if success, otherwise error code
1548 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1550 const size_t size = sizeof(sync_serial_settings);
1551 sync_serial_settings new_line;
1552 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1553 struct slgt_info *info = dev_to_port(dev);
1556 DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
1558 /* return error if TTY interface open */
1562 if (cmd != SIOCWANDEV)
1563 return hdlc_ioctl(dev, ifr, cmd);
1565 switch(ifr->ifr_settings.type) {
1566 case IF_GET_IFACE: /* return current sync_serial_settings */
1568 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1569 if (ifr->ifr_settings.size < size) {
1570 ifr->ifr_settings.size = size; /* data size wanted */
1574 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1575 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1576 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1577 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1580 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
1581 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
1582 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
1583 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
1584 default: new_line.clock_type = CLOCK_DEFAULT;
1587 new_line.clock_rate = info->params.clock_speed;
1588 new_line.loopback = info->params.loopback ? 1:0;
1590 if (copy_to_user(line, &new_line, size))
1594 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
1596 if(!capable(CAP_NET_ADMIN))
1598 if (copy_from_user(&new_line, line, size))
1601 switch (new_line.clock_type)
1603 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
1604 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
1605 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
1606 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
1607 case CLOCK_DEFAULT: flags = info->params.flags &
1608 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1609 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1610 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1611 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
1612 default: return -EINVAL;
1615 if (new_line.loopback != 0 && new_line.loopback != 1)
1618 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1619 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1620 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1621 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1622 info->params.flags |= flags;
1624 info->params.loopback = new_line.loopback;
1626 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
1627 info->params.clock_speed = new_line.clock_rate;
1629 info->params.clock_speed = 0;
1631 /* if network interface up, reprogram hardware */
1637 return hdlc_ioctl(dev, ifr, cmd);
1642 * called by network layer when transmit timeout is detected
1644 * dev pointer to network device structure
1646 static void hdlcdev_tx_timeout(struct net_device *dev)
1648 struct slgt_info *info = dev_to_port(dev);
1649 struct net_device_stats *stats = hdlc_stats(dev);
1650 unsigned long flags;
1652 DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
1655 stats->tx_aborted_errors++;
1657 spin_lock_irqsave(&info->lock,flags);
1659 spin_unlock_irqrestore(&info->lock,flags);
1661 netif_wake_queue(dev);
1665 * called by device driver when transmit completes
1666 * reenable network layer transmit if stopped
1668 * info pointer to device instance information
1670 static void hdlcdev_tx_done(struct slgt_info *info)
1672 if (netif_queue_stopped(info->netdev))
1673 netif_wake_queue(info->netdev);
1677 * called by device driver when frame received
1678 * pass frame to network layer
1680 * info pointer to device instance information
1681 * buf pointer to buffer contianing frame data
1682 * size count of data bytes in buf
1684 static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
1686 struct sk_buff *skb = dev_alloc_skb(size);
1687 struct net_device *dev = info->netdev;
1688 struct net_device_stats *stats = hdlc_stats(dev);
1690 DBGINFO(("%s hdlcdev_rx\n", dev->name));
1693 DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
1694 stats->rx_dropped++;
1698 memcpy(skb_put(skb, size),buf,size);
1700 skb->protocol = hdlc_type_trans(skb, info->netdev);
1702 stats->rx_packets++;
1703 stats->rx_bytes += size;
1707 info->netdev->last_rx = jiffies;
1711 * called by device driver when adding device instance
1712 * do generic HDLC initialization
1714 * info pointer to device instance information
1716 * returns 0 if success, otherwise error code
1718 static int hdlcdev_init(struct slgt_info *info)
1721 struct net_device *dev;
1724 /* allocate and initialize network and HDLC layer objects */
1726 if (!(dev = alloc_hdlcdev(info))) {
1727 printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
1731 /* for network layer reporting purposes only */
1732 dev->mem_start = info->phys_reg_addr;
1733 dev->mem_end = info->phys_reg_addr + SLGT_REG_SIZE - 1;
1734 dev->irq = info->irq_level;
1736 /* network layer callbacks and settings */
1737 dev->do_ioctl = hdlcdev_ioctl;
1738 dev->open = hdlcdev_open;
1739 dev->stop = hdlcdev_close;
1740 dev->tx_timeout = hdlcdev_tx_timeout;
1741 dev->watchdog_timeo = 10*HZ;
1742 dev->tx_queue_len = 50;
1744 /* generic HDLC layer callbacks and settings */
1745 hdlc = dev_to_hdlc(dev);
1746 hdlc->attach = hdlcdev_attach;
1747 hdlc->xmit = hdlcdev_xmit;
1749 /* register objects with HDLC layer */
1750 if ((rc = register_hdlc_device(dev))) {
1751 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
1761 * called by device driver when removing device instance
1762 * do generic HDLC cleanup
1764 * info pointer to device instance information
1766 static void hdlcdev_exit(struct slgt_info *info)
1768 unregister_hdlc_device(info->netdev);
1769 free_netdev(info->netdev);
1770 info->netdev = NULL;
1773 #endif /* ifdef CONFIG_HDLC */
1776 * get async data from rx DMA buffers
1778 static void rx_async(struct slgt_info *info)
1780 struct tty_struct *tty = info->tty;
1781 struct mgsl_icount *icount = &info->icount;
1782 unsigned int start, end;
1784 unsigned char status;
1785 struct slgt_desc *bufs = info->rbufs;
1791 start = end = info->rbuf_current;
1793 while(desc_complete(bufs[end])) {
1794 count = desc_count(bufs[end]) - info->rbuf_index;
1795 p = bufs[end].buf + info->rbuf_index;
1797 DBGISR(("%s rx_async count=%d\n", info->device_name, count));
1798 DBGDATA(info, p, count, "rx");
1800 for(i=0 ; i < count; i+=2, p+=2) {
1806 if ((status = *(p+1) & (BIT1 + BIT0))) {
1809 else if (status & BIT0)
1811 /* discard char if tty control flags say so */
1812 if (status & info->ignore_status_mask)
1816 else if (status & BIT0)
1820 tty_insert_flip_char(tty, ch, stat);
1826 /* receive buffer not completed */
1827 info->rbuf_index += i;
1828 mod_timer(&info->rx_timer, jiffies + 1);
1832 info->rbuf_index = 0;
1833 free_rbufs(info, end, end);
1835 if (++end == info->rbuf_count)
1838 /* if entire list searched then no frame available */
1844 tty_flip_buffer_push(tty);
1848 * return next bottom half action to perform
1850 static int bh_action(struct slgt_info *info)
1852 unsigned long flags;
1855 spin_lock_irqsave(&info->lock,flags);
1857 if (info->pending_bh & BH_RECEIVE) {
1858 info->pending_bh &= ~BH_RECEIVE;
1860 } else if (info->pending_bh & BH_TRANSMIT) {
1861 info->pending_bh &= ~BH_TRANSMIT;
1863 } else if (info->pending_bh & BH_STATUS) {
1864 info->pending_bh &= ~BH_STATUS;
1867 /* Mark BH routine as complete */
1868 info->bh_running = 0;
1869 info->bh_requested = 0;
1873 spin_unlock_irqrestore(&info->lock,flags);
1879 * perform bottom half processing
1881 static void bh_handler(struct work_struct *work)
1883 struct slgt_info *info = container_of(work, struct slgt_info, task);
1888 info->bh_running = 1;
1890 while((action = bh_action(info))) {
1893 DBGBH(("%s bh receive\n", info->device_name));
1894 switch(info->params.mode) {
1895 case MGSL_MODE_ASYNC:
1898 case MGSL_MODE_HDLC:
1899 while(rx_get_frame(info));
1902 case MGSL_MODE_MONOSYNC:
1903 case MGSL_MODE_BISYNC:
1904 while(rx_get_buf(info));
1907 /* restart receiver if rx DMA buffers exhausted */
1908 if (info->rx_restart)
1915 DBGBH(("%s bh status\n", info->device_name));
1916 info->ri_chkcount = 0;
1917 info->dsr_chkcount = 0;
1918 info->dcd_chkcount = 0;
1919 info->cts_chkcount = 0;
1922 DBGBH(("%s unknown action\n", info->device_name));
1926 DBGBH(("%s bh_handler exit\n", info->device_name));
1929 static void bh_transmit(struct slgt_info *info)
1931 struct tty_struct *tty = info->tty;
1933 DBGBH(("%s bh_transmit\n", info->device_name));
1938 static void dsr_change(struct slgt_info *info)
1941 DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
1942 if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
1943 slgt_irq_off(info, IRQ_DSR);
1947 if (info->signals & SerialSignal_DSR)
1948 info->input_signal_events.dsr_up++;
1950 info->input_signal_events.dsr_down++;
1951 wake_up_interruptible(&info->status_event_wait_q);
1952 wake_up_interruptible(&info->event_wait_q);
1953 info->pending_bh |= BH_STATUS;
1956 static void cts_change(struct slgt_info *info)
1959 DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
1960 if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
1961 slgt_irq_off(info, IRQ_CTS);
1965 if (info->signals & SerialSignal_CTS)
1966 info->input_signal_events.cts_up++;
1968 info->input_signal_events.cts_down++;
1969 wake_up_interruptible(&info->status_event_wait_q);
1970 wake_up_interruptible(&info->event_wait_q);
1971 info->pending_bh |= BH_STATUS;
1973 if (info->flags & ASYNC_CTS_FLOW) {
1975 if (info->tty->hw_stopped) {
1976 if (info->signals & SerialSignal_CTS) {
1977 info->tty->hw_stopped = 0;
1978 info->pending_bh |= BH_TRANSMIT;
1982 if (!(info->signals & SerialSignal_CTS))
1983 info->tty->hw_stopped = 1;
1989 static void dcd_change(struct slgt_info *info)
1992 DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
1993 if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
1994 slgt_irq_off(info, IRQ_DCD);
1998 if (info->signals & SerialSignal_DCD) {
1999 info->input_signal_events.dcd_up++;
2001 info->input_signal_events.dcd_down++;
2003 #if SYNCLINK_GENERIC_HDLC
2004 if (info->netcount) {
2005 if (info->signals & SerialSignal_DCD)
2006 netif_carrier_on(info->netdev);
2008 netif_carrier_off(info->netdev);
2011 wake_up_interruptible(&info->status_event_wait_q);
2012 wake_up_interruptible(&info->event_wait_q);
2013 info->pending_bh |= BH_STATUS;
2015 if (info->flags & ASYNC_CHECK_CD) {
2016 if (info->signals & SerialSignal_DCD)
2017 wake_up_interruptible(&info->open_wait);
2020 tty_hangup(info->tty);
2025 static void ri_change(struct slgt_info *info)
2028 DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
2029 if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2030 slgt_irq_off(info, IRQ_RI);
2034 if (info->signals & SerialSignal_RI) {
2035 info->input_signal_events.ri_up++;
2037 info->input_signal_events.ri_down++;
2039 wake_up_interruptible(&info->status_event_wait_q);
2040 wake_up_interruptible(&info->event_wait_q);
2041 info->pending_bh |= BH_STATUS;
2044 static void isr_serial(struct slgt_info *info)
2046 unsigned short status = rd_reg16(info, SSR);
2048 DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
2050 wr_reg16(info, SSR, status); /* clear pending */
2052 info->irq_occurred = 1;
2054 if (info->params.mode == MGSL_MODE_ASYNC) {
2055 if (status & IRQ_TXIDLE) {
2057 isr_txeom(info, status);
2059 if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
2061 /* process break detection if tty control allows */
2063 if (!(status & info->ignore_status_mask)) {
2064 if (info->read_status_mask & MASK_BREAK) {
2065 tty_insert_flip_char(info->tty, 0, TTY_BREAK);
2066 if (info->flags & ASYNC_SAK)
2073 if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
2074 isr_txeom(info, status);
2076 if (status & IRQ_RXIDLE) {
2077 if (status & RXIDLE)
2078 info->icount.rxidle++;
2080 info->icount.exithunt++;
2081 wake_up_interruptible(&info->event_wait_q);
2084 if (status & IRQ_RXOVER)
2088 if (status & IRQ_DSR)
2090 if (status & IRQ_CTS)
2092 if (status & IRQ_DCD)
2094 if (status & IRQ_RI)
2098 static void isr_rdma(struct slgt_info *info)
2100 unsigned int status = rd_reg32(info, RDCSR);
2102 DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
2104 /* RDCSR (rx DMA control/status)
2107 * 06 save status byte to DMA buffer
2109 * 04 eol (end of list)
2110 * 03 eob (end of buffer)
2115 wr_reg32(info, RDCSR, status); /* clear pending */
2117 if (status & (BIT5 + BIT4)) {
2118 DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
2119 info->rx_restart = 1;
2121 info->pending_bh |= BH_RECEIVE;
2124 static void isr_tdma(struct slgt_info *info)
2126 unsigned int status = rd_reg32(info, TDCSR);
2128 DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
2130 /* TDCSR (tx DMA control/status)
2134 * 04 eol (end of list)
2135 * 03 eob (end of buffer)
2140 wr_reg32(info, TDCSR, status); /* clear pending */
2142 if (status & (BIT5 + BIT4 + BIT3)) {
2143 // another transmit buffer has completed
2144 // run bottom half to get more send data from user
2145 info->pending_bh |= BH_TRANSMIT;
2149 static void isr_txeom(struct slgt_info *info, unsigned short status)
2151 DBGISR(("%s txeom status=%04x\n", info->device_name, status));
2153 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
2156 if (status & IRQ_TXUNDER) {
2157 unsigned short val = rd_reg16(info, TCR);
2158 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
2159 wr_reg16(info, TCR, val); /* clear reset bit */
2162 if (info->tx_active) {
2163 if (info->params.mode != MGSL_MODE_ASYNC) {
2164 if (status & IRQ_TXUNDER)
2165 info->icount.txunder++;
2166 else if (status & IRQ_TXIDLE)
2167 info->icount.txok++;
2170 info->tx_active = 0;
2173 del_timer(&info->tx_timer);
2175 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
2176 info->signals &= ~SerialSignal_RTS;
2177 info->drop_rts_on_tx_done = 0;
2181 #if SYNCLINK_GENERIC_HDLC
2183 hdlcdev_tx_done(info);
2187 if (info->tty && (info->tty->stopped || info->tty->hw_stopped)) {
2191 info->pending_bh |= BH_TRANSMIT;
2196 static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
2198 struct cond_wait *w, *prev;
2200 /* wake processes waiting for specific transitions */
2201 for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
2202 if (w->data & changed) {
2204 wake_up_interruptible(&w->q);
2206 prev->next = w->next;
2208 info->gpio_wait_q = w->next;
2214 /* interrupt service routine
2216 * irq interrupt number
2217 * dev_id device ID supplied during interrupt registration
2219 static irqreturn_t slgt_interrupt(int irq, void *dev_id)
2221 struct slgt_info *info;
2225 DBGISR(("slgt_interrupt irq=%d entry\n", irq));
2231 spin_lock(&info->lock);
2233 while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
2234 DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
2235 info->irq_occurred = 1;
2236 for(i=0; i < info->port_count ; i++) {
2237 if (info->port_array[i] == NULL)
2239 if (gsr & (BIT8 << i))
2240 isr_serial(info->port_array[i]);
2241 if (gsr & (BIT16 << (i*2)))
2242 isr_rdma(info->port_array[i]);
2243 if (gsr & (BIT17 << (i*2)))
2244 isr_tdma(info->port_array[i]);
2248 if (info->gpio_present) {
2250 unsigned int changed;
2251 while ((changed = rd_reg32(info, IOSR)) != 0) {
2252 DBGISR(("%s iosr=%08x\n", info->device_name, changed));
2253 /* read latched state of GPIO signals */
2254 state = rd_reg32(info, IOVR);
2255 /* clear pending GPIO interrupt bits */
2256 wr_reg32(info, IOSR, changed);
2257 for (i=0 ; i < info->port_count ; i++) {
2258 if (info->port_array[i] != NULL)
2259 isr_gpio(info->port_array[i], changed, state);
2264 for(i=0; i < info->port_count ; i++) {
2265 struct slgt_info *port = info->port_array[i];
2267 if (port && (port->count || port->netcount) &&
2268 port->pending_bh && !port->bh_running &&
2269 !port->bh_requested) {
2270 DBGISR(("%s bh queued\n", port->device_name));
2271 schedule_work(&port->task);
2272 port->bh_requested = 1;
2276 spin_unlock(&info->lock);
2278 DBGISR(("slgt_interrupt irq=%d exit\n", irq));
2282 static int startup(struct slgt_info *info)
2284 DBGINFO(("%s startup\n", info->device_name));
2286 if (info->flags & ASYNC_INITIALIZED)
2289 if (!info->tx_buf) {
2290 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
2291 if (!info->tx_buf) {
2292 DBGERR(("%s can't allocate tx buffer\n", info->device_name));
2297 info->pending_bh = 0;
2299 memset(&info->icount, 0, sizeof(info->icount));
2301 /* program hardware for current parameters */
2302 change_params(info);
2305 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2307 info->flags |= ASYNC_INITIALIZED;
2313 * called by close() and hangup() to shutdown hardware
2315 static void shutdown(struct slgt_info *info)
2317 unsigned long flags;
2319 if (!(info->flags & ASYNC_INITIALIZED))
2322 DBGINFO(("%s shutdown\n", info->device_name));
2324 /* clear status wait queue because status changes */
2325 /* can't happen after shutting down the hardware */
2326 wake_up_interruptible(&info->status_event_wait_q);
2327 wake_up_interruptible(&info->event_wait_q);
2329 del_timer_sync(&info->tx_timer);
2330 del_timer_sync(&info->rx_timer);
2332 kfree(info->tx_buf);
2333 info->tx_buf = NULL;
2335 spin_lock_irqsave(&info->lock,flags);
2340 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
2342 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
2343 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2347 flush_cond_wait(&info->gpio_wait_q);
2349 spin_unlock_irqrestore(&info->lock,flags);
2352 set_bit(TTY_IO_ERROR, &info->tty->flags);
2354 info->flags &= ~ASYNC_INITIALIZED;
2357 static void program_hw(struct slgt_info *info)
2359 unsigned long flags;
2361 spin_lock_irqsave(&info->lock,flags);
2366 if (info->params.mode != MGSL_MODE_ASYNC ||
2374 info->dcd_chkcount = 0;
2375 info->cts_chkcount = 0;
2376 info->ri_chkcount = 0;
2377 info->dsr_chkcount = 0;
2379 slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR);
2382 if (info->netcount ||
2383 (info->tty && info->tty->termios->c_cflag & CREAD))
2386 spin_unlock_irqrestore(&info->lock,flags);
2390 * reconfigure adapter based on new parameters
2392 static void change_params(struct slgt_info *info)
2397 if (!info->tty || !info->tty->termios)
2399 DBGINFO(("%s change_params\n", info->device_name));
2401 cflag = info->tty->termios->c_cflag;
2403 /* if B0 rate (hangup) specified then negate DTR and RTS */
2404 /* otherwise assert DTR and RTS */
2406 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
2408 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2410 /* byte size and parity */
2412 switch (cflag & CSIZE) {
2413 case CS5: info->params.data_bits = 5; break;
2414 case CS6: info->params.data_bits = 6; break;
2415 case CS7: info->params.data_bits = 7; break;
2416 case CS8: info->params.data_bits = 8; break;
2417 default: info->params.data_bits = 7; break;
2420 info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
2423 info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
2425 info->params.parity = ASYNC_PARITY_NONE;
2427 /* calculate number of jiffies to transmit a full
2428 * FIFO (32 bytes) at specified data rate
2430 bits_per_char = info->params.data_bits +
2431 info->params.stop_bits + 1;
2433 info->params.data_rate = tty_get_baud_rate(info->tty);
2435 if (info->params.data_rate) {
2436 info->timeout = (32*HZ*bits_per_char) /
2437 info->params.data_rate;
2439 info->timeout += HZ/50; /* Add .02 seconds of slop */
2441 if (cflag & CRTSCTS)
2442 info->flags |= ASYNC_CTS_FLOW;
2444 info->flags &= ~ASYNC_CTS_FLOW;
2447 info->flags &= ~ASYNC_CHECK_CD;
2449 info->flags |= ASYNC_CHECK_CD;
2451 /* process tty input control flags */
2453 info->read_status_mask = IRQ_RXOVER;
2454 if (I_INPCK(info->tty))
2455 info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
2456 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2457 info->read_status_mask |= MASK_BREAK;
2458 if (I_IGNPAR(info->tty))
2459 info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
2460 if (I_IGNBRK(info->tty)) {
2461 info->ignore_status_mask |= MASK_BREAK;
2462 /* If ignoring parity and break indicators, ignore
2463 * overruns too. (For real raw support).
2465 if (I_IGNPAR(info->tty))
2466 info->ignore_status_mask |= MASK_OVERRUN;
2472 static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
2474 DBGINFO(("%s get_stats\n", info->device_name));
2476 memset(&info->icount, 0, sizeof(info->icount));
2478 if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
2484 static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
2486 DBGINFO(("%s get_params\n", info->device_name));
2487 if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
2492 static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
2494 unsigned long flags;
2495 MGSL_PARAMS tmp_params;
2497 DBGINFO(("%s set_params\n", info->device_name));
2498 if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
2501 spin_lock_irqsave(&info->lock, flags);
2502 memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
2503 spin_unlock_irqrestore(&info->lock, flags);
2505 change_params(info);
2510 static int get_txidle(struct slgt_info *info, int __user *idle_mode)
2512 DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
2513 if (put_user(info->idle_mode, idle_mode))
2518 static int set_txidle(struct slgt_info *info, int idle_mode)
2520 unsigned long flags;
2521 DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
2522 spin_lock_irqsave(&info->lock,flags);
2523 info->idle_mode = idle_mode;
2524 if (info->params.mode != MGSL_MODE_ASYNC)
2526 spin_unlock_irqrestore(&info->lock,flags);
2530 static int tx_enable(struct slgt_info *info, int enable)
2532 unsigned long flags;
2533 DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
2534 spin_lock_irqsave(&info->lock,flags);
2536 if (!info->tx_enabled)
2539 if (info->tx_enabled)
2542 spin_unlock_irqrestore(&info->lock,flags);
2547 * abort transmit HDLC frame
2549 static int tx_abort(struct slgt_info *info)
2551 unsigned long flags;
2552 DBGINFO(("%s tx_abort\n", info->device_name));
2553 spin_lock_irqsave(&info->lock,flags);
2555 spin_unlock_irqrestore(&info->lock,flags);
2559 static int rx_enable(struct slgt_info *info, int enable)
2561 unsigned long flags;
2562 DBGINFO(("%s rx_enable(%d)\n", info->device_name, enable));
2563 spin_lock_irqsave(&info->lock,flags);
2565 if (!info->rx_enabled)
2567 else if (enable == 2) {
2568 /* force hunt mode (write 1 to RCR[3]) */
2569 wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
2572 if (info->rx_enabled)
2575 spin_unlock_irqrestore(&info->lock,flags);
2580 * wait for specified event to occur
2582 static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
2584 unsigned long flags;
2587 struct mgsl_icount cprev, cnow;
2590 struct _input_signal_events oldsigs, newsigs;
2591 DECLARE_WAITQUEUE(wait, current);
2593 if (get_user(mask, mask_ptr))
2596 DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
2598 spin_lock_irqsave(&info->lock,flags);
2600 /* return immediately if state matches requested events */
2605 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2606 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2607 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2608 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2610 spin_unlock_irqrestore(&info->lock,flags);
2614 /* save current irq counts */
2615 cprev = info->icount;
2616 oldsigs = info->input_signal_events;
2618 /* enable hunt and idle irqs if needed */
2619 if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
2620 unsigned short val = rd_reg16(info, SCR);
2621 if (!(val & IRQ_RXIDLE))
2622 wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
2625 set_current_state(TASK_INTERRUPTIBLE);
2626 add_wait_queue(&info->event_wait_q, &wait);
2628 spin_unlock_irqrestore(&info->lock,flags);
2632 if (signal_pending(current)) {
2637 /* get current irq counts */
2638 spin_lock_irqsave(&info->lock,flags);
2639 cnow = info->icount;
2640 newsigs = info->input_signal_events;
2641 set_current_state(TASK_INTERRUPTIBLE);
2642 spin_unlock_irqrestore(&info->lock,flags);
2644 /* if no change, wait aborted for some reason */
2645 if (newsigs.dsr_up == oldsigs.dsr_up &&
2646 newsigs.dsr_down == oldsigs.dsr_down &&
2647 newsigs.dcd_up == oldsigs.dcd_up &&
2648 newsigs.dcd_down == oldsigs.dcd_down &&
2649 newsigs.cts_up == oldsigs.cts_up &&
2650 newsigs.cts_down == oldsigs.cts_down &&
2651 newsigs.ri_up == oldsigs.ri_up &&
2652 newsigs.ri_down == oldsigs.ri_down &&
2653 cnow.exithunt == cprev.exithunt &&
2654 cnow.rxidle == cprev.rxidle) {
2660 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2661 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2662 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2663 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2664 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2665 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2666 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2667 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2668 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2669 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2677 remove_wait_queue(&info->event_wait_q, &wait);
2678 set_current_state(TASK_RUNNING);
2681 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2682 spin_lock_irqsave(&info->lock,flags);
2683 if (!waitqueue_active(&info->event_wait_q)) {
2684 /* disable enable exit hunt mode/idle rcvd IRQs */
2686 (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
2688 spin_unlock_irqrestore(&info->lock,flags);
2692 rc = put_user(events, mask_ptr);
2696 static int get_interface(struct slgt_info *info, int __user *if_mode)
2698 DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
2699 if (put_user(info->if_mode, if_mode))
2704 static int set_interface(struct slgt_info *info, int if_mode)
2706 unsigned long flags;
2709 DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
2710 spin_lock_irqsave(&info->lock,flags);
2711 info->if_mode = if_mode;
2715 /* TCR (tx control) 07 1=RTS driver control */
2716 val = rd_reg16(info, TCR);
2717 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
2721 wr_reg16(info, TCR, val);
2723 spin_unlock_irqrestore(&info->lock,flags);
2728 * set general purpose IO pin state and direction
2731 * state each bit indicates a pin state
2732 * smask set bit indicates pin state to set
2733 * dir each bit indicates a pin direction (0=input, 1=output)
2734 * dmask set bit indicates pin direction to set
2736 static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2738 unsigned long flags;
2739 struct gpio_desc gpio;
2742 if (!info->gpio_present)
2744 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2746 DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
2747 info->device_name, gpio.state, gpio.smask,
2748 gpio.dir, gpio.dmask));
2750 spin_lock_irqsave(&info->lock,flags);
2752 data = rd_reg32(info, IODR);
2753 data |= gpio.dmask & gpio.dir;
2754 data &= ~(gpio.dmask & ~gpio.dir);
2755 wr_reg32(info, IODR, data);
2758 data = rd_reg32(info, IOVR);
2759 data |= gpio.smask & gpio.state;
2760 data &= ~(gpio.smask & ~gpio.state);
2761 wr_reg32(info, IOVR, data);
2763 spin_unlock_irqrestore(&info->lock,flags);
2769 * get general purpose IO pin state and direction
2771 static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2773 struct gpio_desc gpio;
2774 if (!info->gpio_present)
2776 gpio.state = rd_reg32(info, IOVR);
2777 gpio.smask = 0xffffffff;
2778 gpio.dir = rd_reg32(info, IODR);
2779 gpio.dmask = 0xffffffff;
2780 if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
2782 DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
2783 info->device_name, gpio.state, gpio.dir));
2788 * conditional wait facility
2790 static void init_cond_wait(struct cond_wait *w, unsigned int data)
2792 init_waitqueue_head(&w->q);
2793 init_waitqueue_entry(&w->wait, current);
2797 static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
2799 set_current_state(TASK_INTERRUPTIBLE);
2800 add_wait_queue(&w->q, &w->wait);
2805 static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
2807 struct cond_wait *w, *prev;
2808 remove_wait_queue(&cw->q, &cw->wait);
2809 set_current_state(TASK_RUNNING);
2810 for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
2813 prev->next = w->next;
2821 static void flush_cond_wait(struct cond_wait **head)
2823 while (*head != NULL) {
2824 wake_up_interruptible(&(*head)->q);
2825 *head = (*head)->next;
2830 * wait for general purpose I/O pin(s) to enter specified state
2833 * state - bit indicates target pin state
2834 * smask - set bit indicates watched pin
2836 * The wait ends when at least one watched pin enters the specified
2837 * state. When 0 (no error) is returned, user_gpio->state is set to the
2838 * state of all GPIO pins when the wait ends.
2840 * Note: Each pin may be a dedicated input, dedicated output, or
2841 * configurable input/output. The number and configuration of pins
2842 * varies with the specific adapter model. Only input pins (dedicated
2843 * or configured) can be monitored with this function.
2845 static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2847 unsigned long flags;
2849 struct gpio_desc gpio;
2850 struct cond_wait wait;
2853 if (!info->gpio_present)
2855 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2857 DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
2858 info->device_name, gpio.state, gpio.smask));
2859 /* ignore output pins identified by set IODR bit */
2860 if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
2862 init_cond_wait(&wait, gpio.smask);
2864 spin_lock_irqsave(&info->lock, flags);
2865 /* enable interrupts for watched pins */
2866 wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
2867 /* get current pin states */
2868 state = rd_reg32(info, IOVR);
2870 if (gpio.smask & ~(state ^ gpio.state)) {
2871 /* already in target state */
2874 /* wait for target state */
2875 add_cond_wait(&info->gpio_wait_q, &wait);
2876 spin_unlock_irqrestore(&info->lock, flags);
2878 if (signal_pending(current))
2881 gpio.state = wait.data;
2882 spin_lock_irqsave(&info->lock, flags);
2883 remove_cond_wait(&info->gpio_wait_q, &wait);
2886 /* disable all GPIO interrupts if no waiting processes */
2887 if (info->gpio_wait_q == NULL)
2888 wr_reg32(info, IOER, 0);
2889 spin_unlock_irqrestore(&info->lock,flags);
2891 if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
2896 static int modem_input_wait(struct slgt_info *info,int arg)
2898 unsigned long flags;
2900 struct mgsl_icount cprev, cnow;
2901 DECLARE_WAITQUEUE(wait, current);
2903 /* save current irq counts */
2904 spin_lock_irqsave(&info->lock,flags);
2905 cprev = info->icount;
2906 add_wait_queue(&info->status_event_wait_q, &wait);
2907 set_current_state(TASK_INTERRUPTIBLE);
2908 spin_unlock_irqrestore(&info->lock,flags);
2912 if (signal_pending(current)) {
2917 /* get new irq counts */
2918 spin_lock_irqsave(&info->lock,flags);
2919 cnow = info->icount;
2920 set_current_state(TASK_INTERRUPTIBLE);
2921 spin_unlock_irqrestore(&info->lock,flags);
2923 /* if no change, wait aborted for some reason */
2924 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2925 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2930 /* check for change in caller specified modem input */
2931 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2932 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2933 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2934 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2941 remove_wait_queue(&info->status_event_wait_q, &wait);
2942 set_current_state(TASK_RUNNING);
2947 * return state of serial control and status signals
2949 static int tiocmget(struct tty_struct *tty, struct file *file)
2951 struct slgt_info *info = tty->driver_data;
2952 unsigned int result;
2953 unsigned long flags;
2955 spin_lock_irqsave(&info->lock,flags);
2957 spin_unlock_irqrestore(&info->lock,flags);
2959 result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2960 ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2961 ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2962 ((info->signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2963 ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2964 ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2966 DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
2971 * set modem control signals (DTR/RTS)
2973 * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
2974 * TIOCMSET = set/clear signal values
2975 * value bit mask for command
2977 static int tiocmset(struct tty_struct *tty, struct file *file,
2978 unsigned int set, unsigned int clear)
2980 struct slgt_info *info = tty->driver_data;
2981 unsigned long flags;
2983 DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
2985 if (set & TIOCM_RTS)
2986 info->signals |= SerialSignal_RTS;
2987 if (set & TIOCM_DTR)
2988 info->signals |= SerialSignal_DTR;
2989 if (clear & TIOCM_RTS)
2990 info->signals &= ~SerialSignal_RTS;
2991 if (clear & TIOCM_DTR)
2992 info->signals &= ~SerialSignal_DTR;
2994 spin_lock_irqsave(&info->lock,flags);
2996 spin_unlock_irqrestore(&info->lock,flags);
3001 * block current process until the device is ready to open
3003 static int block_til_ready(struct tty_struct *tty, struct file *filp,
3004 struct slgt_info *info)
3006 DECLARE_WAITQUEUE(wait, current);
3008 int do_clocal = 0, extra_count = 0;
3009 unsigned long flags;
3011 DBGINFO(("%s block_til_ready\n", tty->driver->name));
3013 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3014 /* nonblock mode is set or port is not enabled */
3015 info->flags |= ASYNC_NORMAL_ACTIVE;
3019 if (tty->termios->c_cflag & CLOCAL)
3022 /* Wait for carrier detect and the line to become
3023 * free (i.e., not in use by the callout). While we are in
3024 * this loop, info->count is dropped by one, so that
3025 * close() knows when to free things. We restore it upon
3026 * exit, either normal or abnormal.
3030 add_wait_queue(&info->open_wait, &wait);
3032 spin_lock_irqsave(&info->lock, flags);
3033 if (!tty_hung_up_p(filp)) {
3037 spin_unlock_irqrestore(&info->lock, flags);
3038 info->blocked_open++;
3041 if ((tty->termios->c_cflag & CBAUD)) {
3042 spin_lock_irqsave(&info->lock,flags);
3043 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
3045 spin_unlock_irqrestore(&info->lock,flags);
3048 set_current_state(TASK_INTERRUPTIBLE);
3050 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
3051 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
3052 -EAGAIN : -ERESTARTSYS;
3056 spin_lock_irqsave(&info->lock,flags);
3058 spin_unlock_irqrestore(&info->lock,flags);
3060 if (!(info->flags & ASYNC_CLOSING) &&
3061 (do_clocal || (info->signals & SerialSignal_DCD)) ) {
3065 if (signal_pending(current)) {
3066 retval = -ERESTARTSYS;
3070 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
3074 set_current_state(TASK_RUNNING);
3075 remove_wait_queue(&info->open_wait, &wait);
3079 info->blocked_open--;
3082 info->flags |= ASYNC_NORMAL_ACTIVE;
3084 DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
3088 static int alloc_tmp_rbuf(struct slgt_info *info)
3090 info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
3091 if (info->tmp_rbuf == NULL)
3096 static void free_tmp_rbuf(struct slgt_info *info)
3098 kfree(info->tmp_rbuf);
3099 info->tmp_rbuf = NULL;
3103 * allocate DMA descriptor lists.
3105 static int alloc_desc(struct slgt_info *info)
3110 /* allocate memory to hold descriptor lists */
3111 info->bufs = pci_alloc_consistent(info->pdev, DESC_LIST_SIZE, &info->bufs_dma_addr);
3112 if (info->bufs == NULL)
3115 memset(info->bufs, 0, DESC_LIST_SIZE);
3117 info->rbufs = (struct slgt_desc*)info->bufs;
3118 info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
3120 pbufs = (unsigned int)info->bufs_dma_addr;
3123 * Build circular lists of descriptors
3126 for (i=0; i < info->rbuf_count; i++) {
3127 /* physical address of this descriptor */
3128 info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
3130 /* physical address of next descriptor */
3131 if (i == info->rbuf_count - 1)
3132 info->rbufs[i].next = cpu_to_le32(pbufs);
3134 info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
3135 set_desc_count(info->rbufs[i], DMABUFSIZE);
3138 for (i=0; i < info->tbuf_count; i++) {
3139 /* physical address of this descriptor */
3140 info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
3142 /* physical address of next descriptor */
3143 if (i == info->tbuf_count - 1)
3144 info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
3146 info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
3152 static void free_desc(struct slgt_info *info)
3154 if (info->bufs != NULL) {
3155 pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
3162 static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3165 for (i=0; i < count; i++) {
3166 if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
3168 bufs[i].pbuf = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
3173 static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3176 for (i=0; i < count; i++) {
3177 if (bufs[i].buf == NULL)
3179 pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
3184 static int alloc_dma_bufs(struct slgt_info *info)
3186 info->rbuf_count = 32;
3187 info->tbuf_count = 32;
3189 if (alloc_desc(info) < 0 ||
3190 alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
3191 alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
3192 alloc_tmp_rbuf(info) < 0) {
3193 DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
3200 static void free_dma_bufs(struct slgt_info *info)
3203 free_bufs(info, info->rbufs, info->rbuf_count);
3204 free_bufs(info, info->tbufs, info->tbuf_count);
3207 free_tmp_rbuf(info);
3210 static int claim_resources(struct slgt_info *info)
3212 if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
3213 DBGERR(("%s reg addr conflict, addr=%08X\n",
3214 info->device_name, info->phys_reg_addr));
3215 info->init_error = DiagStatus_AddressConflict;
3219 info->reg_addr_requested = 1;
3221 info->reg_addr = ioremap(info->phys_reg_addr, SLGT_REG_SIZE);
3222 if (!info->reg_addr) {
3223 DBGERR(("%s cant map device registers, addr=%08X\n",
3224 info->device_name, info->phys_reg_addr));
3225 info->init_error = DiagStatus_CantAssignPciResources;
3231 release_resources(info);
3235 static void release_resources(struct slgt_info *info)
3237 if (info->irq_requested) {
3238 free_irq(info->irq_level, info);
3239 info->irq_requested = 0;
3242 if (info->reg_addr_requested) {
3243 release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
3244 info->reg_addr_requested = 0;
3247 if (info->reg_addr) {
3248 iounmap(info->reg_addr);
3249 info->reg_addr = NULL;
3253 /* Add the specified device instance data structure to the
3254 * global linked list of devices and increment the device count.
3256 static void add_device(struct slgt_info *info)
3260 info->next_device = NULL;
3261 info->line = slgt_device_count;
3262 sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
3264 if (info->line < MAX_DEVICES) {
3265 if (maxframe[info->line])
3266 info->max_frame_size = maxframe[info->line];
3267 info->dosyncppp = dosyncppp[info->line];
3270 slgt_device_count++;
3272 if (!slgt_device_list)
3273 slgt_device_list = info;
3275 struct slgt_info *current_dev = slgt_device_list;
3276 while(current_dev->next_device)
3277 current_dev = current_dev->next_device;
3278 current_dev->next_device = info;
3281 if (info->max_frame_size < 4096)
3282 info->max_frame_size = 4096;
3283 else if (info->max_frame_size > 65535)
3284 info->max_frame_size = 65535;
3286 switch(info->pdev->device) {
3287 case SYNCLINK_GT_DEVICE_ID:
3290 case SYNCLINK_GT2_DEVICE_ID:
3293 case SYNCLINK_GT4_DEVICE_ID:
3296 case SYNCLINK_AC_DEVICE_ID:
3298 info->params.mode = MGSL_MODE_ASYNC;
3301 devstr = "(unknown model)";
3303 printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
3304 devstr, info->device_name, info->phys_reg_addr,
3305 info->irq_level, info->max_frame_size);
3307 #if SYNCLINK_GENERIC_HDLC
3313 * allocate device instance structure, return NULL on failure
3315 static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
3317 struct slgt_info *info;
3319 info = kmalloc(sizeof(struct slgt_info), GFP_KERNEL);
3322 DBGERR(("%s device alloc failed adapter=%d port=%d\n",
3323 driver_name, adapter_num, port_num));
3325 memset(info, 0, sizeof(struct slgt_info));
3326 info->magic = MGSL_MAGIC;
3327 INIT_WORK(&info->task, bh_handler);
3328 info->max_frame_size = 4096;
3329 info->raw_rx_size = DMABUFSIZE;
3330 info->close_delay = 5*HZ/10;
3331 info->closing_wait = 30*HZ;
3332 init_waitqueue_head(&info->open_wait);
3333 init_waitqueue_head(&info->close_wait);
3334 init_waitqueue_head(&info->status_event_wait_q);
3335 init_waitqueue_head(&info->event_wait_q);
3336 spin_lock_init(&info->netlock);
3337 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
3338 info->idle_mode = HDLC_TXIDLE_FLAGS;
3339 info->adapter_num = adapter_num;
3340 info->port_num = port_num;
3342 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
3343 setup_timer(&info->rx_timer, rx_timeout, (unsigned long)info);
3345 /* Copy configuration info to device instance data */
3347 info->irq_level = pdev->irq;
3348 info->phys_reg_addr = pci_resource_start(pdev,0);
3350 info->bus_type = MGSL_BUS_TYPE_PCI;
3351 info->irq_flags = IRQF_SHARED;
3353 info->init_error = -1; /* assume error, set to 0 on successful init */
3359 static void device_init(int adapter_num, struct pci_dev *pdev)
3361 struct slgt_info *port_array[SLGT_MAX_PORTS];
3365 if (pdev->device == SYNCLINK_GT2_DEVICE_ID)
3367 else if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
3370 /* allocate device instances for all ports */
3371 for (i=0; i < port_count; ++i) {
3372 port_array[i] = alloc_dev(adapter_num, i, pdev);
3373 if (port_array[i] == NULL) {
3374 for (--i; i >= 0; --i)
3375 kfree(port_array[i]);
3380 /* give copy of port_array to all ports and add to device list */
3381 for (i=0; i < port_count; ++i) {
3382 memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
3383 add_device(port_array[i]);
3384 port_array[i]->port_count = port_count;
3385 spin_lock_init(&port_array[i]->lock);
3388 /* Allocate and claim adapter resources */
3389 if (!claim_resources(port_array[0])) {
3391 alloc_dma_bufs(port_array[0]);
3393 /* copy resource information from first port to others */
3394 for (i = 1; i < port_count; ++i) {
3395 port_array[i]->lock = port_array[0]->lock;
3396 port_array[i]->irq_level = port_array[0]->irq_level;
3397 port_array[i]->reg_addr = port_array[0]->reg_addr;
3398 alloc_dma_bufs(port_array[i]);
3401 if (request_irq(port_array[0]->irq_level,
3403 port_array[0]->irq_flags,
3404 port_array[0]->device_name,
3405 port_array[0]) < 0) {
3406 DBGERR(("%s request_irq failed IRQ=%d\n",
3407 port_array[0]->device_name,
3408 port_array[0]->irq_level));
3410 port_array[0]->irq_requested = 1;
3411 adapter_test(port_array[0]);
3412 for (i=1 ; i < port_count ; i++) {
3413 port_array[i]->init_error = port_array[0]->init_error;
3414 port_array[i]->gpio_present = port_array[0]->gpio_present;
3419 for (i=0; i < port_count; ++i)
3420 tty_register_device(serial_driver, port_array[i]->line, &(port_array[i]->pdev->dev));
3423 static int __devinit init_one(struct pci_dev *dev,
3424 const struct pci_device_id *ent)
3426 if (pci_enable_device(dev)) {
3427 printk("error enabling pci device %p\n", dev);
3430 pci_set_master(dev);
3431 device_init(slgt_device_count, dev);
3435 static void __devexit remove_one(struct pci_dev *dev)
3439 static const struct tty_operations ops = {
3443 .put_char = put_char,
3444 .flush_chars = flush_chars,
3445 .write_room = write_room,
3446 .chars_in_buffer = chars_in_buffer,
3447 .flush_buffer = flush_buffer,
3449 .throttle = throttle,
3450 .unthrottle = unthrottle,
3451 .send_xchar = send_xchar,
3452 .break_ctl = set_break,
3453 .wait_until_sent = wait_until_sent,
3454 .read_proc = read_proc,
3455 .set_termios = set_termios,
3457 .start = tx_release,
3459 .tiocmget = tiocmget,
3460 .tiocmset = tiocmset,
3463 static void slgt_cleanup(void)
3466 struct slgt_info *info;
3467 struct slgt_info *tmp;
3469 printk("unload %s %s\n", driver_name, driver_version);
3471 if (serial_driver) {
3472 for (info=slgt_device_list ; info != NULL ; info=info->next_device)
3473 tty_unregister_device(serial_driver, info->line);
3474 if ((rc = tty_unregister_driver(serial_driver)))
3475 DBGERR(("tty_unregister_driver error=%d\n", rc));
3476 put_tty_driver(serial_driver);
3480 info = slgt_device_list;
3483 info = info->next_device;
3486 /* release devices */
3487 info = slgt_device_list;
3489 #if SYNCLINK_GENERIC_HDLC
3492 free_dma_bufs(info);
3493 free_tmp_rbuf(info);
3494 if (info->port_num == 0)
3495 release_resources(info);
3497 info = info->next_device;
3502 pci_unregister_driver(&pci_driver);
3506 * Driver initialization entry point.
3508 static int __init slgt_init(void)
3512 printk("%s %s\n", driver_name, driver_version);
3514 serial_driver = alloc_tty_driver(MAX_DEVICES);
3515 if (!serial_driver) {
3516 printk("%s can't allocate tty driver\n", driver_name);
3520 /* Initialize the tty_driver structure */
3522 serial_driver->owner = THIS_MODULE;
3523 serial_driver->driver_name = tty_driver_name;
3524 serial_driver->name = tty_dev_prefix;
3525 serial_driver->major = ttymajor;
3526 serial_driver->minor_start = 64;
3527 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3528 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3529 serial_driver->init_termios = tty_std_termios;
3530 serial_driver->init_termios.c_cflag =
3531 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3532 serial_driver->init_termios.c_ispeed = 9600;
3533 serial_driver->init_termios.c_ospeed = 9600;
3534 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3535 tty_set_operations(serial_driver, &ops);
3536 if ((rc = tty_register_driver(serial_driver)) < 0) {
3537 DBGERR(("%s can't register serial driver\n", driver_name));
3538 put_tty_driver(serial_driver);
3539 serial_driver = NULL;
3543 printk("%s %s, tty major#%d\n",
3544 driver_name, driver_version,
3545 serial_driver->major);
3547 slgt_device_count = 0;
3548 if ((rc = pci_register_driver(&pci_driver)) < 0) {
3549 printk("%s pci_register_driver error=%d\n", driver_name, rc);
3554 if (!slgt_device_list)
3555 printk("%s no devices found\n",driver_name);
3564 static void __exit slgt_exit(void)
3569 module_init(slgt_init);
3570 module_exit(slgt_exit);
3573 * register access routines
3576 #define CALC_REGADDR() \
3577 unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
3579 reg_addr += (info->port_num) * 32;
3581 static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
3584 return readb((void __iomem *)reg_addr);
3587 static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
3590 writeb(value, (void __iomem *)reg_addr);
3593 static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
3596 return readw((void __iomem *)reg_addr);
3599 static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
3602 writew(value, (void __iomem *)reg_addr);
3605 static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
3608 return readl((void __iomem *)reg_addr);
3611 static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
3614 writel(value, (void __iomem *)reg_addr);
3617 static void rdma_reset(struct slgt_info *info)
3622 wr_reg32(info, RDCSR, BIT1);
3624 /* wait for enable bit cleared */
3625 for(i=0 ; i < 1000 ; i++)
3626 if (!(rd_reg32(info, RDCSR) & BIT0))
3630 static void tdma_reset(struct slgt_info *info)
3635 wr_reg32(info, TDCSR, BIT1);
3637 /* wait for enable bit cleared */
3638 for(i=0 ; i < 1000 ; i++)
3639 if (!(rd_reg32(info, TDCSR) & BIT0))
3644 * enable internal loopback
3645 * TxCLK and RxCLK are generated from BRG
3646 * and TxD is looped back to RxD internally.
3648 static void enable_loopback(struct slgt_info *info)
3650 /* SCR (serial control) BIT2=looopback enable */
3651 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
3653 if (info->params.mode != MGSL_MODE_ASYNC) {
3654 /* CCR (clock control)
3655 * 07..05 tx clock source (010 = BRG)
3656 * 04..02 rx clock source (010 = BRG)
3657 * 01 auxclk enable (0 = disable)
3658 * 00 BRG enable (1 = enable)
3662 wr_reg8(info, CCR, 0x49);
3664 /* set speed if available, otherwise use default */
3665 if (info->params.clock_speed)
3666 set_rate(info, info->params.clock_speed);
3668 set_rate(info, 3686400);
3673 * set baud rate generator to specified rate
3675 static void set_rate(struct slgt_info *info, u32 rate)
3678 static unsigned int osc = 14745600;
3680 /* div = osc/rate - 1
3682 * Round div up if osc/rate is not integer to
3683 * force to next slowest rate.
3688 if (!(osc % rate) && div)
3690 wr_reg16(info, BDR, (unsigned short)div);
3694 static void rx_stop(struct slgt_info *info)
3698 /* disable and reset receiver */
3699 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3700 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3701 wr_reg16(info, RCR, val); /* clear reset bit */
3703 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
3705 /* clear pending rx interrupts */
3706 wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
3710 info->rx_enabled = 0;
3711 info->rx_restart = 0;
3714 static void rx_start(struct slgt_info *info)
3718 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
3720 /* clear pending rx overrun IRQ */
3721 wr_reg16(info, SSR, IRQ_RXOVER);
3723 /* reset and disable receiver */
3724 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3725 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3726 wr_reg16(info, RCR, val); /* clear reset bit */
3731 /* set 1st descriptor address */
3732 wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
3734 if (info->params.mode != MGSL_MODE_ASYNC) {
3735 /* enable rx DMA and DMA interrupt */
3736 wr_reg32(info, RDCSR, (BIT2 + BIT0));
3738 /* enable saving of rx status, rx DMA and DMA interrupt */
3739 wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
3742 slgt_irq_on(info, IRQ_RXOVER);
3744 /* enable receiver */
3745 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
3747 info->rx_restart = 0;
3748 info->rx_enabled = 1;
3751 static void tx_start(struct slgt_info *info)
3753 if (!info->tx_enabled) {
3755 (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
3756 info->tx_enabled = TRUE;
3759 if (info->tx_count) {
3760 info->drop_rts_on_tx_done = 0;
3762 if (info->params.mode != MGSL_MODE_ASYNC) {
3763 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3765 if (!(info->signals & SerialSignal_RTS)) {
3766 info->signals |= SerialSignal_RTS;
3768 info->drop_rts_on_tx_done = 1;
3772 slgt_irq_off(info, IRQ_TXDATA);
3773 slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
3774 /* clear tx idle and underrun status bits */
3775 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
3777 if (!(rd_reg32(info, TDCSR) & BIT0)) {
3778 /* tx DMA stopped, restart tx DMA */
3780 /* set 1st descriptor address */
3781 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
3782 switch(info->params.mode) {
3784 case MGSL_MODE_MONOSYNC:
3785 case MGSL_MODE_BISYNC:
3786 wr_reg32(info, TDCSR, BIT2 + BIT0); /* IRQ + DMA enable */
3789 wr_reg32(info, TDCSR, BIT0); /* DMA enable */
3793 if (info->params.mode == MGSL_MODE_HDLC)
3794 mod_timer(&info->tx_timer, jiffies +
3795 msecs_to_jiffies(5000));
3798 /* set 1st descriptor address */
3799 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
3801 slgt_irq_off(info, IRQ_TXDATA);
3802 slgt_irq_on(info, IRQ_TXIDLE);
3803 /* clear tx idle status bit */
3804 wr_reg16(info, SSR, IRQ_TXIDLE);
3807 wr_reg32(info, TDCSR, BIT0);
3810 info->tx_active = 1;
3814 static void tx_stop(struct slgt_info *info)
3818 del_timer(&info->tx_timer);
3822 /* reset and disable transmitter */
3823 val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
3824 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
3826 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
3828 /* clear tx idle and underrun status bit */
3829 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
3833 info->tx_enabled = 0;
3834 info->tx_active = 0;
3837 static void reset_port(struct slgt_info *info)
3839 if (!info->reg_addr)
3845 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
3848 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
3851 static void reset_adapter(struct slgt_info *info)
3854 for (i=0; i < info->port_count; ++i) {
3855 if (info->port_array[i])
3856 reset_port(info->port_array[i]);
3860 static void async_mode(struct slgt_info *info)
3864 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
3870 * 15..13 mode, 010=async
3871 * 12..10 encoding, 000=NRZ
3873 * 08 1=odd parity, 0=even parity
3874 * 07 1=RTS driver control
3876 * 05..04 character length
3881 * 03 0=1 stop bit, 1=2 stop bits
3884 * 00 auto-CTS enable
3888 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
3891 if (info->params.parity != ASYNC_PARITY_NONE) {
3893 if (info->params.parity == ASYNC_PARITY_ODD)
3897 switch (info->params.data_bits)
3899 case 6: val |= BIT4; break;
3900 case 7: val |= BIT5; break;
3901 case 8: val |= BIT5 + BIT4; break;
3904 if (info->params.stop_bits != 1)
3907 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3910 wr_reg16(info, TCR, val);
3914 * 15..13 mode, 010=async
3915 * 12..10 encoding, 000=NRZ
3917 * 08 1=odd parity, 0=even parity
3918 * 07..06 reserved, must be 0
3919 * 05..04 character length
3924 * 03 reserved, must be zero
3927 * 00 auto-DCD enable
3931 if (info->params.parity != ASYNC_PARITY_NONE) {
3933 if (info->params.parity == ASYNC_PARITY_ODD)
3937 switch (info->params.data_bits)
3939 case 6: val |= BIT4; break;
3940 case 7: val |= BIT5; break;
3941 case 8: val |= BIT5 + BIT4; break;
3944 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3947 wr_reg16(info, RCR, val);
3949 /* CCR (clock control)
3951 * 07..05 011 = tx clock source is BRG/16
3952 * 04..02 010 = rx clock source is BRG
3953 * 01 0 = auxclk disabled
3954 * 00 1 = BRG enabled
3958 wr_reg8(info, CCR, 0x69);
3962 /* SCR (serial control)
3964 * 15 1=tx req on FIFO half empty
3965 * 14 1=rx req on FIFO half full
3966 * 13 tx data IRQ enable
3967 * 12 tx idle IRQ enable
3968 * 11 rx break on IRQ enable
3969 * 10 rx data IRQ enable
3970 * 09 rx break off IRQ enable
3971 * 08 overrun IRQ enable
3976 * 03 reserved, must be zero
3977 * 02 1=txd->rxd internal loopback enable
3978 * 01 reserved, must be zero
3979 * 00 1=master IRQ enable
3981 val = BIT15 + BIT14 + BIT0;
3982 wr_reg16(info, SCR, val);
3984 slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
3986 set_rate(info, info->params.data_rate * 16);
3988 if (info->params.loopback)
3989 enable_loopback(info);
3992 static void sync_mode(struct slgt_info *info)
3996 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4002 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4006 * 07 1=RTS driver control
4007 * 06 preamble enable
4008 * 05..04 preamble length
4009 * 03 share open/close flag
4012 * 00 auto-CTS enable
4016 switch(info->params.mode) {
4017 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4018 case MGSL_MODE_BISYNC: val |= BIT15; break;
4019 case MGSL_MODE_RAW: val |= BIT13; break;
4021 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4024 switch(info->params.encoding)
4026 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4027 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4028 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4029 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4030 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4031 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4032 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4035 switch (info->params.crc_type & HDLC_CRC_MASK)
4037 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4038 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4041 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
4044 switch (info->params.preamble_length)
4046 case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
4047 case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
4048 case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
4051 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4054 wr_reg16(info, TCR, val);
4056 /* TPR (transmit preamble) */
4058 switch (info->params.preamble)
4060 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
4061 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
4062 case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
4063 case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
4064 case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
4065 default: val = 0x7e; break;
4067 wr_reg8(info, TPR, (unsigned char)val);
4071 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4075 * 07..03 reserved, must be 0
4078 * 00 auto-DCD enable
4082 switch(info->params.mode) {
4083 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4084 case MGSL_MODE_BISYNC: val |= BIT15; break;
4085 case MGSL_MODE_RAW: val |= BIT13; break;
4088 switch(info->params.encoding)
4090 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4091 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4092 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4093 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4094 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4095 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4096 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4099 switch (info->params.crc_type & HDLC_CRC_MASK)
4101 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4102 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4105 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4108 wr_reg16(info, RCR, val);
4110 /* CCR (clock control)
4112 * 07..05 tx clock source
4113 * 04..02 rx clock source
4119 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4121 // when RxC source is DPLL, BRG generates 16X DPLL
4122 // reference clock, so take TxC from BRG/16 to get
4123 // transmit clock at actual data rate
4124 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4125 val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
4127 val |= BIT6; /* 010, txclk = BRG */
4129 else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4130 val |= BIT7; /* 100, txclk = DPLL Input */
4131 else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4132 val |= BIT5; /* 001, txclk = RXC Input */
4134 if (info->params.flags & HDLC_FLAG_RXC_BRG)
4135 val |= BIT3; /* 010, rxclk = BRG */
4136 else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4137 val |= BIT4; /* 100, rxclk = DPLL */
4138 else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4139 val |= BIT2; /* 001, rxclk = TXC Input */
4141 if (info->params.clock_speed)
4144 wr_reg8(info, CCR, (unsigned char)val);
4146 if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
4148 // program DPLL mode
4149 switch(info->params.encoding)
4151 case HDLC_ENCODING_BIPHASE_MARK:
4152 case HDLC_ENCODING_BIPHASE_SPACE:
4154 case HDLC_ENCODING_BIPHASE_LEVEL:
4155 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
4156 val = BIT7 + BIT6; break;
4157 default: val = BIT6; // NRZ encodings
4159 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
4161 // DPLL requires a 16X reference clock from BRG
4162 set_rate(info, info->params.clock_speed * 16);
4165 set_rate(info, info->params.clock_speed);
4171 /* SCR (serial control)
4173 * 15 1=tx req on FIFO half empty
4174 * 14 1=rx req on FIFO half full
4175 * 13 tx data IRQ enable
4176 * 12 tx idle IRQ enable
4177 * 11 underrun IRQ enable
4178 * 10 rx data IRQ enable
4179 * 09 rx idle IRQ enable
4180 * 08 overrun IRQ enable
4185 * 03 reserved, must be zero
4186 * 02 1=txd->rxd internal loopback enable
4187 * 01 reserved, must be zero
4188 * 00 1=master IRQ enable
4190 wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
4192 if (info->params.loopback)
4193 enable_loopback(info);
4197 * set transmit idle mode
4199 static void tx_set_idle(struct slgt_info *info)
4204 /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
4205 * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
4207 tcr = rd_reg16(info, TCR);
4208 if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
4209 /* disable preamble, set idle size to 16 bits */
4210 tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
4211 /* MSB of 16 bit idle specified in tx preamble register (TPR) */
4212 wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
4213 } else if (!(tcr & BIT6)) {
4214 /* preamble is disabled, set idle size to 8 bits */
4215 tcr &= ~(BIT5 + BIT4);
4217 wr_reg16(info, TCR, tcr);
4219 if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
4220 /* LSB of custom tx idle specified in tx idle register */
4221 val = (unsigned char)(info->idle_mode & 0xff);
4223 /* standard 8 bit idle patterns */
4224 switch(info->idle_mode)
4226 case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
4227 case HDLC_TXIDLE_ALT_ZEROS_ONES:
4228 case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
4229 case HDLC_TXIDLE_ZEROS:
4230 case HDLC_TXIDLE_SPACE: val = 0x00; break;
4231 default: val = 0xff;
4235 wr_reg8(info, TIR, val);
4239 * get state of V24 status (input) signals
4241 static void get_signals(struct slgt_info *info)
4243 unsigned short status = rd_reg16(info, SSR);
4245 /* clear all serial signals except DTR and RTS */
4246 info->signals &= SerialSignal_DTR + SerialSignal_RTS;
4249 info->signals |= SerialSignal_DSR;
4251 info->signals |= SerialSignal_CTS;
4253 info->signals |= SerialSignal_DCD;
4255 info->signals |= SerialSignal_RI;
4259 * set V.24 Control Register based on current configuration
4261 static void msc_set_vcr(struct slgt_info *info)
4263 unsigned char val = 0;
4265 /* VCR (V.24 control)
4267 * 07..04 serial IF select
4274 switch(info->if_mode & MGSL_INTERFACE_MASK)
4276 case MGSL_INTERFACE_RS232:
4277 val |= BIT5; /* 0010 */
4279 case MGSL_INTERFACE_V35:
4280 val |= BIT7 + BIT6 + BIT5; /* 1110 */
4282 case MGSL_INTERFACE_RS422:
4283 val |= BIT6; /* 0100 */
4287 if (info->signals & SerialSignal_DTR)
4289 if (info->signals & SerialSignal_RTS)
4291 if (info->if_mode & MGSL_INTERFACE_LL)
4293 if (info->if_mode & MGSL_INTERFACE_RL)
4295 wr_reg8(info, VCR, val);
4299 * set state of V24 control (output) signals
4301 static void set_signals(struct slgt_info *info)
4303 unsigned char val = rd_reg8(info, VCR);
4304 if (info->signals & SerialSignal_DTR)
4308 if (info->signals & SerialSignal_RTS)
4312 wr_reg8(info, VCR, val);
4316 * free range of receive DMA buffers (i to last)
4318 static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
4323 /* reset current buffer for reuse */
4324 info->rbufs[i].status = 0;
4325 switch(info->params.mode) {
4327 case MGSL_MODE_MONOSYNC:
4328 case MGSL_MODE_BISYNC:
4329 set_desc_count(info->rbufs[i], info->raw_rx_size);
4332 set_desc_count(info->rbufs[i], DMABUFSIZE);
4337 if (++i == info->rbuf_count)
4340 info->rbuf_current = i;
4344 * mark all receive DMA buffers as free
4346 static void reset_rbufs(struct slgt_info *info)
4348 free_rbufs(info, 0, info->rbuf_count - 1);
4352 * pass receive HDLC frame to upper layer
4354 * return 1 if frame available, otherwise 0
4356 static int rx_get_frame(struct slgt_info *info)
4358 unsigned int start, end;
4359 unsigned short status;
4360 unsigned int framesize = 0;
4362 unsigned long flags;
4363 struct tty_struct *tty = info->tty;
4364 unsigned char addr_field = 0xff;
4365 unsigned int crc_size = 0;
4367 switch (info->params.crc_type & HDLC_CRC_MASK) {
4368 case HDLC_CRC_16_CCITT: crc_size = 2; break;
4369 case HDLC_CRC_32_CCITT: crc_size = 4; break;
4376 start = end = info->rbuf_current;
4379 if (!desc_complete(info->rbufs[end]))
4382 if (framesize == 0 && info->params.addr_filter != 0xff)
4383 addr_field = info->rbufs[end].buf[0];
4385 framesize += desc_count(info->rbufs[end]);
4387 if (desc_eof(info->rbufs[end]))
4390 if (++end == info->rbuf_count)
4393 if (end == info->rbuf_current) {
4394 if (info->rx_enabled){
4395 spin_lock_irqsave(&info->lock,flags);
4397 spin_unlock_irqrestore(&info->lock,flags);
4405 * 15 buffer complete
4408 * 02 eof (end of frame)
4412 status = desc_status(info->rbufs[end]);
4414 /* ignore CRC bit if not using CRC (bit is undefined) */
4415 if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
4418 if (framesize == 0 ||
4419 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4420 free_rbufs(info, start, end);
4424 if (framesize < (2 + crc_size) || status & BIT0) {
4425 info->icount.rxshort++;
4427 } else if (status & BIT1) {
4428 info->icount.rxcrc++;
4429 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
4433 #if SYNCLINK_GENERIC_HDLC
4434 if (framesize == 0) {
4435 struct net_device_stats *stats = hdlc_stats(info->netdev);
4437 stats->rx_frame_errors++;
4441 DBGBH(("%s rx frame status=%04X size=%d\n",
4442 info->device_name, status, framesize));
4443 DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, DMABUFSIZE), "rx");
4446 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
4447 framesize -= crc_size;
4451 if (framesize > info->max_frame_size + crc_size)
4452 info->icount.rxlong++;
4454 /* copy dma buffer(s) to contiguous temp buffer */
4455 int copy_count = framesize;
4457 unsigned char *p = info->tmp_rbuf;
4458 info->tmp_rbuf_count = framesize;
4460 info->icount.rxok++;
4463 int partial_count = min(copy_count, DMABUFSIZE);
4464 memcpy(p, info->rbufs[i].buf, partial_count);
4466 copy_count -= partial_count;
4467 if (++i == info->rbuf_count)
4471 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4472 *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
4476 #if SYNCLINK_GENERIC_HDLC
4478 hdlcdev_rx(info,info->tmp_rbuf, framesize);
4481 ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
4484 free_rbufs(info, start, end);
4492 * pass receive buffer (RAW synchronous mode) to tty layer
4493 * return 1 if buffer available, otherwise 0
4495 static int rx_get_buf(struct slgt_info *info)
4497 unsigned int i = info->rbuf_current;
4500 if (!desc_complete(info->rbufs[i]))
4502 count = desc_count(info->rbufs[i]);
4503 switch(info->params.mode) {
4504 case MGSL_MODE_MONOSYNC:
4505 case MGSL_MODE_BISYNC:
4506 /* ignore residue in byte synchronous modes */
4507 if (desc_residue(info->rbufs[i]))
4511 DBGDATA(info, info->rbufs[i].buf, count, "rx");
4512 DBGINFO(("rx_get_buf size=%d\n", count));
4514 ldisc_receive_buf(info->tty, info->rbufs[i].buf,
4515 info->flag_buf, count);
4516 free_rbufs(info, i, i);
4520 static void reset_tbufs(struct slgt_info *info)
4523 info->tbuf_current = 0;
4524 for (i=0 ; i < info->tbuf_count ; i++) {
4525 info->tbufs[i].status = 0;
4526 info->tbufs[i].count = 0;
4531 * return number of free transmit DMA buffers
4533 static unsigned int free_tbuf_count(struct slgt_info *info)
4535 unsigned int count = 0;
4536 unsigned int i = info->tbuf_current;
4540 if (desc_count(info->tbufs[i]))
4541 break; /* buffer in use */
4543 if (++i == info->tbuf_count)
4545 } while (i != info->tbuf_current);
4547 /* last buffer with zero count may be in use, assume it is */
4555 * load transmit DMA buffer(s) with data
4557 static void tx_load(struct slgt_info *info, const char *buf, unsigned int size)
4559 unsigned short count;
4561 struct slgt_desc *d;
4566 DBGDATA(info, buf, size, "tx");
4568 info->tbuf_start = i = info->tbuf_current;
4571 d = &info->tbufs[i];
4572 if (++i == info->tbuf_count)
4575 count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
4576 memcpy(d->buf, buf, count);
4582 * set EOF bit for last buffer of HDLC frame or
4583 * for every buffer in raw mode
4585 if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
4586 info->params.mode == MGSL_MODE_RAW)
4587 set_desc_eof(*d, 1);
4589 set_desc_eof(*d, 0);
4591 set_desc_count(*d, count);
4594 info->tbuf_current = i;
4597 static int register_test(struct slgt_info *info)
4599 static unsigned short patterns[] =
4600 {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
4601 static unsigned int count = sizeof(patterns)/sizeof(patterns[0]);
4605 for (i=0 ; i < count ; i++) {
4606 wr_reg16(info, TIR, patterns[i]);
4607 wr_reg16(info, BDR, patterns[(i+1)%count]);
4608 if ((rd_reg16(info, TIR) != patterns[i]) ||
4609 (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
4614 info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
4615 info->init_error = rc ? 0 : DiagStatus_AddressFailure;
4619 static int irq_test(struct slgt_info *info)
4621 unsigned long timeout;
4622 unsigned long flags;
4623 struct tty_struct *oldtty = info->tty;
4624 u32 speed = info->params.data_rate;
4626 info->params.data_rate = 921600;
4629 spin_lock_irqsave(&info->lock, flags);
4631 slgt_irq_on(info, IRQ_TXIDLE);
4633 /* enable transmitter */
4635 (unsigned short)(rd_reg16(info, TCR) | BIT1));
4637 /* write one byte and wait for tx idle */
4638 wr_reg16(info, TDR, 0);
4640 /* assume failure */
4641 info->init_error = DiagStatus_IrqFailure;
4642 info->irq_occurred = FALSE;
4644 spin_unlock_irqrestore(&info->lock, flags);
4647 while(timeout-- && !info->irq_occurred)
4648 msleep_interruptible(10);
4650 spin_lock_irqsave(&info->lock,flags);
4652 spin_unlock_irqrestore(&info->lock,flags);
4654 info->params.data_rate = speed;
4657 info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
4658 return info->irq_occurred ? 0 : -ENODEV;
4661 static int loopback_test_rx(struct slgt_info *info)
4663 unsigned char *src, *dest;
4666 if (desc_complete(info->rbufs[0])) {
4667 count = desc_count(info->rbufs[0]);
4668 src = info->rbufs[0].buf;
4669 dest = info->tmp_rbuf;
4671 for( ; count ; count-=2, src+=2) {
4672 /* src=data byte (src+1)=status byte */
4673 if (!(*(src+1) & (BIT9 + BIT8))) {
4676 info->tmp_rbuf_count++;
4679 DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
4685 static int loopback_test(struct slgt_info *info)
4687 #define TESTFRAMESIZE 20
4689 unsigned long timeout;
4690 u16 count = TESTFRAMESIZE;
4691 unsigned char buf[TESTFRAMESIZE];
4693 unsigned long flags;
4695 struct tty_struct *oldtty = info->tty;
4698 memcpy(¶ms, &info->params, sizeof(params));
4700 info->params.mode = MGSL_MODE_ASYNC;
4701 info->params.data_rate = 921600;
4702 info->params.loopback = 1;
4705 /* build and send transmit frame */
4706 for (count = 0; count < TESTFRAMESIZE; ++count)
4707 buf[count] = (unsigned char)count;
4709 info->tmp_rbuf_count = 0;
4710 memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
4712 /* program hardware for HDLC and enabled receiver */
4713 spin_lock_irqsave(&info->lock,flags);
4716 info->tx_count = count;
4717 tx_load(info, buf, count);
4719 spin_unlock_irqrestore(&info->lock, flags);
4721 /* wait for receive complete */
4722 for (timeout = 100; timeout; --timeout) {
4723 msleep_interruptible(10);
4724 if (loopback_test_rx(info)) {
4730 /* verify received frame length and contents */
4731 if (!rc && (info->tmp_rbuf_count != count ||
4732 memcmp(buf, info->tmp_rbuf, count))) {
4736 spin_lock_irqsave(&info->lock,flags);
4737 reset_adapter(info);
4738 spin_unlock_irqrestore(&info->lock,flags);
4740 memcpy(&info->params, ¶ms, sizeof(info->params));
4743 info->init_error = rc ? DiagStatus_DmaFailure : 0;
4747 static int adapter_test(struct slgt_info *info)
4749 DBGINFO(("testing %s\n", info->device_name));
4750 if (register_test(info) < 0) {
4751 printk("register test failure %s addr=%08X\n",
4752 info->device_name, info->phys_reg_addr);
4753 } else if (irq_test(info) < 0) {
4754 printk("IRQ test failure %s IRQ=%d\n",
4755 info->device_name, info->irq_level);
4756 } else if (loopback_test(info) < 0) {
4757 printk("loopback test failure %s\n", info->device_name);
4759 return info->init_error;
4763 * transmit timeout handler
4765 static void tx_timeout(unsigned long context)
4767 struct slgt_info *info = (struct slgt_info*)context;
4768 unsigned long flags;
4770 DBGINFO(("%s tx_timeout\n", info->device_name));
4771 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
4772 info->icount.txtimeout++;
4774 spin_lock_irqsave(&info->lock,flags);
4775 info->tx_active = 0;
4777 spin_unlock_irqrestore(&info->lock,flags);
4779 #if SYNCLINK_GENERIC_HDLC
4781 hdlcdev_tx_done(info);
4788 * receive buffer polling timer
4790 static void rx_timeout(unsigned long context)
4792 struct slgt_info *info = (struct slgt_info*)context;
4793 unsigned long flags;
4795 DBGINFO(("%s rx_timeout\n", info->device_name));
4796 spin_lock_irqsave(&info->lock, flags);
4797 info->pending_bh |= BH_RECEIVE;
4798 spin_unlock_irqrestore(&info->lock, flags);
4799 bh_handler(&info->task);