blob: 308903ec8bf8464187e22ba14cc39ffe988dc4d0 [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_types.h>
74#include <pcmcia/cs.h>
75#include <pcmcia/cistpl.h>
76#include <pcmcia/cisreg.h>
77#include <pcmcia/ds.h>
78
Paul Fulghumaf69c7f2006-12-06 20:40:24 -080079#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_CS_MODULE))
80#define SYNCLINK_GENERIC_HDLC 1
81#else
82#define SYNCLINK_GENERIC_HDLC 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif
84
85#define GET_USER(error,value,addr) error = get_user(value,addr)
86#define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
87#define PUT_USER(error,value,addr) error = put_user(value,addr)
88#define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
89
90#include <asm/uaccess.h>
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static MGSL_PARAMS default_params = {
93 MGSL_MODE_HDLC, /* unsigned long mode */
94 0, /* unsigned char loopback; */
95 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
96 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
97 0, /* unsigned long clock_speed; */
98 0xff, /* unsigned char addr_filter; */
99 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
100 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
101 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
102 9600, /* unsigned long data_rate; */
103 8, /* unsigned char data_bits; */
104 1, /* unsigned char stop_bits; */
105 ASYNC_PARITY_NONE /* unsigned char parity; */
106};
107
108typedef struct
109{
110 int count;
111 unsigned char status;
112 char data[1];
113} RXBUF;
114
115/* The queue of BH actions to be performed */
116
117#define BH_RECEIVE 1
118#define BH_TRANSMIT 2
119#define BH_STATUS 4
120
121#define IO_PIN_SHUTDOWN_LIMIT 100
122
123#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
124
125struct _input_signal_events {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400126 int ri_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int ri_down;
128 int dsr_up;
129 int dsr_down;
130 int dcd_up;
131 int dcd_down;
132 int cts_up;
133 int cts_down;
134};
135
136
137/*
138 * Device instance data structure
139 */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141typedef struct _mgslpc_info {
Alan Coxeeb46132009-01-02 13:48:47 +0000142 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 void *if_ptr; /* General purpose pointer (used by SPPP) */
144 int magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int line;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct mgsl_icount icount;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int timeout;
150 int x_char; /* xon/xoff character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unsigned char read_status_mask;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400152 unsigned char ignore_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 unsigned char *tx_buf;
155 int tx_put;
156 int tx_get;
157 int tx_count;
158
159 /* circular list of fixed length rx buffers */
160
161 unsigned char *rx_buf; /* memory allocated for all rx buffers */
162 int rx_buf_total_size; /* size of memory allocated for rx buffers */
163 int rx_put; /* index of next empty rx buffer */
164 int rx_get; /* index of next full rx buffer */
165 int rx_buf_size; /* size in bytes of single rx buffer */
166 int rx_buf_count; /* total number of rx buffers */
167 int rx_frame_count; /* number of full rx buffers */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 wait_queue_head_t status_event_wait_q;
170 wait_queue_head_t event_wait_q;
171 struct timer_list tx_timer; /* HDLC transmit timeout timer */
172 struct _mgslpc_info *next_device; /* device list link */
173
174 unsigned short imra_value;
175 unsigned short imrb_value;
176 unsigned char pim_value;
177
178 spinlock_t lock;
179 struct work_struct task; /* task structure for scheduling bh */
180
181 u32 max_frame_size;
182
183 u32 pending_bh;
184
Joe Perches0fab6de2008-04-28 02:14:02 -0700185 bool bh_running;
186 bool bh_requested;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int dcd_chkcount; /* check counts to prevent */
189 int cts_chkcount; /* too many IRQs if a signal */
190 int dsr_chkcount; /* is floating */
191 int ri_chkcount;
192
Joe Perches0fab6de2008-04-28 02:14:02 -0700193 bool rx_enabled;
194 bool rx_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Joe Perches0fab6de2008-04-28 02:14:02 -0700196 bool tx_enabled;
197 bool tx_active;
198 bool tx_aborting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 u32 idle_mode;
200
201 int if_mode; /* serial interface selection (RS-232, v.35 etc) */
202
203 char device_name[25]; /* device instance name */
204
205 unsigned int io_base; /* base I/O address of adapter */
206 unsigned int irq_level;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 MGSL_PARAMS params; /* communications parameters */
209
210 unsigned char serial_signals; /* current serial signal states */
211
Joe Perches0fab6de2008-04-28 02:14:02 -0700212 bool irq_occurred; /* for diagnostics use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 char testing_irq;
214 unsigned int init_error; /* startup error (DIAGS) */
215
216 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
Joe Perches0fab6de2008-04-28 02:14:02 -0700217 bool drop_rts_on_tx_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 struct _input_signal_events input_signal_events;
220
221 /* PCMCIA support */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100222 struct pcmcia_device *p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 int stop;
224
225 /* SPPP/Cisco HDLC device parts */
226 int netcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 spinlock_t netlock;
228
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800229#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct net_device *netdev;
231#endif
232
233} MGSLPC_INFO;
234
235#define MGSLPC_MAGIC 0x5402
236
237/*
238 * The size of the serial xmit buffer is 1 page, or 4096 bytes
239 */
240#define TXBUFSIZE 4096
241
Jeff Garzikd12341f2007-10-19 15:45:35 -0400242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#define CHA 0x00 /* channel A offset */
244#define CHB 0x40 /* channel B offset */
245
246/*
247 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
248 */
249#undef PVR
250
251#define RXFIFO 0
252#define TXFIFO 0
253#define STAR 0x20
254#define CMDR 0x20
255#define RSTA 0x21
256#define PRE 0x21
257#define MODE 0x22
258#define TIMR 0x23
259#define XAD1 0x24
260#define XAD2 0x25
261#define RAH1 0x26
262#define RAH2 0x27
263#define DAFO 0x27
264#define RAL1 0x28
265#define RFC 0x28
266#define RHCR 0x29
267#define RAL2 0x29
268#define RBCL 0x2a
269#define XBCL 0x2a
270#define RBCH 0x2b
271#define XBCH 0x2b
272#define CCR0 0x2c
273#define CCR1 0x2d
274#define CCR2 0x2e
275#define CCR3 0x2f
276#define VSTR 0x34
277#define BGR 0x34
278#define RLCR 0x35
279#define AML 0x36
280#define AMH 0x37
281#define GIS 0x38
282#define IVA 0x38
283#define IPC 0x39
284#define ISR 0x3a
285#define IMR 0x3a
286#define PVR 0x3c
287#define PIS 0x3d
288#define PIM 0x3d
289#define PCR 0x3e
290#define CCR4 0x3f
Jeff Garzikd12341f2007-10-19 15:45:35 -0400291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292// IMR/ISR
Jeff Garzikd12341f2007-10-19 15:45:35 -0400293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294#define IRQ_BREAK_ON BIT15 // rx break detected
295#define IRQ_DATAOVERRUN BIT14 // receive data overflow
296#define IRQ_ALLSENT BIT13 // all sent
297#define IRQ_UNDERRUN BIT12 // transmit data underrun
298#define IRQ_TIMER BIT11 // timer interrupt
299#define IRQ_CTS BIT10 // CTS status change
300#define IRQ_TXREPEAT BIT9 // tx message repeat
301#define IRQ_TXFIFO BIT8 // transmit pool ready
302#define IRQ_RXEOM BIT7 // receive message end
303#define IRQ_EXITHUNT BIT6 // receive frame start
304#define IRQ_RXTIME BIT6 // rx char timeout
305#define IRQ_DCD BIT2 // carrier detect status change
306#define IRQ_OVERRUN BIT1 // receive frame overflow
307#define IRQ_RXFIFO BIT0 // receive pool full
Jeff Garzikd12341f2007-10-19 15:45:35 -0400308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309// STAR
Jeff Garzikd12341f2007-10-19 15:45:35 -0400310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311#define XFW BIT6 // transmit FIFO write enable
312#define CEC BIT2 // command executing
313#define CTS BIT1 // CTS state
Jeff Garzikd12341f2007-10-19 15:45:35 -0400314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#define PVR_DTR BIT0
316#define PVR_DSR BIT1
317#define PVR_RI BIT2
318#define PVR_AUTOCTS BIT3
319#define PVR_RS232 0x20 /* 0010b */
320#define PVR_V35 0xe0 /* 1110b */
321#define PVR_RS422 0x40 /* 0100b */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400322
323/* Register access functions */
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
326#define read_reg(info, reg) inb((info)->io_base + (reg))
327
Jeff Garzikd12341f2007-10-19 15:45:35 -0400328#define read_reg16(info, reg) inw((info)->io_base + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
Jeff Garzikd12341f2007-10-19 15:45:35 -0400330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#define set_reg_bits(info, reg, mask) \
332 write_reg(info, (reg), \
Jeff Garzikd12341f2007-10-19 15:45:35 -0400333 (unsigned char) (read_reg(info, (reg)) | (mask)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#define clear_reg_bits(info, reg, mask) \
335 write_reg(info, (reg), \
Jeff Garzikd12341f2007-10-19 15:45:35 -0400336 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/*
338 * interrupt enable/disable routines
Jeff Garzikd12341f2007-10-19 15:45:35 -0400339 */
340static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 if (channel == CHA) {
343 info->imra_value |= mask;
344 write_reg16(info, CHA + IMR, info->imra_value);
345 } else {
346 info->imrb_value |= mask;
347 write_reg16(info, CHB + IMR, info->imrb_value);
348 }
349}
Jeff Garzikd12341f2007-10-19 15:45:35 -0400350static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 if (channel == CHA) {
353 info->imra_value &= ~mask;
354 write_reg16(info, CHA + IMR, info->imra_value);
355 } else {
356 info->imrb_value &= ~mask;
357 write_reg16(info, CHB + IMR, info->imrb_value);
358 }
359}
360
361#define port_irq_disable(info, mask) \
362 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
363
364#define port_irq_enable(info, mask) \
365 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
366
367static void rx_start(MGSLPC_INFO *info);
368static void rx_stop(MGSLPC_INFO *info);
369
Alan Coxeeb46132009-01-02 13:48:47 +0000370static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static void tx_stop(MGSLPC_INFO *info);
372static void tx_set_idle(MGSLPC_INFO *info);
373
374static void get_signals(MGSLPC_INFO *info);
375static void set_signals(MGSLPC_INFO *info);
376
377static void reset_device(MGSLPC_INFO *info);
378
379static void hdlc_mode(MGSLPC_INFO *info);
380static void async_mode(MGSLPC_INFO *info);
381
382static void tx_timeout(unsigned long context);
383
Alan Coxeeb46132009-01-02 13:48:47 +0000384static int carrier_raised(struct tty_port *port);
Alan Coxfcc8ac12009-06-11 12:24:17 +0100385static void dtr_rts(struct tty_port *port, int onoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800387#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388#define dev_to_port(D) (dev_to_hdlc(D)->priv)
389static void hdlcdev_tx_done(MGSLPC_INFO *info);
390static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
391static int hdlcdev_init(MGSLPC_INFO *info);
392static void hdlcdev_exit(MGSLPC_INFO *info);
393#endif
394
395static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
396
Joe Perches0fab6de2008-04-28 02:14:02 -0700397static bool register_test(MGSLPC_INFO *info);
398static bool irq_test(MGSLPC_INFO *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399static int adapter_test(MGSLPC_INFO *info);
400
401static int claim_resources(MGSLPC_INFO *info);
402static void release_resources(MGSLPC_INFO *info);
403static void mgslpc_add_device(MGSLPC_INFO *info);
404static void mgslpc_remove_device(MGSLPC_INFO *info);
405
Alan Coxeeb46132009-01-02 13:48:47 +0000406static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static void rx_reset_buffers(MGSLPC_INFO *info);
408static int rx_alloc_buffers(MGSLPC_INFO *info);
409static void rx_free_buffers(MGSLPC_INFO *info);
410
David Howells7d12e782006-10-05 14:55:46 +0100411static irqreturn_t mgslpc_isr(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413/*
414 * Bottom half interrupt handlers
415 */
David Howellsc4028952006-11-22 14:57:56 +0000416static void bh_handler(struct work_struct *work);
Alan Coxeeb46132009-01-02 13:48:47 +0000417static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static void bh_status(MGSLPC_INFO *info);
419
420/*
421 * ioctl handlers
422 */
423static int tiocmget(struct tty_struct *tty, struct file *file);
424static int tiocmset(struct tty_struct *tty, struct file *file,
425 unsigned int set, unsigned int clear);
426static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
427static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
Alan Coxeeb46132009-01-02 13:48:47 +0000428static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
430static int set_txidle(MGSLPC_INFO *info, int idle_mode);
Alan Coxeeb46132009-01-02 13:48:47 +0000431static int set_txenable(MGSLPC_INFO *info, int enable, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432static int tx_abort(MGSLPC_INFO *info);
433static int set_rxenable(MGSLPC_INFO *info, int enable);
434static int wait_events(MGSLPC_INFO *info, int __user *mask);
435
436static MGSLPC_INFO *mgslpc_device_list = NULL;
437static int mgslpc_device_count = 0;
438
439/*
440 * Set this param to non-zero to load eax with the
441 * .text section address and breakpoint on module load.
442 * This is useful for use with gdb and add-symbol-file command.
443 */
444static int break_on_load=0;
445
446/*
447 * Driver major number, defaults to zero to get auto
448 * assigned major number. May be forced as module parameter.
449 */
450static int ttymajor=0;
451
452static int debug_level = 0;
453static int maxframe[MAX_DEVICE_COUNT] = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455module_param(break_on_load, bool, 0);
456module_param(ttymajor, int, 0);
457module_param(debug_level, int, 0);
458module_param_array(maxframe, int, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460MODULE_LICENSE("GPL");
461
462static char *driver_name = "SyncLink PC Card driver";
Paul Fulghuma7482a22005-09-10 00:26:07 -0700463static char *driver_version = "$Revision: 4.34 $";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465static struct tty_driver *serial_driver;
466
467/* number of characters left in xmit buffer before we ask for more */
468#define WAKEUP_CHARS 256
469
Alan Coxeeb46132009-01-02 13:48:47 +0000470static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
472
473/* PCMCIA prototypes */
474
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200475static int mgslpc_config(struct pcmcia_device *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static void mgslpc_release(u_long arg);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100477static void mgslpc_detach(struct pcmcia_device *p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/*
480 * 1st function defined in .text section. Calling this function in
481 * init_module() followed by a breakpoint allows a remote debugger
482 * (gdb) to get the .text address for the add-symbol-file command.
483 * This allows remote debugging of dynamically loadable modules.
484 */
485static void* mgslpc_get_text_ptr(void)
486{
487 return mgslpc_get_text_ptr;
488}
489
490/**
491 * line discipline callback wrappers
492 *
493 * The wrappers maintain line discipline references
494 * while calling into the line discipline.
495 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * ldisc_receive_buf - pass receive data to line discipline
497 */
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static void ldisc_receive_buf(struct tty_struct *tty,
500 const __u8 *data, char *flags, int count)
501{
502 struct tty_ldisc *ld;
503 if (!tty)
504 return;
505 ld = tty_ldisc_ref(tty);
506 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100507 if (ld->ops->receive_buf)
508 ld->ops->receive_buf(tty, data, flags, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 tty_ldisc_deref(ld);
510 }
511}
512
Alan Coxeeb46132009-01-02 13:48:47 +0000513static const struct tty_port_operations mgslpc_port_ops = {
514 .carrier_raised = carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +0100515 .dtr_rts = dtr_rts
Alan Coxeeb46132009-01-02 13:48:47 +0000516};
517
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200518static int mgslpc_probe(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 MGSLPC_INFO *info;
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200521 int ret;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (debug_level >= DEBUG_LEVEL_INFO)
524 printk("mgslpc_attach\n");
Dominik Brodowskifd238232006-03-05 10:45:09 +0100525
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700526 info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (!info) {
528 printk("Error can't allocate device instance data\n");
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100529 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 info->magic = MGSLPC_MAGIC;
Alan Coxeeb46132009-01-02 13:48:47 +0000533 tty_port_init(&info->port);
534 info->port.ops = &mgslpc_port_ops;
David Howellsc4028952006-11-22 14:57:56 +0000535 INIT_WORK(&info->task, bh_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 info->max_frame_size = 4096;
Alan Coxeeb46132009-01-02 13:48:47 +0000537 info->port.close_delay = 5*HZ/10;
538 info->port.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 init_waitqueue_head(&info->status_event_wait_q);
540 init_waitqueue_head(&info->event_wait_q);
541 spin_lock_init(&info->lock);
542 spin_lock_init(&info->netlock);
543 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
Jeff Garzikd12341f2007-10-19 15:45:35 -0400544 info->idle_mode = HDLC_TXIDLE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 info->imra_value = 0xffff;
546 info->imrb_value = 0xffff;
547 info->pim_value = 0xff;
548
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200549 info->p_dev = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 link->priv = info;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100551
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200552 /* Initialize the struct pcmcia_device structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 link->conf.Attributes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 link->conf.IntType = INT_MEMORY_AND_IO;
556
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200557 ret = mgslpc_config(link);
558 if (ret)
559 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 mgslpc_add_device(info);
562
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566/* Card has been inserted.
567 */
568
Dominik Brodowskiaaa8cfd2009-10-18 18:28:39 +0200569static int mgslpc_ioprobe(struct pcmcia_device *p_dev,
570 cistpl_cftable_entry_t *cfg,
571 cistpl_cftable_entry_t *dflt,
572 unsigned int vcc,
573 void *priv_data)
574{
575 if (cfg->io.nwin > 0) {
576 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
577 if (!(cfg->io.flags & CISTPL_IO_8BIT))
578 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
579 if (!(cfg->io.flags & CISTPL_IO_16BIT))
580 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
581 p_dev->io.IOAddrLines = cfg->io.flags & CISTPL_IO_LINES_MASK;
582 p_dev->io.BasePort1 = cfg->io.win[0].base;
583 p_dev->io.NumPorts1 = cfg->io.win[0].len;
584 return pcmcia_request_io(p_dev, &p_dev->io);
585 }
586 return -ENODEV;
587}
588
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200589static int mgslpc_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 MGSLPC_INFO *info = link->priv;
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200592 int ret;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (debug_level >= DEBUG_LEVEL_INFO)
595 printk("mgslpc_config(0x%p)\n", link);
596
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200597 ret = pcmcia_loop_config(link, mgslpc_ioprobe, NULL);
598 if (ret != 0)
599 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 link->conf.Attributes = CONF_ENABLE_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 link->conf.IntType = INT_MEMORY_AND_IO;
603 link->conf.ConfigIndex = 8;
604 link->conf.Present = PRESENT_OPTION;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400605
Dominik Brodowskieb141202010-03-07 12:21:16 +0100606 ret = pcmcia_request_irq(link, mgslpc_isr);
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200607 if (ret)
608 goto failed;
609 ret = pcmcia_request_configuration(link, &link->conf);
610 if (ret)
611 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 info->io_base = link->io.BasePort1;
Dominik Brodowskieb141202010-03-07 12:21:16 +0100614 info->irq_level = link->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Dominik Brodowskided6a1a2010-03-20 19:35:12 +0100616 dev_info(&link->dev, "index 0x%02x:",
617 link->conf.ConfigIndex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (link->conf.Attributes & CONF_ENABLE_IRQ)
Dominik Brodowskieb141202010-03-07 12:21:16 +0100619 printk(", irq %d", link->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (link->io.NumPorts1)
621 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
622 link->io.BasePort1+link->io.NumPorts1-1);
623 printk("\n");
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200626failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 mgslpc_release((u_long)link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200628 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/* Card has been removed.
632 * Unregister device and release PCMCIA configuration.
633 * If device is open, postpone until it is closed.
634 */
635static void mgslpc_release(u_long arg)
636{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100637 struct pcmcia_device *link = (struct pcmcia_device *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100639 if (debug_level >= DEBUG_LEVEL_INFO)
640 printk("mgslpc_release(0x%p)\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100642 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200645static void mgslpc_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100647 if (debug_level >= DEBUG_LEVEL_INFO)
648 printk("mgslpc_detach(0x%p)\n", link);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100649
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100650 ((MGSLPC_INFO *)link->priv)->stop = 1;
651 mgslpc_release((u_long)link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100653 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200656static int mgslpc_suspend(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100657{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100658 MGSLPC_INFO *info = link->priv;
659
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100660 info->stop = 1;
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100661
662 return 0;
663}
664
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200665static int mgslpc_resume(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100666{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100667 MGSLPC_INFO *info = link->priv;
668
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100669 info->stop = 0;
670
671 return 0;
672}
673
674
Joe Perches0fab6de2008-04-28 02:14:02 -0700675static inline bool mgslpc_paranoia_check(MGSLPC_INFO *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 char *name, const char *routine)
677{
678#ifdef MGSLPC_PARANOIA_CHECK
679 static const char *badmagic =
680 "Warning: bad magic number for mgsl struct (%s) in %s\n";
681 static const char *badinfo =
682 "Warning: null mgslpc_info for (%s) in %s\n";
683
684 if (!info) {
685 printk(badinfo, name, routine);
Joe Perches0fab6de2008-04-28 02:14:02 -0700686 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688 if (info->magic != MGSLPC_MAGIC) {
689 printk(badmagic, name, routine);
Joe Perches0fab6de2008-04-28 02:14:02 -0700690 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692#else
693 if (!info)
Joe Perches0fab6de2008-04-28 02:14:02 -0700694 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695#endif
Joe Perches0fab6de2008-04-28 02:14:02 -0700696 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699
700#define CMD_RXFIFO BIT7 // release current rx FIFO
701#define CMD_RXRESET BIT6 // receiver reset
702#define CMD_RXFIFO_READ BIT5
703#define CMD_START_TIMER BIT4
704#define CMD_TXFIFO BIT3 // release current tx FIFO
705#define CMD_TXEOM BIT1 // transmit end message
706#define CMD_TXRESET BIT0 // transmit reset
707
Joe Perches0fab6de2008-04-28 02:14:02 -0700708static bool wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 int i = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400711 /* wait for command completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
713 udelay(1);
714 if (i++ == 1000)
Joe Perches0fab6de2008-04-28 02:14:02 -0700715 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Joe Perches0fab6de2008-04-28 02:14:02 -0700717 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Jeff Garzikd12341f2007-10-19 15:45:35 -0400720static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 wait_command_complete(info, channel);
723 write_reg(info, (unsigned char) (channel + CMDR), cmd);
724}
725
726static void tx_pause(struct tty_struct *tty)
727{
728 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
729 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
732 return;
733 if (debug_level >= DEBUG_LEVEL_INFO)
Jeff Garzikd12341f2007-10-19 15:45:35 -0400734 printk("tx_pause(%s)\n",info->device_name);
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 spin_lock_irqsave(&info->lock,flags);
737 if (info->tx_enabled)
738 tx_stop(info);
739 spin_unlock_irqrestore(&info->lock,flags);
740}
741
742static void tx_release(struct tty_struct *tty)
743{
744 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
745 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
748 return;
749 if (debug_level >= DEBUG_LEVEL_INFO)
Jeff Garzikd12341f2007-10-19 15:45:35 -0400750 printk("tx_release(%s)\n",info->device_name);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 spin_lock_irqsave(&info->lock,flags);
753 if (!info->tx_enabled)
Alan Coxeeb46132009-01-02 13:48:47 +0000754 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 spin_unlock_irqrestore(&info->lock,flags);
756}
757
758/* Return next bottom half action to perform.
759 * or 0 if nothing to do.
760 */
761static int bh_action(MGSLPC_INFO *info)
762{
763 unsigned long flags;
764 int rc = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 spin_lock_irqsave(&info->lock,flags);
767
768 if (info->pending_bh & BH_RECEIVE) {
769 info->pending_bh &= ~BH_RECEIVE;
770 rc = BH_RECEIVE;
771 } else if (info->pending_bh & BH_TRANSMIT) {
772 info->pending_bh &= ~BH_TRANSMIT;
773 rc = BH_TRANSMIT;
774 } else if (info->pending_bh & BH_STATUS) {
775 info->pending_bh &= ~BH_STATUS;
776 rc = BH_STATUS;
777 }
778
779 if (!rc) {
780 /* Mark BH routine as complete */
Joe Perches0fab6de2008-04-28 02:14:02 -0700781 info->bh_running = false;
782 info->bh_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Jeff Garzikd12341f2007-10-19 15:45:35 -0400784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return rc;
788}
789
David Howellsc4028952006-11-22 14:57:56 +0000790static void bh_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
David Howellsc4028952006-11-22 14:57:56 +0000792 MGSLPC_INFO *info = container_of(work, MGSLPC_INFO, task);
Alan Coxeeb46132009-01-02 13:48:47 +0000793 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int action;
795
796 if (!info)
797 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (debug_level >= DEBUG_LEVEL_BH)
800 printk( "%s(%d):bh_handler(%s) entry\n",
801 __FILE__,__LINE__,info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400802
Joe Perches0fab6de2008-04-28 02:14:02 -0700803 info->bh_running = true;
Alan Coxeeb46132009-01-02 13:48:47 +0000804 tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 while((action = bh_action(info)) != 0) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /* Process work item */
809 if ( debug_level >= DEBUG_LEVEL_BH )
810 printk( "%s(%d):bh_handler() work item action=%d\n",
811 __FILE__,__LINE__,action);
812
813 switch (action) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 case BH_RECEIVE:
Alan Coxeeb46132009-01-02 13:48:47 +0000816 while(rx_get_frame(info, tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 break;
818 case BH_TRANSMIT:
Alan Coxeeb46132009-01-02 13:48:47 +0000819 bh_transmit(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 break;
821 case BH_STATUS:
822 bh_status(info);
823 break;
824 default:
825 /* unknown work item ID */
826 printk("Unknown work item ID=%08X!\n", action);
827 break;
828 }
829 }
830
Alan Coxeeb46132009-01-02 13:48:47 +0000831 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (debug_level >= DEBUG_LEVEL_BH)
833 printk( "%s(%d):bh_handler(%s) exit\n",
834 __FILE__,__LINE__,info->device_name);
835}
836
Alan Coxeeb46132009-01-02 13:48:47 +0000837static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (debug_level >= DEBUG_LEVEL_BH)
840 printk("bh_transmit() entry on %s\n", info->device_name);
841
Jiri Slabyb963a842007-02-10 01:44:55 -0800842 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Peter Hagervallcdaad342006-06-23 02:06:04 -0700846static void bh_status(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 info->ri_chkcount = 0;
849 info->dsr_chkcount = 0;
850 info->dcd_chkcount = 0;
851 info->cts_chkcount = 0;
852}
853
Jeff Garzikd12341f2007-10-19 15:45:35 -0400854/* eom: non-zero = end of frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
856{
857 unsigned char data[2];
858 unsigned char fifo_count, read_count, i;
859 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
860
861 if (debug_level >= DEBUG_LEVEL_ISR)
862 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (!info->rx_enabled)
865 return;
866
867 if (info->rx_frame_count >= info->rx_buf_count) {
868 /* no more free buffers */
869 issue_command(info, CHA, CMD_RXRESET);
870 info->pending_bh |= BH_RECEIVE;
Joe Perches0fab6de2008-04-28 02:14:02 -0700871 info->rx_overflow = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 info->icount.buf_overrun++;
873 return;
874 }
875
876 if (eom) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400877 /* end of frame, get FIFO count from RBCL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
879 fifo_count = 32;
880 } else
881 fifo_count = 32;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 do {
884 if (fifo_count == 1) {
885 read_count = 1;
886 data[0] = read_reg(info, CHA + RXFIFO);
887 } else {
888 read_count = 2;
889 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
890 }
891 fifo_count -= read_count;
892 if (!fifo_count && eom)
893 buf->status = data[--read_count];
894
895 for (i = 0; i < read_count; i++) {
896 if (buf->count >= info->max_frame_size) {
897 /* frame too large, reset receiver and reset current buffer */
898 issue_command(info, CHA, CMD_RXRESET);
899 buf->count = 0;
900 return;
901 }
902 *(buf->data + buf->count) = data[i];
903 buf->count++;
904 }
905 } while (fifo_count);
906
907 if (eom) {
908 info->pending_bh |= BH_RECEIVE;
909 info->rx_frame_count++;
910 info->rx_put++;
911 if (info->rx_put >= info->rx_buf_count)
912 info->rx_put = 0;
913 }
914 issue_command(info, CHA, CMD_RXFIFO);
915}
916
Alan Coxeeb46132009-01-02 13:48:47 +0000917static void rx_ready_async(MGSLPC_INFO *info, int tcd, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Alan Cox33f0f882006-01-09 20:54:13 -0800919 unsigned char data, status, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int fifo_count;
Alan Cox33f0f882006-01-09 20:54:13 -0800921 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct mgsl_icount *icount = &info->icount;
923
924 if (tcd) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400925 /* early termination, get FIFO count from RBCL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
927
928 /* Zero fifo count could mean 0 or 32 bytes available.
929 * If BIT5 of STAR is set then at least 1 byte is available.
930 */
931 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
932 fifo_count = 32;
933 } else
934 fifo_count = 32;
Alan Cox33f0f882006-01-09 20:54:13 -0800935
936 tty_buffer_request_room(tty, fifo_count);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400937 /* Flush received async data to receive data buffer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 while (fifo_count) {
939 data = read_reg(info, CHA + RXFIFO);
940 status = read_reg(info, CHA + RXFIFO);
941 fifo_count -= 2;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 icount->rx++;
Alan Cox33f0f882006-01-09 20:54:13 -0800944 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 // if no frameing/crc error then save data
947 // BIT7:parity error
948 // BIT6:framing error
949
950 if (status & (BIT7 + BIT6)) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400951 if (status & BIT7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 icount->parity++;
953 else
954 icount->frame++;
955
956 /* discard char if tty control flags say so */
957 if (status & info->ignore_status_mask)
958 continue;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 status &= info->read_status_mask;
961
962 if (status & BIT7)
Alan Cox33f0f882006-01-09 20:54:13 -0800963 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 else if (status & BIT6)
Alan Cox33f0f882006-01-09 20:54:13 -0800965 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
Alan Cox33f0f882006-01-09 20:54:13 -0800967 work += tty_insert_flip_char(tty, data, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969 issue_command(info, CHA, CMD_RXFIFO);
970
971 if (debug_level >= DEBUG_LEVEL_ISR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800972 printk("%s(%d):rx_ready_async",
973 __FILE__,__LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
975 __FILE__,__LINE__,icount->rx,icount->brk,
976 icount->parity,icount->frame,icount->overrun);
977 }
Jeff Garzikd12341f2007-10-19 15:45:35 -0400978
Alan Cox33f0f882006-01-09 20:54:13 -0800979 if (work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 tty_flip_buffer_push(tty);
981}
982
983
Alan Coxeeb46132009-01-02 13:48:47 +0000984static void tx_done(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 if (!info->tx_active)
987 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400988
Joe Perches0fab6de2008-04-28 02:14:02 -0700989 info->tx_active = false;
990 info->tx_aborting = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (info->params.mode == MGSL_MODE_ASYNC)
993 return;
994
995 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400996 del_timer(&info->tx_timer);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (info->drop_rts_on_tx_done) {
999 get_signals(info);
1000 if (info->serial_signals & SerialSignal_RTS) {
1001 info->serial_signals &= ~SerialSignal_RTS;
1002 set_signals(info);
1003 }
Joe Perches0fab6de2008-04-28 02:14:02 -07001004 info->drop_rts_on_tx_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08001007#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (info->netcount)
1009 hdlcdev_tx_done(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001010 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011#endif
1012 {
Alan Coxeeb46132009-01-02 13:48:47 +00001013 if (tty->stopped || tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 tx_stop(info);
1015 return;
1016 }
1017 info->pending_bh |= BH_TRANSMIT;
1018 }
1019}
1020
Alan Coxeeb46132009-01-02 13:48:47 +00001021static void tx_ready(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 unsigned char fifo_count = 32;
1024 int c;
1025
1026 if (debug_level >= DEBUG_LEVEL_ISR)
1027 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1028
1029 if (info->params.mode == MGSL_MODE_HDLC) {
1030 if (!info->tx_active)
1031 return;
1032 } else {
Alan Coxeeb46132009-01-02 13:48:47 +00001033 if (tty->stopped || tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 tx_stop(info);
1035 return;
1036 }
1037 if (!info->tx_count)
Joe Perches0fab6de2008-04-28 02:14:02 -07001038 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040
1041 if (!info->tx_count)
1042 return;
1043
1044 while (info->tx_count && fifo_count) {
1045 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
Jeff Garzikd12341f2007-10-19 15:45:35 -04001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (c == 1) {
1048 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1049 } else {
1050 write_reg16(info, CHA + TXFIFO,
1051 *((unsigned short*)(info->tx_buf + info->tx_get)));
1052 }
1053 info->tx_count -= c;
1054 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1055 fifo_count -= c;
1056 }
1057
1058 if (info->params.mode == MGSL_MODE_ASYNC) {
1059 if (info->tx_count < WAKEUP_CHARS)
1060 info->pending_bh |= BH_TRANSMIT;
1061 issue_command(info, CHA, CMD_TXFIFO);
1062 } else {
1063 if (info->tx_count)
1064 issue_command(info, CHA, CMD_TXFIFO);
1065 else
1066 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1067 }
1068}
1069
Alan Coxeeb46132009-01-02 13:48:47 +00001070static void cts_change(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 get_signals(info);
1073 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1074 irq_disable(info, CHB, IRQ_CTS);
1075 info->icount.cts++;
1076 if (info->serial_signals & SerialSignal_CTS)
1077 info->input_signal_events.cts_up++;
1078 else
1079 info->input_signal_events.cts_down++;
1080 wake_up_interruptible(&info->status_event_wait_q);
1081 wake_up_interruptible(&info->event_wait_q);
1082
Alan Coxeeb46132009-01-02 13:48:47 +00001083 if (info->port.flags & ASYNC_CTS_FLOW) {
1084 if (tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (info->serial_signals & SerialSignal_CTS) {
1086 if (debug_level >= DEBUG_LEVEL_ISR)
1087 printk("CTS tx start...");
Alan Coxeeb46132009-01-02 13:48:47 +00001088 if (tty)
1089 tty->hw_stopped = 0;
1090 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 info->pending_bh |= BH_TRANSMIT;
1092 return;
1093 }
1094 } else {
1095 if (!(info->serial_signals & SerialSignal_CTS)) {
1096 if (debug_level >= DEBUG_LEVEL_ISR)
1097 printk("CTS tx stop...");
Alan Coxeeb46132009-01-02 13:48:47 +00001098 if (tty)
1099 tty->hw_stopped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 tx_stop(info);
1101 }
1102 }
1103 }
1104 info->pending_bh |= BH_STATUS;
1105}
1106
Alan Coxeeb46132009-01-02 13:48:47 +00001107static void dcd_change(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 get_signals(info);
1110 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1111 irq_disable(info, CHB, IRQ_DCD);
1112 info->icount.dcd++;
1113 if (info->serial_signals & SerialSignal_DCD) {
1114 info->input_signal_events.dcd_up++;
1115 }
1116 else
1117 info->input_signal_events.dcd_down++;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08001118#if SYNCLINK_GENERIC_HDLC
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07001119 if (info->netcount) {
1120 if (info->serial_signals & SerialSignal_DCD)
1121 netif_carrier_on(info->netdev);
1122 else
1123 netif_carrier_off(info->netdev);
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125#endif
1126 wake_up_interruptible(&info->status_event_wait_q);
1127 wake_up_interruptible(&info->event_wait_q);
1128
Alan Coxeeb46132009-01-02 13:48:47 +00001129 if (info->port.flags & ASYNC_CHECK_CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (debug_level >= DEBUG_LEVEL_ISR)
1131 printk("%s CD now %s...", info->device_name,
1132 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1133 if (info->serial_signals & SerialSignal_DCD)
Alan Coxeeb46132009-01-02 13:48:47 +00001134 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 else {
1136 if (debug_level >= DEBUG_LEVEL_ISR)
1137 printk("doing serial hangup...");
Alan Coxeeb46132009-01-02 13:48:47 +00001138 if (tty)
1139 tty_hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 }
1142 info->pending_bh |= BH_STATUS;
1143}
1144
1145static void dsr_change(MGSLPC_INFO *info)
1146{
1147 get_signals(info);
1148 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1149 port_irq_disable(info, PVR_DSR);
1150 info->icount.dsr++;
1151 if (info->serial_signals & SerialSignal_DSR)
1152 info->input_signal_events.dsr_up++;
1153 else
1154 info->input_signal_events.dsr_down++;
1155 wake_up_interruptible(&info->status_event_wait_q);
1156 wake_up_interruptible(&info->event_wait_q);
1157 info->pending_bh |= BH_STATUS;
1158}
1159
1160static void ri_change(MGSLPC_INFO *info)
1161{
1162 get_signals(info);
1163 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1164 port_irq_disable(info, PVR_RI);
1165 info->icount.rng++;
1166 if (info->serial_signals & SerialSignal_RI)
1167 info->input_signal_events.ri_up++;
1168 else
1169 info->input_signal_events.ri_down++;
1170 wake_up_interruptible(&info->status_event_wait_q);
1171 wake_up_interruptible(&info->event_wait_q);
1172 info->pending_bh |= BH_STATUS;
1173}
1174
1175/* Interrupt service routine entry point.
Jeff Garzikd12341f2007-10-19 15:45:35 -04001176 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001178 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 * irq interrupt number that caused interrupt
1180 * dev_id device ID supplied during interrupt registration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
Jeff Garzika6f97b22007-10-31 05:20:49 -04001182static irqreturn_t mgslpc_isr(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Jeff Garzika6f97b22007-10-31 05:20:49 -04001184 MGSLPC_INFO *info = dev_id;
Alan Coxeeb46132009-01-02 13:48:47 +00001185 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 unsigned short isr;
1187 unsigned char gis, pis;
1188 int count=0;
1189
Jeff Garzikd12341f2007-10-19 15:45:35 -04001190 if (debug_level >= DEBUG_LEVEL_ISR)
Jeff Garzika6f97b22007-10-31 05:20:49 -04001191 printk("mgslpc_isr(%d) entry.\n", info->irq_level);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001192
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001193 if (!(info->p_dev->_locked))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return IRQ_HANDLED;
1195
Alan Coxeeb46132009-01-02 13:48:47 +00001196 tty = tty_port_tty_get(&info->port);
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 spin_lock(&info->lock);
1199
1200 while ((gis = read_reg(info, CHA + GIS))) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001201 if (debug_level >= DEBUG_LEVEL_ISR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1203
1204 if ((gis & 0x70) || count > 1000) {
1205 printk("synclink_cs:hardware failed or ejected\n");
1206 break;
1207 }
1208 count++;
1209
1210 if (gis & (BIT1 + BIT0)) {
1211 isr = read_reg16(info, CHB + ISR);
1212 if (isr & IRQ_DCD)
Alan Coxeeb46132009-01-02 13:48:47 +00001213 dcd_change(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (isr & IRQ_CTS)
Alan Coxeeb46132009-01-02 13:48:47 +00001215 cts_change(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217 if (gis & (BIT3 + BIT2))
1218 {
1219 isr = read_reg16(info, CHA + ISR);
1220 if (isr & IRQ_TIMER) {
Joe Perches0fab6de2008-04-28 02:14:02 -07001221 info->irq_occurred = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 irq_disable(info, CHA, IRQ_TIMER);
1223 }
1224
Jeff Garzikd12341f2007-10-19 15:45:35 -04001225 /* receive IRQs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (isr & IRQ_EXITHUNT) {
1227 info->icount.exithunt++;
1228 wake_up_interruptible(&info->event_wait_q);
1229 }
1230 if (isr & IRQ_BREAK_ON) {
1231 info->icount.brk++;
Alan Coxeeb46132009-01-02 13:48:47 +00001232 if (info->port.flags & ASYNC_SAK)
1233 do_SAK(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 if (isr & IRQ_RXTIME) {
1236 issue_command(info, CHA, CMD_RXFIFO_READ);
1237 }
1238 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1239 if (info->params.mode == MGSL_MODE_HDLC)
Jeff Garzikd12341f2007-10-19 15:45:35 -04001240 rx_ready_hdlc(info, isr & IRQ_RXEOM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 else
Alan Coxeeb46132009-01-02 13:48:47 +00001242 rx_ready_async(info, isr & IRQ_RXEOM, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244
Jeff Garzikd12341f2007-10-19 15:45:35 -04001245 /* transmit IRQs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (isr & IRQ_UNDERRUN) {
1247 if (info->tx_aborting)
1248 info->icount.txabort++;
1249 else
1250 info->icount.txunder++;
Alan Coxeeb46132009-01-02 13:48:47 +00001251 tx_done(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253 else if (isr & IRQ_ALLSENT) {
1254 info->icount.txok++;
Alan Coxeeb46132009-01-02 13:48:47 +00001255 tx_done(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257 else if (isr & IRQ_TXFIFO)
Alan Coxeeb46132009-01-02 13:48:47 +00001258 tx_ready(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 if (gis & BIT7) {
1261 pis = read_reg(info, CHA + PIS);
1262 if (pis & BIT1)
1263 dsr_change(info);
1264 if (pis & BIT2)
1265 ri_change(info);
1266 }
1267 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001268
1269 /* Request bottom half processing if there's something
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 * for it to do and the bh is not already running
1271 */
1272
1273 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001274 if ( debug_level >= DEBUG_LEVEL_ISR )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 printk("%s(%d):%s queueing bh task.\n",
1276 __FILE__,__LINE__,info->device_name);
1277 schedule_work(&info->task);
Joe Perches0fab6de2008-04-28 02:14:02 -07001278 info->bh_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
1281 spin_unlock(&info->lock);
Alan Coxeeb46132009-01-02 13:48:47 +00001282 tty_kref_put(tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001283
1284 if (debug_level >= DEBUG_LEVEL_ISR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 printk("%s(%d):mgslpc_isr(%d)exit.\n",
Jeff Garzika6f97b22007-10-31 05:20:49 -04001286 __FILE__, __LINE__, info->irq_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 return IRQ_HANDLED;
1289}
1290
1291/* Initialize and start device.
1292 */
Alan Coxeeb46132009-01-02 13:48:47 +00001293static int startup(MGSLPC_INFO * info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 int retval = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (debug_level >= DEBUG_LEVEL_INFO)
1298 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001299
Alan Coxeeb46132009-01-02 13:48:47 +00001300 if (info->port.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (!info->tx_buf) {
1304 /* allocate a page of memory for a transmit buffer */
1305 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1306 if (!info->tx_buf) {
1307 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1308 __FILE__,__LINE__,info->device_name);
1309 return -ENOMEM;
1310 }
1311 }
1312
1313 info->pending_bh = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001314
Paul Fulghuma7482a22005-09-10 00:26:07 -07001315 memset(&info->icount, 0, sizeof(info->icount));
1316
Jiri Slaby40565f12007-02-12 00:52:31 -08001317 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 /* Allocate and claim adapter resources */
1320 retval = claim_resources(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 /* perform existance check and diagnostics */
1323 if ( !retval )
1324 retval = adapter_test(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if ( retval ) {
Alan Coxeeb46132009-01-02 13:48:47 +00001327 if (capable(CAP_SYS_ADMIN) && tty)
1328 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 release_resources(info);
1330 return retval;
1331 }
1332
1333 /* program hardware for current parameters */
Alan Coxeeb46132009-01-02 13:48:47 +00001334 mgslpc_change_params(info, tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001335
Alan Coxeeb46132009-01-02 13:48:47 +00001336 if (tty)
1337 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Alan Coxeeb46132009-01-02 13:48:47 +00001339 info->port.flags |= ASYNC_INITIALIZED;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return 0;
1342}
1343
1344/* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1345 */
Alan Coxeeb46132009-01-02 13:48:47 +00001346static void shutdown(MGSLPC_INFO * info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001349
Alan Coxeeb46132009-01-02 13:48:47 +00001350 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return;
1352
1353 if (debug_level >= DEBUG_LEVEL_INFO)
1354 printk("%s(%d):mgslpc_shutdown(%s)\n",
1355 __FILE__,__LINE__, info->device_name );
1356
1357 /* clear status wait queue because status changes */
1358 /* can't happen after shutting down the hardware */
1359 wake_up_interruptible(&info->status_event_wait_q);
1360 wake_up_interruptible(&info->event_wait_q);
1361
Jiri Slaby40565f12007-02-12 00:52:31 -08001362 del_timer_sync(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 if (info->tx_buf) {
1365 free_page((unsigned long) info->tx_buf);
1366 info->tx_buf = NULL;
1367 }
1368
1369 spin_lock_irqsave(&info->lock,flags);
1370
1371 rx_stop(info);
1372 tx_stop(info);
1373
1374 /* TODO:disable interrupts instead of reset to preserve signal states */
1375 reset_device(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001376
Alan Coxeeb46132009-01-02 13:48:47 +00001377 if (!tty || tty->termios->c_cflag & HUPCL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1379 set_signals(info);
1380 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 spin_unlock_irqrestore(&info->lock,flags);
1383
Jeff Garzikd12341f2007-10-19 15:45:35 -04001384 release_resources(info);
1385
Alan Coxeeb46132009-01-02 13:48:47 +00001386 if (tty)
1387 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Alan Coxeeb46132009-01-02 13:48:47 +00001389 info->port.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Alan Coxeeb46132009-01-02 13:48:47 +00001392static void mgslpc_program_hw(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 unsigned long flags;
1395
1396 spin_lock_irqsave(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 rx_stop(info);
1399 tx_stop(info);
1400 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1403 hdlc_mode(info);
1404 else
1405 async_mode(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 set_signals(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 info->dcd_chkcount = 0;
1410 info->cts_chkcount = 0;
1411 info->ri_chkcount = 0;
1412 info->dsr_chkcount = 0;
1413
1414 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1415 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1416 get_signals(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001417
Alan Coxeeb46132009-01-02 13:48:47 +00001418 if (info->netcount || (tty && (tty->termios->c_cflag & CREAD)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 rx_start(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 spin_unlock_irqrestore(&info->lock,flags);
1422}
1423
1424/* Reconfigure adapter based on new parameters
1425 */
Alan Coxeeb46132009-01-02 13:48:47 +00001426static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 unsigned cflag;
1429 int bits_per_char;
1430
Alan Coxeeb46132009-01-02 13:48:47 +00001431 if (!tty || !tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (debug_level >= DEBUG_LEVEL_INFO)
1435 printk("%s(%d):mgslpc_change_params(%s)\n",
1436 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001437
Alan Coxeeb46132009-01-02 13:48:47 +00001438 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 /* if B0 rate (hangup) specified then negate DTR and RTS */
1441 /* otherwise assert DTR and RTS */
1442 if (cflag & CBAUD)
1443 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1444 else
1445 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* byte size and parity */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 switch (cflag & CSIZE) {
1450 case CS5: info->params.data_bits = 5; break;
1451 case CS6: info->params.data_bits = 6; break;
1452 case CS7: info->params.data_bits = 7; break;
1453 case CS8: info->params.data_bits = 8; break;
1454 default: info->params.data_bits = 7; break;
1455 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (cflag & CSTOPB)
1458 info->params.stop_bits = 2;
1459 else
1460 info->params.stop_bits = 1;
1461
1462 info->params.parity = ASYNC_PARITY_NONE;
1463 if (cflag & PARENB) {
1464 if (cflag & PARODD)
1465 info->params.parity = ASYNC_PARITY_ODD;
1466 else
1467 info->params.parity = ASYNC_PARITY_EVEN;
1468#ifdef CMSPAR
1469 if (cflag & CMSPAR)
1470 info->params.parity = ASYNC_PARITY_SPACE;
1471#endif
1472 }
1473
1474 /* calculate number of jiffies to transmit a full
1475 * FIFO (32 bytes) at specified data rate
1476 */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001477 bits_per_char = info->params.data_bits +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 info->params.stop_bits + 1;
1479
1480 /* if port data rate is set to 460800 or less then
1481 * allow tty settings to override, otherwise keep the
1482 * current data rate.
1483 */
1484 if (info->params.data_rate <= 460800) {
Alan Coxeeb46132009-01-02 13:48:47 +00001485 info->params.data_rate = tty_get_baud_rate(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if ( info->params.data_rate ) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001489 info->timeout = (32*HZ*bits_per_char) /
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 info->params.data_rate;
1491 }
1492 info->timeout += HZ/50; /* Add .02 seconds of slop */
1493
1494 if (cflag & CRTSCTS)
Alan Coxeeb46132009-01-02 13:48:47 +00001495 info->port.flags |= ASYNC_CTS_FLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 else
Alan Coxeeb46132009-01-02 13:48:47 +00001497 info->port.flags &= ~ASYNC_CTS_FLOW;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (cflag & CLOCAL)
Alan Coxeeb46132009-01-02 13:48:47 +00001500 info->port.flags &= ~ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 else
Alan Coxeeb46132009-01-02 13:48:47 +00001502 info->port.flags |= ASYNC_CHECK_CD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 /* process tty input control flags */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 info->read_status_mask = 0;
Alan Coxeeb46132009-01-02 13:48:47 +00001507 if (I_INPCK(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 info->read_status_mask |= BIT7 | BIT6;
Alan Coxeeb46132009-01-02 13:48:47 +00001509 if (I_IGNPAR(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 info->ignore_status_mask |= BIT7 | BIT6;
1511
Alan Coxeeb46132009-01-02 13:48:47 +00001512 mgslpc_program_hw(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515/* Add a character to the transmit buffer
1516 */
Alan Coxd7e752e2008-04-30 00:54:04 -07001517static int mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1520 unsigned long flags;
1521
1522 if (debug_level >= DEBUG_LEVEL_INFO) {
1523 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1524 __FILE__,__LINE__,ch,info->device_name);
1525 }
1526
1527 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
Alan Coxd7e752e2008-04-30 00:54:04 -07001528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Eric Sesterhenn326f28e92006-06-25 05:48:48 -07001530 if (!info->tx_buf)
Alan Coxd7e752e2008-04-30 00:54:04 -07001531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 spin_lock_irqsave(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1536 if (info->tx_count < TXBUFSIZE - 1) {
1537 info->tx_buf[info->tx_put++] = ch;
1538 info->tx_put &= TXBUFSIZE-1;
1539 info->tx_count++;
1540 }
1541 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 spin_unlock_irqrestore(&info->lock,flags);
Alan Coxd7e752e2008-04-30 00:54:04 -07001544 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
1547/* Enable transmitter so remaining characters in the
1548 * transmit buffer are sent.
1549 */
1550static void mgslpc_flush_chars(struct tty_struct *tty)
1551{
1552 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1553 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (debug_level >= DEBUG_LEVEL_INFO)
1556 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1557 __FILE__,__LINE__,info->device_name,info->tx_count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1560 return;
1561
1562 if (info->tx_count <= 0 || tty->stopped ||
1563 tty->hw_stopped || !info->tx_buf)
1564 return;
1565
1566 if (debug_level >= DEBUG_LEVEL_INFO)
1567 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1568 __FILE__,__LINE__,info->device_name);
1569
1570 spin_lock_irqsave(&info->lock,flags);
1571 if (!info->tx_active)
Alan Coxeeb46132009-01-02 13:48:47 +00001572 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 spin_unlock_irqrestore(&info->lock,flags);
1574}
1575
1576/* Send a block of data
Jeff Garzikd12341f2007-10-19 15:45:35 -04001577 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001579 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 * tty pointer to tty information structure
1581 * buf pointer to buffer containing send data
1582 * count size of send data in bytes
Jeff Garzikd12341f2007-10-19 15:45:35 -04001583 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * Returns: number of characters written
1585 */
1586static int mgslpc_write(struct tty_struct * tty,
1587 const unsigned char *buf, int count)
1588{
1589 int c, ret = 0;
1590 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1591 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (debug_level >= DEBUG_LEVEL_INFO)
1594 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1595 __FILE__,__LINE__,info->device_name,count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
Eric Sesterhenn326f28e92006-06-25 05:48:48 -07001598 !info->tx_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 goto cleanup;
1600
1601 if (info->params.mode == MGSL_MODE_HDLC) {
1602 if (count > TXBUFSIZE) {
1603 ret = -EIO;
1604 goto cleanup;
1605 }
1606 if (info->tx_active)
1607 goto cleanup;
1608 else if (info->tx_count)
1609 goto start;
1610 }
1611
1612 for (;;) {
1613 c = min(count,
1614 min(TXBUFSIZE - info->tx_count - 1,
1615 TXBUFSIZE - info->tx_put));
1616 if (c <= 0)
1617 break;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 memcpy(info->tx_buf + info->tx_put, buf, c);
1620
1621 spin_lock_irqsave(&info->lock,flags);
1622 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1623 info->tx_count += c;
1624 spin_unlock_irqrestore(&info->lock,flags);
1625
1626 buf += c;
1627 count -= c;
1628 ret += c;
1629 }
1630start:
1631 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1632 spin_lock_irqsave(&info->lock,flags);
1633 if (!info->tx_active)
Alan Coxeeb46132009-01-02 13:48:47 +00001634 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 spin_unlock_irqrestore(&info->lock,flags);
1636 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001637cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (debug_level >= DEBUG_LEVEL_INFO)
1639 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1640 __FILE__,__LINE__,info->device_name,ret);
1641 return ret;
1642}
1643
1644/* Return the count of free bytes in transmit buffer
1645 */
1646static int mgslpc_write_room(struct tty_struct *tty)
1647{
1648 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1649 int ret;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1652 return 0;
1653
1654 if (info->params.mode == MGSL_MODE_HDLC) {
1655 /* HDLC (frame oriented) mode */
1656 if (info->tx_active)
1657 return 0;
1658 else
1659 return HDLC_MAX_FRAME_SIZE;
1660 } else {
1661 ret = TXBUFSIZE - info->tx_count - 1;
1662 if (ret < 0)
1663 ret = 0;
1664 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (debug_level >= DEBUG_LEVEL_INFO)
1667 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1668 __FILE__,__LINE__, info->device_name, ret);
1669 return ret;
1670}
1671
1672/* Return the count of bytes in transmit buffer
1673 */
1674static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1675{
1676 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1677 int rc;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (debug_level >= DEBUG_LEVEL_INFO)
1680 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1681 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1684 return 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (info->params.mode == MGSL_MODE_HDLC)
1687 rc = info->tx_active ? info->max_frame_size : 0;
1688 else
1689 rc = info->tx_count;
1690
1691 if (debug_level >= DEBUG_LEVEL_INFO)
1692 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1693 __FILE__,__LINE__, info->device_name, rc);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return rc;
1696}
1697
1698/* Discard all data in the send buffer
1699 */
1700static void mgslpc_flush_buffer(struct tty_struct *tty)
1701{
1702 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1703 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (debug_level >= DEBUG_LEVEL_INFO)
1706 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1707 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1710 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001711
1712 spin_lock_irqsave(&info->lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001714 del_timer(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 spin_unlock_irqrestore(&info->lock,flags);
1716
1717 wake_up_interruptible(&tty->write_wait);
1718 tty_wakeup(tty);
1719}
1720
1721/* Send a high-priority XON/XOFF character
1722 */
1723static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1724{
1725 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1726 unsigned long flags;
1727
1728 if (debug_level >= DEBUG_LEVEL_INFO)
1729 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1730 __FILE__,__LINE__, info->device_name, ch );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1733 return;
1734
1735 info->x_char = ch;
1736 if (ch) {
1737 spin_lock_irqsave(&info->lock,flags);
1738 if (!info->tx_enabled)
Alan Coxeeb46132009-01-02 13:48:47 +00001739 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 spin_unlock_irqrestore(&info->lock,flags);
1741 }
1742}
1743
1744/* Signal remote device to throttle send data (our receive data)
1745 */
1746static void mgslpc_throttle(struct tty_struct * tty)
1747{
1748 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1749 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (debug_level >= DEBUG_LEVEL_INFO)
1752 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1753 __FILE__,__LINE__, info->device_name );
1754
1755 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1756 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (I_IXOFF(tty))
1759 mgslpc_send_xchar(tty, STOP_CHAR(tty));
Jeff Garzikd12341f2007-10-19 15:45:35 -04001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (tty->termios->c_cflag & CRTSCTS) {
1762 spin_lock_irqsave(&info->lock,flags);
1763 info->serial_signals &= ~SerialSignal_RTS;
1764 set_signals(info);
1765 spin_unlock_irqrestore(&info->lock,flags);
1766 }
1767}
1768
1769/* Signal remote device to stop throttling send data (our receive data)
1770 */
1771static void mgslpc_unthrottle(struct tty_struct * tty)
1772{
1773 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1774 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (debug_level >= DEBUG_LEVEL_INFO)
1777 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1778 __FILE__,__LINE__, info->device_name );
1779
1780 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1781 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (I_IXOFF(tty)) {
1784 if (info->x_char)
1785 info->x_char = 0;
1786 else
1787 mgslpc_send_xchar(tty, START_CHAR(tty));
1788 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (tty->termios->c_cflag & CRTSCTS) {
1791 spin_lock_irqsave(&info->lock,flags);
1792 info->serial_signals |= SerialSignal_RTS;
1793 set_signals(info);
1794 spin_unlock_irqrestore(&info->lock,flags);
1795 }
1796}
1797
1798/* get the current serial statistics
1799 */
1800static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1801{
1802 int err;
1803 if (debug_level >= DEBUG_LEVEL_INFO)
1804 printk("get_params(%s)\n", info->device_name);
Paul Fulghuma7482a22005-09-10 00:26:07 -07001805 if (!user_icount) {
1806 memset(&info->icount, 0, sizeof(info->icount));
1807 } else {
1808 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1809 if (err)
1810 return -EFAULT;
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
1813}
1814
1815/* get the current serial parameters
1816 */
1817static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1818{
1819 int err;
1820 if (debug_level >= DEBUG_LEVEL_INFO)
1821 printk("get_params(%s)\n", info->device_name);
1822 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1823 if (err)
1824 return -EFAULT;
1825 return 0;
1826}
1827
1828/* set the serial parameters
Jeff Garzikd12341f2007-10-19 15:45:35 -04001829 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001831 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 * info pointer to device instance data
1833 * new_params user buffer containing new serial params
1834 *
1835 * Returns: 0 if success, otherwise error code
1836 */
Alan Coxeeb46132009-01-02 13:48:47 +00001837static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 unsigned long flags;
1840 MGSL_PARAMS tmp_params;
1841 int err;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (debug_level >= DEBUG_LEVEL_INFO)
1844 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1845 info->device_name );
1846 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1847 if (err) {
1848 if ( debug_level >= DEBUG_LEVEL_INFO )
1849 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1850 __FILE__,__LINE__,info->device_name);
1851 return -EFAULT;
1852 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 spin_lock_irqsave(&info->lock,flags);
1855 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1856 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001857
Alan Coxeeb46132009-01-02 13:48:47 +00001858 mgslpc_change_params(info, tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return 0;
1861}
1862
1863static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1864{
1865 int err;
1866 if (debug_level >= DEBUG_LEVEL_INFO)
1867 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1868 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1869 if (err)
1870 return -EFAULT;
1871 return 0;
1872}
1873
1874static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1875{
1876 unsigned long flags;
1877 if (debug_level >= DEBUG_LEVEL_INFO)
1878 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1879 spin_lock_irqsave(&info->lock,flags);
1880 info->idle_mode = idle_mode;
1881 tx_set_idle(info);
1882 spin_unlock_irqrestore(&info->lock,flags);
1883 return 0;
1884}
1885
1886static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1887{
1888 int err;
1889 if (debug_level >= DEBUG_LEVEL_INFO)
1890 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1891 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1892 if (err)
1893 return -EFAULT;
1894 return 0;
1895}
1896
1897static int set_interface(MGSLPC_INFO * info, int if_mode)
1898{
1899 unsigned long flags;
1900 unsigned char val;
1901 if (debug_level >= DEBUG_LEVEL_INFO)
1902 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1903 spin_lock_irqsave(&info->lock,flags);
1904 info->if_mode = if_mode;
1905
1906 val = read_reg(info, PVR) & 0x0f;
1907 switch (info->if_mode)
1908 {
1909 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
1910 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
1911 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
1912 }
1913 write_reg(info, PVR, val);
1914
1915 spin_unlock_irqrestore(&info->lock,flags);
1916 return 0;
1917}
1918
Alan Coxeeb46132009-01-02 13:48:47 +00001919static int set_txenable(MGSLPC_INFO * info, int enable, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (debug_level >= DEBUG_LEVEL_INFO)
1924 printk("set_txenable(%s,%d)\n", info->device_name, enable);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 spin_lock_irqsave(&info->lock,flags);
1927 if (enable) {
1928 if (!info->tx_enabled)
Alan Coxeeb46132009-01-02 13:48:47 +00001929 tx_start(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 } else {
1931 if (info->tx_enabled)
1932 tx_stop(info);
1933 }
1934 spin_unlock_irqrestore(&info->lock,flags);
1935 return 0;
1936}
1937
1938static int tx_abort(MGSLPC_INFO * info)
1939{
1940 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (debug_level >= DEBUG_LEVEL_INFO)
1943 printk("tx_abort(%s)\n", info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 spin_lock_irqsave(&info->lock,flags);
1946 if (info->tx_active && info->tx_count &&
1947 info->params.mode == MGSL_MODE_HDLC) {
1948 /* clear data count so FIFO is not filled on next IRQ.
1949 * This results in underrun and abort transmission.
1950 */
1951 info->tx_count = info->tx_put = info->tx_get = 0;
Joe Perches0fab6de2008-04-28 02:14:02 -07001952 info->tx_aborting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954 spin_unlock_irqrestore(&info->lock,flags);
1955 return 0;
1956}
1957
1958static int set_rxenable(MGSLPC_INFO * info, int enable)
1959{
1960 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (debug_level >= DEBUG_LEVEL_INFO)
1963 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 spin_lock_irqsave(&info->lock,flags);
1966 if (enable) {
1967 if (!info->rx_enabled)
1968 rx_start(info);
1969 } else {
1970 if (info->rx_enabled)
1971 rx_stop(info);
1972 }
1973 spin_unlock_irqrestore(&info->lock,flags);
1974 return 0;
1975}
1976
1977/* wait for specified event to occur
Jeff Garzikd12341f2007-10-19 15:45:35 -04001978 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 * Arguments: info pointer to device instance data
1980 * mask pointer to bitmask of events to wait for
1981 * Return Value: 0 if successful and bit mask updated with
1982 * of events triggerred,
1983 * otherwise error code
1984 */
1985static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
1986{
1987 unsigned long flags;
1988 int s;
1989 int rc=0;
1990 struct mgsl_icount cprev, cnow;
1991 int events;
1992 int mask;
1993 struct _input_signal_events oldsigs, newsigs;
1994 DECLARE_WAITQUEUE(wait, current);
1995
1996 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
1997 if (rc)
1998 return -EFAULT;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (debug_level >= DEBUG_LEVEL_INFO)
2001 printk("wait_events(%s,%d)\n", info->device_name, mask);
2002
2003 spin_lock_irqsave(&info->lock,flags);
2004
2005 /* return immediately if state matches requested events */
2006 get_signals(info);
2007 s = info->serial_signals;
2008 events = mask &
2009 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2010 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2011 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2012 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2013 if (events) {
2014 spin_unlock_irqrestore(&info->lock,flags);
2015 goto exit;
2016 }
2017
2018 /* save current irq counts */
2019 cprev = info->icount;
2020 oldsigs = info->input_signal_events;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if ((info->params.mode == MGSL_MODE_HDLC) &&
2023 (mask & MgslEvent_ExitHuntMode))
2024 irq_enable(info, CHA, IRQ_EXITHUNT);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 set_current_state(TASK_INTERRUPTIBLE);
2027 add_wait_queue(&info->event_wait_q, &wait);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002030
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 for(;;) {
2033 schedule();
2034 if (signal_pending(current)) {
2035 rc = -ERESTARTSYS;
2036 break;
2037 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 /* get current irq counts */
2040 spin_lock_irqsave(&info->lock,flags);
2041 cnow = info->icount;
2042 newsigs = info->input_signal_events;
2043 set_current_state(TASK_INTERRUPTIBLE);
2044 spin_unlock_irqrestore(&info->lock,flags);
2045
2046 /* if no change, wait aborted for some reason */
2047 if (newsigs.dsr_up == oldsigs.dsr_up &&
2048 newsigs.dsr_down == oldsigs.dsr_down &&
2049 newsigs.dcd_up == oldsigs.dcd_up &&
2050 newsigs.dcd_down == oldsigs.dcd_down &&
2051 newsigs.cts_up == oldsigs.cts_up &&
2052 newsigs.cts_down == oldsigs.cts_down &&
2053 newsigs.ri_up == oldsigs.ri_up &&
2054 newsigs.ri_down == oldsigs.ri_down &&
2055 cnow.exithunt == cprev.exithunt &&
2056 cnow.rxidle == cprev.rxidle) {
2057 rc = -EIO;
2058 break;
2059 }
2060
2061 events = mask &
2062 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2063 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2064 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2065 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2066 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2067 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2068 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2069 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2070 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2071 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2072 if (events)
2073 break;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 cprev = cnow;
2076 oldsigs = newsigs;
2077 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 remove_wait_queue(&info->event_wait_q, &wait);
2080 set_current_state(TASK_RUNNING);
2081
2082 if (mask & MgslEvent_ExitHuntMode) {
2083 spin_lock_irqsave(&info->lock,flags);
2084 if (!waitqueue_active(&info->event_wait_q))
2085 irq_disable(info, CHA, IRQ_EXITHUNT);
2086 spin_unlock_irqrestore(&info->lock,flags);
2087 }
2088exit:
2089 if (rc == 0)
2090 PUT_USER(rc, events, mask_ptr);
2091 return rc;
2092}
2093
2094static int modem_input_wait(MGSLPC_INFO *info,int arg)
2095{
2096 unsigned long flags;
2097 int rc;
2098 struct mgsl_icount cprev, cnow;
2099 DECLARE_WAITQUEUE(wait, current);
2100
2101 /* save current irq counts */
2102 spin_lock_irqsave(&info->lock,flags);
2103 cprev = info->icount;
2104 add_wait_queue(&info->status_event_wait_q, &wait);
2105 set_current_state(TASK_INTERRUPTIBLE);
2106 spin_unlock_irqrestore(&info->lock,flags);
2107
2108 for(;;) {
2109 schedule();
2110 if (signal_pending(current)) {
2111 rc = -ERESTARTSYS;
2112 break;
2113 }
2114
2115 /* get new irq counts */
2116 spin_lock_irqsave(&info->lock,flags);
2117 cnow = info->icount;
2118 set_current_state(TASK_INTERRUPTIBLE);
2119 spin_unlock_irqrestore(&info->lock,flags);
2120
2121 /* if no change, wait aborted for some reason */
2122 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2123 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2124 rc = -EIO;
2125 break;
2126 }
2127
2128 /* check for change in caller specified modem input */
2129 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2130 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2131 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2132 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2133 rc = 0;
2134 break;
2135 }
2136
2137 cprev = cnow;
2138 }
2139 remove_wait_queue(&info->status_event_wait_q, &wait);
2140 set_current_state(TASK_RUNNING);
2141 return rc;
2142}
2143
2144/* return the state of the serial control and status signals
2145 */
2146static int tiocmget(struct tty_struct *tty, struct file *file)
2147{
2148 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2149 unsigned int result;
2150 unsigned long flags;
2151
2152 spin_lock_irqsave(&info->lock,flags);
2153 get_signals(info);
2154 spin_unlock_irqrestore(&info->lock,flags);
2155
2156 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2157 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2158 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2159 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2160 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2161 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2162
2163 if (debug_level >= DEBUG_LEVEL_INFO)
2164 printk("%s(%d):%s tiocmget() value=%08X\n",
2165 __FILE__,__LINE__, info->device_name, result );
2166 return result;
2167}
2168
2169/* set modem control signals (DTR/RTS)
2170 */
2171static int tiocmset(struct tty_struct *tty, struct file *file,
2172 unsigned int set, unsigned int clear)
2173{
2174 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2175 unsigned long flags;
2176
2177 if (debug_level >= DEBUG_LEVEL_INFO)
2178 printk("%s(%d):%s tiocmset(%x,%x)\n",
2179 __FILE__,__LINE__,info->device_name, set, clear);
2180
2181 if (set & TIOCM_RTS)
2182 info->serial_signals |= SerialSignal_RTS;
2183 if (set & TIOCM_DTR)
2184 info->serial_signals |= SerialSignal_DTR;
2185 if (clear & TIOCM_RTS)
2186 info->serial_signals &= ~SerialSignal_RTS;
2187 if (clear & TIOCM_DTR)
2188 info->serial_signals &= ~SerialSignal_DTR;
2189
2190 spin_lock_irqsave(&info->lock,flags);
2191 set_signals(info);
2192 spin_unlock_irqrestore(&info->lock,flags);
2193
2194 return 0;
2195}
2196
2197/* Set or clear transmit break condition
2198 *
2199 * Arguments: tty pointer to tty instance data
2200 * break_state -1=set break condition, 0=clear
2201 */
Alan Cox9e989662008-07-22 11:18:03 +01002202static int mgslpc_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2205 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (debug_level >= DEBUG_LEVEL_INFO)
2208 printk("%s(%d):mgslpc_break(%s,%d)\n",
2209 __FILE__,__LINE__, info->device_name, break_state);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
Alan Cox9e989662008-07-22 11:18:03 +01002212 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 spin_lock_irqsave(&info->lock,flags);
2215 if (break_state == -1)
2216 set_reg_bits(info, CHA+DAFO, BIT6);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002217 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 clear_reg_bits(info, CHA+DAFO, BIT6);
2219 spin_unlock_irqrestore(&info->lock,flags);
Alan Cox9e989662008-07-22 11:18:03 +01002220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
2223/* Service an IOCTL request
Jeff Garzikd12341f2007-10-19 15:45:35 -04002224 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04002226 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 * tty pointer to tty instance data
2228 * file pointer to associated file object for device
2229 * cmd IOCTL command code
2230 * arg command argument/context
Jeff Garzikd12341f2007-10-19 15:45:35 -04002231 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 * Return Value: 0 if success, otherwise error code
2233 */
2234static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2235 unsigned int cmd, unsigned long arg)
2236{
2237 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
Alan Coxeeb46132009-01-02 13:48:47 +00002238 int error;
2239 struct mgsl_icount cnow; /* kernel counter temps */
2240 struct serial_icounter_struct __user *p_cuser; /* user space */
2241 void __user *argp = (void __user *)arg;
2242 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (debug_level >= DEBUG_LEVEL_INFO)
2245 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2246 info->device_name, cmd );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2249 return -ENODEV;
2250
2251 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2252 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2253 if (tty->flags & (1 << TTY_IO_ERROR))
2254 return -EIO;
2255 }
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 switch (cmd) {
2258 case MGSL_IOCGPARAMS:
2259 return get_params(info, argp);
2260 case MGSL_IOCSPARAMS:
Alan Coxeeb46132009-01-02 13:48:47 +00002261 return set_params(info, argp, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 case MGSL_IOCGTXIDLE:
2263 return get_txidle(info, argp);
2264 case MGSL_IOCSTXIDLE:
2265 return set_txidle(info, (int)arg);
2266 case MGSL_IOCGIF:
2267 return get_interface(info, argp);
2268 case MGSL_IOCSIF:
2269 return set_interface(info,(int)arg);
2270 case MGSL_IOCTXENABLE:
Alan Coxeeb46132009-01-02 13:48:47 +00002271 return set_txenable(info,(int)arg, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 case MGSL_IOCRXENABLE:
2273 return set_rxenable(info,(int)arg);
2274 case MGSL_IOCTXABORT:
2275 return tx_abort(info);
2276 case MGSL_IOCGSTATS:
2277 return get_stats(info, argp);
2278 case MGSL_IOCWAITEVENT:
2279 return wait_events(info, argp);
2280 case TIOCMIWAIT:
2281 return modem_input_wait(info,(int)arg);
2282 case TIOCGICOUNT:
2283 spin_lock_irqsave(&info->lock,flags);
2284 cnow = info->icount;
2285 spin_unlock_irqrestore(&info->lock,flags);
2286 p_cuser = argp;
2287 PUT_USER(error,cnow.cts, &p_cuser->cts);
2288 if (error) return error;
2289 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2290 if (error) return error;
2291 PUT_USER(error,cnow.rng, &p_cuser->rng);
2292 if (error) return error;
2293 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2294 if (error) return error;
2295 PUT_USER(error,cnow.rx, &p_cuser->rx);
2296 if (error) return error;
2297 PUT_USER(error,cnow.tx, &p_cuser->tx);
2298 if (error) return error;
2299 PUT_USER(error,cnow.frame, &p_cuser->frame);
2300 if (error) return error;
2301 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2302 if (error) return error;
2303 PUT_USER(error,cnow.parity, &p_cuser->parity);
2304 if (error) return error;
2305 PUT_USER(error,cnow.brk, &p_cuser->brk);
2306 if (error) return error;
2307 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2308 if (error) return error;
2309 return 0;
2310 default:
2311 return -ENOIOCTLCMD;
2312 }
2313 return 0;
2314}
2315
2316/* Set new termios settings
Jeff Garzikd12341f2007-10-19 15:45:35 -04002317 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04002319 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 * tty pointer to tty structure
2321 * termios pointer to buffer to hold returned old termios
2322 */
Alan Cox606d0992006-12-08 02:38:45 -08002323static void mgslpc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
2325 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2326 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (debug_level >= DEBUG_LEVEL_INFO)
2329 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2330 tty->driver->name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 /* just return if nothing has changed */
2333 if ((tty->termios->c_cflag == old_termios->c_cflag)
Jeff Garzikd12341f2007-10-19 15:45:35 -04002334 && (RELEVANT_IFLAG(tty->termios->c_iflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 == RELEVANT_IFLAG(old_termios->c_iflag)))
2336 return;
2337
Alan Coxeeb46132009-01-02 13:48:47 +00002338 mgslpc_change_params(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 /* Handle transition to B0 status */
2341 if (old_termios->c_cflag & CBAUD &&
2342 !(tty->termios->c_cflag & CBAUD)) {
2343 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2344 spin_lock_irqsave(&info->lock,flags);
2345 set_signals(info);
2346 spin_unlock_irqrestore(&info->lock,flags);
2347 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /* Handle transition away from B0 status */
2350 if (!(old_termios->c_cflag & CBAUD) &&
2351 tty->termios->c_cflag & CBAUD) {
2352 info->serial_signals |= SerialSignal_DTR;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002353 if (!(tty->termios->c_cflag & CRTSCTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 !test_bit(TTY_THROTTLED, &tty->flags)) {
2355 info->serial_signals |= SerialSignal_RTS;
2356 }
2357 spin_lock_irqsave(&info->lock,flags);
2358 set_signals(info);
2359 spin_unlock_irqrestore(&info->lock,flags);
2360 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 /* Handle turning off CRTSCTS */
2363 if (old_termios->c_cflag & CRTSCTS &&
2364 !(tty->termios->c_cflag & CRTSCTS)) {
2365 tty->hw_stopped = 0;
2366 tx_release(tty);
2367 }
2368}
2369
2370static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2371{
2372 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
Alan Coxeeb46132009-01-02 13:48:47 +00002373 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2376 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (debug_level >= DEBUG_LEVEL_INFO)
2379 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
Alan Coxeeb46132009-01-02 13:48:47 +00002380 __FILE__,__LINE__, info->device_name, port->count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002381
Alan Coxeeb46132009-01-02 13:48:47 +00002382 WARN_ON(!port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Alan Coxeeb46132009-01-02 13:48:47 +00002384 if (tty_port_close_start(port, tty, filp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 goto cleanup;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002386
Alan Coxeeb46132009-01-02 13:48:47 +00002387 if (port->flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 mgslpc_wait_until_sent(tty, info->timeout);
2389
Alan Cox978e5952008-04-30 00:53:59 -07002390 mgslpc_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Alan Cox978e5952008-04-30 00:53:59 -07002392 tty_ldisc_flush(tty);
Alan Coxeeb46132009-01-02 13:48:47 +00002393 shutdown(info, tty);
2394
2395 tty_port_close_end(port, tty);
2396 tty_port_tty_set(port, NULL);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002397cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (debug_level >= DEBUG_LEVEL_INFO)
2399 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
Alan Coxeeb46132009-01-02 13:48:47 +00002400 tty->driver->name, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
2403/* Wait until the transmitter is empty.
2404 */
2405static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2406{
2407 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2408 unsigned long orig_jiffies, char_time;
2409
2410 if (!info )
2411 return;
2412
2413 if (debug_level >= DEBUG_LEVEL_INFO)
2414 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2415 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2418 return;
2419
Alan Coxeeb46132009-01-02 13:48:47 +00002420 if (!(info->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 goto exit;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 orig_jiffies = jiffies;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 /* Set check interval to 1/5 of estimated time to
2426 * send a character, and make it at least 1. The check
2427 * interval should also be less than the timeout.
2428 * Note: use tight timings here to satisfy the NIST-PCTS.
Jeff Garzikd12341f2007-10-19 15:45:35 -04002429 */
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if ( info->params.data_rate ) {
2432 char_time = info->timeout/(32 * 5);
2433 if (!char_time)
2434 char_time++;
2435 } else
2436 char_time = 1;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (timeout)
2439 char_time = min_t(unsigned long, char_time, timeout);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (info->params.mode == MGSL_MODE_HDLC) {
2442 while (info->tx_active) {
2443 msleep_interruptible(jiffies_to_msecs(char_time));
2444 if (signal_pending(current))
2445 break;
2446 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2447 break;
2448 }
2449 } else {
2450 while ((info->tx_count || info->tx_active) &&
2451 info->tx_enabled) {
2452 msleep_interruptible(jiffies_to_msecs(char_time));
2453 if (signal_pending(current))
2454 break;
2455 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2456 break;
2457 }
2458 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460exit:
2461 if (debug_level >= DEBUG_LEVEL_INFO)
2462 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2463 __FILE__,__LINE__, info->device_name );
2464}
2465
2466/* Called by tty_hangup() when a hangup is signaled.
2467 * This is the same as closing all open files for the port.
2468 */
2469static void mgslpc_hangup(struct tty_struct *tty)
2470{
2471 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (debug_level >= DEBUG_LEVEL_INFO)
2474 printk("%s(%d):mgslpc_hangup(%s)\n",
2475 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2478 return;
2479
2480 mgslpc_flush_buffer(tty);
Alan Coxeeb46132009-01-02 13:48:47 +00002481 shutdown(info, tty);
2482 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
Alan Coxeeb46132009-01-02 13:48:47 +00002485static int carrier_raised(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
Alan Coxeeb46132009-01-02 13:48:47 +00002487 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2488 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002489
Alan Coxeeb46132009-01-02 13:48:47 +00002490 spin_lock_irqsave(&info->lock,flags);
2491 get_signals(info);
2492 spin_unlock_irqrestore(&info->lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Alan Coxeeb46132009-01-02 13:48:47 +00002494 if (info->serial_signals & SerialSignal_DCD)
2495 return 1;
2496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
Alan Coxfcc8ac12009-06-11 12:24:17 +01002499static void dtr_rts(struct tty_port *port, int onoff)
Alan Coxeeb46132009-01-02 13:48:47 +00002500{
2501 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2502 unsigned long flags;
2503
2504 spin_lock_irqsave(&info->lock,flags);
Alan Coxfcc8ac12009-06-11 12:24:17 +01002505 if (onoff)
2506 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2507 else
2508 info->serial_signals &= ~SerialSignal_RTS + SerialSignal_DTR;
Alan Coxeeb46132009-01-02 13:48:47 +00002509 set_signals(info);
2510 spin_unlock_irqrestore(&info->lock,flags);
2511}
2512
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2515{
2516 MGSLPC_INFO *info;
Alan Coxeeb46132009-01-02 13:48:47 +00002517 struct tty_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 int retval, line;
2519 unsigned long flags;
2520
Jeff Garzikd12341f2007-10-19 15:45:35 -04002521 /* verify range of specified line number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 line = tty->index;
2523 if ((line < 0) || (line >= mgslpc_device_count)) {
2524 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2525 __FILE__,__LINE__,line);
2526 return -ENODEV;
2527 }
2528
2529 /* find the info structure for the specified line */
2530 info = mgslpc_device_list;
2531 while(info && info->line != line)
2532 info = info->next_device;
2533 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2534 return -ENODEV;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002535
Alan Coxeeb46132009-01-02 13:48:47 +00002536 port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 tty->driver_data = info;
Alan Coxeeb46132009-01-02 13:48:47 +00002538 tty_port_tty_set(port, tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if (debug_level >= DEBUG_LEVEL_INFO)
2541 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
Alan Coxeeb46132009-01-02 13:48:47 +00002542 __FILE__,__LINE__,tty->driver->name, port->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 /* If port is closing, signal caller to try again */
Alan Coxeeb46132009-01-02 13:48:47 +00002545 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING){
2546 if (port->flags & ASYNC_CLOSING)
2547 interruptible_sleep_on(&port->close_wait);
2548 retval = ((port->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 -EAGAIN : -ERESTARTSYS);
2550 goto cleanup;
2551 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002552
Alan Coxeeb46132009-01-02 13:48:47 +00002553 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 spin_lock_irqsave(&info->netlock, flags);
2556 if (info->netcount) {
2557 retval = -EBUSY;
2558 spin_unlock_irqrestore(&info->netlock, flags);
2559 goto cleanup;
2560 }
Alan Coxeeb46132009-01-02 13:48:47 +00002561 spin_lock(&port->lock);
2562 port->count++;
2563 spin_unlock(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 spin_unlock_irqrestore(&info->netlock, flags);
2565
Alan Coxeeb46132009-01-02 13:48:47 +00002566 if (port->count == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 /* 1st open on this device, init hardware */
Alan Coxeeb46132009-01-02 13:48:47 +00002568 retval = startup(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (retval < 0)
2570 goto cleanup;
2571 }
2572
Alan Coxeeb46132009-01-02 13:48:47 +00002573 retval = tty_port_block_til_ready(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (retval) {
2575 if (debug_level >= DEBUG_LEVEL_INFO)
2576 printk("%s(%d):block_til_ready(%s) returned %d\n",
2577 __FILE__,__LINE__, info->device_name, retval);
2578 goto cleanup;
2579 }
2580
2581 if (debug_level >= DEBUG_LEVEL_INFO)
2582 printk("%s(%d):mgslpc_open(%s) success\n",
2583 __FILE__,__LINE__, info->device_name);
2584 retval = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002585
2586cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return retval;
2588}
2589
2590/*
2591 * /proc fs routines....
2592 */
2593
Alexey Dobriyan87687142009-03-31 15:19:17 -07002594static inline void line_info(struct seq_file *m, MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
2596 char stat_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 unsigned long flags;
2598
Alexey Dobriyan87687142009-03-31 15:19:17 -07002599 seq_printf(m, "%s:io:%04X irq:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 info->device_name, info->io_base, info->irq_level);
2601
2602 /* output current serial signal states */
2603 spin_lock_irqsave(&info->lock,flags);
2604 get_signals(info);
2605 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 stat_buf[0] = 0;
2608 stat_buf[1] = 0;
2609 if (info->serial_signals & SerialSignal_RTS)
2610 strcat(stat_buf, "|RTS");
2611 if (info->serial_signals & SerialSignal_CTS)
2612 strcat(stat_buf, "|CTS");
2613 if (info->serial_signals & SerialSignal_DTR)
2614 strcat(stat_buf, "|DTR");
2615 if (info->serial_signals & SerialSignal_DSR)
2616 strcat(stat_buf, "|DSR");
2617 if (info->serial_signals & SerialSignal_DCD)
2618 strcat(stat_buf, "|CD");
2619 if (info->serial_signals & SerialSignal_RI)
2620 strcat(stat_buf, "|RI");
2621
2622 if (info->params.mode == MGSL_MODE_HDLC) {
Alexey Dobriyan87687142009-03-31 15:19:17 -07002623 seq_printf(m, " HDLC txok:%d rxok:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 info->icount.txok, info->icount.rxok);
2625 if (info->icount.txunder)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002626 seq_printf(m, " txunder:%d", info->icount.txunder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (info->icount.txabort)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002628 seq_printf(m, " txabort:%d", info->icount.txabort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (info->icount.rxshort)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002630 seq_printf(m, " rxshort:%d", info->icount.rxshort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 if (info->icount.rxlong)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002632 seq_printf(m, " rxlong:%d", info->icount.rxlong);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (info->icount.rxover)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002634 seq_printf(m, " rxover:%d", info->icount.rxover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (info->icount.rxcrc)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002636 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 } else {
Alexey Dobriyan87687142009-03-31 15:19:17 -07002638 seq_printf(m, " ASYNC tx:%d rx:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 info->icount.tx, info->icount.rx);
2640 if (info->icount.frame)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002641 seq_printf(m, " fe:%d", info->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (info->icount.parity)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002643 seq_printf(m, " pe:%d", info->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (info->icount.brk)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002645 seq_printf(m, " brk:%d", info->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (info->icount.overrun)
Alexey Dobriyan87687142009-03-31 15:19:17 -07002647 seq_printf(m, " oe:%d", info->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 /* Append serial signal status to end */
Alexey Dobriyan87687142009-03-31 15:19:17 -07002651 seq_printf(m, " %s\n", stat_buf+1);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002652
Alexey Dobriyan87687142009-03-31 15:19:17 -07002653 seq_printf(m, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 info->tx_active,info->bh_requested,info->bh_running,
2655 info->pending_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658/* Called to print information about devices
2659 */
Alexey Dobriyan87687142009-03-31 15:19:17 -07002660static int mgslpc_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 MGSLPC_INFO *info;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002663
Alexey Dobriyan87687142009-03-31 15:19:17 -07002664 seq_printf(m, "synclink driver:%s\n", driver_version);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 info = mgslpc_device_list;
2667 while( info ) {
Alexey Dobriyan87687142009-03-31 15:19:17 -07002668 line_info(m, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 info = info->next_device;
2670 }
Alexey Dobriyan87687142009-03-31 15:19:17 -07002671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
Alexey Dobriyan87687142009-03-31 15:19:17 -07002674static int mgslpc_proc_open(struct inode *inode, struct file *file)
2675{
2676 return single_open(file, mgslpc_proc_show, NULL);
2677}
2678
2679static const struct file_operations mgslpc_proc_fops = {
2680 .owner = THIS_MODULE,
2681 .open = mgslpc_proc_open,
2682 .read = seq_read,
2683 .llseek = seq_lseek,
2684 .release = single_release,
2685};
2686
Peter Hagervallcdaad342006-06-23 02:06:04 -07002687static int rx_alloc_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
2689 /* each buffer has header and data */
2690 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2691
2692 /* calculate total allocation size for 8 buffers */
2693 info->rx_buf_total_size = info->rx_buf_size * 8;
2694
2695 /* limit total allocated memory */
2696 if (info->rx_buf_total_size > 0x10000)
2697 info->rx_buf_total_size = 0x10000;
2698
2699 /* calculate number of buffers */
2700 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2701
2702 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2703 if (info->rx_buf == NULL)
2704 return -ENOMEM;
2705
2706 rx_reset_buffers(info);
2707 return 0;
2708}
2709
Peter Hagervallcdaad342006-06-23 02:06:04 -07002710static void rx_free_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Jesper Juhl735d5662005-11-07 01:01:29 -08002712 kfree(info->rx_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 info->rx_buf = NULL;
2714}
2715
Peter Hagervallcdaad342006-06-23 02:06:04 -07002716static int claim_resources(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 if (rx_alloc_buffers(info) < 0 ) {
2719 printk( "Cant allocate rx buffer %s\n", info->device_name);
2720 release_resources(info);
2721 return -ENODEV;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return 0;
2724}
2725
Peter Hagervallcdaad342006-06-23 02:06:04 -07002726static void release_resources(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727{
2728 if (debug_level >= DEBUG_LEVEL_INFO)
2729 printk("release_resources(%s)\n", info->device_name);
2730 rx_free_buffers(info);
2731}
2732
2733/* Add the specified device instance data structure to the
2734 * global linked list of devices and increment the device count.
Jeff Garzikd12341f2007-10-19 15:45:35 -04002735 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 * Arguments: info pointer to device instance data
2737 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07002738static void mgslpc_add_device(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739{
2740 info->next_device = NULL;
2741 info->line = mgslpc_device_count;
2742 sprintf(info->device_name,"ttySLP%d",info->line);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 if (info->line < MAX_DEVICE_COUNT) {
2745 if (maxframe[info->line])
2746 info->max_frame_size = maxframe[info->line];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
2748
2749 mgslpc_device_count++;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if (!mgslpc_device_list)
2752 mgslpc_device_list = info;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002753 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 MGSLPC_INFO *current_dev = mgslpc_device_list;
2755 while( current_dev->next_device )
2756 current_dev = current_dev->next_device;
2757 current_dev->next_device = info;
2758 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (info->max_frame_size < 4096)
2761 info->max_frame_size = 4096;
2762 else if (info->max_frame_size > 65535)
2763 info->max_frame_size = 65535;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2766 info->device_name, info->io_base, info->irq_level);
2767
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002768#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 hdlcdev_init(info);
2770#endif
2771}
2772
Peter Hagervallcdaad342006-06-23 02:06:04 -07002773static void mgslpc_remove_device(MGSLPC_INFO *remove_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
2775 MGSLPC_INFO *info = mgslpc_device_list;
2776 MGSLPC_INFO *last = NULL;
2777
2778 while(info) {
2779 if (info == remove_info) {
2780 if (last)
2781 last->next_device = info->next_device;
2782 else
2783 mgslpc_device_list = info->next_device;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002784#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 hdlcdev_exit(info);
2786#endif
2787 release_resources(info);
2788 kfree(info);
2789 mgslpc_device_count--;
2790 return;
2791 }
2792 last = info;
2793 info = info->next_device;
2794 }
2795}
2796
Dominik Brodowski4af48c82005-06-27 16:28:42 -07002797static struct pcmcia_device_id mgslpc_ids[] = {
2798 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
2799 PCMCIA_DEVICE_NULL
2800};
2801MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803static struct pcmcia_driver mgslpc_driver = {
2804 .owner = THIS_MODULE,
2805 .drv = {
2806 .name = "synclink_cs",
2807 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02002808 .probe = mgslpc_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01002809 .remove = mgslpc_detach,
Dominik Brodowski4af48c82005-06-27 16:28:42 -07002810 .id_table = mgslpc_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +01002811 .suspend = mgslpc_suspend,
2812 .resume = mgslpc_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813};
2814
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002815static const struct tty_operations mgslpc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 .open = mgslpc_open,
2817 .close = mgslpc_close,
2818 .write = mgslpc_write,
2819 .put_char = mgslpc_put_char,
2820 .flush_chars = mgslpc_flush_chars,
2821 .write_room = mgslpc_write_room,
2822 .chars_in_buffer = mgslpc_chars_in_buffer,
2823 .flush_buffer = mgslpc_flush_buffer,
2824 .ioctl = mgslpc_ioctl,
2825 .throttle = mgslpc_throttle,
2826 .unthrottle = mgslpc_unthrottle,
2827 .send_xchar = mgslpc_send_xchar,
2828 .break_ctl = mgslpc_break,
2829 .wait_until_sent = mgslpc_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 .set_termios = mgslpc_set_termios,
2831 .stop = tx_pause,
2832 .start = tx_release,
2833 .hangup = mgslpc_hangup,
2834 .tiocmget = tiocmget,
2835 .tiocmset = tiocmset,
Alexey Dobriyan87687142009-03-31 15:19:17 -07002836 .proc_fops = &mgslpc_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837};
2838
2839static void synclink_cs_cleanup(void)
2840{
2841 int rc;
2842
2843 printk("Unloading %s: version %s\n", driver_name, driver_version);
2844
2845 while(mgslpc_device_list)
2846 mgslpc_remove_device(mgslpc_device_list);
2847
2848 if (serial_driver) {
2849 if ((rc = tty_unregister_driver(serial_driver)))
2850 printk("%s(%d) failed to unregister tty driver err=%d\n",
2851 __FILE__,__LINE__,rc);
2852 put_tty_driver(serial_driver);
2853 }
2854
2855 pcmcia_unregister_driver(&mgslpc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856}
2857
2858static int __init synclink_cs_init(void)
2859{
2860 int rc;
2861
2862 if (break_on_load) {
2863 mgslpc_get_text_ptr();
2864 BREAKPOINT();
2865 }
2866
2867 printk("%s %s\n", driver_name, driver_version);
2868
2869 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
2870 return rc;
2871
2872 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
2873 if (!serial_driver) {
2874 rc = -ENOMEM;
2875 goto error;
2876 }
2877
2878 /* Initialize the tty_driver structure */
Jeff Garzikd12341f2007-10-19 15:45:35 -04002879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 serial_driver->owner = THIS_MODULE;
2881 serial_driver->driver_name = "synclink_cs";
2882 serial_driver->name = "ttySLP";
2883 serial_driver->major = ttymajor;
2884 serial_driver->minor_start = 64;
2885 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2886 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2887 serial_driver->init_termios = tty_std_termios;
2888 serial_driver->init_termios.c_cflag =
2889 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2890 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2891 tty_set_operations(serial_driver, &mgslpc_ops);
2892
2893 if ((rc = tty_register_driver(serial_driver)) < 0) {
2894 printk("%s(%d):Couldn't register serial driver\n",
2895 __FILE__,__LINE__);
2896 put_tty_driver(serial_driver);
2897 serial_driver = NULL;
2898 goto error;
2899 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 printk("%s %s, tty major#%d\n",
2902 driver_name, driver_version,
2903 serial_driver->major);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 return 0;
2906
2907error:
2908 synclink_cs_cleanup();
2909 return rc;
2910}
2911
Jeff Garzikd12341f2007-10-19 15:45:35 -04002912static void __exit synclink_cs_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 synclink_cs_cleanup();
2915}
2916
2917module_init(synclink_cs_init);
2918module_exit(synclink_cs_exit);
2919
2920static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
2921{
2922 unsigned int M, N;
2923 unsigned char val;
2924
Jeff Garzikd12341f2007-10-19 15:45:35 -04002925 /* note:standard BRG mode is broken in V3.2 chip
2926 * so enhanced mode is always used
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 */
2928
2929 if (rate) {
2930 N = 3686400 / rate;
2931 if (!N)
2932 N = 1;
2933 N >>= 1;
2934 for (M = 1; N > 64 && M < 16; M++)
2935 N >>= 1;
2936 N--;
2937
2938 /* BGR[5..0] = N
2939 * BGR[9..6] = M
2940 * BGR[7..0] contained in BGR register
2941 * BGR[9..8] contained in CCR2[7..6]
2942 * divisor = (N+1)*2^M
2943 *
2944 * Note: M *must* not be zero (causes asymetric duty cycle)
Jeff Garzikd12341f2007-10-19 15:45:35 -04002945 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 write_reg(info, (unsigned char) (channel + BGR),
2947 (unsigned char) ((M << 6) + N));
2948 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
2949 val |= ((M << 4) & 0xc0);
2950 write_reg(info, (unsigned char) (channel + CCR2), val);
2951 }
2952}
2953
2954/* Enabled the AUX clock output at the specified frequency.
2955 */
2956static void enable_auxclk(MGSLPC_INFO *info)
2957{
2958 unsigned char val;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /* MODE
2961 *
2962 * 07..06 MDS[1..0] 10 = transparent HDLC mode
2963 * 05 ADM Address Mode, 0 = no addr recognition
2964 * 04 TMD Timer Mode, 0 = external
2965 * 03 RAC Receiver Active, 0 = inactive
2966 * 02 RTS 0=RTS active during xmit, 1=RTS always active
2967 * 01 TRS Timer Resolution, 1=512
2968 * 00 TLP Test Loop, 0 = no loop
2969 *
2970 * 1000 0010
Jeff Garzikd12341f2007-10-19 15:45:35 -04002971 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 val = 0x82;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002973
2974 /* channel B RTS is used to enable AUXCLK driver on SP505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
2976 val |= BIT2;
2977 write_reg(info, CHB + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 /* CCR0
2980 *
2981 * 07 PU Power Up, 1=active, 0=power down
2982 * 06 MCE Master Clock Enable, 1=enabled
2983 * 05 Reserved, 0
2984 * 04..02 SC[2..0] Encoding
2985 * 01..00 SM[1..0] Serial Mode, 00=HDLC
2986 *
2987 * 11000000
Jeff Garzikd12341f2007-10-19 15:45:35 -04002988 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 write_reg(info, CHB + CCR0, 0xc0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 /* CCR1
2992 *
2993 * 07 SFLG Shared Flag, 0 = disable shared flags
2994 * 06 GALP Go Active On Loop, 0 = not used
2995 * 05 GLP Go On Loop, 0 = not used
2996 * 04 ODS Output Driver Select, 1=TxD is push-pull output
2997 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
2998 * 02..00 CM[2..0] Clock Mode
2999 *
3000 * 0001 0111
Jeff Garzikd12341f2007-10-19 15:45:35 -04003001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 write_reg(info, CHB + CCR1, 0x17);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 /* CCR2 (Channel B)
3005 *
3006 * 07..06 BGR[9..8] Baud rate bits 9..8
3007 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3008 * 04 SSEL Clock source select, 1=submode b
3009 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3010 * 02 RWX Read/Write Exchange 0=disabled
3011 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3012 * 00 DIV, data inversion 0=disabled, 1=enabled
3013 *
3014 * 0011 1000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3017 write_reg(info, CHB + CCR2, 0x38);
3018 else
3019 write_reg(info, CHB + CCR2, 0x30);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 /* CCR4
3022 *
3023 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3024 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3025 * 05 TST1 Test Pin, 0=normal operation
3026 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3027 * 03..02 Reserved, must be 0
3028 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3029 *
3030 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003031 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 write_reg(info, CHB + CCR4, 0x50);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 /* if auxclk not enabled, set internal BRG so
3035 * CTS transitions can be detected (requires TxC)
Jeff Garzikd12341f2007-10-19 15:45:35 -04003036 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3038 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3039 else
3040 mgslpc_set_rate(info, CHB, 921600);
3041}
3042
Jeff Garzikd12341f2007-10-19 15:45:35 -04003043static void loopback_enable(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
3045 unsigned char val;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003046
3047 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3049 write_reg(info, CHA + CCR1, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003050
3051 /* CCR2:04 SSEL Clock source select, 1=submode b */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3053 write_reg(info, CHA + CCR2, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003054
3055 /* set LinkSpeed if available, otherwise default to 2Mbps */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 if (info->params.clock_speed)
3057 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3058 else
3059 mgslpc_set_rate(info, CHA, 1843200);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003060
3061 /* MODE:00 TLP Test Loop, 1=loopback enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 val = read_reg(info, CHA + MODE) | BIT0;
3063 write_reg(info, CHA + MODE, val);
3064}
3065
Peter Hagervallcdaad342006-06-23 02:06:04 -07003066static void hdlc_mode(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
3068 unsigned char val;
3069 unsigned char clkmode, clksubmode;
3070
Jeff Garzikd12341f2007-10-19 15:45:35 -04003071 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 irq_disable(info, CHA, 0xffff);
3073 irq_disable(info, CHB, 0xffff);
3074 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003075
3076 /* assume clock mode 0a, rcv=RxC xmt=TxC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 clkmode = clksubmode = 0;
3078 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3079 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003080 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 clkmode = 7;
3082 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3083 && info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003084 /* clock mode 7b, rcv = BRG, xmt = BRG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 clkmode = 7;
3086 clksubmode = 1;
3087 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3088 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003089 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 clkmode = 6;
3091 clksubmode = 1;
3092 } else {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003093 /* clock mode 6a, rcv = DPLL, xmt = TxC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 clkmode = 6;
3095 }
3096 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003097 /* clock mode 0b, rcv = RxC, xmt = BRG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 clksubmode = 1;
3099 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04003100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 /* MODE
3102 *
3103 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3104 * 05 ADM Address Mode, 0 = no addr recognition
3105 * 04 TMD Timer Mode, 0 = external
3106 * 03 RAC Receiver Active, 0 = inactive
3107 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3108 * 01 TRS Timer Resolution, 1=512
3109 * 00 TLP Test Loop, 0 = no loop
3110 *
3111 * 1000 0010
Jeff Garzikd12341f2007-10-19 15:45:35 -04003112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 val = 0x82;
3114 if (info->params.loopback)
3115 val |= BIT0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003116
3117 /* preserve RTS state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (info->serial_signals & SerialSignal_RTS)
3119 val |= BIT2;
3120 write_reg(info, CHA + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 /* CCR0
3123 *
3124 * 07 PU Power Up, 1=active, 0=power down
3125 * 06 MCE Master Clock Enable, 1=enabled
3126 * 05 Reserved, 0
3127 * 04..02 SC[2..0] Encoding
3128 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3129 *
3130 * 11000000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 val = 0xc0;
3133 switch (info->params.encoding)
3134 {
3135 case HDLC_ENCODING_NRZI:
3136 val |= BIT3;
3137 break;
3138 case HDLC_ENCODING_BIPHASE_SPACE:
3139 val |= BIT4;
3140 break; // FM0
3141 case HDLC_ENCODING_BIPHASE_MARK:
3142 val |= BIT4 + BIT2;
3143 break; // FM1
3144 case HDLC_ENCODING_BIPHASE_LEVEL:
3145 val |= BIT4 + BIT3;
3146 break; // Manchester
3147 }
3148 write_reg(info, CHA + CCR0, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 /* CCR1
3151 *
3152 * 07 SFLG Shared Flag, 0 = disable shared flags
3153 * 06 GALP Go Active On Loop, 0 = not used
3154 * 05 GLP Go On Loop, 0 = not used
3155 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3156 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3157 * 02..00 CM[2..0] Clock Mode
3158 *
3159 * 0001 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 val = 0x10 + clkmode;
3162 write_reg(info, CHA + CCR1, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 /* CCR2
3165 *
3166 * 07..06 BGR[9..8] Baud rate bits 9..8
3167 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3168 * 04 SSEL Clock source select, 1=submode b
3169 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3170 * 02 RWX Read/Write Exchange 0=disabled
3171 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3172 * 00 DIV, data inversion 0=disabled, 1=enabled
3173 *
3174 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 val = 0x00;
3177 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3178 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3179 val |= BIT5;
3180 if (clksubmode)
3181 val |= BIT4;
3182 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3183 val |= BIT1;
3184 if (info->params.encoding == HDLC_ENCODING_NRZB)
3185 val |= BIT0;
3186 write_reg(info, CHA + CCR2, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 /* CCR3
3189 *
3190 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3191 * 05 EPT Enable preamble transmission, 1=enabled
3192 * 04 RADD Receive address pushed to FIFO, 0=disabled
3193 * 03 CRL CRC Reset Level, 0=FFFF
3194 * 02 RCRC Rx CRC 0=On 1=Off
3195 * 01 TCRC Tx CRC 0=On 1=Off
3196 * 00 PSD DPLL Phase Shift Disable
3197 *
3198 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 val = 0x00;
3201 if (info->params.crc_type == HDLC_CRC_NONE)
3202 val |= BIT2 + BIT1;
3203 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3204 val |= BIT5;
3205 switch (info->params.preamble_length)
3206 {
3207 case HDLC_PREAMBLE_LENGTH_16BITS:
3208 val |= BIT6;
3209 break;
3210 case HDLC_PREAMBLE_LENGTH_32BITS:
3211 val |= BIT6;
3212 break;
3213 case HDLC_PREAMBLE_LENGTH_64BITS:
3214 val |= BIT7 + BIT6;
3215 break;
3216 }
3217 write_reg(info, CHA + CCR3, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003218
3219 /* PRE - Preamble pattern */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 val = 0;
3221 switch (info->params.preamble)
3222 {
3223 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3224 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3225 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3226 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3227 }
3228 write_reg(info, CHA + PRE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 /* CCR4
3231 *
3232 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3233 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3234 * 05 TST1 Test Pin, 0=normal operation
3235 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3236 * 03..02 Reserved, must be 0
3237 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3238 *
3239 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 val = 0x50;
3242 write_reg(info, CHA + CCR4, val);
3243 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3244 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3245 else
3246 mgslpc_set_rate(info, CHA, info->params.clock_speed);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 /* RLCR Receive length check register
3249 *
3250 * 7 1=enable receive length check
3251 * 6..0 Max frame length = (RL + 1) * 32
Jeff Garzikd12341f2007-10-19 15:45:35 -04003252 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 write_reg(info, CHA + RLCR, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 /* XBCH Transmit Byte Count High
3256 *
3257 * 07 DMA mode, 0 = interrupt driven
3258 * 06 NRM, 0=ABM (ignored)
3259 * 05 CAS Carrier Auto Start
3260 * 04 XC Transmit Continuously (ignored)
3261 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3262 *
3263 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 val = 0x00;
3266 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3267 val |= BIT5;
3268 write_reg(info, CHA + XBCH, val);
3269 enable_auxclk(info);
3270 if (info->params.loopback || info->testing_irq)
3271 loopback_enable(info);
3272 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3273 {
3274 irq_enable(info, CHB, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003275 /* PVR[3] 1=AUTO CTS active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 set_reg_bits(info, CHA + PVR, BIT3);
3277 } else
3278 clear_reg_bits(info, CHA + PVR, BIT3);
3279
3280 irq_enable(info, CHA,
3281 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3282 IRQ_UNDERRUN + IRQ_TXFIFO);
3283 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3284 wait_command_complete(info, CHA);
3285 read_reg16(info, CHA + ISR); /* clear pending IRQs */
Jeff Garzikd12341f2007-10-19 15:45:35 -04003286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 /* Master clock mode enabled above to allow reset commands
3288 * to complete even if no data clocks are present.
3289 *
3290 * Disable master clock mode for normal communications because
3291 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3292 * IRQ when in master clock mode.
3293 *
3294 * Leave master clock mode enabled for IRQ test because the
3295 * timer IRQ used by the test can only happen in master clock mode.
Jeff Garzikd12341f2007-10-19 15:45:35 -04003296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 if (!info->testing_irq)
3298 clear_reg_bits(info, CHA + CCR0, BIT6);
3299
3300 tx_set_idle(info);
3301
3302 tx_stop(info);
3303 rx_stop(info);
3304}
3305
Peter Hagervallcdaad342006-06-23 02:06:04 -07003306static void rx_stop(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
3308 if (debug_level >= DEBUG_LEVEL_ISR)
3309 printk("%s(%d):rx_stop(%s)\n",
3310 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003311
3312 /* MODE:03 RAC Receiver Active, 0=inactive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 clear_reg_bits(info, CHA + MODE, BIT3);
3314
Joe Perches0fab6de2008-04-28 02:14:02 -07003315 info->rx_enabled = false;
3316 info->rx_overflow = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
Peter Hagervallcdaad342006-06-23 02:06:04 -07003319static void rx_start(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
3321 if (debug_level >= DEBUG_LEVEL_ISR)
3322 printk("%s(%d):rx_start(%s)\n",
3323 __FILE__,__LINE__, info->device_name );
3324
3325 rx_reset_buffers(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003326 info->rx_enabled = false;
3327 info->rx_overflow = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Jeff Garzikd12341f2007-10-19 15:45:35 -04003329 /* MODE:03 RAC Receiver Active, 1=active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 set_reg_bits(info, CHA + MODE, BIT3);
3331
Joe Perches0fab6de2008-04-28 02:14:02 -07003332 info->rx_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
Alan Coxeeb46132009-01-02 13:48:47 +00003335static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
3337 if (debug_level >= DEBUG_LEVEL_ISR)
3338 printk("%s(%d):tx_start(%s)\n",
3339 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 if (info->tx_count) {
3342 /* If auto RTS enabled and RTS is inactive, then assert */
3343 /* RTS and set a flag indicating that the driver should */
3344 /* negate RTS when the transmission completes. */
Joe Perches0fab6de2008-04-28 02:14:02 -07003345 info->drop_rts_on_tx_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
3347 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3348 get_signals(info);
3349 if (!(info->serial_signals & SerialSignal_RTS)) {
3350 info->serial_signals |= SerialSignal_RTS;
3351 set_signals(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003352 info->drop_rts_on_tx_done = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
3354 }
3355
3356 if (info->params.mode == MGSL_MODE_ASYNC) {
3357 if (!info->tx_active) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003358 info->tx_active = true;
Alan Coxeeb46132009-01-02 13:48:47 +00003359 tx_ready(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
3361 } else {
Joe Perches0fab6de2008-04-28 02:14:02 -07003362 info->tx_active = true;
Alan Coxeeb46132009-01-02 13:48:47 +00003363 tx_ready(info, tty);
Jiri Slaby40565f12007-02-12 00:52:31 -08003364 mod_timer(&info->tx_timer, jiffies +
3365 msecs_to_jiffies(5000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 }
3367 }
3368
3369 if (!info->tx_enabled)
Joe Perches0fab6de2008-04-28 02:14:02 -07003370 info->tx_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
Peter Hagervallcdaad342006-06-23 02:06:04 -07003373static void tx_stop(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374{
3375 if (debug_level >= DEBUG_LEVEL_ISR)
3376 printk("%s(%d):tx_stop(%s)\n",
3377 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003378
3379 del_timer(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Joe Perches0fab6de2008-04-28 02:14:02 -07003381 info->tx_enabled = false;
3382 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383}
3384
3385/* Reset the adapter to a known state and prepare it for further use.
3386 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003387static void reset_device(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003389 /* power up both channels (set BIT7) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 write_reg(info, CHA + CCR0, 0x80);
3391 write_reg(info, CHB + CCR0, 0x80);
3392 write_reg(info, CHA + MODE, 0);
3393 write_reg(info, CHB + MODE, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003394
3395 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 irq_disable(info, CHA, 0xffff);
3397 irq_disable(info, CHB, 0xffff);
3398 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 /* PCR Port Configuration Register
3401 *
3402 * 07..04 DEC[3..0] Serial I/F select outputs
3403 * 03 output, 1=AUTO CTS control enabled
3404 * 02 RI Ring Indicator input 0=active
3405 * 01 DSR input 0=active
3406 * 00 DTR output 0=active
3407 *
3408 * 0000 0110
Jeff Garzikd12341f2007-10-19 15:45:35 -04003409 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 write_reg(info, PCR, 0x06);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 /* PVR Port Value Register
3413 *
3414 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3415 * 03 AUTO CTS output 1=enabled
3416 * 02 RI Ring Indicator input
3417 * 01 DSR input
3418 * 00 DTR output (1=inactive)
3419 *
3420 * 0000 0001
3421 */
3422// write_reg(info, PVR, PVR_DTR);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 /* IPC Interrupt Port Configuration
3425 *
3426 * 07 VIS 1=Masked interrupts visible
3427 * 06..05 Reserved, 0
3428 * 04..03 SLA Slave address, 00 ignored
3429 * 02 CASM Cascading Mode, 1=daisy chain
3430 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3431 *
3432 * 0000 0101
Jeff Garzikd12341f2007-10-19 15:45:35 -04003433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 write_reg(info, IPC, 0x05);
3435}
3436
Peter Hagervallcdaad342006-06-23 02:06:04 -07003437static void async_mode(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
3439 unsigned char val;
3440
Jeff Garzikd12341f2007-10-19 15:45:35 -04003441 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 irq_disable(info, CHA, 0xffff);
3443 irq_disable(info, CHB, 0xffff);
3444 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 /* MODE
3447 *
3448 * 07 Reserved, 0
3449 * 06 FRTS RTS State, 0=active
3450 * 05 FCTS Flow Control on CTS
3451 * 04 FLON Flow Control Enable
3452 * 03 RAC Receiver Active, 0 = inactive
3453 * 02 RTS 0=Auto RTS, 1=manual RTS
3454 * 01 TRS Timer Resolution, 1=512
3455 * 00 TLP Test Loop, 0 = no loop
3456 *
3457 * 0000 0110
Jeff Garzikd12341f2007-10-19 15:45:35 -04003458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 val = 0x06;
3460 if (info->params.loopback)
3461 val |= BIT0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003462
3463 /* preserve RTS state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (!(info->serial_signals & SerialSignal_RTS))
3465 val |= BIT6;
3466 write_reg(info, CHA + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003467
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 /* CCR0
3469 *
3470 * 07 PU Power Up, 1=active, 0=power down
3471 * 06 MCE Master Clock Enable, 1=enabled
3472 * 05 Reserved, 0
3473 * 04..02 SC[2..0] Encoding, 000=NRZ
3474 * 01..00 SM[1..0] Serial Mode, 11=Async
3475 *
3476 * 1000 0011
Jeff Garzikd12341f2007-10-19 15:45:35 -04003477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 write_reg(info, CHA + CCR0, 0x83);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 /* CCR1
3481 *
3482 * 07..05 Reserved, 0
3483 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3484 * 03 BCR Bit Clock Rate, 1=16x
3485 * 02..00 CM[2..0] Clock Mode, 111=BRG
3486 *
3487 * 0001 1111
Jeff Garzikd12341f2007-10-19 15:45:35 -04003488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 write_reg(info, CHA + CCR1, 0x1f);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 /* CCR2 (channel A)
3492 *
3493 * 07..06 BGR[9..8] Baud rate bits 9..8
3494 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3495 * 04 SSEL Clock source select, 1=submode b
3496 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3497 * 02 RWX Read/Write Exchange 0=disabled
3498 * 01 Reserved, 0
3499 * 00 DIV, data inversion 0=disabled, 1=enabled
3500 *
3501 * 0001 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003502 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 write_reg(info, CHA + CCR2, 0x10);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 /* CCR3
3506 *
3507 * 07..01 Reserved, 0
3508 * 00 PSD DPLL Phase Shift Disable
3509 *
3510 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003511 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 write_reg(info, CHA + CCR3, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003513
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 /* CCR4
3515 *
3516 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3517 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3518 * 05 TST1 Test Pin, 0=normal operation
3519 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3520 * 03..00 Reserved, must be 0
3521 *
3522 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003523 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 write_reg(info, CHA + CCR4, 0x50);
3525 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 /* DAFO Data Format
3528 *
3529 * 07 Reserved, 0
3530 * 06 XBRK transmit break, 0=normal operation
3531 * 05 Stop bits (0=1, 1=2)
3532 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3533 * 02 PAREN Parity Enable
3534 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3535 *
Jeff Garzikd12341f2007-10-19 15:45:35 -04003536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 val = 0x00;
3538 if (info->params.data_bits != 8)
3539 val |= BIT0; /* 7 bits */
3540 if (info->params.stop_bits != 1)
3541 val |= BIT5;
3542 if (info->params.parity != ASYNC_PARITY_NONE)
3543 {
3544 val |= BIT2; /* Parity enable */
3545 if (info->params.parity == ASYNC_PARITY_ODD)
3546 val |= BIT3;
3547 else
3548 val |= BIT4;
3549 }
3550 write_reg(info, CHA + DAFO, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 /* RFC Rx FIFO Control
3553 *
3554 * 07 Reserved, 0
3555 * 06 DPS, 1=parity bit not stored in data byte
3556 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3557 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3558 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3559 * 01 Reserved, 0
3560 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3561 *
3562 * 0101 1100
Jeff Garzikd12341f2007-10-19 15:45:35 -04003563 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 write_reg(info, CHA + RFC, 0x5c);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 /* RLCR Receive length check register
3567 *
3568 * Max frame length = (RL + 1) * 32
Jeff Garzikd12341f2007-10-19 15:45:35 -04003569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 write_reg(info, CHA + RLCR, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 /* XBCH Transmit Byte Count High
3573 *
3574 * 07 DMA mode, 0 = interrupt driven
3575 * 06 NRM, 0=ABM (ignored)
3576 * 05 CAS Carrier Auto Start
3577 * 04 XC Transmit Continuously (ignored)
3578 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3579 *
3580 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003581 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 val = 0x00;
3583 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3584 val |= BIT5;
3585 write_reg(info, CHA + XBCH, val);
3586 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3587 irq_enable(info, CHA, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003588
3589 /* MODE:03 RAC Receiver Active, 1=active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 set_reg_bits(info, CHA + MODE, BIT3);
3591 enable_auxclk(info);
3592 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3593 irq_enable(info, CHB, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003594 /* PVR[3] 1=AUTO CTS active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 set_reg_bits(info, CHA + PVR, BIT3);
3596 } else
3597 clear_reg_bits(info, CHA + PVR, BIT3);
3598 irq_enable(info, CHA,
3599 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3600 IRQ_ALLSENT + IRQ_TXFIFO);
3601 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3602 wait_command_complete(info, CHA);
3603 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3604}
3605
3606/* Set the HDLC idle mode for the transmitter.
3607 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003608static void tx_set_idle(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003610 /* Note: ESCC2 only supports flags and one idle modes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3612 set_reg_bits(info, CHA + CCR1, BIT3);
3613 else
3614 clear_reg_bits(info, CHA + CCR1, BIT3);
3615}
3616
3617/* get state of the V24 status (input) signals.
3618 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003619static void get_signals(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 unsigned char status = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003622
3623 /* preserve DTR and RTS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3625
3626 if (read_reg(info, CHB + VSTR) & BIT7)
3627 info->serial_signals |= SerialSignal_DCD;
3628 if (read_reg(info, CHB + STAR) & BIT1)
3629 info->serial_signals |= SerialSignal_CTS;
3630
3631 status = read_reg(info, CHA + PVR);
3632 if (!(status & PVR_RI))
3633 info->serial_signals |= SerialSignal_RI;
3634 if (!(status & PVR_DSR))
3635 info->serial_signals |= SerialSignal_DSR;
3636}
3637
3638/* Set the state of DTR and RTS based on contents of
3639 * serial_signals member of device extension.
3640 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003641static void set_signals(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
3643 unsigned char val;
3644
3645 val = read_reg(info, CHA + MODE);
3646 if (info->params.mode == MGSL_MODE_ASYNC) {
3647 if (info->serial_signals & SerialSignal_RTS)
3648 val &= ~BIT6;
3649 else
3650 val |= BIT6;
3651 } else {
3652 if (info->serial_signals & SerialSignal_RTS)
3653 val |= BIT2;
3654 else
3655 val &= ~BIT2;
3656 }
3657 write_reg(info, CHA + MODE, val);
3658
3659 if (info->serial_signals & SerialSignal_DTR)
3660 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3661 else
3662 set_reg_bits(info, CHA + PVR, PVR_DTR);
3663}
3664
Peter Hagervallcdaad342006-06-23 02:06:04 -07003665static void rx_reset_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666{
3667 RXBUF *buf;
3668 int i;
3669
3670 info->rx_put = 0;
3671 info->rx_get = 0;
3672 info->rx_frame_count = 0;
3673 for (i=0 ; i < info->rx_buf_count ; i++) {
3674 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3675 buf->status = buf->count = 0;
3676 }
3677}
3678
3679/* Attempt to return a received HDLC frame
3680 * Only frames received without errors are returned.
3681 *
Joe Perches0fab6de2008-04-28 02:14:02 -07003682 * Returns true if frame returned, otherwise false
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 */
Alan Coxeeb46132009-01-02 13:48:47 +00003684static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
3686 unsigned short status;
3687 RXBUF *buf;
3688 unsigned int framesize = 0;
3689 unsigned long flags;
Joe Perches0fab6de2008-04-28 02:14:02 -07003690 bool return_frame = false;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003691
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 if (info->rx_frame_count == 0)
Joe Perches0fab6de2008-04-28 02:14:02 -07003693 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
3695 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3696
3697 status = buf->status;
3698
3699 /* 07 VFR 1=valid frame
3700 * 06 RDO 1=data overrun
3701 * 05 CRC 1=OK, 0=error
3702 * 04 RAB 1=frame aborted
3703 */
3704 if ((status & 0xf0) != 0xA0) {
3705 if (!(status & BIT7) || (status & BIT4))
3706 info->icount.rxabort++;
3707 else if (status & BIT6)
3708 info->icount.rxover++;
3709 else if (!(status & BIT5)) {
3710 info->icount.rxcrc++;
3711 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
Joe Perches0fab6de2008-04-28 02:14:02 -07003712 return_frame = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 }
3714 framesize = 0;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003715#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 {
Krzysztof Halasa198191c2008-06-30 23:26:53 +02003717 info->netdev->stats.rx_errors++;
3718 info->netdev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 }
3720#endif
3721 } else
Joe Perches0fab6de2008-04-28 02:14:02 -07003722 return_frame = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
3724 if (return_frame)
3725 framesize = buf->count;
3726
3727 if (debug_level >= DEBUG_LEVEL_BH)
3728 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3729 __FILE__,__LINE__,info->device_name,status,framesize);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003730
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 if (debug_level >= DEBUG_LEVEL_DATA)
Jeff Garzikd12341f2007-10-19 15:45:35 -04003732 trace_block(info, buf->data, framesize, 0);
3733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 if (framesize) {
3735 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3736 framesize+1 > info->max_frame_size) ||
3737 framesize > info->max_frame_size)
3738 info->icount.rxlong++;
3739 else {
3740 if (status & BIT5)
3741 info->icount.rxok++;
3742
3743 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3744 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3745 ++framesize;
3746 }
3747
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003748#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 if (info->netcount)
3750 hdlcdev_rx(info, buf->data, framesize);
3751 else
3752#endif
3753 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3754 }
3755 }
3756
3757 spin_lock_irqsave(&info->lock,flags);
3758 buf->status = buf->count = 0;
3759 info->rx_frame_count--;
3760 info->rx_get++;
3761 if (info->rx_get >= info->rx_buf_count)
3762 info->rx_get = 0;
3763 spin_unlock_irqrestore(&info->lock,flags);
3764
Joe Perches0fab6de2008-04-28 02:14:02 -07003765 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
3767
Joe Perches0fab6de2008-04-28 02:14:02 -07003768static bool register_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003770 static unsigned char patterns[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
Tobias Klauserfe971072006-01-09 20:54:02 -08003772 static unsigned int count = ARRAY_SIZE(patterns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 unsigned int i;
Joe Perches0fab6de2008-04-28 02:14:02 -07003774 bool rc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 unsigned long flags;
3776
3777 spin_lock_irqsave(&info->lock,flags);
3778 reset_device(info);
3779
3780 for (i = 0; i < count; i++) {
3781 write_reg(info, XAD1, patterns[i]);
3782 write_reg(info, XAD2, patterns[(i + 1) % count]);
Tobias Klauserfe971072006-01-09 20:54:02 -08003783 if ((read_reg(info, XAD1) != patterns[i]) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003785 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 break;
3787 }
3788 }
3789
3790 spin_unlock_irqrestore(&info->lock,flags);
3791 return rc;
3792}
3793
Joe Perches0fab6de2008-04-28 02:14:02 -07003794static bool irq_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
3796 unsigned long end_time;
3797 unsigned long flags;
3798
3799 spin_lock_irqsave(&info->lock,flags);
3800 reset_device(info);
3801
Joe Perches0fab6de2008-04-28 02:14:02 -07003802 info->testing_irq = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 hdlc_mode(info);
3804
Joe Perches0fab6de2008-04-28 02:14:02 -07003805 info->irq_occurred = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
3807 /* init hdlc mode */
3808
3809 irq_enable(info, CHA, IRQ_TIMER);
3810 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
3811 issue_command(info, CHA, CMD_START_TIMER);
3812
3813 spin_unlock_irqrestore(&info->lock,flags);
3814
3815 end_time=100;
3816 while(end_time-- && !info->irq_occurred) {
3817 msleep_interruptible(10);
3818 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04003819
Joe Perches0fab6de2008-04-28 02:14:02 -07003820 info->testing_irq = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822 spin_lock_irqsave(&info->lock,flags);
3823 reset_device(info);
3824 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003825
Joe Perches0fab6de2008-04-28 02:14:02 -07003826 return info->irq_occurred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827}
3828
Peter Hagervallcdaad342006-06-23 02:06:04 -07003829static int adapter_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
3831 if (!register_test(info)) {
3832 info->init_error = DiagStatus_AddressFailure;
3833 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
3834 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
3835 return -ENODEV;
3836 }
3837
3838 if (!irq_test(info)) {
3839 info->init_error = DiagStatus_IrqFailure;
3840 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
3841 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
3842 return -ENODEV;
3843 }
3844
3845 if (debug_level >= DEBUG_LEVEL_INFO)
3846 printk("%s(%d):device %s passed diagnostics\n",
3847 __FILE__,__LINE__,info->device_name);
3848 return 0;
3849}
3850
Peter Hagervallcdaad342006-06-23 02:06:04 -07003851static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852{
3853 int i;
3854 int linecount;
3855 if (xmit)
3856 printk("%s tx data:\n",info->device_name);
3857 else
3858 printk("%s rx data:\n",info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003859
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 while(count) {
3861 if (count > 16)
3862 linecount = 16;
3863 else
3864 linecount = count;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 for(i=0;i<linecount;i++)
3867 printk("%02X ",(unsigned char)data[i]);
3868 for(;i<17;i++)
3869 printk(" ");
3870 for(i=0;i<linecount;i++) {
3871 if (data[i]>=040 && data[i]<=0176)
3872 printk("%c",data[i]);
3873 else
3874 printk(".");
3875 }
3876 printk("\n");
Jeff Garzikd12341f2007-10-19 15:45:35 -04003877
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 data += linecount;
3879 count -= linecount;
3880 }
3881}
3882
3883/* HDLC frame time out
3884 * update stats and do tx completion processing
3885 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003886static void tx_timeout(unsigned long context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887{
3888 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
3889 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 if ( debug_level >= DEBUG_LEVEL_INFO )
3892 printk( "%s(%d):tx_timeout(%s)\n",
3893 __FILE__,__LINE__,info->device_name);
3894 if(info->tx_active &&
3895 info->params.mode == MGSL_MODE_HDLC) {
3896 info->icount.txtimeout++;
3897 }
3898 spin_lock_irqsave(&info->lock,flags);
Joe Perches0fab6de2008-04-28 02:14:02 -07003899 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 info->tx_count = info->tx_put = info->tx_get = 0;
3901
3902 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003903
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003904#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 if (info->netcount)
3906 hdlcdev_tx_done(info);
3907 else
3908#endif
Alan Coxeeb46132009-01-02 13:48:47 +00003909 {
3910 struct tty_struct *tty = tty_port_tty_get(&info->port);
3911 bh_transmit(info, tty);
3912 tty_kref_put(tty);
3913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914}
3915
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003916#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
3918/**
3919 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
3920 * set encoding and frame check sequence (FCS) options
3921 *
3922 * dev pointer to network device structure
3923 * encoding serial encoding setting
3924 * parity FCS setting
3925 *
3926 * returns 0 if success, otherwise error code
3927 */
3928static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
3929 unsigned short parity)
3930{
3931 MGSLPC_INFO *info = dev_to_port(dev);
Alan Coxeeb46132009-01-02 13:48:47 +00003932 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 unsigned char new_encoding;
3934 unsigned short new_crctype;
3935
3936 /* return error if TTY interface open */
Alan Coxeeb46132009-01-02 13:48:47 +00003937 if (info->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 return -EBUSY;
3939
3940 switch (encoding)
3941 {
3942 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
3943 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
3944 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
3945 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
3946 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
3947 default: return -EINVAL;
3948 }
3949
3950 switch (parity)
3951 {
3952 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
3953 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
3954 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
3955 default: return -EINVAL;
3956 }
3957
3958 info->params.encoding = new_encoding;
Alexey Dobriyan53b35312006-03-24 03:16:13 -08003959 info->params.crc_type = new_crctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
3961 /* if network interface up, reprogram hardware */
Alan Coxeeb46132009-01-02 13:48:47 +00003962 if (info->netcount) {
3963 tty = tty_port_tty_get(&info->port);
3964 mgslpc_program_hw(info, tty);
3965 tty_kref_put(tty);
3966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
3968 return 0;
3969}
3970
3971/**
3972 * called by generic HDLC layer to send frame
3973 *
3974 * skb socket buffer containing HDLC frame
3975 * dev pointer to network device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 */
Stephen Hemminger4c5d5022009-08-31 19:50:48 +00003977static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
3978 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
3980 MGSLPC_INFO *info = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 unsigned long flags;
3982
3983 if (debug_level >= DEBUG_LEVEL_INFO)
3984 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
3985
3986 /* stop sending until this frame completes */
3987 netif_stop_queue(dev);
3988
3989 /* copy data to device buffers */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003990 skb_copy_from_linear_data(skb, info->tx_buf, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 info->tx_get = 0;
3992 info->tx_put = info->tx_count = skb->len;
3993
3994 /* update network statistics */
Krzysztof Halasa198191c2008-06-30 23:26:53 +02003995 dev->stats.tx_packets++;
3996 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
3998 /* done with socket buffer, so free it */
3999 dev_kfree_skb(skb);
4000
4001 /* save start time for transmit timeout detection */
4002 dev->trans_start = jiffies;
4003
4004 /* start hardware transmitter if necessary */
4005 spin_lock_irqsave(&info->lock,flags);
Alan Coxeeb46132009-01-02 13:48:47 +00004006 if (!info->tx_active) {
4007 struct tty_struct *tty = tty_port_tty_get(&info->port);
4008 tx_start(info, tty);
4009 tty_kref_put(tty);
4010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 spin_unlock_irqrestore(&info->lock,flags);
4012
Stephen Hemminger4c5d5022009-08-31 19:50:48 +00004013 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014}
4015
4016/**
4017 * called by network layer when interface enabled
4018 * claim resources and initialize hardware
4019 *
4020 * dev pointer to network device structure
4021 *
4022 * returns 0 if success, otherwise error code
4023 */
4024static int hdlcdev_open(struct net_device *dev)
4025{
4026 MGSLPC_INFO *info = dev_to_port(dev);
Alan Coxeeb46132009-01-02 13:48:47 +00004027 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 int rc;
4029 unsigned long flags;
4030
4031 if (debug_level >= DEBUG_LEVEL_INFO)
4032 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4033
4034 /* generic HDLC layer open processing */
4035 if ((rc = hdlc_open(dev)))
4036 return rc;
4037
4038 /* arbitrate between network and tty opens */
4039 spin_lock_irqsave(&info->netlock, flags);
Alan Coxeeb46132009-01-02 13:48:47 +00004040 if (info->port.count != 0 || info->netcount != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4042 spin_unlock_irqrestore(&info->netlock, flags);
4043 return -EBUSY;
4044 }
4045 info->netcount=1;
4046 spin_unlock_irqrestore(&info->netlock, flags);
4047
Alan Coxeeb46132009-01-02 13:48:47 +00004048 tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 /* claim resources and init adapter */
Alan Coxeeb46132009-01-02 13:48:47 +00004050 if ((rc = startup(info, tty)) != 0) {
4051 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 spin_lock_irqsave(&info->netlock, flags);
4053 info->netcount=0;
4054 spin_unlock_irqrestore(&info->netlock, flags);
4055 return rc;
4056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 /* assert DTR and RTS, apply hardware settings */
4058 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
Alan Coxeeb46132009-01-02 13:48:47 +00004059 mgslpc_program_hw(info, tty);
4060 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
4062 /* enable network layer transmit */
4063 dev->trans_start = jiffies;
4064 netif_start_queue(dev);
4065
4066 /* inform generic HDLC layer of current DCD status */
4067 spin_lock_irqsave(&info->lock, flags);
4068 get_signals(info);
4069 spin_unlock_irqrestore(&info->lock, flags);
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07004070 if (info->serial_signals & SerialSignal_DCD)
4071 netif_carrier_on(dev);
4072 else
4073 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 return 0;
4075}
4076
4077/**
4078 * called by network layer when interface is disabled
4079 * shutdown hardware and release resources
4080 *
4081 * dev pointer to network device structure
4082 *
4083 * returns 0 if success, otherwise error code
4084 */
4085static int hdlcdev_close(struct net_device *dev)
4086{
4087 MGSLPC_INFO *info = dev_to_port(dev);
Alan Coxeeb46132009-01-02 13:48:47 +00004088 struct tty_struct *tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 unsigned long flags;
4090
4091 if (debug_level >= DEBUG_LEVEL_INFO)
4092 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4093
4094 netif_stop_queue(dev);
4095
4096 /* shutdown adapter and release resources */
Alan Coxeeb46132009-01-02 13:48:47 +00004097 shutdown(info, tty);
4098 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 hdlc_close(dev);
4100
4101 spin_lock_irqsave(&info->netlock, flags);
4102 info->netcount=0;
4103 spin_unlock_irqrestore(&info->netlock, flags);
4104
4105 return 0;
4106}
4107
4108/**
4109 * called by network layer to process IOCTL call to network device
4110 *
4111 * dev pointer to network device structure
4112 * ifr pointer to network interface request structure
4113 * cmd IOCTL command code
4114 *
4115 * returns 0 if success, otherwise error code
4116 */
4117static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4118{
4119 const size_t size = sizeof(sync_serial_settings);
4120 sync_serial_settings new_line;
4121 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4122 MGSLPC_INFO *info = dev_to_port(dev);
4123 unsigned int flags;
4124
4125 if (debug_level >= DEBUG_LEVEL_INFO)
4126 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4127
4128 /* return error if TTY interface open */
Alan Coxeeb46132009-01-02 13:48:47 +00004129 if (info->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 return -EBUSY;
4131
4132 if (cmd != SIOCWANDEV)
4133 return hdlc_ioctl(dev, ifr, cmd);
4134
4135 switch(ifr->ifr_settings.type) {
4136 case IF_GET_IFACE: /* return current sync_serial_settings */
4137
4138 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4139 if (ifr->ifr_settings.size < size) {
4140 ifr->ifr_settings.size = size; /* data size wanted */
4141 return -ENOBUFS;
4142 }
4143
4144 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4145 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4146 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4147 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4148
4149 switch (flags){
4150 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4151 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4152 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4153 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4154 default: new_line.clock_type = CLOCK_DEFAULT;
4155 }
4156
4157 new_line.clock_rate = info->params.clock_speed;
4158 new_line.loopback = info->params.loopback ? 1:0;
4159
4160 if (copy_to_user(line, &new_line, size))
4161 return -EFAULT;
4162 return 0;
4163
4164 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4165
4166 if(!capable(CAP_NET_ADMIN))
4167 return -EPERM;
4168 if (copy_from_user(&new_line, line, size))
4169 return -EFAULT;
4170
4171 switch (new_line.clock_type)
4172 {
4173 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4174 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4175 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4176 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4177 case CLOCK_DEFAULT: flags = info->params.flags &
4178 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4179 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4180 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4181 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4182 default: return -EINVAL;
4183 }
4184
4185 if (new_line.loopback != 0 && new_line.loopback != 1)
4186 return -EINVAL;
4187
4188 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4189 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4190 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4191 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4192 info->params.flags |= flags;
4193
4194 info->params.loopback = new_line.loopback;
4195
4196 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4197 info->params.clock_speed = new_line.clock_rate;
4198 else
4199 info->params.clock_speed = 0;
4200
4201 /* if network interface up, reprogram hardware */
Alan Coxeeb46132009-01-02 13:48:47 +00004202 if (info->netcount) {
4203 struct tty_struct *tty = tty_port_tty_get(&info->port);
4204 mgslpc_program_hw(info, tty);
4205 tty_kref_put(tty);
4206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 return 0;
4208
4209 default:
4210 return hdlc_ioctl(dev, ifr, cmd);
4211 }
4212}
4213
4214/**
4215 * called by network layer when transmit timeout is detected
4216 *
4217 * dev pointer to network device structure
4218 */
4219static void hdlcdev_tx_timeout(struct net_device *dev)
4220{
4221 MGSLPC_INFO *info = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 unsigned long flags;
4223
4224 if (debug_level >= DEBUG_LEVEL_INFO)
4225 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4226
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004227 dev->stats.tx_errors++;
4228 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
4230 spin_lock_irqsave(&info->lock,flags);
4231 tx_stop(info);
4232 spin_unlock_irqrestore(&info->lock,flags);
4233
4234 netif_wake_queue(dev);
4235}
4236
4237/**
4238 * called by device driver when transmit completes
4239 * reenable network layer transmit if stopped
4240 *
4241 * info pointer to device instance information
4242 */
4243static void hdlcdev_tx_done(MGSLPC_INFO *info)
4244{
4245 if (netif_queue_stopped(info->netdev))
4246 netif_wake_queue(info->netdev);
4247}
4248
4249/**
4250 * called by device driver when frame received
4251 * pass frame to network layer
4252 *
4253 * info pointer to device instance information
4254 * buf pointer to buffer contianing frame data
4255 * size count of data bytes in buf
4256 */
4257static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4258{
4259 struct sk_buff *skb = dev_alloc_skb(size);
4260 struct net_device *dev = info->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
4262 if (debug_level >= DEBUG_LEVEL_INFO)
4263 printk("hdlcdev_rx(%s)\n",dev->name);
4264
4265 if (skb == NULL) {
4266 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004267 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 return;
4269 }
4270
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004271 memcpy(skb_put(skb, size), buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004273 skb->protocol = hdlc_type_trans(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004275 dev->stats.rx_packets++;
4276 dev->stats.rx_bytes += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
4278 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01004281static const struct net_device_ops hdlcdev_ops = {
4282 .ndo_open = hdlcdev_open,
4283 .ndo_stop = hdlcdev_close,
4284 .ndo_change_mtu = hdlc_change_mtu,
4285 .ndo_start_xmit = hdlc_start_xmit,
4286 .ndo_do_ioctl = hdlcdev_ioctl,
4287 .ndo_tx_timeout = hdlcdev_tx_timeout,
4288};
4289
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290/**
4291 * called by device driver when adding device instance
4292 * do generic HDLC initialization
4293 *
4294 * info pointer to device instance information
4295 *
4296 * returns 0 if success, otherwise error code
4297 */
4298static int hdlcdev_init(MGSLPC_INFO *info)
4299{
4300 int rc;
4301 struct net_device *dev;
4302 hdlc_device *hdlc;
4303
4304 /* allocate and initialize network and HDLC layer objects */
4305
4306 if (!(dev = alloc_hdlcdev(info))) {
4307 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4308 return -ENOMEM;
4309 }
4310
4311 /* for network layer reporting purposes only */
4312 dev->base_addr = info->io_base;
4313 dev->irq = info->irq_level;
4314
4315 /* network layer callbacks and settings */
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01004316 dev->netdev_ops = &hdlcdev_ops;
4317 dev->watchdog_timeo = 10 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 dev->tx_queue_len = 50;
4319
4320 /* generic HDLC layer callbacks and settings */
4321 hdlc = dev_to_hdlc(dev);
4322 hdlc->attach = hdlcdev_attach;
4323 hdlc->xmit = hdlcdev_xmit;
4324
4325 /* register objects with HDLC layer */
4326 if ((rc = register_hdlc_device(dev))) {
4327 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4328 free_netdev(dev);
4329 return rc;
4330 }
4331
4332 info->netdev = dev;
4333 return 0;
4334}
4335
4336/**
4337 * called by device driver when removing device instance
4338 * do generic HDLC cleanup
4339 *
4340 * info pointer to device instance information
4341 */
4342static void hdlcdev_exit(MGSLPC_INFO *info)
4343{
4344 unregister_hdlc_device(info->netdev);
4345 free_netdev(info->netdev);
4346 info->netdev = NULL;
4347}
4348
4349#endif /* CONFIG_HDLC */
4350