1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80#define HDLC_MAGIC 0x239e
81
82#include <linux/module.h>
83#include <linux/init.h>
84#include <linux/kernel.h>
85#include <linux/sched.h>
86#include <linux/types.h>
87#include <linux/fcntl.h>
88#include <linux/interrupt.h>
89#include <linux/ptrace.h>
90
91#undef VERSION
92#define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
93
94#include <linux/poll.h>
95#include <linux/in.h>
96#include <linux/ioctl.h>
97#include <linux/slab.h>
98#include <linux/tty.h>
99#include <linux/errno.h>
100#include <linux/string.h>
101#include <linux/signal.h>
102#include <linux/if.h>
103#include <linux/bitops.h>
104
105#include <asm/termios.h>
106#include <asm/uaccess.h>
107
108
109
110
111#define MAX_HDLC_FRAME_SIZE 65535
112#define DEFAULT_RX_BUF_COUNT 10
113#define MAX_RX_BUF_COUNT 60
114#define DEFAULT_TX_BUF_COUNT 3
115
116struct n_hdlc_buf {
117 struct n_hdlc_buf *link;
118 int count;
119 char buf[1];
120};
121
122#define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe)
123
124struct n_hdlc_buf_list {
125 struct n_hdlc_buf *head;
126 struct n_hdlc_buf *tail;
127 int count;
128 spinlock_t spinlock;
129};
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145struct n_hdlc {
146 int magic;
147 __u32 flags;
148 struct tty_struct *tty;
149 struct tty_struct *backup_tty;
150 int tbusy;
151 int woke_up;
152 struct n_hdlc_buf *tbuf;
153 struct n_hdlc_buf_list tx_buf_list;
154 struct n_hdlc_buf_list rx_buf_list;
155 struct n_hdlc_buf_list tx_free_buf_list;
156 struct n_hdlc_buf_list rx_free_buf_list;
157};
158
159
160
161
162static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
163 struct n_hdlc_buf *buf);
164static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
165
166
167
168static struct n_hdlc *n_hdlc_alloc (void);
169
170
171#define DEBUG_LEVEL_INFO 1
172static int debuglevel;
173
174
175static int maxframe = 4096;
176
177
178
179static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
180 __u8 __user *buf, size_t nr);
181static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
182 const unsigned char *buf, size_t nr);
183static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
184 unsigned int cmd, unsigned long arg);
185static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
186 poll_table *wait);
187static int n_hdlc_tty_open(struct tty_struct *tty);
188static void n_hdlc_tty_close(struct tty_struct *tty);
189static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
190 char *fp, int count);
191static void n_hdlc_tty_wakeup(struct tty_struct *tty);
192
193#define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
194
195#define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
196#define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
197
198static void flush_rx_queue(struct tty_struct *tty)
199{
200 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
201 struct n_hdlc_buf *buf;
202
203 while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
204 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf);
205}
206
207static void flush_tx_queue(struct tty_struct *tty)
208{
209 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
210 struct n_hdlc_buf *buf;
211 unsigned long flags;
212
213 while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
214 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
215 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
216 if (n_hdlc->tbuf) {
217 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, n_hdlc->tbuf);
218 n_hdlc->tbuf = NULL;
219 }
220 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
221}
222
223static struct tty_ldisc_ops n_hdlc_ldisc = {
224 .owner = THIS_MODULE,
225 .magic = TTY_LDISC_MAGIC,
226 .name = "hdlc",
227 .open = n_hdlc_tty_open,
228 .close = n_hdlc_tty_close,
229 .read = n_hdlc_tty_read,
230 .write = n_hdlc_tty_write,
231 .ioctl = n_hdlc_tty_ioctl,
232 .poll = n_hdlc_tty_poll,
233 .receive_buf = n_hdlc_tty_receive,
234 .write_wakeup = n_hdlc_tty_wakeup,
235 .flush_buffer = flush_rx_queue,
236};
237
238
239
240
241
242static void n_hdlc_release(struct n_hdlc *n_hdlc)
243{
244 struct tty_struct *tty = n_hdlc2tty (n_hdlc);
245 struct n_hdlc_buf *buf;
246
247 if (debuglevel >= DEBUG_LEVEL_INFO)
248 printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
249
250
251 wake_up_interruptible (&tty->read_wait);
252 wake_up_interruptible (&tty->write_wait);
253
254 if (tty->disc_data == n_hdlc)
255 tty->disc_data = NULL;
256
257
258 for(;;) {
259 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
260 if (buf) {
261 kfree(buf);
262 } else
263 break;
264 }
265 for(;;) {
266 buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
267 if (buf) {
268 kfree(buf);
269 } else
270 break;
271 }
272 for(;;) {
273 buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
274 if (buf) {
275 kfree(buf);
276 } else
277 break;
278 }
279 for(;;) {
280 buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
281 if (buf) {
282 kfree(buf);
283 } else
284 break;
285 }
286 kfree(n_hdlc->tbuf);
287 kfree(n_hdlc);
288
289}
290
291
292
293
294
295
296
297
298static void n_hdlc_tty_close(struct tty_struct *tty)
299{
300 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
301
302 if (debuglevel >= DEBUG_LEVEL_INFO)
303 printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
304
305 if (n_hdlc != NULL) {
306 if (n_hdlc->magic != HDLC_MAGIC) {
307 printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
308 return;
309 }
310#if defined(TTY_NO_WRITE_SPLIT)
311 clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
312#endif
313 tty->disc_data = NULL;
314 if (tty == n_hdlc->backup_tty)
315 n_hdlc->backup_tty = NULL;
316 if (tty != n_hdlc->tty)
317 return;
318 if (n_hdlc->backup_tty) {
319 n_hdlc->tty = n_hdlc->backup_tty;
320 } else {
321 n_hdlc_release (n_hdlc);
322 }
323 }
324
325 if (debuglevel >= DEBUG_LEVEL_INFO)
326 printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
327
328}
329
330
331
332
333
334
335
336static int n_hdlc_tty_open (struct tty_struct *tty)
337{
338 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
339
340 if (debuglevel >= DEBUG_LEVEL_INFO)
341 printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n",
342 __FILE__,__LINE__,
343 tty->name);
344
345
346 if (n_hdlc) {
347 printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
348 return -EEXIST;
349 }
350
351 n_hdlc = n_hdlc_alloc();
352 if (!n_hdlc) {
353 printk (KERN_ERR "n_hdlc_alloc failed\n");
354 return -ENFILE;
355 }
356
357 tty->disc_data = n_hdlc;
358 n_hdlc->tty = tty;
359 tty->receive_room = 65536;
360
361#if defined(TTY_NO_WRITE_SPLIT)
362
363 set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
364#endif
365
366
367 tty_driver_flush_buffer(tty);
368
369 if (debuglevel >= DEBUG_LEVEL_INFO)
370 printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
371
372 return 0;
373
374}
375
376
377
378
379
380
381
382
383
384
385static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
386{
387 register int actual;
388 unsigned long flags;
389 struct n_hdlc_buf *tbuf;
390
391 if (debuglevel >= DEBUG_LEVEL_INFO)
392 printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
393 check_again:
394
395 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
396 if (n_hdlc->tbusy) {
397 n_hdlc->woke_up = 1;
398 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
399 return;
400 }
401 n_hdlc->tbusy = 1;
402 n_hdlc->woke_up = 0;
403 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
404
405
406
407
408 tbuf = n_hdlc->tbuf;
409 if (!tbuf)
410 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
411
412 while (tbuf) {
413 if (debuglevel >= DEBUG_LEVEL_INFO)
414 printk("%s(%d)sending frame %p, count=%d\n",
415 __FILE__,__LINE__,tbuf,tbuf->count);
416
417
418 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
419 actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
420
421
422 if (actual == -ERESTARTSYS) {
423 n_hdlc->tbuf = tbuf;
424 break;
425 }
426
427
428 if (actual < 0)
429 actual = tbuf->count;
430
431 if (actual == tbuf->count) {
432 if (debuglevel >= DEBUG_LEVEL_INFO)
433 printk("%s(%d)frame %p completed\n",
434 __FILE__,__LINE__,tbuf);
435
436
437 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
438
439
440 n_hdlc->tbuf = NULL;
441
442
443 wake_up_interruptible(&tty->write_wait);
444
445
446 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
447 } else {
448 if (debuglevel >= DEBUG_LEVEL_INFO)
449 printk("%s(%d)frame %p pending\n",
450 __FILE__,__LINE__,tbuf);
451
452
453
454 n_hdlc->tbuf = tbuf;
455 break;
456 }
457 }
458
459 if (!tbuf)
460 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
461
462
463 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
464 n_hdlc->tbusy = 0;
465 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
466
467 if (n_hdlc->woke_up)
468 goto check_again;
469
470 if (debuglevel >= DEBUG_LEVEL_INFO)
471 printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
472
473}
474
475
476
477
478
479
480
481static void n_hdlc_tty_wakeup(struct tty_struct *tty)
482{
483 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
484
485 if (debuglevel >= DEBUG_LEVEL_INFO)
486 printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
487
488 if (!n_hdlc)
489 return;
490
491 if (tty != n_hdlc->tty) {
492 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
493 return;
494 }
495
496 n_hdlc_send_frames (n_hdlc, tty);
497
498}
499
500
501
502
503
504
505
506
507
508
509
510static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
511 char *flags, int count)
512{
513 register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
514 register struct n_hdlc_buf *buf;
515
516 if (debuglevel >= DEBUG_LEVEL_INFO)
517 printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
518 __FILE__,__LINE__, count);
519
520
521 if (!n_hdlc || tty != n_hdlc->tty)
522 return;
523
524
525 if (n_hdlc->magic != HDLC_MAGIC) {
526 printk("%s(%d) line not using HDLC discipline\n",
527 __FILE__,__LINE__);
528 return;
529 }
530
531 if ( count>maxframe ) {
532 if (debuglevel >= DEBUG_LEVEL_INFO)
533 printk("%s(%d) rx count>maxframesize, data discarded\n",
534 __FILE__,__LINE__);
535 return;
536 }
537
538
539 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
540 if (!buf) {
541
542
543 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
544 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_ATOMIC);
545 }
546
547 if (!buf) {
548 if (debuglevel >= DEBUG_LEVEL_INFO)
549 printk("%s(%d) no more rx buffers, data discarded\n",
550 __FILE__,__LINE__);
551 return;
552 }
553
554
555 memcpy(buf->buf,data,count);
556 buf->count=count;
557
558
559 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
560
561
562 wake_up_interruptible (&tty->read_wait);
563 if (n_hdlc->tty->fasync != NULL)
564 kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
565
566}
567
568
569
570
571
572
573
574
575
576
577static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
578 __u8 __user *buf, size_t nr)
579{
580 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
581 int ret = 0;
582 struct n_hdlc_buf *rbuf;
583 DECLARE_WAITQUEUE(wait, current);
584
585 if (debuglevel >= DEBUG_LEVEL_INFO)
586 printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
587
588
589 if (!n_hdlc)
590 return -EIO;
591
592
593 if (!access_ok(VERIFY_WRITE, buf, nr)) {
594 printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user "
595 "buffer\n", __FILE__, __LINE__);
596 return -EFAULT;
597 }
598
599 add_wait_queue(&tty->read_wait, &wait);
600
601 for (;;) {
602 if (test_bit(TTY_OTHER_DONE, &tty->flags)) {
603 ret = -EIO;
604 break;
605 }
606 if (tty_hung_up_p(file))
607 break;
608
609 set_current_state(TASK_INTERRUPTIBLE);
610
611 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
612 if (rbuf) {
613 if (rbuf->count > nr) {
614
615 ret = -EOVERFLOW;
616 } else {
617 if (copy_to_user(buf, rbuf->buf, rbuf->count))
618 ret = -EFAULT;
619 else
620 ret = rbuf->count;
621 }
622
623 if (n_hdlc->rx_free_buf_list.count >
624 DEFAULT_RX_BUF_COUNT)
625 kfree(rbuf);
626 else
627 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
628 break;
629 }
630
631
632 if (file->f_flags & O_NONBLOCK) {
633 ret = -EAGAIN;
634 break;
635 }
636
637 schedule();
638
639 if (signal_pending(current)) {
640 ret = -EINTR;
641 break;
642 }
643 }
644
645 remove_wait_queue(&tty->read_wait, &wait);
646 __set_current_state(TASK_RUNNING);
647
648 return ret;
649
650}
651
652
653
654
655
656
657
658
659
660
661static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
662 const unsigned char *data, size_t count)
663{
664 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
665 int error = 0;
666 DECLARE_WAITQUEUE(wait, current);
667 struct n_hdlc_buf *tbuf;
668
669 if (debuglevel >= DEBUG_LEVEL_INFO)
670 printk("%s(%d)n_hdlc_tty_write() called count=%Zd\n",
671 __FILE__,__LINE__,count);
672
673
674 if (!n_hdlc)
675 return -EIO;
676
677 if (n_hdlc->magic != HDLC_MAGIC)
678 return -EIO;
679
680
681 if (count > maxframe ) {
682 if (debuglevel & DEBUG_LEVEL_INFO)
683 printk (KERN_WARNING
684 "n_hdlc_tty_write: truncating user packet "
685 "from %lu to %d\n", (unsigned long) count,
686 maxframe );
687 count = maxframe;
688 }
689
690 add_wait_queue(&tty->write_wait, &wait);
691
692 for (;;) {
693 set_current_state(TASK_INTERRUPTIBLE);
694
695 tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
696 if (tbuf)
697 break;
698
699 if (file->f_flags & O_NONBLOCK) {
700 error = -EAGAIN;
701 break;
702 }
703 schedule();
704
705 n_hdlc = tty2n_hdlc (tty);
706 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
707 tty != n_hdlc->tty) {
708 printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
709 error = -EIO;
710 break;
711 }
712
713 if (signal_pending(current)) {
714 error = -EINTR;
715 break;
716 }
717 }
718
719 __set_current_state(TASK_RUNNING);
720 remove_wait_queue(&tty->write_wait, &wait);
721
722 if (!error) {
723
724 memcpy(tbuf->buf, data, count);
725
726
727 tbuf->count = error = count;
728 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
729 n_hdlc_send_frames(n_hdlc,tty);
730 }
731
732 return error;
733
734}
735
736
737
738
739
740
741
742
743
744
745static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
746 unsigned int cmd, unsigned long arg)
747{
748 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
749 int error = 0;
750 int count;
751 unsigned long flags;
752
753 if (debuglevel >= DEBUG_LEVEL_INFO)
754 printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
755 __FILE__,__LINE__,cmd);
756
757
758 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
759 return -EBADF;
760
761 switch (cmd) {
762 case FIONREAD:
763
764
765 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
766 if (n_hdlc->rx_buf_list.head)
767 count = n_hdlc->rx_buf_list.head->count;
768 else
769 count = 0;
770 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
771 error = put_user(count, (int __user *)arg);
772 break;
773
774 case TIOCOUTQ:
775
776 count = tty_chars_in_buffer(tty);
777
778 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
779 if (n_hdlc->tx_buf_list.head)
780 count += n_hdlc->tx_buf_list.head->count;
781 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
782 error = put_user(count, (int __user *)arg);
783 break;
784
785 case TCFLSH:
786 switch (arg) {
787 case TCIOFLUSH:
788 case TCOFLUSH:
789 flush_tx_queue(tty);
790 }
791
792
793 default:
794 error = n_tty_ioctl_helper(tty, file, cmd, arg);
795 break;
796 }
797 return error;
798
799}
800
801
802
803
804
805
806
807
808
809
810
811static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
812 poll_table *wait)
813{
814 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
815 unsigned int mask = 0;
816
817 if (debuglevel >= DEBUG_LEVEL_INFO)
818 printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
819
820 if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
821
822
823
824 poll_wait(filp, &tty->read_wait, wait);
825 poll_wait(filp, &tty->write_wait, wait);
826
827
828 if (n_hdlc->rx_buf_list.head)
829 mask |= POLLIN | POLLRDNORM;
830 if (test_bit(TTY_OTHER_DONE, &tty->flags))
831 mask |= POLLHUP;
832 if (tty_hung_up_p(filp))
833 mask |= POLLHUP;
834 if (!tty_is_writelocked(tty) &&
835 n_hdlc->tx_free_buf_list.head)
836 mask |= POLLOUT | POLLWRNORM;
837 }
838 return mask;
839}
840
841
842
843
844
845
846static struct n_hdlc *n_hdlc_alloc(void)
847{
848 struct n_hdlc_buf *buf;
849 int i;
850 struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL);
851
852 if (!n_hdlc)
853 return NULL;
854
855 spin_lock_init(&n_hdlc->rx_free_buf_list.spinlock);
856 spin_lock_init(&n_hdlc->tx_free_buf_list.spinlock);
857 spin_lock_init(&n_hdlc->rx_buf_list.spinlock);
858 spin_lock_init(&n_hdlc->tx_buf_list.spinlock);
859
860
861 for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
862 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
863 if (buf)
864 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
865 else if (debuglevel >= DEBUG_LEVEL_INFO)
866 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
867 }
868
869
870 for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
871 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
872 if (buf)
873 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
874 else if (debuglevel >= DEBUG_LEVEL_INFO)
875 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
876 }
877
878
879 n_hdlc->magic = HDLC_MAGIC;
880 n_hdlc->flags = 0;
881
882 return n_hdlc;
883
884}
885
886
887
888
889
890
891static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
892 struct n_hdlc_buf *buf)
893{
894 unsigned long flags;
895 spin_lock_irqsave(&list->spinlock,flags);
896
897 buf->link=NULL;
898 if (list->tail)
899 list->tail->link = buf;
900 else
901 list->head = buf;
902 list->tail = buf;
903 (list->count)++;
904
905 spin_unlock_irqrestore(&list->spinlock,flags);
906
907}
908
909
910
911
912
913
914
915
916
917static struct n_hdlc_buf* n_hdlc_buf_get(struct n_hdlc_buf_list *list)
918{
919 unsigned long flags;
920 struct n_hdlc_buf *buf;
921 spin_lock_irqsave(&list->spinlock,flags);
922
923 buf = list->head;
924 if (buf) {
925 list->head = buf->link;
926 (list->count)--;
927 }
928 if (!list->head)
929 list->tail = NULL;
930
931 spin_unlock_irqrestore(&list->spinlock,flags);
932 return buf;
933
934}
935
936static char hdlc_banner[] __initdata =
937 KERN_INFO "HDLC line discipline maxframe=%u\n";
938static char hdlc_register_ok[] __initdata =
939 KERN_INFO "N_HDLC line discipline registered.\n";
940static char hdlc_register_fail[] __initdata =
941 KERN_ERR "error registering line discipline: %d\n";
942
943static int __init n_hdlc_init(void)
944{
945 int status;
946
947
948 if (maxframe < 4096)
949 maxframe = 4096;
950 else if (maxframe > 65535)
951 maxframe = 65535;
952
953 printk(hdlc_banner, maxframe);
954
955 status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
956 if (!status)
957 printk(hdlc_register_ok);
958 else
959 printk(hdlc_register_fail, status);
960
961 return status;
962
963}
964
965static char hdlc_unregister_ok[] __exitdata =
966 KERN_INFO "N_HDLC: line discipline unregistered\n";
967static char hdlc_unregister_fail[] __exitdata =
968 KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
969
970static void __exit n_hdlc_exit(void)
971{
972
973 int status = tty_unregister_ldisc(N_HDLC);
974
975 if (status)
976 printk(hdlc_unregister_fail, status);
977 else
978 printk(hdlc_unregister_ok);
979}
980
981module_init(n_hdlc_init);
982module_exit(n_hdlc_exit);
983
984MODULE_LICENSE("GPL");
985MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
986module_param(debuglevel, int, 0);
987module_param(maxframe, int, 0);
988MODULE_ALIAS_LDISC(N_HDLC);
989