WAN: Use u32 type instead of u_int32_t in LMC driver.
[linux-2.6.git] / drivers / char / synclink_gt.c
1 /*
2  * $Id: synclink_gt.c,v 4.50 2007/07/25 19:29:25 paulkf Exp $
3  *
4  * Device driver for Microgate SyncLink GT serial adapters.
5  *
6  * written by Paul Fulghum for Microgate Corporation
7  * paulkf@microgate.com
8  *
9  * Microgate and SyncLink are trademarks of Microgate Corporation
10  *
11  * This code is released under the GNU General Public License (GPL)
12  *
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
14  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
17  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23  * OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 /*
27  * DEBUG OUTPUT DEFINITIONS
28  *
29  * uncomment lines below to enable specific types of debug output
30  *
31  * DBGINFO   information - most verbose output
32  * DBGERR    serious errors
33  * DBGBH     bottom half service routine debugging
34  * DBGISR    interrupt service routine debugging
35  * DBGDATA   output receive and transmit data
36  * DBGTBUF   output transmit DMA buffers and registers
37  * DBGRBUF   output receive DMA buffers and registers
38  */
39
40 #define DBGINFO(fmt) if (debug_level >= DEBUG_LEVEL_INFO) printk fmt
41 #define DBGERR(fmt) if (debug_level >= DEBUG_LEVEL_ERROR) printk fmt
42 #define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt
43 #define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt
44 #define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label))
45 //#define DBGTBUF(info) dump_tbufs(info)
46 //#define DBGRBUF(info) dump_rbufs(info)
47
48
49 #include <linux/module.h>
50 #include <linux/version.h>
51 #include <linux/errno.h>
52 #include <linux/signal.h>
53 #include <linux/sched.h>
54 #include <linux/timer.h>
55 #include <linux/interrupt.h>
56 #include <linux/pci.h>
57 #include <linux/tty.h>
58 #include <linux/tty_flip.h>
59 #include <linux/serial.h>
60 #include <linux/major.h>
61 #include <linux/string.h>
62 #include <linux/fcntl.h>
63 #include <linux/ptrace.h>
64 #include <linux/ioport.h>
65 #include <linux/mm.h>
66 #include <linux/slab.h>
67 #include <linux/netdevice.h>
68 #include <linux/vmalloc.h>
69 #include <linux/init.h>
70 #include <linux/delay.h>
71 #include <linux/ioctl.h>
72 #include <linux/termios.h>
73 #include <linux/bitops.h>
74 #include <linux/workqueue.h>
75 #include <linux/hdlc.h>
76 #include <linux/synclink.h>
77
78 #include <asm/system.h>
79 #include <asm/io.h>
80 #include <asm/irq.h>
81 #include <asm/dma.h>
82 #include <asm/types.h>
83 #include <asm/uaccess.h>
84
85 #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_GT_MODULE))
86 #define SYNCLINK_GENERIC_HDLC 1
87 #else
88 #define SYNCLINK_GENERIC_HDLC 0
89 #endif
90
91 /*
92  * module identification
93  */
94 static char *driver_name     = "SyncLink GT";
95 static char *driver_version  = "$Revision: 4.50 $";
96 static char *tty_driver_name = "synclink_gt";
97 static char *tty_dev_prefix  = "ttySLG";
98 MODULE_LICENSE("GPL");
99 #define MGSL_MAGIC 0x5401
100 #define MAX_DEVICES 32
101
102 static struct pci_device_id pci_table[] = {
103         {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
104         {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT2_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
105         {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
106         {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
107         {0,}, /* terminate list */
108 };
109 MODULE_DEVICE_TABLE(pci, pci_table);
110
111 static int  init_one(struct pci_dev *dev,const struct pci_device_id *ent);
112 static void remove_one(struct pci_dev *dev);
113 static struct pci_driver pci_driver = {
114         .name           = "synclink_gt",
115         .id_table       = pci_table,
116         .probe          = init_one,
117         .remove         = __devexit_p(remove_one),
118 };
119
120 static bool pci_registered;
121
122 /*
123  * module configuration and status
124  */
125 static struct slgt_info *slgt_device_list;
126 static int slgt_device_count;
127
128 static int ttymajor;
129 static int debug_level;
130 static int maxframe[MAX_DEVICES];
131
132 module_param(ttymajor, int, 0);
133 module_param(debug_level, int, 0);
134 module_param_array(maxframe, int, NULL, 0);
135
136 MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
137 MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
138 MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
139
140 /*
141  * tty support and callbacks
142  */
143 static struct tty_driver *serial_driver;
144
145 static int  open(struct tty_struct *tty, struct file * filp);
146 static void close(struct tty_struct *tty, struct file * filp);
147 static void hangup(struct tty_struct *tty);
148 static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
149
150 static int  write(struct tty_struct *tty, const unsigned char *buf, int count);
151 static int put_char(struct tty_struct *tty, unsigned char ch);
152 static void send_xchar(struct tty_struct *tty, char ch);
153 static void wait_until_sent(struct tty_struct *tty, int timeout);
154 static int  write_room(struct tty_struct *tty);
155 static void flush_chars(struct tty_struct *tty);
156 static void flush_buffer(struct tty_struct *tty);
157 static void tx_hold(struct tty_struct *tty);
158 static void tx_release(struct tty_struct *tty);
159
160 static int  ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
161 static int  read_proc(char *page, char **start, off_t off, int count,int *eof, void *data);
162 static int  chars_in_buffer(struct tty_struct *tty);
163 static void throttle(struct tty_struct * tty);
164 static void unthrottle(struct tty_struct * tty);
165 static void set_break(struct tty_struct *tty, int break_state);
166
167 /*
168  * generic HDLC support and callbacks
169  */
170 #if SYNCLINK_GENERIC_HDLC
171 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
172 static void hdlcdev_tx_done(struct slgt_info *info);
173 static void hdlcdev_rx(struct slgt_info *info, char *buf, int size);
174 static int  hdlcdev_init(struct slgt_info *info);
175 static void hdlcdev_exit(struct slgt_info *info);
176 #endif
177
178
179 /*
180  * device specific structures, macros and functions
181  */
182
183 #define SLGT_MAX_PORTS 4
184 #define SLGT_REG_SIZE  256
185
186 /*
187  * conditional wait facility
188  */
189 struct cond_wait {
190         struct cond_wait *next;
191         wait_queue_head_t q;
192         wait_queue_t wait;
193         unsigned int data;
194 };
195 static void init_cond_wait(struct cond_wait *w, unsigned int data);
196 static void add_cond_wait(struct cond_wait **head, struct cond_wait *w);
197 static void remove_cond_wait(struct cond_wait **head, struct cond_wait *w);
198 static void flush_cond_wait(struct cond_wait **head);
199
200 /*
201  * DMA buffer descriptor and access macros
202  */
203 struct slgt_desc
204 {
205         __le16 count;
206         __le16 status;
207         __le32 pbuf;  /* physical address of data buffer */
208         __le32 next;  /* physical address of next descriptor */
209
210         /* driver book keeping */
211         char *buf;          /* virtual  address of data buffer */
212         unsigned int pdesc; /* physical address of this descriptor */
213         dma_addr_t buf_dma_addr;
214 };
215
216 #define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
217 #define set_desc_next(a,b) (a).next   = cpu_to_le32((unsigned int)(b))
218 #define set_desc_count(a,b)(a).count  = cpu_to_le16((unsigned short)(b))
219 #define set_desc_eof(a,b)  (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
220 #define desc_count(a)      (le16_to_cpu((a).count))
221 #define desc_status(a)     (le16_to_cpu((a).status))
222 #define desc_complete(a)   (le16_to_cpu((a).status) & BIT15)
223 #define desc_eof(a)        (le16_to_cpu((a).status) & BIT2)
224 #define desc_crc_error(a)  (le16_to_cpu((a).status) & BIT1)
225 #define desc_abort(a)      (le16_to_cpu((a).status) & BIT0)
226 #define desc_residue(a)    ((le16_to_cpu((a).status) & 0x38) >> 3)
227
228 struct _input_signal_events {
229         int ri_up;
230         int ri_down;
231         int dsr_up;
232         int dsr_down;
233         int dcd_up;
234         int dcd_down;
235         int cts_up;
236         int cts_down;
237 };
238
239 /*
240  * device instance data structure
241  */
242 struct slgt_info {
243         void *if_ptr;           /* General purpose pointer (used by SPPP) */
244         struct tty_port port;
245
246         struct slgt_info *next_device;  /* device list link */
247
248         int magic;
249
250         char device_name[25];
251         struct pci_dev *pdev;
252
253         int port_count;  /* count of ports on adapter */
254         int adapter_num; /* adapter instance number */
255         int port_num;    /* port instance number */
256
257         /* array of pointers to port contexts on this adapter */
258         struct slgt_info *port_array[SLGT_MAX_PORTS];
259
260         int                     line;           /* tty line instance number */
261
262         struct mgsl_icount      icount;
263
264         int                     timeout;
265         int                     x_char;         /* xon/xoff character */
266         unsigned int            read_status_mask;
267         unsigned int            ignore_status_mask;
268
269         wait_queue_head_t       status_event_wait_q;
270         wait_queue_head_t       event_wait_q;
271         struct timer_list       tx_timer;
272         struct timer_list       rx_timer;
273
274         unsigned int            gpio_present;
275         struct cond_wait        *gpio_wait_q;
276
277         spinlock_t lock;        /* spinlock for synchronizing with ISR */
278
279         struct work_struct task;
280         u32 pending_bh;
281         bool bh_requested;
282         bool bh_running;
283
284         int isr_overflow;
285         bool irq_requested;     /* true if IRQ requested */
286         bool irq_occurred;      /* for diagnostics use */
287
288         /* device configuration */
289
290         unsigned int bus_type;
291         unsigned int irq_level;
292         unsigned long irq_flags;
293
294         unsigned char __iomem * reg_addr;  /* memory mapped registers address */
295         u32 phys_reg_addr;
296         bool reg_addr_requested;
297
298         MGSL_PARAMS params;       /* communications parameters */
299         u32 idle_mode;
300         u32 max_frame_size;       /* as set by device config */
301
302         unsigned int raw_rx_size;
303         unsigned int if_mode;
304
305         /* device status */
306
307         bool rx_enabled;
308         bool rx_restart;
309
310         bool tx_enabled;
311         bool tx_active;
312
313         unsigned char signals;    /* serial signal states */
314         int init_error;  /* initialization error */
315
316         unsigned char *tx_buf;
317         int tx_count;
318
319         char flag_buf[MAX_ASYNC_BUFFER_SIZE];
320         char char_buf[MAX_ASYNC_BUFFER_SIZE];
321         bool drop_rts_on_tx_done;
322         struct  _input_signal_events    input_signal_events;
323
324         int dcd_chkcount;       /* check counts to prevent */
325         int cts_chkcount;       /* too many IRQs if a signal */
326         int dsr_chkcount;       /* is floating */
327         int ri_chkcount;
328
329         char *bufs;             /* virtual address of DMA buffer lists */
330         dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
331
332         unsigned int rbuf_count;
333         struct slgt_desc *rbufs;
334         unsigned int rbuf_current;
335         unsigned int rbuf_index;
336
337         unsigned int tbuf_count;
338         struct slgt_desc *tbufs;
339         unsigned int tbuf_current;
340         unsigned int tbuf_start;
341
342         unsigned char *tmp_rbuf;
343         unsigned int tmp_rbuf_count;
344
345         /* SPPP/Cisco HDLC device parts */
346
347         int netcount;
348         spinlock_t netlock;
349 #if SYNCLINK_GENERIC_HDLC
350         struct net_device *netdev;
351 #endif
352
353 };
354
355 static MGSL_PARAMS default_params = {
356         .mode            = MGSL_MODE_HDLC,
357         .loopback        = 0,
358         .flags           = HDLC_FLAG_UNDERRUN_ABORT15,
359         .encoding        = HDLC_ENCODING_NRZI_SPACE,
360         .clock_speed     = 0,
361         .addr_filter     = 0xff,
362         .crc_type        = HDLC_CRC_16_CCITT,
363         .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
364         .preamble        = HDLC_PREAMBLE_PATTERN_NONE,
365         .data_rate       = 9600,
366         .data_bits       = 8,
367         .stop_bits       = 1,
368         .parity          = ASYNC_PARITY_NONE
369 };
370
371
372 #define BH_RECEIVE  1
373 #define BH_TRANSMIT 2
374 #define BH_STATUS   4
375 #define IO_PIN_SHUTDOWN_LIMIT 100
376
377 #define DMABUFSIZE 256
378 #define DESC_LIST_SIZE 4096
379
380 #define MASK_PARITY  BIT1
381 #define MASK_FRAMING BIT0
382 #define MASK_BREAK   BIT14
383 #define MASK_OVERRUN BIT4
384
385 #define GSR   0x00 /* global status */
386 #define JCR   0x04 /* JTAG control */
387 #define IODR  0x08 /* GPIO direction */
388 #define IOER  0x0c /* GPIO interrupt enable */
389 #define IOVR  0x10 /* GPIO value */
390 #define IOSR  0x14 /* GPIO interrupt status */
391 #define TDR   0x80 /* tx data */
392 #define RDR   0x80 /* rx data */
393 #define TCR   0x82 /* tx control */
394 #define TIR   0x84 /* tx idle */
395 #define TPR   0x85 /* tx preamble */
396 #define RCR   0x86 /* rx control */
397 #define VCR   0x88 /* V.24 control */
398 #define CCR   0x89 /* clock control */
399 #define BDR   0x8a /* baud divisor */
400 #define SCR   0x8c /* serial control */
401 #define SSR   0x8e /* serial status */
402 #define RDCSR 0x90 /* rx DMA control/status */
403 #define TDCSR 0x94 /* tx DMA control/status */
404 #define RDDAR 0x98 /* rx DMA descriptor address */
405 #define TDDAR 0x9c /* tx DMA descriptor address */
406
407 #define RXIDLE      BIT14
408 #define RXBREAK     BIT14
409 #define IRQ_TXDATA  BIT13
410 #define IRQ_TXIDLE  BIT12
411 #define IRQ_TXUNDER BIT11 /* HDLC */
412 #define IRQ_RXDATA  BIT10
413 #define IRQ_RXIDLE  BIT9  /* HDLC */
414 #define IRQ_RXBREAK BIT9  /* async */
415 #define IRQ_RXOVER  BIT8
416 #define IRQ_DSR     BIT7
417 #define IRQ_CTS     BIT6
418 #define IRQ_DCD     BIT5
419 #define IRQ_RI      BIT4
420 #define IRQ_ALL     0x3ff0
421 #define IRQ_MASTER  BIT0
422
423 #define slgt_irq_on(info, mask) \
424         wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
425 #define slgt_irq_off(info, mask) \
426         wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
427
428 static __u8  rd_reg8(struct slgt_info *info, unsigned int addr);
429 static void  wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
430 static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
431 static void  wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
432 static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
433 static void  wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
434
435 static void  msc_set_vcr(struct slgt_info *info);
436
437 static int  startup(struct slgt_info *info);
438 static int  block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
439 static void shutdown(struct slgt_info *info);
440 static void program_hw(struct slgt_info *info);
441 static void change_params(struct slgt_info *info);
442
443 static int  register_test(struct slgt_info *info);
444 static int  irq_test(struct slgt_info *info);
445 static int  loopback_test(struct slgt_info *info);
446 static int  adapter_test(struct slgt_info *info);
447
448 static void reset_adapter(struct slgt_info *info);
449 static void reset_port(struct slgt_info *info);
450 static void async_mode(struct slgt_info *info);
451 static void sync_mode(struct slgt_info *info);
452
453 static void rx_stop(struct slgt_info *info);
454 static void rx_start(struct slgt_info *info);
455 static void reset_rbufs(struct slgt_info *info);
456 static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
457 static void rdma_reset(struct slgt_info *info);
458 static bool rx_get_frame(struct slgt_info *info);
459 static bool rx_get_buf(struct slgt_info *info);
460
461 static void tx_start(struct slgt_info *info);
462 static void tx_stop(struct slgt_info *info);
463 static void tx_set_idle(struct slgt_info *info);
464 static unsigned int free_tbuf_count(struct slgt_info *info);
465 static void reset_tbufs(struct slgt_info *info);
466 static void tdma_reset(struct slgt_info *info);
467 static void tdma_start(struct slgt_info *info);
468 static void tx_load(struct slgt_info *info, const char *buf, unsigned int count);
469
470 static void get_signals(struct slgt_info *info);
471 static void set_signals(struct slgt_info *info);
472 static void enable_loopback(struct slgt_info *info);
473 static void set_rate(struct slgt_info *info, u32 data_rate);
474
475 static int  bh_action(struct slgt_info *info);
476 static void bh_handler(struct work_struct *work);
477 static void bh_transmit(struct slgt_info *info);
478 static void isr_serial(struct slgt_info *info);
479 static void isr_rdma(struct slgt_info *info);
480 static void isr_txeom(struct slgt_info *info, unsigned short status);
481 static void isr_tdma(struct slgt_info *info);
482
483 static int  alloc_dma_bufs(struct slgt_info *info);
484 static void free_dma_bufs(struct slgt_info *info);
485 static int  alloc_desc(struct slgt_info *info);
486 static void free_desc(struct slgt_info *info);
487 static int  alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
488 static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
489
490 static int  alloc_tmp_rbuf(struct slgt_info *info);
491 static void free_tmp_rbuf(struct slgt_info *info);
492
493 static void tx_timeout(unsigned long context);
494 static void rx_timeout(unsigned long context);
495
496 /*
497  * ioctl handlers
498  */
499 static int  get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount);
500 static int  get_params(struct slgt_info *info, MGSL_PARAMS __user *params);
501 static int  set_params(struct slgt_info *info, MGSL_PARAMS __user *params);
502 static int  get_txidle(struct slgt_info *info, int __user *idle_mode);
503 static int  set_txidle(struct slgt_info *info, int idle_mode);
504 static int  tx_enable(struct slgt_info *info, int enable);
505 static int  tx_abort(struct slgt_info *info);
506 static int  rx_enable(struct slgt_info *info, int enable);
507 static int  modem_input_wait(struct slgt_info *info,int arg);
508 static int  wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr);
509 static int  tiocmget(struct tty_struct *tty, struct file *file);
510 static int  tiocmset(struct tty_struct *tty, struct file *file,
511                      unsigned int set, unsigned int clear);
512 static void set_break(struct tty_struct *tty, int break_state);
513 static int  get_interface(struct slgt_info *info, int __user *if_mode);
514 static int  set_interface(struct slgt_info *info, int if_mode);
515 static int  set_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
516 static int  get_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
517 static int  wait_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
518
519 /*
520  * driver functions
521  */
522 static void add_device(struct slgt_info *info);
523 static void device_init(int adapter_num, struct pci_dev *pdev);
524 static int  claim_resources(struct slgt_info *info);
525 static void release_resources(struct slgt_info *info);
526
527 /*
528  * DEBUG OUTPUT CODE
529  */
530 #ifndef DBGINFO
531 #define DBGINFO(fmt)
532 #endif
533 #ifndef DBGERR
534 #define DBGERR(fmt)
535 #endif
536 #ifndef DBGBH
537 #define DBGBH(fmt)
538 #endif
539 #ifndef DBGISR
540 #define DBGISR(fmt)
541 #endif
542
543 #ifdef DBGDATA
544 static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
545 {
546         int i;
547         int linecount;
548         printk("%s %s data:\n",info->device_name, label);
549         while(count) {
550                 linecount = (count > 16) ? 16 : count;
551                 for(i=0; i < linecount; i++)
552                         printk("%02X ",(unsigned char)data[i]);
553                 for(;i<17;i++)
554                         printk("   ");
555                 for(i=0;i<linecount;i++) {
556                         if (data[i]>=040 && data[i]<=0176)
557                                 printk("%c",data[i]);
558                         else
559                                 printk(".");
560                 }
561                 printk("\n");
562                 data  += linecount;
563                 count -= linecount;
564         }
565 }
566 #else
567 #define DBGDATA(info, buf, size, label)
568 #endif
569
570 #ifdef DBGTBUF
571 static void dump_tbufs(struct slgt_info *info)
572 {
573         int i;
574         printk("tbuf_current=%d\n", info->tbuf_current);
575         for (i=0 ; i < info->tbuf_count ; i++) {
576                 printk("%d: count=%04X status=%04X\n",
577                         i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
578         }
579 }
580 #else
581 #define DBGTBUF(info)
582 #endif
583
584 #ifdef DBGRBUF
585 static void dump_rbufs(struct slgt_info *info)
586 {
587         int i;
588         printk("rbuf_current=%d\n", info->rbuf_current);
589         for (i=0 ; i < info->rbuf_count ; i++) {
590                 printk("%d: count=%04X status=%04X\n",
591                         i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
592         }
593 }
594 #else
595 #define DBGRBUF(info)
596 #endif
597
598 static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
599 {
600 #ifdef SANITY_CHECK
601         if (!info) {
602                 printk("null struct slgt_info for (%s) in %s\n", devname, name);
603                 return 1;
604         }
605         if (info->magic != MGSL_MAGIC) {
606                 printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
607                 return 1;
608         }
609 #else
610         if (!info)
611                 return 1;
612 #endif
613         return 0;
614 }
615
616 /**
617  * line discipline callback wrappers
618  *
619  * The wrappers maintain line discipline references
620  * while calling into the line discipline.
621  *
622  * ldisc_receive_buf  - pass receive data to line discipline
623  */
624 static void ldisc_receive_buf(struct tty_struct *tty,
625                               const __u8 *data, char *flags, int count)
626 {
627         struct tty_ldisc *ld;
628         if (!tty)
629                 return;
630         ld = tty_ldisc_ref(tty);
631         if (ld) {
632                 if (ld->ops->receive_buf)
633                         ld->ops->receive_buf(tty, data, flags, count);
634                 tty_ldisc_deref(ld);
635         }
636 }
637
638 /* tty callbacks */
639
640 static int open(struct tty_struct *tty, struct file *filp)
641 {
642         struct slgt_info *info;
643         int retval, line;
644         unsigned long flags;
645
646         line = tty->index;
647         if ((line < 0) || (line >= slgt_device_count)) {
648                 DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
649                 return -ENODEV;
650         }
651
652         info = slgt_device_list;
653         while(info && info->line != line)
654                 info = info->next_device;
655         if (sanity_check(info, tty->name, "open"))
656                 return -ENODEV;
657         if (info->init_error) {
658                 DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
659                 return -ENODEV;
660         }
661
662         tty->driver_data = info;
663         info->port.tty = tty;
664
665         DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->port.count));
666
667         /* If port is closing, signal caller to try again */
668         if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
669                 if (info->port.flags & ASYNC_CLOSING)
670                         interruptible_sleep_on(&info->port.close_wait);
671                 retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
672                         -EAGAIN : -ERESTARTSYS);
673                 goto cleanup;
674         }
675
676         info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
677
678         spin_lock_irqsave(&info->netlock, flags);
679         if (info->netcount) {
680                 retval = -EBUSY;
681                 spin_unlock_irqrestore(&info->netlock, flags);
682                 goto cleanup;
683         }
684         info->port.count++;
685         spin_unlock_irqrestore(&info->netlock, flags);
686
687         if (info->port.count == 1) {
688                 /* 1st open on this device, init hardware */
689                 retval = startup(info);
690                 if (retval < 0)
691                         goto cleanup;
692         }
693
694         retval = block_til_ready(tty, filp, info);
695         if (retval) {
696                 DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
697                 goto cleanup;
698         }
699
700         retval = 0;
701
702 cleanup:
703         if (retval) {
704                 if (tty->count == 1)
705                         info->port.tty = NULL; /* tty layer will release tty struct */
706                 if(info->port.count)
707                         info->port.count--;
708         }
709
710         DBGINFO(("%s open rc=%d\n", info->device_name, retval));
711         return retval;
712 }
713
714 static void close(struct tty_struct *tty, struct file *filp)
715 {
716         struct slgt_info *info = tty->driver_data;
717
718         if (sanity_check(info, tty->name, "close"))
719                 return;
720         DBGINFO(("%s close entry, count=%d\n", info->device_name, info->port.count));
721
722         if (!info->port.count)
723                 return;
724
725         if (tty_hung_up_p(filp))
726                 goto cleanup;
727
728         if ((tty->count == 1) && (info->port.count != 1)) {
729                 /*
730                  * tty->count is 1 and the tty structure will be freed.
731                  * info->port.count should be one in this case.
732                  * if it's not, correct it so that the port is shutdown.
733                  */
734                 DBGERR(("%s close: bad refcount; tty->count=1, "
735                        "info->port.count=%d\n", info->device_name, info->port.count));
736                 info->port.count = 1;
737         }
738
739         info->port.count--;
740
741         /* if at least one open remaining, leave hardware active */
742         if (info->port.count)
743                 goto cleanup;
744
745         info->port.flags |= ASYNC_CLOSING;
746
747         /* set tty->closing to notify line discipline to
748          * only process XON/XOFF characters. Only the N_TTY
749          * discipline appears to use this (ppp does not).
750          */
751         tty->closing = 1;
752
753         /* wait for transmit data to clear all layers */
754
755         if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE) {
756                 DBGINFO(("%s call tty_wait_until_sent\n", info->device_name));
757                 tty_wait_until_sent(tty, info->port.closing_wait);
758         }
759
760         if (info->port.flags & ASYNC_INITIALIZED)
761                 wait_until_sent(tty, info->timeout);
762         flush_buffer(tty);
763         tty_ldisc_flush(tty);
764
765         shutdown(info);
766
767         tty->closing = 0;
768         info->port.tty = NULL;
769
770         if (info->port.blocked_open) {
771                 if (info->port.close_delay) {
772                         msleep_interruptible(jiffies_to_msecs(info->port.close_delay));
773                 }
774                 wake_up_interruptible(&info->port.open_wait);
775         }
776
777         info->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
778
779         wake_up_interruptible(&info->port.close_wait);
780
781 cleanup:
782         DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->port.count));
783 }
784
785 static void hangup(struct tty_struct *tty)
786 {
787         struct slgt_info *info = tty->driver_data;
788
789         if (sanity_check(info, tty->name, "hangup"))
790                 return;
791         DBGINFO(("%s hangup\n", info->device_name));
792
793         flush_buffer(tty);
794         shutdown(info);
795
796         info->port.count = 0;
797         info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
798         info->port.tty = NULL;
799
800         wake_up_interruptible(&info->port.open_wait);
801 }
802
803 static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
804 {
805         struct slgt_info *info = tty->driver_data;
806         unsigned long flags;
807
808         DBGINFO(("%s set_termios\n", tty->driver->name));
809
810         change_params(info);
811
812         /* Handle transition to B0 status */
813         if (old_termios->c_cflag & CBAUD &&
814             !(tty->termios->c_cflag & CBAUD)) {
815                 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
816                 spin_lock_irqsave(&info->lock,flags);
817                 set_signals(info);
818                 spin_unlock_irqrestore(&info->lock,flags);
819         }
820
821         /* Handle transition away from B0 status */
822         if (!(old_termios->c_cflag & CBAUD) &&
823             tty->termios->c_cflag & CBAUD) {
824                 info->signals |= SerialSignal_DTR;
825                 if (!(tty->termios->c_cflag & CRTSCTS) ||
826                     !test_bit(TTY_THROTTLED, &tty->flags)) {
827                         info->signals |= SerialSignal_RTS;
828                 }
829                 spin_lock_irqsave(&info->lock,flags);
830                 set_signals(info);
831                 spin_unlock_irqrestore(&info->lock,flags);
832         }
833
834         /* Handle turning off CRTSCTS */
835         if (old_termios->c_cflag & CRTSCTS &&
836             !(tty->termios->c_cflag & CRTSCTS)) {
837                 tty->hw_stopped = 0;
838                 tx_release(tty);
839         }
840 }
841
842 static int write(struct tty_struct *tty,
843                  const unsigned char *buf, int count)
844 {
845         int ret = 0;
846         struct slgt_info *info = tty->driver_data;
847         unsigned long flags;
848
849         if (sanity_check(info, tty->name, "write"))
850                 goto cleanup;
851         DBGINFO(("%s write count=%d\n", info->device_name, count));
852
853         if (!info->tx_buf)
854                 goto cleanup;
855
856         if (count > info->max_frame_size) {
857                 ret = -EIO;
858                 goto cleanup;
859         }
860
861         if (!count)
862                 goto cleanup;
863
864         if (info->params.mode == MGSL_MODE_RAW ||
865             info->params.mode == MGSL_MODE_MONOSYNC ||
866             info->params.mode == MGSL_MODE_BISYNC) {
867                 unsigned int bufs_needed = (count/DMABUFSIZE);
868                 unsigned int bufs_free = free_tbuf_count(info);
869                 if (count % DMABUFSIZE)
870                         ++bufs_needed;
871                 if (bufs_needed > bufs_free)
872                         goto cleanup;
873         } else {
874                 if (info->tx_active)
875                         goto cleanup;
876                 if (info->tx_count) {
877                         /* send accumulated data from send_char() calls */
878                         /* as frame and wait before accepting more data. */
879                         tx_load(info, info->tx_buf, info->tx_count);
880                         goto start;
881                 }
882         }
883
884         ret = info->tx_count = count;
885         tx_load(info, buf, count);
886         goto start;
887
888 start:
889         if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
890                 spin_lock_irqsave(&info->lock,flags);
891                 if (!info->tx_active)
892                         tx_start(info);
893                 else
894                         tdma_start(info);
895                 spin_unlock_irqrestore(&info->lock,flags);
896         }
897
898 cleanup:
899         DBGINFO(("%s write rc=%d\n", info->device_name, ret));
900         return ret;
901 }
902
903 static int put_char(struct tty_struct *tty, unsigned char ch)
904 {
905         struct slgt_info *info = tty->driver_data;
906         unsigned long flags;
907         int ret = 0;
908
909         if (sanity_check(info, tty->name, "put_char"))
910                 return 0;
911         DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
912         if (!info->tx_buf)
913                 return 0;
914         spin_lock_irqsave(&info->lock,flags);
915         if (!info->tx_active && (info->tx_count < info->max_frame_size)) {
916                 info->tx_buf[info->tx_count++] = ch;
917                 ret = 1;
918         }
919         spin_unlock_irqrestore(&info->lock,flags);
920         return ret;
921 }
922
923 static void send_xchar(struct tty_struct *tty, char ch)
924 {
925         struct slgt_info *info = tty->driver_data;
926         unsigned long flags;
927
928         if (sanity_check(info, tty->name, "send_xchar"))
929                 return;
930         DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
931         info->x_char = ch;
932         if (ch) {
933                 spin_lock_irqsave(&info->lock,flags);
934                 if (!info->tx_enabled)
935                         tx_start(info);
936                 spin_unlock_irqrestore(&info->lock,flags);
937         }
938 }
939
940 static void wait_until_sent(struct tty_struct *tty, int timeout)
941 {
942         struct slgt_info *info = tty->driver_data;
943         unsigned long orig_jiffies, char_time;
944
945         if (!info )
946                 return;
947         if (sanity_check(info, tty->name, "wait_until_sent"))
948                 return;
949         DBGINFO(("%s wait_until_sent entry\n", info->device_name));
950         if (!(info->port.flags & ASYNC_INITIALIZED))
951                 goto exit;
952
953         orig_jiffies = jiffies;
954
955         /* Set check interval to 1/5 of estimated time to
956          * send a character, and make it at least 1. The check
957          * interval should also be less than the timeout.
958          * Note: use tight timings here to satisfy the NIST-PCTS.
959          */
960
961         lock_kernel();
962
963         if (info->params.data_rate) {
964                 char_time = info->timeout/(32 * 5);
965                 if (!char_time)
966                         char_time++;
967         } else
968                 char_time = 1;
969
970         if (timeout)
971                 char_time = min_t(unsigned long, char_time, timeout);
972
973         while (info->tx_active) {
974                 msleep_interruptible(jiffies_to_msecs(char_time));
975                 if (signal_pending(current))
976                         break;
977                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
978                         break;
979         }
980         unlock_kernel();
981
982 exit:
983         DBGINFO(("%s wait_until_sent exit\n", info->device_name));
984 }
985
986 static int write_room(struct tty_struct *tty)
987 {
988         struct slgt_info *info = tty->driver_data;
989         int ret;
990
991         if (sanity_check(info, tty->name, "write_room"))
992                 return 0;
993         ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
994         DBGINFO(("%s write_room=%d\n", info->device_name, ret));
995         return ret;
996 }
997
998 static void flush_chars(struct tty_struct *tty)
999 {
1000         struct slgt_info *info = tty->driver_data;
1001         unsigned long flags;
1002
1003         if (sanity_check(info, tty->name, "flush_chars"))
1004                 return;
1005         DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
1006
1007         if (info->tx_count <= 0 || tty->stopped ||
1008             tty->hw_stopped || !info->tx_buf)
1009                 return;
1010
1011         DBGINFO(("%s flush_chars start transmit\n", info->device_name));
1012
1013         spin_lock_irqsave(&info->lock,flags);
1014         if (!info->tx_active && info->tx_count) {
1015                 tx_load(info, info->tx_buf,info->tx_count);
1016                 tx_start(info);
1017         }
1018         spin_unlock_irqrestore(&info->lock,flags);
1019 }
1020
1021 static void flush_buffer(struct tty_struct *tty)
1022 {
1023         struct slgt_info *info = tty->driver_data;
1024         unsigned long flags;
1025
1026         if (sanity_check(info, tty->name, "flush_buffer"))
1027                 return;
1028         DBGINFO(("%s flush_buffer\n", info->device_name));
1029
1030         spin_lock_irqsave(&info->lock,flags);
1031         if (!info->tx_active)
1032                 info->tx_count = 0;
1033         spin_unlock_irqrestore(&info->lock,flags);
1034
1035         tty_wakeup(tty);
1036 }
1037
1038 /*
1039  * throttle (stop) transmitter
1040  */
1041 static void tx_hold(struct tty_struct *tty)
1042 {
1043         struct slgt_info *info = tty->driver_data;
1044         unsigned long flags;
1045
1046         if (sanity_check(info, tty->name, "tx_hold"))
1047                 return;
1048         DBGINFO(("%s tx_hold\n", info->device_name));
1049         spin_lock_irqsave(&info->lock,flags);
1050         if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
1051                 tx_stop(info);
1052         spin_unlock_irqrestore(&info->lock,flags);
1053 }
1054
1055 /*
1056  * release (start) transmitter
1057  */
1058 static void tx_release(struct tty_struct *tty)
1059 {
1060         struct slgt_info *info = tty->driver_data;
1061         unsigned long flags;
1062
1063         if (sanity_check(info, tty->name, "tx_release"))
1064                 return;
1065         DBGINFO(("%s tx_release\n", info->device_name));
1066         spin_lock_irqsave(&info->lock,flags);
1067         if (!info->tx_active && info->tx_count) {
1068                 tx_load(info, info->tx_buf, info->tx_count);
1069                 tx_start(info);
1070         }
1071         spin_unlock_irqrestore(&info->lock,flags);
1072 }
1073
1074 /*
1075  * Service an IOCTL request
1076  *
1077  * Arguments
1078  *
1079  *      tty     pointer to tty instance data
1080  *      file    pointer to associated file object for device
1081  *      cmd     IOCTL command code
1082  *      arg     command argument/context
1083  *
1084  * Return 0 if success, otherwise error code
1085  */
1086 static int ioctl(struct tty_struct *tty, struct file *file,
1087                  unsigned int cmd, unsigned long arg)
1088 {
1089         struct slgt_info *info = tty->driver_data;
1090         struct mgsl_icount cnow;        /* kernel counter temps */
1091         struct serial_icounter_struct __user *p_cuser;  /* user space */
1092         unsigned long flags;
1093         void __user *argp = (void __user *)arg;
1094         int ret;
1095
1096         if (sanity_check(info, tty->name, "ioctl"))
1097                 return -ENODEV;
1098         DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
1099
1100         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1101             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1102                 if (tty->flags & (1 << TTY_IO_ERROR))
1103                     return -EIO;
1104         }
1105
1106         lock_kernel();
1107
1108         switch (cmd) {
1109         case MGSL_IOCGPARAMS:
1110                 ret = get_params(info, argp);
1111                 break;
1112         case MGSL_IOCSPARAMS:
1113                 ret = set_params(info, argp);
1114                 break;
1115         case MGSL_IOCGTXIDLE:
1116                 ret = get_txidle(info, argp);
1117                 break;
1118         case MGSL_IOCSTXIDLE:
1119                 ret = set_txidle(info, (int)arg);
1120                 break;
1121         case MGSL_IOCTXENABLE:
1122                 ret = tx_enable(info, (int)arg);
1123                 break;
1124         case MGSL_IOCRXENABLE:
1125                 ret = rx_enable(info, (int)arg);
1126                 break;
1127         case MGSL_IOCTXABORT:
1128                 ret = tx_abort(info);
1129                 break;
1130         case MGSL_IOCGSTATS:
1131                 ret = get_stats(info, argp);
1132                 break;
1133         case MGSL_IOCWAITEVENT:
1134                 ret = wait_mgsl_event(info, argp);
1135                 break;
1136         case TIOCMIWAIT:
1137                 ret = modem_input_wait(info,(int)arg);
1138                 break;
1139         case MGSL_IOCGIF:
1140                 ret = get_interface(info, argp);
1141                 break;
1142         case MGSL_IOCSIF:
1143                 ret = set_interface(info,(int)arg);
1144                 break;
1145         case MGSL_IOCSGPIO:
1146                 ret = set_gpio(info, argp);
1147                 break;
1148         case MGSL_IOCGGPIO:
1149                 ret = get_gpio(info, argp);
1150                 break;
1151         case MGSL_IOCWAITGPIO:
1152                 ret = wait_gpio(info, argp);
1153                 break;
1154         case TIOCGICOUNT:
1155                 spin_lock_irqsave(&info->lock,flags);
1156                 cnow = info->icount;
1157                 spin_unlock_irqrestore(&info->lock,flags);
1158                 p_cuser = argp;
1159                 if (put_user(cnow.cts, &p_cuser->cts) ||
1160                     put_user(cnow.dsr, &p_cuser->dsr) ||
1161                     put_user(cnow.rng, &p_cuser->rng) ||
1162                     put_user(cnow.dcd, &p_cuser->dcd) ||
1163                     put_user(cnow.rx, &p_cuser->rx) ||
1164                     put_user(cnow.tx, &p_cuser->tx) ||
1165                     put_user(cnow.frame, &p_cuser->frame) ||
1166                     put_user(cnow.overrun, &p_cuser->overrun) ||
1167                     put_user(cnow.parity, &p_cuser->parity) ||
1168                     put_user(cnow.brk, &p_cuser->brk) ||
1169                     put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1170                         ret = -EFAULT;
1171                 ret = 0;
1172                 break;
1173         default:
1174                 ret = -ENOIOCTLCMD;
1175         }
1176         unlock_kernel();
1177         return ret;
1178 }
1179
1180 /*
1181  * support for 32 bit ioctl calls on 64 bit systems
1182  */
1183 #ifdef CONFIG_COMPAT
1184 static long get_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *user_params)
1185 {
1186         struct MGSL_PARAMS32 tmp_params;
1187
1188         DBGINFO(("%s get_params32\n", info->device_name));
1189         tmp_params.mode            = (compat_ulong_t)info->params.mode;
1190         tmp_params.loopback        = info->params.loopback;
1191         tmp_params.flags           = info->params.flags;
1192         tmp_params.encoding        = info->params.encoding;
1193         tmp_params.clock_speed     = (compat_ulong_t)info->params.clock_speed;
1194         tmp_params.addr_filter     = info->params.addr_filter;
1195         tmp_params.crc_type        = info->params.crc_type;
1196         tmp_params.preamble_length = info->params.preamble_length;
1197         tmp_params.preamble        = info->params.preamble;
1198         tmp_params.data_rate       = (compat_ulong_t)info->params.data_rate;
1199         tmp_params.data_bits       = info->params.data_bits;
1200         tmp_params.stop_bits       = info->params.stop_bits;
1201         tmp_params.parity          = info->params.parity;
1202         if (copy_to_user(user_params, &tmp_params, sizeof(struct MGSL_PARAMS32)))
1203                 return -EFAULT;
1204         return 0;
1205 }
1206
1207 static long set_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *new_params)
1208 {
1209         struct MGSL_PARAMS32 tmp_params;
1210
1211         DBGINFO(("%s set_params32\n", info->device_name));
1212         if (copy_from_user(&tmp_params, new_params, sizeof(struct MGSL_PARAMS32)))
1213                 return -EFAULT;
1214
1215         spin_lock(&info->lock);
1216         info->params.mode            = tmp_params.mode;
1217         info->params.loopback        = tmp_params.loopback;
1218         info->params.flags           = tmp_params.flags;
1219         info->params.encoding        = tmp_params.encoding;
1220         info->params.clock_speed     = tmp_params.clock_speed;
1221         info->params.addr_filter     = tmp_params.addr_filter;
1222         info->params.crc_type        = tmp_params.crc_type;
1223         info->params.preamble_length = tmp_params.preamble_length;
1224         info->params.preamble        = tmp_params.preamble;
1225         info->params.data_rate       = tmp_params.data_rate;
1226         info->params.data_bits       = tmp_params.data_bits;
1227         info->params.stop_bits       = tmp_params.stop_bits;
1228         info->params.parity          = tmp_params.parity;
1229         spin_unlock(&info->lock);
1230
1231         change_params(info);
1232
1233         return 0;
1234 }
1235
1236 static long slgt_compat_ioctl(struct tty_struct *tty, struct file *file,
1237                          unsigned int cmd, unsigned long arg)
1238 {
1239         struct slgt_info *info = tty->driver_data;
1240         int rc = -ENOIOCTLCMD;
1241
1242         if (sanity_check(info, tty->name, "compat_ioctl"))
1243                 return -ENODEV;
1244         DBGINFO(("%s compat_ioctl() cmd=%08X\n", info->device_name, cmd));
1245
1246         switch (cmd) {
1247
1248         case MGSL_IOCSPARAMS32:
1249                 rc = set_params32(info, compat_ptr(arg));
1250                 break;
1251
1252         case MGSL_IOCGPARAMS32:
1253                 rc = get_params32(info, compat_ptr(arg));
1254                 break;
1255
1256         case MGSL_IOCGPARAMS:
1257         case MGSL_IOCSPARAMS:
1258         case MGSL_IOCGTXIDLE:
1259         case MGSL_IOCGSTATS:
1260         case MGSL_IOCWAITEVENT:
1261         case MGSL_IOCGIF:
1262         case MGSL_IOCSGPIO:
1263         case MGSL_IOCGGPIO:
1264         case MGSL_IOCWAITGPIO:
1265         case TIOCGICOUNT:
1266                 rc = ioctl(tty, file, cmd, (unsigned long)(compat_ptr(arg)));
1267                 break;
1268
1269         case MGSL_IOCSTXIDLE:
1270         case MGSL_IOCTXENABLE:
1271         case MGSL_IOCRXENABLE:
1272         case MGSL_IOCTXABORT:
1273         case TIOCMIWAIT:
1274         case MGSL_IOCSIF:
1275                 rc = ioctl(tty, file, cmd, arg);
1276                 break;
1277         }
1278
1279         DBGINFO(("%s compat_ioctl() cmd=%08X rc=%d\n", info->device_name, cmd, rc));
1280         return rc;
1281 }
1282 #else
1283 #define slgt_compat_ioctl NULL
1284 #endif /* ifdef CONFIG_COMPAT */
1285
1286 /*
1287  * proc fs support
1288  */
1289 static inline int line_info(char *buf, struct slgt_info *info)
1290 {
1291         char stat_buf[30];
1292         int ret;
1293         unsigned long flags;
1294
1295         ret = sprintf(buf, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
1296                       info->device_name, info->phys_reg_addr,
1297                       info->irq_level, info->max_frame_size);
1298
1299         /* output current serial signal states */
1300         spin_lock_irqsave(&info->lock,flags);
1301         get_signals(info);
1302         spin_unlock_irqrestore(&info->lock,flags);
1303
1304         stat_buf[0] = 0;
1305         stat_buf[1] = 0;
1306         if (info->signals & SerialSignal_RTS)
1307                 strcat(stat_buf, "|RTS");
1308         if (info->signals & SerialSignal_CTS)
1309                 strcat(stat_buf, "|CTS");
1310         if (info->signals & SerialSignal_DTR)
1311                 strcat(stat_buf, "|DTR");
1312         if (info->signals & SerialSignal_DSR)
1313                 strcat(stat_buf, "|DSR");
1314         if (info->signals & SerialSignal_DCD)
1315                 strcat(stat_buf, "|CD");
1316         if (info->signals & SerialSignal_RI)
1317                 strcat(stat_buf, "|RI");
1318
1319         if (info->params.mode != MGSL_MODE_ASYNC) {
1320                 ret += sprintf(buf+ret, "\tHDLC txok:%d rxok:%d",
1321                                info->icount.txok, info->icount.rxok);
1322                 if (info->icount.txunder)
1323                         ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
1324                 if (info->icount.txabort)
1325                         ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
1326                 if (info->icount.rxshort)
1327                         ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
1328                 if (info->icount.rxlong)
1329                         ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
1330                 if (info->icount.rxover)
1331                         ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
1332                 if (info->icount.rxcrc)
1333                         ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
1334         } else {
1335                 ret += sprintf(buf+ret, "\tASYNC tx:%d rx:%d",
1336                                info->icount.tx, info->icount.rx);
1337                 if (info->icount.frame)
1338                         ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
1339                 if (info->icount.parity)
1340                         ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
1341                 if (info->icount.brk)
1342                         ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
1343                 if (info->icount.overrun)
1344                         ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
1345         }
1346
1347         /* Append serial signal status to end */
1348         ret += sprintf(buf+ret, " %s\n", stat_buf+1);
1349
1350         ret += sprintf(buf+ret, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1351                        info->tx_active,info->bh_requested,info->bh_running,
1352                        info->pending_bh);
1353
1354         return ret;
1355 }
1356
1357 /* Called to print information about devices
1358  */
1359 static int read_proc(char *page, char **start, off_t off, int count,
1360                      int *eof, void *data)
1361 {
1362         int len = 0, l;
1363         off_t   begin = 0;
1364         struct slgt_info *info;
1365
1366         len += sprintf(page, "synclink_gt driver:%s\n", driver_version);
1367
1368         info = slgt_device_list;
1369         while( info ) {
1370                 l = line_info(page + len, info);
1371                 len += l;
1372                 if (len+begin > off+count)
1373                         goto done;
1374                 if (len+begin < off) {
1375                         begin += len;
1376                         len = 0;
1377                 }
1378                 info = info->next_device;
1379         }
1380
1381         *eof = 1;
1382 done:
1383         if (off >= len+begin)
1384                 return 0;
1385         *start = page + (off-begin);
1386         return ((count < begin+len-off) ? count : begin+len-off);
1387 }
1388
1389 /*
1390  * return count of bytes in transmit buffer
1391  */
1392 static int chars_in_buffer(struct tty_struct *tty)
1393 {
1394         struct slgt_info *info = tty->driver_data;
1395         if (sanity_check(info, tty->name, "chars_in_buffer"))
1396                 return 0;
1397         DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, info->tx_count));
1398         return info->tx_count;
1399 }
1400
1401 /*
1402  * signal remote device to throttle send data (our receive data)
1403  */
1404 static void throttle(struct tty_struct * tty)
1405 {
1406         struct slgt_info *info = tty->driver_data;
1407         unsigned long flags;
1408
1409         if (sanity_check(info, tty->name, "throttle"))
1410                 return;
1411         DBGINFO(("%s throttle\n", info->device_name));
1412         if (I_IXOFF(tty))
1413                 send_xchar(tty, STOP_CHAR(tty));
1414         if (tty->termios->c_cflag & CRTSCTS) {
1415                 spin_lock_irqsave(&info->lock,flags);
1416                 info->signals &= ~SerialSignal_RTS;
1417                 set_signals(info);
1418                 spin_unlock_irqrestore(&info->lock,flags);
1419         }
1420 }
1421
1422 /*
1423  * signal remote device to stop throttling send data (our receive data)
1424  */
1425 static void unthrottle(struct tty_struct * tty)
1426 {
1427         struct slgt_info *info = tty->driver_data;
1428         unsigned long flags;
1429
1430         if (sanity_check(info, tty->name, "unthrottle"))
1431                 return;
1432         DBGINFO(("%s unthrottle\n", info->device_name));
1433         if (I_IXOFF(tty)) {
1434                 if (info->x_char)
1435                         info->x_char = 0;
1436                 else
1437                         send_xchar(tty, START_CHAR(tty));
1438         }
1439         if (tty->termios->c_cflag & CRTSCTS) {
1440                 spin_lock_irqsave(&info->lock,flags);
1441                 info->signals |= SerialSignal_RTS;
1442                 set_signals(info);
1443                 spin_unlock_irqrestore(&info->lock,flags);
1444         }
1445 }
1446
1447 /*
1448  * set or clear transmit break condition
1449  * break_state  -1=set break condition, 0=clear
1450  */
1451 static void set_break(struct tty_struct *tty, int break_state)
1452 {
1453         struct slgt_info *info = tty->driver_data;
1454         unsigned short value;
1455         unsigned long flags;
1456
1457         if (sanity_check(info, tty->name, "set_break"))
1458                 return;
1459         DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
1460
1461         spin_lock_irqsave(&info->lock,flags);
1462         value = rd_reg16(info, TCR);
1463         if (break_state == -1)
1464                 value |= BIT6;
1465         else
1466                 value &= ~BIT6;
1467         wr_reg16(info, TCR, value);
1468         spin_unlock_irqrestore(&info->lock,flags);
1469 }
1470
1471 #if SYNCLINK_GENERIC_HDLC
1472
1473 /**
1474  * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1475  * set encoding and frame check sequence (FCS) options
1476  *
1477  * dev       pointer to network device structure
1478  * encoding  serial encoding setting
1479  * parity    FCS setting
1480  *
1481  * returns 0 if success, otherwise error code
1482  */
1483 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
1484                           unsigned short parity)
1485 {
1486         struct slgt_info *info = dev_to_port(dev);
1487         unsigned char  new_encoding;
1488         unsigned short new_crctype;
1489
1490         /* return error if TTY interface open */
1491         if (info->port.count)
1492                 return -EBUSY;
1493
1494         DBGINFO(("%s hdlcdev_attach\n", info->device_name));
1495
1496         switch (encoding)
1497         {
1498         case ENCODING_NRZ:        new_encoding = HDLC_ENCODING_NRZ; break;
1499         case ENCODING_NRZI:       new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
1500         case ENCODING_FM_MARK:    new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
1501         case ENCODING_FM_SPACE:   new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
1502         case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
1503         default: return -EINVAL;
1504         }
1505
1506         switch (parity)
1507         {
1508         case PARITY_NONE:            new_crctype = HDLC_CRC_NONE; break;
1509         case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
1510         case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
1511         default: return -EINVAL;
1512         }
1513
1514         info->params.encoding = new_encoding;
1515         info->params.crc_type = new_crctype;
1516
1517         /* if network interface up, reprogram hardware */
1518         if (info->netcount)
1519                 program_hw(info);
1520
1521         return 0;
1522 }
1523
1524 /**
1525  * called by generic HDLC layer to send frame
1526  *
1527  * skb  socket buffer containing HDLC frame
1528  * dev  pointer to network device structure
1529  *
1530  * returns 0 if success, otherwise error code
1531  */
1532 static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
1533 {
1534         struct slgt_info *info = dev_to_port(dev);
1535         unsigned long flags;
1536
1537         DBGINFO(("%s hdlc_xmit\n", dev->name));
1538
1539         /* stop sending until this frame completes */
1540         netif_stop_queue(dev);
1541
1542         /* copy data to device buffers */
1543         info->tx_count = skb->len;
1544         tx_load(info, skb->data, skb->len);
1545
1546         /* update network statistics */
1547         dev->stats.tx_packets++;
1548         dev->stats.tx_bytes += skb->len;
1549
1550         /* done with socket buffer, so free it */
1551         dev_kfree_skb(skb);
1552
1553         /* save start time for transmit timeout detection */
1554         dev->trans_start = jiffies;
1555
1556         /* start hardware transmitter if necessary */
1557         spin_lock_irqsave(&info->lock,flags);
1558         if (!info->tx_active)
1559                 tx_start(info);
1560         spin_unlock_irqrestore(&info->lock,flags);
1561
1562         return 0;
1563 }
1564
1565 /**
1566  * called by network layer when interface enabled
1567  * claim resources and initialize hardware
1568  *
1569  * dev  pointer to network device structure
1570  *
1571  * returns 0 if success, otherwise error code
1572  */
1573 static int hdlcdev_open(struct net_device *dev)
1574 {
1575         struct slgt_info *info = dev_to_port(dev);
1576         int rc;
1577         unsigned long flags;
1578
1579         if (!try_module_get(THIS_MODULE))
1580                 return -EBUSY;
1581
1582         DBGINFO(("%s hdlcdev_open\n", dev->name));
1583
1584         /* generic HDLC layer open processing */
1585         if ((rc = hdlc_open(dev)))
1586                 return rc;
1587
1588         /* arbitrate between network and tty opens */
1589         spin_lock_irqsave(&info->netlock, flags);
1590         if (info->port.count != 0 || info->netcount != 0) {
1591                 DBGINFO(("%s hdlc_open busy\n", dev->name));
1592                 spin_unlock_irqrestore(&info->netlock, flags);
1593                 return -EBUSY;
1594         }
1595         info->netcount=1;
1596         spin_unlock_irqrestore(&info->netlock, flags);
1597
1598         /* claim resources and init adapter */
1599         if ((rc = startup(info)) != 0) {
1600                 spin_lock_irqsave(&info->netlock, flags);
1601                 info->netcount=0;
1602                 spin_unlock_irqrestore(&info->netlock, flags);
1603                 return rc;
1604         }
1605
1606         /* assert DTR and RTS, apply hardware settings */
1607         info->signals |= SerialSignal_RTS + SerialSignal_DTR;
1608         program_hw(info);
1609
1610         /* enable network layer transmit */
1611         dev->trans_start = jiffies;
1612         netif_start_queue(dev);
1613
1614         /* inform generic HDLC layer of current DCD status */
1615         spin_lock_irqsave(&info->lock, flags);
1616         get_signals(info);
1617         spin_unlock_irqrestore(&info->lock, flags);
1618         if (info->signals & SerialSignal_DCD)
1619                 netif_carrier_on(dev);
1620         else
1621                 netif_carrier_off(dev);
1622         return 0;
1623 }
1624
1625 /**
1626  * called by network layer when interface is disabled
1627  * shutdown hardware and release resources
1628  *
1629  * dev  pointer to network device structure
1630  *
1631  * returns 0 if success, otherwise error code
1632  */
1633 static int hdlcdev_close(struct net_device *dev)
1634 {
1635         struct slgt_info *info = dev_to_port(dev);
1636         unsigned long flags;
1637
1638         DBGINFO(("%s hdlcdev_close\n", dev->name));
1639
1640         netif_stop_queue(dev);
1641
1642         /* shutdown adapter and release resources */
1643         shutdown(info);
1644
1645         hdlc_close(dev);
1646
1647         spin_lock_irqsave(&info->netlock, flags);
1648         info->netcount=0;
1649         spin_unlock_irqrestore(&info->netlock, flags);
1650
1651         module_put(THIS_MODULE);
1652         return 0;
1653 }
1654
1655 /**
1656  * called by network layer to process IOCTL call to network device
1657  *
1658  * dev  pointer to network device structure
1659  * ifr  pointer to network interface request structure
1660  * cmd  IOCTL command code
1661  *
1662  * returns 0 if success, otherwise error code
1663  */
1664 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1665 {
1666         const size_t size = sizeof(sync_serial_settings);
1667         sync_serial_settings new_line;
1668         sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1669         struct slgt_info *info = dev_to_port(dev);
1670         unsigned int flags;
1671
1672         DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
1673
1674         /* return error if TTY interface open */
1675         if (info->port.count)
1676                 return -EBUSY;
1677
1678         if (cmd != SIOCWANDEV)
1679                 return hdlc_ioctl(dev, ifr, cmd);
1680
1681         switch(ifr->ifr_settings.type) {
1682         case IF_GET_IFACE: /* return current sync_serial_settings */
1683
1684                 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1685                 if (ifr->ifr_settings.size < size) {
1686                         ifr->ifr_settings.size = size; /* data size wanted */
1687                         return -ENOBUFS;
1688                 }
1689
1690                 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1691                                               HDLC_FLAG_RXC_BRG    | HDLC_FLAG_RXC_TXCPIN |
1692                                               HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1693                                               HDLC_FLAG_TXC_BRG    | HDLC_FLAG_TXC_RXCPIN);
1694
1695                 switch (flags){
1696                 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
1697                 case (HDLC_FLAG_RXC_BRG    | HDLC_FLAG_TXC_BRG):    new_line.clock_type = CLOCK_INT; break;
1698                 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG):    new_line.clock_type = CLOCK_TXINT; break;
1699                 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
1700                 default: new_line.clock_type = CLOCK_DEFAULT;
1701                 }
1702
1703                 new_line.clock_rate = info->params.clock_speed;
1704                 new_line.loopback   = info->params.loopback ? 1:0;
1705
1706                 if (copy_to_user(line, &new_line, size))
1707                         return -EFAULT;
1708                 return 0;
1709
1710         case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
1711
1712                 if(!capable(CAP_NET_ADMIN))
1713                         return -EPERM;
1714                 if (copy_from_user(&new_line, line, size))
1715                         return -EFAULT;
1716
1717                 switch (new_line.clock_type)
1718                 {
1719                 case CLOCK_EXT:      flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
1720                 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
1721                 case CLOCK_INT:      flags = HDLC_FLAG_RXC_BRG    | HDLC_FLAG_TXC_BRG;    break;
1722                 case CLOCK_TXINT:    flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG;    break;
1723                 case CLOCK_DEFAULT:  flags = info->params.flags &
1724                                              (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1725                                               HDLC_FLAG_RXC_BRG    | HDLC_FLAG_RXC_TXCPIN |
1726                                               HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1727                                               HDLC_FLAG_TXC_BRG    | HDLC_FLAG_TXC_RXCPIN); break;
1728                 default: return -EINVAL;
1729                 }
1730
1731                 if (new_line.loopback != 0 && new_line.loopback != 1)
1732                         return -EINVAL;
1733
1734                 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1735                                         HDLC_FLAG_RXC_BRG    | HDLC_FLAG_RXC_TXCPIN |
1736                                         HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1737                                         HDLC_FLAG_TXC_BRG    | HDLC_FLAG_TXC_RXCPIN);
1738                 info->params.flags |= flags;
1739
1740                 info->params.loopback = new_line.loopback;
1741
1742                 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
1743                         info->params.clock_speed = new_line.clock_rate;
1744                 else
1745                         info->params.clock_speed = 0;
1746
1747                 /* if network interface up, reprogram hardware */
1748                 if (info->netcount)
1749                         program_hw(info);
1750                 return 0;
1751
1752         default:
1753                 return hdlc_ioctl(dev, ifr, cmd);
1754         }
1755 }
1756
1757 /**
1758  * called by network layer when transmit timeout is detected
1759  *
1760  * dev  pointer to network device structure
1761  */
1762 static void hdlcdev_tx_timeout(struct net_device *dev)
1763 {
1764         struct slgt_info *info = dev_to_port(dev);
1765         unsigned long flags;
1766
1767         DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
1768
1769         dev->stats.tx_errors++;
1770         dev->stats.tx_aborted_errors++;
1771
1772         spin_lock_irqsave(&info->lock,flags);
1773         tx_stop(info);
1774         spin_unlock_irqrestore(&info->lock,flags);
1775
1776         netif_wake_queue(dev);
1777 }
1778
1779 /**
1780  * called by device driver when transmit completes
1781  * reenable network layer transmit if stopped
1782  *
1783  * info  pointer to device instance information
1784  */
1785 static void hdlcdev_tx_done(struct slgt_info *info)
1786 {
1787         if (netif_queue_stopped(info->netdev))
1788                 netif_wake_queue(info->netdev);
1789 }
1790
1791 /**
1792  * called by device driver when frame received
1793  * pass frame to network layer
1794  *
1795  * info  pointer to device instance information
1796  * buf   pointer to buffer contianing frame data
1797  * size  count of data bytes in buf
1798  */
1799 static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
1800 {
1801         struct sk_buff *skb = dev_alloc_skb(size);
1802         struct net_device *dev = info->netdev;
1803
1804         DBGINFO(("%s hdlcdev_rx\n", dev->name));
1805
1806         if (skb == NULL) {
1807                 DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
1808                 dev->stats.rx_dropped++;
1809                 return;
1810         }
1811
1812         memcpy(skb_put(skb, size), buf, size);
1813
1814         skb->protocol = hdlc_type_trans(skb, dev);
1815
1816         dev->stats.rx_packets++;
1817         dev->stats.rx_bytes += size;
1818
1819         netif_rx(skb);
1820
1821         dev->last_rx = jiffies;
1822 }
1823
1824 /**
1825  * called by device driver when adding device instance
1826  * do generic HDLC initialization
1827  *
1828  * info  pointer to device instance information
1829  *
1830  * returns 0 if success, otherwise error code
1831  */
1832 static int hdlcdev_init(struct slgt_info *info)
1833 {
1834         int rc;
1835         struct net_device *dev;
1836         hdlc_device *hdlc;
1837
1838         /* allocate and initialize network and HDLC layer objects */
1839
1840         if (!(dev = alloc_hdlcdev(info))) {
1841                 printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
1842                 return -ENOMEM;
1843         }
1844
1845         /* for network layer reporting purposes only */
1846         dev->mem_start = info->phys_reg_addr;
1847         dev->mem_end   = info->phys_reg_addr + SLGT_REG_SIZE - 1;
1848         dev->irq       = info->irq_level;
1849
1850         /* network layer callbacks and settings */
1851         dev->do_ioctl       = hdlcdev_ioctl;
1852         dev->open           = hdlcdev_open;
1853         dev->stop           = hdlcdev_close;
1854         dev->tx_timeout     = hdlcdev_tx_timeout;
1855         dev->watchdog_timeo = 10*HZ;
1856         dev->tx_queue_len   = 50;
1857
1858         /* generic HDLC layer callbacks and settings */
1859         hdlc         = dev_to_hdlc(dev);
1860         hdlc->attach = hdlcdev_attach;
1861         hdlc->xmit   = hdlcdev_xmit;
1862
1863         /* register objects with HDLC layer */
1864         if ((rc = register_hdlc_device(dev))) {
1865                 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
1866                 free_netdev(dev);
1867                 return rc;
1868         }
1869
1870         info->netdev = dev;
1871         return 0;
1872 }
1873
1874 /**
1875  * called by device driver when removing device instance
1876  * do generic HDLC cleanup
1877  *
1878  * info  pointer to device instance information
1879  */
1880 static void hdlcdev_exit(struct slgt_info *info)
1881 {
1882         unregister_hdlc_device(info->netdev);
1883         free_netdev(info->netdev);
1884         info->netdev = NULL;
1885 }
1886
1887 #endif /* ifdef CONFIG_HDLC */
1888
1889 /*
1890  * get async data from rx DMA buffers
1891  */
1892 static void rx_async(struct slgt_info *info)
1893 {
1894         struct tty_struct *tty = info->port.tty;
1895         struct mgsl_icount *icount = &info->icount;
1896         unsigned int start, end;
1897         unsigned char *p;
1898         unsigned char status;
1899         struct slgt_desc *bufs = info->rbufs;
1900         int i, count;
1901         int chars = 0;
1902         int stat;
1903         unsigned char ch;
1904
1905         start = end = info->rbuf_current;
1906
1907         while(desc_complete(bufs[end])) {
1908                 count = desc_count(bufs[end]) - info->rbuf_index;
1909                 p     = bufs[end].buf + info->rbuf_index;
1910
1911                 DBGISR(("%s rx_async count=%d\n", info->device_name, count));
1912                 DBGDATA(info, p, count, "rx");
1913
1914                 for(i=0 ; i < count; i+=2, p+=2) {
1915                         ch = *p;
1916                         icount->rx++;
1917
1918                         stat = 0;
1919
1920                         if ((status = *(p+1) & (BIT1 + BIT0))) {
1921                                 if (status & BIT1)
1922                                         icount->parity++;
1923                                 else if (status & BIT0)
1924                                         icount->frame++;
1925                                 /* discard char if tty control flags say so */
1926                                 if (status & info->ignore_status_mask)
1927                                         continue;
1928                                 if (status & BIT1)
1929                                         stat = TTY_PARITY;
1930                                 else if (status & BIT0)
1931                                         stat = TTY_FRAME;
1932                         }
1933                         if (tty) {
1934                                 tty_insert_flip_char(tty, ch, stat);
1935                                 chars++;
1936                         }
1937                 }
1938
1939                 if (i < count) {
1940                         /* receive buffer not completed */
1941                         info->rbuf_index += i;
1942                         mod_timer(&info->rx_timer, jiffies + 1);
1943                         break;
1944                 }
1945
1946                 info->rbuf_index = 0;
1947                 free_rbufs(info, end, end);
1948
1949                 if (++end == info->rbuf_count)
1950                         end = 0;
1951
1952                 /* if entire list searched then no frame available */
1953                 if (end == start)
1954                         break;
1955         }
1956
1957         if (tty && chars)
1958                 tty_flip_buffer_push(tty);
1959 }
1960
1961 /*
1962  * return next bottom half action to perform
1963  */
1964 static int bh_action(struct slgt_info *info)
1965 {
1966         unsigned long flags;
1967         int rc;
1968
1969         spin_lock_irqsave(&info->lock,flags);
1970
1971         if (info->pending_bh & BH_RECEIVE) {
1972                 info->pending_bh &= ~BH_RECEIVE;
1973                 rc = BH_RECEIVE;
1974         } else if (info->pending_bh & BH_TRANSMIT) {
1975                 info->pending_bh &= ~BH_TRANSMIT;
1976                 rc = BH_TRANSMIT;
1977         } else if (info->pending_bh & BH_STATUS) {
1978                 info->pending_bh &= ~BH_STATUS;
1979                 rc = BH_STATUS;
1980         } else {
1981                 /* Mark BH routine as complete */
1982                 info->bh_running = false;
1983                 info->bh_requested = false;
1984                 rc = 0;
1985         }
1986
1987         spin_unlock_irqrestore(&info->lock,flags);
1988
1989         return rc;
1990 }
1991
1992 /*
1993  * perform bottom half processing
1994  */
1995 static void bh_handler(struct work_struct *work)
1996 {
1997         struct slgt_info *info = container_of(work, struct slgt_info, task);
1998         int action;
1999
2000         if (!info)
2001                 return;
2002         info->bh_running = true;
2003
2004         while((action = bh_action(info))) {
2005                 switch (action) {
2006                 case BH_RECEIVE:
2007                         DBGBH(("%s bh receive\n", info->device_name));
2008                         switch(info->params.mode) {
2009                         case MGSL_MODE_ASYNC:
2010                                 rx_async(info);
2011                                 break;
2012                         case MGSL_MODE_HDLC:
2013                                 while(rx_get_frame(info));
2014                                 break;
2015                         case MGSL_MODE_RAW:
2016                         case MGSL_MODE_MONOSYNC:
2017                         case MGSL_MODE_BISYNC:
2018                                 while(rx_get_buf(info));
2019                                 break;
2020                         }
2021                         /* restart receiver if rx DMA buffers exhausted */
2022                         if (info->rx_restart)
2023                                 rx_start(info);
2024                         break;
2025                 case BH_TRANSMIT:
2026                         bh_transmit(info);
2027                         break;
2028                 case BH_STATUS:
2029                         DBGBH(("%s bh status\n", info->device_name));
2030                         info->ri_chkcount = 0;
2031                         info->dsr_chkcount = 0;
2032                         info->dcd_chkcount = 0;
2033                         info->cts_chkcount = 0;
2034                         break;
2035                 default:
2036                         DBGBH(("%s unknown action\n", info->device_name));
2037                         break;
2038                 }
2039         }
2040         DBGBH(("%s bh_handler exit\n", info->device_name));
2041 }
2042
2043 static void bh_transmit(struct slgt_info *info)
2044 {
2045         struct tty_struct *tty = info->port.tty;
2046
2047         DBGBH(("%s bh_transmit\n", info->device_name));
2048         if (tty)
2049                 tty_wakeup(tty);
2050 }
2051
2052 static void dsr_change(struct slgt_info *info, unsigned short status)
2053 {
2054         if (status & BIT3) {
2055                 info->signals |= SerialSignal_DSR;
2056                 info->input_signal_events.dsr_up++;
2057         } else {
2058                 info->signals &= ~SerialSignal_DSR;
2059                 info->input_signal_events.dsr_down++;
2060         }
2061         DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
2062         if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2063                 slgt_irq_off(info, IRQ_DSR);
2064                 return;
2065         }
2066         info->icount.dsr++;
2067         wake_up_interruptible(&info->status_event_wait_q);
2068         wake_up_interruptible(&info->event_wait_q);
2069         info->pending_bh |= BH_STATUS;
2070 }
2071
2072 static void cts_change(struct slgt_info *info, unsigned short status)
2073 {
2074         if (status & BIT2) {
2075                 info->signals |= SerialSignal_CTS;
2076                 info->input_signal_events.cts_up++;
2077         } else {
2078                 info->signals &= ~SerialSignal_CTS;
2079                 info->input_signal_events.cts_down++;
2080         }
2081         DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
2082         if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2083                 slgt_irq_off(info, IRQ_CTS);
2084                 return;
2085         }
2086         info->icount.cts++;
2087         wake_up_interruptible(&info->status_event_wait_q);
2088         wake_up_interruptible(&info->event_wait_q);
2089         info->pending_bh |= BH_STATUS;
2090
2091         if (info->port.flags & ASYNC_CTS_FLOW) {
2092                 if (info->port.tty) {
2093                         if (info->port.tty->hw_stopped) {
2094                                 if (info->signals & SerialSignal_CTS) {
2095                                         info->port.tty->hw_stopped = 0;
2096                                         info->pending_bh |= BH_TRANSMIT;
2097                                         return;
2098                                 }
2099                         } else {
2100                                 if (!(info->signals & SerialSignal_CTS))
2101                                         info->port.tty->hw_stopped = 1;
2102                         }
2103                 }
2104         }
2105 }
2106
2107 static void dcd_change(struct slgt_info *info, unsigned short status)
2108 {
2109         if (status & BIT1) {
2110                 info->signals |= SerialSignal_DCD;
2111                 info->input_signal_events.dcd_up++;
2112         } else {
2113                 info->signals &= ~SerialSignal_DCD;
2114                 info->input_signal_events.dcd_down++;
2115         }
2116         DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
2117         if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2118                 slgt_irq_off(info, IRQ_DCD);
2119                 return;
2120         }
2121         info->icount.dcd++;
2122 #if SYNCLINK_GENERIC_HDLC
2123         if (info->netcount) {
2124                 if (info->signals & SerialSignal_DCD)
2125                         netif_carrier_on(info->netdev);
2126                 else
2127                         netif_carrier_off(info->netdev);
2128         }
2129 #endif
2130         wake_up_interruptible(&info->status_event_wait_q);
2131         wake_up_interruptible(&info->event_wait_q);
2132         info->pending_bh |= BH_STATUS;
2133
2134         if (info->port.flags & ASYNC_CHECK_CD) {
2135                 if (info->signals & SerialSignal_DCD)
2136                         wake_up_interruptible(&info->port.open_wait);
2137                 else {
2138                         if (info->port.tty)
2139                                 tty_hangup(info->port.tty);
2140                 }
2141         }
2142 }
2143
2144 static void ri_change(struct slgt_info *info, unsigned short status)
2145 {
2146         if (status & BIT0) {
2147                 info->signals |= SerialSignal_RI;
2148                 info->input_signal_events.ri_up++;
2149         } else {
2150                 info->signals &= ~SerialSignal_RI;
2151                 info->input_signal_events.ri_down++;
2152         }
2153         DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
2154         if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2155                 slgt_irq_off(info, IRQ_RI);
2156                 return;
2157         }
2158         info->icount.rng++;
2159         wake_up_interruptible(&info->status_event_wait_q);
2160         wake_up_interruptible(&info->event_wait_q);
2161         info->pending_bh |= BH_STATUS;
2162 }
2163
2164 static void isr_serial(struct slgt_info *info)
2165 {
2166         unsigned short status = rd_reg16(info, SSR);
2167
2168         DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
2169
2170         wr_reg16(info, SSR, status); /* clear pending */
2171
2172         info->irq_occurred = true;
2173
2174         if (info->params.mode == MGSL_MODE_ASYNC) {
2175                 if (status & IRQ_TXIDLE) {
2176                         if (info->tx_count)
2177                                 isr_txeom(info, status);
2178                 }
2179                 if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
2180                         info->icount.brk++;
2181                         /* process break detection if tty control allows */
2182                         if (info->port.tty) {
2183                                 if (!(status & info->ignore_status_mask)) {
2184                                         if (info->read_status_mask & MASK_BREAK) {
2185                                                 tty_insert_flip_char(info->port.tty, 0, TTY_BREAK);
2186                                                 if (info->port.flags & ASYNC_SAK)
2187                                                         do_SAK(info->port.tty);
2188                                         }
2189                                 }
2190                         }
2191                 }
2192         } else {
2193                 if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
2194                         isr_txeom(info, status);
2195
2196                 if (status & IRQ_RXIDLE) {
2197                         if (status & RXIDLE)
2198                                 info->icount.rxidle++;
2199                         else
2200                                 info->icount.exithunt++;
2201                         wake_up_interruptible(&info->event_wait_q);
2202                 }
2203
2204                 if (status & IRQ_RXOVER)
2205                         rx_start(info);
2206         }
2207
2208         if (status & IRQ_DSR)
2209                 dsr_change(info, status);
2210         if (status & IRQ_CTS)
2211                 cts_change(info, status);
2212         if (status & IRQ_DCD)
2213                 dcd_change(info, status);
2214         if (status & IRQ_RI)
2215                 ri_change(info, status);
2216 }
2217
2218 static void isr_rdma(struct slgt_info *info)
2219 {
2220         unsigned int status = rd_reg32(info, RDCSR);
2221
2222         DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
2223
2224         /* RDCSR (rx DMA control/status)
2225          *
2226          * 31..07  reserved
2227          * 06      save status byte to DMA buffer
2228          * 05      error
2229          * 04      eol (end of list)
2230          * 03      eob (end of buffer)
2231          * 02      IRQ enable
2232          * 01      reset
2233          * 00      enable
2234          */
2235         wr_reg32(info, RDCSR, status);  /* clear pending */
2236
2237         if (status & (BIT5 + BIT4)) {
2238                 DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
2239                 info->rx_restart = true;
2240         }
2241         info->pending_bh |= BH_RECEIVE;
2242 }
2243
2244 static void isr_tdma(struct slgt_info *info)
2245 {
2246         unsigned int status = rd_reg32(info, TDCSR);
2247
2248         DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
2249
2250         /* TDCSR (tx DMA control/status)
2251          *
2252          * 31..06  reserved
2253          * 05      error
2254          * 04      eol (end of list)
2255          * 03      eob (end of buffer)
2256          * 02      IRQ enable
2257          * 01      reset
2258          * 00      enable
2259          */
2260         wr_reg32(info, TDCSR, status);  /* clear pending */
2261
2262         if (status & (BIT5 + BIT4 + BIT3)) {
2263                 // another transmit buffer has completed
2264                 // run bottom half to get more send data from user
2265                 info->pending_bh |= BH_TRANSMIT;
2266         }
2267 }
2268
2269 static void isr_txeom(struct slgt_info *info, unsigned short status)
2270 {
2271         DBGISR(("%s txeom status=%04x\n", info->device_name, status));
2272
2273         slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
2274         tdma_reset(info);
2275         reset_tbufs(info);
2276         if (status & IRQ_TXUNDER) {
2277                 unsigned short val = rd_reg16(info, TCR);
2278                 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
2279                 wr_reg16(info, TCR, val); /* clear reset bit */
2280         }
2281
2282         if (info->tx_active) {
2283                 if (info->params.mode != MGSL_MODE_ASYNC) {
2284                         if (status & IRQ_TXUNDER)
2285                                 info->icount.txunder++;
2286                         else if (status & IRQ_TXIDLE)
2287                                 info->icount.txok++;
2288                 }
2289
2290                 info->tx_active = false;
2291                 info->tx_count = 0;
2292
2293                 del_timer(&info->tx_timer);
2294
2295                 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
2296                         info->signals &= ~SerialSignal_RTS;
2297                         info->drop_rts_on_tx_done = false;
2298                         set_signals(info);
2299                 }
2300
2301 #if SYNCLINK_GENERIC_HDLC
2302                 if (info->netcount)
2303                         hdlcdev_tx_done(info);
2304                 else
2305 #endif
2306                 {
2307                         if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
2308                                 tx_stop(info);
2309                                 return;
2310                         }
2311                         info->pending_bh |= BH_TRANSMIT;
2312                 }
2313         }
2314 }
2315
2316 static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
2317 {
2318         struct cond_wait *w, *prev;
2319
2320         /* wake processes waiting for specific transitions */
2321         for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
2322                 if (w->data & changed) {
2323                         w->data = state;
2324                         wake_up_interruptible(&w->q);
2325                         if (prev != NULL)
2326                                 prev->next = w->next;
2327                         else
2328                                 info->gpio_wait_q = w->next;
2329                 } else
2330                         prev = w;
2331         }
2332 }
2333
2334 /* interrupt service routine
2335  *
2336  *      irq     interrupt number
2337  *      dev_id  device ID supplied during interrupt registration
2338  */
2339 static irqreturn_t slgt_interrupt(int dummy, void *dev_id)
2340 {
2341         struct slgt_info *info = dev_id;
2342         unsigned int gsr;
2343         unsigned int i;
2344
2345         DBGISR(("slgt_interrupt irq=%d entry\n", info->irq_level));
2346
2347         spin_lock(&info->lock);
2348
2349         while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
2350                 DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
2351                 info->irq_occurred = true;
2352                 for(i=0; i < info->port_count ; i++) {
2353                         if (info->port_array[i] == NULL)
2354                                 continue;
2355                         if (gsr & (BIT8 << i))
2356                                 isr_serial(info->port_array[i]);
2357                         if (gsr & (BIT16 << (i*2)))
2358                                 isr_rdma(info->port_array[i]);
2359                         if (gsr & (BIT17 << (i*2)))
2360                                 isr_tdma(info->port_array[i]);
2361                 }
2362         }
2363
2364         if (info->gpio_present) {
2365                 unsigned int state;
2366                 unsigned int changed;
2367                 while ((changed = rd_reg32(info, IOSR)) != 0) {
2368                         DBGISR(("%s iosr=%08x\n", info->device_name, changed));
2369                         /* read latched state of GPIO signals */
2370                         state = rd_reg32(info, IOVR);
2371                         /* clear pending GPIO interrupt bits */
2372                         wr_reg32(info, IOSR, changed);
2373                         for (i=0 ; i < info->port_count ; i++) {
2374                                 if (info->port_array[i] != NULL)
2375                                         isr_gpio(info->port_array[i], changed, state);
2376                         }
2377                 }
2378         }
2379
2380         for(i=0; i < info->port_count ; i++) {
2381                 struct slgt_info *port = info->port_array[i];
2382
2383                 if (port && (port->port.count || port->netcount) &&
2384                     port->pending_bh && !port->bh_running &&
2385                     !port->bh_requested) {
2386                         DBGISR(("%s bh queued\n", port->device_name));
2387                         schedule_work(&port->task);
2388                         port->bh_requested = true;
2389                 }
2390         }
2391
2392         spin_unlock(&info->lock);
2393
2394         DBGISR(("slgt_interrupt irq=%d exit\n", info->irq_level));
2395         return IRQ_HANDLED;
2396 }
2397
2398 static int startup(struct slgt_info *info)
2399 {
2400         DBGINFO(("%s startup\n", info->device_name));
2401
2402         if (info->port.flags & ASYNC_INITIALIZED)
2403                 return 0;
2404
2405         if (!info->tx_buf) {
2406                 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
2407                 if (!info->tx_buf) {
2408                         DBGERR(("%s can't allocate tx buffer\n", info->device_name));
2409                         return -ENOMEM;
2410                 }
2411         }
2412
2413         info->pending_bh = 0;
2414
2415         memset(&info->icount, 0, sizeof(info->icount));
2416
2417         /* program hardware for current parameters */
2418         change_params(info);
2419
2420         if (info->port.tty)
2421                 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
2422
2423         info->port.flags |= ASYNC_INITIALIZED;
2424
2425         return 0;
2426 }
2427
2428 /*
2429  *  called by close() and hangup() to shutdown hardware
2430  */
2431 static void shutdown(struct slgt_info *info)
2432 {
2433         unsigned long flags;
2434
2435         if (!(info->port.flags & ASYNC_INITIALIZED))
2436                 return;
2437
2438         DBGINFO(("%s shutdown\n", info->device_name));
2439
2440         /* clear status wait queue because status changes */
2441         /* can't happen after shutting down the hardware */
2442         wake_up_interruptible(&info->status_event_wait_q);
2443         wake_up_interruptible(&info->event_wait_q);
2444
2445         del_timer_sync(&info->tx_timer);
2446         del_timer_sync(&info->rx_timer);
2447
2448         kfree(info->tx_buf);
2449         info->tx_buf = NULL;
2450
2451         spin_lock_irqsave(&info->lock,flags);
2452
2453         tx_stop(info);
2454         rx_stop(info);
2455
2456         slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
2457
2458         if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
2459                 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2460                 set_signals(info);
2461         }
2462
2463         flush_cond_wait(&info->gpio_wait_q);
2464
2465         spin_unlock_irqrestore(&info->lock,flags);
2466
2467         if (info->port.tty)
2468                 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
2469
2470         info->port.flags &= ~ASYNC_INITIALIZED;
2471 }
2472
2473 static void program_hw(struct slgt_info *info)
2474 {
2475         unsigned long flags;
2476
2477         spin_lock_irqsave(&info->lock,flags);
2478
2479         rx_stop(info);
2480         tx_stop(info);
2481
2482         if (info->params.mode != MGSL_MODE_ASYNC ||
2483             info->netcount)
2484                 sync_mode(info);
2485         else
2486                 async_mode(info);
2487
2488         set_signals(info);
2489
2490         info->dcd_chkcount = 0;
2491         info->cts_chkcount = 0;
2492         info->ri_chkcount = 0;
2493         info->dsr_chkcount = 0;
2494
2495         slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR);
2496         get_signals(info);
2497
2498         if (info->netcount ||
2499             (info->port.tty && info->port.tty->termios->c_cflag & CREAD))
2500                 rx_start(info);
2501
2502         spin_unlock_irqrestore(&info->lock,flags);
2503 }
2504
2505 /*
2506  * reconfigure adapter based on new parameters
2507  */
2508 static void change_params(struct slgt_info *info)
2509 {
2510         unsigned cflag;
2511         int bits_per_char;
2512
2513         if (!info->port.tty || !info->port.tty->termios)
2514                 return;
2515         DBGINFO(("%s change_params\n", info->device_name));
2516
2517         cflag = info->port.tty->termios->c_cflag;
2518
2519         /* if B0 rate (hangup) specified then negate DTR and RTS */
2520         /* otherwise assert DTR and RTS */
2521         if (cflag & CBAUD)
2522                 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
2523         else
2524                 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2525
2526         /* byte size and parity */
2527
2528         switch (cflag & CSIZE) {
2529         case CS5: info->params.data_bits = 5; break;
2530         case CS6: info->params.data_bits = 6; break;
2531         case CS7: info->params.data_bits = 7; break;
2532         case CS8: info->params.data_bits = 8; break;
2533         default:  info->params.data_bits = 7; break;
2534         }
2535
2536         info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
2537
2538         if (cflag & PARENB)
2539                 info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
2540         else
2541                 info->params.parity = ASYNC_PARITY_NONE;
2542
2543         /* calculate number of jiffies to transmit a full
2544          * FIFO (32 bytes) at specified data rate
2545          */
2546         bits_per_char = info->params.data_bits +
2547                         info->params.stop_bits + 1;
2548
2549         info->params.data_rate = tty_get_baud_rate(info->port.tty);
2550
2551         if (info->params.data_rate) {
2552                 info->timeout = (32*HZ*bits_per_char) /
2553                                 info->params.data_rate;
2554         }
2555         info->timeout += HZ/50;         /* Add .02 seconds of slop */
2556
2557         if (cflag & CRTSCTS)
2558                 info->port.flags |= ASYNC_CTS_FLOW;
2559         else
2560                 info->port.flags &= ~ASYNC_CTS_FLOW;
2561
2562         if (cflag & CLOCAL)
2563                 info->port.flags &= ~ASYNC_CHECK_CD;
2564         else
2565                 info->port.flags |= ASYNC_CHECK_CD;
2566
2567         /* process tty input control flags */
2568
2569         info->read_status_mask = IRQ_RXOVER;
2570         if (I_INPCK(info->port.tty))
2571                 info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
2572         if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
2573                 info->read_status_mask |= MASK_BREAK;
2574         if (I_IGNPAR(info->port.tty))
2575                 info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
2576         if (I_IGNBRK(info->port.tty)) {
2577                 info->ignore_status_mask |= MASK_BREAK;
2578                 /* If ignoring parity and break indicators, ignore
2579                  * overruns too.  (For real raw support).
2580                  */
2581                 if (I_IGNPAR(info->port.tty))
2582                         info->ignore_status_mask |= MASK_OVERRUN;
2583         }
2584
2585         program_hw(info);
2586 }
2587
2588 static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
2589 {
2590         DBGINFO(("%s get_stats\n",  info->device_name));
2591         if (!user_icount) {
2592                 memset(&info->icount, 0, sizeof(info->icount));
2593         } else {
2594                 if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
2595                         return -EFAULT;
2596         }
2597         return 0;
2598 }
2599
2600 static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
2601 {
2602         DBGINFO(("%s get_params\n", info->device_name));
2603         if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
2604                 return -EFAULT;
2605         return 0;
2606 }
2607
2608 static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
2609 {
2610         unsigned long flags;
2611         MGSL_PARAMS tmp_params;
2612
2613         DBGINFO(("%s set_params\n", info->device_name));
2614         if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
2615                 return -EFAULT;
2616
2617         spin_lock_irqsave(&info->lock, flags);
2618         memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
2619         spin_unlock_irqrestore(&info->lock, flags);
2620
2621         change_params(info);
2622
2623         return 0;
2624 }
2625
2626 static int get_txidle(struct slgt_info *info, int __user *idle_mode)
2627 {
2628         DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
2629         if (put_user(info->idle_mode, idle_mode))
2630                 return -EFAULT;
2631         return 0;
2632 }
2633
2634 static int set_txidle(struct slgt_info *info, int idle_mode)
2635 {
2636         unsigned long flags;
2637         DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
2638         spin_lock_irqsave(&info->lock,flags);
2639         info->idle_mode = idle_mode;
2640         if (info->params.mode != MGSL_MODE_ASYNC)
2641                 tx_set_idle(info);
2642         spin_unlock_irqrestore(&info->lock,flags);
2643         return 0;
2644 }
2645
2646 static int tx_enable(struct slgt_info *info, int enable)
2647 {
2648         unsigned long flags;
2649         DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
2650         spin_lock_irqsave(&info->lock,flags);
2651         if (enable) {
2652                 if (!info->tx_enabled)
2653                         tx_start(info);
2654         } else {
2655                 if (info->tx_enabled)
2656                         tx_stop(info);
2657         }
2658         spin_unlock_irqrestore(&info->lock,flags);
2659         return 0;
2660 }
2661
2662 /*
2663  * abort transmit HDLC frame
2664  */
2665 static int tx_abort(struct slgt_info *info)
2666 {
2667         unsigned long flags;
2668         DBGINFO(("%s tx_abort\n", info->device_name));
2669         spin_lock_irqsave(&info->lock,flags);
2670         tdma_reset(info);
2671         spin_unlock_irqrestore(&info->lock,flags);
2672         return 0;
2673 }
2674
2675 static int rx_enable(struct slgt_info *info, int enable)
2676 {
2677         unsigned long flags;
2678         DBGINFO(("%s rx_enable(%d)\n", info->device_name, enable));
2679         spin_lock_irqsave(&info->lock,flags);
2680         if (enable) {
2681                 if (!info->rx_enabled)
2682                         rx_start(info);
2683                 else if (enable == 2) {
2684                         /* force hunt mode (write 1 to RCR[3]) */
2685                         wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
2686                 }
2687         } else {
2688                 if (info->rx_enabled)
2689                         rx_stop(info);
2690         }
2691         spin_unlock_irqrestore(&info->lock,flags);
2692         return 0;
2693 }
2694
2695 /*
2696  *  wait for specified event to occur
2697  */
2698 static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
2699 {
2700         unsigned long flags;
2701         int s;
2702         int rc=0;
2703         struct mgsl_icount cprev, cnow;
2704         int events;
2705         int mask;
2706         struct  _input_signal_events oldsigs, newsigs;
2707         DECLARE_WAITQUEUE(wait, current);
2708
2709         if (get_user(mask, mask_ptr))
2710                 return -EFAULT;
2711
2712         DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
2713
2714         spin_lock_irqsave(&info->lock,flags);
2715
2716         /* return immediately if state matches requested events */
2717         get_signals(info);
2718         s = info->signals;
2719
2720         events = mask &
2721                 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2722                   ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2723                   ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2724                   ((s & SerialSignal_RI)  ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2725         if (events) {
2726                 spin_unlock_irqrestore(&info->lock,flags);
2727                 goto exit;
2728         }
2729
2730         /* save current irq counts */
2731         cprev = info->icount;
2732         oldsigs = info->input_signal_events;
2733
2734         /* enable hunt and idle irqs if needed */
2735         if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
2736                 unsigned short val = rd_reg16(info, SCR);
2737                 if (!(val & IRQ_RXIDLE))
2738                         wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
2739         }
2740
2741         set_current_state(TASK_INTERRUPTIBLE);
2742         add_wait_queue(&info->event_wait_q, &wait);
2743
2744         spin_unlock_irqrestore(&info->lock,flags);
2745
2746         for(;;) {
2747                 schedule();
2748                 if (signal_pending(current)) {
2749                         rc = -ERESTARTSYS;
2750                         break;
2751                 }
2752
2753                 /* get current irq counts */
2754                 spin_lock_irqsave(&info->lock,flags);
2755                 cnow = info->icount;
2756                 newsigs = info->input_signal_events;
2757                 set_current_state(TASK_INTERRUPTIBLE);
2758                 spin_unlock_irqrestore(&info->lock,flags);
2759
2760                 /* if no change, wait aborted for some reason */
2761                 if (newsigs.dsr_up   == oldsigs.dsr_up   &&
2762                     newsigs.dsr_down == oldsigs.dsr_down &&
2763                     newsigs.dcd_up   == oldsigs.dcd_up   &&
2764                     newsigs.dcd_down == oldsigs.dcd_down &&
2765                     newsigs.cts_up   == oldsigs.cts_up   &&
2766                     newsigs.cts_down == oldsigs.cts_down &&
2767                     newsigs.ri_up    == oldsigs.ri_up    &&
2768                     newsigs.ri_down  == oldsigs.ri_down  &&
2769                     cnow.exithunt    == cprev.exithunt   &&
2770                     cnow.rxidle      == cprev.rxidle) {
2771                         rc = -EIO;
2772                         break;
2773                 }
2774
2775                 events = mask &
2776                         ( (newsigs.dsr_up   != oldsigs.dsr_up   ? MgslEvent_DsrActive:0)   +
2777                           (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2778                           (newsigs.dcd_up   != oldsigs.dcd_up   ? MgslEvent_DcdActive:0)   +
2779                           (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2780                           (newsigs.cts_up   != oldsigs.cts_up   ? MgslEvent_CtsActive:0)   +
2781                           (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2782                           (newsigs.ri_up    != oldsigs.ri_up    ? MgslEvent_RiActive:0)    +
2783                           (newsigs.ri_down  != oldsigs.ri_down  ? MgslEvent_RiInactive:0)  +
2784                           (cnow.exithunt    != cprev.exithunt   ? MgslEvent_ExitHuntMode:0) +
2785                           (cnow.rxidle      != cprev.rxidle     ? MgslEvent_IdleReceived:0) );
2786                 if (events)
2787                         break;
2788
2789                 cprev = cnow;
2790                 oldsigs = newsigs;
2791         }
2792
2793         remove_wait_queue(&info->event_wait_q, &wait);
2794         set_current_state(TASK_RUNNING);
2795
2796
2797         if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2798                 spin_lock_irqsave(&info->lock,flags);
2799                 if (!waitqueue_active(&info->event_wait_q)) {
2800                         /* disable enable exit hunt mode/idle rcvd IRQs */
2801                         wr_reg16(info, SCR,
2802                                 (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
2803                 }
2804                 spin_unlock_irqrestore(&info->lock,flags);
2805         }
2806 exit:
2807         if (rc == 0)
2808                 rc = put_user(events, mask_ptr);
2809         return rc;
2810 }
2811
2812 static int get_interface(struct slgt_info *info, int __user *if_mode)
2813 {
2814         DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
2815         if (put_user(info->if_mode, if_mode))
2816                 return -EFAULT;
2817         return 0;
2818 }
2819
2820 static int set_interface(struct slgt_info *info, int if_mode)
2821 {
2822         unsigned long flags;
2823         unsigned short val;
2824
2825         DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
2826         spin_lock_irqsave(&info->lock,flags);
2827         info->if_mode = if_mode;
2828
2829         msc_set_vcr(info);
2830
2831         /* TCR (tx control) 07  1=RTS driver control */
2832         val = rd_reg16(info, TCR);
2833         if (info->if_mode & MGSL_INTERFACE_RTS_EN)
2834                 val |= BIT7;
2835         else
2836                 val &= ~BIT7;
2837         wr_reg16(info, TCR, val);
2838
2839         spin_unlock_irqrestore(&info->lock,flags);
2840         return 0;
2841 }
2842
2843 /*
2844  * set general purpose IO pin state and direction
2845  *
2846  * user_gpio fields:
2847  * state   each bit indicates a pin state
2848  * smask   set bit indicates pin state to set
2849  * dir     each bit indicates a pin direction (0=input, 1=output)
2850  * dmask   set bit indicates pin direction to set
2851  */
2852 static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2853 {
2854         unsigned long flags;
2855         struct gpio_desc gpio;
2856         __u32 data;
2857
2858         if (!info->gpio_present)
2859                 return -EINVAL;
2860         if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2861                 return -EFAULT;
2862         DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
2863                  info->device_name, gpio.state, gpio.smask,
2864                  gpio.dir, gpio.dmask));
2865
2866         spin_lock_irqsave(&info->lock,flags);
2867         if (gpio.dmask) {
2868                 data = rd_reg32(info, IODR);
2869                 data |= gpio.dmask & gpio.dir;
2870                 data &= ~(gpio.dmask & ~gpio.dir);
2871                 wr_reg32(info, IODR, data);
2872         }
2873         if (gpio.smask) {
2874                 data = rd_reg32(info, IOVR);
2875                 data |= gpio.smask & gpio.state;
2876                 data &= ~(gpio.smask & ~gpio.state);
2877                 wr_reg32(info, IOVR, data);
2878         }
2879         spin_unlock_irqrestore(&info->lock,flags);
2880
2881         return 0;
2882 }
2883
2884 /*
2885  * get general purpose IO pin state and direction
2886  */
2887 static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2888 {
2889         struct gpio_desc gpio;
2890         if (!info->gpio_present)
2891                 return -EINVAL;
2892         gpio.state = rd_reg32(info, IOVR);
2893         gpio.smask = 0xffffffff;
2894         gpio.dir   = rd_reg32(info, IODR);
2895         gpio.dmask = 0xffffffff;
2896         if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
2897                 return -EFAULT;
2898         DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
2899                  info->device_name, gpio.state, gpio.dir));
2900         return 0;
2901 }
2902
2903 /*
2904  * conditional wait facility
2905  */
2906 static void init_cond_wait(struct cond_wait *w, unsigned int data)
2907 {
2908         init_waitqueue_head(&w->q);
2909         init_waitqueue_entry(&w->wait, current);
2910         w->data = data;
2911 }
2912
2913 static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
2914 {
2915         set_current_state(TASK_INTERRUPTIBLE);
2916         add_wait_queue(&w->q, &w->wait);
2917         w->next = *head;
2918         *head = w;
2919 }
2920
2921 static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
2922 {
2923         struct cond_wait *w, *prev;
2924         remove_wait_queue(&cw->q, &cw->wait);
2925         set_current_state(TASK_RUNNING);
2926         for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
2927                 if (w == cw) {
2928                         if (prev != NULL)
2929                                 prev->next = w->next;
2930                         else
2931                                 *head = w->next;
2932                         break;
2933                 }
2934         }
2935 }
2936
2937 static void flush_cond_wait(struct cond_wait **head)
2938 {
2939         while (*head != NULL) {
2940                 wake_up_interruptible(&(*head)->q);
2941                 *head = (*head)->next;
2942         }
2943 }
2944
2945 /*
2946  * wait for general purpose I/O pin(s) to enter specified state
2947  *
2948  * user_gpio fields:
2949  * state - bit indicates target pin state
2950  * smask - set bit indicates watched pin
2951  *
2952  * The wait ends when at least one watched pin enters the specified
2953  * state. When 0 (no error) is returned, user_gpio->state is set to the
2954  * state of all GPIO pins when the wait ends.
2955  *
2956  * Note: Each pin may be a dedicated input, dedicated output, or
2957  * configurable input/output. The number and configuration of pins
2958  * varies with the specific adapter model. Only input pins (dedicated
2959  * or configured) can be monitored with this function.
2960  */
2961 static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2962 {
2963         unsigned long flags;
2964         int rc = 0;
2965         struct gpio_desc gpio;
2966         struct cond_wait wait;
2967         u32 state;
2968
2969         if (!info->gpio_present)
2970                 return -EINVAL;
2971         if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2972                 return -EFAULT;
2973         DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
2974                  info->device_name, gpio.state, gpio.smask));
2975         /* ignore output pins identified by set IODR bit */
2976         if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
2977                 return -EINVAL;
2978         init_cond_wait(&wait, gpio.smask);
2979
2980         spin_lock_irqsave(&info->lock, flags);
2981         /* enable interrupts for watched pins */
2982         wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
2983         /* get current pin states */
2984         state = rd_reg32(info, IOVR);
2985
2986         if (gpio.smask & ~(state ^ gpio.state)) {
2987                 /* already in target state */
2988                 gpio.state = state;
2989         } else {
2990                 /* wait for target state */
2991                 add_cond_wait(&info->gpio_wait_q, &wait);
2992                 spin_unlock_irqrestore(&info->lock, flags);
2993                 schedule();
2994                 if (signal_pending(current))
2995                         rc = -ERESTARTSYS;
2996                 else
2997                         gpio.state = wait.data;
2998                 spin_lock_irqsave(&info->lock, flags);
2999                 remove_cond_wait(&info->gpio_wait_q, &wait);
3000         }
3001
3002         /* disable all GPIO interrupts if no waiting processes */
3003         if (info->gpio_wait_q == NULL)
3004                 wr_reg32(info, IOER, 0);
3005         spin_unlock_irqrestore(&info->lock,flags);
3006
3007         if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
3008                 rc = -EFAULT;
3009         return rc;
3010 }
3011
3012 static int modem_input_wait(struct slgt_info *info,int arg)
3013 {
3014         unsigned long flags;
3015         int rc;
3016         struct mgsl_icount cprev, cnow;
3017         DECLARE_WAITQUEUE(wait, current);
3018
3019         /* save current irq counts */
3020         spin_lock_irqsave(&info->lock,flags);
3021         cprev = info->icount;
3022         add_wait_queue(&info->status_event_wait_q, &wait);
3023         set_current_state(TASK_INTERRUPTIBLE);
3024         spin_unlock_irqrestore(&info->lock,flags);
3025
3026         for(;;) {
3027                 schedule();
3028                 if (signal_pending(current)) {
3029                         rc = -ERESTARTSYS;
3030                         break;
3031                 }
3032
3033                 /* get new irq counts */
3034                 spin_lock_irqsave(&info->lock,flags);
3035                 cnow = info->icount;
3036                 set_current_state(TASK_INTERRUPTIBLE);
3037                 spin_unlock_irqrestore(&info->lock,flags);
3038
3039                 /* if no change, wait aborted for some reason */
3040                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
3041                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
3042                         rc = -EIO;
3043                         break;
3044                 }
3045
3046                 /* check for change in caller specified modem input */
3047                 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
3048                     (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
3049                     (arg & TIOCM_CD  && cnow.dcd != cprev.dcd) ||
3050                     (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
3051                         rc = 0;
3052                         break;
3053                 }
3054
3055                 cprev = cnow;
3056         }
3057         remove_wait_queue(&info->status_event_wait_q, &wait);
3058         set_current_state(TASK_RUNNING);
3059         return rc;
3060 }
3061
3062 /*
3063  *  return state of serial control and status signals
3064  */
3065 static int tiocmget(struct tty_struct *tty, struct file *file)
3066 {
3067         struct slgt_info *info = tty->driver_data;
3068         unsigned int result;
3069         unsigned long flags;
3070
3071         spin_lock_irqsave(&info->lock,flags);
3072         get_signals(info);
3073         spin_unlock_irqrestore(&info->lock,flags);
3074
3075         result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
3076                 ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
3077                 ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
3078                 ((info->signals & SerialSignal_RI)  ? TIOCM_RNG:0) +
3079                 ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
3080                 ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
3081
3082         DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
3083         return result;
3084 }
3085
3086 /*
3087  * set modem control signals (DTR/RTS)
3088  *
3089  *      cmd     signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
3090  *              TIOCMSET = set/clear signal values
3091  *      value   bit mask for command
3092  */
3093 static int tiocmset(struct tty_struct *tty, struct file *file,
3094                     unsigned int set, unsigned int clear)
3095 {
3096         struct slgt_info *info = tty->driver_data;
3097         unsigned long flags;
3098
3099         DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
3100
3101         if (set & TIOCM_RTS)
3102                 info->signals |= SerialSignal_RTS;
3103         if (set & TIOCM_DTR)
3104                 info->signals |= SerialSignal_DTR;
3105         if (clear & TIOCM_RTS)
3106                 info->signals &= ~SerialSignal_RTS;
3107         if (clear & TIOCM_DTR)
3108                 info->signals &= ~SerialSignal_DTR;
3109
3110         spin_lock_irqsave(&info->lock,flags);
3111         set_signals(info);
3112         spin_unlock_irqrestore(&info->lock,flags);
3113         return 0;
3114 }
3115
3116 /*
3117  *  block current process until the device is ready to open
3118  */
3119 static int block_til_ready(struct tty_struct *tty, struct file *filp,
3120                            struct slgt_info *info)
3121 {
3122         DECLARE_WAITQUEUE(wait, current);
3123         int             retval;
3124         bool            do_clocal = false;
3125         bool            extra_count = false;
3126         unsigned long   flags;
3127
3128         DBGINFO(("%s block_til_ready\n", tty->driver->name));
3129
3130         if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3131                 /* nonblock mode is set or port is not enabled */
3132                 info->port.flags |= ASYNC_NORMAL_ACTIVE;
3133                 return 0;
3134         }
3135
3136         if (tty->termios->c_cflag & CLOCAL)
3137                 do_clocal = true;
3138
3139         /* Wait for carrier detect and the line to become
3140          * free (i.e., not in use by the callout).  While we are in
3141          * this loop, info->port.count is dropped by one, so that
3142          * close() knows when to free things.  We restore it upon
3143          * exit, either normal or abnormal.
3144          */
3145
3146         retval = 0;
3147         add_wait_queue(&info->port.open_wait, &wait);
3148
3149         spin_lock_irqsave(&info->lock, flags);
3150         if (!tty_hung_up_p(filp)) {
3151                 extra_count = true;
3152                 info->port.count--;
3153         }
3154         spin_unlock_irqrestore(&info->lock, flags);
3155         info->port.blocked_open++;
3156
3157         while (1) {
3158                 if ((tty->termios->c_cflag & CBAUD)) {
3159                         spin_lock_irqsave(&info->lock,flags);
3160                         info->signals |= SerialSignal_RTS + SerialSignal_DTR;
3161                         set_signals(info);
3162                         spin_unlock_irqrestore(&info->lock,flags);
3163                 }
3164
3165                 set_current_state(TASK_INTERRUPTIBLE);
3166
3167                 if (tty_hung_up_p(filp) || !(info->port.flags & ASYNC_INITIALIZED)){
3168                         retval = (info->port.flags & ASYNC_HUP_NOTIFY) ?
3169                                         -EAGAIN : -ERESTARTSYS;
3170                         break;
3171                 }
3172
3173                 spin_lock_irqsave(&info->lock,flags);
3174                 get_signals(info);
3175                 spin_unlock_irqrestore(&info->lock,flags);
3176
3177                 if (!(info->port.flags & ASYNC_CLOSING) &&
3178                     (do_clocal || (info->signals & SerialSignal_DCD)) ) {
3179                         break;
3180                 }
3181
3182                 if (signal_pending(current)) {
3183                         retval = -ERESTARTSYS;
3184                         break;
3185                 }
3186
3187                 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
3188                 schedule();
3189         }
3190
3191         set_current_state(TASK_RUNNING);
3192         remove_wait_queue(&info->port.open_wait, &wait);
3193
3194         if (extra_count)
3195                 info->port.count++;
3196         info->port.blocked_open--;
3197
3198         if (!retval)
3199                 info->port.flags |= ASYNC_NORMAL_ACTIVE;
3200
3201         DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
3202         return retval;
3203 }
3204
3205 static int alloc_tmp_rbuf(struct slgt_info *info)
3206 {
3207         info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
3208         if (info->tmp_rbuf == NULL)
3209                 return -ENOMEM;
3210         return 0;
3211 }
3212
3213 static void free_tmp_rbuf(struct slgt_info *info)
3214 {
3215         kfree(info->tmp_rbuf);
3216         info->tmp_rbuf = NULL;
3217 }
3218
3219 /*
3220  * allocate DMA descriptor lists.
3221  */
3222 static int alloc_desc(struct slgt_info *info)
3223 {
3224         unsigned int i;
3225         unsigned int pbufs;
3226
3227         /* allocate memory to hold descriptor lists */
3228         info->bufs = pci_alloc_consistent(info->pdev, DESC_LIST_SIZE, &info->bufs_dma_addr);
3229         if (info->bufs == NULL)
3230                 return -ENOMEM;
3231
3232         memset(info->bufs, 0, DESC_LIST_SIZE);
3233
3234         info->rbufs = (struct slgt_desc*)info->bufs;
3235         info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
3236
3237         pbufs = (unsigned int)info->bufs_dma_addr;
3238
3239         /*
3240          * Build circular lists of descriptors
3241          */
3242
3243         for (i=0; i < info->rbuf_count; i++) {
3244                 /* physical address of this descriptor */
3245                 info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
3246
3247                 /* physical address of next descriptor */
3248                 if (i == info->rbuf_count - 1)
3249                         info->rbufs[i].next = cpu_to_le32(pbufs);
3250                 else
3251                         info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
3252                 set_desc_count(info->rbufs[i], DMABUFSIZE);
3253         }
3254
3255         for (i=0; i < info->tbuf_count; i++) {
3256                 /* physical address of this descriptor */
3257                 info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
3258
3259                 /* physical address of next descriptor */
3260                 if (i == info->tbuf_count - 1)
3261                         info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
3262                 else
3263                         info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
3264         }
3265
3266         return 0;
3267 }
3268
3269 static void free_desc(struct slgt_info *info)
3270 {
3271         if (info->bufs != NULL) {
3272                 pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
3273                 info->bufs  = NULL;
3274                 info->rbufs = NULL;
3275                 info->tbufs = NULL;
3276         }
3277 }
3278
3279 static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3280 {
3281         int i;
3282         for (i=0; i < count; i++) {
3283                 if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
3284                         return -ENOMEM;
3285                 bufs[i].pbuf  = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
3286         }
3287         return 0;
3288 }
3289
3290 static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3291 {
3292         int i;
3293         for (i=0; i < count; i++) {
3294                 if (bufs[i].buf == NULL)
3295                         continue;
3296                 pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
3297                 bufs[i].buf = NULL;
3298         }
3299 }
3300
3301 static int alloc_dma_bufs(struct slgt_info *info)
3302 {
3303         info->rbuf_count = 32;
3304         info->tbuf_count = 32;
3305
3306         if (alloc_desc(info) < 0 ||
3307             alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
3308             alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
3309             alloc_tmp_rbuf(info) < 0) {
3310                 DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
3311                 return -ENOMEM;
3312         }
3313         reset_rbufs(info);
3314         return 0;
3315 }
3316
3317 static void free_dma_bufs(struct slgt_info *info)
3318 {
3319         if (info->bufs) {
3320                 free_bufs(info, info->rbufs, info->rbuf_count);
3321                 free_bufs(info, info->tbufs, info->tbuf_count);
3322                 free_desc(info);
3323         }
3324         free_tmp_rbuf(info);
3325 }
3326
3327 static int claim_resources(struct slgt_info *info)
3328 {
3329         if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
3330                 DBGERR(("%s reg addr conflict, addr=%08X\n",
3331                         info->device_name, info->phys_reg_addr));
3332                 info->init_error = DiagStatus_AddressConflict;
3333                 goto errout;
3334         }
3335         else
3336                 info->reg_addr_requested = true;
3337
3338         info->reg_addr = ioremap_nocache(info->phys_reg_addr, SLGT_REG_SIZE);
3339         if (!info->reg_addr) {
3340                 DBGERR(("%s cant map device registers, addr=%08X\n",
3341                         info->device_name, info->phys_reg_addr));
3342                 info->init_error = DiagStatus_CantAssignPciResources;
3343                 goto errout;
3344         }
3345         return 0;
3346
3347 errout:
3348         release_resources(info);
3349         return -ENODEV;
3350 }
3351
3352 static void release_resources(struct slgt_info *info)
3353 {
3354         if (info->irq_requested) {
3355                 free_irq(info->irq_level, info);
3356                 info->irq_requested = false;
3357         }
3358
3359         if (info->reg_addr_requested) {
3360                 release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
3361                 info->reg_addr_requested = false;
3362         }
3363
3364         if (info->reg_addr) {
3365                 iounmap(info->reg_addr);
3366                 info->reg_addr = NULL;
3367         }
3368 }
3369
3370 /* Add the specified device instance data structure to the
3371  * global linked list of devices and increment the device count.