staging: msm/lcdc.c: Convert IS_ERR result to PTR_ERR
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / synclink_gt.c
blobd01fffeac95171f53cf8d4e7f52eb239989c27ea
1 /*
2 * Device driver for Microgate SyncLink GT serial adapters.
4 * written by Paul Fulghum for Microgate Corporation
5 * paulkf@microgate.com
7 * Microgate and SyncLink are trademarks of Microgate Corporation
9 * This code is released under the GNU General Public License (GPL)
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
12 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21 * OF THE POSSIBILITY OF SUCH DAMAGE.
25 * DEBUG OUTPUT DEFINITIONS
27 * uncomment lines below to enable specific types of debug output
29 * DBGINFO information - most verbose output
30 * DBGERR serious errors
31 * DBGBH bottom half service routine debugging
32 * DBGISR interrupt service routine debugging
33 * DBGDATA output receive and transmit data
34 * DBGTBUF output transmit DMA buffers and registers
35 * DBGRBUF output receive DMA buffers and registers
38 #define DBGINFO(fmt) if (debug_level >= DEBUG_LEVEL_INFO) printk fmt
39 #define DBGERR(fmt) if (debug_level >= DEBUG_LEVEL_ERROR) printk fmt
40 #define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt
41 #define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt
42 #define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label))
43 /*#define DBGTBUF(info) dump_tbufs(info)*/
44 /*#define DBGRBUF(info) dump_rbufs(info)*/
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/signal.h>
50 #include <linux/sched.h>
51 #include <linux/timer.h>
52 #include <linux/interrupt.h>
53 #include <linux/pci.h>
54 #include <linux/tty.h>
55 #include <linux/tty_flip.h>
56 #include <linux/serial.h>
57 #include <linux/major.h>
58 #include <linux/string.h>
59 #include <linux/fcntl.h>
60 #include <linux/ptrace.h>
61 #include <linux/ioport.h>
62 #include <linux/mm.h>
63 #include <linux/seq_file.h>
64 #include <linux/slab.h>
65 #include <linux/netdevice.h>
66 #include <linux/vmalloc.h>
67 #include <linux/init.h>
68 #include <linux/delay.h>
69 #include <linux/ioctl.h>
70 #include <linux/termios.h>
71 #include <linux/bitops.h>
72 #include <linux/workqueue.h>
73 #include <linux/hdlc.h>
74 #include <linux/synclink.h>
76 #include <asm/system.h>
77 #include <asm/io.h>
78 #include <asm/irq.h>
79 #include <asm/dma.h>
80 #include <asm/types.h>
81 #include <asm/uaccess.h>
83 #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_GT_MODULE))
84 #define SYNCLINK_GENERIC_HDLC 1
85 #else
86 #define SYNCLINK_GENERIC_HDLC 0
87 #endif
90 * module identification
92 static char *driver_name = "SyncLink GT";
93 static char *tty_driver_name = "synclink_gt";
94 static char *tty_dev_prefix = "ttySLG";
95 MODULE_LICENSE("GPL");
96 #define MGSL_MAGIC 0x5401
97 #define MAX_DEVICES 32
99 static struct pci_device_id pci_table[] = {
100 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
101 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT2_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
102 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
103 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
104 {0,}, /* terminate list */
106 MODULE_DEVICE_TABLE(pci, pci_table);
108 static int init_one(struct pci_dev *dev,const struct pci_device_id *ent);
109 static void remove_one(struct pci_dev *dev);
110 static struct pci_driver pci_driver = {
111 .name = "synclink_gt",
112 .id_table = pci_table,
113 .probe = init_one,
114 .remove = __devexit_p(remove_one),
117 static bool pci_registered;
120 * module configuration and status
122 static struct slgt_info *slgt_device_list;
123 static int slgt_device_count;
125 static int ttymajor;
126 static int debug_level;
127 static int maxframe[MAX_DEVICES];
129 module_param(ttymajor, int, 0);
130 module_param(debug_level, int, 0);
131 module_param_array(maxframe, int, NULL, 0);
133 MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
134 MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
135 MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
138 * tty support and callbacks
140 static struct tty_driver *serial_driver;
142 static int open(struct tty_struct *tty, struct file * filp);
143 static void close(struct tty_struct *tty, struct file * filp);
144 static void hangup(struct tty_struct *tty);
145 static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
147 static int write(struct tty_struct *tty, const unsigned char *buf, int count);
148 static int put_char(struct tty_struct *tty, unsigned char ch);
149 static void send_xchar(struct tty_struct *tty, char ch);
150 static void wait_until_sent(struct tty_struct *tty, int timeout);
151 static int write_room(struct tty_struct *tty);
152 static void flush_chars(struct tty_struct *tty);
153 static void flush_buffer(struct tty_struct *tty);
154 static void tx_hold(struct tty_struct *tty);
155 static void tx_release(struct tty_struct *tty);
157 static int ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
158 static int chars_in_buffer(struct tty_struct *tty);
159 static void throttle(struct tty_struct * tty);
160 static void unthrottle(struct tty_struct * tty);
161 static int set_break(struct tty_struct *tty, int break_state);
164 * generic HDLC support and callbacks
166 #if SYNCLINK_GENERIC_HDLC
167 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
168 static void hdlcdev_tx_done(struct slgt_info *info);
169 static void hdlcdev_rx(struct slgt_info *info, char *buf, int size);
170 static int hdlcdev_init(struct slgt_info *info);
171 static void hdlcdev_exit(struct slgt_info *info);
172 #endif
176 * device specific structures, macros and functions
179 #define SLGT_MAX_PORTS 4
180 #define SLGT_REG_SIZE 256
183 * conditional wait facility
185 struct cond_wait {
186 struct cond_wait *next;
187 wait_queue_head_t q;
188 wait_queue_t wait;
189 unsigned int data;
191 static void init_cond_wait(struct cond_wait *w, unsigned int data);
192 static void add_cond_wait(struct cond_wait **head, struct cond_wait *w);
193 static void remove_cond_wait(struct cond_wait **head, struct cond_wait *w);
194 static void flush_cond_wait(struct cond_wait **head);
197 * DMA buffer descriptor and access macros
199 struct slgt_desc
201 __le16 count;
202 __le16 status;
203 __le32 pbuf; /* physical address of data buffer */
204 __le32 next; /* physical address of next descriptor */
206 /* driver book keeping */
207 char *buf; /* virtual address of data buffer */
208 unsigned int pdesc; /* physical address of this descriptor */
209 dma_addr_t buf_dma_addr;
210 unsigned short buf_count;
213 #define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
214 #define set_desc_next(a,b) (a).next = cpu_to_le32((unsigned int)(b))
215 #define set_desc_count(a,b)(a).count = cpu_to_le16((unsigned short)(b))
216 #define set_desc_eof(a,b) (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
217 #define set_desc_status(a, b) (a).status = cpu_to_le16((unsigned short)(b))
218 #define desc_count(a) (le16_to_cpu((a).count))
219 #define desc_status(a) (le16_to_cpu((a).status))
220 #define desc_complete(a) (le16_to_cpu((a).status) & BIT15)
221 #define desc_eof(a) (le16_to_cpu((a).status) & BIT2)
222 #define desc_crc_error(a) (le16_to_cpu((a).status) & BIT1)
223 #define desc_abort(a) (le16_to_cpu((a).status) & BIT0)
224 #define desc_residue(a) ((le16_to_cpu((a).status) & 0x38) >> 3)
226 struct _input_signal_events {
227 int ri_up;
228 int ri_down;
229 int dsr_up;
230 int dsr_down;
231 int dcd_up;
232 int dcd_down;
233 int cts_up;
234 int cts_down;
238 * device instance data structure
240 struct slgt_info {
241 void *if_ptr; /* General purpose pointer (used by SPPP) */
242 struct tty_port port;
244 struct slgt_info *next_device; /* device list link */
246 int magic;
248 char device_name[25];
249 struct pci_dev *pdev;
251 int port_count; /* count of ports on adapter */
252 int adapter_num; /* adapter instance number */
253 int port_num; /* port instance number */
255 /* array of pointers to port contexts on this adapter */
256 struct slgt_info *port_array[SLGT_MAX_PORTS];
258 int line; /* tty line instance number */
260 struct mgsl_icount icount;
262 int timeout;
263 int x_char; /* xon/xoff character */
264 unsigned int read_status_mask;
265 unsigned int ignore_status_mask;
267 wait_queue_head_t status_event_wait_q;
268 wait_queue_head_t event_wait_q;
269 struct timer_list tx_timer;
270 struct timer_list rx_timer;
272 unsigned int gpio_present;
273 struct cond_wait *gpio_wait_q;
275 spinlock_t lock; /* spinlock for synchronizing with ISR */
277 struct work_struct task;
278 u32 pending_bh;
279 bool bh_requested;
280 bool bh_running;
282 int isr_overflow;
283 bool irq_requested; /* true if IRQ requested */
284 bool irq_occurred; /* for diagnostics use */
286 /* device configuration */
288 unsigned int bus_type;
289 unsigned int irq_level;
290 unsigned long irq_flags;
292 unsigned char __iomem * reg_addr; /* memory mapped registers address */
293 u32 phys_reg_addr;
294 bool reg_addr_requested;
296 MGSL_PARAMS params; /* communications parameters */
297 u32 idle_mode;
298 u32 max_frame_size; /* as set by device config */
300 unsigned int rbuf_fill_level;
301 unsigned int rx_pio;
302 unsigned int if_mode;
303 unsigned int base_clock;
304 unsigned int xsync;
305 unsigned int xctrl;
307 /* device status */
309 bool rx_enabled;
310 bool rx_restart;
312 bool tx_enabled;
313 bool tx_active;
315 unsigned char signals; /* serial signal states */
316 int init_error; /* initialization error */
318 unsigned char *tx_buf;
319 int tx_count;
321 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
322 char char_buf[MAX_ASYNC_BUFFER_SIZE];
323 bool drop_rts_on_tx_done;
324 struct _input_signal_events input_signal_events;
326 int dcd_chkcount; /* check counts to prevent */
327 int cts_chkcount; /* too many IRQs if a signal */
328 int dsr_chkcount; /* is floating */
329 int ri_chkcount;
331 char *bufs; /* virtual address of DMA buffer lists */
332 dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
334 unsigned int rbuf_count;
335 struct slgt_desc *rbufs;
336 unsigned int rbuf_current;
337 unsigned int rbuf_index;
338 unsigned int rbuf_fill_index;
339 unsigned short rbuf_fill_count;
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 */
351 int netcount;
352 spinlock_t netlock;
353 #if SYNCLINK_GENERIC_HDLC
354 struct net_device *netdev;
355 #endif
359 static MGSL_PARAMS default_params = {
360 .mode = MGSL_MODE_HDLC,
361 .loopback = 0,
362 .flags = HDLC_FLAG_UNDERRUN_ABORT15,
363 .encoding = HDLC_ENCODING_NRZI_SPACE,
364 .clock_speed = 0,
365 .addr_filter = 0xff,
366 .crc_type = HDLC_CRC_16_CCITT,
367 .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
368 .preamble = HDLC_PREAMBLE_PATTERN_NONE,
369 .data_rate = 9600,
370 .data_bits = 8,
371 .stop_bits = 1,
372 .parity = ASYNC_PARITY_NONE
376 #define BH_RECEIVE 1
377 #define BH_TRANSMIT 2
378 #define BH_STATUS 4
379 #define IO_PIN_SHUTDOWN_LIMIT 100
381 #define DMABUFSIZE 256
382 #define DESC_LIST_SIZE 4096
384 #define MASK_PARITY BIT1
385 #define MASK_FRAMING BIT0
386 #define MASK_BREAK BIT14
387 #define MASK_OVERRUN BIT4
389 #define GSR 0x00 /* global status */
390 #define JCR 0x04 /* JTAG control */
391 #define IODR 0x08 /* GPIO direction */
392 #define IOER 0x0c /* GPIO interrupt enable */
393 #define IOVR 0x10 /* GPIO value */
394 #define IOSR 0x14 /* GPIO interrupt status */
395 #define TDR 0x80 /* tx data */
396 #define RDR 0x80 /* rx data */
397 #define TCR 0x82 /* tx control */
398 #define TIR 0x84 /* tx idle */
399 #define TPR 0x85 /* tx preamble */
400 #define RCR 0x86 /* rx control */
401 #define VCR 0x88 /* V.24 control */
402 #define CCR 0x89 /* clock control */
403 #define BDR 0x8a /* baud divisor */
404 #define SCR 0x8c /* serial control */
405 #define SSR 0x8e /* serial status */
406 #define RDCSR 0x90 /* rx DMA control/status */
407 #define TDCSR 0x94 /* tx DMA control/status */
408 #define RDDAR 0x98 /* rx DMA descriptor address */
409 #define TDDAR 0x9c /* tx DMA descriptor address */
410 #define XSR 0x40 /* extended sync pattern */
411 #define XCR 0x44 /* extended control */
413 #define RXIDLE BIT14
414 #define RXBREAK BIT14
415 #define IRQ_TXDATA BIT13
416 #define IRQ_TXIDLE BIT12
417 #define IRQ_TXUNDER BIT11 /* HDLC */
418 #define IRQ_RXDATA BIT10
419 #define IRQ_RXIDLE BIT9 /* HDLC */
420 #define IRQ_RXBREAK BIT9 /* async */
421 #define IRQ_RXOVER BIT8
422 #define IRQ_DSR BIT7
423 #define IRQ_CTS BIT6
424 #define IRQ_DCD BIT5
425 #define IRQ_RI BIT4
426 #define IRQ_ALL 0x3ff0
427 #define IRQ_MASTER BIT0
429 #define slgt_irq_on(info, mask) \
430 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
431 #define slgt_irq_off(info, mask) \
432 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
434 static __u8 rd_reg8(struct slgt_info *info, unsigned int addr);
435 static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
436 static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
437 static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
438 static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
439 static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
441 static void msc_set_vcr(struct slgt_info *info);
443 static int startup(struct slgt_info *info);
444 static int block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
445 static void shutdown(struct slgt_info *info);
446 static void program_hw(struct slgt_info *info);
447 static void change_params(struct slgt_info *info);
449 static int register_test(struct slgt_info *info);
450 static int irq_test(struct slgt_info *info);
451 static int loopback_test(struct slgt_info *info);
452 static int adapter_test(struct slgt_info *info);
454 static void reset_adapter(struct slgt_info *info);
455 static void reset_port(struct slgt_info *info);
456 static void async_mode(struct slgt_info *info);
457 static void sync_mode(struct slgt_info *info);
459 static void rx_stop(struct slgt_info *info);
460 static void rx_start(struct slgt_info *info);
461 static void reset_rbufs(struct slgt_info *info);
462 static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
463 static void rdma_reset(struct slgt_info *info);
464 static bool rx_get_frame(struct slgt_info *info);
465 static bool rx_get_buf(struct slgt_info *info);
467 static void tx_start(struct slgt_info *info);
468 static void tx_stop(struct slgt_info *info);
469 static void tx_set_idle(struct slgt_info *info);
470 static unsigned int free_tbuf_count(struct slgt_info *info);
471 static unsigned int tbuf_bytes(struct slgt_info *info);
472 static void reset_tbufs(struct slgt_info *info);
473 static void tdma_reset(struct slgt_info *info);
474 static bool 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);
503 * ioctl handlers
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);
524 static int get_xsync(struct slgt_info *info, int __user *if_mode);
525 static int set_xsync(struct slgt_info *info, int if_mode);
526 static int get_xctrl(struct slgt_info *info, int __user *if_mode);
527 static int set_xctrl(struct slgt_info *info, int if_mode);
530 * driver functions
532 static void add_device(struct slgt_info *info);
533 static void device_init(int adapter_num, struct pci_dev *pdev);
534 static int claim_resources(struct slgt_info *info);
535 static void release_resources(struct slgt_info *info);
538 * DEBUG OUTPUT CODE
540 #ifndef DBGINFO
541 #define DBGINFO(fmt)
542 #endif
543 #ifndef DBGERR
544 #define DBGERR(fmt)
545 #endif
546 #ifndef DBGBH
547 #define DBGBH(fmt)
548 #endif
549 #ifndef DBGISR
550 #define DBGISR(fmt)
551 #endif
553 #ifdef DBGDATA
554 static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
556 int i;
557 int linecount;
558 printk("%s %s data:\n",info->device_name, label);
559 while(count) {
560 linecount = (count > 16) ? 16 : count;
561 for(i=0; i < linecount; i++)
562 printk("%02X ",(unsigned char)data[i]);
563 for(;i<17;i++)
564 printk(" ");
565 for(i=0;i<linecount;i++) {
566 if (data[i]>=040 && data[i]<=0176)
567 printk("%c",data[i]);
568 else
569 printk(".");
571 printk("\n");
572 data += linecount;
573 count -= linecount;
576 #else
577 #define DBGDATA(info, buf, size, label)
578 #endif
580 #ifdef DBGTBUF
581 static void dump_tbufs(struct slgt_info *info)
583 int i;
584 printk("tbuf_current=%d\n", info->tbuf_current);
585 for (i=0 ; i < info->tbuf_count ; i++) {
586 printk("%d: count=%04X status=%04X\n",
587 i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
590 #else
591 #define DBGTBUF(info)
592 #endif
594 #ifdef DBGRBUF
595 static void dump_rbufs(struct slgt_info *info)
597 int i;
598 printk("rbuf_current=%d\n", info->rbuf_current);
599 for (i=0 ; i < info->rbuf_count ; i++) {
600 printk("%d: count=%04X status=%04X\n",
601 i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
604 #else
605 #define DBGRBUF(info)
606 #endif
608 static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
610 #ifdef SANITY_CHECK
611 if (!info) {
612 printk("null struct slgt_info for (%s) in %s\n", devname, name);
613 return 1;
615 if (info->magic != MGSL_MAGIC) {
616 printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
617 return 1;
619 #else
620 if (!info)
621 return 1;
622 #endif
623 return 0;
627 * line discipline callback wrappers
629 * The wrappers maintain line discipline references
630 * while calling into the line discipline.
632 * ldisc_receive_buf - pass receive data to line discipline
634 static void ldisc_receive_buf(struct tty_struct *tty,
635 const __u8 *data, char *flags, int count)
637 struct tty_ldisc *ld;
638 if (!tty)
639 return;
640 ld = tty_ldisc_ref(tty);
641 if (ld) {
642 if (ld->ops->receive_buf)
643 ld->ops->receive_buf(tty, data, flags, count);
644 tty_ldisc_deref(ld);
648 /* tty callbacks */
650 static int open(struct tty_struct *tty, struct file *filp)
652 struct slgt_info *info;
653 int retval, line;
654 unsigned long flags;
656 line = tty->index;
657 if ((line < 0) || (line >= slgt_device_count)) {
658 DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
659 return -ENODEV;
662 info = slgt_device_list;
663 while(info && info->line != line)
664 info = info->next_device;
665 if (sanity_check(info, tty->name, "open"))
666 return -ENODEV;
667 if (info->init_error) {
668 DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
669 return -ENODEV;
672 tty->driver_data = info;
673 info->port.tty = tty;
675 DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->port.count));
677 /* If port is closing, signal caller to try again */
678 if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
679 if (info->port.flags & ASYNC_CLOSING)
680 interruptible_sleep_on(&info->port.close_wait);
681 retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
682 -EAGAIN : -ERESTARTSYS);
683 goto cleanup;
686 mutex_lock(&info->port.mutex);
687 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
689 spin_lock_irqsave(&info->netlock, flags);
690 if (info->netcount) {
691 retval = -EBUSY;
692 spin_unlock_irqrestore(&info->netlock, flags);
693 mutex_unlock(&info->port.mutex);
694 goto cleanup;
696 info->port.count++;
697 spin_unlock_irqrestore(&info->netlock, flags);
699 if (info->port.count == 1) {
700 /* 1st open on this device, init hardware */
701 retval = startup(info);
702 if (retval < 0) {
703 mutex_unlock(&info->port.mutex);
704 goto cleanup;
707 mutex_unlock(&info->port.mutex);
708 retval = block_til_ready(tty, filp, info);
709 if (retval) {
710 DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
711 goto cleanup;
714 retval = 0;
716 cleanup:
717 if (retval) {
718 if (tty->count == 1)
719 info->port.tty = NULL; /* tty layer will release tty struct */
720 if(info->port.count)
721 info->port.count--;
724 DBGINFO(("%s open rc=%d\n", info->device_name, retval));
725 return retval;
728 static void close(struct tty_struct *tty, struct file *filp)
730 struct slgt_info *info = tty->driver_data;
732 if (sanity_check(info, tty->name, "close"))
733 return;
734 DBGINFO(("%s close entry, count=%d\n", info->device_name, info->port.count));
736 if (tty_port_close_start(&info->port, tty, filp) == 0)
737 goto cleanup;
739 mutex_lock(&info->port.mutex);
740 if (info->port.flags & ASYNC_INITIALIZED)
741 wait_until_sent(tty, info->timeout);
742 flush_buffer(tty);
743 tty_ldisc_flush(tty);
745 shutdown(info);
746 mutex_unlock(&info->port.mutex);
748 tty_port_close_end(&info->port, tty);
749 info->port.tty = NULL;
750 cleanup:
751 DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->port.count));
754 static void hangup(struct tty_struct *tty)
756 struct slgt_info *info = tty->driver_data;
757 unsigned long flags;
759 if (sanity_check(info, tty->name, "hangup"))
760 return;
761 DBGINFO(("%s hangup\n", info->device_name));
763 flush_buffer(tty);
765 mutex_lock(&info->port.mutex);
766 shutdown(info);
768 spin_lock_irqsave(&info->port.lock, flags);
769 info->port.count = 0;
770 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
771 info->port.tty = NULL;
772 spin_unlock_irqrestore(&info->port.lock, flags);
773 mutex_unlock(&info->port.mutex);
775 wake_up_interruptible(&info->port.open_wait);
778 static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
780 struct slgt_info *info = tty->driver_data;
781 unsigned long flags;
783 DBGINFO(("%s set_termios\n", tty->driver->name));
785 change_params(info);
787 /* Handle transition to B0 status */
788 if (old_termios->c_cflag & CBAUD &&
789 !(tty->termios->c_cflag & CBAUD)) {
790 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
791 spin_lock_irqsave(&info->lock,flags);
792 set_signals(info);
793 spin_unlock_irqrestore(&info->lock,flags);
796 /* Handle transition away from B0 status */
797 if (!(old_termios->c_cflag & CBAUD) &&
798 tty->termios->c_cflag & CBAUD) {
799 info->signals |= SerialSignal_DTR;
800 if (!(tty->termios->c_cflag & CRTSCTS) ||
801 !test_bit(TTY_THROTTLED, &tty->flags)) {
802 info->signals |= SerialSignal_RTS;
804 spin_lock_irqsave(&info->lock,flags);
805 set_signals(info);
806 spin_unlock_irqrestore(&info->lock,flags);
809 /* Handle turning off CRTSCTS */
810 if (old_termios->c_cflag & CRTSCTS &&
811 !(tty->termios->c_cflag & CRTSCTS)) {
812 tty->hw_stopped = 0;
813 tx_release(tty);
817 static void update_tx_timer(struct slgt_info *info)
820 * use worst case speed of 1200bps to calculate transmit timeout
821 * based on data in buffers (tbuf_bytes) and FIFO (128 bytes)
823 if (info->params.mode == MGSL_MODE_HDLC) {
824 int timeout = (tbuf_bytes(info) * 7) + 1000;
825 mod_timer(&info->tx_timer, jiffies + msecs_to_jiffies(timeout));
829 static int write(struct tty_struct *tty,
830 const unsigned char *buf, int count)
832 int ret = 0;
833 struct slgt_info *info = tty->driver_data;
834 unsigned long flags;
836 if (sanity_check(info, tty->name, "write"))
837 return -EIO;
839 DBGINFO(("%s write count=%d\n", info->device_name, count));
841 if (!info->tx_buf || (count > info->max_frame_size))
842 return -EIO;
844 if (!count || tty->stopped || tty->hw_stopped)
845 return 0;
847 spin_lock_irqsave(&info->lock, flags);
849 if (info->tx_count) {
850 /* send accumulated data from send_char() */
851 if (!tx_load(info, info->tx_buf, info->tx_count))
852 goto cleanup;
853 info->tx_count = 0;
856 if (tx_load(info, buf, count))
857 ret = count;
859 cleanup:
860 spin_unlock_irqrestore(&info->lock, flags);
861 DBGINFO(("%s write rc=%d\n", info->device_name, ret));
862 return ret;
865 static int put_char(struct tty_struct *tty, unsigned char ch)
867 struct slgt_info *info = tty->driver_data;
868 unsigned long flags;
869 int ret = 0;
871 if (sanity_check(info, tty->name, "put_char"))
872 return 0;
873 DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
874 if (!info->tx_buf)
875 return 0;
876 spin_lock_irqsave(&info->lock,flags);
877 if (info->tx_count < info->max_frame_size) {
878 info->tx_buf[info->tx_count++] = ch;
879 ret = 1;
881 spin_unlock_irqrestore(&info->lock,flags);
882 return ret;
885 static void send_xchar(struct tty_struct *tty, char ch)
887 struct slgt_info *info = tty->driver_data;
888 unsigned long flags;
890 if (sanity_check(info, tty->name, "send_xchar"))
891 return;
892 DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
893 info->x_char = ch;
894 if (ch) {
895 spin_lock_irqsave(&info->lock,flags);
896 if (!info->tx_enabled)
897 tx_start(info);
898 spin_unlock_irqrestore(&info->lock,flags);
902 static void wait_until_sent(struct tty_struct *tty, int timeout)
904 struct slgt_info *info = tty->driver_data;
905 unsigned long orig_jiffies, char_time;
907 if (!info )
908 return;
909 if (sanity_check(info, tty->name, "wait_until_sent"))
910 return;
911 DBGINFO(("%s wait_until_sent entry\n", info->device_name));
912 if (!(info->port.flags & ASYNC_INITIALIZED))
913 goto exit;
915 orig_jiffies = jiffies;
917 /* Set check interval to 1/5 of estimated time to
918 * send a character, and make it at least 1. The check
919 * interval should also be less than the timeout.
920 * Note: use tight timings here to satisfy the NIST-PCTS.
923 if (info->params.data_rate) {
924 char_time = info->timeout/(32 * 5);
925 if (!char_time)
926 char_time++;
927 } else
928 char_time = 1;
930 if (timeout)
931 char_time = min_t(unsigned long, char_time, timeout);
933 while (info->tx_active) {
934 msleep_interruptible(jiffies_to_msecs(char_time));
935 if (signal_pending(current))
936 break;
937 if (timeout && time_after(jiffies, orig_jiffies + timeout))
938 break;
940 exit:
941 DBGINFO(("%s wait_until_sent exit\n", info->device_name));
944 static int write_room(struct tty_struct *tty)
946 struct slgt_info *info = tty->driver_data;
947 int ret;
949 if (sanity_check(info, tty->name, "write_room"))
950 return 0;
951 ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
952 DBGINFO(("%s write_room=%d\n", info->device_name, ret));
953 return ret;
956 static void flush_chars(struct tty_struct *tty)
958 struct slgt_info *info = tty->driver_data;
959 unsigned long flags;
961 if (sanity_check(info, tty->name, "flush_chars"))
962 return;
963 DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
965 if (info->tx_count <= 0 || tty->stopped ||
966 tty->hw_stopped || !info->tx_buf)
967 return;
969 DBGINFO(("%s flush_chars start transmit\n", info->device_name));
971 spin_lock_irqsave(&info->lock,flags);
972 if (info->tx_count && tx_load(info, info->tx_buf, info->tx_count))
973 info->tx_count = 0;
974 spin_unlock_irqrestore(&info->lock,flags);
977 static void flush_buffer(struct tty_struct *tty)
979 struct slgt_info *info = tty->driver_data;
980 unsigned long flags;
982 if (sanity_check(info, tty->name, "flush_buffer"))
983 return;
984 DBGINFO(("%s flush_buffer\n", info->device_name));
986 spin_lock_irqsave(&info->lock, flags);
987 info->tx_count = 0;
988 spin_unlock_irqrestore(&info->lock, flags);
990 tty_wakeup(tty);
994 * throttle (stop) transmitter
996 static void tx_hold(struct tty_struct *tty)
998 struct slgt_info *info = tty->driver_data;
999 unsigned long flags;
1001 if (sanity_check(info, tty->name, "tx_hold"))
1002 return;
1003 DBGINFO(("%s tx_hold\n", info->device_name));
1004 spin_lock_irqsave(&info->lock,flags);
1005 if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
1006 tx_stop(info);
1007 spin_unlock_irqrestore(&info->lock,flags);
1011 * release (start) transmitter
1013 static void tx_release(struct tty_struct *tty)
1015 struct slgt_info *info = tty->driver_data;
1016 unsigned long flags;
1018 if (sanity_check(info, tty->name, "tx_release"))
1019 return;
1020 DBGINFO(("%s tx_release\n", info->device_name));
1021 spin_lock_irqsave(&info->lock, flags);
1022 if (info->tx_count && tx_load(info, info->tx_buf, info->tx_count))
1023 info->tx_count = 0;
1024 spin_unlock_irqrestore(&info->lock, flags);
1028 * Service an IOCTL request
1030 * Arguments
1032 * tty pointer to tty instance data
1033 * file pointer to associated file object for device
1034 * cmd IOCTL command code
1035 * arg command argument/context
1037 * Return 0 if success, otherwise error code
1039 static int ioctl(struct tty_struct *tty, struct file *file,
1040 unsigned int cmd, unsigned long arg)
1042 struct slgt_info *info = tty->driver_data;
1043 void __user *argp = (void __user *)arg;
1044 int ret;
1046 if (sanity_check(info, tty->name, "ioctl"))
1047 return -ENODEV;
1048 DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
1050 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1051 (cmd != TIOCMIWAIT)) {
1052 if (tty->flags & (1 << TTY_IO_ERROR))
1053 return -EIO;
1056 switch (cmd) {
1057 case MGSL_IOCWAITEVENT:
1058 return wait_mgsl_event(info, argp);
1059 case TIOCMIWAIT:
1060 return modem_input_wait(info,(int)arg);
1061 case MGSL_IOCSGPIO:
1062 return set_gpio(info, argp);
1063 case MGSL_IOCGGPIO:
1064 return get_gpio(info, argp);
1065 case MGSL_IOCWAITGPIO:
1066 return wait_gpio(info, argp);
1067 case MGSL_IOCGXSYNC:
1068 return get_xsync(info, argp);
1069 case MGSL_IOCSXSYNC:
1070 return set_xsync(info, (int)arg);
1071 case MGSL_IOCGXCTRL:
1072 return get_xctrl(info, argp);
1073 case MGSL_IOCSXCTRL:
1074 return set_xctrl(info, (int)arg);
1076 mutex_lock(&info->port.mutex);
1077 switch (cmd) {
1078 case MGSL_IOCGPARAMS:
1079 ret = get_params(info, argp);
1080 break;
1081 case MGSL_IOCSPARAMS:
1082 ret = set_params(info, argp);
1083 break;
1084 case MGSL_IOCGTXIDLE:
1085 ret = get_txidle(info, argp);
1086 break;
1087 case MGSL_IOCSTXIDLE:
1088 ret = set_txidle(info, (int)arg);
1089 break;
1090 case MGSL_IOCTXENABLE:
1091 ret = tx_enable(info, (int)arg);
1092 break;
1093 case MGSL_IOCRXENABLE:
1094 ret = rx_enable(info, (int)arg);
1095 break;
1096 case MGSL_IOCTXABORT:
1097 ret = tx_abort(info);
1098 break;
1099 case MGSL_IOCGSTATS:
1100 ret = get_stats(info, argp);
1101 break;
1102 case MGSL_IOCGIF:
1103 ret = get_interface(info, argp);
1104 break;
1105 case MGSL_IOCSIF:
1106 ret = set_interface(info,(int)arg);
1107 break;
1108 default:
1109 ret = -ENOIOCTLCMD;
1111 mutex_unlock(&info->port.mutex);
1112 return ret;
1115 static int get_icount(struct tty_struct *tty,
1116 struct serial_icounter_struct *icount)
1119 struct slgt_info *info = tty->driver_data;
1120 struct mgsl_icount cnow; /* kernel counter temps */
1121 unsigned long flags;
1123 spin_lock_irqsave(&info->lock,flags);
1124 cnow = info->icount;
1125 spin_unlock_irqrestore(&info->lock,flags);
1127 icount->cts = cnow.cts;
1128 icount->dsr = cnow.dsr;
1129 icount->rng = cnow.rng;
1130 icount->dcd = cnow.dcd;
1131 icount->rx = cnow.rx;
1132 icount->tx = cnow.tx;
1133 icount->frame = cnow.frame;
1134 icount->overrun = cnow.overrun;
1135 icount->parity = cnow.parity;
1136 icount->brk = cnow.brk;
1137 icount->buf_overrun = cnow.buf_overrun;
1139 return 0;
1143 * support for 32 bit ioctl calls on 64 bit systems
1145 #ifdef CONFIG_COMPAT
1146 static long get_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *user_params)
1148 struct MGSL_PARAMS32 tmp_params;
1150 DBGINFO(("%s get_params32\n", info->device_name));
1151 memset(&tmp_params, 0, sizeof(tmp_params));
1152 tmp_params.mode = (compat_ulong_t)info->params.mode;
1153 tmp_params.loopback = info->params.loopback;
1154 tmp_params.flags = info->params.flags;
1155 tmp_params.encoding = info->params.encoding;
1156 tmp_params.clock_speed = (compat_ulong_t)info->params.clock_speed;
1157 tmp_params.addr_filter = info->params.addr_filter;
1158 tmp_params.crc_type = info->params.crc_type;
1159 tmp_params.preamble_length = info->params.preamble_length;
1160 tmp_params.preamble = info->params.preamble;
1161 tmp_params.data_rate = (compat_ulong_t)info->params.data_rate;
1162 tmp_params.data_bits = info->params.data_bits;
1163 tmp_params.stop_bits = info->params.stop_bits;
1164 tmp_params.parity = info->params.parity;
1165 if (copy_to_user(user_params, &tmp_params, sizeof(struct MGSL_PARAMS32)))
1166 return -EFAULT;
1167 return 0;
1170 static long set_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *new_params)
1172 struct MGSL_PARAMS32 tmp_params;
1174 DBGINFO(("%s set_params32\n", info->device_name));
1175 if (copy_from_user(&tmp_params, new_params, sizeof(struct MGSL_PARAMS32)))
1176 return -EFAULT;
1178 spin_lock(&info->lock);
1179 if (tmp_params.mode == MGSL_MODE_BASE_CLOCK) {
1180 info->base_clock = tmp_params.clock_speed;
1181 } else {
1182 info->params.mode = tmp_params.mode;
1183 info->params.loopback = tmp_params.loopback;
1184 info->params.flags = tmp_params.flags;
1185 info->params.encoding = tmp_params.encoding;
1186 info->params.clock_speed = tmp_params.clock_speed;
1187 info->params.addr_filter = tmp_params.addr_filter;
1188 info->params.crc_type = tmp_params.crc_type;
1189 info->params.preamble_length = tmp_params.preamble_length;
1190 info->params.preamble = tmp_params.preamble;
1191 info->params.data_rate = tmp_params.data_rate;
1192 info->params.data_bits = tmp_params.data_bits;
1193 info->params.stop_bits = tmp_params.stop_bits;
1194 info->params.parity = tmp_params.parity;
1196 spin_unlock(&info->lock);
1198 program_hw(info);
1200 return 0;
1203 static long slgt_compat_ioctl(struct tty_struct *tty, struct file *file,
1204 unsigned int cmd, unsigned long arg)
1206 struct slgt_info *info = tty->driver_data;
1207 int rc = -ENOIOCTLCMD;
1209 if (sanity_check(info, tty->name, "compat_ioctl"))
1210 return -ENODEV;
1211 DBGINFO(("%s compat_ioctl() cmd=%08X\n", info->device_name, cmd));
1213 switch (cmd) {
1215 case MGSL_IOCSPARAMS32:
1216 rc = set_params32(info, compat_ptr(arg));
1217 break;
1219 case MGSL_IOCGPARAMS32:
1220 rc = get_params32(info, compat_ptr(arg));
1221 break;
1223 case MGSL_IOCGPARAMS:
1224 case MGSL_IOCSPARAMS:
1225 case MGSL_IOCGTXIDLE:
1226 case MGSL_IOCGSTATS:
1227 case MGSL_IOCWAITEVENT:
1228 case MGSL_IOCGIF:
1229 case MGSL_IOCSGPIO:
1230 case MGSL_IOCGGPIO:
1231 case MGSL_IOCWAITGPIO:
1232 case MGSL_IOCGXSYNC:
1233 case MGSL_IOCGXCTRL:
1234 case MGSL_IOCSTXIDLE:
1235 case MGSL_IOCTXENABLE:
1236 case MGSL_IOCRXENABLE:
1237 case MGSL_IOCTXABORT:
1238 case TIOCMIWAIT:
1239 case MGSL_IOCSIF:
1240 case MGSL_IOCSXSYNC:
1241 case MGSL_IOCSXCTRL:
1242 rc = ioctl(tty, file, cmd, arg);
1243 break;
1246 DBGINFO(("%s compat_ioctl() cmd=%08X rc=%d\n", info->device_name, cmd, rc));
1247 return rc;
1249 #else
1250 #define slgt_compat_ioctl NULL
1251 #endif /* ifdef CONFIG_COMPAT */
1254 * proc fs support
1256 static inline void line_info(struct seq_file *m, struct slgt_info *info)
1258 char stat_buf[30];
1259 unsigned long flags;
1261 seq_printf(m, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
1262 info->device_name, info->phys_reg_addr,
1263 info->irq_level, info->max_frame_size);
1265 /* output current serial signal states */
1266 spin_lock_irqsave(&info->lock,flags);
1267 get_signals(info);
1268 spin_unlock_irqrestore(&info->lock,flags);
1270 stat_buf[0] = 0;
1271 stat_buf[1] = 0;
1272 if (info->signals & SerialSignal_RTS)
1273 strcat(stat_buf, "|RTS");
1274 if (info->signals & SerialSignal_CTS)
1275 strcat(stat_buf, "|CTS");
1276 if (info->signals & SerialSignal_DTR)
1277 strcat(stat_buf, "|DTR");
1278 if (info->signals & SerialSignal_DSR)
1279 strcat(stat_buf, "|DSR");
1280 if (info->signals & SerialSignal_DCD)
1281 strcat(stat_buf, "|CD");
1282 if (info->signals & SerialSignal_RI)
1283 strcat(stat_buf, "|RI");
1285 if (info->params.mode != MGSL_MODE_ASYNC) {
1286 seq_printf(m, "\tHDLC txok:%d rxok:%d",
1287 info->icount.txok, info->icount.rxok);
1288 if (info->icount.txunder)
1289 seq_printf(m, " txunder:%d", info->icount.txunder);
1290 if (info->icount.txabort)
1291 seq_printf(m, " txabort:%d", info->icount.txabort);
1292 if (info->icount.rxshort)
1293 seq_printf(m, " rxshort:%d", info->icount.rxshort);
1294 if (info->icount.rxlong)
1295 seq_printf(m, " rxlong:%d", info->icount.rxlong);
1296 if (info->icount.rxover)
1297 seq_printf(m, " rxover:%d", info->icount.rxover);
1298 if (info->icount.rxcrc)
1299 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
1300 } else {
1301 seq_printf(m, "\tASYNC tx:%d rx:%d",
1302 info->icount.tx, info->icount.rx);
1303 if (info->icount.frame)
1304 seq_printf(m, " fe:%d", info->icount.frame);
1305 if (info->icount.parity)
1306 seq_printf(m, " pe:%d", info->icount.parity);
1307 if (info->icount.brk)
1308 seq_printf(m, " brk:%d", info->icount.brk);
1309 if (info->icount.overrun)
1310 seq_printf(m, " oe:%d", info->icount.overrun);
1313 /* Append serial signal status to end */
1314 seq_printf(m, " %s\n", stat_buf+1);
1316 seq_printf(m, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1317 info->tx_active,info->bh_requested,info->bh_running,
1318 info->pending_bh);
1321 /* Called to print information about devices
1323 static int synclink_gt_proc_show(struct seq_file *m, void *v)
1325 struct slgt_info *info;
1327 seq_puts(m, "synclink_gt driver\n");
1329 info = slgt_device_list;
1330 while( info ) {
1331 line_info(m, info);
1332 info = info->next_device;
1334 return 0;
1337 static int synclink_gt_proc_open(struct inode *inode, struct file *file)
1339 return single_open(file, synclink_gt_proc_show, NULL);
1342 static const struct file_operations synclink_gt_proc_fops = {
1343 .owner = THIS_MODULE,
1344 .open = synclink_gt_proc_open,
1345 .read = seq_read,
1346 .llseek = seq_lseek,
1347 .release = single_release,
1351 * return count of bytes in transmit buffer
1353 static int chars_in_buffer(struct tty_struct *tty)
1355 struct slgt_info *info = tty->driver_data;
1356 int count;
1357 if (sanity_check(info, tty->name, "chars_in_buffer"))
1358 return 0;
1359 count = tbuf_bytes(info);
1360 DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, count));
1361 return count;
1365 * signal remote device to throttle send data (our receive data)
1367 static void throttle(struct tty_struct * tty)
1369 struct slgt_info *info = tty->driver_data;
1370 unsigned long flags;
1372 if (sanity_check(info, tty->name, "throttle"))
1373 return;
1374 DBGINFO(("%s throttle\n", info->device_name));
1375 if (I_IXOFF(tty))
1376 send_xchar(tty, STOP_CHAR(tty));
1377 if (tty->termios->c_cflag & CRTSCTS) {
1378 spin_lock_irqsave(&info->lock,flags);
1379 info->signals &= ~SerialSignal_RTS;
1380 set_signals(info);
1381 spin_unlock_irqrestore(&info->lock,flags);
1386 * signal remote device to stop throttling send data (our receive data)
1388 static void unthrottle(struct tty_struct * tty)
1390 struct slgt_info *info = tty->driver_data;
1391 unsigned long flags;
1393 if (sanity_check(info, tty->name, "unthrottle"))
1394 return;
1395 DBGINFO(("%s unthrottle\n", info->device_name));
1396 if (I_IXOFF(tty)) {
1397 if (info->x_char)
1398 info->x_char = 0;
1399 else
1400 send_xchar(tty, START_CHAR(tty));
1402 if (tty->termios->c_cflag & CRTSCTS) {
1403 spin_lock_irqsave(&info->lock,flags);
1404 info->signals |= SerialSignal_RTS;
1405 set_signals(info);
1406 spin_unlock_irqrestore(&info->lock,flags);
1411 * set or clear transmit break condition
1412 * break_state -1=set break condition, 0=clear
1414 static int set_break(struct tty_struct *tty, int break_state)
1416 struct slgt_info *info = tty->driver_data;
1417 unsigned short value;
1418 unsigned long flags;
1420 if (sanity_check(info, tty->name, "set_break"))
1421 return -EINVAL;
1422 DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
1424 spin_lock_irqsave(&info->lock,flags);
1425 value = rd_reg16(info, TCR);
1426 if (break_state == -1)
1427 value |= BIT6;
1428 else
1429 value &= ~BIT6;
1430 wr_reg16(info, TCR, value);
1431 spin_unlock_irqrestore(&info->lock,flags);
1432 return 0;
1435 #if SYNCLINK_GENERIC_HDLC
1438 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1439 * set encoding and frame check sequence (FCS) options
1441 * dev pointer to network device structure
1442 * encoding serial encoding setting
1443 * parity FCS setting
1445 * returns 0 if success, otherwise error code
1447 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
1448 unsigned short parity)
1450 struct slgt_info *info = dev_to_port(dev);
1451 unsigned char new_encoding;
1452 unsigned short new_crctype;
1454 /* return error if TTY interface open */
1455 if (info->port.count)
1456 return -EBUSY;
1458 DBGINFO(("%s hdlcdev_attach\n", info->device_name));
1460 switch (encoding)
1462 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
1463 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
1464 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
1465 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
1466 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
1467 default: return -EINVAL;
1470 switch (parity)
1472 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
1473 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
1474 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
1475 default: return -EINVAL;
1478 info->params.encoding = new_encoding;
1479 info->params.crc_type = new_crctype;
1481 /* if network interface up, reprogram hardware */
1482 if (info->netcount)
1483 program_hw(info);
1485 return 0;
1489 * called by generic HDLC layer to send frame
1491 * skb socket buffer containing HDLC frame
1492 * dev pointer to network device structure
1494 static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
1495 struct net_device *dev)
1497 struct slgt_info *info = dev_to_port(dev);
1498 unsigned long flags;
1500 DBGINFO(("%s hdlc_xmit\n", dev->name));
1502 if (!skb->len)
1503 return NETDEV_TX_OK;
1505 /* stop sending until this frame completes */
1506 netif_stop_queue(dev);
1508 /* update network statistics */
1509 dev->stats.tx_packets++;
1510 dev->stats.tx_bytes += skb->len;
1512 /* save start time for transmit timeout detection */
1513 dev->trans_start = jiffies;
1515 spin_lock_irqsave(&info->lock, flags);
1516 tx_load(info, skb->data, skb->len);
1517 spin_unlock_irqrestore(&info->lock, flags);
1519 /* done with socket buffer, so free it */
1520 dev_kfree_skb(skb);
1522 return NETDEV_TX_OK;
1526 * called by network layer when interface enabled
1527 * claim resources and initialize hardware
1529 * dev pointer to network device structure
1531 * returns 0 if success, otherwise error code
1533 static int hdlcdev_open(struct net_device *dev)
1535 struct slgt_info *info = dev_to_port(dev);
1536 int rc;
1537 unsigned long flags;
1539 if (!try_module_get(THIS_MODULE))
1540 return -EBUSY;
1542 DBGINFO(("%s hdlcdev_open\n", dev->name));
1544 /* generic HDLC layer open processing */
1545 if ((rc = hdlc_open(dev)))
1546 return rc;
1548 /* arbitrate between network and tty opens */
1549 spin_lock_irqsave(&info->netlock, flags);
1550 if (info->port.count != 0 || info->netcount != 0) {
1551 DBGINFO(("%s hdlc_open busy\n", dev->name));
1552 spin_unlock_irqrestore(&info->netlock, flags);
1553 return -EBUSY;
1555 info->netcount=1;
1556 spin_unlock_irqrestore(&info->netlock, flags);
1558 /* claim resources and init adapter */
1559 if ((rc = startup(info)) != 0) {
1560 spin_lock_irqsave(&info->netlock, flags);
1561 info->netcount=0;
1562 spin_unlock_irqrestore(&info->netlock, flags);
1563 return rc;
1566 /* assert DTR and RTS, apply hardware settings */
1567 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
1568 program_hw(info);
1570 /* enable network layer transmit */
1571 dev->trans_start = jiffies;
1572 netif_start_queue(dev);
1574 /* inform generic HDLC layer of current DCD status */
1575 spin_lock_irqsave(&info->lock, flags);
1576 get_signals(info);
1577 spin_unlock_irqrestore(&info->lock, flags);
1578 if (info->signals & SerialSignal_DCD)
1579 netif_carrier_on(dev);
1580 else
1581 netif_carrier_off(dev);
1582 return 0;
1586 * called by network layer when interface is disabled
1587 * shutdown hardware and release resources
1589 * dev pointer to network device structure
1591 * returns 0 if success, otherwise error code
1593 static int hdlcdev_close(struct net_device *dev)
1595 struct slgt_info *info = dev_to_port(dev);
1596 unsigned long flags;
1598 DBGINFO(("%s hdlcdev_close\n", dev->name));
1600 netif_stop_queue(dev);
1602 /* shutdown adapter and release resources */
1603 shutdown(info);
1605 hdlc_close(dev);
1607 spin_lock_irqsave(&info->netlock, flags);
1608 info->netcount=0;
1609 spin_unlock_irqrestore(&info->netlock, flags);
1611 module_put(THIS_MODULE);
1612 return 0;
1616 * called by network layer to process IOCTL call to network device
1618 * dev pointer to network device structure
1619 * ifr pointer to network interface request structure
1620 * cmd IOCTL command code
1622 * returns 0 if success, otherwise error code
1624 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1626 const size_t size = sizeof(sync_serial_settings);
1627 sync_serial_settings new_line;
1628 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1629 struct slgt_info *info = dev_to_port(dev);
1630 unsigned int flags;
1632 DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
1634 /* return error if TTY interface open */
1635 if (info->port.count)
1636 return -EBUSY;
1638 if (cmd != SIOCWANDEV)
1639 return hdlc_ioctl(dev, ifr, cmd);
1641 memset(&new_line, 0, sizeof(new_line));
1643 switch(ifr->ifr_settings.type) {
1644 case IF_GET_IFACE: /* return current sync_serial_settings */
1646 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1647 if (ifr->ifr_settings.size < size) {
1648 ifr->ifr_settings.size = size; /* data size wanted */
1649 return -ENOBUFS;
1652 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1653 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1654 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1655 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1657 switch (flags){
1658 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
1659 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
1660 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
1661 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
1662 default: new_line.clock_type = CLOCK_DEFAULT;
1665 new_line.clock_rate = info->params.clock_speed;
1666 new_line.loopback = info->params.loopback ? 1:0;
1668 if (copy_to_user(line, &new_line, size))
1669 return -EFAULT;
1670 return 0;
1672 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
1674 if(!capable(CAP_NET_ADMIN))
1675 return -EPERM;
1676 if (copy_from_user(&new_line, line, size))
1677 return -EFAULT;
1679 switch (new_line.clock_type)
1681 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
1682 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
1683 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
1684 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
1685 case CLOCK_DEFAULT: flags = info->params.flags &
1686 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1687 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1688 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1689 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
1690 default: return -EINVAL;
1693 if (new_line.loopback != 0 && new_line.loopback != 1)
1694 return -EINVAL;
1696 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1697 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1698 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1699 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1700 info->params.flags |= flags;
1702 info->params.loopback = new_line.loopback;
1704 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
1705 info->params.clock_speed = new_line.clock_rate;
1706 else
1707 info->params.clock_speed = 0;
1709 /* if network interface up, reprogram hardware */
1710 if (info->netcount)
1711 program_hw(info);
1712 return 0;
1714 default:
1715 return hdlc_ioctl(dev, ifr, cmd);
1720 * called by network layer when transmit timeout is detected
1722 * dev pointer to network device structure
1724 static void hdlcdev_tx_timeout(struct net_device *dev)
1726 struct slgt_info *info = dev_to_port(dev);
1727 unsigned long flags;
1729 DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
1731 dev->stats.tx_errors++;
1732 dev->stats.tx_aborted_errors++;
1734 spin_lock_irqsave(&info->lock,flags);
1735 tx_stop(info);
1736 spin_unlock_irqrestore(&info->lock,flags);
1738 netif_wake_queue(dev);
1742 * called by device driver when transmit completes
1743 * reenable network layer transmit if stopped
1745 * info pointer to device instance information
1747 static void hdlcdev_tx_done(struct slgt_info *info)
1749 if (netif_queue_stopped(info->netdev))
1750 netif_wake_queue(info->netdev);
1754 * called by device driver when frame received
1755 * pass frame to network layer
1757 * info pointer to device instance information
1758 * buf pointer to buffer contianing frame data
1759 * size count of data bytes in buf
1761 static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
1763 struct sk_buff *skb = dev_alloc_skb(size);
1764 struct net_device *dev = info->netdev;
1766 DBGINFO(("%s hdlcdev_rx\n", dev->name));
1768 if (skb == NULL) {
1769 DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
1770 dev->stats.rx_dropped++;
1771 return;
1774 memcpy(skb_put(skb, size), buf, size);
1776 skb->protocol = hdlc_type_trans(skb, dev);
1778 dev->stats.rx_packets++;
1779 dev->stats.rx_bytes += size;
1781 netif_rx(skb);
1784 static const struct net_device_ops hdlcdev_ops = {
1785 .ndo_open = hdlcdev_open,
1786 .ndo_stop = hdlcdev_close,
1787 .ndo_change_mtu = hdlc_change_mtu,
1788 .ndo_start_xmit = hdlc_start_xmit,
1789 .ndo_do_ioctl = hdlcdev_ioctl,
1790 .ndo_tx_timeout = hdlcdev_tx_timeout,
1794 * called by device driver when adding device instance
1795 * do generic HDLC initialization
1797 * info pointer to device instance information
1799 * returns 0 if success, otherwise error code
1801 static int hdlcdev_init(struct slgt_info *info)
1803 int rc;
1804 struct net_device *dev;
1805 hdlc_device *hdlc;
1807 /* allocate and initialize network and HDLC layer objects */
1809 if (!(dev = alloc_hdlcdev(info))) {
1810 printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
1811 return -ENOMEM;
1814 /* for network layer reporting purposes only */
1815 dev->mem_start = info->phys_reg_addr;
1816 dev->mem_end = info->phys_reg_addr + SLGT_REG_SIZE - 1;
1817 dev->irq = info->irq_level;
1819 /* network layer callbacks and settings */
1820 dev->netdev_ops = &hdlcdev_ops;
1821 dev->watchdog_timeo = 10 * HZ;
1822 dev->tx_queue_len = 50;
1824 /* generic HDLC layer callbacks and settings */
1825 hdlc = dev_to_hdlc(dev);
1826 hdlc->attach = hdlcdev_attach;
1827 hdlc->xmit = hdlcdev_xmit;
1829 /* register objects with HDLC layer */
1830 if ((rc = register_hdlc_device(dev))) {
1831 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
1832 free_netdev(dev);
1833 return rc;
1836 info->netdev = dev;
1837 return 0;
1841 * called by device driver when removing device instance
1842 * do generic HDLC cleanup
1844 * info pointer to device instance information
1846 static void hdlcdev_exit(struct slgt_info *info)
1848 unregister_hdlc_device(info->netdev);
1849 free_netdev(info->netdev);
1850 info->netdev = NULL;
1853 #endif /* ifdef CONFIG_HDLC */
1856 * get async data from rx DMA buffers
1858 static void rx_async(struct slgt_info *info)
1860 struct tty_struct *tty = info->port.tty;
1861 struct mgsl_icount *icount = &info->icount;
1862 unsigned int start, end;
1863 unsigned char *p;
1864 unsigned char status;
1865 struct slgt_desc *bufs = info->rbufs;
1866 int i, count;
1867 int chars = 0;
1868 int stat;
1869 unsigned char ch;
1871 start = end = info->rbuf_current;
1873 while(desc_complete(bufs[end])) {
1874 count = desc_count(bufs[end]) - info->rbuf_index;
1875 p = bufs[end].buf + info->rbuf_index;
1877 DBGISR(("%s rx_async count=%d\n", info->device_name, count));
1878 DBGDATA(info, p, count, "rx");
1880 for(i=0 ; i < count; i+=2, p+=2) {
1881 ch = *p;
1882 icount->rx++;
1884 stat = 0;
1886 if ((status = *(p+1) & (BIT1 + BIT0))) {
1887 if (status & BIT1)
1888 icount->parity++;
1889 else if (status & BIT0)
1890 icount->frame++;
1891 /* discard char if tty control flags say so */
1892 if (status & info->ignore_status_mask)
1893 continue;
1894 if (status & BIT1)
1895 stat = TTY_PARITY;
1896 else if (status & BIT0)
1897 stat = TTY_FRAME;
1899 if (tty) {
1900 tty_insert_flip_char(tty, ch, stat);
1901 chars++;
1905 if (i < count) {
1906 /* receive buffer not completed */
1907 info->rbuf_index += i;
1908 mod_timer(&info->rx_timer, jiffies + 1);
1909 break;
1912 info->rbuf_index = 0;
1913 free_rbufs(info, end, end);
1915 if (++end == info->rbuf_count)
1916 end = 0;
1918 /* if entire list searched then no frame available */
1919 if (end == start)
1920 break;
1923 if (tty && chars)
1924 tty_flip_buffer_push(tty);
1928 * return next bottom half action to perform
1930 static int bh_action(struct slgt_info *info)
1932 unsigned long flags;
1933 int rc;
1935 spin_lock_irqsave(&info->lock,flags);
1937 if (info->pending_bh & BH_RECEIVE) {
1938 info->pending_bh &= ~BH_RECEIVE;
1939 rc = BH_RECEIVE;
1940 } else if (info->pending_bh & BH_TRANSMIT) {
1941 info->pending_bh &= ~BH_TRANSMIT;
1942 rc = BH_TRANSMIT;
1943 } else if (info->pending_bh & BH_STATUS) {
1944 info->pending_bh &= ~BH_STATUS;
1945 rc = BH_STATUS;
1946 } else {
1947 /* Mark BH routine as complete */
1948 info->bh_running = false;
1949 info->bh_requested = false;
1950 rc = 0;
1953 spin_unlock_irqrestore(&info->lock,flags);
1955 return rc;
1959 * perform bottom half processing
1961 static void bh_handler(struct work_struct *work)
1963 struct slgt_info *info = container_of(work, struct slgt_info, task);
1964 int action;
1966 if (!info)
1967 return;
1968 info->bh_running = true;
1970 while((action = bh_action(info))) {
1971 switch (action) {
1972 case BH_RECEIVE:
1973 DBGBH(("%s bh receive\n", info->device_name));
1974 switch(info->params.mode) {
1975 case MGSL_MODE_ASYNC:
1976 rx_async(info);
1977 break;
1978 case MGSL_MODE_HDLC:
1979 while(rx_get_frame(info));
1980 break;
1981 case MGSL_MODE_RAW:
1982 case MGSL_MODE_MONOSYNC:
1983 case MGSL_MODE_BISYNC:
1984 case MGSL_MODE_XSYNC:
1985 while(rx_get_buf(info));
1986 break;
1988 /* restart receiver if rx DMA buffers exhausted */
1989 if (info->rx_restart)
1990 rx_start(info);
1991 break;
1992 case BH_TRANSMIT:
1993 bh_transmit(info);
1994 break;
1995 case BH_STATUS:
1996 DBGBH(("%s bh status\n", info->device_name));
1997 info->ri_chkcount = 0;
1998 info->dsr_chkcount = 0;
1999 info->dcd_chkcount = 0;
2000 info->cts_chkcount = 0;
2001 break;
2002 default:
2003 DBGBH(("%s unknown action\n", info->device_name));
2004 break;
2007 DBGBH(("%s bh_handler exit\n", info->device_name));
2010 static void bh_transmit(struct slgt_info *info)
2012 struct tty_struct *tty = info->port.tty;
2014 DBGBH(("%s bh_transmit\n", info->device_name));
2015 if (tty)
2016 tty_wakeup(tty);
2019 static void dsr_change(struct slgt_info *info, unsigned short status)
2021 if (status & BIT3) {
2022 info->signals |= SerialSignal_DSR;
2023 info->input_signal_events.dsr_up++;
2024 } else {
2025 info->signals &= ~SerialSignal_DSR;
2026 info->input_signal_events.dsr_down++;
2028 DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
2029 if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2030 slgt_irq_off(info, IRQ_DSR);
2031 return;
2033 info->icount.dsr++;
2034 wake_up_interruptible(&info->status_event_wait_q);
2035 wake_up_interruptible(&info->event_wait_q);
2036 info->pending_bh |= BH_STATUS;
2039 static void cts_change(struct slgt_info *info, unsigned short status)
2041 if (status & BIT2) {
2042 info->signals |= SerialSignal_CTS;
2043 info->input_signal_events.cts_up++;
2044 } else {
2045 info->signals &= ~SerialSignal_CTS;
2046 info->input_signal_events.cts_down++;
2048 DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
2049 if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2050 slgt_irq_off(info, IRQ_CTS);
2051 return;
2053 info->icount.cts++;
2054 wake_up_interruptible(&info->status_event_wait_q);
2055 wake_up_interruptible(&info->event_wait_q);
2056 info->pending_bh |= BH_STATUS;
2058 if (info->port.flags & ASYNC_CTS_FLOW) {
2059 if (info->port.tty) {
2060 if (info->port.tty->hw_stopped) {
2061 if (info->signals & SerialSignal_CTS) {
2062 info->port.tty->hw_stopped = 0;
2063 info->pending_bh |= BH_TRANSMIT;
2064 return;
2066 } else {
2067 if (!(info->signals & SerialSignal_CTS))
2068 info->port.tty->hw_stopped = 1;
2074 static void dcd_change(struct slgt_info *info, unsigned short status)
2076 if (status & BIT1) {
2077 info->signals |= SerialSignal_DCD;
2078 info->input_signal_events.dcd_up++;
2079 } else {
2080 info->signals &= ~SerialSignal_DCD;
2081 info->input_signal_events.dcd_down++;
2083 DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
2084 if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2085 slgt_irq_off(info, IRQ_DCD);
2086 return;
2088 info->icount.dcd++;
2089 #if SYNCLINK_GENERIC_HDLC
2090 if (info->netcount) {
2091 if (info->signals & SerialSignal_DCD)
2092 netif_carrier_on(info->netdev);
2093 else
2094 netif_carrier_off(info->netdev);
2096 #endif
2097 wake_up_interruptible(&info->status_event_wait_q);
2098 wake_up_interruptible(&info->event_wait_q);
2099 info->pending_bh |= BH_STATUS;
2101 if (info->port.flags & ASYNC_CHECK_CD) {
2102 if (info->signals & SerialSignal_DCD)
2103 wake_up_interruptible(&info->port.open_wait);
2104 else {
2105 if (info->port.tty)
2106 tty_hangup(info->port.tty);
2111 static void ri_change(struct slgt_info *info, unsigned short status)
2113 if (status & BIT0) {
2114 info->signals |= SerialSignal_RI;
2115 info->input_signal_events.ri_up++;
2116 } else {
2117 info->signals &= ~SerialSignal_RI;
2118 info->input_signal_events.ri_down++;
2120 DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
2121 if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2122 slgt_irq_off(info, IRQ_RI);
2123 return;
2125 info->icount.rng++;
2126 wake_up_interruptible(&info->status_event_wait_q);
2127 wake_up_interruptible(&info->event_wait_q);
2128 info->pending_bh |= BH_STATUS;
2131 static void isr_rxdata(struct slgt_info *info)
2133 unsigned int count = info->rbuf_fill_count;
2134 unsigned int i = info->rbuf_fill_index;
2135 unsigned short reg;
2137 while (rd_reg16(info, SSR) & IRQ_RXDATA) {
2138 reg = rd_reg16(info, RDR);
2139 DBGISR(("isr_rxdata %s RDR=%04X\n", info->device_name, reg));
2140 if (desc_complete(info->rbufs[i])) {
2141 /* all buffers full */
2142 rx_stop(info);
2143 info->rx_restart = 1;
2144 continue;
2146 info->rbufs[i].buf[count++] = (unsigned char)reg;
2147 /* async mode saves status byte to buffer for each data byte */
2148 if (info->params.mode == MGSL_MODE_ASYNC)
2149 info->rbufs[i].buf[count++] = (unsigned char)(reg >> 8);
2150 if (count == info->rbuf_fill_level || (reg & BIT10)) {
2151 /* buffer full or end of frame */
2152 set_desc_count(info->rbufs[i], count);
2153 set_desc_status(info->rbufs[i], BIT15 | (reg >> 8));
2154 info->rbuf_fill_count = count = 0;
2155 if (++i == info->rbuf_count)
2156 i = 0;
2157 info->pending_bh |= BH_RECEIVE;
2161 info->rbuf_fill_index = i;
2162 info->rbuf_fill_count = count;
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) {
2177 if (info->tx_active)
2178 isr_txeom(info, status);
2180 if (info->rx_pio && (status & IRQ_RXDATA))
2181 isr_rxdata(info);
2182 if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
2183 info->icount.brk++;
2184 /* process break detection if tty control allows */
2185 if (info->port.tty) {
2186 if (!(status & info->ignore_status_mask)) {
2187 if (info->read_status_mask & MASK_BREAK) {
2188 tty_insert_flip_char(info->port.tty, 0, TTY_BREAK);
2189 if (info->port.flags & ASYNC_SAK)
2190 do_SAK(info->port.tty);
2195 } else {
2196 if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
2197 isr_txeom(info, status);
2198 if (info->rx_pio && (status & IRQ_RXDATA))
2199 isr_rxdata(info);
2200 if (status & IRQ_RXIDLE) {
2201 if (status & RXIDLE)
2202 info->icount.rxidle++;
2203 else
2204 info->icount.exithunt++;
2205 wake_up_interruptible(&info->event_wait_q);
2208 if (status & IRQ_RXOVER)
2209 rx_start(info);
2212 if (status & IRQ_DSR)
2213 dsr_change(info, status);
2214 if (status & IRQ_CTS)
2215 cts_change(info, status);
2216 if (status & IRQ_DCD)
2217 dcd_change(info, status);
2218 if (status & IRQ_RI)
2219 ri_change(info, status);
2222 static void isr_rdma(struct slgt_info *info)
2224 unsigned int status = rd_reg32(info, RDCSR);
2226 DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
2228 /* RDCSR (rx DMA control/status)
2230 * 31..07 reserved
2231 * 06 save status byte to DMA buffer
2232 * 05 error
2233 * 04 eol (end of list)
2234 * 03 eob (end of buffer)
2235 * 02 IRQ enable
2236 * 01 reset
2237 * 00 enable
2239 wr_reg32(info, RDCSR, status); /* clear pending */
2241 if (status & (BIT5 + BIT4)) {
2242 DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
2243 info->rx_restart = true;
2245 info->pending_bh |= BH_RECEIVE;
2248 static void isr_tdma(struct slgt_info *info)
2250 unsigned int status = rd_reg32(info, TDCSR);
2252 DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
2254 /* TDCSR (tx DMA control/status)
2256 * 31..06 reserved
2257 * 05 error
2258 * 04 eol (end of list)
2259 * 03 eob (end of buffer)
2260 * 02 IRQ enable
2261 * 01 reset
2262 * 00 enable
2264 wr_reg32(info, TDCSR, status); /* clear pending */
2266 if (status & (BIT5 + BIT4 + BIT3)) {
2267 // another transmit buffer has completed
2268 // run bottom half to get more send data from user
2269 info->pending_bh |= BH_TRANSMIT;
2274 * return true if there are unsent tx DMA buffers, otherwise false
2276 * if there are unsent buffers then info->tbuf_start
2277 * is set to index of first unsent buffer
2279 static bool unsent_tbufs(struct slgt_info *info)
2281 unsigned int i = info->tbuf_current;
2282 bool rc = false;
2285 * search backwards from last loaded buffer (precedes tbuf_current)
2286 * for first unsent buffer (desc_count > 0)
2289 do {
2290 if (i)
2291 i--;
2292 else
2293 i = info->tbuf_count - 1;
2294 if (!desc_count(info->tbufs[i]))
2295 break;
2296 info->tbuf_start = i;
2297 rc = true;
2298 } while (i != info->tbuf_current);
2300 return rc;
2303 static void isr_txeom(struct slgt_info *info, unsigned short status)
2305 DBGISR(("%s txeom status=%04x\n", info->device_name, status));
2307 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
2308 tdma_reset(info);
2309 if (status & IRQ_TXUNDER) {
2310 unsigned short val = rd_reg16(info, TCR);
2311 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
2312 wr_reg16(info, TCR, val); /* clear reset bit */
2315 if (info->tx_active) {
2316 if (info->params.mode != MGSL_MODE_ASYNC) {
2317 if (status & IRQ_TXUNDER)
2318 info->icount.txunder++;
2319 else if (status & IRQ_TXIDLE)
2320 info->icount.txok++;
2323 if (unsent_tbufs(info)) {
2324 tx_start(info);
2325 update_tx_timer(info);
2326 return;
2328 info->tx_active = false;
2330 del_timer(&info->tx_timer);
2332 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
2333 info->signals &= ~SerialSignal_RTS;
2334 info->drop_rts_on_tx_done = false;
2335 set_signals(info);
2338 #if SYNCLINK_GENERIC_HDLC
2339 if (info->netcount)
2340 hdlcdev_tx_done(info);
2341 else
2342 #endif
2344 if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
2345 tx_stop(info);
2346 return;
2348 info->pending_bh |= BH_TRANSMIT;
2353 static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
2355 struct cond_wait *w, *prev;
2357 /* wake processes waiting for specific transitions */
2358 for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
2359 if (w->data & changed) {
2360 w->data = state;
2361 wake_up_interruptible(&w->q);
2362 if (prev != NULL)
2363 prev->next = w->next;
2364 else
2365 info->gpio_wait_q = w->next;
2366 } else
2367 prev = w;
2371 /* interrupt service routine
2373 * irq interrupt number
2374 * dev_id device ID supplied during interrupt registration
2376 static irqreturn_t slgt_interrupt(int dummy, void *dev_id)
2378 struct slgt_info *info = dev_id;
2379 unsigned int gsr;
2380 unsigned int i;
2382 DBGISR(("slgt_interrupt irq=%d entry\n", info->irq_level));
2384 while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
2385 DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
2386 info->irq_occurred = true;
2387 for(i=0; i < info->port_count ; i++) {
2388 if (info->port_array[i] == NULL)
2389 continue;
2390 spin_lock(&info->port_array[i]->lock);
2391 if (gsr & (BIT8 << i))
2392 isr_serial(info->port_array[i]);
2393 if (gsr & (BIT16 << (i*2)))
2394 isr_rdma(info->port_array[i]);
2395 if (gsr & (BIT17 << (i*2)))
2396 isr_tdma(info->port_array[i]);
2397 spin_unlock(&info->port_array[i]->lock);
2401 if (info->gpio_present) {
2402 unsigned int state;
2403 unsigned int changed;
2404 spin_lock(&info->lock);
2405 while ((changed = rd_reg32(info, IOSR)) != 0) {
2406 DBGISR(("%s iosr=%08x\n", info->device_name, changed));
2407 /* read latched state of GPIO signals */
2408 state = rd_reg32(info, IOVR);
2409 /* clear pending GPIO interrupt bits */
2410 wr_reg32(info, IOSR, changed);
2411 for (i=0 ; i < info->port_count ; i++) {
2412 if (info->port_array[i] != NULL)
2413 isr_gpio(info->port_array[i], changed, state);
2416 spin_unlock(&info->lock);
2419 for(i=0; i < info->port_count ; i++) {
2420 struct slgt_info *port = info->port_array[i];
2421 if (port == NULL)
2422 continue;
2423 spin_lock(&port->lock);
2424 if ((port->port.count || port->netcount) &&
2425 port->pending_bh && !port->bh_running &&
2426 !port->bh_requested) {
2427 DBGISR(("%s bh queued\n", port->device_name));
2428 schedule_work(&port->task);
2429 port->bh_requested = true;
2431 spin_unlock(&port->lock);
2434 DBGISR(("slgt_interrupt irq=%d exit\n", info->irq_level));
2435 return IRQ_HANDLED;
2438 static int startup(struct slgt_info *info)
2440 DBGINFO(("%s startup\n", info->device_name));
2442 if (info->port.flags & ASYNC_INITIALIZED)
2443 return 0;
2445 if (!info->tx_buf) {
2446 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
2447 if (!info->tx_buf) {
2448 DBGERR(("%s can't allocate tx buffer\n", info->device_name));
2449 return -ENOMEM;
2453 info->pending_bh = 0;
2455 memset(&info->icount, 0, sizeof(info->icount));
2457 /* program hardware for current parameters */
2458 change_params(info);
2460 if (info->port.tty)
2461 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
2463 info->port.flags |= ASYNC_INITIALIZED;
2465 return 0;
2469 * called by close() and hangup() to shutdown hardware
2471 static void shutdown(struct slgt_info *info)
2473 unsigned long flags;
2475 if (!(info->port.flags & ASYNC_INITIALIZED))
2476 return;
2478 DBGINFO(("%s shutdown\n", info->device_name));
2480 /* clear status wait queue because status changes */
2481 /* can't happen after shutting down the hardware */
2482 wake_up_interruptible(&info->status_event_wait_q);
2483 wake_up_interruptible(&info->event_wait_q);
2485 del_timer_sync(&info->tx_timer);
2486 del_timer_sync(&info->rx_timer);
2488 kfree(info->tx_buf);
2489 info->tx_buf = NULL;
2491 spin_lock_irqsave(&info->lock,flags);
2493 tx_stop(info);
2494 rx_stop(info);
2496 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
2498 if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
2499 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2500 set_signals(info);
2503 flush_cond_wait(&info->gpio_wait_q);
2505 spin_unlock_irqrestore(&info->lock,flags);
2507 if (info->port.tty)
2508 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
2510 info->port.flags &= ~ASYNC_INITIALIZED;
2513 static void program_hw(struct slgt_info *info)
2515 unsigned long flags;
2517 spin_lock_irqsave(&info->lock,flags);
2519 rx_stop(info);
2520 tx_stop(info);
2522 if (info->params.mode != MGSL_MODE_ASYNC ||
2523 info->netcount)
2524 sync_mode(info);
2525 else
2526 async_mode(info);
2528 set_signals(info);
2530 info->dcd_chkcount = 0;
2531 info->cts_chkcount = 0;
2532 info->ri_chkcount = 0;
2533 info->dsr_chkcount = 0;
2535 slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR | IRQ_RI);
2536 get_signals(info);
2538 if (info->netcount ||
2539 (info->port.tty && info->port.tty->termios->c_cflag & CREAD))
2540 rx_start(info);
2542 spin_unlock_irqrestore(&info->lock,flags);
2546 * reconfigure adapter based on new parameters
2548 static void change_params(struct slgt_info *info)
2550 unsigned cflag;
2551 int bits_per_char;
2553 if (!info->port.tty || !info->port.tty->termios)
2554 return;
2555 DBGINFO(("%s change_params\n", info->device_name));
2557 cflag = info->port.tty->termios->c_cflag;
2559 /* if B0 rate (hangup) specified then negate DTR and RTS */
2560 /* otherwise assert DTR and RTS */
2561 if (cflag & CBAUD)
2562 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
2563 else
2564 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2566 /* byte size and parity */
2568 switch (cflag & CSIZE) {
2569 case CS5: info->params.data_bits = 5; break;
2570 case CS6: info->params.data_bits = 6; break;
2571 case CS7: info->params.data_bits = 7; break;
2572 case CS8: info->params.data_bits = 8; break;
2573 default: info->params.data_bits = 7; break;
2576 info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
2578 if (cflag & PARENB)
2579 info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
2580 else
2581 info->params.parity = ASYNC_PARITY_NONE;
2583 /* calculate number of jiffies to transmit a full
2584 * FIFO (32 bytes) at specified data rate
2586 bits_per_char = info->params.data_bits +
2587 info->params.stop_bits + 1;
2589 info->params.data_rate = tty_get_baud_rate(info->port.tty);
2591 if (info->params.data_rate) {
2592 info->timeout = (32*HZ*bits_per_char) /
2593 info->params.data_rate;
2595 info->timeout += HZ/50; /* Add .02 seconds of slop */
2597 if (cflag & CRTSCTS)
2598 info->port.flags |= ASYNC_CTS_FLOW;
2599 else
2600 info->port.flags &= ~ASYNC_CTS_FLOW;
2602 if (cflag & CLOCAL)
2603 info->port.flags &= ~ASYNC_CHECK_CD;
2604 else
2605 info->port.flags |= ASYNC_CHECK_CD;
2607 /* process tty input control flags */
2609 info->read_status_mask = IRQ_RXOVER;
2610 if (I_INPCK(info->port.tty))
2611 info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
2612 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
2613 info->read_status_mask |= MASK_BREAK;
2614 if (I_IGNPAR(info->port.tty))
2615 info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
2616 if (I_IGNBRK(info->port.tty)) {
2617 info->ignore_status_mask |= MASK_BREAK;
2618 /* If ignoring parity and break indicators, ignore
2619 * overruns too. (For real raw support).
2621 if (I_IGNPAR(info->port.tty))
2622 info->ignore_status_mask |= MASK_OVERRUN;
2625 program_hw(info);
2628 static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
2630 DBGINFO(("%s get_stats\n", info->device_name));
2631 if (!user_icount) {
2632 memset(&info->icount, 0, sizeof(info->icount));
2633 } else {
2634 if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
2635 return -EFAULT;
2637 return 0;
2640 static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
2642 DBGINFO(("%s get_params\n", info->device_name));
2643 if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
2644 return -EFAULT;
2645 return 0;
2648 static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
2650 unsigned long flags;
2651 MGSL_PARAMS tmp_params;
2653 DBGINFO(("%s set_params\n", info->device_name));
2654 if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
2655 return -EFAULT;
2657 spin_lock_irqsave(&info->lock, flags);
2658 if (tmp_params.mode == MGSL_MODE_BASE_CLOCK)
2659 info->base_clock = tmp_params.clock_speed;
2660 else
2661 memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
2662 spin_unlock_irqrestore(&info->lock, flags);
2664 program_hw(info);
2666 return 0;
2669 static int get_txidle(struct slgt_info *info, int __user *idle_mode)
2671 DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
2672 if (put_user(info->idle_mode, idle_mode))
2673 return -EFAULT;
2674 return 0;
2677 static int set_txidle(struct slgt_info *info, int idle_mode)
2679 unsigned long flags;
2680 DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
2681 spin_lock_irqsave(&info->lock,flags);
2682 info->idle_mode = idle_mode;
2683 if (info->params.mode != MGSL_MODE_ASYNC)
2684 tx_set_idle(info);
2685 spin_unlock_irqrestore(&info->lock,flags);
2686 return 0;
2689 static int tx_enable(struct slgt_info *info, int enable)
2691 unsigned long flags;
2692 DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
2693 spin_lock_irqsave(&info->lock,flags);
2694 if (enable) {
2695 if (!info->tx_enabled)
2696 tx_start(info);
2697 } else {
2698 if (info->tx_enabled)
2699 tx_stop(info);
2701 spin_unlock_irqrestore(&info->lock,flags);
2702 return 0;
2706 * abort transmit HDLC frame
2708 static int tx_abort(struct slgt_info *info)
2710 unsigned long flags;
2711 DBGINFO(("%s tx_abort\n", info->device_name));
2712 spin_lock_irqsave(&info->lock,flags);
2713 tdma_reset(info);
2714 spin_unlock_irqrestore(&info->lock,flags);
2715 return 0;
2718 static int rx_enable(struct slgt_info *info, int enable)
2720 unsigned long flags;
2721 unsigned int rbuf_fill_level;
2722 DBGINFO(("%s rx_enable(%08x)\n", info->device_name, enable));
2723 spin_lock_irqsave(&info->lock,flags);
2725 * enable[31..16] = receive DMA buffer fill level
2726 * 0 = noop (leave fill level unchanged)
2727 * fill level must be multiple of 4 and <= buffer size
2729 rbuf_fill_level = ((unsigned int)enable) >> 16;
2730 if (rbuf_fill_level) {
2731 if ((rbuf_fill_level > DMABUFSIZE) || (rbuf_fill_level % 4)) {
2732 spin_unlock_irqrestore(&info->lock, flags);
2733 return -EINVAL;
2735 info->rbuf_fill_level = rbuf_fill_level;
2736 if (rbuf_fill_level < 128)
2737 info->rx_pio = 1; /* PIO mode */
2738 else
2739 info->rx_pio = 0; /* DMA mode */
2740 rx_stop(info); /* restart receiver to use new fill level */
2744 * enable[1..0] = receiver enable command
2745 * 0 = disable
2746 * 1 = enable
2747 * 2 = enable or force hunt mode if already enabled
2749 enable &= 3;
2750 if (enable) {
2751 if (!info->rx_enabled)
2752 rx_start(info);
2753 else if (enable == 2) {
2754 /* force hunt mode (write 1 to RCR[3]) */
2755 wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
2757 } else {
2758 if (info->rx_enabled)
2759 rx_stop(info);
2761 spin_unlock_irqrestore(&info->lock,flags);
2762 return 0;
2766 * wait for specified event to occur
2768 static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
2770 unsigned long flags;
2771 int s;
2772 int rc=0;
2773 struct mgsl_icount cprev, cnow;
2774 int events;
2775 int mask;
2776 struct _input_signal_events oldsigs, newsigs;
2777 DECLARE_WAITQUEUE(wait, current);
2779 if (get_user(mask, mask_ptr))
2780 return -EFAULT;
2782 DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
2784 spin_lock_irqsave(&info->lock,flags);
2786 /* return immediately if state matches requested events */
2787 get_signals(info);
2788 s = info->signals;
2790 events = mask &
2791 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2792 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2793 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2794 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2795 if (events) {
2796 spin_unlock_irqrestore(&info->lock,flags);
2797 goto exit;
2800 /* save current irq counts */
2801 cprev = info->icount;
2802 oldsigs = info->input_signal_events;
2804 /* enable hunt and idle irqs if needed */
2805 if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
2806 unsigned short val = rd_reg16(info, SCR);
2807 if (!(val & IRQ_RXIDLE))
2808 wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
2811 set_current_state(TASK_INTERRUPTIBLE);
2812 add_wait_queue(&info->event_wait_q, &wait);
2814 spin_unlock_irqrestore(&info->lock,flags);
2816 for(;;) {
2817 schedule();
2818 if (signal_pending(current)) {
2819 rc = -ERESTARTSYS;
2820 break;
2823 /* get current irq counts */
2824 spin_lock_irqsave(&info->lock,flags);
2825 cnow = info->icount;
2826 newsigs = info->input_signal_events;
2827 set_current_state(TASK_INTERRUPTIBLE);
2828 spin_unlock_irqrestore(&info->lock,flags);
2830 /* if no change, wait aborted for some reason */
2831 if (newsigs.dsr_up == oldsigs.dsr_up &&
2832 newsigs.dsr_down == oldsigs.dsr_down &&
2833 newsigs.dcd_up == oldsigs.dcd_up &&
2834 newsigs.dcd_down == oldsigs.dcd_down &&
2835 newsigs.cts_up == oldsigs.cts_up &&
2836 newsigs.cts_down == oldsigs.cts_down &&
2837 newsigs.ri_up == oldsigs.ri_up &&
2838 newsigs.ri_down == oldsigs.ri_down &&
2839 cnow.exithunt == cprev.exithunt &&
2840 cnow.rxidle == cprev.rxidle) {
2841 rc = -EIO;
2842 break;
2845 events = mask &
2846 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2847 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2848 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2849 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2850 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2851 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2852 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2853 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2854 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2855 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2856 if (events)
2857 break;
2859 cprev = cnow;
2860 oldsigs = newsigs;
2863 remove_wait_queue(&info->event_wait_q, &wait);
2864 set_current_state(TASK_RUNNING);
2867 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2868 spin_lock_irqsave(&info->lock,flags);
2869 if (!waitqueue_active(&info->event_wait_q)) {
2870 /* disable enable exit hunt mode/idle rcvd IRQs */
2871 wr_reg16(info, SCR,
2872 (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
2874 spin_unlock_irqrestore(&info->lock,flags);
2876 exit:
2877 if (rc == 0)
2878 rc = put_user(events, mask_ptr);
2879 return rc;
2882 static int get_interface(struct slgt_info *info, int __user *if_mode)
2884 DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
2885 if (put_user(info->if_mode, if_mode))
2886 return -EFAULT;
2887 return 0;
2890 static int set_interface(struct slgt_info *info, int if_mode)
2892 unsigned long flags;
2893 unsigned short val;
2895 DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
2896 spin_lock_irqsave(&info->lock,flags);
2897 info->if_mode = if_mode;
2899 msc_set_vcr(info);
2901 /* TCR (tx control) 07 1=RTS driver control */
2902 val = rd_reg16(info, TCR);
2903 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
2904 val |= BIT7;
2905 else
2906 val &= ~BIT7;
2907 wr_reg16(info, TCR, val);
2909 spin_unlock_irqrestore(&info->lock,flags);
2910 return 0;
2913 static int get_xsync(struct slgt_info *info, int __user *xsync)
2915 DBGINFO(("%s get_xsync=%x\n", info->device_name, info->xsync));
2916 if (put_user(info->xsync, xsync))
2917 return -EFAULT;
2918 return 0;
2922 * set extended sync pattern (1 to 4 bytes) for extended sync mode
2924 * sync pattern is contained in least significant bytes of value
2925 * most significant byte of sync pattern is oldest (1st sent/detected)
2927 static int set_xsync(struct slgt_info *info, int xsync)
2929 unsigned long flags;
2931 DBGINFO(("%s set_xsync=%x)\n", info->device_name, xsync));
2932 spin_lock_irqsave(&info->lock, flags);
2933 info->xsync = xsync;
2934 wr_reg32(info, XSR, xsync);
2935 spin_unlock_irqrestore(&info->lock, flags);
2936 return 0;
2939 static int get_xctrl(struct slgt_info *info, int __user *xctrl)
2941 DBGINFO(("%s get_xctrl=%x\n", info->device_name, info->xctrl));
2942 if (put_user(info->xctrl, xctrl))
2943 return -EFAULT;
2944 return 0;
2948 * set extended control options
2950 * xctrl[31:19] reserved, must be zero
2951 * xctrl[18:17] extended sync pattern length in bytes
2952 * 00 = 1 byte in xsr[7:0]
2953 * 01 = 2 bytes in xsr[15:0]
2954 * 10 = 3 bytes in xsr[23:0]
2955 * 11 = 4 bytes in xsr[31:0]
2956 * xctrl[16] 1 = enable terminal count, 0=disabled
2957 * xctrl[15:0] receive terminal count for fixed length packets
2958 * value is count minus one (0 = 1 byte packet)
2959 * when terminal count is reached, receiver
2960 * automatically returns to hunt mode and receive
2961 * FIFO contents are flushed to DMA buffers with
2962 * end of frame (EOF) status
2964 static int set_xctrl(struct slgt_info *info, int xctrl)
2966 unsigned long flags;
2968 DBGINFO(("%s set_xctrl=%x)\n", info->device_name, xctrl));
2969 spin_lock_irqsave(&info->lock, flags);
2970 info->xctrl = xctrl;
2971 wr_reg32(info, XCR, xctrl);
2972 spin_unlock_irqrestore(&info->lock, flags);
2973 return 0;
2977 * set general purpose IO pin state and direction
2979 * user_gpio fields:
2980 * state each bit indicates a pin state
2981 * smask set bit indicates pin state to set
2982 * dir each bit indicates a pin direction (0=input, 1=output)
2983 * dmask set bit indicates pin direction to set
2985 static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2987 unsigned long flags;
2988 struct gpio_desc gpio;
2989 __u32 data;
2991 if (!info->gpio_present)
2992 return -EINVAL;
2993 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2994 return -EFAULT;
2995 DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
2996 info->device_name, gpio.state, gpio.smask,
2997 gpio.dir, gpio.dmask));
2999 spin_lock_irqsave(&info->port_array[0]->lock, flags);
3000 if (gpio.dmask) {
3001 data = rd_reg32(info, IODR);
3002 data |= gpio.dmask & gpio.dir;
3003 data &= ~(gpio.dmask & ~gpio.dir);
3004 wr_reg32(info, IODR, data);
3006 if (gpio.smask) {
3007 data = rd_reg32(info, IOVR);
3008 data |= gpio.smask & gpio.state;
3009 data &= ~(gpio.smask & ~gpio.state);
3010 wr_reg32(info, IOVR, data);
3012 spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
3014 return 0;
3018 * get general purpose IO pin state and direction
3020 static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
3022 struct gpio_desc gpio;
3023 if (!info->gpio_present)
3024 return -EINVAL;
3025 gpio.state = rd_reg32(info, IOVR);
3026 gpio.smask = 0xffffffff;
3027 gpio.dir = rd_reg32(info, IODR);
3028 gpio.dmask = 0xffffffff;
3029 if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
3030 return -EFAULT;
3031 DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
3032 info->device_name, gpio.state, gpio.dir));
3033 return 0;
3037 * conditional wait facility
3039 static void init_cond_wait(struct cond_wait *w, unsigned int data)
3041 init_waitqueue_head(&w->q);
3042 init_waitqueue_entry(&w->wait, current);
3043 w->data = data;
3046 static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
3048 set_current_state(TASK_INTERRUPTIBLE);
3049 add_wait_queue(&w->q, &w->wait);
3050 w->next = *head;
3051 *head = w;
3054 static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
3056 struct cond_wait *w, *prev;
3057 remove_wait_queue(&cw->q, &cw->wait);
3058 set_current_state(TASK_RUNNING);
3059 for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
3060 if (w == cw) {
3061 if (prev != NULL)
3062 prev->next = w->next;
3063 else
3064 *head = w->next;
3065 break;
3070 static void flush_cond_wait(struct cond_wait **head)
3072 while (*head != NULL) {
3073 wake_up_interruptible(&(*head)->q);
3074 *head = (*head)->next;
3079 * wait for general purpose I/O pin(s) to enter specified state
3081 * user_gpio fields:
3082 * state - bit indicates target pin state
3083 * smask - set bit indicates watched pin
3085 * The wait ends when at least one watched pin enters the specified
3086 * state. When 0 (no error) is returned, user_gpio->state is set to the
3087 * state of all GPIO pins when the wait ends.
3089 * Note: Each pin may be a dedicated input, dedicated output, or
3090 * configurable input/output. The number and configuration of pins
3091 * varies with the specific adapter model. Only input pins (dedicated
3092 * or configured) can be monitored with this function.
3094 static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
3096 unsigned long flags;
3097 int rc = 0;
3098 struct gpio_desc gpio;
3099 struct cond_wait wait;
3100 u32 state;
3102 if (!info->gpio_present)
3103 return -EINVAL;
3104 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
3105 return -EFAULT;
3106 DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
3107 info->device_name, gpio.state, gpio.smask));
3108 /* ignore output pins identified by set IODR bit */
3109 if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
3110 return -EINVAL;
3111 init_cond_wait(&wait, gpio.smask);
3113 spin_lock_irqsave(&info->port_array[0]->lock, flags);
3114 /* enable interrupts for watched pins */
3115 wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
3116 /* get current pin states */
3117 state = rd_reg32(info, IOVR);
3119 if (gpio.smask & ~(state ^ gpio.state)) {
3120 /* already in target state */
3121 gpio.state = state;
3122 } else {
3123 /* wait for target state */
3124 add_cond_wait(&info->gpio_wait_q, &wait);
3125 spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
3126 schedule();
3127 if (signal_pending(current))
3128 rc = -ERESTARTSYS;
3129 else
3130 gpio.state = wait.data;
3131 spin_lock_irqsave(&info->port_array[0]->lock, flags);
3132 remove_cond_wait(&info->gpio_wait_q, &wait);
3135 /* disable all GPIO interrupts if no waiting processes */
3136 if (info->gpio_wait_q == NULL)
3137 wr_reg32(info, IOER, 0);
3138 spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
3140 if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
3141 rc = -EFAULT;
3142 return rc;
3145 static int modem_input_wait(struct slgt_info *info,int arg)
3147 unsigned long flags;
3148 int rc;
3149 struct mgsl_icount cprev, cnow;
3150 DECLARE_WAITQUEUE(wait, current);
3152 /* save current irq counts */
3153 spin_lock_irqsave(&info->lock,flags);
3154 cprev = info->icount;
3155 add_wait_queue(&info->status_event_wait_q, &wait);
3156 set_current_state(TASK_INTERRUPTIBLE);
3157 spin_unlock_irqrestore(&info->lock,flags);
3159 for(;;) {
3160 schedule();
3161 if (signal_pending(current)) {
3162 rc = -ERESTARTSYS;
3163 break;
3166 /* get new irq counts */
3167 spin_lock_irqsave(&info->lock,flags);
3168 cnow = info->icount;
3169 set_current_state(TASK_INTERRUPTIBLE);
3170 spin_unlock_irqrestore(&info->lock,flags);
3172 /* if no change, wait aborted for some reason */
3173 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
3174 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
3175 rc = -EIO;
3176 break;
3179 /* check for change in caller specified modem input */
3180 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
3181 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
3182 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
3183 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
3184 rc = 0;
3185 break;
3188 cprev = cnow;
3190 remove_wait_queue(&info->status_event_wait_q, &wait);
3191 set_current_state(TASK_RUNNING);
3192 return rc;
3196 * return state of serial control and status signals
3198 static int tiocmget(struct tty_struct *tty, struct file *file)
3200 struct slgt_info *info = tty->driver_data;
3201 unsigned int result;
3202 unsigned long flags;
3204 spin_lock_irqsave(&info->lock,flags);
3205 get_signals(info);
3206 spin_unlock_irqrestore(&info->lock,flags);
3208 result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
3209 ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
3210 ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
3211 ((info->signals & SerialSignal_RI) ? TIOCM_RNG:0) +
3212 ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
3213 ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
3215 DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
3216 return result;
3220 * set modem control signals (DTR/RTS)
3222 * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
3223 * TIOCMSET = set/clear signal values
3224 * value bit mask for command
3226 static int tiocmset(struct tty_struct *tty, struct file *file,
3227 unsigned int set, unsigned int clear)
3229 struct slgt_info *info = tty->driver_data;
3230 unsigned long flags;
3232 DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
3234 if (set & TIOCM_RTS)
3235 info->signals |= SerialSignal_RTS;
3236 if (set & TIOCM_DTR)
3237 info->signals |= SerialSignal_DTR;
3238 if (clear & TIOCM_RTS)
3239 info->signals &= ~SerialSignal_RTS;
3240 if (clear & TIOCM_DTR)
3241 info->signals &= ~SerialSignal_DTR;
3243 spin_lock_irqsave(&info->lock,flags);
3244 set_signals(info);
3245 spin_unlock_irqrestore(&info->lock,flags);
3246 return 0;
3249 static int carrier_raised(struct tty_port *port)
3251 unsigned long flags;
3252 struct slgt_info *info = container_of(port, struct slgt_info, port);
3254 spin_lock_irqsave(&info->lock,flags);
3255 get_signals(info);
3256 spin_unlock_irqrestore(&info->lock,flags);
3257 return (info->signals & SerialSignal_DCD) ? 1 : 0;
3260 static void dtr_rts(struct tty_port *port, int on)
3262 unsigned long flags;
3263 struct slgt_info *info = container_of(port, struct slgt_info, port);
3265 spin_lock_irqsave(&info->lock,flags);
3266 if (on)
3267 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
3268 else
3269 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
3270 set_signals(info);
3271 spin_unlock_irqrestore(&info->lock,flags);
3276 * block current process until the device is ready to open
3278 static int block_til_ready(struct tty_struct *tty, struct file *filp,
3279 struct slgt_info *info)
3281 DECLARE_WAITQUEUE(wait, current);
3282 int retval;
3283 bool do_clocal = false;
3284 bool extra_count = false;
3285 unsigned long flags;
3286 int cd;
3287 struct tty_port *port = &info->port;
3289 DBGINFO(("%s block_til_ready\n", tty->driver->name));
3291 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3292 /* nonblock mode is set or port is not enabled */
3293 port->flags |= ASYNC_NORMAL_ACTIVE;
3294 return 0;
3297 if (tty->termios->c_cflag & CLOCAL)
3298 do_clocal = true;
3300 /* Wait for carrier detect and the line to become
3301 * free (i.e., not in use by the callout). While we are in
3302 * this loop, port->count is dropped by one, so that
3303 * close() knows when to free things. We restore it upon
3304 * exit, either normal or abnormal.
3307 retval = 0;
3308 add_wait_queue(&port->open_wait, &wait);
3310 spin_lock_irqsave(&info->lock, flags);
3311 if (!tty_hung_up_p(filp)) {
3312 extra_count = true;
3313 port->count--;
3315 spin_unlock_irqrestore(&info->lock, flags);
3316 port->blocked_open++;
3318 while (1) {
3319 if ((tty->termios->c_cflag & CBAUD))
3320 tty_port_raise_dtr_rts(port);
3322 set_current_state(TASK_INTERRUPTIBLE);
3324 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)){
3325 retval = (port->flags & ASYNC_HUP_NOTIFY) ?
3326 -EAGAIN : -ERESTARTSYS;
3327 break;
3330 cd = tty_port_carrier_raised(port);
3332 if (!(port->flags & ASYNC_CLOSING) && (do_clocal || cd ))
3333 break;
3335 if (signal_pending(current)) {
3336 retval = -ERESTARTSYS;
3337 break;
3340 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
3341 tty_unlock();
3342 schedule();
3343 tty_lock();
3346 set_current_state(TASK_RUNNING);
3347 remove_wait_queue(&port->open_wait, &wait);
3349 if (extra_count)
3350 port->count++;
3351 port->blocked_open--;
3353 if (!retval)
3354 port->flags |= ASYNC_NORMAL_ACTIVE;
3356 DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
3357 return retval;
3360 static int alloc_tmp_rbuf(struct slgt_info *info)
3362 info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
3363 if (info->tmp_rbuf == NULL)
3364 return -ENOMEM;
3365 return 0;
3368 static void free_tmp_rbuf(struct slgt_info *info)
3370 kfree(info->tmp_rbuf);
3371 info->tmp_rbuf = NULL;
3375 * allocate DMA descriptor lists.
3377 static int alloc_desc(struct slgt_info *info)
3379 unsigned int i;
3380 unsigned int pbufs;
3382 /* allocate memory to hold descriptor lists */
3383 info->bufs = pci_alloc_consistent(info->pdev, DESC_LIST_SIZE, &info->bufs_dma_addr);
3384 if (info->bufs == NULL)
3385 return -ENOMEM;
3387 memset(info->bufs, 0, DESC_LIST_SIZE);
3389 info->rbufs = (struct slgt_desc*)info->bufs;
3390 info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
3392 pbufs = (unsigned int)info->bufs_dma_addr;
3395 * Build circular lists of descriptors
3398 for (i=0; i < info->rbuf_count; i++) {
3399 /* physical address of this descriptor */
3400 info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
3402 /* physical address of next descriptor */
3403 if (i == info->rbuf_count - 1)
3404 info->rbufs[i].next = cpu_to_le32(pbufs);
3405 else
3406 info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
3407 set_desc_count(info->rbufs[i], DMABUFSIZE);
3410 for (i=0; i < info->tbuf_count; i++) {
3411 /* physical address of this descriptor */
3412 info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
3414 /* physical address of next descriptor */
3415 if (i == info->tbuf_count - 1)
3416 info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
3417 else
3418 info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
3421 return 0;
3424 static void free_desc(struct slgt_info *info)
3426 if (info->bufs != NULL) {
3427 pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
3428 info->bufs = NULL;
3429 info->rbufs = NULL;
3430 info->tbufs = NULL;
3434 static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3436 int i;
3437 for (i=0; i < count; i++) {
3438 if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
3439 return -ENOMEM;
3440 bufs[i].pbuf = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
3442 return 0;
3445 static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3447 int i;
3448 for (i=0; i < count; i++) {
3449 if (bufs[i].buf == NULL)
3450 continue;
3451 pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
3452 bufs[i].buf = NULL;
3456 static int alloc_dma_bufs(struct slgt_info *info)
3458 info->rbuf_count = 32;
3459 info->tbuf_count = 32;
3461 if (alloc_desc(info) < 0 ||
3462 alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
3463 alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
3464 alloc_tmp_rbuf(info) < 0) {
3465 DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
3466 return -ENOMEM;
3468 reset_rbufs(info);
3469 return 0;
3472 static void free_dma_bufs(struct slgt_info *info)
3474 if (info->bufs) {
3475 free_bufs(info, info->rbufs, info->rbuf_count);
3476 free_bufs(info, info->tbufs, info->tbuf_count);
3477 free_desc(info);
3479 free_tmp_rbuf(info);
3482 static int claim_resources(struct slgt_info *info)
3484 if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
3485 DBGERR(("%s reg addr conflict, addr=%08X\n",
3486 info->device_name, info->phys_reg_addr));
3487 info->init_error = DiagStatus_AddressConflict;
3488 goto errout;
3490 else
3491 info->reg_addr_requested = true;
3493 info->reg_addr = ioremap_nocache(info->phys_reg_addr, SLGT_REG_SIZE);
3494 if (!info->reg_addr) {
3495 DBGERR(("%s cant map device registers, addr=%08X\n",
3496 info->device_name, info->phys_reg_addr));
3497 info->init_error = DiagStatus_CantAssignPciResources;
3498 goto errout;
3500 return 0;
3502 errout:
3503 release_resources(info);
3504 return -ENODEV;
3507 static void release_resources(struct slgt_info *info)
3509 if (info->irq_requested) {
3510 free_irq(info->irq_level, info);
3511 info->irq_requested = false;
3514 if (info->reg_addr_requested) {
3515 release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
3516 info->reg_addr_requested = false;
3519 if (info->reg_addr) {
3520 iounmap(info->reg_addr);
3521 info->reg_addr = NULL;
3525 /* Add the specified device instance data structure to the
3526 * global linked list of devices and increment the device count.
3528 static void add_device(struct slgt_info *info)
3530 char *devstr;
3532 info->next_device = NULL;
3533 info->line = slgt_device_count;
3534 sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
3536 if (info->line < MAX_DEVICES) {
3537 if (maxframe[info->line])
3538 info->max_frame_size = maxframe[info->line];
3541 slgt_device_count++;
3543 if (!slgt_device_list)
3544 slgt_device_list = info;
3545 else {
3546 struct slgt_info *current_dev = slgt_device_list;
3547 while(current_dev->next_device)
3548 current_dev = current_dev->next_device;
3549 current_dev->next_device = info;
3552 if (info->max_frame_size < 4096)
3553 info->max_frame_size = 4096;
3554 else if (info->max_frame_size > 65535)
3555 info->max_frame_size = 65535;
3557 switch(info->pdev->device) {
3558 case SYNCLINK_GT_DEVICE_ID:
3559 devstr = "GT";
3560 break;
3561 case SYNCLINK_GT2_DEVICE_ID:
3562 devstr = "GT2";
3563 break;
3564 case SYNCLINK_GT4_DEVICE_ID:
3565 devstr = "GT4";
3566 break;
3567 case SYNCLINK_AC_DEVICE_ID:
3568 devstr = "AC";
3569 info->params.mode = MGSL_MODE_ASYNC;
3570 break;
3571 default:
3572 devstr = "(unknown model)";
3574 printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
3575 devstr, info->device_name, info->phys_reg_addr,
3576 info->irq_level, info->max_frame_size);
3578 #if SYNCLINK_GENERIC_HDLC
3579 hdlcdev_init(info);
3580 #endif
3583 static const struct tty_port_operations slgt_port_ops = {
3584 .carrier_raised = carrier_raised,
3585 .dtr_rts = dtr_rts,
3589 * allocate device instance structure, return NULL on failure
3591 static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
3593 struct slgt_info *info;
3595 info = kzalloc(sizeof(struct slgt_info), GFP_KERNEL);
3597 if (!info) {
3598 DBGERR(("%s device alloc failed adapter=%d port=%d\n",
3599 driver_name, adapter_num, port_num));
3600 } else {
3601 tty_port_init(&info->port);
3602 info->port.ops = &slgt_port_ops;
3603 info->magic = MGSL_MAGIC;
3604 INIT_WORK(&info->task, bh_handler);
3605 info->max_frame_size = 4096;
3606 info->base_clock = 14745600;
3607 info->rbuf_fill_level = DMABUFSIZE;
3608 info->port.close_delay = 5*HZ/10;
3609 info->port.closing_wait = 30*HZ;
3610 init_waitqueue_head(&info->status_event_wait_q);
3611 init_waitqueue_head(&info->event_wait_q);
3612 spin_lock_init(&info->netlock);
3613 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
3614 info->idle_mode = HDLC_TXIDLE_FLAGS;
3615 info->adapter_num = adapter_num;
3616 info->port_num = port_num;
3618 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
3619 setup_timer(&info->rx_timer, rx_timeout, (unsigned long)info);
3621 /* Copy configuration info to device instance data */
3622 info->pdev = pdev;
3623 info->irq_level = pdev->irq;
3624 info->phys_reg_addr = pci_resource_start(pdev,0);
3626 info->bus_type = MGSL_BUS_TYPE_PCI;
3627 info->irq_flags = IRQF_SHARED;
3629 info->init_error = -1; /* assume error, set to 0 on successful init */
3632 return info;
3635 static void device_init(int adapter_num, struct pci_dev *pdev)
3637 struct slgt_info *port_array[SLGT_MAX_PORTS];
3638 int i;
3639 int port_count = 1;
3641 if (pdev->device == SYNCLINK_GT2_DEVICE_ID)
3642 port_count = 2;
3643 else if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
3644 port_count = 4;
3646 /* allocate device instances for all ports */
3647 for (i=0; i < port_count; ++i) {
3648 port_array[i] = alloc_dev(adapter_num, i, pdev);
3649 if (port_array[i] == NULL) {
3650 for (--i; i >= 0; --i)
3651 kfree(port_array[i]);
3652 return;
3656 /* give copy of port_array to all ports and add to device list */
3657 for (i=0; i < port_count; ++i) {
3658 memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
3659 add_device(port_array[i]);
3660 port_array[i]->port_count = port_count;
3661 spin_lock_init(&port_array[i]->lock);
3664 /* Allocate and claim adapter resources */
3665 if (!claim_resources(port_array[0])) {
3667 alloc_dma_bufs(port_array[0]);
3669 /* copy resource information from first port to others */
3670 for (i = 1; i < port_count; ++i) {
3671 port_array[i]->irq_level = port_array[0]->irq_level;
3672 port_array[i]->reg_addr = port_array[0]->reg_addr;
3673 alloc_dma_bufs(port_array[i]);
3676 if (request_irq(port_array[0]->irq_level,
3677 slgt_interrupt,
3678 port_array[0]->irq_flags,
3679 port_array[0]->device_name,
3680 port_array[0]) < 0) {
3681 DBGERR(("%s request_irq failed IRQ=%d\n",
3682 port_array[0]->device_name,
3683 port_array[0]->irq_level));
3684 } else {
3685 port_array[0]->irq_requested = true;
3686 adapter_test(port_array[0]);
3687 for (i=1 ; i < port_count ; i++) {
3688 port_array[i]->init_error = port_array[0]->init_error;
3689 port_array[i]->gpio_present = port_array[0]->gpio_present;
3694 for (i=0; i < port_count; ++i)
3695 tty_register_device(serial_driver, port_array[i]->line, &(port_array[i]->pdev->dev));
3698 static int __devinit init_one(struct pci_dev *dev,
3699 const struct pci_device_id *ent)
3701 if (pci_enable_device(dev)) {
3702 printk("error enabling pci device %p\n", dev);
3703 return -EIO;
3705 pci_set_master(dev);
3706 device_init(slgt_device_count, dev);
3707 return 0;
3710 static void __devexit remove_one(struct pci_dev *dev)
3714 static const struct tty_operations ops = {
3715 .open = open,
3716 .close = close,
3717 .write = write,
3718 .put_char = put_char,
3719 .flush_chars = flush_chars,
3720 .write_room = write_room,
3721 .chars_in_buffer = chars_in_buffer,
3722 .flush_buffer = flush_buffer,
3723 .ioctl = ioctl,
3724 .compat_ioctl = slgt_compat_ioctl,
3725 .throttle = throttle,
3726 .unthrottle = unthrottle,
3727 .send_xchar = send_xchar,
3728 .break_ctl = set_break,
3729 .wait_until_sent = wait_until_sent,
3730 .set_termios = set_termios,
3731 .stop = tx_hold,
3732 .start = tx_release,
3733 .hangup = hangup,
3734 .tiocmget = tiocmget,
3735 .tiocmset = tiocmset,
3736 .get_icount = get_icount,
3737 .proc_fops = &synclink_gt_proc_fops,
3740 static void slgt_cleanup(void)
3742 int rc;
3743 struct slgt_info *info;
3744 struct slgt_info *tmp;
3746 printk(KERN_INFO "unload %s\n", driver_name);
3748 if (serial_driver) {
3749 for (info=slgt_device_list ; info != NULL ; info=info->next_device)
3750 tty_unregister_device(serial_driver, info->line);
3751 if ((rc = tty_unregister_driver(serial_driver)))
3752 DBGERR(("tty_unregister_driver error=%d\n", rc));
3753 put_tty_driver(serial_driver);
3756 /* reset devices */
3757 info = slgt_device_list;
3758 while(info) {
3759 reset_port(info);
3760 info = info->next_device;
3763 /* release devices */
3764 info = slgt_device_list;
3765 while(info) {
3766 #if SYNCLINK_GENERIC_HDLC
3767 hdlcdev_exit(info);
3768 #endif
3769 free_dma_bufs(info);
3770 free_tmp_rbuf(info);
3771 if (info->port_num == 0)
3772 release_resources(info);
3773 tmp = info;
3774 info = info->next_device;
3775 kfree(tmp);
3778 if (pci_registered)
3779 pci_unregister_driver(&pci_driver);
3783 * Driver initialization entry point.
3785 static int __init slgt_init(void)
3787 int rc;
3789 printk(KERN_INFO "%s\n", driver_name);
3791 serial_driver = alloc_tty_driver(MAX_DEVICES);
3792 if (!serial_driver) {
3793 printk("%s can't allocate tty driver\n", driver_name);
3794 return -ENOMEM;
3797 /* Initialize the tty_driver structure */
3799 serial_driver->owner = THIS_MODULE;
3800 serial_driver->driver_name = tty_driver_name;
3801 serial_driver->name = tty_dev_prefix;
3802 serial_driver->major = ttymajor;
3803 serial_driver->minor_start = 64;
3804 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3805 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3806 serial_driver->init_termios = tty_std_termios;
3807 serial_driver->init_termios.c_cflag =
3808 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3809 serial_driver->init_termios.c_ispeed = 9600;
3810 serial_driver->init_termios.c_ospeed = 9600;
3811 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3812 tty_set_operations(serial_driver, &ops);
3813 if ((rc = tty_register_driver(serial_driver)) < 0) {
3814 DBGERR(("%s can't register serial driver\n", driver_name));
3815 put_tty_driver(serial_driver);
3816 serial_driver = NULL;
3817 goto error;
3820 printk(KERN_INFO "%s, tty major#%d\n",
3821 driver_name, serial_driver->major);
3823 slgt_device_count = 0;
3824 if ((rc = pci_register_driver(&pci_driver)) < 0) {
3825 printk("%s pci_register_driver error=%d\n", driver_name, rc);
3826 goto error;
3828 pci_registered = true;
3830 if (!slgt_device_list)
3831 printk("%s no devices found\n",driver_name);
3833 return 0;
3835 error:
3836 slgt_cleanup();
3837 return rc;
3840 static void __exit slgt_exit(void)
3842 slgt_cleanup();
3845 module_init(slgt_init);
3846 module_exit(slgt_exit);
3849 * register access routines
3852 #define CALC_REGADDR() \
3853 unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
3854 if (addr >= 0x80) \
3855 reg_addr += (info->port_num) * 32; \
3856 else if (addr >= 0x40) \
3857 reg_addr += (info->port_num) * 16;
3859 static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
3861 CALC_REGADDR();
3862 return readb((void __iomem *)reg_addr);
3865 static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
3867 CALC_REGADDR();
3868 writeb(value, (void __iomem *)reg_addr);
3871 static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
3873 CALC_REGADDR();
3874 return readw((void __iomem *)reg_addr);
3877 static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
3879 CALC_REGADDR();
3880 writew(value, (void __iomem *)reg_addr);
3883 static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
3885 CALC_REGADDR();
3886 return readl((void __iomem *)reg_addr);
3889 static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
3891 CALC_REGADDR();
3892 writel(value, (void __iomem *)reg_addr);
3895 static void rdma_reset(struct slgt_info *info)
3897 unsigned int i;
3899 /* set reset bit */
3900 wr_reg32(info, RDCSR, BIT1);
3902 /* wait for enable bit cleared */
3903 for(i=0 ; i < 1000 ; i++)
3904 if (!(rd_reg32(info, RDCSR) & BIT0))
3905 break;
3908 static void tdma_reset(struct slgt_info *info)
3910 unsigned int i;
3912 /* set reset bit */
3913 wr_reg32(info, TDCSR, BIT1);
3915 /* wait for enable bit cleared */
3916 for(i=0 ; i < 1000 ; i++)
3917 if (!(rd_reg32(info, TDCSR) & BIT0))
3918 break;
3922 * enable internal loopback
3923 * TxCLK and RxCLK are generated from BRG
3924 * and TxD is looped back to RxD internally.
3926 static void enable_loopback(struct slgt_info *info)
3928 /* SCR (serial control) BIT2=looopback enable */
3929 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
3931 if (info->params.mode != MGSL_MODE_ASYNC) {
3932 /* CCR (clock control)
3933 * 07..05 tx clock source (010 = BRG)
3934 * 04..02 rx clock source (010 = BRG)
3935 * 01 auxclk enable (0 = disable)
3936 * 00 BRG enable (1 = enable)
3938 * 0100 1001
3940 wr_reg8(info, CCR, 0x49);
3942 /* set speed if available, otherwise use default */
3943 if (info->params.clock_speed)
3944 set_rate(info, info->params.clock_speed);
3945 else
3946 set_rate(info, 3686400);
3951 * set baud rate generator to specified rate
3953 static void set_rate(struct slgt_info *info, u32 rate)
3955 unsigned int div;
3956 unsigned int osc = info->base_clock;
3958 /* div = osc/rate - 1
3960 * Round div up if osc/rate is not integer to
3961 * force to next slowest rate.
3964 if (rate) {
3965 div = osc/rate;
3966 if (!(osc % rate) && div)
3967 div--;
3968 wr_reg16(info, BDR, (unsigned short)div);
3972 static void rx_stop(struct slgt_info *info)
3974 unsigned short val;
3976 /* disable and reset receiver */
3977 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3978 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3979 wr_reg16(info, RCR, val); /* clear reset bit */
3981 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
3983 /* clear pending rx interrupts */
3984 wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
3986 rdma_reset(info);
3988 info->rx_enabled = false;
3989 info->rx_restart = false;
3992 static void rx_start(struct slgt_info *info)
3994 unsigned short val;
3996 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
3998 /* clear pending rx overrun IRQ */
3999 wr_reg16(info, SSR, IRQ_RXOVER);
4001 /* reset and disable receiver */
4002 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
4003 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
4004 wr_reg16(info, RCR, val); /* clear reset bit */
4006 rdma_reset(info);
4007 reset_rbufs(info);
4009 if (info->rx_pio) {
4010 /* rx request when rx FIFO not empty */
4011 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) & ~BIT14));
4012 slgt_irq_on(info, IRQ_RXDATA);
4013 if (info->params.mode == MGSL_MODE_ASYNC) {
4014 /* enable saving of rx status */
4015 wr_reg32(info, RDCSR, BIT6);
4017 } else {
4018 /* rx request when rx FIFO half full */
4019 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT14));
4020 /* set 1st descriptor address */
4021 wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
4023 if (info->params.mode != MGSL_MODE_ASYNC) {
4024 /* enable rx DMA and DMA interrupt */
4025 wr_reg32(info, RDCSR, (BIT2 + BIT0));
4026 } else {
4027 /* enable saving of rx status, rx DMA and DMA interrupt */
4028 wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
4032 slgt_irq_on(info, IRQ_RXOVER);
4034 /* enable receiver */
4035 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
4037 info->rx_restart = false;
4038 info->rx_enabled = true;
4041 static void tx_start(struct slgt_info *info)
4043 if (!info->tx_enabled) {
4044 wr_reg16(info, TCR,
4045 (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
4046 info->tx_enabled = true;
4049 if (desc_count(info->tbufs[info->tbuf_start])) {
4050 info->drop_rts_on_tx_done = false;
4052 if (info->params.mode != MGSL_MODE_ASYNC) {
4053 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
4054 get_signals(info);
4055 if (!(info->signals & SerialSignal_RTS)) {
4056 info->signals |= SerialSignal_RTS;
4057 set_signals(info);
4058 info->drop_rts_on_tx_done = true;
4062 slgt_irq_off(info, IRQ_TXDATA);
4063 slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
4064 /* clear tx idle and underrun status bits */
4065 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
4066 } else {
4067 slgt_irq_off(info, IRQ_TXDATA);
4068 slgt_irq_on(info, IRQ_TXIDLE);
4069 /* clear tx idle status bit */
4070 wr_reg16(info, SSR, IRQ_TXIDLE);
4072 /* set 1st descriptor address and start DMA */
4073 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
4074 wr_reg32(info, TDCSR, BIT2 + BIT0);
4075 info->tx_active = true;
4079 static void tx_stop(struct slgt_info *info)
4081 unsigned short val;
4083 del_timer(&info->tx_timer);
4085 tdma_reset(info);
4087 /* reset and disable transmitter */
4088 val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
4089 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
4091 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
4093 /* clear tx idle and underrun status bit */
4094 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
4096 reset_tbufs(info);
4098 info->tx_enabled = false;
4099 info->tx_active = false;
4102 static void reset_port(struct slgt_info *info)
4104 if (!info->reg_addr)
4105 return;
4107 tx_stop(info);
4108 rx_stop(info);
4110 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
4111 set_signals(info);
4113 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4116 static void reset_adapter(struct slgt_info *info)
4118 int i;
4119 for (i=0; i < info->port_count; ++i) {
4120 if (info->port_array[i])
4121 reset_port(info->port_array[i]);
4125 static void async_mode(struct slgt_info *info)
4127 unsigned short val;
4129 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4130 tx_stop(info);
4131 rx_stop(info);
4133 /* TCR (tx control)
4135 * 15..13 mode, 010=async
4136 * 12..10 encoding, 000=NRZ
4137 * 09 parity enable
4138 * 08 1=odd parity, 0=even parity
4139 * 07 1=RTS driver control
4140 * 06 1=break enable
4141 * 05..04 character length
4142 * 00=5 bits
4143 * 01=6 bits
4144 * 10=7 bits
4145 * 11=8 bits
4146 * 03 0=1 stop bit, 1=2 stop bits
4147 * 02 reset
4148 * 01 enable
4149 * 00 auto-CTS enable
4151 val = 0x4000;
4153 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4154 val |= BIT7;
4156 if (info->params.parity != ASYNC_PARITY_NONE) {
4157 val |= BIT9;
4158 if (info->params.parity == ASYNC_PARITY_ODD)
4159 val |= BIT8;
4162 switch (info->params.data_bits)
4164 case 6: val |= BIT4; break;
4165 case 7: val |= BIT5; break;
4166 case 8: val |= BIT5 + BIT4; break;
4169 if (info->params.stop_bits != 1)
4170 val |= BIT3;
4172 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4173 val |= BIT0;
4175 wr_reg16(info, TCR, val);
4177 /* RCR (rx control)
4179 * 15..13 mode, 010=async
4180 * 12..10 encoding, 000=NRZ
4181 * 09 parity enable
4182 * 08 1=odd parity, 0=even parity
4183 * 07..06 reserved, must be 0
4184 * 05..04 character length
4185 * 00=5 bits
4186 * 01=6 bits
4187 * 10=7 bits
4188 * 11=8 bits
4189 * 03 reserved, must be zero
4190 * 02 reset
4191 * 01 enable
4192 * 00 auto-DCD enable
4194 val = 0x4000;
4196 if (info->params.parity != ASYNC_PARITY_NONE) {
4197 val |= BIT9;
4198 if (info->params.parity == ASYNC_PARITY_ODD)
4199 val |= BIT8;
4202 switch (info->params.data_bits)
4204 case 6: val |= BIT4; break;
4205 case 7: val |= BIT5; break;
4206 case 8: val |= BIT5 + BIT4; break;
4209 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4210 val |= BIT0;
4212 wr_reg16(info, RCR, val);
4214 /* CCR (clock control)
4216 * 07..05 011 = tx clock source is BRG/16
4217 * 04..02 010 = rx clock source is BRG
4218 * 01 0 = auxclk disabled
4219 * 00 1 = BRG enabled
4221 * 0110 1001
4223 wr_reg8(info, CCR, 0x69);
4225 msc_set_vcr(info);
4227 /* SCR (serial control)
4229 * 15 1=tx req on FIFO half empty
4230 * 14 1=rx req on FIFO half full
4231 * 13 tx data IRQ enable
4232 * 12 tx idle IRQ enable
4233 * 11 rx break on IRQ enable
4234 * 10 rx data IRQ enable
4235 * 09 rx break off IRQ enable
4236 * 08 overrun IRQ enable
4237 * 07 DSR IRQ enable
4238 * 06 CTS IRQ enable
4239 * 05 DCD IRQ enable
4240 * 04 RI IRQ enable
4241 * 03 0=16x sampling, 1=8x sampling
4242 * 02 1=txd->rxd internal loopback enable
4243 * 01 reserved, must be zero
4244 * 00 1=master IRQ enable
4246 val = BIT15 + BIT14 + BIT0;
4247 /* JCR[8] : 1 = x8 async mode feature available */
4248 if ((rd_reg32(info, JCR) & BIT8) && info->params.data_rate &&
4249 ((info->base_clock < (info->params.data_rate * 16)) ||
4250 (info->base_clock % (info->params.data_rate * 16)))) {
4251 /* use 8x sampling */
4252 val |= BIT3;
4253 set_rate(info, info->params.data_rate * 8);
4254 } else {
4255 /* use 16x sampling */
4256 set_rate(info, info->params.data_rate * 16);
4258 wr_reg16(info, SCR, val);
4260 slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
4262 if (info->params.loopback)
4263 enable_loopback(info);
4266 static void sync_mode(struct slgt_info *info)
4268 unsigned short val;
4270 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4271 tx_stop(info);
4272 rx_stop(info);
4274 /* TCR (tx control)
4276 * 15..13 mode
4277 * 000=HDLC/SDLC
4278 * 001=raw bit synchronous
4279 * 010=asynchronous/isochronous
4280 * 011=monosync byte synchronous
4281 * 100=bisync byte synchronous
4282 * 101=xsync byte synchronous
4283 * 12..10 encoding
4284 * 09 CRC enable
4285 * 08 CRC32
4286 * 07 1=RTS driver control
4287 * 06 preamble enable
4288 * 05..04 preamble length
4289 * 03 share open/close flag
4290 * 02 reset
4291 * 01 enable
4292 * 00 auto-CTS enable
4294 val = BIT2;
4296 switch(info->params.mode) {
4297 case MGSL_MODE_XSYNC:
4298 val |= BIT15 + BIT13;
4299 break;
4300 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4301 case MGSL_MODE_BISYNC: val |= BIT15; break;
4302 case MGSL_MODE_RAW: val |= BIT13; break;
4304 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4305 val |= BIT7;
4307 switch(info->params.encoding)
4309 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4310 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4311 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4312 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4313 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4314 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4315 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4318 switch (info->params.crc_type & HDLC_CRC_MASK)
4320 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4321 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4324 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
4325 val |= BIT6;
4327 switch (info->params.preamble_length)
4329 case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
4330 case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
4331 case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
4334 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4335 val |= BIT0;
4337 wr_reg16(info, TCR, val);
4339 /* TPR (transmit preamble) */
4341 switch (info->params.preamble)
4343 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
4344 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
4345 case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
4346 case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
4347 case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
4348 default: val = 0x7e; break;
4350 wr_reg8(info, TPR, (unsigned char)val);
4352 /* RCR (rx control)
4354 * 15..13 mode
4355 * 000=HDLC/SDLC
4356 * 001=raw bit synchronous
4357 * 010=asynchronous/isochronous
4358 * 011=monosync byte synchronous
4359 * 100=bisync byte synchronous
4360 * 101=xsync byte synchronous
4361 * 12..10 encoding
4362 * 09 CRC enable
4363 * 08 CRC32
4364 * 07..03 reserved, must be 0
4365 * 02 reset
4366 * 01 enable
4367 * 00 auto-DCD enable
4369 val = 0;
4371 switch(info->params.mode) {
4372 case MGSL_MODE_XSYNC:
4373 val |= BIT15 + BIT13;
4374 break;
4375 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4376 case MGSL_MODE_BISYNC: val |= BIT15; break;
4377 case MGSL_MODE_RAW: val |= BIT13; break;
4380 switch(info->params.encoding)
4382 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4383 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4384 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4385 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4386 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4387 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4388 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4391 switch (info->params.crc_type & HDLC_CRC_MASK)
4393 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4394 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4397 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4398 val |= BIT0;
4400 wr_reg16(info, RCR, val);
4402 /* CCR (clock control)
4404 * 07..05 tx clock source
4405 * 04..02 rx clock source
4406 * 01 auxclk enable
4407 * 00 BRG enable
4409 val = 0;
4411 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4413 // when RxC source is DPLL, BRG generates 16X DPLL
4414 // reference clock, so take TxC from BRG/16 to get
4415 // transmit clock at actual data rate
4416 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4417 val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
4418 else
4419 val |= BIT6; /* 010, txclk = BRG */
4421 else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4422 val |= BIT7; /* 100, txclk = DPLL Input */
4423 else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4424 val |= BIT5; /* 001, txclk = RXC Input */
4426 if (info->params.flags & HDLC_FLAG_RXC_BRG)
4427 val |= BIT3; /* 010, rxclk = BRG */
4428 else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4429 val |= BIT4; /* 100, rxclk = DPLL */
4430 else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4431 val |= BIT2; /* 001, rxclk = TXC Input */
4433 if (info->params.clock_speed)
4434 val |= BIT1 + BIT0;
4436 wr_reg8(info, CCR, (unsigned char)val);
4438 if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
4440 // program DPLL mode
4441 switch(info->params.encoding)
4443 case HDLC_ENCODING_BIPHASE_MARK:
4444 case HDLC_ENCODING_BIPHASE_SPACE:
4445 val = BIT7; break;
4446 case HDLC_ENCODING_BIPHASE_LEVEL:
4447 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
4448 val = BIT7 + BIT6; break;
4449 default: val = BIT6; // NRZ encodings
4451 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
4453 // DPLL requires a 16X reference clock from BRG
4454 set_rate(info, info->params.clock_speed * 16);
4456 else
4457 set_rate(info, info->params.clock_speed);
4459 tx_set_idle(info);
4461 msc_set_vcr(info);
4463 /* SCR (serial control)
4465 * 15 1=tx req on FIFO half empty
4466 * 14 1=rx req on FIFO half full
4467 * 13 tx data IRQ enable
4468 * 12 tx idle IRQ enable
4469 * 11 underrun IRQ enable
4470 * 10 rx data IRQ enable
4471 * 09 rx idle IRQ enable
4472 * 08 overrun IRQ enable
4473 * 07 DSR IRQ enable
4474 * 06 CTS IRQ enable
4475 * 05 DCD IRQ enable
4476 * 04 RI IRQ enable
4477 * 03 reserved, must be zero
4478 * 02 1=txd->rxd internal loopback enable
4479 * 01 reserved, must be zero
4480 * 00 1=master IRQ enable
4482 wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
4484 if (info->params.loopback)
4485 enable_loopback(info);
4489 * set transmit idle mode
4491 static void tx_set_idle(struct slgt_info *info)
4493 unsigned char val;
4494 unsigned short tcr;
4496 /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
4497 * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
4499 tcr = rd_reg16(info, TCR);
4500 if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
4501 /* disable preamble, set idle size to 16 bits */
4502 tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
4503 /* MSB of 16 bit idle specified in tx preamble register (TPR) */
4504 wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
4505 } else if (!(tcr & BIT6)) {
4506 /* preamble is disabled, set idle size to 8 bits */
4507 tcr &= ~(BIT5 + BIT4);
4509 wr_reg16(info, TCR, tcr);
4511 if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
4512 /* LSB of custom tx idle specified in tx idle register */
4513 val = (unsigned char)(info->idle_mode & 0xff);
4514 } else {
4515 /* standard 8 bit idle patterns */
4516 switch(info->idle_mode)
4518 case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
4519 case HDLC_TXIDLE_ALT_ZEROS_ONES:
4520 case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
4521 case HDLC_TXIDLE_ZEROS:
4522 case HDLC_TXIDLE_SPACE: val = 0x00; break;
4523 default: val = 0xff;
4527 wr_reg8(info, TIR, val);
4531 * get state of V24 status (input) signals
4533 static void get_signals(struct slgt_info *info)
4535 unsigned short status = rd_reg16(info, SSR);
4537 /* clear all serial signals except DTR and RTS */
4538 info->signals &= SerialSignal_DTR + SerialSignal_RTS;
4540 if (status & BIT3)
4541 info->signals |= SerialSignal_DSR;
4542 if (status & BIT2)
4543 info->signals |= SerialSignal_CTS;
4544 if (status & BIT1)
4545 info->signals |= SerialSignal_DCD;
4546 if (status & BIT0)
4547 info->signals |= SerialSignal_RI;
4551 * set V.24 Control Register based on current configuration
4553 static void msc_set_vcr(struct slgt_info *info)
4555 unsigned char val = 0;
4557 /* VCR (V.24 control)
4559 * 07..04 serial IF select
4560 * 03 DTR
4561 * 02 RTS
4562 * 01 LL
4563 * 00 RL
4566 switch(info->if_mode & MGSL_INTERFACE_MASK)
4568 case MGSL_INTERFACE_RS232:
4569 val |= BIT5; /* 0010 */
4570 break;
4571 case MGSL_INTERFACE_V35:
4572 val |= BIT7 + BIT6 + BIT5; /* 1110 */
4573 break;
4574 case MGSL_INTERFACE_RS422:
4575 val |= BIT6; /* 0100 */
4576 break;
4579 if (info->if_mode & MGSL_INTERFACE_MSB_FIRST)
4580 val |= BIT4;
4581 if (info->signals & SerialSignal_DTR)
4582 val |= BIT3;
4583 if (info->signals & SerialSignal_RTS)
4584 val |= BIT2;
4585 if (info->if_mode & MGSL_INTERFACE_LL)
4586 val |= BIT1;
4587 if (info->if_mode & MGSL_INTERFACE_RL)
4588 val |= BIT0;
4589 wr_reg8(info, VCR, val);
4593 * set state of V24 control (output) signals
4595 static void set_signals(struct slgt_info *info)
4597 unsigned char val = rd_reg8(info, VCR);
4598 if (info->signals & SerialSignal_DTR)
4599 val |= BIT3;
4600 else
4601 val &= ~BIT3;
4602 if (info->signals & SerialSignal_RTS)
4603 val |= BIT2;
4604 else
4605 val &= ~BIT2;
4606 wr_reg8(info, VCR, val);
4610 * free range of receive DMA buffers (i to last)
4612 static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
4614 int done = 0;
4616 while(!done) {
4617 /* reset current buffer for reuse */
4618 info->rbufs[i].status = 0;
4619 set_desc_count(info->rbufs[i], info->rbuf_fill_level);
4620 if (i == last)
4621 done = 1;
4622 if (++i == info->rbuf_count)
4623 i = 0;
4625 info->rbuf_current = i;
4629 * mark all receive DMA buffers as free
4631 static void reset_rbufs(struct slgt_info *info)
4633 free_rbufs(info, 0, info->rbuf_count - 1);
4634 info->rbuf_fill_index = 0;
4635 info->rbuf_fill_count = 0;
4639 * pass receive HDLC frame to upper layer
4641 * return true if frame available, otherwise false
4643 static bool rx_get_frame(struct slgt_info *info)
4645 unsigned int start, end;
4646 unsigned short status;
4647 unsigned int framesize = 0;
4648 unsigned long flags;
4649 struct tty_struct *tty = info->port.tty;
4650 unsigned char addr_field = 0xff;
4651 unsigned int crc_size = 0;
4653 switch (info->params.crc_type & HDLC_CRC_MASK) {
4654 case HDLC_CRC_16_CCITT: crc_size = 2; break;
4655 case HDLC_CRC_32_CCITT: crc_size = 4; break;
4658 check_again:
4660 framesize = 0;
4661 addr_field = 0xff;
4662 start = end = info->rbuf_current;
4664 for (;;) {
4665 if (!desc_complete(info->rbufs[end]))
4666 goto cleanup;
4668 if (framesize == 0 && info->params.addr_filter != 0xff)
4669 addr_field = info->rbufs[end].buf[0];
4671 framesize += desc_count(info->rbufs[end]);
4673 if (desc_eof(info->rbufs[end]))
4674 break;
4676 if (++end == info->rbuf_count)
4677 end = 0;
4679 if (end == info->rbuf_current) {
4680 if (info->rx_enabled){
4681 spin_lock_irqsave(&info->lock,flags);
4682 rx_start(info);
4683 spin_unlock_irqrestore(&info->lock,flags);
4685 goto cleanup;
4689 /* status
4691 * 15 buffer complete
4692 * 14..06 reserved
4693 * 05..04 residue
4694 * 02 eof (end of frame)
4695 * 01 CRC error
4696 * 00 abort
4698 status = desc_status(info->rbufs[end]);
4700 /* ignore CRC bit if not using CRC (bit is undefined) */
4701 if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
4702 status &= ~BIT1;
4704 if (framesize == 0 ||
4705 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4706 free_rbufs(info, start, end);
4707 goto check_again;
4710 if (framesize < (2 + crc_size) || status & BIT0) {
4711 info->icount.rxshort++;
4712 framesize = 0;
4713 } else if (status & BIT1) {
4714 info->icount.rxcrc++;
4715 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
4716 framesize = 0;
4719 #if SYNCLINK_GENERIC_HDLC
4720 if (framesize == 0) {
4721 info->netdev->stats.rx_errors++;
4722 info->netdev->stats.rx_frame_errors++;
4724 #endif
4726 DBGBH(("%s rx frame status=%04X size=%d\n",
4727 info->device_name, status, framesize));
4728 DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, info->rbuf_fill_level), "rx");
4730 if (framesize) {
4731 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
4732 framesize -= crc_size;
4733 crc_size = 0;
4736 if (framesize > info->max_frame_size + crc_size)
4737 info->icount.rxlong++;
4738 else {
4739 /* copy dma buffer(s) to contiguous temp buffer */
4740 int copy_count = framesize;
4741 int i = start;
4742 unsigned char *p = info->tmp_rbuf;
4743 info->tmp_rbuf_count = framesize;
4745 info->icount.rxok++;
4747 while(copy_count) {
4748 int partial_count = min_t(int, copy_count, info->rbuf_fill_level);
4749 memcpy(p, info->rbufs[i].buf, partial_count);
4750 p += partial_count;
4751 copy_count -= partial_count;
4752 if (++i == info->rbuf_count)
4753 i = 0;
4756 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4757 *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
4758 framesize++;
4761 #if SYNCLINK_GENERIC_HDLC
4762 if (info->netcount)
4763 hdlcdev_rx(info,info->tmp_rbuf, framesize);
4764 else
4765 #endif
4766 ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
4769 free_rbufs(info, start, end);
4770 return true;
4772 cleanup:
4773 return false;
4777 * pass receive buffer (RAW synchronous mode) to tty layer
4778 * return true if buffer available, otherwise false
4780 static bool rx_get_buf(struct slgt_info *info)
4782 unsigned int i = info->rbuf_current;
4783 unsigned int count;
4785 if (!desc_complete(info->rbufs[i]))
4786 return false;
4787 count = desc_count(info->rbufs[i]);
4788 switch(info->params.mode) {
4789 case MGSL_MODE_MONOSYNC:
4790 case MGSL_MODE_BISYNC:
4791 case MGSL_MODE_XSYNC:
4792 /* ignore residue in byte synchronous modes */
4793 if (desc_residue(info->rbufs[i]))
4794 count--;
4795 break;
4797 DBGDATA(info, info->rbufs[i].buf, count, "rx");
4798 DBGINFO(("rx_get_buf size=%d\n", count));
4799 if (count)
4800 ldisc_receive_buf(info->port.tty, info->rbufs[i].buf,
4801 info->flag_buf, count);
4802 free_rbufs(info, i, i);
4803 return true;
4806 static void reset_tbufs(struct slgt_info *info)
4808 unsigned int i;
4809 info->tbuf_current = 0;
4810 for (i=0 ; i < info->tbuf_count ; i++) {
4811 info->tbufs[i].status = 0;
4812 info->tbufs[i].count = 0;
4817 * return number of free transmit DMA buffers
4819 static unsigned int free_tbuf_count(struct slgt_info *info)
4821 unsigned int count = 0;
4822 unsigned int i = info->tbuf_current;
4826 if (desc_count(info->tbufs[i]))
4827 break; /* buffer in use */
4828 ++count;
4829 if (++i == info->tbuf_count)
4830 i=0;
4831 } while (i != info->tbuf_current);
4833 /* if tx DMA active, last zero count buffer is in use */
4834 if (count && (rd_reg32(info, TDCSR) & BIT0))
4835 --count;
4837 return count;
4841 * return number of bytes in unsent transmit DMA buffers
4842 * and the serial controller tx FIFO
4844 static unsigned int tbuf_bytes(struct slgt_info *info)
4846 unsigned int total_count = 0;
4847 unsigned int i = info->tbuf_current;
4848 unsigned int reg_value;
4849 unsigned int count;
4850 unsigned int active_buf_count = 0;
4853 * Add descriptor counts for all tx DMA buffers.
4854 * If count is zero (cleared by DMA controller after read),
4855 * the buffer is complete or is actively being read from.
4857 * Record buf_count of last buffer with zero count starting
4858 * from current ring position. buf_count is mirror
4859 * copy of count and is not cleared by serial controller.
4860 * If DMA controller is active, that buffer is actively
4861 * being read so add to total.
4863 do {
4864 count = desc_count(info->tbufs[i]);
4865 if (count)
4866 total_count += count;
4867 else if (!total_count)
4868 active_buf_count = info->tbufs[i].buf_count;
4869 if (++i == info->tbuf_count)
4870 i = 0;
4871 } while (i != info->tbuf_current);
4873 /* read tx DMA status register */
4874 reg_value = rd_reg32(info, TDCSR);
4876 /* if tx DMA active, last zero count buffer is in use */
4877 if (reg_value & BIT0)
4878 total_count += active_buf_count;
4880 /* add tx FIFO count = reg_value[15..8] */
4881 total_count += (reg_value >> 8) & 0xff;
4883 /* if transmitter active add one byte for shift register */
4884 if (info->tx_active)
4885 total_count++;
4887 return total_count;
4891 * load data into transmit DMA buffer ring and start transmitter if needed
4892 * return true if data accepted, otherwise false (buffers full)
4894 static bool tx_load(struct slgt_info *info, const char *buf, unsigned int size)
4896 unsigned short count;
4897 unsigned int i;
4898 struct slgt_desc *d;
4900 /* check required buffer space */
4901 if (DIV_ROUND_UP(size, DMABUFSIZE) > free_tbuf_count(info))
4902 return false;
4904 DBGDATA(info, buf, size, "tx");
4907 * copy data to one or more DMA buffers in circular ring
4908 * tbuf_start = first buffer for this data
4909 * tbuf_current = next free buffer
4911 * Copy all data before making data visible to DMA controller by
4912 * setting descriptor count of the first buffer.
4913 * This prevents an active DMA controller from reading the first DMA
4914 * buffers of a frame and stopping before the final buffers are filled.
4917 info->tbuf_start = i = info->tbuf_current;
4919 while (size) {
4920 d = &info->tbufs[i];
4922 count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
4923 memcpy(d->buf, buf, count);
4925 size -= count;
4926 buf += count;
4929 * set EOF bit for last buffer of HDLC frame or
4930 * for every buffer in raw mode
4932 if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
4933 info->params.mode == MGSL_MODE_RAW)
4934 set_desc_eof(*d, 1);
4935 else
4936 set_desc_eof(*d, 0);
4938 /* set descriptor count for all but first buffer */
4939 if (i != info->tbuf_start)
4940 set_desc_count(*d, count);
4941 d->buf_count = count;
4943 if (++i == info->tbuf_count)
4944 i = 0;
4947 info->tbuf_current = i;
4949 /* set first buffer count to make new data visible to DMA controller */
4950 d = &info->tbufs[info->tbuf_start];
4951 set_desc_count(*d, d->buf_count);
4953 /* start transmitter if needed and update transmit timeout */
4954 if (!info->tx_active)
4955 tx_start(info);
4956 update_tx_timer(info);
4958 return true;
4961 static int register_test(struct slgt_info *info)
4963 static unsigned short patterns[] =
4964 {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
4965 static unsigned int count = ARRAY_SIZE(patterns);
4966 unsigned int i;
4967 int rc = 0;
4969 for (i=0 ; i < count ; i++) {
4970 wr_reg16(info, TIR, patterns[i]);
4971 wr_reg16(info, BDR, patterns[(i+1)%count]);
4972 if ((rd_reg16(info, TIR) != patterns[i]) ||
4973 (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
4974 rc = -ENODEV;
4975 break;
4978 info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
4979 info->init_error = rc ? 0 : DiagStatus_AddressFailure;
4980 return rc;
4983 static int irq_test(struct slgt_info *info)
4985 unsigned long timeout;
4986 unsigned long flags;
4987 struct tty_struct *oldtty = info->port.tty;
4988 u32 speed = info->params.data_rate;
4990 info->params.data_rate = 921600;
4991 info->port.tty = NULL;
4993 spin_lock_irqsave(&info->lock, flags);
4994 async_mode(info);
4995 slgt_irq_on(info, IRQ_TXIDLE);
4997 /* enable transmitter */
4998 wr_reg16(info, TCR,
4999 (unsigned short)(rd_reg16(info, TCR) | BIT1));
5001 /* write one byte and wait for tx idle */
5002 wr_reg16(info, TDR, 0);
5004 /* assume failure */
5005 info->init_error = DiagStatus_IrqFailure;
5006 info->irq_occurred = false;
5008 spin_unlock_irqrestore(&info->lock, flags);
5010 timeout=100;
5011 while(timeout-- && !info->irq_occurred)
5012 msleep_interruptible(10);
5014 spin_lock_irqsave(&info->lock,flags);
5015 reset_port(info);
5016 spin_unlock_irqrestore(&info->lock,flags);
5018 info->params.data_rate = speed;
5019 info->port.tty = oldtty;
5021 info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
5022 return info->irq_occurred ? 0 : -ENODEV;
5025 static int loopback_test_rx(struct slgt_info *info)
5027 unsigned char *src, *dest;
5028 int count;
5030 if (desc_complete(info->rbufs[0])) {
5031 count = desc_count(info->rbufs[0]);
5032 src = info->rbufs[0].buf;
5033 dest = info->tmp_rbuf;
5035 for( ; count ; count-=2, src+=2) {
5036 /* src=data byte (src+1)=status byte */
5037 if (!(*(src+1) & (BIT9 + BIT8))) {
5038 *dest = *src;
5039 dest++;
5040 info->tmp_rbuf_count++;
5043 DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
5044 return 1;
5046 return 0;
5049 static int loopback_test(struct slgt_info *info)
5051 #define TESTFRAMESIZE 20
5053 unsigned long timeout;
5054 u16 count = TESTFRAMESIZE;
5055 unsigned char buf[TESTFRAMESIZE];
5056 int rc = -ENODEV;
5057 unsigned long flags;
5059 struct tty_struct *oldtty = info->port.tty;
5060 MGSL_PARAMS params;
5062 memcpy(&params, &info->params, sizeof(params));
5064 info->params.mode = MGSL_MODE_ASYNC;
5065 info->params.data_rate = 921600;
5066 info->params.loopback = 1;
5067 info->port.tty = NULL;
5069 /* build and send transmit frame */
5070 for (count = 0; count < TESTFRAMESIZE; ++count)
5071 buf[count] = (unsigned char)count;
5073 info->tmp_rbuf_count = 0;
5074 memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
5076 /* program hardware for HDLC and enabled receiver */
5077 spin_lock_irqsave(&info->lock,flags);
5078 async_mode(info);
5079 rx_start(info);
5080 tx_load(info, buf, count);
5081 spin_unlock_irqrestore(&info->lock, flags);
5083 /* wait for receive complete */
5084 for (timeout = 100; timeout; --timeout) {
5085 msleep_interruptible(10);
5086 if (loopback_test_rx(info)) {
5087 rc = 0;
5088 break;
5092 /* verify received frame length and contents */
5093 if (!rc && (info->tmp_rbuf_count != count ||
5094 memcmp(buf, info->tmp_rbuf, count))) {
5095 rc = -ENODEV;
5098 spin_lock_irqsave(&info->lock,flags);
5099 reset_adapter(info);
5100 spin_unlock_irqrestore(&info->lock,flags);
5102 memcpy(&info->params, &params, sizeof(info->params));
5103 info->port.tty = oldtty;
5105 info->init_error = rc ? DiagStatus_DmaFailure : 0;
5106 return rc;
5109 static int adapter_test(struct slgt_info *info)
5111 DBGINFO(("testing %s\n", info->device_name));
5112 if (register_test(info) < 0) {
5113 printk("register test failure %s addr=%08X\n",
5114 info->device_name, info->phys_reg_addr);
5115 } else if (irq_test(info) < 0) {
5116 printk("IRQ test failure %s IRQ=%d\n",
5117 info->device_name, info->irq_level);
5118 } else if (loopback_test(info) < 0) {
5119 printk("loopback test failure %s\n", info->device_name);
5121 return info->init_error;
5125 * transmit timeout handler
5127 static void tx_timeout(unsigned long context)
5129 struct slgt_info *info = (struct slgt_info*)context;
5130 unsigned long flags;
5132 DBGINFO(("%s tx_timeout\n", info->device_name));
5133 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
5134 info->icount.txtimeout++;
5136 spin_lock_irqsave(&info->lock,flags);
5137 tx_stop(info);
5138 spin_unlock_irqrestore(&info->lock,flags);
5140 #if SYNCLINK_GENERIC_HDLC
5141 if (info->netcount)
5142 hdlcdev_tx_done(info);
5143 else
5144 #endif
5145 bh_transmit(info);
5149 * receive buffer polling timer
5151 static void rx_timeout(unsigned long context)
5153 struct slgt_info *info = (struct slgt_info*)context;
5154 unsigned long flags;
5156 DBGINFO(("%s rx_timeout\n", info->device_name));
5157 spin_lock_irqsave(&info->lock, flags);
5158 info->pending_bh |= BH_RECEIVE;
5159 spin_unlock_irqrestore(&info->lock, flags);
5160 bh_handler(&info->task);