80 #define HDLC_MAGIC 0x239e
82 #include <linux/module.h>
84 #include <linux/kernel.h>
85 #include <linux/sched.h>
86 #include <linux/types.h>
87 #include <linux/fcntl.h>
89 #include <linux/ptrace.h>
92 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
94 #include <linux/poll.h>
97 #include <linux/slab.h>
98 #include <linux/tty.h>
99 #include <linux/errno.h>
100 #include <linux/string.h>
101 #include <linux/signal.h>
103 #include <linux/bitops.h>
105 #include <asm/termios.h>
106 #include <asm/uaccess.h>
111 #define MAX_HDLC_FRAME_SIZE 65535
112 #define DEFAULT_RX_BUF_COUNT 10
113 #define MAX_RX_BUF_COUNT 60
114 #define DEFAULT_TX_BUF_COUNT 3
122 #define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe)
169 static struct n_hdlc *n_hdlc_alloc (
void);
172 #define DEBUG_LEVEL_INFO 1
176 static int maxframe = 4096;
183 const unsigned char *
buf,
size_t nr);
185 unsigned int cmd,
unsigned long arg);
194 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
196 #define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
197 #define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
204 while ((buf = n_hdlc_buf_get(&n_hdlc->
rx_buf_list)))
208 static void flush_tx_queue(
struct tty_struct *tty)
214 while ((buf = n_hdlc_buf_get(&n_hdlc->
tx_buf_list)))
221 spin_unlock_irqrestore(&n_hdlc->
tx_buf_list.spinlock, flags);
228 .open = n_hdlc_tty_open,
229 .close = n_hdlc_tty_close,
230 .read = n_hdlc_tty_read,
231 .write = n_hdlc_tty_write,
232 .ioctl = n_hdlc_tty_ioctl,
233 .poll = n_hdlc_tty_poll,
234 .receive_buf = n_hdlc_tty_receive,
235 .write_wakeup = n_hdlc_tty_wakeup,
236 .flush_buffer = flush_rx_queue,
243 static void n_hdlc_release(
struct n_hdlc *n_hdlc)
249 printk(
"%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
299 static void n_hdlc_tty_close(
struct tty_struct *tty)
304 printk(
"%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
306 if (n_hdlc !=
NULL) {
311 #if defined(TTY_NO_WRITE_SPLIT)
317 if (tty != n_hdlc->
tty)
322 n_hdlc_release (n_hdlc);
327 printk(
"%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
337 static int n_hdlc_tty_open (
struct tty_struct *tty)
342 printk(
"%s(%d)n_hdlc_tty_open() called (device=%s)\n",
352 n_hdlc = n_hdlc_alloc();
362 #if defined(TTY_NO_WRITE_SPLIT)
371 printk(
"%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
386 static void n_hdlc_send_frames(
struct n_hdlc *n_hdlc,
struct tty_struct *tty)
393 printk(
"%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
399 spin_unlock_irqrestore(&n_hdlc->
tx_buf_list.spinlock, flags);
404 spin_unlock_irqrestore(&n_hdlc->
tx_buf_list.spinlock, flags);
415 printk(
"%s(%d)sending frame %p, count=%d\n",
416 __FILE__,__LINE__,tbuf,tbuf->
count);
420 actual = tty->
ops->write(tty, tbuf->
buf, tbuf->
count);
430 actual = tbuf->
count;
432 if (actual == tbuf->
count) {
434 printk(
"%s(%d)frame %p completed\n",
435 __FILE__,__LINE__,tbuf);
450 printk(
"%s(%d)frame %p pending\n",
451 __FILE__,__LINE__,tbuf);
466 spin_unlock_irqrestore(&n_hdlc->
tx_buf_list.spinlock, flags);
472 printk(
"%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
482 static void n_hdlc_tty_wakeup(
struct tty_struct *tty)
487 printk(
"%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
492 if (tty != n_hdlc->
tty) {
497 n_hdlc_send_frames (n_hdlc, tty);
512 char *flags,
int count)
514 register struct n_hdlc *n_hdlc =
tty2n_hdlc (tty);
518 printk(
"%s(%d)n_hdlc_tty_receive() called count=%d\n",
519 __FILE__,__LINE__, count);
522 if (!n_hdlc || tty != n_hdlc->
tty)
527 printk(
"%s(%d) line not using HDLC discipline\n",
532 if ( count>maxframe ) {
534 printk(
"%s(%d) rx count>maxframesize, data discarded\n",
550 printk(
"%s(%d) no more rx buffers, data discarded\n",
564 if (n_hdlc->
tty->fasync !=
NULL)
579 __u8 __user *buf,
size_t nr)
587 printk(
"%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
596 "buffer\n", __FILE__, __LINE__);
614 if (rbuf->
count > nr) {
663 const unsigned char *data,
size_t count)
671 printk(
"%s(%d)n_hdlc_tty_write() called count=%Zd\n",
672 __FILE__,__LINE__,count);
682 if (count > maxframe ) {
685 "n_hdlc_tty_write: truncating user packet "
686 "from %lu to %d\n", (
unsigned long) count,
708 tty != n_hdlc->
tty) {
709 printk(
"n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
730 n_hdlc_send_frames(n_hdlc,tty);
746 static int n_hdlc_tty_ioctl(
struct tty_struct *tty,
struct file *file,
747 unsigned int cmd,
unsigned long arg)
755 printk(
"%s(%d)n_hdlc_tty_ioctl() called %d\n",
756 __FILE__,__LINE__,cmd);
771 spin_unlock_irqrestore(&n_hdlc->
rx_buf_list.spinlock,flags);
772 error =
put_user(count, (
int __user *)arg);
782 spin_unlock_irqrestore(&n_hdlc->
tx_buf_list.spinlock,flags);
783 error =
put_user(count, (
int __user *)arg);
812 static unsigned int n_hdlc_tty_poll(
struct tty_struct *tty,
struct file *filp,
816 unsigned int mask = 0;
819 printk(
"%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
847 static struct n_hdlc *n_hdlc_alloc(
void)
856 memset(n_hdlc, 0,
sizeof(*n_hdlc));
869 printk(
"%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
878 printk(
"%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
895 memset(list, 0,
sizeof(*list));
918 spin_unlock_irqrestore(&list->
spinlock,flags);
944 spin_unlock_irqrestore(&list->
spinlock,flags);
950 KERN_INFO "HDLC line discipline maxframe=%u\n";
951 static char hdlc_register_ok[] __initdata =
952 KERN_INFO "N_HDLC line discipline registered.\n";
953 static char hdlc_register_fail[] __initdata =
954 KERN_ERR "error registering line discipline: %d\n";
955 static char hdlc_init_fail[] __initdata =
958 static int __init n_hdlc_init(
void)
965 else if (maxframe > 65535)
968 printk(hdlc_banner, maxframe);
974 printk(hdlc_register_fail, status);
977 printk(hdlc_init_fail, status);
983 KERN_INFO "N_HDLC: line discipline unregistered\n";
984 static char hdlc_unregister_fail[] __exitdata =
985 KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
987 static void __exit n_hdlc_exit(
void)
993 printk(hdlc_unregister_fail, status);
995 printk(hdlc_unregister_ok);