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
];
131 static int dosyncppp
[MAX_DEVICES
];
133 module_param(ttymajor
, int, 0);
134 module_param(debug_level
, int, 0);
135 module_param_array(maxframe
, int, NULL
, 0);
136 module_param_array(dosyncppp
, int, NULL
, 0);
138 MODULE_PARM_DESC(ttymajor
, "TTY major device number override: 0=auto assigned");
139 MODULE_PARM_DESC(debug_level
, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
140 MODULE_PARM_DESC(maxframe
, "Maximum frame size used by device (4096 to 65535)");
141 MODULE_PARM_DESC(dosyncppp
, "Enable synchronous net device, 0=disable 1=enable");
144 * tty support and callbacks
146 static struct tty_driver
*serial_driver
;
148 static int open(struct tty_struct
*tty
, struct file
* filp
);
149 static void close(struct tty_struct
*tty
, struct file
* filp
);
150 static void hangup(struct tty_struct
*tty
);
151 static void set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
);
153 static int write(struct tty_struct
*tty
, const unsigned char *buf
, int count
);
154 static int put_char(struct tty_struct
*tty
, unsigned char ch
);
155 static void send_xchar(struct tty_struct
*tty
, char ch
);
156 static void wait_until_sent(struct tty_struct
*tty
, int timeout
);
157 static int write_room(struct tty_struct
*tty
);
158 static void flush_chars(struct tty_struct
*tty
);
159 static void flush_buffer(struct tty_struct
*tty
);
160 static void tx_hold(struct tty_struct
*tty
);
161 static void tx_release(struct tty_struct
*tty
);
163 static int ioctl(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
, unsigned long arg
);
164 static int read_proc(char *page
, char **start
, off_t off
, int count
,int *eof
, void *data
);
165 static int chars_in_buffer(struct tty_struct
*tty
);
166 static void throttle(struct tty_struct
* tty
);
167 static void unthrottle(struct tty_struct
* tty
);
168 static int set_break(struct tty_struct
*tty
, int break_state
);
171 * generic HDLC support and callbacks
173 #if SYNCLINK_GENERIC_HDLC
174 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
175 static void hdlcdev_tx_done(struct slgt_info
*info
);
176 static void hdlcdev_rx(struct slgt_info
*info
, char *buf
, int size
);
177 static int hdlcdev_init(struct slgt_info
*info
);
178 static void hdlcdev_exit(struct slgt_info
*info
);
183 * device specific structures, macros and functions
186 #define SLGT_MAX_PORTS 4
187 #define SLGT_REG_SIZE 256
190 * conditional wait facility
193 struct cond_wait
*next
;
198 static void init_cond_wait(struct cond_wait
*w
, unsigned int data
);
199 static void add_cond_wait(struct cond_wait
**head
, struct cond_wait
*w
);
200 static void remove_cond_wait(struct cond_wait
**head
, struct cond_wait
*w
);
201 static void flush_cond_wait(struct cond_wait
**head
);
204 * DMA buffer descriptor and access macros
210 __le32 pbuf
; /* physical address of data buffer */
211 __le32 next
; /* physical address of next descriptor */
213 /* driver book keeping */
214 char *buf
; /* virtual address of data buffer */
215 unsigned int pdesc
; /* physical address of this descriptor */
216 dma_addr_t buf_dma_addr
;
217 unsigned short buf_count
;
220 #define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
221 #define set_desc_next(a,b) (a).next = cpu_to_le32((unsigned int)(b))
222 #define set_desc_count(a,b)(a).count = cpu_to_le16((unsigned short)(b))
223 #define set_desc_eof(a,b) (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
224 #define desc_count(a) (le16_to_cpu((a).count))
225 #define desc_status(a) (le16_to_cpu((a).status))
226 #define desc_complete(a) (le16_to_cpu((a).status) & BIT15)
227 #define desc_eof(a) (le16_to_cpu((a).status) & BIT2)
228 #define desc_crc_error(a) (le16_to_cpu((a).status) & BIT1)
229 #define desc_abort(a) (le16_to_cpu((a).status) & BIT0)
230 #define desc_residue(a) ((le16_to_cpu((a).status) & 0x38) >> 3)
232 struct _input_signal_events
{
244 * device instance data structure
247 void *if_ptr
; /* General purpose pointer (used by SPPP) */
248 struct tty_port port
;
250 struct slgt_info
*next_device
; /* device list link */
254 char device_name
[25];
255 struct pci_dev
*pdev
;
257 int port_count
; /* count of ports on adapter */
258 int adapter_num
; /* adapter instance number */
259 int port_num
; /* port instance number */
261 /* array of pointers to port contexts on this adapter */
262 struct slgt_info
*port_array
[SLGT_MAX_PORTS
];
264 int line
; /* tty line instance number */
266 struct mgsl_icount icount
;
269 int x_char
; /* xon/xoff character */
270 unsigned int read_status_mask
;
271 unsigned int ignore_status_mask
;
273 wait_queue_head_t status_event_wait_q
;
274 wait_queue_head_t event_wait_q
;
275 struct timer_list tx_timer
;
276 struct timer_list rx_timer
;
278 unsigned int gpio_present
;
279 struct cond_wait
*gpio_wait_q
;
281 spinlock_t lock
; /* spinlock for synchronizing with ISR */
283 struct work_struct task
;
289 bool irq_requested
; /* true if IRQ requested */
290 bool irq_occurred
; /* for diagnostics use */
292 /* device configuration */
294 unsigned int bus_type
;
295 unsigned int irq_level
;
296 unsigned long irq_flags
;
298 unsigned char __iomem
* reg_addr
; /* memory mapped registers address */
300 bool reg_addr_requested
;
302 MGSL_PARAMS params
; /* communications parameters */
304 u32 max_frame_size
; /* as set by device config */
306 unsigned int rbuf_fill_level
;
307 unsigned int if_mode
;
317 unsigned char signals
; /* serial signal states */
318 int init_error
; /* initialization error */
320 unsigned char *tx_buf
;
323 char flag_buf
[MAX_ASYNC_BUFFER_SIZE
];
324 char char_buf
[MAX_ASYNC_BUFFER_SIZE
];
325 bool drop_rts_on_tx_done
;
326 struct _input_signal_events input_signal_events
;
328 int dcd_chkcount
; /* check counts to prevent */
329 int cts_chkcount
; /* too many IRQs if a signal */
330 int dsr_chkcount
; /* is floating */
333 char *bufs
; /* virtual address of DMA buffer lists */
334 dma_addr_t bufs_dma_addr
; /* physical address of buffer descriptors */
336 unsigned int rbuf_count
;
337 struct slgt_desc
*rbufs
;
338 unsigned int rbuf_current
;
339 unsigned int rbuf_index
;
341 unsigned int tbuf_count
;
342 struct slgt_desc
*tbufs
;
343 unsigned int tbuf_current
;
344 unsigned int tbuf_start
;
346 unsigned char *tmp_rbuf
;
347 unsigned int tmp_rbuf_count
;
349 /* SPPP/Cisco HDLC device parts */
354 #if SYNCLINK_GENERIC_HDLC
355 struct net_device
*netdev
;
360 static MGSL_PARAMS default_params
= {
361 .mode
= MGSL_MODE_HDLC
,
363 .flags
= HDLC_FLAG_UNDERRUN_ABORT15
,
364 .encoding
= HDLC_ENCODING_NRZI_SPACE
,
367 .crc_type
= HDLC_CRC_16_CCITT
,
368 .preamble_length
= HDLC_PREAMBLE_LENGTH_8BITS
,
369 .preamble
= HDLC_PREAMBLE_PATTERN_NONE
,
373 .parity
= ASYNC_PARITY_NONE
378 #define BH_TRANSMIT 2
380 #define IO_PIN_SHUTDOWN_LIMIT 100
382 #define DMABUFSIZE 256
383 #define DESC_LIST_SIZE 4096
385 #define MASK_PARITY BIT1
386 #define MASK_FRAMING BIT0
387 #define MASK_BREAK BIT14
388 #define MASK_OVERRUN BIT4
390 #define GSR 0x00 /* global status */
391 #define JCR 0x04 /* JTAG control */
392 #define IODR 0x08 /* GPIO direction */
393 #define IOER 0x0c /* GPIO interrupt enable */
394 #define IOVR 0x10 /* GPIO value */
395 #define IOSR 0x14 /* GPIO interrupt status */
396 #define TDR 0x80 /* tx data */
397 #define RDR 0x80 /* rx data */
398 #define TCR 0x82 /* tx control */
399 #define TIR 0x84 /* tx idle */
400 #define TPR 0x85 /* tx preamble */
401 #define RCR 0x86 /* rx control */
402 #define VCR 0x88 /* V.24 control */
403 #define CCR 0x89 /* clock control */
404 #define BDR 0x8a /* baud divisor */
405 #define SCR 0x8c /* serial control */
406 #define SSR 0x8e /* serial status */
407 #define RDCSR 0x90 /* rx DMA control/status */
408 #define TDCSR 0x94 /* tx DMA control/status */
409 #define RDDAR 0x98 /* rx DMA descriptor address */
410 #define TDDAR 0x9c /* tx DMA descriptor address */
413 #define RXBREAK BIT14
414 #define IRQ_TXDATA BIT13
415 #define IRQ_TXIDLE BIT12
416 #define IRQ_TXUNDER BIT11 /* HDLC */
417 #define IRQ_RXDATA BIT10
418 #define IRQ_RXIDLE BIT9 /* HDLC */
419 #define IRQ_RXBREAK BIT9 /* async */
420 #define IRQ_RXOVER BIT8
425 #define IRQ_ALL 0x3ff0
426 #define IRQ_MASTER BIT0
428 #define slgt_irq_on(info, mask) \
429 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
430 #define slgt_irq_off(info, mask) \
431 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
433 static __u8
rd_reg8(struct slgt_info
*info
, unsigned int addr
);
434 static void wr_reg8(struct slgt_info
*info
, unsigned int addr
, __u8 value
);
435 static __u16
rd_reg16(struct slgt_info
*info
, unsigned int addr
);
436 static void wr_reg16(struct slgt_info
*info
, unsigned int addr
, __u16 value
);
437 static __u32
rd_reg32(struct slgt_info
*info
, unsigned int addr
);
438 static void wr_reg32(struct slgt_info
*info
, unsigned int addr
, __u32 value
);
440 static void msc_set_vcr(struct slgt_info
*info
);
442 static int startup(struct slgt_info
*info
);
443 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,struct slgt_info
*info
);
444 static void shutdown(struct slgt_info
*info
);
445 static void program_hw(struct slgt_info
*info
);
446 static void change_params(struct slgt_info
*info
);
448 static int register_test(struct slgt_info
*info
);
449 static int irq_test(struct slgt_info
*info
);
450 static int loopback_test(struct slgt_info
*info
);
451 static int adapter_test(struct slgt_info
*info
);
453 static void reset_adapter(struct slgt_info
*info
);
454 static void reset_port(struct slgt_info
*info
);
455 static void async_mode(struct slgt_info
*info
);
456 static void sync_mode(struct slgt_info
*info
);
458 static void rx_stop(struct slgt_info
*info
);
459 static void rx_start(struct slgt_info
*info
);
460 static void reset_rbufs(struct slgt_info
*info
);
461 static void free_rbufs(struct slgt_info
*info
, unsigned int first
, unsigned int last
);
462 static void rdma_reset(struct slgt_info
*info
);
463 static bool rx_get_frame(struct slgt_info
*info
);
464 static bool rx_get_buf(struct slgt_info
*info
);
466 static void tx_start(struct slgt_info
*info
);
467 static void tx_stop(struct slgt_info
*info
);
468 static void tx_set_idle(struct slgt_info
*info
);
469 static unsigned int free_tbuf_count(struct slgt_info
*info
);
470 static unsigned int tbuf_bytes(struct slgt_info
*info
);
471 static void reset_tbufs(struct slgt_info
*info
);
472 static void tdma_reset(struct slgt_info
*info
);
473 static void tdma_start(struct slgt_info
*info
);
474 static void tx_load(struct slgt_info
*info
, const char *buf
, unsigned int count
);
476 static void get_signals(struct slgt_info
*info
);
477 static void set_signals(struct slgt_info
*info
);
478 static void enable_loopback(struct slgt_info
*info
);
479 static void set_rate(struct slgt_info
*info
, u32 data_rate
);
481 static int bh_action(struct slgt_info
*info
);
482 static void bh_handler(struct work_struct
*work
);
483 static void bh_transmit(struct slgt_info
*info
);
484 static void isr_serial(struct slgt_info
*info
);
485 static void isr_rdma(struct slgt_info
*info
);
486 static void isr_txeom(struct slgt_info
*info
, unsigned short status
);
487 static void isr_tdma(struct slgt_info
*info
);
489 static int alloc_dma_bufs(struct slgt_info
*info
);
490 static void free_dma_bufs(struct slgt_info
*info
);
491 static int alloc_desc(struct slgt_info
*info
);
492 static void free_desc(struct slgt_info
*info
);
493 static int alloc_bufs(struct slgt_info
*info
, struct slgt_desc
*bufs
, int count
);
494 static void free_bufs(struct slgt_info
*info
, struct slgt_desc
*bufs
, int count
);
496 static int alloc_tmp_rbuf(struct slgt_info
*info
);
497 static void free_tmp_rbuf(struct slgt_info
*info
);
499 static void tx_timeout(unsigned long context
);
500 static void rx_timeout(unsigned long context
);
505 static int get_stats(struct slgt_info
*info
, struct mgsl_icount __user
*user_icount
);
506 static int get_params(struct slgt_info
*info
, MGSL_PARAMS __user
*params
);
507 static int set_params(struct slgt_info
*info
, MGSL_PARAMS __user
*params
);
508 static int get_txidle(struct slgt_info
*info
, int __user
*idle_mode
);
509 static int set_txidle(struct slgt_info
*info
, int idle_mode
);
510 static int tx_enable(struct slgt_info
*info
, int enable
);
511 static int tx_abort(struct slgt_info
*info
);
512 static int rx_enable(struct slgt_info
*info
, int enable
);
513 static int modem_input_wait(struct slgt_info
*info
,int arg
);
514 static int wait_mgsl_event(struct slgt_info
*info
, int __user
*mask_ptr
);
515 static int tiocmget(struct tty_struct
*tty
, struct file
*file
);
516 static int tiocmset(struct tty_struct
*tty
, struct file
*file
,
517 unsigned int set
, unsigned int clear
);
518 static int set_break(struct tty_struct
*tty
, int break_state
);
519 static int get_interface(struct slgt_info
*info
, int __user
*if_mode
);
520 static int set_interface(struct slgt_info
*info
, int if_mode
);
521 static int set_gpio(struct slgt_info
*info
, struct gpio_desc __user
*gpio
);
522 static int get_gpio(struct slgt_info
*info
, struct gpio_desc __user
*gpio
);
523 static int wait_gpio(struct slgt_info
*info
, struct gpio_desc __user
*gpio
);
528 static void add_device(struct slgt_info
*info
);
529 static void device_init(int adapter_num
, struct pci_dev
*pdev
);
530 static int claim_resources(struct slgt_info
*info
);
531 static void release_resources(struct slgt_info
*info
);
550 static void trace_block(struct slgt_info
*info
, const char *data
, int count
, const char *label
)
554 printk("%s %s data:\n",info
->device_name
, label
);
556 linecount
= (count
> 16) ? 16 : count
;
557 for(i
=0; i
< linecount
; i
++)
558 printk("%02X ",(unsigned char)data
[i
]);
561 for(i
=0;i
<linecount
;i
++) {
562 if (data
[i
]>=040 && data
[i
]<=0176)
563 printk("%c",data
[i
]);
573 #define DBGDATA(info, buf, size, label)
577 static void dump_tbufs(struct slgt_info
*info
)
580 printk("tbuf_current=%d\n", info
->tbuf_current
);
581 for (i
=0 ; i
< info
->tbuf_count
; i
++) {
582 printk("%d: count=%04X status=%04X\n",
583 i
, le16_to_cpu(info
->tbufs
[i
].count
), le16_to_cpu(info
->tbufs
[i
].status
));
587 #define DBGTBUF(info)
591 static void dump_rbufs(struct slgt_info
*info
)
594 printk("rbuf_current=%d\n", info
->rbuf_current
);
595 for (i
=0 ; i
< info
->rbuf_count
; i
++) {
596 printk("%d: count=%04X status=%04X\n",
597 i
, le16_to_cpu(info
->rbufs
[i
].count
), le16_to_cpu(info
->rbufs
[i
].status
));
601 #define DBGRBUF(info)
604 static inline int sanity_check(struct slgt_info
*info
, char *devname
, const char *name
)
608 printk("null struct slgt_info for (%s) in %s\n", devname
, name
);
611 if (info
->magic
!= MGSL_MAGIC
) {
612 printk("bad magic number struct slgt_info (%s) in %s\n", devname
, name
);
623 * line discipline callback wrappers
625 * The wrappers maintain line discipline references
626 * while calling into the line discipline.
628 * ldisc_receive_buf - pass receive data to line discipline
630 static void ldisc_receive_buf(struct tty_struct
*tty
,
631 const __u8
*data
, char *flags
, int count
)
633 struct tty_ldisc
*ld
;
636 ld
= tty_ldisc_ref(tty
);
638 if (ld
->ops
->receive_buf
)
639 ld
->ops
->receive_buf(tty
, data
, flags
, count
);
646 static int open(struct tty_struct
*tty
, struct file
*filp
)
648 struct slgt_info
*info
;
653 if ((line
< 0) || (line
>= slgt_device_count
)) {
654 DBGERR(("%s: open with invalid line #%d.\n", driver_name
, line
));
658 info
= slgt_device_list
;
659 while(info
&& info
->line
!= line
)
660 info
= info
->next_device
;
661 if (sanity_check(info
, tty
->name
, "open"))
663 if (info
->init_error
) {
664 DBGERR(("%s init error=%d\n", info
->device_name
, info
->init_error
));
668 tty
->driver_data
= info
;
669 info
->port
.tty
= tty
;
671 DBGINFO(("%s open, old ref count = %d\n", info
->device_name
, info
->port
.count
));
673 /* If port is closing, signal caller to try again */
674 if (tty_hung_up_p(filp
) || info
->port
.flags
& ASYNC_CLOSING
){
675 if (info
->port
.flags
& ASYNC_CLOSING
)
676 interruptible_sleep_on(&info
->port
.close_wait
);
677 retval
= ((info
->port
.flags
& ASYNC_HUP_NOTIFY
) ?
678 -EAGAIN
: -ERESTARTSYS
);
682 info
->port
.tty
->low_latency
= (info
->port
.flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
684 spin_lock_irqsave(&info
->netlock
, flags
);
685 if (info
->netcount
) {
687 spin_unlock_irqrestore(&info
->netlock
, flags
);
691 spin_unlock_irqrestore(&info
->netlock
, flags
);
693 if (info
->port
.count
== 1) {
694 /* 1st open on this device, init hardware */
695 retval
= startup(info
);
700 retval
= block_til_ready(tty
, filp
, info
);
702 DBGINFO(("%s block_til_ready rc=%d\n", info
->device_name
, retval
));
711 info
->port
.tty
= NULL
; /* tty layer will release tty struct */
716 DBGINFO(("%s open rc=%d\n", info
->device_name
, retval
));
720 static void close(struct tty_struct
*tty
, struct file
*filp
)
722 struct slgt_info
*info
= tty
->driver_data
;
724 if (sanity_check(info
, tty
->name
, "close"))
726 DBGINFO(("%s close entry, count=%d\n", info
->device_name
, info
->port
.count
));
728 if (!info
->port
.count
)
731 if (tty_hung_up_p(filp
))
734 if ((tty
->count
== 1) && (info
->port
.count
!= 1)) {
736 * tty->count is 1 and the tty structure will be freed.
737 * info->port.count should be one in this case.
738 * if it's not, correct it so that the port is shutdown.
740 DBGERR(("%s close: bad refcount; tty->count=1, "
741 "info->port.count=%d\n", info
->device_name
, info
->port
.count
));
742 info
->port
.count
= 1;
747 /* if at least one open remaining, leave hardware active */
748 if (info
->port
.count
)
751 info
->port
.flags
|= ASYNC_CLOSING
;
753 /* set tty->closing to notify line discipline to
754 * only process XON/XOFF characters. Only the N_TTY
755 * discipline appears to use this (ppp does not).
759 /* wait for transmit data to clear all layers */
761 if (info
->port
.closing_wait
!= ASYNC_CLOSING_WAIT_NONE
) {
762 DBGINFO(("%s call tty_wait_until_sent\n", info
->device_name
));
763 tty_wait_until_sent(tty
, info
->port
.closing_wait
);
766 if (info
->port
.flags
& ASYNC_INITIALIZED
)
767 wait_until_sent(tty
, info
->timeout
);
769 tty_ldisc_flush(tty
);
774 info
->port
.tty
= NULL
;
776 if (info
->port
.blocked_open
) {
777 if (info
->port
.close_delay
) {
778 msleep_interruptible(jiffies_to_msecs(info
->port
.close_delay
));
780 wake_up_interruptible(&info
->port
.open_wait
);
783 info
->port
.flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CLOSING
);
785 wake_up_interruptible(&info
->port
.close_wait
);
788 DBGINFO(("%s close exit, count=%d\n", tty
->driver
->name
, info
->port
.count
));
791 static void hangup(struct tty_struct
*tty
)
793 struct slgt_info
*info
= tty
->driver_data
;
795 if (sanity_check(info
, tty
->name
, "hangup"))
797 DBGINFO(("%s hangup\n", info
->device_name
));
802 info
->port
.count
= 0;
803 info
->port
.flags
&= ~ASYNC_NORMAL_ACTIVE
;
804 info
->port
.tty
= NULL
;
806 wake_up_interruptible(&info
->port
.open_wait
);
809 static void set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
811 struct slgt_info
*info
= tty
->driver_data
;
814 DBGINFO(("%s set_termios\n", tty
->driver
->name
));
818 /* Handle transition to B0 status */
819 if (old_termios
->c_cflag
& CBAUD
&&
820 !(tty
->termios
->c_cflag
& CBAUD
)) {
821 info
->signals
&= ~(SerialSignal_RTS
+ SerialSignal_DTR
);
822 spin_lock_irqsave(&info
->lock
,flags
);
824 spin_unlock_irqrestore(&info
->lock
,flags
);
827 /* Handle transition away from B0 status */
828 if (!(old_termios
->c_cflag
& CBAUD
) &&
829 tty
->termios
->c_cflag
& CBAUD
) {
830 info
->signals
|= SerialSignal_DTR
;
831 if (!(tty
->termios
->c_cflag
& CRTSCTS
) ||
832 !test_bit(TTY_THROTTLED
, &tty
->flags
)) {
833 info
->signals
|= SerialSignal_RTS
;
835 spin_lock_irqsave(&info
->lock
,flags
);
837 spin_unlock_irqrestore(&info
->lock
,flags
);
840 /* Handle turning off CRTSCTS */
841 if (old_termios
->c_cflag
& CRTSCTS
&&
842 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
848 static int write(struct tty_struct
*tty
,
849 const unsigned char *buf
, int count
)
852 struct slgt_info
*info
= tty
->driver_data
;
854 unsigned int bufs_needed
;
856 if (sanity_check(info
, tty
->name
, "write"))
858 DBGINFO(("%s write count=%d\n", info
->device_name
, count
));
863 if (count
> info
->max_frame_size
) {
871 if (!info
->tx_active
&& info
->tx_count
) {
872 /* send accumulated data from send_char() */
873 tx_load(info
, info
->tx_buf
, info
->tx_count
);
876 bufs_needed
= (count
/DMABUFSIZE
);
877 if (count
% DMABUFSIZE
)
879 if (bufs_needed
> free_tbuf_count(info
))
882 ret
= info
->tx_count
= count
;
883 tx_load(info
, buf
, count
);
887 if (info
->tx_count
&& !tty
->stopped
&& !tty
->hw_stopped
) {
888 spin_lock_irqsave(&info
->lock
,flags
);
889 if (!info
->tx_active
)
893 spin_unlock_irqrestore(&info
->lock
,flags
);
897 DBGINFO(("%s write rc=%d\n", info
->device_name
, ret
));
901 static int put_char(struct tty_struct
*tty
, unsigned char ch
)
903 struct slgt_info
*info
= tty
->driver_data
;
907 if (sanity_check(info
, tty
->name
, "put_char"))
909 DBGINFO(("%s put_char(%d)\n", info
->device_name
, ch
));
912 spin_lock_irqsave(&info
->lock
,flags
);
913 if (!info
->tx_active
&& (info
->tx_count
< info
->max_frame_size
)) {
914 info
->tx_buf
[info
->tx_count
++] = ch
;
917 spin_unlock_irqrestore(&info
->lock
,flags
);
921 static void send_xchar(struct tty_struct
*tty
, char ch
)
923 struct slgt_info
*info
= tty
->driver_data
;
926 if (sanity_check(info
, tty
->name
, "send_xchar"))
928 DBGINFO(("%s send_xchar(%d)\n", info
->device_name
, ch
));
931 spin_lock_irqsave(&info
->lock
,flags
);
932 if (!info
->tx_enabled
)
934 spin_unlock_irqrestore(&info
->lock
,flags
);
938 static void wait_until_sent(struct tty_struct
*tty
, int timeout
)
940 struct slgt_info
*info
= tty
->driver_data
;
941 unsigned long orig_jiffies
, char_time
;
945 if (sanity_check(info
, tty
->name
, "wait_until_sent"))
947 DBGINFO(("%s wait_until_sent entry\n", info
->device_name
));
948 if (!(info
->port
.flags
& ASYNC_INITIALIZED
))
951 orig_jiffies
= jiffies
;
953 /* Set check interval to 1/5 of estimated time to
954 * send a character, and make it at least 1. The check
955 * interval should also be less than the timeout.
956 * Note: use tight timings here to satisfy the NIST-PCTS.
961 if (info
->params
.data_rate
) {
962 char_time
= info
->timeout
/(32 * 5);
969 char_time
= min_t(unsigned long, char_time
, timeout
);
971 while (info
->tx_active
) {
972 msleep_interruptible(jiffies_to_msecs(char_time
));
973 if (signal_pending(current
))
975 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
981 DBGINFO(("%s wait_until_sent exit\n", info
->device_name
));
984 static int write_room(struct tty_struct
*tty
)
986 struct slgt_info
*info
= tty
->driver_data
;
989 if (sanity_check(info
, tty
->name
, "write_room"))
991 ret
= (info
->tx_active
) ? 0 : HDLC_MAX_FRAME_SIZE
;
992 DBGINFO(("%s write_room=%d\n", info
->device_name
, ret
));
996 static void flush_chars(struct tty_struct
*tty
)
998 struct slgt_info
*info
= tty
->driver_data
;
1001 if (sanity_check(info
, tty
->name
, "flush_chars"))
1003 DBGINFO(("%s flush_chars entry tx_count=%d\n", info
->device_name
, info
->tx_count
));
1005 if (info
->tx_count
<= 0 || tty
->stopped
||
1006 tty
->hw_stopped
|| !info
->tx_buf
)
1009 DBGINFO(("%s flush_chars start transmit\n", info
->device_name
));
1011 spin_lock_irqsave(&info
->lock
,flags
);
1012 if (!info
->tx_active
&& info
->tx_count
) {
1013 tx_load(info
, info
->tx_buf
,info
->tx_count
);
1016 spin_unlock_irqrestore(&info
->lock
,flags
);
1019 static void flush_buffer(struct tty_struct
*tty
)
1021 struct slgt_info
*info
= tty
->driver_data
;
1022 unsigned long flags
;
1024 if (sanity_check(info
, tty
->name
, "flush_buffer"))
1026 DBGINFO(("%s flush_buffer\n", info
->device_name
));
1028 spin_lock_irqsave(&info
->lock
,flags
);
1029 if (!info
->tx_active
)
1031 spin_unlock_irqrestore(&info
->lock
,flags
);
1037 * throttle (stop) transmitter
1039 static void tx_hold(struct tty_struct
*tty
)
1041 struct slgt_info
*info
= tty
->driver_data
;
1042 unsigned long flags
;
1044 if (sanity_check(info
, tty
->name
, "tx_hold"))
1046 DBGINFO(("%s tx_hold\n", info
->device_name
));
1047 spin_lock_irqsave(&info
->lock
,flags
);
1048 if (info
->tx_enabled
&& info
->params
.mode
== MGSL_MODE_ASYNC
)
1050 spin_unlock_irqrestore(&info
->lock
,flags
);
1054 * release (start) transmitter
1056 static void tx_release(struct tty_struct
*tty
)
1058 struct slgt_info
*info
= tty
->driver_data
;
1059 unsigned long flags
;
1061 if (sanity_check(info
, tty
->name
, "tx_release"))
1063 DBGINFO(("%s tx_release\n", info
->device_name
));
1064 spin_lock_irqsave(&info
->lock
,flags
);
1065 if (!info
->tx_active
&& info
->tx_count
) {
1066 tx_load(info
, info
->tx_buf
, info
->tx_count
);
1069 spin_unlock_irqrestore(&info
->lock
,flags
);
1073 * Service an IOCTL request
1077 * tty pointer to tty instance data
1078 * file pointer to associated file object for device
1079 * cmd IOCTL command code
1080 * arg command argument/context
1082 * Return 0 if success, otherwise error code
1084 static int ioctl(struct tty_struct
*tty
, struct file
*file
,
1085 unsigned int cmd
, unsigned long arg
)
1087 struct slgt_info
*info
= tty
->driver_data
;
1088 struct mgsl_icount cnow
; /* kernel counter temps */
1089 struct serial_icounter_struct __user
*p_cuser
; /* user space */
1090 unsigned long flags
;
1091 void __user
*argp
= (void __user
*)arg
;
1094 if (sanity_check(info
, tty
->name
, "ioctl"))
1096 DBGINFO(("%s ioctl() cmd=%08X\n", info
->device_name
, cmd
));
1098 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1099 (cmd
!= TIOCMIWAIT
) && (cmd
!= TIOCGICOUNT
)) {
1100 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1107 case MGSL_IOCGPARAMS
:
1108 ret
= get_params(info
, argp
);
1110 case MGSL_IOCSPARAMS
:
1111 ret
= set_params(info
, argp
);
1113 case MGSL_IOCGTXIDLE
:
1114 ret
= get_txidle(info
, argp
);
1116 case MGSL_IOCSTXIDLE
:
1117 ret
= set_txidle(info
, (int)arg
);
1119 case MGSL_IOCTXENABLE
:
1120 ret
= tx_enable(info
, (int)arg
);
1122 case MGSL_IOCRXENABLE
:
1123 ret
= rx_enable(info
, (int)arg
);
1125 case MGSL_IOCTXABORT
:
1126 ret
= tx_abort(info
);
1128 case MGSL_IOCGSTATS
:
1129 ret
= get_stats(info
, argp
);
1131 case MGSL_IOCWAITEVENT
:
1132 ret
= wait_mgsl_event(info
, argp
);
1135 ret
= modem_input_wait(info
,(int)arg
);
1138 ret
= get_interface(info
, argp
);
1141 ret
= set_interface(info
,(int)arg
);
1144 ret
= set_gpio(info
, argp
);
1147 ret
= get_gpio(info
, argp
);
1149 case MGSL_IOCWAITGPIO
:
1150 ret
= wait_gpio(info
, argp
);
1153 spin_lock_irqsave(&info
->lock
,flags
);
1154 cnow
= info
->icount
;
1155 spin_unlock_irqrestore(&info
->lock
,flags
);
1157 if (put_user(cnow
.cts
, &p_cuser
->cts
) ||
1158 put_user(cnow
.dsr
, &p_cuser
->dsr
) ||
1159 put_user(cnow
.rng
, &p_cuser
->rng
) ||
1160 put_user(cnow
.dcd
, &p_cuser
->dcd
) ||
1161 put_user(cnow
.rx
, &p_cuser
->rx
) ||
1162 put_user(cnow
.tx
, &p_cuser
->tx
) ||
1163 put_user(cnow
.frame
, &p_cuser
->frame
) ||
1164 put_user(cnow
.overrun
, &p_cuser
->overrun
) ||
1165 put_user(cnow
.parity
, &p_cuser
->parity
) ||
1166 put_user(cnow
.brk
, &p_cuser
->brk
) ||
1167 put_user(cnow
.buf_overrun
, &p_cuser
->buf_overrun
))
1179 * support for 32 bit ioctl calls on 64 bit systems
1181 #ifdef CONFIG_COMPAT
1182 static long get_params32(struct slgt_info
*info
, struct MGSL_PARAMS32 __user
*user_params
)
1184 struct MGSL_PARAMS32 tmp_params
;
1186 DBGINFO(("%s get_params32\n", info
->device_name
));
1187 tmp_params
.mode
= (compat_ulong_t
)info
->params
.mode
;
1188 tmp_params
.loopback
= info
->params
.loopback
;
1189 tmp_params
.flags
= info
->params
.flags
;
1190 tmp_params
.encoding
= info
->params
.encoding
;
1191 tmp_params
.clock_speed
= (compat_ulong_t
)info
->params
.clock_speed
;
1192 tmp_params
.addr_filter
= info
->params
.addr_filter
;
1193 tmp_params
.crc_type
= info
->params
.crc_type
;
1194 tmp_params
.preamble_length
= info
->params
.preamble_length
;
1195 tmp_params
.preamble
= info
->params
.preamble
;
1196 tmp_params
.data_rate
= (compat_ulong_t
)info
->params
.data_rate
;
1197 tmp_params
.data_bits
= info
->params
.data_bits
;
1198 tmp_params
.stop_bits
= info
->params
.stop_bits
;
1199 tmp_params
.parity
= info
->params
.parity
;
1200 if (copy_to_user(user_params
, &tmp_params
, sizeof(struct MGSL_PARAMS32
)))
1205 static long set_params32(struct slgt_info
*info
, struct MGSL_PARAMS32 __user
*new_params
)
1207 struct MGSL_PARAMS32 tmp_params
;
1209 DBGINFO(("%s set_params32\n", info
->device_name
));
1210 if (copy_from_user(&tmp_params
, new_params
, sizeof(struct MGSL_PARAMS32
)))
1213 spin_lock(&info
->lock
);
1214 info
->params
.mode
= tmp_params
.mode
;
1215 info
->params
.loopback
= tmp_params
.loopback
;
1216 info
->params
.flags
= tmp_params
.flags
;
1217 info
->params
.encoding
= tmp_params
.encoding
;
1218 info
->params
.clock_speed
= tmp_params
.clock_speed
;
1219 info
->params
.addr_filter
= tmp_params
.addr_filter
;
1220 info
->params
.crc_type
= tmp_params
.crc_type
;
1221 info
->params
.preamble_length
= tmp_params
.preamble_length
;
1222 info
->params
.preamble
= tmp_params
.preamble
;
1223 info
->params
.data_rate
= tmp_params
.data_rate
;
1224 info
->params
.data_bits
= tmp_params
.data_bits
;
1225 info
->params
.stop_bits
= tmp_params
.stop_bits
;
1226 info
->params
.parity
= tmp_params
.parity
;
1227 spin_unlock(&info
->lock
);
1229 change_params(info
);
1234 static long slgt_compat_ioctl(struct tty_struct
*tty
, struct file
*file
,
1235 unsigned int cmd
, unsigned long arg
)
1237 struct slgt_info
*info
= tty
->driver_data
;
1238 int rc
= -ENOIOCTLCMD
;
1240 if (sanity_check(info
, tty
->name
, "compat_ioctl"))
1242 DBGINFO(("%s compat_ioctl() cmd=%08X\n", info
->device_name
, cmd
));
1246 case MGSL_IOCSPARAMS32
:
1247 rc
= set_params32(info
, compat_ptr(arg
));
1250 case MGSL_IOCGPARAMS32
:
1251 rc
= get_params32(info
, compat_ptr(arg
));
1254 case MGSL_IOCGPARAMS
:
1255 case MGSL_IOCSPARAMS
:
1256 case MGSL_IOCGTXIDLE
:
1257 case MGSL_IOCGSTATS
:
1258 case MGSL_IOCWAITEVENT
:
1262 case MGSL_IOCWAITGPIO
:
1264 rc
= ioctl(tty
, file
, cmd
, (unsigned long)(compat_ptr(arg
)));
1267 case MGSL_IOCSTXIDLE
:
1268 case MGSL_IOCTXENABLE
:
1269 case MGSL_IOCRXENABLE
:
1270 case MGSL_IOCTXABORT
:
1273 rc
= ioctl(tty
, file
, cmd
, arg
);
1277 DBGINFO(("%s compat_ioctl() cmd=%08X rc=%d\n", info
->device_name
, cmd
, rc
));
1281 #define slgt_compat_ioctl NULL
1282 #endif /* ifdef CONFIG_COMPAT */
1287 static inline int line_info(char *buf
, struct slgt_info
*info
)
1291 unsigned long flags
;
1293 ret
= sprintf(buf
, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
1294 info
->device_name
, info
->phys_reg_addr
,
1295 info
->irq_level
, info
->max_frame_size
);
1297 /* output current serial signal states */
1298 spin_lock_irqsave(&info
->lock
,flags
);
1300 spin_unlock_irqrestore(&info
->lock
,flags
);
1304 if (info
->signals
& SerialSignal_RTS
)
1305 strcat(stat_buf
, "|RTS");
1306 if (info
->signals
& SerialSignal_CTS
)
1307 strcat(stat_buf
, "|CTS");
1308 if (info
->signals
& SerialSignal_DTR
)
1309 strcat(stat_buf
, "|DTR");
1310 if (info
->signals
& SerialSignal_DSR
)
1311 strcat(stat_buf
, "|DSR");
1312 if (info
->signals
& SerialSignal_DCD
)
1313 strcat(stat_buf
, "|CD");
1314 if (info
->signals
& SerialSignal_RI
)
1315 strcat(stat_buf
, "|RI");
1317 if (info
->params
.mode
!= MGSL_MODE_ASYNC
) {
1318 ret
+= sprintf(buf
+ret
, "\tHDLC txok:%d rxok:%d",
1319 info
->icount
.txok
, info
->icount
.rxok
);
1320 if (info
->icount
.txunder
)
1321 ret
+= sprintf(buf
+ret
, " txunder:%d", info
->icount
.txunder
);
1322 if (info
->icount
.txabort
)
1323 ret
+= sprintf(buf
+ret
, " txabort:%d", info
->icount
.txabort
);
1324 if (info
->icount
.rxshort
)
1325 ret
+= sprintf(buf
+ret
, " rxshort:%d", info
->icount
.rxshort
);
1326 if (info
->icount
.rxlong
)
1327 ret
+= sprintf(buf
+ret
, " rxlong:%d", info
->icount
.rxlong
);
1328 if (info
->icount
.rxover
)
1329 ret
+= sprintf(buf
+ret
, " rxover:%d", info
->icount
.rxover
);
1330 if (info
->icount
.rxcrc
)
1331 ret
+= sprintf(buf
+ret
, " rxcrc:%d", info
->icount
.rxcrc
);
1333 ret
+= sprintf(buf
+ret
, "\tASYNC tx:%d rx:%d",
1334 info
->icount
.tx
, info
->icount
.rx
);
1335 if (info
->icount
.frame
)
1336 ret
+= sprintf(buf
+ret
, " fe:%d", info
->icount
.frame
);
1337 if (info
->icount
.parity
)
1338 ret
+= sprintf(buf
+ret
, " pe:%d", info
->icount
.parity
);
1339 if (info
->icount
.brk
)
1340 ret
+= sprintf(buf
+ret
, " brk:%d", info
->icount
.brk
);
1341 if (info
->icount
.overrun
)
1342 ret
+= sprintf(buf
+ret
, " oe:%d", info
->icount
.overrun
);
1345 /* Append serial signal status to end */
1346 ret
+= sprintf(buf
+ret
, " %s\n", stat_buf
+1);
1348 ret
+= sprintf(buf
+ret
, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1349 info
->tx_active
,info
->bh_requested
,info
->bh_running
,
1355 /* Called to print information about devices
1357 static int read_proc(char *page
, char **start
, off_t off
, int count
,
1358 int *eof
, void *data
)
1362 struct slgt_info
*info
;
1364 len
+= sprintf(page
, "synclink_gt driver:%s\n", driver_version
);
1366 info
= slgt_device_list
;
1368 l
= line_info(page
+ len
, info
);
1370 if (len
+begin
> off
+count
)
1372 if (len
+begin
< off
) {
1376 info
= info
->next_device
;
1381 if (off
>= len
+begin
)
1383 *start
= page
+ (off
-begin
);
1384 return ((count
< begin
+len
-off
) ? count
: begin
+len
-off
);
1388 * return count of bytes in transmit buffer
1390 static int chars_in_buffer(struct tty_struct
*tty
)
1392 struct slgt_info
*info
= tty
->driver_data
;
1394 if (sanity_check(info
, tty
->name
, "chars_in_buffer"))
1396 count
= tbuf_bytes(info
);
1397 DBGINFO(("%s chars_in_buffer()=%d\n", info
->device_name
, 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 int 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
);
1472 #if SYNCLINK_GENERIC_HDLC
1475 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1476 * set encoding and frame check sequence (FCS) options
1478 * dev pointer to network device structure
1479 * encoding serial encoding setting
1480 * parity FCS setting
1482 * returns 0 if success, otherwise error code
1484 static int hdlcdev_attach(struct net_device
*dev
, unsigned short encoding
,
1485 unsigned short parity
)
1487 struct slgt_info
*info
= dev_to_port(dev
);
1488 unsigned char new_encoding
;
1489 unsigned short new_crctype
;
1491 /* return error if TTY interface open */
1492 if (info
->port
.count
)
1495 DBGINFO(("%s hdlcdev_attach\n", info
->device_name
));
1499 case ENCODING_NRZ
: new_encoding
= HDLC_ENCODING_NRZ
; break;
1500 case ENCODING_NRZI
: new_encoding
= HDLC_ENCODING_NRZI_SPACE
; break;
1501 case ENCODING_FM_MARK
: new_encoding
= HDLC_ENCODING_BIPHASE_MARK
; break;
1502 case ENCODING_FM_SPACE
: new_encoding
= HDLC_ENCODING_BIPHASE_SPACE
; break;
1503 case ENCODING_MANCHESTER
: new_encoding
= HDLC_ENCODING_BIPHASE_LEVEL
; break;
1504 default: return -EINVAL
;
1509 case PARITY_NONE
: new_crctype
= HDLC_CRC_NONE
; break;
1510 case PARITY_CRC16_PR1_CCITT
: new_crctype
= HDLC_CRC_16_CCITT
; break;
1511 case PARITY_CRC32_PR1_CCITT
: new_crctype
= HDLC_CRC_32_CCITT
; break;
1512 default: return -EINVAL
;
1515 info
->params
.encoding
= new_encoding
;
1516 info
->params
.crc_type
= new_crctype
;
1518 /* if network interface up, reprogram hardware */
1526 * called by generic HDLC layer to send frame
1528 * skb socket buffer containing HDLC frame
1529 * dev pointer to network device structure
1531 * returns 0 if success, otherwise error code
1533 static int hdlcdev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1535 struct slgt_info
*info
= dev_to_port(dev
);
1536 unsigned long flags
;
1538 DBGINFO(("%s hdlc_xmit\n", dev
->name
));
1540 /* stop sending until this frame completes */
1541 netif_stop_queue(dev
);
1543 /* copy data to device buffers */
1544 info
->tx_count
= skb
->len
;
1545 tx_load(info
, skb
->data
, skb
->len
);
1547 /* update network statistics */
1548 dev
->stats
.tx_packets
++;
1549 dev
->stats
.tx_bytes
+= skb
->len
;
1551 /* done with socket buffer, so free it */
1554 /* save start time for transmit timeout detection */
1555 dev
->trans_start
= jiffies
;
1557 /* start hardware transmitter if necessary */
1558 spin_lock_irqsave(&info
->lock
,flags
);
1559 if (!info
->tx_active
)
1561 spin_unlock_irqrestore(&info
->lock
,flags
);
1567 * called by network layer when interface enabled
1568 * claim resources and initialize hardware
1570 * dev pointer to network device structure
1572 * returns 0 if success, otherwise error code
1574 static int hdlcdev_open(struct net_device
*dev
)
1576 struct slgt_info
*info
= dev_to_port(dev
);
1578 unsigned long flags
;
1580 if (!try_module_get(THIS_MODULE
))
1583 DBGINFO(("%s hdlcdev_open\n", dev
->name
));
1585 /* generic HDLC layer open processing */
1586 if ((rc
= hdlc_open(dev
)))
1589 /* arbitrate between network and tty opens */
1590 spin_lock_irqsave(&info
->netlock
, flags
);
1591 if (info
->port
.count
!= 0 || info
->netcount
!= 0) {
1592 DBGINFO(("%s hdlc_open busy\n", dev
->name
));
1593 spin_unlock_irqrestore(&info
->netlock
, flags
);
1597 spin_unlock_irqrestore(&info
->netlock
, flags
);
1599 /* claim resources and init adapter */
1600 if ((rc
= startup(info
)) != 0) {
1601 spin_lock_irqsave(&info
->netlock
, flags
);
1603 spin_unlock_irqrestore(&info
->netlock
, flags
);
1607 /* assert DTR and RTS, apply hardware settings */
1608 info
->signals
|= SerialSignal_RTS
+ SerialSignal_DTR
;
1611 /* enable network layer transmit */
1612 dev
->trans_start
= jiffies
;
1613 netif_start_queue(dev
);
1615 /* inform generic HDLC layer of current DCD status */
1616 spin_lock_irqsave(&info
->lock
, flags
);
1618 spin_unlock_irqrestore(&info
->lock
, flags
);
1619 if (info
->signals
& SerialSignal_DCD
)
1620 netif_carrier_on(dev
);
1622 netif_carrier_off(dev
);
1627 * called by network layer when interface is disabled
1628 * shutdown hardware and release resources
1630 * dev pointer to network device structure
1632 * returns 0 if success, otherwise error code
1634 static int hdlcdev_close(struct net_device
*dev
)
1636 struct slgt_info
*info
= dev_to_port(dev
);
1637 unsigned long flags
;
1639 DBGINFO(("%s hdlcdev_close\n", dev
->name
));
1641 netif_stop_queue(dev
);
1643 /* shutdown adapter and release resources */
1648 spin_lock_irqsave(&info
->netlock
, flags
);
1650 spin_unlock_irqrestore(&info
->netlock
, flags
);
1652 module_put(THIS_MODULE
);
1657 * called by network layer to process IOCTL call to network device
1659 * dev pointer to network device structure
1660 * ifr pointer to network interface request structure
1661 * cmd IOCTL command code
1663 * returns 0 if success, otherwise error code
1665 static int hdlcdev_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1667 const size_t size
= sizeof(sync_serial_settings
);
1668 sync_serial_settings new_line
;
1669 sync_serial_settings __user
*line
= ifr
->ifr_settings
.ifs_ifsu
.sync
;
1670 struct slgt_info
*info
= dev_to_port(dev
);
1673 DBGINFO(("%s hdlcdev_ioctl\n", dev
->name
));
1675 /* return error if TTY interface open */
1676 if (info
->port
.count
)
1679 if (cmd
!= SIOCWANDEV
)
1680 return hdlc_ioctl(dev
, ifr
, cmd
);
1682 switch(ifr
->ifr_settings
.type
) {
1683 case IF_GET_IFACE
: /* return current sync_serial_settings */
1685 ifr
->ifr_settings
.type
= IF_IFACE_SYNC_SERIAL
;
1686 if (ifr
->ifr_settings
.size
< size
) {
1687 ifr
->ifr_settings
.size
= size
; /* data size wanted */
1691 flags
= info
->params
.flags
& (HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_RXC_DPLL
|
1692 HDLC_FLAG_RXC_BRG
| HDLC_FLAG_RXC_TXCPIN
|
1693 HDLC_FLAG_TXC_TXCPIN
| HDLC_FLAG_TXC_DPLL
|
1694 HDLC_FLAG_TXC_BRG
| HDLC_FLAG_TXC_RXCPIN
);
1697 case (HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_TXCPIN
): new_line
.clock_type
= CLOCK_EXT
; break;
1698 case (HDLC_FLAG_RXC_BRG
| HDLC_FLAG_TXC_BRG
): new_line
.clock_type
= CLOCK_INT
; break;
1699 case (HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_BRG
): new_line
.clock_type
= CLOCK_TXINT
; break;
1700 case (HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_RXCPIN
): new_line
.clock_type
= CLOCK_TXFROMRX
; break;
1701 default: new_line
.clock_type
= CLOCK_DEFAULT
;
1704 new_line
.clock_rate
= info
->params
.clock_speed
;
1705 new_line
.loopback
= info
->params
.loopback
? 1:0;
1707 if (copy_to_user(line
, &new_line
, size
))
1711 case IF_IFACE_SYNC_SERIAL
: /* set sync_serial_settings */
1713 if(!capable(CAP_NET_ADMIN
))
1715 if (copy_from_user(&new_line
, line
, size
))
1718 switch (new_line
.clock_type
)
1720 case CLOCK_EXT
: flags
= HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_TXCPIN
; break;
1721 case CLOCK_TXFROMRX
: flags
= HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_RXCPIN
; break;
1722 case CLOCK_INT
: flags
= HDLC_FLAG_RXC_BRG
| HDLC_FLAG_TXC_BRG
; break;
1723 case CLOCK_TXINT
: flags
= HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_BRG
; break;
1724 case CLOCK_DEFAULT
: flags
= info
->params
.flags
&
1725 (HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_RXC_DPLL
|
1726 HDLC_FLAG_RXC_BRG
| HDLC_FLAG_RXC_TXCPIN
|
1727 HDLC_FLAG_TXC_TXCPIN
| HDLC_FLAG_TXC_DPLL
|
1728 HDLC_FLAG_TXC_BRG
| HDLC_FLAG_TXC_RXCPIN
); break;
1729 default: return -EINVAL
;
1732 if (new_line
.loopback
!= 0 && new_line
.loopback
!= 1)
1735 info
->params
.flags
&= ~(HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_RXC_DPLL
|
1736 HDLC_FLAG_RXC_BRG
| HDLC_FLAG_RXC_TXCPIN
|
1737 HDLC_FLAG_TXC_TXCPIN
| HDLC_FLAG_TXC_DPLL
|
1738 HDLC_FLAG_TXC_BRG
| HDLC_FLAG_TXC_RXCPIN
);
1739 info
->params
.flags
|= flags
;
1741 info
->params
.loopback
= new_line
.loopback
;
1743 if (flags
& (HDLC_FLAG_RXC_BRG
| HDLC_FLAG_TXC_BRG
))
1744 info
->params
.clock_speed
= new_line
.clock_rate
;
1746 info
->params
.clock_speed
= 0;
1748 /* if network interface up, reprogram hardware */
1754 return hdlc_ioctl(dev
, ifr
, cmd
);
1759 * called by network layer when transmit timeout is detected
1761 * dev pointer to network device structure
1763 static void hdlcdev_tx_timeout(struct net_device
*dev
)
1765 struct slgt_info
*info
= dev_to_port(dev
);
1766 unsigned long flags
;
1768 DBGINFO(("%s hdlcdev_tx_timeout\n", dev
->name
));
1770 dev
->stats
.tx_errors
++;
1771 dev
->stats
.tx_aborted_errors
++;
1773 spin_lock_irqsave(&info
->lock
,flags
);
1775 spin_unlock_irqrestore(&info
->lock
,flags
);
1777 netif_wake_queue(dev
);
1781 * called by device driver when transmit completes
1782 * reenable network layer transmit if stopped
1784 * info pointer to device instance information
1786 static void hdlcdev_tx_done(struct slgt_info
*info
)
1788 if (netif_queue_stopped(info
->netdev
))
1789 netif_wake_queue(info
->netdev
);
1793 * called by device driver when frame received
1794 * pass frame to network layer
1796 * info pointer to device instance information
1797 * buf pointer to buffer contianing frame data
1798 * size count of data bytes in buf
1800 static void hdlcdev_rx(struct slgt_info
*info
, char *buf
, int size
)
1802 struct sk_buff
*skb
= dev_alloc_skb(size
);
1803 struct net_device
*dev
= info
->netdev
;
1805 DBGINFO(("%s hdlcdev_rx\n", dev
->name
));
1808 DBGERR(("%s: can't alloc skb, drop packet\n", dev
->name
));
1809 dev
->stats
.rx_dropped
++;
1813 memcpy(skb_put(skb
, size
), buf
, size
);
1815 skb
->protocol
= hdlc_type_trans(skb
, dev
);
1817 dev
->stats
.rx_packets
++;
1818 dev
->stats
.rx_bytes
+= size
;
1822 dev
->last_rx
= jiffies
;
1826 * called by device driver when adding device instance
1827 * do generic HDLC initialization
1829 * info pointer to device instance information
1831 * returns 0 if success, otherwise error code
1833 static int hdlcdev_init(struct slgt_info
*info
)
1836 struct net_device
*dev
;
1839 /* allocate and initialize network and HDLC layer objects */
1841 if (!(dev
= alloc_hdlcdev(info
))) {
1842 printk(KERN_ERR
"%s hdlc device alloc failure\n", info
->device_name
);
1846 /* for network layer reporting purposes only */
1847 dev
->mem_start
= info
->phys_reg_addr
;
1848 dev
->mem_end
= info
->phys_reg_addr
+ SLGT_REG_SIZE
- 1;
1849 dev
->irq
= info
->irq_level
;
1851 /* network layer callbacks and settings */
1852 dev
->do_ioctl
= hdlcdev_ioctl
;
1853 dev
->open
= hdlcdev_open
;
1854 dev
->stop
= hdlcdev_close
;
1855 dev
->tx_timeout
= hdlcdev_tx_timeout
;
1856 dev
->watchdog_timeo
= 10*HZ
;
1857 dev
->tx_queue_len
= 50;
1859 /* generic HDLC layer callbacks and settings */
1860 hdlc
= dev_to_hdlc(dev
);
1861 hdlc
->attach
= hdlcdev_attach
;
1862 hdlc
->xmit
= hdlcdev_xmit
;
1864 /* register objects with HDLC layer */
1865 if ((rc
= register_hdlc_device(dev
))) {
1866 printk(KERN_WARNING
"%s:unable to register hdlc device\n",__FILE__
);
1876 * called by device driver when removing device instance
1877 * do generic HDLC cleanup
1879 * info pointer to device instance information
1881 static void hdlcdev_exit(struct slgt_info
*info
)
1883 unregister_hdlc_device(info
->netdev
);
1884 free_netdev(info
->netdev
);
1885 info
->netdev
= NULL
;
1888 #endif /* ifdef CONFIG_HDLC */
1891 * get async data from rx DMA buffers
1893 static void rx_async(struct slgt_info
*info
)
1895 struct tty_struct
*tty
= info
->port
.tty
;
1896 struct mgsl_icount
*icount
= &info
->icount
;
1897 unsigned int start
, end
;
1899 unsigned char status
;
1900 struct slgt_desc
*bufs
= info
->rbufs
;
1906 start
= end
= info
->rbuf_current
;
1908 while(desc_complete(bufs
[end
])) {
1909 count
= desc_count(bufs
[end
]) - info
->rbuf_index
;
1910 p
= bufs
[end
].buf
+ info
->rbuf_index
;
1912 DBGISR(("%s rx_async count=%d\n", info
->device_name
, count
));
1913 DBGDATA(info
, p
, count
, "rx");
1915 for(i
=0 ; i
< count
; i
+=2, p
+=2) {
1921 if ((status
= *(p
+1) & (BIT1
+ BIT0
))) {
1924 else if (status
& BIT0
)
1926 /* discard char if tty control flags say so */
1927 if (status
& info
->ignore_status_mask
)
1931 else if (status
& BIT0
)
1935 tty_insert_flip_char(tty
, ch
, stat
);
1941 /* receive buffer not completed */
1942 info
->rbuf_index
+= i
;
1943 mod_timer(&info
->rx_timer
, jiffies
+ 1);
1947 info
->rbuf_index
= 0;
1948 free_rbufs(info
, end
, end
);
1950 if (++end
== info
->rbuf_count
)
1953 /* if entire list searched then no frame available */
1959 tty_flip_buffer_push(tty
);
1963 * return next bottom half action to perform
1965 static int bh_action(struct slgt_info
*info
)
1967 unsigned long flags
;
1970 spin_lock_irqsave(&info
->lock
,flags
);
1972 if (info
->pending_bh
& BH_RECEIVE
) {
1973 info
->pending_bh
&= ~BH_RECEIVE
;
1975 } else if (info
->pending_bh
& BH_TRANSMIT
) {
1976 info
->pending_bh
&= ~BH_TRANSMIT
;
1978 } else if (info
->pending_bh
& BH_STATUS
) {
1979 info
->pending_bh
&= ~BH_STATUS
;
1982 /* Mark BH routine as complete */
1983 info
->bh_running
= false;
1984 info
->bh_requested
= false;
1988 spin_unlock_irqrestore(&info
->lock
,flags
);
1994 * perform bottom half processing
1996 static void bh_handler(struct work_struct
*work
)
1998 struct slgt_info
*info
= container_of(work
, struct slgt_info
, task
);
2003 info
->bh_running
= true;
2005 while((action
= bh_action(info
))) {
2008 DBGBH(("%s bh receive\n", info
->device_name
));
2009 switch(info
->params
.mode
) {
2010 case MGSL_MODE_ASYNC
:
2013 case MGSL_MODE_HDLC
:
2014 while(rx_get_frame(info
));
2017 case MGSL_MODE_MONOSYNC
:
2018 case MGSL_MODE_BISYNC
:
2019 while(rx_get_buf(info
));
2022 /* restart receiver if rx DMA buffers exhausted */
2023 if (info
->rx_restart
)
2030 DBGBH(("%s bh status\n", info
->device_name
));
2031 info
->ri_chkcount
= 0;
2032 info
->dsr_chkcount
= 0;
2033 info
->dcd_chkcount
= 0;
2034 info
->cts_chkcount
= 0;
2037 DBGBH(("%s unknown action\n", info
->device_name
));
2041 DBGBH(("%s bh_handler exit\n", info
->device_name
));
2044 static void bh_transmit(struct slgt_info
*info
)
2046 struct tty_struct
*tty
= info
->port
.tty
;
2048 DBGBH(("%s bh_transmit\n", info
->device_name
));
2053 static void dsr_change(struct slgt_info
*info
, unsigned short status
)
2055 if (status
& BIT3
) {
2056 info
->signals
|= SerialSignal_DSR
;
2057 info
->input_signal_events
.dsr_up
++;
2059 info
->signals
&= ~SerialSignal_DSR
;
2060 info
->input_signal_events
.dsr_down
++;
2062 DBGISR(("dsr_change %s signals=%04X\n", info
->device_name
, info
->signals
));
2063 if ((info
->dsr_chkcount
)++ == IO_PIN_SHUTDOWN_LIMIT
) {
2064 slgt_irq_off(info
, IRQ_DSR
);
2068 wake_up_interruptible(&info
->status_event_wait_q
);
2069 wake_up_interruptible(&info
->event_wait_q
);
2070 info
->pending_bh
|= BH_STATUS
;
2073 static void cts_change(struct slgt_info
*info
, unsigned short status
)
2075 if (status
& BIT2
) {
2076 info
->signals
|= SerialSignal_CTS
;
2077 info
->input_signal_events
.cts_up
++;
2079 info
->signals
&= ~SerialSignal_CTS
;
2080 info
->input_signal_events
.cts_down
++;
2082 DBGISR(("cts_change %s signals=%04X\n", info
->device_name
, info
->signals
));
2083 if ((info
->cts_chkcount
)++ == IO_PIN_SHUTDOWN_LIMIT
) {
2084 slgt_irq_off(info
, IRQ_CTS
);
2088 wake_up_interruptible(&info
->status_event_wait_q
);
2089 wake_up_interruptible(&info
->event_wait_q
);
2090 info
->pending_bh
|= BH_STATUS
;
2092 if (info
->port
.flags
& ASYNC_CTS_FLOW
) {
2093 if (info
->port
.tty
) {
2094 if (info
->port
.tty
->hw_stopped
) {
2095 if (info
->signals
& SerialSignal_CTS
) {
2096 info
->port
.tty
->hw_stopped
= 0;
2097 info
->pending_bh
|= BH_TRANSMIT
;
2101 if (!(info
->signals
& SerialSignal_CTS
))
2102 info
->port
.tty
->hw_stopped
= 1;
2108 static void dcd_change(struct slgt_info
*info
, unsigned short status
)
2110 if (status
& BIT1
) {
2111 info
->signals
|= SerialSignal_DCD
;
2112 info
->input_signal_events
.dcd_up
++;
2114 info
->signals
&= ~SerialSignal_DCD
;
2115 info
->input_signal_events
.dcd_down
++;
2117 DBGISR(("dcd_change %s signals=%04X\n", info
->device_name
, info
->signals
));
2118 if ((info
->dcd_chkcount
)++ == IO_PIN_SHUTDOWN_LIMIT
) {
2119 slgt_irq_off(info
, IRQ_DCD
);
2123 #if SYNCLINK_GENERIC_HDLC
2124 if (info
->netcount
) {
2125 if (info
->signals
& SerialSignal_DCD
)
2126 netif_carrier_on(info
->netdev
);
2128 netif_carrier_off(info
->netdev
);
2131 wake_up_interruptible(&info
->status_event_wait_q
);
2132 wake_up_interruptible(&info
->event_wait_q
);
2133 info
->pending_bh
|= BH_STATUS
;
2135 if (info
->port
.flags
& ASYNC_CHECK_CD
) {
2136 if (info
->signals
& SerialSignal_DCD
)
2137 wake_up_interruptible(&info
->port
.open_wait
);
2140 tty_hangup(info
->port
.tty
);
2145 static void ri_change(struct slgt_info
*info
, unsigned short status
)
2147 if (status
& BIT0
) {
2148 info
->signals
|= SerialSignal_RI
;
2149 info
->input_signal_events
.ri_up
++;
2151 info
->signals
&= ~SerialSignal_RI
;
2152 info
->input_signal_events
.ri_down
++;
2154 DBGISR(("ri_change %s signals=%04X\n", info
->device_name
, info
->signals
));
2155 if ((info
->ri_chkcount
)++ == IO_PIN_SHUTDOWN_LIMIT
) {
2156 slgt_irq_off(info
, IRQ_RI
);
2160 wake_up_interruptible(&info
->status_event_wait_q
);
2161 wake_up_interruptible(&info
->event_wait_q
);
2162 info
->pending_bh
|= BH_STATUS
;
2165 static void isr_serial(struct slgt_info
*info
)
2167 unsigned short status
= rd_reg16(info
, SSR
);
2169 DBGISR(("%s isr_serial status=%04X\n", info
->device_name
, status
));
2171 wr_reg16(info
, SSR
, status
); /* clear pending */
2173 info
->irq_occurred
= true;
2175 if (info
->params
.mode
== MGSL_MODE_ASYNC
) {
2176 if (status
& IRQ_TXIDLE
) {
2178 isr_txeom(info
, status
);
2180 if ((status
& IRQ_RXBREAK
) && (status
& RXBREAK
)) {
2182 /* process break detection if tty control allows */
2183 if (info
->port
.tty
) {
2184 if (!(status
& info
->ignore_status_mask
)) {
2185 if (info
->read_status_mask
& MASK_BREAK
) {
2186 tty_insert_flip_char(info
->port
.tty
, 0, TTY_BREAK
);
2187 if (info
->port
.flags
& ASYNC_SAK
)
2188 do_SAK(info
->port
.tty
);
2194 if (status
& (IRQ_TXIDLE
+ IRQ_TXUNDER
))
2195 isr_txeom(info
, status
);
2197 if (status
& IRQ_RXIDLE
) {
2198 if (status
& RXIDLE
)
2199 info
->icount
.rxidle
++;
2201 info
->icount
.exithunt
++;
2202 wake_up_interruptible(&info
->event_wait_q
);
2205 if (status
& IRQ_RXOVER
)
2209 if (status
& IRQ_DSR
)
2210 dsr_change(info
, status
);
2211 if (status
& IRQ_CTS
)
2212 cts_change(info
, status
);
2213 if (status
& IRQ_DCD
)
2214 dcd_change(info
, status
);
2215 if (status
& IRQ_RI
)
2216 ri_change(info
, status
);
2219 static void isr_rdma(struct slgt_info
*info
)
2221 unsigned int status
= rd_reg32(info
, RDCSR
);
2223 DBGISR(("%s isr_rdma status=%08x\n", info
->device_name
, status
));
2225 /* RDCSR (rx DMA control/status)
2228 * 06 save status byte to DMA buffer
2230 * 04 eol (end of list)
2231 * 03 eob (end of buffer)
2236 wr_reg32(info
, RDCSR
, status
); /* clear pending */
2238 if (status
& (BIT5
+ BIT4
)) {
2239 DBGISR(("%s isr_rdma rx_restart=1\n", info
->device_name
));
2240 info
->rx_restart
= true;
2242 info
->pending_bh
|= BH_RECEIVE
;
2245 static void isr_tdma(struct slgt_info
*info
)
2247 unsigned int status
= rd_reg32(info
, TDCSR
);
2249 DBGISR(("%s isr_tdma status=%08x\n", info
->device_name
, status
));
2251 /* TDCSR (tx DMA control/status)
2255 * 04 eol (end of list)
2256 * 03 eob (end of buffer)
2261 wr_reg32(info
, TDCSR
, status
); /* clear pending */
2263 if (status
& (BIT5
+ BIT4
+ BIT3
)) {
2264 // another transmit buffer has completed
2265 // run bottom half to get more send data from user
2266 info
->pending_bh
|= BH_TRANSMIT
;
2270 static void isr_txeom(struct slgt_info
*info
, unsigned short status
)
2272 DBGISR(("%s txeom status=%04x\n", info
->device_name
, status
));
2274 slgt_irq_off(info
, IRQ_TXDATA
+ IRQ_TXIDLE
+ IRQ_TXUNDER
);
2277 if (status
& IRQ_TXUNDER
) {
2278 unsigned short val
= rd_reg16(info
, TCR
);
2279 wr_reg16(info
, TCR
, (unsigned short)(val
| BIT2
)); /* set reset bit */
2280 wr_reg16(info
, TCR
, val
); /* clear reset bit */
2283 if (info
->tx_active
) {
2284 if (info
->params
.mode
!= MGSL_MODE_ASYNC
) {
2285 if (status
& IRQ_TXUNDER
)
2286 info
->icount
.txunder
++;
2287 else if (status
& IRQ_TXIDLE
)
2288 info
->icount
.txok
++;
2291 info
->tx_active
= false;
2294 del_timer(&info
->tx_timer
);
2296 if (info
->params
.mode
!= MGSL_MODE_ASYNC
&& info
->drop_rts_on_tx_done
) {
2297 info
->signals
&= ~SerialSignal_RTS
;
2298 info
->drop_rts_on_tx_done
= false;
2302 #if SYNCLINK_GENERIC_HDLC
2304 hdlcdev_tx_done(info
);
2308 if (info
->port
.tty
&& (info
->port
.tty
->stopped
|| info
->port
.tty
->hw_stopped
)) {
2312 info
->pending_bh
|= BH_TRANSMIT
;
2317 static void isr_gpio(struct slgt_info
*info
, unsigned int changed
, unsigned int state
)
2319 struct cond_wait
*w
, *prev
;
2321 /* wake processes waiting for specific transitions */
2322 for (w
= info
->gpio_wait_q
, prev
= NULL
; w
!= NULL
; w
= w
->next
) {
2323 if (w
->data
& changed
) {
2325 wake_up_interruptible(&w
->q
);
2327 prev
->next
= w
->next
;
2329 info
->gpio_wait_q
= w
->next
;
2335 /* interrupt service routine
2337 * irq interrupt number
2338 * dev_id device ID supplied during interrupt registration
2340 static irqreturn_t
slgt_interrupt(int dummy
, void *dev_id
)
2342 struct slgt_info
*info
= dev_id
;
2346 DBGISR(("slgt_interrupt irq=%d entry\n", info
->irq_level
));
2348 spin_lock(&info
->lock
);
2350 while((gsr
= rd_reg32(info
, GSR
) & 0xffffff00)) {
2351 DBGISR(("%s gsr=%08x\n", info
->device_name
, gsr
));
2352 info
->irq_occurred
= true;
2353 for(i
=0; i
< info
->port_count
; i
++) {
2354 if (info
->port_array
[i
] == NULL
)
2356 if (gsr
& (BIT8
<< i
))
2357 isr_serial(info
->port_array
[i
]);
2358 if (gsr
& (BIT16
<< (i
*2)))
2359 isr_rdma(info
->port_array
[i
]);
2360 if (gsr
& (BIT17
<< (i
*2)))
2361 isr_tdma(info
->port_array
[i
]);
2365 if (info
->gpio_present
) {
2367 unsigned int changed
;
2368 while ((changed
= rd_reg32(info
, IOSR
)) != 0) {
2369 DBGISR(("%s iosr=%08x\n", info
->device_name
, changed
));
2370 /* read latched state of GPIO signals */
2371 state
= rd_reg32(info
, IOVR
);
2372 /* clear pending GPIO interrupt bits */
2373 wr_reg32(info
, IOSR
, changed
);
2374 for (i
=0 ; i
< info
->port_count
; i
++) {
2375 if (info
->port_array
[i
] != NULL
)
2376 isr_gpio(info
->port_array
[i
], changed
, state
);
2381 for(i
=0; i
< info
->port_count
; i
++) {
2382 struct slgt_info
*port
= info
->port_array
[i
];
2384 if (port
&& (port
->port
.count
|| port
->netcount
) &&
2385 port
->pending_bh
&& !port
->bh_running
&&
2386 !port
->bh_requested
) {
2387 DBGISR(("%s bh queued\n", port
->device_name
));
2388 schedule_work(&port
->task
);
2389 port
->bh_requested
= true;
2393 spin_unlock(&info
->lock
);
2395 DBGISR(("slgt_interrupt irq=%d exit\n", info
->irq_level
));
2399 static int startup(struct slgt_info
*info
)
2401 DBGINFO(("%s startup\n", info
->device_name
));
2403 if (info
->port
.flags
& ASYNC_INITIALIZED
)
2406 if (!info
->tx_buf
) {
2407 info
->tx_buf
= kmalloc(info
->max_frame_size
, GFP_KERNEL
);
2408 if (!info
->tx_buf
) {
2409 DBGERR(("%s can't allocate tx buffer\n", info
->device_name
));
2414 info
->pending_bh
= 0;
2416 memset(&info
->icount
, 0, sizeof(info
->icount
));
2418 /* program hardware for current parameters */
2419 change_params(info
);
2422 clear_bit(TTY_IO_ERROR
, &info
->port
.tty
->flags
);
2424 info
->port
.flags
|= ASYNC_INITIALIZED
;
2430 * called by close() and hangup() to shutdown hardware
2432 static void shutdown(struct slgt_info
*info
)
2434 unsigned long flags
;
2436 if (!(info
->port
.flags
& ASYNC_INITIALIZED
))
2439 DBGINFO(("%s shutdown\n", info
->device_name
));
2441 /* clear status wait queue because status changes */
2442 /* can't happen after shutting down the hardware */
2443 wake_up_interruptible(&info
->status_event_wait_q
);
2444 wake_up_interruptible(&info
->event_wait_q
);
2446 del_timer_sync(&info
->tx_timer
);
2447 del_timer_sync(&info
->rx_timer
);
2449 kfree(info
->tx_buf
);
2450 info
->tx_buf
= NULL
;
2452 spin_lock_irqsave(&info
->lock
,flags
);
2457 slgt_irq_off(info
, IRQ_ALL
| IRQ_MASTER
);
2459 if (!info
->port
.tty
|| info
->port
.tty
->termios
->c_cflag
& HUPCL
) {
2460 info
->signals
&= ~(SerialSignal_DTR
+ SerialSignal_RTS
);
2464 flush_cond_wait(&info
->gpio_wait_q
);
2466 spin_unlock_irqrestore(&info
->lock
,flags
);
2469 set_bit(TTY_IO_ERROR
, &info
->port
.tty
->flags
);
2471 info
->port
.flags
&= ~ASYNC_INITIALIZED
;
2474 static void program_hw(struct slgt_info
*info
)
2476 unsigned long flags
;
2478 spin_lock_irqsave(&info
->lock
,flags
);
2483 if (info
->params
.mode
!= MGSL_MODE_ASYNC
||
2491 info
->dcd_chkcount
= 0;
2492 info
->cts_chkcount
= 0;
2493 info
->ri_chkcount
= 0;
2494 info
->dsr_chkcount
= 0;
2496 slgt_irq_on(info
, IRQ_DCD
| IRQ_CTS
| IRQ_DSR
);
2499 if (info
->netcount
||
2500 (info
->port
.tty
&& info
->port
.tty
->termios
->c_cflag
& CREAD
))
2503 spin_unlock_irqrestore(&info
->lock
,flags
);
2507 * reconfigure adapter based on new parameters
2509 static void change_params(struct slgt_info
*info
)
2514 if (!info
->port
.tty
|| !info
->port
.tty
->termios
)
2516 DBGINFO(("%s change_params\n", info
->device_name
));
2518 cflag
= info
->port
.tty
->termios
->c_cflag
;
2520 /* if B0 rate (hangup) specified then negate DTR and RTS */
2521 /* otherwise assert DTR and RTS */
2523 info
->signals
|= SerialSignal_RTS
+ SerialSignal_DTR
;
2525 info
->signals
&= ~(SerialSignal_RTS
+ SerialSignal_DTR
);
2527 /* byte size and parity */
2529 switch (cflag
& CSIZE
) {
2530 case CS5
: info
->params
.data_bits
= 5; break;
2531 case CS6
: info
->params
.data_bits
= 6; break;
2532 case CS7
: info
->params
.data_bits
= 7; break;
2533 case CS8
: info
->params
.data_bits
= 8; break;
2534 default: info
->params
.data_bits
= 7; break;
2537 info
->params
.stop_bits
= (cflag
& CSTOPB
) ? 2 : 1;
2540 info
->params
.parity
= (cflag
& PARODD
) ? ASYNC_PARITY_ODD
: ASYNC_PARITY_EVEN
;
2542 info
->params
.parity
= ASYNC_PARITY_NONE
;
2544 /* calculate number of jiffies to transmit a full
2545 * FIFO (32 bytes) at specified data rate
2547 bits_per_char
= info
->params
.data_bits
+
2548 info
->params
.stop_bits
+ 1;
2550 info
->params
.data_rate
= tty_get_baud_rate(info
->port
.tty
);
2552 if (info
->params
.data_rate
) {
2553 info
->timeout
= (32*HZ
*bits_per_char
) /
2554 info
->params
.data_rate
;
2556 info
->timeout
+= HZ
/50; /* Add .02 seconds of slop */
2558 if (cflag
& CRTSCTS
)
2559 info
->port
.flags
|= ASYNC_CTS_FLOW
;
2561 info
->port
.flags
&= ~ASYNC_CTS_FLOW
;
2564 info
->port
.flags
&= ~ASYNC_CHECK_CD
;
2566 info
->port
.flags
|= ASYNC_CHECK_CD
;
2568 /* process tty input control flags */
2570 info
->read_status_mask
= IRQ_RXOVER
;
2571 if (I_INPCK(info
->port
.tty
))
2572 info
->read_status_mask
|= MASK_PARITY
| MASK_FRAMING
;
2573 if (I_BRKINT(info
->port
.tty
) || I_PARMRK(info
->port
.tty
))
2574 info
->read_status_mask
|= MASK_BREAK
;
2575 if (I_IGNPAR(info
->port
.tty
))
2576 info
->ignore_status_mask
|= MASK_PARITY
| MASK_FRAMING
;
2577 if (I_IGNBRK(info
->port
.tty
)) {
2578 info
->ignore_status_mask
|= MASK_BREAK
;
2579 /* If ignoring parity and break indicators, ignore
2580 * overruns too. (For real raw support).
2582 if (I_IGNPAR(info
->port
.tty
))
2583 info
->ignore_status_mask
|= MASK_OVERRUN
;
2589 static int get_stats(struct slgt_info
*info
, struct mgsl_icount __user
*user_icount
)
2591 DBGINFO(("%s get_stats\n", info
->device_name
));
2593 memset(&info
->icount
, 0, sizeof(info
->icount
));
2595 if (copy_to_user(user_icount
, &info
->icount
, sizeof(struct mgsl_icount
)))
2601 static int get_params(struct slgt_info
*info
, MGSL_PARAMS __user
*user_params
)
2603 DBGINFO(("%s get_params\n", info
->device_name
));
2604 if (copy_to_user(user_params
, &info
->params
, sizeof(MGSL_PARAMS
)))
2609 static int set_params(struct slgt_info
*info
, MGSL_PARAMS __user
*new_params
)
2611 unsigned long flags
;
2612 MGSL_PARAMS tmp_params
;
2614 DBGINFO(("%s set_params\n", info
->device_name
));
2615 if (copy_from_user(&tmp_params
, new_params
, sizeof(MGSL_PARAMS
)))
2618 spin_lock_irqsave(&info
->lock
, flags
);
2619 memcpy(&info
->params
, &tmp_params
, sizeof(MGSL_PARAMS
));
2620 spin_unlock_irqrestore(&info
->lock
, flags
);
2622 change_params(info
);
2627 static int get_txidle(struct slgt_info
*info
, int __user
*idle_mode
)
2629 DBGINFO(("%s get_txidle=%d\n", info
->device_name
, info
->idle_mode
));
2630 if (put_user(info
->idle_mode
, idle_mode
))
2635 static int set_txidle(struct slgt_info
*info
, int idle_mode
)
2637 unsigned long flags
;
2638 DBGINFO(("%s set_txidle(%d)\n", info
->device_name
, idle_mode
));
2639 spin_lock_irqsave(&info
->lock
,flags
);
2640 info
->idle_mode
= idle_mode
;
2641 if (info
->params
.mode
!= MGSL_MODE_ASYNC
)
2643 spin_unlock_irqrestore(&info
->lock
,flags
);
2647 static int tx_enable(struct slgt_info
*info
, int enable
)
2649 unsigned long flags
;
2650 DBGINFO(("%s tx_enable(%d)\n", info
->device_name
, enable
));
2651 spin_lock_irqsave(&info
->lock
,flags
);
2653 if (!info
->tx_enabled
)
2656 if (info
->tx_enabled
)
2659 spin_unlock_irqrestore(&info
->lock
,flags
);
2664 * abort transmit HDLC frame
2666 static int tx_abort(struct slgt_info
*info
)
2668 unsigned long flags
;
2669 DBGINFO(("%s tx_abort\n", info
->device_name
));
2670 spin_lock_irqsave(&info
->lock
,flags
);
2672 spin_unlock_irqrestore(&info
->lock
,flags
);
2676 static int rx_enable(struct slgt_info
*info
, int enable
)
2678 unsigned long flags
;
2679 unsigned int rbuf_fill_level
;
2680 DBGINFO(("%s rx_enable(%08x)\n", info
->device_name
, enable
));
2681 spin_lock_irqsave(&info
->lock
,flags
);
2683 * enable[31..16] = receive DMA buffer fill level
2684 * 0 = noop (leave fill level unchanged)
2685 * fill level must be multiple of 4 and <= buffer size
2687 rbuf_fill_level
= ((unsigned int)enable
) >> 16;
2688 if (rbuf_fill_level
) {
2689 if ((rbuf_fill_level
> DMABUFSIZE
) || (rbuf_fill_level
% 4)) {
2690 spin_unlock_irqrestore(&info
->lock
, flags
);
2693 info
->rbuf_fill_level
= rbuf_fill_level
;
2694 rx_stop(info
); /* restart receiver to use new fill level */
2698 * enable[1..0] = receiver enable command
2701 * 2 = enable or force hunt mode if already enabled
2705 if (!info
->rx_enabled
)
2707 else if (enable
== 2) {
2708 /* force hunt mode (write 1 to RCR[3]) */
2709 wr_reg16(info
, RCR
, rd_reg16(info
, RCR
) | BIT3
);
2712 if (info
->rx_enabled
)
2715 spin_unlock_irqrestore(&info
->lock
,flags
);
2720 * wait for specified event to occur
2722 static int wait_mgsl_event(struct slgt_info
*info
, int __user
*mask_ptr
)
2724 unsigned long flags
;
2727 struct mgsl_icount cprev
, cnow
;
2730 struct _input_signal_events oldsigs
, newsigs
;
2731 DECLARE_WAITQUEUE(wait
, current
);
2733 if (get_user(mask
, mask_ptr
))
2736 DBGINFO(("%s wait_mgsl_event(%d)\n", info
->device_name
, mask
));
2738 spin_lock_irqsave(&info
->lock
,flags
);
2740 /* return immediately if state matches requested events */
2745 ( ((s
& SerialSignal_DSR
) ? MgslEvent_DsrActive
:MgslEvent_DsrInactive
) +
2746 ((s
& SerialSignal_DCD
) ? MgslEvent_DcdActive
:MgslEvent_DcdInactive
) +
2747 ((s
& SerialSignal_CTS
) ? MgslEvent_CtsActive
:MgslEvent_CtsInactive
) +
2748 ((s
& SerialSignal_RI
) ? MgslEvent_RiActive
:MgslEvent_RiInactive
) );
2750 spin_unlock_irqrestore(&info
->lock
,flags
);
2754 /* save current irq counts */
2755 cprev
= info
->icount
;
2756 oldsigs
= info
->input_signal_events
;
2758 /* enable hunt and idle irqs if needed */
2759 if (mask
& (MgslEvent_ExitHuntMode
+MgslEvent_IdleReceived
)) {
2760 unsigned short val
= rd_reg16(info
, SCR
);
2761 if (!(val
& IRQ_RXIDLE
))
2762 wr_reg16(info
, SCR
, (unsigned short)(val
| IRQ_RXIDLE
));
2765 set_current_state(TASK_INTERRUPTIBLE
);
2766 add_wait_queue(&info
->event_wait_q
, &wait
);
2768 spin_unlock_irqrestore(&info
->lock
,flags
);
2772 if (signal_pending(current
)) {
2777 /* get current irq counts */
2778 spin_lock_irqsave(&info
->lock
,flags
);
2779 cnow
= info
->icount
;
2780 newsigs
= info
->input_signal_events
;
2781 set_current_state(TASK_INTERRUPTIBLE
);
2782 spin_unlock_irqrestore(&info
->lock
,flags
);
2784 /* if no change, wait aborted for some reason */
2785 if (newsigs
.dsr_up
== oldsigs
.dsr_up
&&
2786 newsigs
.dsr_down
== oldsigs
.dsr_down
&&
2787 newsigs
.dcd_up
== oldsigs
.dcd_up
&&
2788 newsigs
.dcd_down
== oldsigs
.dcd_down
&&
2789 newsigs
.cts_up
== oldsigs
.cts_up
&&
2790 newsigs
.cts_down
== oldsigs
.cts_down
&&
2791 newsigs
.ri_up
== oldsigs
.ri_up
&&
2792 newsigs
.ri_down
== oldsigs
.ri_down
&&
2793 cnow
.exithunt
== cprev
.exithunt
&&
2794 cnow
.rxidle
== cprev
.rxidle
) {
2800 ( (newsigs
.dsr_up
!= oldsigs
.dsr_up
? MgslEvent_DsrActive
:0) +
2801 (newsigs
.dsr_down
!= oldsigs
.dsr_down
? MgslEvent_DsrInactive
:0) +
2802 (newsigs
.dcd_up
!= oldsigs
.dcd_up
? MgslEvent_DcdActive
:0) +
2803 (newsigs
.dcd_down
!= oldsigs
.dcd_down
? MgslEvent_DcdInactive
:0) +
2804 (newsigs
.cts_up
!= oldsigs
.cts_up
? MgslEvent_CtsActive
:0) +
2805 (newsigs
.cts_down
!= oldsigs
.cts_down
? MgslEvent_CtsInactive
:0) +
2806 (newsigs
.ri_up
!= oldsigs
.ri_up
? MgslEvent_RiActive
:0) +
2807 (newsigs
.ri_down
!= oldsigs
.ri_down
? MgslEvent_RiInactive
:0) +
2808 (cnow
.exithunt
!= cprev
.exithunt
? MgslEvent_ExitHuntMode
:0) +
2809 (cnow
.rxidle
!= cprev
.rxidle
? MgslEvent_IdleReceived
:0) );
2817 remove_wait_queue(&info
->event_wait_q
, &wait
);
2818 set_current_state(TASK_RUNNING
);
2821 if (mask
& (MgslEvent_ExitHuntMode
+ MgslEvent_IdleReceived
)) {
2822 spin_lock_irqsave(&info
->lock
,flags
);
2823 if (!waitqueue_active(&info
->event_wait_q
)) {
2824 /* disable enable exit hunt mode/idle rcvd IRQs */
2826 (unsigned short)(rd_reg16(info
, SCR
) & ~IRQ_RXIDLE
));
2828 spin_unlock_irqrestore(&info
->lock
,flags
);
2832 rc
= put_user(events
, mask_ptr
);
2836 static int get_interface(struct slgt_info
*info
, int __user
*if_mode
)
2838 DBGINFO(("%s get_interface=%x\n", info
->device_name
, info
->if_mode
));
2839 if (put_user(info
->if_mode
, if_mode
))
2844 static int set_interface(struct slgt_info
*info
, int if_mode
)
2846 unsigned long flags
;
2849 DBGINFO(("%s set_interface=%x)\n", info
->device_name
, if_mode
));
2850 spin_lock_irqsave(&info
->lock
,flags
);
2851 info
->if_mode
= if_mode
;
2855 /* TCR (tx control) 07 1=RTS driver control */
2856 val
= rd_reg16(info
, TCR
);
2857 if (info
->if_mode
& MGSL_INTERFACE_RTS_EN
)
2861 wr_reg16(info
, TCR
, val
);
2863 spin_unlock_irqrestore(&info
->lock
,flags
);
2868 * set general purpose IO pin state and direction
2871 * state each bit indicates a pin state
2872 * smask set bit indicates pin state to set
2873 * dir each bit indicates a pin direction (0=input, 1=output)
2874 * dmask set bit indicates pin direction to set
2876 static int set_gpio(struct slgt_info
*info
, struct gpio_desc __user
*user_gpio
)
2878 unsigned long flags
;
2879 struct gpio_desc gpio
;
2882 if (!info
->gpio_present
)
2884 if (copy_from_user(&gpio
, user_gpio
, sizeof(gpio
)))
2886 DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
2887 info
->device_name
, gpio
.state
, gpio
.smask
,
2888 gpio
.dir
, gpio
.dmask
));
2890 spin_lock_irqsave(&info
->lock
,flags
);
2892 data
= rd_reg32(info
, IODR
);
2893 data
|= gpio
.dmask
& gpio
.dir
;
2894 data
&= ~(gpio
.dmask
& ~gpio
.dir
);
2895 wr_reg32(info
, IODR
, data
);
2898 data
= rd_reg32(info
, IOVR
);
2899 data
|= gpio
.smask
& gpio
.state
;
2900 data
&= ~(gpio
.smask
& ~gpio
.state
);
2901 wr_reg32(info
, IOVR
, data
);
2903 spin_unlock_irqrestore(&info
->lock
,flags
);
2909 * get general purpose IO pin state and direction
2911 static int get_gpio(struct slgt_info
*info
, struct gpio_desc __user
*user_gpio
)
2913 struct gpio_desc gpio
;
2914 if (!info
->gpio_present
)
2916 gpio
.state
= rd_reg32(info
, IOVR
);
2917 gpio
.smask
= 0xffffffff;
2918 gpio
.dir
= rd_reg32(info
, IODR
);
2919 gpio
.dmask
= 0xffffffff;
2920 if (copy_to_user(user_gpio
, &gpio
, sizeof(gpio
)))
2922 DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
2923 info
->device_name
, gpio
.state
, gpio
.dir
));
2928 * conditional wait facility
2930 static void init_cond_wait(struct cond_wait
*w
, unsigned int data
)
2932 init_waitqueue_head(&w
->q
);
2933 init_waitqueue_entry(&w
->wait
, current
);
2937 static void add_cond_wait(struct cond_wait
**head
, struct cond_wait
*w
)
2939 set_current_state(TASK_INTERRUPTIBLE
);
2940 add_wait_queue(&w
->q
, &w
->wait
);
2945 static void remove_cond_wait(struct cond_wait
**head
, struct cond_wait
*cw
)
2947 struct cond_wait
*w
, *prev
;
2948 remove_wait_queue(&cw
->q
, &cw
->wait
);
2949 set_current_state(TASK_RUNNING
);
2950 for (w
= *head
, prev
= NULL
; w
!= NULL
; prev
= w
, w
= w
->next
) {
2953 prev
->next
= w
->next
;
2961 static void flush_cond_wait(struct cond_wait
**head
)
2963 while (*head
!= NULL
) {
2964 wake_up_interruptible(&(*head
)->q
);
2965 *head
= (*head
)->next
;
2970 * wait for general purpose I/O pin(s) to enter specified state
2973 * state - bit indicates target pin state
2974 * smask - set bit indicates watched pin
2976 * The wait ends when at least one watched pin enters the specified
2977 * state. When 0 (no error) is returned, user_gpio->state is set to the
2978 * state of all GPIO pins when the wait ends.
2980 * Note: Each pin may be a dedicated input, dedicated output, or
2981 * configurable input/output. The number and configuration of pins
2982 * varies with the specific adapter model. Only input pins (dedicated
2983 * or configured) can be monitored with this function.
2985 static int wait_gpio(struct slgt_info
*info
, struct gpio_desc __user
*user_gpio
)
2987 unsigned long flags
;
2989 struct gpio_desc gpio
;
2990 struct cond_wait wait
;
2993 if (!info
->gpio_present
)
2995 if (copy_from_user(&gpio
, user_gpio
, sizeof(gpio
)))
2997 DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
2998 info
->device_name
, gpio
.state
, gpio
.smask
));
2999 /* ignore output pins identified by set IODR bit */
3000 if ((gpio
.smask
&= ~rd_reg32(info
, IODR
)) == 0)
3002 init_cond_wait(&wait
, gpio
.smask
);
3004 spin_lock_irqsave(&info
->lock
, flags
);
3005 /* enable interrupts for watched pins */
3006 wr_reg32(info
, IOER
, rd_reg32(info
, IOER
) | gpio
.smask
);
3007 /* get current pin states */
3008 state
= rd_reg32(info
, IOVR
);
3010 if (gpio
.smask
& ~(state
^ gpio
.state
)) {
3011 /* already in target state */
3014 /* wait for target state */
3015 add_cond_wait(&info
->gpio_wait_q
, &wait
);
3016 spin_unlock_irqrestore(&info
->lock
, flags
);
3018 if (signal_pending(current
))
3021 gpio
.state
= wait
.data
;
3022 spin_lock_irqsave(&info
->lock
, flags
);
3023 remove_cond_wait(&info
->gpio_wait_q
, &wait
);
3026 /* disable all GPIO interrupts if no waiting processes */
3027 if (info
->gpio_wait_q
== NULL
)
3028 wr_reg32(info
, IOER
, 0);
3029 spin_unlock_irqrestore(&info
->lock
,flags
);
3031 if ((rc
== 0) && copy_to_user(user_gpio
, &gpio
, sizeof(gpio
)))
3036 static int modem_input_wait(struct slgt_info
*info
,int arg
)
3038 unsigned long flags
;
3040 struct mgsl_icount cprev
, cnow
;
3041 DECLARE_WAITQUEUE(wait
, current
);
3043 /* save current irq counts */
3044 spin_lock_irqsave(&info
->lock
,flags
);
3045 cprev
= info
->icount
;
3046 add_wait_queue(&info
->status_event_wait_q
, &wait
);
3047 set_current_state(TASK_INTERRUPTIBLE
);
3048 spin_unlock_irqrestore(&info
->lock
,flags
);
3052 if (signal_pending(current
)) {
3057 /* get new irq counts */
3058 spin_lock_irqsave(&info
->lock
,flags
);
3059 cnow
= info
->icount
;
3060 set_current_state(TASK_INTERRUPTIBLE
);
3061 spin_unlock_irqrestore(&info
->lock
,flags
);
3063 /* if no change, wait aborted for some reason */
3064 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
3065 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
) {
3070 /* check for change in caller specified modem input */
3071 if ((arg
& TIOCM_RNG
&& cnow
.rng
!= cprev
.rng
) ||
3072 (arg
& TIOCM_DSR
&& cnow
.dsr
!= cprev
.dsr
) ||
3073 (arg
& TIOCM_CD
&& cnow
.dcd
!= cprev
.dcd
) ||
3074 (arg
& TIOCM_CTS
&& cnow
.cts
!= cprev
.cts
)) {
3081 remove_wait_queue(&info
->status_event_wait_q
, &wait
);
3082 set_current_state(TASK_RUNNING
);
3087 * return state of serial control and status signals
3089 static int tiocmget(struct tty_struct
*tty
, struct file
*file
)
3091 struct slgt_info
*info
= tty
->driver_data
;
3092 unsigned int result
;
3093 unsigned long flags
;
3095 spin_lock_irqsave(&info
->lock
,flags
);
3097 spin_unlock_irqrestore(&info
->lock
,flags
);
3099 result
= ((info
->signals
& SerialSignal_RTS
) ? TIOCM_RTS
:0) +
3100 ((info
->signals
& SerialSignal_DTR
) ? TIOCM_DTR
:0) +
3101 ((info
->signals
& SerialSignal_DCD
) ? TIOCM_CAR
:0) +
3102 ((info
->signals
& SerialSignal_RI
) ? TIOCM_RNG
:0) +
3103 ((info
->signals
& SerialSignal_DSR
) ? TIOCM_DSR
:0) +
3104 ((info
->signals
& SerialSignal_CTS
) ? TIOCM_CTS
:0);
3106 DBGINFO(("%s tiocmget value=%08X\n", info
->device_name
, result
));
3111 * set modem control signals (DTR/RTS)
3113 * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
3114 * TIOCMSET = set/clear signal values
3115 * value bit mask for command
3117 static int tiocmset(struct tty_struct
*tty
, struct file
*file
,
3118 unsigned int set
, unsigned int clear
)
3120 struct slgt_info
*info
= tty
->driver_data
;
3121 unsigned long flags
;
3123 DBGINFO(("%s tiocmset(%x,%x)\n", info
->device_name
, set
, clear
));
3125 if (set
& TIOCM_RTS
)
3126 info
->signals
|= SerialSignal_RTS
;
3127 if (set
& TIOCM_DTR
)
3128 info
->signals
|= SerialSignal_DTR
;
3129 if (clear
& TIOCM_RTS
)
3130 info
->signals
&= ~SerialSignal_RTS
;
3131 if (clear
& TIOCM_DTR
)
3132 info
->signals
&= ~SerialSignal_DTR
;
3134 spin_lock_irqsave(&info
->lock
,flags
);
3136 spin_unlock_irqrestore(&info
->lock
,flags
);
3141 * block current process until the device is ready to open
3143 static int block_til_ready(struct tty_struct
*tty
, struct file
*filp
,
3144 struct slgt_info
*info
)
3146 DECLARE_WAITQUEUE(wait
, current
);
3148 bool do_clocal
= false;
3149 bool extra_count
= false;
3150 unsigned long flags
;
3152 DBGINFO(("%s block_til_ready\n", tty
->driver
->name
));
3154 if (filp
->f_flags
& O_NONBLOCK
|| tty
->flags
& (1 << TTY_IO_ERROR
)){
3155 /* nonblock mode is set or port is not enabled */
3156 info
->port
.flags
|= ASYNC_NORMAL_ACTIVE
;
3160 if (tty
->termios
->c_cflag
& CLOCAL
)
3163 /* Wait for carrier detect and the line to become
3164 * free (i.e., not in use by the callout). While we are in
3165 * this loop, info->port.count is dropped by one, so that
3166 * close() knows when to free things. We restore it upon
3167 * exit, either normal or abnormal.
3171 add_wait_queue(&info
->port
.open_wait
, &wait
);
3173 spin_lock_irqsave(&info
->lock
, flags
);
3174 if (!tty_hung_up_p(filp
)) {
3178 spin_unlock_irqrestore(&info
->lock
, flags
);
3179 info
->port
.blocked_open
++;
3182 if ((tty
->termios
->c_cflag
& CBAUD
)) {
3183 spin_lock_irqsave(&info
->lock
,flags
);
3184 info
->signals
|= SerialSignal_RTS
+ SerialSignal_DTR
;
3186 spin_unlock_irqrestore(&info
->lock
,flags
);
3189 set_current_state(TASK_INTERRUPTIBLE
);
3191 if (tty_hung_up_p(filp
) || !(info
->port
.flags
& ASYNC_INITIALIZED
)){
3192 retval
= (info
->port
.flags
& ASYNC_HUP_NOTIFY
) ?
3193 -EAGAIN
: -ERESTARTSYS
;
3197 spin_lock_irqsave(&info
->lock
,flags
);
3199 spin_unlock_irqrestore(&info
->lock
,flags
);
3201 if (!(info
->port
.flags
& ASYNC_CLOSING
) &&
3202 (do_clocal
|| (info
->signals
& SerialSignal_DCD
)) ) {
3206 if (signal_pending(current
)) {
3207 retval
= -ERESTARTSYS
;
3211 DBGINFO(("%s block_til_ready wait\n", tty
->driver
->name
));
3215 set_current_state(TASK_RUNNING
);
3216 remove_wait_queue(&info
->port
.open_wait
, &wait
);
3220 info
->port
.blocked_open
--;
3223 info
->port
.flags
|= ASYNC_NORMAL_ACTIVE
;
3225 DBGINFO(("%s block_til_ready ready, rc=%d\n", tty
->driver
->name
, retval
));
3229 static int alloc_tmp_rbuf(struct slgt_info
*info
)
3231 info
->tmp_rbuf
= kmalloc(info
->max_frame_size
+ 5, GFP_KERNEL
);
3232 if (info
->tmp_rbuf
== NULL
)
3237 static void free_tmp_rbuf(struct slgt_info
*info
)
3239 kfree(info
->tmp_rbuf
);
3240 info
->tmp_rbuf
= NULL
;
3244 * allocate DMA descriptor lists.
3246 static int alloc_desc(struct slgt_info
*info
)
3251 /* allocate memory to hold descriptor lists */
3252 info
->bufs
= pci_alloc_consistent(info
->pdev
, DESC_LIST_SIZE
, &info
->bufs_dma_addr
);
3253 if (info
->bufs
== NULL
)
3256 memset(info
->bufs
, 0, DESC_LIST_SIZE
);
3258 info
->rbufs
= (struct slgt_desc
*)info
->bufs
;
3259 info
->tbufs
= ((struct slgt_desc
*)info
->bufs
) + info
->rbuf_count
;
3261 pbufs
= (unsigned int)info
->bufs_dma_addr
;
3264 * Build circular lists of descriptors
3267 for (i
=0; i
< info
->rbuf_count
; i
++) {
3268 /* physical address of this descriptor */
3269 info
->rbufs
[i
].pdesc
= pbufs
+ (i
* sizeof(struct slgt_desc
));
3271 /* physical address of next descriptor */
3272 if (i
== info
->rbuf_count
- 1)
3273 info
->rbufs
[i
].next
= cpu_to_le32(pbufs
);
3275 info
->rbufs
[i
].next
= cpu_to_le32(pbufs
+ ((i
+1) * sizeof(struct slgt_desc
)));
3276 set_desc_count(info
->rbufs
[i
], DMABUFSIZE
);
3279 for (i
=0; i
< info
->tbuf_count
; i
++) {
3280 /* physical address of this descriptor */
3281 info
->tbufs
[i
].pdesc
= pbufs
+ ((info
->rbuf_count
+ i
) * sizeof(struct slgt_desc
));
3283 /* physical address of next descriptor */
3284 if (i
== info
->tbuf_count
- 1)
3285 info
->tbufs
[i
].next
= cpu_to_le32(pbufs
+ info
->rbuf_count
* sizeof(struct slgt_desc
));
3287 info
->tbufs
[i
].next
= cpu_to_le32(pbufs
+ ((info
->rbuf_count
+ i
+ 1) * sizeof(struct slgt_desc
)));
3293 static void free_desc(struct slgt_info
*info
)
3295 if (info
->bufs
!= NULL
) {
3296 pci_free_consistent(info
->pdev
, DESC_LIST_SIZE
, info
->bufs
, info
->bufs_dma_addr
);
3303 static int alloc_bufs(struct slgt_info
*info
, struct slgt_desc
*bufs
, int count
)
3306 for (i
=0; i
< count
; i
++) {
3307 if ((bufs
[i
].buf
= pci_alloc_consistent(info
->pdev
, DMABUFSIZE
, &bufs
[i
].buf_dma_addr
)) == NULL
)
3309 bufs
[i
].pbuf
= cpu_to_le32((unsigned int)bufs
[i
].buf_dma_addr
);
3314 static void free_bufs(struct slgt_info
*info
, struct slgt_desc
*bufs
, int count
)
3317 for (i
=0; i
< count
; i
++) {
3318 if (bufs
[i
].buf
== NULL
)
3320 pci_free_consistent(info
->pdev
, DMABUFSIZE
, bufs
[i
].buf
, bufs
[i
].buf_dma_addr
);
3325 static int alloc_dma_bufs(struct slgt_info
*info
)
3327 info
->rbuf_count
= 32;
3328 info
->tbuf_count
= 32;
3330 if (alloc_desc(info
) < 0 ||
3331 alloc_bufs(info
, info
->rbufs
, info
->rbuf_count
) < 0 ||
3332 alloc_bufs(info
, info
->tbufs
, info
->tbuf_count
) < 0 ||
3333 alloc_tmp_rbuf(info
) < 0) {
3334 DBGERR(("%s DMA buffer alloc fail\n", info
->device_name
));
3341 static void free_dma_bufs(struct slgt_info
*info
)
3344 free_bufs(info
, info
->rbufs
, info
->rbuf_count
);
3345 free_bufs(info
, info
->tbufs
, info
->tbuf_count
);
3348 free_tmp_rbuf(info
);
3351 static int claim_resources(struct slgt_info
*info
)
3353 if (request_mem_region(info
->phys_reg_addr
, SLGT_REG_SIZE
, "synclink_gt") == NULL
) {
3354 DBGERR(("%s reg addr conflict, addr=%08X\n",
3355 info
->device_name
, info
->phys_reg_addr
));
3356 info
->init_error
= DiagStatus_AddressConflict
;
3360 info
->reg_addr_requested
= true;
3362 info
->reg_addr
= ioremap_nocache(info
->phys_reg_addr
, SLGT_REG_SIZE
);
3363 if (!info
->reg_addr
) {
3364 DBGERR(("%s cant map device registers, addr=%08X\n",
3365 info
->device_name
, info
->phys_reg_addr
));
3366 info
->init_error
= DiagStatus_CantAssignPciResources
;
3372 release_resources(info
);
3376 static void release_resources(struct slgt_info
*info
)
3378 if (info
->irq_requested
) {
3379 free_irq(info
->irq_level
, info
);
3380 info
->irq_requested
= false;
3383 if (info
->reg_addr_requested
) {
3384 release_mem_region(info
->phys_reg_addr
, SLGT_REG_SIZE
);
3385 info
->reg_addr_requested
= false;
3388 if (info
->reg_addr
) {
3389 iounmap(info
->reg_addr
);
3390 info
->reg_addr
= NULL
;
3394 /* Add the specified device instance data structure to the
3395 * global linked list of devices and increment the device count.
3397 static void add_device(struct slgt_info
*info
)
3401 info
->next_device
= NULL
;
3402 info
->line
= slgt_device_count
;
3403 sprintf(info
->device_name
, "%s%d", tty_dev_prefix
, info
->line
);
3405 if (info
->line
< MAX_DEVICES
) {
3406 if (maxframe
[info
->line
])
3407 info
->max_frame_size
= maxframe
[info
->line
];
3408 info
->dosyncppp
= dosyncppp
[info
->line
];
3411 slgt_device_count
++;
3413 if (!slgt_device_list
)
3414 slgt_device_list
= info
;
3416 struct slgt_info
*current_dev
= slgt_device_list
;
3417 while(current_dev
->next_device
)
3418 current_dev
= current_dev
->next_device
;
3419 current_dev
->next_device
= info
;
3422 if (info
->max_frame_size
< 4096)
3423 info
->max_frame_size
= 4096;
3424 else if (info
->max_frame_size
> 65535)
3425 info
->max_frame_size
= 65535;
3427 switch(info
->pdev
->device
) {
3428 case SYNCLINK_GT_DEVICE_ID
:
3431 case SYNCLINK_GT2_DEVICE_ID
:
3434 case SYNCLINK_GT4_DEVICE_ID
:
3437 case SYNCLINK_AC_DEVICE_ID
:
3439 info
->params
.mode
= MGSL_MODE_ASYNC
;
3442 devstr
= "(unknown model)";
3444 printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
3445 devstr
, info
->device_name
, info
->phys_reg_addr
,
3446 info
->irq_level
, info
->max_frame_size
);
3448 #if SYNCLINK_GENERIC_HDLC
3454 * allocate device instance structure, return NULL on failure
3456 static struct slgt_info
*alloc_dev(int adapter_num
, int port_num
, struct pci_dev
*pdev
)
3458 struct slgt_info
*info
;
3460 info
= kzalloc(sizeof(struct slgt_info
), GFP_KERNEL
);
3463 DBGERR(("%s device alloc failed adapter=%d port=%d\n",
3464 driver_name
, adapter_num
, port_num
));
3466 tty_port_init(&info
->port
);
3467 info
->magic
= MGSL_MAGIC
;
3468 INIT_WORK(&info
->task
, bh_handler
);
3469 info
->max_frame_size
= 4096;
3470 info
->rbuf_fill_level
= DMABUFSIZE
;
3471 info
->port
.close_delay
= 5*HZ
/10;
3472 info
->port
.closing_wait
= 30*HZ
;
3473 init_waitqueue_head(&info
->status_event_wait_q
);
3474 init_waitqueue_head(&info
->event_wait_q
);
3475 spin_lock_init(&info
->netlock
);
3476 memcpy(&info
->params
,&default_params
,sizeof(MGSL_PARAMS
));
3477 info
->idle_mode
= HDLC_TXIDLE_FLAGS
;
3478 info
->adapter_num
= adapter_num
;
3479 info
->port_num
= port_num
;
3481 setup_timer(&info
->tx_timer
, tx_timeout
, (unsigned long)info
);
3482 setup_timer(&info
->rx_timer
, rx_timeout
, (unsigned long)info
);
3484 /* Copy configuration info to device instance data */
3486 info
->irq_level
= pdev
->irq
;
3487 info
->phys_reg_addr
= pci_resource_start(pdev
,0);
3489 info
->bus_type
= MGSL_BUS_TYPE_PCI
;
3490 info
->irq_flags
= IRQF_SHARED
;
3492 info
->init_error
= -1; /* assume error, set to 0 on successful init */
3498 static void device_init(int adapter_num
, struct pci_dev
*pdev
)
3500 struct slgt_info
*port_array
[SLGT_MAX_PORTS
];
3504 if (pdev
->device
== SYNCLINK_GT2_DEVICE_ID
)
3506 else if (pdev
->device
== SYNCLINK_GT4_DEVICE_ID
)
3509 /* allocate device instances for all ports */
3510 for (i
=0; i
< port_count
; ++i
) {
3511 port_array
[i
] = alloc_dev(adapter_num
, i
, pdev
);
3512 if (port_array
[i
] == NULL
) {
3513 for (--i
; i
>= 0; --i
)
3514 kfree(port_array
[i
]);
3519 /* give copy of port_array to all ports and add to device list */
3520 for (i
=0; i
< port_count
; ++i
) {
3521 memcpy(port_array
[i
]->port_array
, port_array
, sizeof(port_array
));
3522 add_device(port_array
[i
]);
3523 port_array
[i
]->port_count
= port_count
;
3524 spin_lock_init(&port_array
[i
]->lock
);
3527 /* Allocate and claim adapter resources */
3528 if (!claim_resources(port_array
[0])) {
3530 alloc_dma_bufs(port_array
[0]);
3532 /* copy resource information from first port to others */
3533 for (i
= 1; i
< port_count
; ++i
) {
3534 port_array
[i
]->lock
= port_array
[0]->lock
;
3535 port_array
[i
]->irq_level
= port_array
[0]->irq_level
;
3536 port_array
[i
]->reg_addr
= port_array
[0]->reg_addr
;
3537 alloc_dma_bufs(port_array
[i
]);
3540 if (request_irq(port_array
[0]->irq_level
,
3542 port_array
[0]->irq_flags
,
3543 port_array
[0]->device_name
,
3544 port_array
[0]) < 0) {
3545 DBGERR(("%s request_irq failed IRQ=%d\n",
3546 port_array
[0]->device_name
,
3547 port_array
[0]->irq_level
));
3549 port_array
[0]->irq_requested
= true;
3550 adapter_test(port_array
[0]);
3551 for (i
=1 ; i
< port_count
; i
++) {
3552 port_array
[i
]->init_error
= port_array
[0]->init_error
;
3553 port_array
[i
]->gpio_present
= port_array
[0]->gpio_present
;
3558 for (i
=0; i
< port_count
; ++i
)
3559 tty_register_device(serial_driver
, port_array
[i
]->line
, &(port_array
[i
]->pdev
->dev
));
3562 static int __devinit
init_one(struct pci_dev
*dev
,
3563 const struct pci_device_id
*ent
)
3565 if (pci_enable_device(dev
)) {
3566 printk("error enabling pci device %p\n", dev
);
3569 pci_set_master(dev
);
3570 device_init(slgt_device_count
, dev
);
3574 static void __devexit
remove_one(struct pci_dev
*dev
)
3578 static const struct tty_operations ops
= {
3582 .put_char
= put_char
,
3583 .flush_chars
= flush_chars
,
3584 .write_room
= write_room
,
3585 .chars_in_buffer
= chars_in_buffer
,
3586 .flush_buffer
= flush_buffer
,
3588 .compat_ioctl
= slgt_compat_ioctl
,
3589 .throttle
= throttle
,
3590 .unthrottle
= unthrottle
,
3591 .send_xchar
= send_xchar
,
3592 .break_ctl
= set_break
,
3593 .wait_until_sent
= wait_until_sent
,
3594 .read_proc
= read_proc
,
3595 .set_termios
= set_termios
,
3597 .start
= tx_release
,
3599 .tiocmget
= tiocmget
,
3600 .tiocmset
= tiocmset
,
3603 static void slgt_cleanup(void)
3606 struct slgt_info
*info
;
3607 struct slgt_info
*tmp
;
3609 printk("unload %s %s\n", driver_name
, driver_version
);
3611 if (serial_driver
) {
3612 for (info
=slgt_device_list
; info
!= NULL
; info
=info
->next_device
)
3613 tty_unregister_device(serial_driver
, info
->line
);
3614 if ((rc
= tty_unregister_driver(serial_driver
)))
3615 DBGERR(("tty_unregister_driver error=%d\n", rc
));
3616 put_tty_driver(serial_driver
);
3620 info
= slgt_device_list
;
3623 info
= info
->next_device
;
3626 /* release devices */
3627 info
= slgt_device_list
;
3629 #if SYNCLINK_GENERIC_HDLC
3632 free_dma_bufs(info
);
3633 free_tmp_rbuf(info
);
3634 if (info
->port_num
== 0)
3635 release_resources(info
);
3637 info
= info
->next_device
;
3642 pci_unregister_driver(&pci_driver
);
3646 * Driver initialization entry point.
3648 static int __init
slgt_init(void)
3652 printk("%s %s\n", driver_name
, driver_version
);
3654 serial_driver
= alloc_tty_driver(MAX_DEVICES
);
3655 if (!serial_driver
) {
3656 printk("%s can't allocate tty driver\n", driver_name
);
3660 /* Initialize the tty_driver structure */
3662 serial_driver
->owner
= THIS_MODULE
;
3663 serial_driver
->driver_name
= tty_driver_name
;
3664 serial_driver
->name
= tty_dev_prefix
;
3665 serial_driver
->major
= ttymajor
;
3666 serial_driver
->minor_start
= 64;
3667 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
3668 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
3669 serial_driver
->init_termios
= tty_std_termios
;
3670 serial_driver
->init_termios
.c_cflag
=
3671 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
3672 serial_driver
->init_termios
.c_ispeed
= 9600;
3673 serial_driver
->init_termios
.c_ospeed
= 9600;
3674 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
3675 tty_set_operations(serial_driver
, &ops
);
3676 if ((rc
= tty_register_driver(serial_driver
)) < 0) {
3677 DBGERR(("%s can't register serial driver\n", driver_name
));
3678 put_tty_driver(serial_driver
);
3679 serial_driver
= NULL
;
3683 printk("%s %s, tty major#%d\n",
3684 driver_name
, driver_version
,
3685 serial_driver
->major
);
3687 slgt_device_count
= 0;
3688 if ((rc
= pci_register_driver(&pci_driver
)) < 0) {
3689 printk("%s pci_register_driver error=%d\n", driver_name
, rc
);
3692 pci_registered
= true;
3694 if (!slgt_device_list
)
3695 printk("%s no devices found\n",driver_name
);
3704 static void __exit
slgt_exit(void)
3709 module_init(slgt_init
);
3710 module_exit(slgt_exit
);
3713 * register access routines
3716 #define CALC_REGADDR() \
3717 unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
3719 reg_addr += (info->port_num) * 32;
3721 static __u8
rd_reg8(struct slgt_info
*info
, unsigned int addr
)
3724 return readb((void __iomem
*)reg_addr
);
3727 static void wr_reg8(struct slgt_info
*info
, unsigned int addr
, __u8 value
)
3730 writeb(value
, (void __iomem
*)reg_addr
);
3733 static __u16
rd_reg16(struct slgt_info
*info
, unsigned int addr
)
3736 return readw((void __iomem
*)reg_addr
);
3739 static void wr_reg16(struct slgt_info
*info
, unsigned int addr
, __u16 value
)
3742 writew(value
, (void __iomem
*)reg_addr
);
3745 static __u32
rd_reg32(struct slgt_info
*info
, unsigned int addr
)
3748 return readl((void __iomem
*)reg_addr
);
3751 static void wr_reg32(struct slgt_info
*info
, unsigned int addr
, __u32 value
)
3754 writel(value
, (void __iomem
*)reg_addr
);
3757 static void rdma_reset(struct slgt_info
*info
)
3762 wr_reg32(info
, RDCSR
, BIT1
);
3764 /* wait for enable bit cleared */
3765 for(i
=0 ; i
< 1000 ; i
++)
3766 if (!(rd_reg32(info
, RDCSR
) & BIT0
))
3770 static void tdma_reset(struct slgt_info
*info
)
3775 wr_reg32(info
, TDCSR
, BIT1
);
3777 /* wait for enable bit cleared */
3778 for(i
=0 ; i
< 1000 ; i
++)
3779 if (!(rd_reg32(info
, TDCSR
) & BIT0
))
3784 * enable internal loopback
3785 * TxCLK and RxCLK are generated from BRG
3786 * and TxD is looped back to RxD internally.
3788 static void enable_loopback(struct slgt_info
*info
)
3790 /* SCR (serial control) BIT2=looopback enable */
3791 wr_reg16(info
, SCR
, (unsigned short)(rd_reg16(info
, SCR
) | BIT2
));
3793 if (info
->params
.mode
!= MGSL_MODE_ASYNC
) {
3794 /* CCR (clock control)
3795 * 07..05 tx clock source (010 = BRG)
3796 * 04..02 rx clock source (010 = BRG)
3797 * 01 auxclk enable (0 = disable)
3798 * 00 BRG enable (1 = enable)
3802 wr_reg8(info
, CCR
, 0x49);
3804 /* set speed if available, otherwise use default */
3805 if (info
->params
.clock_speed
)
3806 set_rate(info
, info
->params
.clock_speed
);
3808 set_rate(info
, 3686400);
3813 * set baud rate generator to specified rate
3815 static void set_rate(struct slgt_info
*info
, u32 rate
)
3818 static unsigned int osc
= 14745600;
3820 /* div = osc/rate - 1
3822 * Round div up if osc/rate is not integer to
3823 * force to next slowest rate.
3828 if (!(osc
% rate
) && div
)
3830 wr_reg16(info
, BDR
, (unsigned short)div
);
3834 static void rx_stop(struct slgt_info
*info
)
3838 /* disable and reset 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 */
3843 slgt_irq_off(info
, IRQ_RXOVER
+ IRQ_RXDATA
+ IRQ_RXIDLE
);
3845 /* clear pending rx interrupts */
3846 wr_reg16(info
, SSR
, IRQ_RXIDLE
+ IRQ_RXOVER
);
3850 info
->rx_enabled
= false;
3851 info
->rx_restart
= false;
3854 static void rx_start(struct slgt_info
*info
)
3858 slgt_irq_off(info
, IRQ_RXOVER
+ IRQ_RXDATA
);
3860 /* clear pending rx overrun IRQ */
3861 wr_reg16(info
, SSR
, IRQ_RXOVER
);
3863 /* reset and disable receiver */
3864 val
= rd_reg16(info
, RCR
) & ~BIT1
; /* clear enable bit */
3865 wr_reg16(info
, RCR
, (unsigned short)(val
| BIT2
)); /* set reset bit */
3866 wr_reg16(info
, RCR
, val
); /* clear reset bit */
3871 /* set 1st descriptor address */
3872 wr_reg32(info
, RDDAR
, info
->rbufs
[0].pdesc
);
3874 if (info
->params
.mode
!= MGSL_MODE_ASYNC
) {
3875 /* enable rx DMA and DMA interrupt */
3876 wr_reg32(info
, RDCSR
, (BIT2
+ BIT0
));
3878 /* enable saving of rx status, rx DMA and DMA interrupt */
3879 wr_reg32(info
, RDCSR
, (BIT6
+ BIT2
+ BIT0
));
3882 slgt_irq_on(info
, IRQ_RXOVER
);
3884 /* enable receiver */
3885 wr_reg16(info
, RCR
, (unsigned short)(rd_reg16(info
, RCR
) | BIT1
));
3887 info
->rx_restart
= false;
3888 info
->rx_enabled
= true;
3891 static void tx_start(struct slgt_info
*info
)
3893 if (!info
->tx_enabled
) {
3895 (unsigned short)((rd_reg16(info
, TCR
) | BIT1
) & ~BIT2
));
3896 info
->tx_enabled
= true;
3899 if (info
->tx_count
) {
3900 info
->drop_rts_on_tx_done
= false;
3902 if (info
->params
.mode
!= MGSL_MODE_ASYNC
) {
3903 if (info
->params
.flags
& HDLC_FLAG_AUTO_RTS
) {
3905 if (!(info
->signals
& SerialSignal_RTS
)) {
3906 info
->signals
|= SerialSignal_RTS
;
3908 info
->drop_rts_on_tx_done
= true;
3912 slgt_irq_off(info
, IRQ_TXDATA
);
3913 slgt_irq_on(info
, IRQ_TXUNDER
+ IRQ_TXIDLE
);
3914 /* clear tx idle and underrun status bits */
3915 wr_reg16(info
, SSR
, (unsigned short)(IRQ_TXIDLE
+ IRQ_TXUNDER
));
3916 if (info
->params
.mode
== MGSL_MODE_HDLC
)
3917 mod_timer(&info
->tx_timer
, jiffies
+
3918 msecs_to_jiffies(5000));
3920 slgt_irq_off(info
, IRQ_TXDATA
);
3921 slgt_irq_on(info
, IRQ_TXIDLE
);
3922 /* clear tx idle status bit */
3923 wr_reg16(info
, SSR
, IRQ_TXIDLE
);
3926 info
->tx_active
= true;
3931 * start transmit DMA if inactive and there are unsent buffers
3933 static void tdma_start(struct slgt_info
*info
)
3937 if (rd_reg32(info
, TDCSR
) & BIT0
)
3940 /* transmit DMA inactive, check for unsent buffers */
3941 i
= info
->tbuf_start
;
3942 while (!desc_count(info
->tbufs
[i
])) {
3943 if (++i
== info
->tbuf_count
)
3945 if (i
== info
->tbuf_current
)
3948 info
->tbuf_start
= i
;
3950 /* there are unsent buffers, start transmit DMA */
3952 /* reset needed if previous error condition */
3955 /* set 1st descriptor address */
3956 wr_reg32(info
, TDDAR
, info
->tbufs
[info
->tbuf_start
].pdesc
);
3957 wr_reg32(info
, TDCSR
, BIT2
+ BIT0
); /* IRQ + DMA enable */
3960 static void tx_stop(struct slgt_info
*info
)
3964 del_timer(&info
->tx_timer
);
3968 /* reset and disable transmitter */
3969 val
= rd_reg16(info
, TCR
) & ~BIT1
; /* clear enable bit */
3970 wr_reg16(info
, TCR
, (unsigned short)(val
| BIT2
)); /* set reset bit */
3972 slgt_irq_off(info
, IRQ_TXDATA
+ IRQ_TXIDLE
+ IRQ_TXUNDER
);
3974 /* clear tx idle and underrun status bit */
3975 wr_reg16(info
, SSR
, (unsigned short)(IRQ_TXIDLE
+ IRQ_TXUNDER
));
3979 info
->tx_enabled
= false;
3980 info
->tx_active
= false;
3983 static void reset_port(struct slgt_info
*info
)
3985 if (!info
->reg_addr
)
3991 info
->signals
&= ~(SerialSignal_DTR
+ SerialSignal_RTS
);
3994 slgt_irq_off(info
, IRQ_ALL
| IRQ_MASTER
);
3997 static void reset_adapter(struct slgt_info
*info
)
4000 for (i
=0; i
< info
->port_count
; ++i
) {
4001 if (info
->port_array
[i
])
4002 reset_port(info
->port_array
[i
]);
4006 static void async_mode(struct slgt_info
*info
)
4010 slgt_irq_off(info
, IRQ_ALL
| IRQ_MASTER
);
4016 * 15..13 mode, 010=async
4017 * 12..10 encoding, 000=NRZ
4019 * 08 1=odd parity, 0=even parity
4020 * 07 1=RTS driver control
4022 * 05..04 character length
4027 * 03 0=1 stop bit, 1=2 stop bits
4030 * 00 auto-CTS enable
4034 if (info
->if_mode
& MGSL_INTERFACE_RTS_EN
)
4037 if (info
->params
.parity
!= ASYNC_PARITY_NONE
) {
4039 if (info
->params
.parity
== ASYNC_PARITY_ODD
)
4043 switch (info
->params
.data_bits
)
4045 case 6: val
|= BIT4
; break;
4046 case 7: val
|= BIT5
; break;
4047 case 8: val
|= BIT5
+ BIT4
; break;
4050 if (info
->params
.stop_bits
!= 1)
4053 if (info
->params
.flags
& HDLC_FLAG_AUTO_CTS
)
4056 wr_reg16(info
, TCR
, val
);
4060 * 15..13 mode, 010=async
4061 * 12..10 encoding, 000=NRZ
4063 * 08 1=odd parity, 0=even parity
4064 * 07..06 reserved, must be 0
4065 * 05..04 character length
4070 * 03 reserved, must be zero
4073 * 00 auto-DCD enable
4077 if (info
->params
.parity
!= ASYNC_PARITY_NONE
) {
4079 if (info
->params
.parity
== ASYNC_PARITY_ODD
)
4083 switch (info
->params
.data_bits
)
4085 case 6: val
|= BIT4
; break;
4086 case 7: val
|= BIT5
; break;
4087 case 8: val
|= BIT5
+ BIT4
; break;
4090 if (info
->params
.flags
& HDLC_FLAG_AUTO_DCD
)
4093 wr_reg16(info
, RCR
, val
);
4095 /* CCR (clock control)
4097 * 07..05 011 = tx clock source is BRG/16
4098 * 04..02 010 = rx clock source is BRG
4099 * 01 0 = auxclk disabled
4100 * 00 1 = BRG enabled
4104 wr_reg8(info
, CCR
, 0x69);
4108 /* SCR (serial control)
4110 * 15 1=tx req on FIFO half empty
4111 * 14 1=rx req on FIFO half full
4112 * 13 tx data IRQ enable
4113 * 12 tx idle IRQ enable
4114 * 11 rx break on IRQ enable
4115 * 10 rx data IRQ enable
4116 * 09 rx break off IRQ enable
4117 * 08 overrun IRQ enable
4122 * 03 reserved, must be zero
4123 * 02 1=txd->rxd internal loopback enable
4124 * 01 reserved, must be zero
4125 * 00 1=master IRQ enable
4127 val
= BIT15
+ BIT14
+ BIT0
;
4128 wr_reg16(info
, SCR
, val
);
4130 slgt_irq_on(info
, IRQ_RXBREAK
| IRQ_RXOVER
);
4132 set_rate(info
, info
->params
.data_rate
* 16);
4134 if (info
->params
.loopback
)
4135 enable_loopback(info
);
4138 static void sync_mode(struct slgt_info
*info
)
4142 slgt_irq_off(info
, IRQ_ALL
| IRQ_MASTER
);
4148 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4152 * 07 1=RTS driver control
4153 * 06 preamble enable
4154 * 05..04 preamble length
4155 * 03 share open/close flag
4158 * 00 auto-CTS enable
4162 switch(info
->params
.mode
) {
4163 case MGSL_MODE_MONOSYNC
: val
|= BIT14
+ BIT13
; break;
4164 case MGSL_MODE_BISYNC
: val
|= BIT15
; break;
4165 case MGSL_MODE_RAW
: val
|= BIT13
; break;
4167 if (info
->if_mode
& MGSL_INTERFACE_RTS_EN
)
4170 switch(info
->params
.encoding
)
4172 case HDLC_ENCODING_NRZB
: val
|= BIT10
; break;
4173 case HDLC_ENCODING_NRZI_MARK
: val
|= BIT11
; break;
4174 case HDLC_ENCODING_NRZI
: val
|= BIT11
+ BIT10
; break;
4175 case HDLC_ENCODING_BIPHASE_MARK
: val
|= BIT12
; break;
4176 case HDLC_ENCODING_BIPHASE_SPACE
: val
|= BIT12
+ BIT10
; break;
4177 case HDLC_ENCODING_BIPHASE_LEVEL
: val
|= BIT12
+ BIT11
; break;
4178 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL
: val
|= BIT12
+ BIT11
+ BIT10
; break;
4181 switch (info
->params
.crc_type
& HDLC_CRC_MASK
)
4183 case HDLC_CRC_16_CCITT
: val
|= BIT9
; break;
4184 case HDLC_CRC_32_CCITT
: val
|= BIT9
+ BIT8
; break;
4187 if (info
->params
.preamble
!= HDLC_PREAMBLE_PATTERN_NONE
)
4190 switch (info
->params
.preamble_length
)
4192 case HDLC_PREAMBLE_LENGTH_16BITS
: val
|= BIT5
; break;
4193 case HDLC_PREAMBLE_LENGTH_32BITS
: val
|= BIT4
; break;
4194 case HDLC_PREAMBLE_LENGTH_64BITS
: val
|= BIT5
+ BIT4
; break;
4197 if (info
->params
.flags
& HDLC_FLAG_AUTO_CTS
)
4200 wr_reg16(info
, TCR
, val
);
4202 /* TPR (transmit preamble) */
4204 switch (info
->params
.preamble
)
4206 case HDLC_PREAMBLE_PATTERN_FLAGS
: val
= 0x7e; break;
4207 case HDLC_PREAMBLE_PATTERN_ONES
: val
= 0xff; break;
4208 case HDLC_PREAMBLE_PATTERN_ZEROS
: val
= 0x00; break;
4209 case HDLC_PREAMBLE_PATTERN_10
: val
= 0x55; break;
4210 case HDLC_PREAMBLE_PATTERN_01
: val
= 0xaa; break;
4211 default: val
= 0x7e; break;
4213 wr_reg8(info
, TPR
, (unsigned char)val
);
4217 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4221 * 07..03 reserved, must be 0
4224 * 00 auto-DCD enable
4228 switch(info
->params
.mode
) {
4229 case MGSL_MODE_MONOSYNC
: val
|= BIT14
+ BIT13
; break;
4230 case MGSL_MODE_BISYNC
: val
|= BIT15
; break;
4231 case MGSL_MODE_RAW
: val
|= BIT13
; break;
4234 switch(info
->params
.encoding
)
4236 case HDLC_ENCODING_NRZB
: val
|= BIT10
; break;
4237 case HDLC_ENCODING_NRZI_MARK
: val
|= BIT11
; break;
4238 case HDLC_ENCODING_NRZI
: val
|= BIT11
+ BIT10
; break;
4239 case HDLC_ENCODING_BIPHASE_MARK
: val
|= BIT12
; break;
4240 case HDLC_ENCODING_BIPHASE_SPACE
: val
|= BIT12
+ BIT10
; break;
4241 case HDLC_ENCODING_BIPHASE_LEVEL
: val
|= BIT12
+ BIT11
; break;
4242 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL
: val
|= BIT12
+ BIT11
+ BIT10
; break;
4245 switch (info
->params
.crc_type
& HDLC_CRC_MASK
)
4247 case HDLC_CRC_16_CCITT
: val
|= BIT9
; break;
4248 case HDLC_CRC_32_CCITT
: val
|= BIT9
+ BIT8
; break;
4251 if (info
->params
.flags
& HDLC_FLAG_AUTO_DCD
)
4254 wr_reg16(info
, RCR
, val
);
4256 /* CCR (clock control)
4258 * 07..05 tx clock source
4259 * 04..02 rx clock source
4265 if (info
->params
.flags
& HDLC_FLAG_TXC_BRG
)
4267 // when RxC source is DPLL, BRG generates 16X DPLL
4268 // reference clock, so take TxC from BRG/16 to get
4269 // transmit clock at actual data rate
4270 if (info
->params
.flags
& HDLC_FLAG_RXC_DPLL
)
4271 val
|= BIT6
+ BIT5
; /* 011, txclk = BRG/16 */
4273 val
|= BIT6
; /* 010, txclk = BRG */
4275 else if (info
->params
.flags
& HDLC_FLAG_TXC_DPLL
)
4276 val
|= BIT7
; /* 100, txclk = DPLL Input */
4277 else if (info
->params
.flags
& HDLC_FLAG_TXC_RXCPIN
)
4278 val
|= BIT5
; /* 001, txclk = RXC Input */
4280 if (info
->params
.flags
& HDLC_FLAG_RXC_BRG
)
4281 val
|= BIT3
; /* 010, rxclk = BRG */
4282 else if (info
->params
.flags
& HDLC_FLAG_RXC_DPLL
)
4283 val
|= BIT4
; /* 100, rxclk = DPLL */
4284 else if (info
->params
.flags
& HDLC_FLAG_RXC_TXCPIN
)
4285 val
|= BIT2
; /* 001, rxclk = TXC Input */
4287 if (info
->params
.clock_speed
)
4290 wr_reg8(info
, CCR
, (unsigned char)val
);
4292 if (info
->params
.flags
& (HDLC_FLAG_TXC_DPLL
+ HDLC_FLAG_RXC_DPLL
))
4294 // program DPLL mode
4295 switch(info
->params
.encoding
)
4297 case HDLC_ENCODING_BIPHASE_MARK
:
4298 case HDLC_ENCODING_BIPHASE_SPACE
:
4300 case HDLC_ENCODING_BIPHASE_LEVEL
:
4301 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL
:
4302 val
= BIT7
+ BIT6
; break;
4303 default: val
= BIT6
; // NRZ encodings
4305 wr_reg16(info
, RCR
, (unsigned short)(rd_reg16(info
, RCR
) | val
));
4307 // DPLL requires a 16X reference clock from BRG
4308 set_rate(info
, info
->params
.clock_speed
* 16);
4311 set_rate(info
, info
->params
.clock_speed
);
4317 /* SCR (serial control)
4319 * 15 1=tx req on FIFO half empty
4320 * 14 1=rx req on FIFO half full
4321 * 13 tx data IRQ enable
4322 * 12 tx idle IRQ enable
4323 * 11 underrun IRQ enable
4324 * 10 rx data IRQ enable
4325 * 09 rx idle IRQ enable
4326 * 08 overrun IRQ enable
4331 * 03 reserved, must be zero
4332 * 02 1=txd->rxd internal loopback enable
4333 * 01 reserved, must be zero
4334 * 00 1=master IRQ enable
4336 wr_reg16(info
, SCR
, BIT15
+ BIT14
+ BIT0
);
4338 if (info
->params
.loopback
)
4339 enable_loopback(info
);
4343 * set transmit idle mode
4345 static void tx_set_idle(struct slgt_info
*info
)
4350 /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
4351 * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
4353 tcr
= rd_reg16(info
, TCR
);
4354 if (info
->idle_mode
& HDLC_TXIDLE_CUSTOM_16
) {
4355 /* disable preamble, set idle size to 16 bits */
4356 tcr
= (tcr
& ~(BIT6
+ BIT5
)) | BIT4
;
4357 /* MSB of 16 bit idle specified in tx preamble register (TPR) */
4358 wr_reg8(info
, TPR
, (unsigned char)((info
->idle_mode
>> 8) & 0xff));
4359 } else if (!(tcr
& BIT6
)) {
4360 /* preamble is disabled, set idle size to 8 bits */
4361 tcr
&= ~(BIT5
+ BIT4
);
4363 wr_reg16(info
, TCR
, tcr
);
4365 if (info
->idle_mode
& (HDLC_TXIDLE_CUSTOM_8
| HDLC_TXIDLE_CUSTOM_16
)) {
4366 /* LSB of custom tx idle specified in tx idle register */
4367 val
= (unsigned char)(info
->idle_mode
& 0xff);
4369 /* standard 8 bit idle patterns */
4370 switch(info
->idle_mode
)
4372 case HDLC_TXIDLE_FLAGS
: val
= 0x7e; break;
4373 case HDLC_TXIDLE_ALT_ZEROS_ONES
:
4374 case HDLC_TXIDLE_ALT_MARK_SPACE
: val
= 0xaa; break;
4375 case HDLC_TXIDLE_ZEROS
:
4376 case HDLC_TXIDLE_SPACE
: val
= 0x00; break;
4377 default: val
= 0xff;
4381 wr_reg8(info
, TIR
, val
);
4385 * get state of V24 status (input) signals
4387 static void get_signals(struct slgt_info
*info
)
4389 unsigned short status
= rd_reg16(info
, SSR
);
4391 /* clear all serial signals except DTR and RTS */
4392 info
->signals
&= SerialSignal_DTR
+ SerialSignal_RTS
;
4395 info
->signals
|= SerialSignal_DSR
;
4397 info
->signals
|= SerialSignal_CTS
;
4399 info
->signals
|= SerialSignal_DCD
;
4401 info
->signals
|= SerialSignal_RI
;
4405 * set V.24 Control Register based on current configuration
4407 static void msc_set_vcr(struct slgt_info
*info
)
4409 unsigned char val
= 0;
4411 /* VCR (V.24 control)
4413 * 07..04 serial IF select
4420 switch(info
->if_mode
& MGSL_INTERFACE_MASK
)
4422 case MGSL_INTERFACE_RS232
:
4423 val
|= BIT5
; /* 0010 */
4425 case MGSL_INTERFACE_V35
:
4426 val
|= BIT7
+ BIT6
+ BIT5
; /* 1110 */
4428 case MGSL_INTERFACE_RS422
:
4429 val
|= BIT6
; /* 0100 */
4433 if (info
->if_mode
& MGSL_INTERFACE_MSB_FIRST
)
4435 if (info
->signals
& SerialSignal_DTR
)
4437 if (info
->signals
& SerialSignal_RTS
)
4439 if (info
->if_mode
& MGSL_INTERFACE_LL
)
4441 if (info
->if_mode
& MGSL_INTERFACE_RL
)
4443 wr_reg8(info
, VCR
, val
);
4447 * set state of V24 control (output) signals
4449 static void set_signals(struct slgt_info
*info
)
4451 unsigned char val
= rd_reg8(info
, VCR
);
4452 if (info
->signals
& SerialSignal_DTR
)
4456 if (info
->signals
& SerialSignal_RTS
)
4460 wr_reg8(info
, VCR
, val
);
4464 * free range of receive DMA buffers (i to last)
4466 static void free_rbufs(struct slgt_info
*info
, unsigned int i
, unsigned int last
)
4471 /* reset current buffer for reuse */
4472 info
->rbufs
[i
].status
= 0;
4473 set_desc_count(info
->rbufs
[i
], info
->rbuf_fill_level
);
4476 if (++i
== info
->rbuf_count
)
4479 info
->rbuf_current
= i
;
4483 * mark all receive DMA buffers as free
4485 static void reset_rbufs(struct slgt_info
*info
)
4487 free_rbufs(info
, 0, info
->rbuf_count
- 1);
4491 * pass receive HDLC frame to upper layer
4493 * return true if frame available, otherwise false
4495 static bool rx_get_frame(struct slgt_info
*info
)
4497 unsigned int start
, end
;
4498 unsigned short status
;
4499 unsigned int framesize
= 0;
4500 unsigned long flags
;
4501 struct tty_struct
*tty
= info
->port
.tty
;
4502 unsigned char addr_field
= 0xff;
4503 unsigned int crc_size
= 0;
4505 switch (info
->params
.crc_type
& HDLC_CRC_MASK
) {
4506 case HDLC_CRC_16_CCITT
: crc_size
= 2; break;
4507 case HDLC_CRC_32_CCITT
: crc_size
= 4; break;
4514 start
= end
= info
->rbuf_current
;
4517 if (!desc_complete(info
->rbufs
[end
]))
4520 if (framesize
== 0 && info
->params
.addr_filter
!= 0xff)
4521 addr_field
= info
->rbufs
[end
].buf
[0];
4523 framesize
+= desc_count(info
->rbufs
[end
]);
4525 if (desc_eof(info
->rbufs
[end
]))
4528 if (++end
== info
->rbuf_count
)
4531 if (end
== info
->rbuf_current
) {
4532 if (info
->rx_enabled
){
4533 spin_lock_irqsave(&info
->lock
,flags
);
4535 spin_unlock_irqrestore(&info
->lock
,flags
);
4543 * 15 buffer complete
4546 * 02 eof (end of frame)
4550 status
= desc_status(info
->rbufs
[end
]);
4552 /* ignore CRC bit if not using CRC (bit is undefined) */
4553 if ((info
->params
.crc_type
& HDLC_CRC_MASK
) == HDLC_CRC_NONE
)
4556 if (framesize
== 0 ||
4557 (addr_field
!= 0xff && addr_field
!= info
->params
.addr_filter
)) {
4558 free_rbufs(info
, start
, end
);
4562 if (framesize
< (2 + crc_size
) || status
& BIT0
) {
4563 info
->icount
.rxshort
++;
4565 } else if (status
& BIT1
) {
4566 info
->icount
.rxcrc
++;
4567 if (!(info
->params
.crc_type
& HDLC_CRC_RETURN_EX
))
4571 #if SYNCLINK_GENERIC_HDLC
4572 if (framesize
== 0) {
4573 info
->netdev
->stats
.rx_errors
++;
4574 info
->netdev
->stats
.rx_frame_errors
++;
4578 DBGBH(("%s rx frame status=%04X size=%d\n",
4579 info
->device_name
, status
, framesize
));
4580 DBGDATA(info
, info
->rbufs
[start
].buf
, min_t(int, framesize
, info
->rbuf_fill_level
), "rx");
4583 if (!(info
->params
.crc_type
& HDLC_CRC_RETURN_EX
)) {
4584 framesize
-= crc_size
;
4588 if (framesize
> info
->max_frame_size
+ crc_size
)
4589 info
->icount
.rxlong
++;
4591 /* copy dma buffer(s) to contiguous temp buffer */
4592 int copy_count
= framesize
;
4594 unsigned char *p
= info
->tmp_rbuf
;
4595 info
->tmp_rbuf_count
= framesize
;
4597 info
->icount
.rxok
++;
4600 int partial_count
= min_t(int, copy_count
, info
->rbuf_fill_level
);
4601 memcpy(p
, info
->rbufs
[i
].buf
, partial_count
);
4603 copy_count
-= partial_count
;
4604 if (++i
== info
->rbuf_count
)
4608 if (info
->params
.crc_type
& HDLC_CRC_RETURN_EX
) {
4609 *p
= (status
& BIT1
) ? RX_CRC_ERROR
: RX_OK
;
4613 #if SYNCLINK_GENERIC_HDLC
4615 hdlcdev_rx(info
,info
->tmp_rbuf
, framesize
);
4618 ldisc_receive_buf(tty
, info
->tmp_rbuf
, info
->flag_buf
, framesize
);
4621 free_rbufs(info
, start
, end
);
4629 * pass receive buffer (RAW synchronous mode) to tty layer
4630 * return true if buffer available, otherwise false
4632 static bool rx_get_buf(struct slgt_info
*info
)
4634 unsigned int i
= info
->rbuf_current
;
4637 if (!desc_complete(info
->rbufs
[i
]))
4639 count
= desc_count(info
->rbufs
[i
]);
4640 switch(info
->params
.mode
) {
4641 case MGSL_MODE_MONOSYNC
:
4642 case MGSL_MODE_BISYNC
:
4643 /* ignore residue in byte synchronous modes */
4644 if (desc_residue(info
->rbufs
[i
]))
4648 DBGDATA(info
, info
->rbufs
[i
].buf
, count
, "rx");
4649 DBGINFO(("rx_get_buf size=%d\n", count
));
4651 ldisc_receive_buf(info
->port
.tty
, info
->rbufs
[i
].buf
,
4652 info
->flag_buf
, count
);
4653 free_rbufs(info
, i
, i
);
4657 static void reset_tbufs(struct slgt_info
*info
)
4660 info
->tbuf_current
= 0;
4661 for (i
=0 ; i
< info
->tbuf_count
; i
++) {
4662 info
->tbufs
[i
].status
= 0;
4663 info
->tbufs
[i
].count
= 0;
4668 * return number of free transmit DMA buffers
4670 static unsigned int free_tbuf_count(struct slgt_info
*info
)
4672 unsigned int count
= 0;
4673 unsigned int i
= info
->tbuf_current
;
4677 if (desc_count(info
->tbufs
[i
]))
4678 break; /* buffer in use */
4680 if (++i
== info
->tbuf_count
)
4682 } while (i
!= info
->tbuf_current
);
4684 /* if tx DMA active, last zero count buffer is in use */
4685 if (count
&& (rd_reg32(info
, TDCSR
) & BIT0
))
4692 * return number of bytes in unsent transmit DMA buffers
4693 * and the serial controller tx FIFO
4695 static unsigned int tbuf_bytes(struct slgt_info
*info
)
4697 unsigned int total_count
= 0;
4698 unsigned int i
= info
->tbuf_current
;
4699 unsigned int reg_value
;
4701 unsigned int active_buf_count
= 0;
4704 * Add descriptor counts for all tx DMA buffers.
4705 * If count is zero (cleared by DMA controller after read),
4706 * the buffer is complete or is actively being read from.
4708 * Record buf_count of last buffer with zero count starting
4709 * from current ring position. buf_count is mirror
4710 * copy of count and is not cleared by serial controller.
4711 * If DMA controller is active, that buffer is actively
4712 * being read so add to total.
4715 count
= desc_count(info
->tbufs
[i
]);
4717 total_count
+= count
;
4718 else if (!total_count
)
4719 active_buf_count
= info
->tbufs
[i
].buf_count
;
4720 if (++i
== info
->tbuf_count
)
4722 } while (i
!= info
->tbuf_current
);
4724 /* read tx DMA status register */
4725 reg_value
= rd_reg32(info
, TDCSR
);
4727 /* if tx DMA active, last zero count buffer is in use */
4728 if (reg_value
& BIT0
)
4729 total_count
+= active_buf_count
;
4731 /* add tx FIFO count = reg_value[15..8] */
4732 total_count
+= (reg_value
>> 8) & 0xff;
4734 /* if transmitter active add one byte for shift register */
4735 if (info
->tx_active
)
4742 * load transmit DMA buffer(s) with data
4744 static void tx_load(struct slgt_info
*info
, const char *buf
, unsigned int size
)
4746 unsigned short count
;
4748 struct slgt_desc
*d
;
4753 DBGDATA(info
, buf
, size
, "tx");
4755 info
->tbuf_start
= i
= info
->tbuf_current
;
4758 d
= &info
->tbufs
[i
];
4759 if (++i
== info
->tbuf_count
)
4762 count
= (unsigned short)((size
> DMABUFSIZE
) ? DMABUFSIZE
: size
);
4763 memcpy(d
->buf
, buf
, count
);
4769 * set EOF bit for last buffer of HDLC frame or
4770 * for every buffer in raw mode
4772 if ((!size
&& info
->params
.mode
== MGSL_MODE_HDLC
) ||
4773 info
->params
.mode
== MGSL_MODE_RAW
)
4774 set_desc_eof(*d
, 1);
4776 set_desc_eof(*d
, 0);
4778 set_desc_count(*d
, count
);
4779 d
->buf_count
= count
;
4782 info
->tbuf_current
= i
;
4785 static int register_test(struct slgt_info
*info
)
4787 static unsigned short patterns
[] =
4788 {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
4789 static unsigned int count
= sizeof(patterns
)/sizeof(patterns
[0]);
4793 for (i
=0 ; i
< count
; i
++) {
4794 wr_reg16(info
, TIR
, patterns
[i
]);
4795 wr_reg16(info
, BDR
, patterns
[(i
+1)%count
]);
4796 if ((rd_reg16(info
, TIR
) != patterns
[i
]) ||
4797 (rd_reg16(info
, BDR
) != patterns
[(i
+1)%count
])) {
4802 info
->gpio_present
= (rd_reg32(info
, JCR
) & BIT5
) ? 1 : 0;
4803 info
->init_error
= rc
? 0 : DiagStatus_AddressFailure
;
4807 static int irq_test(struct slgt_info
*info
)
4809 unsigned long timeout
;
4810 unsigned long flags
;
4811 struct tty_struct
*oldtty
= info
->port
.tty
;
4812 u32 speed
= info
->params
.data_rate
;
4814 info
->params
.data_rate
= 921600;
4815 info
->port
.tty
= NULL
;
4817 spin_lock_irqsave(&info
->lock
, flags
);
4819 slgt_irq_on(info
, IRQ_TXIDLE
);
4821 /* enable transmitter */
4823 (unsigned short)(rd_reg16(info
, TCR
) | BIT1
));
4825 /* write one byte and wait for tx idle */
4826 wr_reg16(info
, TDR
, 0);
4828 /* assume failure */
4829 info
->init_error
= DiagStatus_IrqFailure
;
4830 info
->irq_occurred
= false;
4832 spin_unlock_irqrestore(&info
->lock
, flags
);
4835 while(timeout
-- && !info
->irq_occurred
)
4836 msleep_interruptible(10);
4838 spin_lock_irqsave(&info
->lock
,flags
);
4840 spin_unlock_irqrestore(&info
->lock
,flags
);
4842 info
->params
.data_rate
= speed
;
4843 info
->port
.tty
= oldtty
;
4845 info
->init_error
= info
->irq_occurred
? 0 : DiagStatus_IrqFailure
;
4846 return info
->irq_occurred
? 0 : -ENODEV
;
4849 static int loopback_test_rx(struct slgt_info
*info
)
4851 unsigned char *src
, *dest
;
4854 if (desc_complete(info
->rbufs
[0])) {
4855 count
= desc_count(info
->rbufs
[0]);
4856 src
= info
->rbufs
[0].buf
;
4857 dest
= info
->tmp_rbuf
;
4859 for( ; count
; count
-=2, src
+=2) {
4860 /* src=data byte (src+1)=status byte */
4861 if (!(*(src
+1) & (BIT9
+ BIT8
))) {
4864 info
->tmp_rbuf_count
++;
4867 DBGDATA(info
, info
->tmp_rbuf
, info
->tmp_rbuf_count
, "rx");
4873 static int loopback_test(struct slgt_info
*info
)
4875 #define TESTFRAMESIZE 20
4877 unsigned long timeout
;
4878 u16 count
= TESTFRAMESIZE
;
4879 unsigned char buf
[TESTFRAMESIZE
];
4881 unsigned long flags
;
4883 struct tty_struct
*oldtty
= info
->port
.tty
;
4886 memcpy(¶ms
, &info
->params
, sizeof(params
));
4888 info
->params
.mode
= MGSL_MODE_ASYNC
;
4889 info
->params
.data_rate
= 921600;
4890 info
->params
.loopback
= 1;
4891 info
->port
.tty
= NULL
;
4893 /* build and send transmit frame */
4894 for (count
= 0; count
< TESTFRAMESIZE
; ++count
)
4895 buf
[count
] = (unsigned char)count
;
4897 info
->tmp_rbuf_count
= 0;
4898 memset(info
->tmp_rbuf
, 0, TESTFRAMESIZE
);
4900 /* program hardware for HDLC and enabled receiver */
4901 spin_lock_irqsave(&info
->lock
,flags
);
4904 info
->tx_count
= count
;
4905 tx_load(info
, buf
, count
);
4907 spin_unlock_irqrestore(&info
->lock
, flags
);
4909 /* wait for receive complete */
4910 for (timeout
= 100; timeout
; --timeout
) {
4911 msleep_interruptible(10);
4912 if (loopback_test_rx(info
)) {
4918 /* verify received frame length and contents */
4919 if (!rc
&& (info
->tmp_rbuf_count
!= count
||
4920 memcmp(buf
, info
->tmp_rbuf
, count
))) {
4924 spin_lock_irqsave(&info
->lock
,flags
);
4925 reset_adapter(info
);
4926 spin_unlock_irqrestore(&info
->lock
,flags
);
4928 memcpy(&info
->params
, ¶ms
, sizeof(info
->params
));
4929 info
->port
.tty
= oldtty
;
4931 info
->init_error
= rc
? DiagStatus_DmaFailure
: 0;
4935 static int adapter_test(struct slgt_info
*info
)
4937 DBGINFO(("testing %s\n", info
->device_name
));
4938 if (register_test(info
) < 0) {
4939 printk("register test failure %s addr=%08X\n",
4940 info
->device_name
, info
->phys_reg_addr
);
4941 } else if (irq_test(info
) < 0) {
4942 printk("IRQ test failure %s IRQ=%d\n",
4943 info
->device_name
, info
->irq_level
);
4944 } else if (loopback_test(info
) < 0) {
4945 printk("loopback test failure %s\n", info
->device_name
);
4947 return info
->init_error
;
4951 * transmit timeout handler
4953 static void tx_timeout(unsigned long context
)
4955 struct slgt_info
*info
= (struct slgt_info
*)context
;
4956 unsigned long flags
;
4958 DBGINFO(("%s tx_timeout\n", info
->device_name
));
4959 if(info
->tx_active
&& info
->params
.mode
== MGSL_MODE_HDLC
) {
4960 info
->icount
.txtimeout
++;
4962 spin_lock_irqsave(&info
->lock
,flags
);
4963 info
->tx_active
= false;
4965 spin_unlock_irqrestore(&info
->lock
,flags
);
4967 #if SYNCLINK_GENERIC_HDLC
4969 hdlcdev_tx_done(info
);
4976 * receive buffer polling timer
4978 static void rx_timeout(unsigned long context
)
4980 struct slgt_info
*info
= (struct slgt_info
*)context
;
4981 unsigned long flags
;
4983 DBGINFO(("%s rx_timeout\n", info
->device_name
));
4984 spin_lock_irqsave(&info
->lock
, flags
);
4985 info
->pending_bh
|= BH_RECEIVE
;
4986 spin_unlock_irqrestore(&info
->lock
, flags
);
4987 bh_handler(&info
->task
);