0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079 #define HDLC_MAGIC 0x239e
0080
0081 #include <linux/module.h>
0082 #include <linux/init.h>
0083 #include <linux/kernel.h>
0084 #include <linux/sched.h>
0085 #include <linux/types.h>
0086 #include <linux/fcntl.h>
0087 #include <linux/interrupt.h>
0088 #include <linux/ptrace.h>
0089
0090 #include <linux/poll.h>
0091 #include <linux/in.h>
0092 #include <linux/ioctl.h>
0093 #include <linux/slab.h>
0094 #include <linux/tty.h>
0095 #include <linux/errno.h>
0096 #include <linux/string.h> /* used in new tty drivers */
0097 #include <linux/signal.h> /* used in new tty drivers */
0098 #include <linux/if.h>
0099 #include <linux/bitops.h>
0100
0101 #include <asm/termios.h>
0102 #include <linux/uaccess.h>
0103 #include "tty.h"
0104
0105
0106
0107
0108 #define MAX_HDLC_FRAME_SIZE 65535
0109 #define DEFAULT_RX_BUF_COUNT 10
0110 #define MAX_RX_BUF_COUNT 60
0111 #define DEFAULT_TX_BUF_COUNT 3
0112
0113 struct n_hdlc_buf {
0114 struct list_head list_item;
0115 int count;
0116 char buf[];
0117 };
0118
0119 struct n_hdlc_buf_list {
0120 struct list_head list;
0121 int count;
0122 spinlock_t spinlock;
0123 };
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135 struct n_hdlc {
0136 int magic;
0137 bool tbusy;
0138 bool woke_up;
0139 struct n_hdlc_buf_list tx_buf_list;
0140 struct n_hdlc_buf_list rx_buf_list;
0141 struct n_hdlc_buf_list tx_free_buf_list;
0142 struct n_hdlc_buf_list rx_free_buf_list;
0143 struct work_struct write_work;
0144 struct tty_struct *tty_for_write_work;
0145 };
0146
0147
0148
0149
0150 static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
0151 struct n_hdlc_buf *buf);
0152 static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
0153 struct n_hdlc_buf *buf);
0154 static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
0155
0156
0157
0158 static struct n_hdlc *n_hdlc_alloc(void);
0159 static void n_hdlc_tty_write_work(struct work_struct *work);
0160
0161
0162 static int maxframe = 4096;
0163
0164 static void flush_rx_queue(struct tty_struct *tty)
0165 {
0166 struct n_hdlc *n_hdlc = tty->disc_data;
0167 struct n_hdlc_buf *buf;
0168
0169 while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
0170 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf);
0171 }
0172
0173 static void flush_tx_queue(struct tty_struct *tty)
0174 {
0175 struct n_hdlc *n_hdlc = tty->disc_data;
0176 struct n_hdlc_buf *buf;
0177
0178 while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
0179 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
0180 }
0181
0182 static void n_hdlc_free_buf_list(struct n_hdlc_buf_list *list)
0183 {
0184 struct n_hdlc_buf *buf;
0185
0186 do {
0187 buf = n_hdlc_buf_get(list);
0188 kfree(buf);
0189 } while (buf);
0190 }
0191
0192
0193
0194
0195
0196
0197
0198
0199 static void n_hdlc_tty_close(struct tty_struct *tty)
0200 {
0201 struct n_hdlc *n_hdlc = tty->disc_data;
0202
0203 if (n_hdlc->magic != HDLC_MAGIC) {
0204 pr_warn("n_hdlc: trying to close unopened tty!\n");
0205 return;
0206 }
0207 #if defined(TTY_NO_WRITE_SPLIT)
0208 clear_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
0209 #endif
0210 tty->disc_data = NULL;
0211
0212
0213 wake_up_interruptible(&tty->read_wait);
0214 wake_up_interruptible(&tty->write_wait);
0215
0216 cancel_work_sync(&n_hdlc->write_work);
0217
0218 n_hdlc_free_buf_list(&n_hdlc->rx_free_buf_list);
0219 n_hdlc_free_buf_list(&n_hdlc->tx_free_buf_list);
0220 n_hdlc_free_buf_list(&n_hdlc->rx_buf_list);
0221 n_hdlc_free_buf_list(&n_hdlc->tx_buf_list);
0222 kfree(n_hdlc);
0223 }
0224
0225
0226
0227
0228
0229
0230
0231 static int n_hdlc_tty_open(struct tty_struct *tty)
0232 {
0233 struct n_hdlc *n_hdlc = tty->disc_data;
0234
0235 pr_debug("%s() called (device=%s)\n", __func__, tty->name);
0236
0237
0238 if (n_hdlc) {
0239 pr_err("%s: tty already associated!\n", __func__);
0240 return -EEXIST;
0241 }
0242
0243 n_hdlc = n_hdlc_alloc();
0244 if (!n_hdlc) {
0245 pr_err("%s: n_hdlc_alloc failed\n", __func__);
0246 return -ENFILE;
0247 }
0248
0249 INIT_WORK(&n_hdlc->write_work, n_hdlc_tty_write_work);
0250 n_hdlc->tty_for_write_work = tty;
0251 tty->disc_data = n_hdlc;
0252 tty->receive_room = 65536;
0253
0254
0255 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
0256
0257
0258 tty_driver_flush_buffer(tty);
0259
0260 return 0;
0261
0262 }
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273 static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
0274 {
0275 register int actual;
0276 unsigned long flags;
0277 struct n_hdlc_buf *tbuf;
0278
0279 check_again:
0280
0281 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
0282 if (n_hdlc->tbusy) {
0283 n_hdlc->woke_up = true;
0284 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
0285 return;
0286 }
0287 n_hdlc->tbusy = true;
0288 n_hdlc->woke_up = false;
0289 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
0290
0291 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
0292 while (tbuf) {
0293 pr_debug("sending frame %p, count=%d\n", tbuf, tbuf->count);
0294
0295
0296 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0297 actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
0298
0299
0300 if (actual == -ERESTARTSYS) {
0301 n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
0302 break;
0303 }
0304
0305
0306 if (actual < 0)
0307 actual = tbuf->count;
0308
0309 if (actual == tbuf->count) {
0310 pr_debug("frame %p completed\n", tbuf);
0311
0312
0313 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
0314
0315
0316 wake_up_interruptible(&tty->write_wait);
0317
0318
0319 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
0320 } else {
0321 pr_debug("frame %p pending\n", tbuf);
0322
0323
0324
0325
0326
0327 n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
0328 break;
0329 }
0330 }
0331
0332 if (!tbuf)
0333 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0334
0335
0336 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
0337 n_hdlc->tbusy = false;
0338 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
0339
0340 if (n_hdlc->woke_up)
0341 goto check_again;
0342 }
0343
0344
0345
0346
0347
0348
0349
0350 static void n_hdlc_tty_write_work(struct work_struct *work)
0351 {
0352 struct n_hdlc *n_hdlc = container_of(work, struct n_hdlc, write_work);
0353 struct tty_struct *tty = n_hdlc->tty_for_write_work;
0354
0355 n_hdlc_send_frames(n_hdlc, tty);
0356 }
0357
0358
0359
0360
0361
0362
0363
0364 static void n_hdlc_tty_wakeup(struct tty_struct *tty)
0365 {
0366 struct n_hdlc *n_hdlc = tty->disc_data;
0367
0368 schedule_work(&n_hdlc->write_work);
0369 }
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381 static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
0382 const char *flags, int count)
0383 {
0384 register struct n_hdlc *n_hdlc = tty->disc_data;
0385 register struct n_hdlc_buf *buf;
0386
0387 pr_debug("%s() called count=%d\n", __func__, count);
0388
0389
0390 if (n_hdlc->magic != HDLC_MAGIC) {
0391 pr_err("line not using HDLC discipline\n");
0392 return;
0393 }
0394
0395 if (count > maxframe) {
0396 pr_debug("rx count>maxframesize, data discarded\n");
0397 return;
0398 }
0399
0400
0401 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
0402 if (!buf) {
0403
0404
0405
0406
0407 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
0408 buf = kmalloc(struct_size(buf, buf, maxframe),
0409 GFP_ATOMIC);
0410 }
0411
0412 if (!buf) {
0413 pr_debug("no more rx buffers, data discarded\n");
0414 return;
0415 }
0416
0417
0418 memcpy(buf->buf, data, count);
0419 buf->count = count;
0420
0421
0422 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
0423
0424
0425 wake_up_interruptible(&tty->read_wait);
0426 if (tty->fasync != NULL)
0427 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
0428
0429 }
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442 static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
0443 __u8 *kbuf, size_t nr,
0444 void **cookie, unsigned long offset)
0445 {
0446 struct n_hdlc *n_hdlc = tty->disc_data;
0447 int ret = 0;
0448 struct n_hdlc_buf *rbuf;
0449 DECLARE_WAITQUEUE(wait, current);
0450
0451
0452 rbuf = *cookie;
0453 if (rbuf)
0454 goto have_rbuf;
0455
0456 add_wait_queue(&tty->read_wait, &wait);
0457
0458 for (;;) {
0459 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
0460 ret = -EIO;
0461 break;
0462 }
0463 if (tty_hung_up_p(file))
0464 break;
0465
0466 set_current_state(TASK_INTERRUPTIBLE);
0467
0468 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
0469 if (rbuf)
0470 break;
0471
0472
0473 if (tty_io_nonblock(tty, file)) {
0474 ret = -EAGAIN;
0475 break;
0476 }
0477
0478 schedule();
0479
0480 if (signal_pending(current)) {
0481 ret = -EINTR;
0482 break;
0483 }
0484 }
0485
0486 remove_wait_queue(&tty->read_wait, &wait);
0487 __set_current_state(TASK_RUNNING);
0488
0489 if (!rbuf)
0490 return ret;
0491 *cookie = rbuf;
0492
0493 have_rbuf:
0494
0495 if (offset >= rbuf->count)
0496 goto done_with_rbuf;
0497
0498
0499 ret = -EOVERFLOW;
0500 if (!nr)
0501 goto done_with_rbuf;
0502
0503
0504 ret = rbuf->count - offset;
0505 if (ret > nr)
0506 ret = nr;
0507 memcpy(kbuf, rbuf->buf+offset, ret);
0508 offset += ret;
0509
0510
0511 if (offset < rbuf->count)
0512 return ret;
0513
0514 done_with_rbuf:
0515 *cookie = NULL;
0516
0517 if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
0518 kfree(rbuf);
0519 else
0520 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
0521
0522 return ret;
0523
0524 }
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535 static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
0536 const unsigned char *data, size_t count)
0537 {
0538 struct n_hdlc *n_hdlc = tty->disc_data;
0539 int error = 0;
0540 DECLARE_WAITQUEUE(wait, current);
0541 struct n_hdlc_buf *tbuf;
0542
0543 pr_debug("%s() called count=%zd\n", __func__, count);
0544
0545 if (n_hdlc->magic != HDLC_MAGIC)
0546 return -EIO;
0547
0548
0549 if (count > maxframe) {
0550 pr_debug("%s: truncating user packet from %zu to %d\n",
0551 __func__, count, maxframe);
0552 count = maxframe;
0553 }
0554
0555 add_wait_queue(&tty->write_wait, &wait);
0556
0557 for (;;) {
0558 set_current_state(TASK_INTERRUPTIBLE);
0559
0560 tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
0561 if (tbuf)
0562 break;
0563
0564 if (tty_io_nonblock(tty, file)) {
0565 error = -EAGAIN;
0566 break;
0567 }
0568 schedule();
0569
0570 if (signal_pending(current)) {
0571 error = -EINTR;
0572 break;
0573 }
0574 }
0575
0576 __set_current_state(TASK_RUNNING);
0577 remove_wait_queue(&tty->write_wait, &wait);
0578
0579 if (!error) {
0580
0581 memcpy(tbuf->buf, data, count);
0582
0583
0584 tbuf->count = error = count;
0585 n_hdlc_buf_put(&n_hdlc->tx_buf_list, tbuf);
0586 n_hdlc_send_frames(n_hdlc, tty);
0587 }
0588
0589 return error;
0590
0591 }
0592
0593
0594
0595
0596
0597
0598
0599
0600
0601 static int n_hdlc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
0602 unsigned long arg)
0603 {
0604 struct n_hdlc *n_hdlc = tty->disc_data;
0605 int error = 0;
0606 int count;
0607 unsigned long flags;
0608 struct n_hdlc_buf *buf = NULL;
0609
0610 pr_debug("%s() called %d\n", __func__, cmd);
0611
0612
0613 if (n_hdlc->magic != HDLC_MAGIC)
0614 return -EBADF;
0615
0616 switch (cmd) {
0617 case FIONREAD:
0618
0619
0620 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock, flags);
0621 buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list,
0622 struct n_hdlc_buf, list_item);
0623 if (buf)
0624 count = buf->count;
0625 else
0626 count = 0;
0627 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock, flags);
0628 error = put_user(count, (int __user *)arg);
0629 break;
0630
0631 case TIOCOUTQ:
0632
0633 count = tty_chars_in_buffer(tty);
0634
0635 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
0636 buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list,
0637 struct n_hdlc_buf, list_item);
0638 if (buf)
0639 count += buf->count;
0640 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
0641 error = put_user(count, (int __user *)arg);
0642 break;
0643
0644 case TCFLSH:
0645 switch (arg) {
0646 case TCIOFLUSH:
0647 case TCOFLUSH:
0648 flush_tx_queue(tty);
0649 }
0650 fallthrough;
0651
0652 default:
0653 error = n_tty_ioctl_helper(tty, cmd, arg);
0654 break;
0655 }
0656 return error;
0657
0658 }
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670 static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
0671 poll_table *wait)
0672 {
0673 struct n_hdlc *n_hdlc = tty->disc_data;
0674 __poll_t mask = 0;
0675
0676 if (n_hdlc->magic != HDLC_MAGIC)
0677 return 0;
0678
0679
0680
0681
0682
0683 poll_wait(filp, &tty->read_wait, wait);
0684 poll_wait(filp, &tty->write_wait, wait);
0685
0686
0687 if (!list_empty(&n_hdlc->rx_buf_list.list))
0688 mask |= EPOLLIN | EPOLLRDNORM;
0689 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
0690 mask |= EPOLLHUP;
0691 if (tty_hung_up_p(filp))
0692 mask |= EPOLLHUP;
0693 if (!tty_is_writelocked(tty) &&
0694 !list_empty(&n_hdlc->tx_free_buf_list.list))
0695 mask |= EPOLLOUT | EPOLLWRNORM;
0696
0697 return mask;
0698 }
0699
0700 static void n_hdlc_alloc_buf(struct n_hdlc_buf_list *list, unsigned int count,
0701 const char *name)
0702 {
0703 struct n_hdlc_buf *buf;
0704 unsigned int i;
0705
0706 for (i = 0; i < count; i++) {
0707 buf = kmalloc(struct_size(buf, buf, maxframe), GFP_KERNEL);
0708 if (!buf) {
0709 pr_debug("%s(), kmalloc() failed for %s buffer %u\n",
0710 __func__, name, i);
0711 return;
0712 }
0713 n_hdlc_buf_put(list, buf);
0714 }
0715 }
0716
0717
0718
0719
0720
0721
0722 static struct n_hdlc *n_hdlc_alloc(void)
0723 {
0724 struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL);
0725
0726 if (!n_hdlc)
0727 return NULL;
0728
0729 spin_lock_init(&n_hdlc->rx_free_buf_list.spinlock);
0730 spin_lock_init(&n_hdlc->tx_free_buf_list.spinlock);
0731 spin_lock_init(&n_hdlc->rx_buf_list.spinlock);
0732 spin_lock_init(&n_hdlc->tx_buf_list.spinlock);
0733
0734 INIT_LIST_HEAD(&n_hdlc->rx_free_buf_list.list);
0735 INIT_LIST_HEAD(&n_hdlc->tx_free_buf_list.list);
0736 INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list);
0737 INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list);
0738
0739 n_hdlc_alloc_buf(&n_hdlc->rx_free_buf_list, DEFAULT_RX_BUF_COUNT, "rx");
0740 n_hdlc_alloc_buf(&n_hdlc->tx_free_buf_list, DEFAULT_TX_BUF_COUNT, "tx");
0741
0742
0743 n_hdlc->magic = HDLC_MAGIC;
0744
0745 return n_hdlc;
0746
0747 }
0748
0749
0750
0751
0752
0753
0754 static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
0755 struct n_hdlc_buf *buf)
0756 {
0757 unsigned long flags;
0758
0759 spin_lock_irqsave(&buf_list->spinlock, flags);
0760
0761 list_add(&buf->list_item, &buf_list->list);
0762 buf_list->count++;
0763
0764 spin_unlock_irqrestore(&buf_list->spinlock, flags);
0765 }
0766
0767
0768
0769
0770
0771
0772 static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
0773 struct n_hdlc_buf *buf)
0774 {
0775 unsigned long flags;
0776
0777 spin_lock_irqsave(&buf_list->spinlock, flags);
0778
0779 list_add_tail(&buf->list_item, &buf_list->list);
0780 buf_list->count++;
0781
0782 spin_unlock_irqrestore(&buf_list->spinlock, flags);
0783 }
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793 static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list)
0794 {
0795 unsigned long flags;
0796 struct n_hdlc_buf *buf;
0797
0798 spin_lock_irqsave(&buf_list->spinlock, flags);
0799
0800 buf = list_first_entry_or_null(&buf_list->list,
0801 struct n_hdlc_buf, list_item);
0802 if (buf) {
0803 list_del(&buf->list_item);
0804 buf_list->count--;
0805 }
0806
0807 spin_unlock_irqrestore(&buf_list->spinlock, flags);
0808 return buf;
0809 }
0810
0811 static struct tty_ldisc_ops n_hdlc_ldisc = {
0812 .owner = THIS_MODULE,
0813 .num = N_HDLC,
0814 .name = "hdlc",
0815 .open = n_hdlc_tty_open,
0816 .close = n_hdlc_tty_close,
0817 .read = n_hdlc_tty_read,
0818 .write = n_hdlc_tty_write,
0819 .ioctl = n_hdlc_tty_ioctl,
0820 .poll = n_hdlc_tty_poll,
0821 .receive_buf = n_hdlc_tty_receive,
0822 .write_wakeup = n_hdlc_tty_wakeup,
0823 .flush_buffer = flush_rx_queue,
0824 };
0825
0826 static int __init n_hdlc_init(void)
0827 {
0828 int status;
0829
0830
0831 maxframe = clamp(maxframe, 4096, MAX_HDLC_FRAME_SIZE);
0832
0833 status = tty_register_ldisc(&n_hdlc_ldisc);
0834 if (!status)
0835 pr_info("N_HDLC line discipline registered with maxframe=%d\n",
0836 maxframe);
0837 else
0838 pr_err("N_HDLC: error registering line discipline: %d\n",
0839 status);
0840
0841 return status;
0842
0843 }
0844
0845 static void __exit n_hdlc_exit(void)
0846 {
0847 tty_unregister_ldisc(&n_hdlc_ldisc);
0848 }
0849
0850 module_init(n_hdlc_init);
0851 module_exit(n_hdlc_exit);
0852
0853 MODULE_LICENSE("GPL");
0854 MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
0855 module_param(maxframe, int, 0);
0856 MODULE_ALIAS_LDISC(N_HDLC);