Merge branch 'upstream' of git://lost.foo-projects.org/~ahkok/git/netdev-2.6 into...
[linux-2.6] / drivers / isdn / icn / icn.c
1 /* $Id: icn.c,v 1.65.6.8 2001/09/23 22:24:55 kai Exp $
2  *
3  * ISDN low-level module for the ICN active ISDN-Card.
4  *
5  * Copyright 1994,95,96 by Fritz Elfert (fritz@isdn4linux.de)
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11
12 #include "icn.h"
13 #include <linux/module.h>
14 #include <linux/init.h>
15
16 static int portbase = ICN_BASEADDR;
17 static unsigned long membase = ICN_MEMADDR;
18 static char *icn_id = "\0";
19 static char *icn_id2 = "\0";
20
21 MODULE_DESCRIPTION("ISDN4Linux: Driver for ICN active ISDN card");
22 MODULE_AUTHOR("Fritz Elfert");
23 MODULE_LICENSE("GPL");
24 module_param(portbase, int, 0);
25 MODULE_PARM_DESC(portbase, "Port address of first card");
26 module_param(membase, ulong, 0);
27 MODULE_PARM_DESC(membase, "Shared memory address of all cards");
28 module_param(icn_id, charp, 0);
29 MODULE_PARM_DESC(icn_id, "ID-String of first card");
30 module_param(icn_id2, charp, 0);
31 MODULE_PARM_DESC(icn_id2, "ID-String of first card, second S0 (4B only)");
32
33 /*
34  * Verbose bootcode- and protocol-downloading.
35  */
36 #undef BOOT_DEBUG
37
38 /*
39  * Verbose Shmem-Mapping.
40  */
41 #undef MAP_DEBUG
42
43 static char
44 *revision = "$Revision: 1.65.6.8 $";
45
46 static int icn_addcard(int, char *, char *);
47
48 /*
49  * Free send-queue completely.
50  * Parameter:
51  *   card   = pointer to card struct
52  *   channel = channel number
53  */
54 static void
55 icn_free_queue(icn_card * card, int channel)
56 {
57         struct sk_buff_head *queue = &card->spqueue[channel];
58         struct sk_buff *skb;
59
60         skb_queue_purge(queue);
61         card->xlen[channel] = 0;
62         card->sndcount[channel] = 0;
63         if ((skb = card->xskb[channel])) {
64                 card->xskb[channel] = NULL;
65                 dev_kfree_skb(skb);
66         }
67 }
68
69 /* Put a value into a shift-register, highest bit first.
70  * Parameters:
71  *            port     = port for output (bit 0 is significant)
72  *            val      = value to be output
73  *            firstbit = Bit-Number of highest bit
74  *            bitcount = Number of bits to output
75  */
76 static inline void
77 icn_shiftout(unsigned short port,
78              unsigned long val,
79              int firstbit,
80              int bitcount)
81 {
82
83         register u_char s;
84         register u_char c;
85
86         for (s = firstbit, c = bitcount; c > 0; s--, c--)
87                 OUTB_P((u_char) ((val >> s) & 1) ? 0xff : 0, port);
88 }
89
90 /*
91  * disable a cards shared memory
92  */
93 static inline void
94 icn_disable_ram(icn_card * card)
95 {
96         OUTB_P(0, ICN_MAPRAM);
97 }
98
99 /*
100  * enable a cards shared memory
101  */
102 static inline void
103 icn_enable_ram(icn_card * card)
104 {
105         OUTB_P(0xff, ICN_MAPRAM);
106 }
107
108 /*
109  * Map a cards channel0 (Bank0/Bank8) or channel1 (Bank4/Bank12)
110  *
111  * must called with holding the devlock
112  */
113 static inline void
114 icn_map_channel(icn_card * card, int channel)
115 {
116 #ifdef MAP_DEBUG
117         printk(KERN_DEBUG "icn_map_channel %d %d\n", dev.channel, channel);
118 #endif
119         if ((channel == dev.channel) && (card == dev.mcard))
120                 return;
121         if (dev.mcard)
122                 icn_disable_ram(dev.mcard);
123         icn_shiftout(ICN_BANK, chan2bank[channel], 3, 4);       /* Select Bank          */
124         icn_enable_ram(card);
125         dev.mcard = card;
126         dev.channel = channel;
127 #ifdef MAP_DEBUG
128         printk(KERN_DEBUG "icn_map_channel done\n");
129 #endif
130 }
131
132 /*
133  * Lock a cards channel.
134  * Return 0 if requested card/channel is unmapped (failure).
135  * Return 1 on success.
136  *
137  * must called with holding the devlock
138  */
139 static inline int
140 icn_lock_channel(icn_card * card, int channel)
141 {
142         register int retval;
143
144 #ifdef MAP_DEBUG
145         printk(KERN_DEBUG "icn_lock_channel %d\n", channel);
146 #endif
147         if ((dev.channel == channel) && (card == dev.mcard)) {
148                 dev.chanlock++;
149                 retval = 1;
150 #ifdef MAP_DEBUG
151                 printk(KERN_DEBUG "icn_lock_channel %d OK\n", channel);
152 #endif
153         } else {
154                 retval = 0;
155 #ifdef MAP_DEBUG
156                 printk(KERN_DEBUG "icn_lock_channel %d FAILED, dc=%d\n", channel, dev.channel);
157 #endif
158         }
159         return retval;
160 }
161
162 /*
163  * Release current card/channel lock
164  *
165  * must called with holding the devlock
166  */
167 static inline void
168 __icn_release_channel(void)
169 {
170 #ifdef MAP_DEBUG
171         printk(KERN_DEBUG "icn_release_channel l=%d\n", dev.chanlock);
172 #endif
173         if (dev.chanlock > 0)
174                 dev.chanlock--;
175 }
176
177 /*
178  * Release current card/channel lock
179  */
180 static inline void
181 icn_release_channel(void)
182 {
183         ulong flags;
184
185         spin_lock_irqsave(&dev.devlock, flags);
186         __icn_release_channel();
187         spin_unlock_irqrestore(&dev.devlock, flags);
188 }
189
190 /*
191  * Try to map and lock a cards channel.
192  * Return 1 on success, 0 on failure.
193  */
194 static inline int
195 icn_trymaplock_channel(icn_card * card, int channel)
196 {
197         ulong flags;
198
199 #ifdef MAP_DEBUG
200         printk(KERN_DEBUG "trymaplock c=%d dc=%d l=%d\n", channel, dev.channel,
201                dev.chanlock);
202 #endif
203         spin_lock_irqsave(&dev.devlock, flags);
204         if ((!dev.chanlock) ||
205             ((dev.channel == channel) && (dev.mcard == card))) {
206                 dev.chanlock++;
207                 icn_map_channel(card, channel);
208                 spin_unlock_irqrestore(&dev.devlock, flags);
209 #ifdef MAP_DEBUG
210                 printk(KERN_DEBUG "trymaplock %d OK\n", channel);
211 #endif
212                 return 1;
213         }
214         spin_unlock_irqrestore(&dev.devlock, flags);
215 #ifdef MAP_DEBUG
216         printk(KERN_DEBUG "trymaplock %d FAILED\n", channel);
217 #endif
218         return 0;
219 }
220
221 /*
222  * Release current card/channel lock,
223  * then map same or other channel without locking.
224  */
225 static inline void
226 icn_maprelease_channel(icn_card * card, int channel)
227 {
228         ulong flags;
229
230 #ifdef MAP_DEBUG
231         printk(KERN_DEBUG "map_release c=%d l=%d\n", channel, dev.chanlock);
232 #endif
233         spin_lock_irqsave(&dev.devlock, flags);
234         if (dev.chanlock > 0)
235                 dev.chanlock--;
236         if (!dev.chanlock)
237                 icn_map_channel(card, channel);
238         spin_unlock_irqrestore(&dev.devlock, flags);
239 }
240
241 /* Get Data from the B-Channel, assemble fragmented packets and put them
242  * into receive-queue. Wake up any B-Channel-reading processes.
243  * This routine is called via timer-callback from icn_pollbchan().
244  */
245
246 static void
247 icn_pollbchan_receive(int channel, icn_card * card)
248 {
249         int mch = channel + ((card->secondhalf) ? 2 : 0);
250         int eflag;
251         int cnt;
252         struct sk_buff *skb;
253
254         if (icn_trymaplock_channel(card, mch)) {
255                 while (rbavl) {
256                         cnt = readb(&rbuf_l);
257                         if ((card->rcvidx[channel] + cnt) > 4000) {
258                                 printk(KERN_WARNING
259                                        "icn: (%s) bogus packet on ch%d, dropping.\n",
260                                        CID,
261                                        channel + 1);
262                                 card->rcvidx[channel] = 0;
263                                 eflag = 0;
264                         } else {
265                                 memcpy_fromio(&card->rcvbuf[channel][card->rcvidx[channel]],
266                                               &rbuf_d, cnt);
267                                 card->rcvidx[channel] += cnt;
268                                 eflag = readb(&rbuf_f);
269                         }
270                         rbnext;
271                         icn_maprelease_channel(card, mch & 2);
272                         if (!eflag) {
273                                 if ((cnt = card->rcvidx[channel])) {
274                                         if (!(skb = dev_alloc_skb(cnt))) {
275                                                 printk(KERN_WARNING "icn: receive out of memory\n");
276                                                 break;
277                                         }
278                                         memcpy(skb_put(skb, cnt), card->rcvbuf[channel], cnt);
279                                         card->rcvidx[channel] = 0;
280                                         card->interface.rcvcallb_skb(card->myid, channel, skb);
281                                 }
282                         }
283                         if (!icn_trymaplock_channel(card, mch))
284                                 break;
285                 }
286                 icn_maprelease_channel(card, mch & 2);
287         }
288 }
289
290 /* Send data-packet to B-Channel, split it up into fragments of
291  * ICN_FRAGSIZE length. If last fragment is sent out, signal
292  * success to upper layers via statcallb with ISDN_STAT_BSENT argument.
293  * This routine is called via timer-callback from icn_pollbchan() or
294  * directly from icn_sendbuf().
295  */
296
297 static void
298 icn_pollbchan_send(int channel, icn_card * card)
299 {
300         int mch = channel + ((card->secondhalf) ? 2 : 0);
301         int cnt;
302         unsigned long flags;
303         struct sk_buff *skb;
304         isdn_ctrl cmd;
305
306         if (!(card->sndcount[channel] || card->xskb[channel] ||
307               !skb_queue_empty(&card->spqueue[channel])))
308                 return;
309         if (icn_trymaplock_channel(card, mch)) {
310                 while (sbfree && 
311                        (card->sndcount[channel] ||
312                         !skb_queue_empty(&card->spqueue[channel]) ||
313                         card->xskb[channel])) {
314                         spin_lock_irqsave(&card->lock, flags);
315                         if (card->xmit_lock[channel]) {
316                                 spin_unlock_irqrestore(&card->lock, flags);
317                                 break;
318                         }
319                         card->xmit_lock[channel]++;
320                         spin_unlock_irqrestore(&card->lock, flags);
321                         skb = card->xskb[channel];
322                         if (!skb) {
323                                 skb = skb_dequeue(&card->spqueue[channel]);
324                                 if (skb) {
325                                         /* Pop ACK-flag off skb.
326                                          * Store length to xlen.
327                                          */
328                                         if (*(skb_pull(skb,1)))
329                                                 card->xlen[channel] = skb->len;
330                                         else
331                                                 card->xlen[channel] = 0;
332                                 }
333                         }
334                         if (!skb)
335                                 break;
336                         if (skb->len > ICN_FRAGSIZE) {
337                                 writeb(0xff, &sbuf_f);
338                                 cnt = ICN_FRAGSIZE;
339                         } else {
340                                 writeb(0x0, &sbuf_f);
341                                 cnt = skb->len;
342                         }
343                         writeb(cnt, &sbuf_l);
344                         memcpy_toio(&sbuf_d, skb->data, cnt);
345                         skb_pull(skb, cnt);
346                         sbnext; /* switch to next buffer        */
347                         icn_maprelease_channel(card, mch & 2);
348                         spin_lock_irqsave(&card->lock, flags);
349                         card->sndcount[channel] -= cnt;
350                         if (!skb->len) {
351                                 if (card->xskb[channel])
352                                         card->xskb[channel] = NULL;
353                                 card->xmit_lock[channel] = 0;
354                                 spin_unlock_irqrestore(&card->lock, flags);
355                                 dev_kfree_skb(skb);
356                                 if (card->xlen[channel]) {
357                                         cmd.command = ISDN_STAT_BSENT;
358                                         cmd.driver = card->myid;
359                                         cmd.arg = channel;
360                                         cmd.parm.length = card->xlen[channel];
361                                         card->interface.statcallb(&cmd);
362                                 }
363                         } else {
364                                 card->xskb[channel] = skb;
365                                 card->xmit_lock[channel] = 0;
366                                 spin_unlock_irqrestore(&card->lock, flags);
367                         }
368                         if (!icn_trymaplock_channel(card, mch))
369                                 break;
370                 }
371                 icn_maprelease_channel(card, mch & 2);
372         }
373 }
374
375 /* Send/Receive Data to/from the B-Channel.
376  * This routine is called via timer-callback.
377  * It schedules itself while any B-Channel is open.
378  */
379
380 static void
381 icn_pollbchan(unsigned long data)
382 {
383         icn_card *card = (icn_card *) data;
384         unsigned long flags;
385
386         if (card->flags & ICN_FLAGS_B1ACTIVE) {
387                 icn_pollbchan_receive(0, card);
388                 icn_pollbchan_send(0, card);
389         }
390         if (card->flags & ICN_FLAGS_B2ACTIVE) {
391                 icn_pollbchan_receive(1, card);
392                 icn_pollbchan_send(1, card);
393         }
394         if (card->flags & (ICN_FLAGS_B1ACTIVE | ICN_FLAGS_B2ACTIVE)) {
395                 /* schedule b-channel polling again */
396                 spin_lock_irqsave(&card->lock, flags);
397                 mod_timer(&card->rb_timer, jiffies+ICN_TIMER_BCREAD);
398                 card->flags |= ICN_FLAGS_RBTIMER;
399                 spin_unlock_irqrestore(&card->lock, flags);
400         } else
401                 card->flags &= ~ICN_FLAGS_RBTIMER;
402 }
403
404 typedef struct icn_stat {
405         char *statstr;
406         int command;
407         int action;
408 } icn_stat;
409 /* *INDENT-OFF* */
410 static icn_stat icn_stat_table[] =
411 {
412         {"BCON_",          ISDN_STAT_BCONN, 1}, /* B-Channel connected        */
413         {"BDIS_",          ISDN_STAT_BHUP,  2}, /* B-Channel disconnected     */
414         /*
415         ** add d-channel connect and disconnect support to link-level
416         */
417         {"DCON_",          ISDN_STAT_DCONN, 10},        /* D-Channel connected        */
418         {"DDIS_",          ISDN_STAT_DHUP,  11},        /* D-Channel disconnected     */
419         {"DCAL_I",         ISDN_STAT_ICALL, 3}, /* Incoming call dialup-line  */
420         {"DSCA_I",         ISDN_STAT_ICALL, 3}, /* Incoming call 1TR6-SPV     */
421         {"FCALL",          ISDN_STAT_ICALL, 4}, /* Leased line connection up  */
422         {"CIF",            ISDN_STAT_CINF,  5}, /* Charge-info, 1TR6-type     */
423         {"AOC",            ISDN_STAT_CINF,  6}, /* Charge-info, DSS1-type     */
424         {"CAU",            ISDN_STAT_CAUSE, 7}, /* Cause code                 */
425         {"TEI OK",         ISDN_STAT_RUN,   0}, /* Card connected to wallplug */
426         {"E_L1: ACT FAIL", ISDN_STAT_BHUP,  8}, /* Layer-1 activation failed  */
427         {"E_L2: DATA LIN", ISDN_STAT_BHUP,  8}, /* Layer-2 data link lost     */
428         {"E_L1: ACTIVATION FAILED",
429                                            ISDN_STAT_BHUP,  8}, /* Layer-1 activation failed  */
430         {NULL, 0, -1}
431 };
432 /* *INDENT-ON* */
433
434
435 /*
436  * Check Statusqueue-Pointer from isdn-cards.
437  * If there are new status-replies from the interface, check
438  * them against B-Channel-connects/disconnects and set flags accordingly.
439  * Wake-Up any processes, who are reading the status-device.
440  * If there are B-Channels open, initiate a timer-callback to
441  * icn_pollbchan().
442  * This routine is called periodically via timer.
443  */
444
445 static void
446 icn_parse_status(u_char * status, int channel, icn_card * card)
447 {
448         icn_stat *s = icn_stat_table;
449         int action = -1;
450         unsigned long flags;
451         isdn_ctrl cmd;
452
453         while (s->statstr) {
454                 if (!strncmp(status, s->statstr, strlen(s->statstr))) {
455                         cmd.command = s->command;
456                         action = s->action;
457                         break;
458                 }
459                 s++;
460         }
461         if (action == -1)
462                 return;
463         cmd.driver = card->myid;
464         cmd.arg = channel;
465         switch (action) {
466                 case 11:
467                         spin_lock_irqsave(&card->lock, flags);
468                         icn_free_queue(card,channel);
469                         card->rcvidx[channel] = 0;
470
471                         if (card->flags & 
472                             ((channel)?ICN_FLAGS_B2ACTIVE:ICN_FLAGS_B1ACTIVE)) {
473                                 
474                                 isdn_ctrl ncmd;
475                                 
476                                 card->flags &= ~((channel)?
477                                                  ICN_FLAGS_B2ACTIVE:ICN_FLAGS_B1ACTIVE);
478                                 
479                                 memset(&ncmd, 0, sizeof(ncmd));
480                                 
481                                 ncmd.driver = card->myid;
482                                 ncmd.arg = channel;
483                                 ncmd.command = ISDN_STAT_BHUP;
484                                 spin_unlock_irqrestore(&card->lock, flags);
485                                 card->interface.statcallb(&cmd);
486                         } else
487                                 spin_unlock_irqrestore(&card->lock, flags);
488                         break;
489                 case 1:
490                         spin_lock_irqsave(&card->lock, flags);
491                         icn_free_queue(card,channel);
492                         card->flags |= (channel) ?
493                             ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE;
494                         spin_unlock_irqrestore(&card->lock, flags);
495                         break;
496                 case 2:
497                         spin_lock_irqsave(&card->lock, flags);
498                         card->flags &= ~((channel) ?
499                                 ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE);
500                         icn_free_queue(card, channel);
501                         card->rcvidx[channel] = 0;
502                         spin_unlock_irqrestore(&card->lock, flags);
503                         break;
504                 case 3:
505                         {
506                                 char *t = status + 6;
507                                 char *s = strchr(t, ',');
508
509                                 *s++ = '\0';
510                                 strlcpy(cmd.parm.setup.phone, t,
511                                         sizeof(cmd.parm.setup.phone));
512                                 s = strchr(t = s, ',');
513                                 *s++ = '\0';
514                                 if (!strlen(t))
515                                         cmd.parm.setup.si1 = 0;
516                                 else
517                                         cmd.parm.setup.si1 =
518                                             simple_strtoul(t, NULL, 10);
519                                 s = strchr(t = s, ',');
520                                 *s++ = '\0';
521                                 if (!strlen(t))
522                                         cmd.parm.setup.si2 = 0;
523                                 else
524                                         cmd.parm.setup.si2 =
525                                             simple_strtoul(t, NULL, 10);
526                                 strlcpy(cmd.parm.setup.eazmsn, s,
527                                         sizeof(cmd.parm.setup.eazmsn));
528                         }
529                         cmd.parm.setup.plan = 0;
530                         cmd.parm.setup.screen = 0;
531                         break;
532                 case 4:
533                         sprintf(cmd.parm.setup.phone, "LEASED%d", card->myid);
534                         sprintf(cmd.parm.setup.eazmsn, "%d", channel + 1);
535                         cmd.parm.setup.si1 = 7;
536                         cmd.parm.setup.si2 = 0;
537                         cmd.parm.setup.plan = 0;
538                         cmd.parm.setup.screen = 0;
539                         break;
540                 case 5:
541                         strlcpy(cmd.parm.num, status + 3, sizeof(cmd.parm.num));
542                         break;
543                 case 6:
544                         snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%d",
545                              (int) simple_strtoul(status + 7, NULL, 16));
546                         break;
547                 case 7:
548                         status += 3;
549                         if (strlen(status) == 4)
550                                 snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%s%c%c",
551                                      status + 2, *status, *(status + 1));
552                         else
553                                 strlcpy(cmd.parm.num, status + 1, sizeof(cmd.parm.num));
554                         break;
555                 case 8:
556                         spin_lock_irqsave(&card->lock, flags);
557                         card->flags &= ~ICN_FLAGS_B1ACTIVE;
558                         icn_free_queue(card, 0);
559                         card->rcvidx[0] = 0;
560                         spin_unlock_irqrestore(&card->lock, flags);
561                         cmd.arg = 0;
562                         cmd.driver = card->myid;
563                         card->interface.statcallb(&cmd);
564                         cmd.command = ISDN_STAT_DHUP;
565                         cmd.arg = 0;
566                         cmd.driver = card->myid;
567                         card->interface.statcallb(&cmd);
568                         cmd.command = ISDN_STAT_BHUP;
569                         spin_lock_irqsave(&card->lock, flags);
570                         card->flags &= ~ICN_FLAGS_B2ACTIVE;
571                         icn_free_queue(card, 1);
572                         card->rcvidx[1] = 0;
573                         spin_unlock_irqrestore(&card->lock, flags);
574                         cmd.arg = 1;
575                         cmd.driver = card->myid;
576                         card->interface.statcallb(&cmd);
577                         cmd.command = ISDN_STAT_DHUP;
578                         cmd.arg = 1;
579                         cmd.driver = card->myid;
580                         break;
581         }
582         card->interface.statcallb(&cmd);
583         return;
584 }
585
586 static void
587 icn_putmsg(icn_card * card, unsigned char c)
588 {
589         ulong flags;
590
591         spin_lock_irqsave(&card->lock, flags);
592         *card->msg_buf_write++ = (c == 0xff) ? '\n' : c;
593         if (card->msg_buf_write == card->msg_buf_read) {
594                 if (++card->msg_buf_read > card->msg_buf_end)
595                         card->msg_buf_read = card->msg_buf;
596         }
597         if (card->msg_buf_write > card->msg_buf_end)
598                 card->msg_buf_write = card->msg_buf;
599         spin_unlock_irqrestore(&card->lock, flags);
600 }
601
602 static void
603 icn_polldchan(unsigned long data)
604 {
605         icn_card *card = (icn_card *) data;
606         int mch = card->secondhalf ? 2 : 0;
607         int avail = 0;
608         int left;
609         u_char c;
610         int ch;
611         unsigned long flags;
612         int i;
613         u_char *p;
614         isdn_ctrl cmd;
615
616         if (icn_trymaplock_channel(card, mch)) {
617                 avail = msg_avail;
618                 for (left = avail, i = readb(&msg_o); left > 0; i++, left--) {
619                         c = readb(&dev.shmem->comm_buffers.iopc_buf[i & 0xff]);
620                         icn_putmsg(card, c);
621                         if (c == 0xff) {
622                                 card->imsg[card->iptr] = 0;
623                                 card->iptr = 0;
624                                 if (card->imsg[0] == '0' && card->imsg[1] >= '0' &&
625                                     card->imsg[1] <= '2' && card->imsg[2] == ';') {
626                                         ch = (card->imsg[1] - '0') - 1;
627                                         p = &card->imsg[3];
628                                         icn_parse_status(p, ch, card);
629                                 } else {
630                                         p = card->imsg;
631                                         if (!strncmp(p, "DRV1.", 5)) {
632                                                 u_char vstr[10];
633                                                 u_char *q = vstr;
634
635                                                 printk(KERN_INFO "icn: (%s) %s\n", CID, p);
636                                                 if (!strncmp(p + 7, "TC", 2)) {
637                                                         card->ptype = ISDN_PTYPE_1TR6;
638                                                         card->interface.features |= ISDN_FEATURE_P_1TR6;
639                                                         printk(KERN_INFO
640                                                                "icn: (%s) 1TR6-Protocol loaded and running\n", CID);
641                                                 }
642                                                 if (!strncmp(p + 7, "EC", 2)) {
643                                                         card->ptype = ISDN_PTYPE_EURO;
644                                                         card->interface.features |= ISDN_FEATURE_P_EURO;
645                                                         printk(KERN_INFO
646                                                                "icn: (%s) Euro-Protocol loaded and running\n", CID);
647                                                 }
648                                                 p = strstr(card->imsg, "BRV") + 3;
649                                                 while (*p) {
650                                                         if (*p >= '0' && *p <= '9')
651                                                                 *q++ = *p;
652                                                         p++;
653                                                 }
654                                                 *q = '\0';
655                                                 strcat(vstr, "000");
656                                                 vstr[3] = '\0';
657                                                 card->fw_rev = (int) simple_strtoul(vstr, NULL, 10);
658                                                 continue;
659
660                                         }
661                                 }
662                         } else {
663                                 card->imsg[card->iptr] = c;
664                                 if (card->iptr < 59)
665                                         card->iptr++;
666                         }
667                 }
668                 writeb((readb(&msg_o) + avail) & 0xff, &msg_o);
669                 icn_release_channel();
670         }
671         if (avail) {
672                 cmd.command = ISDN_STAT_STAVAIL;
673                 cmd.driver = card->myid;
674                 cmd.arg = avail;
675                 card->interface.statcallb(&cmd);
676         }
677         spin_lock_irqsave(&card->lock, flags);
678         if (card->flags & (ICN_FLAGS_B1ACTIVE | ICN_FLAGS_B2ACTIVE))
679                 if (!(card->flags & ICN_FLAGS_RBTIMER)) {
680                         /* schedule b-channel polling */
681                         card->flags |= ICN_FLAGS_RBTIMER;
682                         del_timer(&card->rb_timer);
683                         card->rb_timer.function = icn_pollbchan;
684                         card->rb_timer.data = (unsigned long) card;
685                         card->rb_timer.expires = jiffies + ICN_TIMER_BCREAD;
686                         add_timer(&card->rb_timer);
687                 }
688         /* schedule again */
689         mod_timer(&card->st_timer, jiffies+ICN_TIMER_DCREAD);
690         spin_unlock_irqrestore(&card->lock, flags);
691 }
692
693 /* Append a packet to the transmit buffer-queue.
694  * Parameters:
695  *   channel = Number of B-channel
696  *   skb     = pointer to sk_buff
697  *   card    = pointer to card-struct
698  * Return:
699  *   Number of bytes transferred, -E??? on error
700  */
701
702 static int
703 icn_sendbuf(int channel, int ack, struct sk_buff *skb, icn_card * card)
704 {
705         int len = skb->len;
706         unsigned long flags;
707         struct sk_buff *nskb;
708
709         if (len > 4000) {
710                 printk(KERN_WARNING
711                        "icn: Send packet too large\n");
712                 return -EINVAL;
713         }
714         if (len) {
715                 if (!(card->flags & (channel) ? ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE))
716                         return 0;
717                 if (card->sndcount[channel] > ICN_MAX_SQUEUE)
718                         return 0;
719                 #warning TODO test headroom or use skb->nb to flag ACK
720                 nskb = skb_clone(skb, GFP_ATOMIC);
721                 if (nskb) {
722                         /* Push ACK flag as one
723                          * byte in front of data.
724                          */
725                         *(skb_push(nskb, 1)) = ack?1:0;
726                         skb_queue_tail(&card->spqueue[channel], nskb);
727                         dev_kfree_skb(skb);
728                 } else
729                         len = 0;
730                 spin_lock_irqsave(&card->lock, flags);
731                 card->sndcount[channel] += len;
732                 spin_unlock_irqrestore(&card->lock, flags);
733         }
734         return len;
735 }
736
737 /*
738  * Check card's status after starting the bootstrap loader.
739  * On entry, the card's shared memory has already to be mapped.
740  * Return:
741  *   0 on success (Boot loader ready)
742  *   -EIO on failure (timeout)
743  */
744 static int
745 icn_check_loader(int cardnumber)
746 {
747         int timer = 0;
748
749         while (1) {
750 #ifdef BOOT_DEBUG
751                 printk(KERN_DEBUG "Loader %d ?\n", cardnumber);
752 #endif
753                 if (readb(&dev.shmem->data_control.scns) ||
754                     readb(&dev.shmem->data_control.scnr)) {
755                         if (timer++ > 5) {
756                                 printk(KERN_WARNING
757                                        "icn: Boot-Loader %d timed out.\n",
758                                        cardnumber);
759                                 icn_release_channel();
760                                 return -EIO;
761                         }
762 #ifdef BOOT_DEBUG
763                         printk(KERN_DEBUG "Loader %d TO?\n", cardnumber);
764 #endif
765                         msleep_interruptible(ICN_BOOT_TIMEOUT1);
766                 } else {
767 #ifdef BOOT_DEBUG
768                         printk(KERN_DEBUG "Loader %d OK\n", cardnumber);
769 #endif
770                         icn_release_channel();
771                         return 0;
772                 }
773         }
774 }
775
776 /* Load the boot-code into the interface-card's memory and start it.
777  * Always called from user-process.
778  *
779  * Parameters:
780  *            buffer = pointer to packet
781  * Return:
782  *        0 if successfully loaded
783  */
784
785 #ifdef BOOT_DEBUG
786 #define SLEEP(sec) { \
787 int slsec = sec; \
788   printk(KERN_DEBUG "SLEEP(%d)\n",slsec); \
789   while (slsec) { \
790     msleep_interruptible(1000); \
791     slsec--; \
792   } \
793 }
794 #else
795 #define SLEEP(sec)
796 #endif
797
798 static int
799 icn_loadboot(u_char __user * buffer, icn_card * card)
800 {
801         int ret;
802         u_char *codebuf;
803         unsigned long flags;
804
805 #ifdef BOOT_DEBUG
806         printk(KERN_DEBUG "icn_loadboot called, buffaddr=%08lx\n", (ulong) buffer);
807 #endif
808         if (!(codebuf = kmalloc(ICN_CODE_STAGE1, GFP_KERNEL))) {
809                 printk(KERN_WARNING "icn: Could not allocate code buffer\n");
810                 ret = -ENOMEM;
811                 goto out;
812         }
813         if (copy_from_user(codebuf, buffer, ICN_CODE_STAGE1)) {
814                 ret = -EFAULT;
815                 goto out_kfree;
816         }
817         if (!card->rvalid) {
818                 if (!request_region(card->port, ICN_PORTLEN, card->regname)) {
819                         printk(KERN_WARNING
820                                "icn: (%s) ports 0x%03x-0x%03x in use.\n",
821                                CID,
822                                card->port,
823                                card->port + ICN_PORTLEN);
824                         ret = -EBUSY;
825                         goto out_kfree;
826                 }
827                 card->rvalid = 1;
828                 if (card->doubleS0)
829                         card->other->rvalid = 1;
830         }
831         if (!dev.mvalid) {
832                 if (!request_mem_region(dev.memaddr, 0x4000, "icn-isdn (all cards)")) {
833                         printk(KERN_WARNING
834                                "icn: memory at 0x%08lx in use.\n", dev.memaddr);
835                         ret = -EBUSY;
836                         goto out_kfree;
837                 }
838                 dev.shmem = ioremap(dev.memaddr, 0x4000);
839                 dev.mvalid = 1;
840         }
841         OUTB_P(0, ICN_RUN);     /* Reset Controller */
842         OUTB_P(0, ICN_MAPRAM);  /* Disable RAM      */
843         icn_shiftout(ICN_CFG, 0x0f, 3, 4);      /* Windowsize= 16k  */
844         icn_shiftout(ICN_CFG, dev.memaddr, 23, 10);     /* Set RAM-Addr.    */
845 #ifdef BOOT_DEBUG
846         printk(KERN_DEBUG "shmem=%08lx\n", dev.memaddr);
847 #endif
848         SLEEP(1);
849 #ifdef BOOT_DEBUG
850         printk(KERN_DEBUG "Map Bank 0\n");
851 #endif
852         spin_lock_irqsave(&dev.devlock, flags);
853         icn_map_channel(card, 0);       /* Select Bank 0    */
854         icn_lock_channel(card, 0);      /* Lock Bank 0      */
855         spin_unlock_irqrestore(&dev.devlock, flags);
856         SLEEP(1);
857         memcpy_toio(dev.shmem, codebuf, ICN_CODE_STAGE1);       /* Copy code        */
858 #ifdef BOOT_DEBUG
859         printk(KERN_DEBUG "Bootloader transferred\n");
860 #endif
861         if (card->doubleS0) {
862                 SLEEP(1);
863 #ifdef BOOT_DEBUG
864                 printk(KERN_DEBUG "Map Bank 8\n");
865 #endif
866                 spin_lock_irqsave(&dev.devlock, flags);
867                 __icn_release_channel();
868                 icn_map_channel(card, 2);       /* Select Bank 8   */
869                 icn_lock_channel(card, 2);      /* Lock Bank 8     */
870                 spin_unlock_irqrestore(&dev.devlock, flags);
871                 SLEEP(1);
872                 memcpy_toio(dev.shmem, codebuf, ICN_CODE_STAGE1);       /* Copy code        */
873 #ifdef BOOT_DEBUG
874                 printk(KERN_DEBUG "Bootloader transferred\n");
875 #endif
876         }
877         SLEEP(1);
878         OUTB_P(0xff, ICN_RUN);  /* Start Boot-Code */
879         if ((ret = icn_check_loader(card->doubleS0 ? 2 : 1))) {
880                 goto out_kfree;
881         }
882         if (!card->doubleS0) {
883                 ret = 0;
884                 goto out_kfree;
885         }
886         /* reached only, if we have a Double-S0-Card */
887 #ifdef BOOT_DEBUG
888         printk(KERN_DEBUG "Map Bank 0\n");
889 #endif
890         spin_lock_irqsave(&dev.devlock, flags);
891         icn_map_channel(card, 0);       /* Select Bank 0   */
892         icn_lock_channel(card, 0);      /* Lock Bank 0     */
893         spin_unlock_irqrestore(&dev.devlock, flags);
894         SLEEP(1);
895         ret = (icn_check_loader(1));
896
897  out_kfree:
898         kfree(codebuf);
899  out:
900         return ret;
901 }
902
903 static int
904 icn_loadproto(u_char __user * buffer, icn_card * card)
905 {
906         register u_char __user *p = buffer;
907         u_char codebuf[256];
908         uint left = ICN_CODE_STAGE2;
909         uint cnt;
910         int timer;
911         unsigned long flags;
912
913 #ifdef BOOT_DEBUG
914         printk(KERN_DEBUG "icn_loadproto called\n");
915 #endif
916         if (!access_ok(VERIFY_READ, buffer, ICN_CODE_STAGE2))
917                 return -EFAULT;
918         timer = 0;
919         spin_lock_irqsave(&dev.devlock, flags);
920         if (card->secondhalf) {
921                 icn_map_channel(card, 2);
922                 icn_lock_channel(card, 2);
923         } else {
924                 icn_map_channel(card, 0);
925                 icn_lock_channel(card, 0);
926         }
927         spin_unlock_irqrestore(&dev.devlock, flags);
928         while (left) {
929                 if (sbfree) {   /* If there is a free buffer...  */
930                         cnt = left;
931                         if (cnt > 256)
932                                 cnt = 256;
933                         if (copy_from_user(codebuf, p, cnt)) {
934                                 icn_maprelease_channel(card, 0);
935                                 return -EFAULT;
936                         }
937                         memcpy_toio(&sbuf_l, codebuf, cnt);     /* copy data                     */
938                         sbnext; /* switch to next buffer         */
939                         p += cnt;
940                         left -= cnt;
941                         timer = 0;
942                 } else {
943 #ifdef BOOT_DEBUG
944                         printk(KERN_DEBUG "boot 2 !sbfree\n");
945 #endif
946                         if (timer++ > 5) {
947                                 icn_maprelease_channel(card, 0);
948                                 return -EIO;
949                         }
950                         schedule_timeout_interruptible(10);
951                 }
952         }
953         writeb(0x20, &sbuf_n);
954         timer = 0;
955         while (1) {
956                 if (readb(&cmd_o) || readb(&cmd_i)) {
957 #ifdef BOOT_DEBUG
958                         printk(KERN_DEBUG "Proto?\n");
959 #endif
960                         if (timer++ > 5) {
961                                 printk(KERN_WARNING
962                                        "icn: (%s) Protocol timed out.\n",
963                                        CID);
964 #ifdef BOOT_DEBUG
965                                 printk(KERN_DEBUG "Proto TO!\n");
966 #endif
967                                 icn_maprelease_channel(card, 0);
968                                 return -EIO;
969                         }
970 #ifdef BOOT_DEBUG
971                         printk(KERN_DEBUG "Proto TO?\n");
972 #endif
973                         msleep_interruptible(ICN_BOOT_TIMEOUT1);
974                 } else {
975                         if ((card->secondhalf) || (!card->doubleS0)) {
976 #ifdef BOOT_DEBUG
977                                 printk(KERN_DEBUG "Proto loaded, install poll-timer %d\n",
978                                        card->secondhalf);
979 #endif
980                                 spin_lock_irqsave(&card->lock, flags);
981                                 init_timer(&card->st_timer);
982                                 card->st_timer.expires = jiffies + ICN_TIMER_DCREAD;
983                                 card->st_timer.function = icn_polldchan;
984                                 card->st_timer.data = (unsigned long) card;
985                                 add_timer(&card->st_timer);
986                                 card->flags |= ICN_FLAGS_RUNNING;
987                                 if (card->doubleS0) {
988                                         init_timer(&card->other->st_timer);
989                                         card->other->st_timer.expires = jiffies + ICN_TIMER_DCREAD;
990                                         card->other->st_timer.function = icn_polldchan;
991                                         card->other->st_timer.data = (unsigned long) card->other;
992                                         add_timer(&card->other->st_timer);
993                                         card->other->flags |= ICN_FLAGS_RUNNING;
994                                 }
995                                 spin_unlock_irqrestore(&card->lock, flags);
996                         }
997                         icn_maprelease_channel(card, 0);
998                         return 0;
999                 }
1000         }
1001 }
1002
1003 /* Read the Status-replies from the Interface */
1004 static int
1005 icn_readstatus(u_char __user *buf, int len, icn_card * card)
1006 {
1007         int count;
1008         u_char __user *p;
1009
1010         for (p = buf, count = 0; count < len; p++, count++) {
1011                 if (card->msg_buf_read == card->msg_buf_write)
1012                         return count;
1013                 put_user(*card->msg_buf_read++, p);
1014                 if (card->msg_buf_read > card->msg_buf_end)
1015                         card->msg_buf_read = card->msg_buf;
1016         }
1017         return count;
1018 }
1019
1020 /* Put command-strings into the command-queue of the Interface */
1021 static int
1022 icn_writecmd(const u_char * buf, int len, int user, icn_card * card)
1023 {
1024         int mch = card->secondhalf ? 2 : 0;
1025         int pp;
1026         int i;
1027         int count;
1028         int xcount;
1029         int ocount;
1030         int loop;
1031         unsigned long flags;
1032         int lastmap_channel;
1033         struct icn_card *lastmap_card;
1034         u_char *p;
1035         isdn_ctrl cmd;
1036         u_char msg[0x100];
1037
1038         ocount = 1;
1039         xcount = loop = 0;
1040         while (len) {
1041                 count = cmd_free;
1042                 if (count > len)
1043                         count = len;
1044                 if (user) {
1045                         if (copy_from_user(msg, buf, count))
1046                                 return -EFAULT;
1047                 } else
1048                         memcpy(msg, buf, count);
1049
1050                 spin_lock_irqsave(&dev.devlock, flags);
1051                 lastmap_card = dev.mcard;
1052                 lastmap_channel = dev.channel;
1053                 icn_map_channel(card, mch);
1054
1055                 icn_putmsg(card, '>');
1056                 for (p = msg, pp = readb(&cmd_i), i = count; i > 0; i--, p++, pp
1057                      ++) {
1058                         writeb((*p == '\n') ? 0xff : *p,
1059                            &dev.shmem->comm_buffers.pcio_buf[pp & 0xff]);
1060                         len--;
1061                         xcount++;
1062                         icn_putmsg(card, *p);
1063                         if ((*p == '\n') && (i > 1)) {
1064                                 icn_putmsg(card, '>');
1065                                 ocount++;
1066                         }
1067                         ocount++;
1068                 }
1069                 writeb((readb(&cmd_i) + count) & 0xff, &cmd_i);
1070                 if (lastmap_card)
1071                         icn_map_channel(lastmap_card, lastmap_channel);
1072                 spin_unlock_irqrestore(&dev.devlock, flags);
1073                 if (len) {
1074                         mdelay(1);
1075                         if (loop++ > 20)
1076                                 break;
1077                 } else
1078                         break;
1079         }
1080         if (len && (!user))
1081                 printk(KERN_WARNING "icn: writemsg incomplete!\n");
1082         cmd.command = ISDN_STAT_STAVAIL;
1083         cmd.driver = card->myid;
1084         cmd.arg = ocount;
1085         card->interface.statcallb(&cmd);
1086         return xcount;
1087 }
1088
1089 /*
1090  * Delete card's pending timers, send STOP to linklevel
1091  */
1092 static void
1093 icn_stopcard(icn_card * card)
1094 {
1095         unsigned long flags;
1096         isdn_ctrl cmd;
1097
1098         spin_lock_irqsave(&card->lock, flags);
1099         if (card->flags & ICN_FLAGS_RUNNING) {
1100                 card->flags &= ~ICN_FLAGS_RUNNING;
1101                 del_timer(&card->st_timer);
1102                 del_timer(&card->rb_timer);
1103                 spin_unlock_irqrestore(&card->lock, flags);
1104                 cmd.command = ISDN_STAT_STOP;
1105                 cmd.driver = card->myid;
1106                 card->interface.statcallb(&cmd);
1107                 if (card->doubleS0)
1108                         icn_stopcard(card->other);
1109         } else
1110                 spin_unlock_irqrestore(&card->lock, flags);
1111 }
1112
1113 static void
1114 icn_stopallcards(void)
1115 {
1116         icn_card *p = cards;
1117
1118         while (p) {
1119                 icn_stopcard(p);
1120                 p = p->next;
1121         }
1122 }
1123
1124 /*
1125  * Unmap all cards, because some of them may be mapped accidetly during
1126  * autoprobing of some network drivers (SMC-driver?)
1127  */
1128 static void
1129 icn_disable_cards(void)
1130 {
1131         icn_card *card = cards;
1132
1133         while (card) {
1134                 if (!request_region(card->port, ICN_PORTLEN, "icn-isdn")) {
1135                         printk(KERN_WARNING
1136                                "icn: (%s) ports 0x%03x-0x%03x in use.\n",
1137                                CID,
1138                                card->port,
1139                                card->port + ICN_PORTLEN);
1140                 } else {
1141                         OUTB_P(0, ICN_RUN);     /* Reset Controller     */
1142                         OUTB_P(0, ICN_MAPRAM);  /* Disable RAM          */
1143                         release_region(card->port, ICN_PORTLEN);
1144                 }
1145                 card = card->next;
1146         }
1147 }
1148
1149 static int
1150 icn_command(isdn_ctrl * c, icn_card * card)
1151 {
1152         ulong a;
1153         ulong flags;
1154         int i;
1155         char cbuf[60];
1156         isdn_ctrl cmd;
1157         icn_cdef cdef;
1158         char __user *arg;
1159
1160         switch (c->command) {
1161                 case ISDN_CMD_IOCTL:
1162                         memcpy(&a, c->parm.num, sizeof(ulong));
1163                         arg = (char __user *)a;
1164                         switch (c->arg) {
1165                                 case ICN_IOCTL_SETMMIO:
1166                                         if (dev.memaddr != (a & 0x0ffc000)) {
1167                                                 if (!request_mem_region(a & 0x0ffc000, 0x4000, "icn-isdn (all cards)")) {
1168                                                         printk(KERN_WARNING
1169                                                                "icn: memory at 0x%08lx in use.\n",
1170                                                                a & 0x0ffc000);
1171                                                         return -EINVAL;
1172                                                 }
1173                                                 release_mem_region(a & 0x0ffc000, 0x4000);
1174                                                 icn_stopallcards();
1175                                                 spin_lock_irqsave(&card->lock, flags);
1176                                                 if (dev.mvalid) {
1177                                                         iounmap(dev.shmem);
1178                                                         release_mem_region(dev.memaddr, 0x4000);
1179                                                 }
1180                                                 dev.mvalid = 0;
1181                                                 dev.memaddr = a & 0x0ffc000;
1182                                                 spin_unlock_irqrestore(&card->lock, flags);
1183                                                 printk(KERN_INFO
1184                                                        "icn: (%s) mmio set to 0x%08lx\n",
1185                                                        CID,
1186                                                        dev.memaddr);
1187                                         }
1188                                         break;
1189                                 case ICN_IOCTL_GETMMIO:
1190                                         return (long) dev.memaddr;
1191                                 case ICN_IOCTL_SETPORT:
1192                                         if (a == 0x300 || a == 0x310 || a == 0x320 || a == 0x330
1193                                             || a == 0x340 || a == 0x350 || a == 0x360 ||
1194                                             a == 0x308 || a == 0x318 || a == 0x328 || a == 0x338
1195                                             || a == 0x348 || a == 0x358 || a == 0x368) {
1196                                                 if (card->port != (unsigned short) a) {
1197                                                         if (!request_region((unsigned short) a, ICN_PORTLEN, "icn-isdn")) {
1198                                                                 printk(KERN_WARNING
1199                                                                        "icn: (%s) ports 0x%03x-0x%03x in use.\n",
1200                                                                        CID, (int) a, (int) a + ICN_PORTLEN);
1201                                                                 return -EINVAL;
1202                                                         }
1203                                                         release_region((unsigned short) a, ICN_PORTLEN);
1204                                                         icn_stopcard(card);
1205                                                         spin_lock_irqsave(&card->lock, flags);
1206                                                         if (card->rvalid)
1207                                                                 release_region(card->port, ICN_PORTLEN);
1208                                                         card->port = (unsigned short) a;
1209                                                         card->rvalid = 0;
1210                                                         if (card->doubleS0) {
1211                                                                 card->other->port = (unsigned short) a;
1212                                                                 card->other->rvalid = 0;
1213                                                         }
1214                                                         spin_unlock_irqrestore(&card->lock, flags);
1215                                                         printk(KERN_INFO
1216                                                                "icn: (%s) port set to 0x%03x\n",
1217                                                         CID, card->port);
1218                                                 }
1219                                         } else
1220                                                 return -EINVAL;
1221                                         break;
1222                                 case ICN_IOCTL_GETPORT:
1223                                         return (int) card->port;
1224                                 case ICN_IOCTL_GETDOUBLE:
1225                                         return (int) card->doubleS0;
1226                                 case ICN_IOCTL_DEBUGVAR:
1227                                         if (copy_to_user(arg,
1228                                                          &card,
1229                                                          sizeof(ulong)))
1230                                                 return -EFAULT;
1231                                         a += sizeof(ulong);
1232                                         {
1233                                                 ulong l = (ulong) & dev;
1234                                                 if (copy_to_user(arg,
1235                                                                  &l,
1236                                                                  sizeof(ulong)))
1237                                                         return -EFAULT;
1238                                         }
1239                                         return 0;
1240                                 case ICN_IOCTL_LOADBOOT:
1241                                         if (dev.firstload) {
1242                                                 icn_disable_cards();
1243                                                 dev.firstload = 0;
1244                                         }
1245                                         icn_stopcard(card);
1246                                         return (icn_loadboot(arg, card));
1247                                 case ICN_IOCTL_LOADPROTO:
1248                                         icn_stopcard(card);
1249                                         if ((i = (icn_loadproto(arg, card))))
1250                                                 return i;
1251                                         if (card->doubleS0)
1252                                                 i = icn_loadproto(arg + ICN_CODE_STAGE2, card->other);
1253                                         return i;
1254                                         break;
1255                                 case ICN_IOCTL_ADDCARD:
1256                                         if (!dev.firstload)
1257                                                 return -EBUSY;
1258                                         if (copy_from_user(&cdef,
1259                                                            arg,
1260                                                            sizeof(cdef)))
1261                                                 return -EFAULT;
1262                                         return (icn_addcard(cdef.port, cdef.id1, cdef.id2));
1263                                         break;
1264                                 case ICN_IOCTL_LEASEDCFG:
1265                                         if (a) {
1266                                                 if (!card->leased) {
1267                                                         card->leased = 1;
1268                                                         while (card->ptype == ISDN_PTYPE_UNKNOWN) {
1269                                                                 msleep_interruptible(ICN_BOOT_TIMEOUT1);
1270                                                         }
1271                                                         msleep_interruptible(ICN_BOOT_TIMEOUT1);
1272                                                         sprintf(cbuf, "00;FV2ON\n01;EAZ%c\n02;EAZ%c\n",
1273                                                                 (a & 1)?'1':'C', (a & 2)?'2':'C');
1274                                                         i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1275                                                         printk(KERN_INFO
1276                                                                "icn: (%s) Leased-line mode enabled\n",
1277                                                                CID);
1278                                                         cmd.command = ISDN_STAT_RUN;
1279                                                         cmd.driver = card->myid;
1280                                                         cmd.arg = 0;
1281                                                         card->interface.statcallb(&cmd);
1282                                                 }
1283                                         } else {
1284                                                 if (card->leased) {
1285                                                         card->leased = 0;
1286                                                         sprintf(cbuf, "00;FV2OFF\n");
1287                                                         i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1288                                                         printk(KERN_INFO
1289                                                                "icn: (%s) Leased-line mode disabled\n",
1290                                                                CID);
1291                                                         cmd.command = ISDN_STAT_RUN;
1292                                                         cmd.driver = card->myid;
1293                                                         cmd.arg = 0;
1294                                                         card->interface.statcallb(&cmd);
1295                                                 }
1296                                         }
1297                                         return 0;
1298                                 default:
1299                                         return -EINVAL;
1300                         }
1301                         break;
1302                 case ISDN_CMD_DIAL:
1303                         if (!card->flags & ICN_FLAGS_RUNNING)
1304                                 return -ENODEV;
1305                         if (card->leased)
1306                                 break;
1307                         if ((c->arg & 255) < ICN_BCH) {
1308                                 char *p;
1309                                 char dial[50];
1310                                 char dcode[4];
1311
1312                                 a = c->arg;
1313                                 p = c->parm.setup.phone;
1314                                 if (*p == 's' || *p == 'S') {
1315                                         /* Dial for SPV */
1316                                         p++;
1317                                         strcpy(dcode, "SCA");
1318                                 } else
1319                                         /* Normal Dial */
1320                                         strcpy(dcode, "CAL");
1321                                 strcpy(dial, p);
1322                                 sprintf(cbuf, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
1323                                         dcode, dial, c->parm.setup.si1,
1324                                 c->parm.setup.si2, c->parm.setup.eazmsn);
1325                                 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1326                         }
1327                         break;
1328                 case ISDN_CMD_ACCEPTD:
1329                         if (!card->flags & ICN_FLAGS_RUNNING)
1330                                 return -ENODEV;
1331                         if (c->arg < ICN_BCH) {
1332                                 a = c->arg + 1;
1333                                 if (card->fw_rev >= 300) {
1334                                         switch (card->l2_proto[a - 1]) {
1335                                                 case ISDN_PROTO_L2_X75I:
1336                                                         sprintf(cbuf, "%02d;BX75\n", (int) a);
1337                                                         break;
1338                                                 case ISDN_PROTO_L2_HDLC:
1339                                                         sprintf(cbuf, "%02d;BTRA\n", (int) a);
1340                                                         break;
1341                                         }
1342                                         i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1343                                 }
1344                                 sprintf(cbuf, "%02d;DCON_R\n", (int) a);
1345                                 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1346                         }
1347                         break;
1348                 case ISDN_CMD_ACCEPTB:
1349                         if (!card->flags & ICN_FLAGS_RUNNING)
1350                                 return -ENODEV;
1351                         if (c->arg < ICN_BCH) {
1352                                 a = c->arg + 1;
1353                                 if (card->fw_rev >= 300)
1354                                         switch (card->l2_proto[a - 1]) {
1355                                                 case ISDN_PROTO_L2_X75I:
1356                                                         sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
1357                                                         break;
1358                                                 case ISDN_PROTO_L2_HDLC:
1359                                                         sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
1360                                                         break;
1361                                 } else
1362                                         sprintf(cbuf, "%02d;BCON_R\n", (int) a);
1363                                 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1364                         }
1365                         break;
1366                 case ISDN_CMD_HANGUP:
1367                         if (!card->flags & ICN_FLAGS_RUNNING)
1368                                 return -ENODEV;
1369                         if (c->arg < ICN_BCH) {
1370                                 a = c->arg + 1;
1371                                 sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
1372                                 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1373                         }
1374                         break;
1375                 case ISDN_CMD_SETEAZ:
1376                         if (!card->flags & ICN_FLAGS_RUNNING)
1377                                 return -ENODEV;
1378                         if (card->leased)
1379                                 break;
1380                         if (c->arg < ICN_BCH) {
1381                                 a = c->arg + 1;
1382                                 if (card->ptype == ISDN_PTYPE_EURO) {
1383                                         sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
1384                                                 c->parm.num[0] ? "N" : "ALL", c->parm.num);
1385                                 } else
1386                                         sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
1387                                                 c->parm.num[0] ? (char *)(c->parm.num) : "0123456789");
1388                                 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1389                         }
1390                         break;
1391                 case ISDN_CMD_CLREAZ:
1392                         if (!card->flags & ICN_FLAGS_RUNNING)
1393                                 return -ENODEV;
1394                         if (card->leased)
1395                                 break;
1396                         if (c->arg < ICN_BCH) {
1397                                 a = c->arg + 1;
1398                                 if (card->ptype == ISDN_PTYPE_EURO)
1399                                         sprintf(cbuf, "%02d;MSNC\n", (int) a);
1400                                 else
1401                                         sprintf(cbuf, "%02d;EAZC\n", (int) a);
1402                                 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1403                         }
1404                         break;
1405                 case ISDN_CMD_SETL2:
1406                         if (!card->flags & ICN_FLAGS_RUNNING)
1407                                 return -ENODEV;
1408                         if ((c->arg & 255) < ICN_BCH) {
1409                                 a = c->arg;
1410                                 switch (a >> 8) {
1411                                         case ISDN_PROTO_L2_X75I:
1412                                                 sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
1413                                                 break;
1414                                         case ISDN_PROTO_L2_HDLC:
1415                                                 sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
1416                                                 break;
1417                                         default:
1418                                                 return -EINVAL;
1419                                 }
1420                                 i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
1421                                 card->l2_proto[a & 255] = (a >> 8);
1422                         }
1423                         break;
1424                 case ISDN_CMD_SETL3:
1425                         if (!card->flags & ICN_FLAGS_RUNNING)
1426                                 return -ENODEV;
1427                         return 0;
1428                 default:
1429                         return -EINVAL;
1430         }
1431         return 0;
1432 }
1433
1434 /*
1435  * Find card with given driverId
1436  */
1437 static inline icn_card *
1438 icn_findcard(int driverid)
1439 {
1440         icn_card *p = cards;
1441
1442         while (p) {
1443                 if (p->myid == driverid)
1444                         return p;
1445                 p = p->next;
1446         }
1447         return (icn_card *) 0;
1448 }
1449
1450 /*
1451  * Wrapper functions for interface to linklevel
1452  */
1453 static int
1454 if_command(isdn_ctrl * c)
1455 {
1456         icn_card *card = icn_findcard(c->driver);
1457
1458         if (card)
1459                 return (icn_command(c, card));
1460         printk(KERN_ERR
1461                "icn: if_command %d called with invalid driverId %d!\n",
1462                c->command, c->driver);
1463         return -ENODEV;
1464 }
1465
1466 static int
1467 if_writecmd(const u_char __user *buf, int len, int id, int channel)
1468 {
1469         icn_card *card = icn_findcard(id);
1470
1471         if (card) {
1472                 if (!card->flags & ICN_FLAGS_RUNNING)
1473                         return -ENODEV;
1474                 return (icn_writecmd(buf, len, 1, card));
1475         }
1476         printk(KERN_ERR
1477                "icn: if_writecmd called with invalid driverId!\n");
1478         return -ENODEV;
1479 }
1480
1481 static int
1482 if_readstatus(u_char __user *buf, int len, int id, int channel)
1483 {
1484         icn_card *card = icn_findcard(id);
1485
1486         if (card) {
1487                 if (!card->flags & ICN_FLAGS_RUNNING)
1488                         return -ENODEV;
1489                 return (icn_readstatus(buf, len, card));
1490         }
1491         printk(KERN_ERR
1492                "icn: if_readstatus called with invalid driverId!\n");
1493         return -ENODEV;
1494 }
1495
1496 static int
1497 if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
1498 {
1499         icn_card *card = icn_findcard(id);
1500
1501         if (card) {
1502                 if (!card->flags & ICN_FLAGS_RUNNING)
1503                         return -ENODEV;
1504                 return (icn_sendbuf(channel, ack, skb, card));
1505         }
1506         printk(KERN_ERR
1507                "icn: if_sendbuf called with invalid driverId!\n");
1508         return -ENODEV;
1509 }
1510
1511 /*
1512  * Allocate a new card-struct, initialize it
1513  * link it into cards-list and register it at linklevel.
1514  */
1515 static icn_card *
1516 icn_initcard(int port, char *id)
1517 {
1518         icn_card *card;
1519         int i;
1520
1521         if (!(card = (icn_card *) kmalloc(sizeof(icn_card), GFP_KERNEL))) {
1522                 printk(KERN_WARNING
1523                        "icn: (%s) Could not allocate card-struct.\n", id);
1524                 return (icn_card *) 0;
1525         }
1526         memset((char *) card, 0, sizeof(icn_card));
1527         spin_lock_init(&card->lock);
1528         card->port = port;
1529         card->interface.owner = THIS_MODULE;
1530         card->interface.hl_hdrlen = 1;
1531         card->interface.channels = ICN_BCH;
1532         card->interface.maxbufsize = 4000;
1533         card->interface.command = if_command;
1534         card->interface.writebuf_skb = if_sendbuf;
1535         card->interface.writecmd = if_writecmd;
1536         card->interface.readstat = if_readstatus;
1537         card->interface.features = ISDN_FEATURE_L2_X75I |
1538             ISDN_FEATURE_L2_HDLC |
1539             ISDN_FEATURE_L3_TRANS |
1540             ISDN_FEATURE_P_UNKNOWN;
1541         card->ptype = ISDN_PTYPE_UNKNOWN;
1542         strlcpy(card->interface.id, id, sizeof(card->interface.id));
1543         card->msg_buf_write = card->msg_buf;
1544         card->msg_buf_read = card->msg_buf;
1545         card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
1546         for (i = 0; i < ICN_BCH; i++) {
1547                 card->l2_proto[i] = ISDN_PROTO_L2_X75I;
1548                 skb_queue_head_init(&card->spqueue[i]);
1549         }
1550         card->next = cards;
1551         cards = card;
1552         if (!register_isdn(&card->interface)) {
1553                 cards = cards->next;
1554                 printk(KERN_WARNING
1555                        "icn: Unable to register %s\n", id);
1556                 kfree(card);
1557                 return (icn_card *) 0;
1558         }
1559         card->myid = card->interface.channels;
1560         sprintf(card->regname, "icn-isdn (%s)", card->interface.id);
1561         return card;
1562 }
1563
1564 static int
1565 icn_addcard(int port, char *id1, char *id2)
1566 {
1567         icn_card *card;
1568         icn_card *card2;
1569
1570         if (!(card = icn_initcard(port, id1))) {
1571                 return -EIO;
1572         }
1573         if (!strlen(id2)) {
1574                 printk(KERN_INFO
1575                        "icn: (%s) ICN-2B, port 0x%x added\n",
1576                        card->interface.id, port);
1577                 return 0;
1578         }
1579         if (!(card2 = icn_initcard(port, id2))) {
1580                 printk(KERN_INFO
1581                        "icn: (%s) half ICN-4B, port 0x%x added\n",
1582                        card2->interface.id, port);
1583                 return 0;
1584         }
1585         card->doubleS0 = 1;
1586         card->secondhalf = 0;
1587         card->other = card2;
1588         card2->doubleS0 = 1;
1589         card2->secondhalf = 1;
1590         card2->other = card;
1591         printk(KERN_INFO
1592                "icn: (%s and %s) ICN-4B, port 0x%x added\n",
1593                card->interface.id, card2->interface.id, port);
1594         return 0;
1595 }
1596
1597 #ifndef MODULE
1598 static int __init
1599 icn_setup(char *line)
1600 {
1601         char *p, *str;
1602         int     ints[3];
1603         static char sid[20];
1604         static char sid2[20];
1605
1606         str = get_options(line, 2, ints);
1607         if (ints[0])
1608                 portbase = ints[1];
1609         if (ints[0] > 1)
1610                 membase = (unsigned long)ints[2];
1611         if (str && *str) {
1612                 strcpy(sid, str);
1613                 icn_id = sid;
1614                 if ((p = strchr(sid, ','))) {
1615                         *p++ = 0;
1616                         strcpy(sid2, p);
1617                         icn_id2 = sid2;
1618                 }
1619         }
1620         return(1);
1621 }
1622 __setup("icn=", icn_setup);
1623 #endif /* MODULE */
1624
1625 static int __init icn_init(void)
1626 {
1627         char *p;
1628         char rev[10];
1629
1630         memset(&dev, 0, sizeof(icn_dev));
1631         dev.memaddr = (membase & 0x0ffc000);
1632         dev.channel = -1;
1633         dev.mcard = NULL;
1634         dev.firstload = 1;
1635         spin_lock_init(&dev.devlock);
1636
1637         if ((p = strchr(revision, ':'))) {
1638                 strcpy(rev, p + 1);
1639                 p = strchr(rev, '$');
1640                 *p = 0;
1641         } else
1642                 strcpy(rev, " ??? ");
1643         printk(KERN_NOTICE "ICN-ISDN-driver Rev%smem=0x%08lx\n", rev,
1644                dev.memaddr);
1645         return (icn_addcard(portbase, icn_id, icn_id2));
1646 }
1647
1648 static void __exit icn_exit(void)
1649 {
1650         isdn_ctrl cmd;
1651         icn_card *card = cards;
1652         icn_card *last, *tmpcard;
1653         int i;
1654         unsigned long flags;
1655
1656         icn_stopallcards();
1657         while (card) {
1658                 cmd.command = ISDN_STAT_UNLOAD;
1659                 cmd.driver = card->myid;
1660                 card->interface.statcallb(&cmd);
1661                 spin_lock_irqsave(&card->lock, flags);
1662                 if (card->rvalid) {
1663                         OUTB_P(0, ICN_RUN);     /* Reset Controller     */
1664                         OUTB_P(0, ICN_MAPRAM);  /* Disable RAM          */
1665                         if (card->secondhalf || (!card->doubleS0)) {
1666                                 release_region(card->port, ICN_PORTLEN);
1667                                 card->rvalid = 0;
1668                         }
1669                         for (i = 0; i < ICN_BCH; i++)
1670                                 icn_free_queue(card, i);
1671                 }
1672                 tmpcard = card->next;
1673                 spin_unlock_irqrestore(&card->lock, flags);
1674                 card = tmpcard;
1675         }
1676         card = cards;
1677         cards = NULL;
1678         while (card) {
1679                 last = card;
1680                 card = card->next;
1681                 kfree(last);
1682         }
1683         if (dev.mvalid) {
1684                 iounmap(dev.shmem);
1685                 release_mem_region(dev.memaddr, 0x4000);
1686         }
1687         printk(KERN_NOTICE "ICN-ISDN-driver unloaded\n");
1688 }
1689
1690 module_init(icn_init);
1691 module_exit(icn_exit);