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);
1048 wake_up_interruptible(&tty->write_wait);
1053 * throttle (stop) transmitter
1055 static void tx_hold(struct tty_struct *tty)
1057 struct slgt_info *info = tty->driver_data;
1058 unsigned long flags;
1060 if (sanity_check(info, tty->name, "tx_hold"))
1062 DBGINFO(("%s tx_hold\n", info->device_name));
1063 spin_lock_irqsave(&info->lock,flags);
1064 if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
1066 spin_unlock_irqrestore(&info->lock,flags);
1070 * release (start) transmitter
1072 static void tx_release(struct tty_struct *tty)
1074 struct slgt_info *info = tty->driver_data;
1075 unsigned long flags;
1077 if (sanity_check(info, tty->name, "tx_release"))
1079 DBGINFO(("%s tx_release\n", info->device_name));
1080 spin_lock_irqsave(&info->lock,flags);
1081 if (!info->tx_active && info->tx_count) {
1082 tx_load(info, info->tx_buf, info->tx_count);
1085 spin_unlock_irqrestore(&info->lock,flags);
1089 * Service an IOCTL request
1093 * tty pointer to tty instance data
1094 * file pointer to associated file object for device
1095 * cmd IOCTL command code
1096 * arg command argument/context
1098 * Return 0 if success, otherwise error code
1100 static int ioctl(struct tty_struct *tty, struct file *file,
1101 unsigned int cmd, unsigned long arg)
1103 struct slgt_info *info = tty->driver_data;
1104 struct mgsl_icount cnow; /* kernel counter temps */
1105 struct serial_icounter_struct __user *p_cuser; /* user space */
1106 unsigned long flags;
1107 void __user *argp = (void __user *)arg;
1109 if (sanity_check(info, tty->name, "ioctl"))
1111 DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
1113 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1114 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1115 if (tty->flags & (1 << TTY_IO_ERROR))
1120 case MGSL_IOCGPARAMS:
1121 return get_params(info, argp);
1122 case MGSL_IOCSPARAMS:
1123 return set_params(info, argp);
1124 case MGSL_IOCGTXIDLE:
1125 return get_txidle(info, argp);
1126 case MGSL_IOCSTXIDLE:
1127 return set_txidle(info, (int)arg);
1128 case MGSL_IOCTXENABLE:
1129 return tx_enable(info, (int)arg);
1130 case MGSL_IOCRXENABLE:
1131 return rx_enable(info, (int)arg);
1132 case MGSL_IOCTXABORT:
1133 return tx_abort(info);
1134 case MGSL_IOCGSTATS:
1135 return get_stats(info, argp);
1136 case MGSL_IOCWAITEVENT:
1137 return wait_mgsl_event(info, argp);
1139 return modem_input_wait(info,(int)arg);
1141 return get_interface(info, argp);
1143 return set_interface(info,(int)arg);
1145 return set_gpio(info, argp);
1147 return get_gpio(info, argp);
1148 case MGSL_IOCWAITGPIO:
1149 return wait_gpio(info, argp);
1151 spin_lock_irqsave(&info->lock,flags);
1152 cnow = info->icount;
1153 spin_unlock_irqrestore(&info->lock,flags);
1155 if (put_user(cnow.cts, &p_cuser->cts) ||
1156 put_user(cnow.dsr, &p_cuser->dsr) ||
1157 put_user(cnow.rng, &p_cuser->rng) ||
1158 put_user(cnow.dcd, &p_cuser->dcd) ||
1159 put_user(cnow.rx, &p_cuser->rx) ||
1160 put_user(cnow.tx, &p_cuser->tx) ||
1161 put_user(cnow.frame, &p_cuser->frame) ||
1162 put_user(cnow.overrun, &p_cuser->overrun) ||
1163 put_user(cnow.parity, &p_cuser->parity) ||
1164 put_user(cnow.brk, &p_cuser->brk) ||
1165 put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1169 return -ENOIOCTLCMD;
1177 static inline int line_info(char *buf, struct slgt_info *info)
1181 unsigned long flags;
1183 ret = sprintf(buf, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
1184 info->device_name, info->phys_reg_addr,
1185 info->irq_level, info->max_frame_size);
1187 /* output current serial signal states */
1188 spin_lock_irqsave(&info->lock,flags);
1190 spin_unlock_irqrestore(&info->lock,flags);
1194 if (info->signals & SerialSignal_RTS)
1195 strcat(stat_buf, "|RTS");
1196 if (info->signals & SerialSignal_CTS)
1197 strcat(stat_buf, "|CTS");
1198 if (info->signals & SerialSignal_DTR)
1199 strcat(stat_buf, "|DTR");
1200 if (info->signals & SerialSignal_DSR)
1201 strcat(stat_buf, "|DSR");
1202 if (info->signals & SerialSignal_DCD)
1203 strcat(stat_buf, "|CD");
1204 if (info->signals & SerialSignal_RI)
1205 strcat(stat_buf, "|RI");
1207 if (info->params.mode != MGSL_MODE_ASYNC) {
1208 ret += sprintf(buf+ret, "\tHDLC txok:%d rxok:%d",
1209 info->icount.txok, info->icount.rxok);
1210 if (info->icount.txunder)
1211 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
1212 if (info->icount.txabort)
1213 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
1214 if (info->icount.rxshort)
1215 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
1216 if (info->icount.rxlong)
1217 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
1218 if (info->icount.rxover)
1219 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
1220 if (info->icount.rxcrc)
1221 ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
1223 ret += sprintf(buf+ret, "\tASYNC tx:%d rx:%d",
1224 info->icount.tx, info->icount.rx);
1225 if (info->icount.frame)
1226 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
1227 if (info->icount.parity)
1228 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
1229 if (info->icount.brk)
1230 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
1231 if (info->icount.overrun)
1232 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
1235 /* Append serial signal status to end */
1236 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
1238 ret += sprintf(buf+ret, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1239 info->tx_active,info->bh_requested,info->bh_running,
1245 /* Called to print information about devices
1247 static int read_proc(char *page, char **start, off_t off, int count,
1248 int *eof, void *data)
1252 struct slgt_info *info;
1254 len += sprintf(page, "synclink_gt driver:%s\n", driver_version);
1256 info = slgt_device_list;
1258 l = line_info(page + len, info);
1260 if (len+begin > off+count)
1262 if (len+begin < off) {
1266 info = info->next_device;
1271 if (off >= len+begin)
1273 *start = page + (off-begin);
1274 return ((count < begin+len-off) ? count : begin+len-off);
1278 * return count of bytes in transmit buffer
1280 static int chars_in_buffer(struct tty_struct *tty)
1282 struct slgt_info *info = tty->driver_data;
1283 if (sanity_check(info, tty->name, "chars_in_buffer"))
1285 DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, info->tx_count));
1286 return info->tx_count;
1290 * signal remote device to throttle send data (our receive data)
1292 static void throttle(struct tty_struct * tty)
1294 struct slgt_info *info = tty->driver_data;
1295 unsigned long flags;
1297 if (sanity_check(info, tty->name, "throttle"))
1299 DBGINFO(("%s throttle\n", info->device_name));
1301 send_xchar(tty, STOP_CHAR(tty));
1302 if (tty->termios->c_cflag & CRTSCTS) {
1303 spin_lock_irqsave(&info->lock,flags);
1304 info->signals &= ~SerialSignal_RTS;
1306 spin_unlock_irqrestore(&info->lock,flags);
1311 * signal remote device to stop throttling send data (our receive data)
1313 static void unthrottle(struct tty_struct * tty)
1315 struct slgt_info *info = tty->driver_data;
1316 unsigned long flags;
1318 if (sanity_check(info, tty->name, "unthrottle"))
1320 DBGINFO(("%s unthrottle\n", info->device_name));
1325 send_xchar(tty, START_CHAR(tty));
1327 if (tty->termios->c_cflag & CRTSCTS) {
1328 spin_lock_irqsave(&info->lock,flags);
1329 info->signals |= SerialSignal_RTS;
1331 spin_unlock_irqrestore(&info->lock,flags);
1336 * set or clear transmit break condition
1337 * break_state -1=set break condition, 0=clear
1339 static void set_break(struct tty_struct *tty, int break_state)
1341 struct slgt_info *info = tty->driver_data;
1342 unsigned short value;
1343 unsigned long flags;
1345 if (sanity_check(info, tty->name, "set_break"))
1347 DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
1349 spin_lock_irqsave(&info->lock,flags);
1350 value = rd_reg16(info, TCR);
1351 if (break_state == -1)
1355 wr_reg16(info, TCR, value);
1356 spin_unlock_irqrestore(&info->lock,flags);
1359 #if SYNCLINK_GENERIC_HDLC
1362 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1363 * set encoding and frame check sequence (FCS) options
1365 * dev pointer to network device structure
1366 * encoding serial encoding setting
1367 * parity FCS setting
1369 * returns 0 if success, otherwise error code
1371 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
1372 unsigned short parity)
1374 struct slgt_info *info = dev_to_port(dev);
1375 unsigned char new_encoding;
1376 unsigned short new_crctype;
1378 /* return error if TTY interface open */
1382 DBGINFO(("%s hdlcdev_attach\n", info->device_name));
1386 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
1387 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
1388 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
1389 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
1390 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
1391 default: return -EINVAL;
1396 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
1397 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
1398 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
1399 default: return -EINVAL;
1402 info->params.encoding = new_encoding;
1403 info->params.crc_type = new_crctype;
1405 /* if network interface up, reprogram hardware */
1413 * called by generic HDLC layer to send frame
1415 * skb socket buffer containing HDLC frame
1416 * dev pointer to network device structure
1418 * returns 0 if success, otherwise error code
1420 static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
1422 struct slgt_info *info = dev_to_port(dev);
1423 struct net_device_stats *stats = hdlc_stats(dev);
1424 unsigned long flags;
1426 DBGINFO(("%s hdlc_xmit\n", dev->name));
1428 /* stop sending until this frame completes */
1429 netif_stop_queue(dev);
1431 /* copy data to device buffers */
1432 info->tx_count = skb->len;
1433 tx_load(info, skb->data, skb->len);
1435 /* update network statistics */
1436 stats->tx_packets++;
1437 stats->tx_bytes += skb->len;
1439 /* done with socket buffer, so free it */
1442 /* save start time for transmit timeout detection */
1443 dev->trans_start = jiffies;
1445 /* start hardware transmitter if necessary */
1446 spin_lock_irqsave(&info->lock,flags);
1447 if (!info->tx_active)
1449 spin_unlock_irqrestore(&info->lock,flags);
1455 * called by network layer when interface enabled
1456 * claim resources and initialize hardware
1458 * dev pointer to network device structure
1460 * returns 0 if success, otherwise error code
1462 static int hdlcdev_open(struct net_device *dev)
1464 struct slgt_info *info = dev_to_port(dev);
1466 unsigned long flags;
1468 DBGINFO(("%s hdlcdev_open\n", dev->name));
1470 /* generic HDLC layer open processing */
1471 if ((rc = hdlc_open(dev)))
1474 /* arbitrate between network and tty opens */
1475 spin_lock_irqsave(&info->netlock, flags);
1476 if (info->count != 0 || info->netcount != 0) {
1477 DBGINFO(("%s hdlc_open busy\n", dev->name));
1478 spin_unlock_irqrestore(&info->netlock, flags);
1482 spin_unlock_irqrestore(&info->netlock, flags);
1484 /* claim resources and init adapter */
1485 if ((rc = startup(info)) != 0) {
1486 spin_lock_irqsave(&info->netlock, flags);
1488 spin_unlock_irqrestore(&info->netlock, flags);
1492 /* assert DTR and RTS, apply hardware settings */
1493 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
1496 /* enable network layer transmit */
1497 dev->trans_start = jiffies;
1498 netif_start_queue(dev);
1500 /* inform generic HDLC layer of current DCD status */
1501 spin_lock_irqsave(&info->lock, flags);
1503 spin_unlock_irqrestore(&info->lock, flags);
1504 if (info->signals & SerialSignal_DCD)
1505 netif_carrier_on(dev);
1507 netif_carrier_off(dev);
1512 * called by network layer when interface is disabled
1513 * shutdown hardware and release resources
1515 * dev pointer to network device structure
1517 * returns 0 if success, otherwise error code
1519 static int hdlcdev_close(struct net_device *dev)
1521 struct slgt_info *info = dev_to_port(dev);
1522 unsigned long flags;
1524 DBGINFO(("%s hdlcdev_close\n", dev->name));
1526 netif_stop_queue(dev);
1528 /* shutdown adapter and release resources */
1533 spin_lock_irqsave(&info->netlock, flags);
1535 spin_unlock_irqrestore(&info->netlock, flags);
1541 * called by network layer to process IOCTL call to network device
1543 * dev pointer to network device structure
1544 * ifr pointer to network interface request structure
1545 * cmd IOCTL command code
1547 * returns 0 if success, otherwise error code
1549 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1551 const size_t size = sizeof(sync_serial_settings);
1552 sync_serial_settings new_line;
1553 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1554 struct slgt_info *info = dev_to_port(dev);
1557 DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
1559 /* return error if TTY interface open */
1563 if (cmd != SIOCWANDEV)
1564 return hdlc_ioctl(dev, ifr, cmd);
1566 switch(ifr->ifr_settings.type) {
1567 case IF_GET_IFACE: /* return current sync_serial_settings */
1569 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1570 if (ifr->ifr_settings.size < size) {
1571 ifr->ifr_settings.size = size; /* data size wanted */
1575 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1576 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1577 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1578 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1581 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
1582 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
1583 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
1584 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
1585 default: new_line.clock_type = CLOCK_DEFAULT;
1588 new_line.clock_rate = info->params.clock_speed;
1589 new_line.loopback = info->params.loopback ? 1:0;
1591 if (copy_to_user(line, &new_line, size))
1595 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
1597 if(!capable(CAP_NET_ADMIN))
1599 if (copy_from_user(&new_line, line, size))
1602 switch (new_line.clock_type)
1604 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
1605 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
1606 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
1607 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
1608 case CLOCK_DEFAULT: flags = info->params.flags &
1609 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1610 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1611 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1612 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
1613 default: return -EINVAL;
1616 if (new_line.loopback != 0 && new_line.loopback != 1)
1619 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1620 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1621 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1622 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1623 info->params.flags |= flags;
1625 info->params.loopback = new_line.loopback;
1627 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
1628 info->params.clock_speed = new_line.clock_rate;
1630 info->params.clock_speed = 0;
1632 /* if network interface up, reprogram hardware */
1638 return hdlc_ioctl(dev, ifr, cmd);
1643 * called by network layer when transmit timeout is detected
1645 * dev pointer to network device structure
1647 static void hdlcdev_tx_timeout(struct net_device *dev)
1649 struct slgt_info *info = dev_to_port(dev);
1650 struct net_device_stats *stats = hdlc_stats(dev);
1651 unsigned long flags;
1653 DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
1656 stats->tx_aborted_errors++;
1658 spin_lock_irqsave(&info->lock,flags);
1660 spin_unlock_irqrestore(&info->lock,flags);
1662 netif_wake_queue(dev);
1666 * called by device driver when transmit completes
1667 * reenable network layer transmit if stopped
1669 * info pointer to device instance information
1671 static void hdlcdev_tx_done(struct slgt_info *info)
1673 if (netif_queue_stopped(info->netdev))
1674 netif_wake_queue(info->netdev);
1678 * called by device driver when frame received
1679 * pass frame to network layer
1681 * info pointer to device instance information
1682 * buf pointer to buffer contianing frame data
1683 * size count of data bytes in buf
1685 static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
1687 struct sk_buff *skb = dev_alloc_skb(size);
1688 struct net_device *dev = info->netdev;
1689 struct net_device_stats *stats = hdlc_stats(dev);
1691 DBGINFO(("%s hdlcdev_rx\n", dev->name));
1694 DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
1695 stats->rx_dropped++;
1699 memcpy(skb_put(skb, size),buf,size);
1701 skb->protocol = hdlc_type_trans(skb, info->netdev);
1703 stats->rx_packets++;
1704 stats->rx_bytes += size;
1708 info->netdev->last_rx = jiffies;
1712 * called by device driver when adding device instance
1713 * do generic HDLC initialization
1715 * info pointer to device instance information
1717 * returns 0 if success, otherwise error code
1719 static int hdlcdev_init(struct slgt_info *info)
1722 struct net_device *dev;
1725 /* allocate and initialize network and HDLC layer objects */
1727 if (!(dev = alloc_hdlcdev(info))) {
1728 printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
1732 /* for network layer reporting purposes only */
1733 dev->mem_start = info->phys_reg_addr;
1734 dev->mem_end = info->phys_reg_addr + SLGT_REG_SIZE - 1;
1735 dev->irq = info->irq_level;
1737 /* network layer callbacks and settings */
1738 dev->do_ioctl = hdlcdev_ioctl;
1739 dev->open = hdlcdev_open;
1740 dev->stop = hdlcdev_close;
1741 dev->tx_timeout = hdlcdev_tx_timeout;
1742 dev->watchdog_timeo = 10*HZ;
1743 dev->tx_queue_len = 50;
1745 /* generic HDLC layer callbacks and settings */
1746 hdlc = dev_to_hdlc(dev);
1747 hdlc->attach = hdlcdev_attach;
1748 hdlc->xmit = hdlcdev_xmit;
1750 /* register objects with HDLC layer */
1751 if ((rc = register_hdlc_device(dev))) {
1752 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
1762 * called by device driver when removing device instance
1763 * do generic HDLC cleanup
1765 * info pointer to device instance information
1767 static void hdlcdev_exit(struct slgt_info *info)
1769 unregister_hdlc_device(info->netdev);
1770 free_netdev(info->netdev);
1771 info->netdev = NULL;
1774 #endif /* ifdef CONFIG_HDLC */
1777 * get async data from rx DMA buffers
1779 static void rx_async(struct slgt_info *info)
1781 struct tty_struct *tty = info->tty;
1782 struct mgsl_icount *icount = &info->icount;
1783 unsigned int start, end;
1785 unsigned char status;
1786 struct slgt_desc *bufs = info->rbufs;
1792 start = end = info->rbuf_current;
1794 while(desc_complete(bufs[end])) {
1795 count = desc_count(bufs[end]) - info->rbuf_index;
1796 p = bufs[end].buf + info->rbuf_index;
1798 DBGISR(("%s rx_async count=%d\n", info->device_name, count));
1799 DBGDATA(info, p, count, "rx");
1801 for(i=0 ; i < count; i+=2, p+=2) {
1807 if ((status = *(p+1) & (BIT1 + BIT0))) {
1810 else if (status & BIT0)
1812 /* discard char if tty control flags say so */
1813 if (status & info->ignore_status_mask)
1817 else if (status & BIT0)
1821 tty_insert_flip_char(tty, ch, stat);
1827 /* receive buffer not completed */
1828 info->rbuf_index += i;
1829 info->rx_timer.expires = jiffies + 1;
1830 add_timer(&info->rx_timer);
1834 info->rbuf_index = 0;
1835 free_rbufs(info, end, end);
1837 if (++end == info->rbuf_count)
1840 /* if entire list searched then no frame available */
1846 tty_flip_buffer_push(tty);
1850 * return next bottom half action to perform
1852 static int bh_action(struct slgt_info *info)
1854 unsigned long flags;
1857 spin_lock_irqsave(&info->lock,flags);
1859 if (info->pending_bh & BH_RECEIVE) {
1860 info->pending_bh &= ~BH_RECEIVE;
1862 } else if (info->pending_bh & BH_TRANSMIT) {
1863 info->pending_bh &= ~BH_TRANSMIT;
1865 } else if (info->pending_bh & BH_STATUS) {
1866 info->pending_bh &= ~BH_STATUS;
1869 /* Mark BH routine as complete */
1870 info->bh_running = 0;
1871 info->bh_requested = 0;
1875 spin_unlock_irqrestore(&info->lock,flags);
1881 * perform bottom half processing
1883 static void bh_handler(struct work_struct *work)
1885 struct slgt_info *info = container_of(work, struct slgt_info, task);
1890 info->bh_running = 1;
1892 while((action = bh_action(info))) {
1895 DBGBH(("%s bh receive\n", info->device_name));
1896 switch(info->params.mode) {
1897 case MGSL_MODE_ASYNC:
1900 case MGSL_MODE_HDLC:
1901 while(rx_get_frame(info));
1904 case MGSL_MODE_MONOSYNC:
1905 case MGSL_MODE_BISYNC:
1906 while(rx_get_buf(info));
1909 /* restart receiver if rx DMA buffers exhausted */
1910 if (info->rx_restart)
1917 DBGBH(("%s bh status\n", info->device_name));
1918 info->ri_chkcount = 0;
1919 info->dsr_chkcount = 0;
1920 info->dcd_chkcount = 0;
1921 info->cts_chkcount = 0;
1924 DBGBH(("%s unknown action\n", info->device_name));
1928 DBGBH(("%s bh_handler exit\n", info->device_name));
1931 static void bh_transmit(struct slgt_info *info)
1933 struct tty_struct *tty = info->tty;
1935 DBGBH(("%s bh_transmit\n", info->device_name));
1938 wake_up_interruptible(&tty->write_wait);
1942 static void dsr_change(struct slgt_info *info)
1945 DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
1946 if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
1947 slgt_irq_off(info, IRQ_DSR);
1951 if (info->signals & SerialSignal_DSR)
1952 info->input_signal_events.dsr_up++;
1954 info->input_signal_events.dsr_down++;
1955 wake_up_interruptible(&info->status_event_wait_q);
1956 wake_up_interruptible(&info->event_wait_q);
1957 info->pending_bh |= BH_STATUS;
1960 static void cts_change(struct slgt_info *info)
1963 DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
1964 if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
1965 slgt_irq_off(info, IRQ_CTS);
1969 if (info->signals & SerialSignal_CTS)
1970 info->input_signal_events.cts_up++;
1972 info->input_signal_events.cts_down++;
1973 wake_up_interruptible(&info->status_event_wait_q);
1974 wake_up_interruptible(&info->event_wait_q);
1975 info->pending_bh |= BH_STATUS;
1977 if (info->flags & ASYNC_CTS_FLOW) {
1979 if (info->tty->hw_stopped) {
1980 if (info->signals & SerialSignal_CTS) {
1981 info->tty->hw_stopped = 0;
1982 info->pending_bh |= BH_TRANSMIT;
1986 if (!(info->signals & SerialSignal_CTS))
1987 info->tty->hw_stopped = 1;
1993 static void dcd_change(struct slgt_info *info)
1996 DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
1997 if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
1998 slgt_irq_off(info, IRQ_DCD);
2002 if (info->signals & SerialSignal_DCD) {
2003 info->input_signal_events.dcd_up++;
2005 info->input_signal_events.dcd_down++;
2007 #if SYNCLINK_GENERIC_HDLC
2008 if (info->netcount) {
2009 if (info->signals & SerialSignal_DCD)
2010 netif_carrier_on(info->netdev);
2012 netif_carrier_off(info->netdev);
2015 wake_up_interruptible(&info->status_event_wait_q);
2016 wake_up_interruptible(&info->event_wait_q);
2017 info->pending_bh |= BH_STATUS;
2019 if (info->flags & ASYNC_CHECK_CD) {
2020 if (info->signals & SerialSignal_DCD)
2021 wake_up_interruptible(&info->open_wait);
2024 tty_hangup(info->tty);
2029 static void ri_change(struct slgt_info *info)
2032 DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
2033 if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2034 slgt_irq_off(info, IRQ_RI);
2038 if (info->signals & SerialSignal_RI) {
2039 info->input_signal_events.ri_up++;
2041 info->input_signal_events.ri_down++;
2043 wake_up_interruptible(&info->status_event_wait_q);
2044 wake_up_interruptible(&info->event_wait_q);
2045 info->pending_bh |= BH_STATUS;
2048 static void isr_serial(struct slgt_info *info)
2050 unsigned short status = rd_reg16(info, SSR);
2052 DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
2054 wr_reg16(info, SSR, status); /* clear pending */
2056 info->irq_occurred = 1;
2058 if (info->params.mode == MGSL_MODE_ASYNC) {
2059 if (status & IRQ_TXIDLE) {
2061 isr_txeom(info, status);
2063 if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
2065 /* process break detection if tty control allows */
2067 if (!(status & info->ignore_status_mask)) {
2068 if (info->read_status_mask & MASK_BREAK) {
2069 tty_insert_flip_char(info->tty, 0, TTY_BREAK);
2070 if (info->flags & ASYNC_SAK)
2077 if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
2078 isr_txeom(info, status);
2080 if (status & IRQ_RXIDLE) {
2081 if (status & RXIDLE)
2082 info->icount.rxidle++;
2084 info->icount.exithunt++;
2085 wake_up_interruptible(&info->event_wait_q);
2088 if (status & IRQ_RXOVER)
2092 if (status & IRQ_DSR)
2094 if (status & IRQ_CTS)
2096 if (status & IRQ_DCD)
2098 if (status & IRQ_RI)
2102 static void isr_rdma(struct slgt_info *info)
2104 unsigned int status = rd_reg32(info, RDCSR);
2106 DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
2108 /* RDCSR (rx DMA control/status)
2111 * 06 save status byte to DMA buffer
2113 * 04 eol (end of list)
2114 * 03 eob (end of buffer)
2119 wr_reg32(info, RDCSR, status); /* clear pending */
2121 if (status & (BIT5 + BIT4)) {
2122 DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
2123 info->rx_restart = 1;
2125 info->pending_bh |= BH_RECEIVE;
2128 static void isr_tdma(struct slgt_info *info)
2130 unsigned int status = rd_reg32(info, TDCSR);
2132 DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
2134 /* TDCSR (tx DMA control/status)
2138 * 04 eol (end of list)
2139 * 03 eob (end of buffer)
2144 wr_reg32(info, TDCSR, status); /* clear pending */
2146 if (status & (BIT5 + BIT4 + BIT3)) {
2147 // another transmit buffer has completed
2148 // run bottom half to get more send data from user
2149 info->pending_bh |= BH_TRANSMIT;
2153 static void isr_txeom(struct slgt_info *info, unsigned short status)
2155 DBGISR(("%s txeom status=%04x\n", info->device_name, status));
2157 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
2160 if (status & IRQ_TXUNDER) {
2161 unsigned short val = rd_reg16(info, TCR);
2162 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
2163 wr_reg16(info, TCR, val); /* clear reset bit */
2166 if (info->tx_active) {
2167 if (info->params.mode != MGSL_MODE_ASYNC) {
2168 if (status & IRQ_TXUNDER)
2169 info->icount.txunder++;
2170 else if (status & IRQ_TXIDLE)
2171 info->icount.txok++;
2174 info->tx_active = 0;
2177 del_timer(&info->tx_timer);
2179 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
2180 info->signals &= ~SerialSignal_RTS;
2181 info->drop_rts_on_tx_done = 0;
2185 #if SYNCLINK_GENERIC_HDLC
2187 hdlcdev_tx_done(info);
2191 if (info->tty && (info->tty->stopped || info->tty->hw_stopped)) {
2195 info->pending_bh |= BH_TRANSMIT;
2200 static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
2202 struct cond_wait *w, *prev;
2204 /* wake processes waiting for specific transitions */
2205 for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
2206 if (w->data & changed) {
2208 wake_up_interruptible(&w->q);
2210 prev->next = w->next;
2212 info->gpio_wait_q = w->next;
2218 /* interrupt service routine
2220 * irq interrupt number
2221 * dev_id device ID supplied during interrupt registration
2223 static irqreturn_t slgt_interrupt(int irq, void *dev_id)
2225 struct slgt_info *info;
2229 DBGISR(("slgt_interrupt irq=%d entry\n", irq));
2235 spin_lock(&info->lock);
2237 while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
2238 DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
2239 info->irq_occurred = 1;
2240 for(i=0; i < info->port_count ; i++) {
2241 if (info->port_array[i] == NULL)
2243 if (gsr & (BIT8 << i))
2244 isr_serial(info->port_array[i]);
2245 if (gsr & (BIT16 << (i*2)))
2246 isr_rdma(info->port_array[i]);
2247 if (gsr & (BIT17 << (i*2)))
2248 isr_tdma(info->port_array[i]);
2252 if (info->gpio_present) {
2254 unsigned int changed;
2255 while ((changed = rd_reg32(info, IOSR)) != 0) {
2256 DBGISR(("%s iosr=%08x\n", info->device_name, changed));
2257 /* read latched state of GPIO signals */
2258 state = rd_reg32(info, IOVR);
2259 /* clear pending GPIO interrupt bits */
2260 wr_reg32(info, IOSR, changed);
2261 for (i=0 ; i < info->port_count ; i++) {
2262 if (info->port_array[i] != NULL)
2263 isr_gpio(info->port_array[i], changed, state);
2268 for(i=0; i < info->port_count ; i++) {
2269 struct slgt_info *port = info->port_array[i];
2271 if (port && (port->count || port->netcount) &&
2272 port->pending_bh && !port->bh_running &&
2273 !port->bh_requested) {
2274 DBGISR(("%s bh queued\n", port->device_name));
2275 schedule_work(&port->task);
2276 port->bh_requested = 1;
2280 spin_unlock(&info->lock);
2282 DBGISR(("slgt_interrupt irq=%d exit\n", irq));
2286 static int startup(struct slgt_info *info)
2288 DBGINFO(("%s startup\n", info->device_name));
2290 if (info->flags & ASYNC_INITIALIZED)
2293 if (!info->tx_buf) {
2294 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
2295 if (!info->tx_buf) {
2296 DBGERR(("%s can't allocate tx buffer\n", info->device_name));
2301 info->pending_bh = 0;
2303 memset(&info->icount, 0, sizeof(info->icount));
2305 /* program hardware for current parameters */
2306 change_params(info);
2309 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2311 info->flags |= ASYNC_INITIALIZED;
2317 * called by close() and hangup() to shutdown hardware
2319 static void shutdown(struct slgt_info *info)
2321 unsigned long flags;
2323 if (!(info->flags & ASYNC_INITIALIZED))
2326 DBGINFO(("%s shutdown\n", info->device_name));
2328 /* clear status wait queue because status changes */
2329 /* can't happen after shutting down the hardware */
2330 wake_up_interruptible(&info->status_event_wait_q);
2331 wake_up_interruptible(&info->event_wait_q);
2333 del_timer_sync(&info->tx_timer);
2334 del_timer_sync(&info->rx_timer);
2336 kfree(info->tx_buf);
2337 info->tx_buf = NULL;
2339 spin_lock_irqsave(&info->lock,flags);
2344 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
2346 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
2347 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2351 flush_cond_wait(&info->gpio_wait_q);
2353 spin_unlock_irqrestore(&info->lock,flags);
2356 set_bit(TTY_IO_ERROR, &info->tty->flags);
2358 info->flags &= ~ASYNC_INITIALIZED;
2361 static void program_hw(struct slgt_info *info)
2363 unsigned long flags;
2365 spin_lock_irqsave(&info->lock,flags);
2370 if (info->params.mode != MGSL_MODE_ASYNC ||
2378 info->dcd_chkcount = 0;
2379 info->cts_chkcount = 0;
2380 info->ri_chkcount = 0;
2381 info->dsr_chkcount = 0;
2383 slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR);
2386 if (info->netcount ||
2387 (info->tty && info->tty->termios->c_cflag & CREAD))
2390 spin_unlock_irqrestore(&info->lock,flags);
2394 * reconfigure adapter based on new parameters
2396 static void change_params(struct slgt_info *info)
2401 if (!info->tty || !info->tty->termios)
2403 DBGINFO(("%s change_params\n", info->device_name));
2405 cflag = info->tty->termios->c_cflag;
2407 /* if B0 rate (hangup) specified then negate DTR and RTS */
2408 /* otherwise assert DTR and RTS */
2410 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
2412 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2414 /* byte size and parity */
2416 switch (cflag & CSIZE) {
2417 case CS5: info->params.data_bits = 5; break;
2418 case CS6: info->params.data_bits = 6; break;
2419 case CS7: info->params.data_bits = 7; break;
2420 case CS8: info->params.data_bits = 8; break;
2421 default: info->params.data_bits = 7; break;
2424 info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
2427 info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
2429 info->params.parity = ASYNC_PARITY_NONE;
2431 /* calculate number of jiffies to transmit a full
2432 * FIFO (32 bytes) at specified data rate
2434 bits_per_char = info->params.data_bits +
2435 info->params.stop_bits + 1;
2437 info->params.data_rate = tty_get_baud_rate(info->tty);
2439 if (info->params.data_rate) {
2440 info->timeout = (32*HZ*bits_per_char) /
2441 info->params.data_rate;
2443 info->timeout += HZ/50; /* Add .02 seconds of slop */
2445 if (cflag & CRTSCTS)
2446 info->flags |= ASYNC_CTS_FLOW;
2448 info->flags &= ~ASYNC_CTS_FLOW;
2451 info->flags &= ~ASYNC_CHECK_CD;
2453 info->flags |= ASYNC_CHECK_CD;
2455 /* process tty input control flags */
2457 info->read_status_mask = IRQ_RXOVER;
2458 if (I_INPCK(info->tty))
2459 info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
2460 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2461 info->read_status_mask |= MASK_BREAK;
2462 if (I_IGNPAR(info->tty))
2463 info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
2464 if (I_IGNBRK(info->tty)) {
2465 info->ignore_status_mask |= MASK_BREAK;
2466 /* If ignoring parity and break indicators, ignore
2467 * overruns too. (For real raw support).
2469 if (I_IGNPAR(info->tty))
2470 info->ignore_status_mask |= MASK_OVERRUN;
2476 static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
2478 DBGINFO(("%s get_stats\n", info->device_name));
2480 memset(&info->icount, 0, sizeof(info->icount));
2482 if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
2488 static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
2490 DBGINFO(("%s get_params\n", info->device_name));
2491 if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
2496 static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
2498 unsigned long flags;
2499 MGSL_PARAMS tmp_params;
2501 DBGINFO(("%s set_params\n", info->device_name));
2502 if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
2505 spin_lock_irqsave(&info->lock, flags);
2506 memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
2507 spin_unlock_irqrestore(&info->lock, flags);
2509 change_params(info);
2514 static int get_txidle(struct slgt_info *info, int __user *idle_mode)
2516 DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
2517 if (put_user(info->idle_mode, idle_mode))
2522 static int set_txidle(struct slgt_info *info, int idle_mode)
2524 unsigned long flags;
2525 DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
2526 spin_lock_irqsave(&info->lock,flags);
2527 info->idle_mode = idle_mode;
2528 if (info->params.mode != MGSL_MODE_ASYNC)
2530 spin_unlock_irqrestore(&info->lock,flags);
2534 static int tx_enable(struct slgt_info *info, int enable)
2536 unsigned long flags;
2537 DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
2538 spin_lock_irqsave(&info->lock,flags);
2540 if (!info->tx_enabled)
2543 if (info->tx_enabled)
2546 spin_unlock_irqrestore(&info->lock,flags);
2551 * abort transmit HDLC frame
2553 static int tx_abort(struct slgt_info *info)
2555 unsigned long flags;
2556 DBGINFO(("%s tx_abort\n", info->device_name));
2557 spin_lock_irqsave(&info->lock,flags);
2559 spin_unlock_irqrestore(&info->lock,flags);
2563 static int rx_enable(struct slgt_info *info, int enable)
2565 unsigned long flags;
2566 DBGINFO(("%s rx_enable(%d)\n", info->device_name, enable));
2567 spin_lock_irqsave(&info->lock,flags);
2569 if (!info->rx_enabled)
2571 else if (enable == 2) {
2572 /* force hunt mode (write 1 to RCR[3]) */
2573 wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
2576 if (info->rx_enabled)
2579 spin_unlock_irqrestore(&info->lock,flags);
2584 * wait for specified event to occur
2586 static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
2588 unsigned long flags;
2591 struct mgsl_icount cprev, cnow;
2594 struct _input_signal_events oldsigs, newsigs;
2595 DECLARE_WAITQUEUE(wait, current);
2597 if (get_user(mask, mask_ptr))
2600 DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
2602 spin_lock_irqsave(&info->lock,flags);
2604 /* return immediately if state matches requested events */
2609 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2610 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2611 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2612 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2614 spin_unlock_irqrestore(&info->lock,flags);
2618 /* save current irq counts */
2619 cprev = info->icount;
2620 oldsigs = info->input_signal_events;
2622 /* enable hunt and idle irqs if needed */
2623 if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
2624 unsigned short val = rd_reg16(info, SCR);
2625 if (!(val & IRQ_RXIDLE))
2626 wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
2629 set_current_state(TASK_INTERRUPTIBLE);
2630 add_wait_queue(&info->event_wait_q, &wait);
2632 spin_unlock_irqrestore(&info->lock,flags);
2636 if (signal_pending(current)) {
2641 /* get current irq counts */
2642 spin_lock_irqsave(&info->lock,flags);
2643 cnow = info->icount;
2644 newsigs = info->input_signal_events;
2645 set_current_state(TASK_INTERRUPTIBLE);
2646 spin_unlock_irqrestore(&info->lock,flags);
2648 /* if no change, wait aborted for some reason */
2649 if (newsigs.dsr_up == oldsigs.dsr_up &&
2650 newsigs.dsr_down == oldsigs.dsr_down &&
2651 newsigs.dcd_up == oldsigs.dcd_up &&
2652 newsigs.dcd_down == oldsigs.dcd_down &&
2653 newsigs.cts_up == oldsigs.cts_up &&
2654 newsigs.cts_down == oldsigs.cts_down &&
2655 newsigs.ri_up == oldsigs.ri_up &&
2656 newsigs.ri_down == oldsigs.ri_down &&
2657 cnow.exithunt == cprev.exithunt &&
2658 cnow.rxidle == cprev.rxidle) {
2664 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2665 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2666 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2667 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2668 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2669 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2670 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2671 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2672 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2673 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2681 remove_wait_queue(&info->event_wait_q, &wait);
2682 set_current_state(TASK_RUNNING);
2685 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2686 spin_lock_irqsave(&info->lock,flags);
2687 if (!waitqueue_active(&info->event_wait_q)) {
2688 /* disable enable exit hunt mode/idle rcvd IRQs */
2690 (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
2692 spin_unlock_irqrestore(&info->lock,flags);
2696 rc = put_user(events, mask_ptr);
2700 static int get_interface(struct slgt_info *info, int __user *if_mode)
2702 DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
2703 if (put_user(info->if_mode, if_mode))
2708 static int set_interface(struct slgt_info *info, int if_mode)
2710 unsigned long flags;
2713 DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
2714 spin_lock_irqsave(&info->lock,flags);
2715 info->if_mode = if_mode;
2719 /* TCR (tx control) 07 1=RTS driver control */
2720 val = rd_reg16(info, TCR);
2721 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
2725 wr_reg16(info, TCR, val);
2727 spin_unlock_irqrestore(&info->lock,flags);
2732 * set general purpose IO pin state and direction
2735 * state each bit indicates a pin state
2736 * smask set bit indicates pin state to set
2737 * dir each bit indicates a pin direction (0=input, 1=output)
2738 * dmask set bit indicates pin direction to set
2740 static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2742 unsigned long flags;
2743 struct gpio_desc gpio;
2746 if (!info->gpio_present)
2748 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2750 DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
2751 info->device_name, gpio.state, gpio.smask,
2752 gpio.dir, gpio.dmask));
2754 spin_lock_irqsave(&info->lock,flags);
2756 data = rd_reg32(info, IODR);
2757 data |= gpio.dmask & gpio.dir;
2758 data &= ~(gpio.dmask & ~gpio.dir);
2759 wr_reg32(info, IODR, data);
2762 data = rd_reg32(info, IOVR);
2763 data |= gpio.smask & gpio.state;
2764 data &= ~(gpio.smask & ~gpio.state);
2765 wr_reg32(info, IOVR, data);
2767 spin_unlock_irqrestore(&info->lock,flags);
2773 * get general purpose IO pin state and direction
2775 static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2777 struct gpio_desc gpio;
2778 if (!info->gpio_present)
2780 gpio.state = rd_reg32(info, IOVR);
2781 gpio.smask = 0xffffffff;
2782 gpio.dir = rd_reg32(info, IODR);
2783 gpio.dmask = 0xffffffff;
2784 if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
2786 DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
2787 info->device_name, gpio.state, gpio.dir));
2792 * conditional wait facility
2794 static void init_cond_wait(struct cond_wait *w, unsigned int data)
2796 init_waitqueue_head(&w->q);
2797 init_waitqueue_entry(&w->wait, current);
2801 static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
2803 set_current_state(TASK_INTERRUPTIBLE);
2804 add_wait_queue(&w->q, &w->wait);
2809 static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
2811 struct cond_wait *w, *prev;
2812 remove_wait_queue(&cw->q, &cw->wait);
2813 set_current_state(TASK_RUNNING);
2814 for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
2817 prev->next = w->next;
2825 static void flush_cond_wait(struct cond_wait **head)
2827 while (*head != NULL) {
2828 wake_up_interruptible(&(*head)->q);
2829 *head = (*head)->next;
2834 * wait for general purpose I/O pin(s) to enter specified state
2837 * state - bit indicates target pin state
2838 * smask - set bit indicates watched pin
2840 * The wait ends when at least one watched pin enters the specified
2841 * state. When 0 (no error) is returned, user_gpio->state is set to the
2842 * state of all GPIO pins when the wait ends.
2844 * Note: Each pin may be a dedicated input, dedicated output, or
2845 * configurable input/output. The number and configuration of pins
2846 * varies with the specific adapter model. Only input pins (dedicated
2847 * or configured) can be monitored with this function.
2849 static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2851 unsigned long flags;
2853 struct gpio_desc gpio;
2854 struct cond_wait wait;
2857 if (!info->gpio_present)
2859 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2861 DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
2862 info->device_name, gpio.state, gpio.smask));
2863 /* ignore output pins identified by set IODR bit */
2864 if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
2866 init_cond_wait(&wait, gpio.smask);
2868 spin_lock_irqsave(&info->lock, flags);
2869 /* enable interrupts for watched pins */
2870 wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
2871 /* get current pin states */
2872 state = rd_reg32(info, IOVR);
2874 if (gpio.smask & ~(state ^ gpio.state)) {
2875 /* already in target state */
2878 /* wait for target state */
2879 add_cond_wait(&info->gpio_wait_q, &wait);
2880 spin_unlock_irqrestore(&info->lock, flags);
2882 if (signal_pending(current))
2885 gpio.state = wait.data;
2886 spin_lock_irqsave(&info->lock, flags);
2887 remove_cond_wait(&info->gpio_wait_q, &wait);
2890 /* disable all GPIO interrupts if no waiting processes */
2891 if (info->gpio_wait_q == NULL)
2892 wr_reg32(info, IOER, 0);
2893 spin_unlock_irqrestore(&info->lock,flags);
2895 if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
2900 static int modem_input_wait(struct slgt_info *info,int arg)
2902 unsigned long flags;
2904 struct mgsl_icount cprev, cnow;
2905 DECLARE_WAITQUEUE(wait, current);
2907 /* save current irq counts */
2908 spin_lock_irqsave(&info->lock,flags);
2909 cprev = info->icount;
2910 add_wait_queue(&info->status_event_wait_q, &wait);
2911 set_current_state(TASK_INTERRUPTIBLE);
2912 spin_unlock_irqrestore(&info->lock,flags);
2916 if (signal_pending(current)) {
2921 /* get new irq counts */
2922 spin_lock_irqsave(&info->lock,flags);
2923 cnow = info->icount;
2924 set_current_state(TASK_INTERRUPTIBLE);
2925 spin_unlock_irqrestore(&info->lock,flags);
2927 /* if no change, wait aborted for some reason */
2928 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2929 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2934 /* check for change in caller specified modem input */
2935 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2936 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2937 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2938 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2945 remove_wait_queue(&info->status_event_wait_q, &wait);
2946 set_current_state(TASK_RUNNING);
2951 * return state of serial control and status signals
2953 static int tiocmget(struct tty_struct *tty, struct file *file)
2955 struct slgt_info *info = tty->driver_data;
2956 unsigned int result;
2957 unsigned long flags;
2959 spin_lock_irqsave(&info->lock,flags);
2961 spin_unlock_irqrestore(&info->lock,flags);
2963 result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2964 ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2965 ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2966 ((info->signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2967 ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2968 ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2970 DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
2975 * set modem control signals (DTR/RTS)
2977 * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
2978 * TIOCMSET = set/clear signal values
2979 * value bit mask for command
2981 static int tiocmset(struct tty_struct *tty, struct file *file,
2982 unsigned int set, unsigned int clear)
2984 struct slgt_info *info = tty->driver_data;
2985 unsigned long flags;
2987 DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
2989 if (set & TIOCM_RTS)
2990 info->signals |= SerialSignal_RTS;
2991 if (set & TIOCM_DTR)
2992 info->signals |= SerialSignal_DTR;
2993 if (clear & TIOCM_RTS)
2994 info->signals &= ~SerialSignal_RTS;
2995 if (clear & TIOCM_DTR)
2996 info->signals &= ~SerialSignal_DTR;
2998 spin_lock_irqsave(&info->lock,flags);
3000 spin_unlock_irqrestore(&info->lock,flags);
3005 * block current process until the device is ready to open
3007 static int block_til_ready(struct tty_struct *tty, struct file *filp,
3008 struct slgt_info *info)
3010 DECLARE_WAITQUEUE(wait, current);
3012 int do_clocal = 0, extra_count = 0;
3013 unsigned long flags;
3015 DBGINFO(("%s block_til_ready\n", tty->driver->name));
3017 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3018 /* nonblock mode is set or port is not enabled */
3019 info->flags |= ASYNC_NORMAL_ACTIVE;
3023 if (tty->termios->c_cflag & CLOCAL)
3026 /* Wait for carrier detect and the line to become
3027 * free (i.e., not in use by the callout). While we are in
3028 * this loop, info->count is dropped by one, so that
3029 * close() knows when to free things. We restore it upon
3030 * exit, either normal or abnormal.
3034 add_wait_queue(&info->open_wait, &wait);
3036 spin_lock_irqsave(&info->lock, flags);
3037 if (!tty_hung_up_p(filp)) {
3041 spin_unlock_irqrestore(&info->lock, flags);
3042 info->blocked_open++;
3045 if ((tty->termios->c_cflag & CBAUD)) {
3046 spin_lock_irqsave(&info->lock,flags);
3047 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
3049 spin_unlock_irqrestore(&info->lock,flags);
3052 set_current_state(TASK_INTERRUPTIBLE);
3054 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
3055 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
3056 -EAGAIN : -ERESTARTSYS;
3060 spin_lock_irqsave(&info->lock,flags);
3062 spin_unlock_irqrestore(&info->lock,flags);
3064 if (!(info->flags & ASYNC_CLOSING) &&
3065 (do_clocal || (info->signals & SerialSignal_DCD)) ) {
3069 if (signal_pending(current)) {
3070 retval = -ERESTARTSYS;
3074 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
3078 set_current_state(TASK_RUNNING);
3079 remove_wait_queue(&info->open_wait, &wait);
3083 info->blocked_open--;
3086 info->flags |= ASYNC_NORMAL_ACTIVE;
3088 DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
3092 static int alloc_tmp_rbuf(struct slgt_info *info)
3094 info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
3095 if (info->tmp_rbuf == NULL)
3100 static void free_tmp_rbuf(struct slgt_info *info)
3102 kfree(info->tmp_rbuf);
3103 info->tmp_rbuf = NULL;
3107 * allocate DMA descriptor lists.
3109 static int alloc_desc(struct slgt_info *info)
3114 /* allocate memory to hold descriptor lists */
3115 info->bufs = pci_alloc_consistent(info->pdev, DESC_LIST_SIZE, &info->bufs_dma_addr);
3116 if (info->bufs == NULL)
3119 memset(info->bufs, 0, DESC_LIST_SIZE);
3121 info->rbufs = (struct slgt_desc*)info->bufs;
3122 info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
3124 pbufs = (unsigned int)info->bufs_dma_addr;
3127 * Build circular lists of descriptors
3130 for (i=0; i < info->rbuf_count; i++) {
3131 /* physical address of this descriptor */
3132 info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
3134 /* physical address of next descriptor */
3135 if (i == info->rbuf_count - 1)
3136 info->rbufs[i].next = cpu_to_le32(pbufs);
3138 info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
3139 set_desc_count(info->rbufs[i], DMABUFSIZE);
3142 for (i=0; i < info->tbuf_count; i++) {
3143 /* physical address of this descriptor */
3144 info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
3146 /* physical address of next descriptor */
3147 if (i == info->tbuf_count - 1)
3148 info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
3150 info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
3156 static void free_desc(struct slgt_info *info)
3158 if (info->bufs != NULL) {
3159 pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
3166 static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3169 for (i=0; i < count; i++) {
3170 if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
3172 bufs[i].pbuf = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
3177 static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3180 for (i=0; i < count; i++) {
3181 if (bufs[i].buf == NULL)
3183 pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
3188 static int alloc_dma_bufs(struct slgt_info *info)
3190 info->rbuf_count = 32;
3191 info->tbuf_count = 32;
3193 if (alloc_desc(info) < 0 ||
3194 alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
3195 alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
3196 alloc_tmp_rbuf(info) < 0) {
3197 DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
3204 static void free_dma_bufs(struct slgt_info *info)
3207 free_bufs(info, info->rbufs, info->rbuf_count);
3208 free_bufs(info, info->tbufs, info->tbuf_count);
3211 free_tmp_rbuf(info);
3214 static int claim_resources(struct slgt_info *info)
3216 if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
3217 DBGERR(("%s reg addr conflict, addr=%08X\n",
3218 info->device_name, info->phys_reg_addr));
3219 info->init_error = DiagStatus_AddressConflict;
3223 info->reg_addr_requested = 1;
3225 info->reg_addr = ioremap(info->phys_reg_addr, SLGT_REG_SIZE);
3226 if (!info->reg_addr) {
3227 DBGERR(("%s cant map device registers, addr=%08X\n",
3228 info->device_name, info->phys_reg_addr));
3229 info->init_error = DiagStatus_CantAssignPciResources;
3235 release_resources(info);
3239 static void release_resources(struct slgt_info *info)
3241 if (info->irq_requested) {
3242 free_irq(info->irq_level, info);
3243 info->irq_requested = 0;
3246 if (info->reg_addr_requested) {
3247 release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
3248 info->reg_addr_requested = 0;
3251 if (info->reg_addr) {
3252 iounmap(info->reg_addr);
3253 info->reg_addr = NULL;
3257 /* Add the specified device instance data structure to the
3258 * global linked list of devices and increment the device count.
3260 static void add_device(struct slgt_info *info)
3264 info->next_device = NULL;
3265 info->line = slgt_device_count;
3266 sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
3268 if (info->line < MAX_DEVICES) {
3269 if (maxframe[info->line])
3270 info->max_frame_size = maxframe[info->line];
3271 info->dosyncppp = dosyncppp[info->line];
3274 slgt_device_count++;
3276 if (!slgt_device_list)
3277 slgt_device_list = info;
3279 struct slgt_info *current_dev = slgt_device_list;
3280 while(current_dev->next_device)
3281 current_dev = current_dev->next_device;
3282 current_dev->next_device = info;
3285 if (info->max_frame_size < 4096)
3286 info->max_frame_size = 4096;
3287 else if (info->max_frame_size > 65535)
3288 info->max_frame_size = 65535;
3290 switch(info->pdev->device) {
3291 case SYNCLINK_GT_DEVICE_ID:
3294 case SYNCLINK_GT2_DEVICE_ID:
3297 case SYNCLINK_GT4_DEVICE_ID:
3300 case SYNCLINK_AC_DEVICE_ID:
3302 info->params.mode = MGSL_MODE_ASYNC;
3305 devstr = "(unknown model)";
3307 printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
3308 devstr, info->device_name, info->phys_reg_addr,
3309 info->irq_level, info->max_frame_size);
3311 #if SYNCLINK_GENERIC_HDLC
3317 * allocate device instance structure, return NULL on failure
3319 static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
3321 struct slgt_info *info;
3323 info = kmalloc(sizeof(struct slgt_info), GFP_KERNEL);
3326 DBGERR(("%s device alloc failed adapter=%d port=%d\n",
3327 driver_name, adapter_num, port_num));
3329 memset(info, 0, sizeof(struct slgt_info));
3330 info->magic = MGSL_MAGIC;
3331 INIT_WORK(&info->task, bh_handler);
3332 info->max_frame_size = 4096;
3333 info->raw_rx_size = DMABUFSIZE;
3334 info->close_delay = 5*HZ/10;
3335 info->closing_wait = 30*HZ;
3336 init_waitqueue_head(&info->open_wait);
3337 init_waitqueue_head(&info->close_wait);
3338 init_waitqueue_head(&info->status_event_wait_q);
3339 init_waitqueue_head(&info->event_wait_q);
3340 spin_lock_init(&info->netlock);
3341 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
3342 info->idle_mode = HDLC_TXIDLE_FLAGS;
3343 info->adapter_num = adapter_num;
3344 info->port_num = port_num;
3346 init_timer(&info->tx_timer);
3347 info->tx_timer.data = (unsigned long)info;
3348 info->tx_timer.function = tx_timeout;
3350 init_timer(&info->rx_timer);
3351 info->rx_timer.data = (unsigned long)info;
3352 info->rx_timer.function = rx_timeout;
3354 /* Copy configuration info to device instance data */
3356 info->irq_level = pdev->irq;
3357 info->phys_reg_addr = pci_resource_start(pdev,0);
3359 info->bus_type = MGSL_BUS_TYPE_PCI;
3360 info->irq_flags = IRQF_SHARED;
3362 info->init_error = -1; /* assume error, set to 0 on successful init */
3368 static void device_init(int adapter_num, struct pci_dev *pdev)
3370 struct slgt_info *port_array[SLGT_MAX_PORTS];
3374 if (pdev->device == SYNCLINK_GT2_DEVICE_ID)
3376 else if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
3379 /* allocate device instances for all ports */
3380 for (i=0; i < port_count; ++i) {
3381 port_array[i] = alloc_dev(adapter_num, i, pdev);
3382 if (port_array[i] == NULL) {
3383 for (--i; i >= 0; --i)
3384 kfree(port_array[i]);
3389 /* give copy of port_array to all ports and add to device list */
3390 for (i=0; i < port_count; ++i) {
3391 memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
3392 add_device(port_array[i]);
3393 port_array[i]->port_count = port_count;
3394 spin_lock_init(&port_array[i]->lock);
3397 /* Allocate and claim adapter resources */
3398 if (!claim_resources(port_array[0])) {
3400 alloc_dma_bufs(port_array[0]);
3402 /* copy resource information from first port to others */
3403 for (i = 1; i < port_count; ++i) {
3404 port_array[i]->lock = port_array[0]->lock;
3405 port_array[i]->irq_level = port_array[0]->irq_level;
3406 port_array[i]->reg_addr = port_array[0]->reg_addr;
3407 alloc_dma_bufs(port_array[i]);
3410 if (request_irq(port_array[0]->irq_level,
3412 port_array[0]->irq_flags,
3413 port_array[0]->device_name,
3414 port_array[0]) < 0) {
3415 DBGERR(("%s request_irq failed IRQ=%d\n",
3416 port_array[0]->device_name,
3417 port_array[0]->irq_level));
3419 port_array[0]->irq_requested = 1;
3420 adapter_test(port_array[0]);
3421 for (i=1 ; i < port_count ; i++) {
3422 port_array[i]->init_error = port_array[0]->init_error;
3423 port_array[i]->gpio_present = port_array[0]->gpio_present;
3429 static int __devinit init_one(struct pci_dev *dev,
3430 const struct pci_device_id *ent)
3432 if (pci_enable_device(dev)) {
3433 printk("error enabling pci device %p\n", dev);
3436 pci_set_master(dev);
3437 device_init(slgt_device_count, dev);
3441 static void __devexit remove_one(struct pci_dev *dev)
3445 static const struct tty_operations ops = {
3449 .put_char = put_char,
3450 .flush_chars = flush_chars,
3451 .write_room = write_room,
3452 .chars_in_buffer = chars_in_buffer,
3453 .flush_buffer = flush_buffer,
3455 .throttle = throttle,
3456 .unthrottle = unthrottle,
3457 .send_xchar = send_xchar,
3458 .break_ctl = set_break,
3459 .wait_until_sent = wait_until_sent,
3460 .read_proc = read_proc,
3461 .set_termios = set_termios,
3463 .start = tx_release,
3465 .tiocmget = tiocmget,
3466 .tiocmset = tiocmset,
3469 static void slgt_cleanup(void)
3472 struct slgt_info *info;
3473 struct slgt_info *tmp;
3475 printk("unload %s %s\n", driver_name, driver_version);
3477 if (serial_driver) {
3478 if ((rc = tty_unregister_driver(serial_driver)))
3479 DBGERR(("tty_unregister_driver error=%d\n", rc));
3480 put_tty_driver(serial_driver);
3484 info = slgt_device_list;
3487 info = info->next_device;
3490 /* release devices */
3491 info = slgt_device_list;
3493 #if SYNCLINK_GENERIC_HDLC
3496 free_dma_bufs(info);
3497 free_tmp_rbuf(info);
3498 if (info->port_num == 0)
3499 release_resources(info);
3501 info = info->next_device;
3506 pci_unregister_driver(&pci_driver);
3510 * Driver initialization entry point.
3512 static int __init slgt_init(void)
3516 printk("%s %s\n", driver_name, driver_version);
3518 slgt_device_count = 0;
3519 if ((rc = pci_register_driver(&pci_driver)) < 0) {
3520 printk("%s pci_register_driver error=%d\n", driver_name, rc);
3525 if (!slgt_device_list) {
3526 printk("%s no devices found\n",driver_name);
3527 pci_unregister_driver(&pci_driver);
3531 serial_driver = alloc_tty_driver(MAX_DEVICES);
3532 if (!serial_driver) {
3537 /* Initialize the tty_driver structure */
3539 serial_driver->owner = THIS_MODULE;
3540 serial_driver->driver_name = tty_driver_name;
3541 serial_driver->name = tty_dev_prefix;
3542 serial_driver->major = ttymajor;
3543 serial_driver->minor_start = 64;
3544 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3545 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3546 serial_driver->init_termios = tty_std_termios;
3547 serial_driver->init_termios.c_cflag =
3548 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3549 serial_driver->init_termios.c_ispeed = 9600;
3550 serial_driver->init_termios.c_ospeed = 9600;
3551 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3552 tty_set_operations(serial_driver, &ops);
3553 if ((rc = tty_register_driver(serial_driver)) < 0) {
3554 DBGERR(("%s can't register serial driver\n", driver_name));
3555 put_tty_driver(serial_driver);
3556 serial_driver = NULL;
3560 printk("%s %s, tty major#%d\n",
3561 driver_name, driver_version,
3562 serial_driver->major);
3571 static void __exit slgt_exit(void)
3576 module_init(slgt_init);
3577 module_exit(slgt_exit);
3580 * register access routines
3583 #define CALC_REGADDR() \
3584 unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
3586 reg_addr += (info->port_num) * 32;
3588 static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
3591 return readb((void __iomem *)reg_addr);
3594 static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
3597 writeb(value, (void __iomem *)reg_addr);
3600 static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
3603 return readw((void __iomem *)reg_addr);
3606 static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
3609 writew(value, (void __iomem *)reg_addr);
3612 static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
3615 return readl((void __iomem *)reg_addr);
3618 static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
3621 writel(value, (void __iomem *)reg_addr);
3624 static void rdma_reset(struct slgt_info *info)
3629 wr_reg32(info, RDCSR, BIT1);
3631 /* wait for enable bit cleared */
3632 for(i=0 ; i < 1000 ; i++)
3633 if (!(rd_reg32(info, RDCSR) & BIT0))
3637 static void tdma_reset(struct slgt_info *info)
3642 wr_reg32(info, TDCSR, BIT1);
3644 /* wait for enable bit cleared */
3645 for(i=0 ; i < 1000 ; i++)
3646 if (!(rd_reg32(info, TDCSR) & BIT0))
3651 * enable internal loopback
3652 * TxCLK and RxCLK are generated from BRG
3653 * and TxD is looped back to RxD internally.
3655 static void enable_loopback(struct slgt_info *info)
3657 /* SCR (serial control) BIT2=looopback enable */
3658 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
3660 if (info->params.mode != MGSL_MODE_ASYNC) {
3661 /* CCR (clock control)
3662 * 07..05 tx clock source (010 = BRG)
3663 * 04..02 rx clock source (010 = BRG)
3664 * 01 auxclk enable (0 = disable)
3665 * 00 BRG enable (1 = enable)
3669 wr_reg8(info, CCR, 0x49);
3671 /* set speed if available, otherwise use default */
3672 if (info->params.clock_speed)
3673 set_rate(info, info->params.clock_speed);
3675 set_rate(info, 3686400);
3680 * set baud rate generator to specified rate
3682 static void set_rate(struct slgt_info *info, u32 rate)
3685 static unsigned int osc = 14745600;
3687 /* div = osc/rate - 1
3689 * Round div up if osc/rate is not integer to
3690 * force to next slowest rate.
3695 if (!(osc % rate) && div)
3697 wr_reg16(info, BDR, (unsigned short)div);
3701 static void rx_stop(struct slgt_info *info)
3705 /* disable and reset receiver */
3706 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3707 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3708 wr_reg16(info, RCR, val); /* clear reset bit */
3710 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
3712 /* clear pending rx interrupts */
3713 wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
3717 info->rx_enabled = 0;
3718 info->rx_restart = 0;
3721 static void rx_start(struct slgt_info *info)
3725 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
3727 /* clear pending rx overrun IRQ */
3728 wr_reg16(info, SSR, IRQ_RXOVER);
3730 /* reset and disable receiver */
3731 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3732 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3733 wr_reg16(info, RCR, val); /* clear reset bit */
3738 /* set 1st descriptor address */
3739 wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
3741 if (info->params.mode != MGSL_MODE_ASYNC) {
3742 /* enable rx DMA and DMA interrupt */
3743 wr_reg32(info, RDCSR, (BIT2 + BIT0));
3745 /* enable saving of rx status, rx DMA and DMA interrupt */
3746 wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
3749 slgt_irq_on(info, IRQ_RXOVER);
3751 /* enable receiver */
3752 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
3754 info->rx_restart = 0;
3755 info->rx_enabled = 1;
3758 static void tx_start(struct slgt_info *info)
3760 if (!info->tx_enabled) {
3762 (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
3763 info->tx_enabled = TRUE;
3766 if (info->tx_count) {
3767 info->drop_rts_on_tx_done = 0;
3769 if (info->params.mode != MGSL_MODE_ASYNC) {
3770 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3772 if (!(info->signals & SerialSignal_RTS)) {
3773 info->signals |= SerialSignal_RTS;
3775 info->drop_rts_on_tx_done = 1;
3779 slgt_irq_off(info, IRQ_TXDATA);
3780 slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
3781 /* clear tx idle and underrun status bits */
3782 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
3784 if (!(rd_reg32(info, TDCSR) & BIT0)) {
3785 /* tx DMA stopped, restart tx DMA */
3787 /* set 1st descriptor address */
3788 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
3789 switch(info->params.mode) {
3791 case MGSL_MODE_MONOSYNC:
3792 case MGSL_MODE_BISYNC:
3793 wr_reg32(info, TDCSR, BIT2 + BIT0); /* IRQ + DMA enable */
3796 wr_reg32(info, TDCSR, BIT0); /* DMA enable */
3800 if (info->params.mode == MGSL_MODE_HDLC) {
3801 info->tx_timer.expires = jiffies + msecs_to_jiffies(5000);
3802 add_timer(&info->tx_timer);
3806 /* set 1st descriptor address */
3807 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
3809 slgt_irq_off(info, IRQ_TXDATA);
3810 slgt_irq_on(info, IRQ_TXIDLE);
3811 /* clear tx idle status bit */
3812 wr_reg16(info, SSR, IRQ_TXIDLE);
3815 wr_reg32(info, TDCSR, BIT0);
3818 info->tx_active = 1;
3822 static void tx_stop(struct slgt_info *info)
3826 del_timer(&info->tx_timer);
3830 /* reset and disable transmitter */
3831 val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
3832 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
3834 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
3836 /* clear tx idle and underrun status bit */
3837 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
3841 info->tx_enabled = 0;
3842 info->tx_active = 0;
3845 static void reset_port(struct slgt_info *info)
3847 if (!info->reg_addr)
3853 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
3856 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
3859 static void reset_adapter(struct slgt_info *info)
3862 for (i=0; i < info->port_count; ++i) {
3863 if (info->port_array[i])
3864 reset_port(info->port_array[i]);
3868 static void async_mode(struct slgt_info *info)
3872 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
3878 * 15..13 mode, 010=async
3879 * 12..10 encoding, 000=NRZ
3881 * 08 1=odd parity, 0=even parity
3882 * 07 1=RTS driver control
3884 * 05..04 character length
3889 * 03 0=1 stop bit, 1=2 stop bits
3892 * 00 auto-CTS enable
3896 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
3899 if (info->params.parity != ASYNC_PARITY_NONE) {
3901 if (info->params.parity == ASYNC_PARITY_ODD)
3905 switch (info->params.data_bits)
3907 case 6: val |= BIT4; break;
3908 case 7: val |= BIT5; break;
3909 case 8: val |= BIT5 + BIT4; break;
3912 if (info->params.stop_bits != 1)
3915 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3918 wr_reg16(info, TCR, val);
3922 * 15..13 mode, 010=async
3923 * 12..10 encoding, 000=NRZ
3925 * 08 1=odd parity, 0=even parity
3926 * 07..06 reserved, must be 0
3927 * 05..04 character length
3932 * 03 reserved, must be zero
3935 * 00 auto-DCD enable
3939 if (info->params.parity != ASYNC_PARITY_NONE) {
3941 if (info->params.parity == ASYNC_PARITY_ODD)
3945 switch (info->params.data_bits)
3947 case 6: val |= BIT4; break;
3948 case 7: val |= BIT5; break;
3949 case 8: val |= BIT5 + BIT4; break;
3952 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3955 wr_reg16(info, RCR, val);
3957 /* CCR (clock control)
3959 * 07..05 011 = tx clock source is BRG/16
3960 * 04..02 010 = rx clock source is BRG
3961 * 01 0 = auxclk disabled
3962 * 00 1 = BRG enabled
3966 wr_reg8(info, CCR, 0x69);
3970 /* SCR (serial control)
3972 * 15 1=tx req on FIFO half empty
3973 * 14 1=rx req on FIFO half full
3974 * 13 tx data IRQ enable
3975 * 12 tx idle IRQ enable
3976 * 11 rx break on IRQ enable
3977 * 10 rx data IRQ enable
3978 * 09 rx break off IRQ enable
3979 * 08 overrun IRQ enable
3984 * 03 reserved, must be zero
3985 * 02 1=txd->rxd internal loopback enable
3986 * 01 reserved, must be zero
3987 * 00 1=master IRQ enable
3989 val = BIT15 + BIT14 + BIT0;
3990 wr_reg16(info, SCR, val);
3992 slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
3994 set_rate(info, info->params.data_rate * 16);
3996 if (info->params.loopback)
3997 enable_loopback(info);
4000 static void sync_mode(struct slgt_info *info)
4004 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4010 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4014 * 07 1=RTS driver control
4015 * 06 preamble enable
4016 * 05..04 preamble length
4017 * 03 share open/close flag
4020 * 00 auto-CTS enable
4024 switch(info->params.mode) {
4025 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4026 case MGSL_MODE_BISYNC: val |= BIT15; break;
4027 case MGSL_MODE_RAW: val |= BIT13; break;
4029 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4032 switch(info->params.encoding)
4034 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4035 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4036 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4037 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4038 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4039 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4040 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4043 switch (info->params.crc_type & HDLC_CRC_MASK)
4045 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4046 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4049 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
4052 switch (info->params.preamble_length)
4054 case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
4055 case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
4056 case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
4059 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4062 wr_reg16(info, TCR, val);
4064 /* TPR (transmit preamble) */
4066 switch (info->params.preamble)
4068 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
4069 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
4070 case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
4071 case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
4072 case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
4073 default: val = 0x7e; break;
4075 wr_reg8(info, TPR, (unsigned char)val);
4079 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4083 * 07..03 reserved, must be 0
4086 * 00 auto-DCD enable
4090 switch(info->params.mode) {
4091 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4092 case MGSL_MODE_BISYNC: val |= BIT15; break;
4093 case MGSL_MODE_RAW: val |= BIT13; break;
4096 switch(info->params.encoding)
4098 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4099 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4100 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4101 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4102 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4103 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4104 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4107 switch (info->params.crc_type & HDLC_CRC_MASK)
4109 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4110 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4113 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4116 wr_reg16(info, RCR, val);
4118 /* CCR (clock control)
4120 * 07..05 tx clock source
4121 * 04..02 rx clock source
4127 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4129 // when RxC source is DPLL, BRG generates 16X DPLL
4130 // reference clock, so take TxC from BRG/16 to get
4131 // transmit clock at actual data rate
4132 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4133 val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
4135 val |= BIT6; /* 010, txclk = BRG */
4137 else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4138 val |= BIT7; /* 100, txclk = DPLL Input */
4139 else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4140 val |= BIT5; /* 001, txclk = RXC Input */
4142 if (info->params.flags & HDLC_FLAG_RXC_BRG)
4143 val |= BIT3; /* 010, rxclk = BRG */
4144 else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4145 val |= BIT4; /* 100, rxclk = DPLL */
4146 else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4147 val |= BIT2; /* 001, rxclk = TXC Input */
4149 if (info->params.clock_speed)
4152 wr_reg8(info, CCR, (unsigned char)val);
4154 if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
4156 // program DPLL mode
4157 switch(info->params.encoding)
4159 case HDLC_ENCODING_BIPHASE_MARK:
4160 case HDLC_ENCODING_BIPHASE_SPACE:
4162 case HDLC_ENCODING_BIPHASE_LEVEL:
4163 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
4164 val = BIT7 + BIT6; break;
4165 default: val = BIT6; // NRZ encodings
4167 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
4169 // DPLL requires a 16X reference clock from BRG
4170 set_rate(info, info->params.clock_speed * 16);
4173 set_rate(info, info->params.clock_speed);
4179 /* SCR (serial control)
4181 * 15 1=tx req on FIFO half empty
4182 * 14 1=rx req on FIFO half full
4183 * 13 tx data IRQ enable
4184 * 12 tx idle IRQ enable
4185 * 11 underrun IRQ enable
4186 * 10 rx data IRQ enable
4187 * 09 rx idle IRQ enable
4188 * 08 overrun IRQ enable
4193 * 03 reserved, must be zero
4194 * 02 1=txd->rxd internal loopback enable
4195 * 01 reserved, must be zero
4196 * 00 1=master IRQ enable
4198 wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
4200 if (info->params.loopback)
4201 enable_loopback(info);
4205 * set transmit idle mode
4207 static void tx_set_idle(struct slgt_info *info)
4212 /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
4213 * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
4215 tcr = rd_reg16(info, TCR);
4216 if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
4217 /* disable preamble, set idle size to 16 bits */
4218 tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
4219 /* MSB of 16 bit idle specified in tx preamble register (TPR) */
4220 wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
4221 } else if (!(tcr & BIT6)) {
4222 /* preamble is disabled, set idle size to 8 bits */
4223 tcr &= ~(BIT5 + BIT4);
4225 wr_reg16(info, TCR, tcr);
4227 if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
4228 /* LSB of custom tx idle specified in tx idle register */
4229 val = (unsigned char)(info->idle_mode & 0xff);
4231 /* standard 8 bit idle patterns */
4232 switch(info->idle_mode)
4234 case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
4235 case HDLC_TXIDLE_ALT_ZEROS_ONES:
4236 case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
4237 case HDLC_TXIDLE_ZEROS:
4238 case HDLC_TXIDLE_SPACE: val = 0x00; break;
4239 default: val = 0xff;
4243 wr_reg8(info, TIR, val);
4247 * get state of V24 status (input) signals
4249 static void get_signals(struct slgt_info *info)
4251 unsigned short status = rd_reg16(info, SSR);
4253 /* clear all serial signals except DTR and RTS */
4254 info->signals &= SerialSignal_DTR + SerialSignal_RTS;
4257 info->signals |= SerialSignal_DSR;
4259 info->signals |= SerialSignal_CTS;
4261 info->signals |= SerialSignal_DCD;
4263 info->signals |= SerialSignal_RI;
4267 * set V.24 Control Register based on current configuration
4269 static void msc_set_vcr(struct slgt_info *info)
4271 unsigned char val = 0;
4273 /* VCR (V.24 control)
4275 * 07..04 serial IF select
4282 switch(info->if_mode & MGSL_INTERFACE_MASK)
4284 case MGSL_INTERFACE_RS232:
4285 val |= BIT5; /* 0010 */
4287 case MGSL_INTERFACE_V35:
4288 val |= BIT7 + BIT6 + BIT5; /* 1110 */
4290 case MGSL_INTERFACE_RS422:
4291 val |= BIT6; /* 0100 */
4295 if (info->signals & SerialSignal_DTR)
4297 if (info->signals & SerialSignal_RTS)
4299 if (info->if_mode & MGSL_INTERFACE_LL)
4301 if (info->if_mode & MGSL_INTERFACE_RL)
4303 wr_reg8(info, VCR, val);
4307 * set state of V24 control (output) signals
4309 static void set_signals(struct slgt_info *info)
4311 unsigned char val = rd_reg8(info, VCR);
4312 if (info->signals & SerialSignal_DTR)
4316 if (info->signals & SerialSignal_RTS)
4320 wr_reg8(info, VCR, val);
4324 * free range of receive DMA buffers (i to last)
4326 static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
4331 /* reset current buffer for reuse */
4332 info->rbufs[i].status = 0;
4333 switch(info->params.mode) {
4335 case MGSL_MODE_MONOSYNC:
4336 case MGSL_MODE_BISYNC:
4337 set_desc_count(info->rbufs[i], info->raw_rx_size);
4340 set_desc_count(info->rbufs[i], DMABUFSIZE);
4345 if (++i == info->rbuf_count)
4348 info->rbuf_current = i;
4352 * mark all receive DMA buffers as free
4354 static void reset_rbufs(struct slgt_info *info)
4356 free_rbufs(info, 0, info->rbuf_count - 1);
4360 * pass receive HDLC frame to upper layer
4362 * return 1 if frame available, otherwise 0
4364 static int rx_get_frame(struct slgt_info *info)
4366 unsigned int start, end;
4367 unsigned short status;
4368 unsigned int framesize = 0;
4370 unsigned long flags;
4371 struct tty_struct *tty = info->tty;
4372 unsigned char addr_field = 0xff;
4373 unsigned int crc_size = 0;
4375 switch (info->params.crc_type & HDLC_CRC_MASK) {
4376 case HDLC_CRC_16_CCITT: crc_size = 2; break;
4377 case HDLC_CRC_32_CCITT: crc_size = 4; break;
4384 start = end = info->rbuf_current;
4387 if (!desc_complete(info->rbufs[end]))
4390 if (framesize == 0 && info->params.addr_filter != 0xff)
4391 addr_field = info->rbufs[end].buf[0];
4393 framesize += desc_count(info->rbufs[end]);
4395 if (desc_eof(info->rbufs[end]))
4398 if (++end == info->rbuf_count)
4401 if (end == info->rbuf_current) {
4402 if (info->rx_enabled){
4403 spin_lock_irqsave(&info->lock,flags);
4405 spin_unlock_irqrestore(&info->lock,flags);
4413 * 15 buffer complete
4416 * 02 eof (end of frame)
4420 status = desc_status(info->rbufs[end]);
4422 /* ignore CRC bit if not using CRC (bit is undefined) */
4423 if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
4426 if (framesize == 0 ||
4427 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4428 free_rbufs(info, start, end);
4432 if (framesize < (2 + crc_size) || status & BIT0) {
4433 info->icount.rxshort++;
4435 } else if (status & BIT1) {
4436 info->icount.rxcrc++;
4437 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
4441 #if SYNCLINK_GENERIC_HDLC
4442 if (framesize == 0) {
4443 struct net_device_stats *stats = hdlc_stats(info->netdev);
4445 stats->rx_frame_errors++;
4449 DBGBH(("%s rx frame status=%04X size=%d\n",
4450 info->device_name, status, framesize));
4451 DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, DMABUFSIZE), "rx");
4454 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
4455 framesize -= crc_size;
4459 if (framesize > info->max_frame_size + crc_size)
4460 info->icount.rxlong++;
4462 /* copy dma buffer(s) to contiguous temp buffer */
4463 int copy_count = framesize;
4465 unsigned char *p = info->tmp_rbuf;
4466 info->tmp_rbuf_count = framesize;
4468 info->icount.rxok++;
4471 int partial_count = min(copy_count, DMABUFSIZE);
4472 memcpy(p, info->rbufs[i].buf, partial_count);
4474 copy_count -= partial_count;
4475 if (++i == info->rbuf_count)
4479 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4480 *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
4484 #if SYNCLINK_GENERIC_HDLC
4486 hdlcdev_rx(info,info->tmp_rbuf, framesize);
4489 ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
4492 free_rbufs(info, start, end);
4500 * pass receive buffer (RAW synchronous mode) to tty layer
4501 * return 1 if buffer available, otherwise 0
4503 static int rx_get_buf(struct slgt_info *info)
4505 unsigned int i = info->rbuf_current;
4508 if (!desc_complete(info->rbufs[i]))
4510 count = desc_count(info->rbufs[i]);
4511 switch(info->params.mode) {
4512 case MGSL_MODE_MONOSYNC:
4513 case MGSL_MODE_BISYNC:
4514 /* ignore residue in byte synchronous modes */
4515 if (desc_residue(info->rbufs[i]))
4519 DBGDATA(info, info->rbufs[i].buf, count, "rx");
4520 DBGINFO(("rx_get_buf size=%d\n", count));
4522 ldisc_receive_buf(info->tty, info->rbufs[i].buf,
4523 info->flag_buf, count);
4524 free_rbufs(info, i, i);
4528 static void reset_tbufs(struct slgt_info *info)
4531 info->tbuf_current = 0;
4532 for (i=0 ; i < info->tbuf_count ; i++) {
4533 info->tbufs[i].status = 0;
4534 info->tbufs[i].count = 0;
4539 * return number of free transmit DMA buffers
4541 static unsigned int free_tbuf_count(struct slgt_info *info)
4543 unsigned int count = 0;
4544 unsigned int i = info->tbuf_current;
4548 if (desc_count(info->tbufs[i]))
4549 break; /* buffer in use */
4551 if (++i == info->tbuf_count)
4553 } while (i != info->tbuf_current);
4555 /* last buffer with zero count may be in use, assume it is */
4563 * load transmit DMA buffer(s) with data
4565 static void tx_load(struct slgt_info *info, const char *buf, unsigned int size)
4567 unsigned short count;
4569 struct slgt_desc *d;
4574 DBGDATA(info, buf, size, "tx");
4576 info->tbuf_start = i = info->tbuf_current;
4579 d = &info->tbufs[i];
4580 if (++i == info->tbuf_count)
4583 count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
4584 memcpy(d->buf, buf, count);
4590 * set EOF bit for last buffer of HDLC frame or
4591 * for every buffer in raw mode
4593 if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
4594 info->params.mode == MGSL_MODE_RAW)
4595 set_desc_eof(*d, 1);
4597 set_desc_eof(*d, 0);
4599 set_desc_count(*d, count);
4602 info->tbuf_current = i;
4605 static int register_test(struct slgt_info *info)
4607 static unsigned short patterns[] =
4608 {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
4609 static unsigned int count = sizeof(patterns)/sizeof(patterns[0]);
4613 for (i=0 ; i < count ; i++) {
4614 wr_reg16(info, TIR, patterns[i]);
4615 wr_reg16(info, BDR, patterns[(i+1)%count]);
4616 if ((rd_reg16(info, TIR) != patterns[i]) ||
4617 (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
4622 info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
4623 info->init_error = rc ? 0 : DiagStatus_AddressFailure;
4627 static int irq_test(struct slgt_info *info)
4629 unsigned long timeout;
4630 unsigned long flags;
4631 struct tty_struct *oldtty = info->tty;
4632 u32 speed = info->params.data_rate;
4634 info->params.data_rate = 921600;
4637 spin_lock_irqsave(&info->lock, flags);
4639 slgt_irq_on(info, IRQ_TXIDLE);
4641 /* enable transmitter */
4643 (unsigned short)(rd_reg16(info, TCR) | BIT1));
4645 /* write one byte and wait for tx idle */
4646 wr_reg16(info, TDR, 0);
4648 /* assume failure */
4649 info->init_error = DiagStatus_IrqFailure;
4650 info->irq_occurred = FALSE;
4652 spin_unlock_irqrestore(&info->lock, flags);
4655 while(timeout-- && !info->irq_occurred)
4656 msleep_interruptible(10);
4658 spin_lock_irqsave(&info->lock,flags);
4660 spin_unlock_irqrestore(&info->lock,flags);
4662 info->params.data_rate = speed;
4665 info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
4666 return info->irq_occurred ? 0 : -ENODEV;
4669 static int loopback_test_rx(struct slgt_info *info)
4671 unsigned char *src, *dest;
4674 if (desc_complete(info->rbufs[0])) {
4675 count = desc_count(info->rbufs[0]);
4676 src = info->rbufs[0].buf;
4677 dest = info->tmp_rbuf;
4679 for( ; count ; count-=2, src+=2) {
4680 /* src=data byte (src+1)=status byte */
4681 if (!(*(src+1) & (BIT9 + BIT8))) {
4684 info->tmp_rbuf_count++;
4687 DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
4693 static int loopback_test(struct slgt_info *info)
4695 #define TESTFRAMESIZE 20
4697 unsigned long timeout;
4698 u16 count = TESTFRAMESIZE;
4699 unsigned char buf[TESTFRAMESIZE];
4701 unsigned long flags;
4703 struct tty_struct *oldtty = info->tty;
4706 memcpy(¶ms, &info->params, sizeof(params));
4708 info->params.mode = MGSL_MODE_ASYNC;
4709 info->params.data_rate = 921600;
4710 info->params.loopback = 1;
4713 /* build and send transmit frame */
4714 for (count = 0; count < TESTFRAMESIZE; ++count)
4715 buf[count] = (unsigned char)count;
4717 info->tmp_rbuf_count = 0;
4718 memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
4720 /* program hardware for HDLC and enabled receiver */
4721 spin_lock_irqsave(&info->lock,flags);
4724 info->tx_count = count;
4725 tx_load(info, buf, count);
4727 spin_unlock_irqrestore(&info->lock, flags);
4729 /* wait for receive complete */
4730 for (timeout = 100; timeout; --timeout) {
4731 msleep_interruptible(10);
4732 if (loopback_test_rx(info)) {
4738 /* verify received frame length and contents */
4739 if (!rc && (info->tmp_rbuf_count != count ||
4740 memcmp(buf, info->tmp_rbuf, count))) {
4744 spin_lock_irqsave(&info->lock,flags);
4745 reset_adapter(info);
4746 spin_unlock_irqrestore(&info->lock,flags);
4748 memcpy(&info->params, ¶ms, sizeof(info->params));
4751 info->init_error = rc ? DiagStatus_DmaFailure : 0;
4755 static int adapter_test(struct slgt_info *info)
4757 DBGINFO(("testing %s\n", info->device_name));
4758 if (register_test(info) < 0) {
4759 printk("register test failure %s addr=%08X\n",
4760 info->device_name, info->phys_reg_addr);
4761 } else if (irq_test(info) < 0) {
4762 printk("IRQ test failure %s IRQ=%d\n",
4763 info->device_name, info->irq_level);
4764 } else if (loopback_test(info) < 0) {
4765 printk("loopback test failure %s\n", info->device_name);
4767 return info->init_error;
4771 * transmit timeout handler
4773 static void tx_timeout(unsigned long context)
4775 struct slgt_info *info = (struct slgt_info*)context;
4776 unsigned long flags;
4778 DBGINFO(("%s tx_timeout\n", info->device_name));
4779 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
4780 info->icount.txtimeout++;
4782 spin_lock_irqsave(&info->lock,flags);
4783 info->tx_active = 0;
4785 spin_unlock_irqrestore(&info->lock,flags);
4787 #if SYNCLINK_GENERIC_HDLC
4789 hdlcdev_tx_done(info);
4796 * receive buffer polling timer
4798 static void rx_timeout(unsigned long context)
4800 struct slgt_info *info = (struct slgt_info*)context;
4801 unsigned long flags;
4803 DBGINFO(("%s rx_timeout\n", info->device_name));
4804 spin_lock_irqsave(&info->lock, flags);
4805 info->pending_bh |= BH_RECEIVE;
4806 spin_unlock_irqrestore(&info->lock, flags);
4807 bh_handler(&info->task);