2 * $Id: synclink_gt.c,v 4.50 2007/07/25 19:29:25 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>
76 #include <linux/synclink.h>
78 #include <asm/system.h>
82 #include <asm/types.h>
83 #include <asm/uaccess.h>
85 #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_GT_MODULE))
86 #define SYNCLINK_GENERIC_HDLC 1
88 #define SYNCLINK_GENERIC_HDLC 0
92 * module identification
94 static char *driver_name = "SyncLink GT";
95 static char *driver_version = "$Revision: 4.50 $";
96 static char *tty_driver_name = "synclink_gt";
97 static char *tty_dev_prefix = "ttySLG";
98 MODULE_LICENSE("GPL");
99 #define MGSL_MAGIC 0x5401
100 #define MAX_DEVICES 32
102 static struct pci_device_id pci_table[] = {
103 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
104 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT2_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
105 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
106 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
107 {0,}, /* terminate list */
109 MODULE_DEVICE_TABLE(pci, pci_table);
111 static int init_one(struct pci_dev *dev,const struct pci_device_id *ent);
112 static void remove_one(struct pci_dev *dev);
113 static struct pci_driver pci_driver = {
114 .name = "synclink_gt",
115 .id_table = pci_table,
117 .remove = __devexit_p(remove_one),
120 static bool pci_registered;
123 * module configuration and status
125 static struct slgt_info *slgt_device_list;
126 static int slgt_device_count;
129 static int debug_level;
130 static int maxframe[MAX_DEVICES];
132 module_param(ttymajor, int, 0);
133 module_param(debug_level, int, 0);
134 module_param_array(maxframe, int, NULL, 0);
136 MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
137 MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
138 MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
141 * tty support and callbacks
143 static struct tty_driver *serial_driver;
145 static int open(struct tty_struct *tty, struct file * filp);
146 static void close(struct tty_struct *tty, struct file * filp);
147 static void hangup(struct tty_struct *tty);
148 static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
150 static int write(struct tty_struct *tty, const unsigned char *buf, int count);
151 static int put_char(struct tty_struct *tty, unsigned char ch);
152 static void send_xchar(struct tty_struct *tty, char ch);
153 static void wait_until_sent(struct tty_struct *tty, int timeout);
154 static int write_room(struct tty_struct *tty);
155 static void flush_chars(struct tty_struct *tty);
156 static void flush_buffer(struct tty_struct *tty);
157 static void tx_hold(struct tty_struct *tty);
158 static void tx_release(struct tty_struct *tty);
160 static int ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
161 static int read_proc(char *page, char **start, off_t off, int count,int *eof, void *data);
162 static int chars_in_buffer(struct tty_struct *tty);
163 static void throttle(struct tty_struct * tty);
164 static void unthrottle(struct tty_struct * tty);
165 static void set_break(struct tty_struct *tty, int break_state);
168 * generic HDLC support and callbacks
170 #if SYNCLINK_GENERIC_HDLC
171 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
172 static void hdlcdev_tx_done(struct slgt_info *info);
173 static void hdlcdev_rx(struct slgt_info *info, char *buf, int size);
174 static int hdlcdev_init(struct slgt_info *info);
175 static void hdlcdev_exit(struct slgt_info *info);
180 * device specific structures, macros and functions
183 #define SLGT_MAX_PORTS 4
184 #define SLGT_REG_SIZE 256
187 * conditional wait facility
190 struct cond_wait *next;
195 static void init_cond_wait(struct cond_wait *w, unsigned int data);
196 static void add_cond_wait(struct cond_wait **head, struct cond_wait *w);
197 static void remove_cond_wait(struct cond_wait **head, struct cond_wait *w);
198 static void flush_cond_wait(struct cond_wait **head);
201 * DMA buffer descriptor and access macros
207 __le32 pbuf; /* physical address of data buffer */
208 __le32 next; /* physical address of next descriptor */
210 /* driver book keeping */
211 char *buf; /* virtual address of data buffer */
212 unsigned int pdesc; /* physical address of this descriptor */
213 dma_addr_t buf_dma_addr;
216 #define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
217 #define set_desc_next(a,b) (a).next = cpu_to_le32((unsigned int)(b))
218 #define set_desc_count(a,b)(a).count = cpu_to_le16((unsigned short)(b))
219 #define set_desc_eof(a,b) (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
220 #define desc_count(a) (le16_to_cpu((a).count))
221 #define desc_status(a) (le16_to_cpu((a).status))
222 #define desc_complete(a) (le16_to_cpu((a).status) & BIT15)
223 #define desc_eof(a) (le16_to_cpu((a).status) & BIT2)
224 #define desc_crc_error(a) (le16_to_cpu((a).status) & BIT1)
225 #define desc_abort(a) (le16_to_cpu((a).status) & BIT0)
226 #define desc_residue(a) ((le16_to_cpu((a).status) & 0x38) >> 3)
228 struct _input_signal_events {
240 * device instance data structure
243 void *if_ptr; /* General purpose pointer (used by SPPP) */
244 struct tty_port port;
246 struct slgt_info *next_device; /* device list link */
250 char device_name[25];
251 struct pci_dev *pdev;
253 int port_count; /* count of ports on adapter */
254 int adapter_num; /* adapter instance number */
255 int port_num; /* port instance number */
257 /* array of pointers to port contexts on this adapter */
258 struct slgt_info *port_array[SLGT_MAX_PORTS];
260 int line; /* tty line instance number */
262 struct mgsl_icount icount;
265 int x_char; /* xon/xoff character */
266 unsigned int read_status_mask;
267 unsigned int ignore_status_mask;
269 wait_queue_head_t status_event_wait_q;
270 wait_queue_head_t event_wait_q;
271 struct timer_list tx_timer;
272 struct timer_list rx_timer;
274 unsigned int gpio_present;
275 struct cond_wait *gpio_wait_q;
277 spinlock_t lock; /* spinlock for synchronizing with ISR */
279 struct work_struct task;
285 bool irq_requested; /* true if IRQ requested */
286 bool irq_occurred; /* for diagnostics use */
288 /* device configuration */
290 unsigned int bus_type;
291 unsigned int irq_level;
292 unsigned long irq_flags;
294 unsigned char __iomem * reg_addr; /* memory mapped registers address */
296 bool reg_addr_requested;
298 MGSL_PARAMS params; /* communications parameters */
300 u32 max_frame_size; /* as set by device config */
302 unsigned int raw_rx_size;
303 unsigned int if_mode;
313 unsigned char signals; /* serial signal states */
314 int init_error; /* initialization error */
316 unsigned char *tx_buf;
319 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
320 char char_buf[MAX_ASYNC_BUFFER_SIZE];
321 bool drop_rts_on_tx_done;
322 struct _input_signal_events input_signal_events;
324 int dcd_chkcount; /* check counts to prevent */
325 int cts_chkcount; /* too many IRQs if a signal */
326 int dsr_chkcount; /* is floating */
329 char *bufs; /* virtual address of DMA buffer lists */
330 dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
332 unsigned int rbuf_count;
333 struct slgt_desc *rbufs;
334 unsigned int rbuf_current;
335 unsigned int rbuf_index;
337 unsigned int tbuf_count;
338 struct slgt_desc *tbufs;
339 unsigned int tbuf_current;
340 unsigned int tbuf_start;
342 unsigned char *tmp_rbuf;
343 unsigned int tmp_rbuf_count;
345 /* SPPP/Cisco HDLC device parts */
349 #if SYNCLINK_GENERIC_HDLC
350 struct net_device *netdev;
355 static MGSL_PARAMS default_params = {
356 .mode = MGSL_MODE_HDLC,
358 .flags = HDLC_FLAG_UNDERRUN_ABORT15,
359 .encoding = HDLC_ENCODING_NRZI_SPACE,
362 .crc_type = HDLC_CRC_16_CCITT,
363 .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
364 .preamble = HDLC_PREAMBLE_PATTERN_NONE,
368 .parity = ASYNC_PARITY_NONE
373 #define BH_TRANSMIT 2
375 #define IO_PIN_SHUTDOWN_LIMIT 100
377 #define DMABUFSIZE 256
378 #define DESC_LIST_SIZE 4096
380 #define MASK_PARITY BIT1
381 #define MASK_FRAMING BIT0
382 #define MASK_BREAK BIT14
383 #define MASK_OVERRUN BIT4
385 #define GSR 0x00 /* global status */
386 #define JCR 0x04 /* JTAG control */
387 #define IODR 0x08 /* GPIO direction */
388 #define IOER 0x0c /* GPIO interrupt enable */
389 #define IOVR 0x10 /* GPIO value */
390 #define IOSR 0x14 /* GPIO interrupt status */
391 #define TDR 0x80 /* tx data */
392 #define RDR 0x80 /* rx data */
393 #define TCR 0x82 /* tx control */
394 #define TIR 0x84 /* tx idle */
395 #define TPR 0x85 /* tx preamble */
396 #define RCR 0x86 /* rx control */
397 #define VCR 0x88 /* V.24 control */
398 #define CCR 0x89 /* clock control */
399 #define BDR 0x8a /* baud divisor */
400 #define SCR 0x8c /* serial control */
401 #define SSR 0x8e /* serial status */
402 #define RDCSR 0x90 /* rx DMA control/status */
403 #define TDCSR 0x94 /* tx DMA control/status */
404 #define RDDAR 0x98 /* rx DMA descriptor address */
405 #define TDDAR 0x9c /* tx DMA descriptor address */
408 #define RXBREAK BIT14
409 #define IRQ_TXDATA BIT13
410 #define IRQ_TXIDLE BIT12
411 #define IRQ_TXUNDER BIT11 /* HDLC */
412 #define IRQ_RXDATA BIT10
413 #define IRQ_RXIDLE BIT9 /* HDLC */
414 #define IRQ_RXBREAK BIT9 /* async */
415 #define IRQ_RXOVER BIT8
420 #define IRQ_ALL 0x3ff0
421 #define IRQ_MASTER BIT0
423 #define slgt_irq_on(info, mask) \
424 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
425 #define slgt_irq_off(info, mask) \
426 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
428 static __u8 rd_reg8(struct slgt_info *info, unsigned int addr);
429 static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
430 static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
431 static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
432 static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
433 static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
435 static void msc_set_vcr(struct slgt_info *info);
437 static int startup(struct slgt_info *info);
438 static int block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
439 static void shutdown(struct slgt_info *info);
440 static void program_hw(struct slgt_info *info);
441 static void change_params(struct slgt_info *info);
443 static int register_test(struct slgt_info *info);
444 static int irq_test(struct slgt_info *info);
445 static int loopback_test(struct slgt_info *info);
446 static int adapter_test(struct slgt_info *info);
448 static void reset_adapter(struct slgt_info *info);
449 static void reset_port(struct slgt_info *info);
450 static void async_mode(struct slgt_info *info);
451 static void sync_mode(struct slgt_info *info);
453 static void rx_stop(struct slgt_info *info);
454 static void rx_start(struct slgt_info *info);
455 static void reset_rbufs(struct slgt_info *info);
456 static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
457 static void rdma_reset(struct slgt_info *info);
458 static bool rx_get_frame(struct slgt_info *info);
459 static bool rx_get_buf(struct slgt_info *info);
461 static void tx_start(struct slgt_info *info);
462 static void tx_stop(struct slgt_info *info);
463 static void tx_set_idle(struct slgt_info *info);
464 static unsigned int free_tbuf_count(struct slgt_info *info);
465 static void reset_tbufs(struct slgt_info *info);
466 static void tdma_reset(struct slgt_info *info);
467 static void tdma_start(struct slgt_info *info);
468 static void tx_load(struct slgt_info *info, const char *buf, unsigned int count);
470 static void get_signals(struct slgt_info *info);
471 static void set_signals(struct slgt_info *info);
472 static void enable_loopback(struct slgt_info *info);
473 static void set_rate(struct slgt_info *info, u32 data_rate);
475 static int bh_action(struct slgt_info *info);
476 static void bh_handler(struct work_struct *work);
477 static void bh_transmit(struct slgt_info *info);
478 static void isr_serial(struct slgt_info *info);
479 static void isr_rdma(struct slgt_info *info);
480 static void isr_txeom(struct slgt_info *info, unsigned short status);
481 static void isr_tdma(struct slgt_info *info);
483 static int alloc_dma_bufs(struct slgt_info *info);
484 static void free_dma_bufs(struct slgt_info *info);
485 static int alloc_desc(struct slgt_info *info);
486 static void free_desc(struct slgt_info *info);
487 static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
488 static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
490 static int alloc_tmp_rbuf(struct slgt_info *info);
491 static void free_tmp_rbuf(struct slgt_info *info);
493 static void tx_timeout(unsigned long context);
494 static void rx_timeout(unsigned long context);
499 static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount);
500 static int get_params(struct slgt_info *info, MGSL_PARAMS __user *params);
501 static int set_params(struct slgt_info *info, MGSL_PARAMS __user *params);
502 static int get_txidle(struct slgt_info *info, int __user *idle_mode);
503 static int set_txidle(struct slgt_info *info, int idle_mode);
504 static int tx_enable(struct slgt_info *info, int enable);
505 static int tx_abort(struct slgt_info *info);
506 static int rx_enable(struct slgt_info *info, int enable);
507 static int modem_input_wait(struct slgt_info *info,int arg);
508 static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr);
509 static int tiocmget(struct tty_struct *tty, struct file *file);
510 static int tiocmset(struct tty_struct *tty, struct file *file,
511 unsigned int set, unsigned int clear);
512 static void set_break(struct tty_struct *tty, int break_state);
513 static int get_interface(struct slgt_info *info, int __user *if_mode);
514 static int set_interface(struct slgt_info *info, int if_mode);
515 static int set_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
516 static int get_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
517 static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
522 static void add_device(struct slgt_info *info);
523 static void device_init(int adapter_num, struct pci_dev *pdev);
524 static int claim_resources(struct slgt_info *info);
525 static void release_resources(struct slgt_info *info);
544 static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
548 printk("%s %s data:\n",info->device_name, label);
550 linecount = (count > 16) ? 16 : count;
551 for(i=0; i < linecount; i++)
552 printk("%02X ",(unsigned char)data[i]);
555 for(i=0;i<linecount;i++) {
556 if (data[i]>=040 && data[i]<=0176)
557 printk("%c",data[i]);
567 #define DBGDATA(info, buf, size, label)
571 static void dump_tbufs(struct slgt_info *info)
574 printk("tbuf_current=%d\n", info->tbuf_current);
575 for (i=0 ; i < info->tbuf_count ; i++) {
576 printk("%d: count=%04X status=%04X\n",
577 i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
581 #define DBGTBUF(info)
585 static void dump_rbufs(struct slgt_info *info)
588 printk("rbuf_current=%d\n", info->rbuf_current);
589 for (i=0 ; i < info->rbuf_count ; i++) {
590 printk("%d: count=%04X status=%04X\n",
591 i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
595 #define DBGRBUF(info)
598 static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
602 printk("null struct slgt_info for (%s) in %s\n", devname, name);
605 if (info->magic != MGSL_MAGIC) {
606 printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
617 * line discipline callback wrappers
619 * The wrappers maintain line discipline references
620 * while calling into the line discipline.
622 * ldisc_receive_buf - pass receive data to line discipline
624 static void ldisc_receive_buf(struct tty_struct *tty,
625 const __u8 *data, char *flags, int count)
627 struct tty_ldisc *ld;
630 ld = tty_ldisc_ref(tty);
632 if (ld->ops->receive_buf)
633 ld->ops->receive_buf(tty, data, flags, count);
640 static int open(struct tty_struct *tty, struct file *filp)
642 struct slgt_info *info;
647 if ((line < 0) || (line >= slgt_device_count)) {
648 DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
652 info = slgt_device_list;
653 while(info && info->line != line)
654 info = info->next_device;
655 if (sanity_check(info, tty->name, "open"))
657 if (info->init_error) {
658 DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
662 tty->driver_data = info;
663 info->port.tty = tty;
665 DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->port.count));
667 /* If port is closing, signal caller to try again */
668 if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
669 if (info->port.flags & ASYNC_CLOSING)
670 interruptible_sleep_on(&info->port.close_wait);
671 retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
672 -EAGAIN : -ERESTARTSYS);
676 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
678 spin_lock_irqsave(&info->netlock, flags);
679 if (info->netcount) {
681 spin_unlock_irqrestore(&info->netlock, flags);
685 spin_unlock_irqrestore(&info->netlock, flags);
687 if (info->port.count == 1) {
688 /* 1st open on this device, init hardware */
689 retval = startup(info);
694 retval = block_til_ready(tty, filp, info);
696 DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
705 info->port.tty = NULL; /* tty layer will release tty struct */
710 DBGINFO(("%s open rc=%d\n", info->device_name, retval));
714 static void close(struct tty_struct *tty, struct file *filp)
716 struct slgt_info *info = tty->driver_data;
718 if (sanity_check(info, tty->name, "close"))
720 DBGINFO(("%s close entry, count=%d\n", info->device_name, info->port.count));
722 if (!info->port.count)
725 if (tty_hung_up_p(filp))
728 if ((tty->count == 1) && (info->port.count != 1)) {
730 * tty->count is 1 and the tty structure will be freed.
731 * info->port.count should be one in this case.
732 * if it's not, correct it so that the port is shutdown.
734 DBGERR(("%s close: bad refcount; tty->count=1, "
735 "info->port.count=%d\n", info->device_name, info->port.count));
736 info->port.count = 1;
741 /* if at least one open remaining, leave hardware active */
742 if (info->port.count)
745 info->port.flags |= ASYNC_CLOSING;
747 /* set tty->closing to notify line discipline to
748 * only process XON/XOFF characters. Only the N_TTY
749 * discipline appears to use this (ppp does not).
753 /* wait for transmit data to clear all layers */
755 if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE) {
756 DBGINFO(("%s call tty_wait_until_sent\n", info->device_name));
757 tty_wait_until_sent(tty, info->port.closing_wait);
760 if (info->port.flags & ASYNC_INITIALIZED)
761 wait_until_sent(tty, info->timeout);
763 tty_ldisc_flush(tty);
768 info->port.tty = NULL;
770 if (info->port.blocked_open) {
771 if (info->port.close_delay) {
772 msleep_interruptible(jiffies_to_msecs(info->port.close_delay));
774 wake_up_interruptible(&info->port.open_wait);
777 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
779 wake_up_interruptible(&info->port.close_wait);
782 DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->port.count));
785 static void hangup(struct tty_struct *tty)
787 struct slgt_info *info = tty->driver_data;
789 if (sanity_check(info, tty->name, "hangup"))
791 DBGINFO(("%s hangup\n", info->device_name));
796 info->port.count = 0;
797 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
798 info->port.tty = NULL;
800 wake_up_interruptible(&info->port.open_wait);
803 static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
805 struct slgt_info *info = tty->driver_data;
808 DBGINFO(("%s set_termios\n", tty->driver->name));
812 /* Handle transition to B0 status */
813 if (old_termios->c_cflag & CBAUD &&
814 !(tty->termios->c_cflag & CBAUD)) {
815 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
816 spin_lock_irqsave(&info->lock,flags);
818 spin_unlock_irqrestore(&info->lock,flags);
821 /* Handle transition away from B0 status */
822 if (!(old_termios->c_cflag & CBAUD) &&
823 tty->termios->c_cflag & CBAUD) {
824 info->signals |= SerialSignal_DTR;
825 if (!(tty->termios->c_cflag & CRTSCTS) ||
826 !test_bit(TTY_THROTTLED, &tty->flags)) {
827 info->signals |= SerialSignal_RTS;
829 spin_lock_irqsave(&info->lock,flags);
831 spin_unlock_irqrestore(&info->lock,flags);
834 /* Handle turning off CRTSCTS */
835 if (old_termios->c_cflag & CRTSCTS &&
836 !(tty->termios->c_cflag & CRTSCTS)) {
842 static int write(struct tty_struct *tty,
843 const unsigned char *buf, int count)
846 struct slgt_info *info = tty->driver_data;
849 if (sanity_check(info, tty->name, "write"))
851 DBGINFO(("%s write count=%d\n", info->device_name, count));
856 if (count > info->max_frame_size) {
864 if (info->params.mode == MGSL_MODE_RAW ||
865 info->params.mode == MGSL_MODE_MONOSYNC ||
866 info->params.mode == MGSL_MODE_BISYNC) {
867 unsigned int bufs_needed = (count/DMABUFSIZE);
868 unsigned int bufs_free = free_tbuf_count(info);
869 if (count % DMABUFSIZE)
871 if (bufs_needed > bufs_free)
876 if (info->tx_count) {
877 /* send accumulated data from send_char() calls */
878 /* as frame and wait before accepting more data. */
879 tx_load(info, info->tx_buf, info->tx_count);
884 ret = info->tx_count = count;
885 tx_load(info, buf, count);
889 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
890 spin_lock_irqsave(&info->lock,flags);
891 if (!info->tx_active)
895 spin_unlock_irqrestore(&info->lock,flags);
899 DBGINFO(("%s write rc=%d\n", info->device_name, ret));
903 static int put_char(struct tty_struct *tty, unsigned char ch)
905 struct slgt_info *info = tty->driver_data;
909 if (sanity_check(info, tty->name, "put_char"))
911 DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
914 spin_lock_irqsave(&info->lock,flags);
915 if (!info->tx_active && (info->tx_count < info->max_frame_size)) {
916 info->tx_buf[info->tx_count++] = ch;
919 spin_unlock_irqrestore(&info->lock,flags);
923 static void send_xchar(struct tty_struct *tty, char ch)
925 struct slgt_info *info = tty->driver_data;
928 if (sanity_check(info, tty->name, "send_xchar"))
930 DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
933 spin_lock_irqsave(&info->lock,flags);
934 if (!info->tx_enabled)
936 spin_unlock_irqrestore(&info->lock,flags);
940 static void wait_until_sent(struct tty_struct *tty, int timeout)
942 struct slgt_info *info = tty->driver_data;
943 unsigned long orig_jiffies, char_time;
947 if (sanity_check(info, tty->name, "wait_until_sent"))
949 DBGINFO(("%s wait_until_sent entry\n", info->device_name));
950 if (!(info->port.flags & ASYNC_INITIALIZED))
953 orig_jiffies = jiffies;
955 /* Set check interval to 1/5 of estimated time to
956 * send a character, and make it at least 1. The check
957 * interval should also be less than the timeout.
958 * Note: use tight timings here to satisfy the NIST-PCTS.
963 if (info->params.data_rate) {
964 char_time = info->timeout/(32 * 5);
971 char_time = min_t(unsigned long, char_time, timeout);
973 while (info->tx_active) {
974 msleep_interruptible(jiffies_to_msecs(char_time));
975 if (signal_pending(current))
977 if (timeout && time_after(jiffies, orig_jiffies + timeout))
983 DBGINFO(("%s wait_until_sent exit\n", info->device_name));
986 static int write_room(struct tty_struct *tty)
988 struct slgt_info *info = tty->driver_data;
991 if (sanity_check(info, tty->name, "write_room"))
993 ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
994 DBGINFO(("%s write_room=%d\n", info->device_name, ret));
998 static void flush_chars(struct tty_struct *tty)
1000 struct slgt_info *info = tty->driver_data;
1001 unsigned long flags;
1003 if (sanity_check(info, tty->name, "flush_chars"))
1005 DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
1007 if (info->tx_count <= 0 || tty->stopped ||
1008 tty->hw_stopped || !info->tx_buf)
1011 DBGINFO(("%s flush_chars start transmit\n", info->device_name));
1013 spin_lock_irqsave(&info->lock,flags);
1014 if (!info->tx_active && info->tx_count) {
1015 tx_load(info, info->tx_buf,info->tx_count);
1018 spin_unlock_irqrestore(&info->lock,flags);
1021 static void flush_buffer(struct tty_struct *tty)
1023 struct slgt_info *info = tty->driver_data;
1024 unsigned long flags;
1026 if (sanity_check(info, tty->name, "flush_buffer"))
1028 DBGINFO(("%s flush_buffer\n", info->device_name));
1030 spin_lock_irqsave(&info->lock,flags);
1031 if (!info->tx_active)
1033 spin_unlock_irqrestore(&info->lock,flags);
1039 * throttle (stop) transmitter
1041 static void tx_hold(struct tty_struct *tty)
1043 struct slgt_info *info = tty->driver_data;
1044 unsigned long flags;
1046 if (sanity_check(info, tty->name, "tx_hold"))
1048 DBGINFO(("%s tx_hold\n", info->device_name));
1049 spin_lock_irqsave(&info->lock,flags);
1050 if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
1052 spin_unlock_irqrestore(&info->lock,flags);
1056 * release (start) transmitter
1058 static void tx_release(struct tty_struct *tty)
1060 struct slgt_info *info = tty->driver_data;
1061 unsigned long flags;
1063 if (sanity_check(info, tty->name, "tx_release"))
1065 DBGINFO(("%s tx_release\n", info->device_name));
1066 spin_lock_irqsave(&info->lock,flags);
1067 if (!info->tx_active && info->tx_count) {
1068 tx_load(info, info->tx_buf, info->tx_count);
1071 spin_unlock_irqrestore(&info->lock,flags);
1075 * Service an IOCTL request
1079 * tty pointer to tty instance data
1080 * file pointer to associated file object for device
1081 * cmd IOCTL command code
1082 * arg command argument/context
1084 * Return 0 if success, otherwise error code
1086 static int ioctl(struct tty_struct *tty, struct file *file,
1087 unsigned int cmd, unsigned long arg)
1089 struct slgt_info *info = tty->driver_data;
1090 struct mgsl_icount cnow; /* kernel counter temps */
1091 struct serial_icounter_struct __user *p_cuser; /* user space */
1092 unsigned long flags;
1093 void __user *argp = (void __user *)arg;
1096 if (sanity_check(info, tty->name, "ioctl"))
1098 DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
1100 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1101 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1102 if (tty->flags & (1 << TTY_IO_ERROR))
1109 case MGSL_IOCGPARAMS:
1110 ret = get_params(info, argp);
1112 case MGSL_IOCSPARAMS:
1113 ret = set_params(info, argp);
1115 case MGSL_IOCGTXIDLE:
1116 ret = get_txidle(info, argp);
1118 case MGSL_IOCSTXIDLE:
1119 ret = set_txidle(info, (int)arg);
1121 case MGSL_IOCTXENABLE:
1122 ret = tx_enable(info, (int)arg);
1124 case MGSL_IOCRXENABLE:
1125 ret = rx_enable(info, (int)arg);
1127 case MGSL_IOCTXABORT:
1128 ret = tx_abort(info);
1130 case MGSL_IOCGSTATS:
1131 ret = get_stats(info, argp);
1133 case MGSL_IOCWAITEVENT:
1134 ret = wait_mgsl_event(info, argp);
1137 ret = modem_input_wait(info,(int)arg);
1140 ret = get_interface(info, argp);
1143 ret = set_interface(info,(int)arg);
1146 ret = set_gpio(info, argp);
1149 ret = get_gpio(info, argp);
1151 case MGSL_IOCWAITGPIO:
1152 ret = wait_gpio(info, argp);
1155 spin_lock_irqsave(&info->lock,flags);
1156 cnow = info->icount;
1157 spin_unlock_irqrestore(&info->lock,flags);
1159 if (put_user(cnow.cts, &p_cuser->cts) ||
1160 put_user(cnow.dsr, &p_cuser->dsr) ||
1161 put_user(cnow.rng, &p_cuser->rng) ||
1162 put_user(cnow.dcd, &p_cuser->dcd) ||
1163 put_user(cnow.rx, &p_cuser->rx) ||
1164 put_user(cnow.tx, &p_cuser->tx) ||
1165 put_user(cnow.frame, &p_cuser->frame) ||
1166 put_user(cnow.overrun, &p_cuser->overrun) ||
1167 put_user(cnow.parity, &p_cuser->parity) ||
1168 put_user(cnow.brk, &p_cuser->brk) ||
1169 put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1181 * support for 32 bit ioctl calls on 64 bit systems
1183 #ifdef CONFIG_COMPAT
1184 static long get_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *user_params)
1186 struct MGSL_PARAMS32 tmp_params;
1188 DBGINFO(("%s get_params32\n", info->device_name));
1189 tmp_params.mode = (compat_ulong_t)info->params.mode;
1190 tmp_params.loopback = info->params.loopback;
1191 tmp_params.flags = info->params.flags;
1192 tmp_params.encoding = info->params.encoding;
1193 tmp_params.clock_speed = (compat_ulong_t)info->params.clock_speed;
1194 tmp_params.addr_filter = info->params.addr_filter;
1195 tmp_params.crc_type = info->params.crc_type;
1196 tmp_params.preamble_length = info->params.preamble_length;
1197 tmp_params.preamble = info->params.preamble;
1198 tmp_params.data_rate = (compat_ulong_t)info->params.data_rate;
1199 tmp_params.data_bits = info->params.data_bits;
1200 tmp_params.stop_bits = info->params.stop_bits;
1201 tmp_params.parity = info->params.parity;
1202 if (copy_to_user(user_params, &tmp_params, sizeof(struct MGSL_PARAMS32)))
1207 static long set_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *new_params)
1209 struct MGSL_PARAMS32 tmp_params;
1211 DBGINFO(("%s set_params32\n", info->device_name));
1212 if (copy_from_user(&tmp_params, new_params, sizeof(struct MGSL_PARAMS32)))
1215 spin_lock(&info->lock);
1216 info->params.mode = tmp_params.mode;
1217 info->params.loopback = tmp_params.loopback;
1218 info->params.flags = tmp_params.flags;
1219 info->params.encoding = tmp_params.encoding;
1220 info->params.clock_speed = tmp_params.clock_speed;
1221 info->params.addr_filter = tmp_params.addr_filter;
1222 info->params.crc_type = tmp_params.crc_type;
1223 info->params.preamble_length = tmp_params.preamble_length;
1224 info->params.preamble = tmp_params.preamble;
1225 info->params.data_rate = tmp_params.data_rate;
1226 info->params.data_bits = tmp_params.data_bits;
1227 info->params.stop_bits = tmp_params.stop_bits;
1228 info->params.parity = tmp_params.parity;
1229 spin_unlock(&info->lock);
1231 change_params(info);
1236 static long slgt_compat_ioctl(struct tty_struct *tty, struct file *file,
1237 unsigned int cmd, unsigned long arg)
1239 struct slgt_info *info = tty->driver_data;
1240 int rc = -ENOIOCTLCMD;
1242 if (sanity_check(info, tty->name, "compat_ioctl"))
1244 DBGINFO(("%s compat_ioctl() cmd=%08X\n", info->device_name, cmd));
1248 case MGSL_IOCSPARAMS32:
1249 rc = set_params32(info, compat_ptr(arg));
1252 case MGSL_IOCGPARAMS32:
1253 rc = get_params32(info, compat_ptr(arg));
1256 case MGSL_IOCGPARAMS:
1257 case MGSL_IOCSPARAMS:
1258 case MGSL_IOCGTXIDLE:
1259 case MGSL_IOCGSTATS:
1260 case MGSL_IOCWAITEVENT:
1264 case MGSL_IOCWAITGPIO:
1266 rc = ioctl(tty, file, cmd, (unsigned long)(compat_ptr(arg)));
1269 case MGSL_IOCSTXIDLE:
1270 case MGSL_IOCTXENABLE:
1271 case MGSL_IOCRXENABLE:
1272 case MGSL_IOCTXABORT:
1275 rc = ioctl(tty, file, cmd, arg);
1279 DBGINFO(("%s compat_ioctl() cmd=%08X rc=%d\n", info->device_name, cmd, rc));
1283 #define slgt_compat_ioctl NULL
1284 #endif /* ifdef CONFIG_COMPAT */
1289 static inline int line_info(char *buf, struct slgt_info *info)
1293 unsigned long flags;
1295 ret = sprintf(buf, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
1296 info->device_name, info->phys_reg_addr,
1297 info->irq_level, info->max_frame_size);
1299 /* output current serial signal states */
1300 spin_lock_irqsave(&info->lock,flags);
1302 spin_unlock_irqrestore(&info->lock,flags);
1306 if (info->signals & SerialSignal_RTS)
1307 strcat(stat_buf, "|RTS");
1308 if (info->signals & SerialSignal_CTS)
1309 strcat(stat_buf, "|CTS");
1310 if (info->signals & SerialSignal_DTR)
1311 strcat(stat_buf, "|DTR");
1312 if (info->signals & SerialSignal_DSR)
1313 strcat(stat_buf, "|DSR");
1314 if (info->signals & SerialSignal_DCD)
1315 strcat(stat_buf, "|CD");
1316 if (info->signals & SerialSignal_RI)
1317 strcat(stat_buf, "|RI");
1319 if (info->params.mode != MGSL_MODE_ASYNC) {
1320 ret += sprintf(buf+ret, "\tHDLC txok:%d rxok:%d",
1321 info->icount.txok, info->icount.rxok);
1322 if (info->icount.txunder)
1323 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
1324 if (info->icount.txabort)
1325 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
1326 if (info->icount.rxshort)
1327 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
1328 if (info->icount.rxlong)
1329 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
1330 if (info->icount.rxover)
1331 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
1332 if (info->icount.rxcrc)
1333 ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
1335 ret += sprintf(buf+ret, "\tASYNC tx:%d rx:%d",
1336 info->icount.tx, info->icount.rx);
1337 if (info->icount.frame)
1338 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
1339 if (info->icount.parity)
1340 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
1341 if (info->icount.brk)
1342 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
1343 if (info->icount.overrun)
1344 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
1347 /* Append serial signal status to end */
1348 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
1350 ret += sprintf(buf+ret, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1351 info->tx_active,info->bh_requested,info->bh_running,
1357 /* Called to print information about devices
1359 static int read_proc(char *page, char **start, off_t off, int count,
1360 int *eof, void *data)
1364 struct slgt_info *info;
1366 len += sprintf(page, "synclink_gt driver:%s\n", driver_version);
1368 info = slgt_device_list;
1370 l = line_info(page + len, info);
1372 if (len+begin > off+count)
1374 if (len+begin < off) {
1378 info = info->next_device;
1383 if (off >= len+begin)
1385 *start = page + (off-begin);
1386 return ((count < begin+len-off) ? count : begin+len-off);
1390 * return count of bytes in transmit buffer
1392 static int chars_in_buffer(struct tty_struct *tty)
1394 struct slgt_info *info = tty->driver_data;
1395 if (sanity_check(info, tty->name, "chars_in_buffer"))
1397 DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, info->tx_count));
1398 return info->tx_count;
1402 * signal remote device to throttle send data (our receive data)
1404 static void throttle(struct tty_struct * tty)
1406 struct slgt_info *info = tty->driver_data;
1407 unsigned long flags;
1409 if (sanity_check(info, tty->name, "throttle"))
1411 DBGINFO(("%s throttle\n", info->device_name));
1413 send_xchar(tty, STOP_CHAR(tty));
1414 if (tty->termios->c_cflag & CRTSCTS) {
1415 spin_lock_irqsave(&info->lock,flags);
1416 info->signals &= ~SerialSignal_RTS;
1418 spin_unlock_irqrestore(&info->lock,flags);
1423 * signal remote device to stop throttling send data (our receive data)
1425 static void unthrottle(struct tty_struct * tty)
1427 struct slgt_info *info = tty->driver_data;
1428 unsigned long flags;
1430 if (sanity_check(info, tty->name, "unthrottle"))
1432 DBGINFO(("%s unthrottle\n", info->device_name));
1437 send_xchar(tty, START_CHAR(tty));
1439 if (tty->termios->c_cflag & CRTSCTS) {
1440 spin_lock_irqsave(&info->lock,flags);
1441 info->signals |= SerialSignal_RTS;
1443 spin_unlock_irqrestore(&info->lock,flags);
1448 * set or clear transmit break condition
1449 * break_state -1=set break condition, 0=clear
1451 static void set_break(struct tty_struct *tty, int break_state)
1453 struct slgt_info *info = tty->driver_data;
1454 unsigned short value;
1455 unsigned long flags;
1457 if (sanity_check(info, tty->name, "set_break"))
1459 DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
1461 spin_lock_irqsave(&info->lock,flags);
1462 value = rd_reg16(info, TCR);
1463 if (break_state == -1)
1467 wr_reg16(info, TCR, value);
1468 spin_unlock_irqrestore(&info->lock,flags);
1471 #if SYNCLINK_GENERIC_HDLC
1474 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1475 * set encoding and frame check sequence (FCS) options
1477 * dev pointer to network device structure
1478 * encoding serial encoding setting
1479 * parity FCS setting
1481 * returns 0 if success, otherwise error code
1483 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
1484 unsigned short parity)
1486 struct slgt_info *info = dev_to_port(dev);
1487 unsigned char new_encoding;
1488 unsigned short new_crctype;
1490 /* return error if TTY interface open */
1491 if (info->port.count)
1494 DBGINFO(("%s hdlcdev_attach\n", info->device_name));
1498 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
1499 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
1500 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
1501 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
1502 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
1503 default: return -EINVAL;
1508 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
1509 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
1510 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
1511 default: return -EINVAL;
1514 info->params.encoding = new_encoding;
1515 info->params.crc_type = new_crctype;
1517 /* if network interface up, reprogram hardware */
1525 * called by generic HDLC layer to send frame
1527 * skb socket buffer containing HDLC frame
1528 * dev pointer to network device structure
1530 * returns 0 if success, otherwise error code
1532 static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
1534 struct slgt_info *info = dev_to_port(dev);
1535 unsigned long flags;
1537 DBGINFO(("%s hdlc_xmit\n", dev->name));
1539 /* stop sending until this frame completes */
1540 netif_stop_queue(dev);
1542 /* copy data to device buffers */
1543 info->tx_count = skb->len;
1544 tx_load(info, skb->data, skb->len);
1546 /* update network statistics */
1547 dev->stats.tx_packets++;
1548 dev->stats.tx_bytes += skb->len;
1550 /* done with socket buffer, so free it */
1553 /* save start time for transmit timeout detection */
1554 dev->trans_start = jiffies;
1556 /* start hardware transmitter if necessary */
1557 spin_lock_irqsave(&info->lock,flags);
1558 if (!info->tx_active)
1560 spin_unlock_irqrestore(&info->lock,flags);
1566 * called by network layer when interface enabled
1567 * claim resources and initialize hardware
1569 * dev pointer to network device structure
1571 * returns 0 if success, otherwise error code
1573 static int hdlcdev_open(struct net_device *dev)
1575 struct slgt_info *info = dev_to_port(dev);
1577 unsigned long flags;
1579 if (!try_module_get(THIS_MODULE))
1582 DBGINFO(("%s hdlcdev_open\n", dev->name));
1584 /* generic HDLC layer open processing */
1585 if ((rc = hdlc_open(dev)))
1588 /* arbitrate between network and tty opens */
1589 spin_lock_irqsave(&info->netlock, flags);
1590 if (info->port.count != 0 || info->netcount != 0) {
1591 DBGINFO(("%s hdlc_open busy\n", dev->name));
1592 spin_unlock_irqrestore(&info->netlock, flags);
1596 spin_unlock_irqrestore(&info->netlock, flags);
1598 /* claim resources and init adapter */
1599 if ((rc = startup(info)) != 0) {
1600 spin_lock_irqsave(&info->netlock, flags);
1602 spin_unlock_irqrestore(&info->netlock, flags);
1606 /* assert DTR and RTS, apply hardware settings */
1607 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
1610 /* enable network layer transmit */
1611 dev->trans_start = jiffies;
1612 netif_start_queue(dev);
1614 /* inform generic HDLC layer of current DCD status */
1615 spin_lock_irqsave(&info->lock, flags);
1617 spin_unlock_irqrestore(&info->lock, flags);
1618 if (info->signals & SerialSignal_DCD)
1619 netif_carrier_on(dev);
1621 netif_carrier_off(dev);
1626 * called by network layer when interface is disabled
1627 * shutdown hardware and release resources
1629 * dev pointer to network device structure
1631 * returns 0 if success, otherwise error code
1633 static int hdlcdev_close(struct net_device *dev)
1635 struct slgt_info *info = dev_to_port(dev);
1636 unsigned long flags;
1638 DBGINFO(("%s hdlcdev_close\n", dev->name));
1640 netif_stop_queue(dev);
1642 /* shutdown adapter and release resources */
1647 spin_lock_irqsave(&info->netlock, flags);
1649 spin_unlock_irqrestore(&info->netlock, flags);
1651 module_put(THIS_MODULE);
1656 * called by network layer to process IOCTL call to network device
1658 * dev pointer to network device structure
1659 * ifr pointer to network interface request structure
1660 * cmd IOCTL command code
1662 * returns 0 if success, otherwise error code
1664 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1666 const size_t size = sizeof(sync_serial_settings);
1667 sync_serial_settings new_line;
1668 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1669 struct slgt_info *info = dev_to_port(dev);
1672 DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
1674 /* return error if TTY interface open */
1675 if (info->port.count)
1678 if (cmd != SIOCWANDEV)
1679 return hdlc_ioctl(dev, ifr, cmd);
1681 switch(ifr->ifr_settings.type) {
1682 case IF_GET_IFACE: /* return current sync_serial_settings */
1684 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1685 if (ifr->ifr_settings.size < size) {
1686 ifr->ifr_settings.size = size; /* data size wanted */
1690 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1691 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1692 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1693 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1696 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
1697 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
1698 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
1699 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
1700 default: new_line.clock_type = CLOCK_DEFAULT;
1703 new_line.clock_rate = info->params.clock_speed;
1704 new_line.loopback = info->params.loopback ? 1:0;
1706 if (copy_to_user(line, &new_line, size))
1710 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
1712 if(!capable(CAP_NET_ADMIN))
1714 if (copy_from_user(&new_line, line, size))
1717 switch (new_line.clock_type)
1719 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
1720 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
1721 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
1722 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
1723 case CLOCK_DEFAULT: flags = info->params.flags &
1724 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1725 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1726 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1727 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
1728 default: return -EINVAL;
1731 if (new_line.loopback != 0 && new_line.loopback != 1)
1734 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1735 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1736 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1737 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1738 info->params.flags |= flags;
1740 info->params.loopback = new_line.loopback;
1742 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
1743 info->params.clock_speed = new_line.clock_rate;
1745 info->params.clock_speed = 0;
1747 /* if network interface up, reprogram hardware */
1753 return hdlc_ioctl(dev, ifr, cmd);
1758 * called by network layer when transmit timeout is detected
1760 * dev pointer to network device structure
1762 static void hdlcdev_tx_timeout(struct net_device *dev)
1764 struct slgt_info *info = dev_to_port(dev);
1765 unsigned long flags;
1767 DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
1769 dev->stats.tx_errors++;
1770 dev->stats.tx_aborted_errors++;
1772 spin_lock_irqsave(&info->lock,flags);
1774 spin_unlock_irqrestore(&info->lock,flags);
1776 netif_wake_queue(dev);
1780 * called by device driver when transmit completes
1781 * reenable network layer transmit if stopped
1783 * info pointer to device instance information
1785 static void hdlcdev_tx_done(struct slgt_info *info)
1787 if (netif_queue_stopped(info->netdev))
1788 netif_wake_queue(info->netdev);
1792 * called by device driver when frame received
1793 * pass frame to network layer
1795 * info pointer to device instance information
1796 * buf pointer to buffer contianing frame data
1797 * size count of data bytes in buf
1799 static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
1801 struct sk_buff *skb = dev_alloc_skb(size);
1802 struct net_device *dev = info->netdev;
1804 DBGINFO(("%s hdlcdev_rx\n", dev->name));
1807 DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
1808 dev->stats.rx_dropped++;
1812 memcpy(skb_put(skb, size), buf, size);
1814 skb->protocol = hdlc_type_trans(skb, dev);
1816 dev->stats.rx_packets++;
1817 dev->stats.rx_bytes += size;
1821 dev->last_rx = jiffies;
1825 * called by device driver when adding device instance
1826 * do generic HDLC initialization
1828 * info pointer to device instance information
1830 * returns 0 if success, otherwise error code
1832 static int hdlcdev_init(struct slgt_info *info)
1835 struct net_device *dev;
1838 /* allocate and initialize network and HDLC layer objects */
1840 if (!(dev = alloc_hdlcdev(info))) {
1841 printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
1845 /* for network layer reporting purposes only */
1846 dev->mem_start = info->phys_reg_addr;
1847 dev->mem_end = info->phys_reg_addr + SLGT_REG_SIZE - 1;
1848 dev->irq = info->irq_level;
1850 /* network layer callbacks and settings */
1851 dev->do_ioctl = hdlcdev_ioctl;
1852 dev->open = hdlcdev_open;
1853 dev->stop = hdlcdev_close;
1854 dev->tx_timeout = hdlcdev_tx_timeout;
1855 dev->watchdog_timeo = 10*HZ;
1856 dev->tx_queue_len = 50;
1858 /* generic HDLC layer callbacks and settings */
1859 hdlc = dev_to_hdlc(dev);
1860 hdlc->attach = hdlcdev_attach;
1861 hdlc->xmit = hdlcdev_xmit;
1863 /* register objects with HDLC layer */
1864 if ((rc = register_hdlc_device(dev))) {
1865 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
1875 * called by device driver when removing device instance
1876 * do generic HDLC cleanup
1878 * info pointer to device instance information
1880 static void hdlcdev_exit(struct slgt_info *info)
1882 unregister_hdlc_device(info->netdev);
1883 free_netdev(info->netdev);
1884 info->netdev = NULL;
1887 #endif /* ifdef CONFIG_HDLC */
1890 * get async data from rx DMA buffers
1892 static void rx_async(struct slgt_info *info)
1894 struct tty_struct *tty = info->port.tty;
1895 struct mgsl_icount *icount = &info->icount;
1896 unsigned int start, end;
1898 unsigned char status;
1899 struct slgt_desc *bufs = info->rbufs;
1905 start = end = info->rbuf_current;
1907 while(desc_complete(bufs[end])) {
1908 count = desc_count(bufs[end]) - info->rbuf_index;
1909 p = bufs[end].buf + info->rbuf_index;
1911 DBGISR(("%s rx_async count=%d\n", info->device_name, count));
1912 DBGDATA(info, p, count, "rx");
1914 for(i=0 ; i < count; i+=2, p+=2) {
1920 if ((status = *(p+1) & (BIT1 + BIT0))) {
1923 else if (status & BIT0)
1925 /* discard char if tty control flags say so */
1926 if (status & info->ignore_status_mask)
1930 else if (status & BIT0)
1934 tty_insert_flip_char(tty, ch, stat);
1940 /* receive buffer not completed */
1941 info->rbuf_index += i;
1942 mod_timer(&info->rx_timer, jiffies + 1);
1946 info->rbuf_index = 0;
1947 free_rbufs(info, end, end);
1949 if (++end == info->rbuf_count)
1952 /* if entire list searched then no frame available */
1958 tty_flip_buffer_push(tty);
1962 * return next bottom half action to perform
1964 static int bh_action(struct slgt_info *info)
1966 unsigned long flags;
1969 spin_lock_irqsave(&info->lock,flags);
1971 if (info->pending_bh & BH_RECEIVE) {
1972 info->pending_bh &= ~BH_RECEIVE;
1974 } else if (info->pending_bh & BH_TRANSMIT) {
1975 info->pending_bh &= ~BH_TRANSMIT;
1977 } else if (info->pending_bh & BH_STATUS) {
1978 info->pending_bh &= ~BH_STATUS;
1981 /* Mark BH routine as complete */
1982 info->bh_running = false;
1983 info->bh_requested = false;
1987 spin_unlock_irqrestore(&info->lock,flags);
1993 * perform bottom half processing
1995 static void bh_handler(struct work_struct *work)
1997 struct slgt_info *info = container_of(work, struct slgt_info, task);
2002 info->bh_running = true;
2004 while((action = bh_action(info))) {
2007 DBGBH(("%s bh receive\n", info->device_name));
2008 switch(info->params.mode) {
2009 case MGSL_MODE_ASYNC:
2012 case MGSL_MODE_HDLC:
2013 while(rx_get_frame(info));
2016 case MGSL_MODE_MONOSYNC:
2017 case MGSL_MODE_BISYNC:
2018 while(rx_get_buf(info));
2021 /* restart receiver if rx DMA buffers exhausted */
2022 if (info->rx_restart)
2029 DBGBH(("%s bh status\n", info->device_name));
2030 info->ri_chkcount = 0;
2031 info->dsr_chkcount = 0;
2032 info->dcd_chkcount = 0;
2033 info->cts_chkcount = 0;
2036 DBGBH(("%s unknown action\n", info->device_name));
2040 DBGBH(("%s bh_handler exit\n", info->device_name));
2043 static void bh_transmit(struct slgt_info *info)
2045 struct tty_struct *tty = info->port.tty;
2047 DBGBH(("%s bh_transmit\n", info->device_name));
2052 static void dsr_change(struct slgt_info *info, unsigned short status)
2054 if (status & BIT3) {
2055 info->signals |= SerialSignal_DSR;
2056 info->input_signal_events.dsr_up++;
2058 info->signals &= ~SerialSignal_DSR;
2059 info->input_signal_events.dsr_down++;
2061 DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
2062 if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2063 slgt_irq_off(info, IRQ_DSR);
2067 wake_up_interruptible(&info->status_event_wait_q);
2068 wake_up_interruptible(&info->event_wait_q);
2069 info->pending_bh |= BH_STATUS;
2072 static void cts_change(struct slgt_info *info, unsigned short status)
2074 if (status & BIT2) {
2075 info->signals |= SerialSignal_CTS;
2076 info->input_signal_events.cts_up++;
2078 info->signals &= ~SerialSignal_CTS;
2079 info->input_signal_events.cts_down++;
2081 DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
2082 if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2083 slgt_irq_off(info, IRQ_CTS);
2087 wake_up_interruptible(&info->status_event_wait_q);
2088 wake_up_interruptible(&info->event_wait_q);
2089 info->pending_bh |= BH_STATUS;
2091 if (info->port.flags & ASYNC_CTS_FLOW) {
2092 if (info->port.tty) {
2093 if (info->port.tty->hw_stopped) {
2094 if (info->signals & SerialSignal_CTS) {
2095 info->port.tty->hw_stopped = 0;
2096 info->pending_bh |= BH_TRANSMIT;
2100 if (!(info->signals & SerialSignal_CTS))
2101 info->port.tty->hw_stopped = 1;
2107 static void dcd_change(struct slgt_info *info, unsigned short status)
2109 if (status & BIT1) {
2110 info->signals |= SerialSignal_DCD;
2111 info->input_signal_events.dcd_up++;
2113 info->signals &= ~SerialSignal_DCD;
2114 info->input_signal_events.dcd_down++;
2116 DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
2117 if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2118 slgt_irq_off(info, IRQ_DCD);
2122 #if SYNCLINK_GENERIC_HDLC
2123 if (info->netcount) {
2124 if (info->signals & SerialSignal_DCD)
2125 netif_carrier_on(info->netdev);
2127 netif_carrier_off(info->netdev);
2130 wake_up_interruptible(&info->status_event_wait_q);
2131 wake_up_interruptible(&info->event_wait_q);
2132 info->pending_bh |= BH_STATUS;
2134 if (info->port.flags & ASYNC_CHECK_CD) {
2135 if (info->signals & SerialSignal_DCD)
2136 wake_up_interruptible(&info->port.open_wait);
2139 tty_hangup(info->port.tty);
2144 static void ri_change(struct slgt_info *info, unsigned short status)
2146 if (status & BIT0) {
2147 info->signals |= SerialSignal_RI;
2148 info->input_signal_events.ri_up++;
2150 info->signals &= ~SerialSignal_RI;
2151 info->input_signal_events.ri_down++;
2153 DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
2154 if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2155 slgt_irq_off(info, IRQ_RI);
2159 wake_up_interruptible(&info->status_event_wait_q);
2160 wake_up_interruptible(&info->event_wait_q);
2161 info->pending_bh |= BH_STATUS;
2164 static void isr_serial(struct slgt_info *info)
2166 unsigned short status = rd_reg16(info, SSR);
2168 DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
2170 wr_reg16(info, SSR, status); /* clear pending */
2172 info->irq_occurred = true;
2174 if (info->params.mode == MGSL_MODE_ASYNC) {
2175 if (status & IRQ_TXIDLE) {
2177 isr_txeom(info, status);
2179 if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
2181 /* process break detection if tty control allows */
2182 if (info->port.tty) {
2183 if (!(status & info->ignore_status_mask)) {
2184 if (info->read_status_mask & MASK_BREAK) {
2185 tty_insert_flip_char(info->port.tty, 0, TTY_BREAK);
2186 if (info->port.flags & ASYNC_SAK)
2187 do_SAK(info->port.tty);
2193 if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
2194 isr_txeom(info, status);
2196 if (status & IRQ_RXIDLE) {
2197 if (status & RXIDLE)
2198 info->icount.rxidle++;
2200 info->icount.exithunt++;
2201 wake_up_interruptible(&info->event_wait_q);
2204 if (status & IRQ_RXOVER)
2208 if (status & IRQ_DSR)
2209 dsr_change(info, status);
2210 if (status & IRQ_CTS)
2211 cts_change(info, status);
2212 if (status & IRQ_DCD)
2213 dcd_change(info, status);
2214 if (status & IRQ_RI)
2215 ri_change(info, status);
2218 static void isr_rdma(struct slgt_info *info)
2220 unsigned int status = rd_reg32(info, RDCSR);
2222 DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
2224 /* RDCSR (rx DMA control/status)
2227 * 06 save status byte to DMA buffer
2229 * 04 eol (end of list)
2230 * 03 eob (end of buffer)
2235 wr_reg32(info, RDCSR, status); /* clear pending */
2237 if (status & (BIT5 + BIT4)) {
2238 DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
2239 info->rx_restart = true;
2241 info->pending_bh |= BH_RECEIVE;
2244 static void isr_tdma(struct slgt_info *info)
2246 unsigned int status = rd_reg32(info, TDCSR);
2248 DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
2250 /* TDCSR (tx DMA control/status)
2254 * 04 eol (end of list)
2255 * 03 eob (end of buffer)
2260 wr_reg32(info, TDCSR, status); /* clear pending */
2262 if (status & (BIT5 + BIT4 + BIT3)) {
2263 // another transmit buffer has completed
2264 // run bottom half to get more send data from user
2265 info->pending_bh |= BH_TRANSMIT;
2269 static void isr_txeom(struct slgt_info *info, unsigned short status)
2271 DBGISR(("%s txeom status=%04x\n", info->device_name, status));
2273 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
2276 if (status & IRQ_TXUNDER) {
2277 unsigned short val = rd_reg16(info, TCR);
2278 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
2279 wr_reg16(info, TCR, val); /* clear reset bit */
2282 if (info->tx_active) {
2283 if (info->params.mode != MGSL_MODE_ASYNC) {
2284 if (status & IRQ_TXUNDER)
2285 info->icount.txunder++;
2286 else if (status & IRQ_TXIDLE)
2287 info->icount.txok++;
2290 info->tx_active = false;
2293 del_timer(&info->tx_timer);
2295 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
2296 info->signals &= ~SerialSignal_RTS;
2297 info->drop_rts_on_tx_done = false;
2301 #if SYNCLINK_GENERIC_HDLC
2303 hdlcdev_tx_done(info);
2307 if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
2311 info->pending_bh |= BH_TRANSMIT;
2316 static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
2318 struct cond_wait *w, *prev;
2320 /* wake processes waiting for specific transitions */
2321 for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
2322 if (w->data & changed) {
2324 wake_up_interruptible(&w->q);
2326 prev->next = w->next;
2328 info->gpio_wait_q = w->next;
2334 /* interrupt service routine
2336 * irq interrupt number
2337 * dev_id device ID supplied during interrupt registration
2339 static irqreturn_t slgt_interrupt(int dummy, void *dev_id)
2341 struct slgt_info *info = dev_id;
2345 DBGISR(("slgt_interrupt irq=%d entry\n", info->irq_level));
2347 spin_lock(&info->lock);
2349 while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
2350 DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
2351 info->irq_occurred = true;
2352 for(i=0; i < info->port_count ; i++) {
2353 if (info->port_array[i] == NULL)
2355 if (gsr & (BIT8 << i))
2356 isr_serial(info->port_array[i]);
2357 if (gsr & (BIT16 << (i*2)))
2358 isr_rdma(info->port_array[i]);
2359 if (gsr & (BIT17 << (i*2)))
2360 isr_tdma(info->port_array[i]);
2364 if (info->gpio_present) {
2366 unsigned int changed;
2367 while ((changed = rd_reg32(info, IOSR)) != 0) {
2368 DBGISR(("%s iosr=%08x\n", info->device_name, changed));
2369 /* read latched state of GPIO signals */
2370 state = rd_reg32(info, IOVR);
2371 /* clear pending GPIO interrupt bits */
2372 wr_reg32(info, IOSR, changed);
2373 for (i=0 ; i < info->port_count ; i++) {
2374 if (info->port_array[i] != NULL)
2375 isr_gpio(info->port_array[i], changed, state);
2380 for(i=0; i < info->port_count ; i++) {
2381 struct slgt_info *port = info->port_array[i];
2383 if (port && (port->port.count || port->netcount) &&
2384 port->pending_bh && !port->bh_running &&
2385 !port->bh_requested) {
2386 DBGISR(("%s bh queued\n", port->device_name));
2387 schedule_work(&port->task);
2388 port->bh_requested = true;
2392 spin_unlock(&info->lock);
2394 DBGISR(("slgt_interrupt irq=%d exit\n", info->irq_level));
2398 static int startup(struct slgt_info *info)
2400 DBGINFO(("%s startup\n", info->device_name));
2402 if (info->port.flags & ASYNC_INITIALIZED)
2405 if (!info->tx_buf) {
2406 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
2407 if (!info->tx_buf) {
2408 DBGERR(("%s can't allocate tx buffer\n", info->device_name));
2413 info->pending_bh = 0;
2415 memset(&info->icount, 0, sizeof(info->icount));
2417 /* program hardware for current parameters */
2418 change_params(info);
2421 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
2423 info->port.flags |= ASYNC_INITIALIZED;
2429 * called by close() and hangup() to shutdown hardware
2431 static void shutdown(struct slgt_info *info)
2433 unsigned long flags;
2435 if (!(info->port.flags & ASYNC_INITIALIZED))
2438 DBGINFO(("%s shutdown\n", info->device_name));
2440 /* clear status wait queue because status changes */
2441 /* can't happen after shutting down the hardware */
2442 wake_up_interruptible(&info->status_event_wait_q);
2443 wake_up_interruptible(&info->event_wait_q);
2445 del_timer_sync(&info->tx_timer);
2446 del_timer_sync(&info->rx_timer);
2448 kfree(info->tx_buf);
2449 info->tx_buf = NULL;
2451 spin_lock_irqsave(&info->lock,flags);
2456 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
2458 if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
2459 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2463 flush_cond_wait(&info->gpio_wait_q);
2465 spin_unlock_irqrestore(&info->lock,flags);
2468 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
2470 info->port.flags &= ~ASYNC_INITIALIZED;
2473 static void program_hw(struct slgt_info *info)
2475 unsigned long flags;
2477 spin_lock_irqsave(&info->lock,flags);
2482 if (info->params.mode != MGSL_MODE_ASYNC ||
2490 info->dcd_chkcount = 0;
2491 info->cts_chkcount = 0;
2492 info->ri_chkcount = 0;
2493 info->dsr_chkcount = 0;
2495 slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR);
2498 if (info->netcount ||
2499 (info->port.tty && info->port.tty->termios->c_cflag & CREAD))
2502 spin_unlock_irqrestore(&info->lock,flags);
2506 * reconfigure adapter based on new parameters
2508 static void change_params(struct slgt_info *info)
2513 if (!info->port.tty || !info->port.tty->termios)
2515 DBGINFO(("%s change_params\n", info->device_name));
2517 cflag = info->port.tty->termios->c_cflag;
2519 /* if B0 rate (hangup) specified then negate DTR and RTS */
2520 /* otherwise assert DTR and RTS */
2522 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
2524 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2526 /* byte size and parity */
2528 switch (cflag & CSIZE) {
2529 case CS5: info->params.data_bits = 5; break;
2530 case CS6: info->params.data_bits = 6; break;
2531 case CS7: info->params.data_bits = 7; break;
2532 case CS8: info->params.data_bits = 8; break;
2533 default: info->params.data_bits = 7; break;
2536 info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
2539 info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
2541 info->params.parity = ASYNC_PARITY_NONE;
2543 /* calculate number of jiffies to transmit a full
2544 * FIFO (32 bytes) at specified data rate
2546 bits_per_char = info->params.data_bits +
2547 info->params.stop_bits + 1;
2549 info->params.data_rate = tty_get_baud_rate(info->port.tty);
2551 if (info->params.data_rate) {
2552 info->timeout = (32*HZ*bits_per_char) /
2553 info->params.data_rate;
2555 info->timeout += HZ/50; /* Add .02 seconds of slop */
2557 if (cflag & CRTSCTS)
2558 info->port.flags |= ASYNC_CTS_FLOW;
2560 info->port.flags &= ~ASYNC_CTS_FLOW;
2563 info->port.flags &= ~ASYNC_CHECK_CD;
2565 info->port.flags |= ASYNC_CHECK_CD;
2567 /* process tty input control flags */
2569 info->read_status_mask = IRQ_RXOVER;
2570 if (I_INPCK(info->port.tty))
2571 info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
2572 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
2573 info->read_status_mask |= MASK_BREAK;
2574 if (I_IGNPAR(info->port.tty))
2575 info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
2576 if (I_IGNBRK(info->port.tty)) {
2577 info->ignore_status_mask |= MASK_BREAK;
2578 /* If ignoring parity and break indicators, ignore
2579 * overruns too. (For real raw support).
2581 if (I_IGNPAR(info->port.tty))
2582 info->ignore_status_mask |= MASK_OVERRUN;
2588 static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
2590 DBGINFO(("%s get_stats\n", info->device_name));
2592 memset(&info->icount, 0, sizeof(info->icount));
2594 if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
2600 static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
2602 DBGINFO(("%s get_params\n", info->device_name));
2603 if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
2608 static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
2610 unsigned long flags;
2611 MGSL_PARAMS tmp_params;
2613 DBGINFO(("%s set_params\n", info->device_name));
2614 if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
2617 spin_lock_irqsave(&info->lock, flags);
2618 memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
2619 spin_unlock_irqrestore(&info->lock, flags);
2621 change_params(info);
2626 static int get_txidle(struct slgt_info *info, int __user *idle_mode)
2628 DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
2629 if (put_user(info->idle_mode, idle_mode))
2634 static int set_txidle(struct slgt_info *info, int idle_mode)
2636 unsigned long flags;
2637 DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
2638 spin_lock_irqsave(&info->lock,flags);
2639 info->idle_mode = idle_mode;
2640 if (info->params.mode != MGSL_MODE_ASYNC)
2642 spin_unlock_irqrestore(&info->lock,flags);
2646 static int tx_enable(struct slgt_info *info, int enable)
2648 unsigned long flags;
2649 DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
2650 spin_lock_irqsave(&info->lock,flags);
2652 if (!info->tx_enabled)
2655 if (info->tx_enabled)
2658 spin_unlock_irqrestore(&info->lock,flags);
2663 * abort transmit HDLC frame
2665 static int tx_abort(struct slgt_info *info)
2667 unsigned long flags;
2668 DBGINFO(("%s tx_abort\n", info->device_name));
2669 spin_lock_irqsave(&info->lock,flags);
2671 spin_unlock_irqrestore(&info->lock,flags);
2675 static int rx_enable(struct slgt_info *info, int enable)
2677 unsigned long flags;
2678 DBGINFO(("%s rx_enable(%d)\n", info->device_name, enable));
2679 spin_lock_irqsave(&info->lock,flags);
2681 if (!info->rx_enabled)
2683 else if (enable == 2) {
2684 /* force hunt mode (write 1 to RCR[3]) */
2685 wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
2688 if (info->rx_enabled)
2691 spin_unlock_irqrestore(&info->lock,flags);
2696 * wait for specified event to occur
2698 static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
2700 unsigned long flags;
2703 struct mgsl_icount cprev, cnow;
2706 struct _input_signal_events oldsigs, newsigs;
2707 DECLARE_WAITQUEUE(wait, current);
2709 if (get_user(mask, mask_ptr))
2712 DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
2714 spin_lock_irqsave(&info->lock,flags);
2716 /* return immediately if state matches requested events */
2721 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2722 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2723 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2724 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2726 spin_unlock_irqrestore(&info->lock,flags);
2730 /* save current irq counts */
2731 cprev = info->icount;
2732 oldsigs = info->input_signal_events;
2734 /* enable hunt and idle irqs if needed */
2735 if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
2736 unsigned short val = rd_reg16(info, SCR);
2737 if (!(val & IRQ_RXIDLE))
2738 wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
2741 set_current_state(TASK_INTERRUPTIBLE);
2742 add_wait_queue(&info->event_wait_q, &wait);
2744 spin_unlock_irqrestore(&info->lock,flags);
2748 if (signal_pending(current)) {
2753 /* get current irq counts */
2754 spin_lock_irqsave(&info->lock,flags);
2755 cnow = info->icount;
2756 newsigs = info->input_signal_events;
2757 set_current_state(TASK_INTERRUPTIBLE);
2758 spin_unlock_irqrestore(&info->lock,flags);
2760 /* if no change, wait aborted for some reason */
2761 if (newsigs.dsr_up == oldsigs.dsr_up &&
2762 newsigs.dsr_down == oldsigs.dsr_down &&
2763 newsigs.dcd_up == oldsigs.dcd_up &&
2764 newsigs.dcd_down == oldsigs.dcd_down &&
2765 newsigs.cts_up == oldsigs.cts_up &&
2766 newsigs.cts_down == oldsigs.cts_down &&
2767 newsigs.ri_up == oldsigs.ri_up &&
2768 newsigs.ri_down == oldsigs.ri_down &&
2769 cnow.exithunt == cprev.exithunt &&
2770 cnow.rxidle == cprev.rxidle) {
2776 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2777 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2778 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2779 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2780 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2781 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2782 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2783 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2784 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2785 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2793 remove_wait_queue(&info->event_wait_q, &wait);
2794 set_current_state(TASK_RUNNING);
2797 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2798 spin_lock_irqsave(&info->lock,flags);
2799 if (!waitqueue_active(&info->event_wait_q)) {
2800 /* disable enable exit hunt mode/idle rcvd IRQs */
2802 (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
2804 spin_unlock_irqrestore(&info->lock,flags);
2808 rc = put_user(events, mask_ptr);
2812 static int get_interface(struct slgt_info *info, int __user *if_mode)
2814 DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
2815 if (put_user(info->if_mode, if_mode))
2820 static int set_interface(struct slgt_info *info, int if_mode)
2822 unsigned long flags;
2825 DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
2826 spin_lock_irqsave(&info->lock,flags);
2827 info->if_mode = if_mode;
2831 /* TCR (tx control) 07 1=RTS driver control */
2832 val = rd_reg16(info, TCR);
2833 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
2837 wr_reg16(info, TCR, val);
2839 spin_unlock_irqrestore(&info->lock,flags);
2844 * set general purpose IO pin state and direction
2847 * state each bit indicates a pin state
2848 * smask set bit indicates pin state to set
2849 * dir each bit indicates a pin direction (0=input, 1=output)
2850 * dmask set bit indicates pin direction to set
2852 static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2854 unsigned long flags;
2855 struct gpio_desc gpio;
2858 if (!info->gpio_present)
2860 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2862 DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
2863 info->device_name, gpio.state, gpio.smask,
2864 gpio.dir, gpio.dmask));
2866 spin_lock_irqsave(&info->lock,flags);
2868 data = rd_reg32(info, IODR);
2869 data |= gpio.dmask & gpio.dir;
2870 data &= ~(gpio.dmask & ~gpio.dir);
2871 wr_reg32(info, IODR, data);
2874 data = rd_reg32(info, IOVR);
2875 data |= gpio.smask & gpio.state;
2876 data &= ~(gpio.smask & ~gpio.state);
2877 wr_reg32(info, IOVR, data);
2879 spin_unlock_irqrestore(&info->lock,flags);
2885 * get general purpose IO pin state and direction
2887 static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2889 struct gpio_desc gpio;
2890 if (!info->gpio_present)
2892 gpio.state = rd_reg32(info, IOVR);
2893 gpio.smask = 0xffffffff;
2894 gpio.dir = rd_reg32(info, IODR);
2895 gpio.dmask = 0xffffffff;
2896 if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
2898 DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
2899 info->device_name, gpio.state, gpio.dir));
2904 * conditional wait facility
2906 static void init_cond_wait(struct cond_wait *w, unsigned int data)
2908 init_waitqueue_head(&w->q);
2909 init_waitqueue_entry(&w->wait, current);
2913 static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
2915 set_current_state(TASK_INTERRUPTIBLE);
2916 add_wait_queue(&w->q, &w->wait);
2921 static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
2923 struct cond_wait *w, *prev;
2924 remove_wait_queue(&cw->q, &cw->wait);
2925 set_current_state(TASK_RUNNING);
2926 for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
2929 prev->next = w->next;
2937 static void flush_cond_wait(struct cond_wait **head)
2939 while (*head != NULL) {
2940 wake_up_interruptible(&(*head)->q);
2941 *head = (*head)->next;
2946 * wait for general purpose I/O pin(s) to enter specified state
2949 * state - bit indicates target pin state
2950 * smask - set bit indicates watched pin
2952 * The wait ends when at least one watched pin enters the specified
2953 * state. When 0 (no error) is returned, user_gpio->state is set to the
2954 * state of all GPIO pins when the wait ends.
2956 * Note: Each pin may be a dedicated input, dedicated output, or
2957 * configurable input/output. The number and configuration of pins
2958 * varies with the specific adapter model. Only input pins (dedicated
2959 * or configured) can be monitored with this function.
2961 static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2963 unsigned long flags;
2965 struct gpio_desc gpio;
2966 struct cond_wait wait;
2969 if (!info->gpio_present)
2971 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2973 DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
2974 info->device_name, gpio.state, gpio.smask));
2975 /* ignore output pins identified by set IODR bit */
2976 if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
2978 init_cond_wait(&wait, gpio.smask);
2980 spin_lock_irqsave(&info->lock, flags);
2981 /* enable interrupts for watched pins */
2982 wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
2983 /* get current pin states */
2984 state = rd_reg32(info, IOVR);
2986 if (gpio.smask & ~(state ^ gpio.state)) {
2987 /* already in target state */
2990 /* wait for target state */
2991 add_cond_wait(&info->gpio_wait_q, &wait);
2992 spin_unlock_irqrestore(&info->lock, flags);
2994 if (signal_pending(current))
2997 gpio.state = wait.data;
2998 spin_lock_irqsave(&info->lock, flags);
2999 remove_cond_wait(&info->gpio_wait_q, &wait);
3002 /* disable all GPIO interrupts if no waiting processes */
3003 if (info->gpio_wait_q == NULL)
3004 wr_reg32(info, IOER, 0);
3005 spin_unlock_irqrestore(&info->lock,flags);
3007 if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
3012 static int modem_input_wait(struct slgt_info *info,int arg)
3014 unsigned long flags;
3016 struct mgsl_icount cprev, cnow;
3017 DECLARE_WAITQUEUE(wait, current);
3019 /* save current irq counts */
3020 spin_lock_irqsave(&info->lock,flags);
3021 cprev = info->icount;
3022 add_wait_queue(&info->status_event_wait_q, &wait);
3023 set_current_state(TASK_INTERRUPTIBLE);
3024 spin_unlock_irqrestore(&info->lock,flags);
3028 if (signal_pending(current)) {
3033 /* get new irq counts */
3034 spin_lock_irqsave(&info->lock,flags);
3035 cnow = info->icount;
3036 set_current_state(TASK_INTERRUPTIBLE);
3037 spin_unlock_irqrestore(&info->lock,flags);
3039 /* if no change, wait aborted for some reason */
3040 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
3041 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
3046 /* check for change in caller specified modem input */
3047 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
3048 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
3049 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
3050 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
3057 remove_wait_queue(&info->status_event_wait_q, &wait);
3058 set_current_state(TASK_RUNNING);
3063 * return state of serial control and status signals
3065 static int tiocmget(struct tty_struct *tty, struct file *file)
3067 struct slgt_info *info = tty->driver_data;
3068 unsigned int result;
3069 unsigned long flags;
3071 spin_lock_irqsave(&info->lock,flags);
3073 spin_unlock_irqrestore(&info->lock,flags);
3075 result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
3076 ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
3077 ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
3078 ((info->signals & SerialSignal_RI) ? TIOCM_RNG:0) +
3079 ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
3080 ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
3082 DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
3087 * set modem control signals (DTR/RTS)
3089 * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
3090 * TIOCMSET = set/clear signal values
3091 * value bit mask for command
3093 static int tiocmset(struct tty_struct *tty, struct file *file,
3094 unsigned int set, unsigned int clear)
3096 struct slgt_info *info = tty->driver_data;
3097 unsigned long flags;
3099 DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
3101 if (set & TIOCM_RTS)
3102 info->signals |= SerialSignal_RTS;
3103 if (set & TIOCM_DTR)
3104 info->signals |= SerialSignal_DTR;
3105 if (clear & TIOCM_RTS)
3106 info->signals &= ~SerialSignal_RTS;
3107 if (clear & TIOCM_DTR)
3108 info->signals &= ~SerialSignal_DTR;
3110 spin_lock_irqsave(&info->lock,flags);
3112 spin_unlock_irqrestore(&info->lock,flags);
3117 * block current process until the device is ready to open
3119 static int block_til_ready(struct tty_struct *tty, struct file *filp,
3120 struct slgt_info *info)
3122 DECLARE_WAITQUEUE(wait, current);
3124 bool do_clocal = false;
3125 bool extra_count = false;
3126 unsigned long flags;
3128 DBGINFO(("%s block_til_ready\n", tty->driver->name));
3130 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3131 /* nonblock mode is set or port is not enabled */
3132 info->port.flags |= ASYNC_NORMAL_ACTIVE;
3136 if (tty->termios->c_cflag & CLOCAL)
3139 /* Wait for carrier detect and the line to become
3140 * free (i.e., not in use by the callout). While we are in
3141 * this loop, info->port.count is dropped by one, so that
3142 * close() knows when to free things. We restore it upon
3143 * exit, either normal or abnormal.
3147 add_wait_queue(&info->port.open_wait, &wait);
3149 spin_lock_irqsave(&info->lock, flags);
3150 if (!tty_hung_up_p(filp)) {
3154 spin_unlock_irqrestore(&info->lock, flags);
3155 info->port.blocked_open++;
3158 if ((tty->termios->c_cflag & CBAUD)) {
3159 spin_lock_irqsave(&info->lock,flags);
3160 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
3162 spin_unlock_irqrestore(&info->lock,flags);
3165 set_current_state(TASK_INTERRUPTIBLE);
3167 if (tty_hung_up_p(filp) || !(info->port.flags & ASYNC_INITIALIZED)){
3168 retval = (info->port.flags & ASYNC_HUP_NOTIFY) ?
3169 -EAGAIN : -ERESTARTSYS;
3173 spin_lock_irqsave(&info->lock,flags);
3175 spin_unlock_irqrestore(&info->lock,flags);
3177 if (!(info->port.flags & ASYNC_CLOSING) &&
3178 (do_clocal || (info->signals & SerialSignal_DCD)) ) {
3182 if (signal_pending(current)) {
3183 retval = -ERESTARTSYS;
3187 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
3191 set_current_state(TASK_RUNNING);
3192 remove_wait_queue(&info->port.open_wait, &wait);
3196 info->port.blocked_open--;
3199 info->port.flags |= ASYNC_NORMAL_ACTIVE;
3201 DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
3205 static int alloc_tmp_rbuf(struct slgt_info *info)
3207 info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
3208 if (info->tmp_rbuf == NULL)
3213 static void free_tmp_rbuf(struct slgt_info *info)
3215 kfree(info->tmp_rbuf);
3216 info->tmp_rbuf = NULL;
3220 * allocate DMA descriptor lists.
3222 static int alloc_desc(struct slgt_info *info)
3227 /* allocate memory to hold descriptor lists */
3228 info->bufs = pci_alloc_consistent(info->pdev, DESC_LIST_SIZE, &info->bufs_dma_addr);
3229 if (info->bufs == NULL)
3232 memset(info->bufs, 0, DESC_LIST_SIZE);
3234 info->rbufs = (struct slgt_desc*)info->bufs;
3235 info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
3237 pbufs = (unsigned int)info->bufs_dma_addr;
3240 * Build circular lists of descriptors
3243 for (i=0; i < info->rbuf_count; i++) {
3244 /* physical address of this descriptor */
3245 info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
3247 /* physical address of next descriptor */
3248 if (i == info->rbuf_count - 1)
3249 info->rbufs[i].next = cpu_to_le32(pbufs);
3251 info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
3252 set_desc_count(info->rbufs[i], DMABUFSIZE);
3255 for (i=0; i < info->tbuf_count; i++) {
3256 /* physical address of this descriptor */
3257 info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
3259 /* physical address of next descriptor */
3260 if (i == info->tbuf_count - 1)
3261 info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
3263 info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
3269 static void free_desc(struct slgt_info *info)
3271 if (info->bufs != NULL) {
3272 pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
3279 static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3282 for (i=0; i < count; i++) {
3283 if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
3285 bufs[i].pbuf = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
3290 static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3293 for (i=0; i < count; i++) {
3294 if (bufs[i].buf == NULL)
3296 pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
3301 static int alloc_dma_bufs(struct slgt_info *info)
3303 info->rbuf_count = 32;
3304 info->tbuf_count = 32;
3306 if (alloc_desc(info) < 0 ||
3307 alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
3308 alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
3309 alloc_tmp_rbuf(info) < 0) {
3310 DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
3317 static void free_dma_bufs(struct slgt_info *info)
3320 free_bufs(info, info->rbufs, info->rbuf_count);
3321 free_bufs(info, info->tbufs, info->tbuf_count);
3324 free_tmp_rbuf(info);
3327 static int claim_resources(struct slgt_info *info)
3329 if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
3330 DBGERR(("%s reg addr conflict, addr=%08X\n",
3331 info->device_name, info->phys_reg_addr));
3332 info->init_error = DiagStatus_AddressConflict;
3336 info->reg_addr_requested = true;
3338 info->reg_addr = ioremap_nocache(info->phys_reg_addr, SLGT_REG_SIZE);
3339 if (!info->reg_addr) {
3340 DBGERR(("%s cant map device registers, addr=%08X\n",
3341 info->device_name, info->phys_reg_addr));
3342 info->init_error = DiagStatus_CantAssignPciResources;
3348 release_resources(info);
3352 static void release_resources(struct slgt_info *info)
3354 if (info->irq_requested) {
3355 free_irq(info->irq_level, info);
3356 info->irq_requested = false;
3359 if (info->reg_addr_requested) {
3360 release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
3361 info->reg_addr_requested = false;
3364 if (info->reg_addr) {
3365 iounmap(info->reg_addr);
3366 info->reg_addr = NULL;
3370 /* Add the specified device instance data structure to the
3371 * global linked list of devices and increment the device count.
3373 static void add_device(struct slgt_info *info)
3377 info->next_device = NULL;
3378 info->line = slgt_device_count;
3379 sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
3381 if (info->line < MAX_DEVICES) {
3382 if (maxframe[info->line])
3383 info->max_frame_size = maxframe[info->line];
3386 slgt_device_count++;
3388 if (!slgt_device_list)
3389 slgt_device_list = info;
3391 struct slgt_info *current_dev = slgt_device_list;
3392 while(current_dev->next_device)
3393 current_dev = current_dev->next_device;
3394 current_dev->next_device = info;
3397 if (info->max_frame_size < 4096)
3398 info->max_frame_size = 4096;
3399 else if (info->max_frame_size > 65535)
3400 info->max_frame_size = 65535;
3402 switch(info->pdev->device) {
3403 case SYNCLINK_GT_DEVICE_ID:
3406 case SYNCLINK_GT2_DEVICE_ID:
3409 case SYNCLINK_GT4_DEVICE_ID:
3412 case SYNCLINK_AC_DEVICE_ID:
3414 info->params.mode = MGSL_MODE_ASYNC;
3417 devstr = "(unknown model)";
3419 printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
3420 devstr, info->device_name, info->phys_reg_addr,
3421 info->irq_level, info->max_frame_size);
3423 #if SYNCLINK_GENERIC_HDLC
3429 * allocate device instance structure, return NULL on failure
3431 static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
3433 struct slgt_info *info;
3435 info = kzalloc(sizeof(struct slgt_info), GFP_KERNEL);
3438 DBGERR(("%s device alloc failed adapter=%d port=%d\n",
3439 driver_name, adapter_num, port_num));
3441 tty_port_init(&info->port);
3442 info->magic = MGSL_MAGIC;
3443 INIT_WORK(&info->task, bh_handler);
3444 info->max_frame_size = 4096;
3445 info->raw_rx_size = DMABUFSIZE;
3446 info->port.close_delay = 5*HZ/10;
3447 info->port.closing_wait = 30*HZ;
3448 init_waitqueue_head(&info->status_event_wait_q);
3449 init_waitqueue_head(&info->event_wait_q);
3450 spin_lock_init(&info->netlock);
3451 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
3452 info->idle_mode = HDLC_TXIDLE_FLAGS;
3453 info->adapter_num = adapter_num;
3454 info->port_num = port_num;
3456 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
3457 setup_timer(&info->rx_timer, rx_timeout, (unsigned long)info);
3459 /* Copy configuration info to device instance data */
3461 info->irq_level = pdev->irq;
3462 info->phys_reg_addr = pci_resource_start(pdev,0);
3464 info->bus_type = MGSL_BUS_TYPE_PCI;
3465 info->irq_flags = IRQF_SHARED;
3467 info->init_error = -1; /* assume error, set to 0 on successful init */
3473 static void device_init(int adapter_num, struct pci_dev *pdev)
3475 struct slgt_info *port_array[SLGT_MAX_PORTS];
3479 if (pdev->device == SYNCLINK_GT2_DEVICE_ID)
3481 else if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
3484 /* allocate device instances for all ports */
3485 for (i=0; i < port_count; ++i) {
3486 port_array[i] = alloc_dev(adapter_num, i, pdev);
3487 if (port_array[i] == NULL) {
3488 for (--i; i >= 0; --i)
3489 kfree(port_array[i]);
3494 /* give copy of port_array to all ports and add to device list */
3495 for (i=0; i < port_count; ++i) {
3496 memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
3497 add_device(port_array[i]);
3498 port_array[i]->port_count = port_count;
3499 spin_lock_init(&port_array[i]->lock);
3502 /* Allocate and claim adapter resources */
3503 if (!claim_resources(port_array[0])) {
3505 alloc_dma_bufs(port_array[0]);
3507 /* copy resource information from first port to others */
3508 for (i = 1; i < port_count; ++i) {
3509 port_array[i]->lock = port_array[0]->lock;
3510 port_array[i]->irq_level = port_array[0]->irq_level;
3511 port_array[i]->reg_addr = port_array[0]->reg_addr;
3512 alloc_dma_bufs(port_array[i]);
3515 if (request_irq(port_array[0]->irq_level,
3517 port_array[0]->irq_flags,
3518 port_array[0]->device_name,
3519 port_array[0]) < 0) {
3520 DBGERR(("%s request_irq failed IRQ=%d\n",
3521 port_array[0]->device_name,
3522 port_array[0]->irq_level));
3524 port_array[0]->irq_requested = true;
3525 adapter_test(port_array[0]);
3526 for (i=1 ; i < port_count ; i++) {
3527 port_array[i]->init_error = port_array[0]->init_error;
3528 port_array[i]->gpio_present = port_array[0]->gpio_present;
3533 for (i=0; i < port_count; ++i)
3534 tty_register_device(serial_driver, port_array[i]->line, &(port_array[i]->pdev->dev));
3537 static int __devinit init_one(struct pci_dev *dev,
3538 const struct pci_device_id *ent)
3540 if (pci_enable_device(dev)) {
3541 printk("error enabling pci device %p\n", dev);
3544 pci_set_master(dev);
3545 device_init(slgt_device_count, dev);
3549 static void __devexit remove_one(struct pci_dev *dev)
3553 static const struct tty_operations ops = {
3557 .put_char = put_char,
3558 .flush_chars = flush_chars,
3559 .write_room = write_room,
3560 .chars_in_buffer = chars_in_buffer,
3561 .flush_buffer = flush_buffer,
3563 .compat_ioctl = slgt_compat_ioctl,
3564 .throttle = throttle,
3565 .unthrottle = unthrottle,
3566 .send_xchar = send_xchar,
3567 .break_ctl = set_break,
3568 .wait_until_sent = wait_until_sent,
3569 .read_proc = read_proc,
3570 .set_termios = set_termios,
3572 .start = tx_release,
3574 .tiocmget = tiocmget,
3575 .tiocmset = tiocmset,
3578 static void slgt_cleanup(void)
3581 struct slgt_info *info;
3582 struct slgt_info *tmp;
3584 printk("unload %s %s\n", driver_name, driver_version);
3586 if (serial_driver) {
3587 for (info=slgt_device_list ; info != NULL ; info=info->next_device)
3588 tty_unregister_device(serial_driver, info->line);
3589 if ((rc = tty_unregister_driver(serial_driver)))
3590 DBGERR(("tty_unregister_driver error=%d\n", rc));
3591 put_tty_driver(serial_driver);
3595 info = slgt_device_list;
3598 info = info->next_device;
3601 /* release devices */
3602 info = slgt_device_list;
3604 #if SYNCLINK_GENERIC_HDLC
3607 free_dma_bufs(info);
3608 free_tmp_rbuf(info);
3609 if (info->port_num == 0)
3610 release_resources(info);
3612 info = info->next_device;
3617 pci_unregister_driver(&pci_driver);
3621 * Driver initialization entry point.
3623 static int __init slgt_init(void)
3627 printk("%s %s\n", driver_name, driver_version);
3629 serial_driver = alloc_tty_driver(MAX_DEVICES);
3630 if (!serial_driver) {
3631 printk("%s can't allocate tty driver\n", driver_name);
3635 /* Initialize the tty_driver structure */
3637 serial_driver->owner = THIS_MODULE;
3638 serial_driver->driver_name = tty_driver_name;
3639 serial_driver->name = tty_dev_prefix;
3640 serial_driver->major = ttymajor;
3641 serial_driver->minor_start = 64;
3642 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3643 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3644 serial_driver->init_termios = tty_std_termios;
3645 serial_driver->init_termios.c_cflag =
3646 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3647 serial_driver->init_termios.c_ispeed = 9600;
3648 serial_driver->init_termios.c_ospeed = 9600;
3649 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3650 tty_set_operations(serial_driver, &ops);
3651 if ((rc = tty_register_driver(serial_driver)) < 0) {
3652 DBGERR(("%s can't register serial driver\n", driver_name));
3653 put_tty_driver(serial_driver);
3654 serial_driver = NULL;
3658 printk("%s %s, tty major#%d\n",
3659 driver_name, driver_version,
3660 serial_driver->major);
3662 slgt_device_count = 0;
3663 if ((rc = pci_register_driver(&pci_driver)) < 0) {
3664 printk("%s pci_register_driver error=%d\n", driver_name, rc);
3667 pci_registered = true;
3669 if (!slgt_device_list)
3670 printk("%s no devices found\n",driver_name);
3679 static void __exit slgt_exit(void)
3684 module_init(slgt_init);
3685 module_exit(slgt_exit);
3688 * register access routines
3691 #define CALC_REGADDR() \
3692 unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
3694 reg_addr += (info->port_num) * 32;
3696 static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
3699 return readb((void __iomem *)reg_addr);
3702 static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
3705 writeb(value, (void __iomem *)reg_addr);
3708 static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
3711 return readw((void __iomem *)reg_addr);
3714 static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
3717 writew(value, (void __iomem *)reg_addr);
3720 static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
3723 return readl((void __iomem *)reg_addr);
3726 static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
3729 writel(value, (void __iomem *)reg_addr);
3732 static void rdma_reset(struct slgt_info *info)
3737 wr_reg32(info, RDCSR, BIT1);
3739 /* wait for enable bit cleared */
3740 for(i=0 ; i < 1000 ; i++)
3741 if (!(rd_reg32(info, RDCSR) & BIT0))
3745 static void tdma_reset(struct slgt_info *info)
3750 wr_reg32(info, TDCSR, BIT1);
3752 /* wait for enable bit cleared */
3753 for(i=0 ; i < 1000 ; i++)
3754 if (!(rd_reg32(info, TDCSR) & BIT0))
3759 * enable internal loopback
3760 * TxCLK and RxCLK are generated from BRG
3761 * and TxD is looped back to RxD internally.
3763 static void enable_loopback(struct slgt_info *info)
3765 /* SCR (serial control) BIT2=looopback enable */
3766 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
3768 if (info->params.mode != MGSL_MODE_ASYNC) {
3769 /* CCR (clock control)
3770 * 07..05 tx clock source (010 = BRG)
3771 * 04..02 rx clock source (010 = BRG)
3772 * 01 auxclk enable (0 = disable)
3773 * 00 BRG enable (1 = enable)
3777 wr_reg8(info, CCR, 0x49);
3779 /* set speed if available, otherwise use default */
3780 if (info->params.clock_speed)
3781 set_rate(info, info->params.clock_speed);
3783 set_rate(info, 3686400);
3788 * set baud rate generator to specified rate
3790 static void set_rate(struct slgt_info *info, u32 rate)
3793 static unsigned int osc = 14745600;
3795 /* div = osc/rate - 1
3797 * Round div up if osc/rate is not integer to
3798 * force to next slowest rate.
3803 if (!(osc % rate) && div)
3805 wr_reg16(info, BDR, (unsigned short)div);
3809 static void rx_stop(struct slgt_info *info)
3813 /* disable and reset receiver */
3814 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3815 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3816 wr_reg16(info, RCR, val); /* clear reset bit */
3818 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
3820 /* clear pending rx interrupts */
3821 wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
3825 info->rx_enabled = false;
3826 info->rx_restart = false;
3829 static void rx_start(struct slgt_info *info)
3833 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
3835 /* clear pending rx overrun IRQ */
3836 wr_reg16(info, SSR, IRQ_RXOVER);
3838 /* reset and disable receiver */
3839 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3840 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3841 wr_reg16(info, RCR, val); /* clear reset bit */
3846 /* set 1st descriptor address */
3847 wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
3849 if (info->params.mode != MGSL_MODE_ASYNC) {
3850 /* enable rx DMA and DMA interrupt */
3851 wr_reg32(info, RDCSR, (BIT2 + BIT0));
3853 /* enable saving of rx status, rx DMA and DMA interrupt */
3854 wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
3857 slgt_irq_on(info, IRQ_RXOVER);
3859 /* enable receiver */
3860 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
3862 info->rx_restart = false;
3863 info->rx_enabled = true;
3866 static void tx_start(struct slgt_info *info)
3868 if (!info->tx_enabled) {
3870 (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
3871 info->tx_enabled = true;
3874 if (info->tx_count) {
3875 info->drop_rts_on_tx_done = false;
3877 if (info->params.mode != MGSL_MODE_ASYNC) {
3878 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3880 if (!(info->signals & SerialSignal_RTS)) {
3881 info->signals |= SerialSignal_RTS;
3883 info->drop_rts_on_tx_done = true;
3887 slgt_irq_off(info, IRQ_TXDATA);
3888 slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
3889 /* clear tx idle and underrun status bits */
3890 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
3891 if (info->params.mode == MGSL_MODE_HDLC)
3892 mod_timer(&info->tx_timer, jiffies +
3893 msecs_to_jiffies(5000));
3895 slgt_irq_off(info, IRQ_TXDATA);
3896 slgt_irq_on(info, IRQ_TXIDLE);
3897 /* clear tx idle status bit */
3898 wr_reg16(info, SSR, IRQ_TXIDLE);
3901 info->tx_active = true;
3906 * start transmit DMA if inactive and there are unsent buffers
3908 static void tdma_start(struct slgt_info *info)
3912 if (rd_reg32(info, TDCSR) & BIT0)
3915 /* transmit DMA inactive, check for unsent buffers */
3916 i = info->tbuf_start;
3917 while (!desc_count(info->tbufs[i])) {
3918 if (++i == info->tbuf_count)
3920 if (i == info->tbuf_current)
3923 info->tbuf_start = i;
3925 /* there are unsent buffers, start transmit DMA */
3927 /* reset needed if previous error condition */
3930 /* set 1st descriptor address */
3931 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
3932 switch(info->params.mode) {
3934 case MGSL_MODE_MONOSYNC:
3935 case MGSL_MODE_BISYNC:
3936 wr_reg32(info, TDCSR, BIT2 + BIT0); /* IRQ + DMA enable */
3939 wr_reg32(info, TDCSR, BIT0); /* DMA enable */
3943 static void tx_stop(struct slgt_info *info)
3947 del_timer(&info->tx_timer);
3951 /* reset and disable transmitter */
3952 val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
3953 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
3955 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
3957 /* clear tx idle and underrun status bit */
3958 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
3962 info->tx_enabled = false;
3963 info->tx_active = false;
3966 static void reset_port(struct slgt_info *info)
3968 if (!info->reg_addr)
3974 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
3977 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
3980 static void reset_adapter(struct slgt_info *info)
3983 for (i=0; i < info->port_count; ++i) {
3984 if (info->port_array[i])
3985 reset_port(info->port_array[i]);
3989 static void async_mode(struct slgt_info *info)
3993 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
3999 * 15..13 mode, 010=async
4000 * 12..10 encoding, 000=NRZ
4002 * 08 1=odd parity, 0=even parity
4003 * 07 1=RTS driver control
4005 * 05..04 character length
4010 * 03 0=1 stop bit, 1=2 stop bits
4013 * 00 auto-CTS enable
4017 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4020 if (info->params.parity != ASYNC_PARITY_NONE) {
4022 if (info->params.parity == ASYNC_PARITY_ODD)
4026 switch (info->params.data_bits)
4028 case 6: val |= BIT4; break;
4029 case 7: val |= BIT5; break;
4030 case 8: val |= BIT5 + BIT4; break;
4033 if (info->params.stop_bits != 1)
4036 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4039 wr_reg16(info, TCR, val);
4043 * 15..13 mode, 010=async
4044 * 12..10 encoding, 000=NRZ
4046 * 08 1=odd parity, 0=even parity
4047 * 07..06 reserved, must be 0
4048 * 05..04 character length
4053 * 03 reserved, must be zero
4056 * 00 auto-DCD enable
4060 if (info->params.parity != ASYNC_PARITY_NONE) {
4062 if (info->params.parity == ASYNC_PARITY_ODD)
4066 switch (info->params.data_bits)
4068 case 6: val |= BIT4; break;
4069 case 7: val |= BIT5; break;
4070 case 8: val |= BIT5 + BIT4; break;
4073 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4076 wr_reg16(info, RCR, val);
4078 /* CCR (clock control)
4080 * 07..05 011 = tx clock source is BRG/16
4081 * 04..02 010 = rx clock source is BRG
4082 * 01 0 = auxclk disabled
4083 * 00 1 = BRG enabled
4087 wr_reg8(info, CCR, 0x69);
4091 /* SCR (serial control)
4093 * 15 1=tx req on FIFO half empty
4094 * 14 1=rx req on FIFO half full
4095 * 13 tx data IRQ enable
4096 * 12 tx idle IRQ enable
4097 * 11 rx break on IRQ enable
4098 * 10 rx data IRQ enable
4099 * 09 rx break off IRQ enable
4100 * 08 overrun IRQ enable
4105 * 03 reserved, must be zero
4106 * 02 1=txd->rxd internal loopback enable
4107 * 01 reserved, must be zero
4108 * 00 1=master IRQ enable
4110 val = BIT15 + BIT14 + BIT0;
4111 wr_reg16(info, SCR, val);
4113 slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
4115 set_rate(info, info->params.data_rate * 16);
4117 if (info->params.loopback)
4118 enable_loopback(info);
4121 static void sync_mode(struct slgt_info *info)
4125 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4131 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4135 * 07 1=RTS driver control
4136 * 06 preamble enable
4137 * 05..04 preamble length
4138 * 03 share open/close flag
4141 * 00 auto-CTS enable
4145 switch(info->params.mode) {
4146 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4147 case MGSL_MODE_BISYNC: val |= BIT15; break;
4148 case MGSL_MODE_RAW: val |= BIT13; break;
4150 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4153 switch(info->params.encoding)
4155 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4156 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4157 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4158 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4159 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4160 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4161 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4164 switch (info->params.crc_type & HDLC_CRC_MASK)
4166 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4167 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4170 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
4173 switch (info->params.preamble_length)
4175 case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
4176 case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
4177 case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
4180 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4183 wr_reg16(info, TCR, val);
4185 /* TPR (transmit preamble) */
4187 switch (info->params.preamble)
4189 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
4190 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
4191 case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
4192 case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
4193 case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
4194 default: val = 0x7e; break;
4196 wr_reg8(info, TPR, (unsigned char)val);
4200 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4204 * 07..03 reserved, must be 0
4207 * 00 auto-DCD enable
4211 switch(info->params.mode) {
4212 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4213 case MGSL_MODE_BISYNC: val |= BIT15; break;
4214 case MGSL_MODE_RAW: val |= BIT13; break;
4217 switch(info->params.encoding)
4219 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4220 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4221 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4222 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4223 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4224 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4225 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4228 switch (info->params.crc_type & HDLC_CRC_MASK)
4230 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4231 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4234 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4237 wr_reg16(info, RCR, val);
4239 /* CCR (clock control)
4241 * 07..05 tx clock source
4242 * 04..02 rx clock source
4248 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4250 // when RxC source is DPLL, BRG generates 16X DPLL
4251 // reference clock, so take TxC from BRG/16 to get
4252 // transmit clock at actual data rate
4253 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4254 val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
4256 val |= BIT6; /* 010, txclk = BRG */
4258 else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4259 val |= BIT7; /* 100, txclk = DPLL Input */
4260 else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4261 val |= BIT5; /* 001, txclk = RXC Input */
4263 if (info->params.flags & HDLC_FLAG_RXC_BRG)
4264 val |= BIT3; /* 010, rxclk = BRG */
4265 else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4266 val |= BIT4; /* 100, rxclk = DPLL */
4267 else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4268 val |= BIT2; /* 001, rxclk = TXC Input */
4270 if (info->params.clock_speed)
4273 wr_reg8(info, CCR, (unsigned char)val);
4275 if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
4277 // program DPLL mode
4278 switch(info->params.encoding)
4280 case HDLC_ENCODING_BIPHASE_MARK:
4281 case HDLC_ENCODING_BIPHASE_SPACE:
4283 case HDLC_ENCODING_BIPHASE_LEVEL:
4284 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
4285 val = BIT7 + BIT6; break;
4286 default: val = BIT6; // NRZ encodings
4288 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
4290 // DPLL requires a 16X reference clock from BRG
4291 set_rate(info, info->params.clock_speed * 16);
4294 set_rate(info, info->params.clock_speed);
4300 /* SCR (serial control)
4302 * 15 1=tx req on FIFO half empty
4303 * 14 1=rx req on FIFO half full
4304 * 13 tx data IRQ enable
4305 * 12 tx idle IRQ enable
4306 * 11 underrun IRQ enable
4307 * 10 rx data IRQ enable
4308 * 09 rx idle IRQ enable
4309 * 08 overrun IRQ enable
4314 * 03 reserved, must be zero
4315 * 02 1=txd->rxd internal loopback enable
4316 * 01 reserved, must be zero
4317 * 00 1=master IRQ enable
4319 wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
4321 if (info->params.loopback)
4322 enable_loopback(info);
4326 * set transmit idle mode
4328 static void tx_set_idle(struct slgt_info *info)
4333 /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
4334 * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
4336 tcr = rd_reg16(info, TCR);
4337 if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
4338 /* disable preamble, set idle size to 16 bits */
4339 tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
4340 /* MSB of 16 bit idle specified in tx preamble register (TPR) */
4341 wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
4342 } else if (!(tcr & BIT6)) {
4343 /* preamble is disabled, set idle size to 8 bits */
4344 tcr &= ~(BIT5 + BIT4);
4346 wr_reg16(info, TCR, tcr);
4348 if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
4349 /* LSB of custom tx idle specified in tx idle register */
4350 val = (unsigned char)(info->idle_mode & 0xff);
4352 /* standard 8 bit idle patterns */
4353 switch(info->idle_mode)
4355 case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
4356 case HDLC_TXIDLE_ALT_ZEROS_ONES:
4357 case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
4358 case HDLC_TXIDLE_ZEROS:
4359 case HDLC_TXIDLE_SPACE: val = 0x00; break;
4360 default: val = 0xff;
4364 wr_reg8(info, TIR, val);
4368 * get state of V24 status (input) signals
4370 static void get_signals(struct slgt_info *info)
4372 unsigned short status = rd_reg16(info, SSR);
4374 /* clear all serial signals except DTR and RTS */
4375 info->signals &= SerialSignal_DTR + SerialSignal_RTS;
4378 info->signals |= SerialSignal_DSR;
4380 info->signals |= SerialSignal_CTS;
4382 info->signals |= SerialSignal_DCD;
4384 info->signals |= SerialSignal_RI;
4388 * set V.24 Control Register based on current configuration
4390 static void msc_set_vcr(struct slgt_info *info)
4392 unsigned char val = 0;
4394 /* VCR (V.24 control)
4396 * 07..04 serial IF select
4403 switch(info->if_mode & MGSL_INTERFACE_MASK)
4405 case MGSL_INTERFACE_RS232:
4406 val |= BIT5; /* 0010 */
4408 case MGSL_INTERFACE_V35:
4409 val |= BIT7 + BIT6 + BIT5; /* 1110 */
4411 case MGSL_INTERFACE_RS422:
4412 val |= BIT6; /* 0100 */
4416 if (info->signals & SerialSignal_DTR)
4418 if (info->signals & SerialSignal_RTS)
4420 if (info->if_mode & MGSL_INTERFACE_LL)
4422 if (info->if_mode & MGSL_INTERFACE_RL)
4424 wr_reg8(info, VCR, val);
4428 * set state of V24 control (output) signals
4430 static void set_signals(struct slgt_info *info)
4432 unsigned char val = rd_reg8(info, VCR);
4433 if (info->signals & SerialSignal_DTR)
4437 if (info->signals & SerialSignal_RTS)
4441 wr_reg8(info, VCR, val);
4445 * free range of receive DMA buffers (i to last)
4447 static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
4452 /* reset current buffer for reuse */
4453 info->rbufs[i].status = 0;
4454 switch(info->params.mode) {
4456 case MGSL_MODE_MONOSYNC:
4457 case MGSL_MODE_BISYNC:
4458 set_desc_count(info->rbufs[i], info->raw_rx_size);
4461 set_desc_count(info->rbufs[i], DMABUFSIZE);
4466 if (++i == info->rbuf_count)
4469 info->rbuf_current = i;
4473 * mark all receive DMA buffers as free
4475 static void reset_rbufs(struct slgt_info *info)
4477 free_rbufs(info, 0, info->rbuf_count - 1);
4481 * pass receive HDLC frame to upper layer
4483 * return true if frame available, otherwise false
4485 static bool rx_get_frame(struct slgt_info *info)
4487 unsigned int start, end;
4488 unsigned short status;
4489 unsigned int framesize = 0;
4490 unsigned long flags;
4491 struct tty_struct *tty = info->port.tty;
4492 unsigned char addr_field = 0xff;
4493 unsigned int crc_size = 0;
4495 switch (info->params.crc_type & HDLC_CRC_MASK) {
4496 case HDLC_CRC_16_CCITT: crc_size = 2; break;
4497 case HDLC_CRC_32_CCITT: crc_size = 4; break;
4504 start = end = info->rbuf_current;
4507 if (!desc_complete(info->rbufs[end]))
4510 if (framesize == 0 && info->params.addr_filter != 0xff)
4511 addr_field = info->rbufs[end].buf[0];
4513 framesize += desc_count(info->rbufs[end]);
4515 if (desc_eof(info->rbufs[end]))
4518 if (++end == info->rbuf_count)
4521 if (end == info->rbuf_current) {
4522 if (info->rx_enabled){
4523 spin_lock_irqsave(&info->lock,flags);
4525 spin_unlock_irqrestore(&info->lock,flags);
4533 * 15 buffer complete
4536 * 02 eof (end of frame)
4540 status = desc_status(info->rbufs[end]);
4542 /* ignore CRC bit if not using CRC (bit is undefined) */
4543 if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
4546 if (framesize == 0 ||
4547 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4548 free_rbufs(info, start, end);
4552 if (framesize < (2 + crc_size) || status & BIT0) {
4553 info->icount.rxshort++;
4555 } else if (status & BIT1) {
4556 info->icount.rxcrc++;
4557 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
4561 #if SYNCLINK_GENERIC_HDLC
4562 if (framesize == 0) {
4563 info->netdev->stats.rx_errors++;
4564 info->netdev->stats.rx_frame_errors++;
4568 DBGBH(("%s rx frame status=%04X size=%d\n",
4569 info->device_name, status, framesize));
4570 DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, DMABUFSIZE), "rx");
4573 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
4574 framesize -= crc_size;
4578 if (framesize > info->max_frame_size + crc_size)
4579 info->icount.rxlong++;
4581 /* copy dma buffer(s) to contiguous temp buffer */
4582 int copy_count = framesize;
4584 unsigned char *p = info->tmp_rbuf;
4585 info->tmp_rbuf_count = framesize;
4587 info->icount.rxok++;
4590 int partial_count = min(copy_count, DMABUFSIZE);
4591 memcpy(p, info->rbufs[i].buf, partial_count);
4593 copy_count -= partial_count;
4594 if (++i == info->rbuf_count)
4598 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4599 *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
4603 #if SYNCLINK_GENERIC_HDLC
4605 hdlcdev_rx(info,info->tmp_rbuf, framesize);
4608 ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
4611 free_rbufs(info, start, end);
4619 * pass receive buffer (RAW synchronous mode) to tty layer
4620 * return true if buffer available, otherwise false
4622 static bool rx_get_buf(struct slgt_info *info)
4624 unsigned int i = info->rbuf_current;
4627 if (!desc_complete(info->rbufs[i]))
4629 count = desc_count(info->rbufs[i]);
4630 switch(info->params.mode) {
4631 case MGSL_MODE_MONOSYNC:
4632 case MGSL_MODE_BISYNC:
4633 /* ignore residue in byte synchronous modes */
4634 if (desc_residue(info->rbufs[i]))
4638 DBGDATA(info, info->rbufs[i].buf, count, "rx");
4639 DBGINFO(("rx_get_buf size=%d\n", count));
4641 ldisc_receive_buf(info->port.tty, info->rbufs[i].buf,
4642 info->flag_buf, count);
4643 free_rbufs(info, i, i);
4647 static void reset_tbufs(struct slgt_info *info)
4650 info->tbuf_current = 0;
4651 for (i=0 ; i < info->tbuf_count ; i++) {
4652 info->tbufs[i].status = 0;
4653 info->tbufs[i].count = 0;
4658 * return number of free transmit DMA buffers
4660 static unsigned int free_tbuf_count(struct slgt_info *info)
4662 unsigned int count = 0;
4663 unsigned int i = info->tbuf_current;
4667 if (desc_count(info->tbufs[i]))
4668 break; /* buffer in use */
4670 if (++i == info->tbuf_count)
4672 } while (i != info->tbuf_current);
4674 /* if tx DMA active, last zero count buffer is in use */
4675 if (count && (rd_reg32(info, TDCSR) & BIT0))
4682 * load transmit DMA buffer(s) with data
4684 static void tx_load(struct slgt_info *info, const char *buf, unsigned int size)
4686 unsigned short count;
4688 struct slgt_desc *d;
4693 DBGDATA(info, buf, size, "tx");
4695 info->tbuf_start = i = info->tbuf_current;
4698 d = &info->tbufs[i];
4699 if (++i == info->tbuf_count)
4702 count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
4703 memcpy(d->buf, buf, count);
4709 * set EOF bit for last buffer of HDLC frame or
4710 * for every buffer in raw mode
4712 if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
4713 info->params.mode == MGSL_MODE_RAW)
4714 set_desc_eof(*d, 1);
4716 set_desc_eof(*d, 0);
4718 set_desc_count(*d, count);
4721 info->tbuf_current = i;
4724 static int register_test(struct slgt_info *info)
4726 static unsigned short patterns[] =
4727 {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
4728 static unsigned int count = sizeof(patterns)/sizeof(patterns[0]);
4732 for (i=0 ; i < count ; i++) {
4733 wr_reg16(info, TIR, patterns[i]);
4734 wr_reg16(info, BDR, patterns[(i+1)%count]);
4735 if ((rd_reg16(info, TIR) != patterns[i]) ||
4736 (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
4741 info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
4742 info->init_error = rc ? 0 : DiagStatus_AddressFailure;
4746 static int irq_test(struct slgt_info *info)
4748 unsigned long timeout;
4749 unsigned long flags;
4750 struct tty_struct *oldtty = info->port.tty;
4751 u32 speed = info->params.data_rate;
4753 info->params.data_rate = 921600;
4754 info->port.tty = NULL;
4756 spin_lock_irqsave(&info->lock, flags);
4758 slgt_irq_on(info, IRQ_TXIDLE);
4760 /* enable transmitter */
4762 (unsigned short)(rd_reg16(info, TCR) | BIT1));
4764 /* write one byte and wait for tx idle */
4765 wr_reg16(info, TDR, 0);
4767 /* assume failure */
4768 info->init_error = DiagStatus_IrqFailure;
4769 info->irq_occurred = false;
4771 spin_unlock_irqrestore(&info->lock, flags);
4774 while(timeout-- && !info->irq_occurred)
4775 msleep_interruptible(10);
4777 spin_lock_irqsave(&info->lock,flags);
4779 spin_unlock_irqrestore(&info->lock,flags);
4781 info->params.data_rate = speed;
4782 info->port.tty = oldtty;
4784 info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
4785 return info->irq_occurred ? 0 : -ENODEV;
4788 static int loopback_test_rx(struct slgt_info *info)
4790 unsigned char *src, *dest;
4793 if (desc_complete(info->rbufs[0])) {
4794 count = desc_count(info->rbufs[0]);
4795 src = info->rbufs[0].buf;
4796 dest = info->tmp_rbuf;
4798 for( ; count ; count-=2, src+=2) {
4799 /* src=data byte (src+1)=status byte */
4800 if (!(*(src+1) & (BIT9 + BIT8))) {
4803 info->tmp_rbuf_count++;
4806 DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
4812 static int loopback_test(struct slgt_info *info)
4814 #define TESTFRAMESIZE 20
4816 unsigned long timeout;
4817 u16 count = TESTFRAMESIZE;
4818 unsigned char buf[TESTFRAMESIZE];
4820 unsigned long flags;
4822 struct tty_struct *oldtty = info->port.tty;
4825 memcpy(¶ms, &info->params, sizeof(params));
4827 info->params.mode = MGSL_MODE_ASYNC;
4828 info->params.data_rate = 921600;
4829 info->params.loopback = 1;
4830 info->port.tty = NULL;
4832 /* build and send transmit frame */
4833 for (count = 0; count < TESTFRAMESIZE; ++count)
4834 buf[count] = (unsigned char)count;
4836 info->tmp_rbuf_count = 0;
4837 memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
4839 /* program hardware for HDLC and enabled receiver */
4840 spin_lock_irqsave(&info->lock,flags);
4843 info->tx_count = count;
4844 tx_load(info, buf, count);
4846 spin_unlock_irqrestore(&info->lock, flags);
4848 /* wait for receive complete */
4849 for (timeout = 100; timeout; --timeout) {
4850 msleep_interruptible(10);
4851 if (loopback_test_rx(info)) {
4857 /* verify received frame length and contents */
4858 if (!rc && (info->tmp_rbuf_count != count ||
4859 memcmp(buf, info->tmp_rbuf, count))) {
4863 spin_lock_irqsave(&info->lock,flags);
4864 reset_adapter(info);
4865 spin_unlock_irqrestore(&info->lock,flags);
4867 memcpy(&info->params, ¶ms, sizeof(info->params));
4868 info->port.tty = oldtty;
4870 info->init_error = rc ? DiagStatus_DmaFailure : 0;
4874 static int adapter_test(struct slgt_info *info)
4876 DBGINFO(("testing %s\n", info->device_name));
4877 if (register_test(info) < 0) {
4878 printk("register test failure %s addr=%08X\n",
4879 info->device_name, info->phys_reg_addr);
4880 } else if (irq_test(info) < 0) {
4881 printk("IRQ test failure %s IRQ=%d\n",
4882 info->device_name, info->irq_level);
4883 } else if (loopback_test(info) < 0) {
4884 printk("loopback test failure %s\n", info->device_name);
4886 return info->init_error;
4890 * transmit timeout handler
4892 static void tx_timeout(unsigned long context)
4894 struct slgt_info *info = (struct slgt_info*)context;
4895 unsigned long flags;
4897 DBGINFO(("%s tx_timeout\n", info->device_name));
4898 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
4899 info->icount.txtimeout++;
4901 spin_lock_irqsave(&info->lock,flags);
4902 info->tx_active = false;
4904 spin_unlock_irqrestore(&info->lock,flags);
4906 #if SYNCLINK_GENERIC_HDLC
4908 hdlcdev_tx_done(info);
4915 * receive buffer polling timer
4917 static void rx_timeout(unsigned long context)
4919 struct slgt_info *info = (struct slgt_info*)context;
4920 unsigned long flags;
4922 DBGINFO(("%s rx_timeout\n", info->device_name));
4923 spin_lock_irqsave(&info->lock, flags);
4924 info->pending_bh |= BH_RECEIVE;
4925 spin_unlock_irqrestore(&info->lock, flags);
4926 bh_handler(&info->task);