blob: bc4bc1ff775ebb7bf41578415cb2546d9bc15933 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -08002 * $Id: synclink.c,v 4.38 2005/11/07 16:30:34 paulkf Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Device driver for Microgate SyncLink ISA and PCI
5 * high speed multiprotocol serial adapters.
6 *
7 * written by Paul Fulghum for Microgate Corporation
8 * paulkf@microgate.com
9 *
10 * Microgate and SyncLink are trademarks of Microgate Corporation
11 *
12 * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
13 *
14 * Original release 01/11/99
15 *
16 * This code is released under the GNU General Public License (GPL)
17 *
18 * This driver is primarily intended for use in synchronous
19 * HDLC mode. Asynchronous mode is also provided.
20 *
21 * When operating in synchronous mode, each call to mgsl_write()
22 * contains exactly one complete HDLC frame. Calling mgsl_put_char
23 * will start assembling an HDLC frame that will not be sent until
24 * mgsl_flush_chars or mgsl_write is called.
25 *
26 * Synchronous receive data is reported as complete frames. To accomplish
27 * this, the TTY flip buffer is bypassed (too small to hold largest
28 * frame and may fragment frames) and the line discipline
29 * receive entry point is called directly.
30 *
31 * This driver has been tested with a slightly modified ppp.c driver
32 * for synchronous PPP.
33 *
34 * 2000/02/16
35 * Added interface for syncppp.c driver (an alternate synchronous PPP
36 * implementation that also supports Cisco HDLC). Each device instance
37 * registers as a tty device AND a network device (if dosyncppp option
38 * is set for the device). The functionality is determined by which
39 * device interface is opened.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
45 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53
54#if defined(__i386__)
55# define BREAKPOINT() asm(" int $3");
56#else
57# define BREAKPOINT() { }
58#endif
59
60#define MAX_ISA_DEVICES 10
61#define MAX_PCI_DEVICES 10
62#define MAX_TOTAL_DEVICES 20
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/module.h>
65#include <linux/errno.h>
66#include <linux/signal.h>
67#include <linux/sched.h>
68#include <linux/timer.h>
69#include <linux/interrupt.h>
70#include <linux/pci.h>
71#include <linux/tty.h>
72#include <linux/tty_flip.h>
73#include <linux/serial.h>
74#include <linux/major.h>
75#include <linux/string.h>
76#include <linux/fcntl.h>
77#include <linux/ptrace.h>
78#include <linux/ioport.h>
79#include <linux/mm.h>
Alexey Dobriyand3378292009-03-31 15:19:18 -070080#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/slab.h>
82#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/vmalloc.h>
85#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/ioctl.h>
Robert P. J. Day3dd12472008-02-06 01:37:17 -080087#include <linux/synclink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <asm/io.h>
90#include <asm/irq.h>
91#include <asm/dma.h>
92#include <linux/bitops.h>
93#include <asm/types.h>
94#include <linux/termios.h>
95#include <linux/workqueue.h>
96#include <linux/hdlc.h>
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -080097#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Paul Fulghumaf69c7f2006-12-06 20:40:24 -080099#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_MODULE))
100#define SYNCLINK_GENERIC_HDLC 1
101#else
102#define SYNCLINK_GENERIC_HDLC 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif
104
105#define GET_USER(error,value,addr) error = get_user(value,addr)
106#define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
107#define PUT_USER(error,value,addr) error = put_user(value,addr)
108#define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
109
110#include <asm/uaccess.h>
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define RCLRVALUE 0xffff
113
114static MGSL_PARAMS default_params = {
115 MGSL_MODE_HDLC, /* unsigned long mode */
116 0, /* unsigned char loopback; */
117 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
118 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
119 0, /* unsigned long clock_speed; */
120 0xff, /* unsigned char addr_filter; */
121 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
122 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
123 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
124 9600, /* unsigned long data_rate; */
125 8, /* unsigned char data_bits; */
126 1, /* unsigned char stop_bits; */
127 ASYNC_PARITY_NONE /* unsigned char parity; */
128};
129
130#define SHARED_MEM_ADDRESS_SIZE 0x40000
Paul Fulghum623a4392006-10-17 00:09:27 -0700131#define BUFFERLISTSIZE 4096
132#define DMABUFFERSIZE 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define MAXRXFRAMES 7
134
135typedef struct _DMABUFFERENTRY
136{
137 u32 phys_addr; /* 32-bit flat physical address of data buffer */
Paul Fulghum4a918bc2005-09-09 13:02:12 -0700138 volatile u16 count; /* buffer size/data count */
139 volatile u16 status; /* Control/status field */
140 volatile u16 rcc; /* character count field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 u16 reserved; /* padding required by 16C32 */
142 u32 link; /* 32-bit flat link to next buffer entry */
143 char *virt_addr; /* virtual address of data buffer */
144 u32 phys_entry; /* physical address of this buffer entry */
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -0800145 dma_addr_t dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146} DMABUFFERENTRY, *DMAPBUFFERENTRY;
147
148/* The queue of BH actions to be performed */
149
150#define BH_RECEIVE 1
151#define BH_TRANSMIT 2
152#define BH_STATUS 4
153
154#define IO_PIN_SHUTDOWN_LIMIT 100
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156struct _input_signal_events {
157 int ri_up;
158 int ri_down;
159 int dsr_up;
160 int dsr_down;
161 int dcd_up;
162 int dcd_down;
163 int cts_up;
164 int cts_down;
165};
166
167/* transmit holding buffer definitions*/
168#define MAX_TX_HOLDING_BUFFERS 5
169struct tx_holding_buffer {
170 int buffer_size;
171 unsigned char * buffer;
172};
173
174
175/*
176 * Device instance data structure
177 */
178
179struct mgsl_struct {
180 int magic;
Alan Cox8fb06c72008-07-16 21:56:46 +0100181 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int line;
183 int hw_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 struct mgsl_icount icount;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int timeout;
188 int x_char; /* xon/xoff character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 u16 read_status_mask;
190 u16 ignore_status_mask;
191 unsigned char *xmit_buf;
192 int xmit_head;
193 int xmit_tail;
194 int xmit_cnt;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 wait_queue_head_t status_event_wait_q;
197 wait_queue_head_t event_wait_q;
198 struct timer_list tx_timer; /* HDLC transmit timeout timer */
199 struct mgsl_struct *next_device; /* device list link */
200
201 spinlock_t irq_spinlock; /* spinlock for synchronizing with ISR */
202 struct work_struct task; /* task structure for scheduling bh */
203
204 u32 EventMask; /* event trigger mask */
205 u32 RecordedEvents; /* pending events */
206
207 u32 max_frame_size; /* as set by device config */
208
209 u32 pending_bh;
210
Joe Perches0fab6de2008-04-28 02:14:02 -0700211 bool bh_running; /* Protection from multiple */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int isr_overflow;
Joe Perches0fab6de2008-04-28 02:14:02 -0700213 bool bh_requested;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 int dcd_chkcount; /* check counts to prevent */
216 int cts_chkcount; /* too many IRQs if a signal */
217 int dsr_chkcount; /* is floating */
218 int ri_chkcount;
219
220 char *buffer_list; /* virtual address of Rx & Tx buffer lists */
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -0800221 u32 buffer_list_phys;
222 dma_addr_t buffer_list_dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 unsigned int rx_buffer_count; /* count of total allocated Rx buffers */
225 DMABUFFERENTRY *rx_buffer_list; /* list of receive buffer entries */
226 unsigned int current_rx_buffer;
227
228 int num_tx_dma_buffers; /* number of tx dma frames required */
229 int tx_dma_buffers_used;
230 unsigned int tx_buffer_count; /* count of total allocated Tx buffers */
231 DMABUFFERENTRY *tx_buffer_list; /* list of transmit buffer entries */
232 int start_tx_dma_buffer; /* tx dma buffer to start tx dma operation */
233 int current_tx_buffer; /* next tx dma buffer to be loaded */
234
235 unsigned char *intermediate_rxbuffer;
236
237 int num_tx_holding_buffers; /* number of tx holding buffer allocated */
238 int get_tx_holding_index; /* next tx holding buffer for adapter to load */
239 int put_tx_holding_index; /* next tx holding buffer to store user request */
240 int tx_holding_count; /* number of tx holding buffers waiting */
241 struct tx_holding_buffer tx_holding_buffers[MAX_TX_HOLDING_BUFFERS];
242
Joe Perches0fab6de2008-04-28 02:14:02 -0700243 bool rx_enabled;
244 bool rx_overflow;
245 bool rx_rcc_underrun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Joe Perches0fab6de2008-04-28 02:14:02 -0700247 bool tx_enabled;
248 bool tx_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 u32 idle_mode;
250
251 u16 cmr_value;
252 u16 tcsr_value;
253
254 char device_name[25]; /* device instance name */
255
256 unsigned int bus_type; /* expansion bus type (ISA,EISA,PCI) */
257 unsigned char bus; /* expansion bus number (zero based) */
258 unsigned char function; /* PCI device number */
259
260 unsigned int io_base; /* base I/O address of adapter */
261 unsigned int io_addr_size; /* size of the I/O address range */
Joe Perches0fab6de2008-04-28 02:14:02 -0700262 bool io_addr_requested; /* true if I/O address requested */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 unsigned int irq_level; /* interrupt level */
265 unsigned long irq_flags;
Joe Perches0fab6de2008-04-28 02:14:02 -0700266 bool irq_requested; /* true if IRQ requested */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 unsigned int dma_level; /* DMA channel */
Joe Perches0fab6de2008-04-28 02:14:02 -0700269 bool dma_requested; /* true if dma channel requested */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 u16 mbre_bit;
272 u16 loopback_bits;
273 u16 usc_idle_mode;
274
275 MGSL_PARAMS params; /* communications parameters */
276
277 unsigned char serial_signals; /* current serial signal states */
278
Joe Perches0fab6de2008-04-28 02:14:02 -0700279 bool irq_occurred; /* for diagnostics use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 unsigned int init_error; /* Initialization startup error (DIAGS) */
281 int fDiagnosticsmode; /* Driver in Diagnostic mode? (DIAGS) */
282
283 u32 last_mem_alloc;
284 unsigned char* memory_base; /* shared memory address (PCI only) */
285 u32 phys_memory_base;
Joe Perches0fab6de2008-04-28 02:14:02 -0700286 bool shared_mem_requested;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 unsigned char* lcr_base; /* local config registers (PCI only) */
289 u32 phys_lcr_base;
290 u32 lcr_offset;
Joe Perches0fab6de2008-04-28 02:14:02 -0700291 bool lcr_mem_requested;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 u32 misc_ctrl_value;
Paul Fulghuma6b68a62012-12-03 11:13:24 -0600294 char *flag_buf;
Joe Perches0fab6de2008-04-28 02:14:02 -0700295 bool drop_rts_on_tx_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Joe Perches0fab6de2008-04-28 02:14:02 -0700297 bool loopmode_insert_requested;
298 bool loopmode_send_done_requested;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 struct _input_signal_events input_signal_events;
301
302 /* generic HDLC device parts */
303 int netcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 spinlock_t netlock;
305
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800306#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct net_device *netdev;
308#endif
309};
310
311#define MGSL_MAGIC 0x5401
312
313/*
314 * The size of the serial xmit buffer is 1 page, or 4096 bytes
315 */
316#ifndef SERIAL_XMIT_SIZE
317#define SERIAL_XMIT_SIZE 4096
318#endif
319
320/*
321 * These macros define the offsets used in calculating the
322 * I/O address of the specified USC registers.
323 */
324
325
326#define DCPIN 2 /* Bit 1 of I/O address */
327#define SDPIN 4 /* Bit 2 of I/O address */
328
329#define DCAR 0 /* DMA command/address register */
330#define CCAR SDPIN /* channel command/address register */
331#define DATAREG DCPIN + SDPIN /* serial data register */
332#define MSBONLY 0x41
333#define LSBONLY 0x40
334
335/*
336 * These macros define the register address (ordinal number)
337 * used for writing address/value pairs to the USC.
338 */
339
340#define CMR 0x02 /* Channel mode Register */
341#define CCSR 0x04 /* Channel Command/status Register */
342#define CCR 0x06 /* Channel Control Register */
343#define PSR 0x08 /* Port status Register */
344#define PCR 0x0a /* Port Control Register */
345#define TMDR 0x0c /* Test mode Data Register */
346#define TMCR 0x0e /* Test mode Control Register */
347#define CMCR 0x10 /* Clock mode Control Register */
348#define HCR 0x12 /* Hardware Configuration Register */
349#define IVR 0x14 /* Interrupt Vector Register */
350#define IOCR 0x16 /* Input/Output Control Register */
351#define ICR 0x18 /* Interrupt Control Register */
352#define DCCR 0x1a /* Daisy Chain Control Register */
353#define MISR 0x1c /* Misc Interrupt status Register */
354#define SICR 0x1e /* status Interrupt Control Register */
355#define RDR 0x20 /* Receive Data Register */
356#define RMR 0x22 /* Receive mode Register */
357#define RCSR 0x24 /* Receive Command/status Register */
358#define RICR 0x26 /* Receive Interrupt Control Register */
359#define RSR 0x28 /* Receive Sync Register */
360#define RCLR 0x2a /* Receive count Limit Register */
361#define RCCR 0x2c /* Receive Character count Register */
362#define TC0R 0x2e /* Time Constant 0 Register */
363#define TDR 0x30 /* Transmit Data Register */
364#define TMR 0x32 /* Transmit mode Register */
365#define TCSR 0x34 /* Transmit Command/status Register */
366#define TICR 0x36 /* Transmit Interrupt Control Register */
367#define TSR 0x38 /* Transmit Sync Register */
368#define TCLR 0x3a /* Transmit count Limit Register */
369#define TCCR 0x3c /* Transmit Character count Register */
370#define TC1R 0x3e /* Time Constant 1 Register */
371
372
373/*
374 * MACRO DEFINITIONS FOR DMA REGISTERS
375 */
376
377#define DCR 0x06 /* DMA Control Register (shared) */
378#define DACR 0x08 /* DMA Array count Register (shared) */
379#define BDCR 0x12 /* Burst/Dwell Control Register (shared) */
380#define DIVR 0x14 /* DMA Interrupt Vector Register (shared) */
381#define DICR 0x18 /* DMA Interrupt Control Register (shared) */
382#define CDIR 0x1a /* Clear DMA Interrupt Register (shared) */
383#define SDIR 0x1c /* Set DMA Interrupt Register (shared) */
384
385#define TDMR 0x02 /* Transmit DMA mode Register */
386#define TDIAR 0x1e /* Transmit DMA Interrupt Arm Register */
387#define TBCR 0x2a /* Transmit Byte count Register */
388#define TARL 0x2c /* Transmit Address Register (low) */
389#define TARU 0x2e /* Transmit Address Register (high) */
390#define NTBCR 0x3a /* Next Transmit Byte count Register */
391#define NTARL 0x3c /* Next Transmit Address Register (low) */
392#define NTARU 0x3e /* Next Transmit Address Register (high) */
393
394#define RDMR 0x82 /* Receive DMA mode Register (non-shared) */
395#define RDIAR 0x9e /* Receive DMA Interrupt Arm Register */
396#define RBCR 0xaa /* Receive Byte count Register */
397#define RARL 0xac /* Receive Address Register (low) */
398#define RARU 0xae /* Receive Address Register (high) */
399#define NRBCR 0xba /* Next Receive Byte count Register */
400#define NRARL 0xbc /* Next Receive Address Register (low) */
401#define NRARU 0xbe /* Next Receive Address Register (high) */
402
403
404/*
405 * MACRO DEFINITIONS FOR MODEM STATUS BITS
406 */
407
408#define MODEMSTATUS_DTR 0x80
409#define MODEMSTATUS_DSR 0x40
410#define MODEMSTATUS_RTS 0x20
411#define MODEMSTATUS_CTS 0x10
412#define MODEMSTATUS_RI 0x04
413#define MODEMSTATUS_DCD 0x01
414
415
416/*
417 * Channel Command/Address Register (CCAR) Command Codes
418 */
419
420#define RTCmd_Null 0x0000
421#define RTCmd_ResetHighestIus 0x1000
422#define RTCmd_TriggerChannelLoadDma 0x2000
423#define RTCmd_TriggerRxDma 0x2800
424#define RTCmd_TriggerTxDma 0x3000
425#define RTCmd_TriggerRxAndTxDma 0x3800
426#define RTCmd_PurgeRxFifo 0x4800
427#define RTCmd_PurgeTxFifo 0x5000
428#define RTCmd_PurgeRxAndTxFifo 0x5800
429#define RTCmd_LoadRcc 0x6800
430#define RTCmd_LoadTcc 0x7000
431#define RTCmd_LoadRccAndTcc 0x7800
432#define RTCmd_LoadTC0 0x8800
433#define RTCmd_LoadTC1 0x9000
434#define RTCmd_LoadTC0AndTC1 0x9800
435#define RTCmd_SerialDataLSBFirst 0xa000
436#define RTCmd_SerialDataMSBFirst 0xa800
437#define RTCmd_SelectBigEndian 0xb000
438#define RTCmd_SelectLittleEndian 0xb800
439
440
441/*
442 * DMA Command/Address Register (DCAR) Command Codes
443 */
444
445#define DmaCmd_Null 0x0000
446#define DmaCmd_ResetTxChannel 0x1000
447#define DmaCmd_ResetRxChannel 0x1200
448#define DmaCmd_StartTxChannel 0x2000
449#define DmaCmd_StartRxChannel 0x2200
450#define DmaCmd_ContinueTxChannel 0x3000
451#define DmaCmd_ContinueRxChannel 0x3200
452#define DmaCmd_PauseTxChannel 0x4000
453#define DmaCmd_PauseRxChannel 0x4200
454#define DmaCmd_AbortTxChannel 0x5000
455#define DmaCmd_AbortRxChannel 0x5200
456#define DmaCmd_InitTxChannel 0x7000
457#define DmaCmd_InitRxChannel 0x7200
458#define DmaCmd_ResetHighestDmaIus 0x8000
459#define DmaCmd_ResetAllChannels 0x9000
460#define DmaCmd_StartAllChannels 0xa000
461#define DmaCmd_ContinueAllChannels 0xb000
462#define DmaCmd_PauseAllChannels 0xc000
463#define DmaCmd_AbortAllChannels 0xd000
464#define DmaCmd_InitAllChannels 0xf000
465
466#define TCmd_Null 0x0000
467#define TCmd_ClearTxCRC 0x2000
468#define TCmd_SelectTicrTtsaData 0x4000
469#define TCmd_SelectTicrTxFifostatus 0x5000
470#define TCmd_SelectTicrIntLevel 0x6000
471#define TCmd_SelectTicrdma_level 0x7000
472#define TCmd_SendFrame 0x8000
473#define TCmd_SendAbort 0x9000
474#define TCmd_EnableDleInsertion 0xc000
475#define TCmd_DisableDleInsertion 0xd000
476#define TCmd_ClearEofEom 0xe000
477#define TCmd_SetEofEom 0xf000
478
479#define RCmd_Null 0x0000
480#define RCmd_ClearRxCRC 0x2000
481#define RCmd_EnterHuntmode 0x3000
482#define RCmd_SelectRicrRtsaData 0x4000
483#define RCmd_SelectRicrRxFifostatus 0x5000
484#define RCmd_SelectRicrIntLevel 0x6000
485#define RCmd_SelectRicrdma_level 0x7000
486
487/*
488 * Bits for enabling and disabling IRQs in Interrupt Control Register (ICR)
489 */
490
491#define RECEIVE_STATUS BIT5
492#define RECEIVE_DATA BIT4
493#define TRANSMIT_STATUS BIT3
494#define TRANSMIT_DATA BIT2
495#define IO_PIN BIT1
496#define MISC BIT0
497
498
499/*
500 * Receive status Bits in Receive Command/status Register RCSR
501 */
502
503#define RXSTATUS_SHORT_FRAME BIT8
504#define RXSTATUS_CODE_VIOLATION BIT8
505#define RXSTATUS_EXITED_HUNT BIT7
506#define RXSTATUS_IDLE_RECEIVED BIT6
507#define RXSTATUS_BREAK_RECEIVED BIT5
508#define RXSTATUS_ABORT_RECEIVED BIT5
509#define RXSTATUS_RXBOUND BIT4
510#define RXSTATUS_CRC_ERROR BIT3
511#define RXSTATUS_FRAMING_ERROR BIT3
512#define RXSTATUS_ABORT BIT2
513#define RXSTATUS_PARITY_ERROR BIT2
514#define RXSTATUS_OVERRUN BIT1
515#define RXSTATUS_DATA_AVAILABLE BIT0
516#define RXSTATUS_ALL 0x01f6
517#define usc_UnlatchRxstatusBits(a,b) usc_OutReg( (a), RCSR, (u16)((b) & RXSTATUS_ALL) )
518
519/*
520 * Values for setting transmit idle mode in
521 * Transmit Control/status Register (TCSR)
522 */
523#define IDLEMODE_FLAGS 0x0000
524#define IDLEMODE_ALT_ONE_ZERO 0x0100
525#define IDLEMODE_ZERO 0x0200
526#define IDLEMODE_ONE 0x0300
527#define IDLEMODE_ALT_MARK_SPACE 0x0500
528#define IDLEMODE_SPACE 0x0600
529#define IDLEMODE_MARK 0x0700
530#define IDLEMODE_MASK 0x0700
531
532/*
533 * IUSC revision identifiers
534 */
535#define IUSC_SL1660 0x4d44
536#define IUSC_PRE_SL1660 0x4553
537
538/*
539 * Transmit status Bits in Transmit Command/status Register (TCSR)
540 */
541
542#define TCSR_PRESERVE 0x0F00
543
544#define TCSR_UNDERWAIT BIT11
545#define TXSTATUS_PREAMBLE_SENT BIT7
546#define TXSTATUS_IDLE_SENT BIT6
547#define TXSTATUS_ABORT_SENT BIT5
548#define TXSTATUS_EOF_SENT BIT4
549#define TXSTATUS_EOM_SENT BIT4
550#define TXSTATUS_CRC_SENT BIT3
551#define TXSTATUS_ALL_SENT BIT2
552#define TXSTATUS_UNDERRUN BIT1
553#define TXSTATUS_FIFO_EMPTY BIT0
554#define TXSTATUS_ALL 0x00fa
555#define usc_UnlatchTxstatusBits(a,b) usc_OutReg( (a), TCSR, (u16)((a)->tcsr_value + ((b) & 0x00FF)) )
556
557
558#define MISCSTATUS_RXC_LATCHED BIT15
559#define MISCSTATUS_RXC BIT14
560#define MISCSTATUS_TXC_LATCHED BIT13
561#define MISCSTATUS_TXC BIT12
562#define MISCSTATUS_RI_LATCHED BIT11
563#define MISCSTATUS_RI BIT10
564#define MISCSTATUS_DSR_LATCHED BIT9
565#define MISCSTATUS_DSR BIT8
566#define MISCSTATUS_DCD_LATCHED BIT7
567#define MISCSTATUS_DCD BIT6
568#define MISCSTATUS_CTS_LATCHED BIT5
569#define MISCSTATUS_CTS BIT4
570#define MISCSTATUS_RCC_UNDERRUN BIT3
571#define MISCSTATUS_DPLL_NO_SYNC BIT2
572#define MISCSTATUS_BRG1_ZERO BIT1
573#define MISCSTATUS_BRG0_ZERO BIT0
574
575#define usc_UnlatchIostatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0xaaa0))
576#define usc_UnlatchMiscstatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0x000f))
577
578#define SICR_RXC_ACTIVE BIT15
579#define SICR_RXC_INACTIVE BIT14
Alexandru Juncue06922a2013-07-27 11:14:39 +0300580#define SICR_RXC (BIT15|BIT14)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581#define SICR_TXC_ACTIVE BIT13
582#define SICR_TXC_INACTIVE BIT12
Alexandru Juncue06922a2013-07-27 11:14:39 +0300583#define SICR_TXC (BIT13|BIT12)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584#define SICR_RI_ACTIVE BIT11
585#define SICR_RI_INACTIVE BIT10
Alexandru Juncue06922a2013-07-27 11:14:39 +0300586#define SICR_RI (BIT11|BIT10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#define SICR_DSR_ACTIVE BIT9
588#define SICR_DSR_INACTIVE BIT8
Alexandru Juncue06922a2013-07-27 11:14:39 +0300589#define SICR_DSR (BIT9|BIT8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590#define SICR_DCD_ACTIVE BIT7
591#define SICR_DCD_INACTIVE BIT6
Alexandru Juncue06922a2013-07-27 11:14:39 +0300592#define SICR_DCD (BIT7|BIT6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593#define SICR_CTS_ACTIVE BIT5
594#define SICR_CTS_INACTIVE BIT4
Alexandru Juncue06922a2013-07-27 11:14:39 +0300595#define SICR_CTS (BIT5|BIT4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#define SICR_RCC_UNDERFLOW BIT3
597#define SICR_DPLL_NO_SYNC BIT2
598#define SICR_BRG1_ZERO BIT1
599#define SICR_BRG0_ZERO BIT0
600
601void usc_DisableMasterIrqBit( struct mgsl_struct *info );
602void usc_EnableMasterIrqBit( struct mgsl_struct *info );
603void usc_EnableInterrupts( struct mgsl_struct *info, u16 IrqMask );
604void usc_DisableInterrupts( struct mgsl_struct *info, u16 IrqMask );
605void usc_ClearIrqPendingBits( struct mgsl_struct *info, u16 IrqMask );
606
607#define usc_EnableInterrupts( a, b ) \
608 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0xc0 + (b)) )
609
610#define usc_DisableInterrupts( a, b ) \
611 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0x80 + (b)) )
612
613#define usc_EnableMasterIrqBit(a) \
614 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0x0f00) + 0xb000) )
615
616#define usc_DisableMasterIrqBit(a) \
617 usc_OutReg( (a), ICR, (u16)(usc_InReg((a),ICR) & 0x7f00) )
618
619#define usc_ClearIrqPendingBits( a, b ) usc_OutReg( (a), DCCR, 0x40 + (b) )
620
621/*
622 * Transmit status Bits in Transmit Control status Register (TCSR)
623 * and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0)
624 */
625
626#define TXSTATUS_PREAMBLE_SENT BIT7
627#define TXSTATUS_IDLE_SENT BIT6
628#define TXSTATUS_ABORT_SENT BIT5
629#define TXSTATUS_EOF BIT4
630#define TXSTATUS_CRC_SENT BIT3
631#define TXSTATUS_ALL_SENT BIT2
632#define TXSTATUS_UNDERRUN BIT1
633#define TXSTATUS_FIFO_EMPTY BIT0
634
635#define DICR_MASTER BIT15
636#define DICR_TRANSMIT BIT0
637#define DICR_RECEIVE BIT1
638
639#define usc_EnableDmaInterrupts(a,b) \
640 usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) | (b)) )
641
642#define usc_DisableDmaInterrupts(a,b) \
643 usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) & ~(b)) )
644
645#define usc_EnableStatusIrqs(a,b) \
646 usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) | (b)) )
647
648#define usc_DisablestatusIrqs(a,b) \
649 usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) & ~(b)) )
650
651/* Transmit status Bits in Transmit Control status Register (TCSR) */
652/* and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0) */
653
654
655#define DISABLE_UNCONDITIONAL 0
656#define DISABLE_END_OF_FRAME 1
657#define ENABLE_UNCONDITIONAL 2
658#define ENABLE_AUTO_CTS 3
659#define ENABLE_AUTO_DCD 3
660#define usc_EnableTransmitter(a,b) \
661 usc_OutReg( (a), TMR, (u16)((usc_InReg((a),TMR) & 0xfffc) | (b)) )
662#define usc_EnableReceiver(a,b) \
663 usc_OutReg( (a), RMR, (u16)((usc_InReg((a),RMR) & 0xfffc) | (b)) )
664
665static u16 usc_InDmaReg( struct mgsl_struct *info, u16 Port );
666static void usc_OutDmaReg( struct mgsl_struct *info, u16 Port, u16 Value );
667static void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd );
668
669static u16 usc_InReg( struct mgsl_struct *info, u16 Port );
670static void usc_OutReg( struct mgsl_struct *info, u16 Port, u16 Value );
671static void usc_RTCmd( struct mgsl_struct *info, u16 Cmd );
672void usc_RCmd( struct mgsl_struct *info, u16 Cmd );
673void usc_TCmd( struct mgsl_struct *info, u16 Cmd );
674
675#define usc_TCmd(a,b) usc_OutReg((a), TCSR, (u16)((a)->tcsr_value + (b)))
676#define usc_RCmd(a,b) usc_OutReg((a), RCSR, (b))
677
678#define usc_SetTransmitSyncChars(a,s0,s1) usc_OutReg((a), TSR, (u16)(((u16)s0<<8)|(u16)s1))
679
680static void usc_process_rxoverrun_sync( struct mgsl_struct *info );
681static void usc_start_receiver( struct mgsl_struct *info );
682static void usc_stop_receiver( struct mgsl_struct *info );
683
684static void usc_start_transmitter( struct mgsl_struct *info );
685static void usc_stop_transmitter( struct mgsl_struct *info );
686static void usc_set_txidle( struct mgsl_struct *info );
687static void usc_load_txfifo( struct mgsl_struct *info );
688
689static void usc_enable_aux_clock( struct mgsl_struct *info, u32 DataRate );
690static void usc_enable_loopback( struct mgsl_struct *info, int enable );
691
692static void usc_get_serial_signals( struct mgsl_struct *info );
693static void usc_set_serial_signals( struct mgsl_struct *info );
694
695static void usc_reset( struct mgsl_struct *info );
696
697static void usc_set_sync_mode( struct mgsl_struct *info );
698static void usc_set_sdlc_mode( struct mgsl_struct *info );
699static void usc_set_async_mode( struct mgsl_struct *info );
700static void usc_enable_async_clock( struct mgsl_struct *info, u32 DataRate );
701
702static void usc_loopback_frame( struct mgsl_struct *info );
703
704static void mgsl_tx_timeout(unsigned long context);
705
706
707static void usc_loopmode_cancel_transmit( struct mgsl_struct * info );
708static void usc_loopmode_insert_request( struct mgsl_struct * info );
709static int usc_loopmode_active( struct mgsl_struct * info);
710static void usc_loopmode_send_done( struct mgsl_struct * info );
711
712static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg);
713
Paul Fulghumaf69c7f2006-12-06 20:40:24 -0800714#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715#define dev_to_port(D) (dev_to_hdlc(D)->priv)
716static void hdlcdev_tx_done(struct mgsl_struct *info);
717static void hdlcdev_rx(struct mgsl_struct *info, char *buf, int size);
718static int hdlcdev_init(struct mgsl_struct *info);
719static void hdlcdev_exit(struct mgsl_struct *info);
720#endif
721
722/*
723 * Defines a BUS descriptor value for the PCI adapter
724 * local bus address ranges.
725 */
726
727#define BUS_DESCRIPTOR( WrHold, WrDly, RdDly, Nwdd, Nwad, Nxda, Nrdd, Nrad ) \
728(0x00400020 + \
729((WrHold) << 30) + \
730((WrDly) << 28) + \
731((RdDly) << 26) + \
732((Nwdd) << 20) + \
733((Nwad) << 15) + \
734((Nxda) << 13) + \
735((Nrdd) << 11) + \
736((Nrad) << 6) )
737
738static void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit);
739
740/*
741 * Adapter diagnostic routines
742 */
Joe Perches0fab6de2008-04-28 02:14:02 -0700743static bool mgsl_register_test( struct mgsl_struct *info );
744static bool mgsl_irq_test( struct mgsl_struct *info );
745static bool mgsl_dma_test( struct mgsl_struct *info );
746static bool mgsl_memory_test( struct mgsl_struct *info );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static int mgsl_adapter_test( struct mgsl_struct *info );
748
749/*
750 * device and resource management routines
751 */
752static int mgsl_claim_resources(struct mgsl_struct *info);
753static void mgsl_release_resources(struct mgsl_struct *info);
754static void mgsl_add_device(struct mgsl_struct *info);
755static struct mgsl_struct* mgsl_allocate_device(void);
756
757/*
758 * DMA buffer manupulation functions.
759 */
760static void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex );
Joe Perches0fab6de2008-04-28 02:14:02 -0700761static bool mgsl_get_rx_frame( struct mgsl_struct *info );
762static bool mgsl_get_raw_rx_frame( struct mgsl_struct *info );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763static void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info );
764static void mgsl_reset_tx_dma_buffers( struct mgsl_struct *info );
765static int num_free_tx_dma_buffers(struct mgsl_struct *info);
766static void mgsl_load_tx_dma_buffer( struct mgsl_struct *info, const char *Buffer, unsigned int BufferSize);
767static void mgsl_load_pci_memory(char* TargetPtr, const char* SourcePtr, unsigned short count);
768
769/*
770 * DMA and Shared Memory buffer allocation and formatting
771 */
772static int mgsl_allocate_dma_buffers(struct mgsl_struct *info);
773static void mgsl_free_dma_buffers(struct mgsl_struct *info);
774static int mgsl_alloc_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
775static void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
776static int mgsl_alloc_buffer_list_memory(struct mgsl_struct *info);
777static void mgsl_free_buffer_list_memory(struct mgsl_struct *info);
778static int mgsl_alloc_intermediate_rxbuffer_memory(struct mgsl_struct *info);
779static void mgsl_free_intermediate_rxbuffer_memory(struct mgsl_struct *info);
780static int mgsl_alloc_intermediate_txbuffer_memory(struct mgsl_struct *info);
781static void mgsl_free_intermediate_txbuffer_memory(struct mgsl_struct *info);
Joe Perches0fab6de2008-04-28 02:14:02 -0700782static bool load_next_tx_holding_buffer(struct mgsl_struct *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783static int save_tx_buffer_request(struct mgsl_struct *info,const char *Buffer, unsigned int BufferSize);
784
785/*
786 * Bottom half interrupt handlers
787 */
David Howellsc4028952006-11-22 14:57:56 +0000788static void mgsl_bh_handler(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789static void mgsl_bh_receive(struct mgsl_struct *info);
790static void mgsl_bh_transmit(struct mgsl_struct *info);
791static void mgsl_bh_status(struct mgsl_struct *info);
792
793/*
794 * Interrupt handler routines and dispatch table.
795 */
796static void mgsl_isr_null( struct mgsl_struct *info );
797static void mgsl_isr_transmit_data( struct mgsl_struct *info );
798static void mgsl_isr_receive_data( struct mgsl_struct *info );
799static void mgsl_isr_receive_status( struct mgsl_struct *info );
800static void mgsl_isr_transmit_status( struct mgsl_struct *info );
801static void mgsl_isr_io_pin( struct mgsl_struct *info );
802static void mgsl_isr_misc( struct mgsl_struct *info );
803static void mgsl_isr_receive_dma( struct mgsl_struct *info );
804static void mgsl_isr_transmit_dma( struct mgsl_struct *info );
805
806typedef void (*isr_dispatch_func)(struct mgsl_struct *);
807
808static isr_dispatch_func UscIsrTable[7] =
809{
810 mgsl_isr_null,
811 mgsl_isr_misc,
812 mgsl_isr_io_pin,
813 mgsl_isr_transmit_data,
814 mgsl_isr_transmit_status,
815 mgsl_isr_receive_data,
816 mgsl_isr_receive_status
817};
818
819/*
820 * ioctl call handlers
821 */
Alan Cox60b33c12011-02-14 16:26:14 +0000822static int tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000823static int tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 unsigned int set, unsigned int clear);
825static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount
826 __user *user_icount);
827static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_params);
828static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_params);
829static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode);
830static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode);
831static int mgsl_txenable(struct mgsl_struct * info, int enable);
832static int mgsl_txabort(struct mgsl_struct * info);
833static int mgsl_rxenable(struct mgsl_struct * info, int enable);
834static int mgsl_wait_event(struct mgsl_struct * info, int __user *mask);
835static int mgsl_loopmode_send_done( struct mgsl_struct * info );
836
837/* set non-zero on successful registration with PCI subsystem */
Joe Perches0fab6de2008-04-28 02:14:02 -0700838static bool pci_registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840/*
841 * Global linked list of SyncLink devices
842 */
843static struct mgsl_struct *mgsl_device_list;
844static int mgsl_device_count;
845
846/*
847 * Set this param to non-zero to load eax with the
848 * .text section address and breakpoint on module load.
849 * This is useful for use with gdb and add-symbol-file command.
850 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030851static bool break_on_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853/*
854 * Driver major number, defaults to zero to get auto
855 * assigned major number. May be forced as module parameter.
856 */
857static int ttymajor;
858
859/*
860 * Array of user specified options for ISA adapters.
861 */
862static int io[MAX_ISA_DEVICES];
863static int irq[MAX_ISA_DEVICES];
864static int dma[MAX_ISA_DEVICES];
865static int debug_level;
866static int maxframe[MAX_TOTAL_DEVICES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867static int txdmabufs[MAX_TOTAL_DEVICES];
868static int txholdbufs[MAX_TOTAL_DEVICES];
869
870module_param(break_on_load, bool, 0);
871module_param(ttymajor, int, 0);
872module_param_array(io, int, NULL, 0);
873module_param_array(irq, int, NULL, 0);
874module_param_array(dma, int, NULL, 0);
875module_param(debug_level, int, 0);
876module_param_array(maxframe, int, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877module_param_array(txdmabufs, int, NULL, 0);
878module_param_array(txholdbufs, int, NULL, 0);
879
880static char *driver_name = "SyncLink serial driver";
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -0800881static char *driver_version = "$Revision: 4.38 $";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883static int synclink_init_one (struct pci_dev *dev,
884 const struct pci_device_id *ent);
885static void synclink_remove_one (struct pci_dev *dev);
886
887static struct pci_device_id synclink_pci_tbl[] = {
888 { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_USC, PCI_ANY_ID, PCI_ANY_ID, },
889 { PCI_VENDOR_ID_MICROGATE, 0x0210, PCI_ANY_ID, PCI_ANY_ID, },
890 { 0, }, /* terminate list */
891};
892MODULE_DEVICE_TABLE(pci, synclink_pci_tbl);
893
894MODULE_LICENSE("GPL");
895
896static struct pci_driver synclink_pci_driver = {
897 .name = "synclink",
898 .id_table = synclink_pci_tbl,
899 .probe = synclink_init_one,
Bill Pemberton91116cb2012-11-19 13:21:06 -0500900 .remove = synclink_remove_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901};
902
903static struct tty_driver *serial_driver;
904
905/* number of characters left in xmit buffer before we ask for more */
906#define WAKEUP_CHARS 256
907
908
909static void mgsl_change_params(struct mgsl_struct *info);
910static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout);
911
912/*
913 * 1st function defined in .text section. Calling this function in
914 * init_module() followed by a breakpoint allows a remote debugger
915 * (gdb) to get the .text address for the add-symbol-file command.
916 * This allows remote debugging of dynamically loadable modules.
917 */
918static void* mgsl_get_text_ptr(void)
919{
920 return mgsl_get_text_ptr;
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static inline int mgsl_paranoia_check(struct mgsl_struct *info,
924 char *name, const char *routine)
925{
926#ifdef MGSL_PARANOIA_CHECK
927 static const char *badmagic =
928 "Warning: bad magic number for mgsl struct (%s) in %s\n";
929 static const char *badinfo =
930 "Warning: null mgsl_struct for (%s) in %s\n";
931
932 if (!info) {
933 printk(badinfo, name, routine);
934 return 1;
935 }
936 if (info->magic != MGSL_MAGIC) {
937 printk(badmagic, name, routine);
938 return 1;
939 }
940#else
941 if (!info)
942 return 1;
943#endif
944 return 0;
945}
946
947/**
948 * line discipline callback wrappers
949 *
950 * The wrappers maintain line discipline references
951 * while calling into the line discipline.
952 *
953 * ldisc_receive_buf - pass receive data to line discipline
954 */
955
956static void ldisc_receive_buf(struct tty_struct *tty,
957 const __u8 *data, char *flags, int count)
958{
959 struct tty_ldisc *ld;
960 if (!tty)
961 return;
962 ld = tty_ldisc_ref(tty);
963 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100964 if (ld->ops->receive_buf)
965 ld->ops->receive_buf(tty, data, flags, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 tty_ldisc_deref(ld);
967 }
968}
969
970/* mgsl_stop() throttle (stop) transmitter
971 *
972 * Arguments: tty pointer to tty info structure
973 * Return Value: None
974 */
975static void mgsl_stop(struct tty_struct *tty)
976{
Alan Coxc9f19e92009-01-02 13:47:26 +0000977 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 unsigned long flags;
979
980 if (mgsl_paranoia_check(info, tty->name, "mgsl_stop"))
981 return;
982
983 if ( debug_level >= DEBUG_LEVEL_INFO )
984 printk("mgsl_stop(%s)\n",info->device_name);
985
986 spin_lock_irqsave(&info->irq_spinlock,flags);
987 if (info->tx_enabled)
988 usc_stop_transmitter(info);
989 spin_unlock_irqrestore(&info->irq_spinlock,flags);
990
991} /* end of mgsl_stop() */
992
993/* mgsl_start() release (start) transmitter
994 *
995 * Arguments: tty pointer to tty info structure
996 * Return Value: None
997 */
998static void mgsl_start(struct tty_struct *tty)
999{
Alan Coxc9f19e92009-01-02 13:47:26 +00001000 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 unsigned long flags;
1002
1003 if (mgsl_paranoia_check(info, tty->name, "mgsl_start"))
1004 return;
1005
1006 if ( debug_level >= DEBUG_LEVEL_INFO )
1007 printk("mgsl_start(%s)\n",info->device_name);
1008
1009 spin_lock_irqsave(&info->irq_spinlock,flags);
1010 if (!info->tx_enabled)
1011 usc_start_transmitter(info);
1012 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1013
1014} /* end of mgsl_start() */
1015
1016/*
1017 * Bottom half work queue access functions
1018 */
1019
1020/* mgsl_bh_action() Return next bottom half action to perform.
1021 * Return Value: BH action code or 0 if nothing to do.
1022 */
1023static int mgsl_bh_action(struct mgsl_struct *info)
1024{
1025 unsigned long flags;
1026 int rc = 0;
1027
1028 spin_lock_irqsave(&info->irq_spinlock,flags);
1029
1030 if (info->pending_bh & BH_RECEIVE) {
1031 info->pending_bh &= ~BH_RECEIVE;
1032 rc = BH_RECEIVE;
1033 } else if (info->pending_bh & BH_TRANSMIT) {
1034 info->pending_bh &= ~BH_TRANSMIT;
1035 rc = BH_TRANSMIT;
1036 } else if (info->pending_bh & BH_STATUS) {
1037 info->pending_bh &= ~BH_STATUS;
1038 rc = BH_STATUS;
1039 }
1040
1041 if (!rc) {
1042 /* Mark BH routine as complete */
Joe Perches0fab6de2008-04-28 02:14:02 -07001043 info->bh_running = false;
1044 info->bh_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
1047 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1048
1049 return rc;
1050}
1051
1052/*
1053 * Perform bottom half processing of work items queued by ISR.
1054 */
David Howellsc4028952006-11-22 14:57:56 +00001055static void mgsl_bh_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
David Howellsc4028952006-11-22 14:57:56 +00001057 struct mgsl_struct *info =
1058 container_of(work, struct mgsl_struct, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 int action;
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if ( debug_level >= DEBUG_LEVEL_BH )
1062 printk( "%s(%d):mgsl_bh_handler(%s) entry\n",
1063 __FILE__,__LINE__,info->device_name);
1064
Joe Perches0fab6de2008-04-28 02:14:02 -07001065 info->bh_running = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 while((action = mgsl_bh_action(info)) != 0) {
1068
1069 /* Process work item */
1070 if ( debug_level >= DEBUG_LEVEL_BH )
1071 printk( "%s(%d):mgsl_bh_handler() work item action=%d\n",
1072 __FILE__,__LINE__,action);
1073
1074 switch (action) {
1075
1076 case BH_RECEIVE:
1077 mgsl_bh_receive(info);
1078 break;
1079 case BH_TRANSMIT:
1080 mgsl_bh_transmit(info);
1081 break;
1082 case BH_STATUS:
1083 mgsl_bh_status(info);
1084 break;
1085 default:
1086 /* unknown work item ID */
1087 printk("Unknown work item ID=%08X!\n", action);
1088 break;
1089 }
1090 }
1091
1092 if ( debug_level >= DEBUG_LEVEL_BH )
1093 printk( "%s(%d):mgsl_bh_handler(%s) exit\n",
1094 __FILE__,__LINE__,info->device_name);
1095}
1096
1097static void mgsl_bh_receive(struct mgsl_struct *info)
1098{
Joe Perches0fab6de2008-04-28 02:14:02 -07001099 bool (*get_rx_frame)(struct mgsl_struct *info) =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 (info->params.mode == MGSL_MODE_HDLC ? mgsl_get_rx_frame : mgsl_get_raw_rx_frame);
1101
1102 if ( debug_level >= DEBUG_LEVEL_BH )
1103 printk( "%s(%d):mgsl_bh_receive(%s)\n",
1104 __FILE__,__LINE__,info->device_name);
1105
1106 do
1107 {
1108 if (info->rx_rcc_underrun) {
1109 unsigned long flags;
1110 spin_lock_irqsave(&info->irq_spinlock,flags);
1111 usc_start_receiver(info);
1112 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1113 return;
1114 }
1115 } while(get_rx_frame(info));
1116}
1117
1118static void mgsl_bh_transmit(struct mgsl_struct *info)
1119{
Alan Cox8fb06c72008-07-16 21:56:46 +01001120 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 unsigned long flags;
1122
1123 if ( debug_level >= DEBUG_LEVEL_BH )
1124 printk( "%s(%d):mgsl_bh_transmit() entry on %s\n",
1125 __FILE__,__LINE__,info->device_name);
1126
Jiri Slabyb963a842007-02-10 01:44:55 -08001127 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 /* if transmitter idle and loopmode_send_done_requested
1131 * then start echoing RxD to TxD
1132 */
1133 spin_lock_irqsave(&info->irq_spinlock,flags);
1134 if ( !info->tx_active && info->loopmode_send_done_requested )
1135 usc_loopmode_send_done( info );
1136 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1137}
1138
1139static void mgsl_bh_status(struct mgsl_struct *info)
1140{
1141 if ( debug_level >= DEBUG_LEVEL_BH )
1142 printk( "%s(%d):mgsl_bh_status() entry on %s\n",
1143 __FILE__,__LINE__,info->device_name);
1144
1145 info->ri_chkcount = 0;
1146 info->dsr_chkcount = 0;
1147 info->dcd_chkcount = 0;
1148 info->cts_chkcount = 0;
1149}
1150
1151/* mgsl_isr_receive_status()
1152 *
1153 * Service a receive status interrupt. The type of status
1154 * interrupt is indicated by the state of the RCSR.
1155 * This is only used for HDLC mode.
1156 *
1157 * Arguments: info pointer to device instance data
1158 * Return Value: None
1159 */
1160static void mgsl_isr_receive_status( struct mgsl_struct *info )
1161{
1162 u16 status = usc_InReg( info, RCSR );
1163
Alexandru Juncue06922a2013-07-27 11:14:39 +03001164 if ( debug_level >= DEBUG_LEVEL_ISR )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 printk("%s(%d):mgsl_isr_receive_status status=%04X\n",
1166 __FILE__,__LINE__,status);
1167
1168 if ( (status & RXSTATUS_ABORT_RECEIVED) &&
1169 info->loopmode_insert_requested &&
1170 usc_loopmode_active(info) )
1171 {
1172 ++info->icount.rxabort;
Joe Perches0fab6de2008-04-28 02:14:02 -07001173 info->loopmode_insert_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 /* clear CMR:13 to start echoing RxD to TxD */
1176 info->cmr_value &= ~BIT13;
1177 usc_OutReg(info, CMR, info->cmr_value);
1178
1179 /* disable received abort irq (no longer required) */
1180 usc_OutReg(info, RICR,
1181 (usc_InReg(info, RICR) & ~RXSTATUS_ABORT_RECEIVED));
1182 }
1183
Alexandru Juncue06922a2013-07-27 11:14:39 +03001184 if (status & (RXSTATUS_EXITED_HUNT | RXSTATUS_IDLE_RECEIVED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (status & RXSTATUS_EXITED_HUNT)
1186 info->icount.exithunt++;
1187 if (status & RXSTATUS_IDLE_RECEIVED)
1188 info->icount.rxidle++;
1189 wake_up_interruptible(&info->event_wait_q);
1190 }
1191
1192 if (status & RXSTATUS_OVERRUN){
1193 info->icount.rxover++;
1194 usc_process_rxoverrun_sync( info );
1195 }
1196
1197 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
1198 usc_UnlatchRxstatusBits( info, status );
1199
1200} /* end of mgsl_isr_receive_status() */
1201
1202/* mgsl_isr_transmit_status()
1203 *
1204 * Service a transmit status interrupt
1205 * HDLC mode :end of transmit frame
1206 * Async mode:all data is sent
1207 * transmit status is indicated by bits in the TCSR.
1208 *
1209 * Arguments: info pointer to device instance data
1210 * Return Value: None
1211 */
1212static void mgsl_isr_transmit_status( struct mgsl_struct *info )
1213{
1214 u16 status = usc_InReg( info, TCSR );
1215
1216 if ( debug_level >= DEBUG_LEVEL_ISR )
1217 printk("%s(%d):mgsl_isr_transmit_status status=%04X\n",
1218 __FILE__,__LINE__,status);
1219
1220 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
1221 usc_UnlatchTxstatusBits( info, status );
1222
1223 if ( status & (TXSTATUS_UNDERRUN | TXSTATUS_ABORT_SENT) )
1224 {
1225 /* finished sending HDLC abort. This may leave */
1226 /* the TxFifo with data from the aborted frame */
1227 /* so purge the TxFifo. Also shutdown the DMA */
1228 /* channel in case there is data remaining in */
1229 /* the DMA buffer */
1230 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
1231 usc_RTCmd( info, RTCmd_PurgeTxFifo );
1232 }
1233
1234 if ( status & TXSTATUS_EOF_SENT )
1235 info->icount.txok++;
1236 else if ( status & TXSTATUS_UNDERRUN )
1237 info->icount.txunder++;
1238 else if ( status & TXSTATUS_ABORT_SENT )
1239 info->icount.txabort++;
1240 else
1241 info->icount.txunder++;
1242
Joe Perches0fab6de2008-04-28 02:14:02 -07001243 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1245 del_timer(&info->tx_timer);
1246
1247 if ( info->drop_rts_on_tx_done ) {
1248 usc_get_serial_signals( info );
1249 if ( info->serial_signals & SerialSignal_RTS ) {
1250 info->serial_signals &= ~SerialSignal_RTS;
1251 usc_set_serial_signals( info );
1252 }
Joe Perches0fab6de2008-04-28 02:14:02 -07001253 info->drop_rts_on_tx_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08001256#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (info->netcount)
1258 hdlcdev_tx_done(info);
1259 else
1260#endif
1261 {
Alan Cox8fb06c72008-07-16 21:56:46 +01001262 if (info->port.tty->stopped || info->port.tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 usc_stop_transmitter(info);
1264 return;
1265 }
1266 info->pending_bh |= BH_TRANSMIT;
1267 }
1268
1269} /* end of mgsl_isr_transmit_status() */
1270
1271/* mgsl_isr_io_pin()
1272 *
1273 * Service an Input/Output pin interrupt. The type of
1274 * interrupt is indicated by bits in the MISR
1275 *
1276 * Arguments: info pointer to device instance data
1277 * Return Value: None
1278 */
1279static void mgsl_isr_io_pin( struct mgsl_struct *info )
1280{
1281 struct mgsl_icount *icount;
1282 u16 status = usc_InReg( info, MISR );
1283
1284 if ( debug_level >= DEBUG_LEVEL_ISR )
1285 printk("%s(%d):mgsl_isr_io_pin status=%04X\n",
1286 __FILE__,__LINE__,status);
1287
1288 usc_ClearIrqPendingBits( info, IO_PIN );
1289 usc_UnlatchIostatusBits( info, status );
1290
1291 if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
1292 MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
1293 icount = &info->icount;
1294 /* update input line counters */
1295 if (status & MISCSTATUS_RI_LATCHED) {
1296 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1297 usc_DisablestatusIrqs(info,SICR_RI);
1298 icount->rng++;
1299 if ( status & MISCSTATUS_RI )
1300 info->input_signal_events.ri_up++;
1301 else
1302 info->input_signal_events.ri_down++;
1303 }
1304 if (status & MISCSTATUS_DSR_LATCHED) {
1305 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1306 usc_DisablestatusIrqs(info,SICR_DSR);
1307 icount->dsr++;
1308 if ( status & MISCSTATUS_DSR )
1309 info->input_signal_events.dsr_up++;
1310 else
1311 info->input_signal_events.dsr_down++;
1312 }
1313 if (status & MISCSTATUS_DCD_LATCHED) {
1314 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1315 usc_DisablestatusIrqs(info,SICR_DCD);
1316 icount->dcd++;
1317 if (status & MISCSTATUS_DCD) {
1318 info->input_signal_events.dcd_up++;
1319 } else
1320 info->input_signal_events.dcd_down++;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08001321#if SYNCLINK_GENERIC_HDLC
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07001322 if (info->netcount) {
1323 if (status & MISCSTATUS_DCD)
1324 netif_carrier_on(info->netdev);
1325 else
1326 netif_carrier_off(info->netdev);
1327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328#endif
1329 }
1330 if (status & MISCSTATUS_CTS_LATCHED)
1331 {
1332 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1333 usc_DisablestatusIrqs(info,SICR_CTS);
1334 icount->cts++;
1335 if ( status & MISCSTATUS_CTS )
1336 info->input_signal_events.cts_up++;
1337 else
1338 info->input_signal_events.cts_down++;
1339 }
1340 wake_up_interruptible(&info->status_event_wait_q);
1341 wake_up_interruptible(&info->event_wait_q);
1342
Peter Hurley2d686552016-04-09 17:53:23 -07001343 if (tty_port_check_carrier(&info->port) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 (status & MISCSTATUS_DCD_LATCHED) ) {
1345 if ( debug_level >= DEBUG_LEVEL_ISR )
1346 printk("%s CD now %s...", info->device_name,
1347 (status & MISCSTATUS_DCD) ? "on" : "off");
1348 if (status & MISCSTATUS_DCD)
Alan Cox8fb06c72008-07-16 21:56:46 +01001349 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 else {
1351 if ( debug_level >= DEBUG_LEVEL_ISR )
1352 printk("doing serial hangup...");
Alan Cox8fb06c72008-07-16 21:56:46 +01001353 if (info->port.tty)
1354 tty_hangup(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356 }
1357
Huang Shijief21ec3d2012-08-22 22:13:36 -04001358 if (tty_port_cts_enabled(&info->port) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 (status & MISCSTATUS_CTS_LATCHED) ) {
Alan Cox8fb06c72008-07-16 21:56:46 +01001360 if (info->port.tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (status & MISCSTATUS_CTS) {
1362 if ( debug_level >= DEBUG_LEVEL_ISR )
1363 printk("CTS tx start...");
Alan Cox8fb06c72008-07-16 21:56:46 +01001364 if (info->port.tty)
1365 info->port.tty->hw_stopped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 usc_start_transmitter(info);
1367 info->pending_bh |= BH_TRANSMIT;
1368 return;
1369 }
1370 } else {
1371 if (!(status & MISCSTATUS_CTS)) {
1372 if ( debug_level >= DEBUG_LEVEL_ISR )
1373 printk("CTS tx stop...");
Alan Cox8fb06c72008-07-16 21:56:46 +01001374 if (info->port.tty)
1375 info->port.tty->hw_stopped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 usc_stop_transmitter(info);
1377 }
1378 }
1379 }
1380 }
1381
1382 info->pending_bh |= BH_STATUS;
1383
1384 /* for diagnostics set IRQ flag */
1385 if ( status & MISCSTATUS_TXC_LATCHED ){
1386 usc_OutReg( info, SICR,
1387 (unsigned short)(usc_InReg(info,SICR) & ~(SICR_TXC_ACTIVE+SICR_TXC_INACTIVE)) );
1388 usc_UnlatchIostatusBits( info, MISCSTATUS_TXC_LATCHED );
Joe Perches0fab6de2008-04-28 02:14:02 -07001389 info->irq_occurred = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
1392} /* end of mgsl_isr_io_pin() */
1393
1394/* mgsl_isr_transmit_data()
1395 *
1396 * Service a transmit data interrupt (async mode only).
1397 *
1398 * Arguments: info pointer to device instance data
1399 * Return Value: None
1400 */
1401static void mgsl_isr_transmit_data( struct mgsl_struct *info )
1402{
1403 if ( debug_level >= DEBUG_LEVEL_ISR )
1404 printk("%s(%d):mgsl_isr_transmit_data xmit_cnt=%d\n",
1405 __FILE__,__LINE__,info->xmit_cnt);
1406
1407 usc_ClearIrqPendingBits( info, TRANSMIT_DATA );
1408
Alan Cox8fb06c72008-07-16 21:56:46 +01001409 if (info->port.tty->stopped || info->port.tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 usc_stop_transmitter(info);
1411 return;
1412 }
1413
1414 if ( info->xmit_cnt )
1415 usc_load_txfifo( info );
1416 else
Joe Perches0fab6de2008-04-28 02:14:02 -07001417 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 if (info->xmit_cnt < WAKEUP_CHARS)
1420 info->pending_bh |= BH_TRANSMIT;
1421
1422} /* end of mgsl_isr_transmit_data() */
1423
1424/* mgsl_isr_receive_data()
1425 *
1426 * Service a receive data interrupt. This occurs
1427 * when operating in asynchronous interrupt transfer mode.
1428 * The receive data FIFO is flushed to the receive data buffers.
1429 *
1430 * Arguments: info pointer to device instance data
1431 * Return Value: None
1432 */
1433static void mgsl_isr_receive_data( struct mgsl_struct *info )
1434{
1435 int Fifocount;
1436 u16 status;
Alan Cox33f0f882006-01-09 20:54:13 -08001437 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 unsigned char DataByte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 struct mgsl_icount *icount = &info->icount;
1440
1441 if ( debug_level >= DEBUG_LEVEL_ISR )
1442 printk("%s(%d):mgsl_isr_receive_data\n",
1443 __FILE__,__LINE__);
1444
1445 usc_ClearIrqPendingBits( info, RECEIVE_DATA );
1446
1447 /* select FIFO status for RICR readback */
1448 usc_RCmd( info, RCmd_SelectRicrRxFifostatus );
1449
1450 /* clear the Wordstatus bit so that status readback */
1451 /* only reflects the status of this byte */
1452 usc_OutReg( info, RICR+LSBONLY, (u16)(usc_InReg(info, RICR+LSBONLY) & ~BIT3 ));
1453
1454 /* flush the receive FIFO */
1455
1456 while( (Fifocount = (usc_InReg(info,RICR) >> 8)) ) {
Alan Cox33f0f882006-01-09 20:54:13 -08001457 int flag;
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 /* read one byte from RxFIFO */
1460 outw( (inw(info->io_base + CCAR) & 0x0780) | (RDR+LSBONLY),
1461 info->io_base + CCAR );
1462 DataByte = inb( info->io_base + CCAR );
1463
1464 /* get the status of the received byte */
1465 status = usc_InReg(info, RCSR);
Alexandru Juncue06922a2013-07-27 11:14:39 +03001466 if ( status & (RXSTATUS_FRAMING_ERROR | RXSTATUS_PARITY_ERROR |
1467 RXSTATUS_OVERRUN | RXSTATUS_BREAK_RECEIVED) )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 icount->rx++;
1471
Alan Cox33f0f882006-01-09 20:54:13 -08001472 flag = 0;
Alexandru Juncue06922a2013-07-27 11:14:39 +03001473 if ( status & (RXSTATUS_FRAMING_ERROR | RXSTATUS_PARITY_ERROR |
1474 RXSTATUS_OVERRUN | RXSTATUS_BREAK_RECEIVED) ) {
1475 printk("rxerr=%04X\n",status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* update error statistics */
1477 if ( status & RXSTATUS_BREAK_RECEIVED ) {
Alexandru Juncue06922a2013-07-27 11:14:39 +03001478 status &= ~(RXSTATUS_FRAMING_ERROR | RXSTATUS_PARITY_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 icount->brk++;
Alexandru Juncue06922a2013-07-27 11:14:39 +03001480 } else if (status & RXSTATUS_PARITY_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 icount->parity++;
1482 else if (status & RXSTATUS_FRAMING_ERROR)
1483 icount->frame++;
1484 else if (status & RXSTATUS_OVERRUN) {
1485 /* must issue purge fifo cmd before */
1486 /* 16C32 accepts more receive chars */
1487 usc_RTCmd(info,RTCmd_PurgeRxFifo);
1488 icount->overrun++;
1489 }
1490
Alexandru Juncue06922a2013-07-27 11:14:39 +03001491 /* discard char if tty control flags say so */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (status & info->ignore_status_mask)
1493 continue;
1494
1495 status &= info->read_status_mask;
1496
1497 if (status & RXSTATUS_BREAK_RECEIVED) {
Alan Cox33f0f882006-01-09 20:54:13 -08001498 flag = TTY_BREAK;
Alan Cox8fb06c72008-07-16 21:56:46 +01001499 if (info->port.flags & ASYNC_SAK)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001500 do_SAK(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 } else if (status & RXSTATUS_PARITY_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -08001502 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 else if (status & RXSTATUS_FRAMING_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -08001504 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 } /* end of if (error) */
Jiri Slaby92a19f92013-01-03 15:53:03 +01001506 tty_insert_flip_char(&info->port, DataByte, flag);
Alan Cox33f0f882006-01-09 20:54:13 -08001507 if (status & RXSTATUS_OVERRUN) {
1508 /* Overrun is special, since it's
1509 * reported immediately, and doesn't
1510 * affect the current character
1511 */
Jiri Slaby92a19f92013-01-03 15:53:03 +01001512 work += tty_insert_flip_char(&info->port, 0, TTY_OVERRUN);
Alan Cox33f0f882006-01-09 20:54:13 -08001513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515
1516 if ( debug_level >= DEBUG_LEVEL_ISR ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1518 __FILE__,__LINE__,icount->rx,icount->brk,
1519 icount->parity,icount->frame,icount->overrun);
1520 }
1521
Alan Cox33f0f882006-01-09 20:54:13 -08001522 if(work)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001523 tty_flip_buffer_push(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526/* mgsl_isr_misc()
1527 *
Joe Perches8dfba4d2008-02-03 17:11:42 +02001528 * Service a miscellaneous interrupt source.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 *
1530 * Arguments: info pointer to device extension (instance data)
1531 * Return Value: None
1532 */
1533static void mgsl_isr_misc( struct mgsl_struct *info )
1534{
1535 u16 status = usc_InReg( info, MISR );
1536
1537 if ( debug_level >= DEBUG_LEVEL_ISR )
1538 printk("%s(%d):mgsl_isr_misc status=%04X\n",
1539 __FILE__,__LINE__,status);
1540
1541 if ((status & MISCSTATUS_RCC_UNDERRUN) &&
1542 (info->params.mode == MGSL_MODE_HDLC)) {
1543
1544 /* turn off receiver and rx DMA */
1545 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
1546 usc_DmaCmd(info, DmaCmd_ResetRxChannel);
1547 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
Alexandru Juncue06922a2013-07-27 11:14:39 +03001548 usc_ClearIrqPendingBits(info, RECEIVE_DATA | RECEIVE_STATUS);
1549 usc_DisableInterrupts(info, RECEIVE_DATA | RECEIVE_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /* schedule BH handler to restart receiver */
1552 info->pending_bh |= BH_RECEIVE;
Joe Perches0fab6de2008-04-28 02:14:02 -07001553 info->rx_rcc_underrun = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555
1556 usc_ClearIrqPendingBits( info, MISC );
1557 usc_UnlatchMiscstatusBits( info, status );
1558
1559} /* end of mgsl_isr_misc() */
1560
1561/* mgsl_isr_null()
1562 *
1563 * Services undefined interrupt vectors from the
1564 * USC. (hence this function SHOULD never be called)
1565 *
1566 * Arguments: info pointer to device extension (instance data)
1567 * Return Value: None
1568 */
1569static void mgsl_isr_null( struct mgsl_struct *info )
1570{
1571
1572} /* end of mgsl_isr_null() */
1573
1574/* mgsl_isr_receive_dma()
1575 *
1576 * Service a receive DMA channel interrupt.
1577 * For this driver there are two sources of receive DMA interrupts
1578 * as identified in the Receive DMA mode Register (RDMR):
1579 *
1580 * BIT3 EOA/EOL End of List, all receive buffers in receive
1581 * buffer list have been filled (no more free buffers
1582 * available). The DMA controller has shut down.
1583 *
1584 * BIT2 EOB End of Buffer. This interrupt occurs when a receive
1585 * DMA buffer is terminated in response to completion
1586 * of a good frame or a frame with errors. The status
1587 * of the frame is stored in the buffer entry in the
1588 * list of receive buffer entries.
1589 *
1590 * Arguments: info pointer to device instance data
1591 * Return Value: None
1592 */
1593static void mgsl_isr_receive_dma( struct mgsl_struct *info )
1594{
1595 u16 status;
1596
1597 /* clear interrupt pending and IUS bit for Rx DMA IRQ */
Alexandru Juncue06922a2013-07-27 11:14:39 +03001598 usc_OutDmaReg( info, CDIR, BIT9 | BIT1 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 /* Read the receive DMA status to identify interrupt type. */
1601 /* This also clears the status bits. */
1602 status = usc_InDmaReg( info, RDMR );
1603
1604 if ( debug_level >= DEBUG_LEVEL_ISR )
1605 printk("%s(%d):mgsl_isr_receive_dma(%s) status=%04X\n",
1606 __FILE__,__LINE__,info->device_name,status);
1607
1608 info->pending_bh |= BH_RECEIVE;
1609
1610 if ( status & BIT3 ) {
Joe Perches0fab6de2008-04-28 02:14:02 -07001611 info->rx_overflow = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 info->icount.buf_overrun++;
1613 }
1614
1615} /* end of mgsl_isr_receive_dma() */
1616
1617/* mgsl_isr_transmit_dma()
1618 *
1619 * This function services a transmit DMA channel interrupt.
1620 *
1621 * For this driver there is one source of transmit DMA interrupts
1622 * as identified in the Transmit DMA Mode Register (TDMR):
1623 *
1624 * BIT2 EOB End of Buffer. This interrupt occurs when a
1625 * transmit DMA buffer has been emptied.
1626 *
1627 * The driver maintains enough transmit DMA buffers to hold at least
1628 * one max frame size transmit frame. When operating in a buffered
1629 * transmit mode, there may be enough transmit DMA buffers to hold at
1630 * least two or more max frame size frames. On an EOB condition,
1631 * determine if there are any queued transmit buffers and copy into
1632 * transmit DMA buffers if we have room.
1633 *
1634 * Arguments: info pointer to device instance data
1635 * Return Value: None
1636 */
1637static void mgsl_isr_transmit_dma( struct mgsl_struct *info )
1638{
1639 u16 status;
1640
1641 /* clear interrupt pending and IUS bit for Tx DMA IRQ */
Alexandru Juncue06922a2013-07-27 11:14:39 +03001642 usc_OutDmaReg(info, CDIR, BIT8 | BIT0 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 /* Read the transmit DMA status to identify interrupt type. */
1645 /* This also clears the status bits. */
1646
1647 status = usc_InDmaReg( info, TDMR );
1648
1649 if ( debug_level >= DEBUG_LEVEL_ISR )
1650 printk("%s(%d):mgsl_isr_transmit_dma(%s) status=%04X\n",
1651 __FILE__,__LINE__,info->device_name,status);
1652
1653 if ( status & BIT2 ) {
1654 --info->tx_dma_buffers_used;
1655
1656 /* if there are transmit frames queued,
1657 * try to load the next one
1658 */
1659 if ( load_next_tx_holding_buffer(info) ) {
1660 /* if call returns non-zero value, we have
1661 * at least one free tx holding buffer
1662 */
1663 info->pending_bh |= BH_TRANSMIT;
1664 }
1665 }
1666
1667} /* end of mgsl_isr_transmit_dma() */
1668
1669/* mgsl_interrupt()
1670 *
1671 * Interrupt service routine entry point.
1672 *
1673 * Arguments:
1674 *
1675 * irq interrupt number that caused interrupt
1676 * dev_id device ID supplied during interrupt registration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 *
1678 * Return Value: None
1679 */
Jeff Garzika6f97b22007-10-31 05:20:49 -04001680static irqreturn_t mgsl_interrupt(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Jeff Garzika6f97b22007-10-31 05:20:49 -04001682 struct mgsl_struct *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 u16 UscVector;
1684 u16 DmaVector;
1685
1686 if ( debug_level >= DEBUG_LEVEL_ISR )
Jeff Garzika6f97b22007-10-31 05:20:49 -04001687 printk(KERN_DEBUG "%s(%d):mgsl_interrupt(%d)entry.\n",
1688 __FILE__, __LINE__, info->irq_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 spin_lock(&info->irq_spinlock);
1691
1692 for(;;) {
1693 /* Read the interrupt vectors from hardware. */
1694 UscVector = usc_InReg(info, IVR) >> 9;
1695 DmaVector = usc_InDmaReg(info, DIVR);
1696
1697 if ( debug_level >= DEBUG_LEVEL_ISR )
1698 printk("%s(%d):%s UscVector=%08X DmaVector=%08X\n",
1699 __FILE__,__LINE__,info->device_name,UscVector,DmaVector);
1700
1701 if ( !UscVector && !DmaVector )
1702 break;
1703
1704 /* Dispatch interrupt vector */
1705 if ( UscVector )
1706 (*UscIsrTable[UscVector])(info);
1707 else if ( (DmaVector&(BIT10|BIT9)) == BIT10)
1708 mgsl_isr_transmit_dma(info);
1709 else
1710 mgsl_isr_receive_dma(info);
1711
1712 if ( info->isr_overflow ) {
Jeff Garzika6f97b22007-10-31 05:20:49 -04001713 printk(KERN_ERR "%s(%d):%s isr overflow irq=%d\n",
1714 __FILE__, __LINE__, info->device_name, info->irq_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 usc_DisableMasterIrqBit(info);
1716 usc_DisableDmaInterrupts(info,DICR_MASTER);
1717 break;
1718 }
1719 }
1720
1721 /* Request bottom half processing if there's something
1722 * for it to do and the bh is not already running
1723 */
1724
1725 if ( info->pending_bh && !info->bh_running && !info->bh_requested ) {
1726 if ( debug_level >= DEBUG_LEVEL_ISR )
1727 printk("%s(%d):%s queueing bh task.\n",
1728 __FILE__,__LINE__,info->device_name);
1729 schedule_work(&info->task);
Joe Perches0fab6de2008-04-28 02:14:02 -07001730 info->bh_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732
1733 spin_unlock(&info->irq_spinlock);
1734
1735 if ( debug_level >= DEBUG_LEVEL_ISR )
Jeff Garzika6f97b22007-10-31 05:20:49 -04001736 printk(KERN_DEBUG "%s(%d):mgsl_interrupt(%d)exit.\n",
1737 __FILE__, __LINE__, info->irq_level);
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return IRQ_HANDLED;
1740} /* end of mgsl_interrupt() */
1741
1742/* startup()
1743 *
1744 * Initialize and start device.
1745 *
1746 * Arguments: info pointer to device instance data
1747 * Return Value: 0 if success, otherwise error code
1748 */
1749static int startup(struct mgsl_struct * info)
1750{
1751 int retval = 0;
Peter Hurleyd41861c2016-04-09 17:53:25 -07001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if ( debug_level >= DEBUG_LEVEL_INFO )
1754 printk("%s(%d):mgsl_startup(%s)\n",__FILE__,__LINE__,info->device_name);
Peter Hurleyd41861c2016-04-09 17:53:25 -07001755
1756 if (tty_port_initialized(&info->port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return 0;
Peter Hurleyd41861c2016-04-09 17:53:25 -07001758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (!info->xmit_buf) {
1760 /* allocate a page of memory for a transmit buffer */
1761 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1762 if (!info->xmit_buf) {
1763 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1764 __FILE__,__LINE__,info->device_name);
1765 return -ENOMEM;
1766 }
1767 }
1768
1769 info->pending_bh = 0;
1770
Paul Fulghum96612392005-09-09 13:02:13 -07001771 memset(&info->icount, 0, sizeof(info->icount));
1772
Jiri Slaby40565f12007-02-12 00:52:31 -08001773 setup_timer(&info->tx_timer, mgsl_tx_timeout, (unsigned long)info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 /* Allocate and claim adapter resources */
1776 retval = mgsl_claim_resources(info);
1777
1778 /* perform existence check and diagnostics */
1779 if ( !retval )
1780 retval = mgsl_adapter_test(info);
1781
1782 if ( retval ) {
Alan Cox8fb06c72008-07-16 21:56:46 +01001783 if (capable(CAP_SYS_ADMIN) && info->port.tty)
1784 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 mgsl_release_resources(info);
1786 return retval;
1787 }
1788
1789 /* program hardware for current parameters */
1790 mgsl_change_params(info);
Peter Hurleyd41861c2016-04-09 17:53:25 -07001791
Alan Cox8fb06c72008-07-16 21:56:46 +01001792 if (info->port.tty)
1793 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Peter Hurleyd41861c2016-04-09 17:53:25 -07001795 tty_port_set_initialized(&info->port, 1);
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798} /* end of startup() */
1799
1800/* shutdown()
1801 *
1802 * Called by mgsl_close() and mgsl_hangup() to shutdown hardware
1803 *
1804 * Arguments: info pointer to device instance data
1805 * Return Value: None
1806 */
1807static void shutdown(struct mgsl_struct * info)
1808{
1809 unsigned long flags;
Peter Hurleyd41861c2016-04-09 17:53:25 -07001810
1811 if (!tty_port_initialized(&info->port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return;
1813
1814 if (debug_level >= DEBUG_LEVEL_INFO)
1815 printk("%s(%d):mgsl_shutdown(%s)\n",
1816 __FILE__,__LINE__, info->device_name );
1817
1818 /* clear status wait queue because status changes */
1819 /* can't happen after shutting down the hardware */
1820 wake_up_interruptible(&info->status_event_wait_q);
1821 wake_up_interruptible(&info->event_wait_q);
1822
Jiri Slaby40565f12007-02-12 00:52:31 -08001823 del_timer_sync(&info->tx_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 if (info->xmit_buf) {
1826 free_page((unsigned long) info->xmit_buf);
1827 info->xmit_buf = NULL;
1828 }
1829
1830 spin_lock_irqsave(&info->irq_spinlock,flags);
1831 usc_DisableMasterIrqBit(info);
1832 usc_stop_receiver(info);
1833 usc_stop_transmitter(info);
Alexandru Juncue06922a2013-07-27 11:14:39 +03001834 usc_DisableInterrupts(info,RECEIVE_DATA | RECEIVE_STATUS |
1835 TRANSMIT_DATA | TRANSMIT_STATUS | IO_PIN | MISC );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 usc_DisableDmaInterrupts(info,DICR_MASTER + DICR_TRANSMIT + DICR_RECEIVE);
Alan Coxadc8d742012-07-14 15:31:47 +01001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 /* Disable DMAEN (Port 7, Bit 14) */
1839 /* This disconnects the DMA request signal from the ISA bus */
1840 /* on the ISA adapter. This has no effect for the PCI adapter */
1841 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) | BIT14));
Alan Coxadc8d742012-07-14 15:31:47 +01001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* Disable INTEN (Port 6, Bit12) */
1844 /* This disconnects the IRQ request signal to the ISA bus */
1845 /* on the ISA adapter. This has no effect for the PCI adapter */
1846 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) | BIT12));
Alan Coxadc8d742012-07-14 15:31:47 +01001847
1848 if (!info->port.tty || info->port.tty->termios.c_cflag & HUPCL) {
Joe Perches9fe80742013-01-27 18:21:00 -08001849 info->serial_signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 usc_set_serial_signals(info);
1851 }
Alan Coxadc8d742012-07-14 15:31:47 +01001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1854
Peter Hurleyd41861c2016-04-09 17:53:25 -07001855 mgsl_release_resources(info);
1856
Alan Cox8fb06c72008-07-16 21:56:46 +01001857 if (info->port.tty)
1858 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Peter Hurleyd41861c2016-04-09 17:53:25 -07001860 tty_port_set_initialized(&info->port, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861} /* end of shutdown() */
1862
1863static void mgsl_program_hw(struct mgsl_struct *info)
1864{
1865 unsigned long flags;
1866
1867 spin_lock_irqsave(&info->irq_spinlock,flags);
1868
1869 usc_stop_receiver(info);
1870 usc_stop_transmitter(info);
1871 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1872
1873 if (info->params.mode == MGSL_MODE_HDLC ||
1874 info->params.mode == MGSL_MODE_RAW ||
1875 info->netcount)
1876 usc_set_sync_mode(info);
1877 else
1878 usc_set_async_mode(info);
1879
1880 usc_set_serial_signals(info);
1881
1882 info->dcd_chkcount = 0;
1883 info->cts_chkcount = 0;
1884 info->ri_chkcount = 0;
1885 info->dsr_chkcount = 0;
1886
Alexandru Juncue06922a2013-07-27 11:14:39 +03001887 usc_EnableStatusIrqs(info,SICR_CTS+SICR_DSR+SICR_DCD+SICR_RI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 usc_EnableInterrupts(info, IO_PIN);
1889 usc_get_serial_signals(info);
1890
Alan Coxadc8d742012-07-14 15:31:47 +01001891 if (info->netcount || info->port.tty->termios.c_cflag & CREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 usc_start_receiver(info);
1893
1894 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1895}
1896
1897/* Reconfigure adapter based on new parameters
1898 */
1899static void mgsl_change_params(struct mgsl_struct *info)
1900{
1901 unsigned cflag;
1902 int bits_per_char;
1903
Alan Coxadc8d742012-07-14 15:31:47 +01001904 if (!info->port.tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return;
1906
1907 if (debug_level >= DEBUG_LEVEL_INFO)
1908 printk("%s(%d):mgsl_change_params(%s)\n",
1909 __FILE__,__LINE__, info->device_name );
1910
Alan Coxadc8d742012-07-14 15:31:47 +01001911 cflag = info->port.tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Joe Perches9fe80742013-01-27 18:21:00 -08001913 /* if B0 rate (hangup) specified then negate RTS and DTR */
1914 /* otherwise assert RTS and DTR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (cflag & CBAUD)
Joe Perches9fe80742013-01-27 18:21:00 -08001916 info->serial_signals |= SerialSignal_RTS | SerialSignal_DTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 else
Joe Perches9fe80742013-01-27 18:21:00 -08001918 info->serial_signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 /* byte size and parity */
1921
1922 switch (cflag & CSIZE) {
1923 case CS5: info->params.data_bits = 5; break;
1924 case CS6: info->params.data_bits = 6; break;
1925 case CS7: info->params.data_bits = 7; break;
1926 case CS8: info->params.data_bits = 8; break;
1927 /* Never happens, but GCC is too dumb to figure it out */
1928 default: info->params.data_bits = 7; break;
1929 }
1930
1931 if (cflag & CSTOPB)
1932 info->params.stop_bits = 2;
1933 else
1934 info->params.stop_bits = 1;
1935
1936 info->params.parity = ASYNC_PARITY_NONE;
1937 if (cflag & PARENB) {
1938 if (cflag & PARODD)
1939 info->params.parity = ASYNC_PARITY_ODD;
1940 else
1941 info->params.parity = ASYNC_PARITY_EVEN;
1942#ifdef CMSPAR
1943 if (cflag & CMSPAR)
1944 info->params.parity = ASYNC_PARITY_SPACE;
1945#endif
1946 }
1947
1948 /* calculate number of jiffies to transmit a full
1949 * FIFO (32 bytes) at specified data rate
1950 */
1951 bits_per_char = info->params.data_bits +
1952 info->params.stop_bits + 1;
1953
1954 /* if port data rate is set to 460800 or less then
1955 * allow tty settings to override, otherwise keep the
1956 * current data rate.
1957 */
1958 if (info->params.data_rate <= 460800)
Alan Cox8fb06c72008-07-16 21:56:46 +01001959 info->params.data_rate = tty_get_baud_rate(info->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 if ( info->params.data_rate ) {
1962 info->timeout = (32*HZ*bits_per_char) /
1963 info->params.data_rate;
1964 }
1965 info->timeout += HZ/50; /* Add .02 seconds of slop */
1966
Peter Hurley5604a982016-04-09 17:53:21 -07001967 tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
Peter Hurley2d686552016-04-09 17:53:23 -07001968 tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 /* process tty input control flags */
1971
1972 info->read_status_mask = RXSTATUS_OVERRUN;
Alan Cox8fb06c72008-07-16 21:56:46 +01001973 if (I_INPCK(info->port.tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 info->read_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
Alan Cox8fb06c72008-07-16 21:56:46 +01001975 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 info->read_status_mask |= RXSTATUS_BREAK_RECEIVED;
1977
Alan Cox8fb06c72008-07-16 21:56:46 +01001978 if (I_IGNPAR(info->port.tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 info->ignore_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
Alan Cox8fb06c72008-07-16 21:56:46 +01001980 if (I_IGNBRK(info->port.tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 info->ignore_status_mask |= RXSTATUS_BREAK_RECEIVED;
1982 /* If ignoring parity and break indicators, ignore
1983 * overruns too. (For real raw support).
1984 */
Alan Cox8fb06c72008-07-16 21:56:46 +01001985 if (I_IGNPAR(info->port.tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 info->ignore_status_mask |= RXSTATUS_OVERRUN;
1987 }
1988
1989 mgsl_program_hw(info);
1990
1991} /* end of mgsl_change_params() */
1992
1993/* mgsl_put_char()
1994 *
1995 * Add a character to the transmit buffer.
1996 *
1997 * Arguments: tty pointer to tty information structure
1998 * ch character to add to transmit buffer
1999 *
2000 * Return Value: None
2001 */
Alan Cox55da7782008-04-30 00:54:07 -07002002static int mgsl_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Andrew Morton07648232008-05-01 04:35:18 -07002004 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 unsigned long flags;
Andrew Morton07648232008-05-01 04:35:18 -07002006 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Andrew Morton07648232008-05-01 04:35:18 -07002008 if (debug_level >= DEBUG_LEVEL_INFO) {
Andrew Morton50980212008-05-01 04:35:19 -07002009 printk(KERN_DEBUG "%s(%d):mgsl_put_char(%d) on %s\n",
Andrew Morton07648232008-05-01 04:35:18 -07002010 __FILE__, __LINE__, ch, info->device_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
2013 if (mgsl_paranoia_check(info, tty->name, "mgsl_put_char"))
Alan Cox55da7782008-04-30 00:54:07 -07002014 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Jiri Slabyca1cce42010-01-10 12:30:16 +01002016 if (!info->xmit_buf)
Alan Cox55da7782008-04-30 00:54:07 -07002017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Andrew Morton07648232008-05-01 04:35:18 -07002019 spin_lock_irqsave(&info->irq_spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Andrew Morton07648232008-05-01 04:35:18 -07002021 if ((info->params.mode == MGSL_MODE_ASYNC ) || !info->tx_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (info->xmit_cnt < SERIAL_XMIT_SIZE - 1) {
2023 info->xmit_buf[info->xmit_head++] = ch;
2024 info->xmit_head &= SERIAL_XMIT_SIZE-1;
2025 info->xmit_cnt++;
Alan Cox55da7782008-04-30 00:54:07 -07002026 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028 }
Andrew Morton07648232008-05-01 04:35:18 -07002029 spin_unlock_irqrestore(&info->irq_spinlock, flags);
Alan Cox55da7782008-04-30 00:54:07 -07002030 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032} /* end of mgsl_put_char() */
2033
2034/* mgsl_flush_chars()
2035 *
2036 * Enable transmitter so remaining characters in the
2037 * transmit buffer are sent.
2038 *
2039 * Arguments: tty pointer to tty information structure
2040 * Return Value: None
2041 */
2042static void mgsl_flush_chars(struct tty_struct *tty)
2043{
Alan Coxc9f19e92009-01-02 13:47:26 +00002044 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 unsigned long flags;
2046
2047 if ( debug_level >= DEBUG_LEVEL_INFO )
2048 printk( "%s(%d):mgsl_flush_chars() entry on %s xmit_cnt=%d\n",
2049 __FILE__,__LINE__,info->device_name,info->xmit_cnt);
2050
2051 if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_chars"))
2052 return;
2053
2054 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
2055 !info->xmit_buf)
2056 return;
2057
2058 if ( debug_level >= DEBUG_LEVEL_INFO )
2059 printk( "%s(%d):mgsl_flush_chars() entry on %s starting transmitter\n",
2060 __FILE__,__LINE__,info->device_name );
2061
2062 spin_lock_irqsave(&info->irq_spinlock,flags);
2063
2064 if (!info->tx_active) {
2065 if ( (info->params.mode == MGSL_MODE_HDLC ||
2066 info->params.mode == MGSL_MODE_RAW) && info->xmit_cnt ) {
2067 /* operating in synchronous (frame oriented) mode */
2068 /* copy data from circular xmit_buf to */
2069 /* transmit DMA buffer. */
2070 mgsl_load_tx_dma_buffer(info,
2071 info->xmit_buf,info->xmit_cnt);
2072 }
2073 usc_start_transmitter(info);
2074 }
2075
2076 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2077
2078} /* end of mgsl_flush_chars() */
2079
2080/* mgsl_write()
2081 *
2082 * Send a block of data
2083 *
2084 * Arguments:
2085 *
2086 * tty pointer to tty information structure
2087 * buf pointer to buffer containing send data
2088 * count size of send data in bytes
2089 *
2090 * Return Value: number of characters written
2091 */
2092static int mgsl_write(struct tty_struct * tty,
2093 const unsigned char *buf, int count)
2094{
2095 int c, ret = 0;
Alan Coxc9f19e92009-01-02 13:47:26 +00002096 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 unsigned long flags;
2098
2099 if ( debug_level >= DEBUG_LEVEL_INFO )
2100 printk( "%s(%d):mgsl_write(%s) count=%d\n",
2101 __FILE__,__LINE__,info->device_name,count);
2102
2103 if (mgsl_paranoia_check(info, tty->name, "mgsl_write"))
2104 goto cleanup;
2105
Jiri Slabyca1cce42010-01-10 12:30:16 +01002106 if (!info->xmit_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 goto cleanup;
2108
2109 if ( info->params.mode == MGSL_MODE_HDLC ||
2110 info->params.mode == MGSL_MODE_RAW ) {
2111 /* operating in synchronous (frame oriented) mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (info->tx_active) {
2113
2114 if ( info->params.mode == MGSL_MODE_HDLC ) {
2115 ret = 0;
2116 goto cleanup;
2117 }
2118 /* transmitter is actively sending data -
2119 * if we have multiple transmit dma and
2120 * holding buffers, attempt to queue this
2121 * frame for transmission at a later time.
2122 */
2123 if (info->tx_holding_count >= info->num_tx_holding_buffers ) {
2124 /* no tx holding buffers available */
2125 ret = 0;
2126 goto cleanup;
2127 }
2128
2129 /* queue transmit frame request */
2130 ret = count;
2131 save_tx_buffer_request(info,buf,count);
2132
2133 /* if we have sufficient tx dma buffers,
2134 * load the next buffered tx request
2135 */
2136 spin_lock_irqsave(&info->irq_spinlock,flags);
2137 load_next_tx_holding_buffer(info);
2138 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2139 goto cleanup;
2140 }
2141
2142 /* if operating in HDLC LoopMode and the adapter */
2143 /* has yet to be inserted into the loop, we can't */
2144 /* transmit */
2145
2146 if ( (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) &&
2147 !usc_loopmode_active(info) )
2148 {
2149 ret = 0;
2150 goto cleanup;
2151 }
2152
2153 if ( info->xmit_cnt ) {
2154 /* Send accumulated from send_char() calls */
2155 /* as frame and wait before accepting more data. */
2156 ret = 0;
2157
2158 /* copy data from circular xmit_buf to */
2159 /* transmit DMA buffer. */
2160 mgsl_load_tx_dma_buffer(info,
2161 info->xmit_buf,info->xmit_cnt);
2162 if ( debug_level >= DEBUG_LEVEL_INFO )
2163 printk( "%s(%d):mgsl_write(%s) sync xmit_cnt flushing\n",
2164 __FILE__,__LINE__,info->device_name);
2165 } else {
2166 if ( debug_level >= DEBUG_LEVEL_INFO )
2167 printk( "%s(%d):mgsl_write(%s) sync transmit accepted\n",
2168 __FILE__,__LINE__,info->device_name);
2169 ret = count;
2170 info->xmit_cnt = count;
2171 mgsl_load_tx_dma_buffer(info,buf,count);
2172 }
2173 } else {
2174 while (1) {
2175 spin_lock_irqsave(&info->irq_spinlock,flags);
2176 c = min_t(int, count,
2177 min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
2178 SERIAL_XMIT_SIZE - info->xmit_head));
2179 if (c <= 0) {
2180 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2181 break;
2182 }
2183 memcpy(info->xmit_buf + info->xmit_head, buf, c);
2184 info->xmit_head = ((info->xmit_head + c) &
2185 (SERIAL_XMIT_SIZE-1));
2186 info->xmit_cnt += c;
2187 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2188 buf += c;
2189 count -= c;
2190 ret += c;
2191 }
2192 }
2193
2194 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
2195 spin_lock_irqsave(&info->irq_spinlock,flags);
2196 if (!info->tx_active)
2197 usc_start_transmitter(info);
2198 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2199 }
2200cleanup:
2201 if ( debug_level >= DEBUG_LEVEL_INFO )
2202 printk( "%s(%d):mgsl_write(%s) returning=%d\n",
2203 __FILE__,__LINE__,info->device_name,ret);
2204
2205 return ret;
2206
2207} /* end of mgsl_write() */
2208
2209/* mgsl_write_room()
2210 *
2211 * Return the count of free bytes in transmit buffer
2212 *
2213 * Arguments: tty pointer to tty info structure
2214 * Return Value: None
2215 */
2216static int mgsl_write_room(struct tty_struct *tty)
2217{
Alan Coxc9f19e92009-01-02 13:47:26 +00002218 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 int ret;
2220
2221 if (mgsl_paranoia_check(info, tty->name, "mgsl_write_room"))
2222 return 0;
2223 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
2224 if (ret < 0)
2225 ret = 0;
2226
2227 if (debug_level >= DEBUG_LEVEL_INFO)
2228 printk("%s(%d):mgsl_write_room(%s)=%d\n",
2229 __FILE__,__LINE__, info->device_name,ret );
2230
2231 if ( info->params.mode == MGSL_MODE_HDLC ||
2232 info->params.mode == MGSL_MODE_RAW ) {
2233 /* operating in synchronous (frame oriented) mode */
2234 if ( info->tx_active )
2235 return 0;
2236 else
2237 return HDLC_MAX_FRAME_SIZE;
2238 }
2239
2240 return ret;
2241
2242} /* end of mgsl_write_room() */
2243
2244/* mgsl_chars_in_buffer()
2245 *
2246 * Return the count of bytes in transmit buffer
2247 *
2248 * Arguments: tty pointer to tty info structure
2249 * Return Value: None
2250 */
2251static int mgsl_chars_in_buffer(struct tty_struct *tty)
2252{
Alan Coxc9f19e92009-01-02 13:47:26 +00002253 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 if (debug_level >= DEBUG_LEVEL_INFO)
2256 printk("%s(%d):mgsl_chars_in_buffer(%s)\n",
2257 __FILE__,__LINE__, info->device_name );
2258
2259 if (mgsl_paranoia_check(info, tty->name, "mgsl_chars_in_buffer"))
2260 return 0;
2261
2262 if (debug_level >= DEBUG_LEVEL_INFO)
2263 printk("%s(%d):mgsl_chars_in_buffer(%s)=%d\n",
2264 __FILE__,__LINE__, info->device_name,info->xmit_cnt );
2265
2266 if ( info->params.mode == MGSL_MODE_HDLC ||
2267 info->params.mode == MGSL_MODE_RAW ) {
2268 /* operating in synchronous (frame oriented) mode */
2269 if ( info->tx_active )
2270 return info->max_frame_size;
2271 else
2272 return 0;
2273 }
2274
2275 return info->xmit_cnt;
2276} /* end of mgsl_chars_in_buffer() */
2277
2278/* mgsl_flush_buffer()
2279 *
2280 * Discard all data in the send buffer
2281 *
2282 * Arguments: tty pointer to tty info structure
2283 * Return Value: None
2284 */
2285static void mgsl_flush_buffer(struct tty_struct *tty)
2286{
Alan Coxc9f19e92009-01-02 13:47:26 +00002287 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 unsigned long flags;
2289
2290 if (debug_level >= DEBUG_LEVEL_INFO)
2291 printk("%s(%d):mgsl_flush_buffer(%s) entry\n",
2292 __FILE__,__LINE__, info->device_name );
2293
2294 if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_buffer"))
2295 return;
2296
2297 spin_lock_irqsave(&info->irq_spinlock,flags);
2298 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2299 del_timer(&info->tx_timer);
2300 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 tty_wakeup(tty);
2303}
2304
2305/* mgsl_send_xchar()
2306 *
2307 * Send a high-priority XON/XOFF character
2308 *
2309 * Arguments: tty pointer to tty info structure
2310 * ch character to send
2311 * Return Value: None
2312 */
2313static void mgsl_send_xchar(struct tty_struct *tty, char ch)
2314{
Alan Coxc9f19e92009-01-02 13:47:26 +00002315 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 unsigned long flags;
2317
2318 if (debug_level >= DEBUG_LEVEL_INFO)
2319 printk("%s(%d):mgsl_send_xchar(%s,%d)\n",
2320 __FILE__,__LINE__, info->device_name, ch );
2321
2322 if (mgsl_paranoia_check(info, tty->name, "mgsl_send_xchar"))
2323 return;
2324
2325 info->x_char = ch;
2326 if (ch) {
2327 /* Make sure transmit interrupts are on */
2328 spin_lock_irqsave(&info->irq_spinlock,flags);
2329 if (!info->tx_enabled)
2330 usc_start_transmitter(info);
2331 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2332 }
2333} /* end of mgsl_send_xchar() */
2334
2335/* mgsl_throttle()
2336 *
2337 * Signal remote device to throttle send data (our receive data)
2338 *
2339 * Arguments: tty pointer to tty info structure
2340 * Return Value: None
2341 */
2342static void mgsl_throttle(struct tty_struct * tty)
2343{
Alan Coxc9f19e92009-01-02 13:47:26 +00002344 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 unsigned long flags;
2346
2347 if (debug_level >= DEBUG_LEVEL_INFO)
2348 printk("%s(%d):mgsl_throttle(%s) entry\n",
2349 __FILE__,__LINE__, info->device_name );
2350
2351 if (mgsl_paranoia_check(info, tty->name, "mgsl_throttle"))
2352 return;
2353
2354 if (I_IXOFF(tty))
2355 mgsl_send_xchar(tty, STOP_CHAR(tty));
Alan Coxadc8d742012-07-14 15:31:47 +01002356
Peter Hurley9db276f2016-01-10 20:36:15 -08002357 if (C_CRTSCTS(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 spin_lock_irqsave(&info->irq_spinlock,flags);
2359 info->serial_signals &= ~SerialSignal_RTS;
2360 usc_set_serial_signals(info);
2361 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2362 }
2363} /* end of mgsl_throttle() */
2364
2365/* mgsl_unthrottle()
2366 *
2367 * Signal remote device to stop throttling send data (our receive data)
2368 *
2369 * Arguments: tty pointer to tty info structure
2370 * Return Value: None
2371 */
2372static void mgsl_unthrottle(struct tty_struct * tty)
2373{
Alan Coxc9f19e92009-01-02 13:47:26 +00002374 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 unsigned long flags;
2376
2377 if (debug_level >= DEBUG_LEVEL_INFO)
2378 printk("%s(%d):mgsl_unthrottle(%s) entry\n",
2379 __FILE__,__LINE__, info->device_name );
2380
2381 if (mgsl_paranoia_check(info, tty->name, "mgsl_unthrottle"))
2382 return;
2383
2384 if (I_IXOFF(tty)) {
2385 if (info->x_char)
2386 info->x_char = 0;
2387 else
2388 mgsl_send_xchar(tty, START_CHAR(tty));
2389 }
Alan Coxadc8d742012-07-14 15:31:47 +01002390
Peter Hurley9db276f2016-01-10 20:36:15 -08002391 if (C_CRTSCTS(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 spin_lock_irqsave(&info->irq_spinlock,flags);
2393 info->serial_signals |= SerialSignal_RTS;
2394 usc_set_serial_signals(info);
2395 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2396 }
2397
2398} /* end of mgsl_unthrottle() */
2399
2400/* mgsl_get_stats()
2401 *
2402 * get the current serial parameters information
2403 *
2404 * Arguments: info pointer to device instance data
2405 * user_icount pointer to buffer to hold returned stats
2406 *
2407 * Return Value: 0 if success, otherwise error code
2408 */
2409static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount __user *user_icount)
2410{
2411 int err;
2412
2413 if (debug_level >= DEBUG_LEVEL_INFO)
2414 printk("%s(%d):mgsl_get_params(%s)\n",
2415 __FILE__,__LINE__, info->device_name);
2416
Paul Fulghum96612392005-09-09 13:02:13 -07002417 if (!user_icount) {
2418 memset(&info->icount, 0, sizeof(info->icount));
2419 } else {
Alan Coxf6025012010-06-01 22:52:46 +02002420 mutex_lock(&info->port.mutex);
Paul Fulghum96612392005-09-09 13:02:13 -07002421 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
Alan Coxf6025012010-06-01 22:52:46 +02002422 mutex_unlock(&info->port.mutex);
Paul Fulghum96612392005-09-09 13:02:13 -07002423 if (err)
2424 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
2426
2427 return 0;
2428
2429} /* end of mgsl_get_stats() */
2430
2431/* mgsl_get_params()
2432 *
2433 * get the current serial parameters information
2434 *
2435 * Arguments: info pointer to device instance data
2436 * user_params pointer to buffer to hold returned params
2437 *
2438 * Return Value: 0 if success, otherwise error code
2439 */
2440static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_params)
2441{
2442 int err;
2443 if (debug_level >= DEBUG_LEVEL_INFO)
2444 printk("%s(%d):mgsl_get_params(%s)\n",
2445 __FILE__,__LINE__, info->device_name);
2446
Alan Coxf6025012010-06-01 22:52:46 +02002447 mutex_lock(&info->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
Alan Coxf6025012010-06-01 22:52:46 +02002449 mutex_unlock(&info->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (err) {
2451 if ( debug_level >= DEBUG_LEVEL_INFO )
2452 printk( "%s(%d):mgsl_get_params(%s) user buffer copy failed\n",
2453 __FILE__,__LINE__,info->device_name);
2454 return -EFAULT;
2455 }
2456
2457 return 0;
2458
2459} /* end of mgsl_get_params() */
2460
2461/* mgsl_set_params()
2462 *
2463 * set the serial parameters
2464 *
2465 * Arguments:
2466 *
2467 * info pointer to device instance data
2468 * new_params user buffer containing new serial params
2469 *
2470 * Return Value: 0 if success, otherwise error code
2471 */
2472static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_params)
2473{
2474 unsigned long flags;
2475 MGSL_PARAMS tmp_params;
2476 int err;
2477
2478 if (debug_level >= DEBUG_LEVEL_INFO)
2479 printk("%s(%d):mgsl_set_params %s\n", __FILE__,__LINE__,
2480 info->device_name );
2481 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2482 if (err) {
2483 if ( debug_level >= DEBUG_LEVEL_INFO )
2484 printk( "%s(%d):mgsl_set_params(%s) user buffer copy failed\n",
2485 __FILE__,__LINE__,info->device_name);
2486 return -EFAULT;
2487 }
2488
Alan Coxf6025012010-06-01 22:52:46 +02002489 mutex_lock(&info->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 spin_lock_irqsave(&info->irq_spinlock,flags);
2491 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2492 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2493
2494 mgsl_change_params(info);
Alan Coxf6025012010-06-01 22:52:46 +02002495 mutex_unlock(&info->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 return 0;
2498
2499} /* end of mgsl_set_params() */
2500
2501/* mgsl_get_txidle()
2502 *
2503 * get the current transmit idle mode
2504 *
2505 * Arguments: info pointer to device instance data
2506 * idle_mode pointer to buffer to hold returned idle mode
2507 *
2508 * Return Value: 0 if success, otherwise error code
2509 */
2510static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode)
2511{
2512 int err;
2513
2514 if (debug_level >= DEBUG_LEVEL_INFO)
2515 printk("%s(%d):mgsl_get_txidle(%s)=%d\n",
2516 __FILE__,__LINE__, info->device_name, info->idle_mode);
2517
2518 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2519 if (err) {
2520 if ( debug_level >= DEBUG_LEVEL_INFO )
2521 printk( "%s(%d):mgsl_get_txidle(%s) user buffer copy failed\n",
2522 __FILE__,__LINE__,info->device_name);
2523 return -EFAULT;
2524 }
2525
2526 return 0;
2527
2528} /* end of mgsl_get_txidle() */
2529
2530/* mgsl_set_txidle() service ioctl to set transmit idle mode
2531 *
2532 * Arguments: info pointer to device instance data
2533 * idle_mode new idle mode
2534 *
2535 * Return Value: 0 if success, otherwise error code
2536 */
2537static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode)
2538{
2539 unsigned long flags;
2540
2541 if (debug_level >= DEBUG_LEVEL_INFO)
2542 printk("%s(%d):mgsl_set_txidle(%s,%d)\n", __FILE__,__LINE__,
2543 info->device_name, idle_mode );
2544
2545 spin_lock_irqsave(&info->irq_spinlock,flags);
2546 info->idle_mode = idle_mode;
2547 usc_set_txidle( info );
2548 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2549 return 0;
2550
2551} /* end of mgsl_set_txidle() */
2552
2553/* mgsl_txenable()
2554 *
2555 * enable or disable the transmitter
2556 *
2557 * Arguments:
2558 *
2559 * info pointer to device instance data
2560 * enable 1 = enable, 0 = disable
2561 *
2562 * Return Value: 0 if success, otherwise error code
2563 */
2564static int mgsl_txenable(struct mgsl_struct * info, int enable)
2565{
2566 unsigned long flags;
2567
2568 if (debug_level >= DEBUG_LEVEL_INFO)
2569 printk("%s(%d):mgsl_txenable(%s,%d)\n", __FILE__,__LINE__,
2570 info->device_name, enable);
2571
2572 spin_lock_irqsave(&info->irq_spinlock,flags);
2573 if ( enable ) {
2574 if ( !info->tx_enabled ) {
2575
2576 usc_start_transmitter(info);
2577 /*--------------------------------------------------
2578 * if HDLC/SDLC Loop mode, attempt to insert the
2579 * station in the 'loop' by setting CMR:13. Upon
2580 * receipt of the next GoAhead (RxAbort) sequence,
2581 * the OnLoop indicator (CCSR:7) should go active
2582 * to indicate that we are on the loop
2583 *--------------------------------------------------*/
2584 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
2585 usc_loopmode_insert_request( info );
2586 }
2587 } else {
2588 if ( info->tx_enabled )
2589 usc_stop_transmitter(info);
2590 }
2591 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2592 return 0;
2593
2594} /* end of mgsl_txenable() */
2595
2596/* mgsl_txabort() abort send HDLC frame
2597 *
2598 * Arguments: info pointer to device instance data
2599 * Return Value: 0 if success, otherwise error code
2600 */
2601static int mgsl_txabort(struct mgsl_struct * info)
2602{
2603 unsigned long flags;
2604
2605 if (debug_level >= DEBUG_LEVEL_INFO)
2606 printk("%s(%d):mgsl_txabort(%s)\n", __FILE__,__LINE__,
2607 info->device_name);
2608
2609 spin_lock_irqsave(&info->irq_spinlock,flags);
2610 if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC )
2611 {
2612 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
2613 usc_loopmode_cancel_transmit( info );
2614 else
2615 usc_TCmd(info,TCmd_SendAbort);
2616 }
2617 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2618 return 0;
2619
2620} /* end of mgsl_txabort() */
2621
2622/* mgsl_rxenable() enable or disable the receiver
2623 *
2624 * Arguments: info pointer to device instance data
2625 * enable 1 = enable, 0 = disable
2626 * Return Value: 0 if success, otherwise error code
2627 */
2628static int mgsl_rxenable(struct mgsl_struct * info, int enable)
2629{
2630 unsigned long flags;
2631
2632 if (debug_level >= DEBUG_LEVEL_INFO)
2633 printk("%s(%d):mgsl_rxenable(%s,%d)\n", __FILE__,__LINE__,
2634 info->device_name, enable);
2635
2636 spin_lock_irqsave(&info->irq_spinlock,flags);
2637 if ( enable ) {
2638 if ( !info->rx_enabled )
2639 usc_start_receiver(info);
2640 } else {
2641 if ( info->rx_enabled )
2642 usc_stop_receiver(info);
2643 }
2644 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2645 return 0;
2646
2647} /* end of mgsl_rxenable() */
2648
2649/* mgsl_wait_event() wait for specified event to occur
2650 *
2651 * Arguments: info pointer to device instance data
2652 * mask pointer to bitmask of events to wait for
2653 * Return Value: 0 if successful and bit mask updated with
2654 * of events triggerred,
2655 * otherwise error code
2656 */
2657static int mgsl_wait_event(struct mgsl_struct * info, int __user * mask_ptr)
2658{
2659 unsigned long flags;
2660 int s;
2661 int rc=0;
2662 struct mgsl_icount cprev, cnow;
2663 int events;
2664 int mask;
2665 struct _input_signal_events oldsigs, newsigs;
2666 DECLARE_WAITQUEUE(wait, current);
2667
2668 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2669 if (rc) {
2670 return -EFAULT;
2671 }
2672
2673 if (debug_level >= DEBUG_LEVEL_INFO)
2674 printk("%s(%d):mgsl_wait_event(%s,%d)\n", __FILE__,__LINE__,
2675 info->device_name, mask);
2676
2677 spin_lock_irqsave(&info->irq_spinlock,flags);
2678
2679 /* return immediately if state matches requested events */
2680 usc_get_serial_signals(info);
2681 s = info->serial_signals;
2682 events = mask &
2683 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2684 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2685 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2686 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2687 if (events) {
2688 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2689 goto exit;
2690 }
2691
2692 /* save current irq counts */
2693 cprev = info->icount;
2694 oldsigs = info->input_signal_events;
2695
2696 /* enable hunt and idle irqs if needed */
2697 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2698 u16 oldreg = usc_InReg(info,RICR);
2699 u16 newreg = oldreg +
2700 (mask & MgslEvent_ExitHuntMode ? RXSTATUS_EXITED_HUNT:0) +
2701 (mask & MgslEvent_IdleReceived ? RXSTATUS_IDLE_RECEIVED:0);
2702 if (oldreg != newreg)
2703 usc_OutReg(info, RICR, newreg);
2704 }
2705
2706 set_current_state(TASK_INTERRUPTIBLE);
2707 add_wait_queue(&info->event_wait_q, &wait);
2708
2709 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2710
2711
2712 for(;;) {
2713 schedule();
2714 if (signal_pending(current)) {
2715 rc = -ERESTARTSYS;
2716 break;
2717 }
2718
2719 /* get current irq counts */
2720 spin_lock_irqsave(&info->irq_spinlock,flags);
2721 cnow = info->icount;
2722 newsigs = info->input_signal_events;
2723 set_current_state(TASK_INTERRUPTIBLE);
2724 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2725
2726 /* if no change, wait aborted for some reason */
2727 if (newsigs.dsr_up == oldsigs.dsr_up &&
2728 newsigs.dsr_down == oldsigs.dsr_down &&
2729 newsigs.dcd_up == oldsigs.dcd_up &&
2730 newsigs.dcd_down == oldsigs.dcd_down &&
2731 newsigs.cts_up == oldsigs.cts_up &&
2732 newsigs.cts_down == oldsigs.cts_down &&
2733 newsigs.ri_up == oldsigs.ri_up &&
2734 newsigs.ri_down == oldsigs.ri_down &&
2735 cnow.exithunt == cprev.exithunt &&
2736 cnow.rxidle == cprev.rxidle) {
2737 rc = -EIO;
2738 break;
2739 }
2740
2741 events = mask &
2742 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2743 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2744 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2745 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2746 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2747 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2748 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2749 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2750 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2751 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2752 if (events)
2753 break;
2754
2755 cprev = cnow;
2756 oldsigs = newsigs;
2757 }
2758
2759 remove_wait_queue(&info->event_wait_q, &wait);
2760 set_current_state(TASK_RUNNING);
2761
2762 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2763 spin_lock_irqsave(&info->irq_spinlock,flags);
2764 if (!waitqueue_active(&info->event_wait_q)) {
2765 /* disable enable exit hunt mode/idle rcvd IRQs */
2766 usc_OutReg(info, RICR, usc_InReg(info,RICR) &
Alexandru Juncue06922a2013-07-27 11:14:39 +03002767 ~(RXSTATUS_EXITED_HUNT | RXSTATUS_IDLE_RECEIVED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
2769 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2770 }
2771exit:
2772 if ( rc == 0 )
2773 PUT_USER(rc, events, mask_ptr);
2774
2775 return rc;
2776
2777} /* end of mgsl_wait_event() */
2778
2779static int modem_input_wait(struct mgsl_struct *info,int arg)
2780{
2781 unsigned long flags;
2782 int rc;
2783 struct mgsl_icount cprev, cnow;
2784 DECLARE_WAITQUEUE(wait, current);
2785
2786 /* save current irq counts */
2787 spin_lock_irqsave(&info->irq_spinlock,flags);
2788 cprev = info->icount;
2789 add_wait_queue(&info->status_event_wait_q, &wait);
2790 set_current_state(TASK_INTERRUPTIBLE);
2791 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2792
2793 for(;;) {
2794 schedule();
2795 if (signal_pending(current)) {
2796 rc = -ERESTARTSYS;
2797 break;
2798 }
2799
2800 /* get new irq counts */
2801 spin_lock_irqsave(&info->irq_spinlock,flags);
2802 cnow = info->icount;
2803 set_current_state(TASK_INTERRUPTIBLE);
2804 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2805
2806 /* if no change, wait aborted for some reason */
2807 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2808 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2809 rc = -EIO;
2810 break;
2811 }
2812
2813 /* check for change in caller specified modem input */
2814 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2815 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2816 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2817 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2818 rc = 0;
2819 break;
2820 }
2821
2822 cprev = cnow;
2823 }
2824 remove_wait_queue(&info->status_event_wait_q, &wait);
2825 set_current_state(TASK_RUNNING);
2826 return rc;
2827}
2828
2829/* return the state of the serial control and status signals
2830 */
Alan Cox60b33c12011-02-14 16:26:14 +00002831static int tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Alan Coxc9f19e92009-01-02 13:47:26 +00002833 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 unsigned int result;
2835 unsigned long flags;
2836
2837 spin_lock_irqsave(&info->irq_spinlock,flags);
2838 usc_get_serial_signals(info);
2839 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2840
2841 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2842 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2843 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2844 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2845 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2846 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2847
2848 if (debug_level >= DEBUG_LEVEL_INFO)
2849 printk("%s(%d):%s tiocmget() value=%08X\n",
2850 __FILE__,__LINE__, info->device_name, result );
2851 return result;
2852}
2853
2854/* set modem control signals (DTR/RTS)
2855 */
Alan Cox20b9d172011-02-14 16:26:50 +00002856static int tiocmset(struct tty_struct *tty,
2857 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Alan Coxc9f19e92009-01-02 13:47:26 +00002859 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 unsigned long flags;
2861
2862 if (debug_level >= DEBUG_LEVEL_INFO)
2863 printk("%s(%d):%s tiocmset(%x,%x)\n",
2864 __FILE__,__LINE__,info->device_name, set, clear);
2865
2866 if (set & TIOCM_RTS)
2867 info->serial_signals |= SerialSignal_RTS;
2868 if (set & TIOCM_DTR)
2869 info->serial_signals |= SerialSignal_DTR;
2870 if (clear & TIOCM_RTS)
2871 info->serial_signals &= ~SerialSignal_RTS;
2872 if (clear & TIOCM_DTR)
2873 info->serial_signals &= ~SerialSignal_DTR;
2874
2875 spin_lock_irqsave(&info->irq_spinlock,flags);
2876 usc_set_serial_signals(info);
2877 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2878
2879 return 0;
2880}
2881
2882/* mgsl_break() Set or clear transmit break condition
2883 *
2884 * Arguments: tty pointer to tty instance data
2885 * break_state -1=set break condition, 0=clear
Alan Cox9e989662008-07-22 11:18:03 +01002886 * Return Value: error code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 */
Alan Cox9e989662008-07-22 11:18:03 +01002888static int mgsl_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
Alan Coxc9f19e92009-01-02 13:47:26 +00002890 struct mgsl_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 unsigned long flags;
2892
2893 if (debug_level >= DEBUG_LEVEL_INFO)
2894 printk("%s(%d):mgsl_break(%s,%d)\n",
2895 __FILE__,__LINE__, info->device_name, break_state);
2896
2897 if (mgsl_paranoia_check(info, tty->name, "mgsl_break"))
Alan Cox9e989662008-07-22 11:18:03 +01002898 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
2900 spin_lock_irqsave(&info->irq_spinlock,flags);
2901 if (break_state == -1)
2902 usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) | BIT7));
2903 else
2904 usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) & ~BIT7));
2905 spin_unlock_irqrestore(&info->irq_spinlock,flags);
Alan Cox9e989662008-07-22 11:18:03 +01002906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908} /* end of mgsl_break() */
2909
Alan Cox05871022010-09-16 18:21:52 +01002910/*
2911 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2912 * Return: write counters to the user passed counter struct
2913 * NB: both 1->0 and 0->1 transitions are counted except for
2914 * RI where only 0->1 is counted.
2915 */
2916static int msgl_get_icount(struct tty_struct *tty,
2917 struct serial_icounter_struct *icount)
2918
2919{
2920 struct mgsl_struct * info = tty->driver_data;
2921 struct mgsl_icount cnow; /* kernel counter temps */
2922 unsigned long flags;
2923
2924 spin_lock_irqsave(&info->irq_spinlock,flags);
2925 cnow = info->icount;
2926 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2927
2928 icount->cts = cnow.cts;
2929 icount->dsr = cnow.dsr;
2930 icount->rng = cnow.rng;
2931 icount->dcd = cnow.dcd;
2932 icount->rx = cnow.rx;
2933 icount->tx = cnow.tx;
2934 icount->frame = cnow.frame;
2935 icount->overrun = cnow.overrun;
2936 icount->parity = cnow.parity;
2937 icount->brk = cnow.brk;
2938 icount->buf_overrun = cnow.buf_overrun;
2939 return 0;
2940}
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942/* mgsl_ioctl() Service an IOCTL request
2943 *
2944 * Arguments:
2945 *
2946 * tty pointer to tty instance data
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 * cmd IOCTL command code
2948 * arg command argument/context
2949 *
2950 * Return Value: 0 if success, otherwise error code
2951 */
Alan Cox6caa76b2011-02-14 16:27:22 +00002952static int mgsl_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 unsigned int cmd, unsigned long arg)
2954{
Alan Coxc9f19e92009-01-02 13:47:26 +00002955 struct mgsl_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957 if (debug_level >= DEBUG_LEVEL_INFO)
2958 printk("%s(%d):mgsl_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2959 info->device_name, cmd );
2960
2961 if (mgsl_paranoia_check(info, tty->name, "mgsl_ioctl"))
2962 return -ENODEV;
2963
2964 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
Alan Cox05871022010-09-16 18:21:52 +01002965 (cmd != TIOCMIWAIT)) {
Peter Hurley18900ca2016-04-09 17:06:48 -07002966 if (tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return -EIO;
2968 }
2969
Alan Coxf6025012010-06-01 22:52:46 +02002970 return mgsl_ioctl_common(info, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
2973static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg)
2974{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 switch (cmd) {
2978 case MGSL_IOCGPARAMS:
2979 return mgsl_get_params(info, argp);
2980 case MGSL_IOCSPARAMS:
2981 return mgsl_set_params(info, argp);
2982 case MGSL_IOCGTXIDLE:
2983 return mgsl_get_txidle(info, argp);
2984 case MGSL_IOCSTXIDLE:
2985 return mgsl_set_txidle(info,(int)arg);
2986 case MGSL_IOCTXENABLE:
2987 return mgsl_txenable(info,(int)arg);
2988 case MGSL_IOCRXENABLE:
2989 return mgsl_rxenable(info,(int)arg);
2990 case MGSL_IOCTXABORT:
2991 return mgsl_txabort(info);
2992 case MGSL_IOCGSTATS:
2993 return mgsl_get_stats(info, argp);
2994 case MGSL_IOCWAITEVENT:
2995 return mgsl_wait_event(info, argp);
2996 case MGSL_IOCLOOPTXDONE:
2997 return mgsl_loopmode_send_done(info);
2998 /* Wait for modem input (DCD,RI,DSR,CTS) change
2999 * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
3000 */
3001 case TIOCMIWAIT:
3002 return modem_input_wait(info,(int)arg);
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 default:
3005 return -ENOIOCTLCMD;
3006 }
3007 return 0;
3008}
3009
3010/* mgsl_set_termios()
3011 *
3012 * Set new termios settings
3013 *
3014 * Arguments:
3015 *
3016 * tty pointer to tty structure
3017 * termios pointer to buffer to hold returned old termios
3018 *
3019 * Return Value: None
3020 */
Alan Cox606d0992006-12-08 02:38:45 -08003021static void mgsl_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Alan Coxc9f19e92009-01-02 13:47:26 +00003023 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 unsigned long flags;
3025
3026 if (debug_level >= DEBUG_LEVEL_INFO)
3027 printk("%s(%d):mgsl_set_termios %s\n", __FILE__,__LINE__,
3028 tty->driver->name );
3029
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 mgsl_change_params(info);
3031
3032 /* Handle transition to B0 status */
Peter Hurley9db276f2016-01-10 20:36:15 -08003033 if ((old_termios->c_cflag & CBAUD) && !C_BAUD(tty)) {
Joe Perches9fe80742013-01-27 18:21:00 -08003034 info->serial_signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 spin_lock_irqsave(&info->irq_spinlock,flags);
3036 usc_set_serial_signals(info);
3037 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3038 }
Peter Hurley9db276f2016-01-10 20:36:15 -08003039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 /* Handle transition away from B0 status */
Peter Hurley9db276f2016-01-10 20:36:15 -08003041 if (!(old_termios->c_cflag & CBAUD) && C_BAUD(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 info->serial_signals |= SerialSignal_DTR;
Peter Hurley97ef38b2016-04-09 17:11:36 -07003043 if (!C_CRTSCTS(tty) || !tty_throttled(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 info->serial_signals |= SerialSignal_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 spin_lock_irqsave(&info->irq_spinlock,flags);
3046 usc_set_serial_signals(info);
3047 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3048 }
Peter Hurley9db276f2016-01-10 20:36:15 -08003049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 /* Handle turning off CRTSCTS */
Peter Hurley9db276f2016-01-10 20:36:15 -08003051 if (old_termios->c_cflag & CRTSCTS && !C_CRTSCTS(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 tty->hw_stopped = 0;
3053 mgsl_start(tty);
3054 }
3055
3056} /* end of mgsl_set_termios() */
3057
3058/* mgsl_close()
3059 *
3060 * Called when port is closed. Wait for remaining data to be
3061 * sent. Disable port and free resources.
3062 *
3063 * Arguments:
3064 *
3065 * tty pointer to open tty structure
3066 * filp pointer to open file object
3067 *
3068 * Return Value: None
3069 */
3070static void mgsl_close(struct tty_struct *tty, struct file * filp)
3071{
Alan Coxc9f19e92009-01-02 13:47:26 +00003072 struct mgsl_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074 if (mgsl_paranoia_check(info, tty->name, "mgsl_close"))
3075 return;
3076
3077 if (debug_level >= DEBUG_LEVEL_INFO)
3078 printk("%s(%d):mgsl_close(%s) entry, count=%d\n",
Alan Cox8fb06c72008-07-16 21:56:46 +01003079 __FILE__,__LINE__, info->device_name, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Alexandru Juncue06922a2013-07-27 11:14:39 +03003081 if (tty_port_close_start(&info->port, tty, filp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 goto cleanup;
Alan Coxf6025012010-06-01 22:52:46 +02003083
3084 mutex_lock(&info->port.mutex);
Peter Hurleyd41861c2016-04-09 17:53:25 -07003085 if (tty_port_initialized(&info->port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 mgsl_wait_until_sent(tty, info->timeout);
Alan Cox978e5952008-04-30 00:53:59 -07003087 mgsl_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 shutdown(info);
Alan Coxf6025012010-06-01 22:52:46 +02003090 mutex_unlock(&info->port.mutex);
Alan Coxa6614992009-01-02 13:46:50 +00003091
3092 tty_port_close_end(&info->port, tty);
Alan Cox8fb06c72008-07-16 21:56:46 +01003093 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094cleanup:
3095 if (debug_level >= DEBUG_LEVEL_INFO)
3096 printk("%s(%d):mgsl_close(%s) exit, count=%d\n", __FILE__,__LINE__,
Alan Cox8fb06c72008-07-16 21:56:46 +01003097 tty->driver->name, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099} /* end of mgsl_close() */
3100
3101/* mgsl_wait_until_sent()
3102 *
3103 * Wait until the transmitter is empty.
3104 *
3105 * Arguments:
3106 *
3107 * tty pointer to tty info structure
3108 * timeout time to wait for send completion
3109 *
3110 * Return Value: None
3111 */
3112static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout)
3113{
Alan Coxc9f19e92009-01-02 13:47:26 +00003114 struct mgsl_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 unsigned long orig_jiffies, char_time;
3116
3117 if (!info )
3118 return;
3119
3120 if (debug_level >= DEBUG_LEVEL_INFO)
3121 printk("%s(%d):mgsl_wait_until_sent(%s) entry\n",
3122 __FILE__,__LINE__, info->device_name );
Peter Hurleyd41861c2016-04-09 17:53:25 -07003123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if (mgsl_paranoia_check(info, tty->name, "mgsl_wait_until_sent"))
3125 return;
3126
Peter Hurleyd41861c2016-04-09 17:53:25 -07003127 if (!tty_port_initialized(&info->port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 goto exit;
Peter Hurleyd41861c2016-04-09 17:53:25 -07003129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 orig_jiffies = jiffies;
Peter Hurleyd41861c2016-04-09 17:53:25 -07003131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 /* Set check interval to 1/5 of estimated time to
3133 * send a character, and make it at least 1. The check
3134 * interval should also be less than the timeout.
3135 * Note: use tight timings here to satisfy the NIST-PCTS.
3136 */
Alan Cox978e5952008-04-30 00:53:59 -07003137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 if ( info->params.data_rate ) {
3139 char_time = info->timeout/(32 * 5);
3140 if (!char_time)
3141 char_time++;
3142 } else
3143 char_time = 1;
3144
3145 if (timeout)
3146 char_time = min_t(unsigned long, char_time, timeout);
3147
3148 if ( info->params.mode == MGSL_MODE_HDLC ||
3149 info->params.mode == MGSL_MODE_RAW ) {
3150 while (info->tx_active) {
3151 msleep_interruptible(jiffies_to_msecs(char_time));
3152 if (signal_pending(current))
3153 break;
3154 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3155 break;
3156 }
3157 } else {
3158 while (!(usc_InReg(info,TCSR) & TXSTATUS_ALL_SENT) &&
3159 info->tx_enabled) {
3160 msleep_interruptible(jiffies_to_msecs(char_time));
3161 if (signal_pending(current))
3162 break;
3163 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3164 break;
3165 }
3166 }
3167
3168exit:
3169 if (debug_level >= DEBUG_LEVEL_INFO)
3170 printk("%s(%d):mgsl_wait_until_sent(%s) exit\n",
3171 __FILE__,__LINE__, info->device_name );
3172
3173} /* end of mgsl_wait_until_sent() */
3174
3175/* mgsl_hangup()
3176 *
3177 * Called by tty_hangup() when a hangup is signaled.
3178 * This is the same as to closing all open files for the port.
3179 *
3180 * Arguments: tty pointer to associated tty object
3181 * Return Value: None
3182 */
3183static void mgsl_hangup(struct tty_struct *tty)
3184{
Alan Coxc9f19e92009-01-02 13:47:26 +00003185 struct mgsl_struct * info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 if (debug_level >= DEBUG_LEVEL_INFO)
3188 printk("%s(%d):mgsl_hangup(%s)\n",
3189 __FILE__,__LINE__, info->device_name );
3190
3191 if (mgsl_paranoia_check(info, tty->name, "mgsl_hangup"))
3192 return;
3193
3194 mgsl_flush_buffer(tty);
3195 shutdown(info);
3196
Alan Cox8fb06c72008-07-16 21:56:46 +01003197 info->port.count = 0;
Peter Hurley807c8d812016-04-09 17:53:22 -07003198 tty_port_set_active(&info->port, 0);
Alan Cox8fb06c72008-07-16 21:56:46 +01003199 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Alan Cox8fb06c72008-07-16 21:56:46 +01003201 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203} /* end of mgsl_hangup() */
3204
Alan Cox31f35932009-01-02 13:45:05 +00003205/*
3206 * carrier_raised()
3207 *
3208 * Return true if carrier is raised
3209 */
3210
3211static int carrier_raised(struct tty_port *port)
3212{
3213 unsigned long flags;
3214 struct mgsl_struct *info = container_of(port, struct mgsl_struct, port);
3215
3216 spin_lock_irqsave(&info->irq_spinlock, flags);
3217 usc_get_serial_signals(info);
3218 spin_unlock_irqrestore(&info->irq_spinlock, flags);
3219 return (info->serial_signals & SerialSignal_DCD) ? 1 : 0;
3220}
3221
Alan Coxfcc8ac12009-06-11 12:24:17 +01003222static void dtr_rts(struct tty_port *port, int on)
Alan Cox5d951fb2009-01-02 13:45:19 +00003223{
3224 struct mgsl_struct *info = container_of(port, struct mgsl_struct, port);
3225 unsigned long flags;
3226
3227 spin_lock_irqsave(&info->irq_spinlock,flags);
Alan Coxfcc8ac12009-06-11 12:24:17 +01003228 if (on)
Joe Perches9fe80742013-01-27 18:21:00 -08003229 info->serial_signals |= SerialSignal_RTS | SerialSignal_DTR;
Alan Coxfcc8ac12009-06-11 12:24:17 +01003230 else
Joe Perches9fe80742013-01-27 18:21:00 -08003231 info->serial_signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
Alan Cox5d951fb2009-01-02 13:45:19 +00003232 usc_set_serial_signals(info);
3233 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3234}
3235
3236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237/* block_til_ready()
3238 *
3239 * Block the current process until the specified port
3240 * is ready to be opened.
3241 *
3242 * Arguments:
3243 *
3244 * tty pointer to tty info structure
3245 * filp pointer to open file object
3246 * info pointer to device instance data
3247 *
3248 * Return Value: 0 if success, otherwise error code
3249 */
3250static int block_til_ready(struct tty_struct *tty, struct file * filp,
3251 struct mgsl_struct *info)
3252{
3253 DECLARE_WAITQUEUE(wait, current);
3254 int retval;
Joe Perches0fab6de2008-04-28 02:14:02 -07003255 bool do_clocal = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 unsigned long flags;
Alan Cox31f35932009-01-02 13:45:05 +00003257 int dcd;
3258 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
3260 if (debug_level >= DEBUG_LEVEL_INFO)
3261 printk("%s(%d):block_til_ready on %s\n",
3262 __FILE__,__LINE__, tty->driver->name );
3263
Peter Hurley18900ca2016-04-09 17:06:48 -07003264 if (filp->f_flags & O_NONBLOCK || tty_io_error(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 /* nonblock mode is set or port is not enabled */
Peter Hurley807c8d812016-04-09 17:53:22 -07003266 tty_port_set_active(port, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 return 0;
3268 }
3269
Peter Hurley9db276f2016-01-10 20:36:15 -08003270 if (C_CLOCAL(tty))
Joe Perches0fab6de2008-04-28 02:14:02 -07003271 do_clocal = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 /* Wait for carrier detect and the line to become
3274 * free (i.e., not in use by the callout). While we are in
Alan Cox31f35932009-01-02 13:45:05 +00003275 * this loop, port->count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 * mgsl_close() knows when to free things. We restore it upon
3277 * exit, either normal or abnormal.
3278 */
3279
3280 retval = 0;
Alan Cox31f35932009-01-02 13:45:05 +00003281 add_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283 if (debug_level >= DEBUG_LEVEL_INFO)
3284 printk("%s(%d):block_til_ready before block on %s count=%d\n",
Alan Cox31f35932009-01-02 13:45:05 +00003285 __FILE__,__LINE__, tty->driver->name, port->count );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
3287 spin_lock_irqsave(&info->irq_spinlock, flags);
Peter Hurleye359a4e2014-06-16 09:17:06 -04003288 port->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 spin_unlock_irqrestore(&info->irq_spinlock, flags);
Alan Cox31f35932009-01-02 13:45:05 +00003290 port->blocked_open++;
Peter Hurleyd41861c2016-04-09 17:53:25 -07003291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 while (1) {
Peter Hurleyd41861c2016-04-09 17:53:25 -07003293 if (C_BAUD(tty) && tty_port_initialized(port))
Alan Cox5d951fb2009-01-02 13:45:19 +00003294 tty_port_raise_dtr_rts(port);
Peter Hurleyd41861c2016-04-09 17:53:25 -07003295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 set_current_state(TASK_INTERRUPTIBLE);
Peter Hurleyd41861c2016-04-09 17:53:25 -07003297
3298 if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
Alan Cox31f35932009-01-02 13:45:05 +00003299 retval = (port->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 -EAGAIN : -ERESTARTSYS;
3301 break;
3302 }
Peter Hurleyfef062c2015-10-10 16:00:52 -04003303
Alan Cox31f35932009-01-02 13:45:05 +00003304 dcd = tty_port_carrier_raised(&info->port);
Peter Hurleyfef062c2015-10-10 16:00:52 -04003305 if (do_clocal || dcd)
3306 break;
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 if (signal_pending(current)) {
3309 retval = -ERESTARTSYS;
3310 break;
3311 }
3312
3313 if (debug_level >= DEBUG_LEVEL_INFO)
3314 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
Alan Cox31f35932009-01-02 13:45:05 +00003315 __FILE__,__LINE__, tty->driver->name, port->count );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Alan Cox89c8d912012-08-08 16:30:13 +01003317 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 schedule();
Alan Cox89c8d912012-08-08 16:30:13 +01003319 tty_lock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321
3322 set_current_state(TASK_RUNNING);
Alan Cox31f35932009-01-02 13:45:05 +00003323 remove_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Alan Cox36c621d2009-01-02 13:46:10 +00003325 /* FIXME: Racy on hangup during close wait */
Peter Hurleye359a4e2014-06-16 09:17:06 -04003326 if (!tty_hung_up_p(filp))
Alan Cox31f35932009-01-02 13:45:05 +00003327 port->count++;
3328 port->blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
3330 if (debug_level >= DEBUG_LEVEL_INFO)
3331 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
Alan Cox31f35932009-01-02 13:45:05 +00003332 __FILE__,__LINE__, tty->driver->name, port->count );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
3334 if (!retval)
Peter Hurley807c8d812016-04-09 17:53:22 -07003335 tty_port_set_active(port, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337 return retval;
3338
3339} /* end of block_til_ready() */
3340
Jiri Slaby8a3ad102012-08-07 21:48:00 +02003341static int mgsl_install(struct tty_driver *driver, struct tty_struct *tty)
3342{
3343 struct mgsl_struct *info;
3344 int line = tty->index;
3345
3346 /* verify range of specified line number */
3347 if (line >= mgsl_device_count) {
3348 printk("%s(%d):mgsl_open with invalid line #%d.\n",
3349 __FILE__, __LINE__, line);
3350 return -ENODEV;
3351 }
3352
3353 /* find the info structure for the specified line */
3354 info = mgsl_device_list;
3355 while (info && info->line != line)
3356 info = info->next_device;
3357 if (mgsl_paranoia_check(info, tty->name, "mgsl_open"))
3358 return -ENODEV;
3359 tty->driver_data = info;
3360
3361 return tty_port_install(&info->port, driver, tty);
3362}
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364/* mgsl_open()
3365 *
3366 * Called when a port is opened. Init and enable port.
3367 * Perform serial-specific initialization for the tty structure.
3368 *
3369 * Arguments: tty pointer to tty info structure
3370 * filp associated file pointer
3371 *
3372 * Return Value: 0 if success, otherwise error code
3373 */
3374static int mgsl_open(struct tty_struct *tty, struct file * filp)
3375{
Jiri Slaby8a3ad102012-08-07 21:48:00 +02003376 struct mgsl_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 unsigned long flags;
Jiri Slaby8a3ad102012-08-07 21:48:00 +02003378 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Alan Cox8fb06c72008-07-16 21:56:46 +01003380 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382 if (debug_level >= DEBUG_LEVEL_INFO)
3383 printk("%s(%d):mgsl_open(%s), old ref count = %d\n",
Alan Cox8fb06c72008-07-16 21:56:46 +01003384 __FILE__,__LINE__,tty->driver->name, info->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Jiri Slabyd6c53c02013-01-03 15:53:05 +01003386 info->port.low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
3388 spin_lock_irqsave(&info->netlock, flags);
3389 if (info->netcount) {
3390 retval = -EBUSY;
3391 spin_unlock_irqrestore(&info->netlock, flags);
3392 goto cleanup;
3393 }
Alan Cox8fb06c72008-07-16 21:56:46 +01003394 info->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 spin_unlock_irqrestore(&info->netlock, flags);
3396
Alan Cox8fb06c72008-07-16 21:56:46 +01003397 if (info->port.count == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 /* 1st open on this device, init hardware */
3399 retval = startup(info);
3400 if (retval < 0)
3401 goto cleanup;
3402 }
3403
3404 retval = block_til_ready(tty, filp, info);
3405 if (retval) {
3406 if (debug_level >= DEBUG_LEVEL_INFO)
3407 printk("%s(%d):block_til_ready(%s) returned %d\n",
3408 __FILE__,__LINE__, info->device_name, retval);
3409 goto cleanup;
3410 }
3411
3412 if (debug_level >= DEBUG_LEVEL_INFO)
3413 printk("%s(%d):mgsl_open(%s) success\n",
3414 __FILE__,__LINE__, info->device_name);
3415 retval = 0;
3416
3417cleanup:
3418 if (retval) {
3419 if (tty->count == 1)
Alan Cox8fb06c72008-07-16 21:56:46 +01003420 info->port.tty = NULL; /* tty layer will release tty struct */
3421 if(info->port.count)
3422 info->port.count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
3424
3425 return retval;
3426
3427} /* end of mgsl_open() */
3428
3429/*
3430 * /proc fs routines....
3431 */
3432
Alexey Dobriyand3378292009-03-31 15:19:18 -07003433static inline void line_info(struct seq_file *m, struct mgsl_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
3435 char stat_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 unsigned long flags;
3437
3438 if (info->bus_type == MGSL_BUS_TYPE_PCI) {
Alexey Dobriyand3378292009-03-31 15:19:18 -07003439 seq_printf(m, "%s:PCI io:%04X irq:%d mem:%08X lcr:%08X",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 info->device_name, info->io_base, info->irq_level,
3441 info->phys_memory_base, info->phys_lcr_base);
3442 } else {
Alexey Dobriyand3378292009-03-31 15:19:18 -07003443 seq_printf(m, "%s:(E)ISA io:%04X irq:%d dma:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 info->device_name, info->io_base,
3445 info->irq_level, info->dma_level);
3446 }
3447
3448 /* output current serial signal states */
3449 spin_lock_irqsave(&info->irq_spinlock,flags);
3450 usc_get_serial_signals(info);
3451 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3452
3453 stat_buf[0] = 0;
3454 stat_buf[1] = 0;
3455 if (info->serial_signals & SerialSignal_RTS)
3456 strcat(stat_buf, "|RTS");
3457 if (info->serial_signals & SerialSignal_CTS)
3458 strcat(stat_buf, "|CTS");
3459 if (info->serial_signals & SerialSignal_DTR)
3460 strcat(stat_buf, "|DTR");
3461 if (info->serial_signals & SerialSignal_DSR)
3462 strcat(stat_buf, "|DSR");
3463 if (info->serial_signals & SerialSignal_DCD)
3464 strcat(stat_buf, "|CD");
3465 if (info->serial_signals & SerialSignal_RI)
3466 strcat(stat_buf, "|RI");
3467
3468 if (info->params.mode == MGSL_MODE_HDLC ||
3469 info->params.mode == MGSL_MODE_RAW ) {
Alexey Dobriyand3378292009-03-31 15:19:18 -07003470 seq_printf(m, " HDLC txok:%d rxok:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 info->icount.txok, info->icount.rxok);
3472 if (info->icount.txunder)
Alexey Dobriyand3378292009-03-31 15:19:18 -07003473 seq_printf(m, " txunder:%d", info->icount.txunder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 if (info->icount.txabort)
Alexey Dobriyand3378292009-03-31 15:19:18 -07003475 seq_printf(m, " txabort:%d", info->icount.txabort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 if (info->icount.rxshort)
Alexey Dobriyand3378292009-03-31 15:19:18 -07003477 seq_printf(m, " rxshort:%d", info->icount.rxshort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 if (info->icount.rxlong)
Alexey Dobriyand3378292009-03-31 15:19:18 -07003479 seq_printf(m, " rxlong:%d", info->icount.rxlong);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 if (info->icount.rxover)
Alexey Dobriyand3378292009-03-31 15:19:18 -07003481 seq_printf(m, " rxover:%d", info->icount.rxover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 if (info->icount.rxcrc)
Alexey Dobriyand3378292009-03-31 15:19:18 -07003483 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 } else {
Alexey Dobriyand3378292009-03-31 15:19:18 -07003485 seq_printf(m, " ASYNC tx:%d rx:%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 info->icount.tx, info->icount.rx);
3487 if (info->icount.frame)
Alexey Dobriyand3378292009-03-31 15:19:18 -07003488 seq_printf(m, " fe:%d", info->icount.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 if (info->icount.parity)
Alexey Dobriyand3378292009-03-31 15:19:18 -07003490 seq_printf(m, " pe:%d", info->icount.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 if (info->icount.brk)
Alexey Dobriyand3378292009-03-31 15:19:18 -07003492 seq_printf(m, " brk:%d", info->icount.brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 if (info->icount.overrun)
Alexey Dobriyand3378292009-03-31 15:19:18 -07003494 seq_printf(m, " oe:%d", info->icount.overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 }
3496
3497 /* Append serial signal status to end */
Alexey Dobriyand3378292009-03-31 15:19:18 -07003498 seq_printf(m, " %s\n", stat_buf+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Alexey Dobriyand3378292009-03-31 15:19:18 -07003500 seq_printf(m, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 info->tx_active,info->bh_requested,info->bh_running,
3502 info->pending_bh);
3503
3504 spin_lock_irqsave(&info->irq_spinlock,flags);
3505 {
3506 u16 Tcsr = usc_InReg( info, TCSR );
3507 u16 Tdmr = usc_InDmaReg( info, TDMR );
3508 u16 Ticr = usc_InReg( info, TICR );
3509 u16 Rscr = usc_InReg( info, RCSR );
3510 u16 Rdmr = usc_InDmaReg( info, RDMR );
3511 u16 Ricr = usc_InReg( info, RICR );
3512 u16 Icr = usc_InReg( info, ICR );
3513 u16 Dccr = usc_InReg( info, DCCR );
3514 u16 Tmr = usc_InReg( info, TMR );
3515 u16 Tccr = usc_InReg( info, TCCR );
3516 u16 Ccar = inw( info->io_base + CCAR );
Alexey Dobriyand3378292009-03-31 15:19:18 -07003517 seq_printf(m, "tcsr=%04X tdmr=%04X ticr=%04X rcsr=%04X rdmr=%04X\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 "ricr=%04X icr =%04X dccr=%04X tmr=%04X tccr=%04X ccar=%04X\n",
3519 Tcsr,Tdmr,Ticr,Rscr,Rdmr,Ricr,Icr,Dccr,Tmr,Tccr,Ccar );
3520 }
3521 spin_unlock_irqrestore(&info->irq_spinlock,flags);
Alexey Dobriyand3378292009-03-31 15:19:18 -07003522}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Alexey Dobriyand3378292009-03-31 15:19:18 -07003524/* Called to print information about devices */
3525static int mgsl_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 struct mgsl_struct *info;
3528
Alexey Dobriyand3378292009-03-31 15:19:18 -07003529 seq_printf(m, "synclink driver:%s\n", driver_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
3531 info = mgsl_device_list;
3532 while( info ) {
Alexey Dobriyand3378292009-03-31 15:19:18 -07003533 line_info(m, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 info = info->next_device;
3535 }
Alexey Dobriyand3378292009-03-31 15:19:18 -07003536 return 0;
3537}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Alexey Dobriyand3378292009-03-31 15:19:18 -07003539static int mgsl_proc_open(struct inode *inode, struct file *file)
3540{
3541 return single_open(file, mgsl_proc_show, NULL);
3542}
3543
3544static const struct file_operations mgsl_proc_fops = {
3545 .owner = THIS_MODULE,
3546 .open = mgsl_proc_open,
3547 .read = seq_read,
3548 .llseek = seq_lseek,
3549 .release = single_release,
3550};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
3552/* mgsl_allocate_dma_buffers()
3553 *
3554 * Allocate and format DMA buffers (ISA adapter)
3555 * or format shared memory buffers (PCI adapter).
3556 *
3557 * Arguments: info pointer to device instance data
3558 * Return Value: 0 if success, otherwise error
3559 */
3560static int mgsl_allocate_dma_buffers(struct mgsl_struct *info)
3561{
3562 unsigned short BuffersPerFrame;
3563
3564 info->last_mem_alloc = 0;
3565
3566 /* Calculate the number of DMA buffers necessary to hold the */
3567 /* largest allowable frame size. Note: If the max frame size is */
3568 /* not an even multiple of the DMA buffer size then we need to */
3569 /* round the buffer count per frame up one. */
3570
3571 BuffersPerFrame = (unsigned short)(info->max_frame_size/DMABUFFERSIZE);
3572 if ( info->max_frame_size % DMABUFFERSIZE )
3573 BuffersPerFrame++;
3574
3575 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
3576 /*
3577 * The PCI adapter has 256KBytes of shared memory to use.
3578 * This is 64 PAGE_SIZE buffers.
3579 *
3580 * The first page is used for padding at this time so the
3581 * buffer list does not begin at offset 0 of the PCI
3582 * adapter's shared memory.
3583 *
3584 * The 2nd page is used for the buffer list. A 4K buffer
3585 * list can hold 128 DMA_BUFFER structures at 32 bytes
3586 * each.
3587 *
3588 * This leaves 62 4K pages.
3589 *
3590 * The next N pages are used for transmit frame(s). We
3591 * reserve enough 4K page blocks to hold the required
3592 * number of transmit dma buffers (num_tx_dma_buffers),
3593 * each of MaxFrameSize size.
3594 *
3595 * Of the remaining pages (62-N), determine how many can
3596 * be used to receive full MaxFrameSize inbound frames
3597 */
3598 info->tx_buffer_count = info->num_tx_dma_buffers * BuffersPerFrame;
3599 info->rx_buffer_count = 62 - info->tx_buffer_count;
3600 } else {
3601 /* Calculate the number of PAGE_SIZE buffers needed for */
3602 /* receive and transmit DMA buffers. */
3603
3604
3605 /* Calculate the number of DMA buffers necessary to */
3606 /* hold 7 max size receive frames and one max size transmit frame. */
3607 /* The receive buffer count is bumped by one so we avoid an */
3608 /* End of List condition if all receive buffers are used when */
3609 /* using linked list DMA buffers. */
3610
3611 info->tx_buffer_count = info->num_tx_dma_buffers * BuffersPerFrame;
3612 info->rx_buffer_count = (BuffersPerFrame * MAXRXFRAMES) + 6;
3613
3614 /*
3615 * limit total TxBuffers & RxBuffers to 62 4K total
3616 * (ala PCI Allocation)
3617 */
3618
3619 if ( (info->tx_buffer_count + info->rx_buffer_count) > 62 )
3620 info->rx_buffer_count = 62 - info->tx_buffer_count;
3621
3622 }
3623
3624 if ( debug_level >= DEBUG_LEVEL_INFO )
3625 printk("%s(%d):Allocating %d TX and %d RX DMA buffers.\n",
3626 __FILE__,__LINE__, info->tx_buffer_count,info->rx_buffer_count);
3627
3628 if ( mgsl_alloc_buffer_list_memory( info ) < 0 ||
3629 mgsl_alloc_frame_memory(info, info->rx_buffer_list, info->rx_buffer_count) < 0 ||
3630 mgsl_alloc_frame_memory(info, info->tx_buffer_list, info->tx_buffer_count) < 0 ||
3631 mgsl_alloc_intermediate_rxbuffer_memory(info) < 0 ||
3632 mgsl_alloc_intermediate_txbuffer_memory(info) < 0 ) {
3633 printk("%s(%d):Can't allocate DMA buffer memory\n",__FILE__,__LINE__);
3634 return -ENOMEM;
3635 }
3636
3637 mgsl_reset_rx_dma_buffers( info );
3638 mgsl_reset_tx_dma_buffers( info );
3639
3640 return 0;
3641
3642} /* end of mgsl_allocate_dma_buffers() */
3643
3644/*
3645 * mgsl_alloc_buffer_list_memory()
3646 *
3647 * Allocate a common DMA buffer for use as the
3648 * receive and transmit buffer lists.
3649 *
3650 * A buffer list is a set of buffer entries where each entry contains
3651 * a pointer to an actual buffer and a pointer to the next buffer entry
3652 * (plus some other info about the buffer).
3653 *
3654 * The buffer entries for a list are built to form a circular list so
3655 * that when the entire list has been traversed you start back at the
3656 * beginning.
3657 *
3658 * This function allocates memory for just the buffer entries.
3659 * The links (pointer to next entry) are filled in with the physical
3660 * address of the next entry so the adapter can navigate the list
3661 * using bus master DMA. The pointers to the actual buffers are filled
3662 * out later when the actual buffers are allocated.
3663 *
3664 * Arguments: info pointer to device instance data
3665 * Return Value: 0 if success, otherwise error
3666 */
3667static int mgsl_alloc_buffer_list_memory( struct mgsl_struct *info )
3668{
3669 unsigned int i;
3670
3671 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
3672 /* PCI adapter uses shared memory. */
3673 info->buffer_list = info->memory_base + info->last_mem_alloc;
3674 info->buffer_list_phys = info->last_mem_alloc;
3675 info->last_mem_alloc += BUFFERLISTSIZE;
3676 } else {
3677 /* ISA adapter uses system memory. */
3678 /* The buffer lists are allocated as a common buffer that both */
3679 /* the processor and adapter can access. This allows the driver to */
3680 /* inspect portions of the buffer while other portions are being */
3681 /* updated by the adapter using Bus Master DMA. */
3682
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -08003683 info->buffer_list = dma_alloc_coherent(NULL, BUFFERLISTSIZE, &info->buffer_list_dma_addr, GFP_KERNEL);
3684 if (info->buffer_list == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 return -ENOMEM;
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -08003686 info->buffer_list_phys = (u32)(info->buffer_list_dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 }
3688
3689 /* We got the memory for the buffer entry lists. */
3690 /* Initialize the memory block to all zeros. */
3691 memset( info->buffer_list, 0, BUFFERLISTSIZE );
3692
3693 /* Save virtual address pointers to the receive and */
3694 /* transmit buffer lists. (Receive 1st). These pointers will */
3695 /* be used by the processor to access the lists. */
3696 info->rx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
3697 info->tx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
3698 info->tx_buffer_list += info->rx_buffer_count;
3699
3700 /*
3701 * Build the links for the buffer entry lists such that
3702 * two circular lists are built. (Transmit and Receive).
3703 *
3704 * Note: the links are physical addresses
3705 * which are read by the adapter to determine the next
3706 * buffer entry to use.
3707 */
3708
3709 for ( i = 0; i < info->rx_buffer_count; i++ ) {
3710 /* calculate and store physical address of this buffer entry */
3711 info->rx_buffer_list[i].phys_entry =
3712 info->buffer_list_phys + (i * sizeof(DMABUFFERENTRY));
3713
3714 /* calculate and store physical address of */
3715 /* next entry in cirular list of entries */
3716
3717 info->rx_buffer_list[i].link = info->buffer_list_phys;
3718
3719 if ( i < info->rx_buffer_count - 1 )
3720 info->rx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
3721 }
3722
3723 for ( i = 0; i < info->tx_buffer_count; i++ ) {
3724 /* calculate and store physical address of this buffer entry */
3725 info->tx_buffer_list[i].phys_entry = info->buffer_list_phys +
3726 ((info->rx_buffer_count + i) * sizeof(DMABUFFERENTRY));
3727
3728 /* calculate and store physical address of */
3729 /* next entry in cirular list of entries */
3730
3731 info->tx_buffer_list[i].link = info->buffer_list_phys +
3732 info->rx_buffer_count * sizeof(DMABUFFERENTRY);
3733
3734 if ( i < info->tx_buffer_count - 1 )
3735 info->tx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
3736 }
3737
3738 return 0;
3739
3740} /* end of mgsl_alloc_buffer_list_memory() */
3741
3742/* Free DMA buffers allocated for use as the
3743 * receive and transmit buffer lists.
3744 * Warning:
3745 *
3746 * The data transfer buffers associated with the buffer list
3747 * MUST be freed before freeing the buffer list itself because
3748 * the buffer list contains the information necessary to free
3749 * the individual buffers!
3750 */
3751static void mgsl_free_buffer_list_memory( struct mgsl_struct *info )
3752{
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -08003753 if (info->buffer_list && info->bus_type != MGSL_BUS_TYPE_PCI)
3754 dma_free_coherent(NULL, BUFFERLISTSIZE, info->buffer_list, info->buffer_list_dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
3756 info->buffer_list = NULL;
3757 info->rx_buffer_list = NULL;
3758 info->tx_buffer_list = NULL;
3759
3760} /* end of mgsl_free_buffer_list_memory() */
3761
3762/*
3763 * mgsl_alloc_frame_memory()
3764 *
3765 * Allocate the frame DMA buffers used by the specified buffer list.
3766 * Each DMA buffer will be one memory page in size. This is necessary
3767 * because memory can fragment enough that it may be impossible
3768 * contiguous pages.
3769 *
3770 * Arguments:
3771 *
3772 * info pointer to device instance data
3773 * BufferList pointer to list of buffer entries
3774 * Buffercount count of buffer entries in buffer list
3775 *
3776 * Return Value: 0 if success, otherwise -ENOMEM
3777 */
3778static int mgsl_alloc_frame_memory(struct mgsl_struct *info,DMABUFFERENTRY *BufferList,int Buffercount)
3779{
3780 int i;
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -08003781 u32 phys_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
3783 /* Allocate page sized buffers for the receive buffer list */
3784
3785 for ( i = 0; i < Buffercount; i++ ) {
3786 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
3787 /* PCI adapter uses shared memory buffers. */
3788 BufferList[i].virt_addr = info->memory_base + info->last_mem_alloc;
3789 phys_addr = info->last_mem_alloc;
3790 info->last_mem_alloc += DMABUFFERSIZE;
3791 } else {
3792 /* ISA adapter uses system memory. */
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -08003793 BufferList[i].virt_addr = dma_alloc_coherent(NULL, DMABUFFERSIZE, &BufferList[i].dma_addr, GFP_KERNEL);
3794 if (BufferList[i].virt_addr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 return -ENOMEM;
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -08003796 phys_addr = (u32)(BufferList[i].dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 }
3798 BufferList[i].phys_addr = phys_addr;
3799 }
3800
3801 return 0;
3802
3803} /* end of mgsl_alloc_frame_memory() */
3804
3805/*
3806 * mgsl_free_frame_memory()
3807 *
3808 * Free the buffers associated with
3809 * each buffer entry of a buffer list.
3810 *
3811 * Arguments:
3812 *
3813 * info pointer to device instance data
3814 * BufferList pointer to list of buffer entries
3815 * Buffercount count of buffer entries in buffer list
3816 *
3817 * Return Value: None
3818 */
3819static void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList, int Buffercount)
3820{
3821 int i;
3822
3823 if ( BufferList ) {
3824 for ( i = 0 ; i < Buffercount ; i++ ) {
3825 if ( BufferList[i].virt_addr ) {
3826 if ( info->bus_type != MGSL_BUS_TYPE_PCI )
Paul Fulghum0ff1b2c2005-11-13 16:07:19 -08003827 dma_free_coherent(NULL, DMABUFFERSIZE, BufferList[i].virt_addr, BufferList[i].dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 BufferList[i].virt_addr = NULL;
3829 }
3830 }
3831 }
3832
3833} /* end of mgsl_free_frame_memory() */
3834
3835/* mgsl_free_dma_buffers()
3836 *
3837 * Free DMA buffers
3838 *
3839 * Arguments: info pointer to device instance data
3840 * Return Value: None
3841 */
3842static void mgsl_free_dma_buffers( struct mgsl_struct *info )
3843{
3844 mgsl_free_frame_memory( info, info->rx_buffer_list, info->rx_buffer_count );
3845 mgsl_free_frame_memory( info, info->tx_buffer_list, info->tx_buffer_count );
3846 mgsl_free_buffer_list_memory( info );
3847
3848} /* end of mgsl_free_dma_buffers() */
3849
3850
3851/*
3852 * mgsl_alloc_intermediate_rxbuffer_memory()
3853 *
3854 * Allocate a buffer large enough to hold max_frame_size. This buffer
3855 * is used to pass an assembled frame to the line discipline.
3856 *
3857 * Arguments:
3858 *
3859 * info pointer to device instance data
3860 *
3861 * Return Value: 0 if success, otherwise -ENOMEM
3862 */
3863static int mgsl_alloc_intermediate_rxbuffer_memory(struct mgsl_struct *info)
3864{
3865 info->intermediate_rxbuffer = kmalloc(info->max_frame_size, GFP_KERNEL | GFP_DMA);
3866 if ( info->intermediate_rxbuffer == NULL )
3867 return -ENOMEM;
Paul Fulghuma6b68a62012-12-03 11:13:24 -06003868 /* unused flag buffer to satisfy receive_buf calling interface */
3869 info->flag_buf = kzalloc(info->max_frame_size, GFP_KERNEL);
3870 if (!info->flag_buf) {
3871 kfree(info->intermediate_rxbuffer);
3872 info->intermediate_rxbuffer = NULL;
3873 return -ENOMEM;
3874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 return 0;
3876
3877} /* end of mgsl_alloc_intermediate_rxbuffer_memory() */
3878
3879/*
3880 * mgsl_free_intermediate_rxbuffer_memory()
3881 *
3882 *
3883 * Arguments:
3884 *
3885 * info pointer to device instance data
3886 *
3887 * Return Value: None
3888 */
3889static void mgsl_free_intermediate_rxbuffer_memory(struct mgsl_struct *info)
3890{
Jesper Juhl735d5662005-11-07 01:01:29 -08003891 kfree(info->intermediate_rxbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 info->intermediate_rxbuffer = NULL;
Paul Fulghuma6b68a62012-12-03 11:13:24 -06003893 kfree(info->flag_buf);
3894 info->flag_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
3896} /* end of mgsl_free_intermediate_rxbuffer_memory() */
3897
3898/*
3899 * mgsl_alloc_intermediate_txbuffer_memory()
3900 *
3901 * Allocate intermdiate transmit buffer(s) large enough to hold max_frame_size.
3902 * This buffer is used to load transmit frames into the adapter's dma transfer
3903 * buffers when there is sufficient space.
3904 *
3905 * Arguments:
3906 *
3907 * info pointer to device instance data
3908 *
3909 * Return Value: 0 if success, otherwise -ENOMEM
3910 */
3911static int mgsl_alloc_intermediate_txbuffer_memory(struct mgsl_struct *info)
3912{
3913 int i;
3914
3915 if ( debug_level >= DEBUG_LEVEL_INFO )
3916 printk("%s %s(%d) allocating %d tx holding buffers\n",
3917 info->device_name, __FILE__,__LINE__,info->num_tx_holding_buffers);
3918
3919 memset(info->tx_holding_buffers,0,sizeof(info->tx_holding_buffers));
3920
3921 for ( i=0; i<info->num_tx_holding_buffers; ++i) {
3922 info->tx_holding_buffers[i].buffer =
3923 kmalloc(info->max_frame_size, GFP_KERNEL);
Amit Choudharyd9a2f4a2007-05-08 00:26:13 -07003924 if (info->tx_holding_buffers[i].buffer == NULL) {
3925 for (--i; i >= 0; i--) {
3926 kfree(info->tx_holding_buffers[i].buffer);
3927 info->tx_holding_buffers[i].buffer = NULL;
3928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return -ENOMEM;
Amit Choudharyd9a2f4a2007-05-08 00:26:13 -07003930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
3932
3933 return 0;
3934
3935} /* end of mgsl_alloc_intermediate_txbuffer_memory() */
3936
3937/*
3938 * mgsl_free_intermediate_txbuffer_memory()
3939 *
3940 *
3941 * Arguments:
3942 *
3943 * info pointer to device instance data
3944 *
3945 * Return Value: None
3946 */
3947static void mgsl_free_intermediate_txbuffer_memory(struct mgsl_struct *info)
3948{
3949 int i;
3950
3951 for ( i=0; i<info->num_tx_holding_buffers; ++i ) {
Jesper Juhl735d5662005-11-07 01:01:29 -08003952 kfree(info->tx_holding_buffers[i].buffer);
3953 info->tx_holding_buffers[i].buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 }
3955
3956 info->get_tx_holding_index = 0;
3957 info->put_tx_holding_index = 0;
3958 info->tx_holding_count = 0;
3959
3960} /* end of mgsl_free_intermediate_txbuffer_memory() */
3961
3962
3963/*
3964 * load_next_tx_holding_buffer()
3965 *
3966 * attempts to load the next buffered tx request into the
3967 * tx dma buffers
3968 *
3969 * Arguments:
3970 *
3971 * info pointer to device instance data
3972 *
Joe Perches0fab6de2008-04-28 02:14:02 -07003973 * Return Value: true if next buffered tx request loaded
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 * into adapter's tx dma buffer,
Joe Perches0fab6de2008-04-28 02:14:02 -07003975 * false otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 */
Joe Perches0fab6de2008-04-28 02:14:02 -07003977static bool load_next_tx_holding_buffer(struct mgsl_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978{
Joe Perches0fab6de2008-04-28 02:14:02 -07003979 bool ret = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
3981 if ( info->tx_holding_count ) {
3982 /* determine if we have enough tx dma buffers
3983 * to accommodate the next tx frame
3984 */
3985 struct tx_holding_buffer *ptx =
3986 &info->tx_holding_buffers[info->get_tx_holding_index];
3987 int num_free = num_free_tx_dma_buffers(info);
3988 int num_needed = ptx->buffer_size / DMABUFFERSIZE;
3989 if ( ptx->buffer_size % DMABUFFERSIZE )
3990 ++num_needed;
3991
3992 if (num_needed <= num_free) {
3993 info->xmit_cnt = ptx->buffer_size;
3994 mgsl_load_tx_dma_buffer(info,ptx->buffer,ptx->buffer_size);
3995
3996 --info->tx_holding_count;
3997 if ( ++info->get_tx_holding_index >= info->num_tx_holding_buffers)
3998 info->get_tx_holding_index=0;
3999
4000 /* restart transmit timer */
4001 mod_timer(&info->tx_timer, jiffies + msecs_to_jiffies(5000));
4002
Joe Perches0fab6de2008-04-28 02:14:02 -07004003 ret = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 }
4005 }
4006
4007 return ret;
4008}
4009
4010/*
4011 * save_tx_buffer_request()
4012 *
4013 * attempt to store transmit frame request for later transmission
4014 *
4015 * Arguments:
4016 *
4017 * info pointer to device instance data
4018 * Buffer pointer to buffer containing frame to load
4019 * BufferSize size in bytes of frame in Buffer
4020 *
4021 * Return Value: 1 if able to store, 0 otherwise
4022 */
4023static int save_tx_buffer_request(struct mgsl_struct *info,const char *Buffer, unsigned int BufferSize)
4024{
4025 struct tx_holding_buffer *ptx;
4026
4027 if ( info->tx_holding_count >= info->num_tx_holding_buffers ) {
4028 return 0; /* all buffers in use */
4029 }
4030
4031 ptx = &info->tx_holding_buffers[info->put_tx_holding_index];
4032 ptx->buffer_size = BufferSize;
4033 memcpy( ptx->buffer, Buffer, BufferSize);
4034
4035 ++info->tx_holding_count;
4036 if ( ++info->put_tx_holding_index >= info->num_tx_holding_buffers)
4037 info->put_tx_holding_index=0;
4038
4039 return 1;
4040}
4041
4042static int mgsl_claim_resources(struct mgsl_struct *info)
4043{
4044 if (request_region(info->io_base,info->io_addr_size,"synclink") == NULL) {
4045 printk( "%s(%d):I/O address conflict on device %s Addr=%08X\n",
4046 __FILE__,__LINE__,info->device_name, info->io_base);
4047 return -ENODEV;
4048 }
Joe Perches0fab6de2008-04-28 02:14:02 -07004049 info->io_addr_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
4051 if ( request_irq(info->irq_level,mgsl_interrupt,info->irq_flags,
4052 info->device_name, info ) < 0 ) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004053 printk( "%s(%d):Can't request interrupt on device %s IRQ=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 __FILE__,__LINE__,info->device_name, info->irq_level );
4055 goto errout;
4056 }
Joe Perches0fab6de2008-04-28 02:14:02 -07004057 info->irq_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
4059 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
4060 if (request_mem_region(info->phys_memory_base,0x40000,"synclink") == NULL) {
4061 printk( "%s(%d):mem addr conflict device %s Addr=%08X\n",
4062 __FILE__,__LINE__,info->device_name, info->phys_memory_base);
4063 goto errout;
4064 }
Joe Perches0fab6de2008-04-28 02:14:02 -07004065 info->shared_mem_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 if (request_mem_region(info->phys_lcr_base + info->lcr_offset,128,"synclink") == NULL) {
4067 printk( "%s(%d):lcr mem addr conflict device %s Addr=%08X\n",
4068 __FILE__,__LINE__,info->device_name, info->phys_lcr_base + info->lcr_offset);
4069 goto errout;
4070 }
Joe Perches0fab6de2008-04-28 02:14:02 -07004071 info->lcr_mem_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Alan Cox24cb2332008-04-30 00:54:19 -07004073 info->memory_base = ioremap_nocache(info->phys_memory_base,
4074 0x40000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 if (!info->memory_base) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004076 printk( "%s(%d):Can't map shared memory on device %s MemAddr=%08X\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
4078 goto errout;
4079 }
4080
4081 if ( !mgsl_memory_test(info) ) {
4082 printk( "%s(%d):Failed shared memory test %s MemAddr=%08X\n",
4083 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
4084 goto errout;
4085 }
4086
Alan Cox24cb2332008-04-30 00:54:19 -07004087 info->lcr_base = ioremap_nocache(info->phys_lcr_base,
4088 PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 if (!info->lcr_base) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004090 printk( "%s(%d):Can't map LCR memory on device %s MemAddr=%08X\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
4092 goto errout;
4093 }
Alan Cox24cb2332008-04-30 00:54:19 -07004094 info->lcr_base += info->lcr_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
4096 } else {
4097 /* claim DMA channel */
4098
4099 if (request_dma(info->dma_level,info->device_name) < 0){
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004100 printk( "%s(%d):Can't request DMA channel on device %s DMA=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 __FILE__,__LINE__,info->device_name, info->dma_level );
4102 mgsl_release_resources( info );
4103 return -ENODEV;
4104 }
Joe Perches0fab6de2008-04-28 02:14:02 -07004105 info->dma_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
4107 /* ISA adapter uses bus master DMA */
4108 set_dma_mode(info->dma_level,DMA_MODE_CASCADE);
4109 enable_dma(info->dma_level);
4110 }
4111
4112 if ( mgsl_allocate_dma_buffers(info) < 0 ) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004113 printk( "%s(%d):Can't allocate DMA buffers on device %s DMA=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 __FILE__,__LINE__,info->device_name, info->dma_level );
4115 goto errout;
4116 }
4117
4118 return 0;
4119errout:
4120 mgsl_release_resources(info);
4121 return -ENODEV;
4122
4123} /* end of mgsl_claim_resources() */
4124
4125static void mgsl_release_resources(struct mgsl_struct *info)
4126{
4127 if ( debug_level >= DEBUG_LEVEL_INFO )
4128 printk( "%s(%d):mgsl_release_resources(%s) entry\n",
4129 __FILE__,__LINE__,info->device_name );
4130
4131 if ( info->irq_requested ) {
4132 free_irq(info->irq_level, info);
Joe Perches0fab6de2008-04-28 02:14:02 -07004133 info->irq_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
4135 if ( info->dma_requested ) {
4136 disable_dma(info->dma_level);
4137 free_dma(info->dma_level);
Joe Perches0fab6de2008-04-28 02:14:02 -07004138 info->dma_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 }
4140 mgsl_free_dma_buffers(info);
4141 mgsl_free_intermediate_rxbuffer_memory(info);
4142 mgsl_free_intermediate_txbuffer_memory(info);
4143
4144 if ( info->io_addr_requested ) {
4145 release_region(info->io_base,info->io_addr_size);
Joe Perches0fab6de2008-04-28 02:14:02 -07004146 info->io_addr_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 }
4148 if ( info->shared_mem_requested ) {
4149 release_mem_region(info->phys_memory_base,0x40000);
Joe Perches0fab6de2008-04-28 02:14:02 -07004150 info->shared_mem_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 }
4152 if ( info->lcr_mem_requested ) {
4153 release_mem_region(info->phys_lcr_base + info->lcr_offset,128);
Joe Perches0fab6de2008-04-28 02:14:02 -07004154 info->lcr_mem_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 }
4156 if (info->memory_base){
4157 iounmap(info->memory_base);
4158 info->memory_base = NULL;
4159 }
4160 if (info->lcr_base){
4161 iounmap(info->lcr_base - info->lcr_offset);
4162 info->lcr_base = NULL;
4163 }
4164
4165 if ( debug_level >= DEBUG_LEVEL_INFO )
4166 printk( "%s(%d):mgsl_release_resources(%s) exit\n",
4167 __FILE__,__LINE__,info->device_name );
4168
4169} /* end of mgsl_release_resources() */
4170
4171/* mgsl_add_device()
4172 *
4173 * Add the specified device instance data structure to the
4174 * global linked list of devices and increment the device count.
4175 *
4176 * Arguments: info pointer to device instance data
4177 * Return Value: None
4178 */
4179static void mgsl_add_device( struct mgsl_struct *info )
4180{
4181 info->next_device = NULL;
4182 info->line = mgsl_device_count;
4183 sprintf(info->device_name,"ttySL%d",info->line);
4184
4185 if (info->line < MAX_TOTAL_DEVICES) {
4186 if (maxframe[info->line])
4187 info->max_frame_size = maxframe[info->line];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189 if (txdmabufs[info->line]) {
4190 info->num_tx_dma_buffers = txdmabufs[info->line];
4191 if (info->num_tx_dma_buffers < 1)
4192 info->num_tx_dma_buffers = 1;
4193 }
4194
4195 if (txholdbufs[info->line]) {
4196 info->num_tx_holding_buffers = txholdbufs[info->line];
4197 if (info->num_tx_holding_buffers < 1)
4198 info->num_tx_holding_buffers = 1;
4199 else if (info->num_tx_holding_buffers > MAX_TX_HOLDING_BUFFERS)
4200 info->num_tx_holding_buffers = MAX_TX_HOLDING_BUFFERS;
4201 }
4202 }
4203
4204 mgsl_device_count++;
4205
4206 if ( !mgsl_device_list )
4207 mgsl_device_list = info;
4208 else {
4209 struct mgsl_struct *current_dev = mgsl_device_list;
4210 while( current_dev->next_device )
4211 current_dev = current_dev->next_device;
4212 current_dev->next_device = info;
4213 }
4214
4215 if ( info->max_frame_size < 4096 )
4216 info->max_frame_size = 4096;
4217 else if ( info->max_frame_size > 65535 )
4218 info->max_frame_size = 65535;
4219
4220 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
4221 printk( "SyncLink PCI v%d %s: IO=%04X IRQ=%d Mem=%08X,%08X MaxFrameSize=%u\n",
4222 info->hw_version + 1, info->device_name, info->io_base, info->irq_level,
4223 info->phys_memory_base, info->phys_lcr_base,
4224 info->max_frame_size );
4225 } else {
4226 printk( "SyncLink ISA %s: IO=%04X IRQ=%d DMA=%d MaxFrameSize=%u\n",
4227 info->device_name, info->io_base, info->irq_level, info->dma_level,
4228 info->max_frame_size );
4229 }
4230
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08004231#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 hdlcdev_init(info);
4233#endif
4234
4235} /* end of mgsl_add_device() */
4236
Alan Cox31f35932009-01-02 13:45:05 +00004237static const struct tty_port_operations mgsl_port_ops = {
4238 .carrier_raised = carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +01004239 .dtr_rts = dtr_rts,
Alan Cox31f35932009-01-02 13:45:05 +00004240};
4241
4242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243/* mgsl_allocate_device()
4244 *
4245 * Allocate and initialize a device instance structure
4246 *
4247 * Arguments: none
4248 * Return Value: pointer to mgsl_struct if success, otherwise NULL
4249 */
4250static struct mgsl_struct* mgsl_allocate_device(void)
4251{
4252 struct mgsl_struct *info;
4253
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004254 info = kzalloc(sizeof(struct mgsl_struct),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 GFP_KERNEL);
4256
4257 if (!info) {
4258 printk("Error can't allocate device instance data\n");
4259 } else {
Alan Cox44b7d1b2008-07-16 21:57:18 +01004260 tty_port_init(&info->port);
Alan Cox31f35932009-01-02 13:45:05 +00004261 info->port.ops = &mgsl_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 info->magic = MGSL_MAGIC;
David Howellsc4028952006-11-22 14:57:56 +00004263 INIT_WORK(&info->task, mgsl_bh_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 info->max_frame_size = 4096;
Alan Cox44b7d1b2008-07-16 21:57:18 +01004265 info->port.close_delay = 5*HZ/10;
4266 info->port.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 init_waitqueue_head(&info->status_event_wait_q);
4268 init_waitqueue_head(&info->event_wait_q);
4269 spin_lock_init(&info->irq_spinlock);
4270 spin_lock_init(&info->netlock);
4271 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
Alexandru Juncue06922a2013-07-27 11:14:39 +03004272 info->idle_mode = HDLC_TXIDLE_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 info->num_tx_dma_buffers = 1;
4274 info->num_tx_holding_buffers = 0;
4275 }
4276
4277 return info;
4278
4279} /* end of mgsl_allocate_device()*/
4280
Jeff Dikeb68e31d2006-10-02 02:17:18 -07004281static const struct tty_operations mgsl_ops = {
Jiri Slaby8a3ad102012-08-07 21:48:00 +02004282 .install = mgsl_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 .open = mgsl_open,
4284 .close = mgsl_close,
4285 .write = mgsl_write,
4286 .put_char = mgsl_put_char,
4287 .flush_chars = mgsl_flush_chars,
4288 .write_room = mgsl_write_room,
4289 .chars_in_buffer = mgsl_chars_in_buffer,
4290 .flush_buffer = mgsl_flush_buffer,
4291 .ioctl = mgsl_ioctl,
4292 .throttle = mgsl_throttle,
4293 .unthrottle = mgsl_unthrottle,
4294 .send_xchar = mgsl_send_xchar,
4295 .break_ctl = mgsl_break,
4296 .wait_until_sent = mgsl_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 .set_termios = mgsl_set_termios,
4298 .stop = mgsl_stop,
4299 .start = mgsl_start,
4300 .hangup = mgsl_hangup,
4301 .tiocmget = tiocmget,
4302 .tiocmset = tiocmset,
Alan Cox05871022010-09-16 18:21:52 +01004303 .get_icount = msgl_get_icount,
Alexey Dobriyand3378292009-03-31 15:19:18 -07004304 .proc_fops = &mgsl_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305};
4306
4307/*
4308 * perform tty device initialization
4309 */
4310static int mgsl_init_tty(void)
4311{
4312 int rc;
4313
4314 serial_driver = alloc_tty_driver(128);
4315 if (!serial_driver)
4316 return -ENOMEM;
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 serial_driver->driver_name = "synclink";
4319 serial_driver->name = "ttySL";
4320 serial_driver->major = ttymajor;
4321 serial_driver->minor_start = 64;
4322 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
4323 serial_driver->subtype = SERIAL_TYPE_NORMAL;
4324 serial_driver->init_termios = tty_std_termios;
4325 serial_driver->init_termios.c_cflag =
4326 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -08004327 serial_driver->init_termios.c_ispeed = 9600;
4328 serial_driver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 serial_driver->flags = TTY_DRIVER_REAL_RAW;
4330 tty_set_operations(serial_driver, &mgsl_ops);
4331 if ((rc = tty_register_driver(serial_driver)) < 0) {
4332 printk("%s(%d):Couldn't register serial driver\n",
4333 __FILE__,__LINE__);
4334 put_tty_driver(serial_driver);
4335 serial_driver = NULL;
4336 return rc;
4337 }
4338
4339 printk("%s %s, tty major#%d\n",
4340 driver_name, driver_version,
4341 serial_driver->major);
4342 return 0;
4343}
4344
4345/* enumerate user specified ISA adapters
4346 */
4347static void mgsl_enum_isa_devices(void)
4348{
4349 struct mgsl_struct *info;
4350 int i;
4351
4352 /* Check for user specified ISA devices */
4353
4354 for (i=0 ;(i < MAX_ISA_DEVICES) && io[i] && irq[i]; i++){
4355 if ( debug_level >= DEBUG_LEVEL_INFO )
4356 printk("ISA device specified io=%04X,irq=%d,dma=%d\n",
4357 io[i], irq[i], dma[i] );
4358
4359 info = mgsl_allocate_device();
4360 if ( !info ) {
4361 /* error allocating device instance data */
4362 if ( debug_level >= DEBUG_LEVEL_ERROR )
4363 printk( "can't allocate device instance data.\n");
4364 continue;
4365 }
4366
4367 /* Copy user configuration info to device instance data */
4368 info->io_base = (unsigned int)io[i];
4369 info->irq_level = (unsigned int)irq[i];
4370 info->irq_level = irq_canonicalize(info->irq_level);
4371 info->dma_level = (unsigned int)dma[i];
4372 info->bus_type = MGSL_BUS_TYPE_ISA;
4373 info->io_addr_size = 16;
4374 info->irq_flags = 0;
4375
4376 mgsl_add_device( info );
4377 }
4378}
4379
4380static void synclink_cleanup(void)
4381{
4382 int rc;
4383 struct mgsl_struct *info;
4384 struct mgsl_struct *tmp;
4385
4386 printk("Unloading %s: %s\n", driver_name, driver_version);
4387
4388 if (serial_driver) {
Greg Kroah-Hartmana271ca32015-04-30 11:22:14 +02004389 rc = tty_unregister_driver(serial_driver);
4390 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 printk("%s(%d) failed to unregister tty driver err=%d\n",
4392 __FILE__,__LINE__,rc);
4393 put_tty_driver(serial_driver);
4394 }
4395
4396 info = mgsl_device_list;
4397 while(info) {
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08004398#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 hdlcdev_exit(info);
4400#endif
4401 mgsl_release_resources(info);
4402 tmp = info;
4403 info = info->next_device;
Jiri Slaby191c5f12012-11-15 09:49:56 +01004404 tty_port_destroy(&tmp->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 kfree(tmp);
4406 }
4407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 if (pci_registered)
4409 pci_unregister_driver(&synclink_pci_driver);
4410}
4411
4412static int __init synclink_init(void)
4413{
4414 int rc;
4415
4416 if (break_on_load) {
4417 mgsl_get_text_ptr();
4418 BREAKPOINT();
4419 }
4420
4421 printk("%s %s\n", driver_name, driver_version);
4422
4423 mgsl_enum_isa_devices();
4424 if ((rc = pci_register_driver(&synclink_pci_driver)) < 0)
4425 printk("%s:failed to register PCI driver, error=%d\n",__FILE__,rc);
4426 else
Joe Perches0fab6de2008-04-28 02:14:02 -07004427 pci_registered = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
4429 if ((rc = mgsl_init_tty()) < 0)
4430 goto error;
4431
4432 return 0;
4433
4434error:
4435 synclink_cleanup();
4436 return rc;
4437}
4438
4439static void __exit synclink_exit(void)
4440{
4441 synclink_cleanup();
4442}
4443
4444module_init(synclink_init);
4445module_exit(synclink_exit);
4446
4447/*
4448 * usc_RTCmd()
4449 *
4450 * Issue a USC Receive/Transmit command to the
4451 * Channel Command/Address Register (CCAR).
4452 *
4453 * Notes:
4454 *
4455 * The command is encoded in the most significant 5 bits <15..11>
4456 * of the CCAR value. Bits <10..7> of the CCAR must be preserved
4457 * and Bits <6..0> must be written as zeros.
4458 *
4459 * Arguments:
4460 *
4461 * info pointer to device information structure
4462 * Cmd command mask (use symbolic macros)
4463 *
4464 * Return Value:
4465 *
4466 * None
4467 */
4468static void usc_RTCmd( struct mgsl_struct *info, u16 Cmd )
4469{
4470 /* output command to CCAR in bits <15..11> */
4471 /* preserve bits <10..7>, bits <6..0> must be zero */
4472
4473 outw( Cmd + info->loopback_bits, info->io_base + CCAR );
4474
4475 /* Read to flush write to CCAR */
4476 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4477 inw( info->io_base + CCAR );
4478
4479} /* end of usc_RTCmd() */
4480
4481/*
4482 * usc_DmaCmd()
4483 *
4484 * Issue a DMA command to the DMA Command/Address Register (DCAR).
4485 *
4486 * Arguments:
4487 *
4488 * info pointer to device information structure
4489 * Cmd DMA command mask (usc_DmaCmd_XX Macros)
4490 *
4491 * Return Value:
4492 *
4493 * None
4494 */
4495static void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd )
4496{
4497 /* write command mask to DCAR */
4498 outw( Cmd + info->mbre_bit, info->io_base );
4499
4500 /* Read to flush write to DCAR */
4501 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4502 inw( info->io_base );
4503
4504} /* end of usc_DmaCmd() */
4505
4506/*
4507 * usc_OutDmaReg()
4508 *
4509 * Write a 16-bit value to a USC DMA register
4510 *
4511 * Arguments:
4512 *
4513 * info pointer to device info structure
4514 * RegAddr register address (number) for write
4515 * RegValue 16-bit value to write to register
4516 *
4517 * Return Value:
4518 *
4519 * None
4520 *
4521 */
4522static void usc_OutDmaReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
4523{
4524 /* Note: The DCAR is located at the adapter base address */
4525 /* Note: must preserve state of BIT8 in DCAR */
4526
4527 outw( RegAddr + info->mbre_bit, info->io_base );
4528 outw( RegValue, info->io_base );
4529
4530 /* Read to flush write to DCAR */
4531 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4532 inw( info->io_base );
4533
4534} /* end of usc_OutDmaReg() */
4535
4536/*
4537 * usc_InDmaReg()
4538 *
4539 * Read a 16-bit value from a DMA register
4540 *
4541 * Arguments:
4542 *
4543 * info pointer to device info structure
4544 * RegAddr register address (number) to read from
4545 *
4546 * Return Value:
4547 *
4548 * The 16-bit value read from register
4549 *
4550 */
4551static u16 usc_InDmaReg( struct mgsl_struct *info, u16 RegAddr )
4552{
4553 /* Note: The DCAR is located at the adapter base address */
4554 /* Note: must preserve state of BIT8 in DCAR */
4555
4556 outw( RegAddr + info->mbre_bit, info->io_base );
4557 return inw( info->io_base );
4558
4559} /* end of usc_InDmaReg() */
4560
4561/*
4562 *
4563 * usc_OutReg()
4564 *
4565 * Write a 16-bit value to a USC serial channel register
4566 *
4567 * Arguments:
4568 *
4569 * info pointer to device info structure
4570 * RegAddr register address (number) to write to
4571 * RegValue 16-bit value to write to register
4572 *
4573 * Return Value:
4574 *
4575 * None
4576 *
4577 */
4578static void usc_OutReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
4579{
4580 outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
4581 outw( RegValue, info->io_base + CCAR );
4582
4583 /* Read to flush write to CCAR */
4584 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4585 inw( info->io_base + CCAR );
4586
4587} /* end of usc_OutReg() */
4588
4589/*
4590 * usc_InReg()
4591 *
4592 * Reads a 16-bit value from a USC serial channel register
4593 *
4594 * Arguments:
4595 *
4596 * info pointer to device extension
4597 * RegAddr register address (number) to read from
4598 *
4599 * Return Value:
4600 *
4601 * 16-bit value read from register
4602 */
4603static u16 usc_InReg( struct mgsl_struct *info, u16 RegAddr )
4604{
4605 outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
4606 return inw( info->io_base + CCAR );
4607
4608} /* end of usc_InReg() */
4609
4610/* usc_set_sdlc_mode()
4611 *
4612 * Set up the adapter for SDLC DMA communications.
4613 *
4614 * Arguments: info pointer to device instance data
4615 * Return Value: NONE
4616 */
4617static void usc_set_sdlc_mode( struct mgsl_struct *info )
4618{
4619 u16 RegValue;
Joe Perches0fab6de2008-04-28 02:14:02 -07004620 bool PreSL1660;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
4622 /*
4623 * determine if the IUSC on the adapter is pre-SL1660. If
4624 * not, take advantage of the UnderWait feature of more
4625 * modern chips. If an underrun occurs and this bit is set,
4626 * the transmitter will idle the programmed idle pattern
4627 * until the driver has time to service the underrun. Otherwise,
4628 * the dma controller may get the cycles previously requested
4629 * and begin transmitting queued tx data.
4630 */
4631 usc_OutReg(info,TMCR,0x1f);
4632 RegValue=usc_InReg(info,TMDR);
Joe Perches0fab6de2008-04-28 02:14:02 -07004633 PreSL1660 = (RegValue == IUSC_PRE_SL1660);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
4635 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
4636 {
4637 /*
4638 ** Channel Mode Register (CMR)
4639 **
4640 ** <15..14> 10 Tx Sub Modes, Send Flag on Underrun
4641 ** <13> 0 0 = Transmit Disabled (initially)
4642 ** <12> 0 1 = Consecutive Idles share common 0
4643 ** <11..8> 1110 Transmitter Mode = HDLC/SDLC Loop
4644 ** <7..4> 0000 Rx Sub Modes, addr/ctrl field handling
4645 ** <3..0> 0110 Receiver Mode = HDLC/SDLC
4646 **
4647 ** 1000 1110 0000 0110 = 0x8e06
4648 */
4649 RegValue = 0x8e06;
4650
4651 /*--------------------------------------------------
4652 * ignore user options for UnderRun Actions and
4653 * preambles
4654 *--------------------------------------------------*/
4655 }
4656 else
4657 {
4658 /* Channel mode Register (CMR)
4659 *
4660 * <15..14> 00 Tx Sub modes, Underrun Action
4661 * <13> 0 1 = Send Preamble before opening flag
4662 * <12> 0 1 = Consecutive Idles share common 0
4663 * <11..8> 0110 Transmitter mode = HDLC/SDLC
4664 * <7..4> 0000 Rx Sub modes, addr/ctrl field handling
4665 * <3..0> 0110 Receiver mode = HDLC/SDLC
4666 *
4667 * 0000 0110 0000 0110 = 0x0606
4668 */
4669 if (info->params.mode == MGSL_MODE_RAW) {
4670 RegValue = 0x0001; /* Set Receive mode = external sync */
4671
4672 usc_OutReg( info, IOCR, /* Set IOCR DCD is RxSync Detect Input */
4673 (unsigned short)((usc_InReg(info, IOCR) & ~(BIT13|BIT12)) | BIT12));
4674
4675 /*
4676 * TxSubMode:
4677 * CMR <15> 0 Don't send CRC on Tx Underrun
4678 * CMR <14> x undefined
4679 * CMR <13> 0 Send preamble before openning sync
4680 * CMR <12> 0 Send 8-bit syncs, 1=send Syncs per TxLength
4681 *
4682 * TxMode:
4683 * CMR <11-8) 0100 MonoSync
4684 *
4685 * 0x00 0100 xxxx xxxx 04xx
4686 */
4687 RegValue |= 0x0400;
4688 }
4689 else {
4690
4691 RegValue = 0x0606;
4692
4693 if ( info->params.flags & HDLC_FLAG_UNDERRUN_ABORT15 )
4694 RegValue |= BIT14;
4695 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_FLAG )
4696 RegValue |= BIT15;
4697 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_CRC )
Alexandru Juncue06922a2013-07-27 11:14:39 +03004698 RegValue |= BIT15 | BIT14;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 }
4700
4701 if ( info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE )
4702 RegValue |= BIT13;
4703 }
4704
4705 if ( info->params.mode == MGSL_MODE_HDLC &&
4706 (info->params.flags & HDLC_FLAG_SHARE_ZERO) )
4707 RegValue |= BIT12;
4708
4709 if ( info->params.addr_filter != 0xff )
4710 {
4711 /* set up receive address filtering */
4712 usc_OutReg( info, RSR, info->params.addr_filter );
4713 RegValue |= BIT4;
4714 }
4715
4716 usc_OutReg( info, CMR, RegValue );
4717 info->cmr_value = RegValue;
4718
4719 /* Receiver mode Register (RMR)
4720 *
4721 * <15..13> 000 encoding
4722 * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
4723 * <10> 1 1 = Set CRC to all 1s (use for SDLC/HDLC)
4724 * <9> 0 1 = Include Receive chars in CRC
4725 * <8> 1 1 = Use Abort/PE bit as abort indicator
4726 * <7..6> 00 Even parity
4727 * <5> 0 parity disabled
4728 * <4..2> 000 Receive Char Length = 8 bits
4729 * <1..0> 00 Disable Receiver
4730 *
4731 * 0000 0101 0000 0000 = 0x0500
4732 */
4733
4734 RegValue = 0x0500;
4735
4736 switch ( info->params.encoding ) {
4737 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
4738 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
Alexandru Juncue06922a2013-07-27 11:14:39 +03004739 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 | BIT13; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
Alexandru Juncue06922a2013-07-27 11:14:39 +03004741 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 | BIT13; break;
4742 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14; break;
4743 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14 | BIT13; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 }
4745
4746 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
4747 RegValue |= BIT9;
4748 else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
4749 RegValue |= ( BIT12 | BIT10 | BIT9 );
4750
4751 usc_OutReg( info, RMR, RegValue );
4752
4753 /* Set the Receive count Limit Register (RCLR) to 0xffff. */
4754 /* When an opening flag of an SDLC frame is recognized the */
4755 /* Receive Character count (RCC) is loaded with the value in */
4756 /* RCLR. The RCC is decremented for each received byte. The */
4757 /* value of RCC is stored after the closing flag of the frame */
4758 /* allowing the frame size to be computed. */
4759
4760 usc_OutReg( info, RCLR, RCLRVALUE );
4761
4762 usc_RCmd( info, RCmd_SelectRicrdma_level );
4763
4764 /* Receive Interrupt Control Register (RICR)
4765 *
4766 * <15..8> ? RxFIFO DMA Request Level
4767 * <7> 0 Exited Hunt IA (Interrupt Arm)
4768 * <6> 0 Idle Received IA
4769 * <5> 0 Break/Abort IA
4770 * <4> 0 Rx Bound IA
4771 * <3> 1 Queued status reflects oldest 2 bytes in FIFO
4772 * <2> 0 Abort/PE IA
4773 * <1> 1 Rx Overrun IA
4774 * <0> 0 Select TC0 value for readback
4775 *
4776 * 0000 0000 0000 1000 = 0x000a
4777 */
4778
4779 /* Carry over the Exit Hunt and Idle Received bits */
4780 /* in case they have been armed by usc_ArmEvents. */
4781
4782 RegValue = usc_InReg( info, RICR ) & 0xc0;
4783
4784 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4785 usc_OutReg( info, RICR, (u16)(0x030a | RegValue) );
4786 else
4787 usc_OutReg( info, RICR, (u16)(0x140a | RegValue) );
4788
4789 /* Unlatch all Rx status bits and clear Rx status IRQ Pending */
4790
4791 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
4792 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
4793
4794 /* Transmit mode Register (TMR)
4795 *
4796 * <15..13> 000 encoding
4797 * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
4798 * <10> 1 1 = Start CRC as all 1s (use for SDLC/HDLC)
4799 * <9> 0 1 = Tx CRC Enabled
4800 * <8> 0 1 = Append CRC to end of transmit frame
4801 * <7..6> 00 Transmit parity Even
4802 * <5> 0 Transmit parity Disabled
4803 * <4..2> 000 Tx Char Length = 8 bits
4804 * <1..0> 00 Disable Transmitter
4805 *
4806 * 0000 0100 0000 0000 = 0x0400
4807 */
4808
4809 RegValue = 0x0400;
4810
4811 switch ( info->params.encoding ) {
4812 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
4813 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
Alexandru Juncue06922a2013-07-27 11:14:39 +03004814 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 | BIT13; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
Alexandru Juncue06922a2013-07-27 11:14:39 +03004816 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 | BIT13; break;
4817 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14; break;
4818 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14 | BIT13; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 }
4820
4821 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
Alexandru Juncue06922a2013-07-27 11:14:39 +03004822 RegValue |= BIT9 | BIT8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
4824 RegValue |= ( BIT12 | BIT10 | BIT9 | BIT8);
4825
4826 usc_OutReg( info, TMR, RegValue );
4827
4828 usc_set_txidle( info );
4829
4830
4831 usc_TCmd( info, TCmd_SelectTicrdma_level );
4832
4833 /* Transmit Interrupt Control Register (TICR)
4834 *
4835 * <15..8> ? Transmit FIFO DMA Level
4836 * <7> 0 Present IA (Interrupt Arm)
4837 * <6> 0 Idle Sent IA
4838 * <5> 1 Abort Sent IA
4839 * <4> 1 EOF/EOM Sent IA
4840 * <3> 0 CRC Sent IA
4841 * <2> 1 1 = Wait for SW Trigger to Start Frame
4842 * <1> 1 Tx Underrun IA
4843 * <0> 0 TC0 constant on read back
4844 *
4845 * 0000 0000 0011 0110 = 0x0036
4846 */
4847
4848 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4849 usc_OutReg( info, TICR, 0x0736 );
4850 else
4851 usc_OutReg( info, TICR, 0x1436 );
4852
4853 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
4854 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
4855
4856 /*
4857 ** Transmit Command/Status Register (TCSR)
4858 **
4859 ** <15..12> 0000 TCmd
4860 ** <11> 0/1 UnderWait
4861 ** <10..08> 000 TxIdle
4862 ** <7> x PreSent
4863 ** <6> x IdleSent
4864 ** <5> x AbortSent
4865 ** <4> x EOF/EOM Sent
4866 ** <3> x CRC Sent
4867 ** <2> x All Sent
4868 ** <1> x TxUnder
4869 ** <0> x TxEmpty
4870 **
4871 ** 0000 0000 0000 0000 = 0x0000
4872 */
4873 info->tcsr_value = 0;
4874
4875 if ( !PreSL1660 )
4876 info->tcsr_value |= TCSR_UNDERWAIT;
4877
4878 usc_OutReg( info, TCSR, info->tcsr_value );
4879
4880 /* Clock mode Control Register (CMCR)
4881 *
4882 * <15..14> 00 counter 1 Source = Disabled
4883 * <13..12> 00 counter 0 Source = Disabled
4884 * <11..10> 11 BRG1 Input is TxC Pin
4885 * <9..8> 11 BRG0 Input is TxC Pin
4886 * <7..6> 01 DPLL Input is BRG1 Output
4887 * <5..3> XXX TxCLK comes from Port 0
4888 * <2..0> XXX RxCLK comes from Port 1
4889 *
4890 * 0000 1111 0111 0111 = 0x0f77
4891 */
4892
4893 RegValue = 0x0f40;
4894
4895 if ( info->params.flags & HDLC_FLAG_RXC_DPLL )
4896 RegValue |= 0x0003; /* RxCLK from DPLL */
4897 else if ( info->params.flags & HDLC_FLAG_RXC_BRG )
4898 RegValue |= 0x0004; /* RxCLK from BRG0 */
4899 else if ( info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4900 RegValue |= 0x0006; /* RxCLK from TXC Input */
4901 else
4902 RegValue |= 0x0007; /* RxCLK from Port1 */
4903
4904 if ( info->params.flags & HDLC_FLAG_TXC_DPLL )
4905 RegValue |= 0x0018; /* TxCLK from DPLL */
4906 else if ( info->params.flags & HDLC_FLAG_TXC_BRG )
4907 RegValue |= 0x0020; /* TxCLK from BRG0 */
4908 else if ( info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4909 RegValue |= 0x0038; /* RxCLK from TXC Input */
4910 else
4911 RegValue |= 0x0030; /* TxCLK from Port0 */
4912
4913 usc_OutReg( info, CMCR, RegValue );
4914
4915
4916 /* Hardware Configuration Register (HCR)
4917 *
4918 * <15..14> 00 CTR0 Divisor:00=32,01=16,10=8,11=4
4919 * <13> 0 CTR1DSel:0=CTR0Div determines CTR0Div
4920 * <12> 0 CVOK:0=report code violation in biphase
4921 * <11..10> 00 DPLL Divisor:00=32,01=16,10=8,11=4
4922 * <9..8> XX DPLL mode:00=disable,01=NRZ,10=Biphase,11=Biphase Level
4923 * <7..6> 00 reserved
4924 * <5> 0 BRG1 mode:0=continuous,1=single cycle
4925 * <4> X BRG1 Enable
4926 * <3..2> 00 reserved
4927 * <1> 0 BRG0 mode:0=continuous,1=single cycle
4928 * <0> 0 BRG0 Enable
4929 */
4930
4931 RegValue = 0x0000;
4932
Alexandru Juncue06922a2013-07-27 11:14:39 +03004933 if ( info->params.flags & (HDLC_FLAG_RXC_DPLL | HDLC_FLAG_TXC_DPLL) ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 u32 XtalSpeed;
4935 u32 DpllDivisor;
4936 u16 Tc;
4937
4938 /* DPLL is enabled. Use BRG1 to provide continuous reference clock */
4939 /* for DPLL. DPLL mode in HCR is dependent on the encoding used. */
4940
4941 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4942 XtalSpeed = 11059200;
4943 else
4944 XtalSpeed = 14745600;
4945
4946 if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
4947 DpllDivisor = 16;
4948 RegValue |= BIT10;
4949 }
4950 else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
4951 DpllDivisor = 8;
4952 RegValue |= BIT11;
4953 }
4954 else
4955 DpllDivisor = 32;
4956
4957 /* Tc = (Xtal/Speed) - 1 */
4958 /* If twice the remainder of (Xtal/Speed) is greater than Speed */
4959 /* then rounding up gives a more precise time constant. Instead */
4960 /* of rounding up and then subtracting 1 we just don't subtract */
4961 /* the one in this case. */
4962
4963 /*--------------------------------------------------
4964 * ejz: for DPLL mode, application should use the
4965 * same clock speed as the partner system, even
4966 * though clocking is derived from the input RxData.
4967 * In case the user uses a 0 for the clock speed,
4968 * default to 0xffffffff and don't try to divide by
4969 * zero
4970 *--------------------------------------------------*/
4971 if ( info->params.clock_speed )
4972 {
4973 Tc = (u16)((XtalSpeed/DpllDivisor)/info->params.clock_speed);
4974 if ( !((((XtalSpeed/DpllDivisor) % info->params.clock_speed) * 2)
4975 / info->params.clock_speed) )
4976 Tc--;
4977 }
4978 else
4979 Tc = -1;
4980
4981
4982 /* Write 16-bit Time Constant for BRG1 */
4983 usc_OutReg( info, TC1R, Tc );
4984
4985 RegValue |= BIT4; /* enable BRG1 */
4986
4987 switch ( info->params.encoding ) {
4988 case HDLC_ENCODING_NRZ:
4989 case HDLC_ENCODING_NRZB:
4990 case HDLC_ENCODING_NRZI_MARK:
4991 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT8; break;
4992 case HDLC_ENCODING_BIPHASE_MARK:
4993 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT9; break;
4994 case HDLC_ENCODING_BIPHASE_LEVEL:
Alexandru Juncue06922a2013-07-27 11:14:39 +03004995 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT9 | BIT8; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 }
4997 }
4998
4999 usc_OutReg( info, HCR, RegValue );
5000
5001
5002 /* Channel Control/status Register (CCSR)
5003 *
5004 * <15> X RCC FIFO Overflow status (RO)
5005 * <14> X RCC FIFO Not Empty status (RO)
5006 * <13> 0 1 = Clear RCC FIFO (WO)
5007 * <12> X DPLL Sync (RW)
5008 * <11> X DPLL 2 Missed Clocks status (RO)
5009 * <10> X DPLL 1 Missed Clock status (RO)
5010 * <9..8> 00 DPLL Resync on rising and falling edges (RW)
5011 * <7> X SDLC Loop On status (RO)
5012 * <6> X SDLC Loop Send status (RO)
5013 * <5> 1 Bypass counters for TxClk and RxClk (RW)
5014 * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
5015 * <1..0> 00 reserved
5016 *
5017 * 0000 0000 0010 0000 = 0x0020
5018 */
5019
5020 usc_OutReg( info, CCSR, 0x1020 );
5021
5022
5023 if ( info->params.flags & HDLC_FLAG_AUTO_CTS ) {
5024 usc_OutReg( info, SICR,
5025 (u16)(usc_InReg(info,SICR) | SICR_CTS_INACTIVE) );
5026 }
5027
5028
5029 /* enable Master Interrupt Enable bit (MIE) */
5030 usc_EnableMasterIrqBit( info );
5031
Alexandru Juncue06922a2013-07-27 11:14:39 +03005032 usc_ClearIrqPendingBits( info, RECEIVE_STATUS | RECEIVE_DATA |
5033 TRANSMIT_STATUS | TRANSMIT_DATA | MISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
5035 /* arm RCC underflow interrupt */
5036 usc_OutReg(info, SICR, (u16)(usc_InReg(info,SICR) | BIT3));
5037 usc_EnableInterrupts(info, MISC);
5038
5039 info->mbre_bit = 0;
5040 outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
5041 usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
5042 info->mbre_bit = BIT8;
5043 outw( BIT8, info->io_base ); /* set Master Bus Enable (DCAR) */
5044
5045 if (info->bus_type == MGSL_BUS_TYPE_ISA) {
5046 /* Enable DMAEN (Port 7, Bit 14) */
5047 /* This connects the DMA request signal to the ISA bus */
5048 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) & ~BIT14));
5049 }
5050
5051 /* DMA Control Register (DCR)
5052 *
5053 * <15..14> 10 Priority mode = Alternating Tx/Rx
5054 * 01 Rx has priority
5055 * 00 Tx has priority
5056 *
5057 * <13> 1 Enable Priority Preempt per DCR<15..14>
5058 * (WARNING DCR<11..10> must be 00 when this is 1)
5059 * 0 Choose activate channel per DCR<11..10>
5060 *
5061 * <12> 0 Little Endian for Array/List
5062 * <11..10> 00 Both Channels can use each bus grant
5063 * <9..6> 0000 reserved
5064 * <5> 0 7 CLK - Minimum Bus Re-request Interval
5065 * <4> 0 1 = drive D/C and S/D pins
5066 * <3> 1 1 = Add one wait state to all DMA cycles.
5067 * <2> 0 1 = Strobe /UAS on every transfer.
5068 * <1..0> 11 Addr incrementing only affects LS24 bits
5069 *
5070 * 0110 0000 0000 1011 = 0x600b
5071 */
5072
5073 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5074 /* PCI adapter does not need DMA wait state */
5075 usc_OutDmaReg( info, DCR, 0xa00b );
5076 }
5077 else
5078 usc_OutDmaReg( info, DCR, 0x800b );
5079
5080
5081 /* Receive DMA mode Register (RDMR)
5082 *
5083 * <15..14> 11 DMA mode = Linked List Buffer mode
5084 * <13> 1 RSBinA/L = store Rx status Block in Arrary/List entry
5085 * <12> 1 Clear count of List Entry after fetching
5086 * <11..10> 00 Address mode = Increment
5087 * <9> 1 Terminate Buffer on RxBound
5088 * <8> 0 Bus Width = 16bits
5089 * <7..0> ? status Bits (write as 0s)
5090 *
5091 * 1111 0010 0000 0000 = 0xf200
5092 */
5093
5094 usc_OutDmaReg( info, RDMR, 0xf200 );
5095
5096
5097 /* Transmit DMA mode Register (TDMR)
5098 *
5099 * <15..14> 11 DMA mode = Linked List Buffer mode
5100 * <13> 1 TCBinA/L = fetch Tx Control Block from List entry
5101 * <12> 1 Clear count of List Entry after fetching
5102 * <11..10> 00 Address mode = Increment
5103 * <9> 1 Terminate Buffer on end of frame
5104 * <8> 0 Bus Width = 16bits
5105 * <7..0> ? status Bits (Read Only so write as 0)
5106 *
5107 * 1111 0010 0000 0000 = 0xf200
5108 */
5109
5110 usc_OutDmaReg( info, TDMR, 0xf200 );
5111
5112
5113 /* DMA Interrupt Control Register (DICR)
5114 *
5115 * <15> 1 DMA Interrupt Enable
5116 * <14> 0 1 = Disable IEO from USC
5117 * <13> 0 1 = Don't provide vector during IntAck
5118 * <12> 1 1 = Include status in Vector
5119 * <10..2> 0 reserved, Must be 0s
5120 * <1> 0 1 = Rx DMA Interrupt Enabled
5121 * <0> 0 1 = Tx DMA Interrupt Enabled
5122 *
5123 * 1001 0000 0000 0000 = 0x9000
5124 */
5125
5126 usc_OutDmaReg( info, DICR, 0x9000 );
5127
5128 usc_InDmaReg( info, RDMR ); /* clear pending receive DMA IRQ bits */
5129 usc_InDmaReg( info, TDMR ); /* clear pending transmit DMA IRQ bits */
5130 usc_OutDmaReg( info, CDIR, 0x0303 ); /* clear IUS and Pending for Tx and Rx */
5131
5132 /* Channel Control Register (CCR)
5133 *
5134 * <15..14> 10 Use 32-bit Tx Control Blocks (TCBs)
5135 * <13> 0 Trigger Tx on SW Command Disabled
5136 * <12> 0 Flag Preamble Disabled
5137 * <11..10> 00 Preamble Length
5138 * <9..8> 00 Preamble Pattern
5139 * <7..6> 10 Use 32-bit Rx status Blocks (RSBs)
5140 * <5> 0 Trigger Rx on SW Command Disabled
5141 * <4..0> 0 reserved
5142 *
5143 * 1000 0000 1000 0000 = 0x8080
5144 */
5145
5146 RegValue = 0x8080;
5147
5148 switch ( info->params.preamble_length ) {
5149 case HDLC_PREAMBLE_LENGTH_16BITS: RegValue |= BIT10; break;
5150 case HDLC_PREAMBLE_LENGTH_32BITS: RegValue |= BIT11; break;
Alexandru Juncue06922a2013-07-27 11:14:39 +03005151 case HDLC_PREAMBLE_LENGTH_64BITS: RegValue |= BIT11 | BIT10; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 }
5153
5154 switch ( info->params.preamble ) {
Alexandru Juncue06922a2013-07-27 11:14:39 +03005155 case HDLC_PREAMBLE_PATTERN_FLAGS: RegValue |= BIT8 | BIT12; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 case HDLC_PREAMBLE_PATTERN_ONES: RegValue |= BIT8; break;
5157 case HDLC_PREAMBLE_PATTERN_10: RegValue |= BIT9; break;
Alexandru Juncue06922a2013-07-27 11:14:39 +03005158 case HDLC_PREAMBLE_PATTERN_01: RegValue |= BIT9 | BIT8; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 }
5160
5161 usc_OutReg( info, CCR, RegValue );
5162
5163
5164 /*
5165 * Burst/Dwell Control Register
5166 *
5167 * <15..8> 0x20 Maximum number of transfers per bus grant
5168 * <7..0> 0x00 Maximum number of clock cycles per bus grant
5169 */
5170
5171 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5172 /* don't limit bus occupancy on PCI adapter */
5173 usc_OutDmaReg( info, BDCR, 0x0000 );
5174 }
5175 else
5176 usc_OutDmaReg( info, BDCR, 0x2000 );
5177
5178 usc_stop_transmitter(info);
5179 usc_stop_receiver(info);
5180
5181} /* end of usc_set_sdlc_mode() */
5182
5183/* usc_enable_loopback()
5184 *
5185 * Set the 16C32 for internal loopback mode.
5186 * The TxCLK and RxCLK signals are generated from the BRG0 and
5187 * the TxD is looped back to the RxD internally.
5188 *
5189 * Arguments: info pointer to device instance data
5190 * enable 1 = enable loopback, 0 = disable
5191 * Return Value: None
5192 */
5193static void usc_enable_loopback(struct mgsl_struct *info, int enable)
5194{
5195 if (enable) {
5196 /* blank external TXD output */
Alexandru Juncue06922a2013-07-27 11:14:39 +03005197 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) | (BIT7 | BIT6));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198
5199 /* Clock mode Control Register (CMCR)
5200 *
5201 * <15..14> 00 counter 1 Disabled
5202 * <13..12> 00 counter 0 Disabled
5203 * <11..10> 11 BRG1 Input is TxC Pin
5204 * <9..8> 11 BRG0 Input is TxC Pin
5205 * <7..6> 01 DPLL Input is BRG1 Output
5206 * <5..3> 100 TxCLK comes from BRG0
5207 * <2..0> 100 RxCLK comes from BRG0
5208 *
5209 * 0000 1111 0110 0100 = 0x0f64
5210 */
5211
5212 usc_OutReg( info, CMCR, 0x0f64 );
5213
5214 /* Write 16-bit Time Constant for BRG0 */
5215 /* use clock speed if available, otherwise use 8 for diagnostics */
5216 if (info->params.clock_speed) {
5217 if (info->bus_type == MGSL_BUS_TYPE_PCI)
5218 usc_OutReg(info, TC0R, (u16)((11059200/info->params.clock_speed)-1));
5219 else
5220 usc_OutReg(info, TC0R, (u16)((14745600/info->params.clock_speed)-1));
5221 } else
5222 usc_OutReg(info, TC0R, (u16)8);
5223
5224 /* Hardware Configuration Register (HCR) Clear Bit 1, BRG0
5225 mode = Continuous Set Bit 0 to enable BRG0. */
5226 usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
5227
5228 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
5229 usc_OutReg(info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004));
5230
5231 /* set Internal Data loopback mode */
5232 info->loopback_bits = 0x300;
5233 outw( 0x0300, info->io_base + CCAR );
5234 } else {
5235 /* enable external TXD output */
Alexandru Juncue06922a2013-07-27 11:14:39 +03005236 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) & ~(BIT7 | BIT6));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237
5238 /* clear Internal Data loopback mode */
5239 info->loopback_bits = 0;
5240 outw( 0,info->io_base + CCAR );
5241 }
5242
5243} /* end of usc_enable_loopback() */
5244
5245/* usc_enable_aux_clock()
5246 *
5247 * Enabled the AUX clock output at the specified frequency.
5248 *
5249 * Arguments:
5250 *
5251 * info pointer to device extension
5252 * data_rate data rate of clock in bits per second
5253 * A data rate of 0 disables the AUX clock.
5254 *
5255 * Return Value: None
5256 */
5257static void usc_enable_aux_clock( struct mgsl_struct *info, u32 data_rate )
5258{
5259 u32 XtalSpeed;
5260 u16 Tc;
5261
5262 if ( data_rate ) {
5263 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
5264 XtalSpeed = 11059200;
5265 else
5266 XtalSpeed = 14745600;
5267
5268
5269 /* Tc = (Xtal/Speed) - 1 */
5270 /* If twice the remainder of (Xtal/Speed) is greater than Speed */
5271 /* then rounding up gives a more precise time constant. Instead */
5272 /* of rounding up and then subtracting 1 we just don't subtract */
5273 /* the one in this case. */
5274
5275
5276 Tc = (u16)(XtalSpeed/data_rate);
5277 if ( !(((XtalSpeed % data_rate) * 2) / data_rate) )
5278 Tc--;
5279
5280 /* Write 16-bit Time Constant for BRG0 */
5281 usc_OutReg( info, TC0R, Tc );
5282
5283 /*
5284 * Hardware Configuration Register (HCR)
5285 * Clear Bit 1, BRG0 mode = Continuous
5286 * Set Bit 0 to enable BRG0.
5287 */
5288
5289 usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
5290
5291 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
5292 usc_OutReg( info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
5293 } else {
5294 /* data rate == 0 so turn off BRG0 */
5295 usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
5296 }
5297
5298} /* end of usc_enable_aux_clock() */
5299
5300/*
5301 *
5302 * usc_process_rxoverrun_sync()
5303 *
5304 * This function processes a receive overrun by resetting the
5305 * receive DMA buffers and issuing a Purge Rx FIFO command
5306 * to allow the receiver to continue receiving.
5307 *
5308 * Arguments:
5309 *
5310 * info pointer to device extension
5311 *
5312 * Return Value: None
5313 */
5314static void usc_process_rxoverrun_sync( struct mgsl_struct *info )
5315{
5316 int start_index;
5317 int end_index;
5318 int frame_start_index;
Joe Perches0fab6de2008-04-28 02:14:02 -07005319 bool start_of_frame_found = false;
5320 bool end_of_frame_found = false;
5321 bool reprogram_dma = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
5323 DMABUFFERENTRY *buffer_list = info->rx_buffer_list;
5324 u32 phys_addr;
5325
5326 usc_DmaCmd( info, DmaCmd_PauseRxChannel );
5327 usc_RCmd( info, RCmd_EnterHuntmode );
5328 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5329
5330 /* CurrentRxBuffer points to the 1st buffer of the next */
5331 /* possibly available receive frame. */
5332
5333 frame_start_index = start_index = end_index = info->current_rx_buffer;
5334
5335 /* Search for an unfinished string of buffers. This means */
5336 /* that a receive frame started (at least one buffer with */
5337 /* count set to zero) but there is no terminiting buffer */
5338 /* (status set to non-zero). */
5339
5340 while( !buffer_list[end_index].count )
5341 {
5342 /* Count field has been reset to zero by 16C32. */
5343 /* This buffer is currently in use. */
5344
5345 if ( !start_of_frame_found )
5346 {
Joe Perches0fab6de2008-04-28 02:14:02 -07005347 start_of_frame_found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 frame_start_index = end_index;
Joe Perches0fab6de2008-04-28 02:14:02 -07005349 end_of_frame_found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 }
5351
5352 if ( buffer_list[end_index].status )
5353 {
5354 /* Status field has been set by 16C32. */
5355 /* This is the last buffer of a received frame. */
5356
5357 /* We want to leave the buffers for this frame intact. */
5358 /* Move on to next possible frame. */
5359
Joe Perches0fab6de2008-04-28 02:14:02 -07005360 start_of_frame_found = false;
5361 end_of_frame_found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 }
5363
5364 /* advance to next buffer entry in linked list */
5365 end_index++;
5366 if ( end_index == info->rx_buffer_count )
5367 end_index = 0;
5368
5369 if ( start_index == end_index )
5370 {
5371 /* The entire list has been searched with all Counts == 0 and */
5372 /* all Status == 0. The receive buffers are */
5373 /* completely screwed, reset all receive buffers! */
5374 mgsl_reset_rx_dma_buffers( info );
5375 frame_start_index = 0;
Joe Perches0fab6de2008-04-28 02:14:02 -07005376 start_of_frame_found = false;
5377 reprogram_dma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 break;
5379 }
5380 }
5381
5382 if ( start_of_frame_found && !end_of_frame_found )
5383 {
5384 /* There is an unfinished string of receive DMA buffers */
5385 /* as a result of the receiver overrun. */
5386
5387 /* Reset the buffers for the unfinished frame */
5388 /* and reprogram the receive DMA controller to start */
5389 /* at the 1st buffer of unfinished frame. */
5390
5391 start_index = frame_start_index;
5392
5393 do
5394 {
5395 *((unsigned long *)&(info->rx_buffer_list[start_index++].count)) = DMABUFFERSIZE;
5396
5397 /* Adjust index for wrap around. */
5398 if ( start_index == info->rx_buffer_count )
5399 start_index = 0;
5400
5401 } while( start_index != end_index );
5402
Joe Perches0fab6de2008-04-28 02:14:02 -07005403 reprogram_dma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 }
5405
5406 if ( reprogram_dma )
5407 {
5408 usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
5409 usc_ClearIrqPendingBits(info, RECEIVE_DATA|RECEIVE_STATUS);
5410 usc_UnlatchRxstatusBits(info, RECEIVE_DATA|RECEIVE_STATUS);
5411
5412 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
5413
5414 /* This empties the receive FIFO and loads the RCC with RCLR */
5415 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5416
5417 /* program 16C32 with physical address of 1st DMA buffer entry */
5418 phys_addr = info->rx_buffer_list[frame_start_index].phys_entry;
5419 usc_OutDmaReg( info, NRARL, (u16)phys_addr );
5420 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
5421
5422 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
Alexandru Juncue06922a2013-07-27 11:14:39 +03005423 usc_ClearIrqPendingBits( info, RECEIVE_DATA | RECEIVE_STATUS );
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 usc_EnableInterrupts( info, RECEIVE_STATUS );
5425
5426 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
5427 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
5428
Alexandru Juncue06922a2013-07-27 11:14:39 +03005429 usc_OutDmaReg( info, RDIAR, BIT3 | BIT2 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
5431 usc_DmaCmd( info, DmaCmd_InitRxChannel );
5432 if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
5433 usc_EnableReceiver(info,ENABLE_AUTO_DCD);
5434 else
5435 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5436 }
5437 else
5438 {
5439 /* This empties the receive FIFO and loads the RCC with RCLR */
5440 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5441 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5442 }
5443
5444} /* end of usc_process_rxoverrun_sync() */
5445
5446/* usc_stop_receiver()
5447 *
5448 * Disable USC receiver
5449 *
5450 * Arguments: info pointer to device instance data
5451 * Return Value: None
5452 */
5453static void usc_stop_receiver( struct mgsl_struct *info )
5454{
5455 if (debug_level >= DEBUG_LEVEL_ISR)
5456 printk("%s(%d):usc_stop_receiver(%s)\n",
5457 __FILE__,__LINE__, info->device_name );
5458
5459 /* Disable receive DMA channel. */
5460 /* This also disables receive DMA channel interrupts */
5461 usc_DmaCmd( info, DmaCmd_ResetRxChannel );
5462
5463 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
Alexandru Juncue06922a2013-07-27 11:14:39 +03005464 usc_ClearIrqPendingBits( info, RECEIVE_DATA | RECEIVE_STATUS );
5465 usc_DisableInterrupts( info, RECEIVE_DATA | RECEIVE_STATUS );
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
5467 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
5468
5469 /* This empties the receive FIFO and loads the RCC with RCLR */
5470 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5471 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5472
Joe Perches0fab6de2008-04-28 02:14:02 -07005473 info->rx_enabled = false;
5474 info->rx_overflow = false;
5475 info->rx_rcc_underrun = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
5477} /* end of stop_receiver() */
5478
5479/* usc_start_receiver()
5480 *
5481 * Enable the USC receiver
5482 *
5483 * Arguments: info pointer to device instance data
5484 * Return Value: None
5485 */
5486static void usc_start_receiver( struct mgsl_struct *info )
5487{
5488 u32 phys_addr;
5489
5490 if (debug_level >= DEBUG_LEVEL_ISR)
5491 printk("%s(%d):usc_start_receiver(%s)\n",
5492 __FILE__,__LINE__, info->device_name );
5493
5494 mgsl_reset_rx_dma_buffers( info );
5495 usc_stop_receiver( info );
5496
5497 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5498 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5499
5500 if ( info->params.mode == MGSL_MODE_HDLC ||
5501 info->params.mode == MGSL_MODE_RAW ) {
5502 /* DMA mode Transfers */
5503 /* Program the DMA controller. */
5504 /* Enable the DMA controller end of buffer interrupt. */
5505
5506 /* program 16C32 with physical address of 1st DMA buffer entry */
5507 phys_addr = info->rx_buffer_list[0].phys_entry;
5508 usc_OutDmaReg( info, NRARL, (u16)phys_addr );
5509 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
5510
5511 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
Alexandru Juncue06922a2013-07-27 11:14:39 +03005512 usc_ClearIrqPendingBits( info, RECEIVE_DATA | RECEIVE_STATUS );
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 usc_EnableInterrupts( info, RECEIVE_STATUS );
5514
5515 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
5516 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
5517
Alexandru Juncue06922a2013-07-27 11:14:39 +03005518 usc_OutDmaReg( info, RDIAR, BIT3 | BIT2 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
5520 usc_DmaCmd( info, DmaCmd_InitRxChannel );
5521 if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
5522 usc_EnableReceiver(info,ENABLE_AUTO_DCD);
5523 else
5524 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5525 } else {
5526 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
Alexandru Juncue06922a2013-07-27 11:14:39 +03005527 usc_ClearIrqPendingBits(info, RECEIVE_DATA | RECEIVE_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 usc_EnableInterrupts(info, RECEIVE_DATA);
5529
5530 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5531 usc_RCmd( info, RCmd_EnterHuntmode );
5532
5533 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5534 }
5535
5536 usc_OutReg( info, CCSR, 0x1020 );
5537
Joe Perches0fab6de2008-04-28 02:14:02 -07005538 info->rx_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
5540} /* end of usc_start_receiver() */
5541
5542/* usc_start_transmitter()
5543 *
5544 * Enable the USC transmitter and send a transmit frame if
5545 * one is loaded in the DMA buffers.
5546 *
5547 * Arguments: info pointer to device instance data
5548 * Return Value: None
5549 */
5550static void usc_start_transmitter( struct mgsl_struct *info )
5551{
5552 u32 phys_addr;
5553 unsigned int FrameSize;
5554
5555 if (debug_level >= DEBUG_LEVEL_ISR)
5556 printk("%s(%d):usc_start_transmitter(%s)\n",
5557 __FILE__,__LINE__, info->device_name );
5558
5559 if ( info->xmit_cnt ) {
5560
5561 /* If auto RTS enabled and RTS is inactive, then assert */
5562 /* RTS and set a flag indicating that the driver should */
5563 /* negate RTS when the transmission completes. */
5564
Joe Perches0fab6de2008-04-28 02:14:02 -07005565 info->drop_rts_on_tx_done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
5567 if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
5568 usc_get_serial_signals( info );
5569 if ( !(info->serial_signals & SerialSignal_RTS) ) {
5570 info->serial_signals |= SerialSignal_RTS;
5571 usc_set_serial_signals( info );
Joe Perches0fab6de2008-04-28 02:14:02 -07005572 info->drop_rts_on_tx_done = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 }
5574 }
5575
5576
5577 if ( info->params.mode == MGSL_MODE_ASYNC ) {
5578 if ( !info->tx_active ) {
5579 usc_UnlatchTxstatusBits(info, TXSTATUS_ALL);
5580 usc_ClearIrqPendingBits(info, TRANSMIT_STATUS + TRANSMIT_DATA);
5581 usc_EnableInterrupts(info, TRANSMIT_DATA);
5582 usc_load_txfifo(info);
5583 }
5584 } else {
5585 /* Disable transmit DMA controller while programming. */
5586 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
5587
5588 /* Transmit DMA buffer is loaded, so program USC */
5589 /* to send the frame contained in the buffers. */
5590
5591 FrameSize = info->tx_buffer_list[info->start_tx_dma_buffer].rcc;
5592
5593 /* if operating in Raw sync mode, reset the rcc component
5594 * of the tx dma buffer entry, otherwise, the serial controller
5595 * will send a closing sync char after this count.
5596 */
5597 if ( info->params.mode == MGSL_MODE_RAW )
5598 info->tx_buffer_list[info->start_tx_dma_buffer].rcc = 0;
5599
5600 /* Program the Transmit Character Length Register (TCLR) */
5601 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
5602 usc_OutReg( info, TCLR, (u16)FrameSize );
5603
5604 usc_RTCmd( info, RTCmd_PurgeTxFifo );
5605
5606 /* Program the address of the 1st DMA Buffer Entry in linked list */
5607 phys_addr = info->tx_buffer_list[info->start_tx_dma_buffer].phys_entry;
5608 usc_OutDmaReg( info, NTARL, (u16)phys_addr );
5609 usc_OutDmaReg( info, NTARU, (u16)(phys_addr >> 16) );
5610
5611 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
5612 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
5613 usc_EnableInterrupts( info, TRANSMIT_STATUS );
5614
5615 if ( info->params.mode == MGSL_MODE_RAW &&
5616 info->num_tx_dma_buffers > 1 ) {
5617 /* When running external sync mode, attempt to 'stream' transmit */
5618 /* by filling tx dma buffers as they become available. To do this */
5619 /* we need to enable Tx DMA EOB Status interrupts : */
5620 /* */
5621 /* 1. Arm End of Buffer (EOB) Transmit DMA Interrupt (BIT2 of TDIAR) */
5622 /* 2. Enable Transmit DMA Interrupts (BIT0 of DICR) */
5623
5624 usc_OutDmaReg( info, TDIAR, BIT2|BIT3 );
5625 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT0) );
5626 }
5627
5628 /* Initialize Transmit DMA Channel */
5629 usc_DmaCmd( info, DmaCmd_InitTxChannel );
5630
5631 usc_TCmd( info, TCmd_SendFrame );
5632
Jiri Slaby40565f12007-02-12 00:52:31 -08005633 mod_timer(&info->tx_timer, jiffies +
5634 msecs_to_jiffies(5000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 }
Joe Perches0fab6de2008-04-28 02:14:02 -07005636 info->tx_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 }
5638
5639 if ( !info->tx_enabled ) {
Joe Perches0fab6de2008-04-28 02:14:02 -07005640 info->tx_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 if ( info->params.flags & HDLC_FLAG_AUTO_CTS )
5642 usc_EnableTransmitter(info,ENABLE_AUTO_CTS);
5643 else
5644 usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
5645 }
5646
5647} /* end of usc_start_transmitter() */
5648
5649/* usc_stop_transmitter()
5650 *
5651 * Stops the transmitter and DMA
5652 *
5653 * Arguments: info pointer to device isntance data
5654 * Return Value: None
5655 */
5656static void usc_stop_transmitter( struct mgsl_struct *info )
5657{
5658 if (debug_level >= DEBUG_LEVEL_ISR)
5659 printk("%s(%d):usc_stop_transmitter(%s)\n",
5660 __FILE__,__LINE__, info->device_name );
5661
5662 del_timer(&info->tx_timer);
5663
5664 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
5665 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA );
5666 usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA );
5667
5668 usc_EnableTransmitter(info,DISABLE_UNCONDITIONAL);
5669 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
5670 usc_RTCmd( info, RTCmd_PurgeTxFifo );
5671
Joe Perches0fab6de2008-04-28 02:14:02 -07005672 info->tx_enabled = false;
5673 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
5675} /* end of usc_stop_transmitter() */
5676
5677/* usc_load_txfifo()
5678 *
5679 * Fill the transmit FIFO until the FIFO is full or
5680 * there is no more data to load.
5681 *
5682 * Arguments: info pointer to device extension (instance data)
5683 * Return Value: None
5684 */
5685static void usc_load_txfifo( struct mgsl_struct *info )
5686{
5687 int Fifocount;
5688 u8 TwoBytes[2];
5689
5690 if ( !info->xmit_cnt && !info->x_char )
5691 return;
5692
5693 /* Select transmit FIFO status readback in TICR */
5694 usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
5695
5696 /* load the Transmit FIFO until FIFOs full or all data sent */
5697
5698 while( (Fifocount = usc_InReg(info, TICR) >> 8) && info->xmit_cnt ) {
5699 /* there is more space in the transmit FIFO and */
5700 /* there is more data in transmit buffer */
5701
5702 if ( (info->xmit_cnt > 1) && (Fifocount > 1) && !info->x_char ) {
5703 /* write a 16-bit word from transmit buffer to 16C32 */
5704
5705 TwoBytes[0] = info->xmit_buf[info->xmit_tail++];
5706 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5707 TwoBytes[1] = info->xmit_buf[info->xmit_tail++];
5708 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5709
5710 outw( *((u16 *)TwoBytes), info->io_base + DATAREG);
5711
5712 info->xmit_cnt -= 2;
5713 info->icount.tx += 2;
5714 } else {
5715 /* only 1 byte left to transmit or 1 FIFO slot left */
5716
5717 outw( (inw( info->io_base + CCAR) & 0x0780) | (TDR+LSBONLY),
5718 info->io_base + CCAR );
5719
5720 if (info->x_char) {
5721 /* transmit pending high priority char */
5722 outw( info->x_char,info->io_base + CCAR );
5723 info->x_char = 0;
5724 } else {
5725 outw( info->xmit_buf[info->xmit_tail++],info->io_base + CCAR );
5726 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5727 info->xmit_cnt--;
5728 }
5729 info->icount.tx++;
5730 }
5731 }
5732
5733} /* end of usc_load_txfifo() */
5734
5735/* usc_reset()
5736 *
5737 * Reset the adapter to a known state and prepare it for further use.
5738 *
5739 * Arguments: info pointer to device instance data
5740 * Return Value: None
5741 */
5742static void usc_reset( struct mgsl_struct *info )
5743{
5744 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5745 int i;
5746 u32 readval;
5747
5748 /* Set BIT30 of Misc Control Register */
5749 /* (Local Control Register 0x50) to force reset of USC. */
5750
5751 volatile u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
5752 u32 *LCR0BRDR = (u32 *)(info->lcr_base + 0x28);
5753
5754 info->misc_ctrl_value |= BIT30;
5755 *MiscCtrl = info->misc_ctrl_value;
5756
5757 /*
5758 * Force at least 170ns delay before clearing
5759 * reset bit. Each read from LCR takes at least
5760 * 30ns so 10 times for 300ns to be safe.
5761 */
5762 for(i=0;i<10;i++)
5763 readval = *MiscCtrl;
5764
5765 info->misc_ctrl_value &= ~BIT30;
5766 *MiscCtrl = info->misc_ctrl_value;
5767
5768 *LCR0BRDR = BUS_DESCRIPTOR(
5769 1, // Write Strobe Hold (0-3)
5770 2, // Write Strobe Delay (0-3)
5771 2, // Read Strobe Delay (0-3)
5772 0, // NWDD (Write data-data) (0-3)
5773 4, // NWAD (Write Addr-data) (0-31)
5774 0, // NXDA (Read/Write Data-Addr) (0-3)
5775 0, // NRDD (Read Data-Data) (0-3)
5776 5 // NRAD (Read Addr-Data) (0-31)
5777 );
5778 } else {
5779 /* do HW reset */
5780 outb( 0,info->io_base + 8 );
5781 }
5782
5783 info->mbre_bit = 0;
5784 info->loopback_bits = 0;
5785 info->usc_idle_mode = 0;
5786
5787 /*
5788 * Program the Bus Configuration Register (BCR)
5789 *
5790 * <15> 0 Don't use separate address
5791 * <14..6> 0 reserved
5792 * <5..4> 00 IAckmode = Default, don't care
5793 * <3> 1 Bus Request Totem Pole output
5794 * <2> 1 Use 16 Bit data bus
5795 * <1> 0 IRQ Totem Pole output
5796 * <0> 0 Don't Shift Right Addr
5797 *
5798 * 0000 0000 0000 1100 = 0x000c
5799 *
5800 * By writing to io_base + SDPIN the Wait/Ack pin is
5801 * programmed to work as a Wait pin.
5802 */
5803
5804 outw( 0x000c,info->io_base + SDPIN );
5805
5806
5807 outw( 0,info->io_base );
5808 outw( 0,info->io_base + CCAR );
5809
5810 /* select little endian byte ordering */
5811 usc_RTCmd( info, RTCmd_SelectLittleEndian );
5812
5813
5814 /* Port Control Register (PCR)
5815 *
5816 * <15..14> 11 Port 7 is Output (~DMAEN, Bit 14 : 0 = Enabled)
5817 * <13..12> 11 Port 6 is Output (~INTEN, Bit 12 : 0 = Enabled)
5818 * <11..10> 00 Port 5 is Input (No Connect, Don't Care)
5819 * <9..8> 00 Port 4 is Input (No Connect, Don't Care)
5820 * <7..6> 11 Port 3 is Output (~RTS, Bit 6 : 0 = Enabled )
5821 * <5..4> 11 Port 2 is Output (~DTR, Bit 4 : 0 = Enabled )
5822 * <3..2> 01 Port 1 is Input (Dedicated RxC)
5823 * <1..0> 01 Port 0 is Input (Dedicated TxC)
5824 *
5825 * 1111 0000 1111 0101 = 0xf0f5
5826 */
5827
5828 usc_OutReg( info, PCR, 0xf0f5 );
5829
5830
5831 /*
5832 * Input/Output Control Register
5833 *
5834 * <15..14> 00 CTS is active low input
5835 * <13..12> 00 DCD is active low input
5836 * <11..10> 00 TxREQ pin is input (DSR)
5837 * <9..8> 00 RxREQ pin is input (RI)
5838 * <7..6> 00 TxD is output (Transmit Data)
5839 * <5..3> 000 TxC Pin in Input (14.7456MHz Clock)
5840 * <2..0> 100 RxC is Output (drive with BRG0)
5841 *
5842 * 0000 0000 0000 0100 = 0x0004
5843 */
5844
5845 usc_OutReg( info, IOCR, 0x0004 );
5846
5847} /* end of usc_reset() */
5848
5849/* usc_set_async_mode()
5850 *
5851 * Program adapter for asynchronous communications.
5852 *
5853 * Arguments: info pointer to device instance data
5854 * Return Value: None
5855 */
5856static void usc_set_async_mode( struct mgsl_struct *info )
5857{
5858 u16 RegValue;
5859
5860 /* disable interrupts while programming USC */
5861 usc_DisableMasterIrqBit( info );
5862
5863 outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
5864 usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
5865
5866 usc_loopback_frame( info );
5867
5868 /* Channel mode Register (CMR)
5869 *
5870 * <15..14> 00 Tx Sub modes, 00 = 1 Stop Bit
5871 * <13..12> 00 00 = 16X Clock
5872 * <11..8> 0000 Transmitter mode = Asynchronous
5873 * <7..6> 00 reserved?
5874 * <5..4> 00 Rx Sub modes, 00 = 16X Clock
5875 * <3..0> 0000 Receiver mode = Asynchronous
5876 *
5877 * 0000 0000 0000 0000 = 0x0
5878 */
5879
5880 RegValue = 0;
5881 if ( info->params.stop_bits != 1 )
5882 RegValue |= BIT14;
5883 usc_OutReg( info, CMR, RegValue );
5884
5885
5886 /* Receiver mode Register (RMR)
5887 *
5888 * <15..13> 000 encoding = None
5889 * <12..08> 00000 reserved (Sync Only)
5890 * <7..6> 00 Even parity
5891 * <5> 0 parity disabled
5892 * <4..2> 000 Receive Char Length = 8 bits
5893 * <1..0> 00 Disable Receiver
5894 *
5895 * 0000 0000 0000 0000 = 0x0
5896 */
5897
5898 RegValue = 0;
5899
5900 if ( info->params.data_bits != 8 )
Alexandru Juncue06922a2013-07-27 11:14:39 +03005901 RegValue |= BIT4 | BIT3 | BIT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
5903 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5904 RegValue |= BIT5;
5905 if ( info->params.parity != ASYNC_PARITY_ODD )
5906 RegValue |= BIT6;
5907 }
5908
5909 usc_OutReg( info, RMR, RegValue );
5910
5911
5912 /* Set IRQ trigger level */
5913
5914 usc_RCmd( info, RCmd_SelectRicrIntLevel );
5915
5916
5917 /* Receive Interrupt Control Register (RICR)
5918 *
5919 * <15..8> ? RxFIFO IRQ Request Level
5920 *
5921 * Note: For async mode the receive FIFO level must be set
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08005922 * to 0 to avoid the situation where the FIFO contains fewer bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 * than the trigger level and no more data is expected.
5924 *
5925 * <7> 0 Exited Hunt IA (Interrupt Arm)
5926 * <6> 0 Idle Received IA
5927 * <5> 0 Break/Abort IA
5928 * <4> 0 Rx Bound IA
5929 * <3> 0 Queued status reflects oldest byte in FIFO
5930 * <2> 0 Abort/PE IA
5931 * <1> 0 Rx Overrun IA
5932 * <0> 0 Select TC0 value for readback
5933 *
5934 * 0000 0000 0100 0000 = 0x0000 + (FIFOLEVEL in MSB)
5935 */
5936
5937 usc_OutReg( info, RICR, 0x0000 );
5938
5939 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5940 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
5941
5942
5943 /* Transmit mode Register (TMR)
5944 *
5945 * <15..13> 000 encoding = None
5946 * <12..08> 00000 reserved (Sync Only)
5947 * <7..6> 00 Transmit parity Even
5948 * <5> 0 Transmit parity Disabled
5949 * <4..2> 000 Tx Char Length = 8 bits
5950 * <1..0> 00 Disable Transmitter
5951 *
5952 * 0000 0000 0000 0000 = 0x0
5953 */
5954
5955 RegValue = 0;
5956
5957 if ( info->params.data_bits != 8 )
Alexandru Juncue06922a2013-07-27 11:14:39 +03005958 RegValue |= BIT4 | BIT3 | BIT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
5960 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5961 RegValue |= BIT5;
5962 if ( info->params.parity != ASYNC_PARITY_ODD )
5963 RegValue |= BIT6;
5964 }
5965
5966 usc_OutReg( info, TMR, RegValue );
5967
5968 usc_set_txidle( info );
5969
5970
5971 /* Set IRQ trigger level */
5972
5973 usc_TCmd( info, TCmd_SelectTicrIntLevel );
5974
5975
5976 /* Transmit Interrupt Control Register (TICR)
5977 *
5978 * <15..8> ? Transmit FIFO IRQ Level
5979 * <7> 0 Present IA (Interrupt Arm)
5980 * <6> 1 Idle Sent IA
5981 * <5> 0 Abort Sent IA
5982 * <4> 0 EOF/EOM Sent IA
5983 * <3> 0 CRC Sent IA
5984 * <2> 0 1 = Wait for SW Trigger to Start Frame
5985 * <1> 0 Tx Underrun IA
5986 * <0> 0 TC0 constant on read back
5987 *
5988 * 0000 0000 0100 0000 = 0x0040
5989 */
5990
5991 usc_OutReg( info, TICR, 0x1f40 );
5992
5993 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
5994 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
5995
5996 usc_enable_async_clock( info, info->params.data_rate );
5997
5998
5999 /* Channel Control/status Register (CCSR)
6000 *
6001 * <15> X RCC FIFO Overflow status (RO)
6002 * <14> X RCC FIFO Not Empty status (RO)
6003 * <13> 0 1 = Clear RCC FIFO (WO)
6004 * <12> X DPLL in Sync status (RO)
6005 * <11> X DPLL 2 Missed Clocks status (RO)
6006 * <10> X DPLL 1 Missed Clock status (RO)
6007 * <9..8> 00 DPLL Resync on rising and falling edges (RW)
6008 * <7> X SDLC Loop On status (RO)
6009 * <6> X SDLC Loop Send status (RO)
6010 * <5> 1 Bypass counters for TxClk and RxClk (RW)
6011 * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
6012 * <1..0> 00 reserved
6013 *
6014 * 0000 0000 0010 0000 = 0x0020
6015 */
6016
6017 usc_OutReg( info, CCSR, 0x0020 );
6018
6019 usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA +
6020 RECEIVE_DATA + RECEIVE_STATUS );
6021
6022 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA +
6023 RECEIVE_DATA + RECEIVE_STATUS );
6024
6025 usc_EnableMasterIrqBit( info );
6026
6027 if (info->bus_type == MGSL_BUS_TYPE_ISA) {
6028 /* Enable INTEN (Port 6, Bit12) */
6029 /* This connects the IRQ request signal to the ISA bus */
6030 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12));
6031 }
6032
Paul Fulghum7c1fff52005-09-09 13:02:14 -07006033 if (info->params.loopback) {
6034 info->loopback_bits = 0x300;
6035 outw(0x0300, info->io_base + CCAR);
6036 }
6037
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038} /* end of usc_set_async_mode() */
6039
6040/* usc_loopback_frame()
6041 *
6042 * Loop back a small (2 byte) dummy SDLC frame.
6043 * Interrupts and DMA are NOT used. The purpose of this is to
6044 * clear any 'stale' status info left over from running in async mode.
6045 *
6046 * The 16C32 shows the strange behaviour of marking the 1st
6047 * received SDLC frame with a CRC error even when there is no
6048 * CRC error. To get around this a small dummy from of 2 bytes
6049 * is looped back when switching from async to sync mode.
6050 *
6051 * Arguments: info pointer to device instance data
6052 * Return Value: None
6053 */
6054static void usc_loopback_frame( struct mgsl_struct *info )
6055{
6056 int i;
6057 unsigned long oldmode = info->params.mode;
6058
6059 info->params.mode = MGSL_MODE_HDLC;
6060
6061 usc_DisableMasterIrqBit( info );
6062
6063 usc_set_sdlc_mode( info );
6064 usc_enable_loopback( info, 1 );
6065
6066 /* Write 16-bit Time Constant for BRG0 */
6067 usc_OutReg( info, TC0R, 0 );
6068
6069 /* Channel Control Register (CCR)
6070 *
6071 * <15..14> 00 Don't use 32-bit Tx Control Blocks (TCBs)
6072 * <13> 0 Trigger Tx on SW Command Disabled
6073 * <12> 0 Flag Preamble Disabled
6074 * <11..10> 00 Preamble Length = 8-Bits
6075 * <9..8> 01 Preamble Pattern = flags
6076 * <7..6> 10 Don't use 32-bit Rx status Blocks (RSBs)
6077 * <5> 0 Trigger Rx on SW Command Disabled
6078 * <4..0> 0 reserved
6079 *
6080 * 0000 0001 0000 0000 = 0x0100
6081 */
6082
6083 usc_OutReg( info, CCR, 0x0100 );
6084
6085 /* SETUP RECEIVER */
6086 usc_RTCmd( info, RTCmd_PurgeRxFifo );
6087 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
6088
6089 /* SETUP TRANSMITTER */
6090 /* Program the Transmit Character Length Register (TCLR) */
6091 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
6092 usc_OutReg( info, TCLR, 2 );
6093 usc_RTCmd( info, RTCmd_PurgeTxFifo );
6094
6095 /* unlatch Tx status bits, and start transmit channel. */
6096 usc_UnlatchTxstatusBits(info,TXSTATUS_ALL);
6097 outw(0,info->io_base + DATAREG);
6098
6099 /* ENABLE TRANSMITTER */
6100 usc_TCmd( info, TCmd_SendFrame );
6101 usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
6102
6103 /* WAIT FOR RECEIVE COMPLETE */
6104 for (i=0 ; i<1000 ; i++)
Alexandru Juncue06922a2013-07-27 11:14:39 +03006105 if (usc_InReg( info, RCSR ) & (BIT8 | BIT4 | BIT3 | BIT1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 break;
6107
6108 /* clear Internal Data loopback mode */
6109 usc_enable_loopback(info, 0);
6110
6111 usc_EnableMasterIrqBit(info);
6112
6113 info->params.mode = oldmode;
6114
6115} /* end of usc_loopback_frame() */
6116
6117/* usc_set_sync_mode() Programs the USC for SDLC communications.
6118 *
6119 * Arguments: info pointer to adapter info structure
6120 * Return Value: None
6121 */
6122static void usc_set_sync_mode( struct mgsl_struct *info )
6123{
6124 usc_loopback_frame( info );
6125 usc_set_sdlc_mode( info );
6126
6127 if (info->bus_type == MGSL_BUS_TYPE_ISA) {
6128 /* Enable INTEN (Port 6, Bit12) */
6129 /* This connects the IRQ request signal to the ISA bus */
6130 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12));
6131 }
6132
6133 usc_enable_aux_clock(info, info->params.clock_speed);
6134
6135 if (info->params.loopback)
6136 usc_enable_loopback(info,1);
6137
6138} /* end of mgsl_set_sync_mode() */
6139
6140/* usc_set_txidle() Set the HDLC idle mode for the transmitter.
6141 *
6142 * Arguments: info pointer to device instance data
6143 * Return Value: None
6144 */
6145static void usc_set_txidle( struct mgsl_struct *info )
6146{
6147 u16 usc_idle_mode = IDLEMODE_FLAGS;
6148
6149 /* Map API idle mode to USC register bits */
6150
6151 switch( info->idle_mode ){
6152 case HDLC_TXIDLE_FLAGS: usc_idle_mode = IDLEMODE_FLAGS; break;
6153 case HDLC_TXIDLE_ALT_ZEROS_ONES: usc_idle_mode = IDLEMODE_ALT_ONE_ZERO; break;
6154 case HDLC_TXIDLE_ZEROS: usc_idle_mode = IDLEMODE_ZERO; break;
6155 case HDLC_TXIDLE_ONES: usc_idle_mode = IDLEMODE_ONE; break;
6156 case HDLC_TXIDLE_ALT_MARK_SPACE: usc_idle_mode = IDLEMODE_ALT_MARK_SPACE; break;
6157 case HDLC_TXIDLE_SPACE: usc_idle_mode = IDLEMODE_SPACE; break;
6158 case HDLC_TXIDLE_MARK: usc_idle_mode = IDLEMODE_MARK; break;
6159 }
6160
6161 info->usc_idle_mode = usc_idle_mode;
6162 //usc_OutReg(info, TCSR, usc_idle_mode);
6163 info->tcsr_value &= ~IDLEMODE_MASK; /* clear idle mode bits */
6164 info->tcsr_value += usc_idle_mode;
6165 usc_OutReg(info, TCSR, info->tcsr_value);
6166
6167 /*
6168 * if SyncLink WAN adapter is running in external sync mode, the
6169 * transmitter has been set to Monosync in order to try to mimic
6170 * a true raw outbound bit stream. Monosync still sends an open/close
6171 * sync char at the start/end of a frame. Try to match those sync
6172 * patterns to the idle mode set here
6173 */
6174 if ( info->params.mode == MGSL_MODE_RAW ) {
6175 unsigned char syncpat = 0;
6176 switch( info->idle_mode ) {
6177 case HDLC_TXIDLE_FLAGS:
6178 syncpat = 0x7e;
6179 break;
6180 case HDLC_TXIDLE_ALT_ZEROS_ONES:
6181 syncpat = 0x55;
6182 break;
6183 case HDLC_TXIDLE_ZEROS:
6184 case HDLC_TXIDLE_SPACE:
6185 syncpat = 0x00;
6186 break;
6187 case HDLC_TXIDLE_ONES:
6188 case HDLC_TXIDLE_MARK:
6189 syncpat = 0xff;
6190 break;
6191 case HDLC_TXIDLE_ALT_MARK_SPACE:
6192 syncpat = 0xaa;
6193 break;
6194 }
6195
6196 usc_SetTransmitSyncChars(info,syncpat,syncpat);
6197 }
6198
6199} /* end of usc_set_txidle() */
6200
6201/* usc_get_serial_signals()
6202 *
6203 * Query the adapter for the state of the V24 status (input) signals.
6204 *
6205 * Arguments: info pointer to device instance data
6206 * Return Value: None
6207 */
6208static void usc_get_serial_signals( struct mgsl_struct *info )
6209{
6210 u16 status;
6211
Joe Perches9fe80742013-01-27 18:21:00 -08006212 /* clear all serial signals except RTS and DTR */
6213 info->serial_signals &= SerialSignal_RTS | SerialSignal_DTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
6215 /* Read the Misc Interrupt status Register (MISR) to get */
6216 /* the V24 status signals. */
6217
6218 status = usc_InReg( info, MISR );
6219
6220 /* set serial signal bits to reflect MISR */
6221
6222 if ( status & MISCSTATUS_CTS )
6223 info->serial_signals |= SerialSignal_CTS;
6224
6225 if ( status & MISCSTATUS_DCD )
6226 info->serial_signals |= SerialSignal_DCD;
6227
6228 if ( status & MISCSTATUS_RI )
6229 info->serial_signals |= SerialSignal_RI;
6230
6231 if ( status & MISCSTATUS_DSR )
6232 info->serial_signals |= SerialSignal_DSR;
6233
6234} /* end of usc_get_serial_signals() */
6235
6236/* usc_set_serial_signals()
6237 *
Joe Perches9fe80742013-01-27 18:21:00 -08006238 * Set the state of RTS and DTR based on contents of
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 * serial_signals member of device extension.
6240 *
6241 * Arguments: info pointer to device instance data
6242 * Return Value: None
6243 */
6244static void usc_set_serial_signals( struct mgsl_struct *info )
6245{
6246 u16 Control;
6247 unsigned char V24Out = info->serial_signals;
6248
6249 /* get the current value of the Port Control Register (PCR) */
6250
6251 Control = usc_InReg( info, PCR );
6252
6253 if ( V24Out & SerialSignal_RTS )
6254 Control &= ~(BIT6);
6255 else
6256 Control |= BIT6;
6257
6258 if ( V24Out & SerialSignal_DTR )
6259 Control &= ~(BIT4);
6260 else
6261 Control |= BIT4;
6262
6263 usc_OutReg( info, PCR, Control );
6264
6265} /* end of usc_set_serial_signals() */
6266
6267/* usc_enable_async_clock()
6268 *
6269 * Enable the async clock at the specified frequency.
6270 *
6271 * Arguments: info pointer to device instance data
6272 * data_rate data rate of clock in bps
6273 * 0 disables the AUX clock.
6274 * Return Value: None
6275 */
6276static void usc_enable_async_clock( struct mgsl_struct *info, u32 data_rate )
6277{
6278 if ( data_rate ) {
6279 /*
6280 * Clock mode Control Register (CMCR)
6281 *
6282 * <15..14> 00 counter 1 Disabled
6283 * <13..12> 00 counter 0 Disabled
6284 * <11..10> 11 BRG1 Input is TxC Pin
6285 * <9..8> 11 BRG0 Input is TxC Pin
6286 * <7..6> 01 DPLL Input is BRG1 Output
6287 * <5..3> 100 TxCLK comes from BRG0
6288 * <2..0> 100 RxCLK comes from BRG0
6289 *
6290 * 0000 1111 0110 0100 = 0x0f64
6291 */
6292
6293 usc_OutReg( info, CMCR, 0x0f64 );
6294
6295
6296 /*
6297 * Write 16-bit Time Constant for BRG0
6298 * Time Constant = (ClkSpeed / data_rate) - 1
6299 * ClkSpeed = 921600 (ISA), 691200 (PCI)
6300 */
6301
6302 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
6303 usc_OutReg( info, TC0R, (u16)((691200/data_rate) - 1) );
6304 else
6305 usc_OutReg( info, TC0R, (u16)((921600/data_rate) - 1) );
6306
6307
6308 /*
6309 * Hardware Configuration Register (HCR)
6310 * Clear Bit 1, BRG0 mode = Continuous
6311 * Set Bit 0 to enable BRG0.
6312 */
6313
6314 usc_OutReg( info, HCR,
6315 (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
6316
6317
6318 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
6319
6320 usc_OutReg( info, IOCR,
6321 (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
6322 } else {
6323 /* data rate == 0 so turn off BRG0 */
6324 usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
6325 }
6326
6327} /* end of usc_enable_async_clock() */
6328
6329/*
6330 * Buffer Structures:
6331 *
6332 * Normal memory access uses virtual addresses that can make discontiguous
6333 * physical memory pages appear to be contiguous in the virtual address
6334 * space (the processors memory mapping handles the conversions).
6335 *
6336 * DMA transfers require physically contiguous memory. This is because
6337 * the DMA system controller and DMA bus masters deal with memory using
6338 * only physical addresses.
6339 *
6340 * This causes a problem under Windows NT when large DMA buffers are
6341 * needed. Fragmentation of the nonpaged pool prevents allocations of
6342 * physically contiguous buffers larger than the PAGE_SIZE.
6343 *
6344 * However the 16C32 supports Bus Master Scatter/Gather DMA which
6345 * allows DMA transfers to physically discontiguous buffers. Information
6346 * about each data transfer buffer is contained in a memory structure
6347 * called a 'buffer entry'. A list of buffer entries is maintained
6348 * to track and control the use of the data transfer buffers.
6349 *
6350 * To support this strategy we will allocate sufficient PAGE_SIZE
6351 * contiguous memory buffers to allow for the total required buffer
6352 * space.
6353 *
6354 * The 16C32 accesses the list of buffer entries using Bus Master
6355 * DMA. Control information is read from the buffer entries by the
6356 * 16C32 to control data transfers. status information is written to
6357 * the buffer entries by the 16C32 to indicate the status of completed
6358 * transfers.
6359 *
6360 * The CPU writes control information to the buffer entries to control
6361 * the 16C32 and reads status information from the buffer entries to
6362 * determine information about received and transmitted frames.
6363 *
6364 * Because the CPU and 16C32 (adapter) both need simultaneous access
6365 * to the buffer entries, the buffer entry memory is allocated with
6366 * HalAllocateCommonBuffer(). This restricts the size of the buffer
6367 * entry list to PAGE_SIZE.
6368 *
6369 * The actual data buffers on the other hand will only be accessed
6370 * by the CPU or the adapter but not by both simultaneously. This allows
6371 * Scatter/Gather packet based DMA procedures for using physically
6372 * discontiguous pages.
6373 */
6374
6375/*
6376 * mgsl_reset_tx_dma_buffers()
6377 *
6378 * Set the count for all transmit buffers to 0 to indicate the
6379 * buffer is available for use and set the current buffer to the
6380 * first buffer. This effectively makes all buffers free and
6381 * discards any data in buffers.
6382 *
6383 * Arguments: info pointer to device instance data
6384 * Return Value: None
6385 */
6386static void mgsl_reset_tx_dma_buffers( struct mgsl_struct *info )
6387{
6388 unsigned int i;
6389
6390 for ( i = 0; i < info->tx_buffer_count; i++ ) {
6391 *((unsigned long *)&(info->tx_buffer_list[i].count)) = 0;
6392 }
6393
6394 info->current_tx_buffer = 0;
6395 info->start_tx_dma_buffer = 0;
6396 info->tx_dma_buffers_used = 0;
6397
6398 info->get_tx_holding_index = 0;
6399 info->put_tx_holding_index = 0;
6400 info->tx_holding_count = 0;
6401
6402} /* end of mgsl_reset_tx_dma_buffers() */
6403
6404/*
6405 * num_free_tx_dma_buffers()
6406 *
6407 * returns the number of free tx dma buffers available
6408 *
6409 * Arguments: info pointer to device instance data
6410 * Return Value: number of free tx dma buffers
6411 */
6412static int num_free_tx_dma_buffers(struct mgsl_struct *info)
6413{
6414 return info->tx_buffer_count - info->tx_dma_buffers_used;
6415}
6416
6417/*
6418 * mgsl_reset_rx_dma_buffers()
6419 *
6420 * Set the count for all receive buffers to DMABUFFERSIZE
6421 * and set the current buffer to the first buffer. This effectively
6422 * makes all buffers free and discards any data in buffers.
6423 *
6424 * Arguments: info pointer to device instance data
6425 * Return Value: None
6426 */
6427static void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info )
6428{
6429 unsigned int i;
6430
6431 for ( i = 0; i < info->rx_buffer_count; i++ ) {
6432 *((unsigned long *)&(info->rx_buffer_list[i].count)) = DMABUFFERSIZE;
6433// info->rx_buffer_list[i].count = DMABUFFERSIZE;
6434// info->rx_buffer_list[i].status = 0;
6435 }
6436
6437 info->current_rx_buffer = 0;
6438
6439} /* end of mgsl_reset_rx_dma_buffers() */
6440
6441/*
6442 * mgsl_free_rx_frame_buffers()
6443 *
6444 * Free the receive buffers used by a received SDLC
6445 * frame such that the buffers can be reused.
6446 *
6447 * Arguments:
6448 *
6449 * info pointer to device instance data
6450 * StartIndex index of 1st receive buffer of frame
6451 * EndIndex index of last receive buffer of frame
6452 *
6453 * Return Value: None
6454 */
6455static void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex )
6456{
Joe Perches0fab6de2008-04-28 02:14:02 -07006457 bool Done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 DMABUFFERENTRY *pBufEntry;
6459 unsigned int Index;
6460
6461 /* Starting with 1st buffer entry of the frame clear the status */
6462 /* field and set the count field to DMA Buffer Size. */
6463
6464 Index = StartIndex;
6465
6466 while( !Done ) {
6467 pBufEntry = &(info->rx_buffer_list[Index]);
6468
6469 if ( Index == EndIndex ) {
6470 /* This is the last buffer of the frame! */
Joe Perches0fab6de2008-04-28 02:14:02 -07006471 Done = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 }
6473
6474 /* reset current buffer for reuse */
6475// pBufEntry->status = 0;
6476// pBufEntry->count = DMABUFFERSIZE;
6477 *((unsigned long *)&(pBufEntry->count)) = DMABUFFERSIZE;
6478
6479 /* advance to next buffer entry in linked list */
6480 Index++;
6481 if ( Index == info->rx_buffer_count )
6482 Index = 0;
6483 }
6484
6485 /* set current buffer to next buffer after last buffer of frame */
6486 info->current_rx_buffer = Index;
6487
6488} /* end of free_rx_frame_buffers() */
6489
6490/* mgsl_get_rx_frame()
6491 *
6492 * This function attempts to return a received SDLC frame from the
6493 * receive DMA buffers. Only frames received without errors are returned.
6494 *
6495 * Arguments: info pointer to device extension
Joe Perches0fab6de2008-04-28 02:14:02 -07006496 * Return Value: true if frame returned, otherwise false
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 */
Joe Perches0fab6de2008-04-28 02:14:02 -07006498static bool mgsl_get_rx_frame(struct mgsl_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499{
6500 unsigned int StartIndex, EndIndex; /* index of 1st and last buffers of Rx frame */
6501 unsigned short status;
6502 DMABUFFERENTRY *pBufEntry;
6503 unsigned int framesize = 0;
Joe Perches0fab6de2008-04-28 02:14:02 -07006504 bool ReturnCode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 unsigned long flags;
Alan Cox8fb06c72008-07-16 21:56:46 +01006506 struct tty_struct *tty = info->port.tty;
Joe Perches0fab6de2008-04-28 02:14:02 -07006507 bool return_frame = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508
6509 /*
6510 * current_rx_buffer points to the 1st buffer of the next available
6511 * receive frame. To find the last buffer of the frame look for
6512 * a non-zero status field in the buffer entries. (The status
6513 * field is set by the 16C32 after completing a receive frame.
6514 */
6515
6516 StartIndex = EndIndex = info->current_rx_buffer;
6517
6518 while( !info->rx_buffer_list[EndIndex].status ) {
6519 /*
6520 * If the count field of the buffer entry is non-zero then
6521 * this buffer has not been used. (The 16C32 clears the count
6522 * field when it starts using the buffer.) If an unused buffer
6523 * is encountered then there are no frames available.
6524 */
6525
6526 if ( info->rx_buffer_list[EndIndex].count )
6527 goto Cleanup;
6528
6529 /* advance to next buffer entry in linked list */
6530 EndIndex++;
6531 if ( EndIndex == info->rx_buffer_count )
6532 EndIndex = 0;
6533
6534 /* if entire list searched then no frame available */
6535 if ( EndIndex == StartIndex ) {
6536 /* If this occurs then something bad happened,
6537 * all buffers have been 'used' but none mark
6538 * the end of a frame. Reset buffers and receiver.
6539 */
6540
6541 if ( info->rx_enabled ){
6542 spin_lock_irqsave(&info->irq_spinlock,flags);
6543 usc_start_receiver(info);
6544 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6545 }
6546 goto Cleanup;
6547 }
6548 }
6549
6550
6551 /* check status of receive frame */
6552
6553 status = info->rx_buffer_list[EndIndex].status;
6554
Alexandru Juncue06922a2013-07-27 11:14:39 +03006555 if ( status & (RXSTATUS_SHORT_FRAME | RXSTATUS_OVERRUN |
6556 RXSTATUS_CRC_ERROR | RXSTATUS_ABORT) ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 if ( status & RXSTATUS_SHORT_FRAME )
6558 info->icount.rxshort++;
6559 else if ( status & RXSTATUS_ABORT )
6560 info->icount.rxabort++;
6561 else if ( status & RXSTATUS_OVERRUN )
6562 info->icount.rxover++;
6563 else {
6564 info->icount.rxcrc++;
6565 if ( info->params.crc_type & HDLC_CRC_RETURN_EX )
Joe Perches0fab6de2008-04-28 02:14:02 -07006566 return_frame = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 }
6568 framesize = 0;
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08006569#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 {
Krzysztof Halasa198191c2008-06-30 23:26:53 +02006571 info->netdev->stats.rx_errors++;
6572 info->netdev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 }
6574#endif
6575 } else
Joe Perches0fab6de2008-04-28 02:14:02 -07006576 return_frame = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577
6578 if ( return_frame ) {
6579 /* receive frame has no errors, get frame size.
6580 * The frame size is the starting value of the RCC (which was
6581 * set to 0xffff) minus the ending value of the RCC (decremented
6582 * once for each receive character) minus 2 for the 16-bit CRC.
6583 */
6584
6585 framesize = RCLRVALUE - info->rx_buffer_list[EndIndex].rcc;
6586
6587 /* adjust frame size for CRC if any */
6588 if ( info->params.crc_type == HDLC_CRC_16_CCITT )
6589 framesize -= 2;
6590 else if ( info->params.crc_type == HDLC_CRC_32_CCITT )
6591 framesize -= 4;
6592 }
6593
6594 if ( debug_level >= DEBUG_LEVEL_BH )
6595 printk("%s(%d):mgsl_get_rx_frame(%s) status=%04X size=%d\n",
6596 __FILE__,__LINE__,info->device_name,status,framesize);
6597
6598 if ( debug_level >= DEBUG_LEVEL_DATA )
6599 mgsl_trace_block(info,info->rx_buffer_list[StartIndex].virt_addr,
6600 min_t(int, framesize, DMABUFFERSIZE),0);
6601
6602 if (framesize) {
6603 if ( ( (info->params.crc_type & HDLC_CRC_RETURN_EX) &&
6604 ((framesize+1) > info->max_frame_size) ) ||
6605 (framesize > info->max_frame_size) )
6606 info->icount.rxlong++;
6607 else {
6608 /* copy dma buffer(s) to contiguous intermediate buffer */
6609 int copy_count = framesize;
6610 int index = StartIndex;
6611 unsigned char *ptmp = info->intermediate_rxbuffer;
6612
6613 if ( !(status & RXSTATUS_CRC_ERROR))
Jiri Slaby4bd01622015-10-11 15:22:45 +02006614 info->icount.rxok++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
6616 while(copy_count) {
6617 int partial_count;
6618 if ( copy_count > DMABUFFERSIZE )
6619 partial_count = DMABUFFERSIZE;
6620 else
6621 partial_count = copy_count;
6622
6623 pBufEntry = &(info->rx_buffer_list[index]);
6624 memcpy( ptmp, pBufEntry->virt_addr, partial_count );
6625 ptmp += partial_count;
6626 copy_count -= partial_count;
6627
6628 if ( ++index == info->rx_buffer_count )
6629 index = 0;
6630 }
6631
6632 if ( info->params.crc_type & HDLC_CRC_RETURN_EX ) {
6633 ++framesize;
6634 *ptmp = (status & RXSTATUS_CRC_ERROR ?
6635 RX_CRC_ERROR :
6636 RX_OK);
6637
6638 if ( debug_level >= DEBUG_LEVEL_DATA )
6639 printk("%s(%d):mgsl_get_rx_frame(%s) rx frame status=%d\n",
6640 __FILE__,__LINE__,info->device_name,
6641 *ptmp);
6642 }
6643
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08006644#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 if (info->netcount)
6646 hdlcdev_rx(info,info->intermediate_rxbuffer,framesize);
6647 else
6648#endif
6649 ldisc_receive_buf(tty, info->intermediate_rxbuffer, info->flag_buf, framesize);
6650 }
6651 }
6652 /* Free the buffers used by this frame. */
6653 mgsl_free_rx_frame_buffers( info, StartIndex, EndIndex );
6654
Joe Perches0fab6de2008-04-28 02:14:02 -07006655 ReturnCode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
6657Cleanup:
6658
6659 if ( info->rx_enabled && info->rx_overflow ) {
6660 /* The receiver needs to restarted because of
6661 * a receive overflow (buffer or FIFO). If the
6662 * receive buffers are now empty, then restart receiver.
6663 */
6664
6665 if ( !info->rx_buffer_list[EndIndex].status &&
6666 info->rx_buffer_list[EndIndex].count ) {
6667 spin_lock_irqsave(&info->irq_spinlock,flags);
6668 usc_start_receiver(info);
6669 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6670 }
6671 }
6672
6673 return ReturnCode;
6674
6675} /* end of mgsl_get_rx_frame() */
6676
6677/* mgsl_get_raw_rx_frame()
6678 *
6679 * This function attempts to return a received frame from the
6680 * receive DMA buffers when running in external loop mode. In this mode,
6681 * we will return at most one DMABUFFERSIZE frame to the application.
6682 * The USC receiver is triggering off of DCD going active to start a new
6683 * frame, and DCD going inactive to terminate the frame (similar to
6684 * processing a closing flag character).
6685 *
6686 * In this routine, we will return DMABUFFERSIZE "chunks" at a time.
6687 * If DCD goes inactive, the last Rx DMA Buffer will have a non-zero
6688 * status field and the RCC field will indicate the length of the
6689 * entire received frame. We take this RCC field and get the modulus
6690 * of RCC and DMABUFFERSIZE to determine if number of bytes in the
6691 * last Rx DMA buffer and return that last portion of the frame.
6692 *
6693 * Arguments: info pointer to device extension
Joe Perches0fab6de2008-04-28 02:14:02 -07006694 * Return Value: true if frame returned, otherwise false
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 */
Joe Perches0fab6de2008-04-28 02:14:02 -07006696static bool mgsl_get_raw_rx_frame(struct mgsl_struct *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697{
6698 unsigned int CurrentIndex, NextIndex;
6699 unsigned short status;
6700 DMABUFFERENTRY *pBufEntry;
6701 unsigned int framesize = 0;
Joe Perches0fab6de2008-04-28 02:14:02 -07006702 bool ReturnCode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 unsigned long flags;
Alan Cox8fb06c72008-07-16 21:56:46 +01006704 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705
6706 /*
6707 * current_rx_buffer points to the 1st buffer of the next available
6708 * receive frame. The status field is set by the 16C32 after
6709 * completing a receive frame. If the status field of this buffer
6710 * is zero, either the USC is still filling this buffer or this
6711 * is one of a series of buffers making up a received frame.
6712 *
6713 * If the count field of this buffer is zero, the USC is either
6714 * using this buffer or has used this buffer. Look at the count
6715 * field of the next buffer. If that next buffer's count is
6716 * non-zero, the USC is still actively using the current buffer.
6717 * Otherwise, if the next buffer's count field is zero, the
6718 * current buffer is complete and the USC is using the next
6719 * buffer.
6720 */
6721 CurrentIndex = NextIndex = info->current_rx_buffer;
6722 ++NextIndex;
6723 if ( NextIndex == info->rx_buffer_count )
6724 NextIndex = 0;
6725
6726 if ( info->rx_buffer_list[CurrentIndex].status != 0 ||
6727 (info->rx_buffer_list[CurrentIndex].count == 0 &&
6728 info->rx_buffer_list[NextIndex].count == 0)) {
6729 /*
6730 * Either the status field of this dma buffer is non-zero
6731 * (indicating the last buffer of a receive frame) or the next
6732 * buffer is marked as in use -- implying this buffer is complete
6733 * and an intermediate buffer for this received frame.
6734 */
6735
6736 status = info->rx_buffer_list[CurrentIndex].status;
6737
Alexandru Juncue06922a2013-07-27 11:14:39 +03006738 if ( status & (RXSTATUS_SHORT_FRAME | RXSTATUS_OVERRUN |
6739 RXSTATUS_CRC_ERROR | RXSTATUS_ABORT) ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 if ( status & RXSTATUS_SHORT_FRAME )
6741 info->icount.rxshort++;
6742 else if ( status & RXSTATUS_ABORT )
6743 info->icount.rxabort++;
6744 else if ( status & RXSTATUS_OVERRUN )
6745 info->icount.rxover++;
6746 else
6747 info->icount.rxcrc++;
6748 framesize = 0;
6749 } else {
6750 /*
6751 * A receive frame is available, get frame size and status.
6752 *
6753 * The frame size is the starting value of the RCC (which was
6754 * set to 0xffff) minus the ending value of the RCC (decremented
6755 * once for each receive character) minus 2 or 4 for the 16-bit
6756 * or 32-bit CRC.
6757 *
6758 * If the status field is zero, this is an intermediate buffer.
6759 * It's size is 4K.
6760 *
6761 * If the DMA Buffer Entry's Status field is non-zero, the
6762 * receive operation completed normally (ie: DCD dropped). The
6763 * RCC field is valid and holds the received frame size.
6764 * It is possible that the RCC field will be zero on a DMA buffer
6765 * entry with a non-zero status. This can occur if the total
6766 * frame size (number of bytes between the time DCD goes active
6767 * to the time DCD goes inactive) exceeds 65535 bytes. In this
6768 * case the 16C32 has underrun on the RCC count and appears to
6769 * stop updating this counter to let us know the actual received
6770 * frame size. If this happens (non-zero status and zero RCC),
6771 * simply return the entire RxDMA Buffer
6772 */
6773 if ( status ) {
6774 /*
6775 * In the event that the final RxDMA Buffer is
6776 * terminated with a non-zero status and the RCC
6777 * field is zero, we interpret this as the RCC
6778 * having underflowed (received frame > 65535 bytes).
6779 *
6780 * Signal the event to the user by passing back
6781 * a status of RxStatus_CrcError returning the full
6782 * buffer and let the app figure out what data is
6783 * actually valid
6784 */
6785 if ( info->rx_buffer_list[CurrentIndex].rcc )
6786 framesize = RCLRVALUE - info->rx_buffer_list[CurrentIndex].rcc;
6787 else
6788 framesize = DMABUFFERSIZE;
6789 }
6790 else
6791 framesize = DMABUFFERSIZE;
6792 }
6793
6794 if ( framesize > DMABUFFERSIZE ) {
6795 /*
6796 * if running in raw sync mode, ISR handler for
6797 * End Of Buffer events terminates all buffers at 4K.
6798 * If this frame size is said to be >4K, get the
6799 * actual number of bytes of the frame in this buffer.
6800 */
6801 framesize = framesize % DMABUFFERSIZE;
6802 }
6803
6804
6805 if ( debug_level >= DEBUG_LEVEL_BH )
6806 printk("%s(%d):mgsl_get_raw_rx_frame(%s) status=%04X size=%d\n",
6807 __FILE__,__LINE__,info->device_name,status,framesize);
6808
6809 if ( debug_level >= DEBUG_LEVEL_DATA )
6810 mgsl_trace_block(info,info->rx_buffer_list[CurrentIndex].virt_addr,
6811 min_t(int, framesize, DMABUFFERSIZE),0);
6812
6813 if (framesize) {
6814 /* copy dma buffer(s) to contiguous intermediate buffer */
6815 /* NOTE: we never copy more than DMABUFFERSIZE bytes */
6816
6817 pBufEntry = &(info->rx_buffer_list[CurrentIndex]);
6818 memcpy( info->intermediate_rxbuffer, pBufEntry->virt_addr, framesize);
6819 info->icount.rxok++;
6820
6821 ldisc_receive_buf(tty, info->intermediate_rxbuffer, info->flag_buf, framesize);
6822 }
6823
6824 /* Free the buffers used by this frame. */
6825 mgsl_free_rx_frame_buffers( info, CurrentIndex, CurrentIndex );
6826
Joe Perches0fab6de2008-04-28 02:14:02 -07006827 ReturnCode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 }
6829
6830
6831 if ( info->rx_enabled && info->rx_overflow ) {
6832 /* The receiver needs to restarted because of
6833 * a receive overflow (buffer or FIFO). If the
6834 * receive buffers are now empty, then restart receiver.
6835 */
6836
6837 if ( !info->rx_buffer_list[CurrentIndex].status &&
6838 info->rx_buffer_list[CurrentIndex].count ) {
6839 spin_lock_irqsave(&info->irq_spinlock,flags);
6840 usc_start_receiver(info);
6841 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6842 }
6843 }
6844
6845 return ReturnCode;
6846
6847} /* end of mgsl_get_raw_rx_frame() */
6848
6849/* mgsl_load_tx_dma_buffer()
6850 *
6851 * Load the transmit DMA buffer with the specified data.
6852 *
6853 * Arguments:
6854 *
6855 * info pointer to device extension
6856 * Buffer pointer to buffer containing frame to load
6857 * BufferSize size in bytes of frame in Buffer
6858 *
6859 * Return Value: None
6860 */
6861static void mgsl_load_tx_dma_buffer(struct mgsl_struct *info,
6862 const char *Buffer, unsigned int BufferSize)
6863{
6864 unsigned short Copycount;
6865 unsigned int i = 0;
6866 DMABUFFERENTRY *pBufEntry;
6867
6868 if ( debug_level >= DEBUG_LEVEL_DATA )
6869 mgsl_trace_block(info,Buffer, min_t(int, BufferSize, DMABUFFERSIZE), 1);
6870
6871 if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
6872 /* set CMR:13 to start transmit when
6873 * next GoAhead (abort) is received
6874 */
Alexandru Juncue06922a2013-07-27 11:14:39 +03006875 info->cmr_value |= BIT13;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876 }
6877
6878 /* begin loading the frame in the next available tx dma
6879 * buffer, remember it's starting location for setting
6880 * up tx dma operation
6881 */
6882 i = info->current_tx_buffer;
6883 info->start_tx_dma_buffer = i;
6884
6885 /* Setup the status and RCC (Frame Size) fields of the 1st */
6886 /* buffer entry in the transmit DMA buffer list. */
6887
6888 info->tx_buffer_list[i].status = info->cmr_value & 0xf000;
6889 info->tx_buffer_list[i].rcc = BufferSize;
6890 info->tx_buffer_list[i].count = BufferSize;
6891
6892 /* Copy frame data from 1st source buffer to the DMA buffers. */
6893 /* The frame data may span multiple DMA buffers. */
6894
6895 while( BufferSize ){
6896 /* Get a pointer to next DMA buffer entry. */
6897 pBufEntry = &info->tx_buffer_list[i++];
6898
6899 if ( i == info->tx_buffer_count )
6900 i=0;
6901
6902 /* Calculate the number of bytes that can be copied from */
6903 /* the source buffer to this DMA buffer. */
6904 if ( BufferSize > DMABUFFERSIZE )
6905 Copycount = DMABUFFERSIZE;
6906 else
6907 Copycount = BufferSize;
6908
6909 /* Actually copy data from source buffer to DMA buffer. */
6910 /* Also set the data count for this individual DMA buffer. */
6911 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
6912 mgsl_load_pci_memory(pBufEntry->virt_addr, Buffer,Copycount);
6913 else
6914 memcpy(pBufEntry->virt_addr, Buffer, Copycount);
6915
6916 pBufEntry->count = Copycount;
6917
6918 /* Advance source pointer and reduce remaining data count. */
6919 Buffer += Copycount;
6920 BufferSize -= Copycount;
6921
6922 ++info->tx_dma_buffers_used;
6923 }
6924
6925 /* remember next available tx dma buffer */
6926 info->current_tx_buffer = i;
6927
6928} /* end of mgsl_load_tx_dma_buffer() */
6929
6930/*
6931 * mgsl_register_test()
6932 *
6933 * Performs a register test of the 16C32.
6934 *
6935 * Arguments: info pointer to device instance data
Joe Perches0fab6de2008-04-28 02:14:02 -07006936 * Return Value: true if test passed, otherwise false
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 */
Joe Perches0fab6de2008-04-28 02:14:02 -07006938static bool mgsl_register_test( struct mgsl_struct *info )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939{
6940 static unsigned short BitPatterns[] =
6941 { 0x0000, 0xffff, 0xaaaa, 0x5555, 0x1234, 0x6969, 0x9696, 0x0f0f };
Tobias Klauserfe971072006-01-09 20:54:02 -08006942 static unsigned int Patterncount = ARRAY_SIZE(BitPatterns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 unsigned int i;
Joe Perches0fab6de2008-04-28 02:14:02 -07006944 bool rc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 unsigned long flags;
6946
6947 spin_lock_irqsave(&info->irq_spinlock,flags);
6948 usc_reset(info);
6949
6950 /* Verify the reset state of some registers. */
6951
6952 if ( (usc_InReg( info, SICR ) != 0) ||
6953 (usc_InReg( info, IVR ) != 0) ||
6954 (usc_InDmaReg( info, DIVR ) != 0) ){
Joe Perches0fab6de2008-04-28 02:14:02 -07006955 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 }
6957
Joe Perches0fab6de2008-04-28 02:14:02 -07006958 if ( rc ){
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 /* Write bit patterns to various registers but do it out of */
6960 /* sync, then read back and verify values. */
6961
6962 for ( i = 0 ; i < Patterncount ; i++ ) {
6963 usc_OutReg( info, TC0R, BitPatterns[i] );
6964 usc_OutReg( info, TC1R, BitPatterns[(i+1)%Patterncount] );
6965 usc_OutReg( info, TCLR, BitPatterns[(i+2)%Patterncount] );
6966 usc_OutReg( info, RCLR, BitPatterns[(i+3)%Patterncount] );
6967 usc_OutReg( info, RSR, BitPatterns[(i+4)%Patterncount] );
6968 usc_OutDmaReg( info, TBCR, BitPatterns[(i+5)%Patterncount] );
6969
6970 if ( (usc_InReg( info, TC0R ) != BitPatterns[i]) ||
6971 (usc_InReg( info, TC1R ) != BitPatterns[(i+1)%Patterncount]) ||
6972 (usc_InReg( info, TCLR ) != BitPatterns[(i+2)%Patterncount]) ||
6973 (usc_InReg( info, RCLR ) != BitPatterns[(i+3)%Patterncount]) ||
6974 (usc_InReg( info, RSR ) != BitPatterns[(i+4)%Patterncount]) ||
6975 (usc_InDmaReg( info, TBCR ) != BitPatterns[(i+5)%Patterncount]) ){
Joe Perches0fab6de2008-04-28 02:14:02 -07006976 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 break;
6978 }
6979 }
6980 }
6981
6982 usc_reset(info);
6983 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6984
6985 return rc;
6986
6987} /* end of mgsl_register_test() */
6988
6989/* mgsl_irq_test() Perform interrupt test of the 16C32.
6990 *
6991 * Arguments: info pointer to device instance data
Joe Perches0fab6de2008-04-28 02:14:02 -07006992 * Return Value: true if test passed, otherwise false
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 */
Joe Perches0fab6de2008-04-28 02:14:02 -07006994static bool mgsl_irq_test( struct mgsl_struct *info )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995{
6996 unsigned long EndTime;
6997 unsigned long flags;
6998
6999 spin_lock_irqsave(&info->irq_spinlock,flags);
7000 usc_reset(info);
7001
7002 /*
7003 * Setup 16C32 to interrupt on TxC pin (14MHz clock) transition.
Joe Perches0fab6de2008-04-28 02:14:02 -07007004 * The ISR sets irq_occurred to true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 */
7006
Joe Perches0fab6de2008-04-28 02:14:02 -07007007 info->irq_occurred = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008
7009 /* Enable INTEN gate for ISA adapter (Port 6, Bit12) */
7010 /* Enable INTEN (Port 6, Bit12) */
7011 /* This connects the IRQ request signal to the ISA bus */
7012 /* on the ISA adapter. This has no effect for the PCI adapter */
7013 usc_OutReg( info, PCR, (unsigned short)((usc_InReg(info, PCR) | BIT13) & ~BIT12) );
7014
7015 usc_EnableMasterIrqBit(info);
7016 usc_EnableInterrupts(info, IO_PIN);
7017 usc_ClearIrqPendingBits(info, IO_PIN);
7018
7019 usc_UnlatchIostatusBits(info, MISCSTATUS_TXC_LATCHED);
7020 usc_EnableStatusIrqs(info, SICR_TXC_ACTIVE + SICR_TXC_INACTIVE);
7021
7022 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7023
7024 EndTime=100;
7025 while( EndTime-- && !info->irq_occurred ) {
7026 msleep_interruptible(10);
7027 }
7028
7029 spin_lock_irqsave(&info->irq_spinlock,flags);
7030 usc_reset(info);
7031 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7032
Joe Perches0fab6de2008-04-28 02:14:02 -07007033 return info->irq_occurred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034
7035} /* end of mgsl_irq_test() */
7036
7037/* mgsl_dma_test()
7038 *
7039 * Perform a DMA test of the 16C32. A small frame is
7040 * transmitted via DMA from a transmit buffer to a receive buffer
7041 * using single buffer DMA mode.
7042 *
7043 * Arguments: info pointer to device instance data
Joe Perches0fab6de2008-04-28 02:14:02 -07007044 * Return Value: true if test passed, otherwise false
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 */
Joe Perches0fab6de2008-04-28 02:14:02 -07007046static bool mgsl_dma_test( struct mgsl_struct *info )
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047{
7048 unsigned short FifoLevel;
7049 unsigned long phys_addr;
7050 unsigned int FrameSize;
7051 unsigned int i;
7052 char *TmpPtr;
Joe Perches0fab6de2008-04-28 02:14:02 -07007053 bool rc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 unsigned short status=0;
7055 unsigned long EndTime;
7056 unsigned long flags;
7057 MGSL_PARAMS tmp_params;
7058
7059 /* save current port options */
7060 memcpy(&tmp_params,&info->params,sizeof(MGSL_PARAMS));
7061 /* load default port options */
7062 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
7063
7064#define TESTFRAMESIZE 40
7065
7066 spin_lock_irqsave(&info->irq_spinlock,flags);
7067
7068 /* setup 16C32 for SDLC DMA transfer mode */
7069
7070 usc_reset(info);
7071 usc_set_sdlc_mode(info);
7072 usc_enable_loopback(info,1);
7073
7074 /* Reprogram the RDMR so that the 16C32 does NOT clear the count
7075 * field of the buffer entry after fetching buffer address. This
7076 * way we can detect a DMA failure for a DMA read (which should be
7077 * non-destructive to system memory) before we try and write to
7078 * memory (where a failure could corrupt system memory).
7079 */
7080
7081 /* Receive DMA mode Register (RDMR)
7082 *
7083 * <15..14> 11 DMA mode = Linked List Buffer mode
7084 * <13> 1 RSBinA/L = store Rx status Block in List entry
7085 * <12> 0 1 = Clear count of List Entry after fetching
7086 * <11..10> 00 Address mode = Increment
7087 * <9> 1 Terminate Buffer on RxBound
7088 * <8> 0 Bus Width = 16bits
7089 * <7..0> ? status Bits (write as 0s)
7090 *
7091 * 1110 0010 0000 0000 = 0xe200
7092 */
7093
7094 usc_OutDmaReg( info, RDMR, 0xe200 );
7095
7096 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7097
7098
7099 /* SETUP TRANSMIT AND RECEIVE DMA BUFFERS */
7100
7101 FrameSize = TESTFRAMESIZE;
7102
7103 /* setup 1st transmit buffer entry: */
7104 /* with frame size and transmit control word */
7105
7106 info->tx_buffer_list[0].count = FrameSize;
7107 info->tx_buffer_list[0].rcc = FrameSize;
7108 info->tx_buffer_list[0].status = 0x4000;
7109
7110 /* build a transmit frame in 1st transmit DMA buffer */
7111
7112 TmpPtr = info->tx_buffer_list[0].virt_addr;
7113 for (i = 0; i < FrameSize; i++ )
7114 *TmpPtr++ = i;
7115
7116 /* setup 1st receive buffer entry: */
7117 /* clear status, set max receive buffer size */
7118
7119 info->rx_buffer_list[0].status = 0;
7120 info->rx_buffer_list[0].count = FrameSize + 4;
7121
7122 /* zero out the 1st receive buffer */
7123
7124 memset( info->rx_buffer_list[0].virt_addr, 0, FrameSize + 4 );
7125
7126 /* Set count field of next buffer entries to prevent */
7127 /* 16C32 from using buffers after the 1st one. */
7128
7129 info->tx_buffer_list[1].count = 0;
7130 info->rx_buffer_list[1].count = 0;
7131
7132
7133 /***************************/
7134 /* Program 16C32 receiver. */
7135 /***************************/
7136
7137 spin_lock_irqsave(&info->irq_spinlock,flags);
7138
7139 /* setup DMA transfers */
7140 usc_RTCmd( info, RTCmd_PurgeRxFifo );
7141
7142 /* program 16C32 receiver with physical address of 1st DMA buffer entry */
7143 phys_addr = info->rx_buffer_list[0].phys_entry;
7144 usc_OutDmaReg( info, NRARL, (unsigned short)phys_addr );
7145 usc_OutDmaReg( info, NRARU, (unsigned short)(phys_addr >> 16) );
7146
7147 /* Clear the Rx DMA status bits (read RDMR) and start channel */
7148 usc_InDmaReg( info, RDMR );
7149 usc_DmaCmd( info, DmaCmd_InitRxChannel );
7150
7151 /* Enable Receiver (RMR <1..0> = 10) */
7152 usc_OutReg( info, RMR, (unsigned short)((usc_InReg(info, RMR) & 0xfffc) | 0x0002) );
7153
7154 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7155
7156
7157 /*************************************************************/
7158 /* WAIT FOR RECEIVER TO DMA ALL PARAMETERS FROM BUFFER ENTRY */
7159 /*************************************************************/
7160
7161 /* Wait 100ms for interrupt. */
7162 EndTime = jiffies + msecs_to_jiffies(100);
7163
7164 for(;;) {
7165 if (time_after(jiffies, EndTime)) {
Joe Perches0fab6de2008-04-28 02:14:02 -07007166 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 break;
7168 }
7169
7170 spin_lock_irqsave(&info->irq_spinlock,flags);
7171 status = usc_InDmaReg( info, RDMR );
7172 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7173
7174 if ( !(status & BIT4) && (status & BIT5) ) {
7175 /* INITG (BIT 4) is inactive (no entry read in progress) AND */
7176 /* BUSY (BIT 5) is active (channel still active). */
7177 /* This means the buffer entry read has completed. */
7178 break;
7179 }
7180 }
7181
7182
7183 /******************************/
7184 /* Program 16C32 transmitter. */
7185 /******************************/
7186
7187 spin_lock_irqsave(&info->irq_spinlock,flags);
7188
7189 /* Program the Transmit Character Length Register (TCLR) */
7190 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
7191
7192 usc_OutReg( info, TCLR, (unsigned short)info->tx_buffer_list[0].count );
7193 usc_RTCmd( info, RTCmd_PurgeTxFifo );
7194
7195 /* Program the address of the 1st DMA Buffer Entry in linked list */
7196
7197 phys_addr = info->tx_buffer_list[0].phys_entry;
7198 usc_OutDmaReg( info, NTARL, (unsigned short)phys_addr );
7199 usc_OutDmaReg( info, NTARU, (unsigned short)(phys_addr >> 16) );
7200
7201 /* unlatch Tx status bits, and start transmit channel. */
7202
7203 usc_OutReg( info, TCSR, (unsigned short)(( usc_InReg(info, TCSR) & 0x0f00) | 0xfa) );
7204 usc_DmaCmd( info, DmaCmd_InitTxChannel );
7205
7206 /* wait for DMA controller to fill transmit FIFO */
7207
7208 usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
7209
7210 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7211
7212
7213 /**********************************/
7214 /* WAIT FOR TRANSMIT FIFO TO FILL */
7215 /**********************************/
7216
7217 /* Wait 100ms */
7218 EndTime = jiffies + msecs_to_jiffies(100);
7219
7220 for(;;) {
7221 if (time_after(jiffies, EndTime)) {
Joe Perches0fab6de2008-04-28 02:14:02 -07007222 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 break;
7224 }
7225
7226 spin_lock_irqsave(&info->irq_spinlock,flags);
7227 FifoLevel = usc_InReg(info, TICR) >> 8;
7228 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7229
7230 if ( FifoLevel < 16 )
7231 break;
7232 else
7233 if ( FrameSize < 32 ) {
7234 /* This frame is smaller than the entire transmit FIFO */
7235 /* so wait for the entire frame to be loaded. */
7236 if ( FifoLevel <= (32 - FrameSize) )
7237 break;
7238 }
7239 }
7240
7241
Joe Perches0fab6de2008-04-28 02:14:02 -07007242 if ( rc )
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 {
7244 /* Enable 16C32 transmitter. */
7245
7246 spin_lock_irqsave(&info->irq_spinlock,flags);
7247
7248 /* Transmit mode Register (TMR), <1..0> = 10, Enable Transmitter */
7249 usc_TCmd( info, TCmd_SendFrame );
7250 usc_OutReg( info, TMR, (unsigned short)((usc_InReg(info, TMR) & 0xfffc) | 0x0002) );
7251
7252 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7253
Alexandru Juncue06922a2013-07-27 11:14:39 +03007254
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 /******************************/
7256 /* WAIT FOR TRANSMIT COMPLETE */
7257 /******************************/
7258
7259 /* Wait 100ms */
7260 EndTime = jiffies + msecs_to_jiffies(100);
7261
7262 /* While timer not expired wait for transmit complete */
7263
7264 spin_lock_irqsave(&info->irq_spinlock,flags);
7265 status = usc_InReg( info, TCSR );
7266 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7267
Alexandru Juncue06922a2013-07-27 11:14:39 +03007268 while ( !(status & (BIT6 | BIT5 | BIT4 | BIT2 | BIT1)) ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269 if (time_after(jiffies, EndTime)) {
Joe Perches0fab6de2008-04-28 02:14:02 -07007270 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271 break;
7272 }
7273
7274 spin_lock_irqsave(&info->irq_spinlock,flags);
7275 status = usc_InReg( info, TCSR );
7276 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7277 }
7278 }
7279
7280
Joe Perches0fab6de2008-04-28 02:14:02 -07007281 if ( rc ){
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 /* CHECK FOR TRANSMIT ERRORS */
Alexandru Juncue06922a2013-07-27 11:14:39 +03007283 if ( status & (BIT5 | BIT1) )
Joe Perches0fab6de2008-04-28 02:14:02 -07007284 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 }
7286
Joe Perches0fab6de2008-04-28 02:14:02 -07007287 if ( rc ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 /* WAIT FOR RECEIVE COMPLETE */
7289
7290 /* Wait 100ms */
7291 EndTime = jiffies + msecs_to_jiffies(100);
7292
7293 /* Wait for 16C32 to write receive status to buffer entry. */
7294 status=info->rx_buffer_list[0].status;
7295 while ( status == 0 ) {
7296 if (time_after(jiffies, EndTime)) {
Joe Perches0fab6de2008-04-28 02:14:02 -07007297 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298 break;
7299 }
7300 status=info->rx_buffer_list[0].status;
7301 }
7302 }
7303
7304
Joe Perches0fab6de2008-04-28 02:14:02 -07007305 if ( rc ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 /* CHECK FOR RECEIVE ERRORS */
7307 status = info->rx_buffer_list[0].status;
7308
Alexandru Juncue06922a2013-07-27 11:14:39 +03007309 if ( status & (BIT8 | BIT3 | BIT1) ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 /* receive error has occurred */
Joe Perches0fab6de2008-04-28 02:14:02 -07007311 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 } else {
7313 if ( memcmp( info->tx_buffer_list[0].virt_addr ,
7314 info->rx_buffer_list[0].virt_addr, FrameSize ) ){
Joe Perches0fab6de2008-04-28 02:14:02 -07007315 rc = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 }
7317 }
7318 }
7319
7320 spin_lock_irqsave(&info->irq_spinlock,flags);
7321 usc_reset( info );
7322 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7323
7324 /* restore current port options */
7325 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
7326
7327 return rc;
7328
7329} /* end of mgsl_dma_test() */
7330
7331/* mgsl_adapter_test()
7332 *
7333 * Perform the register, IRQ, and DMA tests for the 16C32.
7334 *
7335 * Arguments: info pointer to device instance data
7336 * Return Value: 0 if success, otherwise -ENODEV
7337 */
7338static int mgsl_adapter_test( struct mgsl_struct *info )
7339{
7340 if ( debug_level >= DEBUG_LEVEL_INFO )
7341 printk( "%s(%d):Testing device %s\n",
7342 __FILE__,__LINE__,info->device_name );
7343
7344 if ( !mgsl_register_test( info ) ) {
7345 info->init_error = DiagStatus_AddressFailure;
7346 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
7347 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
7348 return -ENODEV;
7349 }
7350
7351 if ( !mgsl_irq_test( info ) ) {
7352 info->init_error = DiagStatus_IrqFailure;
7353 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
7354 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
7355 return -ENODEV;
7356 }
7357
7358 if ( !mgsl_dma_test( info ) ) {
7359 info->init_error = DiagStatus_DmaFailure;
7360 printk( "%s(%d):DMA test failure for device %s DMA=%d\n",
7361 __FILE__,__LINE__,info->device_name, (unsigned short)(info->dma_level) );
7362 return -ENODEV;
7363 }
7364
7365 if ( debug_level >= DEBUG_LEVEL_INFO )
7366 printk( "%s(%d):device %s passed diagnostics\n",
7367 __FILE__,__LINE__,info->device_name );
7368
7369 return 0;
7370
7371} /* end of mgsl_adapter_test() */
7372
7373/* mgsl_memory_test()
7374 *
7375 * Test the shared memory on a PCI adapter.
7376 *
7377 * Arguments: info pointer to device instance data
Joe Perches0fab6de2008-04-28 02:14:02 -07007378 * Return Value: true if test passed, otherwise false
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 */
Joe Perches0fab6de2008-04-28 02:14:02 -07007380static bool mgsl_memory_test( struct mgsl_struct *info )
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381{
Tobias Klauserfe971072006-01-09 20:54:02 -08007382 static unsigned long BitPatterns[] =
7383 { 0x0, 0x55555555, 0xaaaaaaaa, 0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
7384 unsigned long Patterncount = ARRAY_SIZE(BitPatterns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 unsigned long i;
7386 unsigned long TestLimit = SHARED_MEM_ADDRESS_SIZE/sizeof(unsigned long);
7387 unsigned long * TestAddr;
7388
7389 if ( info->bus_type != MGSL_BUS_TYPE_PCI )
Joe Perches0fab6de2008-04-28 02:14:02 -07007390 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391
7392 TestAddr = (unsigned long *)info->memory_base;
7393
7394 /* Test data lines with test pattern at one location. */
7395
7396 for ( i = 0 ; i < Patterncount ; i++ ) {
7397 *TestAddr = BitPatterns[i];
7398 if ( *TestAddr != BitPatterns[i] )
Joe Perches0fab6de2008-04-28 02:14:02 -07007399 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 }
7401
7402 /* Test address lines with incrementing pattern over */
7403 /* entire address range. */
7404
7405 for ( i = 0 ; i < TestLimit ; i++ ) {
7406 *TestAddr = i * 4;
7407 TestAddr++;
7408 }
7409
7410 TestAddr = (unsigned long *)info->memory_base;
7411
7412 for ( i = 0 ; i < TestLimit ; i++ ) {
7413 if ( *TestAddr != i * 4 )
Joe Perches0fab6de2008-04-28 02:14:02 -07007414 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 TestAddr++;
7416 }
7417
7418 memset( info->memory_base, 0, SHARED_MEM_ADDRESS_SIZE );
7419
Joe Perches0fab6de2008-04-28 02:14:02 -07007420 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421
7422} /* End Of mgsl_memory_test() */
7423
7424
7425/* mgsl_load_pci_memory()
7426 *
7427 * Load a large block of data into the PCI shared memory.
7428 * Use this instead of memcpy() or memmove() to move data
7429 * into the PCI shared memory.
7430 *
7431 * Notes:
7432 *
7433 * This function prevents the PCI9050 interface chip from hogging
7434 * the adapter local bus, which can starve the 16C32 by preventing
7435 * 16C32 bus master cycles.
7436 *
7437 * The PCI9050 documentation says that the 9050 will always release
7438 * control of the local bus after completing the current read
7439 * or write operation.
7440 *
7441 * It appears that as long as the PCI9050 write FIFO is full, the
7442 * PCI9050 treats all of the writes as a single burst transaction
7443 * and will not release the bus. This causes DMA latency problems
7444 * at high speeds when copying large data blocks to the shared
7445 * memory.
7446 *
7447 * This function in effect, breaks the a large shared memory write
7448 * into multiple transations by interleaving a shared memory read
7449 * which will flush the write FIFO and 'complete' the write
7450 * transation. This allows any pending DMA request to gain control
7451 * of the local bus in a timely fasion.
7452 *
7453 * Arguments:
7454 *
7455 * TargetPtr pointer to target address in PCI shared memory
7456 * SourcePtr pointer to source buffer for data
7457 * count count in bytes of data to copy
7458 *
7459 * Return Value: None
7460 */
7461static void mgsl_load_pci_memory( char* TargetPtr, const char* SourcePtr,
7462 unsigned short count )
7463{
7464 /* 16 32-bit writes @ 60ns each = 960ns max latency on local bus */
7465#define PCI_LOAD_INTERVAL 64
7466
7467 unsigned short Intervalcount = count / PCI_LOAD_INTERVAL;
7468 unsigned short Index;
7469 unsigned long Dummy;
7470
7471 for ( Index = 0 ; Index < Intervalcount ; Index++ )
7472 {
7473 memcpy(TargetPtr, SourcePtr, PCI_LOAD_INTERVAL);
7474 Dummy = *((volatile unsigned long *)TargetPtr);
7475 TargetPtr += PCI_LOAD_INTERVAL;
7476 SourcePtr += PCI_LOAD_INTERVAL;
7477 }
7478
7479 memcpy( TargetPtr, SourcePtr, count % PCI_LOAD_INTERVAL );
7480
7481} /* End Of mgsl_load_pci_memory() */
7482
7483static void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit)
7484{
7485 int i;
7486 int linecount;
7487 if (xmit)
7488 printk("%s tx data:\n",info->device_name);
7489 else
7490 printk("%s rx data:\n",info->device_name);
7491
7492 while(count) {
7493 if (count > 16)
7494 linecount = 16;
7495 else
7496 linecount = count;
7497
7498 for(i=0;i<linecount;i++)
7499 printk("%02X ",(unsigned char)data[i]);
7500 for(;i<17;i++)
7501 printk(" ");
7502 for(i=0;i<linecount;i++) {
7503 if (data[i]>=040 && data[i]<=0176)
7504 printk("%c",data[i]);
7505 else
7506 printk(".");
7507 }
7508 printk("\n");
7509
7510 data += linecount;
7511 count -= linecount;
7512 }
7513} /* end of mgsl_trace_block() */
7514
7515/* mgsl_tx_timeout()
7516 *
7517 * called when HDLC frame times out
7518 * update stats and do tx completion processing
7519 *
7520 * Arguments: context pointer to device instance data
7521 * Return Value: None
7522 */
7523static void mgsl_tx_timeout(unsigned long context)
7524{
7525 struct mgsl_struct *info = (struct mgsl_struct*)context;
7526 unsigned long flags;
7527
7528 if ( debug_level >= DEBUG_LEVEL_INFO )
7529 printk( "%s(%d):mgsl_tx_timeout(%s)\n",
7530 __FILE__,__LINE__,info->device_name);
7531 if(info->tx_active &&
7532 (info->params.mode == MGSL_MODE_HDLC ||
7533 info->params.mode == MGSL_MODE_RAW) ) {
7534 info->icount.txtimeout++;
7535 }
7536 spin_lock_irqsave(&info->irq_spinlock,flags);
Joe Perches0fab6de2008-04-28 02:14:02 -07007537 info->tx_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
7539
7540 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
7541 usc_loopmode_cancel_transmit( info );
7542
7543 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7544
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08007545#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 if (info->netcount)
7547 hdlcdev_tx_done(info);
7548 else
7549#endif
7550 mgsl_bh_transmit(info);
7551
7552} /* end of mgsl_tx_timeout() */
7553
7554/* signal that there are no more frames to send, so that
7555 * line is 'released' by echoing RxD to TxD when current
7556 * transmission is complete (or immediately if no tx in progress).
7557 */
7558static int mgsl_loopmode_send_done( struct mgsl_struct * info )
7559{
7560 unsigned long flags;
7561
7562 spin_lock_irqsave(&info->irq_spinlock,flags);
7563 if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
7564 if (info->tx_active)
Joe Perches0fab6de2008-04-28 02:14:02 -07007565 info->loopmode_send_done_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 else
7567 usc_loopmode_send_done(info);
7568 }
7569 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7570
7571 return 0;
7572}
7573
7574/* release the line by echoing RxD to TxD
7575 * upon completion of a transmit frame
7576 */
7577static void usc_loopmode_send_done( struct mgsl_struct * info )
7578{
Joe Perches0fab6de2008-04-28 02:14:02 -07007579 info->loopmode_send_done_requested = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 /* clear CMR:13 to 0 to start echoing RxData to TxData */
Alexandru Juncue06922a2013-07-27 11:14:39 +03007581 info->cmr_value &= ~BIT13;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582 usc_OutReg(info, CMR, info->cmr_value);
7583}
7584
7585/* abort a transmit in progress while in HDLC LoopMode
7586 */
7587static void usc_loopmode_cancel_transmit( struct mgsl_struct * info )
7588{
7589 /* reset tx dma channel and purge TxFifo */
7590 usc_RTCmd( info, RTCmd_PurgeTxFifo );
7591 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
7592 usc_loopmode_send_done( info );
7593}
7594
7595/* for HDLC/SDLC LoopMode, setting CMR:13 after the transmitter is enabled
7596 * is an Insert Into Loop action. Upon receipt of a GoAhead sequence (RxAbort)
7597 * we must clear CMR:13 to begin repeating TxData to RxData
7598 */
7599static void usc_loopmode_insert_request( struct mgsl_struct * info )
7600{
Joe Perches0fab6de2008-04-28 02:14:02 -07007601 info->loopmode_insert_requested = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602
7603 /* enable RxAbort irq. On next RxAbort, clear CMR:13 to
7604 * begin repeating TxData on RxData (complete insertion)
7605 */
7606 usc_OutReg( info, RICR,
7607 (usc_InReg( info, RICR ) | RXSTATUS_ABORT_RECEIVED ) );
7608
7609 /* set CMR:13 to insert into loop on next GoAhead (RxAbort) */
7610 info->cmr_value |= BIT13;
7611 usc_OutReg(info, CMR, info->cmr_value);
7612}
7613
7614/* return 1 if station is inserted into the loop, otherwise 0
7615 */
7616static int usc_loopmode_active( struct mgsl_struct * info)
7617{
7618 return usc_InReg( info, CCSR ) & BIT7 ? 1 : 0 ;
7619}
7620
Paul Fulghumaf69c7f2006-12-06 20:40:24 -08007621#if SYNCLINK_GENERIC_HDLC
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622
7623/**
7624 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
7625 * set encoding and frame check sequence (FCS) options
7626 *
7627 * dev pointer to network device structure
7628 * encoding serial encoding setting
7629 * parity FCS setting
7630 *
7631 * returns 0 if success, otherwise error code
7632 */
7633static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
7634 unsigned short parity)
7635{
7636 struct mgsl_struct *info = dev_to_port(dev);
7637 unsigned char new_encoding;
7638 unsigned short new_crctype;
7639
7640 /* return error if TTY interface open */
Alan Cox8fb06c72008-07-16 21:56:46 +01007641 if (info->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642 return -EBUSY;
7643
7644 switch (encoding)
7645 {
7646 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
7647 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
7648 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
7649 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
7650 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
7651 default: return -EINVAL;
7652 }
7653
7654 switch (parity)
7655 {
7656 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
7657 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
7658 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
7659 default: return -EINVAL;
7660 }
7661
7662 info->params.encoding = new_encoding;
Alexey Dobriyan53b35312006-03-24 03:16:13 -08007663 info->params.crc_type = new_crctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664
7665 /* if network interface up, reprogram hardware */
7666 if (info->netcount)
7667 mgsl_program_hw(info);
7668
7669 return 0;
7670}
7671
7672/**
7673 * called by generic HDLC layer to send frame
7674 *
7675 * skb socket buffer containing HDLC frame
7676 * dev pointer to network device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 */
Stephen Hemminger4c5d5022009-08-31 19:50:48 +00007678static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
7679 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680{
7681 struct mgsl_struct *info = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682 unsigned long flags;
7683
7684 if (debug_level >= DEBUG_LEVEL_INFO)
7685 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
7686
7687 /* stop sending until this frame completes */
7688 netif_stop_queue(dev);
7689
7690 /* copy data to device buffers */
7691 info->xmit_cnt = skb->len;
7692 mgsl_load_tx_dma_buffer(info, skb->data, skb->len);
7693
7694 /* update network statistics */
Krzysztof Halasa198191c2008-06-30 23:26:53 +02007695 dev->stats.tx_packets++;
7696 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697
7698 /* done with socket buffer, so free it */
7699 dev_kfree_skb(skb);
7700
7701 /* save start time for transmit timeout detection */
7702 dev->trans_start = jiffies;
7703
7704 /* start hardware transmitter if necessary */
7705 spin_lock_irqsave(&info->irq_spinlock,flags);
7706 if (!info->tx_active)
7707 usc_start_transmitter(info);
7708 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7709
Stephen Hemminger4c5d5022009-08-31 19:50:48 +00007710 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711}
7712
7713/**
7714 * called by network layer when interface enabled
7715 * claim resources and initialize hardware
7716 *
7717 * dev pointer to network device structure
7718 *
7719 * returns 0 if success, otherwise error code
7720 */
7721static int hdlcdev_open(struct net_device *dev)
7722{
7723 struct mgsl_struct *info = dev_to_port(dev);
7724 int rc;
7725 unsigned long flags;
7726
7727 if (debug_level >= DEBUG_LEVEL_INFO)
7728 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
7729
7730 /* generic HDLC layer open processing */
Greg Kroah-Hartmana271ca32015-04-30 11:22:14 +02007731 rc = hdlc_open(dev);
7732 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 return rc;
7734
7735 /* arbitrate between network and tty opens */
7736 spin_lock_irqsave(&info->netlock, flags);
Alan Cox8fb06c72008-07-16 21:56:46 +01007737 if (info->port.count != 0 || info->netcount != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
7739 spin_unlock_irqrestore(&info->netlock, flags);
7740 return -EBUSY;
7741 }
7742 info->netcount=1;
7743 spin_unlock_irqrestore(&info->netlock, flags);
7744
7745 /* claim resources and init adapter */
7746 if ((rc = startup(info)) != 0) {
7747 spin_lock_irqsave(&info->netlock, flags);
7748 info->netcount=0;
7749 spin_unlock_irqrestore(&info->netlock, flags);
7750 return rc;
7751 }
7752
Joe Perches9fe80742013-01-27 18:21:00 -08007753 /* assert RTS and DTR, apply hardware settings */
7754 info->serial_signals |= SerialSignal_RTS | SerialSignal_DTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 mgsl_program_hw(info);
7756
7757 /* enable network layer transmit */
7758 dev->trans_start = jiffies;
7759 netif_start_queue(dev);
7760
7761 /* inform generic HDLC layer of current DCD status */
7762 spin_lock_irqsave(&info->irq_spinlock, flags);
7763 usc_get_serial_signals(info);
7764 spin_unlock_irqrestore(&info->irq_spinlock, flags);
Krzysztof Halasafbeff3c2006-07-21 14:44:55 -07007765 if (info->serial_signals & SerialSignal_DCD)
7766 netif_carrier_on(dev);
7767 else
7768 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769 return 0;
7770}
7771
7772/**
7773 * called by network layer when interface is disabled
7774 * shutdown hardware and release resources
7775 *
7776 * dev pointer to network device structure
7777 *
7778 * returns 0 if success, otherwise error code
7779 */
7780static int hdlcdev_close(struct net_device *dev)
7781{
7782 struct mgsl_struct *info = dev_to_port(dev);
7783 unsigned long flags;
7784
7785 if (debug_level >= DEBUG_LEVEL_INFO)
7786 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
7787
7788 netif_stop_queue(dev);
7789
7790 /* shutdown adapter and release resources */
7791 shutdown(info);
7792
7793 hdlc_close(dev);
7794
7795 spin_lock_irqsave(&info->netlock, flags);
7796 info->netcount=0;
7797 spin_unlock_irqrestore(&info->netlock, flags);
7798
7799 return 0;
7800}
7801
7802/**
7803 * called by network layer to process IOCTL call to network device
7804 *
7805 * dev pointer to network device structure
7806 * ifr pointer to network interface request structure
7807 * cmd IOCTL command code
7808 *
7809 * returns 0 if success, otherwise error code
7810 */
7811static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7812{
7813 const size_t size = sizeof(sync_serial_settings);
7814 sync_serial_settings new_line;
7815 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
7816 struct mgsl_struct *info = dev_to_port(dev);
7817 unsigned int flags;
7818
7819 if (debug_level >= DEBUG_LEVEL_INFO)
7820 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
7821
7822 /* return error if TTY interface open */
Alan Cox8fb06c72008-07-16 21:56:46 +01007823 if (info->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 return -EBUSY;
7825
7826 if (cmd != SIOCWANDEV)
7827 return hdlc_ioctl(dev, ifr, cmd);
7828
7829 switch(ifr->ifr_settings.type) {
7830 case IF_GET_IFACE: /* return current sync_serial_settings */
7831
7832 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
7833 if (ifr->ifr_settings.size < size) {
7834 ifr->ifr_settings.size = size; /* data size wanted */
7835 return -ENOBUFS;
7836 }
7837
7838 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
7839 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
7840 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
7841 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
7842
Salva Peirób19a47e2014-03-11 19:31:23 +01007843 memset(&new_line, 0, sizeof(new_line));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 switch (flags){
7845 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
7846 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
7847 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
7848 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
7849 default: new_line.clock_type = CLOCK_DEFAULT;
7850 }
7851
7852 new_line.clock_rate = info->params.clock_speed;
7853 new_line.loopback = info->params.loopback ? 1:0;
7854
7855 if (copy_to_user(line, &new_line, size))
7856 return -EFAULT;
7857 return 0;
7858
7859 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
7860
7861 if(!capable(CAP_NET_ADMIN))
7862 return -EPERM;
7863 if (copy_from_user(&new_line, line, size))
7864 return -EFAULT;
7865
7866 switch (new_line.clock_type)
7867 {
7868 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
7869 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
7870 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
7871 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
7872 case CLOCK_DEFAULT: flags = info->params.flags &
7873 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
7874 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
7875 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
7876 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
7877 default: return -EINVAL;
7878 }
7879
7880 if (new_line.loopback != 0 && new_line.loopback != 1)
7881 return -EINVAL;
7882
7883 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
7884 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
7885 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
7886 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
7887 info->params.flags |= flags;
7888
7889 info->params.loopback = new_line.loopback;
7890
7891 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
7892 info->params.clock_speed = new_line.clock_rate;
7893 else
7894 info->params.clock_speed = 0;
7895
7896 /* if network interface up, reprogram hardware */
7897 if (info->netcount)
7898 mgsl_program_hw(info);
7899 return 0;
7900
7901 default:
7902 return hdlc_ioctl(dev, ifr, cmd);
7903 }
7904}
7905
7906/**
7907 * called by network layer when transmit timeout is detected
7908 *
7909 * dev pointer to network device structure
7910 */
7911static void hdlcdev_tx_timeout(struct net_device *dev)
7912{
7913 struct mgsl_struct *info = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 unsigned long flags;
7915
7916 if (debug_level >= DEBUG_LEVEL_INFO)
7917 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
7918
Krzysztof Halasa198191c2008-06-30 23:26:53 +02007919 dev->stats.tx_errors++;
7920 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921
7922 spin_lock_irqsave(&info->irq_spinlock,flags);
7923 usc_stop_transmitter(info);
7924 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7925
7926 netif_wake_queue(dev);
7927}
7928
7929/**
7930 * called by device driver when transmit completes
7931 * reenable network layer transmit if stopped
7932 *
7933 * info pointer to device instance information
7934 */
7935static void hdlcdev_tx_done(struct mgsl_struct *info)
7936{
7937 if (netif_queue_stopped(info->netdev))
7938 netif_wake_queue(info->netdev);
7939}
7940
7941/**
7942 * called by device driver when frame received
7943 * pass frame to network layer
7944 *
7945 * info pointer to device instance information
7946 * buf pointer to buffer contianing frame data
7947 * size count of data bytes in buf
7948 */
7949static void hdlcdev_rx(struct mgsl_struct *info, char *buf, int size)
7950{
7951 struct sk_buff *skb = dev_alloc_skb(size);
7952 struct net_device *dev = info->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953
7954 if (debug_level >= DEBUG_LEVEL_INFO)
Krzysztof Halasa198191c2008-06-30 23:26:53 +02007955 printk("hdlcdev_rx(%s)\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956
7957 if (skb == NULL) {
Krzysztof Halasa198191c2008-06-30 23:26:53 +02007958 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n",
7959 dev->name);
7960 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961 return;
7962 }
7963
Krzysztof Halasa198191c2008-06-30 23:26:53 +02007964 memcpy(skb_put(skb, size), buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965
Krzysztof Halasa198191c2008-06-30 23:26:53 +02007966 skb->protocol = hdlc_type_trans(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967
Krzysztof Halasa198191c2008-06-30 23:26:53 +02007968 dev->stats.rx_packets++;
7969 dev->stats.rx_bytes += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970
7971 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972}
7973
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01007974static const struct net_device_ops hdlcdev_ops = {
7975 .ndo_open = hdlcdev_open,
7976 .ndo_stop = hdlcdev_close,
7977 .ndo_change_mtu = hdlc_change_mtu,
7978 .ndo_start_xmit = hdlc_start_xmit,
7979 .ndo_do_ioctl = hdlcdev_ioctl,
7980 .ndo_tx_timeout = hdlcdev_tx_timeout,
7981};
7982
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983/**
7984 * called by device driver when adding device instance
7985 * do generic HDLC initialization
7986 *
7987 * info pointer to device instance information
7988 *
7989 * returns 0 if success, otherwise error code
7990 */
7991static int hdlcdev_init(struct mgsl_struct *info)
7992{
7993 int rc;
7994 struct net_device *dev;
7995 hdlc_device *hdlc;
7996
7997 /* allocate and initialize network and HDLC layer objects */
7998
Greg Kroah-Hartmana271ca32015-04-30 11:22:14 +02007999 dev = alloc_hdlcdev(info);
8000 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
8002 return -ENOMEM;
8003 }
8004
8005 /* for network layer reporting purposes only */
8006 dev->base_addr = info->io_base;
8007 dev->irq = info->irq_level;
8008 dev->dma = info->dma_level;
8009
8010 /* network layer callbacks and settings */
Krzysztof Hałasa991990a2009-01-08 22:52:11 +01008011 dev->netdev_ops = &hdlcdev_ops;
8012 dev->watchdog_timeo = 10 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013 dev->tx_queue_len = 50;
8014
8015 /* generic HDLC layer callbacks and settings */
8016 hdlc = dev_to_hdlc(dev);
8017 hdlc->attach = hdlcdev_attach;
8018 hdlc->xmit = hdlcdev_xmit;
8019
8020 /* register objects with HDLC layer */
Greg Kroah-Hartmana271ca32015-04-30 11:22:14 +02008021 rc = register_hdlc_device(dev);
8022 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
8024 free_netdev(dev);
8025 return rc;
8026 }
8027
8028 info->netdev = dev;
8029 return 0;
8030}
8031
8032/**
8033 * called by device driver when removing device instance
8034 * do generic HDLC cleanup
8035 *
8036 * info pointer to device instance information
8037 */
8038static void hdlcdev_exit(struct mgsl_struct *info)
8039{
8040 unregister_hdlc_device(info->netdev);
8041 free_netdev(info->netdev);
8042 info->netdev = NULL;
8043}
8044
8045#endif /* CONFIG_HDLC */
8046
8047
Bill Pemberton9671f092012-11-19 13:21:50 -05008048static int synclink_init_one (struct pci_dev *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049 const struct pci_device_id *ent)
8050{
8051 struct mgsl_struct *info;
8052
8053 if (pci_enable_device(dev)) {
8054 printk("error enabling pci device %p\n", dev);
8055 return -EIO;
8056 }
8057
Greg Kroah-Hartmana271ca32015-04-30 11:22:14 +02008058 info = mgsl_allocate_device();
8059 if (!info) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060 printk("can't allocate device instance data.\n");
8061 return -EIO;
8062 }
8063
8064 /* Copy user configuration info to device instance data */
8065
8066 info->io_base = pci_resource_start(dev, 2);
8067 info->irq_level = dev->irq;
8068 info->phys_memory_base = pci_resource_start(dev, 3);
8069
8070 /* Because veremap only works on page boundaries we must map
8071 * a larger area than is actually implemented for the LCR
8072 * memory range. We map a full page starting at the page boundary.
8073 */
8074 info->phys_lcr_base = pci_resource_start(dev, 0);
8075 info->lcr_offset = info->phys_lcr_base & (PAGE_SIZE-1);
8076 info->phys_lcr_base &= ~(PAGE_SIZE-1);
8077
8078 info->bus_type = MGSL_BUS_TYPE_PCI;
8079 info->io_addr_size = 8;
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -07008080 info->irq_flags = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081
8082 if (dev->device == 0x0210) {
8083 /* Version 1 PCI9030 based universal PCI adapter */
8084 info->misc_ctrl_value = 0x007c4080;
8085 info->hw_version = 1;
8086 } else {
8087 /* Version 0 PCI9050 based 5V PCI adapter
8088 * A PCI9050 bug prevents reading LCR registers if
8089 * LCR base address bit 7 is set. Maintain shadow
8090 * value so we can write to LCR misc control reg.
8091 */
8092 info->misc_ctrl_value = 0x087e4546;
8093 info->hw_version = 0;
8094 }
8095
8096 mgsl_add_device(info);
8097
8098 return 0;
8099}
8100
Bill Pembertonae8d8a12012-11-19 13:26:18 -05008101static void synclink_remove_one (struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008102{
8103}
8104