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_types.h>
74 #include <pcmcia/cs.h>
75 #include <pcmcia/cistpl.h>
76 #include <pcmcia/cisreg.h>
77 #include <pcmcia/ds.h>
79 #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_CS_MODULE))
80 #define SYNCLINK_GENERIC_HDLC 1
82 #define SYNCLINK_GENERIC_HDLC 0
85 #define GET_USER(error,value,addr) error = get_user(value,addr)
86 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
87 #define PUT_USER(error,value,addr) error = put_user(value,addr)
88 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
90 #include <asm/uaccess.h>
92 static MGSL_PARAMS default_params = {
93 MGSL_MODE_HDLC, /* unsigned long mode */
94 0, /* unsigned char loopback; */
95 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
96 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
97 0, /* unsigned long clock_speed; */
98 0xff, /* unsigned char addr_filter; */
99 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
100 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
101 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
102 9600, /* unsigned long data_rate; */
103 8, /* unsigned char data_bits; */
104 1, /* unsigned char stop_bits; */
105 ASYNC_PARITY_NONE /* unsigned char parity; */
111 unsigned char status;
115 /* The queue of BH actions to be performed */
118 #define BH_TRANSMIT 2
121 #define IO_PIN_SHUTDOWN_LIMIT 100
123 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
125 struct _input_signal_events {
138 * Device instance data structure
141 typedef struct _mgslpc_info {
142 struct tty_port port;
143 void *if_ptr; /* General purpose pointer (used by SPPP) */
147 struct mgsl_icount icount;
150 int x_char; /* xon/xoff character */
151 unsigned char read_status_mask;
152 unsigned char ignore_status_mask;
154 unsigned char *tx_buf;
159 /* circular list of fixed length rx buffers */
161 unsigned char *rx_buf; /* memory allocated for all rx buffers */
162 int rx_buf_total_size; /* size of memory allocated for rx buffers */
163 int rx_put; /* index of next empty rx buffer */
164 int rx_get; /* index of next full rx buffer */
165 int rx_buf_size; /* size in bytes of single rx buffer */
166 int rx_buf_count; /* total number of rx buffers */
167 int rx_frame_count; /* number of full rx buffers */
169 wait_queue_head_t status_event_wait_q;
170 wait_queue_head_t event_wait_q;
171 struct timer_list tx_timer; /* HDLC transmit timeout timer */
172 struct _mgslpc_info *next_device; /* device list link */
174 unsigned short imra_value;
175 unsigned short imrb_value;
176 unsigned char pim_value;
179 struct work_struct task; /* task structure for scheduling bh */
188 int dcd_chkcount; /* check counts to prevent */
189 int cts_chkcount; /* too many IRQs if a signal */
190 int dsr_chkcount; /* is floating */
201 int if_mode; /* serial interface selection (RS-232, v.35 etc) */
203 char device_name[25]; /* device instance name */
205 unsigned int io_base; /* base I/O address of adapter */
206 unsigned int irq_level;
208 MGSL_PARAMS params; /* communications parameters */
210 unsigned char serial_signals; /* current serial signal states */
212 bool irq_occurred; /* for diagnostics use */
214 unsigned int init_error; /* startup error (DIAGS) */
216 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
217 bool drop_rts_on_tx_done;
219 struct _input_signal_events input_signal_events;
222 struct pcmcia_device *p_dev;
225 /* SPPP/Cisco HDLC device parts */
229 #if SYNCLINK_GENERIC_HDLC
230 struct net_device *netdev;
235 #define MGSLPC_MAGIC 0x5402
238 * The size of the serial xmit buffer is 1 page, or 4096 bytes
240 #define TXBUFSIZE 4096
243 #define CHA 0x00 /* channel A offset */
244 #define CHB 0x40 /* channel B offset */
247 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
294 #define IRQ_BREAK_ON BIT15 // rx break detected
295 #define IRQ_DATAOVERRUN BIT14 // receive data overflow
296 #define IRQ_ALLSENT BIT13 // all sent
297 #define IRQ_UNDERRUN BIT12 // transmit data underrun
298 #define IRQ_TIMER BIT11 // timer interrupt
299 #define IRQ_CTS BIT10 // CTS status change
300 #define IRQ_TXREPEAT BIT9 // tx message repeat
301 #define IRQ_TXFIFO BIT8 // transmit pool ready
302 #define IRQ_RXEOM BIT7 // receive message end
303 #define IRQ_EXITHUNT BIT6 // receive frame start
304 #define IRQ_RXTIME BIT6 // rx char timeout
305 #define IRQ_DCD BIT2 // carrier detect status change
306 #define IRQ_OVERRUN BIT1 // receive frame overflow
307 #define IRQ_RXFIFO BIT0 // receive pool full
311 #define XFW BIT6 // transmit FIFO write enable
312 #define CEC BIT2 // command executing
313 #define CTS BIT1 // CTS state
318 #define PVR_AUTOCTS BIT3
319 #define PVR_RS232 0x20 /* 0010b */
320 #define PVR_V35 0xe0 /* 1110b */
321 #define PVR_RS422 0x40 /* 0100b */
323 /* Register access functions */
325 #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
326 #define read_reg(info, reg) inb((info)->io_base + (reg))
328 #define read_reg16(info, reg) inw((info)->io_base + (reg))
329 #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
331 #define set_reg_bits(info, reg, mask) \
332 write_reg(info, (reg), \
333 (unsigned char) (read_reg(info, (reg)) | (mask)))
334 #define clear_reg_bits(info, reg, mask) \
335 write_reg(info, (reg), \
336 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
338 * interrupt enable/disable routines
340 static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
342 if (channel == CHA) {
343 info->imra_value |= mask;
344 write_reg16(info, CHA + IMR, info->imra_value);
346 info->imrb_value |= mask;
347 write_reg16(info, CHB + IMR, info->imrb_value);
350 static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
352 if (channel == CHA) {
353 info->imra_value &= ~mask;
354 write_reg16(info, CHA + IMR, info->imra_value);
356 info->imrb_value &= ~mask;
357 write_reg16(info, CHB + IMR, info->imrb_value);
361 #define port_irq_disable(info, mask) \
362 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
364 #define port_irq_enable(info, mask) \
365 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
367 static void rx_start(MGSLPC_INFO *info);
368 static void rx_stop(MGSLPC_INFO *info);
370 static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty);
371 static void tx_stop(MGSLPC_INFO *info);
372 static void tx_set_idle(MGSLPC_INFO *info);
374 static void get_signals(MGSLPC_INFO *info);
375 static void set_signals(MGSLPC_INFO *info);
377 static void reset_device(MGSLPC_INFO *info);
379 static void hdlc_mode(MGSLPC_INFO *info);
380 static void async_mode(MGSLPC_INFO *info);
382 static void tx_timeout(unsigned long context);
384 static int carrier_raised(struct tty_port *port);
385 static void dtr_rts(struct tty_port *port, int onoff);
387 #if SYNCLINK_GENERIC_HDLC
388 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
389 static void hdlcdev_tx_done(MGSLPC_INFO *info);
390 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
391 static int hdlcdev_init(MGSLPC_INFO *info);
392 static void hdlcdev_exit(MGSLPC_INFO *info);
395 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
397 static bool register_test(MGSLPC_INFO *info);
398 static bool irq_test(MGSLPC_INFO *info);
399 static int adapter_test(MGSLPC_INFO *info);
401 static int claim_resources(MGSLPC_INFO *info);
402 static void release_resources(MGSLPC_INFO *info);
403 static void mgslpc_add_device(MGSLPC_INFO *info);
404 static void mgslpc_remove_device(MGSLPC_INFO *info);
406 static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty);
407 static void rx_reset_buffers(MGSLPC_INFO *info);
408 static int rx_alloc_buffers(MGSLPC_INFO *info);
409 static void rx_free_buffers(MGSLPC_INFO *info);
411 static irqreturn_t mgslpc_isr(int irq, void *dev_id);
414 * Bottom half interrupt handlers
416 static void bh_handler(struct work_struct *work);
417 static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty);
418 static void bh_status(MGSLPC_INFO *info);
423 static int tiocmget(struct tty_struct *tty, struct file *file);
424 static int tiocmset(struct tty_struct *tty, struct file *file,
425 unsigned int set, unsigned int clear);
426 static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
427 static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
428 static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params, struct tty_struct *tty);
429 static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
430 static int set_txidle(MGSLPC_INFO *info, int idle_mode);
431 static int set_txenable(MGSLPC_INFO *info, int enable, struct tty_struct *tty);
432 static int tx_abort(MGSLPC_INFO *info);
433 static int set_rxenable(MGSLPC_INFO *info, int enable);
434 static int wait_events(MGSLPC_INFO *info, int __user *mask);
436 static MGSLPC_INFO *mgslpc_device_list = NULL;
437 static int mgslpc_device_count = 0;
440 * Set this param to non-zero to load eax with the
441 * .text section address and breakpoint on module load.
442 * This is useful for use with gdb and add-symbol-file command.
444 static int break_on_load=0;
447 * Driver major number, defaults to zero to get auto
448 * assigned major number. May be forced as module parameter.
450 static int ttymajor=0;
452 static int debug_level = 0;
453 static int maxframe[MAX_DEVICE_COUNT] = {0,};
455 module_param(break_on_load, bool, 0);
456 module_param(ttymajor, int, 0);
457 module_param(debug_level, int, 0);
458 module_param_array(maxframe, int, NULL, 0);
460 MODULE_LICENSE("GPL");
462 static char *driver_name = "SyncLink PC Card driver";
463 static char *driver_version = "$Revision: 4.34 $";
465 static struct tty_driver *serial_driver;
467 /* number of characters left in xmit buffer before we ask for more */
468 #define WAKEUP_CHARS 256
470 static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty);
471 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
473 /* PCMCIA prototypes */
475 static int mgslpc_config(struct pcmcia_device *link);
476 static void mgslpc_release(u_long arg);
477 static void mgslpc_detach(struct pcmcia_device *p_dev);
480 * 1st function defined in .text section. Calling this function in
481 * init_module() followed by a breakpoint allows a remote debugger
482 * (gdb) to get the .text address for the add-symbol-file command.
483 * This allows remote debugging of dynamically loadable modules.
485 static void* mgslpc_get_text_ptr(void)
487 return mgslpc_get_text_ptr;
491 * line discipline callback wrappers
493 * The wrappers maintain line discipline references
494 * while calling into the line discipline.
496 * ldisc_receive_buf - pass receive data to line discipline
499 static void ldisc_receive_buf(struct tty_struct *tty,
500 const __u8 *data, char *flags, int count)
502 struct tty_ldisc *ld;
505 ld = tty_ldisc_ref(tty);
507 if (ld->ops->receive_buf)
508 ld->ops->receive_buf(tty, data, flags, count);
513 static const struct tty_port_operations mgslpc_port_ops = {
514 .carrier_raised = carrier_raised,
518 static int mgslpc_probe(struct pcmcia_device *link)
523 if (debug_level >= DEBUG_LEVEL_INFO)
524 printk("mgslpc_attach\n");
526 info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
528 printk("Error can't allocate device instance data\n");
532 info->magic = MGSLPC_MAGIC;
533 tty_port_init(&info->port);
534 info->port.ops = &mgslpc_port_ops;
535 INIT_WORK(&info->task, bh_handler);
536 info->max_frame_size = 4096;
537 info->port.close_delay = 5*HZ/10;
538 info->port.closing_wait = 30*HZ;
539 init_waitqueue_head(&info->status_event_wait_q);
540 init_waitqueue_head(&info->event_wait_q);
541 spin_lock_init(&info->lock);
542 spin_lock_init(&info->netlock);
543 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
544 info->idle_mode = HDLC_TXIDLE_FLAGS;
545 info->imra_value = 0xffff;
546 info->imrb_value = 0xffff;
547 info->pim_value = 0xff;
552 /* Initialize the struct pcmcia_device structure */
554 link->conf.Attributes = 0;
555 link->conf.IntType = INT_MEMORY_AND_IO;
557 ret = mgslpc_config(link);
561 mgslpc_add_device(info);
566 /* Card has been inserted.
569 static int mgslpc_ioprobe(struct pcmcia_device *p_dev,
570 cistpl_cftable_entry_t *cfg,
571 cistpl_cftable_entry_t *dflt,
575 if (cfg->io.nwin > 0) {
576 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
577 if (!(cfg->io.flags & CISTPL_IO_8BIT))
578 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
579 if (!(cfg->io.flags & CISTPL_IO_16BIT))
580 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
581 p_dev->io.IOAddrLines = cfg->io.flags & CISTPL_IO_LINES_MASK;
582 p_dev->io.BasePort1 = cfg->io.win[0].base;
583 p_dev->io.NumPorts1 = cfg->io.win[0].len;
584 return pcmcia_request_io(p_dev, &p_dev->io);
589 static int mgslpc_config(struct pcmcia_device *link)
591 MGSLPC_INFO *info = link->priv;
594 if (debug_level >= DEBUG_LEVEL_INFO)
595 printk("mgslpc_config(0x%p)\n", link);
597 ret = pcmcia_loop_config(link, mgslpc_ioprobe, NULL);
601 link->conf.Attributes = CONF_ENABLE_IRQ;
602 link->conf.IntType = INT_MEMORY_AND_IO;
603 link->conf.ConfigIndex = 8;
604 link->conf.Present = PRESENT_OPTION;
606 ret = pcmcia_request_irq(link, mgslpc_isr);
609 ret = pcmcia_request_configuration(link, &link->conf);
613 info->io_base = link->io.BasePort1;
614 info->irq_level = link->irq;
616 dev_info(&link->dev, "index 0x%02x:",
617 link->conf.ConfigIndex);
618 if (link->conf.Attributes & CONF_ENABLE_IRQ)
619 printk(", irq %d", link->irq);
620 if (link->io.NumPorts1)
621 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
622 link->io.BasePort1+link->io.NumPorts1-1);
627 mgslpc_release((u_long)link);
631 /* Card has been removed.
632 * Unregister device and release PCMCIA configuration.
633 * If device is open, postpone until it is closed.
635 static void mgslpc_release(u_long arg)
637 struct pcmcia_device *link = (struct pcmcia_device *)arg;
639 if (debug_level >= DEBUG_LEVEL_INFO)
640 printk("mgslpc_release(0x%p)\n", link);
642 pcmcia_disable_device(link);
645 static void mgslpc_detach(struct pcmcia_device *link)
647 if (debug_level >= DEBUG_LEVEL_INFO)
648 printk("mgslpc_detach(0x%p)\n", link);
650 ((MGSLPC_INFO *)link->priv)->stop = 1;
651 mgslpc_release((u_long)link);
653 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
656 static int mgslpc_suspend(struct pcmcia_device *link)
658 MGSLPC_INFO *info = link->priv;
665 static int mgslpc_resume(struct pcmcia_device *link)
667 MGSLPC_INFO *info = link->priv;
675 static inline bool mgslpc_paranoia_check(MGSLPC_INFO *info,
676 char *name, const char *routine)
678 #ifdef MGSLPC_PARANOIA_CHECK
679 static const char *badmagic =
680 "Warning: bad magic number for mgsl struct (%s) in %s\n";
681 static const char *badinfo =
682 "Warning: null mgslpc_info for (%s) in %s\n";
685 printk(badinfo, name, routine);
688 if (info->magic != MGSLPC_MAGIC) {
689 printk(badmagic, name, routine);
700 #define CMD_RXFIFO BIT7 // release current rx FIFO
701 #define CMD_RXRESET BIT6 // receiver reset
702 #define CMD_RXFIFO_READ BIT5
703 #define CMD_START_TIMER BIT4
704 #define CMD_TXFIFO BIT3 // release current tx FIFO
705 #define CMD_TXEOM BIT1 // transmit end message
706 #define CMD_TXRESET BIT0 // transmit reset
708 static bool wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
711 /* wait for command completion */
712 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
720 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
722 wait_command_complete(info, channel);
723 write_reg(info, (unsigned char) (channel + CMDR), cmd);
726 static void tx_pause(struct tty_struct *tty)
728 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
731 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
733 if (debug_level >= DEBUG_LEVEL_INFO)
734 printk("tx_pause(%s)\n",info->device_name);
736 spin_lock_irqsave(&info->lock,flags);
737 if (info->tx_enabled)
739 spin_unlock_irqrestore(&info->lock,flags);
742 static void tx_release(struct tty_struct *tty)
744 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
747 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
749 if (debug_level >= DEBUG_LEVEL_INFO)
750 printk("tx_release(%s)\n",info->device_name);
752 spin_lock_irqsave(&info->lock,flags);
753 if (!info->tx_enabled)
755 spin_unlock_irqrestore(&info->lock,flags);
758 /* Return next bottom half action to perform.
759 * or 0 if nothing to do.
761 static int bh_action(MGSLPC_INFO *info)
766 spin_lock_irqsave(&info->lock,flags);
768 if (info->pending_bh & BH_RECEIVE) {
769 info->pending_bh &= ~BH_RECEIVE;
771 } else if (info->pending_bh & BH_TRANSMIT) {
772 info->pending_bh &= ~BH_TRANSMIT;
774 } else if (info->pending_bh & BH_STATUS) {
775 info->pending_bh &= ~BH_STATUS;
780 /* Mark BH routine as complete */
781 info->bh_running = false;
782 info->bh_requested = false;
785 spin_unlock_irqrestore(&info->lock,flags);
790 static void bh_handler(struct work_struct *work)
792 MGSLPC_INFO *info = container_of(work, MGSLPC_INFO, task);
793 struct tty_struct *tty;
799 if (debug_level >= DEBUG_LEVEL_BH)
800 printk( "%s(%d):bh_handler(%s) entry\n",
801 __FILE__,__LINE__,info->device_name);
803 info->bh_running = true;
804 tty = tty_port_tty_get(&info->port);
806 while((action = bh_action(info)) != 0) {
808 /* Process work item */
809 if ( debug_level >= DEBUG_LEVEL_BH )
810 printk( "%s(%d):bh_handler() work item action=%d\n",
811 __FILE__,__LINE__,action);
816 while(rx_get_frame(info, tty));
819 bh_transmit(info, tty);
825 /* unknown work item ID */
826 printk("Unknown work item ID=%08X!\n", action);
832 if (debug_level >= DEBUG_LEVEL_BH)
833 printk( "%s(%d):bh_handler(%s) exit\n",
834 __FILE__,__LINE__,info->device_name);
837 static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty)
839 if (debug_level >= DEBUG_LEVEL_BH)
840 printk("bh_transmit() entry on %s\n", info->device_name);
846 static void bh_status(MGSLPC_INFO *info)
848 info->ri_chkcount = 0;
849 info->dsr_chkcount = 0;
850 info->dcd_chkcount = 0;
851 info->cts_chkcount = 0;
854 /* eom: non-zero = end of frame */
855 static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
857 unsigned char data[2];
858 unsigned char fifo_count, read_count, i;
859 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
861 if (debug_level >= DEBUG_LEVEL_ISR)
862 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
864 if (!info->rx_enabled)
867 if (info->rx_frame_count >= info->rx_buf_count) {
868 /* no more free buffers */
869 issue_command(info, CHA, CMD_RXRESET);
870 info->pending_bh |= BH_RECEIVE;
871 info->rx_overflow = true;
872 info->icount.buf_overrun++;
877 /* end of frame, get FIFO count from RBCL register */
878 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
884 if (fifo_count == 1) {
886 data[0] = read_reg(info, CHA + RXFIFO);
889 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
891 fifo_count -= read_count;
892 if (!fifo_count && eom)
893 buf->status = data[--read_count];
895 for (i = 0; i < read_count; i++) {
896 if (buf->count >= info->max_frame_size) {
897 /* frame too large, reset receiver and reset current buffer */
898 issue_command(info, CHA, CMD_RXRESET);
902 *(buf->data + buf->count) = data[i];
905 } while (fifo_count);
908 info->pending_bh |= BH_RECEIVE;
909 info->rx_frame_count++;
911 if (info->rx_put >= info->rx_buf_count)
914 issue_command(info, CHA, CMD_RXFIFO);
917 static void rx_ready_async(MGSLPC_INFO *info, int tcd, struct tty_struct *tty)
919 unsigned char data, status, flag;
922 struct mgsl_icount *icount = &info->icount;
925 /* early termination, get FIFO count from RBCL register */
926 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
928 /* Zero fifo count could mean 0 or 32 bytes available.
929 * If BIT5 of STAR is set then at least 1 byte is available.
931 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
936 tty_buffer_request_room(tty, fifo_count);
937 /* Flush received async data to receive data buffer. */
939 data = read_reg(info, CHA + RXFIFO);
940 status = read_reg(info, CHA + RXFIFO);
946 // if no frameing/crc error then save data
948 // BIT6:framing error
950 if (status & (BIT7 + BIT6)) {
956 /* discard char if tty control flags say so */
957 if (status & info->ignore_status_mask)
960 status &= info->read_status_mask;
964 else if (status & BIT6)
967 work += tty_insert_flip_char(tty, data, flag);
969 issue_command(info, CHA, CMD_RXFIFO);
971 if (debug_level >= DEBUG_LEVEL_ISR) {
972 printk("%s(%d):rx_ready_async",
974 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
975 __FILE__,__LINE__,icount->rx,icount->brk,
976 icount->parity,icount->frame,icount->overrun);
980 tty_flip_buffer_push(tty);
984 static void tx_done(MGSLPC_INFO *info, struct tty_struct *tty)
986 if (!info->tx_active)
989 info->tx_active = false;
990 info->tx_aborting = false;
992 if (info->params.mode == MGSL_MODE_ASYNC)
995 info->tx_count = info->tx_put = info->tx_get = 0;
996 del_timer(&info->tx_timer);
998 if (info->drop_rts_on_tx_done) {
1000 if (info->serial_signals & SerialSignal_RTS) {
1001 info->serial_signals &= ~SerialSignal_RTS;
1004 info->drop_rts_on_tx_done = false;
1007 #if SYNCLINK_GENERIC_HDLC
1009 hdlcdev_tx_done(info);
1013 if (tty->stopped || tty->hw_stopped) {
1017 info->pending_bh |= BH_TRANSMIT;
1021 static void tx_ready(MGSLPC_INFO *info, struct tty_struct *tty)
1023 unsigned char fifo_count = 32;
1026 if (debug_level >= DEBUG_LEVEL_ISR)
1027 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1029 if (info->params.mode == MGSL_MODE_HDLC) {
1030 if (!info->tx_active)
1033 if (tty->stopped || tty->hw_stopped) {
1037 if (!info->tx_count)
1038 info->tx_active = false;
1041 if (!info->tx_count)
1044 while (info->tx_count && fifo_count) {
1045 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
1048 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1050 write_reg16(info, CHA + TXFIFO,
1051 *((unsigned short*)(info->tx_buf + info->tx_get)));
1053 info->tx_count -= c;
1054 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1058 if (info->params.mode == MGSL_MODE_ASYNC) {
1059 if (info->tx_count < WAKEUP_CHARS)
1060 info->pending_bh |= BH_TRANSMIT;
1061 issue_command(info, CHA, CMD_TXFIFO);
1064 issue_command(info, CHA, CMD_TXFIFO);
1066 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1070 static void cts_change(MGSLPC_INFO *info, struct tty_struct *tty)
1073 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1074 irq_disable(info, CHB, IRQ_CTS);
1076 if (info->serial_signals & SerialSignal_CTS)
1077 info->input_signal_events.cts_up++;
1079 info->input_signal_events.cts_down++;
1080 wake_up_interruptible(&info->status_event_wait_q);
1081 wake_up_interruptible(&info->event_wait_q);
1083 if (info->port.flags & ASYNC_CTS_FLOW) {
1084 if (tty->hw_stopped) {
1085 if (info->serial_signals & SerialSignal_CTS) {
1086 if (debug_level >= DEBUG_LEVEL_ISR)
1087 printk("CTS tx start...");
1089 tty->hw_stopped = 0;
1090 tx_start(info, tty);
1091 info->pending_bh |= BH_TRANSMIT;
1095 if (!(info->serial_signals & SerialSignal_CTS)) {
1096 if (debug_level >= DEBUG_LEVEL_ISR)
1097 printk("CTS tx stop...");
1099 tty->hw_stopped = 1;
1104 info->pending_bh |= BH_STATUS;
1107 static void dcd_change(MGSLPC_INFO *info, struct tty_struct *tty)
1110 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1111 irq_disable(info, CHB, IRQ_DCD);
1113 if (info->serial_signals & SerialSignal_DCD) {
1114 info->input_signal_events.dcd_up++;
1117 info->input_signal_events.dcd_down++;
1118 #if SYNCLINK_GENERIC_HDLC
1119 if (info->netcount) {
1120 if (info->serial_signals & SerialSignal_DCD)
1121 netif_carrier_on(info->netdev);
1123 netif_carrier_off(info->netdev);
1126 wake_up_interruptible(&info->status_event_wait_q);
1127 wake_up_interruptible(&info->event_wait_q);
1129 if (info->port.flags & ASYNC_CHECK_CD) {
1130 if (debug_level >= DEBUG_LEVEL_ISR)
1131 printk("%s CD now %s...", info->device_name,
1132 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1133 if (info->serial_signals & SerialSignal_DCD)
1134 wake_up_interruptible(&info->port.open_wait);
1136 if (debug_level >= DEBUG_LEVEL_ISR)
1137 printk("doing serial hangup...");
1142 info->pending_bh |= BH_STATUS;
1145 static void dsr_change(MGSLPC_INFO *info)
1148 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1149 port_irq_disable(info, PVR_DSR);
1151 if (info->serial_signals & SerialSignal_DSR)
1152 info->input_signal_events.dsr_up++;
1154 info->input_signal_events.dsr_down++;
1155 wake_up_interruptible(&info->status_event_wait_q);
1156 wake_up_interruptible(&info->event_wait_q);
1157 info->pending_bh |= BH_STATUS;
1160 static void ri_change(MGSLPC_INFO *info)
1163 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1164 port_irq_disable(info, PVR_RI);
1166 if (info->serial_signals & SerialSignal_RI)
1167 info->input_signal_events.ri_up++;
1169 info->input_signal_events.ri_down++;
1170 wake_up_interruptible(&info->status_event_wait_q);
1171 wake_up_interruptible(&info->event_wait_q);
1172 info->pending_bh |= BH_STATUS;
1175 /* Interrupt service routine entry point.
1179 * irq interrupt number that caused interrupt
1180 * dev_id device ID supplied during interrupt registration
1182 static irqreturn_t mgslpc_isr(int dummy, void *dev_id)
1184 MGSLPC_INFO *info = dev_id;
1185 struct tty_struct *tty;
1187 unsigned char gis, pis;
1190 if (debug_level >= DEBUG_LEVEL_ISR)
1191 printk("mgslpc_isr(%d) entry.\n", info->irq_level);
1193 if (!(info->p_dev->_locked))
1196 tty = tty_port_tty_get(&info->port);
1198 spin_lock(&info->lock);
1200 while ((gis = read_reg(info, CHA + GIS))) {
1201 if (debug_level >= DEBUG_LEVEL_ISR)
1202 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1204 if ((gis & 0x70) || count > 1000) {
1205 printk("synclink_cs:hardware failed or ejected\n");
1210 if (gis & (BIT1 + BIT0)) {
1211 isr = read_reg16(info, CHB + ISR);
1213 dcd_change(info, tty);
1215 cts_change(info, tty);
1217 if (gis & (BIT3 + BIT2))
1219 isr = read_reg16(info, CHA + ISR);
1220 if (isr & IRQ_TIMER) {
1221 info->irq_occurred = true;
1222 irq_disable(info, CHA, IRQ_TIMER);
1226 if (isr & IRQ_EXITHUNT) {
1227 info->icount.exithunt++;
1228 wake_up_interruptible(&info->event_wait_q);
1230 if (isr & IRQ_BREAK_ON) {
1232 if (info->port.flags & ASYNC_SAK)
1235 if (isr & IRQ_RXTIME) {
1236 issue_command(info, CHA, CMD_RXFIFO_READ);
1238 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1239 if (info->params.mode == MGSL_MODE_HDLC)
1240 rx_ready_hdlc(info, isr & IRQ_RXEOM);
1242 rx_ready_async(info, isr & IRQ_RXEOM, tty);
1246 if (isr & IRQ_UNDERRUN) {
1247 if (info->tx_aborting)
1248 info->icount.txabort++;
1250 info->icount.txunder++;
1253 else if (isr & IRQ_ALLSENT) {
1254 info->icount.txok++;
1257 else if (isr & IRQ_TXFIFO)
1258 tx_ready(info, tty);
1261 pis = read_reg(info, CHA + PIS);
1269 /* Request bottom half processing if there's something
1270 * for it to do and the bh is not already running
1273 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1274 if ( debug_level >= DEBUG_LEVEL_ISR )
1275 printk("%s(%d):%s queueing bh task.\n",
1276 __FILE__,__LINE__,info->device_name);
1277 schedule_work(&info->task);
1278 info->bh_requested = true;
1281 spin_unlock(&info->lock);
1284 if (debug_level >= DEBUG_LEVEL_ISR)
1285 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1286 __FILE__, __LINE__, info->irq_level);
1291 /* Initialize and start device.
1293 static int startup(MGSLPC_INFO * info, struct tty_struct *tty)
1297 if (debug_level >= DEBUG_LEVEL_INFO)
1298 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1300 if (info->port.flags & ASYNC_INITIALIZED)
1303 if (!info->tx_buf) {
1304 /* allocate a page of memory for a transmit buffer */
1305 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1306 if (!info->tx_buf) {
1307 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1308 __FILE__,__LINE__,info->device_name);
1313 info->pending_bh = 0;
1315 memset(&info->icount, 0, sizeof(info->icount));
1317 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
1319 /* Allocate and claim adapter resources */
1320 retval = claim_resources(info);
1322 /* perform existance check and diagnostics */
1324 retval = adapter_test(info);
1327 if (capable(CAP_SYS_ADMIN) && tty)
1328 set_bit(TTY_IO_ERROR, &tty->flags);
1329 release_resources(info);
1333 /* program hardware for current parameters */
1334 mgslpc_change_params(info, tty);
1337 clear_bit(TTY_IO_ERROR, &tty->flags);
1339 info->port.flags |= ASYNC_INITIALIZED;
1344 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1346 static void shutdown(MGSLPC_INFO * info, struct tty_struct *tty)
1348 unsigned long flags;
1350 if (!(info->port.flags & ASYNC_INITIALIZED))
1353 if (debug_level >= DEBUG_LEVEL_INFO)
1354 printk("%s(%d):mgslpc_shutdown(%s)\n",
1355 __FILE__,__LINE__, info->device_name );
1357 /* clear status wait queue because status changes */
1358 /* can't happen after shutting down the hardware */
1359 wake_up_interruptible(&info->status_event_wait_q);
1360 wake_up_interruptible(&info->event_wait_q);
1362 del_timer_sync(&info->tx_timer);
1365 free_page((unsigned long) info->tx_buf);
1366 info->tx_buf = NULL;
1369 spin_lock_irqsave(&info->lock,flags);
1374 /* TODO:disable interrupts instead of reset to preserve signal states */
1377 if (!tty || tty->termios->c_cflag & HUPCL) {
1378 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1382 spin_unlock_irqrestore(&info->lock,flags);
1384 release_resources(info);
1387 set_bit(TTY_IO_ERROR, &tty->flags);
1389 info->port.flags &= ~ASYNC_INITIALIZED;
1392 static void mgslpc_program_hw(MGSLPC_INFO *info, struct tty_struct *tty)
1394 unsigned long flags;
1396 spin_lock_irqsave(&info->lock,flags);
1400 info->tx_count = info->tx_put = info->tx_get = 0;
1402 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1409 info->dcd_chkcount = 0;
1410 info->cts_chkcount = 0;
1411 info->ri_chkcount = 0;
1412 info->dsr_chkcount = 0;
1414 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1415 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1418 if (info->netcount || (tty && (tty->termios->c_cflag & CREAD)))
1421 spin_unlock_irqrestore(&info->lock,flags);
1424 /* Reconfigure adapter based on new parameters
1426 static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty)
1431 if (!tty || !tty->termios)
1434 if (debug_level >= DEBUG_LEVEL_INFO)
1435 printk("%s(%d):mgslpc_change_params(%s)\n",
1436 __FILE__,__LINE__, info->device_name );
1438 cflag = tty->termios->c_cflag;
1440 /* if B0 rate (hangup) specified then negate DTR and RTS */
1441 /* otherwise assert DTR and RTS */
1443 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1445 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1447 /* byte size and parity */
1449 switch (cflag & CSIZE) {
1450 case CS5: info->params.data_bits = 5; break;
1451 case CS6: info->params.data_bits = 6; break;
1452 case CS7: info->params.data_bits = 7; break;
1453 case CS8: info->params.data_bits = 8; break;
1454 default: info->params.data_bits = 7; break;
1458 info->params.stop_bits = 2;
1460 info->params.stop_bits = 1;
1462 info->params.parity = ASYNC_PARITY_NONE;
1463 if (cflag & PARENB) {
1465 info->params.parity = ASYNC_PARITY_ODD;
1467 info->params.parity = ASYNC_PARITY_EVEN;
1470 info->params.parity = ASYNC_PARITY_SPACE;
1474 /* calculate number of jiffies to transmit a full
1475 * FIFO (32 bytes) at specified data rate
1477 bits_per_char = info->params.data_bits +
1478 info->params.stop_bits + 1;
1480 /* if port data rate is set to 460800 or less then
1481 * allow tty settings to override, otherwise keep the
1482 * current data rate.
1484 if (info->params.data_rate <= 460800) {
1485 info->params.data_rate = tty_get_baud_rate(tty);
1488 if ( info->params.data_rate ) {
1489 info->timeout = (32*HZ*bits_per_char) /
1490 info->params.data_rate;
1492 info->timeout += HZ/50; /* Add .02 seconds of slop */
1494 if (cflag & CRTSCTS)
1495 info->port.flags |= ASYNC_CTS_FLOW;
1497 info->port.flags &= ~ASYNC_CTS_FLOW;
1500 info->port.flags &= ~ASYNC_CHECK_CD;
1502 info->port.flags |= ASYNC_CHECK_CD;
1504 /* process tty input control flags */
1506 info->read_status_mask = 0;
1508 info->read_status_mask |= BIT7 | BIT6;
1510 info->ignore_status_mask |= BIT7 | BIT6;
1512 mgslpc_program_hw(info, tty);
1515 /* Add a character to the transmit buffer
1517 static int mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1519 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1520 unsigned long flags;
1522 if (debug_level >= DEBUG_LEVEL_INFO) {
1523 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1524 __FILE__,__LINE__,ch,info->device_name);
1527 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1533 spin_lock_irqsave(&info->lock,flags);
1535 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1536 if (info->tx_count < TXBUFSIZE - 1) {
1537 info->tx_buf[info->tx_put++] = ch;
1538 info->tx_put &= TXBUFSIZE-1;
1543 spin_unlock_irqrestore(&info->lock,flags);
1547 /* Enable transmitter so remaining characters in the
1548 * transmit buffer are sent.
1550 static void mgslpc_flush_chars(struct tty_struct *tty)
1552 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1553 unsigned long flags;
1555 if (debug_level >= DEBUG_LEVEL_INFO)
1556 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1557 __FILE__,__LINE__,info->device_name,info->tx_count);
1559 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1562 if (info->tx_count <= 0 || tty->stopped ||
1563 tty->hw_stopped || !info->tx_buf)
1566 if (debug_level >= DEBUG_LEVEL_INFO)
1567 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1568 __FILE__,__LINE__,info->device_name);
1570 spin_lock_irqsave(&info->lock,flags);
1571 if (!info->tx_active)
1572 tx_start(info, tty);
1573 spin_unlock_irqrestore(&info->lock,flags);
1576 /* Send a block of data
1580 * tty pointer to tty information structure
1581 * buf pointer to buffer containing send data
1582 * count size of send data in bytes
1584 * Returns: number of characters written
1586 static int mgslpc_write(struct tty_struct * tty,
1587 const unsigned char *buf, int count)
1590 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1591 unsigned long flags;
1593 if (debug_level >= DEBUG_LEVEL_INFO)
1594 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1595 __FILE__,__LINE__,info->device_name,count);
1597 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1601 if (info->params.mode == MGSL_MODE_HDLC) {
1602 if (count > TXBUFSIZE) {
1606 if (info->tx_active)
1608 else if (info->tx_count)
1614 min(TXBUFSIZE - info->tx_count - 1,
1615 TXBUFSIZE - info->tx_put));
1619 memcpy(info->tx_buf + info->tx_put, buf, c);
1621 spin_lock_irqsave(&info->lock,flags);
1622 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1623 info->tx_count += c;
1624 spin_unlock_irqrestore(&info->lock,flags);
1631 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1632 spin_lock_irqsave(&info->lock,flags);
1633 if (!info->tx_active)
1634 tx_start(info, tty);
1635 spin_unlock_irqrestore(&info->lock,flags);
1638 if (debug_level >= DEBUG_LEVEL_INFO)
1639 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1640 __FILE__,__LINE__,info->device_name,ret);
1644 /* Return the count of free bytes in transmit buffer
1646 static int mgslpc_write_room(struct tty_struct *tty)
1648 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1651 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1654 if (info->params.mode == MGSL_MODE_HDLC) {
1655 /* HDLC (frame oriented) mode */
1656 if (info->tx_active)
1659 return HDLC_MAX_FRAME_SIZE;
1661 ret = TXBUFSIZE - info->tx_count - 1;
1666 if (debug_level >= DEBUG_LEVEL_INFO)
1667 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1668 __FILE__,__LINE__, info->device_name, ret);
1672 /* Return the count of bytes in transmit buffer
1674 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1676 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1679 if (debug_level >= DEBUG_LEVEL_INFO)
1680 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1681 __FILE__,__LINE__, info->device_name );
1683 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1686 if (info->params.mode == MGSL_MODE_HDLC)
1687 rc = info->tx_active ? info->max_frame_size : 0;
1689 rc = info->tx_count;
1691 if (debug_level >= DEBUG_LEVEL_INFO)
1692 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1693 __FILE__,__LINE__, info->device_name, rc);
1698 /* Discard all data in the send buffer
1700 static void mgslpc_flush_buffer(struct tty_struct *tty)
1702 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1703 unsigned long flags;
1705 if (debug_level >= DEBUG_LEVEL_INFO)
1706 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1707 __FILE__,__LINE__, info->device_name );
1709 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1712 spin_lock_irqsave(&info->lock,flags);
1713 info->tx_count = info->tx_put = info->tx_get = 0;
1714 del_timer(&info->tx_timer);
1715 spin_unlock_irqrestore(&info->lock,flags);
1717 wake_up_interruptible(&tty->write_wait);
1721 /* Send a high-priority XON/XOFF character
1723 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1725 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1726 unsigned long flags;
1728 if (debug_level >= DEBUG_LEVEL_INFO)
1729 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1730 __FILE__,__LINE__, info->device_name, ch );
1732 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1737 spin_lock_irqsave(&info->lock,flags);
1738 if (!info->tx_enabled)
1739 tx_start(info, tty);
1740 spin_unlock_irqrestore(&info->lock,flags);
1744 /* Signal remote device to throttle send data (our receive data)
1746 static void mgslpc_throttle(struct tty_struct * tty)
1748 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1749 unsigned long flags;
1751 if (debug_level >= DEBUG_LEVEL_INFO)
1752 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1753 __FILE__,__LINE__, info->device_name );
1755 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1759 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1761 if (tty->termios->c_cflag & CRTSCTS) {
1762 spin_lock_irqsave(&info->lock,flags);
1763 info->serial_signals &= ~SerialSignal_RTS;
1765 spin_unlock_irqrestore(&info->lock,flags);
1769 /* Signal remote device to stop throttling send data (our receive data)
1771 static void mgslpc_unthrottle(struct tty_struct * tty)
1773 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1774 unsigned long flags;
1776 if (debug_level >= DEBUG_LEVEL_INFO)
1777 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1778 __FILE__,__LINE__, info->device_name );
1780 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1787 mgslpc_send_xchar(tty, START_CHAR(tty));
1790 if (tty->termios->c_cflag & CRTSCTS) {
1791 spin_lock_irqsave(&info->lock,flags);
1792 info->serial_signals |= SerialSignal_RTS;
1794 spin_unlock_irqrestore(&info->lock,flags);
1798 /* get the current serial statistics
1800 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1803 if (debug_level >= DEBUG_LEVEL_INFO)
1804 printk("get_params(%s)\n", info->device_name);
1806 memset(&info->icount, 0, sizeof(info->icount));
1808 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1815 /* get the current serial parameters
1817 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1820 if (debug_level >= DEBUG_LEVEL_INFO)
1821 printk("get_params(%s)\n", info->device_name);
1822 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1828 /* set the serial parameters
1832 * info pointer to device instance data
1833 * new_params user buffer containing new serial params
1835 * Returns: 0 if success, otherwise error code
1837 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params, struct tty_struct *tty)
1839 unsigned long flags;
1840 MGSL_PARAMS tmp_params;
1843 if (debug_level >= DEBUG_LEVEL_INFO)
1844 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1845 info->device_name );
1846 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1848 if ( debug_level >= DEBUG_LEVEL_INFO )
1849 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1850 __FILE__,__LINE__,info->device_name);
1854 spin_lock_irqsave(&info->lock,flags);
1855 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1856 spin_unlock_irqrestore(&info->lock,flags);
1858 mgslpc_change_params(info, tty);
1863 static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1866 if (debug_level >= DEBUG_LEVEL_INFO)
1867 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1868 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1874 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1876 unsigned long flags;
1877 if (debug_level >= DEBUG_LEVEL_INFO)
1878 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1879 spin_lock_irqsave(&info->lock,flags);
1880 info->idle_mode = idle_mode;
1882 spin_unlock_irqrestore(&info->lock,flags);
1886 static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1889 if (debug_level >= DEBUG_LEVEL_INFO)
1890 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1891 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1897 static int set_interface(MGSLPC_INFO * info, int if_mode)
1899 unsigned long flags;
1901 if (debug_level >= DEBUG_LEVEL_INFO)
1902 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1903 spin_lock_irqsave(&info->lock,flags);
1904 info->if_mode = if_mode;
1906 val = read_reg(info, PVR) & 0x0f;
1907 switch (info->if_mode)
1909 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
1910 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
1911 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
1913 write_reg(info, PVR, val);
1915 spin_unlock_irqrestore(&info->lock,flags);
1919 static int set_txenable(MGSLPC_INFO * info, int enable, struct tty_struct *tty)
1921 unsigned long flags;
1923 if (debug_level >= DEBUG_LEVEL_INFO)
1924 printk("set_txenable(%s,%d)\n", info->device_name, enable);
1926 spin_lock_irqsave(&info->lock,flags);
1928 if (!info->tx_enabled)
1929 tx_start(info, tty);
1931 if (info->tx_enabled)
1934 spin_unlock_irqrestore(&info->lock,flags);
1938 static int tx_abort(MGSLPC_INFO * info)
1940 unsigned long flags;
1942 if (debug_level >= DEBUG_LEVEL_INFO)
1943 printk("tx_abort(%s)\n", info->device_name);
1945 spin_lock_irqsave(&info->lock,flags);
1946 if (info->tx_active && info->tx_count &&
1947 info->params.mode == MGSL_MODE_HDLC) {
1948 /* clear data count so FIFO is not filled on next IRQ.
1949 * This results in underrun and abort transmission.
1951 info->tx_count = info->tx_put = info->tx_get = 0;
1952 info->tx_aborting = true;
1954 spin_unlock_irqrestore(&info->lock,flags);
1958 static int set_rxenable(MGSLPC_INFO * info, int enable)
1960 unsigned long flags;
1962 if (debug_level >= DEBUG_LEVEL_INFO)
1963 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
1965 spin_lock_irqsave(&info->lock,flags);
1967 if (!info->rx_enabled)
1970 if (info->rx_enabled)
1973 spin_unlock_irqrestore(&info->lock,flags);
1977 /* wait for specified event to occur
1979 * Arguments: info pointer to device instance data
1980 * mask pointer to bitmask of events to wait for
1981 * Return Value: 0 if successful and bit mask updated with
1982 * of events triggerred,
1983 * otherwise error code
1985 static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
1987 unsigned long flags;
1990 struct mgsl_icount cprev, cnow;
1993 struct _input_signal_events oldsigs, newsigs;
1994 DECLARE_WAITQUEUE(wait, current);
1996 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2000 if (debug_level >= DEBUG_LEVEL_INFO)
2001 printk("wait_events(%s,%d)\n", info->device_name, mask);
2003 spin_lock_irqsave(&info->lock,flags);
2005 /* return immediately if state matches requested events */
2007 s = info->serial_signals;
2009 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2010 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2011 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2012 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2014 spin_unlock_irqrestore(&info->lock,flags);
2018 /* save current irq counts */
2019 cprev = info->icount;
2020 oldsigs = info->input_signal_events;
2022 if ((info->params.mode == MGSL_MODE_HDLC) &&
2023 (mask & MgslEvent_ExitHuntMode))
2024 irq_enable(info, CHA, IRQ_EXITHUNT);
2026 set_current_state(TASK_INTERRUPTIBLE);
2027 add_wait_queue(&info->event_wait_q, &wait);
2029 spin_unlock_irqrestore(&info->lock,flags);
2034 if (signal_pending(current)) {
2039 /* get current irq counts */
2040 spin_lock_irqsave(&info->lock,flags);
2041 cnow = info->icount;
2042 newsigs = info->input_signal_events;
2043 set_current_state(TASK_INTERRUPTIBLE);
2044 spin_unlock_irqrestore(&info->lock,flags);
2046 /* if no change, wait aborted for some reason */
2047 if (newsigs.dsr_up == oldsigs.dsr_up &&
2048 newsigs.dsr_down == oldsigs.dsr_down &&
2049 newsigs.dcd_up == oldsigs.dcd_up &&
2050 newsigs.dcd_down == oldsigs.dcd_down &&
2051 newsigs.cts_up == oldsigs.cts_up &&
2052 newsigs.cts_down == oldsigs.cts_down &&
2053 newsigs.ri_up == oldsigs.ri_up &&
2054 newsigs.ri_down == oldsigs.ri_down &&
2055 cnow.exithunt == cprev.exithunt &&
2056 cnow.rxidle == cprev.rxidle) {
2062 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2063 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2064 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2065 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2066 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2067 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2068 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2069 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2070 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2071 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2079 remove_wait_queue(&info->event_wait_q, &wait);
2080 set_current_state(TASK_RUNNING);
2082 if (mask & MgslEvent_ExitHuntMode) {
2083 spin_lock_irqsave(&info->lock,flags);
2084 if (!waitqueue_active(&info->event_wait_q))
2085 irq_disable(info, CHA, IRQ_EXITHUNT);
2086 spin_unlock_irqrestore(&info->lock,flags);
2090 PUT_USER(rc, events, mask_ptr);
2094 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2096 unsigned long flags;
2098 struct mgsl_icount cprev, cnow;
2099 DECLARE_WAITQUEUE(wait, current);
2101 /* save current irq counts */
2102 spin_lock_irqsave(&info->lock,flags);
2103 cprev = info->icount;
2104 add_wait_queue(&info->status_event_wait_q, &wait);
2105 set_current_state(TASK_INTERRUPTIBLE);
2106 spin_unlock_irqrestore(&info->lock,flags);
2110 if (signal_pending(current)) {
2115 /* get new irq counts */
2116 spin_lock_irqsave(&info->lock,flags);
2117 cnow = info->icount;
2118 set_current_state(TASK_INTERRUPTIBLE);
2119 spin_unlock_irqrestore(&info->lock,flags);
2121 /* if no change, wait aborted for some reason */
2122 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2123 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2128 /* check for change in caller specified modem input */
2129 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2130 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2131 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2132 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2139 remove_wait_queue(&info->status_event_wait_q, &wait);
2140 set_current_state(TASK_RUNNING);
2144 /* return the state of the serial control and status signals
2146 static int tiocmget(struct tty_struct *tty, struct file *file)
2148 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2149 unsigned int result;
2150 unsigned long flags;
2152 spin_lock_irqsave(&info->lock,flags);
2154 spin_unlock_irqrestore(&info->lock,flags);
2156 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2157 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2158 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2159 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2160 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2161 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2163 if (debug_level >= DEBUG_LEVEL_INFO)
2164 printk("%s(%d):%s tiocmget() value=%08X\n",
2165 __FILE__,__LINE__, info->device_name, result );
2169 /* set modem control signals (DTR/RTS)
2171 static int tiocmset(struct tty_struct *tty, struct file *file,
2172 unsigned int set, unsigned int clear)
2174 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2175 unsigned long flags;
2177 if (debug_level >= DEBUG_LEVEL_INFO)
2178 printk("%s(%d):%s tiocmset(%x,%x)\n",
2179 __FILE__,__LINE__,info->device_name, set, clear);
2181 if (set & TIOCM_RTS)
2182 info->serial_signals |= SerialSignal_RTS;
2183 if (set & TIOCM_DTR)
2184 info->serial_signals |= SerialSignal_DTR;
2185 if (clear & TIOCM_RTS)
2186 info->serial_signals &= ~SerialSignal_RTS;
2187 if (clear & TIOCM_DTR)
2188 info->serial_signals &= ~SerialSignal_DTR;
2190 spin_lock_irqsave(&info->lock,flags);
2192 spin_unlock_irqrestore(&info->lock,flags);
2197 /* Set or clear transmit break condition
2199 * Arguments: tty pointer to tty instance data
2200 * break_state -1=set break condition, 0=clear
2202 static int mgslpc_break(struct tty_struct *tty, int break_state)
2204 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2205 unsigned long flags;
2207 if (debug_level >= DEBUG_LEVEL_INFO)
2208 printk("%s(%d):mgslpc_break(%s,%d)\n",
2209 __FILE__,__LINE__, info->device_name, break_state);
2211 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2214 spin_lock_irqsave(&info->lock,flags);
2215 if (break_state == -1)
2216 set_reg_bits(info, CHA+DAFO, BIT6);
2218 clear_reg_bits(info, CHA+DAFO, BIT6);
2219 spin_unlock_irqrestore(&info->lock,flags);
2223 /* Service an IOCTL request
2227 * tty pointer to tty instance data
2228 * file pointer to associated file object for device
2229 * cmd IOCTL command code
2230 * arg command argument/context
2232 * Return Value: 0 if success, otherwise error code
2234 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2235 unsigned int cmd, unsigned long arg)
2237 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2239 struct mgsl_icount cnow; /* kernel counter temps */
2240 struct serial_icounter_struct __user *p_cuser; /* user space */
2241 void __user *argp = (void __user *)arg;
2242 unsigned long flags;
2244 if (debug_level >= DEBUG_LEVEL_INFO)
2245 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2246 info->device_name, cmd );
2248 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2251 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2252 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2253 if (tty->flags & (1 << TTY_IO_ERROR))
2258 case MGSL_IOCGPARAMS:
2259 return get_params(info, argp);
2260 case MGSL_IOCSPARAMS:
2261 return set_params(info, argp, tty);
2262 case MGSL_IOCGTXIDLE:
2263 return get_txidle(info, argp);
2264 case MGSL_IOCSTXIDLE:
2265 return set_txidle(info, (int)arg);
2267 return get_interface(info, argp);
2269 return set_interface(info,(int)arg);
2270 case MGSL_IOCTXENABLE:
2271 return set_txenable(info,(int)arg, tty);
2272 case MGSL_IOCRXENABLE:
2273 return set_rxenable(info,(int)arg);
2274 case MGSL_IOCTXABORT:
2275 return tx_abort(info);
2276 case MGSL_IOCGSTATS:
2277 return get_stats(info, argp);
2278 case MGSL_IOCWAITEVENT:
2279 return wait_events(info, argp);
2281 return modem_input_wait(info,(int)arg);
2283 spin_lock_irqsave(&info->lock,flags);
2284 cnow = info->icount;
2285 spin_unlock_irqrestore(&info->lock,flags);
2287 PUT_USER(error,cnow.cts, &p_cuser->cts);
2288 if (error) return error;
2289 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2290 if (error) return error;
2291 PUT_USER(error,cnow.rng, &p_cuser->rng);
2292 if (error) return error;
2293 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2294 if (error) return error;
2295 PUT_USER(error,cnow.rx, &p_cuser->rx);
2296 if (error) return error;
2297 PUT_USER(error,cnow.tx, &p_cuser->tx);
2298 if (error) return error;
2299 PUT_USER(error,cnow.frame, &p_cuser->frame);
2300 if (error) return error;
2301 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2302 if (error) return error;
2303 PUT_USER(error,cnow.parity, &p_cuser->parity);
2304 if (error) return error;
2305 PUT_USER(error,cnow.brk, &p_cuser->brk);
2306 if (error) return error;
2307 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2308 if (error) return error;
2311 return -ENOIOCTLCMD;
2316 /* Set new termios settings
2320 * tty pointer to tty structure
2321 * termios pointer to buffer to hold returned old termios
2323 static void mgslpc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2325 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2326 unsigned long flags;
2328 if (debug_level >= DEBUG_LEVEL_INFO)
2329 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2330 tty->driver->name );
2332 /* just return if nothing has changed */
2333 if ((tty->termios->c_cflag == old_termios->c_cflag)
2334 && (RELEVANT_IFLAG(tty->termios->c_iflag)
2335 == RELEVANT_IFLAG(old_termios->c_iflag)))
2338 mgslpc_change_params(info, tty);
2340 /* Handle transition to B0 status */
2341 if (old_termios->c_cflag & CBAUD &&
2342 !(tty->termios->c_cflag & CBAUD)) {
2343 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2344 spin_lock_irqsave(&info->lock,flags);
2346 spin_unlock_irqrestore(&info->lock,flags);
2349 /* Handle transition away from B0 status */
2350 if (!(old_termios->c_cflag & CBAUD) &&
2351 tty->termios->c_cflag & CBAUD) {
2352 info->serial_signals |= SerialSignal_DTR;
2353 if (!(tty->termios->c_cflag & CRTSCTS) ||
2354 !test_bit(TTY_THROTTLED, &tty->flags)) {
2355 info->serial_signals |= SerialSignal_RTS;
2357 spin_lock_irqsave(&info->lock,flags);
2359 spin_unlock_irqrestore(&info->lock,flags);
2362 /* Handle turning off CRTSCTS */
2363 if (old_termios->c_cflag & CRTSCTS &&
2364 !(tty->termios->c_cflag & CRTSCTS)) {
2365 tty->hw_stopped = 0;
2370 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2372 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2373 struct tty_port *port = &info->port;
2375 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2378 if (debug_level >= DEBUG_LEVEL_INFO)
2379 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2380 __FILE__,__LINE__, info->device_name, port->count);
2382 WARN_ON(!port->count);
2384 if (tty_port_close_start(port, tty, filp) == 0)
2387 if (port->flags & ASYNC_INITIALIZED)
2388 mgslpc_wait_until_sent(tty, info->timeout);
2390 mgslpc_flush_buffer(tty);
2392 tty_ldisc_flush(tty);
2393 shutdown(info, tty);
2395 tty_port_close_end(port, tty);
2396 tty_port_tty_set(port, NULL);
2398 if (debug_level >= DEBUG_LEVEL_INFO)
2399 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2400 tty->driver->name, port->count);
2403 /* Wait until the transmitter is empty.
2405 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2407 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2408 unsigned long orig_jiffies, char_time;
2413 if (debug_level >= DEBUG_LEVEL_INFO)
2414 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2415 __FILE__,__LINE__, info->device_name );
2417 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2420 if (!(info->port.flags & ASYNC_INITIALIZED))
2423 orig_jiffies = jiffies;
2425 /* Set check interval to 1/5 of estimated time to
2426 * send a character, and make it at least 1. The check
2427 * interval should also be less than the timeout.
2428 * Note: use tight timings here to satisfy the NIST-PCTS.
2431 if ( info->params.data_rate ) {
2432 char_time = info->timeout/(32 * 5);
2439 char_time = min_t(unsigned long, char_time, timeout);
2441 if (info->params.mode == MGSL_MODE_HDLC) {
2442 while (info->tx_active) {
2443 msleep_interruptible(jiffies_to_msecs(char_time));
2444 if (signal_pending(current))
2446 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2450 while ((info->tx_count || info->tx_active) &&
2452 msleep_interruptible(jiffies_to_msecs(char_time));
2453 if (signal_pending(current))
2455 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2461 if (debug_level >= DEBUG_LEVEL_INFO)
2462 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2463 __FILE__,__LINE__, info->device_name );
2466 /* Called by tty_hangup() when a hangup is signaled.
2467 * This is the same as closing all open files for the port.
2469 static void mgslpc_hangup(struct tty_struct *tty)
2471 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2473 if (debug_level >= DEBUG_LEVEL_INFO)
2474 printk("%s(%d):mgslpc_hangup(%s)\n",
2475 __FILE__,__LINE__, info->device_name );
2477 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2480 mgslpc_flush_buffer(tty);
2481 shutdown(info, tty);
2482 tty_port_hangup(&info->port);
2485 static int carrier_raised(struct tty_port *port)
2487 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2488 unsigned long flags;
2490 spin_lock_irqsave(&info->lock,flags);
2492 spin_unlock_irqrestore(&info->lock,flags);
2494 if (info->serial_signals & SerialSignal_DCD)
2499 static void dtr_rts(struct tty_port *port, int onoff)
2501 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2502 unsigned long flags;
2504 spin_lock_irqsave(&info->lock,flags);
2506 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2508 info->serial_signals &= ~SerialSignal_RTS + SerialSignal_DTR;
2510 spin_unlock_irqrestore(&info->lock,flags);
2514 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2517 struct tty_port *port;
2519 unsigned long flags;
2521 /* verify range of specified line number */
2523 if ((line < 0) || (line >= mgslpc_device_count)) {
2524 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2525 __FILE__,__LINE__,line);
2529 /* find the info structure for the specified line */
2530 info = mgslpc_device_list;
2531 while(info && info->line != line)
2532 info = info->next_device;
2533 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2537 tty->driver_data = info;
2538 tty_port_tty_set(port, tty);
2540 if (debug_level >= DEBUG_LEVEL_INFO)
2541 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2542 __FILE__,__LINE__,tty->driver->name, port->count);
2544 /* If port is closing, signal caller to try again */
2545 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING){
2546 if (port->flags & ASYNC_CLOSING)
2547 interruptible_sleep_on(&port->close_wait);
2548 retval = ((port->flags & ASYNC_HUP_NOTIFY) ?
2549 -EAGAIN : -ERESTARTSYS);
2553 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2555 spin_lock_irqsave(&info->netlock, flags);
2556 if (info->netcount) {
2558 spin_unlock_irqrestore(&info->netlock, flags);
2561 spin_lock(&port->lock);
2563 spin_unlock(&port->lock);
2564 spin_unlock_irqrestore(&info->netlock, flags);
2566 if (port->count == 1) {
2567 /* 1st open on this device, init hardware */
2568 retval = startup(info, tty);
2573 retval = tty_port_block_til_ready(&info->port, tty, filp);
2575 if (debug_level >= DEBUG_LEVEL_INFO)
2576 printk("%s(%d):block_til_ready(%s) returned %d\n",
2577 __FILE__,__LINE__, info->device_name, retval);
2581 if (debug_level >= DEBUG_LEVEL_INFO)
2582 printk("%s(%d):mgslpc_open(%s) success\n",
2583 __FILE__,__LINE__, info->device_name);
2591 * /proc fs routines....
2594 static inline void line_info(struct seq_file *m, MGSLPC_INFO *info)
2597 unsigned long flags;
2599 seq_printf(m, "%s:io:%04X irq:%d",
2600 info->device_name, info->io_base, info->irq_level);
2602 /* output current serial signal states */
2603 spin_lock_irqsave(&info->lock,flags);
2605 spin_unlock_irqrestore(&info->lock,flags);
2609 if (info->serial_signals & SerialSignal_RTS)
2610 strcat(stat_buf, "|RTS");
2611 if (info->serial_signals & SerialSignal_CTS)
2612 strcat(stat_buf, "|CTS");
2613 if (info->serial_signals & SerialSignal_DTR)
2614 strcat(stat_buf, "|DTR");
2615 if (info->serial_signals & SerialSignal_DSR)
2616 strcat(stat_buf, "|DSR");
2617 if (info->serial_signals & SerialSignal_DCD)
2618 strcat(stat_buf, "|CD");
2619 if (info->serial_signals & SerialSignal_RI)
2620 strcat(stat_buf, "|RI");
2622 if (info->params.mode == MGSL_MODE_HDLC) {
2623 seq_printf(m, " HDLC txok:%d rxok:%d",
2624 info->icount.txok, info->icount.rxok);
2625 if (info->icount.txunder)
2626 seq_printf(m, " txunder:%d", info->icount.txunder);
2627 if (info->icount.txabort)
2628 seq_printf(m, " txabort:%d", info->icount.txabort);
2629 if (info->icount.rxshort)
2630 seq_printf(m, " rxshort:%d", info->icount.rxshort);
2631 if (info->icount.rxlong)
2632 seq_printf(m, " rxlong:%d", info->icount.rxlong);
2633 if (info->icount.rxover)
2634 seq_printf(m, " rxover:%d", info->icount.rxover);
2635 if (info->icount.rxcrc)
2636 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
2638 seq_printf(m, " ASYNC tx:%d rx:%d",
2639 info->icount.tx, info->icount.rx);
2640 if (info->icount.frame)
2641 seq_printf(m, " fe:%d", info->icount.frame);
2642 if (info->icount.parity)
2643 seq_printf(m, " pe:%d", info->icount.parity);
2644 if (info->icount.brk)
2645 seq_printf(m, " brk:%d", info->icount.brk);
2646 if (info->icount.overrun)
2647 seq_printf(m, " oe:%d", info->icount.overrun);
2650 /* Append serial signal status to end */
2651 seq_printf(m, " %s\n", stat_buf+1);
2653 seq_printf(m, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2654 info->tx_active,info->bh_requested,info->bh_running,
2658 /* Called to print information about devices
2660 static int mgslpc_proc_show(struct seq_file *m, void *v)
2664 seq_printf(m, "synclink driver:%s\n", driver_version);
2666 info = mgslpc_device_list;
2669 info = info->next_device;
2674 static int mgslpc_proc_open(struct inode *inode, struct file *file)
2676 return single_open(file, mgslpc_proc_show, NULL);
2679 static const struct file_operations mgslpc_proc_fops = {
2680 .owner = THIS_MODULE,
2681 .open = mgslpc_proc_open,
2683 .llseek = seq_lseek,
2684 .release = single_release,
2687 static int rx_alloc_buffers(MGSLPC_INFO *info)
2689 /* each buffer has header and data */
2690 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2692 /* calculate total allocation size for 8 buffers */
2693 info->rx_buf_total_size = info->rx_buf_size * 8;
2695 /* limit total allocated memory */
2696 if (info->rx_buf_total_size > 0x10000)
2697 info->rx_buf_total_size = 0x10000;
2699 /* calculate number of buffers */
2700 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2702 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2703 if (info->rx_buf == NULL)
2706 rx_reset_buffers(info);
2710 static void rx_free_buffers(MGSLPC_INFO *info)
2712 kfree(info->rx_buf);
2713 info->rx_buf = NULL;
2716 static int claim_resources(MGSLPC_INFO *info)
2718 if (rx_alloc_buffers(info) < 0 ) {
2719 printk( "Cant allocate rx buffer %s\n", info->device_name);
2720 release_resources(info);
2726 static void release_resources(MGSLPC_INFO *info)
2728 if (debug_level >= DEBUG_LEVEL_INFO)
2729 printk("release_resources(%s)\n", info->device_name);
2730 rx_free_buffers(info);
2733 /* Add the specified device instance data structure to the
2734 * global linked list of devices and increment the device count.
2736 * Arguments: info pointer to device instance data
2738 static void mgslpc_add_device(MGSLPC_INFO *info)
2740 info->next_device = NULL;
2741 info->line = mgslpc_device_count;
2742 sprintf(info->device_name,"ttySLP%d",info->line);
2744 if (info->line < MAX_DEVICE_COUNT) {
2745 if (maxframe[info->line])
2746 info->max_frame_size = maxframe[info->line];
2749 mgslpc_device_count++;
2751 if (!mgslpc_device_list)
2752 mgslpc_device_list = info;
2754 MGSLPC_INFO *current_dev = mgslpc_device_list;
2755 while( current_dev->next_device )
2756 current_dev = current_dev->next_device;
2757 current_dev->next_device = info;
2760 if (info->max_frame_size < 4096)
2761 info->max_frame_size = 4096;
2762 else if (info->max_frame_size > 65535)
2763 info->max_frame_size = 65535;
2765 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2766 info->device_name, info->io_base, info->irq_level);
2768 #if SYNCLINK_GENERIC_HDLC
2773 static void mgslpc_remove_device(MGSLPC_INFO *remove_info)
2775 MGSLPC_INFO *info = mgslpc_device_list;
2776 MGSLPC_INFO *last = NULL;
2779 if (info == remove_info) {
2781 last->next_device = info->next_device;
2783 mgslpc_device_list = info->next_device;
2784 #if SYNCLINK_GENERIC_HDLC
2787 release_resources(info);
2789 mgslpc_device_count--;
2793 info = info->next_device;
2797 static struct pcmcia_device_id mgslpc_ids[] = {
2798 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
2801 MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
2803 static struct pcmcia_driver mgslpc_driver = {
2804 .owner = THIS_MODULE,
2806 .name = "synclink_cs",
2808 .probe = mgslpc_probe,
2809 .remove = mgslpc_detach,
2810 .id_table = mgslpc_ids,
2811 .suspend = mgslpc_suspend,
2812 .resume = mgslpc_resume,
2815 static const struct tty_operations mgslpc_ops = {
2816 .open = mgslpc_open,
2817 .close = mgslpc_close,
2818 .write = mgslpc_write,
2819 .put_char = mgslpc_put_char,
2820 .flush_chars = mgslpc_flush_chars,
2821 .write_room = mgslpc_write_room,
2822 .chars_in_buffer = mgslpc_chars_in_buffer,
2823 .flush_buffer = mgslpc_flush_buffer,
2824 .ioctl = mgslpc_ioctl,
2825 .throttle = mgslpc_throttle,
2826 .unthrottle = mgslpc_unthrottle,
2827 .send_xchar = mgslpc_send_xchar,
2828 .break_ctl = mgslpc_break,
2829 .wait_until_sent = mgslpc_wait_until_sent,
2830 .set_termios = mgslpc_set_termios,
2832 .start = tx_release,
2833 .hangup = mgslpc_hangup,
2834 .tiocmget = tiocmget,
2835 .tiocmset = tiocmset,
2836 .proc_fops = &mgslpc_proc_fops,
2839 static void synclink_cs_cleanup(void)
2843 printk("Unloading %s: version %s\n", driver_name, driver_version);
2845 while(mgslpc_device_list)
2846 mgslpc_remove_device(mgslpc_device_list);
2848 if (serial_driver) {
2849 if ((rc = tty_unregister_driver(serial_driver)))
2850 printk("%s(%d) failed to unregister tty driver err=%d\n",
2851 __FILE__,__LINE__,rc);
2852 put_tty_driver(serial_driver);
2855 pcmcia_unregister_driver(&mgslpc_driver);
2858 static int __init synclink_cs_init(void)
2862 if (break_on_load) {
2863 mgslpc_get_text_ptr();
2867 printk("%s %s\n", driver_name, driver_version);
2869 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
2872 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
2873 if (!serial_driver) {
2878 /* Initialize the tty_driver structure */
2880 serial_driver->owner = THIS_MODULE;
2881 serial_driver->driver_name = "synclink_cs";
2882 serial_driver->name = "ttySLP";
2883 serial_driver->major = ttymajor;
2884 serial_driver->minor_start = 64;
2885 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2886 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2887 serial_driver->init_termios = tty_std_termios;
2888 serial_driver->init_termios.c_cflag =
2889 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2890 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2891 tty_set_operations(serial_driver, &mgslpc_ops);
2893 if ((rc = tty_register_driver(serial_driver)) < 0) {
2894 printk("%s(%d):Couldn't register serial driver\n",
2896 put_tty_driver(serial_driver);
2897 serial_driver = NULL;
2901 printk("%s %s, tty major#%d\n",
2902 driver_name, driver_version,
2903 serial_driver->major);
2908 synclink_cs_cleanup();
2912 static void __exit synclink_cs_exit(void)
2914 synclink_cs_cleanup();
2917 module_init(synclink_cs_init);
2918 module_exit(synclink_cs_exit);
2920 static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
2925 /* note:standard BRG mode is broken in V3.2 chip
2926 * so enhanced mode is always used
2934 for (M = 1; N > 64 && M < 16; M++)
2940 * BGR[7..0] contained in BGR register
2941 * BGR[9..8] contained in CCR2[7..6]
2942 * divisor = (N+1)*2^M
2944 * Note: M *must* not be zero (causes asymetric duty cycle)
2946 write_reg(info, (unsigned char) (channel + BGR),
2947 (unsigned char) ((M << 6) + N));
2948 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
2949 val |= ((M << 4) & 0xc0);
2950 write_reg(info, (unsigned char) (channel + CCR2), val);
2954 /* Enabled the AUX clock output at the specified frequency.
2956 static void enable_auxclk(MGSLPC_INFO *info)
2962 * 07..06 MDS[1..0] 10 = transparent HDLC mode
2963 * 05 ADM Address Mode, 0 = no addr recognition
2964 * 04 TMD Timer Mode, 0 = external
2965 * 03 RAC Receiver Active, 0 = inactive
2966 * 02 RTS 0=RTS active during xmit, 1=RTS always active
2967 * 01 TRS Timer Resolution, 1=512
2968 * 00 TLP Test Loop, 0 = no loop
2974 /* channel B RTS is used to enable AUXCLK driver on SP505 */
2975 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
2977 write_reg(info, CHB + MODE, val);
2981 * 07 PU Power Up, 1=active, 0=power down
2982 * 06 MCE Master Clock Enable, 1=enabled
2984 * 04..02 SC[2..0] Encoding
2985 * 01..00 SM[1..0] Serial Mode, 00=HDLC
2989 write_reg(info, CHB + CCR0, 0xc0);
2993 * 07 SFLG Shared Flag, 0 = disable shared flags
2994 * 06 GALP Go Active On Loop, 0 = not used
2995 * 05 GLP Go On Loop, 0 = not used
2996 * 04 ODS Output Driver Select, 1=TxD is push-pull output
2997 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
2998 * 02..00 CM[2..0] Clock Mode
3002 write_reg(info, CHB + CCR1, 0x17);
3006 * 07..06 BGR[9..8] Baud rate bits 9..8
3007 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3008 * 04 SSEL Clock source select, 1=submode b
3009 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3010 * 02 RWX Read/Write Exchange 0=disabled
3011 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3012 * 00 DIV, data inversion 0=disabled, 1=enabled
3016 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3017 write_reg(info, CHB + CCR2, 0x38);
3019 write_reg(info, CHB + CCR2, 0x30);
3023 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3024 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3025 * 05 TST1 Test Pin, 0=normal operation
3026 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3027 * 03..02 Reserved, must be 0
3028 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3032 write_reg(info, CHB + CCR4, 0x50);
3034 /* if auxclk not enabled, set internal BRG so
3035 * CTS transitions can be detected (requires TxC)
3037 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3038 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3040 mgslpc_set_rate(info, CHB, 921600);
3043 static void loopback_enable(MGSLPC_INFO *info)
3047 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3048 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3049 write_reg(info, CHA + CCR1, val);
3051 /* CCR2:04 SSEL Clock source select, 1=submode b */
3052 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3053 write_reg(info, CHA + CCR2, val);
3055 /* set LinkSpeed if available, otherwise default to 2Mbps */
3056 if (info->params.clock_speed)
3057 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3059 mgslpc_set_rate(info, CHA, 1843200);
3061 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3062 val = read_reg(info, CHA + MODE) | BIT0;
3063 write_reg(info, CHA + MODE, val);
3066 static void hdlc_mode(MGSLPC_INFO *info)
3069 unsigned char clkmode, clksubmode;
3071 /* disable all interrupts */
3072 irq_disable(info, CHA, 0xffff);
3073 irq_disable(info, CHB, 0xffff);
3074 port_irq_disable(info, 0xff);
3076 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3077 clkmode = clksubmode = 0;
3078 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3079 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3080 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3082 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3083 && info->params.flags & HDLC_FLAG_TXC_BRG) {
3084 /* clock mode 7b, rcv = BRG, xmt = BRG */
3087 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3088 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3089 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3093 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3096 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3097 /* clock mode 0b, rcv = RxC, xmt = BRG */
3103 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3104 * 05 ADM Address Mode, 0 = no addr recognition
3105 * 04 TMD Timer Mode, 0 = external
3106 * 03 RAC Receiver Active, 0 = inactive
3107 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3108 * 01 TRS Timer Resolution, 1=512
3109 * 00 TLP Test Loop, 0 = no loop
3114 if (info->params.loopback)
3117 /* preserve RTS state */
3118 if (info->serial_signals & SerialSignal_RTS)
3120 write_reg(info, CHA + MODE, val);
3124 * 07 PU Power Up, 1=active, 0=power down
3125 * 06 MCE Master Clock Enable, 1=enabled
3127 * 04..02 SC[2..0] Encoding
3128 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3133 switch (info->params.encoding)
3135 case HDLC_ENCODING_NRZI:
3138 case HDLC_ENCODING_BIPHASE_SPACE:
3141 case HDLC_ENCODING_BIPHASE_MARK:
3144 case HDLC_ENCODING_BIPHASE_LEVEL:
3146 break; // Manchester
3148 write_reg(info, CHA + CCR0, val);
3152 * 07 SFLG Shared Flag, 0 = disable shared flags
3153 * 06 GALP Go Active On Loop, 0 = not used
3154 * 05 GLP Go On Loop, 0 = not used
3155 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3156 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3157 * 02..00 CM[2..0] Clock Mode
3161 val = 0x10 + clkmode;
3162 write_reg(info, CHA + CCR1, val);
3166 * 07..06 BGR[9..8] Baud rate bits 9..8
3167 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3168 * 04 SSEL Clock source select, 1=submode b
3169 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3170 * 02 RWX Read/Write Exchange 0=disabled
3171 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3172 * 00 DIV, data inversion 0=disabled, 1=enabled
3177 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3178 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3182 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3184 if (info->params.encoding == HDLC_ENCODING_NRZB)
3186 write_reg(info, CHA + CCR2, val);
3190 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3191 * 05 EPT Enable preamble transmission, 1=enabled
3192 * 04 RADD Receive address pushed to FIFO, 0=disabled
3193 * 03 CRL CRC Reset Level, 0=FFFF
3194 * 02 RCRC Rx CRC 0=On 1=Off
3195 * 01 TCRC Tx CRC 0=On 1=Off
3196 * 00 PSD DPLL Phase Shift Disable
3201 if (info->params.crc_type == HDLC_CRC_NONE)
3203 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3205 switch (info->params.preamble_length)
3207 case HDLC_PREAMBLE_LENGTH_16BITS:
3210 case HDLC_PREAMBLE_LENGTH_32BITS:
3213 case HDLC_PREAMBLE_LENGTH_64BITS:
3217 write_reg(info, CHA + CCR3, val);
3219 /* PRE - Preamble pattern */
3221 switch (info->params.preamble)
3223 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3224 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3225 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3226 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3228 write_reg(info, CHA + PRE, val);
3232 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3233 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3234 * 05 TST1 Test Pin, 0=normal operation
3235 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3236 * 03..02 Reserved, must be 0
3237 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3242 write_reg(info, CHA + CCR4, val);
3243 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3244 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3246 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3248 /* RLCR Receive length check register
3250 * 7 1=enable receive length check
3251 * 6..0 Max frame length = (RL + 1) * 32
3253 write_reg(info, CHA + RLCR, 0);
3255 /* XBCH Transmit Byte Count High
3257 * 07 DMA mode, 0 = interrupt driven
3258 * 06 NRM, 0=ABM (ignored)
3259 * 05 CAS Carrier Auto Start
3260 * 04 XC Transmit Continuously (ignored)
3261 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3266 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3268 write_reg(info, CHA + XBCH, val);
3269 enable_auxclk(info);
3270 if (info->params.loopback || info->testing_irq)
3271 loopback_enable(info);
3272 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3274 irq_enable(info, CHB, IRQ_CTS);
3275 /* PVR[3] 1=AUTO CTS active */
3276 set_reg_bits(info, CHA + PVR, BIT3);
3278 clear_reg_bits(info, CHA + PVR, BIT3);
3280 irq_enable(info, CHA,
3281 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3282 IRQ_UNDERRUN + IRQ_TXFIFO);
3283 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3284 wait_command_complete(info, CHA);
3285 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3287 /* Master clock mode enabled above to allow reset commands
3288 * to complete even if no data clocks are present.
3290 * Disable master clock mode for normal communications because
3291 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3292 * IRQ when in master clock mode.
3294 * Leave master clock mode enabled for IRQ test because the
3295 * timer IRQ used by the test can only happen in master clock mode.
3297 if (!info->testing_irq)
3298 clear_reg_bits(info, CHA + CCR0, BIT6);
3306 static void rx_stop(MGSLPC_INFO *info)
3308 if (debug_level >= DEBUG_LEVEL_ISR)
3309 printk("%s(%d):rx_stop(%s)\n",
3310 __FILE__,__LINE__, info->device_name );
3312 /* MODE:03 RAC Receiver Active, 0=inactive */
3313 clear_reg_bits(info, CHA + MODE, BIT3);
3315 info->rx_enabled = false;
3316 info->rx_overflow = false;
3319 static void rx_start(MGSLPC_INFO *info)
3321 if (debug_level >= DEBUG_LEVEL_ISR)
3322 printk("%s(%d):rx_start(%s)\n",
3323 __FILE__,__LINE__, info->device_name );
3325 rx_reset_buffers(info);
3326 info->rx_enabled = false;
3327 info->rx_overflow = false;
3329 /* MODE:03 RAC Receiver Active, 1=active */
3330 set_reg_bits(info, CHA + MODE, BIT3);
3332 info->rx_enabled = true;
3335 static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty)
3337 if (debug_level >= DEBUG_LEVEL_ISR)
3338 printk("%s(%d):tx_start(%s)\n",
3339 __FILE__,__LINE__, info->device_name );
3341 if (info->tx_count) {
3342 /* If auto RTS enabled and RTS is inactive, then assert */
3343 /* RTS and set a flag indicating that the driver should */
3344 /* negate RTS when the transmission completes. */
3345 info->drop_rts_on_tx_done = false;
3347 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3349 if (!(info->serial_signals & SerialSignal_RTS)) {
3350 info->serial_signals |= SerialSignal_RTS;
3352 info->drop_rts_on_tx_done = true;
3356 if (info->params.mode == MGSL_MODE_ASYNC) {
3357 if (!info->tx_active) {
3358 info->tx_active = true;
3359 tx_ready(info, tty);
3362 info->tx_active = true;
3363 tx_ready(info, tty);
3364 mod_timer(&info->tx_timer, jiffies +
3365 msecs_to_jiffies(5000));
3369 if (!info->tx_enabled)
3370 info->tx_enabled = true;
3373 static void tx_stop(MGSLPC_INFO *info)
3375 if (debug_level >= DEBUG_LEVEL_ISR)
3376 printk("%s(%d):tx_stop(%s)\n",
3377 __FILE__,__LINE__, info->device_name );
3379 del_timer(&info->tx_timer);
3381 info->tx_enabled = false;
3382 info->tx_active = false;
3385 /* Reset the adapter to a known state and prepare it for further use.
3387 static void reset_device(MGSLPC_INFO *info)
3389 /* power up both channels (set BIT7) */
3390 write_reg(info, CHA + CCR0, 0x80);
3391 write_reg(info, CHB + CCR0, 0x80);
3392 write_reg(info, CHA + MODE, 0);
3393 write_reg(info, CHB + MODE, 0);
3395 /* disable all interrupts */
3396 irq_disable(info, CHA, 0xffff);
3397 irq_disable(info, CHB, 0xffff);
3398 port_irq_disable(info, 0xff);
3400 /* PCR Port Configuration Register
3402 * 07..04 DEC[3..0] Serial I/F select outputs
3403 * 03 output, 1=AUTO CTS control enabled
3404 * 02 RI Ring Indicator input 0=active
3405 * 01 DSR input 0=active
3406 * 00 DTR output 0=active
3410 write_reg(info, PCR, 0x06);
3412 /* PVR Port Value Register
3414 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3415 * 03 AUTO CTS output 1=enabled
3416 * 02 RI Ring Indicator input
3418 * 00 DTR output (1=inactive)
3422 // write_reg(info, PVR, PVR_DTR);
3424 /* IPC Interrupt Port Configuration
3426 * 07 VIS 1=Masked interrupts visible
3427 * 06..05 Reserved, 0
3428 * 04..03 SLA Slave address, 00 ignored
3429 * 02 CASM Cascading Mode, 1=daisy chain
3430 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3434 write_reg(info, IPC, 0x05);
3437 static void async_mode(MGSLPC_INFO *info)
3441 /* disable all interrupts */
3442 irq_disable(info, CHA, 0xffff);
3443 irq_disable(info, CHB, 0xffff);
3444 port_irq_disable(info, 0xff);
3449 * 06 FRTS RTS State, 0=active
3450 * 05 FCTS Flow Control on CTS
3451 * 04 FLON Flow Control Enable
3452 * 03 RAC Receiver Active, 0 = inactive
3453 * 02 RTS 0=Auto RTS, 1=manual RTS
3454 * 01 TRS Timer Resolution, 1=512
3455 * 00 TLP Test Loop, 0 = no loop
3460 if (info->params.loopback)
3463 /* preserve RTS state */
3464 if (!(info->serial_signals & SerialSignal_RTS))
3466 write_reg(info, CHA + MODE, val);
3470 * 07 PU Power Up, 1=active, 0=power down
3471 * 06 MCE Master Clock Enable, 1=enabled
3473 * 04..02 SC[2..0] Encoding, 000=NRZ
3474 * 01..00 SM[1..0] Serial Mode, 11=Async
3478 write_reg(info, CHA + CCR0, 0x83);
3482 * 07..05 Reserved, 0
3483 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3484 * 03 BCR Bit Clock Rate, 1=16x
3485 * 02..00 CM[2..0] Clock Mode, 111=BRG
3489 write_reg(info, CHA + CCR1, 0x1f);
3493 * 07..06 BGR[9..8] Baud rate bits 9..8
3494 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3495 * 04 SSEL Clock source select, 1=submode b
3496 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3497 * 02 RWX Read/Write Exchange 0=disabled
3499 * 00 DIV, data inversion 0=disabled, 1=enabled
3503 write_reg(info, CHA + CCR2, 0x10);
3507 * 07..01 Reserved, 0
3508 * 00 PSD DPLL Phase Shift Disable
3512 write_reg(info, CHA + CCR3, 0);
3516 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3517 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3518 * 05 TST1 Test Pin, 0=normal operation
3519 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3520 * 03..00 Reserved, must be 0
3524 write_reg(info, CHA + CCR4, 0x50);
3525 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3530 * 06 XBRK transmit break, 0=normal operation
3531 * 05 Stop bits (0=1, 1=2)
3532 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3533 * 02 PAREN Parity Enable
3534 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3538 if (info->params.data_bits != 8)
3539 val |= BIT0; /* 7 bits */
3540 if (info->params.stop_bits != 1)
3542 if (info->params.parity != ASYNC_PARITY_NONE)
3544 val |= BIT2; /* Parity enable */
3545 if (info->params.parity == ASYNC_PARITY_ODD)
3550 write_reg(info, CHA + DAFO, val);
3552 /* RFC Rx FIFO Control
3555 * 06 DPS, 1=parity bit not stored in data byte
3556 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3557 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3558 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3560 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3564 write_reg(info, CHA + RFC, 0x5c);
3566 /* RLCR Receive length check register
3568 * Max frame length = (RL + 1) * 32
3570 write_reg(info, CHA + RLCR, 0);
3572 /* XBCH Transmit Byte Count High
3574 * 07 DMA mode, 0 = interrupt driven
3575 * 06 NRM, 0=ABM (ignored)
3576 * 05 CAS Carrier Auto Start
3577 * 04 XC Transmit Continuously (ignored)
3578 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3583 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3585 write_reg(info, CHA + XBCH, val);
3586 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3587 irq_enable(info, CHA, IRQ_CTS);
3589 /* MODE:03 RAC Receiver Active, 1=active */
3590 set_reg_bits(info, CHA + MODE, BIT3);
3591 enable_auxclk(info);
3592 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3593 irq_enable(info, CHB, IRQ_CTS);
3594 /* PVR[3] 1=AUTO CTS active */
3595 set_reg_bits(info, CHA + PVR, BIT3);
3597 clear_reg_bits(info, CHA + PVR, BIT3);
3598 irq_enable(info, CHA,
3599 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3600 IRQ_ALLSENT + IRQ_TXFIFO);
3601 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3602 wait_command_complete(info, CHA);
3603 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3606 /* Set the HDLC idle mode for the transmitter.
3608 static void tx_set_idle(MGSLPC_INFO *info)
3610 /* Note: ESCC2 only supports flags and one idle modes */
3611 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3612 set_reg_bits(info, CHA + CCR1, BIT3);
3614 clear_reg_bits(info, CHA + CCR1, BIT3);
3617 /* get state of the V24 status (input) signals.
3619 static void get_signals(MGSLPC_INFO *info)
3621 unsigned char status = 0;
3623 /* preserve DTR and RTS */
3624 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3626 if (read_reg(info, CHB + VSTR) & BIT7)
3627 info->serial_signals |= SerialSignal_DCD;
3628 if (read_reg(info, CHB + STAR) & BIT1)
3629 info->serial_signals |= SerialSignal_CTS;
3631 status = read_reg(info, CHA + PVR);
3632 if (!(status & PVR_RI))
3633 info->serial_signals |= SerialSignal_RI;
3634 if (!(status & PVR_DSR))
3635 info->serial_signals |= SerialSignal_DSR;
3638 /* Set the state of DTR and RTS based on contents of
3639 * serial_signals member of device extension.
3641 static void set_signals(MGSLPC_INFO *info)
3645 val = read_reg(info, CHA + MODE);
3646 if (info->params.mode == MGSL_MODE_ASYNC) {
3647 if (info->serial_signals & SerialSignal_RTS)
3652 if (info->serial_signals & SerialSignal_RTS)
3657 write_reg(info, CHA + MODE, val);
3659 if (info->serial_signals & SerialSignal_DTR)
3660 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3662 set_reg_bits(info, CHA + PVR, PVR_DTR);
3665 static void rx_reset_buffers(MGSLPC_INFO *info)
3672 info->rx_frame_count = 0;
3673 for (i=0 ; i < info->rx_buf_count ; i++) {
3674 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3675 buf->status = buf->count = 0;
3679 /* Attempt to return a received HDLC frame
3680 * Only frames received without errors are returned.
3682 * Returns true if frame returned, otherwise false
3684 static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty)
3686 unsigned short status;
3688 unsigned int framesize = 0;
3689 unsigned long flags;
3690 bool return_frame = false;
3692 if (info->rx_frame_count == 0)
3695 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3697 status = buf->status;
3699 /* 07 VFR 1=valid frame
3700 * 06 RDO 1=data overrun
3701 * 05 CRC 1=OK, 0=error
3702 * 04 RAB 1=frame aborted
3704 if ((status & 0xf0) != 0xA0) {
3705 if (!(status & BIT7) || (status & BIT4))
3706 info->icount.rxabort++;
3707 else if (status & BIT6)
3708 info->icount.rxover++;
3709 else if (!(status & BIT5)) {
3710 info->icount.rxcrc++;
3711 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
3712 return_frame = true;
3715 #if SYNCLINK_GENERIC_HDLC
3717 info->netdev->stats.rx_errors++;
3718 info->netdev->stats.rx_frame_errors++;
3722 return_frame = true;
3725 framesize = buf->count;
3727 if (debug_level >= DEBUG_LEVEL_BH)
3728 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3729 __FILE__,__LINE__,info->device_name,status,framesize);
3731 if (debug_level >= DEBUG_LEVEL_DATA)
3732 trace_block(info, buf->data, framesize, 0);
3735 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3736 framesize+1 > info->max_frame_size) ||
3737 framesize > info->max_frame_size)
3738 info->icount.rxlong++;
3741 info->icount.rxok++;
3743 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3744 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3748 #if SYNCLINK_GENERIC_HDLC
3750 hdlcdev_rx(info, buf->data, framesize);
3753 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3757 spin_lock_irqsave(&info->lock,flags);
3758 buf->status = buf->count = 0;
3759 info->rx_frame_count--;
3761 if (info->rx_get >= info->rx_buf_count)
3763 spin_unlock_irqrestore(&info->lock,flags);
3768 static bool register_test(MGSLPC_INFO *info)
3770 static unsigned char patterns[] =
3771 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
3772 static unsigned int count = ARRAY_SIZE(patterns);
3775 unsigned long flags;
3777 spin_lock_irqsave(&info->lock,flags);
3780 for (i = 0; i < count; i++) {
3781 write_reg(info, XAD1, patterns[i]);
3782 write_reg(info, XAD2, patterns[(i + 1) % count]);
3783 if ((read_reg(info, XAD1) != patterns[i]) ||
3784 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
3790 spin_unlock_irqrestore(&info->lock,flags);
3794 static bool irq_test(MGSLPC_INFO *info)
3796 unsigned long end_time;
3797 unsigned long flags;
3799 spin_lock_irqsave(&info->lock,flags);
3802 info->testing_irq = true;
3805 info->irq_occurred = false;
3807 /* init hdlc mode */
3809 irq_enable(info, CHA, IRQ_TIMER);
3810 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
3811 issue_command(info, CHA, CMD_START_TIMER);
3813 spin_unlock_irqrestore(&info->lock,flags);
3816 while(end_time-- && !info->irq_occurred) {
3817 msleep_interruptible(10);
3820 info->testing_irq = false;
3822 spin_lock_irqsave(&info->lock,flags);
3824 spin_unlock_irqrestore(&info->lock,flags);
3826 return info->irq_occurred;
3829 static int adapter_test(MGSLPC_INFO *info)
3831 if (!register_test(info)) {
3832 info->init_error = DiagStatus_AddressFailure;
3833 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
3834 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
3838 if (!irq_test(info)) {
3839 info->init_error = DiagStatus_IrqFailure;
3840 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
3841 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
3845 if (debug_level >= DEBUG_LEVEL_INFO)
3846 printk("%s(%d):device %s passed diagnostics\n",
3847 __FILE__,__LINE__,info->device_name);
3851 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
3856 printk("%s tx data:\n",info->device_name);
3858 printk("%s rx data:\n",info->device_name);
3866 for(i=0;i<linecount;i++)
3867 printk("%02X ",(unsigned char)data[i]);
3870 for(i=0;i<linecount;i++) {
3871 if (data[i]>=040 && data[i]<=0176)
3872 printk("%c",data[i]);
3883 /* HDLC frame time out
3884 * update stats and do tx completion processing
3886 static void tx_timeout(unsigned long context)
3888 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
3889 unsigned long flags;
3891 if ( debug_level >= DEBUG_LEVEL_INFO )
3892 printk( "%s(%d):tx_timeout(%s)\n",
3893 __FILE__,__LINE__,info->device_name);
3894 if(info->tx_active &&
3895 info->params.mode == MGSL_MODE_HDLC) {
3896 info->icount.txtimeout++;
3898 spin_lock_irqsave(&info->lock,flags);
3899 info->tx_active = false;
3900 info->tx_count = info->tx_put = info->tx_get = 0;
3902 spin_unlock_irqrestore(&info->lock,flags);
3904 #if SYNCLINK_GENERIC_HDLC
3906 hdlcdev_tx_done(info);
3910 struct tty_struct *tty = tty_port_tty_get(&info->port);
3911 bh_transmit(info, tty);
3916 #if SYNCLINK_GENERIC_HDLC
3919 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
3920 * set encoding and frame check sequence (FCS) options
3922 * dev pointer to network device structure
3923 * encoding serial encoding setting
3924 * parity FCS setting
3926 * returns 0 if success, otherwise error code
3928 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
3929 unsigned short parity)
3931 MGSLPC_INFO *info = dev_to_port(dev);
3932 struct tty_struct *tty;
3933 unsigned char new_encoding;
3934 unsigned short new_crctype;
3936 /* return error if TTY interface open */
3937 if (info->port.count)
3942 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
3943 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
3944 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
3945 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
3946 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
3947 default: return -EINVAL;
3952 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
3953 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
3954 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
3955 default: return -EINVAL;
3958 info->params.encoding = new_encoding;
3959 info->params.crc_type = new_crctype;
3961 /* if network interface up, reprogram hardware */
3962 if (info->netcount) {
3963 tty = tty_port_tty_get(&info->port);
3964 mgslpc_program_hw(info, tty);
3972 * called by generic HDLC layer to send frame
3974 * skb socket buffer containing HDLC frame
3975 * dev pointer to network device structure
3977 static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
3978 struct net_device *dev)
3980 MGSLPC_INFO *info = dev_to_port(dev);
3981 unsigned long flags;
3983 if (debug_level >= DEBUG_LEVEL_INFO)
3984 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
3986 /* stop sending until this frame completes */
3987 netif_stop_queue(dev);
3989 /* copy data to device buffers */
3990 skb_copy_from_linear_data(skb, info->tx_buf, skb->len);
3992 info->tx_put = info->tx_count = skb->len;
3994 /* update network statistics */
3995 dev->stats.tx_packets++;
3996 dev->stats.tx_bytes += skb->len;
3998 /* done with socket buffer, so free it */
4001 /* save start time for transmit timeout detection */
4002 dev->trans_start = jiffies;
4004 /* start hardware transmitter if necessary */
4005 spin_lock_irqsave(&info->lock,flags);
4006 if (!info->tx_active) {
4007 struct tty_struct *tty = tty_port_tty_get(&info->port);
4008 tx_start(info, tty);
4011 spin_unlock_irqrestore(&info->lock,flags);
4013 return NETDEV_TX_OK;
4017 * called by network layer when interface enabled
4018 * claim resources and initialize hardware
4020 * dev pointer to network device structure
4022 * returns 0 if success, otherwise error code
4024 static int hdlcdev_open(struct net_device *dev)
4026 MGSLPC_INFO *info = dev_to_port(dev);
4027 struct tty_struct *tty;
4029 unsigned long flags;
4031 if (debug_level >= DEBUG_LEVEL_INFO)
4032 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4034 /* generic HDLC layer open processing */
4035 if ((rc = hdlc_open(dev)))
4038 /* arbitrate between network and tty opens */
4039 spin_lock_irqsave(&info->netlock, flags);
4040 if (info->port.count != 0 || info->netcount != 0) {
4041 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4042 spin_unlock_irqrestore(&info->netlock, flags);
4046 spin_unlock_irqrestore(&info->netlock, flags);
4048 tty = tty_port_tty_get(&info->port);
4049 /* claim resources and init adapter */
4050 if ((rc = startup(info, tty)) != 0) {
4052 spin_lock_irqsave(&info->netlock, flags);
4054 spin_unlock_irqrestore(&info->netlock, flags);
4057 /* assert DTR and RTS, apply hardware settings */
4058 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4059 mgslpc_program_hw(info, tty);
4062 /* enable network layer transmit */
4063 dev->trans_start = jiffies;
4064 netif_start_queue(dev);
4066 /* inform generic HDLC layer of current DCD status */
4067 spin_lock_irqsave(&info->lock, flags);
4069 spin_unlock_irqrestore(&info->lock, flags);
4070 if (info->serial_signals & SerialSignal_DCD)
4071 netif_carrier_on(dev);
4073 netif_carrier_off(dev);
4078 * called by network layer when interface is disabled
4079 * shutdown hardware and release resources
4081 * dev pointer to network device structure
4083 * returns 0 if success, otherwise error code
4085 static int hdlcdev_close(struct net_device *dev)
4087 MGSLPC_INFO *info = dev_to_port(dev);
4088 struct tty_struct *tty = tty_port_tty_get(&info->port);
4089 unsigned long flags;
4091 if (debug_level >= DEBUG_LEVEL_INFO)
4092 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4094 netif_stop_queue(dev);
4096 /* shutdown adapter and release resources */
4097 shutdown(info, tty);
4101 spin_lock_irqsave(&info->netlock, flags);
4103 spin_unlock_irqrestore(&info->netlock, flags);
4109 * called by network layer to process IOCTL call to network device
4111 * dev pointer to network device structure
4112 * ifr pointer to network interface request structure
4113 * cmd IOCTL command code
4115 * returns 0 if success, otherwise error code
4117 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4119 const size_t size = sizeof(sync_serial_settings);
4120 sync_serial_settings new_line;
4121 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4122 MGSLPC_INFO *info = dev_to_port(dev);
4125 if (debug_level >= DEBUG_LEVEL_INFO)
4126 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4128 /* return error if TTY interface open */
4129 if (info->port.count)
4132 if (cmd != SIOCWANDEV)
4133 return hdlc_ioctl(dev, ifr, cmd);
4135 switch(ifr->ifr_settings.type) {
4136 case IF_GET_IFACE: /* return current sync_serial_settings */
4138 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4139 if (ifr->ifr_settings.size < size) {
4140 ifr->ifr_settings.size = size; /* data size wanted */
4144 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4145 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4146 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4147 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4150 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4151 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4152 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4153 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4154 default: new_line.clock_type = CLOCK_DEFAULT;
4157 new_line.clock_rate = info->params.clock_speed;
4158 new_line.loopback = info->params.loopback ? 1:0;
4160 if (copy_to_user(line, &new_line, size))
4164 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4166 if(!capable(CAP_NET_ADMIN))
4168 if (copy_from_user(&new_line, line, size))
4171 switch (new_line.clock_type)
4173 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4174 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4175 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4176 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4177 case CLOCK_DEFAULT: flags = info->params.flags &
4178 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4179 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4180 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4181 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4182 default: return -EINVAL;
4185 if (new_line.loopback != 0 && new_line.loopback != 1)
4188 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4189 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4190 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4191 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4192 info->params.flags |= flags;
4194 info->params.loopback = new_line.loopback;
4196 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4197 info->params.clock_speed = new_line.clock_rate;
4199 info->params.clock_speed = 0;
4201 /* if network interface up, reprogram hardware */
4202 if (info->netcount) {
4203 struct tty_struct *tty = tty_port_tty_get(&info->port);
4204 mgslpc_program_hw(info, tty);
4210 return hdlc_ioctl(dev, ifr, cmd);
4215 * called by network layer when transmit timeout is detected
4217 * dev pointer to network device structure
4219 static void hdlcdev_tx_timeout(struct net_device *dev)
4221 MGSLPC_INFO *info = dev_to_port(dev);
4222 unsigned long flags;
4224 if (debug_level >= DEBUG_LEVEL_INFO)
4225 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4227 dev->stats.tx_errors++;
4228 dev->stats.tx_aborted_errors++;
4230 spin_lock_irqsave(&info->lock,flags);
4232 spin_unlock_irqrestore(&info->lock,flags);
4234 netif_wake_queue(dev);
4238 * called by device driver when transmit completes
4239 * reenable network layer transmit if stopped
4241 * info pointer to device instance information
4243 static void hdlcdev_tx_done(MGSLPC_INFO *info)
4245 if (netif_queue_stopped(info->netdev))
4246 netif_wake_queue(info->netdev);
4250 * called by device driver when frame received
4251 * pass frame to network layer
4253 * info pointer to device instance information
4254 * buf pointer to buffer contianing frame data
4255 * size count of data bytes in buf
4257 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4259 struct sk_buff *skb = dev_alloc_skb(size);
4260 struct net_device *dev = info->netdev;
4262 if (debug_level >= DEBUG_LEVEL_INFO)
4263 printk("hdlcdev_rx(%s)\n",dev->name);
4266 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
4267 dev->stats.rx_dropped++;
4271 memcpy(skb_put(skb, size), buf, size);
4273 skb->protocol = hdlc_type_trans(skb, dev);
4275 dev->stats.rx_packets++;
4276 dev->stats.rx_bytes += size;
4281 static const struct net_device_ops hdlcdev_ops = {
4282 .ndo_open = hdlcdev_open,
4283 .ndo_stop = hdlcdev_close,
4284 .ndo_change_mtu = hdlc_change_mtu,
4285 .ndo_start_xmit = hdlc_start_xmit,
4286 .ndo_do_ioctl = hdlcdev_ioctl,
4287 .ndo_tx_timeout = hdlcdev_tx_timeout,
4291 * called by device driver when adding device instance
4292 * do generic HDLC initialization
4294 * info pointer to device instance information
4296 * returns 0 if success, otherwise error code
4298 static int hdlcdev_init(MGSLPC_INFO *info)
4301 struct net_device *dev;
4304 /* allocate and initialize network and HDLC layer objects */
4306 if (!(dev = alloc_hdlcdev(info))) {
4307 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4311 /* for network layer reporting purposes only */
4312 dev->base_addr = info->io_base;
4313 dev->irq = info->irq_level;
4315 /* network layer callbacks and settings */
4316 dev->netdev_ops = &hdlcdev_ops;
4317 dev->watchdog_timeo = 10 * HZ;
4318 dev->tx_queue_len = 50;
4320 /* generic HDLC layer callbacks and settings */
4321 hdlc = dev_to_hdlc(dev);
4322 hdlc->attach = hdlcdev_attach;
4323 hdlc->xmit = hdlcdev_xmit;
4325 /* register objects with HDLC layer */
4326 if ((rc = register_hdlc_device(dev))) {
4327 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4337 * called by device driver when removing device instance
4338 * do generic HDLC cleanup
4340 * info pointer to device instance information
4342 static void hdlcdev_exit(MGSLPC_INFO *info)
4344 unregister_hdlc_device(info->netdev);
4345 free_netdev(info->netdev);
4346 info->netdev = NULL;
4349 #endif /* CONFIG_HDLC */