blob: 67986ea0d479cea42ddd0ddde8ccd5fae30a8386 [file] [log] [blame]
Paul Fulghum705b6c72006-01-08 01:02:06 -08001/*
Paul Fulghum705b6c72006-01-08 01:02:06 -08002 * Device driver for Microgate SyncLink GT serial adapters.
3 *
4 * written by Paul Fulghum for Microgate Corporation
5 * paulkf@microgate.com
6 *
7 * Microgate and SyncLink are trademarks of Microgate Corporation
8 *
9 * This code is released under the GNU General Public License (GPL)
10 *
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
12 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21 * OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23
24/*
25 * DEBUG OUTPUT DEFINITIONS
26 *
27 * uncomment lines below to enable specific types of debug output
28 *
29 * DBGINFO information - most verbose output
30 * DBGERR serious errors
31 * DBGBH bottom half service routine debugging
32 * DBGISR interrupt service routine debugging
33 * DBGDATA output receive and transmit data
34 * DBGTBUF output transmit DMA buffers and registers
35 * DBGRBUF output receive DMA buffers and registers
36 */
37
38#define DBGINFO(fmt) if (debug_level >= DEBUG_LEVEL_INFO) printk fmt
39#define DBGERR(fmt) if (debug_level >= DEBUG_LEVEL_ERROR) printk fmt
40#define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt
41#define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt
42#define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label))
43//#define DBGTBUF(info) dump_tbufs(info)
44//#define DBGRBUF(info) dump_rbufs(info)
45
46
Paul Fulghum705b6c72006-01-08 01:02:06 -080047#include <linux/module.h>
Paul Fulghum705b6c72006-01-08 01:02:06 -080048#include <linux/errno.h>
49#include <linux/signal.h>
50#include <linux/sched.h>
51#include <linux/timer.h>
52#include <linux/interrupt.h>
53#include <linux/pci.h>
54#include <linux/tty.h>
55#include <linux/tty_flip.h>
56#include <linux/serial.h>
57#include <linux/major.h>
58#include <linux/string.h>
59#include <linux/fcntl.h>
60#include <linux/ptrace.h>
61#include <linux/ioport.h>
62#include <linux/mm.h>
Alexey Dobriyana18c56e2009-03-31 15:19:19 -070063#include <linux/seq_file.h>
Paul Fulghum705b6c72006-01-08 01:02:06 -080064#include <linux/slab.h>
65#include <linux/netdevice.h>
66#include <linux/vmalloc.h>
67#include <linux/init.h>
68#include <linux/delay.h>
69#include <linux/ioctl.h>
70#include <linux/termios.h>
71#include <linux/bitops.h>
72#include <linux/workqueue.h>
73#include <linux/hdlc.h>
Robert P. J. Day3dd12472008-02-06 01:37:17 -080074#include <linux/synclink.h>
Paul Fulghum705b6c72006-01-08 01:02:06 -080075
Paul Fulghum705b6c72006-01-08 01:02:06 -080076#include <asm/system.h>
77#include <asm/io.h>
78#include <asm/irq.h>
79#include <asm/dma.h>
80#include <asm/types.h>
81#include <asm/uaccess.h>
82
Paul Fulghumaf69c7f2006-12-06 20:40:24 -080083#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_GT_MODULE))
84#define SYNCLINK_GENERIC_HDLC 1
85#else
86#define SYNCLINK_GENERIC_HDLC 0
Paul Fulghum705b6c72006-01-08 01:02:06 -080087#endif
88
89/*
90 * module identification
91 */
92static char *driver_name = "SyncLink GT";
Paul Fulghum705b6c72006-01-08 01:02:06 -080093static char *tty_driver_name = "synclink_gt";
94static char *tty_dev_prefix = "ttySLG";
95MODULE_LICENSE("GPL");
96#define MGSL_MAGIC 0x5401
Paul Fulghuma077c1a2006-09-30 23:27:46 -070097#define MAX_DEVICES 32
Paul Fulghum705b6c72006-01-08 01:02:06 -080098
99static struct pci_device_id pci_table[] = {
100 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
Paul Fulghum6f84be82006-06-25 05:49:22 -0700101 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT2_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
Paul Fulghum705b6c72006-01-08 01:02:06 -0800102 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
103 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
104 {0,}, /* terminate list */
105};
106MODULE_DEVICE_TABLE(pci, pci_table);
107
108static int init_one(struct pci_dev *dev,const struct pci_device_id *ent);
109static void remove_one(struct pci_dev *dev);
110static struct pci_driver pci_driver = {
111 .name = "synclink_gt",
112 .id_table = pci_table,
113 .probe = init_one,
114 .remove = __devexit_p(remove_one),
115};
116
Joe Perches0fab6de2008-04-28 02:14:02 -0700117static bool pci_registered;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800118
119/*
120 * module configuration and status
121 */
122static struct slgt_info *slgt_device_list;
123static int slgt_device_count;
124
125static int ttymajor;
126static int debug_level;
127static int maxframe[MAX_DEVICES];
Paul Fulghum705b6c72006-01-08 01:02:06 -0800128
129module_param(ttymajor, int, 0);
130module_param(debug_level, int, 0);
131module_param_array(maxframe, int, NULL, 0);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800132
133MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
134MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
135MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
Paul Fulghum705b6c72006-01-08 01:02:06 -0800136
137/*
138 * tty support and callbacks
139 */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800140static struct tty_driver *serial_driver;
141
142static int open(struct tty_struct *tty, struct file * filp);
143static void close(struct tty_struct *tty, struct file * filp);
144static void hangup(struct tty_struct *tty);
Alan Cox606d0992006-12-08 02:38:45 -0800145static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800146
147static int write(struct tty_struct *tty, const unsigned char *buf, int count);
Alan Cox55da7782008-04-30 00:54:07 -0700148static int put_char(struct tty_struct *tty, unsigned char ch);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800149static void send_xchar(struct tty_struct *tty, char ch);
150static void wait_until_sent(struct tty_struct *tty, int timeout);
151static int write_room(struct tty_struct *tty);
152static void flush_chars(struct tty_struct *tty);
153static void flush_buffer(struct tty_struct *tty);
154static void tx_hold(struct tty_struct *tty);
155static void tx_release(struct tty_struct *tty);
156
157static int ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800158static int chars_in_buffer(struct tty_struct *tty);
159static void throttle(struct tty_struct * tty);
160static void unthrottle(struct tty_struct * tty);
Alan Cox9e989662008-07-22 11:18:03 +0100161static int set_break(struct tty_struct *tty, int break_state);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800162
163/*
164 * generic HDLC support and callbacks
165 */
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800166#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -0800167#define dev_to_port(D) (dev_to_hdlc(D)->priv)
168static void hdlcdev_tx_done(struct slgt_info *info);
169static void hdlcdev_rx(struct slgt_info *info, char *buf, int size);
170static int hdlcdev_init(struct slgt_info *info);
171static void hdlcdev_exit(struct slgt_info *info);
172#endif
173
174
175/*
176 * device specific structures, macros and functions
177 */
178
179#define SLGT_MAX_PORTS 4
180#define SLGT_REG_SIZE 256
181
182/*
Paul Fulghum0080b7a2006-03-28 01:56:15 -0800183 * conditional wait facility
184 */
185struct cond_wait {
186 struct cond_wait *next;
187 wait_queue_head_t q;
188 wait_queue_t wait;
189 unsigned int data;
190};
191static void init_cond_wait(struct cond_wait *w, unsigned int data);
192static void add_cond_wait(struct cond_wait **head, struct cond_wait *w);
193static void remove_cond_wait(struct cond_wait **head, struct cond_wait *w);
194static void flush_cond_wait(struct cond_wait **head);
195
196/*
Paul Fulghum705b6c72006-01-08 01:02:06 -0800197 * DMA buffer descriptor and access macros
198 */
199struct slgt_desc
200{
Al Viro51ef9c52007-10-14 19:34:30 +0100201 __le16 count;
202 __le16 status;
203 __le32 pbuf; /* physical address of data buffer */
204 __le32 next; /* physical address of next descriptor */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800205
206 /* driver book keeping */
207 char *buf; /* virtual address of data buffer */
208 unsigned int pdesc; /* physical address of this descriptor */
209 dma_addr_t buf_dma_addr;
Paul Fulghum403214d2008-07-22 11:21:55 +0100210 unsigned short buf_count;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800211};
212
213#define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
214#define set_desc_next(a,b) (a).next = cpu_to_le32((unsigned int)(b))
215#define set_desc_count(a,b)(a).count = cpu_to_le16((unsigned short)(b))
216#define set_desc_eof(a,b) (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
217#define desc_count(a) (le16_to_cpu((a).count))
218#define desc_status(a) (le16_to_cpu((a).status))
219#define desc_complete(a) (le16_to_cpu((a).status) & BIT15)
220#define desc_eof(a) (le16_to_cpu((a).status) & BIT2)
221#define desc_crc_error(a) (le16_to_cpu((a).status) & BIT1)
222#define desc_abort(a) (le16_to_cpu((a).status) & BIT0)
223#define desc_residue(a) ((le16_to_cpu((a).status) & 0x38) >> 3)
224
225struct _input_signal_events {
226 int ri_up;
227 int ri_down;
228 int dsr_up;
229 int dsr_down;
230 int dcd_up;
231 int dcd_down;
232 int cts_up;
233 int cts_down;
234};
235
236/*
237 * device instance data structure
238 */
239struct slgt_info {
240 void *if_ptr; /* General purpose pointer (used by SPPP) */
Alan Cox8fb06c72008-07-16 21:56:46 +0100241 struct tty_port port;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800242
243 struct slgt_info *next_device; /* device list link */
244
245 int magic;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800246
247 char device_name[25];
248 struct pci_dev *pdev;
249
250 int port_count; /* count of ports on adapter */
251 int adapter_num; /* adapter instance number */
252 int port_num; /* port instance number */
253
254 /* array of pointers to port contexts on this adapter */
255 struct slgt_info *port_array[SLGT_MAX_PORTS];
256
Paul Fulghum705b6c72006-01-08 01:02:06 -0800257 int line; /* tty line instance number */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800258
259 struct mgsl_icount icount;
260
Paul Fulghum705b6c72006-01-08 01:02:06 -0800261 int timeout;
262 int x_char; /* xon/xoff character */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800263 unsigned int read_status_mask;
264 unsigned int ignore_status_mask;
265
Paul Fulghum705b6c72006-01-08 01:02:06 -0800266 wait_queue_head_t status_event_wait_q;
267 wait_queue_head_t event_wait_q;
268 struct timer_list tx_timer;
269 struct timer_list rx_timer;
270
Paul Fulghum0080b7a2006-03-28 01:56:15 -0800271 unsigned int gpio_present;
272 struct cond_wait *gpio_wait_q;
273
Paul Fulghum705b6c72006-01-08 01:02:06 -0800274 spinlock_t lock; /* spinlock for synchronizing with ISR */
275
276 struct work_struct task;
277 u32 pending_bh;
Joe Perches0fab6de2008-04-28 02:14:02 -0700278 bool bh_requested;
279 bool bh_running;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800280
281 int isr_overflow;
Joe Perches0fab6de2008-04-28 02:14:02 -0700282 bool irq_requested; /* true if IRQ requested */
283 bool irq_occurred; /* for diagnostics use */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800284
285 /* device configuration */
286
287 unsigned int bus_type;
288 unsigned int irq_level;
289 unsigned long irq_flags;
290
291 unsigned char __iomem * reg_addr; /* memory mapped registers address */
292 u32 phys_reg_addr;
Joe Perches0fab6de2008-04-28 02:14:02 -0700293 bool reg_addr_requested;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800294
295 MGSL_PARAMS params; /* communications parameters */
296 u32 idle_mode;
297 u32 max_frame_size; /* as set by device config */
298
Paul Fulghum814dae02008-07-22 11:22:14 +0100299 unsigned int rbuf_fill_level;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800300 unsigned int if_mode;
Paul Fulghum1f807692009-04-02 16:58:30 -0700301 unsigned int base_clock;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800302
303 /* device status */
304
Joe Perches0fab6de2008-04-28 02:14:02 -0700305 bool rx_enabled;
306 bool rx_restart;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800307
Joe Perches0fab6de2008-04-28 02:14:02 -0700308 bool tx_enabled;
309 bool tx_active;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800310
311 unsigned char signals; /* serial signal states */
Darren Jenkins2641dfd2006-02-28 16:59:20 -0800312 int init_error; /* initialization error */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800313
314 unsigned char *tx_buf;
315 int tx_count;
316
317 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
318 char char_buf[MAX_ASYNC_BUFFER_SIZE];
Joe Perches0fab6de2008-04-28 02:14:02 -0700319 bool drop_rts_on_tx_done;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800320 struct _input_signal_events input_signal_events;
321
322 int dcd_chkcount; /* check counts to prevent */
323 int cts_chkcount; /* too many IRQs if a signal */
324 int dsr_chkcount; /* is floating */
325 int ri_chkcount;
326
327 char *bufs; /* virtual address of DMA buffer lists */
328 dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
329
330 unsigned int rbuf_count;
331 struct slgt_desc *rbufs;
332 unsigned int rbuf_current;
333 unsigned int rbuf_index;
334
335 unsigned int tbuf_count;
336 struct slgt_desc *tbufs;
337 unsigned int tbuf_current;
338 unsigned int tbuf_start;
339
340 unsigned char *tmp_rbuf;
341 unsigned int tmp_rbuf_count;
342
343 /* SPPP/Cisco HDLC device parts */
344
345 int netcount;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800346 spinlock_t netlock;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800347#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -0800348 struct net_device *netdev;
349#endif
350
351};
352
353static MGSL_PARAMS default_params = {
354 .mode = MGSL_MODE_HDLC,
355 .loopback = 0,
356 .flags = HDLC_FLAG_UNDERRUN_ABORT15,
357 .encoding = HDLC_ENCODING_NRZI_SPACE,
358 .clock_speed = 0,
359 .addr_filter = 0xff,
360 .crc_type = HDLC_CRC_16_CCITT,
361 .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
362 .preamble = HDLC_PREAMBLE_PATTERN_NONE,
363 .data_rate = 9600,
364 .data_bits = 8,
365 .stop_bits = 1,
366 .parity = ASYNC_PARITY_NONE
367};
368
369
370#define BH_RECEIVE 1
371#define BH_TRANSMIT 2
372#define BH_STATUS 4
373#define IO_PIN_SHUTDOWN_LIMIT 100
374
375#define DMABUFSIZE 256
376#define DESC_LIST_SIZE 4096
377
378#define MASK_PARITY BIT1
Paul Fulghum202af6d2006-08-31 21:27:36 -0700379#define MASK_FRAMING BIT0
380#define MASK_BREAK BIT14
Paul Fulghum705b6c72006-01-08 01:02:06 -0800381#define MASK_OVERRUN BIT4
382
383#define GSR 0x00 /* global status */
Paul Fulghum0080b7a2006-03-28 01:56:15 -0800384#define JCR 0x04 /* JTAG control */
385#define IODR 0x08 /* GPIO direction */
386#define IOER 0x0c /* GPIO interrupt enable */
387#define IOVR 0x10 /* GPIO value */
388#define IOSR 0x14 /* GPIO interrupt status */
Paul Fulghum705b6c72006-01-08 01:02:06 -0800389#define TDR 0x80 /* tx data */
390#define RDR 0x80 /* rx data */
391#define TCR 0x82 /* tx control */
392#define TIR 0x84 /* tx idle */
393#define TPR 0x85 /* tx preamble */
394#define RCR 0x86 /* rx control */
395#define VCR 0x88 /* V.24 control */
396#define CCR 0x89 /* clock control */
397#define BDR 0x8a /* baud divisor */
398#define SCR 0x8c /* serial control */
399#define SSR 0x8e /* serial status */
400#define RDCSR 0x90 /* rx DMA control/status */
401#define TDCSR 0x94 /* tx DMA control/status */
402#define RDDAR 0x98 /* rx DMA descriptor address */
403#define TDDAR 0x9c /* tx DMA descriptor address */
404
405#define RXIDLE BIT14
406#define RXBREAK BIT14
407#define IRQ_TXDATA BIT13
408#define IRQ_TXIDLE BIT12
409#define IRQ_TXUNDER BIT11 /* HDLC */
410#define IRQ_RXDATA BIT10
411#define IRQ_RXIDLE BIT9 /* HDLC */
412#define IRQ_RXBREAK BIT9 /* async */
413#define IRQ_RXOVER BIT8
414#define IRQ_DSR BIT7
415#define IRQ_CTS BIT6
416#define IRQ_DCD BIT5
417#define IRQ_RI BIT4
418#define IRQ_ALL 0x3ff0
419#define IRQ_MASTER BIT0
420
421#define slgt_irq_on(info, mask) \
422 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
423#define slgt_irq_off(info, mask) \
424 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
425
426static __u8 rd_reg8(struct slgt_info *info, unsigned int addr);
427static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
428static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
429static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
430static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
431static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
432
433static void msc_set_vcr(struct slgt_info *info);
434
435static int startup(struct slgt_info *info);
436static int block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
437static void shutdown(struct slgt_info *info);
438static void program_hw(struct slgt_info *info);
439static void change_params(struct slgt_info *info);
440
441static int register_test(struct slgt_info *info);
442static int irq_test(struct slgt_info *info);
443static int loopback_test(struct slgt_info *info);
444static int adapter_test(struct slgt_info *info);
445
446static void reset_adapter(struct slgt_info *info);
447static void reset_port(struct slgt_info *info);
448static void async_mode(struct slgt_info *info);
Paul Fulghumcb10dc92006-09-30 23:27:45 -0700449static void sync_mode(struct slgt_info *info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800450
451static void rx_stop(struct slgt_info *info);
452static void rx_start(struct slgt_info *info);
453static void reset_rbufs(struct slgt_info *info);
454static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
455static void rdma_reset(struct slgt_info *info);
Joe Perches0fab6de2008-04-28 02:14:02 -0700456static bool rx_get_frame(struct slgt_info *info);
457static bool rx_get_buf(struct slgt_info *info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800458
459static void tx_start(struct slgt_info *info);
460static void tx_stop(struct slgt_info *info);
461static void tx_set_idle(struct slgt_info *info);
462static unsigned int free_tbuf_count(struct slgt_info *info);
Paul Fulghum403214d2008-07-22 11:21:55 +0100463static unsigned int tbuf_bytes(struct slgt_info *info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800464static void reset_tbufs(struct slgt_info *info);
465static void tdma_reset(struct slgt_info *info);
Paul Fulghumbb029c62007-07-31 00:37:35 -0700466static void tdma_start(struct slgt_info *info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800467static void tx_load(struct slgt_info *info, const char *buf, unsigned int count);
468
469static void get_signals(struct slgt_info *info);
470static void set_signals(struct slgt_info *info);
471static void enable_loopback(struct slgt_info *info);
472static void set_rate(struct slgt_info *info, u32 data_rate);
473
474static int bh_action(struct slgt_info *info);
David Howellsc4028952006-11-22 14:57:56 +0000475static void bh_handler(struct work_struct *work);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800476static void bh_transmit(struct slgt_info *info);
477static void isr_serial(struct slgt_info *info);
478static void isr_rdma(struct slgt_info *info);
479static void isr_txeom(struct slgt_info *info, unsigned short status);
480static void isr_tdma(struct slgt_info *info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800481
482static int alloc_dma_bufs(struct slgt_info *info);
483static void free_dma_bufs(struct slgt_info *info);
484static int alloc_desc(struct slgt_info *info);
485static void free_desc(struct slgt_info *info);
486static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
487static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
488
489static int alloc_tmp_rbuf(struct slgt_info *info);
490static void free_tmp_rbuf(struct slgt_info *info);
491
492static void tx_timeout(unsigned long context);
493static void rx_timeout(unsigned long context);
494
495/*
496 * ioctl handlers
497 */
498static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount);
499static int get_params(struct slgt_info *info, MGSL_PARAMS __user *params);
500static int set_params(struct slgt_info *info, MGSL_PARAMS __user *params);
501static int get_txidle(struct slgt_info *info, int __user *idle_mode);
502static int set_txidle(struct slgt_info *info, int idle_mode);
503static int tx_enable(struct slgt_info *info, int enable);
504static int tx_abort(struct slgt_info *info);
505static int rx_enable(struct slgt_info *info, int enable);
506static int modem_input_wait(struct slgt_info *info,int arg);
507static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr);
508static int tiocmget(struct tty_struct *tty, struct file *file);
509static int tiocmset(struct tty_struct *tty, struct file *file,
510 unsigned int set, unsigned int clear);
Alan Cox9e989662008-07-22 11:18:03 +0100511static int set_break(struct tty_struct *tty, int break_state);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800512static int get_interface(struct slgt_info *info, int __user *if_mode);
513static int set_interface(struct slgt_info *info, int if_mode);
Paul Fulghum0080b7a2006-03-28 01:56:15 -0800514static int set_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
515static int get_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
516static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800517
518/*
519 * driver functions
520 */
521static void add_device(struct slgt_info *info);
522static void device_init(int adapter_num, struct pci_dev *pdev);
523static int claim_resources(struct slgt_info *info);
524static void release_resources(struct slgt_info *info);
525
526/*
527 * DEBUG OUTPUT CODE
528 */
529#ifndef DBGINFO
530#define DBGINFO(fmt)
531#endif
532#ifndef DBGERR
533#define DBGERR(fmt)
534#endif
535#ifndef DBGBH
536#define DBGBH(fmt)
537#endif
538#ifndef DBGISR
539#define DBGISR(fmt)
540#endif
541
542#ifdef DBGDATA
543static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
544{
545 int i;
546 int linecount;
547 printk("%s %s data:\n",info->device_name, label);
548 while(count) {
549 linecount = (count > 16) ? 16 : count;
550 for(i=0; i < linecount; i++)
551 printk("%02X ",(unsigned char)data[i]);
552 for(;i<17;i++)
553 printk(" ");
554 for(i=0;i<linecount;i++) {
555 if (data[i]>=040 && data[i]<=0176)
556 printk("%c",data[i]);
557 else
558 printk(".");
559 }
560 printk("\n");
561 data += linecount;
562 count -= linecount;
563 }
564}
565#else
566#define DBGDATA(info, buf, size, label)
567#endif
568
569#ifdef DBGTBUF
570static void dump_tbufs(struct slgt_info *info)
571{
572 int i;
573 printk("tbuf_current=%d\n", info->tbuf_current);
574 for (i=0 ; i < info->tbuf_count ; i++) {
575 printk("%d: count=%04X status=%04X\n",
576 i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
577 }
578}
579#else
580#define DBGTBUF(info)
581#endif
582
583#ifdef DBGRBUF
584static void dump_rbufs(struct slgt_info *info)
585{
586 int i;
587 printk("rbuf_current=%d\n", info->rbuf_current);
588 for (i=0 ; i < info->rbuf_count ; i++) {
589 printk("%d: count=%04X status=%04X\n",
590 i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
591 }
592}
593#else
594#define DBGRBUF(info)
595#endif
596
597static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
598{
599#ifdef SANITY_CHECK
600 if (!info) {
601 printk("null struct slgt_info for (%s) in %s\n", devname, name);
602 return 1;
603 }
604 if (info->magic != MGSL_MAGIC) {
605 printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
606 return 1;
607 }
608#else
609 if (!info)
610 return 1;
611#endif
612 return 0;
613}
614
615/**
616 * line discipline callback wrappers
617 *
618 * The wrappers maintain line discipline references
619 * while calling into the line discipline.
620 *
621 * ldisc_receive_buf - pass receive data to line discipline
622 */
623static void ldisc_receive_buf(struct tty_struct *tty,
624 const __u8 *data, char *flags, int count)
625{
626 struct tty_ldisc *ld;
627 if (!tty)
628 return;
629 ld = tty_ldisc_ref(tty);
630 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100631 if (ld->ops->receive_buf)
632 ld->ops->receive_buf(tty, data, flags, count);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800633 tty_ldisc_deref(ld);
634 }
635}
636
637/* tty callbacks */
638
639static int open(struct tty_struct *tty, struct file *filp)
640{
641 struct slgt_info *info;
642 int retval, line;
643 unsigned long flags;
644
645 line = tty->index;
646 if ((line < 0) || (line >= slgt_device_count)) {
647 DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
648 return -ENODEV;
649 }
650
651 info = slgt_device_list;
652 while(info && info->line != line)
653 info = info->next_device;
654 if (sanity_check(info, tty->name, "open"))
655 return -ENODEV;
656 if (info->init_error) {
657 DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
658 return -ENODEV;
659 }
660
661 tty->driver_data = info;
Alan Cox8fb06c72008-07-16 21:56:46 +0100662 info->port.tty = tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800663
Alan Cox8fb06c72008-07-16 21:56:46 +0100664 DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->port.count));
Paul Fulghum705b6c72006-01-08 01:02:06 -0800665
666 /* If port is closing, signal caller to try again */
Alan Cox8fb06c72008-07-16 21:56:46 +0100667 if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
668 if (info->port.flags & ASYNC_CLOSING)
669 interruptible_sleep_on(&info->port.close_wait);
670 retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
Paul Fulghum705b6c72006-01-08 01:02:06 -0800671 -EAGAIN : -ERESTARTSYS);
672 goto cleanup;
673 }
674
Alan Cox8fb06c72008-07-16 21:56:46 +0100675 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800676
677 spin_lock_irqsave(&info->netlock, flags);
678 if (info->netcount) {
679 retval = -EBUSY;
680 spin_unlock_irqrestore(&info->netlock, flags);
681 goto cleanup;
682 }
Alan Cox8fb06c72008-07-16 21:56:46 +0100683 info->port.count++;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800684 spin_unlock_irqrestore(&info->netlock, flags);
685
Alan Cox8fb06c72008-07-16 21:56:46 +0100686 if (info->port.count == 1) {
Paul Fulghum705b6c72006-01-08 01:02:06 -0800687 /* 1st open on this device, init hardware */
688 retval = startup(info);
689 if (retval < 0)
690 goto cleanup;
691 }
692
693 retval = block_til_ready(tty, filp, info);
694 if (retval) {
695 DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
696 goto cleanup;
697 }
698
699 retval = 0;
700
701cleanup:
702 if (retval) {
703 if (tty->count == 1)
Alan Cox8fb06c72008-07-16 21:56:46 +0100704 info->port.tty = NULL; /* tty layer will release tty struct */
705 if(info->port.count)
706 info->port.count--;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800707 }
708
709 DBGINFO(("%s open rc=%d\n", info->device_name, retval));
710 return retval;
711}
712
713static void close(struct tty_struct *tty, struct file *filp)
714{
715 struct slgt_info *info = tty->driver_data;
716
717 if (sanity_check(info, tty->name, "close"))
718 return;
Alan Cox8fb06c72008-07-16 21:56:46 +0100719 DBGINFO(("%s close entry, count=%d\n", info->device_name, info->port.count));
Paul Fulghum705b6c72006-01-08 01:02:06 -0800720
Alan Coxa6614992009-01-02 13:46:50 +0000721 if (tty_port_close_start(&info->port, tty, filp) == 0)
Paul Fulghum705b6c72006-01-08 01:02:06 -0800722 goto cleanup;
723
Alan Cox8fb06c72008-07-16 21:56:46 +0100724 if (info->port.flags & ASYNC_INITIALIZED)
Paul Fulghum705b6c72006-01-08 01:02:06 -0800725 wait_until_sent(tty, info->timeout);
Alan Cox978e5952008-04-30 00:53:59 -0700726 flush_buffer(tty);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800727 tty_ldisc_flush(tty);
728
729 shutdown(info);
730
Alan Coxa6614992009-01-02 13:46:50 +0000731 tty_port_close_end(&info->port, tty);
Alan Cox8fb06c72008-07-16 21:56:46 +0100732 info->port.tty = NULL;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800733cleanup:
Alan Cox8fb06c72008-07-16 21:56:46 +0100734 DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->port.count));
Paul Fulghum705b6c72006-01-08 01:02:06 -0800735}
736
737static void hangup(struct tty_struct *tty)
738{
739 struct slgt_info *info = tty->driver_data;
740
741 if (sanity_check(info, tty->name, "hangup"))
742 return;
743 DBGINFO(("%s hangup\n", info->device_name));
744
745 flush_buffer(tty);
746 shutdown(info);
747
Alan Cox8fb06c72008-07-16 21:56:46 +0100748 info->port.count = 0;
749 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
750 info->port.tty = NULL;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800751
Alan Cox8fb06c72008-07-16 21:56:46 +0100752 wake_up_interruptible(&info->port.open_wait);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800753}
754
Alan Cox606d0992006-12-08 02:38:45 -0800755static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Paul Fulghum705b6c72006-01-08 01:02:06 -0800756{
757 struct slgt_info *info = tty->driver_data;
758 unsigned long flags;
759
760 DBGINFO(("%s set_termios\n", tty->driver->name));
761
Paul Fulghum705b6c72006-01-08 01:02:06 -0800762 change_params(info);
763
764 /* Handle transition to B0 status */
765 if (old_termios->c_cflag & CBAUD &&
766 !(tty->termios->c_cflag & CBAUD)) {
767 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
768 spin_lock_irqsave(&info->lock,flags);
769 set_signals(info);
770 spin_unlock_irqrestore(&info->lock,flags);
771 }
772
773 /* Handle transition away from B0 status */
774 if (!(old_termios->c_cflag & CBAUD) &&
775 tty->termios->c_cflag & CBAUD) {
776 info->signals |= SerialSignal_DTR;
777 if (!(tty->termios->c_cflag & CRTSCTS) ||
778 !test_bit(TTY_THROTTLED, &tty->flags)) {
779 info->signals |= SerialSignal_RTS;
780 }
781 spin_lock_irqsave(&info->lock,flags);
782 set_signals(info);
783 spin_unlock_irqrestore(&info->lock,flags);
784 }
785
786 /* Handle turning off CRTSCTS */
787 if (old_termios->c_cflag & CRTSCTS &&
788 !(tty->termios->c_cflag & CRTSCTS)) {
789 tty->hw_stopped = 0;
790 tx_release(tty);
791 }
792}
793
794static int write(struct tty_struct *tty,
795 const unsigned char *buf, int count)
796{
797 int ret = 0;
798 struct slgt_info *info = tty->driver_data;
799 unsigned long flags;
Paul Fulghum8a38c282008-07-22 11:21:28 +0100800 unsigned int bufs_needed;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800801
802 if (sanity_check(info, tty->name, "write"))
803 goto cleanup;
804 DBGINFO(("%s write count=%d\n", info->device_name, count));
805
Eric Sesterhenn326f28e92006-06-25 05:48:48 -0700806 if (!info->tx_buf)
Paul Fulghum705b6c72006-01-08 01:02:06 -0800807 goto cleanup;
808
809 if (count > info->max_frame_size) {
810 ret = -EIO;
811 goto cleanup;
812 }
813
814 if (!count)
815 goto cleanup;
816
Paul Fulghum8a38c282008-07-22 11:21:28 +0100817 if (!info->tx_active && info->tx_count) {
818 /* send accumulated data from send_char() */
819 tx_load(info, info->tx_buf, info->tx_count);
820 goto start;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800821 }
Paul Fulghum8a38c282008-07-22 11:21:28 +0100822 bufs_needed = (count/DMABUFSIZE);
823 if (count % DMABUFSIZE)
824 ++bufs_needed;
825 if (bufs_needed > free_tbuf_count(info))
826 goto cleanup;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800827
828 ret = info->tx_count = count;
829 tx_load(info, buf, count);
830 goto start;
831
832start:
833 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
834 spin_lock_irqsave(&info->lock,flags);
835 if (!info->tx_active)
836 tx_start(info);
Paul Fulghumbb029c62007-07-31 00:37:35 -0700837 else
838 tdma_start(info);
Paul Fulghum705b6c72006-01-08 01:02:06 -0800839 spin_unlock_irqrestore(&info->lock,flags);
840 }
841
842cleanup:
843 DBGINFO(("%s write rc=%d\n", info->device_name, ret));
844 return ret;
845}
846
Alan Cox55da7782008-04-30 00:54:07 -0700847static int put_char(struct tty_struct *tty, unsigned char ch)
Paul Fulghum705b6c72006-01-08 01:02:06 -0800848{
849 struct slgt_info *info = tty->driver_data;
850 unsigned long flags;
Andrew Morton6c82c412008-05-12 14:02:34 -0700851 int ret = 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800852
853 if (sanity_check(info, tty->name, "put_char"))
Alan Cox55da7782008-04-30 00:54:07 -0700854 return 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800855 DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
Eric Sesterhenn326f28e92006-06-25 05:48:48 -0700856 if (!info->tx_buf)
Alan Cox55da7782008-04-30 00:54:07 -0700857 return 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800858 spin_lock_irqsave(&info->lock,flags);
Alan Cox55da7782008-04-30 00:54:07 -0700859 if (!info->tx_active && (info->tx_count < info->max_frame_size)) {
Paul Fulghum705b6c72006-01-08 01:02:06 -0800860 info->tx_buf[info->tx_count++] = ch;
Alan Cox55da7782008-04-30 00:54:07 -0700861 ret = 1;
862 }
Paul Fulghum705b6c72006-01-08 01:02:06 -0800863 spin_unlock_irqrestore(&info->lock,flags);
Alan Cox55da7782008-04-30 00:54:07 -0700864 return ret;
Paul Fulghum705b6c72006-01-08 01:02:06 -0800865}
866
867static void send_xchar(struct tty_struct *tty, char ch)
868{
869 struct slgt_info *info = tty->driver_data;
870 unsigned long flags;
871
872 if (sanity_check(info, tty->name, "send_xchar"))
873 return;
874 DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
875 info->x_char = ch;
876 if (ch) {
877 spin_lock_irqsave(&info->lock,flags);
878 if (!info->tx_enabled)
879 tx_start(info);
880 spin_unlock_irqrestore(&info->lock,flags);
881 }
882}
883
884static void wait_until_sent(struct tty_struct *tty, int timeout)
885{
886 struct slgt_info *info = tty->driver_data;
887 unsigned long orig_jiffies, char_time;
888
889 if (!info )
890 return;
891 if (sanity_check(info, tty->name, "wait_until_sent"))
892 return;
893 DBGINFO(("%s wait_until_sent entry\n", info->device_name));
Alan Cox8fb06c72008-07-16 21:56:46 +0100894 if (!(info->port.flags & ASYNC_INITIALIZED))
Paul Fulghum705b6c72006-01-08 01:02:06 -0800895 goto exit;
896
897 orig_jiffies = jiffies;
898
899 /* Set check interval to 1/5 of estimated time to
900 * send a character, and make it at least 1. The check
901 * interval should also be less than the timeout.
902 * Note: use tight timings here to satisfy the NIST-PCTS.
903 */
904
Alan Cox978e5952008-04-30 00:53:59 -0700905 lock_kernel();
906
Paul Fulghum705b6c72006-01-08 01:02:06 -0800907 if (info->params.data_rate) {
908 char_time = info->timeout/(32 * 5);
909 if (!char_time)
910 char_time++;
911 } else
912 char_time = 1;
913
914 if (timeout)
915 char_time = min_t(unsigned long, char_time, timeout);
916
917 while (info->tx_active) {
918 msleep_interruptible(jiffies_to_msecs(char_time));
919 if (signal_pending(current))
920 break;
921 if (timeout && time_after(jiffies, orig_jiffies + timeout))
922 break;
923 }
Alan Cox978e5952008-04-30 00:53:59 -0700924 unlock_kernel();
Paul Fulghum705b6c72006-01-08 01:02:06 -0800925
926exit:
927 DBGINFO(("%s wait_until_sent exit\n", info->device_name));
928}
929
930static int write_room(struct tty_struct *tty)
931{
932 struct slgt_info *info = tty->driver_data;
933 int ret;
934
935 if (sanity_check(info, tty->name, "write_room"))
936 return 0;
937 ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
938 DBGINFO(("%s write_room=%d\n", info->device_name, ret));
939 return ret;
940}
941
942static void flush_chars(struct tty_struct *tty)
943{
944 struct slgt_info *info = tty->driver_data;
945 unsigned long flags;
946
947 if (sanity_check(info, tty->name, "flush_chars"))
948 return;
949 DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
950
951 if (info->tx_count <= 0 || tty->stopped ||
952 tty->hw_stopped || !info->tx_buf)
953 return;
954
955 DBGINFO(("%s flush_chars start transmit\n", info->device_name));
956
957 spin_lock_irqsave(&info->lock,flags);
958 if (!info->tx_active && info->tx_count) {
959 tx_load(info, info->tx_buf,info->tx_count);
960 tx_start(info);
961 }
962 spin_unlock_irqrestore(&info->lock,flags);
963}
964
965static void flush_buffer(struct tty_struct *tty)
966{
967 struct slgt_info *info = tty->driver_data;
968 unsigned long flags;
969
970 if (sanity_check(info, tty->name, "flush_buffer"))
971 return;
972 DBGINFO(("%s flush_buffer\n", info->device_name));
973
974 spin_lock_irqsave(&info->lock,flags);
975 if (!info->tx_active)
976 info->tx_count = 0;
977 spin_unlock_irqrestore(&info->lock,flags);
978
Paul Fulghum705b6c72006-01-08 01:02:06 -0800979 tty_wakeup(tty);
980}
981
982/*
983 * throttle (stop) transmitter
984 */
985static void tx_hold(struct tty_struct *tty)
986{
987 struct slgt_info *info = tty->driver_data;
988 unsigned long flags;
989
990 if (sanity_check(info, tty->name, "tx_hold"))
991 return;
992 DBGINFO(("%s tx_hold\n", info->device_name));
993 spin_lock_irqsave(&info->lock,flags);
994 if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
995 tx_stop(info);
996 spin_unlock_irqrestore(&info->lock,flags);
997}
998
999/*
1000 * release (start) transmitter
1001 */
1002static void tx_release(struct tty_struct *tty)
1003{
1004 struct slgt_info *info = tty->driver_data;
1005 unsigned long flags;
1006
1007 if (sanity_check(info, tty->name, "tx_release"))
1008 return;
1009 DBGINFO(("%s tx_release\n", info->device_name));
1010 spin_lock_irqsave(&info->lock,flags);
1011 if (!info->tx_active && info->tx_count) {
1012 tx_load(info, info->tx_buf, info->tx_count);
1013 tx_start(info);
1014 }
1015 spin_unlock_irqrestore(&info->lock,flags);
1016}
1017
1018/*
1019 * Service an IOCTL request
1020 *
1021 * Arguments
1022 *
1023 * tty pointer to tty instance data
1024 * file pointer to associated file object for device
1025 * cmd IOCTL command code
1026 * arg command argument/context
1027 *
1028 * Return 0 if success, otherwise error code
1029 */
1030static int ioctl(struct tty_struct *tty, struct file *file,
1031 unsigned int cmd, unsigned long arg)
1032{
1033 struct slgt_info *info = tty->driver_data;
1034 struct mgsl_icount cnow; /* kernel counter temps */
1035 struct serial_icounter_struct __user *p_cuser; /* user space */
1036 unsigned long flags;
1037 void __user *argp = (void __user *)arg;
Alan Cox1f8cabb2008-04-30 00:53:24 -07001038 int ret;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001039
1040 if (sanity_check(info, tty->name, "ioctl"))
1041 return -ENODEV;
1042 DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
1043
1044 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1045 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1046 if (tty->flags & (1 << TTY_IO_ERROR))
1047 return -EIO;
1048 }
1049
Alan Cox1f8cabb2008-04-30 00:53:24 -07001050 lock_kernel();
1051
Paul Fulghum705b6c72006-01-08 01:02:06 -08001052 switch (cmd) {
1053 case MGSL_IOCGPARAMS:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001054 ret = get_params(info, argp);
1055 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001056 case MGSL_IOCSPARAMS:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001057 ret = set_params(info, argp);
1058 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001059 case MGSL_IOCGTXIDLE:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001060 ret = get_txidle(info, argp);
1061 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001062 case MGSL_IOCSTXIDLE:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001063 ret = set_txidle(info, (int)arg);
1064 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001065 case MGSL_IOCTXENABLE:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001066 ret = tx_enable(info, (int)arg);
1067 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001068 case MGSL_IOCRXENABLE:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001069 ret = rx_enable(info, (int)arg);
1070 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001071 case MGSL_IOCTXABORT:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001072 ret = tx_abort(info);
1073 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001074 case MGSL_IOCGSTATS:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001075 ret = get_stats(info, argp);
1076 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001077 case MGSL_IOCWAITEVENT:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001078 ret = wait_mgsl_event(info, argp);
1079 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001080 case TIOCMIWAIT:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001081 ret = modem_input_wait(info,(int)arg);
1082 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001083 case MGSL_IOCGIF:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001084 ret = get_interface(info, argp);
1085 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001086 case MGSL_IOCSIF:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001087 ret = set_interface(info,(int)arg);
1088 break;
Paul Fulghum0080b7a2006-03-28 01:56:15 -08001089 case MGSL_IOCSGPIO:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001090 ret = set_gpio(info, argp);
1091 break;
Paul Fulghum0080b7a2006-03-28 01:56:15 -08001092 case MGSL_IOCGGPIO:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001093 ret = get_gpio(info, argp);
1094 break;
Paul Fulghum0080b7a2006-03-28 01:56:15 -08001095 case MGSL_IOCWAITGPIO:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001096 ret = wait_gpio(info, argp);
1097 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001098 case TIOCGICOUNT:
1099 spin_lock_irqsave(&info->lock,flags);
1100 cnow = info->icount;
1101 spin_unlock_irqrestore(&info->lock,flags);
1102 p_cuser = argp;
1103 if (put_user(cnow.cts, &p_cuser->cts) ||
1104 put_user(cnow.dsr, &p_cuser->dsr) ||
1105 put_user(cnow.rng, &p_cuser->rng) ||
1106 put_user(cnow.dcd, &p_cuser->dcd) ||
1107 put_user(cnow.rx, &p_cuser->rx) ||
1108 put_user(cnow.tx, &p_cuser->tx) ||
1109 put_user(cnow.frame, &p_cuser->frame) ||
1110 put_user(cnow.overrun, &p_cuser->overrun) ||
1111 put_user(cnow.parity, &p_cuser->parity) ||
1112 put_user(cnow.brk, &p_cuser->brk) ||
1113 put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
Alan Cox1f8cabb2008-04-30 00:53:24 -07001114 ret = -EFAULT;
1115 ret = 0;
1116 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001117 default:
Alan Cox1f8cabb2008-04-30 00:53:24 -07001118 ret = -ENOIOCTLCMD;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001119 }
Alan Cox1f8cabb2008-04-30 00:53:24 -07001120 unlock_kernel();
1121 return ret;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001122}
1123
1124/*
Paul Fulghum2acdb162007-05-10 22:22:43 -07001125 * support for 32 bit ioctl calls on 64 bit systems
1126 */
1127#ifdef CONFIG_COMPAT
1128static long get_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *user_params)
1129{
1130 struct MGSL_PARAMS32 tmp_params;
1131
1132 DBGINFO(("%s get_params32\n", info->device_name));
1133 tmp_params.mode = (compat_ulong_t)info->params.mode;
1134 tmp_params.loopback = info->params.loopback;
1135 tmp_params.flags = info->params.flags;
1136 tmp_params.encoding = info->params.encoding;
1137 tmp_params.clock_speed = (compat_ulong_t)info->params.clock_speed;
1138 tmp_params.addr_filter = info->params.addr_filter;
1139 tmp_params.crc_type = info->params.crc_type;
1140 tmp_params.preamble_length = info->params.preamble_length;
1141 tmp_params.preamble = info->params.preamble;
1142 tmp_params.data_rate = (compat_ulong_t)info->params.data_rate;
1143 tmp_params.data_bits = info->params.data_bits;
1144 tmp_params.stop_bits = info->params.stop_bits;
1145 tmp_params.parity = info->params.parity;
1146 if (copy_to_user(user_params, &tmp_params, sizeof(struct MGSL_PARAMS32)))
1147 return -EFAULT;
1148 return 0;
1149}
1150
1151static long set_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *new_params)
1152{
1153 struct MGSL_PARAMS32 tmp_params;
1154
1155 DBGINFO(("%s set_params32\n", info->device_name));
1156 if (copy_from_user(&tmp_params, new_params, sizeof(struct MGSL_PARAMS32)))
1157 return -EFAULT;
1158
1159 spin_lock(&info->lock);
Paul Fulghum1f807692009-04-02 16:58:30 -07001160 if (tmp_params.mode == MGSL_MODE_BASE_CLOCK) {
1161 info->base_clock = tmp_params.clock_speed;
1162 } else {
1163 info->params.mode = tmp_params.mode;
1164 info->params.loopback = tmp_params.loopback;
1165 info->params.flags = tmp_params.flags;
1166 info->params.encoding = tmp_params.encoding;
1167 info->params.clock_speed = tmp_params.clock_speed;
1168 info->params.addr_filter = tmp_params.addr_filter;
1169 info->params.crc_type = tmp_params.crc_type;
1170 info->params.preamble_length = tmp_params.preamble_length;
1171 info->params.preamble = tmp_params.preamble;
1172 info->params.data_rate = tmp_params.data_rate;
1173 info->params.data_bits = tmp_params.data_bits;
1174 info->params.stop_bits = tmp_params.stop_bits;
1175 info->params.parity = tmp_params.parity;
1176 }
Paul Fulghum2acdb162007-05-10 22:22:43 -07001177 spin_unlock(&info->lock);
1178
Paul Fulghum1f807692009-04-02 16:58:30 -07001179 program_hw(info);
Paul Fulghum2acdb162007-05-10 22:22:43 -07001180
1181 return 0;
1182}
1183
1184static long slgt_compat_ioctl(struct tty_struct *tty, struct file *file,
1185 unsigned int cmd, unsigned long arg)
1186{
1187 struct slgt_info *info = tty->driver_data;
1188 int rc = -ENOIOCTLCMD;
1189
1190 if (sanity_check(info, tty->name, "compat_ioctl"))
1191 return -ENODEV;
1192 DBGINFO(("%s compat_ioctl() cmd=%08X\n", info->device_name, cmd));
1193
1194 switch (cmd) {
1195
1196 case MGSL_IOCSPARAMS32:
1197 rc = set_params32(info, compat_ptr(arg));
1198 break;
1199
1200 case MGSL_IOCGPARAMS32:
1201 rc = get_params32(info, compat_ptr(arg));
1202 break;
1203
1204 case MGSL_IOCGPARAMS:
1205 case MGSL_IOCSPARAMS:
1206 case MGSL_IOCGTXIDLE:
1207 case MGSL_IOCGSTATS:
1208 case MGSL_IOCWAITEVENT:
1209 case MGSL_IOCGIF:
1210 case MGSL_IOCSGPIO:
1211 case MGSL_IOCGGPIO:
1212 case MGSL_IOCWAITGPIO:
1213 case TIOCGICOUNT:
1214 rc = ioctl(tty, file, cmd, (unsigned long)(compat_ptr(arg)));
1215 break;
1216
1217 case MGSL_IOCSTXIDLE:
1218 case MGSL_IOCTXENABLE:
1219 case MGSL_IOCRXENABLE:
1220 case MGSL_IOCTXABORT:
1221 case TIOCMIWAIT:
1222 case MGSL_IOCSIF:
1223 rc = ioctl(tty, file, cmd, arg);
1224 break;
1225 }
1226
1227 DBGINFO(("%s compat_ioctl() cmd=%08X rc=%d\n", info->device_name, cmd, rc));
1228 return rc;
1229}
1230#else
1231#define slgt_compat_ioctl NULL
1232#endif /* ifdef CONFIG_COMPAT */
1233
1234/*
Paul Fulghum705b6c72006-01-08 01:02:06 -08001235 * proc fs support
1236 */
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001237static inline void line_info(struct seq_file *m, struct slgt_info *info)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001238{
1239 char stat_buf[30];
Paul Fulghum705b6c72006-01-08 01:02:06 -08001240 unsigned long flags;
1241
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001242 seq_printf(m, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
Paul Fulghum705b6c72006-01-08 01:02:06 -08001243 info->device_name, info->phys_reg_addr,
1244 info->irq_level, info->max_frame_size);
1245
1246 /* output current serial signal states */
1247 spin_lock_irqsave(&info->lock,flags);
1248 get_signals(info);
1249 spin_unlock_irqrestore(&info->lock,flags);
1250
1251 stat_buf[0] = 0;
1252 stat_buf[1] = 0;
1253 if (info->signals & SerialSignal_RTS)
1254 strcat(stat_buf, "|RTS");
1255 if (info->signals & SerialSignal_CTS)
1256 strcat(stat_buf, "|CTS");
1257 if (info->signals & SerialSignal_DTR)
1258 strcat(stat_buf, "|DTR");
1259 if (info->signals & SerialSignal_DSR)
1260 strcat(stat_buf, "|DSR");
1261 if (info->signals & SerialSignal_DCD)
1262 strcat(stat_buf, "|CD");
1263 if (info->signals & SerialSignal_RI)
1264 strcat(stat_buf, "|RI");
1265
1266 if (info->params.mode != MGSL_MODE_ASYNC) {
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001267 seq_printf(m, "\tHDLC txok:%d rxok:%d",
Paul Fulghum705b6c72006-01-08 01:02:06 -08001268 info->icount.txok, info->icount.rxok);
1269 if (info->icount.txunder)
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001270 seq_printf(m, " txunder:%d", info->icount.txunder);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001271 if (info->icount.txabort)
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001272 seq_printf(m, " txabort:%d", info->icount.txabort);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001273 if (info->icount.rxshort)
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001274 seq_printf(m, " rxshort:%d", info->icount.rxshort);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001275 if (info->icount.rxlong)
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001276 seq_printf(m, " rxlong:%d", info->icount.rxlong);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001277 if (info->icount.rxover)
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001278 seq_printf(m, " rxover:%d", info->icount.rxover);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001279 if (info->icount.rxcrc)
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001280 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001281 } else {
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001282 seq_printf(m, "\tASYNC tx:%d rx:%d",
Paul Fulghum705b6c72006-01-08 01:02:06 -08001283 info->icount.tx, info->icount.rx);
1284 if (info->icount.frame)
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001285 seq_printf(m, " fe:%d", info->icount.frame);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001286 if (info->icount.parity)
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001287 seq_printf(m, " pe:%d", info->icount.parity);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001288 if (info->icount.brk)
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001289 seq_printf(m, " brk:%d", info->icount.brk);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001290 if (info->icount.overrun)
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001291 seq_printf(m, " oe:%d", info->icount.overrun);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001292 }
1293
1294 /* Append serial signal status to end */
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001295 seq_printf(m, " %s\n", stat_buf+1);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001296
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001297 seq_printf(m, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
Paul Fulghum705b6c72006-01-08 01:02:06 -08001298 info->tx_active,info->bh_requested,info->bh_running,
1299 info->pending_bh);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001300}
1301
1302/* Called to print information about devices
1303 */
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001304static int synclink_gt_proc_show(struct seq_file *m, void *v)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001305{
Paul Fulghum705b6c72006-01-08 01:02:06 -08001306 struct slgt_info *info;
1307
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001308 seq_puts(m, "synclink_gt driver\n");
Paul Fulghum705b6c72006-01-08 01:02:06 -08001309
1310 info = slgt_device_list;
1311 while( info ) {
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001312 line_info(m, info);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001313 info = info->next_device;
1314 }
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001315 return 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001316}
1317
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07001318static int synclink_gt_proc_open(struct inode *inode, struct file *file)
1319{
1320 return single_open(file, synclink_gt_proc_show, NULL);
1321}
1322
1323static const struct file_operations synclink_gt_proc_fops = {
1324 .owner = THIS_MODULE,
1325 .open = synclink_gt_proc_open,
1326 .read = seq_read,
1327 .llseek = seq_lseek,
1328 .release = single_release,
1329};
1330
Paul Fulghum705b6c72006-01-08 01:02:06 -08001331/*
1332 * return count of bytes in transmit buffer
1333 */
1334static int chars_in_buffer(struct tty_struct *tty)
1335{
1336 struct slgt_info *info = tty->driver_data;
Paul Fulghum403214d2008-07-22 11:21:55 +01001337 int count;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001338 if (sanity_check(info, tty->name, "chars_in_buffer"))
1339 return 0;
Paul Fulghum403214d2008-07-22 11:21:55 +01001340 count = tbuf_bytes(info);
1341 DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, count));
1342 return count;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001343}
1344
1345/*
1346 * signal remote device to throttle send data (our receive data)
1347 */
1348static void throttle(struct tty_struct * tty)
1349{
1350 struct slgt_info *info = tty->driver_data;
1351 unsigned long flags;
1352
1353 if (sanity_check(info, tty->name, "throttle"))
1354 return;
1355 DBGINFO(("%s throttle\n", info->device_name));
1356 if (I_IXOFF(tty))
1357 send_xchar(tty, STOP_CHAR(tty));
1358 if (tty->termios->c_cflag & CRTSCTS) {
1359 spin_lock_irqsave(&info->lock,flags);
1360 info->signals &= ~SerialSignal_RTS;
1361 set_signals(info);
1362 spin_unlock_irqrestore(&info->lock,flags);
1363 }
1364}
1365
1366/*
1367 * signal remote device to stop throttling send data (our receive data)
1368 */
1369static void unthrottle(struct tty_struct * tty)
1370{
1371 struct slgt_info *info = tty->driver_data;
1372 unsigned long flags;
1373
1374 if (sanity_check(info, tty->name, "unthrottle"))
1375 return;
1376 DBGINFO(("%s unthrottle\n", info->device_name));
1377 if (I_IXOFF(tty)) {
1378 if (info->x_char)
1379 info->x_char = 0;
1380 else
1381 send_xchar(tty, START_CHAR(tty));
1382 }
1383 if (tty->termios->c_cflag & CRTSCTS) {
1384 spin_lock_irqsave(&info->lock,flags);
1385 info->signals |= SerialSignal_RTS;
1386 set_signals(info);
1387 spin_unlock_irqrestore(&info->lock,flags);
1388 }
1389}
1390
1391/*
1392 * set or clear transmit break condition
1393 * break_state -1=set break condition, 0=clear
1394 */
Alan Cox9e989662008-07-22 11:18:03 +01001395static int set_break(struct tty_struct *tty, int break_state)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001396{
1397 struct slgt_info *info = tty->driver_data;
1398 unsigned short value;
1399 unsigned long flags;
1400
1401 if (sanity_check(info, tty->name, "set_break"))
Alan Cox9e989662008-07-22 11:18:03 +01001402 return -EINVAL;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001403 DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
1404
1405 spin_lock_irqsave(&info->lock,flags);
1406 value = rd_reg16(info, TCR);
1407 if (break_state == -1)
1408 value |= BIT6;
1409 else
1410 value &= ~BIT6;
1411 wr_reg16(info, TCR, value);
1412 spin_unlock_irqrestore(&info->lock,flags);
Alan Cox9e989662008-07-22 11:18:03 +01001413 return 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001414}
1415
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08001416#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08001417
1418/**
1419 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1420 * set encoding and frame check sequence (FCS) options
1421 *
1422 * dev pointer to network device structure
1423 * encoding serial encoding setting
1424 * parity FCS setting
1425 *
1426 * returns 0 if success, otherwise error code
1427 */
1428static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
1429 unsigned short parity)
1430{
1431 struct slgt_info *info = dev_to_port(dev);
1432 unsigned char new_encoding;
1433 unsigned short new_crctype;
1434
1435 /* return error if TTY interface open */
Alan Cox8fb06c72008-07-16 21:56:46 +01001436 if (info->port.count)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001437 return -EBUSY;
1438
1439 DBGINFO(("%s hdlcdev_attach\n", info->device_name));
1440
1441 switch (encoding)
1442 {
1443 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
1444 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
1445 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
1446 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
1447 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
1448 default: return -EINVAL;
1449 }
1450
1451 switch (parity)
1452 {
1453 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
1454 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
1455 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
1456 default: return -EINVAL;
1457 }
1458
1459 info->params.encoding = new_encoding;
Alexey Dobriyan53b35312006-03-24 03:16:13 -08001460 info->params.crc_type = new_crctype;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001461
1462 /* if network interface up, reprogram hardware */
1463 if (info->netcount)
1464 program_hw(info);
1465
1466 return 0;
1467}
1468
1469/**
1470 * called by generic HDLC layer to send frame
1471 *
1472 * skb socket buffer containing HDLC frame
1473 * dev pointer to network device structure
1474 *
1475 * returns 0 if success, otherwise error code
1476 */
1477static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
1478{
1479 struct slgt_info *info = dev_to_port(dev);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001480 unsigned long flags;
1481
1482 DBGINFO(("%s hdlc_xmit\n", dev->name));
1483
1484 /* stop sending until this frame completes */
1485 netif_stop_queue(dev);
1486
1487 /* copy data to device buffers */
1488 info->tx_count = skb->len;
1489 tx_load(info, skb->data, skb->len);
1490
1491 /* update network statistics */
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001492 dev->stats.tx_packets++;
1493 dev->stats.tx_bytes += skb->len;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001494
1495 /* done with socket buffer, so free it */
1496 dev_kfree_skb(skb);
1497
1498 /* save start time for transmit timeout detection */
1499 dev->trans_start = jiffies;
1500
1501 /* start hardware transmitter if necessary */
1502 spin_lock_irqsave(&info->lock,flags);
1503 if (!info->tx_active)
1504 tx_start(info);
1505 spin_unlock_irqrestore(&info->lock,flags);
1506
1507 return 0;
1508}
1509
1510/**
1511 * called by network layer when interface enabled
1512 * claim resources and initialize hardware
1513 *
1514 * dev pointer to network device structure
1515 *
1516 * returns 0 if success, otherwise error code
1517 */
1518static int hdlcdev_open(struct net_device *dev)
1519{
1520 struct slgt_info *info = dev_to_port(dev);
1521 int rc;
1522 unsigned long flags;
1523
Paul Fulghumd4c63b72007-08-22 14:01:50 -07001524 if (!try_module_get(THIS_MODULE))
1525 return -EBUSY;
1526
Paul Fulghum705b6c72006-01-08 01:02:06 -08001527 DBGINFO(("%s hdlcdev_open\n", dev->name));
1528
1529 /* generic HDLC layer open processing */
1530 if ((rc = hdlc_open(dev)))
1531 return rc;
1532
1533 /* arbitrate between network and tty opens */
1534 spin_lock_irqsave(&info->netlock, flags);
Alan Cox8fb06c72008-07-16 21:56:46 +01001535 if (info->port.count != 0 || info->netcount != 0) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08001536 DBGINFO(("%s hdlc_open busy\n", dev->name));
1537 spin_unlock_irqrestore(&info->netlock, flags);
1538 return -EBUSY;
1539 }
1540 info->netcount=1;
1541 spin_unlock_irqrestore(&info->netlock, flags);
1542
1543 /* claim resources and init adapter */
1544 if ((rc = startup(info)) != 0) {
1545 spin_lock_irqsave(&info->netlock, flags);
1546 info->netcount=0;
1547 spin_unlock_irqrestore(&info->netlock, flags);
1548 return rc;
1549 }
1550
1551 /* assert DTR and RTS, apply hardware settings */
1552 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
1553 program_hw(info);
1554
1555 /* enable network layer transmit */
1556 dev->trans_start = jiffies;
1557 netif_start_queue(dev);
1558
1559 /* inform generic HDLC layer of current DCD status */
1560 spin_lock_irqsave(&info->lock, flags);
1561 get_signals(info);
1562 spin_unlock_irqrestore(&info->lock, flags);
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07001563 if (info->signals & SerialSignal_DCD)
1564 netif_carrier_on(dev);
1565 else
1566 netif_carrier_off(dev);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001567 return 0;
1568}
1569
1570/**
1571 * called by network layer when interface is disabled
1572 * shutdown hardware and release resources
1573 *
1574 * dev pointer to network device structure
1575 *
1576 * returns 0 if success, otherwise error code
1577 */
1578static int hdlcdev_close(struct net_device *dev)
1579{
1580 struct slgt_info *info = dev_to_port(dev);
1581 unsigned long flags;
1582
1583 DBGINFO(("%s hdlcdev_close\n", dev->name));
1584
1585 netif_stop_queue(dev);
1586
1587 /* shutdown adapter and release resources */
1588 shutdown(info);
1589
1590 hdlc_close(dev);
1591
1592 spin_lock_irqsave(&info->netlock, flags);
1593 info->netcount=0;
1594 spin_unlock_irqrestore(&info->netlock, flags);
1595
Paul Fulghumd4c63b72007-08-22 14:01:50 -07001596 module_put(THIS_MODULE);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001597 return 0;
1598}
1599
1600/**
1601 * called by network layer to process IOCTL call to network device
1602 *
1603 * dev pointer to network device structure
1604 * ifr pointer to network interface request structure
1605 * cmd IOCTL command code
1606 *
1607 * returns 0 if success, otherwise error code
1608 */
1609static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1610{
1611 const size_t size = sizeof(sync_serial_settings);
1612 sync_serial_settings new_line;
1613 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1614 struct slgt_info *info = dev_to_port(dev);
1615 unsigned int flags;
1616
1617 DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
1618
1619 /* return error if TTY interface open */
Alan Cox8fb06c72008-07-16 21:56:46 +01001620 if (info->port.count)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001621 return -EBUSY;
1622
1623 if (cmd != SIOCWANDEV)
1624 return hdlc_ioctl(dev, ifr, cmd);
1625
1626 switch(ifr->ifr_settings.type) {
1627 case IF_GET_IFACE: /* return current sync_serial_settings */
1628
1629 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1630 if (ifr->ifr_settings.size < size) {
1631 ifr->ifr_settings.size = size; /* data size wanted */
1632 return -ENOBUFS;
1633 }
1634
1635 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1636 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1637 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1638 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1639
1640 switch (flags){
1641 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
1642 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
1643 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
1644 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
1645 default: new_line.clock_type = CLOCK_DEFAULT;
1646 }
1647
1648 new_line.clock_rate = info->params.clock_speed;
1649 new_line.loopback = info->params.loopback ? 1:0;
1650
1651 if (copy_to_user(line, &new_line, size))
1652 return -EFAULT;
1653 return 0;
1654
1655 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
1656
1657 if(!capable(CAP_NET_ADMIN))
1658 return -EPERM;
1659 if (copy_from_user(&new_line, line, size))
1660 return -EFAULT;
1661
1662 switch (new_line.clock_type)
1663 {
1664 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
1665 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
1666 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
1667 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
1668 case CLOCK_DEFAULT: flags = info->params.flags &
1669 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1670 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1671 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1672 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
1673 default: return -EINVAL;
1674 }
1675
1676 if (new_line.loopback != 0 && new_line.loopback != 1)
1677 return -EINVAL;
1678
1679 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1680 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1681 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1682 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1683 info->params.flags |= flags;
1684
1685 info->params.loopback = new_line.loopback;
1686
1687 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
1688 info->params.clock_speed = new_line.clock_rate;
1689 else
1690 info->params.clock_speed = 0;
1691
1692 /* if network interface up, reprogram hardware */
1693 if (info->netcount)
1694 program_hw(info);
1695 return 0;
1696
1697 default:
1698 return hdlc_ioctl(dev, ifr, cmd);
1699 }
1700}
1701
1702/**
1703 * called by network layer when transmit timeout is detected
1704 *
1705 * dev pointer to network device structure
1706 */
1707static void hdlcdev_tx_timeout(struct net_device *dev)
1708{
1709 struct slgt_info *info = dev_to_port(dev);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001710 unsigned long flags;
1711
1712 DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
1713
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001714 dev->stats.tx_errors++;
1715 dev->stats.tx_aborted_errors++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001716
1717 spin_lock_irqsave(&info->lock,flags);
1718 tx_stop(info);
1719 spin_unlock_irqrestore(&info->lock,flags);
1720
1721 netif_wake_queue(dev);
1722}
1723
1724/**
1725 * called by device driver when transmit completes
1726 * reenable network layer transmit if stopped
1727 *
1728 * info pointer to device instance information
1729 */
1730static void hdlcdev_tx_done(struct slgt_info *info)
1731{
1732 if (netif_queue_stopped(info->netdev))
1733 netif_wake_queue(info->netdev);
1734}
1735
1736/**
1737 * called by device driver when frame received
1738 * pass frame to network layer
1739 *
1740 * info pointer to device instance information
1741 * buf pointer to buffer contianing frame data
1742 * size count of data bytes in buf
1743 */
1744static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
1745{
1746 struct sk_buff *skb = dev_alloc_skb(size);
1747 struct net_device *dev = info->netdev;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001748
1749 DBGINFO(("%s hdlcdev_rx\n", dev->name));
1750
1751 if (skb == NULL) {
1752 DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001753 dev->stats.rx_dropped++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001754 return;
1755 }
1756
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001757 memcpy(skb_put(skb, size), buf, size);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001758
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001759 skb->protocol = hdlc_type_trans(skb, dev);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001760
Krzysztof Halasa198191c2008-06-30 23:26:53 +02001761 dev->stats.rx_packets++;
1762 dev->stats.rx_bytes += size;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001763
1764 netif_rx(skb);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001765}
1766
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01001767static const struct net_device_ops hdlcdev_ops = {
1768 .ndo_open = hdlcdev_open,
1769 .ndo_stop = hdlcdev_close,
1770 .ndo_change_mtu = hdlc_change_mtu,
1771 .ndo_start_xmit = hdlc_start_xmit,
1772 .ndo_do_ioctl = hdlcdev_ioctl,
1773 .ndo_tx_timeout = hdlcdev_tx_timeout,
1774};
1775
Paul Fulghum705b6c72006-01-08 01:02:06 -08001776/**
1777 * called by device driver when adding device instance
1778 * do generic HDLC initialization
1779 *
1780 * info pointer to device instance information
1781 *
1782 * returns 0 if success, otherwise error code
1783 */
1784static int hdlcdev_init(struct slgt_info *info)
1785{
1786 int rc;
1787 struct net_device *dev;
1788 hdlc_device *hdlc;
1789
1790 /* allocate and initialize network and HDLC layer objects */
1791
1792 if (!(dev = alloc_hdlcdev(info))) {
1793 printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
1794 return -ENOMEM;
1795 }
1796
1797 /* for network layer reporting purposes only */
1798 dev->mem_start = info->phys_reg_addr;
1799 dev->mem_end = info->phys_reg_addr + SLGT_REG_SIZE - 1;
1800 dev->irq = info->irq_level;
1801
1802 /* network layer callbacks and settings */
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01001803 dev->netdev_ops = &hdlcdev_ops;
1804 dev->watchdog_timeo = 10 * HZ;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001805 dev->tx_queue_len = 50;
1806
1807 /* generic HDLC layer callbacks and settings */
1808 hdlc = dev_to_hdlc(dev);
1809 hdlc->attach = hdlcdev_attach;
1810 hdlc->xmit = hdlcdev_xmit;
1811
1812 /* register objects with HDLC layer */
1813 if ((rc = register_hdlc_device(dev))) {
1814 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
1815 free_netdev(dev);
1816 return rc;
1817 }
1818
1819 info->netdev = dev;
1820 return 0;
1821}
1822
1823/**
1824 * called by device driver when removing device instance
1825 * do generic HDLC cleanup
1826 *
1827 * info pointer to device instance information
1828 */
1829static void hdlcdev_exit(struct slgt_info *info)
1830{
1831 unregister_hdlc_device(info->netdev);
1832 free_netdev(info->netdev);
1833 info->netdev = NULL;
1834}
1835
1836#endif /* ifdef CONFIG_HDLC */
1837
1838/*
1839 * get async data from rx DMA buffers
1840 */
1841static void rx_async(struct slgt_info *info)
1842{
Alan Cox8fb06c72008-07-16 21:56:46 +01001843 struct tty_struct *tty = info->port.tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001844 struct mgsl_icount *icount = &info->icount;
1845 unsigned int start, end;
1846 unsigned char *p;
1847 unsigned char status;
1848 struct slgt_desc *bufs = info->rbufs;
1849 int i, count;
Alan Cox33f0f882006-01-09 20:54:13 -08001850 int chars = 0;
1851 int stat;
1852 unsigned char ch;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001853
1854 start = end = info->rbuf_current;
1855
1856 while(desc_complete(bufs[end])) {
1857 count = desc_count(bufs[end]) - info->rbuf_index;
1858 p = bufs[end].buf + info->rbuf_index;
1859
1860 DBGISR(("%s rx_async count=%d\n", info->device_name, count));
1861 DBGDATA(info, p, count, "rx");
1862
1863 for(i=0 ; i < count; i+=2, p+=2) {
Alan Cox33f0f882006-01-09 20:54:13 -08001864 ch = *p;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001865 icount->rx++;
1866
Alan Cox33f0f882006-01-09 20:54:13 -08001867 stat = 0;
1868
Paul Fulghum202af6d2006-08-31 21:27:36 -07001869 if ((status = *(p+1) & (BIT1 + BIT0))) {
1870 if (status & BIT1)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001871 icount->parity++;
Paul Fulghum202af6d2006-08-31 21:27:36 -07001872 else if (status & BIT0)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001873 icount->frame++;
1874 /* discard char if tty control flags say so */
1875 if (status & info->ignore_status_mask)
1876 continue;
Paul Fulghum202af6d2006-08-31 21:27:36 -07001877 if (status & BIT1)
Alan Cox33f0f882006-01-09 20:54:13 -08001878 stat = TTY_PARITY;
Paul Fulghum202af6d2006-08-31 21:27:36 -07001879 else if (status & BIT0)
Alan Cox33f0f882006-01-09 20:54:13 -08001880 stat = TTY_FRAME;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001881 }
1882 if (tty) {
Alan Cox33f0f882006-01-09 20:54:13 -08001883 tty_insert_flip_char(tty, ch, stat);
1884 chars++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001885 }
1886 }
1887
1888 if (i < count) {
1889 /* receive buffer not completed */
1890 info->rbuf_index += i;
Jiri Slaby40565f12007-02-12 00:52:31 -08001891 mod_timer(&info->rx_timer, jiffies + 1);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001892 break;
1893 }
1894
1895 info->rbuf_index = 0;
1896 free_rbufs(info, end, end);
1897
1898 if (++end == info->rbuf_count)
1899 end = 0;
1900
1901 /* if entire list searched then no frame available */
1902 if (end == start)
1903 break;
1904 }
1905
Alan Cox33f0f882006-01-09 20:54:13 -08001906 if (tty && chars)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001907 tty_flip_buffer_push(tty);
1908}
1909
1910/*
1911 * return next bottom half action to perform
1912 */
1913static int bh_action(struct slgt_info *info)
1914{
1915 unsigned long flags;
1916 int rc;
1917
1918 spin_lock_irqsave(&info->lock,flags);
1919
1920 if (info->pending_bh & BH_RECEIVE) {
1921 info->pending_bh &= ~BH_RECEIVE;
1922 rc = BH_RECEIVE;
1923 } else if (info->pending_bh & BH_TRANSMIT) {
1924 info->pending_bh &= ~BH_TRANSMIT;
1925 rc = BH_TRANSMIT;
1926 } else if (info->pending_bh & BH_STATUS) {
1927 info->pending_bh &= ~BH_STATUS;
1928 rc = BH_STATUS;
1929 } else {
1930 /* Mark BH routine as complete */
Joe Perches0fab6de2008-04-28 02:14:02 -07001931 info->bh_running = false;
1932 info->bh_requested = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001933 rc = 0;
1934 }
1935
1936 spin_unlock_irqrestore(&info->lock,flags);
1937
1938 return rc;
1939}
1940
1941/*
1942 * perform bottom half processing
1943 */
David Howellsc4028952006-11-22 14:57:56 +00001944static void bh_handler(struct work_struct *work)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001945{
David Howellsc4028952006-11-22 14:57:56 +00001946 struct slgt_info *info = container_of(work, struct slgt_info, task);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001947 int action;
1948
1949 if (!info)
1950 return;
Joe Perches0fab6de2008-04-28 02:14:02 -07001951 info->bh_running = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001952
1953 while((action = bh_action(info))) {
1954 switch (action) {
1955 case BH_RECEIVE:
1956 DBGBH(("%s bh receive\n", info->device_name));
1957 switch(info->params.mode) {
1958 case MGSL_MODE_ASYNC:
1959 rx_async(info);
1960 break;
1961 case MGSL_MODE_HDLC:
1962 while(rx_get_frame(info));
1963 break;
1964 case MGSL_MODE_RAW:
Paul Fulghumcb10dc92006-09-30 23:27:45 -07001965 case MGSL_MODE_MONOSYNC:
1966 case MGSL_MODE_BISYNC:
Paul Fulghum705b6c72006-01-08 01:02:06 -08001967 while(rx_get_buf(info));
1968 break;
1969 }
1970 /* restart receiver if rx DMA buffers exhausted */
1971 if (info->rx_restart)
1972 rx_start(info);
1973 break;
1974 case BH_TRANSMIT:
1975 bh_transmit(info);
1976 break;
1977 case BH_STATUS:
1978 DBGBH(("%s bh status\n", info->device_name));
1979 info->ri_chkcount = 0;
1980 info->dsr_chkcount = 0;
1981 info->dcd_chkcount = 0;
1982 info->cts_chkcount = 0;
1983 break;
1984 default:
1985 DBGBH(("%s unknown action\n", info->device_name));
1986 break;
1987 }
1988 }
1989 DBGBH(("%s bh_handler exit\n", info->device_name));
1990}
1991
1992static void bh_transmit(struct slgt_info *info)
1993{
Alan Cox8fb06c72008-07-16 21:56:46 +01001994 struct tty_struct *tty = info->port.tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08001995
1996 DBGBH(("%s bh_transmit\n", info->device_name));
Jiri Slabyb963a842007-02-10 01:44:55 -08001997 if (tty)
Paul Fulghum705b6c72006-01-08 01:02:06 -08001998 tty_wakeup(tty);
Paul Fulghum705b6c72006-01-08 01:02:06 -08001999}
2000
Paul Fulghumed8485f2008-02-06 01:37:18 -08002001static void dsr_change(struct slgt_info *info, unsigned short status)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002002{
Paul Fulghumed8485f2008-02-06 01:37:18 -08002003 if (status & BIT3) {
2004 info->signals |= SerialSignal_DSR;
2005 info->input_signal_events.dsr_up++;
2006 } else {
2007 info->signals &= ~SerialSignal_DSR;
2008 info->input_signal_events.dsr_down++;
2009 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002010 DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
2011 if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2012 slgt_irq_off(info, IRQ_DSR);
2013 return;
2014 }
2015 info->icount.dsr++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002016 wake_up_interruptible(&info->status_event_wait_q);
2017 wake_up_interruptible(&info->event_wait_q);
2018 info->pending_bh |= BH_STATUS;
2019}
2020
Paul Fulghumed8485f2008-02-06 01:37:18 -08002021static void cts_change(struct slgt_info *info, unsigned short status)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002022{
Paul Fulghumed8485f2008-02-06 01:37:18 -08002023 if (status & BIT2) {
2024 info->signals |= SerialSignal_CTS;
2025 info->input_signal_events.cts_up++;
2026 } else {
2027 info->signals &= ~SerialSignal_CTS;
2028 info->input_signal_events.cts_down++;
2029 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002030 DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
2031 if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2032 slgt_irq_off(info, IRQ_CTS);
2033 return;
2034 }
2035 info->icount.cts++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002036 wake_up_interruptible(&info->status_event_wait_q);
2037 wake_up_interruptible(&info->event_wait_q);
2038 info->pending_bh |= BH_STATUS;
2039
Alan Cox8fb06c72008-07-16 21:56:46 +01002040 if (info->port.flags & ASYNC_CTS_FLOW) {
2041 if (info->port.tty) {
2042 if (info->port.tty->hw_stopped) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002043 if (info->signals & SerialSignal_CTS) {
Alan Cox8fb06c72008-07-16 21:56:46 +01002044 info->port.tty->hw_stopped = 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002045 info->pending_bh |= BH_TRANSMIT;
2046 return;
2047 }
2048 } else {
2049 if (!(info->signals & SerialSignal_CTS))
Alan Cox8fb06c72008-07-16 21:56:46 +01002050 info->port.tty->hw_stopped = 1;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002051 }
2052 }
2053 }
2054}
2055
Paul Fulghumed8485f2008-02-06 01:37:18 -08002056static void dcd_change(struct slgt_info *info, unsigned short status)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002057{
Paul Fulghumed8485f2008-02-06 01:37:18 -08002058 if (status & BIT1) {
2059 info->signals |= SerialSignal_DCD;
2060 info->input_signal_events.dcd_up++;
2061 } else {
2062 info->signals &= ~SerialSignal_DCD;
2063 info->input_signal_events.dcd_down++;
2064 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002065 DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
2066 if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2067 slgt_irq_off(info, IRQ_DCD);
2068 return;
2069 }
2070 info->icount.dcd++;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002071#if SYNCLINK_GENERIC_HDLC
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07002072 if (info->netcount) {
2073 if (info->signals & SerialSignal_DCD)
2074 netif_carrier_on(info->netdev);
2075 else
2076 netif_carrier_off(info->netdev);
2077 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002078#endif
2079 wake_up_interruptible(&info->status_event_wait_q);
2080 wake_up_interruptible(&info->event_wait_q);
2081 info->pending_bh |= BH_STATUS;
2082
Alan Cox8fb06c72008-07-16 21:56:46 +01002083 if (info->port.flags & ASYNC_CHECK_CD) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002084 if (info->signals & SerialSignal_DCD)
Alan Cox8fb06c72008-07-16 21:56:46 +01002085 wake_up_interruptible(&info->port.open_wait);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002086 else {
Alan Cox8fb06c72008-07-16 21:56:46 +01002087 if (info->port.tty)
2088 tty_hangup(info->port.tty);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002089 }
2090 }
2091}
2092
Paul Fulghumed8485f2008-02-06 01:37:18 -08002093static void ri_change(struct slgt_info *info, unsigned short status)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002094{
Paul Fulghumed8485f2008-02-06 01:37:18 -08002095 if (status & BIT0) {
2096 info->signals |= SerialSignal_RI;
2097 info->input_signal_events.ri_up++;
2098 } else {
2099 info->signals &= ~SerialSignal_RI;
2100 info->input_signal_events.ri_down++;
2101 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002102 DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
2103 if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2104 slgt_irq_off(info, IRQ_RI);
2105 return;
2106 }
Paul Fulghumed8485f2008-02-06 01:37:18 -08002107 info->icount.rng++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002108 wake_up_interruptible(&info->status_event_wait_q);
2109 wake_up_interruptible(&info->event_wait_q);
2110 info->pending_bh |= BH_STATUS;
2111}
2112
2113static void isr_serial(struct slgt_info *info)
2114{
2115 unsigned short status = rd_reg16(info, SSR);
2116
2117 DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
2118
2119 wr_reg16(info, SSR, status); /* clear pending */
2120
Joe Perches0fab6de2008-04-28 02:14:02 -07002121 info->irq_occurred = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002122
2123 if (info->params.mode == MGSL_MODE_ASYNC) {
2124 if (status & IRQ_TXIDLE) {
2125 if (info->tx_count)
2126 isr_txeom(info, status);
2127 }
2128 if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
2129 info->icount.brk++;
2130 /* process break detection if tty control allows */
Alan Cox8fb06c72008-07-16 21:56:46 +01002131 if (info->port.tty) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002132 if (!(status & info->ignore_status_mask)) {
2133 if (info->read_status_mask & MASK_BREAK) {
Alan Cox8fb06c72008-07-16 21:56:46 +01002134 tty_insert_flip_char(info->port.tty, 0, TTY_BREAK);
2135 if (info->port.flags & ASYNC_SAK)
2136 do_SAK(info->port.tty);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002137 }
2138 }
2139 }
2140 }
2141 } else {
2142 if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
2143 isr_txeom(info, status);
2144
2145 if (status & IRQ_RXIDLE) {
2146 if (status & RXIDLE)
2147 info->icount.rxidle++;
2148 else
2149 info->icount.exithunt++;
2150 wake_up_interruptible(&info->event_wait_q);
2151 }
2152
2153 if (status & IRQ_RXOVER)
2154 rx_start(info);
2155 }
2156
2157 if (status & IRQ_DSR)
Paul Fulghumed8485f2008-02-06 01:37:18 -08002158 dsr_change(info, status);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002159 if (status & IRQ_CTS)
Paul Fulghumed8485f2008-02-06 01:37:18 -08002160 cts_change(info, status);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002161 if (status & IRQ_DCD)
Paul Fulghumed8485f2008-02-06 01:37:18 -08002162 dcd_change(info, status);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002163 if (status & IRQ_RI)
Paul Fulghumed8485f2008-02-06 01:37:18 -08002164 ri_change(info, status);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002165}
2166
2167static void isr_rdma(struct slgt_info *info)
2168{
2169 unsigned int status = rd_reg32(info, RDCSR);
2170
2171 DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
2172
2173 /* RDCSR (rx DMA control/status)
2174 *
2175 * 31..07 reserved
2176 * 06 save status byte to DMA buffer
2177 * 05 error
2178 * 04 eol (end of list)
2179 * 03 eob (end of buffer)
2180 * 02 IRQ enable
2181 * 01 reset
2182 * 00 enable
2183 */
2184 wr_reg32(info, RDCSR, status); /* clear pending */
2185
2186 if (status & (BIT5 + BIT4)) {
2187 DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
Joe Perches0fab6de2008-04-28 02:14:02 -07002188 info->rx_restart = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002189 }
2190 info->pending_bh |= BH_RECEIVE;
2191}
2192
2193static void isr_tdma(struct slgt_info *info)
2194{
2195 unsigned int status = rd_reg32(info, TDCSR);
2196
2197 DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
2198
2199 /* TDCSR (tx DMA control/status)
2200 *
2201 * 31..06 reserved
2202 * 05 error
2203 * 04 eol (end of list)
2204 * 03 eob (end of buffer)
2205 * 02 IRQ enable
2206 * 01 reset
2207 * 00 enable
2208 */
2209 wr_reg32(info, TDCSR, status); /* clear pending */
2210
2211 if (status & (BIT5 + BIT4 + BIT3)) {
2212 // another transmit buffer has completed
2213 // run bottom half to get more send data from user
2214 info->pending_bh |= BH_TRANSMIT;
2215 }
2216}
2217
2218static void isr_txeom(struct slgt_info *info, unsigned short status)
2219{
2220 DBGISR(("%s txeom status=%04x\n", info->device_name, status));
2221
2222 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
2223 tdma_reset(info);
2224 reset_tbufs(info);
2225 if (status & IRQ_TXUNDER) {
2226 unsigned short val = rd_reg16(info, TCR);
2227 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
2228 wr_reg16(info, TCR, val); /* clear reset bit */
2229 }
2230
2231 if (info->tx_active) {
2232 if (info->params.mode != MGSL_MODE_ASYNC) {
2233 if (status & IRQ_TXUNDER)
2234 info->icount.txunder++;
2235 else if (status & IRQ_TXIDLE)
2236 info->icount.txok++;
2237 }
2238
Joe Perches0fab6de2008-04-28 02:14:02 -07002239 info->tx_active = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002240 info->tx_count = 0;
2241
2242 del_timer(&info->tx_timer);
2243
2244 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
2245 info->signals &= ~SerialSignal_RTS;
Joe Perches0fab6de2008-04-28 02:14:02 -07002246 info->drop_rts_on_tx_done = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002247 set_signals(info);
2248 }
2249
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08002250#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08002251 if (info->netcount)
2252 hdlcdev_tx_done(info);
2253 else
2254#endif
2255 {
Alan Cox8fb06c72008-07-16 21:56:46 +01002256 if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002257 tx_stop(info);
2258 return;
2259 }
2260 info->pending_bh |= BH_TRANSMIT;
2261 }
2262 }
2263}
2264
Paul Fulghum0080b7a2006-03-28 01:56:15 -08002265static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
2266{
2267 struct cond_wait *w, *prev;
2268
2269 /* wake processes waiting for specific transitions */
2270 for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
2271 if (w->data & changed) {
2272 w->data = state;
2273 wake_up_interruptible(&w->q);
2274 if (prev != NULL)
2275 prev->next = w->next;
2276 else
2277 info->gpio_wait_q = w->next;
2278 } else
2279 prev = w;
2280 }
2281}
2282
Paul Fulghum705b6c72006-01-08 01:02:06 -08002283/* interrupt service routine
2284 *
2285 * irq interrupt number
2286 * dev_id device ID supplied during interrupt registration
Paul Fulghum705b6c72006-01-08 01:02:06 -08002287 */
Jeff Garzika6f97b22007-10-31 05:20:49 -04002288static irqreturn_t slgt_interrupt(int dummy, void *dev_id)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002289{
Jeff Garzika6f97b22007-10-31 05:20:49 -04002290 struct slgt_info *info = dev_id;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002291 unsigned int gsr;
2292 unsigned int i;
2293
Jeff Garzika6f97b22007-10-31 05:20:49 -04002294 DBGISR(("slgt_interrupt irq=%d entry\n", info->irq_level));
Paul Fulghum705b6c72006-01-08 01:02:06 -08002295
2296 spin_lock(&info->lock);
2297
2298 while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
2299 DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
Joe Perches0fab6de2008-04-28 02:14:02 -07002300 info->irq_occurred = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002301 for(i=0; i < info->port_count ; i++) {
2302 if (info->port_array[i] == NULL)
2303 continue;
2304 if (gsr & (BIT8 << i))
2305 isr_serial(info->port_array[i]);
2306 if (gsr & (BIT16 << (i*2)))
2307 isr_rdma(info->port_array[i]);
2308 if (gsr & (BIT17 << (i*2)))
2309 isr_tdma(info->port_array[i]);
2310 }
2311 }
2312
Paul Fulghum0080b7a2006-03-28 01:56:15 -08002313 if (info->gpio_present) {
2314 unsigned int state;
2315 unsigned int changed;
2316 while ((changed = rd_reg32(info, IOSR)) != 0) {
2317 DBGISR(("%s iosr=%08x\n", info->device_name, changed));
2318 /* read latched state of GPIO signals */
2319 state = rd_reg32(info, IOVR);
2320 /* clear pending GPIO interrupt bits */
2321 wr_reg32(info, IOSR, changed);
2322 for (i=0 ; i < info->port_count ; i++) {
2323 if (info->port_array[i] != NULL)
2324 isr_gpio(info->port_array[i], changed, state);
2325 }
2326 }
2327 }
2328
Paul Fulghum705b6c72006-01-08 01:02:06 -08002329 for(i=0; i < info->port_count ; i++) {
2330 struct slgt_info *port = info->port_array[i];
2331
Alan Cox8fb06c72008-07-16 21:56:46 +01002332 if (port && (port->port.count || port->netcount) &&
Paul Fulghum705b6c72006-01-08 01:02:06 -08002333 port->pending_bh && !port->bh_running &&
2334 !port->bh_requested) {
2335 DBGISR(("%s bh queued\n", port->device_name));
2336 schedule_work(&port->task);
Joe Perches0fab6de2008-04-28 02:14:02 -07002337 port->bh_requested = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002338 }
2339 }
2340
2341 spin_unlock(&info->lock);
2342
Jeff Garzika6f97b22007-10-31 05:20:49 -04002343 DBGISR(("slgt_interrupt irq=%d exit\n", info->irq_level));
Paul Fulghum705b6c72006-01-08 01:02:06 -08002344 return IRQ_HANDLED;
2345}
2346
2347static int startup(struct slgt_info *info)
2348{
2349 DBGINFO(("%s startup\n", info->device_name));
2350
Alan Cox8fb06c72008-07-16 21:56:46 +01002351 if (info->port.flags & ASYNC_INITIALIZED)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002352 return 0;
2353
2354 if (!info->tx_buf) {
2355 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
2356 if (!info->tx_buf) {
2357 DBGERR(("%s can't allocate tx buffer\n", info->device_name));
2358 return -ENOMEM;
2359 }
2360 }
2361
2362 info->pending_bh = 0;
2363
2364 memset(&info->icount, 0, sizeof(info->icount));
2365
2366 /* program hardware for current parameters */
2367 change_params(info);
2368
Alan Cox8fb06c72008-07-16 21:56:46 +01002369 if (info->port.tty)
2370 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002371
Alan Cox8fb06c72008-07-16 21:56:46 +01002372 info->port.flags |= ASYNC_INITIALIZED;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002373
2374 return 0;
2375}
2376
2377/*
2378 * called by close() and hangup() to shutdown hardware
2379 */
2380static void shutdown(struct slgt_info *info)
2381{
2382 unsigned long flags;
2383
Alan Cox8fb06c72008-07-16 21:56:46 +01002384 if (!(info->port.flags & ASYNC_INITIALIZED))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002385 return;
2386
2387 DBGINFO(("%s shutdown\n", info->device_name));
2388
2389 /* clear status wait queue because status changes */
2390 /* can't happen after shutting down the hardware */
2391 wake_up_interruptible(&info->status_event_wait_q);
2392 wake_up_interruptible(&info->event_wait_q);
2393
2394 del_timer_sync(&info->tx_timer);
2395 del_timer_sync(&info->rx_timer);
2396
2397 kfree(info->tx_buf);
2398 info->tx_buf = NULL;
2399
2400 spin_lock_irqsave(&info->lock,flags);
2401
2402 tx_stop(info);
2403 rx_stop(info);
2404
2405 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
2406
Alan Cox8fb06c72008-07-16 21:56:46 +01002407 if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002408 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2409 set_signals(info);
2410 }
2411
Paul Fulghum0080b7a2006-03-28 01:56:15 -08002412 flush_cond_wait(&info->gpio_wait_q);
2413
Paul Fulghum705b6c72006-01-08 01:02:06 -08002414 spin_unlock_irqrestore(&info->lock,flags);
2415
Alan Cox8fb06c72008-07-16 21:56:46 +01002416 if (info->port.tty)
2417 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002418
Alan Cox8fb06c72008-07-16 21:56:46 +01002419 info->port.flags &= ~ASYNC_INITIALIZED;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002420}
2421
2422static void program_hw(struct slgt_info *info)
2423{
2424 unsigned long flags;
2425
2426 spin_lock_irqsave(&info->lock,flags);
2427
2428 rx_stop(info);
2429 tx_stop(info);
2430
Paul Fulghumcb10dc92006-09-30 23:27:45 -07002431 if (info->params.mode != MGSL_MODE_ASYNC ||
Paul Fulghum705b6c72006-01-08 01:02:06 -08002432 info->netcount)
Paul Fulghumcb10dc92006-09-30 23:27:45 -07002433 sync_mode(info);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002434 else
2435 async_mode(info);
2436
2437 set_signals(info);
2438
2439 info->dcd_chkcount = 0;
2440 info->cts_chkcount = 0;
2441 info->ri_chkcount = 0;
2442 info->dsr_chkcount = 0;
2443
Paul Fulghuma6b2f872009-01-15 13:50:57 -08002444 slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR | IRQ_RI);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002445 get_signals(info);
2446
2447 if (info->netcount ||
Alan Cox8fb06c72008-07-16 21:56:46 +01002448 (info->port.tty && info->port.tty->termios->c_cflag & CREAD))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002449 rx_start(info);
2450
2451 spin_unlock_irqrestore(&info->lock,flags);
2452}
2453
2454/*
2455 * reconfigure adapter based on new parameters
2456 */
2457static void change_params(struct slgt_info *info)
2458{
2459 unsigned cflag;
2460 int bits_per_char;
2461
Alan Cox8fb06c72008-07-16 21:56:46 +01002462 if (!info->port.tty || !info->port.tty->termios)
Paul Fulghum705b6c72006-01-08 01:02:06 -08002463 return;
2464 DBGINFO(("%s change_params\n", info->device_name));
2465
Alan Cox8fb06c72008-07-16 21:56:46 +01002466 cflag = info->port.tty->termios->c_cflag;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002467
2468 /* if B0 rate (hangup) specified then negate DTR and RTS */
2469 /* otherwise assert DTR and RTS */
2470 if (cflag & CBAUD)
2471 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
2472 else
2473 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2474
2475 /* byte size and parity */
2476
2477 switch (cflag & CSIZE) {
2478 case CS5: info->params.data_bits = 5; break;
2479 case CS6: info->params.data_bits = 6; break;
2480 case CS7: info->params.data_bits = 7; break;
2481 case CS8: info->params.data_bits = 8; break;
2482 default: info->params.data_bits = 7; break;
2483 }
2484
2485 info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
2486
2487 if (cflag & PARENB)
2488 info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
2489 else
2490 info->params.parity = ASYNC_PARITY_NONE;
2491
2492 /* calculate number of jiffies to transmit a full
2493 * FIFO (32 bytes) at specified data rate
2494 */
2495 bits_per_char = info->params.data_bits +
2496 info->params.stop_bits + 1;
2497
Alan Cox8fb06c72008-07-16 21:56:46 +01002498 info->params.data_rate = tty_get_baud_rate(info->port.tty);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002499
2500 if (info->params.data_rate) {
2501 info->timeout = (32*HZ*bits_per_char) /
2502 info->params.data_rate;
2503 }
2504 info->timeout += HZ/50; /* Add .02 seconds of slop */
2505
2506 if (cflag & CRTSCTS)
Alan Cox8fb06c72008-07-16 21:56:46 +01002507 info->port.flags |= ASYNC_CTS_FLOW;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002508 else
Alan Cox8fb06c72008-07-16 21:56:46 +01002509 info->port.flags &= ~ASYNC_CTS_FLOW;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002510
2511 if (cflag & CLOCAL)
Alan Cox8fb06c72008-07-16 21:56:46 +01002512 info->port.flags &= ~ASYNC_CHECK_CD;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002513 else
Alan Cox8fb06c72008-07-16 21:56:46 +01002514 info->port.flags |= ASYNC_CHECK_CD;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002515
2516 /* process tty input control flags */
2517
2518 info->read_status_mask = IRQ_RXOVER;
Alan Cox8fb06c72008-07-16 21:56:46 +01002519 if (I_INPCK(info->port.tty))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002520 info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
Alan Cox8fb06c72008-07-16 21:56:46 +01002521 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002522 info->read_status_mask |= MASK_BREAK;
Alan Cox8fb06c72008-07-16 21:56:46 +01002523 if (I_IGNPAR(info->port.tty))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002524 info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
Alan Cox8fb06c72008-07-16 21:56:46 +01002525 if (I_IGNBRK(info->port.tty)) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08002526 info->ignore_status_mask |= MASK_BREAK;
2527 /* If ignoring parity and break indicators, ignore
2528 * overruns too. (For real raw support).
2529 */
Alan Cox8fb06c72008-07-16 21:56:46 +01002530 if (I_IGNPAR(info->port.tty))
Paul Fulghum705b6c72006-01-08 01:02:06 -08002531 info->ignore_status_mask |= MASK_OVERRUN;
2532 }
2533
2534 program_hw(info);
2535}
2536
2537static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
2538{
2539 DBGINFO(("%s get_stats\n", info->device_name));
2540 if (!user_icount) {
2541 memset(&info->icount, 0, sizeof(info->icount));
2542 } else {
2543 if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
2544 return -EFAULT;
2545 }
2546 return 0;
2547}
2548
2549static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
2550{
2551 DBGINFO(("%s get_params\n", info->device_name));
2552 if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
2553 return -EFAULT;
2554 return 0;
2555}
2556
2557static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
2558{
2559 unsigned long flags;
2560 MGSL_PARAMS tmp_params;
2561
2562 DBGINFO(("%s set_params\n", info->device_name));
2563 if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
2564 return -EFAULT;
2565
2566 spin_lock_irqsave(&info->lock, flags);
Paul Fulghum1f807692009-04-02 16:58:30 -07002567 if (tmp_params.mode == MGSL_MODE_BASE_CLOCK)
2568 info->base_clock = tmp_params.clock_speed;
2569 else
2570 memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
Paul Fulghum705b6c72006-01-08 01:02:06 -08002571 spin_unlock_irqrestore(&info->lock, flags);
2572
Paul Fulghum1f807692009-04-02 16:58:30 -07002573 program_hw(info);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002574
2575 return 0;
2576}
2577
2578static int get_txidle(struct slgt_info *info, int __user *idle_mode)
2579{
2580 DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
2581 if (put_user(info->idle_mode, idle_mode))
2582 return -EFAULT;
2583 return 0;
2584}
2585
2586static int set_txidle(struct slgt_info *info, int idle_mode)
2587{
2588 unsigned long flags;
2589 DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
2590 spin_lock_irqsave(&info->lock,flags);
2591 info->idle_mode = idle_mode;
Paul Fulghum643f3312006-06-25 05:49:20 -07002592 if (info->params.mode != MGSL_MODE_ASYNC)
2593 tx_set_idle(info);
Paul Fulghum705b6c72006-01-08 01:02:06 -08002594 spin_unlock_irqrestore(&info->lock,flags);
2595 return 0;
2596}
2597
2598static int tx_enable(struct slgt_info *info, int enable)
2599{
2600 unsigned long flags;
2601 DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
2602 spin_lock_irqsave(&info->lock,flags);
2603 if (enable) {
2604 if (!info->tx_enabled)
2605 tx_start(info);
2606 } else {
2607 if (info->tx_enabled)
2608 tx_stop(info);
2609 }
2610 spin_unlock_irqrestore(&info->lock,flags);
2611 return 0;
2612}
2613
2614/*
2615 * abort transmit HDLC frame
2616 */
2617static int tx_abort(struct slgt_info *info)
2618{
2619 unsigned long flags;
2620 DBGINFO(("%s tx_abort\n", info->device_name));
2621 spin_lock_irqsave(&info->lock,flags);
2622 tdma_reset(info);
2623 spin_unlock_irqrestore(&info->lock,flags);
2624 return 0;
2625}
2626
2627static int rx_enable(struct slgt_info *info, int enable)
2628{
2629 unsigned long flags;
Paul Fulghum814dae02008-07-22 11:22:14 +01002630 unsigned int rbuf_fill_level;
2631 DBGINFO(("%s rx_enable(%08x)\n", info->device_name, enable));
Paul Fulghum705b6c72006-01-08 01:02:06 -08002632 spin_lock_irqsave(&info->lock,flags);
Paul Fulghum814dae02008-07-22 11:22:14 +01002633 /*
2634 * enable[31..16] = receive DMA buffer fill level
2635 * 0 = noop (leave fill level unchanged)
2636 * fill level must be multiple of 4 and <= buffer size
2637 */
2638 rbuf_fill_level = ((unsigned int)enable) >> 16;
2639 if (rbuf_fill_level) {
Paul Fulghumc68a99c2008-07-22 11:23:24 +01002640 if ((rbuf_fill_level > DMABUFSIZE) || (rbuf_fill_level % 4)) {
2641 spin_unlock_irqrestore(&info->lock, flags);
Paul Fulghum814dae02008-07-22 11:22:14 +01002642 return -EINVAL;
Paul Fulghumc68a99c2008-07-22 11:23:24 +01002643 }
Paul Fulghum814dae02008-07-22 11:22:14 +01002644 info->rbuf_fill_level = rbuf_fill_level;
2645 rx_stop(info); /* restart receiver to use new fill level */
2646 }
2647
2648 /*
2649 * enable[1..0] = receiver enable command
2650 * 0 = disable
2651 * 1 = enable
2652 * 2 = enable or force hunt mode if already enabled
2653 */
2654 enable &= 3;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002655 if (enable) {
2656 if (!info->rx_enabled)
2657 rx_start(info);
Paul Fulghumcb10dc92006-09-30 23:27:45 -07002658 else if (enable == 2) {
2659 /* force hunt mode (write 1 to RCR[3]) */
2660 wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
2661 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08002662 } else {
2663 if (info->rx_enabled)
2664 rx_stop(info);
2665 }
2666 spin_unlock_irqrestore(&info->lock,flags);
2667 return 0;
2668}
2669
2670/*
2671 * wait for specified event to occur
2672 */
2673static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
2674{
2675 unsigned long flags;
2676 int s;
2677 int rc=0;
2678 struct mgsl_icount cprev, cnow;
2679 int events;
2680 int mask;
2681 struct _input_signal_events oldsigs, newsigs;
2682 DECLARE_WAITQUEUE(wait, current);
2683
2684 if (get_user(mask, mask_ptr))
2685 return -EFAULT;
2686
2687 DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
2688
2689 spin_lock_irqsave(&info->lock,flags);
2690
2691 /* return immediately if state matches requested events */
2692 get_signals(info);
2693 s = info->signals;
2694
2695 events = mask &
2696 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2697 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2698 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2699 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2700 if (events) {
2701 spin_unlock_irqrestore(&info->lock,flags);
2702 goto exit;
2703 }
2704
2705 /* save current irq counts */
2706 cprev = info->icount;
2707 oldsigs = info->input_signal_events;
2708
2709 /* enable hunt and idle irqs if needed */
2710 if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
2711 unsigned short val = rd_reg16(info, SCR);
2712 if (!(val & IRQ_RXIDLE))
2713 wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
2714 }
2715
2716 set_current_state(TASK_INTERRUPTIBLE);
2717 add_wait_queue(&info->event_wait_q, &wait);
2718
2719 spin_unlock_irqrestore(&info->lock,flags);
2720
2721 for(;;) {
2722 schedule();
2723 if (signal_pending(current)) {
2724 rc = -ERESTARTSYS;
2725 break;
2726 }
2727
2728 /* get current irq counts */
2729 spin_lock_irqsave(&info->lock,flags);
2730 cnow = info->icount;
2731 newsigs = info->input_signal_events;
2732 set_current_state(TASK_INTERRUPTIBLE);
2733 spin_unlock_irqrestore(&info->lock,flags);
2734
2735 /* if no change, wait aborted for some reason */
2736 if (newsigs.dsr_up == oldsigs.dsr_up &&
2737 newsigs.dsr_down == oldsigs.dsr_down &&
2738 newsigs.dcd_up == oldsigs.dcd_up &&
2739 newsigs.dcd_down == oldsigs.dcd_down &&
2740 newsigs.cts_up == oldsigs.cts_up &&
2741 newsigs.cts_down == oldsigs.cts_down &&
2742 newsigs.ri_up == oldsigs.ri_up &&
2743 newsigs.ri_down == oldsigs.ri_down &&
2744 cnow.exithunt == cprev.exithunt &&
2745 cnow.rxidle == cprev.rxidle) {
2746 rc = -EIO;
2747 break;
2748 }
2749
2750 events = mask &
2751 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2752 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2753 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2754 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2755 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2756 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2757 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2758 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2759 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2760 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2761 if (events)
2762 break;
2763
2764 cprev = cnow;
2765 oldsigs = newsigs;
2766 }
2767
2768 remove_wait_queue(&info->event_wait_q, &wait);
2769 set_current_state(TASK_RUNNING);
2770
2771
2772 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2773 spin_lock_irqsave(&info->lock,flags);
2774 if (!waitqueue_active(&info->event_wait_q)) {
2775 /* disable enable exit hunt mode/idle rcvd IRQs */
2776 wr_reg16(info, SCR,
2777 (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
2778 }
2779 spin_unlock_irqrestore(&info->lock,flags);
2780 }
2781exit:
2782 if (rc == 0)
2783 rc = put_user(events, mask_ptr);
2784 return rc;
2785}
2786
2787static int get_interface(struct slgt_info *info, int __user *if_mode)
2788{
2789 DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
2790 if (put_user(info->if_mode, if_mode))
2791 return -EFAULT;
2792 return 0;
2793}
2794
2795static int set_interface(struct slgt_info *info, int if_mode)
2796{
2797 unsigned long flags;
Paul Fulghum35fbd392006-01-18 17:42:24 -08002798 unsigned short val;
Paul Fulghum705b6c72006-01-08 01:02:06 -08002799
2800 DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
2801 spin_lock_irqsave(&info->lock,flags);
2802 info->if_mode = if_mode;
2803
2804 msc_set_vcr(info);
2805
2806 /* TCR (tx control) 07 1=RTS driver control */
2807 val = rd_reg16(info, TCR);
2808 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
2809 val |= BIT7;
2810 else
2811 val &= ~BIT7;
2812 wr_reg16(info, TCR, val);
2813
2814 spin_unlock_irqrestore(&info->lock,flags);
2815 return 0;
2816}
2817
Paul Fulghum0080b7a2006-03-28 01:56:15 -08002818/*
2819 * set general purpose IO pin state and direction
2820 *
2821 * user_gpio fields:
2822 * state each bit indicates a pin state
2823 * smask set bit indicates pin state to set
2824 * dir each bit indicates a pin direction (0=input, 1=output)
2825 * dmask set bit indicates pin direction to set
2826 */
2827static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2828{
2829 unsigned long flags;
2830 struct gpio_desc gpio;
2831 __u32 data;
2832
2833 if (!info->gpio_present)
2834 return -EINVAL;
2835 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2836 return -EFAULT;
2837 DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
2838 info->device_name, gpio.state, gpio.smask,
2839 gpio.dir, gpio.dmask));
2840
2841 spin_lock_irqsave(&info->lock,flags);
2842 if (gpio.dmask) {
2843 data = rd_reg32(info, IODR);
2844 data |= gpio.dmask & gpio.dir;
2845 data &= ~(gpio.dmask & ~gpio.dir);
2846 wr_reg32(info, IODR, data);
2847 }
2848 if (gpio.smask) {
2849 data = rd_reg32(info, IOVR);
2850 data |= gpio.smask & gpio.state;
2851 data &= ~(gpio.smask & ~gpio.state);
2852 wr_reg32(info, IOVR, data);
2853 }
2854 spin_unlock_irqrestore(&info->lock,flags);
2855
2856 return 0;
2857}
2858
2859/*
2860 * get general purpose IO pin state and direction
2861 */
2862static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2863{
2864 struct gpio_desc gpio;
2865 if (!info->gpio_present)
2866 return -EINVAL;
2867 gpio.state = rd_reg32(info, IOVR);
2868 gpio.smask = 0xffffffff;
2869 gpio.dir = rd_reg32(info, IODR);
2870 gpio.dmask = 0xffffffff;
2871 if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
2872 return -EFAULT;
2873 DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
2874 info->device_name, gpio.state, gpio.dir));
2875 return 0;
2876}
2877
2878/*
2879 * conditional wait facility
2880 */
2881static void init_cond_wait(struct cond_wait *w, unsigned int data)
2882{
2883 init_waitqueue_head(&w->q);
2884 init_waitqueue_entry(&w->wait, current);
2885 w->data = data;
2886}
2887
2888static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
2889{
2890 set_current_state(TASK_INTERRUPTIBLE);
2891 add_wait_queue(&w->q, &w->wait);
2892 w->next = *head;
2893 *head = w;
2894}
2895
2896static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
2897{
2898 struct cond_wait *w, *prev;
2899 remove_wait_queue(&cw->q, &cw->wait);
2900 set_current_state(TASK_RUNNING);
2901 for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
2902 if (w == cw) {
2903 if (prev != NULL)
2904 prev->next = w->next;
2905 else
2906 *head = w->next;
2907 break;
2908 }
2909 }
2910}
2911
2912static void flush_cond_wait(struct cond_wait **head)
2913{
2914 while (*head != NULL) {
2915 wake_up_interruptible(&(*head)->q);
2916 *head = (*head)->next;
2917 }
2918}
2919
2920/*
2921 * wait for general purpose I/O pin(s) to enter specified state
2922 *
2923 * user_gpio fields:
2924 * state - bit indicates target pin state
2925 * smask - set bit indicates watched pin
2926 *
2927 * The wait ends when at least one watched pin enters the specified
2928 * state. When 0 (no error) is returned, user_gpio->state is set to the
2929 * state of all GPIO pins when the wait ends.
2930 *
2931 * Note: Each pin may be a dedicated input, dedicated output, or
2932 * configurable input/output. The number and configuration of pins
2933 * varies with the specific adapter model. Only input pins (dedicated
2934 * or configured) can be monitored with this function.
2935 */
2936static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2937{
2938 unsigned long flags;
2939 int rc = 0;
2940 struct gpio_desc gpio;
2941 struct cond_wait wait;
2942 u32 state;
2943
2944 if (!info->gpio_present)
2945 return -EINVAL;
2946 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2947 return -EFAULT;
2948 DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
2949 info->device_name, gpio.state, gpio.smask));
2950 /* ignore output pins identified by set IODR bit */
2951 if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
2952 return -EINVAL;
2953 init_cond_wait(&wait, gpio.smask);
2954
2955 spin_lock_irqsave(&info->lock, flags);
2956 /* enable interrupts for watched pins */
2957 wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
2958 /* get current pin states */
2959 state = rd_reg32(info, IOVR);
2960
2961 if (gpio.smask & ~(state ^ gpio.state)) {
2962 /* already in target state */
2963 gpio.state = state;
2964 } else {
2965 /* wait for target state */
2966 add_cond_wait(&info->gpio_wait_q, &wait);
2967 spin_unlock_irqrestore(&info->lock, flags);
2968 schedule();
2969 if (signal_pending(current))
2970 rc = -ERESTARTSYS;
2971 else
2972 gpio.state = wait.data;
2973 spin_lock_irqsave(&info->lock, flags);
2974 remove_cond_wait(&info->gpio_wait_q, &wait);
2975 }
2976
2977 /* disable all GPIO interrupts if no waiting processes */
2978 if (info->gpio_wait_q == NULL)
2979 wr_reg32(info, IOER, 0);
2980 spin_unlock_irqrestore(&info->lock,flags);
2981
2982 if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
2983 rc = -EFAULT;
2984 return rc;
2985}
2986
Paul Fulghum705b6c72006-01-08 01:02:06 -08002987static int modem_input_wait(struct slgt_info *info,int arg)
2988{
2989 unsigned long flags;
2990 int rc;
2991 struct mgsl_icount cprev, cnow;
2992 DECLARE_WAITQUEUE(wait, current);
2993
2994 /* save current irq counts */
2995 spin_lock_irqsave(&info->lock,flags);
2996 cprev = info->icount;
2997 add_wait_queue(&info->status_event_wait_q, &wait);
2998 set_current_state(TASK_INTERRUPTIBLE);
2999 spin_unlock_irqrestore(&info->lock,flags);
3000
3001 for(;;) {
3002 schedule();
3003 if (signal_pending(current)) {
3004 rc = -ERESTARTSYS;
3005 break;
3006 }
3007
3008 /* get new irq counts */
3009 spin_lock_irqsave(&info->lock,flags);
3010 cnow = info->icount;
3011 set_current_state(TASK_INTERRUPTIBLE);
3012 spin_unlock_irqrestore(&info->lock,flags);
3013
3014 /* if no change, wait aborted for some reason */
3015 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
3016 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
3017 rc = -EIO;
3018 break;
3019 }
3020
3021 /* check for change in caller specified modem input */
3022 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
3023 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
3024 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
3025 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
3026 rc = 0;
3027 break;
3028 }
3029
3030 cprev = cnow;
3031 }
3032 remove_wait_queue(&info->status_event_wait_q, &wait);
3033 set_current_state(TASK_RUNNING);
3034 return rc;
3035}
3036
3037/*
3038 * return state of serial control and status signals
3039 */
3040static int tiocmget(struct tty_struct *tty, struct file *file)
3041{
3042 struct slgt_info *info = tty->driver_data;
3043 unsigned int result;
3044 unsigned long flags;
3045
3046 spin_lock_irqsave(&info->lock,flags);
3047 get_signals(info);
3048 spin_unlock_irqrestore(&info->lock,flags);
3049
3050 result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
3051 ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
3052 ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
3053 ((info->signals & SerialSignal_RI) ? TIOCM_RNG:0) +
3054 ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
3055 ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
3056
3057 DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
3058 return result;
3059}
3060
3061/*
3062 * set modem control signals (DTR/RTS)
3063 *
3064 * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
3065 * TIOCMSET = set/clear signal values
3066 * value bit mask for command
3067 */
3068static int tiocmset(struct tty_struct *tty, struct file *file,
3069 unsigned int set, unsigned int clear)
3070{
3071 struct slgt_info *info = tty->driver_data;
3072 unsigned long flags;
3073
3074 DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
3075
3076 if (set & TIOCM_RTS)
3077 info->signals |= SerialSignal_RTS;
3078 if (set & TIOCM_DTR)
3079 info->signals |= SerialSignal_DTR;
3080 if (clear & TIOCM_RTS)
3081 info->signals &= ~SerialSignal_RTS;
3082 if (clear & TIOCM_DTR)
3083 info->signals &= ~SerialSignal_DTR;
3084
3085 spin_lock_irqsave(&info->lock,flags);
3086 set_signals(info);
3087 spin_unlock_irqrestore(&info->lock,flags);
3088 return 0;
3089}
3090
Alan Cox31f35932009-01-02 13:45:05 +00003091static int carrier_raised(struct tty_port *port)
3092{
3093 unsigned long flags;
3094 struct slgt_info *info = container_of(port, struct slgt_info, port);
3095
3096 spin_lock_irqsave(&info->lock,flags);
3097 get_signals(info);
3098 spin_unlock_irqrestore(&info->lock,flags);
3099 return (info->signals & SerialSignal_DCD) ? 1 : 0;
3100}
3101
Alan Coxfcc8ac12009-06-11 12:24:17 +01003102static void dtr_rts(struct tty_port *port, int on)
Alan Cox5d951fb2009-01-02 13:45:19 +00003103{
3104 unsigned long flags;
3105 struct slgt_info *info = container_of(port, struct slgt_info, port);
3106
3107 spin_lock_irqsave(&info->lock,flags);
Alan Coxfcc8ac12009-06-11 12:24:17 +01003108 if (on)
3109 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
3110 else
3111 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
Alan Cox5d951fb2009-01-02 13:45:19 +00003112 set_signals(info);
3113 spin_unlock_irqrestore(&info->lock,flags);
3114}
3115
3116
Paul Fulghum705b6c72006-01-08 01:02:06 -08003117/*
3118 * block current process until the device is ready to open
3119 */
3120static int block_til_ready(struct tty_struct *tty, struct file *filp,
3121 struct slgt_info *info)
3122{
3123 DECLARE_WAITQUEUE(wait, current);
3124 int retval;
Joe Perches0fab6de2008-04-28 02:14:02 -07003125 bool do_clocal = false;
3126 bool extra_count = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003127 unsigned long flags;
Alan Cox31f35932009-01-02 13:45:05 +00003128 int cd;
3129 struct tty_port *port = &info->port;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003130
3131 DBGINFO(("%s block_til_ready\n", tty->driver->name));
3132
3133 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3134 /* nonblock mode is set or port is not enabled */
Alan Cox31f35932009-01-02 13:45:05 +00003135 port->flags |= ASYNC_NORMAL_ACTIVE;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003136 return 0;
3137 }
3138
3139 if (tty->termios->c_cflag & CLOCAL)
Joe Perches0fab6de2008-04-28 02:14:02 -07003140 do_clocal = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003141
3142 /* Wait for carrier detect and the line to become
3143 * free (i.e., not in use by the callout). While we are in
Alan Cox31f35932009-01-02 13:45:05 +00003144 * this loop, port->count is dropped by one, so that
Paul Fulghum705b6c72006-01-08 01:02:06 -08003145 * close() knows when to free things. We restore it upon
3146 * exit, either normal or abnormal.
3147 */
3148
3149 retval = 0;
Alan Cox31f35932009-01-02 13:45:05 +00003150 add_wait_queue(&port->open_wait, &wait);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003151
3152 spin_lock_irqsave(&info->lock, flags);
3153 if (!tty_hung_up_p(filp)) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003154 extra_count = true;
Alan Cox31f35932009-01-02 13:45:05 +00003155 port->count--;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003156 }
3157 spin_unlock_irqrestore(&info->lock, flags);
Alan Cox31f35932009-01-02 13:45:05 +00003158 port->blocked_open++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003159
3160 while (1) {
Alan Cox5d951fb2009-01-02 13:45:19 +00003161 if ((tty->termios->c_cflag & CBAUD))
3162 tty_port_raise_dtr_rts(port);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003163
3164 set_current_state(TASK_INTERRUPTIBLE);
3165
Alan Cox31f35932009-01-02 13:45:05 +00003166 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)){
3167 retval = (port->flags & ASYNC_HUP_NOTIFY) ?
Paul Fulghum705b6c72006-01-08 01:02:06 -08003168 -EAGAIN : -ERESTARTSYS;
3169 break;
3170 }
3171
Alan Cox31f35932009-01-02 13:45:05 +00003172 cd = tty_port_carrier_raised(port);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003173
Alan Cox31f35932009-01-02 13:45:05 +00003174 if (!(port->flags & ASYNC_CLOSING) && (do_clocal || cd ))
Paul Fulghum705b6c72006-01-08 01:02:06 -08003175 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003176
3177 if (signal_pending(current)) {
3178 retval = -ERESTARTSYS;
3179 break;
3180 }
3181
3182 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
3183 schedule();
3184 }
3185
3186 set_current_state(TASK_RUNNING);
Alan Cox31f35932009-01-02 13:45:05 +00003187 remove_wait_queue(&port->open_wait, &wait);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003188
3189 if (extra_count)
Alan Cox31f35932009-01-02 13:45:05 +00003190 port->count++;
3191 port->blocked_open--;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003192
3193 if (!retval)
Alan Cox31f35932009-01-02 13:45:05 +00003194 port->flags |= ASYNC_NORMAL_ACTIVE;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003195
3196 DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
3197 return retval;
3198}
3199
3200static int alloc_tmp_rbuf(struct slgt_info *info)
3201{
Paul Fulghum04b374d2006-06-25 05:49:21 -07003202 info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003203 if (info->tmp_rbuf == NULL)
3204 return -ENOMEM;
3205 return 0;
3206}
3207
3208static void free_tmp_rbuf(struct slgt_info *info)
3209{
3210 kfree(info->tmp_rbuf);
3211 info->tmp_rbuf = NULL;
3212}
3213
3214/*
3215 * allocate DMA descriptor lists.
3216 */
3217static int alloc_desc(struct slgt_info *info)
3218{
3219 unsigned int i;
3220 unsigned int pbufs;
3221
3222 /* allocate memory to hold descriptor lists */
3223 info->bufs = pci_alloc_consistent(info->pdev, DESC_LIST_SIZE, &info->bufs_dma_addr);
3224 if (info->bufs == NULL)
3225 return -ENOMEM;
3226
3227 memset(info->bufs, 0, DESC_LIST_SIZE);
3228
3229 info->rbufs = (struct slgt_desc*)info->bufs;
3230 info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
3231
3232 pbufs = (unsigned int)info->bufs_dma_addr;
3233
3234 /*
3235 * Build circular lists of descriptors
3236 */
3237
3238 for (i=0; i < info->rbuf_count; i++) {
3239 /* physical address of this descriptor */
3240 info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
3241
3242 /* physical address of next descriptor */
3243 if (i == info->rbuf_count - 1)
3244 info->rbufs[i].next = cpu_to_le32(pbufs);
3245 else
3246 info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
3247 set_desc_count(info->rbufs[i], DMABUFSIZE);
3248 }
3249
3250 for (i=0; i < info->tbuf_count; i++) {
3251 /* physical address of this descriptor */
3252 info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
3253
3254 /* physical address of next descriptor */
3255 if (i == info->tbuf_count - 1)
3256 info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
3257 else
3258 info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
3259 }
3260
3261 return 0;
3262}
3263
3264static void free_desc(struct slgt_info *info)
3265{
3266 if (info->bufs != NULL) {
3267 pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
3268 info->bufs = NULL;
3269 info->rbufs = NULL;
3270 info->tbufs = NULL;
3271 }
3272}
3273
3274static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3275{
3276 int i;
3277 for (i=0; i < count; i++) {
3278 if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
3279 return -ENOMEM;
3280 bufs[i].pbuf = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
3281 }
3282 return 0;
3283}
3284
3285static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3286{
3287 int i;
3288 for (i=0; i < count; i++) {
3289 if (bufs[i].buf == NULL)
3290 continue;
3291 pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
3292 bufs[i].buf = NULL;
3293 }
3294}
3295
3296static int alloc_dma_bufs(struct slgt_info *info)
3297{
3298 info->rbuf_count = 32;
3299 info->tbuf_count = 32;
3300
3301 if (alloc_desc(info) < 0 ||
3302 alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
3303 alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
3304 alloc_tmp_rbuf(info) < 0) {
3305 DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
3306 return -ENOMEM;
3307 }
3308 reset_rbufs(info);
3309 return 0;
3310}
3311
3312static void free_dma_bufs(struct slgt_info *info)
3313{
3314 if (info->bufs) {
3315 free_bufs(info, info->rbufs, info->rbuf_count);
3316 free_bufs(info, info->tbufs, info->tbuf_count);
3317 free_desc(info);
3318 }
3319 free_tmp_rbuf(info);
3320}
3321
3322static int claim_resources(struct slgt_info *info)
3323{
3324 if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
3325 DBGERR(("%s reg addr conflict, addr=%08X\n",
3326 info->device_name, info->phys_reg_addr));
3327 info->init_error = DiagStatus_AddressConflict;
3328 goto errout;
3329 }
3330 else
Joe Perches0fab6de2008-04-28 02:14:02 -07003331 info->reg_addr_requested = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003332
Alan Cox24cb2332008-04-30 00:54:19 -07003333 info->reg_addr = ioremap_nocache(info->phys_reg_addr, SLGT_REG_SIZE);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003334 if (!info->reg_addr) {
3335 DBGERR(("%s cant map device registers, addr=%08X\n",
3336 info->device_name, info->phys_reg_addr));
3337 info->init_error = DiagStatus_CantAssignPciResources;
3338 goto errout;
3339 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08003340 return 0;
3341
3342errout:
3343 release_resources(info);
3344 return -ENODEV;
3345}
3346
3347static void release_resources(struct slgt_info *info)
3348{
3349 if (info->irq_requested) {
3350 free_irq(info->irq_level, info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003351 info->irq_requested = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003352 }
3353
3354 if (info->reg_addr_requested) {
3355 release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
Joe Perches0fab6de2008-04-28 02:14:02 -07003356 info->reg_addr_requested = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003357 }
3358
3359 if (info->reg_addr) {
Paul Fulghum0c8365e2006-01-11 12:17:39 -08003360 iounmap(info->reg_addr);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003361 info->reg_addr = NULL;
3362 }
3363}
3364
3365/* Add the specified device instance data structure to the
3366 * global linked list of devices and increment the device count.
3367 */
3368static void add_device(struct slgt_info *info)
3369{
3370 char *devstr;
3371
3372 info->next_device = NULL;
3373 info->line = slgt_device_count;
3374 sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
3375
3376 if (info->line < MAX_DEVICES) {
3377 if (maxframe[info->line])
3378 info->max_frame_size = maxframe[info->line];
Paul Fulghum705b6c72006-01-08 01:02:06 -08003379 }
3380
3381 slgt_device_count++;
3382
3383 if (!slgt_device_list)
3384 slgt_device_list = info;
3385 else {
3386 struct slgt_info *current_dev = slgt_device_list;
3387 while(current_dev->next_device)
3388 current_dev = current_dev->next_device;
3389 current_dev->next_device = info;
3390 }
3391
3392 if (info->max_frame_size < 4096)
3393 info->max_frame_size = 4096;
3394 else if (info->max_frame_size > 65535)
3395 info->max_frame_size = 65535;
3396
3397 switch(info->pdev->device) {
3398 case SYNCLINK_GT_DEVICE_ID:
3399 devstr = "GT";
3400 break;
Paul Fulghum6f84be82006-06-25 05:49:22 -07003401 case SYNCLINK_GT2_DEVICE_ID:
3402 devstr = "GT2";
3403 break;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003404 case SYNCLINK_GT4_DEVICE_ID:
3405 devstr = "GT4";
3406 break;
3407 case SYNCLINK_AC_DEVICE_ID:
3408 devstr = "AC";
3409 info->params.mode = MGSL_MODE_ASYNC;
3410 break;
3411 default:
3412 devstr = "(unknown model)";
3413 }
3414 printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
3415 devstr, info->device_name, info->phys_reg_addr,
3416 info->irq_level, info->max_frame_size);
3417
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003418#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08003419 hdlcdev_init(info);
3420#endif
3421}
3422
Alan Cox31f35932009-01-02 13:45:05 +00003423static const struct tty_port_operations slgt_port_ops = {
3424 .carrier_raised = carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +01003425 .dtr_rts = dtr_rts,
Alan Cox31f35932009-01-02 13:45:05 +00003426};
3427
Paul Fulghum705b6c72006-01-08 01:02:06 -08003428/*
3429 * allocate device instance structure, return NULL on failure
3430 */
3431static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
3432{
3433 struct slgt_info *info;
3434
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003435 info = kzalloc(sizeof(struct slgt_info), GFP_KERNEL);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003436
3437 if (!info) {
3438 DBGERR(("%s device alloc failed adapter=%d port=%d\n",
3439 driver_name, adapter_num, port_num));
3440 } else {
Alan Cox44b7d1b2008-07-16 21:57:18 +01003441 tty_port_init(&info->port);
Alan Cox31f35932009-01-02 13:45:05 +00003442 info->port.ops = &slgt_port_ops;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003443 info->magic = MGSL_MAGIC;
David Howellsc4028952006-11-22 14:57:56 +00003444 INIT_WORK(&info->task, bh_handler);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003445 info->max_frame_size = 4096;
Paul Fulghum1f807692009-04-02 16:58:30 -07003446 info->base_clock = 14745600;
Paul Fulghum814dae02008-07-22 11:22:14 +01003447 info->rbuf_fill_level = DMABUFSIZE;
Alan Cox44b7d1b2008-07-16 21:57:18 +01003448 info->port.close_delay = 5*HZ/10;
3449 info->port.closing_wait = 30*HZ;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003450 init_waitqueue_head(&info->status_event_wait_q);
3451 init_waitqueue_head(&info->event_wait_q);
3452 spin_lock_init(&info->netlock);
3453 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
3454 info->idle_mode = HDLC_TXIDLE_FLAGS;
3455 info->adapter_num = adapter_num;
3456 info->port_num = port_num;
3457
Jiri Slaby40565f12007-02-12 00:52:31 -08003458 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
3459 setup_timer(&info->rx_timer, rx_timeout, (unsigned long)info);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003460
3461 /* Copy configuration info to device instance data */
3462 info->pdev = pdev;
3463 info->irq_level = pdev->irq;
3464 info->phys_reg_addr = pci_resource_start(pdev,0);
3465
Paul Fulghum705b6c72006-01-08 01:02:06 -08003466 info->bus_type = MGSL_BUS_TYPE_PCI;
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -07003467 info->irq_flags = IRQF_SHARED;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003468
3469 info->init_error = -1; /* assume error, set to 0 on successful init */
3470 }
3471
3472 return info;
3473}
3474
3475static void device_init(int adapter_num, struct pci_dev *pdev)
3476{
3477 struct slgt_info *port_array[SLGT_MAX_PORTS];
3478 int i;
3479 int port_count = 1;
3480
Paul Fulghum6f84be82006-06-25 05:49:22 -07003481 if (pdev->device == SYNCLINK_GT2_DEVICE_ID)
3482 port_count = 2;
3483 else if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
Paul Fulghum705b6c72006-01-08 01:02:06 -08003484 port_count = 4;
3485
3486 /* allocate device instances for all ports */
3487 for (i=0; i < port_count; ++i) {
3488 port_array[i] = alloc_dev(adapter_num, i, pdev);
3489 if (port_array[i] == NULL) {
3490 for (--i; i >= 0; --i)
3491 kfree(port_array[i]);
3492 return;
3493 }
3494 }
3495
3496 /* give copy of port_array to all ports and add to device list */
3497 for (i=0; i < port_count; ++i) {
3498 memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
3499 add_device(port_array[i]);
3500 port_array[i]->port_count = port_count;
3501 spin_lock_init(&port_array[i]->lock);
3502 }
3503
3504 /* Allocate and claim adapter resources */
3505 if (!claim_resources(port_array[0])) {
3506
3507 alloc_dma_bufs(port_array[0]);
3508
3509 /* copy resource information from first port to others */
3510 for (i = 1; i < port_count; ++i) {
3511 port_array[i]->lock = port_array[0]->lock;
3512 port_array[i]->irq_level = port_array[0]->irq_level;
3513 port_array[i]->reg_addr = port_array[0]->reg_addr;
3514 alloc_dma_bufs(port_array[i]);
3515 }
3516
3517 if (request_irq(port_array[0]->irq_level,
3518 slgt_interrupt,
3519 port_array[0]->irq_flags,
3520 port_array[0]->device_name,
3521 port_array[0]) < 0) {
3522 DBGERR(("%s request_irq failed IRQ=%d\n",
3523 port_array[0]->device_name,
3524 port_array[0]->irq_level));
3525 } else {
Joe Perches0fab6de2008-04-28 02:14:02 -07003526 port_array[0]->irq_requested = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003527 adapter_test(port_array[0]);
Paul Fulghum0080b7a2006-03-28 01:56:15 -08003528 for (i=1 ; i < port_count ; i++) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08003529 port_array[i]->init_error = port_array[0]->init_error;
Paul Fulghum0080b7a2006-03-28 01:56:15 -08003530 port_array[i]->gpio_present = port_array[0]->gpio_present;
3531 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08003532 }
3533 }
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003534
3535 for (i=0; i < port_count; ++i)
3536 tty_register_device(serial_driver, port_array[i]->line, &(port_array[i]->pdev->dev));
Paul Fulghum705b6c72006-01-08 01:02:06 -08003537}
3538
3539static int __devinit init_one(struct pci_dev *dev,
3540 const struct pci_device_id *ent)
3541{
3542 if (pci_enable_device(dev)) {
3543 printk("error enabling pci device %p\n", dev);
3544 return -EIO;
3545 }
3546 pci_set_master(dev);
3547 device_init(slgt_device_count, dev);
3548 return 0;
3549}
3550
3551static void __devexit remove_one(struct pci_dev *dev)
3552{
3553}
3554
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003555static const struct tty_operations ops = {
Paul Fulghum705b6c72006-01-08 01:02:06 -08003556 .open = open,
3557 .close = close,
3558 .write = write,
3559 .put_char = put_char,
3560 .flush_chars = flush_chars,
3561 .write_room = write_room,
3562 .chars_in_buffer = chars_in_buffer,
3563 .flush_buffer = flush_buffer,
3564 .ioctl = ioctl,
Paul Fulghum2acdb162007-05-10 22:22:43 -07003565 .compat_ioctl = slgt_compat_ioctl,
Paul Fulghum705b6c72006-01-08 01:02:06 -08003566 .throttle = throttle,
3567 .unthrottle = unthrottle,
3568 .send_xchar = send_xchar,
3569 .break_ctl = set_break,
3570 .wait_until_sent = wait_until_sent,
Paul Fulghum705b6c72006-01-08 01:02:06 -08003571 .set_termios = set_termios,
3572 .stop = tx_hold,
3573 .start = tx_release,
3574 .hangup = hangup,
3575 .tiocmget = tiocmget,
3576 .tiocmset = tiocmset,
Alexey Dobriyana18c56e2009-03-31 15:19:19 -07003577 .proc_fops = &synclink_gt_proc_fops,
Paul Fulghum705b6c72006-01-08 01:02:06 -08003578};
3579
3580static void slgt_cleanup(void)
3581{
3582 int rc;
3583 struct slgt_info *info;
3584 struct slgt_info *tmp;
3585
Paul Fulghuma6b2f872009-01-15 13:50:57 -08003586 printk(KERN_INFO "unload %s\n", driver_name);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003587
3588 if (serial_driver) {
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003589 for (info=slgt_device_list ; info != NULL ; info=info->next_device)
3590 tty_unregister_device(serial_driver, info->line);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003591 if ((rc = tty_unregister_driver(serial_driver)))
3592 DBGERR(("tty_unregister_driver error=%d\n", rc));
3593 put_tty_driver(serial_driver);
3594 }
3595
3596 /* reset devices */
3597 info = slgt_device_list;
3598 while(info) {
3599 reset_port(info);
3600 info = info->next_device;
3601 }
3602
3603 /* release devices */
3604 info = slgt_device_list;
3605 while(info) {
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08003606#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08003607 hdlcdev_exit(info);
3608#endif
3609 free_dma_bufs(info);
3610 free_tmp_rbuf(info);
3611 if (info->port_num == 0)
3612 release_resources(info);
3613 tmp = info;
3614 info = info->next_device;
3615 kfree(tmp);
3616 }
3617
3618 if (pci_registered)
3619 pci_unregister_driver(&pci_driver);
3620}
3621
3622/*
3623 * Driver initialization entry point.
3624 */
3625static int __init slgt_init(void)
3626{
3627 int rc;
3628
Paul Fulghuma6b2f872009-01-15 13:50:57 -08003629 printk(KERN_INFO "%s\n", driver_name);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003630
Paul Fulghum705b6c72006-01-08 01:02:06 -08003631 serial_driver = alloc_tty_driver(MAX_DEVICES);
3632 if (!serial_driver) {
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003633 printk("%s can't allocate tty driver\n", driver_name);
3634 return -ENOMEM;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003635 }
3636
3637 /* Initialize the tty_driver structure */
3638
3639 serial_driver->owner = THIS_MODULE;
3640 serial_driver->driver_name = tty_driver_name;
3641 serial_driver->name = tty_dev_prefix;
3642 serial_driver->major = ttymajor;
3643 serial_driver->minor_start = 64;
3644 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3645 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3646 serial_driver->init_termios = tty_std_termios;
3647 serial_driver->init_termios.c_cflag =
3648 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08003649 serial_driver->init_termios.c_ispeed = 9600;
3650 serial_driver->init_termios.c_ospeed = 9600;
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003651 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003652 tty_set_operations(serial_driver, &ops);
3653 if ((rc = tty_register_driver(serial_driver)) < 0) {
3654 DBGERR(("%s can't register serial driver\n", driver_name));
3655 put_tty_driver(serial_driver);
3656 serial_driver = NULL;
3657 goto error;
3658 }
3659
Paul Fulghuma6b2f872009-01-15 13:50:57 -08003660 printk(KERN_INFO "%s, tty major#%d\n",
3661 driver_name, serial_driver->major);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003662
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003663 slgt_device_count = 0;
3664 if ((rc = pci_register_driver(&pci_driver)) < 0) {
3665 printk("%s pci_register_driver error=%d\n", driver_name, rc);
3666 goto error;
3667 }
Joe Perches0fab6de2008-04-28 02:14:02 -07003668 pci_registered = true;
Paul Fulghum62eb5b12007-05-08 00:31:48 -07003669
3670 if (!slgt_device_list)
3671 printk("%s no devices found\n",driver_name);
3672
Paul Fulghum705b6c72006-01-08 01:02:06 -08003673 return 0;
3674
3675error:
3676 slgt_cleanup();
3677 return rc;
3678}
3679
3680static void __exit slgt_exit(void)
3681{
3682 slgt_cleanup();
3683}
3684
3685module_init(slgt_init);
3686module_exit(slgt_exit);
3687
3688/*
3689 * register access routines
3690 */
3691
3692#define CALC_REGADDR() \
3693 unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
3694 if (addr >= 0x80) \
3695 reg_addr += (info->port_num) * 32;
3696
3697static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
3698{
3699 CALC_REGADDR();
3700 return readb((void __iomem *)reg_addr);
3701}
3702
3703static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
3704{
3705 CALC_REGADDR();
3706 writeb(value, (void __iomem *)reg_addr);
3707}
3708
3709static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
3710{
3711 CALC_REGADDR();
3712 return readw((void __iomem *)reg_addr);
3713}
3714
3715static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
3716{
3717 CALC_REGADDR();
3718 writew(value, (void __iomem *)reg_addr);
3719}
3720
3721static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
3722{
3723 CALC_REGADDR();
3724 return readl((void __iomem *)reg_addr);
3725}
3726
3727static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
3728{
3729 CALC_REGADDR();
3730 writel(value, (void __iomem *)reg_addr);
3731}
3732
3733static void rdma_reset(struct slgt_info *info)
3734{
3735 unsigned int i;
3736
3737 /* set reset bit */
3738 wr_reg32(info, RDCSR, BIT1);
3739
3740 /* wait for enable bit cleared */
3741 for(i=0 ; i < 1000 ; i++)
3742 if (!(rd_reg32(info, RDCSR) & BIT0))
3743 break;
3744}
3745
3746static void tdma_reset(struct slgt_info *info)
3747{
3748 unsigned int i;
3749
3750 /* set reset bit */
3751 wr_reg32(info, TDCSR, BIT1);
3752
3753 /* wait for enable bit cleared */
3754 for(i=0 ; i < 1000 ; i++)
3755 if (!(rd_reg32(info, TDCSR) & BIT0))
3756 break;
3757}
3758
3759/*
3760 * enable internal loopback
3761 * TxCLK and RxCLK are generated from BRG
3762 * and TxD is looped back to RxD internally.
3763 */
3764static void enable_loopback(struct slgt_info *info)
3765{
3766 /* SCR (serial control) BIT2=looopback enable */
3767 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
3768
3769 if (info->params.mode != MGSL_MODE_ASYNC) {
3770 /* CCR (clock control)
3771 * 07..05 tx clock source (010 = BRG)
3772 * 04..02 rx clock source (010 = BRG)
3773 * 01 auxclk enable (0 = disable)
3774 * 00 BRG enable (1 = enable)
3775 *
3776 * 0100 1001
3777 */
3778 wr_reg8(info, CCR, 0x49);
3779
3780 /* set speed if available, otherwise use default */
3781 if (info->params.clock_speed)
3782 set_rate(info, info->params.clock_speed);
3783 else
3784 set_rate(info, 3686400);
3785 }
3786}
3787
3788/*
3789 * set baud rate generator to specified rate
3790 */
3791static void set_rate(struct slgt_info *info, u32 rate)
3792{
3793 unsigned int div;
Paul Fulghum1f807692009-04-02 16:58:30 -07003794 unsigned int osc = info->base_clock;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003795
3796 /* div = osc/rate - 1
3797 *
3798 * Round div up if osc/rate is not integer to
3799 * force to next slowest rate.
3800 */
3801
3802 if (rate) {
3803 div = osc/rate;
3804 if (!(osc % rate) && div)
3805 div--;
3806 wr_reg16(info, BDR, (unsigned short)div);
3807 }
3808}
3809
3810static void rx_stop(struct slgt_info *info)
3811{
3812 unsigned short val;
3813
3814 /* disable and reset receiver */
3815 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3816 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3817 wr_reg16(info, RCR, val); /* clear reset bit */
3818
3819 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
3820
3821 /* clear pending rx interrupts */
3822 wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
3823
3824 rdma_reset(info);
3825
Joe Perches0fab6de2008-04-28 02:14:02 -07003826 info->rx_enabled = false;
3827 info->rx_restart = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003828}
3829
3830static void rx_start(struct slgt_info *info)
3831{
3832 unsigned short val;
3833
3834 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
3835
3836 /* clear pending rx overrun IRQ */
3837 wr_reg16(info, SSR, IRQ_RXOVER);
3838
3839 /* reset and disable receiver */
3840 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3841 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3842 wr_reg16(info, RCR, val); /* clear reset bit */
3843
3844 rdma_reset(info);
3845 reset_rbufs(info);
3846
3847 /* set 1st descriptor address */
3848 wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
3849
3850 if (info->params.mode != MGSL_MODE_ASYNC) {
3851 /* enable rx DMA and DMA interrupt */
3852 wr_reg32(info, RDCSR, (BIT2 + BIT0));
3853 } else {
3854 /* enable saving of rx status, rx DMA and DMA interrupt */
3855 wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
3856 }
3857
3858 slgt_irq_on(info, IRQ_RXOVER);
3859
3860 /* enable receiver */
3861 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
3862
Joe Perches0fab6de2008-04-28 02:14:02 -07003863 info->rx_restart = false;
3864 info->rx_enabled = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003865}
3866
3867static void tx_start(struct slgt_info *info)
3868{
3869 if (!info->tx_enabled) {
3870 wr_reg16(info, TCR,
Paul Fulghumcb10dc92006-09-30 23:27:45 -07003871 (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
Joe Perches0fab6de2008-04-28 02:14:02 -07003872 info->tx_enabled = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003873 }
3874
3875 if (info->tx_count) {
Joe Perches0fab6de2008-04-28 02:14:02 -07003876 info->drop_rts_on_tx_done = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003877
3878 if (info->params.mode != MGSL_MODE_ASYNC) {
3879 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3880 get_signals(info);
3881 if (!(info->signals & SerialSignal_RTS)) {
3882 info->signals |= SerialSignal_RTS;
3883 set_signals(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003884 info->drop_rts_on_tx_done = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003885 }
3886 }
3887
3888 slgt_irq_off(info, IRQ_TXDATA);
3889 slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
3890 /* clear tx idle and underrun status bits */
3891 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
Jiri Slaby40565f12007-02-12 00:52:31 -08003892 if (info->params.mode == MGSL_MODE_HDLC)
3893 mod_timer(&info->tx_timer, jiffies +
3894 msecs_to_jiffies(5000));
Paul Fulghum705b6c72006-01-08 01:02:06 -08003895 } else {
Paul Fulghum705b6c72006-01-08 01:02:06 -08003896 slgt_irq_off(info, IRQ_TXDATA);
3897 slgt_irq_on(info, IRQ_TXIDLE);
3898 /* clear tx idle status bit */
3899 wr_reg16(info, SSR, IRQ_TXIDLE);
Paul Fulghum705b6c72006-01-08 01:02:06 -08003900 }
Paul Fulghumbb029c62007-07-31 00:37:35 -07003901 tdma_start(info);
Joe Perches0fab6de2008-04-28 02:14:02 -07003902 info->tx_active = true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003903 }
3904}
3905
Paul Fulghumbb029c62007-07-31 00:37:35 -07003906/*
3907 * start transmit DMA if inactive and there are unsent buffers
3908 */
3909static void tdma_start(struct slgt_info *info)
3910{
3911 unsigned int i;
3912
3913 if (rd_reg32(info, TDCSR) & BIT0)
3914 return;
3915
3916 /* transmit DMA inactive, check for unsent buffers */
3917 i = info->tbuf_start;
3918 while (!desc_count(info->tbufs[i])) {
3919 if (++i == info->tbuf_count)
3920 i = 0;
3921 if (i == info->tbuf_current)
3922 return;
3923 }
3924 info->tbuf_start = i;
3925
3926 /* there are unsent buffers, start transmit DMA */
3927
3928 /* reset needed if previous error condition */
3929 tdma_reset(info);
3930
3931 /* set 1st descriptor address */
3932 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
Paul Fulghum8a38c282008-07-22 11:21:28 +01003933 wr_reg32(info, TDCSR, BIT2 + BIT0); /* IRQ + DMA enable */
Paul Fulghumbb029c62007-07-31 00:37:35 -07003934}
3935
Paul Fulghum705b6c72006-01-08 01:02:06 -08003936static void tx_stop(struct slgt_info *info)
3937{
3938 unsigned short val;
3939
3940 del_timer(&info->tx_timer);
3941
3942 tdma_reset(info);
3943
3944 /* reset and disable transmitter */
3945 val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
3946 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
Paul Fulghum705b6c72006-01-08 01:02:06 -08003947
3948 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
3949
3950 /* clear tx idle and underrun status bit */
3951 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
3952
3953 reset_tbufs(info);
3954
Joe Perches0fab6de2008-04-28 02:14:02 -07003955 info->tx_enabled = false;
3956 info->tx_active = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08003957}
3958
3959static void reset_port(struct slgt_info *info)
3960{
3961 if (!info->reg_addr)
3962 return;
3963
3964 tx_stop(info);
3965 rx_stop(info);
3966
3967 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
3968 set_signals(info);
3969
3970 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
3971}
3972
3973static void reset_adapter(struct slgt_info *info)
3974{
3975 int i;
3976 for (i=0; i < info->port_count; ++i) {
3977 if (info->port_array[i])
3978 reset_port(info->port_array[i]);
3979 }
3980}
3981
3982static void async_mode(struct slgt_info *info)
3983{
3984 unsigned short val;
3985
3986 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
3987 tx_stop(info);
3988 rx_stop(info);
3989
3990 /* TCR (tx control)
3991 *
3992 * 15..13 mode, 010=async
3993 * 12..10 encoding, 000=NRZ
3994 * 09 parity enable
3995 * 08 1=odd parity, 0=even parity
3996 * 07 1=RTS driver control
3997 * 06 1=break enable
3998 * 05..04 character length
3999 * 00=5 bits
4000 * 01=6 bits
4001 * 10=7 bits
4002 * 11=8 bits
4003 * 03 0=1 stop bit, 1=2 stop bits
4004 * 02 reset
4005 * 01 enable
4006 * 00 auto-CTS enable
4007 */
4008 val = 0x4000;
4009
4010 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4011 val |= BIT7;
4012
4013 if (info->params.parity != ASYNC_PARITY_NONE) {
4014 val |= BIT9;
4015 if (info->params.parity == ASYNC_PARITY_ODD)
4016 val |= BIT8;
4017 }
4018
4019 switch (info->params.data_bits)
4020 {
4021 case 6: val |= BIT4; break;
4022 case 7: val |= BIT5; break;
4023 case 8: val |= BIT5 + BIT4; break;
4024 }
4025
4026 if (info->params.stop_bits != 1)
4027 val |= BIT3;
4028
4029 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4030 val |= BIT0;
4031
4032 wr_reg16(info, TCR, val);
4033
4034 /* RCR (rx control)
4035 *
4036 * 15..13 mode, 010=async
4037 * 12..10 encoding, 000=NRZ
4038 * 09 parity enable
4039 * 08 1=odd parity, 0=even parity
4040 * 07..06 reserved, must be 0
4041 * 05..04 character length
4042 * 00=5 bits
4043 * 01=6 bits
4044 * 10=7 bits
4045 * 11=8 bits
4046 * 03 reserved, must be zero
4047 * 02 reset
4048 * 01 enable
4049 * 00 auto-DCD enable
4050 */
4051 val = 0x4000;
4052
4053 if (info->params.parity != ASYNC_PARITY_NONE) {
4054 val |= BIT9;
4055 if (info->params.parity == ASYNC_PARITY_ODD)
4056 val |= BIT8;
4057 }
4058
4059 switch (info->params.data_bits)
4060 {
4061 case 6: val |= BIT4; break;
4062 case 7: val |= BIT5; break;
4063 case 8: val |= BIT5 + BIT4; break;
4064 }
4065
4066 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4067 val |= BIT0;
4068
4069 wr_reg16(info, RCR, val);
4070
4071 /* CCR (clock control)
4072 *
4073 * 07..05 011 = tx clock source is BRG/16
4074 * 04..02 010 = rx clock source is BRG
4075 * 01 0 = auxclk disabled
4076 * 00 1 = BRG enabled
4077 *
4078 * 0110 1001
4079 */
4080 wr_reg8(info, CCR, 0x69);
4081
4082 msc_set_vcr(info);
4083
Paul Fulghum705b6c72006-01-08 01:02:06 -08004084 /* SCR (serial control)
4085 *
4086 * 15 1=tx req on FIFO half empty
4087 * 14 1=rx req on FIFO half full
4088 * 13 tx data IRQ enable
4089 * 12 tx idle IRQ enable
4090 * 11 rx break on IRQ enable
4091 * 10 rx data IRQ enable
4092 * 09 rx break off IRQ enable
4093 * 08 overrun IRQ enable
4094 * 07 DSR IRQ enable
4095 * 06 CTS IRQ enable
4096 * 05 DCD IRQ enable
4097 * 04 RI IRQ enable
Paul Fulghum1f807692009-04-02 16:58:30 -07004098 * 03 0=16x sampling, 1=8x sampling
Paul Fulghum705b6c72006-01-08 01:02:06 -08004099 * 02 1=txd->rxd internal loopback enable
4100 * 01 reserved, must be zero
4101 * 00 1=master IRQ enable
4102 */
4103 val = BIT15 + BIT14 + BIT0;
Paul Fulghum1f807692009-04-02 16:58:30 -07004104 /* JCR[8] : 1 = x8 async mode feature available */
4105 if ((rd_reg32(info, JCR) & BIT8) && info->params.data_rate &&
4106 ((info->base_clock < (info->params.data_rate * 16)) ||
4107 (info->base_clock % (info->params.data_rate * 16)))) {
4108 /* use 8x sampling */
4109 val |= BIT3;
4110 set_rate(info, info->params.data_rate * 8);
4111 } else {
4112 /* use 16x sampling */
4113 set_rate(info, info->params.data_rate * 16);
4114 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08004115 wr_reg16(info, SCR, val);
4116
4117 slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
4118
Paul Fulghum705b6c72006-01-08 01:02:06 -08004119 if (info->params.loopback)
4120 enable_loopback(info);
4121}
4122
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004123static void sync_mode(struct slgt_info *info)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004124{
4125 unsigned short val;
4126
4127 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4128 tx_stop(info);
4129 rx_stop(info);
4130
4131 /* TCR (tx control)
4132 *
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004133 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
Paul Fulghum705b6c72006-01-08 01:02:06 -08004134 * 12..10 encoding
4135 * 09 CRC enable
4136 * 08 CRC32
4137 * 07 1=RTS driver control
4138 * 06 preamble enable
4139 * 05..04 preamble length
4140 * 03 share open/close flag
4141 * 02 reset
4142 * 01 enable
4143 * 00 auto-CTS enable
4144 */
Paul Fulghum993456c2008-07-22 11:22:04 +01004145 val = BIT2;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004146
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004147 switch(info->params.mode) {
4148 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4149 case MGSL_MODE_BISYNC: val |= BIT15; break;
4150 case MGSL_MODE_RAW: val |= BIT13; break;
4151 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08004152 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4153 val |= BIT7;
4154
4155 switch(info->params.encoding)
4156 {
4157 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4158 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4159 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4160 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4161 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4162 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4163 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4164 }
4165
Paul Fulghum04b374d2006-06-25 05:49:21 -07004166 switch (info->params.crc_type & HDLC_CRC_MASK)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004167 {
4168 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4169 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4170 }
4171
4172 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
4173 val |= BIT6;
4174
4175 switch (info->params.preamble_length)
4176 {
4177 case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
4178 case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
4179 case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
4180 }
4181
4182 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4183 val |= BIT0;
4184
4185 wr_reg16(info, TCR, val);
4186
4187 /* TPR (transmit preamble) */
4188
4189 switch (info->params.preamble)
4190 {
4191 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
4192 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
4193 case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
4194 case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
4195 case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
4196 default: val = 0x7e; break;
4197 }
4198 wr_reg8(info, TPR, (unsigned char)val);
4199
4200 /* RCR (rx control)
4201 *
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004202 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
Paul Fulghum705b6c72006-01-08 01:02:06 -08004203 * 12..10 encoding
4204 * 09 CRC enable
4205 * 08 CRC32
4206 * 07..03 reserved, must be 0
4207 * 02 reset
4208 * 01 enable
4209 * 00 auto-DCD enable
4210 */
4211 val = 0;
4212
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004213 switch(info->params.mode) {
4214 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4215 case MGSL_MODE_BISYNC: val |= BIT15; break;
4216 case MGSL_MODE_RAW: val |= BIT13; break;
4217 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08004218
4219 switch(info->params.encoding)
4220 {
4221 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4222 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4223 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4224 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4225 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4226 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4227 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4228 }
4229
Paul Fulghum04b374d2006-06-25 05:49:21 -07004230 switch (info->params.crc_type & HDLC_CRC_MASK)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004231 {
4232 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4233 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4234 }
4235
4236 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4237 val |= BIT0;
4238
4239 wr_reg16(info, RCR, val);
4240
4241 /* CCR (clock control)
4242 *
4243 * 07..05 tx clock source
4244 * 04..02 rx clock source
4245 * 01 auxclk enable
4246 * 00 BRG enable
4247 */
4248 val = 0;
4249
4250 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4251 {
4252 // when RxC source is DPLL, BRG generates 16X DPLL
4253 // reference clock, so take TxC from BRG/16 to get
4254 // transmit clock at actual data rate
4255 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4256 val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
4257 else
4258 val |= BIT6; /* 010, txclk = BRG */
4259 }
4260 else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4261 val |= BIT7; /* 100, txclk = DPLL Input */
4262 else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4263 val |= BIT5; /* 001, txclk = RXC Input */
4264
4265 if (info->params.flags & HDLC_FLAG_RXC_BRG)
4266 val |= BIT3; /* 010, rxclk = BRG */
4267 else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4268 val |= BIT4; /* 100, rxclk = DPLL */
4269 else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4270 val |= BIT2; /* 001, rxclk = TXC Input */
4271
4272 if (info->params.clock_speed)
4273 val |= BIT1 + BIT0;
4274
4275 wr_reg8(info, CCR, (unsigned char)val);
4276
4277 if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
4278 {
4279 // program DPLL mode
4280 switch(info->params.encoding)
4281 {
4282 case HDLC_ENCODING_BIPHASE_MARK:
4283 case HDLC_ENCODING_BIPHASE_SPACE:
4284 val = BIT7; break;
4285 case HDLC_ENCODING_BIPHASE_LEVEL:
4286 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
4287 val = BIT7 + BIT6; break;
4288 default: val = BIT6; // NRZ encodings
4289 }
4290 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
4291
4292 // DPLL requires a 16X reference clock from BRG
4293 set_rate(info, info->params.clock_speed * 16);
4294 }
4295 else
4296 set_rate(info, info->params.clock_speed);
4297
4298 tx_set_idle(info);
4299
4300 msc_set_vcr(info);
4301
4302 /* SCR (serial control)
4303 *
4304 * 15 1=tx req on FIFO half empty
4305 * 14 1=rx req on FIFO half full
4306 * 13 tx data IRQ enable
4307 * 12 tx idle IRQ enable
4308 * 11 underrun IRQ enable
4309 * 10 rx data IRQ enable
4310 * 09 rx idle IRQ enable
4311 * 08 overrun IRQ enable
4312 * 07 DSR IRQ enable
4313 * 06 CTS IRQ enable
4314 * 05 DCD IRQ enable
4315 * 04 RI IRQ enable
4316 * 03 reserved, must be zero
4317 * 02 1=txd->rxd internal loopback enable
4318 * 01 reserved, must be zero
4319 * 00 1=master IRQ enable
4320 */
4321 wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
4322
4323 if (info->params.loopback)
4324 enable_loopback(info);
4325}
4326
4327/*
4328 * set transmit idle mode
4329 */
4330static void tx_set_idle(struct slgt_info *info)
4331{
Paul Fulghum643f3312006-06-25 05:49:20 -07004332 unsigned char val;
4333 unsigned short tcr;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004334
Paul Fulghum643f3312006-06-25 05:49:20 -07004335 /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
4336 * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
4337 */
4338 tcr = rd_reg16(info, TCR);
4339 if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
4340 /* disable preamble, set idle size to 16 bits */
4341 tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
4342 /* MSB of 16 bit idle specified in tx preamble register (TPR) */
4343 wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
4344 } else if (!(tcr & BIT6)) {
4345 /* preamble is disabled, set idle size to 8 bits */
4346 tcr &= ~(BIT5 + BIT4);
4347 }
4348 wr_reg16(info, TCR, tcr);
4349
4350 if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
4351 /* LSB of custom tx idle specified in tx idle register */
4352 val = (unsigned char)(info->idle_mode & 0xff);
4353 } else {
4354 /* standard 8 bit idle patterns */
4355 switch(info->idle_mode)
4356 {
4357 case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
4358 case HDLC_TXIDLE_ALT_ZEROS_ONES:
4359 case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
4360 case HDLC_TXIDLE_ZEROS:
4361 case HDLC_TXIDLE_SPACE: val = 0x00; break;
4362 default: val = 0xff;
4363 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08004364 }
4365
4366 wr_reg8(info, TIR, val);
4367}
4368
4369/*
4370 * get state of V24 status (input) signals
4371 */
4372static void get_signals(struct slgt_info *info)
4373{
4374 unsigned short status = rd_reg16(info, SSR);
4375
4376 /* clear all serial signals except DTR and RTS */
4377 info->signals &= SerialSignal_DTR + SerialSignal_RTS;
4378
4379 if (status & BIT3)
4380 info->signals |= SerialSignal_DSR;
4381 if (status & BIT2)
4382 info->signals |= SerialSignal_CTS;
4383 if (status & BIT1)
4384 info->signals |= SerialSignal_DCD;
4385 if (status & BIT0)
4386 info->signals |= SerialSignal_RI;
4387}
4388
4389/*
4390 * set V.24 Control Register based on current configuration
4391 */
4392static void msc_set_vcr(struct slgt_info *info)
4393{
4394 unsigned char val = 0;
4395
4396 /* VCR (V.24 control)
4397 *
4398 * 07..04 serial IF select
4399 * 03 DTR
4400 * 02 RTS
4401 * 01 LL
4402 * 00 RL
4403 */
4404
4405 switch(info->if_mode & MGSL_INTERFACE_MASK)
4406 {
4407 case MGSL_INTERFACE_RS232:
4408 val |= BIT5; /* 0010 */
4409 break;
4410 case MGSL_INTERFACE_V35:
4411 val |= BIT7 + BIT6 + BIT5; /* 1110 */
4412 break;
4413 case MGSL_INTERFACE_RS422:
4414 val |= BIT6; /* 0100 */
4415 break;
4416 }
4417
Paul Fulghume5590712008-07-22 11:21:39 +01004418 if (info->if_mode & MGSL_INTERFACE_MSB_FIRST)
4419 val |= BIT4;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004420 if (info->signals & SerialSignal_DTR)
4421 val |= BIT3;
4422 if (info->signals & SerialSignal_RTS)
4423 val |= BIT2;
4424 if (info->if_mode & MGSL_INTERFACE_LL)
4425 val |= BIT1;
4426 if (info->if_mode & MGSL_INTERFACE_RL)
4427 val |= BIT0;
4428 wr_reg8(info, VCR, val);
4429}
4430
4431/*
4432 * set state of V24 control (output) signals
4433 */
4434static void set_signals(struct slgt_info *info)
4435{
4436 unsigned char val = rd_reg8(info, VCR);
4437 if (info->signals & SerialSignal_DTR)
4438 val |= BIT3;
4439 else
4440 val &= ~BIT3;
4441 if (info->signals & SerialSignal_RTS)
4442 val |= BIT2;
4443 else
4444 val &= ~BIT2;
4445 wr_reg8(info, VCR, val);
4446}
4447
4448/*
4449 * free range of receive DMA buffers (i to last)
4450 */
4451static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
4452{
4453 int done = 0;
4454
4455 while(!done) {
4456 /* reset current buffer for reuse */
4457 info->rbufs[i].status = 0;
Paul Fulghum814dae02008-07-22 11:22:14 +01004458 set_desc_count(info->rbufs[i], info->rbuf_fill_level);
Paul Fulghum705b6c72006-01-08 01:02:06 -08004459 if (i == last)
4460 done = 1;
4461 if (++i == info->rbuf_count)
4462 i = 0;
4463 }
4464 info->rbuf_current = i;
4465}
4466
4467/*
4468 * mark all receive DMA buffers as free
4469 */
4470static void reset_rbufs(struct slgt_info *info)
4471{
4472 free_rbufs(info, 0, info->rbuf_count - 1);
4473}
4474
4475/*
4476 * pass receive HDLC frame to upper layer
4477 *
Joe Perches0fab6de2008-04-28 02:14:02 -07004478 * return true if frame available, otherwise false
Paul Fulghum705b6c72006-01-08 01:02:06 -08004479 */
Joe Perches0fab6de2008-04-28 02:14:02 -07004480static bool rx_get_frame(struct slgt_info *info)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004481{
4482 unsigned int start, end;
4483 unsigned short status;
4484 unsigned int framesize = 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004485 unsigned long flags;
Alan Cox8fb06c72008-07-16 21:56:46 +01004486 struct tty_struct *tty = info->port.tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004487 unsigned char addr_field = 0xff;
Paul Fulghum04b374d2006-06-25 05:49:21 -07004488 unsigned int crc_size = 0;
4489
4490 switch (info->params.crc_type & HDLC_CRC_MASK) {
4491 case HDLC_CRC_16_CCITT: crc_size = 2; break;
4492 case HDLC_CRC_32_CCITT: crc_size = 4; break;
4493 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08004494
4495check_again:
4496
4497 framesize = 0;
4498 addr_field = 0xff;
4499 start = end = info->rbuf_current;
4500
4501 for (;;) {
4502 if (!desc_complete(info->rbufs[end]))
4503 goto cleanup;
4504
4505 if (framesize == 0 && info->params.addr_filter != 0xff)
4506 addr_field = info->rbufs[end].buf[0];
4507
4508 framesize += desc_count(info->rbufs[end]);
4509
4510 if (desc_eof(info->rbufs[end]))
4511 break;
4512
4513 if (++end == info->rbuf_count)
4514 end = 0;
4515
4516 if (end == info->rbuf_current) {
4517 if (info->rx_enabled){
4518 spin_lock_irqsave(&info->lock,flags);
4519 rx_start(info);
4520 spin_unlock_irqrestore(&info->lock,flags);
4521 }
4522 goto cleanup;
4523 }
4524 }
4525
4526 /* status
4527 *
4528 * 15 buffer complete
4529 * 14..06 reserved
4530 * 05..04 residue
4531 * 02 eof (end of frame)
4532 * 01 CRC error
4533 * 00 abort
4534 */
4535 status = desc_status(info->rbufs[end]);
4536
4537 /* ignore CRC bit if not using CRC (bit is undefined) */
Paul Fulghum04b374d2006-06-25 05:49:21 -07004538 if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004539 status &= ~BIT1;
4540
4541 if (framesize == 0 ||
4542 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4543 free_rbufs(info, start, end);
4544 goto check_again;
4545 }
4546
Paul Fulghum04b374d2006-06-25 05:49:21 -07004547 if (framesize < (2 + crc_size) || status & BIT0) {
4548 info->icount.rxshort++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004549 framesize = 0;
Paul Fulghum04b374d2006-06-25 05:49:21 -07004550 } else if (status & BIT1) {
4551 info->icount.rxcrc++;
4552 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
4553 framesize = 0;
4554 }
Paul Fulghum705b6c72006-01-08 01:02:06 -08004555
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08004556#if SYNCLINK_GENERIC_HDLC
Paul Fulghum04b374d2006-06-25 05:49:21 -07004557 if (framesize == 0) {
Krzysztof Halasa198191c2008-06-30 23:26:53 +02004558 info->netdev->stats.rx_errors++;
4559 info->netdev->stats.rx_frame_errors++;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004560 }
Paul Fulghum04b374d2006-06-25 05:49:21 -07004561#endif
Paul Fulghum705b6c72006-01-08 01:02:06 -08004562
4563 DBGBH(("%s rx frame status=%04X size=%d\n",
4564 info->device_name, status, framesize));
Paul Fulghum814dae02008-07-22 11:22:14 +01004565 DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, info->rbuf_fill_level), "rx");
Paul Fulghum705b6c72006-01-08 01:02:06 -08004566
4567 if (framesize) {
Paul Fulghum04b374d2006-06-25 05:49:21 -07004568 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
4569 framesize -= crc_size;
4570 crc_size = 0;
4571 }
4572
4573 if (framesize > info->max_frame_size + crc_size)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004574 info->icount.rxlong++;
4575 else {
4576 /* copy dma buffer(s) to contiguous temp buffer */
4577 int copy_count = framesize;
4578 int i = start;
4579 unsigned char *p = info->tmp_rbuf;
4580 info->tmp_rbuf_count = framesize;
4581
4582 info->icount.rxok++;
4583
4584 while(copy_count) {
Paul Fulghum814dae02008-07-22 11:22:14 +01004585 int partial_count = min_t(int, copy_count, info->rbuf_fill_level);
Paul Fulghum705b6c72006-01-08 01:02:06 -08004586 memcpy(p, info->rbufs[i].buf, partial_count);
4587 p += partial_count;
4588 copy_count -= partial_count;
4589 if (++i == info->rbuf_count)
4590 i = 0;
4591 }
4592
Paul Fulghum04b374d2006-06-25 05:49:21 -07004593 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4594 *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
4595 framesize++;
4596 }
4597
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08004598#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08004599 if (info->netcount)
4600 hdlcdev_rx(info,info->tmp_rbuf, framesize);
4601 else
4602#endif
4603 ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
4604 }
4605 }
4606 free_rbufs(info, start, end);
Joe Perches0fab6de2008-04-28 02:14:02 -07004607 return true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004608
4609cleanup:
Joe Perches0fab6de2008-04-28 02:14:02 -07004610 return false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004611}
4612
4613/*
4614 * pass receive buffer (RAW synchronous mode) to tty layer
Joe Perches0fab6de2008-04-28 02:14:02 -07004615 * return true if buffer available, otherwise false
Paul Fulghum705b6c72006-01-08 01:02:06 -08004616 */
Joe Perches0fab6de2008-04-28 02:14:02 -07004617static bool rx_get_buf(struct slgt_info *info)
Paul Fulghum705b6c72006-01-08 01:02:06 -08004618{
4619 unsigned int i = info->rbuf_current;
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004620 unsigned int count;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004621
4622 if (!desc_complete(info->rbufs[i]))
Joe Perches0fab6de2008-04-28 02:14:02 -07004623 return false;
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004624 count = desc_count(info->rbufs[i]);
4625 switch(info->params.mode) {
4626 case MGSL_MODE_MONOSYNC:
4627 case MGSL_MODE_BISYNC:
4628 /* ignore residue in byte synchronous modes */
4629 if (desc_residue(info->rbufs[i]))
4630 count--;
4631 break;
4632 }
4633 DBGDATA(info, info->rbufs[i].buf, count, "rx");
4634 DBGINFO(("rx_get_buf size=%d\n", count));
4635 if (count)
Alan Cox8fb06c72008-07-16 21:56:46 +01004636 ldisc_receive_buf(info->port.tty, info->rbufs[i].buf,
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004637 info->flag_buf, count);
Paul Fulghum705b6c72006-01-08 01:02:06 -08004638 free_rbufs(info, i, i);
Joe Perches0fab6de2008-04-28 02:14:02 -07004639 return true;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004640}
4641
4642static void reset_tbufs(struct slgt_info *info)
4643{
4644 unsigned int i;
4645 info->tbuf_current = 0;
4646 for (i=0 ; i < info->tbuf_count ; i++) {
4647 info->tbufs[i].status = 0;
4648 info->tbufs[i].count = 0;
4649 }
4650}
4651
4652/*
4653 * return number of free transmit DMA buffers
4654 */
4655static unsigned int free_tbuf_count(struct slgt_info *info)
4656{
4657 unsigned int count = 0;
4658 unsigned int i = info->tbuf_current;
4659
4660 do
4661 {
4662 if (desc_count(info->tbufs[i]))
4663 break; /* buffer in use */
4664 ++count;
4665 if (++i == info->tbuf_count)
4666 i=0;
4667 } while (i != info->tbuf_current);
4668
Paul Fulghumbb029c62007-07-31 00:37:35 -07004669 /* if tx DMA active, last zero count buffer is in use */
4670 if (count && (rd_reg32(info, TDCSR) & BIT0))
Paul Fulghum705b6c72006-01-08 01:02:06 -08004671 --count;
4672
4673 return count;
4674}
4675
4676/*
Paul Fulghum403214d2008-07-22 11:21:55 +01004677 * return number of bytes in unsent transmit DMA buffers
4678 * and the serial controller tx FIFO
4679 */
4680static unsigned int tbuf_bytes(struct slgt_info *info)
4681{
4682 unsigned int total_count = 0;
4683 unsigned int i = info->tbuf_current;
4684 unsigned int reg_value;
4685 unsigned int count;
4686 unsigned int active_buf_count = 0;
4687
4688 /*
4689 * Add descriptor counts for all tx DMA buffers.
4690 * If count is zero (cleared by DMA controller after read),
4691 * the buffer is complete or is actively being read from.
4692 *
4693 * Record buf_count of last buffer with zero count starting
4694 * from current ring position. buf_count is mirror
4695 * copy of count and is not cleared by serial controller.
4696 * If DMA controller is active, that buffer is actively
4697 * being read so add to total.
4698 */
4699 do {
4700 count = desc_count(info->tbufs[i]);
4701 if (count)
4702 total_count += count;
4703 else if (!total_count)
4704 active_buf_count = info->tbufs[i].buf_count;
4705 if (++i == info->tbuf_count)
4706 i = 0;
4707 } while (i != info->tbuf_current);
4708
4709 /* read tx DMA status register */
4710 reg_value = rd_reg32(info, TDCSR);
4711
4712 /* if tx DMA active, last zero count buffer is in use */
4713 if (reg_value & BIT0)
4714 total_count += active_buf_count;
4715
4716 /* add tx FIFO count = reg_value[15..8] */
4717 total_count += (reg_value >> 8) & 0xff;
4718
4719 /* if transmitter active add one byte for shift register */
4720 if (info->tx_active)
4721 total_count++;
4722
4723 return total_count;
4724}
4725
4726/*
Paul Fulghum705b6c72006-01-08 01:02:06 -08004727 * load transmit DMA buffer(s) with data
4728 */
4729static void tx_load(struct slgt_info *info, const char *buf, unsigned int size)
4730{
4731 unsigned short count;
4732 unsigned int i;
4733 struct slgt_desc *d;
4734
4735 if (size == 0)
4736 return;
4737
4738 DBGDATA(info, buf, size, "tx");
4739
4740 info->tbuf_start = i = info->tbuf_current;
4741
4742 while (size) {
4743 d = &info->tbufs[i];
4744 if (++i == info->tbuf_count)
4745 i = 0;
4746
4747 count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
4748 memcpy(d->buf, buf, count);
4749
4750 size -= count;
4751 buf += count;
4752
Paul Fulghumcb10dc92006-09-30 23:27:45 -07004753 /*
4754 * set EOF bit for last buffer of HDLC frame or
4755 * for every buffer in raw mode
4756 */
4757 if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
4758 info->params.mode == MGSL_MODE_RAW)
4759 set_desc_eof(*d, 1);
Paul Fulghum705b6c72006-01-08 01:02:06 -08004760 else
4761 set_desc_eof(*d, 0);
4762
4763 set_desc_count(*d, count);
Paul Fulghum403214d2008-07-22 11:21:55 +01004764 d->buf_count = count;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004765 }
4766
4767 info->tbuf_current = i;
4768}
4769
4770static int register_test(struct slgt_info *info)
4771{
4772 static unsigned short patterns[] =
4773 {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
4774 static unsigned int count = sizeof(patterns)/sizeof(patterns[0]);
4775 unsigned int i;
4776 int rc = 0;
4777
4778 for (i=0 ; i < count ; i++) {
4779 wr_reg16(info, TIR, patterns[i]);
4780 wr_reg16(info, BDR, patterns[(i+1)%count]);
4781 if ((rd_reg16(info, TIR) != patterns[i]) ||
4782 (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
4783 rc = -ENODEV;
4784 break;
4785 }
4786 }
Paul Fulghum0080b7a2006-03-28 01:56:15 -08004787 info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004788 info->init_error = rc ? 0 : DiagStatus_AddressFailure;
4789 return rc;
4790}
4791
4792static int irq_test(struct slgt_info *info)
4793{
4794 unsigned long timeout;
4795 unsigned long flags;
Alan Cox8fb06c72008-07-16 21:56:46 +01004796 struct tty_struct *oldtty = info->port.tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004797 u32 speed = info->params.data_rate;
4798
4799 info->params.data_rate = 921600;
Alan Cox8fb06c72008-07-16 21:56:46 +01004800 info->port.tty = NULL;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004801
4802 spin_lock_irqsave(&info->lock, flags);
4803 async_mode(info);
4804 slgt_irq_on(info, IRQ_TXIDLE);
4805
4806 /* enable transmitter */
4807 wr_reg16(info, TCR,
4808 (unsigned short)(rd_reg16(info, TCR) | BIT1));
4809
4810 /* write one byte and wait for tx idle */
4811 wr_reg16(info, TDR, 0);
4812
4813 /* assume failure */
4814 info->init_error = DiagStatus_IrqFailure;
Joe Perches0fab6de2008-04-28 02:14:02 -07004815 info->irq_occurred = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004816
4817 spin_unlock_irqrestore(&info->lock, flags);
4818
4819 timeout=100;
4820 while(timeout-- && !info->irq_occurred)
4821 msleep_interruptible(10);
4822
4823 spin_lock_irqsave(&info->lock,flags);
4824 reset_port(info);
4825 spin_unlock_irqrestore(&info->lock,flags);
4826
4827 info->params.data_rate = speed;
Alan Cox8fb06c72008-07-16 21:56:46 +01004828 info->port.tty = oldtty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004829
4830 info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
4831 return info->irq_occurred ? 0 : -ENODEV;
4832}
4833
4834static int loopback_test_rx(struct slgt_info *info)
4835{
4836 unsigned char *src, *dest;
4837 int count;
4838
4839 if (desc_complete(info->rbufs[0])) {
4840 count = desc_count(info->rbufs[0]);
4841 src = info->rbufs[0].buf;
4842 dest = info->tmp_rbuf;
4843
4844 for( ; count ; count-=2, src+=2) {
4845 /* src=data byte (src+1)=status byte */
4846 if (!(*(src+1) & (BIT9 + BIT8))) {
4847 *dest = *src;
4848 dest++;
4849 info->tmp_rbuf_count++;
4850 }
4851 }
4852 DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
4853 return 1;
4854 }
4855 return 0;
4856}
4857
4858static int loopback_test(struct slgt_info *info)
4859{
4860#define TESTFRAMESIZE 20
4861
4862 unsigned long timeout;
4863 u16 count = TESTFRAMESIZE;
4864 unsigned char buf[TESTFRAMESIZE];
4865 int rc = -ENODEV;
4866 unsigned long flags;
4867
Alan Cox8fb06c72008-07-16 21:56:46 +01004868 struct tty_struct *oldtty = info->port.tty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004869 MGSL_PARAMS params;
4870
4871 memcpy(&params, &info->params, sizeof(params));
4872
4873 info->params.mode = MGSL_MODE_ASYNC;
4874 info->params.data_rate = 921600;
4875 info->params.loopback = 1;
Alan Cox8fb06c72008-07-16 21:56:46 +01004876 info->port.tty = NULL;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004877
4878 /* build and send transmit frame */
4879 for (count = 0; count < TESTFRAMESIZE; ++count)
4880 buf[count] = (unsigned char)count;
4881
4882 info->tmp_rbuf_count = 0;
4883 memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
4884
4885 /* program hardware for HDLC and enabled receiver */
4886 spin_lock_irqsave(&info->lock,flags);
4887 async_mode(info);
4888 rx_start(info);
4889 info->tx_count = count;
4890 tx_load(info, buf, count);
4891 tx_start(info);
4892 spin_unlock_irqrestore(&info->lock, flags);
4893
4894 /* wait for receive complete */
4895 for (timeout = 100; timeout; --timeout) {
4896 msleep_interruptible(10);
4897 if (loopback_test_rx(info)) {
4898 rc = 0;
4899 break;
4900 }
4901 }
4902
4903 /* verify received frame length and contents */
4904 if (!rc && (info->tmp_rbuf_count != count ||
4905 memcmp(buf, info->tmp_rbuf, count))) {
4906 rc = -ENODEV;
4907 }
4908
4909 spin_lock_irqsave(&info->lock,flags);
4910 reset_adapter(info);
4911 spin_unlock_irqrestore(&info->lock,flags);
4912
4913 memcpy(&info->params, &params, sizeof(info->params));
Alan Cox8fb06c72008-07-16 21:56:46 +01004914 info->port.tty = oldtty;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004915
4916 info->init_error = rc ? DiagStatus_DmaFailure : 0;
4917 return rc;
4918}
4919
4920static int adapter_test(struct slgt_info *info)
4921{
4922 DBGINFO(("testing %s\n", info->device_name));
Paul Fulghum294dad02006-06-25 05:49:21 -07004923 if (register_test(info) < 0) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08004924 printk("register test failure %s addr=%08X\n",
4925 info->device_name, info->phys_reg_addr);
Paul Fulghum294dad02006-06-25 05:49:21 -07004926 } else if (irq_test(info) < 0) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08004927 printk("IRQ test failure %s IRQ=%d\n",
4928 info->device_name, info->irq_level);
Paul Fulghum294dad02006-06-25 05:49:21 -07004929 } else if (loopback_test(info) < 0) {
Paul Fulghum705b6c72006-01-08 01:02:06 -08004930 printk("loopback test failure %s\n", info->device_name);
4931 }
4932 return info->init_error;
4933}
4934
4935/*
4936 * transmit timeout handler
4937 */
4938static void tx_timeout(unsigned long context)
4939{
4940 struct slgt_info *info = (struct slgt_info*)context;
4941 unsigned long flags;
4942
4943 DBGINFO(("%s tx_timeout\n", info->device_name));
4944 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
4945 info->icount.txtimeout++;
4946 }
4947 spin_lock_irqsave(&info->lock,flags);
Joe Perches0fab6de2008-04-28 02:14:02 -07004948 info->tx_active = false;
Paul Fulghum705b6c72006-01-08 01:02:06 -08004949 info->tx_count = 0;
4950 spin_unlock_irqrestore(&info->lock,flags);
4951
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08004952#if SYNCLINK_GENERIC_HDLC
Paul Fulghum705b6c72006-01-08 01:02:06 -08004953 if (info->netcount)
4954 hdlcdev_tx_done(info);
4955 else
4956#endif
4957 bh_transmit(info);
4958}
4959
4960/*
4961 * receive buffer polling timer
4962 */
4963static void rx_timeout(unsigned long context)
4964{
4965 struct slgt_info *info = (struct slgt_info*)context;
4966 unsigned long flags;
4967
4968 DBGINFO(("%s rx_timeout\n", info->device_name));
4969 spin_lock_irqsave(&info->lock, flags);
4970 info->pending_bh |= BH_RECEIVE;
4971 spin_unlock_irqrestore(&info->lock, flags);
David Howellsc4028952006-11-22 14:57:56 +00004972 bh_handler(&info->task);
Paul Fulghum705b6c72006-01-08 01:02:06 -08004973}
4974