blob: 99feaedc53a1409bf4d4bc81c897f697fdf74f3e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/pcmcia/synclink_cs.c
3 *
Paul Fulghuma7482a22005-09-10 00:26:07 -07004 * $Id: synclink_cs.c,v 4.34 2005/09/08 13:20:54 paulkf Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Device driver for Microgate SyncLink PC Card
7 * multiprotocol serial adapter.
8 *
9 * written by Paul Fulghum for Microgate Corporation
10 * paulkf@microgate.com
11 *
12 * Microgate and SyncLink are trademarks of Microgate Corporation
13 *
14 * This code is released under the GNU General Public License (GPL)
15 *
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
30#if defined(__i386__)
31# define BREAKPOINT() asm(" int $3");
32#else
33# define BREAKPOINT() { }
34#endif
35
36#define MAX_DEVICE_COUNT 4
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
39#include <linux/errno.h>
40#include <linux/signal.h>
41#include <linux/sched.h>
42#include <linux/timer.h>
43#include <linux/time.h>
44#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/tty.h>
46#include <linux/tty_flip.h>
47#include <linux/serial.h>
48#include <linux/major.h>
49#include <linux/string.h>
50#include <linux/fcntl.h>
51#include <linux/ptrace.h>
52#include <linux/ioport.h>
53#include <linux/mm.h>
Alexey Dobriyan87687142009-03-31 15:19:17 -070054#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/slab.h>
56#include <linux/netdevice.h>
57#include <linux/vmalloc.h>
58#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/delay.h>
60#include <linux/ioctl.h>
Robert P. J. Day3dd12472008-02-06 01:37:17 -080061#include <linux/synclink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#include <asm/system.h>
64#include <asm/io.h>
65#include <asm/irq.h>
66#include <asm/dma.h>
67#include <linux/bitops.h>
68#include <asm/types.h>
69#include <linux/termios.h>
70#include <linux/workqueue.h>
71#include <linux/hdlc.h>
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <pcmcia/cistpl.h>
74#include <pcmcia/cisreg.h>
75#include <pcmcia/ds.h>
76
Paul Fulghumaf69c7f2006-12-06 20:40:24 -080077#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_CS_MODULE))
78#define SYNCLINK_GENERIC_HDLC 1
79#else
80#define SYNCLINK_GENERIC_HDLC 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
82
83#define GET_USER(error,value,addr) error = get_user(value,addr)
84#define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
85#define PUT_USER(error,value,addr) error = put_user(value,addr)
86#define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
87
88#include <asm/uaccess.h>
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static MGSL_PARAMS default_params = {
91 MGSL_MODE_HDLC, /* unsigned long mode */
92 0, /* unsigned char loopback; */
93 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
94 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
95 0, /* unsigned long clock_speed; */
96 0xff, /* unsigned char addr_filter; */
97 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
98 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
99 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
100 9600, /* unsigned long data_rate; */
101 8, /* unsigned char data_bits; */
102 1, /* unsigned char stop_bits; */
103 ASYNC_PARITY_NONE /* unsigned char parity; */
104};
105
106typedef struct
107{
108 int count;
109 unsigned char status;
110 char data[1];
111} RXBUF;
112
113/* The queue of BH actions to be performed */
114
115#define BH_RECEIVE 1
116#define BH_TRANSMIT 2
117#define BH_STATUS 4
118
119#define IO_PIN_SHUTDOWN_LIMIT 100
120
121#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
122
123struct _input_signal_events {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400124 int ri_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int ri_down;
126 int dsr_up;
127 int dsr_down;
128 int dcd_up;
129 int dcd_down;
130 int cts_up;
131 int cts_down;
132};
133
134
135/*
136 * Device instance data structure
137 */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139typedef struct _mgslpc_info {
Alan Coxeeb46132009-01-02 13:48:47 +0000140 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 void *if_ptr; /* General purpose pointer (used by SPPP) */
142 int magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int line;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct mgsl_icount icount;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int timeout;
148 int x_char; /* xon/xoff character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 unsigned char read_status_mask;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400150 unsigned char ignore_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 unsigned char *tx_buf;
153 int tx_put;
154 int tx_get;
155 int tx_count;
156
157 /* circular list of fixed length rx buffers */
158
159 unsigned char *rx_buf; /* memory allocated for all rx buffers */
160 int rx_buf_total_size; /* size of memory allocated for rx buffers */
161 int rx_put; /* index of next empty rx buffer */
162 int rx_get; /* index of next full rx buffer */
163 int rx_buf_size; /* size in bytes of single rx buffer */
164 int rx_buf_count; /* total number of rx buffers */
165 int rx_frame_count; /* number of full rx buffers */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 wait_queue_head_t status_event_wait_q;
168 wait_queue_head_t event_wait_q;
169 struct timer_list tx_timer; /* HDLC transmit timeout timer */
170 struct _mgslpc_info *next_device; /* device list link */
171
172 unsigned short imra_value;
173 unsigned short imrb_value;
174 unsigned char pim_value;
175
176 spinlock_t lock;
177 struct work_struct task; /* task structure for scheduling bh */
178
179 u32 max_frame_size;
180
181 u32 pending_bh;
182
Joe Perches0fab6de2008-04-28 02:14:02 -0700183 bool bh_running;
184 bool bh_requested;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int dcd_chkcount; /* check counts to prevent */
187 int cts_chkcount; /* too many IRQs if a signal */
188 int dsr_chkcount; /* is floating */
189 int ri_chkcount;
190
Joe Perches0fab6de2008-04-28 02:14:02 -0700191 bool rx_enabled;
192 bool rx_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Joe Perches0fab6de2008-04-28 02:14:02 -0700194 bool tx_enabled;
195 bool tx_active;
196 bool tx_aborting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 u32 idle_mode;
198
199 int if_mode; /* serial interface selection (RS-232, v.35 etc) */
200
201 char device_name[25]; /* device instance name */
202
203 unsigned int io_base; /* base I/O address of adapter */
204 unsigned int irq_level;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 MGSL_PARAMS params; /* communications parameters */
207
208 unsigned char serial_signals; /* current serial signal states */
209
Joe Perches0fab6de2008-04-28 02:14:02 -0700210 bool irq_occurred; /* for diagnostics use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 char testing_irq;
212 unsigned int init_error; /* startup error (DIAGS) */
213
214 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
Joe Perches0fab6de2008-04-28 02:14:02 -0700215 bool drop_rts_on_tx_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 struct _input_signal_events input_signal_events;
218
219 /* PCMCIA support */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100220 struct pcmcia_device *p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 int stop;
222
223 /* SPPP/Cisco HDLC device parts */
224 int netcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 spinlock_t netlock;
226
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800227#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct net_device *netdev;
229#endif
230
231} MGSLPC_INFO;
232
233#define MGSLPC_MAGIC 0x5402
234
235/*
236 * The size of the serial xmit buffer is 1 page, or 4096 bytes
237 */
238#define TXBUFSIZE 4096
239
Jeff Garzikd12341f2007-10-19 15:45:35 -0400240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#define CHA 0x00 /* channel A offset */
242#define CHB 0x40 /* channel B offset */
243
244/*
245 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
246 */
247#undef PVR
248
249#define RXFIFO 0
250#define TXFIFO 0
251#define STAR 0x20
252#define CMDR 0x20
253#define RSTA 0x21
254#define PRE 0x21
255#define MODE 0x22
256#define TIMR 0x23
257#define XAD1 0x24
258#define XAD2 0x25
259#define RAH1 0x26
260#define RAH2 0x27
261#define DAFO 0x27
262#define RAL1 0x28
263#define RFC 0x28
264#define RHCR 0x29
265#define RAL2 0x29
266#define RBCL 0x2a
267#define XBCL 0x2a
268#define RBCH 0x2b
269#define XBCH 0x2b
270#define CCR0 0x2c
271#define CCR1 0x2d
272#define CCR2 0x2e
273#define CCR3 0x2f
274#define VSTR 0x34
275#define BGR 0x34
276#define RLCR 0x35
277#define AML 0x36
278#define AMH 0x37
279#define GIS 0x38
280#define IVA 0x38
281#define IPC 0x39
282#define ISR 0x3a
283#define IMR 0x3a
284#define PVR 0x3c
285#define PIS 0x3d
286#define PIM 0x3d
287#define PCR 0x3e
288#define CCR4 0x3f
Jeff Garzikd12341f2007-10-19 15:45:35 -0400289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290// IMR/ISR
Jeff Garzikd12341f2007-10-19 15:45:35 -0400291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292#define IRQ_BREAK_ON BIT15 // rx break detected
293#define IRQ_DATAOVERRUN BIT14 // receive data overflow
294#define IRQ_ALLSENT BIT13 // all sent
295#define IRQ_UNDERRUN BIT12 // transmit data underrun
296#define IRQ_TIMER BIT11 // timer interrupt
297#define IRQ_CTS BIT10 // CTS status change
298#define IRQ_TXREPEAT BIT9 // tx message repeat
299#define IRQ_TXFIFO BIT8 // transmit pool ready
300#define IRQ_RXEOM BIT7 // receive message end
301#define IRQ_EXITHUNT BIT6 // receive frame start
302#define IRQ_RXTIME BIT6 // rx char timeout
303#define IRQ_DCD BIT2 // carrier detect status change
304#define IRQ_OVERRUN BIT1 // receive frame overflow
305#define IRQ_RXFIFO BIT0 // receive pool full
Jeff Garzikd12341f2007-10-19 15:45:35 -0400306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307// STAR
Jeff Garzikd12341f2007-10-19 15:45:35 -0400308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#define XFW BIT6 // transmit FIFO write enable
310#define CEC BIT2 // command executing
311#define CTS BIT1 // CTS state
Jeff Garzikd12341f2007-10-19 15:45:35 -0400312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#define PVR_DTR BIT0
314#define PVR_DSR BIT1
315#define PVR_RI BIT2
316#define PVR_AUTOCTS BIT3
317#define PVR_RS232 0x20 /* 0010b */
318#define PVR_V35 0xe0 /* 1110b */
319#define PVR_RS422 0x40 /* 0100b */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400320
321/* Register access functions */
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
324#define read_reg(info, reg) inb((info)->io_base + (reg))
325
Jeff Garzikd12341f2007-10-19 15:45:35 -0400326#define read_reg16(info, reg) inw((info)->io_base + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
Jeff Garzikd12341f2007-10-19 15:45:35 -0400328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#define set_reg_bits(info, reg, mask) \
330 write_reg(info, (reg), \
Jeff Garzikd12341f2007-10-19 15:45:35 -0400331 (unsigned char) (read_reg(info, (reg)) | (mask)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#define clear_reg_bits(info, reg, mask) \
333 write_reg(info, (reg), \
Jeff Garzikd12341f2007-10-19 15:45:35 -0400334 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/*
336 * interrupt enable/disable routines
Jeff Garzikd12341f2007-10-19 15:45:35 -0400337 */
338static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 if (channel == CHA) {
341 info->imra_value |= mask;
342 write_reg16(info, CHA + IMR, info->imra_value);
343 } else {
344 info->imrb_value |= mask;
345 write_reg16(info, CHB + IMR, info->imrb_value);
346 }
347}
Jeff Garzikd12341f2007-10-19 15:45:35 -0400348static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 if (channel == CHA) {
351 info->imra_value &= ~mask;
352 write_reg16(info, CHA + IMR, info->imra_value);
353 } else {
354 info->imrb_value &= ~mask;
355 write_reg16(info, CHB + IMR, info->imrb_value);
356 }
357}
358
359#define port_irq_disable(info, mask) \
360 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
361
362#define port_irq_enable(info, mask) \
363 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
364
365static void rx_start(MGSLPC_INFO *info);
366static void rx_stop(MGSLPC_INFO *info);
367
Alan Coxeeb46132009-01-02 13:48:47 +0000368static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static void tx_stop(MGSLPC_INFO *info);
370static void tx_set_idle(MGSLPC_INFO *info);
371
372static void get_signals(MGSLPC_INFO *info);
373static void set_signals(MGSLPC_INFO *info);
374
375static void reset_device(MGSLPC_INFO *info);
376
377static void hdlc_mode(MGSLPC_INFO *info);
378static void async_mode(MGSLPC_INFO *info);
379
380static void tx_timeout(unsigned long context);
381
Alan Coxeeb46132009-01-02 13:48:47 +0000382static int carrier_raised(struct tty_port *port);
Alan Coxfcc8ac12009-06-11 12:24:17 +0100383static void dtr_rts(struct tty_port *port, int onoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800385#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#define dev_to_port(D) (dev_to_hdlc(D)->priv)
387static void hdlcdev_tx_done(MGSLPC_INFO *info);
388static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
389static int hdlcdev_init(MGSLPC_INFO *info);
390static void hdlcdev_exit(MGSLPC_INFO *info);
391#endif
392
393static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
394
Joe Perches0fab6de2008-04-28 02:14:02 -0700395static bool register_test(MGSLPC_INFO *info);
396static bool irq_test(MGSLPC_INFO *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397static int adapter_test(MGSLPC_INFO *info);
398
399static int claim_resources(MGSLPC_INFO *info);
400static void release_resources(MGSLPC_INFO *info);
401static void mgslpc_add_device(MGSLPC_INFO *info);
402static void mgslpc_remove_device(MGSLPC_INFO *info);
403
Alan Coxeeb46132009-01-02 13:48:47 +0000404static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static void rx_reset_buffers(MGSLPC_INFO *info);
406static int rx_alloc_buffers(MGSLPC_INFO *info);
407static void rx_free_buffers(MGSLPC_INFO *info);
408
David Howells7d12e782006-10-05 14:55:46 +0100409static irqreturn_t mgslpc_isr(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411/*
412 * Bottom half interrupt handlers
413 */
David Howellsc4028952006-11-22 14:57:56 +0000414static void bh_handler(struct work_struct *work);
Alan Coxeeb46132009-01-02 13:48:47 +0000415static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static void bh_status(MGSLPC_INFO *info);
417
418/*
419 * ioctl handlers
420 */
421static int tiocmget(struct tty_struct *tty, struct file *file);
422static int tiocmset(struct tty_struct *tty, struct file *file,
423 unsigned int set, unsigned int clear);
424static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
425static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
Alan Coxeeb46132009-01-02 13:48:47 +0000426static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
428static int set_txidle(MGSLPC_INFO *info, int idle_mode);
Alan Coxeeb46132009-01-02 13:48:47 +0000429static int set_txenable(MGSLPC_INFO *info, int enable, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static int tx_abort(MGSLPC_INFO *info);
431static int set_rxenable(MGSLPC_INFO *info, int enable);
432static int wait_events(MGSLPC_INFO *info, int __user *mask);
433
434static MGSLPC_INFO *mgslpc_device_list = NULL;
435static int mgslpc_device_count = 0;
436
437/*
438 * Set this param to non-zero to load eax with the
439 * .text section address and breakpoint on module load.
440 * This is useful for use with gdb and add-symbol-file command.
441 */
442static int break_on_load=0;
443
444/*
445 * Driver major number, defaults to zero to get auto
446 * assigned major number. May be forced as module parameter.
447 */
448static int ttymajor=0;
449
450static int debug_level = 0;
451static int maxframe[MAX_DEVICE_COUNT] = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453module_param(break_on_load, bool, 0);
454module_param(ttymajor, int, 0);
455module_param(debug_level, int, 0);
456module_param_array(maxframe, int, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458MODULE_LICENSE("GPL");
459
460static char *driver_name = "SyncLink PC Card driver";
Paul Fulghuma7482a22005-09-10 00:26:07 -0700461static char *driver_version = "$Revision: 4.34 $";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463static struct tty_driver *serial_driver;
464
465/* number of characters left in xmit buffer before we ask for more */
466#define WAKEUP_CHARS 256
467
Alan Coxeeb46132009-01-02 13:48:47 +0000468static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
470
471/* PCMCIA prototypes */
472
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200473static int mgslpc_config(struct pcmcia_device *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474static void mgslpc_release(u_long arg);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100475static void mgslpc_detach(struct pcmcia_device *p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/*
478 * 1st function defined in .text section. Calling this function in
479 * init_module() followed by a breakpoint allows a remote debugger
480 * (gdb) to get the .text address for the add-symbol-file command.
481 * This allows remote debugging of dynamically loadable modules.
482 */
483static void* mgslpc_get_text_ptr(void)
484{
485 return mgslpc_get_text_ptr;
486}
487
488/**
489 * line discipline callback wrappers
490 *
491 * The wrappers maintain line discipline references
492 * while calling into the line discipline.
493 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * ldisc_receive_buf - pass receive data to line discipline
495 */
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497static void ldisc_receive_buf(struct tty_struct *tty,
498 const __u8 *data, char *flags, int count)
499{
500 struct tty_ldisc *ld;
501 if (!tty)
502 return;
503 ld = tty_ldisc_ref(tty);
504 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100505 if (ld->ops->receive_buf)
506 ld->ops->receive_buf(tty, data, flags, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 tty_ldisc_deref(ld);
508 }
509}
510
Alan Coxeeb46132009-01-02 13:48:47 +0000511static const struct tty_port_operations mgslpc_port_ops = {
512 .carrier_raised = carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +0100513 .dtr_rts = dtr_rts
Alan Coxeeb46132009-01-02 13:48:47 +0000514};
515
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200516static int mgslpc_probe(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 MGSLPC_INFO *info;
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200519 int ret;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (debug_level >= DEBUG_LEVEL_INFO)
522 printk("mgslpc_attach\n");
Dominik Brodowskifd238232006-03-05 10:45:09 +0100523
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700524 info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (!info) {
526 printk("Error can't allocate device instance data\n");
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100527 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 info->magic = MGSLPC_MAGIC;
Alan Coxeeb46132009-01-02 13:48:47 +0000531 tty_port_init(&info->port);
532 info->port.ops = &mgslpc_port_ops;
David Howellsc4028952006-11-22 14:57:56 +0000533 INIT_WORK(&info->task, bh_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 info->max_frame_size = 4096;
Alan Coxeeb46132009-01-02 13:48:47 +0000535 info->port.close_delay = 5*HZ/10;
536 info->port.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 init_waitqueue_head(&info->status_event_wait_q);
538 init_waitqueue_head(&info->event_wait_q);
539 spin_lock_init(&info->lock);
540 spin_lock_init(&info->netlock);
541 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
Jeff Garzikd12341f2007-10-19 15:45:35 -0400542 info->idle_mode = HDLC_TXIDLE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 info->imra_value = 0xffff;
544 info->imrb_value = 0xffff;
545 info->pim_value = 0xff;
546
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200547 info->p_dev = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 link->priv = info;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100549
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200550 /* Initialize the struct pcmcia_device structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200552 ret = mgslpc_config(link);
553 if (ret)
554 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 mgslpc_add_device(info);
557
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/* Card has been inserted.
562 */
563
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200564static int mgslpc_ioprobe(struct pcmcia_device *p_dev,
565 cistpl_cftable_entry_t *cfg,
566 cistpl_cftable_entry_t *dflt,
567 unsigned int vcc,
568 void *priv_data)
569{
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200570 if (!cfg->io.nwin)
571 return -ENODEV;
572
573 p_dev->resource[0]->start = cfg->io.win[0].base;
574 p_dev->resource[0]->end = cfg->io.win[0].len;
575 p_dev->resource[0]->flags |= pcmcia_io_cfg_data_width(cfg->io.flags);
576 p_dev->io_lines = cfg->io.flags & CISTPL_IO_LINES_MASK;
577
578 return pcmcia_request_io(p_dev);
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200579}
580
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200581static int mgslpc_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 MGSLPC_INFO *info = link->priv;
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200584 int ret;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (debug_level >= DEBUG_LEVEL_INFO)
587 printk("mgslpc_config(0x%p)\n", link);
588
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200589 ret = pcmcia_loop_config(link, mgslpc_ioprobe, NULL);
590 if (ret != 0)
591 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200593 link->config_flags |= CONF_ENABLE_IRQ;
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200594 link->config_index = 8;
595 link->config_regs = PRESENT_OPTION;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400596
Dominik Brodowskieb141202010-03-07 12:21:16 +0100597 ret = pcmcia_request_irq(link, mgslpc_isr);
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200598 if (ret)
599 goto failed;
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200600 ret = pcmcia_enable_device(link);
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200601 if (ret)
602 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200604 info->io_base = link->resource[0]->start;
Dominik Brodowskieb141202010-03-07 12:21:16 +0100605 info->irq_level = link->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Dominik Brodowskided6a1a2010-03-20 19:35:12 +0100607 dev_info(&link->dev, "index 0x%02x:",
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200608 link->config_index);
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200609 printk(", irq %d", link->irq);
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200610 if (link->resource[0])
611 printk(", io %pR", link->resource[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 printk("\n");
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200615failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 mgslpc_release((u_long)link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200617 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620/* Card has been removed.
621 * Unregister device and release PCMCIA configuration.
622 * If device is open, postpone until it is closed.
623 */
624static void mgslpc_release(u_long arg)
625{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100626 struct pcmcia_device *link = (struct pcmcia_device *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100628 if (debug_level >= DEBUG_LEVEL_INFO)
629 printk("mgslpc_release(0x%p)\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100631 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200634static void mgslpc_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100636 if (debug_level >= DEBUG_LEVEL_INFO)
637 printk("mgslpc_detach(0x%p)\n", link);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100638
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100639 ((MGSLPC_INFO *)link->priv)->stop = 1;
640 mgslpc_release((u_long)link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100642 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200645static int mgslpc_suspend(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100646{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100647 MGSLPC_INFO *info = link->priv;
648
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100649 info->stop = 1;
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100650
651 return 0;
652}
653
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200654static int mgslpc_resume(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100655{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100656 MGSLPC_INFO *info = link->priv;
657
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100658 info->stop = 0;
659
660 return 0;
661}
662
663
Joe Perches0fab6de2008-04-28 02:14:02 -0700664static inline bool mgslpc_paranoia_check(MGSLPC_INFO *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 char *name, const char *routine)
666{
667#ifdef MGSLPC_PARANOIA_CHECK
668 static const char *badmagic =
669 "Warning: bad magic number for mgsl struct (%s) in %s\n";
670 static const char *badinfo =
671 "Warning: null mgslpc_info for (%s) in %s\n";
672
673 if (!info) {
674 printk(badinfo, name, routine);
Joe Perches0fab6de2008-04-28 02:14:02 -0700675 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 if (info->magic != MGSLPC_MAGIC) {
678 printk(badmagic, name, routine);
Joe Perches0fab6de2008-04-28 02:14:02 -0700679 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681#else
682 if (!info)
Joe Perches0fab6de2008-04-28 02:14:02 -0700683 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#endif
Joe Perches0fab6de2008-04-28 02:14:02 -0700685 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688
689#define CMD_RXFIFO BIT7 // release current rx FIFO
690#define CMD_RXRESET BIT6 // receiver reset
691#define CMD_RXFIFO_READ BIT5
692#define CMD_START_TIMER BIT4
693#define CMD_TXFIFO BIT3 // release current tx FIFO
694#define CMD_TXEOM BIT1 // transmit end message
695#define CMD_TXRESET BIT0 // transmit reset
696
Joe Perches0fab6de2008-04-28 02:14:02 -0700697static bool wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 int i = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400700 /* wait for command completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
702 udelay(1);
703 if (i++ == 1000)
Joe Perches0fab6de2008-04-28 02:14:02 -0700704 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Joe Perches0fab6de2008-04-28 02:14:02 -0700706 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Jeff Garzikd12341f2007-10-19 15:45:35 -0400709static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 wait_command_complete(info, channel);
712 write_reg(info, (unsigned char) (channel + CMDR), cmd);
713}
714
715static void tx_pause(struct tty_struct *tty)
716{
717 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
718 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
721 return;
722 if (debug_level >= DEBUG_LEVEL_INFO)
Jeff Garzikd12341f2007-10-19 15:45:35 -0400723 printk("tx_pause(%s)\n",info->device_name);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 spin_lock_irqsave(&info->lock,flags);
726 if (info->tx_enabled)
727 tx_stop(info);
728 spin_unlock_irqrestore(&info->lock,flags);
729}
730
731static void tx_release(struct tty_struct *tty)
732{
733 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
734 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
737 return;
738 if (debug_level >= DEBUG_LEVEL_INFO)
Jeff Garzikd12341f2007-10-19 15:45:35 -0400739 printk("tx_release(%s)\n",info->device_name);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 spin_lock_irqsave(&info->lock,flags);
742 if (!info->tx_enabled)
Alan Coxeeb46132009-01-02 13:48:47 +0000743 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 spin_unlock_irqrestore(&info->lock,flags);
745}
746
747/* Return next bottom half action to perform.
748 * or 0 if nothing to do.
749 */
750static int bh_action(MGSLPC_INFO *info)
751{
752 unsigned long flags;
753 int rc = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 spin_lock_irqsave(&info->lock,flags);
756
757 if (info->pending_bh & BH_RECEIVE) {
758 info->pending_bh &= ~BH_RECEIVE;
759 rc = BH_RECEIVE;
760 } else if (info->pending_bh & BH_TRANSMIT) {
761 info->pending_bh &= ~BH_TRANSMIT;
762 rc = BH_TRANSMIT;
763 } else if (info->pending_bh & BH_STATUS) {
764 info->pending_bh &= ~BH_STATUS;
765 rc = BH_STATUS;
766 }
767
768 if (!rc) {
769 /* Mark BH routine as complete */
Joe Perches0fab6de2008-04-28 02:14:02 -0700770 info->bh_running = false;
771 info->bh_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Jeff Garzikd12341f2007-10-19 15:45:35 -0400773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return rc;
777}
778
David Howellsc4028952006-11-22 14:57:56 +0000779static void bh_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
David Howellsc4028952006-11-22 14:57:56 +0000781 MGSLPC_INFO *info = container_of(work, MGSLPC_INFO, task);
Alan Coxeeb46132009-01-02 13:48:47 +0000782 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int action;
784
785 if (!info)
786 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (debug_level >= DEBUG_LEVEL_BH)
789 printk( "%s(%d):bh_handler(%s) entry\n",
790 __FILE__,__LINE__,info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400791
Joe Perches0fab6de2008-04-28 02:14:02 -0700792 info->bh_running = true;
Alan Coxeeb46132009-01-02 13:48:47 +0000793 tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 while((action = bh_action(info)) != 0) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /* Process work item */
798 if ( debug_level >= DEBUG_LEVEL_BH )
799 printk( "%s(%d):bh_handler() work item action=%d\n",
800 __FILE__,__LINE__,action);
801
802 switch (action) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 case BH_RECEIVE:
Alan Coxeeb46132009-01-02 13:48:47 +0000805 while(rx_get_frame(info, tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 break;
807 case BH_TRANSMIT:
Alan Coxeeb46132009-01-02 13:48:47 +0000808 bh_transmit(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 break;
810 case BH_STATUS:
811 bh_status(info);
812 break;
813 default:
814 /* unknown work item ID */
815 printk("Unknown work item ID=%08X!\n", action);
816 break;
817 }
818 }
819
Alan Coxeeb46132009-01-02 13:48:47 +0000820 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (debug_level >= DEBUG_LEVEL_BH)
822 printk( "%s(%d):bh_handler(%s) exit\n",
823 __FILE__,__LINE__,info->device_name);
824}
825
Alan Coxeeb46132009-01-02 13:48:47 +0000826static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (debug_level >= DEBUG_LEVEL_BH)
829 printk("bh_transmit() entry on %s\n", info->device_name);
830
Jiri Slabyb963a842007-02-10 01:44:55 -0800831 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Peter Hagervallcdaad342006-06-23 02:06:04 -0700835static void bh_status(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 info->ri_chkcount = 0;
838 info->dsr_chkcount = 0;
839 info->dcd_chkcount = 0;
840 info->cts_chkcount = 0;
841}
842
Jeff Garzikd12341f2007-10-19 15:45:35 -0400843/* eom: non-zero = end of frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
845{
846 unsigned char data[2];
847 unsigned char fifo_count, read_count, i;
848 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
849
850 if (debug_level >= DEBUG_LEVEL_ISR)
851 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (!info->rx_enabled)
854 return;
855
856 if (info->rx_frame_count >= info->rx_buf_count) {
857 /* no more free buffers */
858 issue_command(info, CHA, CMD_RXRESET);
859 info->pending_bh |= BH_RECEIVE;
Joe Perches0fab6de2008-04-28 02:14:02 -0700860 info->rx_overflow = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 info->icount.buf_overrun++;
862 return;
863 }
864
865 if (eom) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400866 /* end of frame, get FIFO count from RBCL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
868 fifo_count = 32;
869 } else
870 fifo_count = 32;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 do {
873 if (fifo_count == 1) {
874 read_count = 1;
875 data[0] = read_reg(info, CHA + RXFIFO);
876 } else {
877 read_count = 2;
878 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
879 }
880 fifo_count -= read_count;
881 if (!fifo_count && eom)
882 buf->status = data[--read_count];
883
884 for (i = 0; i < read_count; i++) {
885 if (buf->count >= info->max_frame_size) {
886 /* frame too large, reset receiver and reset current buffer */
887 issue_command(info, CHA, CMD_RXRESET);
888 buf->count = 0;
889 return;
890 }
891 *(buf->data + buf->count) = data[i];
892 buf->count++;
893 }
894 } while (fifo_count);
895
896 if (eom) {
897 info->pending_bh |= BH_RECEIVE;
898 info->rx_frame_count++;
899 info->rx_put++;
900 if (info->rx_put >= info->rx_buf_count)
901 info->rx_put = 0;
902 }
903 issue_command(info, CHA, CMD_RXFIFO);
904}
905
Alan Coxeeb46132009-01-02 13:48:47 +0000906static void rx_ready_async(MGSLPC_INFO *info, int tcd, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Alan Cox33f0f882006-01-09 20:54:13 -0800908 unsigned char data, status, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int fifo_count;
Alan Cox33f0f882006-01-09 20:54:13 -0800910 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct mgsl_icount *icount = &info->icount;
912
913 if (tcd) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400914 /* early termination, get FIFO count from RBCL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
916
917 /* Zero fifo count could mean 0 or 32 bytes available.
918 * If BIT5 of STAR is set then at least 1 byte is available.
919 */
920 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
921 fifo_count = 32;
922 } else
923 fifo_count = 32;
Alan Cox33f0f882006-01-09 20:54:13 -0800924
925 tty_buffer_request_room(tty, fifo_count);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400926 /* Flush received async data to receive data buffer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 while (fifo_count) {
928 data = read_reg(info, CHA + RXFIFO);
929 status = read_reg(info, CHA + RXFIFO);
930 fifo_count -= 2;
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 icount->rx++;
Alan Cox33f0f882006-01-09 20:54:13 -0800933 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 // if no frameing/crc error then save data
936 // BIT7:parity error
937 // BIT6:framing error
938
939 if (status & (BIT7 + BIT6)) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400940 if (status & BIT7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 icount->parity++;
942 else
943 icount->frame++;
944
945 /* discard char if tty control flags say so */
946 if (status & info->ignore_status_mask)
947 continue;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 status &= info->read_status_mask;
950
951 if (status & BIT7)
Alan Cox33f0f882006-01-09 20:54:13 -0800952 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 else if (status & BIT6)
Alan Cox33f0f882006-01-09 20:54:13 -0800954 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Alan Cox33f0f882006-01-09 20:54:13 -0800956 work += tty_insert_flip_char(tty, data, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958 issue_command(info, CHA, CMD_RXFIFO);
959
960 if (debug_level >= DEBUG_LEVEL_ISR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800961 printk("%s(%d):rx_ready_async",
962 __FILE__,__LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
964 __FILE__,__LINE__,icount->rx,icount->brk,
965 icount->parity,icount->frame,icount->overrun);
966 }
Jeff Garzikd12341f2007-10-19 15:45:35 -0400967
Alan Cox33f0f882006-01-09 20:54:13 -0800968 if (work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 tty_flip_buffer_push(tty);
970}
971
972
Alan Coxeeb46132009-01-02 13:48:47 +0000973static void tx_done(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 if (!info->tx_active)
976 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400977
Joe Perches0fab6de2008-04-28 02:14:02 -0700978 info->tx_active = false;
979 info->tx_aborting = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (info->params.mode == MGSL_MODE_ASYNC)
982 return;
983
984 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400985 del_timer(&info->tx_timer);
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (info->drop_rts_on_tx_done) {
988 get_signals(info);
989 if (info->serial_signals & SerialSignal_RTS) {
990 info->serial_signals &= ~SerialSignal_RTS;
991 set_signals(info);
992 }
Joe Perches0fab6de2008-04-28 02:14:02 -0700993 info->drop_rts_on_tx_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800996#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (info->netcount)
998 hdlcdev_tx_done(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400999 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000#endif
1001 {
Alan Coxeeb46132009-01-02 13:48:47 +00001002 if (tty->stopped || tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 tx_stop(info);
1004 return;
1005 }
1006 info->pending_bh |= BH_TRANSMIT;
1007 }
1008}
1009
Alan Coxeeb46132009-01-02 13:48:47 +00001010static void tx_ready(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 unsigned char fifo_count = 32;
1013 int c;
1014
1015 if (debug_level >= DEBUG_LEVEL_ISR)
1016 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1017
1018 if (info->params.mode == MGSL_MODE_HDLC) {
1019 if (!info->tx_active)
1020 return;
1021 } else {
Alan Coxeeb46132009-01-02 13:48:47 +00001022 if (tty->stopped || tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 tx_stop(info);
1024 return;
1025 }
1026 if (!info->tx_count)
Joe Perches0fab6de2008-04-28 02:14:02 -07001027 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
1030 if (!info->tx_count)
1031 return;
1032
1033 while (info->tx_count && fifo_count) {
1034 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
Jeff Garzikd12341f2007-10-19 15:45:35 -04001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (c == 1) {
1037 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1038 } else {
1039 write_reg16(info, CHA + TXFIFO,
1040 *((unsigned short*)(info->tx_buf + info->tx_get)));
1041 }
1042 info->tx_count -= c;
1043 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1044 fifo_count -= c;
1045 }
1046
1047 if (info->params.mode == MGSL_MODE_ASYNC) {
1048 if (info->tx_count < WAKEUP_CHARS)
1049 info->pending_bh |= BH_TRANSMIT;
1050 issue_command(info, CHA, CMD_TXFIFO);
1051 } else {
1052 if (info->tx_count)
1053 issue_command(info, CHA, CMD_TXFIFO);
1054 else
1055 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1056 }
1057}
1058
Alan Coxeeb46132009-01-02 13:48:47 +00001059static void cts_change(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 get_signals(info);
1062 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1063 irq_disable(info, CHB, IRQ_CTS);
1064 info->icount.cts++;
1065 if (info->serial_signals & SerialSignal_CTS)
1066 info->input_signal_events.cts_up++;
1067 else
1068 info->input_signal_events.cts_down++;
1069 wake_up_interruptible(&info->status_event_wait_q);
1070 wake_up_interruptible(&info->event_wait_q);
1071
Alan Coxeeb46132009-01-02 13:48:47 +00001072 if (info->port.flags & ASYNC_CTS_FLOW) {
1073 if (tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (info->serial_signals & SerialSignal_CTS) {
1075 if (debug_level >= DEBUG_LEVEL_ISR)
1076 printk("CTS tx start...");
Alan Coxeeb46132009-01-02 13:48:47 +00001077 if (tty)
1078 tty->hw_stopped = 0;
1079 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 info->pending_bh |= BH_TRANSMIT;
1081 return;
1082 }
1083 } else {
1084 if (!(info->serial_signals & SerialSignal_CTS)) {
1085 if (debug_level >= DEBUG_LEVEL_ISR)
1086 printk("CTS tx stop...");
Alan Coxeeb46132009-01-02 13:48:47 +00001087 if (tty)
1088 tty->hw_stopped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 tx_stop(info);
1090 }
1091 }
1092 }
1093 info->pending_bh |= BH_STATUS;
1094}
1095
Alan Coxeeb46132009-01-02 13:48:47 +00001096static void dcd_change(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 get_signals(info);
1099 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1100 irq_disable(info, CHB, IRQ_DCD);
1101 info->icount.dcd++;
1102 if (info->serial_signals & SerialSignal_DCD) {
1103 info->input_signal_events.dcd_up++;
1104 }
1105 else
1106 info->input_signal_events.dcd_down++;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08001107#if SYNCLINK_GENERIC_HDLC
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07001108 if (info->netcount) {
1109 if (info->serial_signals & SerialSignal_DCD)
1110 netif_carrier_on(info->netdev);
1111 else
1112 netif_carrier_off(info->netdev);
1113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114#endif
1115 wake_up_interruptible(&info->status_event_wait_q);
1116 wake_up_interruptible(&info->event_wait_q);
1117
Alan Coxeeb46132009-01-02 13:48:47 +00001118 if (info->port.flags & ASYNC_CHECK_CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (debug_level >= DEBUG_LEVEL_ISR)
1120 printk("%s CD now %s...", info->device_name,
1121 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1122 if (info->serial_signals & SerialSignal_DCD)
Alan Coxeeb46132009-01-02 13:48:47 +00001123 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 else {
1125 if (debug_level >= DEBUG_LEVEL_ISR)
1126 printk("doing serial hangup...");
Alan Coxeeb46132009-01-02 13:48:47 +00001127 if (tty)
1128 tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 }
1131 info->pending_bh |= BH_STATUS;
1132}
1133
1134static void dsr_change(MGSLPC_INFO *info)
1135{
1136 get_signals(info);
1137 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1138 port_irq_disable(info, PVR_DSR);
1139 info->icount.dsr++;
1140 if (info->serial_signals & SerialSignal_DSR)
1141 info->input_signal_events.dsr_up++;
1142 else
1143 info->input_signal_events.dsr_down++;
1144 wake_up_interruptible(&info->status_event_wait_q);
1145 wake_up_interruptible(&info->event_wait_q);
1146 info->pending_bh |= BH_STATUS;
1147}
1148
1149static void ri_change(MGSLPC_INFO *info)
1150{
1151 get_signals(info);
1152 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1153 port_irq_disable(info, PVR_RI);
1154 info->icount.rng++;
1155 if (info->serial_signals & SerialSignal_RI)
1156 info->input_signal_events.ri_up++;
1157 else
1158 info->input_signal_events.ri_down++;
1159 wake_up_interruptible(&info->status_event_wait_q);
1160 wake_up_interruptible(&info->event_wait_q);
1161 info->pending_bh |= BH_STATUS;
1162}
1163
1164/* Interrupt service routine entry point.
Jeff Garzikd12341f2007-10-19 15:45:35 -04001165 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001167 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 * irq interrupt number that caused interrupt
1169 * dev_id device ID supplied during interrupt registration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 */
Jeff Garzika6f97b22007-10-31 05:20:49 -04001171static irqreturn_t mgslpc_isr(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Jeff Garzika6f97b22007-10-31 05:20:49 -04001173 MGSLPC_INFO *info = dev_id;
Alan Coxeeb46132009-01-02 13:48:47 +00001174 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 unsigned short isr;
1176 unsigned char gis, pis;
1177 int count=0;
1178
Jeff Garzikd12341f2007-10-19 15:45:35 -04001179 if (debug_level >= DEBUG_LEVEL_ISR)
Jeff Garzika6f97b22007-10-31 05:20:49 -04001180 printk("mgslpc_isr(%d) entry.\n", info->irq_level);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001181
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001182 if (!(info->p_dev->_locked))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return IRQ_HANDLED;
1184
Alan Coxeeb46132009-01-02 13:48:47 +00001185 tty = tty_port_tty_get(&info->port);
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 spin_lock(&info->lock);
1188
1189 while ((gis = read_reg(info, CHA + GIS))) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001190 if (debug_level >= DEBUG_LEVEL_ISR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1192
1193 if ((gis & 0x70) || count > 1000) {
1194 printk("synclink_cs:hardware failed or ejected\n");
1195 break;
1196 }
1197 count++;
1198
1199 if (gis & (BIT1 + BIT0)) {
1200 isr = read_reg16(info, CHB + ISR);
1201 if (isr & IRQ_DCD)
Alan Coxeeb46132009-01-02 13:48:47 +00001202 dcd_change(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (isr & IRQ_CTS)
Alan Coxeeb46132009-01-02 13:48:47 +00001204 cts_change(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 if (gis & (BIT3 + BIT2))
1207 {
1208 isr = read_reg16(info, CHA + ISR);
1209 if (isr & IRQ_TIMER) {
Joe Perches0fab6de2008-04-28 02:14:02 -07001210 info->irq_occurred = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 irq_disable(info, CHA, IRQ_TIMER);
1212 }
1213
Jeff Garzikd12341f2007-10-19 15:45:35 -04001214 /* receive IRQs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (isr & IRQ_EXITHUNT) {
1216 info->icount.exithunt++;
1217 wake_up_interruptible(&info->event_wait_q);
1218 }
1219 if (isr & IRQ_BREAK_ON) {
1220 info->icount.brk++;
Alan Coxeeb46132009-01-02 13:48:47 +00001221 if (info->port.flags & ASYNC_SAK)
1222 do_SAK(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 if (isr & IRQ_RXTIME) {
1225 issue_command(info, CHA, CMD_RXFIFO_READ);
1226 }
1227 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1228 if (info->params.mode == MGSL_MODE_HDLC)
Jeff Garzikd12341f2007-10-19 15:45:35 -04001229 rx_ready_hdlc(info, isr & IRQ_RXEOM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 else
Alan Coxeeb46132009-01-02 13:48:47 +00001231 rx_ready_async(info, isr & IRQ_RXEOM, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
Jeff Garzikd12341f2007-10-19 15:45:35 -04001234 /* transmit IRQs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (isr & IRQ_UNDERRUN) {
1236 if (info->tx_aborting)
1237 info->icount.txabort++;
1238 else
1239 info->icount.txunder++;
Alan Coxeeb46132009-01-02 13:48:47 +00001240 tx_done(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242 else if (isr & IRQ_ALLSENT) {
1243 info->icount.txok++;
Alan Coxeeb46132009-01-02 13:48:47 +00001244 tx_done(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246 else if (isr & IRQ_TXFIFO)
Alan Coxeeb46132009-01-02 13:48:47 +00001247 tx_ready(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249 if (gis & BIT7) {
1250 pis = read_reg(info, CHA + PIS);
1251 if (pis & BIT1)
1252 dsr_change(info);
1253 if (pis & BIT2)
1254 ri_change(info);
1255 }
1256 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001257
1258 /* Request bottom half processing if there's something
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 * for it to do and the bh is not already running
1260 */
1261
1262 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001263 if ( debug_level >= DEBUG_LEVEL_ISR )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 printk("%s(%d):%s queueing bh task.\n",
1265 __FILE__,__LINE__,info->device_name);
1266 schedule_work(&info->task);
Joe Perches0fab6de2008-04-28 02:14:02 -07001267 info->bh_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269
1270 spin_unlock(&info->lock);
Alan Coxeeb46132009-01-02 13:48:47 +00001271 tty_kref_put(tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001272
1273 if (debug_level >= DEBUG_LEVEL_ISR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 printk("%s(%d):mgslpc_isr(%d)exit.\n",
Jeff Garzika6f97b22007-10-31 05:20:49 -04001275 __FILE__, __LINE__, info->irq_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 return IRQ_HANDLED;
1278}
1279
1280/* Initialize and start device.
1281 */
Alan Coxeeb46132009-01-02 13:48:47 +00001282static int startup(MGSLPC_INFO * info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 int retval = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (debug_level >= DEBUG_LEVEL_INFO)
1287 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001288
Alan Coxeeb46132009-01-02 13:48:47 +00001289 if (info->port.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (!info->tx_buf) {
1293 /* allocate a page of memory for a transmit buffer */
1294 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1295 if (!info->tx_buf) {
1296 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1297 __FILE__,__LINE__,info->device_name);
1298 return -ENOMEM;
1299 }
1300 }
1301
1302 info->pending_bh = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001303
Paul Fulghuma7482a22005-09-10 00:26:07 -07001304 memset(&info->icount, 0, sizeof(info->icount));
1305
Jiri Slaby40565f12007-02-12 00:52:31 -08001306 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /* Allocate and claim adapter resources */
1309 retval = claim_resources(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /* perform existance check and diagnostics */
1312 if ( !retval )
1313 retval = adapter_test(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if ( retval ) {
Alan Coxeeb46132009-01-02 13:48:47 +00001316 if (capable(CAP_SYS_ADMIN) && tty)
1317 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 release_resources(info);
1319 return retval;
1320 }
1321
1322 /* program hardware for current parameters */
Alan Coxeeb46132009-01-02 13:48:47 +00001323 mgslpc_change_params(info, tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001324
Alan Coxeeb46132009-01-02 13:48:47 +00001325 if (tty)
1326 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Alan Coxeeb46132009-01-02 13:48:47 +00001328 info->port.flags |= ASYNC_INITIALIZED;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return 0;
1331}
1332
1333/* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1334 */
Alan Coxeeb46132009-01-02 13:48:47 +00001335static void shutdown(MGSLPC_INFO * info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001338
Alan Coxeeb46132009-01-02 13:48:47 +00001339 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return;
1341
1342 if (debug_level >= DEBUG_LEVEL_INFO)
1343 printk("%s(%d):mgslpc_shutdown(%s)\n",
1344 __FILE__,__LINE__, info->device_name );
1345
1346 /* clear status wait queue because status changes */
1347 /* can't happen after shutting down the hardware */
1348 wake_up_interruptible(&info->status_event_wait_q);
1349 wake_up_interruptible(&info->event_wait_q);
1350
Jiri Slaby40565f12007-02-12 00:52:31 -08001351 del_timer_sync(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (info->tx_buf) {
1354 free_page((unsigned long) info->tx_buf);
1355 info->tx_buf = NULL;
1356 }
1357
1358 spin_lock_irqsave(&info->lock,flags);
1359
1360 rx_stop(info);
1361 tx_stop(info);
1362
1363 /* TODO:disable interrupts instead of reset to preserve signal states */
1364 reset_device(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001365
Alan Coxeeb46132009-01-02 13:48:47 +00001366 if (!tty || tty->termios->c_cflag & HUPCL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1368 set_signals(info);
1369 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 spin_unlock_irqrestore(&info->lock,flags);
1372
Jeff Garzikd12341f2007-10-19 15:45:35 -04001373 release_resources(info);
1374
Alan Coxeeb46132009-01-02 13:48:47 +00001375 if (tty)
1376 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Alan Coxeeb46132009-01-02 13:48:47 +00001378 info->port.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Alan Coxeeb46132009-01-02 13:48:47 +00001381static void mgslpc_program_hw(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 unsigned long flags;
1384
1385 spin_lock_irqsave(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 rx_stop(info);
1388 tx_stop(info);
1389 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1392 hdlc_mode(info);
1393 else
1394 async_mode(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 set_signals(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 info->dcd_chkcount = 0;
1399 info->cts_chkcount = 0;
1400 info->ri_chkcount = 0;
1401 info->dsr_chkcount = 0;
1402
1403 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1404 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1405 get_signals(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001406
Alan Coxeeb46132009-01-02 13:48:47 +00001407 if (info->netcount || (tty && (tty->termios->c_cflag & CREAD)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 rx_start(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 spin_unlock_irqrestore(&info->lock,flags);
1411}
1412
1413/* Reconfigure adapter based on new parameters
1414 */
Alan Coxeeb46132009-01-02 13:48:47 +00001415static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 unsigned cflag;
1418 int bits_per_char;
1419
Alan Coxeeb46132009-01-02 13:48:47 +00001420 if (!tty || !tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (debug_level >= DEBUG_LEVEL_INFO)
1424 printk("%s(%d):mgslpc_change_params(%s)\n",
1425 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001426
Alan Coxeeb46132009-01-02 13:48:47 +00001427 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 /* if B0 rate (hangup) specified then negate DTR and RTS */
1430 /* otherwise assert DTR and RTS */
1431 if (cflag & CBAUD)
1432 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1433 else
1434 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /* byte size and parity */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 switch (cflag & CSIZE) {
1439 case CS5: info->params.data_bits = 5; break;
1440 case CS6: info->params.data_bits = 6; break;
1441 case CS7: info->params.data_bits = 7; break;
1442 case CS8: info->params.data_bits = 8; break;
1443 default: info->params.data_bits = 7; break;
1444 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (cflag & CSTOPB)
1447 info->params.stop_bits = 2;
1448 else
1449 info->params.stop_bits = 1;
1450
1451 info->params.parity = ASYNC_PARITY_NONE;
1452 if (cflag & PARENB) {
1453 if (cflag & PARODD)
1454 info->params.parity = ASYNC_PARITY_ODD;
1455 else
1456 info->params.parity = ASYNC_PARITY_EVEN;
1457#ifdef CMSPAR
1458 if (cflag & CMSPAR)
1459 info->params.parity = ASYNC_PARITY_SPACE;
1460#endif
1461 }
1462
1463 /* calculate number of jiffies to transmit a full
1464 * FIFO (32 bytes) at specified data rate
1465 */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001466 bits_per_char = info->params.data_bits +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 info->params.stop_bits + 1;
1468
1469 /* if port data rate is set to 460800 or less then
1470 * allow tty settings to override, otherwise keep the
1471 * current data rate.
1472 */
1473 if (info->params.data_rate <= 460800) {
Alan Coxeeb46132009-01-02 13:48:47 +00001474 info->params.data_rate = tty_get_baud_rate(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if ( info->params.data_rate ) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001478 info->timeout = (32*HZ*bits_per_char) /
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 info->params.data_rate;
1480 }
1481 info->timeout += HZ/50; /* Add .02 seconds of slop */
1482
1483 if (cflag & CRTSCTS)
Alan Coxeeb46132009-01-02 13:48:47 +00001484 info->port.flags |= ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 else
Alan Coxeeb46132009-01-02 13:48:47 +00001486 info->port.flags &= ~ASYNC_CTS_FLOW;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (cflag & CLOCAL)
Alan Coxeeb46132009-01-02 13:48:47 +00001489 info->port.flags &= ~ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 else
Alan Coxeeb46132009-01-02 13:48:47 +00001491 info->port.flags |= ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 /* process tty input control flags */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 info->read_status_mask = 0;
Alan Coxeeb46132009-01-02 13:48:47 +00001496 if (I_INPCK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 info->read_status_mask |= BIT7 | BIT6;
Alan Coxeeb46132009-01-02 13:48:47 +00001498 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 info->ignore_status_mask |= BIT7 | BIT6;
1500
Alan Coxeeb46132009-01-02 13:48:47 +00001501 mgslpc_program_hw(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
1504/* Add a character to the transmit buffer
1505 */
Alan Coxd7e752e2008-04-30 00:54:04 -07001506static int mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1509 unsigned long flags;
1510
1511 if (debug_level >= DEBUG_LEVEL_INFO) {
1512 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1513 __FILE__,__LINE__,ch,info->device_name);
1514 }
1515
1516 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
Alan Coxd7e752e2008-04-30 00:54:04 -07001517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Eric Sesterhenn326f28e92006-06-25 05:48:48 -07001519 if (!info->tx_buf)
Alan Coxd7e752e2008-04-30 00:54:04 -07001520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 spin_lock_irqsave(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1525 if (info->tx_count < TXBUFSIZE - 1) {
1526 info->tx_buf[info->tx_put++] = ch;
1527 info->tx_put &= TXBUFSIZE-1;
1528 info->tx_count++;
1529 }
1530 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 spin_unlock_irqrestore(&info->lock,flags);
Alan Coxd7e752e2008-04-30 00:54:04 -07001533 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536/* Enable transmitter so remaining characters in the
1537 * transmit buffer are sent.
1538 */
1539static void mgslpc_flush_chars(struct tty_struct *tty)
1540{
1541 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1542 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (debug_level >= DEBUG_LEVEL_INFO)
1545 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1546 __FILE__,__LINE__,info->device_name,info->tx_count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1549 return;
1550
1551 if (info->tx_count <= 0 || tty->stopped ||
1552 tty->hw_stopped || !info->tx_buf)
1553 return;
1554
1555 if (debug_level >= DEBUG_LEVEL_INFO)
1556 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1557 __FILE__,__LINE__,info->device_name);
1558
1559 spin_lock_irqsave(&info->lock,flags);
1560 if (!info->tx_active)
Alan Coxeeb46132009-01-02 13:48:47 +00001561 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 spin_unlock_irqrestore(&info->lock,flags);
1563}
1564
1565/* Send a block of data
Jeff Garzikd12341f2007-10-19 15:45:35 -04001566 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001568 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 * tty pointer to tty information structure
1570 * buf pointer to buffer containing send data
1571 * count size of send data in bytes
Jeff Garzikd12341f2007-10-19 15:45:35 -04001572 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 * Returns: number of characters written
1574 */
1575static int mgslpc_write(struct tty_struct * tty,
1576 const unsigned char *buf, int count)
1577{
1578 int c, ret = 0;
1579 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1580 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (debug_level >= DEBUG_LEVEL_INFO)
1583 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1584 __FILE__,__LINE__,info->device_name,count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
Eric Sesterhenn326f28e92006-06-25 05:48:48 -07001587 !info->tx_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 goto cleanup;
1589
1590 if (info->params.mode == MGSL_MODE_HDLC) {
1591 if (count > TXBUFSIZE) {
1592 ret = -EIO;
1593 goto cleanup;
1594 }
1595 if (info->tx_active)
1596 goto cleanup;
1597 else if (info->tx_count)
1598 goto start;
1599 }
1600
1601 for (;;) {
1602 c = min(count,
1603 min(TXBUFSIZE - info->tx_count - 1,
1604 TXBUFSIZE - info->tx_put));
1605 if (c <= 0)
1606 break;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 memcpy(info->tx_buf + info->tx_put, buf, c);
1609
1610 spin_lock_irqsave(&info->lock,flags);
1611 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1612 info->tx_count += c;
1613 spin_unlock_irqrestore(&info->lock,flags);
1614
1615 buf += c;
1616 count -= c;
1617 ret += c;
1618 }
1619start:
1620 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1621 spin_lock_irqsave(&info->lock,flags);
1622 if (!info->tx_active)
Alan Coxeeb46132009-01-02 13:48:47 +00001623 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 spin_unlock_irqrestore(&info->lock,flags);
1625 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001626cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (debug_level >= DEBUG_LEVEL_INFO)
1628 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1629 __FILE__,__LINE__,info->device_name,ret);
1630 return ret;
1631}
1632
1633/* Return the count of free bytes in transmit buffer
1634 */
1635static int mgslpc_write_room(struct tty_struct *tty)
1636{
1637 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1638 int ret;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1641 return 0;
1642
1643 if (info->params.mode == MGSL_MODE_HDLC) {
1644 /* HDLC (frame oriented) mode */
1645 if (info->tx_active)
1646 return 0;
1647 else
1648 return HDLC_MAX_FRAME_SIZE;
1649 } else {
1650 ret = TXBUFSIZE - info->tx_count - 1;
1651 if (ret < 0)
1652 ret = 0;
1653 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (debug_level >= DEBUG_LEVEL_INFO)
1656 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1657 __FILE__,__LINE__, info->device_name, ret);
1658 return ret;
1659}
1660
1661/* Return the count of bytes in transmit buffer
1662 */
1663static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1664{
1665 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1666 int rc;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (debug_level >= DEBUG_LEVEL_INFO)
1669 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1670 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1673 return 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (info->params.mode == MGSL_MODE_HDLC)
1676 rc = info->tx_active ? info->max_frame_size : 0;
1677 else
1678 rc = info->tx_count;
1679
1680 if (debug_level >= DEBUG_LEVEL_INFO)
1681 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1682 __FILE__,__LINE__, info->device_name, rc);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return rc;
1685}
1686
1687/* Discard all data in the send buffer
1688 */
1689static void mgslpc_flush_buffer(struct tty_struct *tty)
1690{
1691 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1692 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (debug_level >= DEBUG_LEVEL_INFO)
1695 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1696 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1699 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001700
1701 spin_lock_irqsave(&info->lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001703 del_timer(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 spin_unlock_irqrestore(&info->lock,flags);
1705
1706 wake_up_interruptible(&tty->write_wait);
1707 tty_wakeup(tty);
1708}
1709
1710/* Send a high-priority XON/XOFF character
1711 */
1712static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1713{
1714 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1715 unsigned long flags;
1716
1717 if (debug_level >= DEBUG_LEVEL_INFO)
1718 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1719 __FILE__,__LINE__, info->device_name, ch );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1722 return;
1723
1724 info->x_char = ch;
1725 if (ch) {
1726 spin_lock_irqsave(&info->lock,flags);
1727 if (!info->tx_enabled)
Alan Coxeeb46132009-01-02 13:48:47 +00001728 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 spin_unlock_irqrestore(&info->lock,flags);
1730 }
1731}
1732
1733/* Signal remote device to throttle send data (our receive data)
1734 */
1735static void mgslpc_throttle(struct tty_struct * tty)
1736{
1737 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1738 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (debug_level >= DEBUG_LEVEL_INFO)
1741 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1742 __FILE__,__LINE__, info->device_name );
1743
1744 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1745 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (I_IXOFF(tty))
1748 mgslpc_send_xchar(tty, STOP_CHAR(tty));
Jeff Garzikd12341f2007-10-19 15:45:35 -04001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (tty->termios->c_cflag & CRTSCTS) {
1751 spin_lock_irqsave(&info->lock,flags);
1752 info->serial_signals &= ~SerialSignal_RTS;
1753 set_signals(info);
1754 spin_unlock_irqrestore(&info->lock,flags);
1755 }
1756}
1757
1758/* Signal remote device to stop throttling send data (our receive data)
1759 */
1760static void mgslpc_unthrottle(struct tty_struct * tty)
1761{
1762 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1763 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (debug_level >= DEBUG_LEVEL_INFO)
1766 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1767 __FILE__,__LINE__, info->device_name );
1768
1769 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1770 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (I_IXOFF(tty)) {
1773 if (info->x_char)
1774 info->x_char = 0;
1775 else
1776 mgslpc_send_xchar(tty, START_CHAR(tty));
1777 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (tty->termios->c_cflag & CRTSCTS) {
1780 spin_lock_irqsave(&info->lock,flags);
1781 info->serial_signals |= SerialSignal_RTS;
1782 set_signals(info);
1783 spin_unlock_irqrestore(&info->lock,flags);
1784 }
1785}
1786
1787/* get the current serial statistics
1788 */
1789static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1790{
1791 int err;
1792 if (debug_level >= DEBUG_LEVEL_INFO)
1793 printk("get_params(%s)\n", info->device_name);
Paul Fulghuma7482a22005-09-10 00:26:07 -07001794 if (!user_icount) {
1795 memset(&info->icount, 0, sizeof(info->icount));
1796 } else {
1797 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1798 if (err)
1799 return -EFAULT;
1800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return 0;
1802}
1803
1804/* get the current serial parameters
1805 */
1806static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1807{
1808 int err;
1809 if (debug_level >= DEBUG_LEVEL_INFO)
1810 printk("get_params(%s)\n", info->device_name);
1811 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1812 if (err)
1813 return -EFAULT;
1814 return 0;
1815}
1816
1817/* set the serial parameters
Jeff Garzikd12341f2007-10-19 15:45:35 -04001818 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001820 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 * info pointer to device instance data
1822 * new_params user buffer containing new serial params
1823 *
1824 * Returns: 0 if success, otherwise error code
1825 */
Alan Coxeeb46132009-01-02 13:48:47 +00001826static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 unsigned long flags;
1829 MGSL_PARAMS tmp_params;
1830 int err;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (debug_level >= DEBUG_LEVEL_INFO)
1833 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1834 info->device_name );
1835 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1836 if (err) {
1837 if ( debug_level >= DEBUG_LEVEL_INFO )
1838 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1839 __FILE__,__LINE__,info->device_name);
1840 return -EFAULT;
1841 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 spin_lock_irqsave(&info->lock,flags);
1844 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1845 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001846
Alan Coxeeb46132009-01-02 13:48:47 +00001847 mgslpc_change_params(info, tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return 0;
1850}
1851
1852static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1853{
1854 int err;
1855 if (debug_level >= DEBUG_LEVEL_INFO)
1856 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1857 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1858 if (err)
1859 return -EFAULT;
1860 return 0;
1861}
1862
1863static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1864{
1865 unsigned long flags;
1866 if (debug_level >= DEBUG_LEVEL_INFO)
1867 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1868 spin_lock_irqsave(&info->lock,flags);
1869 info->idle_mode = idle_mode;
1870 tx_set_idle(info);
1871 spin_unlock_irqrestore(&info->lock,flags);
1872 return 0;
1873}
1874
1875static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1876{
1877 int err;
1878 if (debug_level >= DEBUG_LEVEL_INFO)
1879 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1880 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1881 if (err)
1882 return -EFAULT;
1883 return 0;
1884}
1885
1886static int set_interface(MGSLPC_INFO * info, int if_mode)
1887{
1888 unsigned long flags;
1889 unsigned char val;
1890 if (debug_level >= DEBUG_LEVEL_INFO)
1891 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1892 spin_lock_irqsave(&info->lock,flags);
1893 info->if_mode = if_mode;
1894
1895 val = read_reg(info, PVR) & 0x0f;
1896 switch (info->if_mode)
1897 {
1898 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
1899 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
1900 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
1901 }
1902 write_reg(info, PVR, val);
1903
1904 spin_unlock_irqrestore(&info->lock,flags);
1905 return 0;
1906}
1907
Alan Coxeeb46132009-01-02 13:48:47 +00001908static int set_txenable(MGSLPC_INFO * info, int enable, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (debug_level >= DEBUG_LEVEL_INFO)
1913 printk("set_txenable(%s,%d)\n", info->device_name, enable);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 spin_lock_irqsave(&info->lock,flags);
1916 if (enable) {
1917 if (!info->tx_enabled)
Alan Coxeeb46132009-01-02 13:48:47 +00001918 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 } else {
1920 if (info->tx_enabled)
1921 tx_stop(info);
1922 }
1923 spin_unlock_irqrestore(&info->lock,flags);
1924 return 0;
1925}
1926
1927static int tx_abort(MGSLPC_INFO * info)
1928{
1929 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (debug_level >= DEBUG_LEVEL_INFO)
1932 printk("tx_abort(%s)\n", info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 spin_lock_irqsave(&info->lock,flags);
1935 if (info->tx_active && info->tx_count &&
1936 info->params.mode == MGSL_MODE_HDLC) {
1937 /* clear data count so FIFO is not filled on next IRQ.
1938 * This results in underrun and abort transmission.
1939 */
1940 info->tx_count = info->tx_put = info->tx_get = 0;
Joe Perches0fab6de2008-04-28 02:14:02 -07001941 info->tx_aborting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943 spin_unlock_irqrestore(&info->lock,flags);
1944 return 0;
1945}
1946
1947static int set_rxenable(MGSLPC_INFO * info, int enable)
1948{
1949 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (debug_level >= DEBUG_LEVEL_INFO)
1952 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 spin_lock_irqsave(&info->lock,flags);
1955 if (enable) {
1956 if (!info->rx_enabled)
1957 rx_start(info);
1958 } else {
1959 if (info->rx_enabled)
1960 rx_stop(info);
1961 }
1962 spin_unlock_irqrestore(&info->lock,flags);
1963 return 0;
1964}
1965
1966/* wait for specified event to occur
Jeff Garzikd12341f2007-10-19 15:45:35 -04001967 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 * Arguments: info pointer to device instance data
1969 * mask pointer to bitmask of events to wait for
1970 * Return Value: 0 if successful and bit mask updated with
1971 * of events triggerred,
1972 * otherwise error code
1973 */
1974static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
1975{
1976 unsigned long flags;
1977 int s;
1978 int rc=0;
1979 struct mgsl_icount cprev, cnow;
1980 int events;
1981 int mask;
1982 struct _input_signal_events oldsigs, newsigs;
1983 DECLARE_WAITQUEUE(wait, current);
1984
1985 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
1986 if (rc)
1987 return -EFAULT;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (debug_level >= DEBUG_LEVEL_INFO)
1990 printk("wait_events(%s,%d)\n", info->device_name, mask);
1991
1992 spin_lock_irqsave(&info->lock,flags);
1993
1994 /* return immediately if state matches requested events */
1995 get_signals(info);
1996 s = info->serial_signals;
1997 events = mask &
1998 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
1999 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2000 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2001 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2002 if (events) {
2003 spin_unlock_irqrestore(&info->lock,flags);
2004 goto exit;
2005 }
2006
2007 /* save current irq counts */
2008 cprev = info->icount;
2009 oldsigs = info->input_signal_events;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if ((info->params.mode == MGSL_MODE_HDLC) &&
2012 (mask & MgslEvent_ExitHuntMode))
2013 irq_enable(info, CHA, IRQ_EXITHUNT);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 set_current_state(TASK_INTERRUPTIBLE);
2016 add_wait_queue(&info->event_wait_q, &wait);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002019
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 for(;;) {
2022 schedule();
2023 if (signal_pending(current)) {
2024 rc = -ERESTARTSYS;
2025 break;
2026 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 /* get current irq counts */
2029 spin_lock_irqsave(&info->lock,flags);
2030 cnow = info->icount;
2031 newsigs = info->input_signal_events;
2032 set_current_state(TASK_INTERRUPTIBLE);
2033 spin_unlock_irqrestore(&info->lock,flags);
2034
2035 /* if no change, wait aborted for some reason */
2036 if (newsigs.dsr_up == oldsigs.dsr_up &&
2037 newsigs.dsr_down == oldsigs.dsr_down &&
2038 newsigs.dcd_up == oldsigs.dcd_up &&
2039 newsigs.dcd_down == oldsigs.dcd_down &&
2040 newsigs.cts_up == oldsigs.cts_up &&
2041 newsigs.cts_down == oldsigs.cts_down &&
2042 newsigs.ri_up == oldsigs.ri_up &&
2043 newsigs.ri_down == oldsigs.ri_down &&
2044 cnow.exithunt == cprev.exithunt &&
2045 cnow.rxidle == cprev.rxidle) {
2046 rc = -EIO;
2047 break;
2048 }
2049
2050 events = mask &
2051 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2052 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2053 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2054 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2055 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2056 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2057 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2058 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2059 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2060 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2061 if (events)
2062 break;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 cprev = cnow;
2065 oldsigs = newsigs;
2066 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 remove_wait_queue(&info->event_wait_q, &wait);
2069 set_current_state(TASK_RUNNING);
2070
2071 if (mask & MgslEvent_ExitHuntMode) {
2072 spin_lock_irqsave(&info->lock,flags);
2073 if (!waitqueue_active(&info->event_wait_q))
2074 irq_disable(info, CHA, IRQ_EXITHUNT);
2075 spin_unlock_irqrestore(&info->lock,flags);
2076 }
2077exit:
2078 if (rc == 0)
2079 PUT_USER(rc, events, mask_ptr);
2080 return rc;
2081}
2082
2083static int modem_input_wait(MGSLPC_INFO *info,int arg)
2084{
2085 unsigned long flags;
2086 int rc;
2087 struct mgsl_icount cprev, cnow;
2088 DECLARE_WAITQUEUE(wait, current);
2089
2090 /* save current irq counts */
2091 spin_lock_irqsave(&info->lock,flags);
2092 cprev = info->icount;
2093 add_wait_queue(&info->status_event_wait_q, &wait);
2094 set_current_state(TASK_INTERRUPTIBLE);
2095 spin_unlock_irqrestore(&info->lock,flags);
2096
2097 for(;;) {
2098 schedule();
2099 if (signal_pending(current)) {
2100 rc = -ERESTARTSYS;
2101 break;
2102 }
2103
2104 /* get new irq counts */
2105 spin_lock_irqsave(&info->lock,flags);
2106 cnow = info->icount;
2107 set_current_state(TASK_INTERRUPTIBLE);
2108 spin_unlock_irqrestore(&info->lock,flags);
2109
2110 /* if no change, wait aborted for some reason */
2111 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2112 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2113 rc = -EIO;
2114 break;
2115 }
2116
2117 /* check for change in caller specified modem input */
2118 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2119 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2120 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2121 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2122 rc = 0;
2123 break;
2124 }
2125
2126 cprev = cnow;
2127 }
2128 remove_wait_queue(&info->status_event_wait_q, &wait);
2129 set_current_state(TASK_RUNNING);
2130 return rc;
2131}
2132
2133/* return the state of the serial control and status signals
2134 */
2135static int tiocmget(struct tty_struct *tty, struct file *file)
2136{
2137 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2138 unsigned int result;
2139 unsigned long flags;
2140
2141 spin_lock_irqsave(&info->lock,flags);
2142 get_signals(info);
2143 spin_unlock_irqrestore(&info->lock,flags);
2144
2145 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2146 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2147 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2148 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2149 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2150 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2151
2152 if (debug_level >= DEBUG_LEVEL_INFO)
2153 printk("%s(%d):%s tiocmget() value=%08X\n",
2154 __FILE__,__LINE__, info->device_name, result );
2155 return result;
2156}
2157
2158/* set modem control signals (DTR/RTS)
2159 */
2160static int tiocmset(struct tty_struct *tty, struct file *file,
2161 unsigned int set, unsigned int clear)
2162{
2163 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2164 unsigned long flags;
2165
2166 if (debug_level >= DEBUG_LEVEL_INFO)
2167 printk("%s(%d):%s tiocmset(%x,%x)\n",
2168 __FILE__,__LINE__,info->device_name, set, clear);
2169
2170 if (set & TIOCM_RTS)
2171 info->serial_signals |= SerialSignal_RTS;
2172 if (set & TIOCM_DTR)
2173 info->serial_signals |= SerialSignal_DTR;
2174 if (clear & TIOCM_RTS)
2175 info->serial_signals &= ~SerialSignal_RTS;
2176 if (clear & TIOCM_DTR)
2177 info->serial_signals &= ~SerialSignal_DTR;
2178
2179 spin_lock_irqsave(&info->lock,flags);
2180 set_signals(info);
2181 spin_unlock_irqrestore(&info->lock,flags);
2182
2183 return 0;
2184}
2185
2186/* Set or clear transmit break condition
2187 *
2188 * Arguments: tty pointer to tty instance data
2189 * break_state -1=set break condition, 0=clear
2190 */
Alan Cox9e989662008-07-22 11:18:03 +01002191static int mgslpc_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2194 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if (debug_level >= DEBUG_LEVEL_INFO)
2197 printk("%s(%d):mgslpc_break(%s,%d)\n",
2198 __FILE__,__LINE__, info->device_name, break_state);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
Alan Cox9e989662008-07-22 11:18:03 +01002201 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 spin_lock_irqsave(&info->lock,flags);
2204 if (break_state == -1)
2205 set_reg_bits(info, CHA+DAFO, BIT6);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002206 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 clear_reg_bits(info, CHA+DAFO, BIT6);
2208 spin_unlock_irqrestore(&info->lock,flags);
Alan Cox9e989662008-07-22 11:18:03 +01002209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212/* Service an IOCTL request
Jeff Garzikd12341f2007-10-19 15:45:35 -04002213 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04002215 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 * tty pointer to tty instance data
2217 * file pointer to associated file object for device
2218 * cmd IOCTL command code
2219 * arg command argument/context
Jeff Garzikd12341f2007-10-19 15:45:35 -04002220 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 * Return Value: 0 if success, otherwise error code
2222 */
2223static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2224 unsigned int cmd, unsigned long arg)
2225{
2226 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
Alan Coxeeb46132009-01-02 13:48:47 +00002227 int error;
2228 struct mgsl_icount cnow; /* kernel counter temps */
2229 struct serial_icounter_struct __user *p_cuser; /* user space */
2230 void __user *argp = (void __user *)arg;
2231 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (debug_level >= DEBUG_LEVEL_INFO)
2234 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2235 info->device_name, cmd );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2238 return -ENODEV;
2239
2240 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2241 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2242 if (tty->flags & (1 << TTY_IO_ERROR))
2243 return -EIO;
2244 }
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 switch (cmd) {
2247 case MGSL_IOCGPARAMS:
2248 return get_params(info, argp);
2249 case MGSL_IOCSPARAMS:
Alan Coxeeb46132009-01-02 13:48:47 +00002250 return set_params(info, argp, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 case MGSL_IOCGTXIDLE:
2252 return get_txidle(info, argp);
2253 case MGSL_IOCSTXIDLE:
2254 return set_txidle(info, (int)arg);
2255 case MGSL_IOCGIF:
2256 return get_interface(info, argp);
2257 case MGSL_IOCSIF:
2258 return set_interface(info,(int)arg);
2259 case MGSL_IOCTXENABLE:
Alan Coxeeb46132009-01-02 13:48:47 +00002260 return set_txenable(info,(int)arg, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 case MGSL_IOCRXENABLE:
2262 return set_rxenable(info,(int)arg);
2263 case MGSL_IOCTXABORT:
2264 return tx_abort(info);
2265 case MGSL_IOCGSTATS:
2266 return get_stats(info, argp);
2267 case MGSL_IOCWAITEVENT:
2268 return wait_events(info, argp);
2269 case TIOCMIWAIT:
2270 return modem_input_wait(info,(int)arg);
2271 case TIOCGICOUNT:
2272 spin_lock_irqsave(&info->lock,flags);
2273 cnow = info->icount;
2274 spin_unlock_irqrestore(&info->lock,flags);
2275 p_cuser = argp;
2276 PUT_USER(error,cnow.cts, &p_cuser->cts);
2277 if (error) return error;
2278 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2279 if (error) return error;
2280 PUT_USER(error,cnow.rng, &p_cuser->rng);
2281 if (error) return error;
2282 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2283 if (error) return error;
2284 PUT_USER(error,cnow.rx, &p_cuser->rx);
2285 if (error) return error;
2286 PUT_USER(error,cnow.tx, &p_cuser->tx);
2287 if (error) return error;
2288 PUT_USER(error,cnow.frame, &p_cuser->frame);
2289 if (error) return error;
2290 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2291 if (error) return error;
2292 PUT_USER(error,cnow.parity, &p_cuser->parity);
2293 if (error) return error;
2294 PUT_USER(error,cnow.brk, &p_cuser->brk);
2295 if (error) return error;
2296 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2297 if (error) return error;
2298 return 0;
2299 default:
2300 return -ENOIOCTLCMD;
2301 }
2302 return 0;
2303}
2304
2305/* Set new termios settings
Jeff Garzikd12341f2007-10-19 15:45:35 -04002306 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04002308 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 * tty pointer to tty structure
2310 * termios pointer to buffer to hold returned old termios
2311 */
Alan Cox606d0992006-12-08 02:38:45 -08002312static void mgslpc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2315 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (debug_level >= DEBUG_LEVEL_INFO)
2318 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2319 tty->driver->name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 /* just return if nothing has changed */
2322 if ((tty->termios->c_cflag == old_termios->c_cflag)
Jeff Garzikd12341f2007-10-19 15:45:35 -04002323 && (RELEVANT_IFLAG(tty->termios->c_iflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 == RELEVANT_IFLAG(old_termios->c_iflag)))
2325 return;
2326
Alan Coxeeb46132009-01-02 13:48:47 +00002327 mgslpc_change_params(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 /* Handle transition to B0 status */
2330 if (old_termios->c_cflag & CBAUD &&
2331 !(tty->termios->c_cflag & CBAUD)) {
2332 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2333 spin_lock_irqsave(&info->lock,flags);
2334 set_signals(info);
2335 spin_unlock_irqrestore(&info->lock,flags);
2336 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /* Handle transition away from B0 status */
2339 if (!(old_termios->c_cflag & CBAUD) &&
2340 tty->termios->c_cflag & CBAUD) {
2341 info->serial_signals |= SerialSignal_DTR;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002342 if (!(tty->termios->c_cflag & CRTSCTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 !test_bit(TTY_THROTTLED, &tty->flags)) {
2344 info->serial_signals |= SerialSignal_RTS;
2345 }
2346 spin_lock_irqsave(&info->lock,flags);
2347 set_signals(info);
2348 spin_unlock_irqrestore(&info->lock,flags);
2349 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 /* Handle turning off CRTSCTS */
2352 if (old_termios->c_cflag & CRTSCTS &&
2353 !(tty->termios->c_cflag & CRTSCTS)) {
2354 tty->hw_stopped = 0;
2355 tx_release(tty);
2356 }
2357}
2358
2359static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2360{
2361 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
Alan Coxeeb46132009-01-02 13:48:47 +00002362 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2365 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (debug_level >= DEBUG_LEVEL_INFO)
2368 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
Alan Coxeeb46132009-01-02 13:48:47 +00002369 __FILE__,__LINE__, info->device_name, port->count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002370
Alan Coxeeb46132009-01-02 13:48:47 +00002371 WARN_ON(!port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Alan Coxeeb46132009-01-02 13:48:47 +00002373 if (tty_port_close_start(port, tty, filp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 goto cleanup;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002375
Alan Coxeeb46132009-01-02 13:48:47 +00002376 if (port->flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 mgslpc_wait_until_sent(tty, info->timeout);
2378
Alan Cox978e5952008-04-30 00:53:59 -07002379 mgslpc_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Alan Cox978e5952008-04-30 00:53:59 -07002381 tty_ldisc_flush(tty);
Alan Coxeeb46132009-01-02 13:48:47 +00002382 shutdown(info, tty);
2383
2384 tty_port_close_end(port, tty);
2385 tty_port_tty_set(port, NULL);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002386cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (debug_level >= DEBUG_LEVEL_INFO)
2388 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
Alan Coxeeb46132009-01-02 13:48:47 +00002389 tty->driver->name, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
2392/* Wait until the transmitter is empty.
2393 */
2394static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2395{
2396 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2397 unsigned long orig_jiffies, char_time;
2398
2399 if (!info )
2400 return;
2401
2402 if (debug_level >= DEBUG_LEVEL_INFO)
2403 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2404 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2407 return;
2408
Alan Coxeeb46132009-01-02 13:48:47 +00002409 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 goto exit;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 orig_jiffies = jiffies;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 /* Set check interval to 1/5 of estimated time to
2415 * send a character, and make it at least 1. The check
2416 * interval should also be less than the timeout.
2417 * Note: use tight timings here to satisfy the NIST-PCTS.
Jeff Garzikd12341f2007-10-19 15:45:35 -04002418 */
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if ( info->params.data_rate ) {
2421 char_time = info->timeout/(32 * 5);
2422 if (!char_time)
2423 char_time++;
2424 } else
2425 char_time = 1;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (timeout)
2428 char_time = min_t(unsigned long, char_time, timeout);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (info->params.mode == MGSL_MODE_HDLC) {
2431 while (info->tx_active) {
2432 msleep_interruptible(jiffies_to_msecs(char_time));
2433 if (signal_pending(current))
2434 break;
2435 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2436 break;
2437 }
2438 } else {
2439 while ((info->tx_count || info->tx_active) &&
2440 info->tx_enabled) {
2441 msleep_interruptible(jiffies_to_msecs(char_time));
2442 if (signal_pending(current))
2443 break;
2444 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2445 break;
2446 }
2447 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449exit:
2450 if (debug_level >= DEBUG_LEVEL_INFO)
2451 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2452 __FILE__,__LINE__, info->device_name );
2453}
2454
2455/* Called by tty_hangup() when a hangup is signaled.
2456 * This is the same as closing all open files for the port.
2457 */
2458static void mgslpc_hangup(struct tty_struct *tty)
2459{
2460 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (debug_level >= DEBUG_LEVEL_INFO)
2463 printk("%s(%d):mgslpc_hangup(%s)\n",
2464 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2467 return;
2468
2469 mgslpc_flush_buffer(tty);
Alan Coxeeb46132009-01-02 13:48:47 +00002470 shutdown(info, tty);
2471 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
Alan Coxeeb46132009-01-02 13:48:47 +00002474static int carrier_raised(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Alan Coxeeb46132009-01-02 13:48:47 +00002476 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2477 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002478
Alan Coxeeb46132009-01-02 13:48:47 +00002479 spin_lock_irqsave(&info->lock,flags);
2480 get_signals(info);
2481 spin_unlock_irqrestore(&info->lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Alan Coxeeb46132009-01-02 13:48:47 +00002483 if (info->serial_signals & SerialSignal_DCD)
2484 return 1;
2485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487
Alan Coxfcc8ac12009-06-11 12:24:17 +01002488static void dtr_rts(struct tty_port *port, int onoff)
Alan Coxeeb46132009-01-02 13:48:47 +00002489{
2490 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2491 unsigned long flags;
2492
2493 spin_lock_irqsave(&info->lock,flags);
Alan Coxfcc8ac12009-06-11 12:24:17 +01002494 if (onoff)
2495 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2496 else
2497 info->serial_signals &= ~SerialSignal_RTS + SerialSignal_DTR;
Alan Coxeeb46132009-01-02 13:48:47 +00002498 set_signals(info);
2499 spin_unlock_irqrestore(&info->lock,flags);
2500}
2501
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2504{
2505 MGSLPC_INFO *info;
Alan Coxeeb46132009-01-02 13:48:47 +00002506 struct tty_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 int retval, line;
2508 unsigned long flags;
2509
Jeff Garzikd12341f2007-10-19 15:45:35 -04002510 /* verify range of specified line number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 line = tty->index;
2512 if ((line < 0) || (line >= mgslpc_device_count)) {
2513 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2514 __FILE__,__LINE__,line);
2515 return -ENODEV;
2516 }
2517
2518 /* find the info structure for the specified line */
2519 info = mgslpc_device_list;
2520 while(info && info->line != line)
2521 info = info->next_device;
2522 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2523 return -ENODEV;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002524
Alan Coxeeb46132009-01-02 13:48:47 +00002525 port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 tty->driver_data = info;
Alan Coxeeb46132009-01-02 13:48:47 +00002527 tty_port_tty_set(port, tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 if (debug_level >= DEBUG_LEVEL_INFO)
2530 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
Alan Coxeeb46132009-01-02 13:48:47 +00002531 __FILE__,__LINE__,tty->driver->name, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 /* If port is closing, signal caller to try again */
Alan Coxeeb46132009-01-02 13:48:47 +00002534 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING){
2535 if (port->flags & ASYNC_CLOSING)
2536 interruptible_sleep_on(&port->close_wait);
2537 retval = ((port->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 -EAGAIN : -ERESTARTSYS);
2539 goto cleanup;
2540 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002541
Alan Coxeeb46132009-01-02 13:48:47 +00002542 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 spin_lock_irqsave(&info->netlock, flags);
2545 if (info->netcount) {
2546 retval = -EBUSY;
2547 spin_unlock_irqrestore(&info->netlock, flags);
2548 goto cleanup;
2549 }
Alan Coxeeb46132009-01-02 13:48:47 +00002550 spin_lock(&port->lock);
2551 port->count++;
2552 spin_unlock(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 spin_unlock_irqrestore(&info->netlock, flags);
2554
Alan Coxeeb46132009-01-02 13:48:47 +00002555 if (port->count == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 /* 1st open on this device, init hardware */
Alan Coxeeb46132009-01-02 13:48:47 +00002557 retval = startup(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (retval < 0)
2559 goto cleanup;
2560 }
2561
Alan Coxeeb46132009-01-02 13:48:47 +00002562 retval = tty_port_block_til_ready(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (retval) {
2564 if (debug_level >= DEBUG_LEVEL_INFO)
2565 printk("%s(%d):block_til_ready(%s) returned %d\n",
2566 __FILE__,__LINE__, info->device_name, retval);
2567 goto cleanup;
2568 }
2569
2570 if (debug_level >= DEBUG_LEVEL_INFO)
2571 printk("%s(%d):mgslpc_open(%s) success\n",
2572 __FILE__,__LINE__, info->device_name);
2573 retval = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002574
2575cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return retval;
2577}
2578
2579/*
2580 * /proc fs routines....
2581 */
2582
Alexey Dobriyan87687142009-03-31 15:19:17 -07002583static inline void line_info(struct seq_file *m, MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
2585 char stat_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 unsigned long flags;
2587
Alexey Dobriyan87687142009-03-31 15:19:17 -07002588 seq_printf(m, "%s:io:%04X irq:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 info->device_name, info->io_base, info->irq_level);
2590
2591 /* output current serial signal states */
2592 spin_lock_irqsave(&info->lock,flags);
2593 get_signals(info);
2594 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 stat_buf[0] = 0;
2597 stat_buf[1] = 0;
2598 if (info->serial_signals & SerialSignal_RTS)
2599 strcat(stat_buf, "|RTS");
2600 if (info->serial_signals & SerialSignal_CTS)
2601 strcat(stat_buf, "|CTS");
2602 if (info->serial_signals & SerialSignal_DTR)
2603 strcat(stat_buf, "|DTR");
2604 if (info->serial_signals & SerialSignal_DSR)
2605 strcat(stat_buf, "|DSR");
2606 if (info->serial_signals & SerialSignal_DCD)
2607 strcat(stat_buf, "|CD");
2608 if (info->serial_signals & SerialSignal_RI)
2609 strcat(stat_buf, "|RI");
2610
2611 if (info->params.mode == MGSL_MODE_HDLC) {
Alexey Dobriyan87687142009-03-31 15:19:17 -07002612 seq_printf(m, " HDLC txok:%d rxok:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 info->icount.txok, info->icount.rxok);
2614 if (info->icount.txunder)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002615 seq_printf(m, " txunder:%d", info->icount.txunder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 if (info->icount.txabort)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002617 seq_printf(m, " txabort:%d", info->icount.txabort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 if (info->icount.rxshort)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002619 seq_printf(m, " rxshort:%d", info->icount.rxshort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (info->icount.rxlong)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002621 seq_printf(m, " rxlong:%d", info->icount.rxlong);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (info->icount.rxover)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002623 seq_printf(m, " rxover:%d", info->icount.rxover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (info->icount.rxcrc)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002625 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 } else {
Alexey Dobriyan87687142009-03-31 15:19:17 -07002627 seq_printf(m, " ASYNC tx:%d rx:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 info->icount.tx, info->icount.rx);
2629 if (info->icount.frame)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002630 seq_printf(m, " fe:%d", info->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 if (info->icount.parity)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002632 seq_printf(m, " pe:%d", info->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (info->icount.brk)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002634 seq_printf(m, " brk:%d", info->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (info->icount.overrun)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002636 seq_printf(m, " oe:%d", info->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 /* Append serial signal status to end */
Alexey Dobriyan87687142009-03-31 15:19:17 -07002640 seq_printf(m, " %s\n", stat_buf+1);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002641
Alexey Dobriyan87687142009-03-31 15:19:17 -07002642 seq_printf(m, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 info->tx_active,info->bh_requested,info->bh_running,
2644 info->pending_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
2646
2647/* Called to print information about devices
2648 */
Alexey Dobriyan87687142009-03-31 15:19:17 -07002649static int mgslpc_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 MGSLPC_INFO *info;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002652
Alexey Dobriyan87687142009-03-31 15:19:17 -07002653 seq_printf(m, "synclink driver:%s\n", driver_version);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 info = mgslpc_device_list;
2656 while( info ) {
Alexey Dobriyan87687142009-03-31 15:19:17 -07002657 line_info(m, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 info = info->next_device;
2659 }
Alexey Dobriyan87687142009-03-31 15:19:17 -07002660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
Alexey Dobriyan87687142009-03-31 15:19:17 -07002663static int mgslpc_proc_open(struct inode *inode, struct file *file)
2664{
2665 return single_open(file, mgslpc_proc_show, NULL);
2666}
2667
2668static const struct file_operations mgslpc_proc_fops = {
2669 .owner = THIS_MODULE,
2670 .open = mgslpc_proc_open,
2671 .read = seq_read,
2672 .llseek = seq_lseek,
2673 .release = single_release,
2674};
2675
Peter Hagervallcdaad342006-06-23 02:06:04 -07002676static int rx_alloc_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
2678 /* each buffer has header and data */
2679 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2680
2681 /* calculate total allocation size for 8 buffers */
2682 info->rx_buf_total_size = info->rx_buf_size * 8;
2683
2684 /* limit total allocated memory */
2685 if (info->rx_buf_total_size > 0x10000)
2686 info->rx_buf_total_size = 0x10000;
2687
2688 /* calculate number of buffers */
2689 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2690
2691 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2692 if (info->rx_buf == NULL)
2693 return -ENOMEM;
2694
2695 rx_reset_buffers(info);
2696 return 0;
2697}
2698
Peter Hagervallcdaad342006-06-23 02:06:04 -07002699static void rx_free_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Jesper Juhl735d5662005-11-07 01:01:29 -08002701 kfree(info->rx_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 info->rx_buf = NULL;
2703}
2704
Peter Hagervallcdaad342006-06-23 02:06:04 -07002705static int claim_resources(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
2707 if (rx_alloc_buffers(info) < 0 ) {
2708 printk( "Cant allocate rx buffer %s\n", info->device_name);
2709 release_resources(info);
2710 return -ENODEV;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return 0;
2713}
2714
Peter Hagervallcdaad342006-06-23 02:06:04 -07002715static void release_resources(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
2717 if (debug_level >= DEBUG_LEVEL_INFO)
2718 printk("release_resources(%s)\n", info->device_name);
2719 rx_free_buffers(info);
2720}
2721
2722/* Add the specified device instance data structure to the
2723 * global linked list of devices and increment the device count.
Jeff Garzikd12341f2007-10-19 15:45:35 -04002724 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 * Arguments: info pointer to device instance data
2726 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07002727static void mgslpc_add_device(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
2729 info->next_device = NULL;
2730 info->line = mgslpc_device_count;
2731 sprintf(info->device_name,"ttySLP%d",info->line);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 if (info->line < MAX_DEVICE_COUNT) {
2734 if (maxframe[info->line])
2735 info->max_frame_size = maxframe[info->line];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 }
2737
2738 mgslpc_device_count++;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 if (!mgslpc_device_list)
2741 mgslpc_device_list = info;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002742 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 MGSLPC_INFO *current_dev = mgslpc_device_list;
2744 while( current_dev->next_device )
2745 current_dev = current_dev->next_device;
2746 current_dev->next_device = info;
2747 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (info->max_frame_size < 4096)
2750 info->max_frame_size = 4096;
2751 else if (info->max_frame_size > 65535)
2752 info->max_frame_size = 65535;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2755 info->device_name, info->io_base, info->irq_level);
2756
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002757#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 hdlcdev_init(info);
2759#endif
2760}
2761
Peter Hagervallcdaad342006-06-23 02:06:04 -07002762static void mgslpc_remove_device(MGSLPC_INFO *remove_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
2764 MGSLPC_INFO *info = mgslpc_device_list;
2765 MGSLPC_INFO *last = NULL;
2766
2767 while(info) {
2768 if (info == remove_info) {
2769 if (last)
2770 last->next_device = info->next_device;
2771 else
2772 mgslpc_device_list = info->next_device;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002773#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 hdlcdev_exit(info);
2775#endif
2776 release_resources(info);
2777 kfree(info);
2778 mgslpc_device_count--;
2779 return;
2780 }
2781 last = info;
2782 info = info->next_device;
2783 }
2784}
2785
Dominik Brodowski4af48c82005-06-27 16:28:42 -07002786static struct pcmcia_device_id mgslpc_ids[] = {
2787 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
2788 PCMCIA_DEVICE_NULL
2789};
2790MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792static struct pcmcia_driver mgslpc_driver = {
2793 .owner = THIS_MODULE,
2794 .drv = {
2795 .name = "synclink_cs",
2796 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02002797 .probe = mgslpc_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01002798 .remove = mgslpc_detach,
Dominik Brodowski4af48c82005-06-27 16:28:42 -07002799 .id_table = mgslpc_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +01002800 .suspend = mgslpc_suspend,
2801 .resume = mgslpc_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802};
2803
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002804static const struct tty_operations mgslpc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 .open = mgslpc_open,
2806 .close = mgslpc_close,
2807 .write = mgslpc_write,
2808 .put_char = mgslpc_put_char,
2809 .flush_chars = mgslpc_flush_chars,
2810 .write_room = mgslpc_write_room,
2811 .chars_in_buffer = mgslpc_chars_in_buffer,
2812 .flush_buffer = mgslpc_flush_buffer,
2813 .ioctl = mgslpc_ioctl,
2814 .throttle = mgslpc_throttle,
2815 .unthrottle = mgslpc_unthrottle,
2816 .send_xchar = mgslpc_send_xchar,
2817 .break_ctl = mgslpc_break,
2818 .wait_until_sent = mgslpc_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 .set_termios = mgslpc_set_termios,
2820 .stop = tx_pause,
2821 .start = tx_release,
2822 .hangup = mgslpc_hangup,
2823 .tiocmget = tiocmget,
2824 .tiocmset = tiocmset,
Alexey Dobriyan87687142009-03-31 15:19:17 -07002825 .proc_fops = &mgslpc_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826};
2827
2828static void synclink_cs_cleanup(void)
2829{
2830 int rc;
2831
2832 printk("Unloading %s: version %s\n", driver_name, driver_version);
2833
2834 while(mgslpc_device_list)
2835 mgslpc_remove_device(mgslpc_device_list);
2836
2837 if (serial_driver) {
2838 if ((rc = tty_unregister_driver(serial_driver)))
2839 printk("%s(%d) failed to unregister tty driver err=%d\n",
2840 __FILE__,__LINE__,rc);
2841 put_tty_driver(serial_driver);
2842 }
2843
2844 pcmcia_unregister_driver(&mgslpc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846
2847static int __init synclink_cs_init(void)
2848{
2849 int rc;
2850
2851 if (break_on_load) {
2852 mgslpc_get_text_ptr();
2853 BREAKPOINT();
2854 }
2855
2856 printk("%s %s\n", driver_name, driver_version);
2857
2858 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
2859 return rc;
2860
2861 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
2862 if (!serial_driver) {
2863 rc = -ENOMEM;
2864 goto error;
2865 }
2866
2867 /* Initialize the tty_driver structure */
Jeff Garzikd12341f2007-10-19 15:45:35 -04002868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 serial_driver->owner = THIS_MODULE;
2870 serial_driver->driver_name = "synclink_cs";
2871 serial_driver->name = "ttySLP";
2872 serial_driver->major = ttymajor;
2873 serial_driver->minor_start = 64;
2874 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2875 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2876 serial_driver->init_termios = tty_std_termios;
2877 serial_driver->init_termios.c_cflag =
2878 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2879 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2880 tty_set_operations(serial_driver, &mgslpc_ops);
2881
2882 if ((rc = tty_register_driver(serial_driver)) < 0) {
2883 printk("%s(%d):Couldn't register serial driver\n",
2884 __FILE__,__LINE__);
2885 put_tty_driver(serial_driver);
2886 serial_driver = NULL;
2887 goto error;
2888 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 printk("%s %s, tty major#%d\n",
2891 driver_name, driver_version,
2892 serial_driver->major);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return 0;
2895
2896error:
2897 synclink_cs_cleanup();
2898 return rc;
2899}
2900
Jeff Garzikd12341f2007-10-19 15:45:35 -04002901static void __exit synclink_cs_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 synclink_cs_cleanup();
2904}
2905
2906module_init(synclink_cs_init);
2907module_exit(synclink_cs_exit);
2908
2909static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
2910{
2911 unsigned int M, N;
2912 unsigned char val;
2913
Jeff Garzikd12341f2007-10-19 15:45:35 -04002914 /* note:standard BRG mode is broken in V3.2 chip
2915 * so enhanced mode is always used
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 */
2917
2918 if (rate) {
2919 N = 3686400 / rate;
2920 if (!N)
2921 N = 1;
2922 N >>= 1;
2923 for (M = 1; N > 64 && M < 16; M++)
2924 N >>= 1;
2925 N--;
2926
2927 /* BGR[5..0] = N
2928 * BGR[9..6] = M
2929 * BGR[7..0] contained in BGR register
2930 * BGR[9..8] contained in CCR2[7..6]
2931 * divisor = (N+1)*2^M
2932 *
2933 * Note: M *must* not be zero (causes asymetric duty cycle)
Jeff Garzikd12341f2007-10-19 15:45:35 -04002934 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 write_reg(info, (unsigned char) (channel + BGR),
2936 (unsigned char) ((M << 6) + N));
2937 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
2938 val |= ((M << 4) & 0xc0);
2939 write_reg(info, (unsigned char) (channel + CCR2), val);
2940 }
2941}
2942
2943/* Enabled the AUX clock output at the specified frequency.
2944 */
2945static void enable_auxclk(MGSLPC_INFO *info)
2946{
2947 unsigned char val;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 /* MODE
2950 *
2951 * 07..06 MDS[1..0] 10 = transparent HDLC mode
2952 * 05 ADM Address Mode, 0 = no addr recognition
2953 * 04 TMD Timer Mode, 0 = external
2954 * 03 RAC Receiver Active, 0 = inactive
2955 * 02 RTS 0=RTS active during xmit, 1=RTS always active
2956 * 01 TRS Timer Resolution, 1=512
2957 * 00 TLP Test Loop, 0 = no loop
2958 *
2959 * 1000 0010
Jeff Garzikd12341f2007-10-19 15:45:35 -04002960 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 val = 0x82;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002962
2963 /* channel B RTS is used to enable AUXCLK driver on SP505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
2965 val |= BIT2;
2966 write_reg(info, CHB + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 /* CCR0
2969 *
2970 * 07 PU Power Up, 1=active, 0=power down
2971 * 06 MCE Master Clock Enable, 1=enabled
2972 * 05 Reserved, 0
2973 * 04..02 SC[2..0] Encoding
2974 * 01..00 SM[1..0] Serial Mode, 00=HDLC
2975 *
2976 * 11000000
Jeff Garzikd12341f2007-10-19 15:45:35 -04002977 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 write_reg(info, CHB + CCR0, 0xc0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 /* CCR1
2981 *
2982 * 07 SFLG Shared Flag, 0 = disable shared flags
2983 * 06 GALP Go Active On Loop, 0 = not used
2984 * 05 GLP Go On Loop, 0 = not used
2985 * 04 ODS Output Driver Select, 1=TxD is push-pull output
2986 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
2987 * 02..00 CM[2..0] Clock Mode
2988 *
2989 * 0001 0111
Jeff Garzikd12341f2007-10-19 15:45:35 -04002990 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 write_reg(info, CHB + CCR1, 0x17);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 /* CCR2 (Channel B)
2994 *
2995 * 07..06 BGR[9..8] Baud rate bits 9..8
2996 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
2997 * 04 SSEL Clock source select, 1=submode b
2998 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
2999 * 02 RWX Read/Write Exchange 0=disabled
3000 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3001 * 00 DIV, data inversion 0=disabled, 1=enabled
3002 *
3003 * 0011 1000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3006 write_reg(info, CHB + CCR2, 0x38);
3007 else
3008 write_reg(info, CHB + CCR2, 0x30);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 /* CCR4
3011 *
3012 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3013 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3014 * 05 TST1 Test Pin, 0=normal operation
3015 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3016 * 03..02 Reserved, must be 0
3017 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3018 *
3019 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 write_reg(info, CHB + CCR4, 0x50);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 /* if auxclk not enabled, set internal BRG so
3024 * CTS transitions can be detected (requires TxC)
Jeff Garzikd12341f2007-10-19 15:45:35 -04003025 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3027 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3028 else
3029 mgslpc_set_rate(info, CHB, 921600);
3030}
3031
Jeff Garzikd12341f2007-10-19 15:45:35 -04003032static void loopback_enable(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
3034 unsigned char val;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003035
3036 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3038 write_reg(info, CHA + CCR1, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003039
3040 /* CCR2:04 SSEL Clock source select, 1=submode b */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3042 write_reg(info, CHA + CCR2, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003043
3044 /* set LinkSpeed if available, otherwise default to 2Mbps */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 if (info->params.clock_speed)
3046 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3047 else
3048 mgslpc_set_rate(info, CHA, 1843200);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003049
3050 /* MODE:00 TLP Test Loop, 1=loopback enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 val = read_reg(info, CHA + MODE) | BIT0;
3052 write_reg(info, CHA + MODE, val);
3053}
3054
Peter Hagervallcdaad342006-06-23 02:06:04 -07003055static void hdlc_mode(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 unsigned char val;
3058 unsigned char clkmode, clksubmode;
3059
Jeff Garzikd12341f2007-10-19 15:45:35 -04003060 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 irq_disable(info, CHA, 0xffff);
3062 irq_disable(info, CHB, 0xffff);
3063 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003064
3065 /* assume clock mode 0a, rcv=RxC xmt=TxC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 clkmode = clksubmode = 0;
3067 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3068 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003069 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 clkmode = 7;
3071 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3072 && info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003073 /* clock mode 7b, rcv = BRG, xmt = BRG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 clkmode = 7;
3075 clksubmode = 1;
3076 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3077 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003078 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 clkmode = 6;
3080 clksubmode = 1;
3081 } else {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003082 /* clock mode 6a, rcv = DPLL, xmt = TxC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 clkmode = 6;
3084 }
3085 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003086 /* clock mode 0b, rcv = RxC, xmt = BRG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 clksubmode = 1;
3088 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04003089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 /* MODE
3091 *
3092 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3093 * 05 ADM Address Mode, 0 = no addr recognition
3094 * 04 TMD Timer Mode, 0 = external
3095 * 03 RAC Receiver Active, 0 = inactive
3096 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3097 * 01 TRS Timer Resolution, 1=512
3098 * 00 TLP Test Loop, 0 = no loop
3099 *
3100 * 1000 0010
Jeff Garzikd12341f2007-10-19 15:45:35 -04003101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 val = 0x82;
3103 if (info->params.loopback)
3104 val |= BIT0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003105
3106 /* preserve RTS state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 if (info->serial_signals & SerialSignal_RTS)
3108 val |= BIT2;
3109 write_reg(info, CHA + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 /* CCR0
3112 *
3113 * 07 PU Power Up, 1=active, 0=power down
3114 * 06 MCE Master Clock Enable, 1=enabled
3115 * 05 Reserved, 0
3116 * 04..02 SC[2..0] Encoding
3117 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3118 *
3119 * 11000000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 val = 0xc0;
3122 switch (info->params.encoding)
3123 {
3124 case HDLC_ENCODING_NRZI:
3125 val |= BIT3;
3126 break;
3127 case HDLC_ENCODING_BIPHASE_SPACE:
3128 val |= BIT4;
3129 break; // FM0
3130 case HDLC_ENCODING_BIPHASE_MARK:
3131 val |= BIT4 + BIT2;
3132 break; // FM1
3133 case HDLC_ENCODING_BIPHASE_LEVEL:
3134 val |= BIT4 + BIT3;
3135 break; // Manchester
3136 }
3137 write_reg(info, CHA + CCR0, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 /* CCR1
3140 *
3141 * 07 SFLG Shared Flag, 0 = disable shared flags
3142 * 06 GALP Go Active On Loop, 0 = not used
3143 * 05 GLP Go On Loop, 0 = not used
3144 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3145 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3146 * 02..00 CM[2..0] Clock Mode
3147 *
3148 * 0001 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 val = 0x10 + clkmode;
3151 write_reg(info, CHA + CCR1, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 /* CCR2
3154 *
3155 * 07..06 BGR[9..8] Baud rate bits 9..8
3156 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3157 * 04 SSEL Clock source select, 1=submode b
3158 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3159 * 02 RWX Read/Write Exchange 0=disabled
3160 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3161 * 00 DIV, data inversion 0=disabled, 1=enabled
3162 *
3163 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 val = 0x00;
3166 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3167 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3168 val |= BIT5;
3169 if (clksubmode)
3170 val |= BIT4;
3171 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3172 val |= BIT1;
3173 if (info->params.encoding == HDLC_ENCODING_NRZB)
3174 val |= BIT0;
3175 write_reg(info, CHA + CCR2, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 /* CCR3
3178 *
3179 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3180 * 05 EPT Enable preamble transmission, 1=enabled
3181 * 04 RADD Receive address pushed to FIFO, 0=disabled
3182 * 03 CRL CRC Reset Level, 0=FFFF
3183 * 02 RCRC Rx CRC 0=On 1=Off
3184 * 01 TCRC Tx CRC 0=On 1=Off
3185 * 00 PSD DPLL Phase Shift Disable
3186 *
3187 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 val = 0x00;
3190 if (info->params.crc_type == HDLC_CRC_NONE)
3191 val |= BIT2 + BIT1;
3192 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3193 val |= BIT5;
3194 switch (info->params.preamble_length)
3195 {
3196 case HDLC_PREAMBLE_LENGTH_16BITS:
3197 val |= BIT6;
3198 break;
3199 case HDLC_PREAMBLE_LENGTH_32BITS:
3200 val |= BIT6;
3201 break;
3202 case HDLC_PREAMBLE_LENGTH_64BITS:
3203 val |= BIT7 + BIT6;
3204 break;
3205 }
3206 write_reg(info, CHA + CCR3, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003207
3208 /* PRE - Preamble pattern */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 val = 0;
3210 switch (info->params.preamble)
3211 {
3212 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3213 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3214 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3215 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3216 }
3217 write_reg(info, CHA + PRE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 /* CCR4
3220 *
3221 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3222 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3223 * 05 TST1 Test Pin, 0=normal operation
3224 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3225 * 03..02 Reserved, must be 0
3226 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3227 *
3228 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003229 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 val = 0x50;
3231 write_reg(info, CHA + CCR4, val);
3232 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3233 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3234 else
3235 mgslpc_set_rate(info, CHA, info->params.clock_speed);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 /* RLCR Receive length check register
3238 *
3239 * 7 1=enable receive length check
3240 * 6..0 Max frame length = (RL + 1) * 32
Jeff Garzikd12341f2007-10-19 15:45:35 -04003241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 write_reg(info, CHA + RLCR, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 /* XBCH Transmit Byte Count High
3245 *
3246 * 07 DMA mode, 0 = interrupt driven
3247 * 06 NRM, 0=ABM (ignored)
3248 * 05 CAS Carrier Auto Start
3249 * 04 XC Transmit Continuously (ignored)
3250 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3251 *
3252 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 val = 0x00;
3255 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3256 val |= BIT5;
3257 write_reg(info, CHA + XBCH, val);
3258 enable_auxclk(info);
3259 if (info->params.loopback || info->testing_irq)
3260 loopback_enable(info);
3261 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3262 {
3263 irq_enable(info, CHB, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003264 /* PVR[3] 1=AUTO CTS active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 set_reg_bits(info, CHA + PVR, BIT3);
3266 } else
3267 clear_reg_bits(info, CHA + PVR, BIT3);
3268
3269 irq_enable(info, CHA,
3270 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3271 IRQ_UNDERRUN + IRQ_TXFIFO);
3272 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3273 wait_command_complete(info, CHA);
3274 read_reg16(info, CHA + ISR); /* clear pending IRQs */
Jeff Garzikd12341f2007-10-19 15:45:35 -04003275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 /* Master clock mode enabled above to allow reset commands
3277 * to complete even if no data clocks are present.
3278 *
3279 * Disable master clock mode for normal communications because
3280 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3281 * IRQ when in master clock mode.
3282 *
3283 * Leave master clock mode enabled for IRQ test because the
3284 * timer IRQ used by the test can only happen in master clock mode.
Jeff Garzikd12341f2007-10-19 15:45:35 -04003285 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 if (!info->testing_irq)
3287 clear_reg_bits(info, CHA + CCR0, BIT6);
3288
3289 tx_set_idle(info);
3290
3291 tx_stop(info);
3292 rx_stop(info);
3293}
3294
Peter Hagervallcdaad342006-06-23 02:06:04 -07003295static void rx_stop(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
3297 if (debug_level >= DEBUG_LEVEL_ISR)
3298 printk("%s(%d):rx_stop(%s)\n",
3299 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003300
3301 /* MODE:03 RAC Receiver Active, 0=inactive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 clear_reg_bits(info, CHA + MODE, BIT3);
3303
Joe Perches0fab6de2008-04-28 02:14:02 -07003304 info->rx_enabled = false;
3305 info->rx_overflow = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306}
3307
Peter Hagervallcdaad342006-06-23 02:06:04 -07003308static void rx_start(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
3310 if (debug_level >= DEBUG_LEVEL_ISR)
3311 printk("%s(%d):rx_start(%s)\n",
3312 __FILE__,__LINE__, info->device_name );
3313
3314 rx_reset_buffers(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003315 info->rx_enabled = false;
3316 info->rx_overflow = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Jeff Garzikd12341f2007-10-19 15:45:35 -04003318 /* MODE:03 RAC Receiver Active, 1=active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 set_reg_bits(info, CHA + MODE, BIT3);
3320
Joe Perches0fab6de2008-04-28 02:14:02 -07003321 info->rx_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
3323
Alan Coxeeb46132009-01-02 13:48:47 +00003324static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
3326 if (debug_level >= DEBUG_LEVEL_ISR)
3327 printk("%s(%d):tx_start(%s)\n",
3328 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (info->tx_count) {
3331 /* If auto RTS enabled and RTS is inactive, then assert */
3332 /* RTS and set a flag indicating that the driver should */
3333 /* negate RTS when the transmission completes. */
Joe Perches0fab6de2008-04-28 02:14:02 -07003334 info->drop_rts_on_tx_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3337 get_signals(info);
3338 if (!(info->serial_signals & SerialSignal_RTS)) {
3339 info->serial_signals |= SerialSignal_RTS;
3340 set_signals(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003341 info->drop_rts_on_tx_done = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343 }
3344
3345 if (info->params.mode == MGSL_MODE_ASYNC) {
3346 if (!info->tx_active) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003347 info->tx_active = true;
Alan Coxeeb46132009-01-02 13:48:47 +00003348 tx_ready(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
3350 } else {
Joe Perches0fab6de2008-04-28 02:14:02 -07003351 info->tx_active = true;
Alan Coxeeb46132009-01-02 13:48:47 +00003352 tx_ready(info, tty);
Jiri Slaby40565f12007-02-12 00:52:31 -08003353 mod_timer(&info->tx_timer, jiffies +
3354 msecs_to_jiffies(5000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
3356 }
3357
3358 if (!info->tx_enabled)
Joe Perches0fab6de2008-04-28 02:14:02 -07003359 info->tx_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
Peter Hagervallcdaad342006-06-23 02:06:04 -07003362static void tx_stop(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363{
3364 if (debug_level >= DEBUG_LEVEL_ISR)
3365 printk("%s(%d):tx_stop(%s)\n",
3366 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003367
3368 del_timer(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Joe Perches0fab6de2008-04-28 02:14:02 -07003370 info->tx_enabled = false;
3371 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372}
3373
3374/* Reset the adapter to a known state and prepare it for further use.
3375 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003376static void reset_device(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003378 /* power up both channels (set BIT7) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 write_reg(info, CHA + CCR0, 0x80);
3380 write_reg(info, CHB + CCR0, 0x80);
3381 write_reg(info, CHA + MODE, 0);
3382 write_reg(info, CHB + MODE, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003383
3384 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 irq_disable(info, CHA, 0xffff);
3386 irq_disable(info, CHB, 0xffff);
3387 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 /* PCR Port Configuration Register
3390 *
3391 * 07..04 DEC[3..0] Serial I/F select outputs
3392 * 03 output, 1=AUTO CTS control enabled
3393 * 02 RI Ring Indicator input 0=active
3394 * 01 DSR input 0=active
3395 * 00 DTR output 0=active
3396 *
3397 * 0000 0110
Jeff Garzikd12341f2007-10-19 15:45:35 -04003398 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 write_reg(info, PCR, 0x06);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 /* PVR Port Value Register
3402 *
3403 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3404 * 03 AUTO CTS output 1=enabled
3405 * 02 RI Ring Indicator input
3406 * 01 DSR input
3407 * 00 DTR output (1=inactive)
3408 *
3409 * 0000 0001
3410 */
3411// write_reg(info, PVR, PVR_DTR);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 /* IPC Interrupt Port Configuration
3414 *
3415 * 07 VIS 1=Masked interrupts visible
3416 * 06..05 Reserved, 0
3417 * 04..03 SLA Slave address, 00 ignored
3418 * 02 CASM Cascading Mode, 1=daisy chain
3419 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3420 *
3421 * 0000 0101
Jeff Garzikd12341f2007-10-19 15:45:35 -04003422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 write_reg(info, IPC, 0x05);
3424}
3425
Peter Hagervallcdaad342006-06-23 02:06:04 -07003426static void async_mode(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
3428 unsigned char val;
3429
Jeff Garzikd12341f2007-10-19 15:45:35 -04003430 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 irq_disable(info, CHA, 0xffff);
3432 irq_disable(info, CHB, 0xffff);
3433 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003434
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 /* MODE
3436 *
3437 * 07 Reserved, 0
3438 * 06 FRTS RTS State, 0=active
3439 * 05 FCTS Flow Control on CTS
3440 * 04 FLON Flow Control Enable
3441 * 03 RAC Receiver Active, 0 = inactive
3442 * 02 RTS 0=Auto RTS, 1=manual RTS
3443 * 01 TRS Timer Resolution, 1=512
3444 * 00 TLP Test Loop, 0 = no loop
3445 *
3446 * 0000 0110
Jeff Garzikd12341f2007-10-19 15:45:35 -04003447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 val = 0x06;
3449 if (info->params.loopback)
3450 val |= BIT0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003451
3452 /* preserve RTS state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 if (!(info->serial_signals & SerialSignal_RTS))
3454 val |= BIT6;
3455 write_reg(info, CHA + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 /* CCR0
3458 *
3459 * 07 PU Power Up, 1=active, 0=power down
3460 * 06 MCE Master Clock Enable, 1=enabled
3461 * 05 Reserved, 0
3462 * 04..02 SC[2..0] Encoding, 000=NRZ
3463 * 01..00 SM[1..0] Serial Mode, 11=Async
3464 *
3465 * 1000 0011
Jeff Garzikd12341f2007-10-19 15:45:35 -04003466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 write_reg(info, CHA + CCR0, 0x83);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 /* CCR1
3470 *
3471 * 07..05 Reserved, 0
3472 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3473 * 03 BCR Bit Clock Rate, 1=16x
3474 * 02..00 CM[2..0] Clock Mode, 111=BRG
3475 *
3476 * 0001 1111
Jeff Garzikd12341f2007-10-19 15:45:35 -04003477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 write_reg(info, CHA + CCR1, 0x1f);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 /* CCR2 (channel A)
3481 *
3482 * 07..06 BGR[9..8] Baud rate bits 9..8
3483 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3484 * 04 SSEL Clock source select, 1=submode b
3485 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3486 * 02 RWX Read/Write Exchange 0=disabled
3487 * 01 Reserved, 0
3488 * 00 DIV, data inversion 0=disabled, 1=enabled
3489 *
3490 * 0001 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 write_reg(info, CHA + CCR2, 0x10);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003493
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 /* CCR3
3495 *
3496 * 07..01 Reserved, 0
3497 * 00 PSD DPLL Phase Shift Disable
3498 *
3499 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 write_reg(info, CHA + CCR3, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 /* CCR4
3504 *
3505 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3506 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3507 * 05 TST1 Test Pin, 0=normal operation
3508 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3509 * 03..00 Reserved, must be 0
3510 *
3511 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 write_reg(info, CHA + CCR4, 0x50);
3514 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 /* DAFO Data Format
3517 *
3518 * 07 Reserved, 0
3519 * 06 XBRK transmit break, 0=normal operation
3520 * 05 Stop bits (0=1, 1=2)
3521 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3522 * 02 PAREN Parity Enable
3523 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3524 *
Jeff Garzikd12341f2007-10-19 15:45:35 -04003525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 val = 0x00;
3527 if (info->params.data_bits != 8)
3528 val |= BIT0; /* 7 bits */
3529 if (info->params.stop_bits != 1)
3530 val |= BIT5;
3531 if (info->params.parity != ASYNC_PARITY_NONE)
3532 {
3533 val |= BIT2; /* Parity enable */
3534 if (info->params.parity == ASYNC_PARITY_ODD)
3535 val |= BIT3;
3536 else
3537 val |= BIT4;
3538 }
3539 write_reg(info, CHA + DAFO, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 /* RFC Rx FIFO Control
3542 *
3543 * 07 Reserved, 0
3544 * 06 DPS, 1=parity bit not stored in data byte
3545 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3546 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3547 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3548 * 01 Reserved, 0
3549 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3550 *
3551 * 0101 1100
Jeff Garzikd12341f2007-10-19 15:45:35 -04003552 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 write_reg(info, CHA + RFC, 0x5c);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003554
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 /* RLCR Receive length check register
3556 *
3557 * Max frame length = (RL + 1) * 32
Jeff Garzikd12341f2007-10-19 15:45:35 -04003558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 write_reg(info, CHA + RLCR, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 /* XBCH Transmit Byte Count High
3562 *
3563 * 07 DMA mode, 0 = interrupt driven
3564 * 06 NRM, 0=ABM (ignored)
3565 * 05 CAS Carrier Auto Start
3566 * 04 XC Transmit Continuously (ignored)
3567 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3568 *
3569 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 val = 0x00;
3572 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3573 val |= BIT5;
3574 write_reg(info, CHA + XBCH, val);
3575 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3576 irq_enable(info, CHA, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003577
3578 /* MODE:03 RAC Receiver Active, 1=active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 set_reg_bits(info, CHA + MODE, BIT3);
3580 enable_auxclk(info);
3581 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3582 irq_enable(info, CHB, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003583 /* PVR[3] 1=AUTO CTS active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 set_reg_bits(info, CHA + PVR, BIT3);
3585 } else
3586 clear_reg_bits(info, CHA + PVR, BIT3);
3587 irq_enable(info, CHA,
3588 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3589 IRQ_ALLSENT + IRQ_TXFIFO);
3590 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3591 wait_command_complete(info, CHA);
3592 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3593}
3594
3595/* Set the HDLC idle mode for the transmitter.
3596 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003597static void tx_set_idle(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003599 /* Note: ESCC2 only supports flags and one idle modes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3601 set_reg_bits(info, CHA + CCR1, BIT3);
3602 else
3603 clear_reg_bits(info, CHA + CCR1, BIT3);
3604}
3605
3606/* get state of the V24 status (input) signals.
3607 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003608static void get_signals(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
3610 unsigned char status = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003611
3612 /* preserve DTR and RTS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3614
3615 if (read_reg(info, CHB + VSTR) & BIT7)
3616 info->serial_signals |= SerialSignal_DCD;
3617 if (read_reg(info, CHB + STAR) & BIT1)
3618 info->serial_signals |= SerialSignal_CTS;
3619
3620 status = read_reg(info, CHA + PVR);
3621 if (!(status & PVR_RI))
3622 info->serial_signals |= SerialSignal_RI;
3623 if (!(status & PVR_DSR))
3624 info->serial_signals |= SerialSignal_DSR;
3625}
3626
3627/* Set the state of DTR and RTS based on contents of
3628 * serial_signals member of device extension.
3629 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003630static void set_signals(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
3632 unsigned char val;
3633
3634 val = read_reg(info, CHA + MODE);
3635 if (info->params.mode == MGSL_MODE_ASYNC) {
3636 if (info->serial_signals & SerialSignal_RTS)
3637 val &= ~BIT6;
3638 else
3639 val |= BIT6;
3640 } else {
3641 if (info->serial_signals & SerialSignal_RTS)
3642 val |= BIT2;
3643 else
3644 val &= ~BIT2;
3645 }
3646 write_reg(info, CHA + MODE, val);
3647
3648 if (info->serial_signals & SerialSignal_DTR)
3649 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3650 else
3651 set_reg_bits(info, CHA + PVR, PVR_DTR);
3652}
3653
Peter Hagervallcdaad342006-06-23 02:06:04 -07003654static void rx_reset_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
3656 RXBUF *buf;
3657 int i;
3658
3659 info->rx_put = 0;
3660 info->rx_get = 0;
3661 info->rx_frame_count = 0;
3662 for (i=0 ; i < info->rx_buf_count ; i++) {
3663 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3664 buf->status = buf->count = 0;
3665 }
3666}
3667
3668/* Attempt to return a received HDLC frame
3669 * Only frames received without errors are returned.
3670 *
Joe Perches0fab6de2008-04-28 02:14:02 -07003671 * Returns true if frame returned, otherwise false
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 */
Alan Coxeeb46132009-01-02 13:48:47 +00003673static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
3675 unsigned short status;
3676 RXBUF *buf;
3677 unsigned int framesize = 0;
3678 unsigned long flags;
Joe Perches0fab6de2008-04-28 02:14:02 -07003679 bool return_frame = false;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003680
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 if (info->rx_frame_count == 0)
Joe Perches0fab6de2008-04-28 02:14:02 -07003682 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3685
3686 status = buf->status;
3687
3688 /* 07 VFR 1=valid frame
3689 * 06 RDO 1=data overrun
3690 * 05 CRC 1=OK, 0=error
3691 * 04 RAB 1=frame aborted
3692 */
3693 if ((status & 0xf0) != 0xA0) {
3694 if (!(status & BIT7) || (status & BIT4))
3695 info->icount.rxabort++;
3696 else if (status & BIT6)
3697 info->icount.rxover++;
3698 else if (!(status & BIT5)) {
3699 info->icount.rxcrc++;
3700 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
Joe Perches0fab6de2008-04-28 02:14:02 -07003701 return_frame = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 }
3703 framesize = 0;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003704#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 {
Krzysztof Halasa198191c2008-06-30 23:26:53 +02003706 info->netdev->stats.rx_errors++;
3707 info->netdev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 }
3709#endif
3710 } else
Joe Perches0fab6de2008-04-28 02:14:02 -07003711 return_frame = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
3713 if (return_frame)
3714 framesize = buf->count;
3715
3716 if (debug_level >= DEBUG_LEVEL_BH)
3717 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3718 __FILE__,__LINE__,info->device_name,status,framesize);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 if (debug_level >= DEBUG_LEVEL_DATA)
Jeff Garzikd12341f2007-10-19 15:45:35 -04003721 trace_block(info, buf->data, framesize, 0);
3722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 if (framesize) {
3724 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3725 framesize+1 > info->max_frame_size) ||
3726 framesize > info->max_frame_size)
3727 info->icount.rxlong++;
3728 else {
3729 if (status & BIT5)
3730 info->icount.rxok++;
3731
3732 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3733 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3734 ++framesize;
3735 }
3736
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003737#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 if (info->netcount)
3739 hdlcdev_rx(info, buf->data, framesize);
3740 else
3741#endif
3742 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3743 }
3744 }
3745
3746 spin_lock_irqsave(&info->lock,flags);
3747 buf->status = buf->count = 0;
3748 info->rx_frame_count--;
3749 info->rx_get++;
3750 if (info->rx_get >= info->rx_buf_count)
3751 info->rx_get = 0;
3752 spin_unlock_irqrestore(&info->lock,flags);
3753
Joe Perches0fab6de2008-04-28 02:14:02 -07003754 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755}
3756
Joe Perches0fab6de2008-04-28 02:14:02 -07003757static bool register_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003759 static unsigned char patterns[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
Tobias Klauserfe971072006-01-09 20:54:02 -08003761 static unsigned int count = ARRAY_SIZE(patterns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 unsigned int i;
Joe Perches0fab6de2008-04-28 02:14:02 -07003763 bool rc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 unsigned long flags;
3765
3766 spin_lock_irqsave(&info->lock,flags);
3767 reset_device(info);
3768
3769 for (i = 0; i < count; i++) {
3770 write_reg(info, XAD1, patterns[i]);
3771 write_reg(info, XAD2, patterns[(i + 1) % count]);
Tobias Klauserfe971072006-01-09 20:54:02 -08003772 if ((read_reg(info, XAD1) != patterns[i]) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003774 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 break;
3776 }
3777 }
3778
3779 spin_unlock_irqrestore(&info->lock,flags);
3780 return rc;
3781}
3782
Joe Perches0fab6de2008-04-28 02:14:02 -07003783static bool irq_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
3785 unsigned long end_time;
3786 unsigned long flags;
3787
3788 spin_lock_irqsave(&info->lock,flags);
3789 reset_device(info);
3790
Joe Perches0fab6de2008-04-28 02:14:02 -07003791 info->testing_irq = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 hdlc_mode(info);
3793
Joe Perches0fab6de2008-04-28 02:14:02 -07003794 info->irq_occurred = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
3796 /* init hdlc mode */
3797
3798 irq_enable(info, CHA, IRQ_TIMER);
3799 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
3800 issue_command(info, CHA, CMD_START_TIMER);
3801
3802 spin_unlock_irqrestore(&info->lock,flags);
3803
3804 end_time=100;
3805 while(end_time-- && !info->irq_occurred) {
3806 msleep_interruptible(10);
3807 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04003808
Joe Perches0fab6de2008-04-28 02:14:02 -07003809 info->testing_irq = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
3811 spin_lock_irqsave(&info->lock,flags);
3812 reset_device(info);
3813 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003814
Joe Perches0fab6de2008-04-28 02:14:02 -07003815 return info->irq_occurred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816}
3817
Peter Hagervallcdaad342006-06-23 02:06:04 -07003818static int adapter_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819{
3820 if (!register_test(info)) {
3821 info->init_error = DiagStatus_AddressFailure;
3822 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
3823 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
3824 return -ENODEV;
3825 }
3826
3827 if (!irq_test(info)) {
3828 info->init_error = DiagStatus_IrqFailure;
3829 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
3830 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
3831 return -ENODEV;
3832 }
3833
3834 if (debug_level >= DEBUG_LEVEL_INFO)
3835 printk("%s(%d):device %s passed diagnostics\n",
3836 __FILE__,__LINE__,info->device_name);
3837 return 0;
3838}
3839
Peter Hagervallcdaad342006-06-23 02:06:04 -07003840static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841{
3842 int i;
3843 int linecount;
3844 if (xmit)
3845 printk("%s tx data:\n",info->device_name);
3846 else
3847 printk("%s rx data:\n",info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 while(count) {
3850 if (count > 16)
3851 linecount = 16;
3852 else
3853 linecount = count;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 for(i=0;i<linecount;i++)
3856 printk("%02X ",(unsigned char)data[i]);
3857 for(;i<17;i++)
3858 printk(" ");
3859 for(i=0;i<linecount;i++) {
3860 if (data[i]>=040 && data[i]<=0176)
3861 printk("%c",data[i]);
3862 else
3863 printk(".");
3864 }
3865 printk("\n");
Jeff Garzikd12341f2007-10-19 15:45:35 -04003866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 data += linecount;
3868 count -= linecount;
3869 }
3870}
3871
3872/* HDLC frame time out
3873 * update stats and do tx completion processing
3874 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003875static void tx_timeout(unsigned long context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876{
3877 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
3878 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 if ( debug_level >= DEBUG_LEVEL_INFO )
3881 printk( "%s(%d):tx_timeout(%s)\n",
3882 __FILE__,__LINE__,info->device_name);
3883 if(info->tx_active &&
3884 info->params.mode == MGSL_MODE_HDLC) {
3885 info->icount.txtimeout++;
3886 }
3887 spin_lock_irqsave(&info->lock,flags);
Joe Perches0fab6de2008-04-28 02:14:02 -07003888 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 info->tx_count = info->tx_put = info->tx_get = 0;
3890
3891 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003892
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003893#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 if (info->netcount)
3895 hdlcdev_tx_done(info);
3896 else
3897#endif
Alan Coxeeb46132009-01-02 13:48:47 +00003898 {
3899 struct tty_struct *tty = tty_port_tty_get(&info->port);
3900 bh_transmit(info, tty);
3901 tty_kref_put(tty);
3902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903}
3904
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003905#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
3907/**
3908 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
3909 * set encoding and frame check sequence (FCS) options
3910 *
3911 * dev pointer to network device structure
3912 * encoding serial encoding setting
3913 * parity FCS setting
3914 *
3915 * returns 0 if success, otherwise error code
3916 */
3917static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
3918 unsigned short parity)
3919{
3920 MGSLPC_INFO *info = dev_to_port(dev);
Alan Coxeeb46132009-01-02 13:48:47 +00003921 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 unsigned char new_encoding;
3923 unsigned short new_crctype;
3924
3925 /* return error if TTY interface open */
Alan Coxeeb46132009-01-02 13:48:47 +00003926 if (info->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 return -EBUSY;
3928
3929 switch (encoding)
3930 {
3931 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
3932 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
3933 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
3934 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
3935 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
3936 default: return -EINVAL;
3937 }
3938
3939 switch (parity)
3940 {
3941 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
3942 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
3943 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
3944 default: return -EINVAL;
3945 }
3946
3947 info->params.encoding = new_encoding;
Alexey Dobriyan53b35312006-03-24 03:16:13 -08003948 info->params.crc_type = new_crctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
3950 /* if network interface up, reprogram hardware */
Alan Coxeeb46132009-01-02 13:48:47 +00003951 if (info->netcount) {
3952 tty = tty_port_tty_get(&info->port);
3953 mgslpc_program_hw(info, tty);
3954 tty_kref_put(tty);
3955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
3957 return 0;
3958}
3959
3960/**
3961 * called by generic HDLC layer to send frame
3962 *
3963 * skb socket buffer containing HDLC frame
3964 * dev pointer to network device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 */
Stephen Hemminger4c5d5022009-08-31 19:50:48 +00003966static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
3967 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
3969 MGSLPC_INFO *info = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 unsigned long flags;
3971
3972 if (debug_level >= DEBUG_LEVEL_INFO)
3973 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
3974
3975 /* stop sending until this frame completes */
3976 netif_stop_queue(dev);
3977
3978 /* copy data to device buffers */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003979 skb_copy_from_linear_data(skb, info->tx_buf, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 info->tx_get = 0;
3981 info->tx_put = info->tx_count = skb->len;
3982
3983 /* update network statistics */
Krzysztof Halasa198191c2008-06-30 23:26:53 +02003984 dev->stats.tx_packets++;
3985 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
3987 /* done with socket buffer, so free it */
3988 dev_kfree_skb(skb);
3989
3990 /* save start time for transmit timeout detection */
3991 dev->trans_start = jiffies;
3992
3993 /* start hardware transmitter if necessary */
3994 spin_lock_irqsave(&info->lock,flags);
Alan Coxeeb46132009-01-02 13:48:47 +00003995 if (!info->tx_active) {
3996 struct tty_struct *tty = tty_port_tty_get(&info->port);
3997 tx_start(info, tty);
3998 tty_kref_put(tty);
3999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 spin_unlock_irqrestore(&info->lock,flags);
4001
Stephen Hemminger4c5d5022009-08-31 19:50:48 +00004002 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003}
4004
4005/**
4006 * called by network layer when interface enabled
4007 * claim resources and initialize hardware
4008 *
4009 * dev pointer to network device structure
4010 *
4011 * returns 0 if success, otherwise error code
4012 */
4013static int hdlcdev_open(struct net_device *dev)
4014{
4015 MGSLPC_INFO *info = dev_to_port(dev);
Alan Coxeeb46132009-01-02 13:48:47 +00004016 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 int rc;
4018 unsigned long flags;
4019
4020 if (debug_level >= DEBUG_LEVEL_INFO)
4021 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4022
4023 /* generic HDLC layer open processing */
4024 if ((rc = hdlc_open(dev)))
4025 return rc;
4026
4027 /* arbitrate between network and tty opens */
4028 spin_lock_irqsave(&info->netlock, flags);
Alan Coxeeb46132009-01-02 13:48:47 +00004029 if (info->port.count != 0 || info->netcount != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4031 spin_unlock_irqrestore(&info->netlock, flags);
4032 return -EBUSY;
4033 }
4034 info->netcount=1;
4035 spin_unlock_irqrestore(&info->netlock, flags);
4036
Alan Coxeeb46132009-01-02 13:48:47 +00004037 tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 /* claim resources and init adapter */
Alan Coxeeb46132009-01-02 13:48:47 +00004039 if ((rc = startup(info, tty)) != 0) {
4040 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 spin_lock_irqsave(&info->netlock, flags);
4042 info->netcount=0;
4043 spin_unlock_irqrestore(&info->netlock, flags);
4044 return rc;
4045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 /* assert DTR and RTS, apply hardware settings */
4047 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
Alan Coxeeb46132009-01-02 13:48:47 +00004048 mgslpc_program_hw(info, tty);
4049 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
4051 /* enable network layer transmit */
4052 dev->trans_start = jiffies;
4053 netif_start_queue(dev);
4054
4055 /* inform generic HDLC layer of current DCD status */
4056 spin_lock_irqsave(&info->lock, flags);
4057 get_signals(info);
4058 spin_unlock_irqrestore(&info->lock, flags);
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07004059 if (info->serial_signals & SerialSignal_DCD)
4060 netif_carrier_on(dev);
4061 else
4062 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 return 0;
4064}
4065
4066/**
4067 * called by network layer when interface is disabled
4068 * shutdown hardware and release resources
4069 *
4070 * dev pointer to network device structure
4071 *
4072 * returns 0 if success, otherwise error code
4073 */
4074static int hdlcdev_close(struct net_device *dev)
4075{
4076 MGSLPC_INFO *info = dev_to_port(dev);
Alan Coxeeb46132009-01-02 13:48:47 +00004077 struct tty_struct *tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 unsigned long flags;
4079
4080 if (debug_level >= DEBUG_LEVEL_INFO)
4081 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4082
4083 netif_stop_queue(dev);
4084
4085 /* shutdown adapter and release resources */
Alan Coxeeb46132009-01-02 13:48:47 +00004086 shutdown(info, tty);
4087 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 hdlc_close(dev);
4089
4090 spin_lock_irqsave(&info->netlock, flags);
4091 info->netcount=0;
4092 spin_unlock_irqrestore(&info->netlock, flags);
4093
4094 return 0;
4095}
4096
4097/**
4098 * called by network layer to process IOCTL call to network device
4099 *
4100 * dev pointer to network device structure
4101 * ifr pointer to network interface request structure
4102 * cmd IOCTL command code
4103 *
4104 * returns 0 if success, otherwise error code
4105 */
4106static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4107{
4108 const size_t size = sizeof(sync_serial_settings);
4109 sync_serial_settings new_line;
4110 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4111 MGSLPC_INFO *info = dev_to_port(dev);
4112 unsigned int flags;
4113
4114 if (debug_level >= DEBUG_LEVEL_INFO)
4115 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4116
4117 /* return error if TTY interface open */
Alan Coxeeb46132009-01-02 13:48:47 +00004118 if (info->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 return -EBUSY;
4120
4121 if (cmd != SIOCWANDEV)
4122 return hdlc_ioctl(dev, ifr, cmd);
4123
4124 switch(ifr->ifr_settings.type) {
4125 case IF_GET_IFACE: /* return current sync_serial_settings */
4126
4127 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4128 if (ifr->ifr_settings.size < size) {
4129 ifr->ifr_settings.size = size; /* data size wanted */
4130 return -ENOBUFS;
4131 }
4132
4133 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4134 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4135 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4136 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4137
4138 switch (flags){
4139 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4140 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4141 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4142 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4143 default: new_line.clock_type = CLOCK_DEFAULT;
4144 }
4145
4146 new_line.clock_rate = info->params.clock_speed;
4147 new_line.loopback = info->params.loopback ? 1:0;
4148
4149 if (copy_to_user(line, &new_line, size))
4150 return -EFAULT;
4151 return 0;
4152
4153 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4154
4155 if(!capable(CAP_NET_ADMIN))
4156 return -EPERM;
4157 if (copy_from_user(&new_line, line, size))
4158 return -EFAULT;
4159
4160 switch (new_line.clock_type)
4161 {
4162 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4163 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4164 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4165 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4166 case CLOCK_DEFAULT: flags = info->params.flags &
4167 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4168 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4169 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4170 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4171 default: return -EINVAL;
4172 }
4173
4174 if (new_line.loopback != 0 && new_line.loopback != 1)
4175 return -EINVAL;
4176
4177 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4178 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4179 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4180 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4181 info->params.flags |= flags;
4182
4183 info->params.loopback = new_line.loopback;
4184
4185 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4186 info->params.clock_speed = new_line.clock_rate;
4187 else
4188 info->params.clock_speed = 0;
4189
4190 /* if network interface up, reprogram hardware */
Alan Coxeeb46132009-01-02 13:48:47 +00004191 if (info->netcount) {
4192 struct tty_struct *tty = tty_port_tty_get(&info->port);
4193 mgslpc_program_hw(info, tty);
4194 tty_kref_put(tty);
4195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 return 0;
4197
4198 default:
4199 return hdlc_ioctl(dev, ifr, cmd);
4200 }
4201}
4202
4203/**
4204 * called by network layer when transmit timeout is detected
4205 *
4206 * dev pointer to network device structure
4207 */
4208static void hdlcdev_tx_timeout(struct net_device *dev)
4209{
4210 MGSLPC_INFO *info = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 unsigned long flags;
4212
4213 if (debug_level >= DEBUG_LEVEL_INFO)
4214 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4215
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004216 dev->stats.tx_errors++;
4217 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
4219 spin_lock_irqsave(&info->lock,flags);
4220 tx_stop(info);
4221 spin_unlock_irqrestore(&info->lock,flags);
4222
4223 netif_wake_queue(dev);
4224}
4225
4226/**
4227 * called by device driver when transmit completes
4228 * reenable network layer transmit if stopped
4229 *
4230 * info pointer to device instance information
4231 */
4232static void hdlcdev_tx_done(MGSLPC_INFO *info)
4233{
4234 if (netif_queue_stopped(info->netdev))
4235 netif_wake_queue(info->netdev);
4236}
4237
4238/**
4239 * called by device driver when frame received
4240 * pass frame to network layer
4241 *
4242 * info pointer to device instance information
4243 * buf pointer to buffer contianing frame data
4244 * size count of data bytes in buf
4245 */
4246static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4247{
4248 struct sk_buff *skb = dev_alloc_skb(size);
4249 struct net_device *dev = info->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
4251 if (debug_level >= DEBUG_LEVEL_INFO)
4252 printk("hdlcdev_rx(%s)\n",dev->name);
4253
4254 if (skb == NULL) {
4255 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004256 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 return;
4258 }
4259
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004260 memcpy(skb_put(skb, size), buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004262 skb->protocol = hdlc_type_trans(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004264 dev->stats.rx_packets++;
4265 dev->stats.rx_bytes += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
4267 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268}
4269
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01004270static const struct net_device_ops hdlcdev_ops = {
4271 .ndo_open = hdlcdev_open,
4272 .ndo_stop = hdlcdev_close,
4273 .ndo_change_mtu = hdlc_change_mtu,
4274 .ndo_start_xmit = hdlc_start_xmit,
4275 .ndo_do_ioctl = hdlcdev_ioctl,
4276 .ndo_tx_timeout = hdlcdev_tx_timeout,
4277};
4278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279/**
4280 * called by device driver when adding device instance
4281 * do generic HDLC initialization
4282 *
4283 * info pointer to device instance information
4284 *
4285 * returns 0 if success, otherwise error code
4286 */
4287static int hdlcdev_init(MGSLPC_INFO *info)
4288{
4289 int rc;
4290 struct net_device *dev;
4291 hdlc_device *hdlc;
4292
4293 /* allocate and initialize network and HDLC layer objects */
4294
4295 if (!(dev = alloc_hdlcdev(info))) {
4296 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4297 return -ENOMEM;
4298 }
4299
4300 /* for network layer reporting purposes only */
4301 dev->base_addr = info->io_base;
4302 dev->irq = info->irq_level;
4303
4304 /* network layer callbacks and settings */
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01004305 dev->netdev_ops = &hdlcdev_ops;
4306 dev->watchdog_timeo = 10 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 dev->tx_queue_len = 50;
4308
4309 /* generic HDLC layer callbacks and settings */
4310 hdlc = dev_to_hdlc(dev);
4311 hdlc->attach = hdlcdev_attach;
4312 hdlc->xmit = hdlcdev_xmit;
4313
4314 /* register objects with HDLC layer */
4315 if ((rc = register_hdlc_device(dev))) {
4316 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4317 free_netdev(dev);
4318 return rc;
4319 }
4320
4321 info->netdev = dev;
4322 return 0;
4323}
4324
4325/**
4326 * called by device driver when removing device instance
4327 * do generic HDLC cleanup
4328 *
4329 * info pointer to device instance information
4330 */
4331static void hdlcdev_exit(MGSLPC_INFO *info)
4332{
4333 unregister_hdlc_device(info->netdev);
4334 free_netdev(info->netdev);
4335 info->netdev = NULL;
4336}
4337
4338#endif /* CONFIG_HDLC */
4339