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