2 * linux/drivers/char/pcmcia/synclink_cs.c
4 * $Id: synclink_cs.c,v 4.34 2005/09/08 13:20:54 paulkf Exp $
6 * Device driver for Microgate SyncLink PC Card
7 * multiprotocol serial adapter.
9 * written by Paul Fulghum for Microgate Corporation
10 * paulkf@microgate.com
12 * Microgate and SyncLink are trademarks of Microgate Corporation
14 * This code is released under the GNU General Public License (GPL)
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.
29 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
31 # define BREAKPOINT() asm(" int $3");
33 # define BREAKPOINT() { }
36 #define MAX_DEVICE_COUNT 4
38 #include <linux/module.h>
39 #include <linux/errno.h>
40 #include <linux/signal.h>
41 #include <linux/sched.h>
42 #include <linux/timer.h>
43 #include <linux/time.h>
44 #include <linux/interrupt.h>
45 #include <linux/tty.h>
46 #include <linux/tty_flip.h>
47 #include <linux/serial.h>
48 #include <linux/major.h>
49 #include <linux/string.h>
50 #include <linux/fcntl.h>
51 #include <linux/ptrace.h>
52 #include <linux/ioport.h>
54 #include <linux/seq_file.h>
55 #include <linux/slab.h>
56 #include <linux/netdevice.h>
57 #include <linux/vmalloc.h>
58 #include <linux/init.h>
59 #include <linux/delay.h>
60 #include <linux/ioctl.h>
61 #include <linux/synclink.h>
63 #include <asm/system.h>
67 #include <linux/bitops.h>
68 #include <asm/types.h>
69 #include <linux/termios.h>
70 #include <linux/workqueue.h>
71 #include <linux/hdlc.h>
73 #include <pcmcia/cs.h>
74 #include <pcmcia/cistpl.h>
75 #include <pcmcia/cisreg.h>
76 #include <pcmcia/ds.h>
78 #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_CS_MODULE))
79 #define SYNCLINK_GENERIC_HDLC 1
81 #define SYNCLINK_GENERIC_HDLC 0
84 #define GET_USER(error,value,addr) error = get_user(value,addr)
85 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
86 #define PUT_USER(error,value,addr) error = put_user(value,addr)
87 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
89 #include <asm/uaccess.h>
91 static MGSL_PARAMS default_params = {
92 MGSL_MODE_HDLC, /* unsigned long mode */
93 0, /* unsigned char loopback; */
94 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
95 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
96 0, /* unsigned long clock_speed; */
97 0xff, /* unsigned char addr_filter; */
98 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
99 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
100 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
101 9600, /* unsigned long data_rate; */
102 8, /* unsigned char data_bits; */
103 1, /* unsigned char stop_bits; */
104 ASYNC_PARITY_NONE /* unsigned char parity; */
110 unsigned char status;
114 /* The queue of BH actions to be performed */
117 #define BH_TRANSMIT 2
120 #define IO_PIN_SHUTDOWN_LIMIT 100
122 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
124 struct _input_signal_events {
137 * Device instance data structure
140 typedef struct _mgslpc_info {
141 struct tty_port port;
142 void *if_ptr; /* General purpose pointer (used by SPPP) */
146 struct mgsl_icount icount;
149 int x_char; /* xon/xoff character */
150 unsigned char read_status_mask;
151 unsigned char ignore_status_mask;
153 unsigned char *tx_buf;
158 /* circular list of fixed length rx buffers */
160 unsigned char *rx_buf; /* memory allocated for all rx buffers */
161 int rx_buf_total_size; /* size of memory allocated for rx buffers */
162 int rx_put; /* index of next empty rx buffer */
163 int rx_get; /* index of next full rx buffer */
164 int rx_buf_size; /* size in bytes of single rx buffer */
165 int rx_buf_count; /* total number of rx buffers */
166 int rx_frame_count; /* number of full rx buffers */
168 wait_queue_head_t status_event_wait_q;
169 wait_queue_head_t event_wait_q;
170 struct timer_list tx_timer; /* HDLC transmit timeout timer */
171 struct _mgslpc_info *next_device; /* device list link */
173 unsigned short imra_value;
174 unsigned short imrb_value;
175 unsigned char pim_value;
178 struct work_struct task; /* task structure for scheduling bh */
187 int dcd_chkcount; /* check counts to prevent */
188 int cts_chkcount; /* too many IRQs if a signal */
189 int dsr_chkcount; /* is floating */
200 int if_mode; /* serial interface selection (RS-232, v.35 etc) */
202 char device_name[25]; /* device instance name */
204 unsigned int io_base; /* base I/O address of adapter */
205 unsigned int irq_level;
207 MGSL_PARAMS params; /* communications parameters */
209 unsigned char serial_signals; /* current serial signal states */
211 bool irq_occurred; /* for diagnostics use */
213 unsigned int init_error; /* startup error (DIAGS) */
215 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
216 bool drop_rts_on_tx_done;
218 struct _input_signal_events input_signal_events;
221 struct pcmcia_device *p_dev;
224 /* SPPP/Cisco HDLC device parts */
228 #if SYNCLINK_GENERIC_HDLC
229 struct net_device *netdev;
234 #define MGSLPC_MAGIC 0x5402
237 * The size of the serial xmit buffer is 1 page, or 4096 bytes
239 #define TXBUFSIZE 4096
242 #define CHA 0x00 /* channel A offset */
243 #define CHB 0x40 /* channel B offset */
246 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
293 #define IRQ_BREAK_ON BIT15 // rx break detected
294 #define IRQ_DATAOVERRUN BIT14 // receive data overflow
295 #define IRQ_ALLSENT BIT13 // all sent
296 #define IRQ_UNDERRUN BIT12 // transmit data underrun
297 #define IRQ_TIMER BIT11 // timer interrupt
298 #define IRQ_CTS BIT10 // CTS status change
299 #define IRQ_TXREPEAT BIT9 // tx message repeat
300 #define IRQ_TXFIFO BIT8 // transmit pool ready
301 #define IRQ_RXEOM BIT7 // receive message end
302 #define IRQ_EXITHUNT BIT6 // receive frame start
303 #define IRQ_RXTIME BIT6 // rx char timeout
304 #define IRQ_DCD BIT2 // carrier detect status change
305 #define IRQ_OVERRUN BIT1 // receive frame overflow
306 #define IRQ_RXFIFO BIT0 // receive pool full
310 #define XFW BIT6 // transmit FIFO write enable
311 #define CEC BIT2 // command executing
312 #define CTS BIT1 // CTS state
317 #define PVR_AUTOCTS BIT3
318 #define PVR_RS232 0x20 /* 0010b */
319 #define PVR_V35 0xe0 /* 1110b */
320 #define PVR_RS422 0x40 /* 0100b */
322 /* Register access functions */
324 #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
325 #define read_reg(info, reg) inb((info)->io_base + (reg))
327 #define read_reg16(info, reg) inw((info)->io_base + (reg))
328 #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
330 #define set_reg_bits(info, reg, mask) \
331 write_reg(info, (reg), \
332 (unsigned char) (read_reg(info, (reg)) | (mask)))
333 #define clear_reg_bits(info, reg, mask) \
334 write_reg(info, (reg), \
335 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
337 * interrupt enable/disable routines
339 static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
341 if (channel == CHA) {
342 info->imra_value |= mask;
343 write_reg16(info, CHA + IMR, info->imra_value);
345 info->imrb_value |= mask;
346 write_reg16(info, CHB + IMR, info->imrb_value);
349 static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
351 if (channel == CHA) {
352 info->imra_value &= ~mask;
353 write_reg16(info, CHA + IMR, info->imra_value);
355 info->imrb_value &= ~mask;
356 write_reg16(info, CHB + IMR, info->imrb_value);
360 #define port_irq_disable(info, mask) \
361 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
363 #define port_irq_enable(info, mask) \
364 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
366 static void rx_start(MGSLPC_INFO *info);
367 static void rx_stop(MGSLPC_INFO *info);
369 static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty);
370 static void tx_stop(MGSLPC_INFO *info);
371 static void tx_set_idle(MGSLPC_INFO *info);
373 static void get_signals(MGSLPC_INFO *info);
374 static void set_signals(MGSLPC_INFO *info);
376 static void reset_device(MGSLPC_INFO *info);
378 static void hdlc_mode(MGSLPC_INFO *info);
379 static void async_mode(MGSLPC_INFO *info);
381 static void tx_timeout(unsigned long context);
383 static int carrier_raised(struct tty_port *port);
384 static void dtr_rts(struct tty_port *port, int onoff);
386 #if SYNCLINK_GENERIC_HDLC
387 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
388 static void hdlcdev_tx_done(MGSLPC_INFO *info);
389 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
390 static int hdlcdev_init(MGSLPC_INFO *info);
391 static void hdlcdev_exit(MGSLPC_INFO *info);
394 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
396 static bool register_test(MGSLPC_INFO *info);
397 static bool irq_test(MGSLPC_INFO *info);
398 static int adapter_test(MGSLPC_INFO *info);
400 static int claim_resources(MGSLPC_INFO *info);
401 static void release_resources(MGSLPC_INFO *info);
402 static void mgslpc_add_device(MGSLPC_INFO *info);
403 static void mgslpc_remove_device(MGSLPC_INFO *info);
405 static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty);
406 static void rx_reset_buffers(MGSLPC_INFO *info);
407 static int rx_alloc_buffers(MGSLPC_INFO *info);
408 static void rx_free_buffers(MGSLPC_INFO *info);
410 static irqreturn_t mgslpc_isr(int irq, void *dev_id);
413 * Bottom half interrupt handlers
415 static void bh_handler(struct work_struct *work);
416 static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty);
417 static void bh_status(MGSLPC_INFO *info);
422 static int tiocmget(struct tty_struct *tty, struct file *file);
423 static int tiocmset(struct tty_struct *tty, struct file *file,
424 unsigned int set, unsigned int clear);
425 static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
426 static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
427 static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params, struct tty_struct *tty);
428 static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
429 static int set_txidle(MGSLPC_INFO *info, int idle_mode);
430 static int set_txenable(MGSLPC_INFO *info, int enable, struct tty_struct *tty);
431 static int tx_abort(MGSLPC_INFO *info);
432 static int set_rxenable(MGSLPC_INFO *info, int enable);
433 static int wait_events(MGSLPC_INFO *info, int __user *mask);
435 static MGSLPC_INFO *mgslpc_device_list = NULL;
436 static int mgslpc_device_count = 0;
439 * Set this param to non-zero to load eax with the
440 * .text section address and breakpoint on module load.
441 * This is useful for use with gdb and add-symbol-file command.
443 static int break_on_load=0;
446 * Driver major number, defaults to zero to get auto
447 * assigned major number. May be forced as module parameter.
449 static int ttymajor=0;
451 static int debug_level = 0;
452 static int maxframe[MAX_DEVICE_COUNT] = {0,};
454 module_param(break_on_load, bool, 0);
455 module_param(ttymajor, int, 0);
456 module_param(debug_level, int, 0);
457 module_param_array(maxframe, int, NULL, 0);
459 MODULE_LICENSE("GPL");
461 static char *driver_name = "SyncLink PC Card driver";
462 static char *driver_version = "$Revision: 4.34 $";
464 static struct tty_driver *serial_driver;
466 /* number of characters left in xmit buffer before we ask for more */
467 #define WAKEUP_CHARS 256
469 static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty);
470 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
472 /* PCMCIA prototypes */
474 static int mgslpc_config(struct pcmcia_device *link);
475 static void mgslpc_release(u_long arg);
476 static void mgslpc_detach(struct pcmcia_device *p_dev);
479 * 1st function defined in .text section. Calling this function in
480 * init_module() followed by a breakpoint allows a remote debugger
481 * (gdb) to get the .text address for the add-symbol-file command.
482 * This allows remote debugging of dynamically loadable modules.
484 static void* mgslpc_get_text_ptr(void)
486 return mgslpc_get_text_ptr;
490 * line discipline callback wrappers
492 * The wrappers maintain line discipline references
493 * while calling into the line discipline.
495 * ldisc_receive_buf - pass receive data to line discipline
498 static void ldisc_receive_buf(struct tty_struct *tty,
499 const __u8 *data, char *flags, int count)
501 struct tty_ldisc *ld;
504 ld = tty_ldisc_ref(tty);
506 if (ld->ops->receive_buf)
507 ld->ops->receive_buf(tty, data, flags, count);
512 static const struct tty_port_operations mgslpc_port_ops = {
513 .carrier_raised = carrier_raised,
517 static int mgslpc_probe(struct pcmcia_device *link)
522 if (debug_level >= DEBUG_LEVEL_INFO)
523 printk("mgslpc_attach\n");
525 info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
527 printk("Error can't allocate device instance data\n");
531 info->magic = MGSLPC_MAGIC;
532 tty_port_init(&info->port);
533 info->port.ops = &mgslpc_port_ops;
534 INIT_WORK(&info->task, bh_handler);
535 info->max_frame_size = 4096;
536 info->port.close_delay = 5*HZ/10;
537 info->port.closing_wait = 30*HZ;
538 init_waitqueue_head(&info->status_event_wait_q);
539 init_waitqueue_head(&info->event_wait_q);
540 spin_lock_init(&info->lock);
541 spin_lock_init(&info->netlock);
542 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
543 info->idle_mode = HDLC_TXIDLE_FLAGS;
544 info->imra_value = 0xffff;
545 info->imrb_value = 0xffff;
546 info->pim_value = 0xff;
551 /* Initialize the struct pcmcia_device structure */
553 link->conf.Attributes = 0;
554 link->conf.IntType = INT_MEMORY_AND_IO;
556 ret = mgslpc_config(link);
560 mgslpc_add_device(info);
565 /* Card has been inserted.
568 static int mgslpc_ioprobe(struct pcmcia_device *p_dev,
569 cistpl_cftable_entry_t *cfg,
570 cistpl_cftable_entry_t *dflt,
574 if (cfg->io.nwin > 0) {
575 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
576 if (!(cfg->io.flags & CISTPL_IO_8BIT))
577 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
578 if (!(cfg->io.flags & CISTPL_IO_16BIT))
579 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
580 p_dev->io.IOAddrLines = cfg->io.flags & CISTPL_IO_LINES_MASK;
581 p_dev->io.BasePort1 = cfg->io.win[0].base;
582 p_dev->io.NumPorts1 = cfg->io.win[0].len;
583 return pcmcia_request_io(p_dev, &p_dev->io);
588 static int mgslpc_config(struct pcmcia_device *link)
590 MGSLPC_INFO *info = link->priv;
593 if (debug_level >= DEBUG_LEVEL_INFO)
594 printk("mgslpc_config(0x%p)\n", link);
596 ret = pcmcia_loop_config(link, mgslpc_ioprobe, NULL);
600 link->conf.Attributes = CONF_ENABLE_IRQ;
601 link->conf.IntType = INT_MEMORY_AND_IO;
602 link->conf.ConfigIndex = 8;
603 link->conf.Present = PRESENT_OPTION;
605 ret = pcmcia_request_irq(link, mgslpc_isr);
608 ret = pcmcia_request_configuration(link, &link->conf);
612 info->io_base = link->io.BasePort1;
613 info->irq_level = link->irq;
615 dev_info(&link->dev, "index 0x%02x:",
616 link->conf.ConfigIndex);
617 if (link->conf.Attributes & CONF_ENABLE_IRQ)
618 printk(", irq %d", link->irq);
619 if (link->io.NumPorts1)
620 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
621 link->io.BasePort1+link->io.NumPorts1-1);
626 mgslpc_release((u_long)link);
630 /* Card has been removed.
631 * Unregister device and release PCMCIA configuration.
632 * If device is open, postpone until it is closed.
634 static void mgslpc_release(u_long arg)
636 struct pcmcia_device *link = (struct pcmcia_device *)arg;
638 if (debug_level >= DEBUG_LEVEL_INFO)
639 printk("mgslpc_release(0x%p)\n", link);
641 pcmcia_disable_device(link);
644 static void mgslpc_detach(struct pcmcia_device *link)
646 if (debug_level >= DEBUG_LEVEL_INFO)
647 printk("mgslpc_detach(0x%p)\n", link);
649 ((MGSLPC_INFO *)link->priv)->stop = 1;
650 mgslpc_release((u_long)link);
652 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
655 static int mgslpc_suspend(struct pcmcia_device *link)
657 MGSLPC_INFO *info = link->priv;
664 static int mgslpc_resume(struct pcmcia_device *link)
666 MGSLPC_INFO *info = link->priv;
674 static inline bool mgslpc_paranoia_check(MGSLPC_INFO *info,
675 char *name, const char *routine)
677 #ifdef MGSLPC_PARANOIA_CHECK
678 static const char *badmagic =
679 "Warning: bad magic number for mgsl struct (%s) in %s\n";
680 static const char *badinfo =
681 "Warning: null mgslpc_info for (%s) in %s\n";
684 printk(badinfo, name, routine);
687 if (info->magic != MGSLPC_MAGIC) {
688 printk(badmagic, name, routine);
699 #define CMD_RXFIFO BIT7 // release current rx FIFO
700 #define CMD_RXRESET BIT6 // receiver reset
701 #define CMD_RXFIFO_READ BIT5
702 #define CMD_START_TIMER BIT4
703 #define CMD_TXFIFO BIT3 // release current tx FIFO
704 #define CMD_TXEOM BIT1 // transmit end message
705 #define CMD_TXRESET BIT0 // transmit reset
707 static bool wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
710 /* wait for command completion */
711 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
719 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
721 wait_command_complete(info, channel);
722 write_reg(info, (unsigned char) (channel + CMDR), cmd);
725 static void tx_pause(struct tty_struct *tty)
727 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
730 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
732 if (debug_level >= DEBUG_LEVEL_INFO)
733 printk("tx_pause(%s)\n",info->device_name);
735 spin_lock_irqsave(&info->lock,flags);
736 if (info->tx_enabled)
738 spin_unlock_irqrestore(&info->lock,flags);
741 static void tx_release(struct tty_struct *tty)
743 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
746 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
748 if (debug_level >= DEBUG_LEVEL_INFO)
749 printk("tx_release(%s)\n",info->device_name);
751 spin_lock_irqsave(&info->lock,flags);
752 if (!info->tx_enabled)
754 spin_unlock_irqrestore(&info->lock,flags);
757 /* Return next bottom half action to perform.
758 * or 0 if nothing to do.
760 static int bh_action(MGSLPC_INFO *info)
765 spin_lock_irqsave(&info->lock,flags);
767 if (info->pending_bh & BH_RECEIVE) {
768 info->pending_bh &= ~BH_RECEIVE;
770 } else if (info->pending_bh & BH_TRANSMIT) {
771 info->pending_bh &= ~BH_TRANSMIT;
773 } else if (info->pending_bh & BH_STATUS) {
774 info->pending_bh &= ~BH_STATUS;
779 /* Mark BH routine as complete */
780 info->bh_running = false;
781 info->bh_requested = false;
784 spin_unlock_irqrestore(&info->lock,flags);
789 static void bh_handler(struct work_struct *work)
791 MGSLPC_INFO *info = container_of(work, MGSLPC_INFO, task);
792 struct tty_struct *tty;
798 if (debug_level >= DEBUG_LEVEL_BH)
799 printk( "%s(%d):bh_handler(%s) entry\n",
800 __FILE__,__LINE__,info->device_name);
802 info->bh_running = true;
803 tty = tty_port_tty_get(&info->port);
805 while((action = bh_action(info)) != 0) {
807 /* Process work item */
808 if ( debug_level >= DEBUG_LEVEL_BH )
809 printk( "%s(%d):bh_handler() work item action=%d\n",
810 __FILE__,__LINE__,action);
815 while(rx_get_frame(info, tty));
818 bh_transmit(info, tty);
824 /* unknown work item ID */
825 printk("Unknown work item ID=%08X!\n", action);
831 if (debug_level >= DEBUG_LEVEL_BH)
832 printk( "%s(%d):bh_handler(%s) exit\n",
833 __FILE__,__LINE__,info->device_name);
836 static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty)
838 if (debug_level >= DEBUG_LEVEL_BH)
839 printk("bh_transmit() entry on %s\n", info->device_name);
845 static void bh_status(MGSLPC_INFO *info)
847 info->ri_chkcount = 0;
848 info->dsr_chkcount = 0;
849 info->dcd_chkcount = 0;
850 info->cts_chkcount = 0;
853 /* eom: non-zero = end of frame */
854 static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
856 unsigned char data[2];
857 unsigned char fifo_count, read_count, i;
858 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
860 if (debug_level >= DEBUG_LEVEL_ISR)
861 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
863 if (!info->rx_enabled)
866 if (info->rx_frame_count >= info->rx_buf_count) {
867 /* no more free buffers */
868 issue_command(info, CHA, CMD_RXRESET);
869 info->pending_bh |= BH_RECEIVE;
870 info->rx_overflow = true;
871 info->icount.buf_overrun++;
876 /* end of frame, get FIFO count from RBCL register */
877 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
883 if (fifo_count == 1) {
885 data[0] = read_reg(info, CHA + RXFIFO);
888 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
890 fifo_count -= read_count;
891 if (!fifo_count && eom)
892 buf->status = data[--read_count];
894 for (i = 0; i < read_count; i++) {
895 if (buf->count >= info->max_frame_size) {
896 /* frame too large, reset receiver and reset current buffer */
897 issue_command(info, CHA, CMD_RXRESET);
901 *(buf->data + buf->count) = data[i];
904 } while (fifo_count);
907 info->pending_bh |= BH_RECEIVE;
908 info->rx_frame_count++;
910 if (info->rx_put >= info->rx_buf_count)
913 issue_command(info, CHA, CMD_RXFIFO);
916 static void rx_ready_async(MGSLPC_INFO *info, int tcd, struct tty_struct *tty)
918 unsigned char data, status, flag;
921 struct mgsl_icount *icount = &info->icount;
924 /* early termination, get FIFO count from RBCL register */
925 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
927 /* Zero fifo count could mean 0 or 32 bytes available.
928 * If BIT5 of STAR is set then at least 1 byte is available.
930 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
935 tty_buffer_request_room(tty, fifo_count);
936 /* Flush received async data to receive data buffer. */
938 data = read_reg(info, CHA + RXFIFO);
939 status = read_reg(info, CHA + RXFIFO);
945 // if no frameing/crc error then save data
947 // BIT6:framing error
949 if (status & (BIT7 + BIT6)) {
955 /* discard char if tty control flags say so */
956 if (status & info->ignore_status_mask)
959 status &= info->read_status_mask;
963 else if (status & BIT6)
966 work += tty_insert_flip_char(tty, data, flag);
968 issue_command(info, CHA, CMD_RXFIFO);
970 if (debug_level >= DEBUG_LEVEL_ISR) {
971 printk("%s(%d):rx_ready_async",
973 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
974 __FILE__,__LINE__,icount->rx,icount->brk,
975 icount->parity,icount->frame,icount->overrun);
979 tty_flip_buffer_push(tty);
983 static void tx_done(MGSLPC_INFO *info, struct tty_struct *tty)
985 if (!info->tx_active)
988 info->tx_active = false;
989 info->tx_aborting = false;
991 if (info->params.mode == MGSL_MODE_ASYNC)
994 info->tx_count = info->tx_put = info->tx_get = 0;
995 del_timer(&info->tx_timer);
997 if (info->drop_rts_on_tx_done) {
999 if (info->serial_signals & SerialSignal_RTS) {
1000 info->serial_signals &= ~SerialSignal_RTS;
1003 info->drop_rts_on_tx_done = false;
1006 #if SYNCLINK_GENERIC_HDLC
1008 hdlcdev_tx_done(info);
1012 if (tty->stopped || tty->hw_stopped) {
1016 info->pending_bh |= BH_TRANSMIT;
1020 static void tx_ready(MGSLPC_INFO *info, struct tty_struct *tty)
1022 unsigned char fifo_count = 32;
1025 if (debug_level >= DEBUG_LEVEL_ISR)
1026 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1028 if (info->params.mode == MGSL_MODE_HDLC) {
1029 if (!info->tx_active)
1032 if (tty->stopped || tty->hw_stopped) {
1036 if (!info->tx_count)
1037 info->tx_active = false;
1040 if (!info->tx_count)
1043 while (info->tx_count && fifo_count) {
1044 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
1047 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1049 write_reg16(info, CHA + TXFIFO,
1050 *((unsigned short*)(info->tx_buf + info->tx_get)));
1052 info->tx_count -= c;
1053 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1057 if (info->params.mode == MGSL_MODE_ASYNC) {
1058 if (info->tx_count < WAKEUP_CHARS)
1059 info->pending_bh |= BH_TRANSMIT;
1060 issue_command(info, CHA, CMD_TXFIFO);
1063 issue_command(info, CHA, CMD_TXFIFO);
1065 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1069 static void cts_change(MGSLPC_INFO *info, struct tty_struct *tty)
1072 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1073 irq_disable(info, CHB, IRQ_CTS);
1075 if (info->serial_signals & SerialSignal_CTS)
1076 info->input_signal_events.cts_up++;
1078 info->input_signal_events.cts_down++;
1079 wake_up_interruptible(&info->status_event_wait_q);
1080 wake_up_interruptible(&info->event_wait_q);
1082 if (info->port.flags & ASYNC_CTS_FLOW) {
1083 if (tty->hw_stopped) {
1084 if (info->serial_signals & SerialSignal_CTS) {
1085 if (debug_level >= DEBUG_LEVEL_ISR)
1086 printk("CTS tx start...");
1088 tty->hw_stopped = 0;
1089 tx_start(info, tty);
1090 info->pending_bh |= BH_TRANSMIT;
1094 if (!(info->serial_signals & SerialSignal_CTS)) {
1095 if (debug_level >= DEBUG_LEVEL_ISR)
1096 printk("CTS tx stop...");
1098 tty->hw_stopped = 1;
1103 info->pending_bh |= BH_STATUS;
1106 static void dcd_change(MGSLPC_INFO *info, struct tty_struct *tty)
1109 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1110 irq_disable(info, CHB, IRQ_DCD);
1112 if (info->serial_signals & SerialSignal_DCD) {
1113 info->input_signal_events.dcd_up++;
1116 info->input_signal_events.dcd_down++;
1117 #if SYNCLINK_GENERIC_HDLC
1118 if (info->netcount) {
1119 if (info->serial_signals & SerialSignal_DCD)
1120 netif_carrier_on(info->netdev);
1122 netif_carrier_off(info->netdev);
1125 wake_up_interruptible(&info->status_event_wait_q);
1126 wake_up_interruptible(&info->event_wait_q);
1128 if (info->port.flags & ASYNC_CHECK_CD) {
1129 if (debug_level >= DEBUG_LEVEL_ISR)
1130 printk("%s CD now %s...", info->device_name,
1131 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1132 if (info->serial_signals & SerialSignal_DCD)
1133 wake_up_interruptible(&info->port.open_wait);
1135 if (debug_level >= DEBUG_LEVEL_ISR)
1136 printk("doing serial hangup...");
1141 info->pending_bh |= BH_STATUS;
1144 static void dsr_change(MGSLPC_INFO *info)
1147 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1148 port_irq_disable(info, PVR_DSR);
1150 if (info->serial_signals & SerialSignal_DSR)
1151 info->input_signal_events.dsr_up++;
1153 info->input_signal_events.dsr_down++;
1154 wake_up_interruptible(&info->status_event_wait_q);
1155 wake_up_interruptible(&info->event_wait_q);
1156 info->pending_bh |= BH_STATUS;
1159 static void ri_change(MGSLPC_INFO *info)
1162 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1163 port_irq_disable(info, PVR_RI);
1165 if (info->serial_signals & SerialSignal_RI)
1166 info->input_signal_events.ri_up++;
1168 info->input_signal_events.ri_down++;
1169 wake_up_interruptible(&info->status_event_wait_q);
1170 wake_up_interruptible(&info->event_wait_q);
1171 info->pending_bh |= BH_STATUS;
1174 /* Interrupt service routine entry point.
1178 * irq interrupt number that caused interrupt
1179 * dev_id device ID supplied during interrupt registration
1181 static irqreturn_t mgslpc_isr(int dummy, void *dev_id)
1183 MGSLPC_INFO *info = dev_id;
1184 struct tty_struct *tty;
1186 unsigned char gis, pis;
1189 if (debug_level >= DEBUG_LEVEL_ISR)
1190 printk("mgslpc_isr(%d) entry.\n", info->irq_level);
1192 if (!(info->p_dev->_locked))
1195 tty = tty_port_tty_get(&info->port);
1197 spin_lock(&info->lock);
1199 while ((gis = read_reg(info, CHA + GIS))) {
1200 if (debug_level >= DEBUG_LEVEL_ISR)
1201 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1203 if ((gis & 0x70) || count > 1000) {
1204 printk("synclink_cs:hardware failed or ejected\n");
1209 if (gis & (BIT1 + BIT0)) {
1210 isr = read_reg16(info, CHB + ISR);
1212 dcd_change(info, tty);
1214 cts_change(info, tty);
1216 if (gis & (BIT3 + BIT2))
1218 isr = read_reg16(info, CHA + ISR);
1219 if (isr & IRQ_TIMER) {
1220 info->irq_occurred = true;
1221 irq_disable(info, CHA, IRQ_TIMER);
1225 if (isr & IRQ_EXITHUNT) {
1226 info->icount.exithunt++;
1227 wake_up_interruptible(&info->event_wait_q);
1229 if (isr & IRQ_BREAK_ON) {
1231 if (info->port.flags & ASYNC_SAK)
1234 if (isr & IRQ_RXTIME) {
1235 issue_command(info, CHA, CMD_RXFIFO_READ);
1237 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1238 if (info->params.mode == MGSL_MODE_HDLC)
1239 rx_ready_hdlc(info, isr & IRQ_RXEOM);
1241 rx_ready_async(info, isr & IRQ_RXEOM, tty);
1245 if (isr & IRQ_UNDERRUN) {
1246 if (info->tx_aborting)
1247 info->icount.txabort++;
1249 info->icount.txunder++;
1252 else if (isr & IRQ_ALLSENT) {
1253 info->icount.txok++;
1256 else if (isr & IRQ_TXFIFO)
1257 tx_ready(info, tty);
1260 pis = read_reg(info, CHA + PIS);
1268 /* Request bottom half processing if there's something
1269 * for it to do and the bh is not already running
1272 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1273 if ( debug_level >= DEBUG_LEVEL_ISR )
1274 printk("%s(%d):%s queueing bh task.\n",
1275 __FILE__,__LINE__,info->device_name);
1276 schedule_work(&info->task);
1277 info->bh_requested = true;
1280 spin_unlock(&info->lock);
1283 if (debug_level >= DEBUG_LEVEL_ISR)
1284 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1285 __FILE__, __LINE__, info->irq_level);
1290 /* Initialize and start device.
1292 static int startup(MGSLPC_INFO * info, struct tty_struct *tty)
1296 if (debug_level >= DEBUG_LEVEL_INFO)
1297 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1299 if (info->port.flags & ASYNC_INITIALIZED)
1302 if (!info->tx_buf) {
1303 /* allocate a page of memory for a transmit buffer */
1304 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1305 if (!info->tx_buf) {
1306 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1307 __FILE__,__LINE__,info->device_name);
1312 info->pending_bh = 0;
1314 memset(&info->icount, 0, sizeof(info->icount));
1316 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
1318 /* Allocate and claim adapter resources */
1319 retval = claim_resources(info);
1321 /* perform existance check and diagnostics */
1323 retval = adapter_test(info);
1326 if (capable(CAP_SYS_ADMIN) && tty)
1327 set_bit(TTY_IO_ERROR, &tty->flags);
1328 release_resources(info);
1332 /* program hardware for current parameters */
1333 mgslpc_change_params(info, tty);
1336 clear_bit(TTY_IO_ERROR, &tty->flags);
1338 info->port.flags |= ASYNC_INITIALIZED;
1343 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1345 static void shutdown(MGSLPC_INFO * info, struct tty_struct *tty)
1347 unsigned long flags;
1349 if (!(info->port.flags & ASYNC_INITIALIZED))
1352 if (debug_level >= DEBUG_LEVEL_INFO)
1353 printk("%s(%d):mgslpc_shutdown(%s)\n",
1354 __FILE__,__LINE__, info->device_name );
1356 /* clear status wait queue because status changes */
1357 /* can't happen after shutting down the hardware */
1358 wake_up_interruptible(&info->status_event_wait_q);
1359 wake_up_interruptible(&info->event_wait_q);
1361 del_timer_sync(&info->tx_timer);
1364 free_page((unsigned long) info->tx_buf);
1365 info->tx_buf = NULL;
1368 spin_lock_irqsave(&info->lock,flags);
1373 /* TODO:disable interrupts instead of reset to preserve signal states */
1376 if (!tty || tty->termios->c_cflag & HUPCL) {
1377 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1381 spin_unlock_irqrestore(&info->lock,flags);
1383 release_resources(info);
1386 set_bit(TTY_IO_ERROR, &tty->flags);
1388 info->port.flags &= ~ASYNC_INITIALIZED;
1391 static void mgslpc_program_hw(MGSLPC_INFO *info, struct tty_struct *tty)
1393 unsigned long flags;
1395 spin_lock_irqsave(&info->lock,flags);
1399 info->tx_count = info->tx_put = info->tx_get = 0;
1401 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1408 info->dcd_chkcount = 0;
1409 info->cts_chkcount = 0;
1410 info->ri_chkcount = 0;
1411 info->dsr_chkcount = 0;
1413 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1414 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1417 if (info->netcount || (tty && (tty->termios->c_cflag & CREAD)))
1420 spin_unlock_irqrestore(&info->lock,flags);
1423 /* Reconfigure adapter based on new parameters
1425 static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty)
1430 if (!tty || !tty->termios)
1433 if (debug_level >= DEBUG_LEVEL_INFO)
1434 printk("%s(%d):mgslpc_change_params(%s)\n",
1435 __FILE__,__LINE__, info->device_name );
1437 cflag = tty->termios->c_cflag;
1439 /* if B0 rate (hangup) specified then negate DTR and RTS */
1440 /* otherwise assert DTR and RTS */
1442 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1444 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1446 /* byte size and parity */
1448 switch (cflag & CSIZE) {
1449 case CS5: info->params.data_bits = 5; break;
1450 case CS6: info->params.data_bits = 6; break;
1451 case CS7: info->params.data_bits = 7; break;
1452 case CS8: info->params.data_bits = 8; break;
1453 default: info->params.data_bits = 7; break;
1457 info->params.stop_bits = 2;
1459 info->params.stop_bits = 1;
1461 info->params.parity = ASYNC_PARITY_NONE;
1462 if (cflag & PARENB) {
1464 info->params.parity = ASYNC_PARITY_ODD;
1466 info->params.parity = ASYNC_PARITY_EVEN;
1469 info->params.parity = ASYNC_PARITY_SPACE;
1473 /* calculate number of jiffies to transmit a full
1474 * FIFO (32 bytes) at specified data rate
1476 bits_per_char = info->params.data_bits +
1477 info->params.stop_bits + 1;
1479 /* if port data rate is set to 460800 or less then
1480 * allow tty settings to override, otherwise keep the
1481 * current data rate.
1483 if (info->params.data_rate <= 460800) {
1484 info->params.data_rate = tty_get_baud_rate(tty);
1487 if ( info->params.data_rate ) {
1488 info->timeout = (32*HZ*bits_per_char) /
1489 info->params.data_rate;
1491 info->timeout += HZ/50; /* Add .02 seconds of slop */
1493 if (cflag & CRTSCTS)
1494 info->port.flags |= ASYNC_CTS_FLOW;
1496 info->port.flags &= ~ASYNC_CTS_FLOW;
1499 info->port.flags &= ~ASYNC_CHECK_CD;
1501 info->port.flags |= ASYNC_CHECK_CD;
1503 /* process tty input control flags */
1505 info->read_status_mask = 0;
1507 info->read_status_mask |= BIT7 | BIT6;
1509 info->ignore_status_mask |= BIT7 | BIT6;
1511 mgslpc_program_hw(info, tty);
1514 /* Add a character to the transmit buffer
1516 static int mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1518 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1519 unsigned long flags;
1521 if (debug_level >= DEBUG_LEVEL_INFO) {
1522 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1523 __FILE__,__LINE__,ch,info->device_name);
1526 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1532 spin_lock_irqsave(&info->lock,flags);
1534 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1535 if (info->tx_count < TXBUFSIZE - 1) {
1536 info->tx_buf[info->tx_put++] = ch;
1537 info->tx_put &= TXBUFSIZE-1;
1542 spin_unlock_irqrestore(&info->lock,flags);
1546 /* Enable transmitter so remaining characters in the
1547 * transmit buffer are sent.
1549 static void mgslpc_flush_chars(struct tty_struct *tty)
1551 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1552 unsigned long flags;
1554 if (debug_level >= DEBUG_LEVEL_INFO)
1555 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1556 __FILE__,__LINE__,info->device_name,info->tx_count);
1558 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1561 if (info->tx_count <= 0 || tty->stopped ||
1562 tty->hw_stopped || !info->tx_buf)
1565 if (debug_level >= DEBUG_LEVEL_INFO)
1566 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1567 __FILE__,__LINE__,info->device_name);
1569 spin_lock_irqsave(&info->lock,flags);
1570 if (!info->tx_active)
1571 tx_start(info, tty);
1572 spin_unlock_irqrestore(&info->lock,flags);
1575 /* Send a block of data
1579 * tty pointer to tty information structure
1580 * buf pointer to buffer containing send data
1581 * count size of send data in bytes
1583 * Returns: number of characters written
1585 static int mgslpc_write(struct tty_struct * tty,
1586 const unsigned char *buf, int count)
1589 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1590 unsigned long flags;
1592 if (debug_level >= DEBUG_LEVEL_INFO)
1593 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1594 __FILE__,__LINE__,info->device_name,count);
1596 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1600 if (info->params.mode == MGSL_MODE_HDLC) {
1601 if (count > TXBUFSIZE) {
1605 if (info->tx_active)
1607 else if (info->tx_count)
1613 min(TXBUFSIZE - info->tx_count - 1,
1614 TXBUFSIZE - info->tx_put));
1618 memcpy(info->tx_buf + info->tx_put, buf, c);
1620 spin_lock_irqsave(&info->lock,flags);
1621 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1622 info->tx_count += c;
1623 spin_unlock_irqrestore(&info->lock,flags);
1630 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1631 spin_lock_irqsave(&info->lock,flags);
1632 if (!info->tx_active)
1633 tx_start(info, tty);
1634 spin_unlock_irqrestore(&info->lock,flags);
1637 if (debug_level >= DEBUG_LEVEL_INFO)
1638 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1639 __FILE__,__LINE__,info->device_name,ret);
1643 /* Return the count of free bytes in transmit buffer
1645 static int mgslpc_write_room(struct tty_struct *tty)
1647 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1650 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1653 if (info->params.mode == MGSL_MODE_HDLC) {
1654 /* HDLC (frame oriented) mode */
1655 if (info->tx_active)
1658 return HDLC_MAX_FRAME_SIZE;
1660 ret = TXBUFSIZE - info->tx_count - 1;
1665 if (debug_level >= DEBUG_LEVEL_INFO)
1666 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1667 __FILE__,__LINE__, info->device_name, ret);
1671 /* Return the count of bytes in transmit buffer
1673 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1675 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1678 if (debug_level >= DEBUG_LEVEL_INFO)
1679 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1680 __FILE__,__LINE__, info->device_name );
1682 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1685 if (info->params.mode == MGSL_MODE_HDLC)
1686 rc = info->tx_active ? info->max_frame_size : 0;
1688 rc = info->tx_count;
1690 if (debug_level >= DEBUG_LEVEL_INFO)
1691 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1692 __FILE__,__LINE__, info->device_name, rc);
1697 /* Discard all data in the send buffer
1699 static void mgslpc_flush_buffer(struct tty_struct *tty)
1701 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1702 unsigned long flags;
1704 if (debug_level >= DEBUG_LEVEL_INFO)
1705 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1706 __FILE__,__LINE__, info->device_name );
1708 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1711 spin_lock_irqsave(&info->lock,flags);
1712 info->tx_count = info->tx_put = info->tx_get = 0;
1713 del_timer(&info->tx_timer);
1714 spin_unlock_irqrestore(&info->lock,flags);
1716 wake_up_interruptible(&tty->write_wait);
1720 /* Send a high-priority XON/XOFF character
1722 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1724 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1725 unsigned long flags;
1727 if (debug_level >= DEBUG_LEVEL_INFO)
1728 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1729 __FILE__,__LINE__, info->device_name, ch );
1731 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1736 spin_lock_irqsave(&info->lock,flags);
1737 if (!info->tx_enabled)
1738 tx_start(info, tty);
1739 spin_unlock_irqrestore(&info->lock,flags);
1743 /* Signal remote device to throttle send data (our receive data)
1745 static void mgslpc_throttle(struct tty_struct * tty)
1747 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1748 unsigned long flags;
1750 if (debug_level >= DEBUG_LEVEL_INFO)
1751 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1752 __FILE__,__LINE__, info->device_name );
1754 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1758 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1760 if (tty->termios->c_cflag & CRTSCTS) {
1761 spin_lock_irqsave(&info->lock,flags);
1762 info->serial_signals &= ~SerialSignal_RTS;
1764 spin_unlock_irqrestore(&info->lock,flags);
1768 /* Signal remote device to stop throttling send data (our receive data)
1770 static void mgslpc_unthrottle(struct tty_struct * tty)
1772 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1773 unsigned long flags;
1775 if (debug_level >= DEBUG_LEVEL_INFO)
1776 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1777 __FILE__,__LINE__, info->device_name );
1779 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1786 mgslpc_send_xchar(tty, START_CHAR(tty));
1789 if (tty->termios->c_cflag & CRTSCTS) {
1790 spin_lock_irqsave(&info->lock,flags);
1791 info->serial_signals |= SerialSignal_RTS;
1793 spin_unlock_irqrestore(&info->lock,flags);
1797 /* get the current serial statistics
1799 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1802 if (debug_level >= DEBUG_LEVEL_INFO)
1803 printk("get_params(%s)\n", info->device_name);
1805 memset(&info->icount, 0, sizeof(info->icount));
1807 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1814 /* get the current serial parameters
1816 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1819 if (debug_level >= DEBUG_LEVEL_INFO)
1820 printk("get_params(%s)\n", info->device_name);
1821 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1827 /* set the serial parameters
1831 * info pointer to device instance data
1832 * new_params user buffer containing new serial params
1834 * Returns: 0 if success, otherwise error code
1836 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params, struct tty_struct *tty)
1838 unsigned long flags;
1839 MGSL_PARAMS tmp_params;
1842 if (debug_level >= DEBUG_LEVEL_INFO)
1843 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1844 info->device_name );
1845 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1847 if ( debug_level >= DEBUG_LEVEL_INFO )
1848 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1849 __FILE__,__LINE__,info->device_name);
1853 spin_lock_irqsave(&info->lock,flags);
1854 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1855 spin_unlock_irqrestore(&info->lock,flags);
1857 mgslpc_change_params(info, tty);
1862 static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1865 if (debug_level >= DEBUG_LEVEL_INFO)
1866 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1867 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1873 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1875 unsigned long flags;
1876 if (debug_level >= DEBUG_LEVEL_INFO)
1877 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1878 spin_lock_irqsave(&info->lock,flags);
1879 info->idle_mode = idle_mode;
1881 spin_unlock_irqrestore(&info->lock,flags);
1885 static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1888 if (debug_level >= DEBUG_LEVEL_INFO)
1889 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1890 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1896 static int set_interface(MGSLPC_INFO * info, int if_mode)
1898 unsigned long flags;
1900 if (debug_level >= DEBUG_LEVEL_INFO)
1901 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1902 spin_lock_irqsave(&info->lock,flags);
1903 info->if_mode = if_mode;
1905 val = read_reg(info, PVR) & 0x0f;
1906 switch (info->if_mode)
1908 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
1909 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
1910 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
1912 write_reg(info, PVR, val);
1914 spin_unlock_irqrestore(&info->lock,flags);
1918 static int set_txenable(MGSLPC_INFO * info, int enable, struct tty_struct *tty)
1920 unsigned long flags;
1922 if (debug_level >= DEBUG_LEVEL_INFO)
1923 printk("set_txenable(%s,%d)\n", info->device_name, enable);
1925 spin_lock_irqsave(&info->lock,flags);
1927 if (!info->tx_enabled)
1928 tx_start(info, tty);
1930 if (info->tx_enabled)
1933 spin_unlock_irqrestore(&info->lock,flags);
1937 static int tx_abort(MGSLPC_INFO * info)
1939 unsigned long flags;
1941 if (debug_level >= DEBUG_LEVEL_INFO)
1942 printk("tx_abort(%s)\n", info->device_name);
1944 spin_lock_irqsave(&info->lock,flags);
1945 if (info->tx_active && info->tx_count &&
1946 info->params.mode == MGSL_MODE_HDLC) {
1947 /* clear data count so FIFO is not filled on next IRQ.
1948 * This results in underrun and abort transmission.
1950 info->tx_count = info->tx_put = info->tx_get = 0;
1951 info->tx_aborting = true;
1953 spin_unlock_irqrestore(&info->lock,flags);
1957 static int set_rxenable(MGSLPC_INFO * info, int enable)
1959 unsigned long flags;
1961 if (debug_level >= DEBUG_LEVEL_INFO)
1962 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
1964 spin_lock_irqsave(&info->lock,flags);
1966 if (!info->rx_enabled)
1969 if (info->rx_enabled)
1972 spin_unlock_irqrestore(&info->lock,flags);
1976 /* wait for specified event to occur
1978 * Arguments: info pointer to device instance data
1979 * mask pointer to bitmask of events to wait for
1980 * Return Value: 0 if successful and bit mask updated with
1981 * of events triggerred,
1982 * otherwise error code
1984 static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
1986 unsigned long flags;
1989 struct mgsl_icount cprev, cnow;
1992 struct _input_signal_events oldsigs, newsigs;
1993 DECLARE_WAITQUEUE(wait, current);
1995 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
1999 if (debug_level >= DEBUG_LEVEL_INFO)
2000 printk("wait_events(%s,%d)\n", info->device_name, mask);
2002 spin_lock_irqsave(&info->lock,flags);
2004 /* return immediately if state matches requested events */
2006 s = info->serial_signals;
2008 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2009 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2010 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2011 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2013 spin_unlock_irqrestore(&info->lock,flags);
2017 /* save current irq counts */
2018 cprev = info->icount;
2019 oldsigs = info->input_signal_events;
2021 if ((info->params.mode == MGSL_MODE_HDLC) &&
2022 (mask & MgslEvent_ExitHuntMode))
2023 irq_enable(info, CHA, IRQ_EXITHUNT);
2025 set_current_state(TASK_INTERRUPTIBLE);
2026 add_wait_queue(&info->event_wait_q, &wait);
2028 spin_unlock_irqrestore(&info->lock,flags);
2033 if (signal_pending(current)) {
2038 /* get current irq counts */
2039 spin_lock_irqsave(&info->lock,flags);
2040 cnow = info->icount;
2041 newsigs = info->input_signal_events;
2042 set_current_state(TASK_INTERRUPTIBLE);
2043 spin_unlock_irqrestore(&info->lock,flags);
2045 /* if no change, wait aborted for some reason */
2046 if (newsigs.dsr_up == oldsigs.dsr_up &&
2047 newsigs.dsr_down == oldsigs.dsr_down &&
2048 newsigs.dcd_up == oldsigs.dcd_up &&
2049 newsigs.dcd_down == oldsigs.dcd_down &&
2050 newsigs.cts_up == oldsigs.cts_up &&
2051 newsigs.cts_down == oldsigs.cts_down &&
2052 newsigs.ri_up == oldsigs.ri_up &&
2053 newsigs.ri_down == oldsigs.ri_down &&
2054 cnow.exithunt == cprev.exithunt &&
2055 cnow.rxidle == cprev.rxidle) {
2061 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2062 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2063 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2064 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2065 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2066 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2067 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2068 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2069 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2070 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2078 remove_wait_queue(&info->event_wait_q, &wait);
2079 set_current_state(TASK_RUNNING);
2081 if (mask & MgslEvent_ExitHuntMode) {
2082 spin_lock_irqsave(&info->lock,flags);
2083 if (!waitqueue_active(&info->event_wait_q))
2084 irq_disable(info, CHA, IRQ_EXITHUNT);
2085 spin_unlock_irqrestore(&info->lock,flags);
2089 PUT_USER(rc, events, mask_ptr);
2093 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2095 unsigned long flags;
2097 struct mgsl_icount cprev, cnow;
2098 DECLARE_WAITQUEUE(wait, current);
2100 /* save current irq counts */
2101 spin_lock_irqsave(&info->lock,flags);
2102 cprev = info->icount;
2103 add_wait_queue(&info->status_event_wait_q, &wait);
2104 set_current_state(TASK_INTERRUPTIBLE);
2105 spin_unlock_irqrestore(&info->lock,flags);
2109 if (signal_pending(current)) {
2114 /* get new irq counts */
2115 spin_lock_irqsave(&info->lock,flags);
2116 cnow = info->icount;
2117 set_current_state(TASK_INTERRUPTIBLE);
2118 spin_unlock_irqrestore(&info->lock,flags);
2120 /* if no change, wait aborted for some reason */
2121 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2122 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2127 /* check for change in caller specified modem input */
2128 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2129 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2130 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2131 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2138 remove_wait_queue(&info->status_event_wait_q, &wait);
2139 set_current_state(TASK_RUNNING);
2143 /* return the state of the serial control and status signals
2145 static int tiocmget(struct tty_struct *tty, struct file *file)
2147 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2148 unsigned int result;
2149 unsigned long flags;
2151 spin_lock_irqsave(&info->lock,flags);
2153 spin_unlock_irqrestore(&info->lock,flags);
2155 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2156 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2157 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2158 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2159 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2160 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2162 if (debug_level >= DEBUG_LEVEL_INFO)
2163 printk("%s(%d):%s tiocmget() value=%08X\n",
2164 __FILE__,__LINE__, info->device_name, result );
2168 /* set modem control signals (DTR/RTS)
2170 static int tiocmset(struct tty_struct *tty, struct file *file,
2171 unsigned int set, unsigned int clear)
2173 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2174 unsigned long flags;
2176 if (debug_level >= DEBUG_LEVEL_INFO)
2177 printk("%s(%d):%s tiocmset(%x,%x)\n",
2178 __FILE__,__LINE__,info->device_name, set, clear);
2180 if (set & TIOCM_RTS)
2181 info->serial_signals |= SerialSignal_RTS;
2182 if (set & TIOCM_DTR)
2183 info->serial_signals |= SerialSignal_DTR;
2184 if (clear & TIOCM_RTS)
2185 info->serial_signals &= ~SerialSignal_RTS;
2186 if (clear & TIOCM_DTR)
2187 info->serial_signals &= ~SerialSignal_DTR;
2189 spin_lock_irqsave(&info->lock,flags);
2191 spin_unlock_irqrestore(&info->lock,flags);
2196 /* Set or clear transmit break condition
2198 * Arguments: tty pointer to tty instance data
2199 * break_state -1=set break condition, 0=clear
2201 static int mgslpc_break(struct tty_struct *tty, int break_state)
2203 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2204 unsigned long flags;
2206 if (debug_level >= DEBUG_LEVEL_INFO)
2207 printk("%s(%d):mgslpc_break(%s,%d)\n",
2208 __FILE__,__LINE__, info->device_name, break_state);
2210 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2213 spin_lock_irqsave(&info->lock,flags);
2214 if (break_state == -1)
2215 set_reg_bits(info, CHA+DAFO, BIT6);
2217 clear_reg_bits(info, CHA+DAFO, BIT6);
2218 spin_unlock_irqrestore(&info->lock,flags);
2222 /* Service an IOCTL request
2226 * tty pointer to tty instance data
2227 * file pointer to associated file object for device
2228 * cmd IOCTL command code
2229 * arg command argument/context
2231 * Return Value: 0 if success, otherwise error code
2233 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2234 unsigned int cmd, unsigned long arg)
2236 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2238 struct mgsl_icount cnow; /* kernel counter temps */
2239 struct serial_icounter_struct __user *p_cuser; /* user space */
2240 void __user *argp = (void __user *)arg;
2241 unsigned long flags;
2243 if (debug_level >= DEBUG_LEVEL_INFO)
2244 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2245 info->device_name, cmd );
2247 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2250 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2251 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2252 if (tty->flags & (1 << TTY_IO_ERROR))
2257 case MGSL_IOCGPARAMS:
2258 return get_params(info, argp);
2259 case MGSL_IOCSPARAMS:
2260 return set_params(info, argp, tty);
2261 case MGSL_IOCGTXIDLE:
2262 return get_txidle(info, argp);
2263 case MGSL_IOCSTXIDLE:
2264 return set_txidle(info, (int)arg);
2266 return get_interface(info, argp);
2268 return set_interface(info,(int)arg);
2269 case MGSL_IOCTXENABLE:
2270 return set_txenable(info,(int)arg, tty);
2271 case MGSL_IOCRXENABLE:
2272 return set_rxenable(info,(int)arg);
2273 case MGSL_IOCTXABORT:
2274 return tx_abort(info);
2275 case MGSL_IOCGSTATS:
2276 return get_stats(info, argp);
2277 case MGSL_IOCWAITEVENT:
2278 return wait_events(info, argp);
2280 return modem_input_wait(info,(int)arg);
2282 spin_lock_irqsave(&info->lock,flags);
2283 cnow = info->icount;
2284 spin_unlock_irqrestore(&info->lock,flags);
2286 PUT_USER(error,cnow.cts, &p_cuser->cts);
2287 if (error) return error;
2288 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2289 if (error) return error;
2290 PUT_USER(error,cnow.rng, &p_cuser->rng);
2291 if (error) return error;
2292 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2293 if (error) return error;
2294 PUT_USER(error,cnow.rx, &p_cuser->rx);
2295 if (error) return error;
2296 PUT_USER(error,cnow.tx, &p_cuser->tx);
2297 if (error) return error;
2298 PUT_USER(error,cnow.frame, &p_cuser->frame);
2299 if (error) return error;
2300 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2301 if (error) return error;
2302 PUT_USER(error,cnow.parity, &p_cuser->parity);
2303 if (error) return error;
2304 PUT_USER(error,cnow.brk, &p_cuser->brk);
2305 if (error) return error;
2306 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2307 if (error) return error;
2310 return -ENOIOCTLCMD;
2315 /* Set new termios settings
2319 * tty pointer to tty structure
2320 * termios pointer to buffer to hold returned old termios
2322 static void mgslpc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2324 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2325 unsigned long flags;
2327 if (debug_level >= DEBUG_LEVEL_INFO)
2328 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2329 tty->driver->name );
2331 /* just return if nothing has changed */
2332 if ((tty->termios->c_cflag == old_termios->c_cflag)
2333 && (RELEVANT_IFLAG(tty->termios->c_iflag)
2334 == RELEVANT_IFLAG(old_termios->c_iflag)))
2337 mgslpc_change_params(info, tty);
2339 /* Handle transition to B0 status */
2340 if (old_termios->c_cflag & CBAUD &&
2341 !(tty->termios->c_cflag & CBAUD)) {
2342 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2343 spin_lock_irqsave(&info->lock,flags);
2345 spin_unlock_irqrestore(&info->lock,flags);
2348 /* Handle transition away from B0 status */
2349 if (!(old_termios->c_cflag & CBAUD) &&
2350 tty->termios->c_cflag & CBAUD) {
2351 info->serial_signals |= SerialSignal_DTR;
2352 if (!(tty->termios->c_cflag & CRTSCTS) ||
2353 !test_bit(TTY_THROTTLED, &tty->flags)) {
2354 info->serial_signals |= SerialSignal_RTS;
2356 spin_lock_irqsave(&info->lock,flags);
2358 spin_unlock_irqrestore(&info->lock,flags);
2361 /* Handle turning off CRTSCTS */
2362 if (old_termios->c_cflag & CRTSCTS &&
2363 !(tty->termios->c_cflag & CRTSCTS)) {
2364 tty->hw_stopped = 0;
2369 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2371 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2372 struct tty_port *port = &info->port;
2374 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2377 if (debug_level >= DEBUG_LEVEL_INFO)
2378 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2379 __FILE__,__LINE__, info->device_name, port->count);
2381 WARN_ON(!port->count);
2383 if (tty_port_close_start(port, tty, filp) == 0)
2386 if (port->flags & ASYNC_INITIALIZED)
2387 mgslpc_wait_until_sent(tty, info->timeout);
2389 mgslpc_flush_buffer(tty);
2391 tty_ldisc_flush(tty);
2392 shutdown(info, tty);
2394 tty_port_close_end(port, tty);
2395 tty_port_tty_set(port, NULL);
2397 if (debug_level >= DEBUG_LEVEL_INFO)
2398 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2399 tty->driver->name, port->count);
2402 /* Wait until the transmitter is empty.
2404 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2406 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2407 unsigned long orig_jiffies, char_time;
2412 if (debug_level >= DEBUG_LEVEL_INFO)
2413 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2414 __FILE__,__LINE__, info->device_name );
2416 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2419 if (!(info->port.flags & ASYNC_INITIALIZED))
2422 orig_jiffies = jiffies;
2424 /* Set check interval to 1/5 of estimated time to
2425 * send a character, and make it at least 1. The check
2426 * interval should also be less than the timeout.
2427 * Note: use tight timings here to satisfy the NIST-PCTS.
2430 if ( info->params.data_rate ) {
2431 char_time = info->timeout/(32 * 5);
2438 char_time = min_t(unsigned long, char_time, timeout);
2440 if (info->params.mode == MGSL_MODE_HDLC) {
2441 while (info->tx_active) {
2442 msleep_interruptible(jiffies_to_msecs(char_time));
2443 if (signal_pending(current))
2445 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2449 while ((info->tx_count || info->tx_active) &&
2451 msleep_interruptible(jiffies_to_msecs(char_time));
2452 if (signal_pending(current))
2454 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2460 if (debug_level >= DEBUG_LEVEL_INFO)
2461 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2462 __FILE__,__LINE__, info->device_name );
2465 /* Called by tty_hangup() when a hangup is signaled.
2466 * This is the same as closing all open files for the port.
2468 static void mgslpc_hangup(struct tty_struct *tty)
2470 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2472 if (debug_level >= DEBUG_LEVEL_INFO)
2473 printk("%s(%d):mgslpc_hangup(%s)\n",
2474 __FILE__,__LINE__, info->device_name );
2476 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2479 mgslpc_flush_buffer(tty);
2480 shutdown(info, tty);
2481 tty_port_hangup(&info->port);
2484 static int carrier_raised(struct tty_port *port)
2486 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2487 unsigned long flags;
2489 spin_lock_irqsave(&info->lock,flags);
2491 spin_unlock_irqrestore(&info->lock,flags);
2493 if (info->serial_signals & SerialSignal_DCD)
2498 static void dtr_rts(struct tty_port *port, int onoff)
2500 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2501 unsigned long flags;
2503 spin_lock_irqsave(&info->lock,flags);
2505 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2507 info->serial_signals &= ~SerialSignal_RTS + SerialSignal_DTR;
2509 spin_unlock_irqrestore(&info->lock,flags);
2513 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2516 struct tty_port *port;
2518 unsigned long flags;
2520 /* verify range of specified line number */
2522 if ((line < 0) || (line >= mgslpc_device_count)) {
2523 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2524 __FILE__,__LINE__,line);
2528 /* find the info structure for the specified line */
2529 info = mgslpc_device_list;
2530 while(info && info->line != line)
2531 info = info->next_device;
2532 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2536 tty->driver_data = info;
2537 tty_port_tty_set(port, tty);
2539 if (debug_level >= DEBUG_LEVEL_INFO)
2540 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2541 __FILE__,__LINE__,tty->driver->name, port->count);
2543 /* If port is closing, signal caller to try again */
2544 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING){
2545 if (port->flags & ASYNC_CLOSING)
2546 interruptible_sleep_on(&port->close_wait);
2547 retval = ((port->flags & ASYNC_HUP_NOTIFY) ?
2548 -EAGAIN : -ERESTARTSYS);
2552 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2554 spin_lock_irqsave(&info->netlock, flags);
2555 if (info->netcount) {
2557 spin_unlock_irqrestore(&info->netlock, flags);
2560 spin_lock(&port->lock);
2562 spin_unlock(&port->lock);
2563 spin_unlock_irqrestore(&info->netlock, flags);
2565 if (port->count == 1) {
2566 /* 1st open on this device, init hardware */
2567 retval = startup(info, tty);
2572 retval = tty_port_block_til_ready(&info->port, tty, filp);
2574 if (debug_level >= DEBUG_LEVEL_INFO)
2575 printk("%s(%d):block_til_ready(%s) returned %d\n",
2576 __FILE__,__LINE__, info->device_name, retval);
2580 if (debug_level >= DEBUG_LEVEL_INFO)
2581 printk("%s(%d):mgslpc_open(%s) success\n",
2582 __FILE__,__LINE__, info->device_name);
2590 * /proc fs routines....
2593 static inline void line_info(struct seq_file *m, MGSLPC_INFO *info)
2596 unsigned long flags;
2598 seq_printf(m, "%s:io:%04X irq:%d",
2599 info->device_name, info->io_base, info->irq_level);
2601 /* output current serial signal states */
2602 spin_lock_irqsave(&info->lock,flags);
2604 spin_unlock_irqrestore(&info->lock,flags);
2608 if (info->serial_signals & SerialSignal_RTS)
2609 strcat(stat_buf, "|RTS");
2610 if (info->serial_signals & SerialSignal_CTS)
2611 strcat(stat_buf, "|CTS");
2612 if (info->serial_signals & SerialSignal_DTR)
2613 strcat(stat_buf, "|DTR");
2614 if (info->serial_signals & SerialSignal_DSR)
2615 strcat(stat_buf, "|DSR");
2616 if (info->serial_signals & SerialSignal_DCD)
2617 strcat(stat_buf, "|CD");
2618 if (info->serial_signals & SerialSignal_RI)
2619 strcat(stat_buf, "|RI");
2621 if (info->params.mode == MGSL_MODE_HDLC) {
2622 seq_printf(m, " HDLC txok:%d rxok:%d",
2623 info->icount.txok, info->icount.rxok);
2624 if (info->icount.txunder)
2625 seq_printf(m, " txunder:%d", info->icount.txunder);
2626 if (info->icount.txabort)
2627 seq_printf(m, " txabort:%d", info->icount.txabort);
2628 if (info->icount.rxshort)
2629 seq_printf(m, " rxshort:%d", info->icount.rxshort);
2630 if (info->icount.rxlong)
2631 seq_printf(m, " rxlong:%d", info->icount.rxlong);
2632 if (info->icount.rxover)
2633 seq_printf(m, " rxover:%d", info->icount.rxover);
2634 if (info->icount.rxcrc)
2635 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
2637 seq_printf(m, " ASYNC tx:%d rx:%d",
2638 info->icount.tx, info->icount.rx);
2639 if (info->icount.frame)
2640 seq_printf(m, " fe:%d", info->icount.frame);
2641 if (info->icount.parity)
2642 seq_printf(m, " pe:%d", info->icount.parity);
2643 if (info->icount.brk)
2644 seq_printf(m, " brk:%d", info->icount.brk);
2645 if (info->icount.overrun)
2646 seq_printf(m, " oe:%d", info->icount.overrun);
2649 /* Append serial signal status to end */
2650 seq_printf(m, " %s\n", stat_buf+1);
2652 seq_printf(m, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2653 info->tx_active,info->bh_requested,info->bh_running,
2657 /* Called to print information about devices
2659 static int mgslpc_proc_show(struct seq_file *m, void *v)
2663 seq_printf(m, "synclink driver:%s\n", driver_version);
2665 info = mgslpc_device_list;
2668 info = info->next_device;
2673 static int mgslpc_proc_open(struct inode *inode, struct file *file)
2675 return single_open(file, mgslpc_proc_show, NULL);
2678 static const struct file_operations mgslpc_proc_fops = {
2679 .owner = THIS_MODULE,
2680 .open = mgslpc_proc_open,
2682 .llseek = seq_lseek,
2683 .release = single_release,
2686 static int rx_alloc_buffers(MGSLPC_INFO *info)
2688 /* each buffer has header and data */
2689 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2691 /* calculate total allocation size for 8 buffers */
2692 info->rx_buf_total_size = info->rx_buf_size * 8;
2694 /* limit total allocated memory */
2695 if (info->rx_buf_total_size > 0x10000)
2696 info->rx_buf_total_size = 0x10000;
2698 /* calculate number of buffers */
2699 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2701 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2702 if (info->rx_buf == NULL)
2705 rx_reset_buffers(info);
2709 static void rx_free_buffers(MGSLPC_INFO *info)
2711 kfree(info->rx_buf);
2712 info->rx_buf = NULL;
2715 static int claim_resources(MGSLPC_INFO *info)
2717 if (rx_alloc_buffers(info) < 0 ) {
2718 printk( "Cant allocate rx buffer %s\n", info->device_name);
2719 release_resources(info);
2725 static void release_resources(MGSLPC_INFO *info)
2727 if (debug_level >= DEBUG_LEVEL_INFO)
2728 printk("release_resources(%s)\n", info->device_name);
2729 rx_free_buffers(info);
2732 /* Add the specified device instance data structure to the
2733 * global linked list of devices and increment the device count.
2735 * Arguments: info pointer to device instance data
2737 static void mgslpc_add_device(MGSLPC_INFO *info)
2739 info->next_device = NULL;
2740 info->line = mgslpc_device_count;
2741 sprintf(info->device_name,"ttySLP%d",info->line);
2743 if (info->line < MAX_DEVICE_COUNT) {
2744 if (maxframe[info->line])
2745 info->max_frame_size = maxframe[info->line];
2748 mgslpc_device_count++;
2750 if (!mgslpc_device_list)
2751 mgslpc_device_list = info;
2753 MGSLPC_INFO *current_dev = mgslpc_device_list;
2754 while( current_dev->next_device )
2755 current_dev = current_dev->next_device;
2756 current_dev->next_device = info;
2759 if (info->max_frame_size < 4096)
2760 info->max_frame_size = 4096;
2761 else if (info->max_frame_size > 65535)
2762 info->max_frame_size = 65535;
2764 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2765 info->device_name, info->io_base, info->irq_level);
2767 #if SYNCLINK_GENERIC_HDLC
2772 static void mgslpc_remove_device(MGSLPC_INFO *remove_info)
2774 MGSLPC_INFO *info = mgslpc_device_list;
2775 MGSLPC_INFO *last = NULL;
2778 if (info == remove_info) {
2780 last->next_device = info->next_device;
2782 mgslpc_device_list = info->next_device;
2783 #if SYNCLINK_GENERIC_HDLC
2786 release_resources(info);
2788 mgslpc_device_count--;
2792 info = info->next_device;
2796 static struct pcmcia_device_id mgslpc_ids[] = {
2797 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
2800 MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
2802 static struct pcmcia_driver mgslpc_driver = {
2803 .owner = THIS_MODULE,
2805 .name = "synclink_cs",
2807 .probe = mgslpc_probe,
2808 .remove = mgslpc_detach,
2809 .id_table = mgslpc_ids,
2810 .suspend = mgslpc_suspend,
2811 .resume = mgslpc_resume,
2814 static const struct tty_operations mgslpc_ops = {
2815 .open = mgslpc_open,
2816 .close = mgslpc_close,
2817 .write = mgslpc_write,
2818 .put_char = mgslpc_put_char,
2819 .flush_chars = mgslpc_flush_chars,
2820 .write_room = mgslpc_write_room,
2821 .chars_in_buffer = mgslpc_chars_in_buffer,
2822 .flush_buffer = mgslpc_flush_buffer,
2823 .ioctl = mgslpc_ioctl,
2824 .throttle = mgslpc_throttle,
2825 .unthrottle = mgslpc_unthrottle,
2826 .send_xchar = mgslpc_send_xchar,
2827 .break_ctl = mgslpc_break,
2828 .wait_until_sent = mgslpc_wait_until_sent,
2829 .set_termios = mgslpc_set_termios,
2831 .start = tx_release,
2832 .hangup = mgslpc_hangup,
2833 .tiocmget = tiocmget,
2834 .tiocmset = tiocmset,
2835 .proc_fops = &mgslpc_proc_fops,
2838 static void synclink_cs_cleanup(void)
2842 printk("Unloading %s: version %s\n", driver_name, driver_version);
2844 while(mgslpc_device_list)
2845 mgslpc_remove_device(mgslpc_device_list);
2847 if (serial_driver) {
2848 if ((rc = tty_unregister_driver(serial_driver)))
2849 printk("%s(%d) failed to unregister tty driver err=%d\n",
2850 __FILE__,__LINE__,rc);
2851 put_tty_driver(serial_driver);
2854 pcmcia_unregister_driver(&mgslpc_driver);
2857 static int __init synclink_cs_init(void)
2861 if (break_on_load) {
2862 mgslpc_get_text_ptr();
2866 printk("%s %s\n", driver_name, driver_version);
2868 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
2871 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
2872 if (!serial_driver) {
2877 /* Initialize the tty_driver structure */
2879 serial_driver->owner = THIS_MODULE;
2880 serial_driver->driver_name = "synclink_cs";
2881 serial_driver->name = "ttySLP";
2882 serial_driver->major = ttymajor;
2883 serial_driver->minor_start = 64;
2884 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2885 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2886 serial_driver->init_termios = tty_std_termios;
2887 serial_driver->init_termios.c_cflag =
2888 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2889 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2890 tty_set_operations(serial_driver, &mgslpc_ops);
2892 if ((rc = tty_register_driver(serial_driver)) < 0) {
2893 printk("%s(%d):Couldn't register serial driver\n",
2895 put_tty_driver(serial_driver);
2896 serial_driver = NULL;
2900 printk("%s %s, tty major#%d\n",
2901 driver_name, driver_version,
2902 serial_driver->major);
2907 synclink_cs_cleanup();
2911 static void __exit synclink_cs_exit(void)
2913 synclink_cs_cleanup();
2916 module_init(synclink_cs_init);
2917 module_exit(synclink_cs_exit);
2919 static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
2924 /* note:standard BRG mode is broken in V3.2 chip
2925 * so enhanced mode is always used
2933 for (M = 1; N > 64 && M < 16; M++)
2939 * BGR[7..0] contained in BGR register
2940 * BGR[9..8] contained in CCR2[7..6]
2941 * divisor = (N+1)*2^M
2943 * Note: M *must* not be zero (causes asymetric duty cycle)
2945 write_reg(info, (unsigned char) (channel + BGR),
2946 (unsigned char) ((M << 6) + N));
2947 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
2948 val |= ((M << 4) & 0xc0);
2949 write_reg(info, (unsigned char) (channel + CCR2), val);
2953 /* Enabled the AUX clock output at the specified frequency.
2955 static void enable_auxclk(MGSLPC_INFO *info)
2961 * 07..06 MDS[1..0] 10 = transparent HDLC mode
2962 * 05 ADM Address Mode, 0 = no addr recognition
2963 * 04 TMD Timer Mode, 0 = external
2964 * 03 RAC Receiver Active, 0 = inactive
2965 * 02 RTS 0=RTS active during xmit, 1=RTS always active
2966 * 01 TRS Timer Resolution, 1=512
2967 * 00 TLP Test Loop, 0 = no loop
2973 /* channel B RTS is used to enable AUXCLK driver on SP505 */
2974 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
2976 write_reg(info, CHB + MODE, val);
2980 * 07 PU Power Up, 1=active, 0=power down
2981 * 06 MCE Master Clock Enable, 1=enabled
2983 * 04..02 SC[2..0] Encoding
2984 * 01..00 SM[1..0] Serial Mode, 00=HDLC
2988 write_reg(info, CHB + CCR0, 0xc0);
2992 * 07 SFLG Shared Flag, 0 = disable shared flags
2993 * 06 GALP Go Active On Loop, 0 = not used
2994 * 05 GLP Go On Loop, 0 = not used
2995 * 04 ODS Output Driver Select, 1=TxD is push-pull output
2996 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
2997 * 02..00 CM[2..0] Clock Mode
3001 write_reg(info, CHB + CCR1, 0x17);
3005 * 07..06 BGR[9..8] Baud rate bits 9..8
3006 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3007 * 04 SSEL Clock source select, 1=submode b
3008 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3009 * 02 RWX Read/Write Exchange 0=disabled
3010 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3011 * 00 DIV, data inversion 0=disabled, 1=enabled
3015 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3016 write_reg(info, CHB + CCR2, 0x38);
3018 write_reg(info, CHB + CCR2, 0x30);
3022 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3023 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3024 * 05 TST1 Test Pin, 0=normal operation
3025 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3026 * 03..02 Reserved, must be 0
3027 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3031 write_reg(info, CHB + CCR4, 0x50);
3033 /* if auxclk not enabled, set internal BRG so
3034 * CTS transitions can be detected (requires TxC)
3036 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3037 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3039 mgslpc_set_rate(info, CHB, 921600);
3042 static void loopback_enable(MGSLPC_INFO *info)
3046 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3047 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3048 write_reg(info, CHA + CCR1, val);
3050 /* CCR2:04 SSEL Clock source select, 1=submode b */
3051 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3052 write_reg(info, CHA + CCR2, val);
3054 /* set LinkSpeed if available, otherwise default to 2Mbps */
3055 if (info->params.clock_speed)
3056 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3058 mgslpc_set_rate(info, CHA, 1843200);
3060 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3061 val = read_reg(info, CHA + MODE) | BIT0;
3062 write_reg(info, CHA + MODE, val);
3065 static void hdlc_mode(MGSLPC_INFO *info)
3068 unsigned char clkmode, clksubmode;
3070 /* disable all interrupts */
3071 irq_disable(info, CHA, 0xffff);
3072 irq_disable(info, CHB, 0xffff);
3073 port_irq_disable(info, 0xff);
3075 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3076 clkmode = clksubmode = 0;
3077 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3078 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3079 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3081 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3082 && info->params.flags & HDLC_FLAG_TXC_BRG) {
3083 /* clock mode 7b, rcv = BRG, xmt = BRG */
3086 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3087 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3088 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3092 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3095 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3096 /* clock mode 0b, rcv = RxC, xmt = BRG */
3102 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3103 * 05 ADM Address Mode, 0 = no addr recognition
3104 * 04 TMD Timer Mode, 0 = external
3105 * 03 RAC Receiver Active, 0 = inactive
3106 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3107 * 01 TRS Timer Resolution, 1=512
3108 * 00 TLP Test Loop, 0 = no loop
3113 if (info->params.loopback)
3116 /* preserve RTS state */
3117 if (info->serial_signals & SerialSignal_RTS)
3119 write_reg(info, CHA + MODE, val);
3123 * 07 PU Power Up, 1=active, 0=power down
3124 * 06 MCE Master Clock Enable, 1=enabled
3126 * 04..02 SC[2..0] Encoding
3127 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3132 switch (info->params.encoding)
3134 case HDLC_ENCODING_NRZI:
3137 case HDLC_ENCODING_BIPHASE_SPACE:
3140 case HDLC_ENCODING_BIPHASE_MARK:
3143 case HDLC_ENCODING_BIPHASE_LEVEL:
3145 break; // Manchester
3147 write_reg(info, CHA + CCR0, val);
3151 * 07 SFLG Shared Flag, 0 = disable shared flags
3152 * 06 GALP Go Active On Loop, 0 = not used
3153 * 05 GLP Go On Loop, 0 = not used
3154 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3155 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3156 * 02..00 CM[2..0] Clock Mode
3160 val = 0x10 + clkmode;
3161 write_reg(info, CHA + CCR1, val);
3165 * 07..06 BGR[9..8] Baud rate bits 9..8
3166 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3167 * 04 SSEL Clock source select, 1=submode b
3168 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3169 * 02 RWX Read/Write Exchange 0=disabled
3170 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3171 * 00 DIV, data inversion 0=disabled, 1=enabled
3176 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3177 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3181 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3183 if (info->params.encoding == HDLC_ENCODING_NRZB)
3185 write_reg(info, CHA + CCR2, val);
3189 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3190 * 05 EPT Enable preamble transmission, 1=enabled
3191 * 04 RADD Receive address pushed to FIFO, 0=disabled
3192 * 03 CRL CRC Reset Level, 0=FFFF
3193 * 02 RCRC Rx CRC 0=On 1=Off
3194 * 01 TCRC Tx CRC 0=On 1=Off
3195 * 00 PSD DPLL Phase Shift Disable
3200 if (info->params.crc_type == HDLC_CRC_NONE)
3202 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3204 switch (info->params.preamble_length)
3206 case HDLC_PREAMBLE_LENGTH_16BITS:
3209 case HDLC_PREAMBLE_LENGTH_32BITS:
3212 case HDLC_PREAMBLE_LENGTH_64BITS:
3216 write_reg(info, CHA + CCR3, val);
3218 /* PRE - Preamble pattern */
3220 switch (info->params.preamble)
3222 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3223 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3224 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3225 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3227 write_reg(info, CHA + PRE, val);
3231 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3232 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3233 * 05 TST1 Test Pin, 0=normal operation
3234 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3235 * 03..02 Reserved, must be 0
3236 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3241 write_reg(info, CHA + CCR4, val);
3242 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3243 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3245 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3247 /* RLCR Receive length check register
3249 * 7 1=enable receive length check
3250 * 6..0 Max frame length = (RL + 1) * 32
3252 write_reg(info, CHA + RLCR, 0);
3254 /* XBCH Transmit Byte Count High
3256 * 07 DMA mode, 0 = interrupt driven
3257 * 06 NRM, 0=ABM (ignored)
3258 * 05 CAS Carrier Auto Start
3259 * 04 XC Transmit Continuously (ignored)
3260 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3265 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3267 write_reg(info, CHA + XBCH, val);
3268 enable_auxclk(info);
3269 if (info->params.loopback || info->testing_irq)
3270 loopback_enable(info);
3271 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3273 irq_enable(info, CHB, IRQ_CTS);
3274 /* PVR[3] 1=AUTO CTS active */
3275 set_reg_bits(info, CHA + PVR, BIT3);
3277 clear_reg_bits(info, CHA + PVR, BIT3);
3279 irq_enable(info, CHA,
3280 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3281 IRQ_UNDERRUN + IRQ_TXFIFO);
3282 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3283 wait_command_complete(info, CHA);
3284 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3286 /* Master clock mode enabled above to allow reset commands
3287 * to complete even if no data clocks are present.
3289 * Disable master clock mode for normal communications because
3290 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3291 * IRQ when in master clock mode.
3293 * Leave master clock mode enabled for IRQ test because the
3294 * timer IRQ used by the test can only happen in master clock mode.
3296 if (!info->testing_irq)
3297 clear_reg_bits(info, CHA + CCR0, BIT6);
3305 static void rx_stop(MGSLPC_INFO *info)
3307 if (debug_level >= DEBUG_LEVEL_ISR)
3308 printk("%s(%d):rx_stop(%s)\n",
3309 __FILE__,__LINE__, info->device_name );
3311 /* MODE:03 RAC Receiver Active, 0=inactive */
3312 clear_reg_bits(info, CHA + MODE, BIT3);
3314 info->rx_enabled = false;
3315 info->rx_overflow = false;
3318 static void rx_start(MGSLPC_INFO *info)
3320 if (debug_level >= DEBUG_LEVEL_ISR)
3321 printk("%s(%d):rx_start(%s)\n",
3322 __FILE__,__LINE__, info->device_name );
3324 rx_reset_buffers(info);
3325 info->rx_enabled = false;
3326 info->rx_overflow = false;
3328 /* MODE:03 RAC Receiver Active, 1=active */
3329 set_reg_bits(info, CHA + MODE, BIT3);
3331 info->rx_enabled = true;
3334 static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty)
3336 if (debug_level >= DEBUG_LEVEL_ISR)
3337 printk("%s(%d):tx_start(%s)\n",
3338 __FILE__,__LINE__, info->device_name );
3340 if (info->tx_count) {
3341 /* If auto RTS enabled and RTS is inactive, then assert */
3342 /* RTS and set a flag indicating that the driver should */
3343 /* negate RTS when the transmission completes. */
3344 info->drop_rts_on_tx_done = false;
3346 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3348 if (!(info->serial_signals & SerialSignal_RTS)) {
3349 info->serial_signals |= SerialSignal_RTS;
3351 info->drop_rts_on_tx_done = true;
3355 if (info->params.mode == MGSL_MODE_ASYNC) {
3356 if (!info->tx_active) {
3357 info->tx_active = true;
3358 tx_ready(info, tty);
3361 info->tx_active = true;
3362 tx_ready(info, tty);
3363 mod_timer(&info->tx_timer, jiffies +
3364 msecs_to_jiffies(5000));
3368 if (!info->tx_enabled)
3369 info->tx_enabled = true;
3372 static void tx_stop(MGSLPC_INFO *info)
3374 if (debug_level >= DEBUG_LEVEL_ISR)
3375 printk("%s(%d):tx_stop(%s)\n",
3376 __FILE__,__LINE__, info->device_name );
3378 del_timer(&info->tx_timer);
3380 info->tx_enabled = false;
3381 info->tx_active = false;
3384 /* Reset the adapter to a known state and prepare it for further use.
3386 static void reset_device(MGSLPC_INFO *info)
3388 /* power up both channels (set BIT7) */
3389 write_reg(info, CHA + CCR0, 0x80);
3390 write_reg(info, CHB + CCR0, 0x80);
3391 write_reg(info, CHA + MODE, 0);
3392 write_reg(info, CHB + MODE, 0);
3394 /* disable all interrupts */
3395 irq_disable(info, CHA, 0xffff);
3396 irq_disable(info, CHB, 0xffff);
3397 port_irq_disable(info, 0xff);
3399 /* PCR Port Configuration Register
3401 * 07..04 DEC[3..0] Serial I/F select outputs
3402 * 03 output, 1=AUTO CTS control enabled
3403 * 02 RI Ring Indicator input 0=active
3404 * 01 DSR input 0=active
3405 * 00 DTR output 0=active
3409 write_reg(info, PCR, 0x06);
3411 /* PVR Port Value Register
3413 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3414 * 03 AUTO CTS output 1=enabled
3415 * 02 RI Ring Indicator input
3417 * 00 DTR output (1=inactive)
3421 // write_reg(info, PVR, PVR_DTR);
3423 /* IPC Interrupt Port Configuration
3425 * 07 VIS 1=Masked interrupts visible
3426 * 06..05 Reserved, 0
3427 * 04..03 SLA Slave address, 00 ignored
3428 * 02 CASM Cascading Mode, 1=daisy chain
3429 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3433 write_reg(info, IPC, 0x05);
3436 static void async_mode(MGSLPC_INFO *info)
3440 /* disable all interrupts */
3441 irq_disable(info, CHA, 0xffff);
3442 irq_disable(info, CHB, 0xffff);
3443 port_irq_disable(info, 0xff);
3448 * 06 FRTS RTS State, 0=active
3449 * 05 FCTS Flow Control on CTS
3450 * 04 FLON Flow Control Enable
3451 * 03 RAC Receiver Active, 0 = inactive
3452 * 02 RTS 0=Auto RTS, 1=manual RTS
3453 * 01 TRS Timer Resolution, 1=512
3454 * 00 TLP Test Loop, 0 = no loop
3459 if (info->params.loopback)
3462 /* preserve RTS state */
3463 if (!(info->serial_signals & SerialSignal_RTS))
3465 write_reg(info, CHA + MODE, val);
3469 * 07 PU Power Up, 1=active, 0=power down
3470 * 06 MCE Master Clock Enable, 1=enabled
3472 * 04..02 SC[2..0] Encoding, 000=NRZ
3473 * 01..00 SM[1..0] Serial Mode, 11=Async
3477 write_reg(info, CHA + CCR0, 0x83);
3481 * 07..05 Reserved, 0
3482 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3483 * 03 BCR Bit Clock Rate, 1=16x
3484 * 02..00 CM[2..0] Clock Mode, 111=BRG
3488 write_reg(info, CHA + CCR1, 0x1f);
3492 * 07..06 BGR[9..8] Baud rate bits 9..8
3493 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3494 * 04 SSEL Clock source select, 1=submode b
3495 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3496 * 02 RWX Read/Write Exchange 0=disabled
3498 * 00 DIV, data inversion 0=disabled, 1=enabled
3502 write_reg(info, CHA + CCR2, 0x10);
3506 * 07..01 Reserved, 0
3507 * 00 PSD DPLL Phase Shift Disable
3511 write_reg(info, CHA + CCR3, 0);
3515 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3516 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3517 * 05 TST1 Test Pin, 0=normal operation
3518 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3519 * 03..00 Reserved, must be 0
3523 write_reg(info, CHA + CCR4, 0x50);
3524 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3529 * 06 XBRK transmit break, 0=normal operation
3530 * 05 Stop bits (0=1, 1=2)
3531 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3532 * 02 PAREN Parity Enable
3533 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3537 if (info->params.data_bits != 8)
3538 val |= BIT0; /* 7 bits */
3539 if (info->params.stop_bits != 1)
3541 if (info->params.parity != ASYNC_PARITY_NONE)
3543 val |= BIT2; /* Parity enable */
3544 if (info->params.parity == ASYNC_PARITY_ODD)
3549 write_reg(info, CHA + DAFO, val);
3551 /* RFC Rx FIFO Control
3554 * 06 DPS, 1=parity bit not stored in data byte
3555 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3556 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3557 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3559 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3563 write_reg(info, CHA + RFC, 0x5c);
3565 /* RLCR Receive length check register
3567 * Max frame length = (RL + 1) * 32
3569 write_reg(info, CHA + RLCR, 0);
3571 /* XBCH Transmit Byte Count High
3573 * 07 DMA mode, 0 = interrupt driven
3574 * 06 NRM, 0=ABM (ignored)
3575 * 05 CAS Carrier Auto Start
3576 * 04 XC Transmit Continuously (ignored)
3577 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3582 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3584 write_reg(info, CHA + XBCH, val);
3585 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3586 irq_enable(info, CHA, IRQ_CTS);
3588 /* MODE:03 RAC Receiver Active, 1=active */
3589 set_reg_bits(info, CHA + MODE, BIT3);
3590 enable_auxclk(info);
3591 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3592 irq_enable(info, CHB, IRQ_CTS);
3593 /* PVR[3] 1=AUTO CTS active */
3594 set_reg_bits(info, CHA + PVR, BIT3);
3596 clear_reg_bits(info, CHA + PVR, BIT3);
3597 irq_enable(info, CHA,
3598 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3599 IRQ_ALLSENT + IRQ_TXFIFO);
3600 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3601 wait_command_complete(info, CHA);
3602 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3605 /* Set the HDLC idle mode for the transmitter.
3607 static void tx_set_idle(MGSLPC_INFO *info)
3609 /* Note: ESCC2 only supports flags and one idle modes */
3610 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3611 set_reg_bits(info, CHA + CCR1, BIT3);
3613 clear_reg_bits(info, CHA + CCR1, BIT3);
3616 /* get state of the V24 status (input) signals.
3618 static void get_signals(MGSLPC_INFO *info)
3620 unsigned char status = 0;
3622 /* preserve DTR and RTS */
3623 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3625 if (read_reg(info, CHB + VSTR) & BIT7)
3626 info->serial_signals |= SerialSignal_DCD;
3627 if (read_reg(info, CHB + STAR) & BIT1)
3628 info->serial_signals |= SerialSignal_CTS;
3630 status = read_reg(info, CHA + PVR);
3631 if (!(status & PVR_RI))
3632 info->serial_signals |= SerialSignal_RI;
3633 if (!(status & PVR_DSR))
3634 info->serial_signals |= SerialSignal_DSR;
3637 /* Set the state of DTR and RTS based on contents of
3638 * serial_signals member of device extension.
3640 static void set_signals(MGSLPC_INFO *info)
3644 val = read_reg(info, CHA + MODE);
3645 if (info->params.mode == MGSL_MODE_ASYNC) {
3646 if (info->serial_signals & SerialSignal_RTS)
3651 if (info->serial_signals & SerialSignal_RTS)
3656 write_reg(info, CHA + MODE, val);
3658 if (info->serial_signals & SerialSignal_DTR)
3659 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3661 set_reg_bits(info, CHA + PVR, PVR_DTR);
3664 static void rx_reset_buffers(MGSLPC_INFO *info)
3671 info->rx_frame_count = 0;
3672 for (i=0 ; i < info->rx_buf_count ; i++) {
3673 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3674 buf->status = buf->count = 0;
3678 /* Attempt to return a received HDLC frame
3679 * Only frames received without errors are returned.
3681 * Returns true if frame returned, otherwise false
3683 static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty)
3685 unsigned short status;
3687 unsigned int framesize = 0;
3688 unsigned long flags;
3689 bool return_frame = false;
3691 if (info->rx_frame_count == 0)
3694 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3696 status = buf->status;
3698 /* 07 VFR 1=valid frame
3699 * 06 RDO 1=data overrun
3700 * 05 CRC 1=OK, 0=error
3701 * 04 RAB 1=frame aborted
3703 if ((status & 0xf0) != 0xA0) {
3704 if (!(status & BIT7) || (status & BIT4))
3705 info->icount.rxabort++;
3706 else if (status & BIT6)
3707 info->icount.rxover++;
3708 else if (!(status & BIT5)) {
3709 info->icount.rxcrc++;
3710 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
3711 return_frame = true;
3714 #if SYNCLINK_GENERIC_HDLC
3716 info->netdev->stats.rx_errors++;
3717 info->netdev->stats.rx_frame_errors++;
3721 return_frame = true;
3724 framesize = buf->count;
3726 if (debug_level >= DEBUG_LEVEL_BH)
3727 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3728 __FILE__,__LINE__,info->device_name,status,framesize);
3730 if (debug_level >= DEBUG_LEVEL_DATA)
3731 trace_block(info, buf->data, framesize, 0);
3734 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3735 framesize+1 > info->max_frame_size) ||
3736 framesize > info->max_frame_size)
3737 info->icount.rxlong++;
3740 info->icount.rxok++;
3742 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3743 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3747 #if SYNCLINK_GENERIC_HDLC
3749 hdlcdev_rx(info, buf->data, framesize);
3752 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3756 spin_lock_irqsave(&info->lock,flags);
3757 buf->status = buf->count = 0;
3758 info->rx_frame_count--;
3760 if (info->rx_get >= info->rx_buf_count)
3762 spin_unlock_irqrestore(&info->lock,flags);
3767 static bool register_test(MGSLPC_INFO *info)
3769 static unsigned char patterns[] =
3770 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
3771 static unsigned int count = ARRAY_SIZE(patterns);
3774 unsigned long flags;
3776 spin_lock_irqsave(&info->lock,flags);
3779 for (i = 0; i < count; i++) {
3780 write_reg(info, XAD1, patterns[i]);
3781 write_reg(info, XAD2, patterns[(i + 1) % count]);
3782 if ((read_reg(info, XAD1) != patterns[i]) ||
3783 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
3789 spin_unlock_irqrestore(&info->lock,flags);
3793 static bool irq_test(MGSLPC_INFO *info)
3795 unsigned long end_time;
3796 unsigned long flags;
3798 spin_lock_irqsave(&info->lock,flags);
3801 info->testing_irq = true;
3804 info->irq_occurred = false;
3806 /* init hdlc mode */
3808 irq_enable(info, CHA, IRQ_TIMER);
3809 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
3810 issue_command(info, CHA, CMD_START_TIMER);
3812 spin_unlock_irqrestore(&info->lock,flags);
3815 while(end_time-- && !info->irq_occurred) {
3816 msleep_interruptible(10);
3819 info->testing_irq = false;
3821 spin_lock_irqsave(&info->lock,flags);
3823 spin_unlock_irqrestore(&info->lock,flags);
3825 return info->irq_occurred;
3828 static int adapter_test(MGSLPC_INFO *info)
3830 if (!register_test(info)) {
3831 info->init_error = DiagStatus_AddressFailure;
3832 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
3833 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
3837 if (!irq_test(info)) {
3838 info->init_error = DiagStatus_IrqFailure;
3839 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
3840 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
3844 if (debug_level >= DEBUG_LEVEL_INFO)
3845 printk("%s(%d):device %s passed diagnostics\n",
3846 __FILE__,__LINE__,info->device_name);
3850 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
3855 printk("%s tx data:\n",info->device_name);
3857 printk("%s rx data:\n",info->device_name);
3865 for(i=0;i<linecount;i++)
3866 printk("%02X ",(unsigned char)data[i]);
3869 for(i=0;i<linecount;i++) {
3870 if (data[i]>=040 && data[i]<=0176)
3871 printk("%c",data[i]);
3882 /* HDLC frame time out
3883 * update stats and do tx completion processing
3885 static void tx_timeout(unsigned long context)
3887 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
3888 unsigned long flags;
3890 if ( debug_level >= DEBUG_LEVEL_INFO )
3891 printk( "%s(%d):tx_timeout(%s)\n",
3892 __FILE__,__LINE__,info->device_name);
3893 if(info->tx_active &&
3894 info->params.mode == MGSL_MODE_HDLC) {
3895 info->icount.txtimeout++;
3897 spin_lock_irqsave(&info->lock,flags);
3898 info->tx_active = false;
3899 info->tx_count = info->tx_put = info->tx_get = 0;
3901 spin_unlock_irqrestore(&info->lock,flags);
3903 #if SYNCLINK_GENERIC_HDLC
3905 hdlcdev_tx_done(info);
3909 struct tty_struct *tty = tty_port_tty_get(&info->port);
3910 bh_transmit(info, tty);
3915 #if SYNCLINK_GENERIC_HDLC
3918 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
3919 * set encoding and frame check sequence (FCS) options
3921 * dev pointer to network device structure
3922 * encoding serial encoding setting
3923 * parity FCS setting
3925 * returns 0 if success, otherwise error code
3927 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
3928 unsigned short parity)
3930 MGSLPC_INFO *info = dev_to_port(dev);
3931 struct tty_struct *tty;
3932 unsigned char new_encoding;
3933 unsigned short new_crctype;
3935 /* return error if TTY interface open */
3936 if (info->port.count)
3941 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
3942 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
3943 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
3944 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
3945 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
3946 default: return -EINVAL;
3951 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
3952 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
3953 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
3954 default: return -EINVAL;
3957 info->params.encoding = new_encoding;
3958 info->params.crc_type = new_crctype;
3960 /* if network interface up, reprogram hardware */
3961 if (info->netcount) {
3962 tty = tty_port_tty_get(&info->port);
3963 mgslpc_program_hw(info, tty);
3971 * called by generic HDLC layer to send frame
3973 * skb socket buffer containing HDLC frame
3974 * dev pointer to network device structure
3976 static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
3977 struct net_device *dev)
3979 MGSLPC_INFO *info = dev_to_port(dev);
3980 unsigned long flags;
3982 if (debug_level >= DEBUG_LEVEL_INFO)
3983 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
3985 /* stop sending until this frame completes */
3986 netif_stop_queue(dev);
3988 /* copy data to device buffers */
3989 skb_copy_from_linear_data(skb, info->tx_buf, skb->len);
3991 info->tx_put = info->tx_count = skb->len;
3993 /* update network statistics */
3994 dev->stats.tx_packets++;
3995 dev->stats.tx_bytes += skb->len;
3997 /* done with socket buffer, so free it */
4000 /* save start time for transmit timeout detection */
4001 dev->trans_start = jiffies;
4003 /* start hardware transmitter if necessary */
4004 spin_lock_irqsave(&info->lock,flags);
4005 if (!info->tx_active) {
4006 struct tty_struct *tty = tty_port_tty_get(&info->port);
4007 tx_start(info, tty);
4010 spin_unlock_irqrestore(&info->lock,flags);
4012 return NETDEV_TX_OK;
4016 * called by network layer when interface enabled
4017 * claim resources and initialize hardware
4019 * dev pointer to network device structure
4021 * returns 0 if success, otherwise error code
4023 static int hdlcdev_open(struct net_device *dev)
4025 MGSLPC_INFO *info = dev_to_port(dev);
4026 struct tty_struct *tty;
4028 unsigned long flags;
4030 if (debug_level >= DEBUG_LEVEL_INFO)
4031 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4033 /* generic HDLC layer open processing */
4034 if ((rc = hdlc_open(dev)))
4037 /* arbitrate between network and tty opens */
4038 spin_lock_irqsave(&info->netlock, flags);
4039 if (info->port.count != 0 || info->netcount != 0) {
4040 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4041 spin_unlock_irqrestore(&info->netlock, flags);
4045 spin_unlock_irqrestore(&info->netlock, flags);
4047 tty = tty_port_tty_get(&info->port);
4048 /* claim resources and init adapter */
4049 if ((rc = startup(info, tty)) != 0) {
4051 spin_lock_irqsave(&info->netlock, flags);
4053 spin_unlock_irqrestore(&info->netlock, flags);
4056 /* assert DTR and RTS, apply hardware settings */
4057 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4058 mgslpc_program_hw(info, tty);
4061 /* enable network layer transmit */
4062 dev->trans_start = jiffies;
4063 netif_start_queue(dev);
4065 /* inform generic HDLC layer of current DCD status */
4066 spin_lock_irqsave(&info->lock, flags);
4068 spin_unlock_irqrestore(&info->lock, flags);
4069 if (info->serial_signals & SerialSignal_DCD)
4070 netif_carrier_on(dev);
4072 netif_carrier_off(dev);
4077 * called by network layer when interface is disabled
4078 * shutdown hardware and release resources
4080 * dev pointer to network device structure
4082 * returns 0 if success, otherwise error code
4084 static int hdlcdev_close(struct net_device *dev)
4086 MGSLPC_INFO *info = dev_to_port(dev);
4087 struct tty_struct *tty = tty_port_tty_get(&info->port);
4088 unsigned long flags;
4090 if (debug_level >= DEBUG_LEVEL_INFO)
4091 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4093 netif_stop_queue(dev);
4095 /* shutdown adapter and release resources */
4096 shutdown(info, tty);
4100 spin_lock_irqsave(&info->netlock, flags);
4102 spin_unlock_irqrestore(&info->netlock, flags);
4108 * called by network layer to process IOCTL call to network device
4110 * dev pointer to network device structure
4111 * ifr pointer to network interface request structure
4112 * cmd IOCTL command code
4114 * returns 0 if success, otherwise error code
4116 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4118 const size_t size = sizeof(sync_serial_settings);
4119 sync_serial_settings new_line;
4120 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4121 MGSLPC_INFO *info = dev_to_port(dev);
4124 if (debug_level >= DEBUG_LEVEL_INFO)
4125 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4127 /* return error if TTY interface open */
4128 if (info->port.count)
4131 if (cmd != SIOCWANDEV)
4132 return hdlc_ioctl(dev, ifr, cmd);
4134 switch(ifr->ifr_settings.type) {
4135 case IF_GET_IFACE: /* return current sync_serial_settings */
4137 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4138 if (ifr->ifr_settings.size < size) {
4139 ifr->ifr_settings.size = size; /* data size wanted */
4143 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4144 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4145 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4146 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4149 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4150 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4151 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4152 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4153 default: new_line.clock_type = CLOCK_DEFAULT;
4156 new_line.clock_rate = info->params.clock_speed;
4157 new_line.loopback = info->params.loopback ? 1:0;
4159 if (copy_to_user(line, &new_line, size))
4163 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4165 if(!capable(CAP_NET_ADMIN))
4167 if (copy_from_user(&new_line, line, size))
4170 switch (new_line.clock_type)
4172 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4173 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4174 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4175 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4176 case CLOCK_DEFAULT: flags = info->params.flags &
4177 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4178 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4179 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4180 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4181 default: return -EINVAL;
4184 if (new_line.loopback != 0 && new_line.loopback != 1)
4187 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4188 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4189 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4190 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4191 info->params.flags |= flags;
4193 info->params.loopback = new_line.loopback;
4195 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4196 info->params.clock_speed = new_line.clock_rate;
4198 info->params.clock_speed = 0;
4200 /* if network interface up, reprogram hardware */
4201 if (info->netcount) {
4202 struct tty_struct *tty = tty_port_tty_get(&info->port);
4203 mgslpc_program_hw(info, tty);
4209 return hdlc_ioctl(dev, ifr, cmd);
4214 * called by network layer when transmit timeout is detected
4216 * dev pointer to network device structure
4218 static void hdlcdev_tx_timeout(struct net_device *dev)
4220 MGSLPC_INFO *info = dev_to_port(dev);
4221 unsigned long flags;
4223 if (debug_level >= DEBUG_LEVEL_INFO)
4224 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4226 dev->stats.tx_errors++;
4227 dev->stats.tx_aborted_errors++;
4229 spin_lock_irqsave(&info->lock,flags);
4231 spin_unlock_irqrestore(&info->lock,flags);
4233 netif_wake_queue(dev);
4237 * called by device driver when transmit completes
4238 * reenable network layer transmit if stopped
4240 * info pointer to device instance information
4242 static void hdlcdev_tx_done(MGSLPC_INFO *info)
4244 if (netif_queue_stopped(info->netdev))
4245 netif_wake_queue(info->netdev);
4249 * called by device driver when frame received
4250 * pass frame to network layer
4252 * info pointer to device instance information
4253 * buf pointer to buffer contianing frame data
4254 * size count of data bytes in buf
4256 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4258 struct sk_buff *skb = dev_alloc_skb(size);
4259 struct net_device *dev = info->netdev;
4261 if (debug_level >= DEBUG_LEVEL_INFO)
4262 printk("hdlcdev_rx(%s)\n",dev->name);
4265 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
4266 dev->stats.rx_dropped++;
4270 memcpy(skb_put(skb, size), buf, size);
4272 skb->protocol = hdlc_type_trans(skb, dev);
4274 dev->stats.rx_packets++;
4275 dev->stats.rx_bytes += size;
4280 static const struct net_device_ops hdlcdev_ops = {
4281 .ndo_open = hdlcdev_open,
4282 .ndo_stop = hdlcdev_close,
4283 .ndo_change_mtu = hdlc_change_mtu,
4284 .ndo_start_xmit = hdlc_start_xmit,
4285 .ndo_do_ioctl = hdlcdev_ioctl,
4286 .ndo_tx_timeout = hdlcdev_tx_timeout,
4290 * called by device driver when adding device instance
4291 * do generic HDLC initialization
4293 * info pointer to device instance information
4295 * returns 0 if success, otherwise error code
4297 static int hdlcdev_init(MGSLPC_INFO *info)
4300 struct net_device *dev;
4303 /* allocate and initialize network and HDLC layer objects */
4305 if (!(dev = alloc_hdlcdev(info))) {
4306 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4310 /* for network layer reporting purposes only */
4311 dev->base_addr = info->io_base;
4312 dev->irq = info->irq_level;
4314 /* network layer callbacks and settings */
4315 dev->netdev_ops = &hdlcdev_ops;
4316 dev->watchdog_timeo = 10 * HZ;
4317 dev->tx_queue_len = 50;
4319 /* generic HDLC layer callbacks and settings */
4320 hdlc = dev_to_hdlc(dev);
4321 hdlc->attach = hdlcdev_attach;
4322 hdlc->xmit = hdlcdev_xmit;
4324 /* register objects with HDLC layer */
4325 if ((rc = register_hdlc_device(dev))) {
4326 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4336 * called by device driver when removing device instance
4337 * do generic HDLC cleanup
4339 * info pointer to device instance information
4341 static void hdlcdev_exit(MGSLPC_INFO *info)
4343 unregister_hdlc_device(info->netdev);
4344 free_netdev(info->netdev);
4345 info->netdev = NULL;
4348 #endif /* CONFIG_HDLC */