Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-1.0+
0002 /* generic HDLC line discipline for Linux
0003  *
0004  * Written by Paul Fulghum paulkf@microgate.com
0005  * for Microgate Corporation
0006  *
0007  * Microgate and SyncLink are registered trademarks of Microgate Corporation
0008  *
0009  * Adapted from ppp.c, written by Michael Callahan <callahan@maths.ox.ac.uk>,
0010  *  Al Longyear <longyear@netcom.com>,
0011  *  Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
0012  *
0013  * Original release 01/11/99
0014  *
0015  * This module implements the tty line discipline N_HDLC for use with
0016  * tty device drivers that support bit-synchronous HDLC communications.
0017  *
0018  * All HDLC data is frame oriented which means:
0019  *
0020  * 1. tty write calls represent one complete transmit frame of data
0021  *    The device driver should accept the complete frame or none of
0022  *    the frame (busy) in the write method. Each write call should have
0023  *    a byte count in the range of 2-65535 bytes (2 is min HDLC frame
0024  *    with 1 addr byte and 1 ctrl byte). The max byte count of 65535
0025  *    should include any crc bytes required. For example, when using
0026  *    CCITT CRC32, 4 crc bytes are required, so the maximum size frame
0027  *    the application may transmit is limited to 65531 bytes. For CCITT
0028  *    CRC16, the maximum application frame size would be 65533.
0029  *
0030  *
0031  * 2. receive callbacks from the device driver represents
0032  *    one received frame. The device driver should bypass
0033  *    the tty flip buffer and call the line discipline receive
0034  *    callback directly to avoid fragmenting or concatenating
0035  *    multiple frames into a single receive callback.
0036  *
0037  *    The HDLC line discipline queues the receive frames in separate
0038  *    buffers so complete receive frames can be returned by the
0039  *    tty read calls.
0040  *
0041  * 3. tty read calls returns an entire frame of data or nothing.
0042  *
0043  * 4. all send and receive data is considered raw. No processing
0044  *    or translation is performed by the line discipline, regardless
0045  *    of the tty flags
0046  *
0047  * 5. When line discipline is queried for the amount of receive
0048  *    data available (FIOC), 0 is returned if no data available,
0049  *    otherwise the count of the next available frame is returned.
0050  *    (instead of the sum of all received frame counts).
0051  *
0052  * These conventions allow the standard tty programming interface
0053  * to be used for synchronous HDLC applications when used with
0054  * this line discipline (or another line discipline that is frame
0055  * oriented such as N_PPP).
0056  *
0057  * The SyncLink driver (synclink.c) implements both asynchronous
0058  * (using standard line discipline N_TTY) and synchronous HDLC
0059  * (using N_HDLC) communications, with the latter using the above
0060  * conventions.
0061  *
0062  * This implementation is very basic and does not maintain
0063  * any statistics. The main point is to enforce the raw data
0064  * and frame orientation of HDLC communications.
0065  *
0066  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
0067  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0068  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0069  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
0070  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0071  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
0072  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0073  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
0074  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0075  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
0076  * OF THE POSSIBILITY OF SUCH DAMAGE.
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  * Buffers for individual HDLC frames
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  * struct n_hdlc - per device instance data structure
0127  * @magic: magic value for structure
0128  * @tbusy: reentrancy flag for tx wakeup code
0129  * @woke_up: tx wakeup needs to be run again as it was called while @tbusy
0130  * @tx_buf_list: list of pending transmit frame buffers
0131  * @rx_buf_list: list of received frame buffers
0132  * @tx_free_buf_list: list unused transmit frame buffers
0133  * @rx_free_buf_list: list unused received frame buffers
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  * HDLC buffer list manipulation functions
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 /* Local functions */
0157 
0158 static struct n_hdlc *n_hdlc_alloc(void);
0159 static void n_hdlc_tty_write_work(struct work_struct *work);
0160 
0161 /* max frame size for memory allocations */
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  * n_hdlc_tty_close - line discipline close
0194  * @tty: pointer to tty info structure
0195  *
0196  * Called when the line discipline is changed to something
0197  * else, the tty is closed, or the tty detects a hangup.
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     /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
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 }   /* end of n_hdlc_tty_close() */
0224 
0225 /**
0226  * n_hdlc_tty_open - called when line discipline changed to n_hdlc
0227  * @tty: pointer to tty info structure
0228  *
0229  * Returns 0 if success, otherwise error code
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     /* There should not be an existing table for this slot. */
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     /* change tty_io write() to not split large writes into 8K chunks */
0255     set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
0256 
0257     /* flush receive data from driver */
0258     tty_driver_flush_buffer(tty);
0259 
0260     return 0;
0261 
0262 }   /* end of n_tty_hdlc_open() */
0263 
0264 /**
0265  * n_hdlc_send_frames - send frames on pending send buffer list
0266  * @n_hdlc: pointer to ldisc instance data
0267  * @tty: pointer to tty instance data
0268  *
0269  * Send frames on pending send buffer list until the driver does not accept a
0270  * frame (busy) this function is called after adding a frame to the send buffer
0271  * list and by the tty wakeup callback.
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         /* Send the next block of data to device */
0296         set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0297         actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
0298 
0299         /* rollback was possible and has been done */
0300         if (actual == -ERESTARTSYS) {
0301             n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
0302             break;
0303         }
0304         /* if transmit error, throw frame away by */
0305         /* pretending it was accepted by driver */
0306         if (actual < 0)
0307             actual = tbuf->count;
0308 
0309         if (actual == tbuf->count) {
0310             pr_debug("frame %p completed\n", tbuf);
0311 
0312             /* free current transmit buffer */
0313             n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
0314 
0315             /* wait up sleeping writers */
0316             wake_up_interruptible(&tty->write_wait);
0317 
0318             /* get next pending transmit buffer */
0319             tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
0320         } else {
0321             pr_debug("frame %p pending\n", tbuf);
0322 
0323             /*
0324              * the buffer was not accepted by driver,
0325              * return it back into tx queue
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     /* Clear the re-entry flag */
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 }   /* end of n_hdlc_send_frames() */
0343 
0344 /**
0345  * n_hdlc_tty_write_work - Asynchronous callback for transmit wakeup
0346  * @work: pointer to work_struct
0347  *
0348  * Called when low level device driver can accept more send data.
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 }   /* end of n_hdlc_tty_write_work() */
0357 
0358 /**
0359  * n_hdlc_tty_wakeup - Callback for transmit wakeup
0360  * @tty: pointer to associated tty instance data
0361  *
0362  * Called when low level device driver can accept more send data.
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 }   /* end of n_hdlc_tty_wakeup() */
0370 
0371 /**
0372  * n_hdlc_tty_receive - Called by tty driver when receive data is available
0373  * @tty: pointer to tty instance data
0374  * @data: pointer to received data
0375  * @flags: pointer to flags for data
0376  * @count: count of received data in bytes
0377  *
0378  * Called by tty low level driver when receive data is available. Data is
0379  * interpreted as one HDLC frame.
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     /* verify line is using HDLC discipline */
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     /* get a free HDLC buffer */
0401     buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
0402     if (!buf) {
0403         /*
0404          * no buffers in free list, attempt to allocate another rx
0405          * buffer unless the maximum count has been reached
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     /* copy received data to HDLC buffer */
0418     memcpy(buf->buf, data, count);
0419     buf->count = count;
0420 
0421     /* add HDLC buffer to list of received frames */
0422     n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
0423 
0424     /* wake up any blocked reads and perform async signalling */
0425     wake_up_interruptible(&tty->read_wait);
0426     if (tty->fasync != NULL)
0427         kill_fasync(&tty->fasync, SIGIO, POLL_IN);
0428 
0429 }   /* end of n_hdlc_tty_receive() */
0430 
0431 /**
0432  * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
0433  * @tty: pointer to tty instance data
0434  * @file: pointer to open file object
0435  * @kbuf: pointer to returned data buffer
0436  * @nr: size of returned data buffer
0437  * @cookie: stored rbuf from previous run
0438  * @offset: offset into the data buffer
0439  *
0440  * Returns the number of bytes returned or error code.
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     /* Is this a repeated call for an rbuf we already found earlier? */
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         /* no data */
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     /* Have we used it up entirely? */
0495     if (offset >= rbuf->count)
0496         goto done_with_rbuf;
0497 
0498     /* More data to go, but can't copy any more? EOVERFLOW */
0499     ret = -EOVERFLOW;
0500     if (!nr)
0501         goto done_with_rbuf;
0502 
0503     /* Copy as much data as possible */
0504     ret = rbuf->count - offset;
0505     if (ret > nr)
0506         ret = nr;
0507     memcpy(kbuf, rbuf->buf+offset, ret);
0508     offset += ret;
0509 
0510     /* If we still have data left, we leave the rbuf in the cookie */
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 }   /* end of n_hdlc_tty_read() */
0525 
0526 /**
0527  * n_hdlc_tty_write - write a single frame of data to device
0528  * @tty: pointer to associated tty device instance data
0529  * @file: pointer to file object data
0530  * @data: pointer to transmit data (one frame)
0531  * @count: size of transmit frame in bytes
0532  *
0533  * Returns the number of bytes written (or error code).
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     /* verify frame size */
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         /* Retrieve the user's buffer */
0581         memcpy(tbuf->buf, data, count);
0582 
0583         /* Send the data */
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 }   /* end of n_hdlc_tty_write() */
0592 
0593 /**
0594  * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
0595  * @tty: pointer to tty instance data
0596  * @cmd: IOCTL command code
0597  * @arg: argument for IOCTL call (cmd dependent)
0598  *
0599  * Returns command dependent result.
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     /* Verify the status of the device */
0613     if (n_hdlc->magic != HDLC_MAGIC)
0614         return -EBADF;
0615 
0616     switch (cmd) {
0617     case FIONREAD:
0618         /* report count of read data available */
0619         /* in next available frame (if any) */
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         /* get the pending tx byte count in the driver */
0633         count = tty_chars_in_buffer(tty);
0634         /* add size of next output frame in queue */
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;    /* to default */
0651 
0652     default:
0653         error = n_tty_ioctl_helper(tty, cmd, arg);
0654         break;
0655     }
0656     return error;
0657 
0658 }   /* end of n_hdlc_tty_ioctl() */
0659 
0660 /**
0661  * n_hdlc_tty_poll - TTY callback for poll system call
0662  * @tty: pointer to tty instance data
0663  * @filp: pointer to open file object for device
0664  * @wait: wait queue for operations
0665  *
0666  * Determine which operations (read/write) will not block and return info
0667  * to caller.
0668  * Returns a bit mask containing info on which ops will not block.
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      * queue the current process into any wait queue that may awaken in the
0681      * future (read and write)
0682      */
0683     poll_wait(filp, &tty->read_wait, wait);
0684     poll_wait(filp, &tty->write_wait, wait);
0685 
0686     /* set bits for operations that won't block */
0687     if (!list_empty(&n_hdlc->rx_buf_list.list))
0688         mask |= EPOLLIN | EPOLLRDNORM;  /* readable */
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; /* writable */
0696 
0697     return mask;
0698 }   /* end of n_hdlc_tty_poll() */
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  * n_hdlc_alloc - allocate an n_hdlc instance data structure
0719  *
0720  * Returns a pointer to newly created structure if success, otherwise %NULL
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     /* Initialize the control block */
0743     n_hdlc->magic  = HDLC_MAGIC;
0744 
0745     return n_hdlc;
0746 
0747 }   /* end of n_hdlc_alloc() */
0748 
0749 /**
0750  * n_hdlc_buf_return - put the HDLC buffer after the head of the specified list
0751  * @buf_list: pointer to the buffer list
0752  * @buf: pointer to the buffer
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  * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
0769  * @buf_list: pointer to buffer list
0770  * @buf: pointer to buffer
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 }   /* end of n_hdlc_buf_put() */
0784 
0785 /**
0786  * n_hdlc_buf_get - remove and return an HDLC buffer from list
0787  * @buf_list: pointer to HDLC buffer list
0788  *
0789  * Remove and return an HDLC buffer from the head of the specified HDLC buffer
0790  * list.
0791  * Returns a pointer to HDLC buffer if available, otherwise %NULL.
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 }   /* end of n_hdlc_buf_get() */
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     /* range check maxframe arg */
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 }   /* end of init_module() */
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);