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