• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #include <linux/kernel.h>
24 
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>
31 
32 #include <linux/isdnif.h>
33 #include <asm/string.h>
34 #include <asm/io.h>
35 #include <linux/ioport.h>
36 
37 #include "pcbit.h"
38 #include "edss1.h"
39 #include "layer2.h"
40 #include "capi.h"
41 
42 
43 extern ushort last_ref_num;
44 
45 static int pcbit_ioctl(isdn_ctrl* ctl);
46 
47 static char* pcbit_devname[MAX_PCBIT_CARDS] = {
48 	"pcbit0",
49 	"pcbit1",
50 	"pcbit2",
51 	"pcbit3"
52 };
53 
54 /*
55  * prototypes
56  */
57 
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);
62 
63 static int set_protocol_running(struct pcbit_dev * dev);
64 
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);
68 
69 
pcbit_init_dev(int board,int mem_base,int irq)70 int pcbit_init_dev(int board, int mem_base, int irq)
71 {
72 	struct pcbit_dev *dev;
73 	isdn_if *dev_if;
74 
75 	if ((dev=kzalloc(sizeof(struct pcbit_dev), GFP_KERNEL)) == NULL)
76 	{
77 		printk("pcbit_init: couldn't malloc pcbit_dev struct\n");
78 		return -ENOMEM;
79 	}
80 
81 	dev_pcbit[board] = dev;
82 	init_waitqueue_head(&dev->set_running_wq);
83 	spin_lock_init(&dev->lock);
84 
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;
94 		}
95 		dev->sh_mem = ioremap(dev->ph_mem, 4096);
96 	}
97 	else
98 	{
99 		printk("memory address invalid");
100 		kfree(dev);
101 		dev_pcbit[board] = NULL;
102 		return -EACCES;
103 	}
104 
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;
112 	}
113 
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;
122 	}
123 
124 	dev->b2->id = 1;
125 
126 	INIT_WORK(&dev->qdelivery, pcbit_deliver);
127 
128 	/*
129 	 *  interrupts
130 	 */
131 
132 	if (request_irq(irq, &pcbit_irq_handler, 0, pcbit_devname[board], dev) != 0)
133 	{
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;
141 	}
142 
143 	dev->irq = irq;
144 
145 	/* next frame to be received */
146 	dev->rcv_seq = 0;
147 	dev->send_seq = 0;
148 	dev->unack_seq = 0;
149 
150 	dev->hl_hdrlen = 16;
151 
152 	dev_if = kmalloc(sizeof(isdn_if), GFP_KERNEL);
153 
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;
163 	}
164 
165 	dev->dev_if = dev_if;
166 
167 	dev_if->owner = THIS_MODULE;
168 
169 	dev_if->channels = 2;
170 
171 	dev_if->features = (ISDN_FEATURE_P_EURO  | ISDN_FEATURE_L3_TRANS |
172 			    ISDN_FEATURE_L2_HDLC | ISDN_FEATURE_L2_TRANS );
173 
174 	dev_if->writebuf_skb = pcbit_xmit;
175 	dev_if->hl_hdrlen = 16;
176 
177 	dev_if->maxbufsize = MAXBUFSIZE;
178 	dev_if->command  = pcbit_command;
179 
180 	dev_if->writecmd = pcbit_writecmd;
181 	dev_if->readstat = pcbit_stat;
182 
183 
184 	strcpy(dev_if->id, pcbit_devname[board]);
185 
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;
195 	}
196 
197 	dev->id = dev_if->channels;
198 
199 
200 	dev->l2_state = L2_DOWN;
201 	dev->free = 511;
202 
203 	/*
204 	 * set_protocol_running(dev);
205 	 */
206 
207 	return 0;
208 }
209 
210 #ifdef MODULE
pcbit_terminate(int board)211 void pcbit_terminate(int board)
212 {
213 	struct pcbit_dev * dev;
214 
215 	dev = dev_pcbit[board];
216 
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);
231 	}
232 }
233 #endif
234 
pcbit_command(isdn_ctrl * ctl)235 static int pcbit_command(isdn_ctrl* ctl)
236 {
237 	struct pcbit_dev  *dev;
238 	struct pcbit_chan *chan;
239 	struct callb_data info;
240 
241 	dev = finddev(ctl->driver);
242 
243 	if (!dev)
244 	{
245 		printk("pcbit_command: unknown device\n");
246 		return -1;
247 	}
248 
249 	chan = (ctl->arg & 0x0F) ? dev->b2 : dev->b1;
250 
251 
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;
286 	};
287 
288 	return 0;
289 }
290 
291 /*
292  * Another Hack :-(
293  * on some conditions the board stops sending TDATA_CONFs
294  * let's see if we can turn around the problem
295  */
296 
297 #ifdef BLOCK_TIMER
pcbit_block_timer(unsigned long data)298 static void pcbit_block_timer(unsigned long data)
299 {
300 	struct pcbit_chan *chan;
301 	struct pcbit_dev * dev;
302 	isdn_ctrl ictl;
303 
304 	chan = (struct pcbit_chan *) data;
305 
306 	dev = chan2dev(chan);
307 
308 	if (dev == NULL) {
309 		printk(KERN_DEBUG "pcbit: chan2dev failed\n");
310 		return;
311 	}
312 
313 	del_timer(&chan->block_timer);
314 	chan->block_timer.function = NULL;
315 
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);
324 }
325 #endif
326 
pcbit_xmit(int driver,int chnum,int ack,struct sk_buff * skb)327 static int pcbit_xmit(int driver, int chnum, int ack, struct sk_buff *skb)
328 {
329 	ushort hdrlen;
330 	int refnum, len;
331 	struct pcbit_chan * chan;
332 	struct pcbit_dev *dev;
333 
334 	dev = finddev(driver);
335 	if (dev == NULL)
336 	{
337 		printk("finddev returned NULL");
338 		return -1;
339 	}
340 
341 	chan = chnum ? dev->b2 : dev->b1;
342 
343 
344 	if (chan->fsm_state != ST_ACTIVE)
345 		return -1;
346 
347 	if (chan->queued >= MAX_QUEUED )
348 	{
349 #ifdef DEBUG_QUEUE
350 		printk(KERN_DEBUG
351 		       "pcbit: %d packets already in queue - write fails\n",
352 		       chan->queued);
353 #endif
354 		/*
355 		 * packet stays on the head of the device queue
356 		 * since dev_start_xmit will fail
357 		 * see net/core/dev.c
358 		 */
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);
366 		}
367 #endif
368 		return 0;
369 	}
370 
371 
372 	chan->queued++;
373 
374         len = skb->len;
375 
376 	hdrlen = capi_tdata_req(chan, skb);
377 
378 	refnum = last_ref_num++ & 0x7fffU;
379 	chan->s_refnum = refnum;
380 
381 	pcbit_l2_write(dev, MSG_TDATA_REQ, refnum, skb, hdrlen);
382 
383 	return len;
384 }
385 
pcbit_writecmd(const u_char __user * buf,int len,int driver,int channel)386 static int pcbit_writecmd(const u_char __user *buf, int len, int driver, int channel)
387 {
388 	struct pcbit_dev * dev;
389 	int i, j;
390 	const u_char * loadbuf;
391 	u_char * ptr = NULL;
392 	u_char *cbuf;
393 
394 	int errstat;
395 
396 	dev = finddev(driver);
397 
398 	if (!dev)
399 	{
400 		printk("pcbit_writecmd: couldn't find device");
401 		return -ENODEV;
402 	}
403 
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)
408 		{
409 			printk("pcbit_writecmd: invalid length %d\n", len);
410 			return -EINVAL;
411 		}
412 
413 		cbuf = kmalloc(len, GFP_KERNEL);
414 		if (!cbuf)
415 			return -ENOMEM;
416 
417 		if (copy_from_user(cbuf, buf, len)) {
418 			kfree(cbuf);
419 			return -EFAULT;
420 		}
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;
433 		}
434 		loadbuf = ptr;
435 
436 		errstat = 0;
437 
438 		for (i=0; i < len; i++)
439 		{
440 			for(j=0; j < LOAD_RETRY; j++)
441 				if (!(readb(dev->sh_mem + dev->loadptr)))
442 					break;
443 
444 			if (j == LOAD_RETRY)
445 			{
446 				errstat = -ETIME;
447 				printk("TIMEOUT i=%d\n", i);
448 				break;
449 			}
450 			writeb(loadbuf[i], dev->sh_mem + dev->loadptr + 1);
451 			writeb(0x01, dev->sh_mem + dev->loadptr);
452 
453 			dev->loadptr += 2;
454 			if (dev->loadptr > LOAD_ZONE_END)
455 				dev->loadptr = LOAD_ZONE_START;
456 		}
457 		kfree(ptr);
458 
459 		return errstat ? errstat : len;
460 	default:
461 		return -EBUSY;
462 	}
463 }
464 
465 /*
466  *  demultiplexing of messages
467  *
468  */
469 
pcbit_l3_receive(struct pcbit_dev * dev,ulong msg,struct sk_buff * skb,ushort hdr_len,ushort refnum)470 void pcbit_l3_receive(struct pcbit_dev * dev, ulong msg,
471 			     struct sk_buff * skb,
472 			     ushort hdr_len, ushort refnum)
473 {
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;
480 
481 	switch(msg) {
482 
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;
488 		}
489 		chan->r_refnum = skb->data[7];
490 		skb_pull(skb, 8);
491 
492 		dev->dev_if->rcvcallb_skb(dev->id, chan->id, skb);
493 
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;
504 		}
505 
506 #ifdef DEBUG
507 		if ( (*((ushort *) (skb->data + 2) )) != 0) {
508                         printk(KERN_DEBUG "TDATA_CONF error\n");
509 		}
510 #endif
511 #ifdef BLOCK_TIMER
512                 if (chan->queued == MAX_QUEUED) {
513                         del_timer(&chan->block_timer);
514 			chan->block_timer.function = NULL;
515 		}
516 
517 #endif
518 		chan->queued--;
519 
520 		ictl.driver = dev->id;
521 		ictl.command = ISDN_STAT_BSENT;
522 		ictl.arg = chan->id;
523 		dev->dev_if->statcallb(&ictl);
524 		break;
525 
526 	case MSG_CONN_IND:
527 		/*
528 		 *  channel: 1st not used will do
529 		 *           if both are used we're in trouble
530 		 */
531 
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");
539 
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;
543 		}
544 
545 		cbdata.data.setup.CalledPN = NULL;
546 		cbdata.data.setup.CallingPN = NULL;
547 
548 		capi_decode_conn_ind(chan, skb, &cbdata);
549 		cbdata.type = EV_NET_SETUP;
550 
551 		pcbit_fsm_event(dev, chan, EV_NET_SETUP, NULL);
552 
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);
557 
558 		kfree(cbdata.data.setup.CalledPN);
559 		kfree(cbdata.data.setup.CallingPN);
560 		break;
561 
562 	case MSG_CONN_CONF:
563 		/*
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.
567 		 */
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 */
574 
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;
584 			}
585 		}
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);
589 		}
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:
597 
598 		if (!(chan = capi_channel(dev, skb))) {
599 			printk(KERN_WARNING
600 			       "CAPI header: unknown channel id\n");
601 			break;
602 		}
603 
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;
608 		}
609 		chan->r_refnum = refnum;
610 		pcbit_fsm_event(dev, chan, EV_NET_CONN, NULL);
611 		break;
612 	case MSG_CONN_ACTV_CONF:
613 
614 		if (!(chan = capi_channel(dev, skb))) {
615 			printk(KERN_WARNING
616 			       "CAPI header: unknown channel id\n");
617 			break;
618 		}
619 
620 		if (capi_decode_conn_actv_conf(chan, skb) == 0)
621 			pcbit_fsm_event(dev, chan, EV_NET_CONN_ACK, NULL);
622 
623 		else
624 			printk(KERN_DEBUG "decode_conn_actv_conf failed\n");
625 		break;
626 
627 	case  MSG_SELP_CONF:
628 
629 		if (!(chan = capi_channel(dev, skb))) {
630 			printk(KERN_WARNING
631 			       "CAPI header: unknown channel id\n");
632 			break;
633 		}
634 
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);
640 		}
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;
647 		}
648 
649 		if (!capi_decode_actv_trans_conf(chan, skb))
650 			pcbit_fsm_event(dev, chan, EV_NET_ACTV_RESP, NULL);
651 		break;
652 
653 	case MSG_DISC_IND:
654 
655 		if (!(chan = capi_channel(dev, skb))) {
656 			printk(KERN_WARNING
657 			       "CAPI header: unknown channel id\n");
658 			break;
659 		}
660 
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;
671 		}
672 
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;
687 
688 	default:
689 		printk(KERN_DEBUG "pcbit_l3_receive: unknown message %08lx\n",
690 		       msg);
691 		break;
692 #endif
693 	}
694 
695 	kfree_skb(skb);
696 
697 }
698 
699 /*
700  *   Single statbuf
701  *   should be a statbuf per device
702  */
703 
704 static char statbuf[STATBUF_LEN];
705 static int stat_st = 0;
706 static int stat_end = 0;
707 
pcbit_stat(u_char __user * buf,int len,int driver,int channel)708 static int pcbit_stat(u_char __user *buf, int len, int driver, int channel)
709 {
710 	int stat_count;
711 	stat_count = stat_end - stat_st;
712 
713 	if (stat_count < 0)
714 		stat_count = STATBUF_LEN - stat_st + stat_end;
715 
716 	/* FIXME: should we sleep and wait for more cookies ? */
717 	if (len > stat_count)
718 		len = stat_count;
719 
720 	if (stat_st < stat_end)
721 	{
722 		if (copy_to_user(buf, statbuf + stat_st, len))
723 			return -EFAULT;
724 		stat_st += len;
725 	}
726 	else
727 	{
728 		if (len > STATBUF_LEN - stat_st)
729 		{
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;
736 
737 			stat_st = len - (STATBUF_LEN - stat_st);
738 		}
739 		else
740 		{
741 			if (copy_to_user(buf, statbuf + stat_st, len))
742 				return -EFAULT;
743 
744 			stat_st += len;
745 
746 			if (stat_st == STATBUF_LEN)
747 				stat_st = 0;
748 		}
749 	}
750 
751 	if (stat_st == stat_end)
752 		stat_st = stat_end = 0;
753 
754 	return len;
755 }
756 
pcbit_logstat(struct pcbit_dev * dev,char * str)757 static void pcbit_logstat(struct pcbit_dev *dev, char *str)
758 {
759 	int i;
760 	isdn_ctrl ictl;
761 
762 	for (i=stat_end; i<strlen(str); i++)
763 	{
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;
768 	}
769 
770 	ictl.command=ISDN_STAT_STAVAIL;
771 	ictl.driver=dev->id;
772 	ictl.arg=strlen(str);
773 	dev->dev_if->statcallb(&ictl);
774 }
775 
pcbit_state_change(struct pcbit_dev * dev,struct pcbit_chan * chan,unsigned short i,unsigned short ev,unsigned short f)776 void pcbit_state_change(struct pcbit_dev * dev, struct pcbit_chan * chan,
777 			unsigned short i, unsigned short ev, unsigned short f)
778 {
779 	char buf[256];
780 
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]
784 		);
785 
786 #ifdef DEBUG
787 	printk("%s", buf);
788 #endif
789 
790 	pcbit_logstat(dev, buf);
791 }
792 
set_running_timeout(unsigned long ptr)793 static void set_running_timeout(unsigned long ptr)
794 {
795 	struct pcbit_dev * dev;
796 
797 #ifdef DEBUG
798 	printk(KERN_DEBUG "set_running_timeout\n");
799 #endif
800 	dev = (struct pcbit_dev *) ptr;
801 
802 	wake_up_interruptible(&dev->set_running_wq);
803 }
804 
set_protocol_running(struct pcbit_dev * dev)805 static int set_protocol_running(struct pcbit_dev * dev)
806 {
807 	isdn_ctrl ctl;
808 
809 	init_timer(&dev->set_running_timer);
810 
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;
814 
815 	/* kick it */
816 
817 	dev->l2_state = L2_STARTING;
818 
819 	writeb((0x80U | ((dev->rcv_seq & 0x07) << 3) | (dev->send_seq & 0x07)),
820 	       dev->sh_mem + BANK4);
821 
822 	add_timer(&dev->set_running_timer);
823 
824 	interruptible_sleep_on(&dev->set_running_wq);
825 
826 	del_timer(&dev->set_running_timer);
827 
828 	if (dev->l2_state == L2_RUNNING)
829 	{
830 		printk(KERN_DEBUG "pcbit: running\n");
831 
832 		dev->unack_seq = dev->send_seq;
833 
834 		dev->writeptr = dev->sh_mem;
835 		dev->readptr = dev->sh_mem + BANK2;
836 
837 		/* tell the good news to the upper layer */
838 		ctl.driver = dev->id;
839 		ctl.command = ISDN_STAT_RUN;
840 
841 		dev->dev_if->statcallb(&ctl);
842 	}
843 	else
844 	{
845 		printk(KERN_DEBUG "pcbit: initialization failed\n");
846 		printk(KERN_DEBUG "pcbit: firmware not loaded\n");
847 
848 		dev->l2_state = L2_DOWN;
849 
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);
855 
856 		/* warn the upper layer */
857 		ctl.driver = dev->id;
858 		ctl.command = ISDN_STAT_STOP;
859 
860 		dev->dev_if->statcallb(&ctl);
861 
862 		return -EL2HLT;	/* Level 2 halted */
863 	}
864 
865 	return 0;
866 }
867 
pcbit_ioctl(isdn_ctrl * ctl)868 static int pcbit_ioctl(isdn_ctrl* ctl)
869 {
870 	struct pcbit_dev * dev;
871 	struct pcbit_ioctl *cmd;
872 
873 	dev = finddev(ctl->driver);
874 
875 	if (!dev)
876 	{
877 		printk(KERN_DEBUG "pcbit_ioctl: unknown device\n");
878 		return -ENODEV;
879 	}
880 
881 	cmd = (struct pcbit_ioctl *) ctl->parm.num;
882 
883 	switch(ctl->arg) {
884 	case PCBIT_IOCTL_GETSTAT:
885 		cmd->info.l2_status = dev->l2_state;
886 		break;
887 
888 	case PCBIT_IOCTL_STRLOAD:
889 		if (dev->l2_state == L2_RUNNING)
890 			return -EBUSY;
891 
892 		dev->unack_seq = dev->send_seq = dev->rcv_seq = 0;
893 
894 		dev->writeptr = dev->sh_mem;
895 		dev->readptr = dev->sh_mem + BANK2;
896 
897 		dev->l2_state = L2_LOADING;
898 		break;
899 
900 	case PCBIT_IOCTL_LWMODE:
901 		if (dev->l2_state != L2_LOADING)
902 			return -EINVAL;
903 
904 		dev->l2_state = L2_LWMODE;
905 		break;
906 
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;
912 
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;
919 
920 	case PCBIT_IOCTL_SETBYTE:
921 		if (dev->l2_state == L2_RUNNING)
922 			return -EBUSY;
923 
924 		/* check addr */
925 		if (cmd->info.rdp_byte.addr > BANK4)
926 			return -EFAULT;
927 
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;
933 
934 		/* check addr */
935 
936 		if (cmd->info.rdp_byte.addr > BANK4)
937 		{
938 			printk("getbyte: invalid addr %04x\n", cmd->info.rdp_byte.addr);
939 			return -EFAULT;
940 		}
941 
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;
974 	};
975 	return 0;
976 }
977 
978 /*
979  *        MSN list handling
980  *
981  *        if null reject all calls
982  *        if first entry has null MSN accept all calls
983  */
984 
pcbit_clear_msn(struct pcbit_dev * dev)985 static void pcbit_clear_msn(struct pcbit_dev *dev)
986 {
987 	struct msn_entry *ptr, *back;
988 
989 	for (ptr=dev->msn_list; ptr; )
990 	{
991 		back = ptr->next;
992 		kfree(ptr);
993 		ptr = back;
994 	}
995 
996 	dev->msn_list = NULL;
997 }
998 
pcbit_set_msn(struct pcbit_dev * dev,char * list)999 static void pcbit_set_msn(struct pcbit_dev *dev, char *list)
1000 {
1001 	struct msn_entry *ptr;
1002 	struct msn_entry *back = NULL;
1003 	char *cp, *sp;
1004 	int len;
1005 
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;
1011 		}
1012 
1013 		ptr->msn = NULL;
1014 
1015 		ptr->next = dev->msn_list;
1016 		dev->msn_list = ptr;
1017 
1018 		return;
1019 	}
1020 
1021 	if (dev->msn_list)
1022 		for (back=dev->msn_list; back->next; back=back->next);
1023 
1024 	sp = list;
1025 
1026 	do {
1027 		cp=strchr(sp, ',');
1028 		if (cp)
1029 			len = cp - sp;
1030 		else
1031 			len = strlen(sp);
1032 
1033 		ptr = kmalloc(sizeof(struct msn_entry), GFP_ATOMIC);
1034 
1035 		if (!ptr) {
1036 			printk(KERN_WARNING "kmalloc failed\n");
1037 			return;
1038 		}
1039 		ptr->next = NULL;
1040 
1041 		ptr->msn = kmalloc(len, GFP_ATOMIC);
1042 		if (!ptr->msn) {
1043 			printk(KERN_WARNING "kmalloc failed\n");
1044 			kfree(ptr);
1045 			return;
1046 		}
1047 
1048 		memcpy(ptr->msn, sp, len - 1);
1049 		ptr->msn[len] = 0;
1050 
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);
1061 }
1062 
1063 /*
1064  *  check if we do signal or reject an incoming call
1065  */
pcbit_check_msn(struct pcbit_dev * dev,char * msn)1066 static int pcbit_check_msn(struct pcbit_dev *dev, char *msn)
1067 {
1068 	struct msn_entry *ptr;
1069 
1070 	for (ptr=dev->msn_list; ptr; ptr=ptr->next) {
1071 
1072 		if (ptr->msn == NULL)
1073 			return 1;
1074 
1075 		if (strcmp(ptr->msn, msn) == 0)
1076 			return 1;
1077 	}
1078 
1079 	return 0;
1080 }
1081