blob: 5ffdb364ea4093cec29ff306779ebdc77ff11ab4 [file] [log] [blame]
Paul Fulghum705b6c72006-01-08 01:02:06 -08001/*
Paul Fulghumbb029c62007-07-31 00:37:35 -07002 * $Id: synclink_gt.c,v 4.50 2007/07/25 19:29:25 paulkf Exp $
Paul Fulghum705b6c72006-01-08 01:02:06 -08003 *
4 * Device driver for Microgate SyncLink GT serial adapters.
5 *
6 * written by Paul Fulghum for Microgate Corporation
7 * paulkf@microgate.com
8 *
9 * Microgate and SyncLink are trademarks of Microgate Corporation
10 *
11 * This code is released under the GNU General Public License (GPL)
12 *
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
17 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26/*
27 * DEBUG OUTPUT DEFINITIONS
28 *
29 * uncomment lines below to enable specific types of debug output
30 *
31 * DBGINFO information - most verbose output
32 * DBGERR serious errors
33 * DBGBH bottom half service routine debugging
34 * DBGISR interrupt service routine debugging
35 * DBGDATA output receive and transmit data
36 * DBGTBUF output transmit DMA buffers and registers
37 * DBGRBUF output receive DMA buffers and registers
38 */
39
40#define DBGINFO(fmt) if (debug_level >= DEBUG_LEVEL_INFO) printk fmt
41#define DBGERR(fmt) if (debug_level >= DEBUG_LEVEL_ERROR) printk fmt
42#define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt
43#define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt
44#define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label))
45//#define DBGTBUF(info) dump_tbufs(info)
46//#define DBGRBUF(info) dump_rbufs(info)
47
48
Paul Fulghum705b6c72006-01-08 01:02:06 -080049#include <linux/module.h>
50#include <linux/version.h>
51#include <linux/errno.h>
52#include <linux/signal.h>
53#include <linux/sched.h>
54#include <linux/timer.h>
55#include <linux/interrupt.h>
56#include <linux/pci.h>
57#include <linux/tty.h>
58#include <linux/tty_flip.h>
59#include <linux/serial.h>
60#include <linux/major.h>
61#include <linux/string.h>
62#include <linux/fcntl.h>
63#include <linux/ptrace.h>
64#include <linux/ioport.h>
65#include <linux/mm.h>
66#include <linux/slab.h>
67#include <linux/netdevice.h>
68#include <linux/vmalloc.h>
69#include <linux/init.h>
70#include <linux/delay.h>
71#include <linux/ioctl.h>
72#include <linux/termios.h>
73#include <linux/bitops.h>
74#include <linux/workqueue.h>
75#include <linux/hdlc.h>
Robert P. J. Day3dd12472008-02-06 01:37:17 -080076#include <linux/synclink.h>
Paul Fulghum705b6c72006-01-08 01:02:06 -080077
Paul Fulghum705b6c72006-01-08 01:02:06 -080078#include <asm/system.h>
79#include <asm/io.h>
80#include <asm/irq.h>
81#include <asm/dma.h>
82#include <asm/types.h>
83#include <asm/uaccess.h>
84
Paul Fulghumaf69c7f2006-12-06 20:40:24 -080085#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_GT_MODULE))
86#define SYNCLINK_GENERIC_HDLC 1
87#else
88#define SYNCLINK_GENERIC_HDLC 0
Paul Fulghum705b6c72006-01-08 01:02:06 -080089#endif
90
91/*
92 * module identification
93 */
94static char *driver_name = "SyncLink GT";
Paul Fulghumbb029c62007-07-31 00:37:35 -070095static char *driver_version = "$Revision: 4.50 $";
Paul Fulghum705b6c72006-01-08 01:02:06 -080096static char *tty_driver_name = "synclink_gt";
97static char *tty_dev_prefix = "ttySLG";
98MODULE_LICENSE("GPL");
99#define MGSL_MAGIC 0x5401
Paul Fulghuma077c1a2006-09-30 23:27:46 -0700100#define MAX_DEVICES 32
Paul Fulghum705b6c72006-01-08 01:02:06 -0800101
102static struct pci_device_id pci_table[] = {
103 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
Paul Fulghum6f84be82006-06-25 05:49:22 -0700104 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT2_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
Paul Fulghum705b6c72006-01-08 01:02:06 -0800105 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
106 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
107 {0,}, /* terminate list */
108};
109MODULE_DEVICE_TABLE(pci, pci_table);
110
111static int init_one(struct pci_dev *dev,const struct pci_device_id *ent);
112static void remove_one(struct pci_dev *dev);
113static struct pci_driver pci_driver = {
114 .name = "synclink_gt",
115 .id_table = pci_table,
116 .probe = init_one,
117 .remove = __devexit_p(remove_one),
118};
119
Joe Perches0fab6de2008-04-28 02:14:02 -0700120static bool pci_registered;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800121
122/*
123 * module configuration and status
124 */
125static struct slgt_info *slgt_device_list;
126static int slgt_device_count;
127
128static int ttymajor;
129static int debug_level;
130static int maxframe[MAX_DEVICES];
131static int dosyncppp[MAX_DEVICES];
132
133module_param(ttymajor, int, 0);
134module_param(debug_level, int, 0);
135module_param_array(maxframe, int, NULL, 0);
136module_param_array(dosyncppp, int, NULL, 0);
137
138MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
139MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
140MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
141MODULE_PARM_DESC(dosyncppp, "Enable synchronous net device, 0=disable 1=enable");
142
143/*
144 * tty support and callbacks
145 */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800146static struct tty_driver *serial_driver;
147
148static int open(struct tty_struct *tty, struct file * filp);
149static void close(struct tty_struct *tty, struct file * filp);
150static void hangup(struct tty_struct *tty);
Alan Cox606d0992006-12-08 02:38:45 -0800151static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800152
153static int write(struct tty_struct *tty, const unsigned char *buf, int count);
Alan Cox55da7782008-04-30 00:54:07 -0700154static int put_char(struct tty_struct *tty, unsigned char ch);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800155static void send_xchar(struct tty_struct *tty, char ch);
156static void wait_until_sent(struct tty_struct *tty, int timeout);
157static int write_room(struct tty_struct *tty);
158static void flush_chars(struct tty_struct *tty);
159static void flush_buffer(struct tty_struct *tty);
160static void tx_hold(struct tty_struct *tty);
161static void tx_release(struct tty_struct *tty);
162
163static int ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
164static int read_proc(char *page, char **start, off_t off, int count,int *eof, void *data);
165static int chars_in_buffer(struct tty_struct *tty);
166static void throttle(struct tty_struct * tty);
167static void unthrottle(struct tty_struct * tty);
Alan Cox9e989662008-07-22 11:18:03 +0100168static int set_break(struct tty_struct *tty, int break_state);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800169
170/*
171 * generic HDLC support and callbacks
172 */
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800173#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -0800174#define dev_to_port(D) (dev_to_hdlc(D)->priv)
175static void hdlcdev_tx_done(struct slgt_info *info);
176static void hdlcdev_rx(struct slgt_info *info, char *buf, int size);
177static int hdlcdev_init(struct slgt_info *info);
178static void hdlcdev_exit(struct slgt_info *info);
179#endif
180
181
182/*
183 * device specific structures, macros and functions
184 */
185
186#define SLGT_MAX_PORTS 4
187#define SLGT_REG_SIZE 256
188
189/*
Paul Fulghum0080b7a2006-03-28 01:56:15 -0800190 * conditional wait facility
191 */
192struct cond_wait {
193 struct cond_wait *next;
194 wait_queue_head_t q;
195 wait_queue_t wait;
196 unsigned int data;
197};
198static void init_cond_wait(struct cond_wait *w, unsigned int data);
199static void add_cond_wait(struct cond_wait **head, struct cond_wait *w);
200static void remove_cond_wait(struct cond_wait **head, struct cond_wait *w);
201static void flush_cond_wait(struct cond_wait **head);
202
203/*
Paul Fulghum705b6c72006-01-08 01:02:06 -0800204 * DMA buffer descriptor and access macros
205 */
206struct slgt_desc
207{
Al Viro51ef9c52007-10-14 19:34:30 +0100208 __le16 count;
209 __le16 status;
210 __le32 pbuf; /* physical address of data buffer */
211 __le32 next; /* physical address of next descriptor */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800212
213 /* driver book keeping */
214 char *buf; /* virtual address of data buffer */
215 unsigned int pdesc; /* physical address of this descriptor */
216 dma_addr_t buf_dma_addr;
Paul Fulghum403214d2008-07-22 11:21:55 +0100217 unsigned short buf_count;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800218};
219
220#define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
221#define set_desc_next(a,b) (a).next = cpu_to_le32((unsigned int)(b))
222#define set_desc_count(a,b)(a).count = cpu_to_le16((unsigned short)(b))
223#define set_desc_eof(a,b) (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
224#define desc_count(a) (le16_to_cpu((a).count))
225#define desc_status(a) (le16_to_cpu((a).status))
226#define desc_complete(a) (le16_to_cpu((a).status) & BIT15)
227#define desc_eof(a) (le16_to_cpu((a).status) & BIT2)
228#define desc_crc_error(a) (le16_to_cpu((a).status) & BIT1)
229#define desc_abort(a) (le16_to_cpu((a).status) & BIT0)
230#define desc_residue(a) ((le16_to_cpu((a).status) & 0x38) >> 3)
231
232struct _input_signal_events {
233 int ri_up;
234 int ri_down;
235 int dsr_up;
236 int dsr_down;
237 int dcd_up;
238 int dcd_down;
239 int cts_up;
240 int cts_down;
241};
242
243/*
244 * device instance data structure
245 */
246struct slgt_info {
247 void *if_ptr; /* General purpose pointer (used by SPPP) */
Alan Cox8fb06c72008-07-16 21:56:46 +0100248 struct tty_port port;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800249
250 struct slgt_info *next_device; /* device list link */
251
252 int magic;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800253
254 char device_name[25];
255 struct pci_dev *pdev;
256
257 int port_count; /* count of ports on adapter */
258 int adapter_num; /* adapter instance number */
259 int port_num; /* port instance number */
260
261 /* array of pointers to port contexts on this adapter */
262 struct slgt_info *port_array[SLGT_MAX_PORTS];
263
Paul Fulghum705b6c72006-01-08 01:02:06 -0800264 int line; /* tty line instance number */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800265
266 struct mgsl_icount icount;
267
Paul Fulghum705b6c72006-01-08 01:02:06 -0800268 int timeout;
269 int x_char; /* xon/xoff character */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800270 unsigned int read_status_mask;
271 unsigned int ignore_status_mask;
272
Paul Fulghum705b6c72006-01-08 01:02:06 -0800273 wait_queue_head_t status_event_wait_q;
274 wait_queue_head_t event_wait_q;
275 struct timer_list tx_timer;
276 struct timer_list rx_timer;
277
Paul Fulghum0080b7a2006-03-28 01:56:15 -0800278 unsigned int gpio_present;
279 struct cond_wait *gpio_wait_q;
280
Paul Fulghum705b6c72006-01-08 01:02:06 -0800281 spinlock_t lock; /* spinlock for synchronizing with ISR */
282
283 struct work_struct task;
284 u32 pending_bh;
Joe Perches0fab6de2008-04-28 02:14:02 -0700285 bool bh_requested;
286 bool bh_running;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800287
288 int isr_overflow;
Joe Perches0fab6de2008-04-28 02:14:02 -0700289 bool irq_requested; /* true if IRQ requested */
290 bool irq_occurred; /* for diagnostics use */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800291
292 /* device configuration */
293
294 unsigned int bus_type;
295 unsigned int irq_level;
296 unsigned long irq_flags;
297
298 unsigned char __iomem * reg_addr; /* memory mapped registers address */
299 u32 phys_reg_addr;
Joe Perches0fab6de2008-04-28 02:14:02 -0700300 bool reg_addr_requested;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800301
302 MGSL_PARAMS params; /* communications parameters */
303 u32 idle_mode;
304 u32 max_frame_size; /* as set by device config */
305
Paul Fulghum814dae02008-07-22 11:22:14 +0100306 unsigned int rbuf_fill_level;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800307 unsigned int if_mode;
308
309 /* device status */
310
Joe Perches0fab6de2008-04-28 02:14:02 -0700311 bool rx_enabled;
312 bool rx_restart;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800313
Joe Perches0fab6de2008-04-28 02:14:02 -0700314 bool tx_enabled;
315 bool tx_active;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800316
317 unsigned char signals; /* serial signal states */
Darren Jenkins2641dfd2006-02-28 16:59:20 -0800318 int init_error; /* initialization error */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800319
320 unsigned char *tx_buf;
321 int tx_count;
322
323 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
324 char char_buf[MAX_ASYNC_BUFFER_SIZE];
Joe Perches0fab6de2008-04-28 02:14:02 -0700325 bool drop_rts_on_tx_done;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800326 struct _input_signal_events input_signal_events;
327
328 int dcd_chkcount; /* check counts to prevent */
329 int cts_chkcount; /* too many IRQs if a signal */
330 int dsr_chkcount; /* is floating */
331 int ri_chkcount;
332
333 char *bufs; /* virtual address of DMA buffer lists */
334 dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
335
336 unsigned int rbuf_count;
337 struct slgt_desc *rbufs;
338 unsigned int rbuf_current;
339 unsigned int rbuf_index;
340
341 unsigned int tbuf_count;
342 struct slgt_desc *tbufs;
343 unsigned int tbuf_current;
344 unsigned int tbuf_start;
345
346 unsigned char *tmp_rbuf;
347 unsigned int tmp_rbuf_count;
348
349 /* SPPP/Cisco HDLC device parts */
350
351 int netcount;
352 int dosyncppp;
353 spinlock_t netlock;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800354#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -0800355 struct net_device *netdev;
356#endif
357
358};
359
360static MGSL_PARAMS default_params = {
361 .mode = MGSL_MODE_HDLC,
362 .loopback = 0,
363 .flags = HDLC_FLAG_UNDERRUN_ABORT15,
364 .encoding = HDLC_ENCODING_NRZI_SPACE,
365 .clock_speed = 0,
366 .addr_filter = 0xff,
367 .crc_type = HDLC_CRC_16_CCITT,
368 .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
369 .preamble = HDLC_PREAMBLE_PATTERN_NONE,
370 .data_rate = 9600,
371 .data_bits = 8,
372 .stop_bits = 1,
373 .parity = ASYNC_PARITY_NONE
374};
375
376
377#define BH_RECEIVE 1
378#define BH_TRANSMIT 2
379#define BH_STATUS 4
380#define IO_PIN_SHUTDOWN_LIMIT 100
381
382#define DMABUFSIZE 256
383#define DESC_LIST_SIZE 4096
384
385#define MASK_PARITY BIT1
Paul Fulghum202af6d2006-08-31 21:27:36 -0700386#define MASK_FRAMING BIT0
387#define MASK_BREAK BIT14
Paul Fulghum705b6c72006-01-08 01:02:06 -0800388#define MASK_OVERRUN BIT4
389
390#define GSR 0x00 /* global status */
Paul Fulghum0080b7a2006-03-28 01:56:15 -0800391#define JCR 0x04 /* JTAG control */
392#define IODR 0x08 /* GPIO direction */
393#define IOER 0x0c /* GPIO interrupt enable */
394#define IOVR 0x10 /* GPIO value */
395#define IOSR 0x14 /* GPIO interrupt status */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800396#define TDR 0x80 /* tx data */
397#define RDR 0x80 /* rx data */
398#define TCR 0x82 /* tx control */
399#define TIR 0x84 /* tx idle */
400#define TPR 0x85 /* tx preamble */
401#define RCR 0x86 /* rx control */
402#define VCR 0x88 /* V.24 control */
403#define CCR 0x89 /* clock control */
404#define BDR 0x8a /* baud divisor */
405#define SCR 0x8c /* serial control */
406#define SSR 0x8e /* serial status */
407#define RDCSR 0x90 /* rx DMA control/status */
408#define TDCSR 0x94 /* tx DMA control/status */
409#define RDDAR 0x98 /* rx DMA descriptor address */
410#define TDDAR 0x9c /* tx DMA descriptor address */
411
412#define RXIDLE BIT14
413#define RXBREAK BIT14
414#define IRQ_TXDATA BIT13
415#define IRQ_TXIDLE BIT12
416#define IRQ_TXUNDER BIT11 /* HDLC */
417#define IRQ_RXDATA BIT10
418#define IRQ_RXIDLE BIT9 /* HDLC */
419#define IRQ_RXBREAK BIT9 /* async */
420#define IRQ_RXOVER BIT8
421#define IRQ_DSR BIT7
422#define IRQ_CTS BIT6
423#define IRQ_DCD BIT5
424#define IRQ_RI BIT4
425#define IRQ_ALL 0x3ff0
426#define IRQ_MASTER BIT0
427
428#define slgt_irq_on(info, mask) \
429 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
430#define slgt_irq_off(info, mask) \
431 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
432
433static __u8 rd_reg8(struct slgt_info *info, unsigned int addr);
434static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
435static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
436static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
437static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
438static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
439
440static void msc_set_vcr(struct slgt_info *info);
441
442static int startup(struct slgt_info *info);
443static int block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
444static void shutdown(struct slgt_info *info);
445static void program_hw(struct slgt_info *info);
446static void change_params(struct slgt_info *info);
447
448static int register_test(struct slgt_info *info);
449static int irq_test(struct slgt_info *info);
450static int loopback_test(struct slgt_info *info);
451static int adapter_test(struct slgt_info *info);
452
453static void reset_adapter(struct slgt_info *info);
454static void reset_port(struct slgt_info *info);
455static void async_mode(struct slgt_info *info);
Paul Fulghumcb10dc92006-09-30 23:27:45 -0700456static void sync_mode(struct slgt_info *info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800457
458static void rx_stop(struct slgt_info *info);
459static void rx_start(struct slgt_info *info);
460static void reset_rbufs(struct slgt_info *info);
461static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
462static void rdma_reset(struct slgt_info *info);
Joe Perches0fab6de2008-04-28 02:14:02 -0700463static bool rx_get_frame(struct slgt_info *info);
464static bool rx_get_buf(struct slgt_info *info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800465
466static void tx_start(struct slgt_info *info);
467static void tx_stop(struct slgt_info *info);
468static void tx_set_idle(struct slgt_info *info);
469static unsigned int free_tbuf_count(struct slgt_info *info);
Paul Fulghum403214d2008-07-22 11:21:55 +0100470static unsigned int tbuf_bytes(struct slgt_info *info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800471static void reset_tbufs(struct slgt_info *info);
472static void tdma_reset(struct slgt_info *info);
Paul Fulghumbb029c62007-07-31 00:37:35 -0700473static void tdma_start(struct slgt_info *info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800474static void tx_load(struct slgt_info *info, const char *buf, unsigned int count);
475
476static void get_signals(struct slgt_info *info);
477static void set_signals(struct slgt_info *info);
478static void enable_loopback(struct slgt_info *info);
479static void set_rate(struct slgt_info *info, u32 data_rate);
480
481static int bh_action(struct slgt_info *info);
David Howellsc4028952006-11-22 14:57:56 +0000482static void bh_handler(struct work_struct *work);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800483static void bh_transmit(struct slgt_info *info);
484static void isr_serial(struct slgt_info *info);
485static void isr_rdma(struct slgt_info *info);
486static void isr_txeom(struct slgt_info *info, unsigned short status);
487static void isr_tdma(struct slgt_info *info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800488
489static int alloc_dma_bufs(struct slgt_info *info);
490static void free_dma_bufs(struct slgt_info *info);
491static int alloc_desc(struct slgt_info *info);
492static void free_desc(struct slgt_info *info);
493static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
494static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
495
496static int alloc_tmp_rbuf(struct slgt_info *info);
497static void free_tmp_rbuf(struct slgt_info *info);
498
499static void tx_timeout(unsigned long context);
500static void rx_timeout(unsigned long context);
501
502/*
503 * ioctl handlers
504 */
505static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount);
506static int get_params(struct slgt_info *info, MGSL_PARAMS __user *params);
507static int set_params(struct slgt_info *info, MGSL_PARAMS __user *params);
508static int get_txidle(struct slgt_info *info, int __user *idle_mode);
509static int set_txidle(struct slgt_info *info, int idle_mode);
510static int tx_enable(struct slgt_info *info, int enable);
511static int tx_abort(struct slgt_info *info);
512static int rx_enable(struct slgt_info *info, int enable);
513static int modem_input_wait(struct slgt_info *info,int arg);
514static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr);
515static int tiocmget(struct tty_struct *tty, struct file *file);
516static int tiocmset(struct tty_struct *tty, struct file *file,
517 unsigned int set, unsigned int clear);
Alan Cox9e989662008-07-22 11:18:03 +0100518static int set_break(struct tty_struct *tty, int break_state);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800519static int get_interface(struct slgt_info *info, int __user *if_mode);
520static int set_interface(struct slgt_info *info, int if_mode);
Paul Fulghum0080b7a2006-03-28 01:56:15 -0800521static int set_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
522static int get_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
523static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800524
525/*
526 * driver functions
527 */
528static void add_device(struct slgt_info *info);
529static void device_init(int adapter_num, struct pci_dev *pdev);
530static int claim_resources(struct slgt_info *info);
531static void release_resources(struct slgt_info *info);
532
533/*
534 * DEBUG OUTPUT CODE
535 */
536#ifndef DBGINFO
537#define DBGINFO(fmt)
538#endif
539#ifndef DBGERR
540#define DBGERR(fmt)
541#endif
542#ifndef DBGBH
543#define DBGBH(fmt)
544#endif
545#ifndef DBGISR
546#define DBGISR(fmt)
547#endif
548
549#ifdef DBGDATA
550static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
551{
552 int i;
553 int linecount;
554 printk("%s %s data:\n",info->device_name, label);
555 while(count) {
556 linecount = (count > 16) ? 16 : count;
557 for(i=0; i < linecount; i++)
558 printk("%02X ",(unsigned char)data[i]);
559 for(;i<17;i++)
560 printk(" ");
561 for(i=0;i<linecount;i++) {
562 if (data[i]>=040 && data[i]<=0176)
563 printk("%c",data[i]);
564 else
565 printk(".");
566 }
567 printk("\n");
568 data += linecount;
569 count -= linecount;
570 }
571}
572#else
573#define DBGDATA(info, buf, size, label)
574#endif
575
576#ifdef DBGTBUF
577static void dump_tbufs(struct slgt_info *info)
578{
579 int i;
580 printk("tbuf_current=%d\n", info->tbuf_current);
581 for (i=0 ; i < info->tbuf_count ; i++) {
582 printk("%d: count=%04X status=%04X\n",
583 i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
584 }
585}
586#else
587#define DBGTBUF(info)
588#endif
589
590#ifdef DBGRBUF
591static void dump_rbufs(struct slgt_info *info)
592{
593 int i;
594 printk("rbuf_current=%d\n", info->rbuf_current);
595 for (i=0 ; i < info->rbuf_count ; i++) {
596 printk("%d: count=%04X status=%04X\n",
597 i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
598 }
599}
600#else
601#define DBGRBUF(info)
602#endif
603
604static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
605{
606#ifdef SANITY_CHECK
607 if (!info) {
608 printk("null struct slgt_info for (%s) in %s\n", devname, name);
609 return 1;
610 }
611 if (info->magic != MGSL_MAGIC) {
612 printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
613 return 1;
614 }
615#else
616 if (!info)
617 return 1;
618#endif
619 return 0;
620}
621
622/**
623 * line discipline callback wrappers
624 *
625 * The wrappers maintain line discipline references
626 * while calling into the line discipline.
627 *
628 * ldisc_receive_buf - pass receive data to line discipline
629 */
630static void ldisc_receive_buf(struct tty_struct *tty,
631 const __u8 *data, char *flags, int count)
632{
633 struct tty_ldisc *ld;
634 if (!tty)
635 return;
636 ld = tty_ldisc_ref(tty);
637 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100638 if (ld->ops->receive_buf)
639 ld->ops->receive_buf(tty, data, flags, count);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800640 tty_ldisc_deref(ld);
641 }
642}
643
644/* tty callbacks */
645
646static int open(struct tty_struct *tty, struct file *filp)
647{
648 struct slgt_info *info;
649 int retval, line;
650 unsigned long flags;
651
652 line = tty->index;
653 if ((line < 0) || (line >= slgt_device_count)) {
654 DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
655 return -ENODEV;
656 }
657
658 info = slgt_device_list;
659 while(info && info->line != line)
660 info = info->next_device;
661 if (sanity_check(info, tty->name, "open"))
662 return -ENODEV;
663 if (info->init_error) {
664 DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
665 return -ENODEV;
666 }
667
668 tty->driver_data = info;
Alan Cox8fb06c72008-07-16 21:56:46 +0100669 info->port.tty = tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800670
Alan Cox8fb06c72008-07-16 21:56:46 +0100671 DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->port.count));
Paul Fulghum705b6c72006-01-08 01:02:06 -0800672
673 /* If port is closing, signal caller to try again */
Alan Cox8fb06c72008-07-16 21:56:46 +0100674 if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
675 if (info->port.flags & ASYNC_CLOSING)
676 interruptible_sleep_on(&info->port.close_wait);
677 retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
Paul Fulghum705b6c72006-01-08 01:02:06 -0800678 -EAGAIN : -ERESTARTSYS);
679 goto cleanup;
680 }
681
Alan Cox8fb06c72008-07-16 21:56:46 +0100682 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800683
684 spin_lock_irqsave(&info->netlock, flags);
685 if (info->netcount) {
686 retval = -EBUSY;
687 spin_unlock_irqrestore(&info->netlock, flags);
688 goto cleanup;
689 }
Alan Cox8fb06c72008-07-16 21:56:46 +0100690 info->port.count++;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800691 spin_unlock_irqrestore(&info->netlock, flags);
692
Alan Cox8fb06c72008-07-16 21:56:46 +0100693 if (info->port.count == 1) {
Paul Fulghum705b6c72006-01-08 01:02:06 -0800694 /* 1st open on this device, init hardware */
695 retval = startup(info);
696 if (retval < 0)
697 goto cleanup;
698 }
699
700 retval = block_til_ready(tty, filp, info);
701 if (retval) {
702 DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
703 goto cleanup;
704 }
705
706 retval = 0;
707
708cleanup:
709 if (retval) {
710 if (tty->count == 1)
Alan Cox8fb06c72008-07-16 21:56:46 +0100711 info->port.tty = NULL; /* tty layer will release tty struct */
712 if(info->port.count)
713 info->port.count--;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800714 }
715
716 DBGINFO(("%s open rc=%d\n", info->device_name, retval));
717 return retval;
718}
719
720static void close(struct tty_struct *tty, struct file *filp)
721{
722 struct slgt_info *info = tty->driver_data;
723
724 if (sanity_check(info, tty->name, "close"))
725 return;
Alan Cox8fb06c72008-07-16 21:56:46 +0100726 DBGINFO(("%s close entry, count=%d\n", info->device_name, info->port.count));
Paul Fulghum705b6c72006-01-08 01:02:06 -0800727
Alan Cox8fb06c72008-07-16 21:56:46 +0100728 if (!info->port.count)
Paul Fulghum705b6c72006-01-08 01:02:06 -0800729 return;
730
731 if (tty_hung_up_p(filp))
732 goto cleanup;
733
Alan Cox8fb06c72008-07-16 21:56:46 +0100734 if ((tty->count == 1) && (info->port.count != 1)) {
Paul Fulghum705b6c72006-01-08 01:02:06 -0800735 /*
736 * tty->count is 1 and the tty structure will be freed.
Alan Cox8fb06c72008-07-16 21:56:46 +0100737 * info->port.count should be one in this case.
Paul Fulghum705b6c72006-01-08 01:02:06 -0800738 * if it's not, correct it so that the port is shutdown.
739 */
740 DBGERR(("%s close: bad refcount; tty->count=1, "
Alan Cox8fb06c72008-07-16 21:56:46 +0100741 "info->port.count=%d\n", info->device_name, info->port.count));
742 info->port.count = 1;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800743 }
744
Alan Cox8fb06c72008-07-16 21:56:46 +0100745 info->port.count--;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800746
747 /* if at least one open remaining, leave hardware active */
Alan Cox8fb06c72008-07-16 21:56:46 +0100748 if (info->port.count)
Paul Fulghum705b6c72006-01-08 01:02:06 -0800749 goto cleanup;
750
Alan Cox8fb06c72008-07-16 21:56:46 +0100751 info->port.flags |= ASYNC_CLOSING;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800752
753 /* set tty->closing to notify line discipline to
754 * only process XON/XOFF characters. Only the N_TTY
755 * discipline appears to use this (ppp does not).
756 */
757 tty->closing = 1;
758
759 /* wait for transmit data to clear all layers */
760
Alan Cox44b7d1b2008-07-16 21:57:18 +0100761 if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE) {
Paul Fulghum705b6c72006-01-08 01:02:06 -0800762 DBGINFO(("%s call tty_wait_until_sent\n", info->device_name));
Alan Cox44b7d1b2008-07-16 21:57:18 +0100763 tty_wait_until_sent(tty, info->port.closing_wait);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800764 }
765
Alan Cox8fb06c72008-07-16 21:56:46 +0100766 if (info->port.flags & ASYNC_INITIALIZED)
Paul Fulghum705b6c72006-01-08 01:02:06 -0800767 wait_until_sent(tty, info->timeout);
Alan Cox978e5952008-04-30 00:53:59 -0700768 flush_buffer(tty);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800769 tty_ldisc_flush(tty);
770
771 shutdown(info);
772
773 tty->closing = 0;
Alan Cox8fb06c72008-07-16 21:56:46 +0100774 info->port.tty = NULL;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800775
Alan Cox8fb06c72008-07-16 21:56:46 +0100776 if (info->port.blocked_open) {
Alan Cox44b7d1b2008-07-16 21:57:18 +0100777 if (info->port.close_delay) {
778 msleep_interruptible(jiffies_to_msecs(info->port.close_delay));
Paul Fulghum705b6c72006-01-08 01:02:06 -0800779 }
Alan Cox8fb06c72008-07-16 21:56:46 +0100780 wake_up_interruptible(&info->port.open_wait);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800781 }
782
Alan Cox8fb06c72008-07-16 21:56:46 +0100783 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800784
Alan Cox8fb06c72008-07-16 21:56:46 +0100785 wake_up_interruptible(&info->port.close_wait);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800786
787cleanup:
Alan Cox8fb06c72008-07-16 21:56:46 +0100788 DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->port.count));
Paul Fulghum705b6c72006-01-08 01:02:06 -0800789}
790
791static void hangup(struct tty_struct *tty)
792{
793 struct slgt_info *info = tty->driver_data;
794
795 if (sanity_check(info, tty->name, "hangup"))
796 return;
797 DBGINFO(("%s hangup\n", info->device_name));
798
799 flush_buffer(tty);
800 shutdown(info);
801
Alan Cox8fb06c72008-07-16 21:56:46 +0100802 info->port.count = 0;
803 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
804 info->port.tty = NULL;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800805
Alan Cox8fb06c72008-07-16 21:56:46 +0100806 wake_up_interruptible(&info->port.open_wait);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800807}
808
Alan Cox606d0992006-12-08 02:38:45 -0800809static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Paul Fulghum705b6c72006-01-08 01:02:06 -0800810{
811 struct slgt_info *info = tty->driver_data;
812 unsigned long flags;
813
814 DBGINFO(("%s set_termios\n", tty->driver->name));
815
Paul Fulghum705b6c72006-01-08 01:02:06 -0800816 change_params(info);
817
818 /* Handle transition to B0 status */
819 if (old_termios->c_cflag & CBAUD &&
820 !(tty->termios->c_cflag & CBAUD)) {
821 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
822 spin_lock_irqsave(&info->lock,flags);
823 set_signals(info);
824 spin_unlock_irqrestore(&info->lock,flags);
825 }
826
827 /* Handle transition away from B0 status */
828 if (!(old_termios->c_cflag & CBAUD) &&
829 tty->termios->c_cflag & CBAUD) {
830 info->signals |= SerialSignal_DTR;
831 if (!(tty->termios->c_cflag & CRTSCTS) ||
832 !test_bit(TTY_THROTTLED, &tty->flags)) {
833 info->signals |= SerialSignal_RTS;
834 }
835 spin_lock_irqsave(&info->lock,flags);
836 set_signals(info);
837 spin_unlock_irqrestore(&info->lock,flags);
838 }
839
840 /* Handle turning off CRTSCTS */
841 if (old_termios->c_cflag & CRTSCTS &&
842 !(tty->termios->c_cflag & CRTSCTS)) {
843 tty->hw_stopped = 0;
844 tx_release(tty);
845 }
846}
847
848static int write(struct tty_struct *tty,
849 const unsigned char *buf, int count)
850{
851 int ret = 0;
852 struct slgt_info *info = tty->driver_data;
853 unsigned long flags;
Paul Fulghum8a38c282008-07-22 11:21:28 +0100854 unsigned int bufs_needed;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800855
856 if (sanity_check(info, tty->name, "write"))
857 goto cleanup;
858 DBGINFO(("%s write count=%d\n", info->device_name, count));
859
Eric Sesterhenn326f28e92006-06-25 05:48:48 -0700860 if (!info->tx_buf)
Paul Fulghum705b6c72006-01-08 01:02:06 -0800861 goto cleanup;
862
863 if (count > info->max_frame_size) {
864 ret = -EIO;
865 goto cleanup;
866 }
867
868 if (!count)
869 goto cleanup;
870
Paul Fulghum8a38c282008-07-22 11:21:28 +0100871 if (!info->tx_active && info->tx_count) {
872 /* send accumulated data from send_char() */
873 tx_load(info, info->tx_buf, info->tx_count);
874 goto start;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800875 }
Paul Fulghum8a38c282008-07-22 11:21:28 +0100876 bufs_needed = (count/DMABUFSIZE);
877 if (count % DMABUFSIZE)
878 ++bufs_needed;
879 if (bufs_needed > free_tbuf_count(info))
880 goto cleanup;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800881
882 ret = info->tx_count = count;
883 tx_load(info, buf, count);
884 goto start;
885
886start:
887 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
888 spin_lock_irqsave(&info->lock,flags);
889 if (!info->tx_active)
890 tx_start(info);
Paul Fulghumbb029c62007-07-31 00:37:35 -0700891 else
892 tdma_start(info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800893 spin_unlock_irqrestore(&info->lock,flags);
894 }
895
896cleanup:
897 DBGINFO(("%s write rc=%d\n", info->device_name, ret));
898 return ret;
899}
900
Alan Cox55da7782008-04-30 00:54:07 -0700901static int put_char(struct tty_struct *tty, unsigned char ch)
Paul Fulghum705b6c72006-01-08 01:02:06 -0800902{
903 struct slgt_info *info = tty->driver_data;
904 unsigned long flags;
Andrew Morton6c82c412008-05-12 14:02:34 -0700905 int ret = 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800906
907 if (sanity_check(info, tty->name, "put_char"))
Alan Cox55da7782008-04-30 00:54:07 -0700908 return 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800909 DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
Eric Sesterhenn326f28e92006-06-25 05:48:48 -0700910 if (!info->tx_buf)
Alan Cox55da7782008-04-30 00:54:07 -0700911 return 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800912 spin_lock_irqsave(&info->lock,flags);
Alan Cox55da7782008-04-30 00:54:07 -0700913 if (!info->tx_active && (info->tx_count < info->max_frame_size)) {
Paul Fulghum705b6c72006-01-08 01:02:06 -0800914 info->tx_buf[info->tx_count++] = ch;
Alan Cox55da7782008-04-30 00:54:07 -0700915 ret = 1;
916 }
Paul Fulghum705b6c72006-01-08 01:02:06 -0800917 spin_unlock_irqrestore(&info->lock,flags);
Alan Cox55da7782008-04-30 00:54:07 -0700918 return ret;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800919}
920
921static void send_xchar(struct tty_struct *tty, char ch)
922{
923 struct slgt_info *info = tty->driver_data;
924 unsigned long flags;
925
926 if (sanity_check(info, tty->name, "send_xchar"))
927 return;
928 DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
929 info->x_char = ch;
930 if (ch) {
931 spin_lock_irqsave(&info->lock,flags);
932 if (!info->tx_enabled)
933 tx_start(info);
934 spin_unlock_irqrestore(&info->lock,flags);
935 }
936}
937
938static void wait_until_sent(struct tty_struct *tty, int timeout)
939{
940 struct slgt_info *info = tty->driver_data;
941 unsigned long orig_jiffies, char_time;
942
943 if (!info )
944 return;
945 if (sanity_check(info, tty->name, "wait_until_sent"))
946 return;
947 DBGINFO(("%s wait_until_sent entry\n", info->device_name));
Alan Cox8fb06c72008-07-16 21:56:46 +0100948 if (!(info->port.flags & ASYNC_INITIALIZED))
Paul Fulghum705b6c72006-01-08 01:02:06 -0800949 goto exit;
950
951 orig_jiffies = jiffies;
952
953 /* Set check interval to 1/5 of estimated time to
954 * send a character, and make it at least 1. The check
955 * interval should also be less than the timeout.
956 * Note: use tight timings here to satisfy the NIST-PCTS.
957 */
958
Alan Cox978e5952008-04-30 00:53:59 -0700959 lock_kernel();
960
Paul Fulghum705b6c72006-01-08 01:02:06 -0800961 if (info->params.data_rate) {
962 char_time = info->timeout/(32 * 5);
963 if (!char_time)
964 char_time++;
965 } else
966 char_time = 1;
967
968 if (timeout)
969 char_time = min_t(unsigned long, char_time, timeout);
970
971 while (info->tx_active) {
972 msleep_interruptible(jiffies_to_msecs(char_time));
973 if (signal_pending(current))
974 break;
975 if (timeout && time_after(jiffies, orig_jiffies + timeout))
976 break;
977 }
Alan Cox978e5952008-04-30 00:53:59 -0700978 unlock_kernel();
Paul Fulghum705b6c72006-01-08 01:02:06 -0800979
980exit:
981 DBGINFO(("%s wait_until_sent exit\n", info->device_name));
982}
983
984static int write_room(struct tty_struct *tty)
985{
986 struct slgt_info *info = tty->driver_data;
987 int ret;
988
989 if (sanity_check(info, tty->name, "write_room"))
990 return 0;
991 ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
992 DBGINFO(("%s write_room=%d\n", info->device_name, ret));
993 return ret;
994}
995
996static void flush_chars(struct tty_struct *tty)
997{
998 struct slgt_info *info = tty->driver_data;
999 unsigned long flags;
1000
1001 if (sanity_check(info, tty->name, "flush_chars"))
1002 return;
1003 DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
1004
1005 if (info->tx_count <= 0 || tty->stopped ||
1006 tty->hw_stopped || !info->tx_buf)
1007 return;
1008
1009 DBGINFO(("%s flush_chars start transmit\n", info->device_name));
1010
1011 spin_lock_irqsave(&info->lock,flags);
1012 if (!info->tx_active && info->tx_count) {
1013 tx_load(info, info->tx_buf,info->tx_count);
1014 tx_start(info);
1015 }
1016 spin_unlock_irqrestore(&info->lock,flags);
1017}
1018
1019static void flush_buffer(struct tty_struct *tty)
1020{
1021 struct slgt_info *info = tty->driver_data;
1022 unsigned long flags;
1023
1024 if (sanity_check(info, tty->name, "flush_buffer"))
1025 return;
1026 DBGINFO(("%s flush_buffer\n", info->device_name));
1027
1028 spin_lock_irqsave(&info->lock,flags);
1029 if (!info->tx_active)
1030 info->tx_count = 0;
1031 spin_unlock_irqrestore(&info->lock,flags);
1032
Paul Fulghum705b6c72006-01-08 01:02:06 -08001033 tty_wakeup(tty);
1034}
1035
1036/*
1037 * throttle (stop) transmitter
1038 */
1039static void tx_hold(struct tty_struct *tty)
1040{
1041 struct slgt_info *info = tty->driver_data;
1042 unsigned long flags;
1043
1044 if (sanity_check(info, tty->name, "tx_hold"))
1045 return;
1046 DBGINFO(("%s tx_hold\n", info->device_name));
1047 spin_lock_irqsave(&info->lock,flags);
1048 if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
1049 tx_stop(info);
1050 spin_unlock_irqrestore(&info->lock,flags);
1051}
1052
1053/*
1054 * release (start) transmitter
1055 */
1056static void tx_release(struct tty_struct *tty)
1057{
1058 struct slgt_info *info = tty->driver_data;
1059 unsigned long flags;
1060
1061 if (sanity_check(info, tty->name, "tx_release"))
1062 return;
1063 DBGINFO(("%s tx_release\n", info->device_name));
1064 spin_lock_irqsave(&info->lock,flags);
1065 if (!info->tx_active && info->tx_count) {
1066 tx_load(info, info->tx_buf, info->tx_count);
1067 tx_start(info);
1068 }
1069 spin_unlock_irqrestore(&info->lock,flags);
1070}
1071
1072/*
1073 * Service an IOCTL request
1074 *
1075 * Arguments
1076 *
1077 * tty pointer to tty instance data
1078 * file pointer to associated file object for device
1079 * cmd IOCTL command code
1080 * arg command argument/context
1081 *
1082 * Return 0 if success, otherwise error code
1083 */
1084static int ioctl(struct tty_struct *tty, struct file *file,
1085 unsigned int cmd, unsigned long arg)
1086{
1087 struct slgt_info *info = tty->driver_data;
1088 struct mgsl_icount cnow; /* kernel counter temps */
1089 struct serial_icounter_struct __user *p_cuser; /* user space */
1090 unsigned long flags;
1091 void __user *argp = (void __user *)arg;
Alan Cox1f8cabb2008-04-30 00:53:24 -07001092 int ret;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001093
1094 if (sanity_check(info, tty->name, "ioctl"))
1095 return -ENODEV;
1096 DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
1097
1098 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1099 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1100 if (tty->flags & (1 << TTY_IO_ERROR))
1101 return -EIO;
1102 }
1103
Alan Cox1f8cabb2008-04-30 00:53:24 -07001104 lock_kernel();
1105
Paul Fulghum705b6c72006-01-08 01:02:06 -08001106 switch (cmd) {
1107 case MGSL_IOCGPARAMS:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001108 ret = get_params(info, argp);
1109 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001110 case MGSL_IOCSPARAMS:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001111 ret = set_params(info, argp);
1112 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001113 case MGSL_IOCGTXIDLE:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001114 ret = get_txidle(info, argp);
1115 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001116 case MGSL_IOCSTXIDLE:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001117 ret = set_txidle(info, (int)arg);
1118 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001119 case MGSL_IOCTXENABLE:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001120 ret = tx_enable(info, (int)arg);
1121 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001122 case MGSL_IOCRXENABLE:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001123 ret = rx_enable(info, (int)arg);
1124 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001125 case MGSL_IOCTXABORT:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001126 ret = tx_abort(info);
1127 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001128 case MGSL_IOCGSTATS:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001129 ret = get_stats(info, argp);
1130 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001131 case MGSL_IOCWAITEVENT:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001132 ret = wait_mgsl_event(info, argp);
1133 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001134 case TIOCMIWAIT:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001135 ret = modem_input_wait(info,(int)arg);
1136 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001137 case MGSL_IOCGIF:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001138 ret = get_interface(info, argp);
1139 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001140 case MGSL_IOCSIF:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001141 ret = set_interface(info,(int)arg);
1142 break;
Paul Fulghum0080b7a2006-03-28 01:56:15 -08001143 case MGSL_IOCSGPIO:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001144 ret = set_gpio(info, argp);
1145 break;
Paul Fulghum0080b7a2006-03-28 01:56:15 -08001146 case MGSL_IOCGGPIO:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001147 ret = get_gpio(info, argp);
1148 break;
Paul Fulghum0080b7a2006-03-28 01:56:15 -08001149 case MGSL_IOCWAITGPIO:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001150 ret = wait_gpio(info, argp);
1151 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001152 case TIOCGICOUNT:
1153 spin_lock_irqsave(&info->lock,flags);
1154 cnow = info->icount;
1155 spin_unlock_irqrestore(&info->lock,flags);
1156 p_cuser = argp;
1157 if (put_user(cnow.cts, &p_cuser->cts) ||
1158 put_user(cnow.dsr, &p_cuser->dsr) ||
1159 put_user(cnow.rng, &p_cuser->rng) ||
1160 put_user(cnow.dcd, &p_cuser->dcd) ||
1161 put_user(cnow.rx, &p_cuser->rx) ||
1162 put_user(cnow.tx, &p_cuser->tx) ||
1163 put_user(cnow.frame, &p_cuser->frame) ||
1164 put_user(cnow.overrun, &p_cuser->overrun) ||
1165 put_user(cnow.parity, &p_cuser->parity) ||
1166 put_user(cnow.brk, &p_cuser->brk) ||
1167 put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
Alan Cox1f8cabb2008-04-30 00:53:24 -07001168 ret = -EFAULT;
1169 ret = 0;
1170 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001171 default:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001172 ret = -ENOIOCTLCMD;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001173 }
Alan Cox1f8cabb2008-04-30 00:53:24 -07001174 unlock_kernel();
1175 return ret;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001176}
1177
1178/*
Paul Fulghum2acdb162007-05-10 22:22:43 -07001179 * support for 32 bit ioctl calls on 64 bit systems
1180 */
1181#ifdef CONFIG_COMPAT
1182static long get_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *user_params)
1183{
1184 struct MGSL_PARAMS32 tmp_params;
1185
1186 DBGINFO(("%s get_params32\n", info->device_name));
1187 tmp_params.mode = (compat_ulong_t)info->params.mode;
1188 tmp_params.loopback = info->params.loopback;
1189 tmp_params.flags = info->params.flags;
1190 tmp_params.encoding = info->params.encoding;
1191 tmp_params.clock_speed = (compat_ulong_t)info->params.clock_speed;
1192 tmp_params.addr_filter = info->params.addr_filter;
1193 tmp_params.crc_type = info->params.crc_type;
1194 tmp_params.preamble_length = info->params.preamble_length;
1195 tmp_params.preamble = info->params.preamble;
1196 tmp_params.data_rate = (compat_ulong_t)info->params.data_rate;
1197 tmp_params.data_bits = info->params.data_bits;
1198 tmp_params.stop_bits = info->params.stop_bits;
1199 tmp_params.parity = info->params.parity;
1200 if (copy_to_user(user_params, &tmp_params, sizeof(struct MGSL_PARAMS32)))
1201 return -EFAULT;
1202 return 0;
1203}
1204
1205static long set_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *new_params)
1206{
1207 struct MGSL_PARAMS32 tmp_params;
1208
1209 DBGINFO(("%s set_params32\n", info->device_name));
1210 if (copy_from_user(&tmp_params, new_params, sizeof(struct MGSL_PARAMS32)))
1211 return -EFAULT;
1212
1213 spin_lock(&info->lock);
1214 info->params.mode = tmp_params.mode;
1215 info->params.loopback = tmp_params.loopback;
1216 info->params.flags = tmp_params.flags;
1217 info->params.encoding = tmp_params.encoding;
1218 info->params.clock_speed = tmp_params.clock_speed;
1219 info->params.addr_filter = tmp_params.addr_filter;
1220 info->params.crc_type = tmp_params.crc_type;
1221 info->params.preamble_length = tmp_params.preamble_length;
1222 info->params.preamble = tmp_params.preamble;
1223 info->params.data_rate = tmp_params.data_rate;
1224 info->params.data_bits = tmp_params.data_bits;
1225 info->params.stop_bits = tmp_params.stop_bits;
1226 info->params.parity = tmp_params.parity;
1227 spin_unlock(&info->lock);
1228
1229 change_params(info);
1230
1231 return 0;
1232}
1233
1234static long slgt_compat_ioctl(struct tty_struct *tty, struct file *file,
1235 unsigned int cmd, unsigned long arg)
1236{
1237 struct slgt_info *info = tty->driver_data;
1238 int rc = -ENOIOCTLCMD;
1239
1240 if (sanity_check(info, tty->name, "compat_ioctl"))
1241 return -ENODEV;
1242 DBGINFO(("%s compat_ioctl() cmd=%08X\n", info->device_name, cmd));
1243
1244 switch (cmd) {
1245
1246 case MGSL_IOCSPARAMS32:
1247 rc = set_params32(info, compat_ptr(arg));
1248 break;
1249
1250 case MGSL_IOCGPARAMS32:
1251 rc = get_params32(info, compat_ptr(arg));
1252 break;
1253
1254 case MGSL_IOCGPARAMS:
1255 case MGSL_IOCSPARAMS:
1256 case MGSL_IOCGTXIDLE:
1257 case MGSL_IOCGSTATS:
1258 case MGSL_IOCWAITEVENT:
1259 case MGSL_IOCGIF:
1260 case MGSL_IOCSGPIO:
1261 case MGSL_IOCGGPIO:
1262 case MGSL_IOCWAITGPIO:
1263 case TIOCGICOUNT:
1264 rc = ioctl(tty, file, cmd, (unsigned long)(compat_ptr(arg)));
1265 break;
1266
1267 case MGSL_IOCSTXIDLE:
1268 case MGSL_IOCTXENABLE:
1269 case MGSL_IOCRXENABLE:
1270 case MGSL_IOCTXABORT:
1271 case TIOCMIWAIT:
1272 case MGSL_IOCSIF:
1273 rc = ioctl(tty, file, cmd, arg);
1274 break;
1275 }
1276
1277 DBGINFO(("%s compat_ioctl() cmd=%08X rc=%d\n", info->device_name, cmd, rc));
1278 return rc;
1279}
1280#else
1281#define slgt_compat_ioctl NULL
1282#endif /* ifdef CONFIG_COMPAT */
1283
1284/*
Paul Fulghum705b6c72006-01-08 01:02:06 -08001285 * proc fs support
1286 */
1287static inline int line_info(char *buf, struct slgt_info *info)
1288{
1289 char stat_buf[30];
1290 int ret;
1291 unsigned long flags;
1292
1293 ret = sprintf(buf, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
1294 info->device_name, info->phys_reg_addr,
1295 info->irq_level, info->max_frame_size);
1296
1297 /* output current serial signal states */
1298 spin_lock_irqsave(&info->lock,flags);
1299 get_signals(info);
1300 spin_unlock_irqrestore(&info->lock,flags);
1301
1302 stat_buf[0] = 0;
1303 stat_buf[1] = 0;
1304 if (info->signals & SerialSignal_RTS)
1305 strcat(stat_buf, "|RTS");
1306 if (info->signals & SerialSignal_CTS)
1307 strcat(stat_buf, "|CTS");
1308 if (info->signals & SerialSignal_DTR)
1309 strcat(stat_buf, "|DTR");
1310 if (info->signals & SerialSignal_DSR)
1311 strcat(stat_buf, "|DSR");
1312 if (info->signals & SerialSignal_DCD)
1313 strcat(stat_buf, "|CD");
1314 if (info->signals & SerialSignal_RI)
1315 strcat(stat_buf, "|RI");
1316
1317 if (info->params.mode != MGSL_MODE_ASYNC) {
1318 ret += sprintf(buf+ret, "\tHDLC txok:%d rxok:%d",
1319 info->icount.txok, info->icount.rxok);
1320 if (info->icount.txunder)
1321 ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
1322 if (info->icount.txabort)
1323 ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
1324 if (info->icount.rxshort)
1325 ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
1326 if (info->icount.rxlong)
1327 ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
1328 if (info->icount.rxover)
1329 ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
1330 if (info->icount.rxcrc)
1331 ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
1332 } else {
1333 ret += sprintf(buf+ret, "\tASYNC tx:%d rx:%d",
1334 info->icount.tx, info->icount.rx);
1335 if (info->icount.frame)
1336 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
1337 if (info->icount.parity)
1338 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
1339 if (info->icount.brk)
1340 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
1341 if (info->icount.overrun)
1342 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
1343 }
1344
1345 /* Append serial signal status to end */
1346 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
1347
1348 ret += sprintf(buf+ret, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1349 info->tx_active,info->bh_requested,info->bh_running,
1350 info->pending_bh);
1351
1352 return ret;
1353}
1354
1355/* Called to print information about devices
1356 */
1357static int read_proc(char *page, char **start, off_t off, int count,
1358 int *eof, void *data)
1359{
1360 int len = 0, l;
1361 off_t begin = 0;
1362 struct slgt_info *info;
1363
1364 len += sprintf(page, "synclink_gt driver:%s\n", driver_version);
1365
1366 info = slgt_device_list;
1367 while( info ) {
1368 l = line_info(page + len, info);
1369 len += l;
1370 if (len+begin > off+count)
1371 goto done;
1372 if (len+begin < off) {
1373 begin += len;
1374 len = 0;
1375 }
1376 info = info->next_device;
1377 }
1378
1379 *eof = 1;
1380done:
1381 if (off >= len+begin)
1382 return 0;
1383 *start = page + (off-begin);
1384 return ((count < begin+len-off) ? count : begin+len-off);
1385}
1386
1387/*
1388 * return count of bytes in transmit buffer
1389 */
1390static int chars_in_buffer(struct tty_struct *tty)
1391{
1392 struct slgt_info *info = tty->driver_data;
Paul Fulghum403214d2008-07-22 11:21:55 +01001393 int count;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001394 if (sanity_check(info, tty->name, "chars_in_buffer"))
1395 return 0;
Paul Fulghum403214d2008-07-22 11:21:55 +01001396 count = tbuf_bytes(info);
1397 DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, count));
1398 return count;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001399}
1400
1401/*
1402 * signal remote device to throttle send data (our receive data)
1403 */
1404static void throttle(struct tty_struct * tty)
1405{
1406 struct slgt_info *info = tty->driver_data;
1407 unsigned long flags;
1408
1409 if (sanity_check(info, tty->name, "throttle"))
1410 return;
1411 DBGINFO(("%s throttle\n", info->device_name));
1412 if (I_IXOFF(tty))
1413 send_xchar(tty, STOP_CHAR(tty));
1414 if (tty->termios->c_cflag & CRTSCTS) {
1415 spin_lock_irqsave(&info->lock,flags);
1416 info->signals &= ~SerialSignal_RTS;
1417 set_signals(info);
1418 spin_unlock_irqrestore(&info->lock,flags);
1419 }
1420}
1421
1422/*
1423 * signal remote device to stop throttling send data (our receive data)
1424 */
1425static void unthrottle(struct tty_struct * tty)
1426{
1427 struct slgt_info *info = tty->driver_data;
1428 unsigned long flags;
1429
1430 if (sanity_check(info, tty->name, "unthrottle"))
1431 return;
1432 DBGINFO(("%s unthrottle\n", info->device_name));
1433 if (I_IXOFF(tty)) {
1434 if (info->x_char)
1435 info->x_char = 0;
1436 else
1437 send_xchar(tty, START_CHAR(tty));
1438 }
1439 if (tty->termios->c_cflag & CRTSCTS) {
1440 spin_lock_irqsave(&info->lock,flags);
1441 info->signals |= SerialSignal_RTS;
1442 set_signals(info);
1443 spin_unlock_irqrestore(&info->lock,flags);
1444 }
1445}
1446
1447/*
1448 * set or clear transmit break condition
1449 * break_state -1=set break condition, 0=clear
1450 */
Alan Cox9e989662008-07-22 11:18:03 +01001451static int set_break(struct tty_struct *tty, int break_state)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001452{
1453 struct slgt_info *info = tty->driver_data;
1454 unsigned short value;
1455 unsigned long flags;
1456
1457 if (sanity_check(info, tty->name, "set_break"))
Alan Cox9e989662008-07-22 11:18:03 +01001458 return -EINVAL;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001459 DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
1460
1461 spin_lock_irqsave(&info->lock,flags);
1462 value = rd_reg16(info, TCR);
1463 if (break_state == -1)
1464 value |= BIT6;
1465 else
1466 value &= ~BIT6;
1467 wr_reg16(info, TCR, value);
1468 spin_unlock_irqrestore(&info->lock,flags);
Alan Cox9e989662008-07-22 11:18:03 +01001469 return 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001470}
1471
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08001472#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08001473
1474/**
1475 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1476 * set encoding and frame check sequence (FCS) options
1477 *
1478 * dev pointer to network device structure
1479 * encoding serial encoding setting
1480 * parity FCS setting
1481 *
1482 * returns 0 if success, otherwise error code
1483 */
1484static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
1485 unsigned short parity)
1486{
1487 struct slgt_info *info = dev_to_port(dev);
1488 unsigned char new_encoding;
1489 unsigned short new_crctype;
1490
1491 /* return error if TTY interface open */
Alan Cox8fb06c72008-07-16 21:56:46 +01001492 if (info->port.count)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001493 return -EBUSY;
1494
1495 DBGINFO(("%s hdlcdev_attach\n", info->device_name));
1496
1497 switch (encoding)
1498 {
1499 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
1500 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
1501 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
1502 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
1503 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
1504 default: return -EINVAL;
1505 }
1506
1507 switch (parity)
1508 {
1509 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
1510 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
1511 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
1512 default: return -EINVAL;
1513 }
1514
1515 info->params.encoding = new_encoding;
Alexey Dobriyan53b35312006-03-24 03:16:13 -08001516 info->params.crc_type = new_crctype;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001517
1518 /* if network interface up, reprogram hardware */
1519 if (info->netcount)
1520 program_hw(info);
1521
1522 return 0;
1523}
1524
1525/**
1526 * called by generic HDLC layer to send frame
1527 *
1528 * skb socket buffer containing HDLC frame
1529 * dev pointer to network device structure
1530 *
1531 * returns 0 if success, otherwise error code
1532 */
1533static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
1534{
1535 struct slgt_info *info = dev_to_port(dev);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001536 unsigned long flags;
1537
1538 DBGINFO(("%s hdlc_xmit\n", dev->name));
1539
1540 /* stop sending until this frame completes */
1541 netif_stop_queue(dev);
1542
1543 /* copy data to device buffers */
1544 info->tx_count = skb->len;
1545 tx_load(info, skb->data, skb->len);
1546
1547 /* update network statistics */
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001548 dev->stats.tx_packets++;
1549 dev->stats.tx_bytes += skb->len;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001550
1551 /* done with socket buffer, so free it */
1552 dev_kfree_skb(skb);
1553
1554 /* save start time for transmit timeout detection */
1555 dev->trans_start = jiffies;
1556
1557 /* start hardware transmitter if necessary */
1558 spin_lock_irqsave(&info->lock,flags);
1559 if (!info->tx_active)
1560 tx_start(info);
1561 spin_unlock_irqrestore(&info->lock,flags);
1562
1563 return 0;
1564}
1565
1566/**
1567 * called by network layer when interface enabled
1568 * claim resources and initialize hardware
1569 *
1570 * dev pointer to network device structure
1571 *
1572 * returns 0 if success, otherwise error code
1573 */
1574static int hdlcdev_open(struct net_device *dev)
1575{
1576 struct slgt_info *info = dev_to_port(dev);
1577 int rc;
1578 unsigned long flags;
1579
Paul Fulghumd4c63b72007-08-22 14:01:50 -07001580 if (!try_module_get(THIS_MODULE))
1581 return -EBUSY;
1582
Paul Fulghum705b6c72006-01-08 01:02:06 -08001583 DBGINFO(("%s hdlcdev_open\n", dev->name));
1584
1585 /* generic HDLC layer open processing */
1586 if ((rc = hdlc_open(dev)))
1587 return rc;
1588
1589 /* arbitrate between network and tty opens */
1590 spin_lock_irqsave(&info->netlock, flags);
Alan Cox8fb06c72008-07-16 21:56:46 +01001591 if (info->port.count != 0 || info->netcount != 0) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08001592 DBGINFO(("%s hdlc_open busy\n", dev->name));
1593 spin_unlock_irqrestore(&info->netlock, flags);
1594 return -EBUSY;
1595 }
1596 info->netcount=1;
1597 spin_unlock_irqrestore(&info->netlock, flags);
1598
1599 /* claim resources and init adapter */
1600 if ((rc = startup(info)) != 0) {
1601 spin_lock_irqsave(&info->netlock, flags);
1602 info->netcount=0;
1603 spin_unlock_irqrestore(&info->netlock, flags);
1604 return rc;
1605 }
1606
1607 /* assert DTR and RTS, apply hardware settings */
1608 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
1609 program_hw(info);
1610
1611 /* enable network layer transmit */
1612 dev->trans_start = jiffies;
1613 netif_start_queue(dev);
1614
1615 /* inform generic HDLC layer of current DCD status */
1616 spin_lock_irqsave(&info->lock, flags);
1617 get_signals(info);
1618 spin_unlock_irqrestore(&info->lock, flags);
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07001619 if (info->signals & SerialSignal_DCD)
1620 netif_carrier_on(dev);
1621 else
1622 netif_carrier_off(dev);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001623 return 0;
1624}
1625
1626/**
1627 * called by network layer when interface is disabled
1628 * shutdown hardware and release resources
1629 *
1630 * dev pointer to network device structure
1631 *
1632 * returns 0 if success, otherwise error code
1633 */
1634static int hdlcdev_close(struct net_device *dev)
1635{
1636 struct slgt_info *info = dev_to_port(dev);
1637 unsigned long flags;
1638
1639 DBGINFO(("%s hdlcdev_close\n", dev->name));
1640
1641 netif_stop_queue(dev);
1642
1643 /* shutdown adapter and release resources */
1644 shutdown(info);
1645
1646 hdlc_close(dev);
1647
1648 spin_lock_irqsave(&info->netlock, flags);
1649 info->netcount=0;
1650 spin_unlock_irqrestore(&info->netlock, flags);
1651
Paul Fulghumd4c63b72007-08-22 14:01:50 -07001652 module_put(THIS_MODULE);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001653 return 0;
1654}
1655
1656/**
1657 * called by network layer to process IOCTL call to network device
1658 *
1659 * dev pointer to network device structure
1660 * ifr pointer to network interface request structure
1661 * cmd IOCTL command code
1662 *
1663 * returns 0 if success, otherwise error code
1664 */
1665static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1666{
1667 const size_t size = sizeof(sync_serial_settings);
1668 sync_serial_settings new_line;
1669 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1670 struct slgt_info *info = dev_to_port(dev);
1671 unsigned int flags;
1672
1673 DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
1674
1675 /* return error if TTY interface open */
Alan Cox8fb06c72008-07-16 21:56:46 +01001676 if (info->port.count)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001677 return -EBUSY;
1678
1679 if (cmd != SIOCWANDEV)
1680 return hdlc_ioctl(dev, ifr, cmd);
1681
1682 switch(ifr->ifr_settings.type) {
1683 case IF_GET_IFACE: /* return current sync_serial_settings */
1684
1685 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1686 if (ifr->ifr_settings.size < size) {
1687 ifr->ifr_settings.size = size; /* data size wanted */
1688 return -ENOBUFS;
1689 }
1690
1691 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1692 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1693 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1694 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1695
1696 switch (flags){
1697 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
1698 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
1699 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
1700 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
1701 default: new_line.clock_type = CLOCK_DEFAULT;
1702 }
1703
1704 new_line.clock_rate = info->params.clock_speed;
1705 new_line.loopback = info->params.loopback ? 1:0;
1706
1707 if (copy_to_user(line, &new_line, size))
1708 return -EFAULT;
1709 return 0;
1710
1711 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
1712
1713 if(!capable(CAP_NET_ADMIN))
1714 return -EPERM;
1715 if (copy_from_user(&new_line, line, size))
1716 return -EFAULT;
1717
1718 switch (new_line.clock_type)
1719 {
1720 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
1721 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
1722 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
1723 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
1724 case CLOCK_DEFAULT: flags = info->params.flags &
1725 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1726 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1727 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1728 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
1729 default: return -EINVAL;
1730 }
1731
1732 if (new_line.loopback != 0 && new_line.loopback != 1)
1733 return -EINVAL;
1734
1735 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1736 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1737 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1738 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1739 info->params.flags |= flags;
1740
1741 info->params.loopback = new_line.loopback;
1742
1743 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
1744 info->params.clock_speed = new_line.clock_rate;
1745 else
1746 info->params.clock_speed = 0;
1747
1748 /* if network interface up, reprogram hardware */
1749 if (info->netcount)
1750 program_hw(info);
1751 return 0;
1752
1753 default:
1754 return hdlc_ioctl(dev, ifr, cmd);
1755 }
1756}
1757
1758/**
1759 * called by network layer when transmit timeout is detected
1760 *
1761 * dev pointer to network device structure
1762 */
1763static void hdlcdev_tx_timeout(struct net_device *dev)
1764{
1765 struct slgt_info *info = dev_to_port(dev);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001766 unsigned long flags;
1767
1768 DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
1769
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001770 dev->stats.tx_errors++;
1771 dev->stats.tx_aborted_errors++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001772
1773 spin_lock_irqsave(&info->lock,flags);
1774 tx_stop(info);
1775 spin_unlock_irqrestore(&info->lock,flags);
1776
1777 netif_wake_queue(dev);
1778}
1779
1780/**
1781 * called by device driver when transmit completes
1782 * reenable network layer transmit if stopped
1783 *
1784 * info pointer to device instance information
1785 */
1786static void hdlcdev_tx_done(struct slgt_info *info)
1787{
1788 if (netif_queue_stopped(info->netdev))
1789 netif_wake_queue(info->netdev);
1790}
1791
1792/**
1793 * called by device driver when frame received
1794 * pass frame to network layer
1795 *
1796 * info pointer to device instance information
1797 * buf pointer to buffer contianing frame data
1798 * size count of data bytes in buf
1799 */
1800static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
1801{
1802 struct sk_buff *skb = dev_alloc_skb(size);
1803 struct net_device *dev = info->netdev;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001804
1805 DBGINFO(("%s hdlcdev_rx\n", dev->name));
1806
1807 if (skb == NULL) {
1808 DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001809 dev->stats.rx_dropped++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001810 return;
1811 }
1812
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001813 memcpy(skb_put(skb, size), buf, size);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001814
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001815 skb->protocol = hdlc_type_trans(skb, dev);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001816
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001817 dev->stats.rx_packets++;
1818 dev->stats.rx_bytes += size;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001819
1820 netif_rx(skb);
1821
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001822 dev->last_rx = jiffies;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001823}
1824
1825/**
1826 * called by device driver when adding device instance
1827 * do generic HDLC initialization
1828 *
1829 * info pointer to device instance information
1830 *
1831 * returns 0 if success, otherwise error code
1832 */
1833static int hdlcdev_init(struct slgt_info *info)
1834{
1835 int rc;
1836 struct net_device *dev;
1837 hdlc_device *hdlc;
1838
1839 /* allocate and initialize network and HDLC layer objects */
1840
1841 if (!(dev = alloc_hdlcdev(info))) {
1842 printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
1843 return -ENOMEM;
1844 }
1845
1846 /* for network layer reporting purposes only */
1847 dev->mem_start = info->phys_reg_addr;
1848 dev->mem_end = info->phys_reg_addr + SLGT_REG_SIZE - 1;
1849 dev->irq = info->irq_level;
1850
1851 /* network layer callbacks and settings */
1852 dev->do_ioctl = hdlcdev_ioctl;
1853 dev->open = hdlcdev_open;
1854 dev->stop = hdlcdev_close;
1855 dev->tx_timeout = hdlcdev_tx_timeout;
1856 dev->watchdog_timeo = 10*HZ;
1857 dev->tx_queue_len = 50;
1858
1859 /* generic HDLC layer callbacks and settings */
1860 hdlc = dev_to_hdlc(dev);
1861 hdlc->attach = hdlcdev_attach;
1862 hdlc->xmit = hdlcdev_xmit;
1863
1864 /* register objects with HDLC layer */
1865 if ((rc = register_hdlc_device(dev))) {
1866 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
1867 free_netdev(dev);
1868 return rc;
1869 }
1870
1871 info->netdev = dev;
1872 return 0;
1873}
1874
1875/**
1876 * called by device driver when removing device instance
1877 * do generic HDLC cleanup
1878 *
1879 * info pointer to device instance information
1880 */
1881static void hdlcdev_exit(struct slgt_info *info)
1882{
1883 unregister_hdlc_device(info->netdev);
1884 free_netdev(info->netdev);
1885 info->netdev = NULL;
1886}
1887
1888#endif /* ifdef CONFIG_HDLC */
1889
1890/*
1891 * get async data from rx DMA buffers
1892 */
1893static void rx_async(struct slgt_info *info)
1894{
Alan Cox8fb06c72008-07-16 21:56:46 +01001895 struct tty_struct *tty = info->port.tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001896 struct mgsl_icount *icount = &info->icount;
1897 unsigned int start, end;
1898 unsigned char *p;
1899 unsigned char status;
1900 struct slgt_desc *bufs = info->rbufs;
1901 int i, count;
Alan Cox33f0f882006-01-09 20:54:13 -08001902 int chars = 0;
1903 int stat;
1904 unsigned char ch;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001905
1906 start = end = info->rbuf_current;
1907
1908 while(desc_complete(bufs[end])) {
1909 count = desc_count(bufs[end]) - info->rbuf_index;
1910 p = bufs[end].buf + info->rbuf_index;
1911
1912 DBGISR(("%s rx_async count=%d\n", info->device_name, count));
1913 DBGDATA(info, p, count, "rx");
1914
1915 for(i=0 ; i < count; i+=2, p+=2) {
Alan Cox33f0f882006-01-09 20:54:13 -08001916 ch = *p;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001917 icount->rx++;
1918
Alan Cox33f0f882006-01-09 20:54:13 -08001919 stat = 0;
1920
Paul Fulghum202af6d2006-08-31 21:27:36 -07001921 if ((status = *(p+1) & (BIT1 + BIT0))) {
1922 if (status & BIT1)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001923 icount->parity++;
Paul Fulghum202af6d2006-08-31 21:27:36 -07001924 else if (status & BIT0)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001925 icount->frame++;
1926 /* discard char if tty control flags say so */
1927 if (status & info->ignore_status_mask)
1928 continue;
Paul Fulghum202af6d2006-08-31 21:27:36 -07001929 if (status & BIT1)
Alan Cox33f0f882006-01-09 20:54:13 -08001930 stat = TTY_PARITY;
Paul Fulghum202af6d2006-08-31 21:27:36 -07001931 else if (status & BIT0)
Alan Cox33f0f882006-01-09 20:54:13 -08001932 stat = TTY_FRAME;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001933 }
1934 if (tty) {
Alan Cox33f0f882006-01-09 20:54:13 -08001935 tty_insert_flip_char(tty, ch, stat);
1936 chars++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001937 }
1938 }
1939
1940 if (i < count) {
1941 /* receive buffer not completed */
1942 info->rbuf_index += i;
Jiri Slaby40565f12007-02-12 00:52:31 -08001943 mod_timer(&info->rx_timer, jiffies + 1);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001944 break;
1945 }
1946
1947 info->rbuf_index = 0;
1948 free_rbufs(info, end, end);
1949
1950 if (++end == info->rbuf_count)
1951 end = 0;
1952
1953 /* if entire list searched then no frame available */
1954 if (end == start)
1955 break;
1956 }
1957
Alan Cox33f0f882006-01-09 20:54:13 -08001958 if (tty && chars)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001959 tty_flip_buffer_push(tty);
1960}
1961
1962/*
1963 * return next bottom half action to perform
1964 */
1965static int bh_action(struct slgt_info *info)
1966{
1967 unsigned long flags;
1968 int rc;
1969
1970 spin_lock_irqsave(&info->lock,flags);
1971
1972 if (info->pending_bh & BH_RECEIVE) {
1973 info->pending_bh &= ~BH_RECEIVE;
1974 rc = BH_RECEIVE;
1975 } else if (info->pending_bh & BH_TRANSMIT) {
1976 info->pending_bh &= ~BH_TRANSMIT;
1977 rc = BH_TRANSMIT;
1978 } else if (info->pending_bh & BH_STATUS) {
1979 info->pending_bh &= ~BH_STATUS;
1980 rc = BH_STATUS;
1981 } else {
1982 /* Mark BH routine as complete */
Joe Perches0fab6de2008-04-28 02:14:02 -07001983 info->bh_running = false;
1984 info->bh_requested = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001985 rc = 0;
1986 }
1987
1988 spin_unlock_irqrestore(&info->lock,flags);
1989
1990 return rc;
1991}
1992
1993/*
1994 * perform bottom half processing
1995 */
David Howellsc4028952006-11-22 14:57:56 +00001996static void bh_handler(struct work_struct *work)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001997{
David Howellsc4028952006-11-22 14:57:56 +00001998 struct slgt_info *info = container_of(work, struct slgt_info, task);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001999 int action;
2000
2001 if (!info)
2002 return;
Joe Perches0fab6de2008-04-28 02:14:02 -07002003 info->bh_running = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002004
2005 while((action = bh_action(info))) {
2006 switch (action) {
2007 case BH_RECEIVE:
2008 DBGBH(("%s bh receive\n", info->device_name));
2009 switch(info->params.mode) {
2010 case MGSL_MODE_ASYNC:
2011 rx_async(info);
2012 break;
2013 case MGSL_MODE_HDLC:
2014 while(rx_get_frame(info));
2015 break;
2016 case MGSL_MODE_RAW:
Paul Fulghumcb10dc92006-09-30 23:27:45 -07002017 case MGSL_MODE_MONOSYNC:
2018 case MGSL_MODE_BISYNC:
Paul Fulghum705b6c72006-01-08 01:02:06 -08002019 while(rx_get_buf(info));
2020 break;
2021 }
2022 /* restart receiver if rx DMA buffers exhausted */
2023 if (info->rx_restart)
2024 rx_start(info);
2025 break;
2026 case BH_TRANSMIT:
2027 bh_transmit(info);
2028 break;
2029 case BH_STATUS:
2030 DBGBH(("%s bh status\n", info->device_name));
2031 info->ri_chkcount = 0;
2032 info->dsr_chkcount = 0;
2033 info->dcd_chkcount = 0;
2034 info->cts_chkcount = 0;
2035 break;
2036 default:
2037 DBGBH(("%s unknown action\n", info->device_name));
2038 break;
2039 }
2040 }
2041 DBGBH(("%s bh_handler exit\n", info->device_name));
2042}
2043
2044static void bh_transmit(struct slgt_info *info)
2045{
Alan Cox8fb06c72008-07-16 21:56:46 +01002046 struct tty_struct *tty = info->port.tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002047
2048 DBGBH(("%s bh_transmit\n", info->device_name));
Jiri Slabyb963a842007-02-10 01:44:55 -08002049 if (tty)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002050 tty_wakeup(tty);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002051}
2052
Paul Fulghumed8485f2008-02-06 01:37:18 -08002053static void dsr_change(struct slgt_info *info, unsigned short status)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002054{
Paul Fulghumed8485f2008-02-06 01:37:18 -08002055 if (status & BIT3) {
2056 info->signals |= SerialSignal_DSR;
2057 info->input_signal_events.dsr_up++;
2058 } else {
2059 info->signals &= ~SerialSignal_DSR;
2060 info->input_signal_events.dsr_down++;
2061 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002062 DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
2063 if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2064 slgt_irq_off(info, IRQ_DSR);
2065 return;
2066 }
2067 info->icount.dsr++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002068 wake_up_interruptible(&info->status_event_wait_q);
2069 wake_up_interruptible(&info->event_wait_q);
2070 info->pending_bh |= BH_STATUS;
2071}
2072
Paul Fulghumed8485f2008-02-06 01:37:18 -08002073static void cts_change(struct slgt_info *info, unsigned short status)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002074{
Paul Fulghumed8485f2008-02-06 01:37:18 -08002075 if (status & BIT2) {
2076 info->signals |= SerialSignal_CTS;
2077 info->input_signal_events.cts_up++;
2078 } else {
2079 info->signals &= ~SerialSignal_CTS;
2080 info->input_signal_events.cts_down++;
2081 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002082 DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
2083 if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2084 slgt_irq_off(info, IRQ_CTS);
2085 return;
2086 }
2087 info->icount.cts++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002088 wake_up_interruptible(&info->status_event_wait_q);
2089 wake_up_interruptible(&info->event_wait_q);
2090 info->pending_bh |= BH_STATUS;
2091
Alan Cox8fb06c72008-07-16 21:56:46 +01002092 if (info->port.flags & ASYNC_CTS_FLOW) {
2093 if (info->port.tty) {
2094 if (info->port.tty->hw_stopped) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002095 if (info->signals & SerialSignal_CTS) {
Alan Cox8fb06c72008-07-16 21:56:46 +01002096 info->port.tty->hw_stopped = 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002097 info->pending_bh |= BH_TRANSMIT;
2098 return;
2099 }
2100 } else {
2101 if (!(info->signals & SerialSignal_CTS))
Alan Cox8fb06c72008-07-16 21:56:46 +01002102 info->port.tty->hw_stopped = 1;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002103 }
2104 }
2105 }
2106}
2107
Paul Fulghumed8485f2008-02-06 01:37:18 -08002108static void dcd_change(struct slgt_info *info, unsigned short status)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002109{
Paul Fulghumed8485f2008-02-06 01:37:18 -08002110 if (status & BIT1) {
2111 info->signals |= SerialSignal_DCD;
2112 info->input_signal_events.dcd_up++;
2113 } else {
2114 info->signals &= ~SerialSignal_DCD;
2115 info->input_signal_events.dcd_down++;
2116 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002117 DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
2118 if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2119 slgt_irq_off(info, IRQ_DCD);
2120 return;
2121 }
2122 info->icount.dcd++;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002123#if SYNCLINK_GENERIC_HDLC
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07002124 if (info->netcount) {
2125 if (info->signals & SerialSignal_DCD)
2126 netif_carrier_on(info->netdev);
2127 else
2128 netif_carrier_off(info->netdev);
2129 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002130#endif
2131 wake_up_interruptible(&info->status_event_wait_q);
2132 wake_up_interruptible(&info->event_wait_q);
2133 info->pending_bh |= BH_STATUS;
2134
Alan Cox8fb06c72008-07-16 21:56:46 +01002135 if (info->port.flags & ASYNC_CHECK_CD) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002136 if (info->signals & SerialSignal_DCD)
Alan Cox8fb06c72008-07-16 21:56:46 +01002137 wake_up_interruptible(&info->port.open_wait);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002138 else {
Alan Cox8fb06c72008-07-16 21:56:46 +01002139 if (info->port.tty)
2140 tty_hangup(info->port.tty);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002141 }
2142 }
2143}
2144
Paul Fulghumed8485f2008-02-06 01:37:18 -08002145static void ri_change(struct slgt_info *info, unsigned short status)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002146{
Paul Fulghumed8485f2008-02-06 01:37:18 -08002147 if (status & BIT0) {
2148 info->signals |= SerialSignal_RI;
2149 info->input_signal_events.ri_up++;
2150 } else {
2151 info->signals &= ~SerialSignal_RI;
2152 info->input_signal_events.ri_down++;
2153 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002154 DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
2155 if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2156 slgt_irq_off(info, IRQ_RI);
2157 return;
2158 }
Paul Fulghumed8485f2008-02-06 01:37:18 -08002159 info->icount.rng++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002160 wake_up_interruptible(&info->status_event_wait_q);
2161 wake_up_interruptible(&info->event_wait_q);
2162 info->pending_bh |= BH_STATUS;
2163}
2164
2165static void isr_serial(struct slgt_info *info)
2166{
2167 unsigned short status = rd_reg16(info, SSR);
2168
2169 DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
2170
2171 wr_reg16(info, SSR, status); /* clear pending */
2172
Joe Perches0fab6de2008-04-28 02:14:02 -07002173 info->irq_occurred = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002174
2175 if (info->params.mode == MGSL_MODE_ASYNC) {
2176 if (status & IRQ_TXIDLE) {
2177 if (info->tx_count)
2178 isr_txeom(info, status);
2179 }
2180 if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
2181 info->icount.brk++;
2182 /* process break detection if tty control allows */
Alan Cox8fb06c72008-07-16 21:56:46 +01002183 if (info->port.tty) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002184 if (!(status & info->ignore_status_mask)) {
2185 if (info->read_status_mask & MASK_BREAK) {
Alan Cox8fb06c72008-07-16 21:56:46 +01002186 tty_insert_flip_char(info->port.tty, 0, TTY_BREAK);
2187 if (info->port.flags & ASYNC_SAK)
2188 do_SAK(info->port.tty);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002189 }
2190 }
2191 }
2192 }
2193 } else {
2194 if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
2195 isr_txeom(info, status);
2196
2197 if (status & IRQ_RXIDLE) {
2198 if (status & RXIDLE)
2199 info->icount.rxidle++;
2200 else
2201 info->icount.exithunt++;
2202 wake_up_interruptible(&info->event_wait_q);
2203 }
2204
2205 if (status & IRQ_RXOVER)
2206 rx_start(info);
2207 }
2208
2209 if (status & IRQ_DSR)
Paul Fulghumed8485f2008-02-06 01:37:18 -08002210 dsr_change(info, status);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002211 if (status & IRQ_CTS)
Paul Fulghumed8485f2008-02-06 01:37:18 -08002212 cts_change(info, status);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002213 if (status & IRQ_DCD)
Paul Fulghumed8485f2008-02-06 01:37:18 -08002214 dcd_change(info, status);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002215 if (status & IRQ_RI)
Paul Fulghumed8485f2008-02-06 01:37:18 -08002216 ri_change(info, status);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002217}
2218
2219static void isr_rdma(struct slgt_info *info)
2220{
2221 unsigned int status = rd_reg32(info, RDCSR);
2222
2223 DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
2224
2225 /* RDCSR (rx DMA control/status)
2226 *
2227 * 31..07 reserved
2228 * 06 save status byte to DMA buffer
2229 * 05 error
2230 * 04 eol (end of list)
2231 * 03 eob (end of buffer)
2232 * 02 IRQ enable
2233 * 01 reset
2234 * 00 enable
2235 */
2236 wr_reg32(info, RDCSR, status); /* clear pending */
2237
2238 if (status & (BIT5 + BIT4)) {
2239 DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
Joe Perches0fab6de2008-04-28 02:14:02 -07002240 info->rx_restart = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002241 }
2242 info->pending_bh |= BH_RECEIVE;
2243}
2244
2245static void isr_tdma(struct slgt_info *info)
2246{
2247 unsigned int status = rd_reg32(info, TDCSR);
2248
2249 DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
2250
2251 /* TDCSR (tx DMA control/status)
2252 *
2253 * 31..06 reserved
2254 * 05 error
2255 * 04 eol (end of list)
2256 * 03 eob (end of buffer)
2257 * 02 IRQ enable
2258 * 01 reset
2259 * 00 enable
2260 */
2261 wr_reg32(info, TDCSR, status); /* clear pending */
2262
2263 if (status & (BIT5 + BIT4 + BIT3)) {
2264 // another transmit buffer has completed
2265 // run bottom half to get more send data from user
2266 info->pending_bh |= BH_TRANSMIT;
2267 }
2268}
2269
2270static void isr_txeom(struct slgt_info *info, unsigned short status)
2271{
2272 DBGISR(("%s txeom status=%04x\n", info->device_name, status));
2273
2274 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
2275 tdma_reset(info);
2276 reset_tbufs(info);
2277 if (status & IRQ_TXUNDER) {
2278 unsigned short val = rd_reg16(info, TCR);
2279 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
2280 wr_reg16(info, TCR, val); /* clear reset bit */
2281 }
2282
2283 if (info->tx_active) {
2284 if (info->params.mode != MGSL_MODE_ASYNC) {
2285 if (status & IRQ_TXUNDER)
2286 info->icount.txunder++;
2287 else if (status & IRQ_TXIDLE)
2288 info->icount.txok++;
2289 }
2290
Joe Perches0fab6de2008-04-28 02:14:02 -07002291 info->tx_active = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002292 info->tx_count = 0;
2293
2294 del_timer(&info->tx_timer);
2295
2296 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
2297 info->signals &= ~SerialSignal_RTS;
Joe Perches0fab6de2008-04-28 02:14:02 -07002298 info->drop_rts_on_tx_done = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002299 set_signals(info);
2300 }
2301
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002302#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08002303 if (info->netcount)
2304 hdlcdev_tx_done(info);
2305 else
2306#endif
2307 {
Alan Cox8fb06c72008-07-16 21:56:46 +01002308 if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002309 tx_stop(info);
2310 return;
2311 }
2312 info->pending_bh |= BH_TRANSMIT;
2313 }
2314 }
2315}
2316
Paul Fulghum0080b7a2006-03-28 01:56:15 -08002317static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
2318{
2319 struct cond_wait *w, *prev;
2320
2321 /* wake processes waiting for specific transitions */
2322 for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
2323 if (w->data & changed) {
2324 w->data = state;
2325 wake_up_interruptible(&w->q);
2326 if (prev != NULL)
2327 prev->next = w->next;
2328 else
2329 info->gpio_wait_q = w->next;
2330 } else
2331 prev = w;
2332 }
2333}
2334
Paul Fulghum705b6c72006-01-08 01:02:06 -08002335/* interrupt service routine
2336 *
2337 * irq interrupt number
2338 * dev_id device ID supplied during interrupt registration
Paul Fulghum705b6c72006-01-08 01:02:06 -08002339 */
Jeff Garzika6f97b22007-10-31 05:20:49 -04002340static irqreturn_t slgt_interrupt(int dummy, void *dev_id)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002341{
Jeff Garzika6f97b22007-10-31 05:20:49 -04002342 struct slgt_info *info = dev_id;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002343 unsigned int gsr;
2344 unsigned int i;
2345
Jeff Garzika6f97b22007-10-31 05:20:49 -04002346 DBGISR(("slgt_interrupt irq=%d entry\n", info->irq_level));
Paul Fulghum705b6c72006-01-08 01:02:06 -08002347
2348 spin_lock(&info->lock);
2349
2350 while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
2351 DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
Joe Perches0fab6de2008-04-28 02:14:02 -07002352 info->irq_occurred = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002353 for(i=0; i < info->port_count ; i++) {
2354 if (info->port_array[i] == NULL)
2355 continue;
2356 if (gsr & (BIT8 << i))
2357 isr_serial(info->port_array[i]);
2358 if (gsr & (BIT16 << (i*2)))
2359 isr_rdma(info->port_array[i]);
2360 if (gsr & (BIT17 << (i*2)))
2361 isr_tdma(info->port_array[i]);
2362 }
2363 }
2364
Paul Fulghum0080b7a2006-03-28 01:56:15 -08002365 if (info->gpio_present) {
2366 unsigned int state;
2367 unsigned int changed;
2368 while ((changed = rd_reg32(info, IOSR)) != 0) {
2369 DBGISR(("%s iosr=%08x\n", info->device_name, changed));
2370 /* read latched state of GPIO signals */
2371 state = rd_reg32(info, IOVR);
2372 /* clear pending GPIO interrupt bits */
2373 wr_reg32(info, IOSR, changed);
2374 for (i=0 ; i < info->port_count ; i++) {
2375 if (info->port_array[i] != NULL)
2376 isr_gpio(info->port_array[i], changed, state);
2377 }
2378 }
2379 }
2380
Paul Fulghum705b6c72006-01-08 01:02:06 -08002381 for(i=0; i < info->port_count ; i++) {
2382 struct slgt_info *port = info->port_array[i];
2383
Alan Cox8fb06c72008-07-16 21:56:46 +01002384 if (port && (port->port.count || port->netcount) &&
Paul Fulghum705b6c72006-01-08 01:02:06 -08002385 port->pending_bh && !port->bh_running &&
2386 !port->bh_requested) {
2387 DBGISR(("%s bh queued\n", port->device_name));
2388 schedule_work(&port->task);
Joe Perches0fab6de2008-04-28 02:14:02 -07002389 port->bh_requested = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002390 }
2391 }
2392
2393 spin_unlock(&info->lock);
2394
Jeff Garzika6f97b22007-10-31 05:20:49 -04002395 DBGISR(("slgt_interrupt irq=%d exit\n", info->irq_level));
Paul Fulghum705b6c72006-01-08 01:02:06 -08002396 return IRQ_HANDLED;
2397}
2398
2399static int startup(struct slgt_info *info)
2400{
2401 DBGINFO(("%s startup\n", info->device_name));
2402
Alan Cox8fb06c72008-07-16 21:56:46 +01002403 if (info->port.flags & ASYNC_INITIALIZED)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002404 return 0;
2405
2406 if (!info->tx_buf) {
2407 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
2408 if (!info->tx_buf) {
2409 DBGERR(("%s can't allocate tx buffer\n", info->device_name));
2410 return -ENOMEM;
2411 }
2412 }
2413
2414 info->pending_bh = 0;
2415
2416 memset(&info->icount, 0, sizeof(info->icount));
2417
2418 /* program hardware for current parameters */
2419 change_params(info);
2420
Alan Cox8fb06c72008-07-16 21:56:46 +01002421 if (info->port.tty)
2422 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002423
Alan Cox8fb06c72008-07-16 21:56:46 +01002424 info->port.flags |= ASYNC_INITIALIZED;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002425
2426 return 0;
2427}
2428
2429/*
2430 * called by close() and hangup() to shutdown hardware
2431 */
2432static void shutdown(struct slgt_info *info)
2433{
2434 unsigned long flags;
2435
Alan Cox8fb06c72008-07-16 21:56:46 +01002436 if (!(info->port.flags & ASYNC_INITIALIZED))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002437 return;
2438
2439 DBGINFO(("%s shutdown\n", info->device_name));
2440
2441 /* clear status wait queue because status changes */
2442 /* can't happen after shutting down the hardware */
2443 wake_up_interruptible(&info->status_event_wait_q);
2444 wake_up_interruptible(&info->event_wait_q);
2445
2446 del_timer_sync(&info->tx_timer);
2447 del_timer_sync(&info->rx_timer);
2448
2449 kfree(info->tx_buf);
2450 info->tx_buf = NULL;
2451
2452 spin_lock_irqsave(&info->lock,flags);
2453
2454 tx_stop(info);
2455 rx_stop(info);
2456
2457 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
2458
Alan Cox8fb06c72008-07-16 21:56:46 +01002459 if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002460 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2461 set_signals(info);
2462 }
2463
Paul Fulghum0080b7a2006-03-28 01:56:15 -08002464 flush_cond_wait(&info->gpio_wait_q);
2465
Paul Fulghum705b6c72006-01-08 01:02:06 -08002466 spin_unlock_irqrestore(&info->lock,flags);
2467
Alan Cox8fb06c72008-07-16 21:56:46 +01002468 if (info->port.tty)
2469 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002470
Alan Cox8fb06c72008-07-16 21:56:46 +01002471 info->port.flags &= ~ASYNC_INITIALIZED;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002472}
2473
2474static void program_hw(struct slgt_info *info)
2475{
2476 unsigned long flags;
2477
2478 spin_lock_irqsave(&info->lock,flags);
2479
2480 rx_stop(info);
2481 tx_stop(info);
2482
Paul Fulghumcb10dc92006-09-30 23:27:45 -07002483 if (info->params.mode != MGSL_MODE_ASYNC ||
Paul Fulghum705b6c72006-01-08 01:02:06 -08002484 info->netcount)
Paul Fulghumcb10dc92006-09-30 23:27:45 -07002485 sync_mode(info);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002486 else
2487 async_mode(info);
2488
2489 set_signals(info);
2490
2491 info->dcd_chkcount = 0;
2492 info->cts_chkcount = 0;
2493 info->ri_chkcount = 0;
2494 info->dsr_chkcount = 0;
2495
2496 slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR);
2497 get_signals(info);
2498
2499 if (info->netcount ||
Alan Cox8fb06c72008-07-16 21:56:46 +01002500 (info->port.tty && info->port.tty->termios->c_cflag & CREAD))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002501 rx_start(info);
2502
2503 spin_unlock_irqrestore(&info->lock,flags);
2504}
2505
2506/*
2507 * reconfigure adapter based on new parameters
2508 */
2509static void change_params(struct slgt_info *info)
2510{
2511 unsigned cflag;
2512 int bits_per_char;
2513
Alan Cox8fb06c72008-07-16 21:56:46 +01002514 if (!info->port.tty || !info->port.tty->termios)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002515 return;
2516 DBGINFO(("%s change_params\n", info->device_name));
2517
Alan Cox8fb06c72008-07-16 21:56:46 +01002518 cflag = info->port.tty->termios->c_cflag;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002519
2520 /* if B0 rate (hangup) specified then negate DTR and RTS */
2521 /* otherwise assert DTR and RTS */
2522 if (cflag & CBAUD)
2523 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
2524 else
2525 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2526
2527 /* byte size and parity */
2528
2529 switch (cflag & CSIZE) {
2530 case CS5: info->params.data_bits = 5; break;
2531 case CS6: info->params.data_bits = 6; break;
2532 case CS7: info->params.data_bits = 7; break;
2533 case CS8: info->params.data_bits = 8; break;
2534 default: info->params.data_bits = 7; break;
2535 }
2536
2537 info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
2538
2539 if (cflag & PARENB)
2540 info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
2541 else
2542 info->params.parity = ASYNC_PARITY_NONE;
2543
2544 /* calculate number of jiffies to transmit a full
2545 * FIFO (32 bytes) at specified data rate
2546 */
2547 bits_per_char = info->params.data_bits +
2548 info->params.stop_bits + 1;
2549
Alan Cox8fb06c72008-07-16 21:56:46 +01002550 info->params.data_rate = tty_get_baud_rate(info->port.tty);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002551
2552 if (info->params.data_rate) {
2553 info->timeout = (32*HZ*bits_per_char) /
2554 info->params.data_rate;
2555 }
2556 info->timeout += HZ/50; /* Add .02 seconds of slop */
2557
2558 if (cflag & CRTSCTS)
Alan Cox8fb06c72008-07-16 21:56:46 +01002559 info->port.flags |= ASYNC_CTS_FLOW;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002560 else
Alan Cox8fb06c72008-07-16 21:56:46 +01002561 info->port.flags &= ~ASYNC_CTS_FLOW;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002562
2563 if (cflag & CLOCAL)
Alan Cox8fb06c72008-07-16 21:56:46 +01002564 info->port.flags &= ~ASYNC_CHECK_CD;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002565 else
Alan Cox8fb06c72008-07-16 21:56:46 +01002566 info->port.flags |= ASYNC_CHECK_CD;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002567
2568 /* process tty input control flags */
2569
2570 info->read_status_mask = IRQ_RXOVER;
Alan Cox8fb06c72008-07-16 21:56:46 +01002571 if (I_INPCK(info->port.tty))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002572 info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
Alan Cox8fb06c72008-07-16 21:56:46 +01002573 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002574 info->read_status_mask |= MASK_BREAK;
Alan Cox8fb06c72008-07-16 21:56:46 +01002575 if (I_IGNPAR(info->port.tty))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002576 info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
Alan Cox8fb06c72008-07-16 21:56:46 +01002577 if (I_IGNBRK(info->port.tty)) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002578 info->ignore_status_mask |= MASK_BREAK;
2579 /* If ignoring parity and break indicators, ignore
2580 * overruns too. (For real raw support).
2581 */
Alan Cox8fb06c72008-07-16 21:56:46 +01002582 if (I_IGNPAR(info->port.tty))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002583 info->ignore_status_mask |= MASK_OVERRUN;
2584 }
2585
2586 program_hw(info);
2587}
2588
2589static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
2590{
2591 DBGINFO(("%s get_stats\n", info->device_name));
2592 if (!user_icount) {
2593 memset(&info->icount, 0, sizeof(info->icount));
2594 } else {
2595 if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
2596 return -EFAULT;
2597 }
2598 return 0;
2599}
2600
2601static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
2602{
2603 DBGINFO(("%s get_params\n", info->device_name));
2604 if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
2605 return -EFAULT;
2606 return 0;
2607}
2608
2609static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
2610{
2611 unsigned long flags;
2612 MGSL_PARAMS tmp_params;
2613
2614 DBGINFO(("%s set_params\n", info->device_name));
2615 if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
2616 return -EFAULT;
2617
2618 spin_lock_irqsave(&info->lock, flags);
2619 memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
2620 spin_unlock_irqrestore(&info->lock, flags);
2621
2622 change_params(info);
2623
2624 return 0;
2625}
2626
2627static int get_txidle(struct slgt_info *info, int __user *idle_mode)
2628{
2629 DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
2630 if (put_user(info->idle_mode, idle_mode))
2631 return -EFAULT;
2632 return 0;
2633}
2634
2635static int set_txidle(struct slgt_info *info, int idle_mode)
2636{
2637 unsigned long flags;
2638 DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
2639 spin_lock_irqsave(&info->lock,flags);
2640 info->idle_mode = idle_mode;
Paul Fulghum643f3312006-06-25 05:49:20 -07002641 if (info->params.mode != MGSL_MODE_ASYNC)
2642 tx_set_idle(info);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002643 spin_unlock_irqrestore(&info->lock,flags);
2644 return 0;
2645}
2646
2647static int tx_enable(struct slgt_info *info, int enable)
2648{
2649 unsigned long flags;
2650 DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
2651 spin_lock_irqsave(&info->lock,flags);
2652 if (enable) {
2653 if (!info->tx_enabled)
2654 tx_start(info);
2655 } else {
2656 if (info->tx_enabled)
2657 tx_stop(info);
2658 }
2659 spin_unlock_irqrestore(&info->lock,flags);
2660 return 0;
2661}
2662
2663/*
2664 * abort transmit HDLC frame
2665 */
2666static int tx_abort(struct slgt_info *info)
2667{
2668 unsigned long flags;
2669 DBGINFO(("%s tx_abort\n", info->device_name));
2670 spin_lock_irqsave(&info->lock,flags);
2671 tdma_reset(info);
2672 spin_unlock_irqrestore(&info->lock,flags);
2673 return 0;
2674}
2675
2676static int rx_enable(struct slgt_info *info, int enable)
2677{
2678 unsigned long flags;
Paul Fulghum814dae02008-07-22 11:22:14 +01002679 unsigned int rbuf_fill_level;
2680 DBGINFO(("%s rx_enable(%08x)\n", info->device_name, enable));
Paul Fulghum705b6c72006-01-08 01:02:06 -08002681 spin_lock_irqsave(&info->lock,flags);
Paul Fulghum814dae02008-07-22 11:22:14 +01002682 /*
2683 * enable[31..16] = receive DMA buffer fill level
2684 * 0 = noop (leave fill level unchanged)
2685 * fill level must be multiple of 4 and <= buffer size
2686 */
2687 rbuf_fill_level = ((unsigned int)enable) >> 16;
2688 if (rbuf_fill_level) {
2689 if ((rbuf_fill_level > DMABUFSIZE) || (rbuf_fill_level % 4))
2690 return -EINVAL;
2691 info->rbuf_fill_level = rbuf_fill_level;
2692 rx_stop(info); /* restart receiver to use new fill level */
2693 }
2694
2695 /*
2696 * enable[1..0] = receiver enable command
2697 * 0 = disable
2698 * 1 = enable
2699 * 2 = enable or force hunt mode if already enabled
2700 */
2701 enable &= 3;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002702 if (enable) {
2703 if (!info->rx_enabled)
2704 rx_start(info);
Paul Fulghumcb10dc92006-09-30 23:27:45 -07002705 else if (enable == 2) {
2706 /* force hunt mode (write 1 to RCR[3]) */
2707 wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
2708 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002709 } else {
2710 if (info->rx_enabled)
2711 rx_stop(info);
2712 }
2713 spin_unlock_irqrestore(&info->lock,flags);
2714 return 0;
2715}
2716
2717/*
2718 * wait for specified event to occur
2719 */
2720static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
2721{
2722 unsigned long flags;
2723 int s;
2724 int rc=0;
2725 struct mgsl_icount cprev, cnow;
2726 int events;
2727 int mask;
2728 struct _input_signal_events oldsigs, newsigs;
2729 DECLARE_WAITQUEUE(wait, current);
2730
2731 if (get_user(mask, mask_ptr))
2732 return -EFAULT;
2733
2734 DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
2735
2736 spin_lock_irqsave(&info->lock,flags);
2737
2738 /* return immediately if state matches requested events */
2739 get_signals(info);
2740 s = info->signals;
2741
2742 events = mask &
2743 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2744 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2745 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2746 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2747 if (events) {
2748 spin_unlock_irqrestore(&info->lock,flags);
2749 goto exit;
2750 }
2751
2752 /* save current irq counts */
2753 cprev = info->icount;
2754 oldsigs = info->input_signal_events;
2755
2756 /* enable hunt and idle irqs if needed */
2757 if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
2758 unsigned short val = rd_reg16(info, SCR);
2759 if (!(val & IRQ_RXIDLE))
2760 wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
2761 }
2762
2763 set_current_state(TASK_INTERRUPTIBLE);
2764 add_wait_queue(&info->event_wait_q, &wait);
2765
2766 spin_unlock_irqrestore(&info->lock,flags);
2767
2768 for(;;) {
2769 schedule();
2770 if (signal_pending(current)) {
2771 rc = -ERESTARTSYS;
2772 break;
2773 }
2774
2775 /* get current irq counts */
2776 spin_lock_irqsave(&info->lock,flags);
2777 cnow = info->icount;
2778 newsigs = info->input_signal_events;
2779 set_current_state(TASK_INTERRUPTIBLE);
2780 spin_unlock_irqrestore(&info->lock,flags);
2781
2782 /* if no change, wait aborted for some reason */
2783 if (newsigs.dsr_up == oldsigs.dsr_up &&
2784 newsigs.dsr_down == oldsigs.dsr_down &&
2785 newsigs.dcd_up == oldsigs.dcd_up &&
2786 newsigs.dcd_down == oldsigs.dcd_down &&
2787 newsigs.cts_up == oldsigs.cts_up &&
2788 newsigs.cts_down == oldsigs.cts_down &&
2789 newsigs.ri_up == oldsigs.ri_up &&
2790 newsigs.ri_down == oldsigs.ri_down &&
2791 cnow.exithunt == cprev.exithunt &&
2792 cnow.rxidle == cprev.rxidle) {
2793 rc = -EIO;
2794 break;
2795 }
2796
2797 events = mask &
2798 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2799 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2800 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2801 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2802 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2803 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2804 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2805 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2806 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2807 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2808 if (events)
2809 break;
2810
2811 cprev = cnow;
2812 oldsigs = newsigs;
2813 }
2814
2815 remove_wait_queue(&info->event_wait_q, &wait);
2816 set_current_state(TASK_RUNNING);
2817
2818
2819 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2820 spin_lock_irqsave(&info->lock,flags);
2821 if (!waitqueue_active(&info->event_wait_q)) {
2822 /* disable enable exit hunt mode/idle rcvd IRQs */
2823 wr_reg16(info, SCR,
2824 (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
2825 }
2826 spin_unlock_irqrestore(&info->lock,flags);
2827 }
2828exit:
2829 if (rc == 0)
2830 rc = put_user(events, mask_ptr);
2831 return rc;
2832}
2833
2834static int get_interface(struct slgt_info *info, int __user *if_mode)
2835{
2836 DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
2837 if (put_user(info->if_mode, if_mode))
2838 return -EFAULT;
2839 return 0;
2840}
2841
2842static int set_interface(struct slgt_info *info, int if_mode)
2843{
2844 unsigned long flags;
Paul Fulghum35fbd392006-01-18 17:42:24 -08002845 unsigned short val;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002846
2847 DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
2848 spin_lock_irqsave(&info->lock,flags);
2849 info->if_mode = if_mode;
2850
2851 msc_set_vcr(info);
2852
2853 /* TCR (tx control) 07 1=RTS driver control */
2854 val = rd_reg16(info, TCR);
2855 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
2856 val |= BIT7;
2857 else
2858 val &= ~BIT7;
2859 wr_reg16(info, TCR, val);
2860
2861 spin_unlock_irqrestore(&info->lock,flags);
2862 return 0;
2863}
2864
Paul Fulghum0080b7a2006-03-28 01:56:15 -08002865/*
2866 * set general purpose IO pin state and direction
2867 *
2868 * user_gpio fields:
2869 * state each bit indicates a pin state
2870 * smask set bit indicates pin state to set
2871 * dir each bit indicates a pin direction (0=input, 1=output)
2872 * dmask set bit indicates pin direction to set
2873 */
2874static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2875{
2876 unsigned long flags;
2877 struct gpio_desc gpio;
2878 __u32 data;
2879
2880 if (!info->gpio_present)
2881 return -EINVAL;
2882 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2883 return -EFAULT;
2884 DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
2885 info->device_name, gpio.state, gpio.smask,
2886 gpio.dir, gpio.dmask));
2887
2888 spin_lock_irqsave(&info->lock,flags);
2889 if (gpio.dmask) {
2890 data = rd_reg32(info, IODR);
2891 data |= gpio.dmask & gpio.dir;
2892 data &= ~(gpio.dmask & ~gpio.dir);
2893 wr_reg32(info, IODR, data);
2894 }
2895 if (gpio.smask) {
2896 data = rd_reg32(info, IOVR);
2897 data |= gpio.smask & gpio.state;
2898 data &= ~(gpio.smask & ~gpio.state);
2899 wr_reg32(info, IOVR, data);
2900 }
2901 spin_unlock_irqrestore(&info->lock,flags);
2902
2903 return 0;
2904}
2905
2906/*
2907 * get general purpose IO pin state and direction
2908 */
2909static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2910{
2911 struct gpio_desc gpio;
2912 if (!info->gpio_present)
2913 return -EINVAL;
2914 gpio.state = rd_reg32(info, IOVR);
2915 gpio.smask = 0xffffffff;
2916 gpio.dir = rd_reg32(info, IODR);
2917 gpio.dmask = 0xffffffff;
2918 if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
2919 return -EFAULT;
2920 DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
2921 info->device_name, gpio.state, gpio.dir));
2922 return 0;
2923}
2924
2925/*
2926 * conditional wait facility
2927 */
2928static void init_cond_wait(struct cond_wait *w, unsigned int data)
2929{
2930 init_waitqueue_head(&w->q);
2931 init_waitqueue_entry(&w->wait, current);
2932 w->data = data;
2933}
2934
2935static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
2936{
2937 set_current_state(TASK_INTERRUPTIBLE);
2938 add_wait_queue(&w->q, &w->wait);
2939 w->next = *head;
2940 *head = w;
2941}
2942
2943static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
2944{
2945 struct cond_wait *w, *prev;
2946 remove_wait_queue(&cw->q, &cw->wait);
2947 set_current_state(TASK_RUNNING);
2948 for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
2949 if (w == cw) {
2950 if (prev != NULL)
2951 prev->next = w->next;
2952 else
2953 *head = w->next;
2954 break;
2955 }
2956 }
2957}
2958
2959static void flush_cond_wait(struct cond_wait **head)
2960{
2961 while (*head != NULL) {
2962 wake_up_interruptible(&(*head)->q);
2963 *head = (*head)->next;
2964 }
2965}
2966
2967/*
2968 * wait for general purpose I/O pin(s) to enter specified state
2969 *
2970 * user_gpio fields:
2971 * state - bit indicates target pin state
2972 * smask - set bit indicates watched pin
2973 *
2974 * The wait ends when at least one watched pin enters the specified
2975 * state. When 0 (no error) is returned, user_gpio->state is set to the
2976 * state of all GPIO pins when the wait ends.
2977 *
2978 * Note: Each pin may be a dedicated input, dedicated output, or
2979 * configurable input/output. The number and configuration of pins
2980 * varies with the specific adapter model. Only input pins (dedicated
2981 * or configured) can be monitored with this function.
2982 */
2983static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2984{
2985 unsigned long flags;
2986 int rc = 0;
2987 struct gpio_desc gpio;
2988 struct cond_wait wait;
2989 u32 state;
2990
2991 if (!info->gpio_present)
2992 return -EINVAL;
2993 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2994 return -EFAULT;
2995 DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
2996 info->device_name, gpio.state, gpio.smask));
2997 /* ignore output pins identified by set IODR bit */
2998 if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
2999 return -EINVAL;
3000 init_cond_wait(&wait, gpio.smask);
3001
3002 spin_lock_irqsave(&info->lock, flags);
3003 /* enable interrupts for watched pins */
3004 wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
3005 /* get current pin states */
3006 state = rd_reg32(info, IOVR);
3007
3008 if (gpio.smask & ~(state ^ gpio.state)) {
3009 /* already in target state */
3010 gpio.state = state;
3011 } else {
3012 /* wait for target state */
3013 add_cond_wait(&info->gpio_wait_q, &wait);
3014 spin_unlock_irqrestore(&info->lock, flags);
3015 schedule();
3016 if (signal_pending(current))
3017 rc = -ERESTARTSYS;
3018 else
3019 gpio.state = wait.data;
3020 spin_lock_irqsave(&info->lock, flags);
3021 remove_cond_wait(&info->gpio_wait_q, &wait);
3022 }
3023
3024 /* disable all GPIO interrupts if no waiting processes */
3025 if (info->gpio_wait_q == NULL)
3026 wr_reg32(info, IOER, 0);
3027 spin_unlock_irqrestore(&info->lock,flags);
3028
3029 if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
3030 rc = -EFAULT;
3031 return rc;
3032}
3033
Paul Fulghum705b6c72006-01-08 01:02:06 -08003034static int modem_input_wait(struct slgt_info *info,int arg)
3035{
3036 unsigned long flags;
3037 int rc;
3038 struct mgsl_icount cprev, cnow;
3039 DECLARE_WAITQUEUE(wait, current);
3040
3041 /* save current irq counts */
3042 spin_lock_irqsave(&info->lock,flags);
3043 cprev = info->icount;
3044 add_wait_queue(&info->status_event_wait_q, &wait);
3045 set_current_state(TASK_INTERRUPTIBLE);
3046 spin_unlock_irqrestore(&info->lock,flags);
3047
3048 for(;;) {
3049 schedule();
3050 if (signal_pending(current)) {
3051 rc = -ERESTARTSYS;
3052 break;
3053 }
3054
3055 /* get new irq counts */
3056 spin_lock_irqsave(&info->lock,flags);
3057 cnow = info->icount;
3058 set_current_state(TASK_INTERRUPTIBLE);
3059 spin_unlock_irqrestore(&info->lock,flags);
3060
3061 /* if no change, wait aborted for some reason */
3062 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
3063 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
3064 rc = -EIO;
3065 break;
3066 }
3067
3068 /* check for change in caller specified modem input */
3069 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
3070 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
3071 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
3072 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
3073 rc = 0;
3074 break;
3075 }
3076
3077 cprev = cnow;
3078 }
3079 remove_wait_queue(&info->status_event_wait_q, &wait);
3080 set_current_state(TASK_RUNNING);
3081 return rc;
3082}
3083
3084/*
3085 * return state of serial control and status signals
3086 */
3087static int tiocmget(struct tty_struct *tty, struct file *file)
3088{
3089 struct slgt_info *info = tty->driver_data;
3090 unsigned int result;
3091 unsigned long flags;
3092
3093 spin_lock_irqsave(&info->lock,flags);
3094 get_signals(info);
3095 spin_unlock_irqrestore(&info->lock,flags);
3096
3097 result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
3098 ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
3099 ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
3100 ((info->signals & SerialSignal_RI) ? TIOCM_RNG:0) +
3101 ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
3102 ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
3103
3104 DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
3105 return result;
3106}
3107
3108/*
3109 * set modem control signals (DTR/RTS)
3110 *
3111 * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
3112 * TIOCMSET = set/clear signal values
3113 * value bit mask for command
3114 */
3115static int tiocmset(struct tty_struct *tty, struct file *file,
3116 unsigned int set, unsigned int clear)
3117{
3118 struct slgt_info *info = tty->driver_data;
3119 unsigned long flags;
3120
3121 DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
3122
3123 if (set & TIOCM_RTS)
3124 info->signals |= SerialSignal_RTS;
3125 if (set & TIOCM_DTR)
3126 info->signals |= SerialSignal_DTR;
3127 if (clear & TIOCM_RTS)
3128 info->signals &= ~SerialSignal_RTS;
3129 if (clear & TIOCM_DTR)
3130 info->signals &= ~SerialSignal_DTR;
3131
3132 spin_lock_irqsave(&info->lock,flags);
3133 set_signals(info);
3134 spin_unlock_irqrestore(&info->lock,flags);
3135 return 0;
3136}
3137
3138/*
3139 * block current process until the device is ready to open
3140 */
3141static int block_til_ready(struct tty_struct *tty, struct file *filp,
3142 struct slgt_info *info)
3143{
3144 DECLARE_WAITQUEUE(wait, current);
3145 int retval;
Joe Perches0fab6de2008-04-28 02:14:02 -07003146 bool do_clocal = false;
3147 bool extra_count = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003148 unsigned long flags;
3149
3150 DBGINFO(("%s block_til_ready\n", tty->driver->name));
3151
3152 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3153 /* nonblock mode is set or port is not enabled */
Alan Cox8fb06c72008-07-16 21:56:46 +01003154 info->port.flags |= ASYNC_NORMAL_ACTIVE;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003155 return 0;
3156 }
3157
3158 if (tty->termios->c_cflag & CLOCAL)
Joe Perches0fab6de2008-04-28 02:14:02 -07003159 do_clocal = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003160
3161 /* Wait for carrier detect and the line to become
3162 * free (i.e., not in use by the callout). While we are in
Alan Cox8fb06c72008-07-16 21:56:46 +01003163 * this loop, info->port.count is dropped by one, so that
Paul Fulghum705b6c72006-01-08 01:02:06 -08003164 * close() knows when to free things. We restore it upon
3165 * exit, either normal or abnormal.
3166 */
3167
3168 retval = 0;
Alan Cox8fb06c72008-07-16 21:56:46 +01003169 add_wait_queue(&info->port.open_wait, &wait);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003170
3171 spin_lock_irqsave(&info->lock, flags);
3172 if (!tty_hung_up_p(filp)) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003173 extra_count = true;
Alan Cox8fb06c72008-07-16 21:56:46 +01003174 info->port.count--;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003175 }
3176 spin_unlock_irqrestore(&info->lock, flags);
Alan Cox8fb06c72008-07-16 21:56:46 +01003177 info->port.blocked_open++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003178
3179 while (1) {
3180 if ((tty->termios->c_cflag & CBAUD)) {
3181 spin_lock_irqsave(&info->lock,flags);
3182 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
3183 set_signals(info);
3184 spin_unlock_irqrestore(&info->lock,flags);
3185 }
3186
3187 set_current_state(TASK_INTERRUPTIBLE);
3188
Alan Cox8fb06c72008-07-16 21:56:46 +01003189 if (tty_hung_up_p(filp) || !(info->port.flags & ASYNC_INITIALIZED)){
3190 retval = (info->port.flags & ASYNC_HUP_NOTIFY) ?
Paul Fulghum705b6c72006-01-08 01:02:06 -08003191 -EAGAIN : -ERESTARTSYS;
3192 break;
3193 }
3194
3195 spin_lock_irqsave(&info->lock,flags);
3196 get_signals(info);
3197 spin_unlock_irqrestore(&info->lock,flags);
3198
Alan Cox8fb06c72008-07-16 21:56:46 +01003199 if (!(info->port.flags & ASYNC_CLOSING) &&
Paul Fulghum705b6c72006-01-08 01:02:06 -08003200 (do_clocal || (info->signals & SerialSignal_DCD)) ) {
3201 break;
3202 }
3203
3204 if (signal_pending(current)) {
3205 retval = -ERESTARTSYS;
3206 break;
3207 }
3208
3209 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
3210 schedule();
3211 }
3212
3213 set_current_state(TASK_RUNNING);
Alan Cox8fb06c72008-07-16 21:56:46 +01003214 remove_wait_queue(&info->port.open_wait, &wait);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003215
3216 if (extra_count)
Alan Cox8fb06c72008-07-16 21:56:46 +01003217 info->port.count++;
3218 info->port.blocked_open--;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003219
3220 if (!retval)
Alan Cox8fb06c72008-07-16 21:56:46 +01003221 info->port.flags |= ASYNC_NORMAL_ACTIVE;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003222
3223 DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
3224 return retval;
3225}
3226
3227static int alloc_tmp_rbuf(struct slgt_info *info)
3228{
Paul Fulghum04b374d2006-06-25 05:49:21 -07003229 info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003230 if (info->tmp_rbuf == NULL)
3231 return -ENOMEM;
3232 return 0;
3233}
3234
3235static void free_tmp_rbuf(struct slgt_info *info)
3236{
3237 kfree(info->tmp_rbuf);
3238 info->tmp_rbuf = NULL;
3239}
3240
3241/*
3242 * allocate DMA descriptor lists.
3243 */
3244static int alloc_desc(struct slgt_info *info)
3245{
3246 unsigned int i;
3247 unsigned int pbufs;
3248
3249 /* allocate memory to hold descriptor lists */
3250 info->bufs = pci_alloc_consistent(info->pdev, DESC_LIST_SIZE, &info->bufs_dma_addr);
3251 if (info->bufs == NULL)
3252 return -ENOMEM;
3253
3254 memset(info->bufs, 0, DESC_LIST_SIZE);
3255
3256 info->rbufs = (struct slgt_desc*)info->bufs;
3257 info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
3258
3259 pbufs = (unsigned int)info->bufs_dma_addr;
3260
3261 /*
3262 * Build circular lists of descriptors
3263 */
3264
3265 for (i=0; i < info->rbuf_count; i++) {
3266 /* physical address of this descriptor */
3267 info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
3268
3269 /* physical address of next descriptor */
3270 if (i == info->rbuf_count - 1)
3271 info->rbufs[i].next = cpu_to_le32(pbufs);
3272 else
3273 info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
3274 set_desc_count(info->rbufs[i], DMABUFSIZE);
3275 }
3276
3277 for (i=0; i < info->tbuf_count; i++) {
3278 /* physical address of this descriptor */
3279 info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
3280
3281 /* physical address of next descriptor */
3282 if (i == info->tbuf_count - 1)
3283 info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
3284 else
3285 info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
3286 }
3287
3288 return 0;
3289}
3290
3291static void free_desc(struct slgt_info *info)
3292{
3293 if (info->bufs != NULL) {
3294 pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
3295 info->bufs = NULL;
3296 info->rbufs = NULL;
3297 info->tbufs = NULL;
3298 }
3299}
3300
3301static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3302{
3303 int i;
3304 for (i=0; i < count; i++) {
3305 if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
3306 return -ENOMEM;
3307 bufs[i].pbuf = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
3308 }
3309 return 0;
3310}
3311
3312static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3313{
3314 int i;
3315 for (i=0; i < count; i++) {
3316 if (bufs[i].buf == NULL)
3317 continue;
3318 pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
3319 bufs[i].buf = NULL;
3320 }
3321}
3322
3323static int alloc_dma_bufs(struct slgt_info *info)
3324{
3325 info->rbuf_count = 32;
3326 info->tbuf_count = 32;
3327
3328 if (alloc_desc(info) < 0 ||
3329 alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
3330 alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
3331 alloc_tmp_rbuf(info) < 0) {
3332 DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
3333 return -ENOMEM;
3334 }
3335 reset_rbufs(info);
3336 return 0;
3337}
3338
3339static void free_dma_bufs(struct slgt_info *info)
3340{
3341 if (info->bufs) {
3342 free_bufs(info, info->rbufs, info->rbuf_count);
3343 free_bufs(info, info->tbufs, info->tbuf_count);
3344 free_desc(info);
3345 }
3346 free_tmp_rbuf(info);
3347}
3348
3349static int claim_resources(struct slgt_info *info)
3350{
3351 if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
3352 DBGERR(("%s reg addr conflict, addr=%08X\n",
3353 info->device_name, info->phys_reg_addr));
3354 info->init_error = DiagStatus_AddressConflict;
3355 goto errout;
3356 }
3357 else
Joe Perches0fab6de2008-04-28 02:14:02 -07003358 info->reg_addr_requested = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003359
Alan Cox24cb2332008-04-30 00:54:19 -07003360 info->reg_addr = ioremap_nocache(info->phys_reg_addr, SLGT_REG_SIZE);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003361 if (!info->reg_addr) {
3362 DBGERR(("%s cant map device registers, addr=%08X\n",
3363 info->device_name, info->phys_reg_addr));
3364 info->init_error = DiagStatus_CantAssignPciResources;
3365 goto errout;
3366 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08003367 return 0;
3368
3369errout:
3370 release_resources(info);
3371 return -ENODEV;
3372}
3373
3374static void release_resources(struct slgt_info *info)
3375{
3376 if (info->irq_requested) {
3377 free_irq(info->irq_level, info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003378 info->irq_requested = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003379 }
3380
3381 if (info->reg_addr_requested) {
3382 release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
Joe Perches0fab6de2008-04-28 02:14:02 -07003383 info->reg_addr_requested = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003384 }
3385
3386 if (info->reg_addr) {
Paul Fulghum0c8365e2006-01-11 12:17:39 -08003387 iounmap(info->reg_addr);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003388 info->reg_addr = NULL;
3389 }
3390}
3391
3392/* Add the specified device instance data structure to the
3393 * global linked list of devices and increment the device count.
3394 */
3395static void add_device(struct slgt_info *info)
3396{
3397 char *devstr;
3398
3399 info->next_device = NULL;
3400 info->line = slgt_device_count;
3401 sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
3402
3403 if (info->line < MAX_DEVICES) {
3404 if (maxframe[info->line])
3405 info->max_frame_size = maxframe[info->line];
3406 info->dosyncppp = dosyncppp[info->line];
3407 }
3408
3409 slgt_device_count++;
3410
3411 if (!slgt_device_list)
3412 slgt_device_list = info;
3413 else {
3414 struct slgt_info *current_dev = slgt_device_list;
3415 while(current_dev->next_device)
3416 current_dev = current_dev->next_device;
3417 current_dev->next_device = info;
3418 }
3419
3420 if (info->max_frame_size < 4096)
3421 info->max_frame_size = 4096;
3422 else if (info->max_frame_size > 65535)
3423 info->max_frame_size = 65535;
3424
3425 switch(info->pdev->device) {
3426 case SYNCLINK_GT_DEVICE_ID:
3427 devstr = "GT";
3428 break;
Paul Fulghum6f84be82006-06-25 05:49:22 -07003429 case SYNCLINK_GT2_DEVICE_ID:
3430 devstr = "GT2";
3431 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003432 case SYNCLINK_GT4_DEVICE_ID:
3433 devstr = "GT4";
3434 break;
3435 case SYNCLINK_AC_DEVICE_ID:
3436 devstr = "AC";
3437 info->params.mode = MGSL_MODE_ASYNC;
3438 break;
3439 default:
3440 devstr = "(unknown model)";
3441 }
3442 printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
3443 devstr, info->device_name, info->phys_reg_addr,
3444 info->irq_level, info->max_frame_size);
3445
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003446#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08003447 hdlcdev_init(info);
3448#endif
3449}
3450
3451/*
3452 * allocate device instance structure, return NULL on failure
3453 */
3454static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
3455{
3456 struct slgt_info *info;
3457
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003458 info = kzalloc(sizeof(struct slgt_info), GFP_KERNEL);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003459
3460 if (!info) {
3461 DBGERR(("%s device alloc failed adapter=%d port=%d\n",
3462 driver_name, adapter_num, port_num));
3463 } else {
Alan Cox44b7d1b2008-07-16 21:57:18 +01003464 tty_port_init(&info->port);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003465 info->magic = MGSL_MAGIC;
David Howellsc4028952006-11-22 14:57:56 +00003466 INIT_WORK(&info->task, bh_handler);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003467 info->max_frame_size = 4096;
Paul Fulghum814dae02008-07-22 11:22:14 +01003468 info->rbuf_fill_level = DMABUFSIZE;
Alan Cox44b7d1b2008-07-16 21:57:18 +01003469 info->port.close_delay = 5*HZ/10;
3470 info->port.closing_wait = 30*HZ;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003471 init_waitqueue_head(&info->status_event_wait_q);
3472 init_waitqueue_head(&info->event_wait_q);
3473 spin_lock_init(&info->netlock);
3474 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
3475 info->idle_mode = HDLC_TXIDLE_FLAGS;
3476 info->adapter_num = adapter_num;
3477 info->port_num = port_num;
3478
Jiri Slaby40565f12007-02-12 00:52:31 -08003479 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
3480 setup_timer(&info->rx_timer, rx_timeout, (unsigned long)info);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003481
3482 /* Copy configuration info to device instance data */
3483 info->pdev = pdev;
3484 info->irq_level = pdev->irq;
3485 info->phys_reg_addr = pci_resource_start(pdev,0);
3486
Paul Fulghum705b6c72006-01-08 01:02:06 -08003487 info->bus_type = MGSL_BUS_TYPE_PCI;
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -07003488 info->irq_flags = IRQF_SHARED;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003489
3490 info->init_error = -1; /* assume error, set to 0 on successful init */
3491 }
3492
3493 return info;
3494}
3495
3496static void device_init(int adapter_num, struct pci_dev *pdev)
3497{
3498 struct slgt_info *port_array[SLGT_MAX_PORTS];
3499 int i;
3500 int port_count = 1;
3501
Paul Fulghum6f84be82006-06-25 05:49:22 -07003502 if (pdev->device == SYNCLINK_GT2_DEVICE_ID)
3503 port_count = 2;
3504 else if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
Paul Fulghum705b6c72006-01-08 01:02:06 -08003505 port_count = 4;
3506
3507 /* allocate device instances for all ports */
3508 for (i=0; i < port_count; ++i) {
3509 port_array[i] = alloc_dev(adapter_num, i, pdev);
3510 if (port_array[i] == NULL) {
3511 for (--i; i >= 0; --i)
3512 kfree(port_array[i]);
3513 return;
3514 }
3515 }
3516
3517 /* give copy of port_array to all ports and add to device list */
3518 for (i=0; i < port_count; ++i) {
3519 memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
3520 add_device(port_array[i]);
3521 port_array[i]->port_count = port_count;
3522 spin_lock_init(&port_array[i]->lock);
3523 }
3524
3525 /* Allocate and claim adapter resources */
3526 if (!claim_resources(port_array[0])) {
3527
3528 alloc_dma_bufs(port_array[0]);
3529
3530 /* copy resource information from first port to others */
3531 for (i = 1; i < port_count; ++i) {
3532 port_array[i]->lock = port_array[0]->lock;
3533 port_array[i]->irq_level = port_array[0]->irq_level;
3534 port_array[i]->reg_addr = port_array[0]->reg_addr;
3535 alloc_dma_bufs(port_array[i]);
3536 }
3537
3538 if (request_irq(port_array[0]->irq_level,
3539 slgt_interrupt,
3540 port_array[0]->irq_flags,
3541 port_array[0]->device_name,
3542 port_array[0]) < 0) {
3543 DBGERR(("%s request_irq failed IRQ=%d\n",
3544 port_array[0]->device_name,
3545 port_array[0]->irq_level));
3546 } else {
Joe Perches0fab6de2008-04-28 02:14:02 -07003547 port_array[0]->irq_requested = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003548 adapter_test(port_array[0]);
Paul Fulghum0080b7a2006-03-28 01:56:15 -08003549 for (i=1 ; i < port_count ; i++) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08003550 port_array[i]->init_error = port_array[0]->init_error;
Paul Fulghum0080b7a2006-03-28 01:56:15 -08003551 port_array[i]->gpio_present = port_array[0]->gpio_present;
3552 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08003553 }
3554 }
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003555
3556 for (i=0; i < port_count; ++i)
3557 tty_register_device(serial_driver, port_array[i]->line, &(port_array[i]->pdev->dev));
Paul Fulghum705b6c72006-01-08 01:02:06 -08003558}
3559
3560static int __devinit init_one(struct pci_dev *dev,
3561 const struct pci_device_id *ent)
3562{
3563 if (pci_enable_device(dev)) {
3564 printk("error enabling pci device %p\n", dev);
3565 return -EIO;
3566 }
3567 pci_set_master(dev);
3568 device_init(slgt_device_count, dev);
3569 return 0;
3570}
3571
3572static void __devexit remove_one(struct pci_dev *dev)
3573{
3574}
3575
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003576static const struct tty_operations ops = {
Paul Fulghum705b6c72006-01-08 01:02:06 -08003577 .open = open,
3578 .close = close,
3579 .write = write,
3580 .put_char = put_char,
3581 .flush_chars = flush_chars,
3582 .write_room = write_room,
3583 .chars_in_buffer = chars_in_buffer,
3584 .flush_buffer = flush_buffer,
3585 .ioctl = ioctl,
Paul Fulghum2acdb162007-05-10 22:22:43 -07003586 .compat_ioctl = slgt_compat_ioctl,
Paul Fulghum705b6c72006-01-08 01:02:06 -08003587 .throttle = throttle,
3588 .unthrottle = unthrottle,
3589 .send_xchar = send_xchar,
3590 .break_ctl = set_break,
3591 .wait_until_sent = wait_until_sent,
3592 .read_proc = read_proc,
3593 .set_termios = set_termios,
3594 .stop = tx_hold,
3595 .start = tx_release,
3596 .hangup = hangup,
3597 .tiocmget = tiocmget,
3598 .tiocmset = tiocmset,
3599};
3600
3601static void slgt_cleanup(void)
3602{
3603 int rc;
3604 struct slgt_info *info;
3605 struct slgt_info *tmp;
3606
3607 printk("unload %s %s\n", driver_name, driver_version);
3608
3609 if (serial_driver) {
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003610 for (info=slgt_device_list ; info != NULL ; info=info->next_device)
3611 tty_unregister_device(serial_driver, info->line);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003612 if ((rc = tty_unregister_driver(serial_driver)))
3613 DBGERR(("tty_unregister_driver error=%d\n", rc));
3614 put_tty_driver(serial_driver);
3615 }
3616
3617 /* reset devices */
3618 info = slgt_device_list;
3619 while(info) {
3620 reset_port(info);
3621 info = info->next_device;
3622 }
3623
3624 /* release devices */
3625 info = slgt_device_list;
3626 while(info) {
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003627#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08003628 hdlcdev_exit(info);
3629#endif
3630 free_dma_bufs(info);
3631 free_tmp_rbuf(info);
3632 if (info->port_num == 0)
3633 release_resources(info);
3634 tmp = info;
3635 info = info->next_device;
3636 kfree(tmp);
3637 }
3638
3639 if (pci_registered)
3640 pci_unregister_driver(&pci_driver);
3641}
3642
3643/*
3644 * Driver initialization entry point.
3645 */
3646static int __init slgt_init(void)
3647{
3648 int rc;
3649
3650 printk("%s %s\n", driver_name, driver_version);
3651
Paul Fulghum705b6c72006-01-08 01:02:06 -08003652 serial_driver = alloc_tty_driver(MAX_DEVICES);
3653 if (!serial_driver) {
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003654 printk("%s can't allocate tty driver\n", driver_name);
3655 return -ENOMEM;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003656 }
3657
3658 /* Initialize the tty_driver structure */
3659
3660 serial_driver->owner = THIS_MODULE;
3661 serial_driver->driver_name = tty_driver_name;
3662 serial_driver->name = tty_dev_prefix;
3663 serial_driver->major = ttymajor;
3664 serial_driver->minor_start = 64;
3665 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3666 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3667 serial_driver->init_termios = tty_std_termios;
3668 serial_driver->init_termios.c_cflag =
3669 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08003670 serial_driver->init_termios.c_ispeed = 9600;
3671 serial_driver->init_termios.c_ospeed = 9600;
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003672 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003673 tty_set_operations(serial_driver, &ops);
3674 if ((rc = tty_register_driver(serial_driver)) < 0) {
3675 DBGERR(("%s can't register serial driver\n", driver_name));
3676 put_tty_driver(serial_driver);
3677 serial_driver = NULL;
3678 goto error;
3679 }
3680
3681 printk("%s %s, tty major#%d\n",
3682 driver_name, driver_version,
3683 serial_driver->major);
3684
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003685 slgt_device_count = 0;
3686 if ((rc = pci_register_driver(&pci_driver)) < 0) {
3687 printk("%s pci_register_driver error=%d\n", driver_name, rc);
3688 goto error;
3689 }
Joe Perches0fab6de2008-04-28 02:14:02 -07003690 pci_registered = true;
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003691
3692 if (!slgt_device_list)
3693 printk("%s no devices found\n",driver_name);
3694
Paul Fulghum705b6c72006-01-08 01:02:06 -08003695 return 0;
3696
3697error:
3698 slgt_cleanup();
3699 return rc;
3700}
3701
3702static void __exit slgt_exit(void)
3703{
3704 slgt_cleanup();
3705}
3706
3707module_init(slgt_init);
3708module_exit(slgt_exit);
3709
3710/*
3711 * register access routines
3712 */
3713
3714#define CALC_REGADDR() \
3715 unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
3716 if (addr >= 0x80) \
3717 reg_addr += (info->port_num) * 32;
3718
3719static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
3720{
3721 CALC_REGADDR();
3722 return readb((void __iomem *)reg_addr);
3723}
3724
3725static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
3726{
3727 CALC_REGADDR();
3728 writeb(value, (void __iomem *)reg_addr);
3729}
3730
3731static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
3732{
3733 CALC_REGADDR();
3734 return readw((void __iomem *)reg_addr);
3735}
3736
3737static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
3738{
3739 CALC_REGADDR();
3740 writew(value, (void __iomem *)reg_addr);
3741}
3742
3743static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
3744{
3745 CALC_REGADDR();
3746 return readl((void __iomem *)reg_addr);
3747}
3748
3749static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
3750{
3751 CALC_REGADDR();
3752 writel(value, (void __iomem *)reg_addr);
3753}
3754
3755static void rdma_reset(struct slgt_info *info)
3756{
3757 unsigned int i;
3758
3759 /* set reset bit */
3760 wr_reg32(info, RDCSR, BIT1);
3761
3762 /* wait for enable bit cleared */
3763 for(i=0 ; i < 1000 ; i++)
3764 if (!(rd_reg32(info, RDCSR) & BIT0))
3765 break;
3766}
3767
3768static void tdma_reset(struct slgt_info *info)
3769{
3770 unsigned int i;
3771
3772 /* set reset bit */
3773 wr_reg32(info, TDCSR, BIT1);
3774
3775 /* wait for enable bit cleared */
3776 for(i=0 ; i < 1000 ; i++)
3777 if (!(rd_reg32(info, TDCSR) & BIT0))
3778 break;
3779}
3780
3781/*
3782 * enable internal loopback
3783 * TxCLK and RxCLK are generated from BRG
3784 * and TxD is looped back to RxD internally.
3785 */
3786static void enable_loopback(struct slgt_info *info)
3787{
3788 /* SCR (serial control) BIT2=looopback enable */
3789 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
3790
3791 if (info->params.mode != MGSL_MODE_ASYNC) {
3792 /* CCR (clock control)
3793 * 07..05 tx clock source (010 = BRG)
3794 * 04..02 rx clock source (010 = BRG)
3795 * 01 auxclk enable (0 = disable)
3796 * 00 BRG enable (1 = enable)
3797 *
3798 * 0100 1001
3799 */
3800 wr_reg8(info, CCR, 0x49);
3801
3802 /* set speed if available, otherwise use default */
3803 if (info->params.clock_speed)
3804 set_rate(info, info->params.clock_speed);
3805 else
3806 set_rate(info, 3686400);
3807 }
3808}
3809
3810/*
3811 * set baud rate generator to specified rate
3812 */
3813static void set_rate(struct slgt_info *info, u32 rate)
3814{
3815 unsigned int div;
3816 static unsigned int osc = 14745600;
3817
3818 /* div = osc/rate - 1
3819 *
3820 * Round div up if osc/rate is not integer to
3821 * force to next slowest rate.
3822 */
3823
3824 if (rate) {
3825 div = osc/rate;
3826 if (!(osc % rate) && div)
3827 div--;
3828 wr_reg16(info, BDR, (unsigned short)div);
3829 }
3830}
3831
3832static void rx_stop(struct slgt_info *info)
3833{
3834 unsigned short val;
3835
3836 /* disable and reset receiver */
3837 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3838 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3839 wr_reg16(info, RCR, val); /* clear reset bit */
3840
3841 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
3842
3843 /* clear pending rx interrupts */
3844 wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
3845
3846 rdma_reset(info);
3847
Joe Perches0fab6de2008-04-28 02:14:02 -07003848 info->rx_enabled = false;
3849 info->rx_restart = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003850}
3851
3852static void rx_start(struct slgt_info *info)
3853{
3854 unsigned short val;
3855
3856 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
3857
3858 /* clear pending rx overrun IRQ */
3859 wr_reg16(info, SSR, IRQ_RXOVER);
3860
3861 /* reset and disable receiver */
3862 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3863 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3864 wr_reg16(info, RCR, val); /* clear reset bit */
3865
3866 rdma_reset(info);
3867 reset_rbufs(info);
3868
3869 /* set 1st descriptor address */
3870 wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
3871
3872 if (info->params.mode != MGSL_MODE_ASYNC) {
3873 /* enable rx DMA and DMA interrupt */
3874 wr_reg32(info, RDCSR, (BIT2 + BIT0));
3875 } else {
3876 /* enable saving of rx status, rx DMA and DMA interrupt */
3877 wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
3878 }
3879
3880 slgt_irq_on(info, IRQ_RXOVER);
3881
3882 /* enable receiver */
3883 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
3884
Joe Perches0fab6de2008-04-28 02:14:02 -07003885 info->rx_restart = false;
3886 info->rx_enabled = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003887}
3888
3889static void tx_start(struct slgt_info *info)
3890{
3891 if (!info->tx_enabled) {
3892 wr_reg16(info, TCR,
Paul Fulghumcb10dc92006-09-30 23:27:45 -07003893 (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
Joe Perches0fab6de2008-04-28 02:14:02 -07003894 info->tx_enabled = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003895 }
3896
3897 if (info->tx_count) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003898 info->drop_rts_on_tx_done = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003899
3900 if (info->params.mode != MGSL_MODE_ASYNC) {
3901 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3902 get_signals(info);
3903 if (!(info->signals & SerialSignal_RTS)) {
3904 info->signals |= SerialSignal_RTS;
3905 set_signals(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003906 info->drop_rts_on_tx_done = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003907 }
3908 }
3909
3910 slgt_irq_off(info, IRQ_TXDATA);
3911 slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
3912 /* clear tx idle and underrun status bits */
3913 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
Jiri Slaby40565f12007-02-12 00:52:31 -08003914 if (info->params.mode == MGSL_MODE_HDLC)
3915 mod_timer(&info->tx_timer, jiffies +
3916 msecs_to_jiffies(5000));
Paul Fulghum705b6c72006-01-08 01:02:06 -08003917 } else {
Paul Fulghum705b6c72006-01-08 01:02:06 -08003918 slgt_irq_off(info, IRQ_TXDATA);
3919 slgt_irq_on(info, IRQ_TXIDLE);
3920 /* clear tx idle status bit */
3921 wr_reg16(info, SSR, IRQ_TXIDLE);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003922 }
Paul Fulghumbb029c62007-07-31 00:37:35 -07003923 tdma_start(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003924 info->tx_active = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003925 }
3926}
3927
Paul Fulghumbb029c62007-07-31 00:37:35 -07003928/*
3929 * start transmit DMA if inactive and there are unsent buffers
3930 */
3931static void tdma_start(struct slgt_info *info)
3932{
3933 unsigned int i;
3934
3935 if (rd_reg32(info, TDCSR) & BIT0)
3936 return;
3937
3938 /* transmit DMA inactive, check for unsent buffers */
3939 i = info->tbuf_start;
3940 while (!desc_count(info->tbufs[i])) {
3941 if (++i == info->tbuf_count)
3942 i = 0;
3943 if (i == info->tbuf_current)
3944 return;
3945 }
3946 info->tbuf_start = i;
3947
3948 /* there are unsent buffers, start transmit DMA */
3949
3950 /* reset needed if previous error condition */
3951 tdma_reset(info);
3952
3953 /* set 1st descriptor address */
3954 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
Paul Fulghum8a38c282008-07-22 11:21:28 +01003955 wr_reg32(info, TDCSR, BIT2 + BIT0); /* IRQ + DMA enable */
Paul Fulghumbb029c62007-07-31 00:37:35 -07003956}
3957
Paul Fulghum705b6c72006-01-08 01:02:06 -08003958static void tx_stop(struct slgt_info *info)
3959{
3960 unsigned short val;
3961
3962 del_timer(&info->tx_timer);
3963
3964 tdma_reset(info);
3965
3966 /* reset and disable transmitter */
3967 val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
3968 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
Paul Fulghum705b6c72006-01-08 01:02:06 -08003969
3970 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
3971
3972 /* clear tx idle and underrun status bit */
3973 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
3974
3975 reset_tbufs(info);
3976
Joe Perches0fab6de2008-04-28 02:14:02 -07003977 info->tx_enabled = false;
3978 info->tx_active = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003979}
3980
3981static void reset_port(struct slgt_info *info)
3982{
3983 if (!info->reg_addr)
3984 return;
3985
3986 tx_stop(info);
3987 rx_stop(info);
3988
3989 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
3990 set_signals(info);
3991
3992 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
3993}
3994
3995static void reset_adapter(struct slgt_info *info)
3996{
3997 int i;
3998 for (i=0; i < info->port_count; ++i) {
3999 if (info->port_array[i])
4000 reset_port(info->port_array[i]);
4001 }
4002}
4003
4004static void async_mode(struct slgt_info *info)
4005{
4006 unsigned short val;
4007
4008 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4009 tx_stop(info);
4010 rx_stop(info);
4011
4012 /* TCR (tx control)
4013 *
4014 * 15..13 mode, 010=async
4015 * 12..10 encoding, 000=NRZ
4016 * 09 parity enable
4017 * 08 1=odd parity, 0=even parity
4018 * 07 1=RTS driver control
4019 * 06 1=break enable
4020 * 05..04 character length
4021 * 00=5 bits
4022 * 01=6 bits
4023 * 10=7 bits
4024 * 11=8 bits
4025 * 03 0=1 stop bit, 1=2 stop bits
4026 * 02 reset
4027 * 01 enable
4028 * 00 auto-CTS enable
4029 */
4030 val = 0x4000;
4031
4032 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4033 val |= BIT7;
4034
4035 if (info->params.parity != ASYNC_PARITY_NONE) {
4036 val |= BIT9;
4037 if (info->params.parity == ASYNC_PARITY_ODD)
4038 val |= BIT8;
4039 }
4040
4041 switch (info->params.data_bits)
4042 {
4043 case 6: val |= BIT4; break;
4044 case 7: val |= BIT5; break;
4045 case 8: val |= BIT5 + BIT4; break;
4046 }
4047
4048 if (info->params.stop_bits != 1)
4049 val |= BIT3;
4050
4051 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4052 val |= BIT0;
4053
4054 wr_reg16(info, TCR, val);
4055
4056 /* RCR (rx control)
4057 *
4058 * 15..13 mode, 010=async
4059 * 12..10 encoding, 000=NRZ
4060 * 09 parity enable
4061 * 08 1=odd parity, 0=even parity
4062 * 07..06 reserved, must be 0
4063 * 05..04 character length
4064 * 00=5 bits
4065 * 01=6 bits
4066 * 10=7 bits
4067 * 11=8 bits
4068 * 03 reserved, must be zero
4069 * 02 reset
4070 * 01 enable
4071 * 00 auto-DCD enable
4072 */
4073 val = 0x4000;
4074
4075 if (info->params.parity != ASYNC_PARITY_NONE) {
4076 val |= BIT9;
4077 if (info->params.parity == ASYNC_PARITY_ODD)
4078 val |= BIT8;
4079 }
4080
4081 switch (info->params.data_bits)
4082 {
4083 case 6: val |= BIT4; break;
4084 case 7: val |= BIT5; break;
4085 case 8: val |= BIT5 + BIT4; break;
4086 }
4087
4088 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4089 val |= BIT0;
4090
4091 wr_reg16(info, RCR, val);
4092
4093 /* CCR (clock control)
4094 *
4095 * 07..05 011 = tx clock source is BRG/16
4096 * 04..02 010 = rx clock source is BRG
4097 * 01 0 = auxclk disabled
4098 * 00 1 = BRG enabled
4099 *
4100 * 0110 1001
4101 */
4102 wr_reg8(info, CCR, 0x69);
4103
4104 msc_set_vcr(info);
4105
Paul Fulghum705b6c72006-01-08 01:02:06 -08004106 /* SCR (serial control)
4107 *
4108 * 15 1=tx req on FIFO half empty
4109 * 14 1=rx req on FIFO half full
4110 * 13 tx data IRQ enable
4111 * 12 tx idle IRQ enable
4112 * 11 rx break on IRQ enable
4113 * 10 rx data IRQ enable
4114 * 09 rx break off IRQ enable
4115 * 08 overrun IRQ enable
4116 * 07 DSR IRQ enable
4117 * 06 CTS IRQ enable
4118 * 05 DCD IRQ enable
4119 * 04 RI IRQ enable
4120 * 03 reserved, must be zero
4121 * 02 1=txd->rxd internal loopback enable
4122 * 01 reserved, must be zero
4123 * 00 1=master IRQ enable
4124 */
4125 val = BIT15 + BIT14 + BIT0;
4126 wr_reg16(info, SCR, val);
4127
4128 slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
4129
4130 set_rate(info, info->params.data_rate * 16);
4131
4132 if (info->params.loopback)
4133 enable_loopback(info);
4134}
4135
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004136static void sync_mode(struct slgt_info *info)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004137{
4138 unsigned short val;
4139
4140 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4141 tx_stop(info);
4142 rx_stop(info);
4143
4144 /* TCR (tx control)
4145 *
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004146 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
Paul Fulghum705b6c72006-01-08 01:02:06 -08004147 * 12..10 encoding
4148 * 09 CRC enable
4149 * 08 CRC32
4150 * 07 1=RTS driver control
4151 * 06 preamble enable
4152 * 05..04 preamble length
4153 * 03 share open/close flag
4154 * 02 reset
4155 * 01 enable
4156 * 00 auto-CTS enable
4157 */
Paul Fulghum993456c2008-07-22 11:22:04 +01004158 val = BIT2;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004159
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004160 switch(info->params.mode) {
4161 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4162 case MGSL_MODE_BISYNC: val |= BIT15; break;
4163 case MGSL_MODE_RAW: val |= BIT13; break;
4164 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08004165 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4166 val |= BIT7;
4167
4168 switch(info->params.encoding)
4169 {
4170 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4171 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4172 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4173 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4174 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4175 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4176 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4177 }
4178
Paul Fulghum04b374d2006-06-25 05:49:21 -07004179 switch (info->params.crc_type & HDLC_CRC_MASK)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004180 {
4181 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4182 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4183 }
4184
4185 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
4186 val |= BIT6;
4187
4188 switch (info->params.preamble_length)
4189 {
4190 case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
4191 case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
4192 case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
4193 }
4194
4195 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4196 val |= BIT0;
4197
4198 wr_reg16(info, TCR, val);
4199
4200 /* TPR (transmit preamble) */
4201
4202 switch (info->params.preamble)
4203 {
4204 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
4205 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
4206 case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
4207 case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
4208 case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
4209 default: val = 0x7e; break;
4210 }
4211 wr_reg8(info, TPR, (unsigned char)val);
4212
4213 /* RCR (rx control)
4214 *
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004215 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
Paul Fulghum705b6c72006-01-08 01:02:06 -08004216 * 12..10 encoding
4217 * 09 CRC enable
4218 * 08 CRC32
4219 * 07..03 reserved, must be 0
4220 * 02 reset
4221 * 01 enable
4222 * 00 auto-DCD enable
4223 */
4224 val = 0;
4225
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004226 switch(info->params.mode) {
4227 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4228 case MGSL_MODE_BISYNC: val |= BIT15; break;
4229 case MGSL_MODE_RAW: val |= BIT13; break;
4230 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08004231
4232 switch(info->params.encoding)
4233 {
4234 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4235 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4236 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4237 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4238 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4239 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4240 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4241 }
4242
Paul Fulghum04b374d2006-06-25 05:49:21 -07004243 switch (info->params.crc_type & HDLC_CRC_MASK)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004244 {
4245 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4246 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4247 }
4248
4249 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4250 val |= BIT0;
4251
4252 wr_reg16(info, RCR, val);
4253
4254 /* CCR (clock control)
4255 *
4256 * 07..05 tx clock source
4257 * 04..02 rx clock source
4258 * 01 auxclk enable
4259 * 00 BRG enable
4260 */
4261 val = 0;
4262
4263 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4264 {
4265 // when RxC source is DPLL, BRG generates 16X DPLL
4266 // reference clock, so take TxC from BRG/16 to get
4267 // transmit clock at actual data rate
4268 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4269 val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
4270 else
4271 val |= BIT6; /* 010, txclk = BRG */
4272 }
4273 else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4274 val |= BIT7; /* 100, txclk = DPLL Input */
4275 else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4276 val |= BIT5; /* 001, txclk = RXC Input */
4277
4278 if (info->params.flags & HDLC_FLAG_RXC_BRG)
4279 val |= BIT3; /* 010, rxclk = BRG */
4280 else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4281 val |= BIT4; /* 100, rxclk = DPLL */
4282 else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4283 val |= BIT2; /* 001, rxclk = TXC Input */
4284
4285 if (info->params.clock_speed)
4286 val |= BIT1 + BIT0;
4287
4288 wr_reg8(info, CCR, (unsigned char)val);
4289
4290 if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
4291 {
4292 // program DPLL mode
4293 switch(info->params.encoding)
4294 {
4295 case HDLC_ENCODING_BIPHASE_MARK:
4296 case HDLC_ENCODING_BIPHASE_SPACE:
4297 val = BIT7; break;
4298 case HDLC_ENCODING_BIPHASE_LEVEL:
4299 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
4300 val = BIT7 + BIT6; break;
4301 default: val = BIT6; // NRZ encodings
4302 }
4303 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
4304
4305 // DPLL requires a 16X reference clock from BRG
4306 set_rate(info, info->params.clock_speed * 16);
4307 }
4308 else
4309 set_rate(info, info->params.clock_speed);
4310
4311 tx_set_idle(info);
4312
4313 msc_set_vcr(info);
4314
4315 /* SCR (serial control)
4316 *
4317 * 15 1=tx req on FIFO half empty
4318 * 14 1=rx req on FIFO half full
4319 * 13 tx data IRQ enable
4320 * 12 tx idle IRQ enable
4321 * 11 underrun IRQ enable
4322 * 10 rx data IRQ enable
4323 * 09 rx idle IRQ enable
4324 * 08 overrun IRQ enable
4325 * 07 DSR IRQ enable
4326 * 06 CTS IRQ enable
4327 * 05 DCD IRQ enable
4328 * 04 RI IRQ enable
4329 * 03 reserved, must be zero
4330 * 02 1=txd->rxd internal loopback enable
4331 * 01 reserved, must be zero
4332 * 00 1=master IRQ enable
4333 */
4334 wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
4335
4336 if (info->params.loopback)
4337 enable_loopback(info);
4338}
4339
4340/*
4341 * set transmit idle mode
4342 */
4343static void tx_set_idle(struct slgt_info *info)
4344{
Paul Fulghum643f3312006-06-25 05:49:20 -07004345 unsigned char val;
4346 unsigned short tcr;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004347
Paul Fulghum643f3312006-06-25 05:49:20 -07004348 /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
4349 * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
4350 */
4351 tcr = rd_reg16(info, TCR);
4352 if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
4353 /* disable preamble, set idle size to 16 bits */
4354 tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
4355 /* MSB of 16 bit idle specified in tx preamble register (TPR) */
4356 wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
4357 } else if (!(tcr & BIT6)) {
4358 /* preamble is disabled, set idle size to 8 bits */
4359 tcr &= ~(BIT5 + BIT4);
4360 }
4361 wr_reg16(info, TCR, tcr);
4362
4363 if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
4364 /* LSB of custom tx idle specified in tx idle register */
4365 val = (unsigned char)(info->idle_mode & 0xff);
4366 } else {
4367 /* standard 8 bit idle patterns */
4368 switch(info->idle_mode)
4369 {
4370 case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
4371 case HDLC_TXIDLE_ALT_ZEROS_ONES:
4372 case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
4373 case HDLC_TXIDLE_ZEROS:
4374 case HDLC_TXIDLE_SPACE: val = 0x00; break;
4375 default: val = 0xff;
4376 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08004377 }
4378
4379 wr_reg8(info, TIR, val);
4380}
4381
4382/*
4383 * get state of V24 status (input) signals
4384 */
4385static void get_signals(struct slgt_info *info)
4386{
4387 unsigned short status = rd_reg16(info, SSR);
4388
4389 /* clear all serial signals except DTR and RTS */
4390 info->signals &= SerialSignal_DTR + SerialSignal_RTS;
4391
4392 if (status & BIT3)
4393 info->signals |= SerialSignal_DSR;
4394 if (status & BIT2)
4395 info->signals |= SerialSignal_CTS;
4396 if (status & BIT1)
4397 info->signals |= SerialSignal_DCD;
4398 if (status & BIT0)
4399 info->signals |= SerialSignal_RI;
4400}
4401
4402/*
4403 * set V.24 Control Register based on current configuration
4404 */
4405static void msc_set_vcr(struct slgt_info *info)
4406{
4407 unsigned char val = 0;
4408
4409 /* VCR (V.24 control)
4410 *
4411 * 07..04 serial IF select
4412 * 03 DTR
4413 * 02 RTS
4414 * 01 LL
4415 * 00 RL
4416 */
4417
4418 switch(info->if_mode & MGSL_INTERFACE_MASK)
4419 {
4420 case MGSL_INTERFACE_RS232:
4421 val |= BIT5; /* 0010 */
4422 break;
4423 case MGSL_INTERFACE_V35:
4424 val |= BIT7 + BIT6 + BIT5; /* 1110 */
4425 break;
4426 case MGSL_INTERFACE_RS422:
4427 val |= BIT6; /* 0100 */
4428 break;
4429 }
4430
Paul Fulghume5590712008-07-22 11:21:39 +01004431 if (info->if_mode & MGSL_INTERFACE_MSB_FIRST)
4432 val |= BIT4;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004433 if (info->signals & SerialSignal_DTR)
4434 val |= BIT3;
4435 if (info->signals & SerialSignal_RTS)
4436 val |= BIT2;
4437 if (info->if_mode & MGSL_INTERFACE_LL)
4438 val |= BIT1;
4439 if (info->if_mode & MGSL_INTERFACE_RL)
4440 val |= BIT0;
4441 wr_reg8(info, VCR, val);
4442}
4443
4444/*
4445 * set state of V24 control (output) signals
4446 */
4447static void set_signals(struct slgt_info *info)
4448{
4449 unsigned char val = rd_reg8(info, VCR);
4450 if (info->signals & SerialSignal_DTR)
4451 val |= BIT3;
4452 else
4453 val &= ~BIT3;
4454 if (info->signals & SerialSignal_RTS)
4455 val |= BIT2;
4456 else
4457 val &= ~BIT2;
4458 wr_reg8(info, VCR, val);
4459}
4460
4461/*
4462 * free range of receive DMA buffers (i to last)
4463 */
4464static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
4465{
4466 int done = 0;
4467
4468 while(!done) {
4469 /* reset current buffer for reuse */
4470 info->rbufs[i].status = 0;
Paul Fulghum814dae02008-07-22 11:22:14 +01004471 set_desc_count(info->rbufs[i], info->rbuf_fill_level);
Paul Fulghum705b6c72006-01-08 01:02:06 -08004472 if (i == last)
4473 done = 1;
4474 if (++i == info->rbuf_count)
4475 i = 0;
4476 }
4477 info->rbuf_current = i;
4478}
4479
4480/*
4481 * mark all receive DMA buffers as free
4482 */
4483static void reset_rbufs(struct slgt_info *info)
4484{
4485 free_rbufs(info, 0, info->rbuf_count - 1);
4486}
4487
4488/*
4489 * pass receive HDLC frame to upper layer
4490 *
Joe Perches0fab6de2008-04-28 02:14:02 -07004491 * return true if frame available, otherwise false
Paul Fulghum705b6c72006-01-08 01:02:06 -08004492 */
Joe Perches0fab6de2008-04-28 02:14:02 -07004493static bool rx_get_frame(struct slgt_info *info)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004494{
4495 unsigned int start, end;
4496 unsigned short status;
4497 unsigned int framesize = 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004498 unsigned long flags;
Alan Cox8fb06c72008-07-16 21:56:46 +01004499 struct tty_struct *tty = info->port.tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004500 unsigned char addr_field = 0xff;
Paul Fulghum04b374d2006-06-25 05:49:21 -07004501 unsigned int crc_size = 0;
4502
4503 switch (info->params.crc_type & HDLC_CRC_MASK) {
4504 case HDLC_CRC_16_CCITT: crc_size = 2; break;
4505 case HDLC_CRC_32_CCITT: crc_size = 4; break;
4506 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08004507
4508check_again:
4509
4510 framesize = 0;
4511 addr_field = 0xff;
4512 start = end = info->rbuf_current;
4513
4514 for (;;) {
4515 if (!desc_complete(info->rbufs[end]))
4516 goto cleanup;
4517
4518 if (framesize == 0 && info->params.addr_filter != 0xff)
4519 addr_field = info->rbufs[end].buf[0];
4520
4521 framesize += desc_count(info->rbufs[end]);
4522
4523 if (desc_eof(info->rbufs[end]))
4524 break;
4525
4526 if (++end == info->rbuf_count)
4527 end = 0;
4528
4529 if (end == info->rbuf_current) {
4530 if (info->rx_enabled){
4531 spin_lock_irqsave(&info->lock,flags);
4532 rx_start(info);
4533 spin_unlock_irqrestore(&info->lock,flags);
4534 }
4535 goto cleanup;
4536 }
4537 }
4538
4539 /* status
4540 *
4541 * 15 buffer complete
4542 * 14..06 reserved
4543 * 05..04 residue
4544 * 02 eof (end of frame)
4545 * 01 CRC error
4546 * 00 abort
4547 */
4548 status = desc_status(info->rbufs[end]);
4549
4550 /* ignore CRC bit if not using CRC (bit is undefined) */
Paul Fulghum04b374d2006-06-25 05:49:21 -07004551 if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004552 status &= ~BIT1;
4553
4554 if (framesize == 0 ||
4555 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4556 free_rbufs(info, start, end);
4557 goto check_again;
4558 }
4559
Paul Fulghum04b374d2006-06-25 05:49:21 -07004560 if (framesize < (2 + crc_size) || status & BIT0) {
4561 info->icount.rxshort++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004562 framesize = 0;
Paul Fulghum04b374d2006-06-25 05:49:21 -07004563 } else if (status & BIT1) {
4564 info->icount.rxcrc++;
4565 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
4566 framesize = 0;
4567 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08004568
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08004569#if SYNCLINK_GENERIC_HDLC
Paul Fulghum04b374d2006-06-25 05:49:21 -07004570 if (framesize == 0) {
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004571 info->netdev->stats.rx_errors++;
4572 info->netdev->stats.rx_frame_errors++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004573 }
Paul Fulghum04b374d2006-06-25 05:49:21 -07004574#endif
Paul Fulghum705b6c72006-01-08 01:02:06 -08004575
4576 DBGBH(("%s rx frame status=%04X size=%d\n",
4577 info->device_name, status, framesize));
Paul Fulghum814dae02008-07-22 11:22:14 +01004578 DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, info->rbuf_fill_level), "rx");
Paul Fulghum705b6c72006-01-08 01:02:06 -08004579
4580 if (framesize) {
Paul Fulghum04b374d2006-06-25 05:49:21 -07004581 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
4582 framesize -= crc_size;
4583 crc_size = 0;
4584 }
4585
4586 if (framesize > info->max_frame_size + crc_size)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004587 info->icount.rxlong++;
4588 else {
4589 /* copy dma buffer(s) to contiguous temp buffer */
4590 int copy_count = framesize;
4591 int i = start;
4592 unsigned char *p = info->tmp_rbuf;
4593 info->tmp_rbuf_count = framesize;
4594
4595 info->icount.rxok++;
4596
4597 while(copy_count) {
Paul Fulghum814dae02008-07-22 11:22:14 +01004598 int partial_count = min_t(int, copy_count, info->rbuf_fill_level);
Paul Fulghum705b6c72006-01-08 01:02:06 -08004599 memcpy(p, info->rbufs[i].buf, partial_count);
4600 p += partial_count;
4601 copy_count -= partial_count;
4602 if (++i == info->rbuf_count)
4603 i = 0;
4604 }
4605
Paul Fulghum04b374d2006-06-25 05:49:21 -07004606 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4607 *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
4608 framesize++;
4609 }
4610
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08004611#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08004612 if (info->netcount)
4613 hdlcdev_rx(info,info->tmp_rbuf, framesize);
4614 else
4615#endif
4616 ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
4617 }
4618 }
4619 free_rbufs(info, start, end);
Joe Perches0fab6de2008-04-28 02:14:02 -07004620 return true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004621
4622cleanup:
Joe Perches0fab6de2008-04-28 02:14:02 -07004623 return false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004624}
4625
4626/*
4627 * pass receive buffer (RAW synchronous mode) to tty layer
Joe Perches0fab6de2008-04-28 02:14:02 -07004628 * return true if buffer available, otherwise false
Paul Fulghum705b6c72006-01-08 01:02:06 -08004629 */
Joe Perches0fab6de2008-04-28 02:14:02 -07004630static bool rx_get_buf(struct slgt_info *info)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004631{
4632 unsigned int i = info->rbuf_current;
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004633 unsigned int count;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004634
4635 if (!desc_complete(info->rbufs[i]))
Joe Perches0fab6de2008-04-28 02:14:02 -07004636 return false;
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004637 count = desc_count(info->rbufs[i]);
4638 switch(info->params.mode) {
4639 case MGSL_MODE_MONOSYNC:
4640 case MGSL_MODE_BISYNC:
4641 /* ignore residue in byte synchronous modes */
4642 if (desc_residue(info->rbufs[i]))
4643 count--;
4644 break;
4645 }
4646 DBGDATA(info, info->rbufs[i].buf, count, "rx");
4647 DBGINFO(("rx_get_buf size=%d\n", count));
4648 if (count)
Alan Cox8fb06c72008-07-16 21:56:46 +01004649 ldisc_receive_buf(info->port.tty, info->rbufs[i].buf,
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004650 info->flag_buf, count);
Paul Fulghum705b6c72006-01-08 01:02:06 -08004651 free_rbufs(info, i, i);
Joe Perches0fab6de2008-04-28 02:14:02 -07004652 return true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004653}
4654
4655static void reset_tbufs(struct slgt_info *info)
4656{
4657 unsigned int i;
4658 info->tbuf_current = 0;
4659 for (i=0 ; i < info->tbuf_count ; i++) {
4660 info->tbufs[i].status = 0;
4661 info->tbufs[i].count = 0;
4662 }
4663}
4664
4665/*
4666 * return number of free transmit DMA buffers
4667 */
4668static unsigned int free_tbuf_count(struct slgt_info *info)
4669{
4670 unsigned int count = 0;
4671 unsigned int i = info->tbuf_current;
4672
4673 do
4674 {
4675 if (desc_count(info->tbufs[i]))
4676 break; /* buffer in use */
4677 ++count;
4678 if (++i == info->tbuf_count)
4679 i=0;
4680 } while (i != info->tbuf_current);
4681
Paul Fulghumbb029c62007-07-31 00:37:35 -07004682 /* if tx DMA active, last zero count buffer is in use */
4683 if (count && (rd_reg32(info, TDCSR) & BIT0))
Paul Fulghum705b6c72006-01-08 01:02:06 -08004684 --count;
4685
4686 return count;
4687}
4688
4689/*
Paul Fulghum403214d2008-07-22 11:21:55 +01004690 * return number of bytes in unsent transmit DMA buffers
4691 * and the serial controller tx FIFO
4692 */
4693static unsigned int tbuf_bytes(struct slgt_info *info)
4694{
4695 unsigned int total_count = 0;
4696 unsigned int i = info->tbuf_current;
4697 unsigned int reg_value;
4698 unsigned int count;
4699 unsigned int active_buf_count = 0;
4700
4701 /*
4702 * Add descriptor counts for all tx DMA buffers.
4703 * If count is zero (cleared by DMA controller after read),
4704 * the buffer is complete or is actively being read from.
4705 *
4706 * Record buf_count of last buffer with zero count starting
4707 * from current ring position. buf_count is mirror
4708 * copy of count and is not cleared by serial controller.
4709 * If DMA controller is active, that buffer is actively
4710 * being read so add to total.
4711 */
4712 do {
4713 count = desc_count(info->tbufs[i]);
4714 if (count)
4715 total_count += count;
4716 else if (!total_count)
4717 active_buf_count = info->tbufs[i].buf_count;
4718 if (++i == info->tbuf_count)
4719 i = 0;
4720 } while (i != info->tbuf_current);
4721
4722 /* read tx DMA status register */
4723 reg_value = rd_reg32(info, TDCSR);
4724
4725 /* if tx DMA active, last zero count buffer is in use */
4726 if (reg_value & BIT0)
4727 total_count += active_buf_count;
4728
4729 /* add tx FIFO count = reg_value[15..8] */
4730 total_count += (reg_value >> 8) & 0xff;
4731
4732 /* if transmitter active add one byte for shift register */
4733 if (info->tx_active)
4734 total_count++;
4735
4736 return total_count;
4737}
4738
4739/*
Paul Fulghum705b6c72006-01-08 01:02:06 -08004740 * load transmit DMA buffer(s) with data
4741 */
4742static void tx_load(struct slgt_info *info, const char *buf, unsigned int size)
4743{
4744 unsigned short count;
4745 unsigned int i;
4746 struct slgt_desc *d;
4747
4748 if (size == 0)
4749 return;
4750
4751 DBGDATA(info, buf, size, "tx");
4752
4753 info->tbuf_start = i = info->tbuf_current;
4754
4755 while (size) {
4756 d = &info->tbufs[i];
4757 if (++i == info->tbuf_count)
4758 i = 0;
4759
4760 count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
4761 memcpy(d->buf, buf, count);
4762
4763 size -= count;
4764 buf += count;
4765
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004766 /*
4767 * set EOF bit for last buffer of HDLC frame or
4768 * for every buffer in raw mode
4769 */
4770 if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
4771 info->params.mode == MGSL_MODE_RAW)
4772 set_desc_eof(*d, 1);
Paul Fulghum705b6c72006-01-08 01:02:06 -08004773 else
4774 set_desc_eof(*d, 0);
4775
4776 set_desc_count(*d, count);
Paul Fulghum403214d2008-07-22 11:21:55 +01004777 d->buf_count = count;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004778 }
4779
4780 info->tbuf_current = i;
4781}
4782
4783static int register_test(struct slgt_info *info)
4784{
4785 static unsigned short patterns[] =
4786 {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
4787 static unsigned int count = sizeof(patterns)/sizeof(patterns[0]);
4788 unsigned int i;
4789 int rc = 0;
4790
4791 for (i=0 ; i < count ; i++) {
4792 wr_reg16(info, TIR, patterns[i]);
4793 wr_reg16(info, BDR, patterns[(i+1)%count]);
4794 if ((rd_reg16(info, TIR) != patterns[i]) ||
4795 (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
4796 rc = -ENODEV;
4797 break;
4798 }
4799 }
Paul Fulghum0080b7a2006-03-28 01:56:15 -08004800 info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004801 info->init_error = rc ? 0 : DiagStatus_AddressFailure;
4802 return rc;
4803}
4804
4805static int irq_test(struct slgt_info *info)
4806{
4807 unsigned long timeout;
4808 unsigned long flags;
Alan Cox8fb06c72008-07-16 21:56:46 +01004809 struct tty_struct *oldtty = info->port.tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004810 u32 speed = info->params.data_rate;
4811
4812 info->params.data_rate = 921600;
Alan Cox8fb06c72008-07-16 21:56:46 +01004813 info->port.tty = NULL;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004814
4815 spin_lock_irqsave(&info->lock, flags);
4816 async_mode(info);
4817 slgt_irq_on(info, IRQ_TXIDLE);
4818
4819 /* enable transmitter */
4820 wr_reg16(info, TCR,
4821 (unsigned short)(rd_reg16(info, TCR) | BIT1));
4822
4823 /* write one byte and wait for tx idle */
4824 wr_reg16(info, TDR, 0);
4825
4826 /* assume failure */
4827 info->init_error = DiagStatus_IrqFailure;
Joe Perches0fab6de2008-04-28 02:14:02 -07004828 info->irq_occurred = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004829
4830 spin_unlock_irqrestore(&info->lock, flags);
4831
4832 timeout=100;
4833 while(timeout-- && !info->irq_occurred)
4834 msleep_interruptible(10);
4835
4836 spin_lock_irqsave(&info->lock,flags);
4837 reset_port(info);
4838 spin_unlock_irqrestore(&info->lock,flags);
4839
4840 info->params.data_rate = speed;
Alan Cox8fb06c72008-07-16 21:56:46 +01004841 info->port.tty = oldtty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004842
4843 info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
4844 return info->irq_occurred ? 0 : -ENODEV;
4845}
4846
4847static int loopback_test_rx(struct slgt_info *info)
4848{
4849 unsigned char *src, *dest;
4850 int count;
4851
4852 if (desc_complete(info->rbufs[0])) {
4853 count = desc_count(info->rbufs[0]);
4854 src = info->rbufs[0].buf;
4855 dest = info->tmp_rbuf;
4856
4857 for( ; count ; count-=2, src+=2) {
4858 /* src=data byte (src+1)=status byte */
4859 if (!(*(src+1) & (BIT9 + BIT8))) {
4860 *dest = *src;
4861 dest++;
4862 info->tmp_rbuf_count++;
4863 }
4864 }
4865 DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
4866 return 1;
4867 }
4868 return 0;
4869}
4870
4871static int loopback_test(struct slgt_info *info)
4872{
4873#define TESTFRAMESIZE 20
4874
4875 unsigned long timeout;
4876 u16 count = TESTFRAMESIZE;
4877 unsigned char buf[TESTFRAMESIZE];
4878 int rc = -ENODEV;
4879 unsigned long flags;
4880
Alan Cox8fb06c72008-07-16 21:56:46 +01004881 struct tty_struct *oldtty = info->port.tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004882 MGSL_PARAMS params;
4883
4884 memcpy(&params, &info->params, sizeof(params));
4885
4886 info->params.mode = MGSL_MODE_ASYNC;
4887 info->params.data_rate = 921600;
4888 info->params.loopback = 1;
Alan Cox8fb06c72008-07-16 21:56:46 +01004889 info->port.tty = NULL;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004890
4891 /* build and send transmit frame */
4892 for (count = 0; count < TESTFRAMESIZE; ++count)
4893 buf[count] = (unsigned char)count;
4894
4895 info->tmp_rbuf_count = 0;
4896 memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
4897
4898 /* program hardware for HDLC and enabled receiver */
4899 spin_lock_irqsave(&info->lock,flags);
4900 async_mode(info);
4901 rx_start(info);
4902 info->tx_count = count;
4903 tx_load(info, buf, count);
4904 tx_start(info);
4905 spin_unlock_irqrestore(&info->lock, flags);
4906
4907 /* wait for receive complete */
4908 for (timeout = 100; timeout; --timeout) {
4909 msleep_interruptible(10);
4910 if (loopback_test_rx(info)) {
4911 rc = 0;
4912 break;
4913 }
4914 }
4915
4916 /* verify received frame length and contents */
4917 if (!rc && (info->tmp_rbuf_count != count ||
4918 memcmp(buf, info->tmp_rbuf, count))) {
4919 rc = -ENODEV;
4920 }
4921
4922 spin_lock_irqsave(&info->lock,flags);
4923 reset_adapter(info);
4924 spin_unlock_irqrestore(&info->lock,flags);
4925
4926 memcpy(&info->params, &params, sizeof(info->params));
Alan Cox8fb06c72008-07-16 21:56:46 +01004927 info->port.tty = oldtty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004928
4929 info->init_error = rc ? DiagStatus_DmaFailure : 0;
4930 return rc;
4931}
4932
4933static int adapter_test(struct slgt_info *info)
4934{
4935 DBGINFO(("testing %s\n", info->device_name));
Paul Fulghum294dad02006-06-25 05:49:21 -07004936 if (register_test(info) < 0) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08004937 printk("register test failure %s addr=%08X\n",
4938 info->device_name, info->phys_reg_addr);
Paul Fulghum294dad02006-06-25 05:49:21 -07004939 } else if (irq_test(info) < 0) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08004940 printk("IRQ test failure %s IRQ=%d\n",
4941 info->device_name, info->irq_level);
Paul Fulghum294dad02006-06-25 05:49:21 -07004942 } else if (loopback_test(info) < 0) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08004943 printk("loopback test failure %s\n", info->device_name);
4944 }
4945 return info->init_error;
4946}
4947
4948/*
4949 * transmit timeout handler
4950 */
4951static void tx_timeout(unsigned long context)
4952{
4953 struct slgt_info *info = (struct slgt_info*)context;
4954 unsigned long flags;
4955
4956 DBGINFO(("%s tx_timeout\n", info->device_name));
4957 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
4958 info->icount.txtimeout++;
4959 }
4960 spin_lock_irqsave(&info->lock,flags);
Joe Perches0fab6de2008-04-28 02:14:02 -07004961 info->tx_active = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004962 info->tx_count = 0;
4963 spin_unlock_irqrestore(&info->lock,flags);
4964
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08004965#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08004966 if (info->netcount)
4967 hdlcdev_tx_done(info);
4968 else
4969#endif
4970 bh_transmit(info);
4971}
4972
4973/*
4974 * receive buffer polling timer
4975 */
4976static void rx_timeout(unsigned long context)
4977{
4978 struct slgt_info *info = (struct slgt_info*)context;
4979 unsigned long flags;
4980
4981 DBGINFO(("%s rx_timeout\n", info->device_name));
4982 spin_lock_irqsave(&info->lock, flags);
4983 info->pending_bh |= BH_RECEIVE;
4984 spin_unlock_irqrestore(&info->lock, flags);
David Howellsc4028952006-11-22 14:57:56 +00004985 bh_handler(&info->task);
Paul Fulghum705b6c72006-01-08 01:02:06 -08004986}
4987