OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [net/] [mkiss.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *      MKISS Driver
3
 *
4
 *      This module:
5
 *              This module is free software; you can redistribute it and/or
6
 *              modify it under the terms of the GNU General Public License
7
 *              as published by the Free Software Foundation; either version
8
 *              2 of the License, or (at your option) any later version.
9
 *
10
 *              This module implements the AX.25 protocol for kernel-based
11
 *              devices like TTYs. It interfaces between a raw TTY, and the
12
 *              kernel's AX.25 protocol layers, just like slip.c.
13
 *              AX.25 needs to be seperated from slip.c while slip.c is no
14
 *              longer a static kernel device since it is a module.
15
 *              This method clears the way to implement other kiss protocols
16
 *              like mkiss smack g8bpq ..... so far only mkiss is implemented.
17
 *
18
 * Hans Alblas <hans@esrac.ele.tue.nl>
19
 *
20
 *      History
21
 */
22
 
23
#include <linux/config.h>
24
#include <linux/module.h>
25
#include <asm/system.h>
26
#include <asm/segment.h>
27
#include <asm/bitops.h>
28
#include <linux/string.h>
29
#include <linux/mm.h>
30
#include <linux/interrupt.h>
31
#include <linux/in.h>
32
#include <linux/inet.h>
33
#include <linux/tty.h>
34
#include <linux/errno.h>
35
#include <linux/netdevice.h>
36
#include <linux/major.h>
37
 
38
#include <linux/timer.h>
39
 
40
#include <linux/etherdevice.h>
41
#include <linux/skbuff.h>
42
#include <linux/if_arp.h>
43
 
44
#include <net/ax25.h>
45
 
46
#include "mkiss.h"
47
 
48
#ifdef CONFIG_INET
49
#include <linux/ip.h>
50
#include <linux/tcp.h>
51
#endif
52
 
53
#ifdef MODULE
54
#define AX25_VERSION    "AX25-MODULAR-NET3.019-NEWTTY"
55
#define min(a,b)        (a < b ? a : b)
56
#else
57
#define AX25_VERSION    "AX25-NET3.019-NEWTTY"
58
#endif
59
 
60
#define NR_MKISS 4
61
#define MKISS_SERIAL_TYPE_NORMAL 1
62
 
63
struct mkiss_channel {
64
        int magic;              /* magic word */
65
        int init;               /* channel exists? */
66
        struct tty_struct *tty; /* link to tty control structure */
67
};
68
 
69
typedef struct ax25_ctrl {
70
        char if_name[8];        /* "ax0\0" .. "ax99999\0"       */
71
        struct ax_disp ctrl;    /*                              */
72
        struct device  dev;     /* the device                   */
73
} ax25_ctrl_t;
74
 
75
static ax25_ctrl_t **ax25_ctrls = NULL;
76
int ax25_maxdev = AX25_MAXDEV;          /* Can be overridden with insmod! */
77
 
78
static struct tty_ldisc ax_ldisc;
79
static struct tty_driver mkiss_driver;
80
static int mkiss_refcount;
81
static struct tty_struct *mkiss_table[NR_MKISS];
82
static struct termios *mkiss_termios[NR_MKISS];
83
static struct termios *mkiss_termios_locked[NR_MKISS];
84
struct mkiss_channel MKISS_Info[NR_MKISS];
85
 
86
static int ax25_init(struct device *);
87
static int mkiss_init(void);
88
static int mkiss_write(struct tty_struct *, int, const unsigned char *, int);
89
static int kiss_esc(unsigned char *, unsigned char *, int);
90
static void kiss_unesc(struct ax_disp *, unsigned char);
91
 
92
/* Find a free channel, and link in this `tty' line. */
93
static inline struct ax_disp *ax_alloc(void)
94
{
95
        ax25_ctrl_t *axp;
96
        int i;
97
 
98
        if (ax25_ctrls == NULL)         /* Master array missing ! */
99
                return NULL;
100
 
101
        for (i = 0; i < ax25_maxdev; i++) {
102
                axp = ax25_ctrls[i];
103
 
104
                /* Not allocated ? */
105
                if (axp == NULL)
106
                        break;
107
 
108
                /* Not in use ? */
109
                if (!set_bit(AXF_INUSE, &axp->ctrl.flags))
110
                        break;
111
        }
112
 
113
        /* Sorry, too many, all slots in use */
114
        if (i >= ax25_maxdev)
115
                return NULL;
116
 
117
        /* If no channels are available, allocate one */
118
        if (axp == NULL && (ax25_ctrls[i] = (ax25_ctrl_t *)kmalloc(sizeof(ax25_ctrl_t), GFP_KERNEL)) != NULL) {
119
                axp = ax25_ctrls[i];
120
                memset(axp, 0, sizeof(ax25_ctrl_t));
121
 
122
                /* Initialize channel control data */
123
                set_bit(AXF_INUSE, &axp->ctrl.flags);
124
                sprintf(axp->if_name, "ax%d", i++);
125
                axp->ctrl.tty      = NULL;
126
                axp->dev.name      = axp->if_name;
127
                axp->dev.base_addr = i;
128
                axp->dev.priv      = (void *)&axp->ctrl;
129
                axp->dev.next      = NULL;
130
                axp->dev.init      = ax25_init;
131
        }
132
 
133
        if (axp != NULL) {
134
                /*
135
                 * register device so that it can be ifconfig'ed
136
                 * ax25_init() will be called as a side-effect
137
                 * SIDE-EFFECT WARNING: ax25_init() CLEARS axp->ctrl !
138
                 */
139
                if (register_netdev(&axp->dev) == 0) {
140
                        /* (Re-)Set the INUSE bit.   Very Important! */
141
                        set_bit(AXF_INUSE, &axp->ctrl.flags);
142
                        axp->ctrl.dev = &axp->dev;
143
                        axp->dev.priv = (void *)&axp->ctrl;
144
 
145
                        return &axp->ctrl;
146
                } else {
147
                        clear_bit(AXF_INUSE,&axp->ctrl.flags);
148
                        printk(KERN_ERR "ax_alloc() - register_netdev() failure.\n");
149
                }
150
        }
151
 
152
        return NULL;
153
}
154
 
155
/* Free an AX25 channel. */
156
static inline void ax_free(struct ax_disp *ax)
157
{
158
        /* Free all AX25 frame buffers. */
159
        if (ax->rbuff)
160
                kfree(ax->rbuff);
161
        ax->rbuff = NULL;
162
        if (ax->xbuff)
163
                kfree(ax->xbuff);
164
        ax->xbuff = NULL;
165
        if (!clear_bit(AXF_INUSE, &ax->flags))
166
                printk(KERN_ERR "%s: ax_free for already free unit.\n", ax->dev->name);
167
}
168
 
169
static void ax_changedmtu(struct ax_disp *ax)
170
{
171
        struct device *dev = ax->dev;
172
        unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
173
        int len;
174
        unsigned long flags;
175
 
176
        len = dev->mtu * 2;
177
 
178
        /*
179
         * allow for arrival of larger UDP packets, even if we say not to
180
         * also fixes a bug in which SunOS sends 512-byte packets even with
181
         * an MSS of 128
182
         */
183
        if (len < 576 * 2)
184
                len = 576 * 2;
185
 
186
        xbuff = (unsigned char *)kmalloc(len + 4, GFP_ATOMIC);
187
        rbuff = (unsigned char *)kmalloc(len + 4, GFP_ATOMIC);
188
 
189
        if (xbuff == NULL || rbuff == NULL)  {
190
                printk(KERN_ERR "%s: unable to grow ax25 buffers, MTU change cancelled.\n",
191
                       ax->dev->name);
192
                dev->mtu = ax->mtu;
193
                if (xbuff != NULL)
194
                        kfree(xbuff);
195
                if (rbuff != NULL)
196
                        kfree(rbuff);
197
                return;
198
        }
199
 
200
        save_flags(flags);
201
        cli();
202
 
203
        oxbuff    = ax->xbuff;
204
        ax->xbuff = xbuff;
205
        orbuff    = ax->rbuff;
206
        ax->rbuff = rbuff;
207
 
208
        if (ax->xleft) {
209
                if (ax->xleft <= len) {
210
                        memcpy(ax->xbuff, ax->xhead, ax->xleft);
211
                } else  {
212
                        ax->xleft = 0;
213
                        ax->tx_dropped++;
214
                }
215
        }
216
 
217
        ax->xhead = ax->xbuff;
218
 
219
        if (ax->rcount) {
220
                if (ax->rcount <= len) {
221
                        memcpy(ax->rbuff, orbuff, ax->rcount);
222
                } else  {
223
                        ax->rcount = 0;
224
                        ax->rx_over_errors++;
225
                        set_bit(AXF_ERROR, &ax->flags);
226
                }
227
        }
228
 
229
        ax->mtu      = dev->mtu + 73;
230
        ax->buffsize = len;
231
 
232
        restore_flags(flags);
233
 
234
        if (oxbuff != NULL)
235
                kfree(oxbuff);
236
        if (orbuff != NULL)
237
                kfree(orbuff);
238
}
239
 
240
 
241
/* Set the "sending" flag.  This must be atomic, hence the ASM. */
242
static inline void ax_lock(struct ax_disp *ax)
243
{
244
        if (set_bit(0, (void *)&ax->dev->tbusy))
245
                printk(KERN_ERR "%s: trying to lock already locked device!\n", ax->dev->name);
246
}
247
 
248
 
249
/* Clear the "sending" flag.  This must be atomic, hence the ASM. */
250
static inline void ax_unlock(struct ax_disp *ax)
251
{
252
        if (!clear_bit(0, (void *)&ax->dev->tbusy))
253
                printk(KERN_ERR "%s: trying to unlock already unlocked device!\n", ax->dev->name);
254
}
255
 
256
/* Send one completely decapsulated AX.25 packet to the AX.25 layer. */
257
static void ax_bump(struct ax_disp *ax)
258
{
259
        struct ax_disp *tmp_ax;
260
        struct sk_buff *skb;
261
        struct mkiss_channel *mkiss;
262
        int count;
263
 
264
        tmp_ax = ax;
265
 
266
        if (ax->rbuff[0] > 0x0f) {
267
                if (ax->mkiss != NULL) {
268
                        mkiss= ax->mkiss->tty->driver_data;
269
                        if (mkiss->magic == MKISS_DRIVER_MAGIC)
270
                                tmp_ax = ax->mkiss;
271
                }
272
        }
273
 
274
        count = ax->rcount;
275
 
276
        if ((skb = dev_alloc_skb(count)) == NULL) {
277
                printk(KERN_ERR "%s: memory squeeze, dropping packet.\n", ax->dev->name);
278
                ax->rx_dropped++;
279
                return;
280
        }
281
 
282
        skb->dev      = tmp_ax->dev;
283
        memcpy(skb_put(skb,count), ax->rbuff, count);
284
        skb->mac.raw  = skb->data;
285
        skb->protocol = htons(ETH_P_AX25);
286
        netif_rx(skb);
287
        tmp_ax->rx_packets++;
288
}
289
 
290
/* Encapsulate one AX.25 packet and stuff into a TTY queue. */
291
static void ax_encaps(struct ax_disp *ax, unsigned char *icp, int len)
292
{
293
        unsigned char *p;
294
        int actual, count;
295
        struct mkiss_channel *mkiss = ax->tty->driver_data;
296
 
297
        if (ax->mtu != ax->dev->mtu + 73)       /* Someone has been ifconfigging */
298
                ax_changedmtu(ax);
299
 
300
        if (len > ax->mtu) {            /* Sigh, shouldn't occur BUT ... */
301
                len = ax->mtu;
302
                printk(KERN_ERR "%s: truncating oversized transmit packet!\n", ax->dev->name);
303
                ax->tx_dropped++;
304
                ax_unlock(ax);
305
                return;
306
        }
307
 
308
        p = icp;
309
 
310
        if (mkiss->magic  != MKISS_DRIVER_MAGIC) {
311
                count = kiss_esc(p, (unsigned char *)ax->xbuff, len);
312
                ax->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
313
                actual = ax->tty->driver.write(ax->tty, 0, ax->xbuff, count);
314
                ax->tx_packets++;
315
                ax->dev->trans_start = jiffies;
316
                ax->xleft = count - actual;
317
                ax->xhead = ax->xbuff + actual;
318
        } else {
319
                count = kiss_esc(p, (unsigned char *) ax->mkiss->xbuff, len);
320
                ax->mkiss->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
321
                actual = ax->mkiss->tty->driver.write(ax->mkiss->tty, 0, ax->mkiss->xbuff, count);
322
                ax->tx_packets++;
323
                ax->mkiss->dev->trans_start = jiffies;
324
                ax->mkiss->xleft = count - actual;
325
                ax->mkiss->xhead = ax->mkiss->xbuff + actual;
326
        }
327
}
328
 
329
/*
330
 * Called by the driver when there's room for more data.  If we have
331
 * more packets to send, we send them here.
332
 */
333
static void ax25_write_wakeup(struct tty_struct *tty)
334
{
335
        int actual;
336
        struct ax_disp *ax = (struct ax_disp *)tty->disc_data;
337
        struct mkiss_channel *mkiss;
338
 
339
        /* First make sure we're connected. */
340
        if (ax == NULL || ax->magic != AX25_MAGIC || !ax->dev->start)
341
                return;
342
        if (ax->xleft <= 0)  {
343
                /* Now serial buffer is almost free & we can start
344
                 * transmission of another packet
345
                 */
346
                tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
347
 
348
                if (ax->mkiss != NULL) {
349
                        mkiss= ax->mkiss->tty->driver_data;
350
                        if (mkiss->magic  == MKISS_DRIVER_MAGIC)
351
                                ax_unlock(ax->mkiss);
352
                }
353
 
354
                ax_unlock(ax);
355
                mark_bh(NET_BH);
356
                return;
357
        }
358
 
359
        actual = tty->driver.write(tty, 0, ax->xhead, ax->xleft);
360
        ax->xleft -= actual;
361
        ax->xhead += actual;
362
}
363
 
364
/* Encapsulate an AX.25 packet and kick it into a TTY queue. */
365
static int ax_xmit(struct sk_buff *skb, struct device *dev)
366
{
367
        struct ax_disp *ax = (struct ax_disp*)dev->priv;
368
        struct mkiss_channel *mkiss = ax->tty->driver_data;
369
        struct ax_disp *tmp_ax;
370
 
371
        tmp_ax = NULL;
372
 
373
        if (mkiss->magic  == MKISS_DRIVER_MAGIC) {
374
                if (skb->data[0] < 0x10)
375
                        skb->data[0] = skb->data[0] + 0x10;
376
                tmp_ax = ax->mkiss;
377
        }
378
 
379
        if (!dev->start)  {
380
                printk(KERN_ERR "%s: xmit call when iface is down\n", dev->name);
381
                return 1;
382
        }
383
 
384
        if (tmp_ax != NULL)
385
                if (tmp_ax->dev->tbusy)
386
                        return 1;
387
 
388
        if (tmp_ax != NULL)
389
                if (dev->tbusy) {
390
                        printk(KERN_ERR "mkiss: dev busy while serial dev is free\n");
391
                        ax_unlock(ax);
392
                }
393
 
394
        if (dev->tbusy) {
395
                /*
396
                 * May be we must check transmitter timeout here ?
397
                 *      14 Oct 1994 Dmitry Gorodchanin.
398
                 */
399
                if (jiffies - dev->trans_start  < 20 * HZ) {
400
                        /* 20 sec timeout not reached */
401
                        return 1;
402
                }
403
 
404
                printk(KERN_ERR "%s: transmit timed out, %s?\n", dev->name,
405
                       (ax->tty->driver.chars_in_buffer(ax->tty) || ax->xleft) ?
406
                       "bad line quality" : "driver error");
407
 
408
                ax->xleft = 0;
409
                ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
410
                ax_unlock(ax);
411
        }
412
 
413
        /* We were not busy, so we are now... :-) */
414
        if (skb != NULL) {
415
                ax_lock(ax);
416
                if (tmp_ax != NULL)
417
                        ax_lock(tmp_ax);
418
                ax_encaps(ax, skb->data, skb->len);
419
                dev_kfree_skb(skb, FREE_WRITE);
420
        }
421
 
422
        return 0;
423
}
424
 
425
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
426
 
427
/* Return the frame type ID */
428
static int ax_header(struct sk_buff *skb, struct device *dev, unsigned short type,
429
          void *daddr, void *saddr, unsigned len)
430
{
431
#ifdef CONFIG_INET
432
        if (type != htons(ETH_P_AX25))
433
                return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
434
#endif
435
        return 0;
436
}
437
 
438
 
439
static int ax_rebuild_header(void *buff, struct device *dev, unsigned long raddr, struct sk_buff *skb)
440
{
441
#ifdef CONFIG_INET
442
        return ax25_rebuild_header(buff, dev, raddr, skb);
443
#else
444
        return 0;
445
#endif
446
}
447
 
448
#endif /* CONFIG_{AX25,AX25_MODULE} */
449
 
450
/* Open the low-level part of the AX25 channel. Easy! */
451
static int ax_open(struct device *dev)
452
{
453
        struct ax_disp *ax = (struct ax_disp*)dev->priv;
454
        unsigned long len;
455
 
456
        if (ax->tty == NULL)
457
                return -ENODEV;
458
 
459
        /*
460
         * Allocate the frame buffers:
461
         *
462
         * rbuff        Receive buffer.
463
         * xbuff        Transmit buffer.
464
         * cbuff        Temporary compression buffer.
465
         */
466
        len = dev->mtu * 2;
467
 
468
        /*
469
         * allow for arrival of larger UDP packets, even if we say not to
470
         * also fixes a bug in which SunOS sends 512-byte packets even with
471
         * an MSS of 128
472
         */
473
        if (len < 576 * 2)
474
                len = 576 * 2;
475
 
476
        if ((ax->rbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL)) == NULL)
477
                goto norbuff;
478
 
479
        if ((ax->xbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL)) == NULL)
480
                goto noxbuff;
481
 
482
        ax->mtu      = dev->mtu + 73;
483
        ax->buffsize = len;
484
        ax->rcount   = 0;
485
        ax->xleft    = 0;
486
 
487
        ax->flags   &= (1 << AXF_INUSE);      /* Clear ESCAPE & ERROR flags */
488
        /* Needed because address '0' is special */
489
        if (dev->pa_addr == 0)
490
                dev->pa_addr = ntohl(0xC0A80001);
491
        dev->tbusy  = 0;
492
        dev->start  = 1;
493
 
494
        return 0;
495
 
496
        /* Cleanup */
497
        kfree(ax->xbuff);
498
 
499
noxbuff:
500
        kfree(ax->rbuff);
501
 
502
norbuff:
503
        return -ENOMEM;
504
}
505
 
506
 
507
/* Close the low-level part of the AX25 channel. Easy! */
508
static int ax_close(struct device *dev)
509
{
510
        struct ax_disp *ax = (struct ax_disp*)dev->priv;
511
 
512
        if (ax->tty == NULL)
513
                return -EBUSY;
514
 
515
        ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
516
 
517
        dev->tbusy = 1;
518
        dev->start = 0;
519
 
520
        return 0;
521
}
522
 
523
static int ax25_receive_room(struct tty_struct *tty)
524
{
525
        return 65536;  /* We can handle an infinite amount of data. :-) */
526
}
527
 
528
/*
529
 * Handle the 'receiver data ready' interrupt.
530
 * This function is called by the 'tty_io' module in the kernel when
531
 * a block of data has been received, which can now be decapsulated
532
 * and sent on to the AX.25 layer for further processing.
533
 */
534
static void ax25_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
535
{
536
        struct ax_disp *ax = (struct ax_disp *)tty->disc_data;
537
 
538
        if (ax == NULL || ax->magic != AX25_MAGIC || !ax->dev->start)
539
                return;
540
 
541
        /*
542
         * Argh! mtu change time! - costs us the packet part received
543
         * at the change
544
         */
545
        if (ax->mtu != ax->dev->mtu + 73)
546
                ax_changedmtu(ax);
547
 
548
        /* Read the characters out of the buffer */
549
        while (count--) {
550
                if (fp != NULL && *fp++) {
551
                        if (!set_bit(AXF_ERROR, &ax->flags))
552
                                ax->rx_errors++;
553
                        cp++;
554
                        continue;
555
                }
556
 
557
                kiss_unesc(ax, *cp++);
558
        }
559
}
560
 
561
static int ax25_open(struct tty_struct *tty)
562
{
563
        struct ax_disp *ax = (struct ax_disp *)tty->disc_data;
564
        struct ax_disp *tmp_ax;
565
        struct mkiss_channel *mkiss;
566
        int err, cnt;
567
 
568
        /* First make sure we're not already connected. */
569
        if (ax && ax->magic == AX25_MAGIC)
570
                return -EEXIST;
571
 
572
        /* OK.  Find a free AX25 channel to use. */
573
        if ((ax = ax_alloc()) == NULL)
574
                return -ENFILE;
575
 
576
        ax->tty = tty;
577
        tty->disc_data = ax;
578
 
579
        ax->mkiss = NULL;
580
        tmp_ax    = NULL;
581
 
582
        if (tty->driver.flush_buffer)
583
                tty->driver.flush_buffer(tty);
584
        if (tty->ldisc.flush_buffer)
585
                tty->ldisc.flush_buffer(tty);
586
 
587
        /* Restore default settings */
588
        ax->dev->type = ARPHRD_AX25;
589
 
590
        /* Perform the low-level AX25 initialization. */
591
        if ((err = ax_open(ax->dev)))
592
                return err;
593
 
594
        mkiss= ax->tty->driver_data;
595
 
596
        if (mkiss->magic  == MKISS_DRIVER_MAGIC) {
597
                for (cnt = 1; cnt < ax25_maxdev; cnt++) {
598
                        if (ax25_ctrls[cnt]) {
599
                                if (ax25_ctrls[cnt]->dev.start) {
600
                                        if (ax == &ax25_ctrls[cnt]->ctrl) {
601
                                                cnt--;
602
                                                tmp_ax = &ax25_ctrls[cnt]->ctrl;
603
                                                break;
604
                                        }
605
                                }
606
                        }
607
                }
608
        }
609
 
610
        if (tmp_ax != NULL) {
611
                ax->mkiss     = tmp_ax;
612
                tmp_ax->mkiss = ax;
613
        }
614
 
615
        MOD_INC_USE_COUNT;
616
 
617
        /* Done.  We have linked the TTY line to a channel. */
618
        return ax->dev->base_addr;
619
}
620
 
621
static void ax25_close(struct tty_struct *tty)
622
{
623
        struct ax_disp *ax = (struct ax_disp *)tty->disc_data;
624
        int mkiss ;
625
 
626
        /* First make sure we're connected. */
627
        if (ax == NULL || ax->magic != AX25_MAGIC)
628
                return;
629
 
630
        mkiss = ax->mode;
631
        dev_close(ax->dev);
632
 
633
        tty->disc_data = 0;
634
        ax->tty        = NULL;
635
 
636
        /* VSV = very important to remove timers */
637
        ax_free(ax);
638
        unregister_netdev(ax->dev);
639
 
640
        MOD_DEC_USE_COUNT;
641
}
642
 
643
 
644
static struct enet_statistics *ax_get_stats(struct device *dev)
645
{
646
        static struct enet_statistics stats;
647
        struct ax_disp *ax = (struct ax_disp*)dev->priv;
648
 
649
        memset(&stats, 0, sizeof(struct enet_statistics));
650
 
651
        stats.rx_packets     = ax->rx_packets;
652
        stats.tx_packets     = ax->tx_packets;
653
        stats.rx_dropped     = ax->rx_dropped;
654
        stats.tx_dropped     = ax->tx_dropped;
655
        stats.tx_errors      = ax->tx_errors;
656
        stats.rx_errors      = ax->rx_errors;
657
        stats.rx_over_errors = ax->rx_over_errors;
658
 
659
        return &stats;
660
}
661
 
662
 
663
/************************************************************************
664
 *                         STANDARD ENCAPSULATION                        *
665
 ************************************************************************/
666
 
667
int kiss_esc(unsigned char *s, unsigned char *d, int len)
668
{
669
        unsigned char *ptr = d;
670
        unsigned char c;
671
 
672
        /*
673
         * Send an initial END character to flush out any
674
         * data that may have accumulated in the receiver
675
         * due to line noise.
676
         */
677
 
678
        *ptr++ = END;
679
 
680
        while (len-- > 0) {
681
                switch (c = *s++) {
682
                        case END:
683
                                *ptr++ = ESC;
684
                                *ptr++ = ESC_END;
685
                                break;
686
                        case ESC:
687
                                *ptr++ = ESC;
688
                                *ptr++ = ESC_ESC;
689
                                break;
690
                        default:
691
                                *ptr++ = c;
692
                                break;
693
                }
694
        }
695
 
696
        *ptr++ = END;
697
 
698
        return ptr - d;
699
}
700
 
701
static void kiss_unesc(struct ax_disp *ax, unsigned char s)
702
{
703
        switch (s) {
704
                case END:
705
                        /* drop keeptest bit = VSV */
706
                        if (test_bit(AXF_KEEPTEST, &ax->flags))
707
                                clear_bit(AXF_KEEPTEST, &ax->flags);
708
 
709
                        if (!clear_bit(AXF_ERROR, &ax->flags) && (ax->rcount > 2))
710
                                ax_bump(ax);
711
 
712
                        clear_bit(AXF_ESCAPE, &ax->flags);
713
                        ax->rcount = 0;
714
                        return;
715
 
716
                case ESC:
717
                        set_bit(AXF_ESCAPE, &ax->flags);
718
                        return;
719
                case ESC_ESC:
720
                        if (clear_bit(AXF_ESCAPE, &ax->flags))
721
                                s = ESC;
722
                        break;
723
                case ESC_END:
724
                        if (clear_bit(AXF_ESCAPE, &ax->flags))
725
                                s = END;
726
                        break;
727
        }
728
 
729
        if (!test_bit(AXF_ERROR, &ax->flags)) {
730
                if (ax->rcount < ax->buffsize) {
731
                        ax->rbuff[ax->rcount++] = s;
732
                        return;
733
                }
734
 
735
                ax->rx_over_errors++;
736
                set_bit(AXF_ERROR, &ax->flags);
737
        }
738
}
739
 
740
 
741
int ax_set_mac_address(struct device *dev, void *addr)
742
{
743
        int err;
744
 
745
        if ((err = verify_area(VERIFY_READ, addr, AX25_ADDR_LEN)) != 0)
746
                return err;
747
 
748
        /* addr is an AX.25 shifted ASCII mac address */
749
        memcpy_fromfs(dev->dev_addr, addr, AX25_ADDR_LEN);
750
 
751
        return 0;
752
}
753
 
754
static int ax_set_dev_mac_address(struct device *dev, void *addr)
755
{
756
        struct sockaddr *sa = addr;
757
 
758
        memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
759
 
760
        return 0;
761
}
762
 
763
 
764
/* Perform I/O control on an active ax25 channel. */
765
static int ax25_disp_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
766
{
767
        struct ax_disp *ax = (struct ax_disp *)tty->disc_data;
768
        int err;
769
        unsigned int tmp;
770
 
771
        /* First make sure we're connected. */
772
        if (ax == NULL || ax->magic != AX25_MAGIC)
773
                return -EINVAL;
774
 
775
        switch (cmd) {
776
                case SIOCGIFNAME:
777
                        if ((err = verify_area(VERIFY_WRITE, arg, strlen(ax->dev->name) + 1)) != 0)
778
                                return err;
779
                        memcpy_tofs(arg, ax->dev->name, strlen(ax->dev->name) + 1);
780
                        return 0;
781
 
782
                case SIOCGIFENCAP:
783
                        if ((err = verify_area(VERIFY_WRITE, arg, sizeof(int))) != 0)
784
                                return err;
785
                        put_user(4, (int *)arg);
786
                        return 0;
787
 
788
                case SIOCSIFENCAP:
789
                        if ((err = verify_area(VERIFY_READ, arg, sizeof(int))) != 0)
790
                                return err;
791
                        tmp = get_user((int *)arg);
792
                        ax->mode = tmp;
793
                        ax->dev->addr_len        = AX25_ADDR_LEN;         /* sizeof an AX.25 addr */
794
                        ax->dev->hard_header_len = AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
795
                        ax->dev->type            = ARPHRD_AX25;
796
                        return 0;
797
 
798
                 case SIOCSIFHWADDR:
799
                        return ax_set_mac_address(ax->dev, arg);
800
 
801
                default:
802
                        return -ENOIOCTLCMD;
803
        }
804
}
805
 
806
static int ax_open_dev(struct device *dev)
807
{
808
        struct ax_disp *ax = (struct ax_disp*)dev->priv;
809
 
810
        if (ax->tty==NULL)
811
                return -ENODEV;
812
 
813
        return 0;
814
}
815
 
816
/* Initialize AX25 control device -- register AX25 line discipline */
817
int mkiss_init_ctrl_dev(void)
818
{
819
        int status;
820
 
821
        if (ax25_maxdev < 4) ax25_maxdev = 4; /* Sanity */
822
 
823
        if ((ax25_ctrls = (ax25_ctrl_t **)kmalloc(sizeof(void*) * ax25_maxdev, GFP_KERNEL)) == NULL) {
824
                printk(KERN_ERR "mkiss: Can't allocate ax25_ctrls[] array !  No mkiss available\n");
825
                return -ENOMEM;
826
        }
827
 
828
        /* Clear the pointer array, we allocate devices when we need them */
829
        memset(ax25_ctrls, 0, sizeof(void*) * ax25_maxdev); /* Pointers */
830
 
831
        /* Fill in our line protocol discipline, and register it */
832
        memset(&ax_ldisc, 0, sizeof(ax_ldisc));
833
        ax_ldisc.magic  = TTY_LDISC_MAGIC;
834
        ax_ldisc.flags  = 0;
835
        ax_ldisc.open   = ax25_open;
836
        ax_ldisc.close  = ax25_close;
837
        ax_ldisc.read   = NULL;
838
        ax_ldisc.write  = NULL;
839
        ax_ldisc.ioctl  = (int (*)(struct tty_struct *, struct file *, unsigned int, unsigned long))ax25_disp_ioctl;
840
        ax_ldisc.select = NULL;
841
 
842
        ax_ldisc.receive_buf  = ax25_receive_buf;
843
        ax_ldisc.receive_room = ax25_receive_room;
844
        ax_ldisc.write_wakeup = ax25_write_wakeup;
845
 
846
        if ((status = tty_register_ldisc(N_AX25, &ax_ldisc)) != 0)
847
                printk(KERN_ERR "mkiss: can't register line discipline (err = %d)\n", status);
848
 
849
        mkiss_init();
850
 
851
#ifdef MODULE
852
        return status;
853
#else
854
        /*
855
         * Return "not found", so that dev_init() will unlink
856
         * the placeholder device entry for us.
857
         */
858
        return ENODEV;
859
#endif
860
}
861
 
862
 
863
/* Initialize the driver.  Called by network startup. */
864
 
865
static int ax25_init(struct device *dev)
866
{
867
        struct ax_disp *ax = (struct ax_disp*)dev->priv;
868
        int i;
869
 
870
        static char ax25_bcast[AX25_ADDR_LEN] =
871
                {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
872
        static char ax25_test[AX25_ADDR_LEN] =
873
                {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
874
 
875
        if (ax == NULL)         /* Allocation failed ?? */
876
                return -ENODEV;
877
 
878
        /* Set up the "AX25 Control Block". (And clear statistics) */
879
        memset(ax, 0, sizeof (struct ax_disp));
880
        ax->magic  = AX25_MAGIC;
881
        ax->dev    = dev;
882
 
883
        /* Finish setting up the DEVICE info. */
884
        dev->mtu             = AX_MTU;
885
        dev->hard_start_xmit = ax_xmit;
886
        dev->open            = ax_open_dev;
887
        dev->stop            = ax_close;
888
        dev->get_stats       = ax_get_stats;
889
#ifdef HAVE_SET_MAC_ADDR
890
        dev->set_mac_address = ax_set_dev_mac_address;
891
#endif
892
        dev->hard_header_len = 0;
893
        dev->addr_len        = 0;
894
        dev->type            = ARPHRD_AX25;
895
        dev->tx_queue_len    = 10;
896
 
897
        memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
898
        memcpy(dev->dev_addr,  ax25_test,  AX25_ADDR_LEN);
899
 
900
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
901
        dev->hard_header     = ax_header;
902
        dev->rebuild_header  = ax_rebuild_header;
903
#endif
904
 
905
        for (i = 0; i < DEV_NUMBUFFS; i++)
906
                skb_queue_head_init(&dev->buffs[i]);
907
 
908
        /* New-style flags. */
909
        dev->flags      = 0;
910
        dev->family     = AF_INET;
911
 
912
#ifdef CONFIG_INET
913
        dev->pa_addr    = in_aton("192.168.0.1");
914
        dev->pa_brdaddr = in_aton("192.168.0.255");
915
        dev->pa_mask    = in_aton("255.255.255.0");
916
        dev->pa_alen    = 4;
917
#endif
918
 
919
        return 0;
920
}
921
 
922
static int mkiss_open(struct tty_struct *tty, struct file *filp)
923
{
924
        struct mkiss_channel *mkiss;
925
        int chan;
926
 
927
        chan = MINOR(tty->device) - tty->driver.minor_start;
928
 
929
        if (chan < 0 || chan >= NR_MKISS)
930
                return -ENODEV;
931
 
932
        mkiss = &MKISS_Info[chan];
933
 
934
        mkiss->magic =  MKISS_DRIVER_MAGIC;
935
        mkiss->init  = 1;
936
        mkiss->tty   = tty;
937
 
938
        tty->driver_data = mkiss;
939
 
940
        tty->termios->c_iflag  = IGNBRK | IGNPAR;
941
        tty->termios->c_cflag  = B9600 | CS8 | CLOCAL;
942
        tty->termios->c_cflag &= ~CBAUD;
943
 
944
        return 0;
945
}
946
 
947
static void mkiss_close(struct tty_struct *tty, struct file * filp)
948
{
949
        struct mkiss_channel *mkiss = tty->driver_data;
950
 
951
        if (mkiss == NULL || mkiss->magic != MKISS_DRIVER_MAGIC)
952
                return;
953
 
954
        mkiss->tty   = NULL;
955
        mkiss->init  = 0;
956
        tty->stopped = 0;
957
}
958
 
959
static int mkiss_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
960
{
961
        return 0;
962
}
963
 
964
static int mkiss_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
965
{
966
        /* Ignore serial ioctl's */
967
        switch (cmd) {
968
                case TCSBRK:
969
                case TIOCMGET:
970
                case TIOCMBIS:
971
                case TIOCMBIC:
972
                case TIOCMSET:
973
                case TCSETS:
974
                case TCSETSF:           /* should flush first, but... */
975
                case TCSETSW:           /* should wait until flush, but... */
976
                        return 0;
977
                default:
978
                        return -ENOIOCTLCMD;
979
        }
980
}
981
 
982
 
983
static void mkiss_dummy(struct tty_struct *tty)
984
{
985
        struct mkiss_channel *mkiss = tty->driver_data;
986
 
987
        if (tty == NULL)
988
                return;
989
 
990
        if (mkiss == NULL)
991
                return;
992
}
993
 
994
static void mkiss_dummy2(struct tty_struct *tty, unsigned char ch)
995
{
996
        struct mkiss_channel *mkiss = tty->driver_data;
997
 
998
        if (tty == NULL)
999
                return;
1000
 
1001
        if (mkiss == NULL)
1002
                return;
1003
}
1004
 
1005
 
1006
static int mkiss_write_room(struct tty_struct * tty)
1007
{
1008
        struct mkiss_channel *mkiss = tty->driver_data;
1009
 
1010
        if (tty == NULL)
1011
                return 0;
1012
 
1013
        if (mkiss == NULL)
1014
                return 0;
1015
 
1016
        return 65536;  /* We can handle an infinite amount of data. :-) */
1017
}
1018
 
1019
 
1020
static int mkiss_chars_in_buffer(struct tty_struct *tty)
1021
{
1022
        struct mkiss_channel *mkiss = tty->driver_data;
1023
 
1024
        if (tty == NULL)
1025
                return 0;
1026
 
1027
        if (mkiss == NULL)
1028
                return 0;
1029
 
1030
        return 0;
1031
}
1032
 
1033
 
1034
static void mkiss_set_termios(struct tty_struct *tty, struct termios *old_termios)
1035
{
1036
        /* we don't do termios */
1037
}
1038
 
1039
/* ******************************************************************** */
1040
/* *                    Init MKISS driver                             * */
1041
/* ******************************************************************** */
1042
 
1043
static int mkiss_init(void)
1044
{
1045
        memset(&mkiss_driver, 0, sizeof(struct tty_driver));
1046
 
1047
        mkiss_driver.magic       = MKISS_DRIVER_MAGIC;
1048
        mkiss_driver.name        = "mkiss";
1049
        mkiss_driver.major       = MKISS_MAJOR;
1050
        mkiss_driver.minor_start = 0;
1051
        mkiss_driver.num         = NR_MKISS;
1052
        mkiss_driver.type        = TTY_DRIVER_TYPE_SERIAL;
1053
        mkiss_driver.subtype     = MKISS_SERIAL_TYPE_NORMAL;    /* not needed */
1054
 
1055
        mkiss_driver.init_termios         = tty_std_termios;
1056
        mkiss_driver.init_termios.c_iflag = IGNBRK | IGNPAR;
1057
        mkiss_driver.init_termios.c_cflag = B9600 | CS8 | CLOCAL;
1058
 
1059
        mkiss_driver.flags           = TTY_DRIVER_REAL_RAW;
1060
        mkiss_driver.refcount        = &mkiss_refcount;
1061
        mkiss_driver.table           = mkiss_table;
1062
        mkiss_driver.termios         = (struct termios **)mkiss_termios;
1063
        mkiss_driver.termios_locked  = (struct termios **)mkiss_termios_locked;
1064
 
1065
        mkiss_driver.ioctl           = mkiss_ioctl;
1066
        mkiss_driver.open            = mkiss_open;
1067
        mkiss_driver.close           = mkiss_close;
1068
        mkiss_driver.write           = mkiss_write;
1069
        mkiss_driver.write_room      = mkiss_write_room;
1070
        mkiss_driver.chars_in_buffer = mkiss_chars_in_buffer;
1071
        mkiss_driver.set_termios     = mkiss_set_termios;
1072
 
1073
        /* some unused functions */
1074
        mkiss_driver.flush_buffer = mkiss_dummy;
1075
        mkiss_driver.throttle     = mkiss_dummy;
1076
        mkiss_driver.unthrottle   = mkiss_dummy;
1077
        mkiss_driver.stop         = mkiss_dummy;
1078
        mkiss_driver.start        = mkiss_dummy;
1079
        mkiss_driver.hangup       = mkiss_dummy;
1080
        mkiss_driver.flush_chars  = mkiss_dummy;
1081
        mkiss_driver.put_char     = mkiss_dummy2;
1082
 
1083
        if (tty_register_driver(&mkiss_driver)) {
1084
                printk(KERN_ERR "Couldn't register Mkiss device\n");
1085
                return -EIO;
1086
        }
1087
 
1088
        printk(KERN_INFO "AX.25 Multikiss device enabled\n");
1089
 
1090
        return 0;
1091
}
1092
 
1093
#ifdef MODULE
1094
 
1095
int init_module(void)
1096
{
1097
        register_symtab(NULL);
1098
 
1099
        return mkiss_init_ctrl_dev();
1100
}
1101
 
1102
void cleanup_module(void)
1103
{
1104
        int i;
1105
 
1106
        if (ax25_ctrls != NULL) {
1107
                for (i = 0; i < ax25_maxdev; i++) {
1108
                        if (ax25_ctrls[i]) {
1109
                                /*
1110
                                 * VSV = if dev->start==0, then device
1111
                                 * unregistred while close proc.
1112
                                 */
1113
                                if (ax25_ctrls[i]->dev.start)
1114
                                        unregister_netdev(&(ax25_ctrls[i]->dev));
1115
 
1116
                                kfree(ax25_ctrls[i]);
1117
                                ax25_ctrls[i] = NULL;
1118
                        }
1119
                }
1120
 
1121
                kfree(ax25_ctrls);
1122
                ax25_ctrls = NULL;
1123
        }
1124
 
1125
        if ((i = tty_register_ldisc(N_AX25, NULL)))
1126
                printk(KERN_ERR "mkiss: can't unregister line discipline (err = %d)\n", i);
1127
 
1128
        if (tty_unregister_driver(&mkiss_driver))       /* remove devive */
1129
                printk(KERN_ERR "mkiss: can't unregister MKISS device\n");
1130
}
1131
 
1132
#endif /* MODULE */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.