2 * PCBIT-D interface with isdn4linux
4 * Copyright (C) 1996 Universidade de Lisboa
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.
15 * Nuno Grilo <l38486@alfa.ist.utl.pt>
16 * fixed msn_list NULL pointer dereference.
20 #include <linux/module.h>
22 #include <linux/sched.h>
24 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/string.h>
31 #include <linux/skbuff.h>
33 #include <linux/isdnif.h>
34 #include <asm/string.h>
36 #include <linux/ioport.h>
44 extern ushort last_ref_num;
46 static int pcbit_ioctl(isdn_ctrl* ctl);
48 static char* pcbit_devname[MAX_PCBIT_CARDS] = {
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);
64 static int set_protocol_running(struct pcbit_dev * dev);
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);
71 int pcbit_init_dev(int board, int mem_base, int irq)
73 struct pcbit_dev *dev;
76 if ((dev=kmalloc(sizeof(struct pcbit_dev), GFP_KERNEL)) == NULL)
78 printk("pcbit_init: couldn't malloc pcbit_dev struct\n");
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);
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")) {
91 "PCBIT: memory region %lx-%lx already in use\n",
92 dev->ph_mem, dev->ph_mem + 4096);
94 dev_pcbit[board] = NULL;
97 dev->sh_mem = ioremap(dev->ph_mem, 4096);
101 printk("memory address invalid");
103 dev_pcbit[board] = NULL;
107 dev->b1 = kmalloc(sizeof(struct pcbit_chan), GFP_KERNEL);
109 printk("pcbit_init: couldn't malloc pcbit_chan struct\n");
110 iounmap(dev->sh_mem);
111 release_mem_region(dev->ph_mem, 4096);
116 dev->b2 = kmalloc(sizeof(struct pcbit_chan), GFP_KERNEL);
118 printk("pcbit_init: couldn't malloc pcbit_chan struct\n");
120 iounmap(dev->sh_mem);
121 release_mem_region(dev->ph_mem, 4096);
126 memset(dev->b1, 0, sizeof(struct pcbit_chan));
127 memset(dev->b2, 0, sizeof(struct pcbit_chan));
130 INIT_WORK(&dev->qdelivery, pcbit_deliver);
136 if (request_irq(irq, &pcbit_irq_handler, 0, pcbit_devname[board], dev) != 0)
140 iounmap(dev->sh_mem);
141 release_mem_region(dev->ph_mem, 4096);
143 dev_pcbit[board] = NULL;
149 /* next frame to be received */
156 dev_if = kmalloc(sizeof(isdn_if), GFP_KERNEL);
162 iounmap(dev->sh_mem);
163 release_mem_region(dev->ph_mem, 4096);
165 dev_pcbit[board] = NULL;
169 dev->dev_if = dev_if;
171 dev_if->owner = THIS_MODULE;
173 dev_if->channels = 2;
175 dev_if->features = (ISDN_FEATURE_P_EURO | ISDN_FEATURE_L3_TRANS |
176 ISDN_FEATURE_L2_HDLC | ISDN_FEATURE_L2_TRANS );
178 dev_if->writebuf_skb = pcbit_xmit;
179 dev_if->hl_hdrlen = 16;
181 dev_if->maxbufsize = MAXBUFSIZE;
182 dev_if->command = pcbit_command;
184 dev_if->writecmd = pcbit_writecmd;
185 dev_if->readstat = pcbit_stat;
188 strcpy(dev_if->id, pcbit_devname[board]);
190 if (!register_isdn(dev_if)) {
194 iounmap(dev->sh_mem);
195 release_mem_region(dev->ph_mem, 4096);
197 dev_pcbit[board] = NULL;
201 dev->id = dev_if->channels;
204 dev->l2_state = L2_DOWN;
208 * set_protocol_running(dev);
215 void pcbit_terminate(int board)
217 struct pcbit_dev * dev;
219 dev = dev_pcbit[board];
222 /* unregister_isdn(dev->dev_if); */
223 free_irq(dev->irq, dev);
224 pcbit_clear_msn(dev);
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);
232 iounmap(dev->sh_mem);
233 release_mem_region(dev->ph_mem, 4096);
239 static int pcbit_command(isdn_ctrl* ctl)
241 struct pcbit_dev *dev;
242 struct pcbit_chan *chan;
243 struct callb_data info;
245 dev = finddev(ctl->driver);
249 printk("pcbit_command: unknown device\n");
253 chan = (ctl->arg & 0x0F) ? dev->b2 : dev->b1;
256 switch(ctl->command) {
258 return pcbit_ioctl(ctl);
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);
265 case ISDN_CMD_ACCEPTD:
266 pcbit_fsm_event(dev, chan, EV_USR_SETUP_RESP, NULL);
268 case ISDN_CMD_ACCEPTB:
269 printk("ISDN_CMD_ACCEPTB - not really needed\n");
271 case ISDN_CMD_HANGUP:
272 pcbit_fsm_event(dev, chan, EV_USR_RELEASE_REQ, NULL);
275 chan->proto = (ctl->arg >> 8);
277 case ISDN_CMD_CLREAZ:
278 pcbit_clear_msn(dev);
280 case ISDN_CMD_SETEAZ:
281 pcbit_set_msn(dev, ctl->parm.num);
284 if ((ctl->arg >> 8) != ISDN_PROTO_L3_TRANS)
285 printk(KERN_DEBUG "L3 protocol unknown\n");
288 printk(KERN_DEBUG "pcbit_command: unknown command\n");
297 * on some conditions the board stops sending TDATA_CONFs
298 * let's see if we can turn around the problem
302 static void pcbit_block_timer(unsigned long data)
304 struct pcbit_chan *chan;
305 struct pcbit_dev * dev;
308 chan = (struct pcbit_chan *) data;
310 dev = chan2dev(chan);
313 printk(KERN_DEBUG "pcbit: chan2dev failed\n");
317 del_timer(&chan->block_timer);
318 chan->block_timer.function = NULL;
321 printk(KERN_DEBUG "pcbit_block_timer\n");
324 ictl.driver = dev->id;
325 ictl.command = ISDN_STAT_BSENT;
327 dev->dev_if->statcallb(&ictl);
331 static int pcbit_xmit(int driver, int chnum, int ack, struct sk_buff *skb)
335 struct pcbit_chan * chan;
336 struct pcbit_dev *dev;
338 dev = finddev(driver);
341 printk("finddev returned NULL");
345 chan = chnum ? dev->b2 : dev->b1;
348 if (chan->fsm_state != ST_ACTIVE)
351 if (chan->queued >= MAX_QUEUED )
355 "pcbit: %d packets already in queue - write fails\n",
359 * packet stays on the head of the device queue
360 * since dev_start_xmit will fail
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);
380 hdrlen = capi_tdata_req(chan, skb);
382 refnum = last_ref_num++ & 0x7fffU;
383 chan->s_refnum = refnum;
385 pcbit_l2_write(dev, MSG_TDATA_REQ, refnum, skb, hdrlen);
390 static int pcbit_writecmd(const u_char __user *buf, int len, int driver, int channel)
392 struct pcbit_dev * dev;
394 const u_char * loadbuf;
400 dev = finddev(driver);
404 printk("pcbit_writecmd: couldn't find device");
408 switch(dev->l2_state) {
410 /* check (size <= rdp_size); write buf into board */
411 if (len < 0 || len > BANK4 + 1 || len > 1024)
413 printk("pcbit_writecmd: invalid length %d\n", len);
417 cbuf = kmalloc(len, GFP_KERNEL);
421 if (copy_from_user(cbuf, buf, len)) {
425 memcpy_toio(dev->sh_mem, cbuf, len);
429 /* this is the hard part */
431 /* get it into kernel space */
432 if ((ptr = kmalloc(len, GFP_KERNEL))==NULL)
434 if (copy_from_user(ptr, buf, len)) {
442 for (i=0; i < len; i++)
444 for(j=0; j < LOAD_RETRY; j++)
445 if (!(readb(dev->sh_mem + dev->loadptr)))
451 printk("TIMEOUT i=%d\n", i);
454 writeb(loadbuf[i], dev->sh_mem + dev->loadptr + 1);
455 writeb(0x01, dev->sh_mem + dev->loadptr);
458 if (dev->loadptr > LOAD_ZONE_END)
459 dev->loadptr = LOAD_ZONE_START;
463 return errstat ? errstat : len;
470 * demultiplexing of messages
474 void pcbit_l3_receive(struct pcbit_dev * dev, ulong msg,
475 struct sk_buff * skb,
476 ushort hdr_len, ushort refnum)
478 struct pcbit_chan *chan;
479 struct sk_buff *skb2;
481 struct callb_data cbdata;
488 if (!(chan = capi_channel(dev, skb))) {
490 "CAPI header: unknown channel id\n");
493 chan->r_refnum = skb->data[7];
496 dev->dev_if->rcvcallb_skb(dev->id, chan->id, skb);
498 if (capi_tdata_resp(chan, &skb2) > 0)
499 pcbit_l2_write(dev, MSG_TDATA_RESP, refnum,
504 if (!(chan = capi_channel(dev, skb))) {
506 "CAPI header: unknown channel id\n");
511 if ( (*((ushort *) (skb->data + 2) )) != 0) {
512 printk(KERN_DEBUG "TDATA_CONF error\n");
516 if (chan->queued == MAX_QUEUED) {
517 del_timer(&chan->block_timer);
518 chan->block_timer.function = NULL;
524 ictl.driver = dev->id;
525 ictl.command = ISDN_STAT_BSENT;
527 dev->dev_if->statcallb(&ictl);
532 * channel: 1st not used will do
533 * if both are used we're in trouble
536 if (!dev->b1->fsm_state)
538 else if (!dev->b2->fsm_state)
542 "Incoming connection: no channels available");
544 if ((len = capi_disc_req(*(ushort*)(skb->data), &skb2, CAUSE_NOCHAN)) > 0)
545 pcbit_l2_write(dev, MSG_DISC_REQ, refnum, skb2, len);
549 cbdata.data.setup.CalledPN = NULL;
550 cbdata.data.setup.CallingPN = NULL;
552 capi_decode_conn_ind(chan, skb, &cbdata);
553 cbdata.type = EV_NET_SETUP;
555 pcbit_fsm_event(dev, chan, EV_NET_SETUP, NULL);
557 if (pcbit_check_msn(dev, cbdata.data.setup.CallingPN))
558 pcbit_fsm_event(dev, chan, EV_USR_PROCED_REQ, &cbdata);
560 pcbit_fsm_event(dev, chan, EV_USR_RELEASE_REQ, NULL);
562 kfree(cbdata.data.setup.CalledPN);
563 kfree(cbdata.data.setup.CallingPN);
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.
573 printk(KERN_DEBUG "refnum=%04x b1=%04x b2=%04x\n", refnum,
577 /* We just try to find a channel in the right state */
579 if (dev->b1->fsm_state == ST_CALL_INIT)
582 if (dev->b2->s_refnum == ST_CALL_INIT)
586 printk(KERN_WARNING "Connection Confirm - no channel in Call Init state\n");
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);
596 pcbit_fsm_event(dev, chan, EV_NET_CALL_PROC, NULL);
598 pcbit_fsm_event(dev, chan, EV_NET_SETUP_ACK, NULL);
600 case MSG_CONN_ACTV_IND:
602 if (!(chan = capi_channel(dev, skb))) {
604 "CAPI header: unknown channel id\n");
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); */
613 chan->r_refnum = refnum;
614 pcbit_fsm_event(dev, chan, EV_NET_CONN, NULL);
616 case MSG_CONN_ACTV_CONF:
618 if (!(chan = capi_channel(dev, skb))) {
620 "CAPI header: unknown channel id\n");
624 if (capi_decode_conn_actv_conf(chan, skb) == 0)
625 pcbit_fsm_event(dev, chan, EV_NET_CONN_ACK, NULL);
628 printk(KERN_DEBUG "decode_conn_actv_conf failed\n");
633 if (!(chan = capi_channel(dev, skb))) {
635 "CAPI header: unknown channel id\n");
639 if (!(err = capi_decode_sel_proto_conf(chan, skb)))
640 pcbit_fsm_event(dev, chan, EV_NET_SELP_RESP, NULL);
643 printk("error %d - capi_decode_sel_proto_conf\n", err);
646 case MSG_ACT_TRANSP_CONF:
647 if (!(chan = capi_channel(dev, skb))) {
649 "CAPI header: unknown channel id\n");
653 if (!capi_decode_actv_trans_conf(chan, skb))
654 pcbit_fsm_event(dev, chan, EV_NET_ACTV_RESP, NULL);
659 if (!(chan = capi_channel(dev, skb))) {
661 "CAPI header: unknown channel id\n");
665 if (!capi_decode_disc_ind(chan, skb))
666 pcbit_fsm_event(dev, chan, EV_NET_DISC, NULL);
668 printk(KERN_WARNING "capi_decode_disc_ind - error\n");
671 if (!(chan = capi_channel(dev, skb))) {
673 "CAPI header: unknown channel id\n");
677 if (!capi_decode_disc_ind(chan, skb))
678 pcbit_fsm_event(dev, chan, EV_NET_RELEASE, NULL);
680 printk(KERN_WARNING "capi_decode_disc_conf - error\n");
684 printk(KERN_DEBUG "received Info Indication - discarded\n");
689 capi_decode_debug_188(skb->data, skb->len);
693 printk(KERN_DEBUG "pcbit_l3_receive: unknown message %08lx\n",
705 * should be a statbuf per device
708 static char statbuf[STATBUF_LEN];
709 static int stat_st = 0;
710 static int stat_end = 0;
712 static int pcbit_stat(u_char __user *buf, int len, int driver, int channel)
715 stat_count = stat_end - stat_st;
718 stat_count = STATBUF_LEN - stat_st + stat_end;
720 /* FIXME: should we sleep and wait for more cookies ? */
721 if (len > stat_count)
724 if (stat_st < stat_end)
726 if (copy_to_user(buf, statbuf + stat_st, len))
732 if (len > STATBUF_LEN - stat_st)
734 if (copy_to_user(buf, statbuf + stat_st,
735 STATBUF_LEN - stat_st))
737 if (copy_to_user(buf, statbuf,
738 len - (STATBUF_LEN - stat_st)))
741 stat_st = len - (STATBUF_LEN - stat_st);
745 if (copy_to_user(buf, statbuf + stat_st, len))
750 if (stat_st == STATBUF_LEN)
755 if (stat_st == stat_end)
756 stat_st = stat_end = 0;
761 static void pcbit_logstat(struct pcbit_dev *dev, char *str)
766 for (i=stat_end; i<strlen(str); i++)
769 stat_end = (stat_end + 1) % STATBUF_LEN;
770 if (stat_end == stat_st)
771 stat_st = (stat_st + 1) % STATBUF_LEN;
774 ictl.command=ISDN_STAT_STAVAIL;
776 ictl.arg=strlen(str);
777 dev->dev_if->statcallb(&ictl);
780 extern char * isdn_state_table[];
781 extern char * strisdnevent(unsigned short);
784 void pcbit_state_change(struct pcbit_dev * dev, struct pcbit_chan * chan,
785 unsigned short i, unsigned short ev, unsigned short f)
789 sprintf(buf, "change on device: %d channel:%d\n%s -> %s -> %s\n",
791 isdn_state_table[i], strisdnevent(ev), isdn_state_table[f]
798 pcbit_logstat(dev, buf);
801 static void set_running_timeout(unsigned long ptr)
803 struct pcbit_dev * dev;
806 printk(KERN_DEBUG "set_running_timeout\n");
808 dev = (struct pcbit_dev *) ptr;
810 wake_up_interruptible(&dev->set_running_wq);
813 static int set_protocol_running(struct pcbit_dev * dev)
817 init_timer(&dev->set_running_timer);
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;
825 dev->l2_state = L2_STARTING;
827 writeb((0x80U | ((dev->rcv_seq & 0x07) << 3) | (dev->send_seq & 0x07)),
828 dev->sh_mem + BANK4);
830 add_timer(&dev->set_running_timer);
832 interruptible_sleep_on(&dev->set_running_wq);
834 del_timer(&dev->set_running_timer);
836 if (dev->l2_state == L2_RUNNING)
838 printk(KERN_DEBUG "pcbit: running\n");
840 dev->unack_seq = dev->send_seq;
842 dev->writeptr = dev->sh_mem;
843 dev->readptr = dev->sh_mem + BANK2;
845 /* tell the good news to the upper layer */
846 ctl.driver = dev->id;
847 ctl.command = ISDN_STAT_RUN;
849 dev->dev_if->statcallb(&ctl);
853 printk(KERN_DEBUG "pcbit: initialization failed\n");
854 printk(KERN_DEBUG "pcbit: firmware not loaded\n");
856 dev->l2_state = L2_DOWN;
859 printk(KERN_DEBUG "Bank3 = %02x\n",
860 readb(dev->sh_mem + BANK3));
862 writeb(0x40, dev->sh_mem + BANK4);
864 /* warn the upper layer */
865 ctl.driver = dev->id;
866 ctl.command = ISDN_STAT_STOP;
868 dev->dev_if->statcallb(&ctl);
870 return -EL2HLT; /* Level 2 halted */
876 static int pcbit_ioctl(isdn_ctrl* ctl)
878 struct pcbit_dev * dev;
879 struct pcbit_ioctl *cmd;
881 dev = finddev(ctl->driver);
885 printk(KERN_DEBUG "pcbit_ioctl: unknown device\n");
889 cmd = (struct pcbit_ioctl *) ctl->parm.num;
892 case PCBIT_IOCTL_GETSTAT:
893 cmd->info.l2_status = dev->l2_state;
896 case PCBIT_IOCTL_STRLOAD:
897 if (dev->l2_state == L2_RUNNING)
900 dev->unack_seq = dev->send_seq = dev->rcv_seq = 0;
902 dev->writeptr = dev->sh_mem;
903 dev->readptr = dev->sh_mem + BANK2;
905 dev->l2_state = L2_LOADING;
908 case PCBIT_IOCTL_LWMODE:
909 if (dev->l2_state != L2_LOADING)
912 dev->l2_state = L2_LWMODE;
915 case PCBIT_IOCTL_FWMODE:
916 if (dev->l2_state == L2_RUNNING)
918 dev->loadptr = LOAD_ZONE_START;
919 dev->l2_state = L2_FWMODE;
922 case PCBIT_IOCTL_ENDLOAD:
923 if (dev->l2_state == L2_RUNNING)
925 dev->l2_state = L2_DOWN;
928 case PCBIT_IOCTL_SETBYTE:
929 if (dev->l2_state == L2_RUNNING)
933 if (cmd->info.rdp_byte.addr > BANK4)
936 writeb(cmd->info.rdp_byte.value, dev->sh_mem + cmd->info.rdp_byte.addr);
938 case PCBIT_IOCTL_GETBYTE:
939 if (dev->l2_state == L2_RUNNING)
944 if (cmd->info.rdp_byte.addr > BANK4)
946 printk("getbyte: invalid addr %04x\n", cmd->info.rdp_byte.addr);
950 cmd->info.rdp_byte.value = readb(dev->sh_mem + cmd->info.rdp_byte.addr);
952 case PCBIT_IOCTL_RUNNING:
953 if (dev->l2_state == L2_RUNNING)
955 return set_protocol_running(dev);
957 case PCBIT_IOCTL_WATCH188:
958 if (dev->l2_state != L2_LOADING)
960 pcbit_l2_write(dev, MSG_WATCH188, 0x0001, NULL, 0);
962 case PCBIT_IOCTL_PING188:
963 if (dev->l2_state != L2_LOADING)
965 pcbit_l2_write(dev, MSG_PING188_REQ, 0x0001, NULL, 0);
967 case PCBIT_IOCTL_APION:
968 if (dev->l2_state != L2_LOADING)
970 pcbit_l2_write(dev, MSG_API_ON, 0x0001, NULL, 0);
972 case PCBIT_IOCTL_STOP:
973 dev->l2_state = L2_DOWN;
974 writeb(0x40, dev->sh_mem + BANK4);
980 printk("error: unknown ioctl\n");
989 * if null reject all calls
990 * if first entry has null MSN accept all calls
993 static void pcbit_clear_msn(struct pcbit_dev *dev)
995 struct msn_entry *ptr, *back;
997 for (ptr=dev->msn_list; ptr; )
1004 dev->msn_list = NULL;
1007 static void pcbit_set_msn(struct pcbit_dev *dev, char *list)
1009 struct msn_entry *ptr;
1010 struct msn_entry *back = NULL;
1014 if (strlen(list) == 0) {
1015 ptr = kmalloc(sizeof(struct msn_entry), GFP_ATOMIC);
1017 printk(KERN_WARNING "kmalloc failed\n");
1023 ptr->next = dev->msn_list;
1024 dev->msn_list = ptr;
1030 for (back=dev->msn_list; back->next; back=back->next);
1041 ptr = kmalloc(sizeof(struct msn_entry), GFP_ATOMIC);
1044 printk(KERN_WARNING "kmalloc failed\n");
1049 ptr->msn = kmalloc(len, GFP_ATOMIC);
1051 printk(KERN_WARNING "kmalloc failed\n");
1056 memcpy(ptr->msn, sp, len - 1);
1060 printk(KERN_DEBUG "msn: %s\n", ptr->msn);
1062 if (dev->msn_list == NULL)
1063 dev->msn_list = ptr;
1072 * check if we do signal or reject an incoming call
1074 static int pcbit_check_msn(struct pcbit_dev *dev, char *msn)
1076 struct msn_entry *ptr;
1078 for (ptr=dev->msn_list; ptr; ptr=ptr->next) {
1080 if (ptr->msn == NULL)
1083 if (strcmp(ptr->msn, msn) == 0)