1 /* generic HDLC line discipline for Linux
3 * Written by Paul Fulghum paulkf@microgate.com
4 * for Microgate Corporation
6 * Microgate and SyncLink are registered trademarks of Microgate Corporation
8 * Adapted from ppp.c, written by Michael Callahan <callahan@maths.ox.ac.uk>,
9 * Al Longyear <longyear@netcom.com>, Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
11 * Original release 01/11/99
12 * $Id: n_hdlc.c,v 3.2 2000/11/06 22:34:38 paul Exp $
14 * This code is released under the GNU General Public License (GPL)
16 * This module implements the tty line discipline N_HDLC for use with
17 * tty device drivers that support bit-synchronous HDLC communications.
19 * All HDLC data is frame oriented which means:
21 * 1. tty write calls represent one complete transmit frame of data
22 * The device driver should accept the complete frame or none of
23 * the frame (busy) in the write method. Each write call should have
24 * a byte count in the range of 2-65535 bytes (2 is min HDLC frame
25 * with 1 addr byte and 1 ctrl byte). The max byte count of 65535
26 * should include any crc bytes required. For example, when using
27 * CCITT CRC32, 4 crc bytes are required, so the maximum size frame
28 * the application may transmit is limited to 65531 bytes. For CCITT
29 * CRC16, the maximum application frame size would be 65533.
32 * 2. receive callbacks from the device driver represents
33 * one received frame. The device driver should bypass
34 * the tty flip buffer and call the line discipline receive
35 * callback directly to avoid fragmenting or concatenating
36 * multiple frames into a single receive callback.
38 * The HDLC line discipline queues the receive frames in seperate
39 * buffers so complete receive frames can be returned by the
42 * 3. tty read calls returns an entire frame of data or nothing.
44 * 4. all send and receive data is considered raw. No processing
45 * or translation is performed by the line discipline, regardless
48 * 5. When line discipline is queried for the amount of receive
49 * data available (FIOC), 0 is returned if no data available,
50 * otherwise the count of the next available frame is returned.
51 * (instead of the sum of all received frame counts).
53 * These conventions allow the standard tty programming interface
54 * to be used for synchronous HDLC applications when used with
55 * this line discipline (or another line discipline that is frame
56 * oriented such as N_PPP).
58 * The SyncLink driver (synclink.c) implements both asynchronous
59 * (using standard line discipline N_TTY) and synchronous HDLC
60 * (using N_HDLC) communications, with the latter using the above
63 * This implementation is very basic and does not maintain
64 * any statistics. The main point is to enforce the raw data
65 * and frame orientation of HDLC communications.
67 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
68 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
70 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
71 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
72 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
73 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
75 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
76 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
77 * OF THE POSSIBILITY OF SUCH DAMAGE.
80 #define HDLC_MAGIC 0x239e
81 #define HDLC_VERSION "3.2"
83 #include <linux/version.h>
84 #include <linux/config.h>
85 #include <linux/module.h>
86 #include <linux/init.h>
87 #include <linux/kernel.h>
88 #include <linux/sched.h>
89 #include <linux/types.h>
90 #include <linux/fcntl.h>
91 #include <linux/interrupt.h>
92 #include <linux/ptrace.h>
95 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
97 #include <linux/poll.h>
99 #include <linux/malloc.h>
100 #include <linux/tty.h>
101 #include <linux/errno.h>
102 #include <linux/string.h> /* used in new tty drivers */
103 #include <linux/signal.h> /* used in new tty drivers */
104 #include <asm/system.h>
105 #include <asm/bitops.h>
106 #include <asm/termios.h>
107 #include <linux/if.h>
109 #include <linux/ioctl.h>
111 #ifdef CONFIG_KERNELD
112 #include <linux/kerneld.h>
115 #include <asm/segment.h>
116 #define GET_USER(error,value,addr) error = get_user(value,addr)
117 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
118 #define PUT_USER(error,value,addr) error = put_user(value,addr)
119 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
121 #include <asm/uaccess.h>
123 typedef ssize_t rw_ret_t;
124 typedef size_t rw_count_t;
127 * Buffers for individual HDLC frames
129 #define MAX_HDLC_FRAME_SIZE 65535
130 #define DEFAULT_RX_BUF_COUNT 10
131 #define MAX_RX_BUF_COUNT 60
132 #define DEFAULT_TX_BUF_COUNT 1
135 typedef struct _n_hdlc_buf
137 struct _n_hdlc_buf *link;
142 #define N_HDLC_BUF_SIZE (sizeof(N_HDLC_BUF)+maxframe)
144 typedef struct _n_hdlc_buf_list
154 * Per device instance data structure
157 int magic; /* magic value for structure */
158 __u32 flags; /* miscellaneous control flags */
160 struct tty_struct *tty; /* ptr to TTY structure */
161 struct tty_struct *backup_tty; /* TTY to use if tty gets closed */
163 /* Queues for select() functionality */
164 wait_queue_head_t read_wait;
165 wait_queue_head_t write_wait;
166 wait_queue_head_t poll_wait;
168 int tbusy; /* reentrancy flag for tx wakeup code */
170 N_HDLC_BUF *tbuf; /* currently transmitting tx buffer */
171 N_HDLC_BUF_LIST tx_buf_list; /* list of pending transmit frame buffers */
172 N_HDLC_BUF_LIST rx_buf_list; /* list of received frame buffers */
173 N_HDLC_BUF_LIST tx_free_buf_list; /* list unused transmit frame buffers */
174 N_HDLC_BUF_LIST rx_free_buf_list; /* list unused received frame buffers */
178 * HDLC buffer list manipulation functions
180 void n_hdlc_buf_list_init(N_HDLC_BUF_LIST *list);
181 void n_hdlc_buf_put(N_HDLC_BUF_LIST *list,N_HDLC_BUF *buf);
182 N_HDLC_BUF* n_hdlc_buf_get(N_HDLC_BUF_LIST *list);
184 /* Local functions */
186 static struct n_hdlc *n_hdlc_alloc (void);
188 MODULE_PARM(debuglevel, "i");
189 MODULE_PARM(maxframe, "i");
192 /* debug level can be set by insmod for debugging purposes */
193 #define DEBUG_LEVEL_INFO 1
196 /* max frame size for memory allocations */
197 ssize_t maxframe=4096;
201 static rw_ret_t n_hdlc_tty_read(struct tty_struct *,
202 struct file *, __u8 *, rw_count_t);
203 static rw_ret_t n_hdlc_tty_write(struct tty_struct *,
204 struct file *, const __u8 *, rw_count_t);
205 static int n_hdlc_tty_ioctl(struct tty_struct *,
206 struct file *, unsigned int, unsigned long);
207 static unsigned int n_hdlc_tty_poll (struct tty_struct *tty, struct file *filp,
209 static int n_hdlc_tty_open (struct tty_struct *);
210 static void n_hdlc_tty_close (struct tty_struct *);
211 static int n_hdlc_tty_room (struct tty_struct *tty);
212 static void n_hdlc_tty_receive (struct tty_struct *tty,
213 const __u8 * cp, char *fp, int count);
214 static void n_hdlc_tty_wakeup (struct tty_struct *tty);
216 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
218 #define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
219 #define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
221 /* Define this string only once for all macro invocations */
222 static char szVersion[] = HDLC_VERSION;
226 * release an n_hdlc per device line discipline info structure
229 static void n_hdlc_release (struct n_hdlc *n_hdlc)
231 struct tty_struct *tty = n_hdlc2tty (n_hdlc);
234 if (debuglevel >= DEBUG_LEVEL_INFO)
235 printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
237 /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
238 wake_up_interruptible (&n_hdlc->read_wait);
239 wake_up_interruptible (&n_hdlc->poll_wait);
240 wake_up_interruptible (&n_hdlc->write_wait);
242 if (tty != NULL && tty->disc_data == n_hdlc)
243 tty->disc_data = NULL; /* Break the tty->n_hdlc link */
245 /* Release transmit and receive buffers */
247 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
254 buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
261 buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
268 buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
277 } /* end of n_hdlc_release() */
279 /* n_hdlc_tty_close()
281 * Called when the line discipline is changed to something
282 * else, the tty is closed, or the tty detects a hangup.
284 static void n_hdlc_tty_close(struct tty_struct *tty)
286 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
288 if (debuglevel >= DEBUG_LEVEL_INFO)
289 printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
291 if (n_hdlc != NULL) {
292 if (n_hdlc->magic != HDLC_MAGIC) {
293 printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
296 #if defined(TTY_NO_WRITE_SPLIT)
297 clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
299 tty->disc_data = NULL;
300 if (tty == n_hdlc->backup_tty)
301 n_hdlc->backup_tty = 0;
302 if (tty != n_hdlc->tty)
304 if (n_hdlc->backup_tty) {
305 n_hdlc->tty = n_hdlc->backup_tty;
307 n_hdlc_release (n_hdlc);
312 if (debuglevel >= DEBUG_LEVEL_INFO)
313 printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
315 } /* end of n_hdlc_tty_close() */
319 * called when line discipline changed to n_hdlc
321 * Arguments: tty pointer to tty info structure
322 * Return Value: 0 if success, otherwise error code
324 static int n_hdlc_tty_open (struct tty_struct *tty)
326 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
328 if (debuglevel >= DEBUG_LEVEL_INFO)
329 printk("%s(%d)n_hdlc_tty_open() called (major=%u,minor=%u)\n",
331 MAJOR(tty->device), MINOR(tty->device));
333 /* There should not be an existing table for this slot. */
335 printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
339 n_hdlc = n_hdlc_alloc();
341 printk (KERN_ERR "n_hdlc_alloc failed\n");
345 tty->disc_data = n_hdlc;
350 #if defined(TTY_NO_WRITE_SPLIT)
351 /* change tty_io write() to not split large writes into 8K chunks */
352 set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
355 /* Flush any pending characters in the driver and discipline. */
357 if (tty->ldisc.flush_buffer)
358 tty->ldisc.flush_buffer (tty);
360 if (tty->driver.flush_buffer)
361 tty->driver.flush_buffer (tty);
363 if (debuglevel >= DEBUG_LEVEL_INFO)
364 printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
368 } /* end of n_tty_hdlc_open() */
370 /* n_hdlc_send_frames()
372 * send frames on pending send buffer list until the
373 * driver does not accept a frame (busy)
374 * this function is called after adding a frame to the
375 * send buffer list and by the tty wakeup callback
377 * Arguments: n_hdlc pointer to ldisc instance data
378 * tty pointer to tty instance data
381 static void n_hdlc_send_frames (struct n_hdlc *n_hdlc, struct tty_struct *tty)
387 if (debuglevel >= DEBUG_LEVEL_INFO)
388 printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
395 restore_flags(flags);
400 restore_flags(flags);
402 /* get current transmit buffer or get new transmit */
403 /* buffer from list of pending transmit buffers */
407 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
410 if (debuglevel >= DEBUG_LEVEL_INFO)
411 printk("%s(%d)sending frame %p, count=%d\n",
412 __FILE__,__LINE__,tbuf,tbuf->count);
414 /* Send the next block of data to device */
415 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
416 actual = tty->driver.write(tty, 0, tbuf->buf, tbuf->count);
418 /* if transmit error, throw frame away by */
419 /* pretending it was accepted by driver */
421 actual = tbuf->count;
423 if (actual == tbuf->count) {
424 if (debuglevel >= DEBUG_LEVEL_INFO)
425 printk("%s(%d)frame %p completed\n",
426 __FILE__,__LINE__,tbuf);
428 /* free current transmit buffer */
429 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,tbuf);
431 /* this tx buffer is done */
434 /* wait up sleeping writers */
435 wake_up_interruptible(&n_hdlc->write_wait);
436 wake_up_interruptible(&n_hdlc->poll_wait);
438 /* get next pending transmit buffer */
439 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
441 if (debuglevel >= DEBUG_LEVEL_INFO)
442 printk("%s(%d)frame %p pending\n",
443 __FILE__,__LINE__,tbuf);
445 /* buffer not accepted by driver */
446 /* set this buffer as pending buffer */
453 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
455 /* Clear the re-entry flag */
459 restore_flags(flags);
464 if (debuglevel >= DEBUG_LEVEL_INFO)
465 printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
467 } /* end of n_hdlc_send_frames() */
469 /* n_hdlc_tty_wakeup()
471 * Callback for transmit wakeup. Called when low level
472 * device driver can accept more send data.
474 * Arguments: tty pointer to associated tty instance data
477 static void n_hdlc_tty_wakeup (struct tty_struct *tty)
479 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
481 if (debuglevel >= DEBUG_LEVEL_INFO)
482 printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
487 if (tty != n_hdlc->tty) {
488 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
492 n_hdlc_send_frames (n_hdlc, tty);
494 } /* end of n_hdlc_tty_wakeup() */
498 * Callback function from tty driver. Return the amount of
499 * space left in the receiver's buffer to decide if remote
500 * transmitter is to be throttled.
502 * Arguments: tty pointer to associated tty instance data
503 * Return Value: number of bytes left in receive buffer
505 static int n_hdlc_tty_room (struct tty_struct *tty)
507 if (debuglevel >= DEBUG_LEVEL_INFO)
508 printk("%s(%d)n_hdlc_tty_room() called\n",__FILE__,__LINE__);
509 /* always return a larger number to prevent */
510 /* throttling of remote transmitter. */
512 } /* end of n_hdlc_tty_root() */
514 /* n_hdlc_tty_receive()
516 * Called by tty low level driver when receive data is
517 * available. Data is interpreted as one HDLC frame.
519 * Arguments: tty pointer to tty isntance data
520 * data pointer to received data
521 * flags pointer to flags for data
522 * count count of received data in bytes
526 static void n_hdlc_tty_receive(struct tty_struct *tty,
527 const __u8 * data, char *flags, int count)
529 register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
530 register N_HDLC_BUF *buf;
532 if (debuglevel >= DEBUG_LEVEL_INFO)
533 printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
534 __FILE__,__LINE__, count);
536 /* This can happen if stuff comes in on the backup tty */
537 if (n_hdlc == 0 || tty != n_hdlc->tty)
540 /* verify line is using HDLC discipline */
541 if (n_hdlc->magic != HDLC_MAGIC) {
542 printk("%s(%d) line not using HDLC discipline\n",
547 if ( count>maxframe ) {
548 if (debuglevel >= DEBUG_LEVEL_INFO)
549 printk("%s(%d) rx count>maxframesize, data discarded\n",
554 /* get a free HDLC buffer */
555 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
557 /* no buffers in free list, attempt to allocate another rx buffer */
558 /* unless the maximum count has been reached */
559 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
560 buf = (N_HDLC_BUF*)kmalloc(N_HDLC_BUF_SIZE,GFP_ATOMIC);
564 if (debuglevel >= DEBUG_LEVEL_INFO)
565 printk("%s(%d) no more rx buffers, data discarded\n",
570 /* copy received data to HDLC buffer */
571 memcpy(buf->buf,data,count);
574 /* add HDLC buffer to list of received frames */
575 n_hdlc_buf_put(&n_hdlc->rx_buf_list,buf);
577 /* wake up any blocked reads and perform async signalling */
578 wake_up_interruptible (&n_hdlc->read_wait);
579 wake_up_interruptible (&n_hdlc->poll_wait);
580 if (n_hdlc->tty->fasync != NULL)
581 kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
583 } /* end of n_hdlc_tty_receive() */
587 * Called to retreive one frame of data (if available)
591 * tty pointer to tty instance data
592 * file pointer to open file object
593 * buf pointer to returned data buffer
594 * nr size of returned data buffer
598 * Number of bytes returned or error code
600 static rw_ret_t n_hdlc_tty_read (struct tty_struct *tty,
601 struct file *file, __u8 * buf, rw_count_t nr)
603 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
608 if (debuglevel >= DEBUG_LEVEL_INFO)
609 printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
611 /* Validate the pointers */
615 /* verify user access to buffer */
616 error = verify_area (VERIFY_WRITE, buf, nr);
618 printk(KERN_WARNING"%s(%d) n_hdlc_tty_read() can't verify user "
619 "buffer\n",__FILE__,__LINE__);
624 n_hdlc = tty2n_hdlc (tty);
625 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
629 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
634 if (file->f_flags & O_NONBLOCK)
637 interruptible_sleep_on (&n_hdlc->read_wait);
638 if (signal_pending(current))
642 if (rbuf->count > nr) {
643 /* frame too large for caller's buffer (discard frame) */
644 ret = (rw_ret_t)-EOVERFLOW;
646 /* Copy the data to the caller's buffer */
647 COPY_TO_USER(error,buf,rbuf->buf,rbuf->count);
649 ret = (rw_ret_t)error;
651 ret = (rw_ret_t)rbuf->count;
654 /* return HDLC buffer to free list unless the free list */
655 /* count has exceeded the default value, in which case the */
656 /* buffer is freed back to the OS to conserve memory */
657 if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
660 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf);
664 } /* end of n_hdlc_tty_read() */
666 /* n_hdlc_tty_write()
668 * write a single frame of data to device
670 * Arguments: tty pointer to associated tty device instance data
671 * file pointer to file object data
672 * data pointer to transmit data (one frame)
673 * count size of transmit frame in bytes
675 * Return Value: number of bytes written (or error code)
677 static rw_ret_t n_hdlc_tty_write (struct tty_struct *tty, struct file *file,
678 const __u8 * data, rw_count_t count)
680 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
682 DECLARE_WAITQUEUE(wait, current);
685 if (debuglevel >= DEBUG_LEVEL_INFO)
686 printk("%s(%d)n_hdlc_tty_write() called count=%d\n",
687 __FILE__,__LINE__,count);
689 /* Verify pointers */
693 if (n_hdlc->magic != HDLC_MAGIC)
696 /* verify frame size */
697 if (count > maxframe ) {
698 if (debuglevel & DEBUG_LEVEL_INFO)
700 "n_hdlc_tty_write: truncating user packet "
701 "from %lu to %d\n", (unsigned long) count,
706 add_wait_queue(&n_hdlc->write_wait, &wait);
707 set_current_state(TASK_INTERRUPTIBLE);
709 /* Allocate transmit buffer */
710 /* sleep until transmit buffer available */
711 while (!(tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list))) {
714 n_hdlc = tty2n_hdlc (tty);
715 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
716 tty != n_hdlc->tty) {
717 printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
722 if (signal_pending(current)) {
728 set_current_state(TASK_RUNNING);
729 remove_wait_queue(&n_hdlc->write_wait, &wait);
732 /* Retrieve the user's buffer */
733 COPY_FROM_USER (error, tbuf->buf, data, count);
735 /* return tx buffer to free list */
736 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,tbuf);
739 tbuf->count = error = count;
740 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
741 n_hdlc_send_frames(n_hdlc,tty);
747 } /* end of n_hdlc_tty_write() */
749 /* n_hdlc_tty_ioctl()
751 * Process IOCTL system call for the tty device.
755 * tty pointer to tty instance data
756 * file pointer to open file object for device
757 * cmd IOCTL command code
758 * arg argument for IOCTL call (cmd dependent)
760 * Return Value: Command dependent
762 static int n_hdlc_tty_ioctl (struct tty_struct *tty, struct file * file,
763 unsigned int cmd, unsigned long arg)
765 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
770 if (debuglevel >= DEBUG_LEVEL_INFO)
771 printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
772 __FILE__,__LINE__,cmd);
774 /* Verify the status of the device */
775 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
780 /* report count of read data available */
781 /* in next available frame (if any) */
782 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
783 if (n_hdlc->rx_buf_list.head)
784 count = n_hdlc->rx_buf_list.head->count;
787 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
788 PUT_USER (error, count, (int *) arg);
792 /* get the pending tx byte count in the driver */
793 count = tty->driver.chars_in_buffer ?
794 tty->driver.chars_in_buffer(tty) : 0;
795 /* add size of next output frame in queue */
796 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
797 if (n_hdlc->tx_buf_list.head)
798 count += n_hdlc->tx_buf_list.head->count;
799 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
800 PUT_USER (error, count, (int*)arg);
804 error = n_tty_ioctl (tty, file, cmd, arg);
809 } /* end of n_hdlc_tty_ioctl() */
813 * TTY callback for poll system call. Determine which
814 * operations (read/write) will not block and return
819 * tty pointer to tty instance data
820 * filp pointer to open file object for device
821 * poll_table wait queue for operations
825 * bit mask containing info on which ops will not block
827 static unsigned int n_hdlc_tty_poll (struct tty_struct *tty,
828 struct file *filp, poll_table * wait)
830 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
831 unsigned int mask = 0;
833 if (debuglevel >= DEBUG_LEVEL_INFO)
834 printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
836 if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
837 /* queue current process into any wait queue that */
838 /* may awaken in the future (read and write) */
839 poll_wait(filp, &n_hdlc->poll_wait, wait);
841 /* set bits for operations that wont block */
842 if(n_hdlc->rx_buf_list.head)
843 mask |= POLLIN | POLLRDNORM; /* readable */
844 if(tty->flags & (1 << TTY_OTHER_CLOSED))
846 if(tty_hung_up_p(filp))
848 if(n_hdlc->tx_free_buf_list.head)
849 mask |= POLLOUT | POLLWRNORM; /* writable */
852 } /* end of n_hdlc_tty_poll() */
856 * Allocate an n_hdlc instance data structure
859 * Return Value: pointer to structure if success, otherwise 0
861 static struct n_hdlc *n_hdlc_alloc (void)
863 struct n_hdlc *n_hdlc;
867 n_hdlc = (struct n_hdlc *)kmalloc(sizeof(struct n_hdlc), GFP_KERNEL);
871 memset(n_hdlc, 0, sizeof(*n_hdlc));
873 n_hdlc_buf_list_init(&n_hdlc->rx_free_buf_list);
874 n_hdlc_buf_list_init(&n_hdlc->tx_free_buf_list);
875 n_hdlc_buf_list_init(&n_hdlc->rx_buf_list);
876 n_hdlc_buf_list_init(&n_hdlc->tx_buf_list);
878 /* allocate free rx buffer list */
879 for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
880 buf = (N_HDLC_BUF*)kmalloc(N_HDLC_BUF_SIZE,GFP_KERNEL);
882 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
883 else if (debuglevel >= DEBUG_LEVEL_INFO)
884 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
887 /* allocate free tx buffer list */
888 for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
889 buf = (N_HDLC_BUF*)kmalloc(N_HDLC_BUF_SIZE,GFP_KERNEL);
891 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
892 else if (debuglevel >= DEBUG_LEVEL_INFO)
893 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
896 /* Initialize the control block */
897 n_hdlc->magic = HDLC_MAGIC;
900 init_waitqueue_head(&n_hdlc->read_wait);
901 init_waitqueue_head(&n_hdlc->poll_wait);
902 init_waitqueue_head(&n_hdlc->write_wait);
906 } /* end of n_hdlc_alloc() */
908 /* n_hdlc_buf_list_init()
910 * initialize specified HDLC buffer list
912 * Arguments: list pointer to buffer list
915 void n_hdlc_buf_list_init(N_HDLC_BUF_LIST *list)
917 memset(list,0,sizeof(N_HDLC_BUF_LIST));
918 spin_lock_init(&list->spinlock);
919 } /* end of n_hdlc_buf_list_init() */
923 * add specified HDLC buffer to tail of specified list
927 * list pointer to buffer list
928 * buf pointer to buffer
932 void n_hdlc_buf_put(N_HDLC_BUF_LIST *list,N_HDLC_BUF *buf)
935 spin_lock_irqsave(&list->spinlock,flags);
939 list->tail->link = buf;
945 spin_unlock_irqrestore(&list->spinlock,flags);
947 } /* end of n_hdlc_buf_put() */
951 * remove and return an HDLC buffer from the
952 * head of the specified HDLC buffer list
956 * list pointer to HDLC buffer list
960 * pointer to HDLC buffer if available, otherwise NULL
962 N_HDLC_BUF* n_hdlc_buf_get(N_HDLC_BUF_LIST *list)
966 spin_lock_irqsave(&list->spinlock,flags);
970 list->head = buf->link;
976 spin_unlock_irqrestore(&list->spinlock,flags);
979 } /* end of n_hdlc_buf_get() */
981 static int __init n_hdlc_init(void)
983 static struct tty_ldisc n_hdlc_ldisc;
986 /* range check maxframe arg */
989 else if ( maxframe>65535)
992 printk("HDLC line discipline: version %s, maxframe=%u\n",
993 szVersion, maxframe);
995 /* Register the tty discipline */
997 memset(&n_hdlc_ldisc, 0, sizeof (n_hdlc_ldisc));
998 n_hdlc_ldisc.magic = TTY_LDISC_MAGIC;
999 n_hdlc_ldisc.name = "hdlc";
1000 n_hdlc_ldisc.open = n_hdlc_tty_open;
1001 n_hdlc_ldisc.close = n_hdlc_tty_close;
1002 n_hdlc_ldisc.read = n_hdlc_tty_read;
1003 n_hdlc_ldisc.write = n_hdlc_tty_write;
1004 n_hdlc_ldisc.ioctl = n_hdlc_tty_ioctl;
1005 n_hdlc_ldisc.poll = n_hdlc_tty_poll;
1006 n_hdlc_ldisc.receive_room = n_hdlc_tty_room;
1007 n_hdlc_ldisc.receive_buf = n_hdlc_tty_receive;
1008 n_hdlc_ldisc.write_wakeup = n_hdlc_tty_wakeup;
1010 status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
1012 printk (KERN_INFO"N_HDLC line discipline registered.\n");
1014 printk (KERN_ERR"error registering line discipline: %d\n",status);
1017 printk(KERN_INFO"N_HDLC: init failure %d\n", status);
1020 } /* end of init_module() */
1022 static void __exit n_hdlc_exit(void)
1025 /* Release tty registration of line discipline */
1026 if ((status = tty_register_ldisc(N_HDLC, NULL)))
1027 printk("N_HDLC: can't unregister line discipline (err = %d)\n", status);
1029 printk("N_HDLC: line discipline unregistered\n");
1032 module_init(n_hdlc_init);
1033 module_exit(n_hdlc_exit);