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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [isdn/] [pcbit/] [drv.c] - Blame information for rev 1765

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * Copyright (C) 1996 Universidade de Lisboa
3
 *
4
 * Written by Pedro Roque Marques (roque@di.fc.ul.pt)
5
 *
6
 * This software may be used and distributed according to the terms of
7
 * the GNU Public License, incorporated herein by reference.
8
 */
9
 
10
/*
11
 *        PCBIT-D interface with isdn4linux
12
 */
13
 
14
/*
15
 *      Fixes:
16
 *
17
 *      Nuno Grilo      <l38486@alfa.ist.utl.pt>
18
 *      fixed msn_list NULL pointer dereference.
19
 *
20
 */
21
 
22
#define __NO_VERSION__
23
 
24
#include <linux/module.h>
25
 
26
#include <linux/sched.h>
27
 
28
#include <linux/kernel.h>
29
 
30
#include <linux/types.h>
31
#include <linux/malloc.h>
32
#include <linux/mm.h>
33
#include <linux/interrupt.h>
34
#include <linux/string.h>
35
#include <linux/skbuff.h>
36
 
37
#include <linux/isdnif.h>
38
#include <asm/string.h>
39
#include <asm/io.h>
40
 
41
#include "pcbit.h"
42
#include "edss1.h"
43
#include "layer2.h"
44
#include "capi.h"
45
 
46
 
47
extern ushort last_ref_num;
48
 
49
static int pcbit_ioctl(isdn_ctrl* ctl);
50
 
51
static char* pcbit_devname[MAX_PCBIT_CARDS] = {
52
        "pcbit0",
53
        "pcbit1",
54
        "pcbit2",
55
        "pcbit3"
56
};
57
 
58
/*
59
 * prototypes
60
 */
61
 
62
int pcbit_command(isdn_ctrl* ctl);
63
int pcbit_stat(u_char* buf, int len, int user, int, int);
64
int pcbit_xmit(int driver, int chan, struct sk_buff *skb);
65
int pcbit_writecmd(const u_char*, int, int, int, int);
66
 
67
static int set_protocol_running(struct pcbit_dev * dev);
68
 
69
static void pcbit_clear_msn(struct pcbit_dev *dev);
70
static void pcbit_set_msn(struct pcbit_dev *dev, char *list);
71
static int pcbit_check_msn(struct pcbit_dev *dev, char *msn);
72
 
73
 
74
extern void pcbit_deliver(void * data);
75
 
76
int pcbit_init_dev(int board, int mem_base, int irq)
77
{
78
        struct pcbit_dev *dev;
79
        isdn_if *dev_if;
80
 
81
        if ((dev=kmalloc(sizeof(struct pcbit_dev), GFP_KERNEL)) == NULL)
82
        {
83
                printk("pcbit_init: couldn't malloc pcbit_dev struct\n");
84
                return -ENOMEM;
85
        }
86
 
87
        dev_pcbit[board] = dev;
88
        memset(dev, 0, sizeof(struct pcbit_dev));
89
 
90
        if (mem_base >= 0xA0000 && mem_base <= 0xFFFFF )
91
                dev->sh_mem = (unsigned char*) mem_base;
92
        else
93
        {
94
                printk("memory address invalid");
95
                kfree(dev);
96
                dev_pcbit[board] = NULL;
97
                return -EACCES;
98
        }
99
 
100
        dev->b1 = kmalloc(sizeof(struct pcbit_chan), GFP_KERNEL);
101
        if (!dev->b1) {
102
                printk("pcbit_init: couldn't malloc pcbit_chan struct\n");
103
                kfree(dev);
104
                return -ENOMEM;
105
        }
106
 
107
        dev->b2 = kmalloc(sizeof(struct pcbit_chan), GFP_KERNEL);
108
        if (!dev->b2) {
109
                printk("pcbit_init: couldn't malloc pcbit_chan struct\n");
110
                kfree(dev->b1);
111
                kfree(dev);
112
                return -ENOMEM;
113
        }
114
 
115
        memset(dev->b1, 0, sizeof(struct pcbit_chan));
116
        memset(dev->b2, 0, sizeof(struct pcbit_chan));
117
        dev->b2->id = 1;
118
 
119
 
120
        dev->qdelivery.next = NULL;
121
        dev->qdelivery.sync = 0;
122
        dev->qdelivery.routine = pcbit_deliver;
123
        dev->qdelivery.data = dev;
124
 
125
        /*
126
         *  interrupts
127
         */
128
 
129
        if (request_irq(irq, &pcbit_irq_handler, 0, pcbit_devname[board], dev) != 0)
130
        {
131
                kfree(dev->b1);
132
                kfree(dev->b2);
133
                kfree(dev);
134
                dev_pcbit[board] = NULL;
135
                return -EIO;
136
        }
137
 
138
        dev->irq = irq;
139
 
140
        /* next frame to be received */
141
        dev->rcv_seq = 0;
142
        dev->send_seq = 0;
143
        dev->unack_seq = 0;
144
 
145
        dev->hl_hdrlen = 10;
146
 
147
        dev_if = kmalloc(sizeof(isdn_if), GFP_KERNEL);
148
 
149
        if (!dev_if) {
150
                free_irq(irq, dev);
151
                kfree(dev->b1);
152
                kfree(dev->b2);
153
                kfree(dev);
154
                dev_pcbit[board] = NULL;
155
                return -EIO;
156
        }
157
 
158
        dev->dev_if = dev_if;
159
 
160
        dev_if->channels = 2;
161
 
162
 
163
        dev_if->features = (ISDN_FEATURE_P_EURO  | ISDN_FEATURE_L3_TRANS |
164
                            ISDN_FEATURE_L2_HDLC | ISDN_FEATURE_L2_TRANS );
165
 
166
        dev_if->writebuf_skb = pcbit_xmit;
167
        dev_if->writebuf  = NULL;
168
        dev_if->hl_hdrlen = 10;
169
 
170
        dev_if->maxbufsize = MAXBUFSIZE;
171
        dev_if->command  = pcbit_command;
172
 
173
        dev_if->writecmd = pcbit_writecmd;
174
        dev_if->readstat = pcbit_stat;
175
 
176
 
177
        strcpy(dev_if->id, pcbit_devname[board]);
178
 
179
        if (!register_isdn(dev_if)) {
180
                free_irq(irq, dev);
181
                kfree(dev->b1);
182
                kfree(dev->b2);
183
                kfree(dev);
184
                dev_pcbit[board] = NULL;
185
                return -EIO;
186
        }
187
 
188
        dev->id = dev_if->channels;
189
 
190
 
191
        dev->l2_state = L2_DOWN;
192
        dev->free = 511;
193
 
194
        /*
195
         * set_protocol_running(dev);
196
         */
197
 
198
        return 0;
199
}
200
 
201
#ifdef MODULE
202
void pcbit_terminate(int board)
203
{
204
        struct pcbit_dev * dev;
205
 
206
        dev = dev_pcbit[board];
207
 
208
        if (dev) {
209
             /* unregister_isdn(dev->dev_if); */
210
                free_irq(dev->irq, dev);
211
                pcbit_clear_msn(dev);
212
                kfree(dev->dev_if);
213
                if (dev->b1->fsm_timer.function)
214
                        del_timer(&dev->b1->fsm_timer);
215
                if (dev->b2->fsm_timer.function)
216
                        del_timer(&dev->b2->fsm_timer);
217
                kfree(dev->b1);
218
                kfree(dev->b2);
219
                kfree(dev);
220
        }
221
}
222
#endif
223
 
224
int pcbit_command(isdn_ctrl* ctl)
225
{
226
        struct pcbit_dev  *dev;
227
        struct pcbit_chan *chan;
228
        struct callb_data info;
229
 
230
        dev = finddev(ctl->driver);
231
 
232
        if (!dev)
233
        {
234
                printk("pcbit_command: unknown device\n");
235
                return -1;
236
        }
237
 
238
        chan = (ctl->arg & 0x0F) ? dev->b2 : dev->b1;
239
 
240
 
241
        switch(ctl->command) {
242
        case ISDN_CMD_IOCTL:
243
                return pcbit_ioctl(ctl);
244
                break;
245
        case ISDN_CMD_DIAL:
246
                info.type = EV_USR_SETUP_REQ;
247
                info.data.setup.CalledPN = (char *) &ctl->parm.setup.phone;
248
                pcbit_fsm_event(dev, chan, EV_USR_SETUP_REQ, &info);
249
                break;
250
        case ISDN_CMD_ACCEPTD:
251
                pcbit_fsm_event(dev, chan, EV_USR_SETUP_RESP, NULL);
252
                break;
253
        case ISDN_CMD_ACCEPTB:
254
                printk("ISDN_CMD_ACCEPTB - not really needed\n");
255
                break;
256
        case ISDN_CMD_HANGUP:
257
                pcbit_fsm_event(dev, chan, EV_USR_RELEASE_REQ, NULL);
258
                break;
259
        case ISDN_CMD_SETL2:
260
                chan->proto = (ctl->arg >> 8);
261
                break;
262
        case ISDN_CMD_GETL2:
263
                return chan->proto;
264
                break;
265
        case ISDN_CMD_LOCK:
266
                MOD_INC_USE_COUNT;
267
                break;
268
        case ISDN_CMD_UNLOCK:
269
                MOD_DEC_USE_COUNT;
270
                break;
271
        case ISDN_CMD_CLREAZ:
272
                pcbit_clear_msn(dev);
273
                break;
274
        case ISDN_CMD_SETEAZ:
275
                pcbit_set_msn(dev, ctl->parm.num);
276
                break;
277
        case ISDN_CMD_SETL3:
278
                if ((ctl->arg >> 8) != ISDN_PROTO_L3_TRANS)
279
                        printk(KERN_DEBUG "L3 protocol unknown\n");
280
                break;
281
        case ISDN_CMD_GETL3:
282
                return ISDN_PROTO_L3_TRANS;
283
                break;
284
        case ISDN_CMD_GETEAZ:
285
        case ISDN_CMD_SETSIL:
286
        case ISDN_CMD_GETSIL:
287
                printk(KERN_DEBUG "pcbit_command: code %d not implemented yet\n", ctl->command);
288
                break;
289
        default:
290
                printk(KERN_DEBUG "pcbit_command: unknown command\n");
291
                break;
292
        };
293
 
294
        return 0;
295
}
296
 
297
/*
298
 * Another Hack :-(
299
 * on some conditions the board stops sending TDATA_CONFs
300
 * let's see if we can turn around the problem
301
 */
302
 
303
#ifdef BLOCK_TIMER
304
static void pcbit_block_timer(unsigned long data)
305
{
306
        struct pcbit_chan *chan;
307
        struct pcbit_dev * dev;
308
        isdn_ctrl ictl;
309
 
310
        chan = (struct pcbit_chan *) data;
311
 
312
        dev = chan2dev(chan);
313
 
314
        if (dev == NULL) {
315
                printk(KERN_DEBUG "pcbit: chan2dev failed\n");
316
                return;
317
        }
318
 
319
        del_timer(&chan->block_timer);
320
        chan->block_timer.function = NULL;
321
 
322
#ifdef DEBUG
323
        printk(KERN_DEBUG "pcbit_block_timer\n");
324
#endif  
325
        chan->queued = 0;
326
        ictl.driver = dev->id;
327
        ictl.command = ISDN_STAT_BSENT;
328
        ictl.arg = chan->id;
329
        dev->dev_if->statcallb(&ictl);
330
}
331
#endif
332
 
333
int pcbit_xmit(int driver, int chnum, struct sk_buff *skb)
334
{
335
        ushort hdrlen;
336
        int refnum, len;
337
        struct pcbit_chan * chan;
338
        struct pcbit_dev *dev;
339
 
340
        dev = finddev(driver);
341
        if (dev == NULL)
342
        {
343
                printk("finddev returned NULL");
344
                return -1;
345
        }
346
 
347
        chan = chnum ? dev->b2 : dev->b1;
348
 
349
 
350
        if (chan->fsm_state != ST_ACTIVE)
351
                return -1;
352
 
353
        if (chan->queued >= MAX_QUEUED )
354
        {
355
#ifdef DEBUG_QUEUE
356
                printk(KERN_DEBUG
357
                       "pcbit: %d packets already in queue - write fails\n",
358
                       chan->queued);
359
#endif
360
                /*
361
                 * packet stays on the head of the device queue
362
                 * since dev_start_xmit will fail
363
                 * see net/core/dev.c
364
                 */
365
#ifdef BLOCK_TIMER
366
                if (chan->block_timer.function == NULL) {
367
                        init_timer(&chan->block_timer);
368
                        chan->block_timer.function =  &pcbit_block_timer;
369
                        chan->block_timer.data = (long) chan;
370
                        chan->block_timer.expires = jiffies + 1 * HZ;
371
                        add_timer(&chan->block_timer);
372
                }
373
#endif          
374
                return 0;
375
        }
376
 
377
 
378
        chan->queued++;
379
 
380
        len = skb->len;
381
 
382
        hdrlen = capi_tdata_req(chan, skb);
383
 
384
        refnum = last_ref_num++ & 0x7fffU;
385
        chan->s_refnum = refnum;
386
 
387
        pcbit_l2_write(dev, MSG_TDATA_REQ, refnum, skb, hdrlen);
388
 
389
        return len;
390
}
391
 
392
 
393
int pcbit_writecmd(const u_char* buf, int len, int user, int driver, int channel)
394
{
395
        struct pcbit_dev * dev;
396
        int i, j;
397
        const u_char * loadbuf;
398
        u_char * ptr = NULL;
399
 
400
        int errstat;
401
 
402
        dev = finddev(driver);
403
 
404
        if (!dev)
405
        {
406
                printk("pcbit_writecmd: couldn't find device");
407
                return -ENODEV;
408
        }
409
 
410
        switch(dev->l2_state) {
411
        case L2_LWMODE:
412
                /* check (size <= rdp_size); write buf into board */
413
                if (len > BANK4 + 1)
414
                {
415
                        printk("pcbit_writecmd: invalid length %d\n", len);
416
                        return -EFAULT;
417
                }
418
 
419
                if (user)
420
                {
421
                        u_char cbuf[1024];
422
 
423
                        copy_from_user(cbuf, buf, len);
424
                        for (i=0; i<len; i++)
425
                                writeb(cbuf[i], dev->sh_mem + i);
426
                }
427
                else
428
                        memcpy_toio(dev->sh_mem, buf, len);
429
                return len;
430
                break;
431
        case L2_FWMODE:
432
                /* this is the hard part */
433
                /* dumb board */
434
                if (len < 0)
435
                        return -EINVAL;
436
 
437
                if (user) {
438
                        /* get it into kernel space */
439
                        if ((ptr = kmalloc(len, GFP_KERNEL))==NULL)
440
                                return -ENOMEM;
441
                        copy_from_user(ptr, buf, len);
442
                        loadbuf = ptr;
443
                }
444
                else
445
                        loadbuf = buf;
446
 
447
                errstat = 0;
448
 
449
                for (i=0; i < len; i++)
450
                {
451
                        for(j=0; j < LOAD_RETRY; j++)
452
                                if (!(readb(dev->sh_mem + dev->loadptr)))
453
                                        break;
454
 
455
                        if (j == LOAD_RETRY)
456
                        {
457
                                errstat = -ETIME;
458
                                printk("TIMEOUT i=%d\n", i);
459
                                break;
460
                        }
461
                        writeb(loadbuf[i], dev->sh_mem + dev->loadptr + 1);
462
                        writeb(0x01, dev->sh_mem + dev->loadptr);
463
 
464
                        dev->loadptr += 2;
465
                        if (dev->loadptr > LOAD_ZONE_END)
466
                                dev->loadptr = LOAD_ZONE_START;
467
                }
468
 
469
                if (user)
470
                        kfree(ptr);
471
 
472
                return errstat ? errstat : len;
473
 
474
                break;
475
        default:
476
                return -EBUSY;
477
        }
478
        return 0;
479
}
480
 
481
/*
482
 *  demultiplexing of messages
483
 *
484
 */
485
 
486
void pcbit_l3_receive(struct pcbit_dev * dev, ulong msg,
487
                             struct sk_buff * skb,
488
                             ushort hdr_len, ushort refnum)
489
{
490
        struct pcbit_chan *chan;
491
        struct sk_buff *skb2;
492
        unsigned short len;
493
        struct callb_data cbdata;
494
        int complete, err;
495
        isdn_ctrl ictl;
496
#ifdef DEBUG
497
        struct msg_fmt * fmsg;
498
#endif
499
 
500
        switch(msg) {
501
 
502
        case MSG_TDATA_IND:
503
                if (!(chan = capi_channel(dev, skb))) {
504
                        printk(KERN_WARNING
505
                               "CAPI header: unknown channel id\n");
506
                        break;
507
                }
508
                chan->r_refnum = skb->data[7];
509
                skb_pull(skb, 8);
510
 
511
                dev->dev_if->rcvcallb_skb(dev->id, chan->id, skb);
512
 
513
                if (capi_tdata_resp(chan, &skb2) > 0)
514
                        pcbit_l2_write(dev, MSG_TDATA_RESP, refnum,
515
                                       skb2, skb2->len);
516
                return;
517
                break;
518
        case MSG_TDATA_CONF:
519
                if (!(chan = capi_channel(dev, skb))) {
520
                        printk(KERN_WARNING
521
                               "CAPI header: unknown channel id\n");
522
                        break;
523
                }
524
 
525
#ifdef DEBUG
526
                if ( (*((ushort *) (skb->data + 2) )) != 0) {
527
                        printk(KERN_DEBUG "TDATA_CONF error\n");
528
                }
529
#endif
530
#ifdef BLOCK_TIMER
531
                if (chan->queued == MAX_QUEUED) {
532
                        del_timer(&chan->block_timer);
533
                        chan->block_timer.function = NULL;
534
                }
535
 
536
#endif          
537
                chan->queued--;
538
 
539
                ictl.driver = dev->id;
540
                ictl.command = ISDN_STAT_BSENT;
541
                ictl.arg = chan->id;
542
                dev->dev_if->statcallb(&ictl);
543
                break;
544
 
545
        case MSG_CONN_IND:
546
                /*
547
                 *  channel: 1st not used will do
548
                 *           if both are used we're in trouble
549
                 */
550
 
551
                if (!dev->b1->fsm_state)
552
                        chan = dev->b1;
553
                else if (!dev->b2->fsm_state)
554
                        chan = dev->b2;
555
                else {
556
                        printk(KERN_INFO
557
                               "Incoming connection: no channels available");
558
 
559
                        if ((len = capi_disc_req(*(ushort*)(skb->data), &skb2, CAUSE_NOCHAN)) > 0)
560
                                pcbit_l2_write(dev, MSG_DISC_REQ, refnum, skb2, len);
561
                        break;
562
                }
563
 
564
                cbdata.data.setup.CalledPN = NULL;
565
                cbdata.data.setup.CallingPN = NULL;
566
 
567
                capi_decode_conn_ind(chan, skb, &cbdata);
568
                cbdata.type = EV_NET_SETUP;
569
 
570
                pcbit_fsm_event(dev, chan, EV_NET_SETUP, NULL);
571
 
572
                if (pcbit_check_msn(dev, cbdata.data.setup.CallingPN))
573
                        pcbit_fsm_event(dev, chan, EV_USR_PROCED_REQ, &cbdata);
574
                else
575
                        pcbit_fsm_event(dev, chan, EV_USR_RELEASE_REQ, NULL);
576
 
577
                if (cbdata.data.setup.CalledPN)
578
                        kfree(cbdata.data.setup.CalledPN);
579
                if (cbdata.data.setup.CallingPN)
580
                        kfree(cbdata.data.setup.CallingPN);
581
                break;
582
 
583
        case MSG_CONN_CONF:
584
                /*
585
                 * We should be able to find the channel by the message
586
                 * reference number. The current version of the firmware
587
                 * doesn't sent the ref number correctly.
588
                 */
589
#ifdef DEBUG
590
                printk(KERN_DEBUG "refnum=%04x b1=%04x b2=%04x\n", refnum,
591
                       dev->b1->s_refnum,
592
                       dev->b2->s_refnum);
593
#endif
594
#if 0   
595
                if (dev->b1->s_refnum == refnum)
596
                        chan = dev->b1;
597
                else {
598
 
599
                        if (dev->b2->s_refnum == refnum)
600
                                chan = dev->b2;
601
                        else {
602
                                chan = NULL;
603
                                printk(KERN_WARNING "Connection Confirm - refnum doesn't match chan\n");
604
                                break;
605
                        }
606
                }
607
#else
608
                /* We just try to find a channel in the right state */
609
 
610
                if (dev->b1->fsm_state == ST_CALL_INIT)
611
                        chan = dev->b1;
612
                else {
613
                        if (dev->b2->s_refnum == ST_CALL_INIT)
614
                                chan = dev->b2;
615
                        else {
616
                                chan = NULL;
617
                                printk(KERN_WARNING "Connection Confirm - no channel in Call Init state\n");
618
                                break;
619
                        }
620
                }
621
#endif
622
                if (capi_decode_conn_conf(chan, skb, &complete)) {
623
                        printk(KERN_DEBUG "conn_conf indicates error\n");
624
                        pcbit_fsm_event(dev, chan, EV_ERROR, NULL);
625
                }
626
                else
627
                        if (complete)
628
                                pcbit_fsm_event(dev, chan, EV_NET_CALL_PROC, NULL);
629
                        else
630
                                pcbit_fsm_event(dev, chan, EV_NET_SETUP_ACK, NULL);
631
                break;
632
        case MSG_CONN_ACTV_IND:
633
 
634
                if (!(chan = capi_channel(dev, skb))) {
635
                        printk(KERN_WARNING
636
                               "CAPI header: unknown channel id\n");
637
                        break;
638
                }
639
 
640
                if (capi_decode_conn_actv_ind(chan, skb)) {
641
                        printk("error in capi_decode_conn_actv_ind\n");
642
                     /* pcbit_fsm_event(dev, chan, EV_ERROR, NULL); */
643
                        break;
644
                }
645
                chan->r_refnum = refnum;
646
                pcbit_fsm_event(dev, chan, EV_NET_CONN, NULL);
647
                break;
648
        case MSG_CONN_ACTV_CONF:
649
 
650
                if (!(chan = capi_channel(dev, skb))) {
651
                        printk(KERN_WARNING
652
                               "CAPI header: unknown channel id\n");
653
                        break;
654
                }
655
 
656
                if (capi_decode_conn_actv_conf(chan, skb) == 0)
657
                        pcbit_fsm_event(dev, chan, EV_NET_CONN_ACK, NULL);
658
 
659
                else
660
                        printk(KERN_DEBUG "decode_conn_actv_conf failed\n");
661
                break;
662
 
663
        case  MSG_SELP_CONF:
664
 
665
                if (!(chan = capi_channel(dev, skb))) {
666
                        printk(KERN_WARNING
667
                               "CAPI header: unknown channel id\n");
668
                        break;
669
                }
670
 
671
                if (!(err = capi_decode_sel_proto_conf(chan, skb)))
672
                        pcbit_fsm_event(dev, chan, EV_NET_SELP_RESP, NULL);
673
                else {
674
                        /* Error */
675
                        printk("error %d - capi_decode_sel_proto_conf\n", err);
676
                }
677
                break;
678
        case MSG_ACT_TRANSP_CONF:
679
                if (!(chan = capi_channel(dev, skb))) {
680
                        printk(KERN_WARNING
681
                               "CAPI header: unknown channel id\n");
682
                        break;
683
                }
684
 
685
                if (!capi_decode_actv_trans_conf(chan, skb))
686
                        pcbit_fsm_event(dev, chan, EV_NET_ACTV_RESP, NULL);
687
                break;
688
 
689
        case MSG_DISC_IND:
690
 
691
                if (!(chan = capi_channel(dev, skb))) {
692
                        printk(KERN_WARNING
693
                               "CAPI header: unknown channel id\n");
694
                        break;
695
                }
696
 
697
                if (!capi_decode_disc_ind(chan, skb))
698
                        pcbit_fsm_event(dev, chan, EV_NET_DISC, NULL);
699
                else
700
                        printk(KERN_WARNING "capi_decode_disc_ind - error\n");
701
                break;
702
        case MSG_DISC_CONF:
703
                if (!(chan = capi_channel(dev, skb))) {
704
                        printk(KERN_WARNING
705
                               "CAPI header: unknown channel id\n");
706
                        break;
707
                }
708
 
709
                if (!capi_decode_disc_ind(chan, skb))
710
                        pcbit_fsm_event(dev, chan, EV_NET_RELEASE, NULL);
711
                else
712
                        printk(KERN_WARNING "capi_decode_disc_conf - error\n");
713
                break;
714
        case MSG_INFO_IND:
715
#ifdef DEBUG
716
                printk(KERN_DEBUG "received Info Indication - discarded\n");
717
#endif
718
                break;
719
#ifdef DEBUG
720
        case MSG_DEBUG_188:
721
                capi_decode_debug_188(skb->data, skb->len);
722
                break;
723
 
724
        default:
725
                printk(KERN_DEBUG "pcbit_l3_receive: unknown message %08lx\n",
726
                       msg);
727
                fmsg = (struct msg_fmt *) &msg;
728
                printk(KERN_DEBUG "cmd=%02x sub=%02x\n",
729
                       fmsg->cmd, fmsg->scmd);
730
                break;
731
#endif
732
        }
733
 
734
        SET_SKB_FREE(skb);
735
 
736
        kfree_skb(skb, FREE_READ);
737
 
738
}
739
 
740
/*
741
 *   Single statbuf
742
 *   should be a statbuf per device
743
 */
744
 
745
static char statbuf[STATBUF_LEN];
746
static int stat_st = 0;
747
static int stat_end = 0;
748
 
749
 
750
static __inline void
751
memcpy_to_COND(int flag, char *d, const char *s, int len) {
752
        if (flag)
753
                copy_to_user(d, s, len);
754
        else
755
                memcpy(d, s, len);
756
}
757
 
758
 
759
int pcbit_stat(u_char* buf, int len, int user, int driver, int channel)
760
{
761
        int stat_count;
762
        stat_count = stat_end - stat_st;
763
 
764
        if (stat_count < 0)
765
                stat_count = STATBUF_LEN - stat_st + stat_end;
766
 
767
        /* FIXME: should we sleep and wait for more cookies ? */
768
        if (len > stat_count)
769
                len = stat_count;
770
 
771
        if (stat_st < stat_end)
772
        {
773
                memcpy_to_COND(user, buf, statbuf + stat_st, len);
774
                stat_st += len;
775
        }
776
        else
777
        {
778
                if (len > STATBUF_LEN - stat_st)
779
                {
780
                        memcpy_to_COND(user, buf, statbuf + stat_st,
781
                                       STATBUF_LEN - stat_st);
782
                        memcpy_to_COND(user, buf, statbuf,
783
                                       len - (STATBUF_LEN - stat_st));
784
 
785
                        stat_st = len - (STATBUF_LEN - stat_st);
786
                }
787
                else
788
                {
789
                        memcpy_to_COND(user, buf, statbuf + stat_st,
790
                                       len);
791
 
792
                        stat_st += len;
793
 
794
                        if (stat_st == STATBUF_LEN)
795
                                stat_st = 0;
796
                }
797
        }
798
 
799
        if (stat_st == stat_end)
800
                stat_st = stat_end = 0;
801
 
802
        return len;
803
}
804
 
805
static void pcbit_logstat(struct pcbit_dev *dev, char *str)
806
{
807
        int i;
808
        isdn_ctrl ictl;
809
 
810
        for (i=stat_end; i<strlen(str); i++)
811
        {
812
                statbuf[i]=str[i];
813
                stat_end = (stat_end + 1) % STATBUF_LEN;
814
                if (stat_end == stat_st)
815
                        stat_st = (stat_st + 1) % STATBUF_LEN;
816
        }
817
 
818
        ictl.command=ISDN_STAT_STAVAIL;
819
        ictl.driver=dev->id;
820
        ictl.arg=strlen(str);
821
        dev->dev_if->statcallb(&ictl);
822
}
823
 
824
extern char * isdn_state_table[];
825
extern char * strisdnevent(unsigned short);
826
 
827
 
828
void pcbit_state_change(struct pcbit_dev * dev, struct pcbit_chan * chan,
829
                        unsigned short i, unsigned short ev, unsigned short f)
830
{
831
        char buf[256];
832
 
833
        sprintf(buf, "change on device: %d channel:%d\n%s -> %s -> %s\n",
834
                dev->id, chan->id,
835
                isdn_state_table[i], strisdnevent(ev), isdn_state_table[f]
836
                );
837
 
838
#ifdef DEBUG
839
        printk("%s", buf);
840
#endif
841
 
842
        pcbit_logstat(dev, buf);
843
}
844
 
845
static void set_running_timeout(unsigned long ptr)
846
{
847
        struct pcbit_dev * dev;
848
 
849
#ifdef DEBUG
850
        printk(KERN_DEBUG "set_running_timeout\n");
851
#endif
852
        dev = (struct pcbit_dev *) ptr;
853
 
854
        wake_up_interruptible(&dev->set_running_wq);
855
}
856
 
857
static int set_protocol_running(struct pcbit_dev * dev)
858
{
859
        isdn_ctrl ctl;
860
 
861
        init_timer(&dev->set_running_timer);
862
 
863
        dev->set_running_timer.function = &set_running_timeout;
864
        dev->set_running_timer.data = (ulong) dev;
865
        dev->set_running_timer.expires = jiffies + SET_RUN_TIMEOUT;
866
 
867
        /* kick it */
868
 
869
        dev->l2_state = L2_STARTING;
870
 
871
        writeb((0x80U | ((dev->rcv_seq & 0x07) << 3) | (dev->send_seq & 0x07)),
872
               dev->sh_mem + BANK4);
873
 
874
        add_timer(&dev->set_running_timer);
875
 
876
        interruptible_sleep_on(&dev->set_running_wq);
877
 
878
        del_timer(&dev->set_running_timer);
879
 
880
        if (dev->l2_state == L2_RUNNING)
881
        {
882
                printk(KERN_DEBUG "pcbit: running\n");
883
 
884
                dev->unack_seq = dev->send_seq;
885
 
886
                dev->writeptr = dev->sh_mem;
887
                dev->readptr = dev->sh_mem + BANK2;
888
 
889
                /* tell the good news to the upper layer */
890
                ctl.driver = dev->id;
891
                ctl.command = ISDN_STAT_RUN;
892
 
893
                dev->dev_if->statcallb(&ctl);
894
        }
895
        else
896
        {
897
                printk(KERN_DEBUG "pcbit: initialization failed\n");
898
                printk(KERN_DEBUG "pcbit: firmware not loaded\n");
899
 
900
                dev->l2_state = L2_DOWN;
901
 
902
#ifdef DEBUG
903
                printk(KERN_DEBUG "Bank3 = %02x\n",
904
                       readb(dev->sh_mem + BANK3));
905
#endif
906
                *(dev->sh_mem + BANK4) = 0x40U;
907
 
908
                /* warn the upper layer */
909
                ctl.driver = dev->id;
910
                ctl.command = ISDN_STAT_STOP;
911
 
912
                dev->dev_if->statcallb(&ctl);
913
 
914
                return -EL2HLT; /* Level 2 halted */
915
        }
916
 
917
        return 0;
918
}
919
 
920
static int pcbit_ioctl(isdn_ctrl* ctl)
921
{
922
        struct pcbit_dev * dev;
923
        struct pcbit_ioctl *cmd;
924
 
925
        dev = finddev(ctl->driver);
926
 
927
        if (!dev)
928
        {
929
                printk(KERN_DEBUG "pcbit_ioctl: unknown device\n");
930
                return -ENODEV;
931
        }
932
 
933
        cmd = (struct pcbit_ioctl *) ctl->parm.num;
934
 
935
        switch(ctl->arg) {
936
        case PCBIT_IOCTL_GETSTAT:
937
                cmd->info.l2_status = dev->l2_state;
938
                break;
939
 
940
        case PCBIT_IOCTL_STRLOAD:
941
                if (dev->l2_state == L2_RUNNING)
942
                        return -EBUSY;
943
 
944
                dev->unack_seq = dev->send_seq = dev->rcv_seq = 0;
945
 
946
                dev->writeptr = dev->sh_mem;
947
                dev->readptr = dev->sh_mem + BANK2;
948
 
949
                dev->l2_state = L2_LOADING;
950
                break;
951
 
952
        case PCBIT_IOCTL_LWMODE:
953
                if (dev->l2_state != L2_LOADING)
954
                        return -EINVAL;
955
 
956
                dev->l2_state = L2_LWMODE;
957
                break;
958
 
959
        case PCBIT_IOCTL_FWMODE:
960
                if (dev->l2_state == L2_RUNNING)
961
                        return -EBUSY;
962
                dev->loadptr = LOAD_ZONE_START;
963
                dev->l2_state = L2_FWMODE;
964
 
965
                break;
966
        case PCBIT_IOCTL_ENDLOAD:
967
                if (dev->l2_state == L2_RUNNING)
968
                        return -EBUSY;
969
                dev->l2_state = L2_DOWN;
970
                break;
971
 
972
        case PCBIT_IOCTL_SETBYTE:
973
                if (dev->l2_state == L2_RUNNING)
974
                        return -EBUSY;
975
 
976
                /* check addr */
977
                if (cmd->info.rdp_byte.addr > BANK4)
978
                        return -EFAULT;
979
 
980
                writeb(cmd->info.rdp_byte.value, dev->sh_mem + cmd->info.rdp_byte.addr);
981
                break;
982
        case PCBIT_IOCTL_GETBYTE:
983
                if (dev->l2_state == L2_RUNNING)
984
                        return -EBUSY;
985
 
986
                /* check addr */
987
 
988
                if (cmd->info.rdp_byte.addr > BANK4)
989
                {
990
                        printk("getbyte: invalid addr %04x\n", cmd->info.rdp_byte.addr);
991
                        return -EFAULT;
992
                }
993
 
994
                cmd->info.rdp_byte.value = readb(dev->sh_mem + cmd->info.rdp_byte.addr);
995
                break;
996
        case PCBIT_IOCTL_RUNNING:
997
                if (dev->l2_state == L2_RUNNING)
998
                        return -EBUSY;
999
                return set_protocol_running(dev);
1000
                break;
1001
        case PCBIT_IOCTL_WATCH188:
1002
                if (dev->l2_state != L2_LOADING)
1003
                        return -EINVAL;
1004
                pcbit_l2_write(dev, MSG_WATCH188, 0x0001, NULL, 0);
1005
                break;
1006
        case PCBIT_IOCTL_PING188:
1007
                if (dev->l2_state != L2_LOADING)
1008
                        return -EINVAL;
1009
                pcbit_l2_write(dev, MSG_PING188_REQ, 0x0001, NULL, 0);
1010
                break;
1011
        case PCBIT_IOCTL_APION:
1012
                if (dev->l2_state != L2_LOADING)
1013
                        return -EINVAL;
1014
                pcbit_l2_write(dev, MSG_API_ON, 0x0001, NULL, 0);
1015
                break;
1016
        case PCBIT_IOCTL_STOP:
1017
                dev->l2_state = L2_DOWN;
1018
                writeb(0x40, dev->sh_mem + BANK4);
1019
                dev->rcv_seq = 0;
1020
                dev->send_seq = 0;
1021
                dev->unack_seq = 0;
1022
                break;
1023
        default:
1024
                printk("error: unknown ioctl\n");
1025
                break;
1026
        };
1027
        return 0;
1028
}
1029
 
1030
/*
1031
 *        MSN list handling
1032
 *
1033
 *        if null reject all calls
1034
 *        if first entry has null MSN accept all calls
1035
 */
1036
 
1037
static void pcbit_clear_msn(struct pcbit_dev *dev)
1038
{
1039
        struct msn_entry *ptr, *back;
1040
 
1041
        for (ptr=dev->msn_list; ptr; )
1042
        {
1043
                back = ptr->next;
1044
                kfree(ptr);
1045
                ptr = back;
1046
        }
1047
 
1048
        dev->msn_list = NULL;
1049
}
1050
 
1051
static void pcbit_set_msn(struct pcbit_dev *dev, char *list)
1052
{
1053
        struct msn_entry *ptr;
1054
        struct msn_entry *back = NULL;
1055
        char *cp, *sp;
1056
        int len;
1057
 
1058
        if (strlen(list) == 0) {
1059
                ptr = kmalloc(sizeof(struct msn_entry), GFP_ATOMIC);
1060
                if (!ptr) {
1061
                        printk(KERN_WARNING "kmalloc failed\n");
1062
                        return;
1063
                }
1064
 
1065
                ptr->msn = NULL;
1066
 
1067
                ptr->next = dev->msn_list;
1068
                dev->msn_list = ptr;
1069
 
1070
                return;
1071
        }
1072
 
1073
        if (dev->msn_list)
1074
                for (back=dev->msn_list; back->next; back=back->next);
1075
 
1076
        sp = list;
1077
 
1078
        do {
1079
                cp=strchr(sp, ',');
1080
                if (cp)
1081
                        len = cp - sp;
1082
                else
1083
                        len = strlen(sp);
1084
 
1085
                ptr = kmalloc(sizeof(struct msn_entry), GFP_ATOMIC);
1086
 
1087
                if (!ptr) {
1088
                        printk(KERN_WARNING "kmalloc failed\n");
1089
                        return;
1090
                }
1091
                ptr->next = NULL;
1092
 
1093
                ptr->msn = kmalloc(len, GFP_ATOMIC);
1094
                if (!ptr->msn) {
1095
                        printk(KERN_WARNING "kmalloc failed\n");
1096
                        return;
1097
                }
1098
 
1099
                memcpy(ptr->msn, sp, len - 1);
1100
                ptr->msn[len] = 0;
1101
 
1102
#ifdef DEBUG
1103
                printk(KERN_DEBUG "msn: %s\n", ptr->msn);
1104
#endif
1105
                if (dev->msn_list == NULL)
1106
                        dev->msn_list = ptr;
1107
                else
1108
                        back->next = ptr;
1109
                back = ptr;
1110
                sp += len;
1111
        } while(cp);
1112
}
1113
 
1114
/*
1115
 *  check if we do signal or reject an incoming call
1116
 */
1117
static int pcbit_check_msn(struct pcbit_dev *dev, char *msn)
1118
{
1119
        struct msn_entry *ptr;
1120
 
1121
        for (ptr=dev->msn_list; ptr; ptr=ptr->next) {
1122
 
1123
                if (ptr->msn == NULL)
1124
                        return 1;
1125
 
1126
                if (strcmp(ptr->msn, msn) == 0)
1127
                        return 1;
1128
        }
1129
 
1130
        return 0;
1131
}

powered by: WebSVN 2.1.0

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