blob: 583356426dfb1fb7a8c675bb82a4a0e1d3436ba6 [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>
54#include <linux/slab.h>
55#include <linux/netdevice.h>
56#include <linux/vmalloc.h>
57#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/delay.h>
59#include <linux/ioctl.h>
Robert P. J. Day3dd12472008-02-06 01:37:17 -080060#include <linux/synclink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include <asm/system.h>
63#include <asm/io.h>
64#include <asm/irq.h>
65#include <asm/dma.h>
66#include <linux/bitops.h>
67#include <asm/types.h>
68#include <linux/termios.h>
69#include <linux/workqueue.h>
70#include <linux/hdlc.h>
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <pcmcia/cs_types.h>
73#include <pcmcia/cs.h>
74#include <pcmcia/cistpl.h>
75#include <pcmcia/cisreg.h>
76#include <pcmcia/ds.h>
77
Paul Fulghumaf69c7f2006-12-06 20:40:24 -080078#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_CS_MODULE))
79#define SYNCLINK_GENERIC_HDLC 1
80#else
81#define SYNCLINK_GENERIC_HDLC 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
84#define GET_USER(error,value,addr) error = get_user(value,addr)
85#define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
86#define PUT_USER(error,value,addr) error = put_user(value,addr)
87#define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
88
89#include <asm/uaccess.h>
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static MGSL_PARAMS default_params = {
92 MGSL_MODE_HDLC, /* unsigned long mode */
93 0, /* unsigned char loopback; */
94 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
95 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
96 0, /* unsigned long clock_speed; */
97 0xff, /* unsigned char addr_filter; */
98 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
99 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
100 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
101 9600, /* unsigned long data_rate; */
102 8, /* unsigned char data_bits; */
103 1, /* unsigned char stop_bits; */
104 ASYNC_PARITY_NONE /* unsigned char parity; */
105};
106
107typedef struct
108{
109 int count;
110 unsigned char status;
111 char data[1];
112} RXBUF;
113
114/* The queue of BH actions to be performed */
115
116#define BH_RECEIVE 1
117#define BH_TRANSMIT 2
118#define BH_STATUS 4
119
120#define IO_PIN_SHUTDOWN_LIMIT 100
121
122#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
123
124struct _input_signal_events {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400125 int ri_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int ri_down;
127 int dsr_up;
128 int dsr_down;
129 int dcd_up;
130 int dcd_down;
131 int cts_up;
132 int cts_down;
133};
134
135
136/*
137 * Device instance data structure
138 */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140typedef struct _mgslpc_info {
141 void *if_ptr; /* General purpose pointer (used by SPPP) */
142 int magic;
143 int flags;
144 int count; /* count of opens */
145 int line;
146 unsigned short close_delay;
147 unsigned short closing_wait; /* time to wait before closing */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct mgsl_icount icount;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct tty_struct *tty;
152 int timeout;
153 int x_char; /* xon/xoff character */
154 int blocked_open; /* # of blocked opens */
155 unsigned char read_status_mask;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400156 unsigned char ignore_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 unsigned char *tx_buf;
159 int tx_put;
160 int tx_get;
161 int tx_count;
162
163 /* circular list of fixed length rx buffers */
164
165 unsigned char *rx_buf; /* memory allocated for all rx buffers */
166 int rx_buf_total_size; /* size of memory allocated for rx buffers */
167 int rx_put; /* index of next empty rx buffer */
168 int rx_get; /* index of next full rx buffer */
169 int rx_buf_size; /* size in bytes of single rx buffer */
170 int rx_buf_count; /* total number of rx buffers */
171 int rx_frame_count; /* number of full rx buffers */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 wait_queue_head_t open_wait;
174 wait_queue_head_t close_wait;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 wait_queue_head_t status_event_wait_q;
177 wait_queue_head_t event_wait_q;
178 struct timer_list tx_timer; /* HDLC transmit timeout timer */
179 struct _mgslpc_info *next_device; /* device list link */
180
181 unsigned short imra_value;
182 unsigned short imrb_value;
183 unsigned char pim_value;
184
185 spinlock_t lock;
186 struct work_struct task; /* task structure for scheduling bh */
187
188 u32 max_frame_size;
189
190 u32 pending_bh;
191
Joe Perches0fab6de2008-04-28 02:14:02 -0700192 bool bh_running;
193 bool bh_requested;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int dcd_chkcount; /* check counts to prevent */
196 int cts_chkcount; /* too many IRQs if a signal */
197 int dsr_chkcount; /* is floating */
198 int ri_chkcount;
199
Joe Perches0fab6de2008-04-28 02:14:02 -0700200 bool rx_enabled;
201 bool rx_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Joe Perches0fab6de2008-04-28 02:14:02 -0700203 bool tx_enabled;
204 bool tx_active;
205 bool tx_aborting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 u32 idle_mode;
207
208 int if_mode; /* serial interface selection (RS-232, v.35 etc) */
209
210 char device_name[25]; /* device instance name */
211
212 unsigned int io_base; /* base I/O address of adapter */
213 unsigned int irq_level;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 MGSL_PARAMS params; /* communications parameters */
216
217 unsigned char serial_signals; /* current serial signal states */
218
Joe Perches0fab6de2008-04-28 02:14:02 -0700219 bool irq_occurred; /* for diagnostics use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 char testing_irq;
221 unsigned int init_error; /* startup error (DIAGS) */
222
223 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
Joe Perches0fab6de2008-04-28 02:14:02 -0700224 bool drop_rts_on_tx_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 struct _input_signal_events input_signal_events;
227
228 /* PCMCIA support */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100229 struct pcmcia_device *p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 dev_node_t node;
231 int stop;
232
233 /* SPPP/Cisco HDLC device parts */
234 int netcount;
235 int dosyncppp;
236 spinlock_t netlock;
237
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800238#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 struct net_device *netdev;
240#endif
241
242} MGSLPC_INFO;
243
244#define MGSLPC_MAGIC 0x5402
245
246/*
247 * The size of the serial xmit buffer is 1 page, or 4096 bytes
248 */
249#define TXBUFSIZE 4096
250
Jeff Garzikd12341f2007-10-19 15:45:35 -0400251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#define CHA 0x00 /* channel A offset */
253#define CHB 0x40 /* channel B offset */
254
255/*
256 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
257 */
258#undef PVR
259
260#define RXFIFO 0
261#define TXFIFO 0
262#define STAR 0x20
263#define CMDR 0x20
264#define RSTA 0x21
265#define PRE 0x21
266#define MODE 0x22
267#define TIMR 0x23
268#define XAD1 0x24
269#define XAD2 0x25
270#define RAH1 0x26
271#define RAH2 0x27
272#define DAFO 0x27
273#define RAL1 0x28
274#define RFC 0x28
275#define RHCR 0x29
276#define RAL2 0x29
277#define RBCL 0x2a
278#define XBCL 0x2a
279#define RBCH 0x2b
280#define XBCH 0x2b
281#define CCR0 0x2c
282#define CCR1 0x2d
283#define CCR2 0x2e
284#define CCR3 0x2f
285#define VSTR 0x34
286#define BGR 0x34
287#define RLCR 0x35
288#define AML 0x36
289#define AMH 0x37
290#define GIS 0x38
291#define IVA 0x38
292#define IPC 0x39
293#define ISR 0x3a
294#define IMR 0x3a
295#define PVR 0x3c
296#define PIS 0x3d
297#define PIM 0x3d
298#define PCR 0x3e
299#define CCR4 0x3f
Jeff Garzikd12341f2007-10-19 15:45:35 -0400300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301// IMR/ISR
Jeff Garzikd12341f2007-10-19 15:45:35 -0400302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303#define IRQ_BREAK_ON BIT15 // rx break detected
304#define IRQ_DATAOVERRUN BIT14 // receive data overflow
305#define IRQ_ALLSENT BIT13 // all sent
306#define IRQ_UNDERRUN BIT12 // transmit data underrun
307#define IRQ_TIMER BIT11 // timer interrupt
308#define IRQ_CTS BIT10 // CTS status change
309#define IRQ_TXREPEAT BIT9 // tx message repeat
310#define IRQ_TXFIFO BIT8 // transmit pool ready
311#define IRQ_RXEOM BIT7 // receive message end
312#define IRQ_EXITHUNT BIT6 // receive frame start
313#define IRQ_RXTIME BIT6 // rx char timeout
314#define IRQ_DCD BIT2 // carrier detect status change
315#define IRQ_OVERRUN BIT1 // receive frame overflow
316#define IRQ_RXFIFO BIT0 // receive pool full
Jeff Garzikd12341f2007-10-19 15:45:35 -0400317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318// STAR
Jeff Garzikd12341f2007-10-19 15:45:35 -0400319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#define XFW BIT6 // transmit FIFO write enable
321#define CEC BIT2 // command executing
322#define CTS BIT1 // CTS state
Jeff Garzikd12341f2007-10-19 15:45:35 -0400323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#define PVR_DTR BIT0
325#define PVR_DSR BIT1
326#define PVR_RI BIT2
327#define PVR_AUTOCTS BIT3
328#define PVR_RS232 0x20 /* 0010b */
329#define PVR_V35 0xe0 /* 1110b */
330#define PVR_RS422 0x40 /* 0100b */
Jeff Garzikd12341f2007-10-19 15:45:35 -0400331
332/* Register access functions */
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
335#define read_reg(info, reg) inb((info)->io_base + (reg))
336
Jeff Garzikd12341f2007-10-19 15:45:35 -0400337#define read_reg16(info, reg) inw((info)->io_base + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
Jeff Garzikd12341f2007-10-19 15:45:35 -0400339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#define set_reg_bits(info, reg, mask) \
341 write_reg(info, (reg), \
Jeff Garzikd12341f2007-10-19 15:45:35 -0400342 (unsigned char) (read_reg(info, (reg)) | (mask)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343#define clear_reg_bits(info, reg, mask) \
344 write_reg(info, (reg), \
Jeff Garzikd12341f2007-10-19 15:45:35 -0400345 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
347 * interrupt enable/disable routines
Jeff Garzikd12341f2007-10-19 15:45:35 -0400348 */
349static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 if (channel == CHA) {
352 info->imra_value |= mask;
353 write_reg16(info, CHA + IMR, info->imra_value);
354 } else {
355 info->imrb_value |= mask;
356 write_reg16(info, CHB + IMR, info->imrb_value);
357 }
358}
Jeff Garzikd12341f2007-10-19 15:45:35 -0400359static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 if (channel == CHA) {
362 info->imra_value &= ~mask;
363 write_reg16(info, CHA + IMR, info->imra_value);
364 } else {
365 info->imrb_value &= ~mask;
366 write_reg16(info, CHB + IMR, info->imrb_value);
367 }
368}
369
370#define port_irq_disable(info, mask) \
371 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
372
373#define port_irq_enable(info, mask) \
374 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
375
376static void rx_start(MGSLPC_INFO *info);
377static void rx_stop(MGSLPC_INFO *info);
378
379static void tx_start(MGSLPC_INFO *info);
380static void tx_stop(MGSLPC_INFO *info);
381static void tx_set_idle(MGSLPC_INFO *info);
382
383static void get_signals(MGSLPC_INFO *info);
384static void set_signals(MGSLPC_INFO *info);
385
386static void reset_device(MGSLPC_INFO *info);
387
388static void hdlc_mode(MGSLPC_INFO *info);
389static void async_mode(MGSLPC_INFO *info);
390
391static void tx_timeout(unsigned long context);
392
393static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg);
394
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800395#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396#define dev_to_port(D) (dev_to_hdlc(D)->priv)
397static void hdlcdev_tx_done(MGSLPC_INFO *info);
398static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
399static int hdlcdev_init(MGSLPC_INFO *info);
400static void hdlcdev_exit(MGSLPC_INFO *info);
401#endif
402
403static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
404
Joe Perches0fab6de2008-04-28 02:14:02 -0700405static bool register_test(MGSLPC_INFO *info);
406static bool irq_test(MGSLPC_INFO *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static int adapter_test(MGSLPC_INFO *info);
408
409static int claim_resources(MGSLPC_INFO *info);
410static void release_resources(MGSLPC_INFO *info);
411static void mgslpc_add_device(MGSLPC_INFO *info);
412static void mgslpc_remove_device(MGSLPC_INFO *info);
413
Joe Perches0fab6de2008-04-28 02:14:02 -0700414static bool rx_get_frame(MGSLPC_INFO *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static void rx_reset_buffers(MGSLPC_INFO *info);
416static int rx_alloc_buffers(MGSLPC_INFO *info);
417static void rx_free_buffers(MGSLPC_INFO *info);
418
David Howells7d12e782006-10-05 14:55:46 +0100419static irqreturn_t mgslpc_isr(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421/*
422 * Bottom half interrupt handlers
423 */
David Howellsc4028952006-11-22 14:57:56 +0000424static void bh_handler(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static void bh_transmit(MGSLPC_INFO *info);
426static void bh_status(MGSLPC_INFO *info);
427
428/*
429 * ioctl handlers
430 */
431static int tiocmget(struct tty_struct *tty, struct file *file);
432static int tiocmset(struct tty_struct *tty, struct file *file,
433 unsigned int set, unsigned int clear);
434static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
435static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
436static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params);
437static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
438static int set_txidle(MGSLPC_INFO *info, int idle_mode);
439static int set_txenable(MGSLPC_INFO *info, int enable);
440static int tx_abort(MGSLPC_INFO *info);
441static int set_rxenable(MGSLPC_INFO *info, int enable);
442static int wait_events(MGSLPC_INFO *info, int __user *mask);
443
444static MGSLPC_INFO *mgslpc_device_list = NULL;
445static int mgslpc_device_count = 0;
446
447/*
448 * Set this param to non-zero to load eax with the
449 * .text section address and breakpoint on module load.
450 * This is useful for use with gdb and add-symbol-file command.
451 */
452static int break_on_load=0;
453
454/*
455 * Driver major number, defaults to zero to get auto
456 * assigned major number. May be forced as module parameter.
457 */
458static int ttymajor=0;
459
460static int debug_level = 0;
461static int maxframe[MAX_DEVICE_COUNT] = {0,};
462static int dosyncppp[MAX_DEVICE_COUNT] = {1,1,1,1};
463
464module_param(break_on_load, bool, 0);
465module_param(ttymajor, int, 0);
466module_param(debug_level, int, 0);
467module_param_array(maxframe, int, NULL, 0);
468module_param_array(dosyncppp, int, NULL, 0);
469
470MODULE_LICENSE("GPL");
471
472static char *driver_name = "SyncLink PC Card driver";
Paul Fulghuma7482a22005-09-10 00:26:07 -0700473static char *driver_version = "$Revision: 4.34 $";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475static struct tty_driver *serial_driver;
476
477/* number of characters left in xmit buffer before we ask for more */
478#define WAKEUP_CHARS 256
479
480static void mgslpc_change_params(MGSLPC_INFO *info);
481static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
482
483/* PCMCIA prototypes */
484
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200485static int mgslpc_config(struct pcmcia_device *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static void mgslpc_release(u_long arg);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100487static void mgslpc_detach(struct pcmcia_device *p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/*
490 * 1st function defined in .text section. Calling this function in
491 * init_module() followed by a breakpoint allows a remote debugger
492 * (gdb) to get the .text address for the add-symbol-file command.
493 * This allows remote debugging of dynamically loadable modules.
494 */
495static void* mgslpc_get_text_ptr(void)
496{
497 return mgslpc_get_text_ptr;
498}
499
500/**
501 * line discipline callback wrappers
502 *
503 * The wrappers maintain line discipline references
504 * while calling into the line discipline.
505 *
506 * ldisc_flush_buffer - flush line discipline receive buffers
507 * ldisc_receive_buf - pass receive data to line discipline
508 */
509
510static void ldisc_flush_buffer(struct tty_struct *tty)
511{
512 struct tty_ldisc *ld = tty_ldisc_ref(tty);
513 if (ld) {
514 if (ld->flush_buffer)
515 ld->flush_buffer(tty);
516 tty_ldisc_deref(ld);
517 }
518}
519
520static void ldisc_receive_buf(struct tty_struct *tty,
521 const __u8 *data, char *flags, int count)
522{
523 struct tty_ldisc *ld;
524 if (!tty)
525 return;
526 ld = tty_ldisc_ref(tty);
527 if (ld) {
528 if (ld->receive_buf)
529 ld->receive_buf(tty, data, flags, count);
530 tty_ldisc_deref(ld);
531 }
532}
533
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200534static int mgslpc_probe(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 MGSLPC_INFO *info;
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200537 int ret;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (debug_level >= DEBUG_LEVEL_INFO)
540 printk("mgslpc_attach\n");
Dominik Brodowskifd238232006-03-05 10:45:09 +0100541
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700542 info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (!info) {
544 printk("Error can't allocate device instance data\n");
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100545 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 info->magic = MGSLPC_MAGIC;
David Howellsc4028952006-11-22 14:57:56 +0000549 INIT_WORK(&info->task, bh_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 info->max_frame_size = 4096;
551 info->close_delay = 5*HZ/10;
552 info->closing_wait = 30*HZ;
553 init_waitqueue_head(&info->open_wait);
554 init_waitqueue_head(&info->close_wait);
555 init_waitqueue_head(&info->status_event_wait_q);
556 init_waitqueue_head(&info->event_wait_q);
557 spin_lock_init(&info->lock);
558 spin_lock_init(&info->netlock);
559 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
Jeff Garzikd12341f2007-10-19 15:45:35 -0400560 info->idle_mode = HDLC_TXIDLE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 info->imra_value = 0xffff;
562 info->imrb_value = 0xffff;
563 info->pim_value = 0xff;
564
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200565 info->p_dev = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 link->priv = info;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100567
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200568 /* Initialize the struct pcmcia_device structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* Interrupt setup */
571 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
Dominik Brodowski0c7ab672005-06-27 16:28:56 -0700572 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 link->irq.Handler = NULL;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 link->conf.Attributes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 link->conf.IntType = INT_MEMORY_AND_IO;
577
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200578 ret = mgslpc_config(link);
579 if (ret)
580 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 mgslpc_add_device(info);
583
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587/* Card has been inserted.
588 */
589
590#define CS_CHECK(fn, ret) \
591do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
592
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200593static int mgslpc_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 MGSLPC_INFO *info = link->priv;
596 tuple_t tuple;
597 cisparse_t parse;
598 int last_fn, last_ret;
599 u_char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 cistpl_cftable_entry_t dflt = { 0 };
601 cistpl_cftable_entry_t *cfg;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (debug_level >= DEBUG_LEVEL_INFO)
604 printk("mgslpc_config(0x%p)\n", link);
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 tuple.Attributes = 0;
607 tuple.TupleData = buf;
608 tuple.TupleDataMax = sizeof(buf);
609 tuple.TupleOffset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* get CIS configuration entry */
612
613 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200614 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 cfg = &(parse.cftable_entry);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200617 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
618 CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
621 if (cfg->index == 0)
622 goto cs_failed;
623
624 link->conf.ConfigIndex = cfg->index;
625 link->conf.Attributes |= CONF_ENABLE_IRQ;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* IO window settings */
628 link->io.NumPorts1 = 0;
629 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
630 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
631 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
632 if (!(io->flags & CISTPL_IO_8BIT))
633 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
634 if (!(io->flags & CISTPL_IO_16BIT))
635 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
636 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
637 link->io.BasePort1 = io->win[0].base;
638 link->io.NumPorts1 = io->win[0].len;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200639 CS_CHECK(RequestIO, pcmcia_request_io(link, &link->io));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
642 link->conf.Attributes = CONF_ENABLE_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 link->conf.IntType = INT_MEMORY_AND_IO;
644 link->conf.ConfigIndex = 8;
645 link->conf.Present = PRESENT_OPTION;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 link->irq.Attributes |= IRQ_HANDLE_PRESENT;
648 link->irq.Handler = mgslpc_isr;
649 link->irq.Instance = info;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200650 CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200652 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 info->io_base = link->io.BasePort1;
655 info->irq_level = link->irq.AssignedIRQ;
656
657 /* add to linked list of devices */
658 sprintf(info->node.dev_name, "mgslpc0");
659 info->node.major = info->node.minor = 0;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100660 link->dev_node = &info->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 printk(KERN_INFO "%s: index 0x%02x:",
663 info->node.dev_name, link->conf.ConfigIndex);
664 if (link->conf.Attributes & CONF_ENABLE_IRQ)
665 printk(", irq %d", link->irq.AssignedIRQ);
666 if (link->io.NumPorts1)
667 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
668 link->io.BasePort1+link->io.NumPorts1-1);
669 printk("\n");
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672cs_failed:
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200673 cs_error(link, last_fn, last_ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 mgslpc_release((u_long)link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200675 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678/* Card has been removed.
679 * Unregister device and release PCMCIA configuration.
680 * If device is open, postpone until it is closed.
681 */
682static void mgslpc_release(u_long arg)
683{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100684 struct pcmcia_device *link = (struct pcmcia_device *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100686 if (debug_level >= DEBUG_LEVEL_INFO)
687 printk("mgslpc_release(0x%p)\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100689 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200692static void mgslpc_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100694 if (debug_level >= DEBUG_LEVEL_INFO)
695 printk("mgslpc_detach(0x%p)\n", link);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100696
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100697 ((MGSLPC_INFO *)link->priv)->stop = 1;
698 mgslpc_release((u_long)link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100700 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200703static int mgslpc_suspend(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100704{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100705 MGSLPC_INFO *info = link->priv;
706
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100707 info->stop = 1;
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100708
709 return 0;
710}
711
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200712static int mgslpc_resume(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100713{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100714 MGSLPC_INFO *info = link->priv;
715
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100716 info->stop = 0;
717
718 return 0;
719}
720
721
Joe Perches0fab6de2008-04-28 02:14:02 -0700722static inline bool mgslpc_paranoia_check(MGSLPC_INFO *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 char *name, const char *routine)
724{
725#ifdef MGSLPC_PARANOIA_CHECK
726 static const char *badmagic =
727 "Warning: bad magic number for mgsl struct (%s) in %s\n";
728 static const char *badinfo =
729 "Warning: null mgslpc_info for (%s) in %s\n";
730
731 if (!info) {
732 printk(badinfo, name, routine);
Joe Perches0fab6de2008-04-28 02:14:02 -0700733 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 if (info->magic != MGSLPC_MAGIC) {
736 printk(badmagic, name, routine);
Joe Perches0fab6de2008-04-28 02:14:02 -0700737 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739#else
740 if (!info)
Joe Perches0fab6de2008-04-28 02:14:02 -0700741 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742#endif
Joe Perches0fab6de2008-04-28 02:14:02 -0700743 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746
747#define CMD_RXFIFO BIT7 // release current rx FIFO
748#define CMD_RXRESET BIT6 // receiver reset
749#define CMD_RXFIFO_READ BIT5
750#define CMD_START_TIMER BIT4
751#define CMD_TXFIFO BIT3 // release current tx FIFO
752#define CMD_TXEOM BIT1 // transmit end message
753#define CMD_TXRESET BIT0 // transmit reset
754
Joe Perches0fab6de2008-04-28 02:14:02 -0700755static bool wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 int i = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400758 /* wait for command completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
760 udelay(1);
761 if (i++ == 1000)
Joe Perches0fab6de2008-04-28 02:14:02 -0700762 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Joe Perches0fab6de2008-04-28 02:14:02 -0700764 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Jeff Garzikd12341f2007-10-19 15:45:35 -0400767static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 wait_command_complete(info, channel);
770 write_reg(info, (unsigned char) (channel + CMDR), cmd);
771}
772
773static void tx_pause(struct tty_struct *tty)
774{
775 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
776 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
779 return;
780 if (debug_level >= DEBUG_LEVEL_INFO)
Jeff Garzikd12341f2007-10-19 15:45:35 -0400781 printk("tx_pause(%s)\n",info->device_name);
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 spin_lock_irqsave(&info->lock,flags);
784 if (info->tx_enabled)
785 tx_stop(info);
786 spin_unlock_irqrestore(&info->lock,flags);
787}
788
789static void tx_release(struct tty_struct *tty)
790{
791 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
792 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
795 return;
796 if (debug_level >= DEBUG_LEVEL_INFO)
Jeff Garzikd12341f2007-10-19 15:45:35 -0400797 printk("tx_release(%s)\n",info->device_name);
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 spin_lock_irqsave(&info->lock,flags);
800 if (!info->tx_enabled)
801 tx_start(info);
802 spin_unlock_irqrestore(&info->lock,flags);
803}
804
805/* Return next bottom half action to perform.
806 * or 0 if nothing to do.
807 */
808static int bh_action(MGSLPC_INFO *info)
809{
810 unsigned long flags;
811 int rc = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 spin_lock_irqsave(&info->lock,flags);
814
815 if (info->pending_bh & BH_RECEIVE) {
816 info->pending_bh &= ~BH_RECEIVE;
817 rc = BH_RECEIVE;
818 } else if (info->pending_bh & BH_TRANSMIT) {
819 info->pending_bh &= ~BH_TRANSMIT;
820 rc = BH_TRANSMIT;
821 } else if (info->pending_bh & BH_STATUS) {
822 info->pending_bh &= ~BH_STATUS;
823 rc = BH_STATUS;
824 }
825
826 if (!rc) {
827 /* Mark BH routine as complete */
Joe Perches0fab6de2008-04-28 02:14:02 -0700828 info->bh_running = false;
829 info->bh_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Jeff Garzikd12341f2007-10-19 15:45:35 -0400831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return rc;
835}
836
David Howellsc4028952006-11-22 14:57:56 +0000837static void bh_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
David Howellsc4028952006-11-22 14:57:56 +0000839 MGSLPC_INFO *info = container_of(work, MGSLPC_INFO, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 int action;
841
842 if (!info)
843 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (debug_level >= DEBUG_LEVEL_BH)
846 printk( "%s(%d):bh_handler(%s) entry\n",
847 __FILE__,__LINE__,info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400848
Joe Perches0fab6de2008-04-28 02:14:02 -0700849 info->bh_running = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 while((action = bh_action(info)) != 0) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /* Process work item */
854 if ( debug_level >= DEBUG_LEVEL_BH )
855 printk( "%s(%d):bh_handler() work item action=%d\n",
856 __FILE__,__LINE__,action);
857
858 switch (action) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 case BH_RECEIVE:
861 while(rx_get_frame(info));
862 break;
863 case BH_TRANSMIT:
864 bh_transmit(info);
865 break;
866 case BH_STATUS:
867 bh_status(info);
868 break;
869 default:
870 /* unknown work item ID */
871 printk("Unknown work item ID=%08X!\n", action);
872 break;
873 }
874 }
875
876 if (debug_level >= DEBUG_LEVEL_BH)
877 printk( "%s(%d):bh_handler(%s) exit\n",
878 __FILE__,__LINE__,info->device_name);
879}
880
Peter Hagervallcdaad342006-06-23 02:06:04 -0700881static void bh_transmit(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 struct tty_struct *tty = info->tty;
884 if (debug_level >= DEBUG_LEVEL_BH)
885 printk("bh_transmit() entry on %s\n", info->device_name);
886
Jiri Slabyb963a842007-02-10 01:44:55 -0800887 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Peter Hagervallcdaad342006-06-23 02:06:04 -0700891static void bh_status(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 info->ri_chkcount = 0;
894 info->dsr_chkcount = 0;
895 info->dcd_chkcount = 0;
896 info->cts_chkcount = 0;
897}
898
Jeff Garzikd12341f2007-10-19 15:45:35 -0400899/* eom: non-zero = end of frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
901{
902 unsigned char data[2];
903 unsigned char fifo_count, read_count, i;
904 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
905
906 if (debug_level >= DEBUG_LEVEL_ISR)
907 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (!info->rx_enabled)
910 return;
911
912 if (info->rx_frame_count >= info->rx_buf_count) {
913 /* no more free buffers */
914 issue_command(info, CHA, CMD_RXRESET);
915 info->pending_bh |= BH_RECEIVE;
Joe Perches0fab6de2008-04-28 02:14:02 -0700916 info->rx_overflow = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 info->icount.buf_overrun++;
918 return;
919 }
920
921 if (eom) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400922 /* end of frame, get FIFO count from RBCL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
924 fifo_count = 32;
925 } else
926 fifo_count = 32;
Jeff Garzikd12341f2007-10-19 15:45:35 -0400927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 do {
929 if (fifo_count == 1) {
930 read_count = 1;
931 data[0] = read_reg(info, CHA + RXFIFO);
932 } else {
933 read_count = 2;
934 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
935 }
936 fifo_count -= read_count;
937 if (!fifo_count && eom)
938 buf->status = data[--read_count];
939
940 for (i = 0; i < read_count; i++) {
941 if (buf->count >= info->max_frame_size) {
942 /* frame too large, reset receiver and reset current buffer */
943 issue_command(info, CHA, CMD_RXRESET);
944 buf->count = 0;
945 return;
946 }
947 *(buf->data + buf->count) = data[i];
948 buf->count++;
949 }
950 } while (fifo_count);
951
952 if (eom) {
953 info->pending_bh |= BH_RECEIVE;
954 info->rx_frame_count++;
955 info->rx_put++;
956 if (info->rx_put >= info->rx_buf_count)
957 info->rx_put = 0;
958 }
959 issue_command(info, CHA, CMD_RXFIFO);
960}
961
962static void rx_ready_async(MGSLPC_INFO *info, int tcd)
963{
Alan Cox33f0f882006-01-09 20:54:13 -0800964 unsigned char data, status, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 int fifo_count;
Alan Cox33f0f882006-01-09 20:54:13 -0800966 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 struct tty_struct *tty = info->tty;
968 struct mgsl_icount *icount = &info->icount;
969
970 if (tcd) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400971 /* early termination, get FIFO count from RBCL register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
973
974 /* Zero fifo count could mean 0 or 32 bytes available.
975 * If BIT5 of STAR is set then at least 1 byte is available.
976 */
977 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
978 fifo_count = 32;
979 } else
980 fifo_count = 32;
Alan Cox33f0f882006-01-09 20:54:13 -0800981
982 tty_buffer_request_room(tty, fifo_count);
Jeff Garzikd12341f2007-10-19 15:45:35 -0400983 /* Flush received async data to receive data buffer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 while (fifo_count) {
985 data = read_reg(info, CHA + RXFIFO);
986 status = read_reg(info, CHA + RXFIFO);
987 fifo_count -= 2;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 icount->rx++;
Alan Cox33f0f882006-01-09 20:54:13 -0800990 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 // if no frameing/crc error then save data
993 // BIT7:parity error
994 // BIT6:framing error
995
996 if (status & (BIT7 + BIT6)) {
Jeff Garzikd12341f2007-10-19 15:45:35 -0400997 if (status & BIT7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 icount->parity++;
999 else
1000 icount->frame++;
1001
1002 /* discard char if tty control flags say so */
1003 if (status & info->ignore_status_mask)
1004 continue;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 status &= info->read_status_mask;
1007
1008 if (status & BIT7)
Alan Cox33f0f882006-01-09 20:54:13 -08001009 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 else if (status & BIT6)
Alan Cox33f0f882006-01-09 20:54:13 -08001011 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Alan Cox33f0f882006-01-09 20:54:13 -08001013 work += tty_insert_flip_char(tty, data, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 issue_command(info, CHA, CMD_RXFIFO);
1016
1017 if (debug_level >= DEBUG_LEVEL_ISR) {
Alan Cox33f0f882006-01-09 20:54:13 -08001018 printk("%s(%d):rx_ready_async",
1019 __FILE__,__LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1021 __FILE__,__LINE__,icount->rx,icount->brk,
1022 icount->parity,icount->frame,icount->overrun);
1023 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001024
Alan Cox33f0f882006-01-09 20:54:13 -08001025 if (work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 tty_flip_buffer_push(tty);
1027}
1028
1029
1030static void tx_done(MGSLPC_INFO *info)
1031{
1032 if (!info->tx_active)
1033 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001034
Joe Perches0fab6de2008-04-28 02:14:02 -07001035 info->tx_active = false;
1036 info->tx_aborting = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 if (info->params.mode == MGSL_MODE_ASYNC)
1039 return;
1040
1041 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001042 del_timer(&info->tx_timer);
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (info->drop_rts_on_tx_done) {
1045 get_signals(info);
1046 if (info->serial_signals & SerialSignal_RTS) {
1047 info->serial_signals &= ~SerialSignal_RTS;
1048 set_signals(info);
1049 }
Joe Perches0fab6de2008-04-28 02:14:02 -07001050 info->drop_rts_on_tx_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08001053#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (info->netcount)
1055 hdlcdev_tx_done(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001056 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057#endif
1058 {
1059 if (info->tty->stopped || info->tty->hw_stopped) {
1060 tx_stop(info);
1061 return;
1062 }
1063 info->pending_bh |= BH_TRANSMIT;
1064 }
1065}
1066
1067static void tx_ready(MGSLPC_INFO *info)
1068{
1069 unsigned char fifo_count = 32;
1070 int c;
1071
1072 if (debug_level >= DEBUG_LEVEL_ISR)
1073 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1074
1075 if (info->params.mode == MGSL_MODE_HDLC) {
1076 if (!info->tx_active)
1077 return;
1078 } else {
1079 if (info->tty->stopped || info->tty->hw_stopped) {
1080 tx_stop(info);
1081 return;
1082 }
1083 if (!info->tx_count)
Joe Perches0fab6de2008-04-28 02:14:02 -07001084 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086
1087 if (!info->tx_count)
1088 return;
1089
1090 while (info->tx_count && fifo_count) {
1091 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
Jeff Garzikd12341f2007-10-19 15:45:35 -04001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (c == 1) {
1094 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1095 } else {
1096 write_reg16(info, CHA + TXFIFO,
1097 *((unsigned short*)(info->tx_buf + info->tx_get)));
1098 }
1099 info->tx_count -= c;
1100 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1101 fifo_count -= c;
1102 }
1103
1104 if (info->params.mode == MGSL_MODE_ASYNC) {
1105 if (info->tx_count < WAKEUP_CHARS)
1106 info->pending_bh |= BH_TRANSMIT;
1107 issue_command(info, CHA, CMD_TXFIFO);
1108 } else {
1109 if (info->tx_count)
1110 issue_command(info, CHA, CMD_TXFIFO);
1111 else
1112 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1113 }
1114}
1115
1116static void cts_change(MGSLPC_INFO *info)
1117{
1118 get_signals(info);
1119 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1120 irq_disable(info, CHB, IRQ_CTS);
1121 info->icount.cts++;
1122 if (info->serial_signals & SerialSignal_CTS)
1123 info->input_signal_events.cts_up++;
1124 else
1125 info->input_signal_events.cts_down++;
1126 wake_up_interruptible(&info->status_event_wait_q);
1127 wake_up_interruptible(&info->event_wait_q);
1128
1129 if (info->flags & ASYNC_CTS_FLOW) {
1130 if (info->tty->hw_stopped) {
1131 if (info->serial_signals & SerialSignal_CTS) {
1132 if (debug_level >= DEBUG_LEVEL_ISR)
1133 printk("CTS tx start...");
1134 if (info->tty)
1135 info->tty->hw_stopped = 0;
1136 tx_start(info);
1137 info->pending_bh |= BH_TRANSMIT;
1138 return;
1139 }
1140 } else {
1141 if (!(info->serial_signals & SerialSignal_CTS)) {
1142 if (debug_level >= DEBUG_LEVEL_ISR)
1143 printk("CTS tx stop...");
1144 if (info->tty)
1145 info->tty->hw_stopped = 1;
1146 tx_stop(info);
1147 }
1148 }
1149 }
1150 info->pending_bh |= BH_STATUS;
1151}
1152
1153static void dcd_change(MGSLPC_INFO *info)
1154{
1155 get_signals(info);
1156 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1157 irq_disable(info, CHB, IRQ_DCD);
1158 info->icount.dcd++;
1159 if (info->serial_signals & SerialSignal_DCD) {
1160 info->input_signal_events.dcd_up++;
1161 }
1162 else
1163 info->input_signal_events.dcd_down++;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08001164#if SYNCLINK_GENERIC_HDLC
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07001165 if (info->netcount) {
1166 if (info->serial_signals & SerialSignal_DCD)
1167 netif_carrier_on(info->netdev);
1168 else
1169 netif_carrier_off(info->netdev);
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171#endif
1172 wake_up_interruptible(&info->status_event_wait_q);
1173 wake_up_interruptible(&info->event_wait_q);
1174
1175 if (info->flags & ASYNC_CHECK_CD) {
1176 if (debug_level >= DEBUG_LEVEL_ISR)
1177 printk("%s CD now %s...", info->device_name,
1178 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1179 if (info->serial_signals & SerialSignal_DCD)
1180 wake_up_interruptible(&info->open_wait);
1181 else {
1182 if (debug_level >= DEBUG_LEVEL_ISR)
1183 printk("doing serial hangup...");
1184 if (info->tty)
1185 tty_hangup(info->tty);
1186 }
1187 }
1188 info->pending_bh |= BH_STATUS;
1189}
1190
1191static void dsr_change(MGSLPC_INFO *info)
1192{
1193 get_signals(info);
1194 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1195 port_irq_disable(info, PVR_DSR);
1196 info->icount.dsr++;
1197 if (info->serial_signals & SerialSignal_DSR)
1198 info->input_signal_events.dsr_up++;
1199 else
1200 info->input_signal_events.dsr_down++;
1201 wake_up_interruptible(&info->status_event_wait_q);
1202 wake_up_interruptible(&info->event_wait_q);
1203 info->pending_bh |= BH_STATUS;
1204}
1205
1206static void ri_change(MGSLPC_INFO *info)
1207{
1208 get_signals(info);
1209 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1210 port_irq_disable(info, PVR_RI);
1211 info->icount.rng++;
1212 if (info->serial_signals & SerialSignal_RI)
1213 info->input_signal_events.ri_up++;
1214 else
1215 info->input_signal_events.ri_down++;
1216 wake_up_interruptible(&info->status_event_wait_q);
1217 wake_up_interruptible(&info->event_wait_q);
1218 info->pending_bh |= BH_STATUS;
1219}
1220
1221/* Interrupt service routine entry point.
Jeff Garzikd12341f2007-10-19 15:45:35 -04001222 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001224 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 * irq interrupt number that caused interrupt
1226 * dev_id device ID supplied during interrupt registration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 */
Jeff Garzika6f97b22007-10-31 05:20:49 -04001228static irqreturn_t mgslpc_isr(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Jeff Garzika6f97b22007-10-31 05:20:49 -04001230 MGSLPC_INFO *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 unsigned short isr;
1232 unsigned char gis, pis;
1233 int count=0;
1234
Jeff Garzikd12341f2007-10-19 15:45:35 -04001235 if (debug_level >= DEBUG_LEVEL_ISR)
Jeff Garzika6f97b22007-10-31 05:20:49 -04001236 printk("mgslpc_isr(%d) entry.\n", info->irq_level);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001237
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001238 if (!(info->p_dev->_locked))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return IRQ_HANDLED;
1240
1241 spin_lock(&info->lock);
1242
1243 while ((gis = read_reg(info, CHA + GIS))) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001244 if (debug_level >= DEBUG_LEVEL_ISR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1246
1247 if ((gis & 0x70) || count > 1000) {
1248 printk("synclink_cs:hardware failed or ejected\n");
1249 break;
1250 }
1251 count++;
1252
1253 if (gis & (BIT1 + BIT0)) {
1254 isr = read_reg16(info, CHB + ISR);
1255 if (isr & IRQ_DCD)
1256 dcd_change(info);
1257 if (isr & IRQ_CTS)
1258 cts_change(info);
1259 }
1260 if (gis & (BIT3 + BIT2))
1261 {
1262 isr = read_reg16(info, CHA + ISR);
1263 if (isr & IRQ_TIMER) {
Joe Perches0fab6de2008-04-28 02:14:02 -07001264 info->irq_occurred = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 irq_disable(info, CHA, IRQ_TIMER);
1266 }
1267
Jeff Garzikd12341f2007-10-19 15:45:35 -04001268 /* receive IRQs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (isr & IRQ_EXITHUNT) {
1270 info->icount.exithunt++;
1271 wake_up_interruptible(&info->event_wait_q);
1272 }
1273 if (isr & IRQ_BREAK_ON) {
1274 info->icount.brk++;
1275 if (info->flags & ASYNC_SAK)
1276 do_SAK(info->tty);
1277 }
1278 if (isr & IRQ_RXTIME) {
1279 issue_command(info, CHA, CMD_RXFIFO_READ);
1280 }
1281 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1282 if (info->params.mode == MGSL_MODE_HDLC)
Jeff Garzikd12341f2007-10-19 15:45:35 -04001283 rx_ready_hdlc(info, isr & IRQ_RXEOM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 else
1285 rx_ready_async(info, isr & IRQ_RXEOM);
1286 }
1287
Jeff Garzikd12341f2007-10-19 15:45:35 -04001288 /* transmit IRQs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (isr & IRQ_UNDERRUN) {
1290 if (info->tx_aborting)
1291 info->icount.txabort++;
1292 else
1293 info->icount.txunder++;
1294 tx_done(info);
1295 }
1296 else if (isr & IRQ_ALLSENT) {
1297 info->icount.txok++;
1298 tx_done(info);
1299 }
1300 else if (isr & IRQ_TXFIFO)
1301 tx_ready(info);
1302 }
1303 if (gis & BIT7) {
1304 pis = read_reg(info, CHA + PIS);
1305 if (pis & BIT1)
1306 dsr_change(info);
1307 if (pis & BIT2)
1308 ri_change(info);
1309 }
1310 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001311
1312 /* Request bottom half processing if there's something
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * for it to do and the bh is not already running
1314 */
1315
1316 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001317 if ( debug_level >= DEBUG_LEVEL_ISR )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 printk("%s(%d):%s queueing bh task.\n",
1319 __FILE__,__LINE__,info->device_name);
1320 schedule_work(&info->task);
Joe Perches0fab6de2008-04-28 02:14:02 -07001321 info->bh_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
1324 spin_unlock(&info->lock);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001325
1326 if (debug_level >= DEBUG_LEVEL_ISR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 printk("%s(%d):mgslpc_isr(%d)exit.\n",
Jeff Garzika6f97b22007-10-31 05:20:49 -04001328 __FILE__, __LINE__, info->irq_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 return IRQ_HANDLED;
1331}
1332
1333/* Initialize and start device.
1334 */
1335static int startup(MGSLPC_INFO * info)
1336{
1337 int retval = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (debug_level >= DEBUG_LEVEL_INFO)
1340 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (info->flags & ASYNC_INITIALIZED)
1343 return 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (!info->tx_buf) {
1346 /* allocate a page of memory for a transmit buffer */
1347 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1348 if (!info->tx_buf) {
1349 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1350 __FILE__,__LINE__,info->device_name);
1351 return -ENOMEM;
1352 }
1353 }
1354
1355 info->pending_bh = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001356
Paul Fulghuma7482a22005-09-10 00:26:07 -07001357 memset(&info->icount, 0, sizeof(info->icount));
1358
Jiri Slaby40565f12007-02-12 00:52:31 -08001359 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 /* Allocate and claim adapter resources */
1362 retval = claim_resources(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /* perform existance check and diagnostics */
1365 if ( !retval )
1366 retval = adapter_test(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if ( retval ) {
1369 if (capable(CAP_SYS_ADMIN) && info->tty)
1370 set_bit(TTY_IO_ERROR, &info->tty->flags);
1371 release_resources(info);
1372 return retval;
1373 }
1374
1375 /* program hardware for current parameters */
1376 mgslpc_change_params(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (info->tty)
1379 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1380
1381 info->flags |= ASYNC_INITIALIZED;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return 0;
1384}
1385
1386/* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1387 */
1388static void shutdown(MGSLPC_INFO * info)
1389{
1390 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (!(info->flags & ASYNC_INITIALIZED))
1393 return;
1394
1395 if (debug_level >= DEBUG_LEVEL_INFO)
1396 printk("%s(%d):mgslpc_shutdown(%s)\n",
1397 __FILE__,__LINE__, info->device_name );
1398
1399 /* clear status wait queue because status changes */
1400 /* can't happen after shutting down the hardware */
1401 wake_up_interruptible(&info->status_event_wait_q);
1402 wake_up_interruptible(&info->event_wait_q);
1403
Jiri Slaby40565f12007-02-12 00:52:31 -08001404 del_timer_sync(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 if (info->tx_buf) {
1407 free_page((unsigned long) info->tx_buf);
1408 info->tx_buf = NULL;
1409 }
1410
1411 spin_lock_irqsave(&info->lock,flags);
1412
1413 rx_stop(info);
1414 tx_stop(info);
1415
1416 /* TODO:disable interrupts instead of reset to preserve signal states */
1417 reset_device(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
1420 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1421 set_signals(info);
1422 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 spin_unlock_irqrestore(&info->lock,flags);
1425
Jeff Garzikd12341f2007-10-19 15:45:35 -04001426 release_resources(info);
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (info->tty)
1429 set_bit(TTY_IO_ERROR, &info->tty->flags);
1430
1431 info->flags &= ~ASYNC_INITIALIZED;
1432}
1433
1434static void mgslpc_program_hw(MGSLPC_INFO *info)
1435{
1436 unsigned long flags;
1437
1438 spin_lock_irqsave(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 rx_stop(info);
1441 tx_stop(info);
1442 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1445 hdlc_mode(info);
1446 else
1447 async_mode(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 set_signals(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 info->dcd_chkcount = 0;
1452 info->cts_chkcount = 0;
1453 info->ri_chkcount = 0;
1454 info->dsr_chkcount = 0;
1455
1456 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1457 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1458 get_signals(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (info->netcount || info->tty->termios->c_cflag & CREAD)
1461 rx_start(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 spin_unlock_irqrestore(&info->lock,flags);
1464}
1465
1466/* Reconfigure adapter based on new parameters
1467 */
1468static void mgslpc_change_params(MGSLPC_INFO *info)
1469{
1470 unsigned cflag;
1471 int bits_per_char;
1472
1473 if (!info->tty || !info->tty->termios)
1474 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (debug_level >= DEBUG_LEVEL_INFO)
1477 printk("%s(%d):mgslpc_change_params(%s)\n",
1478 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 cflag = info->tty->termios->c_cflag;
1481
1482 /* if B0 rate (hangup) specified then negate DTR and RTS */
1483 /* otherwise assert DTR and RTS */
1484 if (cflag & CBAUD)
1485 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1486 else
1487 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 /* byte size and parity */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 switch (cflag & CSIZE) {
1492 case CS5: info->params.data_bits = 5; break;
1493 case CS6: info->params.data_bits = 6; break;
1494 case CS7: info->params.data_bits = 7; break;
1495 case CS8: info->params.data_bits = 8; break;
1496 default: info->params.data_bits = 7; break;
1497 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (cflag & CSTOPB)
1500 info->params.stop_bits = 2;
1501 else
1502 info->params.stop_bits = 1;
1503
1504 info->params.parity = ASYNC_PARITY_NONE;
1505 if (cflag & PARENB) {
1506 if (cflag & PARODD)
1507 info->params.parity = ASYNC_PARITY_ODD;
1508 else
1509 info->params.parity = ASYNC_PARITY_EVEN;
1510#ifdef CMSPAR
1511 if (cflag & CMSPAR)
1512 info->params.parity = ASYNC_PARITY_SPACE;
1513#endif
1514 }
1515
1516 /* calculate number of jiffies to transmit a full
1517 * FIFO (32 bytes) at specified data rate
1518 */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001519 bits_per_char = info->params.data_bits +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 info->params.stop_bits + 1;
1521
1522 /* if port data rate is set to 460800 or less then
1523 * allow tty settings to override, otherwise keep the
1524 * current data rate.
1525 */
1526 if (info->params.data_rate <= 460800) {
1527 info->params.data_rate = tty_get_baud_rate(info->tty);
1528 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if ( info->params.data_rate ) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04001531 info->timeout = (32*HZ*bits_per_char) /
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 info->params.data_rate;
1533 }
1534 info->timeout += HZ/50; /* Add .02 seconds of slop */
1535
1536 if (cflag & CRTSCTS)
1537 info->flags |= ASYNC_CTS_FLOW;
1538 else
1539 info->flags &= ~ASYNC_CTS_FLOW;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (cflag & CLOCAL)
1542 info->flags &= ~ASYNC_CHECK_CD;
1543 else
1544 info->flags |= ASYNC_CHECK_CD;
1545
1546 /* process tty input control flags */
Jeff Garzikd12341f2007-10-19 15:45:35 -04001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 info->read_status_mask = 0;
1549 if (I_INPCK(info->tty))
1550 info->read_status_mask |= BIT7 | BIT6;
1551 if (I_IGNPAR(info->tty))
1552 info->ignore_status_mask |= BIT7 | BIT6;
1553
1554 mgslpc_program_hw(info);
1555}
1556
1557/* Add a character to the transmit buffer
1558 */
1559static void mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1560{
1561 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1562 unsigned long flags;
1563
1564 if (debug_level >= DEBUG_LEVEL_INFO) {
1565 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1566 __FILE__,__LINE__,ch,info->device_name);
1567 }
1568
1569 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1570 return;
1571
Eric Sesterhenn326f28e92006-06-25 05:48:48 -07001572 if (!info->tx_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return;
1574
1575 spin_lock_irqsave(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1578 if (info->tx_count < TXBUFSIZE - 1) {
1579 info->tx_buf[info->tx_put++] = ch;
1580 info->tx_put &= TXBUFSIZE-1;
1581 info->tx_count++;
1582 }
1583 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 spin_unlock_irqrestore(&info->lock,flags);
1586}
1587
1588/* Enable transmitter so remaining characters in the
1589 * transmit buffer are sent.
1590 */
1591static void mgslpc_flush_chars(struct tty_struct *tty)
1592{
1593 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1594 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (debug_level >= DEBUG_LEVEL_INFO)
1597 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1598 __FILE__,__LINE__,info->device_name,info->tx_count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1601 return;
1602
1603 if (info->tx_count <= 0 || tty->stopped ||
1604 tty->hw_stopped || !info->tx_buf)
1605 return;
1606
1607 if (debug_level >= DEBUG_LEVEL_INFO)
1608 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1609 __FILE__,__LINE__,info->device_name);
1610
1611 spin_lock_irqsave(&info->lock,flags);
1612 if (!info->tx_active)
1613 tx_start(info);
1614 spin_unlock_irqrestore(&info->lock,flags);
1615}
1616
1617/* Send a block of data
Jeff Garzikd12341f2007-10-19 15:45:35 -04001618 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001620 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 * tty pointer to tty information structure
1622 * buf pointer to buffer containing send data
1623 * count size of send data in bytes
Jeff Garzikd12341f2007-10-19 15:45:35 -04001624 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * Returns: number of characters written
1626 */
1627static int mgslpc_write(struct tty_struct * tty,
1628 const unsigned char *buf, int count)
1629{
1630 int c, ret = 0;
1631 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1632 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (debug_level >= DEBUG_LEVEL_INFO)
1635 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1636 __FILE__,__LINE__,info->device_name,count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
Eric Sesterhenn326f28e92006-06-25 05:48:48 -07001639 !info->tx_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 goto cleanup;
1641
1642 if (info->params.mode == MGSL_MODE_HDLC) {
1643 if (count > TXBUFSIZE) {
1644 ret = -EIO;
1645 goto cleanup;
1646 }
1647 if (info->tx_active)
1648 goto cleanup;
1649 else if (info->tx_count)
1650 goto start;
1651 }
1652
1653 for (;;) {
1654 c = min(count,
1655 min(TXBUFSIZE - info->tx_count - 1,
1656 TXBUFSIZE - info->tx_put));
1657 if (c <= 0)
1658 break;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 memcpy(info->tx_buf + info->tx_put, buf, c);
1661
1662 spin_lock_irqsave(&info->lock,flags);
1663 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1664 info->tx_count += c;
1665 spin_unlock_irqrestore(&info->lock,flags);
1666
1667 buf += c;
1668 count -= c;
1669 ret += c;
1670 }
1671start:
1672 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1673 spin_lock_irqsave(&info->lock,flags);
1674 if (!info->tx_active)
1675 tx_start(info);
1676 spin_unlock_irqrestore(&info->lock,flags);
1677 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001678cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (debug_level >= DEBUG_LEVEL_INFO)
1680 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1681 __FILE__,__LINE__,info->device_name,ret);
1682 return ret;
1683}
1684
1685/* Return the count of free bytes in transmit buffer
1686 */
1687static int mgslpc_write_room(struct tty_struct *tty)
1688{
1689 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1690 int ret;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1693 return 0;
1694
1695 if (info->params.mode == MGSL_MODE_HDLC) {
1696 /* HDLC (frame oriented) mode */
1697 if (info->tx_active)
1698 return 0;
1699 else
1700 return HDLC_MAX_FRAME_SIZE;
1701 } else {
1702 ret = TXBUFSIZE - info->tx_count - 1;
1703 if (ret < 0)
1704 ret = 0;
1705 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (debug_level >= DEBUG_LEVEL_INFO)
1708 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1709 __FILE__,__LINE__, info->device_name, ret);
1710 return ret;
1711}
1712
1713/* Return the count of bytes in transmit buffer
1714 */
1715static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1716{
1717 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1718 int rc;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (debug_level >= DEBUG_LEVEL_INFO)
1721 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1722 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1725 return 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (info->params.mode == MGSL_MODE_HDLC)
1728 rc = info->tx_active ? info->max_frame_size : 0;
1729 else
1730 rc = info->tx_count;
1731
1732 if (debug_level >= DEBUG_LEVEL_INFO)
1733 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1734 __FILE__,__LINE__, info->device_name, rc);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return rc;
1737}
1738
1739/* Discard all data in the send buffer
1740 */
1741static void mgslpc_flush_buffer(struct tty_struct *tty)
1742{
1743 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1744 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (debug_level >= DEBUG_LEVEL_INFO)
1747 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1748 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1751 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001752
1753 spin_lock_irqsave(&info->lock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 info->tx_count = info->tx_put = info->tx_get = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001755 del_timer(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 spin_unlock_irqrestore(&info->lock,flags);
1757
1758 wake_up_interruptible(&tty->write_wait);
1759 tty_wakeup(tty);
1760}
1761
1762/* Send a high-priority XON/XOFF character
1763 */
1764static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1765{
1766 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1767 unsigned long flags;
1768
1769 if (debug_level >= DEBUG_LEVEL_INFO)
1770 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1771 __FILE__,__LINE__, info->device_name, ch );
Jeff Garzikd12341f2007-10-19 15:45:35 -04001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1774 return;
1775
1776 info->x_char = ch;
1777 if (ch) {
1778 spin_lock_irqsave(&info->lock,flags);
1779 if (!info->tx_enabled)
1780 tx_start(info);
1781 spin_unlock_irqrestore(&info->lock,flags);
1782 }
1783}
1784
1785/* Signal remote device to throttle send data (our receive data)
1786 */
1787static void mgslpc_throttle(struct tty_struct * tty)
1788{
1789 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1790 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (debug_level >= DEBUG_LEVEL_INFO)
1793 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1794 __FILE__,__LINE__, info->device_name );
1795
1796 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1797 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (I_IXOFF(tty))
1800 mgslpc_send_xchar(tty, STOP_CHAR(tty));
Jeff Garzikd12341f2007-10-19 15:45:35 -04001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (tty->termios->c_cflag & CRTSCTS) {
1803 spin_lock_irqsave(&info->lock,flags);
1804 info->serial_signals &= ~SerialSignal_RTS;
1805 set_signals(info);
1806 spin_unlock_irqrestore(&info->lock,flags);
1807 }
1808}
1809
1810/* Signal remote device to stop throttling send data (our receive data)
1811 */
1812static void mgslpc_unthrottle(struct tty_struct * tty)
1813{
1814 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1815 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (debug_level >= DEBUG_LEVEL_INFO)
1818 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1819 __FILE__,__LINE__, info->device_name );
1820
1821 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1822 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (I_IXOFF(tty)) {
1825 if (info->x_char)
1826 info->x_char = 0;
1827 else
1828 mgslpc_send_xchar(tty, START_CHAR(tty));
1829 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (tty->termios->c_cflag & CRTSCTS) {
1832 spin_lock_irqsave(&info->lock,flags);
1833 info->serial_signals |= SerialSignal_RTS;
1834 set_signals(info);
1835 spin_unlock_irqrestore(&info->lock,flags);
1836 }
1837}
1838
1839/* get the current serial statistics
1840 */
1841static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1842{
1843 int err;
1844 if (debug_level >= DEBUG_LEVEL_INFO)
1845 printk("get_params(%s)\n", info->device_name);
Paul Fulghuma7482a22005-09-10 00:26:07 -07001846 if (!user_icount) {
1847 memset(&info->icount, 0, sizeof(info->icount));
1848 } else {
1849 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1850 if (err)
1851 return -EFAULT;
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return 0;
1854}
1855
1856/* get the current serial parameters
1857 */
1858static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1859{
1860 int err;
1861 if (debug_level >= DEBUG_LEVEL_INFO)
1862 printk("get_params(%s)\n", info->device_name);
1863 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1864 if (err)
1865 return -EFAULT;
1866 return 0;
1867}
1868
1869/* set the serial parameters
Jeff Garzikd12341f2007-10-19 15:45:35 -04001870 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04001872 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 * info pointer to device instance data
1874 * new_params user buffer containing new serial params
1875 *
1876 * Returns: 0 if success, otherwise error code
1877 */
1878static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params)
1879{
1880 unsigned long flags;
1881 MGSL_PARAMS tmp_params;
1882 int err;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (debug_level >= DEBUG_LEVEL_INFO)
1885 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1886 info->device_name );
1887 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1888 if (err) {
1889 if ( debug_level >= DEBUG_LEVEL_INFO )
1890 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1891 __FILE__,__LINE__,info->device_name);
1892 return -EFAULT;
1893 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 spin_lock_irqsave(&info->lock,flags);
1896 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1897 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 mgslpc_change_params(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902}
1903
1904static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1905{
1906 int err;
1907 if (debug_level >= DEBUG_LEVEL_INFO)
1908 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1909 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1910 if (err)
1911 return -EFAULT;
1912 return 0;
1913}
1914
1915static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1916{
1917 unsigned long flags;
1918 if (debug_level >= DEBUG_LEVEL_INFO)
1919 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1920 spin_lock_irqsave(&info->lock,flags);
1921 info->idle_mode = idle_mode;
1922 tx_set_idle(info);
1923 spin_unlock_irqrestore(&info->lock,flags);
1924 return 0;
1925}
1926
1927static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1928{
1929 int err;
1930 if (debug_level >= DEBUG_LEVEL_INFO)
1931 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1932 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1933 if (err)
1934 return -EFAULT;
1935 return 0;
1936}
1937
1938static int set_interface(MGSLPC_INFO * info, int if_mode)
1939{
1940 unsigned long flags;
1941 unsigned char val;
1942 if (debug_level >= DEBUG_LEVEL_INFO)
1943 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1944 spin_lock_irqsave(&info->lock,flags);
1945 info->if_mode = if_mode;
1946
1947 val = read_reg(info, PVR) & 0x0f;
1948 switch (info->if_mode)
1949 {
1950 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
1951 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
1952 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
1953 }
1954 write_reg(info, PVR, val);
1955
1956 spin_unlock_irqrestore(&info->lock,flags);
1957 return 0;
1958}
1959
1960static int set_txenable(MGSLPC_INFO * info, int enable)
1961{
1962 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (debug_level >= DEBUG_LEVEL_INFO)
1965 printk("set_txenable(%s,%d)\n", info->device_name, enable);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 spin_lock_irqsave(&info->lock,flags);
1968 if (enable) {
1969 if (!info->tx_enabled)
1970 tx_start(info);
1971 } else {
1972 if (info->tx_enabled)
1973 tx_stop(info);
1974 }
1975 spin_unlock_irqrestore(&info->lock,flags);
1976 return 0;
1977}
1978
1979static int tx_abort(MGSLPC_INFO * info)
1980{
1981 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (debug_level >= DEBUG_LEVEL_INFO)
1984 printk("tx_abort(%s)\n", info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 spin_lock_irqsave(&info->lock,flags);
1987 if (info->tx_active && info->tx_count &&
1988 info->params.mode == MGSL_MODE_HDLC) {
1989 /* clear data count so FIFO is not filled on next IRQ.
1990 * This results in underrun and abort transmission.
1991 */
1992 info->tx_count = info->tx_put = info->tx_get = 0;
Joe Perches0fab6de2008-04-28 02:14:02 -07001993 info->tx_aborting = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995 spin_unlock_irqrestore(&info->lock,flags);
1996 return 0;
1997}
1998
1999static int set_rxenable(MGSLPC_INFO * info, int enable)
2000{
2001 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (debug_level >= DEBUG_LEVEL_INFO)
2004 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 spin_lock_irqsave(&info->lock,flags);
2007 if (enable) {
2008 if (!info->rx_enabled)
2009 rx_start(info);
2010 } else {
2011 if (info->rx_enabled)
2012 rx_stop(info);
2013 }
2014 spin_unlock_irqrestore(&info->lock,flags);
2015 return 0;
2016}
2017
2018/* wait for specified event to occur
Jeff Garzikd12341f2007-10-19 15:45:35 -04002019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 * Arguments: info pointer to device instance data
2021 * mask pointer to bitmask of events to wait for
2022 * Return Value: 0 if successful and bit mask updated with
2023 * of events triggerred,
2024 * otherwise error code
2025 */
2026static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
2027{
2028 unsigned long flags;
2029 int s;
2030 int rc=0;
2031 struct mgsl_icount cprev, cnow;
2032 int events;
2033 int mask;
2034 struct _input_signal_events oldsigs, newsigs;
2035 DECLARE_WAITQUEUE(wait, current);
2036
2037 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2038 if (rc)
2039 return -EFAULT;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (debug_level >= DEBUG_LEVEL_INFO)
2042 printk("wait_events(%s,%d)\n", info->device_name, mask);
2043
2044 spin_lock_irqsave(&info->lock,flags);
2045
2046 /* return immediately if state matches requested events */
2047 get_signals(info);
2048 s = info->serial_signals;
2049 events = mask &
2050 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2051 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2052 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2053 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2054 if (events) {
2055 spin_unlock_irqrestore(&info->lock,flags);
2056 goto exit;
2057 }
2058
2059 /* save current irq counts */
2060 cprev = info->icount;
2061 oldsigs = info->input_signal_events;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if ((info->params.mode == MGSL_MODE_HDLC) &&
2064 (mask & MgslEvent_ExitHuntMode))
2065 irq_enable(info, CHA, IRQ_EXITHUNT);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 set_current_state(TASK_INTERRUPTIBLE);
2068 add_wait_queue(&info->event_wait_q, &wait);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002071
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 for(;;) {
2074 schedule();
2075 if (signal_pending(current)) {
2076 rc = -ERESTARTSYS;
2077 break;
2078 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* get current irq counts */
2081 spin_lock_irqsave(&info->lock,flags);
2082 cnow = info->icount;
2083 newsigs = info->input_signal_events;
2084 set_current_state(TASK_INTERRUPTIBLE);
2085 spin_unlock_irqrestore(&info->lock,flags);
2086
2087 /* if no change, wait aborted for some reason */
2088 if (newsigs.dsr_up == oldsigs.dsr_up &&
2089 newsigs.dsr_down == oldsigs.dsr_down &&
2090 newsigs.dcd_up == oldsigs.dcd_up &&
2091 newsigs.dcd_down == oldsigs.dcd_down &&
2092 newsigs.cts_up == oldsigs.cts_up &&
2093 newsigs.cts_down == oldsigs.cts_down &&
2094 newsigs.ri_up == oldsigs.ri_up &&
2095 newsigs.ri_down == oldsigs.ri_down &&
2096 cnow.exithunt == cprev.exithunt &&
2097 cnow.rxidle == cprev.rxidle) {
2098 rc = -EIO;
2099 break;
2100 }
2101
2102 events = mask &
2103 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2104 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2105 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2106 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2107 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2108 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2109 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2110 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2111 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2112 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2113 if (events)
2114 break;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 cprev = cnow;
2117 oldsigs = newsigs;
2118 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 remove_wait_queue(&info->event_wait_q, &wait);
2121 set_current_state(TASK_RUNNING);
2122
2123 if (mask & MgslEvent_ExitHuntMode) {
2124 spin_lock_irqsave(&info->lock,flags);
2125 if (!waitqueue_active(&info->event_wait_q))
2126 irq_disable(info, CHA, IRQ_EXITHUNT);
2127 spin_unlock_irqrestore(&info->lock,flags);
2128 }
2129exit:
2130 if (rc == 0)
2131 PUT_USER(rc, events, mask_ptr);
2132 return rc;
2133}
2134
2135static int modem_input_wait(MGSLPC_INFO *info,int arg)
2136{
2137 unsigned long flags;
2138 int rc;
2139 struct mgsl_icount cprev, cnow;
2140 DECLARE_WAITQUEUE(wait, current);
2141
2142 /* save current irq counts */
2143 spin_lock_irqsave(&info->lock,flags);
2144 cprev = info->icount;
2145 add_wait_queue(&info->status_event_wait_q, &wait);
2146 set_current_state(TASK_INTERRUPTIBLE);
2147 spin_unlock_irqrestore(&info->lock,flags);
2148
2149 for(;;) {
2150 schedule();
2151 if (signal_pending(current)) {
2152 rc = -ERESTARTSYS;
2153 break;
2154 }
2155
2156 /* get new irq counts */
2157 spin_lock_irqsave(&info->lock,flags);
2158 cnow = info->icount;
2159 set_current_state(TASK_INTERRUPTIBLE);
2160 spin_unlock_irqrestore(&info->lock,flags);
2161
2162 /* if no change, wait aborted for some reason */
2163 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2164 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2165 rc = -EIO;
2166 break;
2167 }
2168
2169 /* check for change in caller specified modem input */
2170 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2171 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2172 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2173 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2174 rc = 0;
2175 break;
2176 }
2177
2178 cprev = cnow;
2179 }
2180 remove_wait_queue(&info->status_event_wait_q, &wait);
2181 set_current_state(TASK_RUNNING);
2182 return rc;
2183}
2184
2185/* return the state of the serial control and status signals
2186 */
2187static int tiocmget(struct tty_struct *tty, struct file *file)
2188{
2189 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2190 unsigned int result;
2191 unsigned long flags;
2192
2193 spin_lock_irqsave(&info->lock,flags);
2194 get_signals(info);
2195 spin_unlock_irqrestore(&info->lock,flags);
2196
2197 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2198 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2199 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2200 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2201 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2202 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2203
2204 if (debug_level >= DEBUG_LEVEL_INFO)
2205 printk("%s(%d):%s tiocmget() value=%08X\n",
2206 __FILE__,__LINE__, info->device_name, result );
2207 return result;
2208}
2209
2210/* set modem control signals (DTR/RTS)
2211 */
2212static int tiocmset(struct tty_struct *tty, struct file *file,
2213 unsigned int set, unsigned int clear)
2214{
2215 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2216 unsigned long flags;
2217
2218 if (debug_level >= DEBUG_LEVEL_INFO)
2219 printk("%s(%d):%s tiocmset(%x,%x)\n",
2220 __FILE__,__LINE__,info->device_name, set, clear);
2221
2222 if (set & TIOCM_RTS)
2223 info->serial_signals |= SerialSignal_RTS;
2224 if (set & TIOCM_DTR)
2225 info->serial_signals |= SerialSignal_DTR;
2226 if (clear & TIOCM_RTS)
2227 info->serial_signals &= ~SerialSignal_RTS;
2228 if (clear & TIOCM_DTR)
2229 info->serial_signals &= ~SerialSignal_DTR;
2230
2231 spin_lock_irqsave(&info->lock,flags);
2232 set_signals(info);
2233 spin_unlock_irqrestore(&info->lock,flags);
2234
2235 return 0;
2236}
2237
2238/* Set or clear transmit break condition
2239 *
2240 * Arguments: tty pointer to tty instance data
2241 * break_state -1=set break condition, 0=clear
2242 */
2243static void mgslpc_break(struct tty_struct *tty, int break_state)
2244{
2245 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2246 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (debug_level >= DEBUG_LEVEL_INFO)
2249 printk("%s(%d):mgslpc_break(%s,%d)\n",
2250 __FILE__,__LINE__, info->device_name, break_state);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2253 return;
2254
2255 spin_lock_irqsave(&info->lock,flags);
2256 if (break_state == -1)
2257 set_reg_bits(info, CHA+DAFO, BIT6);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002258 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 clear_reg_bits(info, CHA+DAFO, BIT6);
2260 spin_unlock_irqrestore(&info->lock,flags);
2261}
2262
2263/* Service an IOCTL request
Jeff Garzikd12341f2007-10-19 15:45:35 -04002264 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04002266 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * tty pointer to tty instance data
2268 * file pointer to associated file object for device
2269 * cmd IOCTL command code
2270 * arg command argument/context
Jeff Garzikd12341f2007-10-19 15:45:35 -04002271 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 * Return Value: 0 if success, otherwise error code
2273 */
2274static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2275 unsigned int cmd, unsigned long arg)
2276{
2277 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (debug_level >= DEBUG_LEVEL_INFO)
2280 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2281 info->device_name, cmd );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2284 return -ENODEV;
2285
2286 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2287 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2288 if (tty->flags & (1 << TTY_IO_ERROR))
2289 return -EIO;
2290 }
2291
2292 return ioctl_common(info, cmd, arg);
2293}
2294
Peter Hagervallcdaad342006-06-23 02:06:04 -07002295static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
2297 int error;
2298 struct mgsl_icount cnow; /* kernel counter temps */
2299 struct serial_icounter_struct __user *p_cuser; /* user space */
2300 void __user *argp = (void __user *)arg;
2301 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 switch (cmd) {
2304 case MGSL_IOCGPARAMS:
2305 return get_params(info, argp);
2306 case MGSL_IOCSPARAMS:
2307 return set_params(info, argp);
2308 case MGSL_IOCGTXIDLE:
2309 return get_txidle(info, argp);
2310 case MGSL_IOCSTXIDLE:
2311 return set_txidle(info, (int)arg);
2312 case MGSL_IOCGIF:
2313 return get_interface(info, argp);
2314 case MGSL_IOCSIF:
2315 return set_interface(info,(int)arg);
2316 case MGSL_IOCTXENABLE:
2317 return set_txenable(info,(int)arg);
2318 case MGSL_IOCRXENABLE:
2319 return set_rxenable(info,(int)arg);
2320 case MGSL_IOCTXABORT:
2321 return tx_abort(info);
2322 case MGSL_IOCGSTATS:
2323 return get_stats(info, argp);
2324 case MGSL_IOCWAITEVENT:
2325 return wait_events(info, argp);
2326 case TIOCMIWAIT:
2327 return modem_input_wait(info,(int)arg);
2328 case TIOCGICOUNT:
2329 spin_lock_irqsave(&info->lock,flags);
2330 cnow = info->icount;
2331 spin_unlock_irqrestore(&info->lock,flags);
2332 p_cuser = argp;
2333 PUT_USER(error,cnow.cts, &p_cuser->cts);
2334 if (error) return error;
2335 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2336 if (error) return error;
2337 PUT_USER(error,cnow.rng, &p_cuser->rng);
2338 if (error) return error;
2339 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2340 if (error) return error;
2341 PUT_USER(error,cnow.rx, &p_cuser->rx);
2342 if (error) return error;
2343 PUT_USER(error,cnow.tx, &p_cuser->tx);
2344 if (error) return error;
2345 PUT_USER(error,cnow.frame, &p_cuser->frame);
2346 if (error) return error;
2347 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2348 if (error) return error;
2349 PUT_USER(error,cnow.parity, &p_cuser->parity);
2350 if (error) return error;
2351 PUT_USER(error,cnow.brk, &p_cuser->brk);
2352 if (error) return error;
2353 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2354 if (error) return error;
2355 return 0;
2356 default:
2357 return -ENOIOCTLCMD;
2358 }
2359 return 0;
2360}
2361
2362/* Set new termios settings
Jeff Garzikd12341f2007-10-19 15:45:35 -04002363 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 * Arguments:
Jeff Garzikd12341f2007-10-19 15:45:35 -04002365 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 * tty pointer to tty structure
2367 * termios pointer to buffer to hold returned old termios
2368 */
Alan Cox606d0992006-12-08 02:38:45 -08002369static void mgslpc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
2371 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2372 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (debug_level >= DEBUG_LEVEL_INFO)
2375 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2376 tty->driver->name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 /* just return if nothing has changed */
2379 if ((tty->termios->c_cflag == old_termios->c_cflag)
Jeff Garzikd12341f2007-10-19 15:45:35 -04002380 && (RELEVANT_IFLAG(tty->termios->c_iflag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 == RELEVANT_IFLAG(old_termios->c_iflag)))
2382 return;
2383
2384 mgslpc_change_params(info);
2385
2386 /* Handle transition to B0 status */
2387 if (old_termios->c_cflag & CBAUD &&
2388 !(tty->termios->c_cflag & CBAUD)) {
2389 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2390 spin_lock_irqsave(&info->lock,flags);
2391 set_signals(info);
2392 spin_unlock_irqrestore(&info->lock,flags);
2393 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 /* Handle transition away from B0 status */
2396 if (!(old_termios->c_cflag & CBAUD) &&
2397 tty->termios->c_cflag & CBAUD) {
2398 info->serial_signals |= SerialSignal_DTR;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002399 if (!(tty->termios->c_cflag & CRTSCTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 !test_bit(TTY_THROTTLED, &tty->flags)) {
2401 info->serial_signals |= SerialSignal_RTS;
2402 }
2403 spin_lock_irqsave(&info->lock,flags);
2404 set_signals(info);
2405 spin_unlock_irqrestore(&info->lock,flags);
2406 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 /* Handle turning off CRTSCTS */
2409 if (old_termios->c_cflag & CRTSCTS &&
2410 !(tty->termios->c_cflag & CRTSCTS)) {
2411 tty->hw_stopped = 0;
2412 tx_release(tty);
2413 }
2414}
2415
2416static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2417{
2418 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2419
2420 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2421 return;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (debug_level >= DEBUG_LEVEL_INFO)
2424 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2425 __FILE__,__LINE__, info->device_name, info->count);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (!info->count)
2428 return;
2429
2430 if (tty_hung_up_p(filp))
2431 goto cleanup;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if ((tty->count == 1) && (info->count != 1)) {
2434 /*
2435 * tty->count is 1 and the tty structure will be freed.
2436 * info->count should be one in this case.
2437 * if it's not, correct it so that the port is shutdown.
2438 */
2439 printk("mgslpc_close: bad refcount; tty->count is 1, "
2440 "info->count is %d\n", info->count);
2441 info->count = 1;
2442 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 info->count--;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 /* if at least one open remaining, leave hardware active */
2447 if (info->count)
2448 goto cleanup;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 info->flags |= ASYNC_CLOSING;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002451
2452 /* set tty->closing to notify line discipline to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 * only process XON/XOFF characters. Only the N_TTY
2454 * discipline appears to use this (ppp does not).
2455 */
2456 tty->closing = 1;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /* wait for transmit data to clear all layers */
Jeff Garzikd12341f2007-10-19 15:45:35 -04002459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
2461 if (debug_level >= DEBUG_LEVEL_INFO)
2462 printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sent\n",
2463 __FILE__,__LINE__, info->device_name );
2464 tty_wait_until_sent(tty, info->closing_wait);
2465 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (info->flags & ASYNC_INITIALIZED)
2468 mgslpc_wait_until_sent(tty, info->timeout);
2469
2470 if (tty->driver->flush_buffer)
2471 tty->driver->flush_buffer(tty);
2472
2473 ldisc_flush_buffer(tty);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 shutdown(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 tty->closing = 0;
2478 info->tty = NULL;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (info->blocked_open) {
2481 if (info->close_delay) {
2482 msleep_interruptible(jiffies_to_msecs(info->close_delay));
2483 }
2484 wake_up_interruptible(&info->open_wait);
2485 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 wake_up_interruptible(&info->close_wait);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002490
2491cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (debug_level >= DEBUG_LEVEL_INFO)
2493 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2494 tty->driver->name, info->count);
2495}
2496
2497/* Wait until the transmitter is empty.
2498 */
2499static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2500{
2501 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2502 unsigned long orig_jiffies, char_time;
2503
2504 if (!info )
2505 return;
2506
2507 if (debug_level >= DEBUG_LEVEL_INFO)
2508 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2509 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2512 return;
2513
2514 if (!(info->flags & ASYNC_INITIALIZED))
2515 goto exit;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 orig_jiffies = jiffies;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 /* Set check interval to 1/5 of estimated time to
2520 * send a character, and make it at least 1. The check
2521 * interval should also be less than the timeout.
2522 * Note: use tight timings here to satisfy the NIST-PCTS.
Jeff Garzikd12341f2007-10-19 15:45:35 -04002523 */
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if ( info->params.data_rate ) {
2526 char_time = info->timeout/(32 * 5);
2527 if (!char_time)
2528 char_time++;
2529 } else
2530 char_time = 1;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (timeout)
2533 char_time = min_t(unsigned long, char_time, timeout);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (info->params.mode == MGSL_MODE_HDLC) {
2536 while (info->tx_active) {
2537 msleep_interruptible(jiffies_to_msecs(char_time));
2538 if (signal_pending(current))
2539 break;
2540 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2541 break;
2542 }
2543 } else {
2544 while ((info->tx_count || info->tx_active) &&
2545 info->tx_enabled) {
2546 msleep_interruptible(jiffies_to_msecs(char_time));
2547 if (signal_pending(current))
2548 break;
2549 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2550 break;
2551 }
2552 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554exit:
2555 if (debug_level >= DEBUG_LEVEL_INFO)
2556 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2557 __FILE__,__LINE__, info->device_name );
2558}
2559
2560/* Called by tty_hangup() when a hangup is signaled.
2561 * This is the same as closing all open files for the port.
2562 */
2563static void mgslpc_hangup(struct tty_struct *tty)
2564{
2565 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (debug_level >= DEBUG_LEVEL_INFO)
2568 printk("%s(%d):mgslpc_hangup(%s)\n",
2569 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2572 return;
2573
2574 mgslpc_flush_buffer(tty);
2575 shutdown(info);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002576
2577 info->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2579 info->tty = NULL;
2580
2581 wake_up_interruptible(&info->open_wait);
2582}
2583
2584/* Block the current process until the specified port
2585 * is ready to be opened.
2586 */
2587static int block_til_ready(struct tty_struct *tty, struct file *filp,
2588 MGSLPC_INFO *info)
2589{
2590 DECLARE_WAITQUEUE(wait, current);
2591 int retval;
Joe Perches0fab6de2008-04-28 02:14:02 -07002592 bool do_clocal = false;
2593 bool extra_count = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (debug_level >= DEBUG_LEVEL_INFO)
2597 printk("%s(%d):block_til_ready on %s\n",
2598 __FILE__,__LINE__, tty->driver->name );
2599
2600 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
2601 /* nonblock mode is set or port is not enabled */
2602 /* just verify that callout device is not active */
2603 info->flags |= ASYNC_NORMAL_ACTIVE;
2604 return 0;
2605 }
2606
2607 if (tty->termios->c_cflag & CLOCAL)
Joe Perches0fab6de2008-04-28 02:14:02 -07002608 do_clocal = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 /* Wait for carrier detect and the line to become
2611 * free (i.e., not in use by the callout). While we are in
2612 * this loop, info->count is dropped by one, so that
2613 * mgslpc_close() knows when to free things. We restore it upon
2614 * exit, either normal or abnormal.
2615 */
Jeff Garzikd12341f2007-10-19 15:45:35 -04002616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 retval = 0;
2618 add_wait_queue(&info->open_wait, &wait);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (debug_level >= DEBUG_LEVEL_INFO)
2621 printk("%s(%d):block_til_ready before block on %s count=%d\n",
2622 __FILE__,__LINE__, tty->driver->name, info->count );
2623
2624 spin_lock_irqsave(&info->lock, flags);
2625 if (!tty_hung_up_p(filp)) {
Joe Perches0fab6de2008-04-28 02:14:02 -07002626 extra_count = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 info->count--;
2628 }
2629 spin_unlock_irqrestore(&info->lock, flags);
2630 info->blocked_open++;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 while (1) {
2633 if ((tty->termios->c_cflag & CBAUD)) {
2634 spin_lock_irqsave(&info->lock,flags);
2635 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2636 set_signals(info);
2637 spin_unlock_irqrestore(&info->lock,flags);
2638 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 set_current_state(TASK_INTERRUPTIBLE);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
2643 retval = (info->flags & ASYNC_HUP_NOTIFY) ?
2644 -EAGAIN : -ERESTARTSYS;
2645 break;
2646 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 spin_lock_irqsave(&info->lock,flags);
2649 get_signals(info);
2650 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if (!(info->flags & ASYNC_CLOSING) &&
2653 (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
2654 break;
2655 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (signal_pending(current)) {
2658 retval = -ERESTARTSYS;
2659 break;
2660 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (debug_level >= DEBUG_LEVEL_INFO)
2663 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
2664 __FILE__,__LINE__, tty->driver->name, info->count );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 schedule();
2667 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 set_current_state(TASK_RUNNING);
2670 remove_wait_queue(&info->open_wait, &wait);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (extra_count)
2673 info->count++;
2674 info->blocked_open--;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (debug_level >= DEBUG_LEVEL_INFO)
2677 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
2678 __FILE__,__LINE__, tty->driver->name, info->count );
Jeff Garzikd12341f2007-10-19 15:45:35 -04002679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (!retval)
2681 info->flags |= ASYNC_NORMAL_ACTIVE;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return retval;
2684}
2685
2686static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2687{
2688 MGSLPC_INFO *info;
2689 int retval, line;
2690 unsigned long flags;
2691
Jeff Garzikd12341f2007-10-19 15:45:35 -04002692 /* verify range of specified line number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 line = tty->index;
2694 if ((line < 0) || (line >= mgslpc_device_count)) {
2695 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2696 __FILE__,__LINE__,line);
2697 return -ENODEV;
2698 }
2699
2700 /* find the info structure for the specified line */
2701 info = mgslpc_device_list;
2702 while(info && info->line != line)
2703 info = info->next_device;
2704 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2705 return -ENODEV;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 tty->driver_data = info;
2708 info->tty = tty;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (debug_level >= DEBUG_LEVEL_INFO)
2711 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2712 __FILE__,__LINE__,tty->driver->name, info->count);
2713
2714 /* If port is closing, signal caller to try again */
2715 if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
2716 if (info->flags & ASYNC_CLOSING)
2717 interruptible_sleep_on(&info->close_wait);
2718 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2719 -EAGAIN : -ERESTARTSYS);
2720 goto cleanup;
2721 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2724
2725 spin_lock_irqsave(&info->netlock, flags);
2726 if (info->netcount) {
2727 retval = -EBUSY;
2728 spin_unlock_irqrestore(&info->netlock, flags);
2729 goto cleanup;
2730 }
2731 info->count++;
2732 spin_unlock_irqrestore(&info->netlock, flags);
2733
2734 if (info->count == 1) {
2735 /* 1st open on this device, init hardware */
2736 retval = startup(info);
2737 if (retval < 0)
2738 goto cleanup;
2739 }
2740
2741 retval = block_til_ready(tty, filp, info);
2742 if (retval) {
2743 if (debug_level >= DEBUG_LEVEL_INFO)
2744 printk("%s(%d):block_til_ready(%s) returned %d\n",
2745 __FILE__,__LINE__, info->device_name, retval);
2746 goto cleanup;
2747 }
2748
2749 if (debug_level >= DEBUG_LEVEL_INFO)
2750 printk("%s(%d):mgslpc_open(%s) success\n",
2751 __FILE__,__LINE__, info->device_name);
2752 retval = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002753
2754cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (retval) {
2756 if (tty->count == 1)
2757 info->tty = NULL; /* tty layer will release tty struct */
2758 if(info->count)
2759 info->count--;
2760 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return retval;
2763}
2764
2765/*
2766 * /proc fs routines....
2767 */
2768
2769static inline int line_info(char *buf, MGSLPC_INFO *info)
2770{
2771 char stat_buf[30];
2772 int ret;
2773 unsigned long flags;
2774
2775 ret = sprintf(buf, "%s:io:%04X irq:%d",
2776 info->device_name, info->io_base, info->irq_level);
2777
2778 /* output current serial signal states */
2779 spin_lock_irqsave(&info->lock,flags);
2780 get_signals(info);
2781 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 stat_buf[0] = 0;
2784 stat_buf[1] = 0;
2785 if (info->serial_signals & SerialSignal_RTS)
2786 strcat(stat_buf, "|RTS");
2787 if (info->serial_signals & SerialSignal_CTS)
2788 strcat(stat_buf, "|CTS");
2789 if (info->serial_signals & SerialSignal_DTR)
2790 strcat(stat_buf, "|DTR");
2791 if (info->serial_signals & SerialSignal_DSR)
2792 strcat(stat_buf, "|DSR");
2793 if (info->serial_signals & SerialSignal_DCD)
2794 strcat(stat_buf, "|CD");
2795 if (info->serial_signals & SerialSignal_RI)
2796 strcat(stat_buf, "|RI");
2797
2798 if (info->params.mode == MGSL_MODE_HDLC) {
2799 ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
2800 info->icount.txok, info->icount.rxok);
2801 if (info->icount.txunder)
2802 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
2803 if (info->icount.txabort)
2804 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
2805 if (info->icount.rxshort)
Jeff Garzikd12341f2007-10-19 15:45:35 -04002806 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (info->icount.rxlong)
2808 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
2809 if (info->icount.rxover)
2810 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
2811 if (info->icount.rxcrc)
2812 ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
2813 } else {
2814 ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
2815 info->icount.tx, info->icount.rx);
2816 if (info->icount.frame)
2817 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2818 if (info->icount.parity)
2819 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2820 if (info->icount.brk)
Jeff Garzikd12341f2007-10-19 15:45:35 -04002821 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (info->icount.overrun)
2823 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2824 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 /* Append serial signal status to end */
2827 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2830 info->tx_active,info->bh_requested,info->bh_running,
2831 info->pending_bh);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return ret;
2834}
2835
2836/* Called to print information about devices
2837 */
2838static int mgslpc_read_proc(char *page, char **start, off_t off, int count,
2839 int *eof, void *data)
2840{
2841 int len = 0, l;
2842 off_t begin = 0;
2843 MGSLPC_INFO *info;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 len += sprintf(page, "synclink driver:%s\n", driver_version);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 info = mgslpc_device_list;
2848 while( info ) {
2849 l = line_info(page + len, info);
2850 len += l;
2851 if (len+begin > off+count)
2852 goto done;
2853 if (len+begin < off) {
2854 begin += len;
2855 len = 0;
2856 }
2857 info = info->next_device;
2858 }
2859
2860 *eof = 1;
2861done:
2862 if (off >= len+begin)
2863 return 0;
2864 *start = page + (off-begin);
2865 return ((count < begin+len-off) ? count : begin+len-off);
2866}
2867
Peter Hagervallcdaad342006-06-23 02:06:04 -07002868static int rx_alloc_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
2870 /* each buffer has header and data */
2871 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2872
2873 /* calculate total allocation size for 8 buffers */
2874 info->rx_buf_total_size = info->rx_buf_size * 8;
2875
2876 /* limit total allocated memory */
2877 if (info->rx_buf_total_size > 0x10000)
2878 info->rx_buf_total_size = 0x10000;
2879
2880 /* calculate number of buffers */
2881 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2882
2883 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2884 if (info->rx_buf == NULL)
2885 return -ENOMEM;
2886
2887 rx_reset_buffers(info);
2888 return 0;
2889}
2890
Peter Hagervallcdaad342006-06-23 02:06:04 -07002891static void rx_free_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
Jesper Juhl735d5662005-11-07 01:01:29 -08002893 kfree(info->rx_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 info->rx_buf = NULL;
2895}
2896
Peter Hagervallcdaad342006-06-23 02:06:04 -07002897static int claim_resources(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
2899 if (rx_alloc_buffers(info) < 0 ) {
2900 printk( "Cant allocate rx buffer %s\n", info->device_name);
2901 release_resources(info);
2902 return -ENODEV;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return 0;
2905}
2906
Peter Hagervallcdaad342006-06-23 02:06:04 -07002907static void release_resources(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
2909 if (debug_level >= DEBUG_LEVEL_INFO)
2910 printk("release_resources(%s)\n", info->device_name);
2911 rx_free_buffers(info);
2912}
2913
2914/* Add the specified device instance data structure to the
2915 * global linked list of devices and increment the device count.
Jeff Garzikd12341f2007-10-19 15:45:35 -04002916 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 * Arguments: info pointer to device instance data
2918 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07002919static void mgslpc_add_device(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
2921 info->next_device = NULL;
2922 info->line = mgslpc_device_count;
2923 sprintf(info->device_name,"ttySLP%d",info->line);
Jeff Garzikd12341f2007-10-19 15:45:35 -04002924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 if (info->line < MAX_DEVICE_COUNT) {
2926 if (maxframe[info->line])
2927 info->max_frame_size = maxframe[info->line];
2928 info->dosyncppp = dosyncppp[info->line];
2929 }
2930
2931 mgslpc_device_count++;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (!mgslpc_device_list)
2934 mgslpc_device_list = info;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002935 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 MGSLPC_INFO *current_dev = mgslpc_device_list;
2937 while( current_dev->next_device )
2938 current_dev = current_dev->next_device;
2939 current_dev->next_device = info;
2940 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04002941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 if (info->max_frame_size < 4096)
2943 info->max_frame_size = 4096;
2944 else if (info->max_frame_size > 65535)
2945 info->max_frame_size = 65535;
Jeff Garzikd12341f2007-10-19 15:45:35 -04002946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2948 info->device_name, info->io_base, info->irq_level);
2949
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002950#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 hdlcdev_init(info);
2952#endif
2953}
2954
Peter Hagervallcdaad342006-06-23 02:06:04 -07002955static void mgslpc_remove_device(MGSLPC_INFO *remove_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 MGSLPC_INFO *info = mgslpc_device_list;
2958 MGSLPC_INFO *last = NULL;
2959
2960 while(info) {
2961 if (info == remove_info) {
2962 if (last)
2963 last->next_device = info->next_device;
2964 else
2965 mgslpc_device_list = info->next_device;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002966#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 hdlcdev_exit(info);
2968#endif
2969 release_resources(info);
2970 kfree(info);
2971 mgslpc_device_count--;
2972 return;
2973 }
2974 last = info;
2975 info = info->next_device;
2976 }
2977}
2978
Dominik Brodowski4af48c82005-06-27 16:28:42 -07002979static struct pcmcia_device_id mgslpc_ids[] = {
2980 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
2981 PCMCIA_DEVICE_NULL
2982};
2983MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985static struct pcmcia_driver mgslpc_driver = {
2986 .owner = THIS_MODULE,
2987 .drv = {
2988 .name = "synclink_cs",
2989 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02002990 .probe = mgslpc_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01002991 .remove = mgslpc_detach,
Dominik Brodowski4af48c82005-06-27 16:28:42 -07002992 .id_table = mgslpc_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +01002993 .suspend = mgslpc_suspend,
2994 .resume = mgslpc_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995};
2996
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002997static const struct tty_operations mgslpc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 .open = mgslpc_open,
2999 .close = mgslpc_close,
3000 .write = mgslpc_write,
3001 .put_char = mgslpc_put_char,
3002 .flush_chars = mgslpc_flush_chars,
3003 .write_room = mgslpc_write_room,
3004 .chars_in_buffer = mgslpc_chars_in_buffer,
3005 .flush_buffer = mgslpc_flush_buffer,
3006 .ioctl = mgslpc_ioctl,
3007 .throttle = mgslpc_throttle,
3008 .unthrottle = mgslpc_unthrottle,
3009 .send_xchar = mgslpc_send_xchar,
3010 .break_ctl = mgslpc_break,
3011 .wait_until_sent = mgslpc_wait_until_sent,
3012 .read_proc = mgslpc_read_proc,
3013 .set_termios = mgslpc_set_termios,
3014 .stop = tx_pause,
3015 .start = tx_release,
3016 .hangup = mgslpc_hangup,
3017 .tiocmget = tiocmget,
3018 .tiocmset = tiocmset,
3019};
3020
3021static void synclink_cs_cleanup(void)
3022{
3023 int rc;
3024
3025 printk("Unloading %s: version %s\n", driver_name, driver_version);
3026
3027 while(mgslpc_device_list)
3028 mgslpc_remove_device(mgslpc_device_list);
3029
3030 if (serial_driver) {
3031 if ((rc = tty_unregister_driver(serial_driver)))
3032 printk("%s(%d) failed to unregister tty driver err=%d\n",
3033 __FILE__,__LINE__,rc);
3034 put_tty_driver(serial_driver);
3035 }
3036
3037 pcmcia_unregister_driver(&mgslpc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
3040static int __init synclink_cs_init(void)
3041{
3042 int rc;
3043
3044 if (break_on_load) {
3045 mgslpc_get_text_ptr();
3046 BREAKPOINT();
3047 }
3048
3049 printk("%s %s\n", driver_name, driver_version);
3050
3051 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
3052 return rc;
3053
3054 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
3055 if (!serial_driver) {
3056 rc = -ENOMEM;
3057 goto error;
3058 }
3059
3060 /* Initialize the tty_driver structure */
Jeff Garzikd12341f2007-10-19 15:45:35 -04003061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 serial_driver->owner = THIS_MODULE;
3063 serial_driver->driver_name = "synclink_cs";
3064 serial_driver->name = "ttySLP";
3065 serial_driver->major = ttymajor;
3066 serial_driver->minor_start = 64;
3067 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3068 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3069 serial_driver->init_termios = tty_std_termios;
3070 serial_driver->init_termios.c_cflag =
3071 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3072 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3073 tty_set_operations(serial_driver, &mgslpc_ops);
3074
3075 if ((rc = tty_register_driver(serial_driver)) < 0) {
3076 printk("%s(%d):Couldn't register serial driver\n",
3077 __FILE__,__LINE__);
3078 put_tty_driver(serial_driver);
3079 serial_driver = NULL;
3080 goto error;
3081 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04003082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 printk("%s %s, tty major#%d\n",
3084 driver_name, driver_version,
3085 serial_driver->major);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return 0;
3088
3089error:
3090 synclink_cs_cleanup();
3091 return rc;
3092}
3093
Jeff Garzikd12341f2007-10-19 15:45:35 -04003094static void __exit synclink_cs_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
3096 synclink_cs_cleanup();
3097}
3098
3099module_init(synclink_cs_init);
3100module_exit(synclink_cs_exit);
3101
3102static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
3103{
3104 unsigned int M, N;
3105 unsigned char val;
3106
Jeff Garzikd12341f2007-10-19 15:45:35 -04003107 /* note:standard BRG mode is broken in V3.2 chip
3108 * so enhanced mode is always used
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 */
3110
3111 if (rate) {
3112 N = 3686400 / rate;
3113 if (!N)
3114 N = 1;
3115 N >>= 1;
3116 for (M = 1; N > 64 && M < 16; M++)
3117 N >>= 1;
3118 N--;
3119
3120 /* BGR[5..0] = N
3121 * BGR[9..6] = M
3122 * BGR[7..0] contained in BGR register
3123 * BGR[9..8] contained in CCR2[7..6]
3124 * divisor = (N+1)*2^M
3125 *
3126 * Note: M *must* not be zero (causes asymetric duty cycle)
Jeff Garzikd12341f2007-10-19 15:45:35 -04003127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 write_reg(info, (unsigned char) (channel + BGR),
3129 (unsigned char) ((M << 6) + N));
3130 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
3131 val |= ((M << 4) & 0xc0);
3132 write_reg(info, (unsigned char) (channel + CCR2), val);
3133 }
3134}
3135
3136/* Enabled the AUX clock output at the specified frequency.
3137 */
3138static void enable_auxclk(MGSLPC_INFO *info)
3139{
3140 unsigned char val;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 /* MODE
3143 *
3144 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3145 * 05 ADM Address Mode, 0 = no addr recognition
3146 * 04 TMD Timer Mode, 0 = external
3147 * 03 RAC Receiver Active, 0 = inactive
3148 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3149 * 01 TRS Timer Resolution, 1=512
3150 * 00 TLP Test Loop, 0 = no loop
3151 *
3152 * 1000 0010
Jeff Garzikd12341f2007-10-19 15:45:35 -04003153 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 val = 0x82;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003155
3156 /* channel B RTS is used to enable AUXCLK driver on SP505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3158 val |= BIT2;
3159 write_reg(info, CHB + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 /* CCR0
3162 *
3163 * 07 PU Power Up, 1=active, 0=power down
3164 * 06 MCE Master Clock Enable, 1=enabled
3165 * 05 Reserved, 0
3166 * 04..02 SC[2..0] Encoding
3167 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3168 *
3169 * 11000000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 write_reg(info, CHB + CCR0, 0xc0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 /* CCR1
3174 *
3175 * 07 SFLG Shared Flag, 0 = disable shared flags
3176 * 06 GALP Go Active On Loop, 0 = not used
3177 * 05 GLP Go On Loop, 0 = not used
3178 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3179 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3180 * 02..00 CM[2..0] Clock Mode
3181 *
3182 * 0001 0111
Jeff Garzikd12341f2007-10-19 15:45:35 -04003183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 write_reg(info, CHB + CCR1, 0x17);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 /* CCR2 (Channel B)
3187 *
3188 * 07..06 BGR[9..8] Baud rate bits 9..8
3189 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3190 * 04 SSEL Clock source select, 1=submode b
3191 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3192 * 02 RWX Read/Write Exchange 0=disabled
3193 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3194 * 00 DIV, data inversion 0=disabled, 1=enabled
3195 *
3196 * 0011 1000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3199 write_reg(info, CHB + CCR2, 0x38);
3200 else
3201 write_reg(info, CHB + CCR2, 0x30);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 /* CCR4
3204 *
3205 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3206 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3207 * 05 TST1 Test Pin, 0=normal operation
3208 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3209 * 03..02 Reserved, must be 0
3210 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3211 *
3212 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003213 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 write_reg(info, CHB + CCR4, 0x50);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003215
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 /* if auxclk not enabled, set internal BRG so
3217 * CTS transitions can be detected (requires TxC)
Jeff Garzikd12341f2007-10-19 15:45:35 -04003218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3220 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3221 else
3222 mgslpc_set_rate(info, CHB, 921600);
3223}
3224
Jeff Garzikd12341f2007-10-19 15:45:35 -04003225static void loopback_enable(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 unsigned char val;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003228
3229 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3231 write_reg(info, CHA + CCR1, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003232
3233 /* CCR2:04 SSEL Clock source select, 1=submode b */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3235 write_reg(info, CHA + CCR2, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003236
3237 /* set LinkSpeed if available, otherwise default to 2Mbps */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 if (info->params.clock_speed)
3239 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3240 else
3241 mgslpc_set_rate(info, CHA, 1843200);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003242
3243 /* MODE:00 TLP Test Loop, 1=loopback enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 val = read_reg(info, CHA + MODE) | BIT0;
3245 write_reg(info, CHA + MODE, val);
3246}
3247
Peter Hagervallcdaad342006-06-23 02:06:04 -07003248static void hdlc_mode(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
3250 unsigned char val;
3251 unsigned char clkmode, clksubmode;
3252
Jeff Garzikd12341f2007-10-19 15:45:35 -04003253 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 irq_disable(info, CHA, 0xffff);
3255 irq_disable(info, CHB, 0xffff);
3256 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003257
3258 /* assume clock mode 0a, rcv=RxC xmt=TxC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 clkmode = clksubmode = 0;
3260 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3261 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003262 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 clkmode = 7;
3264 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3265 && info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003266 /* clock mode 7b, rcv = BRG, xmt = BRG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 clkmode = 7;
3268 clksubmode = 1;
3269 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3270 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003271 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 clkmode = 6;
3273 clksubmode = 1;
3274 } else {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003275 /* clock mode 6a, rcv = DPLL, xmt = TxC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 clkmode = 6;
3277 }
3278 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
Jeff Garzikd12341f2007-10-19 15:45:35 -04003279 /* clock mode 0b, rcv = RxC, xmt = BRG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 clksubmode = 1;
3281 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04003282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 /* MODE
3284 *
3285 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3286 * 05 ADM Address Mode, 0 = no addr recognition
3287 * 04 TMD Timer Mode, 0 = external
3288 * 03 RAC Receiver Active, 0 = inactive
3289 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3290 * 01 TRS Timer Resolution, 1=512
3291 * 00 TLP Test Loop, 0 = no loop
3292 *
3293 * 1000 0010
Jeff Garzikd12341f2007-10-19 15:45:35 -04003294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 val = 0x82;
3296 if (info->params.loopback)
3297 val |= BIT0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003298
3299 /* preserve RTS state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (info->serial_signals & SerialSignal_RTS)
3301 val |= BIT2;
3302 write_reg(info, CHA + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 /* CCR0
3305 *
3306 * 07 PU Power Up, 1=active, 0=power down
3307 * 06 MCE Master Clock Enable, 1=enabled
3308 * 05 Reserved, 0
3309 * 04..02 SC[2..0] Encoding
3310 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3311 *
3312 * 11000000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 val = 0xc0;
3315 switch (info->params.encoding)
3316 {
3317 case HDLC_ENCODING_NRZI:
3318 val |= BIT3;
3319 break;
3320 case HDLC_ENCODING_BIPHASE_SPACE:
3321 val |= BIT4;
3322 break; // FM0
3323 case HDLC_ENCODING_BIPHASE_MARK:
3324 val |= BIT4 + BIT2;
3325 break; // FM1
3326 case HDLC_ENCODING_BIPHASE_LEVEL:
3327 val |= BIT4 + BIT3;
3328 break; // Manchester
3329 }
3330 write_reg(info, CHA + CCR0, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 /* CCR1
3333 *
3334 * 07 SFLG Shared Flag, 0 = disable shared flags
3335 * 06 GALP Go Active On Loop, 0 = not used
3336 * 05 GLP Go On Loop, 0 = not used
3337 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3338 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3339 * 02..00 CM[2..0] Clock Mode
3340 *
3341 * 0001 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003342 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 val = 0x10 + clkmode;
3344 write_reg(info, CHA + CCR1, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 /* CCR2
3347 *
3348 * 07..06 BGR[9..8] Baud rate bits 9..8
3349 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3350 * 04 SSEL Clock source select, 1=submode b
3351 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3352 * 02 RWX Read/Write Exchange 0=disabled
3353 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3354 * 00 DIV, data inversion 0=disabled, 1=enabled
3355 *
3356 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 val = 0x00;
3359 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3360 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3361 val |= BIT5;
3362 if (clksubmode)
3363 val |= BIT4;
3364 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3365 val |= BIT1;
3366 if (info->params.encoding == HDLC_ENCODING_NRZB)
3367 val |= BIT0;
3368 write_reg(info, CHA + CCR2, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 /* CCR3
3371 *
3372 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3373 * 05 EPT Enable preamble transmission, 1=enabled
3374 * 04 RADD Receive address pushed to FIFO, 0=disabled
3375 * 03 CRL CRC Reset Level, 0=FFFF
3376 * 02 RCRC Rx CRC 0=On 1=Off
3377 * 01 TCRC Tx CRC 0=On 1=Off
3378 * 00 PSD DPLL Phase Shift Disable
3379 *
3380 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 val = 0x00;
3383 if (info->params.crc_type == HDLC_CRC_NONE)
3384 val |= BIT2 + BIT1;
3385 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3386 val |= BIT5;
3387 switch (info->params.preamble_length)
3388 {
3389 case HDLC_PREAMBLE_LENGTH_16BITS:
3390 val |= BIT6;
3391 break;
3392 case HDLC_PREAMBLE_LENGTH_32BITS:
3393 val |= BIT6;
3394 break;
3395 case HDLC_PREAMBLE_LENGTH_64BITS:
3396 val |= BIT7 + BIT6;
3397 break;
3398 }
3399 write_reg(info, CHA + CCR3, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003400
3401 /* PRE - Preamble pattern */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 val = 0;
3403 switch (info->params.preamble)
3404 {
3405 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3406 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3407 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3408 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3409 }
3410 write_reg(info, CHA + PRE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 /* CCR4
3413 *
3414 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3415 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3416 * 05 TST1 Test Pin, 0=normal operation
3417 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3418 * 03..02 Reserved, must be 0
3419 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3420 *
3421 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 val = 0x50;
3424 write_reg(info, CHA + CCR4, val);
3425 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3426 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3427 else
3428 mgslpc_set_rate(info, CHA, info->params.clock_speed);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 /* RLCR Receive length check register
3431 *
3432 * 7 1=enable receive length check
3433 * 6..0 Max frame length = (RL + 1) * 32
Jeff Garzikd12341f2007-10-19 15:45:35 -04003434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 write_reg(info, CHA + RLCR, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 /* XBCH Transmit Byte Count High
3438 *
3439 * 07 DMA mode, 0 = interrupt driven
3440 * 06 NRM, 0=ABM (ignored)
3441 * 05 CAS Carrier Auto Start
3442 * 04 XC Transmit Continuously (ignored)
3443 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3444 *
3445 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 val = 0x00;
3448 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3449 val |= BIT5;
3450 write_reg(info, CHA + XBCH, val);
3451 enable_auxclk(info);
3452 if (info->params.loopback || info->testing_irq)
3453 loopback_enable(info);
3454 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3455 {
3456 irq_enable(info, CHB, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003457 /* PVR[3] 1=AUTO CTS active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 set_reg_bits(info, CHA + PVR, BIT3);
3459 } else
3460 clear_reg_bits(info, CHA + PVR, BIT3);
3461
3462 irq_enable(info, CHA,
3463 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3464 IRQ_UNDERRUN + IRQ_TXFIFO);
3465 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3466 wait_command_complete(info, CHA);
3467 read_reg16(info, CHA + ISR); /* clear pending IRQs */
Jeff Garzikd12341f2007-10-19 15:45:35 -04003468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 /* Master clock mode enabled above to allow reset commands
3470 * to complete even if no data clocks are present.
3471 *
3472 * Disable master clock mode for normal communications because
3473 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3474 * IRQ when in master clock mode.
3475 *
3476 * Leave master clock mode enabled for IRQ test because the
3477 * timer IRQ used by the test can only happen in master clock mode.
Jeff Garzikd12341f2007-10-19 15:45:35 -04003478 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 if (!info->testing_irq)
3480 clear_reg_bits(info, CHA + CCR0, BIT6);
3481
3482 tx_set_idle(info);
3483
3484 tx_stop(info);
3485 rx_stop(info);
3486}
3487
Peter Hagervallcdaad342006-06-23 02:06:04 -07003488static void rx_stop(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489{
3490 if (debug_level >= DEBUG_LEVEL_ISR)
3491 printk("%s(%d):rx_stop(%s)\n",
3492 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003493
3494 /* MODE:03 RAC Receiver Active, 0=inactive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 clear_reg_bits(info, CHA + MODE, BIT3);
3496
Joe Perches0fab6de2008-04-28 02:14:02 -07003497 info->rx_enabled = false;
3498 info->rx_overflow = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
Peter Hagervallcdaad342006-06-23 02:06:04 -07003501static void rx_start(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
3503 if (debug_level >= DEBUG_LEVEL_ISR)
3504 printk("%s(%d):rx_start(%s)\n",
3505 __FILE__,__LINE__, info->device_name );
3506
3507 rx_reset_buffers(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003508 info->rx_enabled = false;
3509 info->rx_overflow = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Jeff Garzikd12341f2007-10-19 15:45:35 -04003511 /* MODE:03 RAC Receiver Active, 1=active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 set_reg_bits(info, CHA + MODE, BIT3);
3513
Joe Perches0fab6de2008-04-28 02:14:02 -07003514 info->rx_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Peter Hagervallcdaad342006-06-23 02:06:04 -07003517static void tx_start(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
3519 if (debug_level >= DEBUG_LEVEL_ISR)
3520 printk("%s(%d):tx_start(%s)\n",
3521 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 if (info->tx_count) {
3524 /* If auto RTS enabled and RTS is inactive, then assert */
3525 /* RTS and set a flag indicating that the driver should */
3526 /* negate RTS when the transmission completes. */
Joe Perches0fab6de2008-04-28 02:14:02 -07003527 info->drop_rts_on_tx_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
3529 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3530 get_signals(info);
3531 if (!(info->serial_signals & SerialSignal_RTS)) {
3532 info->serial_signals |= SerialSignal_RTS;
3533 set_signals(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003534 info->drop_rts_on_tx_done = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 }
3536 }
3537
3538 if (info->params.mode == MGSL_MODE_ASYNC) {
3539 if (!info->tx_active) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003540 info->tx_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 tx_ready(info);
3542 }
3543 } else {
Joe Perches0fab6de2008-04-28 02:14:02 -07003544 info->tx_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 tx_ready(info);
Jiri Slaby40565f12007-02-12 00:52:31 -08003546 mod_timer(&info->tx_timer, jiffies +
3547 msecs_to_jiffies(5000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 }
3549 }
3550
3551 if (!info->tx_enabled)
Joe Perches0fab6de2008-04-28 02:14:02 -07003552 info->tx_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553}
3554
Peter Hagervallcdaad342006-06-23 02:06:04 -07003555static void tx_stop(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
3557 if (debug_level >= DEBUG_LEVEL_ISR)
3558 printk("%s(%d):tx_stop(%s)\n",
3559 __FILE__,__LINE__, info->device_name );
Jeff Garzikd12341f2007-10-19 15:45:35 -04003560
3561 del_timer(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Joe Perches0fab6de2008-04-28 02:14:02 -07003563 info->tx_enabled = false;
3564 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565}
3566
3567/* Reset the adapter to a known state and prepare it for further use.
3568 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003569static void reset_device(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003571 /* power up both channels (set BIT7) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 write_reg(info, CHA + CCR0, 0x80);
3573 write_reg(info, CHB + CCR0, 0x80);
3574 write_reg(info, CHA + MODE, 0);
3575 write_reg(info, CHB + MODE, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003576
3577 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 irq_disable(info, CHA, 0xffff);
3579 irq_disable(info, CHB, 0xffff);
3580 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 /* PCR Port Configuration Register
3583 *
3584 * 07..04 DEC[3..0] Serial I/F select outputs
3585 * 03 output, 1=AUTO CTS control enabled
3586 * 02 RI Ring Indicator input 0=active
3587 * 01 DSR input 0=active
3588 * 00 DTR output 0=active
3589 *
3590 * 0000 0110
Jeff Garzikd12341f2007-10-19 15:45:35 -04003591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 write_reg(info, PCR, 0x06);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 /* PVR Port Value Register
3595 *
3596 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3597 * 03 AUTO CTS output 1=enabled
3598 * 02 RI Ring Indicator input
3599 * 01 DSR input
3600 * 00 DTR output (1=inactive)
3601 *
3602 * 0000 0001
3603 */
3604// write_reg(info, PVR, PVR_DTR);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 /* IPC Interrupt Port Configuration
3607 *
3608 * 07 VIS 1=Masked interrupts visible
3609 * 06..05 Reserved, 0
3610 * 04..03 SLA Slave address, 00 ignored
3611 * 02 CASM Cascading Mode, 1=daisy chain
3612 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3613 *
3614 * 0000 0101
Jeff Garzikd12341f2007-10-19 15:45:35 -04003615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 write_reg(info, IPC, 0x05);
3617}
3618
Peter Hagervallcdaad342006-06-23 02:06:04 -07003619static void async_mode(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 unsigned char val;
3622
Jeff Garzikd12341f2007-10-19 15:45:35 -04003623 /* disable all interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 irq_disable(info, CHA, 0xffff);
3625 irq_disable(info, CHB, 0xffff);
3626 port_irq_disable(info, 0xff);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 /* MODE
3629 *
3630 * 07 Reserved, 0
3631 * 06 FRTS RTS State, 0=active
3632 * 05 FCTS Flow Control on CTS
3633 * 04 FLON Flow Control Enable
3634 * 03 RAC Receiver Active, 0 = inactive
3635 * 02 RTS 0=Auto RTS, 1=manual RTS
3636 * 01 TRS Timer Resolution, 1=512
3637 * 00 TLP Test Loop, 0 = no loop
3638 *
3639 * 0000 0110
Jeff Garzikd12341f2007-10-19 15:45:35 -04003640 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 val = 0x06;
3642 if (info->params.loopback)
3643 val |= BIT0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003644
3645 /* preserve RTS state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 if (!(info->serial_signals & SerialSignal_RTS))
3647 val |= BIT6;
3648 write_reg(info, CHA + MODE, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 /* CCR0
3651 *
3652 * 07 PU Power Up, 1=active, 0=power down
3653 * 06 MCE Master Clock Enable, 1=enabled
3654 * 05 Reserved, 0
3655 * 04..02 SC[2..0] Encoding, 000=NRZ
3656 * 01..00 SM[1..0] Serial Mode, 11=Async
3657 *
3658 * 1000 0011
Jeff Garzikd12341f2007-10-19 15:45:35 -04003659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 write_reg(info, CHA + CCR0, 0x83);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 /* CCR1
3663 *
3664 * 07..05 Reserved, 0
3665 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3666 * 03 BCR Bit Clock Rate, 1=16x
3667 * 02..00 CM[2..0] Clock Mode, 111=BRG
3668 *
3669 * 0001 1111
Jeff Garzikd12341f2007-10-19 15:45:35 -04003670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 write_reg(info, CHA + CCR1, 0x1f);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 /* CCR2 (channel A)
3674 *
3675 * 07..06 BGR[9..8] Baud rate bits 9..8
3676 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3677 * 04 SSEL Clock source select, 1=submode b
3678 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3679 * 02 RWX Read/Write Exchange 0=disabled
3680 * 01 Reserved, 0
3681 * 00 DIV, data inversion 0=disabled, 1=enabled
3682 *
3683 * 0001 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003684 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 write_reg(info, CHA + CCR2, 0x10);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 /* CCR3
3688 *
3689 * 07..01 Reserved, 0
3690 * 00 PSD DPLL Phase Shift Disable
3691 *
3692 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 write_reg(info, CHA + CCR3, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 /* CCR4
3697 *
3698 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3699 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3700 * 05 TST1 Test Pin, 0=normal operation
3701 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3702 * 03..00 Reserved, must be 0
3703 *
3704 * 0101 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003705 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 write_reg(info, CHA + CCR4, 0x50);
3707 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 /* DAFO Data Format
3710 *
3711 * 07 Reserved, 0
3712 * 06 XBRK transmit break, 0=normal operation
3713 * 05 Stop bits (0=1, 1=2)
3714 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3715 * 02 PAREN Parity Enable
3716 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3717 *
Jeff Garzikd12341f2007-10-19 15:45:35 -04003718 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 val = 0x00;
3720 if (info->params.data_bits != 8)
3721 val |= BIT0; /* 7 bits */
3722 if (info->params.stop_bits != 1)
3723 val |= BIT5;
3724 if (info->params.parity != ASYNC_PARITY_NONE)
3725 {
3726 val |= BIT2; /* Parity enable */
3727 if (info->params.parity == ASYNC_PARITY_ODD)
3728 val |= BIT3;
3729 else
3730 val |= BIT4;
3731 }
3732 write_reg(info, CHA + DAFO, val);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 /* RFC Rx FIFO Control
3735 *
3736 * 07 Reserved, 0
3737 * 06 DPS, 1=parity bit not stored in data byte
3738 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3739 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3740 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3741 * 01 Reserved, 0
3742 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3743 *
3744 * 0101 1100
Jeff Garzikd12341f2007-10-19 15:45:35 -04003745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 write_reg(info, CHA + RFC, 0x5c);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 /* RLCR Receive length check register
3749 *
3750 * Max frame length = (RL + 1) * 32
Jeff Garzikd12341f2007-10-19 15:45:35 -04003751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 write_reg(info, CHA + RLCR, 0);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003753
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 /* XBCH Transmit Byte Count High
3755 *
3756 * 07 DMA mode, 0 = interrupt driven
3757 * 06 NRM, 0=ABM (ignored)
3758 * 05 CAS Carrier Auto Start
3759 * 04 XC Transmit Continuously (ignored)
3760 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3761 *
3762 * 0000 0000
Jeff Garzikd12341f2007-10-19 15:45:35 -04003763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 val = 0x00;
3765 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3766 val |= BIT5;
3767 write_reg(info, CHA + XBCH, val);
3768 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3769 irq_enable(info, CHA, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003770
3771 /* MODE:03 RAC Receiver Active, 1=active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 set_reg_bits(info, CHA + MODE, BIT3);
3773 enable_auxclk(info);
3774 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3775 irq_enable(info, CHB, IRQ_CTS);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003776 /* PVR[3] 1=AUTO CTS active */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 set_reg_bits(info, CHA + PVR, BIT3);
3778 } else
3779 clear_reg_bits(info, CHA + PVR, BIT3);
3780 irq_enable(info, CHA,
3781 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3782 IRQ_ALLSENT + IRQ_TXFIFO);
3783 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3784 wait_command_complete(info, CHA);
3785 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3786}
3787
3788/* Set the HDLC idle mode for the transmitter.
3789 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003790static void tx_set_idle(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003792 /* Note: ESCC2 only supports flags and one idle modes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3794 set_reg_bits(info, CHA + CCR1, BIT3);
3795 else
3796 clear_reg_bits(info, CHA + CCR1, BIT3);
3797}
3798
3799/* get state of the V24 status (input) signals.
3800 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003801static void get_signals(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
3803 unsigned char status = 0;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003804
3805 /* preserve DTR and RTS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3807
3808 if (read_reg(info, CHB + VSTR) & BIT7)
3809 info->serial_signals |= SerialSignal_DCD;
3810 if (read_reg(info, CHB + STAR) & BIT1)
3811 info->serial_signals |= SerialSignal_CTS;
3812
3813 status = read_reg(info, CHA + PVR);
3814 if (!(status & PVR_RI))
3815 info->serial_signals |= SerialSignal_RI;
3816 if (!(status & PVR_DSR))
3817 info->serial_signals |= SerialSignal_DSR;
3818}
3819
3820/* Set the state of DTR and RTS based on contents of
3821 * serial_signals member of device extension.
3822 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07003823static void set_signals(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
3825 unsigned char val;
3826
3827 val = read_reg(info, CHA + MODE);
3828 if (info->params.mode == MGSL_MODE_ASYNC) {
3829 if (info->serial_signals & SerialSignal_RTS)
3830 val &= ~BIT6;
3831 else
3832 val |= BIT6;
3833 } else {
3834 if (info->serial_signals & SerialSignal_RTS)
3835 val |= BIT2;
3836 else
3837 val &= ~BIT2;
3838 }
3839 write_reg(info, CHA + MODE, val);
3840
3841 if (info->serial_signals & SerialSignal_DTR)
3842 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3843 else
3844 set_reg_bits(info, CHA + PVR, PVR_DTR);
3845}
3846
Peter Hagervallcdaad342006-06-23 02:06:04 -07003847static void rx_reset_buffers(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848{
3849 RXBUF *buf;
3850 int i;
3851
3852 info->rx_put = 0;
3853 info->rx_get = 0;
3854 info->rx_frame_count = 0;
3855 for (i=0 ; i < info->rx_buf_count ; i++) {
3856 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3857 buf->status = buf->count = 0;
3858 }
3859}
3860
3861/* Attempt to return a received HDLC frame
3862 * Only frames received without errors are returned.
3863 *
Joe Perches0fab6de2008-04-28 02:14:02 -07003864 * Returns true if frame returned, otherwise false
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 */
Joe Perches0fab6de2008-04-28 02:14:02 -07003866static bool rx_get_frame(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867{
3868 unsigned short status;
3869 RXBUF *buf;
3870 unsigned int framesize = 0;
3871 unsigned long flags;
3872 struct tty_struct *tty = info->tty;
Joe Perches0fab6de2008-04-28 02:14:02 -07003873 bool return_frame = false;
Jeff Garzikd12341f2007-10-19 15:45:35 -04003874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 if (info->rx_frame_count == 0)
Joe Perches0fab6de2008-04-28 02:14:02 -07003876 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3879
3880 status = buf->status;
3881
3882 /* 07 VFR 1=valid frame
3883 * 06 RDO 1=data overrun
3884 * 05 CRC 1=OK, 0=error
3885 * 04 RAB 1=frame aborted
3886 */
3887 if ((status & 0xf0) != 0xA0) {
3888 if (!(status & BIT7) || (status & BIT4))
3889 info->icount.rxabort++;
3890 else if (status & BIT6)
3891 info->icount.rxover++;
3892 else if (!(status & BIT5)) {
3893 info->icount.rxcrc++;
3894 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
Joe Perches0fab6de2008-04-28 02:14:02 -07003895 return_frame = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 }
3897 framesize = 0;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003898#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 {
3900 struct net_device_stats *stats = hdlc_stats(info->netdev);
3901 stats->rx_errors++;
3902 stats->rx_frame_errors++;
3903 }
3904#endif
3905 } else
Joe Perches0fab6de2008-04-28 02:14:02 -07003906 return_frame = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
3908 if (return_frame)
3909 framesize = buf->count;
3910
3911 if (debug_level >= DEBUG_LEVEL_BH)
3912 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3913 __FILE__,__LINE__,info->device_name,status,framesize);
Jeff Garzikd12341f2007-10-19 15:45:35 -04003914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 if (debug_level >= DEBUG_LEVEL_DATA)
Jeff Garzikd12341f2007-10-19 15:45:35 -04003916 trace_block(info, buf->data, framesize, 0);
3917
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 if (framesize) {
3919 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3920 framesize+1 > info->max_frame_size) ||
3921 framesize > info->max_frame_size)
3922 info->icount.rxlong++;
3923 else {
3924 if (status & BIT5)
3925 info->icount.rxok++;
3926
3927 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3928 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3929 ++framesize;
3930 }
3931
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003932#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 if (info->netcount)
3934 hdlcdev_rx(info, buf->data, framesize);
3935 else
3936#endif
3937 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3938 }
3939 }
3940
3941 spin_lock_irqsave(&info->lock,flags);
3942 buf->status = buf->count = 0;
3943 info->rx_frame_count--;
3944 info->rx_get++;
3945 if (info->rx_get >= info->rx_buf_count)
3946 info->rx_get = 0;
3947 spin_unlock_irqrestore(&info->lock,flags);
3948
Joe Perches0fab6de2008-04-28 02:14:02 -07003949 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
Joe Perches0fab6de2008-04-28 02:14:02 -07003952static bool register_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953{
Jeff Garzikd12341f2007-10-19 15:45:35 -04003954 static unsigned char patterns[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
Tobias Klauserfe971072006-01-09 20:54:02 -08003956 static unsigned int count = ARRAY_SIZE(patterns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 unsigned int i;
Joe Perches0fab6de2008-04-28 02:14:02 -07003958 bool rc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 unsigned long flags;
3960
3961 spin_lock_irqsave(&info->lock,flags);
3962 reset_device(info);
3963
3964 for (i = 0; i < count; i++) {
3965 write_reg(info, XAD1, patterns[i]);
3966 write_reg(info, XAD2, patterns[(i + 1) % count]);
Tobias Klauserfe971072006-01-09 20:54:02 -08003967 if ((read_reg(info, XAD1) != patterns[i]) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003969 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 break;
3971 }
3972 }
3973
3974 spin_unlock_irqrestore(&info->lock,flags);
3975 return rc;
3976}
3977
Joe Perches0fab6de2008-04-28 02:14:02 -07003978static bool irq_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
3980 unsigned long end_time;
3981 unsigned long flags;
3982
3983 spin_lock_irqsave(&info->lock,flags);
3984 reset_device(info);
3985
Joe Perches0fab6de2008-04-28 02:14:02 -07003986 info->testing_irq = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 hdlc_mode(info);
3988
Joe Perches0fab6de2008-04-28 02:14:02 -07003989 info->irq_occurred = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
3991 /* init hdlc mode */
3992
3993 irq_enable(info, CHA, IRQ_TIMER);
3994 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
3995 issue_command(info, CHA, CMD_START_TIMER);
3996
3997 spin_unlock_irqrestore(&info->lock,flags);
3998
3999 end_time=100;
4000 while(end_time-- && !info->irq_occurred) {
4001 msleep_interruptible(10);
4002 }
Jeff Garzikd12341f2007-10-19 15:45:35 -04004003
Joe Perches0fab6de2008-04-28 02:14:02 -07004004 info->testing_irq = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
4006 spin_lock_irqsave(&info->lock,flags);
4007 reset_device(info);
4008 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04004009
Joe Perches0fab6de2008-04-28 02:14:02 -07004010 return info->irq_occurred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
4012
Peter Hagervallcdaad342006-06-23 02:06:04 -07004013static int adapter_test(MGSLPC_INFO *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
4015 if (!register_test(info)) {
4016 info->init_error = DiagStatus_AddressFailure;
4017 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
4018 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
4019 return -ENODEV;
4020 }
4021
4022 if (!irq_test(info)) {
4023 info->init_error = DiagStatus_IrqFailure;
4024 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
4025 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
4026 return -ENODEV;
4027 }
4028
4029 if (debug_level >= DEBUG_LEVEL_INFO)
4030 printk("%s(%d):device %s passed diagnostics\n",
4031 __FILE__,__LINE__,info->device_name);
4032 return 0;
4033}
4034
Peter Hagervallcdaad342006-06-23 02:06:04 -07004035static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036{
4037 int i;
4038 int linecount;
4039 if (xmit)
4040 printk("%s tx data:\n",info->device_name);
4041 else
4042 printk("%s rx data:\n",info->device_name);
Jeff Garzikd12341f2007-10-19 15:45:35 -04004043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 while(count) {
4045 if (count > 16)
4046 linecount = 16;
4047 else
4048 linecount = count;
Jeff Garzikd12341f2007-10-19 15:45:35 -04004049
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 for(i=0;i<linecount;i++)
4051 printk("%02X ",(unsigned char)data[i]);
4052 for(;i<17;i++)
4053 printk(" ");
4054 for(i=0;i<linecount;i++) {
4055 if (data[i]>=040 && data[i]<=0176)
4056 printk("%c",data[i]);
4057 else
4058 printk(".");
4059 }
4060 printk("\n");
Jeff Garzikd12341f2007-10-19 15:45:35 -04004061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 data += linecount;
4063 count -= linecount;
4064 }
4065}
4066
4067/* HDLC frame time out
4068 * update stats and do tx completion processing
4069 */
Peter Hagervallcdaad342006-06-23 02:06:04 -07004070static void tx_timeout(unsigned long context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
4072 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
4073 unsigned long flags;
Jeff Garzikd12341f2007-10-19 15:45:35 -04004074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 if ( debug_level >= DEBUG_LEVEL_INFO )
4076 printk( "%s(%d):tx_timeout(%s)\n",
4077 __FILE__,__LINE__,info->device_name);
4078 if(info->tx_active &&
4079 info->params.mode == MGSL_MODE_HDLC) {
4080 info->icount.txtimeout++;
4081 }
4082 spin_lock_irqsave(&info->lock,flags);
Joe Perches0fab6de2008-04-28 02:14:02 -07004083 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 info->tx_count = info->tx_put = info->tx_get = 0;
4085
4086 spin_unlock_irqrestore(&info->lock,flags);
Jeff Garzikd12341f2007-10-19 15:45:35 -04004087
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08004088#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 if (info->netcount)
4090 hdlcdev_tx_done(info);
4091 else
4092#endif
4093 bh_transmit(info);
4094}
4095
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08004096#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
4098/**
4099 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
4100 * set encoding and frame check sequence (FCS) options
4101 *
4102 * dev pointer to network device structure
4103 * encoding serial encoding setting
4104 * parity FCS setting
4105 *
4106 * returns 0 if success, otherwise error code
4107 */
4108static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
4109 unsigned short parity)
4110{
4111 MGSLPC_INFO *info = dev_to_port(dev);
4112 unsigned char new_encoding;
4113 unsigned short new_crctype;
4114
4115 /* return error if TTY interface open */
4116 if (info->count)
4117 return -EBUSY;
4118
4119 switch (encoding)
4120 {
4121 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
4122 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
4123 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
4124 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
4125 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
4126 default: return -EINVAL;
4127 }
4128
4129 switch (parity)
4130 {
4131 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
4132 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
4133 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
4134 default: return -EINVAL;
4135 }
4136
4137 info->params.encoding = new_encoding;
Alexey Dobriyan53b35312006-03-24 03:16:13 -08004138 info->params.crc_type = new_crctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
4140 /* if network interface up, reprogram hardware */
4141 if (info->netcount)
4142 mgslpc_program_hw(info);
4143
4144 return 0;
4145}
4146
4147/**
4148 * called by generic HDLC layer to send frame
4149 *
4150 * skb socket buffer containing HDLC frame
4151 * dev pointer to network device structure
4152 *
4153 * returns 0 if success, otherwise error code
4154 */
4155static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
4156{
4157 MGSLPC_INFO *info = dev_to_port(dev);
4158 struct net_device_stats *stats = hdlc_stats(dev);
4159 unsigned long flags;
4160
4161 if (debug_level >= DEBUG_LEVEL_INFO)
4162 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
4163
4164 /* stop sending until this frame completes */
4165 netif_stop_queue(dev);
4166
4167 /* copy data to device buffers */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004168 skb_copy_from_linear_data(skb, info->tx_buf, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 info->tx_get = 0;
4170 info->tx_put = info->tx_count = skb->len;
4171
4172 /* update network statistics */
4173 stats->tx_packets++;
4174 stats->tx_bytes += skb->len;
4175
4176 /* done with socket buffer, so free it */
4177 dev_kfree_skb(skb);
4178
4179 /* save start time for transmit timeout detection */
4180 dev->trans_start = jiffies;
4181
4182 /* start hardware transmitter if necessary */
4183 spin_lock_irqsave(&info->lock,flags);
4184 if (!info->tx_active)
4185 tx_start(info);
4186 spin_unlock_irqrestore(&info->lock,flags);
4187
4188 return 0;
4189}
4190
4191/**
4192 * called by network layer when interface enabled
4193 * claim resources and initialize hardware
4194 *
4195 * dev pointer to network device structure
4196 *
4197 * returns 0 if success, otherwise error code
4198 */
4199static int hdlcdev_open(struct net_device *dev)
4200{
4201 MGSLPC_INFO *info = dev_to_port(dev);
4202 int rc;
4203 unsigned long flags;
4204
4205 if (debug_level >= DEBUG_LEVEL_INFO)
4206 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4207
4208 /* generic HDLC layer open processing */
4209 if ((rc = hdlc_open(dev)))
4210 return rc;
4211
4212 /* arbitrate between network and tty opens */
4213 spin_lock_irqsave(&info->netlock, flags);
4214 if (info->count != 0 || info->netcount != 0) {
4215 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4216 spin_unlock_irqrestore(&info->netlock, flags);
4217 return -EBUSY;
4218 }
4219 info->netcount=1;
4220 spin_unlock_irqrestore(&info->netlock, flags);
4221
4222 /* claim resources and init adapter */
4223 if ((rc = startup(info)) != 0) {
4224 spin_lock_irqsave(&info->netlock, flags);
4225 info->netcount=0;
4226 spin_unlock_irqrestore(&info->netlock, flags);
4227 return rc;
4228 }
4229
4230 /* assert DTR and RTS, apply hardware settings */
4231 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4232 mgslpc_program_hw(info);
4233
4234 /* enable network layer transmit */
4235 dev->trans_start = jiffies;
4236 netif_start_queue(dev);
4237
4238 /* inform generic HDLC layer of current DCD status */
4239 spin_lock_irqsave(&info->lock, flags);
4240 get_signals(info);
4241 spin_unlock_irqrestore(&info->lock, flags);
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07004242 if (info->serial_signals & SerialSignal_DCD)
4243 netif_carrier_on(dev);
4244 else
4245 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 return 0;
4247}
4248
4249/**
4250 * called by network layer when interface is disabled
4251 * shutdown hardware and release resources
4252 *
4253 * dev pointer to network device structure
4254 *
4255 * returns 0 if success, otherwise error code
4256 */
4257static int hdlcdev_close(struct net_device *dev)
4258{
4259 MGSLPC_INFO *info = dev_to_port(dev);
4260 unsigned long flags;
4261
4262 if (debug_level >= DEBUG_LEVEL_INFO)
4263 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4264
4265 netif_stop_queue(dev);
4266
4267 /* shutdown adapter and release resources */
4268 shutdown(info);
4269
4270 hdlc_close(dev);
4271
4272 spin_lock_irqsave(&info->netlock, flags);
4273 info->netcount=0;
4274 spin_unlock_irqrestore(&info->netlock, flags);
4275
4276 return 0;
4277}
4278
4279/**
4280 * called by network layer to process IOCTL call to network device
4281 *
4282 * dev pointer to network device structure
4283 * ifr pointer to network interface request structure
4284 * cmd IOCTL command code
4285 *
4286 * returns 0 if success, otherwise error code
4287 */
4288static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4289{
4290 const size_t size = sizeof(sync_serial_settings);
4291 sync_serial_settings new_line;
4292 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4293 MGSLPC_INFO *info = dev_to_port(dev);
4294 unsigned int flags;
4295
4296 if (debug_level >= DEBUG_LEVEL_INFO)
4297 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4298
4299 /* return error if TTY interface open */
4300 if (info->count)
4301 return -EBUSY;
4302
4303 if (cmd != SIOCWANDEV)
4304 return hdlc_ioctl(dev, ifr, cmd);
4305
4306 switch(ifr->ifr_settings.type) {
4307 case IF_GET_IFACE: /* return current sync_serial_settings */
4308
4309 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4310 if (ifr->ifr_settings.size < size) {
4311 ifr->ifr_settings.size = size; /* data size wanted */
4312 return -ENOBUFS;
4313 }
4314
4315 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4316 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4317 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4318 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4319
4320 switch (flags){
4321 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4322 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4323 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4324 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4325 default: new_line.clock_type = CLOCK_DEFAULT;
4326 }
4327
4328 new_line.clock_rate = info->params.clock_speed;
4329 new_line.loopback = info->params.loopback ? 1:0;
4330
4331 if (copy_to_user(line, &new_line, size))
4332 return -EFAULT;
4333 return 0;
4334
4335 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4336
4337 if(!capable(CAP_NET_ADMIN))
4338 return -EPERM;
4339 if (copy_from_user(&new_line, line, size))
4340 return -EFAULT;
4341
4342 switch (new_line.clock_type)
4343 {
4344 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4345 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4346 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4347 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4348 case CLOCK_DEFAULT: flags = info->params.flags &
4349 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4350 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4351 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4352 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4353 default: return -EINVAL;
4354 }
4355
4356 if (new_line.loopback != 0 && new_line.loopback != 1)
4357 return -EINVAL;
4358
4359 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4360 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4361 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4362 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4363 info->params.flags |= flags;
4364
4365 info->params.loopback = new_line.loopback;
4366
4367 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4368 info->params.clock_speed = new_line.clock_rate;
4369 else
4370 info->params.clock_speed = 0;
4371
4372 /* if network interface up, reprogram hardware */
4373 if (info->netcount)
4374 mgslpc_program_hw(info);
4375 return 0;
4376
4377 default:
4378 return hdlc_ioctl(dev, ifr, cmd);
4379 }
4380}
4381
4382/**
4383 * called by network layer when transmit timeout is detected
4384 *
4385 * dev pointer to network device structure
4386 */
4387static void hdlcdev_tx_timeout(struct net_device *dev)
4388{
4389 MGSLPC_INFO *info = dev_to_port(dev);
4390 struct net_device_stats *stats = hdlc_stats(dev);
4391 unsigned long flags;
4392
4393 if (debug_level >= DEBUG_LEVEL_INFO)
4394 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4395
4396 stats->tx_errors++;
4397 stats->tx_aborted_errors++;
4398
4399 spin_lock_irqsave(&info->lock,flags);
4400 tx_stop(info);
4401 spin_unlock_irqrestore(&info->lock,flags);
4402
4403 netif_wake_queue(dev);
4404}
4405
4406/**
4407 * called by device driver when transmit completes
4408 * reenable network layer transmit if stopped
4409 *
4410 * info pointer to device instance information
4411 */
4412static void hdlcdev_tx_done(MGSLPC_INFO *info)
4413{
4414 if (netif_queue_stopped(info->netdev))
4415 netif_wake_queue(info->netdev);
4416}
4417
4418/**
4419 * called by device driver when frame received
4420 * pass frame to network layer
4421 *
4422 * info pointer to device instance information
4423 * buf pointer to buffer contianing frame data
4424 * size count of data bytes in buf
4425 */
4426static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4427{
4428 struct sk_buff *skb = dev_alloc_skb(size);
4429 struct net_device *dev = info->netdev;
4430 struct net_device_stats *stats = hdlc_stats(dev);
4431
4432 if (debug_level >= DEBUG_LEVEL_INFO)
4433 printk("hdlcdev_rx(%s)\n",dev->name);
4434
4435 if (skb == NULL) {
4436 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
4437 stats->rx_dropped++;
4438 return;
4439 }
4440
4441 memcpy(skb_put(skb, size),buf,size);
4442
4443 skb->protocol = hdlc_type_trans(skb, info->netdev);
4444
4445 stats->rx_packets++;
4446 stats->rx_bytes += size;
4447
4448 netif_rx(skb);
4449
4450 info->netdev->last_rx = jiffies;
4451}
4452
4453/**
4454 * called by device driver when adding device instance
4455 * do generic HDLC initialization
4456 *
4457 * info pointer to device instance information
4458 *
4459 * returns 0 if success, otherwise error code
4460 */
4461static int hdlcdev_init(MGSLPC_INFO *info)
4462{
4463 int rc;
4464 struct net_device *dev;
4465 hdlc_device *hdlc;
4466
4467 /* allocate and initialize network and HDLC layer objects */
4468
4469 if (!(dev = alloc_hdlcdev(info))) {
4470 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4471 return -ENOMEM;
4472 }
4473
4474 /* for network layer reporting purposes only */
4475 dev->base_addr = info->io_base;
4476 dev->irq = info->irq_level;
4477
4478 /* network layer callbacks and settings */
4479 dev->do_ioctl = hdlcdev_ioctl;
4480 dev->open = hdlcdev_open;
4481 dev->stop = hdlcdev_close;
4482 dev->tx_timeout = hdlcdev_tx_timeout;
4483 dev->watchdog_timeo = 10*HZ;
4484 dev->tx_queue_len = 50;
4485
4486 /* generic HDLC layer callbacks and settings */
4487 hdlc = dev_to_hdlc(dev);
4488 hdlc->attach = hdlcdev_attach;
4489 hdlc->xmit = hdlcdev_xmit;
4490
4491 /* register objects with HDLC layer */
4492 if ((rc = register_hdlc_device(dev))) {
4493 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4494 free_netdev(dev);
4495 return rc;
4496 }
4497
4498 info->netdev = dev;
4499 return 0;
4500}
4501
4502/**
4503 * called by device driver when removing device instance
4504 * do generic HDLC cleanup
4505 *
4506 * info pointer to device instance information
4507 */
4508static void hdlcdev_exit(MGSLPC_INFO *info)
4509{
4510 unregister_hdlc_device(info->netdev);
4511 free_netdev(info->netdev);
4512 info->netdev = NULL;
4513}
4514
4515#endif /* CONFIG_HDLC */
4516