PageRenderTime 110ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/char/synclink.c

https://bitbucket.org/evzijst/gittest
C | 8214 lines | 4490 code | 1386 blank | 2338 comment | 929 complexity | c6885025eab792ef4139b80b7ffa6901 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * linux/drivers/char/synclink.c
  3. *
  4. * $Id: synclink.c,v 4.28 2004/08/11 19:30:01 paulkf Exp $
  5. *
  6. * Device driver for Microgate SyncLink ISA and PCI
  7. * high speed multiprotocol serial adapters.
  8. *
  9. * written by Paul Fulghum for Microgate Corporation
  10. * paulkf@microgate.com
  11. *
  12. * Microgate and SyncLink are trademarks of Microgate Corporation
  13. *
  14. * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
  15. *
  16. * Original release 01/11/99
  17. *
  18. * This code is released under the GNU General Public License (GPL)
  19. *
  20. * This driver is primarily intended for use in synchronous
  21. * HDLC mode. Asynchronous mode is also provided.
  22. *
  23. * When operating in synchronous mode, each call to mgsl_write()
  24. * contains exactly one complete HDLC frame. Calling mgsl_put_char
  25. * will start assembling an HDLC frame that will not be sent until
  26. * mgsl_flush_chars or mgsl_write is called.
  27. *
  28. * Synchronous receive data is reported as complete frames. To accomplish
  29. * this, the TTY flip buffer is bypassed (too small to hold largest
  30. * frame and may fragment frames) and the line discipline
  31. * receive entry point is called directly.
  32. *
  33. * This driver has been tested with a slightly modified ppp.c driver
  34. * for synchronous PPP.
  35. *
  36. * 2000/02/16
  37. * Added interface for syncppp.c driver (an alternate synchronous PPP
  38. * implementation that also supports Cisco HDLC). Each device instance
  39. * registers as a tty device AND a network device (if dosyncppp option
  40. * is set for the device). The functionality is determined by which
  41. * device interface is opened.
  42. *
  43. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  44. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  45. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  46. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  47. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  48. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  49. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  50. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  51. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  52. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  53. * OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #if defined(__i386__)
  56. # define BREAKPOINT() asm(" int $3");
  57. #else
  58. # define BREAKPOINT() { }
  59. #endif
  60. #define MAX_ISA_DEVICES 10
  61. #define MAX_PCI_DEVICES 10
  62. #define MAX_TOTAL_DEVICES 20
  63. #include <linux/config.h>
  64. #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>
  80. #include <linux/slab.h>
  81. #include <linux/delay.h>
  82. #include <linux/netdevice.h>
  83. #include <linux/vmalloc.h>
  84. #include <linux/init.h>
  85. #include <asm/serial.h>
  86. #include <linux/delay.h>
  87. #include <linux/ioctl.h>
  88. #include <asm/system.h>
  89. #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>
  97. #ifdef CONFIG_HDLC_MODULE
  98. #define CONFIG_HDLC 1
  99. #endif
  100. #define GET_USER(error,value,addr) error = get_user(value,addr)
  101. #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
  102. #define PUT_USER(error,value,addr) error = put_user(value,addr)
  103. #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
  104. #include <asm/uaccess.h>
  105. #include "linux/synclink.h"
  106. #define RCLRVALUE 0xffff
  107. static MGSL_PARAMS default_params = {
  108. MGSL_MODE_HDLC, /* unsigned long mode */
  109. 0, /* unsigned char loopback; */
  110. HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
  111. HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
  112. 0, /* unsigned long clock_speed; */
  113. 0xff, /* unsigned char addr_filter; */
  114. HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
  115. HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
  116. HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
  117. 9600, /* unsigned long data_rate; */
  118. 8, /* unsigned char data_bits; */
  119. 1, /* unsigned char stop_bits; */
  120. ASYNC_PARITY_NONE /* unsigned char parity; */
  121. };
  122. #define SHARED_MEM_ADDRESS_SIZE 0x40000
  123. #define BUFFERLISTSIZE (PAGE_SIZE)
  124. #define DMABUFFERSIZE (PAGE_SIZE)
  125. #define MAXRXFRAMES 7
  126. typedef struct _DMABUFFERENTRY
  127. {
  128. u32 phys_addr; /* 32-bit flat physical address of data buffer */
  129. u16 count; /* buffer size/data count */
  130. u16 status; /* Control/status field */
  131. u16 rcc; /* character count field */
  132. u16 reserved; /* padding required by 16C32 */
  133. u32 link; /* 32-bit flat link to next buffer entry */
  134. char *virt_addr; /* virtual address of data buffer */
  135. u32 phys_entry; /* physical address of this buffer entry */
  136. } DMABUFFERENTRY, *DMAPBUFFERENTRY;
  137. /* The queue of BH actions to be performed */
  138. #define BH_RECEIVE 1
  139. #define BH_TRANSMIT 2
  140. #define BH_STATUS 4
  141. #define IO_PIN_SHUTDOWN_LIMIT 100
  142. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  143. struct _input_signal_events {
  144. int ri_up;
  145. int ri_down;
  146. int dsr_up;
  147. int dsr_down;
  148. int dcd_up;
  149. int dcd_down;
  150. int cts_up;
  151. int cts_down;
  152. };
  153. /* transmit holding buffer definitions*/
  154. #define MAX_TX_HOLDING_BUFFERS 5
  155. struct tx_holding_buffer {
  156. int buffer_size;
  157. unsigned char * buffer;
  158. };
  159. /*
  160. * Device instance data structure
  161. */
  162. struct mgsl_struct {
  163. int magic;
  164. int flags;
  165. int count; /* count of opens */
  166. int line;
  167. int hw_version;
  168. unsigned short close_delay;
  169. unsigned short closing_wait; /* time to wait before closing */
  170. struct mgsl_icount icount;
  171. struct tty_struct *tty;
  172. int timeout;
  173. int x_char; /* xon/xoff character */
  174. int blocked_open; /* # of blocked opens */
  175. u16 read_status_mask;
  176. u16 ignore_status_mask;
  177. unsigned char *xmit_buf;
  178. int xmit_head;
  179. int xmit_tail;
  180. int xmit_cnt;
  181. wait_queue_head_t open_wait;
  182. wait_queue_head_t close_wait;
  183. wait_queue_head_t status_event_wait_q;
  184. wait_queue_head_t event_wait_q;
  185. struct timer_list tx_timer; /* HDLC transmit timeout timer */
  186. struct mgsl_struct *next_device; /* device list link */
  187. spinlock_t irq_spinlock; /* spinlock for synchronizing with ISR */
  188. struct work_struct task; /* task structure for scheduling bh */
  189. u32 EventMask; /* event trigger mask */
  190. u32 RecordedEvents; /* pending events */
  191. u32 max_frame_size; /* as set by device config */
  192. u32 pending_bh;
  193. int bh_running; /* Protection from multiple */
  194. int isr_overflow;
  195. int bh_requested;
  196. int dcd_chkcount; /* check counts to prevent */
  197. int cts_chkcount; /* too many IRQs if a signal */
  198. int dsr_chkcount; /* is floating */
  199. int ri_chkcount;
  200. char *buffer_list; /* virtual address of Rx & Tx buffer lists */
  201. unsigned long buffer_list_phys;
  202. unsigned int rx_buffer_count; /* count of total allocated Rx buffers */
  203. DMABUFFERENTRY *rx_buffer_list; /* list of receive buffer entries */
  204. unsigned int current_rx_buffer;
  205. int num_tx_dma_buffers; /* number of tx dma frames required */
  206. int tx_dma_buffers_used;
  207. unsigned int tx_buffer_count; /* count of total allocated Tx buffers */
  208. DMABUFFERENTRY *tx_buffer_list; /* list of transmit buffer entries */
  209. int start_tx_dma_buffer; /* tx dma buffer to start tx dma operation */
  210. int current_tx_buffer; /* next tx dma buffer to be loaded */
  211. unsigned char *intermediate_rxbuffer;
  212. int num_tx_holding_buffers; /* number of tx holding buffer allocated */
  213. int get_tx_holding_index; /* next tx holding buffer for adapter to load */
  214. int put_tx_holding_index; /* next tx holding buffer to store user request */
  215. int tx_holding_count; /* number of tx holding buffers waiting */
  216. struct tx_holding_buffer tx_holding_buffers[MAX_TX_HOLDING_BUFFERS];
  217. int rx_enabled;
  218. int rx_overflow;
  219. int rx_rcc_underrun;
  220. int tx_enabled;
  221. int tx_active;
  222. u32 idle_mode;
  223. u16 cmr_value;
  224. u16 tcsr_value;
  225. char device_name[25]; /* device instance name */
  226. unsigned int bus_type; /* expansion bus type (ISA,EISA,PCI) */
  227. unsigned char bus; /* expansion bus number (zero based) */
  228. unsigned char function; /* PCI device number */
  229. unsigned int io_base; /* base I/O address of adapter */
  230. unsigned int io_addr_size; /* size of the I/O address range */
  231. int io_addr_requested; /* nonzero if I/O address requested */
  232. unsigned int irq_level; /* interrupt level */
  233. unsigned long irq_flags;
  234. int irq_requested; /* nonzero if IRQ requested */
  235. unsigned int dma_level; /* DMA channel */
  236. int dma_requested; /* nonzero if dma channel requested */
  237. u16 mbre_bit;
  238. u16 loopback_bits;
  239. u16 usc_idle_mode;
  240. MGSL_PARAMS params; /* communications parameters */
  241. unsigned char serial_signals; /* current serial signal states */
  242. int irq_occurred; /* for diagnostics use */
  243. unsigned int init_error; /* Initialization startup error (DIAGS) */
  244. int fDiagnosticsmode; /* Driver in Diagnostic mode? (DIAGS) */
  245. u32 last_mem_alloc;
  246. unsigned char* memory_base; /* shared memory address (PCI only) */
  247. u32 phys_memory_base;
  248. int shared_mem_requested;
  249. unsigned char* lcr_base; /* local config registers (PCI only) */
  250. u32 phys_lcr_base;
  251. u32 lcr_offset;
  252. int lcr_mem_requested;
  253. u32 misc_ctrl_value;
  254. char flag_buf[MAX_ASYNC_BUFFER_SIZE];
  255. char char_buf[MAX_ASYNC_BUFFER_SIZE];
  256. BOOLEAN drop_rts_on_tx_done;
  257. BOOLEAN loopmode_insert_requested;
  258. BOOLEAN loopmode_send_done_requested;
  259. struct _input_signal_events input_signal_events;
  260. /* generic HDLC device parts */
  261. int netcount;
  262. int dosyncppp;
  263. spinlock_t netlock;
  264. #ifdef CONFIG_HDLC
  265. struct net_device *netdev;
  266. #endif
  267. };
  268. #define MGSL_MAGIC 0x5401
  269. /*
  270. * The size of the serial xmit buffer is 1 page, or 4096 bytes
  271. */
  272. #ifndef SERIAL_XMIT_SIZE
  273. #define SERIAL_XMIT_SIZE 4096
  274. #endif
  275. /*
  276. * These macros define the offsets used in calculating the
  277. * I/O address of the specified USC registers.
  278. */
  279. #define DCPIN 2 /* Bit 1 of I/O address */
  280. #define SDPIN 4 /* Bit 2 of I/O address */
  281. #define DCAR 0 /* DMA command/address register */
  282. #define CCAR SDPIN /* channel command/address register */
  283. #define DATAREG DCPIN + SDPIN /* serial data register */
  284. #define MSBONLY 0x41
  285. #define LSBONLY 0x40
  286. /*
  287. * These macros define the register address (ordinal number)
  288. * used for writing address/value pairs to the USC.
  289. */
  290. #define CMR 0x02 /* Channel mode Register */
  291. #define CCSR 0x04 /* Channel Command/status Register */
  292. #define CCR 0x06 /* Channel Control Register */
  293. #define PSR 0x08 /* Port status Register */
  294. #define PCR 0x0a /* Port Control Register */
  295. #define TMDR 0x0c /* Test mode Data Register */
  296. #define TMCR 0x0e /* Test mode Control Register */
  297. #define CMCR 0x10 /* Clock mode Control Register */
  298. #define HCR 0x12 /* Hardware Configuration Register */
  299. #define IVR 0x14 /* Interrupt Vector Register */
  300. #define IOCR 0x16 /* Input/Output Control Register */
  301. #define ICR 0x18 /* Interrupt Control Register */
  302. #define DCCR 0x1a /* Daisy Chain Control Register */
  303. #define MISR 0x1c /* Misc Interrupt status Register */
  304. #define SICR 0x1e /* status Interrupt Control Register */
  305. #define RDR 0x20 /* Receive Data Register */
  306. #define RMR 0x22 /* Receive mode Register */
  307. #define RCSR 0x24 /* Receive Command/status Register */
  308. #define RICR 0x26 /* Receive Interrupt Control Register */
  309. #define RSR 0x28 /* Receive Sync Register */
  310. #define RCLR 0x2a /* Receive count Limit Register */
  311. #define RCCR 0x2c /* Receive Character count Register */
  312. #define TC0R 0x2e /* Time Constant 0 Register */
  313. #define TDR 0x30 /* Transmit Data Register */
  314. #define TMR 0x32 /* Transmit mode Register */
  315. #define TCSR 0x34 /* Transmit Command/status Register */
  316. #define TICR 0x36 /* Transmit Interrupt Control Register */
  317. #define TSR 0x38 /* Transmit Sync Register */
  318. #define TCLR 0x3a /* Transmit count Limit Register */
  319. #define TCCR 0x3c /* Transmit Character count Register */
  320. #define TC1R 0x3e /* Time Constant 1 Register */
  321. /*
  322. * MACRO DEFINITIONS FOR DMA REGISTERS
  323. */
  324. #define DCR 0x06 /* DMA Control Register (shared) */
  325. #define DACR 0x08 /* DMA Array count Register (shared) */
  326. #define BDCR 0x12 /* Burst/Dwell Control Register (shared) */
  327. #define DIVR 0x14 /* DMA Interrupt Vector Register (shared) */
  328. #define DICR 0x18 /* DMA Interrupt Control Register (shared) */
  329. #define CDIR 0x1a /* Clear DMA Interrupt Register (shared) */
  330. #define SDIR 0x1c /* Set DMA Interrupt Register (shared) */
  331. #define TDMR 0x02 /* Transmit DMA mode Register */
  332. #define TDIAR 0x1e /* Transmit DMA Interrupt Arm Register */
  333. #define TBCR 0x2a /* Transmit Byte count Register */
  334. #define TARL 0x2c /* Transmit Address Register (low) */
  335. #define TARU 0x2e /* Transmit Address Register (high) */
  336. #define NTBCR 0x3a /* Next Transmit Byte count Register */
  337. #define NTARL 0x3c /* Next Transmit Address Register (low) */
  338. #define NTARU 0x3e /* Next Transmit Address Register (high) */
  339. #define RDMR 0x82 /* Receive DMA mode Register (non-shared) */
  340. #define RDIAR 0x9e /* Receive DMA Interrupt Arm Register */
  341. #define RBCR 0xaa /* Receive Byte count Register */
  342. #define RARL 0xac /* Receive Address Register (low) */
  343. #define RARU 0xae /* Receive Address Register (high) */
  344. #define NRBCR 0xba /* Next Receive Byte count Register */
  345. #define NRARL 0xbc /* Next Receive Address Register (low) */
  346. #define NRARU 0xbe /* Next Receive Address Register (high) */
  347. /*
  348. * MACRO DEFINITIONS FOR MODEM STATUS BITS
  349. */
  350. #define MODEMSTATUS_DTR 0x80
  351. #define MODEMSTATUS_DSR 0x40
  352. #define MODEMSTATUS_RTS 0x20
  353. #define MODEMSTATUS_CTS 0x10
  354. #define MODEMSTATUS_RI 0x04
  355. #define MODEMSTATUS_DCD 0x01
  356. /*
  357. * Channel Command/Address Register (CCAR) Command Codes
  358. */
  359. #define RTCmd_Null 0x0000
  360. #define RTCmd_ResetHighestIus 0x1000
  361. #define RTCmd_TriggerChannelLoadDma 0x2000
  362. #define RTCmd_TriggerRxDma 0x2800
  363. #define RTCmd_TriggerTxDma 0x3000
  364. #define RTCmd_TriggerRxAndTxDma 0x3800
  365. #define RTCmd_PurgeRxFifo 0x4800
  366. #define RTCmd_PurgeTxFifo 0x5000
  367. #define RTCmd_PurgeRxAndTxFifo 0x5800
  368. #define RTCmd_LoadRcc 0x6800
  369. #define RTCmd_LoadTcc 0x7000
  370. #define RTCmd_LoadRccAndTcc 0x7800
  371. #define RTCmd_LoadTC0 0x8800
  372. #define RTCmd_LoadTC1 0x9000
  373. #define RTCmd_LoadTC0AndTC1 0x9800
  374. #define RTCmd_SerialDataLSBFirst 0xa000
  375. #define RTCmd_SerialDataMSBFirst 0xa800
  376. #define RTCmd_SelectBigEndian 0xb000
  377. #define RTCmd_SelectLittleEndian 0xb800
  378. /*
  379. * DMA Command/Address Register (DCAR) Command Codes
  380. */
  381. #define DmaCmd_Null 0x0000
  382. #define DmaCmd_ResetTxChannel 0x1000
  383. #define DmaCmd_ResetRxChannel 0x1200
  384. #define DmaCmd_StartTxChannel 0x2000
  385. #define DmaCmd_StartRxChannel 0x2200
  386. #define DmaCmd_ContinueTxChannel 0x3000
  387. #define DmaCmd_ContinueRxChannel 0x3200
  388. #define DmaCmd_PauseTxChannel 0x4000
  389. #define DmaCmd_PauseRxChannel 0x4200
  390. #define DmaCmd_AbortTxChannel 0x5000
  391. #define DmaCmd_AbortRxChannel 0x5200
  392. #define DmaCmd_InitTxChannel 0x7000
  393. #define DmaCmd_InitRxChannel 0x7200
  394. #define DmaCmd_ResetHighestDmaIus 0x8000
  395. #define DmaCmd_ResetAllChannels 0x9000
  396. #define DmaCmd_StartAllChannels 0xa000
  397. #define DmaCmd_ContinueAllChannels 0xb000
  398. #define DmaCmd_PauseAllChannels 0xc000
  399. #define DmaCmd_AbortAllChannels 0xd000
  400. #define DmaCmd_InitAllChannels 0xf000
  401. #define TCmd_Null 0x0000
  402. #define TCmd_ClearTxCRC 0x2000
  403. #define TCmd_SelectTicrTtsaData 0x4000
  404. #define TCmd_SelectTicrTxFifostatus 0x5000
  405. #define TCmd_SelectTicrIntLevel 0x6000
  406. #define TCmd_SelectTicrdma_level 0x7000
  407. #define TCmd_SendFrame 0x8000
  408. #define TCmd_SendAbort 0x9000
  409. #define TCmd_EnableDleInsertion 0xc000
  410. #define TCmd_DisableDleInsertion 0xd000
  411. #define TCmd_ClearEofEom 0xe000
  412. #define TCmd_SetEofEom 0xf000
  413. #define RCmd_Null 0x0000
  414. #define RCmd_ClearRxCRC 0x2000
  415. #define RCmd_EnterHuntmode 0x3000
  416. #define RCmd_SelectRicrRtsaData 0x4000
  417. #define RCmd_SelectRicrRxFifostatus 0x5000
  418. #define RCmd_SelectRicrIntLevel 0x6000
  419. #define RCmd_SelectRicrdma_level 0x7000
  420. /*
  421. * Bits for enabling and disabling IRQs in Interrupt Control Register (ICR)
  422. */
  423. #define RECEIVE_STATUS BIT5
  424. #define RECEIVE_DATA BIT4
  425. #define TRANSMIT_STATUS BIT3
  426. #define TRANSMIT_DATA BIT2
  427. #define IO_PIN BIT1
  428. #define MISC BIT0
  429. /*
  430. * Receive status Bits in Receive Command/status Register RCSR
  431. */
  432. #define RXSTATUS_SHORT_FRAME BIT8
  433. #define RXSTATUS_CODE_VIOLATION BIT8
  434. #define RXSTATUS_EXITED_HUNT BIT7
  435. #define RXSTATUS_IDLE_RECEIVED BIT6
  436. #define RXSTATUS_BREAK_RECEIVED BIT5
  437. #define RXSTATUS_ABORT_RECEIVED BIT5
  438. #define RXSTATUS_RXBOUND BIT4
  439. #define RXSTATUS_CRC_ERROR BIT3
  440. #define RXSTATUS_FRAMING_ERROR BIT3
  441. #define RXSTATUS_ABORT BIT2
  442. #define RXSTATUS_PARITY_ERROR BIT2
  443. #define RXSTATUS_OVERRUN BIT1
  444. #define RXSTATUS_DATA_AVAILABLE BIT0
  445. #define RXSTATUS_ALL 0x01f6
  446. #define usc_UnlatchRxstatusBits(a,b) usc_OutReg( (a), RCSR, (u16)((b) & RXSTATUS_ALL) )
  447. /*
  448. * Values for setting transmit idle mode in
  449. * Transmit Control/status Register (TCSR)
  450. */
  451. #define IDLEMODE_FLAGS 0x0000
  452. #define IDLEMODE_ALT_ONE_ZERO 0x0100
  453. #define IDLEMODE_ZERO 0x0200
  454. #define IDLEMODE_ONE 0x0300
  455. #define IDLEMODE_ALT_MARK_SPACE 0x0500
  456. #define IDLEMODE_SPACE 0x0600
  457. #define IDLEMODE_MARK 0x0700
  458. #define IDLEMODE_MASK 0x0700
  459. /*
  460. * IUSC revision identifiers
  461. */
  462. #define IUSC_SL1660 0x4d44
  463. #define IUSC_PRE_SL1660 0x4553
  464. /*
  465. * Transmit status Bits in Transmit Command/status Register (TCSR)
  466. */
  467. #define TCSR_PRESERVE 0x0F00
  468. #define TCSR_UNDERWAIT BIT11
  469. #define TXSTATUS_PREAMBLE_SENT BIT7
  470. #define TXSTATUS_IDLE_SENT BIT6
  471. #define TXSTATUS_ABORT_SENT BIT5
  472. #define TXSTATUS_EOF_SENT BIT4
  473. #define TXSTATUS_EOM_SENT BIT4
  474. #define TXSTATUS_CRC_SENT BIT3
  475. #define TXSTATUS_ALL_SENT BIT2
  476. #define TXSTATUS_UNDERRUN BIT1
  477. #define TXSTATUS_FIFO_EMPTY BIT0
  478. #define TXSTATUS_ALL 0x00fa
  479. #define usc_UnlatchTxstatusBits(a,b) usc_OutReg( (a), TCSR, (u16)((a)->tcsr_value + ((b) & 0x00FF)) )
  480. #define MISCSTATUS_RXC_LATCHED BIT15
  481. #define MISCSTATUS_RXC BIT14
  482. #define MISCSTATUS_TXC_LATCHED BIT13
  483. #define MISCSTATUS_TXC BIT12
  484. #define MISCSTATUS_RI_LATCHED BIT11
  485. #define MISCSTATUS_RI BIT10
  486. #define MISCSTATUS_DSR_LATCHED BIT9
  487. #define MISCSTATUS_DSR BIT8
  488. #define MISCSTATUS_DCD_LATCHED BIT7
  489. #define MISCSTATUS_DCD BIT6
  490. #define MISCSTATUS_CTS_LATCHED BIT5
  491. #define MISCSTATUS_CTS BIT4
  492. #define MISCSTATUS_RCC_UNDERRUN BIT3
  493. #define MISCSTATUS_DPLL_NO_SYNC BIT2
  494. #define MISCSTATUS_BRG1_ZERO BIT1
  495. #define MISCSTATUS_BRG0_ZERO BIT0
  496. #define usc_UnlatchIostatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0xaaa0))
  497. #define usc_UnlatchMiscstatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0x000f))
  498. #define SICR_RXC_ACTIVE BIT15
  499. #define SICR_RXC_INACTIVE BIT14
  500. #define SICR_RXC (BIT15+BIT14)
  501. #define SICR_TXC_ACTIVE BIT13
  502. #define SICR_TXC_INACTIVE BIT12
  503. #define SICR_TXC (BIT13+BIT12)
  504. #define SICR_RI_ACTIVE BIT11
  505. #define SICR_RI_INACTIVE BIT10
  506. #define SICR_RI (BIT11+BIT10)
  507. #define SICR_DSR_ACTIVE BIT9
  508. #define SICR_DSR_INACTIVE BIT8
  509. #define SICR_DSR (BIT9+BIT8)
  510. #define SICR_DCD_ACTIVE BIT7
  511. #define SICR_DCD_INACTIVE BIT6
  512. #define SICR_DCD (BIT7+BIT6)
  513. #define SICR_CTS_ACTIVE BIT5
  514. #define SICR_CTS_INACTIVE BIT4
  515. #define SICR_CTS (BIT5+BIT4)
  516. #define SICR_RCC_UNDERFLOW BIT3
  517. #define SICR_DPLL_NO_SYNC BIT2
  518. #define SICR_BRG1_ZERO BIT1
  519. #define SICR_BRG0_ZERO BIT0
  520. void usc_DisableMasterIrqBit( struct mgsl_struct *info );
  521. void usc_EnableMasterIrqBit( struct mgsl_struct *info );
  522. void usc_EnableInterrupts( struct mgsl_struct *info, u16 IrqMask );
  523. void usc_DisableInterrupts( struct mgsl_struct *info, u16 IrqMask );
  524. void usc_ClearIrqPendingBits( struct mgsl_struct *info, u16 IrqMask );
  525. #define usc_EnableInterrupts( a, b ) \
  526. usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0xc0 + (b)) )
  527. #define usc_DisableInterrupts( a, b ) \
  528. usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0x80 + (b)) )
  529. #define usc_EnableMasterIrqBit(a) \
  530. usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0x0f00) + 0xb000) )
  531. #define usc_DisableMasterIrqBit(a) \
  532. usc_OutReg( (a), ICR, (u16)(usc_InReg((a),ICR) & 0x7f00) )
  533. #define usc_ClearIrqPendingBits( a, b ) usc_OutReg( (a), DCCR, 0x40 + (b) )
  534. /*
  535. * Transmit status Bits in Transmit Control status Register (TCSR)
  536. * and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0)
  537. */
  538. #define TXSTATUS_PREAMBLE_SENT BIT7
  539. #define TXSTATUS_IDLE_SENT BIT6
  540. #define TXSTATUS_ABORT_SENT BIT5
  541. #define TXSTATUS_EOF BIT4
  542. #define TXSTATUS_CRC_SENT BIT3
  543. #define TXSTATUS_ALL_SENT BIT2
  544. #define TXSTATUS_UNDERRUN BIT1
  545. #define TXSTATUS_FIFO_EMPTY BIT0
  546. #define DICR_MASTER BIT15
  547. #define DICR_TRANSMIT BIT0
  548. #define DICR_RECEIVE BIT1
  549. #define usc_EnableDmaInterrupts(a,b) \
  550. usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) | (b)) )
  551. #define usc_DisableDmaInterrupts(a,b) \
  552. usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) & ~(b)) )
  553. #define usc_EnableStatusIrqs(a,b) \
  554. usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) | (b)) )
  555. #define usc_DisablestatusIrqs(a,b) \
  556. usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) & ~(b)) )
  557. /* Transmit status Bits in Transmit Control status Register (TCSR) */
  558. /* and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0) */
  559. #define DISABLE_UNCONDITIONAL 0
  560. #define DISABLE_END_OF_FRAME 1
  561. #define ENABLE_UNCONDITIONAL 2
  562. #define ENABLE_AUTO_CTS 3
  563. #define ENABLE_AUTO_DCD 3
  564. #define usc_EnableTransmitter(a,b) \
  565. usc_OutReg( (a), TMR, (u16)((usc_InReg((a),TMR) & 0xfffc) | (b)) )
  566. #define usc_EnableReceiver(a,b) \
  567. usc_OutReg( (a), RMR, (u16)((usc_InReg((a),RMR) & 0xfffc) | (b)) )
  568. static u16 usc_InDmaReg( struct mgsl_struct *info, u16 Port );
  569. static void usc_OutDmaReg( struct mgsl_struct *info, u16 Port, u16 Value );
  570. static void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd );
  571. static u16 usc_InReg( struct mgsl_struct *info, u16 Port );
  572. static void usc_OutReg( struct mgsl_struct *info, u16 Port, u16 Value );
  573. static void usc_RTCmd( struct mgsl_struct *info, u16 Cmd );
  574. void usc_RCmd( struct mgsl_struct *info, u16 Cmd );
  575. void usc_TCmd( struct mgsl_struct *info, u16 Cmd );
  576. #define usc_TCmd(a,b) usc_OutReg((a), TCSR, (u16)((a)->tcsr_value + (b)))
  577. #define usc_RCmd(a,b) usc_OutReg((a), RCSR, (b))
  578. #define usc_SetTransmitSyncChars(a,s0,s1) usc_OutReg((a), TSR, (u16)(((u16)s0<<8)|(u16)s1))
  579. static void usc_process_rxoverrun_sync( struct mgsl_struct *info );
  580. static void usc_start_receiver( struct mgsl_struct *info );
  581. static void usc_stop_receiver( struct mgsl_struct *info );
  582. static void usc_start_transmitter( struct mgsl_struct *info );
  583. static void usc_stop_transmitter( struct mgsl_struct *info );
  584. static void usc_set_txidle( struct mgsl_struct *info );
  585. static void usc_load_txfifo( struct mgsl_struct *info );
  586. static void usc_enable_aux_clock( struct mgsl_struct *info, u32 DataRate );
  587. static void usc_enable_loopback( struct mgsl_struct *info, int enable );
  588. static void usc_get_serial_signals( struct mgsl_struct *info );
  589. static void usc_set_serial_signals( struct mgsl_struct *info );
  590. static void usc_reset( struct mgsl_struct *info );
  591. static void usc_set_sync_mode( struct mgsl_struct *info );
  592. static void usc_set_sdlc_mode( struct mgsl_struct *info );
  593. static void usc_set_async_mode( struct mgsl_struct *info );
  594. static void usc_enable_async_clock( struct mgsl_struct *info, u32 DataRate );
  595. static void usc_loopback_frame( struct mgsl_struct *info );
  596. static void mgsl_tx_timeout(unsigned long context);
  597. static void usc_loopmode_cancel_transmit( struct mgsl_struct * info );
  598. static void usc_loopmode_insert_request( struct mgsl_struct * info );
  599. static int usc_loopmode_active( struct mgsl_struct * info);
  600. static void usc_loopmode_send_done( struct mgsl_struct * info );
  601. static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg);
  602. #ifdef CONFIG_HDLC
  603. #define dev_to_port(D) (dev_to_hdlc(D)->priv)
  604. static void hdlcdev_tx_done(struct mgsl_struct *info);
  605. static void hdlcdev_rx(struct mgsl_struct *info, char *buf, int size);
  606. static int hdlcdev_init(struct mgsl_struct *info);
  607. static void hdlcdev_exit(struct mgsl_struct *info);
  608. #endif
  609. /*
  610. * Defines a BUS descriptor value for the PCI adapter
  611. * local bus address ranges.
  612. */
  613. #define BUS_DESCRIPTOR( WrHold, WrDly, RdDly, Nwdd, Nwad, Nxda, Nrdd, Nrad ) \
  614. (0x00400020 + \
  615. ((WrHold) << 30) + \
  616. ((WrDly) << 28) + \
  617. ((RdDly) << 26) + \
  618. ((Nwdd) << 20) + \
  619. ((Nwad) << 15) + \
  620. ((Nxda) << 13) + \
  621. ((Nrdd) << 11) + \
  622. ((Nrad) << 6) )
  623. static void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit);
  624. /*
  625. * Adapter diagnostic routines
  626. */
  627. static BOOLEAN mgsl_register_test( struct mgsl_struct *info );
  628. static BOOLEAN mgsl_irq_test( struct mgsl_struct *info );
  629. static BOOLEAN mgsl_dma_test( struct mgsl_struct *info );
  630. static BOOLEAN mgsl_memory_test( struct mgsl_struct *info );
  631. static int mgsl_adapter_test( struct mgsl_struct *info );
  632. /*
  633. * device and resource management routines
  634. */
  635. static int mgsl_claim_resources(struct mgsl_struct *info);
  636. static void mgsl_release_resources(struct mgsl_struct *info);
  637. static void mgsl_add_device(struct mgsl_struct *info);
  638. static struct mgsl_struct* mgsl_allocate_device(void);
  639. /*
  640. * DMA buffer manupulation functions.
  641. */
  642. static void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex );
  643. static int mgsl_get_rx_frame( struct mgsl_struct *info );
  644. static int mgsl_get_raw_rx_frame( struct mgsl_struct *info );
  645. static void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info );
  646. static void mgsl_reset_tx_dma_buffers( struct mgsl_struct *info );
  647. static int num_free_tx_dma_buffers(struct mgsl_struct *info);
  648. static void mgsl_load_tx_dma_buffer( struct mgsl_struct *info, const char *Buffer, unsigned int BufferSize);
  649. static void mgsl_load_pci_memory(char* TargetPtr, const char* SourcePtr, unsigned short count);
  650. /*
  651. * DMA and Shared Memory buffer allocation and formatting
  652. */
  653. static int mgsl_allocate_dma_buffers(struct mgsl_struct *info);
  654. static void mgsl_free_dma_buffers(struct mgsl_struct *info);
  655. static int mgsl_alloc_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
  656. static void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
  657. static int mgsl_alloc_buffer_list_memory(struct mgsl_struct *info);
  658. static void mgsl_free_buffer_list_memory(struct mgsl_struct *info);
  659. static int mgsl_alloc_intermediate_rxbuffer_memory(struct mgsl_struct *info);
  660. static void mgsl_free_intermediate_rxbuffer_memory(struct mgsl_struct *info);
  661. static int mgsl_alloc_intermediate_txbuffer_memory(struct mgsl_struct *info);
  662. static void mgsl_free_intermediate_txbuffer_memory(struct mgsl_struct *info);
  663. static int load_next_tx_holding_buffer(struct mgsl_struct *info);
  664. static int save_tx_buffer_request(struct mgsl_struct *info,const char *Buffer, unsigned int BufferSize);
  665. /*
  666. * Bottom half interrupt handlers
  667. */
  668. static void mgsl_bh_handler(void* Context);
  669. static void mgsl_bh_receive(struct mgsl_struct *info);
  670. static void mgsl_bh_transmit(struct mgsl_struct *info);
  671. static void mgsl_bh_status(struct mgsl_struct *info);
  672. /*
  673. * Interrupt handler routines and dispatch table.
  674. */
  675. static void mgsl_isr_null( struct mgsl_struct *info );
  676. static void mgsl_isr_transmit_data( struct mgsl_struct *info );
  677. static void mgsl_isr_receive_data( struct mgsl_struct *info );
  678. static void mgsl_isr_receive_status( struct mgsl_struct *info );
  679. static void mgsl_isr_transmit_status( struct mgsl_struct *info );
  680. static void mgsl_isr_io_pin( struct mgsl_struct *info );
  681. static void mgsl_isr_misc( struct mgsl_struct *info );
  682. static void mgsl_isr_receive_dma( struct mgsl_struct *info );
  683. static void mgsl_isr_transmit_dma( struct mgsl_struct *info );
  684. typedef void (*isr_dispatch_func)(struct mgsl_struct *);
  685. static isr_dispatch_func UscIsrTable[7] =
  686. {
  687. mgsl_isr_null,
  688. mgsl_isr_misc,
  689. mgsl_isr_io_pin,
  690. mgsl_isr_transmit_data,
  691. mgsl_isr_transmit_status,
  692. mgsl_isr_receive_data,
  693. mgsl_isr_receive_status
  694. };
  695. /*
  696. * ioctl call handlers
  697. */
  698. static int tiocmget(struct tty_struct *tty, struct file *file);
  699. static int tiocmset(struct tty_struct *tty, struct file *file,
  700. unsigned int set, unsigned int clear);
  701. static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount
  702. __user *user_icount);
  703. static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_params);
  704. static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_params);
  705. static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode);
  706. static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode);
  707. static int mgsl_txenable(struct mgsl_struct * info, int enable);
  708. static int mgsl_txabort(struct mgsl_struct * info);
  709. static int mgsl_rxenable(struct mgsl_struct * info, int enable);
  710. static int mgsl_wait_event(struct mgsl_struct * info, int __user *mask);
  711. static int mgsl_loopmode_send_done( struct mgsl_struct * info );
  712. /* set non-zero on successful registration with PCI subsystem */
  713. static int pci_registered;
  714. /*
  715. * Global linked list of SyncLink devices
  716. */
  717. static struct mgsl_struct *mgsl_device_list;
  718. static int mgsl_device_count;
  719. /*
  720. * Set this param to non-zero to load eax with the
  721. * .text section address and breakpoint on module load.
  722. * This is useful for use with gdb and add-symbol-file command.
  723. */
  724. static int break_on_load;
  725. /*
  726. * Driver major number, defaults to zero to get auto
  727. * assigned major number. May be forced as module parameter.
  728. */
  729. static int ttymajor;
  730. /*
  731. * Array of user specified options for ISA adapters.
  732. */
  733. static int io[MAX_ISA_DEVICES];
  734. static int irq[MAX_ISA_DEVICES];
  735. static int dma[MAX_ISA_DEVICES];
  736. static int debug_level;
  737. static int maxframe[MAX_TOTAL_DEVICES];
  738. static int dosyncppp[MAX_TOTAL_DEVICES];
  739. static int txdmabufs[MAX_TOTAL_DEVICES];
  740. static int txholdbufs[MAX_TOTAL_DEVICES];
  741. module_param(break_on_load, bool, 0);
  742. module_param(ttymajor, int, 0);
  743. module_param_array(io, int, NULL, 0);
  744. module_param_array(irq, int, NULL, 0);
  745. module_param_array(dma, int, NULL, 0);
  746. module_param(debug_level, int, 0);
  747. module_param_array(maxframe, int, NULL, 0);
  748. module_param_array(dosyncppp, int, NULL, 0);
  749. module_param_array(txdmabufs, int, NULL, 0);
  750. module_param_array(txholdbufs, int, NULL, 0);
  751. static char *driver_name = "SyncLink serial driver";
  752. static char *driver_version = "$Revision: 4.28 $";
  753. static int synclink_init_one (struct pci_dev *dev,
  754. const struct pci_device_id *ent);
  755. static void synclink_remove_one (struct pci_dev *dev);
  756. static struct pci_device_id synclink_pci_tbl[] = {
  757. { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_USC, PCI_ANY_ID, PCI_ANY_ID, },
  758. { PCI_VENDOR_ID_MICROGATE, 0x0210, PCI_ANY_ID, PCI_ANY_ID, },
  759. { 0, }, /* terminate list */
  760. };
  761. MODULE_DEVICE_TABLE(pci, synclink_pci_tbl);
  762. MODULE_LICENSE("GPL");
  763. static struct pci_driver synclink_pci_driver = {
  764. .name = "synclink",
  765. .id_table = synclink_pci_tbl,
  766. .probe = synclink_init_one,
  767. .remove = __devexit_p(synclink_remove_one),
  768. };
  769. static struct tty_driver *serial_driver;
  770. /* number of characters left in xmit buffer before we ask for more */
  771. #define WAKEUP_CHARS 256
  772. static void mgsl_change_params(struct mgsl_struct *info);
  773. static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout);
  774. /*
  775. * 1st function defined in .text section. Calling this function in
  776. * init_module() followed by a breakpoint allows a remote debugger
  777. * (gdb) to get the .text address for the add-symbol-file command.
  778. * This allows remote debugging of dynamically loadable modules.
  779. */
  780. static void* mgsl_get_text_ptr(void)
  781. {
  782. return mgsl_get_text_ptr;
  783. }
  784. /*
  785. * tmp_buf is used as a temporary buffer by mgsl_write. We need to
  786. * lock it in case the COPY_FROM_USER blocks while swapping in a page,
  787. * and some other program tries to do a serial write at the same time.
  788. * Since the lock will only come under contention when the system is
  789. * swapping and available memory is low, it makes sense to share one
  790. * buffer across all the serial ioports, since it significantly saves
  791. * memory if large numbers of serial ports are open.
  792. */
  793. static unsigned char *tmp_buf;
  794. static DECLARE_MUTEX(tmp_buf_sem);
  795. static inline int mgsl_paranoia_check(struct mgsl_struct *info,
  796. char *name, const char *routine)
  797. {
  798. #ifdef MGSL_PARANOIA_CHECK
  799. static const char *badmagic =
  800. "Warning: bad magic number for mgsl struct (%s) in %s\n";
  801. static const char *badinfo =
  802. "Warning: null mgsl_struct for (%s) in %s\n";
  803. if (!info) {
  804. printk(badinfo, name, routine);
  805. return 1;
  806. }
  807. if (info->magic != MGSL_MAGIC) {
  808. printk(badmagic, name, routine);
  809. return 1;
  810. }
  811. #else
  812. if (!info)
  813. return 1;
  814. #endif
  815. return 0;
  816. }
  817. /**
  818. * line discipline callback wrappers
  819. *
  820. * The wrappers maintain line discipline references
  821. * while calling into the line discipline.
  822. *
  823. * ldisc_receive_buf - pass receive data to line discipline
  824. */
  825. static void ldisc_receive_buf(struct tty_struct *tty,
  826. const __u8 *data, char *flags, int count)
  827. {
  828. struct tty_ldisc *ld;
  829. if (!tty)
  830. return;
  831. ld = tty_ldisc_ref(tty);
  832. if (ld) {
  833. if (ld->receive_buf)
  834. ld->receive_buf(tty, data, flags, count);
  835. tty_ldisc_deref(ld);
  836. }
  837. }
  838. /* mgsl_stop() throttle (stop) transmitter
  839. *
  840. * Arguments: tty pointer to tty info structure
  841. * Return Value: None
  842. */
  843. static void mgsl_stop(struct tty_struct *tty)
  844. {
  845. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  846. unsigned long flags;
  847. if (mgsl_paranoia_check(info, tty->name, "mgsl_stop"))
  848. return;
  849. if ( debug_level >= DEBUG_LEVEL_INFO )
  850. printk("mgsl_stop(%s)\n",info->device_name);
  851. spin_lock_irqsave(&info->irq_spinlock,flags);
  852. if (info->tx_enabled)
  853. usc_stop_transmitter(info);
  854. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  855. } /* end of mgsl_stop() */
  856. /* mgsl_start() release (start) transmitter
  857. *
  858. * Arguments: tty pointer to tty info structure
  859. * Return Value: None
  860. */
  861. static void mgsl_start(struct tty_struct *tty)
  862. {
  863. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  864. unsigned long flags;
  865. if (mgsl_paranoia_check(info, tty->name, "mgsl_start"))
  866. return;
  867. if ( debug_level >= DEBUG_LEVEL_INFO )
  868. printk("mgsl_start(%s)\n",info->device_name);
  869. spin_lock_irqsave(&info->irq_spinlock,flags);
  870. if (!info->tx_enabled)
  871. usc_start_transmitter(info);
  872. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  873. } /* end of mgsl_start() */
  874. /*
  875. * Bottom half work queue access functions
  876. */
  877. /* mgsl_bh_action() Return next bottom half action to perform.
  878. * Return Value: BH action code or 0 if nothing to do.
  879. */
  880. static int mgsl_bh_action(struct mgsl_struct *info)
  881. {
  882. unsigned long flags;
  883. int rc = 0;
  884. spin_lock_irqsave(&info->irq_spinlock,flags);
  885. if (info->pending_bh & BH_RECEIVE) {
  886. info->pending_bh &= ~BH_RECEIVE;
  887. rc = BH_RECEIVE;
  888. } else if (info->pending_bh & BH_TRANSMIT) {
  889. info->pending_bh &= ~BH_TRANSMIT;
  890. rc = BH_TRANSMIT;
  891. } else if (info->pending_bh & BH_STATUS) {
  892. info->pending_bh &= ~BH_STATUS;
  893. rc = BH_STATUS;
  894. }
  895. if (!rc) {
  896. /* Mark BH routine as complete */
  897. info->bh_running = 0;
  898. info->bh_requested = 0;
  899. }
  900. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  901. return rc;
  902. }
  903. /*
  904. * Perform bottom half processing of work items queued by ISR.
  905. */
  906. static void mgsl_bh_handler(void* Context)
  907. {
  908. struct mgsl_struct *info = (struct mgsl_struct*)Context;
  909. int action;
  910. if (!info)
  911. return;
  912. if ( debug_level >= DEBUG_LEVEL_BH )
  913. printk( "%s(%d):mgsl_bh_handler(%s) entry\n",
  914. __FILE__,__LINE__,info->device_name);
  915. info->bh_running = 1;
  916. while((action = mgsl_bh_action(info)) != 0) {
  917. /* Process work item */
  918. if ( debug_level >= DEBUG_LEVEL_BH )
  919. printk( "%s(%d):mgsl_bh_handler() work item action=%d\n",
  920. __FILE__,__LINE__,action);
  921. switch (action) {
  922. case BH_RECEIVE:
  923. mgsl_bh_receive(info);
  924. break;
  925. case BH_TRANSMIT:
  926. mgsl_bh_transmit(info);
  927. break;
  928. case BH_STATUS:
  929. mgsl_bh_status(info);
  930. break;
  931. default:
  932. /* unknown work item ID */
  933. printk("Unknown work item ID=%08X!\n", action);
  934. break;
  935. }
  936. }
  937. if ( debug_level >= DEBUG_LEVEL_BH )
  938. printk( "%s(%d):mgsl_bh_handler(%s) exit\n",
  939. __FILE__,__LINE__,info->device_name);
  940. }
  941. static void mgsl_bh_receive(struct mgsl_struct *info)
  942. {
  943. int (*get_rx_frame)(struct mgsl_struct *info) =
  944. (info->params.mode == MGSL_MODE_HDLC ? mgsl_get_rx_frame : mgsl_get_raw_rx_frame);
  945. if ( debug_level >= DEBUG_LEVEL_BH )
  946. printk( "%s(%d):mgsl_bh_receive(%s)\n",
  947. __FILE__,__LINE__,info->device_name);
  948. do
  949. {
  950. if (info->rx_rcc_underrun) {
  951. unsigned long flags;
  952. spin_lock_irqsave(&info->irq_spinlock,flags);
  953. usc_start_receiver(info);
  954. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  955. return;
  956. }
  957. } while(get_rx_frame(info));
  958. }
  959. static void mgsl_bh_transmit(struct mgsl_struct *info)
  960. {
  961. struct tty_struct *tty = info->tty;
  962. unsigned long flags;
  963. if ( debug_level >= DEBUG_LEVEL_BH )
  964. printk( "%s(%d):mgsl_bh_transmit() entry on %s\n",
  965. __FILE__,__LINE__,info->device_name);
  966. if (tty) {
  967. tty_wakeup(tty);
  968. wake_up_interruptible(&tty->write_wait);
  969. }
  970. /* if transmitter idle and loopmode_send_done_requested
  971. * then start echoing RxD to TxD
  972. */
  973. spin_lock_irqsave(&info->irq_spinlock,flags);
  974. if ( !info->tx_active && info->loopmode_send_done_requested )
  975. usc_loopmode_send_done( info );
  976. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  977. }
  978. static void mgsl_bh_status(struct mgsl_struct *info)
  979. {
  980. if ( debug_level >= DEBUG_LEVEL_BH )
  981. printk( "%s(%d):mgsl_bh_status() entry on %s\n",
  982. __FILE__,__LINE__,info->device_name);
  983. info->ri_chkcount = 0;
  984. info->dsr_chkcount = 0;
  985. info->dcd_chkcount = 0;
  986. info->cts_chkcount = 0;
  987. }
  988. /* mgsl_isr_receive_status()
  989. *
  990. * Service a receive status interrupt. The type of status
  991. * interrupt is indicated by the state of the RCSR.
  992. * This is only used for HDLC mode.
  993. *
  994. * Arguments: info pointer to device instance data
  995. * Return Value: None
  996. */
  997. static void mgsl_isr_receive_status( struct mgsl_struct *info )
  998. {
  999. u16 status = usc_InReg( info, RCSR );
  1000. if ( debug_level >= DEBUG_LEVEL_ISR )
  1001. printk("%s(%d):mgsl_isr_receive_status status=%04X\n",
  1002. __FILE__,__LINE__,status);
  1003. if ( (status & RXSTATUS_ABORT_RECEIVED) &&
  1004. info->loopmode_insert_requested &&
  1005. usc_loopmode_active(info) )
  1006. {
  1007. ++info->icount.rxabort;
  1008. info->loopmode_insert_requested = FALSE;
  1009. /* clear CMR:13 to start echoing RxD to TxD */
  1010. info->cmr_value &= ~BIT13;
  1011. usc_OutReg(info, CMR, info->cmr_value);
  1012. /* disable received abort irq (no longer required) */
  1013. usc_OutReg(info, RICR,
  1014. (usc_InReg(info, RICR) & ~RXSTATUS_ABORT_RECEIVED));
  1015. }
  1016. if (status & (RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED)) {
  1017. if (status & RXSTATUS_EXITED_HUNT)
  1018. info->icount.exithunt++;
  1019. if (status & RXSTATUS_IDLE_RECEIVED)
  1020. info->icount.rxidle++;
  1021. wake_up_interruptible(&info->event_wait_q);
  1022. }
  1023. if (status & RXSTATUS_OVERRUN){
  1024. info->icount.rxover++;
  1025. usc_process_rxoverrun_sync( info );
  1026. }
  1027. usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
  1028. usc_UnlatchRxstatusBits( info, status );
  1029. } /* end of mgsl_isr_receive_status() */
  1030. /* mgsl_isr_transmit_status()
  1031. *
  1032. * Service a transmit status interrupt
  1033. * HDLC mode :end of transmit frame
  1034. * Async mode:all data is sent
  1035. * transmit status is indicated by bits in the TCSR.
  1036. *
  1037. * Arguments: info pointer to device instance data
  1038. * Return Value: None
  1039. */
  1040. static void mgsl_isr_transmit_status( struct mgsl_struct *info )
  1041. {
  1042. u16 status = usc_InReg( info, TCSR );
  1043. if ( debug_level >= DEBUG_LEVEL_ISR )
  1044. printk("%s(%d):mgsl_isr_transmit_status status=%04X\n",
  1045. __FILE__,__LINE__,status);
  1046. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
  1047. usc_UnlatchTxstatusBits( info, status );
  1048. if ( status & (TXSTATUS_UNDERRUN | TXSTATUS_ABORT_SENT) )
  1049. {
  1050. /* finished sending HDLC abort. This may leave */
  1051. /* the TxFifo with data from the aborted frame */
  1052. /* so purge the TxFifo. Also shutdown the DMA */
  1053. /* channel in case there is data remaining in */
  1054. /* the DMA buffer */
  1055. usc_DmaCmd( info, DmaCmd_ResetTxChannel );
  1056. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  1057. }
  1058. if ( status & TXSTATUS_EOF_SENT )
  1059. info->icount.txok++;
  1060. else if ( status & TXSTATUS_UNDERRUN )
  1061. info->icount.txunder++;
  1062. else if ( status & TXSTATUS_ABORT_SENT )
  1063. info->icount.txabort++;
  1064. else
  1065. info->icount.txunder++;
  1066. info->tx_active = 0;
  1067. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1068. del_timer(&info->tx_timer);
  1069. if ( info->drop_rts_on_tx_done ) {
  1070. usc_get_serial_signals( info );
  1071. if ( info->serial_signals & SerialSignal_RTS ) {
  1072. info->serial_signals &= ~SerialSignal_RTS;
  1073. usc_set_serial_signals( info );
  1074. }
  1075. info->drop_rts_on_tx_done = 0;
  1076. }
  1077. #ifdef CONFIG_HDLC
  1078. if (info->netcount)
  1079. hdlcdev_tx_done(info);
  1080. else
  1081. #endif
  1082. {
  1083. if (info->tty->stopped || info->tty->hw_stopped) {
  1084. usc_stop_transmitter(info);
  1085. return;
  1086. }
  1087. info->pending_bh |= BH_TRANSMIT;
  1088. }
  1089. } /* end of mgsl_isr_transmit_status() */
  1090. /* mgsl_isr_io_pin()
  1091. *
  1092. * Service an Input/Output pin interrupt. The type of
  1093. * interrupt is indicated by bits in the MISR
  1094. *
  1095. * Arguments: info pointer to device instance data
  1096. * Return Value: None
  1097. */
  1098. static void mgsl_isr_io_pin( struct mgsl_struct *info )
  1099. {
  1100. struct mgsl_icount *icount;
  1101. u16 status = usc_InReg( info, MISR );
  1102. if ( debug_level >= DEBUG_LEVEL_ISR )
  1103. printk("%s(%d):mgsl_isr_io_pin status=%04X\n",
  1104. __FILE__,__LINE__,status);
  1105. usc_ClearIrqPendingBits( info, IO_PIN );
  1106. usc_UnlatchIostatusBits( info, status );
  1107. if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
  1108. MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
  1109. icount = &info->icount;
  1110. /* update input line counters */
  1111. if (status & MISCSTATUS_RI_LATCHED) {
  1112. if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1113. usc_DisablestatusIrqs(info,SICR_RI);
  1114. icount->rng++;
  1115. if ( status & MISCSTATUS_RI )
  1116. info->input_signal_events.ri_up++;
  1117. else
  1118. info->input_signal_events.ri_down++;
  1119. }
  1120. if (status & MISCSTATUS_DSR_LATCHED) {
  1121. if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1122. usc_DisablestatusIrqs(info,SICR_DSR);
  1123. icount->dsr++;
  1124. if ( status & MISCSTATUS_DSR )
  1125. info->input_signal_events.dsr_up++;
  1126. else
  1127. info->input_signal_events.dsr_down++;
  1128. }
  1129. if (status & MISCSTATUS_DCD_LATCHED) {
  1130. if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1131. usc_DisablestatusIrqs(info,SICR_DCD);
  1132. icount->dcd++;
  1133. if (status & MISCSTATUS_DCD) {
  1134. info->input_signal_events.dcd_up++;
  1135. } else
  1136. info->input_signal_events.dcd_down++;
  1137. #ifdef CONFIG_HDLC
  1138. if (info->netcount)
  1139. hdlc_set_carrier(status & MISCSTATUS_DCD, info->netdev);
  1140. #endif
  1141. }
  1142. if (status & MISCSTATUS_CTS_LATCHED)
  1143. {
  1144. if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1145. usc_DisablestatusIrqs(info,SICR_CTS);
  1146. icount->cts++;
  1147. if ( status & MISCSTATUS_CTS )
  1148. info->input_signal_events.cts_up++;
  1149. else
  1150. info->input_signal_events.cts_down++;
  1151. }
  1152. wake_up_interruptible(&info->status_event_wait_q);
  1153. wake_up_interruptible(&info->event_wait_q);
  1154. if ( (info->flags & ASYNC_CHECK_CD) &&
  1155. (status & MISCSTATUS_DCD_LATCHED) ) {
  1156. if ( debug_level >= DEBUG_LEVEL_ISR )
  1157. printk("%s CD now %s...", info->device_name,
  1158. (status & MISCSTATUS_DCD) ? "on" : "off");
  1159. if (status & MISCSTATUS_DCD)
  1160. wake_up_interruptible(&info->open_wait);
  1161. else {
  1162. if ( debug_level >= DEBUG_LEVEL_ISR )
  1163. printk("doing serial hangup...");
  1164. if (info->tty)
  1165. tty_hangup(info->tty);
  1166. }
  1167. }
  1168. if ( (info->flags & ASYNC_CTS_FLOW) &&
  1169. (status & MISCSTATUS_CTS_LATCHED) ) {
  1170. if (info->tty->hw_stopped) {
  1171. if (status & MISCSTATUS_CTS) {
  1172. if ( debug_level >= DEBUG_LEVEL_ISR )
  1173. printk("CTS tx start...");
  1174. if (info->tty)
  1175. info->tty->hw_stopped = 0;
  1176. usc_start_transmitter(info);
  1177. info->pending_bh |= BH_TRANSMIT;
  1178. return;
  1179. }
  1180. } else {
  1181. if (!(status & MISCSTATUS_CTS)) {
  1182. if ( debug_level >= DEBUG_LEVEL_ISR )
  1183. printk("CTS tx stop...");
  1184. if (info->tty)
  1185. info->tty->hw_stopped = 1;
  1186. usc_stop_transmitter(info);
  1187. }
  1188. }
  1189. }
  1190. }
  1191. info->pending_bh |= BH_STATUS;
  1192. /* for diagnostics set IRQ flag */
  1193. if ( status & MISCSTATUS_TXC_LATCHED ){
  1194. usc_OutReg( info, SICR,
  1195. (unsigned short)(usc_InReg(info,SICR) & ~(SICR_TXC_ACTIVE+SICR_TXC_INACTIVE)) );
  1196. usc_UnlatchIostatusBits( info, MISCSTATUS_TXC_LATCHED );
  1197. info->irq_occurred = 1;
  1198. }
  1199. } /* end of mgsl_isr_io_pin() */
  1200. /* mgsl_isr_transmit_data()
  1201. *
  1202. * Service a transmit data interrupt (async mode only).
  1203. *
  1204. * Arguments: info pointer to device instance data
  1205. * Return Value: None
  1206. */
  1207. static void mgsl_isr_transmit_data( struct mgsl_struct *info )
  1208. {
  1209. if ( debug_level >= DEBUG_LEVEL_ISR )
  1210. printk("%s(%d):mgsl_isr_transmit_data xmit_cnt=%d\n",
  1211. __FILE__,__LINE__,info->xmit_cnt);
  1212. usc_ClearIrqPendingBits( info, TRANSMIT_DATA );
  1213. if (info->tty->stopped || info->tty->hw_stopped) {
  1214. usc_stop_transmitter(info);
  1215. return;
  1216. }
  1217. if ( info->xmit_cnt )
  1218. usc_load_txfifo( info );
  1219. else
  1220. info->tx_active = 0;
  1221. if (info->xmit_cnt < WAKEUP_CHARS)
  1222. info->pending_bh |= BH_TRANSMIT;
  1223. } /* end of mgsl_isr_transmit_data() */
  1224. /* mgsl_isr_receive_data()
  1225. *
  1226. * Service a receive data interrupt. This occurs
  1227. * when operating in asynchronous interrupt transfer mode.
  1228. * The receive data FIFO is flushed to the receive data buffers.
  1229. *
  1230. * Arguments: info pointer to device instance data
  1231. * Return Value: None
  1232. */
  1233. static void mgsl_isr_receive_data( struct mgsl_struct *info )
  1234. {
  1235. int Fifocount;
  1236. u16 status;
  1237. unsigned char DataByte;
  1238. struct tty_struct *tty = info->tty;
  1239. struct mgsl_icount *icount = &info->icount;
  1240. if ( debug_level >= DEBUG_LEVEL_ISR )
  1241. printk("%s(%d):mgsl_isr_receive_data\n",
  1242. __FILE__,__LINE__);
  1243. usc_ClearIrqPendingBits( info, RECEIVE_DATA );
  1244. /* select FIFO status for RICR readback */
  1245. usc_RCmd( info, RCmd_SelectRicrRxFifostatus );
  1246. /* clear the Wordstatus bit so that status readback */
  1247. /* only reflects the status of this byte */
  1248. usc_OutReg( info, RICR+LSBONLY, (u16)(usc_InReg(info, RICR+LSBONLY) & ~BIT3 ));
  1249. /* flush the receive FIFO */
  1250. while( (Fifocount = (usc_InReg(info,RICR) >> 8)) ) {
  1251. /* read one byte from RxFIFO */
  1252. outw( (inw(info->io_base + CCAR) & 0x0780) | (RDR+LSBONLY),
  1253. info->io_base + CCAR );
  1254. DataByte = inb( info->io_base + CCAR );
  1255. /* get the status of the received byte */
  1256. status = usc_InReg(info, RCSR);
  1257. if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
  1258. RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) )
  1259. usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
  1260. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  1261. continue;
  1262. *tty->flip.char_buf_ptr = DataByte;
  1263. icount->rx++;
  1264. *tty->flip.flag_buf_ptr = 0;
  1265. if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
  1266. RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) ) {
  1267. printk("rxerr=%04X\n",status);
  1268. /* update error statistics */
  1269. if ( status & RXSTATUS_BREAK_RECEIVED ) {
  1270. status &= ~(RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR);
  1271. icount->brk++;
  1272. } else if (status & RXSTATUS_PARITY_ERROR)
  1273. icount->parity++;
  1274. else if (status & RXSTATUS_FRAMING_ERROR)
  1275. icount->frame++;
  1276. else if (status & RXSTATUS_OVERRUN) {
  1277. /* must issue purge fifo cmd before */
  1278. /* 16C32 accepts more receive chars */
  1279. usc_RTCmd(info,RTCmd_PurgeRxFifo);
  1280. icount->overrun++;
  1281. }
  1282. /* discard char if tty control flags say so */
  1283. if (status & info->ignore_status_mask)
  1284. continue;
  1285. status &= info->read_status_mask;
  1286. if (status & RXSTATUS_BREAK_RECEIVED) {
  1287. *tty->flip.flag_buf_ptr = TTY_BREAK;
  1288. if (info->flags & ASYNC_SAK)
  1289. do_SAK(tty);
  1290. } else if (status & RXSTATUS_PARITY_ERROR)
  1291. *tty->flip.flag_buf_ptr = TTY_PARITY;
  1292. else if (status & RXSTATUS_FRAMING_ERROR)
  1293. *tty->flip.flag_buf_ptr = TTY_FRAME;
  1294. if (status & RXSTATUS_OVERRUN) {
  1295. /* Overrun is special, since it's
  1296. * reported immediately, and doesn't
  1297. * affect the current character
  1298. */
  1299. if (tty->flip.count < TTY_FLIPBUF_SIZE) {
  1300. tty->flip.count++;
  1301. tty->flip.flag_buf_ptr++;
  1302. tty->flip.char_buf_ptr++;
  1303. *tty->flip.flag_buf_ptr = TTY_OVERRUN;
  1304. }
  1305. }
  1306. } /* end of if (error) */
  1307. tty->flip.flag_buf_ptr++;
  1308. tty->flip.char_buf_ptr++;
  1309. tty->flip.count++;
  1310. }
  1311. if ( debug_level >= DEBUG_LEVEL_ISR ) {
  1312. printk("%s(%d):mgsl_isr_receive_data flip count=%d\n",
  1313. __FILE__,__LINE__,tty->flip.count);
  1314. printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
  1315. __FILE__,__LINE__,icount->rx,icount->brk,
  1316. icount->parity,icount->frame,icount->overrun);
  1317. }
  1318. if ( tty->flip.count )
  1319. tty_flip_buffer_push(tty);
  1320. }
  1321. /* mgsl_isr_misc()
  1322. *
  1323. * Service a miscellaneos interrupt source.
  1324. *
  1325. * Arguments: info pointer to device extension (instance data)
  1326. * Return Value: None
  1327. */
  1328. static void mgsl_isr_misc( struct mgsl_struct *info )
  1329. {
  1330. u16 status = usc_InReg( info, MISR );
  1331. if ( debug_level >= DEBUG_LEVEL_ISR )
  1332. printk("%s(%d):mgsl_isr_misc status=%04X\n",
  1333. __FILE__,__LINE__,status);
  1334. if ((status & MISCSTATUS_RCC_UNDERRUN) &&
  1335. (info->params.mode == MGSL_MODE_HDLC)) {
  1336. /* turn off receiver and rx DMA */
  1337. usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
  1338. usc_DmaCmd(info, DmaCmd_ResetRxChannel);
  1339. usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
  1340. usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS);
  1341. usc_DisableInterrupts(info, RECEIVE_DATA + RECEIVE_STATUS);
  1342. /* schedule BH handler to restart receiver */
  1343. info->pending_bh |= BH_RECEIVE;
  1344. info->rx_rcc_underrun = 1;
  1345. }
  1346. usc_ClearIrqPendingBits( info, MISC );
  1347. usc_UnlatchMiscstatusBits( info, status );
  1348. } /* end of mgsl_isr_misc() */
  1349. /* mgsl_isr_null()
  1350. *
  1351. * Services undefined interrupt vectors from the
  1352. * USC. (hence this function SHOULD never be called)
  1353. *
  1354. * Arguments: info pointer to device extension (instance data)
  1355. * Return Value: None
  1356. */
  1357. static void mgsl_isr_null( struct mgsl_struct *info )
  1358. {
  1359. } /* end of mgsl_isr_null() */
  1360. /* mgsl_isr_receive_dma()
  1361. *
  1362. * Service a receive DMA channel interrupt.
  1363. * For this driver there are two sources of receive DMA interrupts
  1364. * as identified in the Receive DMA mode Register (RDMR):
  1365. *
  1366. * BIT3 EOA/EOL End of List, all receive buffers in receive
  1367. * buffer list have been filled (no more free buffers
  1368. * available). The DMA controller has shut down.
  1369. *
  1370. * BIT2 EOB End of Buffer. This interrupt occurs when a receive
  1371. * DMA buffer is terminated in response to completion
  1372. * of a good frame or a frame with errors. The status
  1373. * of the frame is stored in the buffer entry in the
  1374. * list of receive buffer entries.
  1375. *
  1376. * Arguments: info pointer to device instance data
  1377. * Return Value: None
  1378. */
  1379. static void mgsl_isr_receive_dma( struct mgsl_struct *info )
  1380. {
  1381. u16 status;
  1382. /* clear interrupt pending and IUS bit for Rx DMA IRQ */
  1383. usc_OutDmaReg( info, CDIR, BIT9+BIT1 );
  1384. /* Read the receive DMA status to identify interrupt type. */
  1385. /* This also clears the status bits. */
  1386. status = usc_InDmaReg( info, RDMR );
  1387. if ( debug_level >= DEBUG_LEVEL_ISR )
  1388. printk("%s(%d):mgsl_isr_receive_dma(%s) status=%04X\n",
  1389. __FILE__,__LINE__,info->device_name,status);
  1390. info->pending_bh |= BH_RECEIVE;
  1391. if ( status & BIT3 ) {
  1392. info->rx_overflow = 1;
  1393. info->icount.buf_overrun++;
  1394. }
  1395. } /* end of mgsl_isr_receive_dma() */
  1396. /* mgsl_isr_transmit_dma()
  1397. *
  1398. * This function services a transmit DMA channel interrupt.
  1399. *
  1400. * For this driver there is one source of transmit DMA interrupts
  1401. * as identified in the Transmit DMA Mode Register (TDMR):
  1402. *
  1403. * BIT2 EOB End of Buffer. This interrupt occurs when a
  1404. * transmit DMA buffer has been emptied.
  1405. *
  1406. * The driver maintains enough transmit DMA buffers to hold at least
  1407. * one max frame size transmit frame. When operating in a buffered
  1408. * transmit mode, there may be enough transmit DMA buffers to hold at
  1409. * least two or more max frame size frames. On an EOB condition,
  1410. * determine if there are any queued transmit buffers and copy into
  1411. * transmit DMA buffers if we have room.
  1412. *
  1413. * Arguments: info pointer to device instance data
  1414. * Return Value: None
  1415. */
  1416. static void mgsl_isr_transmit_dma( struct mgsl_struct *info )
  1417. {
  1418. u16 status;
  1419. /* clear interrupt pending and IUS bit for Tx DMA IRQ */
  1420. usc_OutDmaReg(info, CDIR, BIT8+BIT0 );
  1421. /* Read the transmit DMA status to identify interrupt type. */
  1422. /* This also clears the status bits. */
  1423. status = usc_InDmaReg( info, TDMR );
  1424. if ( debug_level >= DEBUG_LEVEL_ISR )
  1425. printk("%s(%d):mgsl_isr_transmit_dma(%s) status=%04X\n",
  1426. __FILE__,__LINE__,info->device_name,status);
  1427. if ( status & BIT2 ) {
  1428. --info->tx_dma_buffers_used;
  1429. /* if there are transmit frames queued,
  1430. * try to load the next one
  1431. */
  1432. if ( load_next_tx_holding_buffer(info) ) {
  1433. /* if call returns non-zero value, we have
  1434. * at least one free tx holding buffer
  1435. */
  1436. info->pending_bh |= BH_TRANSMIT;
  1437. }
  1438. }
  1439. } /* end of mgsl_isr_transmit_dma() */
  1440. /* mgsl_interrupt()
  1441. *
  1442. * Interrupt service routine entry point.
  1443. *
  1444. * Arguments:
  1445. *
  1446. * irq interrupt number that caused interrupt
  1447. * dev_id device ID supplied during interrupt registration
  1448. * regs interrupted processor context
  1449. *
  1450. * Return Value: None
  1451. */
  1452. static irqreturn_t mgsl_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  1453. {
  1454. struct mgsl_struct * info;
  1455. u16 UscVector;
  1456. u16 DmaVector;
  1457. if ( debug_level >= DEBUG_LEVEL_ISR )
  1458. printk("%s(%d):mgsl_interrupt(%d)entry.\n",
  1459. __FILE__,__LINE__,irq);
  1460. info = (struct mgsl_struct *)dev_id;
  1461. if (!info)
  1462. return IRQ_NONE;
  1463. spin_lock(&info->irq_spinlock);
  1464. for(;;) {
  1465. /* Read the interrupt vectors from hardware. */
  1466. UscVector = usc_InReg(info, IVR) >> 9;
  1467. DmaVector = usc_InDmaReg(info, DIVR);
  1468. if ( debug_level >= DEBUG_LEVEL_ISR )
  1469. printk("%s(%d):%s UscVector=%08X DmaVector=%08X\n",
  1470. __FILE__,__LINE__,info->device_name,UscVector,DmaVector);
  1471. if ( !UscVector && !DmaVector )
  1472. break;
  1473. /* Dispatch interrupt vector */
  1474. if ( UscVector )
  1475. (*UscIsrTable[UscVector])(info);
  1476. else if ( (DmaVector&(BIT10|BIT9)) == BIT10)
  1477. mgsl_isr_transmit_dma(info);
  1478. else
  1479. mgsl_isr_receive_dma(info);
  1480. if ( info->isr_overflow ) {
  1481. printk(KERN_ERR"%s(%d):%s isr overflow irq=%d\n",
  1482. __FILE__,__LINE__,info->device_name, irq);
  1483. usc_DisableMasterIrqBit(info);
  1484. usc_DisableDmaInterrupts(info,DICR_MASTER);
  1485. break;
  1486. }
  1487. }
  1488. /* Request bottom half processing if there's something
  1489. * for it to do and the bh is not already running
  1490. */
  1491. if ( info->pending_bh && !info->bh_running && !info->bh_requested ) {
  1492. if ( debug_level >= DEBUG_LEVEL_ISR )
  1493. printk("%s(%d):%s queueing bh task.\n",
  1494. __FILE__,__LINE__,info->device_name);
  1495. schedule_work(&info->task);
  1496. info->bh_requested = 1;
  1497. }
  1498. spin_unlock(&info->irq_spinlock);
  1499. if ( debug_level >= DEBUG_LEVEL_ISR )
  1500. printk("%s(%d):mgsl_interrupt(%d)exit.\n",
  1501. __FILE__,__LINE__,irq);
  1502. return IRQ_HANDLED;
  1503. } /* end of mgsl_interrupt() */
  1504. /* startup()
  1505. *
  1506. * Initialize and start device.
  1507. *
  1508. * Arguments: info pointer to device instance data
  1509. * Return Value: 0 if success, otherwise error code
  1510. */
  1511. static int startup(struct mgsl_struct * info)
  1512. {
  1513. int retval = 0;
  1514. if ( debug_level >= DEBUG_LEVEL_INFO )
  1515. printk("%s(%d):mgsl_startup(%s)\n",__FILE__,__LINE__,info->device_name);
  1516. if (info->flags & ASYNC_INITIALIZED)
  1517. return 0;
  1518. if (!info->xmit_buf) {
  1519. /* allocate a page of memory for a transmit buffer */
  1520. info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
  1521. if (!info->xmit_buf) {
  1522. printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
  1523. __FILE__,__LINE__,info->device_name);
  1524. return -ENOMEM;
  1525. }
  1526. }
  1527. info->pending_bh = 0;
  1528. init_timer(&info->tx_timer);
  1529. info->tx_timer.data = (unsigned long)info;
  1530. info->tx_timer.function = mgsl_tx_timeout;
  1531. /* Allocate and claim adapter resources */
  1532. retval = mgsl_claim_resources(info);
  1533. /* perform existence check and diagnostics */
  1534. if ( !retval )
  1535. retval = mgsl_adapter_test(info);
  1536. if ( retval ) {
  1537. if (capable(CAP_SYS_ADMIN) && info->tty)
  1538. set_bit(TTY_IO_ERROR, &info->tty->flags);
  1539. mgsl_release_resources(info);
  1540. return retval;
  1541. }
  1542. /* program hardware for current parameters */
  1543. mgsl_change_params(info);
  1544. if (info->tty)
  1545. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  1546. info->flags |= ASYNC_INITIALIZED;
  1547. return 0;
  1548. } /* end of startup() */
  1549. /* shutdown()
  1550. *
  1551. * Called by mgsl_close() and mgsl_hangup() to shutdown hardware
  1552. *
  1553. * Arguments: info pointer to device instance data
  1554. * Return Value: None
  1555. */
  1556. static void shutdown(struct mgsl_struct * info)
  1557. {
  1558. unsigned long flags;
  1559. if (!(info->flags & ASYNC_INITIALIZED))
  1560. return;
  1561. if (debug_level >= DEBUG_LEVEL_INFO)
  1562. printk("%s(%d):mgsl_shutdown(%s)\n",
  1563. __FILE__,__LINE__, info->device_name );
  1564. /* clear status wait queue because status changes */
  1565. /* can't happen after shutting down the hardware */
  1566. wake_up_interruptible(&info->status_event_wait_q);
  1567. wake_up_interruptible(&info->event_wait_q);
  1568. del_timer(&info->tx_timer);
  1569. if (info->xmit_buf) {
  1570. free_page((unsigned long) info->xmit_buf);
  1571. info->xmit_buf = NULL;
  1572. }
  1573. spin_lock_irqsave(&info->irq_spinlock,flags);
  1574. usc_DisableMasterIrqBit(info);
  1575. usc_stop_receiver(info);
  1576. usc_stop_transmitter(info);
  1577. usc_DisableInterrupts(info,RECEIVE_DATA + RECEIVE_STATUS +
  1578. TRANSMIT_DATA + TRANSMIT_STATUS + IO_PIN + MISC );
  1579. usc_DisableDmaInterrupts(info,DICR_MASTER + DICR_TRANSMIT + DICR_RECEIVE);
  1580. /* Disable DMAEN (Port 7, Bit 14) */
  1581. /* This disconnects the DMA request signal from the ISA bus */
  1582. /* on the ISA adapter. This has no effect for the PCI adapter */
  1583. usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) | BIT14));
  1584. /* Disable INTEN (Port 6, Bit12) */
  1585. /* This disconnects the IRQ request signal to the ISA bus */
  1586. /* on the ISA adapter. This has no effect for the PCI adapter */
  1587. usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) | BIT12));
  1588. if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
  1589. info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  1590. usc_set_serial_signals(info);
  1591. }
  1592. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1593. mgsl_release_resources(info);
  1594. if (info->tty)
  1595. set_bit(TTY_IO_ERROR, &info->tty->flags);
  1596. info->flags &= ~ASYNC_INITIALIZED;
  1597. } /* end of shutdown() */
  1598. static void mgsl_program_hw(struct mgsl_struct *info)
  1599. {
  1600. unsigned long flags;
  1601. spin_lock_irqsave(&info->irq_spinlock,flags);
  1602. usc_stop_receiver(info);
  1603. usc_stop_transmitter(info);
  1604. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1605. if (info->params.mode == MGSL_MODE_HDLC ||
  1606. info->params.mode == MGSL_MODE_RAW ||
  1607. info->netcount)
  1608. usc_set_sync_mode(info);
  1609. else
  1610. usc_set_async_mode(info);
  1611. usc_set_serial_signals(info);
  1612. info->dcd_chkcount = 0;
  1613. info->cts_chkcount = 0;
  1614. info->ri_chkcount = 0;
  1615. info->dsr_chkcount = 0;
  1616. usc_EnableStatusIrqs(info,SICR_CTS+SICR_DSR+SICR_DCD+SICR_RI);
  1617. usc_EnableInterrupts(info, IO_PIN);
  1618. usc_get_serial_signals(info);
  1619. if (info->netcount || info->tty->termios->c_cflag & CREAD)
  1620. usc_start_receiver(info);
  1621. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1622. }
  1623. /* Reconfigure adapter based on new parameters
  1624. */
  1625. static void mgsl_change_params(struct mgsl_struct *info)
  1626. {
  1627. unsigned cflag;
  1628. int bits_per_char;
  1629. if (!info->tty || !info->tty->termios)
  1630. return;
  1631. if (debug_level >= DEBUG_LEVEL_INFO)
  1632. printk("%s(%d):mgsl_change_params(%s)\n",
  1633. __FILE__,__LINE__, info->device_name );
  1634. cflag = info->tty->termios->c_cflag;
  1635. /* if B0 rate (hangup) specified then negate DTR and RTS */
  1636. /* otherwise assert DTR and RTS */
  1637. if (cflag & CBAUD)
  1638. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  1639. else
  1640. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  1641. /* byte size and parity */
  1642. switch (cflag & CSIZE) {
  1643. case CS5: info->params.data_bits = 5; break;
  1644. case CS6: info->params.data_bits = 6; break;
  1645. case CS7: info->params.data_bits = 7; break;
  1646. case CS8: info->params.data_bits = 8; break;
  1647. /* Never happens, but GCC is too dumb to figure it out */
  1648. default: info->params.data_bits = 7; break;
  1649. }
  1650. if (cflag & CSTOPB)
  1651. info->params.stop_bits = 2;
  1652. else
  1653. info->params.stop_bits = 1;
  1654. info->params.parity = ASYNC_PARITY_NONE;
  1655. if (cflag & PARENB) {
  1656. if (cflag & PARODD)
  1657. info->params.parity = ASYNC_PARITY_ODD;
  1658. else
  1659. info->params.parity = ASYNC_PARITY_EVEN;
  1660. #ifdef CMSPAR
  1661. if (cflag & CMSPAR)
  1662. info->params.parity = ASYNC_PARITY_SPACE;
  1663. #endif
  1664. }
  1665. /* calculate number of jiffies to transmit a full
  1666. * FIFO (32 bytes) at specified data rate
  1667. */
  1668. bits_per_char = info->params.data_bits +
  1669. info->params.stop_bits + 1;
  1670. /* if port data rate is set to 460800 or less then
  1671. * allow tty settings to override, otherwise keep the
  1672. * current data rate.
  1673. */
  1674. if (info->params.data_rate <= 460800)
  1675. info->params.data_rate = tty_get_baud_rate(info->tty);
  1676. if ( info->params.data_rate ) {
  1677. info->timeout = (32*HZ*bits_per_char) /
  1678. info->params.data_rate;
  1679. }
  1680. info->timeout += HZ/50; /* Add .02 seconds of slop */
  1681. if (cflag & CRTSCTS)
  1682. info->flags |= ASYNC_CTS_FLOW;
  1683. else
  1684. info->flags &= ~ASYNC_CTS_FLOW;
  1685. if (cflag & CLOCAL)
  1686. info->flags &= ~ASYNC_CHECK_CD;
  1687. else
  1688. info->flags |= ASYNC_CHECK_CD;
  1689. /* process tty input control flags */
  1690. info->read_status_mask = RXSTATUS_OVERRUN;
  1691. if (I_INPCK(info->tty))
  1692. info->read_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
  1693. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  1694. info->read_status_mask |= RXSTATUS_BREAK_RECEIVED;
  1695. if (I_IGNPAR(info->tty))
  1696. info->ignore_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
  1697. if (I_IGNBRK(info->tty)) {
  1698. info->ignore_status_mask |= RXSTATUS_BREAK_RECEIVED;
  1699. /* If ignoring parity and break indicators, ignore
  1700. * overruns too. (For real raw support).
  1701. */
  1702. if (I_IGNPAR(info->tty))
  1703. info->ignore_status_mask |= RXSTATUS_OVERRUN;
  1704. }
  1705. mgsl_program_hw(info);
  1706. } /* end of mgsl_change_params() */
  1707. /* mgsl_put_char()
  1708. *
  1709. * Add a character to the transmit buffer.
  1710. *
  1711. * Arguments: tty pointer to tty information structure
  1712. * ch character to add to transmit buffer
  1713. *
  1714. * Return Value: None
  1715. */
  1716. static void mgsl_put_char(struct tty_struct *tty, unsigned char ch)
  1717. {
  1718. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1719. unsigned long flags;
  1720. if ( debug_level >= DEBUG_LEVEL_INFO ) {
  1721. printk( "%s(%d):mgsl_put_char(%d) on %s\n",
  1722. __FILE__,__LINE__,ch,info->device_name);
  1723. }
  1724. if (mgsl_paranoia_check(info, tty->name, "mgsl_put_char"))
  1725. return;
  1726. if (!tty || !info->xmit_buf)
  1727. return;
  1728. spin_lock_irqsave(&info->irq_spinlock,flags);
  1729. if ( (info->params.mode == MGSL_MODE_ASYNC ) || !info->tx_active ) {
  1730. if (info->xmit_cnt < SERIAL_XMIT_SIZE - 1) {
  1731. info->xmit_buf[info->xmit_head++] = ch;
  1732. info->xmit_head &= SERIAL_XMIT_SIZE-1;
  1733. info->xmit_cnt++;
  1734. }
  1735. }
  1736. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1737. } /* end of mgsl_put_char() */
  1738. /* mgsl_flush_chars()
  1739. *
  1740. * Enable transmitter so remaining characters in the
  1741. * transmit buffer are sent.
  1742. *
  1743. * Arguments: tty pointer to tty information structure
  1744. * Return Value: None
  1745. */
  1746. static void mgsl_flush_chars(struct tty_struct *tty)
  1747. {
  1748. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1749. unsigned long flags;
  1750. if ( debug_level >= DEBUG_LEVEL_INFO )
  1751. printk( "%s(%d):mgsl_flush_chars() entry on %s xmit_cnt=%d\n",
  1752. __FILE__,__LINE__,info->device_name,info->xmit_cnt);
  1753. if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_chars"))
  1754. return;
  1755. if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  1756. !info->xmit_buf)
  1757. return;
  1758. if ( debug_level >= DEBUG_LEVEL_INFO )
  1759. printk( "%s(%d):mgsl_flush_chars() entry on %s starting transmitter\n",
  1760. __FILE__,__LINE__,info->device_name );
  1761. spin_lock_irqsave(&info->irq_spinlock,flags);
  1762. if (!info->tx_active) {
  1763. if ( (info->params.mode == MGSL_MODE_HDLC ||
  1764. info->params.mode == MGSL_MODE_RAW) && info->xmit_cnt ) {
  1765. /* operating in synchronous (frame oriented) mode */
  1766. /* copy data from circular xmit_buf to */
  1767. /* transmit DMA buffer. */
  1768. mgsl_load_tx_dma_buffer(info,
  1769. info->xmit_buf,info->xmit_cnt);
  1770. }
  1771. usc_start_transmitter(info);
  1772. }
  1773. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1774. } /* end of mgsl_flush_chars() */
  1775. /* mgsl_write()
  1776. *
  1777. * Send a block of data
  1778. *
  1779. * Arguments:
  1780. *
  1781. * tty pointer to tty information structure
  1782. * buf pointer to buffer containing send data
  1783. * count size of send data in bytes
  1784. *
  1785. * Return Value: number of characters written
  1786. */
  1787. static int mgsl_write(struct tty_struct * tty,
  1788. const unsigned char *buf, int count)
  1789. {
  1790. int c, ret = 0;
  1791. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1792. unsigned long flags;
  1793. if ( debug_level >= DEBUG_LEVEL_INFO )
  1794. printk( "%s(%d):mgsl_write(%s) count=%d\n",
  1795. __FILE__,__LINE__,info->device_name,count);
  1796. if (mgsl_paranoia_check(info, tty->name, "mgsl_write"))
  1797. goto cleanup;
  1798. if (!tty || !info->xmit_buf || !tmp_buf)
  1799. goto cleanup;
  1800. if ( info->params.mode == MGSL_MODE_HDLC ||
  1801. info->params.mode == MGSL_MODE_RAW ) {
  1802. /* operating in synchronous (frame oriented) mode */
  1803. /* operating in synchronous (frame oriented) mode */
  1804. if (info->tx_active) {
  1805. if ( info->params.mode == MGSL_MODE_HDLC ) {
  1806. ret = 0;
  1807. goto cleanup;
  1808. }
  1809. /* transmitter is actively sending data -
  1810. * if we have multiple transmit dma and
  1811. * holding buffers, attempt to queue this
  1812. * frame for transmission at a later time.
  1813. */
  1814. if (info->tx_holding_count >= info->num_tx_holding_buffers ) {
  1815. /* no tx holding buffers available */
  1816. ret = 0;
  1817. goto cleanup;
  1818. }
  1819. /* queue transmit frame request */
  1820. ret = count;
  1821. save_tx_buffer_request(info,buf,count);
  1822. /* if we have sufficient tx dma buffers,
  1823. * load the next buffered tx request
  1824. */
  1825. spin_lock_irqsave(&info->irq_spinlock,flags);
  1826. load_next_tx_holding_buffer(info);
  1827. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1828. goto cleanup;
  1829. }
  1830. /* if operating in HDLC LoopMode and the adapter */
  1831. /* has yet to be inserted into the loop, we can't */
  1832. /* transmit */
  1833. if ( (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) &&
  1834. !usc_loopmode_active(info) )
  1835. {
  1836. ret = 0;
  1837. goto cleanup;
  1838. }
  1839. if ( info->xmit_cnt ) {
  1840. /* Send accumulated from send_char() calls */
  1841. /* as frame and wait before accepting more data. */
  1842. ret = 0;
  1843. /* copy data from circular xmit_buf to */
  1844. /* transmit DMA buffer. */
  1845. mgsl_load_tx_dma_buffer(info,
  1846. info->xmit_buf,info->xmit_cnt);
  1847. if ( debug_level >= DEBUG_LEVEL_INFO )
  1848. printk( "%s(%d):mgsl_write(%s) sync xmit_cnt flushing\n",
  1849. __FILE__,__LINE__,info->device_name);
  1850. } else {
  1851. if ( debug_level >= DEBUG_LEVEL_INFO )
  1852. printk( "%s(%d):mgsl_write(%s) sync transmit accepted\n",
  1853. __FILE__,__LINE__,info->device_name);
  1854. ret = count;
  1855. info->xmit_cnt = count;
  1856. mgsl_load_tx_dma_buffer(info,buf,count);
  1857. }
  1858. } else {
  1859. while (1) {
  1860. spin_lock_irqsave(&info->irq_spinlock,flags);
  1861. c = min_t(int, count,
  1862. min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  1863. SERIAL_XMIT_SIZE - info->xmit_head));
  1864. if (c <= 0) {
  1865. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1866. break;
  1867. }
  1868. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  1869. info->xmit_head = ((info->xmit_head + c) &
  1870. (SERIAL_XMIT_SIZE-1));
  1871. info->xmit_cnt += c;
  1872. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1873. buf += c;
  1874. count -= c;
  1875. ret += c;
  1876. }
  1877. }
  1878. if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
  1879. spin_lock_irqsave(&info->irq_spinlock,flags);
  1880. if (!info->tx_active)
  1881. usc_start_transmitter(info);
  1882. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1883. }
  1884. cleanup:
  1885. if ( debug_level >= DEBUG_LEVEL_INFO )
  1886. printk( "%s(%d):mgsl_write(%s) returning=%d\n",
  1887. __FILE__,__LINE__,info->device_name,ret);
  1888. return ret;
  1889. } /* end of mgsl_write() */
  1890. /* mgsl_write_room()
  1891. *
  1892. * Return the count of free bytes in transmit buffer
  1893. *
  1894. * Arguments: tty pointer to tty info structure
  1895. * Return Value: None
  1896. */
  1897. static int mgsl_write_room(struct tty_struct *tty)
  1898. {
  1899. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1900. int ret;
  1901. if (mgsl_paranoia_check(info, tty->name, "mgsl_write_room"))
  1902. return 0;
  1903. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  1904. if (ret < 0)
  1905. ret = 0;
  1906. if (debug_level >= DEBUG_LEVEL_INFO)
  1907. printk("%s(%d):mgsl_write_room(%s)=%d\n",
  1908. __FILE__,__LINE__, info->device_name,ret );
  1909. if ( info->params.mode == MGSL_MODE_HDLC ||
  1910. info->params.mode == MGSL_MODE_RAW ) {
  1911. /* operating in synchronous (frame oriented) mode */
  1912. if ( info->tx_active )
  1913. return 0;
  1914. else
  1915. return HDLC_MAX_FRAME_SIZE;
  1916. }
  1917. return ret;
  1918. } /* end of mgsl_write_room() */
  1919. /* mgsl_chars_in_buffer()
  1920. *
  1921. * Return the count of bytes in transmit buffer
  1922. *
  1923. * Arguments: tty pointer to tty info structure
  1924. * Return Value: None
  1925. */
  1926. static int mgsl_chars_in_buffer(struct tty_struct *tty)
  1927. {
  1928. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1929. if (debug_level >= DEBUG_LEVEL_INFO)
  1930. printk("%s(%d):mgsl_chars_in_buffer(%s)\n",
  1931. __FILE__,__LINE__, info->device_name );
  1932. if (mgsl_paranoia_check(info, tty->name, "mgsl_chars_in_buffer"))
  1933. return 0;
  1934. if (debug_level >= DEBUG_LEVEL_INFO)
  1935. printk("%s(%d):mgsl_chars_in_buffer(%s)=%d\n",
  1936. __FILE__,__LINE__, info->device_name,info->xmit_cnt );
  1937. if ( info->params.mode == MGSL_MODE_HDLC ||
  1938. info->params.mode == MGSL_MODE_RAW ) {
  1939. /* operating in synchronous (frame oriented) mode */
  1940. if ( info->tx_active )
  1941. return info->max_frame_size;
  1942. else
  1943. return 0;
  1944. }
  1945. return info->xmit_cnt;
  1946. } /* end of mgsl_chars_in_buffer() */
  1947. /* mgsl_flush_buffer()
  1948. *
  1949. * Discard all data in the send buffer
  1950. *
  1951. * Arguments: tty pointer to tty info structure
  1952. * Return Value: None
  1953. */
  1954. static void mgsl_flush_buffer(struct tty_struct *tty)
  1955. {
  1956. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1957. unsigned long flags;
  1958. if (debug_level >= DEBUG_LEVEL_INFO)
  1959. printk("%s(%d):mgsl_flush_buffer(%s) entry\n",
  1960. __FILE__,__LINE__, info->device_name );
  1961. if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_buffer"))
  1962. return;
  1963. spin_lock_irqsave(&info->irq_spinlock,flags);
  1964. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1965. del_timer(&info->tx_timer);
  1966. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1967. wake_up_interruptible(&tty->write_wait);
  1968. tty_wakeup(tty);
  1969. }
  1970. /* mgsl_send_xchar()
  1971. *
  1972. * Send a high-priority XON/XOFF character
  1973. *
  1974. * Arguments: tty pointer to tty info structure
  1975. * ch character to send
  1976. * Return Value: None
  1977. */
  1978. static void mgsl_send_xchar(struct tty_struct *tty, char ch)
  1979. {
  1980. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1981. unsigned long flags;
  1982. if (debug_level >= DEBUG_LEVEL_INFO)
  1983. printk("%s(%d):mgsl_send_xchar(%s,%d)\n",
  1984. __FILE__,__LINE__, info->device_name, ch );
  1985. if (mgsl_paranoia_check(info, tty->name, "mgsl_send_xchar"))
  1986. return;
  1987. info->x_char = ch;
  1988. if (ch) {
  1989. /* Make sure transmit interrupts are on */
  1990. spin_lock_irqsave(&info->irq_spinlock,flags);
  1991. if (!info->tx_enabled)
  1992. usc_start_transmitter(info);
  1993. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1994. }
  1995. } /* end of mgsl_send_xchar() */
  1996. /* mgsl_throttle()
  1997. *
  1998. * Signal remote device to throttle send data (our receive data)
  1999. *
  2000. * Arguments: tty pointer to tty info structure
  2001. * Return Value: None
  2002. */
  2003. static void mgsl_throttle(struct tty_struct * tty)
  2004. {
  2005. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  2006. unsigned long flags;
  2007. if (debug_level >= DEBUG_LEVEL_INFO)
  2008. printk("%s(%d):mgsl_throttle(%s) entry\n",
  2009. __FILE__,__LINE__, info->device_name );
  2010. if (mgsl_paranoia_check(info, tty->name, "mgsl_throttle"))
  2011. return;
  2012. if (I_IXOFF(tty))
  2013. mgsl_send_xchar(tty, STOP_CHAR(tty));
  2014. if (tty->termios->c_cflag & CRTSCTS) {
  2015. spin_lock_irqsave(&info->irq_spinlock,flags);
  2016. info->serial_signals &= ~SerialSignal_RTS;
  2017. usc_set_serial_signals(info);
  2018. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2019. }
  2020. } /* end of mgsl_throttle() */
  2021. /* mgsl_unthrottle()
  2022. *
  2023. * Signal remote device to stop throttling send data (our receive data)
  2024. *
  2025. * Arguments: tty pointer to tty info structure
  2026. * Return Value: None
  2027. */
  2028. static void mgsl_unthrottle(struct tty_struct * tty)
  2029. {
  2030. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  2031. unsigned long flags;
  2032. if (debug_level >= DEBUG_LEVEL_INFO)
  2033. printk("%s(%d):mgsl_unthrottle(%s) entry\n",
  2034. __FILE__,__LINE__, info->device_name );
  2035. if (mgsl_paranoia_check(info, tty->name, "mgsl_unthrottle"))
  2036. return;
  2037. if (I_IXOFF(tty)) {
  2038. if (info->x_char)
  2039. info->x_char = 0;
  2040. else
  2041. mgsl_send_xchar(tty, START_CHAR(tty));
  2042. }
  2043. if (tty->termios->c_cflag & CRTSCTS) {
  2044. spin_lock_irqsave(&info->irq_spinlock,flags);
  2045. info->serial_signals |= SerialSignal_RTS;
  2046. usc_set_serial_signals(info);
  2047. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2048. }
  2049. } /* end of mgsl_unthrottle() */
  2050. /* mgsl_get_stats()
  2051. *
  2052. * get the current serial parameters information
  2053. *
  2054. * Arguments: info pointer to device instance data
  2055. * user_icount pointer to buffer to hold returned stats
  2056. *
  2057. * Return Value: 0 if success, otherwise error code
  2058. */
  2059. static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount __user *user_icount)
  2060. {
  2061. int err;
  2062. if (debug_level >= DEBUG_LEVEL_INFO)
  2063. printk("%s(%d):mgsl_get_params(%s)\n",
  2064. __FILE__,__LINE__, info->device_name);
  2065. COPY_TO_USER(err,user_icount, &info->icount, sizeof(struct mgsl_icount));
  2066. if (err) {
  2067. if ( debug_level >= DEBUG_LEVEL_INFO )
  2068. printk( "%s(%d):mgsl_get_stats(%s) user buffer copy failed\n",
  2069. __FILE__,__LINE__,info->device_name);
  2070. return -EFAULT;
  2071. }
  2072. return 0;
  2073. } /* end of mgsl_get_stats() */
  2074. /* mgsl_get_params()
  2075. *
  2076. * get the current serial parameters information
  2077. *
  2078. * Arguments: info pointer to device instance data
  2079. * user_params pointer to buffer to hold returned params
  2080. *
  2081. * Return Value: 0 if success, otherwise error code
  2082. */
  2083. static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_params)
  2084. {
  2085. int err;
  2086. if (debug_level >= DEBUG_LEVEL_INFO)
  2087. printk("%s(%d):mgsl_get_params(%s)\n",
  2088. __FILE__,__LINE__, info->device_name);
  2089. COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
  2090. if (err) {
  2091. if ( debug_level >= DEBUG_LEVEL_INFO )
  2092. printk( "%s(%d):mgsl_get_params(%s) user buffer copy failed\n",
  2093. __FILE__,__LINE__,info->device_name);
  2094. return -EFAULT;
  2095. }
  2096. return 0;
  2097. } /* end of mgsl_get_params() */
  2098. /* mgsl_set_params()
  2099. *
  2100. * set the serial parameters
  2101. *
  2102. * Arguments:
  2103. *
  2104. * info pointer to device instance data
  2105. * new_params user buffer containing new serial params
  2106. *
  2107. * Return Value: 0 if success, otherwise error code
  2108. */
  2109. static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_params)
  2110. {
  2111. unsigned long flags;
  2112. MGSL_PARAMS tmp_params;
  2113. int err;
  2114. if (debug_level >= DEBUG_LEVEL_INFO)
  2115. printk("%s(%d):mgsl_set_params %s\n", __FILE__,__LINE__,
  2116. info->device_name );
  2117. COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
  2118. if (err) {
  2119. if ( debug_level >= DEBUG_LEVEL_INFO )
  2120. printk( "%s(%d):mgsl_set_params(%s) user buffer copy failed\n",
  2121. __FILE__,__LINE__,info->device_name);
  2122. return -EFAULT;
  2123. }
  2124. spin_lock_irqsave(&info->irq_spinlock,flags);
  2125. memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
  2126. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2127. mgsl_change_params(info);
  2128. return 0;
  2129. } /* end of mgsl_set_params() */
  2130. /* mgsl_get_txidle()
  2131. *
  2132. * get the current transmit idle mode
  2133. *
  2134. * Arguments: info pointer to device instance data
  2135. * idle_mode pointer to buffer to hold returned idle mode
  2136. *
  2137. * Return Value: 0 if success, otherwise error code
  2138. */
  2139. static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode)
  2140. {
  2141. int err;
  2142. if (debug_level >= DEBUG_LEVEL_INFO)
  2143. printk("%s(%d):mgsl_get_txidle(%s)=%d\n",
  2144. __FILE__,__LINE__, info->device_name, info->idle_mode);
  2145. COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
  2146. if (err) {
  2147. if ( debug_level >= DEBUG_LEVEL_INFO )
  2148. printk( "%s(%d):mgsl_get_txidle(%s) user buffer copy failed\n",
  2149. __FILE__,__LINE__,info->device_name);
  2150. return -EFAULT;
  2151. }
  2152. return 0;
  2153. } /* end of mgsl_get_txidle() */
  2154. /* mgsl_set_txidle() service ioctl to set transmit idle mode
  2155. *
  2156. * Arguments: info pointer to device instance data
  2157. * idle_mode new idle mode
  2158. *
  2159. * Return Value: 0 if success, otherwise error code
  2160. */
  2161. static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode)
  2162. {
  2163. unsigned long flags;
  2164. if (debug_level >= DEBUG_LEVEL_INFO)
  2165. printk("%s(%d):mgsl_set_txidle(%s,%d)\n", __FILE__,__LINE__,
  2166. info->device_name, idle_mode );
  2167. spin_lock_irqsave(&info->irq_spinlock,flags);
  2168. info->idle_mode = idle_mode;
  2169. usc_set_txidle( info );
  2170. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2171. return 0;
  2172. } /* end of mgsl_set_txidle() */
  2173. /* mgsl_txenable()
  2174. *
  2175. * enable or disable the transmitter
  2176. *
  2177. * Arguments:
  2178. *
  2179. * info pointer to device instance data
  2180. * enable 1 = enable, 0 = disable
  2181. *
  2182. * Return Value: 0 if success, otherwise error code
  2183. */
  2184. static int mgsl_txenable(struct mgsl_struct * info, int enable)
  2185. {
  2186. unsigned long flags;
  2187. if (debug_level >= DEBUG_LEVEL_INFO)
  2188. printk("%s(%d):mgsl_txenable(%s,%d)\n", __FILE__,__LINE__,
  2189. info->device_name, enable);
  2190. spin_lock_irqsave(&info->irq_spinlock,flags);
  2191. if ( enable ) {
  2192. if ( !info->tx_enabled ) {
  2193. usc_start_transmitter(info);
  2194. /*--------------------------------------------------
  2195. * if HDLC/SDLC Loop mode, attempt to insert the
  2196. * station in the 'loop' by setting CMR:13. Upon
  2197. * receipt of the next GoAhead (RxAbort) sequence,
  2198. * the OnLoop indicator (CCSR:7) should go active
  2199. * to indicate that we are on the loop
  2200. *--------------------------------------------------*/
  2201. if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
  2202. usc_loopmode_insert_request( info );
  2203. }
  2204. } else {
  2205. if ( info->tx_enabled )
  2206. usc_stop_transmitter(info);
  2207. }
  2208. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2209. return 0;
  2210. } /* end of mgsl_txenable() */
  2211. /* mgsl_txabort() abort send HDLC frame
  2212. *
  2213. * Arguments: info pointer to device instance data
  2214. * Return Value: 0 if success, otherwise error code
  2215. */
  2216. static int mgsl_txabort(struct mgsl_struct * info)
  2217. {
  2218. unsigned long flags;
  2219. if (debug_level >= DEBUG_LEVEL_INFO)
  2220. printk("%s(%d):mgsl_txabort(%s)\n", __FILE__,__LINE__,
  2221. info->device_name);
  2222. spin_lock_irqsave(&info->irq_spinlock,flags);
  2223. if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC )
  2224. {
  2225. if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
  2226. usc_loopmode_cancel_transmit( info );
  2227. else
  2228. usc_TCmd(info,TCmd_SendAbort);
  2229. }
  2230. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2231. return 0;
  2232. } /* end of mgsl_txabort() */
  2233. /* mgsl_rxenable() enable or disable the receiver
  2234. *
  2235. * Arguments: info pointer to device instance data
  2236. * enable 1 = enable, 0 = disable
  2237. * Return Value: 0 if success, otherwise error code
  2238. */
  2239. static int mgsl_rxenable(struct mgsl_struct * info, int enable)
  2240. {
  2241. unsigned long flags;
  2242. if (debug_level >= DEBUG_LEVEL_INFO)
  2243. printk("%s(%d):mgsl_rxenable(%s,%d)\n", __FILE__,__LINE__,
  2244. info->device_name, enable);
  2245. spin_lock_irqsave(&info->irq_spinlock,flags);
  2246. if ( enable ) {
  2247. if ( !info->rx_enabled )
  2248. usc_start_receiver(info);
  2249. } else {
  2250. if ( info->rx_enabled )
  2251. usc_stop_receiver(info);
  2252. }
  2253. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2254. return 0;
  2255. } /* end of mgsl_rxenable() */
  2256. /* mgsl_wait_event() wait for specified event to occur
  2257. *
  2258. * Arguments: info pointer to device instance data
  2259. * mask pointer to bitmask of events to wait for
  2260. * Return Value: 0 if successful and bit mask updated with
  2261. * of events triggerred,
  2262. * otherwise error code
  2263. */
  2264. static int mgsl_wait_event(struct mgsl_struct * info, int __user * mask_ptr)
  2265. {
  2266. unsigned long flags;
  2267. int s;
  2268. int rc=0;
  2269. struct mgsl_icount cprev, cnow;
  2270. int events;
  2271. int mask;
  2272. struct _input_signal_events oldsigs, newsigs;
  2273. DECLARE_WAITQUEUE(wait, current);
  2274. COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
  2275. if (rc) {
  2276. return -EFAULT;
  2277. }
  2278. if (debug_level >= DEBUG_LEVEL_INFO)
  2279. printk("%s(%d):mgsl_wait_event(%s,%d)\n", __FILE__,__LINE__,
  2280. info->device_name, mask);
  2281. spin_lock_irqsave(&info->irq_spinlock,flags);
  2282. /* return immediately if state matches requested events */
  2283. usc_get_serial_signals(info);
  2284. s = info->serial_signals;
  2285. events = mask &
  2286. ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
  2287. ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
  2288. ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
  2289. ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
  2290. if (events) {
  2291. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2292. goto exit;
  2293. }
  2294. /* save current irq counts */
  2295. cprev = info->icount;
  2296. oldsigs = info->input_signal_events;
  2297. /* enable hunt and idle irqs if needed */
  2298. if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
  2299. u16 oldreg = usc_InReg(info,RICR);
  2300. u16 newreg = oldreg +
  2301. (mask & MgslEvent_ExitHuntMode ? RXSTATUS_EXITED_HUNT:0) +
  2302. (mask & MgslEvent_IdleReceived ? RXSTATUS_IDLE_RECEIVED:0);
  2303. if (oldreg != newreg)
  2304. usc_OutReg(info, RICR, newreg);
  2305. }
  2306. set_current_state(TASK_INTERRUPTIBLE);
  2307. add_wait_queue(&info->event_wait_q, &wait);
  2308. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2309. for(;;) {
  2310. schedule();
  2311. if (signal_pending(current)) {
  2312. rc = -ERESTARTSYS;
  2313. break;
  2314. }
  2315. /* get current irq counts */
  2316. spin_lock_irqsave(&info->irq_spinlock,flags);
  2317. cnow = info->icount;
  2318. newsigs = info->input_signal_events;
  2319. set_current_state(TASK_INTERRUPTIBLE);
  2320. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2321. /* if no change, wait aborted for some reason */
  2322. if (newsigs.dsr_up == oldsigs.dsr_up &&
  2323. newsigs.dsr_down == oldsigs.dsr_down &&
  2324. newsigs.dcd_up == oldsigs.dcd_up &&
  2325. newsigs.dcd_down == oldsigs.dcd_down &&
  2326. newsigs.cts_up == oldsigs.cts_up &&
  2327. newsigs.cts_down == oldsigs.cts_down &&
  2328. newsigs.ri_up == oldsigs.ri_up &&
  2329. newsigs.ri_down == oldsigs.ri_down &&
  2330. cnow.exithunt == cprev.exithunt &&
  2331. cnow.rxidle == cprev.rxidle) {
  2332. rc = -EIO;
  2333. break;
  2334. }
  2335. events = mask &
  2336. ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
  2337. (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
  2338. (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
  2339. (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
  2340. (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
  2341. (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
  2342. (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
  2343. (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
  2344. (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
  2345. (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
  2346. if (events)
  2347. break;
  2348. cprev = cnow;
  2349. oldsigs = newsigs;
  2350. }
  2351. remove_wait_queue(&info->event_wait_q, &wait);
  2352. set_current_state(TASK_RUNNING);
  2353. if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
  2354. spin_lock_irqsave(&info->irq_spinlock,flags);
  2355. if (!waitqueue_active(&info->event_wait_q)) {
  2356. /* disable enable exit hunt mode/idle rcvd IRQs */
  2357. usc_OutReg(info, RICR, usc_InReg(info,RICR) &
  2358. ~(RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED));
  2359. }
  2360. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2361. }
  2362. exit:
  2363. if ( rc == 0 )
  2364. PUT_USER(rc, events, mask_ptr);
  2365. return rc;
  2366. } /* end of mgsl_wait_event() */
  2367. static int modem_input_wait(struct mgsl_struct *info,int arg)
  2368. {
  2369. unsigned long flags;
  2370. int rc;
  2371. struct mgsl_icount cprev, cnow;
  2372. DECLARE_WAITQUEUE(wait, current);
  2373. /* save current irq counts */
  2374. spin_lock_irqsave(&info->irq_spinlock,flags);
  2375. cprev = info->icount;
  2376. add_wait_queue(&info->status_event_wait_q, &wait);
  2377. set_current_state(TASK_INTERRUPTIBLE);
  2378. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2379. for(;;) {
  2380. schedule();
  2381. if (signal_pending(current)) {
  2382. rc = -ERESTARTSYS;
  2383. break;
  2384. }
  2385. /* get new irq counts */
  2386. spin_lock_irqsave(&info->irq_spinlock,flags);
  2387. cnow = info->icount;
  2388. set_current_state(TASK_INTERRUPTIBLE);
  2389. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2390. /* if no change, wait aborted for some reason */
  2391. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2392. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  2393. rc = -EIO;
  2394. break;
  2395. }
  2396. /* check for change in caller specified modem input */
  2397. if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
  2398. (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
  2399. (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
  2400. (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
  2401. rc = 0;
  2402. break;
  2403. }
  2404. cprev = cnow;
  2405. }
  2406. remove_wait_queue(&info->status_event_wait_q, &wait);
  2407. set_current_state(TASK_RUNNING);
  2408. return rc;
  2409. }
  2410. /* return the state of the serial control and status signals
  2411. */
  2412. static int tiocmget(struct tty_struct *tty, struct file *file)
  2413. {
  2414. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  2415. unsigned int result;
  2416. unsigned long flags;
  2417. spin_lock_irqsave(&info->irq_spinlock,flags);
  2418. usc_get_serial_signals(info);
  2419. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2420. result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
  2421. ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
  2422. ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
  2423. ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
  2424. ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
  2425. ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
  2426. if (debug_level >= DEBUG_LEVEL_INFO)
  2427. printk("%s(%d):%s tiocmget() value=%08X\n",
  2428. __FILE__,__LINE__, info->device_name, result );
  2429. return result;
  2430. }
  2431. /* set modem control signals (DTR/RTS)
  2432. */
  2433. static int tiocmset(struct tty_struct *tty, struct file *file,
  2434. unsigned int set, unsigned int clear)
  2435. {
  2436. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  2437. unsigned long flags;
  2438. if (debug_level >= DEBUG_LEVEL_INFO)
  2439. printk("%s(%d):%s tiocmset(%x,%x)\n",
  2440. __FILE__,__LINE__,info->device_name, set, clear);
  2441. if (set & TIOCM_RTS)
  2442. info->serial_signals |= SerialSignal_RTS;
  2443. if (set & TIOCM_DTR)
  2444. info->serial_signals |= SerialSignal_DTR;
  2445. if (clear & TIOCM_RTS)
  2446. info->serial_signals &= ~SerialSignal_RTS;
  2447. if (clear & TIOCM_DTR)
  2448. info->serial_signals &= ~SerialSignal_DTR;
  2449. spin_lock_irqsave(&info->irq_spinlock,flags);
  2450. usc_set_serial_signals(info);
  2451. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2452. return 0;
  2453. }
  2454. /* mgsl_break() Set or clear transmit break condition
  2455. *
  2456. * Arguments: tty pointer to tty instance data
  2457. * break_state -1=set break condition, 0=clear
  2458. * Return Value: None
  2459. */
  2460. static void mgsl_break(struct tty_struct *tty, int break_state)
  2461. {
  2462. struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
  2463. unsigned long flags;
  2464. if (debug_level >= DEBUG_LEVEL_INFO)
  2465. printk("%s(%d):mgsl_break(%s,%d)\n",
  2466. __FILE__,__LINE__, info->device_name, break_state);
  2467. if (mgsl_paranoia_check(info, tty->name, "mgsl_break"))
  2468. return;
  2469. spin_lock_irqsave(&info->irq_spinlock,flags);
  2470. if (break_state == -1)
  2471. usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) | BIT7));
  2472. else
  2473. usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) & ~BIT7));
  2474. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2475. } /* end of mgsl_break() */
  2476. /* mgsl_ioctl() Service an IOCTL request
  2477. *
  2478. * Arguments:
  2479. *
  2480. * tty pointer to tty instance data
  2481. * file pointer to associated file object for device
  2482. * cmd IOCTL command code
  2483. * arg command argument/context
  2484. *
  2485. * Return Value: 0 if success, otherwise error code
  2486. */
  2487. static int mgsl_ioctl(struct tty_struct *tty, struct file * file,
  2488. unsigned int cmd, unsigned long arg)
  2489. {
  2490. struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
  2491. if (debug_level >= DEBUG_LEVEL_INFO)
  2492. printk("%s(%d):mgsl_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
  2493. info->device_name, cmd );
  2494. if (mgsl_paranoia_check(info, tty->name, "mgsl_ioctl"))
  2495. return -ENODEV;
  2496. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  2497. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  2498. if (tty->flags & (1 << TTY_IO_ERROR))
  2499. return -EIO;
  2500. }
  2501. return mgsl_ioctl_common(info, cmd, arg);
  2502. }
  2503. static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg)
  2504. {
  2505. int error;
  2506. struct mgsl_icount cnow; /* kernel counter temps */
  2507. void __user *argp = (void __user *)arg;
  2508. struct serial_icounter_struct __user *p_cuser; /* user space */
  2509. unsigned long flags;
  2510. switch (cmd) {
  2511. case MGSL_IOCGPARAMS:
  2512. return mgsl_get_params(info, argp);
  2513. case MGSL_IOCSPARAMS:
  2514. return mgsl_set_params(info, argp);
  2515. case MGSL_IOCGTXIDLE:
  2516. return mgsl_get_txidle(info, argp);
  2517. case MGSL_IOCSTXIDLE:
  2518. return mgsl_set_txidle(info,(int)arg);
  2519. case MGSL_IOCTXENABLE:
  2520. return mgsl_txenable(info,(int)arg);
  2521. case MGSL_IOCRXENABLE:
  2522. return mgsl_rxenable(info,(int)arg);
  2523. case MGSL_IOCTXABORT:
  2524. return mgsl_txabort(info);
  2525. case MGSL_IOCGSTATS:
  2526. return mgsl_get_stats(info, argp);
  2527. case MGSL_IOCWAITEVENT:
  2528. return mgsl_wait_event(info, argp);
  2529. case MGSL_IOCLOOPTXDONE:
  2530. return mgsl_loopmode_send_done(info);
  2531. /* Wait for modem input (DCD,RI,DSR,CTS) change
  2532. * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
  2533. */
  2534. case TIOCMIWAIT:
  2535. return modem_input_wait(info,(int)arg);
  2536. /*
  2537. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  2538. * Return: write counters to the user passed counter struct
  2539. * NB: both 1->0 and 0->1 transitions are counted except for
  2540. * RI where only 0->1 is counted.
  2541. */
  2542. case TIOCGICOUNT:
  2543. spin_lock_irqsave(&info->irq_spinlock,flags);
  2544. cnow = info->icount;
  2545. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2546. p_cuser = argp;
  2547. PUT_USER(error,cnow.cts, &p_cuser->cts);
  2548. if (error) return error;
  2549. PUT_USER(error,cnow.dsr, &p_cuser->dsr);
  2550. if (error) return error;
  2551. PUT_USER(error,cnow.rng, &p_cuser->rng);
  2552. if (error) return error;
  2553. PUT_USER(error,cnow.dcd, &p_cuser->dcd);
  2554. if (error) return error;
  2555. PUT_USER(error,cnow.rx, &p_cuser->rx);
  2556. if (error) return error;
  2557. PUT_USER(error,cnow.tx, &p_cuser->tx);
  2558. if (error) return error;
  2559. PUT_USER(error,cnow.frame, &p_cuser->frame);
  2560. if (error) return error;
  2561. PUT_USER(error,cnow.overrun, &p_cuser->overrun);
  2562. if (error) return error;
  2563. PUT_USER(error,cnow.parity, &p_cuser->parity);
  2564. if (error) return error;
  2565. PUT_USER(error,cnow.brk, &p_cuser->brk);
  2566. if (error) return error;
  2567. PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
  2568. if (error) return error;
  2569. return 0;
  2570. default:
  2571. return -ENOIOCTLCMD;
  2572. }
  2573. return 0;
  2574. }
  2575. /* mgsl_set_termios()
  2576. *
  2577. * Set new termios settings
  2578. *
  2579. * Arguments:
  2580. *
  2581. * tty pointer to tty structure
  2582. * termios pointer to buffer to hold returned old termios
  2583. *
  2584. * Return Value: None
  2585. */
  2586. static void mgsl_set_termios(struct tty_struct *tty, struct termios *old_termios)
  2587. {
  2588. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  2589. unsigned long flags;
  2590. if (debug_level >= DEBUG_LEVEL_INFO)
  2591. printk("%s(%d):mgsl_set_termios %s\n", __FILE__,__LINE__,
  2592. tty->driver->name );
  2593. /* just return if nothing has changed */
  2594. if ((tty->termios->c_cflag == old_termios->c_cflag)
  2595. && (RELEVANT_IFLAG(tty->termios->c_iflag)
  2596. == RELEVANT_IFLAG(old_termios->c_iflag)))
  2597. return;
  2598. mgsl_change_params(info);
  2599. /* Handle transition to B0 status */
  2600. if (old_termios->c_cflag & CBAUD &&
  2601. !(tty->termios->c_cflag & CBAUD)) {
  2602. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  2603. spin_lock_irqsave(&info->irq_spinlock,flags);
  2604. usc_set_serial_signals(info);
  2605. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2606. }
  2607. /* Handle transition away from B0 status */
  2608. if (!(old_termios->c_cflag & CBAUD) &&
  2609. tty->termios->c_cflag & CBAUD) {
  2610. info->serial_signals |= SerialSignal_DTR;
  2611. if (!(tty->termios->c_cflag & CRTSCTS) ||
  2612. !test_bit(TTY_THROTTLED, &tty->flags)) {
  2613. info->serial_signals |= SerialSignal_RTS;
  2614. }
  2615. spin_lock_irqsave(&info->irq_spinlock,flags);
  2616. usc_set_serial_signals(info);
  2617. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2618. }
  2619. /* Handle turning off CRTSCTS */
  2620. if (old_termios->c_cflag & CRTSCTS &&
  2621. !(tty->termios->c_cflag & CRTSCTS)) {
  2622. tty->hw_stopped = 0;
  2623. mgsl_start(tty);
  2624. }
  2625. } /* end of mgsl_set_termios() */
  2626. /* mgsl_close()
  2627. *
  2628. * Called when port is closed. Wait for remaining data to be
  2629. * sent. Disable port and free resources.
  2630. *
  2631. * Arguments:
  2632. *
  2633. * tty pointer to open tty structure
  2634. * filp pointer to open file object
  2635. *
  2636. * Return Value: None
  2637. */
  2638. static void mgsl_close(struct tty_struct *tty, struct file * filp)
  2639. {
  2640. struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
  2641. if (mgsl_paranoia_check(info, tty->name, "mgsl_close"))
  2642. return;
  2643. if (debug_level >= DEBUG_LEVEL_INFO)
  2644. printk("%s(%d):mgsl_close(%s) entry, count=%d\n",
  2645. __FILE__,__LINE__, info->device_name, info->count);
  2646. if (!info->count)
  2647. return;
  2648. if (tty_hung_up_p(filp))
  2649. goto cleanup;
  2650. if ((tty->count == 1) && (info->count != 1)) {
  2651. /*
  2652. * tty->count is 1 and the tty structure will be freed.
  2653. * info->count should be one in this case.
  2654. * if it's not, correct it so that the port is shutdown.
  2655. */
  2656. printk("mgsl_close: bad refcount; tty->count is 1, "
  2657. "info->count is %d\n", info->count);
  2658. info->count = 1;
  2659. }
  2660. info->count--;
  2661. /* if at least one open remaining, leave hardware active */
  2662. if (info->count)
  2663. goto cleanup;
  2664. info->flags |= ASYNC_CLOSING;
  2665. /* set tty->closing to notify line discipline to
  2666. * only process XON/XOFF characters. Only the N_TTY
  2667. * discipline appears to use this (ppp does not).
  2668. */
  2669. tty->closing = 1;
  2670. /* wait for transmit data to clear all layers */
  2671. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
  2672. if (debug_level >= DEBUG_LEVEL_INFO)
  2673. printk("%s(%d):mgsl_close(%s) calling tty_wait_until_sent\n",
  2674. __FILE__,__LINE__, info->device_name );
  2675. tty_wait_until_sent(tty, info->closing_wait);
  2676. }
  2677. if (info->flags & ASYNC_INITIALIZED)
  2678. mgsl_wait_until_sent(tty, info->timeout);
  2679. if (tty->driver->flush_buffer)
  2680. tty->driver->flush_buffer(tty);
  2681. tty_ldisc_flush(tty);
  2682. shutdown(info);
  2683. tty->closing = 0;
  2684. info->tty = NULL;
  2685. if (info->blocked_open) {
  2686. if (info->close_delay) {
  2687. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  2688. }
  2689. wake_up_interruptible(&info->open_wait);
  2690. }
  2691. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  2692. wake_up_interruptible(&info->close_wait);
  2693. cleanup:
  2694. if (debug_level >= DEBUG_LEVEL_INFO)
  2695. printk("%s(%d):mgsl_close(%s) exit, count=%d\n", __FILE__,__LINE__,
  2696. tty->driver->name, info->count);
  2697. } /* end of mgsl_close() */
  2698. /* mgsl_wait_until_sent()
  2699. *
  2700. * Wait until the transmitter is empty.
  2701. *
  2702. * Arguments:
  2703. *
  2704. * tty pointer to tty info structure
  2705. * timeout time to wait for send completion
  2706. *
  2707. * Return Value: None
  2708. */
  2709. static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout)
  2710. {
  2711. struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
  2712. unsigned long orig_jiffies, char_time;
  2713. if (!info )
  2714. return;
  2715. if (debug_level >= DEBUG_LEVEL_INFO)
  2716. printk("%s(%d):mgsl_wait_until_sent(%s) entry\n",
  2717. __FILE__,__LINE__, info->device_name );
  2718. if (mgsl_paranoia_check(info, tty->name, "mgsl_wait_until_sent"))
  2719. return;
  2720. if (!(info->flags & ASYNC_INITIALIZED))
  2721. goto exit;
  2722. orig_jiffies = jiffies;
  2723. /* Set check interval to 1/5 of estimated time to
  2724. * send a character, and make it at least 1. The check
  2725. * interval should also be less than the timeout.
  2726. * Note: use tight timings here to satisfy the NIST-PCTS.
  2727. */
  2728. if ( info->params.data_rate ) {
  2729. char_time = info->timeout/(32 * 5);
  2730. if (!char_time)
  2731. char_time++;
  2732. } else
  2733. char_time = 1;
  2734. if (timeout)
  2735. char_time = min_t(unsigned long, char_time, timeout);
  2736. if ( info->params.mode == MGSL_MODE_HDLC ||
  2737. info->params.mode == MGSL_MODE_RAW ) {
  2738. while (info->tx_active) {
  2739. msleep_interruptible(jiffies_to_msecs(char_time));
  2740. if (signal_pending(current))
  2741. break;
  2742. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  2743. break;
  2744. }
  2745. } else {
  2746. while (!(usc_InReg(info,TCSR) & TXSTATUS_ALL_SENT) &&
  2747. info->tx_enabled) {
  2748. msleep_interruptible(jiffies_to_msecs(char_time));
  2749. if (signal_pending(current))
  2750. break;
  2751. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  2752. break;
  2753. }
  2754. }
  2755. exit:
  2756. if (debug_level >= DEBUG_LEVEL_INFO)
  2757. printk("%s(%d):mgsl_wait_until_sent(%s) exit\n",
  2758. __FILE__,__LINE__, info->device_name );
  2759. } /* end of mgsl_wait_until_sent() */
  2760. /* mgsl_hangup()
  2761. *
  2762. * Called by tty_hangup() when a hangup is signaled.
  2763. * This is the same as to closing all open files for the port.
  2764. *
  2765. * Arguments: tty pointer to associated tty object
  2766. * Return Value: None
  2767. */
  2768. static void mgsl_hangup(struct tty_struct *tty)
  2769. {
  2770. struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
  2771. if (debug_level >= DEBUG_LEVEL_INFO)
  2772. printk("%s(%d):mgsl_hangup(%s)\n",
  2773. __FILE__,__LINE__, info->device_name );
  2774. if (mgsl_paranoia_check(info, tty->name, "mgsl_hangup"))
  2775. return;
  2776. mgsl_flush_buffer(tty);
  2777. shutdown(info);
  2778. info->count = 0;
  2779. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  2780. info->tty = NULL;
  2781. wake_up_interruptible(&info->open_wait);
  2782. } /* end of mgsl_hangup() */
  2783. /* block_til_ready()
  2784. *
  2785. * Block the current process until the specified port
  2786. * is ready to be opened.
  2787. *
  2788. * Arguments:
  2789. *
  2790. * tty pointer to tty info structure
  2791. * filp pointer to open file object
  2792. * info pointer to device instance data
  2793. *
  2794. * Return Value: 0 if success, otherwise error code
  2795. */
  2796. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  2797. struct mgsl_struct *info)
  2798. {
  2799. DECLARE_WAITQUEUE(wait, current);
  2800. int retval;
  2801. int do_clocal = 0, extra_count = 0;
  2802. unsigned long flags;
  2803. if (debug_level >= DEBUG_LEVEL_INFO)
  2804. printk("%s(%d):block_til_ready on %s\n",
  2805. __FILE__,__LINE__, tty->driver->name );
  2806. if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
  2807. /* nonblock mode is set or port is not enabled */
  2808. info->flags |= ASYNC_NORMAL_ACTIVE;
  2809. return 0;
  2810. }
  2811. if (tty->termios->c_cflag & CLOCAL)
  2812. do_clocal = 1;
  2813. /* Wait for carrier detect and the line to become
  2814. * free (i.e., not in use by the callout). While we are in
  2815. * this loop, info->count is dropped by one, so that
  2816. * mgsl_close() knows when to free things. We restore it upon
  2817. * exit, either normal or abnormal.
  2818. */
  2819. retval = 0;
  2820. add_wait_queue(&info->open_wait, &wait);
  2821. if (debug_level >= DEBUG_LEVEL_INFO)
  2822. printk("%s(%d):block_til_ready before block on %s count=%d\n",
  2823. __FILE__,__LINE__, tty->driver->name, info->count );
  2824. spin_lock_irqsave(&info->irq_spinlock, flags);
  2825. if (!tty_hung_up_p(filp)) {
  2826. extra_count = 1;
  2827. info->count--;
  2828. }
  2829. spin_unlock_irqrestore(&info->irq_spinlock, flags);
  2830. info->blocked_open++;
  2831. while (1) {
  2832. if (tty->termios->c_cflag & CBAUD) {
  2833. spin_lock_irqsave(&info->irq_spinlock,flags);
  2834. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  2835. usc_set_serial_signals(info);
  2836. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2837. }
  2838. set_current_state(TASK_INTERRUPTIBLE);
  2839. if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
  2840. retval = (info->flags & ASYNC_HUP_NOTIFY) ?
  2841. -EAGAIN : -ERESTARTSYS;
  2842. break;
  2843. }
  2844. spin_lock_irqsave(&info->irq_spinlock,flags);
  2845. usc_get_serial_signals(info);
  2846. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2847. if (!(info->flags & ASYNC_CLOSING) &&
  2848. (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
  2849. break;
  2850. }
  2851. if (signal_pending(current)) {
  2852. retval = -ERESTARTSYS;
  2853. break;
  2854. }
  2855. if (debug_level >= DEBUG_LEVEL_INFO)
  2856. printk("%s(%d):block_til_ready blocking on %s count=%d\n",
  2857. __FILE__,__LINE__, tty->driver->name, info->count );
  2858. schedule();
  2859. }
  2860. set_current_state(TASK_RUNNING);
  2861. remove_wait_queue(&info->open_wait, &wait);
  2862. if (extra_count)
  2863. info->count++;
  2864. info->blocked_open--;
  2865. if (debug_level >= DEBUG_LEVEL_INFO)
  2866. printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
  2867. __FILE__,__LINE__, tty->driver->name, info->count );
  2868. if (!retval)
  2869. info->flags |= ASYNC_NORMAL_ACTIVE;
  2870. return retval;
  2871. } /* end of block_til_ready() */
  2872. /* mgsl_open()
  2873. *
  2874. * Called when a port is opened. Init and enable port.
  2875. * Perform serial-specific initialization for the tty structure.
  2876. *
  2877. * Arguments: tty pointer to tty info structure
  2878. * filp associated file pointer
  2879. *
  2880. * Return Value: 0 if success, otherwise error code
  2881. */
  2882. static int mgsl_open(struct tty_struct *tty, struct file * filp)
  2883. {
  2884. struct mgsl_struct *info;
  2885. int retval, line;
  2886. unsigned long page;
  2887. unsigned long flags;
  2888. /* verify range of specified line number */
  2889. line = tty->index;
  2890. if ((line < 0) || (line >= mgsl_device_count)) {
  2891. printk("%s(%d):mgsl_open with invalid line #%d.\n",
  2892. __FILE__,__LINE__,line);
  2893. return -ENODEV;
  2894. }
  2895. /* find the info structure for the specified line */
  2896. info = mgsl_device_list;
  2897. while(info && info->line != line)
  2898. info = info->next_device;
  2899. if (mgsl_paranoia_check(info, tty->name, "mgsl_open"))
  2900. return -ENODEV;
  2901. tty->driver_data = info;
  2902. info->tty = tty;
  2903. if (debug_level >= DEBUG_LEVEL_INFO)
  2904. printk("%s(%d):mgsl_open(%s), old ref count = %d\n",
  2905. __FILE__,__LINE__,tty->driver->name, info->count);
  2906. /* If port is closing, signal caller to try again */
  2907. if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
  2908. if (info->flags & ASYNC_CLOSING)
  2909. interruptible_sleep_on(&info->close_wait);
  2910. retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
  2911. -EAGAIN : -ERESTARTSYS);
  2912. goto cleanup;
  2913. }
  2914. if (!tmp_buf) {
  2915. page = get_zeroed_page(GFP_KERNEL);
  2916. if (!page) {
  2917. retval = -ENOMEM;
  2918. goto cleanup;
  2919. }
  2920. if (tmp_buf)
  2921. free_page(page);
  2922. else
  2923. tmp_buf = (unsigned char *) page;
  2924. }
  2925. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  2926. spin_lock_irqsave(&info->netlock, flags);
  2927. if (info->netcount) {
  2928. retval = -EBUSY;
  2929. spin_unlock_irqrestore(&info->netlock, flags);
  2930. goto cleanup;
  2931. }
  2932. info->count++;
  2933. spin_unlock_irqrestore(&info->netlock, flags);
  2934. if (info->count == 1) {
  2935. /* 1st open on this device, init hardware */
  2936. retval = startup(info);
  2937. if (retval < 0)
  2938. goto cleanup;
  2939. }
  2940. retval = block_til_ready(tty, filp, info);
  2941. if (retval) {
  2942. if (debug_level >= DEBUG_LEVEL_INFO)
  2943. printk("%s(%d):block_til_ready(%s) returned %d\n",
  2944. __FILE__,__LINE__, info->device_name, retval);
  2945. goto cleanup;
  2946. }
  2947. if (debug_level >= DEBUG_LEVEL_INFO)
  2948. printk("%s(%d):mgsl_open(%s) success\n",
  2949. __FILE__,__LINE__, info->device_name);
  2950. retval = 0;
  2951. cleanup:
  2952. if (retval) {
  2953. if (tty->count == 1)
  2954. info->tty = NULL; /* tty layer will release tty struct */
  2955. if(info->count)
  2956. info->count--;
  2957. }
  2958. return retval;
  2959. } /* end of mgsl_open() */
  2960. /*
  2961. * /proc fs routines....
  2962. */
  2963. static inline int line_info(char *buf, struct mgsl_struct *info)
  2964. {
  2965. char stat_buf[30];
  2966. int ret;
  2967. unsigned long flags;
  2968. if (info->bus_type == MGSL_BUS_TYPE_PCI) {
  2969. ret = sprintf(buf, "%s:PCI io:%04X irq:%d mem:%08X lcr:%08X",
  2970. info->device_name, info->io_base, info->irq_level,
  2971. info->phys_memory_base, info->phys_lcr_base);
  2972. } else {
  2973. ret = sprintf(buf, "%s:(E)ISA io:%04X irq:%d dma:%d",
  2974. info->device_name, info->io_base,
  2975. info->irq_level, info->dma_level);
  2976. }
  2977. /* output current serial signal states */
  2978. spin_lock_irqsave(&info->irq_spinlock,flags);
  2979. usc_get_serial_signals(info);
  2980. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2981. stat_buf[0] = 0;
  2982. stat_buf[1] = 0;
  2983. if (info->serial_signals & SerialSignal_RTS)
  2984. strcat(stat_buf, "|RTS");
  2985. if (info->serial_signals & SerialSignal_CTS)
  2986. strcat(stat_buf, "|CTS");
  2987. if (info->serial_signals & SerialSignal_DTR)
  2988. strcat(stat_buf, "|DTR");
  2989. if (info->serial_signals & SerialSignal_DSR)
  2990. strcat(stat_buf, "|DSR");
  2991. if (info->serial_signals & SerialSignal_DCD)
  2992. strcat(stat_buf, "|CD");
  2993. if (info->serial_signals & SerialSignal_RI)
  2994. strcat(stat_buf, "|RI");
  2995. if (info->params.mode == MGSL_MODE_HDLC ||
  2996. info->params.mode == MGSL_MODE_RAW ) {
  2997. ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
  2998. info->icount.txok, info->icount.rxok);
  2999. if (info->icount.txunder)
  3000. ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
  3001. if (info->icount.txabort)
  3002. ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
  3003. if (info->icount.rxshort)
  3004. ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
  3005. if (info->icount.rxlong)
  3006. ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
  3007. if (info->icount.rxover)
  3008. ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
  3009. if (info->icount.rxcrc)
  3010. ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
  3011. } else {
  3012. ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
  3013. info->icount.tx, info->icount.rx);
  3014. if (info->icount.frame)
  3015. ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
  3016. if (info->icount.parity)
  3017. ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
  3018. if (info->icount.brk)
  3019. ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
  3020. if (info->icount.overrun)
  3021. ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
  3022. }
  3023. /* Append serial signal status to end */
  3024. ret += sprintf(buf+ret, " %s\n", stat_buf+1);
  3025. ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
  3026. info->tx_active,info->bh_requested,info->bh_running,
  3027. info->pending_bh);
  3028. spin_lock_irqsave(&info->irq_spinlock,flags);
  3029. {
  3030. u16 Tcsr = usc_InReg( info, TCSR );
  3031. u16 Tdmr = usc_InDmaReg( info, TDMR );
  3032. u16 Ticr = usc_InReg( info, TICR );
  3033. u16 Rscr = usc_InReg( info, RCSR );
  3034. u16 Rdmr = usc_InDmaReg( info, RDMR );
  3035. u16 Ricr = usc_InReg( info, RICR );
  3036. u16 Icr = usc_InReg( info, ICR );
  3037. u16 Dccr = usc_InReg( info, DCCR );
  3038. u16 Tmr = usc_InReg( info, TMR );
  3039. u16 Tccr = usc_InReg( info, TCCR );
  3040. u16 Ccar = inw( info->io_base + CCAR );
  3041. ret += sprintf(buf+ret, "tcsr=%04X tdmr=%04X ticr=%04X rcsr=%04X rdmr=%04X\n"
  3042. "ricr=%04X icr =%04X dccr=%04X tmr=%04X tccr=%04X ccar=%04X\n",
  3043. Tcsr,Tdmr,Ticr,Rscr,Rdmr,Ricr,Icr,Dccr,Tmr,Tccr,Ccar );
  3044. }
  3045. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  3046. return ret;
  3047. } /* end of line_info() */
  3048. /* mgsl_read_proc()
  3049. *
  3050. * Called to print information about devices
  3051. *
  3052. * Arguments:
  3053. * page page of memory to hold returned info
  3054. * start
  3055. * off
  3056. * count
  3057. * eof
  3058. * data
  3059. *
  3060. * Return Value:
  3061. */
  3062. static int mgsl_read_proc(char *page, char **start, off_t off, int count,
  3063. int *eof, void *data)
  3064. {
  3065. int len = 0, l;
  3066. off_t begin = 0;
  3067. struct mgsl_struct *info;
  3068. len += sprintf(page, "synclink driver:%s\n", driver_version);
  3069. info = mgsl_device_list;
  3070. while( info ) {
  3071. l = line_info(page + len, info);
  3072. len += l;
  3073. if (len+begin > off+count)
  3074. goto done;
  3075. if (len+begin < off) {
  3076. begin += len;
  3077. len = 0;
  3078. }
  3079. info = info->next_device;
  3080. }
  3081. *eof = 1;
  3082. done:
  3083. if (off >= len+begin)
  3084. return 0;
  3085. *start = page + (off-begin);
  3086. return ((count < begin+len-off) ? count : begin+len-off);
  3087. } /* end of mgsl_read_proc() */
  3088. /* mgsl_allocate_dma_buffers()
  3089. *
  3090. * Allocate and format DMA buffers (ISA adapter)
  3091. * or format shared memory buffers (PCI adapter).
  3092. *
  3093. * Arguments: info pointer to device instance data
  3094. * Return Value: 0 if success, otherwise error
  3095. */
  3096. static int mgsl_allocate_dma_buffers(struct mgsl_struct *info)
  3097. {
  3098. unsigned short BuffersPerFrame;
  3099. info->last_mem_alloc = 0;
  3100. /* Calculate the number of DMA buffers necessary to hold the */
  3101. /* largest allowable frame size. Note: If the max frame size is */
  3102. /* not an even multiple of the DMA buffer size then we need to */
  3103. /* round the buffer count per frame up one. */
  3104. BuffersPerFrame = (unsigned short)(info->max_frame_size/DMABUFFERSIZE);
  3105. if ( info->max_frame_size % DMABUFFERSIZE )
  3106. BuffersPerFrame++;
  3107. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  3108. /*
  3109. * The PCI adapter has 256KBytes of shared memory to use.
  3110. * This is 64 PAGE_SIZE buffers.
  3111. *
  3112. * The first page is used for padding at this time so the
  3113. * buffer list does not begin at offset 0 of the PCI
  3114. * adapter's shared memory.
  3115. *
  3116. * The 2nd page is used for the buffer list. A 4K buffer
  3117. * list can hold 128 DMA_BUFFER structures at 32 bytes
  3118. * each.
  3119. *
  3120. * This leaves 62 4K pages.
  3121. *
  3122. * The next N pages are used for transmit frame(s). We
  3123. * reserve enough 4K page blocks to hold the required
  3124. * number of transmit dma buffers (num_tx_dma_buffers),
  3125. * each of MaxFrameSize size.
  3126. *
  3127. * Of the remaining pages (62-N), determine how many can
  3128. * be used to receive full MaxFrameSize inbound frames
  3129. */
  3130. info->tx_buffer_count = info->num_tx_dma_buffers * BuffersPerFrame;
  3131. info->rx_buffer_count = 62 - info->tx_buffer_count;
  3132. } else {
  3133. /* Calculate the number of PAGE_SIZE buffers needed for */
  3134. /* receive and transmit DMA buffers. */
  3135. /* Calculate the number of DMA buffers necessary to */
  3136. /* hold 7 max size receive frames and one max size transmit frame. */
  3137. /* The receive buffer count is bumped by one so we avoid an */
  3138. /* End of List condition if all receive buffers are used when */
  3139. /* using linked list DMA buffers. */
  3140. info->tx_buffer_count = info->num_tx_dma_buffers * BuffersPerFrame;
  3141. info->rx_buffer_count = (BuffersPerFrame * MAXRXFRAMES) + 6;
  3142. /*
  3143. * limit total TxBuffers & RxBuffers to 62 4K total
  3144. * (ala PCI Allocation)
  3145. */
  3146. if ( (info->tx_buffer_count + info->rx_buffer_count) > 62 )
  3147. info->rx_buffer_count = 62 - info->tx_buffer_count;
  3148. }
  3149. if ( debug_level >= DEBUG_LEVEL_INFO )
  3150. printk("%s(%d):Allocating %d TX and %d RX DMA buffers.\n",
  3151. __FILE__,__LINE__, info->tx_buffer_count,info->rx_buffer_count);
  3152. if ( mgsl_alloc_buffer_list_memory( info ) < 0 ||
  3153. mgsl_alloc_frame_memory(info, info->rx_buffer_list, info->rx_buffer_count) < 0 ||
  3154. mgsl_alloc_frame_memory(info, info->tx_buffer_list, info->tx_buffer_count) < 0 ||
  3155. mgsl_alloc_intermediate_rxbuffer_memory(info) < 0 ||
  3156. mgsl_alloc_intermediate_txbuffer_memory(info) < 0 ) {
  3157. printk("%s(%d):Can't allocate DMA buffer memory\n",__FILE__,__LINE__);
  3158. return -ENOMEM;
  3159. }
  3160. mgsl_reset_rx_dma_buffers( info );
  3161. mgsl_reset_tx_dma_buffers( info );
  3162. return 0;
  3163. } /* end of mgsl_allocate_dma_buffers() */
  3164. /*
  3165. * mgsl_alloc_buffer_list_memory()
  3166. *
  3167. * Allocate a common DMA buffer for use as the
  3168. * receive and transmit buffer lists.
  3169. *
  3170. * A buffer list is a set of buffer entries where each entry contains
  3171. * a pointer to an actual buffer and a pointer to the next buffer entry
  3172. * (plus some other info about the buffer).
  3173. *
  3174. * The buffer entries for a list are built to form a circular list so
  3175. * that when the entire list has been traversed you start back at the
  3176. * beginning.
  3177. *
  3178. * This function allocates memory for just the buffer entries.
  3179. * The links (pointer to next entry) are filled in with the physical
  3180. * address of the next entry so the adapter can navigate the list
  3181. * using bus master DMA. The pointers to the actual buffers are filled
  3182. * out later when the actual buffers are allocated.
  3183. *
  3184. * Arguments: info pointer to device instance data
  3185. * Return Value: 0 if success, otherwise error
  3186. */
  3187. static int mgsl_alloc_buffer_list_memory( struct mgsl_struct *info )
  3188. {
  3189. unsigned int i;
  3190. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  3191. /* PCI adapter uses shared memory. */
  3192. info->buffer_list = info->memory_base + info->last_mem_alloc;
  3193. info->buffer_list_phys = info->last_mem_alloc;
  3194. info->last_mem_alloc += BUFFERLISTSIZE;
  3195. } else {
  3196. /* ISA adapter uses system memory. */
  3197. /* The buffer lists are allocated as a common buffer that both */
  3198. /* the processor and adapter can access. This allows the driver to */
  3199. /* inspect portions of the buffer while other portions are being */
  3200. /* updated by the adapter using Bus Master DMA. */
  3201. info->buffer_list = kmalloc(BUFFERLISTSIZE, GFP_KERNEL | GFP_DMA);
  3202. if ( info->buffer_list == NULL )
  3203. return -ENOMEM;
  3204. info->buffer_list_phys = isa_virt_to_bus(info->buffer_list);
  3205. }
  3206. /* We got the memory for the buffer entry lists. */
  3207. /* Initialize the memory block to all zeros. */
  3208. memset( info->buffer_list, 0, BUFFERLISTSIZE );
  3209. /* Save virtual address pointers to the receive and */
  3210. /* transmit buffer lists. (Receive 1st). These pointers will */
  3211. /* be used by the processor to access the lists. */
  3212. info->rx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
  3213. info->tx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
  3214. info->tx_buffer_list += info->rx_buffer_count;
  3215. /*
  3216. * Build the links for the buffer entry lists such that
  3217. * two circular lists are built. (Transmit and Receive).
  3218. *
  3219. * Note: the links are physical addresses
  3220. * which are read by the adapter to determine the next
  3221. * buffer entry to use.
  3222. */
  3223. for ( i = 0; i < info->rx_buffer_count; i++ ) {
  3224. /* calculate and store physical address of this buffer entry */
  3225. info->rx_buffer_list[i].phys_entry =
  3226. info->buffer_list_phys + (i * sizeof(DMABUFFERENTRY));
  3227. /* calculate and store physical address of */
  3228. /* next entry in cirular list of entries */
  3229. info->rx_buffer_list[i].link = info->buffer_list_phys;
  3230. if ( i < info->rx_buffer_count - 1 )
  3231. info->rx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
  3232. }
  3233. for ( i = 0; i < info->tx_buffer_count; i++ ) {
  3234. /* calculate and store physical address of this buffer entry */
  3235. info->tx_buffer_list[i].phys_entry = info->buffer_list_phys +
  3236. ((info->rx_buffer_count + i) * sizeof(DMABUFFERENTRY));
  3237. /* calculate and store physical address of */
  3238. /* next entry in cirular list of entries */
  3239. info->tx_buffer_list[i].link = info->buffer_list_phys +
  3240. info->rx_buffer_count * sizeof(DMABUFFERENTRY);
  3241. if ( i < info->tx_buffer_count - 1 )
  3242. info->tx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
  3243. }
  3244. return 0;
  3245. } /* end of mgsl_alloc_buffer_list_memory() */
  3246. /* Free DMA buffers allocated for use as the
  3247. * receive and transmit buffer lists.
  3248. * Warning:
  3249. *
  3250. * The data transfer buffers associated with the buffer list
  3251. * MUST be freed before freeing the buffer list itself because
  3252. * the buffer list contains the information necessary to free
  3253. * the individual buffers!
  3254. */
  3255. static void mgsl_free_buffer_list_memory( struct mgsl_struct *info )
  3256. {
  3257. if ( info->buffer_list && info->bus_type != MGSL_BUS_TYPE_PCI )
  3258. kfree(info->buffer_list);
  3259. info->buffer_list = NULL;
  3260. info->rx_buffer_list = NULL;
  3261. info->tx_buffer_list = NULL;
  3262. } /* end of mgsl_free_buffer_list_memory() */
  3263. /*
  3264. * mgsl_alloc_frame_memory()
  3265. *
  3266. * Allocate the frame DMA buffers used by the specified buffer list.
  3267. * Each DMA buffer will be one memory page in size. This is necessary
  3268. * because memory can fragment enough that it may be impossible
  3269. * contiguous pages.
  3270. *
  3271. * Arguments:
  3272. *
  3273. * info pointer to device instance data
  3274. * BufferList pointer to list of buffer entries
  3275. * Buffercount count of buffer entries in buffer list
  3276. *
  3277. * Return Value: 0 if success, otherwise -ENOMEM
  3278. */
  3279. static int mgsl_alloc_frame_memory(struct mgsl_struct *info,DMABUFFERENTRY *BufferList,int Buffercount)
  3280. {
  3281. int i;
  3282. unsigned long phys_addr;
  3283. /* Allocate page sized buffers for the receive buffer list */
  3284. for ( i = 0; i < Buffercount; i++ ) {
  3285. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  3286. /* PCI adapter uses shared memory buffers. */
  3287. BufferList[i].virt_addr = info->memory_base + info->last_mem_alloc;
  3288. phys_addr = info->last_mem_alloc;
  3289. info->last_mem_alloc += DMABUFFERSIZE;
  3290. } else {
  3291. /* ISA adapter uses system memory. */
  3292. BufferList[i].virt_addr =
  3293. kmalloc(DMABUFFERSIZE, GFP_KERNEL | GFP_DMA);
  3294. if ( BufferList[i].virt_addr == NULL )
  3295. return -ENOMEM;
  3296. phys_addr = isa_virt_to_bus(BufferList[i].virt_addr);
  3297. }
  3298. BufferList[i].phys_addr = phys_addr;
  3299. }
  3300. return 0;
  3301. } /* end of mgsl_alloc_frame_memory() */
  3302. /*
  3303. * mgsl_free_frame_memory()
  3304. *
  3305. * Free the buffers associated with
  3306. * each buffer entry of a buffer list.
  3307. *
  3308. * Arguments:
  3309. *
  3310. * info pointer to device instance data
  3311. * BufferList pointer to list of buffer entries
  3312. * Buffercount count of buffer entries in buffer list
  3313. *
  3314. * Return Value: None
  3315. */
  3316. static void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList, int Buffercount)
  3317. {
  3318. int i;
  3319. if ( BufferList ) {
  3320. for ( i = 0 ; i < Buffercount ; i++ ) {
  3321. if ( BufferList[i].virt_addr ) {
  3322. if ( info->bus_type != MGSL_BUS_TYPE_PCI )
  3323. kfree(BufferList[i].virt_addr);
  3324. BufferList[i].virt_addr = NULL;
  3325. }
  3326. }
  3327. }
  3328. } /* end of mgsl_free_frame_memory() */
  3329. /* mgsl_free_dma_buffers()
  3330. *
  3331. * Free DMA buffers
  3332. *
  3333. * Arguments: info pointer to device instance data
  3334. * Return Value: None
  3335. */
  3336. static void mgsl_free_dma_buffers( struct mgsl_struct *info )
  3337. {
  3338. mgsl_free_frame_memory( info, info->rx_buffer_list, info->rx_buffer_count );
  3339. mgsl_free_frame_memory( info, info->tx_buffer_list, info->tx_buffer_count );
  3340. mgsl_free_buffer_list_memory( info );
  3341. } /* end of mgsl_free_dma_buffers() */
  3342. /*
  3343. * mgsl_alloc_intermediate_rxbuffer_memory()
  3344. *
  3345. * Allocate a buffer large enough to hold max_frame_size. This buffer
  3346. * is used to pass an assembled frame to the line discipline.
  3347. *
  3348. * Arguments:
  3349. *
  3350. * info pointer to device instance data
  3351. *
  3352. * Return Value: 0 if success, otherwise -ENOMEM
  3353. */
  3354. static int mgsl_alloc_intermediate_rxbuffer_memory(struct mgsl_struct *info)
  3355. {
  3356. info->intermediate_rxbuffer = kmalloc(info->max_frame_size, GFP_KERNEL | GFP_DMA);
  3357. if ( info->intermediate_rxbuffer == NULL )
  3358. return -ENOMEM;
  3359. return 0;
  3360. } /* end of mgsl_alloc_intermediate_rxbuffer_memory() */
  3361. /*
  3362. * mgsl_free_intermediate_rxbuffer_memory()
  3363. *
  3364. *
  3365. * Arguments:
  3366. *
  3367. * info pointer to device instance data
  3368. *
  3369. * Return Value: None
  3370. */
  3371. static void mgsl_free_intermediate_rxbuffer_memory(struct mgsl_struct *info)
  3372. {
  3373. if ( info->intermediate_rxbuffer )
  3374. kfree(info->intermediate_rxbuffer);
  3375. info->intermediate_rxbuffer = NULL;
  3376. } /* end of mgsl_free_intermediate_rxbuffer_memory() */
  3377. /*
  3378. * mgsl_alloc_intermediate_txbuffer_memory()
  3379. *
  3380. * Allocate intermdiate transmit buffer(s) large enough to hold max_frame_size.
  3381. * This buffer is used to load transmit frames into the adapter's dma transfer
  3382. * buffers when there is sufficient space.
  3383. *
  3384. * Arguments:
  3385. *
  3386. * info pointer to device instance data
  3387. *
  3388. * Return Value: 0 if success, otherwise -ENOMEM
  3389. */
  3390. static int mgsl_alloc_intermediate_txbuffer_memory(struct mgsl_struct *info)
  3391. {
  3392. int i;
  3393. if ( debug_level >= DEBUG_LEVEL_INFO )
  3394. printk("%s %s(%d) allocating %d tx holding buffers\n",
  3395. info->device_name, __FILE__,__LINE__,info->num_tx_holding_buffers);
  3396. memset(info->tx_holding_buffers,0,sizeof(info->tx_holding_buffers));
  3397. for ( i=0; i<info->num_tx_holding_buffers; ++i) {
  3398. info->tx_holding_buffers[i].buffer =
  3399. kmalloc(info->max_frame_size, GFP_KERNEL);
  3400. if ( info->tx_holding_buffers[i].buffer == NULL )
  3401. return -ENOMEM;
  3402. }
  3403. return 0;
  3404. } /* end of mgsl_alloc_intermediate_txbuffer_memory() */
  3405. /*
  3406. * mgsl_free_intermediate_txbuffer_memory()
  3407. *
  3408. *
  3409. * Arguments:
  3410. *
  3411. * info pointer to device instance data
  3412. *
  3413. * Return Value: None
  3414. */
  3415. static void mgsl_free_intermediate_txbuffer_memory(struct mgsl_struct *info)
  3416. {
  3417. int i;
  3418. for ( i=0; i<info->num_tx_holding_buffers; ++i ) {
  3419. if ( info->tx_holding_buffers[i].buffer ) {
  3420. kfree(info->tx_holding_buffers[i].buffer);
  3421. info->tx_holding_buffers[i].buffer=NULL;
  3422. }
  3423. }
  3424. info->get_tx_holding_index = 0;
  3425. info->put_tx_holding_index = 0;
  3426. info->tx_holding_count = 0;
  3427. } /* end of mgsl_free_intermediate_txbuffer_memory() */
  3428. /*
  3429. * load_next_tx_holding_buffer()
  3430. *
  3431. * attempts to load the next buffered tx request into the
  3432. * tx dma buffers
  3433. *
  3434. * Arguments:
  3435. *
  3436. * info pointer to device instance data
  3437. *
  3438. * Return Value: 1 if next buffered tx request loaded
  3439. * into adapter's tx dma buffer,
  3440. * 0 otherwise
  3441. */
  3442. static int load_next_tx_holding_buffer(struct mgsl_struct *info)
  3443. {
  3444. int ret = 0;
  3445. if ( info->tx_holding_count ) {
  3446. /* determine if we have enough tx dma buffers
  3447. * to accommodate the next tx frame
  3448. */
  3449. struct tx_holding_buffer *ptx =
  3450. &info->tx_holding_buffers[info->get_tx_holding_index];
  3451. int num_free = num_free_tx_dma_buffers(info);
  3452. int num_needed = ptx->buffer_size / DMABUFFERSIZE;
  3453. if ( ptx->buffer_size % DMABUFFERSIZE )
  3454. ++num_needed;
  3455. if (num_needed <= num_free) {
  3456. info->xmit_cnt = ptx->buffer_size;
  3457. mgsl_load_tx_dma_buffer(info,ptx->buffer,ptx->buffer_size);
  3458. --info->tx_holding_count;
  3459. if ( ++info->get_tx_holding_index >= info->num_tx_holding_buffers)
  3460. info->get_tx_holding_index=0;
  3461. /* restart transmit timer */
  3462. mod_timer(&info->tx_timer, jiffies + msecs_to_jiffies(5000));
  3463. ret = 1;
  3464. }
  3465. }
  3466. return ret;
  3467. }
  3468. /*
  3469. * save_tx_buffer_request()
  3470. *
  3471. * attempt to store transmit frame request for later transmission
  3472. *
  3473. * Arguments:
  3474. *
  3475. * info pointer to device instance data
  3476. * Buffer pointer to buffer containing frame to load
  3477. * BufferSize size in bytes of frame in Buffer
  3478. *
  3479. * Return Value: 1 if able to store, 0 otherwise
  3480. */
  3481. static int save_tx_buffer_request(struct mgsl_struct *info,const char *Buffer, unsigned int BufferSize)
  3482. {
  3483. struct tx_holding_buffer *ptx;
  3484. if ( info->tx_holding_count >= info->num_tx_holding_buffers ) {
  3485. return 0; /* all buffers in use */
  3486. }
  3487. ptx = &info->tx_holding_buffers[info->put_tx_holding_index];
  3488. ptx->buffer_size = BufferSize;
  3489. memcpy( ptx->buffer, Buffer, BufferSize);
  3490. ++info->tx_holding_count;
  3491. if ( ++info->put_tx_holding_index >= info->num_tx_holding_buffers)
  3492. info->put_tx_holding_index=0;
  3493. return 1;
  3494. }
  3495. static int mgsl_claim_resources(struct mgsl_struct *info)
  3496. {
  3497. if (request_region(info->io_base,info->io_addr_size,"synclink") == NULL) {
  3498. printk( "%s(%d):I/O address conflict on device %s Addr=%08X\n",
  3499. __FILE__,__LINE__,info->device_name, info->io_base);
  3500. return -ENODEV;
  3501. }
  3502. info->io_addr_requested = 1;
  3503. if ( request_irq(info->irq_level,mgsl_interrupt,info->irq_flags,
  3504. info->device_name, info ) < 0 ) {
  3505. printk( "%s(%d):Cant request interrupt on device %s IRQ=%d\n",
  3506. __FILE__,__LINE__,info->device_name, info->irq_level );
  3507. goto errout;
  3508. }
  3509. info->irq_requested = 1;
  3510. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  3511. if (request_mem_region(info->phys_memory_base,0x40000,"synclink") == NULL) {
  3512. printk( "%s(%d):mem addr conflict device %s Addr=%08X\n",
  3513. __FILE__,__LINE__,info->device_name, info->phys_memory_base);
  3514. goto errout;
  3515. }
  3516. info->shared_mem_requested = 1;
  3517. if (request_mem_region(info->phys_lcr_base + info->lcr_offset,128,"synclink") == NULL) {
  3518. printk( "%s(%d):lcr mem addr conflict device %s Addr=%08X\n",
  3519. __FILE__,__LINE__,info->device_name, info->phys_lcr_base + info->lcr_offset);
  3520. goto errout;
  3521. }
  3522. info->lcr_mem_requested = 1;
  3523. info->memory_base = ioremap(info->phys_memory_base,0x40000);
  3524. if (!info->memory_base) {
  3525. printk( "%s(%d):Cant map shared memory on device %s MemAddr=%08X\n",
  3526. __FILE__,__LINE__,info->device_name, info->phys_memory_base );
  3527. goto errout;
  3528. }
  3529. if ( !mgsl_memory_test(info) ) {
  3530. printk( "%s(%d):Failed shared memory test %s MemAddr=%08X\n",
  3531. __FILE__,__LINE__,info->device_name, info->phys_memory_base );
  3532. goto errout;
  3533. }
  3534. info->lcr_base = ioremap(info->phys_lcr_base,PAGE_SIZE) + info->lcr_offset;
  3535. if (!info->lcr_base) {
  3536. printk( "%s(%d):Cant map LCR memory on device %s MemAddr=%08X\n",
  3537. __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
  3538. goto errout;
  3539. }
  3540. } else {
  3541. /* claim DMA channel */
  3542. if (request_dma(info->dma_level,info->device_name) < 0){
  3543. printk( "%s(%d):Cant request DMA channel on device %s DMA=%d\n",
  3544. __FILE__,__LINE__,info->device_name, info->dma_level );
  3545. mgsl_release_resources( info );
  3546. return -ENODEV;
  3547. }
  3548. info->dma_requested = 1;
  3549. /* ISA adapter uses bus master DMA */
  3550. set_dma_mode(info->dma_level,DMA_MODE_CASCADE);
  3551. enable_dma(info->dma_level);
  3552. }
  3553. if ( mgsl_allocate_dma_buffers(info) < 0 ) {
  3554. printk( "%s(%d):Cant allocate DMA buffers on device %s DMA=%d\n",
  3555. __FILE__,__LINE__,info->device_name, info->dma_level );
  3556. goto errout;
  3557. }
  3558. return 0;
  3559. errout:
  3560. mgsl_release_resources(info);
  3561. return -ENODEV;
  3562. } /* end of mgsl_claim_resources() */
  3563. static void mgsl_release_resources(struct mgsl_struct *info)
  3564. {
  3565. if ( debug_level >= DEBUG_LEVEL_INFO )
  3566. printk( "%s(%d):mgsl_release_resources(%s) entry\n",
  3567. __FILE__,__LINE__,info->device_name );
  3568. if ( info->irq_requested ) {
  3569. free_irq(info->irq_level, info);
  3570. info->irq_requested = 0;
  3571. }
  3572. if ( info->dma_requested ) {
  3573. disable_dma(info->dma_level);
  3574. free_dma(info->dma_level);
  3575. info->dma_requested = 0;
  3576. }
  3577. mgsl_free_dma_buffers(info);
  3578. mgsl_free_intermediate_rxbuffer_memory(info);
  3579. mgsl_free_intermediate_txbuffer_memory(info);
  3580. if ( info->io_addr_requested ) {
  3581. release_region(info->io_base,info->io_addr_size);
  3582. info->io_addr_requested = 0;
  3583. }
  3584. if ( info->shared_mem_requested ) {
  3585. release_mem_region(info->phys_memory_base,0x40000);
  3586. info->shared_mem_requested = 0;
  3587. }
  3588. if ( info->lcr_mem_requested ) {
  3589. release_mem_region(info->phys_lcr_base + info->lcr_offset,128);
  3590. info->lcr_mem_requested = 0;
  3591. }
  3592. if (info->memory_base){
  3593. iounmap(info->memory_base);
  3594. info->memory_base = NULL;
  3595. }
  3596. if (info->lcr_base){
  3597. iounmap(info->lcr_base - info->lcr_offset);
  3598. info->lcr_base = NULL;
  3599. }
  3600. if ( debug_level >= DEBUG_LEVEL_INFO )
  3601. printk( "%s(%d):mgsl_release_resources(%s) exit\n",
  3602. __FILE__,__LINE__,info->device_name );
  3603. } /* end of mgsl_release_resources() */
  3604. /* mgsl_add_device()
  3605. *
  3606. * Add the specified device instance data structure to the
  3607. * global linked list of devices and increment the device count.
  3608. *
  3609. * Arguments: info pointer to device instance data
  3610. * Return Value: None
  3611. */
  3612. static void mgsl_add_device( struct mgsl_struct *info )
  3613. {
  3614. info->next_device = NULL;
  3615. info->line = mgsl_device_count;
  3616. sprintf(info->device_name,"ttySL%d",info->line);
  3617. if (info->line < MAX_TOTAL_DEVICES) {
  3618. if (maxframe[info->line])
  3619. info->max_frame_size = maxframe[info->line];
  3620. info->dosyncppp = dosyncppp[info->line];
  3621. if (txdmabufs[info->line]) {
  3622. info->num_tx_dma_buffers = txdmabufs[info->line];
  3623. if (info->num_tx_dma_buffers < 1)
  3624. info->num_tx_dma_buffers = 1;
  3625. }
  3626. if (txholdbufs[info->line]) {
  3627. info->num_tx_holding_buffers = txholdbufs[info->line];
  3628. if (info->num_tx_holding_buffers < 1)
  3629. info->num_tx_holding_buffers = 1;
  3630. else if (info->num_tx_holding_buffers > MAX_TX_HOLDING_BUFFERS)
  3631. info->num_tx_holding_buffers = MAX_TX_HOLDING_BUFFERS;
  3632. }
  3633. }
  3634. mgsl_device_count++;
  3635. if ( !mgsl_device_list )
  3636. mgsl_device_list = info;
  3637. else {
  3638. struct mgsl_struct *current_dev = mgsl_device_list;
  3639. while( current_dev->next_device )
  3640. current_dev = current_dev->next_device;
  3641. current_dev->next_device = info;
  3642. }
  3643. if ( info->max_frame_size < 4096 )
  3644. info->max_frame_size = 4096;
  3645. else if ( info->max_frame_size > 65535 )
  3646. info->max_frame_size = 65535;
  3647. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  3648. printk( "SyncLink PCI v%d %s: IO=%04X IRQ=%d Mem=%08X,%08X MaxFrameSize=%u\n",
  3649. info->hw_version + 1, info->device_name, info->io_base, info->irq_level,
  3650. info->phys_memory_base, info->phys_lcr_base,
  3651. info->max_frame_size );
  3652. } else {
  3653. printk( "SyncLink ISA %s: IO=%04X IRQ=%d DMA=%d MaxFrameSize=%u\n",
  3654. info->device_name, info->io_base, info->irq_level, info->dma_level,
  3655. info->max_frame_size );
  3656. }
  3657. #ifdef CONFIG_HDLC
  3658. hdlcdev_init(info);
  3659. #endif
  3660. } /* end of mgsl_add_device() */
  3661. /* mgsl_allocate_device()
  3662. *
  3663. * Allocate and initialize a device instance structure
  3664. *
  3665. * Arguments: none
  3666. * Return Value: pointer to mgsl_struct if success, otherwise NULL
  3667. */
  3668. static struct mgsl_struct* mgsl_allocate_device(void)
  3669. {
  3670. struct mgsl_struct *info;
  3671. info = (struct mgsl_struct *)kmalloc(sizeof(struct mgsl_struct),
  3672. GFP_KERNEL);
  3673. if (!info) {
  3674. printk("Error can't allocate device instance data\n");
  3675. } else {
  3676. memset(info, 0, sizeof(struct mgsl_struct));
  3677. info->magic = MGSL_MAGIC;
  3678. INIT_WORK(&info->task, mgsl_bh_handler, info);
  3679. info->max_frame_size = 4096;
  3680. info->close_delay = 5*HZ/10;
  3681. info->closing_wait = 30*HZ;
  3682. init_waitqueue_head(&info->open_wait);
  3683. init_waitqueue_head(&info->close_wait);
  3684. init_waitqueue_head(&info->status_event_wait_q);
  3685. init_waitqueue_head(&info->event_wait_q);
  3686. spin_lock_init(&info->irq_spinlock);
  3687. spin_lock_init(&info->netlock);
  3688. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  3689. info->idle_mode = HDLC_TXIDLE_FLAGS;
  3690. info->num_tx_dma_buffers = 1;
  3691. info->num_tx_holding_buffers = 0;
  3692. }
  3693. return info;
  3694. } /* end of mgsl_allocate_device()*/
  3695. static struct tty_operations mgsl_ops = {
  3696. .open = mgsl_open,
  3697. .close = mgsl_close,
  3698. .write = mgsl_write,
  3699. .put_char = mgsl_put_char,
  3700. .flush_chars = mgsl_flush_chars,
  3701. .write_room = mgsl_write_room,
  3702. .chars_in_buffer = mgsl_chars_in_buffer,
  3703. .flush_buffer = mgsl_flush_buffer,
  3704. .ioctl = mgsl_ioctl,
  3705. .throttle = mgsl_throttle,
  3706. .unthrottle = mgsl_unthrottle,
  3707. .send_xchar = mgsl_send_xchar,
  3708. .break_ctl = mgsl_break,
  3709. .wait_until_sent = mgsl_wait_until_sent,
  3710. .read_proc = mgsl_read_proc,
  3711. .set_termios = mgsl_set_termios,
  3712. .stop = mgsl_stop,
  3713. .start = mgsl_start,
  3714. .hangup = mgsl_hangup,
  3715. .tiocmget = tiocmget,
  3716. .tiocmset = tiocmset,
  3717. };
  3718. /*
  3719. * perform tty device initialization
  3720. */
  3721. static int mgsl_init_tty(void)
  3722. {
  3723. int rc;
  3724. serial_driver = alloc_tty_driver(128);
  3725. if (!serial_driver)
  3726. return -ENOMEM;
  3727. serial_driver->owner = THIS_MODULE;
  3728. serial_driver->driver_name = "synclink";
  3729. serial_driver->name = "ttySL";
  3730. serial_driver->major = ttymajor;
  3731. serial_driver->minor_start = 64;
  3732. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  3733. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  3734. serial_driver->init_termios = tty_std_termios;
  3735. serial_driver->init_termios.c_cflag =
  3736. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  3737. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  3738. tty_set_operations(serial_driver, &mgsl_ops);
  3739. if ((rc = tty_register_driver(serial_driver)) < 0) {
  3740. printk("%s(%d):Couldn't register serial driver\n",
  3741. __FILE__,__LINE__);
  3742. put_tty_driver(serial_driver);
  3743. serial_driver = NULL;
  3744. return rc;
  3745. }
  3746. printk("%s %s, tty major#%d\n",
  3747. driver_name, driver_version,
  3748. serial_driver->major);
  3749. return 0;
  3750. }
  3751. /* enumerate user specified ISA adapters
  3752. */
  3753. static void mgsl_enum_isa_devices(void)
  3754. {
  3755. struct mgsl_struct *info;
  3756. int i;
  3757. /* Check for user specified ISA devices */
  3758. for (i=0 ;(i < MAX_ISA_DEVICES) && io[i] && irq[i]; i++){
  3759. if ( debug_level >= DEBUG_LEVEL_INFO )
  3760. printk("ISA device specified io=%04X,irq=%d,dma=%d\n",
  3761. io[i], irq[i], dma[i] );
  3762. info = mgsl_allocate_device();
  3763. if ( !info ) {
  3764. /* error allocating device instance data */
  3765. if ( debug_level >= DEBUG_LEVEL_ERROR )
  3766. printk( "can't allocate device instance data.\n");
  3767. continue;
  3768. }
  3769. /* Copy user configuration info to device instance data */
  3770. info->io_base = (unsigned int)io[i];
  3771. info->irq_level = (unsigned int)irq[i];
  3772. info->irq_level = irq_canonicalize(info->irq_level);
  3773. info->dma_level = (unsigned int)dma[i];
  3774. info->bus_type = MGSL_BUS_TYPE_ISA;
  3775. info->io_addr_size = 16;
  3776. info->irq_flags = 0;
  3777. mgsl_add_device( info );
  3778. }
  3779. }
  3780. static void synclink_cleanup(void)
  3781. {
  3782. int rc;
  3783. struct mgsl_struct *info;
  3784. struct mgsl_struct *tmp;
  3785. printk("Unloading %s: %s\n", driver_name, driver_version);
  3786. if (serial_driver) {
  3787. if ((rc = tty_unregister_driver(serial_driver)))
  3788. printk("%s(%d) failed to unregister tty driver err=%d\n",
  3789. __FILE__,__LINE__,rc);
  3790. put_tty_driver(serial_driver);
  3791. }
  3792. info = mgsl_device_list;
  3793. while(info) {
  3794. #ifdef CONFIG_HDLC
  3795. hdlcdev_exit(info);
  3796. #endif
  3797. mgsl_release_resources(info);
  3798. tmp = info;
  3799. info = info->next_device;
  3800. kfree(tmp);
  3801. }
  3802. if (tmp_buf) {
  3803. free_page((unsigned long) tmp_buf);
  3804. tmp_buf = NULL;
  3805. }
  3806. if (pci_registered)
  3807. pci_unregister_driver(&synclink_pci_driver);
  3808. }
  3809. static int __init synclink_init(void)
  3810. {
  3811. int rc;
  3812. if (break_on_load) {
  3813. mgsl_get_text_ptr();
  3814. BREAKPOINT();
  3815. }
  3816. printk("%s %s\n", driver_name, driver_version);
  3817. mgsl_enum_isa_devices();
  3818. if ((rc = pci_register_driver(&synclink_pci_driver)) < 0)
  3819. printk("%s:failed to register PCI driver, error=%d\n",__FILE__,rc);
  3820. else
  3821. pci_registered = 1;
  3822. if ((rc = mgsl_init_tty()) < 0)
  3823. goto error;
  3824. return 0;
  3825. error:
  3826. synclink_cleanup();
  3827. return rc;
  3828. }
  3829. static void __exit synclink_exit(void)
  3830. {
  3831. synclink_cleanup();
  3832. }
  3833. module_init(synclink_init);
  3834. module_exit(synclink_exit);
  3835. /*
  3836. * usc_RTCmd()
  3837. *
  3838. * Issue a USC Receive/Transmit command to the
  3839. * Channel Command/Address Register (CCAR).
  3840. *
  3841. * Notes:
  3842. *
  3843. * The command is encoded in the most significant 5 bits <15..11>
  3844. * of the CCAR value. Bits <10..7> of the CCAR must be preserved
  3845. * and Bits <6..0> must be written as zeros.
  3846. *
  3847. * Arguments:
  3848. *
  3849. * info pointer to device information structure
  3850. * Cmd command mask (use symbolic macros)
  3851. *
  3852. * Return Value:
  3853. *
  3854. * None
  3855. */
  3856. static void usc_RTCmd( struct mgsl_struct *info, u16 Cmd )
  3857. {
  3858. /* output command to CCAR in bits <15..11> */
  3859. /* preserve bits <10..7>, bits <6..0> must be zero */
  3860. outw( Cmd + info->loopback_bits, info->io_base + CCAR );
  3861. /* Read to flush write to CCAR */
  3862. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  3863. inw( info->io_base + CCAR );
  3864. } /* end of usc_RTCmd() */
  3865. /*
  3866. * usc_DmaCmd()
  3867. *
  3868. * Issue a DMA command to the DMA Command/Address Register (DCAR).
  3869. *
  3870. * Arguments:
  3871. *
  3872. * info pointer to device information structure
  3873. * Cmd DMA command mask (usc_DmaCmd_XX Macros)
  3874. *
  3875. * Return Value:
  3876. *
  3877. * None
  3878. */
  3879. static void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd )
  3880. {
  3881. /* write command mask to DCAR */
  3882. outw( Cmd + info->mbre_bit, info->io_base );
  3883. /* Read to flush write to DCAR */
  3884. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  3885. inw( info->io_base );
  3886. } /* end of usc_DmaCmd() */
  3887. /*
  3888. * usc_OutDmaReg()
  3889. *
  3890. * Write a 16-bit value to a USC DMA register
  3891. *
  3892. * Arguments:
  3893. *
  3894. * info pointer to device info structure
  3895. * RegAddr register address (number) for write
  3896. * RegValue 16-bit value to write to register
  3897. *
  3898. * Return Value:
  3899. *
  3900. * None
  3901. *
  3902. */
  3903. static void usc_OutDmaReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
  3904. {
  3905. /* Note: The DCAR is located at the adapter base address */
  3906. /* Note: must preserve state of BIT8 in DCAR */
  3907. outw( RegAddr + info->mbre_bit, info->io_base );
  3908. outw( RegValue, info->io_base );
  3909. /* Read to flush write to DCAR */
  3910. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  3911. inw( info->io_base );
  3912. } /* end of usc_OutDmaReg() */
  3913. /*
  3914. * usc_InDmaReg()
  3915. *
  3916. * Read a 16-bit value from a DMA register
  3917. *
  3918. * Arguments:
  3919. *
  3920. * info pointer to device info structure
  3921. * RegAddr register address (number) to read from
  3922. *
  3923. * Return Value:
  3924. *
  3925. * The 16-bit value read from register
  3926. *
  3927. */
  3928. static u16 usc_InDmaReg( struct mgsl_struct *info, u16 RegAddr )
  3929. {
  3930. /* Note: The DCAR is located at the adapter base address */
  3931. /* Note: must preserve state of BIT8 in DCAR */
  3932. outw( RegAddr + info->mbre_bit, info->io_base );
  3933. return inw( info->io_base );
  3934. } /* end of usc_InDmaReg() */
  3935. /*
  3936. *
  3937. * usc_OutReg()
  3938. *
  3939. * Write a 16-bit value to a USC serial channel register
  3940. *
  3941. * Arguments:
  3942. *
  3943. * info pointer to device info structure
  3944. * RegAddr register address (number) to write to
  3945. * RegValue 16-bit value to write to register
  3946. *
  3947. * Return Value:
  3948. *
  3949. * None
  3950. *
  3951. */
  3952. static void usc_OutReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
  3953. {
  3954. outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
  3955. outw( RegValue, info->io_base + CCAR );
  3956. /* Read to flush write to CCAR */
  3957. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  3958. inw( info->io_base + CCAR );
  3959. } /* end of usc_OutReg() */
  3960. /*
  3961. * usc_InReg()
  3962. *
  3963. * Reads a 16-bit value from a USC serial channel register
  3964. *
  3965. * Arguments:
  3966. *
  3967. * info pointer to device extension
  3968. * RegAddr register address (number) to read from
  3969. *
  3970. * Return Value:
  3971. *
  3972. * 16-bit value read from register
  3973. */
  3974. static u16 usc_InReg( struct mgsl_struct *info, u16 RegAddr )
  3975. {
  3976. outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
  3977. return inw( info->io_base + CCAR );
  3978. } /* end of usc_InReg() */
  3979. /* usc_set_sdlc_mode()
  3980. *
  3981. * Set up the adapter for SDLC DMA communications.
  3982. *
  3983. * Arguments: info pointer to device instance data
  3984. * Return Value: NONE
  3985. */
  3986. static void usc_set_sdlc_mode( struct mgsl_struct *info )
  3987. {
  3988. u16 RegValue;
  3989. int PreSL1660;
  3990. /*
  3991. * determine if the IUSC on the adapter is pre-SL1660. If
  3992. * not, take advantage of the UnderWait feature of more
  3993. * modern chips. If an underrun occurs and this bit is set,
  3994. * the transmitter will idle the programmed idle pattern
  3995. * until the driver has time to service the underrun. Otherwise,
  3996. * the dma controller may get the cycles previously requested
  3997. * and begin transmitting queued tx data.
  3998. */
  3999. usc_OutReg(info,TMCR,0x1f);
  4000. RegValue=usc_InReg(info,TMDR);
  4001. if ( RegValue == IUSC_PRE_SL1660 )
  4002. PreSL1660 = 1;
  4003. else
  4004. PreSL1660 = 0;
  4005. if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
  4006. {
  4007. /*
  4008. ** Channel Mode Register (CMR)
  4009. **
  4010. ** <15..14> 10 Tx Sub Modes, Send Flag on Underrun
  4011. ** <13> 0 0 = Transmit Disabled (initially)
  4012. ** <12> 0 1 = Consecutive Idles share common 0
  4013. ** <11..8> 1110 Transmitter Mode = HDLC/SDLC Loop
  4014. ** <7..4> 0000 Rx Sub Modes, addr/ctrl field handling
  4015. ** <3..0> 0110 Receiver Mode = HDLC/SDLC
  4016. **
  4017. ** 1000 1110 0000 0110 = 0x8e06
  4018. */
  4019. RegValue = 0x8e06;
  4020. /*--------------------------------------------------
  4021. * ignore user options for UnderRun Actions and
  4022. * preambles
  4023. *--------------------------------------------------*/
  4024. }
  4025. else
  4026. {
  4027. /* Channel mode Register (CMR)
  4028. *
  4029. * <15..14> 00 Tx Sub modes, Underrun Action
  4030. * <13> 0 1 = Send Preamble before opening flag
  4031. * <12> 0 1 = Consecutive Idles share common 0
  4032. * <11..8> 0110 Transmitter mode = HDLC/SDLC
  4033. * <7..4> 0000 Rx Sub modes, addr/ctrl field handling
  4034. * <3..0> 0110 Receiver mode = HDLC/SDLC
  4035. *
  4036. * 0000 0110 0000 0110 = 0x0606
  4037. */
  4038. if (info->params.mode == MGSL_MODE_RAW) {
  4039. RegValue = 0x0001; /* Set Receive mode = external sync */
  4040. usc_OutReg( info, IOCR, /* Set IOCR DCD is RxSync Detect Input */
  4041. (unsigned short)((usc_InReg(info, IOCR) & ~(BIT13|BIT12)) | BIT12));
  4042. /*
  4043. * TxSubMode:
  4044. * CMR <15> 0 Don't send CRC on Tx Underrun
  4045. * CMR <14> x undefined
  4046. * CMR <13> 0 Send preamble before openning sync
  4047. * CMR <12> 0 Send 8-bit syncs, 1=send Syncs per TxLength
  4048. *
  4049. * TxMode:
  4050. * CMR <11-8) 0100 MonoSync
  4051. *
  4052. * 0x00 0100 xxxx xxxx 04xx
  4053. */
  4054. RegValue |= 0x0400;
  4055. }
  4056. else {
  4057. RegValue = 0x0606;
  4058. if ( info->params.flags & HDLC_FLAG_UNDERRUN_ABORT15 )
  4059. RegValue |= BIT14;
  4060. else if ( info->params.flags & HDLC_FLAG_UNDERRUN_FLAG )
  4061. RegValue |= BIT15;
  4062. else if ( info->params.flags & HDLC_FLAG_UNDERRUN_CRC )
  4063. RegValue |= BIT15 + BIT14;
  4064. }
  4065. if ( info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE )
  4066. RegValue |= BIT13;
  4067. }
  4068. if ( info->params.mode == MGSL_MODE_HDLC &&
  4069. (info->params.flags & HDLC_FLAG_SHARE_ZERO) )
  4070. RegValue |= BIT12;
  4071. if ( info->params.addr_filter != 0xff )
  4072. {
  4073. /* set up receive address filtering */
  4074. usc_OutReg( info, RSR, info->params.addr_filter );
  4075. RegValue |= BIT4;
  4076. }
  4077. usc_OutReg( info, CMR, RegValue );
  4078. info->cmr_value = RegValue;
  4079. /* Receiver mode Register (RMR)
  4080. *
  4081. * <15..13> 000 encoding
  4082. * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
  4083. * <10> 1 1 = Set CRC to all 1s (use for SDLC/HDLC)
  4084. * <9> 0 1 = Include Receive chars in CRC
  4085. * <8> 1 1 = Use Abort/PE bit as abort indicator
  4086. * <7..6> 00 Even parity
  4087. * <5> 0 parity disabled
  4088. * <4..2> 000 Receive Char Length = 8 bits
  4089. * <1..0> 00 Disable Receiver
  4090. *
  4091. * 0000 0101 0000 0000 = 0x0500
  4092. */
  4093. RegValue = 0x0500;
  4094. switch ( info->params.encoding ) {
  4095. case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
  4096. case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
  4097. case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break;
  4098. case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
  4099. case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break;
  4100. case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break;
  4101. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break;
  4102. }
  4103. if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
  4104. RegValue |= BIT9;
  4105. else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
  4106. RegValue |= ( BIT12 | BIT10 | BIT9 );
  4107. usc_OutReg( info, RMR, RegValue );
  4108. /* Set the Receive count Limit Register (RCLR) to 0xffff. */
  4109. /* When an opening flag of an SDLC frame is recognized the */
  4110. /* Receive Character count (RCC) is loaded with the value in */
  4111. /* RCLR. The RCC is decremented for each received byte. The */
  4112. /* value of RCC is stored after the closing flag of the frame */
  4113. /* allowing the frame size to be computed. */
  4114. usc_OutReg( info, RCLR, RCLRVALUE );
  4115. usc_RCmd( info, RCmd_SelectRicrdma_level );
  4116. /* Receive Interrupt Control Register (RICR)
  4117. *
  4118. * <15..8> ? RxFIFO DMA Request Level
  4119. * <7> 0 Exited Hunt IA (Interrupt Arm)
  4120. * <6> 0 Idle Received IA
  4121. * <5> 0 Break/Abort IA
  4122. * <4> 0 Rx Bound IA
  4123. * <3> 1 Queued status reflects oldest 2 bytes in FIFO
  4124. * <2> 0 Abort/PE IA
  4125. * <1> 1 Rx Overrun IA
  4126. * <0> 0 Select TC0 value for readback
  4127. *
  4128. * 0000 0000 0000 1000 = 0x000a
  4129. */
  4130. /* Carry over the Exit Hunt and Idle Received bits */
  4131. /* in case they have been armed by usc_ArmEvents. */
  4132. RegValue = usc_InReg( info, RICR ) & 0xc0;
  4133. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  4134. usc_OutReg( info, RICR, (u16)(0x030a | RegValue) );
  4135. else
  4136. usc_OutReg( info, RICR, (u16)(0x140a | RegValue) );
  4137. /* Unlatch all Rx status bits and clear Rx status IRQ Pending */
  4138. usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
  4139. usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
  4140. /* Transmit mode Register (TMR)
  4141. *
  4142. * <15..13> 000 encoding
  4143. * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
  4144. * <10> 1 1 = Start CRC as all 1s (use for SDLC/HDLC)
  4145. * <9> 0 1 = Tx CRC Enabled
  4146. * <8> 0 1 = Append CRC to end of transmit frame
  4147. * <7..6> 00 Transmit parity Even
  4148. * <5> 0 Transmit parity Disabled
  4149. * <4..2> 000 Tx Char Length = 8 bits
  4150. * <1..0> 00 Disable Transmitter
  4151. *
  4152. * 0000 0100 0000 0000 = 0x0400
  4153. */
  4154. RegValue = 0x0400;
  4155. switch ( info->params.encoding ) {
  4156. case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
  4157. case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
  4158. case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break;
  4159. case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
  4160. case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break;
  4161. case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break;
  4162. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break;
  4163. }
  4164. if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
  4165. RegValue |= BIT9 + BIT8;
  4166. else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
  4167. RegValue |= ( BIT12 | BIT10 | BIT9 | BIT8);
  4168. usc_OutReg( info, TMR, RegValue );
  4169. usc_set_txidle( info );
  4170. usc_TCmd( info, TCmd_SelectTicrdma_level );
  4171. /* Transmit Interrupt Control Register (TICR)
  4172. *
  4173. * <15..8> ? Transmit FIFO DMA Level
  4174. * <7> 0 Present IA (Interrupt Arm)
  4175. * <6> 0 Idle Sent IA
  4176. * <5> 1 Abort Sent IA
  4177. * <4> 1 EOF/EOM Sent IA
  4178. * <3> 0 CRC Sent IA
  4179. * <2> 1 1 = Wait for SW Trigger to Start Frame
  4180. * <1> 1 Tx Underrun IA
  4181. * <0> 0 TC0 constant on read back
  4182. *
  4183. * 0000 0000 0011 0110 = 0x0036
  4184. */
  4185. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  4186. usc_OutReg( info, TICR, 0x0736 );
  4187. else
  4188. usc_OutReg( info, TICR, 0x1436 );
  4189. usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
  4190. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
  4191. /*
  4192. ** Transmit Command/Status Register (TCSR)
  4193. **
  4194. ** <15..12> 0000 TCmd
  4195. ** <11> 0/1 UnderWait
  4196. ** <10..08> 000 TxIdle
  4197. ** <7> x PreSent
  4198. ** <6> x IdleSent
  4199. ** <5> x AbortSent
  4200. ** <4> x EOF/EOM Sent
  4201. ** <3> x CRC Sent
  4202. ** <2> x All Sent
  4203. ** <1> x TxUnder
  4204. ** <0> x TxEmpty
  4205. **
  4206. ** 0000 0000 0000 0000 = 0x0000
  4207. */
  4208. info->tcsr_value = 0;
  4209. if ( !PreSL1660 )
  4210. info->tcsr_value |= TCSR_UNDERWAIT;
  4211. usc_OutReg( info, TCSR, info->tcsr_value );
  4212. /* Clock mode Control Register (CMCR)
  4213. *
  4214. * <15..14> 00 counter 1 Source = Disabled
  4215. * <13..12> 00 counter 0 Source = Disabled
  4216. * <11..10> 11 BRG1 Input is TxC Pin
  4217. * <9..8> 11 BRG0 Input is TxC Pin
  4218. * <7..6> 01 DPLL Input is BRG1 Output
  4219. * <5..3> XXX TxCLK comes from Port 0
  4220. * <2..0> XXX RxCLK comes from Port 1
  4221. *
  4222. * 0000 1111 0111 0111 = 0x0f77
  4223. */
  4224. RegValue = 0x0f40;
  4225. if ( info->params.flags & HDLC_FLAG_RXC_DPLL )
  4226. RegValue |= 0x0003; /* RxCLK from DPLL */
  4227. else if ( info->params.flags & HDLC_FLAG_RXC_BRG )
  4228. RegValue |= 0x0004; /* RxCLK from BRG0 */
  4229. else if ( info->params.flags & HDLC_FLAG_RXC_TXCPIN)
  4230. RegValue |= 0x0006; /* RxCLK from TXC Input */
  4231. else
  4232. RegValue |= 0x0007; /* RxCLK from Port1 */
  4233. if ( info->params.flags & HDLC_FLAG_TXC_DPLL )
  4234. RegValue |= 0x0018; /* TxCLK from DPLL */
  4235. else if ( info->params.flags & HDLC_FLAG_TXC_BRG )
  4236. RegValue |= 0x0020; /* TxCLK from BRG0 */
  4237. else if ( info->params.flags & HDLC_FLAG_TXC_RXCPIN)
  4238. RegValue |= 0x0038; /* RxCLK from TXC Input */
  4239. else
  4240. RegValue |= 0x0030; /* TxCLK from Port0 */
  4241. usc_OutReg( info, CMCR, RegValue );
  4242. /* Hardware Configuration Register (HCR)
  4243. *
  4244. * <15..14> 00 CTR0 Divisor:00=32,01=16,10=8,11=4
  4245. * <13> 0 CTR1DSel:0=CTR0Div determines CTR0Div
  4246. * <12> 0 CVOK:0=report code violation in biphase
  4247. * <11..10> 00 DPLL Divisor:00=32,01=16,10=8,11=4
  4248. * <9..8> XX DPLL mode:00=disable,01=NRZ,10=Biphase,11=Biphase Level
  4249. * <7..6> 00 reserved
  4250. * <5> 0 BRG1 mode:0=continuous,1=single cycle
  4251. * <4> X BRG1 Enable
  4252. * <3..2> 00 reserved
  4253. * <1> 0 BRG0 mode:0=continuous,1=single cycle
  4254. * <0> 0 BRG0 Enable
  4255. */
  4256. RegValue = 0x0000;
  4257. if ( info->params.flags & (HDLC_FLAG_RXC_DPLL + HDLC_FLAG_TXC_DPLL) ) {
  4258. u32 XtalSpeed;
  4259. u32 DpllDivisor;
  4260. u16 Tc;
  4261. /* DPLL is enabled. Use BRG1 to provide continuous reference clock */
  4262. /* for DPLL. DPLL mode in HCR is dependent on the encoding used. */
  4263. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  4264. XtalSpeed = 11059200;
  4265. else
  4266. XtalSpeed = 14745600;
  4267. if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
  4268. DpllDivisor = 16;
  4269. RegValue |= BIT10;
  4270. }
  4271. else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
  4272. DpllDivisor = 8;
  4273. RegValue |= BIT11;
  4274. }
  4275. else
  4276. DpllDivisor = 32;
  4277. /* Tc = (Xtal/Speed) - 1 */
  4278. /* If twice the remainder of (Xtal/Speed) is greater than Speed */
  4279. /* then rounding up gives a more precise time constant. Instead */
  4280. /* of rounding up and then subtracting 1 we just don't subtract */
  4281. /* the one in this case. */
  4282. /*--------------------------------------------------
  4283. * ejz: for DPLL mode, application should use the
  4284. * same clock speed as the partner system, even
  4285. * though clocking is derived from the input RxData.
  4286. * In case the user uses a 0 for the clock speed,
  4287. * default to 0xffffffff and don't try to divide by
  4288. * zero
  4289. *--------------------------------------------------*/
  4290. if ( info->params.clock_speed )
  4291. {
  4292. Tc = (u16)((XtalSpeed/DpllDivisor)/info->params.clock_speed);
  4293. if ( !((((XtalSpeed/DpllDivisor) % info->params.clock_speed) * 2)
  4294. / info->params.clock_speed) )
  4295. Tc--;
  4296. }
  4297. else
  4298. Tc = -1;
  4299. /* Write 16-bit Time Constant for BRG1 */
  4300. usc_OutReg( info, TC1R, Tc );
  4301. RegValue |= BIT4; /* enable BRG1 */
  4302. switch ( info->params.encoding ) {
  4303. case HDLC_ENCODING_NRZ:
  4304. case HDLC_ENCODING_NRZB:
  4305. case HDLC_ENCODING_NRZI_MARK:
  4306. case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT8; break;
  4307. case HDLC_ENCODING_BIPHASE_MARK:
  4308. case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT9; break;
  4309. case HDLC_ENCODING_BIPHASE_LEVEL:
  4310. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT9 + BIT8; break;
  4311. }
  4312. }
  4313. usc_OutReg( info, HCR, RegValue );
  4314. /* Channel Control/status Register (CCSR)
  4315. *
  4316. * <15> X RCC FIFO Overflow status (RO)
  4317. * <14> X RCC FIFO Not Empty status (RO)
  4318. * <13> 0 1 = Clear RCC FIFO (WO)
  4319. * <12> X DPLL Sync (RW)
  4320. * <11> X DPLL 2 Missed Clocks status (RO)
  4321. * <10> X DPLL 1 Missed Clock status (RO)
  4322. * <9..8> 00 DPLL Resync on rising and falling edges (RW)
  4323. * <7> X SDLC Loop On status (RO)
  4324. * <6> X SDLC Loop Send status (RO)
  4325. * <5> 1 Bypass counters for TxClk and RxClk (RW)
  4326. * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
  4327. * <1..0> 00 reserved
  4328. *
  4329. * 0000 0000 0010 0000 = 0x0020
  4330. */
  4331. usc_OutReg( info, CCSR, 0x1020 );
  4332. if ( info->params.flags & HDLC_FLAG_AUTO_CTS ) {
  4333. usc_OutReg( info, SICR,
  4334. (u16)(usc_InReg(info,SICR) | SICR_CTS_INACTIVE) );
  4335. }
  4336. /* enable Master Interrupt Enable bit (MIE) */
  4337. usc_EnableMasterIrqBit( info );
  4338. usc_ClearIrqPendingBits( info, RECEIVE_STATUS + RECEIVE_DATA +
  4339. TRANSMIT_STATUS + TRANSMIT_DATA + MISC);
  4340. /* arm RCC underflow interrupt */
  4341. usc_OutReg(info, SICR, (u16)(usc_InReg(info,SICR) | BIT3));
  4342. usc_EnableInterrupts(info, MISC);
  4343. info->mbre_bit = 0;
  4344. outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
  4345. usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
  4346. info->mbre_bit = BIT8;
  4347. outw( BIT8, info->io_base ); /* set Master Bus Enable (DCAR) */
  4348. if (info->bus_type == MGSL_BUS_TYPE_ISA) {
  4349. /* Enable DMAEN (Port 7, Bit 14) */
  4350. /* This connects the DMA request signal to the ISA bus */
  4351. usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) & ~BIT14));
  4352. }
  4353. /* DMA Control Register (DCR)
  4354. *
  4355. * <15..14> 10 Priority mode = Alternating Tx/Rx
  4356. * 01 Rx has priority
  4357. * 00 Tx has priority
  4358. *
  4359. * <13> 1 Enable Priority Preempt per DCR<15..14>
  4360. * (WARNING DCR<11..10> must be 00 when this is 1)
  4361. * 0 Choose activate channel per DCR<11..10>
  4362. *
  4363. * <12> 0 Little Endian for Array/List
  4364. * <11..10> 00 Both Channels can use each bus grant
  4365. * <9..6> 0000 reserved
  4366. * <5> 0 7 CLK - Minimum Bus Re-request Interval
  4367. * <4> 0 1 = drive D/C and S/D pins
  4368. * <3> 1 1 = Add one wait state to all DMA cycles.
  4369. * <2> 0 1 = Strobe /UAS on every transfer.
  4370. * <1..0> 11 Addr incrementing only affects LS24 bits
  4371. *
  4372. * 0110 0000 0000 1011 = 0x600b
  4373. */
  4374. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  4375. /* PCI adapter does not need DMA wait state */
  4376. usc_OutDmaReg( info, DCR, 0xa00b );
  4377. }
  4378. else
  4379. usc_OutDmaReg( info, DCR, 0x800b );
  4380. /* Receive DMA mode Register (RDMR)
  4381. *
  4382. * <15..14> 11 DMA mode = Linked List Buffer mode
  4383. * <13> 1 RSBinA/L = store Rx status Block in Arrary/List entry
  4384. * <12> 1 Clear count of List Entry after fetching
  4385. * <11..10> 00 Address mode = Increment
  4386. * <9> 1 Terminate Buffer on RxBound
  4387. * <8> 0 Bus Width = 16bits
  4388. * <7..0> ? status Bits (write as 0s)
  4389. *
  4390. * 1111 0010 0000 0000 = 0xf200
  4391. */
  4392. usc_OutDmaReg( info, RDMR, 0xf200 );
  4393. /* Transmit DMA mode Register (TDMR)
  4394. *
  4395. * <15..14> 11 DMA mode = Linked List Buffer mode
  4396. * <13> 1 TCBinA/L = fetch Tx Control Block from List entry
  4397. * <12> 1 Clear count of List Entry after fetching
  4398. * <11..10> 00 Address mode = Increment
  4399. * <9> 1 Terminate Buffer on end of frame
  4400. * <8> 0 Bus Width = 16bits
  4401. * <7..0> ? status Bits (Read Only so write as 0)
  4402. *
  4403. * 1111 0010 0000 0000 = 0xf200
  4404. */
  4405. usc_OutDmaReg( info, TDMR, 0xf200 );
  4406. /* DMA Interrupt Control Register (DICR)
  4407. *
  4408. * <15> 1 DMA Interrupt Enable
  4409. * <14> 0 1 = Disable IEO from USC
  4410. * <13> 0 1 = Don't provide vector during IntAck
  4411. * <12> 1 1 = Include status in Vector
  4412. * <10..2> 0 reserved, Must be 0s
  4413. * <1> 0 1 = Rx DMA Interrupt Enabled
  4414. * <0> 0 1 = Tx DMA Interrupt Enabled
  4415. *
  4416. * 1001 0000 0000 0000 = 0x9000
  4417. */
  4418. usc_OutDmaReg( info, DICR, 0x9000 );
  4419. usc_InDmaReg( info, RDMR ); /* clear pending receive DMA IRQ bits */
  4420. usc_InDmaReg( info, TDMR ); /* clear pending transmit DMA IRQ bits */
  4421. usc_OutDmaReg( info, CDIR, 0x0303 ); /* clear IUS and Pending for Tx and Rx */
  4422. /* Channel Control Register (CCR)
  4423. *
  4424. * <15..14> 10 Use 32-bit Tx Control Blocks (TCBs)
  4425. * <13> 0 Trigger Tx on SW Command Disabled
  4426. * <12> 0 Flag Preamble Disabled
  4427. * <11..10> 00 Preamble Length
  4428. * <9..8> 00 Preamble Pattern
  4429. * <7..6> 10 Use 32-bit Rx status Blocks (RSBs)
  4430. * <5> 0 Trigger Rx on SW Command Disabled
  4431. * <4..0> 0 reserved
  4432. *
  4433. * 1000 0000 1000 0000 = 0x8080
  4434. */
  4435. RegValue = 0x8080;
  4436. switch ( info->params.preamble_length ) {
  4437. case HDLC_PREAMBLE_LENGTH_16BITS: RegValue |= BIT10; break;
  4438. case HDLC_PREAMBLE_LENGTH_32BITS: RegValue |= BIT11; break;
  4439. case HDLC_PREAMBLE_LENGTH_64BITS: RegValue |= BIT11 + BIT10; break;
  4440. }
  4441. switch ( info->params.preamble ) {
  4442. case HDLC_PREAMBLE_PATTERN_FLAGS: RegValue |= BIT8 + BIT12; break;
  4443. case HDLC_PREAMBLE_PATTERN_ONES: RegValue |= BIT8; break;
  4444. case HDLC_PREAMBLE_PATTERN_10: RegValue |= BIT9; break;
  4445. case HDLC_PREAMBLE_PATTERN_01: RegValue |= BIT9 + BIT8; break;
  4446. }
  4447. usc_OutReg( info, CCR, RegValue );
  4448. /*
  4449. * Burst/Dwell Control Register
  4450. *
  4451. * <15..8> 0x20 Maximum number of transfers per bus grant
  4452. * <7..0> 0x00 Maximum number of clock cycles per bus grant
  4453. */
  4454. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  4455. /* don't limit bus occupancy on PCI adapter */
  4456. usc_OutDmaReg( info, BDCR, 0x0000 );
  4457. }
  4458. else
  4459. usc_OutDmaReg( info, BDCR, 0x2000 );
  4460. usc_stop_transmitter(info);
  4461. usc_stop_receiver(info);
  4462. } /* end of usc_set_sdlc_mode() */
  4463. /* usc_enable_loopback()
  4464. *
  4465. * Set the 16C32 for internal loopback mode.
  4466. * The TxCLK and RxCLK signals are generated from the BRG0 and
  4467. * the TxD is looped back to the RxD internally.
  4468. *
  4469. * Arguments: info pointer to device instance data
  4470. * enable 1 = enable loopback, 0 = disable
  4471. * Return Value: None
  4472. */
  4473. static void usc_enable_loopback(struct mgsl_struct *info, int enable)
  4474. {
  4475. if (enable) {
  4476. /* blank external TXD output */
  4477. usc_OutReg(info,IOCR,usc_InReg(info,IOCR) | (BIT7+BIT6));
  4478. /* Clock mode Control Register (CMCR)
  4479. *
  4480. * <15..14> 00 counter 1 Disabled
  4481. * <13..12> 00 counter 0 Disabled
  4482. * <11..10> 11 BRG1 Input is TxC Pin
  4483. * <9..8> 11 BRG0 Input is TxC Pin
  4484. * <7..6> 01 DPLL Input is BRG1 Output
  4485. * <5..3> 100 TxCLK comes from BRG0
  4486. * <2..0> 100 RxCLK comes from BRG0
  4487. *
  4488. * 0000 1111 0110 0100 = 0x0f64
  4489. */
  4490. usc_OutReg( info, CMCR, 0x0f64 );
  4491. /* Write 16-bit Time Constant for BRG0 */
  4492. /* use clock speed if available, otherwise use 8 for diagnostics */
  4493. if (info->params.clock_speed) {
  4494. if (info->bus_type == MGSL_BUS_TYPE_PCI)
  4495. usc_OutReg(info, TC0R, (u16)((11059200/info->params.clock_speed)-1));
  4496. else
  4497. usc_OutReg(info, TC0R, (u16)((14745600/info->params.clock_speed)-1));
  4498. } else
  4499. usc_OutReg(info, TC0R, (u16)8);
  4500. /* Hardware Configuration Register (HCR) Clear Bit 1, BRG0
  4501. mode = Continuous Set Bit 0 to enable BRG0. */
  4502. usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
  4503. /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
  4504. usc_OutReg(info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004));
  4505. /* set Internal Data loopback mode */
  4506. info->loopback_bits = 0x300;
  4507. outw( 0x0300, info->io_base + CCAR );
  4508. } else {
  4509. /* enable external TXD output */
  4510. usc_OutReg(info,IOCR,usc_InReg(info,IOCR) & ~(BIT7+BIT6));
  4511. /* clear Internal Data loopback mode */
  4512. info->loopback_bits = 0;
  4513. outw( 0,info->io_base + CCAR );
  4514. }
  4515. } /* end of usc_enable_loopback() */
  4516. /* usc_enable_aux_clock()
  4517. *
  4518. * Enabled the AUX clock output at the specified frequency.
  4519. *
  4520. * Arguments:
  4521. *
  4522. * info pointer to device extension
  4523. * data_rate data rate of clock in bits per second
  4524. * A data rate of 0 disables the AUX clock.
  4525. *
  4526. * Return Value: None
  4527. */
  4528. static void usc_enable_aux_clock( struct mgsl_struct *info, u32 data_rate )
  4529. {
  4530. u32 XtalSpeed;
  4531. u16 Tc;
  4532. if ( data_rate ) {
  4533. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  4534. XtalSpeed = 11059200;
  4535. else
  4536. XtalSpeed = 14745600;
  4537. /* Tc = (Xtal/Speed) - 1 */
  4538. /* If twice the remainder of (Xtal/Speed) is greater than Speed */
  4539. /* then rounding up gives a more precise time constant. Instead */
  4540. /* of rounding up and then subtracting 1 we just don't subtract */
  4541. /* the one in this case. */
  4542. Tc = (u16)(XtalSpeed/data_rate);
  4543. if ( !(((XtalSpeed % data_rate) * 2) / data_rate) )
  4544. Tc--;
  4545. /* Write 16-bit Time Constant for BRG0 */
  4546. usc_OutReg( info, TC0R, Tc );
  4547. /*
  4548. * Hardware Configuration Register (HCR)
  4549. * Clear Bit 1, BRG0 mode = Continuous
  4550. * Set Bit 0 to enable BRG0.
  4551. */
  4552. usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
  4553. /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
  4554. usc_OutReg( info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
  4555. } else {
  4556. /* data rate == 0 so turn off BRG0 */
  4557. usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
  4558. }
  4559. } /* end of usc_enable_aux_clock() */
  4560. /*
  4561. *
  4562. * usc_process_rxoverrun_sync()
  4563. *
  4564. * This function processes a receive overrun by resetting the
  4565. * receive DMA buffers and issuing a Purge Rx FIFO command
  4566. * to allow the receiver to continue receiving.
  4567. *
  4568. * Arguments:
  4569. *
  4570. * info pointer to device extension
  4571. *
  4572. * Return Value: None
  4573. */
  4574. static void usc_process_rxoverrun_sync( struct mgsl_struct *info )
  4575. {
  4576. int start_index;
  4577. int end_index;
  4578. int frame_start_index;
  4579. int start_of_frame_found = FALSE;
  4580. int end_of_frame_found = FALSE;
  4581. int reprogram_dma = FALSE;
  4582. DMABUFFERENTRY *buffer_list = info->rx_buffer_list;
  4583. u32 phys_addr;
  4584. usc_DmaCmd( info, DmaCmd_PauseRxChannel );
  4585. usc_RCmd( info, RCmd_EnterHuntmode );
  4586. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  4587. /* CurrentRxBuffer points to the 1st buffer of the next */
  4588. /* possibly available receive frame. */
  4589. frame_start_index = start_index = end_index = info->current_rx_buffer;
  4590. /* Search for an unfinished string of buffers. This means */
  4591. /* that a receive frame started (at least one buffer with */
  4592. /* count set to zero) but there is no terminiting buffer */
  4593. /* (status set to non-zero). */
  4594. while( !buffer_list[end_index].count )
  4595. {
  4596. /* Count field has been reset to zero by 16C32. */
  4597. /* This buffer is currently in use. */
  4598. if ( !start_of_frame_found )
  4599. {
  4600. start_of_frame_found = TRUE;
  4601. frame_start_index = end_index;
  4602. end_of_frame_found = FALSE;
  4603. }
  4604. if ( buffer_list[end_index].status )
  4605. {
  4606. /* Status field has been set by 16C32. */
  4607. /* This is the last buffer of a received frame. */
  4608. /* We want to leave the buffers for this frame intact. */
  4609. /* Move on to next possible frame. */
  4610. start_of_frame_found = FALSE;
  4611. end_of_frame_found = TRUE;
  4612. }
  4613. /* advance to next buffer entry in linked list */
  4614. end_index++;
  4615. if ( end_index == info->rx_buffer_count )
  4616. end_index = 0;
  4617. if ( start_index == end_index )
  4618. {
  4619. /* The entire list has been searched with all Counts == 0 and */
  4620. /* all Status == 0. The receive buffers are */
  4621. /* completely screwed, reset all receive buffers! */
  4622. mgsl_reset_rx_dma_buffers( info );
  4623. frame_start_index = 0;
  4624. start_of_frame_found = FALSE;
  4625. reprogram_dma = TRUE;
  4626. break;
  4627. }
  4628. }
  4629. if ( start_of_frame_found && !end_of_frame_found )
  4630. {
  4631. /* There is an unfinished string of receive DMA buffers */
  4632. /* as a result of the receiver overrun. */
  4633. /* Reset the buffers for the unfinished frame */
  4634. /* and reprogram the receive DMA controller to start */
  4635. /* at the 1st buffer of unfinished frame. */
  4636. start_index = frame_start_index;
  4637. do
  4638. {
  4639. *((unsigned long *)&(info->rx_buffer_list[start_index++].count)) = DMABUFFERSIZE;
  4640. /* Adjust index for wrap around. */
  4641. if ( start_index == info->rx_buffer_count )
  4642. start_index = 0;
  4643. } while( start_index != end_index );
  4644. reprogram_dma = TRUE;
  4645. }
  4646. if ( reprogram_dma )
  4647. {
  4648. usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
  4649. usc_ClearIrqPendingBits(info, RECEIVE_DATA|RECEIVE_STATUS);
  4650. usc_UnlatchRxstatusBits(info, RECEIVE_DATA|RECEIVE_STATUS);
  4651. usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
  4652. /* This empties the receive FIFO and loads the RCC with RCLR */
  4653. usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
  4654. /* program 16C32 with physical address of 1st DMA buffer entry */
  4655. phys_addr = info->rx_buffer_list[frame_start_index].phys_entry;
  4656. usc_OutDmaReg( info, NRARL, (u16)phys_addr );
  4657. usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
  4658. usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
  4659. usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
  4660. usc_EnableInterrupts( info, RECEIVE_STATUS );
  4661. /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
  4662. /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
  4663. usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 );
  4664. usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
  4665. usc_DmaCmd( info, DmaCmd_InitRxChannel );
  4666. if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
  4667. usc_EnableReceiver(info,ENABLE_AUTO_DCD);
  4668. else
  4669. usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
  4670. }
  4671. else
  4672. {
  4673. /* This empties the receive FIFO and loads the RCC with RCLR */
  4674. usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
  4675. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  4676. }
  4677. } /* end of usc_process_rxoverrun_sync() */
  4678. /* usc_stop_receiver()
  4679. *
  4680. * Disable USC receiver
  4681. *
  4682. * Arguments: info pointer to device instance data
  4683. * Return Value: None
  4684. */
  4685. static void usc_stop_receiver( struct mgsl_struct *info )
  4686. {
  4687. if (debug_level >= DEBUG_LEVEL_ISR)
  4688. printk("%s(%d):usc_stop_receiver(%s)\n",
  4689. __FILE__,__LINE__, info->device_name );
  4690. /* Disable receive DMA channel. */
  4691. /* This also disables receive DMA channel interrupts */
  4692. usc_DmaCmd( info, DmaCmd_ResetRxChannel );
  4693. usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
  4694. usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
  4695. usc_DisableInterrupts( info, RECEIVE_DATA + RECEIVE_STATUS );
  4696. usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
  4697. /* This empties the receive FIFO and loads the RCC with RCLR */
  4698. usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
  4699. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  4700. info->rx_enabled = 0;
  4701. info->rx_overflow = 0;
  4702. info->rx_rcc_underrun = 0;
  4703. } /* end of stop_receiver() */
  4704. /* usc_start_receiver()
  4705. *
  4706. * Enable the USC receiver
  4707. *
  4708. * Arguments: info pointer to device instance data
  4709. * Return Value: None
  4710. */
  4711. static void usc_start_receiver( struct mgsl_struct *info )
  4712. {
  4713. u32 phys_addr;
  4714. if (debug_level >= DEBUG_LEVEL_ISR)
  4715. printk("%s(%d):usc_start_receiver(%s)\n",
  4716. __FILE__,__LINE__, info->device_name );
  4717. mgsl_reset_rx_dma_buffers( info );
  4718. usc_stop_receiver( info );
  4719. usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
  4720. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  4721. if ( info->params.mode == MGSL_MODE_HDLC ||
  4722. info->params.mode == MGSL_MODE_RAW ) {
  4723. /* DMA mode Transfers */
  4724. /* Program the DMA controller. */
  4725. /* Enable the DMA controller end of buffer interrupt. */
  4726. /* program 16C32 with physical address of 1st DMA buffer entry */
  4727. phys_addr = info->rx_buffer_list[0].phys_entry;
  4728. usc_OutDmaReg( info, NRARL, (u16)phys_addr );
  4729. usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
  4730. usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
  4731. usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
  4732. usc_EnableInterrupts( info, RECEIVE_STATUS );
  4733. /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
  4734. /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
  4735. usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 );
  4736. usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
  4737. usc_DmaCmd( info, DmaCmd_InitRxChannel );
  4738. if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
  4739. usc_EnableReceiver(info,ENABLE_AUTO_DCD);
  4740. else
  4741. usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
  4742. } else {
  4743. usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
  4744. usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS);
  4745. usc_EnableInterrupts(info, RECEIVE_DATA);
  4746. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  4747. usc_RCmd( info, RCmd_EnterHuntmode );
  4748. usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
  4749. }
  4750. usc_OutReg( info, CCSR, 0x1020 );
  4751. info->rx_enabled = 1;
  4752. } /* end of usc_start_receiver() */
  4753. /* usc_start_transmitter()
  4754. *
  4755. * Enable the USC transmitter and send a transmit frame if
  4756. * one is loaded in the DMA buffers.
  4757. *
  4758. * Arguments: info pointer to device instance data
  4759. * Return Value: None
  4760. */
  4761. static void usc_start_transmitter( struct mgsl_struct *info )
  4762. {
  4763. u32 phys_addr;
  4764. unsigned int FrameSize;
  4765. if (debug_level >= DEBUG_LEVEL_ISR)
  4766. printk("%s(%d):usc_start_transmitter(%s)\n",
  4767. __FILE__,__LINE__, info->device_name );
  4768. if ( info->xmit_cnt ) {
  4769. /* If auto RTS enabled and RTS is inactive, then assert */
  4770. /* RTS and set a flag indicating that the driver should */
  4771. /* negate RTS when the transmission completes. */
  4772. info->drop_rts_on_tx_done = 0;
  4773. if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
  4774. usc_get_serial_signals( info );
  4775. if ( !(info->serial_signals & SerialSignal_RTS) ) {
  4776. info->serial_signals |= SerialSignal_RTS;
  4777. usc_set_serial_signals( info );
  4778. info->drop_rts_on_tx_done = 1;
  4779. }
  4780. }
  4781. if ( info->params.mode == MGSL_MODE_ASYNC ) {
  4782. if ( !info->tx_active ) {
  4783. usc_UnlatchTxstatusBits(info, TXSTATUS_ALL);
  4784. usc_ClearIrqPendingBits(info, TRANSMIT_STATUS + TRANSMIT_DATA);
  4785. usc_EnableInterrupts(info, TRANSMIT_DATA);
  4786. usc_load_txfifo(info);
  4787. }
  4788. } else {
  4789. /* Disable transmit DMA controller while programming. */
  4790. usc_DmaCmd( info, DmaCmd_ResetTxChannel );
  4791. /* Transmit DMA buffer is loaded, so program USC */
  4792. /* to send the frame contained in the buffers. */
  4793. FrameSize = info->tx_buffer_list[info->start_tx_dma_buffer].rcc;
  4794. /* if operating in Raw sync mode, reset the rcc component
  4795. * of the tx dma buffer entry, otherwise, the serial controller
  4796. * will send a closing sync char after this count.
  4797. */
  4798. if ( info->params.mode == MGSL_MODE_RAW )
  4799. info->tx_buffer_list[info->start_tx_dma_buffer].rcc = 0;
  4800. /* Program the Transmit Character Length Register (TCLR) */
  4801. /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
  4802. usc_OutReg( info, TCLR, (u16)FrameSize );
  4803. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  4804. /* Program the address of the 1st DMA Buffer Entry in linked list */
  4805. phys_addr = info->tx_buffer_list[info->start_tx_dma_buffer].phys_entry;
  4806. usc_OutDmaReg( info, NTARL, (u16)phys_addr );
  4807. usc_OutDmaReg( info, NTARU, (u16)(phys_addr >> 16) );
  4808. usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
  4809. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
  4810. usc_EnableInterrupts( info, TRANSMIT_STATUS );
  4811. if ( info->params.mode == MGSL_MODE_RAW &&
  4812. info->num_tx_dma_buffers > 1 ) {
  4813. /* When running external sync mode, attempt to 'stream' transmit */
  4814. /* by filling tx dma buffers as they become available. To do this */
  4815. /* we need to enable Tx DMA EOB Status interrupts : */
  4816. /* */
  4817. /* 1. Arm End of Buffer (EOB) Transmit DMA Interrupt (BIT2 of TDIAR) */
  4818. /* 2. Enable Transmit DMA Interrupts (BIT0 of DICR) */
  4819. usc_OutDmaReg( info, TDIAR, BIT2|BIT3 );
  4820. usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT0) );
  4821. }
  4822. /* Initialize Transmit DMA Channel */
  4823. usc_DmaCmd( info, DmaCmd_InitTxChannel );
  4824. usc_TCmd( info, TCmd_SendFrame );
  4825. info->tx_timer.expires = jiffies + msecs_to_jiffies(5000);
  4826. add_timer(&info->tx_timer);
  4827. }
  4828. info->tx_active = 1;
  4829. }
  4830. if ( !info->tx_enabled ) {
  4831. info->tx_enabled = 1;
  4832. if ( info->params.flags & HDLC_FLAG_AUTO_CTS )
  4833. usc_EnableTransmitter(info,ENABLE_AUTO_CTS);
  4834. else
  4835. usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
  4836. }
  4837. } /* end of usc_start_transmitter() */
  4838. /* usc_stop_transmitter()
  4839. *
  4840. * Stops the transmitter and DMA
  4841. *
  4842. * Arguments: info pointer to device isntance data
  4843. * Return Value: None
  4844. */
  4845. static void usc_stop_transmitter( struct mgsl_struct *info )
  4846. {
  4847. if (debug_level >= DEBUG_LEVEL_ISR)
  4848. printk("%s(%d):usc_stop_transmitter(%s)\n",
  4849. __FILE__,__LINE__, info->device_name );
  4850. del_timer(&info->tx_timer);
  4851. usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
  4852. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA );
  4853. usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA );
  4854. usc_EnableTransmitter(info,DISABLE_UNCONDITIONAL);
  4855. usc_DmaCmd( info, DmaCmd_ResetTxChannel );
  4856. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  4857. info->tx_enabled = 0;
  4858. info->tx_active = 0;
  4859. } /* end of usc_stop_transmitter() */
  4860. /* usc_load_txfifo()
  4861. *
  4862. * Fill the transmit FIFO until the FIFO is full or
  4863. * there is no more data to load.
  4864. *
  4865. * Arguments: info pointer to device extension (instance data)
  4866. * Return Value: None
  4867. */
  4868. static void usc_load_txfifo( struct mgsl_struct *info )
  4869. {
  4870. int Fifocount;
  4871. u8 TwoBytes[2];
  4872. if ( !info->xmit_cnt && !info->x_char )
  4873. return;
  4874. /* Select transmit FIFO status readback in TICR */
  4875. usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
  4876. /* load the Transmit FIFO until FIFOs full or all data sent */
  4877. while( (Fifocount = usc_InReg(info, TICR) >> 8) && info->xmit_cnt ) {
  4878. /* there is more space in the transmit FIFO and */
  4879. /* there is more data in transmit buffer */
  4880. if ( (info->xmit_cnt > 1) && (Fifocount > 1) && !info->x_char ) {
  4881. /* write a 16-bit word from transmit buffer to 16C32 */
  4882. TwoBytes[0] = info->xmit_buf[info->xmit_tail++];
  4883. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  4884. TwoBytes[1] = info->xmit_buf[info->xmit_tail++];
  4885. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  4886. outw( *((u16 *)TwoBytes), info->io_base + DATAREG);
  4887. info->xmit_cnt -= 2;
  4888. info->icount.tx += 2;
  4889. } else {
  4890. /* only 1 byte left to transmit or 1 FIFO slot left */
  4891. outw( (inw( info->io_base + CCAR) & 0x0780) | (TDR+LSBONLY),
  4892. info->io_base + CCAR );
  4893. if (info->x_char) {
  4894. /* transmit pending high priority char */
  4895. outw( info->x_char,info->io_base + CCAR );
  4896. info->x_char = 0;
  4897. } else {
  4898. outw( info->xmit_buf[info->xmit_tail++],info->io_base + CCAR );
  4899. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  4900. info->xmit_cnt--;
  4901. }
  4902. info->icount.tx++;
  4903. }
  4904. }
  4905. } /* end of usc_load_txfifo() */
  4906. /* usc_reset()
  4907. *
  4908. * Reset the adapter to a known state and prepare it for further use.
  4909. *
  4910. * Arguments: info pointer to device instance data
  4911. * Return Value: None
  4912. */
  4913. static void usc_reset( struct mgsl_struct *info )
  4914. {
  4915. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  4916. int i;
  4917. u32 readval;
  4918. /* Set BIT30 of Misc Control Register */
  4919. /* (Local Control Register 0x50) to force reset of USC. */
  4920. volatile u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
  4921. u32 *LCR0BRDR = (u32 *)(info->lcr_base + 0x28);
  4922. info->misc_ctrl_value |= BIT30;
  4923. *MiscCtrl = info->misc_ctrl_value;
  4924. /*
  4925. * Force at least 170ns delay before clearing
  4926. * reset bit. Each read from LCR takes at least
  4927. * 30ns so 10 times for 300ns to be safe.
  4928. */
  4929. for(i=0;i<10;i++)
  4930. readval = *MiscCtrl;
  4931. info->misc_ctrl_value &= ~BIT30;
  4932. *MiscCtrl = info->misc_ctrl_value;
  4933. *LCR0BRDR = BUS_DESCRIPTOR(
  4934. 1, // Write Strobe Hold (0-3)
  4935. 2, // Write Strobe Delay (0-3)
  4936. 2, // Read Strobe Delay (0-3)
  4937. 0, // NWDD (Write data-data) (0-3)
  4938. 4, // NWAD (Write Addr-data) (0-31)
  4939. 0, // NXDA (Read/Write Data-Addr) (0-3)
  4940. 0, // NRDD (Read Data-Data) (0-3)
  4941. 5 // NRAD (Read Addr-Data) (0-31)
  4942. );
  4943. } else {
  4944. /* do HW reset */
  4945. outb( 0,info->io_base + 8 );
  4946. }
  4947. info->mbre_bit = 0;
  4948. info->loopback_bits = 0;
  4949. info->usc_idle_mode = 0;
  4950. /*
  4951. * Program the Bus Configuration Register (BCR)
  4952. *
  4953. * <15> 0 Don't use separate address
  4954. * <14..6> 0 reserved
  4955. * <5..4> 00 IAckmode = Default, don't care
  4956. * <3> 1 Bus Request Totem Pole output
  4957. * <2> 1 Use 16 Bit data bus
  4958. * <1> 0 IRQ Totem Pole output
  4959. * <0> 0 Don't Shift Right Addr
  4960. *
  4961. * 0000 0000 0000 1100 = 0x000c
  4962. *
  4963. * By writing to io_base + SDPIN the Wait/Ack pin is
  4964. * programmed to work as a Wait pin.
  4965. */
  4966. outw( 0x000c,info->io_base + SDPIN );
  4967. outw( 0,info->io_base );
  4968. outw( 0,info->io_base + CCAR );
  4969. /* select little endian byte ordering */
  4970. usc_RTCmd( info, RTCmd_SelectLittleEndian );
  4971. /* Port Control Register (PCR)
  4972. *
  4973. * <15..14> 11 Port 7 is Output (~DMAEN, Bit 14 : 0 = Enabled)
  4974. * <13..12> 11 Port 6 is Output (~INTEN, Bit 12 : 0 = Enabled)
  4975. * <11..10> 00 Port 5 is Input (No Connect, Don't Care)
  4976. * <9..8> 00 Port 4 is Input (No Connect, Don't Care)
  4977. * <7..6> 11 Port 3 is Output (~RTS, Bit 6 : 0 = Enabled )
  4978. * <5..4> 11 Port 2 is Output (~DTR, Bit 4 : 0 = Enabled )
  4979. * <3..2> 01 Port 1 is Input (Dedicated RxC)
  4980. * <1..0> 01 Port 0 is Input (Dedicated TxC)
  4981. *
  4982. * 1111 0000 1111 0101 = 0xf0f5
  4983. */
  4984. usc_OutReg( info, PCR, 0xf0f5 );
  4985. /*
  4986. * Input/Output Control Register
  4987. *
  4988. * <15..14> 00 CTS is active low input
  4989. * <13..12> 00 DCD is active low input
  4990. * <11..10> 00 TxREQ pin is input (DSR)
  4991. * <9..8> 00 RxREQ pin is input (RI)
  4992. * <7..6> 00 TxD is output (Transmit Data)
  4993. * <5..3> 000 TxC Pin in Input (14.7456MHz Clock)
  4994. * <2..0> 100 RxC is Output (drive with BRG0)
  4995. *
  4996. * 0000 0000 0000 0100 = 0x0004
  4997. */
  4998. usc_OutReg( info, IOCR, 0x0004 );
  4999. } /* end of usc_reset() */
  5000. /* usc_set_async_mode()
  5001. *
  5002. * Program adapter for asynchronous communications.
  5003. *
  5004. * Arguments: info pointer to device instance data
  5005. * Return Value: None
  5006. */
  5007. static void usc_set_async_mode( struct mgsl_struct *info )
  5008. {
  5009. u16 RegValue;
  5010. /* disable interrupts while programming USC */
  5011. usc_DisableMasterIrqBit( info );
  5012. outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
  5013. usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
  5014. usc_loopback_frame( info );
  5015. /* Channel mode Register (CMR)
  5016. *
  5017. * <15..14> 00 Tx Sub modes, 00 = 1 Stop Bit
  5018. * <13..12> 00 00 = 16X Clock
  5019. * <11..8> 0000 Transmitter mode = Asynchronous
  5020. * <7..6> 00 reserved?
  5021. * <5..4> 00 Rx Sub modes, 00 = 16X Clock
  5022. * <3..0> 0000 Receiver mode = Asynchronous
  5023. *
  5024. * 0000 0000 0000 0000 = 0x0
  5025. */
  5026. RegValue = 0;
  5027. if ( info->params.stop_bits != 1 )
  5028. RegValue |= BIT14;
  5029. usc_OutReg( info, CMR, RegValue );
  5030. /* Receiver mode Register (RMR)
  5031. *
  5032. * <15..13> 000 encoding = None
  5033. * <12..08> 00000 reserved (Sync Only)
  5034. * <7..6> 00 Even parity
  5035. * <5> 0 parity disabled
  5036. * <4..2> 000 Receive Char Length = 8 bits
  5037. * <1..0> 00 Disable Receiver
  5038. *
  5039. * 0000 0000 0000 0000 = 0x0
  5040. */
  5041. RegValue = 0;
  5042. if ( info->params.data_bits != 8 )
  5043. RegValue |= BIT4+BIT3+BIT2;
  5044. if ( info->params.parity != ASYNC_PARITY_NONE ) {
  5045. RegValue |= BIT5;
  5046. if ( info->params.parity != ASYNC_PARITY_ODD )
  5047. RegValue |= BIT6;
  5048. }
  5049. usc_OutReg( info, RMR, RegValue );
  5050. /* Set IRQ trigger level */
  5051. usc_RCmd( info, RCmd_SelectRicrIntLevel );
  5052. /* Receive Interrupt Control Register (RICR)
  5053. *
  5054. * <15..8> ? RxFIFO IRQ Request Level
  5055. *
  5056. * Note: For async mode the receive FIFO level must be set
  5057. * to 0 to aviod the situation where the FIFO contains fewer bytes
  5058. * than the trigger level and no more data is expected.
  5059. *
  5060. * <7> 0 Exited Hunt IA (Interrupt Arm)
  5061. * <6> 0 Idle Received IA
  5062. * <5> 0 Break/Abort IA
  5063. * <4> 0 Rx Bound IA
  5064. * <3> 0 Queued status reflects oldest byte in FIFO
  5065. * <2> 0 Abort/PE IA
  5066. * <1> 0 Rx Overrun IA
  5067. * <0> 0 Select TC0 value for readback
  5068. *
  5069. * 0000 0000 0100 0000 = 0x0000 + (FIFOLEVEL in MSB)
  5070. */
  5071. usc_OutReg( info, RICR, 0x0000 );
  5072. usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
  5073. usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
  5074. /* Transmit mode Register (TMR)
  5075. *
  5076. * <15..13> 000 encoding = None
  5077. * <12..08> 00000 reserved (Sync Only)
  5078. * <7..6> 00 Transmit parity Even
  5079. * <5> 0 Transmit parity Disabled
  5080. * <4..2> 000 Tx Char Length = 8 bits
  5081. * <1..0> 00 Disable Transmitter
  5082. *
  5083. * 0000 0000 0000 0000 = 0x0
  5084. */
  5085. RegValue = 0;
  5086. if ( info->params.data_bits != 8 )
  5087. RegValue |= BIT4+BIT3+BIT2;
  5088. if ( info->params.parity != ASYNC_PARITY_NONE ) {
  5089. RegValue |= BIT5;
  5090. if ( info->params.parity != ASYNC_PARITY_ODD )
  5091. RegValue |= BIT6;
  5092. }
  5093. usc_OutReg( info, TMR, RegValue );
  5094. usc_set_txidle( info );
  5095. /* Set IRQ trigger level */
  5096. usc_TCmd( info, TCmd_SelectTicrIntLevel );
  5097. /* Transmit Interrupt Control Register (TICR)
  5098. *
  5099. * <15..8> ? Transmit FIFO IRQ Level
  5100. * <7> 0 Present IA (Interrupt Arm)
  5101. * <6> 1 Idle Sent IA
  5102. * <5> 0 Abort Sent IA
  5103. * <4> 0 EOF/EOM Sent IA
  5104. * <3> 0 CRC Sent IA
  5105. * <2> 0 1 = Wait for SW Trigger to Start Frame
  5106. * <1> 0 Tx Underrun IA
  5107. * <0> 0 TC0 constant on read back
  5108. *
  5109. * 0000 0000 0100 0000 = 0x0040
  5110. */
  5111. usc_OutReg( info, TICR, 0x1f40 );
  5112. usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
  5113. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
  5114. usc_enable_async_clock( info, info->params.data_rate );
  5115. /* Channel Control/status Register (CCSR)
  5116. *
  5117. * <15> X RCC FIFO Overflow status (RO)
  5118. * <14> X RCC FIFO Not Empty status (RO)
  5119. * <13> 0 1 = Clear RCC FIFO (WO)
  5120. * <12> X DPLL in Sync status (RO)
  5121. * <11> X DPLL 2 Missed Clocks status (RO)
  5122. * <10> X DPLL 1 Missed Clock status (RO)
  5123. * <9..8> 00 DPLL Resync on rising and falling edges (RW)
  5124. * <7> X SDLC Loop On status (RO)
  5125. * <6> X SDLC Loop Send status (RO)
  5126. * <5> 1 Bypass counters for TxClk and RxClk (RW)
  5127. * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
  5128. * <1..0> 00 reserved
  5129. *
  5130. * 0000 0000 0010 0000 = 0x0020
  5131. */
  5132. usc_OutReg( info, CCSR, 0x0020 );
  5133. usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA +
  5134. RECEIVE_DATA + RECEIVE_STATUS );
  5135. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA +
  5136. RECEIVE_DATA + RECEIVE_STATUS );
  5137. usc_EnableMasterIrqBit( info );
  5138. if (info->bus_type == MGSL_BUS_TYPE_ISA) {
  5139. /* Enable INTEN (Port 6, Bit12) */
  5140. /* This connects the IRQ request signal to the ISA bus */
  5141. usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12));
  5142. }
  5143. } /* end of usc_set_async_mode() */
  5144. /* usc_loopback_frame()
  5145. *
  5146. * Loop back a small (2 byte) dummy SDLC frame.
  5147. * Interrupts and DMA are NOT used. The purpose of this is to
  5148. * clear any 'stale' status info left over from running in async mode.
  5149. *
  5150. * The 16C32 shows the strange behaviour of marking the 1st
  5151. * received SDLC frame with a CRC error even when there is no
  5152. * CRC error. To get around this a small dummy from of 2 bytes
  5153. * is looped back when switching from async to sync mode.
  5154. *
  5155. * Arguments: info pointer to device instance data
  5156. * Return Value: None
  5157. */
  5158. static void usc_loopback_frame( struct mgsl_struct *info )
  5159. {
  5160. int i;
  5161. unsigned long oldmode = info->params.mode;
  5162. info->params.mode = MGSL_MODE_HDLC;
  5163. usc_DisableMasterIrqBit( info );
  5164. usc_set_sdlc_mode( info );
  5165. usc_enable_loopback( info, 1 );
  5166. /* Write 16-bit Time Constant for BRG0 */
  5167. usc_OutReg( info, TC0R, 0 );
  5168. /* Channel Control Register (CCR)
  5169. *
  5170. * <15..14> 00 Don't use 32-bit Tx Control Blocks (TCBs)
  5171. * <13> 0 Trigger Tx on SW Command Disabled
  5172. * <12> 0 Flag Preamble Disabled
  5173. * <11..10> 00 Preamble Length = 8-Bits
  5174. * <9..8> 01 Preamble Pattern = flags
  5175. * <7..6> 10 Don't use 32-bit Rx status Blocks (RSBs)
  5176. * <5> 0 Trigger Rx on SW Command Disabled
  5177. * <4..0> 0 reserved
  5178. *
  5179. * 0000 0001 0000 0000 = 0x0100
  5180. */
  5181. usc_OutReg( info, CCR, 0x0100 );
  5182. /* SETUP RECEIVER */
  5183. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  5184. usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
  5185. /* SETUP TRANSMITTER */
  5186. /* Program the Transmit Character Length Register (TCLR) */
  5187. /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
  5188. usc_OutReg( info, TCLR, 2 );
  5189. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  5190. /* unlatch Tx status bits, and start transmit channel. */
  5191. usc_UnlatchTxstatusBits(info,TXSTATUS_ALL);
  5192. outw(0,info->io_base + DATAREG);
  5193. /* ENABLE TRANSMITTER */
  5194. usc_TCmd( info, TCmd_SendFrame );
  5195. usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
  5196. /* WAIT FOR RECEIVE COMPLETE */
  5197. for (i=0 ; i<1000 ; i++)
  5198. if (usc_InReg( info, RCSR ) & (BIT8 + BIT4 + BIT3 + BIT1))
  5199. break;
  5200. /* clear Internal Data loopback mode */
  5201. usc_enable_loopback(info, 0);
  5202. usc_EnableMasterIrqBit(info);
  5203. info->params.mode = oldmode;
  5204. } /* end of usc_loopback_frame() */
  5205. /* usc_set_sync_mode() Programs the USC for SDLC communications.
  5206. *
  5207. * Arguments: info pointer to adapter info structure
  5208. * Return Value: None
  5209. */
  5210. static void usc_set_sync_mode( struct mgsl_struct *info )
  5211. {
  5212. usc_loopback_frame( info );
  5213. usc_set_sdlc_mode( info );
  5214. if (info->bus_type == MGSL_BUS_TYPE_ISA) {
  5215. /* Enable INTEN (Port 6, Bit12) */
  5216. /* This connects the IRQ request signal to the ISA bus */
  5217. usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12));
  5218. }
  5219. usc_enable_aux_clock(info, info->params.clock_speed);
  5220. if (info->params.loopback)
  5221. usc_enable_loopback(info,1);
  5222. } /* end of mgsl_set_sync_mode() */
  5223. /* usc_set_txidle() Set the HDLC idle mode for the transmitter.
  5224. *
  5225. * Arguments: info pointer to device instance data
  5226. * Return Value: None
  5227. */
  5228. static void usc_set_txidle( struct mgsl_struct *info )
  5229. {
  5230. u16 usc_idle_mode = IDLEMODE_FLAGS;
  5231. /* Map API idle mode to USC register bits */
  5232. switch( info->idle_mode ){
  5233. case HDLC_TXIDLE_FLAGS: usc_idle_mode = IDLEMODE_FLAGS; break;
  5234. case HDLC_TXIDLE_ALT_ZEROS_ONES: usc_idle_mode = IDLEMODE_ALT_ONE_ZERO; break;
  5235. case HDLC_TXIDLE_ZEROS: usc_idle_mode = IDLEMODE_ZERO; break;
  5236. case HDLC_TXIDLE_ONES: usc_idle_mode = IDLEMODE_ONE; break;
  5237. case HDLC_TXIDLE_ALT_MARK_SPACE: usc_idle_mode = IDLEMODE_ALT_MARK_SPACE; break;
  5238. case HDLC_TXIDLE_SPACE: usc_idle_mode = IDLEMODE_SPACE; break;
  5239. case HDLC_TXIDLE_MARK: usc_idle_mode = IDLEMODE_MARK; break;
  5240. }
  5241. info->usc_idle_mode = usc_idle_mode;
  5242. //usc_OutReg(info, TCSR, usc_idle_mode);
  5243. info->tcsr_value &= ~IDLEMODE_MASK; /* clear idle mode bits */
  5244. info->tcsr_value += usc_idle_mode;
  5245. usc_OutReg(info, TCSR, info->tcsr_value);
  5246. /*
  5247. * if SyncLink WAN adapter is running in external sync mode, the
  5248. * transmitter has been set to Monosync in order to try to mimic
  5249. * a true raw outbound bit stream. Monosync still sends an open/close
  5250. * sync char at the start/end of a frame. Try to match those sync
  5251. * patterns to the idle mode set here
  5252. */
  5253. if ( info->params.mode == MGSL_MODE_RAW ) {
  5254. unsigned char syncpat = 0;
  5255. switch( info->idle_mode ) {
  5256. case HDLC_TXIDLE_FLAGS:
  5257. syncpat = 0x7e;
  5258. break;
  5259. case HDLC_TXIDLE_ALT_ZEROS_ONES:
  5260. syncpat = 0x55;
  5261. break;
  5262. case HDLC_TXIDLE_ZEROS:
  5263. case HDLC_TXIDLE_SPACE:
  5264. syncpat = 0x00;
  5265. break;
  5266. case HDLC_TXIDLE_ONES:
  5267. case HDLC_TXIDLE_MARK:
  5268. syncpat = 0xff;
  5269. break;
  5270. case HDLC_TXIDLE_ALT_MARK_SPACE:
  5271. syncpat = 0xaa;
  5272. break;
  5273. }
  5274. usc_SetTransmitSyncChars(info,syncpat,syncpat);
  5275. }
  5276. } /* end of usc_set_txidle() */
  5277. /* usc_get_serial_signals()
  5278. *
  5279. * Query the adapter for the state of the V24 status (input) signals.
  5280. *
  5281. * Arguments: info pointer to device instance data
  5282. * Return Value: None
  5283. */
  5284. static void usc_get_serial_signals( struct mgsl_struct *info )
  5285. {
  5286. u16 status;
  5287. /* clear all serial signals except DTR and RTS */
  5288. info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
  5289. /* Read the Misc Interrupt status Register (MISR) to get */
  5290. /* the V24 status signals. */
  5291. status = usc_InReg( info, MISR );
  5292. /* set serial signal bits to reflect MISR */
  5293. if ( status & MISCSTATUS_CTS )
  5294. info->serial_signals |= SerialSignal_CTS;
  5295. if ( status & MISCSTATUS_DCD )
  5296. info->serial_signals |= SerialSignal_DCD;
  5297. if ( status & MISCSTATUS_RI )
  5298. info->serial_signals |= SerialSignal_RI;
  5299. if ( status & MISCSTATUS_DSR )
  5300. info->serial_signals |= SerialSignal_DSR;
  5301. } /* end of usc_get_serial_signals() */
  5302. /* usc_set_serial_signals()
  5303. *
  5304. * Set the state of DTR and RTS based on contents of
  5305. * serial_signals member of device extension.
  5306. *
  5307. * Arguments: info pointer to device instance data
  5308. * Return Value: None
  5309. */
  5310. static void usc_set_serial_signals( struct mgsl_struct *info )
  5311. {
  5312. u16 Control;
  5313. unsigned char V24Out = info->serial_signals;
  5314. /* get the current value of the Port Control Register (PCR) */
  5315. Control = usc_InReg( info, PCR );
  5316. if ( V24Out & SerialSignal_RTS )
  5317. Control &= ~(BIT6);
  5318. else
  5319. Control |= BIT6;
  5320. if ( V24Out & SerialSignal_DTR )
  5321. Control &= ~(BIT4);
  5322. else
  5323. Control |= BIT4;
  5324. usc_OutReg( info, PCR, Control );
  5325. } /* end of usc_set_serial_signals() */
  5326. /* usc_enable_async_clock()
  5327. *
  5328. * Enable the async clock at the specified frequency.
  5329. *
  5330. * Arguments: info pointer to device instance data
  5331. * data_rate data rate of clock in bps
  5332. * 0 disables the AUX clock.
  5333. * Return Value: None
  5334. */
  5335. static void usc_enable_async_clock( struct mgsl_struct *info, u32 data_rate )
  5336. {
  5337. if ( data_rate ) {
  5338. /*
  5339. * Clock mode Control Register (CMCR)
  5340. *
  5341. * <15..14> 00 counter 1 Disabled
  5342. * <13..12> 00 counter 0 Disabled
  5343. * <11..10> 11 BRG1 Input is TxC Pin
  5344. * <9..8> 11 BRG0 Input is TxC Pin
  5345. * <7..6> 01 DPLL Input is BRG1 Output
  5346. * <5..3> 100 TxCLK comes from BRG0
  5347. * <2..0> 100 RxCLK comes from BRG0
  5348. *
  5349. * 0000 1111 0110 0100 = 0x0f64
  5350. */
  5351. usc_OutReg( info, CMCR, 0x0f64 );
  5352. /*
  5353. * Write 16-bit Time Constant for BRG0
  5354. * Time Constant = (ClkSpeed / data_rate) - 1
  5355. * ClkSpeed = 921600 (ISA), 691200 (PCI)
  5356. */
  5357. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  5358. usc_OutReg( info, TC0R, (u16)((691200/data_rate) - 1) );
  5359. else
  5360. usc_OutReg( info, TC0R, (u16)((921600/data_rate) - 1) );
  5361. /*
  5362. * Hardware Configuration Register (HCR)
  5363. * Clear Bit 1, BRG0 mode = Continuous
  5364. * Set Bit 0 to enable BRG0.
  5365. */
  5366. usc_OutReg( info, HCR,
  5367. (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
  5368. /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
  5369. usc_OutReg( info, IOCR,
  5370. (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
  5371. } else {
  5372. /* data rate == 0 so turn off BRG0 */
  5373. usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
  5374. }
  5375. } /* end of usc_enable_async_clock() */
  5376. /*
  5377. * Buffer Structures:
  5378. *
  5379. * Normal memory access uses virtual addresses that can make discontiguous
  5380. * physical memory pages appear to be contiguous in the virtual address
  5381. * space (the processors memory mapping handles the conversions).
  5382. *
  5383. * DMA transfers require physically contiguous memory. This is because
  5384. * the DMA system controller and DMA bus masters deal with memory using
  5385. * only physical addresses.
  5386. *
  5387. * This causes a problem under Windows NT when large DMA buffers are
  5388. * needed. Fragmentation of the nonpaged pool prevents allocations of
  5389. * physically contiguous buffers larger than the PAGE_SIZE.
  5390. *
  5391. * However the 16C32 supports Bus Master Scatter/Gather DMA which
  5392. * allows DMA transfers to physically discontiguous buffers. Information
  5393. * about each data transfer buffer is contained in a memory structure
  5394. * called a 'buffer entry'. A list of buffer entries is maintained
  5395. * to track and control the use of the data transfer buffers.
  5396. *
  5397. * To support this strategy we will allocate sufficient PAGE_SIZE
  5398. * contiguous memory buffers to allow for the total required buffer
  5399. * space.
  5400. *
  5401. * The 16C32 accesses the list of buffer entries using Bus Master
  5402. * DMA. Control information is read from the buffer entries by the
  5403. * 16C32 to control data transfers. status information is written to
  5404. * the buffer entries by the 16C32 to indicate the status of completed
  5405. * transfers.
  5406. *
  5407. * The CPU writes control information to the buffer entries to control
  5408. * the 16C32 and reads status information from the buffer entries to
  5409. * determine information about received and transmitted frames.
  5410. *
  5411. * Because the CPU and 16C32 (adapter) both need simultaneous access
  5412. * to the buffer entries, the buffer entry memory is allocated with
  5413. * HalAllocateCommonBuffer(). This restricts the size of the buffer
  5414. * entry list to PAGE_SIZE.
  5415. *
  5416. * The actual data buffers on the other hand will only be accessed
  5417. * by the CPU or the adapter but not by both simultaneously. This allows
  5418. * Scatter/Gather packet based DMA procedures for using physically
  5419. * discontiguous pages.
  5420. */
  5421. /*
  5422. * mgsl_reset_tx_dma_buffers()
  5423. *
  5424. * Set the count for all transmit buffers to 0 to indicate the
  5425. * buffer is available for use and set the current buffer to the
  5426. * first buffer. This effectively makes all buffers free and
  5427. * discards any data in buffers.
  5428. *
  5429. * Arguments: info pointer to device instance data
  5430. * Return Value: None
  5431. */
  5432. static void mgsl_reset_tx_dma_buffers( struct mgsl_struct *info )
  5433. {
  5434. unsigned int i;
  5435. for ( i = 0; i < info->tx_buffer_count; i++ ) {
  5436. *((unsigned long *)&(info->tx_buffer_list[i].count)) = 0;
  5437. }
  5438. info->current_tx_buffer = 0;
  5439. info->start_tx_dma_buffer = 0;
  5440. info->tx_dma_buffers_used = 0;
  5441. info->get_tx_holding_index = 0;
  5442. info->put_tx_holding_index = 0;
  5443. info->tx_holding_count = 0;
  5444. } /* end of mgsl_reset_tx_dma_buffers() */
  5445. /*
  5446. * num_free_tx_dma_buffers()
  5447. *
  5448. * returns the number of free tx dma buffers available
  5449. *
  5450. * Arguments: info pointer to device instance data
  5451. * Return Value: number of free tx dma buffers
  5452. */
  5453. static int num_free_tx_dma_buffers(struct mgsl_struct *info)
  5454. {
  5455. return info->tx_buffer_count - info->tx_dma_buffers_used;
  5456. }
  5457. /*
  5458. * mgsl_reset_rx_dma_buffers()
  5459. *
  5460. * Set the count for all receive buffers to DMABUFFERSIZE
  5461. * and set the current buffer to the first buffer. This effectively
  5462. * makes all buffers free and discards any data in buffers.
  5463. *
  5464. * Arguments: info pointer to device instance data
  5465. * Return Value: None
  5466. */
  5467. static void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info )
  5468. {
  5469. unsigned int i;
  5470. for ( i = 0; i < info->rx_buffer_count; i++ ) {
  5471. *((unsigned long *)&(info->rx_buffer_list[i].count)) = DMABUFFERSIZE;
  5472. // info->rx_buffer_list[i].count = DMABUFFERSIZE;
  5473. // info->rx_buffer_list[i].status = 0;
  5474. }
  5475. info->current_rx_buffer = 0;
  5476. } /* end of mgsl_reset_rx_dma_buffers() */
  5477. /*
  5478. * mgsl_free_rx_frame_buffers()
  5479. *
  5480. * Free the receive buffers used by a received SDLC
  5481. * frame such that the buffers can be reused.
  5482. *
  5483. * Arguments:
  5484. *
  5485. * info pointer to device instance data
  5486. * StartIndex index of 1st receive buffer of frame
  5487. * EndIndex index of last receive buffer of frame
  5488. *
  5489. * Return Value: None
  5490. */
  5491. static void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex )
  5492. {
  5493. int Done = 0;
  5494. DMABUFFERENTRY *pBufEntry;
  5495. unsigned int Index;
  5496. /* Starting with 1st buffer entry of the frame clear the status */
  5497. /* field and set the count field to DMA Buffer Size. */
  5498. Index = StartIndex;
  5499. while( !Done ) {
  5500. pBufEntry = &(info->rx_buffer_list[Index]);
  5501. if ( Index == EndIndex ) {
  5502. /* This is the last buffer of the frame! */
  5503. Done = 1;
  5504. }
  5505. /* reset current buffer for reuse */
  5506. // pBufEntry->status = 0;
  5507. // pBufEntry->count = DMABUFFERSIZE;
  5508. *((unsigned long *)&(pBufEntry->count)) = DMABUFFERSIZE;
  5509. /* advance to next buffer entry in linked list */
  5510. Index++;
  5511. if ( Index == info->rx_buffer_count )
  5512. Index = 0;
  5513. }
  5514. /* set current buffer to next buffer after last buffer of frame */
  5515. info->current_rx_buffer = Index;
  5516. } /* end of free_rx_frame_buffers() */
  5517. /* mgsl_get_rx_frame()
  5518. *
  5519. * This function attempts to return a received SDLC frame from the
  5520. * receive DMA buffers. Only frames received without errors are returned.
  5521. *
  5522. * Arguments: info pointer to device extension
  5523. * Return Value: 1 if frame returned, otherwise 0
  5524. */
  5525. static int mgsl_get_rx_frame(struct mgsl_struct *info)
  5526. {
  5527. unsigned int StartIndex, EndIndex; /* index of 1st and last buffers of Rx frame */
  5528. unsigned short status;
  5529. DMABUFFERENTRY *pBufEntry;
  5530. unsigned int framesize = 0;
  5531. int ReturnCode = 0;
  5532. unsigned long flags;
  5533. struct tty_struct *tty = info->tty;
  5534. int return_frame = 0;
  5535. /*
  5536. * current_rx_buffer points to the 1st buffer of the next available
  5537. * receive frame. To find the last buffer of the frame look for
  5538. * a non-zero status field in the buffer entries. (The status
  5539. * field is set by the 16C32 after completing a receive frame.
  5540. */
  5541. StartIndex = EndIndex = info->current_rx_buffer;
  5542. while( !info->rx_buffer_list[EndIndex].status ) {
  5543. /*
  5544. * If the count field of the buffer entry is non-zero then
  5545. * this buffer has not been used. (The 16C32 clears the count
  5546. * field when it starts using the buffer.) If an unused buffer
  5547. * is encountered then there are no frames available.
  5548. */
  5549. if ( info->rx_buffer_list[EndIndex].count )
  5550. goto Cleanup;
  5551. /* advance to next buffer entry in linked list */
  5552. EndIndex++;
  5553. if ( EndIndex == info->rx_buffer_count )
  5554. EndIndex = 0;
  5555. /* if entire list searched then no frame available */
  5556. if ( EndIndex == StartIndex ) {
  5557. /* If this occurs then something bad happened,
  5558. * all buffers have been 'used' but none mark
  5559. * the end of a frame. Reset buffers and receiver.
  5560. */
  5561. if ( info->rx_enabled ){
  5562. spin_lock_irqsave(&info->irq_spinlock,flags);
  5563. usc_start_receiver(info);
  5564. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5565. }
  5566. goto Cleanup;
  5567. }
  5568. }
  5569. /* check status of receive frame */
  5570. status = info->rx_buffer_list[EndIndex].status;
  5571. if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN +
  5572. RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) {
  5573. if ( status & RXSTATUS_SHORT_FRAME )
  5574. info->icount.rxshort++;
  5575. else if ( status & RXSTATUS_ABORT )
  5576. info->icount.rxabort++;
  5577. else if ( status & RXSTATUS_OVERRUN )
  5578. info->icount.rxover++;
  5579. else {
  5580. info->icount.rxcrc++;
  5581. if ( info->params.crc_type & HDLC_CRC_RETURN_EX )
  5582. return_frame = 1;
  5583. }
  5584. framesize = 0;
  5585. #ifdef CONFIG_HDLC
  5586. {
  5587. struct net_device_stats *stats = hdlc_stats(info->netdev);
  5588. stats->rx_errors++;
  5589. stats->rx_frame_errors++;
  5590. }
  5591. #endif
  5592. } else
  5593. return_frame = 1;
  5594. if ( return_frame ) {
  5595. /* receive frame has no errors, get frame size.
  5596. * The frame size is the starting value of the RCC (which was
  5597. * set to 0xffff) minus the ending value of the RCC (decremented
  5598. * once for each receive character) minus 2 for the 16-bit CRC.
  5599. */
  5600. framesize = RCLRVALUE - info->rx_buffer_list[EndIndex].rcc;
  5601. /* adjust frame size for CRC if any */
  5602. if ( info->params.crc_type == HDLC_CRC_16_CCITT )
  5603. framesize -= 2;
  5604. else if ( info->params.crc_type == HDLC_CRC_32_CCITT )
  5605. framesize -= 4;
  5606. }
  5607. if ( debug_level >= DEBUG_LEVEL_BH )
  5608. printk("%s(%d):mgsl_get_rx_frame(%s) status=%04X size=%d\n",
  5609. __FILE__,__LINE__,info->device_name,status,framesize);
  5610. if ( debug_level >= DEBUG_LEVEL_DATA )
  5611. mgsl_trace_block(info,info->rx_buffer_list[StartIndex].virt_addr,
  5612. min_t(int, framesize, DMABUFFERSIZE),0);
  5613. if (framesize) {
  5614. if ( ( (info->params.crc_type & HDLC_CRC_RETURN_EX) &&
  5615. ((framesize+1) > info->max_frame_size) ) ||
  5616. (framesize > info->max_frame_size) )
  5617. info->icount.rxlong++;
  5618. else {
  5619. /* copy dma buffer(s) to contiguous intermediate buffer */
  5620. int copy_count = framesize;
  5621. int index = StartIndex;
  5622. unsigned char *ptmp = info->intermediate_rxbuffer;
  5623. if ( !(status & RXSTATUS_CRC_ERROR))
  5624. info->icount.rxok++;
  5625. while(copy_count) {
  5626. int partial_count;
  5627. if ( copy_count > DMABUFFERSIZE )
  5628. partial_count = DMABUFFERSIZE;
  5629. else
  5630. partial_count = copy_count;
  5631. pBufEntry = &(info->rx_buffer_list[index]);
  5632. memcpy( ptmp, pBufEntry->virt_addr, partial_count );
  5633. ptmp += partial_count;
  5634. copy_count -= partial_count;
  5635. if ( ++index == info->rx_buffer_count )
  5636. index = 0;
  5637. }
  5638. if ( info->params.crc_type & HDLC_CRC_RETURN_EX ) {
  5639. ++framesize;
  5640. *ptmp = (status & RXSTATUS_CRC_ERROR ?
  5641. RX_CRC_ERROR :
  5642. RX_OK);
  5643. if ( debug_level >= DEBUG_LEVEL_DATA )
  5644. printk("%s(%d):mgsl_get_rx_frame(%s) rx frame status=%d\n",
  5645. __FILE__,__LINE__,info->device_name,
  5646. *ptmp);
  5647. }
  5648. #ifdef CONFIG_HDLC
  5649. if (info->netcount)
  5650. hdlcdev_rx(info,info->intermediate_rxbuffer,framesize);
  5651. else
  5652. #endif
  5653. ldisc_receive_buf(tty, info->intermediate_rxbuffer, info->flag_buf, framesize);
  5654. }
  5655. }
  5656. /* Free the buffers used by this frame. */
  5657. mgsl_free_rx_frame_buffers( info, StartIndex, EndIndex );
  5658. ReturnCode = 1;
  5659. Cleanup:
  5660. if ( info->rx_enabled && info->rx_overflow ) {
  5661. /* The receiver needs to restarted because of
  5662. * a receive overflow (buffer or FIFO). If the
  5663. * receive buffers are now empty, then restart receiver.
  5664. */
  5665. if ( !info->rx_buffer_list[EndIndex].status &&
  5666. info->rx_buffer_list[EndIndex].count ) {
  5667. spin_lock_irqsave(&info->irq_spinlock,flags);
  5668. usc_start_receiver(info);
  5669. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5670. }
  5671. }
  5672. return ReturnCode;
  5673. } /* end of mgsl_get_rx_frame() */
  5674. /* mgsl_get_raw_rx_frame()
  5675. *
  5676. * This function attempts to return a received frame from the
  5677. * receive DMA buffers when running in external loop mode. In this mode,
  5678. * we will return at most one DMABUFFERSIZE frame to the application.
  5679. * The USC receiver is triggering off of DCD going active to start a new
  5680. * frame, and DCD going inactive to terminate the frame (similar to
  5681. * processing a closing flag character).
  5682. *
  5683. * In this routine, we will return DMABUFFERSIZE "chunks" at a time.
  5684. * If DCD goes inactive, the last Rx DMA Buffer will have a non-zero
  5685. * status field and the RCC field will indicate the length of the
  5686. * entire received frame. We take this RCC field and get the modulus
  5687. * of RCC and DMABUFFERSIZE to determine if number of bytes in the
  5688. * last Rx DMA buffer and return that last portion of the frame.
  5689. *
  5690. * Arguments: info pointer to device extension
  5691. * Return Value: 1 if frame returned, otherwise 0
  5692. */
  5693. static int mgsl_get_raw_rx_frame(struct mgsl_struct *info)
  5694. {
  5695. unsigned int CurrentIndex, NextIndex;
  5696. unsigned short status;
  5697. DMABUFFERENTRY *pBufEntry;
  5698. unsigned int framesize = 0;
  5699. int ReturnCode = 0;
  5700. unsigned long flags;
  5701. struct tty_struct *tty = info->tty;
  5702. /*
  5703. * current_rx_buffer points to the 1st buffer of the next available
  5704. * receive frame. The status field is set by the 16C32 after
  5705. * completing a receive frame. If the status field of this buffer
  5706. * is zero, either the USC is still filling this buffer or this
  5707. * is one of a series of buffers making up a received frame.
  5708. *
  5709. * If the count field of this buffer is zero, the USC is either
  5710. * using this buffer or has used this buffer. Look at the count
  5711. * field of the next buffer. If that next buffer's count is
  5712. * non-zero, the USC is still actively using the current buffer.
  5713. * Otherwise, if the next buffer's count field is zero, the
  5714. * current buffer is complete and the USC is using the next
  5715. * buffer.
  5716. */
  5717. CurrentIndex = NextIndex = info->current_rx_buffer;
  5718. ++NextIndex;
  5719. if ( NextIndex == info->rx_buffer_count )
  5720. NextIndex = 0;
  5721. if ( info->rx_buffer_list[CurrentIndex].status != 0 ||
  5722. (info->rx_buffer_list[CurrentIndex].count == 0 &&
  5723. info->rx_buffer_list[NextIndex].count == 0)) {
  5724. /*
  5725. * Either the status field of this dma buffer is non-zero
  5726. * (indicating the last buffer of a receive frame) or the next
  5727. * buffer is marked as in use -- implying this buffer is complete
  5728. * and an intermediate buffer for this received frame.
  5729. */
  5730. status = info->rx_buffer_list[CurrentIndex].status;
  5731. if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN +
  5732. RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) {
  5733. if ( status & RXSTATUS_SHORT_FRAME )
  5734. info->icount.rxshort++;
  5735. else if ( status & RXSTATUS_ABORT )
  5736. info->icount.rxabort++;
  5737. else if ( status & RXSTATUS_OVERRUN )
  5738. info->icount.rxover++;
  5739. else
  5740. info->icount.rxcrc++;
  5741. framesize = 0;
  5742. } else {
  5743. /*
  5744. * A receive frame is available, get frame size and status.
  5745. *
  5746. * The frame size is the starting value of the RCC (which was
  5747. * set to 0xffff) minus the ending value of the RCC (decremented
  5748. * once for each receive character) minus 2 or 4 for the 16-bit
  5749. * or 32-bit CRC.
  5750. *
  5751. * If the status field is zero, this is an intermediate buffer.
  5752. * It's size is 4K.
  5753. *
  5754. * If the DMA Buffer Entry's Status field is non-zero, the
  5755. * receive operation completed normally (ie: DCD dropped). The
  5756. * RCC field is valid and holds the received frame size.
  5757. * It is possible that the RCC field will be zero on a DMA buffer
  5758. * entry with a non-zero status. This can occur if the total
  5759. * frame size (number of bytes between the time DCD goes active
  5760. * to the time DCD goes inactive) exceeds 65535 bytes. In this
  5761. * case the 16C32 has underrun on the RCC count and appears to
  5762. * stop updating this counter to let us know the actual received
  5763. * frame size. If this happens (non-zero status and zero RCC),
  5764. * simply return the entire RxDMA Buffer
  5765. */
  5766. if ( status ) {
  5767. /*
  5768. * In the event that the final RxDMA Buffer is
  5769. * terminated with a non-zero status and the RCC
  5770. * field is zero, we interpret this as the RCC
  5771. * having underflowed (received frame > 65535 bytes).
  5772. *
  5773. * Signal the event to the user by passing back
  5774. * a status of RxStatus_CrcError returning the full
  5775. * buffer and let the app figure out what data is
  5776. * actually valid
  5777. */
  5778. if ( info->rx_buffer_list[CurrentIndex].rcc )
  5779. framesize = RCLRVALUE - info->rx_buffer_list[CurrentIndex].rcc;
  5780. else
  5781. framesize = DMABUFFERSIZE;
  5782. }
  5783. else
  5784. framesize = DMABUFFERSIZE;
  5785. }
  5786. if ( framesize > DMABUFFERSIZE ) {
  5787. /*
  5788. * if running in raw sync mode, ISR handler for
  5789. * End Of Buffer events terminates all buffers at 4K.
  5790. * If this frame size is said to be >4K, get the
  5791. * actual number of bytes of the frame in this buffer.
  5792. */
  5793. framesize = framesize % DMABUFFERSIZE;
  5794. }
  5795. if ( debug_level >= DEBUG_LEVEL_BH )
  5796. printk("%s(%d):mgsl_get_raw_rx_frame(%s) status=%04X size=%d\n",
  5797. __FILE__,__LINE__,info->device_name,status,framesize);
  5798. if ( debug_level >= DEBUG_LEVEL_DATA )
  5799. mgsl_trace_block(info,info->rx_buffer_list[CurrentIndex].virt_addr,
  5800. min_t(int, framesize, DMABUFFERSIZE),0);
  5801. if (framesize) {
  5802. /* copy dma buffer(s) to contiguous intermediate buffer */
  5803. /* NOTE: we never copy more than DMABUFFERSIZE bytes */
  5804. pBufEntry = &(info->rx_buffer_list[CurrentIndex]);
  5805. memcpy( info->intermediate_rxbuffer, pBufEntry->virt_addr, framesize);
  5806. info->icount.rxok++;
  5807. ldisc_receive_buf(tty, info->intermediate_rxbuffer, info->flag_buf, framesize);
  5808. }
  5809. /* Free the buffers used by this frame. */
  5810. mgsl_free_rx_frame_buffers( info, CurrentIndex, CurrentIndex );
  5811. ReturnCode = 1;
  5812. }
  5813. if ( info->rx_enabled && info->rx_overflow ) {
  5814. /* The receiver needs to restarted because of
  5815. * a receive overflow (buffer or FIFO). If the
  5816. * receive buffers are now empty, then restart receiver.
  5817. */
  5818. if ( !info->rx_buffer_list[CurrentIndex].status &&
  5819. info->rx_buffer_list[CurrentIndex].count ) {
  5820. spin_lock_irqsave(&info->irq_spinlock,flags);
  5821. usc_start_receiver(info);
  5822. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5823. }
  5824. }
  5825. return ReturnCode;
  5826. } /* end of mgsl_get_raw_rx_frame() */
  5827. /* mgsl_load_tx_dma_buffer()
  5828. *
  5829. * Load the transmit DMA buffer with the specified data.
  5830. *
  5831. * Arguments:
  5832. *
  5833. * info pointer to device extension
  5834. * Buffer pointer to buffer containing frame to load
  5835. * BufferSize size in bytes of frame in Buffer
  5836. *
  5837. * Return Value: None
  5838. */
  5839. static void mgsl_load_tx_dma_buffer(struct mgsl_struct *info,
  5840. const char *Buffer, unsigned int BufferSize)
  5841. {
  5842. unsigned short Copycount;
  5843. unsigned int i = 0;
  5844. DMABUFFERENTRY *pBufEntry;
  5845. if ( debug_level >= DEBUG_LEVEL_DATA )
  5846. mgsl_trace_block(info,Buffer, min_t(int, BufferSize, DMABUFFERSIZE), 1);
  5847. if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
  5848. /* set CMR:13 to start transmit when
  5849. * next GoAhead (abort) is received
  5850. */
  5851. info->cmr_value |= BIT13;
  5852. }
  5853. /* begin loading the frame in the next available tx dma
  5854. * buffer, remember it's starting location for setting
  5855. * up tx dma operation
  5856. */
  5857. i = info->current_tx_buffer;
  5858. info->start_tx_dma_buffer = i;
  5859. /* Setup the status and RCC (Frame Size) fields of the 1st */
  5860. /* buffer entry in the transmit DMA buffer list. */
  5861. info->tx_buffer_list[i].status = info->cmr_value & 0xf000;
  5862. info->tx_buffer_list[i].rcc = BufferSize;
  5863. info->tx_buffer_list[i].count = BufferSize;
  5864. /* Copy frame data from 1st source buffer to the DMA buffers. */
  5865. /* The frame data may span multiple DMA buffers. */
  5866. while( BufferSize ){
  5867. /* Get a pointer to next DMA buffer entry. */
  5868. pBufEntry = &info->tx_buffer_list[i++];
  5869. if ( i == info->tx_buffer_count )
  5870. i=0;
  5871. /* Calculate the number of bytes that can be copied from */
  5872. /* the source buffer to this DMA buffer. */
  5873. if ( BufferSize > DMABUFFERSIZE )
  5874. Copycount = DMABUFFERSIZE;
  5875. else
  5876. Copycount = BufferSize;
  5877. /* Actually copy data from source buffer to DMA buffer. */
  5878. /* Also set the data count for this individual DMA buffer. */
  5879. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  5880. mgsl_load_pci_memory(pBufEntry->virt_addr, Buffer,Copycount);
  5881. else
  5882. memcpy(pBufEntry->virt_addr, Buffer, Copycount);
  5883. pBufEntry->count = Copycount;
  5884. /* Advance source pointer and reduce remaining data count. */
  5885. Buffer += Copycount;
  5886. BufferSize -= Copycount;
  5887. ++info->tx_dma_buffers_used;
  5888. }
  5889. /* remember next available tx dma buffer */
  5890. info->current_tx_buffer = i;
  5891. } /* end of mgsl_load_tx_dma_buffer() */
  5892. /*
  5893. * mgsl_register_test()
  5894. *
  5895. * Performs a register test of the 16C32.
  5896. *
  5897. * Arguments: info pointer to device instance data
  5898. * Return Value: TRUE if test passed, otherwise FALSE
  5899. */
  5900. static BOOLEAN mgsl_register_test( struct mgsl_struct *info )
  5901. {
  5902. static unsigned short BitPatterns[] =
  5903. { 0x0000, 0xffff, 0xaaaa, 0x5555, 0x1234, 0x6969, 0x9696, 0x0f0f };
  5904. static unsigned int Patterncount = sizeof(BitPatterns)/sizeof(unsigned short);
  5905. unsigned int i;
  5906. BOOLEAN rc = TRUE;
  5907. unsigned long flags;
  5908. spin_lock_irqsave(&info->irq_spinlock,flags);
  5909. usc_reset(info);
  5910. /* Verify the reset state of some registers. */
  5911. if ( (usc_InReg( info, SICR ) != 0) ||
  5912. (usc_InReg( info, IVR ) != 0) ||
  5913. (usc_InDmaReg( info, DIVR ) != 0) ){
  5914. rc = FALSE;
  5915. }
  5916. if ( rc == TRUE ){
  5917. /* Write bit patterns to various registers but do it out of */
  5918. /* sync, then read back and verify values. */
  5919. for ( i = 0 ; i < Patterncount ; i++ ) {
  5920. usc_OutReg( info, TC0R, BitPatterns[i] );
  5921. usc_OutReg( info, TC1R, BitPatterns[(i+1)%Patterncount] );
  5922. usc_OutReg( info, TCLR, BitPatterns[(i+2)%Patterncount] );
  5923. usc_OutReg( info, RCLR, BitPatterns[(i+3)%Patterncount] );
  5924. usc_OutReg( info, RSR, BitPatterns[(i+4)%Patterncount] );
  5925. usc_OutDmaReg( info, TBCR, BitPatterns[(i+5)%Patterncount] );
  5926. if ( (usc_InReg( info, TC0R ) != BitPatterns[i]) ||
  5927. (usc_InReg( info, TC1R ) != BitPatterns[(i+1)%Patterncount]) ||
  5928. (usc_InReg( info, TCLR ) != BitPatterns[(i+2)%Patterncount]) ||
  5929. (usc_InReg( info, RCLR ) != BitPatterns[(i+3)%Patterncount]) ||
  5930. (usc_InReg( info, RSR ) != BitPatterns[(i+4)%Patterncount]) ||
  5931. (usc_InDmaReg( info, TBCR ) != BitPatterns[(i+5)%Patterncount]) ){
  5932. rc = FALSE;
  5933. break;
  5934. }
  5935. }
  5936. }
  5937. usc_reset(info);
  5938. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5939. return rc;
  5940. } /* end of mgsl_register_test() */
  5941. /* mgsl_irq_test() Perform interrupt test of the 16C32.
  5942. *
  5943. * Arguments: info pointer to device instance data
  5944. * Return Value: TRUE if test passed, otherwise FALSE
  5945. */
  5946. static BOOLEAN mgsl_irq_test( struct mgsl_struct *info )
  5947. {
  5948. unsigned long EndTime;
  5949. unsigned long flags;
  5950. spin_lock_irqsave(&info->irq_spinlock,flags);
  5951. usc_reset(info);
  5952. /*
  5953. * Setup 16C32 to interrupt on TxC pin (14MHz clock) transition.
  5954. * The ISR sets irq_occurred to 1.
  5955. */
  5956. info->irq_occurred = FALSE;
  5957. /* Enable INTEN gate for ISA adapter (Port 6, Bit12) */
  5958. /* Enable INTEN (Port 6, Bit12) */
  5959. /* This connects the IRQ request signal to the ISA bus */
  5960. /* on the ISA adapter. This has no effect for the PCI adapter */
  5961. usc_OutReg( info, PCR, (unsigned short)((usc_InReg(info, PCR) | BIT13) & ~BIT12) );
  5962. usc_EnableMasterIrqBit(info);
  5963. usc_EnableInterrupts(info, IO_PIN);
  5964. usc_ClearIrqPendingBits(info, IO_PIN);
  5965. usc_UnlatchIostatusBits(info, MISCSTATUS_TXC_LATCHED);
  5966. usc_EnableStatusIrqs(info, SICR_TXC_ACTIVE + SICR_TXC_INACTIVE);
  5967. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5968. EndTime=100;
  5969. while( EndTime-- && !info->irq_occurred ) {
  5970. msleep_interruptible(10);
  5971. }
  5972. spin_lock_irqsave(&info->irq_spinlock,flags);
  5973. usc_reset(info);
  5974. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5975. if ( !info->irq_occurred )
  5976. return FALSE;
  5977. else
  5978. return TRUE;
  5979. } /* end of mgsl_irq_test() */
  5980. /* mgsl_dma_test()
  5981. *
  5982. * Perform a DMA test of the 16C32. A small frame is
  5983. * transmitted via DMA from a transmit buffer to a receive buffer
  5984. * using single buffer DMA mode.
  5985. *
  5986. * Arguments: info pointer to device instance data
  5987. * Return Value: TRUE if test passed, otherwise FALSE
  5988. */
  5989. static BOOLEAN mgsl_dma_test( struct mgsl_struct *info )
  5990. {
  5991. unsigned short FifoLevel;
  5992. unsigned long phys_addr;
  5993. unsigned int FrameSize;
  5994. unsigned int i;
  5995. char *TmpPtr;
  5996. BOOLEAN rc = TRUE;
  5997. unsigned short status=0;
  5998. unsigned long EndTime;
  5999. unsigned long flags;
  6000. MGSL_PARAMS tmp_params;
  6001. /* save current port options */
  6002. memcpy(&tmp_params,&info->params,sizeof(MGSL_PARAMS));
  6003. /* load default port options */
  6004. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  6005. #define TESTFRAMESIZE 40
  6006. spin_lock_irqsave(&info->irq_spinlock,flags);
  6007. /* setup 16C32 for SDLC DMA transfer mode */
  6008. usc_reset(info);
  6009. usc_set_sdlc_mode(info);
  6010. usc_enable_loopback(info,1);
  6011. /* Reprogram the RDMR so that the 16C32 does NOT clear the count
  6012. * field of the buffer entry after fetching buffer address. This
  6013. * way we can detect a DMA failure for a DMA read (which should be
  6014. * non-destructive to system memory) before we try and write to
  6015. * memory (where a failure could corrupt system memory).
  6016. */
  6017. /* Receive DMA mode Register (RDMR)
  6018. *
  6019. * <15..14> 11 DMA mode = Linked List Buffer mode
  6020. * <13> 1 RSBinA/L = store Rx status Block in List entry
  6021. * <12> 0 1 = Clear count of List Entry after fetching
  6022. * <11..10> 00 Address mode = Increment
  6023. * <9> 1 Terminate Buffer on RxBound
  6024. * <8> 0 Bus Width = 16bits
  6025. * <7..0> ? status Bits (write as 0s)
  6026. *
  6027. * 1110 0010 0000 0000 = 0xe200
  6028. */
  6029. usc_OutDmaReg( info, RDMR, 0xe200 );
  6030. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6031. /* SETUP TRANSMIT AND RECEIVE DMA BUFFERS */
  6032. FrameSize = TESTFRAMESIZE;
  6033. /* setup 1st transmit buffer entry: */
  6034. /* with frame size and transmit control word */
  6035. info->tx_buffer_list[0].count = FrameSize;
  6036. info->tx_buffer_list[0].rcc = FrameSize;
  6037. info->tx_buffer_list[0].status = 0x4000;
  6038. /* build a transmit frame in 1st transmit DMA buffer */
  6039. TmpPtr = info->tx_buffer_list[0].virt_addr;
  6040. for (i = 0; i < FrameSize; i++ )
  6041. *TmpPtr++ = i;
  6042. /* setup 1st receive buffer entry: */
  6043. /* clear status, set max receive buffer size */
  6044. info->rx_buffer_list[0].status = 0;
  6045. info->rx_buffer_list[0].count = FrameSize + 4;
  6046. /* zero out the 1st receive buffer */
  6047. memset( info->rx_buffer_list[0].virt_addr, 0, FrameSize + 4 );
  6048. /* Set count field of next buffer entries to prevent */
  6049. /* 16C32 from using buffers after the 1st one. */
  6050. info->tx_buffer_list[1].count = 0;
  6051. info->rx_buffer_list[1].count = 0;
  6052. /***************************/
  6053. /* Program 16C32 receiver. */
  6054. /***************************/
  6055. spin_lock_irqsave(&info->irq_spinlock,flags);
  6056. /* setup DMA transfers */
  6057. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  6058. /* program 16C32 receiver with physical address of 1st DMA buffer entry */
  6059. phys_addr = info->rx_buffer_list[0].phys_entry;
  6060. usc_OutDmaReg( info, NRARL, (unsigned short)phys_addr );
  6061. usc_OutDmaReg( info, NRARU, (unsigned short)(phys_addr >> 16) );
  6062. /* Clear the Rx DMA status bits (read RDMR) and start channel */
  6063. usc_InDmaReg( info, RDMR );
  6064. usc_DmaCmd( info, DmaCmd_InitRxChannel );
  6065. /* Enable Receiver (RMR <1..0> = 10) */
  6066. usc_OutReg( info, RMR, (unsigned short)((usc_InReg(info, RMR) & 0xfffc) | 0x0002) );
  6067. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6068. /*************************************************************/
  6069. /* WAIT FOR RECEIVER TO DMA ALL PARAMETERS FROM BUFFER ENTRY */
  6070. /*************************************************************/
  6071. /* Wait 100ms for interrupt. */
  6072. EndTime = jiffies + msecs_to_jiffies(100);
  6073. for(;;) {
  6074. if (time_after(jiffies, EndTime)) {
  6075. rc = FALSE;
  6076. break;
  6077. }
  6078. spin_lock_irqsave(&info->irq_spinlock,flags);
  6079. status = usc_InDmaReg( info, RDMR );
  6080. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6081. if ( !(status & BIT4) && (status & BIT5) ) {
  6082. /* INITG (BIT 4) is inactive (no entry read in progress) AND */
  6083. /* BUSY (BIT 5) is active (channel still active). */
  6084. /* This means the buffer entry read has completed. */
  6085. break;
  6086. }
  6087. }
  6088. /******************************/
  6089. /* Program 16C32 transmitter. */
  6090. /******************************/
  6091. spin_lock_irqsave(&info->irq_spinlock,flags);
  6092. /* Program the Transmit Character Length Register (TCLR) */
  6093. /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
  6094. usc_OutReg( info, TCLR, (unsigned short)info->tx_buffer_list[0].count );
  6095. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  6096. /* Program the address of the 1st DMA Buffer Entry in linked list */
  6097. phys_addr = info->tx_buffer_list[0].phys_entry;
  6098. usc_OutDmaReg( info, NTARL, (unsigned short)phys_addr );
  6099. usc_OutDmaReg( info, NTARU, (unsigned short)(phys_addr >> 16) );
  6100. /* unlatch Tx status bits, and start transmit channel. */
  6101. usc_OutReg( info, TCSR, (unsigned short)(( usc_InReg(info, TCSR) & 0x0f00) | 0xfa) );
  6102. usc_DmaCmd( info, DmaCmd_InitTxChannel );
  6103. /* wait for DMA controller to fill transmit FIFO */
  6104. usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
  6105. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6106. /**********************************/
  6107. /* WAIT FOR TRANSMIT FIFO TO FILL */
  6108. /**********************************/
  6109. /* Wait 100ms */
  6110. EndTime = jiffies + msecs_to_jiffies(100);
  6111. for(;;) {
  6112. if (time_after(jiffies, EndTime)) {
  6113. rc = FALSE;
  6114. break;
  6115. }
  6116. spin_lock_irqsave(&info->irq_spinlock,flags);
  6117. FifoLevel = usc_InReg(info, TICR) >> 8;
  6118. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6119. if ( FifoLevel < 16 )
  6120. break;
  6121. else
  6122. if ( FrameSize < 32 ) {
  6123. /* This frame is smaller than the entire transmit FIFO */
  6124. /* so wait for the entire frame to be loaded. */
  6125. if ( FifoLevel <= (32 - FrameSize) )
  6126. break;
  6127. }
  6128. }
  6129. if ( rc == TRUE )
  6130. {
  6131. /* Enable 16C32 transmitter. */
  6132. spin_lock_irqsave(&info->irq_spinlock,flags);
  6133. /* Transmit mode Register (TMR), <1..0> = 10, Enable Transmitter */
  6134. usc_TCmd( info, TCmd_SendFrame );
  6135. usc_OutReg( info, TMR, (unsigned short)((usc_InReg(info, TMR) & 0xfffc) | 0x0002) );
  6136. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6137. /******************************/
  6138. /* WAIT FOR TRANSMIT COMPLETE */
  6139. /******************************/
  6140. /* Wait 100ms */
  6141. EndTime = jiffies + msecs_to_jiffies(100);
  6142. /* While timer not expired wait for transmit complete */
  6143. spin_lock_irqsave(&info->irq_spinlock,flags);
  6144. status = usc_InReg( info, TCSR );
  6145. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6146. while ( !(status & (BIT6+BIT5+BIT4+BIT2+BIT1)) ) {
  6147. if (time_after(jiffies, EndTime)) {
  6148. rc = FALSE;
  6149. break;
  6150. }
  6151. spin_lock_irqsave(&info->irq_spinlock,flags);
  6152. status = usc_InReg( info, TCSR );
  6153. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6154. }
  6155. }
  6156. if ( rc == TRUE ){
  6157. /* CHECK FOR TRANSMIT ERRORS */
  6158. if ( status & (BIT5 + BIT1) )
  6159. rc = FALSE;
  6160. }
  6161. if ( rc == TRUE ) {
  6162. /* WAIT FOR RECEIVE COMPLETE */
  6163. /* Wait 100ms */
  6164. EndTime = jiffies + msecs_to_jiffies(100);
  6165. /* Wait for 16C32 to write receive status to buffer entry. */
  6166. status=info->rx_buffer_list[0].status;
  6167. while ( status == 0 ) {
  6168. if (time_after(jiffies, EndTime)) {
  6169. rc = FALSE;
  6170. break;
  6171. }
  6172. status=info->rx_buffer_list[0].status;
  6173. }
  6174. }
  6175. if ( rc == TRUE ) {
  6176. /* CHECK FOR RECEIVE ERRORS */
  6177. status = info->rx_buffer_list[0].status;
  6178. if ( status & (BIT8 + BIT3 + BIT1) ) {
  6179. /* receive error has occurred */
  6180. rc = FALSE;
  6181. } else {
  6182. if ( memcmp( info->tx_buffer_list[0].virt_addr ,
  6183. info->rx_buffer_list[0].virt_addr, FrameSize ) ){
  6184. rc = FALSE;
  6185. }
  6186. }
  6187. }
  6188. spin_lock_irqsave(&info->irq_spinlock,flags);
  6189. usc_reset( info );
  6190. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6191. /* restore current port options */
  6192. memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
  6193. return rc;
  6194. } /* end of mgsl_dma_test() */
  6195. /* mgsl_adapter_test()
  6196. *
  6197. * Perform the register, IRQ, and DMA tests for the 16C32.
  6198. *
  6199. * Arguments: info pointer to device instance data
  6200. * Return Value: 0 if success, otherwise -ENODEV
  6201. */
  6202. static int mgsl_adapter_test( struct mgsl_struct *info )
  6203. {
  6204. if ( debug_level >= DEBUG_LEVEL_INFO )
  6205. printk( "%s(%d):Testing device %s\n",
  6206. __FILE__,__LINE__,info->device_name );
  6207. if ( !mgsl_register_test( info ) ) {
  6208. info->init_error = DiagStatus_AddressFailure;
  6209. printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
  6210. __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
  6211. return -ENODEV;
  6212. }
  6213. if ( !mgsl_irq_test( info ) ) {
  6214. info->init_error = DiagStatus_IrqFailure;
  6215. printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
  6216. __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
  6217. return -ENODEV;
  6218. }
  6219. if ( !mgsl_dma_test( info ) ) {
  6220. info->init_error = DiagStatus_DmaFailure;
  6221. printk( "%s(%d):DMA test failure for device %s DMA=%d\n",
  6222. __FILE__,__LINE__,info->device_name, (unsigned short)(info->dma_level) );
  6223. return -ENODEV;
  6224. }
  6225. if ( debug_level >= DEBUG_LEVEL_INFO )
  6226. printk( "%s(%d):device %s passed diagnostics\n",
  6227. __FILE__,__LINE__,info->device_name );
  6228. return 0;
  6229. } /* end of mgsl_adapter_test() */
  6230. /* mgsl_memory_test()
  6231. *
  6232. * Test the shared memory on a PCI adapter.
  6233. *
  6234. * Arguments: info pointer to device instance data
  6235. * Return Value: TRUE if test passed, otherwise FALSE
  6236. */
  6237. static BOOLEAN mgsl_memory_test( struct mgsl_struct *info )
  6238. {
  6239. static unsigned long BitPatterns[] = { 0x0, 0x55555555, 0xaaaaaaaa,
  6240. 0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
  6241. unsigned long Patterncount = sizeof(BitPatterns)/sizeof(unsigned long);
  6242. unsigned long i;
  6243. unsigned long TestLimit = SHARED_MEM_ADDRESS_SIZE/sizeof(unsigned long);
  6244. unsigned long * TestAddr;
  6245. if ( info->bus_type != MGSL_BUS_TYPE_PCI )
  6246. return TRUE;
  6247. TestAddr = (unsigned long *)info->memory_base;
  6248. /* Test data lines with test pattern at one location. */
  6249. for ( i = 0 ; i < Patterncount ; i++ ) {
  6250. *TestAddr = BitPatterns[i];
  6251. if ( *TestAddr != BitPatterns[i] )
  6252. return FALSE;
  6253. }
  6254. /* Test address lines with incrementing pattern over */
  6255. /* entire address range. */
  6256. for ( i = 0 ; i < TestLimit ; i++ ) {
  6257. *TestAddr = i * 4;
  6258. TestAddr++;
  6259. }
  6260. TestAddr = (unsigned long *)info->memory_base;
  6261. for ( i = 0 ; i < TestLimit ; i++ ) {
  6262. if ( *TestAddr != i * 4 )
  6263. return FALSE;
  6264. TestAddr++;
  6265. }
  6266. memset( info->memory_base, 0, SHARED_MEM_ADDRESS_SIZE );
  6267. return TRUE;
  6268. } /* End Of mgsl_memory_test() */
  6269. /* mgsl_load_pci_memory()
  6270. *
  6271. * Load a large block of data into the PCI shared memory.
  6272. * Use this instead of memcpy() or memmove() to move data
  6273. * into the PCI shared memory.
  6274. *
  6275. * Notes:
  6276. *
  6277. * This function prevents the PCI9050 interface chip from hogging
  6278. * the adapter local bus, which can starve the 16C32 by preventing
  6279. * 16C32 bus master cycles.
  6280. *
  6281. * The PCI9050 documentation says that the 9050 will always release
  6282. * control of the local bus after completing the current read
  6283. * or write operation.
  6284. *
  6285. * It appears that as long as the PCI9050 write FIFO is full, the
  6286. * PCI9050 treats all of the writes as a single burst transaction
  6287. * and will not release the bus. This causes DMA latency problems
  6288. * at high speeds when copying large data blocks to the shared
  6289. * memory.
  6290. *
  6291. * This function in effect, breaks the a large shared memory write
  6292. * into multiple transations by interleaving a shared memory read
  6293. * which will flush the write FIFO and 'complete' the write
  6294. * transation. This allows any pending DMA request to gain control
  6295. * of the local bus in a timely fasion.
  6296. *
  6297. * Arguments:
  6298. *
  6299. * TargetPtr pointer to target address in PCI shared memory
  6300. * SourcePtr pointer to source buffer for data
  6301. * count count in bytes of data to copy
  6302. *
  6303. * Return Value: None
  6304. */
  6305. static void mgsl_load_pci_memory( char* TargetPtr, const char* SourcePtr,
  6306. unsigned short count )
  6307. {
  6308. /* 16 32-bit writes @ 60ns each = 960ns max latency on local bus */
  6309. #define PCI_LOAD_INTERVAL 64
  6310. unsigned short Intervalcount = count / PCI_LOAD_INTERVAL;
  6311. unsigned short Index;
  6312. unsigned long Dummy;
  6313. for ( Index = 0 ; Index < Intervalcount ; Index++ )
  6314. {
  6315. memcpy(TargetPtr, SourcePtr, PCI_LOAD_INTERVAL);
  6316. Dummy = *((volatile unsigned long *)TargetPtr);
  6317. TargetPtr += PCI_LOAD_INTERVAL;
  6318. SourcePtr += PCI_LOAD_INTERVAL;
  6319. }
  6320. memcpy( TargetPtr, SourcePtr, count % PCI_LOAD_INTERVAL );
  6321. } /* End Of mgsl_load_pci_memory() */
  6322. static void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit)
  6323. {
  6324. int i;
  6325. int linecount;
  6326. if (xmit)
  6327. printk("%s tx data:\n",info->device_name);
  6328. else
  6329. printk("%s rx data:\n",info->device_name);
  6330. while(count) {
  6331. if (count > 16)
  6332. linecount = 16;
  6333. else
  6334. linecount = count;
  6335. for(i=0;i<linecount;i++)
  6336. printk("%02X ",(unsigned char)data[i]);
  6337. for(;i<17;i++)
  6338. printk(" ");
  6339. for(i=0;i<linecount;i++) {
  6340. if (data[i]>=040 && data[i]<=0176)
  6341. printk("%c",data[i]);
  6342. else
  6343. printk(".");
  6344. }
  6345. printk("\n");
  6346. data += linecount;
  6347. count -= linecount;
  6348. }
  6349. } /* end of mgsl_trace_block() */
  6350. /* mgsl_tx_timeout()
  6351. *
  6352. * called when HDLC frame times out
  6353. * update stats and do tx completion processing
  6354. *
  6355. * Arguments: context pointer to device instance data
  6356. * Return Value: None
  6357. */
  6358. static void mgsl_tx_timeout(unsigned long context)
  6359. {
  6360. struct mgsl_struct *info = (struct mgsl_struct*)context;
  6361. unsigned long flags;
  6362. if ( debug_level >= DEBUG_LEVEL_INFO )
  6363. printk( "%s(%d):mgsl_tx_timeout(%s)\n",
  6364. __FILE__,__LINE__,info->device_name);
  6365. if(info->tx_active &&
  6366. (info->params.mode == MGSL_MODE_HDLC ||
  6367. info->params.mode == MGSL_MODE_RAW) ) {
  6368. info->icount.txtimeout++;
  6369. }
  6370. spin_lock_irqsave(&info->irq_spinlock,flags);
  6371. info->tx_active = 0;
  6372. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  6373. if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
  6374. usc_loopmode_cancel_transmit( info );
  6375. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6376. #ifdef CONFIG_HDLC
  6377. if (info->netcount)
  6378. hdlcdev_tx_done(info);
  6379. else
  6380. #endif
  6381. mgsl_bh_transmit(info);
  6382. } /* end of mgsl_tx_timeout() */
  6383. /* signal that there are no more frames to send, so that
  6384. * line is 'released' by echoing RxD to TxD when current
  6385. * transmission is complete (or immediately if no tx in progress).
  6386. */
  6387. static int mgsl_loopmode_send_done( struct mgsl_struct * info )
  6388. {
  6389. unsigned long flags;
  6390. spin_lock_irqsave(&info->irq_spinlock,flags);
  6391. if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
  6392. if (info->tx_active)
  6393. info->loopmode_send_done_requested = TRUE;
  6394. else
  6395. usc_loopmode_send_done(info);
  6396. }
  6397. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6398. return 0;
  6399. }
  6400. /* release the line by echoing RxD to TxD
  6401. * upon completion of a transmit frame
  6402. */
  6403. static void usc_loopmode_send_done( struct mgsl_struct * info )
  6404. {
  6405. info->loopmode_send_done_requested = FALSE;
  6406. /* clear CMR:13 to 0 to start echoing RxData to TxData */
  6407. info->cmr_value &= ~BIT13;
  6408. usc_OutReg(info, CMR, info->cmr_value);
  6409. }
  6410. /* abort a transmit in progress while in HDLC LoopMode
  6411. */
  6412. static void usc_loopmode_cancel_transmit( struct mgsl_struct * info )
  6413. {
  6414. /* reset tx dma channel and purge TxFifo */
  6415. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  6416. usc_DmaCmd( info, DmaCmd_ResetTxChannel );
  6417. usc_loopmode_send_done( info );
  6418. }
  6419. /* for HDLC/SDLC LoopMode, setting CMR:13 after the transmitter is enabled
  6420. * is an Insert Into Loop action. Upon receipt of a GoAhead sequence (RxAbort)
  6421. * we must clear CMR:13 to begin repeating TxData to RxData
  6422. */
  6423. static void usc_loopmode_insert_request( struct mgsl_struct * info )
  6424. {
  6425. info->loopmode_insert_requested = TRUE;
  6426. /* enable RxAbort irq. On next RxAbort, clear CMR:13 to
  6427. * begin repeating TxData on RxData (complete insertion)
  6428. */
  6429. usc_OutReg( info, RICR,
  6430. (usc_InReg( info, RICR ) | RXSTATUS_ABORT_RECEIVED ) );
  6431. /* set CMR:13 to insert into loop on next GoAhead (RxAbort) */
  6432. info->cmr_value |= BIT13;
  6433. usc_OutReg(info, CMR, info->cmr_value);
  6434. }
  6435. /* return 1 if station is inserted into the loop, otherwise 0
  6436. */
  6437. static int usc_loopmode_active( struct mgsl_struct * info)
  6438. {
  6439. return usc_InReg( info, CCSR ) & BIT7 ? 1 : 0 ;
  6440. }
  6441. #ifdef CONFIG_HDLC
  6442. /**
  6443. * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
  6444. * set encoding and frame check sequence (FCS) options
  6445. *
  6446. * dev pointer to network device structure
  6447. * encoding serial encoding setting
  6448. * parity FCS setting
  6449. *
  6450. * returns 0 if success, otherwise error code
  6451. */
  6452. static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
  6453. unsigned short parity)
  6454. {
  6455. struct mgsl_struct *info = dev_to_port(dev);
  6456. unsigned char new_encoding;
  6457. unsigned short new_crctype;
  6458. /* return error if TTY interface open */
  6459. if (info->count)
  6460. return -EBUSY;
  6461. switch (encoding)
  6462. {
  6463. case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
  6464. case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
  6465. case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
  6466. case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
  6467. case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
  6468. default: return -EINVAL;
  6469. }
  6470. switch (parity)
  6471. {
  6472. case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
  6473. case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
  6474. case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
  6475. default: return -EINVAL;
  6476. }
  6477. info->params.encoding = new_encoding;
  6478. info->params.crc_type = new_crctype;;
  6479. /* if network interface up, reprogram hardware */
  6480. if (info->netcount)
  6481. mgsl_program_hw(info);
  6482. return 0;
  6483. }
  6484. /**
  6485. * called by generic HDLC layer to send frame
  6486. *
  6487. * skb socket buffer containing HDLC frame
  6488. * dev pointer to network device structure
  6489. *
  6490. * returns 0 if success, otherwise error code
  6491. */
  6492. static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
  6493. {
  6494. struct mgsl_struct *info = dev_to_port(dev);
  6495. struct net_device_stats *stats = hdlc_stats(dev);
  6496. unsigned long flags;
  6497. if (debug_level >= DEBUG_LEVEL_INFO)
  6498. printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
  6499. /* stop sending until this frame completes */
  6500. netif_stop_queue(dev);
  6501. /* copy data to device buffers */
  6502. info->xmit_cnt = skb->len;
  6503. mgsl_load_tx_dma_buffer(info, skb->data, skb->len);
  6504. /* update network statistics */
  6505. stats->tx_packets++;
  6506. stats->tx_bytes += skb->len;
  6507. /* done with socket buffer, so free it */
  6508. dev_kfree_skb(skb);
  6509. /* save start time for transmit timeout detection */
  6510. dev->trans_start = jiffies;
  6511. /* start hardware transmitter if necessary */
  6512. spin_lock_irqsave(&info->irq_spinlock,flags);
  6513. if (!info->tx_active)
  6514. usc_start_transmitter(info);
  6515. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6516. return 0;
  6517. }
  6518. /**
  6519. * called by network layer when interface enabled
  6520. * claim resources and initialize hardware
  6521. *
  6522. * dev pointer to network device structure
  6523. *
  6524. * returns 0 if success, otherwise error code
  6525. */
  6526. static int hdlcdev_open(struct net_device *dev)
  6527. {
  6528. struct mgsl_struct *info = dev_to_port(dev);
  6529. int rc;
  6530. unsigned long flags;
  6531. if (debug_level >= DEBUG_LEVEL_INFO)
  6532. printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
  6533. /* generic HDLC layer open processing */
  6534. if ((rc = hdlc_open(dev)))
  6535. return rc;
  6536. /* arbitrate between network and tty opens */
  6537. spin_lock_irqsave(&info->netlock, flags);
  6538. if (info->count != 0 || info->netcount != 0) {
  6539. printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
  6540. spin_unlock_irqrestore(&info->netlock, flags);
  6541. return -EBUSY;
  6542. }
  6543. info->netcount=1;
  6544. spin_unlock_irqrestore(&info->netlock, flags);
  6545. /* claim resources and init adapter */
  6546. if ((rc = startup(info)) != 0) {
  6547. spin_lock_irqsave(&info->netlock, flags);
  6548. info->netcount=0;
  6549. spin_unlock_irqrestore(&info->netlock, flags);
  6550. return rc;
  6551. }
  6552. /* assert DTR and RTS, apply hardware settings */
  6553. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  6554. mgsl_program_hw(info);
  6555. /* enable network layer transmit */
  6556. dev->trans_start = jiffies;
  6557. netif_start_queue(dev);
  6558. /* inform generic HDLC layer of current DCD status */
  6559. spin_lock_irqsave(&info->irq_spinlock, flags);
  6560. usc_get_serial_signals(info);
  6561. spin_unlock_irqrestore(&info->irq_spinlock, flags);
  6562. hdlc_set_carrier(info->serial_signals & SerialSignal_DCD, dev);
  6563. return 0;
  6564. }
  6565. /**
  6566. * called by network layer when interface is disabled
  6567. * shutdown hardware and release resources
  6568. *
  6569. * dev pointer to network device structure
  6570. *
  6571. * returns 0 if success, otherwise error code
  6572. */
  6573. static int hdlcdev_close(struct net_device *dev)
  6574. {
  6575. struct mgsl_struct *info = dev_to_port(dev);
  6576. unsigned long flags;
  6577. if (debug_level >= DEBUG_LEVEL_INFO)
  6578. printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
  6579. netif_stop_queue(dev);
  6580. /* shutdown adapter and release resources */
  6581. shutdown(info);
  6582. hdlc_close(dev);
  6583. spin_lock_irqsave(&info->netlock, flags);
  6584. info->netcount=0;
  6585. spin_unlock_irqrestore(&info->netlock, flags);
  6586. return 0;
  6587. }
  6588. /**
  6589. * called by network layer to process IOCTL call to network device
  6590. *
  6591. * dev pointer to network device structure
  6592. * ifr pointer to network interface request structure
  6593. * cmd IOCTL command code
  6594. *
  6595. * returns 0 if success, otherwise error code
  6596. */
  6597. static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  6598. {
  6599. const size_t size = sizeof(sync_serial_settings);
  6600. sync_serial_settings new_line;
  6601. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  6602. struct mgsl_struct *info = dev_to_port(dev);
  6603. unsigned int flags;
  6604. if (debug_level >= DEBUG_LEVEL_INFO)
  6605. printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
  6606. /* return error if TTY interface open */
  6607. if (info->count)
  6608. return -EBUSY;
  6609. if (cmd != SIOCWANDEV)
  6610. return hdlc_ioctl(dev, ifr, cmd);
  6611. switch(ifr->ifr_settings.type) {
  6612. case IF_GET_IFACE: /* return current sync_serial_settings */
  6613. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  6614. if (ifr->ifr_settings.size < size) {
  6615. ifr->ifr_settings.size = size; /* data size wanted */
  6616. return -ENOBUFS;
  6617. }
  6618. flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  6619. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  6620. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  6621. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  6622. switch (flags){
  6623. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
  6624. case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
  6625. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
  6626. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
  6627. default: new_line.clock_type = CLOCK_DEFAULT;
  6628. }
  6629. new_line.clock_rate = info->params.clock_speed;
  6630. new_line.loopback = info->params.loopback ? 1:0;
  6631. if (copy_to_user(line, &new_line, size))
  6632. return -EFAULT;
  6633. return 0;
  6634. case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
  6635. if(!capable(CAP_NET_ADMIN))
  6636. return -EPERM;
  6637. if (copy_from_user(&new_line, line, size))
  6638. return -EFAULT;
  6639. switch (new_line.clock_type)
  6640. {
  6641. case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
  6642. case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
  6643. case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
  6644. case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
  6645. case CLOCK_DEFAULT: flags = info->params.flags &
  6646. (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  6647. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  6648. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  6649. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
  6650. default: return -EINVAL;
  6651. }
  6652. if (new_line.loopback != 0 && new_line.loopback != 1)
  6653. return -EINVAL;
  6654. info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  6655. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  6656. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  6657. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  6658. info->params.flags |= flags;
  6659. info->params.loopback = new_line.loopback;
  6660. if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
  6661. info->params.clock_speed = new_line.clock_rate;
  6662. else
  6663. info->params.clock_speed = 0;
  6664. /* if network interface up, reprogram hardware */
  6665. if (info->netcount)
  6666. mgsl_program_hw(info);
  6667. return 0;
  6668. default:
  6669. return hdlc_ioctl(dev, ifr, cmd);
  6670. }
  6671. }
  6672. /**
  6673. * called by network layer when transmit timeout is detected
  6674. *
  6675. * dev pointer to network device structure
  6676. */
  6677. static void hdlcdev_tx_timeout(struct net_device *dev)
  6678. {
  6679. struct mgsl_struct *info = dev_to_port(dev);
  6680. struct net_device_stats *stats = hdlc_stats(dev);
  6681. unsigned long flags;
  6682. if (debug_level >= DEBUG_LEVEL_INFO)
  6683. printk("hdlcdev_tx_timeout(%s)\n",dev->name);
  6684. stats->tx_errors++;
  6685. stats->tx_aborted_errors++;
  6686. spin_lock_irqsave(&info->irq_spinlock,flags);
  6687. usc_stop_transmitter(info);
  6688. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6689. netif_wake_queue(dev);
  6690. }
  6691. /**
  6692. * called by device driver when transmit completes
  6693. * reenable network layer transmit if stopped
  6694. *
  6695. * info pointer to device instance information
  6696. */
  6697. static void hdlcdev_tx_done(struct mgsl_struct *info)
  6698. {
  6699. if (netif_queue_stopped(info->netdev))
  6700. netif_wake_queue(info->netdev);
  6701. }
  6702. /**
  6703. * called by device driver when frame received
  6704. * pass frame to network layer
  6705. *
  6706. * info pointer to device instance information
  6707. * buf pointer to buffer contianing frame data
  6708. * size count of data bytes in buf
  6709. */
  6710. static void hdlcdev_rx(struct mgsl_struct *info, char *buf, int size)
  6711. {
  6712. struct sk_buff *skb = dev_alloc_skb(size);
  6713. struct net_device *dev = info->netdev;
  6714. struct net_device_stats *stats = hdlc_stats(dev);
  6715. if (debug_level >= DEBUG_LEVEL_INFO)
  6716. printk("hdlcdev_rx(%s)\n",dev->name);
  6717. if (skb == NULL) {
  6718. printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
  6719. stats->rx_dropped++;
  6720. return;
  6721. }
  6722. memcpy(skb_put(skb, size),buf,size);
  6723. skb->protocol = hdlc_type_trans(skb, info->netdev);
  6724. stats->rx_packets++;
  6725. stats->rx_bytes += size;
  6726. netif_rx(skb);
  6727. info->netdev->last_rx = jiffies;
  6728. }
  6729. /**
  6730. * called by device driver when adding device instance
  6731. * do generic HDLC initialization
  6732. *
  6733. * info pointer to device instance information
  6734. *
  6735. * returns 0 if success, otherwise error code
  6736. */
  6737. static int hdlcdev_init(struct mgsl_struct *info)
  6738. {
  6739. int rc;
  6740. struct net_device *dev;
  6741. hdlc_device *hdlc;
  6742. /* allocate and initialize network and HDLC layer objects */
  6743. if (!(dev = alloc_hdlcdev(info))) {
  6744. printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
  6745. return -ENOMEM;
  6746. }
  6747. /* for network layer reporting purposes only */
  6748. dev->base_addr = info->io_base;
  6749. dev->irq = info->irq_level;
  6750. dev->dma = info->dma_level;
  6751. /* network layer callbacks and settings */
  6752. dev->do_ioctl = hdlcdev_ioctl;
  6753. dev->open = hdlcdev_open;
  6754. dev->stop = hdlcdev_close;
  6755. dev->tx_timeout = hdlcdev_tx_timeout;
  6756. dev->watchdog_timeo = 10*HZ;
  6757. dev->tx_queue_len = 50;
  6758. /* generic HDLC layer callbacks and settings */
  6759. hdlc = dev_to_hdlc(dev);
  6760. hdlc->attach = hdlcdev_attach;
  6761. hdlc->xmit = hdlcdev_xmit;
  6762. /* register objects with HDLC layer */
  6763. if ((rc = register_hdlc_device(dev))) {
  6764. printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
  6765. free_netdev(dev);
  6766. return rc;
  6767. }
  6768. info->netdev = dev;
  6769. return 0;
  6770. }
  6771. /**
  6772. * called by device driver when removing device instance
  6773. * do generic HDLC cleanup
  6774. *
  6775. * info pointer to device instance information
  6776. */
  6777. static void hdlcdev_exit(struct mgsl_struct *info)
  6778. {
  6779. unregister_hdlc_device(info->netdev);
  6780. free_netdev(info->netdev);
  6781. info->netdev = NULL;
  6782. }
  6783. #endif /* CONFIG_HDLC */
  6784. static int __devinit synclink_init_one (struct pci_dev *dev,
  6785. const struct pci_device_id *ent)
  6786. {
  6787. struct mgsl_struct *info;
  6788. if (pci_enable_device(dev)) {
  6789. printk("error enabling pci device %p\n", dev);
  6790. return -EIO;
  6791. }
  6792. if (!(info = mgsl_allocate_device())) {
  6793. printk("can't allocate device instance data.\n");
  6794. return -EIO;
  6795. }
  6796. /* Copy user configuration info to device instance data */
  6797. info->io_base = pci_resource_start(dev, 2);
  6798. info->irq_level = dev->irq;
  6799. info->phys_memory_base = pci_resource_start(dev, 3);
  6800. /* Because veremap only works on page boundaries we must map
  6801. * a larger area than is actually implemented for the LCR
  6802. * memory range. We map a full page starting at the page boundary.
  6803. */
  6804. info->phys_lcr_base = pci_resource_start(dev, 0);
  6805. info->lcr_offset = info->phys_lcr_base & (PAGE_SIZE-1);
  6806. info->phys_lcr_base &= ~(PAGE_SIZE-1);
  6807. info->bus_type = MGSL_BUS_TYPE_PCI;
  6808. info->io_addr_size = 8;
  6809. info->irq_flags = SA_SHIRQ;
  6810. if (dev->device == 0x0210) {
  6811. /* Version 1 PCI9030 based universal PCI adapter */
  6812. info->misc_ctrl_value = 0x007c4080;
  6813. info->hw_version = 1;
  6814. } else {
  6815. /* Version 0 PCI9050 based 5V PCI adapter
  6816. * A PCI9050 bug prevents reading LCR registers if
  6817. * LCR base address bit 7 is set. Maintain shadow
  6818. * value so we can write to LCR misc control reg.
  6819. */
  6820. info->misc_ctrl_value = 0x087e4546;
  6821. info->hw_version = 0;
  6822. }
  6823. mgsl_add_device(info);
  6824. return 0;
  6825. }
  6826. static void __devexit synclink_remove_one (struct pci_dev *dev)
  6827. {
  6828. }