synclink_cs.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:118k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/drivers/char/synclink_cs.c
  3.  *
  4.  * $Id: synclink_cs.c,v 3.4 2002/04/22 14:36:41 paulkf Exp $
  5.  *
  6.  * Device driver for Microgate SyncLink PC Card
  7.  * multiprotocol serial adapter.
  8.  *
  9.  * written by Paul Fulghum for Microgate Corporation
  10.  * paulkf@microgate.com
  11.  *
  12.  * Microgate and SyncLink are trademarks of Microgate Corporation
  13.  *
  14.  * This code is released under the GNU General Public License (GPL)
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19.  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20.  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  26.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28. #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
  29. #if defined(__i386__)
  30. #  define BREAKPOINT() asm("   int $3");
  31. #else
  32. #  define BREAKPOINT() { }
  33. #endif
  34. #define MAX_DEVICE_COUNT 4
  35. #include <linux/config.h>
  36. #include <linux/module.h>
  37. #include <linux/version.h>
  38. #include <linux/errno.h>
  39. #include <linux/signal.h>
  40. #include <linux/sched.h>
  41. #include <linux/timer.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/pci.h>
  44. #include <linux/tty.h>
  45. #include <linux/tty_flip.h>
  46. #include <linux/serial.h>
  47. #include <linux/major.h>
  48. #include <linux/string.h>
  49. #include <linux/fcntl.h>
  50. #include <linux/ptrace.h>
  51. #include <linux/ioport.h>
  52. #include <linux/mm.h>
  53. #include <linux/slab.h>
  54. #include <linux/netdevice.h>
  55. #include <linux/vmalloc.h>
  56. #include <linux/init.h>
  57. #include <asm/serial.h>
  58. #include <linux/delay.h>
  59. #include <linux/ioctl.h>
  60. #include <asm/system.h>
  61. #include <asm/io.h>
  62. #include <asm/irq.h>
  63. #include <asm/dma.h>
  64. #include <asm/bitops.h>
  65. #include <asm/types.h>
  66. #include <linux/termios.h>
  67. #include <linux/tqueue.h>
  68. #include <pcmcia/version.h>
  69. #include <pcmcia/cs_types.h>
  70. #include <pcmcia/cs.h>
  71. #include <pcmcia/cistpl.h>
  72. #include <pcmcia/cisreg.h>
  73. #include <pcmcia/ds.h>
  74. #include <pcmcia/bus_ops.h>
  75. #ifdef CONFIG_SYNCLINK_SYNCPPP_MODULE
  76. #define CONFIG_SYNCLINK_SYNCPPP 1
  77. #endif
  78. #ifdef CONFIG_SYNCLINK_SYNCPPP
  79. #if LINUX_VERSION_CODE < VERSION(2,4,3) 
  80. #include "../net/wan/syncppp.h"
  81. #else
  82. #include <net/syncppp.h>
  83. #endif
  84. #endif
  85. #include <asm/segment.h>
  86. #define GET_USER(error,value,addr) error = get_user(value,addr)
  87. #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
  88. #define PUT_USER(error,value,addr) error = put_user(value,addr)
  89. #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
  90. #include <asm/uaccess.h>
  91. #include "linux/synclink.h"
  92. static MGSL_PARAMS default_params = {
  93. MGSL_MODE_HDLC, /* unsigned long mode */
  94. 0, /* unsigned char loopback; */
  95. HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
  96. HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
  97. 0, /* unsigned long clock_speed; */
  98. 0xff, /* unsigned char addr_filter; */
  99. HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
  100. HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
  101. HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
  102. 9600, /* unsigned long data_rate; */
  103. 8, /* unsigned char data_bits; */
  104. 1, /* unsigned char stop_bits; */
  105. ASYNC_PARITY_NONE /* unsigned char parity; */
  106. };
  107. typedef struct
  108. {
  109. int count;
  110. unsigned char status;
  111. char data[1];
  112. } RXBUF;
  113. /* The queue of BH actions to be performed */
  114. #define BH_RECEIVE  1
  115. #define BH_TRANSMIT 2
  116. #define BH_STATUS   4
  117. #define IO_PIN_SHUTDOWN_LIMIT 100
  118. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  119. struct _input_signal_events {
  120. int ri_up;
  121. int ri_down;
  122. int dsr_up;
  123. int dsr_down;
  124. int dcd_up;
  125. int dcd_down;
  126. int cts_up;
  127. int cts_down;
  128. };
  129. /*
  130.  * Device instance data structure
  131.  */
  132.  
  133. typedef struct _mgslpc_info {
  134. void *if_ptr; /* General purpose pointer (used by SPPP) */
  135. int magic;
  136. int flags;
  137. int count; /* count of opens */
  138. int line;
  139. unsigned short close_delay;
  140. unsigned short closing_wait; /* time to wait before closing */
  141. struct mgsl_icount icount;
  142. struct termios normal_termios;
  143. struct termios callout_termios;
  144. struct tty_struct  *tty;
  145. int timeout;
  146. int x_char; /* xon/xoff character */
  147. int blocked_open; /* # of blocked opens */
  148. long session; /* Session of opening process */
  149. long pgrp; /* pgrp of opening process */
  150. unsigned char read_status_mask;
  151. unsigned char ignore_status_mask;
  152. unsigned char *tx_buf;
  153. int            tx_put;
  154. int            tx_get;
  155. int            tx_count;
  156. /* circular list of fixed length rx buffers */
  157. unsigned char  *rx_buf;        /* memory allocated for all rx buffers */
  158. int            rx_buf_total_size; /* size of memory allocated for rx buffers */
  159. int            rx_put;         /* index of next empty rx buffer */
  160. int            rx_get;         /* index of next full rx buffer */
  161. int            rx_buf_size;    /* size in bytes of single rx buffer */
  162. int            rx_buf_count;   /* total number of rx buffers */
  163. int            rx_frame_count; /* number of full rx buffers */
  164. wait_queue_head_t open_wait;
  165. wait_queue_head_t close_wait;
  166. wait_queue_head_t status_event_wait_q;
  167. wait_queue_head_t event_wait_q;
  168. struct timer_list tx_timer; /* HDLC transmit timeout timer */
  169. struct _mgslpc_info *next_device; /* device list link */
  170. unsigned short imra_value;
  171. unsigned short imrb_value;
  172. unsigned char  pim_value;
  173. spinlock_t lock;
  174. struct tq_struct task; /* task structure for scheduling bh */
  175. u32 max_frame_size;
  176. u32 pending_bh;
  177. int bh_running;
  178. int bh_requested;
  179. int dcd_chkcount; /* check counts to prevent */
  180. int cts_chkcount; /* too many IRQs if a signal */
  181. int dsr_chkcount; /* is floating */
  182. int ri_chkcount;
  183. int rx_enabled;
  184. int rx_overflow;
  185. int tx_enabled;
  186. int tx_active;
  187. int tx_aborting;
  188. u32 idle_mode;
  189. int if_mode; /* serial interface selection (RS-232, v.35 etc) */
  190. char device_name[25]; /* device instance name */
  191. unsigned int io_base; /* base I/O address of adapter */
  192. unsigned int irq_level;
  193. MGSL_PARAMS params; /* communications parameters */
  194. unsigned char serial_signals; /* current serial signal states */
  195. char irq_occurred; /* for diagnostics use */
  196. char testing_irq;
  197. unsigned int init_error; /* startup error (DIAGS) */
  198. char flag_buf[MAX_ASYNC_BUFFER_SIZE];
  199. BOOLEAN drop_rts_on_tx_done;
  200. struct _input_signal_events input_signal_events;
  201. /* PCMCIA support */
  202. dev_link_t       link;
  203. dev_node_t       node;
  204. int       stop;
  205. struct bus_operations *bus;
  206. /* SPPP/Cisco HDLC device parts */
  207. int netcount;
  208. int dosyncppp;
  209. spinlock_t netlock;
  210. #ifdef CONFIG_SYNCLINK_SYNCPPP
  211. struct ppp_device pppdev;
  212. char netname[10];
  213. struct net_device *netdev;
  214. struct net_device_stats netstats;
  215. struct net_device netdevice;
  216. #endif
  217. } MGSLPC_INFO;
  218. #define MGSLPC_MAGIC 0x5402
  219. /*
  220.  * The size of the serial xmit buffer is 1 page, or 4096 bytes
  221.  */
  222. #define TXBUFSIZE 4096
  223.     
  224. #define CHA     0x00   /* channel A offset */
  225. #define CHB     0x40   /* channel B offset */
  226.     
  227. #define RXFIFO  0
  228. #define TXFIFO  0
  229. #define STAR    0x20
  230. #define CMDR    0x20
  231. #define RSTA    0x21
  232. #define PRE     0x21
  233. #define MODE    0x22
  234. #define TIMR    0x23
  235. #define XAD1    0x24
  236. #define XAD2    0x25
  237. #define RAH1    0x26
  238. #define RAH2    0x27
  239. #define DAFO    0x27
  240. #define RAL1    0x28
  241. #define RFC     0x28
  242. #define RHCR    0x29
  243. #define RAL2    0x29
  244. #define RBCL    0x2a
  245. #define XBCL    0x2a
  246. #define RBCH    0x2b
  247. #define XBCH    0x2b
  248. #define CCR0    0x2c
  249. #define CCR1    0x2d
  250. #define CCR2    0x2e
  251. #define CCR3    0x2f
  252. #define VSTR    0x34
  253. #define BGR     0x34
  254. #define RLCR    0x35
  255. #define AML     0x36
  256. #define AMH     0x37
  257. #define GIS     0x38
  258. #define IVA     0x38
  259. #define IPC     0x39
  260. #define ISR     0x3a
  261. #define IMR     0x3a
  262. #define PVR     0x3c
  263. #define PIS     0x3d
  264. #define PIM     0x3d
  265. #define PCR     0x3e
  266. #define CCR4    0x3f
  267.     
  268. // IMR/ISR
  269.     
  270. #define IRQ_BREAK_ON    BIT15   // rx break detected
  271. #define IRQ_DATAOVERRUN BIT14 // receive data overflow
  272. #define IRQ_ALLSENT     BIT13 // all sent
  273. #define IRQ_UNDERRUN    BIT12 // transmit data underrun
  274. #define IRQ_TIMER       BIT11 // timer interrupt
  275. #define IRQ_CTS         BIT10 // CTS status change
  276. #define IRQ_TXREPEAT    BIT9 // tx message repeat
  277. #define IRQ_TXFIFO      BIT8 // transmit pool ready
  278. #define IRQ_RXEOM       BIT7 // receive message end
  279. #define IRQ_EXITHUNT    BIT6 // receive frame start
  280. #define IRQ_RXTIME      BIT6    // rx char timeout
  281. #define IRQ_DCD         BIT2 // carrier detect status change
  282. #define IRQ_OVERRUN     BIT1 // receive frame overflow
  283. #define IRQ_RXFIFO      BIT0 // receive pool full
  284.     
  285. // STAR
  286.     
  287. #define XFW   BIT6 // transmit FIFO write enable
  288. #define CEC   BIT2 // command executing
  289. #define CTS   BIT1 // CTS state
  290.     
  291. #define PVR_DTR      BIT0
  292. #define PVR_DSR      BIT1
  293. #define PVR_RI       BIT2
  294. #define PVR_AUTOCTS  BIT3
  295. #define PVR_RS232    0x20   /* 0010b */
  296. #define PVR_V35      0xe0   /* 1110b */
  297. #define PVR_RS422    0x40   /* 0100b */
  298.     
  299. /* Register access functions */ 
  300.     
  301. #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
  302. #define read_reg(info, reg) inb((info)->io_base + (reg))
  303. #define read_reg16(info, reg) inw((info)->io_base + (reg))  
  304. #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
  305.     
  306. #define set_reg_bits(info, reg, mask) 
  307.     write_reg(info, (reg), 
  308.  (unsigned char) (read_reg(info, (reg)) | (mask)))  
  309. #define clear_reg_bits(info, reg, mask) 
  310.     write_reg(info, (reg), 
  311.  (unsigned char) (read_reg(info, (reg)) & ~(mask)))  
  312. /*
  313.  * interrupt enable/disable routines
  314.  */ 
  315. static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask) 
  316. {
  317. if (channel == CHA) {
  318. info->imra_value |= mask;
  319. write_reg16(info, CHA + IMR, info->imra_value);
  320. } else {
  321. info->imrb_value |= mask;
  322. write_reg16(info, CHB + IMR, info->imrb_value);
  323. }
  324. }
  325. static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask) 
  326. {
  327. if (channel == CHA) {
  328. info->imra_value &= ~mask;
  329. write_reg16(info, CHA + IMR, info->imra_value);
  330. } else {
  331. info->imrb_value &= ~mask;
  332. write_reg16(info, CHB + IMR, info->imrb_value);
  333. }
  334. }
  335. #define port_irq_disable(info, mask) 
  336.   { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
  337. #define port_irq_enable(info, mask) 
  338.   { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
  339. static void rx_start(MGSLPC_INFO *info);
  340. static void rx_stop(MGSLPC_INFO *info);
  341. static void tx_start(MGSLPC_INFO *info);
  342. static void tx_stop(MGSLPC_INFO *info);
  343. static void tx_set_idle(MGSLPC_INFO *info);
  344. static void get_signals(MGSLPC_INFO *info);
  345. static void set_signals(MGSLPC_INFO *info);
  346. static void reset_device(MGSLPC_INFO *info);
  347. static void hdlc_mode(MGSLPC_INFO *info);
  348. static void async_mode(MGSLPC_INFO *info);
  349. static void tx_timeout(unsigned long context);
  350. static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg);
  351. #ifdef CONFIG_SYNCLINK_SYNCPPP
  352. /* SPPP/HDLC stuff */
  353. static void mgslpc_sppp_init(MGSLPC_INFO *info);
  354. static void mgslpc_sppp_delete(MGSLPC_INFO *info);
  355. static int  mgslpc_sppp_open(struct net_device *d);
  356. static int  mgslpc_sppp_close(struct net_device *d);
  357. static void mgslpc_sppp_tx_timeout(struct net_device *d);
  358. static int  mgslpc_sppp_tx(struct sk_buff *skb, struct net_device *d);
  359. static void mgslpc_sppp_rx_done(MGSLPC_INFO *info, char *buf, int size);
  360. static void mgslpc_sppp_tx_done(MGSLPC_INFO *info);
  361. static int  mgslpc_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  362. struct net_device_stats *mgslpc_net_stats(struct net_device *dev);
  363. #endif
  364. static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
  365. static BOOLEAN register_test(MGSLPC_INFO *info);
  366. static BOOLEAN irq_test(MGSLPC_INFO *info);
  367. static int adapter_test(MGSLPC_INFO *info);
  368. static int claim_resources(MGSLPC_INFO *info);
  369. static void release_resources(MGSLPC_INFO *info);
  370. static void mgslpc_add_device(MGSLPC_INFO *info);
  371. static void mgslpc_remove_device(MGSLPC_INFO *info);
  372. static int  rx_get_frame(MGSLPC_INFO *info);
  373. static void rx_reset_buffers(MGSLPC_INFO *info);
  374. static int  rx_alloc_buffers(MGSLPC_INFO *info);
  375. static void rx_free_buffers(MGSLPC_INFO *info);
  376. static void mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs);
  377. /*
  378.  * Bottom half interrupt handlers
  379.  */
  380. static void bh_handler(void* Context);
  381. static void bh_transmit(MGSLPC_INFO *info);
  382. static void bh_status(MGSLPC_INFO *info);
  383. /*
  384.  * ioctl handlers
  385.  */
  386. static int set_modem_info(MGSLPC_INFO *info, unsigned int cmd,
  387.   unsigned int *value);
  388. static int get_modem_info(MGSLPC_INFO *info, unsigned int *value);
  389. static int get_stats(MGSLPC_INFO *info, struct mgsl_icount *user_icount);
  390. static int get_params(MGSLPC_INFO *info, MGSL_PARAMS *user_params);
  391. static int set_params(MGSLPC_INFO *info, MGSL_PARAMS *new_params);
  392. static int get_txidle(MGSLPC_INFO *info, int*idle_mode);
  393. static int set_txidle(MGSLPC_INFO *info, int idle_mode);
  394. static int set_txenable(MGSLPC_INFO *info, int enable);
  395. static int tx_abort(MGSLPC_INFO *info);
  396. static int set_rxenable(MGSLPC_INFO *info, int enable);
  397. static int wait_events(MGSLPC_INFO *info, int *mask);
  398. #define jiffies_from_ms(a) ((((a) * HZ)/1000)+1)
  399. static MGSLPC_INFO *mgslpc_device_list = NULL;
  400. static int mgslpc_device_count = 0;
  401. /*
  402.  * Set this param to non-zero to load eax with the
  403.  * .text section address and breakpoint on module load.
  404.  * This is useful for use with gdb and add-symbol-file command.
  405.  */
  406. static int break_on_load=0;
  407. /*
  408.  * Driver major number, defaults to zero to get auto
  409.  * assigned major number. May be forced as module parameter.
  410.  */
  411. static int ttymajor=0;
  412. static int cuamajor=0;
  413. static int debug_level = 0;
  414. static int maxframe[MAX_DEVICE_COUNT] = {0,};
  415. /* The old way: bit map of interrupts to choose from */
  416. /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, and 3 */
  417. static u_int irq_mask = 0xdeb8;
  418. /* Newer, simpler way of listing specific interrupts */
  419. static int irq_list[4] = { -1 };
  420. MODULE_PARM(irq_mask, "i");
  421. MODULE_PARM(irq_list, "1-4i");
  422. MODULE_PARM(break_on_load,"i");
  423. MODULE_PARM(ttymajor,"i");
  424. MODULE_PARM(cuamajor,"i");
  425. MODULE_PARM(debug_level,"i");
  426. MODULE_PARM(maxframe,"1-" __MODULE_STRING(MAX_DEVICE_COUNT) "i");
  427. static char *driver_name = "SyncLink PC Card driver";
  428. static char *driver_version = "$Revision: 3.4 $";
  429. static struct tty_driver serial_driver, callout_driver;
  430. static int serial_refcount;
  431. /* number of characters left in xmit buffer before we ask for more */
  432. #define WAKEUP_CHARS 256
  433. static void mgslpc_change_params(MGSLPC_INFO *info);
  434. static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
  435. static struct tty_struct *serial_table[MAX_DEVICE_COUNT];
  436. static struct termios *serial_termios[MAX_DEVICE_COUNT];
  437. static struct termios *serial_termios_locked[MAX_DEVICE_COUNT];
  438. #ifndef MIN
  439. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  440. #endif
  441. /* PCMCIA prototypes */
  442. static void mgslpc_config(dev_link_t *link);
  443. static void mgslpc_release(u_long arg);
  444. static int  mgslpc_event(event_t event, int priority,
  445.  event_callback_args_t *args);
  446. static dev_link_t *mgslpc_attach(void);
  447. static void mgslpc_detach(dev_link_t *);
  448. static dev_info_t dev_info = "synclink_cs";
  449. static dev_link_t *dev_list = NULL;
  450. static void cs_error(client_handle_t handle, int func, int ret)
  451. {
  452.     error_info_t err = { func, ret };
  453.     CardServices(ReportError, handle, &err);
  454. }
  455. /*
  456.  * 1st function defined in .text section. Calling this function in
  457.  * init_module() followed by a breakpoint allows a remote debugger
  458.  * (gdb) to get the .text address for the add-symbol-file command.
  459.  * This allows remote debugging of dynamically loadable modules.
  460.  */
  461. static void* mgslpc_get_text_ptr(void);
  462. static void* mgslpc_get_text_ptr() {return mgslpc_get_text_ptr;}
  463. static dev_link_t *mgslpc_attach(void)
  464. {
  465.     MGSLPC_INFO *info;
  466.     dev_link_t *link;
  467.     client_reg_t client_reg;
  468.     int ret, i;
  469.     
  470.     if (debug_level >= DEBUG_LEVEL_INFO)
  471.     printk("mgslpc_attachn");
  472.     info = (MGSLPC_INFO *)kmalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
  473.     if (!info) {
  474.     printk("Error can't allocate device instance datan");
  475.     return NULL;
  476.     }
  477.     memset(info, 0, sizeof(MGSLPC_INFO));
  478.     info->magic = MGSLPC_MAGIC;
  479.     info->task.sync = 0;
  480.     info->task.routine = bh_handler;
  481.     info->task.data    = info;
  482.     info->max_frame_size = 4096;
  483.     info->close_delay = 5*HZ/10;
  484.     info->closing_wait = 30*HZ;
  485.     init_waitqueue_head(&info->open_wait);
  486.     init_waitqueue_head(&info->close_wait);
  487.     init_waitqueue_head(&info->status_event_wait_q);
  488.     init_waitqueue_head(&info->event_wait_q);
  489.     spin_lock_init(&info->lock);
  490.     spin_lock_init(&info->netlock);
  491.     memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  492.     info->idle_mode = HDLC_TXIDLE_FLAGS;
  493.     info->imra_value = 0xffff;
  494.     info->imrb_value = 0xffff;
  495.     info->pim_value = 0xff;
  496.     link = &info->link;
  497.     link->priv = info;
  498.     
  499.     /* Initialize the dev_link_t structure */
  500.     link->release.function = &mgslpc_release;
  501.     link->release.data = (u_long)link;
  502.     /* Interrupt setup */
  503.     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  504.     link->irq.IRQInfo1   = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
  505.     if (irq_list[0] == -1)
  506.     link->irq.IRQInfo2 = irq_mask;
  507.     else
  508.     for (i = 0; i < 4; i++)
  509.     link->irq.IRQInfo2 |= 1 << irq_list[i];
  510.     link->irq.Handler = NULL;
  511.     
  512.     link->conf.Attributes = 0;
  513.     link->conf.Vcc = 50;
  514.     link->conf.IntType = INT_MEMORY_AND_IO;
  515.     /* Register with Card Services */
  516.     link->next = dev_list;
  517.     dev_list = link;
  518.     client_reg.dev_info = &dev_info;
  519.     client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
  520.     client_reg.EventMask =
  521.     CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
  522.     CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
  523.     CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
  524.     client_reg.event_handler = &mgslpc_event;
  525.     client_reg.Version = 0x0210;
  526.     client_reg.event_callback_args.client_data = link;
  527.     ret = CardServices(RegisterClient, &link->handle, &client_reg);
  528.     if (ret != CS_SUCCESS) {
  529.     cs_error(link->handle, RegisterClient, ret);
  530.     mgslpc_detach(link);
  531.     return NULL;
  532.     }
  533.     mgslpc_add_device(info);
  534.     memset(serial_table,0,sizeof(struct tty_struct*)*MAX_DEVICE_COUNT);
  535.     memset(serial_termios,0,sizeof(struct termios*)*MAX_DEVICE_COUNT);
  536.     memset(serial_termios_locked,0,sizeof(struct termios*)*MAX_DEVICE_COUNT);
  537.     info->callout_termios = callout_driver.init_termios;
  538.     info->normal_termios  = serial_driver.init_termios;
  539.     return link;
  540. }
  541. /* Card has been inserted.
  542.  */
  543. #define CS_CHECK(fn, args...) 
  544. while ((last_ret=CardServices(last_fn=(fn),args))!=0) goto cs_failed
  545. static void mgslpc_config(dev_link_t *link)
  546. {
  547.     client_handle_t handle = link->handle;
  548.     MGSLPC_INFO *info = link->priv;
  549.     tuple_t tuple;
  550.     cisparse_t parse;
  551.     int last_fn, last_ret;
  552.     u_char buf[64];
  553.     config_info_t conf;
  554.     cistpl_cftable_entry_t dflt = { 0 };
  555.     cistpl_cftable_entry_t *cfg;
  556.     
  557.     if (debug_level >= DEBUG_LEVEL_INFO)
  558.     printk("mgslpc_config(0x%p)n", link);
  559.     /* read CONFIG tuple to find its configuration registers */
  560.     tuple.DesiredTuple = CISTPL_CONFIG;
  561.     tuple.Attributes = 0;
  562.     tuple.TupleData = buf;
  563.     tuple.TupleDataMax = sizeof(buf);
  564.     tuple.TupleOffset = 0;
  565.     CS_CHECK(GetFirstTuple, handle, &tuple);
  566.     CS_CHECK(GetTupleData, handle, &tuple);
  567.     CS_CHECK(ParseTuple, handle, &tuple, &parse);
  568.     link->conf.ConfigBase = parse.config.base;
  569.     link->conf.Present = parse.config.rmask[0];
  570.     
  571.     /* Configure card */
  572.     link->state |= DEV_CONFIG;
  573.     /* Look up the current Vcc */
  574.     CS_CHECK(GetConfigurationInfo, handle, &conf);
  575.     link->conf.Vcc = conf.Vcc;
  576.     /* get CIS configuration entry */
  577.     tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  578.     CS_CHECK(GetFirstTuple, handle, &tuple);
  579.     cfg = &(parse.cftable_entry);
  580.     CS_CHECK(GetTupleData, handle, &tuple);
  581.     CS_CHECK(ParseTuple, handle, &tuple, &parse);
  582.     if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
  583.     if (cfg->index == 0)
  584.     goto cs_failed;
  585.     link->conf.ConfigIndex = cfg->index;
  586.     link->conf.Attributes |= CONF_ENABLE_IRQ;
  587.     /* IO window settings */
  588.     link->io.NumPorts1 = 0;
  589.     if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
  590.     cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
  591.     link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  592.     if (!(io->flags & CISTPL_IO_8BIT))
  593.     link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
  594.     if (!(io->flags & CISTPL_IO_16BIT))
  595.     link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  596.     link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
  597.     link->io.BasePort1 = io->win[0].base;
  598.     link->io.NumPorts1 = io->win[0].len;
  599.     CS_CHECK(RequestIO, link->handle, &link->io);
  600.     }
  601.     link->conf.Attributes = CONF_ENABLE_IRQ;
  602.     link->conf.Vcc = 50;
  603.     link->conf.IntType = INT_MEMORY_AND_IO;
  604.     link->conf.ConfigIndex = 8;
  605.     link->conf.Present = PRESENT_OPTION;
  606.     
  607.     link->irq.Attributes |= IRQ_HANDLE_PRESENT;
  608.     link->irq.Handler     = mgslpc_isr;
  609.     link->irq.Instance    = info;
  610.     CS_CHECK(RequestIRQ, link->handle, &link->irq);
  611.     CS_CHECK(RequestConfiguration, link->handle, &link->conf);
  612.     info->io_base = link->io.BasePort1;
  613.     info->irq_level = link->irq.AssignedIRQ;
  614.     /* add to linked list of devices */
  615.     sprintf(info->node.dev_name, "mgslpc0");
  616.     info->node.major = info->node.minor = 0;
  617.     link->dev = &info->node;
  618.     printk(KERN_INFO "%s: index 0x%02x:",
  619.    info->node.dev_name, link->conf.ConfigIndex);
  620.     if (link->conf.Attributes & CONF_ENABLE_IRQ)
  621.     printk(", irq %d", link->irq.AssignedIRQ);
  622.     if (link->io.NumPorts1)
  623.     printk(", io 0x%04x-0x%04x", link->io.BasePort1,
  624.    link->io.BasePort1+link->io.NumPorts1-1);
  625.     printk("n");
  626.     
  627.     link->state &= ~DEV_CONFIG_PENDING;
  628.     return;
  629. cs_failed:
  630.     cs_error(link->handle, last_fn, last_ret);
  631.     mgslpc_release((u_long)link);
  632. }
  633. /* Card has been removed.
  634.  * Unregister device and release PCMCIA configuration.
  635.  * If device is open, postpone until it is closed.
  636.  */
  637. static void mgslpc_release(u_long arg)
  638. {
  639.     dev_link_t *link = (dev_link_t *)arg;
  640.     if (debug_level >= DEBUG_LEVEL_INFO)
  641.     printk("mgslpc_release(0x%p)n", link);
  642.     if (link->open) {
  643.     if (debug_level >= DEBUG_LEVEL_INFO)
  644.     printk("synclink_cs: release postponed, '%s' still openn",
  645.    link->dev->dev_name);
  646.     link->state |= DEV_STALE_CONFIG;
  647.     return;
  648.     }
  649.     /* Unlink the device chain */
  650.     link->dev = NULL;
  651.     link->state &= ~DEV_CONFIG;
  652.     CardServices(ReleaseConfiguration, link->handle);
  653.     if (link->io.NumPorts1)
  654.     CardServices(ReleaseIO, link->handle, &link->io);
  655.     if (link->irq.AssignedIRQ)
  656.     CardServices(ReleaseIRQ, link->handle, &link->irq);
  657.     if (link->state & DEV_STALE_LINK)
  658.     mgslpc_detach(link);
  659. }
  660. static void mgslpc_detach(dev_link_t *link)
  661. {
  662.     dev_link_t **linkp;
  663.     if (debug_level >= DEBUG_LEVEL_INFO)
  664.     printk("mgslpc_detach(0x%p)n", link);
  665.     
  666.     /* find device */
  667.     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  668.     if (*linkp == link) break;
  669.     if (*linkp == NULL)
  670.     return;
  671.     if (link->state & DEV_CONFIG) {
  672.     /* device is configured/active, mark it so when
  673.      * release() is called a proper detach() occurs.
  674.      */
  675.     if (debug_level >= DEBUG_LEVEL_INFO)
  676.     printk(KERN_DEBUG "synclinkpc: detach postponed, '%s' "
  677.    "still lockedn", link->dev->dev_name);
  678.     link->state |= DEV_STALE_LINK;
  679.     return;
  680.     }
  681.     /* Break the link with Card Services */
  682.     if (link->handle)
  683.     CardServices(DeregisterClient, link->handle);
  684.     
  685.     /* Unlink device structure, and free it */
  686.     *linkp = link->next;
  687.     mgslpc_remove_device((MGSLPC_INFO *)link->priv);
  688. }
  689. static int mgslpc_event(event_t event, int priority,
  690. event_callback_args_t *args)
  691. {
  692.     dev_link_t *link = args->client_data;
  693.     MGSLPC_INFO *info = link->priv;
  694.     
  695.     if (debug_level >= DEBUG_LEVEL_INFO)
  696.     printk("mgslpc_event(0x%06x)n", event);
  697.     
  698.     switch (event) {
  699.     case CS_EVENT_CARD_REMOVAL:
  700.     link->state &= ~DEV_PRESENT;
  701.     if (link->state & DEV_CONFIG) {
  702.     ((MGSLPC_INFO *)link->priv)->stop = 1;
  703.     mod_timer(&link->release, jiffies + HZ/20);
  704.     }
  705.     break;
  706.     case CS_EVENT_CARD_INSERTION:
  707.     link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  708.     info->bus = args->bus;
  709.     mgslpc_config(link);
  710.     break;
  711.     case CS_EVENT_PM_SUSPEND:
  712.     link->state |= DEV_SUSPEND;
  713.     /* Fall through... */
  714.     case CS_EVENT_RESET_PHYSICAL:
  715.     /* Mark the device as stopped, to block IO until later */
  716.     info->stop = 1;
  717.     if (link->state & DEV_CONFIG)
  718.     CardServices(ReleaseConfiguration, link->handle);
  719.     break;
  720.     case CS_EVENT_PM_RESUME:
  721.     link->state &= ~DEV_SUSPEND;
  722.     /* Fall through... */
  723.     case CS_EVENT_CARD_RESET:
  724.     if (link->state & DEV_CONFIG)
  725.     CardServices(RequestConfiguration, link->handle, &link->conf);
  726.     info->stop = 0;
  727.     break;
  728.     }
  729.     return 0;
  730. }
  731. static inline int mgslpc_paranoia_check(MGSLPC_INFO *info,
  732. kdev_t device, const char *routine)
  733. {
  734. #ifdef MGSLPC_PARANOIA_CHECK
  735. static const char *badmagic =
  736. "Warning: bad magic number for mgsl struct (%s) in %sn";
  737. static const char *badinfo =
  738. "Warning: null mgslpc_info for (%s) in %sn";
  739. if (!info) {
  740. printk(badinfo, kdevname(device), routine);
  741. return 1;
  742. }
  743. if (info->magic != MGSLPC_MAGIC) {
  744. printk(badmagic, kdevname(device), routine);
  745. return 1;
  746. }
  747. #endif
  748. return 0;
  749. }
  750. #define CMD_RXFIFO      BIT7 // release current rx FIFO
  751. #define CMD_RXRESET     BIT6 // receiver reset
  752. #define CMD_RXFIFO_READ BIT5
  753. #define CMD_START_TIMER BIT4
  754. #define CMD_TXFIFO      BIT3 // release current tx FIFO
  755. #define CMD_TXEOM       BIT1 // transmit end message
  756. #define CMD_TXRESET     BIT0 // transmit reset
  757. static BOOLEAN wait_command_complete(MGSLPC_INFO *info, unsigned char channel) 
  758. {
  759. int i = 0;
  760. unsigned char status;
  761. /* wait for command completion */ 
  762. while ((status = read_reg(info, (unsigned char)(channel+STAR)) & BIT2)) {
  763. udelay(1);
  764. if (i++ == 1000)
  765. return FALSE;
  766. }
  767. return TRUE;
  768. }
  769. static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd) 
  770. {
  771. wait_command_complete(info, channel);
  772. write_reg(info, (unsigned char) (channel + CMDR), cmd);
  773. }
  774. static void tx_pause(struct tty_struct *tty)
  775. {
  776. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  777. unsigned long flags;
  778. if (mgslpc_paranoia_check(info, tty->device, "tx_pause"))
  779. return;
  780. if (debug_level >= DEBUG_LEVEL_INFO)
  781. printk("tx_pause(%s)n",info->device_name);
  782. spin_lock_irqsave(&info->lock,flags);
  783. if (info->tx_enabled)
  784.   tx_stop(info);
  785. spin_unlock_irqrestore(&info->lock,flags);
  786. }
  787. static void tx_release(struct tty_struct *tty)
  788. {
  789. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  790. unsigned long flags;
  791. if (mgslpc_paranoia_check(info, tty->device, "tx_release"))
  792. return;
  793. if (debug_level >= DEBUG_LEVEL_INFO)
  794. printk("tx_release(%s)n",info->device_name);
  795. spin_lock_irqsave(&info->lock,flags);
  796. if (!info->tx_enabled)
  797.   tx_start(info);
  798. spin_unlock_irqrestore(&info->lock,flags);
  799. }
  800. /* Return next bottom half action to perform.
  801.  * or 0 if nothing to do.
  802.  */
  803. int bh_action(MGSLPC_INFO *info)
  804. {
  805. unsigned long flags;
  806. int rc = 0;
  807. spin_lock_irqsave(&info->lock,flags);
  808. if (info->pending_bh & BH_RECEIVE) {
  809. info->pending_bh &= ~BH_RECEIVE;
  810. rc = BH_RECEIVE;
  811. } else if (info->pending_bh & BH_TRANSMIT) {
  812. info->pending_bh &= ~BH_TRANSMIT;
  813. rc = BH_TRANSMIT;
  814. } else if (info->pending_bh & BH_STATUS) {
  815. info->pending_bh &= ~BH_STATUS;
  816. rc = BH_STATUS;
  817. }
  818. if (!rc) {
  819. /* Mark BH routine as complete */
  820. info->bh_running   = 0;
  821. info->bh_requested = 0;
  822. }
  823. spin_unlock_irqrestore(&info->lock,flags);
  824. return rc;
  825. }
  826. void bh_handler(void* Context)
  827. {
  828. MGSLPC_INFO *info = (MGSLPC_INFO*)Context;
  829. int action;
  830. if (!info)
  831. return;
  832. if (debug_level >= DEBUG_LEVEL_BH)
  833. printk( "%s(%d):bh_handler(%s) entryn",
  834. __FILE__,__LINE__,info->device_name);
  835. info->bh_running = 1;
  836. while((action = bh_action(info)) != 0) {
  837. /* Process work item */
  838. if ( debug_level >= DEBUG_LEVEL_BH )
  839. printk( "%s(%d):bh_handler() work item action=%dn",
  840. __FILE__,__LINE__,action);
  841. switch (action) {
  842. case BH_RECEIVE:
  843. while(rx_get_frame(info));
  844. break;
  845. case BH_TRANSMIT:
  846. bh_transmit(info);
  847. break;
  848. case BH_STATUS:
  849. bh_status(info);
  850. break;
  851. default:
  852. /* unknown work item ID */
  853. printk("Unknown work item ID=%08X!n", action);
  854. break;
  855. }
  856. }
  857. if (debug_level >= DEBUG_LEVEL_BH)
  858. printk( "%s(%d):bh_handler(%s) exitn",
  859. __FILE__,__LINE__,info->device_name);
  860. }
  861. void bh_transmit(MGSLPC_INFO *info)
  862. {
  863. struct tty_struct *tty = info->tty;
  864. if (debug_level >= DEBUG_LEVEL_BH)
  865. printk("bh_transmit() entry on %sn", info->device_name);
  866. if (tty) {
  867. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  868.     tty->ldisc.write_wakeup) {
  869. if ( debug_level >= DEBUG_LEVEL_BH )
  870. printk( "%s(%d):calling ldisc.write_wakeup on %sn",
  871. __FILE__,__LINE__,info->device_name);
  872. (tty->ldisc.write_wakeup)(tty);
  873. }
  874. wake_up_interruptible(&tty->write_wait);
  875. }
  876. }
  877. void bh_status(MGSLPC_INFO *info)
  878. {
  879. info->ri_chkcount = 0;
  880. info->dsr_chkcount = 0;
  881. info->dcd_chkcount = 0;
  882. info->cts_chkcount = 0;
  883. }
  884. /* eom: non-zero = end of frame */ 
  885. void rx_ready_hdlc(MGSLPC_INFO *info, int eom) 
  886. {
  887. unsigned char data[2];
  888. unsigned char fifo_count, read_count, i;
  889. RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
  890. if (debug_level >= DEBUG_LEVEL_ISR)
  891. printk("%s(%d):rx_ready_hdlc(eom=%d)n",__FILE__,__LINE__,eom);
  892. if (!info->rx_enabled)
  893. return;
  894. if (info->rx_frame_count >= info->rx_buf_count) {
  895. /* no more free buffers */
  896. issue_command(info, CHA, CMD_RXRESET);
  897. info->pending_bh |= BH_RECEIVE;
  898. info->rx_overflow = 1;
  899. info->icount.buf_overrun++;
  900. return;
  901. }
  902. if (eom) {
  903. /* end of frame, get FIFO count from RBCL register */ 
  904. if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
  905. fifo_count = 32;
  906. } else
  907. fifo_count = 32;
  908. do {
  909. if (fifo_count == 1) {
  910. read_count = 1;
  911. data[0] = read_reg(info, CHA + RXFIFO);
  912. } else {
  913. read_count = 2;
  914. *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
  915. }
  916. fifo_count -= read_count;
  917. if (!fifo_count && eom)
  918. buf->status = data[--read_count];
  919. for (i = 0; i < read_count; i++) {
  920. if (buf->count >= info->max_frame_size) {
  921. /* frame too large, reset receiver and reset current buffer */
  922. issue_command(info, CHA, CMD_RXRESET);
  923. buf->count = 0;
  924. return;
  925. }
  926. *(buf->data + buf->count) = data[i];
  927. buf->count++;
  928. }
  929. } while (fifo_count);
  930. if (eom) {
  931. info->pending_bh |= BH_RECEIVE;
  932. info->rx_frame_count++;
  933. info->rx_put++;
  934. if (info->rx_put >= info->rx_buf_count)
  935. info->rx_put = 0;
  936. }
  937. issue_command(info, CHA, CMD_RXFIFO);
  938. }
  939. void rx_ready_async(MGSLPC_INFO *info, int tcd) 
  940. {
  941. unsigned char data, status;
  942. int fifo_count;
  943.   struct tty_struct *tty = info->tty;
  944.   struct mgsl_icount *icount = &info->icount;
  945. if (tcd) {
  946. /* early termination, get FIFO count from RBCL register */ 
  947. fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
  948. /* Zero fifo count could mean 0 or 32 bytes available.
  949.  * If BIT5 of STAR is set then at least 1 byte is available.
  950.  */
  951. if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
  952. fifo_count = 32;
  953. } else
  954. fifo_count = 32;
  955. /* Flush received async data to receive data buffer. */ 
  956. while (fifo_count) {
  957. data   = read_reg(info, CHA + RXFIFO);
  958. status = read_reg(info, CHA + RXFIFO);
  959. fifo_count -= 2;
  960. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  961. break;
  962. *tty->flip.char_buf_ptr = data;
  963. icount->rx++;
  964. *tty->flip.flag_buf_ptr = 0;
  965. // if no frameing/crc error then save data
  966. // BIT7:parity error
  967. // BIT6:framing error
  968. if (status & (BIT7 + BIT6)) {
  969. if (status & BIT7) 
  970. icount->parity++;
  971. else
  972. icount->frame++;
  973. /* discard char if tty control flags say so */
  974. if (status & info->ignore_status_mask)
  975. continue;
  976. status &= info->read_status_mask;
  977. if (status & BIT7)
  978. *tty->flip.flag_buf_ptr = TTY_PARITY;
  979. else if (status & BIT6)
  980. *tty->flip.flag_buf_ptr = TTY_FRAME;
  981. }
  982. tty->flip.flag_buf_ptr++;
  983. tty->flip.char_buf_ptr++;
  984. tty->flip.count++;
  985. }
  986. issue_command(info, CHA, CMD_RXFIFO);
  987. if (debug_level >= DEBUG_LEVEL_ISR) {
  988. printk("%s(%d):rx_ready_async count=%dn",
  989. __FILE__,__LINE__,tty->flip.count);
  990. printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%dn",
  991. __FILE__,__LINE__,icount->rx,icount->brk,
  992. icount->parity,icount->frame,icount->overrun);
  993. }
  994. if (tty->flip.count)
  995. tty_flip_buffer_push(tty);
  996. }
  997. void tx_done(MGSLPC_INFO *info) 
  998. {
  999. if (!info->tx_active)
  1000. return;
  1001. info->tx_active = 0;
  1002. info->tx_aborting = 0;
  1003. if (info->params.mode == MGSL_MODE_ASYNC)
  1004. return;
  1005. info->tx_count = info->tx_put = info->tx_get = 0;
  1006. del_timer(&info->tx_timer);
  1007. if (info->drop_rts_on_tx_done) {
  1008. get_signals(info);
  1009. if (info->serial_signals & SerialSignal_RTS) {
  1010. info->serial_signals &= ~SerialSignal_RTS;
  1011. set_signals(info);
  1012. }
  1013. info->drop_rts_on_tx_done = 0;
  1014. }
  1015. #ifdef CONFIG_SYNCLINK_SYNCPPP
  1016. if (info->netcount)
  1017. mgslpc_sppp_tx_done(info);
  1018. else 
  1019. #endif
  1020. {
  1021. if (info->tty->stopped || info->tty->hw_stopped) {
  1022. tx_stop(info);
  1023. return;
  1024. }
  1025. info->pending_bh |= BH_TRANSMIT;
  1026. }
  1027. }
  1028. void tx_ready(MGSLPC_INFO *info) 
  1029. {
  1030. unsigned char fifo_count = 32;
  1031. int c;
  1032. if (debug_level >= DEBUG_LEVEL_ISR)
  1033. printk("%s(%d):tx_ready(%s)n", __FILE__,__LINE__,info->device_name);
  1034. if (info->params.mode == MGSL_MODE_HDLC) {
  1035. if (!info->tx_active)
  1036. return;
  1037. } else {
  1038. if (info->tty->stopped || info->tty->hw_stopped) {
  1039. tx_stop(info);
  1040. return;
  1041. }
  1042. if (!info->tx_count)
  1043. info->tx_active = 0;
  1044. }
  1045. if (!info->tx_count)
  1046. return;
  1047. while (info->tx_count && fifo_count) {
  1048. c = MIN(2, MIN(fifo_count, MIN(info->tx_count, TXBUFSIZE - info->tx_get)));
  1049. if (c == 1) {
  1050. write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
  1051. } else {
  1052. write_reg16(info, CHA + TXFIFO,
  1053.   *((unsigned short*)(info->tx_buf + info->tx_get)));
  1054. }
  1055. info->tx_count -= c;
  1056. info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
  1057. fifo_count -= c;
  1058. }
  1059. if (info->params.mode == MGSL_MODE_ASYNC) {
  1060. if (info->tx_count < WAKEUP_CHARS)
  1061. info->pending_bh |= BH_TRANSMIT;
  1062. issue_command(info, CHA, CMD_TXFIFO);
  1063. } else {
  1064. if (info->tx_count)
  1065. issue_command(info, CHA, CMD_TXFIFO);
  1066. else
  1067. issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
  1068. }
  1069. }
  1070. void cts_change(MGSLPC_INFO *info) 
  1071. {
  1072. get_signals(info);
  1073. if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1074. irq_disable(info, CHB, IRQ_CTS);
  1075. info->icount.cts++;
  1076. if (info->serial_signals & SerialSignal_CTS)
  1077. info->input_signal_events.cts_up++;
  1078. else
  1079. info->input_signal_events.cts_down++;
  1080. wake_up_interruptible(&info->status_event_wait_q);
  1081. wake_up_interruptible(&info->event_wait_q);
  1082. if (info->flags & ASYNC_CTS_FLOW) {
  1083. if (info->tty->hw_stopped) {
  1084. if (info->serial_signals & SerialSignal_CTS) {
  1085. if (debug_level >= DEBUG_LEVEL_ISR)
  1086. printk("CTS tx start...");
  1087. if (info->tty)
  1088. info->tty->hw_stopped = 0;
  1089. tx_start(info);
  1090. info->pending_bh |= BH_TRANSMIT;
  1091. return;
  1092. }
  1093. } else {
  1094. if (!(info->serial_signals & SerialSignal_CTS)) {
  1095. if (debug_level >= DEBUG_LEVEL_ISR)
  1096. printk("CTS tx stop...");
  1097. if (info->tty)
  1098. info->tty->hw_stopped = 1;
  1099. tx_stop(info);
  1100. }
  1101. }
  1102. }
  1103. info->pending_bh |= BH_STATUS;
  1104. }
  1105. void dcd_change(MGSLPC_INFO *info) 
  1106. {
  1107. get_signals(info);
  1108. if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1109. irq_disable(info, CHB, IRQ_DCD);
  1110. info->icount.dcd++;
  1111. if (info->serial_signals & SerialSignal_DCD) {
  1112. info->input_signal_events.dcd_up++;
  1113. #ifdef CONFIG_SYNCLINK_SYNCPPP
  1114. if (info->netcount)
  1115. sppp_reopen(info->netdev);
  1116. #endif
  1117. }
  1118. else
  1119. info->input_signal_events.dcd_down++;
  1120. wake_up_interruptible(&info->status_event_wait_q);
  1121. wake_up_interruptible(&info->event_wait_q);
  1122. if (info->flags & ASYNC_CHECK_CD) {
  1123. if (debug_level >= DEBUG_LEVEL_ISR)
  1124. printk("%s CD now %s...", info->device_name,
  1125.        (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
  1126. if (info->serial_signals & SerialSignal_DCD)
  1127. wake_up_interruptible(&info->open_wait);
  1128. else if (!(info->flags & (ASYNC_CALLOUT_ACTIVE | ASYNC_CALLOUT_NOHUP))) {
  1129. if (debug_level >= DEBUG_LEVEL_ISR)
  1130. printk("doing serial hangup...");
  1131. if (info->tty)
  1132. tty_hangup(info->tty);
  1133. }
  1134. }
  1135. info->pending_bh |= BH_STATUS;
  1136. }
  1137. void dsr_change(MGSLPC_INFO *info) 
  1138. {
  1139. get_signals(info);
  1140. if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1141. port_irq_disable(info, PVR_DSR);
  1142. info->icount.dsr++;
  1143. if (info->serial_signals & SerialSignal_DSR)
  1144. info->input_signal_events.dsr_up++;
  1145. else
  1146. info->input_signal_events.dsr_down++;
  1147. wake_up_interruptible(&info->status_event_wait_q);
  1148. wake_up_interruptible(&info->event_wait_q);
  1149. info->pending_bh |= BH_STATUS;
  1150. }
  1151. void ri_change(MGSLPC_INFO *info) 
  1152. {
  1153. get_signals(info);
  1154. if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1155. port_irq_disable(info, PVR_RI);
  1156. info->icount.rng++;
  1157. if (info->serial_signals & SerialSignal_RI)
  1158. info->input_signal_events.ri_up++;
  1159. else
  1160. info->input_signal_events.ri_down++;
  1161. wake_up_interruptible(&info->status_event_wait_q);
  1162. wake_up_interruptible(&info->event_wait_q);
  1163. info->pending_bh |= BH_STATUS;
  1164. }
  1165. /* Interrupt service routine entry point.
  1166.  * 
  1167.  * Arguments:
  1168.  * 
  1169.  * irq     interrupt number that caused interrupt
  1170.  * dev_id  device ID supplied during interrupt registration
  1171.  * regs    interrupted processor context
  1172.  */
  1173. static void mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs)
  1174. {
  1175. MGSLPC_INFO * info = (MGSLPC_INFO *)dev_id;
  1176. unsigned short isr;
  1177. unsigned char gis, pis;
  1178. int count=0;
  1179. if (debug_level >= DEBUG_LEVEL_ISR)
  1180. printk("mgslpc_isr(%d) entry.n", irq);
  1181. if (!info)
  1182. return;
  1183. if (!(info->link.state & DEV_CONFIG))
  1184. return;
  1185. spin_lock(&info->lock);
  1186. while ((gis = read_reg(info, CHA + GIS))) {
  1187. if (debug_level >= DEBUG_LEVEL_ISR)
  1188. printk("mgslpc_isr %s gis=%04Xn", info->device_name,gis);
  1189. if ((gis & 0x70) || count > 1000) {
  1190. printk("synclink_cs:hardware failed or ejectedn");
  1191. break;
  1192. }
  1193. count++;
  1194. if (gis & (BIT1 + BIT0)) {
  1195. isr = read_reg16(info, CHB + ISR);
  1196. if (isr & IRQ_DCD)
  1197. dcd_change(info);
  1198. if (isr & IRQ_CTS)
  1199. cts_change(info);
  1200. }
  1201. if (gis & (BIT3 + BIT2))
  1202. {
  1203. isr = read_reg16(info, CHA + ISR);
  1204. if (isr & IRQ_TIMER) {
  1205. info->irq_occurred = 1;
  1206. irq_disable(info, CHA, IRQ_TIMER);
  1207. }
  1208. /* receive IRQs */ 
  1209. if (isr & IRQ_EXITHUNT) {
  1210. info->icount.exithunt++;
  1211. wake_up_interruptible(&info->event_wait_q);
  1212. }
  1213. if (isr & IRQ_BREAK_ON) {
  1214. info->icount.brk++;
  1215. if (info->flags & ASYNC_SAK)
  1216. do_SAK(info->tty);
  1217. }
  1218. if (isr & IRQ_RXTIME) {
  1219. issue_command(info, CHA, CMD_RXFIFO_READ);
  1220. }
  1221. if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
  1222. if (info->params.mode == MGSL_MODE_HDLC)
  1223. rx_ready_hdlc(info, isr & IRQ_RXEOM); 
  1224. else
  1225. rx_ready_async(info, isr & IRQ_RXEOM);
  1226. }
  1227. /* transmit IRQs */ 
  1228. if (isr & IRQ_UNDERRUN) {
  1229. if (info->tx_aborting)
  1230. info->icount.txabort++;
  1231. else
  1232. info->icount.txunder++;
  1233. tx_done(info);
  1234. }
  1235. else if (isr & IRQ_ALLSENT) {
  1236. info->icount.txok++;
  1237. tx_done(info);
  1238. }
  1239. else if (isr & IRQ_TXFIFO)
  1240. tx_ready(info);
  1241. }
  1242. if (gis & BIT7) {
  1243. pis = read_reg(info, CHA + PIS);
  1244. if (pis & BIT1)
  1245. dsr_change(info);
  1246. if (pis & BIT2)
  1247. ri_change(info);
  1248. }
  1249. }
  1250. /* Request bottom half processing if there's something 
  1251.  * for it to do and the bh is not already running
  1252.  */
  1253. if (info->pending_bh && !info->bh_running && !info->bh_requested) {
  1254. if ( debug_level >= DEBUG_LEVEL_ISR )
  1255. printk("%s(%d):%s queueing bh task.n",
  1256. __FILE__,__LINE__,info->device_name);
  1257. queue_task(&info->task, &tq_immediate);
  1258. mark_bh(IMMEDIATE_BH);
  1259. info->bh_requested = 1;
  1260. }
  1261. spin_unlock(&info->lock);
  1262. if (debug_level >= DEBUG_LEVEL_ISR)
  1263. printk("%s(%d):mgslpc_isr(%d)exit.n",
  1264.        __FILE__,__LINE__,irq);
  1265. }
  1266. /* Initialize and start device.
  1267.  */
  1268. static int startup(MGSLPC_INFO * info)
  1269. {
  1270. int retval = 0;
  1271. if (debug_level >= DEBUG_LEVEL_INFO)
  1272. printk("%s(%d):startup(%s)n",__FILE__,__LINE__,info->device_name);
  1273. if (info->flags & ASYNC_INITIALIZED)
  1274. return 0;
  1275. if (!info->tx_buf) {
  1276. /* allocate a page of memory for a transmit buffer */
  1277. info->tx_buf = (unsigned char *)get_free_page(GFP_KERNEL);
  1278. if (!info->tx_buf) {
  1279. printk(KERN_ERR"%s(%d):%s can't allocate transmit buffern",
  1280. __FILE__,__LINE__,info->device_name);
  1281. return -ENOMEM;
  1282. }
  1283. }
  1284. info->pending_bh = 0;
  1285. init_timer(&info->tx_timer);
  1286. info->tx_timer.data = (unsigned long)info;
  1287. info->tx_timer.function = tx_timeout;
  1288. /* Allocate and claim adapter resources */
  1289. retval = claim_resources(info);
  1290. /* perform existance check and diagnostics */
  1291. if ( !retval )
  1292. retval = adapter_test(info);
  1293. if ( retval ) {
  1294.    if (capable(CAP_SYS_ADMIN) && info->tty)
  1295. set_bit(TTY_IO_ERROR, &info->tty->flags);
  1296. release_resources(info);
  1297.    return retval;
  1298.    }
  1299. /* program hardware for current parameters */
  1300. mgslpc_change_params(info);
  1301. if (info->tty)
  1302. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  1303. info->flags |= ASYNC_INITIALIZED;
  1304. return 0;
  1305. }
  1306. /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
  1307.  */
  1308. static void shutdown(MGSLPC_INFO * info)
  1309. {
  1310. unsigned long flags;
  1311. if (!(info->flags & ASYNC_INITIALIZED))
  1312. return;
  1313. if (debug_level >= DEBUG_LEVEL_INFO)
  1314. printk("%s(%d):mgslpc_shutdown(%s)n",
  1315.  __FILE__,__LINE__, info->device_name );
  1316. /* clear status wait queue because status changes */
  1317. /* can't happen after shutting down the hardware */
  1318. wake_up_interruptible(&info->status_event_wait_q);
  1319. wake_up_interruptible(&info->event_wait_q);
  1320. del_timer(&info->tx_timer);
  1321. if (info->tx_buf) {
  1322. free_page((unsigned long) info->tx_buf);
  1323. info->tx_buf = 0;
  1324. }
  1325. spin_lock_irqsave(&info->lock,flags);
  1326. rx_stop(info);
  1327. tx_stop(info);
  1328. /* TODO:disable interrupts instead of reset to preserve signal states */
  1329. reset_device(info);
  1330.   if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
  1331.   info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  1332. set_signals(info);
  1333. }
  1334. spin_unlock_irqrestore(&info->lock,flags);
  1335. release_resources(info);
  1336. if (info->tty)
  1337. set_bit(TTY_IO_ERROR, &info->tty->flags);
  1338. info->flags &= ~ASYNC_INITIALIZED;
  1339. }
  1340. static void mgslpc_program_hw(MGSLPC_INFO *info)
  1341. {
  1342. unsigned long flags;
  1343. spin_lock_irqsave(&info->lock,flags);
  1344. rx_stop(info);
  1345. tx_stop(info);
  1346. info->tx_count = info->tx_put = info->tx_get = 0;
  1347. if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
  1348. hdlc_mode(info);
  1349. else
  1350. async_mode(info);
  1351. set_signals(info);
  1352. info->dcd_chkcount = 0;
  1353. info->cts_chkcount = 0;
  1354. info->ri_chkcount = 0;
  1355. info->dsr_chkcount = 0;
  1356. irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
  1357. port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
  1358. get_signals(info);
  1359. if (info->netcount || info->tty->termios->c_cflag & CREAD)
  1360. rx_start(info);
  1361. spin_unlock_irqrestore(&info->lock,flags);
  1362. }
  1363. /* Reconfigure adapter based on new parameters
  1364.  */
  1365. static void mgslpc_change_params(MGSLPC_INFO *info)
  1366. {
  1367. unsigned cflag;
  1368. int bits_per_char;
  1369. if (!info->tty || !info->tty->termios)
  1370. return;
  1371. if (debug_level >= DEBUG_LEVEL_INFO)
  1372. printk("%s(%d):mgslpc_change_params(%s)n",
  1373.  __FILE__,__LINE__, info->device_name );
  1374.  
  1375. cflag = info->tty->termios->c_cflag;
  1376. /* if B0 rate (hangup) specified then negate DTR and RTS */
  1377. /* otherwise assert DTR and RTS */
  1378.   if (cflag & CBAUD)
  1379. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  1380. else
  1381. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  1382. /* byte size and parity */
  1383. switch (cflag & CSIZE) {
  1384. case CS5: info->params.data_bits = 5; break;
  1385. case CS6: info->params.data_bits = 6; break;
  1386. case CS7: info->params.data_bits = 7; break;
  1387. case CS8: info->params.data_bits = 8; break;
  1388. default:  info->params.data_bits = 7; break;
  1389. }
  1390.       
  1391. if (cflag & CSTOPB)
  1392. info->params.stop_bits = 2;
  1393. else
  1394. info->params.stop_bits = 1;
  1395. info->params.parity = ASYNC_PARITY_NONE;
  1396. if (cflag & PARENB) {
  1397. if (cflag & PARODD)
  1398. info->params.parity = ASYNC_PARITY_ODD;
  1399. else
  1400. info->params.parity = ASYNC_PARITY_EVEN;
  1401. #ifdef CMSPAR
  1402. if (cflag & CMSPAR)
  1403. info->params.parity = ASYNC_PARITY_SPACE;
  1404. #endif
  1405. }
  1406. /* calculate number of jiffies to transmit a full
  1407.  * FIFO (32 bytes) at specified data rate
  1408.  */
  1409. bits_per_char = info->params.data_bits + 
  1410. info->params.stop_bits + 1;
  1411. /* if port data rate is set to 460800 or less then
  1412.  * allow tty settings to override, otherwise keep the
  1413.  * current data rate.
  1414.  */
  1415. if (info->params.data_rate <= 460800) {
  1416. info->params.data_rate = tty_get_baud_rate(info->tty);
  1417. }
  1418. if ( info->params.data_rate ) {
  1419. info->timeout = (32*HZ*bits_per_char) / 
  1420. info->params.data_rate;
  1421. }
  1422. info->timeout += HZ/50; /* Add .02 seconds of slop */
  1423. if (cflag & CRTSCTS)
  1424. info->flags |= ASYNC_CTS_FLOW;
  1425. else
  1426. info->flags &= ~ASYNC_CTS_FLOW;
  1427. if (cflag & CLOCAL)
  1428. info->flags &= ~ASYNC_CHECK_CD;
  1429. else
  1430. info->flags |= ASYNC_CHECK_CD;
  1431. /* process tty input control flags */
  1432. info->read_status_mask = 0;
  1433. if (I_INPCK(info->tty))
  1434. info->read_status_mask |= BIT7 | BIT6;
  1435. if (I_IGNPAR(info->tty))
  1436. info->ignore_status_mask |= BIT7 | BIT6;
  1437. mgslpc_program_hw(info);
  1438. }
  1439. /* Add a character to the transmit buffer
  1440.  */
  1441. static void mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
  1442. {
  1443. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1444. unsigned long flags;
  1445. if (debug_level >= DEBUG_LEVEL_INFO) {
  1446. printk( "%s(%d):mgslpc_put_char(%d) on %sn",
  1447. __FILE__,__LINE__,ch,info->device_name);
  1448. }
  1449. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_put_char"))
  1450. return;
  1451. if (!tty || !info->tx_buf)
  1452. return;
  1453. spin_lock_irqsave(&info->lock,flags);
  1454. if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
  1455. if (info->tx_count < TXBUFSIZE - 1) {
  1456. info->tx_buf[info->tx_put++] = ch;
  1457. info->tx_put &= TXBUFSIZE-1;
  1458. info->tx_count++;
  1459. }
  1460. }
  1461. spin_unlock_irqrestore(&info->lock,flags);
  1462. }
  1463. /* Enable transmitter so remaining characters in the
  1464.  * transmit buffer are sent.
  1465.  */
  1466. static void mgslpc_flush_chars(struct tty_struct *tty)
  1467. {
  1468. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1469. unsigned long flags;
  1470. if (debug_level >= DEBUG_LEVEL_INFO)
  1471. printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%dn",
  1472. __FILE__,__LINE__,info->device_name,info->tx_count);
  1473. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_flush_chars"))
  1474. return;
  1475. if (info->tx_count <= 0 || tty->stopped ||
  1476.     tty->hw_stopped || !info->tx_buf)
  1477. return;
  1478. if (debug_level >= DEBUG_LEVEL_INFO)
  1479. printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmittern",
  1480. __FILE__,__LINE__,info->device_name);
  1481. spin_lock_irqsave(&info->lock,flags);
  1482. if (!info->tx_active)
  1483.   tx_start(info);
  1484. spin_unlock_irqrestore(&info->lock,flags);
  1485. }
  1486. /* Send a block of data
  1487.  * 
  1488.  * Arguments:
  1489.  * 
  1490.  * tty        pointer to tty information structure
  1491.  * from_user  flag: 1 = from user process
  1492.  * buf       pointer to buffer containing send data
  1493.  * count      size of send data in bytes
  1494.  * 
  1495.  * Returns: number of characters written
  1496.  */
  1497. static int mgslpc_write(struct tty_struct * tty, int from_user,
  1498. const unsigned char *buf, int count)
  1499. {
  1500. int c, ret = 0, err;
  1501. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1502. unsigned long flags;
  1503. if (debug_level >= DEBUG_LEVEL_INFO)
  1504. printk( "%s(%d):mgslpc_write(%s) count=%dn",
  1505. __FILE__,__LINE__,info->device_name,count);
  1506. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_write") ||
  1507.     !tty || !info->tx_buf)
  1508. goto cleanup;
  1509. if (info->params.mode == MGSL_MODE_HDLC) {
  1510. if (count > TXBUFSIZE) {
  1511. ret = -EIO;
  1512. goto cleanup;
  1513. }
  1514. if (info->tx_active)
  1515. goto cleanup;
  1516. else if (info->tx_count)
  1517. goto start;
  1518. }
  1519. for (;;) {
  1520. c = MIN(count,
  1521. MIN(TXBUFSIZE - info->tx_count - 1,
  1522.     TXBUFSIZE - info->tx_put));
  1523. if (c <= 0)
  1524. break;
  1525. if (from_user) {
  1526. COPY_FROM_USER(err, info->tx_buf + info->tx_put, buf, c);
  1527. if (err) {
  1528. if (!ret)
  1529. ret = -EFAULT;
  1530. break;
  1531. }
  1532. } else
  1533. memcpy(info->tx_buf + info->tx_put, buf, c);
  1534. spin_lock_irqsave(&info->lock,flags);
  1535. info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
  1536. info->tx_count += c;
  1537. spin_unlock_irqrestore(&info->lock,flags);
  1538. buf += c;
  1539. count -= c;
  1540. ret += c;
  1541. }
  1542. start:
  1543.   if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
  1544. spin_lock_irqsave(&info->lock,flags);
  1545. if (!info->tx_active)
  1546.   tx_start(info);
  1547. spin_unlock_irqrestore(&info->lock,flags);
  1548.   }
  1549. cleanup:
  1550. if (debug_level >= DEBUG_LEVEL_INFO)
  1551. printk( "%s(%d):mgslpc_write(%s) returning=%dn",
  1552. __FILE__,__LINE__,info->device_name,ret);
  1553. return ret;
  1554. }
  1555. /* Return the count of free bytes in transmit buffer
  1556.  */
  1557. static int mgslpc_write_room(struct tty_struct *tty)
  1558. {
  1559. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1560. int ret;
  1561. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_write_room"))
  1562. return 0;
  1563. if (info->params.mode == MGSL_MODE_HDLC) {
  1564. /* HDLC (frame oriented) mode */
  1565. if (info->tx_active)
  1566. return 0;
  1567. else
  1568. return HDLC_MAX_FRAME_SIZE;
  1569. } else {
  1570. ret = TXBUFSIZE - info->tx_count - 1;
  1571. if (ret < 0)
  1572. ret = 0;
  1573. }
  1574. if (debug_level >= DEBUG_LEVEL_INFO)
  1575. printk("%s(%d):mgslpc_write_room(%s)=%dn",
  1576.  __FILE__,__LINE__, info->device_name, ret);
  1577. return ret;
  1578. }
  1579. /* Return the count of bytes in transmit buffer
  1580.  */
  1581. static int mgslpc_chars_in_buffer(struct tty_struct *tty)
  1582. {
  1583. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1584. int rc;
  1585.  
  1586. if (debug_level >= DEBUG_LEVEL_INFO)
  1587. printk("%s(%d):mgslpc_chars_in_buffer(%s)n",
  1588.  __FILE__,__LINE__, info->device_name );
  1589.  
  1590. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_chars_in_buffer"))
  1591. return 0;
  1592. if (info->params.mode == MGSL_MODE_HDLC)
  1593. rc = info->tx_active ? info->max_frame_size : 0;
  1594. else
  1595. rc = info->tx_count;
  1596. if (debug_level >= DEBUG_LEVEL_INFO)
  1597. printk("%s(%d):mgslpc_chars_in_buffer(%s)=%dn",
  1598.  __FILE__,__LINE__, info->device_name, rc);
  1599.  
  1600. return rc;
  1601. }
  1602. /* Discard all data in the send buffer
  1603.  */
  1604. static void mgslpc_flush_buffer(struct tty_struct *tty)
  1605. {
  1606. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1607. unsigned long flags;
  1608. if (debug_level >= DEBUG_LEVEL_INFO)
  1609. printk("%s(%d):mgslpc_flush_buffer(%s) entryn",
  1610.  __FILE__,__LINE__, info->device_name );
  1611. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_flush_buffer"))
  1612. return;
  1613. spin_lock_irqsave(&info->lock,flags); 
  1614. info->tx_count = info->tx_put = info->tx_get = 0;
  1615. del_timer(&info->tx_timer);
  1616. spin_unlock_irqrestore(&info->lock,flags);
  1617. wake_up_interruptible(&tty->write_wait);
  1618. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  1619.     tty->ldisc.write_wakeup)
  1620. (tty->ldisc.write_wakeup)(tty);
  1621. }
  1622. /* Send a high-priority XON/XOFF character
  1623.  */
  1624. static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
  1625. {
  1626. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1627. unsigned long flags;
  1628. if (debug_level >= DEBUG_LEVEL_INFO)
  1629. printk("%s(%d):mgslpc_send_xchar(%s,%d)n",
  1630.  __FILE__,__LINE__, info->device_name, ch );
  1631.  
  1632. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_send_xchar"))
  1633. return;
  1634. info->x_char = ch;
  1635. if (ch) {
  1636. spin_lock_irqsave(&info->lock,flags);
  1637. if (!info->tx_enabled)
  1638.   tx_start(info);
  1639. spin_unlock_irqrestore(&info->lock,flags);
  1640. }
  1641. }
  1642. /* Signal remote device to throttle send data (our receive data)
  1643.  */
  1644. static void mgslpc_throttle(struct tty_struct * tty)
  1645. {
  1646. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1647. unsigned long flags;
  1648. if (debug_level >= DEBUG_LEVEL_INFO)
  1649. printk("%s(%d):mgslpc_throttle(%s) entryn",
  1650.  __FILE__,__LINE__, info->device_name );
  1651. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_throttle"))
  1652. return;
  1653. if (I_IXOFF(tty))
  1654. mgslpc_send_xchar(tty, STOP_CHAR(tty));
  1655.  
  1656.   if (tty->termios->c_cflag & CRTSCTS) {
  1657. spin_lock_irqsave(&info->lock,flags);
  1658. info->serial_signals &= ~SerialSignal_RTS;
  1659.   set_signals(info);
  1660. spin_unlock_irqrestore(&info->lock,flags);
  1661. }
  1662. }
  1663. /* Signal remote device to stop throttling send data (our receive data)
  1664.  */
  1665. static void mgslpc_unthrottle(struct tty_struct * tty)
  1666. {
  1667. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1668. unsigned long flags;
  1669. if (debug_level >= DEBUG_LEVEL_INFO)
  1670. printk("%s(%d):mgslpc_unthrottle(%s) entryn",
  1671.  __FILE__,__LINE__, info->device_name );
  1672. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_unthrottle"))
  1673. return;
  1674. if (I_IXOFF(tty)) {
  1675. if (info->x_char)
  1676. info->x_char = 0;
  1677. else
  1678. mgslpc_send_xchar(tty, START_CHAR(tty));
  1679. }
  1680.   if (tty->termios->c_cflag & CRTSCTS) {
  1681. spin_lock_irqsave(&info->lock,flags);
  1682. info->serial_signals |= SerialSignal_RTS;
  1683.   set_signals(info);
  1684. spin_unlock_irqrestore(&info->lock,flags);
  1685. }
  1686. }
  1687. /* get the current serial statistics
  1688.  */
  1689. static int get_stats(MGSLPC_INFO * info, struct mgsl_icount *user_icount)
  1690. {
  1691. int err;
  1692. if (debug_level >= DEBUG_LEVEL_INFO)
  1693. printk("get_params(%s)n", info->device_name);
  1694. COPY_TO_USER(err,user_icount, &info->icount, sizeof(struct mgsl_icount));
  1695. if (err)
  1696. return -EFAULT;
  1697. return 0;
  1698. }
  1699. /* get the current serial parameters
  1700.  */
  1701. static int get_params(MGSLPC_INFO * info, MGSL_PARAMS *user_params)
  1702. {
  1703. int err;
  1704. if (debug_level >= DEBUG_LEVEL_INFO)
  1705. printk("get_params(%s)n", info->device_name);
  1706. COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
  1707. if (err)
  1708. return -EFAULT;
  1709. return 0;
  1710. }
  1711. /* set the serial parameters
  1712.  * 
  1713.  * Arguments:
  1714.  * 
  1715.  *  info pointer to device instance data
  1716.  *  new_params user buffer containing new serial params
  1717.  *
  1718.  * Returns: 0 if success, otherwise error code
  1719.  */
  1720. static int set_params(MGSLPC_INFO * info, MGSL_PARAMS *new_params)
  1721. {
  1722.   unsigned long flags;
  1723. MGSL_PARAMS tmp_params;
  1724. int err;
  1725.  
  1726. if (debug_level >= DEBUG_LEVEL_INFO)
  1727. printk("%s(%d):set_params %sn", __FILE__,__LINE__,
  1728. info->device_name );
  1729. COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
  1730. if (err) {
  1731. if ( debug_level >= DEBUG_LEVEL_INFO )
  1732. printk( "%s(%d):set_params(%s) user buffer copy failedn",
  1733. __FILE__,__LINE__,info->device_name);
  1734. return -EFAULT;
  1735. }
  1736. spin_lock_irqsave(&info->lock,flags);
  1737. memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
  1738. spin_unlock_irqrestore(&info->lock,flags);
  1739.   mgslpc_change_params(info);
  1740. return 0;
  1741. }
  1742. static int get_txidle(MGSLPC_INFO * info, int*idle_mode)
  1743. {
  1744. int err;
  1745. if (debug_level >= DEBUG_LEVEL_INFO)
  1746. printk("get_txidle(%s)=%dn", info->device_name, info->idle_mode);
  1747. COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
  1748. if (err)
  1749. return -EFAULT;
  1750. return 0;
  1751. }
  1752. static int set_txidle(MGSLPC_INFO * info, int idle_mode)
  1753. {
  1754.   unsigned long flags;
  1755. if (debug_level >= DEBUG_LEVEL_INFO)
  1756. printk("set_txidle(%s,%d)n", info->device_name, idle_mode);
  1757. spin_lock_irqsave(&info->lock,flags);
  1758. info->idle_mode = idle_mode;
  1759. tx_set_idle(info);
  1760. spin_unlock_irqrestore(&info->lock,flags);
  1761. return 0;
  1762. }
  1763. static int get_interface(MGSLPC_INFO * info, int*if_mode)
  1764. {
  1765. int err;
  1766. if (debug_level >= DEBUG_LEVEL_INFO)
  1767. printk("get_interface(%s)=%dn", info->device_name, info->if_mode);
  1768. COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
  1769. if (err)
  1770. return -EFAULT;
  1771. return 0;
  1772. }
  1773. static int set_interface(MGSLPC_INFO * info, int if_mode)
  1774. {
  1775.   unsigned long flags;
  1776. unsigned char val;
  1777. if (debug_level >= DEBUG_LEVEL_INFO)
  1778. printk("set_interface(%s,%d)n", info->device_name, if_mode);
  1779. spin_lock_irqsave(&info->lock,flags);
  1780. info->if_mode = if_mode;
  1781. val = read_reg(info, PVR) & 0x0f;
  1782. switch (info->if_mode)
  1783. {
  1784. case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
  1785. case MGSL_INTERFACE_V35:   val |= PVR_V35;   break;
  1786. case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
  1787. }
  1788. write_reg(info, PVR, val);
  1789. spin_unlock_irqrestore(&info->lock,flags);
  1790. return 0;
  1791. }
  1792. static int set_txenable(MGSLPC_INFO * info, int enable)
  1793. {
  1794.   unsigned long flags;
  1795.  
  1796. if (debug_level >= DEBUG_LEVEL_INFO)
  1797. printk("set_txenable(%s,%d)n", info->device_name, enable);
  1798. spin_lock_irqsave(&info->lock,flags);
  1799. if (enable) {
  1800. if (!info->tx_enabled)
  1801. tx_start(info);
  1802. } else {
  1803. if (info->tx_enabled)
  1804. tx_stop(info);
  1805. }
  1806. spin_unlock_irqrestore(&info->lock,flags);
  1807. return 0;
  1808. }
  1809. static int tx_abort(MGSLPC_INFO * info)
  1810. {
  1811.   unsigned long flags;
  1812.  
  1813. if (debug_level >= DEBUG_LEVEL_INFO)
  1814. printk("tx_abort(%s)n", info->device_name);
  1815. spin_lock_irqsave(&info->lock,flags);
  1816. if (info->tx_active && info->tx_count &&
  1817.     info->params.mode == MGSL_MODE_HDLC) {
  1818. /* clear data count so FIFO is not filled on next IRQ.
  1819.  * This results in underrun and abort transmission.
  1820.  */
  1821. info->tx_count = info->tx_put = info->tx_get = 0;
  1822. info->tx_aborting = TRUE;
  1823. }
  1824. spin_unlock_irqrestore(&info->lock,flags);
  1825. return 0;
  1826. }
  1827. static int set_rxenable(MGSLPC_INFO * info, int enable)
  1828. {
  1829.   unsigned long flags;
  1830.  
  1831. if (debug_level >= DEBUG_LEVEL_INFO)
  1832. printk("set_rxenable(%s,%d)n", info->device_name, enable);
  1833. spin_lock_irqsave(&info->lock,flags);
  1834. if (enable) {
  1835. if (!info->rx_enabled)
  1836. rx_start(info);
  1837. } else {
  1838. if (info->rx_enabled)
  1839. rx_stop(info);
  1840. }
  1841. spin_unlock_irqrestore(&info->lock,flags);
  1842. return 0;
  1843. }
  1844. /* wait for specified event to occur
  1845.  * 
  1846.  * Arguments:   info pointer to device instance data
  1847.  *  mask pointer to bitmask of events to wait for
  1848.  * Return Value: 0  if successful and bit mask updated with
  1849.  * of events triggerred,
  1850.  *  otherwise error code
  1851.  */
  1852. static int wait_events(MGSLPC_INFO * info, int * mask_ptr)
  1853. {
  1854.   unsigned long flags;
  1855. int s;
  1856. int rc=0;
  1857. struct mgsl_icount cprev, cnow;
  1858. int events;
  1859. int mask;
  1860. struct _input_signal_events oldsigs, newsigs;
  1861. DECLARE_WAITQUEUE(wait, current);
  1862. COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
  1863. if (rc)
  1864. return  -EFAULT;
  1865.  
  1866. if (debug_level >= DEBUG_LEVEL_INFO)
  1867. printk("wait_events(%s,%d)n", info->device_name, mask);
  1868. spin_lock_irqsave(&info->lock,flags);
  1869. /* return immediately if state matches requested events */
  1870. get_signals(info);
  1871. s = info->serial_signals;
  1872. events = mask &
  1873. ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
  1874.     ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
  1875.   ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
  1876.   ((s & SerialSignal_RI)  ? MgslEvent_RiActive :MgslEvent_RiInactive) );
  1877. if (events) {
  1878. spin_unlock_irqrestore(&info->lock,flags);
  1879. goto exit;
  1880. }
  1881. /* save current irq counts */
  1882. cprev = info->icount;
  1883. oldsigs = info->input_signal_events;
  1884. if ((info->params.mode == MGSL_MODE_HDLC) &&
  1885.     (mask & MgslEvent_ExitHuntMode))
  1886. irq_enable(info, CHA, IRQ_EXITHUNT);
  1887. set_current_state(TASK_INTERRUPTIBLE);
  1888. add_wait_queue(&info->event_wait_q, &wait);
  1889. spin_unlock_irqrestore(&info->lock,flags);
  1890. for(;;) {
  1891. schedule();
  1892. if (signal_pending(current)) {
  1893. rc = -ERESTARTSYS;
  1894. break;
  1895. }
  1896. /* get current irq counts */
  1897. spin_lock_irqsave(&info->lock,flags);
  1898. cnow = info->icount;
  1899. newsigs = info->input_signal_events;
  1900. set_current_state(TASK_INTERRUPTIBLE);
  1901. spin_unlock_irqrestore(&info->lock,flags);
  1902. /* if no change, wait aborted for some reason */
  1903. if (newsigs.dsr_up   == oldsigs.dsr_up   &&
  1904.     newsigs.dsr_down == oldsigs.dsr_down &&
  1905.     newsigs.dcd_up   == oldsigs.dcd_up   &&
  1906.     newsigs.dcd_down == oldsigs.dcd_down &&
  1907.     newsigs.cts_up   == oldsigs.cts_up   &&
  1908.     newsigs.cts_down == oldsigs.cts_down &&
  1909.     newsigs.ri_up    == oldsigs.ri_up    &&
  1910.     newsigs.ri_down  == oldsigs.ri_down  &&
  1911.     cnow.exithunt    == cprev.exithunt   &&
  1912.     cnow.rxidle      == cprev.rxidle) {
  1913. rc = -EIO;
  1914. break;
  1915. }
  1916. events = mask &
  1917. ( (newsigs.dsr_up   != oldsigs.dsr_up   ? MgslEvent_DsrActive:0)   +
  1918.   (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
  1919.   (newsigs.dcd_up   != oldsigs.dcd_up   ? MgslEvent_DcdActive:0)   +
  1920.   (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
  1921.   (newsigs.cts_up   != oldsigs.cts_up   ? MgslEvent_CtsActive:0)   +
  1922.   (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
  1923.   (newsigs.ri_up    != oldsigs.ri_up    ? MgslEvent_RiActive:0)    +
  1924.   (newsigs.ri_down  != oldsigs.ri_down  ? MgslEvent_RiInactive:0)  +
  1925.   (cnow.exithunt    != cprev.exithunt   ? MgslEvent_ExitHuntMode:0) +
  1926.   (cnow.rxidle      != cprev.rxidle     ? MgslEvent_IdleReceived:0) );
  1927. if (events)
  1928. break;
  1929. cprev = cnow;
  1930. oldsigs = newsigs;
  1931. }
  1932. remove_wait_queue(&info->event_wait_q, &wait);
  1933. set_current_state(TASK_RUNNING);
  1934. if (mask & MgslEvent_ExitHuntMode) {
  1935. spin_lock_irqsave(&info->lock,flags);
  1936. if (!waitqueue_active(&info->event_wait_q))
  1937. irq_disable(info, CHA, IRQ_EXITHUNT);
  1938. spin_unlock_irqrestore(&info->lock,flags);
  1939. }
  1940. exit:
  1941. if (rc == 0)
  1942. PUT_USER(rc, events, mask_ptr);
  1943. return rc;
  1944. }
  1945. static int modem_input_wait(MGSLPC_INFO *info,int arg)
  1946. {
  1947.   unsigned long flags;
  1948. int rc;
  1949. struct mgsl_icount cprev, cnow;
  1950. DECLARE_WAITQUEUE(wait, current);
  1951. /* save current irq counts */
  1952. spin_lock_irqsave(&info->lock,flags);
  1953. cprev = info->icount;
  1954. add_wait_queue(&info->status_event_wait_q, &wait);
  1955. set_current_state(TASK_INTERRUPTIBLE);
  1956. spin_unlock_irqrestore(&info->lock,flags);
  1957. for(;;) {
  1958. schedule();
  1959. if (signal_pending(current)) {
  1960. rc = -ERESTARTSYS;
  1961. break;
  1962. }
  1963. /* get new irq counts */
  1964. spin_lock_irqsave(&info->lock,flags);
  1965. cnow = info->icount;
  1966. set_current_state(TASK_INTERRUPTIBLE);
  1967. spin_unlock_irqrestore(&info->lock,flags);
  1968. /* if no change, wait aborted for some reason */
  1969. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1970.     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  1971. rc = -EIO;
  1972. break;
  1973. }
  1974. /* check for change in caller specified modem input */
  1975. if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
  1976.     (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
  1977.     (arg & TIOCM_CD  && cnow.dcd != cprev.dcd) ||
  1978.     (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
  1979. rc = 0;
  1980. break;
  1981. }
  1982. cprev = cnow;
  1983. }
  1984. remove_wait_queue(&info->status_event_wait_q, &wait);
  1985. set_current_state(TASK_RUNNING);
  1986. return rc;
  1987. }
  1988. /* Return state of the serial control/status signals
  1989.  * 
  1990.  * Arguments:   info pointer to device instance data
  1991.  *  value pointer to int to hold returned info
  1992.  * 
  1993.  * Return Value: 0 if success, otherwise error code
  1994.  */
  1995. static int get_modem_info(MGSLPC_INFO * info, unsigned int *value)
  1996. {
  1997. unsigned int result;
  1998.   unsigned long flags;
  1999. int err;
  2000.  
  2001. spin_lock_irqsave(&info->lock,flags);
  2002.   get_signals(info);
  2003. spin_unlock_irqrestore(&info->lock,flags);
  2004. result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) |
  2005.  ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) |
  2006.  ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) |
  2007.  ((info->serial_signals & SerialSignal_RI)  ? TIOCM_RNG:0) |
  2008.  ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) |
  2009.  ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
  2010. if (debug_level >= DEBUG_LEVEL_INFO)
  2011. printk("mgslpc_get_modem_info %s value=%08Xn", info->device_name, result);
  2012. PUT_USER(err,result,value);
  2013. return err ? -EFAULT : 0;
  2014. }
  2015. /* Set the state of the modem control signals (DTR/RTS)
  2016.  * 
  2017.  * Arguments:
  2018.  * 
  2019.  *  info pointer to device instance data
  2020.  *  cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
  2021.  * TIOCMSET = set/clear signal values
  2022.  *  value bit mask for command
  2023.  * 
  2024.  * Return Value: 0 if success, otherwise error code
  2025.  */
  2026. static int set_modem_info(MGSLPC_INFO * info, unsigned int cmd,
  2027.   unsigned int *value)
  2028. {
  2029.   int error;
  2030.   unsigned int arg;
  2031.   unsigned long flags;
  2032.  
  2033. if (debug_level >= DEBUG_LEVEL_INFO)
  2034. printk("mgslpc_set_modem_info %sn", info->device_name);
  2035.   GET_USER(error,arg,value);
  2036.   if (error)
  2037.   return error;
  2038.   switch (cmd) {
  2039.   case TIOCMBIS: 
  2040.   if (arg & TIOCM_RTS)
  2041.   info->serial_signals |= SerialSignal_RTS;
  2042.   if (arg & TIOCM_DTR)
  2043.   info->serial_signals |= SerialSignal_DTR;
  2044.   break;
  2045.   case TIOCMBIC:
  2046.   if (arg & TIOCM_RTS)
  2047.   info->serial_signals &= ~SerialSignal_RTS;
  2048.   if (arg & TIOCM_DTR)
  2049.   info->serial_signals &= ~SerialSignal_DTR;
  2050.   break;
  2051.   case TIOCMSET:
  2052.   if (arg & TIOCM_RTS)
  2053.   info->serial_signals |= SerialSignal_RTS;
  2054. else
  2055.   info->serial_signals &= ~SerialSignal_RTS;
  2056.   if (arg & TIOCM_DTR)
  2057.   info->serial_signals |= SerialSignal_DTR;
  2058. else
  2059.   info->serial_signals &= ~SerialSignal_DTR;
  2060.   break;
  2061.   default:
  2062.   return -EINVAL;
  2063.   }
  2064. spin_lock_irqsave(&info->lock,flags);
  2065.   set_signals(info);
  2066. spin_unlock_irqrestore(&info->lock,flags);
  2067. return 0;
  2068. }
  2069. /* Set or clear transmit break condition
  2070.  *
  2071.  * Arguments: tty pointer to tty instance data
  2072.  * break_state -1=set break condition, 0=clear
  2073.  */
  2074. static void mgslpc_break(struct tty_struct *tty, int break_state)
  2075. {
  2076. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  2077. unsigned long flags;
  2078. if (debug_level >= DEBUG_LEVEL_INFO)
  2079. printk("%s(%d):mgslpc_break(%s,%d)n",
  2080.  __FILE__,__LINE__, info->device_name, break_state);
  2081.  
  2082. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_break"))
  2083. return;
  2084. spin_lock_irqsave(&info->lock,flags);
  2085.   if (break_state == -1)
  2086. set_reg_bits(info, CHA+DAFO, BIT6);
  2087. else 
  2088. clear_reg_bits(info, CHA+DAFO, BIT6);
  2089. spin_unlock_irqrestore(&info->lock,flags);
  2090. }
  2091. /* Service an IOCTL request
  2092.  * 
  2093.  * Arguments:
  2094.  * 
  2095.  *  tty pointer to tty instance data
  2096.  *  file pointer to associated file object for device
  2097.  *  cmd IOCTL command code
  2098.  *  arg command argument/context
  2099.  * 
  2100.  * Return Value: 0 if success, otherwise error code
  2101.  */
  2102. static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
  2103. unsigned int cmd, unsigned long arg)
  2104. {
  2105. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  2106. if (debug_level >= DEBUG_LEVEL_INFO)
  2107. printk("%s(%d):mgslpc_ioctl %s cmd=%08Xn", __FILE__,__LINE__,
  2108. info->device_name, cmd );
  2109. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_ioctl"))
  2110. return -ENODEV;
  2111. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  2112.     (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  2113. if (tty->flags & (1 << TTY_IO_ERROR))
  2114.     return -EIO;
  2115. }
  2116. return ioctl_common(info, cmd, arg);
  2117. }
  2118. int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
  2119. {
  2120. int error;
  2121. struct mgsl_icount cnow; /* kernel counter temps */
  2122. struct serial_icounter_struct *p_cuser; /* user space */
  2123. unsigned long flags;
  2124. switch (cmd) {
  2125. case TIOCMGET:
  2126. return get_modem_info(info, (unsigned int *) arg);
  2127. case TIOCMBIS:
  2128. case TIOCMBIC:
  2129. case TIOCMSET:
  2130. return set_modem_info(info, cmd, (unsigned int *) arg);
  2131. case MGSL_IOCGPARAMS:
  2132. return get_params(info,(MGSL_PARAMS *)arg);
  2133. case MGSL_IOCSPARAMS:
  2134. return set_params(info,(MGSL_PARAMS *)arg);
  2135. case MGSL_IOCGTXIDLE:
  2136. return get_txidle(info,(int*)arg);
  2137. case MGSL_IOCSTXIDLE:
  2138. return set_txidle(info,(int)arg);
  2139. case MGSL_IOCGIF:
  2140. return get_interface(info,(int*)arg);
  2141. case MGSL_IOCSIF:
  2142. return set_interface(info,(int)arg);
  2143. case MGSL_IOCTXENABLE:
  2144. return set_txenable(info,(int)arg);
  2145. case MGSL_IOCRXENABLE:
  2146. return set_rxenable(info,(int)arg);
  2147. case MGSL_IOCTXABORT:
  2148. return tx_abort(info);
  2149. case MGSL_IOCGSTATS:
  2150. return get_stats(info,(struct mgsl_icount*)arg);
  2151. case MGSL_IOCWAITEVENT:
  2152. return wait_events(info,(int*)arg);
  2153. case MGSL_IOCCLRMODCOUNT:
  2154. while(MOD_IN_USE)
  2155. MOD_DEC_USE_COUNT;
  2156. return 0;
  2157. case TIOCMIWAIT:
  2158. return modem_input_wait(info,(int)arg);
  2159. case TIOCGICOUNT:
  2160. spin_lock_irqsave(&info->lock,flags);
  2161. cnow = info->icount;
  2162. spin_unlock_irqrestore(&info->lock,flags);
  2163. p_cuser = (struct serial_icounter_struct *) arg;
  2164. PUT_USER(error,cnow.cts, &p_cuser->cts);
  2165. if (error) return error;
  2166. PUT_USER(error,cnow.dsr, &p_cuser->dsr);
  2167. if (error) return error;
  2168. PUT_USER(error,cnow.rng, &p_cuser->rng);
  2169. if (error) return error;
  2170. PUT_USER(error,cnow.dcd, &p_cuser->dcd);
  2171. if (error) return error;
  2172. PUT_USER(error,cnow.rx, &p_cuser->rx);
  2173. if (error) return error;
  2174. PUT_USER(error,cnow.tx, &p_cuser->tx);
  2175. if (error) return error;
  2176. PUT_USER(error,cnow.frame, &p_cuser->frame);
  2177. if (error) return error;
  2178. PUT_USER(error,cnow.overrun, &p_cuser->overrun);
  2179. if (error) return error;
  2180. PUT_USER(error,cnow.parity, &p_cuser->parity);
  2181. if (error) return error;
  2182. PUT_USER(error,cnow.brk, &p_cuser->brk);
  2183. if (error) return error;
  2184. PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
  2185. if (error) return error;
  2186. return 0;
  2187. default:
  2188. return -ENOIOCTLCMD;
  2189. }
  2190. return 0;
  2191. }
  2192. /* Set new termios settings
  2193.  * 
  2194.  * Arguments:
  2195.  * 
  2196.  *  tty pointer to tty structure
  2197.  *  termios pointer to buffer to hold returned old termios
  2198.  */
  2199. static void mgslpc_set_termios(struct tty_struct *tty, struct termios *old_termios)
  2200. {
  2201. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  2202. unsigned long flags;
  2203. if (debug_level >= DEBUG_LEVEL_INFO)
  2204. printk("%s(%d):mgslpc_set_termios %sn", __FILE__,__LINE__,
  2205. tty->driver.name );
  2206. /* just return if nothing has changed */
  2207. if ((tty->termios->c_cflag == old_termios->c_cflag)
  2208.     && (RELEVANT_IFLAG(tty->termios->c_iflag) 
  2209. == RELEVANT_IFLAG(old_termios->c_iflag)))
  2210.   return;
  2211. mgslpc_change_params(info);
  2212. /* Handle transition to B0 status */
  2213. if (old_termios->c_cflag & CBAUD &&
  2214.     !(tty->termios->c_cflag & CBAUD)) {
  2215. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  2216. spin_lock_irqsave(&info->lock,flags);
  2217.   set_signals(info);
  2218. spin_unlock_irqrestore(&info->lock,flags);
  2219. }
  2220. /* Handle transition away from B0 status */
  2221. if (!(old_termios->c_cflag & CBAUD) &&
  2222.     tty->termios->c_cflag & CBAUD) {
  2223. info->serial_signals |= SerialSignal_DTR;
  2224.   if (!(tty->termios->c_cflag & CRTSCTS) || 
  2225.       !test_bit(TTY_THROTTLED, &tty->flags)) {
  2226. info->serial_signals |= SerialSignal_RTS;
  2227.   }
  2228. spin_lock_irqsave(&info->lock,flags);
  2229.   set_signals(info);
  2230. spin_unlock_irqrestore(&info->lock,flags);
  2231. }
  2232. /* Handle turning off CRTSCTS */
  2233. if (old_termios->c_cflag & CRTSCTS &&
  2234.     !(tty->termios->c_cflag & CRTSCTS)) {
  2235. tty->hw_stopped = 0;
  2236. tx_release(tty);
  2237. }
  2238. }
  2239. static void mgslpc_close(struct tty_struct *tty, struct file * filp)
  2240. {
  2241. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  2242. if (!info || mgslpc_paranoia_check(info, tty->device, "mgslpc_close"))
  2243. return;
  2244. if (debug_level >= DEBUG_LEVEL_INFO)
  2245. printk("%s(%d):mgslpc_close(%s) entry, count=%dn",
  2246.  __FILE__,__LINE__, info->device_name, info->count);
  2247.  
  2248. if (!info->count || tty_hung_up_p(filp))
  2249. goto cleanup;
  2250. if ((tty->count == 1) && (info->count != 1)) {
  2251. /*
  2252.  * tty->count is 1 and the tty structure will be freed.
  2253.  * info->count should be one in this case.
  2254.  * if it's not, correct it so that the port is shutdown.
  2255.  */
  2256. printk("mgslpc_close: bad refcount; tty->count is 1, "
  2257.        "info->count is %dn", info->count);
  2258. info->count = 1;
  2259. }
  2260. info->count--;
  2261. /* if at least one open remaining, leave hardware active */
  2262. if (info->count)
  2263. goto cleanup;
  2264. info->flags |= ASYNC_CLOSING;
  2265. /* Save the termios structure, since this port may have
  2266.  * separate termios for callout and dialin.
  2267.  */
  2268. if (info->flags & ASYNC_NORMAL_ACTIVE)
  2269. info->normal_termios = *tty->termios;
  2270. if (info->flags & ASYNC_CALLOUT_ACTIVE)
  2271. info->callout_termios = *tty->termios;
  2272. /* set tty->closing to notify line discipline to 
  2273.  * only process XON/XOFF characters. Only the N_TTY
  2274.  * discipline appears to use this (ppp does not).
  2275.  */
  2276. tty->closing = 1;
  2277. /* wait for transmit data to clear all layers */
  2278. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
  2279. if (debug_level >= DEBUG_LEVEL_INFO)
  2280. printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sentn",
  2281.  __FILE__,__LINE__, info->device_name );
  2282. tty_wait_until_sent(tty, info->closing_wait);
  2283. }
  2284.   if (info->flags & ASYNC_INITIALIZED)
  2285.   mgslpc_wait_until_sent(tty, info->timeout);
  2286. if (tty->driver.flush_buffer)
  2287. tty->driver.flush_buffer(tty);
  2288. if (tty->ldisc.flush_buffer)
  2289. tty->ldisc.flush_buffer(tty);
  2290. shutdown(info);
  2291. tty->closing = 0;
  2292. info->tty = 0;
  2293. if (info->blocked_open) {
  2294. if (info->close_delay) {
  2295. set_current_state(TASK_INTERRUPTIBLE);
  2296. schedule_timeout(info->close_delay);
  2297. }
  2298. wake_up_interruptible(&info->open_wait);
  2299. }
  2300. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
  2301.  ASYNC_CLOSING);
  2302.  
  2303. wake_up_interruptible(&info->close_wait);
  2304. cleanup:
  2305. if (debug_level >= DEBUG_LEVEL_INFO)
  2306. printk("%s(%d):mgslpc_close(%s) exit, count=%dn", __FILE__,__LINE__,
  2307. tty->driver.name, info->count);
  2308. if(MOD_IN_USE)
  2309. MOD_DEC_USE_COUNT;
  2310. }
  2311. /* Wait until the transmitter is empty.
  2312.  */
  2313. static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
  2314. {
  2315. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  2316. unsigned long orig_jiffies, char_time;
  2317. if (!info )
  2318. return;
  2319. if (debug_level >= DEBUG_LEVEL_INFO)
  2320. printk("%s(%d):mgslpc_wait_until_sent(%s) entryn",
  2321.  __FILE__,__LINE__, info->device_name );
  2322.       
  2323. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_wait_until_sent"))
  2324. return;
  2325. if (!(info->flags & ASYNC_INITIALIZED))
  2326. goto exit;
  2327.  
  2328. orig_jiffies = jiffies;
  2329.       
  2330. /* Set check interval to 1/5 of estimated time to
  2331.  * send a character, and make it at least 1. The check
  2332.  * interval should also be less than the timeout.
  2333.  * Note: use tight timings here to satisfy the NIST-PCTS.
  2334.  */ 
  2335.        
  2336. if ( info->params.data_rate ) {
  2337.         char_time = info->timeout/(32 * 5);
  2338. if (!char_time)
  2339. char_time++;
  2340. } else
  2341. char_time = 1;
  2342. if (timeout)
  2343. char_time = MIN(char_time, timeout);
  2344. if (info->params.mode == MGSL_MODE_HDLC) {
  2345. while (info->tx_active) {
  2346. set_current_state(TASK_INTERRUPTIBLE);
  2347. schedule_timeout(char_time);
  2348. if (signal_pending(current))
  2349. break;
  2350. if (timeout && ((orig_jiffies + timeout) < jiffies))
  2351. break;
  2352. }
  2353. } else {
  2354. while ((info->tx_count || info->tx_active) &&
  2355. info->tx_enabled) {
  2356. set_current_state(TASK_INTERRUPTIBLE);
  2357. schedule_timeout(char_time);
  2358. if (signal_pending(current))
  2359. break;
  2360. if (timeout && ((orig_jiffies + timeout) < jiffies))
  2361. break;
  2362. }
  2363. }
  2364.       
  2365. exit:
  2366. if (debug_level >= DEBUG_LEVEL_INFO)
  2367. printk("%s(%d):mgslpc_wait_until_sent(%s) exitn",
  2368.  __FILE__,__LINE__, info->device_name );
  2369. }
  2370. /* Called by tty_hangup() when a hangup is signaled.
  2371.  * This is the same as closing all open files for the port.
  2372.  */
  2373. static void mgslpc_hangup(struct tty_struct *tty)
  2374. {
  2375. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  2376. if (debug_level >= DEBUG_LEVEL_INFO)
  2377. printk("%s(%d):mgslpc_hangup(%s)n",
  2378.  __FILE__,__LINE__, info->device_name );
  2379.  
  2380. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_hangup"))
  2381. return;
  2382. mgslpc_flush_buffer(tty);
  2383. shutdown(info);
  2384. info->count = 0;
  2385. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
  2386. info->tty = 0;
  2387. wake_up_interruptible(&info->open_wait);
  2388. }
  2389. /* Block the current process until the specified port
  2390.  * is ready to be opened.
  2391.  */
  2392. static int block_til_ready(struct tty_struct *tty, struct file *filp,
  2393.    MGSLPC_INFO *info)
  2394. {
  2395. DECLARE_WAITQUEUE(wait, current);
  2396. int retval;
  2397. int do_clocal = 0, extra_count = 0;
  2398. unsigned long flags;
  2399. if (debug_level >= DEBUG_LEVEL_INFO)
  2400. printk("%s(%d):block_til_ready on %sn",
  2401.  __FILE__,__LINE__, tty->driver.name );
  2402. if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
  2403. /* this is a callout device */
  2404. /* just verify that normal device is not in use */
  2405. if (info->flags & ASYNC_NORMAL_ACTIVE)
  2406. return -EBUSY;
  2407. if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
  2408.     (info->flags & ASYNC_SESSION_LOCKOUT) &&
  2409.     (info->session != current->session))
  2410.     return -EBUSY;
  2411. if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
  2412.     (info->flags & ASYNC_PGRP_LOCKOUT) &&
  2413.     (info->pgrp != current->pgrp))
  2414.     return -EBUSY;
  2415. info->flags |= ASYNC_CALLOUT_ACTIVE;
  2416. return 0;
  2417. }
  2418. if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
  2419. /* nonblock mode is set or port is not enabled */
  2420. /* just verify that callout device is not active */
  2421. if (info->flags & ASYNC_CALLOUT_ACTIVE)
  2422. return -EBUSY;
  2423. info->flags |= ASYNC_NORMAL_ACTIVE;
  2424. return 0;
  2425. }
  2426. if (info->flags & ASYNC_CALLOUT_ACTIVE) {
  2427. if (info->normal_termios.c_cflag & CLOCAL)
  2428. do_clocal = 1;
  2429. } else {
  2430. if (tty->termios->c_cflag & CLOCAL)
  2431. do_clocal = 1;
  2432. }
  2433. /* Wait for carrier detect and the line to become
  2434.  * free (i.e., not in use by the callout).  While we are in
  2435.  * this loop, info->count is dropped by one, so that
  2436.  * mgslpc_close() knows when to free things.  We restore it upon
  2437.  * exit, either normal or abnormal.
  2438.  */
  2439.  
  2440. retval = 0;
  2441. add_wait_queue(&info->open_wait, &wait);
  2442. if (debug_level >= DEBUG_LEVEL_INFO)
  2443. printk("%s(%d):block_til_ready before block on %s count=%dn",
  2444.  __FILE__,__LINE__, tty->driver.name, info->count );
  2445. save_flags(flags); cli();
  2446. if (!tty_hung_up_p(filp)) {
  2447. extra_count = 1;
  2448. info->count--;
  2449. }
  2450. restore_flags(flags);
  2451. info->blocked_open++;
  2452. while (1) {
  2453. if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
  2454.       (tty->termios->c_cflag & CBAUD)) {
  2455. spin_lock_irqsave(&info->lock,flags);
  2456. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  2457.   set_signals(info);
  2458. spin_unlock_irqrestore(&info->lock,flags);
  2459. }
  2460. set_current_state(TASK_INTERRUPTIBLE);
  2461. if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
  2462. retval = (info->flags & ASYNC_HUP_NOTIFY) ?
  2463. -EAGAIN : -ERESTARTSYS;
  2464. break;
  2465. }
  2466. spin_lock_irqsave(&info->lock,flags);
  2467.   get_signals(info);
  2468. spin_unlock_irqrestore(&info->lock,flags);
  2469.   if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
  2470.       !(info->flags & ASYNC_CLOSING) &&
  2471.       (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
  2472.   break;
  2473. }
  2474. if (signal_pending(current)) {
  2475. retval = -ERESTARTSYS;
  2476. break;
  2477. }
  2478. if (debug_level >= DEBUG_LEVEL_INFO)
  2479. printk("%s(%d):block_til_ready blocking on %s count=%dn",
  2480.  __FILE__,__LINE__, tty->driver.name, info->count );
  2481.  
  2482. schedule();
  2483. }
  2484. set_current_state(TASK_RUNNING);
  2485. remove_wait_queue(&info->open_wait, &wait);
  2486. if (extra_count)
  2487. info->count++;
  2488. info->blocked_open--;
  2489. if (debug_level >= DEBUG_LEVEL_INFO)
  2490. printk("%s(%d):block_til_ready after blocking on %s count=%dn",
  2491.  __FILE__,__LINE__, tty->driver.name, info->count );
  2492.  
  2493. if (!retval)
  2494. info->flags |= ASYNC_NORMAL_ACTIVE;
  2495. return retval;
  2496. }
  2497. static int mgslpc_open(struct tty_struct *tty, struct file * filp)
  2498. {
  2499. MGSLPC_INFO *info;
  2500. int  retval, line;
  2501. unsigned long flags;
  2502. /* verify range of specified line number */
  2503. line = MINOR(tty->device) - tty->driver.minor_start;
  2504. if ((line < 0) || (line >= mgslpc_device_count)) {
  2505. printk("%s(%d):mgslpc_open with illegal line #%d.n",
  2506. __FILE__,__LINE__,line);
  2507. return -ENODEV;
  2508. }
  2509. /* find the info structure for the specified line */
  2510. info = mgslpc_device_list;
  2511. while(info && info->line != line)
  2512. info = info->next_device;
  2513. if ( !info ){
  2514. printk("%s(%d):Can't find specified device on open (line=%d)n",
  2515. __FILE__,__LINE__,line);
  2516. return -ENODEV;
  2517. }
  2518. tty->driver_data = info;
  2519. info->tty = tty;
  2520. if (mgslpc_paranoia_check(info, tty->device, "mgslpc_open"))
  2521. return -ENODEV;
  2522. if (debug_level >= DEBUG_LEVEL_INFO)
  2523. printk("%s(%d):mgslpc_open(%s), old ref count = %dn",
  2524.  __FILE__,__LINE__,tty->driver.name, info->count);
  2525. MOD_INC_USE_COUNT;
  2526. /* If port is closing, signal caller to try again */
  2527. if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
  2528. if (info->flags & ASYNC_CLOSING)
  2529. interruptible_sleep_on(&info->close_wait);
  2530. retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
  2531. -EAGAIN : -ERESTARTSYS);
  2532. goto cleanup;
  2533. }
  2534. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  2535. spin_lock_irqsave(&info->netlock, flags);
  2536. if (info->netcount) {
  2537. retval = -EBUSY;
  2538. spin_unlock_irqrestore(&info->netlock, flags);
  2539. goto cleanup;
  2540. }
  2541. info->count++;
  2542. spin_unlock_irqrestore(&info->netlock, flags);
  2543. if (info->count == 1) {
  2544. /* 1st open on this device, init hardware */
  2545. retval = startup(info);
  2546. if (retval < 0)
  2547. goto cleanup;
  2548. }
  2549. retval = block_til_ready(tty, filp, info);
  2550. if (retval) {
  2551. if (debug_level >= DEBUG_LEVEL_INFO)
  2552. printk("%s(%d):block_til_ready(%s) returned %dn",
  2553.  __FILE__,__LINE__, info->device_name, retval);
  2554. goto cleanup;
  2555. }
  2556. if ((info->count == 1) &&
  2557.     info->flags & ASYNC_SPLIT_TERMIOS) {
  2558. if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
  2559. *tty->termios = info->normal_termios;
  2560. else 
  2561. *tty->termios = info->callout_termios;
  2562. mgslpc_change_params(info);
  2563. }
  2564. info->session = current->session;
  2565. info->pgrp    = current->pgrp;
  2566. if (debug_level >= DEBUG_LEVEL_INFO)
  2567. printk("%s(%d):mgslpc_open(%s) successn",
  2568.  __FILE__,__LINE__, info->device_name);
  2569. retval = 0;
  2570. cleanup:
  2571. if (retval) {
  2572. if(MOD_IN_USE)
  2573. MOD_DEC_USE_COUNT;
  2574. if(info->count)
  2575. info->count--;
  2576. }
  2577. return retval;
  2578. }
  2579. /*
  2580.  * /proc fs routines....
  2581.  */
  2582. static inline int line_info(char *buf, MGSLPC_INFO *info)
  2583. {
  2584. char stat_buf[30];
  2585. int ret;
  2586. unsigned long flags;
  2587. ret = sprintf(buf, "%s:io:%04X irq:%d",
  2588.       info->device_name, info->io_base, info->irq_level);
  2589. /* output current serial signal states */
  2590. spin_lock_irqsave(&info->lock,flags);
  2591.   get_signals(info);
  2592. spin_unlock_irqrestore(&info->lock,flags);
  2593. stat_buf[0] = 0;
  2594. stat_buf[1] = 0;
  2595. if (info->serial_signals & SerialSignal_RTS)
  2596. strcat(stat_buf, "|RTS");
  2597. if (info->serial_signals & SerialSignal_CTS)
  2598. strcat(stat_buf, "|CTS");
  2599. if (info->serial_signals & SerialSignal_DTR)
  2600. strcat(stat_buf, "|DTR");
  2601. if (info->serial_signals & SerialSignal_DSR)
  2602. strcat(stat_buf, "|DSR");
  2603. if (info->serial_signals & SerialSignal_DCD)
  2604. strcat(stat_buf, "|CD");
  2605. if (info->serial_signals & SerialSignal_RI)
  2606. strcat(stat_buf, "|RI");
  2607. if (info->params.mode == MGSL_MODE_HDLC) {
  2608. ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
  2609.       info->icount.txok, info->icount.rxok);
  2610. if (info->icount.txunder)
  2611. ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
  2612. if (info->icount.txabort)
  2613. ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
  2614. if (info->icount.rxshort)
  2615. ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
  2616. if (info->icount.rxlong)
  2617. ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
  2618. if (info->icount.rxover)
  2619. ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
  2620. if (info->icount.rxcrc)
  2621. ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxcrc);
  2622. } else {
  2623. ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
  2624.       info->icount.tx, info->icount.rx);
  2625. if (info->icount.frame)
  2626. ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
  2627. if (info->icount.parity)
  2628. ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
  2629. if (info->icount.brk)
  2630. ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
  2631. if (info->icount.overrun)
  2632. ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
  2633. }
  2634. /* Append serial signal status to end */
  2635. ret += sprintf(buf+ret, " %sn", stat_buf+1);
  2636. ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%xn",
  2637.        info->tx_active,info->bh_requested,info->bh_running,
  2638.        info->pending_bh);
  2639. return ret;
  2640. }
  2641. /* Called to print information about devices
  2642.  */
  2643. int mgslpc_read_proc(char *page, char **start, off_t off, int count,
  2644.  int *eof, void *data)
  2645. {
  2646. int len = 0, l;
  2647. off_t begin = 0;
  2648. MGSLPC_INFO *info;
  2649. len += sprintf(page, "synclink driver:%sn", driver_version);
  2650. info = mgslpc_device_list;
  2651. while( info ) {
  2652. l = line_info(page + len, info);
  2653. len += l;
  2654. if (len+begin > off+count)
  2655. goto done;
  2656. if (len+begin < off) {
  2657. begin += len;
  2658. len = 0;
  2659. }
  2660. info = info->next_device;
  2661. }
  2662. *eof = 1;
  2663. done:
  2664. if (off >= len+begin)
  2665. return 0;
  2666. *start = page + (off-begin);
  2667. return ((count < begin+len-off) ? count : begin+len-off);
  2668. }
  2669. int rx_alloc_buffers(MGSLPC_INFO *info)
  2670. {
  2671. /* each buffer has header and data */
  2672. info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
  2673. /* calculate total allocation size for 8 buffers */
  2674. info->rx_buf_total_size = info->rx_buf_size * 8;
  2675. /* limit total allocated memory */
  2676. if (info->rx_buf_total_size > 0x10000)
  2677. info->rx_buf_total_size = 0x10000;
  2678. /* calculate number of buffers */
  2679. info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
  2680. info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
  2681. if (info->rx_buf == NULL)
  2682. return -ENOMEM;
  2683. rx_reset_buffers(info);
  2684. return 0;
  2685. }
  2686. void rx_free_buffers(MGSLPC_INFO *info)
  2687. {
  2688. if (info->rx_buf)
  2689. kfree(info->rx_buf);
  2690. info->rx_buf = NULL;
  2691. }
  2692. int claim_resources(MGSLPC_INFO *info)
  2693. {
  2694. if (rx_alloc_buffers(info) < 0 ) {
  2695. printk( "Cant allocate rx buffer %sn", info->device_name);
  2696. release_resources(info);
  2697. return -ENODEV;
  2698. }
  2699. return 0;
  2700. }
  2701. void release_resources(MGSLPC_INFO *info)
  2702. {
  2703. if (debug_level >= DEBUG_LEVEL_INFO)
  2704. printk("release_resources(%s)n", info->device_name);
  2705. rx_free_buffers(info);
  2706. }
  2707. /* Add the specified device instance data structure to the
  2708.  * global linked list of devices and increment the device count.
  2709.  * 
  2710.  * Arguments: info pointer to device instance data
  2711.  */
  2712. void mgslpc_add_device(MGSLPC_INFO *info)
  2713. {
  2714. info->next_device = NULL;
  2715. info->line = mgslpc_device_count;
  2716. sprintf(info->device_name,"ttySLP%d",info->line);
  2717. if (info->line < MAX_DEVICE_COUNT) {
  2718. if (maxframe[info->line])
  2719. info->max_frame_size = maxframe[info->line];
  2720. // info->dosyncppp = dosyncppp[info->line];
  2721. info->dosyncppp = 1;
  2722. }
  2723. mgslpc_device_count++;
  2724. if (!mgslpc_device_list)
  2725. mgslpc_device_list = info;
  2726. else {
  2727. MGSLPC_INFO *current_dev = mgslpc_device_list;
  2728. while( current_dev->next_device )
  2729. current_dev = current_dev->next_device;
  2730. current_dev->next_device = info;
  2731. }
  2732. if (info->max_frame_size < 4096)
  2733. info->max_frame_size = 4096;
  2734. else if (info->max_frame_size > 65535)
  2735. info->max_frame_size = 65535;
  2736. printk( "SyncLink PC Card %s:IO=%04X IRQ=%dn",
  2737. info->device_name, info->io_base, info->irq_level);
  2738. #ifdef CONFIG_SYNCLINK_SYNCPPP
  2739. #ifdef MODULE
  2740. if (info->dosyncppp)
  2741. #endif
  2742. mgslpc_sppp_init(info);
  2743. #endif
  2744. }
  2745. void mgslpc_remove_device(MGSLPC_INFO *remove_info)
  2746. {
  2747. MGSLPC_INFO *info = mgslpc_device_list;
  2748. MGSLPC_INFO *last = NULL;
  2749. while(info) {
  2750. if (info == remove_info) {
  2751. if (last)
  2752. last->next_device = info->next_device;
  2753. else
  2754. mgslpc_device_list = info->next_device;
  2755. #ifdef CONFIG_SYNCLINK_SYNCPPP
  2756. if (info->dosyncppp)
  2757. mgslpc_sppp_delete(info);
  2758. #endif
  2759. release_resources(info);
  2760. kfree(info);
  2761. mgslpc_device_count--;
  2762. return;
  2763. }
  2764. last = info;
  2765. info = info->next_device;
  2766. }
  2767. }
  2768. static int __init synclink_cs_init(void)
  2769. {
  2770.     servinfo_t serv;
  2771.     EXPORT_NO_SYMBOLS;
  2772.     if (break_on_load) {
  2773.     mgslpc_get_text_ptr();
  2774.     BREAKPOINT();
  2775.     }
  2776.     printk("%s %sn", driver_name, driver_version);
  2777.     CardServices(GetCardServicesInfo, &serv);
  2778.     if (serv.Revision != CS_RELEASE_CODE) {
  2779.     printk(KERN_NOTICE "synclink_cs: Card Services release "
  2780.    "does not match!n");
  2781.     return -1;
  2782.     }
  2783.     register_pccard_driver(&dev_info, &mgslpc_attach, &mgslpc_detach);
  2784.     /* Initialize the tty_driver structure */
  2785.     memset(&serial_driver, 0, sizeof(struct tty_driver));
  2786.     serial_driver.magic = TTY_DRIVER_MAGIC;
  2787.     serial_driver.driver_name = "synclink_cs";
  2788.     serial_driver.name = "ttySLP";
  2789.     serial_driver.major = ttymajor;
  2790.     serial_driver.minor_start = 64;
  2791.     serial_driver.num = MAX_DEVICE_COUNT;
  2792.     serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
  2793.     serial_driver.subtype = SERIAL_TYPE_NORMAL;
  2794.     serial_driver.init_termios = tty_std_termios;
  2795.     serial_driver.init_termios.c_cflag =
  2796.     B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2797.     serial_driver.flags = TTY_DRIVER_REAL_RAW;
  2798.     serial_driver.refcount = &serial_refcount;
  2799.     serial_driver.table = serial_table;
  2800.     serial_driver.termios = serial_termios;
  2801.     serial_driver.termios_locked = serial_termios_locked;
  2802.     serial_driver.open = mgslpc_open;
  2803.     serial_driver.close = mgslpc_close;
  2804.     serial_driver.write = mgslpc_write;
  2805.     serial_driver.put_char = mgslpc_put_char;
  2806.     serial_driver.flush_chars = mgslpc_flush_chars;
  2807.     serial_driver.write_room = mgslpc_write_room;
  2808.     serial_driver.chars_in_buffer = mgslpc_chars_in_buffer;
  2809.     serial_driver.flush_buffer = mgslpc_flush_buffer;
  2810.     serial_driver.ioctl = mgslpc_ioctl;
  2811.     serial_driver.throttle = mgslpc_throttle;
  2812.     serial_driver.unthrottle = mgslpc_unthrottle;
  2813.     serial_driver.send_xchar = mgslpc_send_xchar;
  2814.     serial_driver.break_ctl = mgslpc_break;
  2815.     serial_driver.wait_until_sent = mgslpc_wait_until_sent;
  2816.     serial_driver.read_proc = mgslpc_read_proc;
  2817.     serial_driver.set_termios = mgslpc_set_termios;
  2818.     serial_driver.stop = tx_pause;
  2819.     serial_driver.start = tx_release;
  2820.     serial_driver.hangup = mgslpc_hangup;
  2821.     /*
  2822.      * The callout device is just like normal device except for
  2823.      * major number and the subtype code.
  2824.      */
  2825.     callout_driver = serial_driver;
  2826.     callout_driver.name = "cuaSLP";
  2827.     callout_driver.major = cuamajor;
  2828.     callout_driver.subtype = SERIAL_TYPE_CALLOUT;
  2829.     callout_driver.read_proc = 0;
  2830.     callout_driver.proc_entry = 0;
  2831.     if (tty_register_driver(&serial_driver) < 0)
  2832.     printk("%s(%d):Couldn't register serial drivern",
  2833.    __FILE__,__LINE__);
  2834.     if (tty_register_driver(&callout_driver) < 0)
  2835.     printk("%s(%d):Couldn't register callout drivern",
  2836.    __FILE__,__LINE__);
  2837.     printk("%s %s, tty major#%d callout major#%dn",
  2838.    driver_name, driver_version,
  2839.    serial_driver.major, callout_driver.major);
  2840.     return 0;
  2841. }
  2842. static void __exit synclink_cs_exit(void) 
  2843. {
  2844. unsigned long flags;
  2845. int rc;
  2846. printk("Unloading %s: version %sn", driver_name, driver_version);
  2847. while(mgslpc_device_list)
  2848. mgslpc_remove_device(mgslpc_device_list);
  2849. save_flags(flags);
  2850. cli();
  2851. if ((rc = tty_unregister_driver(&serial_driver)))
  2852. printk("%s(%d) failed to unregister tty driver err=%dn",
  2853.        __FILE__,__LINE__,rc);
  2854. if ((rc = tty_unregister_driver(&callout_driver)))
  2855. printk("%s(%d) failed to unregister callout driver err=%dn",
  2856.        __FILE__,__LINE__,rc);
  2857. restore_flags(flags);
  2858. unregister_pccard_driver(&dev_info);
  2859. while (dev_list != NULL) {
  2860. del_timer(&dev_list->release);
  2861. if (dev_list->state & DEV_CONFIG)
  2862. mgslpc_release((u_long)dev_list);
  2863. mgslpc_detach(dev_list);
  2864. }
  2865. }
  2866. module_init(synclink_cs_init);
  2867. module_exit(synclink_cs_exit);
  2868. void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate) 
  2869. {
  2870. unsigned int M, N;
  2871. unsigned char val;
  2872. /* note:standard BRG mode is broken in V3.2 chip 
  2873.  * so enhanced mode is always used 
  2874.  */
  2875. if (rate) {
  2876. N = 3686400 / rate;
  2877. if (!N)
  2878. N = 1;
  2879. N >>= 1;
  2880. for (M = 1; N > 64 && M < 16; M++)
  2881. N >>= 1;
  2882. N--;
  2883. /* BGR[5..0] = N
  2884.  * BGR[9..6] = M
  2885.  * BGR[7..0] contained in BGR register
  2886.  * BGR[9..8] contained in CCR2[7..6]
  2887.  * divisor = (N+1)*2^M
  2888.  *
  2889.  * Note: M *must* not be zero (causes asymetric duty cycle)
  2890.  */ 
  2891. write_reg(info, (unsigned char) (channel + BGR),
  2892.   (unsigned char) ((M << 6) + N));
  2893. val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
  2894. val |= ((M << 4) & 0xc0);
  2895. write_reg(info, (unsigned char) (channel + CCR2), val);
  2896. }
  2897. }
  2898. /* Enabled the AUX clock output at the specified frequency.
  2899.  */
  2900. void enable_auxclk(MGSLPC_INFO *info)
  2901. {
  2902. unsigned char val;
  2903. /* MODE
  2904.  *
  2905.  * 07..06  MDS[1..0] 10 = transparent HDLC mode
  2906.  * 05      ADM Address Mode, 0 = no addr recognition
  2907.  * 04      TMD Timer Mode, 0 = external
  2908.  * 03      RAC Receiver Active, 0 = inactive
  2909.  * 02      RTS 0=RTS active during xmit, 1=RTS always active
  2910.  * 01      TRS Timer Resolution, 1=512
  2911.  * 00      TLP Test Loop, 0 = no loop
  2912.  *
  2913.  * 1000 0010
  2914.  */ 
  2915. val = 0x82;
  2916. /* channel B RTS is used to enable AUXCLK driver on SP505 */ 
  2917. if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
  2918. val |= BIT2;
  2919. write_reg(info, CHB + MODE, val);
  2920. /* CCR0
  2921.  *
  2922.  * 07      PU Power Up, 1=active, 0=power down
  2923.  * 06      MCE Master Clock Enable, 1=enabled
  2924.  * 05      Reserved, 0
  2925.  * 04..02  SC[2..0] Encoding
  2926.  * 01..00  SM[1..0] Serial Mode, 00=HDLC
  2927.  *
  2928.  * 11000000
  2929.  */ 
  2930. write_reg(info, CHB + CCR0, 0xc0);
  2931. /* CCR1
  2932.  *
  2933.  * 07      SFLG Shared Flag, 0 = disable shared flags
  2934.  * 06      GALP Go Active On Loop, 0 = not used
  2935.  * 05      GLP Go On Loop, 0 = not used
  2936.  * 04      ODS Output Driver Select, 1=TxD is push-pull output
  2937.  * 03      ITF Interframe Time Fill, 0=mark, 1=flag
  2938.  * 02..00  CM[2..0] Clock Mode
  2939.  *
  2940.  * 0001 0111
  2941.  */ 
  2942. write_reg(info, CHB + CCR1, 0x17);
  2943. /* CCR2 (Channel B)
  2944.  *
  2945.  * 07..06  BGR[9..8] Baud rate bits 9..8
  2946.  * 05      BDF Baud rate divisor factor, 0=1, 1=BGR value
  2947.  * 04      SSEL Clock source select, 1=submode b
  2948.  * 03      TOE 0=TxCLK is input, 1=TxCLK is output
  2949.  * 02      RWX Read/Write Exchange 0=disabled
  2950.  * 01      C32, CRC select, 0=CRC-16, 1=CRC-32
  2951.  * 00      DIV, data inversion 0=disabled, 1=enabled
  2952.  *
  2953.  * 0011 1000
  2954.  */ 
  2955. if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
  2956. write_reg(info, CHB + CCR2, 0x38);
  2957. else
  2958. write_reg(info, CHB + CCR2, 0x30);
  2959. /* CCR4
  2960.  *
  2961.  * 07      MCK4 Master Clock Divide by 4, 1=enabled
  2962.  * 06      EBRG Enhanced Baud Rate Generator Mode, 1=enabled
  2963.  * 05      TST1 Test Pin, 0=normal operation
  2964.  * 04      ICD Ivert Carrier Detect, 1=enabled (active low)
  2965.  * 03..02  Reserved, must be 0
  2966.  * 01..00  RFT[1..0] RxFIFO Threshold 00=32 bytes
  2967.  *
  2968.  * 0101 0000
  2969.  */ 
  2970. write_reg(info, CHB + CCR4, 0x50);
  2971. /* if auxclk not enabled, set internal BRG so
  2972.  * CTS transitions can be detected (requires TxC)
  2973.  */ 
  2974. if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
  2975. mgslpc_set_rate(info, CHB, info->params.clock_speed);
  2976. else
  2977. mgslpc_set_rate(info, CHB, 921600);
  2978. }
  2979. static void loopback_enable(MGSLPC_INFO *info) 
  2980. {
  2981. unsigned char val;
  2982. /* CCR1:02..00  CM[2..0] Clock Mode = 111 (clock mode 7) */ 
  2983. val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
  2984. write_reg(info, CHA + CCR1, val);
  2985. /* CCR2:04 SSEL Clock source select, 1=submode b */ 
  2986. val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
  2987. write_reg(info, CHA + CCR2, val);
  2988. /* set LinkSpeed if available, otherwise default to 2Mbps */ 
  2989. if (info->params.clock_speed)
  2990. mgslpc_set_rate(info, CHA, info->params.clock_speed);
  2991. else
  2992. mgslpc_set_rate(info, CHA, 1843200);
  2993. /* MODE:00 TLP Test Loop, 1=loopback enabled */ 
  2994. val = read_reg(info, CHA + MODE) | BIT0;
  2995. write_reg(info, CHA + MODE, val);
  2996. }
  2997. void hdlc_mode(MGSLPC_INFO *info)
  2998. {
  2999. unsigned char val;
  3000. unsigned char clkmode, clksubmode;
  3001. /* disable all interrupts */ 
  3002. irq_disable(info, CHA, 0xffff);
  3003. irq_disable(info, CHB, 0xffff);
  3004. port_irq_disable(info, 0xff);
  3005. /* assume clock mode 0a, rcv=RxC xmt=TxC */ 
  3006. clkmode = clksubmode = 0;
  3007. if (info->params.flags & HDLC_FLAG_RXC_DPLL
  3008.     && info->params.flags & HDLC_FLAG_TXC_DPLL) {
  3009. /* clock mode 7a, rcv = DPLL, xmt = DPLL */ 
  3010. clkmode = 7;
  3011. } else if (info->params.flags & HDLC_FLAG_RXC_BRG
  3012.  && info->params.flags & HDLC_FLAG_TXC_BRG) {
  3013. /* clock mode 7b, rcv = BRG, xmt = BRG */ 
  3014. clkmode = 7;
  3015. clksubmode = 1;
  3016. } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
  3017. if (info->params.flags & HDLC_FLAG_TXC_BRG) {
  3018. /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */ 
  3019. clkmode = 6;
  3020. clksubmode = 1;
  3021. } else {
  3022. /* clock mode 6a, rcv = DPLL, xmt = TxC */ 
  3023. clkmode = 6;
  3024. }
  3025. } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
  3026. /* clock mode 0b, rcv = RxC, xmt = BRG */ 
  3027. clksubmode = 1;
  3028. }
  3029. /* MODE
  3030.  *
  3031.  * 07..06  MDS[1..0] 10 = transparent HDLC mode
  3032.  * 05      ADM Address Mode, 0 = no addr recognition
  3033.  * 04      TMD Timer Mode, 0 = external
  3034.  * 03      RAC Receiver Active, 0 = inactive
  3035.  * 02      RTS 0=RTS active during xmit, 1=RTS always active
  3036.  * 01      TRS Timer Resolution, 1=512
  3037.  * 00      TLP Test Loop, 0 = no loop
  3038.  *
  3039.  * 1000 0010
  3040.  */ 
  3041. val = 0x82;
  3042. if (info->params.loopback)
  3043. val |= BIT0;
  3044. /* preserve RTS state */ 
  3045. if (info->serial_signals & SerialSignal_RTS)
  3046. val |= BIT2;
  3047. write_reg(info, CHA + MODE, val);
  3048. /* CCR0
  3049.  *
  3050.  * 07      PU Power Up, 1=active, 0=power down
  3051.  * 06      MCE Master Clock Enable, 1=enabled
  3052.  * 05      Reserved, 0
  3053.  * 04..02  SC[2..0] Encoding
  3054.  * 01..00  SM[1..0] Serial Mode, 00=HDLC
  3055.  *
  3056.  * 11000000
  3057.  */ 
  3058. val = 0xc0;
  3059. switch (info->params.encoding)
  3060. {
  3061. case HDLC_ENCODING_NRZI:
  3062. val |= BIT3;
  3063. break;
  3064. case HDLC_ENCODING_BIPHASE_SPACE:
  3065. val |= BIT4;
  3066. break; // FM0
  3067. case HDLC_ENCODING_BIPHASE_MARK:
  3068. val |= BIT4 + BIT2;
  3069. break; // FM1
  3070. case HDLC_ENCODING_BIPHASE_LEVEL:
  3071. val |= BIT4 + BIT3;
  3072. break; // Manchester
  3073. }
  3074. write_reg(info, CHA + CCR0, val);
  3075. /* CCR1
  3076.  *
  3077.  * 07      SFLG Shared Flag, 0 = disable shared flags
  3078.  * 06      GALP Go Active On Loop, 0 = not used
  3079.  * 05      GLP Go On Loop, 0 = not used
  3080.  * 04      ODS Output Driver Select, 1=TxD is push-pull output
  3081.  * 03      ITF Interframe Time Fill, 0=mark, 1=flag
  3082.  * 02..00  CM[2..0] Clock Mode
  3083.  *
  3084.  * 0001 0000
  3085.  */ 
  3086. val = 0x10 + clkmode;
  3087. write_reg(info, CHA + CCR1, val);
  3088. /* CCR2
  3089.  *
  3090.  * 07..06  BGR[9..8] Baud rate bits 9..8
  3091.  * 05      BDF Baud rate divisor factor, 0=1, 1=BGR value
  3092.  * 04      SSEL Clock source select, 1=submode b
  3093.  * 03      TOE 0=TxCLK is input, 0=TxCLK is input
  3094.  * 02      RWX Read/Write Exchange 0=disabled
  3095.  * 01      C32, CRC select, 0=CRC-16, 1=CRC-32
  3096.  * 00      DIV, data inversion 0=disabled, 1=enabled
  3097.  *
  3098.  * 0000 0000
  3099.  */ 
  3100. val = 0x00;
  3101. if (clkmode == 2 || clkmode == 3 || clkmode == 6
  3102.     || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
  3103. val |= BIT5;
  3104. if (clksubmode)
  3105. val |= BIT4;
  3106. if (info->params.crc_type == HDLC_CRC_32_CCITT)
  3107. val |= BIT1;
  3108. if (info->params.encoding == HDLC_ENCODING_NRZB)
  3109. val |= BIT0;
  3110. write_reg(info, CHA + CCR2, val);
  3111. /* CCR3
  3112.  *
  3113.  * 07..06  PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
  3114.  * 05      EPT Enable preamble transmission, 1=enabled
  3115.  * 04      RADD Receive address pushed to FIFO, 0=disabled
  3116.  * 03      CRL CRC Reset Level, 0=FFFF
  3117.  * 02      RCRC Rx CRC 0=On 1=Off
  3118.  * 01      TCRC Tx CRC 0=On 1=Off
  3119.  * 00      PSD DPLL Phase Shift Disable
  3120.  *
  3121.  * 0000 0000
  3122.  */ 
  3123. val = 0x00;
  3124. if (info->params.crc_type == HDLC_CRC_NONE)
  3125. val |= BIT2 + BIT1;
  3126. if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
  3127. val |= BIT5;
  3128. switch (info->params.preamble_length)
  3129. {
  3130. case HDLC_PREAMBLE_LENGTH_16BITS:
  3131. val |= BIT6;
  3132. break;
  3133. case HDLC_PREAMBLE_LENGTH_32BITS:
  3134. val |= BIT6;
  3135. break;
  3136. case HDLC_PREAMBLE_LENGTH_64BITS:
  3137. val |= BIT7 + BIT6;
  3138. break;
  3139. }
  3140. write_reg(info, CHA + CCR3, val);
  3141. /* PRE - Preamble pattern */ 
  3142. val = 0;
  3143. switch (info->params.preamble)
  3144. {
  3145. case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
  3146. case HDLC_PREAMBLE_PATTERN_10:    val = 0xaa; break;
  3147. case HDLC_PREAMBLE_PATTERN_01:    val = 0x55; break;
  3148. case HDLC_PREAMBLE_PATTERN_ONES:  val = 0xff; break;
  3149. }
  3150. write_reg(info, CHA + PRE, val);
  3151. /* CCR4
  3152.  *
  3153.  * 07      MCK4 Master Clock Divide by 4, 1=enabled
  3154.  * 06      EBRG Enhanced Baud Rate Generator Mode, 1=enabled
  3155.  * 05      TST1 Test Pin, 0=normal operation
  3156.  * 04      ICD Ivert Carrier Detect, 1=enabled (active low)
  3157.  * 03..02  Reserved, must be 0
  3158.  * 01..00  RFT[1..0] RxFIFO Threshold 00=32 bytes
  3159.  *
  3160.  * 0101 0000
  3161.  */ 
  3162. val = 0x50;
  3163. write_reg(info, CHA + CCR4, val);
  3164. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3165. mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
  3166. else
  3167. mgslpc_set_rate(info, CHA, info->params.clock_speed);
  3168. /* RLCR Receive length check register
  3169.  *
  3170.  * 7     1=enable receive length check
  3171.  * 6..0  Max frame length = (RL + 1) * 32
  3172.  */ 
  3173. write_reg(info, CHA + RLCR, 0);
  3174. /* XBCH Transmit Byte Count High
  3175.  *
  3176.  * 07      DMA mode, 0 = interrupt driven
  3177.  * 06      NRM, 0=ABM (ignored)
  3178.  * 05      CAS Carrier Auto Start
  3179.  * 04      XC Transmit Continuously (ignored)
  3180.  * 03..00  XBC[10..8] Transmit byte count bits 10..8
  3181.  *
  3182.  * 0000 0000
  3183.  */ 
  3184. val = 0x00;
  3185. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3186. val |= BIT5;
  3187. write_reg(info, CHA + XBCH, val);
  3188. enable_auxclk(info);
  3189. if (info->params.loopback || info->testing_irq)
  3190. loopback_enable(info);
  3191. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3192. {
  3193. irq_enable(info, CHB, IRQ_CTS);
  3194. /* PVR[3] 1=AUTO CTS active */ 
  3195. set_reg_bits(info, CHA + PVR, BIT3);
  3196. } else
  3197. clear_reg_bits(info, CHA + PVR, BIT3);
  3198. irq_enable(info, CHA,
  3199.  IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
  3200.  IRQ_UNDERRUN + IRQ_TXFIFO);
  3201. issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
  3202. wait_command_complete(info, CHA);
  3203. read_reg16(info, CHA + ISR); /* clear pending IRQs */
  3204. /* Master clock mode enabled above to allow reset commands
  3205.  * to complete even if no data clocks are present.
  3206.  *
  3207.  * Disable master clock mode for normal communications because
  3208.  * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
  3209.  * IRQ when in master clock mode.
  3210.  *
  3211.  * Leave master clock mode enabled for IRQ test because the
  3212.  * timer IRQ used by the test can only happen in master clock mode.
  3213.  */ 
  3214. if (!info->testing_irq)
  3215. clear_reg_bits(info, CHA + CCR0, BIT6);
  3216. tx_set_idle(info);
  3217. tx_stop(info);
  3218. rx_stop(info);
  3219. }
  3220. void rx_stop(MGSLPC_INFO *info)
  3221. {
  3222. if (debug_level >= DEBUG_LEVEL_ISR)
  3223. printk("%s(%d):rx_stop(%s)n",
  3224.  __FILE__,__LINE__, info->device_name );
  3225.  
  3226. /* MODE:03 RAC Receiver Active, 0=inactive */ 
  3227. clear_reg_bits(info, CHA + MODE, BIT3);
  3228. info->rx_enabled = 0;
  3229. info->rx_overflow = 0;
  3230. }
  3231. void rx_start(MGSLPC_INFO *info)
  3232. {
  3233. if (debug_level >= DEBUG_LEVEL_ISR)
  3234. printk("%s(%d):rx_start(%s)n",
  3235.  __FILE__,__LINE__, info->device_name );
  3236. rx_reset_buffers(info);
  3237. info->rx_enabled = 0;
  3238. info->rx_overflow = 0;
  3239. /* MODE:03 RAC Receiver Active, 1=active */ 
  3240. set_reg_bits(info, CHA + MODE, BIT3);
  3241. info->rx_enabled = 1;
  3242. }
  3243. void tx_start(MGSLPC_INFO *info)
  3244. {
  3245. if (debug_level >= DEBUG_LEVEL_ISR)
  3246. printk("%s(%d):tx_start(%s)n",
  3247.  __FILE__,__LINE__, info->device_name );
  3248.  
  3249. if (info->tx_count) {
  3250. /* If auto RTS enabled and RTS is inactive, then assert */
  3251. /* RTS and set a flag indicating that the driver should */
  3252. /* negate RTS when the transmission completes. */
  3253. info->drop_rts_on_tx_done = 0;
  3254. if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
  3255. get_signals(info);
  3256. if (!(info->serial_signals & SerialSignal_RTS)) {
  3257. info->serial_signals |= SerialSignal_RTS;
  3258. set_signals(info);
  3259. info->drop_rts_on_tx_done = 1;
  3260. }
  3261. }
  3262. if (info->params.mode == MGSL_MODE_ASYNC) {
  3263. if (!info->tx_active) {
  3264. info->tx_active = 1;
  3265. tx_ready(info);
  3266. }
  3267. } else {
  3268. info->tx_active = 1;
  3269. tx_ready(info);
  3270. info->tx_timer.expires = jiffies + jiffies_from_ms(5000);
  3271. add_timer(&info->tx_timer);
  3272. }
  3273. }
  3274. if (!info->tx_enabled)
  3275. info->tx_enabled = 1;
  3276. }
  3277. void tx_stop(MGSLPC_INFO *info)
  3278. {
  3279. if (debug_level >= DEBUG_LEVEL_ISR)
  3280. printk("%s(%d):tx_stop(%s)n",
  3281.  __FILE__,__LINE__, info->device_name );
  3282.  
  3283. del_timer(&info->tx_timer);
  3284. info->tx_enabled = 0;
  3285. info->tx_active  = 0;
  3286. }
  3287. /* Reset the adapter to a known state and prepare it for further use.
  3288.  */
  3289. void reset_device(MGSLPC_INFO *info)
  3290. {
  3291. /* power up both channels (set BIT7) */ 
  3292. write_reg(info, CHA + CCR0, 0x80);
  3293. write_reg(info, CHB + CCR0, 0x80);
  3294. write_reg(info, CHA + MODE, 0);
  3295. write_reg(info, CHB + MODE, 0);
  3296. /* disable all interrupts */ 
  3297. irq_disable(info, CHA, 0xffff);
  3298. irq_disable(info, CHB, 0xffff);
  3299. port_irq_disable(info, 0xff);
  3300. /* PCR Port Configuration Register
  3301.  *
  3302.  * 07..04  DEC[3..0] Serial I/F select outputs
  3303.  * 03      output, 1=AUTO CTS control enabled
  3304.  * 02      RI Ring Indicator input 0=active
  3305.  * 01      DSR input 0=active
  3306.  * 00      DTR output 0=active
  3307.  *
  3308.  * 0000 0110
  3309.  */ 
  3310. write_reg(info, PCR, 0x06);
  3311. /* PVR Port Value Register
  3312.  *
  3313.  * 07..04  DEC[3..0] Serial I/F select (0000=disabled)
  3314.  * 03      AUTO CTS output 1=enabled
  3315.  * 02      RI Ring Indicator input
  3316.  * 01      DSR input
  3317.  * 00      DTR output (1=inactive)
  3318.  *
  3319.  * 0000 0001
  3320.  */
  3321. // write_reg(info, PVR, PVR_DTR);
  3322. /* IPC Interrupt Port Configuration
  3323.  *
  3324.  * 07      VIS 1=Masked interrupts visible
  3325.  * 06..05  Reserved, 0
  3326.  * 04..03  SLA Slave address, 00 ignored
  3327.  * 02      CASM Cascading Mode, 1=daisy chain
  3328.  * 01..00  IC[1..0] Interrupt Config, 01=push-pull output, active low
  3329.  *
  3330.  * 0000 0101
  3331.  */ 
  3332. write_reg(info, IPC, 0x05);
  3333. }
  3334. void async_mode(MGSLPC_INFO *info)
  3335. {
  3336. unsigned char val;
  3337. /* disable all interrupts */ 
  3338. irq_disable(info, CHA, 0xffff);
  3339. irq_disable(info, CHB, 0xffff);
  3340. port_irq_disable(info, 0xff);
  3341. /* MODE
  3342.  *
  3343.  * 07      Reserved, 0
  3344.  * 06      FRTS RTS State, 0=active
  3345.  * 05      FCTS Flow Control on CTS
  3346.  * 04      FLON Flow Control Enable
  3347.  * 03      RAC Receiver Active, 0 = inactive
  3348.  * 02      RTS 0=Auto RTS, 1=manual RTS
  3349.  * 01      TRS Timer Resolution, 1=512
  3350.  * 00      TLP Test Loop, 0 = no loop
  3351.  *
  3352.  * 0000 0110
  3353.  */ 
  3354. val = 0x06;
  3355. if (info->params.loopback)
  3356. val |= BIT0;
  3357. /* preserve RTS state */ 
  3358. if (!(info->serial_signals & SerialSignal_RTS))
  3359. val |= BIT6;
  3360. write_reg(info, CHA + MODE, val);
  3361. /* CCR0
  3362.  *
  3363.  * 07      PU Power Up, 1=active, 0=power down
  3364.  * 06      MCE Master Clock Enable, 1=enabled
  3365.  * 05      Reserved, 0
  3366.  * 04..02  SC[2..0] Encoding, 000=NRZ
  3367.  * 01..00  SM[1..0] Serial Mode, 11=Async
  3368.  *
  3369.  * 1000 0011
  3370.  */ 
  3371. write_reg(info, CHA + CCR0, 0x83);
  3372. /* CCR1
  3373.  *
  3374.  * 07..05  Reserved, 0
  3375.  * 04      ODS Output Driver Select, 1=TxD is push-pull output
  3376.  * 03      BCR Bit Clock Rate, 1=16x
  3377.  * 02..00  CM[2..0] Clock Mode, 111=BRG
  3378.  *
  3379.  * 0001 1111
  3380.  */ 
  3381. write_reg(info, CHA + CCR1, 0x1f);
  3382. /* CCR2 (channel A)
  3383.  *
  3384.  * 07..06  BGR[9..8] Baud rate bits 9..8
  3385.  * 05      BDF Baud rate divisor factor, 0=1, 1=BGR value
  3386.  * 04      SSEL Clock source select, 1=submode b
  3387.  * 03      TOE 0=TxCLK is input, 0=TxCLK is input
  3388.  * 02      RWX Read/Write Exchange 0=disabled
  3389.  * 01      Reserved, 0
  3390.  * 00      DIV, data inversion 0=disabled, 1=enabled
  3391.  *
  3392.  * 0001 0000
  3393.  */ 
  3394. write_reg(info, CHA + CCR2, 0x10);
  3395. /* CCR3
  3396.  *
  3397.  * 07..01  Reserved, 0
  3398.  * 00      PSD DPLL Phase Shift Disable
  3399.  *
  3400.  * 0000 0000
  3401.  */ 
  3402. write_reg(info, CHA + CCR3, 0);
  3403. /* CCR4
  3404.  *
  3405.  * 07      MCK4 Master Clock Divide by 4, 1=enabled
  3406.  * 06      EBRG Enhanced Baud Rate Generator Mode, 1=enabled
  3407.  * 05      TST1 Test Pin, 0=normal operation
  3408.  * 04      ICD Ivert Carrier Detect, 1=enabled (active low)
  3409.  * 03..00  Reserved, must be 0
  3410.  *
  3411.  * 0101 0000
  3412.  */ 
  3413. write_reg(info, CHA + CCR4, 0x50);
  3414. mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
  3415. /* DAFO Data Format
  3416.  *
  3417.  * 07      Reserved, 0
  3418.  * 06      XBRK transmit break, 0=normal operation
  3419.  * 05      Stop bits (0=1, 1=2)
  3420.  * 04..03  PAR[1..0] Parity (01=odd, 10=even)
  3421.  * 02      PAREN Parity Enable
  3422.  * 01..00  CHL[1..0] Character Length (00=8, 01=7)
  3423.  *
  3424.  */ 
  3425. val = 0x00;
  3426. if (info->params.data_bits != 8)
  3427. val |= BIT0; /* 7 bits */
  3428. if (info->params.stop_bits != 1)
  3429. val |= BIT5;
  3430. if (info->params.parity != ASYNC_PARITY_NONE)
  3431. {
  3432. val |= BIT2; /* Parity enable */
  3433. if (info->params.parity == ASYNC_PARITY_ODD)
  3434. val |= BIT3;
  3435. else
  3436. val |= BIT4;
  3437. }
  3438. write_reg(info, CHA + DAFO, val);
  3439. /* RFC Rx FIFO Control
  3440.  *
  3441.  * 07      Reserved, 0
  3442.  * 06      DPS, 1=parity bit not stored in data byte
  3443.  * 05      DXS, 0=all data stored in FIFO (including XON/XOFF)
  3444.  * 04      RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
  3445.  * 03..02  RFTH[1..0], rx threshold, 11=16 status + 16 data byte
  3446.  * 01      Reserved, 0
  3447.  * 00      TCDE Terminate Char Detect Enable, 0=disabled
  3448.  *
  3449.  * 0101 1100
  3450.  */ 
  3451. write_reg(info, CHA + RFC, 0x5c);
  3452. /* RLCR Receive length check register
  3453.  *
  3454.  * Max frame length = (RL + 1) * 32
  3455.  */ 
  3456. write_reg(info, CHA + RLCR, 0);
  3457. /* XBCH Transmit Byte Count High
  3458.  *
  3459.  * 07      DMA mode, 0 = interrupt driven
  3460.  * 06      NRM, 0=ABM (ignored)
  3461.  * 05      CAS Carrier Auto Start
  3462.  * 04      XC Transmit Continuously (ignored)
  3463.  * 03..00  XBC[10..8] Transmit byte count bits 10..8
  3464.  *
  3465.  * 0000 0000
  3466.  */ 
  3467. val = 0x00;
  3468. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3469. val |= BIT5;
  3470. write_reg(info, CHA + XBCH, val);
  3471. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3472. irq_enable(info, CHA, IRQ_CTS);
  3473. /* MODE:03 RAC Receiver Active, 1=active */ 
  3474. set_reg_bits(info, CHA + MODE, BIT3);
  3475. enable_auxclk(info);
  3476. if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
  3477. irq_enable(info, CHB, IRQ_CTS);
  3478. /* PVR[3] 1=AUTO CTS active */ 
  3479. set_reg_bits(info, CHA + PVR, BIT3);
  3480. } else
  3481. clear_reg_bits(info, CHA + PVR, BIT3);
  3482. irq_enable(info, CHA,
  3483.   IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
  3484.   IRQ_ALLSENT + IRQ_TXFIFO);
  3485. issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
  3486. wait_command_complete(info, CHA);
  3487. read_reg16(info, CHA + ISR); /* clear pending IRQs */
  3488. }
  3489. /* Set the HDLC idle mode for the transmitter.
  3490.  */
  3491. void tx_set_idle(MGSLPC_INFO *info)
  3492. {
  3493. /* Note: ESCC2 only supports flags and one idle modes */ 
  3494. if (info->idle_mode == HDLC_TXIDLE_FLAGS)
  3495. set_reg_bits(info, CHA + CCR1, BIT3);
  3496. else
  3497. clear_reg_bits(info, CHA + CCR1, BIT3);
  3498. }
  3499. /* get state of the V24 status (input) signals.
  3500.  */
  3501. void get_signals(MGSLPC_INFO *info)
  3502. {
  3503. unsigned char status = 0;
  3504. /* preserve DTR and RTS */ 
  3505. info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
  3506. if (read_reg(info, CHB + VSTR) & BIT7)
  3507. info->serial_signals |= SerialSignal_DCD;
  3508. if (read_reg(info, CHB + STAR) & BIT1)
  3509. info->serial_signals |= SerialSignal_CTS;
  3510. status = read_reg(info, CHA + PVR);
  3511. if (!(status & PVR_RI))
  3512. info->serial_signals |= SerialSignal_RI;
  3513. if (!(status & PVR_DSR))
  3514. info->serial_signals |= SerialSignal_DSR;
  3515. }
  3516. /* Set the state of DTR and RTS based on contents of
  3517.  * serial_signals member of device extension.
  3518.  */
  3519. void set_signals(MGSLPC_INFO *info)
  3520. {
  3521. unsigned char val;
  3522. val = read_reg(info, CHA + MODE);
  3523. if (info->params.mode == MGSL_MODE_ASYNC) {
  3524. if (info->serial_signals & SerialSignal_RTS)
  3525. val &= ~BIT6;
  3526. else
  3527. val |= BIT6;
  3528. } else {
  3529. if (info->serial_signals & SerialSignal_RTS)
  3530. val |= BIT2;
  3531. else
  3532. val &= ~BIT2;
  3533. }
  3534. write_reg(info, CHA + MODE, val);
  3535. if (info->serial_signals & SerialSignal_DTR)
  3536. clear_reg_bits(info, CHA + PVR, PVR_DTR);
  3537. else
  3538. set_reg_bits(info, CHA + PVR, PVR_DTR);
  3539. }
  3540. void rx_reset_buffers(MGSLPC_INFO *info)
  3541. {
  3542. RXBUF *buf;
  3543. int i;
  3544. info->rx_put = 0;
  3545. info->rx_get = 0;
  3546. info->rx_frame_count = 0;
  3547. for (i=0 ; i < info->rx_buf_count ; i++) {
  3548. buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
  3549. buf->status = buf->count = 0;
  3550. }
  3551. }
  3552. /* Attempt to return a received HDLC frame
  3553.  * Only frames received without errors are returned.
  3554.  *
  3555.  * Returns 1 if frame returned, otherwise 0
  3556.  */
  3557. int rx_get_frame(MGSLPC_INFO *info)
  3558. {
  3559. unsigned short status;
  3560. RXBUF *buf;
  3561. unsigned int framesize = 0;
  3562. unsigned long flags;
  3563. struct tty_struct *tty = info->tty;
  3564. int return_frame = 0;
  3565. if (info->rx_frame_count == 0)
  3566. return 0;
  3567. buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
  3568. status = buf->status;
  3569. /* 07  VFR  1=valid frame
  3570.  * 06  RDO  1=data overrun
  3571.  * 05  CRC  1=OK, 0=error
  3572.  * 04  RAB  1=frame aborted
  3573.  */
  3574. if ((status & 0xf0) != 0xA0) {
  3575. if (!(status & BIT7) || (status & BIT4))
  3576. info->icount.rxabort++;
  3577. else if (status & BIT6)
  3578. info->icount.rxover++;
  3579. else if (!(status & BIT5)) {
  3580. info->icount.rxcrc++;
  3581. if (info->params.crc_type & HDLC_CRC_RETURN_EX)
  3582. return_frame = 1;
  3583. }
  3584. framesize = 0;
  3585. #ifdef CONFIG_SYNCLINK_SYNCPPP
  3586. info->netstats.rx_errors++;
  3587. info->netstats.rx_frame_errors++;
  3588. #endif
  3589. } else
  3590. return_frame = 1;
  3591. if (return_frame)
  3592. framesize = buf->count;
  3593. if (debug_level >= DEBUG_LEVEL_BH)
  3594. printk("%s(%d):rx_get_frame(%s) status=%04X size=%dn",
  3595. __FILE__,__LINE__,info->device_name,status,framesize);
  3596. if (debug_level >= DEBUG_LEVEL_DATA)
  3597. trace_block(info, buf->data, framesize, 0);
  3598. if (framesize) {
  3599. if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
  3600.       framesize+1 > info->max_frame_size) ||
  3601.     framesize > info->max_frame_size)
  3602. info->icount.rxlong++;
  3603. else {
  3604. if (status & BIT5)
  3605. info->icount.rxok++;
  3606. if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
  3607. *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
  3608. ++framesize;
  3609. }
  3610. #ifdef CONFIG_SYNCLINK_SYNCPPP
  3611. if (info->netcount) {
  3612. /* pass frame to syncppp device */
  3613. mgslpc_sppp_rx_done(info, buf->data, framesize);
  3614. else
  3615. #endif
  3616. {
  3617. /* Call the line discipline receive callback directly. */
  3618. if (tty && tty->ldisc.receive_buf)
  3619. tty->ldisc.receive_buf(tty, buf->data, info->flag_buf, framesize);
  3620. }
  3621. }
  3622. }
  3623. spin_lock_irqsave(&info->lock,flags);
  3624. buf->status = buf->count = 0;
  3625. info->rx_frame_count--;
  3626. info->rx_get++;
  3627. if (info->rx_get >= info->rx_buf_count)
  3628. info->rx_get = 0;
  3629. spin_unlock_irqrestore(&info->lock,flags);
  3630. return 1;
  3631. }
  3632. BOOLEAN register_test(MGSLPC_INFO *info)
  3633. {
  3634. static unsigned char patterns[] = 
  3635.     { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
  3636. static unsigned int count = sizeof(patterns) / sizeof(patterns[0]);
  3637. unsigned int i;
  3638. BOOLEAN rc = TRUE;
  3639. unsigned long flags;
  3640. spin_lock_irqsave(&info->lock,flags);
  3641. reset_device(info);
  3642. for (i = 0; i < count; i++) {
  3643. write_reg(info, XAD1, patterns[i]);
  3644. write_reg(info, XAD2, patterns[(i + 1) % count]);
  3645. if ((read_reg(info, XAD1) != patterns[i]) || 
  3646.     (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
  3647. rc = FALSE;
  3648. break;
  3649. }
  3650. }
  3651. spin_unlock_irqrestore(&info->lock,flags);
  3652. return rc;
  3653. }
  3654. BOOLEAN irq_test(MGSLPC_INFO *info)
  3655. {
  3656. unsigned long end_time;
  3657. unsigned long flags;
  3658. spin_lock_irqsave(&info->lock,flags);
  3659. reset_device(info);
  3660. info->testing_irq = TRUE;
  3661. hdlc_mode(info);
  3662. info->irq_occurred = FALSE;
  3663. /* init hdlc mode */
  3664. irq_enable(info, CHA, IRQ_TIMER);
  3665. write_reg(info, CHA + TIMR, 0); /* 512 cycles */
  3666. issue_command(info, CHA, CMD_START_TIMER);
  3667. spin_unlock_irqrestore(&info->lock,flags);
  3668. end_time=100;
  3669. while(end_time-- && !info->irq_occurred) {
  3670. set_current_state(TASK_INTERRUPTIBLE);
  3671. schedule_timeout(jiffies_from_ms(10));
  3672. }
  3673. info->testing_irq = FALSE;
  3674. spin_lock_irqsave(&info->lock,flags);
  3675. reset_device(info);
  3676. spin_unlock_irqrestore(&info->lock,flags);
  3677. return info->irq_occurred ? TRUE : FALSE;
  3678. }
  3679. int adapter_test(MGSLPC_INFO *info)
  3680. {
  3681. if (!register_test(info)) {
  3682. info->init_error = DiagStatus_AddressFailure;
  3683. printk( "%s(%d):Register test failure for device %s Addr=%04Xn",
  3684. __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
  3685. return -ENODEV;
  3686. }
  3687. if (!irq_test(info)) {
  3688. info->init_error = DiagStatus_IrqFailure;
  3689. printk( "%s(%d):Interrupt test failure for device %s IRQ=%dn",
  3690. __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
  3691. return -ENODEV;
  3692. }
  3693. if (debug_level >= DEBUG_LEVEL_INFO)
  3694. printk("%s(%d):device %s passed diagnosticsn",
  3695. __FILE__,__LINE__,info->device_name);
  3696. return 0;
  3697. }
  3698. void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
  3699. {
  3700. int i;
  3701. int linecount;
  3702. if (xmit)
  3703. printk("%s tx data:n",info->device_name);
  3704. else
  3705. printk("%s rx data:n",info->device_name);
  3706. while(count) {
  3707. if (count > 16)
  3708. linecount = 16;
  3709. else
  3710. linecount = count;
  3711. for(i=0;i<linecount;i++)
  3712. printk("%02X ",(unsigned char)data[i]);
  3713. for(;i<17;i++)
  3714. printk("   ");
  3715. for(i=0;i<linecount;i++) {
  3716. if (data[i]>=040 && data[i]<=0176)
  3717. printk("%c",data[i]);
  3718. else
  3719. printk(".");
  3720. }
  3721. printk("n");
  3722. data  += linecount;
  3723. count -= linecount;
  3724. }
  3725. }
  3726. /* HDLC frame time out
  3727.  * update stats and do tx completion processing
  3728.  */
  3729. void tx_timeout(unsigned long context)
  3730. {
  3731. MGSLPC_INFO *info = (MGSLPC_INFO*)context;
  3732. unsigned long flags;
  3733. if ( debug_level >= DEBUG_LEVEL_INFO )
  3734. printk( "%s(%d):tx_timeout(%s)n",
  3735. __FILE__,__LINE__,info->device_name);
  3736. if(info->tx_active &&
  3737.    info->params.mode == MGSL_MODE_HDLC) {
  3738. info->icount.txtimeout++;
  3739. }
  3740. spin_lock_irqsave(&info->lock,flags);
  3741. info->tx_active = 0;
  3742. info->tx_count = info->tx_put = info->tx_get = 0;
  3743. spin_unlock_irqrestore(&info->lock,flags);
  3744. #ifdef CONFIG_SYNCLINK_SYNCPPP
  3745. if (info->netcount)
  3746. mgslpc_sppp_tx_done(info);
  3747. else
  3748. #endif
  3749. bh_transmit(info);
  3750. }
  3751. #ifdef CONFIG_SYNCLINK_SYNCPPP
  3752. /* syncppp net device routines
  3753.  */
  3754. void mgslpc_sppp_init(MGSLPC_INFO *info)
  3755. {
  3756. struct net_device *d;
  3757. sprintf(info->netname,"mgslp%d",info->line);
  3758. info->if_ptr = &info->pppdev;
  3759. info->netdev = info->pppdev.dev = &info->netdevice;
  3760. sppp_attach(&info->pppdev);
  3761. d = info->netdev;
  3762. strcpy(d->name,info->netname);
  3763. d->base_addr = info->io_base;
  3764. d->irq = info->irq_level;
  3765. d->priv = info;
  3766. d->init = NULL;
  3767. d->open = mgslpc_sppp_open;
  3768. d->stop = mgslpc_sppp_close;
  3769. d->hard_start_xmit = mgslpc_sppp_tx;
  3770. d->do_ioctl = mgslpc_sppp_ioctl;
  3771. d->get_stats = mgslpc_net_stats;
  3772. d->tx_timeout = mgslpc_sppp_tx_timeout;
  3773. d->watchdog_timeo = 10*HZ;
  3774. #if LINUX_VERSION_CODE < VERSION(2,4,4) 
  3775. dev_init_buffers(d);
  3776. #endif
  3777. if (register_netdev(d) == -1) {
  3778. printk(KERN_WARNING "%s: register_netdev failed.n", d->name);
  3779. sppp_detach(info->netdev);
  3780. return;
  3781. }
  3782. if (debug_level >= DEBUG_LEVEL_INFO)
  3783. printk("mgslpc_sppp_init()n");
  3784. }
  3785. void mgslpc_sppp_delete(MGSLPC_INFO *info)
  3786. {
  3787. if (debug_level >= DEBUG_LEVEL_INFO)
  3788. printk("mgslpc_sppp_delete(%s)n",info->netname);
  3789. sppp_detach(info->netdev);
  3790. unregister_netdev(info->netdev);
  3791. }
  3792. int mgslpc_sppp_open(struct net_device *d)
  3793. {
  3794. MGSLPC_INFO *info = d->priv;
  3795. int err, flags;
  3796. if (debug_level >= DEBUG_LEVEL_INFO)
  3797. printk("mgslpc_sppp_open(%s)n",info->netname);
  3798. spin_lock_irqsave(&info->netlock, flags);
  3799. if (info->count != 0 || info->netcount != 0) {
  3800. printk(KERN_WARNING "%s: sppp_open returning busyn", info->netname);
  3801. spin_unlock_irqrestore(&info->netlock, flags);
  3802. return -EBUSY;
  3803. }
  3804. info->netcount=1;
  3805. MOD_INC_USE_COUNT;
  3806. spin_unlock_irqrestore(&info->netlock, flags);
  3807. /* claim resources and init adapter */
  3808. if ((err = startup(info)) != 0)
  3809. goto open_fail;
  3810. /* allow syncppp module to do open processing */
  3811. if ((err = sppp_open(d)) != 0) {
  3812. shutdown(info);
  3813. goto open_fail;
  3814. }
  3815. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  3816. mgslpc_program_hw(info);
  3817. d->trans_start = jiffies;
  3818. netif_start_queue(d);
  3819. return 0;
  3820. open_fail:
  3821. spin_lock_irqsave(&info->netlock, flags);
  3822. info->netcount=0;
  3823. MOD_DEC_USE_COUNT;
  3824. spin_unlock_irqrestore(&info->netlock, flags);
  3825. return err;
  3826. }
  3827. void mgslpc_sppp_tx_timeout(struct net_device *dev)
  3828. {
  3829. MGSLPC_INFO *info = dev->priv;
  3830. int flags;
  3831. if (debug_level >= DEBUG_LEVEL_INFO)
  3832. printk("mgslpc_sppp_tx_timeout(%s)n",info->netname);
  3833. info->netstats.tx_errors++;
  3834. info->netstats.tx_aborted_errors++;
  3835. spin_lock_irqsave(&info->lock,flags);
  3836. tx_stop(info);
  3837. spin_unlock_irqrestore(&info->lock,flags);
  3838. netif_wake_queue(dev);
  3839. }
  3840. int mgslpc_sppp_tx(struct sk_buff *skb, struct net_device *dev)
  3841. {
  3842. MGSLPC_INFO *info = dev->priv;
  3843. unsigned long flags;
  3844. if (debug_level >= DEBUG_LEVEL_INFO)
  3845. printk("mgslpc_sppp_tx(%s)n",info->netname);
  3846. netif_stop_queue(dev);
  3847. info->tx_count = skb->len;
  3848. memcpy(info->tx_buf, skb->data, skb->len);
  3849. info->tx_get = 0;
  3850. info->tx_put = info->tx_count = skb->len;
  3851. info->netstats.tx_packets++;
  3852. info->netstats.tx_bytes += skb->len;
  3853. dev_kfree_skb(skb);
  3854. dev->trans_start = jiffies;
  3855. spin_lock_irqsave(&info->lock,flags);
  3856. if (!info->tx_active)
  3857.   tx_start(info);
  3858. spin_unlock_irqrestore(&info->lock,flags);
  3859. return 0;
  3860. }
  3861. int mgslpc_sppp_close(struct net_device *d)
  3862. {
  3863. MGSLPC_INFO *info = d->priv;
  3864. unsigned long flags;
  3865. if (debug_level >= DEBUG_LEVEL_INFO)
  3866. printk("mgslpc_sppp_close(%s)n",info->netname);
  3867. /* shutdown adapter and release resources */
  3868. shutdown(info);
  3869. /* allow syncppp to do close processing */
  3870. sppp_close(d);
  3871. netif_stop_queue(d);
  3872. spin_lock_irqsave(&info->netlock, flags);
  3873. info->netcount=0;
  3874. MOD_DEC_USE_COUNT;
  3875. spin_unlock_irqrestore(&info->netlock, flags);
  3876. return 0;
  3877. }
  3878. void mgslpc_sppp_rx_done(MGSLPC_INFO *info, char *buf, int size)
  3879. {
  3880. struct sk_buff *skb = dev_alloc_skb(size);
  3881. if (debug_level >= DEBUG_LEVEL_INFO)
  3882. printk("mgslpc_sppp_rx_done(%s)n",info->netname);
  3883. if (skb == NULL) {
  3884. printk(KERN_NOTICE "%s: cant alloc skb, dropping packetn",
  3885. info->netname);
  3886. info->netstats.rx_dropped++;
  3887. return;
  3888. }
  3889. memcpy(skb_put(skb, size),buf,size);
  3890. skb->protocol = htons(ETH_P_WAN_PPP);
  3891. skb->dev = info->netdev;
  3892. skb->mac.raw = skb->data;
  3893. info->netstats.rx_packets++;
  3894. info->netstats.rx_bytes += size;
  3895. netif_rx(skb);
  3896. info->netdev->trans_start = jiffies;
  3897. }
  3898. void mgslpc_sppp_tx_done(MGSLPC_INFO *info)
  3899. {
  3900. if (netif_queue_stopped(info->netdev))
  3901.     netif_wake_queue(info->netdev);
  3902. }
  3903. struct net_device_stats *mgslpc_net_stats(struct net_device *dev)
  3904. {
  3905. MGSLPC_INFO *info = dev->priv;
  3906. if (debug_level >= DEBUG_LEVEL_INFO)
  3907. printk("mgslpc_net_stats(%s)n",info->netname);
  3908. return &info->netstats;
  3909. }
  3910. int mgslpc_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  3911. {
  3912. MGSLPC_INFO *info = (MGSLPC_INFO *)dev->priv;
  3913. if (debug_level >= DEBUG_LEVEL_INFO)
  3914. printk("%s(%d):mgslpc_ioctl %s cmd=%08Xn", __FILE__,__LINE__,
  3915. info->netname, cmd );
  3916. return sppp_do_ioctl(dev, ifr, cmd);
  3917. }
  3918. #endif /* ifdef CONFIG_SYNCLINK_SYNCPPP */