13 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
18 static int portbase = ICN_BASEADDR;
19 static unsigned long membase = ICN_MEMADDR;
20 static char *icn_id =
"\0";
21 static char *icn_id2 =
"\0";
48 static int icn_addcard(
int,
char *,
char *);
65 if ((skb = card->xskb[channel])) {
79 icn_shiftout(
unsigned short port,
88 for (s = firstbit, c = bitcount; c > 0; s--, c--)
89 OUTB_P((
u_char) ((val >> s) & 1) ? 0xff : 0, port);
96 icn_disable_ram(icn_card *card)
98 OUTB_P(0, ICN_MAPRAM);
105 icn_enable_ram(icn_card *card)
107 OUTB_P(0xff, ICN_MAPRAM);
116 icn_map_channel(icn_card *card,
int channel)
121 if ((channel ==
dev.channel) && (card ==
dev.mcard))
124 icn_disable_ram(
dev.mcard);
125 icn_shiftout(ICN_BANK, chan2bank[channel], 3, 4);
126 icn_enable_ram(card);
142 icn_lock_channel(icn_card *card,
int channel)
149 if ((
dev.channel == channel) && (card ==
dev.mcard)) {
170 __icn_release_channel(
void)
175 if (
dev.chanlock > 0)
183 icn_release_channel(
void)
188 __icn_release_channel();
189 spin_unlock_irqrestore(&
dev.devlock, flags);
197 icn_trymaplock_channel(icn_card *card,
int channel)
206 if ((!
dev.chanlock) ||
207 ((
dev.channel == channel) && (
dev.mcard == card))) {
209 icn_map_channel(card, channel);
210 spin_unlock_irqrestore(&
dev.devlock, flags);
216 spin_unlock_irqrestore(&
dev.devlock, flags);
228 icn_maprelease_channel(icn_card *card,
int channel)
236 if (
dev.chanlock > 0)
239 icn_map_channel(card, channel);
240 spin_unlock_irqrestore(&
dev.devlock, flags);
249 icn_pollbchan_receive(
int channel, icn_card *card)
251 int mch = channel + ((card->secondhalf) ? 2 : 0);
256 if (icn_trymaplock_channel(card, mch)) {
258 cnt =
readb(&rbuf_l);
259 if ((card->rcvidx[channel] + cnt) > 4000) {
261 "icn: (%s) bogus packet on ch%d, dropping.\n",
270 eflag =
readb(&rbuf_f);
273 icn_maprelease_channel(card, mch & 2);
275 if ((cnt = card->rcvidx[channel])) {
276 if (!(skb = dev_alloc_skb(cnt))) {
282 card->interface.rcvcallb_skb(card->myid, channel, skb);
285 if (!icn_trymaplock_channel(card, mch))
288 icn_maprelease_channel(card, mch & 2);
300 icn_pollbchan_send(
int channel, icn_card *card)
302 int mch = channel + ((card->secondhalf) ? 2 : 0);
308 if (!(card->sndcount[channel] || card->xskb[channel] ||
309 !skb_queue_empty(&card->spqueue[channel])))
311 if (icn_trymaplock_channel(card, mch)) {
313 (card->sndcount[channel] ||
314 !skb_queue_empty(&card->spqueue[channel]) ||
315 card->xskb[channel])) {
317 if (card->xmit_lock[channel]) {
318 spin_unlock_irqrestore(&card->lock, flags);
322 spin_unlock_irqrestore(&card->lock, flags);
338 if (skb->
len > ICN_FRAGSIZE) {
349 icn_maprelease_channel(card, mch & 2);
353 if (card->xskb[channel])
356 spin_unlock_irqrestore(&card->lock, flags);
358 if (card->xlen[channel]) {
363 card->interface.statcallb(&cmd);
368 spin_unlock_irqrestore(&card->lock, flags);
370 if (!icn_trymaplock_channel(card, mch))
373 icn_maprelease_channel(card, mch & 2);
383 icn_pollbchan(
unsigned long data)
385 icn_card *card = (icn_card *) data;
388 if (card->flags & ICN_FLAGS_B1ACTIVE) {
389 icn_pollbchan_receive(0, card);
390 icn_pollbchan_send(0, card);
392 if (card->flags & ICN_FLAGS_B2ACTIVE) {
393 icn_pollbchan_receive(1, card);
394 icn_pollbchan_send(1, card);
396 if (card->flags & (ICN_FLAGS_B1ACTIVE | ICN_FLAGS_B2ACTIVE)) {
399 mod_timer(&card->rb_timer, jiffies + ICN_TIMER_BCREAD);
400 card->flags |= ICN_FLAGS_RBTIMER;
401 spin_unlock_irqrestore(&card->lock, flags);
403 card->flags &= ~ICN_FLAGS_RBTIMER;
430 {
"E_L1: ACTIVATION FAILED",
448 icn_parse_status(
u_char *
status,
int channel, icn_card *card)
470 icn_free_queue(card, channel);
474 ((channel) ? ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE)) {
479 ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE);
481 memset(&ncmd, 0,
sizeof(ncmd));
486 spin_unlock_irqrestore(&card->lock, flags);
487 card->interface.statcallb(&cmd);
489 spin_unlock_irqrestore(&card->lock, flags);
493 icn_free_queue(card, channel);
495 ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE;
496 spin_unlock_irqrestore(&card->lock, flags);
501 ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE);
502 icn_free_queue(card, channel);
504 spin_unlock_irqrestore(&card->lock, flags);
508 char *
t = status + 6;
553 status + 2, *status, *(status + 1));
559 card->flags &= ~ICN_FLAGS_B1ACTIVE;
560 icn_free_queue(card, 0);
562 spin_unlock_irqrestore(&card->lock, flags);
565 card->interface.statcallb(&cmd);
569 card->interface.statcallb(&cmd);
572 card->flags &= ~ICN_FLAGS_B2ACTIVE;
573 icn_free_queue(card, 1);
575 spin_unlock_irqrestore(&card->lock, flags);
578 card->interface.statcallb(&cmd);
584 card->interface.statcallb(&cmd);
589 icn_putmsg(icn_card *card,
unsigned char c)
594 *card->msg_buf_write++ = (c == 0xff) ?
'\n' : c;
595 if (card->msg_buf_write == card->msg_buf_read) {
596 if (++card->msg_buf_read > card->msg_buf_end)
597 card->msg_buf_read = card->msg_buf;
599 if (card->msg_buf_write > card->msg_buf_end)
600 card->msg_buf_write = card->msg_buf;
601 spin_unlock_irqrestore(&card->lock, flags);
605 icn_polldchan(
unsigned long data)
607 icn_card *card = (icn_card *) data;
608 int mch = card->secondhalf ? 2 : 0;
618 if (icn_trymaplock_channel(card, mch)) {
620 for (left = avail, i =
readb(&msg_o); left > 0; i++, left--) {
621 c =
readb(&
dev.shmem->comm_buffers.iopc_buf[i & 0xff]);
624 card->imsg[card->iptr] = 0;
626 if (card->imsg[0] ==
'0' && card->imsg[1] >=
'0' &&
627 card->imsg[1] <=
'2' && card->imsg[2] ==
';') {
628 ch = (card->imsg[1] -
'0') - 1;
630 icn_parse_status(p, ch, card);
638 if (!
strncmp(p + 7,
"TC", 2)) {
642 "icn: (%s) 1TR6-Protocol loaded and running\n", CID);
644 if (!
strncmp(p + 7,
"EC", 2)) {
648 "icn: (%s) Euro-Protocol loaded and running\n", CID);
650 p =
strstr(card->imsg,
"BRV") + 3;
652 if (*p >=
'0' && *p <=
'9')
665 card->imsg[card->iptr] =
c;
671 icn_release_channel();
677 card->interface.statcallb(&cmd);
680 if (card->flags & (ICN_FLAGS_B1ACTIVE | ICN_FLAGS_B2ACTIVE))
681 if (!(card->flags & ICN_FLAGS_RBTIMER)) {
683 card->flags |= ICN_FLAGS_RBTIMER;
685 card->rb_timer.function = icn_pollbchan;
686 card->rb_timer.data = (
unsigned long) card;
687 card->rb_timer.expires =
jiffies + ICN_TIMER_BCREAD;
691 mod_timer(&card->st_timer, jiffies + ICN_TIMER_DCREAD);
692 spin_unlock_irqrestore(&card->lock, flags);
705 icn_sendbuf(
int channel,
int ack,
struct sk_buff *skb, icn_card *card)
713 "icn: Send packet too large\n");
717 if (!(card->flags & (channel) ? ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE))
719 if (card->sndcount[channel] > ICN_MAX_SQUEUE)
721 #warning TODO test headroom or use skb->nb to flag ACK
734 spin_unlock_irqrestore(&card->lock, flags);
747 icn_check_loader(
int cardnumber)
755 if (
readb(&
dev.shmem->data_control.scns) ||
756 readb(&
dev.shmem->data_control.scnr)) {
759 "icn: Boot-Loader %d timed out.\n",
761 icn_release_channel();
772 icn_release_channel();
788 #define SLEEP(sec) { \
790 printk(KERN_DEBUG "SLEEP(%d)\n", slsec); \
792 msleep_interruptible(1000); \
822 "icn: (%s) ports 0x%03x-0x%03x in use.\n",
825 card->port + ICN_PORTLEN);
831 card->other->rvalid = 1;
836 "icn: memory at 0x%08lx in use.\n",
dev.memaddr);
844 OUTB_P(0, ICN_MAPRAM);
845 icn_shiftout(ICN_CFG, 0x0f, 3, 4);
846 icn_shiftout(ICN_CFG,
dev.memaddr, 23, 10);
855 icn_map_channel(card, 0);
856 icn_lock_channel(card, 0);
857 spin_unlock_irqrestore(&
dev.devlock, flags);
863 if (card->doubleS0) {
869 __icn_release_channel();
870 icn_map_channel(card, 2);
871 icn_lock_channel(card, 2);
872 spin_unlock_irqrestore(&
dev.devlock, flags);
880 OUTB_P(0xff, ICN_RUN);
881 if ((ret = icn_check_loader(card->doubleS0 ? 2 : 1))) {
884 if (!card->doubleS0) {
893 icn_map_channel(card, 0);
894 icn_lock_channel(card, 0);
895 spin_unlock_irqrestore(&
dev.devlock, flags);
897 ret = (icn_check_loader(1));
910 uint left = ICN_CODE_STAGE2;
922 if (card->secondhalf) {
923 icn_map_channel(card, 2);
924 icn_lock_channel(card, 2);
926 icn_map_channel(card, 0);
927 icn_lock_channel(card, 0);
929 spin_unlock_irqrestore(&
dev.devlock, flags);
936 icn_maprelease_channel(card, 0);
949 icn_maprelease_channel(card, 0);
964 "icn: (%s) Protocol timed out.\n",
969 icn_maprelease_channel(card, 0);
977 if ((card->secondhalf) || (!card->doubleS0)) {
984 card->st_timer.expires =
jiffies + ICN_TIMER_DCREAD;
985 card->st_timer.function = icn_polldchan;
986 card->st_timer.data = (
unsigned long) card;
988 card->flags |= ICN_FLAGS_RUNNING;
989 if (card->doubleS0) {
991 card->other->st_timer.expires =
jiffies + ICN_TIMER_DCREAD;
992 card->other->st_timer.function = icn_polldchan;
993 card->other->st_timer.data = (
unsigned long) card->other;
995 card->other->flags |= ICN_FLAGS_RUNNING;
997 spin_unlock_irqrestore(&card->lock, flags);
999 icn_maprelease_channel(card, 0);
1007 icn_readstatus(
u_char __user *
buf,
int len, icn_card *card)
1012 for (p = buf, count = 0; count <
len; p++, count++) {
1013 if (card->msg_buf_read == card->msg_buf_write)
1015 if (
put_user(*card->msg_buf_read++, p))
1017 if (card->msg_buf_read > card->msg_buf_end)
1018 card->msg_buf_read = card->msg_buf;
1025 icn_writecmd(
const u_char *buf,
int len,
int user, icn_card *card)
1027 int mch = card->secondhalf ? 2 : 0;
1034 unsigned long flags;
1035 int lastmap_channel;
1036 struct icn_card *lastmap_card;
1054 lastmap_card =
dev.mcard;
1055 lastmap_channel =
dev.channel;
1056 icn_map_channel(card, mch);
1058 icn_putmsg(card,
'>');
1059 for (p = msg, pp =
readb(&cmd_i), i = count; i > 0; i--, p++, pp
1061 writeb((*p ==
'\n') ? 0xff : *p,
1062 &
dev.shmem->comm_buffers.pcio_buf[pp & 0xff]);
1065 icn_putmsg(card, *p);
1066 if ((*p ==
'\n') && (i > 1)) {
1067 icn_putmsg(card,
'>');
1074 icn_map_channel(lastmap_card, lastmap_channel);
1075 spin_unlock_irqrestore(&
dev.devlock, flags);
1088 card->interface.statcallb(&cmd);
1096 icn_stopcard(icn_card *card)
1098 unsigned long flags;
1102 if (card->flags & ICN_FLAGS_RUNNING) {
1103 card->flags &= ~ICN_FLAGS_RUNNING;
1106 spin_unlock_irqrestore(&card->lock, flags);
1109 card->interface.statcallb(&cmd);
1111 icn_stopcard(card->other);
1113 spin_unlock_irqrestore(&card->lock, flags);
1117 icn_stopallcards(
void)
1119 icn_card *p =
cards;
1132 icn_disable_cards(
void)
1134 icn_card *card =
cards;
1139 "icn: (%s) ports 0x%03x-0x%03x in use.\n",
1142 card->port + ICN_PORTLEN);
1145 OUTB_P(0, ICN_MAPRAM);
1153 icn_command(
isdn_ctrl *c, icn_card *card)
1169 if (
dev.memaddr != (a & 0x0ffc000)) {
1172 "icn: memory at 0x%08lx in use.\n",
1184 dev.memaddr = a & 0x0ffc000;
1185 spin_unlock_irqrestore(&card->lock, flags);
1187 "icn: (%s) mmio set to 0x%08lx\n",
1193 return (
long)
dev.memaddr;
1195 if (a == 0x300 || a == 0x310 || a == 0x320 || a == 0x330
1196 || a == 0x340 || a == 0x350 || a == 0x360 ||
1197 a == 0x308 || a == 0x318 || a == 0x328 || a == 0x338
1198 || a == 0x348 || a == 0x358 || a == 0x368) {
1199 if (card->port != (
unsigned short) a) {
1200 if (!
request_region((
unsigned short) a, ICN_PORTLEN,
"icn-isdn")) {
1202 "icn: (%s) ports 0x%03x-0x%03x in use.\n",
1203 CID, (
int) a, (
int) a + ICN_PORTLEN);
1211 card->port = (
unsigned short) a;
1213 if (card->doubleS0) {
1214 card->other->port = (
unsigned short) a;
1215 card->other->rvalid = 0;
1217 spin_unlock_irqrestore(&card->lock, flags);
1219 "icn: (%s) port set to 0x%03x\n",
1226 return (
int) card->port;
1228 return (
int) card->doubleS0;
1244 if (
dev.firstload) {
1245 icn_disable_cards();
1249 return (icn_loadboot(arg, card));
1252 if ((i = (icn_loadproto(arg, card))))
1255 i = icn_loadproto(arg + ICN_CODE_STAGE2, card->other);
1265 return (icn_addcard(cdef.
port, cdef.
id1, cdef.
id2));
1269 if (!card->leased) {
1275 sprintf(cbuf,
"00;FV2ON\n01;EAZ%c\n02;EAZ%c\n",
1276 (a & 1) ?
'1' :
'C', (a & 2) ?
'2' :
'C');
1277 i = icn_writecmd(cbuf,
strlen(cbuf), 0, card);
1279 "icn: (%s) Leased-line mode enabled\n",
1284 card->interface.statcallb(&cmd);
1290 i = icn_writecmd(cbuf,
strlen(cbuf), 0, card);
1292 "icn: (%s) Leased-line mode disabled\n",
1297 card->interface.statcallb(&cmd);
1306 if (!(card->flags & ICN_FLAGS_RUNNING))
1310 if ((c->
arg & 255) < ICN_BCH) {
1317 if (*p ==
's' || *p ==
'S') {
1325 sprintf(cbuf,
"%02d;D%s_R%s,%02d,%02d,%s\n", (
int) (a + 1),
1328 i = icn_writecmd(cbuf,
strlen(cbuf), 0, card);
1332 if (!(card->flags & ICN_FLAGS_RUNNING))
1334 if (c->
arg < ICN_BCH) {
1336 if (card->fw_rev >= 300) {
1337 switch (card->l2_proto[a - 1]) {
1339 sprintf(cbuf,
"%02d;BX75\n", (
int) a);
1342 sprintf(cbuf,
"%02d;BTRA\n", (
int) a);
1345 i = icn_writecmd(cbuf,
strlen(cbuf), 0, card);
1347 sprintf(cbuf,
"%02d;DCON_R\n", (
int) a);
1348 i = icn_writecmd(cbuf,
strlen(cbuf), 0, card);
1352 if (!(card->flags & ICN_FLAGS_RUNNING))
1354 if (c->
arg < ICN_BCH) {
1356 if (card->fw_rev >= 300)
1357 switch (card->l2_proto[a - 1]) {
1359 sprintf(cbuf,
"%02d;BCON_R,BX75\n", (
int) a);
1362 sprintf(cbuf,
"%02d;BCON_R,BTRA\n", (
int) a);
1365 sprintf(cbuf,
"%02d;BCON_R\n", (
int) a);
1366 i = icn_writecmd(cbuf,
strlen(cbuf), 0, card);
1370 if (!(card->flags & ICN_FLAGS_RUNNING))
1372 if (c->
arg < ICN_BCH) {
1374 sprintf(cbuf,
"%02d;BDIS_R\n%02d;DDIS_R\n", (
int) a, (
int) a);
1375 i = icn_writecmd(cbuf,
strlen(cbuf), 0, card);
1379 if (!(card->flags & ICN_FLAGS_RUNNING))
1383 if (c->
arg < ICN_BCH) {
1386 sprintf(cbuf,
"%02d;MS%s%s\n", (
int) a,
1389 sprintf(cbuf,
"%02d;EAZ%s\n", (
int) a,
1391 i = icn_writecmd(cbuf,
strlen(cbuf), 0, card);
1395 if (!(card->flags & ICN_FLAGS_RUNNING))
1399 if (c->
arg < ICN_BCH) {
1402 sprintf(cbuf,
"%02d;MSNC\n", (
int) a);
1404 sprintf(cbuf,
"%02d;EAZC\n", (
int) a);
1405 i = icn_writecmd(cbuf,
strlen(cbuf), 0, card);
1409 if (!(card->flags & ICN_FLAGS_RUNNING))
1411 if ((c->
arg & 255) < ICN_BCH) {
1415 sprintf(cbuf,
"%02d;BX75\n", (
int) (a & 255) + 1);
1418 sprintf(cbuf,
"%02d;BTRA\n", (
int) (a & 255) + 1);
1423 i = icn_writecmd(cbuf,
strlen(cbuf), 0, card);
1424 card->l2_proto[a & 255] = (a >> 8);
1428 if (!(card->flags & ICN_FLAGS_RUNNING))
1440 static inline icn_card *
1441 icn_findcard(
int driverid)
1443 icn_card *p =
cards;
1446 if (p->myid == driverid)
1450 return (icn_card *) 0;
1459 icn_card *card = icn_findcard(c->
driver);
1462 return (icn_command(c, card));
1464 "icn: if_command %d called with invalid driverId %d!\n",
1470 if_writecmd(
const u_char __user *buf,
int len,
int id,
int channel)
1472 icn_card *card = icn_findcard(
id);
1475 if (!(card->flags & ICN_FLAGS_RUNNING))
1477 return (icn_writecmd(buf, len, 1, card));
1480 "icn: if_writecmd called with invalid driverId!\n");
1485 if_readstatus(
u_char __user *buf,
int len,
int id,
int channel)
1487 icn_card *card = icn_findcard(
id);
1490 if (!(card->flags & ICN_FLAGS_RUNNING))
1492 return (icn_readstatus(buf, len, card));
1495 "icn: if_readstatus called with invalid driverId!\n");
1500 if_sendbuf(
int id,
int channel,
int ack,
struct sk_buff *skb)
1502 icn_card *card = icn_findcard(
id);
1505 if (!(card->flags & ICN_FLAGS_RUNNING))
1507 return (icn_sendbuf(channel, ack, skb, card));
1510 "icn: if_sendbuf called with invalid driverId!\n");
1519 icn_initcard(
int port,
char *
id)
1524 if (!(card = kzalloc(
sizeof(icn_card),
GFP_KERNEL))) {
1526 "icn: (%s) Could not allocate card-struct.\n",
id);
1527 return (icn_card *) 0;
1532 card->interface.hl_hdrlen = 1;
1533 card->interface.channels = ICN_BCH;
1534 card->interface.maxbufsize = 4000;
1535 card->interface.command = if_command;
1536 card->interface.writebuf_skb = if_sendbuf;
1537 card->interface.writecmd = if_writecmd;
1538 card->interface.readstat = if_readstatus;
1544 strlcpy(card->interface.id,
id,
sizeof(card->interface.id));
1545 card->msg_buf_write = card->msg_buf;
1546 card->msg_buf_read = card->msg_buf;
1547 card->msg_buf_end = &card->msg_buf[
sizeof(card->msg_buf) - 1];
1548 for (i = 0; i < ICN_BCH; i++) {
1550 skb_queue_head_init(&card->spqueue[i]);
1557 "icn: Unable to register %s\n",
id);
1559 return (icn_card *) 0;
1561 card->myid = card->interface.channels;
1562 sprintf(card->regname,
"icn-isdn (%s)", card->interface.id);
1567 icn_addcard(
int port,
char *
id1,
char *
id2)
1572 if (!(card = icn_initcard(port, id1))) {
1577 "icn: (%s) ICN-2B, port 0x%x added\n",
1578 card->interface.id, port);
1581 if (!(card2 = icn_initcard(port, id2))) {
1583 "icn: (%s) half ICN-4B, port 0x%x added\n",
1584 card2->interface.id, port);
1588 card->secondhalf = 0;
1589 card->other = card2;
1590 card2->doubleS0 = 1;
1591 card2->secondhalf = 1;
1592 card2->other =
card;
1594 "icn: (%s and %s) ICN-4B, port 0x%x added\n",
1595 card->interface.id, card2->interface.id, port);
1601 icn_setup(
char *
line)
1605 static char sid[20];
1606 static char sid2[20];
1612 membase = (
unsigned long)ints[2];
1616 if ((p =
strchr(sid,
','))) {
1627 static int __init icn_init(
void)
1633 dev.memaddr = (membase & 0x0ffc000);
1649 return (icn_addcard(portbase, icn_id, icn_id2));
1652 static void __exit icn_exit(
void)
1655 icn_card *card =
cards;
1656 icn_card *
last, *tmpcard;
1658 unsigned long flags;
1664 card->interface.statcallb(&cmd);
1668 OUTB_P(0, ICN_MAPRAM);
1669 if (card->secondhalf || (!card->doubleS0)) {
1673 for (i = 0; i < ICN_BCH; i++)
1674 icn_free_queue(card, i);
1676 tmpcard = card->next;
1677 spin_unlock_irqrestore(&card->lock, flags);