blob: 9f54733f16236e9e5618c91967d1d1434ef824ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* generic HDLC line discipline for Linux
2 *
3 * Written by Paul Fulghum paulkf@microgate.com
4 * for Microgate Corporation
5 *
6 * Microgate and SyncLink are registered trademarks of Microgate Corporation
7 *
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>
11 *
12 * Original release 01/11/99
13 * $Id: n_hdlc.c,v 4.8 2003/05/06 21:18:51 paulkf Exp $
14 *
15 * This code is released under the GNU General Public License (GPL)
16 *
17 * This module implements the tty line discipline N_HDLC for use with
18 * tty device drivers that support bit-synchronous HDLC communications.
19 *
20 * All HDLC data is frame oriented which means:
21 *
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.
31 *
32 *
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.
38 *
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.
42 *
43 * 3. tty read calls returns an entire frame of data or nothing.
44 *
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
48 *
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).
53 *
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).
58 *
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.
63 *
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.
67 *
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.
79 */
80
81#define HDLC_MAGIC 0x239e
82#define HDLC_VERSION "$Revision: 4.8 $"
83
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>
93
94#undef VERSION
95#define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
96
97#include <linux/poll.h>
98#include <linux/in.h>
99#include <linux/ioctl.h>
100#include <linux/slab.h>
101#include <linux/tty.h>
102#include <linux/errno.h>
103#include <linux/string.h> /* used in new tty drivers */
104#include <linux/signal.h> /* used in new tty drivers */
105#include <linux/if.h>
106#include <linux/bitops.h>
107
108#include <asm/system.h>
109#include <asm/termios.h>
110#include <asm/uaccess.h>
111
112/*
113 * Buffers for individual HDLC frames
114 */
115#define MAX_HDLC_FRAME_SIZE 65535
116#define DEFAULT_RX_BUF_COUNT 10
117#define MAX_RX_BUF_COUNT 60
118#define DEFAULT_TX_BUF_COUNT 1
119
120struct n_hdlc_buf {
121 struct n_hdlc_buf *link;
122 int count;
123 char buf[1];
124};
125
126#define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe)
127
128struct n_hdlc_buf_list {
129 struct n_hdlc_buf *head;
130 struct n_hdlc_buf *tail;
131 int count;
132 spinlock_t spinlock;
133};
134
135/**
136 * struct n_hdlc - per device instance data structure
137 * @magic - magic value for structure
138 * @flags - miscellaneous control flags
139 * @tty - ptr to TTY structure
140 * @backup_tty - TTY to use if tty gets closed
141 * @tbusy - reentrancy flag for tx wakeup code
142 * @woke_up - FIXME: describe this field
143 * @tbuf - currently transmitting tx buffer
144 * @tx_buf_list - list of pending transmit frame buffers
145 * @rx_buf_list - list of received frame buffers
146 * @tx_free_buf_list - list unused transmit frame buffers
147 * @rx_free_buf_list - list unused received frame buffers
148 */
149struct n_hdlc {
150 int magic;
151 __u32 flags;
152 struct tty_struct *tty;
153 struct tty_struct *backup_tty;
154 int tbusy;
155 int woke_up;
156 struct n_hdlc_buf *tbuf;
157 struct n_hdlc_buf_list tx_buf_list;
158 struct n_hdlc_buf_list rx_buf_list;
159 struct n_hdlc_buf_list tx_free_buf_list;
160 struct n_hdlc_buf_list rx_free_buf_list;
161};
162
163/*
164 * HDLC buffer list manipulation functions
165 */
166static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list);
167static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
168 struct n_hdlc_buf *buf);
169static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
170
171/* Local functions */
172
173static struct n_hdlc *n_hdlc_alloc (void);
174
175/* debug level can be set by insmod for debugging purposes */
176#define DEBUG_LEVEL_INFO 1
177static int debuglevel;
178
179/* max frame size for memory allocations */
180static int maxframe = 4096;
181
182/* TTY callbacks */
183
184static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
185 __u8 __user *buf, size_t nr);
186static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
187 const unsigned char *buf, size_t nr);
188static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
189 unsigned int cmd, unsigned long arg);
190static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
191 poll_table *wait);
192static int n_hdlc_tty_open(struct tty_struct *tty);
193static void n_hdlc_tty_close(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
195 char *fp, int count);
196static void n_hdlc_tty_wakeup(struct tty_struct *tty);
197
198#define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
199
200#define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
201#define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
202
203static struct tty_ldisc n_hdlc_ldisc = {
204 .owner = THIS_MODULE,
205 .magic = TTY_LDISC_MAGIC,
206 .name = "hdlc",
207 .open = n_hdlc_tty_open,
208 .close = n_hdlc_tty_close,
209 .read = n_hdlc_tty_read,
210 .write = n_hdlc_tty_write,
211 .ioctl = n_hdlc_tty_ioctl,
212 .poll = n_hdlc_tty_poll,
213 .receive_buf = n_hdlc_tty_receive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .write_wakeup = n_hdlc_tty_wakeup,
215};
216
217/**
218 * n_hdlc_release - release an n_hdlc per device line discipline info structure
219 * @n_hdlc - per device line discipline info structure
220 */
221static void n_hdlc_release(struct n_hdlc *n_hdlc)
222{
223 struct tty_struct *tty = n_hdlc2tty (n_hdlc);
224 struct n_hdlc_buf *buf;
225
226 if (debuglevel >= DEBUG_LEVEL_INFO)
227 printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
228
229 /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
230 wake_up_interruptible (&tty->read_wait);
231 wake_up_interruptible (&tty->write_wait);
232
233 if (tty != NULL && tty->disc_data == n_hdlc)
234 tty->disc_data = NULL; /* Break the tty->n_hdlc link */
235
236 /* Release transmit and receive buffers */
237 for(;;) {
238 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
239 if (buf) {
240 kfree(buf);
241 } else
242 break;
243 }
244 for(;;) {
245 buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
246 if (buf) {
247 kfree(buf);
248 } else
249 break;
250 }
251 for(;;) {
252 buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
253 if (buf) {
254 kfree(buf);
255 } else
256 break;
257 }
258 for(;;) {
259 buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
260 if (buf) {
261 kfree(buf);
262 } else
263 break;
264 }
Jesper Juhl735d5662005-11-07 01:01:29 -0800265 kfree(n_hdlc->tbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 kfree(n_hdlc);
267
268} /* end of n_hdlc_release() */
269
270/**
271 * n_hdlc_tty_close - line discipline close
272 * @tty - pointer to tty info structure
273 *
274 * Called when the line discipline is changed to something
275 * else, the tty is closed, or the tty detects a hangup.
276 */
277static void n_hdlc_tty_close(struct tty_struct *tty)
278{
279 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
280
281 if (debuglevel >= DEBUG_LEVEL_INFO)
282 printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
283
284 if (n_hdlc != NULL) {
285 if (n_hdlc->magic != HDLC_MAGIC) {
286 printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
287 return;
288 }
289#if defined(TTY_NO_WRITE_SPLIT)
290 clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
291#endif
292 tty->disc_data = NULL;
293 if (tty == n_hdlc->backup_tty)
294 n_hdlc->backup_tty = NULL;
295 if (tty != n_hdlc->tty)
296 return;
297 if (n_hdlc->backup_tty) {
298 n_hdlc->tty = n_hdlc->backup_tty;
299 } else {
300 n_hdlc_release (n_hdlc);
301 }
302 }
303
304 if (debuglevel >= DEBUG_LEVEL_INFO)
305 printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
306
307} /* end of n_hdlc_tty_close() */
308
309/**
310 * n_hdlc_tty_open - called when line discipline changed to n_hdlc
311 * @tty - pointer to tty info structure
312 *
313 * Returns 0 if success, otherwise error code
314 */
315static int n_hdlc_tty_open (struct tty_struct *tty)
316{
317 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
318
319 if (debuglevel >= DEBUG_LEVEL_INFO)
320 printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n",
321 __FILE__,__LINE__,
322 tty->name);
323
324 /* There should not be an existing table for this slot. */
325 if (n_hdlc) {
326 printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
327 return -EEXIST;
328 }
329
330 n_hdlc = n_hdlc_alloc();
331 if (!n_hdlc) {
332 printk (KERN_ERR "n_hdlc_alloc failed\n");
333 return -ENFILE;
334 }
335
336 tty->disc_data = n_hdlc;
337 n_hdlc->tty = tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800338 tty->receive_room = 65536;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340#if defined(TTY_NO_WRITE_SPLIT)
341 /* change tty_io write() to not split large writes into 8K chunks */
342 set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
343#endif
344
345 /* Flush any pending characters in the driver and discipline. */
346
347 if (tty->ldisc.flush_buffer)
348 tty->ldisc.flush_buffer (tty);
349
350 if (tty->driver->flush_buffer)
351 tty->driver->flush_buffer (tty);
352
353 if (debuglevel >= DEBUG_LEVEL_INFO)
354 printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
355
356 return 0;
357
358} /* end of n_tty_hdlc_open() */
359
360/**
361 * n_hdlc_send_frames - send frames on pending send buffer list
362 * @n_hdlc - pointer to ldisc instance data
363 * @tty - pointer to tty instance data
364 *
365 * Send frames on pending send buffer list until the driver does not accept a
366 * frame (busy) this function is called after adding a frame to the send buffer
367 * list and by the tty wakeup callback.
368 */
369static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
370{
371 register int actual;
372 unsigned long flags;
373 struct n_hdlc_buf *tbuf;
374
375 if (debuglevel >= DEBUG_LEVEL_INFO)
376 printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
377 check_again:
378
379 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
380 if (n_hdlc->tbusy) {
381 n_hdlc->woke_up = 1;
382 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
383 return;
384 }
385 n_hdlc->tbusy = 1;
386 n_hdlc->woke_up = 0;
387 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
388
389 /* get current transmit buffer or get new transmit */
390 /* buffer from list of pending transmit buffers */
391
392 tbuf = n_hdlc->tbuf;
393 if (!tbuf)
394 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
395
396 while (tbuf) {
397 if (debuglevel >= DEBUG_LEVEL_INFO)
398 printk("%s(%d)sending frame %p, count=%d\n",
399 __FILE__,__LINE__,tbuf,tbuf->count);
400
401 /* Send the next block of data to device */
402 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
403 actual = tty->driver->write(tty, tbuf->buf, tbuf->count);
404
405 /* if transmit error, throw frame away by */
406 /* pretending it was accepted by driver */
407 if (actual < 0)
408 actual = tbuf->count;
409
410 if (actual == tbuf->count) {
411 if (debuglevel >= DEBUG_LEVEL_INFO)
412 printk("%s(%d)frame %p completed\n",
413 __FILE__,__LINE__,tbuf);
414
415 /* free current transmit buffer */
416 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
417
418 /* this tx buffer is done */
419 n_hdlc->tbuf = NULL;
420
421 /* wait up sleeping writers */
422 wake_up_interruptible(&tty->write_wait);
423
424 /* get next pending transmit buffer */
425 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
426 } else {
427 if (debuglevel >= DEBUG_LEVEL_INFO)
428 printk("%s(%d)frame %p pending\n",
429 __FILE__,__LINE__,tbuf);
430
431 /* buffer not accepted by driver */
432 /* set this buffer as pending buffer */
433 n_hdlc->tbuf = tbuf;
434 break;
435 }
436 }
437
438 if (!tbuf)
439 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
440
441 /* Clear the re-entry flag */
442 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
443 n_hdlc->tbusy = 0;
444 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
445
446 if (n_hdlc->woke_up)
447 goto check_again;
448
449 if (debuglevel >= DEBUG_LEVEL_INFO)
450 printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
451
452} /* end of n_hdlc_send_frames() */
453
454/**
455 * n_hdlc_tty_wakeup - Callback for transmit wakeup
456 * @tty - pointer to associated tty instance data
457 *
458 * Called when low level device driver can accept more send data.
459 */
460static void n_hdlc_tty_wakeup(struct tty_struct *tty)
461{
462 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
463
464 if (debuglevel >= DEBUG_LEVEL_INFO)
465 printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
466
467 if (!n_hdlc)
468 return;
469
470 if (tty != n_hdlc->tty) {
471 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
472 return;
473 }
474
475 n_hdlc_send_frames (n_hdlc, tty);
476
477} /* end of n_hdlc_tty_wakeup() */
478
479/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * n_hdlc_tty_receive - Called by tty driver when receive data is available
481 * @tty - pointer to tty instance data
482 * @data - pointer to received data
483 * @flags - pointer to flags for data
484 * @count - count of received data in bytes
485 *
486 * Called by tty low level driver when receive data is available. Data is
487 * interpreted as one HDLC frame.
488 */
489static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
490 char *flags, int count)
491{
492 register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
493 register struct n_hdlc_buf *buf;
494
495 if (debuglevel >= DEBUG_LEVEL_INFO)
496 printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
497 __FILE__,__LINE__, count);
498
499 /* This can happen if stuff comes in on the backup tty */
500 if (n_hdlc == 0 || tty != n_hdlc->tty)
501 return;
502
503 /* verify line is using HDLC discipline */
504 if (n_hdlc->magic != HDLC_MAGIC) {
505 printk("%s(%d) line not using HDLC discipline\n",
506 __FILE__,__LINE__);
507 return;
508 }
509
510 if ( count>maxframe ) {
511 if (debuglevel >= DEBUG_LEVEL_INFO)
512 printk("%s(%d) rx count>maxframesize, data discarded\n",
513 __FILE__,__LINE__);
514 return;
515 }
516
517 /* get a free HDLC buffer */
518 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
519 if (!buf) {
520 /* no buffers in free list, attempt to allocate another rx buffer */
521 /* unless the maximum count has been reached */
522 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
523 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_ATOMIC);
524 }
525
526 if (!buf) {
527 if (debuglevel >= DEBUG_LEVEL_INFO)
528 printk("%s(%d) no more rx buffers, data discarded\n",
529 __FILE__,__LINE__);
530 return;
531 }
532
533 /* copy received data to HDLC buffer */
534 memcpy(buf->buf,data,count);
535 buf->count=count;
536
537 /* add HDLC buffer to list of received frames */
538 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
539
540 /* wake up any blocked reads and perform async signalling */
541 wake_up_interruptible (&tty->read_wait);
542 if (n_hdlc->tty->fasync != NULL)
543 kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
544
545} /* end of n_hdlc_tty_receive() */
546
547/**
Matt Mackall4a4efbd2006-01-03 13:27:11 +0100548 * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * @tty - pointer to tty instance data
550 * @file - pointer to open file object
551 * @buf - pointer to returned data buffer
552 * @nr - size of returned data buffer
553 *
554 * Returns the number of bytes returned or error code.
555 */
556static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
557 __u8 __user *buf, size_t nr)
558{
559 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
560 int ret;
561 struct n_hdlc_buf *rbuf;
562
563 if (debuglevel >= DEBUG_LEVEL_INFO)
564 printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
565
566 /* Validate the pointers */
567 if (!n_hdlc)
568 return -EIO;
569
570 /* verify user access to buffer */
571 if (!access_ok(VERIFY_WRITE, buf, nr)) {
572 printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user "
573 "buffer\n", __FILE__, __LINE__);
574 return -EFAULT;
575 }
576
577 for (;;) {
578 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
579 return -EIO;
580
581 n_hdlc = tty2n_hdlc (tty);
582 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
583 tty != n_hdlc->tty)
584 return 0;
585
586 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
587 if (rbuf)
588 break;
589
590 /* no data */
591 if (file->f_flags & O_NONBLOCK)
592 return -EAGAIN;
593
594 interruptible_sleep_on (&tty->read_wait);
595 if (signal_pending(current))
596 return -EINTR;
597 }
598
599 if (rbuf->count > nr)
600 /* frame too large for caller's buffer (discard frame) */
601 ret = -EOVERFLOW;
602 else {
603 /* Copy the data to the caller's buffer */
604 if (copy_to_user(buf, rbuf->buf, rbuf->count))
605 ret = -EFAULT;
606 else
607 ret = rbuf->count;
608 }
609
610 /* return HDLC buffer to free list unless the free list */
611 /* count has exceeded the default value, in which case the */
612 /* buffer is freed back to the OS to conserve memory */
613 if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
614 kfree(rbuf);
615 else
616 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf);
617
618 return ret;
619
620} /* end of n_hdlc_tty_read() */
621
622/**
623 * n_hdlc_tty_write - write a single frame of data to device
624 * @tty - pointer to associated tty device instance data
625 * @file - pointer to file object data
626 * @data - pointer to transmit data (one frame)
627 * @count - size of transmit frame in bytes
628 *
629 * Returns the number of bytes written (or error code).
630 */
631static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
632 const unsigned char *data, size_t count)
633{
634 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
635 int error = 0;
636 DECLARE_WAITQUEUE(wait, current);
637 struct n_hdlc_buf *tbuf;
638
639 if (debuglevel >= DEBUG_LEVEL_INFO)
640 printk("%s(%d)n_hdlc_tty_write() called count=%Zd\n",
641 __FILE__,__LINE__,count);
642
643 /* Verify pointers */
644 if (!n_hdlc)
645 return -EIO;
646
647 if (n_hdlc->magic != HDLC_MAGIC)
648 return -EIO;
649
650 /* verify frame size */
651 if (count > maxframe ) {
652 if (debuglevel & DEBUG_LEVEL_INFO)
653 printk (KERN_WARNING
654 "n_hdlc_tty_write: truncating user packet "
655 "from %lu to %d\n", (unsigned long) count,
656 maxframe );
657 count = maxframe;
658 }
659
660 add_wait_queue(&tty->write_wait, &wait);
661 set_current_state(TASK_INTERRUPTIBLE);
662
663 /* Allocate transmit buffer */
664 /* sleep until transmit buffer available */
665 while (!(tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list))) {
666 schedule();
667
668 n_hdlc = tty2n_hdlc (tty);
669 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
670 tty != n_hdlc->tty) {
671 printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
672 error = -EIO;
673 break;
674 }
675
676 if (signal_pending(current)) {
677 error = -EINTR;
678 break;
679 }
680 }
681
682 set_current_state(TASK_RUNNING);
683 remove_wait_queue(&tty->write_wait, &wait);
684
685 if (!error) {
686 /* Retrieve the user's buffer */
687 memcpy(tbuf->buf, data, count);
688
689 /* Send the data */
690 tbuf->count = error = count;
691 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
692 n_hdlc_send_frames(n_hdlc,tty);
693 }
694
695 return error;
696
697} /* end of n_hdlc_tty_write() */
698
699/**
700 * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
701 * @tty - pointer to tty instance data
702 * @file - pointer to open file object for device
703 * @cmd - IOCTL command code
704 * @arg - argument for IOCTL call (cmd dependent)
705 *
706 * Returns command dependent result.
707 */
708static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
709 unsigned int cmd, unsigned long arg)
710{
711 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
712 int error = 0;
713 int count;
714 unsigned long flags;
715
716 if (debuglevel >= DEBUG_LEVEL_INFO)
717 printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
718 __FILE__,__LINE__,cmd);
719
720 /* Verify the status of the device */
721 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
722 return -EBADF;
723
724 switch (cmd) {
725 case FIONREAD:
726 /* report count of read data available */
727 /* in next available frame (if any) */
728 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
729 if (n_hdlc->rx_buf_list.head)
730 count = n_hdlc->rx_buf_list.head->count;
731 else
732 count = 0;
733 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
734 error = put_user(count, (int __user *)arg);
735 break;
736
737 case TIOCOUTQ:
738 /* get the pending tx byte count in the driver */
739 count = tty->driver->chars_in_buffer ?
740 tty->driver->chars_in_buffer(tty) : 0;
741 /* add size of next output frame in queue */
742 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
743 if (n_hdlc->tx_buf_list.head)
744 count += n_hdlc->tx_buf_list.head->count;
745 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
746 error = put_user(count, (int __user *)arg);
747 break;
748
749 default:
750 error = n_tty_ioctl (tty, file, cmd, arg);
751 break;
752 }
753 return error;
754
755} /* end of n_hdlc_tty_ioctl() */
756
757/**
758 * n_hdlc_tty_poll - TTY callback for poll system call
759 * @tty - pointer to tty instance data
760 * @filp - pointer to open file object for device
761 * @poll_table - wait queue for operations
762 *
763 * Determine which operations (read/write) will not block and return info
764 * to caller.
765 * Returns a bit mask containing info on which ops will not block.
766 */
767static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
768 poll_table *wait)
769{
770 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
771 unsigned int mask = 0;
772
773 if (debuglevel >= DEBUG_LEVEL_INFO)
774 printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
775
776 if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
777 /* queue current process into any wait queue that */
778 /* may awaken in the future (read and write) */
779
780 poll_wait(filp, &tty->read_wait, wait);
781 poll_wait(filp, &tty->write_wait, wait);
782
783 /* set bits for operations that won't block */
784 if(n_hdlc->rx_buf_list.head)
785 mask |= POLLIN | POLLRDNORM; /* readable */
786 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
787 mask |= POLLHUP;
788 if(tty_hung_up_p(filp))
789 mask |= POLLHUP;
790 if(n_hdlc->tx_free_buf_list.head)
791 mask |= POLLOUT | POLLWRNORM; /* writable */
792 }
793 return mask;
794} /* end of n_hdlc_tty_poll() */
795
796/**
797 * n_hdlc_alloc - allocate an n_hdlc instance data structure
798 *
799 * Returns a pointer to newly created structure if success, otherwise %NULL
800 */
801static struct n_hdlc *n_hdlc_alloc(void)
802{
803 struct n_hdlc_buf *buf;
804 int i;
805 struct n_hdlc *n_hdlc = kmalloc(sizeof(*n_hdlc), GFP_KERNEL);
806
807 if (!n_hdlc)
808 return NULL;
809
810 memset(n_hdlc, 0, sizeof(*n_hdlc));
811
812 n_hdlc_buf_list_init(&n_hdlc->rx_free_buf_list);
813 n_hdlc_buf_list_init(&n_hdlc->tx_free_buf_list);
814 n_hdlc_buf_list_init(&n_hdlc->rx_buf_list);
815 n_hdlc_buf_list_init(&n_hdlc->tx_buf_list);
816
817 /* allocate free rx buffer list */
818 for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
819 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
820 if (buf)
821 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
822 else if (debuglevel >= DEBUG_LEVEL_INFO)
823 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
824 }
825
826 /* allocate free tx buffer list */
827 for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
828 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
829 if (buf)
830 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
831 else if (debuglevel >= DEBUG_LEVEL_INFO)
832 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
833 }
834
835 /* Initialize the control block */
836 n_hdlc->magic = HDLC_MAGIC;
837 n_hdlc->flags = 0;
838
839 return n_hdlc;
840
841} /* end of n_hdlc_alloc() */
842
843/**
844 * n_hdlc_buf_list_init - initialize specified HDLC buffer list
845 * @list - pointer to buffer list
846 */
847static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list)
848{
849 memset(list, 0, sizeof(*list));
850 spin_lock_init(&list->spinlock);
851} /* end of n_hdlc_buf_list_init() */
852
853/**
854 * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
855 * @list - pointer to buffer list
856 * @buf - pointer to buffer
857 */
858static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
859 struct n_hdlc_buf *buf)
860{
861 unsigned long flags;
862 spin_lock_irqsave(&list->spinlock,flags);
863
864 buf->link=NULL;
865 if(list->tail)
866 list->tail->link = buf;
867 else
868 list->head = buf;
869 list->tail = buf;
870 (list->count)++;
871
872 spin_unlock_irqrestore(&list->spinlock,flags);
873
874} /* end of n_hdlc_buf_put() */
875
876/**
877 * n_hdlc_buf_get - remove and return an HDLC buffer from list
878 * @list - pointer to HDLC buffer list
879 *
880 * Remove and return an HDLC buffer from the head of the specified HDLC buffer
881 * list.
882 * Returns a pointer to HDLC buffer if available, otherwise %NULL.
883 */
884static struct n_hdlc_buf* n_hdlc_buf_get(struct n_hdlc_buf_list *list)
885{
886 unsigned long flags;
887 struct n_hdlc_buf *buf;
888 spin_lock_irqsave(&list->spinlock,flags);
889
890 buf = list->head;
891 if (buf) {
892 list->head = buf->link;
893 (list->count)--;
894 }
895 if (!list->head)
896 list->tail = NULL;
897
898 spin_unlock_irqrestore(&list->spinlock,flags);
899 return buf;
900
901} /* end of n_hdlc_buf_get() */
902
903static char hdlc_banner[] __initdata =
904 KERN_INFO "HDLC line discipline: version " HDLC_VERSION
905 ", maxframe=%u\n";
906static char hdlc_register_ok[] __initdata =
907 KERN_INFO "N_HDLC line discipline registered.\n";
908static char hdlc_register_fail[] __initdata =
909 KERN_ERR "error registering line discipline: %d\n";
910static char hdlc_init_fail[] __initdata =
911 KERN_INFO "N_HDLC: init failure %d\n";
912
913static int __init n_hdlc_init(void)
914{
915 int status;
916
917 /* range check maxframe arg */
918 if (maxframe < 4096)
919 maxframe = 4096;
920 else if (maxframe > 65535)
921 maxframe = 65535;
922
923 printk(hdlc_banner, maxframe);
924
925 status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
926 if (!status)
927 printk(hdlc_register_ok);
928 else
929 printk(hdlc_register_fail, status);
930
931 if (status)
932 printk(hdlc_init_fail, status);
933 return status;
934
935} /* end of init_module() */
936
937static char hdlc_unregister_ok[] __exitdata =
938 KERN_INFO "N_HDLC: line discipline unregistered\n";
939static char hdlc_unregister_fail[] __exitdata =
940 KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
941
942static void __exit n_hdlc_exit(void)
943{
944 /* Release tty registration of line discipline */
Alexey Dobriyan64ccd712005-06-23 00:10:33 -0700945 int status = tty_unregister_ldisc(N_HDLC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 if (status)
948 printk(hdlc_unregister_fail, status);
949 else
950 printk(hdlc_unregister_ok);
951}
952
953module_init(n_hdlc_init);
954module_exit(n_hdlc_exit);
955
956MODULE_LICENSE("GPL");
957MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
958module_param(debuglevel, int, 0);
959module_param(maxframe, int, 0);
960MODULE_ALIAS_LDISC(N_HDLC);