blob: 9ecd6bef5d3b5457ec5bcc6566db7621c1d791f3 [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/cs.h>
74#include <pcmcia/cistpl.h>
75#include <pcmcia/cisreg.h>
76#include <pcmcia/ds.h>
77
Paul Fulghumaf69c7f2006-12-06 20:40:24 -080078#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_CS_MODULE))
79#define SYNCLINK_GENERIC_HDLC 1
80#else
81#define SYNCLINK_GENERIC_HDLC 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
84#define GET_USER(error,value,addr) error = get_user(value,addr)
85#define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
86#define PUT_USER(error,value,addr) error = put_user(value,addr)
87#define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
88
89#include <asm/uaccess.h>
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static MGSL_PARAMS default_params = {
92 MGSL_MODE_HDLC, /* unsigned long mode */
93 0, /* unsigned char loopback; */
94 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
95 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
96 0, /* unsigned long clock_speed; */
97 0xff, /* unsigned char addr_filter; */
98 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
99 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
100 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
101 9600, /* unsigned long data_rate; */
102 8, /* unsigned char data_bits; */
103 1, /* unsigned char stop_bits; */
104 ASYNC_PARITY_NONE /* unsigned char parity; */
105};
106
107typedef struct
108{
109 int count;
110 unsigned char status;
111 char data[1];
112} RXBUF;
113
114/* The queue of BH actions to be performed */
115
116#define BH_RECEIVE 1
117#define BH_TRANSMIT 2
118#define BH_STATUS 4
119
120#define IO_PIN_SHUTDOWN_LIMIT 100
121
122#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
123
124struct _input_signal_events {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400125 int ri_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int ri_down;
127 int dsr_up;
128 int dsr_down;
129 int dcd_up;
130 int dcd_down;
131 int cts_up;
132 int cts_down;
133};
134
135
136/*
137 * Device instance data structure
138 */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140typedef struct _mgslpc_info {
Alan Coxeeb46132009-01-02 13:48:47 +0000141 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 void *if_ptr; /* General purpose pointer (used by SPPP) */
143 int magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int line;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct mgsl_icount icount;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int timeout;
149 int x_char; /* xon/xoff character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned char read_status_mask;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400151 unsigned char ignore_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 unsigned char *tx_buf;
154 int tx_put;
155 int tx_get;
156 int tx_count;
157
158 /* circular list of fixed length rx buffers */
159
160 unsigned char *rx_buf; /* memory allocated for all rx buffers */
161 int rx_buf_total_size; /* size of memory allocated for rx buffers */
162 int rx_put; /* index of next empty rx buffer */
163 int rx_get; /* index of next full rx buffer */
164 int rx_buf_size; /* size in bytes of single rx buffer */
165 int rx_buf_count; /* total number of rx buffers */
166 int rx_frame_count; /* number of full rx buffers */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 wait_queue_head_t status_event_wait_q;
169 wait_queue_head_t event_wait_q;
170 struct timer_list tx_timer; /* HDLC transmit timeout timer */
171 struct _mgslpc_info *next_device; /* device list link */
172
173 unsigned short imra_value;
174 unsigned short imrb_value;
175 unsigned char pim_value;
176
177 spinlock_t lock;
178 struct work_struct task; /* task structure for scheduling bh */
179
180 u32 max_frame_size;
181
182 u32 pending_bh;
183
Joe Perches0fab6de2008-04-28 02:14:02 -0700184 bool bh_running;
185 bool bh_requested;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int dcd_chkcount; /* check counts to prevent */
188 int cts_chkcount; /* too many IRQs if a signal */
189 int dsr_chkcount; /* is floating */
190 int ri_chkcount;
191
Joe Perches0fab6de2008-04-28 02:14:02 -0700192 bool rx_enabled;
193 bool rx_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Joe Perches0fab6de2008-04-28 02:14:02 -0700195 bool tx_enabled;
196 bool tx_active;
197 bool tx_aborting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 u32 idle_mode;
199
200 int if_mode; /* serial interface selection (RS-232, v.35 etc) */
201
202 char device_name[25]; /* device instance name */
203
204 unsigned int io_base; /* base I/O address of adapter */
205 unsigned int irq_level;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 MGSL_PARAMS params; /* communications parameters */
208
209 unsigned char serial_signals; /* current serial signal states */
210
Joe Perches0fab6de2008-04-28 02:14:02 -0700211 bool irq_occurred; /* for diagnostics use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 char testing_irq;
213 unsigned int init_error; /* startup error (DIAGS) */
214
215 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
Joe Perches0fab6de2008-04-28 02:14:02 -0700216 bool drop_rts_on_tx_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 struct _input_signal_events input_signal_events;
219
220 /* PCMCIA support */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100221 struct pcmcia_device *p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int stop;
223
224 /* SPPP/Cisco HDLC device parts */
225 int netcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 spinlock_t netlock;
227
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800228#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct net_device *netdev;
230#endif
231
232} MGSLPC_INFO;
233
234#define MGSLPC_MAGIC 0x5402
235
236/*
237 * The size of the serial xmit buffer is 1 page, or 4096 bytes
238 */
239#define TXBUFSIZE 4096
240
Jeff Garzikd12341f2007-10-19 15:45:35 -0400241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#define CHA 0x00 /* channel A offset */
243#define CHB 0x40 /* channel B offset */
244
245/*
246 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
247 */
248#undef PVR
249
250#define RXFIFO 0
251#define TXFIFO 0
252#define STAR 0x20
253#define CMDR 0x20
254#define RSTA 0x21
255#define PRE 0x21
256#define MODE 0x22
257#define TIMR 0x23
258#define XAD1 0x24
259#define XAD2 0x25
260#define RAH1 0x26
261#define RAH2 0x27
262#define DAFO 0x27
263#define RAL1 0x28
264#define RFC 0x28
265#define RHCR 0x29
266#define RAL2 0x29
267#define RBCL 0x2a
268#define XBCL 0x2a
269#define RBCH 0x2b
270#define XBCH 0x2b
271#define CCR0 0x2c
272#define CCR1 0x2d
273#define CCR2 0x2e
274#define CCR3 0x2f
275#define VSTR 0x34
276#define BGR 0x34
277#define RLCR 0x35
278#define AML 0x36
279#define AMH 0x37
280#define GIS 0x38
281#define IVA 0x38
282#define IPC 0x39
283#define ISR 0x3a
284#define IMR 0x3a
285#define PVR 0x3c
286#define PIS 0x3d
287#define PIM 0x3d
288#define PCR 0x3e
289#define CCR4 0x3f
Jeff Garzikd12341f2007-10-19 15:45:35 -0400290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291// IMR/ISR
Jeff Garzikd12341f2007-10-19 15:45:35 -0400292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293#define IRQ_BREAK_ON BIT15 // rx break detected
294#define IRQ_DATAOVERRUN BIT14 // receive data overflow
295#define IRQ_ALLSENT BIT13 // all sent
296#define IRQ_UNDERRUN BIT12 // transmit data underrun
297#define IRQ_TIMER BIT11 // timer interrupt
298#define IRQ_CTS BIT10 // CTS status change
299#define IRQ_TXREPEAT BIT9 // tx message repeat
300#define IRQ_TXFIFO BIT8 // transmit pool ready
301#define IRQ_RXEOM BIT7 // receive message end
302#define IRQ_EXITHUNT BIT6 // receive frame start
303#define IRQ_RXTIME BIT6 // rx char timeout
304#define IRQ_DCD BIT2 // carrier detect status change
305#define IRQ_OVERRUN BIT1 // receive frame overflow
306#define IRQ_RXFIFO BIT0 // receive pool full
Jeff Garzikd12341f2007-10-19 15:45:35 -0400307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308// STAR
Jeff Garzikd12341f2007-10-19 15:45:35 -0400309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#define XFW BIT6 // transmit FIFO write enable
311#define CEC BIT2 // command executing
312#define CTS BIT1 // CTS state
Jeff Garzikd12341f2007-10-19 15:45:35 -0400313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#define PVR_DTR BIT0
315#define PVR_DSR BIT1
316#define PVR_RI BIT2
317#define PVR_AUTOCTS BIT3
318#define PVR_RS232 0x20 /* 0010b */
319#define PVR_V35 0xe0 /* 1110b */
320#define PVR_RS422 0x40 /* 0100b */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400321
322/* Register access functions */
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
325#define read_reg(info, reg) inb((info)->io_base + (reg))
326
Jeff Garzikd12341f2007-10-19 15:45:35 -0400327#define read_reg16(info, reg) inw((info)->io_base + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328#define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
Jeff Garzikd12341f2007-10-19 15:45:35 -0400329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330#define set_reg_bits(info, reg, mask) \
331 write_reg(info, (reg), \
Jeff Garzikd12341f2007-10-19 15:45:35 -0400332 (unsigned char) (read_reg(info, (reg)) | (mask)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#define clear_reg_bits(info, reg, mask) \
334 write_reg(info, (reg), \
Jeff Garzikd12341f2007-10-19 15:45:35 -0400335 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/*
337 * interrupt enable/disable routines
Jeff Garzikd12341f2007-10-19 15:45:35 -0400338 */
339static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 if (channel == CHA) {
342 info->imra_value |= mask;
343 write_reg16(info, CHA + IMR, info->imra_value);
344 } else {
345 info->imrb_value |= mask;
346 write_reg16(info, CHB + IMR, info->imrb_value);
347 }
348}
Jeff Garzikd12341f2007-10-19 15:45:35 -0400349static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 if (channel == CHA) {
352 info->imra_value &= ~mask;
353 write_reg16(info, CHA + IMR, info->imra_value);
354 } else {
355 info->imrb_value &= ~mask;
356 write_reg16(info, CHB + IMR, info->imrb_value);
357 }
358}
359
360#define port_irq_disable(info, mask) \
361 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
362
363#define port_irq_enable(info, mask) \
364 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
365
366static void rx_start(MGSLPC_INFO *info);
367static void rx_stop(MGSLPC_INFO *info);
368
Alan Coxeeb46132009-01-02 13:48:47 +0000369static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static void tx_stop(MGSLPC_INFO *info);
371static void tx_set_idle(MGSLPC_INFO *info);
372
373static void get_signals(MGSLPC_INFO *info);
374static void set_signals(MGSLPC_INFO *info);
375
376static void reset_device(MGSLPC_INFO *info);
377
378static void hdlc_mode(MGSLPC_INFO *info);
379static void async_mode(MGSLPC_INFO *info);
380
381static void tx_timeout(unsigned long context);
382
Alan Coxeeb46132009-01-02 13:48:47 +0000383static int carrier_raised(struct tty_port *port);
Alan Coxfcc8ac12009-06-11 12:24:17 +0100384static void dtr_rts(struct tty_port *port, int onoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800386#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387#define dev_to_port(D) (dev_to_hdlc(D)->priv)
388static void hdlcdev_tx_done(MGSLPC_INFO *info);
389static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
390static int hdlcdev_init(MGSLPC_INFO *info);
391static void hdlcdev_exit(MGSLPC_INFO *info);
392#endif
393
394static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
395
Joe Perches0fab6de2008-04-28 02:14:02 -0700396static bool register_test(MGSLPC_INFO *info);
397static bool irq_test(MGSLPC_INFO *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static int adapter_test(MGSLPC_INFO *info);
399
400static int claim_resources(MGSLPC_INFO *info);
401static void release_resources(MGSLPC_INFO *info);
402static void mgslpc_add_device(MGSLPC_INFO *info);
403static void mgslpc_remove_device(MGSLPC_INFO *info);
404
Alan Coxeeb46132009-01-02 13:48:47 +0000405static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static void rx_reset_buffers(MGSLPC_INFO *info);
407static int rx_alloc_buffers(MGSLPC_INFO *info);
408static void rx_free_buffers(MGSLPC_INFO *info);
409
David Howells7d12e782006-10-05 14:55:46 +0100410static irqreturn_t mgslpc_isr(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412/*
413 * Bottom half interrupt handlers
414 */
David Howellsc4028952006-11-22 14:57:56 +0000415static void bh_handler(struct work_struct *work);
Alan Coxeeb46132009-01-02 13:48:47 +0000416static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static void bh_status(MGSLPC_INFO *info);
418
419/*
420 * ioctl handlers
421 */
422static int tiocmget(struct tty_struct *tty, struct file *file);
423static int tiocmset(struct tty_struct *tty, struct file *file,
424 unsigned int set, unsigned int clear);
425static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
426static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
Alan Coxeeb46132009-01-02 13:48:47 +0000427static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
429static int set_txidle(MGSLPC_INFO *info, int idle_mode);
Alan Coxeeb46132009-01-02 13:48:47 +0000430static int set_txenable(MGSLPC_INFO *info, int enable, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431static int tx_abort(MGSLPC_INFO *info);
432static int set_rxenable(MGSLPC_INFO *info, int enable);
433static int wait_events(MGSLPC_INFO *info, int __user *mask);
434
435static MGSLPC_INFO *mgslpc_device_list = NULL;
436static int mgslpc_device_count = 0;
437
438/*
439 * Set this param to non-zero to load eax with the
440 * .text section address and breakpoint on module load.
441 * This is useful for use with gdb and add-symbol-file command.
442 */
443static int break_on_load=0;
444
445/*
446 * Driver major number, defaults to zero to get auto
447 * assigned major number. May be forced as module parameter.
448 */
449static int ttymajor=0;
450
451static int debug_level = 0;
452static int maxframe[MAX_DEVICE_COUNT] = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454module_param(break_on_load, bool, 0);
455module_param(ttymajor, int, 0);
456module_param(debug_level, int, 0);
457module_param_array(maxframe, int, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459MODULE_LICENSE("GPL");
460
461static char *driver_name = "SyncLink PC Card driver";
Paul Fulghuma7482a22005-09-10 00:26:07 -0700462static char *driver_version = "$Revision: 4.34 $";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464static struct tty_driver *serial_driver;
465
466/* number of characters left in xmit buffer before we ask for more */
467#define WAKEUP_CHARS 256
468
Alan Coxeeb46132009-01-02 13:48:47 +0000469static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
471
472/* PCMCIA prototypes */
473
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200474static int mgslpc_config(struct pcmcia_device *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static void mgslpc_release(u_long arg);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100476static void mgslpc_detach(struct pcmcia_device *p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/*
479 * 1st function defined in .text section. Calling this function in
480 * init_module() followed by a breakpoint allows a remote debugger
481 * (gdb) to get the .text address for the add-symbol-file command.
482 * This allows remote debugging of dynamically loadable modules.
483 */
484static void* mgslpc_get_text_ptr(void)
485{
486 return mgslpc_get_text_ptr;
487}
488
489/**
490 * line discipline callback wrappers
491 *
492 * The wrappers maintain line discipline references
493 * while calling into the line discipline.
494 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * ldisc_receive_buf - pass receive data to line discipline
496 */
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static void ldisc_receive_buf(struct tty_struct *tty,
499 const __u8 *data, char *flags, int count)
500{
501 struct tty_ldisc *ld;
502 if (!tty)
503 return;
504 ld = tty_ldisc_ref(tty);
505 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100506 if (ld->ops->receive_buf)
507 ld->ops->receive_buf(tty, data, flags, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 tty_ldisc_deref(ld);
509 }
510}
511
Alan Coxeeb46132009-01-02 13:48:47 +0000512static const struct tty_port_operations mgslpc_port_ops = {
513 .carrier_raised = carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +0100514 .dtr_rts = dtr_rts
Alan Coxeeb46132009-01-02 13:48:47 +0000515};
516
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200517static int mgslpc_probe(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 MGSLPC_INFO *info;
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200520 int ret;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (debug_level >= DEBUG_LEVEL_INFO)
523 printk("mgslpc_attach\n");
Dominik Brodowskifd238232006-03-05 10:45:09 +0100524
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700525 info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (!info) {
527 printk("Error can't allocate device instance data\n");
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100528 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 info->magic = MGSLPC_MAGIC;
Alan Coxeeb46132009-01-02 13:48:47 +0000532 tty_port_init(&info->port);
533 info->port.ops = &mgslpc_port_ops;
David Howellsc4028952006-11-22 14:57:56 +0000534 INIT_WORK(&info->task, bh_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 info->max_frame_size = 4096;
Alan Coxeeb46132009-01-02 13:48:47 +0000536 info->port.close_delay = 5*HZ/10;
537 info->port.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 init_waitqueue_head(&info->status_event_wait_q);
539 init_waitqueue_head(&info->event_wait_q);
540 spin_lock_init(&info->lock);
541 spin_lock_init(&info->netlock);
542 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
Jeff Garzikd12341f2007-10-19 15:45:35 -0400543 info->idle_mode = HDLC_TXIDLE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 info->imra_value = 0xffff;
545 info->imrb_value = 0xffff;
546 info->pim_value = 0xff;
547
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200548 info->p_dev = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 link->priv = info;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100550
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200551 /* Initialize the struct pcmcia_device structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 link->conf.Attributes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 link->conf.IntType = INT_MEMORY_AND_IO;
555
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200556 ret = mgslpc_config(link);
557 if (ret)
558 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 mgslpc_add_device(info);
561
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/* Card has been inserted.
566 */
567
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200568static int mgslpc_ioprobe(struct pcmcia_device *p_dev,
569 cistpl_cftable_entry_t *cfg,
570 cistpl_cftable_entry_t *dflt,
571 unsigned int vcc,
572 void *priv_data)
573{
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200574 if (!cfg->io.nwin)
575 return -ENODEV;
576
577 p_dev->resource[0]->start = cfg->io.win[0].base;
578 p_dev->resource[0]->end = cfg->io.win[0].len;
579 p_dev->resource[0]->flags |= pcmcia_io_cfg_data_width(cfg->io.flags);
580 p_dev->io_lines = cfg->io.flags & CISTPL_IO_LINES_MASK;
581
582 return pcmcia_request_io(p_dev);
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200583}
584
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200585static int mgslpc_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 MGSLPC_INFO *info = link->priv;
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200588 int ret;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (debug_level >= DEBUG_LEVEL_INFO)
591 printk("mgslpc_config(0x%p)\n", link);
592
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200593 ret = pcmcia_loop_config(link, mgslpc_ioprobe, NULL);
594 if (ret != 0)
595 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 link->conf.Attributes = CONF_ENABLE_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 link->conf.IntType = INT_MEMORY_AND_IO;
599 link->conf.ConfigIndex = 8;
600 link->conf.Present = PRESENT_OPTION;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400601
Dominik Brodowskieb141202010-03-07 12:21:16 +0100602 ret = pcmcia_request_irq(link, mgslpc_isr);
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200603 if (ret)
604 goto failed;
605 ret = pcmcia_request_configuration(link, &link->conf);
606 if (ret)
607 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200609 info->io_base = link->resource[0]->start;
Dominik Brodowskieb141202010-03-07 12:21:16 +0100610 info->irq_level = link->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Dominik Brodowskided6a1a2010-03-20 19:35:12 +0100612 dev_info(&link->dev, "index 0x%02x:",
613 link->conf.ConfigIndex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (link->conf.Attributes & CONF_ENABLE_IRQ)
Dominik Brodowskieb141202010-03-07 12:21:16 +0100615 printk(", irq %d", link->irq);
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200616 if (link->resource[0])
617 printk(", io %pR", link->resource[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 printk("\n");
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200621failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 mgslpc_release((u_long)link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200623 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626/* Card has been removed.
627 * Unregister device and release PCMCIA configuration.
628 * If device is open, postpone until it is closed.
629 */
630static void mgslpc_release(u_long arg)
631{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100632 struct pcmcia_device *link = (struct pcmcia_device *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100634 if (debug_level >= DEBUG_LEVEL_INFO)
635 printk("mgslpc_release(0x%p)\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100637 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200640static void mgslpc_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100642 if (debug_level >= DEBUG_LEVEL_INFO)
643 printk("mgslpc_detach(0x%p)\n", link);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100644
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100645 ((MGSLPC_INFO *)link->priv)->stop = 1;
646 mgslpc_release((u_long)link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100648 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200651static int mgslpc_suspend(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100652{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100653 MGSLPC_INFO *info = link->priv;
654
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100655 info->stop = 1;
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100656
657 return 0;
658}
659
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200660static int mgslpc_resume(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100661{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100662 MGSLPC_INFO *info = link->priv;
663
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100664 info->stop = 0;
665
666 return 0;
667}
668
669
Joe Perches0fab6de2008-04-28 02:14:02 -0700670static inline bool mgslpc_paranoia_check(MGSLPC_INFO *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 char *name, const char *routine)
672{
673#ifdef MGSLPC_PARANOIA_CHECK
674 static const char *badmagic =
675 "Warning: bad magic number for mgsl struct (%s) in %s\n";
676 static const char *badinfo =
677 "Warning: null mgslpc_info for (%s) in %s\n";
678
679 if (!info) {
680 printk(badinfo, name, routine);
Joe Perches0fab6de2008-04-28 02:14:02 -0700681 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683 if (info->magic != MGSLPC_MAGIC) {
684 printk(badmagic, name, routine);
Joe Perches0fab6de2008-04-28 02:14:02 -0700685 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687#else
688 if (!info)
Joe Perches0fab6de2008-04-28 02:14:02 -0700689 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690#endif
Joe Perches0fab6de2008-04-28 02:14:02 -0700691 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694
695#define CMD_RXFIFO BIT7 // release current rx FIFO
696#define CMD_RXRESET BIT6 // receiver reset
697#define CMD_RXFIFO_READ BIT5
698#define CMD_START_TIMER BIT4
699#define CMD_TXFIFO BIT3 // release current tx FIFO
700#define CMD_TXEOM BIT1 // transmit end message
701#define CMD_TXRESET BIT0 // transmit reset
702
Joe Perches0fab6de2008-04-28 02:14:02 -0700703static bool wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 int i = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400706 /* wait for command completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
708 udelay(1);
709 if (i++ == 1000)
Joe Perches0fab6de2008-04-28 02:14:02 -0700710 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Joe Perches0fab6de2008-04-28 02:14:02 -0700712 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Jeff Garzikd12341f2007-10-19 15:45:35 -0400715static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 wait_command_complete(info, channel);
718 write_reg(info, (unsigned char) (channel + CMDR), cmd);
719}
720
721static void tx_pause(struct tty_struct *tty)
722{
723 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
724 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
727 return;
728 if (debug_level >= DEBUG_LEVEL_INFO)
Jeff Garzikd12341f2007-10-19 15:45:35 -0400729 printk("tx_pause(%s)\n",info->device_name);
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 spin_lock_irqsave(&info->lock,flags);
732 if (info->tx_enabled)
733 tx_stop(info);
734 spin_unlock_irqrestore(&info->lock,flags);
735}
736
737static void tx_release(struct tty_struct *tty)
738{
739 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
740 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
743 return;
744 if (debug_level >= DEBUG_LEVEL_INFO)
Jeff Garzikd12341f2007-10-19 15:45:35 -0400745 printk("tx_release(%s)\n",info->device_name);
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 spin_lock_irqsave(&info->lock,flags);
748 if (!info->tx_enabled)
Alan Coxeeb46132009-01-02 13:48:47 +0000749 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 spin_unlock_irqrestore(&info->lock,flags);
751}
752
753/* Return next bottom half action to perform.
754 * or 0 if nothing to do.
755 */
756static int bh_action(MGSLPC_INFO *info)
757{
758 unsigned long flags;
759 int rc = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 spin_lock_irqsave(&info->lock,flags);
762
763 if (info->pending_bh & BH_RECEIVE) {
764 info->pending_bh &= ~BH_RECEIVE;
765 rc = BH_RECEIVE;
766 } else if (info->pending_bh & BH_TRANSMIT) {
767 info->pending_bh &= ~BH_TRANSMIT;
768 rc = BH_TRANSMIT;
769 } else if (info->pending_bh & BH_STATUS) {
770 info->pending_bh &= ~BH_STATUS;
771 rc = BH_STATUS;
772 }
773
774 if (!rc) {
775 /* Mark BH routine as complete */
Joe Perches0fab6de2008-04-28 02:14:02 -0700776 info->bh_running = false;
777 info->bh_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Jeff Garzikd12341f2007-10-19 15:45:35 -0400779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return rc;
783}
784
David Howellsc4028952006-11-22 14:57:56 +0000785static void bh_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
David Howellsc4028952006-11-22 14:57:56 +0000787 MGSLPC_INFO *info = container_of(work, MGSLPC_INFO, task);
Alan Coxeeb46132009-01-02 13:48:47 +0000788 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int action;
790
791 if (!info)
792 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (debug_level >= DEBUG_LEVEL_BH)
795 printk( "%s(%d):bh_handler(%s) entry\n",
796 __FILE__,__LINE__,info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400797
Joe Perches0fab6de2008-04-28 02:14:02 -0700798 info->bh_running = true;
Alan Coxeeb46132009-01-02 13:48:47 +0000799 tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 while((action = bh_action(info)) != 0) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* Process work item */
804 if ( debug_level >= DEBUG_LEVEL_BH )
805 printk( "%s(%d):bh_handler() work item action=%d\n",
806 __FILE__,__LINE__,action);
807
808 switch (action) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 case BH_RECEIVE:
Alan Coxeeb46132009-01-02 13:48:47 +0000811 while(rx_get_frame(info, tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813 case BH_TRANSMIT:
Alan Coxeeb46132009-01-02 13:48:47 +0000814 bh_transmit(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 break;
816 case BH_STATUS:
817 bh_status(info);
818 break;
819 default:
820 /* unknown work item ID */
821 printk("Unknown work item ID=%08X!\n", action);
822 break;
823 }
824 }
825
Alan Coxeeb46132009-01-02 13:48:47 +0000826 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (debug_level >= DEBUG_LEVEL_BH)
828 printk( "%s(%d):bh_handler(%s) exit\n",
829 __FILE__,__LINE__,info->device_name);
830}
831
Alan Coxeeb46132009-01-02 13:48:47 +0000832static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (debug_level >= DEBUG_LEVEL_BH)
835 printk("bh_transmit() entry on %s\n", info->device_name);
836
Jiri Slabyb963a842007-02-10 01:44:55 -0800837 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Peter Hagervallcdaad342006-06-23 02:06:04 -0700841static void bh_status(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 info->ri_chkcount = 0;
844 info->dsr_chkcount = 0;
845 info->dcd_chkcount = 0;
846 info->cts_chkcount = 0;
847}
848
Jeff Garzikd12341f2007-10-19 15:45:35 -0400849/* eom: non-zero = end of frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
851{
852 unsigned char data[2];
853 unsigned char fifo_count, read_count, i;
854 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
855
856 if (debug_level >= DEBUG_LEVEL_ISR)
857 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (!info->rx_enabled)
860 return;
861
862 if (info->rx_frame_count >= info->rx_buf_count) {
863 /* no more free buffers */
864 issue_command(info, CHA, CMD_RXRESET);
865 info->pending_bh |= BH_RECEIVE;
Joe Perches0fab6de2008-04-28 02:14:02 -0700866 info->rx_overflow = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 info->icount.buf_overrun++;
868 return;
869 }
870
871 if (eom) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400872 /* end of frame, get FIFO count from RBCL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
874 fifo_count = 32;
875 } else
876 fifo_count = 32;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 do {
879 if (fifo_count == 1) {
880 read_count = 1;
881 data[0] = read_reg(info, CHA + RXFIFO);
882 } else {
883 read_count = 2;
884 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
885 }
886 fifo_count -= read_count;
887 if (!fifo_count && eom)
888 buf->status = data[--read_count];
889
890 for (i = 0; i < read_count; i++) {
891 if (buf->count >= info->max_frame_size) {
892 /* frame too large, reset receiver and reset current buffer */
893 issue_command(info, CHA, CMD_RXRESET);
894 buf->count = 0;
895 return;
896 }
897 *(buf->data + buf->count) = data[i];
898 buf->count++;
899 }
900 } while (fifo_count);
901
902 if (eom) {
903 info->pending_bh |= BH_RECEIVE;
904 info->rx_frame_count++;
905 info->rx_put++;
906 if (info->rx_put >= info->rx_buf_count)
907 info->rx_put = 0;
908 }
909 issue_command(info, CHA, CMD_RXFIFO);
910}
911
Alan Coxeeb46132009-01-02 13:48:47 +0000912static void rx_ready_async(MGSLPC_INFO *info, int tcd, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Alan Cox33f0f882006-01-09 20:54:13 -0800914 unsigned char data, status, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int fifo_count;
Alan Cox33f0f882006-01-09 20:54:13 -0800916 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct mgsl_icount *icount = &info->icount;
918
919 if (tcd) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400920 /* early termination, get FIFO count from RBCL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
922
923 /* Zero fifo count could mean 0 or 32 bytes available.
924 * If BIT5 of STAR is set then at least 1 byte is available.
925 */
926 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
927 fifo_count = 32;
928 } else
929 fifo_count = 32;
Alan Cox33f0f882006-01-09 20:54:13 -0800930
931 tty_buffer_request_room(tty, fifo_count);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400932 /* Flush received async data to receive data buffer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 while (fifo_count) {
934 data = read_reg(info, CHA + RXFIFO);
935 status = read_reg(info, CHA + RXFIFO);
936 fifo_count -= 2;
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 icount->rx++;
Alan Cox33f0f882006-01-09 20:54:13 -0800939 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 // if no frameing/crc error then save data
942 // BIT7:parity error
943 // BIT6:framing error
944
945 if (status & (BIT7 + BIT6)) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400946 if (status & BIT7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 icount->parity++;
948 else
949 icount->frame++;
950
951 /* discard char if tty control flags say so */
952 if (status & info->ignore_status_mask)
953 continue;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 status &= info->read_status_mask;
956
957 if (status & BIT7)
Alan Cox33f0f882006-01-09 20:54:13 -0800958 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 else if (status & BIT6)
Alan Cox33f0f882006-01-09 20:54:13 -0800960 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Alan Cox33f0f882006-01-09 20:54:13 -0800962 work += tty_insert_flip_char(tty, data, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964 issue_command(info, CHA, CMD_RXFIFO);
965
966 if (debug_level >= DEBUG_LEVEL_ISR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800967 printk("%s(%d):rx_ready_async",
968 __FILE__,__LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
970 __FILE__,__LINE__,icount->rx,icount->brk,
971 icount->parity,icount->frame,icount->overrun);
972 }
Jeff Garzikd12341f2007-10-19 15:45:35 -0400973
Alan Cox33f0f882006-01-09 20:54:13 -0800974 if (work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 tty_flip_buffer_push(tty);
976}
977
978
Alan Coxeeb46132009-01-02 13:48:47 +0000979static void tx_done(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 if (!info->tx_active)
982 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400983
Joe Perches0fab6de2008-04-28 02:14:02 -0700984 info->tx_active = false;
985 info->tx_aborting = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 if (info->params.mode == MGSL_MODE_ASYNC)
988 return;
989
990 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400991 del_timer(&info->tx_timer);
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (info->drop_rts_on_tx_done) {
994 get_signals(info);
995 if (info->serial_signals & SerialSignal_RTS) {
996 info->serial_signals &= ~SerialSignal_RTS;
997 set_signals(info);
998 }
Joe Perches0fab6de2008-04-28 02:14:02 -0700999 info->drop_rts_on_tx_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08001002#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (info->netcount)
1004 hdlcdev_tx_done(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001005 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006#endif
1007 {
Alan Coxeeb46132009-01-02 13:48:47 +00001008 if (tty->stopped || tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 tx_stop(info);
1010 return;
1011 }
1012 info->pending_bh |= BH_TRANSMIT;
1013 }
1014}
1015
Alan Coxeeb46132009-01-02 13:48:47 +00001016static void tx_ready(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 unsigned char fifo_count = 32;
1019 int c;
1020
1021 if (debug_level >= DEBUG_LEVEL_ISR)
1022 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1023
1024 if (info->params.mode == MGSL_MODE_HDLC) {
1025 if (!info->tx_active)
1026 return;
1027 } else {
Alan Coxeeb46132009-01-02 13:48:47 +00001028 if (tty->stopped || tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 tx_stop(info);
1030 return;
1031 }
1032 if (!info->tx_count)
Joe Perches0fab6de2008-04-28 02:14:02 -07001033 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
1036 if (!info->tx_count)
1037 return;
1038
1039 while (info->tx_count && fifo_count) {
1040 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
Jeff Garzikd12341f2007-10-19 15:45:35 -04001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (c == 1) {
1043 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1044 } else {
1045 write_reg16(info, CHA + TXFIFO,
1046 *((unsigned short*)(info->tx_buf + info->tx_get)));
1047 }
1048 info->tx_count -= c;
1049 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1050 fifo_count -= c;
1051 }
1052
1053 if (info->params.mode == MGSL_MODE_ASYNC) {
1054 if (info->tx_count < WAKEUP_CHARS)
1055 info->pending_bh |= BH_TRANSMIT;
1056 issue_command(info, CHA, CMD_TXFIFO);
1057 } else {
1058 if (info->tx_count)
1059 issue_command(info, CHA, CMD_TXFIFO);
1060 else
1061 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1062 }
1063}
1064
Alan Coxeeb46132009-01-02 13:48:47 +00001065static void cts_change(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 get_signals(info);
1068 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1069 irq_disable(info, CHB, IRQ_CTS);
1070 info->icount.cts++;
1071 if (info->serial_signals & SerialSignal_CTS)
1072 info->input_signal_events.cts_up++;
1073 else
1074 info->input_signal_events.cts_down++;
1075 wake_up_interruptible(&info->status_event_wait_q);
1076 wake_up_interruptible(&info->event_wait_q);
1077
Alan Coxeeb46132009-01-02 13:48:47 +00001078 if (info->port.flags & ASYNC_CTS_FLOW) {
1079 if (tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (info->serial_signals & SerialSignal_CTS) {
1081 if (debug_level >= DEBUG_LEVEL_ISR)
1082 printk("CTS tx start...");
Alan Coxeeb46132009-01-02 13:48:47 +00001083 if (tty)
1084 tty->hw_stopped = 0;
1085 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 info->pending_bh |= BH_TRANSMIT;
1087 return;
1088 }
1089 } else {
1090 if (!(info->serial_signals & SerialSignal_CTS)) {
1091 if (debug_level >= DEBUG_LEVEL_ISR)
1092 printk("CTS tx stop...");
Alan Coxeeb46132009-01-02 13:48:47 +00001093 if (tty)
1094 tty->hw_stopped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 tx_stop(info);
1096 }
1097 }
1098 }
1099 info->pending_bh |= BH_STATUS;
1100}
1101
Alan Coxeeb46132009-01-02 13:48:47 +00001102static void dcd_change(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 get_signals(info);
1105 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1106 irq_disable(info, CHB, IRQ_DCD);
1107 info->icount.dcd++;
1108 if (info->serial_signals & SerialSignal_DCD) {
1109 info->input_signal_events.dcd_up++;
1110 }
1111 else
1112 info->input_signal_events.dcd_down++;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08001113#if SYNCLINK_GENERIC_HDLC
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07001114 if (info->netcount) {
1115 if (info->serial_signals & SerialSignal_DCD)
1116 netif_carrier_on(info->netdev);
1117 else
1118 netif_carrier_off(info->netdev);
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120#endif
1121 wake_up_interruptible(&info->status_event_wait_q);
1122 wake_up_interruptible(&info->event_wait_q);
1123
Alan Coxeeb46132009-01-02 13:48:47 +00001124 if (info->port.flags & ASYNC_CHECK_CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (debug_level >= DEBUG_LEVEL_ISR)
1126 printk("%s CD now %s...", info->device_name,
1127 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1128 if (info->serial_signals & SerialSignal_DCD)
Alan Coxeeb46132009-01-02 13:48:47 +00001129 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 else {
1131 if (debug_level >= DEBUG_LEVEL_ISR)
1132 printk("doing serial hangup...");
Alan Coxeeb46132009-01-02 13:48:47 +00001133 if (tty)
1134 tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136 }
1137 info->pending_bh |= BH_STATUS;
1138}
1139
1140static void dsr_change(MGSLPC_INFO *info)
1141{
1142 get_signals(info);
1143 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1144 port_irq_disable(info, PVR_DSR);
1145 info->icount.dsr++;
1146 if (info->serial_signals & SerialSignal_DSR)
1147 info->input_signal_events.dsr_up++;
1148 else
1149 info->input_signal_events.dsr_down++;
1150 wake_up_interruptible(&info->status_event_wait_q);
1151 wake_up_interruptible(&info->event_wait_q);
1152 info->pending_bh |= BH_STATUS;
1153}
1154
1155static void ri_change(MGSLPC_INFO *info)
1156{
1157 get_signals(info);
1158 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1159 port_irq_disable(info, PVR_RI);
1160 info->icount.rng++;
1161 if (info->serial_signals & SerialSignal_RI)
1162 info->input_signal_events.ri_up++;
1163 else
1164 info->input_signal_events.ri_down++;
1165 wake_up_interruptible(&info->status_event_wait_q);
1166 wake_up_interruptible(&info->event_wait_q);
1167 info->pending_bh |= BH_STATUS;
1168}
1169
1170/* Interrupt service routine entry point.
Jeff Garzikd12341f2007-10-19 15:45:35 -04001171 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001173 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 * irq interrupt number that caused interrupt
1175 * dev_id device ID supplied during interrupt registration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
Jeff Garzika6f97b22007-10-31 05:20:49 -04001177static irqreturn_t mgslpc_isr(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Jeff Garzika6f97b22007-10-31 05:20:49 -04001179 MGSLPC_INFO *info = dev_id;
Alan Coxeeb46132009-01-02 13:48:47 +00001180 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 unsigned short isr;
1182 unsigned char gis, pis;
1183 int count=0;
1184
Jeff Garzikd12341f2007-10-19 15:45:35 -04001185 if (debug_level >= DEBUG_LEVEL_ISR)
Jeff Garzika6f97b22007-10-31 05:20:49 -04001186 printk("mgslpc_isr(%d) entry.\n", info->irq_level);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001187
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001188 if (!(info->p_dev->_locked))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return IRQ_HANDLED;
1190
Alan Coxeeb46132009-01-02 13:48:47 +00001191 tty = tty_port_tty_get(&info->port);
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 spin_lock(&info->lock);
1194
1195 while ((gis = read_reg(info, CHA + GIS))) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001196 if (debug_level >= DEBUG_LEVEL_ISR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1198
1199 if ((gis & 0x70) || count > 1000) {
1200 printk("synclink_cs:hardware failed or ejected\n");
1201 break;
1202 }
1203 count++;
1204
1205 if (gis & (BIT1 + BIT0)) {
1206 isr = read_reg16(info, CHB + ISR);
1207 if (isr & IRQ_DCD)
Alan Coxeeb46132009-01-02 13:48:47 +00001208 dcd_change(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (isr & IRQ_CTS)
Alan Coxeeb46132009-01-02 13:48:47 +00001210 cts_change(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 if (gis & (BIT3 + BIT2))
1213 {
1214 isr = read_reg16(info, CHA + ISR);
1215 if (isr & IRQ_TIMER) {
Joe Perches0fab6de2008-04-28 02:14:02 -07001216 info->irq_occurred = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 irq_disable(info, CHA, IRQ_TIMER);
1218 }
1219
Jeff Garzikd12341f2007-10-19 15:45:35 -04001220 /* receive IRQs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (isr & IRQ_EXITHUNT) {
1222 info->icount.exithunt++;
1223 wake_up_interruptible(&info->event_wait_q);
1224 }
1225 if (isr & IRQ_BREAK_ON) {
1226 info->icount.brk++;
Alan Coxeeb46132009-01-02 13:48:47 +00001227 if (info->port.flags & ASYNC_SAK)
1228 do_SAK(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230 if (isr & IRQ_RXTIME) {
1231 issue_command(info, CHA, CMD_RXFIFO_READ);
1232 }
1233 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1234 if (info->params.mode == MGSL_MODE_HDLC)
Jeff Garzikd12341f2007-10-19 15:45:35 -04001235 rx_ready_hdlc(info, isr & IRQ_RXEOM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 else
Alan Coxeeb46132009-01-02 13:48:47 +00001237 rx_ready_async(info, isr & IRQ_RXEOM, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
Jeff Garzikd12341f2007-10-19 15:45:35 -04001240 /* transmit IRQs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (isr & IRQ_UNDERRUN) {
1242 if (info->tx_aborting)
1243 info->icount.txabort++;
1244 else
1245 info->icount.txunder++;
Alan Coxeeb46132009-01-02 13:48:47 +00001246 tx_done(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248 else if (isr & IRQ_ALLSENT) {
1249 info->icount.txok++;
Alan Coxeeb46132009-01-02 13:48:47 +00001250 tx_done(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 else if (isr & IRQ_TXFIFO)
Alan Coxeeb46132009-01-02 13:48:47 +00001253 tx_ready(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255 if (gis & BIT7) {
1256 pis = read_reg(info, CHA + PIS);
1257 if (pis & BIT1)
1258 dsr_change(info);
1259 if (pis & BIT2)
1260 ri_change(info);
1261 }
1262 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001263
1264 /* Request bottom half processing if there's something
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 * for it to do and the bh is not already running
1266 */
1267
1268 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001269 if ( debug_level >= DEBUG_LEVEL_ISR )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 printk("%s(%d):%s queueing bh task.\n",
1271 __FILE__,__LINE__,info->device_name);
1272 schedule_work(&info->task);
Joe Perches0fab6de2008-04-28 02:14:02 -07001273 info->bh_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
1276 spin_unlock(&info->lock);
Alan Coxeeb46132009-01-02 13:48:47 +00001277 tty_kref_put(tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001278
1279 if (debug_level >= DEBUG_LEVEL_ISR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 printk("%s(%d):mgslpc_isr(%d)exit.\n",
Jeff Garzika6f97b22007-10-31 05:20:49 -04001281 __FILE__, __LINE__, info->irq_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 return IRQ_HANDLED;
1284}
1285
1286/* Initialize and start device.
1287 */
Alan Coxeeb46132009-01-02 13:48:47 +00001288static int startup(MGSLPC_INFO * info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 int retval = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (debug_level >= DEBUG_LEVEL_INFO)
1293 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001294
Alan Coxeeb46132009-01-02 13:48:47 +00001295 if (info->port.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (!info->tx_buf) {
1299 /* allocate a page of memory for a transmit buffer */
1300 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1301 if (!info->tx_buf) {
1302 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1303 __FILE__,__LINE__,info->device_name);
1304 return -ENOMEM;
1305 }
1306 }
1307
1308 info->pending_bh = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001309
Paul Fulghuma7482a22005-09-10 00:26:07 -07001310 memset(&info->icount, 0, sizeof(info->icount));
1311
Jiri Slaby40565f12007-02-12 00:52:31 -08001312 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 /* Allocate and claim adapter resources */
1315 retval = claim_resources(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /* perform existance check and diagnostics */
1318 if ( !retval )
1319 retval = adapter_test(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if ( retval ) {
Alan Coxeeb46132009-01-02 13:48:47 +00001322 if (capable(CAP_SYS_ADMIN) && tty)
1323 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 release_resources(info);
1325 return retval;
1326 }
1327
1328 /* program hardware for current parameters */
Alan Coxeeb46132009-01-02 13:48:47 +00001329 mgslpc_change_params(info, tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001330
Alan Coxeeb46132009-01-02 13:48:47 +00001331 if (tty)
1332 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Alan Coxeeb46132009-01-02 13:48:47 +00001334 info->port.flags |= ASYNC_INITIALIZED;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return 0;
1337}
1338
1339/* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1340 */
Alan Coxeeb46132009-01-02 13:48:47 +00001341static void shutdown(MGSLPC_INFO * info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001344
Alan Coxeeb46132009-01-02 13:48:47 +00001345 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return;
1347
1348 if (debug_level >= DEBUG_LEVEL_INFO)
1349 printk("%s(%d):mgslpc_shutdown(%s)\n",
1350 __FILE__,__LINE__, info->device_name );
1351
1352 /* clear status wait queue because status changes */
1353 /* can't happen after shutting down the hardware */
1354 wake_up_interruptible(&info->status_event_wait_q);
1355 wake_up_interruptible(&info->event_wait_q);
1356
Jiri Slaby40565f12007-02-12 00:52:31 -08001357 del_timer_sync(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 if (info->tx_buf) {
1360 free_page((unsigned long) info->tx_buf);
1361 info->tx_buf = NULL;
1362 }
1363
1364 spin_lock_irqsave(&info->lock,flags);
1365
1366 rx_stop(info);
1367 tx_stop(info);
1368
1369 /* TODO:disable interrupts instead of reset to preserve signal states */
1370 reset_device(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001371
Alan Coxeeb46132009-01-02 13:48:47 +00001372 if (!tty || tty->termios->c_cflag & HUPCL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1374 set_signals(info);
1375 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 spin_unlock_irqrestore(&info->lock,flags);
1378
Jeff Garzikd12341f2007-10-19 15:45:35 -04001379 release_resources(info);
1380
Alan Coxeeb46132009-01-02 13:48:47 +00001381 if (tty)
1382 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Alan Coxeeb46132009-01-02 13:48:47 +00001384 info->port.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Alan Coxeeb46132009-01-02 13:48:47 +00001387static void mgslpc_program_hw(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 unsigned long flags;
1390
1391 spin_lock_irqsave(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 rx_stop(info);
1394 tx_stop(info);
1395 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1398 hdlc_mode(info);
1399 else
1400 async_mode(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 set_signals(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 info->dcd_chkcount = 0;
1405 info->cts_chkcount = 0;
1406 info->ri_chkcount = 0;
1407 info->dsr_chkcount = 0;
1408
1409 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1410 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1411 get_signals(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001412
Alan Coxeeb46132009-01-02 13:48:47 +00001413 if (info->netcount || (tty && (tty->termios->c_cflag & CREAD)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 rx_start(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 spin_unlock_irqrestore(&info->lock,flags);
1417}
1418
1419/* Reconfigure adapter based on new parameters
1420 */
Alan Coxeeb46132009-01-02 13:48:47 +00001421static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 unsigned cflag;
1424 int bits_per_char;
1425
Alan Coxeeb46132009-01-02 13:48:47 +00001426 if (!tty || !tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (debug_level >= DEBUG_LEVEL_INFO)
1430 printk("%s(%d):mgslpc_change_params(%s)\n",
1431 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001432
Alan Coxeeb46132009-01-02 13:48:47 +00001433 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 /* if B0 rate (hangup) specified then negate DTR and RTS */
1436 /* otherwise assert DTR and RTS */
1437 if (cflag & CBAUD)
1438 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1439 else
1440 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* byte size and parity */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 switch (cflag & CSIZE) {
1445 case CS5: info->params.data_bits = 5; break;
1446 case CS6: info->params.data_bits = 6; break;
1447 case CS7: info->params.data_bits = 7; break;
1448 case CS8: info->params.data_bits = 8; break;
1449 default: info->params.data_bits = 7; break;
1450 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (cflag & CSTOPB)
1453 info->params.stop_bits = 2;
1454 else
1455 info->params.stop_bits = 1;
1456
1457 info->params.parity = ASYNC_PARITY_NONE;
1458 if (cflag & PARENB) {
1459 if (cflag & PARODD)
1460 info->params.parity = ASYNC_PARITY_ODD;
1461 else
1462 info->params.parity = ASYNC_PARITY_EVEN;
1463#ifdef CMSPAR
1464 if (cflag & CMSPAR)
1465 info->params.parity = ASYNC_PARITY_SPACE;
1466#endif
1467 }
1468
1469 /* calculate number of jiffies to transmit a full
1470 * FIFO (32 bytes) at specified data rate
1471 */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001472 bits_per_char = info->params.data_bits +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 info->params.stop_bits + 1;
1474
1475 /* if port data rate is set to 460800 or less then
1476 * allow tty settings to override, otherwise keep the
1477 * current data rate.
1478 */
1479 if (info->params.data_rate <= 460800) {
Alan Coxeeb46132009-01-02 13:48:47 +00001480 info->params.data_rate = tty_get_baud_rate(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if ( info->params.data_rate ) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001484 info->timeout = (32*HZ*bits_per_char) /
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 info->params.data_rate;
1486 }
1487 info->timeout += HZ/50; /* Add .02 seconds of slop */
1488
1489 if (cflag & CRTSCTS)
Alan Coxeeb46132009-01-02 13:48:47 +00001490 info->port.flags |= ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 else
Alan Coxeeb46132009-01-02 13:48:47 +00001492 info->port.flags &= ~ASYNC_CTS_FLOW;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (cflag & CLOCAL)
Alan Coxeeb46132009-01-02 13:48:47 +00001495 info->port.flags &= ~ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 else
Alan Coxeeb46132009-01-02 13:48:47 +00001497 info->port.flags |= ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 /* process tty input control flags */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 info->read_status_mask = 0;
Alan Coxeeb46132009-01-02 13:48:47 +00001502 if (I_INPCK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 info->read_status_mask |= BIT7 | BIT6;
Alan Coxeeb46132009-01-02 13:48:47 +00001504 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 info->ignore_status_mask |= BIT7 | BIT6;
1506
Alan Coxeeb46132009-01-02 13:48:47 +00001507 mgslpc_program_hw(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
1510/* Add a character to the transmit buffer
1511 */
Alan Coxd7e752e2008-04-30 00:54:04 -07001512static int mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1515 unsigned long flags;
1516
1517 if (debug_level >= DEBUG_LEVEL_INFO) {
1518 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1519 __FILE__,__LINE__,ch,info->device_name);
1520 }
1521
1522 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
Alan Coxd7e752e2008-04-30 00:54:04 -07001523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Eric Sesterhenn326f28e92006-06-25 05:48:48 -07001525 if (!info->tx_buf)
Alan Coxd7e752e2008-04-30 00:54:04 -07001526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 spin_lock_irqsave(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1531 if (info->tx_count < TXBUFSIZE - 1) {
1532 info->tx_buf[info->tx_put++] = ch;
1533 info->tx_put &= TXBUFSIZE-1;
1534 info->tx_count++;
1535 }
1536 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 spin_unlock_irqrestore(&info->lock,flags);
Alan Coxd7e752e2008-04-30 00:54:04 -07001539 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
1542/* Enable transmitter so remaining characters in the
1543 * transmit buffer are sent.
1544 */
1545static void mgslpc_flush_chars(struct tty_struct *tty)
1546{
1547 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1548 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (debug_level >= DEBUG_LEVEL_INFO)
1551 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1552 __FILE__,__LINE__,info->device_name,info->tx_count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1555 return;
1556
1557 if (info->tx_count <= 0 || tty->stopped ||
1558 tty->hw_stopped || !info->tx_buf)
1559 return;
1560
1561 if (debug_level >= DEBUG_LEVEL_INFO)
1562 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1563 __FILE__,__LINE__,info->device_name);
1564
1565 spin_lock_irqsave(&info->lock,flags);
1566 if (!info->tx_active)
Alan Coxeeb46132009-01-02 13:48:47 +00001567 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 spin_unlock_irqrestore(&info->lock,flags);
1569}
1570
1571/* Send a block of data
Jeff Garzikd12341f2007-10-19 15:45:35 -04001572 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001574 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 * tty pointer to tty information structure
1576 * buf pointer to buffer containing send data
1577 * count size of send data in bytes
Jeff Garzikd12341f2007-10-19 15:45:35 -04001578 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * Returns: number of characters written
1580 */
1581static int mgslpc_write(struct tty_struct * tty,
1582 const unsigned char *buf, int count)
1583{
1584 int c, ret = 0;
1585 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1586 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (debug_level >= DEBUG_LEVEL_INFO)
1589 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1590 __FILE__,__LINE__,info->device_name,count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
Eric Sesterhenn326f28e92006-06-25 05:48:48 -07001593 !info->tx_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 goto cleanup;
1595
1596 if (info->params.mode == MGSL_MODE_HDLC) {
1597 if (count > TXBUFSIZE) {
1598 ret = -EIO;
1599 goto cleanup;
1600 }
1601 if (info->tx_active)
1602 goto cleanup;
1603 else if (info->tx_count)
1604 goto start;
1605 }
1606
1607 for (;;) {
1608 c = min(count,
1609 min(TXBUFSIZE - info->tx_count - 1,
1610 TXBUFSIZE - info->tx_put));
1611 if (c <= 0)
1612 break;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 memcpy(info->tx_buf + info->tx_put, buf, c);
1615
1616 spin_lock_irqsave(&info->lock,flags);
1617 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1618 info->tx_count += c;
1619 spin_unlock_irqrestore(&info->lock,flags);
1620
1621 buf += c;
1622 count -= c;
1623 ret += c;
1624 }
1625start:
1626 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1627 spin_lock_irqsave(&info->lock,flags);
1628 if (!info->tx_active)
Alan Coxeeb46132009-01-02 13:48:47 +00001629 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 spin_unlock_irqrestore(&info->lock,flags);
1631 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001632cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (debug_level >= DEBUG_LEVEL_INFO)
1634 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1635 __FILE__,__LINE__,info->device_name,ret);
1636 return ret;
1637}
1638
1639/* Return the count of free bytes in transmit buffer
1640 */
1641static int mgslpc_write_room(struct tty_struct *tty)
1642{
1643 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1644 int ret;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1647 return 0;
1648
1649 if (info->params.mode == MGSL_MODE_HDLC) {
1650 /* HDLC (frame oriented) mode */
1651 if (info->tx_active)
1652 return 0;
1653 else
1654 return HDLC_MAX_FRAME_SIZE;
1655 } else {
1656 ret = TXBUFSIZE - info->tx_count - 1;
1657 if (ret < 0)
1658 ret = 0;
1659 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (debug_level >= DEBUG_LEVEL_INFO)
1662 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1663 __FILE__,__LINE__, info->device_name, ret);
1664 return ret;
1665}
1666
1667/* Return the count of bytes in transmit buffer
1668 */
1669static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1670{
1671 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1672 int rc;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (debug_level >= DEBUG_LEVEL_INFO)
1675 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1676 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1679 return 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (info->params.mode == MGSL_MODE_HDLC)
1682 rc = info->tx_active ? info->max_frame_size : 0;
1683 else
1684 rc = info->tx_count;
1685
1686 if (debug_level >= DEBUG_LEVEL_INFO)
1687 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1688 __FILE__,__LINE__, info->device_name, rc);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return rc;
1691}
1692
1693/* Discard all data in the send buffer
1694 */
1695static void mgslpc_flush_buffer(struct tty_struct *tty)
1696{
1697 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1698 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (debug_level >= DEBUG_LEVEL_INFO)
1701 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1702 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1705 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001706
1707 spin_lock_irqsave(&info->lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001709 del_timer(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 spin_unlock_irqrestore(&info->lock,flags);
1711
1712 wake_up_interruptible(&tty->write_wait);
1713 tty_wakeup(tty);
1714}
1715
1716/* Send a high-priority XON/XOFF character
1717 */
1718static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1719{
1720 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1721 unsigned long flags;
1722
1723 if (debug_level >= DEBUG_LEVEL_INFO)
1724 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1725 __FILE__,__LINE__, info->device_name, ch );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1728 return;
1729
1730 info->x_char = ch;
1731 if (ch) {
1732 spin_lock_irqsave(&info->lock,flags);
1733 if (!info->tx_enabled)
Alan Coxeeb46132009-01-02 13:48:47 +00001734 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 spin_unlock_irqrestore(&info->lock,flags);
1736 }
1737}
1738
1739/* Signal remote device to throttle send data (our receive data)
1740 */
1741static void mgslpc_throttle(struct tty_struct * tty)
1742{
1743 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1744 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (debug_level >= DEBUG_LEVEL_INFO)
1747 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1748 __FILE__,__LINE__, info->device_name );
1749
1750 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1751 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (I_IXOFF(tty))
1754 mgslpc_send_xchar(tty, STOP_CHAR(tty));
Jeff Garzikd12341f2007-10-19 15:45:35 -04001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (tty->termios->c_cflag & CRTSCTS) {
1757 spin_lock_irqsave(&info->lock,flags);
1758 info->serial_signals &= ~SerialSignal_RTS;
1759 set_signals(info);
1760 spin_unlock_irqrestore(&info->lock,flags);
1761 }
1762}
1763
1764/* Signal remote device to stop throttling send data (our receive data)
1765 */
1766static void mgslpc_unthrottle(struct tty_struct * tty)
1767{
1768 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1769 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (debug_level >= DEBUG_LEVEL_INFO)
1772 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1773 __FILE__,__LINE__, info->device_name );
1774
1775 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1776 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (I_IXOFF(tty)) {
1779 if (info->x_char)
1780 info->x_char = 0;
1781 else
1782 mgslpc_send_xchar(tty, START_CHAR(tty));
1783 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (tty->termios->c_cflag & CRTSCTS) {
1786 spin_lock_irqsave(&info->lock,flags);
1787 info->serial_signals |= SerialSignal_RTS;
1788 set_signals(info);
1789 spin_unlock_irqrestore(&info->lock,flags);
1790 }
1791}
1792
1793/* get the current serial statistics
1794 */
1795static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1796{
1797 int err;
1798 if (debug_level >= DEBUG_LEVEL_INFO)
1799 printk("get_params(%s)\n", info->device_name);
Paul Fulghuma7482a22005-09-10 00:26:07 -07001800 if (!user_icount) {
1801 memset(&info->icount, 0, sizeof(info->icount));
1802 } else {
1803 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1804 if (err)
1805 return -EFAULT;
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return 0;
1808}
1809
1810/* get the current serial parameters
1811 */
1812static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1813{
1814 int err;
1815 if (debug_level >= DEBUG_LEVEL_INFO)
1816 printk("get_params(%s)\n", info->device_name);
1817 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1818 if (err)
1819 return -EFAULT;
1820 return 0;
1821}
1822
1823/* set the serial parameters
Jeff Garzikd12341f2007-10-19 15:45:35 -04001824 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001826 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 * info pointer to device instance data
1828 * new_params user buffer containing new serial params
1829 *
1830 * Returns: 0 if success, otherwise error code
1831 */
Alan Coxeeb46132009-01-02 13:48:47 +00001832static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 unsigned long flags;
1835 MGSL_PARAMS tmp_params;
1836 int err;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (debug_level >= DEBUG_LEVEL_INFO)
1839 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1840 info->device_name );
1841 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1842 if (err) {
1843 if ( debug_level >= DEBUG_LEVEL_INFO )
1844 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1845 __FILE__,__LINE__,info->device_name);
1846 return -EFAULT;
1847 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 spin_lock_irqsave(&info->lock,flags);
1850 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1851 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001852
Alan Coxeeb46132009-01-02 13:48:47 +00001853 mgslpc_change_params(info, tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return 0;
1856}
1857
1858static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1859{
1860 int err;
1861 if (debug_level >= DEBUG_LEVEL_INFO)
1862 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1863 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1864 if (err)
1865 return -EFAULT;
1866 return 0;
1867}
1868
1869static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1870{
1871 unsigned long flags;
1872 if (debug_level >= DEBUG_LEVEL_INFO)
1873 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1874 spin_lock_irqsave(&info->lock,flags);
1875 info->idle_mode = idle_mode;
1876 tx_set_idle(info);
1877 spin_unlock_irqrestore(&info->lock,flags);
1878 return 0;
1879}
1880
1881static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1882{
1883 int err;
1884 if (debug_level >= DEBUG_LEVEL_INFO)
1885 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1886 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1887 if (err)
1888 return -EFAULT;
1889 return 0;
1890}
1891
1892static int set_interface(MGSLPC_INFO * info, int if_mode)
1893{
1894 unsigned long flags;
1895 unsigned char val;
1896 if (debug_level >= DEBUG_LEVEL_INFO)
1897 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1898 spin_lock_irqsave(&info->lock,flags);
1899 info->if_mode = if_mode;
1900
1901 val = read_reg(info, PVR) & 0x0f;
1902 switch (info->if_mode)
1903 {
1904 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
1905 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
1906 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
1907 }
1908 write_reg(info, PVR, val);
1909
1910 spin_unlock_irqrestore(&info->lock,flags);
1911 return 0;
1912}
1913
Alan Coxeeb46132009-01-02 13:48:47 +00001914static int set_txenable(MGSLPC_INFO * info, int enable, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (debug_level >= DEBUG_LEVEL_INFO)
1919 printk("set_txenable(%s,%d)\n", info->device_name, enable);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 spin_lock_irqsave(&info->lock,flags);
1922 if (enable) {
1923 if (!info->tx_enabled)
Alan Coxeeb46132009-01-02 13:48:47 +00001924 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 } else {
1926 if (info->tx_enabled)
1927 tx_stop(info);
1928 }
1929 spin_unlock_irqrestore(&info->lock,flags);
1930 return 0;
1931}
1932
1933static int tx_abort(MGSLPC_INFO * info)
1934{
1935 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (debug_level >= DEBUG_LEVEL_INFO)
1938 printk("tx_abort(%s)\n", info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 spin_lock_irqsave(&info->lock,flags);
1941 if (info->tx_active && info->tx_count &&
1942 info->params.mode == MGSL_MODE_HDLC) {
1943 /* clear data count so FIFO is not filled on next IRQ.
1944 * This results in underrun and abort transmission.
1945 */
1946 info->tx_count = info->tx_put = info->tx_get = 0;
Joe Perches0fab6de2008-04-28 02:14:02 -07001947 info->tx_aborting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949 spin_unlock_irqrestore(&info->lock,flags);
1950 return 0;
1951}
1952
1953static int set_rxenable(MGSLPC_INFO * info, int enable)
1954{
1955 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (debug_level >= DEBUG_LEVEL_INFO)
1958 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 spin_lock_irqsave(&info->lock,flags);
1961 if (enable) {
1962 if (!info->rx_enabled)
1963 rx_start(info);
1964 } else {
1965 if (info->rx_enabled)
1966 rx_stop(info);
1967 }
1968 spin_unlock_irqrestore(&info->lock,flags);
1969 return 0;
1970}
1971
1972/* wait for specified event to occur
Jeff Garzikd12341f2007-10-19 15:45:35 -04001973 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 * Arguments: info pointer to device instance data
1975 * mask pointer to bitmask of events to wait for
1976 * Return Value: 0 if successful and bit mask updated with
1977 * of events triggerred,
1978 * otherwise error code
1979 */
1980static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
1981{
1982 unsigned long flags;
1983 int s;
1984 int rc=0;
1985 struct mgsl_icount cprev, cnow;
1986 int events;
1987 int mask;
1988 struct _input_signal_events oldsigs, newsigs;
1989 DECLARE_WAITQUEUE(wait, current);
1990
1991 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
1992 if (rc)
1993 return -EFAULT;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (debug_level >= DEBUG_LEVEL_INFO)
1996 printk("wait_events(%s,%d)\n", info->device_name, mask);
1997
1998 spin_lock_irqsave(&info->lock,flags);
1999
2000 /* return immediately if state matches requested events */
2001 get_signals(info);
2002 s = info->serial_signals;
2003 events = mask &
2004 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2005 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2006 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2007 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2008 if (events) {
2009 spin_unlock_irqrestore(&info->lock,flags);
2010 goto exit;
2011 }
2012
2013 /* save current irq counts */
2014 cprev = info->icount;
2015 oldsigs = info->input_signal_events;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if ((info->params.mode == MGSL_MODE_HDLC) &&
2018 (mask & MgslEvent_ExitHuntMode))
2019 irq_enable(info, CHA, IRQ_EXITHUNT);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 set_current_state(TASK_INTERRUPTIBLE);
2022 add_wait_queue(&info->event_wait_q, &wait);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002025
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 for(;;) {
2028 schedule();
2029 if (signal_pending(current)) {
2030 rc = -ERESTARTSYS;
2031 break;
2032 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 /* get current irq counts */
2035 spin_lock_irqsave(&info->lock,flags);
2036 cnow = info->icount;
2037 newsigs = info->input_signal_events;
2038 set_current_state(TASK_INTERRUPTIBLE);
2039 spin_unlock_irqrestore(&info->lock,flags);
2040
2041 /* if no change, wait aborted for some reason */
2042 if (newsigs.dsr_up == oldsigs.dsr_up &&
2043 newsigs.dsr_down == oldsigs.dsr_down &&
2044 newsigs.dcd_up == oldsigs.dcd_up &&
2045 newsigs.dcd_down == oldsigs.dcd_down &&
2046 newsigs.cts_up == oldsigs.cts_up &&
2047 newsigs.cts_down == oldsigs.cts_down &&
2048 newsigs.ri_up == oldsigs.ri_up &&
2049 newsigs.ri_down == oldsigs.ri_down &&
2050 cnow.exithunt == cprev.exithunt &&
2051 cnow.rxidle == cprev.rxidle) {
2052 rc = -EIO;
2053 break;
2054 }
2055
2056 events = mask &
2057 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2058 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2059 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2060 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2061 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2062 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2063 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2064 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2065 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2066 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2067 if (events)
2068 break;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 cprev = cnow;
2071 oldsigs = newsigs;
2072 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 remove_wait_queue(&info->event_wait_q, &wait);
2075 set_current_state(TASK_RUNNING);
2076
2077 if (mask & MgslEvent_ExitHuntMode) {
2078 spin_lock_irqsave(&info->lock,flags);
2079 if (!waitqueue_active(&info->event_wait_q))
2080 irq_disable(info, CHA, IRQ_EXITHUNT);
2081 spin_unlock_irqrestore(&info->lock,flags);
2082 }
2083exit:
2084 if (rc == 0)
2085 PUT_USER(rc, events, mask_ptr);
2086 return rc;
2087}
2088
2089static int modem_input_wait(MGSLPC_INFO *info,int arg)
2090{
2091 unsigned long flags;
2092 int rc;
2093 struct mgsl_icount cprev, cnow;
2094 DECLARE_WAITQUEUE(wait, current);
2095
2096 /* save current irq counts */
2097 spin_lock_irqsave(&info->lock,flags);
2098 cprev = info->icount;
2099 add_wait_queue(&info->status_event_wait_q, &wait);
2100 set_current_state(TASK_INTERRUPTIBLE);
2101 spin_unlock_irqrestore(&info->lock,flags);
2102
2103 for(;;) {
2104 schedule();
2105 if (signal_pending(current)) {
2106 rc = -ERESTARTSYS;
2107 break;
2108 }
2109
2110 /* get new irq counts */
2111 spin_lock_irqsave(&info->lock,flags);
2112 cnow = info->icount;
2113 set_current_state(TASK_INTERRUPTIBLE);
2114 spin_unlock_irqrestore(&info->lock,flags);
2115
2116 /* if no change, wait aborted for some reason */
2117 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2118 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2119 rc = -EIO;
2120 break;
2121 }
2122
2123 /* check for change in caller specified modem input */
2124 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2125 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2126 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2127 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2128 rc = 0;
2129 break;
2130 }
2131
2132 cprev = cnow;
2133 }
2134 remove_wait_queue(&info->status_event_wait_q, &wait);
2135 set_current_state(TASK_RUNNING);
2136 return rc;
2137}
2138
2139/* return the state of the serial control and status signals
2140 */
2141static int tiocmget(struct tty_struct *tty, struct file *file)
2142{
2143 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2144 unsigned int result;
2145 unsigned long flags;
2146
2147 spin_lock_irqsave(&info->lock,flags);
2148 get_signals(info);
2149 spin_unlock_irqrestore(&info->lock,flags);
2150
2151 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2152 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2153 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2154 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2155 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2156 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2157
2158 if (debug_level >= DEBUG_LEVEL_INFO)
2159 printk("%s(%d):%s tiocmget() value=%08X\n",
2160 __FILE__,__LINE__, info->device_name, result );
2161 return result;
2162}
2163
2164/* set modem control signals (DTR/RTS)
2165 */
2166static int tiocmset(struct tty_struct *tty, struct file *file,
2167 unsigned int set, unsigned int clear)
2168{
2169 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2170 unsigned long flags;
2171
2172 if (debug_level >= DEBUG_LEVEL_INFO)
2173 printk("%s(%d):%s tiocmset(%x,%x)\n",
2174 __FILE__,__LINE__,info->device_name, set, clear);
2175
2176 if (set & TIOCM_RTS)
2177 info->serial_signals |= SerialSignal_RTS;
2178 if (set & TIOCM_DTR)
2179 info->serial_signals |= SerialSignal_DTR;
2180 if (clear & TIOCM_RTS)
2181 info->serial_signals &= ~SerialSignal_RTS;
2182 if (clear & TIOCM_DTR)
2183 info->serial_signals &= ~SerialSignal_DTR;
2184
2185 spin_lock_irqsave(&info->lock,flags);
2186 set_signals(info);
2187 spin_unlock_irqrestore(&info->lock,flags);
2188
2189 return 0;
2190}
2191
2192/* Set or clear transmit break condition
2193 *
2194 * Arguments: tty pointer to tty instance data
2195 * break_state -1=set break condition, 0=clear
2196 */
Alan Cox9e989662008-07-22 11:18:03 +01002197static int mgslpc_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
2199 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2200 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (debug_level >= DEBUG_LEVEL_INFO)
2203 printk("%s(%d):mgslpc_break(%s,%d)\n",
2204 __FILE__,__LINE__, info->device_name, break_state);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
Alan Cox9e989662008-07-22 11:18:03 +01002207 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 spin_lock_irqsave(&info->lock,flags);
2210 if (break_state == -1)
2211 set_reg_bits(info, CHA+DAFO, BIT6);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002212 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 clear_reg_bits(info, CHA+DAFO, BIT6);
2214 spin_unlock_irqrestore(&info->lock,flags);
Alan Cox9e989662008-07-22 11:18:03 +01002215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
2218/* Service an IOCTL request
Jeff Garzikd12341f2007-10-19 15:45:35 -04002219 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04002221 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 * tty pointer to tty instance data
2223 * file pointer to associated file object for device
2224 * cmd IOCTL command code
2225 * arg command argument/context
Jeff Garzikd12341f2007-10-19 15:45:35 -04002226 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 * Return Value: 0 if success, otherwise error code
2228 */
2229static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2230 unsigned int cmd, unsigned long arg)
2231{
2232 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
Alan Coxeeb46132009-01-02 13:48:47 +00002233 int error;
2234 struct mgsl_icount cnow; /* kernel counter temps */
2235 struct serial_icounter_struct __user *p_cuser; /* user space */
2236 void __user *argp = (void __user *)arg;
2237 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (debug_level >= DEBUG_LEVEL_INFO)
2240 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2241 info->device_name, cmd );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2244 return -ENODEV;
2245
2246 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2247 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2248 if (tty->flags & (1 << TTY_IO_ERROR))
2249 return -EIO;
2250 }
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 switch (cmd) {
2253 case MGSL_IOCGPARAMS:
2254 return get_params(info, argp);
2255 case MGSL_IOCSPARAMS:
Alan Coxeeb46132009-01-02 13:48:47 +00002256 return set_params(info, argp, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 case MGSL_IOCGTXIDLE:
2258 return get_txidle(info, argp);
2259 case MGSL_IOCSTXIDLE:
2260 return set_txidle(info, (int)arg);
2261 case MGSL_IOCGIF:
2262 return get_interface(info, argp);
2263 case MGSL_IOCSIF:
2264 return set_interface(info,(int)arg);
2265 case MGSL_IOCTXENABLE:
Alan Coxeeb46132009-01-02 13:48:47 +00002266 return set_txenable(info,(int)arg, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 case MGSL_IOCRXENABLE:
2268 return set_rxenable(info,(int)arg);
2269 case MGSL_IOCTXABORT:
2270 return tx_abort(info);
2271 case MGSL_IOCGSTATS:
2272 return get_stats(info, argp);
2273 case MGSL_IOCWAITEVENT:
2274 return wait_events(info, argp);
2275 case TIOCMIWAIT:
2276 return modem_input_wait(info,(int)arg);
2277 case TIOCGICOUNT:
2278 spin_lock_irqsave(&info->lock,flags);
2279 cnow = info->icount;
2280 spin_unlock_irqrestore(&info->lock,flags);
2281 p_cuser = argp;
2282 PUT_USER(error,cnow.cts, &p_cuser->cts);
2283 if (error) return error;
2284 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2285 if (error) return error;
2286 PUT_USER(error,cnow.rng, &p_cuser->rng);
2287 if (error) return error;
2288 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2289 if (error) return error;
2290 PUT_USER(error,cnow.rx, &p_cuser->rx);
2291 if (error) return error;
2292 PUT_USER(error,cnow.tx, &p_cuser->tx);
2293 if (error) return error;
2294 PUT_USER(error,cnow.frame, &p_cuser->frame);
2295 if (error) return error;
2296 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2297 if (error) return error;
2298 PUT_USER(error,cnow.parity, &p_cuser->parity);
2299 if (error) return error;
2300 PUT_USER(error,cnow.brk, &p_cuser->brk);
2301 if (error) return error;
2302 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2303 if (error) return error;
2304 return 0;
2305 default:
2306 return -ENOIOCTLCMD;
2307 }
2308 return 0;
2309}
2310
2311/* Set new termios settings
Jeff Garzikd12341f2007-10-19 15:45:35 -04002312 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04002314 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 * tty pointer to tty structure
2316 * termios pointer to buffer to hold returned old termios
2317 */
Alan Cox606d0992006-12-08 02:38:45 -08002318static void mgslpc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
2320 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2321 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (debug_level >= DEBUG_LEVEL_INFO)
2324 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2325 tty->driver->name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 /* just return if nothing has changed */
2328 if ((tty->termios->c_cflag == old_termios->c_cflag)
Jeff Garzikd12341f2007-10-19 15:45:35 -04002329 && (RELEVANT_IFLAG(tty->termios->c_iflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 == RELEVANT_IFLAG(old_termios->c_iflag)))
2331 return;
2332
Alan Coxeeb46132009-01-02 13:48:47 +00002333 mgslpc_change_params(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 /* Handle transition to B0 status */
2336 if (old_termios->c_cflag & CBAUD &&
2337 !(tty->termios->c_cflag & CBAUD)) {
2338 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2339 spin_lock_irqsave(&info->lock,flags);
2340 set_signals(info);
2341 spin_unlock_irqrestore(&info->lock,flags);
2342 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 /* Handle transition away from B0 status */
2345 if (!(old_termios->c_cflag & CBAUD) &&
2346 tty->termios->c_cflag & CBAUD) {
2347 info->serial_signals |= SerialSignal_DTR;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002348 if (!(tty->termios->c_cflag & CRTSCTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 !test_bit(TTY_THROTTLED, &tty->flags)) {
2350 info->serial_signals |= SerialSignal_RTS;
2351 }
2352 spin_lock_irqsave(&info->lock,flags);
2353 set_signals(info);
2354 spin_unlock_irqrestore(&info->lock,flags);
2355 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 /* Handle turning off CRTSCTS */
2358 if (old_termios->c_cflag & CRTSCTS &&
2359 !(tty->termios->c_cflag & CRTSCTS)) {
2360 tty->hw_stopped = 0;
2361 tx_release(tty);
2362 }
2363}
2364
2365static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2366{
2367 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
Alan Coxeeb46132009-01-02 13:48:47 +00002368 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2371 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (debug_level >= DEBUG_LEVEL_INFO)
2374 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
Alan Coxeeb46132009-01-02 13:48:47 +00002375 __FILE__,__LINE__, info->device_name, port->count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002376
Alan Coxeeb46132009-01-02 13:48:47 +00002377 WARN_ON(!port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Alan Coxeeb46132009-01-02 13:48:47 +00002379 if (tty_port_close_start(port, tty, filp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 goto cleanup;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002381
Alan Coxeeb46132009-01-02 13:48:47 +00002382 if (port->flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 mgslpc_wait_until_sent(tty, info->timeout);
2384
Alan Cox978e5952008-04-30 00:53:59 -07002385 mgslpc_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Alan Cox978e5952008-04-30 00:53:59 -07002387 tty_ldisc_flush(tty);
Alan Coxeeb46132009-01-02 13:48:47 +00002388 shutdown(info, tty);
2389
2390 tty_port_close_end(port, tty);
2391 tty_port_tty_set(port, NULL);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002392cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (debug_level >= DEBUG_LEVEL_INFO)
2394 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
Alan Coxeeb46132009-01-02 13:48:47 +00002395 tty->driver->name, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396}
2397
2398/* Wait until the transmitter is empty.
2399 */
2400static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2401{
2402 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2403 unsigned long orig_jiffies, char_time;
2404
2405 if (!info )
2406 return;
2407
2408 if (debug_level >= DEBUG_LEVEL_INFO)
2409 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2410 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2413 return;
2414
Alan Coxeeb46132009-01-02 13:48:47 +00002415 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 goto exit;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 orig_jiffies = jiffies;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 /* Set check interval to 1/5 of estimated time to
2421 * send a character, and make it at least 1. The check
2422 * interval should also be less than the timeout.
2423 * Note: use tight timings here to satisfy the NIST-PCTS.
Jeff Garzikd12341f2007-10-19 15:45:35 -04002424 */
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if ( info->params.data_rate ) {
2427 char_time = info->timeout/(32 * 5);
2428 if (!char_time)
2429 char_time++;
2430 } else
2431 char_time = 1;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (timeout)
2434 char_time = min_t(unsigned long, char_time, timeout);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (info->params.mode == MGSL_MODE_HDLC) {
2437 while (info->tx_active) {
2438 msleep_interruptible(jiffies_to_msecs(char_time));
2439 if (signal_pending(current))
2440 break;
2441 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2442 break;
2443 }
2444 } else {
2445 while ((info->tx_count || info->tx_active) &&
2446 info->tx_enabled) {
2447 msleep_interruptible(jiffies_to_msecs(char_time));
2448 if (signal_pending(current))
2449 break;
2450 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2451 break;
2452 }
2453 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455exit:
2456 if (debug_level >= DEBUG_LEVEL_INFO)
2457 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2458 __FILE__,__LINE__, info->device_name );
2459}
2460
2461/* Called by tty_hangup() when a hangup is signaled.
2462 * This is the same as closing all open files for the port.
2463 */
2464static void mgslpc_hangup(struct tty_struct *tty)
2465{
2466 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (debug_level >= DEBUG_LEVEL_INFO)
2469 printk("%s(%d):mgslpc_hangup(%s)\n",
2470 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2473 return;
2474
2475 mgslpc_flush_buffer(tty);
Alan Coxeeb46132009-01-02 13:48:47 +00002476 shutdown(info, tty);
2477 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
2479
Alan Coxeeb46132009-01-02 13:48:47 +00002480static int carrier_raised(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Alan Coxeeb46132009-01-02 13:48:47 +00002482 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2483 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002484
Alan Coxeeb46132009-01-02 13:48:47 +00002485 spin_lock_irqsave(&info->lock,flags);
2486 get_signals(info);
2487 spin_unlock_irqrestore(&info->lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Alan Coxeeb46132009-01-02 13:48:47 +00002489 if (info->serial_signals & SerialSignal_DCD)
2490 return 1;
2491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Alan Coxfcc8ac12009-06-11 12:24:17 +01002494static void dtr_rts(struct tty_port *port, int onoff)
Alan Coxeeb46132009-01-02 13:48:47 +00002495{
2496 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2497 unsigned long flags;
2498
2499 spin_lock_irqsave(&info->lock,flags);
Alan Coxfcc8ac12009-06-11 12:24:17 +01002500 if (onoff)
2501 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2502 else
2503 info->serial_signals &= ~SerialSignal_RTS + SerialSignal_DTR;
Alan Coxeeb46132009-01-02 13:48:47 +00002504 set_signals(info);
2505 spin_unlock_irqrestore(&info->lock,flags);
2506}
2507
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2510{
2511 MGSLPC_INFO *info;
Alan Coxeeb46132009-01-02 13:48:47 +00002512 struct tty_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 int retval, line;
2514 unsigned long flags;
2515
Jeff Garzikd12341f2007-10-19 15:45:35 -04002516 /* verify range of specified line number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 line = tty->index;
2518 if ((line < 0) || (line >= mgslpc_device_count)) {
2519 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2520 __FILE__,__LINE__,line);
2521 return -ENODEV;
2522 }
2523
2524 /* find the info structure for the specified line */
2525 info = mgslpc_device_list;
2526 while(info && info->line != line)
2527 info = info->next_device;
2528 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2529 return -ENODEV;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002530
Alan Coxeeb46132009-01-02 13:48:47 +00002531 port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 tty->driver_data = info;
Alan Coxeeb46132009-01-02 13:48:47 +00002533 tty_port_tty_set(port, tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (debug_level >= DEBUG_LEVEL_INFO)
2536 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
Alan Coxeeb46132009-01-02 13:48:47 +00002537 __FILE__,__LINE__,tty->driver->name, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 /* If port is closing, signal caller to try again */
Alan Coxeeb46132009-01-02 13:48:47 +00002540 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING){
2541 if (port->flags & ASYNC_CLOSING)
2542 interruptible_sleep_on(&port->close_wait);
2543 retval = ((port->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 -EAGAIN : -ERESTARTSYS);
2545 goto cleanup;
2546 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002547
Alan Coxeeb46132009-01-02 13:48:47 +00002548 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
2550 spin_lock_irqsave(&info->netlock, flags);
2551 if (info->netcount) {
2552 retval = -EBUSY;
2553 spin_unlock_irqrestore(&info->netlock, flags);
2554 goto cleanup;
2555 }
Alan Coxeeb46132009-01-02 13:48:47 +00002556 spin_lock(&port->lock);
2557 port->count++;
2558 spin_unlock(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 spin_unlock_irqrestore(&info->netlock, flags);
2560
Alan Coxeeb46132009-01-02 13:48:47 +00002561 if (port->count == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 /* 1st open on this device, init hardware */
Alan Coxeeb46132009-01-02 13:48:47 +00002563 retval = startup(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (retval < 0)
2565 goto cleanup;
2566 }
2567
Alan Coxeeb46132009-01-02 13:48:47 +00002568 retval = tty_port_block_til_ready(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (retval) {
2570 if (debug_level >= DEBUG_LEVEL_INFO)
2571 printk("%s(%d):block_til_ready(%s) returned %d\n",
2572 __FILE__,__LINE__, info->device_name, retval);
2573 goto cleanup;
2574 }
2575
2576 if (debug_level >= DEBUG_LEVEL_INFO)
2577 printk("%s(%d):mgslpc_open(%s) success\n",
2578 __FILE__,__LINE__, info->device_name);
2579 retval = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002580
2581cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return retval;
2583}
2584
2585/*
2586 * /proc fs routines....
2587 */
2588
Alexey Dobriyan87687142009-03-31 15:19:17 -07002589static inline void line_info(struct seq_file *m, MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 char stat_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 unsigned long flags;
2593
Alexey Dobriyan87687142009-03-31 15:19:17 -07002594 seq_printf(m, "%s:io:%04X irq:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 info->device_name, info->io_base, info->irq_level);
2596
2597 /* output current serial signal states */
2598 spin_lock_irqsave(&info->lock,flags);
2599 get_signals(info);
2600 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 stat_buf[0] = 0;
2603 stat_buf[1] = 0;
2604 if (info->serial_signals & SerialSignal_RTS)
2605 strcat(stat_buf, "|RTS");
2606 if (info->serial_signals & SerialSignal_CTS)
2607 strcat(stat_buf, "|CTS");
2608 if (info->serial_signals & SerialSignal_DTR)
2609 strcat(stat_buf, "|DTR");
2610 if (info->serial_signals & SerialSignal_DSR)
2611 strcat(stat_buf, "|DSR");
2612 if (info->serial_signals & SerialSignal_DCD)
2613 strcat(stat_buf, "|CD");
2614 if (info->serial_signals & SerialSignal_RI)
2615 strcat(stat_buf, "|RI");
2616
2617 if (info->params.mode == MGSL_MODE_HDLC) {
Alexey Dobriyan87687142009-03-31 15:19:17 -07002618 seq_printf(m, " HDLC txok:%d rxok:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 info->icount.txok, info->icount.rxok);
2620 if (info->icount.txunder)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002621 seq_printf(m, " txunder:%d", info->icount.txunder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (info->icount.txabort)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002623 seq_printf(m, " txabort:%d", info->icount.txabort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (info->icount.rxshort)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002625 seq_printf(m, " rxshort:%d", info->icount.rxshort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (info->icount.rxlong)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002627 seq_printf(m, " rxlong:%d", info->icount.rxlong);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 if (info->icount.rxover)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002629 seq_printf(m, " rxover:%d", info->icount.rxover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (info->icount.rxcrc)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002631 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 } else {
Alexey Dobriyan87687142009-03-31 15:19:17 -07002633 seq_printf(m, " ASYNC tx:%d rx:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 info->icount.tx, info->icount.rx);
2635 if (info->icount.frame)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002636 seq_printf(m, " fe:%d", info->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (info->icount.parity)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002638 seq_printf(m, " pe:%d", info->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (info->icount.brk)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002640 seq_printf(m, " brk:%d", info->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (info->icount.overrun)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002642 seq_printf(m, " oe:%d", info->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 /* Append serial signal status to end */
Alexey Dobriyan87687142009-03-31 15:19:17 -07002646 seq_printf(m, " %s\n", stat_buf+1);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002647
Alexey Dobriyan87687142009-03-31 15:19:17 -07002648 seq_printf(m, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 info->tx_active,info->bh_requested,info->bh_running,
2650 info->pending_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
2653/* Called to print information about devices
2654 */
Alexey Dobriyan87687142009-03-31 15:19:17 -07002655static int mgslpc_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 MGSLPC_INFO *info;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002658
Alexey Dobriyan87687142009-03-31 15:19:17 -07002659 seq_printf(m, "synclink driver:%s\n", driver_version);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 info = mgslpc_device_list;
2662 while( info ) {
Alexey Dobriyan87687142009-03-31 15:19:17 -07002663 line_info(m, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 info = info->next_device;
2665 }
Alexey Dobriyan87687142009-03-31 15:19:17 -07002666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
Alexey Dobriyan87687142009-03-31 15:19:17 -07002669static int mgslpc_proc_open(struct inode *inode, struct file *file)
2670{
2671 return single_open(file, mgslpc_proc_show, NULL);
2672}
2673
2674static const struct file_operations mgslpc_proc_fops = {
2675 .owner = THIS_MODULE,
2676 .open = mgslpc_proc_open,
2677 .read = seq_read,
2678 .llseek = seq_lseek,
2679 .release = single_release,
2680};
2681
Peter Hagervallcdaad342006-06-23 02:06:04 -07002682static int rx_alloc_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 /* each buffer has header and data */
2685 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2686
2687 /* calculate total allocation size for 8 buffers */
2688 info->rx_buf_total_size = info->rx_buf_size * 8;
2689
2690 /* limit total allocated memory */
2691 if (info->rx_buf_total_size > 0x10000)
2692 info->rx_buf_total_size = 0x10000;
2693
2694 /* calculate number of buffers */
2695 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2696
2697 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2698 if (info->rx_buf == NULL)
2699 return -ENOMEM;
2700
2701 rx_reset_buffers(info);
2702 return 0;
2703}
2704
Peter Hagervallcdaad342006-06-23 02:06:04 -07002705static void rx_free_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
Jesper Juhl735d5662005-11-07 01:01:29 -08002707 kfree(info->rx_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 info->rx_buf = NULL;
2709}
2710
Peter Hagervallcdaad342006-06-23 02:06:04 -07002711static int claim_resources(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
2713 if (rx_alloc_buffers(info) < 0 ) {
2714 printk( "Cant allocate rx buffer %s\n", info->device_name);
2715 release_resources(info);
2716 return -ENODEV;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return 0;
2719}
2720
Peter Hagervallcdaad342006-06-23 02:06:04 -07002721static void release_resources(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
2723 if (debug_level >= DEBUG_LEVEL_INFO)
2724 printk("release_resources(%s)\n", info->device_name);
2725 rx_free_buffers(info);
2726}
2727
2728/* Add the specified device instance data structure to the
2729 * global linked list of devices and increment the device count.
Jeff Garzikd12341f2007-10-19 15:45:35 -04002730 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 * Arguments: info pointer to device instance data
2732 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07002733static void mgslpc_add_device(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
2735 info->next_device = NULL;
2736 info->line = mgslpc_device_count;
2737 sprintf(info->device_name,"ttySLP%d",info->line);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (info->line < MAX_DEVICE_COUNT) {
2740 if (maxframe[info->line])
2741 info->max_frame_size = maxframe[info->line];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
2743
2744 mgslpc_device_count++;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 if (!mgslpc_device_list)
2747 mgslpc_device_list = info;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002748 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 MGSLPC_INFO *current_dev = mgslpc_device_list;
2750 while( current_dev->next_device )
2751 current_dev = current_dev->next_device;
2752 current_dev->next_device = info;
2753 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (info->max_frame_size < 4096)
2756 info->max_frame_size = 4096;
2757 else if (info->max_frame_size > 65535)
2758 info->max_frame_size = 65535;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2761 info->device_name, info->io_base, info->irq_level);
2762
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002763#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 hdlcdev_init(info);
2765#endif
2766}
2767
Peter Hagervallcdaad342006-06-23 02:06:04 -07002768static void mgslpc_remove_device(MGSLPC_INFO *remove_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
2770 MGSLPC_INFO *info = mgslpc_device_list;
2771 MGSLPC_INFO *last = NULL;
2772
2773 while(info) {
2774 if (info == remove_info) {
2775 if (last)
2776 last->next_device = info->next_device;
2777 else
2778 mgslpc_device_list = info->next_device;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002779#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 hdlcdev_exit(info);
2781#endif
2782 release_resources(info);
2783 kfree(info);
2784 mgslpc_device_count--;
2785 return;
2786 }
2787 last = info;
2788 info = info->next_device;
2789 }
2790}
2791
Dominik Brodowski4af48c82005-06-27 16:28:42 -07002792static struct pcmcia_device_id mgslpc_ids[] = {
2793 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
2794 PCMCIA_DEVICE_NULL
2795};
2796MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798static struct pcmcia_driver mgslpc_driver = {
2799 .owner = THIS_MODULE,
2800 .drv = {
2801 .name = "synclink_cs",
2802 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02002803 .probe = mgslpc_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01002804 .remove = mgslpc_detach,
Dominik Brodowski4af48c82005-06-27 16:28:42 -07002805 .id_table = mgslpc_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +01002806 .suspend = mgslpc_suspend,
2807 .resume = mgslpc_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808};
2809
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002810static const struct tty_operations mgslpc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 .open = mgslpc_open,
2812 .close = mgslpc_close,
2813 .write = mgslpc_write,
2814 .put_char = mgslpc_put_char,
2815 .flush_chars = mgslpc_flush_chars,
2816 .write_room = mgslpc_write_room,
2817 .chars_in_buffer = mgslpc_chars_in_buffer,
2818 .flush_buffer = mgslpc_flush_buffer,
2819 .ioctl = mgslpc_ioctl,
2820 .throttle = mgslpc_throttle,
2821 .unthrottle = mgslpc_unthrottle,
2822 .send_xchar = mgslpc_send_xchar,
2823 .break_ctl = mgslpc_break,
2824 .wait_until_sent = mgslpc_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 .set_termios = mgslpc_set_termios,
2826 .stop = tx_pause,
2827 .start = tx_release,
2828 .hangup = mgslpc_hangup,
2829 .tiocmget = tiocmget,
2830 .tiocmset = tiocmset,
Alexey Dobriyan87687142009-03-31 15:19:17 -07002831 .proc_fops = &mgslpc_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832};
2833
2834static void synclink_cs_cleanup(void)
2835{
2836 int rc;
2837
2838 printk("Unloading %s: version %s\n", driver_name, driver_version);
2839
2840 while(mgslpc_device_list)
2841 mgslpc_remove_device(mgslpc_device_list);
2842
2843 if (serial_driver) {
2844 if ((rc = tty_unregister_driver(serial_driver)))
2845 printk("%s(%d) failed to unregister tty driver err=%d\n",
2846 __FILE__,__LINE__,rc);
2847 put_tty_driver(serial_driver);
2848 }
2849
2850 pcmcia_unregister_driver(&mgslpc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
2853static int __init synclink_cs_init(void)
2854{
2855 int rc;
2856
2857 if (break_on_load) {
2858 mgslpc_get_text_ptr();
2859 BREAKPOINT();
2860 }
2861
2862 printk("%s %s\n", driver_name, driver_version);
2863
2864 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
2865 return rc;
2866
2867 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
2868 if (!serial_driver) {
2869 rc = -ENOMEM;
2870 goto error;
2871 }
2872
2873 /* Initialize the tty_driver structure */
Jeff Garzikd12341f2007-10-19 15:45:35 -04002874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 serial_driver->owner = THIS_MODULE;
2876 serial_driver->driver_name = "synclink_cs";
2877 serial_driver->name = "ttySLP";
2878 serial_driver->major = ttymajor;
2879 serial_driver->minor_start = 64;
2880 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2881 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2882 serial_driver->init_termios = tty_std_termios;
2883 serial_driver->init_termios.c_cflag =
2884 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2885 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2886 tty_set_operations(serial_driver, &mgslpc_ops);
2887
2888 if ((rc = tty_register_driver(serial_driver)) < 0) {
2889 printk("%s(%d):Couldn't register serial driver\n",
2890 __FILE__,__LINE__);
2891 put_tty_driver(serial_driver);
2892 serial_driver = NULL;
2893 goto error;
2894 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 printk("%s %s, tty major#%d\n",
2897 driver_name, driver_version,
2898 serial_driver->major);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return 0;
2901
2902error:
2903 synclink_cs_cleanup();
2904 return rc;
2905}
2906
Jeff Garzikd12341f2007-10-19 15:45:35 -04002907static void __exit synclink_cs_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
2909 synclink_cs_cleanup();
2910}
2911
2912module_init(synclink_cs_init);
2913module_exit(synclink_cs_exit);
2914
2915static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
2916{
2917 unsigned int M, N;
2918 unsigned char val;
2919
Jeff Garzikd12341f2007-10-19 15:45:35 -04002920 /* note:standard BRG mode is broken in V3.2 chip
2921 * so enhanced mode is always used
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 */
2923
2924 if (rate) {
2925 N = 3686400 / rate;
2926 if (!N)
2927 N = 1;
2928 N >>= 1;
2929 for (M = 1; N > 64 && M < 16; M++)
2930 N >>= 1;
2931 N--;
2932
2933 /* BGR[5..0] = N
2934 * BGR[9..6] = M
2935 * BGR[7..0] contained in BGR register
2936 * BGR[9..8] contained in CCR2[7..6]
2937 * divisor = (N+1)*2^M
2938 *
2939 * Note: M *must* not be zero (causes asymetric duty cycle)
Jeff Garzikd12341f2007-10-19 15:45:35 -04002940 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 write_reg(info, (unsigned char) (channel + BGR),
2942 (unsigned char) ((M << 6) + N));
2943 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
2944 val |= ((M << 4) & 0xc0);
2945 write_reg(info, (unsigned char) (channel + CCR2), val);
2946 }
2947}
2948
2949/* Enabled the AUX clock output at the specified frequency.
2950 */
2951static void enable_auxclk(MGSLPC_INFO *info)
2952{
2953 unsigned char val;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 /* MODE
2956 *
2957 * 07..06 MDS[1..0] 10 = transparent HDLC mode
2958 * 05 ADM Address Mode, 0 = no addr recognition
2959 * 04 TMD Timer Mode, 0 = external
2960 * 03 RAC Receiver Active, 0 = inactive
2961 * 02 RTS 0=RTS active during xmit, 1=RTS always active
2962 * 01 TRS Timer Resolution, 1=512
2963 * 00 TLP Test Loop, 0 = no loop
2964 *
2965 * 1000 0010
Jeff Garzikd12341f2007-10-19 15:45:35 -04002966 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 val = 0x82;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002968
2969 /* channel B RTS is used to enable AUXCLK driver on SP505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
2971 val |= BIT2;
2972 write_reg(info, CHB + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 /* CCR0
2975 *
2976 * 07 PU Power Up, 1=active, 0=power down
2977 * 06 MCE Master Clock Enable, 1=enabled
2978 * 05 Reserved, 0
2979 * 04..02 SC[2..0] Encoding
2980 * 01..00 SM[1..0] Serial Mode, 00=HDLC
2981 *
2982 * 11000000
Jeff Garzikd12341f2007-10-19 15:45:35 -04002983 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 write_reg(info, CHB + CCR0, 0xc0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 /* CCR1
2987 *
2988 * 07 SFLG Shared Flag, 0 = disable shared flags
2989 * 06 GALP Go Active On Loop, 0 = not used
2990 * 05 GLP Go On Loop, 0 = not used
2991 * 04 ODS Output Driver Select, 1=TxD is push-pull output
2992 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
2993 * 02..00 CM[2..0] Clock Mode
2994 *
2995 * 0001 0111
Jeff Garzikd12341f2007-10-19 15:45:35 -04002996 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 write_reg(info, CHB + CCR1, 0x17);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002998
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 /* CCR2 (Channel B)
3000 *
3001 * 07..06 BGR[9..8] Baud rate bits 9..8
3002 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3003 * 04 SSEL Clock source select, 1=submode b
3004 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3005 * 02 RWX Read/Write Exchange 0=disabled
3006 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3007 * 00 DIV, data inversion 0=disabled, 1=enabled
3008 *
3009 * 0011 1000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3012 write_reg(info, CHB + CCR2, 0x38);
3013 else
3014 write_reg(info, CHB + CCR2, 0x30);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 /* CCR4
3017 *
3018 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3019 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3020 * 05 TST1 Test Pin, 0=normal operation
3021 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3022 * 03..02 Reserved, must be 0
3023 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3024 *
3025 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 write_reg(info, CHB + CCR4, 0x50);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /* if auxclk not enabled, set internal BRG so
3030 * CTS transitions can be detected (requires TxC)
Jeff Garzikd12341f2007-10-19 15:45:35 -04003031 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3033 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3034 else
3035 mgslpc_set_rate(info, CHB, 921600);
3036}
3037
Jeff Garzikd12341f2007-10-19 15:45:35 -04003038static void loopback_enable(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
3040 unsigned char val;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003041
3042 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3044 write_reg(info, CHA + CCR1, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003045
3046 /* CCR2:04 SSEL Clock source select, 1=submode b */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3048 write_reg(info, CHA + CCR2, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003049
3050 /* set LinkSpeed if available, otherwise default to 2Mbps */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (info->params.clock_speed)
3052 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3053 else
3054 mgslpc_set_rate(info, CHA, 1843200);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003055
3056 /* MODE:00 TLP Test Loop, 1=loopback enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 val = read_reg(info, CHA + MODE) | BIT0;
3058 write_reg(info, CHA + MODE, val);
3059}
3060
Peter Hagervallcdaad342006-06-23 02:06:04 -07003061static void hdlc_mode(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
3063 unsigned char val;
3064 unsigned char clkmode, clksubmode;
3065
Jeff Garzikd12341f2007-10-19 15:45:35 -04003066 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 irq_disable(info, CHA, 0xffff);
3068 irq_disable(info, CHB, 0xffff);
3069 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003070
3071 /* assume clock mode 0a, rcv=RxC xmt=TxC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 clkmode = clksubmode = 0;
3073 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3074 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003075 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 clkmode = 7;
3077 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3078 && info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003079 /* clock mode 7b, rcv = BRG, xmt = BRG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 clkmode = 7;
3081 clksubmode = 1;
3082 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3083 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003084 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 clkmode = 6;
3086 clksubmode = 1;
3087 } else {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003088 /* clock mode 6a, rcv = DPLL, xmt = TxC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 clkmode = 6;
3090 }
3091 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003092 /* clock mode 0b, rcv = RxC, xmt = BRG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 clksubmode = 1;
3094 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04003095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 /* MODE
3097 *
3098 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3099 * 05 ADM Address Mode, 0 = no addr recognition
3100 * 04 TMD Timer Mode, 0 = external
3101 * 03 RAC Receiver Active, 0 = inactive
3102 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3103 * 01 TRS Timer Resolution, 1=512
3104 * 00 TLP Test Loop, 0 = no loop
3105 *
3106 * 1000 0010
Jeff Garzikd12341f2007-10-19 15:45:35 -04003107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 val = 0x82;
3109 if (info->params.loopback)
3110 val |= BIT0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003111
3112 /* preserve RTS state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 if (info->serial_signals & SerialSignal_RTS)
3114 val |= BIT2;
3115 write_reg(info, CHA + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /* CCR0
3118 *
3119 * 07 PU Power Up, 1=active, 0=power down
3120 * 06 MCE Master Clock Enable, 1=enabled
3121 * 05 Reserved, 0
3122 * 04..02 SC[2..0] Encoding
3123 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3124 *
3125 * 11000000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 val = 0xc0;
3128 switch (info->params.encoding)
3129 {
3130 case HDLC_ENCODING_NRZI:
3131 val |= BIT3;
3132 break;
3133 case HDLC_ENCODING_BIPHASE_SPACE:
3134 val |= BIT4;
3135 break; // FM0
3136 case HDLC_ENCODING_BIPHASE_MARK:
3137 val |= BIT4 + BIT2;
3138 break; // FM1
3139 case HDLC_ENCODING_BIPHASE_LEVEL:
3140 val |= BIT4 + BIT3;
3141 break; // Manchester
3142 }
3143 write_reg(info, CHA + CCR0, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 /* CCR1
3146 *
3147 * 07 SFLG Shared Flag, 0 = disable shared flags
3148 * 06 GALP Go Active On Loop, 0 = not used
3149 * 05 GLP Go On Loop, 0 = not used
3150 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3151 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3152 * 02..00 CM[2..0] Clock Mode
3153 *
3154 * 0001 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 val = 0x10 + clkmode;
3157 write_reg(info, CHA + CCR1, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 /* CCR2
3160 *
3161 * 07..06 BGR[9..8] Baud rate bits 9..8
3162 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3163 * 04 SSEL Clock source select, 1=submode b
3164 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3165 * 02 RWX Read/Write Exchange 0=disabled
3166 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3167 * 00 DIV, data inversion 0=disabled, 1=enabled
3168 *
3169 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 val = 0x00;
3172 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3173 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3174 val |= BIT5;
3175 if (clksubmode)
3176 val |= BIT4;
3177 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3178 val |= BIT1;
3179 if (info->params.encoding == HDLC_ENCODING_NRZB)
3180 val |= BIT0;
3181 write_reg(info, CHA + CCR2, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 /* CCR3
3184 *
3185 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3186 * 05 EPT Enable preamble transmission, 1=enabled
3187 * 04 RADD Receive address pushed to FIFO, 0=disabled
3188 * 03 CRL CRC Reset Level, 0=FFFF
3189 * 02 RCRC Rx CRC 0=On 1=Off
3190 * 01 TCRC Tx CRC 0=On 1=Off
3191 * 00 PSD DPLL Phase Shift Disable
3192 *
3193 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 val = 0x00;
3196 if (info->params.crc_type == HDLC_CRC_NONE)
3197 val |= BIT2 + BIT1;
3198 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3199 val |= BIT5;
3200 switch (info->params.preamble_length)
3201 {
3202 case HDLC_PREAMBLE_LENGTH_16BITS:
3203 val |= BIT6;
3204 break;
3205 case HDLC_PREAMBLE_LENGTH_32BITS:
3206 val |= BIT6;
3207 break;
3208 case HDLC_PREAMBLE_LENGTH_64BITS:
3209 val |= BIT7 + BIT6;
3210 break;
3211 }
3212 write_reg(info, CHA + CCR3, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003213
3214 /* PRE - Preamble pattern */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 val = 0;
3216 switch (info->params.preamble)
3217 {
3218 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3219 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3220 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3221 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3222 }
3223 write_reg(info, CHA + PRE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 /* CCR4
3226 *
3227 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3228 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3229 * 05 TST1 Test Pin, 0=normal operation
3230 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3231 * 03..02 Reserved, must be 0
3232 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3233 *
3234 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 val = 0x50;
3237 write_reg(info, CHA + CCR4, val);
3238 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3239 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3240 else
3241 mgslpc_set_rate(info, CHA, info->params.clock_speed);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003242
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 /* RLCR Receive length check register
3244 *
3245 * 7 1=enable receive length check
3246 * 6..0 Max frame length = (RL + 1) * 32
Jeff Garzikd12341f2007-10-19 15:45:35 -04003247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 write_reg(info, CHA + RLCR, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 /* XBCH Transmit Byte Count High
3251 *
3252 * 07 DMA mode, 0 = interrupt driven
3253 * 06 NRM, 0=ABM (ignored)
3254 * 05 CAS Carrier Auto Start
3255 * 04 XC Transmit Continuously (ignored)
3256 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3257 *
3258 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 val = 0x00;
3261 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3262 val |= BIT5;
3263 write_reg(info, CHA + XBCH, val);
3264 enable_auxclk(info);
3265 if (info->params.loopback || info->testing_irq)
3266 loopback_enable(info);
3267 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3268 {
3269 irq_enable(info, CHB, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003270 /* PVR[3] 1=AUTO CTS active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 set_reg_bits(info, CHA + PVR, BIT3);
3272 } else
3273 clear_reg_bits(info, CHA + PVR, BIT3);
3274
3275 irq_enable(info, CHA,
3276 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3277 IRQ_UNDERRUN + IRQ_TXFIFO);
3278 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3279 wait_command_complete(info, CHA);
3280 read_reg16(info, CHA + ISR); /* clear pending IRQs */
Jeff Garzikd12341f2007-10-19 15:45:35 -04003281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 /* Master clock mode enabled above to allow reset commands
3283 * to complete even if no data clocks are present.
3284 *
3285 * Disable master clock mode for normal communications because
3286 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3287 * IRQ when in master clock mode.
3288 *
3289 * Leave master clock mode enabled for IRQ test because the
3290 * timer IRQ used by the test can only happen in master clock mode.
Jeff Garzikd12341f2007-10-19 15:45:35 -04003291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 if (!info->testing_irq)
3293 clear_reg_bits(info, CHA + CCR0, BIT6);
3294
3295 tx_set_idle(info);
3296
3297 tx_stop(info);
3298 rx_stop(info);
3299}
3300
Peter Hagervallcdaad342006-06-23 02:06:04 -07003301static void rx_stop(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
3303 if (debug_level >= DEBUG_LEVEL_ISR)
3304 printk("%s(%d):rx_stop(%s)\n",
3305 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003306
3307 /* MODE:03 RAC Receiver Active, 0=inactive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 clear_reg_bits(info, CHA + MODE, BIT3);
3309
Joe Perches0fab6de2008-04-28 02:14:02 -07003310 info->rx_enabled = false;
3311 info->rx_overflow = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
Peter Hagervallcdaad342006-06-23 02:06:04 -07003314static void rx_start(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
3316 if (debug_level >= DEBUG_LEVEL_ISR)
3317 printk("%s(%d):rx_start(%s)\n",
3318 __FILE__,__LINE__, info->device_name );
3319
3320 rx_reset_buffers(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003321 info->rx_enabled = false;
3322 info->rx_overflow = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Jeff Garzikd12341f2007-10-19 15:45:35 -04003324 /* MODE:03 RAC Receiver Active, 1=active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 set_reg_bits(info, CHA + MODE, BIT3);
3326
Joe Perches0fab6de2008-04-28 02:14:02 -07003327 info->rx_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328}
3329
Alan Coxeeb46132009-01-02 13:48:47 +00003330static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
3332 if (debug_level >= DEBUG_LEVEL_ISR)
3333 printk("%s(%d):tx_start(%s)\n",
3334 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (info->tx_count) {
3337 /* If auto RTS enabled and RTS is inactive, then assert */
3338 /* RTS and set a flag indicating that the driver should */
3339 /* negate RTS when the transmission completes. */
Joe Perches0fab6de2008-04-28 02:14:02 -07003340 info->drop_rts_on_tx_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3343 get_signals(info);
3344 if (!(info->serial_signals & SerialSignal_RTS)) {
3345 info->serial_signals |= SerialSignal_RTS;
3346 set_signals(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003347 info->drop_rts_on_tx_done = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 }
3349 }
3350
3351 if (info->params.mode == MGSL_MODE_ASYNC) {
3352 if (!info->tx_active) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003353 info->tx_active = true;
Alan Coxeeb46132009-01-02 13:48:47 +00003354 tx_ready(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
3356 } else {
Joe Perches0fab6de2008-04-28 02:14:02 -07003357 info->tx_active = true;
Alan Coxeeb46132009-01-02 13:48:47 +00003358 tx_ready(info, tty);
Jiri Slaby40565f12007-02-12 00:52:31 -08003359 mod_timer(&info->tx_timer, jiffies +
3360 msecs_to_jiffies(5000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 }
3362 }
3363
3364 if (!info->tx_enabled)
Joe Perches0fab6de2008-04-28 02:14:02 -07003365 info->tx_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
Peter Hagervallcdaad342006-06-23 02:06:04 -07003368static void tx_stop(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369{
3370 if (debug_level >= DEBUG_LEVEL_ISR)
3371 printk("%s(%d):tx_stop(%s)\n",
3372 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003373
3374 del_timer(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Joe Perches0fab6de2008-04-28 02:14:02 -07003376 info->tx_enabled = false;
3377 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
3380/* Reset the adapter to a known state and prepare it for further use.
3381 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003382static void reset_device(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003384 /* power up both channels (set BIT7) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 write_reg(info, CHA + CCR0, 0x80);
3386 write_reg(info, CHB + CCR0, 0x80);
3387 write_reg(info, CHA + MODE, 0);
3388 write_reg(info, CHB + MODE, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003389
3390 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 irq_disable(info, CHA, 0xffff);
3392 irq_disable(info, CHB, 0xffff);
3393 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 /* PCR Port Configuration Register
3396 *
3397 * 07..04 DEC[3..0] Serial I/F select outputs
3398 * 03 output, 1=AUTO CTS control enabled
3399 * 02 RI Ring Indicator input 0=active
3400 * 01 DSR input 0=active
3401 * 00 DTR output 0=active
3402 *
3403 * 0000 0110
Jeff Garzikd12341f2007-10-19 15:45:35 -04003404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 write_reg(info, PCR, 0x06);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 /* PVR Port Value Register
3408 *
3409 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3410 * 03 AUTO CTS output 1=enabled
3411 * 02 RI Ring Indicator input
3412 * 01 DSR input
3413 * 00 DTR output (1=inactive)
3414 *
3415 * 0000 0001
3416 */
3417// write_reg(info, PVR, PVR_DTR);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 /* IPC Interrupt Port Configuration
3420 *
3421 * 07 VIS 1=Masked interrupts visible
3422 * 06..05 Reserved, 0
3423 * 04..03 SLA Slave address, 00 ignored
3424 * 02 CASM Cascading Mode, 1=daisy chain
3425 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3426 *
3427 * 0000 0101
Jeff Garzikd12341f2007-10-19 15:45:35 -04003428 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 write_reg(info, IPC, 0x05);
3430}
3431
Peter Hagervallcdaad342006-06-23 02:06:04 -07003432static void async_mode(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
3434 unsigned char val;
3435
Jeff Garzikd12341f2007-10-19 15:45:35 -04003436 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 irq_disable(info, CHA, 0xffff);
3438 irq_disable(info, CHB, 0xffff);
3439 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 /* MODE
3442 *
3443 * 07 Reserved, 0
3444 * 06 FRTS RTS State, 0=active
3445 * 05 FCTS Flow Control on CTS
3446 * 04 FLON Flow Control Enable
3447 * 03 RAC Receiver Active, 0 = inactive
3448 * 02 RTS 0=Auto RTS, 1=manual RTS
3449 * 01 TRS Timer Resolution, 1=512
3450 * 00 TLP Test Loop, 0 = no loop
3451 *
3452 * 0000 0110
Jeff Garzikd12341f2007-10-19 15:45:35 -04003453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 val = 0x06;
3455 if (info->params.loopback)
3456 val |= BIT0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003457
3458 /* preserve RTS state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 if (!(info->serial_signals & SerialSignal_RTS))
3460 val |= BIT6;
3461 write_reg(info, CHA + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 /* CCR0
3464 *
3465 * 07 PU Power Up, 1=active, 0=power down
3466 * 06 MCE Master Clock Enable, 1=enabled
3467 * 05 Reserved, 0
3468 * 04..02 SC[2..0] Encoding, 000=NRZ
3469 * 01..00 SM[1..0] Serial Mode, 11=Async
3470 *
3471 * 1000 0011
Jeff Garzikd12341f2007-10-19 15:45:35 -04003472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 write_reg(info, CHA + CCR0, 0x83);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003474
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 /* CCR1
3476 *
3477 * 07..05 Reserved, 0
3478 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3479 * 03 BCR Bit Clock Rate, 1=16x
3480 * 02..00 CM[2..0] Clock Mode, 111=BRG
3481 *
3482 * 0001 1111
Jeff Garzikd12341f2007-10-19 15:45:35 -04003483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 write_reg(info, CHA + CCR1, 0x1f);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 /* CCR2 (channel A)
3487 *
3488 * 07..06 BGR[9..8] Baud rate bits 9..8
3489 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3490 * 04 SSEL Clock source select, 1=submode b
3491 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3492 * 02 RWX Read/Write Exchange 0=disabled
3493 * 01 Reserved, 0
3494 * 00 DIV, data inversion 0=disabled, 1=enabled
3495 *
3496 * 0001 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 write_reg(info, CHA + CCR2, 0x10);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 /* CCR3
3501 *
3502 * 07..01 Reserved, 0
3503 * 00 PSD DPLL Phase Shift Disable
3504 *
3505 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003506 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 write_reg(info, CHA + CCR3, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 /* CCR4
3510 *
3511 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3512 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3513 * 05 TST1 Test Pin, 0=normal operation
3514 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3515 * 03..00 Reserved, must be 0
3516 *
3517 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 write_reg(info, CHA + CCR4, 0x50);
3520 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 /* DAFO Data Format
3523 *
3524 * 07 Reserved, 0
3525 * 06 XBRK transmit break, 0=normal operation
3526 * 05 Stop bits (0=1, 1=2)
3527 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3528 * 02 PAREN Parity Enable
3529 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3530 *
Jeff Garzikd12341f2007-10-19 15:45:35 -04003531 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 val = 0x00;
3533 if (info->params.data_bits != 8)
3534 val |= BIT0; /* 7 bits */
3535 if (info->params.stop_bits != 1)
3536 val |= BIT5;
3537 if (info->params.parity != ASYNC_PARITY_NONE)
3538 {
3539 val |= BIT2; /* Parity enable */
3540 if (info->params.parity == ASYNC_PARITY_ODD)
3541 val |= BIT3;
3542 else
3543 val |= BIT4;
3544 }
3545 write_reg(info, CHA + DAFO, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 /* RFC Rx FIFO Control
3548 *
3549 * 07 Reserved, 0
3550 * 06 DPS, 1=parity bit not stored in data byte
3551 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3552 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3553 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3554 * 01 Reserved, 0
3555 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3556 *
3557 * 0101 1100
Jeff Garzikd12341f2007-10-19 15:45:35 -04003558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 write_reg(info, CHA + RFC, 0x5c);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 /* RLCR Receive length check register
3562 *
3563 * Max frame length = (RL + 1) * 32
Jeff Garzikd12341f2007-10-19 15:45:35 -04003564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 write_reg(info, CHA + RLCR, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 /* XBCH Transmit Byte Count High
3568 *
3569 * 07 DMA mode, 0 = interrupt driven
3570 * 06 NRM, 0=ABM (ignored)
3571 * 05 CAS Carrier Auto Start
3572 * 04 XC Transmit Continuously (ignored)
3573 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3574 *
3575 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003576 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 val = 0x00;
3578 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3579 val |= BIT5;
3580 write_reg(info, CHA + XBCH, val);
3581 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3582 irq_enable(info, CHA, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003583
3584 /* MODE:03 RAC Receiver Active, 1=active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 set_reg_bits(info, CHA + MODE, BIT3);
3586 enable_auxclk(info);
3587 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3588 irq_enable(info, CHB, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003589 /* PVR[3] 1=AUTO CTS active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 set_reg_bits(info, CHA + PVR, BIT3);
3591 } else
3592 clear_reg_bits(info, CHA + PVR, BIT3);
3593 irq_enable(info, CHA,
3594 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3595 IRQ_ALLSENT + IRQ_TXFIFO);
3596 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3597 wait_command_complete(info, CHA);
3598 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3599}
3600
3601/* Set the HDLC idle mode for the transmitter.
3602 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003603static void tx_set_idle(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003605 /* Note: ESCC2 only supports flags and one idle modes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3607 set_reg_bits(info, CHA + CCR1, BIT3);
3608 else
3609 clear_reg_bits(info, CHA + CCR1, BIT3);
3610}
3611
3612/* get state of the V24 status (input) signals.
3613 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003614static void get_signals(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
3616 unsigned char status = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003617
3618 /* preserve DTR and RTS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3620
3621 if (read_reg(info, CHB + VSTR) & BIT7)
3622 info->serial_signals |= SerialSignal_DCD;
3623 if (read_reg(info, CHB + STAR) & BIT1)
3624 info->serial_signals |= SerialSignal_CTS;
3625
3626 status = read_reg(info, CHA + PVR);
3627 if (!(status & PVR_RI))
3628 info->serial_signals |= SerialSignal_RI;
3629 if (!(status & PVR_DSR))
3630 info->serial_signals |= SerialSignal_DSR;
3631}
3632
3633/* Set the state of DTR and RTS based on contents of
3634 * serial_signals member of device extension.
3635 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003636static void set_signals(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637{
3638 unsigned char val;
3639
3640 val = read_reg(info, CHA + MODE);
3641 if (info->params.mode == MGSL_MODE_ASYNC) {
3642 if (info->serial_signals & SerialSignal_RTS)
3643 val &= ~BIT6;
3644 else
3645 val |= BIT6;
3646 } else {
3647 if (info->serial_signals & SerialSignal_RTS)
3648 val |= BIT2;
3649 else
3650 val &= ~BIT2;
3651 }
3652 write_reg(info, CHA + MODE, val);
3653
3654 if (info->serial_signals & SerialSignal_DTR)
3655 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3656 else
3657 set_reg_bits(info, CHA + PVR, PVR_DTR);
3658}
3659
Peter Hagervallcdaad342006-06-23 02:06:04 -07003660static void rx_reset_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661{
3662 RXBUF *buf;
3663 int i;
3664
3665 info->rx_put = 0;
3666 info->rx_get = 0;
3667 info->rx_frame_count = 0;
3668 for (i=0 ; i < info->rx_buf_count ; i++) {
3669 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3670 buf->status = buf->count = 0;
3671 }
3672}
3673
3674/* Attempt to return a received HDLC frame
3675 * Only frames received without errors are returned.
3676 *
Joe Perches0fab6de2008-04-28 02:14:02 -07003677 * Returns true if frame returned, otherwise false
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 */
Alan Coxeeb46132009-01-02 13:48:47 +00003679static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680{
3681 unsigned short status;
3682 RXBUF *buf;
3683 unsigned int framesize = 0;
3684 unsigned long flags;
Joe Perches0fab6de2008-04-28 02:14:02 -07003685 bool return_frame = false;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 if (info->rx_frame_count == 0)
Joe Perches0fab6de2008-04-28 02:14:02 -07003688 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
3690 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3691
3692 status = buf->status;
3693
3694 /* 07 VFR 1=valid frame
3695 * 06 RDO 1=data overrun
3696 * 05 CRC 1=OK, 0=error
3697 * 04 RAB 1=frame aborted
3698 */
3699 if ((status & 0xf0) != 0xA0) {
3700 if (!(status & BIT7) || (status & BIT4))
3701 info->icount.rxabort++;
3702 else if (status & BIT6)
3703 info->icount.rxover++;
3704 else if (!(status & BIT5)) {
3705 info->icount.rxcrc++;
3706 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
Joe Perches0fab6de2008-04-28 02:14:02 -07003707 return_frame = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 }
3709 framesize = 0;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003710#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 {
Krzysztof Halasa198191c2008-06-30 23:26:53 +02003712 info->netdev->stats.rx_errors++;
3713 info->netdev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 }
3715#endif
3716 } else
Joe Perches0fab6de2008-04-28 02:14:02 -07003717 return_frame = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
3719 if (return_frame)
3720 framesize = buf->count;
3721
3722 if (debug_level >= DEBUG_LEVEL_BH)
3723 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3724 __FILE__,__LINE__,info->device_name,status,framesize);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 if (debug_level >= DEBUG_LEVEL_DATA)
Jeff Garzikd12341f2007-10-19 15:45:35 -04003727 trace_block(info, buf->data, framesize, 0);
3728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 if (framesize) {
3730 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3731 framesize+1 > info->max_frame_size) ||
3732 framesize > info->max_frame_size)
3733 info->icount.rxlong++;
3734 else {
3735 if (status & BIT5)
3736 info->icount.rxok++;
3737
3738 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3739 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3740 ++framesize;
3741 }
3742
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003743#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 if (info->netcount)
3745 hdlcdev_rx(info, buf->data, framesize);
3746 else
3747#endif
3748 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3749 }
3750 }
3751
3752 spin_lock_irqsave(&info->lock,flags);
3753 buf->status = buf->count = 0;
3754 info->rx_frame_count--;
3755 info->rx_get++;
3756 if (info->rx_get >= info->rx_buf_count)
3757 info->rx_get = 0;
3758 spin_unlock_irqrestore(&info->lock,flags);
3759
Joe Perches0fab6de2008-04-28 02:14:02 -07003760 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761}
3762
Joe Perches0fab6de2008-04-28 02:14:02 -07003763static bool register_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003765 static unsigned char patterns[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
Tobias Klauserfe971072006-01-09 20:54:02 -08003767 static unsigned int count = ARRAY_SIZE(patterns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 unsigned int i;
Joe Perches0fab6de2008-04-28 02:14:02 -07003769 bool rc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 unsigned long flags;
3771
3772 spin_lock_irqsave(&info->lock,flags);
3773 reset_device(info);
3774
3775 for (i = 0; i < count; i++) {
3776 write_reg(info, XAD1, patterns[i]);
3777 write_reg(info, XAD2, patterns[(i + 1) % count]);
Tobias Klauserfe971072006-01-09 20:54:02 -08003778 if ((read_reg(info, XAD1) != patterns[i]) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003780 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 break;
3782 }
3783 }
3784
3785 spin_unlock_irqrestore(&info->lock,flags);
3786 return rc;
3787}
3788
Joe Perches0fab6de2008-04-28 02:14:02 -07003789static bool irq_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
3791 unsigned long end_time;
3792 unsigned long flags;
3793
3794 spin_lock_irqsave(&info->lock,flags);
3795 reset_device(info);
3796
Joe Perches0fab6de2008-04-28 02:14:02 -07003797 info->testing_irq = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 hdlc_mode(info);
3799
Joe Perches0fab6de2008-04-28 02:14:02 -07003800 info->irq_occurred = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 /* init hdlc mode */
3803
3804 irq_enable(info, CHA, IRQ_TIMER);
3805 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
3806 issue_command(info, CHA, CMD_START_TIMER);
3807
3808 spin_unlock_irqrestore(&info->lock,flags);
3809
3810 end_time=100;
3811 while(end_time-- && !info->irq_occurred) {
3812 msleep_interruptible(10);
3813 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04003814
Joe Perches0fab6de2008-04-28 02:14:02 -07003815 info->testing_irq = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
3817 spin_lock_irqsave(&info->lock,flags);
3818 reset_device(info);
3819 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003820
Joe Perches0fab6de2008-04-28 02:14:02 -07003821 return info->irq_occurred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
Peter Hagervallcdaad342006-06-23 02:06:04 -07003824static int adapter_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
3826 if (!register_test(info)) {
3827 info->init_error = DiagStatus_AddressFailure;
3828 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
3829 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
3830 return -ENODEV;
3831 }
3832
3833 if (!irq_test(info)) {
3834 info->init_error = DiagStatus_IrqFailure;
3835 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
3836 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
3837 return -ENODEV;
3838 }
3839
3840 if (debug_level >= DEBUG_LEVEL_INFO)
3841 printk("%s(%d):device %s passed diagnostics\n",
3842 __FILE__,__LINE__,info->device_name);
3843 return 0;
3844}
3845
Peter Hagervallcdaad342006-06-23 02:06:04 -07003846static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
3848 int i;
3849 int linecount;
3850 if (xmit)
3851 printk("%s tx data:\n",info->device_name);
3852 else
3853 printk("%s rx data:\n",info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 while(count) {
3856 if (count > 16)
3857 linecount = 16;
3858 else
3859 linecount = count;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 for(i=0;i<linecount;i++)
3862 printk("%02X ",(unsigned char)data[i]);
3863 for(;i<17;i++)
3864 printk(" ");
3865 for(i=0;i<linecount;i++) {
3866 if (data[i]>=040 && data[i]<=0176)
3867 printk("%c",data[i]);
3868 else
3869 printk(".");
3870 }
3871 printk("\n");
Jeff Garzikd12341f2007-10-19 15:45:35 -04003872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 data += linecount;
3874 count -= linecount;
3875 }
3876}
3877
3878/* HDLC frame time out
3879 * update stats and do tx completion processing
3880 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003881static void tx_timeout(unsigned long context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
3883 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
3884 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 if ( debug_level >= DEBUG_LEVEL_INFO )
3887 printk( "%s(%d):tx_timeout(%s)\n",
3888 __FILE__,__LINE__,info->device_name);
3889 if(info->tx_active &&
3890 info->params.mode == MGSL_MODE_HDLC) {
3891 info->icount.txtimeout++;
3892 }
3893 spin_lock_irqsave(&info->lock,flags);
Joe Perches0fab6de2008-04-28 02:14:02 -07003894 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 info->tx_count = info->tx_put = info->tx_get = 0;
3896
3897 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003898
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003899#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 if (info->netcount)
3901 hdlcdev_tx_done(info);
3902 else
3903#endif
Alan Coxeeb46132009-01-02 13:48:47 +00003904 {
3905 struct tty_struct *tty = tty_port_tty_get(&info->port);
3906 bh_transmit(info, tty);
3907 tty_kref_put(tty);
3908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909}
3910
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003911#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
3913/**
3914 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
3915 * set encoding and frame check sequence (FCS) options
3916 *
3917 * dev pointer to network device structure
3918 * encoding serial encoding setting
3919 * parity FCS setting
3920 *
3921 * returns 0 if success, otherwise error code
3922 */
3923static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
3924 unsigned short parity)
3925{
3926 MGSLPC_INFO *info = dev_to_port(dev);
Alan Coxeeb46132009-01-02 13:48:47 +00003927 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 unsigned char new_encoding;
3929 unsigned short new_crctype;
3930
3931 /* return error if TTY interface open */
Alan Coxeeb46132009-01-02 13:48:47 +00003932 if (info->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 return -EBUSY;
3934
3935 switch (encoding)
3936 {
3937 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
3938 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
3939 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
3940 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
3941 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
3942 default: return -EINVAL;
3943 }
3944
3945 switch (parity)
3946 {
3947 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
3948 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
3949 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
3950 default: return -EINVAL;
3951 }
3952
3953 info->params.encoding = new_encoding;
Alexey Dobriyan53b35312006-03-24 03:16:13 -08003954 info->params.crc_type = new_crctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
3956 /* if network interface up, reprogram hardware */
Alan Coxeeb46132009-01-02 13:48:47 +00003957 if (info->netcount) {
3958 tty = tty_port_tty_get(&info->port);
3959 mgslpc_program_hw(info, tty);
3960 tty_kref_put(tty);
3961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
3963 return 0;
3964}
3965
3966/**
3967 * called by generic HDLC layer to send frame
3968 *
3969 * skb socket buffer containing HDLC frame
3970 * dev pointer to network device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 */
Stephen Hemminger4c5d5022009-08-31 19:50:48 +00003972static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
3973 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974{
3975 MGSLPC_INFO *info = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 unsigned long flags;
3977
3978 if (debug_level >= DEBUG_LEVEL_INFO)
3979 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
3980
3981 /* stop sending until this frame completes */
3982 netif_stop_queue(dev);
3983
3984 /* copy data to device buffers */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003985 skb_copy_from_linear_data(skb, info->tx_buf, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 info->tx_get = 0;
3987 info->tx_put = info->tx_count = skb->len;
3988
3989 /* update network statistics */
Krzysztof Halasa198191c2008-06-30 23:26:53 +02003990 dev->stats.tx_packets++;
3991 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
3993 /* done with socket buffer, so free it */
3994 dev_kfree_skb(skb);
3995
3996 /* save start time for transmit timeout detection */
3997 dev->trans_start = jiffies;
3998
3999 /* start hardware transmitter if necessary */
4000 spin_lock_irqsave(&info->lock,flags);
Alan Coxeeb46132009-01-02 13:48:47 +00004001 if (!info->tx_active) {
4002 struct tty_struct *tty = tty_port_tty_get(&info->port);
4003 tx_start(info, tty);
4004 tty_kref_put(tty);
4005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 spin_unlock_irqrestore(&info->lock,flags);
4007
Stephen Hemminger4c5d5022009-08-31 19:50:48 +00004008 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009}
4010
4011/**
4012 * called by network layer when interface enabled
4013 * claim resources and initialize hardware
4014 *
4015 * dev pointer to network device structure
4016 *
4017 * returns 0 if success, otherwise error code
4018 */
4019static int hdlcdev_open(struct net_device *dev)
4020{
4021 MGSLPC_INFO *info = dev_to_port(dev);
Alan Coxeeb46132009-01-02 13:48:47 +00004022 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 int rc;
4024 unsigned long flags;
4025
4026 if (debug_level >= DEBUG_LEVEL_INFO)
4027 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4028
4029 /* generic HDLC layer open processing */
4030 if ((rc = hdlc_open(dev)))
4031 return rc;
4032
4033 /* arbitrate between network and tty opens */
4034 spin_lock_irqsave(&info->netlock, flags);
Alan Coxeeb46132009-01-02 13:48:47 +00004035 if (info->port.count != 0 || info->netcount != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4037 spin_unlock_irqrestore(&info->netlock, flags);
4038 return -EBUSY;
4039 }
4040 info->netcount=1;
4041 spin_unlock_irqrestore(&info->netlock, flags);
4042
Alan Coxeeb46132009-01-02 13:48:47 +00004043 tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 /* claim resources and init adapter */
Alan Coxeeb46132009-01-02 13:48:47 +00004045 if ((rc = startup(info, tty)) != 0) {
4046 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 spin_lock_irqsave(&info->netlock, flags);
4048 info->netcount=0;
4049 spin_unlock_irqrestore(&info->netlock, flags);
4050 return rc;
4051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 /* assert DTR and RTS, apply hardware settings */
4053 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
Alan Coxeeb46132009-01-02 13:48:47 +00004054 mgslpc_program_hw(info, tty);
4055 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
4057 /* enable network layer transmit */
4058 dev->trans_start = jiffies;
4059 netif_start_queue(dev);
4060
4061 /* inform generic HDLC layer of current DCD status */
4062 spin_lock_irqsave(&info->lock, flags);
4063 get_signals(info);
4064 spin_unlock_irqrestore(&info->lock, flags);
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07004065 if (info->serial_signals & SerialSignal_DCD)
4066 netif_carrier_on(dev);
4067 else
4068 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 return 0;
4070}
4071
4072/**
4073 * called by network layer when interface is disabled
4074 * shutdown hardware and release resources
4075 *
4076 * dev pointer to network device structure
4077 *
4078 * returns 0 if success, otherwise error code
4079 */
4080static int hdlcdev_close(struct net_device *dev)
4081{
4082 MGSLPC_INFO *info = dev_to_port(dev);
Alan Coxeeb46132009-01-02 13:48:47 +00004083 struct tty_struct *tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 unsigned long flags;
4085
4086 if (debug_level >= DEBUG_LEVEL_INFO)
4087 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4088
4089 netif_stop_queue(dev);
4090
4091 /* shutdown adapter and release resources */
Alan Coxeeb46132009-01-02 13:48:47 +00004092 shutdown(info, tty);
4093 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 hdlc_close(dev);
4095
4096 spin_lock_irqsave(&info->netlock, flags);
4097 info->netcount=0;
4098 spin_unlock_irqrestore(&info->netlock, flags);
4099
4100 return 0;
4101}
4102
4103/**
4104 * called by network layer to process IOCTL call to network device
4105 *
4106 * dev pointer to network device structure
4107 * ifr pointer to network interface request structure
4108 * cmd IOCTL command code
4109 *
4110 * returns 0 if success, otherwise error code
4111 */
4112static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4113{
4114 const size_t size = sizeof(sync_serial_settings);
4115 sync_serial_settings new_line;
4116 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4117 MGSLPC_INFO *info = dev_to_port(dev);
4118 unsigned int flags;
4119
4120 if (debug_level >= DEBUG_LEVEL_INFO)
4121 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4122
4123 /* return error if TTY interface open */
Alan Coxeeb46132009-01-02 13:48:47 +00004124 if (info->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 return -EBUSY;
4126
4127 if (cmd != SIOCWANDEV)
4128 return hdlc_ioctl(dev, ifr, cmd);
4129
4130 switch(ifr->ifr_settings.type) {
4131 case IF_GET_IFACE: /* return current sync_serial_settings */
4132
4133 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4134 if (ifr->ifr_settings.size < size) {
4135 ifr->ifr_settings.size = size; /* data size wanted */
4136 return -ENOBUFS;
4137 }
4138
4139 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4140 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4141 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4142 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4143
4144 switch (flags){
4145 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4146 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4147 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4148 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4149 default: new_line.clock_type = CLOCK_DEFAULT;
4150 }
4151
4152 new_line.clock_rate = info->params.clock_speed;
4153 new_line.loopback = info->params.loopback ? 1:0;
4154
4155 if (copy_to_user(line, &new_line, size))
4156 return -EFAULT;
4157 return 0;
4158
4159 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4160
4161 if(!capable(CAP_NET_ADMIN))
4162 return -EPERM;
4163 if (copy_from_user(&new_line, line, size))
4164 return -EFAULT;
4165
4166 switch (new_line.clock_type)
4167 {
4168 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4169 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4170 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4171 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4172 case CLOCK_DEFAULT: flags = info->params.flags &
4173 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4174 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4175 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4176 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4177 default: return -EINVAL;
4178 }
4179
4180 if (new_line.loopback != 0 && new_line.loopback != 1)
4181 return -EINVAL;
4182
4183 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4184 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4185 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4186 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4187 info->params.flags |= flags;
4188
4189 info->params.loopback = new_line.loopback;
4190
4191 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4192 info->params.clock_speed = new_line.clock_rate;
4193 else
4194 info->params.clock_speed = 0;
4195
4196 /* if network interface up, reprogram hardware */
Alan Coxeeb46132009-01-02 13:48:47 +00004197 if (info->netcount) {
4198 struct tty_struct *tty = tty_port_tty_get(&info->port);
4199 mgslpc_program_hw(info, tty);
4200 tty_kref_put(tty);
4201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 return 0;
4203
4204 default:
4205 return hdlc_ioctl(dev, ifr, cmd);
4206 }
4207}
4208
4209/**
4210 * called by network layer when transmit timeout is detected
4211 *
4212 * dev pointer to network device structure
4213 */
4214static void hdlcdev_tx_timeout(struct net_device *dev)
4215{
4216 MGSLPC_INFO *info = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 unsigned long flags;
4218
4219 if (debug_level >= DEBUG_LEVEL_INFO)
4220 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4221
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004222 dev->stats.tx_errors++;
4223 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
4225 spin_lock_irqsave(&info->lock,flags);
4226 tx_stop(info);
4227 spin_unlock_irqrestore(&info->lock,flags);
4228
4229 netif_wake_queue(dev);
4230}
4231
4232/**
4233 * called by device driver when transmit completes
4234 * reenable network layer transmit if stopped
4235 *
4236 * info pointer to device instance information
4237 */
4238static void hdlcdev_tx_done(MGSLPC_INFO *info)
4239{
4240 if (netif_queue_stopped(info->netdev))
4241 netif_wake_queue(info->netdev);
4242}
4243
4244/**
4245 * called by device driver when frame received
4246 * pass frame to network layer
4247 *
4248 * info pointer to device instance information
4249 * buf pointer to buffer contianing frame data
4250 * size count of data bytes in buf
4251 */
4252static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4253{
4254 struct sk_buff *skb = dev_alloc_skb(size);
4255 struct net_device *dev = info->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257 if (debug_level >= DEBUG_LEVEL_INFO)
4258 printk("hdlcdev_rx(%s)\n",dev->name);
4259
4260 if (skb == NULL) {
4261 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004262 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 return;
4264 }
4265
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004266 memcpy(skb_put(skb, size), buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004268 skb->protocol = hdlc_type_trans(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004270 dev->stats.rx_packets++;
4271 dev->stats.rx_bytes += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}
4275
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01004276static const struct net_device_ops hdlcdev_ops = {
4277 .ndo_open = hdlcdev_open,
4278 .ndo_stop = hdlcdev_close,
4279 .ndo_change_mtu = hdlc_change_mtu,
4280 .ndo_start_xmit = hdlc_start_xmit,
4281 .ndo_do_ioctl = hdlcdev_ioctl,
4282 .ndo_tx_timeout = hdlcdev_tx_timeout,
4283};
4284
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285/**
4286 * called by device driver when adding device instance
4287 * do generic HDLC initialization
4288 *
4289 * info pointer to device instance information
4290 *
4291 * returns 0 if success, otherwise error code
4292 */
4293static int hdlcdev_init(MGSLPC_INFO *info)
4294{
4295 int rc;
4296 struct net_device *dev;
4297 hdlc_device *hdlc;
4298
4299 /* allocate and initialize network and HDLC layer objects */
4300
4301 if (!(dev = alloc_hdlcdev(info))) {
4302 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4303 return -ENOMEM;
4304 }
4305
4306 /* for network layer reporting purposes only */
4307 dev->base_addr = info->io_base;
4308 dev->irq = info->irq_level;
4309
4310 /* network layer callbacks and settings */
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01004311 dev->netdev_ops = &hdlcdev_ops;
4312 dev->watchdog_timeo = 10 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 dev->tx_queue_len = 50;
4314
4315 /* generic HDLC layer callbacks and settings */
4316 hdlc = dev_to_hdlc(dev);
4317 hdlc->attach = hdlcdev_attach;
4318 hdlc->xmit = hdlcdev_xmit;
4319
4320 /* register objects with HDLC layer */
4321 if ((rc = register_hdlc_device(dev))) {
4322 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4323 free_netdev(dev);
4324 return rc;
4325 }
4326
4327 info->netdev = dev;
4328 return 0;
4329}
4330
4331/**
4332 * called by device driver when removing device instance
4333 * do generic HDLC cleanup
4334 *
4335 * info pointer to device instance information
4336 */
4337static void hdlcdev_exit(MGSLPC_INFO *info)
4338{
4339 unregister_hdlc_device(info->netdev);
4340 free_netdev(info->netdev);
4341 info->netdev = NULL;
4342}
4343
4344#endif /* CONFIG_HDLC */
4345