2 * linux/drivers/char/pcmcia/synclink_cs.c
4 * $Id: synclink_cs.c,v 4.34 2005/09/08 13:20:54 Exp $
6 * Device driver for Microgate SyncLink PC Card
7 * multiprotocol serial adapter.
9 * written by Paul Fulghum for Microgate Corporation
10 * paulkf@microgate.com
12 * Microgate and SyncLink are trademarks of Microgate Corporation
14 * This code is released under the GNU General Public License (GPL)
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
31 # define BREAKPOINT() asm(" int $3");
33 # define BREAKPOINT() { }
36 #define MAX_DEVICE_COUNT 4
38 #include <linux/module.h>
39 #include <linux/errno.h>
40 #include <linux/signal.h>
41 #include <linux/sched.h>
42 #include <linux/timer.h>
43 #include <linux/time.h>
44 #include <linux/interrupt.h>
45 #include <linux/tty.h>
46 #include <linux/tty_flip.h>
47 #include <linux/serial.h>
48 #include <linux/major.h>
49 #include <linux/string.h>
50 #include <linux/fcntl.h>
51 #include <linux/ptrace.h>
52 #include <linux/ioport.h>
54 #include <linux/seq_file.h>
55 #include <linux/slab.h>
56 #include <linux/netdevice.h>
57 #include <linux/vmalloc.h>
58 #include <linux/init.h>
59 #include <linux/delay.h>
60 #include <linux/ioctl.h>
61 #include <linux/synclink.h>
63 #include <asm/system.h>
67 #include <linux/bitops.h>
68 #include <asm/types.h>
69 #include <linux/termios.h>
70 #include <linux/workqueue.h>
71 #include <linux/hdlc.h>
73 #include <pcmcia/cs.h>
74 #include <pcmcia/cistpl.h>
75 #include <pcmcia/cisreg.h>
76 #include <pcmcia/ds.h>
78 #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && \
79 defined(CONFIG_SYNCLINK_CS_MODULE))
80 #define SYNCLINK_GENERIC_HDLC 1
82 #define SYNCLINK_GENERIC_HDLC 0
85 #define GET_USER(error,value,addr) error = get_user(value,addr)
86 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
87 #define PUT_USER(error,value,addr) error = put_user(value,addr)
88 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
90 #include <asm/uaccess.h>
92 static MGSL_PARAMS default_params
= {
93 MGSL_MODE_HDLC
, /* unsigned long mode */
94 0, /* unsigned char loopback; */
95 HDLC_FLAG_UNDERRUN_ABORT15
, /* unsigned short flags; */
96 HDLC_ENCODING_NRZI_SPACE
, /* unsigned char encoding; */
97 0, /* unsigned long clock_speed; */
98 0xff, /* unsigned char addr_filter; */
99 HDLC_CRC_16_CCITT
, /* unsigned short crc_type; */
100 HDLC_PREAMBLE_LENGTH_8BITS
, /* unsigned char preamble_length; */
101 HDLC_PREAMBLE_PATTERN_NONE
, /* unsigned char preamble; */
102 9600, /* unsigned long data_rate; */
103 8, /* unsigned char data_bits; */
104 1, /* unsigned char stop_bits; */
105 ASYNC_PARITY_NONE
/* unsigned char parity; */
111 unsigned char status
;
115 /* The queue of BH actions to be performed */
118 #define BH_TRANSMIT 2
121 #define IO_PIN_SHUTDOWN_LIMIT 100
123 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
125 struct _input_signal_events
{
138 * Device instance data structure
141 typedef struct _mgslpc_info
{
142 struct tty_port port
;
143 void *if_ptr
; /* General purpose pointer (used by SPPP) */
147 struct mgsl_icount icount
;
150 int x_char
; /* xon/xoff character */
151 unsigned char read_status_mask
;
152 unsigned char ignore_status_mask
;
154 unsigned char *tx_buf
;
159 /* circular list of fixed length rx buffers */
161 unsigned char *rx_buf
; /* memory allocated for all rx buffers */
162 int rx_buf_total_size
; /* size of memory allocated for rx buffers */
163 int rx_put
; /* index of next empty rx buffer */
164 int rx_get
; /* index of next full rx buffer */
165 int rx_buf_size
; /* size in bytes of single rx buffer */
166 int rx_buf_count
; /* total number of rx buffers */
167 int rx_frame_count
; /* number of full rx buffers */
169 wait_queue_head_t status_event_wait_q
;
170 wait_queue_head_t event_wait_q
;
171 struct timer_list tx_timer
; /* HDLC transmit timeout timer */
172 struct _mgslpc_info
*next_device
; /* device list link */
174 unsigned short imra_value
;
175 unsigned short imrb_value
;
176 unsigned char pim_value
;
179 struct work_struct task
; /* task structure for scheduling bh */
188 int dcd_chkcount
; /* check counts to prevent */
189 int cts_chkcount
; /* too many IRQs if a signal */
190 int dsr_chkcount
; /* is floating */
201 int if_mode
; /* serial interface selection (RS-232, v.35 etc) */
203 char device_name
[25]; /* device instance name */
205 unsigned int io_base
; /* base I/O address of adapter */
206 unsigned int irq_level
;
208 MGSL_PARAMS params
; /* communications parameters */
210 unsigned char serial_signals
; /* current serial signal states */
212 bool irq_occurred
; /* for diagnostics use */
214 unsigned int init_error
; /* startup error (DIAGS) */
216 char flag_buf
[MAX_ASYNC_BUFFER_SIZE
];
217 bool drop_rts_on_tx_done
;
219 struct _input_signal_events input_signal_events
;
222 struct pcmcia_device
*p_dev
;
225 /* SPPP/Cisco HDLC device parts */
229 #if SYNCLINK_GENERIC_HDLC
230 struct net_device
*netdev
;
235 #define MGSLPC_MAGIC 0x5402
238 * The size of the serial xmit buffer is 1 page, or 4096 bytes
240 #define TXBUFSIZE 4096
243 #define CHA 0x00 /* channel A offset */
244 #define CHB 0x40 /* channel B offset */
291 #define IRQ_BREAK_ON BIT15 // rx break detected
292 #define IRQ_DATAOVERRUN BIT14 // receive data overflow
293 #define IRQ_ALLSENT BIT13 // all sent
294 #define IRQ_UNDERRUN BIT12 // transmit data underrun
295 #define IRQ_TIMER BIT11 // timer interrupt
296 #define IRQ_CTS BIT10 // CTS status change
297 #define IRQ_TXREPEAT BIT9 // tx message repeat
298 #define IRQ_TXFIFO BIT8 // transmit pool ready
299 #define IRQ_RXEOM BIT7 // receive message end
300 #define IRQ_EXITHUNT BIT6 // receive frame start
301 #define IRQ_RXTIME BIT6 // rx char timeout
302 #define IRQ_DCD BIT2 // carrier detect status change
303 #define IRQ_OVERRUN BIT1 // receive frame overflow
304 #define IRQ_RXFIFO BIT0 // receive pool full
308 #define XFW BIT6 // transmit FIFO write enable
309 #define CEC BIT2 // command executing
310 #define CTS BIT1 // CTS state
315 #define PVR_AUTOCTS BIT3
316 #define PVR_RS232 0x20 /* 0010b */
317 #define PVR_V35 0xe0 /* 1110b */
318 #define PVR_RS422 0x40 /* 0100b */
320 /* Register access functions */
322 #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
323 #define read_reg(info, reg) inb((info)->io_base + (reg))
325 #define read_reg16(info, reg) inw((info)->io_base + (reg))
326 #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
328 #define set_reg_bits(info, reg, mask) \
329 write_reg(info, (reg), \
330 (unsigned char) (read_reg(info, (reg)) | (mask)))
331 #define clear_reg_bits(info, reg, mask) \
332 write_reg(info, (reg), \
333 (unsigned char) (read_reg(info, (reg)) & ~(mask)))
335 * interrupt enable/disable routines
337 static void irq_disable(MGSLPC_INFO
*info
, unsigned char channel
, unsigned short mask
)
339 if (channel
== CHA
) {
340 info
->imra_value
|= mask
;
341 write_reg16(info
, CHA
+ IMR
, info
->imra_value
);
343 info
->imrb_value
|= mask
;
344 write_reg16(info
, CHB
+ IMR
, info
->imrb_value
);
347 static void irq_enable(MGSLPC_INFO
*info
, unsigned char channel
, unsigned short mask
)
349 if (channel
== CHA
) {
350 info
->imra_value
&= ~mask
;
351 write_reg16(info
, CHA
+ IMR
, info
->imra_value
);
353 info
->imrb_value
&= ~mask
;
354 write_reg16(info
, CHB
+ IMR
, info
->imrb_value
);
358 #define port_irq_disable(info, mask) \
359 { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
361 #define port_irq_enable(info, mask) \
362 { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
364 static void rx_start(MGSLPC_INFO
*info
);
365 static void rx_stop(MGSLPC_INFO
*info
);
367 static void tx_start(MGSLPC_INFO
*info
, struct tty_struct
*tty
);
368 static void tx_stop(MGSLPC_INFO
*info
);
369 static void tx_set_idle(MGSLPC_INFO
*info
);
371 static void get_signals(MGSLPC_INFO
*info
);
372 static void set_signals(MGSLPC_INFO
*info
);
374 static void reset_device(MGSLPC_INFO
*info
);
376 static void hdlc_mode(MGSLPC_INFO
*info
);
377 static void async_mode(MGSLPC_INFO
*info
);
379 static void tx_timeout(unsigned long context
);
381 static int carrier_raised(struct tty_port
*port
);
382 static void dtr_rts(struct tty_port
*port
, int onoff
);
384 #if SYNCLINK_GENERIC_HDLC
385 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
386 static void hdlcdev_tx_done(MGSLPC_INFO
*info
);
387 static void hdlcdev_rx(MGSLPC_INFO
*info
, char *buf
, int size
);
388 static int hdlcdev_init(MGSLPC_INFO
*info
);
389 static void hdlcdev_exit(MGSLPC_INFO
*info
);
392 static void trace_block(MGSLPC_INFO
*info
,const char* data
, int count
, int xmit
);
394 static bool register_test(MGSLPC_INFO
*info
);
395 static bool irq_test(MGSLPC_INFO
*info
);
396 static int adapter_test(MGSLPC_INFO
*info
);
398 static int claim_resources(MGSLPC_INFO
*info
);
399 static void release_resources(MGSLPC_INFO
*info
);
400 static void mgslpc_add_device(MGSLPC_INFO
*info
);
401 static void mgslpc_remove_device(MGSLPC_INFO
*info
);
403 static bool rx_get_frame(MGSLPC_INFO
*info
, struct tty_struct
*tty
);
404 static void rx_reset_buffers(MGSLPC_INFO
*info
);
405 static int rx_alloc_buffers(MGSLPC_INFO
*info
);
406 static void rx_free_buffers(MGSLPC_INFO
*info
);
408 static irqreturn_t
mgslpc_isr(int irq
, void *dev_id
);
411 * Bottom half interrupt handlers
413 static void bh_handler(struct work_struct
*work
);
414 static void bh_transmit(MGSLPC_INFO
*info
, struct tty_struct
*tty
);
415 static void bh_status(MGSLPC_INFO
*info
);
420 static int tiocmget(struct tty_struct
*tty
, struct file
*file
);
421 static int tiocmset(struct tty_struct
*tty
, struct file
*file
,
422 unsigned int set
, unsigned int clear
);
423 static int get_stats(MGSLPC_INFO
*info
, struct mgsl_icount __user
*user_icount
);
424 static int get_params(MGSLPC_INFO
*info
, MGSL_PARAMS __user
*user_params
);
425 static int set_params(MGSLPC_INFO
*info
, MGSL_PARAMS __user
*new_params
, struct tty_struct
*tty
);
426 static int get_txidle(MGSLPC_INFO
*info
, int __user
*idle_mode
);
427 static int set_txidle(MGSLPC_INFO
*info
, int idle_mode
);
428 static int set_txenable(MGSLPC_INFO
*info
, int enable
, struct tty_struct
*tty
);
429 static int tx_abort(MGSLPC_INFO
*info
);
430 static int set_rxenable(MGSLPC_INFO
*info
, int enable
);
431 static int wait_events(MGSLPC_INFO
*info
, int __user
*mask
);
433 static MGSLPC_INFO
*mgslpc_device_list
= NULL
;
434 static int mgslpc_device_count
= 0;
437 * Set this param to non-zero to load eax with the
438 * .text section address and breakpoint on module load.
439 * This is useful for use with gdb and add-symbol-file command.
441 static int break_on_load
=0;
444 * Driver major number, defaults to zero to get auto
445 * assigned major number. May be forced as module parameter.
447 static int ttymajor
=0;
449 static int debug_level
= 0;
450 static int maxframe
[MAX_DEVICE_COUNT
] = {0,};
452 module_param(break_on_load
, bool, 0);
453 module_param(ttymajor
, int, 0);
454 module_param(debug_level
, int, 0);
455 module_param_array(maxframe
, int, NULL
, 0);
457 MODULE_LICENSE("GPL");
459 static char *driver_name
= "SyncLink PC Card driver";
460 static char *driver_version
= "$Revision: 4.34 $";
462 static struct tty_driver
*serial_driver
;
464 /* number of characters left in xmit buffer before we ask for more */
465 #define WAKEUP_CHARS 256
467 static void mgslpc_change_params(MGSLPC_INFO
*info
, struct tty_struct
*tty
);
468 static void mgslpc_wait_until_sent(struct tty_struct
*tty
, int timeout
);
470 /* PCMCIA prototypes */
472 static int mgslpc_config(struct pcmcia_device
*link
);
473 static void mgslpc_release(u_long arg
);
474 static void mgslpc_detach(struct pcmcia_device
*p_dev
);
477 * 1st function defined in .text section. Calling this function in
478 * init_module() followed by a breakpoint allows a remote debugger
479 * (gdb) to get the .text address for the add-symbol-file command.
480 * This allows remote debugging of dynamically loadable modules.
482 static void* mgslpc_get_text_ptr(void)
484 return mgslpc_get_text_ptr
;
488 * line discipline callback wrappers
490 * The wrappers maintain line discipline references
491 * while calling into the line discipline.
493 * ldisc_receive_buf - pass receive data to line discipline
496 static void ldisc_receive_buf(struct tty_struct
*tty
,
497 const __u8
*data
, char *flags
, int count
)
499 struct tty_ldisc
*ld
;
502 ld
= tty_ldisc_ref(tty
);
504 if (ld
->ops
->receive_buf
)
505 ld
->ops
->receive_buf(tty
, data
, flags
, count
);
510 static const struct tty_port_operations mgslpc_port_ops
= {
511 .carrier_raised
= carrier_raised
,
515 static int mgslpc_probe(struct pcmcia_device
*link
)
520 if (debug_level
>= DEBUG_LEVEL_INFO
)
521 printk("mgslpc_attach\n");
523 info
= kzalloc(sizeof(MGSLPC_INFO
), GFP_KERNEL
);
525 printk("Error can't allocate device instance data\n");
529 info
->magic
= MGSLPC_MAGIC
;
530 tty_port_init(&info
->port
);
531 info
->port
.ops
= &mgslpc_port_ops
;
532 INIT_WORK(&info
->task
, bh_handler
);
533 info
->max_frame_size
= 4096;
534 info
->port
.close_delay
= 5*HZ
/10;
535 info
->port
.closing_wait
= 30*HZ
;
536 init_waitqueue_head(&info
->status_event_wait_q
);
537 init_waitqueue_head(&info
->event_wait_q
);
538 spin_lock_init(&info
->lock
);
539 spin_lock_init(&info
->netlock
);
540 memcpy(&info
->params
,&default_params
,sizeof(MGSL_PARAMS
));
541 info
->idle_mode
= HDLC_TXIDLE_FLAGS
;
542 info
->imra_value
= 0xffff;
543 info
->imrb_value
= 0xffff;
544 info
->pim_value
= 0xff;
549 /* Initialize the struct pcmcia_device structure */
551 link
->conf
.Attributes
= 0;
552 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
554 ret
= mgslpc_config(link
);
558 mgslpc_add_device(info
);
563 /* Card has been inserted.
566 static int mgslpc_ioprobe(struct pcmcia_device
*p_dev
,
567 cistpl_cftable_entry_t
*cfg
,
568 cistpl_cftable_entry_t
*dflt
,
575 p_dev
->resource
[0]->start
= cfg
->io
.win
[0].base
;
576 p_dev
->resource
[0]->end
= cfg
->io
.win
[0].len
;
577 p_dev
->resource
[0]->flags
|= pcmcia_io_cfg_data_width(cfg
->io
.flags
);
578 p_dev
->io_lines
= cfg
->io
.flags
& CISTPL_IO_LINES_MASK
;
580 return pcmcia_request_io(p_dev
);
583 static int mgslpc_config(struct pcmcia_device
*link
)
585 MGSLPC_INFO
*info
= link
->priv
;
588 if (debug_level
>= DEBUG_LEVEL_INFO
)
589 printk("mgslpc_config(0x%p)\n", link
);
591 ret
= pcmcia_loop_config(link
, mgslpc_ioprobe
, NULL
);
595 link
->conf
.Attributes
= CONF_ENABLE_IRQ
;
596 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
597 link
->conf
.ConfigIndex
= 8;
598 link
->conf
.Present
= PRESENT_OPTION
;
600 ret
= pcmcia_request_irq(link
, mgslpc_isr
);
603 ret
= pcmcia_request_configuration(link
, &link
->conf
);
607 info
->io_base
= link
->resource
[0]->start
;
608 info
->irq_level
= link
->irq
;
610 dev_info(&link
->dev
, "index 0x%02x:",
611 link
->conf
.ConfigIndex
);
612 if (link
->conf
.Attributes
& CONF_ENABLE_IRQ
)
613 printk(", irq %d", link
->irq
);
614 if (link
->resource
[0])
615 printk(", io %pR", link
->resource
[0]);
620 mgslpc_release((u_long
)link
);
624 /* Card has been removed.
625 * Unregister device and release PCMCIA configuration.
626 * If device is open, postpone until it is closed.
628 static void mgslpc_release(u_long arg
)
630 struct pcmcia_device
*link
= (struct pcmcia_device
*)arg
;
632 if (debug_level
>= DEBUG_LEVEL_INFO
)
633 printk("mgslpc_release(0x%p)\n", link
);
635 pcmcia_disable_device(link
);
638 static void mgslpc_detach(struct pcmcia_device
*link
)
640 if (debug_level
>= DEBUG_LEVEL_INFO
)
641 printk("mgslpc_detach(0x%p)\n", link
);
643 ((MGSLPC_INFO
*)link
->priv
)->stop
= 1;
644 mgslpc_release((u_long
)link
);
646 mgslpc_remove_device((MGSLPC_INFO
*)link
->priv
);
649 static int mgslpc_suspend(struct pcmcia_device
*link
)
651 MGSLPC_INFO
*info
= link
->priv
;
658 static int mgslpc_resume(struct pcmcia_device
*link
)
660 MGSLPC_INFO
*info
= link
->priv
;
668 static inline bool mgslpc_paranoia_check(MGSLPC_INFO
*info
,
669 char *name
, const char *routine
)
671 #ifdef MGSLPC_PARANOIA_CHECK
672 static const char *badmagic
=
673 "Warning: bad magic number for mgsl struct (%s) in %s\n";
674 static const char *badinfo
=
675 "Warning: null mgslpc_info for (%s) in %s\n";
678 printk(badinfo
, name
, routine
);
681 if (info
->magic
!= MGSLPC_MAGIC
) {
682 printk(badmagic
, name
, routine
);
693 #define CMD_RXFIFO BIT7 // release current rx FIFO
694 #define CMD_RXRESET BIT6 // receiver reset
695 #define CMD_RXFIFO_READ BIT5
696 #define CMD_START_TIMER BIT4
697 #define CMD_TXFIFO BIT3 // release current tx FIFO
698 #define CMD_TXEOM BIT1 // transmit end message
699 #define CMD_TXRESET BIT0 // transmit reset
701 static bool wait_command_complete(MGSLPC_INFO
*info
, unsigned char channel
)
704 /* wait for command completion */
705 while (read_reg(info
, (unsigned char)(channel
+STAR
)) & BIT2
) {
713 static void issue_command(MGSLPC_INFO
*info
, unsigned char channel
, unsigned char cmd
)
715 wait_command_complete(info
, channel
);
716 write_reg(info
, (unsigned char) (channel
+ CMDR
), cmd
);
719 static void tx_pause(struct tty_struct
*tty
)
721 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
724 if (mgslpc_paranoia_check(info
, tty
->name
, "tx_pause"))
726 if (debug_level
>= DEBUG_LEVEL_INFO
)
727 printk("tx_pause(%s)\n",info
->device_name
);
729 spin_lock_irqsave(&info
->lock
,flags
);
730 if (info
->tx_enabled
)
732 spin_unlock_irqrestore(&info
->lock
,flags
);
735 static void tx_release(struct tty_struct
*tty
)
737 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
740 if (mgslpc_paranoia_check(info
, tty
->name
, "tx_release"))
742 if (debug_level
>= DEBUG_LEVEL_INFO
)
743 printk("tx_release(%s)\n",info
->device_name
);
745 spin_lock_irqsave(&info
->lock
,flags
);
746 if (!info
->tx_enabled
)
748 spin_unlock_irqrestore(&info
->lock
,flags
);
751 /* Return next bottom half action to perform.
752 * or 0 if nothing to do.
754 static int bh_action(MGSLPC_INFO
*info
)
759 spin_lock_irqsave(&info
->lock
,flags
);
761 if (info
->pending_bh
& BH_RECEIVE
) {
762 info
->pending_bh
&= ~BH_RECEIVE
;
764 } else if (info
->pending_bh
& BH_TRANSMIT
) {
765 info
->pending_bh
&= ~BH_TRANSMIT
;
767 } else if (info
->pending_bh
& BH_STATUS
) {
768 info
->pending_bh
&= ~BH_STATUS
;
773 /* Mark BH routine as complete */
774 info
->bh_running
= false;
775 info
->bh_requested
= false;
778 spin_unlock_irqrestore(&info
->lock
,flags
);
783 static void bh_handler(struct work_struct
*work
)
785 MGSLPC_INFO
*info
= container_of(work
, MGSLPC_INFO
, task
);
786 struct tty_struct
*tty
;
792 if (debug_level
>= DEBUG_LEVEL_BH
)
793 printk( "%s(%d):bh_handler(%s) entry\n",
794 __FILE__
,__LINE__
,info
->device_name
);
796 info
->bh_running
= true;
797 tty
= tty_port_tty_get(&info
->port
);
799 while((action
= bh_action(info
)) != 0) {
801 /* Process work item */
802 if ( debug_level
>= DEBUG_LEVEL_BH
)
803 printk( "%s(%d):bh_handler() work item action=%d\n",
804 __FILE__
,__LINE__
,action
);
809 while(rx_get_frame(info
, tty
));
812 bh_transmit(info
, tty
);
818 /* unknown work item ID */
819 printk("Unknown work item ID=%08X!\n", action
);
825 if (debug_level
>= DEBUG_LEVEL_BH
)
826 printk( "%s(%d):bh_handler(%s) exit\n",
827 __FILE__
,__LINE__
,info
->device_name
);
830 static void bh_transmit(MGSLPC_INFO
*info
, struct tty_struct
*tty
)
832 if (debug_level
>= DEBUG_LEVEL_BH
)
833 printk("bh_transmit() entry on %s\n", info
->device_name
);
839 static void bh_status(MGSLPC_INFO
*info
)
841 info
->ri_chkcount
= 0;
842 info
->dsr_chkcount
= 0;
843 info
->dcd_chkcount
= 0;
844 info
->cts_chkcount
= 0;
847 /* eom: non-zero = end of frame */
848 static void rx_ready_hdlc(MGSLPC_INFO
*info
, int eom
)
850 unsigned char data
[2];
851 unsigned char fifo_count
, read_count
, i
;
852 RXBUF
*buf
= (RXBUF
*)(info
->rx_buf
+ (info
->rx_put
* info
->rx_buf_size
));
854 if (debug_level
>= DEBUG_LEVEL_ISR
)
855 printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__
,__LINE__
,eom
);
857 if (!info
->rx_enabled
)
860 if (info
->rx_frame_count
>= info
->rx_buf_count
) {
861 /* no more free buffers */
862 issue_command(info
, CHA
, CMD_RXRESET
);
863 info
->pending_bh
|= BH_RECEIVE
;
864 info
->rx_overflow
= true;
865 info
->icount
.buf_overrun
++;
870 /* end of frame, get FIFO count from RBCL register */
871 if (!(fifo_count
= (unsigned char)(read_reg(info
, CHA
+RBCL
) & 0x1f)))
877 if (fifo_count
== 1) {
879 data
[0] = read_reg(info
, CHA
+ RXFIFO
);
882 *((unsigned short *) data
) = read_reg16(info
, CHA
+ RXFIFO
);
884 fifo_count
-= read_count
;
885 if (!fifo_count
&& eom
)
886 buf
->status
= data
[--read_count
];
888 for (i
= 0; i
< read_count
; i
++) {
889 if (buf
->count
>= info
->max_frame_size
) {
890 /* frame too large, reset receiver and reset current buffer */
891 issue_command(info
, CHA
, CMD_RXRESET
);
895 *(buf
->data
+ buf
->count
) = data
[i
];
898 } while (fifo_count
);
901 info
->pending_bh
|= BH_RECEIVE
;
902 info
->rx_frame_count
++;
904 if (info
->rx_put
>= info
->rx_buf_count
)
907 issue_command(info
, CHA
, CMD_RXFIFO
);
910 static void rx_ready_async(MGSLPC_INFO
*info
, int tcd
, struct tty_struct
*tty
)
912 unsigned char data
, status
, flag
;
915 struct mgsl_icount
*icount
= &info
->icount
;
918 /* early termination, get FIFO count from RBCL register */
919 fifo_count
= (unsigned char)(read_reg(info
, CHA
+RBCL
) & 0x1f);
921 /* Zero fifo count could mean 0 or 32 bytes available.
922 * If BIT5 of STAR is set then at least 1 byte is available.
924 if (!fifo_count
&& (read_reg(info
,CHA
+STAR
) & BIT5
))
929 tty_buffer_request_room(tty
, fifo_count
);
930 /* Flush received async data to receive data buffer. */
932 data
= read_reg(info
, CHA
+ RXFIFO
);
933 status
= read_reg(info
, CHA
+ RXFIFO
);
939 // if no frameing/crc error then save data
941 // BIT6:framing error
943 if (status
& (BIT7
+ BIT6
)) {
949 /* discard char if tty control flags say so */
950 if (status
& info
->ignore_status_mask
)
953 status
&= info
->read_status_mask
;
957 else if (status
& BIT6
)
960 work
+= tty_insert_flip_char(tty
, data
, flag
);
962 issue_command(info
, CHA
, CMD_RXFIFO
);
964 if (debug_level
>= DEBUG_LEVEL_ISR
) {
965 printk("%s(%d):rx_ready_async",
967 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
968 __FILE__
,__LINE__
,icount
->rx
,icount
->brk
,
969 icount
->parity
,icount
->frame
,icount
->overrun
);
973 tty_flip_buffer_push(tty
);
977 static void tx_done(MGSLPC_INFO
*info
, struct tty_struct
*tty
)
979 if (!info
->tx_active
)
982 info
->tx_active
= false;
983 info
->tx_aborting
= false;
985 if (info
->params
.mode
== MGSL_MODE_ASYNC
)
988 info
->tx_count
= info
->tx_put
= info
->tx_get
= 0;
989 del_timer(&info
->tx_timer
);
991 if (info
->drop_rts_on_tx_done
) {
993 if (info
->serial_signals
& SerialSignal_RTS
) {
994 info
->serial_signals
&= ~SerialSignal_RTS
;
997 info
->drop_rts_on_tx_done
= false;
1000 #if SYNCLINK_GENERIC_HDLC
1002 hdlcdev_tx_done(info
);
1006 if (tty
->stopped
|| tty
->hw_stopped
) {
1010 info
->pending_bh
|= BH_TRANSMIT
;
1014 static void tx_ready(MGSLPC_INFO
*info
, struct tty_struct
*tty
)
1016 unsigned char fifo_count
= 32;
1019 if (debug_level
>= DEBUG_LEVEL_ISR
)
1020 printk("%s(%d):tx_ready(%s)\n", __FILE__
,__LINE__
,info
->device_name
);
1022 if (info
->params
.mode
== MGSL_MODE_HDLC
) {
1023 if (!info
->tx_active
)
1026 if (tty
->stopped
|| tty
->hw_stopped
) {
1030 if (!info
->tx_count
)
1031 info
->tx_active
= false;
1034 if (!info
->tx_count
)
1037 while (info
->tx_count
&& fifo_count
) {
1038 c
= min(2, min_t(int, fifo_count
, min(info
->tx_count
, TXBUFSIZE
- info
->tx_get
)));
1041 write_reg(info
, CHA
+ TXFIFO
, *(info
->tx_buf
+ info
->tx_get
));
1043 write_reg16(info
, CHA
+ TXFIFO
,
1044 *((unsigned short*)(info
->tx_buf
+ info
->tx_get
)));
1046 info
->tx_count
-= c
;
1047 info
->tx_get
= (info
->tx_get
+ c
) & (TXBUFSIZE
- 1);
1051 if (info
->params
.mode
== MGSL_MODE_ASYNC
) {
1052 if (info
->tx_count
< WAKEUP_CHARS
)
1053 info
->pending_bh
|= BH_TRANSMIT
;
1054 issue_command(info
, CHA
, CMD_TXFIFO
);
1057 issue_command(info
, CHA
, CMD_TXFIFO
);
1059 issue_command(info
, CHA
, CMD_TXFIFO
+ CMD_TXEOM
);
1063 static void cts_change(MGSLPC_INFO
*info
, struct tty_struct
*tty
)
1066 if ((info
->cts_chkcount
)++ >= IO_PIN_SHUTDOWN_LIMIT
)
1067 irq_disable(info
, CHB
, IRQ_CTS
);
1069 if (info
->serial_signals
& SerialSignal_CTS
)
1070 info
->input_signal_events
.cts_up
++;
1072 info
->input_signal_events
.cts_down
++;
1073 wake_up_interruptible(&info
->status_event_wait_q
);
1074 wake_up_interruptible(&info
->event_wait_q
);
1076 if (info
->port
.flags
& ASYNC_CTS_FLOW
) {
1077 if (tty
->hw_stopped
) {
1078 if (info
->serial_signals
& SerialSignal_CTS
) {
1079 if (debug_level
>= DEBUG_LEVEL_ISR
)
1080 printk("CTS tx start...");
1082 tty
->hw_stopped
= 0;
1083 tx_start(info
, tty
);
1084 info
->pending_bh
|= BH_TRANSMIT
;
1088 if (!(info
->serial_signals
& SerialSignal_CTS
)) {
1089 if (debug_level
>= DEBUG_LEVEL_ISR
)
1090 printk("CTS tx stop...");
1092 tty
->hw_stopped
= 1;
1097 info
->pending_bh
|= BH_STATUS
;
1100 static void dcd_change(MGSLPC_INFO
*info
, struct tty_struct
*tty
)
1103 if ((info
->dcd_chkcount
)++ >= IO_PIN_SHUTDOWN_LIMIT
)
1104 irq_disable(info
, CHB
, IRQ_DCD
);
1106 if (info
->serial_signals
& SerialSignal_DCD
) {
1107 info
->input_signal_events
.dcd_up
++;
1110 info
->input_signal_events
.dcd_down
++;
1111 #if SYNCLINK_GENERIC_HDLC
1112 if (info
->netcount
) {
1113 if (info
->serial_signals
& SerialSignal_DCD
)
1114 netif_carrier_on(info
->netdev
);
1116 netif_carrier_off(info
->netdev
);
1119 wake_up_interruptible(&info
->status_event_wait_q
);
1120 wake_up_interruptible(&info
->event_wait_q
);
1122 if (info
->port
.flags
& ASYNC_CHECK_CD
) {
1123 if (debug_level
>= DEBUG_LEVEL_ISR
)
1124 printk("%s CD now %s...", info
->device_name
,
1125 (info
->serial_signals
& SerialSignal_DCD
) ? "on" : "off");
1126 if (info
->serial_signals
& SerialSignal_DCD
)
1127 wake_up_interruptible(&info
->port
.open_wait
);
1129 if (debug_level
>= DEBUG_LEVEL_ISR
)
1130 printk("doing serial hangup...");
1135 info
->pending_bh
|= BH_STATUS
;
1138 static void dsr_change(MGSLPC_INFO
*info
)
1141 if ((info
->dsr_chkcount
)++ >= IO_PIN_SHUTDOWN_LIMIT
)
1142 port_irq_disable(info
, PVR_DSR
);
1144 if (info
->serial_signals
& SerialSignal_DSR
)
1145 info
->input_signal_events
.dsr_up
++;
1147 info
->input_signal_events
.dsr_down
++;
1148 wake_up_interruptible(&info
->status_event_wait_q
);
1149 wake_up_interruptible(&info
->event_wait_q
);
1150 info
->pending_bh
|= BH_STATUS
;
1153 static void ri_change(MGSLPC_INFO
*info
)
1156 if ((info
->ri_chkcount
)++ >= IO_PIN_SHUTDOWN_LIMIT
)
1157 port_irq_disable(info
, PVR_RI
);
1159 if (info
->serial_signals
& SerialSignal_RI
)
1160 info
->input_signal_events
.ri_up
++;
1162 info
->input_signal_events
.ri_down
++;
1163 wake_up_interruptible(&info
->status_event_wait_q
);
1164 wake_up_interruptible(&info
->event_wait_q
);
1165 info
->pending_bh
|= BH_STATUS
;
1168 /* Interrupt service routine entry point.
1172 * irq interrupt number that caused interrupt
1173 * dev_id device ID supplied during interrupt registration
1175 static irqreturn_t
mgslpc_isr(int dummy
, void *dev_id
)
1177 MGSLPC_INFO
*info
= dev_id
;
1178 struct tty_struct
*tty
;
1180 unsigned char gis
, pis
;
1183 if (debug_level
>= DEBUG_LEVEL_ISR
)
1184 printk("mgslpc_isr(%d) entry.\n", info
->irq_level
);
1186 if (!(info
->p_dev
->_locked
))
1189 tty
= tty_port_tty_get(&info
->port
);
1191 spin_lock(&info
->lock
);
1193 while ((gis
= read_reg(info
, CHA
+ GIS
))) {
1194 if (debug_level
>= DEBUG_LEVEL_ISR
)
1195 printk("mgslpc_isr %s gis=%04X\n", info
->device_name
,gis
);
1197 if ((gis
& 0x70) || count
> 1000) {
1198 printk("synclink_cs:hardware failed or ejected\n");
1203 if (gis
& (BIT1
+ BIT0
)) {
1204 isr
= read_reg16(info
, CHB
+ ISR
);
1206 dcd_change(info
, tty
);
1208 cts_change(info
, tty
);
1210 if (gis
& (BIT3
+ BIT2
))
1212 isr
= read_reg16(info
, CHA
+ ISR
);
1213 if (isr
& IRQ_TIMER
) {
1214 info
->irq_occurred
= true;
1215 irq_disable(info
, CHA
, IRQ_TIMER
);
1219 if (isr
& IRQ_EXITHUNT
) {
1220 info
->icount
.exithunt
++;
1221 wake_up_interruptible(&info
->event_wait_q
);
1223 if (isr
& IRQ_BREAK_ON
) {
1225 if (info
->port
.flags
& ASYNC_SAK
)
1228 if (isr
& IRQ_RXTIME
) {
1229 issue_command(info
, CHA
, CMD_RXFIFO_READ
);
1231 if (isr
& (IRQ_RXEOM
+ IRQ_RXFIFO
)) {
1232 if (info
->params
.mode
== MGSL_MODE_HDLC
)
1233 rx_ready_hdlc(info
, isr
& IRQ_RXEOM
);
1235 rx_ready_async(info
, isr
& IRQ_RXEOM
, tty
);
1239 if (isr
& IRQ_UNDERRUN
) {
1240 if (info
->tx_aborting
)
1241 info
->icount
.txabort
++;
1243 info
->icount
.txunder
++;
1246 else if (isr
& IRQ_ALLSENT
) {
1247 info
->icount
.txok
++;
1250 else if (isr
& IRQ_TXFIFO
)
1251 tx_ready(info
, tty
);
1254 pis
= read_reg(info
, CHA
+ PIS
);
1262 /* Request bottom half processing if there's something
1263 * for it to do and the bh is not already running
1266 if (info
->pending_bh
&& !info
->bh_running
&& !info
->bh_requested
) {
1267 if ( debug_level
>= DEBUG_LEVEL_ISR
)
1268 printk("%s(%d):%s queueing bh task.\n",
1269 __FILE__
,__LINE__
,info
->device_name
);
1270 schedule_work(&info
->task
);
1271 info
->bh_requested
= true;
1274 spin_unlock(&info
->lock
);
1277 if (debug_level
>= DEBUG_LEVEL_ISR
)
1278 printk("%s(%d):mgslpc_isr(%d)exit.\n",
1279 __FILE__
, __LINE__
, info
->irq_level
);
1284 /* Initialize and start device.
1286 static int startup(MGSLPC_INFO
* info
, struct tty_struct
*tty
)
1290 if (debug_level
>= DEBUG_LEVEL_INFO
)
1291 printk("%s(%d):startup(%s)\n",__FILE__
,__LINE__
,info
->device_name
);
1293 if (info
->port
.flags
& ASYNC_INITIALIZED
)
1296 if (!info
->tx_buf
) {
1297 /* allocate a page of memory for a transmit buffer */
1298 info
->tx_buf
= (unsigned char *)get_zeroed_page(GFP_KERNEL
);
1299 if (!info
->tx_buf
) {
1300 printk(KERN_ERR
"%s(%d):%s can't allocate transmit buffer\n",
1301 __FILE__
,__LINE__
,info
->device_name
);
1306 info
->pending_bh
= 0;
1308 memset(&info
->icount
, 0, sizeof(info
->icount
));
1310 setup_timer(&info
->tx_timer
, tx_timeout
, (unsigned long)info
);
1312 /* Allocate and claim adapter resources */
1313 retval
= claim_resources(info
);
1315 /* perform existance check and diagnostics */
1317 retval
= adapter_test(info
);
1320 if (capable(CAP_SYS_ADMIN
) && tty
)
1321 set_bit(TTY_IO_ERROR
, &tty
->flags
);
1322 release_resources(info
);
1326 /* program hardware for current parameters */
1327 mgslpc_change_params(info
, tty
);
1330 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
1332 info
->port
.flags
|= ASYNC_INITIALIZED
;
1337 /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
1339 static void shutdown(MGSLPC_INFO
* info
, struct tty_struct
*tty
)
1341 unsigned long flags
;
1343 if (!(info
->port
.flags
& ASYNC_INITIALIZED
))
1346 if (debug_level
>= DEBUG_LEVEL_INFO
)
1347 printk("%s(%d):mgslpc_shutdown(%s)\n",
1348 __FILE__
,__LINE__
, info
->device_name
);
1350 /* clear status wait queue because status changes */
1351 /* can't happen after shutting down the hardware */
1352 wake_up_interruptible(&info
->status_event_wait_q
);
1353 wake_up_interruptible(&info
->event_wait_q
);
1355 del_timer_sync(&info
->tx_timer
);
1358 free_page((unsigned long) info
->tx_buf
);
1359 info
->tx_buf
= NULL
;
1362 spin_lock_irqsave(&info
->lock
,flags
);
1367 /* TODO:disable interrupts instead of reset to preserve signal states */
1370 if (!tty
|| tty
->termios
->c_cflag
& HUPCL
) {
1371 info
->serial_signals
&= ~(SerialSignal_DTR
+ SerialSignal_RTS
);
1375 spin_unlock_irqrestore(&info
->lock
,flags
);
1377 release_resources(info
);
1380 set_bit(TTY_IO_ERROR
, &tty
->flags
);
1382 info
->port
.flags
&= ~ASYNC_INITIALIZED
;
1385 static void mgslpc_program_hw(MGSLPC_INFO
*info
, struct tty_struct
*tty
)
1387 unsigned long flags
;
1389 spin_lock_irqsave(&info
->lock
,flags
);
1393 info
->tx_count
= info
->tx_put
= info
->tx_get
= 0;
1395 if (info
->params
.mode
== MGSL_MODE_HDLC
|| info
->netcount
)
1402 info
->dcd_chkcount
= 0;
1403 info
->cts_chkcount
= 0;
1404 info
->ri_chkcount
= 0;
1405 info
->dsr_chkcount
= 0;
1407 irq_enable(info
, CHB
, IRQ_DCD
| IRQ_CTS
);
1408 port_irq_enable(info
, (unsigned char) PVR_DSR
| PVR_RI
);
1411 if (info
->netcount
|| (tty
&& (tty
->termios
->c_cflag
& CREAD
)))
1414 spin_unlock_irqrestore(&info
->lock
,flags
);
1417 /* Reconfigure adapter based on new parameters
1419 static void mgslpc_change_params(MGSLPC_INFO
*info
, struct tty_struct
*tty
)
1424 if (!tty
|| !tty
->termios
)
1427 if (debug_level
>= DEBUG_LEVEL_INFO
)
1428 printk("%s(%d):mgslpc_change_params(%s)\n",
1429 __FILE__
,__LINE__
, info
->device_name
);
1431 cflag
= tty
->termios
->c_cflag
;
1433 /* if B0 rate (hangup) specified then negate DTR and RTS */
1434 /* otherwise assert DTR and RTS */
1436 info
->serial_signals
|= SerialSignal_RTS
+ SerialSignal_DTR
;
1438 info
->serial_signals
&= ~(SerialSignal_RTS
+ SerialSignal_DTR
);
1440 /* byte size and parity */
1442 switch (cflag
& CSIZE
) {
1443 case CS5
: info
->params
.data_bits
= 5; break;
1444 case CS6
: info
->params
.data_bits
= 6; break;
1445 case CS7
: info
->params
.data_bits
= 7; break;
1446 case CS8
: info
->params
.data_bits
= 8; break;
1447 default: info
->params
.data_bits
= 7; break;
1451 info
->params
.stop_bits
= 2;
1453 info
->params
.stop_bits
= 1;
1455 info
->params
.parity
= ASYNC_PARITY_NONE
;
1456 if (cflag
& PARENB
) {
1458 info
->params
.parity
= ASYNC_PARITY_ODD
;
1460 info
->params
.parity
= ASYNC_PARITY_EVEN
;
1463 info
->params
.parity
= ASYNC_PARITY_SPACE
;
1467 /* calculate number of jiffies to transmit a full
1468 * FIFO (32 bytes) at specified data rate
1470 bits_per_char
= info
->params
.data_bits
+
1471 info
->params
.stop_bits
+ 1;
1473 /* if port data rate is set to 460800 or less then
1474 * allow tty settings to override, otherwise keep the
1475 * current data rate.
1477 if (info
->params
.data_rate
<= 460800) {
1478 info
->params
.data_rate
= tty_get_baud_rate(tty
);
1481 if ( info
->params
.data_rate
) {
1482 info
->timeout
= (32*HZ
*bits_per_char
) /
1483 info
->params
.data_rate
;
1485 info
->timeout
+= HZ
/50; /* Add .02 seconds of slop */
1487 if (cflag
& CRTSCTS
)
1488 info
->port
.flags
|= ASYNC_CTS_FLOW
;
1490 info
->port
.flags
&= ~ASYNC_CTS_FLOW
;
1493 info
->port
.flags
&= ~ASYNC_CHECK_CD
;
1495 info
->port
.flags
|= ASYNC_CHECK_CD
;
1497 /* process tty input control flags */
1499 info
->read_status_mask
= 0;
1501 info
->read_status_mask
|= BIT7
| BIT6
;
1503 info
->ignore_status_mask
|= BIT7
| BIT6
;
1505 mgslpc_program_hw(info
, tty
);
1508 /* Add a character to the transmit buffer
1510 static int mgslpc_put_char(struct tty_struct
*tty
, unsigned char ch
)
1512 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
1513 unsigned long flags
;
1515 if (debug_level
>= DEBUG_LEVEL_INFO
) {
1516 printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
1517 __FILE__
,__LINE__
,ch
,info
->device_name
);
1520 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_put_char"))
1526 spin_lock_irqsave(&info
->lock
,flags
);
1528 if (info
->params
.mode
== MGSL_MODE_ASYNC
|| !info
->tx_active
) {
1529 if (info
->tx_count
< TXBUFSIZE
- 1) {
1530 info
->tx_buf
[info
->tx_put
++] = ch
;
1531 info
->tx_put
&= TXBUFSIZE
-1;
1536 spin_unlock_irqrestore(&info
->lock
,flags
);
1540 /* Enable transmitter so remaining characters in the
1541 * transmit buffer are sent.
1543 static void mgslpc_flush_chars(struct tty_struct
*tty
)
1545 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
1546 unsigned long flags
;
1548 if (debug_level
>= DEBUG_LEVEL_INFO
)
1549 printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
1550 __FILE__
,__LINE__
,info
->device_name
,info
->tx_count
);
1552 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_flush_chars"))
1555 if (info
->tx_count
<= 0 || tty
->stopped
||
1556 tty
->hw_stopped
|| !info
->tx_buf
)
1559 if (debug_level
>= DEBUG_LEVEL_INFO
)
1560 printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
1561 __FILE__
,__LINE__
,info
->device_name
);
1563 spin_lock_irqsave(&info
->lock
,flags
);
1564 if (!info
->tx_active
)
1565 tx_start(info
, tty
);
1566 spin_unlock_irqrestore(&info
->lock
,flags
);
1569 /* Send a block of data
1573 * tty pointer to tty information structure
1574 * buf pointer to buffer containing send data
1575 * count size of send data in bytes
1577 * Returns: number of characters written
1579 static int mgslpc_write(struct tty_struct
* tty
,
1580 const unsigned char *buf
, int count
)
1583 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
1584 unsigned long flags
;
1586 if (debug_level
>= DEBUG_LEVEL_INFO
)
1587 printk( "%s(%d):mgslpc_write(%s) count=%d\n",
1588 __FILE__
,__LINE__
,info
->device_name
,count
);
1590 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_write") ||
1594 if (info
->params
.mode
== MGSL_MODE_HDLC
) {
1595 if (count
> TXBUFSIZE
) {
1599 if (info
->tx_active
)
1601 else if (info
->tx_count
)
1607 min(TXBUFSIZE
- info
->tx_count
- 1,
1608 TXBUFSIZE
- info
->tx_put
));
1612 memcpy(info
->tx_buf
+ info
->tx_put
, buf
, c
);
1614 spin_lock_irqsave(&info
->lock
,flags
);
1615 info
->tx_put
= (info
->tx_put
+ c
) & (TXBUFSIZE
-1);
1616 info
->tx_count
+= c
;
1617 spin_unlock_irqrestore(&info
->lock
,flags
);
1624 if (info
->tx_count
&& !tty
->stopped
&& !tty
->hw_stopped
) {
1625 spin_lock_irqsave(&info
->lock
,flags
);
1626 if (!info
->tx_active
)
1627 tx_start(info
, tty
);
1628 spin_unlock_irqrestore(&info
->lock
,flags
);
1631 if (debug_level
>= DEBUG_LEVEL_INFO
)
1632 printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
1633 __FILE__
,__LINE__
,info
->device_name
,ret
);
1637 /* Return the count of free bytes in transmit buffer
1639 static int mgslpc_write_room(struct tty_struct
*tty
)
1641 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
1644 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_write_room"))
1647 if (info
->params
.mode
== MGSL_MODE_HDLC
) {
1648 /* HDLC (frame oriented) mode */
1649 if (info
->tx_active
)
1652 return HDLC_MAX_FRAME_SIZE
;
1654 ret
= TXBUFSIZE
- info
->tx_count
- 1;
1659 if (debug_level
>= DEBUG_LEVEL_INFO
)
1660 printk("%s(%d):mgslpc_write_room(%s)=%d\n",
1661 __FILE__
,__LINE__
, info
->device_name
, ret
);
1665 /* Return the count of bytes in transmit buffer
1667 static int mgslpc_chars_in_buffer(struct tty_struct
*tty
)
1669 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
1672 if (debug_level
>= DEBUG_LEVEL_INFO
)
1673 printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
1674 __FILE__
,__LINE__
, info
->device_name
);
1676 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_chars_in_buffer"))
1679 if (info
->params
.mode
== MGSL_MODE_HDLC
)
1680 rc
= info
->tx_active
? info
->max_frame_size
: 0;
1682 rc
= info
->tx_count
;
1684 if (debug_level
>= DEBUG_LEVEL_INFO
)
1685 printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
1686 __FILE__
,__LINE__
, info
->device_name
, rc
);
1691 /* Discard all data in the send buffer
1693 static void mgslpc_flush_buffer(struct tty_struct
*tty
)
1695 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
1696 unsigned long flags
;
1698 if (debug_level
>= DEBUG_LEVEL_INFO
)
1699 printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
1700 __FILE__
,__LINE__
, info
->device_name
);
1702 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_flush_buffer"))
1705 spin_lock_irqsave(&info
->lock
,flags
);
1706 info
->tx_count
= info
->tx_put
= info
->tx_get
= 0;
1707 del_timer(&info
->tx_timer
);
1708 spin_unlock_irqrestore(&info
->lock
,flags
);
1710 wake_up_interruptible(&tty
->write_wait
);
1714 /* Send a high-priority XON/XOFF character
1716 static void mgslpc_send_xchar(struct tty_struct
*tty
, char ch
)
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_send_xchar(%s,%d)\n",
1723 __FILE__
,__LINE__
, info
->device_name
, ch
);
1725 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_send_xchar"))
1730 spin_lock_irqsave(&info
->lock
,flags
);
1731 if (!info
->tx_enabled
)
1732 tx_start(info
, tty
);
1733 spin_unlock_irqrestore(&info
->lock
,flags
);
1737 /* Signal remote device to throttle send data (our receive data)
1739 static void mgslpc_throttle(struct tty_struct
* tty
)
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_throttle(%s) entry\n",
1746 __FILE__
,__LINE__
, info
->device_name
);
1748 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_throttle"))
1752 mgslpc_send_xchar(tty
, STOP_CHAR(tty
));
1754 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1755 spin_lock_irqsave(&info
->lock
,flags
);
1756 info
->serial_signals
&= ~SerialSignal_RTS
;
1758 spin_unlock_irqrestore(&info
->lock
,flags
);
1762 /* Signal remote device to stop throttling send data (our receive data)
1764 static void mgslpc_unthrottle(struct tty_struct
* tty
)
1766 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
1767 unsigned long flags
;
1769 if (debug_level
>= DEBUG_LEVEL_INFO
)
1770 printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
1771 __FILE__
,__LINE__
, info
->device_name
);
1773 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_unthrottle"))
1780 mgslpc_send_xchar(tty
, START_CHAR(tty
));
1783 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1784 spin_lock_irqsave(&info
->lock
,flags
);
1785 info
->serial_signals
|= SerialSignal_RTS
;
1787 spin_unlock_irqrestore(&info
->lock
,flags
);
1791 /* get the current serial statistics
1793 static int get_stats(MGSLPC_INFO
* info
, struct mgsl_icount __user
*user_icount
)
1796 if (debug_level
>= DEBUG_LEVEL_INFO
)
1797 printk("get_params(%s)\n", info
->device_name
);
1799 memset(&info
->icount
, 0, sizeof(info
->icount
));
1801 COPY_TO_USER(err
, user_icount
, &info
->icount
, sizeof(struct mgsl_icount
));
1808 /* get the current serial parameters
1810 static int get_params(MGSLPC_INFO
* info
, MGSL_PARAMS __user
*user_params
)
1813 if (debug_level
>= DEBUG_LEVEL_INFO
)
1814 printk("get_params(%s)\n", info
->device_name
);
1815 COPY_TO_USER(err
,user_params
, &info
->params
, sizeof(MGSL_PARAMS
));
1821 /* set the serial parameters
1825 * info pointer to device instance data
1826 * new_params user buffer containing new serial params
1828 * Returns: 0 if success, otherwise error code
1830 static int set_params(MGSLPC_INFO
* info
, MGSL_PARAMS __user
*new_params
, struct tty_struct
*tty
)
1832 unsigned long flags
;
1833 MGSL_PARAMS tmp_params
;
1836 if (debug_level
>= DEBUG_LEVEL_INFO
)
1837 printk("%s(%d):set_params %s\n", __FILE__
,__LINE__
,
1838 info
->device_name
);
1839 COPY_FROM_USER(err
,&tmp_params
, new_params
, sizeof(MGSL_PARAMS
));
1841 if ( debug_level
>= DEBUG_LEVEL_INFO
)
1842 printk( "%s(%d):set_params(%s) user buffer copy failed\n",
1843 __FILE__
,__LINE__
,info
->device_name
);
1847 spin_lock_irqsave(&info
->lock
,flags
);
1848 memcpy(&info
->params
,&tmp_params
,sizeof(MGSL_PARAMS
));
1849 spin_unlock_irqrestore(&info
->lock
,flags
);
1851 mgslpc_change_params(info
, tty
);
1856 static int get_txidle(MGSLPC_INFO
* info
, int __user
*idle_mode
)
1859 if (debug_level
>= DEBUG_LEVEL_INFO
)
1860 printk("get_txidle(%s)=%d\n", info
->device_name
, info
->idle_mode
);
1861 COPY_TO_USER(err
,idle_mode
, &info
->idle_mode
, sizeof(int));
1867 static int set_txidle(MGSLPC_INFO
* info
, int idle_mode
)
1869 unsigned long flags
;
1870 if (debug_level
>= DEBUG_LEVEL_INFO
)
1871 printk("set_txidle(%s,%d)\n", info
->device_name
, idle_mode
);
1872 spin_lock_irqsave(&info
->lock
,flags
);
1873 info
->idle_mode
= idle_mode
;
1875 spin_unlock_irqrestore(&info
->lock
,flags
);
1879 static int get_interface(MGSLPC_INFO
* info
, int __user
*if_mode
)
1882 if (debug_level
>= DEBUG_LEVEL_INFO
)
1883 printk("get_interface(%s)=%d\n", info
->device_name
, info
->if_mode
);
1884 COPY_TO_USER(err
,if_mode
, &info
->if_mode
, sizeof(int));
1890 static int set_interface(MGSLPC_INFO
* info
, int if_mode
)
1892 unsigned long flags
;
1894 if (debug_level
>= DEBUG_LEVEL_INFO
)
1895 printk("set_interface(%s,%d)\n", info
->device_name
, if_mode
);
1896 spin_lock_irqsave(&info
->lock
,flags
);
1897 info
->if_mode
= if_mode
;
1899 val
= read_reg(info
, PVR
) & 0x0f;
1900 switch (info
->if_mode
)
1902 case MGSL_INTERFACE_RS232
: val
|= PVR_RS232
; break;
1903 case MGSL_INTERFACE_V35
: val
|= PVR_V35
; break;
1904 case MGSL_INTERFACE_RS422
: val
|= PVR_RS422
; break;
1906 write_reg(info
, PVR
, val
);
1908 spin_unlock_irqrestore(&info
->lock
,flags
);
1912 static int set_txenable(MGSLPC_INFO
* info
, int enable
, struct tty_struct
*tty
)
1914 unsigned long flags
;
1916 if (debug_level
>= DEBUG_LEVEL_INFO
)
1917 printk("set_txenable(%s,%d)\n", info
->device_name
, enable
);
1919 spin_lock_irqsave(&info
->lock
,flags
);
1921 if (!info
->tx_enabled
)
1922 tx_start(info
, tty
);
1924 if (info
->tx_enabled
)
1927 spin_unlock_irqrestore(&info
->lock
,flags
);
1931 static int tx_abort(MGSLPC_INFO
* info
)
1933 unsigned long flags
;
1935 if (debug_level
>= DEBUG_LEVEL_INFO
)
1936 printk("tx_abort(%s)\n", info
->device_name
);
1938 spin_lock_irqsave(&info
->lock
,flags
);
1939 if (info
->tx_active
&& info
->tx_count
&&
1940 info
->params
.mode
== MGSL_MODE_HDLC
) {
1941 /* clear data count so FIFO is not filled on next IRQ.
1942 * This results in underrun and abort transmission.
1944 info
->tx_count
= info
->tx_put
= info
->tx_get
= 0;
1945 info
->tx_aborting
= true;
1947 spin_unlock_irqrestore(&info
->lock
,flags
);
1951 static int set_rxenable(MGSLPC_INFO
* info
, int enable
)
1953 unsigned long flags
;
1955 if (debug_level
>= DEBUG_LEVEL_INFO
)
1956 printk("set_rxenable(%s,%d)\n", info
->device_name
, enable
);
1958 spin_lock_irqsave(&info
->lock
,flags
);
1960 if (!info
->rx_enabled
)
1963 if (info
->rx_enabled
)
1966 spin_unlock_irqrestore(&info
->lock
,flags
);
1970 /* wait for specified event to occur
1972 * Arguments: info pointer to device instance data
1973 * mask pointer to bitmask of events to wait for
1974 * Return Value: 0 if successful and bit mask updated with
1975 * of events triggerred,
1976 * otherwise error code
1978 static int wait_events(MGSLPC_INFO
* info
, int __user
*mask_ptr
)
1980 unsigned long flags
;
1983 struct mgsl_icount cprev
, cnow
;
1986 struct _input_signal_events oldsigs
, newsigs
;
1987 DECLARE_WAITQUEUE(wait
, current
);
1989 COPY_FROM_USER(rc
,&mask
, mask_ptr
, sizeof(int));
1993 if (debug_level
>= DEBUG_LEVEL_INFO
)
1994 printk("wait_events(%s,%d)\n", info
->device_name
, mask
);
1996 spin_lock_irqsave(&info
->lock
,flags
);
1998 /* return immediately if state matches requested events */
2000 s
= info
->serial_signals
;
2002 ( ((s
& SerialSignal_DSR
) ? MgslEvent_DsrActive
:MgslEvent_DsrInactive
) +
2003 ((s
& SerialSignal_DCD
) ? MgslEvent_DcdActive
:MgslEvent_DcdInactive
) +
2004 ((s
& SerialSignal_CTS
) ? MgslEvent_CtsActive
:MgslEvent_CtsInactive
) +
2005 ((s
& SerialSignal_RI
) ? MgslEvent_RiActive
:MgslEvent_RiInactive
) );
2007 spin_unlock_irqrestore(&info
->lock
,flags
);
2011 /* save current irq counts */
2012 cprev
= info
->icount
;
2013 oldsigs
= info
->input_signal_events
;
2015 if ((info
->params
.mode
== MGSL_MODE_HDLC
) &&
2016 (mask
& MgslEvent_ExitHuntMode
))
2017 irq_enable(info
, CHA
, IRQ_EXITHUNT
);
2019 set_current_state(TASK_INTERRUPTIBLE
);
2020 add_wait_queue(&info
->event_wait_q
, &wait
);
2022 spin_unlock_irqrestore(&info
->lock
,flags
);
2027 if (signal_pending(current
)) {
2032 /* get current irq counts */
2033 spin_lock_irqsave(&info
->lock
,flags
);
2034 cnow
= info
->icount
;
2035 newsigs
= info
->input_signal_events
;
2036 set_current_state(TASK_INTERRUPTIBLE
);
2037 spin_unlock_irqrestore(&info
->lock
,flags
);
2039 /* if no change, wait aborted for some reason */
2040 if (newsigs
.dsr_up
== oldsigs
.dsr_up
&&
2041 newsigs
.dsr_down
== oldsigs
.dsr_down
&&
2042 newsigs
.dcd_up
== oldsigs
.dcd_up
&&
2043 newsigs
.dcd_down
== oldsigs
.dcd_down
&&
2044 newsigs
.cts_up
== oldsigs
.cts_up
&&
2045 newsigs
.cts_down
== oldsigs
.cts_down
&&
2046 newsigs
.ri_up
== oldsigs
.ri_up
&&
2047 newsigs
.ri_down
== oldsigs
.ri_down
&&
2048 cnow
.exithunt
== cprev
.exithunt
&&
2049 cnow
.rxidle
== cprev
.rxidle
) {
2055 ( (newsigs
.dsr_up
!= oldsigs
.dsr_up
? MgslEvent_DsrActive
:0) +
2056 (newsigs
.dsr_down
!= oldsigs
.dsr_down
? MgslEvent_DsrInactive
:0) +
2057 (newsigs
.dcd_up
!= oldsigs
.dcd_up
? MgslEvent_DcdActive
:0) +
2058 (newsigs
.dcd_down
!= oldsigs
.dcd_down
? MgslEvent_DcdInactive
:0) +
2059 (newsigs
.cts_up
!= oldsigs
.cts_up
? MgslEvent_CtsActive
:0) +
2060 (newsigs
.cts_down
!= oldsigs
.cts_down
? MgslEvent_CtsInactive
:0) +
2061 (newsigs
.ri_up
!= oldsigs
.ri_up
? MgslEvent_RiActive
:0) +
2062 (newsigs
.ri_down
!= oldsigs
.ri_down
? MgslEvent_RiInactive
:0) +
2063 (cnow
.exithunt
!= cprev
.exithunt
? MgslEvent_ExitHuntMode
:0) +
2064 (cnow
.rxidle
!= cprev
.rxidle
? MgslEvent_IdleReceived
:0) );
2072 remove_wait_queue(&info
->event_wait_q
, &wait
);
2073 set_current_state(TASK_RUNNING
);
2075 if (mask
& MgslEvent_ExitHuntMode
) {
2076 spin_lock_irqsave(&info
->lock
,flags
);
2077 if (!waitqueue_active(&info
->event_wait_q
))
2078 irq_disable(info
, CHA
, IRQ_EXITHUNT
);
2079 spin_unlock_irqrestore(&info
->lock
,flags
);
2083 PUT_USER(rc
, events
, mask_ptr
);
2087 static int modem_input_wait(MGSLPC_INFO
*info
,int arg
)
2089 unsigned long flags
;
2091 struct mgsl_icount cprev
, cnow
;
2092 DECLARE_WAITQUEUE(wait
, current
);
2094 /* save current irq counts */
2095 spin_lock_irqsave(&info
->lock
,flags
);
2096 cprev
= info
->icount
;
2097 add_wait_queue(&info
->status_event_wait_q
, &wait
);
2098 set_current_state(TASK_INTERRUPTIBLE
);
2099 spin_unlock_irqrestore(&info
->lock
,flags
);
2103 if (signal_pending(current
)) {
2108 /* get new irq counts */
2109 spin_lock_irqsave(&info
->lock
,flags
);
2110 cnow
= info
->icount
;
2111 set_current_state(TASK_INTERRUPTIBLE
);
2112 spin_unlock_irqrestore(&info
->lock
,flags
);
2114 /* if no change, wait aborted for some reason */
2115 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
2116 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
) {
2121 /* check for change in caller specified modem input */
2122 if ((arg
& TIOCM_RNG
&& cnow
.rng
!= cprev
.rng
) ||
2123 (arg
& TIOCM_DSR
&& cnow
.dsr
!= cprev
.dsr
) ||
2124 (arg
& TIOCM_CD
&& cnow
.dcd
!= cprev
.dcd
) ||
2125 (arg
& TIOCM_CTS
&& cnow
.cts
!= cprev
.cts
)) {
2132 remove_wait_queue(&info
->status_event_wait_q
, &wait
);
2133 set_current_state(TASK_RUNNING
);
2137 /* return the state of the serial control and status signals
2139 static int tiocmget(struct tty_struct
*tty
, struct file
*file
)
2141 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
2142 unsigned int result
;
2143 unsigned long flags
;
2145 spin_lock_irqsave(&info
->lock
,flags
);
2147 spin_unlock_irqrestore(&info
->lock
,flags
);
2149 result
= ((info
->serial_signals
& SerialSignal_RTS
) ? TIOCM_RTS
:0) +
2150 ((info
->serial_signals
& SerialSignal_DTR
) ? TIOCM_DTR
:0) +
2151 ((info
->serial_signals
& SerialSignal_DCD
) ? TIOCM_CAR
:0) +
2152 ((info
->serial_signals
& SerialSignal_RI
) ? TIOCM_RNG
:0) +
2153 ((info
->serial_signals
& SerialSignal_DSR
) ? TIOCM_DSR
:0) +
2154 ((info
->serial_signals
& SerialSignal_CTS
) ? TIOCM_CTS
:0);
2156 if (debug_level
>= DEBUG_LEVEL_INFO
)
2157 printk("%s(%d):%s tiocmget() value=%08X\n",
2158 __FILE__
,__LINE__
, info
->device_name
, result
);
2162 /* set modem control signals (DTR/RTS)
2164 static int tiocmset(struct tty_struct
*tty
, struct file
*file
,
2165 unsigned int set
, unsigned int clear
)
2167 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
2168 unsigned long flags
;
2170 if (debug_level
>= DEBUG_LEVEL_INFO
)
2171 printk("%s(%d):%s tiocmset(%x,%x)\n",
2172 __FILE__
,__LINE__
,info
->device_name
, set
, clear
);
2174 if (set
& TIOCM_RTS
)
2175 info
->serial_signals
|= SerialSignal_RTS
;
2176 if (set
& TIOCM_DTR
)
2177 info
->serial_signals
|= SerialSignal_DTR
;
2178 if (clear
& TIOCM_RTS
)
2179 info
->serial_signals
&= ~SerialSignal_RTS
;
2180 if (clear
& TIOCM_DTR
)
2181 info
->serial_signals
&= ~SerialSignal_DTR
;
2183 spin_lock_irqsave(&info
->lock
,flags
);
2185 spin_unlock_irqrestore(&info
->lock
,flags
);
2190 /* Set or clear transmit break condition
2192 * Arguments: tty pointer to tty instance data
2193 * break_state -1=set break condition, 0=clear
2195 static int mgslpc_break(struct tty_struct
*tty
, int break_state
)
2197 MGSLPC_INFO
* info
= (MGSLPC_INFO
*)tty
->driver_data
;
2198 unsigned long flags
;
2200 if (debug_level
>= DEBUG_LEVEL_INFO
)
2201 printk("%s(%d):mgslpc_break(%s,%d)\n",
2202 __FILE__
,__LINE__
, info
->device_name
, break_state
);
2204 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_break"))
2207 spin_lock_irqsave(&info
->lock
,flags
);
2208 if (break_state
== -1)
2209 set_reg_bits(info
, CHA
+DAFO
, BIT6
);
2211 clear_reg_bits(info
, CHA
+DAFO
, BIT6
);
2212 spin_unlock_irqrestore(&info
->lock
,flags
);
2216 /* Service an IOCTL request
2220 * tty pointer to tty instance data
2221 * file pointer to associated file object for device
2222 * cmd IOCTL command code
2223 * arg command argument/context
2225 * Return Value: 0 if success, otherwise error code
2227 static int mgslpc_ioctl(struct tty_struct
*tty
, struct file
* file
,
2228 unsigned int cmd
, unsigned long arg
)
2230 MGSLPC_INFO
* info
= (MGSLPC_INFO
*)tty
->driver_data
;
2232 struct mgsl_icount cnow
; /* kernel counter temps */
2233 struct serial_icounter_struct __user
*p_cuser
; /* user space */
2234 void __user
*argp
= (void __user
*)arg
;
2235 unsigned long flags
;
2237 if (debug_level
>= DEBUG_LEVEL_INFO
)
2238 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__
,__LINE__
,
2239 info
->device_name
, cmd
);
2241 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_ioctl"))
2244 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
2245 (cmd
!= TIOCMIWAIT
) && (cmd
!= TIOCGICOUNT
)) {
2246 if (tty
->flags
& (1 << TTY_IO_ERROR
))
2251 case MGSL_IOCGPARAMS
:
2252 return get_params(info
, argp
);
2253 case MGSL_IOCSPARAMS
:
2254 return set_params(info
, argp
, tty
);
2255 case MGSL_IOCGTXIDLE
:
2256 return get_txidle(info
, argp
);
2257 case MGSL_IOCSTXIDLE
:
2258 return set_txidle(info
, (int)arg
);
2260 return get_interface(info
, argp
);
2262 return set_interface(info
,(int)arg
);
2263 case MGSL_IOCTXENABLE
:
2264 return set_txenable(info
,(int)arg
, tty
);
2265 case MGSL_IOCRXENABLE
:
2266 return set_rxenable(info
,(int)arg
);
2267 case MGSL_IOCTXABORT
:
2268 return tx_abort(info
);
2269 case MGSL_IOCGSTATS
:
2270 return get_stats(info
, argp
);
2271 case MGSL_IOCWAITEVENT
:
2272 return wait_events(info
, argp
);
2274 return modem_input_wait(info
,(int)arg
);
2276 spin_lock_irqsave(&info
->lock
,flags
);
2277 cnow
= info
->icount
;
2278 spin_unlock_irqrestore(&info
->lock
,flags
);
2280 PUT_USER(error
,cnow
.cts
, &p_cuser
->cts
);
2281 if (error
) return error
;
2282 PUT_USER(error
,cnow
.dsr
, &p_cuser
->dsr
);
2283 if (error
) return error
;
2284 PUT_USER(error
,cnow
.rng
, &p_cuser
->rng
);
2285 if (error
) return error
;
2286 PUT_USER(error
,cnow
.dcd
, &p_cuser
->dcd
);
2287 if (error
) return error
;
2288 PUT_USER(error
,cnow
.rx
, &p_cuser
->rx
);
2289 if (error
) return error
;
2290 PUT_USER(error
,cnow
.tx
, &p_cuser
->tx
);
2291 if (error
) return error
;
2292 PUT_USER(error
,cnow
.frame
, &p_cuser
->frame
);
2293 if (error
) return error
;
2294 PUT_USER(error
,cnow
.overrun
, &p_cuser
->overrun
);
2295 if (error
) return error
;
2296 PUT_USER(error
,cnow
.parity
, &p_cuser
->parity
);
2297 if (error
) return error
;
2298 PUT_USER(error
,cnow
.brk
, &p_cuser
->brk
);
2299 if (error
) return error
;
2300 PUT_USER(error
,cnow
.buf_overrun
, &p_cuser
->buf_overrun
);
2301 if (error
) return error
;
2304 return -ENOIOCTLCMD
;
2309 /* Set new termios settings
2313 * tty pointer to tty structure
2314 * termios pointer to buffer to hold returned old termios
2316 static void mgslpc_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
2318 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)tty
->driver_data
;
2319 unsigned long flags
;
2321 if (debug_level
>= DEBUG_LEVEL_INFO
)
2322 printk("%s(%d):mgslpc_set_termios %s\n", __FILE__
,__LINE__
,
2323 tty
->driver
->name
);
2325 /* just return if nothing has changed */
2326 if ((tty
->termios
->c_cflag
== old_termios
->c_cflag
)
2327 && (RELEVANT_IFLAG(tty
->termios
->c_iflag
)
2328 == RELEVANT_IFLAG(old_termios
->c_iflag
)))
2331 mgslpc_change_params(info
, tty
);
2333 /* Handle transition to B0 status */
2334 if (old_termios
->c_cflag
& CBAUD
&&
2335 !(tty
->termios
->c_cflag
& CBAUD
)) {
2336 info
->serial_signals
&= ~(SerialSignal_RTS
+ SerialSignal_DTR
);
2337 spin_lock_irqsave(&info
->lock
,flags
);
2339 spin_unlock_irqrestore(&info
->lock
,flags
);
2342 /* Handle transition away from B0 status */
2343 if (!(old_termios
->c_cflag
& CBAUD
) &&
2344 tty
->termios
->c_cflag
& CBAUD
) {
2345 info
->serial_signals
|= SerialSignal_DTR
;
2346 if (!(tty
->termios
->c_cflag
& CRTSCTS
) ||
2347 !test_bit(TTY_THROTTLED
, &tty
->flags
)) {
2348 info
->serial_signals
|= SerialSignal_RTS
;
2350 spin_lock_irqsave(&info
->lock
,flags
);
2352 spin_unlock_irqrestore(&info
->lock
,flags
);
2355 /* Handle turning off CRTSCTS */
2356 if (old_termios
->c_cflag
& CRTSCTS
&&
2357 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
2358 tty
->hw_stopped
= 0;
2363 static void mgslpc_close(struct tty_struct
*tty
, struct file
* filp
)
2365 MGSLPC_INFO
* info
= (MGSLPC_INFO
*)tty
->driver_data
;
2366 struct tty_port
*port
= &info
->port
;
2368 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_close"))
2371 if (debug_level
>= DEBUG_LEVEL_INFO
)
2372 printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
2373 __FILE__
,__LINE__
, info
->device_name
, port
->count
);
2375 WARN_ON(!port
->count
);
2377 if (tty_port_close_start(port
, tty
, filp
) == 0)
2380 if (port
->flags
& ASYNC_INITIALIZED
)
2381 mgslpc_wait_until_sent(tty
, info
->timeout
);
2383 mgslpc_flush_buffer(tty
);
2385 tty_ldisc_flush(tty
);
2386 shutdown(info
, tty
);
2388 tty_port_close_end(port
, tty
);
2389 tty_port_tty_set(port
, NULL
);
2391 if (debug_level
>= DEBUG_LEVEL_INFO
)
2392 printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__
,__LINE__
,
2393 tty
->driver
->name
, port
->count
);
2396 /* Wait until the transmitter is empty.
2398 static void mgslpc_wait_until_sent(struct tty_struct
*tty
, int timeout
)
2400 MGSLPC_INFO
* info
= (MGSLPC_INFO
*)tty
->driver_data
;
2401 unsigned long orig_jiffies
, char_time
;
2406 if (debug_level
>= DEBUG_LEVEL_INFO
)
2407 printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
2408 __FILE__
,__LINE__
, info
->device_name
);
2410 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_wait_until_sent"))
2413 if (!(info
->port
.flags
& ASYNC_INITIALIZED
))
2416 orig_jiffies
= jiffies
;
2418 /* Set check interval to 1/5 of estimated time to
2419 * send a character, and make it at least 1. The check
2420 * interval should also be less than the timeout.
2421 * Note: use tight timings here to satisfy the NIST-PCTS.
2424 if ( info
->params
.data_rate
) {
2425 char_time
= info
->timeout
/(32 * 5);
2432 char_time
= min_t(unsigned long, char_time
, timeout
);
2434 if (info
->params
.mode
== MGSL_MODE_HDLC
) {
2435 while (info
->tx_active
) {
2436 msleep_interruptible(jiffies_to_msecs(char_time
));
2437 if (signal_pending(current
))
2439 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
2443 while ((info
->tx_count
|| info
->tx_active
) &&
2445 msleep_interruptible(jiffies_to_msecs(char_time
));
2446 if (signal_pending(current
))
2448 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
2454 if (debug_level
>= DEBUG_LEVEL_INFO
)
2455 printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
2456 __FILE__
,__LINE__
, info
->device_name
);
2459 /* Called by tty_hangup() when a hangup is signaled.
2460 * This is the same as closing all open files for the port.
2462 static void mgslpc_hangup(struct tty_struct
*tty
)
2464 MGSLPC_INFO
* info
= (MGSLPC_INFO
*)tty
->driver_data
;
2466 if (debug_level
>= DEBUG_LEVEL_INFO
)
2467 printk("%s(%d):mgslpc_hangup(%s)\n",
2468 __FILE__
,__LINE__
, info
->device_name
);
2470 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_hangup"))
2473 mgslpc_flush_buffer(tty
);
2474 shutdown(info
, tty
);
2475 tty_port_hangup(&info
->port
);
2478 static int carrier_raised(struct tty_port
*port
)
2480 MGSLPC_INFO
*info
= container_of(port
, MGSLPC_INFO
, port
);
2481 unsigned long flags
;
2483 spin_lock_irqsave(&info
->lock
,flags
);
2485 spin_unlock_irqrestore(&info
->lock
,flags
);
2487 if (info
->serial_signals
& SerialSignal_DCD
)
2492 static void dtr_rts(struct tty_port
*port
, int onoff
)
2494 MGSLPC_INFO
*info
= container_of(port
, MGSLPC_INFO
, port
);
2495 unsigned long flags
;
2497 spin_lock_irqsave(&info
->lock
,flags
);
2499 info
->serial_signals
|= SerialSignal_RTS
+ SerialSignal_DTR
;
2501 info
->serial_signals
&= ~SerialSignal_RTS
+ SerialSignal_DTR
;
2503 spin_unlock_irqrestore(&info
->lock
,flags
);
2507 static int mgslpc_open(struct tty_struct
*tty
, struct file
* filp
)
2510 struct tty_port
*port
;
2512 unsigned long flags
;
2514 /* verify range of specified line number */
2516 if ((line
< 0) || (line
>= mgslpc_device_count
)) {
2517 printk("%s(%d):mgslpc_open with invalid line #%d.\n",
2518 __FILE__
,__LINE__
,line
);
2522 /* find the info structure for the specified line */
2523 info
= mgslpc_device_list
;
2524 while(info
&& info
->line
!= line
)
2525 info
= info
->next_device
;
2526 if (mgslpc_paranoia_check(info
, tty
->name
, "mgslpc_open"))
2530 tty
->driver_data
= info
;
2531 tty_port_tty_set(port
, tty
);
2533 if (debug_level
>= DEBUG_LEVEL_INFO
)
2534 printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
2535 __FILE__
,__LINE__
,tty
->driver
->name
, port
->count
);
2537 /* If port is closing, signal caller to try again */
2538 if (tty_hung_up_p(filp
) || port
->flags
& ASYNC_CLOSING
){
2539 if (port
->flags
& ASYNC_CLOSING
)
2540 interruptible_sleep_on(&port
->close_wait
);
2541 retval
= ((port
->flags
& ASYNC_HUP_NOTIFY
) ?
2542 -EAGAIN
: -ERESTARTSYS
);
2546 tty
->low_latency
= (port
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
2548 spin_lock_irqsave(&info
->netlock
, flags
);
2549 if (info
->netcount
) {
2551 spin_unlock_irqrestore(&info
->netlock
, flags
);
2554 spin_lock(&port
->lock
);
2556 spin_unlock(&port
->lock
);
2557 spin_unlock_irqrestore(&info
->netlock
, flags
);
2559 if (port
->count
== 1) {
2560 /* 1st open on this device, init hardware */
2561 retval
= startup(info
, tty
);
2566 retval
= tty_port_block_til_ready(&info
->port
, tty
, filp
);
2568 if (debug_level
>= DEBUG_LEVEL_INFO
)
2569 printk("%s(%d):block_til_ready(%s) returned %d\n",
2570 __FILE__
,__LINE__
, info
->device_name
, retval
);
2574 if (debug_level
>= DEBUG_LEVEL_INFO
)
2575 printk("%s(%d):mgslpc_open(%s) success\n",
2576 __FILE__
,__LINE__
, info
->device_name
);
2584 * /proc fs routines....
2587 static inline void line_info(struct seq_file
*m
, MGSLPC_INFO
*info
)
2590 unsigned long flags
;
2592 seq_printf(m
, "%s:io:%04X irq:%d",
2593 info
->device_name
, info
->io_base
, info
->irq_level
);
2595 /* output current serial signal states */
2596 spin_lock_irqsave(&info
->lock
,flags
);
2598 spin_unlock_irqrestore(&info
->lock
,flags
);
2602 if (info
->serial_signals
& SerialSignal_RTS
)
2603 strcat(stat_buf
, "|RTS");
2604 if (info
->serial_signals
& SerialSignal_CTS
)
2605 strcat(stat_buf
, "|CTS");
2606 if (info
->serial_signals
& SerialSignal_DTR
)
2607 strcat(stat_buf
, "|DTR");
2608 if (info
->serial_signals
& SerialSignal_DSR
)
2609 strcat(stat_buf
, "|DSR");
2610 if (info
->serial_signals
& SerialSignal_DCD
)
2611 strcat(stat_buf
, "|CD");
2612 if (info
->serial_signals
& SerialSignal_RI
)
2613 strcat(stat_buf
, "|RI");
2615 if (info
->params
.mode
== MGSL_MODE_HDLC
) {
2616 seq_printf(m
, " HDLC txok:%d rxok:%d",
2617 info
->icount
.txok
, info
->icount
.rxok
);
2618 if (info
->icount
.txunder
)
2619 seq_printf(m
, " txunder:%d", info
->icount
.txunder
);
2620 if (info
->icount
.txabort
)
2621 seq_printf(m
, " txabort:%d", info
->icount
.txabort
);
2622 if (info
->icount
.rxshort
)
2623 seq_printf(m
, " rxshort:%d", info
->icount
.rxshort
);
2624 if (info
->icount
.rxlong
)
2625 seq_printf(m
, " rxlong:%d", info
->icount
.rxlong
);
2626 if (info
->icount
.rxover
)
2627 seq_printf(m
, " rxover:%d", info
->icount
.rxover
);
2628 if (info
->icount
.rxcrc
)
2629 seq_printf(m
, " rxcrc:%d", info
->icount
.rxcrc
);
2631 seq_printf(m
, " ASYNC tx:%d rx:%d",
2632 info
->icount
.tx
, info
->icount
.rx
);
2633 if (info
->icount
.frame
)
2634 seq_printf(m
, " fe:%d", info
->icount
.frame
);
2635 if (info
->icount
.parity
)
2636 seq_printf(m
, " pe:%d", info
->icount
.parity
);
2637 if (info
->icount
.brk
)
2638 seq_printf(m
, " brk:%d", info
->icount
.brk
);
2639 if (info
->icount
.overrun
)
2640 seq_printf(m
, " oe:%d", info
->icount
.overrun
);
2643 /* Append serial signal status to end */
2644 seq_printf(m
, " %s\n", stat_buf
+1);
2646 seq_printf(m
, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
2647 info
->tx_active
,info
->bh_requested
,info
->bh_running
,
2651 /* Called to print information about devices
2653 static int mgslpc_proc_show(struct seq_file
*m
, void *v
)
2657 seq_printf(m
, "synclink driver:%s\n", driver_version
);
2659 info
= mgslpc_device_list
;
2662 info
= info
->next_device
;
2667 static int mgslpc_proc_open(struct inode
*inode
, struct file
*file
)
2669 return single_open(file
, mgslpc_proc_show
, NULL
);
2672 static const struct file_operations mgslpc_proc_fops
= {
2673 .owner
= THIS_MODULE
,
2674 .open
= mgslpc_proc_open
,
2676 .llseek
= seq_lseek
,
2677 .release
= single_release
,
2680 static int rx_alloc_buffers(MGSLPC_INFO
*info
)
2682 /* each buffer has header and data */
2683 info
->rx_buf_size
= sizeof(RXBUF
) + info
->max_frame_size
;
2685 /* calculate total allocation size for 8 buffers */
2686 info
->rx_buf_total_size
= info
->rx_buf_size
* 8;
2688 /* limit total allocated memory */
2689 if (info
->rx_buf_total_size
> 0x10000)
2690 info
->rx_buf_total_size
= 0x10000;
2692 /* calculate number of buffers */
2693 info
->rx_buf_count
= info
->rx_buf_total_size
/ info
->rx_buf_size
;
2695 info
->rx_buf
= kmalloc(info
->rx_buf_total_size
, GFP_KERNEL
);
2696 if (info
->rx_buf
== NULL
)
2699 rx_reset_buffers(info
);
2703 static void rx_free_buffers(MGSLPC_INFO
*info
)
2705 kfree(info
->rx_buf
);
2706 info
->rx_buf
= NULL
;
2709 static int claim_resources(MGSLPC_INFO
*info
)
2711 if (rx_alloc_buffers(info
) < 0 ) {
2712 printk( "Cant allocate rx buffer %s\n", info
->device_name
);
2713 release_resources(info
);
2719 static void release_resources(MGSLPC_INFO
*info
)
2721 if (debug_level
>= DEBUG_LEVEL_INFO
)
2722 printk("release_resources(%s)\n", info
->device_name
);
2723 rx_free_buffers(info
);
2726 /* Add the specified device instance data structure to the
2727 * global linked list of devices and increment the device count.
2729 * Arguments: info pointer to device instance data
2731 static void mgslpc_add_device(MGSLPC_INFO
*info
)
2733 info
->next_device
= NULL
;
2734 info
->line
= mgslpc_device_count
;
2735 sprintf(info
->device_name
,"ttySLP%d",info
->line
);
2737 if (info
->line
< MAX_DEVICE_COUNT
) {
2738 if (maxframe
[info
->line
])
2739 info
->max_frame_size
= maxframe
[info
->line
];
2742 mgslpc_device_count
++;
2744 if (!mgslpc_device_list
)
2745 mgslpc_device_list
= info
;
2747 MGSLPC_INFO
*current_dev
= mgslpc_device_list
;
2748 while( current_dev
->next_device
)
2749 current_dev
= current_dev
->next_device
;
2750 current_dev
->next_device
= info
;
2753 if (info
->max_frame_size
< 4096)
2754 info
->max_frame_size
= 4096;
2755 else if (info
->max_frame_size
> 65535)
2756 info
->max_frame_size
= 65535;
2758 printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
2759 info
->device_name
, info
->io_base
, info
->irq_level
);
2761 #if SYNCLINK_GENERIC_HDLC
2766 static void mgslpc_remove_device(MGSLPC_INFO
*remove_info
)
2768 MGSLPC_INFO
*info
= mgslpc_device_list
;
2769 MGSLPC_INFO
*last
= NULL
;
2772 if (info
== remove_info
) {
2774 last
->next_device
= info
->next_device
;
2776 mgslpc_device_list
= info
->next_device
;
2777 #if SYNCLINK_GENERIC_HDLC
2780 release_resources(info
);
2782 mgslpc_device_count
--;
2786 info
= info
->next_device
;
2790 static struct pcmcia_device_id mgslpc_ids
[] = {
2791 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
2794 MODULE_DEVICE_TABLE(pcmcia
, mgslpc_ids
);
2796 static struct pcmcia_driver mgslpc_driver
= {
2797 .owner
= THIS_MODULE
,
2799 .name
= "synclink_cs",
2801 .probe
= mgslpc_probe
,
2802 .remove
= mgslpc_detach
,
2803 .id_table
= mgslpc_ids
,
2804 .suspend
= mgslpc_suspend
,
2805 .resume
= mgslpc_resume
,
2808 static const struct tty_operations mgslpc_ops
= {
2809 .open
= mgslpc_open
,
2810 .close
= mgslpc_close
,
2811 .write
= mgslpc_write
,
2812 .put_char
= mgslpc_put_char
,
2813 .flush_chars
= mgslpc_flush_chars
,
2814 .write_room
= mgslpc_write_room
,
2815 .chars_in_buffer
= mgslpc_chars_in_buffer
,
2816 .flush_buffer
= mgslpc_flush_buffer
,
2817 .ioctl
= mgslpc_ioctl
,
2818 .throttle
= mgslpc_throttle
,
2819 .unthrottle
= mgslpc_unthrottle
,
2820 .send_xchar
= mgslpc_send_xchar
,
2821 .break_ctl
= mgslpc_break
,
2822 .wait_until_sent
= mgslpc_wait_until_sent
,
2823 .set_termios
= mgslpc_set_termios
,
2825 .start
= tx_release
,
2826 .hangup
= mgslpc_hangup
,
2827 .tiocmget
= tiocmget
,
2828 .tiocmset
= tiocmset
,
2829 .proc_fops
= &mgslpc_proc_fops
,
2832 static void synclink_cs_cleanup(void)
2836 printk("Unloading %s: version %s\n", driver_name
, driver_version
);
2838 while(mgslpc_device_list
)
2839 mgslpc_remove_device(mgslpc_device_list
);
2841 if (serial_driver
) {
2842 if ((rc
= tty_unregister_driver(serial_driver
)))
2843 printk("%s(%d) failed to unregister tty driver err=%d\n",
2844 __FILE__
,__LINE__
,rc
);
2845 put_tty_driver(serial_driver
);
2848 pcmcia_unregister_driver(&mgslpc_driver
);
2851 static int __init
synclink_cs_init(void)
2855 if (break_on_load
) {
2856 mgslpc_get_text_ptr();
2860 printk("%s %s\n", driver_name
, driver_version
);
2862 if ((rc
= pcmcia_register_driver(&mgslpc_driver
)) < 0)
2865 serial_driver
= alloc_tty_driver(MAX_DEVICE_COUNT
);
2866 if (!serial_driver
) {
2871 /* Initialize the tty_driver structure */
2873 serial_driver
->owner
= THIS_MODULE
;
2874 serial_driver
->driver_name
= "synclink_cs";
2875 serial_driver
->name
= "ttySLP";
2876 serial_driver
->major
= ttymajor
;
2877 serial_driver
->minor_start
= 64;
2878 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
2879 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
2880 serial_driver
->init_termios
= tty_std_termios
;
2881 serial_driver
->init_termios
.c_cflag
=
2882 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2883 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
2884 tty_set_operations(serial_driver
, &mgslpc_ops
);
2886 if ((rc
= tty_register_driver(serial_driver
)) < 0) {
2887 printk("%s(%d):Couldn't register serial driver\n",
2889 put_tty_driver(serial_driver
);
2890 serial_driver
= NULL
;
2894 printk("%s %s, tty major#%d\n",
2895 driver_name
, driver_version
,
2896 serial_driver
->major
);
2901 synclink_cs_cleanup();
2905 static void __exit
synclink_cs_exit(void)
2907 synclink_cs_cleanup();
2910 module_init(synclink_cs_init
);
2911 module_exit(synclink_cs_exit
);
2913 static void mgslpc_set_rate(MGSLPC_INFO
*info
, unsigned char channel
, unsigned int rate
)
2918 /* note:standard BRG mode is broken in V3.2 chip
2919 * so enhanced mode is always used
2927 for (M
= 1; N
> 64 && M
< 16; M
++)
2933 * BGR[7..0] contained in BGR register
2934 * BGR[9..8] contained in CCR2[7..6]
2935 * divisor = (N+1)*2^M
2937 * Note: M *must* not be zero (causes asymetric duty cycle)
2939 write_reg(info
, (unsigned char) (channel
+ BGR
),
2940 (unsigned char) ((M
<< 6) + N
));
2941 val
= read_reg(info
, (unsigned char) (channel
+ CCR2
)) & 0x3f;
2942 val
|= ((M
<< 4) & 0xc0);
2943 write_reg(info
, (unsigned char) (channel
+ CCR2
), val
);
2947 /* Enabled the AUX clock output at the specified frequency.
2949 static void enable_auxclk(MGSLPC_INFO
*info
)
2955 * 07..06 MDS[1..0] 10 = transparent HDLC mode
2956 * 05 ADM Address Mode, 0 = no addr recognition
2957 * 04 TMD Timer Mode, 0 = external
2958 * 03 RAC Receiver Active, 0 = inactive
2959 * 02 RTS 0=RTS active during xmit, 1=RTS always active
2960 * 01 TRS Timer Resolution, 1=512
2961 * 00 TLP Test Loop, 0 = no loop
2967 /* channel B RTS is used to enable AUXCLK driver on SP505 */
2968 if (info
->params
.mode
== MGSL_MODE_HDLC
&& info
->params
.clock_speed
)
2970 write_reg(info
, CHB
+ MODE
, val
);
2974 * 07 PU Power Up, 1=active, 0=power down
2975 * 06 MCE Master Clock Enable, 1=enabled
2977 * 04..02 SC[2..0] Encoding
2978 * 01..00 SM[1..0] Serial Mode, 00=HDLC
2982 write_reg(info
, CHB
+ CCR0
, 0xc0);
2986 * 07 SFLG Shared Flag, 0 = disable shared flags
2987 * 06 GALP Go Active On Loop, 0 = not used
2988 * 05 GLP Go On Loop, 0 = not used
2989 * 04 ODS Output Driver Select, 1=TxD is push-pull output
2990 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
2991 * 02..00 CM[2..0] Clock Mode
2995 write_reg(info
, CHB
+ CCR1
, 0x17);
2999 * 07..06 BGR[9..8] Baud rate bits 9..8
3000 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3001 * 04 SSEL Clock source select, 1=submode b
3002 * 03 TOE 0=TxCLK is input, 1=TxCLK is output
3003 * 02 RWX Read/Write Exchange 0=disabled
3004 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3005 * 00 DIV, data inversion 0=disabled, 1=enabled
3009 if (info
->params
.mode
== MGSL_MODE_HDLC
&& info
->params
.clock_speed
)
3010 write_reg(info
, CHB
+ CCR2
, 0x38);
3012 write_reg(info
, CHB
+ CCR2
, 0x30);
3016 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3017 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3018 * 05 TST1 Test Pin, 0=normal operation
3019 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3020 * 03..02 Reserved, must be 0
3021 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3025 write_reg(info
, CHB
+ CCR4
, 0x50);
3027 /* if auxclk not enabled, set internal BRG so
3028 * CTS transitions can be detected (requires TxC)
3030 if (info
->params
.mode
== MGSL_MODE_HDLC
&& info
->params
.clock_speed
)
3031 mgslpc_set_rate(info
, CHB
, info
->params
.clock_speed
);
3033 mgslpc_set_rate(info
, CHB
, 921600);
3036 static void loopback_enable(MGSLPC_INFO
*info
)
3040 /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
3041 val
= read_reg(info
, CHA
+ CCR1
) | (BIT2
+ BIT1
+ BIT0
);
3042 write_reg(info
, CHA
+ CCR1
, val
);
3044 /* CCR2:04 SSEL Clock source select, 1=submode b */
3045 val
= read_reg(info
, CHA
+ CCR2
) | (BIT4
+ BIT5
);
3046 write_reg(info
, CHA
+ CCR2
, val
);
3048 /* set LinkSpeed if available, otherwise default to 2Mbps */
3049 if (info
->params
.clock_speed
)
3050 mgslpc_set_rate(info
, CHA
, info
->params
.clock_speed
);
3052 mgslpc_set_rate(info
, CHA
, 1843200);
3054 /* MODE:00 TLP Test Loop, 1=loopback enabled */
3055 val
= read_reg(info
, CHA
+ MODE
) | BIT0
;
3056 write_reg(info
, CHA
+ MODE
, val
);
3059 static void hdlc_mode(MGSLPC_INFO
*info
)
3062 unsigned char clkmode
, clksubmode
;
3064 /* disable all interrupts */
3065 irq_disable(info
, CHA
, 0xffff);
3066 irq_disable(info
, CHB
, 0xffff);
3067 port_irq_disable(info
, 0xff);
3069 /* assume clock mode 0a, rcv=RxC xmt=TxC */
3070 clkmode
= clksubmode
= 0;
3071 if (info
->params
.flags
& HDLC_FLAG_RXC_DPLL
3072 && info
->params
.flags
& HDLC_FLAG_TXC_DPLL
) {
3073 /* clock mode 7a, rcv = DPLL, xmt = DPLL */
3075 } else if (info
->params
.flags
& HDLC_FLAG_RXC_BRG
3076 && info
->params
.flags
& HDLC_FLAG_TXC_BRG
) {
3077 /* clock mode 7b, rcv = BRG, xmt = BRG */
3080 } else if (info
->params
.flags
& HDLC_FLAG_RXC_DPLL
) {
3081 if (info
->params
.flags
& HDLC_FLAG_TXC_BRG
) {
3082 /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
3086 /* clock mode 6a, rcv = DPLL, xmt = TxC */
3089 } else if (info
->params
.flags
& HDLC_FLAG_TXC_BRG
) {
3090 /* clock mode 0b, rcv = RxC, xmt = BRG */
3096 * 07..06 MDS[1..0] 10 = transparent HDLC mode
3097 * 05 ADM Address Mode, 0 = no addr recognition
3098 * 04 TMD Timer Mode, 0 = external
3099 * 03 RAC Receiver Active, 0 = inactive
3100 * 02 RTS 0=RTS active during xmit, 1=RTS always active
3101 * 01 TRS Timer Resolution, 1=512
3102 * 00 TLP Test Loop, 0 = no loop
3107 if (info
->params
.loopback
)
3110 /* preserve RTS state */
3111 if (info
->serial_signals
& SerialSignal_RTS
)
3113 write_reg(info
, CHA
+ MODE
, val
);
3117 * 07 PU Power Up, 1=active, 0=power down
3118 * 06 MCE Master Clock Enable, 1=enabled
3120 * 04..02 SC[2..0] Encoding
3121 * 01..00 SM[1..0] Serial Mode, 00=HDLC
3126 switch (info
->params
.encoding
)
3128 case HDLC_ENCODING_NRZI
:
3131 case HDLC_ENCODING_BIPHASE_SPACE
:
3134 case HDLC_ENCODING_BIPHASE_MARK
:
3137 case HDLC_ENCODING_BIPHASE_LEVEL
:
3139 break; // Manchester
3141 write_reg(info
, CHA
+ CCR0
, val
);
3145 * 07 SFLG Shared Flag, 0 = disable shared flags
3146 * 06 GALP Go Active On Loop, 0 = not used
3147 * 05 GLP Go On Loop, 0 = not used
3148 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3149 * 03 ITF Interframe Time Fill, 0=mark, 1=flag
3150 * 02..00 CM[2..0] Clock Mode
3154 val
= 0x10 + clkmode
;
3155 write_reg(info
, CHA
+ CCR1
, val
);
3159 * 07..06 BGR[9..8] Baud rate bits 9..8
3160 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3161 * 04 SSEL Clock source select, 1=submode b
3162 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3163 * 02 RWX Read/Write Exchange 0=disabled
3164 * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
3165 * 00 DIV, data inversion 0=disabled, 1=enabled
3170 if (clkmode
== 2 || clkmode
== 3 || clkmode
== 6
3171 || clkmode
== 7 || (clkmode
== 0 && clksubmode
== 1))
3175 if (info
->params
.crc_type
== HDLC_CRC_32_CCITT
)
3177 if (info
->params
.encoding
== HDLC_ENCODING_NRZB
)
3179 write_reg(info
, CHA
+ CCR2
, val
);
3183 * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
3184 * 05 EPT Enable preamble transmission, 1=enabled
3185 * 04 RADD Receive address pushed to FIFO, 0=disabled
3186 * 03 CRL CRC Reset Level, 0=FFFF
3187 * 02 RCRC Rx CRC 0=On 1=Off
3188 * 01 TCRC Tx CRC 0=On 1=Off
3189 * 00 PSD DPLL Phase Shift Disable
3194 if (info
->params
.crc_type
== HDLC_CRC_NONE
)
3196 if (info
->params
.preamble
!= HDLC_PREAMBLE_PATTERN_NONE
)
3198 switch (info
->params
.preamble_length
)
3200 case HDLC_PREAMBLE_LENGTH_16BITS
:
3203 case HDLC_PREAMBLE_LENGTH_32BITS
:
3206 case HDLC_PREAMBLE_LENGTH_64BITS
:
3210 write_reg(info
, CHA
+ CCR3
, val
);
3212 /* PRE - Preamble pattern */
3214 switch (info
->params
.preamble
)
3216 case HDLC_PREAMBLE_PATTERN_FLAGS
: val
= 0x7e; break;
3217 case HDLC_PREAMBLE_PATTERN_10
: val
= 0xaa; break;
3218 case HDLC_PREAMBLE_PATTERN_01
: val
= 0x55; break;
3219 case HDLC_PREAMBLE_PATTERN_ONES
: val
= 0xff; break;
3221 write_reg(info
, CHA
+ PRE
, val
);
3225 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3226 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3227 * 05 TST1 Test Pin, 0=normal operation
3228 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3229 * 03..02 Reserved, must be 0
3230 * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
3235 write_reg(info
, CHA
+ CCR4
, val
);
3236 if (info
->params
.flags
& HDLC_FLAG_RXC_DPLL
)
3237 mgslpc_set_rate(info
, CHA
, info
->params
.clock_speed
* 16);
3239 mgslpc_set_rate(info
, CHA
, info
->params
.clock_speed
);
3241 /* RLCR Receive length check register
3243 * 7 1=enable receive length check
3244 * 6..0 Max frame length = (RL + 1) * 32
3246 write_reg(info
, CHA
+ RLCR
, 0);
3248 /* XBCH Transmit Byte Count High
3250 * 07 DMA mode, 0 = interrupt driven
3251 * 06 NRM, 0=ABM (ignored)
3252 * 05 CAS Carrier Auto Start
3253 * 04 XC Transmit Continuously (ignored)
3254 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3259 if (info
->params
.flags
& HDLC_FLAG_AUTO_DCD
)
3261 write_reg(info
, CHA
+ XBCH
, val
);
3262 enable_auxclk(info
);
3263 if (info
->params
.loopback
|| info
->testing_irq
)
3264 loopback_enable(info
);
3265 if (info
->params
.flags
& HDLC_FLAG_AUTO_CTS
)
3267 irq_enable(info
, CHB
, IRQ_CTS
);
3268 /* PVR[3] 1=AUTO CTS active */
3269 set_reg_bits(info
, CHA
+ PVR
, BIT3
);
3271 clear_reg_bits(info
, CHA
+ PVR
, BIT3
);
3273 irq_enable(info
, CHA
,
3274 IRQ_RXEOM
+ IRQ_RXFIFO
+ IRQ_ALLSENT
+
3275 IRQ_UNDERRUN
+ IRQ_TXFIFO
);
3276 issue_command(info
, CHA
, CMD_TXRESET
+ CMD_RXRESET
);
3277 wait_command_complete(info
, CHA
);
3278 read_reg16(info
, CHA
+ ISR
); /* clear pending IRQs */
3280 /* Master clock mode enabled above to allow reset commands
3281 * to complete even if no data clocks are present.
3283 * Disable master clock mode for normal communications because
3284 * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
3285 * IRQ when in master clock mode.
3287 * Leave master clock mode enabled for IRQ test because the
3288 * timer IRQ used by the test can only happen in master clock mode.
3290 if (!info
->testing_irq
)
3291 clear_reg_bits(info
, CHA
+ CCR0
, BIT6
);
3299 static void rx_stop(MGSLPC_INFO
*info
)
3301 if (debug_level
>= DEBUG_LEVEL_ISR
)
3302 printk("%s(%d):rx_stop(%s)\n",
3303 __FILE__
,__LINE__
, info
->device_name
);
3305 /* MODE:03 RAC Receiver Active, 0=inactive */
3306 clear_reg_bits(info
, CHA
+ MODE
, BIT3
);
3308 info
->rx_enabled
= false;
3309 info
->rx_overflow
= false;
3312 static void rx_start(MGSLPC_INFO
*info
)
3314 if (debug_level
>= DEBUG_LEVEL_ISR
)
3315 printk("%s(%d):rx_start(%s)\n",
3316 __FILE__
,__LINE__
, info
->device_name
);
3318 rx_reset_buffers(info
);
3319 info
->rx_enabled
= false;
3320 info
->rx_overflow
= false;
3322 /* MODE:03 RAC Receiver Active, 1=active */
3323 set_reg_bits(info
, CHA
+ MODE
, BIT3
);
3325 info
->rx_enabled
= true;
3328 static void tx_start(MGSLPC_INFO
*info
, struct tty_struct
*tty
)
3330 if (debug_level
>= DEBUG_LEVEL_ISR
)
3331 printk("%s(%d):tx_start(%s)\n",
3332 __FILE__
,__LINE__
, info
->device_name
);
3334 if (info
->tx_count
) {
3335 /* If auto RTS enabled and RTS is inactive, then assert */
3336 /* RTS and set a flag indicating that the driver should */
3337 /* negate RTS when the transmission completes. */
3338 info
->drop_rts_on_tx_done
= false;
3340 if (info
->params
.flags
& HDLC_FLAG_AUTO_RTS
) {
3342 if (!(info
->serial_signals
& SerialSignal_RTS
)) {
3343 info
->serial_signals
|= SerialSignal_RTS
;
3345 info
->drop_rts_on_tx_done
= true;
3349 if (info
->params
.mode
== MGSL_MODE_ASYNC
) {
3350 if (!info
->tx_active
) {
3351 info
->tx_active
= true;
3352 tx_ready(info
, tty
);
3355 info
->tx_active
= true;
3356 tx_ready(info
, tty
);
3357 mod_timer(&info
->tx_timer
, jiffies
+
3358 msecs_to_jiffies(5000));
3362 if (!info
->tx_enabled
)
3363 info
->tx_enabled
= true;
3366 static void tx_stop(MGSLPC_INFO
*info
)
3368 if (debug_level
>= DEBUG_LEVEL_ISR
)
3369 printk("%s(%d):tx_stop(%s)\n",
3370 __FILE__
,__LINE__
, info
->device_name
);
3372 del_timer(&info
->tx_timer
);
3374 info
->tx_enabled
= false;
3375 info
->tx_active
= false;
3378 /* Reset the adapter to a known state and prepare it for further use.
3380 static void reset_device(MGSLPC_INFO
*info
)
3382 /* power up both channels (set BIT7) */
3383 write_reg(info
, CHA
+ CCR0
, 0x80);
3384 write_reg(info
, CHB
+ CCR0
, 0x80);
3385 write_reg(info
, CHA
+ MODE
, 0);
3386 write_reg(info
, CHB
+ MODE
, 0);
3388 /* disable all interrupts */
3389 irq_disable(info
, CHA
, 0xffff);
3390 irq_disable(info
, CHB
, 0xffff);
3391 port_irq_disable(info
, 0xff);
3393 /* PCR Port Configuration Register
3395 * 07..04 DEC[3..0] Serial I/F select outputs
3396 * 03 output, 1=AUTO CTS control enabled
3397 * 02 RI Ring Indicator input 0=active
3398 * 01 DSR input 0=active
3399 * 00 DTR output 0=active
3403 write_reg(info
, PCR
, 0x06);
3405 /* PVR Port Value Register
3407 * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
3408 * 03 AUTO CTS output 1=enabled
3409 * 02 RI Ring Indicator input
3411 * 00 DTR output (1=inactive)
3415 // write_reg(info, PVR, PVR_DTR);
3417 /* IPC Interrupt Port Configuration
3419 * 07 VIS 1=Masked interrupts visible
3420 * 06..05 Reserved, 0
3421 * 04..03 SLA Slave address, 00 ignored
3422 * 02 CASM Cascading Mode, 1=daisy chain
3423 * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
3427 write_reg(info
, IPC
, 0x05);
3430 static void async_mode(MGSLPC_INFO
*info
)
3434 /* disable all interrupts */
3435 irq_disable(info
, CHA
, 0xffff);
3436 irq_disable(info
, CHB
, 0xffff);
3437 port_irq_disable(info
, 0xff);
3442 * 06 FRTS RTS State, 0=active
3443 * 05 FCTS Flow Control on CTS
3444 * 04 FLON Flow Control Enable
3445 * 03 RAC Receiver Active, 0 = inactive
3446 * 02 RTS 0=Auto RTS, 1=manual RTS
3447 * 01 TRS Timer Resolution, 1=512
3448 * 00 TLP Test Loop, 0 = no loop
3453 if (info
->params
.loopback
)
3456 /* preserve RTS state */
3457 if (!(info
->serial_signals
& SerialSignal_RTS
))
3459 write_reg(info
, CHA
+ MODE
, val
);
3463 * 07 PU Power Up, 1=active, 0=power down
3464 * 06 MCE Master Clock Enable, 1=enabled
3466 * 04..02 SC[2..0] Encoding, 000=NRZ
3467 * 01..00 SM[1..0] Serial Mode, 11=Async
3471 write_reg(info
, CHA
+ CCR0
, 0x83);
3475 * 07..05 Reserved, 0
3476 * 04 ODS Output Driver Select, 1=TxD is push-pull output
3477 * 03 BCR Bit Clock Rate, 1=16x
3478 * 02..00 CM[2..0] Clock Mode, 111=BRG
3482 write_reg(info
, CHA
+ CCR1
, 0x1f);
3486 * 07..06 BGR[9..8] Baud rate bits 9..8
3487 * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
3488 * 04 SSEL Clock source select, 1=submode b
3489 * 03 TOE 0=TxCLK is input, 0=TxCLK is input
3490 * 02 RWX Read/Write Exchange 0=disabled
3492 * 00 DIV, data inversion 0=disabled, 1=enabled
3496 write_reg(info
, CHA
+ CCR2
, 0x10);
3500 * 07..01 Reserved, 0
3501 * 00 PSD DPLL Phase Shift Disable
3505 write_reg(info
, CHA
+ CCR3
, 0);
3509 * 07 MCK4 Master Clock Divide by 4, 1=enabled
3510 * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
3511 * 05 TST1 Test Pin, 0=normal operation
3512 * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
3513 * 03..00 Reserved, must be 0
3517 write_reg(info
, CHA
+ CCR4
, 0x50);
3518 mgslpc_set_rate(info
, CHA
, info
->params
.data_rate
* 16);
3523 * 06 XBRK transmit break, 0=normal operation
3524 * 05 Stop bits (0=1, 1=2)
3525 * 04..03 PAR[1..0] Parity (01=odd, 10=even)
3526 * 02 PAREN Parity Enable
3527 * 01..00 CHL[1..0] Character Length (00=8, 01=7)
3531 if (info
->params
.data_bits
!= 8)
3532 val
|= BIT0
; /* 7 bits */
3533 if (info
->params
.stop_bits
!= 1)
3535 if (info
->params
.parity
!= ASYNC_PARITY_NONE
)
3537 val
|= BIT2
; /* Parity enable */
3538 if (info
->params
.parity
== ASYNC_PARITY_ODD
)
3543 write_reg(info
, CHA
+ DAFO
, val
);
3545 /* RFC Rx FIFO Control
3548 * 06 DPS, 1=parity bit not stored in data byte
3549 * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
3550 * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
3551 * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
3553 * 00 TCDE Terminate Char Detect Enable, 0=disabled
3557 write_reg(info
, CHA
+ RFC
, 0x5c);
3559 /* RLCR Receive length check register
3561 * Max frame length = (RL + 1) * 32
3563 write_reg(info
, CHA
+ RLCR
, 0);
3565 /* XBCH Transmit Byte Count High
3567 * 07 DMA mode, 0 = interrupt driven
3568 * 06 NRM, 0=ABM (ignored)
3569 * 05 CAS Carrier Auto Start
3570 * 04 XC Transmit Continuously (ignored)
3571 * 03..00 XBC[10..8] Transmit byte count bits 10..8
3576 if (info
->params
.flags
& HDLC_FLAG_AUTO_DCD
)
3578 write_reg(info
, CHA
+ XBCH
, val
);
3579 if (info
->params
.flags
& HDLC_FLAG_AUTO_CTS
)
3580 irq_enable(info
, CHA
, IRQ_CTS
);
3582 /* MODE:03 RAC Receiver Active, 1=active */
3583 set_reg_bits(info
, CHA
+ MODE
, BIT3
);
3584 enable_auxclk(info
);
3585 if (info
->params
.flags
& HDLC_FLAG_AUTO_CTS
) {
3586 irq_enable(info
, CHB
, IRQ_CTS
);
3587 /* PVR[3] 1=AUTO CTS active */
3588 set_reg_bits(info
, CHA
+ PVR
, BIT3
);
3590 clear_reg_bits(info
, CHA
+ PVR
, BIT3
);
3591 irq_enable(info
, CHA
,
3592 IRQ_RXEOM
+ IRQ_RXFIFO
+ IRQ_BREAK_ON
+ IRQ_RXTIME
+
3593 IRQ_ALLSENT
+ IRQ_TXFIFO
);
3594 issue_command(info
, CHA
, CMD_TXRESET
+ CMD_RXRESET
);
3595 wait_command_complete(info
, CHA
);
3596 read_reg16(info
, CHA
+ ISR
); /* clear pending IRQs */
3599 /* Set the HDLC idle mode for the transmitter.
3601 static void tx_set_idle(MGSLPC_INFO
*info
)
3603 /* Note: ESCC2 only supports flags and one idle modes */
3604 if (info
->idle_mode
== HDLC_TXIDLE_FLAGS
)
3605 set_reg_bits(info
, CHA
+ CCR1
, BIT3
);
3607 clear_reg_bits(info
, CHA
+ CCR1
, BIT3
);
3610 /* get state of the V24 status (input) signals.
3612 static void get_signals(MGSLPC_INFO
*info
)
3614 unsigned char status
= 0;
3616 /* preserve DTR and RTS */
3617 info
->serial_signals
&= SerialSignal_DTR
+ SerialSignal_RTS
;
3619 if (read_reg(info
, CHB
+ VSTR
) & BIT7
)
3620 info
->serial_signals
|= SerialSignal_DCD
;
3621 if (read_reg(info
, CHB
+ STAR
) & BIT1
)
3622 info
->serial_signals
|= SerialSignal_CTS
;
3624 status
= read_reg(info
, CHA
+ PVR
);
3625 if (!(status
& PVR_RI
))
3626 info
->serial_signals
|= SerialSignal_RI
;
3627 if (!(status
& PVR_DSR
))
3628 info
->serial_signals
|= SerialSignal_DSR
;
3631 /* Set the state of DTR and RTS based on contents of
3632 * serial_signals member of device extension.
3634 static void set_signals(MGSLPC_INFO
*info
)
3638 val
= read_reg(info
, CHA
+ MODE
);
3639 if (info
->params
.mode
== MGSL_MODE_ASYNC
) {
3640 if (info
->serial_signals
& SerialSignal_RTS
)
3645 if (info
->serial_signals
& SerialSignal_RTS
)
3650 write_reg(info
, CHA
+ MODE
, val
);
3652 if (info
->serial_signals
& SerialSignal_DTR
)
3653 clear_reg_bits(info
, CHA
+ PVR
, PVR_DTR
);
3655 set_reg_bits(info
, CHA
+ PVR
, PVR_DTR
);
3658 static void rx_reset_buffers(MGSLPC_INFO
*info
)
3665 info
->rx_frame_count
= 0;
3666 for (i
=0 ; i
< info
->rx_buf_count
; i
++) {
3667 buf
= (RXBUF
*)(info
->rx_buf
+ (i
* info
->rx_buf_size
));
3668 buf
->status
= buf
->count
= 0;
3672 /* Attempt to return a received HDLC frame
3673 * Only frames received without errors are returned.
3675 * Returns true if frame returned, otherwise false
3677 static bool rx_get_frame(MGSLPC_INFO
*info
, struct tty_struct
*tty
)
3679 unsigned short status
;
3681 unsigned int framesize
= 0;
3682 unsigned long flags
;
3683 bool return_frame
= false;
3685 if (info
->rx_frame_count
== 0)
3688 buf
= (RXBUF
*)(info
->rx_buf
+ (info
->rx_get
* info
->rx_buf_size
));
3690 status
= buf
->status
;
3692 /* 07 VFR 1=valid frame
3693 * 06 RDO 1=data overrun
3694 * 05 CRC 1=OK, 0=error
3695 * 04 RAB 1=frame aborted
3697 if ((status
& 0xf0) != 0xA0) {
3698 if (!(status
& BIT7
) || (status
& BIT4
))
3699 info
->icount
.rxabort
++;
3700 else if (status
& BIT6
)
3701 info
->icount
.rxover
++;
3702 else if (!(status
& BIT5
)) {
3703 info
->icount
.rxcrc
++;
3704 if (info
->params
.crc_type
& HDLC_CRC_RETURN_EX
)
3705 return_frame
= true;
3708 #if SYNCLINK_GENERIC_HDLC
3710 info
->netdev
->stats
.rx_errors
++;
3711 info
->netdev
->stats
.rx_frame_errors
++;
3715 return_frame
= true;
3718 framesize
= buf
->count
;
3720 if (debug_level
>= DEBUG_LEVEL_BH
)
3721 printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
3722 __FILE__
,__LINE__
,info
->device_name
,status
,framesize
);
3724 if (debug_level
>= DEBUG_LEVEL_DATA
)
3725 trace_block(info
, buf
->data
, framesize
, 0);
3728 if ((info
->params
.crc_type
& HDLC_CRC_RETURN_EX
&&
3729 framesize
+1 > info
->max_frame_size
) ||
3730 framesize
> info
->max_frame_size
)
3731 info
->icount
.rxlong
++;
3734 info
->icount
.rxok
++;
3736 if (info
->params
.crc_type
& HDLC_CRC_RETURN_EX
) {
3737 *(buf
->data
+ framesize
) = status
& BIT5
? RX_OK
:RX_CRC_ERROR
;
3741 #if SYNCLINK_GENERIC_HDLC
3743 hdlcdev_rx(info
, buf
->data
, framesize
);
3746 ldisc_receive_buf(tty
, buf
->data
, info
->flag_buf
, framesize
);
3750 spin_lock_irqsave(&info
->lock
,flags
);
3751 buf
->status
= buf
->count
= 0;
3752 info
->rx_frame_count
--;
3754 if (info
->rx_get
>= info
->rx_buf_count
)
3756 spin_unlock_irqrestore(&info
->lock
,flags
);
3761 static bool register_test(MGSLPC_INFO
*info
)
3763 static unsigned char patterns
[] =
3764 { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
3765 static unsigned int count
= ARRAY_SIZE(patterns
);
3768 unsigned long flags
;
3770 spin_lock_irqsave(&info
->lock
,flags
);
3773 for (i
= 0; i
< count
; i
++) {
3774 write_reg(info
, XAD1
, patterns
[i
]);
3775 write_reg(info
, XAD2
, patterns
[(i
+ 1) % count
]);
3776 if ((read_reg(info
, XAD1
) != patterns
[i
]) ||
3777 (read_reg(info
, XAD2
) != patterns
[(i
+ 1) % count
])) {
3783 spin_unlock_irqrestore(&info
->lock
,flags
);
3787 static bool irq_test(MGSLPC_INFO
*info
)
3789 unsigned long end_time
;
3790 unsigned long flags
;
3792 spin_lock_irqsave(&info
->lock
,flags
);
3795 info
->testing_irq
= true;
3798 info
->irq_occurred
= false;
3800 /* init hdlc mode */
3802 irq_enable(info
, CHA
, IRQ_TIMER
);
3803 write_reg(info
, CHA
+ TIMR
, 0); /* 512 cycles */
3804 issue_command(info
, CHA
, CMD_START_TIMER
);
3806 spin_unlock_irqrestore(&info
->lock
,flags
);
3809 while(end_time
-- && !info
->irq_occurred
) {
3810 msleep_interruptible(10);
3813 info
->testing_irq
= false;
3815 spin_lock_irqsave(&info
->lock
,flags
);
3817 spin_unlock_irqrestore(&info
->lock
,flags
);
3819 return info
->irq_occurred
;
3822 static int adapter_test(MGSLPC_INFO
*info
)
3824 if (!register_test(info
)) {
3825 info
->init_error
= DiagStatus_AddressFailure
;
3826 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
3827 __FILE__
,__LINE__
,info
->device_name
, (unsigned short)(info
->io_base
) );
3831 if (!irq_test(info
)) {
3832 info
->init_error
= DiagStatus_IrqFailure
;
3833 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
3834 __FILE__
,__LINE__
,info
->device_name
, (unsigned short)(info
->irq_level
) );
3838 if (debug_level
>= DEBUG_LEVEL_INFO
)
3839 printk("%s(%d):device %s passed diagnostics\n",
3840 __FILE__
,__LINE__
,info
->device_name
);
3844 static void trace_block(MGSLPC_INFO
*info
,const char* data
, int count
, int xmit
)
3849 printk("%s tx data:\n",info
->device_name
);
3851 printk("%s rx data:\n",info
->device_name
);
3859 for(i
=0;i
<linecount
;i
++)
3860 printk("%02X ",(unsigned char)data
[i
]);
3863 for(i
=0;i
<linecount
;i
++) {
3864 if (data
[i
]>=040 && data
[i
]<=0176)
3865 printk("%c",data
[i
]);
3876 /* HDLC frame time out
3877 * update stats and do tx completion processing
3879 static void tx_timeout(unsigned long context
)
3881 MGSLPC_INFO
*info
= (MGSLPC_INFO
*)context
;
3882 unsigned long flags
;
3884 if ( debug_level
>= DEBUG_LEVEL_INFO
)
3885 printk( "%s(%d):tx_timeout(%s)\n",
3886 __FILE__
,__LINE__
,info
->device_name
);
3887 if(info
->tx_active
&&
3888 info
->params
.mode
== MGSL_MODE_HDLC
) {
3889 info
->icount
.txtimeout
++;
3891 spin_lock_irqsave(&info
->lock
,flags
);
3892 info
->tx_active
= false;
3893 info
->tx_count
= info
->tx_put
= info
->tx_get
= 0;
3895 spin_unlock_irqrestore(&info
->lock
,flags
);
3897 #if SYNCLINK_GENERIC_HDLC
3899 hdlcdev_tx_done(info
);
3903 struct tty_struct
*tty
= tty_port_tty_get(&info
->port
);
3904 bh_transmit(info
, tty
);
3909 #if SYNCLINK_GENERIC_HDLC
3912 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
3913 * set encoding and frame check sequence (FCS) options
3915 * dev pointer to network device structure
3916 * encoding serial encoding setting
3917 * parity FCS setting
3919 * returns 0 if success, otherwise error code
3921 static int hdlcdev_attach(struct net_device
*dev
, unsigned short encoding
,
3922 unsigned short parity
)
3924 MGSLPC_INFO
*info
= dev_to_port(dev
);
3925 struct tty_struct
*tty
;
3926 unsigned char new_encoding
;
3927 unsigned short new_crctype
;
3929 /* return error if TTY interface open */
3930 if (info
->port
.count
)
3935 case ENCODING_NRZ
: new_encoding
= HDLC_ENCODING_NRZ
; break;
3936 case ENCODING_NRZI
: new_encoding
= HDLC_ENCODING_NRZI_SPACE
; break;
3937 case ENCODING_FM_MARK
: new_encoding
= HDLC_ENCODING_BIPHASE_MARK
; break;
3938 case ENCODING_FM_SPACE
: new_encoding
= HDLC_ENCODING_BIPHASE_SPACE
; break;
3939 case ENCODING_MANCHESTER
: new_encoding
= HDLC_ENCODING_BIPHASE_LEVEL
; break;
3940 default: return -EINVAL
;
3945 case PARITY_NONE
: new_crctype
= HDLC_CRC_NONE
; break;
3946 case PARITY_CRC16_PR1_CCITT
: new_crctype
= HDLC_CRC_16_CCITT
; break;
3947 case PARITY_CRC32_PR1_CCITT
: new_crctype
= HDLC_CRC_32_CCITT
; break;
3948 default: return -EINVAL
;
3951 info
->params
.encoding
= new_encoding
;
3952 info
->params
.crc_type
= new_crctype
;
3954 /* if network interface up, reprogram hardware */
3955 if (info
->netcount
) {
3956 tty
= tty_port_tty_get(&info
->port
);
3957 mgslpc_program_hw(info
, tty
);
3965 * called by generic HDLC layer to send frame
3967 * skb socket buffer containing HDLC frame
3968 * dev pointer to network device structure
3970 static netdev_tx_t
hdlcdev_xmit(struct sk_buff
*skb
,
3971 struct net_device
*dev
)
3973 MGSLPC_INFO
*info
= dev_to_port(dev
);
3974 unsigned long flags
;
3976 if (debug_level
>= DEBUG_LEVEL_INFO
)
3977 printk(KERN_INFO
"%s:hdlc_xmit(%s)\n",__FILE__
,dev
->name
);
3979 /* stop sending until this frame completes */
3980 netif_stop_queue(dev
);
3982 /* copy data to device buffers */
3983 skb_copy_from_linear_data(skb
, info
->tx_buf
, skb
->len
);
3985 info
->tx_put
= info
->tx_count
= skb
->len
;
3987 /* update network statistics */
3988 dev
->stats
.tx_packets
++;
3989 dev
->stats
.tx_bytes
+= skb
->len
;
3991 /* done with socket buffer, so free it */
3994 /* save start time for transmit timeout detection */
3995 dev
->trans_start
= jiffies
;
3997 /* start hardware transmitter if necessary */
3998 spin_lock_irqsave(&info
->lock
,flags
);
3999 if (!info
->tx_active
) {
4000 struct tty_struct
*tty
= tty_port_tty_get(&info
->port
);
4001 tx_start(info
, tty
);
4004 spin_unlock_irqrestore(&info
->lock
,flags
);
4006 return NETDEV_TX_OK
;
4010 * called by network layer when interface enabled
4011 * claim resources and initialize hardware
4013 * dev pointer to network device structure
4015 * returns 0 if success, otherwise error code
4017 static int hdlcdev_open(struct net_device
*dev
)
4019 MGSLPC_INFO
*info
= dev_to_port(dev
);
4020 struct tty_struct
*tty
;
4022 unsigned long flags
;
4024 if (debug_level
>= DEBUG_LEVEL_INFO
)
4025 printk("%s:hdlcdev_open(%s)\n",__FILE__
,dev
->name
);
4027 /* generic HDLC layer open processing */
4028 if ((rc
= hdlc_open(dev
)))
4031 /* arbitrate between network and tty opens */
4032 spin_lock_irqsave(&info
->netlock
, flags
);
4033 if (info
->port
.count
!= 0 || info
->netcount
!= 0) {
4034 printk(KERN_WARNING
"%s: hdlc_open returning busy\n", dev
->name
);
4035 spin_unlock_irqrestore(&info
->netlock
, flags
);
4039 spin_unlock_irqrestore(&info
->netlock
, flags
);
4041 tty
= tty_port_tty_get(&info
->port
);
4042 /* claim resources and init adapter */
4043 if ((rc
= startup(info
, tty
)) != 0) {
4045 spin_lock_irqsave(&info
->netlock
, flags
);
4047 spin_unlock_irqrestore(&info
->netlock
, flags
);
4050 /* assert DTR and RTS, apply hardware settings */
4051 info
->serial_signals
|= SerialSignal_RTS
+ SerialSignal_DTR
;
4052 mgslpc_program_hw(info
, tty
);
4055 /* enable network layer transmit */
4056 dev
->trans_start
= jiffies
;
4057 netif_start_queue(dev
);
4059 /* inform generic HDLC layer of current DCD status */
4060 spin_lock_irqsave(&info
->lock
, flags
);
4062 spin_unlock_irqrestore(&info
->lock
, flags
);
4063 if (info
->serial_signals
& SerialSignal_DCD
)
4064 netif_carrier_on(dev
);
4066 netif_carrier_off(dev
);
4071 * called by network layer when interface is disabled
4072 * shutdown hardware and release resources
4074 * dev pointer to network device structure
4076 * returns 0 if success, otherwise error code
4078 static int hdlcdev_close(struct net_device
*dev
)
4080 MGSLPC_INFO
*info
= dev_to_port(dev
);
4081 struct tty_struct
*tty
= tty_port_tty_get(&info
->port
);
4082 unsigned long flags
;
4084 if (debug_level
>= DEBUG_LEVEL_INFO
)
4085 printk("%s:hdlcdev_close(%s)\n",__FILE__
,dev
->name
);
4087 netif_stop_queue(dev
);
4089 /* shutdown adapter and release resources */
4090 shutdown(info
, tty
);
4094 spin_lock_irqsave(&info
->netlock
, flags
);
4096 spin_unlock_irqrestore(&info
->netlock
, flags
);
4102 * called by network layer to process IOCTL call to network device
4104 * dev pointer to network device structure
4105 * ifr pointer to network interface request structure
4106 * cmd IOCTL command code
4108 * returns 0 if success, otherwise error code
4110 static int hdlcdev_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
4112 const size_t size
= sizeof(sync_serial_settings
);
4113 sync_serial_settings new_line
;
4114 sync_serial_settings __user
*line
= ifr
->ifr_settings
.ifs_ifsu
.sync
;
4115 MGSLPC_INFO
*info
= dev_to_port(dev
);
4118 if (debug_level
>= DEBUG_LEVEL_INFO
)
4119 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__
,dev
->name
);
4121 /* return error if TTY interface open */
4122 if (info
->port
.count
)
4125 if (cmd
!= SIOCWANDEV
)
4126 return hdlc_ioctl(dev
, ifr
, cmd
);
4128 memset(&new_line
, 0, size
);
4130 switch(ifr
->ifr_settings
.type
) {
4131 case IF_GET_IFACE
: /* return current sync_serial_settings */
4133 ifr
->ifr_settings
.type
= IF_IFACE_SYNC_SERIAL
;
4134 if (ifr
->ifr_settings
.size
< size
) {
4135 ifr
->ifr_settings
.size
= size
; /* data size wanted */
4139 flags
= info
->params
.flags
& (HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_RXC_DPLL
|
4140 HDLC_FLAG_RXC_BRG
| HDLC_FLAG_RXC_TXCPIN
|
4141 HDLC_FLAG_TXC_TXCPIN
| HDLC_FLAG_TXC_DPLL
|
4142 HDLC_FLAG_TXC_BRG
| HDLC_FLAG_TXC_RXCPIN
);
4145 case (HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_TXCPIN
): new_line
.clock_type
= CLOCK_EXT
; break;
4146 case (HDLC_FLAG_RXC_BRG
| HDLC_FLAG_TXC_BRG
): new_line
.clock_type
= CLOCK_INT
; break;
4147 case (HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_BRG
): new_line
.clock_type
= CLOCK_TXINT
; break;
4148 case (HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_RXCPIN
): new_line
.clock_type
= CLOCK_TXFROMRX
; break;
4149 default: new_line
.clock_type
= CLOCK_DEFAULT
;
4152 new_line
.clock_rate
= info
->params
.clock_speed
;
4153 new_line
.loopback
= info
->params
.loopback
? 1:0;
4155 if (copy_to_user(line
, &new_line
, size
))
4159 case IF_IFACE_SYNC_SERIAL
: /* set sync_serial_settings */
4161 if(!capable(CAP_NET_ADMIN
))
4163 if (copy_from_user(&new_line
, line
, size
))
4166 switch (new_line
.clock_type
)
4168 case CLOCK_EXT
: flags
= HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_TXCPIN
; break;
4169 case CLOCK_TXFROMRX
: flags
= HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_RXCPIN
; break;
4170 case CLOCK_INT
: flags
= HDLC_FLAG_RXC_BRG
| HDLC_FLAG_TXC_BRG
; break;
4171 case CLOCK_TXINT
: flags
= HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_TXC_BRG
; break;
4172 case CLOCK_DEFAULT
: flags
= info
->params
.flags
&
4173 (HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_RXC_DPLL
|
4174 HDLC_FLAG_RXC_BRG
| HDLC_FLAG_RXC_TXCPIN
|
4175 HDLC_FLAG_TXC_TXCPIN
| HDLC_FLAG_TXC_DPLL
|
4176 HDLC_FLAG_TXC_BRG
| HDLC_FLAG_TXC_RXCPIN
); break;
4177 default: return -EINVAL
;
4180 if (new_line
.loopback
!= 0 && new_line
.loopback
!= 1)
4183 info
->params
.flags
&= ~(HDLC_FLAG_RXC_RXCPIN
| HDLC_FLAG_RXC_DPLL
|
4184 HDLC_FLAG_RXC_BRG
| HDLC_FLAG_RXC_TXCPIN
|
4185 HDLC_FLAG_TXC_TXCPIN
| HDLC_FLAG_TXC_DPLL
|
4186 HDLC_FLAG_TXC_BRG
| HDLC_FLAG_TXC_RXCPIN
);
4187 info
->params
.flags
|= flags
;
4189 info
->params
.loopback
= new_line
.loopback
;
4191 if (flags
& (HDLC_FLAG_RXC_BRG
| HDLC_FLAG_TXC_BRG
))
4192 info
->params
.clock_speed
= new_line
.clock_rate
;
4194 info
->params
.clock_speed
= 0;
4196 /* if network interface up, reprogram hardware */
4197 if (info
->netcount
) {
4198 struct tty_struct
*tty
= tty_port_tty_get(&info
->port
);
4199 mgslpc_program_hw(info
, tty
);
4205 return hdlc_ioctl(dev
, ifr
, cmd
);
4210 * called by network layer when transmit timeout is detected
4212 * dev pointer to network device structure
4214 static void hdlcdev_tx_timeout(struct net_device
*dev
)
4216 MGSLPC_INFO
*info
= dev_to_port(dev
);
4217 unsigned long flags
;
4219 if (debug_level
>= DEBUG_LEVEL_INFO
)
4220 printk("hdlcdev_tx_timeout(%s)\n",dev
->name
);
4222 dev
->stats
.tx_errors
++;
4223 dev
->stats
.tx_aborted_errors
++;
4225 spin_lock_irqsave(&info
->lock
,flags
);
4227 spin_unlock_irqrestore(&info
->lock
,flags
);
4229 netif_wake_queue(dev
);
4233 * called by device driver when transmit completes
4234 * reenable network layer transmit if stopped
4236 * info pointer to device instance information
4238 static void hdlcdev_tx_done(MGSLPC_INFO
*info
)
4240 if (netif_queue_stopped(info
->netdev
))
4241 netif_wake_queue(info
->netdev
);
4245 * called by device driver when frame received
4246 * pass frame to network layer
4248 * info pointer to device instance information
4249 * buf pointer to buffer contianing frame data
4250 * size count of data bytes in buf
4252 static void hdlcdev_rx(MGSLPC_INFO
*info
, char *buf
, int size
)
4254 struct sk_buff
*skb
= dev_alloc_skb(size
);
4255 struct net_device
*dev
= info
->netdev
;
4257 if (debug_level
>= DEBUG_LEVEL_INFO
)
4258 printk("hdlcdev_rx(%s)\n",dev
->name
);
4261 printk(KERN_NOTICE
"%s: can't alloc skb, dropping packet\n", dev
->name
);
4262 dev
->stats
.rx_dropped
++;
4266 memcpy(skb_put(skb
, size
), buf
, size
);
4268 skb
->protocol
= hdlc_type_trans(skb
, dev
);
4270 dev
->stats
.rx_packets
++;
4271 dev
->stats
.rx_bytes
+= size
;
4276 static const struct net_device_ops hdlcdev_ops
= {
4277 .ndo_open
= hdlcdev_open
,
4278 .ndo_stop
= hdlcdev_close
,
4279 .ndo_change_mtu
= hdlc_change_mtu
,
4280 .ndo_start_xmit
= hdlc_start_xmit
,
4281 .ndo_do_ioctl
= hdlcdev_ioctl
,
4282 .ndo_tx_timeout
= hdlcdev_tx_timeout
,
4286 * called by device driver when adding device instance
4287 * do generic HDLC initialization
4289 * info pointer to device instance information
4291 * returns 0 if success, otherwise error code
4293 static int hdlcdev_init(MGSLPC_INFO
*info
)
4296 struct net_device
*dev
;
4299 /* allocate and initialize network and HDLC layer objects */
4301 if (!(dev
= alloc_hdlcdev(info
))) {
4302 printk(KERN_ERR
"%s:hdlc device allocation failure\n",__FILE__
);
4306 /* for network layer reporting purposes only */
4307 dev
->base_addr
= info
->io_base
;
4308 dev
->irq
= info
->irq_level
;
4310 /* network layer callbacks and settings */
4311 dev
->netdev_ops
= &hdlcdev_ops
;
4312 dev
->watchdog_timeo
= 10 * HZ
;
4313 dev
->tx_queue_len
= 50;
4315 /* generic HDLC layer callbacks and settings */
4316 hdlc
= dev_to_hdlc(dev
);
4317 hdlc
->attach
= hdlcdev_attach
;
4318 hdlc
->xmit
= hdlcdev_xmit
;
4320 /* register objects with HDLC layer */
4321 if ((rc
= register_hdlc_device(dev
))) {
4322 printk(KERN_WARNING
"%s:unable to register hdlc device\n",__FILE__
);
4332 * called by device driver when removing device instance
4333 * do generic HDLC cleanup
4335 * info pointer to device instance information
4337 static void hdlcdev_exit(MGSLPC_INFO
*info
)
4339 unregister_hdlc_device(info
->netdev
);
4340 free_netdev(info
->netdev
);
4341 info
->netdev
= NULL
;
4344 #endif /* CONFIG_HDLC */