Linux 2.6.26-rc1
[cris-mirror.git] / drivers / isdn / pcbit / drv.c
blob8c66bcb953a1e97051b1fc23e7f7e43eb78f957b
1 /*
2 * PCBIT-D interface with isdn4linux
4 * Copyright (C) 1996 Universidade de Lisboa
5 *
6 * Written by Pedro Roque Marques (roque@di.fc.ul.pt)
8 * This software may be used and distributed according to the terms of
9 * the GNU General Public License, incorporated herein by reference.
13 * Fixes:
15 * Nuno Grilo <l38486@alfa.ist.utl.pt>
16 * fixed msn_list NULL pointer dereference.
20 #include <linux/module.h>
23 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/slab.h>
27 #include <linux/mm.h>
28 #include <linux/interrupt.h>
29 #include <linux/string.h>
30 #include <linux/skbuff.h>
32 #include <linux/isdnif.h>
33 #include <asm/string.h>
34 #include <asm/io.h>
35 #include <linux/ioport.h>
37 #include "pcbit.h"
38 #include "edss1.h"
39 #include "layer2.h"
40 #include "capi.h"
43 extern ushort last_ref_num;
45 static int pcbit_ioctl(isdn_ctrl* ctl);
47 static char* pcbit_devname[MAX_PCBIT_CARDS] = {
48 "pcbit0",
49 "pcbit1",
50 "pcbit2",
51 "pcbit3"
55 * prototypes
58 static int pcbit_command(isdn_ctrl* ctl);
59 static int pcbit_stat(u_char __user * buf, int len, int, int);
60 static int pcbit_xmit(int driver, int chan, int ack, struct sk_buff *skb);
61 static int pcbit_writecmd(const u_char __user *, int, int, int);
63 static int set_protocol_running(struct pcbit_dev * dev);
65 static void pcbit_clear_msn(struct pcbit_dev *dev);
66 static void pcbit_set_msn(struct pcbit_dev *dev, char *list);
67 static int pcbit_check_msn(struct pcbit_dev *dev, char *msn);
70 int pcbit_init_dev(int board, int mem_base, int irq)
72 struct pcbit_dev *dev;
73 isdn_if *dev_if;
75 if ((dev=kzalloc(sizeof(struct pcbit_dev), GFP_KERNEL)) == NULL)
77 printk("pcbit_init: couldn't malloc pcbit_dev struct\n");
78 return -ENOMEM;
81 dev_pcbit[board] = dev;
82 init_waitqueue_head(&dev->set_running_wq);
83 spin_lock_init(&dev->lock);
85 if (mem_base >= 0xA0000 && mem_base <= 0xFFFFF ) {
86 dev->ph_mem = mem_base;
87 if (!request_mem_region(dev->ph_mem, 4096, "PCBIT mem")) {
88 printk(KERN_WARNING
89 "PCBIT: memory region %lx-%lx already in use\n",
90 dev->ph_mem, dev->ph_mem + 4096);
91 kfree(dev);
92 dev_pcbit[board] = NULL;
93 return -EACCES;
95 dev->sh_mem = ioremap(dev->ph_mem, 4096);
97 else
99 printk("memory address invalid");
100 kfree(dev);
101 dev_pcbit[board] = NULL;
102 return -EACCES;
105 dev->b1 = kzalloc(sizeof(struct pcbit_chan), GFP_KERNEL);
106 if (!dev->b1) {
107 printk("pcbit_init: couldn't malloc pcbit_chan struct\n");
108 iounmap(dev->sh_mem);
109 release_mem_region(dev->ph_mem, 4096);
110 kfree(dev);
111 return -ENOMEM;
114 dev->b2 = kzalloc(sizeof(struct pcbit_chan), GFP_KERNEL);
115 if (!dev->b2) {
116 printk("pcbit_init: couldn't malloc pcbit_chan struct\n");
117 kfree(dev->b1);
118 iounmap(dev->sh_mem);
119 release_mem_region(dev->ph_mem, 4096);
120 kfree(dev);
121 return -ENOMEM;
124 dev->b2->id = 1;
126 INIT_WORK(&dev->qdelivery, pcbit_deliver);
129 * interrupts
132 if (request_irq(irq, &pcbit_irq_handler, 0, pcbit_devname[board], dev) != 0)
134 kfree(dev->b1);
135 kfree(dev->b2);
136 iounmap(dev->sh_mem);
137 release_mem_region(dev->ph_mem, 4096);
138 kfree(dev);
139 dev_pcbit[board] = NULL;
140 return -EIO;
143 dev->irq = irq;
145 /* next frame to be received */
146 dev->rcv_seq = 0;
147 dev->send_seq = 0;
148 dev->unack_seq = 0;
150 dev->hl_hdrlen = 16;
152 dev_if = kmalloc(sizeof(isdn_if), GFP_KERNEL);
154 if (!dev_if) {
155 free_irq(irq, dev);
156 kfree(dev->b1);
157 kfree(dev->b2);
158 iounmap(dev->sh_mem);
159 release_mem_region(dev->ph_mem, 4096);
160 kfree(dev);
161 dev_pcbit[board] = NULL;
162 return -EIO;
165 dev->dev_if = dev_if;
167 dev_if->owner = THIS_MODULE;
169 dev_if->channels = 2;
171 dev_if->features = (ISDN_FEATURE_P_EURO | ISDN_FEATURE_L3_TRANS |
172 ISDN_FEATURE_L2_HDLC | ISDN_FEATURE_L2_TRANS );
174 dev_if->writebuf_skb = pcbit_xmit;
175 dev_if->hl_hdrlen = 16;
177 dev_if->maxbufsize = MAXBUFSIZE;
178 dev_if->command = pcbit_command;
180 dev_if->writecmd = pcbit_writecmd;
181 dev_if->readstat = pcbit_stat;
184 strcpy(dev_if->id, pcbit_devname[board]);
186 if (!register_isdn(dev_if)) {
187 free_irq(irq, dev);
188 kfree(dev->b1);
189 kfree(dev->b2);
190 iounmap(dev->sh_mem);
191 release_mem_region(dev->ph_mem, 4096);
192 kfree(dev);
193 dev_pcbit[board] = NULL;
194 return -EIO;
197 dev->id = dev_if->channels;
200 dev->l2_state = L2_DOWN;
201 dev->free = 511;
204 * set_protocol_running(dev);
207 return 0;
210 #ifdef MODULE
211 void pcbit_terminate(int board)
213 struct pcbit_dev * dev;
215 dev = dev_pcbit[board];
217 if (dev) {
218 /* unregister_isdn(dev->dev_if); */
219 free_irq(dev->irq, dev);
220 pcbit_clear_msn(dev);
221 kfree(dev->dev_if);
222 if (dev->b1->fsm_timer.function)
223 del_timer(&dev->b1->fsm_timer);
224 if (dev->b2->fsm_timer.function)
225 del_timer(&dev->b2->fsm_timer);
226 kfree(dev->b1);
227 kfree(dev->b2);
228 iounmap(dev->sh_mem);
229 release_mem_region(dev->ph_mem, 4096);
230 kfree(dev);
233 #endif
235 static int pcbit_command(isdn_ctrl* ctl)
237 struct pcbit_dev *dev;
238 struct pcbit_chan *chan;
239 struct callb_data info;
241 dev = finddev(ctl->driver);
243 if (!dev)
245 printk("pcbit_command: unknown device\n");
246 return -1;
249 chan = (ctl->arg & 0x0F) ? dev->b2 : dev->b1;
252 switch(ctl->command) {
253 case ISDN_CMD_IOCTL:
254 return pcbit_ioctl(ctl);
255 break;
256 case ISDN_CMD_DIAL:
257 info.type = EV_USR_SETUP_REQ;
258 info.data.setup.CalledPN = (char *) &ctl->parm.setup.phone;
259 pcbit_fsm_event(dev, chan, EV_USR_SETUP_REQ, &info);
260 break;
261 case ISDN_CMD_ACCEPTD:
262 pcbit_fsm_event(dev, chan, EV_USR_SETUP_RESP, NULL);
263 break;
264 case ISDN_CMD_ACCEPTB:
265 printk("ISDN_CMD_ACCEPTB - not really needed\n");
266 break;
267 case ISDN_CMD_HANGUP:
268 pcbit_fsm_event(dev, chan, EV_USR_RELEASE_REQ, NULL);
269 break;
270 case ISDN_CMD_SETL2:
271 chan->proto = (ctl->arg >> 8);
272 break;
273 case ISDN_CMD_CLREAZ:
274 pcbit_clear_msn(dev);
275 break;
276 case ISDN_CMD_SETEAZ:
277 pcbit_set_msn(dev, ctl->parm.num);
278 break;
279 case ISDN_CMD_SETL3:
280 if ((ctl->arg >> 8) != ISDN_PROTO_L3_TRANS)
281 printk(KERN_DEBUG "L3 protocol unknown\n");
282 break;
283 default:
284 printk(KERN_DEBUG "pcbit_command: unknown command\n");
285 break;
288 return 0;
292 * Another Hack :-(
293 * on some conditions the board stops sending TDATA_CONFs
294 * let's see if we can turn around the problem
297 #ifdef BLOCK_TIMER
298 static void pcbit_block_timer(unsigned long data)
300 struct pcbit_chan *chan;
301 struct pcbit_dev * dev;
302 isdn_ctrl ictl;
304 chan = (struct pcbit_chan *) data;
306 dev = chan2dev(chan);
308 if (dev == NULL) {
309 printk(KERN_DEBUG "pcbit: chan2dev failed\n");
310 return;
313 del_timer(&chan->block_timer);
314 chan->block_timer.function = NULL;
316 #ifdef DEBUG
317 printk(KERN_DEBUG "pcbit_block_timer\n");
318 #endif
319 chan->queued = 0;
320 ictl.driver = dev->id;
321 ictl.command = ISDN_STAT_BSENT;
322 ictl.arg = chan->id;
323 dev->dev_if->statcallb(&ictl);
325 #endif
327 static int pcbit_xmit(int driver, int chnum, int ack, struct sk_buff *skb)
329 ushort hdrlen;
330 int refnum, len;
331 struct pcbit_chan * chan;
332 struct pcbit_dev *dev;
334 dev = finddev(driver);
335 if (dev == NULL)
337 printk("finddev returned NULL");
338 return -1;
341 chan = chnum ? dev->b2 : dev->b1;
344 if (chan->fsm_state != ST_ACTIVE)
345 return -1;
347 if (chan->queued >= MAX_QUEUED )
349 #ifdef DEBUG_QUEUE
350 printk(KERN_DEBUG
351 "pcbit: %d packets already in queue - write fails\n",
352 chan->queued);
353 #endif
355 * packet stays on the head of the device queue
356 * since dev_start_xmit will fail
357 * see net/core/dev.c
359 #ifdef BLOCK_TIMER
360 if (chan->block_timer.function == NULL) {
361 init_timer(&chan->block_timer);
362 chan->block_timer.function = &pcbit_block_timer;
363 chan->block_timer.data = (long) chan;
364 chan->block_timer.expires = jiffies + 1 * HZ;
365 add_timer(&chan->block_timer);
367 #endif
368 return 0;
372 chan->queued++;
374 len = skb->len;
376 hdrlen = capi_tdata_req(chan, skb);
378 refnum = last_ref_num++ & 0x7fffU;
379 chan->s_refnum = refnum;
381 pcbit_l2_write(dev, MSG_TDATA_REQ, refnum, skb, hdrlen);
383 return len;
386 static int pcbit_writecmd(const u_char __user *buf, int len, int driver, int channel)
388 struct pcbit_dev * dev;
389 int i, j;
390 const u_char * loadbuf;
391 u_char * ptr = NULL;
392 u_char *cbuf;
394 int errstat;
396 dev = finddev(driver);
398 if (!dev)
400 printk("pcbit_writecmd: couldn't find device");
401 return -ENODEV;
404 switch(dev->l2_state) {
405 case L2_LWMODE:
406 /* check (size <= rdp_size); write buf into board */
407 if (len < 0 || len > BANK4 + 1 || len > 1024)
409 printk("pcbit_writecmd: invalid length %d\n", len);
410 return -EINVAL;
413 cbuf = kmalloc(len, GFP_KERNEL);
414 if (!cbuf)
415 return -ENOMEM;
417 if (copy_from_user(cbuf, buf, len)) {
418 kfree(cbuf);
419 return -EFAULT;
421 memcpy_toio(dev->sh_mem, cbuf, len);
422 kfree(cbuf);
423 return len;
424 case L2_FWMODE:
425 /* this is the hard part */
426 /* dumb board */
427 /* get it into kernel space */
428 if ((ptr = kmalloc(len, GFP_KERNEL))==NULL)
429 return -ENOMEM;
430 if (copy_from_user(ptr, buf, len)) {
431 kfree(ptr);
432 return -EFAULT;
434 loadbuf = ptr;
436 errstat = 0;
438 for (i=0; i < len; i++)
440 for(j=0; j < LOAD_RETRY; j++)
441 if (!(readb(dev->sh_mem + dev->loadptr)))
442 break;
444 if (j == LOAD_RETRY)
446 errstat = -ETIME;
447 printk("TIMEOUT i=%d\n", i);
448 break;
450 writeb(loadbuf[i], dev->sh_mem + dev->loadptr + 1);
451 writeb(0x01, dev->sh_mem + dev->loadptr);
453 dev->loadptr += 2;
454 if (dev->loadptr > LOAD_ZONE_END)
455 dev->loadptr = LOAD_ZONE_START;
457 kfree(ptr);
459 return errstat ? errstat : len;
460 default:
461 return -EBUSY;
466 * demultiplexing of messages
470 void pcbit_l3_receive(struct pcbit_dev * dev, ulong msg,
471 struct sk_buff * skb,
472 ushort hdr_len, ushort refnum)
474 struct pcbit_chan *chan;
475 struct sk_buff *skb2;
476 unsigned short len;
477 struct callb_data cbdata;
478 int complete, err;
479 isdn_ctrl ictl;
481 switch(msg) {
483 case MSG_TDATA_IND:
484 if (!(chan = capi_channel(dev, skb))) {
485 printk(KERN_WARNING
486 "CAPI header: unknown channel id\n");
487 break;
489 chan->r_refnum = skb->data[7];
490 skb_pull(skb, 8);
492 dev->dev_if->rcvcallb_skb(dev->id, chan->id, skb);
494 if (capi_tdata_resp(chan, &skb2) > 0)
495 pcbit_l2_write(dev, MSG_TDATA_RESP, refnum,
496 skb2, skb2->len);
497 return;
498 break;
499 case MSG_TDATA_CONF:
500 if (!(chan = capi_channel(dev, skb))) {
501 printk(KERN_WARNING
502 "CAPI header: unknown channel id\n");
503 break;
506 #ifdef DEBUG
507 if ( (*((ushort *) (skb->data + 2) )) != 0) {
508 printk(KERN_DEBUG "TDATA_CONF error\n");
510 #endif
511 #ifdef BLOCK_TIMER
512 if (chan->queued == MAX_QUEUED) {
513 del_timer(&chan->block_timer);
514 chan->block_timer.function = NULL;
517 #endif
518 chan->queued--;
520 ictl.driver = dev->id;
521 ictl.command = ISDN_STAT_BSENT;
522 ictl.arg = chan->id;
523 dev->dev_if->statcallb(&ictl);
524 break;
526 case MSG_CONN_IND:
528 * channel: 1st not used will do
529 * if both are used we're in trouble
532 if (!dev->b1->fsm_state)
533 chan = dev->b1;
534 else if (!dev->b2->fsm_state)
535 chan = dev->b2;
536 else {
537 printk(KERN_INFO
538 "Incoming connection: no channels available");
540 if ((len = capi_disc_req(*(ushort*)(skb->data), &skb2, CAUSE_NOCHAN)) > 0)
541 pcbit_l2_write(dev, MSG_DISC_REQ, refnum, skb2, len);
542 break;
545 cbdata.data.setup.CalledPN = NULL;
546 cbdata.data.setup.CallingPN = NULL;
548 capi_decode_conn_ind(chan, skb, &cbdata);
549 cbdata.type = EV_NET_SETUP;
551 pcbit_fsm_event(dev, chan, EV_NET_SETUP, NULL);
553 if (pcbit_check_msn(dev, cbdata.data.setup.CallingPN))
554 pcbit_fsm_event(dev, chan, EV_USR_PROCED_REQ, &cbdata);
555 else
556 pcbit_fsm_event(dev, chan, EV_USR_RELEASE_REQ, NULL);
558 kfree(cbdata.data.setup.CalledPN);
559 kfree(cbdata.data.setup.CallingPN);
560 break;
562 case MSG_CONN_CONF:
564 * We should be able to find the channel by the message
565 * reference number. The current version of the firmware
566 * doesn't sent the ref number correctly.
568 #ifdef DEBUG
569 printk(KERN_DEBUG "refnum=%04x b1=%04x b2=%04x\n", refnum,
570 dev->b1->s_refnum,
571 dev->b2->s_refnum);
572 #endif
573 /* We just try to find a channel in the right state */
575 if (dev->b1->fsm_state == ST_CALL_INIT)
576 chan = dev->b1;
577 else {
578 if (dev->b2->s_refnum == ST_CALL_INIT)
579 chan = dev->b2;
580 else {
581 chan = NULL;
582 printk(KERN_WARNING "Connection Confirm - no channel in Call Init state\n");
583 break;
586 if (capi_decode_conn_conf(chan, skb, &complete)) {
587 printk(KERN_DEBUG "conn_conf indicates error\n");
588 pcbit_fsm_event(dev, chan, EV_ERROR, NULL);
590 else
591 if (complete)
592 pcbit_fsm_event(dev, chan, EV_NET_CALL_PROC, NULL);
593 else
594 pcbit_fsm_event(dev, chan, EV_NET_SETUP_ACK, NULL);
595 break;
596 case MSG_CONN_ACTV_IND:
598 if (!(chan = capi_channel(dev, skb))) {
599 printk(KERN_WARNING
600 "CAPI header: unknown channel id\n");
601 break;
604 if (capi_decode_conn_actv_ind(chan, skb)) {
605 printk("error in capi_decode_conn_actv_ind\n");
606 /* pcbit_fsm_event(dev, chan, EV_ERROR, NULL); */
607 break;
609 chan->r_refnum = refnum;
610 pcbit_fsm_event(dev, chan, EV_NET_CONN, NULL);
611 break;
612 case MSG_CONN_ACTV_CONF:
614 if (!(chan = capi_channel(dev, skb))) {
615 printk(KERN_WARNING
616 "CAPI header: unknown channel id\n");
617 break;
620 if (capi_decode_conn_actv_conf(chan, skb) == 0)
621 pcbit_fsm_event(dev, chan, EV_NET_CONN_ACK, NULL);
623 else
624 printk(KERN_DEBUG "decode_conn_actv_conf failed\n");
625 break;
627 case MSG_SELP_CONF:
629 if (!(chan = capi_channel(dev, skb))) {
630 printk(KERN_WARNING
631 "CAPI header: unknown channel id\n");
632 break;
635 if (!(err = capi_decode_sel_proto_conf(chan, skb)))
636 pcbit_fsm_event(dev, chan, EV_NET_SELP_RESP, NULL);
637 else {
638 /* Error */
639 printk("error %d - capi_decode_sel_proto_conf\n", err);
641 break;
642 case MSG_ACT_TRANSP_CONF:
643 if (!(chan = capi_channel(dev, skb))) {
644 printk(KERN_WARNING
645 "CAPI header: unknown channel id\n");
646 break;
649 if (!capi_decode_actv_trans_conf(chan, skb))
650 pcbit_fsm_event(dev, chan, EV_NET_ACTV_RESP, NULL);
651 break;
653 case MSG_DISC_IND:
655 if (!(chan = capi_channel(dev, skb))) {
656 printk(KERN_WARNING
657 "CAPI header: unknown channel id\n");
658 break;
661 if (!capi_decode_disc_ind(chan, skb))
662 pcbit_fsm_event(dev, chan, EV_NET_DISC, NULL);
663 else
664 printk(KERN_WARNING "capi_decode_disc_ind - error\n");
665 break;
666 case MSG_DISC_CONF:
667 if (!(chan = capi_channel(dev, skb))) {
668 printk(KERN_WARNING
669 "CAPI header: unknown channel id\n");
670 break;
673 if (!capi_decode_disc_ind(chan, skb))
674 pcbit_fsm_event(dev, chan, EV_NET_RELEASE, NULL);
675 else
676 printk(KERN_WARNING "capi_decode_disc_conf - error\n");
677 break;
678 case MSG_INFO_IND:
679 #ifdef DEBUG
680 printk(KERN_DEBUG "received Info Indication - discarded\n");
681 #endif
682 break;
683 #ifdef DEBUG
684 case MSG_DEBUG_188:
685 capi_decode_debug_188(skb->data, skb->len);
686 break;
688 default:
689 printk(KERN_DEBUG "pcbit_l3_receive: unknown message %08lx\n",
690 msg);
691 break;
692 #endif
695 kfree_skb(skb);
700 * Single statbuf
701 * should be a statbuf per device
704 static char statbuf[STATBUF_LEN];
705 static int stat_st = 0;
706 static int stat_end = 0;
708 static int pcbit_stat(u_char __user *buf, int len, int driver, int channel)
710 int stat_count;
711 stat_count = stat_end - stat_st;
713 if (stat_count < 0)
714 stat_count = STATBUF_LEN - stat_st + stat_end;
716 /* FIXME: should we sleep and wait for more cookies ? */
717 if (len > stat_count)
718 len = stat_count;
720 if (stat_st < stat_end)
722 if (copy_to_user(buf, statbuf + stat_st, len))
723 return -EFAULT;
724 stat_st += len;
726 else
728 if (len > STATBUF_LEN - stat_st)
730 if (copy_to_user(buf, statbuf + stat_st,
731 STATBUF_LEN - stat_st))
732 return -EFAULT;
733 if (copy_to_user(buf, statbuf,
734 len - (STATBUF_LEN - stat_st)))
735 return -EFAULT;
737 stat_st = len - (STATBUF_LEN - stat_st);
739 else
741 if (copy_to_user(buf, statbuf + stat_st, len))
742 return -EFAULT;
744 stat_st += len;
746 if (stat_st == STATBUF_LEN)
747 stat_st = 0;
751 if (stat_st == stat_end)
752 stat_st = stat_end = 0;
754 return len;
757 static void pcbit_logstat(struct pcbit_dev *dev, char *str)
759 int i;
760 isdn_ctrl ictl;
762 for (i=stat_end; i<strlen(str); i++)
764 statbuf[i]=str[i];
765 stat_end = (stat_end + 1) % STATBUF_LEN;
766 if (stat_end == stat_st)
767 stat_st = (stat_st + 1) % STATBUF_LEN;
770 ictl.command=ISDN_STAT_STAVAIL;
771 ictl.driver=dev->id;
772 ictl.arg=strlen(str);
773 dev->dev_if->statcallb(&ictl);
776 void pcbit_state_change(struct pcbit_dev * dev, struct pcbit_chan * chan,
777 unsigned short i, unsigned short ev, unsigned short f)
779 char buf[256];
781 sprintf(buf, "change on device: %d channel:%d\n%s -> %s -> %s\n",
782 dev->id, chan->id,
783 isdn_state_table[i], strisdnevent(ev), isdn_state_table[f]
786 #ifdef DEBUG
787 printk("%s", buf);
788 #endif
790 pcbit_logstat(dev, buf);
793 static void set_running_timeout(unsigned long ptr)
795 struct pcbit_dev * dev;
797 #ifdef DEBUG
798 printk(KERN_DEBUG "set_running_timeout\n");
799 #endif
800 dev = (struct pcbit_dev *) ptr;
802 wake_up_interruptible(&dev->set_running_wq);
805 static int set_protocol_running(struct pcbit_dev * dev)
807 isdn_ctrl ctl;
809 init_timer(&dev->set_running_timer);
811 dev->set_running_timer.function = &set_running_timeout;
812 dev->set_running_timer.data = (ulong) dev;
813 dev->set_running_timer.expires = jiffies + SET_RUN_TIMEOUT;
815 /* kick it */
817 dev->l2_state = L2_STARTING;
819 writeb((0x80U | ((dev->rcv_seq & 0x07) << 3) | (dev->send_seq & 0x07)),
820 dev->sh_mem + BANK4);
822 add_timer(&dev->set_running_timer);
824 interruptible_sleep_on(&dev->set_running_wq);
826 del_timer(&dev->set_running_timer);
828 if (dev->l2_state == L2_RUNNING)
830 printk(KERN_DEBUG "pcbit: running\n");
832 dev->unack_seq = dev->send_seq;
834 dev->writeptr = dev->sh_mem;
835 dev->readptr = dev->sh_mem + BANK2;
837 /* tell the good news to the upper layer */
838 ctl.driver = dev->id;
839 ctl.command = ISDN_STAT_RUN;
841 dev->dev_if->statcallb(&ctl);
843 else
845 printk(KERN_DEBUG "pcbit: initialization failed\n");
846 printk(KERN_DEBUG "pcbit: firmware not loaded\n");
848 dev->l2_state = L2_DOWN;
850 #ifdef DEBUG
851 printk(KERN_DEBUG "Bank3 = %02x\n",
852 readb(dev->sh_mem + BANK3));
853 #endif
854 writeb(0x40, dev->sh_mem + BANK4);
856 /* warn the upper layer */
857 ctl.driver = dev->id;
858 ctl.command = ISDN_STAT_STOP;
860 dev->dev_if->statcallb(&ctl);
862 return -EL2HLT; /* Level 2 halted */
865 return 0;
868 static int pcbit_ioctl(isdn_ctrl* ctl)
870 struct pcbit_dev * dev;
871 struct pcbit_ioctl *cmd;
873 dev = finddev(ctl->driver);
875 if (!dev)
877 printk(KERN_DEBUG "pcbit_ioctl: unknown device\n");
878 return -ENODEV;
881 cmd = (struct pcbit_ioctl *) ctl->parm.num;
883 switch(ctl->arg) {
884 case PCBIT_IOCTL_GETSTAT:
885 cmd->info.l2_status = dev->l2_state;
886 break;
888 case PCBIT_IOCTL_STRLOAD:
889 if (dev->l2_state == L2_RUNNING)
890 return -EBUSY;
892 dev->unack_seq = dev->send_seq = dev->rcv_seq = 0;
894 dev->writeptr = dev->sh_mem;
895 dev->readptr = dev->sh_mem + BANK2;
897 dev->l2_state = L2_LOADING;
898 break;
900 case PCBIT_IOCTL_LWMODE:
901 if (dev->l2_state != L2_LOADING)
902 return -EINVAL;
904 dev->l2_state = L2_LWMODE;
905 break;
907 case PCBIT_IOCTL_FWMODE:
908 if (dev->l2_state == L2_RUNNING)
909 return -EBUSY;
910 dev->loadptr = LOAD_ZONE_START;
911 dev->l2_state = L2_FWMODE;
913 break;
914 case PCBIT_IOCTL_ENDLOAD:
915 if (dev->l2_state == L2_RUNNING)
916 return -EBUSY;
917 dev->l2_state = L2_DOWN;
918 break;
920 case PCBIT_IOCTL_SETBYTE:
921 if (dev->l2_state == L2_RUNNING)
922 return -EBUSY;
924 /* check addr */
925 if (cmd->info.rdp_byte.addr > BANK4)
926 return -EFAULT;
928 writeb(cmd->info.rdp_byte.value, dev->sh_mem + cmd->info.rdp_byte.addr);
929 break;
930 case PCBIT_IOCTL_GETBYTE:
931 if (dev->l2_state == L2_RUNNING)
932 return -EBUSY;
934 /* check addr */
936 if (cmd->info.rdp_byte.addr > BANK4)
938 printk("getbyte: invalid addr %04x\n", cmd->info.rdp_byte.addr);
939 return -EFAULT;
942 cmd->info.rdp_byte.value = readb(dev->sh_mem + cmd->info.rdp_byte.addr);
943 break;
944 case PCBIT_IOCTL_RUNNING:
945 if (dev->l2_state == L2_RUNNING)
946 return -EBUSY;
947 return set_protocol_running(dev);
948 break;
949 case PCBIT_IOCTL_WATCH188:
950 if (dev->l2_state != L2_LOADING)
951 return -EINVAL;
952 pcbit_l2_write(dev, MSG_WATCH188, 0x0001, NULL, 0);
953 break;
954 case PCBIT_IOCTL_PING188:
955 if (dev->l2_state != L2_LOADING)
956 return -EINVAL;
957 pcbit_l2_write(dev, MSG_PING188_REQ, 0x0001, NULL, 0);
958 break;
959 case PCBIT_IOCTL_APION:
960 if (dev->l2_state != L2_LOADING)
961 return -EINVAL;
962 pcbit_l2_write(dev, MSG_API_ON, 0x0001, NULL, 0);
963 break;
964 case PCBIT_IOCTL_STOP:
965 dev->l2_state = L2_DOWN;
966 writeb(0x40, dev->sh_mem + BANK4);
967 dev->rcv_seq = 0;
968 dev->send_seq = 0;
969 dev->unack_seq = 0;
970 break;
971 default:
972 printk("error: unknown ioctl\n");
973 break;
975 return 0;
979 * MSN list handling
981 * if null reject all calls
982 * if first entry has null MSN accept all calls
985 static void pcbit_clear_msn(struct pcbit_dev *dev)
987 struct msn_entry *ptr, *back;
989 for (ptr=dev->msn_list; ptr; )
991 back = ptr->next;
992 kfree(ptr);
993 ptr = back;
996 dev->msn_list = NULL;
999 static void pcbit_set_msn(struct pcbit_dev *dev, char *list)
1001 struct msn_entry *ptr;
1002 struct msn_entry *back = NULL;
1003 char *cp, *sp;
1004 int len;
1006 if (strlen(list) == 0) {
1007 ptr = kmalloc(sizeof(struct msn_entry), GFP_ATOMIC);
1008 if (!ptr) {
1009 printk(KERN_WARNING "kmalloc failed\n");
1010 return;
1013 ptr->msn = NULL;
1015 ptr->next = dev->msn_list;
1016 dev->msn_list = ptr;
1018 return;
1021 if (dev->msn_list)
1022 for (back=dev->msn_list; back->next; back=back->next);
1024 sp = list;
1026 do {
1027 cp=strchr(sp, ',');
1028 if (cp)
1029 len = cp - sp;
1030 else
1031 len = strlen(sp);
1033 ptr = kmalloc(sizeof(struct msn_entry), GFP_ATOMIC);
1035 if (!ptr) {
1036 printk(KERN_WARNING "kmalloc failed\n");
1037 return;
1039 ptr->next = NULL;
1041 ptr->msn = kmalloc(len, GFP_ATOMIC);
1042 if (!ptr->msn) {
1043 printk(KERN_WARNING "kmalloc failed\n");
1044 kfree(ptr);
1045 return;
1048 memcpy(ptr->msn, sp, len - 1);
1049 ptr->msn[len] = 0;
1051 #ifdef DEBUG
1052 printk(KERN_DEBUG "msn: %s\n", ptr->msn);
1053 #endif
1054 if (dev->msn_list == NULL)
1055 dev->msn_list = ptr;
1056 else
1057 back->next = ptr;
1058 back = ptr;
1059 sp += len;
1060 } while(cp);
1064 * check if we do signal or reject an incoming call
1066 static int pcbit_check_msn(struct pcbit_dev *dev, char *msn)
1068 struct msn_entry *ptr;
1070 for (ptr=dev->msn_list; ptr; ptr=ptr->next) {
1072 if (ptr->msn == NULL)
1073 return 1;
1075 if (strcmp(ptr->msn, msn) == 0)
1076 return 1;
1079 return 0;