^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-1.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* generic HDLC line discipline for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Written by Paul Fulghum paulkf@microgate.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * for Microgate Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Microgate and SyncLink are registered trademarks of Microgate Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Adapted from ppp.c, written by Michael Callahan <callahan@maths.ox.ac.uk>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Al Longyear <longyear@netcom.com>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Original release 01/11/99
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * This module implements the tty line discipline N_HDLC for use with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * tty device drivers that support bit-synchronous HDLC communications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * All HDLC data is frame oriented which means:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * 1. tty write calls represent one complete transmit frame of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * The device driver should accept the complete frame or none of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * the frame (busy) in the write method. Each write call should have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * a byte count in the range of 2-65535 bytes (2 is min HDLC frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * with 1 addr byte and 1 ctrl byte). The max byte count of 65535
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * should include any crc bytes required. For example, when using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * CCITT CRC32, 4 crc bytes are required, so the maximum size frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * the application may transmit is limited to 65531 bytes. For CCITT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * CRC16, the maximum application frame size would be 65533.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * 2. receive callbacks from the device driver represents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * one received frame. The device driver should bypass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * the tty flip buffer and call the line discipline receive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * callback directly to avoid fragmenting or concatenating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * multiple frames into a single receive callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * The HDLC line discipline queues the receive frames in separate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * buffers so complete receive frames can be returned by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * tty read calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * 3. tty read calls returns an entire frame of data or nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * 4. all send and receive data is considered raw. No processing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * or translation is performed by the line discipline, regardless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * of the tty flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * 5. When line discipline is queried for the amount of receive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * data available (FIOC), 0 is returned if no data available,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * otherwise the count of the next available frame is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * (instead of the sum of all received frame counts).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * These conventions allow the standard tty programming interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * to be used for synchronous HDLC applications when used with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * this line discipline (or another line discipline that is frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * oriented such as N_PPP).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * The SyncLink driver (synclink.c) implements both asynchronous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * (using standard line discipline N_TTY) and synchronous HDLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * (using N_HDLC) communications, with the latter using the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * conventions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * This implementation is very basic and does not maintain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * any statistics. The main point is to enforce the raw data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * and frame orientation of HDLC communications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * OF THE POSSIBILITY OF SUCH DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define HDLC_MAGIC 0x239e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #include <linux/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #include <linux/string.h> /* used in new tty drivers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #include <linux/signal.h> /* used in new tty drivers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #include <linux/if.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #include <asm/termios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * Buffers for individual HDLC frames
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define MAX_HDLC_FRAME_SIZE 65535
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #define DEFAULT_RX_BUF_COUNT 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define MAX_RX_BUF_COUNT 60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #define DEFAULT_TX_BUF_COUNT 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct n_hdlc_buf {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct list_head list_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) char buf[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct n_hdlc_buf_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) spinlock_t spinlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * struct n_hdlc - per device instance data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * @magic: magic value for structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * @tbusy: reentrancy flag for tx wakeup code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * @woke_up: tx wakeup needs to be run again as it was called while @tbusy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * @tx_buf_list: list of pending transmit frame buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * @rx_buf_list: list of received frame buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * @tx_free_buf_list: list unused transmit frame buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * @rx_free_buf_list: list unused received frame buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct n_hdlc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) bool tbusy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) bool woke_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct n_hdlc_buf_list tx_buf_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct n_hdlc_buf_list rx_buf_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct n_hdlc_buf_list tx_free_buf_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct n_hdlc_buf_list rx_free_buf_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct work_struct write_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct tty_struct *tty_for_write_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * HDLC buffer list manipulation functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct n_hdlc_buf *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct n_hdlc_buf *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* Local functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static struct n_hdlc *n_hdlc_alloc(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static void n_hdlc_tty_write_work(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* max frame size for memory allocations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int maxframe = 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static void flush_rx_queue(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct n_hdlc *n_hdlc = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct n_hdlc_buf *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static void flush_tx_queue(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct n_hdlc *n_hdlc = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct n_hdlc_buf *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static void n_hdlc_free_buf_list(struct n_hdlc_buf_list *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct n_hdlc_buf *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) buf = n_hdlc_buf_get(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) } while (buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * n_hdlc_tty_close - line discipline close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * @tty: pointer to tty info structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * Called when the line discipline is changed to something
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * else, the tty is closed, or the tty detects a hangup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static void n_hdlc_tty_close(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct n_hdlc *n_hdlc = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (n_hdlc->magic != HDLC_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) pr_warn("n_hdlc: trying to close unopened tty!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) #if defined(TTY_NO_WRITE_SPLIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) clear_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) tty->disc_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) wake_up_interruptible(&tty->read_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) wake_up_interruptible(&tty->write_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) cancel_work_sync(&n_hdlc->write_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) n_hdlc_free_buf_list(&n_hdlc->rx_free_buf_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) n_hdlc_free_buf_list(&n_hdlc->tx_free_buf_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) n_hdlc_free_buf_list(&n_hdlc->rx_buf_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) n_hdlc_free_buf_list(&n_hdlc->tx_buf_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) kfree(n_hdlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) } /* end of n_hdlc_tty_close() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * n_hdlc_tty_open - called when line discipline changed to n_hdlc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * @tty: pointer to tty info structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * Returns 0 if success, otherwise error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int n_hdlc_tty_open(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct n_hdlc *n_hdlc = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) pr_debug("%s() called (device=%s)\n", __func__, tty->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* There should not be an existing table for this slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (n_hdlc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) pr_err("%s: tty already associated!\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) n_hdlc = n_hdlc_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (!n_hdlc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) pr_err("%s: n_hdlc_alloc failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return -ENFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) INIT_WORK(&n_hdlc->write_work, n_hdlc_tty_write_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) n_hdlc->tty_for_write_work = tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) tty->disc_data = n_hdlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) tty->receive_room = 65536;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* change tty_io write() to not split large writes into 8K chunks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /* flush receive data from driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) tty_driver_flush_buffer(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) } /* end of n_tty_hdlc_open() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * n_hdlc_send_frames - send frames on pending send buffer list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * @n_hdlc: pointer to ldisc instance data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * @tty: pointer to tty instance data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * Send frames on pending send buffer list until the driver does not accept a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * frame (busy) this function is called after adding a frame to the send buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * list and by the tty wakeup callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) register int actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct n_hdlc_buf *tbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) check_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (n_hdlc->tbusy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) n_hdlc->woke_up = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) n_hdlc->tbusy = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) n_hdlc->woke_up = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) while (tbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) pr_debug("sending frame %p, count=%d\n", tbuf, tbuf->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* Send the next block of data to device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /* rollback was possible and has been done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (actual == -ERESTARTSYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /* if transmit error, throw frame away by */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /* pretending it was accepted by driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (actual < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) actual = tbuf->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (actual == tbuf->count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) pr_debug("frame %p completed\n", tbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /* free current transmit buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* wait up sleeping writers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) wake_up_interruptible(&tty->write_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /* get next pending transmit buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) pr_debug("frame %p pending\n", tbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * the buffer was not accepted by driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * return it back into tx queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (!tbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* Clear the re-entry flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) n_hdlc->tbusy = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (n_hdlc->woke_up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) goto check_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) } /* end of n_hdlc_send_frames() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * n_hdlc_tty_write_work - Asynchronous callback for transmit wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * @work: pointer to work_struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * Called when low level device driver can accept more send data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static void n_hdlc_tty_write_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct n_hdlc *n_hdlc = container_of(work, struct n_hdlc, write_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct tty_struct *tty = n_hdlc->tty_for_write_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) n_hdlc_send_frames(n_hdlc, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) } /* end of n_hdlc_tty_write_work() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * n_hdlc_tty_wakeup - Callback for transmit wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * @tty: pointer to associated tty instance data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * Called when low level device driver can accept more send data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static void n_hdlc_tty_wakeup(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct n_hdlc *n_hdlc = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) schedule_work(&n_hdlc->write_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) } /* end of n_hdlc_tty_wakeup() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * n_hdlc_tty_receive - Called by tty driver when receive data is available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * @tty: pointer to tty instance data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * @data: pointer to received data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * @flags: pointer to flags for data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * @count: count of received data in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * Called by tty low level driver when receive data is available. Data is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * interpreted as one HDLC frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) char *flags, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) register struct n_hdlc *n_hdlc = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) register struct n_hdlc_buf *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) pr_debug("%s() called count=%d\n", __func__, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /* verify line is using HDLC discipline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (n_hdlc->magic != HDLC_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) pr_err("line not using HDLC discipline\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (count > maxframe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) pr_debug("rx count>maxframesize, data discarded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /* get a free HDLC buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * no buffers in free list, attempt to allocate another rx
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * buffer unless the maximum count has been reached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) buf = kmalloc(struct_size(buf, buf, maxframe),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) pr_debug("no more rx buffers, data discarded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* copy received data to HDLC buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) memcpy(buf->buf, data, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) buf->count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* add HDLC buffer to list of received frames */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) /* wake up any blocked reads and perform async signalling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) wake_up_interruptible(&tty->read_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (tty->fasync != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) kill_fasync(&tty->fasync, SIGIO, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) } /* end of n_hdlc_tty_receive() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * @tty: pointer to tty instance data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) * @file: pointer to open file object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * @buf: pointer to returned data buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * @nr: size of returned data buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * Returns the number of bytes returned or error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) __u8 *kbuf, size_t nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) void **cookie, unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) struct n_hdlc *n_hdlc = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct n_hdlc_buf *rbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) /* Is this a repeated call for an rbuf we already found earlier? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) rbuf = *cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (rbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) goto have_rbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) add_wait_queue(&tty->read_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (tty_hung_up_p(file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (rbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) /* no data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (tty_io_nonblock(tty, file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) ret = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) remove_wait_queue(&tty->read_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (!rbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) *cookie = rbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) have_rbuf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /* Have we used it up entirely? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (offset >= rbuf->count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) goto done_with_rbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) /* More data to go, but can't copy any more? EOVERFLOW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) ret = -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (!nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) goto done_with_rbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /* Copy as much data as possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) ret = rbuf->count - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (ret > nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) ret = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) memcpy(kbuf, rbuf->buf+offset, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) offset += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) /* If we still have data left, we leave the rbuf in the cookie */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (offset < rbuf->count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) done_with_rbuf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) *cookie = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) kfree(rbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) } /* end of n_hdlc_tty_read() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) * n_hdlc_tty_write - write a single frame of data to device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) * @tty: pointer to associated tty device instance data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) * @file: pointer to file object data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) * @data: pointer to transmit data (one frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * @count: size of transmit frame in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * Returns the number of bytes written (or error code).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) const unsigned char *data, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) struct n_hdlc *n_hdlc = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) struct n_hdlc_buf *tbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) pr_debug("%s() called count=%zd\n", __func__, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (n_hdlc->magic != HDLC_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) /* verify frame size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (count > maxframe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) pr_debug("%s: truncating user packet from %zu to %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) __func__, count, maxframe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) count = maxframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) add_wait_queue(&tty->write_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) if (tbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) if (tty_io_nonblock(tty, file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) error = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) error = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) remove_wait_queue(&tty->write_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) /* Retrieve the user's buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) memcpy(tbuf->buf, data, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) /* Send the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) tbuf->count = error = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) n_hdlc_buf_put(&n_hdlc->tx_buf_list, tbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) n_hdlc_send_frames(n_hdlc, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) } /* end of n_hdlc_tty_write() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * @tty: pointer to tty instance data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) * @file: pointer to open file object for device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) * @cmd: IOCTL command code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) * @arg: argument for IOCTL call (cmd dependent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) * Returns command dependent result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) struct n_hdlc *n_hdlc = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) struct n_hdlc_buf *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) pr_debug("%s() called %d\n", __func__, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) /* Verify the status of the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) if (n_hdlc->magic != HDLC_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) case FIONREAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) /* report count of read data available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) /* in next available frame (if any) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) struct n_hdlc_buf, list_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) count = buf->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) error = put_user(count, (int __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) case TIOCOUTQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) /* get the pending tx byte count in the driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) count = tty_chars_in_buffer(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) /* add size of next output frame in queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) struct n_hdlc_buf, list_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) count += buf->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) error = put_user(count, (int __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) case TCFLSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) switch (arg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) case TCIOFLUSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) case TCOFLUSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) flush_tx_queue(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) fallthrough; /* to default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) error = n_tty_ioctl_helper(tty, file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) } /* end of n_hdlc_tty_ioctl() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) * n_hdlc_tty_poll - TTY callback for poll system call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * @tty: pointer to tty instance data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * @filp: pointer to open file object for device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * @wait: wait queue for operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * Determine which operations (read/write) will not block and return info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * to caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * Returns a bit mask containing info on which ops will not block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) struct n_hdlc *n_hdlc = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) __poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (n_hdlc->magic != HDLC_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) * queue the current process into any wait queue that may awaken in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) * future (read and write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) poll_wait(filp, &tty->read_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) poll_wait(filp, &tty->write_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) /* set bits for operations that won't block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (!list_empty(&n_hdlc->rx_buf_list.list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) mask |= EPOLLIN | EPOLLRDNORM; /* readable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) mask |= EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) if (tty_hung_up_p(filp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) mask |= EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (!tty_is_writelocked(tty) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) !list_empty(&n_hdlc->tx_free_buf_list.list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) mask |= EPOLLOUT | EPOLLWRNORM; /* writable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) } /* end of n_hdlc_tty_poll() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) static void n_hdlc_alloc_buf(struct n_hdlc_buf_list *list, unsigned int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) struct n_hdlc_buf *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) buf = kmalloc(struct_size(buf, buf, maxframe), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) pr_debug("%s(), kmalloc() failed for %s buffer %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) __func__, name, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) n_hdlc_buf_put(list, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) * n_hdlc_alloc - allocate an n_hdlc instance data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * Returns a pointer to newly created structure if success, otherwise %NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) static struct n_hdlc *n_hdlc_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) if (!n_hdlc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) spin_lock_init(&n_hdlc->rx_free_buf_list.spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) spin_lock_init(&n_hdlc->tx_free_buf_list.spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) spin_lock_init(&n_hdlc->rx_buf_list.spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) spin_lock_init(&n_hdlc->tx_buf_list.spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) INIT_LIST_HEAD(&n_hdlc->rx_free_buf_list.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) INIT_LIST_HEAD(&n_hdlc->tx_free_buf_list.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) n_hdlc_alloc_buf(&n_hdlc->rx_free_buf_list, DEFAULT_RX_BUF_COUNT, "rx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) n_hdlc_alloc_buf(&n_hdlc->tx_free_buf_list, DEFAULT_TX_BUF_COUNT, "tx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) /* Initialize the control block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) n_hdlc->magic = HDLC_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) return n_hdlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) } /* end of n_hdlc_alloc() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) * n_hdlc_buf_return - put the HDLC buffer after the head of the specified list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) * @buf_list: pointer to the buffer list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) * @buf: pointer to the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) struct n_hdlc_buf *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) spin_lock_irqsave(&buf_list->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) list_add(&buf->list_item, &buf_list->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) buf_list->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) spin_unlock_irqrestore(&buf_list->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * @buf_list: pointer to buffer list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * @buf: pointer to buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct n_hdlc_buf *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) spin_lock_irqsave(&buf_list->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) list_add_tail(&buf->list_item, &buf_list->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) buf_list->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) spin_unlock_irqrestore(&buf_list->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) } /* end of n_hdlc_buf_put() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * n_hdlc_buf_get - remove and return an HDLC buffer from list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) * @buf_list: pointer to HDLC buffer list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) * Remove and return an HDLC buffer from the head of the specified HDLC buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) * Returns a pointer to HDLC buffer if available, otherwise %NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) struct n_hdlc_buf *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) spin_lock_irqsave(&buf_list->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) buf = list_first_entry_or_null(&buf_list->list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) struct n_hdlc_buf, list_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) if (buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) list_del(&buf->list_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) buf_list->count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) spin_unlock_irqrestore(&buf_list->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) } /* end of n_hdlc_buf_get() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) static struct tty_ldisc_ops n_hdlc_ldisc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) .magic = TTY_LDISC_MAGIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) .name = "hdlc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) .open = n_hdlc_tty_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) .close = n_hdlc_tty_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) .read = n_hdlc_tty_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) .write = n_hdlc_tty_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) .ioctl = n_hdlc_tty_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) .poll = n_hdlc_tty_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) .receive_buf = n_hdlc_tty_receive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) .write_wakeup = n_hdlc_tty_wakeup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) .flush_buffer = flush_rx_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) static int __init n_hdlc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) /* range check maxframe arg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) maxframe = clamp(maxframe, 4096, MAX_HDLC_FRAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (!status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) pr_info("N_HDLC line discipline registered with maxframe=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) maxframe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) pr_err("N_HDLC: error registering line discipline: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) } /* end of init_module() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) static void __exit n_hdlc_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) /* Release tty registration of line discipline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) int status = tty_unregister_ldisc(N_HDLC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) pr_err("N_HDLC: can't unregister line discipline (err = %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) pr_info("N_HDLC: line discipline unregistered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) module_init(n_hdlc_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) module_exit(n_hdlc_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) module_param(maxframe, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) MODULE_ALIAS_LDISC(N_HDLC);