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;
226 /* SPPP/Cisco HDLC device parts */
230 #if SYNCLINK_GENERIC_HDLC
231 struct net_device *netdev;
236 #define MGSLPC_MAGIC 0x5402
239 * The size of the serial xmit buffer is 1 page, or 4096 bytes
241 #define TXBUFSIZE 4096
244 #define CHA 0x00 /* channel A offset */
245 #define CHB 0x40 /* channel B offset */
248 * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
295 #define IRQ_BREAK_ON BIT15 // rx break detected
296 #define IRQ_DATAOVERRUN BIT14 // receive data overflow
297 #define IRQ_ALLSENT BIT13 // all sent
298 #define IRQ_UNDERRUN BIT12 // transmit data underrun
299 #define IRQ_TIMER BIT11 // timer interrupt
300 #define IRQ_CTS BIT10 // CTS status change
301 #define IRQ_TXREPEAT BIT9 // tx message repeat
302 #define IRQ_TXFIFO BIT8 // transmit pool ready
303 #define IRQ_RXEOM BIT7 // receive message end
304 #define IRQ_EXITHUNT BIT6 // receive frame start
305 #define IRQ_RXTIME BIT6 // rx char timeout
306 #define IRQ_DCD BIT2 // carrier detect status change
307 #define IRQ_OVERRUN BIT1 // receive frame overflow
308 #define IRQ_RXFIFO BIT0 // receive pool full
312 #define XFW BIT6 // transmit FIFO write enable
313 #define CEC BIT2 // command executing
314 #define CTS BIT1 // CTS state
319 #define PVR_AUTOCTS BIT3
320 #define PVR_RS232 0x20 /* 0010b */
321 #define PVR_V35 0xe0 /* 1110b */
322 #define PVR_RS422 0x40 /* 0100b */
324 /* Register access functions */
326 #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
327 #define read_reg(info, reg) inb((info)->io_base + (reg))
329 #define read_reg16(info, reg) inw((info)->io_base + (reg))
330 #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
332 #define set_reg_bits(info, reg, mask) \
333 write_reg(info, (reg), \
334 (unsigned char) (read_reg(info, (reg)) | (mask)))
335 #define clear_reg_bits(info, reg, mask) \
336 write_reg(info, (reg), \
337 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
339 * interrupt enable/disable routines
341 static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
343 if (channel == CHA) {
344 info->imra_value |= mask;
345 write_reg16(info, CHA + IMR, info->imra_value);
347 info->imrb_value |= mask;
348 write_reg16(info, CHB + IMR, info->imrb_value);
351 static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
353 if (channel == CHA) {
354 info->imra_value &= ~mask;
355 write_reg16(info, CHA + IMR, info->imra_value);
357 info->imrb_value &= ~mask;
358 write_reg16(info, CHB + IMR, info->imrb_value);
362 #define port_irq_disable(info, mask) \
363 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
365 #define port_irq_enable(info, mask) \
366 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
368 static void rx_start(MGSLPC_INFO *info);
369 static void rx_stop(MGSLPC_INFO *info);
371 static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty);
372 static void tx_stop(MGSLPC_INFO *info);
373 static void tx_set_idle(MGSLPC_INFO *info);
375 static void get_signals(MGSLPC_INFO *info);
376 static void set_signals(MGSLPC_INFO *info);
378 static void reset_device(MGSLPC_INFO *info);
380 static void hdlc_mode(MGSLPC_INFO *info);
381 static void async_mode(MGSLPC_INFO *info);
383 static void tx_timeout(unsigned long context);
385 static int carrier_raised(struct tty_port *port);
386 static void dtr_rts(struct tty_port *port, int onoff);
388 #if SYNCLINK_GENERIC_HDLC
389 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
390 static void hdlcdev_tx_done(MGSLPC_INFO *info);
391 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
392 static int hdlcdev_init(MGSLPC_INFO *info);
393 static void hdlcdev_exit(MGSLPC_INFO *info);
396 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
398 static bool register_test(MGSLPC_INFO *info);
399 static bool irq_test(MGSLPC_INFO *info);
400 static int adapter_test(MGSLPC_INFO *info);
402 static int claim_resources(MGSLPC_INFO *info);
403 static void release_resources(MGSLPC_INFO *info);
404 static void mgslpc_add_device(MGSLPC_INFO *info);
405 static void mgslpc_remove_device(MGSLPC_INFO *info);
407 static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty);
408 static void rx_reset_buffers(MGSLPC_INFO *info);
409 static int rx_alloc_buffers(MGSLPC_INFO *info);
410 static void rx_free_buffers(MGSLPC_INFO *info);
412 static irqreturn_t mgslpc_isr(int irq, void *dev_id);
415 * Bottom half interrupt handlers
417 static void bh_handler(struct work_struct *work);
418 static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty);
419 static void bh_status(MGSLPC_INFO *info);
424 static int tiocmget(struct tty_struct *tty, struct file *file);
425 static int tiocmset(struct tty_struct *tty, struct file *file,
426 unsigned int set, unsigned int clear);
427 static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
428 static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
429 static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params, struct tty_struct *tty);
430 static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
431 static int set_txidle(MGSLPC_INFO *info, int idle_mode);
432 static int set_txenable(MGSLPC_INFO *info, int enable, struct tty_struct *tty);
433 static int tx_abort(MGSLPC_INFO *info);
434 static int set_rxenable(MGSLPC_INFO *info, int enable);
435 static int wait_events(MGSLPC_INFO *info, int __user *mask);
437 static MGSLPC_INFO *mgslpc_device_list = NULL;
438 static int mgslpc_device_count = 0;
441 * Set this param to non-zero to load eax with the
442 * .text section address and breakpoint on module load.
443 * This is useful for use with gdb and add-symbol-file command.
445 static int break_on_load=0;
448 * Driver major number, defaults to zero to get auto
449 * assigned major number. May be forced as module parameter.
451 static int ttymajor=0;
453 static int debug_level = 0;
454 static int maxframe[MAX_DEVICE_COUNT] = {0,};
456 module_param(break_on_load, bool, 0);
457 module_param(ttymajor, int, 0);
458 module_param(debug_level, int, 0);
459 module_param_array(maxframe, int, NULL, 0);
461 MODULE_LICENSE("GPL");
463 static char *driver_name = "SyncLink PC Card driver";
464 static char *driver_version = "$Revision: 4.34 $";
466 static struct tty_driver *serial_driver;
468 /* number of characters left in xmit buffer before we ask for more */
469 #define WAKEUP_CHARS 256
471 static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty);
472 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
474 /* PCMCIA prototypes */
476 static int mgslpc_config(struct pcmcia_device *link);
477 static void mgslpc_release(u_long arg);
478 static void mgslpc_detach(struct pcmcia_device *p_dev);
481 * 1st function defined in .text section. Calling this function in
482 * init_module() followed by a breakpoint allows a remote debugger
483 * (gdb) to get the .text address for the add-symbol-file command.
484 * This allows remote debugging of dynamically loadable modules.
486 static void* mgslpc_get_text_ptr(void)
488 return mgslpc_get_text_ptr;
492 * line discipline callback wrappers
494 * The wrappers maintain line discipline references
495 * while calling into the line discipline.
497 * ldisc_receive_buf - pass receive data to line discipline
500 static void ldisc_receive_buf(struct tty_struct *tty,
501 const __u8 *data, char *flags, int count)
503 struct tty_ldisc *ld;
506 ld = tty_ldisc_ref(tty);
508 if (ld->ops->receive_buf)
509 ld->ops->receive_buf(tty, data, flags, count);
514 static const struct tty_port_operations mgslpc_port_ops = {
515 .carrier_raised = carrier_raised,
519 static int mgslpc_probe(struct pcmcia_device *link)
524 if (debug_level >= DEBUG_LEVEL_INFO)
525 printk("mgslpc_attach\n");
527 info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
529 printk("Error can't allocate device instance data\n");
533 info->magic = MGSLPC_MAGIC;
534 tty_port_init(&info->port);
535 info->port.ops = &mgslpc_port_ops;
536 INIT_WORK(&info->task, bh_handler);
537 info->max_frame_size = 4096;
538 info->port.close_delay = 5*HZ/10;
539 info->port.closing_wait = 30*HZ;
540 init_waitqueue_head(&info->status_event_wait_q);
541 init_waitqueue_head(&info->event_wait_q);
542 spin_lock_init(&info->lock);
543 spin_lock_init(&info->netlock);
544 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
545 info->idle_mode = HDLC_TXIDLE_FLAGS;
546 info->imra_value = 0xffff;
547 info->imrb_value = 0xffff;
548 info->pim_value = 0xff;
553 /* Initialize the struct pcmcia_device structure */
555 /* Interrupt setup */
556 link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
557 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
558 link->irq.Handler = NULL;
560 link->conf.Attributes = 0;
561 link->conf.IntType = INT_MEMORY_AND_IO;
563 ret = mgslpc_config(link);
567 mgslpc_add_device(info);
572 /* Card has been inserted.
575 #define CS_CHECK(fn, ret) \
576 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
578 static int mgslpc_ioprobe(struct pcmcia_device *p_dev,
579 cistpl_cftable_entry_t *cfg,
580 cistpl_cftable_entry_t *dflt,
584 if (cfg->io.nwin > 0) {
585 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
586 if (!(cfg->io.flags & CISTPL_IO_8BIT))
587 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
588 if (!(cfg->io.flags & CISTPL_IO_16BIT))
589 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
590 p_dev->io.IOAddrLines = cfg->io.flags & CISTPL_IO_LINES_MASK;
591 p_dev->io.BasePort1 = cfg->io.win[0].base;
592 p_dev->io.NumPorts1 = cfg->io.win[0].len;
593 return pcmcia_request_io(p_dev, &p_dev->io);
598 static int mgslpc_config(struct pcmcia_device *link)
600 MGSLPC_INFO *info = link->priv;
601 int last_fn = RequestIO;
604 if (debug_level >= DEBUG_LEVEL_INFO)
605 printk("mgslpc_config(0x%p)\n", link);
607 last_ret = pcmcia_loop_config(link, mgslpc_ioprobe, NULL);
611 link->conf.Attributes = CONF_ENABLE_IRQ;
612 link->conf.IntType = INT_MEMORY_AND_IO;
613 link->conf.ConfigIndex = 8;
614 link->conf.Present = PRESENT_OPTION;
616 link->irq.Attributes |= IRQ_HANDLE_PRESENT;
617 link->irq.Handler = mgslpc_isr;
618 link->irq.Instance = info;
619 CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
621 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
623 info->io_base = link->io.BasePort1;
624 info->irq_level = link->irq.AssignedIRQ;
626 /* add to linked list of devices */
627 sprintf(info->node.dev_name, "mgslpc0");
628 info->node.major = info->node.minor = 0;
629 link->dev_node = &info->node;
631 printk(KERN_INFO "%s: index 0x%02x:",
632 info->node.dev_name, link->conf.ConfigIndex);
633 if (link->conf.Attributes & CONF_ENABLE_IRQ)
634 printk(", irq %d", link->irq.AssignedIRQ);
635 if (link->io.NumPorts1)
636 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
637 link->io.BasePort1+link->io.NumPorts1-1);
642 cs_error(link, last_fn, last_ret);
643 mgslpc_release((u_long)link);
647 /* Card has been removed.
648 * Unregister device and release PCMCIA configuration.
649 * If device is open, postpone until it is closed.
651 static void mgslpc_release(u_long arg)
653 struct pcmcia_device *link = (struct pcmcia_device *)arg;
655 if (debug_level >= DEBUG_LEVEL_INFO)
656 printk("mgslpc_release(0x%p)\n", link);
658 pcmcia_disable_device(link);
661 static void mgslpc_detach(struct pcmcia_device *link)
663 if (debug_level >= DEBUG_LEVEL_INFO)
664 printk("mgslpc_detach(0x%p)\n", link);
666 ((MGSLPC_INFO *)link->priv)->stop = 1;
667 mgslpc_release((u_long)link);
669 mgslpc_remove_device((MGSLPC_INFO *)link->priv);
672 static int mgslpc_suspend(struct pcmcia_device *link)
674 MGSLPC_INFO *info = link->priv;
681 static int mgslpc_resume(struct pcmcia_device *link)
683 MGSLPC_INFO *info = link->priv;
691 static inline bool mgslpc_paranoia_check(MGSLPC_INFO *info,
692 char *name, const char *routine)
694 #ifdef MGSLPC_PARANOIA_CHECK
695 static const char *badmagic =
696 "Warning: bad magic number for mgsl struct (%s) in %s\n";
697 static const char *badinfo =
698 "Warning: null mgslpc_info for (%s) in %s\n";
701 printk(badinfo, name, routine);
704 if (info->magic != MGSLPC_MAGIC) {
705 printk(badmagic, name, routine);
716 #define CMD_RXFIFO BIT7 // release current rx FIFO
717 #define CMD_RXRESET BIT6 // receiver reset
718 #define CMD_RXFIFO_READ BIT5
719 #define CMD_START_TIMER BIT4
720 #define CMD_TXFIFO BIT3 // release current tx FIFO
721 #define CMD_TXEOM BIT1 // transmit end message
722 #define CMD_TXRESET BIT0 // transmit reset
724 static bool wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
727 /* wait for command completion */
728 while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
736 static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
738 wait_command_complete(info, channel);
739 write_reg(info, (unsigned char) (channel + CMDR), cmd);
742 static void tx_pause(struct tty_struct *tty)
744 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
747 if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
749 if (debug_level >= DEBUG_LEVEL_INFO)
750 printk("tx_pause(%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 static void tx_release(struct tty_struct *tty)
760 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
763 if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
765 if (debug_level >= DEBUG_LEVEL_INFO)
766 printk("tx_release(%s)\n",info->device_name);
768 spin_lock_irqsave(&info->lock,flags);
769 if (!info->tx_enabled)
771 spin_unlock_irqrestore(&info->lock,flags);
774 /* Return next bottom half action to perform.
775 * or 0 if nothing to do.
777 static int bh_action(MGSLPC_INFO *info)
782 spin_lock_irqsave(&info->lock,flags);
784 if (info->pending_bh & BH_RECEIVE) {
785 info->pending_bh &= ~BH_RECEIVE;
787 } else if (info->pending_bh & BH_TRANSMIT) {
788 info->pending_bh &= ~BH_TRANSMIT;
790 } else if (info->pending_bh & BH_STATUS) {
791 info->pending_bh &= ~BH_STATUS;
796 /* Mark BH routine as complete */
797 info->bh_running = false;
798 info->bh_requested = false;
801 spin_unlock_irqrestore(&info->lock,flags);
806 static void bh_handler(struct work_struct *work)
808 MGSLPC_INFO *info = container_of(work, MGSLPC_INFO, task);
809 struct tty_struct *tty;
815 if (debug_level >= DEBUG_LEVEL_BH)
816 printk( "%s(%d):bh_handler(%s) entry\n",
817 __FILE__,__LINE__,info->device_name);
819 info->bh_running = true;
820 tty = tty_port_tty_get(&info->port);
822 while((action = bh_action(info)) != 0) {
824 /* Process work item */
825 if ( debug_level >= DEBUG_LEVEL_BH )
826 printk( "%s(%d):bh_handler() work item action=%d\n",
827 __FILE__,__LINE__,action);
832 while(rx_get_frame(info, tty));
835 bh_transmit(info, tty);
841 /* unknown work item ID */
842 printk("Unknown work item ID=%08X!\n", action);
848 if (debug_level >= DEBUG_LEVEL_BH)
849 printk( "%s(%d):bh_handler(%s) exit\n",
850 __FILE__,__LINE__,info->device_name);
853 static void bh_transmit(MGSLPC_INFO *info, struct tty_struct *tty)
855 if (debug_level >= DEBUG_LEVEL_BH)
856 printk("bh_transmit() entry on %s\n", info->device_name);
862 static void bh_status(MGSLPC_INFO *info)
864 info->ri_chkcount = 0;
865 info->dsr_chkcount = 0;
866 info->dcd_chkcount = 0;
867 info->cts_chkcount = 0;
870 /* eom: non-zero = end of frame */
871 static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
873 unsigned char data[2];
874 unsigned char fifo_count, read_count, i;
875 RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
877 if (debug_level >= DEBUG_LEVEL_ISR)
878 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
880 if (!info->rx_enabled)
883 if (info->rx_frame_count >= info->rx_buf_count) {
884 /* no more free buffers */
885 issue_command(info, CHA, CMD_RXRESET);
886 info->pending_bh |= BH_RECEIVE;
887 info->rx_overflow = true;
888 info->icount.buf_overrun++;
893 /* end of frame, get FIFO count from RBCL register */
894 if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
900 if (fifo_count == 1) {
902 data[0] = read_reg(info, CHA + RXFIFO);
905 *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
907 fifo_count -= read_count;
908 if (!fifo_count && eom)
909 buf->status = data[--read_count];
911 for (i = 0; i < read_count; i++) {
912 if (buf->count >= info->max_frame_size) {
913 /* frame too large, reset receiver and reset current buffer */
914 issue_command(info, CHA, CMD_RXRESET);
918 *(buf->data + buf->count) = data[i];
921 } while (fifo_count);
924 info->pending_bh |= BH_RECEIVE;
925 info->rx_frame_count++;
927 if (info->rx_put >= info->rx_buf_count)
930 issue_command(info, CHA, CMD_RXFIFO);
933 static void rx_ready_async(MGSLPC_INFO *info, int tcd, struct tty_struct *tty)
935 unsigned char data, status, flag;
938 struct mgsl_icount *icount = &info->icount;
941 /* early termination, get FIFO count from RBCL register */
942 fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
944 /* Zero fifo count could mean 0 or 32 bytes available.
945 * If BIT5 of STAR is set then at least 1 byte is available.
947 if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
952 tty_buffer_request_room(tty, fifo_count);
953 /* Flush received async data to receive data buffer. */
955 data = read_reg(info, CHA + RXFIFO);
956 status = read_reg(info, CHA + RXFIFO);
962 // if no frameing/crc error then save data
964 // BIT6:framing error
966 if (status & (BIT7 + BIT6)) {
972 /* discard char if tty control flags say so */
973 if (status & info->ignore_status_mask)
976 status &= info->read_status_mask;
980 else if (status & BIT6)
983 work += tty_insert_flip_char(tty, data, flag);
985 issue_command(info, CHA, CMD_RXFIFO);
987 if (debug_level >= DEBUG_LEVEL_ISR) {
988 printk("%s(%d):rx_ready_async",
990 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
991 __FILE__,__LINE__,icount->rx,icount->brk,
992 icount->parity,icount->frame,icount->overrun);
996 tty_flip_buffer_push(tty);
1000 static void tx_done(MGSLPC_INFO *info, struct tty_struct *tty)
1002 if (!info->tx_active)
1005 info->tx_active = false;
1006 info->tx_aborting = false;
1008 if (info->params.mode == MGSL_MODE_ASYNC)
1011 info->tx_count = info->tx_put = info->tx_get = 0;
1012 del_timer(&info->tx_timer);
1014 if (info->drop_rts_on_tx_done) {
1016 if (info->serial_signals & SerialSignal_RTS) {
1017 info->serial_signals &= ~SerialSignal_RTS;
1020 info->drop_rts_on_tx_done = false;
1023 #if SYNCLINK_GENERIC_HDLC
1025 hdlcdev_tx_done(info);
1029 if (tty->stopped || tty->hw_stopped) {
1033 info->pending_bh |= BH_TRANSMIT;
1037 static void tx_ready(MGSLPC_INFO *info, struct tty_struct *tty)
1039 unsigned char fifo_count = 32;
1042 if (debug_level >= DEBUG_LEVEL_ISR)
1043 printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
1045 if (info->params.mode == MGSL_MODE_HDLC) {
1046 if (!info->tx_active)
1049 if (tty->stopped || tty->hw_stopped) {
1053 if (!info->tx_count)
1054 info->tx_active = false;
1057 if (!info->tx_count)
1060 while (info->tx_count && fifo_count) {
1061 c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
1064 write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
1066 write_reg16(info, CHA + TXFIFO,
1067 *((unsigned short*)(info->tx_buf + info->tx_get)));
1069 info->tx_count -= c;
1070 info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
1074 if (info->params.mode == MGSL_MODE_ASYNC) {
1075 if (info->tx_count < WAKEUP_CHARS)
1076 info->pending_bh |= BH_TRANSMIT;
1077 issue_command(info, CHA, CMD_TXFIFO);
1080 issue_command(info, CHA, CMD_TXFIFO);
1082 issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
1086 static void cts_change(MGSLPC_INFO *info, struct tty_struct *tty)
1089 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1090 irq_disable(info, CHB, IRQ_CTS);
1092 if (info->serial_signals & SerialSignal_CTS)
1093 info->input_signal_events.cts_up++;
1095 info->input_signal_events.cts_down++;
1096 wake_up_interruptible(&info->status_event_wait_q);
1097 wake_up_interruptible(&info->event_wait_q);
1099 if (info->port.flags & ASYNC_CTS_FLOW) {
1100 if (tty->hw_stopped) {
1101 if (info->serial_signals & SerialSignal_CTS) {
1102 if (debug_level >= DEBUG_LEVEL_ISR)
1103 printk("CTS tx start...");
1105 tty->hw_stopped = 0;
1106 tx_start(info, tty);
1107 info->pending_bh |= BH_TRANSMIT;
1111 if (!(info->serial_signals & SerialSignal_CTS)) {
1112 if (debug_level >= DEBUG_LEVEL_ISR)
1113 printk("CTS tx stop...");
1115 tty->hw_stopped = 1;
1120 info->pending_bh |= BH_STATUS;
1123 static void dcd_change(MGSLPC_INFO *info, struct tty_struct *tty)
1126 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1127 irq_disable(info, CHB, IRQ_DCD);
1129 if (info->serial_signals & SerialSignal_DCD) {
1130 info->input_signal_events.dcd_up++;
1133 info->input_signal_events.dcd_down++;
1134 #if SYNCLINK_GENERIC_HDLC
1135 if (info->netcount) {
1136 if (info->serial_signals & SerialSignal_DCD)
1137 netif_carrier_on(info->netdev);
1139 netif_carrier_off(info->netdev);
1142 wake_up_interruptible(&info->status_event_wait_q);
1143 wake_up_interruptible(&info->event_wait_q);
1145 if (info->port.flags & ASYNC_CHECK_CD) {
1146 if (debug_level >= DEBUG_LEVEL_ISR)
1147 printk("%s CD now %s...", info->device_name,
1148 (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
1149 if (info->serial_signals & SerialSignal_DCD)
1150 wake_up_interruptible(&info->port.open_wait);
1152 if (debug_level >= DEBUG_LEVEL_ISR)
1153 printk("doing serial hangup...");
1158 info->pending_bh |= BH_STATUS;
1161 static void dsr_change(MGSLPC_INFO *info)
1164 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1165 port_irq_disable(info, PVR_DSR);
1167 if (info->serial_signals & SerialSignal_DSR)
1168 info->input_signal_events.dsr_up++;
1170 info->input_signal_events.dsr_down++;
1171 wake_up_interruptible(&info->status_event_wait_q);
1172 wake_up_interruptible(&info->event_wait_q);
1173 info->pending_bh |= BH_STATUS;
1176 static void ri_change(MGSLPC_INFO *info)
1179 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1180 port_irq_disable(info, PVR_RI);
1182 if (info->serial_signals & SerialSignal_RI)
1183 info->input_signal_events.ri_up++;
1185 info->input_signal_events.ri_down++;
1186 wake_up_interruptible(&info->status_event_wait_q);
1187 wake_up_interruptible(&info->event_wait_q);
1188 info->pending_bh |= BH_STATUS;
1191 /* Interrupt service routine entry point.
1195 * irq interrupt number that caused interrupt
1196 * dev_id device ID supplied during interrupt registration
1198 static irqreturn_t mgslpc_isr(int dummy, void *dev_id)
1200 MGSLPC_INFO *info = dev_id;
1201 struct tty_struct *tty;
1203 unsigned char gis, pis;
1206 if (debug_level >= DEBUG_LEVEL_ISR)
1207 printk("mgslpc_isr(%d) entry.\n", info->irq_level);
1209 if (!(info->p_dev->_locked))
1212 tty = tty_port_tty_get(&info->port);
1214 spin_lock(&info->lock);
1216 while ((gis = read_reg(info, CHA + GIS))) {
1217 if (debug_level >= DEBUG_LEVEL_ISR)
1218 printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
1220 if ((gis & 0x70) || count > 1000) {
1221 printk("synclink_cs:hardware failed or ejected\n");
1226 if (gis & (BIT1 + BIT0)) {
1227 isr = read_reg16(info, CHB + ISR);
1229 dcd_change(info, tty);
1231 cts_change(info, tty);
1233 if (gis & (BIT3 + BIT2))
1235 isr = read_reg16(info, CHA + ISR);
1236 if (isr & IRQ_TIMER) {
1237 info->irq_occurred = true;
1238 irq_disable(info, CHA, IRQ_TIMER);
1242 if (isr & IRQ_EXITHUNT) {
1243 info->icount.exithunt++;
1244 wake_up_interruptible(&info->event_wait_q);
1246 if (isr & IRQ_BREAK_ON) {
1248 if (info->port.flags & ASYNC_SAK)
1251 if (isr & IRQ_RXTIME) {
1252 issue_command(info, CHA, CMD_RXFIFO_READ);
1254 if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
1255 if (info->params.mode == MGSL_MODE_HDLC)
1256 rx_ready_hdlc(info, isr & IRQ_RXEOM);
1258 rx_ready_async(info, isr & IRQ_RXEOM, tty);
1262 if (isr & IRQ_UNDERRUN) {
1263 if (info->tx_aborting)
1264 info->icount.txabort++;
1266 info->icount.txunder++;
1269 else if (isr & IRQ_ALLSENT) {
1270 info->icount.txok++;
1273 else if (isr & IRQ_TXFIFO)
1274 tx_ready(info, tty);
1277 pis = read_reg(info, CHA + PIS);
1285 /* Request bottom half processing if there's something
1286 * for it to do and the bh is not already running
1289 if (info->pending_bh && !info->bh_running && !info->bh_requested) {
1290 if ( debug_level >= DEBUG_LEVEL_ISR )
1291 printk("%s(%d):%s queueing bh task.\n",
1292 __FILE__,__LINE__,info->device_name);
1293 schedule_work(&info->task);
1294 info->bh_requested = true;
1297 spin_unlock(&info->lock);
1300 if (debug_level >= DEBUG_LEVEL_ISR)
1301 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1302 __FILE__, __LINE__, info->irq_level);
1307 /* Initialize and start device.
1309 static int startup(MGSLPC_INFO * info, struct tty_struct *tty)
1313 if (debug_level >= DEBUG_LEVEL_INFO)
1314 printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
1316 if (info->port.flags & ASYNC_INITIALIZED)
1319 if (!info->tx_buf) {
1320 /* allocate a page of memory for a transmit buffer */
1321 info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1322 if (!info->tx_buf) {
1323 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1324 __FILE__,__LINE__,info->device_name);
1329 info->pending_bh = 0;
1331 memset(&info->icount, 0, sizeof(info->icount));
1333 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
1335 /* Allocate and claim adapter resources */
1336 retval = claim_resources(info);
1338 /* perform existance check and diagnostics */
1340 retval = adapter_test(info);
1343 if (capable(CAP_SYS_ADMIN) && tty)
1344 set_bit(TTY_IO_ERROR, &tty->flags);
1345 release_resources(info);
1349 /* program hardware for current parameters */
1350 mgslpc_change_params(info, tty);
1353 clear_bit(TTY_IO_ERROR, &tty->flags);
1355 info->port.flags |= ASYNC_INITIALIZED;
1360 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1362 static void shutdown(MGSLPC_INFO * info, struct tty_struct *tty)
1364 unsigned long flags;
1366 if (!(info->port.flags & ASYNC_INITIALIZED))
1369 if (debug_level >= DEBUG_LEVEL_INFO)
1370 printk("%s(%d):mgslpc_shutdown(%s)\n",
1371 __FILE__,__LINE__, info->device_name );
1373 /* clear status wait queue because status changes */
1374 /* can't happen after shutting down the hardware */
1375 wake_up_interruptible(&info->status_event_wait_q);
1376 wake_up_interruptible(&info->event_wait_q);
1378 del_timer_sync(&info->tx_timer);
1381 free_page((unsigned long) info->tx_buf);
1382 info->tx_buf = NULL;
1385 spin_lock_irqsave(&info->lock,flags);
1390 /* TODO:disable interrupts instead of reset to preserve signal states */
1393 if (!tty || tty->termios->c_cflag & HUPCL) {
1394 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1398 spin_unlock_irqrestore(&info->lock,flags);
1400 release_resources(info);
1403 set_bit(TTY_IO_ERROR, &tty->flags);
1405 info->port.flags &= ~ASYNC_INITIALIZED;
1408 static void mgslpc_program_hw(MGSLPC_INFO *info, struct tty_struct *tty)
1410 unsigned long flags;
1412 spin_lock_irqsave(&info->lock,flags);
1416 info->tx_count = info->tx_put = info->tx_get = 0;
1418 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
1425 info->dcd_chkcount = 0;
1426 info->cts_chkcount = 0;
1427 info->ri_chkcount = 0;
1428 info->dsr_chkcount = 0;
1430 irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
1431 port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
1434 if (info->netcount || (tty && (tty->termios->c_cflag & CREAD)))
1437 spin_unlock_irqrestore(&info->lock,flags);
1440 /* Reconfigure adapter based on new parameters
1442 static void mgslpc_change_params(MGSLPC_INFO *info, struct tty_struct *tty)
1447 if (!tty || !tty->termios)
1450 if (debug_level >= DEBUG_LEVEL_INFO)
1451 printk("%s(%d):mgslpc_change_params(%s)\n",
1452 __FILE__,__LINE__, info->device_name );
1454 cflag = tty->termios->c_cflag;
1456 /* if B0 rate (hangup) specified then negate DTR and RTS */
1457 /* otherwise assert DTR and RTS */
1459 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1461 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1463 /* byte size and parity */
1465 switch (cflag & CSIZE) {
1466 case CS5: info->params.data_bits = 5; break;
1467 case CS6: info->params.data_bits = 6; break;
1468 case CS7: info->params.data_bits = 7; break;
1469 case CS8: info->params.data_bits = 8; break;
1470 default: info->params.data_bits = 7; break;
1474 info->params.stop_bits = 2;
1476 info->params.stop_bits = 1;
1478 info->params.parity = ASYNC_PARITY_NONE;
1479 if (cflag & PARENB) {
1481 info->params.parity = ASYNC_PARITY_ODD;
1483 info->params.parity = ASYNC_PARITY_EVEN;
1486 info->params.parity = ASYNC_PARITY_SPACE;
1490 /* calculate number of jiffies to transmit a full
1491 * FIFO (32 bytes) at specified data rate
1493 bits_per_char = info->params.data_bits +
1494 info->params.stop_bits + 1;
1496 /* if port data rate is set to 460800 or less then
1497 * allow tty settings to override, otherwise keep the
1498 * current data rate.
1500 if (info->params.data_rate <= 460800) {
1501 info->params.data_rate = tty_get_baud_rate(tty);
1504 if ( info->params.data_rate ) {
1505 info->timeout = (32*HZ*bits_per_char) /
1506 info->params.data_rate;
1508 info->timeout += HZ/50; /* Add .02 seconds of slop */
1510 if (cflag & CRTSCTS)
1511 info->port.flags |= ASYNC_CTS_FLOW;
1513 info->port.flags &= ~ASYNC_CTS_FLOW;
1516 info->port.flags &= ~ASYNC_CHECK_CD;
1518 info->port.flags |= ASYNC_CHECK_CD;
1520 /* process tty input control flags */
1522 info->read_status_mask = 0;
1524 info->read_status_mask |= BIT7 | BIT6;
1526 info->ignore_status_mask |= BIT7 | BIT6;
1528 mgslpc_program_hw(info, tty);
1531 /* Add a character to the transmit buffer
1533 static int mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
1535 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1536 unsigned long flags;
1538 if (debug_level >= DEBUG_LEVEL_INFO) {
1539 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1540 __FILE__,__LINE__,ch,info->device_name);
1543 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
1549 spin_lock_irqsave(&info->lock,flags);
1551 if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
1552 if (info->tx_count < TXBUFSIZE - 1) {
1553 info->tx_buf[info->tx_put++] = ch;
1554 info->tx_put &= TXBUFSIZE-1;
1559 spin_unlock_irqrestore(&info->lock,flags);
1563 /* Enable transmitter so remaining characters in the
1564 * transmit buffer are sent.
1566 static void mgslpc_flush_chars(struct tty_struct *tty)
1568 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1569 unsigned long flags;
1571 if (debug_level >= DEBUG_LEVEL_INFO)
1572 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1573 __FILE__,__LINE__,info->device_name,info->tx_count);
1575 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
1578 if (info->tx_count <= 0 || tty->stopped ||
1579 tty->hw_stopped || !info->tx_buf)
1582 if (debug_level >= DEBUG_LEVEL_INFO)
1583 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1584 __FILE__,__LINE__,info->device_name);
1586 spin_lock_irqsave(&info->lock,flags);
1587 if (!info->tx_active)
1588 tx_start(info, tty);
1589 spin_unlock_irqrestore(&info->lock,flags);
1592 /* Send a block of data
1596 * tty pointer to tty information structure
1597 * buf pointer to buffer containing send data
1598 * count size of send data in bytes
1600 * Returns: number of characters written
1602 static int mgslpc_write(struct tty_struct * tty,
1603 const unsigned char *buf, int count)
1606 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1607 unsigned long flags;
1609 if (debug_level >= DEBUG_LEVEL_INFO)
1610 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1611 __FILE__,__LINE__,info->device_name,count);
1613 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
1617 if (info->params.mode == MGSL_MODE_HDLC) {
1618 if (count > TXBUFSIZE) {
1622 if (info->tx_active)
1624 else if (info->tx_count)
1630 min(TXBUFSIZE - info->tx_count - 1,
1631 TXBUFSIZE - info->tx_put));
1635 memcpy(info->tx_buf + info->tx_put, buf, c);
1637 spin_lock_irqsave(&info->lock,flags);
1638 info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
1639 info->tx_count += c;
1640 spin_unlock_irqrestore(&info->lock,flags);
1647 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
1648 spin_lock_irqsave(&info->lock,flags);
1649 if (!info->tx_active)
1650 tx_start(info, tty);
1651 spin_unlock_irqrestore(&info->lock,flags);
1654 if (debug_level >= DEBUG_LEVEL_INFO)
1655 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1656 __FILE__,__LINE__,info->device_name,ret);
1660 /* Return the count of free bytes in transmit buffer
1662 static int mgslpc_write_room(struct tty_struct *tty)
1664 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1667 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
1670 if (info->params.mode == MGSL_MODE_HDLC) {
1671 /* HDLC (frame oriented) mode */
1672 if (info->tx_active)
1675 return HDLC_MAX_FRAME_SIZE;
1677 ret = TXBUFSIZE - info->tx_count - 1;
1682 if (debug_level >= DEBUG_LEVEL_INFO)
1683 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1684 __FILE__,__LINE__, info->device_name, ret);
1688 /* Return the count of bytes in transmit buffer
1690 static int mgslpc_chars_in_buffer(struct tty_struct *tty)
1692 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1695 if (debug_level >= DEBUG_LEVEL_INFO)
1696 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1697 __FILE__,__LINE__, info->device_name );
1699 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
1702 if (info->params.mode == MGSL_MODE_HDLC)
1703 rc = info->tx_active ? info->max_frame_size : 0;
1705 rc = info->tx_count;
1707 if (debug_level >= DEBUG_LEVEL_INFO)
1708 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1709 __FILE__,__LINE__, info->device_name, rc);
1714 /* Discard all data in the send buffer
1716 static void mgslpc_flush_buffer(struct tty_struct *tty)
1718 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1719 unsigned long flags;
1721 if (debug_level >= DEBUG_LEVEL_INFO)
1722 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1723 __FILE__,__LINE__, info->device_name );
1725 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
1728 spin_lock_irqsave(&info->lock,flags);
1729 info->tx_count = info->tx_put = info->tx_get = 0;
1730 del_timer(&info->tx_timer);
1731 spin_unlock_irqrestore(&info->lock,flags);
1733 wake_up_interruptible(&tty->write_wait);
1737 /* Send a high-priority XON/XOFF character
1739 static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
1741 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1742 unsigned long flags;
1744 if (debug_level >= DEBUG_LEVEL_INFO)
1745 printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
1746 __FILE__,__LINE__, info->device_name, ch );
1748 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
1753 spin_lock_irqsave(&info->lock,flags);
1754 if (!info->tx_enabled)
1755 tx_start(info, tty);
1756 spin_unlock_irqrestore(&info->lock,flags);
1760 /* Signal remote device to throttle send data (our receive data)
1762 static void mgslpc_throttle(struct tty_struct * tty)
1764 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1765 unsigned long flags;
1767 if (debug_level >= DEBUG_LEVEL_INFO)
1768 printk("%s(%d):mgslpc_throttle(%s) entry\n",
1769 __FILE__,__LINE__, info->device_name );
1771 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
1775 mgslpc_send_xchar(tty, STOP_CHAR(tty));
1777 if (tty->termios->c_cflag & CRTSCTS) {
1778 spin_lock_irqsave(&info->lock,flags);
1779 info->serial_signals &= ~SerialSignal_RTS;
1781 spin_unlock_irqrestore(&info->lock,flags);
1785 /* Signal remote device to stop throttling send data (our receive data)
1787 static void mgslpc_unthrottle(struct tty_struct * tty)
1789 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
1790 unsigned long flags;
1792 if (debug_level >= DEBUG_LEVEL_INFO)
1793 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1794 __FILE__,__LINE__, info->device_name );
1796 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
1803 mgslpc_send_xchar(tty, START_CHAR(tty));
1806 if (tty->termios->c_cflag & CRTSCTS) {
1807 spin_lock_irqsave(&info->lock,flags);
1808 info->serial_signals |= SerialSignal_RTS;
1810 spin_unlock_irqrestore(&info->lock,flags);
1814 /* get the current serial statistics
1816 static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
1819 if (debug_level >= DEBUG_LEVEL_INFO)
1820 printk("get_params(%s)\n", info->device_name);
1822 memset(&info->icount, 0, sizeof(info->icount));
1824 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
1831 /* get the current serial parameters
1833 static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
1836 if (debug_level >= DEBUG_LEVEL_INFO)
1837 printk("get_params(%s)\n", info->device_name);
1838 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
1844 /* set the serial parameters
1848 * info pointer to device instance data
1849 * new_params user buffer containing new serial params
1851 * Returns: 0 if success, otherwise error code
1853 static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params, struct tty_struct *tty)
1855 unsigned long flags;
1856 MGSL_PARAMS tmp_params;
1859 if (debug_level >= DEBUG_LEVEL_INFO)
1860 printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
1861 info->device_name );
1862 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
1864 if ( debug_level >= DEBUG_LEVEL_INFO )
1865 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1866 __FILE__,__LINE__,info->device_name);
1870 spin_lock_irqsave(&info->lock,flags);
1871 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
1872 spin_unlock_irqrestore(&info->lock,flags);
1874 mgslpc_change_params(info, tty);
1879 static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
1882 if (debug_level >= DEBUG_LEVEL_INFO)
1883 printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
1884 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
1890 static int set_txidle(MGSLPC_INFO * info, int idle_mode)
1892 unsigned long flags;
1893 if (debug_level >= DEBUG_LEVEL_INFO)
1894 printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
1895 spin_lock_irqsave(&info->lock,flags);
1896 info->idle_mode = idle_mode;
1898 spin_unlock_irqrestore(&info->lock,flags);
1902 static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
1905 if (debug_level >= DEBUG_LEVEL_INFO)
1906 printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
1907 COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
1913 static int set_interface(MGSLPC_INFO * info, int if_mode)
1915 unsigned long flags;
1917 if (debug_level >= DEBUG_LEVEL_INFO)
1918 printk("set_interface(%s,%d)\n", info->device_name, if_mode);
1919 spin_lock_irqsave(&info->lock,flags);
1920 info->if_mode = if_mode;
1922 val = read_reg(info, PVR) & 0x0f;
1923 switch (info->if_mode)
1925 case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
1926 case MGSL_INTERFACE_V35: val |= PVR_V35; break;
1927 case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
1929 write_reg(info, PVR, val);
1931 spin_unlock_irqrestore(&info->lock,flags);
1935 static int set_txenable(MGSLPC_INFO * info, int enable, struct tty_struct *tty)
1937 unsigned long flags;
1939 if (debug_level >= DEBUG_LEVEL_INFO)
1940 printk("set_txenable(%s,%d)\n", info->device_name, enable);
1942 spin_lock_irqsave(&info->lock,flags);
1944 if (!info->tx_enabled)
1945 tx_start(info, tty);
1947 if (info->tx_enabled)
1950 spin_unlock_irqrestore(&info->lock,flags);
1954 static int tx_abort(MGSLPC_INFO * info)
1956 unsigned long flags;
1958 if (debug_level >= DEBUG_LEVEL_INFO)
1959 printk("tx_abort(%s)\n", info->device_name);
1961 spin_lock_irqsave(&info->lock,flags);
1962 if (info->tx_active && info->tx_count &&
1963 info->params.mode == MGSL_MODE_HDLC) {
1964 /* clear data count so FIFO is not filled on next IRQ.
1965 * This results in underrun and abort transmission.
1967 info->tx_count = info->tx_put = info->tx_get = 0;
1968 info->tx_aborting = true;
1970 spin_unlock_irqrestore(&info->lock,flags);
1974 static int set_rxenable(MGSLPC_INFO * info, int enable)
1976 unsigned long flags;
1978 if (debug_level >= DEBUG_LEVEL_INFO)
1979 printk("set_rxenable(%s,%d)\n", info->device_name, enable);
1981 spin_lock_irqsave(&info->lock,flags);
1983 if (!info->rx_enabled)
1986 if (info->rx_enabled)
1989 spin_unlock_irqrestore(&info->lock,flags);
1993 /* wait for specified event to occur
1995 * Arguments: info pointer to device instance data
1996 * mask pointer to bitmask of events to wait for
1997 * Return Value: 0 if successful and bit mask updated with
1998 * of events triggerred,
1999 * otherwise error code
2001 static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
2003 unsigned long flags;
2006 struct mgsl_icount cprev, cnow;
2009 struct _input_signal_events oldsigs, newsigs;
2010 DECLARE_WAITQUEUE(wait, current);
2012 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2016 if (debug_level >= DEBUG_LEVEL_INFO)
2017 printk("wait_events(%s,%d)\n", info->device_name, mask);
2019 spin_lock_irqsave(&info->lock,flags);
2021 /* return immediately if state matches requested events */
2023 s = info->serial_signals;
2025 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2026 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2027 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2028 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2030 spin_unlock_irqrestore(&info->lock,flags);
2034 /* save current irq counts */
2035 cprev = info->icount;
2036 oldsigs = info->input_signal_events;
2038 if ((info->params.mode == MGSL_MODE_HDLC) &&
2039 (mask & MgslEvent_ExitHuntMode))
2040 irq_enable(info, CHA, IRQ_EXITHUNT);
2042 set_current_state(TASK_INTERRUPTIBLE);
2043 add_wait_queue(&info->event_wait_q, &wait);
2045 spin_unlock_irqrestore(&info->lock,flags);
2050 if (signal_pending(current)) {
2055 /* get current irq counts */
2056 spin_lock_irqsave(&info->lock,flags);
2057 cnow = info->icount;
2058 newsigs = info->input_signal_events;
2059 set_current_state(TASK_INTERRUPTIBLE);
2060 spin_unlock_irqrestore(&info->lock,flags);
2062 /* if no change, wait aborted for some reason */
2063 if (newsigs.dsr_up == oldsigs.dsr_up &&
2064 newsigs.dsr_down == oldsigs.dsr_down &&
2065 newsigs.dcd_up == oldsigs.dcd_up &&
2066 newsigs.dcd_down == oldsigs.dcd_down &&
2067 newsigs.cts_up == oldsigs.cts_up &&
2068 newsigs.cts_down == oldsigs.cts_down &&
2069 newsigs.ri_up == oldsigs.ri_up &&
2070 newsigs.ri_down == oldsigs.ri_down &&
2071 cnow.exithunt == cprev.exithunt &&
2072 cnow.rxidle == cprev.rxidle) {
2078 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2079 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2080 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2081 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2082 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2083 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2084 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2085 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2086 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2087 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2095 remove_wait_queue(&info->event_wait_q, &wait);
2096 set_current_state(TASK_RUNNING);
2098 if (mask & MgslEvent_ExitHuntMode) {
2099 spin_lock_irqsave(&info->lock,flags);
2100 if (!waitqueue_active(&info->event_wait_q))
2101 irq_disable(info, CHA, IRQ_EXITHUNT);
2102 spin_unlock_irqrestore(&info->lock,flags);
2106 PUT_USER(rc, events, mask_ptr);
2110 static int modem_input_wait(MGSLPC_INFO *info,int arg)
2112 unsigned long flags;
2114 struct mgsl_icount cprev, cnow;
2115 DECLARE_WAITQUEUE(wait, current);
2117 /* save current irq counts */
2118 spin_lock_irqsave(&info->lock,flags);
2119 cprev = info->icount;
2120 add_wait_queue(&info->status_event_wait_q, &wait);
2121 set_current_state(TASK_INTERRUPTIBLE);
2122 spin_unlock_irqrestore(&info->lock,flags);
2126 if (signal_pending(current)) {
2131 /* get new irq counts */
2132 spin_lock_irqsave(&info->lock,flags);
2133 cnow = info->icount;
2134 set_current_state(TASK_INTERRUPTIBLE);
2135 spin_unlock_irqrestore(&info->lock,flags);
2137 /* if no change, wait aborted for some reason */
2138 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2139 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2144 /* check for change in caller specified modem input */
2145 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2146 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2147 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2148 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2155 remove_wait_queue(&info->status_event_wait_q, &wait);
2156 set_current_state(TASK_RUNNING);
2160 /* return the state of the serial control and status signals
2162 static int tiocmget(struct tty_struct *tty, struct file *file)
2164 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2165 unsigned int result;
2166 unsigned long flags;
2168 spin_lock_irqsave(&info->lock,flags);
2170 spin_unlock_irqrestore(&info->lock,flags);
2172 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2173 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2174 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2175 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2176 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2177 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2179 if (debug_level >= DEBUG_LEVEL_INFO)
2180 printk("%s(%d):%s tiocmget() value=%08X\n",
2181 __FILE__,__LINE__, info->device_name, result );
2185 /* set modem control signals (DTR/RTS)
2187 static int tiocmset(struct tty_struct *tty, struct file *file,
2188 unsigned int set, unsigned int clear)
2190 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2191 unsigned long flags;
2193 if (debug_level >= DEBUG_LEVEL_INFO)
2194 printk("%s(%d):%s tiocmset(%x,%x)\n",
2195 __FILE__,__LINE__,info->device_name, set, clear);
2197 if (set & TIOCM_RTS)
2198 info->serial_signals |= SerialSignal_RTS;
2199 if (set & TIOCM_DTR)
2200 info->serial_signals |= SerialSignal_DTR;
2201 if (clear & TIOCM_RTS)
2202 info->serial_signals &= ~SerialSignal_RTS;
2203 if (clear & TIOCM_DTR)
2204 info->serial_signals &= ~SerialSignal_DTR;
2206 spin_lock_irqsave(&info->lock,flags);
2208 spin_unlock_irqrestore(&info->lock,flags);
2213 /* Set or clear transmit break condition
2215 * Arguments: tty pointer to tty instance data
2216 * break_state -1=set break condition, 0=clear
2218 static int mgslpc_break(struct tty_struct *tty, int break_state)
2220 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2221 unsigned long flags;
2223 if (debug_level >= DEBUG_LEVEL_INFO)
2224 printk("%s(%d):mgslpc_break(%s,%d)\n",
2225 __FILE__,__LINE__, info->device_name, break_state);
2227 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
2230 spin_lock_irqsave(&info->lock,flags);
2231 if (break_state == -1)
2232 set_reg_bits(info, CHA+DAFO, BIT6);
2234 clear_reg_bits(info, CHA+DAFO, BIT6);
2235 spin_unlock_irqrestore(&info->lock,flags);
2239 /* Service an IOCTL request
2243 * tty pointer to tty instance data
2244 * file pointer to associated file object for device
2245 * cmd IOCTL command code
2246 * arg command argument/context
2248 * Return Value: 0 if success, otherwise error code
2250 static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2251 unsigned int cmd, unsigned long arg)
2253 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2255 struct mgsl_icount cnow; /* kernel counter temps */
2256 struct serial_icounter_struct __user *p_cuser; /* user space */
2257 void __user *argp = (void __user *)arg;
2258 unsigned long flags;
2260 if (debug_level >= DEBUG_LEVEL_INFO)
2261 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2262 info->device_name, cmd );
2264 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
2267 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2268 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2269 if (tty->flags & (1 << TTY_IO_ERROR))
2274 case MGSL_IOCGPARAMS:
2275 return get_params(info, argp);
2276 case MGSL_IOCSPARAMS:
2277 return set_params(info, argp, tty);
2278 case MGSL_IOCGTXIDLE:
2279 return get_txidle(info, argp);
2280 case MGSL_IOCSTXIDLE:
2281 return set_txidle(info, (int)arg);
2283 return get_interface(info, argp);
2285 return set_interface(info,(int)arg);
2286 case MGSL_IOCTXENABLE:
2287 return set_txenable(info,(int)arg, tty);
2288 case MGSL_IOCRXENABLE:
2289 return set_rxenable(info,(int)arg);
2290 case MGSL_IOCTXABORT:
2291 return tx_abort(info);
2292 case MGSL_IOCGSTATS:
2293 return get_stats(info, argp);
2294 case MGSL_IOCWAITEVENT:
2295 return wait_events(info, argp);
2297 return modem_input_wait(info,(int)arg);
2299 spin_lock_irqsave(&info->lock,flags);
2300 cnow = info->icount;
2301 spin_unlock_irqrestore(&info->lock,flags);
2303 PUT_USER(error,cnow.cts, &p_cuser->cts);
2304 if (error) return error;
2305 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2306 if (error) return error;
2307 PUT_USER(error,cnow.rng, &p_cuser->rng);
2308 if (error) return error;
2309 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2310 if (error) return error;
2311 PUT_USER(error,cnow.rx, &p_cuser->rx);
2312 if (error) return error;
2313 PUT_USER(error,cnow.tx, &p_cuser->tx);
2314 if (error) return error;
2315 PUT_USER(error,cnow.frame, &p_cuser->frame);
2316 if (error) return error;
2317 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2318 if (error) return error;
2319 PUT_USER(error,cnow.parity, &p_cuser->parity);
2320 if (error) return error;
2321 PUT_USER(error,cnow.brk, &p_cuser->brk);
2322 if (error) return error;
2323 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2324 if (error) return error;
2327 return -ENOIOCTLCMD;
2332 /* Set new termios settings
2336 * tty pointer to tty structure
2337 * termios pointer to buffer to hold returned old termios
2339 static void mgslpc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2341 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2342 unsigned long flags;
2344 if (debug_level >= DEBUG_LEVEL_INFO)
2345 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
2346 tty->driver->name );
2348 /* just return if nothing has changed */
2349 if ((tty->termios->c_cflag == old_termios->c_cflag)
2350 && (RELEVANT_IFLAG(tty->termios->c_iflag)
2351 == RELEVANT_IFLAG(old_termios->c_iflag)))
2354 mgslpc_change_params(info, tty);
2356 /* Handle transition to B0 status */
2357 if (old_termios->c_cflag & CBAUD &&
2358 !(tty->termios->c_cflag & CBAUD)) {
2359 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2360 spin_lock_irqsave(&info->lock,flags);
2362 spin_unlock_irqrestore(&info->lock,flags);
2365 /* Handle transition away from B0 status */
2366 if (!(old_termios->c_cflag & CBAUD) &&
2367 tty->termios->c_cflag & CBAUD) {
2368 info->serial_signals |= SerialSignal_DTR;
2369 if (!(tty->termios->c_cflag & CRTSCTS) ||
2370 !test_bit(TTY_THROTTLED, &tty->flags)) {
2371 info->serial_signals |= SerialSignal_RTS;
2373 spin_lock_irqsave(&info->lock,flags);
2375 spin_unlock_irqrestore(&info->lock,flags);
2378 /* Handle turning off CRTSCTS */
2379 if (old_termios->c_cflag & CRTSCTS &&
2380 !(tty->termios->c_cflag & CRTSCTS)) {
2381 tty->hw_stopped = 0;
2386 static void mgslpc_close(struct tty_struct *tty, struct file * filp)
2388 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2389 struct tty_port *port = &info->port;
2391 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
2394 if (debug_level >= DEBUG_LEVEL_INFO)
2395 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2396 __FILE__,__LINE__, info->device_name, port->count);
2398 WARN_ON(!port->count);
2400 if (tty_port_close_start(port, tty, filp) == 0)
2403 if (port->flags & ASYNC_INITIALIZED)
2404 mgslpc_wait_until_sent(tty, info->timeout);
2406 mgslpc_flush_buffer(tty);
2408 tty_ldisc_flush(tty);
2409 shutdown(info, tty);
2411 tty_port_close_end(port, tty);
2412 tty_port_tty_set(port, NULL);
2414 if (debug_level >= DEBUG_LEVEL_INFO)
2415 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
2416 tty->driver->name, port->count);
2419 /* Wait until the transmitter is empty.
2421 static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
2423 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2424 unsigned long orig_jiffies, char_time;
2429 if (debug_level >= DEBUG_LEVEL_INFO)
2430 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2431 __FILE__,__LINE__, info->device_name );
2433 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
2436 if (!(info->port.flags & ASYNC_INITIALIZED))
2439 orig_jiffies = jiffies;
2441 /* Set check interval to 1/5 of estimated time to
2442 * send a character, and make it at least 1. The check
2443 * interval should also be less than the timeout.
2444 * Note: use tight timings here to satisfy the NIST-PCTS.
2447 if ( info->params.data_rate ) {
2448 char_time = info->timeout/(32 * 5);
2455 char_time = min_t(unsigned long, char_time, timeout);
2457 if (info->params.mode == MGSL_MODE_HDLC) {
2458 while (info->tx_active) {
2459 msleep_interruptible(jiffies_to_msecs(char_time));
2460 if (signal_pending(current))
2462 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2466 while ((info->tx_count || info->tx_active) &&
2468 msleep_interruptible(jiffies_to_msecs(char_time));
2469 if (signal_pending(current))
2471 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2477 if (debug_level >= DEBUG_LEVEL_INFO)
2478 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2479 __FILE__,__LINE__, info->device_name );
2482 /* Called by tty_hangup() when a hangup is signaled.
2483 * This is the same as closing all open files for the port.
2485 static void mgslpc_hangup(struct tty_struct *tty)
2487 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2489 if (debug_level >= DEBUG_LEVEL_INFO)
2490 printk("%s(%d):mgslpc_hangup(%s)\n",
2491 __FILE__,__LINE__, info->device_name );
2493 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
2496 mgslpc_flush_buffer(tty);
2497 shutdown(info, tty);
2498 tty_port_hangup(&info->port);
2501 static int carrier_raised(struct tty_port *port)
2503 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2504 unsigned long flags;
2506 spin_lock_irqsave(&info->lock,flags);
2508 spin_unlock_irqrestore(&info->lock,flags);
2510 if (info->serial_signals & SerialSignal_DCD)
2515 static void dtr_rts(struct tty_port *port, int onoff)
2517 MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
2518 unsigned long flags;
2520 spin_lock_irqsave(&info->lock,flags);
2522 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2524 info->serial_signals &= ~SerialSignal_RTS + SerialSignal_DTR;
2526 spin_unlock_irqrestore(&info->lock,flags);
2530 static int mgslpc_open(struct tty_struct *tty, struct file * filp)
2533 struct tty_port *port;
2535 unsigned long flags;
2537 /* verify range of specified line number */
2539 if ((line < 0) || (line >= mgslpc_device_count)) {
2540 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2541 __FILE__,__LINE__,line);
2545 /* find the info structure for the specified line */
2546 info = mgslpc_device_list;
2547 while(info && info->line != line)
2548 info = info->next_device;
2549 if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
2553 tty->driver_data = info;
2554 tty_port_tty_set(port, tty);
2556 if (debug_level >= DEBUG_LEVEL_INFO)
2557 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2558 __FILE__,__LINE__,tty->driver->name, port->count);
2560 /* If port is closing, signal caller to try again */
2561 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING){
2562 if (port->flags & ASYNC_CLOSING)
2563 interruptible_sleep_on(&port->close_wait);
2564 retval = ((port->flags & ASYNC_HUP_NOTIFY) ?
2565 -EAGAIN : -ERESTARTSYS);
2569 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2571 spin_lock_irqsave(&info->netlock, flags);
2572 if (info->netcount) {
2574 spin_unlock_irqrestore(&info->netlock, flags);
2577 spin_lock(&port->lock);
2579 spin_unlock(&port->lock);
2580 spin_unlock_irqrestore(&info->netlock, flags);
2582 if (port->count == 1) {
2583 /* 1st open on this device, init hardware */
2584 retval = startup(info, tty);
2589 retval = tty_port_block_til_ready(&info->port, tty, filp);
2591 if (debug_level >= DEBUG_LEVEL_INFO)
2592 printk("%s(%d):block_til_ready(%s) returned %d\n",
2593 __FILE__,__LINE__, info->device_name, retval);
2597 if (debug_level >= DEBUG_LEVEL_INFO)
2598 printk("%s(%d):mgslpc_open(%s) success\n",
2599 __FILE__,__LINE__, info->device_name);
2607 * /proc fs routines....
2610 static inline void line_info(struct seq_file *m, MGSLPC_INFO *info)
2613 unsigned long flags;
2615 seq_printf(m, "%s:io:%04X irq:%d",
2616 info->device_name, info->io_base, info->irq_level);
2618 /* output current serial signal states */
2619 spin_lock_irqsave(&info->lock,flags);
2621 spin_unlock_irqrestore(&info->lock,flags);
2625 if (info->serial_signals & SerialSignal_RTS)
2626 strcat(stat_buf, "|RTS");
2627 if (info->serial_signals & SerialSignal_CTS)
2628 strcat(stat_buf, "|CTS");
2629 if (info->serial_signals & SerialSignal_DTR)
2630 strcat(stat_buf, "|DTR");
2631 if (info->serial_signals & SerialSignal_DSR)
2632 strcat(stat_buf, "|DSR");
2633 if (info->serial_signals & SerialSignal_DCD)
2634 strcat(stat_buf, "|CD");
2635 if (info->serial_signals & SerialSignal_RI)
2636 strcat(stat_buf, "|RI");
2638 if (info->params.mode == MGSL_MODE_HDLC) {
2639 seq_printf(m, " HDLC txok:%d rxok:%d",
2640 info->icount.txok, info->icount.rxok);
2641 if (info->icount.txunder)
2642 seq_printf(m, " txunder:%d", info->icount.txunder);
2643 if (info->icount.txabort)
2644 seq_printf(m, " txabort:%d", info->icount.txabort);
2645 if (info->icount.rxshort)
2646 seq_printf(m, " rxshort:%d", info->icount.rxshort);
2647 if (info->icount.rxlong)
2648 seq_printf(m, " rxlong:%d", info->icount.rxlong);
2649 if (info->icount.rxover)
2650 seq_printf(m, " rxover:%d", info->icount.rxover);
2651 if (info->icount.rxcrc)
2652 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
2654 seq_printf(m, " ASYNC tx:%d rx:%d",
2655 info->icount.tx, info->icount.rx);
2656 if (info->icount.frame)
2657 seq_printf(m, " fe:%d", info->icount.frame);
2658 if (info->icount.parity)
2659 seq_printf(m, " pe:%d", info->icount.parity);
2660 if (info->icount.brk)
2661 seq_printf(m, " brk:%d", info->icount.brk);
2662 if (info->icount.overrun)
2663 seq_printf(m, " oe:%d", info->icount.overrun);
2666 /* Append serial signal status to end */
2667 seq_printf(m, " %s\n", stat_buf+1);
2669 seq_printf(m, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2670 info->tx_active,info->bh_requested,info->bh_running,
2674 /* Called to print information about devices
2676 static int mgslpc_proc_show(struct seq_file *m, void *v)
2680 seq_printf(m, "synclink driver:%s\n", driver_version);
2682 info = mgslpc_device_list;
2685 info = info->next_device;
2690 static int mgslpc_proc_open(struct inode *inode, struct file *file)
2692 return single_open(file, mgslpc_proc_show, NULL);
2695 static const struct file_operations mgslpc_proc_fops = {
2696 .owner = THIS_MODULE,
2697 .open = mgslpc_proc_open,
2699 .llseek = seq_lseek,
2700 .release = single_release,
2703 static int rx_alloc_buffers(MGSLPC_INFO *info)
2705 /* each buffer has header and data */
2706 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
2708 /* calculate total allocation size for 8 buffers */
2709 info->rx_buf_total_size = info->rx_buf_size * 8;
2711 /* limit total allocated memory */
2712 if (info->rx_buf_total_size > 0x10000)
2713 info->rx_buf_total_size = 0x10000;
2715 /* calculate number of buffers */
2716 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
2718 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
2719 if (info->rx_buf == NULL)
2722 rx_reset_buffers(info);
2726 static void rx_free_buffers(MGSLPC_INFO *info)
2728 kfree(info->rx_buf);
2729 info->rx_buf = NULL;
2732 static int claim_resources(MGSLPC_INFO *info)
2734 if (rx_alloc_buffers(info) < 0 ) {
2735 printk( "Cant allocate rx buffer %s\n", info->device_name);
2736 release_resources(info);
2742 static void release_resources(MGSLPC_INFO *info)
2744 if (debug_level >= DEBUG_LEVEL_INFO)
2745 printk("release_resources(%s)\n", info->device_name);
2746 rx_free_buffers(info);
2749 /* Add the specified device instance data structure to the
2750 * global linked list of devices and increment the device count.
2752 * Arguments: info pointer to device instance data
2754 static void mgslpc_add_device(MGSLPC_INFO *info)
2756 info->next_device = NULL;
2757 info->line = mgslpc_device_count;
2758 sprintf(info->device_name,"ttySLP%d",info->line);
2760 if (info->line < MAX_DEVICE_COUNT) {
2761 if (maxframe[info->line])
2762 info->max_frame_size = maxframe[info->line];
2765 mgslpc_device_count++;
2767 if (!mgslpc_device_list)
2768 mgslpc_device_list = info;
2770 MGSLPC_INFO *current_dev = mgslpc_device_list;
2771 while( current_dev->next_device )
2772 current_dev = current_dev->next_device;
2773 current_dev->next_device = info;
2776 if (info->max_frame_size < 4096)
2777 info->max_frame_size = 4096;
2778 else if (info->max_frame_size > 65535)
2779 info->max_frame_size = 65535;
2781 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2782 info->device_name, info->io_base, info->irq_level);
2784 #if SYNCLINK_GENERIC_HDLC
2789 static void mgslpc_remove_device(MGSLPC_INFO *remove_info)
2791 MGSLPC_INFO *info = mgslpc_device_list;
2792 MGSLPC_INFO *last = NULL;
2795 if (info == remove_info) {
2797 last->next_device = info->next_device;
2799 mgslpc_device_list = info->next_device;
2800 #if SYNCLINK_GENERIC_HDLC
2803 release_resources(info);
2805 mgslpc_device_count--;
2809 info = info->next_device;
2813 static struct pcmcia_device_id mgslpc_ids[] = {
2814 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
2817 MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
2819 static struct pcmcia_driver mgslpc_driver = {
2820 .owner = THIS_MODULE,
2822 .name = "synclink_cs",
2824 .probe = mgslpc_probe,
2825 .remove = mgslpc_detach,
2826 .id_table = mgslpc_ids,
2827 .suspend = mgslpc_suspend,
2828 .resume = mgslpc_resume,
2831 static const struct tty_operations mgslpc_ops = {
2832 .open = mgslpc_open,
2833 .close = mgslpc_close,
2834 .write = mgslpc_write,
2835 .put_char = mgslpc_put_char,
2836 .flush_chars = mgslpc_flush_chars,
2837 .write_room = mgslpc_write_room,
2838 .chars_in_buffer = mgslpc_chars_in_buffer,
2839 .flush_buffer = mgslpc_flush_buffer,
2840 .ioctl = mgslpc_ioctl,
2841 .throttle = mgslpc_throttle,
2842 .unthrottle = mgslpc_unthrottle,
2843 .send_xchar = mgslpc_send_xchar,
2844 .break_ctl = mgslpc_break,
2845 .wait_until_sent = mgslpc_wait_until_sent,
2846 .set_termios = mgslpc_set_termios,
2848 .start = tx_release,
2849 .hangup = mgslpc_hangup,
2850 .tiocmget = tiocmget,
2851 .tiocmset = tiocmset,
2852 .proc_fops = &mgslpc_proc_fops,
2855 static void synclink_cs_cleanup(void)
2859 printk("Unloading %s: version %s\n", driver_name, driver_version);
2861 while(mgslpc_device_list)
2862 mgslpc_remove_device(mgslpc_device_list);
2864 if (serial_driver) {
2865 if ((rc = tty_unregister_driver(serial_driver)))
2866 printk("%s(%d) failed to unregister tty driver err=%d\n",
2867 __FILE__,__LINE__,rc);
2868 put_tty_driver(serial_driver);
2871 pcmcia_unregister_driver(&mgslpc_driver);
2874 static int __init synclink_cs_init(void)
2878 if (break_on_load) {
2879 mgslpc_get_text_ptr();
2883 printk("%s %s\n", driver_name, driver_version);
2885 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
2888 serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
2889 if (!serial_driver) {
2894 /* Initialize the tty_driver structure */
2896 serial_driver->owner = THIS_MODULE;
2897 serial_driver->driver_name = "synclink_cs";
2898 serial_driver->name = "ttySLP";
2899 serial_driver->major = ttymajor;
2900 serial_driver->minor_start = 64;
2901 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2902 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2903 serial_driver->init_termios = tty_std_termios;
2904 serial_driver->init_termios.c_cflag =
2905 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2906 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2907 tty_set_operations(serial_driver, &mgslpc_ops);
2909 if ((rc = tty_register_driver(serial_driver)) < 0) {
2910 printk("%s(%d):Couldn't register serial driver\n",
2912 put_tty_driver(serial_driver);
2913 serial_driver = NULL;
2917 printk("%s %s, tty major#%d\n",
2918 driver_name, driver_version,
2919 serial_driver->major);
2924 synclink_cs_cleanup();
2928 static void __exit synclink_cs_exit(void)
2930 synclink_cs_cleanup();
2933 module_init(synclink_cs_init);
2934 module_exit(synclink_cs_exit);
2936 static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
2941 /* note:standard BRG mode is broken in V3.2 chip
2942 * so enhanced mode is always used
2950 for (M = 1; N > 64 && M < 16; M++)
2956 * BGR[7..0] contained in BGR register
2957 * BGR[9..8] contained in CCR2[7..6]
2958 * divisor = (N+1)*2^M
2960 * Note: M *must* not be zero (causes asymetric duty cycle)
2962 write_reg(info, (unsigned char) (channel + BGR),
2963 (unsigned char) ((M << 6) + N));
2964 val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
2965 val |= ((M << 4) & 0xc0);
2966 write_reg(info, (unsigned char) (channel + CCR2), val);
2970 /* Enabled the AUX clock output at the specified frequency.
2972 static void enable_auxclk(MGSLPC_INFO *info)
2978 * 07..06 MDS[1..0] 10 = transparent HDLC mode
2979 * 05 ADM Address Mode, 0 = no addr recognition
2980 * 04 TMD Timer Mode, 0 = external
2981 * 03 RAC Receiver Active, 0 = inactive
2982 * 02 RTS 0=RTS active during xmit, 1=RTS always active
2983 * 01 TRS Timer Resolution, 1=512
2984 * 00 TLP Test Loop, 0 = no loop
2990 /* channel B RTS is used to enable AUXCLK driver on SP505 */
2991 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
2993 write_reg(info, CHB + MODE, val);
2997 * 07 PU Power Up, 1=active, 0=power down
2998 * 06 MCE Master Clock Enable, 1=enabled
3000 * 04..02 SC[2..0] Encoding
3001 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3005 write_reg(info, CHB + CCR0, 0xc0);
3009 * 07 SFLG Shared Flag, 0 = disable shared flags
3010 * 06 GALP Go Active On Loop, 0 = not used
3011 * 05 GLP Go On Loop, 0 = not used
3012 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3013 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3014 * 02..00 CM[2..0] Clock Mode
3018 write_reg(info, CHB + CCR1, 0x17);
3022 * 07..06 BGR[9..8] Baud rate bits 9..8
3023 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3024 * 04 SSEL Clock source select, 1=submode b
3025 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3026 * 02 RWX Read/Write Exchange 0=disabled
3027 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3028 * 00 DIV, data inversion 0=disabled, 1=enabled
3032 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3033 write_reg(info, CHB + CCR2, 0x38);
3035 write_reg(info, CHB + CCR2, 0x30);
3039 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3040 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3041 * 05 TST1 Test Pin, 0=normal operation
3042 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3043 * 03..02 Reserved, must be 0
3044 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3048 write_reg(info, CHB + CCR4, 0x50);
3050 /* if auxclk not enabled, set internal BRG so
3051 * CTS transitions can be detected (requires TxC)
3053 if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
3054 mgslpc_set_rate(info, CHB, info->params.clock_speed);
3056 mgslpc_set_rate(info, CHB, 921600);
3059 static void loopback_enable(MGSLPC_INFO *info)
3063 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3064 val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
3065 write_reg(info, CHA + CCR1, val);
3067 /* CCR2:04 SSEL Clock source select, 1=submode b */
3068 val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
3069 write_reg(info, CHA + CCR2, val);
3071 /* set LinkSpeed if available, otherwise default to 2Mbps */
3072 if (info->params.clock_speed)
3073 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3075 mgslpc_set_rate(info, CHA, 1843200);
3077 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3078 val = read_reg(info, CHA + MODE) | BIT0;
3079 write_reg(info, CHA + MODE, val);
3082 static void hdlc_mode(MGSLPC_INFO *info)
3085 unsigned char clkmode, clksubmode;
3087 /* disable all interrupts */
3088 irq_disable(info, CHA, 0xffff);
3089 irq_disable(info, CHB, 0xffff);
3090 port_irq_disable(info, 0xff);
3092 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3093 clkmode = clksubmode = 0;
3094 if (info->params.flags & HDLC_FLAG_RXC_DPLL
3095 && info->params.flags & HDLC_FLAG_TXC_DPLL) {
3096 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3098 } else if (info->params.flags & HDLC_FLAG_RXC_BRG
3099 && info->params.flags & HDLC_FLAG_TXC_BRG) {
3100 /* clock mode 7b, rcv = BRG, xmt = BRG */
3103 } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
3104 if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3105 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3109 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3112 } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
3113 /* clock mode 0b, rcv = RxC, xmt = BRG */
3119 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3120 * 05 ADM Address Mode, 0 = no addr recognition
3121 * 04 TMD Timer Mode, 0 = external
3122 * 03 RAC Receiver Active, 0 = inactive
3123 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3124 * 01 TRS Timer Resolution, 1=512
3125 * 00 TLP Test Loop, 0 = no loop
3130 if (info->params.loopback)
3133 /* preserve RTS state */
3134 if (info->serial_signals & SerialSignal_RTS)
3136 write_reg(info, CHA + MODE, val);
3140 * 07 PU Power Up, 1=active, 0=power down
3141 * 06 MCE Master Clock Enable, 1=enabled
3143 * 04..02 SC[2..0] Encoding
3144 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3149 switch (info->params.encoding)
3151 case HDLC_ENCODING_NRZI:
3154 case HDLC_ENCODING_BIPHASE_SPACE:
3157 case HDLC_ENCODING_BIPHASE_MARK:
3160 case HDLC_ENCODING_BIPHASE_LEVEL:
3162 break; // Manchester
3164 write_reg(info, CHA + CCR0, val);
3168 * 07 SFLG Shared Flag, 0 = disable shared flags
3169 * 06 GALP Go Active On Loop, 0 = not used
3170 * 05 GLP Go On Loop, 0 = not used
3171 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3172 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3173 * 02..00 CM[2..0] Clock Mode
3177 val = 0x10 + clkmode;
3178 write_reg(info, CHA + CCR1, val);
3182 * 07..06 BGR[9..8] Baud rate bits 9..8
3183 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3184 * 04 SSEL Clock source select, 1=submode b
3185 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3186 * 02 RWX Read/Write Exchange 0=disabled
3187 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3188 * 00 DIV, data inversion 0=disabled, 1=enabled
3193 if (clkmode == 2 || clkmode == 3 || clkmode == 6
3194 || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
3198 if (info->params.crc_type == HDLC_CRC_32_CCITT)
3200 if (info->params.encoding == HDLC_ENCODING_NRZB)
3202 write_reg(info, CHA + CCR2, val);
3206 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3207 * 05 EPT Enable preamble transmission, 1=enabled
3208 * 04 RADD Receive address pushed to FIFO, 0=disabled
3209 * 03 CRL CRC Reset Level, 0=FFFF
3210 * 02 RCRC Rx CRC 0=On 1=Off
3211 * 01 TCRC Tx CRC 0=On 1=Off
3212 * 00 PSD DPLL Phase Shift Disable
3217 if (info->params.crc_type == HDLC_CRC_NONE)
3219 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
3221 switch (info->params.preamble_length)
3223 case HDLC_PREAMBLE_LENGTH_16BITS:
3226 case HDLC_PREAMBLE_LENGTH_32BITS:
3229 case HDLC_PREAMBLE_LENGTH_64BITS:
3233 write_reg(info, CHA + CCR3, val);
3235 /* PRE - Preamble pattern */
3237 switch (info->params.preamble)
3239 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
3240 case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
3241 case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
3242 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
3244 write_reg(info, CHA + PRE, val);
3248 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3249 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3250 * 05 TST1 Test Pin, 0=normal operation
3251 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3252 * 03..02 Reserved, must be 0
3253 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3258 write_reg(info, CHA + CCR4, val);
3259 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
3260 mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
3262 mgslpc_set_rate(info, CHA, info->params.clock_speed);
3264 /* RLCR Receive length check register
3266 * 7 1=enable receive length check
3267 * 6..0 Max frame length = (RL + 1) * 32
3269 write_reg(info, CHA + RLCR, 0);
3271 /* XBCH Transmit Byte Count High
3273 * 07 DMA mode, 0 = interrupt driven
3274 * 06 NRM, 0=ABM (ignored)
3275 * 05 CAS Carrier Auto Start
3276 * 04 XC Transmit Continuously (ignored)
3277 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3282 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3284 write_reg(info, CHA + XBCH, val);
3285 enable_auxclk(info);
3286 if (info->params.loopback || info->testing_irq)
3287 loopback_enable(info);
3288 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3290 irq_enable(info, CHB, IRQ_CTS);
3291 /* PVR[3] 1=AUTO CTS active */
3292 set_reg_bits(info, CHA + PVR, BIT3);
3294 clear_reg_bits(info, CHA + PVR, BIT3);
3296 irq_enable(info, CHA,
3297 IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
3298 IRQ_UNDERRUN + IRQ_TXFIFO);
3299 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3300 wait_command_complete(info, CHA);
3301 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3303 /* Master clock mode enabled above to allow reset commands
3304 * to complete even if no data clocks are present.
3306 * Disable master clock mode for normal communications because
3307 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3308 * IRQ when in master clock mode.
3310 * Leave master clock mode enabled for IRQ test because the
3311 * timer IRQ used by the test can only happen in master clock mode.
3313 if (!info->testing_irq)
3314 clear_reg_bits(info, CHA + CCR0, BIT6);
3322 static void rx_stop(MGSLPC_INFO *info)
3324 if (debug_level >= DEBUG_LEVEL_ISR)
3325 printk("%s(%d):rx_stop(%s)\n",
3326 __FILE__,__LINE__, info->device_name );
3328 /* MODE:03 RAC Receiver Active, 0=inactive */
3329 clear_reg_bits(info, CHA + MODE, BIT3);
3331 info->rx_enabled = false;
3332 info->rx_overflow = false;
3335 static void rx_start(MGSLPC_INFO *info)
3337 if (debug_level >= DEBUG_LEVEL_ISR)
3338 printk("%s(%d):rx_start(%s)\n",
3339 __FILE__,__LINE__, info->device_name );
3341 rx_reset_buffers(info);
3342 info->rx_enabled = false;
3343 info->rx_overflow = false;
3345 /* MODE:03 RAC Receiver Active, 1=active */
3346 set_reg_bits(info, CHA + MODE, BIT3);
3348 info->rx_enabled = true;
3351 static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty)
3353 if (debug_level >= DEBUG_LEVEL_ISR)
3354 printk("%s(%d):tx_start(%s)\n",
3355 __FILE__,__LINE__, info->device_name );
3357 if (info->tx_count) {
3358 /* If auto RTS enabled and RTS is inactive, then assert */
3359 /* RTS and set a flag indicating that the driver should */
3360 /* negate RTS when the transmission completes. */
3361 info->drop_rts_on_tx_done = false;
3363 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3365 if (!(info->serial_signals & SerialSignal_RTS)) {
3366 info->serial_signals |= SerialSignal_RTS;
3368 info->drop_rts_on_tx_done = true;
3372 if (info->params.mode == MGSL_MODE_ASYNC) {
3373 if (!info->tx_active) {
3374 info->tx_active = true;
3375 tx_ready(info, tty);
3378 info->tx_active = true;
3379 tx_ready(info, tty);
3380 mod_timer(&info->tx_timer, jiffies +
3381 msecs_to_jiffies(5000));
3385 if (!info->tx_enabled)
3386 info->tx_enabled = true;
3389 static void tx_stop(MGSLPC_INFO *info)
3391 if (debug_level >= DEBUG_LEVEL_ISR)
3392 printk("%s(%d):tx_stop(%s)\n",
3393 __FILE__,__LINE__, info->device_name );
3395 del_timer(&info->tx_timer);
3397 info->tx_enabled = false;
3398 info->tx_active = false;
3401 /* Reset the adapter to a known state and prepare it for further use.
3403 static void reset_device(MGSLPC_INFO *info)
3405 /* power up both channels (set BIT7) */
3406 write_reg(info, CHA + CCR0, 0x80);
3407 write_reg(info, CHB + CCR0, 0x80);
3408 write_reg(info, CHA + MODE, 0);
3409 write_reg(info, CHB + MODE, 0);
3411 /* disable all interrupts */
3412 irq_disable(info, CHA, 0xffff);
3413 irq_disable(info, CHB, 0xffff);
3414 port_irq_disable(info, 0xff);
3416 /* PCR Port Configuration Register
3418 * 07..04 DEC[3..0] Serial I/F select outputs
3419 * 03 output, 1=AUTO CTS control enabled
3420 * 02 RI Ring Indicator input 0=active
3421 * 01 DSR input 0=active
3422 * 00 DTR output 0=active
3426 write_reg(info, PCR, 0x06);
3428 /* PVR Port Value Register
3430 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3431 * 03 AUTO CTS output 1=enabled
3432 * 02 RI Ring Indicator input
3434 * 00 DTR output (1=inactive)
3438 // write_reg(info, PVR, PVR_DTR);
3440 /* IPC Interrupt Port Configuration
3442 * 07 VIS 1=Masked interrupts visible
3443 * 06..05 Reserved, 0
3444 * 04..03 SLA Slave address, 00 ignored
3445 * 02 CASM Cascading Mode, 1=daisy chain
3446 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3450 write_reg(info, IPC, 0x05);
3453 static void async_mode(MGSLPC_INFO *info)
3457 /* disable all interrupts */
3458 irq_disable(info, CHA, 0xffff);
3459 irq_disable(info, CHB, 0xffff);
3460 port_irq_disable(info, 0xff);
3465 * 06 FRTS RTS State, 0=active
3466 * 05 FCTS Flow Control on CTS
3467 * 04 FLON Flow Control Enable
3468 * 03 RAC Receiver Active, 0 = inactive
3469 * 02 RTS 0=Auto RTS, 1=manual RTS
3470 * 01 TRS Timer Resolution, 1=512
3471 * 00 TLP Test Loop, 0 = no loop
3476 if (info->params.loopback)
3479 /* preserve RTS state */
3480 if (!(info->serial_signals & SerialSignal_RTS))
3482 write_reg(info, CHA + MODE, val);
3486 * 07 PU Power Up, 1=active, 0=power down
3487 * 06 MCE Master Clock Enable, 1=enabled
3489 * 04..02 SC[2..0] Encoding, 000=NRZ
3490 * 01..00 SM[1..0] Serial Mode, 11=Async
3494 write_reg(info, CHA + CCR0, 0x83);
3498 * 07..05 Reserved, 0
3499 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3500 * 03 BCR Bit Clock Rate, 1=16x
3501 * 02..00 CM[2..0] Clock Mode, 111=BRG
3505 write_reg(info, CHA + CCR1, 0x1f);
3509 * 07..06 BGR[9..8] Baud rate bits 9..8
3510 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3511 * 04 SSEL Clock source select, 1=submode b
3512 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3513 * 02 RWX Read/Write Exchange 0=disabled
3515 * 00 DIV, data inversion 0=disabled, 1=enabled
3519 write_reg(info, CHA + CCR2, 0x10);
3523 * 07..01 Reserved, 0
3524 * 00 PSD DPLL Phase Shift Disable
3528 write_reg(info, CHA + CCR3, 0);
3532 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3533 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3534 * 05 TST1 Test Pin, 0=normal operation
3535 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3536 * 03..00 Reserved, must be 0
3540 write_reg(info, CHA + CCR4, 0x50);
3541 mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
3546 * 06 XBRK transmit break, 0=normal operation
3547 * 05 Stop bits (0=1, 1=2)
3548 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3549 * 02 PAREN Parity Enable
3550 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3554 if (info->params.data_bits != 8)
3555 val |= BIT0; /* 7 bits */
3556 if (info->params.stop_bits != 1)
3558 if (info->params.parity != ASYNC_PARITY_NONE)
3560 val |= BIT2; /* Parity enable */
3561 if (info->params.parity == ASYNC_PARITY_ODD)
3566 write_reg(info, CHA + DAFO, val);
3568 /* RFC Rx FIFO Control
3571 * 06 DPS, 1=parity bit not stored in data byte
3572 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3573 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3574 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3576 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3580 write_reg(info, CHA + RFC, 0x5c);
3582 /* RLCR Receive length check register
3584 * Max frame length = (RL + 1) * 32
3586 write_reg(info, CHA + RLCR, 0);
3588 /* XBCH Transmit Byte Count High
3590 * 07 DMA mode, 0 = interrupt driven
3591 * 06 NRM, 0=ABM (ignored)
3592 * 05 CAS Carrier Auto Start
3593 * 04 XC Transmit Continuously (ignored)
3594 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3599 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
3601 write_reg(info, CHA + XBCH, val);
3602 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
3603 irq_enable(info, CHA, IRQ_CTS);
3605 /* MODE:03 RAC Receiver Active, 1=active */
3606 set_reg_bits(info, CHA + MODE, BIT3);
3607 enable_auxclk(info);
3608 if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
3609 irq_enable(info, CHB, IRQ_CTS);
3610 /* PVR[3] 1=AUTO CTS active */
3611 set_reg_bits(info, CHA + PVR, BIT3);
3613 clear_reg_bits(info, CHA + PVR, BIT3);
3614 irq_enable(info, CHA,
3615 IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
3616 IRQ_ALLSENT + IRQ_TXFIFO);
3617 issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
3618 wait_command_complete(info, CHA);
3619 read_reg16(info, CHA + ISR); /* clear pending IRQs */
3622 /* Set the HDLC idle mode for the transmitter.
3624 static void tx_set_idle(MGSLPC_INFO *info)
3626 /* Note: ESCC2 only supports flags and one idle modes */
3627 if (info->idle_mode == HDLC_TXIDLE_FLAGS)
3628 set_reg_bits(info, CHA + CCR1, BIT3);
3630 clear_reg_bits(info, CHA + CCR1, BIT3);
3633 /* get state of the V24 status (input) signals.
3635 static void get_signals(MGSLPC_INFO *info)
3637 unsigned char status = 0;
3639 /* preserve DTR and RTS */
3640 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
3642 if (read_reg(info, CHB + VSTR) & BIT7)
3643 info->serial_signals |= SerialSignal_DCD;
3644 if (read_reg(info, CHB + STAR) & BIT1)
3645 info->serial_signals |= SerialSignal_CTS;
3647 status = read_reg(info, CHA + PVR);
3648 if (!(status & PVR_RI))
3649 info->serial_signals |= SerialSignal_RI;
3650 if (!(status & PVR_DSR))
3651 info->serial_signals |= SerialSignal_DSR;
3654 /* Set the state of DTR and RTS based on contents of
3655 * serial_signals member of device extension.
3657 static void set_signals(MGSLPC_INFO *info)
3661 val = read_reg(info, CHA + MODE);
3662 if (info->params.mode == MGSL_MODE_ASYNC) {
3663 if (info->serial_signals & SerialSignal_RTS)
3668 if (info->serial_signals & SerialSignal_RTS)
3673 write_reg(info, CHA + MODE, val);
3675 if (info->serial_signals & SerialSignal_DTR)
3676 clear_reg_bits(info, CHA + PVR, PVR_DTR);
3678 set_reg_bits(info, CHA + PVR, PVR_DTR);
3681 static void rx_reset_buffers(MGSLPC_INFO *info)
3688 info->rx_frame_count = 0;
3689 for (i=0 ; i < info->rx_buf_count ; i++) {
3690 buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
3691 buf->status = buf->count = 0;
3695 /* Attempt to return a received HDLC frame
3696 * Only frames received without errors are returned.
3698 * Returns true if frame returned, otherwise false
3700 static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty)
3702 unsigned short status;
3704 unsigned int framesize = 0;
3705 unsigned long flags;
3706 bool return_frame = false;
3708 if (info->rx_frame_count == 0)
3711 buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
3713 status = buf->status;
3715 /* 07 VFR 1=valid frame
3716 * 06 RDO 1=data overrun
3717 * 05 CRC 1=OK, 0=error
3718 * 04 RAB 1=frame aborted
3720 if ((status & 0xf0) != 0xA0) {
3721 if (!(status & BIT7) || (status & BIT4))
3722 info->icount.rxabort++;
3723 else if (status & BIT6)
3724 info->icount.rxover++;
3725 else if (!(status & BIT5)) {
3726 info->icount.rxcrc++;
3727 if (info->params.crc_type & HDLC_CRC_RETURN_EX)
3728 return_frame = true;
3731 #if SYNCLINK_GENERIC_HDLC
3733 info->netdev->stats.rx_errors++;
3734 info->netdev->stats.rx_frame_errors++;
3738 return_frame = true;
3741 framesize = buf->count;
3743 if (debug_level >= DEBUG_LEVEL_BH)
3744 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3745 __FILE__,__LINE__,info->device_name,status,framesize);
3747 if (debug_level >= DEBUG_LEVEL_DATA)
3748 trace_block(info, buf->data, framesize, 0);
3751 if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
3752 framesize+1 > info->max_frame_size) ||
3753 framesize > info->max_frame_size)
3754 info->icount.rxlong++;
3757 info->icount.rxok++;
3759 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
3760 *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
3764 #if SYNCLINK_GENERIC_HDLC
3766 hdlcdev_rx(info, buf->data, framesize);
3769 ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
3773 spin_lock_irqsave(&info->lock,flags);
3774 buf->status = buf->count = 0;
3775 info->rx_frame_count--;
3777 if (info->rx_get >= info->rx_buf_count)
3779 spin_unlock_irqrestore(&info->lock,flags);
3784 static bool register_test(MGSLPC_INFO *info)
3786 static unsigned char patterns[] =
3787 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
3788 static unsigned int count = ARRAY_SIZE(patterns);
3791 unsigned long flags;
3793 spin_lock_irqsave(&info->lock,flags);
3796 for (i = 0; i < count; i++) {
3797 write_reg(info, XAD1, patterns[i]);
3798 write_reg(info, XAD2, patterns[(i + 1) % count]);
3799 if ((read_reg(info, XAD1) != patterns[i]) ||
3800 (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
3806 spin_unlock_irqrestore(&info->lock,flags);
3810 static bool irq_test(MGSLPC_INFO *info)
3812 unsigned long end_time;
3813 unsigned long flags;
3815 spin_lock_irqsave(&info->lock,flags);
3818 info->testing_irq = true;
3821 info->irq_occurred = false;
3823 /* init hdlc mode */
3825 irq_enable(info, CHA, IRQ_TIMER);
3826 write_reg(info, CHA + TIMR, 0); /* 512 cycles */
3827 issue_command(info, CHA, CMD_START_TIMER);
3829 spin_unlock_irqrestore(&info->lock,flags);
3832 while(end_time-- && !info->irq_occurred) {
3833 msleep_interruptible(10);
3836 info->testing_irq = false;
3838 spin_lock_irqsave(&info->lock,flags);
3840 spin_unlock_irqrestore(&info->lock,flags);
3842 return info->irq_occurred;
3845 static int adapter_test(MGSLPC_INFO *info)
3847 if (!register_test(info)) {
3848 info->init_error = DiagStatus_AddressFailure;
3849 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
3850 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
3854 if (!irq_test(info)) {
3855 info->init_error = DiagStatus_IrqFailure;
3856 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
3857 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
3861 if (debug_level >= DEBUG_LEVEL_INFO)
3862 printk("%s(%d):device %s passed diagnostics\n",
3863 __FILE__,__LINE__,info->device_name);
3867 static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
3872 printk("%s tx data:\n",info->device_name);
3874 printk("%s rx data:\n",info->device_name);
3882 for(i=0;i<linecount;i++)
3883 printk("%02X ",(unsigned char)data[i]);
3886 for(i=0;i<linecount;i++) {
3887 if (data[i]>=040 && data[i]<=0176)
3888 printk("%c",data[i]);
3899 /* HDLC frame time out
3900 * update stats and do tx completion processing
3902 static void tx_timeout(unsigned long context)
3904 MGSLPC_INFO *info = (MGSLPC_INFO*)context;
3905 unsigned long flags;
3907 if ( debug_level >= DEBUG_LEVEL_INFO )
3908 printk( "%s(%d):tx_timeout(%s)\n",
3909 __FILE__,__LINE__,info->device_name);
3910 if(info->tx_active &&
3911 info->params.mode == MGSL_MODE_HDLC) {
3912 info->icount.txtimeout++;
3914 spin_lock_irqsave(&info->lock,flags);
3915 info->tx_active = false;
3916 info->tx_count = info->tx_put = info->tx_get = 0;
3918 spin_unlock_irqrestore(&info->lock,flags);
3920 #if SYNCLINK_GENERIC_HDLC
3922 hdlcdev_tx_done(info);
3926 struct tty_struct *tty = tty_port_tty_get(&info->port);
3927 bh_transmit(info, tty);
3932 #if SYNCLINK_GENERIC_HDLC
3935 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
3936 * set encoding and frame check sequence (FCS) options
3938 * dev pointer to network device structure
3939 * encoding serial encoding setting
3940 * parity FCS setting
3942 * returns 0 if success, otherwise error code
3944 static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
3945 unsigned short parity)
3947 MGSLPC_INFO *info = dev_to_port(dev);
3948 struct tty_struct *tty;
3949 unsigned char new_encoding;
3950 unsigned short new_crctype;
3952 /* return error if TTY interface open */
3953 if (info->port.count)
3958 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
3959 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
3960 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
3961 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
3962 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
3963 default: return -EINVAL;
3968 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
3969 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
3970 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
3971 default: return -EINVAL;
3974 info->params.encoding = new_encoding;
3975 info->params.crc_type = new_crctype;
3977 /* if network interface up, reprogram hardware */
3978 if (info->netcount) {
3979 tty = tty_port_tty_get(&info->port);
3980 mgslpc_program_hw(info, tty);
3988 * called by generic HDLC layer to send frame
3990 * skb socket buffer containing HDLC frame
3991 * dev pointer to network device structure
3993 static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
3994 struct net_device *dev)
3996 MGSLPC_INFO *info = dev_to_port(dev);
3997 unsigned long flags;
3999 if (debug_level >= DEBUG_LEVEL_INFO)
4000 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
4002 /* stop sending until this frame completes */
4003 netif_stop_queue(dev);
4005 /* copy data to device buffers */
4006 skb_copy_from_linear_data(skb, info->tx_buf, skb->len);
4008 info->tx_put = info->tx_count = skb->len;
4010 /* update network statistics */
4011 dev->stats.tx_packets++;
4012 dev->stats.tx_bytes += skb->len;
4014 /* done with socket buffer, so free it */
4017 /* save start time for transmit timeout detection */
4018 dev->trans_start = jiffies;
4020 /* start hardware transmitter if necessary */
4021 spin_lock_irqsave(&info->lock,flags);
4022 if (!info->tx_active) {
4023 struct tty_struct *tty = tty_port_tty_get(&info->port);
4024 tx_start(info, tty);
4027 spin_unlock_irqrestore(&info->lock,flags);
4029 return NETDEV_TX_OK;
4033 * called by network layer when interface enabled
4034 * claim resources and initialize hardware
4036 * dev pointer to network device structure
4038 * returns 0 if success, otherwise error code
4040 static int hdlcdev_open(struct net_device *dev)
4042 MGSLPC_INFO *info = dev_to_port(dev);
4043 struct tty_struct *tty;
4045 unsigned long flags;
4047 if (debug_level >= DEBUG_LEVEL_INFO)
4048 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
4050 /* generic HDLC layer open processing */
4051 if ((rc = hdlc_open(dev)))
4054 /* arbitrate between network and tty opens */
4055 spin_lock_irqsave(&info->netlock, flags);
4056 if (info->port.count != 0 || info->netcount != 0) {
4057 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
4058 spin_unlock_irqrestore(&info->netlock, flags);
4062 spin_unlock_irqrestore(&info->netlock, flags);
4064 tty = tty_port_tty_get(&info->port);
4065 /* claim resources and init adapter */
4066 if ((rc = startup(info, tty)) != 0) {
4068 spin_lock_irqsave(&info->netlock, flags);
4070 spin_unlock_irqrestore(&info->netlock, flags);
4073 /* assert DTR and RTS, apply hardware settings */
4074 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
4075 mgslpc_program_hw(info, tty);
4078 /* enable network layer transmit */
4079 dev->trans_start = jiffies;
4080 netif_start_queue(dev);
4082 /* inform generic HDLC layer of current DCD status */
4083 spin_lock_irqsave(&info->lock, flags);
4085 spin_unlock_irqrestore(&info->lock, flags);
4086 if (info->serial_signals & SerialSignal_DCD)
4087 netif_carrier_on(dev);
4089 netif_carrier_off(dev);
4094 * called by network layer when interface is disabled
4095 * shutdown hardware and release resources
4097 * dev pointer to network device structure
4099 * returns 0 if success, otherwise error code
4101 static int hdlcdev_close(struct net_device *dev)
4103 MGSLPC_INFO *info = dev_to_port(dev);
4104 struct tty_struct *tty = tty_port_tty_get(&info->port);
4105 unsigned long flags;
4107 if (debug_level >= DEBUG_LEVEL_INFO)
4108 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
4110 netif_stop_queue(dev);
4112 /* shutdown adapter and release resources */
4113 shutdown(info, tty);
4117 spin_lock_irqsave(&info->netlock, flags);
4119 spin_unlock_irqrestore(&info->netlock, flags);
4125 * called by network layer to process IOCTL call to network device
4127 * dev pointer to network device structure
4128 * ifr pointer to network interface request structure
4129 * cmd IOCTL command code
4131 * returns 0 if success, otherwise error code
4133 static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4135 const size_t size = sizeof(sync_serial_settings);
4136 sync_serial_settings new_line;
4137 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
4138 MGSLPC_INFO *info = dev_to_port(dev);
4141 if (debug_level >= DEBUG_LEVEL_INFO)
4142 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
4144 /* return error if TTY interface open */
4145 if (info->port.count)
4148 if (cmd != SIOCWANDEV)
4149 return hdlc_ioctl(dev, ifr, cmd);
4151 switch(ifr->ifr_settings.type) {
4152 case IF_GET_IFACE: /* return current sync_serial_settings */
4154 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
4155 if (ifr->ifr_settings.size < size) {
4156 ifr->ifr_settings.size = size; /* data size wanted */
4160 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4161 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4162 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4163 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4166 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
4167 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
4168 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
4169 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
4170 default: new_line.clock_type = CLOCK_DEFAULT;
4173 new_line.clock_rate = info->params.clock_speed;
4174 new_line.loopback = info->params.loopback ? 1:0;
4176 if (copy_to_user(line, &new_line, size))
4180 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
4182 if(!capable(CAP_NET_ADMIN))
4184 if (copy_from_user(&new_line, line, size))
4187 switch (new_line.clock_type)
4189 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
4190 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
4191 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
4192 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
4193 case CLOCK_DEFAULT: flags = info->params.flags &
4194 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4195 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4196 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4197 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
4198 default: return -EINVAL;
4201 if (new_line.loopback != 0 && new_line.loopback != 1)
4204 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
4205 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
4206 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
4207 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
4208 info->params.flags |= flags;
4210 info->params.loopback = new_line.loopback;
4212 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
4213 info->params.clock_speed = new_line.clock_rate;
4215 info->params.clock_speed = 0;
4217 /* if network interface up, reprogram hardware */
4218 if (info->netcount) {
4219 struct tty_struct *tty = tty_port_tty_get(&info->port);
4220 mgslpc_program_hw(info, tty);
4226 return hdlc_ioctl(dev, ifr, cmd);
4231 * called by network layer when transmit timeout is detected
4233 * dev pointer to network device structure
4235 static void hdlcdev_tx_timeout(struct net_device *dev)
4237 MGSLPC_INFO *info = dev_to_port(dev);
4238 unsigned long flags;
4240 if (debug_level >= DEBUG_LEVEL_INFO)
4241 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
4243 dev->stats.tx_errors++;
4244 dev->stats.tx_aborted_errors++;
4246 spin_lock_irqsave(&info->lock,flags);
4248 spin_unlock_irqrestore(&info->lock,flags);
4250 netif_wake_queue(dev);
4254 * called by device driver when transmit completes
4255 * reenable network layer transmit if stopped
4257 * info pointer to device instance information
4259 static void hdlcdev_tx_done(MGSLPC_INFO *info)
4261 if (netif_queue_stopped(info->netdev))
4262 netif_wake_queue(info->netdev);
4266 * called by device driver when frame received
4267 * pass frame to network layer
4269 * info pointer to device instance information
4270 * buf pointer to buffer contianing frame data
4271 * size count of data bytes in buf
4273 static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
4275 struct sk_buff *skb = dev_alloc_skb(size);
4276 struct net_device *dev = info->netdev;
4278 if (debug_level >= DEBUG_LEVEL_INFO)
4279 printk("hdlcdev_rx(%s)\n",dev->name);
4282 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
4283 dev->stats.rx_dropped++;
4287 memcpy(skb_put(skb, size), buf, size);
4289 skb->protocol = hdlc_type_trans(skb, dev);
4291 dev->stats.rx_packets++;
4292 dev->stats.rx_bytes += size;
4297 static const struct net_device_ops hdlcdev_ops = {
4298 .ndo_open = hdlcdev_open,
4299 .ndo_stop = hdlcdev_close,
4300 .ndo_change_mtu = hdlc_change_mtu,
4301 .ndo_start_xmit = hdlc_start_xmit,
4302 .ndo_do_ioctl = hdlcdev_ioctl,
4303 .ndo_tx_timeout = hdlcdev_tx_timeout,
4307 * called by device driver when adding device instance
4308 * do generic HDLC initialization
4310 * info pointer to device instance information
4312 * returns 0 if success, otherwise error code
4314 static int hdlcdev_init(MGSLPC_INFO *info)
4317 struct net_device *dev;
4320 /* allocate and initialize network and HDLC layer objects */
4322 if (!(dev = alloc_hdlcdev(info))) {
4323 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
4327 /* for network layer reporting purposes only */
4328 dev->base_addr = info->io_base;
4329 dev->irq = info->irq_level;
4331 /* network layer callbacks and settings */
4332 dev->netdev_ops = &hdlcdev_ops;
4333 dev->watchdog_timeo = 10 * HZ;
4334 dev->tx_queue_len = 50;
4336 /* generic HDLC layer callbacks and settings */
4337 hdlc = dev_to_hdlc(dev);
4338 hdlc->attach = hdlcdev_attach;
4339 hdlc->xmit = hdlcdev_xmit;
4341 /* register objects with HDLC layer */
4342 if ((rc = register_hdlc_device(dev))) {
4343 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
4353 * called by device driver when removing device instance
4354 * do generic HDLC cleanup
4356 * info pointer to device instance information
4358 static void hdlcdev_exit(MGSLPC_INFO *info)
4360 unregister_hdlc_device(info->netdev);
4361 free_netdev(info->netdev);
4362 info->netdev = NULL;
4365 #endif /* CONFIG_HDLC */