1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
  
     | 
    
      /*
 * zaphfc.c - Dahdi driver for HFC-S PCI A based ISDN BRI cards
 *
 * Dahdi rewrite in hardhdlc mode
 * Jose A. Deniz <odicha@hotmail.com>
 *
 * Copyright (C) 2009, Jose A. Deniz
 * Copyright (C) 2006, headiisue GmbH; Jens Wilke
 * Copyright (C) 2004 Daniele Orlandi
 * Copyright (C) 2002, 2003, 2004, Junghanns.NET GmbH
 *
 * Jens Wilke <jw_vzaphfc@headissue.com>
 *
 * Original author of this code is
 * Daniele "Vihai" Orlandi <daniele@orlandi.com>
 *
 * Major rewrite of the driver made by
 * Klaus-Peter Junghanns <kpj@junghanns.net>
 *
 * This program is free software and may be modified and
 * distributed under the terms of the GNU General Public License.
 *
 * Please read the README file for important infos.
 */
#include <linux/spinlock.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/proc_fs.h>
#include <linux/if_arp.h>
#include <dahdi/kernel.h>
#include "zaphfc.h"
#include "fifo.h"
#if CONFIG_PCI
#define DAHDI_B1 0
#define DAHDI_B2 1
#define DAHDI_D 2
#define D 0
#define B1 1
#define B2 2
/*
 * Mode Te for all
 */
static int modes;
static int nt_modes[hfc_MAX_BOARDS];
static int nt_modes_count;
static int force_l1_up;
#ifdef DEBUG
int debug_level;
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
#define	SET_PROC_DIRENTRY_OWNER(p)  do { (p)->owner = THIS_MODULE; } while (0);
#else
#define	SET_PROC_DIRENTRY_OWNER(p)	do { } while (0);
#endif
static struct pci_device_id hfc_pci_ids[] = {
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_2BD0,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B000,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B006,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B007,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B008,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B009,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00A,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00B,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00C,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B100,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_ABOCOM, PCI_DEVICE_ID_ABOCOM_2BD1,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_ASUSTEK, PCI_DEVICE_ID_ASUSTEK_0675,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_BERKOM, PCI_DEVICE_ID_BERKOM_T_CONCEPT,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_BERKOM, PCI_DEVICE_ID_BERKOM_A1T,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_ANIGMA, PCI_DEVICE_ID_ANIGMA_MC145575,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_ZOLTRIX, PCI_DEVICE_ID_ZOLTRIX_2BD0,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_E,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_E,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_A,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_A,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_SITECOM, PCI_DEVICE_ID_SITECOM_3069,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{0,}
};
MODULE_DEVICE_TABLE(pci, hfc_pci_ids);
static int __devinit hfc_probe(struct pci_dev *dev
			, const struct pci_device_id *ent);
static void __devexit hfc_remove(struct pci_dev *dev);
static struct pci_driver hfc_driver = {
	.name     = hfc_DRIVER_NAME,
	.id_table = hfc_pci_ids,
	.probe    = hfc_probe,
	.remove   = hfc_remove,
};
/******************************************
 * HW routines
 ******************************************/
static void hfc_softreset(struct hfc_card *card)
{
	printk(KERN_INFO hfc_DRIVER_PREFIX
		"card %d: "
		"resetting\n",
		card->cardnum);
/*
 * Softreset procedure. Put it on, wait and off again
 */
	hfc_outb(card, hfc_CIRM, hfc_CIRM_RESET);
	udelay(6);
	hfc_outb(card, hfc_CIRM, 0);
	set_current_state(TASK_UNINTERRUPTIBLE);
	schedule_timeout((hfc_RESET_DELAY * HZ) / 1000);
}
static void hfc_resetCard(struct hfc_card *card)
{
	card->regs.m1 = 0;
	hfc_outb(card, hfc_INT_M1, card->regs.m1);
	card->regs.m2 = 0;
	hfc_outb(card, hfc_INT_M2, card->regs.m2);
	hfc_softreset(card);
	card->regs.trm = 0;
	hfc_outb(card, hfc_TRM, card->regs.trm);
	/*
	 * Select the non-capacitive line mode for the S/T interface
	 */
	card->regs.sctrl = hfc_SCTRL_NONE_CAP;
	if (card->nt_mode) {
		/*
		 * ST-Bit delay for NT-Mode
		 */
		hfc_outb(card, hfc_CLKDEL, hfc_CLKDEL_NT);
		card->regs.sctrl |= hfc_SCTRL_MODE_NT;
	} else {
		/*
		 * ST-Bit delay for TE-Mode
		 */
		hfc_outb(card, hfc_CLKDEL, hfc_CLKDEL_TE);
		card->regs.sctrl |= hfc_SCTRL_MODE_TE;
	}
	hfc_outb(card, hfc_SCTRL, card->regs.sctrl);
	/*
	 * S/T Auto awake
	 */
	card->regs.sctrl_e = hfc_SCTRL_E_AUTO_AWAKE;
	hfc_outb(card, hfc_SCTRL_E, card->regs.sctrl_e);
	/*
	 * No B-channel enabled at startup
	 */
	card->regs.sctrl_r = 0;
	hfc_outb(card, hfc_SCTRL_R, card->regs.sctrl_r);
	/*
	 * HFC Master Mode
	 */
	hfc_outb(card, hfc_MST_MODE, hfc_MST_MODE_MASTER);
	/*
	 * Connect internal blocks
	 */
	card->regs.connect =
		hfc_CONNECT_B1_HFC_from_ST |
		hfc_CONNECT_B1_ST_from_HFC |
		hfc_CONNECT_B1_GCI_from_HFC |
		hfc_CONNECT_B2_HFC_from_ST |
		hfc_CONNECT_B2_ST_from_HFC |
		hfc_CONNECT_B2_GCI_from_HFC;
	hfc_outb(card, hfc_CONNECT, card->regs.connect);
	/*
	 * All bchans are HDLC by default, not useful, actually
	 * since mode is set during open()
	 */
	hfc_outb(card, hfc_CTMT, 0);
	/*
	 * bit order
	 */
	hfc_outb(card, hfc_CIRM, 0);
	/*
	 * Enable D-rx FIFO. At least one FIFO must be enabled (by specs)
	 */
	card->regs.fifo_en = hfc_FIFOEN_DRX;
	hfc_outb(card, hfc_FIFO_EN, card->regs.fifo_en);
	card->late_irqs = 0;
	/*
	 * Clear already pending ints
	 */
	hfc_inb(card, hfc_INT_S1);
	hfc_inb(card, hfc_INT_S2);
	/*
	 * Enable IRQ output
	 */
	card->regs.m1 = hfc_INTS_DREC | hfc_INTS_L1STATE | hfc_INTS_TIMER;
	hfc_outb(card, hfc_INT_M1, card->regs.m1);
	card->regs.m2 = hfc_M2_IRQ_ENABLE;
	hfc_outb(card, hfc_INT_M2, card->regs.m2);
	/*
	 * Unlocks the states machine
	 */
	hfc_outb(card, hfc_STATES, 0);
	/*
	 * There's no need to explicitly activate L1 now.
	 * Activation is managed inside the interrupt routine.
	 */
}
static void hfc_update_fifo_state(struct hfc_card *card)
{
	/*
	 * I'm not sure if irqsave is needed but there could be a race
	 * condition since hfc_update_fifo_state could be called from
	 * both the IRQ handler and the *_(open|close) functions
	 */
	unsigned long flags;
	spin_lock_irqsave(&card->chans[B1].lock, flags);
	if (!card->fifo_suspended &&
		(card->chans[B1].status == open_framed ||
		card->chans[B1].status == open_voice)) {
		if (!(card->regs.fifo_en & hfc_FIFOEN_B1RX)) {
			card->regs.fifo_en |= hfc_FIFOEN_B1RX;
			hfc_clear_fifo_rx(&card->chans[B1].rx);
		}
		if (!(card->regs.fifo_en & hfc_FIFOEN_B1TX)) {
			card->regs.fifo_en |= hfc_FIFOEN_B1TX;
			hfc_clear_fifo_tx(&card->chans[B1].tx);
		}
	} else {
		if (card->regs.fifo_en & hfc_FIFOEN_B1RX)
			card->regs.fifo_en &= ~hfc_FIFOEN_B1RX;
		if (card->regs.fifo_en & hfc_FIFOEN_B1TX)
			card->regs.fifo_en &= ~hfc_FIFOEN_B1TX;
	}
	spin_unlock_irqrestore(&card->chans[B1].lock, flags);
	spin_lock_irqsave(&card->chans[B2].lock, flags);
	if (!card->fifo_suspended &&
		(card->chans[B2].status == open_framed ||
		card->chans[B2].status == open_voice ||
		card->chans[B2].status == sniff_aux)) {
		if (!(card->regs.fifo_en & hfc_FIFOEN_B2RX)) {
			card->regs.fifo_en |= hfc_FIFOEN_B2RX;
			hfc_clear_fifo_rx(&card->chans[B2].rx);
		}
		if (!(card->regs.fifo_en & hfc_FIFOEN_B2TX)) {
			card->regs.fifo_en |= hfc_FIFOEN_B2TX;
			hfc_clear_fifo_tx(&card->chans[B2].tx);
		}
	} else {
		if (card->regs.fifo_en & hfc_FIFOEN_B2RX)
			card->regs.fifo_en &= ~hfc_FIFOEN_B2RX;
		if (card->regs.fifo_en & hfc_FIFOEN_B2TX)
			card->regs.fifo_en &= ~hfc_FIFOEN_B2TX;
	}
	spin_unlock_irqrestore(&card->chans[B2].lock, flags);
	spin_lock_irqsave(&card->chans[D].lock, flags);
	if (!card->fifo_suspended &&
		card->chans[D].status == open_framed) {
		if (!(card->regs.fifo_en & hfc_FIFOEN_DTX)) {
			card->regs.fifo_en |= hfc_FIFOEN_DTX;
			card->chans[D].tx.ugly_framebuf_size = 0;
			card->chans[D].tx.ugly_framebuf_off = 0;
		}
	} else {
		if (card->regs.fifo_en & hfc_FIFOEN_DTX)
			card->regs.fifo_en &= ~hfc_FIFOEN_DTX;
	}
	spin_unlock_irqrestore(&card->chans[D].lock, flags);
	hfc_outb(card, hfc_FIFO_EN, card->regs.fifo_en);
}
static inline void hfc_suspend_fifo(struct hfc_card *card)
{
	card->fifo_suspended = TRUE;
	hfc_update_fifo_state(card);
	/*
	 * When L1 goes down D rx receives garbage; it is nice to
	 * clear it to avoid a CRC error on reactivation
	 * udelay is needed because the FIFO deactivation happens
	 * in 250us
	 */
	udelay(250);
	hfc_clear_fifo_rx(&card->chans[D].rx);
#ifdef DEBUG
	if (debug_level >= 3) {
		printk(KERN_DEBUG hfc_DRIVER_PREFIX
			"card %d: "
			"FIFOs suspended\n",
			card->cardnum);
	}
#endif
}
static inline void hfc_resume_fifo(struct hfc_card *card)
{
	card->fifo_suspended = FALSE;
	hfc_update_fifo_state(card);
#ifdef DEBUG
	if (debug_level >= 3) {
		printk(KERN_DEBUG hfc_DRIVER_PREFIX
			"card %d: "
			"FIFOs resumed\n",
			card->cardnum);
	}
#endif
}
static void hfc_check_l1_up(struct hfc_card *card)
{
	if ((!card->nt_mode && card->l1_state != 7)
		|| (card->nt_mode && card->l1_state != 3)) {
		hfc_outb(card, hfc_STATES, hfc_STATES_DO_ACTION |
			hfc_STATES_ACTIVATE|
				hfc_STATES_NT_G2_G3);
	/*
	 * 0 because this is quite verbose when an inferface is unconnected, jaw
	 */
#if 0
		if (debug_level >= 1) {
			printk(KERN_DEBUG hfc_DRIVER_PREFIX
				"card %d: "
				"L1 is down, bringing up L1.\n",
				card->cardnum);
		}
#endif
	}
}
/*******************
 * Dahdi interface *
 *******************/
static int hfc_zap_open(struct dahdi_chan *zaptel_chan)
{
	struct hfc_chan_duplex *chan = zaptel_chan->pvt;
	struct hfc_card *card = chan->card;
	spin_lock(&chan->lock);
	switch (chan->number) {
	case D:
		if (chan->status != free &&
			chan->status != open_framed) {
			spin_unlock(&chan->lock);
			return -EBUSY;
		}
		chan->status = open_framed;
	break;
	case B1:
	case B2:
		if (chan->status != free) {
			spin_unlock(&chan->lock);
			return -EBUSY;
		}
		chan->status = open_voice;
	break;
	}
	chan->open_by_zaptel = TRUE;
	try_module_get(THIS_MODULE);
	spin_unlock(&chan->lock);
	switch (chan->number) {
	case D:
	break;
	case B1:
		card->regs.m2 |= hfc_M2_PROC_TRANS;
		/*
		 * Enable transparent mode
		 */
		card->regs.ctmt |= hfc_CTMT_TRANSB1;
		/*
		* Reversed bit order
		*/
		card->regs.cirm |= hfc_CIRM_B1_REV;
		/*
		 * Enable transmission
		 */
		card->regs.sctrl |= hfc_SCTRL_B1_ENA;
		/*
		 * Enable reception
		 */
		card->regs.sctrl_r |= hfc_SCTRL_R_B1_ENA;
	break;
	case B2:
		card->regs.m2 |= hfc_M2_PROC_TRANS;
		card->regs.ctmt |= hfc_CTMT_TRANSB2;
		card->regs.cirm |= hfc_CIRM_B2_REV;
		card->regs.sctrl |= hfc_SCTRL_B2_ENA;
		card->regs.sctrl_r |= hfc_SCTRL_R_B2_ENA;
	break;
	}
	/*
	 * If not already enabled, enable processing transition (8KHz)
	 * interrupt
	 */
	hfc_outb(card, hfc_INT_M2, card->regs.m2);
	hfc_outb(card, hfc_CTMT, card->regs.ctmt);
	hfc_outb(card, hfc_CIRM, card->regs.cirm);
	hfc_outb(card, hfc_SCTRL, card->regs.sctrl);
	hfc_outb(card, hfc_SCTRL_R, card->regs.sctrl_r);
	hfc_update_fifo_state(card);
	printk(KERN_INFO hfc_DRIVER_PREFIX
		"card %d: "
		"chan %s opened as %s.\n",
		card->cardnum,
		chan->name,
		zaptel_chan->name);
	return 0;
}
static int hfc_zap_close(struct dahdi_chan *zaptel_chan)
{
	struct hfc_chan_duplex *chan = zaptel_chan->pvt;
	struct hfc_card *card = chan->card;
	if (!card) {
		printk(KERN_CRIT hfc_DRIVER_PREFIX
			"hfc_zap_close called with NULL card\n");
		return -1;
	}
	spin_lock(&chan->lock);
	if (chan->status == free) {
		spin_unlock(&chan->lock);
		return -EINVAL;
	}
	chan->status = free;
	chan->open_by_zaptel = FALSE;
	spin_unlock(&chan->lock);
	switch (chan->number) {
	case D:
	break;
	case B1:
		card->regs.ctmt &= ~hfc_CTMT_TRANSB1;
		card->regs.cirm &= ~hfc_CIRM_B1_REV;
		card->regs.sctrl &= ~hfc_SCTRL_B1_ENA;
		card->regs.sctrl_r &= ~hfc_SCTRL_R_B1_ENA;
	break;
	case B2:
		card->regs.ctmt &= ~hfc_CTMT_TRANSB2;
		card->regs.cirm &= ~hfc_CIRM_B2_REV;
		card->regs.sctrl &= ~hfc_SCTRL_B2_ENA;
		card->regs.sctrl_r &= ~hfc_SCTRL_R_B2_ENA;
	break;
	}
	if (card->chans[B1].status == free &&
		card->chans[B2].status == free)
		card->regs.m2 &= ~hfc_M2_PROC_TRANS;
	hfc_outb(card, hfc_INT_M2, card->regs.m2);
	hfc_outb(card, hfc_CTMT, card->regs.ctmt);
	hfc_outb(card, hfc_CIRM, card->regs.cirm);
	hfc_outb(card, hfc_SCTRL, card->regs.sctrl);
	hfc_outb(card, hfc_SCTRL_R, card->regs.sctrl_r);
	hfc_update_fifo_state(card);
	module_put(THIS_MODULE);
	printk(KERN_INFO hfc_DRIVER_PREFIX
		"card %d: "
		"chan %s closed as %s.\n",
		card->cardnum,
		chan->name,
		zaptel_chan->name);
	return 0;
}
static int hfc_zap_rbsbits(struct dahdi_chan *chan, int bits)
{
	return 0;
}
static int hfc_zap_ioctl(struct dahdi_chan *chan,
		unsigned int cmd, unsigned long data)
{
	switch (cmd) {
	default:
		return -ENOTTY;
	}
	return 0;
}
static void hfc_hdlc_hard_xmit(struct dahdi_chan *d_chan)
{
	struct hfc_chan_duplex *chan = d_chan->pvt;
	struct hfc_card *card = chan->card;
	struct dahdi_hfc *hfccard = card->ztdev;
	atomic_inc(&hfccard->hdlc_pending);
}
static int hfc_zap_startup(struct file *file, struct dahdi_span *span)
{
	struct dahdi_hfc *zthfc = dahdi_hfc_from_span(span);
	struct hfc_card *hfctmp = zthfc->card;
	int alreadyrunning;
	if (!hfctmp) {
		printk(KERN_INFO hfc_DRIVER_PREFIX
			"card %d: "
			"no card for span at startup!\n",
			hfctmp->cardnum);
	}
	alreadyrunning = span->flags & DAHDI_FLAG_RUNNING;
	if (!alreadyrunning)
		span->flags |= DAHDI_FLAG_RUNNING;
	return 0;
}
static int hfc_zap_shutdown(struct dahdi_span *span)
{
	return 0;
}
static int hfc_zap_maint(struct dahdi_span *span, int cmd)
{
	return 0;
}
static int hfc_zap_chanconfig(struct file *file, struct dahdi_chan *d_chan,
		int sigtype)
{
	struct hfc_chan_duplex *chan = d_chan->pvt;
	struct hfc_card *card = chan->card;
	struct dahdi_hfc *hfccard = card->ztdev;
	if ((sigtype == DAHDI_SIG_HARDHDLC) && (hfccard->sigchan == d_chan)) {
		hfccard->sigactive = 0;
		atomic_set(&hfccard->hdlc_pending, 0);
	}
	return 0;
}
static int hfc_zap_spanconfig(struct file *file, struct dahdi_span *span,
		struct dahdi_lineconfig *lc)
{
	span->lineconfig = lc->lineconfig;
	return 0;
}
static const struct dahdi_span_ops hfc_zap_span_ops = {
	.owner = THIS_MODULE,
	.chanconfig = hfc_zap_chanconfig,
	.spanconfig = hfc_zap_spanconfig,
	.startup = hfc_zap_startup,
	.shutdown = hfc_zap_shutdown,
	.maint = hfc_zap_maint,
	.rbsbits = hfc_zap_rbsbits,
	.open = hfc_zap_open,
	.close = hfc_zap_close,
	.ioctl = hfc_zap_ioctl,
	.hdlc_hard_xmit = hfc_hdlc_hard_xmit
};
static int hfc_zap_initialize(struct dahdi_hfc *hfccard)
{
	 struct hfc_card *hfctmp = hfccard->card;
	int i;
	hfccard->ddev = dahdi_create_device();
	hfccard->ddev->manufacturer = "Cologne Chips";
	hfccard->ddev->devicetype = "HFC-S PCI-A ISDN";
	hfccard->ddev->location = kasprintf(GFP_KERNEL,
			"PCI Bus %02d Slot %02d",
			hfctmp->pcidev->bus->number,
			PCI_SLOT(hfctmp->pcidev->devfn) + 1);
	memset(&hfccard->span, 0x0, sizeof(struct dahdi_span));
	sprintf(hfccard->span.name, "ZTHFC%d", hfctmp->cardnum + 1);
	sprintf(hfccard->span.desc,
			"HFC-S PCI A ISDN card %d [%s] ",
			hfctmp->cardnum,
			hfctmp->nt_mode ? "NT" : "TE");
	hfccard->span.spantype = hfctmp->nt_mode ? SPANTYPE_DIGITAL_BRI_NT : 
		SPANTYPE_DIGITAL_BRI_TE;
	hfccard->span.flags = 0;
	hfccard->span.ops = &hfc_zap_span_ops;
	hfccard->span.chans = hfccard->_chans;
	hfccard->span.channels = 3;
	for (i = 0; i < hfccard->span.channels; i++)
		hfccard->_chans[i] = &hfccard->chans[i];
	hfccard->span.deflaw = DAHDI_LAW_ALAW;
	hfccard->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_CCS;
	hfccard->span.offset = 0;
	for (i = 0; i < hfccard->span.channels; i++) {
		memset(&hfccard->chans[i], 0x0, sizeof(struct dahdi_chan));
		sprintf(hfccard->chans[i].name,
				"ZTHFC%d/%d/%d",
				hfctmp->cardnum + 1, 0, i + 1);
		printk(KERN_INFO hfc_DRIVER_PREFIX
			"card %d: "
			"registered %s\n",
			hfctmp->cardnum,
			hfccard->chans[i].name);
		if (i == hfccard->span.channels - 1) {
			hfccard->chans[i].sigcap = DAHDI_SIG_HARDHDLC;
			hfccard->sigchan = &hfccard->chans[DAHDI_D];
			hfccard->sigactive = 0;
			atomic_set(&hfccard->hdlc_pending, 0);
		} else {
			hfccard->chans[i].sigcap =
				DAHDI_SIG_CLEAR | DAHDI_SIG_DACS;
		}
		hfccard->chans[i].chanpos = i + 1;
	}
	hfccard->chans[DAHDI_D].readchunk  =
		hfctmp->chans[D].rx.zaptel_buffer;
	hfccard->chans[DAHDI_D].writechunk =
		hfctmp->chans[D].tx.zaptel_buffer;
	hfccard->chans[DAHDI_D].pvt = &hfctmp->chans[D];
	hfccard->chans[DAHDI_B1].readchunk  =
		hfctmp->chans[B1].rx.zaptel_buffer;
	hfccard->chans[DAHDI_B1].writechunk =
		hfctmp->chans[B1].tx.zaptel_buffer;
	hfccard->chans[DAHDI_B1].pvt = &hfctmp->chans[B1];
	hfccard->chans[DAHDI_B2].readchunk  =
		hfctmp->chans[B2].rx.zaptel_buffer;
	hfccard->chans[DAHDI_B2].writechunk =
		hfctmp->chans[B2].tx.zaptel_buffer;
	hfccard->chans[DAHDI_B2].pvt = &hfctmp->chans[B2];
	list_add_tail(&hfccard->span.device_node, &hfccard->ddev->spans);
	if (dahdi_register_device(hfccard->ddev, &hfctmp->pcidev->dev)) {
		printk(KERN_ERR "unable to register DAHDI device %s!\n",
				hfccard->span.name);
		return -1; /* FIXME: release resources? */
	}
	return 0;
}
static void hfc_dahdi_free(struct dahdi_hfc *hfccard)
{
	kfree(hfccard->ddev->location);
	dahdi_free_device(hfccard->ddev);
}
static void hfc_zap_transmit(struct hfc_chan_simplex *chan)
{
	hfc_fifo_put(chan, chan->zaptel_buffer, DAHDI_CHUNKSIZE);
}
static void hfc_zap_receive(struct hfc_chan_simplex *chan)
{
	hfc_fifo_get(chan, chan->zaptel_buffer, DAHDI_CHUNKSIZE);
}
/******************************************
 * Interrupt Handler
 ******************************************/
static void hfc_handle_timer_interrupt(struct hfc_card *card);
static void hfc_handle_state_interrupt(struct hfc_card *card);
static void hfc_handle_processing_interrupt(struct hfc_card *card);
static void hfc_frame_arrived(struct hfc_chan_duplex *chan);
static void hfc_handle_voice(struct hfc_card *card);
#if (KERNEL_VERSION(2, 6, 24) < LINUX_VERSION_CODE)
static irqreturn_t hfc_interrupt(int irq, void *dev_id)
#else
static irqreturn_t hfc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
#endif
{
	struct hfc_card *card = dev_id;
	unsigned long flags;
	u8 status, s1, s2;
	if (!card) {
		printk(KERN_CRIT hfc_DRIVER_PREFIX
			"spurious interrupt (IRQ %d)\n",
			irq);
		return IRQ_NONE;
	}
	spin_lock_irqsave(&card->lock, flags);
	status = hfc_inb(card, hfc_STATUS);
	if (!(status & hfc_STATUS_ANYINT)) {
		/*
		 * maybe we are sharing the irq
		 */
		spin_unlock_irqrestore(&card->lock, flags);
		return IRQ_NONE;
	}
	/* We used to ingore the IRQ when the card was in processing
	 * state but apparently there is no restriction to access the
	 * card in such state:
	 *
	 * Joerg Ciesielski wrote:
	 * > There is no restriction for the IRQ handler to access
	 * > HFC-S PCI during processing phase. A IRQ latency of 375 us
	 * > is also no problem since there are no interrupt sources in
	 * > HFC-S PCI which must be handled very fast.
	 * > Due to its deep fifos the IRQ latency can be several ms with
	 * > out the risk of loosing data. Even the S/T state interrupts
	 * > must not be handled with a latency less than <5ms.
	 * >
	 * > The processing phase only indicates that HFC-S PCI is
	 * > processing the Fifos as PCI master so that data is read and
	 * > written in the 32k memory window. But there is no restriction
	 * > to access data in the memory window during this time.
	 *
	 * // if (status & hfc_STATUS_PCI_PROC) {
	 * // return IRQ_HANDLED;
	 * // }
	 */
	s1 = hfc_inb(card, hfc_INT_S1);
	s2 = hfc_inb(card, hfc_INT_S2);
	if (s1 != 0) {
		if (s1 & hfc_INTS_TIMER) {
			/*
			 * timer (bit 7)
			 */
			hfc_handle_timer_interrupt(card);
		}
		if (s1 & hfc_INTS_L1STATE) {
			/*
			 * state machine (bit 6)
			 */
			hfc_handle_state_interrupt(card);
		}
		if (s1 & hfc_INTS_DREC) {
			/*
			 * D chan RX (bit 5)
			 */
			hfc_frame_arrived(&card->chans[D]);
		}
		if (s1 & hfc_INTS_B1REC) {
			/*
			 * B1 chan RX (bit 3)
			 */
			hfc_frame_arrived(&card->chans[B1]);
		}
		if (s1 & hfc_INTS_B2REC) {
			/*
			 * B2 chan RX (bit 4)
			 */
			hfc_frame_arrived(&card->chans[B2]);
		}
		if (s1 & hfc_INTS_DTRANS) {
			/*
			 * D chan TX (bit 2)
			 */
		}
		if (s1 & hfc_INTS_B1TRANS) {
			/*
			 * B1 chan TX (bit 0)
			 */
		}
		if (s1 & hfc_INTS_B2TRANS) {
			/*
			 * B2 chan TX (bit 1)
			 */
		}
	}
	if (s2 != 0) {
		if (s2 & hfc_M2_PMESEL) {
			/*
			 * kaboom irq (bit 7)
			 *
			 * CologneChip says:
			 *
			 * the meaning of this fatal error bit is that HFC-S
			 * PCI as PCI master could not access the PCI bus
			 * within 125us to finish its data processing. If this
			 * happens only very seldom it does not cause big
			 * problems but of course some B-channel or D-channel
			 * data will be corrupted due to this event.
			 *
			 * Unfortunately this bit is only set once after the
			 * problem occurs and can only be reseted by a
			 * software reset. That means it is not easily
			 * possible to check how often this fatal error
			 * happens.
			 *
			 */
			if (!card->sync_loss_reported) {
				printk(KERN_CRIT hfc_DRIVER_PREFIX
					"card %d: "
					"sync lost, pci performance too low!\n",
					card->cardnum);
				card->sync_loss_reported = TRUE;
			}
		}
		if (s2 & hfc_M2_GCI_MON_REC) {
			/*
			 * RxR monitor channel (bit 2)
			 */
		}
		if (s2 & hfc_M2_GCI_I_CHG) {
			/*
			 * GCI I-change  (bit 1)
			 */
		}
		if (s2 & hfc_M2_PROC_TRANS) {
			/*
			 * processing/non-processing transition  (bit 0)
			 */
			hfc_handle_processing_interrupt(card);
		}
	}
	spin_unlock_irqrestore(&card->lock, flags);
	return IRQ_HANDLED;
}
static void hfc_handle_timer_interrupt(struct hfc_card *card)
{
	if (card->ignore_first_timer_interrupt) {
		card->ignore_first_timer_interrupt = FALSE;
		return;
	}
	if ((card->nt_mode && card->l1_state == 3) ||
		(!card->nt_mode && card->l1_state == 7)) {
		card->regs.ctmt &= ~hfc_CTMT_TIMER_MASK;
		hfc_outb(card, hfc_CTMT, card->regs.ctmt);
		hfc_resume_fifo(card);
	}
}
static void hfc_handle_state_interrupt(struct hfc_card *card)
{
	u8 new_state = hfc_inb(card, hfc_STATES)  & hfc_STATES_STATE_MASK;
#ifdef DEBUG
	if (debug_level >= 1) {
		printk(KERN_DEBUG hfc_DRIVER_PREFIX
			"card %d: "
			"layer 1 state = %c%d\n",
			card->cardnum,
			card->nt_mode ? 'G' : 'F',
			new_state);
	}
#endif
	if (card->nt_mode) {
		/*
		 * NT mode
		 */
		if (new_state == 3) {
			/*
			 * fix to G3 state (see specs)
			 */
			hfc_outb(card, hfc_STATES, hfc_STATES_LOAD_STATE | 3);
		}
		if (new_state == 3 && card->l1_state != 3)
			hfc_resume_fifo(card);
		if (new_state != 3 && card->l1_state == 3)
			hfc_suspend_fifo(card);
	} else {
		if (new_state == 3) {
			/*
			 * Keep L1 up... zaptel & libpri expects
			 * a always up L1...
			 * Enable only  when using an unpatched libpri
			 */
			if (force_l1_up) {
				hfc_outb(card, hfc_STATES,
					hfc_STATES_DO_ACTION |
					hfc_STATES_ACTIVATE|
					hfc_STATES_NT_G2_G3);
			}
		}
		if (new_state == 7 && card->l1_state != 7) {
			/*
			 * TE is now active, schedule FIFO activation after
			 * some time, otherwise the first frames are lost
			 */
			card->regs.ctmt |= hfc_CTMT_TIMER_50 |
				hfc_CTMT_TIMER_CLEAR;
			hfc_outb(card, hfc_CTMT, card->regs.ctmt);
			/*
			 * Activating the timer firest an
			 * interrupt immediately, we
			 * obviously need to ignore it
			 */
			card->ignore_first_timer_interrupt = TRUE;
		}
		if (new_state != 7 && card->l1_state == 7) {
			/*
			 * TE has become inactive, disable FIFO
			 */
			hfc_suspend_fifo(card);
		}
	}
	card->l1_state = new_state;
}
static void hfc_handle_processing_interrupt(struct hfc_card *card)
{
	int available_bytes = 0;
	/*
	 * Synchronize with the first enabled channel
	 */
	if (card->regs.fifo_en & hfc_FIFOEN_B1RX)
		available_bytes = hfc_fifo_used_rx(&card->chans[B1].rx);
	if (card->regs.fifo_en & hfc_FIFOEN_B2RX)
		available_bytes = hfc_fifo_used_rx(&card->chans[B2].rx);
	else
		available_bytes = -1;
	if ((available_bytes == -1 && card->ticks == 8) ||
		available_bytes >= DAHDI_CHUNKSIZE + hfc_RX_FIFO_PRELOAD) {
		card->ticks = 0;
		if (available_bytes > DAHDI_CHUNKSIZE*2 + hfc_RX_FIFO_PRELOAD) {
			card->late_irqs++;
			/*
			 * we are out of sync, clear fifos, jaw
			 */
			hfc_clear_fifo_rx(&card->chans[B1].rx);
			hfc_clear_fifo_tx(&card->chans[B1].tx);
			hfc_clear_fifo_rx(&card->chans[B2].rx);
			hfc_clear_fifo_tx(&card->chans[B2].tx);
#ifdef DEBUG
			if (debug_level >= 4) {
				printk(KERN_DEBUG hfc_DRIVER_PREFIX
					"card %d: "
					"late IRQ, %d bytes late\n",
					card->cardnum,
					available_bytes -
						(DAHDI_CHUNKSIZE +
						 hfc_RX_FIFO_PRELOAD));
			}
#endif
		} else {
			hfc_handle_voice(card);
		}
	}
	card->ticks++;
}
static void hfc_handle_voice(struct hfc_card *card)
{
	struct dahdi_hfc *hfccard = card->ztdev;
	int frame_left, res;
	unsigned char buf[hfc_HDLC_BUF_LEN];
	unsigned int size = sizeof(buf) / sizeof(buf[0]);
	if (card->chans[B1].status != open_voice &&
		card->chans[B2].status != open_voice)
		return;
	dahdi_transmit(&hfccard->span);
	if (card->regs.fifo_en & hfc_FIFOEN_B1TX)
		hfc_zap_transmit(&card->chans[B1].tx);
	if (card->regs.fifo_en & hfc_FIFOEN_B2TX)
		hfc_zap_transmit(&card->chans[B2].tx);
	/*
	 * dahdi hdlc frame tx
	 */
	if (atomic_read(&hfccard->hdlc_pending)) {
		hfc_check_l1_up(card);
		res = dahdi_hdlc_getbuf(hfccard->sigchan, buf, &size);
			if (size > 0) {
				hfccard->sigactive = 1;
				memcpy(card->chans[D].tx.ugly_framebuf +
				card->chans[D].tx.ugly_framebuf_size,
				buf, size);
				card->chans[D].tx.ugly_framebuf_size += size;
			if (res != 0) {
					hfc_fifo_put_frame(&card->chans[D].tx,
					card->chans[D].tx.ugly_framebuf,
					card->chans[D].tx.ugly_framebuf_size);
					++hfccard->frames_out;
					hfccard->sigactive = 0;
					card->chans[D].tx.ugly_framebuf_size
						= 0;
					atomic_dec(&hfccard->hdlc_pending);
				}
			}
	}
	/*
	 * dahdi hdlc frame tx done
	 */
	if (card->regs.fifo_en & hfc_FIFOEN_B1RX)
		hfc_zap_receive(&card->chans[B1].rx);
	else
		memset(&card->chans[B1].rx.zaptel_buffer, 0x7f,
			sizeof(card->chans[B1].rx.zaptel_buffer));
	if (card->regs.fifo_en & hfc_FIFOEN_B2RX)
		hfc_zap_receive(&card->chans[B2].rx);
	else
		memset(&card->chans[B2].rx.zaptel_buffer, 0x7f,
			sizeof(card->chans[B1].rx.zaptel_buffer));
	/*
	 * Echo cancellation
	 */
	dahdi_ec_chunk(&hfccard->chans[DAHDI_B1],
			card->chans[B1].rx.zaptel_buffer,
			card->chans[B1].tx.zaptel_buffer);
	dahdi_ec_chunk(&hfccard->chans[DAHDI_B2],
			card->chans[B2].rx.zaptel_buffer,
			card->chans[B2].tx.zaptel_buffer);
	/*
	 * dahdi hdlc frame rx
	 */
	if (hfc_fifo_has_frames(&card->chans[D].rx))
		hfc_frame_arrived(&card->chans[D]);
	if (card->chans[D].rx.ugly_framebuf_size) {
		frame_left = card->chans[D].rx.ugly_framebuf_size -
			card->chans[D].rx.ugly_framebuf_off ;
		if (frame_left > hfc_HDLC_BUF_LEN) {
			dahdi_hdlc_putbuf(hfccard->sigchan,
					card->chans[D].rx.ugly_framebuf +
					card->chans[D].rx.ugly_framebuf_off,
					hfc_HDLC_BUF_LEN);
			card->chans[D].rx.ugly_framebuf_off +=
				hfc_HDLC_BUF_LEN;
		} else {
			dahdi_hdlc_putbuf(hfccard->sigchan,
					card->chans[D].rx.ugly_framebuf +
					card->chans[D].rx.ugly_framebuf_off,
					frame_left);
			dahdi_hdlc_finish(hfccard->sigchan);
			card->chans[D].rx.ugly_framebuf_size = 0;
			card->chans[D].rx.ugly_framebuf_off = 0;
		}
	}
	/*
	 * dahdi hdlc frame rx done
	 */
	if (hfccard->span.flags & DAHDI_FLAG_RUNNING)
		dahdi_receive(&hfccard->span);
}
static void hfc_frame_arrived(struct hfc_chan_duplex *chan)
{
	struct hfc_card *card = chan->card;
	int antiloop = 16;
	while (hfc_fifo_has_frames(&chan->rx) && --antiloop) {
		int frame_size = hfc_fifo_get_frame_size(&chan->rx);
		if (frame_size < 3) {
#ifdef DEBUG
			if (debug_level >= 2)
				printk(KERN_DEBUG hfc_DRIVER_PREFIX
					"card %d: "
					"chan %s: "
					"invalid frame received, "
					"just %d bytes\n",
					card->cardnum,
					chan->name,
					frame_size);
#endif
			hfc_fifo_drop_frame(&chan->rx);
			continue;
		} else if (frame_size == 3) {
#ifdef DEBUG
			if (debug_level >= 2)
				printk(KERN_DEBUG hfc_DRIVER_PREFIX
					"card %d: "
					"chan %s: "
					"empty frame received\n",
					card->cardnum,
					chan->name);
#endif
			hfc_fifo_drop_frame(&chan->rx);
			continue;
		}
		if (chan->open_by_zaptel) {
			if (card->chans[D].rx.ugly_framebuf_size) {
				/*
				 * We have to wait for Dahdi to transmit the
				 * frame... wait for next time
				 */
				 break;
			} else {
				card->chans[D].rx.ugly_framebuf_size =
					frame_size - 1;
				if (hfc_fifo_get_frame(&card->chans[D].rx,
					card->chans[D].rx.ugly_framebuf,
					frame_size - 1) == -1) {
					continue;
				}
			}
		} else {
			hfc_fifo_drop_frame(&chan->rx);
		}
	}
	if (!antiloop)
		printk(KERN_CRIT hfc_DRIVER_PREFIX
			"card %d: "
			"Infinite loop detected\n",
			card->cardnum);
}
/******************************************
 * Module initialization and cleanup
 ******************************************/
static int __devinit hfc_probe(struct pci_dev *pci_dev,
	const struct pci_device_id *ent)
{
	static int cardnum;
	int err;
	int i;
	struct hfc_card *card = NULL;
	struct dahdi_hfc *zthfc = NULL;
	card = kmalloc(sizeof(struct hfc_card), GFP_KERNEL);
	if (!card) {
		printk(KERN_CRIT hfc_DRIVER_PREFIX
			"unable to kmalloc!\n");
		err = -ENOMEM;
		goto err_alloc_hfccard;
	}
	memset(card, 0x00, sizeof(struct hfc_card));
	card->cardnum = cardnum;
	card->pcidev = pci_dev;
	spin_lock_init(&card->lock);
	pci_set_drvdata(pci_dev, card);
	err = pci_enable_device(pci_dev);
	if (err)
		goto err_pci_enable_device;
	err = pci_set_dma_mask(pci_dev, PCI_DMA_32BIT);
	if (err) {
		printk(KERN_ERR hfc_DRIVER_PREFIX
			"card %d: "
			"No suitable DMA configuration available.\n",
			card->cardnum);
		goto err_pci_set_dma_mask;
	}
	pci_write_config_word(pci_dev, PCI_COMMAND, PCI_COMMAND_MEMORY);
	err = pci_request_regions(pci_dev, hfc_DRIVER_NAME);
	if (err) {
		printk(KERN_CRIT hfc_DRIVER_PREFIX
			"card %d: "
			"cannot request I/O memory region\n",
			card->cardnum);
		goto err_pci_request_regions;
	}
	pci_set_master(pci_dev);
	if (!pci_dev->irq) {
		printk(KERN_CRIT hfc_DRIVER_PREFIX
			"card %d: "
			"no irq!\n",
			card->cardnum);
		err = -ENODEV;
		goto err_noirq;
	}
	card->io_bus_mem = pci_resource_start(pci_dev, 1);
	if (!card->io_bus_mem) {
		printk(KERN_CRIT hfc_DRIVER_PREFIX
			"card %d: "
			"no iomem!\n",
			card->cardnum);
		err = -ENODEV;
		goto err_noiobase;
	}
	card->io_mem = ioremap(card->io_bus_mem, hfc_PCI_MEM_SIZE);
	if (!(card->io_mem)) {
		printk(KERN_CRIT hfc_DRIVER_PREFIX
			"card %d: "
			"cannot ioremap I/O memory\n",
			card->cardnum);
		err = -ENODEV;
		goto err_ioremap;
	}
	/*
	 * pci_alloc_consistent guarantees alignment
	 * (Documentation/DMA-mapping.txt)
	 */
	card->fifo_mem = pci_alloc_consistent(pci_dev,
			hfc_FIFO_SIZE, &card->fifo_bus_mem);
	if (!card->fifo_mem) {
		printk(KERN_CRIT hfc_DRIVER_PREFIX
			"card %d: "
			"unable to allocate FIFO DMA memory!\n",
			card->cardnum);
		err = -ENOMEM;
		goto err_alloc_fifo;
	}
	memset(card->fifo_mem, 0x00, hfc_FIFO_SIZE);
	card->fifos = card->fifo_mem;
	pci_write_config_dword(card->pcidev, hfc_PCI_MWBA, card->fifo_bus_mem);
	err = request_irq(card->pcidev->irq, &hfc_interrupt,
#if (KERNEL_VERSION(2, 6, 23) < LINUX_VERSION_CODE)
		IRQF_SHARED, hfc_DRIVER_NAME, card);
#else
		SA_SHIRQ, hfc_DRIVER_NAME, card);
#endif
	if (err) {
		printk(KERN_CRIT hfc_DRIVER_PREFIX
			"card %d: "
			"unable to register irq\n",
			card->cardnum);
		goto err_request_irq;
	}
	card->nt_mode = FALSE;
	if (modes & (1 << card->cardnum))
		card->nt_mode = TRUE;
	for (i = 0; i < nt_modes_count; i++) {
		if (nt_modes[i] == card->cardnum)
			card->nt_mode = TRUE;
	}
	/*
	 * D Channel
	 */
	card->chans[D].card = card;
	card->chans[D].name = "D";
	card->chans[D].status = free;
	card->chans[D].number = D;
	spin_lock_init(&card->chans[D].lock);
	card->chans[D].rx.chan      = &card->chans[D];
	card->chans[D].rx.fifo_base = card->fifos + 0x4000;
	card->chans[D].rx.z_base    = card->fifos + 0x4000;
	card->chans[D].rx.z1_base   = card->fifos + 0x6080;
	card->chans[D].rx.z2_base   = card->fifos + 0x6082;
	card->chans[D].rx.z_min     = 0x0000;
	card->chans[D].rx.z_max     = 0x01FF;
	card->chans[D].rx.f_min     = 0x10;
	card->chans[D].rx.f_max     = 0x1F;
	card->chans[D].rx.f1        = card->fifos + 0x60a0;
	card->chans[D].rx.f2        = card->fifos + 0x60a1;
	card->chans[D].rx.fifo_size = card->chans[D].rx.z_max
		- card->chans[D].rx.z_min + 1;
	card->chans[D].rx.f_num     = card->chans[D].rx.f_max
		- card->chans[D].rx.f_min + 1;
	card->chans[D].tx.chan      = &card->chans[D];
	card->chans[D].tx.fifo_base = card->fifos + 0x0000;
	card->chans[D].tx.z_base    = card->fifos + 0x0000;
	card->chans[D].tx.z1_base   = card->fifos + 0x2080;
	card->chans[D].tx.z2_base   = card->fifos + 0x2082;
	card->chans[D].tx.z_min     = 0x0000;
	card->chans[D].tx.z_max     = 0x01FF;
	card->chans[D].tx.f_min     = 0x10;
	card->chans[D].tx.f_max     = 0x1F;
	card->chans[D].tx.f1        = card->fifos + 0x20a0;
	card->chans[D].tx.f2        = card->fifos + 0x20a1;
	card->chans[D].tx.fifo_size = card->chans[D].tx.z_max -
		card->chans[D].tx.z_min + 1;
	card->chans[D].tx.f_num     = card->chans[D].tx.f_max -
		card->chans[D].tx.f_min + 1;
	/*
	 * B1 Channel
	 */
	card->chans[B1].card = card;
	card->chans[B1].name = "B1";
	card->chans[B1].status = free;
	card->chans[B1].number = B1;
	card->chans[B1].protocol = 0;
	spin_lock_init(&card->chans[B1].lock);
	card->chans[B1].rx.chan      = &card->chans[B1];
	card->chans[B1].rx.fifo_base = card->fifos + 0x4200;
	card->chans[B1].rx.z_base    = card->fifos + 0x4000;
	card->chans[B1].rx.z1_base   = card->fifos + 0x6000;
	card->chans[B1].rx.z2_base   = card->fifos + 0x6002;
	card->chans[B1].rx.z_min     = 0x0200;
	card->chans[B1].rx.z_max     = 0x1FFF;
	card->chans[B1].rx.f_min     = 0x00;
	card->chans[B1].rx.f_max     = 0x1F;
	card->chans[B1].rx.f1        = card->fifos + 0x6080;
	card->chans[B1].rx.f2        = card->fifos + 0x6081;
	card->chans[B1].rx.fifo_size = card->chans[B1].rx.z_max -
		card->chans[B1].rx.z_min + 1;
	card->chans[B1].rx.f_num     = card->chans[B1].rx.f_max -
		card->chans[B1].rx.f_min + 1;
	card->chans[B1].tx.chan      = &card->chans[B1];
	card->chans[B1].tx.fifo_base = card->fifos + 0x0200;
	card->chans[B1].tx.z_base    = card->fifos + 0x0000;
	card->chans[B1].tx.z1_base   = card->fifos + 0x2000;
	card->chans[B1].tx.z2_base   = card->fifos + 0x2002;
	card->chans[B1].tx.z_min     = 0x0200;
	card->chans[B1].tx.z_max     = 0x1FFF;
	card->chans[B1].tx.f_min     = 0x00;
	card->chans[B1].tx.f_max     = 0x1F;
	card->chans[B1].tx.f1        = card->fifos + 0x2080;
	card->chans[B1].tx.f2        = card->fifos + 0x2081;
	card->chans[B1].tx.fifo_size = card->chans[B1].tx.z_max -
		card->chans[B1].tx.z_min + 1;
	card->chans[B1].tx.f_num     = card->chans[B1].tx.f_max -
		card->chans[B1].tx.f_min + 1;
	/*
	 * B2 Channel
	 */
	card->chans[B2].card = card;
	card->chans[B2].name = "B2";
	card->chans[B2].status = free;
	card->chans[B2].number = B2;
	card->chans[B2].protocol = 0;
	spin_lock_init(&card->chans[B2].lock);
	card->chans[B2].rx.chan      = &card->chans[B2];
	card->chans[B2].rx.fifo_base = card->fifos + 0x6200,
	card->chans[B2].rx.z_base    = card->fifos + 0x6000;
	card->chans[B2].rx.z1_base   = card->fifos + 0x6100;
	card->chans[B2].rx.z2_base   = card->fifos + 0x6102;
	card->chans[B2].rx.z_min     = 0x0200;
	card->chans[B2].rx.z_max     = 0x1FFF;
	card->chans[B2].rx.f_min     = 0x00;
	card->chans[B2].rx.f_max     = 0x1F;
	card->chans[B2].rx.f1        = card->fifos + 0x6180;
	card->chans[B2].rx.f2        = card->fifos + 0x6181;
	card->chans[B2].rx.fifo_size = card->chans[B2].rx.z_max -
		card->chans[B2].rx.z_min + 1;
	card->chans[B2].rx.f_num     = card->chans[B2].rx.f_max -
		card->chans[B2].rx.f_min + 1;
	card->chans[B2].tx.chan      = &card->chans[B2];
	card->chans[B2].tx.fifo_base = card->fifos + 0x2200;
	card->chans[B2].tx.z_base    = card->fifos + 0x2000;
	card->chans[B2].tx.z1_base   = card->fifos + 0x2100;
	card->chans[B2].tx.z2_base   = card->fifos + 0x2102;
	card->chans[B2].tx.z_min     = 0x0200;
	card->chans[B2].tx.z_max     = 0x1FFF;
	card->chans[B2].tx.f_min     = 0x00;
	card->chans[B2].tx.f_max     = 0x1F;
	card->chans[B2].tx.f1        = card->fifos + 0x2180;
	card->chans[B2].tx.f2        = card->fifos + 0x2181;
	card->chans[B2].tx.fifo_size = card->chans[B2].tx.z_max -
		card->chans[B2].tx.z_min + 1;
	card->chans[B2].tx.f_num     = card->chans[B2].tx.f_max -
		card->chans[B2].tx.f_min + 1;
	/*
	 * All done
	 */
	zthfc = kmalloc(sizeof(struct dahdi_hfc), GFP_KERNEL);
	if (!zthfc) {
		printk(KERN_CRIT hfc_DRIVER_PREFIX
			"unable to kmalloc!\n");
		goto err_request_irq;
	}
	memset(zthfc, 0x0, sizeof(struct dahdi_hfc));
	zthfc->card = card;
	hfc_zap_initialize(zthfc);
	card->ztdev = zthfc;
	hfc_resetCard(card);
	printk(KERN_INFO hfc_DRIVER_PREFIX
		"card %d configured for %s mode at mem %#lx (0x%p) IRQ %u\n",
		card->cardnum,
		card->nt_mode ? "NT" : "TE",
		card->io_bus_mem,
		card->io_mem,
		card->pcidev->irq);
	cardnum++;
	return 0;
err_request_irq:
	pci_free_consistent(pci_dev, hfc_FIFO_SIZE,
		card->fifo_mem, card->fifo_bus_mem);
err_alloc_fifo:
	iounmap(card->io_mem);
err_ioremap:
err_noiobase:
err_noirq:
	pci_release_regions(pci_dev);
err_pci_request_regions:
err_pci_set_dma_mask:
err_pci_enable_device:
	kfree(card);
err_alloc_hfccard:
	return err;
}
static void __devexit hfc_remove(struct pci_dev *pci_dev)
{
	struct hfc_card *card = pci_get_drvdata(pci_dev);
	printk(KERN_INFO hfc_DRIVER_PREFIX
		"card %d: "
		"shutting down card at %p.\n",
		card->cardnum,
		card->io_mem);
	hfc_softreset(card);
	dahdi_unregister_device(card->ztdev->ddev);
	hfc_dahdi_free(card->ztdev);
	/*
	 * disable memio and busmaster
	 */
	pci_write_config_word(pci_dev, PCI_COMMAND, 0);
	free_irq(pci_dev->irq, card);
	pci_free_consistent(pci_dev, hfc_FIFO_SIZE,
		card->fifo_mem, card->fifo_bus_mem);
	iounmap(card->io_mem);
	pci_release_regions(pci_dev);
	pci_disable_device(pci_dev);
	kfree(card);
}
/******************************************
 * Module stuff
 ******************************************/
static int __init hfc_init_module(void)
{
	int ret;
	printk(KERN_INFO hfc_DRIVER_PREFIX
		hfc_DRIVER_STRING " loading\n");
	ret = dahdi_pci_module(&hfc_driver);
	return ret;
}
module_init(hfc_init_module);
static void __exit hfc_module_exit(void)
{
	pci_unregister_driver(&hfc_driver);
	printk(KERN_INFO hfc_DRIVER_PREFIX
		hfc_DRIVER_STRING " unloaded\n");
}
module_exit(hfc_module_exit);
#endif
MODULE_DESCRIPTION(hfc_DRIVER_DESCR);
MODULE_AUTHOR("Jens Wilke <jw_vzaphfc@headissue.com>, "
		"Daniele (Vihai) Orlandi <daniele@orlandi.com>, "
		"Jose A. Deniz <odicha@hotmail.com>");
MODULE_ALIAS("vzaphfc");
#ifdef MODULE_LICENSE
MODULE_LICENSE("GPL");
#endif
module_param(modes, int, 0444);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
module_param_array(nt_modes, int, &nt_modes_count, 0444);
#else
module_param_array(nt_modes, int, nt_modes_count, 0444);
#endif
module_param(force_l1_up, int, 0444);
#ifdef DEBUG
module_param(debug_level, int, 0444);
#endif
MODULE_PARM_DESC(modes, "[Deprecated] bit-mask to configure NT mode");
MODULE_PARM_DESC(nt_modes,
		"Comma-separated list of card IDs to configure in NT mode");
MODULE_PARM_DESC(force_l1_up, "Don't allow L1 to go down");
#ifdef DEBUG
MODULE_PARM_DESC(debug_level, "Debug verbosity level");
#endif
 
     |