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 termios *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 termios *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->flags = TTY_DRIVER_REAL_RAW;
3550 tty_set_operations(serial_driver, &ops);
3551 if ((rc = tty_register_driver(serial_driver)) < 0) {
3552 DBGERR(("%s can't register serial driver\n", driver_name));
3553 put_tty_driver(serial_driver);
3554 serial_driver = NULL;
3558 printk("%s %s, tty major#%d\n",
3559 driver_name, driver_version,
3560 serial_driver->major);
3569 static void __exit slgt_exit(void)
3574 module_init(slgt_init);
3575 module_exit(slgt_exit);
3578 * register access routines
3581 #define CALC_REGADDR() \
3582 unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
3584 reg_addr += (info->port_num) * 32;
3586 static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
3589 return readb((void __iomem *)reg_addr);
3592 static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
3595 writeb(value, (void __iomem *)reg_addr);
3598 static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
3601 return readw((void __iomem *)reg_addr);
3604 static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
3607 writew(value, (void __iomem *)reg_addr);
3610 static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
3613 return readl((void __iomem *)reg_addr);
3616 static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
3619 writel(value, (void __iomem *)reg_addr);
3622 static void rdma_reset(struct slgt_info *info)
3627 wr_reg32(info, RDCSR, BIT1);
3629 /* wait for enable bit cleared */
3630 for(i=0 ; i < 1000 ; i++)
3631 if (!(rd_reg32(info, RDCSR) & BIT0))
3635 static void tdma_reset(struct slgt_info *info)
3640 wr_reg32(info, TDCSR, BIT1);
3642 /* wait for enable bit cleared */
3643 for(i=0 ; i < 1000 ; i++)
3644 if (!(rd_reg32(info, TDCSR) & BIT0))
3649 * enable internal loopback
3650 * TxCLK and RxCLK are generated from BRG
3651 * and TxD is looped back to RxD internally.
3653 static void enable_loopback(struct slgt_info *info)
3655 /* SCR (serial control) BIT2=looopback enable */
3656 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
3658 if (info->params.mode != MGSL_MODE_ASYNC) {
3659 /* CCR (clock control)
3660 * 07..05 tx clock source (010 = BRG)
3661 * 04..02 rx clock source (010 = BRG)
3662 * 01 auxclk enable (0 = disable)
3663 * 00 BRG enable (1 = enable)
3667 wr_reg8(info, CCR, 0x49);
3669 /* set speed if available, otherwise use default */
3670 if (info->params.clock_speed)
3671 set_rate(info, info->params.clock_speed);
3673 set_rate(info, 3686400);
3678 * set baud rate generator to specified rate
3680 static void set_rate(struct slgt_info *info, u32 rate)
3683 static unsigned int osc = 14745600;
3685 /* div = osc/rate - 1
3687 * Round div up if osc/rate is not integer to
3688 * force to next slowest rate.
3693 if (!(osc % rate) && div)
3695 wr_reg16(info, BDR, (unsigned short)div);
3699 static void rx_stop(struct slgt_info *info)
3703 /* disable and reset receiver */
3704 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3705 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3706 wr_reg16(info, RCR, val); /* clear reset bit */
3708 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
3710 /* clear pending rx interrupts */
3711 wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
3715 info->rx_enabled = 0;
3716 info->rx_restart = 0;
3719 static void rx_start(struct slgt_info *info)
3723 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
3725 /* clear pending rx overrun IRQ */
3726 wr_reg16(info, SSR, IRQ_RXOVER);
3728 /* reset and disable receiver */
3729 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3730 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3731 wr_reg16(info, RCR, val); /* clear reset bit */
3736 /* set 1st descriptor address */
3737 wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
3739 if (info->params.mode != MGSL_MODE_ASYNC) {
3740 /* enable rx DMA and DMA interrupt */
3741 wr_reg32(info, RDCSR, (BIT2 + BIT0));
3743 /* enable saving of rx status, rx DMA and DMA interrupt */
3744 wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
3747 slgt_irq_on(info, IRQ_RXOVER);
3749 /* enable receiver */
3750 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
3752 info->rx_restart = 0;
3753 info->rx_enabled = 1;
3756 static void tx_start(struct slgt_info *info)
3758 if (!info->tx_enabled) {
3760 (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
3761 info->tx_enabled = TRUE;
3764 if (info->tx_count) {
3765 info->drop_rts_on_tx_done = 0;
3767 if (info->params.mode != MGSL_MODE_ASYNC) {
3768 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3770 if (!(info->signals & SerialSignal_RTS)) {
3771 info->signals |= SerialSignal_RTS;
3773 info->drop_rts_on_tx_done = 1;
3777 slgt_irq_off(info, IRQ_TXDATA);
3778 slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
3779 /* clear tx idle and underrun status bits */
3780 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
3782 if (!(rd_reg32(info, TDCSR) & BIT0)) {
3783 /* tx DMA stopped, restart tx DMA */
3785 /* set 1st descriptor address */
3786 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
3787 switch(info->params.mode) {
3789 case MGSL_MODE_MONOSYNC:
3790 case MGSL_MODE_BISYNC:
3791 wr_reg32(info, TDCSR, BIT2 + BIT0); /* IRQ + DMA enable */
3794 wr_reg32(info, TDCSR, BIT0); /* DMA enable */
3798 if (info->params.mode == MGSL_MODE_HDLC) {
3799 info->tx_timer.expires = jiffies + msecs_to_jiffies(5000);
3800 add_timer(&info->tx_timer);
3804 /* set 1st descriptor address */
3805 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
3807 slgt_irq_off(info, IRQ_TXDATA);
3808 slgt_irq_on(info, IRQ_TXIDLE);
3809 /* clear tx idle status bit */
3810 wr_reg16(info, SSR, IRQ_TXIDLE);
3813 wr_reg32(info, TDCSR, BIT0);
3816 info->tx_active = 1;
3820 static void tx_stop(struct slgt_info *info)
3824 del_timer(&info->tx_timer);
3828 /* reset and disable transmitter */
3829 val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
3830 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
3832 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
3834 /* clear tx idle and underrun status bit */
3835 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
3839 info->tx_enabled = 0;
3840 info->tx_active = 0;
3843 static void reset_port(struct slgt_info *info)
3845 if (!info->reg_addr)
3851 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
3854 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
3857 static void reset_adapter(struct slgt_info *info)
3860 for (i=0; i < info->port_count; ++i) {
3861 if (info->port_array[i])
3862 reset_port(info->port_array[i]);
3866 static void async_mode(struct slgt_info *info)
3870 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
3876 * 15..13 mode, 010=async
3877 * 12..10 encoding, 000=NRZ
3879 * 08 1=odd parity, 0=even parity
3880 * 07 1=RTS driver control
3882 * 05..04 character length
3887 * 03 0=1 stop bit, 1=2 stop bits
3890 * 00 auto-CTS enable
3894 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
3897 if (info->params.parity != ASYNC_PARITY_NONE) {
3899 if (info->params.parity == ASYNC_PARITY_ODD)
3903 switch (info->params.data_bits)
3905 case 6: val |= BIT4; break;
3906 case 7: val |= BIT5; break;
3907 case 8: val |= BIT5 + BIT4; break;
3910 if (info->params.stop_bits != 1)
3913 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3916 wr_reg16(info, TCR, val);
3920 * 15..13 mode, 010=async
3921 * 12..10 encoding, 000=NRZ
3923 * 08 1=odd parity, 0=even parity
3924 * 07..06 reserved, must be 0
3925 * 05..04 character length
3930 * 03 reserved, must be zero
3933 * 00 auto-DCD enable
3937 if (info->params.parity != ASYNC_PARITY_NONE) {
3939 if (info->params.parity == ASYNC_PARITY_ODD)
3943 switch (info->params.data_bits)
3945 case 6: val |= BIT4; break;
3946 case 7: val |= BIT5; break;
3947 case 8: val |= BIT5 + BIT4; break;
3950 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3953 wr_reg16(info, RCR, val);
3955 /* CCR (clock control)
3957 * 07..05 011 = tx clock source is BRG/16
3958 * 04..02 010 = rx clock source is BRG
3959 * 01 0 = auxclk disabled
3960 * 00 1 = BRG enabled
3964 wr_reg8(info, CCR, 0x69);
3968 /* SCR (serial control)
3970 * 15 1=tx req on FIFO half empty
3971 * 14 1=rx req on FIFO half full
3972 * 13 tx data IRQ enable
3973 * 12 tx idle IRQ enable
3974 * 11 rx break on IRQ enable
3975 * 10 rx data IRQ enable
3976 * 09 rx break off IRQ enable
3977 * 08 overrun IRQ enable
3982 * 03 reserved, must be zero
3983 * 02 1=txd->rxd internal loopback enable
3984 * 01 reserved, must be zero
3985 * 00 1=master IRQ enable
3987 val = BIT15 + BIT14 + BIT0;
3988 wr_reg16(info, SCR, val);
3990 slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
3992 set_rate(info, info->params.data_rate * 16);
3994 if (info->params.loopback)
3995 enable_loopback(info);
3998 static void sync_mode(struct slgt_info *info)
4002 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4008 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4012 * 07 1=RTS driver control
4013 * 06 preamble enable
4014 * 05..04 preamble length
4015 * 03 share open/close flag
4018 * 00 auto-CTS enable
4022 switch(info->params.mode) {
4023 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4024 case MGSL_MODE_BISYNC: val |= BIT15; break;
4025 case MGSL_MODE_RAW: val |= BIT13; break;
4027 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4030 switch(info->params.encoding)
4032 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4033 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4034 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4035 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4036 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4037 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4038 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4041 switch (info->params.crc_type & HDLC_CRC_MASK)
4043 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4044 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4047 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
4050 switch (info->params.preamble_length)
4052 case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
4053 case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
4054 case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
4057 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4060 wr_reg16(info, TCR, val);
4062 /* TPR (transmit preamble) */
4064 switch (info->params.preamble)
4066 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
4067 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
4068 case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
4069 case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
4070 case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
4071 default: val = 0x7e; break;
4073 wr_reg8(info, TPR, (unsigned char)val);
4077 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4081 * 07..03 reserved, must be 0
4084 * 00 auto-DCD enable
4088 switch(info->params.mode) {
4089 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4090 case MGSL_MODE_BISYNC: val |= BIT15; break;
4091 case MGSL_MODE_RAW: val |= BIT13; break;
4094 switch(info->params.encoding)
4096 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4097 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4098 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4099 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4100 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4101 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4102 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4105 switch (info->params.crc_type & HDLC_CRC_MASK)
4107 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4108 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4111 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4114 wr_reg16(info, RCR, val);
4116 /* CCR (clock control)
4118 * 07..05 tx clock source
4119 * 04..02 rx clock source
4125 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4127 // when RxC source is DPLL, BRG generates 16X DPLL
4128 // reference clock, so take TxC from BRG/16 to get
4129 // transmit clock at actual data rate
4130 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4131 val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
4133 val |= BIT6; /* 010, txclk = BRG */
4135 else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4136 val |= BIT7; /* 100, txclk = DPLL Input */
4137 else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4138 val |= BIT5; /* 001, txclk = RXC Input */
4140 if (info->params.flags & HDLC_FLAG_RXC_BRG)
4141 val |= BIT3; /* 010, rxclk = BRG */
4142 else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4143 val |= BIT4; /* 100, rxclk = DPLL */
4144 else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4145 val |= BIT2; /* 001, rxclk = TXC Input */
4147 if (info->params.clock_speed)
4150 wr_reg8(info, CCR, (unsigned char)val);
4152 if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
4154 // program DPLL mode
4155 switch(info->params.encoding)
4157 case HDLC_ENCODING_BIPHASE_MARK:
4158 case HDLC_ENCODING_BIPHASE_SPACE:
4160 case HDLC_ENCODING_BIPHASE_LEVEL:
4161 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
4162 val = BIT7 + BIT6; break;
4163 default: val = BIT6; // NRZ encodings
4165 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
4167 // DPLL requires a 16X reference clock from BRG
4168 set_rate(info, info->params.clock_speed * 16);
4171 set_rate(info, info->params.clock_speed);
4177 /* SCR (serial control)
4179 * 15 1=tx req on FIFO half empty
4180 * 14 1=rx req on FIFO half full
4181 * 13 tx data IRQ enable
4182 * 12 tx idle IRQ enable
4183 * 11 underrun IRQ enable
4184 * 10 rx data IRQ enable
4185 * 09 rx idle IRQ enable
4186 * 08 overrun IRQ enable
4191 * 03 reserved, must be zero
4192 * 02 1=txd->rxd internal loopback enable
4193 * 01 reserved, must be zero
4194 * 00 1=master IRQ enable
4196 wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
4198 if (info->params.loopback)
4199 enable_loopback(info);
4203 * set transmit idle mode
4205 static void tx_set_idle(struct slgt_info *info)
4210 /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
4211 * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
4213 tcr = rd_reg16(info, TCR);
4214 if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
4215 /* disable preamble, set idle size to 16 bits */
4216 tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
4217 /* MSB of 16 bit idle specified in tx preamble register (TPR) */
4218 wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
4219 } else if (!(tcr & BIT6)) {
4220 /* preamble is disabled, set idle size to 8 bits */
4221 tcr &= ~(BIT5 + BIT4);
4223 wr_reg16(info, TCR, tcr);
4225 if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
4226 /* LSB of custom tx idle specified in tx idle register */
4227 val = (unsigned char)(info->idle_mode & 0xff);
4229 /* standard 8 bit idle patterns */
4230 switch(info->idle_mode)
4232 case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
4233 case HDLC_TXIDLE_ALT_ZEROS_ONES:
4234 case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
4235 case HDLC_TXIDLE_ZEROS:
4236 case HDLC_TXIDLE_SPACE: val = 0x00; break;
4237 default: val = 0xff;
4241 wr_reg8(info, TIR, val);
4245 * get state of V24 status (input) signals
4247 static void get_signals(struct slgt_info *info)
4249 unsigned short status = rd_reg16(info, SSR);
4251 /* clear all serial signals except DTR and RTS */
4252 info->signals &= SerialSignal_DTR + SerialSignal_RTS;
4255 info->signals |= SerialSignal_DSR;
4257 info->signals |= SerialSignal_CTS;
4259 info->signals |= SerialSignal_DCD;
4261 info->signals |= SerialSignal_RI;
4265 * set V.24 Control Register based on current configuration
4267 static void msc_set_vcr(struct slgt_info *info)
4269 unsigned char val = 0;
4271 /* VCR (V.24 control)
4273 * 07..04 serial IF select
4280 switch(info->if_mode & MGSL_INTERFACE_MASK)
4282 case MGSL_INTERFACE_RS232:
4283 val |= BIT5; /* 0010 */
4285 case MGSL_INTERFACE_V35:
4286 val |= BIT7 + BIT6 + BIT5; /* 1110 */
4288 case MGSL_INTERFACE_RS422:
4289 val |= BIT6; /* 0100 */
4293 if (info->signals & SerialSignal_DTR)
4295 if (info->signals & SerialSignal_RTS)
4297 if (info->if_mode & MGSL_INTERFACE_LL)
4299 if (info->if_mode & MGSL_INTERFACE_RL)
4301 wr_reg8(info, VCR, val);
4305 * set state of V24 control (output) signals
4307 static void set_signals(struct slgt_info *info)
4309 unsigned char val = rd_reg8(info, VCR);
4310 if (info->signals & SerialSignal_DTR)
4314 if (info->signals & SerialSignal_RTS)
4318 wr_reg8(info, VCR, val);
4322 * free range of receive DMA buffers (i to last)
4324 static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
4329 /* reset current buffer for reuse */
4330 info->rbufs[i].status = 0;
4331 switch(info->params.mode) {
4333 case MGSL_MODE_MONOSYNC:
4334 case MGSL_MODE_BISYNC:
4335 set_desc_count(info->rbufs[i], info->raw_rx_size);
4338 set_desc_count(info->rbufs[i], DMABUFSIZE);
4343 if (++i == info->rbuf_count)
4346 info->rbuf_current = i;
4350 * mark all receive DMA buffers as free
4352 static void reset_rbufs(struct slgt_info *info)
4354 free_rbufs(info, 0, info->rbuf_count - 1);
4358 * pass receive HDLC frame to upper layer
4360 * return 1 if frame available, otherwise 0
4362 static int rx_get_frame(struct slgt_info *info)
4364 unsigned int start, end;
4365 unsigned short status;
4366 unsigned int framesize = 0;
4368 unsigned long flags;
4369 struct tty_struct *tty = info->tty;
4370 unsigned char addr_field = 0xff;
4371 unsigned int crc_size = 0;
4373 switch (info->params.crc_type & HDLC_CRC_MASK) {
4374 case HDLC_CRC_16_CCITT: crc_size = 2; break;
4375 case HDLC_CRC_32_CCITT: crc_size = 4; break;
4382 start = end = info->rbuf_current;
4385 if (!desc_complete(info->rbufs[end]))
4388 if (framesize == 0 && info->params.addr_filter != 0xff)
4389 addr_field = info->rbufs[end].buf[0];
4391 framesize += desc_count(info->rbufs[end]);
4393 if (desc_eof(info->rbufs[end]))
4396 if (++end == info->rbuf_count)
4399 if (end == info->rbuf_current) {
4400 if (info->rx_enabled){
4401 spin_lock_irqsave(&info->lock,flags);
4403 spin_unlock_irqrestore(&info->lock,flags);
4411 * 15 buffer complete
4414 * 02 eof (end of frame)
4418 status = desc_status(info->rbufs[end]);
4420 /* ignore CRC bit if not using CRC (bit is undefined) */
4421 if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
4424 if (framesize == 0 ||
4425 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4426 free_rbufs(info, start, end);
4430 if (framesize < (2 + crc_size) || status & BIT0) {
4431 info->icount.rxshort++;
4433 } else if (status & BIT1) {
4434 info->icount.rxcrc++;
4435 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
4439 #if SYNCLINK_GENERIC_HDLC
4440 if (framesize == 0) {
4441 struct net_device_stats *stats = hdlc_stats(info->netdev);
4443 stats->rx_frame_errors++;
4447 DBGBH(("%s rx frame status=%04X size=%d\n",
4448 info->device_name, status, framesize));
4449 DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, DMABUFSIZE), "rx");
4452 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
4453 framesize -= crc_size;
4457 if (framesize > info->max_frame_size + crc_size)
4458 info->icount.rxlong++;
4460 /* copy dma buffer(s) to contiguous temp buffer */
4461 int copy_count = framesize;
4463 unsigned char *p = info->tmp_rbuf;
4464 info->tmp_rbuf_count = framesize;
4466 info->icount.rxok++;
4469 int partial_count = min(copy_count, DMABUFSIZE);
4470 memcpy(p, info->rbufs[i].buf, partial_count);
4472 copy_count -= partial_count;
4473 if (++i == info->rbuf_count)
4477 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4478 *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
4482 #if SYNCLINK_GENERIC_HDLC
4484 hdlcdev_rx(info,info->tmp_rbuf, framesize);
4487 ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
4490 free_rbufs(info, start, end);
4498 * pass receive buffer (RAW synchronous mode) to tty layer
4499 * return 1 if buffer available, otherwise 0
4501 static int rx_get_buf(struct slgt_info *info)
4503 unsigned int i = info->rbuf_current;
4506 if (!desc_complete(info->rbufs[i]))
4508 count = desc_count(info->rbufs[i]);
4509 switch(info->params.mode) {
4510 case MGSL_MODE_MONOSYNC:
4511 case MGSL_MODE_BISYNC:
4512 /* ignore residue in byte synchronous modes */
4513 if (desc_residue(info->rbufs[i]))
4517 DBGDATA(info, info->rbufs[i].buf, count, "rx");
4518 DBGINFO(("rx_get_buf size=%d\n", count));
4520 ldisc_receive_buf(info->tty, info->rbufs[i].buf,
4521 info->flag_buf, count);
4522 free_rbufs(info, i, i);
4526 static void reset_tbufs(struct slgt_info *info)
4529 info->tbuf_current = 0;
4530 for (i=0 ; i < info->tbuf_count ; i++) {
4531 info->tbufs[i].status = 0;
4532 info->tbufs[i].count = 0;
4537 * return number of free transmit DMA buffers
4539 static unsigned int free_tbuf_count(struct slgt_info *info)
4541 unsigned int count = 0;
4542 unsigned int i = info->tbuf_current;
4546 if (desc_count(info->tbufs[i]))
4547 break; /* buffer in use */
4549 if (++i == info->tbuf_count)
4551 } while (i != info->tbuf_current);
4553 /* last buffer with zero count may be in use, assume it is */
4561 * load transmit DMA buffer(s) with data
4563 static void tx_load(struct slgt_info *info, const char *buf, unsigned int size)
4565 unsigned short count;
4567 struct slgt_desc *d;
4572 DBGDATA(info, buf, size, "tx");
4574 info->tbuf_start = i = info->tbuf_current;
4577 d = &info->tbufs[i];
4578 if (++i == info->tbuf_count)
4581 count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
4582 memcpy(d->buf, buf, count);
4588 * set EOF bit for last buffer of HDLC frame or
4589 * for every buffer in raw mode
4591 if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
4592 info->params.mode == MGSL_MODE_RAW)
4593 set_desc_eof(*d, 1);
4595 set_desc_eof(*d, 0);
4597 set_desc_count(*d, count);
4600 info->tbuf_current = i;
4603 static int register_test(struct slgt_info *info)
4605 static unsigned short patterns[] =
4606 {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
4607 static unsigned int count = sizeof(patterns)/sizeof(patterns[0]);
4611 for (i=0 ; i < count ; i++) {
4612 wr_reg16(info, TIR, patterns[i]);
4613 wr_reg16(info, BDR, patterns[(i+1)%count]);
4614 if ((rd_reg16(info, TIR) != patterns[i]) ||
4615 (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
4620 info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
4621 info->init_error = rc ? 0 : DiagStatus_AddressFailure;
4625 static int irq_test(struct slgt_info *info)
4627 unsigned long timeout;
4628 unsigned long flags;
4629 struct tty_struct *oldtty = info->tty;
4630 u32 speed = info->params.data_rate;
4632 info->params.data_rate = 921600;
4635 spin_lock_irqsave(&info->lock, flags);
4637 slgt_irq_on(info, IRQ_TXIDLE);
4639 /* enable transmitter */
4641 (unsigned short)(rd_reg16(info, TCR) | BIT1));
4643 /* write one byte and wait for tx idle */
4644 wr_reg16(info, TDR, 0);
4646 /* assume failure */
4647 info->init_error = DiagStatus_IrqFailure;
4648 info->irq_occurred = FALSE;
4650 spin_unlock_irqrestore(&info->lock, flags);
4653 while(timeout-- && !info->irq_occurred)
4654 msleep_interruptible(10);
4656 spin_lock_irqsave(&info->lock,flags);
4658 spin_unlock_irqrestore(&info->lock,flags);
4660 info->params.data_rate = speed;
4663 info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
4664 return info->irq_occurred ? 0 : -ENODEV;
4667 static int loopback_test_rx(struct slgt_info *info)
4669 unsigned char *src, *dest;
4672 if (desc_complete(info->rbufs[0])) {
4673 count = desc_count(info->rbufs[0]);
4674 src = info->rbufs[0].buf;
4675 dest = info->tmp_rbuf;
4677 for( ; count ; count-=2, src+=2) {
4678 /* src=data byte (src+1)=status byte */
4679 if (!(*(src+1) & (BIT9 + BIT8))) {
4682 info->tmp_rbuf_count++;
4685 DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
4691 static int loopback_test(struct slgt_info *info)
4693 #define TESTFRAMESIZE 20
4695 unsigned long timeout;
4696 u16 count = TESTFRAMESIZE;
4697 unsigned char buf[TESTFRAMESIZE];
4699 unsigned long flags;
4701 struct tty_struct *oldtty = info->tty;
4704 memcpy(¶ms, &info->params, sizeof(params));
4706 info->params.mode = MGSL_MODE_ASYNC;
4707 info->params.data_rate = 921600;
4708 info->params.loopback = 1;
4711 /* build and send transmit frame */
4712 for (count = 0; count < TESTFRAMESIZE; ++count)
4713 buf[count] = (unsigned char)count;
4715 info->tmp_rbuf_count = 0;
4716 memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
4718 /* program hardware for HDLC and enabled receiver */
4719 spin_lock_irqsave(&info->lock,flags);
4722 info->tx_count = count;
4723 tx_load(info, buf, count);
4725 spin_unlock_irqrestore(&info->lock, flags);
4727 /* wait for receive complete */
4728 for (timeout = 100; timeout; --timeout) {
4729 msleep_interruptible(10);
4730 if (loopback_test_rx(info)) {
4736 /* verify received frame length and contents */
4737 if (!rc && (info->tmp_rbuf_count != count ||
4738 memcmp(buf, info->tmp_rbuf, count))) {
4742 spin_lock_irqsave(&info->lock,flags);
4743 reset_adapter(info);
4744 spin_unlock_irqrestore(&info->lock,flags);
4746 memcpy(&info->params, ¶ms, sizeof(info->params));
4749 info->init_error = rc ? DiagStatus_DmaFailure : 0;
4753 static int adapter_test(struct slgt_info *info)
4755 DBGINFO(("testing %s\n", info->device_name));
4756 if (register_test(info) < 0) {
4757 printk("register test failure %s addr=%08X\n",
4758 info->device_name, info->phys_reg_addr);
4759 } else if (irq_test(info) < 0) {
4760 printk("IRQ test failure %s IRQ=%d\n",
4761 info->device_name, info->irq_level);
4762 } else if (loopback_test(info) < 0) {
4763 printk("loopback test failure %s\n", info->device_name);
4765 return info->init_error;
4769 * transmit timeout handler
4771 static void tx_timeout(unsigned long context)
4773 struct slgt_info *info = (struct slgt_info*)context;
4774 unsigned long flags;
4776 DBGINFO(("%s tx_timeout\n", info->device_name));
4777 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
4778 info->icount.txtimeout++;
4780 spin_lock_irqsave(&info->lock,flags);
4781 info->tx_active = 0;
4783 spin_unlock_irqrestore(&info->lock,flags);
4785 #if SYNCLINK_GENERIC_HDLC
4787 hdlcdev_tx_done(info);
4794 * receive buffer polling timer
4796 static void rx_timeout(unsigned long context)
4798 struct slgt_info *info = (struct slgt_info*)context;
4799 unsigned long flags;
4801 DBGINFO(("%s rx_timeout\n", info->device_name));
4802 spin_lock_irqsave(&info->lock, flags);
4803 info->pending_bh |= BH_RECEIVE;
4804 spin_unlock_irqrestore(&info->lock, flags);
4805 bh_handler(&info->task);