Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / char / n_hdlc.c
blobac9af7c3e7dcee19c617009ce4a8d89b962a83a3
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>,
10 * Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
12 * Original release 01/11/99
13 * $Id: n_hdlc.c,v 4.8 2003/05/06 21:18:51 paulkf Exp $
15 * This code is released under the GNU General Public License (GPL)
17 * This module implements the tty line discipline N_HDLC for use with
18 * tty device drivers that support bit-synchronous HDLC communications.
20 * All HDLC data is frame oriented which means:
22 * 1. tty write calls represent one complete transmit frame of data
23 * The device driver should accept the complete frame or none of
24 * the frame (busy) in the write method. Each write call should have
25 * a byte count in the range of 2-65535 bytes (2 is min HDLC frame
26 * with 1 addr byte and 1 ctrl byte). The max byte count of 65535
27 * should include any crc bytes required. For example, when using
28 * CCITT CRC32, 4 crc bytes are required, so the maximum size frame
29 * the application may transmit is limited to 65531 bytes. For CCITT
30 * CRC16, the maximum application frame size would be 65533.
33 * 2. receive callbacks from the device driver represents
34 * one received frame. The device driver should bypass
35 * the tty flip buffer and call the line discipline receive
36 * callback directly to avoid fragmenting or concatenating
37 * multiple frames into a single receive callback.
39 * The HDLC line discipline queues the receive frames in separate
40 * buffers so complete receive frames can be returned by the
41 * tty read calls.
43 * 3. tty read calls returns an entire frame of data or nothing.
45 * 4. all send and receive data is considered raw. No processing
46 * or translation is performed by the line discipline, regardless
47 * of the tty flags
49 * 5. When line discipline is queried for the amount of receive
50 * data available (FIOC), 0 is returned if no data available,
51 * otherwise the count of the next available frame is returned.
52 * (instead of the sum of all received frame counts).
54 * These conventions allow the standard tty programming interface
55 * to be used for synchronous HDLC applications when used with
56 * this line discipline (or another line discipline that is frame
57 * oriented such as N_PPP).
59 * The SyncLink driver (synclink.c) implements both asynchronous
60 * (using standard line discipline N_TTY) and synchronous HDLC
61 * (using N_HDLC) communications, with the latter using the above
62 * conventions.
64 * This implementation is very basic and does not maintain
65 * any statistics. The main point is to enforce the raw data
66 * and frame orientation of HDLC communications.
68 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
69 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
70 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
71 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
72 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
73 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
74 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
76 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
77 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
78 * OF THE POSSIBILITY OF SUCH DAMAGE.
81 #define HDLC_MAGIC 0x239e
82 #define HDLC_VERSION "$Revision: 4.8 $"
84 #include <linux/version.h>
85 #include <linux/config.h>
86 #include <linux/module.h>
87 #include <linux/init.h>
88 #include <linux/kernel.h>
89 #include <linux/sched.h>
90 #include <linux/types.h>
91 #include <linux/fcntl.h>
92 #include <linux/interrupt.h>
93 #include <linux/ptrace.h>
95 #undef VERSION
96 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
98 #include <linux/poll.h>
99 #include <linux/in.h>
100 #include <linux/ioctl.h>
101 #include <linux/slab.h>
102 #include <linux/tty.h>
103 #include <linux/errno.h>
104 #include <linux/string.h> /* used in new tty drivers */
105 #include <linux/signal.h> /* used in new tty drivers */
106 #include <linux/if.h>
108 #include <asm/system.h>
109 #include <asm/bitops.h>
110 #include <asm/termios.h>
111 #include <asm/uaccess.h>
114 * Buffers for individual HDLC frames
116 #define MAX_HDLC_FRAME_SIZE 65535
117 #define DEFAULT_RX_BUF_COUNT 10
118 #define MAX_RX_BUF_COUNT 60
119 #define DEFAULT_TX_BUF_COUNT 1
121 struct n_hdlc_buf {
122 struct n_hdlc_buf *link;
123 int count;
124 char buf[1];
127 #define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe)
129 struct n_hdlc_buf_list {
130 struct n_hdlc_buf *head;
131 struct n_hdlc_buf *tail;
132 int count;
133 spinlock_t spinlock;
137 * struct n_hdlc - per device instance data structure
138 * @magic - magic value for structure
139 * @flags - miscellaneous control flags
140 * @tty - ptr to TTY structure
141 * @backup_tty - TTY to use if tty gets closed
142 * @tbusy - reentrancy flag for tx wakeup code
143 * @woke_up - FIXME: describe this field
144 * @tbuf - currently transmitting tx buffer
145 * @tx_buf_list - list of pending transmit frame buffers
146 * @rx_buf_list - list of received frame buffers
147 * @tx_free_buf_list - list unused transmit frame buffers
148 * @rx_free_buf_list - list unused received frame buffers
150 struct n_hdlc {
151 int magic;
152 __u32 flags;
153 struct tty_struct *tty;
154 struct tty_struct *backup_tty;
155 int tbusy;
156 int woke_up;
157 struct n_hdlc_buf *tbuf;
158 struct n_hdlc_buf_list tx_buf_list;
159 struct n_hdlc_buf_list rx_buf_list;
160 struct n_hdlc_buf_list tx_free_buf_list;
161 struct n_hdlc_buf_list rx_free_buf_list;
165 * HDLC buffer list manipulation functions
167 static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list);
168 static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
169 struct n_hdlc_buf *buf);
170 static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
172 /* Local functions */
174 static struct n_hdlc *n_hdlc_alloc (void);
176 /* debug level can be set by insmod for debugging purposes */
177 #define DEBUG_LEVEL_INFO 1
178 static int debuglevel;
180 /* max frame size for memory allocations */
181 static ssize_t maxframe = 4096;
183 /* TTY callbacks */
185 static int n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
186 __u8 *buf, size_t nr);
187 static int n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
188 const __u8 *buf, size_t nr);
189 static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
190 unsigned int cmd, unsigned long arg);
191 static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
192 poll_table *wait);
193 static int n_hdlc_tty_open(struct tty_struct *tty);
194 static void n_hdlc_tty_close(struct tty_struct *tty);
195 static int n_hdlc_tty_room(struct tty_struct *tty);
196 static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
197 char *fp, int count);
198 static void n_hdlc_tty_wakeup(struct tty_struct *tty);
200 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
202 #define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
203 #define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
205 static struct tty_ldisc n_hdlc_ldisc = {
206 .owner = THIS_MODULE,
207 .magic = TTY_LDISC_MAGIC,
208 .name = "hdlc",
209 .open = n_hdlc_tty_open,
210 .close = n_hdlc_tty_close,
211 .read = n_hdlc_tty_read,
212 .write = n_hdlc_tty_write,
213 .ioctl = n_hdlc_tty_ioctl,
214 .poll = n_hdlc_tty_poll,
215 .receive_buf = n_hdlc_tty_receive,
216 .receive_room = n_hdlc_tty_room,
217 .write_wakeup = n_hdlc_tty_wakeup,
221 * n_hdlc_release - release an n_hdlc per device line discipline info structure
222 * @n_hdlc - per device line discipline info structure
224 static void n_hdlc_release(struct n_hdlc *n_hdlc)
226 struct tty_struct *tty = n_hdlc2tty (n_hdlc);
227 struct n_hdlc_buf *buf;
229 if (debuglevel >= DEBUG_LEVEL_INFO)
230 printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
232 /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
233 wake_up_interruptible (&tty->read_wait);
234 wake_up_interruptible (&tty->write_wait);
236 if (tty != NULL && tty->disc_data == n_hdlc)
237 tty->disc_data = NULL; /* Break the tty->n_hdlc link */
239 /* Release transmit and receive buffers */
240 for(;;) {
241 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
242 if (buf) {
243 kfree(buf);
244 } else
245 break;
247 for(;;) {
248 buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
249 if (buf) {
250 kfree(buf);
251 } else
252 break;
254 for(;;) {
255 buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
256 if (buf) {
257 kfree(buf);
258 } else
259 break;
261 for(;;) {
262 buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
263 if (buf) {
264 kfree(buf);
265 } else
266 break;
268 if (n_hdlc->tbuf)
269 kfree(n_hdlc->tbuf);
270 kfree(n_hdlc);
272 } /* end of n_hdlc_release() */
275 * n_hdlc_tty_close - line discipline close
276 * @tty - pointer to tty info structure
278 * Called when the line discipline is changed to something
279 * else, the tty is closed, or the tty detects a hangup.
281 static void n_hdlc_tty_close(struct tty_struct *tty)
283 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
285 if (debuglevel >= DEBUG_LEVEL_INFO)
286 printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
288 if (n_hdlc != NULL) {
289 if (n_hdlc->magic != HDLC_MAGIC) {
290 printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
291 return;
293 #if defined(TTY_NO_WRITE_SPLIT)
294 clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
295 #endif
296 tty->disc_data = NULL;
297 if (tty == n_hdlc->backup_tty)
298 n_hdlc->backup_tty = 0;
299 if (tty != n_hdlc->tty)
300 return;
301 if (n_hdlc->backup_tty) {
302 n_hdlc->tty = n_hdlc->backup_tty;
303 } else {
304 n_hdlc_release (n_hdlc);
308 if (debuglevel >= DEBUG_LEVEL_INFO)
309 printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
311 } /* end of n_hdlc_tty_close() */
314 * n_hdlc_tty_open - called when line discipline changed to n_hdlc
315 * @tty - pointer to tty info structure
317 * Returns 0 if success, otherwise error code
319 static int n_hdlc_tty_open (struct tty_struct *tty)
321 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
323 if (debuglevel >= DEBUG_LEVEL_INFO)
324 printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n",
325 __FILE__,__LINE__,
326 tty->name);
328 /* There should not be an existing table for this slot. */
329 if (n_hdlc) {
330 printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
331 return -EEXIST;
334 n_hdlc = n_hdlc_alloc();
335 if (!n_hdlc) {
336 printk (KERN_ERR "n_hdlc_alloc failed\n");
337 return -ENFILE;
340 tty->disc_data = n_hdlc;
341 n_hdlc->tty = tty;
343 #if defined(TTY_NO_WRITE_SPLIT)
344 /* change tty_io write() to not split large writes into 8K chunks */
345 set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
346 #endif
348 /* Flush any pending characters in the driver and discipline. */
350 if (tty->ldisc.flush_buffer)
351 tty->ldisc.flush_buffer (tty);
353 if (tty->driver->flush_buffer)
354 tty->driver->flush_buffer (tty);
356 if (debuglevel >= DEBUG_LEVEL_INFO)
357 printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
359 return 0;
361 } /* end of n_tty_hdlc_open() */
364 * n_hdlc_send_frames - send frames on pending send buffer list
365 * @n_hdlc - pointer to ldisc instance data
366 * @tty - pointer to tty instance data
368 * Send frames on pending send buffer list until the driver does not accept a
369 * frame (busy) this function is called after adding a frame to the send buffer
370 * list and by the tty wakeup callback.
372 static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
374 register int actual;
375 unsigned long flags;
376 struct n_hdlc_buf *tbuf;
378 if (debuglevel >= DEBUG_LEVEL_INFO)
379 printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
380 check_again:
382 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
383 if (n_hdlc->tbusy) {
384 n_hdlc->woke_up = 1;
385 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
386 return;
388 n_hdlc->tbusy = 1;
389 n_hdlc->woke_up = 0;
390 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
392 /* get current transmit buffer or get new transmit */
393 /* buffer from list of pending transmit buffers */
395 tbuf = n_hdlc->tbuf;
396 if (!tbuf)
397 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
399 while (tbuf) {
400 if (debuglevel >= DEBUG_LEVEL_INFO)
401 printk("%s(%d)sending frame %p, count=%d\n",
402 __FILE__,__LINE__,tbuf,tbuf->count);
404 /* Send the next block of data to device */
405 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
406 actual = tty->driver->write(tty, 0, tbuf->buf, tbuf->count);
408 /* if transmit error, throw frame away by */
409 /* pretending it was accepted by driver */
410 if (actual < 0)
411 actual = tbuf->count;
413 if (actual == tbuf->count) {
414 if (debuglevel >= DEBUG_LEVEL_INFO)
415 printk("%s(%d)frame %p completed\n",
416 __FILE__,__LINE__,tbuf);
418 /* free current transmit buffer */
419 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
421 /* this tx buffer is done */
422 n_hdlc->tbuf = NULL;
424 /* wait up sleeping writers */
425 wake_up_interruptible(&tty->write_wait);
427 /* get next pending transmit buffer */
428 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
429 } else {
430 if (debuglevel >= DEBUG_LEVEL_INFO)
431 printk("%s(%d)frame %p pending\n",
432 __FILE__,__LINE__,tbuf);
434 /* buffer not accepted by driver */
435 /* set this buffer as pending buffer */
436 n_hdlc->tbuf = tbuf;
437 break;
441 if (!tbuf)
442 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
444 /* Clear the re-entry flag */
445 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
446 n_hdlc->tbusy = 0;
447 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
449 if (n_hdlc->woke_up)
450 goto check_again;
452 if (debuglevel >= DEBUG_LEVEL_INFO)
453 printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
455 } /* end of n_hdlc_send_frames() */
458 * n_hdlc_tty_wakeup - Callback for transmit wakeup
459 * @tty - pointer to associated tty instance data
461 * Called when low level device driver can accept more send data.
463 static void n_hdlc_tty_wakeup(struct tty_struct *tty)
465 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
467 if (debuglevel >= DEBUG_LEVEL_INFO)
468 printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
470 if (!n_hdlc)
471 return;
473 if (tty != n_hdlc->tty) {
474 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
475 return;
478 n_hdlc_send_frames (n_hdlc, tty);
480 } /* end of n_hdlc_tty_wakeup() */
483 * n_hdlc_tty_room - Return the amount of space left in the receiver's buffer
484 * @tty - pointer to associated tty instance data
486 * Callback function from tty driver. Return the amount of space left in the
487 * receiver's buffer to decide if remote transmitter is to be throttled.
489 static int n_hdlc_tty_room(struct tty_struct *tty)
491 if (debuglevel >= DEBUG_LEVEL_INFO)
492 printk("%s(%d)n_hdlc_tty_room() called\n",__FILE__,__LINE__);
493 /* always return a larger number to prevent */
494 /* throttling of remote transmitter. */
495 return 65536;
496 } /* end of n_hdlc_tty_root() */
499 * n_hdlc_tty_receive - Called by tty driver when receive data is available
500 * @tty - pointer to tty instance data
501 * @data - pointer to received data
502 * @flags - pointer to flags for data
503 * @count - count of received data in bytes
505 * Called by tty low level driver when receive data is available. Data is
506 * interpreted as one HDLC frame.
508 static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
509 char *flags, int count)
511 register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
512 register struct n_hdlc_buf *buf;
514 if (debuglevel >= DEBUG_LEVEL_INFO)
515 printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
516 __FILE__,__LINE__, count);
518 /* This can happen if stuff comes in on the backup tty */
519 if (n_hdlc == 0 || tty != n_hdlc->tty)
520 return;
522 /* verify line is using HDLC discipline */
523 if (n_hdlc->magic != HDLC_MAGIC) {
524 printk("%s(%d) line not using HDLC discipline\n",
525 __FILE__,__LINE__);
526 return;
529 if ( count>maxframe ) {
530 if (debuglevel >= DEBUG_LEVEL_INFO)
531 printk("%s(%d) rx count>maxframesize, data discarded\n",
532 __FILE__,__LINE__);
533 return;
536 /* get a free HDLC buffer */
537 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
538 if (!buf) {
539 /* no buffers in free list, attempt to allocate another rx buffer */
540 /* unless the maximum count has been reached */
541 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
542 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_ATOMIC);
545 if (!buf) {
546 if (debuglevel >= DEBUG_LEVEL_INFO)
547 printk("%s(%d) no more rx buffers, data discarded\n",
548 __FILE__,__LINE__);
549 return;
552 /* copy received data to HDLC buffer */
553 memcpy(buf->buf,data,count);
554 buf->count=count;
556 /* add HDLC buffer to list of received frames */
557 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
559 /* wake up any blocked reads and perform async signalling */
560 wake_up_interruptible (&tty->read_wait);
561 if (n_hdlc->tty->fasync != NULL)
562 kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
564 } /* end of n_hdlc_tty_receive() */
567 * n_hdlc_tty_read - Called to retreive one frame of data (if available)
568 * @tty - pointer to tty instance data
569 * @file - pointer to open file object
570 * @buf - pointer to returned data buffer
571 * @nr - size of returned data buffer
573 * Returns the number of bytes returned or error code.
575 static int n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
576 __u8 *buf, size_t nr)
578 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
579 int error;
580 int ret;
581 struct n_hdlc_buf *rbuf;
583 if (debuglevel >= DEBUG_LEVEL_INFO)
584 printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
586 /* Validate the pointers */
587 if (!n_hdlc)
588 return -EIO;
590 /* verify user access to buffer */
591 error = verify_area (VERIFY_WRITE, buf, nr);
592 if (error != 0) {
593 printk(KERN_WARNING"%s(%d) n_hdlc_tty_read() can't verify user "
594 "buffer\n",__FILE__,__LINE__);
595 return (error);
598 for (;;) {
599 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
600 return -EIO;
602 n_hdlc = tty2n_hdlc (tty);
603 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
604 tty != n_hdlc->tty)
605 return 0;
607 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
608 if (rbuf)
609 break;
611 /* no data */
612 if (file->f_flags & O_NONBLOCK)
613 return -EAGAIN;
615 interruptible_sleep_on (&tty->read_wait);
616 if (signal_pending(current))
617 return -EINTR;
620 if (rbuf->count > nr)
621 /* frame too large for caller's buffer (discard frame) */
622 ret = -EOVERFLOW;
623 else {
624 /* Copy the data to the caller's buffer */
625 if (copy_to_user(buf, rbuf->buf, rbuf->count))
626 ret = -EFAULT;
627 else
628 ret = rbuf->count;
631 /* return HDLC buffer to free list unless the free list */
632 /* count has exceeded the default value, in which case the */
633 /* buffer is freed back to the OS to conserve memory */
634 if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
635 kfree(rbuf);
636 else
637 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf);
639 return ret;
641 } /* end of n_hdlc_tty_read() */
644 * n_hdlc_tty_write - write a single frame of data to device
645 * @tty - pointer to associated tty device instance data
646 * @file - pointer to file object data
647 * @data - pointer to transmit data (one frame)
648 * @count - size of transmit frame in bytes
650 * Returns the number of bytes written (or error code).
652 static int n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
653 const __u8 *data, size_t count)
655 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
656 int error = 0;
657 DECLARE_WAITQUEUE(wait, current);
658 struct n_hdlc_buf *tbuf;
660 if (debuglevel >= DEBUG_LEVEL_INFO)
661 printk("%s(%d)n_hdlc_tty_write() called count=%d\n",
662 __FILE__,__LINE__,count);
664 /* Verify pointers */
665 if (!n_hdlc)
666 return -EIO;
668 if (n_hdlc->magic != HDLC_MAGIC)
669 return -EIO;
671 /* verify frame size */
672 if (count > maxframe ) {
673 if (debuglevel & DEBUG_LEVEL_INFO)
674 printk (KERN_WARNING
675 "n_hdlc_tty_write: truncating user packet "
676 "from %lu to %d\n", (unsigned long) count,
677 maxframe );
678 count = maxframe;
681 add_wait_queue(&tty->write_wait, &wait);
682 set_current_state(TASK_INTERRUPTIBLE);
684 /* Allocate transmit buffer */
685 /* sleep until transmit buffer available */
686 while (!(tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list))) {
687 schedule();
689 n_hdlc = tty2n_hdlc (tty);
690 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
691 tty != n_hdlc->tty) {
692 printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
693 error = -EIO;
694 break;
697 if (signal_pending(current)) {
698 error = -EINTR;
699 break;
703 set_current_state(TASK_RUNNING);
704 remove_wait_queue(&tty->write_wait, &wait);
706 if (!error) {
707 /* Retrieve the user's buffer */
708 if (copy_from_user(tbuf->buf, data, count)) {
709 /* return tx buffer to free list */
710 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,tbuf);
711 error = -EFAULT;
712 } else {
713 /* Send the data */
714 tbuf->count = error = count;
715 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
716 n_hdlc_send_frames(n_hdlc,tty);
720 return error;
722 } /* end of n_hdlc_tty_write() */
725 * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
726 * @tty - pointer to tty instance data
727 * @file - pointer to open file object for device
728 * @cmd - IOCTL command code
729 * @arg - argument for IOCTL call (cmd dependent)
731 * Returns command dependent result.
733 static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
734 unsigned int cmd, unsigned long arg)
736 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
737 int error = 0;
738 int count;
739 unsigned long flags;
741 if (debuglevel >= DEBUG_LEVEL_INFO)
742 printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
743 __FILE__,__LINE__,cmd);
745 /* Verify the status of the device */
746 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
747 return -EBADF;
749 switch (cmd) {
750 case FIONREAD:
751 /* report count of read data available */
752 /* in next available frame (if any) */
753 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
754 if (n_hdlc->rx_buf_list.head)
755 count = n_hdlc->rx_buf_list.head->count;
756 else
757 count = 0;
758 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
759 error = put_user(count, (int *)arg);
760 break;
762 case TIOCOUTQ:
763 /* get the pending tx byte count in the driver */
764 count = tty->driver->chars_in_buffer ?
765 tty->driver->chars_in_buffer(tty) : 0;
766 /* add size of next output frame in queue */
767 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
768 if (n_hdlc->tx_buf_list.head)
769 count += n_hdlc->tx_buf_list.head->count;
770 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
771 error = put_user(count, (int*)arg);
772 break;
774 default:
775 error = n_tty_ioctl (tty, file, cmd, arg);
776 break;
778 return error;
780 } /* end of n_hdlc_tty_ioctl() */
783 * n_hdlc_tty_poll - TTY callback for poll system call
784 * @tty - pointer to tty instance data
785 * @filp - pointer to open file object for device
786 * @poll_table - wait queue for operations
788 * Determine which operations (read/write) will not block and return info
789 * to caller.
790 * Returns a bit mask containing info on which ops will not block.
792 static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
793 poll_table *wait)
795 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
796 unsigned int mask = 0;
798 if (debuglevel >= DEBUG_LEVEL_INFO)
799 printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
801 if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
802 /* queue current process into any wait queue that */
803 /* may awaken in the future (read and write) */
805 poll_wait(filp, &tty->read_wait, wait);
806 poll_wait(filp, &tty->write_wait, wait);
808 /* set bits for operations that won't block */
809 if(n_hdlc->rx_buf_list.head)
810 mask |= POLLIN | POLLRDNORM; /* readable */
811 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
812 mask |= POLLHUP;
813 if(tty_hung_up_p(filp))
814 mask |= POLLHUP;
815 if(n_hdlc->tx_free_buf_list.head)
816 mask |= POLLOUT | POLLWRNORM; /* writable */
818 return mask;
819 } /* end of n_hdlc_tty_poll() */
822 * n_hdlc_alloc - allocate an n_hdlc instance data structure
824 * Returns a pointer to newly created structure if success, otherwise %NULL
826 static struct n_hdlc *n_hdlc_alloc(void)
828 struct n_hdlc_buf *buf;
829 int i;
830 struct n_hdlc *n_hdlc = kmalloc(sizeof(*n_hdlc), GFP_KERNEL);
832 if (!n_hdlc)
833 return 0;
835 memset(n_hdlc, 0, sizeof(*n_hdlc));
837 n_hdlc_buf_list_init(&n_hdlc->rx_free_buf_list);
838 n_hdlc_buf_list_init(&n_hdlc->tx_free_buf_list);
839 n_hdlc_buf_list_init(&n_hdlc->rx_buf_list);
840 n_hdlc_buf_list_init(&n_hdlc->tx_buf_list);
842 /* allocate free rx buffer list */
843 for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
844 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
845 if (buf)
846 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
847 else if (debuglevel >= DEBUG_LEVEL_INFO)
848 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
851 /* allocate free tx buffer list */
852 for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
853 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
854 if (buf)
855 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
856 else if (debuglevel >= DEBUG_LEVEL_INFO)
857 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
860 /* Initialize the control block */
861 n_hdlc->magic = HDLC_MAGIC;
862 n_hdlc->flags = 0;
864 return n_hdlc;
866 } /* end of n_hdlc_alloc() */
869 * n_hdlc_buf_list_init - initialize specified HDLC buffer list
870 * @list - pointer to buffer list
872 static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list)
874 memset(list, 0, sizeof(*list));
875 spin_lock_init(&list->spinlock);
876 } /* end of n_hdlc_buf_list_init() */
879 * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
880 * @list - pointer to buffer list
881 * @buf - pointer to buffer
883 static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
884 struct n_hdlc_buf *buf)
886 unsigned long flags;
887 spin_lock_irqsave(&list->spinlock,flags);
889 buf->link=NULL;
890 if(list->tail)
891 list->tail->link = buf;
892 else
893 list->head = buf;
894 list->tail = buf;
895 (list->count)++;
897 spin_unlock_irqrestore(&list->spinlock,flags);
899 } /* end of n_hdlc_buf_put() */
902 * n_hdlc_buf_get - remove and return an HDLC buffer from list
903 * @list - pointer to HDLC buffer list
905 * Remove and return an HDLC buffer from the head of the specified HDLC buffer
906 * list.
907 * Returns a pointer to HDLC buffer if available, otherwise %NULL.
909 static struct n_hdlc_buf* n_hdlc_buf_get(struct n_hdlc_buf_list *list)
911 unsigned long flags;
912 struct n_hdlc_buf *buf;
913 spin_lock_irqsave(&list->spinlock,flags);
915 buf = list->head;
916 if (buf) {
917 list->head = buf->link;
918 (list->count)--;
920 if (!list->head)
921 list->tail = NULL;
923 spin_unlock_irqrestore(&list->spinlock,flags);
924 return buf;
926 } /* end of n_hdlc_buf_get() */
928 static char hdlc_banner[] __initdata =
929 KERN_INFO "HDLC line discipline: version " HDLC_VERSION
930 ", maxframe=%u\n";
931 static char hdlc_register_ok[] __initdata =
932 KERN_INFO "N_HDLC line discipline registered.\n";
933 static char hdlc_register_fail[] __initdata =
934 KERN_ERR "error registering line discipline: %d\n";
935 static char hdlc_init_fail[] __initdata =
936 KERN_INFO "N_HDLC: init failure %d\n";
938 static int __init n_hdlc_init(void)
940 int status;
942 /* range check maxframe arg */
943 if (maxframe < 4096)
944 maxframe = 4096;
945 else if (maxframe > 65535)
946 maxframe = 65535;
948 printk(hdlc_banner, maxframe);
950 status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
951 if (!status)
952 printk(hdlc_register_ok);
953 else
954 printk(hdlc_register_fail, status);
956 if (status)
957 printk(hdlc_init_fail, status);
958 return status;
960 } /* end of init_module() */
962 static char hdlc_unregister_ok[] __exitdata =
963 KERN_INFO "N_HDLC: line discipline unregistered\n";
964 static char hdlc_unregister_fail[] __exitdata =
965 KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
967 static void __exit n_hdlc_exit(void)
969 /* Release tty registration of line discipline */
970 int status = tty_register_ldisc(N_HDLC, NULL);
972 if (status)
973 printk(hdlc_unregister_fail, status);
974 else
975 printk(hdlc_unregister_ok);
978 module_init(n_hdlc_init);
979 module_exit(n_hdlc_exit);
981 MODULE_LICENSE("GPL");
982 MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
983 MODULE_PARM(debuglevel, "i");
984 MODULE_PARM(maxframe, "i");