diff options
Diffstat (limited to 'drivers/tty/n_hdlc.c')
-rw-r--r-- | drivers/tty/n_hdlc.c | 549 |
1 files changed, 188 insertions, 361 deletions
diff --git a/drivers/tty/n_hdlc.c b/drivers/tty/n_hdlc.c index 27b506bf03ce..991f49ee4026 100644 --- a/drivers/tty/n_hdlc.c +++ b/drivers/tty/n_hdlc.c @@ -18,7 +18,7 @@ * All HDLC data is frame oriented which means: * * 1. tty write calls represent one complete transmit frame of data - * The device driver should accept the complete frame or none of + * The device driver should accept the complete frame or none of * the frame (busy) in the write method. Each write call should have * a byte count in the range of 2-65535 bytes (2 is min HDLC frame * with 1 addr byte and 1 ctrl byte). The max byte count of 65535 @@ -39,7 +39,7 @@ * tty read calls. * * 3. tty read calls returns an entire frame of data or nothing. - * + * * 4. all send and receive data is considered raw. No processing * or translation is performed by the line discipline, regardless * of the tty flags @@ -87,9 +87,6 @@ #include <linux/interrupt.h> #include <linux/ptrace.h> -#undef VERSION -#define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch)) - #include <linux/poll.h> #include <linux/in.h> #include <linux/ioctl.h> @@ -107,7 +104,7 @@ /* * Buffers for individual HDLC frames */ -#define MAX_HDLC_FRAME_SIZE 65535 +#define MAX_HDLC_FRAME_SIZE 65535 #define DEFAULT_RX_BUF_COUNT 10 #define MAX_RX_BUF_COUNT 60 #define DEFAULT_TX_BUF_COUNT 3 @@ -127,11 +124,8 @@ struct n_hdlc_buf_list { /** * struct n_hdlc - per device instance data structure * @magic - magic value for structure - * @flags - miscellaneous control flags - * @tty - ptr to TTY structure - * @backup_tty - TTY to use if tty gets closed * @tbusy - reentrancy flag for tx wakeup code - * @woke_up - FIXME: describe this field + * @woke_up - tx wakeup needs to be run again as it was called while @tbusy * @tx_buf_list - list of pending transmit frame buffers * @rx_buf_list - list of received frame buffers * @tx_free_buf_list - list unused transmit frame buffers @@ -139,11 +133,8 @@ struct n_hdlc_buf_list { */ struct n_hdlc { int magic; - __u32 flags; - struct tty_struct *tty; - struct tty_struct *backup_tty; - int tbusy; - int woke_up; + bool tbusy; + bool woke_up; struct n_hdlc_buf_list tx_buf_list; struct n_hdlc_buf_list rx_buf_list; struct n_hdlc_buf_list tx_free_buf_list; @@ -161,39 +152,14 @@ static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list); /* Local functions */ -static struct n_hdlc *n_hdlc_alloc (void); - -/* debug level can be set by insmod for debugging purposes */ -#define DEBUG_LEVEL_INFO 1 -static int debuglevel; +static struct n_hdlc *n_hdlc_alloc(void); /* max frame size for memory allocations */ static int maxframe = 4096; -/* TTY callbacks */ - -static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, - __u8 __user *buf, size_t nr); -static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, - const unsigned char *buf, size_t nr); -static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file, - unsigned int cmd, unsigned long arg); -static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp, - poll_table *wait); -static int n_hdlc_tty_open(struct tty_struct *tty); -static void n_hdlc_tty_close(struct tty_struct *tty); -static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp, - char *fp, int count); -static void n_hdlc_tty_wakeup(struct tty_struct *tty); - -#define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f))) - -#define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data)) -#define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty) - static void flush_rx_queue(struct tty_struct *tty) { - struct n_hdlc *n_hdlc = tty2n_hdlc(tty); + struct n_hdlc *n_hdlc = tty->disc_data; struct n_hdlc_buf *buf; while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list))) @@ -202,79 +168,22 @@ static void flush_rx_queue(struct tty_struct *tty) static void flush_tx_queue(struct tty_struct *tty) { - struct n_hdlc *n_hdlc = tty2n_hdlc(tty); + struct n_hdlc *n_hdlc = tty->disc_data; struct n_hdlc_buf *buf; while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list))) n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf); } -static struct tty_ldisc_ops n_hdlc_ldisc = { - .owner = THIS_MODULE, - .magic = TTY_LDISC_MAGIC, - .name = "hdlc", - .open = n_hdlc_tty_open, - .close = n_hdlc_tty_close, - .read = n_hdlc_tty_read, - .write = n_hdlc_tty_write, - .ioctl = n_hdlc_tty_ioctl, - .poll = n_hdlc_tty_poll, - .receive_buf = n_hdlc_tty_receive, - .write_wakeup = n_hdlc_tty_wakeup, - .flush_buffer = flush_rx_queue, -}; - -/** - * n_hdlc_release - release an n_hdlc per device line discipline info structure - * @n_hdlc - per device line discipline info structure - */ -static void n_hdlc_release(struct n_hdlc *n_hdlc) +static void n_hdlc_free_buf_list(struct n_hdlc_buf_list *list) { - struct tty_struct *tty = n_hdlc2tty (n_hdlc); struct n_hdlc_buf *buf; - - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__); - - /* Ensure that the n_hdlcd process is not hanging on select()/poll() */ - wake_up_interruptible (&tty->read_wait); - wake_up_interruptible (&tty->write_wait); - - if (tty->disc_data == n_hdlc) - tty->disc_data = NULL; /* Break the tty->n_hdlc link */ - - /* Release transmit and receive buffers */ - for(;;) { - buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list); - if (buf) { - kfree(buf); - } else - break; - } - for(;;) { - buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list); - if (buf) { - kfree(buf); - } else - break; - } - for(;;) { - buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list); - if (buf) { - kfree(buf); - } else - break; - } - for(;;) { - buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); - if (buf) { - kfree(buf); - } else - break; - } - kfree(n_hdlc); - -} /* end of n_hdlc_release() */ + + do { + buf = n_hdlc_buf_get(list); + kfree(buf); + } while (buf); +} /** * n_hdlc_tty_close - line discipline close @@ -285,34 +194,26 @@ static void n_hdlc_release(struct n_hdlc *n_hdlc) */ static void n_hdlc_tty_close(struct tty_struct *tty) { - struct n_hdlc *n_hdlc = tty2n_hdlc (tty); - - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__); - - if (n_hdlc != NULL) { - if (n_hdlc->magic != HDLC_MAGIC) { - printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n"); - return; - } + struct n_hdlc *n_hdlc = tty->disc_data; + + if (n_hdlc->magic != HDLC_MAGIC) { + pr_warn("n_hdlc: trying to close unopened tty!\n"); + return; + } #if defined(TTY_NO_WRITE_SPLIT) - clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags); + clear_bit(TTY_NO_WRITE_SPLIT, &tty->flags); #endif - tty->disc_data = NULL; - if (tty == n_hdlc->backup_tty) - n_hdlc->backup_tty = NULL; - if (tty != n_hdlc->tty) - return; - if (n_hdlc->backup_tty) { - n_hdlc->tty = n_hdlc->backup_tty; - } else { - n_hdlc_release (n_hdlc); - } - } - - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__); - + tty->disc_data = NULL; + + /* Ensure that the n_hdlcd process is not hanging on select()/poll() */ + wake_up_interruptible(&tty->read_wait); + wake_up_interruptible(&tty->write_wait); + + n_hdlc_free_buf_list(&n_hdlc->rx_free_buf_list); + n_hdlc_free_buf_list(&n_hdlc->tx_free_buf_list); + n_hdlc_free_buf_list(&n_hdlc->rx_buf_list); + n_hdlc_free_buf_list(&n_hdlc->tx_buf_list); + kfree(n_hdlc); } /* end of n_hdlc_tty_close() */ /** @@ -321,44 +222,35 @@ static void n_hdlc_tty_close(struct tty_struct *tty) * * Returns 0 if success, otherwise error code */ -static int n_hdlc_tty_open (struct tty_struct *tty) +static int n_hdlc_tty_open(struct tty_struct *tty) { - struct n_hdlc *n_hdlc = tty2n_hdlc (tty); + struct n_hdlc *n_hdlc = tty->disc_data; + + pr_debug("%s() called (device=%s)\n", __func__, tty->name); - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n", - __FILE__,__LINE__, - tty->name); - /* There should not be an existing table for this slot. */ if (n_hdlc) { - printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" ); + pr_err("%s: tty already associated!\n", __func__); return -EEXIST; } - + n_hdlc = n_hdlc_alloc(); if (!n_hdlc) { - printk (KERN_ERR "n_hdlc_alloc failed\n"); + pr_err("%s: n_hdlc_alloc failed\n", __func__); return -ENFILE; } - + tty->disc_data = n_hdlc; - n_hdlc->tty = tty; tty->receive_room = 65536; - -#if defined(TTY_NO_WRITE_SPLIT) + /* change tty_io write() to not split large writes into 8K chunks */ - set_bit(TTY_NO_WRITE_SPLIT,&tty->flags); -#endif - + set_bit(TTY_NO_WRITE_SPLIT, &tty->flags); + /* flush receive data from driver */ tty_driver_flush_buffer(tty); - - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__); - + return 0; - + } /* end of n_tty_hdlc_open() */ /** @@ -376,26 +268,22 @@ static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty) unsigned long flags; struct n_hdlc_buf *tbuf; - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__); - check_again: - - spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); +check_again: + + spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); if (n_hdlc->tbusy) { - n_hdlc->woke_up = 1; - spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); + n_hdlc->woke_up = true; + spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); return; } - n_hdlc->tbusy = 1; - n_hdlc->woke_up = 0; + n_hdlc->tbusy = true; + n_hdlc->woke_up = false; spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); while (tbuf) { - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)sending frame %p, count=%d\n", - __FILE__,__LINE__,tbuf,tbuf->count); - + pr_debug("sending frame %p, count=%d\n", tbuf, tbuf->count); + /* Send the next block of data to device */ set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); actual = tty->ops->write(tty, tbuf->buf, tbuf->count); @@ -409,24 +297,20 @@ static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty) /* pretending it was accepted by driver */ if (actual < 0) actual = tbuf->count; - + if (actual == tbuf->count) { - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)frame %p completed\n", - __FILE__,__LINE__,tbuf); - + pr_debug("frame %p completed\n", tbuf); + /* free current transmit buffer */ n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf); /* wait up sleeping writers */ wake_up_interruptible(&tty->write_wait); - + /* get next pending transmit buffer */ tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); } else { - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)frame %p pending\n", - __FILE__,__LINE__,tbuf); + pr_debug("frame %p pending\n", tbuf); /* * the buffer was not accepted by driver, @@ -436,21 +320,17 @@ static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty) break; } } - + if (!tbuf) clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); - + /* Clear the re-entry flag */ spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); - n_hdlc->tbusy = 0; - spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); - - if (n_hdlc->woke_up) - goto check_again; - - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__); - + n_hdlc->tbusy = false; + spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); + + if (n_hdlc->woke_up) + goto check_again; } /* end of n_hdlc_send_frames() */ /** @@ -461,21 +341,9 @@ static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty) */ static void n_hdlc_tty_wakeup(struct tty_struct *tty) { - struct n_hdlc *n_hdlc = tty2n_hdlc(tty); + struct n_hdlc *n_hdlc = tty->disc_data; - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__); - - if (!n_hdlc) - return; - - if (tty != n_hdlc->tty) { - clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); - return; - } - - n_hdlc_send_frames (n_hdlc, tty); - + n_hdlc_send_frames(n_hdlc, tty); } /* end of n_hdlc_tty_wakeup() */ /** @@ -491,59 +359,50 @@ static void n_hdlc_tty_wakeup(struct tty_struct *tty) static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data, char *flags, int count) { - register struct n_hdlc *n_hdlc = tty2n_hdlc (tty); + register struct n_hdlc *n_hdlc = tty->disc_data; register struct n_hdlc_buf *buf; - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_tty_receive() called count=%d\n", - __FILE__,__LINE__, count); - - /* This can happen if stuff comes in on the backup tty */ - if (!n_hdlc || tty != n_hdlc->tty) - return; - + pr_debug("%s() called count=%d\n", __func__, count); + /* verify line is using HDLC discipline */ if (n_hdlc->magic != HDLC_MAGIC) { - printk("%s(%d) line not using HDLC discipline\n", - __FILE__,__LINE__); + pr_err("line not using HDLC discipline\n"); return; } - - if ( count>maxframe ) { - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d) rx count>maxframesize, data discarded\n", - __FILE__,__LINE__); + + if (count > maxframe) { + pr_debug("rx count>maxframesize, data discarded\n"); return; } - /* get a free HDLC buffer */ + /* get a free HDLC buffer */ buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list); if (!buf) { - /* no buffers in free list, attempt to allocate another rx buffer */ - /* unless the maximum count has been reached */ + /* + * no buffers in free list, attempt to allocate another rx + * buffer unless the maximum count has been reached + */ if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT) buf = kmalloc(struct_size(buf, buf, maxframe), GFP_ATOMIC); } - + if (!buf) { - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d) no more rx buffers, data discarded\n", - __FILE__,__LINE__); + pr_debug("no more rx buffers, data discarded\n"); return; } - + /* copy received data to HDLC buffer */ - memcpy(buf->buf,data,count); - buf->count=count; + memcpy(buf->buf, data, count); + buf->count = count; /* add HDLC buffer to list of received frames */ n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf); - + /* wake up any blocked reads and perform async signalling */ - wake_up_interruptible (&tty->read_wait); - if (n_hdlc->tty->fasync != NULL) - kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN); + wake_up_interruptible(&tty->read_wait); + if (tty->fasync != NULL) + kill_fasync(&tty->fasync, SIGIO, POLL_IN); } /* end of n_hdlc_tty_receive() */ @@ -553,28 +412,21 @@ static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data, * @file - pointer to open file object * @buf - pointer to returned data buffer * @nr - size of returned data buffer - * + * * Returns the number of bytes returned or error code. */ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, __u8 __user *buf, size_t nr) { - struct n_hdlc *n_hdlc = tty2n_hdlc(tty); + struct n_hdlc *n_hdlc = tty->disc_data; int ret = 0; struct n_hdlc_buf *rbuf; DECLARE_WAITQUEUE(wait, current); - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__); - - /* Validate the pointers */ - if (!n_hdlc) - return -EIO; - /* verify user access to buffer */ if (!access_ok(buf, nr)) { - printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user " - "buffer\n", __FILE__, __LINE__); + pr_warn("%s(%d) %s() can't verify user buffer\n", + __FILE__, __LINE__, __func__); return -EFAULT; } @@ -610,7 +462,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf); break; } - + /* no data */ if (tty_io_nonblock(tty, file)) { ret = -EAGAIN; @@ -629,7 +481,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, __set_current_state(TASK_RUNNING); return ret; - + } /* end of n_hdlc_tty_read() */ /** @@ -638,43 +490,34 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, * @file - pointer to file object data * @data - pointer to transmit data (one frame) * @count - size of transmit frame in bytes - * + * * Returns the number of bytes written (or error code). */ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, const unsigned char *data, size_t count) { - struct n_hdlc *n_hdlc = tty2n_hdlc (tty); + struct n_hdlc *n_hdlc = tty->disc_data; int error = 0; DECLARE_WAITQUEUE(wait, current); struct n_hdlc_buf *tbuf; - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_tty_write() called count=%zd\n", - __FILE__,__LINE__,count); - - /* Verify pointers */ - if (!n_hdlc) - return -EIO; + pr_debug("%s() called count=%zd\n", __func__, count); if (n_hdlc->magic != HDLC_MAGIC) return -EIO; /* verify frame size */ - if (count > maxframe ) { - if (debuglevel & DEBUG_LEVEL_INFO) - printk (KERN_WARNING - "n_hdlc_tty_write: truncating user packet " - "from %lu to %d\n", (unsigned long) count, - maxframe ); + if (count > maxframe) { + pr_debug("%s: truncating user packet from %zu to %d\n", + __func__, count, maxframe); count = maxframe; } - + add_wait_queue(&tty->write_wait, &wait); for (;;) { set_current_state(TASK_INTERRUPTIBLE); - + tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list); if (tbuf) break; @@ -684,15 +527,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, break; } schedule(); - - n_hdlc = tty2n_hdlc (tty); - if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC || - tty != n_hdlc->tty) { - printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc); - error = -EIO; - break; - } - + if (signal_pending(current)) { error = -EINTR; break; @@ -702,18 +537,18 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, __set_current_state(TASK_RUNNING); remove_wait_queue(&tty->write_wait, &wait); - if (!error) { + if (!error) { /* Retrieve the user's buffer */ memcpy(tbuf->buf, data, count); /* Send the data */ tbuf->count = error = count; - n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf); - n_hdlc_send_frames(n_hdlc,tty); + n_hdlc_buf_put(&n_hdlc->tx_buf_list, tbuf); + n_hdlc_send_frames(n_hdlc, tty); } return error; - + } /* end of n_hdlc_tty_write() */ /** @@ -728,32 +563,30 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) { - struct n_hdlc *n_hdlc = tty2n_hdlc (tty); + struct n_hdlc *n_hdlc = tty->disc_data; int error = 0; int count; unsigned long flags; struct n_hdlc_buf *buf = NULL; - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_tty_ioctl() called %d\n", - __FILE__,__LINE__,cmd); - + pr_debug("%s() called %d\n", __func__, cmd); + /* Verify the status of the device */ - if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC) + if (n_hdlc->magic != HDLC_MAGIC) return -EBADF; switch (cmd) { case FIONREAD: /* report count of read data available */ /* in next available frame (if any) */ - spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags); + spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock, flags); buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list, struct n_hdlc_buf, list_item); if (buf) count = buf->count; else count = 0; - spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags); + spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock, flags); error = put_user(count, (int __user *)arg); break; @@ -761,12 +594,12 @@ static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file, /* get the pending tx byte count in the driver */ count = tty_chars_in_buffer(tty); /* add size of next output frame in queue */ - spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags); + spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list, struct n_hdlc_buf, list_item); if (buf) count += buf->count; - spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags); + spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); error = put_user(count, (int __user *)arg); break; @@ -783,7 +616,7 @@ static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file, break; } return error; - + } /* end of n_hdlc_tty_ioctl() */ /** @@ -791,7 +624,7 @@ static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file, * @tty - pointer to tty instance data * @filp - pointer to open file object for device * @poll_table - wait queue for operations - * + * * Determine which operations (read/write) will not block and return info * to caller. * Returns a bit mask containing info on which ops will not block. @@ -799,33 +632,50 @@ static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file, static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp, poll_table *wait) { - struct n_hdlc *n_hdlc = tty2n_hdlc (tty); + struct n_hdlc *n_hdlc = tty->disc_data; __poll_t mask = 0; - if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__); - - if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) { - /* queue current process into any wait queue that */ - /* may awaken in the future (read and write) */ - - poll_wait(filp, &tty->read_wait, wait); - poll_wait(filp, &tty->write_wait, wait); - - /* set bits for operations that won't block */ - if (!list_empty(&n_hdlc->rx_buf_list.list)) - mask |= EPOLLIN | EPOLLRDNORM; /* readable */ - if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) - mask |= EPOLLHUP; - if (tty_hung_up_p(filp)) - mask |= EPOLLHUP; - if (!tty_is_writelocked(tty) && - !list_empty(&n_hdlc->tx_free_buf_list.list)) - mask |= EPOLLOUT | EPOLLWRNORM; /* writable */ - } + if (n_hdlc->magic != HDLC_MAGIC) + return 0; + + /* + * queue the current process into any wait queue that may awaken in the + * future (read and write) + */ + poll_wait(filp, &tty->read_wait, wait); + poll_wait(filp, &tty->write_wait, wait); + + /* set bits for operations that won't block */ + if (!list_empty(&n_hdlc->rx_buf_list.list)) + mask |= EPOLLIN | EPOLLRDNORM; /* readable */ + if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) + mask |= EPOLLHUP; + if (tty_hung_up_p(filp)) + mask |= EPOLLHUP; + if (!tty_is_writelocked(tty) && + !list_empty(&n_hdlc->tx_free_buf_list.list)) + mask |= EPOLLOUT | EPOLLWRNORM; /* writable */ + return mask; } /* end of n_hdlc_tty_poll() */ +static void n_hdlc_alloc_buf(struct n_hdlc_buf_list *list, unsigned int count, + const char *name) +{ + struct n_hdlc_buf *buf; + unsigned int i; + + for (i = 0; i < count; i++) { + buf = kmalloc(struct_size(buf, buf, maxframe), GFP_KERNEL); + if (!buf) { + pr_debug("%s(), kmalloc() failed for %s buffer %u\n", + __func__, name, i); + return; + } + n_hdlc_buf_put(list, buf); + } +} + /** * n_hdlc_alloc - allocate an n_hdlc instance data structure * @@ -833,8 +683,6 @@ static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp, */ static struct n_hdlc *n_hdlc_alloc(void) { - struct n_hdlc_buf *buf; - int i; struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL); if (!n_hdlc) @@ -850,30 +698,14 @@ static struct n_hdlc *n_hdlc_alloc(void) INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list); INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list); - /* allocate free rx buffer list */ - for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) { - buf = kmalloc(struct_size(buf, buf, maxframe), GFP_KERNEL); - if (buf) - n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf); - else if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i); - } - - /* allocate free tx buffer list */ - for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) { - buf = kmalloc(struct_size(buf, buf, maxframe), GFP_KERNEL); - if (buf) - n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf); - else if (debuglevel >= DEBUG_LEVEL_INFO) - printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i); - } - + n_hdlc_alloc_buf(&n_hdlc->rx_free_buf_list, DEFAULT_RX_BUF_COUNT, "rx"); + n_hdlc_alloc_buf(&n_hdlc->tx_free_buf_list, DEFAULT_TX_BUF_COUNT, "tx"); + /* Initialize the control block */ n_hdlc->magic = HDLC_MAGIC; - n_hdlc->flags = 0; - + return n_hdlc; - + } /* end of n_hdlc_alloc() */ /** @@ -915,7 +747,7 @@ static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list, /** * n_hdlc_buf_get - remove and return an HDLC buffer from list * @buf_list - pointer to HDLC buffer list - * + * * Remove and return an HDLC buffer from the head of the specified HDLC buffer * list. * Returns a pointer to HDLC buffer if available, otherwise %NULL. @@ -938,44 +770,39 @@ static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list) return buf; } /* end of n_hdlc_buf_get() */ -static const char hdlc_banner[] __initconst = - KERN_INFO "HDLC line discipline maxframe=%u\n"; -static const char hdlc_register_ok[] __initconst = - KERN_INFO "N_HDLC line discipline registered.\n"; -static const char hdlc_register_fail[] __initconst = - KERN_ERR "error registering line discipline: %d\n"; +static struct tty_ldisc_ops n_hdlc_ldisc = { + .owner = THIS_MODULE, + .magic = TTY_LDISC_MAGIC, + .name = "hdlc", + .open = n_hdlc_tty_open, + .close = n_hdlc_tty_close, + .read = n_hdlc_tty_read, + .write = n_hdlc_tty_write, + .ioctl = n_hdlc_tty_ioctl, + .poll = n_hdlc_tty_poll, + .receive_buf = n_hdlc_tty_receive, + .write_wakeup = n_hdlc_tty_wakeup, + .flush_buffer = flush_rx_queue, +}; static int __init n_hdlc_init(void) { int status; /* range check maxframe arg */ - if (maxframe < 4096) - maxframe = 4096; - else if (maxframe > 65535) - maxframe = 65535; - - printk(hdlc_banner, maxframe); + maxframe = clamp(maxframe, 4096, MAX_HDLC_FRAME_SIZE); status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc); if (!status) - printk(hdlc_register_ok); + pr_info("N_HDLC line discipline registered with maxframe=%d\n", + maxframe); else - printk(hdlc_register_fail, status); + pr_err("N_HDLC: error registering line discipline: %d\n", + status); return status; - -} /* end of init_module() */ - -#ifdef CONFIG_SPARC -#undef __exitdata -#define __exitdata -#endif -static const char hdlc_unregister_ok[] __exitdata = - KERN_INFO "N_HDLC: line discipline unregistered\n"; -static const char hdlc_unregister_fail[] __exitdata = - KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n"; +} /* end of init_module() */ static void __exit n_hdlc_exit(void) { @@ -983,9 +810,10 @@ static void __exit n_hdlc_exit(void) int status = tty_unregister_ldisc(N_HDLC); if (status) - printk(hdlc_unregister_fail, status); + pr_err("N_HDLC: can't unregister line discipline (err = %d)\n", + status); else - printk(hdlc_unregister_ok); + pr_info("N_HDLC: line discipline unregistered\n"); } module_init(n_hdlc_init); @@ -993,6 +821,5 @@ module_exit(n_hdlc_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com"); -module_param(debuglevel, int, 0); module_param(maxframe, int, 0); MODULE_ALIAS_LDISC(N_HDLC); |