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