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