/drivers/char/pcmcia/synclink_cs.c

https://bitbucket.org/wisechild/galaxy-nexus · C · 4311 lines · 2938 code · 672 blank · 701 comment · 630 complexity · 9990b381aff71ea36e28a41ddda3c1f6 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * linux/drivers/char/pcmcia/synclink_cs.c
  3. *
  4. * $Id: synclink_cs.c,v 4.34 2005/09/08 13:20:54 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/module.h>
  36. #include <linux/errno.h>
  37. #include <linux/signal.h>
  38. #include <linux/sched.h>
  39. #include <linux/timer.h>
  40. #include <linux/time.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/tty.h>
  43. #include <linux/tty_flip.h>
  44. #include <linux/serial.h>
  45. #include <linux/major.h>
  46. #include <linux/string.h>
  47. #include <linux/fcntl.h>
  48. #include <linux/ptrace.h>
  49. #include <linux/ioport.h>
  50. #include <linux/mm.h>
  51. #include <linux/seq_file.h>
  52. #include <linux/slab.h>
  53. #include <linux/netdevice.h>
  54. #include <linux/vmalloc.h>
  55. #include <linux/init.h>
  56. #include <linux/delay.h>
  57. #include <linux/ioctl.h>
  58. #include <linux/synclink.h>
  59. #include <asm/system.h>
  60. #include <asm/io.h>
  61. #include <asm/irq.h>
  62. #include <asm/dma.h>
  63. #include <linux/bitops.h>
  64. #include <asm/types.h>
  65. #include <linux/termios.h>
  66. #include <linux/workqueue.h>
  67. #include <linux/hdlc.h>
  68. #include <pcmcia/cistpl.h>
  69. #include <pcmcia/cisreg.h>
  70. #include <pcmcia/ds.h>
  71. #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_CS_MODULE))
  72. #define SYNCLINK_GENERIC_HDLC 1
  73. #else
  74. #define SYNCLINK_GENERIC_HDLC 0
  75. #endif
  76. #define GET_USER(error,value,addr) error = get_user(value,addr)
  77. #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
  78. #define PUT_USER(error,value,addr) error = put_user(value,addr)
  79. #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
  80. #include <asm/uaccess.h>
  81. static MGSL_PARAMS default_params = {
  82. MGSL_MODE_HDLC, /* unsigned long mode */
  83. 0, /* unsigned char loopback; */
  84. HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
  85. HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
  86. 0, /* unsigned long clock_speed; */
  87. 0xff, /* unsigned char addr_filter; */
  88. HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
  89. HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
  90. HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
  91. 9600, /* unsigned long data_rate; */
  92. 8, /* unsigned char data_bits; */
  93. 1, /* unsigned char stop_bits; */
  94. ASYNC_PARITY_NONE /* unsigned char parity; */
  95. };
  96. typedef struct
  97. {
  98. int count;
  99. unsigned char status;
  100. char data[1];
  101. } RXBUF;
  102. /* The queue of BH actions to be performed */
  103. #define BH_RECEIVE 1
  104. #define BH_TRANSMIT 2
  105. #define BH_STATUS 4
  106. #define IO_PIN_SHUTDOWN_LIMIT 100
  107. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  108. struct _input_signal_events {
  109. int ri_up;
  110. int ri_down;
  111. int dsr_up;
  112. int dsr_down;
  113. int dcd_up;
  114. int dcd_down;
  115. int cts_up;
  116. int cts_down;
  117. };
  118. /*
  119. * Device instance data structure
  120. */
  121. typedef struct _mgslpc_info {
  122. struct tty_port port;
  123. void *if_ptr; /* General purpose pointer (used by SPPP) */
  124. int magic;
  125. int line;
  126. struct mgsl_icount icount;
  127. int timeout;
  128. int x_char; /* xon/xoff character */
  129. unsigned char read_status_mask;
  130. unsigned char ignore_status_mask;
  131. unsigned char *tx_buf;
  132. int tx_put;
  133. int tx_get;
  134. int tx_count;
  135. /* circular list of fixed length rx buffers */
  136. unsigned char *rx_buf; /* memory allocated for all rx buffers */
  137. int rx_buf_total_size; /* size of memory allocated for rx buffers */
  138. int rx_put; /* index of next empty rx buffer */
  139. int rx_get; /* index of next full rx buffer */
  140. int rx_buf_size; /* size in bytes of single rx buffer */
  141. int rx_buf_count; /* total number of rx buffers */
  142. int rx_frame_count; /* number of full rx buffers */
  143. wait_queue_head_t status_event_wait_q;
  144. wait_queue_head_t event_wait_q;
  145. struct timer_list tx_timer; /* HDLC transmit timeout timer */
  146. struct _mgslpc_info *next_device; /* device list link */
  147. unsigned short imra_value;
  148. unsigned short imrb_value;
  149. unsigned char pim_value;
  150. spinlock_t lock;
  151. struct work_struct task; /* task structure for scheduling bh */
  152. u32 max_frame_size;
  153. u32 pending_bh;
  154. bool bh_running;
  155. bool bh_requested;
  156. int dcd_chkcount; /* check counts to prevent */
  157. int cts_chkcount; /* too many IRQs if a signal */
  158. int dsr_chkcount; /* is floating */
  159. int ri_chkcount;
  160. bool rx_enabled;
  161. bool rx_overflow;
  162. bool tx_enabled;
  163. bool tx_active;
  164. bool tx_aborting;
  165. u32 idle_mode;
  166. int if_mode; /* serial interface selection (RS-232, v.35 etc) */
  167. char device_name[25]; /* device instance name */
  168. unsigned int io_base; /* base I/O address of adapter */
  169. unsigned int irq_level;
  170. MGSL_PARAMS params; /* communications parameters */
  171. unsigned char serial_signals; /* current serial signal states */
  172. bool irq_occurred; /* for diagnostics use */
  173. char testing_irq;
  174. unsigned int init_error; /* startup error (DIAGS) */
  175. char flag_buf[MAX_ASYNC_BUFFER_SIZE];
  176. bool drop_rts_on_tx_done;
  177. struct _input_signal_events input_signal_events;
  178. /* PCMCIA support */
  179. struct pcmcia_device *p_dev;
  180. int stop;
  181. /* SPPP/Cisco HDLC device parts */
  182. int netcount;
  183. spinlock_t netlock;
  184. #if SYNCLINK_GENERIC_HDLC
  185. struct net_device *netdev;
  186. #endif
  187. } MGSLPC_INFO;
  188. #define MGSLPC_MAGIC 0x5402
  189. /*
  190. * The size of the serial xmit buffer is 1 page, or 4096 bytes
  191. */
  192. #define TXBUFSIZE 4096
  193. #define CHA 0x00 /* channel A offset */
  194. #define CHB 0x40 /* channel B offset */
  195. /*
  196. * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
  197. */
  198. #undef PVR
  199. #define RXFIFO 0
  200. #define TXFIFO 0
  201. #define STAR 0x20
  202. #define CMDR 0x20
  203. #define RSTA 0x21
  204. #define PRE 0x21
  205. #define MODE 0x22
  206. #define TIMR 0x23
  207. #define XAD1 0x24
  208. #define XAD2 0x25
  209. #define RAH1 0x26
  210. #define RAH2 0x27
  211. #define DAFO 0x27
  212. #define RAL1 0x28
  213. #define RFC 0x28
  214. #define RHCR 0x29
  215. #define RAL2 0x29
  216. #define RBCL 0x2a
  217. #define XBCL 0x2a
  218. #define RBCH 0x2b
  219. #define XBCH 0x2b
  220. #define CCR0 0x2c
  221. #define CCR1 0x2d
  222. #define CCR2 0x2e
  223. #define CCR3 0x2f
  224. #define VSTR 0x34
  225. #define BGR 0x34
  226. #define RLCR 0x35
  227. #define AML 0x36
  228. #define AMH 0x37
  229. #define GIS 0x38
  230. #define IVA 0x38
  231. #define IPC 0x39
  232. #define ISR 0x3a
  233. #define IMR 0x3a
  234. #define PVR 0x3c
  235. #define PIS 0x3d
  236. #define PIM 0x3d
  237. #define PCR 0x3e
  238. #define CCR4 0x3f
  239. // IMR/ISR
  240. #define IRQ_BREAK_ON BIT15 // rx break detected
  241. #define IRQ_DATAOVERRUN BIT14 // receive data overflow
  242. #define IRQ_ALLSENT BIT13 // all sent
  243. #define IRQ_UNDERRUN BIT12 // transmit data underrun
  244. #define IRQ_TIMER BIT11 // timer interrupt
  245. #define IRQ_CTS BIT10 // CTS status change
  246. #define IRQ_TXREPEAT BIT9 // tx message repeat
  247. #define IRQ_TXFIFO BIT8 // transmit pool ready
  248. #define IRQ_RXEOM BIT7 // receive message end
  249. #define IRQ_EXITHUNT BIT6 // receive frame start
  250. #define IRQ_RXTIME BIT6 // rx char timeout
  251. #define IRQ_DCD BIT2 // carrier detect status change
  252. #define IRQ_OVERRUN BIT1 // receive frame overflow
  253. #define IRQ_RXFIFO BIT0 // receive pool full
  254. // STAR
  255. #define XFW BIT6 // transmit FIFO write enable
  256. #define CEC BIT2 // command executing
  257. #define CTS BIT1 // CTS state
  258. #define PVR_DTR BIT0
  259. #define PVR_DSR BIT1
  260. #define PVR_RI BIT2
  261. #define PVR_AUTOCTS BIT3
  262. #define PVR_RS232 0x20 /* 0010b */
  263. #define PVR_V35 0xe0 /* 1110b */
  264. #define PVR_RS422 0x40 /* 0100b */
  265. /* Register access functions */
  266. #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
  267. #define read_reg(info, reg) inb((info)->io_base + (reg))
  268. #define read_reg16(info, reg) inw((info)->io_base + (reg))
  269. #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
  270. #define set_reg_bits(info, reg, mask) \
  271. write_reg(info, (reg), \
  272. (unsigned char) (read_reg(info, (reg)) | (mask)))
  273. #define clear_reg_bits(info, reg, mask) \
  274. write_reg(info, (reg), \
  275. (unsigned char) (read_reg(info, (reg)) & ~(mask)))
  276. /*
  277. * interrupt enable/disable routines
  278. */
  279. static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
  280. {
  281. if (channel == CHA) {
  282. info->imra_value |= mask;
  283. write_reg16(info, CHA + IMR, info->imra_value);
  284. } else {
  285. info->imrb_value |= mask;
  286. write_reg16(info, CHB + IMR, info->imrb_value);
  287. }
  288. }
  289. static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
  290. {
  291. if (channel == CHA) {
  292. info->imra_value &= ~mask;
  293. write_reg16(info, CHA + IMR, info->imra_value);
  294. } else {
  295. info->imrb_value &= ~mask;
  296. write_reg16(info, CHB + IMR, info->imrb_value);
  297. }
  298. }
  299. #define port_irq_disable(info, mask) \
  300. { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
  301. #define port_irq_enable(info, mask) \
  302. { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
  303. static void rx_start(MGSLPC_INFO *info);
  304. static void rx_stop(MGSLPC_INFO *info);
  305. static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty);
  306. static void tx_stop(MGSLPC_INFO *info);
  307. static void tx_set_idle(MGSLPC_INFO *info);
  308. static void get_signals(MGSLPC_INFO *info);
  309. static void set_signals(MGSLPC_INFO *info);
  310. static void reset_device(MGSLPC_INFO *info);
  311. static void hdlc_mode(MGSLPC_INFO *info);
  312. static void async_mode(MGSLPC_INFO *info);
  313. static void tx_timeout(unsigned long context);
  314. static int carrier_raised(struct tty_port *port);
  315. static void dtr_rts(struct tty_port *port, int onoff);
  316. #if SYNCLINK_GENERIC_HDLC
  317. #define dev_to_port(D) (dev_to_hdlc(D)->priv)
  318. static void hdlcdev_tx_done(MGSLPC_INFO *info);
  319. static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
  320. static int hdlcdev_init(MGSLPC_INFO *info);
  321. static void hdlcdev_exit(MGSLPC_INFO *info);
  322. #endif
  323. static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
  324. static bool register_test(MGSLPC_INFO *info);
  325. static bool irq_test(MGSLPC_INFO *info);
  326. static int adapter_test(MGSLPC_INFO *info);
  327. static int claim_resources(MGSLPC_INFO *info);
  328. static void release_resources(MGSLPC_INFO *info);
  329. static void mgslpc_add_device(MGSLPC_INFO *info);
  330. static void mgslpc_remove_device(MGSLPC_INFO *info);
  331. static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty);
  332. static void rx_reset_buffers(MGSLPC_INFO *info);
  333. static int rx_alloc_buffers(MGSLPC_INFO *info);
  334. static void rx_free_buffers(MGSLPC_INFO *info);
  335. static irqreturn_t mgslpc_isr(int irq, void *dev_id);
  336. /*
  337. * Bottom half interrupt handlers
  338. */
  339. static void bh_handler(struct work_struct *work);
  340. static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty);
  341. static void bh_status(MGSLPC_INFO *info);
  342. /*
  343. * ioctl handlers
  344. */
  345. static int tiocmget(struct tty_struct *tty);
  346. static int tiocmset(struct tty_struct *tty,
  347. unsigned int set, unsigned int clear);
  348. static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
  349. static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
  350. static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params, struct tty_struct *tty);
  351. static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
  352. static int set_txidle(MGSLPC_INFO *info, int idle_mode);
  353. static int set_txenable(MGSLPC_INFO *info, int enable, struct tty_struct *tty);
  354. static int tx_abort(MGSLPC_INFO *info);
  355. static int set_rxenable(MGSLPC_INFO *info, int enable);
  356. static int wait_events(MGSLPC_INFO *info, int __user *mask);
  357. static MGSLPC_INFO *mgslpc_device_list = NULL;
  358. static int mgslpc_device_count = 0;
  359. /*
  360. * Set this param to non-zero to load eax with the
  361. * .text section address and breakpoint on module load.
  362. * This is useful for use with gdb and add-symbol-file command.
  363. */
  364. static int break_on_load=0;
  365. /*
  366. * Driver major number, defaults to zero to get auto
  367. * assigned major number. May be forced as module parameter.
  368. */
  369. static int ttymajor=0;
  370. static int debug_level = 0;
  371. static int maxframe[MAX_DEVICE_COUNT] = {0,};
  372. module_param(break_on_load, bool, 0);
  373. module_param(ttymajor, int, 0);
  374. module_param(debug_level, int, 0);
  375. module_param_array(maxframe, int, NULL, 0);
  376. MODULE_LICENSE("GPL");
  377. static char *driver_name = "SyncLink PC Card driver";
  378. static char *driver_version = "$Revision: 4.34 $";
  379. static struct tty_driver *serial_driver;
  380. /* number of characters left in xmit buffer before we ask for more */
  381. #define WAKEUP_CHARS 256
  382. static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty);
  383. static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
  384. /* PCMCIA prototypes */
  385. static int mgslpc_config(struct pcmcia_device *link);
  386. static void mgslpc_release(u_long arg);
  387. static void mgslpc_detach(struct pcmcia_device *p_dev);
  388. /*
  389. * 1st function defined in .text section. Calling this function in
  390. * init_module() followed by a breakpoint allows a remote debugger
  391. * (gdb) to get the .text address for the add-symbol-file command.
  392. * This allows remote debugging of dynamically loadable modules.
  393. */
  394. static void* mgslpc_get_text_ptr(void)
  395. {
  396. return mgslpc_get_text_ptr;
  397. }
  398. /**
  399. * line discipline callback wrappers
  400. *
  401. * The wrappers maintain line discipline references
  402. * while calling into the line discipline.
  403. *
  404. * ldisc_receive_buf - pass receive data to line discipline
  405. */
  406. static void ldisc_receive_buf(struct tty_struct *tty,
  407. const __u8 *data, char *flags, int count)
  408. {
  409. struct tty_ldisc *ld;
  410. if (!tty)
  411. return;
  412. ld = tty_ldisc_ref(tty);
  413. if (ld) {
  414. if (ld->ops->receive_buf)
  415. ld->ops->receive_buf(tty, data, flags, count);
  416. tty_ldisc_deref(ld);
  417. }
  418. }
  419. static const struct tty_port_operations mgslpc_port_ops = {
  420. .carrier_raised = carrier_raised,
  421. .dtr_rts = dtr_rts
  422. };
  423. static int mgslpc_probe(struct pcmcia_device *link)
  424. {
  425. MGSLPC_INFO *info;
  426. int ret;
  427. if (debug_level >= DEBUG_LEVEL_INFO)
  428. printk("mgslpc_attach\n");
  429. info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
  430. if (!info) {
  431. printk("Error can't allocate device instance data\n");
  432. return -ENOMEM;
  433. }
  434. info->magic = MGSLPC_MAGIC;
  435. tty_port_init(&info->port);
  436. info->port.ops = &mgslpc_port_ops;
  437. INIT_WORK(&info->task, bh_handler);
  438. info->max_frame_size = 4096;
  439. info->port.close_delay = 5*HZ/10;
  440. info->port.closing_wait = 30*HZ;
  441. init_waitqueue_head(&info->status_event_wait_q);
  442. init_waitqueue_head(&info->event_wait_q);
  443. spin_lock_init(&info->lock);
  444. spin_lock_init(&info->netlock);
  445. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  446. info->idle_mode = HDLC_TXIDLE_FLAGS;
  447. info->imra_value = 0xffff;
  448. info->imrb_value = 0xffff;
  449. info->pim_value = 0xff;
  450. info->p_dev = link;
  451. link->priv = info;
  452. /* Initialize the struct pcmcia_device structure */
  453. ret = mgslpc_config(link);
  454. if (ret)
  455. return ret;
  456. mgslpc_add_device(info);
  457. return 0;
  458. }
  459. /* Card has been inserted.
  460. */
  461. static int mgslpc_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
  462. {
  463. return pcmcia_request_io(p_dev);
  464. }
  465. static int mgslpc_config(struct pcmcia_device *link)
  466. {
  467. MGSLPC_INFO *info = link->priv;
  468. int ret;
  469. if (debug_level >= DEBUG_LEVEL_INFO)
  470. printk("mgslpc_config(0x%p)\n", link);
  471. link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
  472. ret = pcmcia_loop_config(link, mgslpc_ioprobe, NULL);
  473. if (ret != 0)
  474. goto failed;
  475. link->config_index = 8;
  476. link->config_regs = PRESENT_OPTION;
  477. ret = pcmcia_request_irq(link, mgslpc_isr);
  478. if (ret)
  479. goto failed;
  480. ret = pcmcia_enable_device(link);
  481. if (ret)
  482. goto failed;
  483. info->io_base = link->resource[0]->start;
  484. info->irq_level = link->irq;
  485. return 0;
  486. failed:
  487. mgslpc_release((u_long)link);
  488. return -ENODEV;
  489. }
  490. /* Card has been removed.
  491. * Unregister device and release PCMCIA configuration.
  492. * If device is open, postpone until it is closed.
  493. */
  494. static void mgslpc_release(u_long arg)
  495. {
  496. struct pcmcia_device *link = (struct pcmcia_device *)arg;
  497. if (debug_level >= DEBUG_LEVEL_INFO)
  498. printk("mgslpc_release(0x%p)\n", link);
  499. pcmcia_disable_device(link);
  500. }
  501. static void mgslpc_detach(struct pcmcia_device *link)
  502. {
  503. if (debug_level >= DEBUG_LEVEL_INFO)
  504. printk("mgslpc_detach(0x%p)\n", link);
  505. ((MGSLPC_INFO *)link->priv)->stop = 1;
  506. mgslpc_release((u_long)link);
  507. mgslpc_remove_device((MGSLPC_INFO *)link->priv);
  508. }
  509. static int mgslpc_suspend(struct pcmcia_device *link)
  510. {
  511. MGSLPC_INFO *info = link->priv;
  512. info->stop = 1;
  513. return 0;
  514. }
  515. static int mgslpc_resume(struct pcmcia_device *link)
  516. {
  517. MGSLPC_INFO *info = link->priv;
  518. info->stop = 0;
  519. return 0;
  520. }
  521. static inline bool mgslpc_paranoia_check(MGSLPC_INFO *info,
  522. char *name, const char *routine)
  523. {
  524. #ifdef MGSLPC_PARANOIA_CHECK
  525. static const char *badmagic =
  526. "Warning: bad magic number for mgsl struct (%s) in %s\n";
  527. static const char *badinfo =
  528. "Warning: null mgslpc_info for (%s) in %s\n";
  529. if (!info) {
  530. printk(badinfo, name, routine);
  531. return true;
  532. }
  533. if (info->magic != MGSLPC_MAGIC) {
  534. printk(badmagic, name, routine);
  535. return true;
  536. }
  537. #else
  538. if (!info)
  539. return true;
  540. #endif
  541. return false;
  542. }
  543. #define CMD_RXFIFO BIT7 // release current rx FIFO
  544. #define CMD_RXRESET BIT6 // receiver reset
  545. #define CMD_RXFIFO_READ BIT5
  546. #define CMD_START_TIMER BIT4
  547. #define CMD_TXFIFO BIT3 // release current tx FIFO
  548. #define CMD_TXEOM BIT1 // transmit end message
  549. #define CMD_TXRESET BIT0 // transmit reset
  550. static bool wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
  551. {
  552. int i = 0;
  553. /* wait for command completion */
  554. while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
  555. udelay(1);
  556. if (i++ == 1000)
  557. return false;
  558. }
  559. return true;
  560. }
  561. static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
  562. {
  563. wait_command_complete(info, channel);
  564. write_reg(info, (unsigned char) (channel + CMDR), cmd);
  565. }
  566. static void tx_pause(struct tty_struct *tty)
  567. {
  568. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  569. unsigned long flags;
  570. if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
  571. return;
  572. if (debug_level >= DEBUG_LEVEL_INFO)
  573. printk("tx_pause(%s)\n",info->device_name);
  574. spin_lock_irqsave(&info->lock,flags);
  575. if (info->tx_enabled)
  576. tx_stop(info);
  577. spin_unlock_irqrestore(&info->lock,flags);
  578. }
  579. static void tx_release(struct tty_struct *tty)
  580. {
  581. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  582. unsigned long flags;
  583. if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
  584. return;
  585. if (debug_level >= DEBUG_LEVEL_INFO)
  586. printk("tx_release(%s)\n",info->device_name);
  587. spin_lock_irqsave(&info->lock,flags);
  588. if (!info->tx_enabled)
  589. tx_start(info, tty);
  590. spin_unlock_irqrestore(&info->lock,flags);
  591. }
  592. /* Return next bottom half action to perform.
  593. * or 0 if nothing to do.
  594. */
  595. static int bh_action(MGSLPC_INFO *info)
  596. {
  597. unsigned long flags;
  598. int rc = 0;
  599. spin_lock_irqsave(&info->lock,flags);
  600. if (info->pending_bh & BH_RECEIVE) {
  601. info->pending_bh &= ~BH_RECEIVE;
  602. rc = BH_RECEIVE;
  603. } else if (info->pending_bh & BH_TRANSMIT) {
  604. info->pending_bh &= ~BH_TRANSMIT;
  605. rc = BH_TRANSMIT;
  606. } else if (info->pending_bh & BH_STATUS) {
  607. info->pending_bh &= ~BH_STATUS;
  608. rc = BH_STATUS;
  609. }
  610. if (!rc) {
  611. /* Mark BH routine as complete */
  612. info->bh_running = false;
  613. info->bh_requested = false;
  614. }
  615. spin_unlock_irqrestore(&info->lock,flags);
  616. return rc;
  617. }
  618. static void bh_handler(struct work_struct *work)
  619. {
  620. MGSLPC_INFO *info = container_of(work, MGSLPC_INFO, task);
  621. struct tty_struct *tty;
  622. int action;
  623. if (!info)
  624. return;
  625. if (debug_level >= DEBUG_LEVEL_BH)
  626. printk( "%s(%d):bh_handler(%s) entry\n",
  627. __FILE__,__LINE__,info->device_name);
  628. info->bh_running = true;
  629. tty = tty_port_tty_get(&info->port);
  630. while((action = bh_action(info)) != 0) {
  631. /* Process work item */
  632. if ( debug_level >= DEBUG_LEVEL_BH )
  633. printk( "%s(%d):bh_handler() work item action=%d\n",
  634. __FILE__,__LINE__,action);
  635. switch (action) {
  636. case BH_RECEIVE:
  637. while(rx_get_frame(info, tty));
  638. break;
  639. case BH_TRANSMIT:
  640. bh_transmit(info, tty);
  641. break;
  642. case BH_STATUS:
  643. bh_status(info);
  644. break;
  645. default:
  646. /* unknown work item ID */
  647. printk("Unknown work item ID=%08X!\n", action);
  648. break;
  649. }
  650. }
  651. tty_kref_put(tty);
  652. if (debug_level >= DEBUG_LEVEL_BH)
  653. printk( "%s(%d):bh_handler(%s) exit\n",
  654. __FILE__,__LINE__,info->device_name);
  655. }
  656. static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty)
  657. {
  658. if (debug_level >= DEBUG_LEVEL_BH)
  659. printk("bh_transmit() entry on %s\n", info->device_name);
  660. if (tty)
  661. tty_wakeup(tty);
  662. }
  663. static void bh_status(MGSLPC_INFO *info)
  664. {
  665. info->ri_chkcount = 0;
  666. info->dsr_chkcount = 0;
  667. info->dcd_chkcount = 0;
  668. info->cts_chkcount = 0;
  669. }
  670. /* eom: non-zero = end of frame */
  671. static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
  672. {
  673. unsigned char data[2];
  674. unsigned char fifo_count, read_count, i;
  675. RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
  676. if (debug_level >= DEBUG_LEVEL_ISR)
  677. printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
  678. if (!info->rx_enabled)
  679. return;
  680. if (info->rx_frame_count >= info->rx_buf_count) {
  681. /* no more free buffers */
  682. issue_command(info, CHA, CMD_RXRESET);
  683. info->pending_bh |= BH_RECEIVE;
  684. info->rx_overflow = true;
  685. info->icount.buf_overrun++;
  686. return;
  687. }
  688. if (eom) {
  689. /* end of frame, get FIFO count from RBCL register */
  690. if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
  691. fifo_count = 32;
  692. } else
  693. fifo_count = 32;
  694. do {
  695. if (fifo_count == 1) {
  696. read_count = 1;
  697. data[0] = read_reg(info, CHA + RXFIFO);
  698. } else {
  699. read_count = 2;
  700. *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
  701. }
  702. fifo_count -= read_count;
  703. if (!fifo_count && eom)
  704. buf->status = data[--read_count];
  705. for (i = 0; i < read_count; i++) {
  706. if (buf->count >= info->max_frame_size) {
  707. /* frame too large, reset receiver and reset current buffer */
  708. issue_command(info, CHA, CMD_RXRESET);
  709. buf->count = 0;
  710. return;
  711. }
  712. *(buf->data + buf->count) = data[i];
  713. buf->count++;
  714. }
  715. } while (fifo_count);
  716. if (eom) {
  717. info->pending_bh |= BH_RECEIVE;
  718. info->rx_frame_count++;
  719. info->rx_put++;
  720. if (info->rx_put >= info->rx_buf_count)
  721. info->rx_put = 0;
  722. }
  723. issue_command(info, CHA, CMD_RXFIFO);
  724. }
  725. static void rx_ready_async(MGSLPC_INFO *info, int tcd, struct tty_struct *tty)
  726. {
  727. unsigned char data, status, flag;
  728. int fifo_count;
  729. int work = 0;
  730. struct mgsl_icount *icount = &info->icount;
  731. if (tcd) {
  732. /* early termination, get FIFO count from RBCL register */
  733. fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
  734. /* Zero fifo count could mean 0 or 32 bytes available.
  735. * If BIT5 of STAR is set then at least 1 byte is available.
  736. */
  737. if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
  738. fifo_count = 32;
  739. } else
  740. fifo_count = 32;
  741. tty_buffer_request_room(tty, fifo_count);
  742. /* Flush received async data to receive data buffer. */
  743. while (fifo_count) {
  744. data = read_reg(info, CHA + RXFIFO);
  745. status = read_reg(info, CHA + RXFIFO);
  746. fifo_count -= 2;
  747. icount->rx++;
  748. flag = TTY_NORMAL;
  749. // if no frameing/crc error then save data
  750. // BIT7:parity error
  751. // BIT6:framing error
  752. if (status & (BIT7 + BIT6)) {
  753. if (status & BIT7)
  754. icount->parity++;
  755. else
  756. icount->frame++;
  757. /* discard char if tty control flags say so */
  758. if (status & info->ignore_status_mask)
  759. continue;
  760. status &= info->read_status_mask;
  761. if (status & BIT7)
  762. flag = TTY_PARITY;
  763. else if (status & BIT6)
  764. flag = TTY_FRAME;
  765. }
  766. work += tty_insert_flip_char(tty, data, flag);
  767. }
  768. issue_command(info, CHA, CMD_RXFIFO);
  769. if (debug_level >= DEBUG_LEVEL_ISR) {
  770. printk("%s(%d):rx_ready_async",
  771. __FILE__,__LINE__);
  772. printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
  773. __FILE__,__LINE__,icount->rx,icount->brk,
  774. icount->parity,icount->frame,icount->overrun);
  775. }
  776. if (work)
  777. tty_flip_buffer_push(tty);
  778. }
  779. static void tx_done(MGSLPC_INFO *info, struct tty_struct *tty)
  780. {
  781. if (!info->tx_active)
  782. return;
  783. info->tx_active = false;
  784. info->tx_aborting = false;
  785. if (info->params.mode == MGSL_MODE_ASYNC)
  786. return;
  787. info->tx_count = info->tx_put = info->tx_get = 0;
  788. del_timer(&info->tx_timer);
  789. if (info->drop_rts_on_tx_done) {
  790. get_signals(info);
  791. if (info->serial_signals & SerialSignal_RTS) {
  792. info->serial_signals &= ~SerialSignal_RTS;
  793. set_signals(info);
  794. }
  795. info->drop_rts_on_tx_done = false;
  796. }
  797. #if SYNCLINK_GENERIC_HDLC
  798. if (info->netcount)
  799. hdlcdev_tx_done(info);
  800. else
  801. #endif
  802. {
  803. if (tty->stopped || tty->hw_stopped) {
  804. tx_stop(info);
  805. return;
  806. }
  807. info->pending_bh |= BH_TRANSMIT;
  808. }
  809. }
  810. static void tx_ready(MGSLPC_INFO *info, struct tty_struct *tty)
  811. {
  812. unsigned char fifo_count = 32;
  813. int c;
  814. if (debug_level >= DEBUG_LEVEL_ISR)
  815. printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
  816. if (info->params.mode == MGSL_MODE_HDLC) {
  817. if (!info->tx_active)
  818. return;
  819. } else {
  820. if (tty->stopped || tty->hw_stopped) {
  821. tx_stop(info);
  822. return;
  823. }
  824. if (!info->tx_count)
  825. info->tx_active = false;
  826. }
  827. if (!info->tx_count)
  828. return;
  829. while (info->tx_count && fifo_count) {
  830. c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
  831. if (c == 1) {
  832. write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
  833. } else {
  834. write_reg16(info, CHA + TXFIFO,
  835. *((unsigned short*)(info->tx_buf + info->tx_get)));
  836. }
  837. info->tx_count -= c;
  838. info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
  839. fifo_count -= c;
  840. }
  841. if (info->params.mode == MGSL_MODE_ASYNC) {
  842. if (info->tx_count < WAKEUP_CHARS)
  843. info->pending_bh |= BH_TRANSMIT;
  844. issue_command(info, CHA, CMD_TXFIFO);
  845. } else {
  846. if (info->tx_count)
  847. issue_command(info, CHA, CMD_TXFIFO);
  848. else
  849. issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
  850. }
  851. }
  852. static void cts_change(MGSLPC_INFO *info, struct tty_struct *tty)
  853. {
  854. get_signals(info);
  855. if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  856. irq_disable(info, CHB, IRQ_CTS);
  857. info->icount.cts++;
  858. if (info->serial_signals & SerialSignal_CTS)
  859. info->input_signal_events.cts_up++;
  860. else
  861. info->input_signal_events.cts_down++;
  862. wake_up_interruptible(&info->status_event_wait_q);
  863. wake_up_interruptible(&info->event_wait_q);
  864. if (info->port.flags & ASYNC_CTS_FLOW) {
  865. if (tty->hw_stopped) {
  866. if (info->serial_signals & SerialSignal_CTS) {
  867. if (debug_level >= DEBUG_LEVEL_ISR)
  868. printk("CTS tx start...");
  869. if (tty)
  870. tty->hw_stopped = 0;
  871. tx_start(info, tty);
  872. info->pending_bh |= BH_TRANSMIT;
  873. return;
  874. }
  875. } else {
  876. if (!(info->serial_signals & SerialSignal_CTS)) {
  877. if (debug_level >= DEBUG_LEVEL_ISR)
  878. printk("CTS tx stop...");
  879. if (tty)
  880. tty->hw_stopped = 1;
  881. tx_stop(info);
  882. }
  883. }
  884. }
  885. info->pending_bh |= BH_STATUS;
  886. }
  887. static void dcd_change(MGSLPC_INFO *info, struct tty_struct *tty)
  888. {
  889. get_signals(info);
  890. if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  891. irq_disable(info, CHB, IRQ_DCD);
  892. info->icount.dcd++;
  893. if (info->serial_signals & SerialSignal_DCD) {
  894. info->input_signal_events.dcd_up++;
  895. }
  896. else
  897. info->input_signal_events.dcd_down++;
  898. #if SYNCLINK_GENERIC_HDLC
  899. if (info->netcount) {
  900. if (info->serial_signals & SerialSignal_DCD)
  901. netif_carrier_on(info->netdev);
  902. else
  903. netif_carrier_off(info->netdev);
  904. }
  905. #endif
  906. wake_up_interruptible(&info->status_event_wait_q);
  907. wake_up_interruptible(&info->event_wait_q);
  908. if (info->port.flags & ASYNC_CHECK_CD) {
  909. if (debug_level >= DEBUG_LEVEL_ISR)
  910. printk("%s CD now %s...", info->device_name,
  911. (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
  912. if (info->serial_signals & SerialSignal_DCD)
  913. wake_up_interruptible(&info->port.open_wait);
  914. else {
  915. if (debug_level >= DEBUG_LEVEL_ISR)
  916. printk("doing serial hangup...");
  917. if (tty)
  918. tty_hangup(tty);
  919. }
  920. }
  921. info->pending_bh |= BH_STATUS;
  922. }
  923. static void dsr_change(MGSLPC_INFO *info)
  924. {
  925. get_signals(info);
  926. if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  927. port_irq_disable(info, PVR_DSR);
  928. info->icount.dsr++;
  929. if (info->serial_signals & SerialSignal_DSR)
  930. info->input_signal_events.dsr_up++;
  931. else
  932. info->input_signal_events.dsr_down++;
  933. wake_up_interruptible(&info->status_event_wait_q);
  934. wake_up_interruptible(&info->event_wait_q);
  935. info->pending_bh |= BH_STATUS;
  936. }
  937. static void ri_change(MGSLPC_INFO *info)
  938. {
  939. get_signals(info);
  940. if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  941. port_irq_disable(info, PVR_RI);
  942. info->icount.rng++;
  943. if (info->serial_signals & SerialSignal_RI)
  944. info->input_signal_events.ri_up++;
  945. else
  946. info->input_signal_events.ri_down++;
  947. wake_up_interruptible(&info->status_event_wait_q);
  948. wake_up_interruptible(&info->event_wait_q);
  949. info->pending_bh |= BH_STATUS;
  950. }
  951. /* Interrupt service routine entry point.
  952. *
  953. * Arguments:
  954. *
  955. * irq interrupt number that caused interrupt
  956. * dev_id device ID supplied during interrupt registration
  957. */
  958. static irqreturn_t mgslpc_isr(int dummy, void *dev_id)
  959. {
  960. MGSLPC_INFO *info = dev_id;
  961. struct tty_struct *tty;
  962. unsigned short isr;
  963. unsigned char gis, pis;
  964. int count=0;
  965. if (debug_level >= DEBUG_LEVEL_ISR)
  966. printk("mgslpc_isr(%d) entry.\n", info->irq_level);
  967. if (!(info->p_dev->_locked))
  968. return IRQ_HANDLED;
  969. tty = tty_port_tty_get(&info->port);
  970. spin_lock(&info->lock);
  971. while ((gis = read_reg(info, CHA + GIS))) {
  972. if (debug_level >= DEBUG_LEVEL_ISR)
  973. printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
  974. if ((gis & 0x70) || count > 1000) {
  975. printk("synclink_cs:hardware failed or ejected\n");
  976. break;
  977. }
  978. count++;
  979. if (gis & (BIT1 + BIT0)) {
  980. isr = read_reg16(info, CHB + ISR);
  981. if (isr & IRQ_DCD)
  982. dcd_change(info, tty);
  983. if (isr & IRQ_CTS)
  984. cts_change(info, tty);
  985. }
  986. if (gis & (BIT3 + BIT2))
  987. {
  988. isr = read_reg16(info, CHA + ISR);
  989. if (isr & IRQ_TIMER) {
  990. info->irq_occurred = true;
  991. irq_disable(info, CHA, IRQ_TIMER);
  992. }
  993. /* receive IRQs */
  994. if (isr & IRQ_EXITHUNT) {
  995. info->icount.exithunt++;
  996. wake_up_interruptible(&info->event_wait_q);
  997. }
  998. if (isr & IRQ_BREAK_ON) {
  999. info->icount.brk++;
  1000. if (info->port.flags & ASYNC_SAK)
  1001. do_SAK(tty);
  1002. }
  1003. if (isr & IRQ_RXTIME) {
  1004. issue_command(info, CHA, CMD_RXFIFO_READ);
  1005. }
  1006. if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
  1007. if (info->params.mode == MGSL_MODE_HDLC)
  1008. rx_ready_hdlc(info, isr & IRQ_RXEOM);
  1009. else
  1010. rx_ready_async(info, isr & IRQ_RXEOM, tty);
  1011. }
  1012. /* transmit IRQs */
  1013. if (isr & IRQ_UNDERRUN) {
  1014. if (info->tx_aborting)
  1015. info->icount.txabort++;
  1016. else
  1017. info->icount.txunder++;
  1018. tx_done(info, tty);
  1019. }
  1020. else if (isr & IRQ_ALLSENT) {
  1021. info->icount.txok++;
  1022. tx_done(info, tty);
  1023. }
  1024. else if (isr & IRQ_TXFIFO)
  1025. tx_ready(info, tty);
  1026. }
  1027. if (gis & BIT7) {
  1028. pis = read_reg(info, CHA + PIS);
  1029. if (pis & BIT1)
  1030. dsr_change(info);
  1031. if (pis & BIT2)
  1032. ri_change(info);
  1033. }
  1034. }
  1035. /* Request bottom half processing if there's something
  1036. * for it to do and the bh is not already running
  1037. */
  1038. if (info->pending_bh && !info->bh_running && !info->bh_requested) {
  1039. if ( debug_level >= DEBUG_LEVEL_ISR )
  1040. printk("%s(%d):%s queueing bh task.\n",
  1041. __FILE__,__LINE__,info->device_name);
  1042. schedule_work(&info->task);
  1043. info->bh_requested = true;
  1044. }
  1045. spin_unlock(&info->lock);
  1046. tty_kref_put(tty);
  1047. if (debug_level >= DEBUG_LEVEL_ISR)
  1048. printk("%s(%d):mgslpc_isr(%d)exit.\n",
  1049. __FILE__, __LINE__, info->irq_level);
  1050. return IRQ_HANDLED;
  1051. }
  1052. /* Initialize and start device.
  1053. */
  1054. static int startup(MGSLPC_INFO * info, struct tty_struct *tty)
  1055. {
  1056. int retval = 0;
  1057. if (debug_level >= DEBUG_LEVEL_INFO)
  1058. printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
  1059. if (info->port.flags & ASYNC_INITIALIZED)
  1060. return 0;
  1061. if (!info->tx_buf) {
  1062. /* allocate a page of memory for a transmit buffer */
  1063. info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
  1064. if (!info->tx_buf) {
  1065. printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
  1066. __FILE__,__LINE__,info->device_name);
  1067. return -ENOMEM;
  1068. }
  1069. }
  1070. info->pending_bh = 0;
  1071. memset(&info->icount, 0, sizeof(info->icount));
  1072. setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
  1073. /* Allocate and claim adapter resources */
  1074. retval = claim_resources(info);
  1075. /* perform existence check and diagnostics */
  1076. if ( !retval )
  1077. retval = adapter_test(info);
  1078. if ( retval ) {
  1079. if (capable(CAP_SYS_ADMIN) && tty)
  1080. set_bit(TTY_IO_ERROR, &tty->flags);
  1081. release_resources(info);
  1082. return retval;
  1083. }
  1084. /* program hardware for current parameters */
  1085. mgslpc_change_params(info, tty);
  1086. if (tty)
  1087. clear_bit(TTY_IO_ERROR, &tty->flags);
  1088. info->port.flags |= ASYNC_INITIALIZED;
  1089. return 0;
  1090. }
  1091. /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
  1092. */
  1093. static void shutdown(MGSLPC_INFO * info, struct tty_struct *tty)
  1094. {
  1095. unsigned long flags;
  1096. if (!(info->port.flags & ASYNC_INITIALIZED))
  1097. return;
  1098. if (debug_level >= DEBUG_LEVEL_INFO)
  1099. printk("%s(%d):mgslpc_shutdown(%s)\n",
  1100. __FILE__,__LINE__, info->device_name );
  1101. /* clear status wait queue because status changes */
  1102. /* can't happen after shutting down the hardware */
  1103. wake_up_interruptible(&info->status_event_wait_q);
  1104. wake_up_interruptible(&info->event_wait_q);
  1105. del_timer_sync(&info->tx_timer);
  1106. if (info->tx_buf) {
  1107. free_page((unsigned long) info->tx_buf);
  1108. info->tx_buf = NULL;
  1109. }
  1110. spin_lock_irqsave(&info->lock,flags);
  1111. rx_stop(info);
  1112. tx_stop(info);
  1113. /* TODO:disable interrupts instead of reset to preserve signal states */
  1114. reset_device(info);
  1115. if (!tty || tty->termios->c_cflag & HUPCL) {
  1116. info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  1117. set_signals(info);
  1118. }
  1119. spin_unlock_irqrestore(&info->lock,flags);
  1120. release_resources(info);
  1121. if (tty)
  1122. set_bit(TTY_IO_ERROR, &tty->flags);
  1123. info->port.flags &= ~ASYNC_INITIALIZED;
  1124. }
  1125. static void mgslpc_program_hw(MGSLPC_INFO *info, struct tty_struct *tty)
  1126. {
  1127. unsigned long flags;
  1128. spin_lock_irqsave(&info->lock,flags);
  1129. rx_stop(info);
  1130. tx_stop(info);
  1131. info->tx_count = info->tx_put = info->tx_get = 0;
  1132. if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
  1133. hdlc_mode(info);
  1134. else
  1135. async_mode(info);
  1136. set_signals(info);
  1137. info->dcd_chkcount = 0;
  1138. info->cts_chkcount = 0;
  1139. info->ri_chkcount = 0;
  1140. info->dsr_chkcount = 0;
  1141. irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
  1142. port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
  1143. get_signals(info);
  1144. if (info->netcount || (tty && (tty->termios->c_cflag & CREAD)))
  1145. rx_start(info);
  1146. spin_unlock_irqrestore(&info->lock,flags);
  1147. }
  1148. /* Reconfigure adapter based on new parameters
  1149. */
  1150. static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty)
  1151. {
  1152. unsigned cflag;
  1153. int bits_per_char;
  1154. if (!tty || !tty->termios)
  1155. return;
  1156. if (debug_level >= DEBUG_LEVEL_INFO)
  1157. printk("%s(%d):mgslpc_change_params(%s)\n",
  1158. __FILE__,__LINE__, info->device_name );
  1159. cflag = tty->termios->c_cflag;
  1160. /* if B0 rate (hangup) specified then negate DTR and RTS */
  1161. /* otherwise assert DTR and RTS */
  1162. if (cflag & CBAUD)
  1163. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  1164. else
  1165. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  1166. /* byte size and parity */
  1167. switch (cflag & CSIZE) {
  1168. case CS5: info->params.data_bits = 5; break;
  1169. case CS6: info->params.data_bits = 6; break;
  1170. case CS7: info->params.data_bits = 7; break;
  1171. case CS8: info->params.data_bits = 8; break;
  1172. default: info->params.data_bits = 7; break;
  1173. }
  1174. if (cflag & CSTOPB)
  1175. info->params.stop_bits = 2;
  1176. else
  1177. info->params.stop_bits = 1;
  1178. info->params.parity = ASYNC_PARITY_NONE;
  1179. if (cflag & PARENB) {
  1180. if (cflag & PARODD)
  1181. info->params.parity = ASYNC_PARITY_ODD;
  1182. else
  1183. info->params.parity = ASYNC_PARITY_EVEN;
  1184. #ifdef CMSPAR
  1185. if (cflag & CMSPAR)
  1186. info->params.parity = ASYNC_PARITY_SPACE;
  1187. #endif
  1188. }
  1189. /* calculate number of jiffies to transmit a full
  1190. * FIFO (32 bytes) at specified data rate
  1191. */
  1192. bits_per_char = info->params.data_bits +
  1193. info->params.stop_bits + 1;
  1194. /* if port data rate is set to 460800 or less then
  1195. * allow tty settings to override, otherwise keep the
  1196. * current data rate.
  1197. */
  1198. if (info->params.data_rate <= 460800) {
  1199. info->params.data_rate = tty_get_baud_rate(tty);
  1200. }
  1201. if ( info->params.data_rate ) {
  1202. info->timeout = (32*HZ*bits_per_char) /
  1203. info->params.data_rate;
  1204. }
  1205. info->timeout += HZ/50; /* Add .02 seconds of slop */
  1206. if (cflag & CRTSCTS)
  1207. info->port.flags |= ASYNC_CTS_FLOW;
  1208. else
  1209. info->port.flags &= ~ASYNC_CTS_FLOW;
  1210. if (cflag & CLOCAL)
  1211. info->port.flags &= ~ASYNC_CHECK_CD;
  1212. else
  1213. info->port.flags |= ASYNC_CHECK_CD;
  1214. /* process tty input control flags */
  1215. info->read_status_mask = 0;
  1216. if (I_INPCK(tty))
  1217. info->read_status_mask |= BIT7 | BIT6;
  1218. if (I_IGNPAR(tty))
  1219. info->ignore_status_mask |= BIT7 | BIT6;
  1220. mgslpc_program_hw(info, tty);
  1221. }
  1222. /* Add a character to the transmit buffer
  1223. */
  1224. static int mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
  1225. {
  1226. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1227. unsigned long flags;
  1228. if (debug_level >= DEBUG_LEVEL_INFO) {
  1229. printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
  1230. __FILE__,__LINE__,ch,info->device_name);
  1231. }
  1232. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
  1233. return 0;
  1234. if (!info->tx_buf)
  1235. return 0;
  1236. spin_lock_irqsave(&info->lock,flags);
  1237. if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
  1238. if (info->tx_count < TXBUFSIZE - 1) {
  1239. info->tx_buf[info->tx_put++] = ch;
  1240. info->tx_put &= TXBUFSIZE-1;
  1241. info->tx_count++;
  1242. }
  1243. }
  1244. spin_unlock_irqrestore(&info->lock,flags);
  1245. return 1;
  1246. }
  1247. /* Enable transmitter so remaining characters in the
  1248. * transmit buffer are sent.
  1249. */
  1250. static void mgslpc_flush_chars(struct tty_struct *tty)
  1251. {
  1252. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1253. unsigned long flags;
  1254. if (debug_level >= DEBUG_LEVEL_INFO)
  1255. printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
  1256. __FILE__,__LINE__,info->device_name,info->tx_count);
  1257. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
  1258. return;
  1259. if (info->tx_count <= 0 || tty->stopped ||
  1260. tty->hw_stopped || !info->tx_buf)
  1261. return;
  1262. if (debug_level >= DEBUG_LEVEL_INFO)
  1263. printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
  1264. __FILE__,__LINE__,info->device_name);
  1265. spin_lock_irqsave(&info->lock,flags);
  1266. if (!info->tx_active)
  1267. tx_start(info, tty);
  1268. spin_unlock_irqrestore(&info->lock,flags);
  1269. }
  1270. /* Send a block of data
  1271. *
  1272. * Arguments:
  1273. *
  1274. * tty pointer to tty information structure
  1275. * buf pointer to buffer containing send data
  1276. * count size of send data in bytes
  1277. *
  1278. * Returns: number of characters written
  1279. */
  1280. static int mgslpc_write(struct tty_struct * tty,
  1281. const unsigned char *buf, int count)
  1282. {
  1283. int c, ret = 0;
  1284. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1285. unsigned long flags;
  1286. if (debug_level >= DEBUG_LEVEL_INFO)
  1287. printk( "%s(%d):mgslpc_write(%s) count=%d\n",
  1288. __FILE__,__LINE__,info->device_name,count);
  1289. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
  1290. !info->tx_buf)
  1291. goto cleanup;
  1292. if (info->params.mode == MGSL_MODE_HDLC) {
  1293. if (count > TXBUFSIZE) {
  1294. ret = -EIO;
  1295. goto cleanup;
  1296. }
  1297. if (info->tx_active)
  1298. goto cleanup;
  1299. else if (info->tx_count)
  1300. goto start;
  1301. }
  1302. for (;;) {
  1303. c = min(count,
  1304. min(TXBUFSIZE - info->tx_count - 1,
  1305. TXBUFSIZE - info->tx_put));
  1306. if (c <= 0)
  1307. break;
  1308. memcpy(info->tx_buf + info->tx_put, buf, c);
  1309. spin_lock_irqsave(&info->lock,flags);
  1310. info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
  1311. info->tx_count += c;
  1312. spin_unlock_irqrestore(&info->lock,flags);
  1313. buf += c;
  1314. count -= c;
  1315. ret += c;
  1316. }
  1317. start:
  1318. if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
  1319. spin_lock_irqsave(&info->lock,flags);
  1320. if (!info->tx_active)
  1321. tx_start(info, tty);
  1322. spin_unlock_irqrestore(&info->lock,flags);
  1323. }
  1324. cleanup:
  1325. if (debug_level >= DEBUG_LEVEL_INFO)
  1326. printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
  1327. __FILE__,__LINE__,info->device_name,ret);
  1328. return ret;
  1329. }
  1330. /* Return the count of free bytes in transmit buffer
  1331. */
  1332. static int mgslpc_write_room(struct tty_struct *tty)
  1333. {
  1334. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1335. int ret;
  1336. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
  1337. return 0;
  1338. if (info->params.mode == MGSL_MODE_HDLC) {
  1339. /* HDLC (frame oriented) mode */
  1340. if (info->tx_active)
  1341. return 0;
  1342. else
  1343. return HDLC_MAX_FRAME_SIZE;
  1344. } else {
  1345. ret = TXBUFSIZE - info->tx_count - 1;
  1346. if (ret < 0)
  1347. ret = 0;
  1348. }
  1349. if (debug_level >= DEBUG_LEVEL_INFO)
  1350. printk("%s(%d):mgslpc_write_room(%s)=%d\n",
  1351. __FILE__,__LINE__, info->device_name, ret);
  1352. return ret;
  1353. }
  1354. /* Return the count of bytes in transmit buffer
  1355. */
  1356. static int mgslpc_chars_in_buffer(struct tty_struct *tty)
  1357. {
  1358. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1359. int rc;
  1360. if (debug_level >= DEBUG_LEVEL_INFO)
  1361. printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
  1362. __FILE__,__LINE__, info->device_name );
  1363. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
  1364. return 0;
  1365. if (info->params.mode == MGSL_MODE_HDLC)
  1366. rc = info->tx_active ? info->max_frame_size : 0;
  1367. else
  1368. rc = info->tx_count;
  1369. if (debug_level >= DEBUG_LEVEL_INFO)
  1370. printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
  1371. __FILE__,__LINE__, info->device_name, rc);
  1372. return rc;
  1373. }
  1374. /* Discard all data in the send buffer
  1375. */
  1376. static void mgslpc_flush_buffer(struct tty_struct *tty)
  1377. {
  1378. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1379. unsigned long flags;
  1380. if (debug_level >= DEBUG_LEVEL_INFO)
  1381. printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
  1382. __FILE__,__LINE__, info->device_name );
  1383. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
  1384. return;
  1385. spin_lock_irqsave(&info->lock,flags);
  1386. info->tx_count = info->tx_put = info->tx_get = 0;
  1387. del_timer(&info->tx_timer);
  1388. spin_unlock_irqrestore(&info->lock,flags);
  1389. wake_up_interruptible(&tty->write_wait);
  1390. tty_wakeup(tty);
  1391. }
  1392. /* Send a high-priority XON/XOFF character
  1393. */
  1394. static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
  1395. {
  1396. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1397. unsigned long flags;
  1398. if (debug_level >= DEBUG_LEVEL_INFO)
  1399. printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
  1400. __FILE__,__LINE__, info->device_name, ch );
  1401. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
  1402. return;
  1403. info->x_char = ch;
  1404. if (ch) {
  1405. spin_lock_irqsave(&info->lock,flags);
  1406. if (!info->tx_enabled)
  1407. tx_start(info, tty);
  1408. spin_unlock_irqrestore(&info->lock,flags);
  1409. }
  1410. }
  1411. /* Signal remote device to throttle send data (our receive data)
  1412. */
  1413. static void mgslpc_throttle(struct tty_struct * tty)
  1414. {
  1415. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1416. unsigned long flags;
  1417. if (debug_level >= DEBUG_LEVEL_INFO)
  1418. printk("%s(%d):mgslpc_throttle(%s) entry\n",
  1419. __FILE__,__LINE__, info->device_name );
  1420. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
  1421. return;
  1422. if (I_IXOFF(tty))
  1423. mgslpc_send_xchar(tty, STOP_CHAR(tty));
  1424. if (tty->termios->c_cflag & CRTSCTS) {
  1425. spin_lock_irqsave(&info->lock,flags);
  1426. info->serial_signals &= ~SerialSignal_RTS;
  1427. set_signals(info);
  1428. spin_unlock_irqrestore(&info->lock,flags);
  1429. }
  1430. }
  1431. /* Signal remote device to stop throttling send data (our receive data)
  1432. */
  1433. static void mgslpc_unthrottle(struct tty_struct * tty)
  1434. {
  1435. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1436. unsigned long flags;
  1437. if (debug_level >= DEBUG_LEVEL_INFO)
  1438. printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
  1439. __FILE__,__LINE__, info->device_name );
  1440. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
  1441. return;
  1442. if (I_IXOFF(tty)) {
  1443. if (info->x_char)
  1444. info->x_char = 0;
  1445. else
  1446. mgslpc_send_xchar(tty, START_CHAR(tty));
  1447. }
  1448. if (tty->termios->c_cflag & CRTSCTS) {
  1449. spin_lock_irqsave(&info->lock,flags);
  1450. info->serial_signals |= SerialSignal_RTS;
  1451. set_signals(info);
  1452. spin_unlock_irqrestore(&info->lock,flags);
  1453. }
  1454. }
  1455. /* get the current serial statistics
  1456. */
  1457. static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
  1458. {
  1459. int err;
  1460. if (debug_level >= DEBUG_LEVEL_INFO)
  1461. printk("get_params(%s)\n", info->device_name);
  1462. if (!user_icount) {
  1463. memset(&info->icount, 0, sizeof(info->icount));
  1464. } else {
  1465. COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
  1466. if (err)
  1467. return -EFAULT;
  1468. }
  1469. return 0;
  1470. }
  1471. /* get the current serial parameters
  1472. */
  1473. static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
  1474. {
  1475. int err;
  1476. if (debug_level >= DEBUG_LEVEL_INFO)
  1477. printk("get_params(%s)\n", info->device_name);
  1478. COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
  1479. if (err)
  1480. return -EFAULT;
  1481. return 0;
  1482. }
  1483. /* set the serial parameters
  1484. *
  1485. * Arguments:
  1486. *
  1487. * info pointer to device instance data
  1488. * new_params user buffer containing new serial params
  1489. *
  1490. * Returns: 0 if success, otherwise error code
  1491. */
  1492. static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params, struct tty_struct *tty)
  1493. {
  1494. unsigned long flags;
  1495. MGSL_PARAMS tmp_params;
  1496. int err;
  1497. if (debug_level >= DEBUG_LEVEL_INFO)
  1498. printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
  1499. info->device_name );
  1500. COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
  1501. if (err) {
  1502. if ( debug_level >= DEBUG_LEVEL_INFO )
  1503. printk( "%s(%d):set_params(%s) user buffer copy failed\n",
  1504. __FILE__,__LINE__,info->device_name);
  1505. return -EFAULT;
  1506. }
  1507. spin_lock_irqsave(&info->lock,flags);
  1508. memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
  1509. spin_unlock_irqrestore(&info->lock,flags);
  1510. mgslpc_change_params(info, tty);
  1511. return 0;
  1512. }
  1513. static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
  1514. {
  1515. int err;
  1516. if (debug_level >= DEBUG_LEVEL_INFO)
  1517. printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
  1518. COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
  1519. if (err)
  1520. return -EFAULT;
  1521. return 0;
  1522. }
  1523. static int set_txidle(MGSLPC_INFO * info, int idle_mode)
  1524. {
  1525. unsigned long flags;
  1526. if (debug_level >= DEBUG_LEVEL_INFO)
  1527. printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
  1528. spin_lock_irqsave(&info->lock,flags);
  1529. info->idle_mode = idle_mode;
  1530. tx_set_idle(info);
  1531. spin_unlock_irqrestore(&info->lock,flags);
  1532. return 0;
  1533. }
  1534. static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
  1535. {
  1536. int err;
  1537. if (debug_level >= DEBUG_LEVEL_INFO)
  1538. printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
  1539. COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
  1540. if (err)
  1541. return -EFAULT;
  1542. return 0;
  1543. }
  1544. static int set_interface(MGSLPC_INFO * info, int if_mode)
  1545. {
  1546. unsigned long flags;
  1547. unsigned char val;
  1548. if (debug_level >= DEBUG_LEVEL_INFO)
  1549. printk("set_interface(%s,%d)\n", info->device_name, if_mode);
  1550. spin_lock_irqsave(&info->lock,flags);
  1551. info->if_mode = if_mode;
  1552. val = read_reg(info, PVR) & 0x0f;
  1553. switch (info->if_mode)
  1554. {
  1555. case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
  1556. case MGSL_INTERFACE_V35: val |= PVR_V35; break;
  1557. case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
  1558. }
  1559. write_reg(info, PVR, val);
  1560. spin_unlock_irqrestore(&info->lock,flags);
  1561. return 0;
  1562. }
  1563. static int set_txenable(MGSLPC_INFO * info, int enable, struct tty_struct *tty)
  1564. {
  1565. unsigned long flags;
  1566. if (debug_level >= DEBUG_LEVEL_INFO)
  1567. printk("set_txenable(%s,%d)\n", info->device_name, enable);
  1568. spin_lock_irqsave(&info->lock,flags);
  1569. if (enable) {
  1570. if (!info->tx_enabled)
  1571. tx_start(info, tty);
  1572. } else {
  1573. if (info->tx_enabled)
  1574. tx_stop(info);
  1575. }
  1576. spin_unlock_irqrestore(&info->lock,flags);
  1577. return 0;
  1578. }
  1579. static int tx_abort(MGSLPC_INFO * info)
  1580. {
  1581. unsigned long flags;
  1582. if (debug_level >= DEBUG_LEVEL_INFO)
  1583. printk("tx_abort(%s)\n", info->device_name);
  1584. spin_lock_irqsave(&info->lock,flags);
  1585. if (info->tx_active && info->tx_count &&
  1586. info->params.mode == MGSL_MODE_HDLC) {
  1587. /* clear data count so FIFO is not filled on next IRQ.
  1588. * This results in underrun and abort transmission.
  1589. */
  1590. info->tx_count = info->tx_put = info->tx_get = 0;
  1591. info->tx_aborting = true;
  1592. }
  1593. spin_unlock_irqrestore(&info->lock,flags);
  1594. return 0;
  1595. }
  1596. static int set_rxenable(MGSLPC_INFO * info, int enable)
  1597. {
  1598. unsigned long flags;
  1599. if (debug_level >= DEBUG_LEVEL_INFO)
  1600. printk("set_rxenable(%s,%d)\n", info->device_name, enable);
  1601. spin_lock_irqsave(&info->lock,flags);
  1602. if (enable) {
  1603. if (!info->rx_enabled)
  1604. rx_start(info);
  1605. } else {
  1606. if (info->rx_enabled)
  1607. rx_stop(info);
  1608. }
  1609. spin_unlock_irqrestore(&info->lock,flags);
  1610. return 0;
  1611. }
  1612. /* wait for specified event to occur
  1613. *
  1614. * Arguments: info pointer to device instance data
  1615. * mask pointer to bitmask of events to wait for
  1616. * Return Value: 0 if successful and bit mask updated with
  1617. * of events triggerred,
  1618. * otherwise error code
  1619. */
  1620. static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
  1621. {
  1622. unsigned long flags;
  1623. int s;
  1624. int rc=0;
  1625. struct mgsl_icount cprev, cnow;
  1626. int events;
  1627. int mask;
  1628. struct _input_signal_events oldsigs, newsigs;
  1629. DECLARE_WAITQUEUE(wait, current);
  1630. COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
  1631. if (rc)
  1632. return -EFAULT;
  1633. if