hfcpci.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255
  1. /*
  2. *
  3. * hfcpci.c low level driver for CCD's hfc-pci based cards
  4. *
  5. * Author Werner Cornelius (werner@isdn4linux.de)
  6. * based on existing driver for CCD hfc ISA cards
  7. * type approval valid for HFC-S PCI A based card
  8. *
  9. * Copyright 1999 by Werner Cornelius (werner@isdn-development.de)
  10. * Copyright 2008 by Karsten Keil <kkeil@novell.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/delay.h>
  30. #include <linux/mISDNhw.h>
  31. #include "hfc_pci.h"
  32. static const char *hfcpci_revision = "2.0";
  33. #define MAX_CARDS 8
  34. static int HFC_cnt;
  35. static uint debug;
  36. MODULE_AUTHOR("Karsten Keil");
  37. MODULE_LICENSE("GPL");
  38. module_param(debug, uint, 0);
  39. static LIST_HEAD(HFClist);
  40. DEFINE_RWLOCK(HFClock);
  41. enum {
  42. HFC_CCD_2BD0,
  43. HFC_CCD_B000,
  44. HFC_CCD_B006,
  45. HFC_CCD_B007,
  46. HFC_CCD_B008,
  47. HFC_CCD_B009,
  48. HFC_CCD_B00A,
  49. HFC_CCD_B00B,
  50. HFC_CCD_B00C,
  51. HFC_CCD_B100,
  52. HFC_CCD_B700,
  53. HFC_CCD_B701,
  54. HFC_ASUS_0675,
  55. HFC_BERKOM_A1T,
  56. HFC_BERKOM_TCONCEPT,
  57. HFC_ANIGMA_MC145575,
  58. HFC_ZOLTRIX_2BD0,
  59. HFC_DIGI_DF_M_IOM2_E,
  60. HFC_DIGI_DF_M_E,
  61. HFC_DIGI_DF_M_IOM2_A,
  62. HFC_DIGI_DF_M_A,
  63. HFC_ABOCOM_2BD1,
  64. HFC_SITECOM_DC105V2,
  65. };
  66. struct hfcPCI_hw {
  67. unsigned char cirm;
  68. unsigned char ctmt;
  69. unsigned char clkdel;
  70. unsigned char states;
  71. unsigned char conn;
  72. unsigned char mst_m;
  73. unsigned char int_m1;
  74. unsigned char int_m2;
  75. unsigned char sctrl;
  76. unsigned char sctrl_r;
  77. unsigned char sctrl_e;
  78. unsigned char trm;
  79. unsigned char fifo_en;
  80. unsigned char bswapped;
  81. unsigned char protocol;
  82. int nt_timer;
  83. unsigned char *pci_io; /* start of PCI IO memory */
  84. dma_addr_t dmahandle;
  85. void *fifos; /* FIFO memory */
  86. int last_bfifo_cnt[2];
  87. /* marker saving last b-fifo frame count */
  88. struct timer_list timer;
  89. };
  90. #define HFC_CFG_MASTER 1
  91. #define HFC_CFG_SLAVE 2
  92. #define HFC_CFG_PCM 3
  93. #define HFC_CFG_2HFC 4
  94. #define HFC_CFG_SLAVEHFC 5
  95. #define HFC_CFG_NEG_F0 6
  96. #define HFC_CFG_SW_DD_DU 7
  97. #define FLG_HFC_TIMER_T1 16
  98. #define FLG_HFC_TIMER_T3 17
  99. #define NT_T1_COUNT 1120 /* number of 3.125ms interrupts (3.5s) */
  100. #define NT_T3_COUNT 31 /* number of 3.125ms interrupts (97 ms) */
  101. #define CLKDEL_TE 0x0e /* CLKDEL in TE mode */
  102. #define CLKDEL_NT 0x6c /* CLKDEL in NT mode */
  103. struct hfc_pci {
  104. struct list_head list;
  105. u_char subtype;
  106. u_char chanlimit;
  107. u_char initdone;
  108. u_long cfg;
  109. u_int irq;
  110. u_int irqcnt;
  111. struct pci_dev *pdev;
  112. struct hfcPCI_hw hw;
  113. spinlock_t lock; /* card lock */
  114. struct dchannel dch;
  115. struct bchannel bch[2];
  116. };
  117. /* Interface functions */
  118. static void
  119. enable_hwirq(struct hfc_pci *hc)
  120. {
  121. hc->hw.int_m2 |= HFCPCI_IRQ_ENABLE;
  122. Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);
  123. }
  124. static void
  125. disable_hwirq(struct hfc_pci *hc)
  126. {
  127. hc->hw.int_m2 &= ~((u_char)HFCPCI_IRQ_ENABLE);
  128. Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);
  129. }
  130. /*
  131. * free hardware resources used by driver
  132. */
  133. static void
  134. release_io_hfcpci(struct hfc_pci *hc)
  135. {
  136. /* disable memory mapped ports + busmaster */
  137. pci_write_config_word(hc->pdev, PCI_COMMAND, 0);
  138. del_timer(&hc->hw.timer);
  139. pci_free_consistent(hc->pdev, 0x8000, hc->hw.fifos, hc->hw.dmahandle);
  140. iounmap((void *)hc->hw.pci_io);
  141. }
  142. /*
  143. * set mode (NT or TE)
  144. */
  145. static void
  146. hfcpci_setmode(struct hfc_pci *hc)
  147. {
  148. if (hc->hw.protocol == ISDN_P_NT_S0) {
  149. hc->hw.clkdel = CLKDEL_NT; /* ST-Bit delay for NT-Mode */
  150. hc->hw.sctrl |= SCTRL_MODE_NT; /* NT-MODE */
  151. hc->hw.states = 1; /* G1 */
  152. } else {
  153. hc->hw.clkdel = CLKDEL_TE; /* ST-Bit delay for TE-Mode */
  154. hc->hw.sctrl &= ~SCTRL_MODE_NT; /* TE-MODE */
  155. hc->hw.states = 2; /* F2 */
  156. }
  157. Write_hfc(hc, HFCPCI_CLKDEL, hc->hw.clkdel);
  158. Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | hc->hw.states);
  159. udelay(10);
  160. Write_hfc(hc, HFCPCI_STATES, hc->hw.states | 0x40); /* Deactivate */
  161. Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);
  162. }
  163. /*
  164. * function called to reset the HFC PCI chip. A complete software reset of chip
  165. * and fifos is done.
  166. */
  167. static void
  168. reset_hfcpci(struct hfc_pci *hc)
  169. {
  170. u_char val;
  171. int cnt = 0;
  172. printk(KERN_DEBUG "reset_hfcpci: entered\n");
  173. val = Read_hfc(hc, HFCPCI_CHIP_ID);
  174. printk(KERN_INFO "HFC_PCI: resetting HFC ChipId(%x)\n", val);
  175. /* enable memory mapped ports, disable busmaster */
  176. pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);
  177. disable_hwirq(hc);
  178. /* enable memory ports + busmaster */
  179. pci_write_config_word(hc->pdev, PCI_COMMAND,
  180. PCI_ENA_MEMIO + PCI_ENA_MASTER);
  181. val = Read_hfc(hc, HFCPCI_STATUS);
  182. printk(KERN_DEBUG "HFC-PCI status(%x) before reset\n", val);
  183. hc->hw.cirm = HFCPCI_RESET; /* Reset On */
  184. Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
  185. set_current_state(TASK_UNINTERRUPTIBLE);
  186. mdelay(10); /* Timeout 10ms */
  187. hc->hw.cirm = 0; /* Reset Off */
  188. Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
  189. val = Read_hfc(hc, HFCPCI_STATUS);
  190. printk(KERN_DEBUG "HFC-PCI status(%x) after reset\n", val);
  191. while (cnt < 50000) { /* max 50000 us */
  192. udelay(5);
  193. cnt += 5;
  194. val = Read_hfc(hc, HFCPCI_STATUS);
  195. if (!(val & 2))
  196. break;
  197. }
  198. printk(KERN_DEBUG "HFC-PCI status(%x) after %dus\n", val, cnt);
  199. hc->hw.fifo_en = 0x30; /* only D fifos enabled */
  200. hc->hw.bswapped = 0; /* no exchange */
  201. hc->hw.ctmt = HFCPCI_TIM3_125 | HFCPCI_AUTO_TIMER;
  202. hc->hw.trm = HFCPCI_BTRANS_THRESMASK; /* no echo connect , threshold */
  203. hc->hw.sctrl = 0x40; /* set tx_lo mode, error in datasheet ! */
  204. hc->hw.sctrl_r = 0;
  205. hc->hw.sctrl_e = HFCPCI_AUTO_AWAKE; /* S/T Auto awake */
  206. hc->hw.mst_m = 0;
  207. if (test_bit(HFC_CFG_MASTER, &hc->cfg))
  208. hc->hw.mst_m |= HFCPCI_MASTER; /* HFC Master Mode */
  209. if (test_bit(HFC_CFG_NEG_F0, &hc->cfg))
  210. hc->hw.mst_m |= HFCPCI_F0_NEGATIV;
  211. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  212. Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
  213. Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);
  214. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
  215. hc->hw.int_m1 = HFCPCI_INTS_DTRANS | HFCPCI_INTS_DREC |
  216. HFCPCI_INTS_L1STATE | HFCPCI_INTS_TIMER;
  217. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  218. /* Clear already pending ints */
  219. if (Read_hfc(hc, HFCPCI_INT_S1));
  220. /* set NT/TE mode */
  221. hfcpci_setmode(hc);
  222. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  223. Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
  224. /*
  225. * Init GCI/IOM2 in master mode
  226. * Slots 0 and 1 are set for B-chan 1 and 2
  227. * D- and monitor/CI channel are not enabled
  228. * STIO1 is used as output for data, B1+B2 from ST->IOM+HFC
  229. * STIO2 is used as data input, B1+B2 from IOM->ST
  230. * ST B-channel send disabled -> continous 1s
  231. * The IOM slots are always enabled
  232. */
  233. if (test_bit(HFC_CFG_PCM, &hc->cfg)) {
  234. /* set data flow directions: connect B1,B2: HFC to/from PCM */
  235. hc->hw.conn = 0x09;
  236. } else {
  237. hc->hw.conn = 0x36; /* set data flow directions */
  238. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {
  239. Write_hfc(hc, HFCPCI_B1_SSL, 0xC0);
  240. Write_hfc(hc, HFCPCI_B2_SSL, 0xC1);
  241. Write_hfc(hc, HFCPCI_B1_RSL, 0xC0);
  242. Write_hfc(hc, HFCPCI_B2_RSL, 0xC1);
  243. } else {
  244. Write_hfc(hc, HFCPCI_B1_SSL, 0x80);
  245. Write_hfc(hc, HFCPCI_B2_SSL, 0x81);
  246. Write_hfc(hc, HFCPCI_B1_RSL, 0x80);
  247. Write_hfc(hc, HFCPCI_B2_RSL, 0x81);
  248. }
  249. }
  250. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  251. val = Read_hfc(hc, HFCPCI_INT_S2);
  252. }
  253. /*
  254. * Timer function called when kernel timer expires
  255. */
  256. static void
  257. hfcpci_Timer(struct hfc_pci *hc)
  258. {
  259. hc->hw.timer.expires = jiffies + 75;
  260. /* WD RESET */
  261. /*
  262. * WriteReg(hc, HFCD_DATA, HFCD_CTMT, hc->hw.ctmt | 0x80);
  263. * add_timer(&hc->hw.timer);
  264. */
  265. }
  266. /*
  267. * select a b-channel entry matching and active
  268. */
  269. static struct bchannel *
  270. Sel_BCS(struct hfc_pci *hc, int channel)
  271. {
  272. if (test_bit(FLG_ACTIVE, &hc->bch[0].Flags) &&
  273. (hc->bch[0].nr & channel))
  274. return &hc->bch[0];
  275. else if (test_bit(FLG_ACTIVE, &hc->bch[1].Flags) &&
  276. (hc->bch[1].nr & channel))
  277. return &hc->bch[1];
  278. else
  279. return NULL;
  280. }
  281. /*
  282. * clear the desired B-channel rx fifo
  283. */
  284. static void
  285. hfcpci_clear_fifo_rx(struct hfc_pci *hc, int fifo)
  286. {
  287. u_char fifo_state;
  288. struct bzfifo *bzr;
  289. if (fifo) {
  290. bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;
  291. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2RX;
  292. } else {
  293. bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;
  294. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1RX;
  295. }
  296. if (fifo_state)
  297. hc->hw.fifo_en ^= fifo_state;
  298. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  299. hc->hw.last_bfifo_cnt[fifo] = 0;
  300. bzr->f1 = MAX_B_FRAMES;
  301. bzr->f2 = bzr->f1; /* init F pointers to remain constant */
  302. bzr->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);
  303. bzr->za[MAX_B_FRAMES].z2 = cpu_to_le16(
  304. le16_to_cpu(bzr->za[MAX_B_FRAMES].z1));
  305. if (fifo_state)
  306. hc->hw.fifo_en |= fifo_state;
  307. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  308. }
  309. /*
  310. * clear the desired B-channel tx fifo
  311. */
  312. static void hfcpci_clear_fifo_tx(struct hfc_pci *hc, int fifo)
  313. {
  314. u_char fifo_state;
  315. struct bzfifo *bzt;
  316. if (fifo) {
  317. bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
  318. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2TX;
  319. } else {
  320. bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
  321. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1TX;
  322. }
  323. if (fifo_state)
  324. hc->hw.fifo_en ^= fifo_state;
  325. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  326. if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)
  327. printk(KERN_DEBUG "hfcpci_clear_fifo_tx%d f1(%x) f2(%x) "
  328. "z1(%x) z2(%x) state(%x)\n",
  329. fifo, bzt->f1, bzt->f2,
  330. le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),
  331. le16_to_cpu(bzt->za[MAX_B_FRAMES].z2),
  332. fifo_state);
  333. bzt->f2 = MAX_B_FRAMES;
  334. bzt->f1 = bzt->f2; /* init F pointers to remain constant */
  335. bzt->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);
  336. bzt->za[MAX_B_FRAMES].z2 = cpu_to_le16(
  337. le16_to_cpu(bzt->za[MAX_B_FRAMES].z1 - 1));
  338. if (fifo_state)
  339. hc->hw.fifo_en |= fifo_state;
  340. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  341. if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)
  342. printk(KERN_DEBUG
  343. "hfcpci_clear_fifo_tx%d f1(%x) f2(%x) z1(%x) z2(%x)\n",
  344. fifo, bzt->f1, bzt->f2,
  345. le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),
  346. le16_to_cpu(bzt->za[MAX_B_FRAMES].z2));
  347. }
  348. /*
  349. * read a complete B-frame out of the buffer
  350. */
  351. static void
  352. hfcpci_empty_bfifo(struct bchannel *bch, struct bzfifo *bz,
  353. u_char *bdata, int count)
  354. {
  355. u_char *ptr, *ptr1, new_f2;
  356. int total, maxlen, new_z2;
  357. struct zt *zp;
  358. if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))
  359. printk(KERN_DEBUG "hfcpci_empty_fifo\n");
  360. zp = &bz->za[bz->f2]; /* point to Z-Regs */
  361. new_z2 = le16_to_cpu(zp->z2) + count; /* new position in fifo */
  362. if (new_z2 >= (B_FIFO_SIZE + B_SUB_VAL))
  363. new_z2 -= B_FIFO_SIZE; /* buffer wrap */
  364. new_f2 = (bz->f2 + 1) & MAX_B_FRAMES;
  365. if ((count > MAX_DATA_SIZE + 3) || (count < 4) ||
  366. (*(bdata + (le16_to_cpu(zp->z1) - B_SUB_VAL)))) {
  367. if (bch->debug & DEBUG_HW)
  368. printk(KERN_DEBUG "hfcpci_empty_fifo: incoming packet "
  369. "invalid length %d or crc\n", count);
  370. #ifdef ERROR_STATISTIC
  371. bch->err_inv++;
  372. #endif
  373. bz->za[new_f2].z2 = cpu_to_le16(new_z2);
  374. bz->f2 = new_f2; /* next buffer */
  375. } else {
  376. bch->rx_skb = mI_alloc_skb(count - 3, GFP_ATOMIC);
  377. if (!bch->rx_skb) {
  378. printk(KERN_WARNING "HFCPCI: receive out of memory\n");
  379. return;
  380. }
  381. total = count;
  382. count -= 3;
  383. ptr = skb_put(bch->rx_skb, count);
  384. if (le16_to_cpu(zp->z2) + count <= B_FIFO_SIZE + B_SUB_VAL)
  385. maxlen = count; /* complete transfer */
  386. else
  387. maxlen = B_FIFO_SIZE + B_SUB_VAL -
  388. le16_to_cpu(zp->z2); /* maximum */
  389. ptr1 = bdata + (le16_to_cpu(zp->z2) - B_SUB_VAL);
  390. /* start of data */
  391. memcpy(ptr, ptr1, maxlen); /* copy data */
  392. count -= maxlen;
  393. if (count) { /* rest remaining */
  394. ptr += maxlen;
  395. ptr1 = bdata; /* start of buffer */
  396. memcpy(ptr, ptr1, count); /* rest */
  397. }
  398. bz->za[new_f2].z2 = cpu_to_le16(new_z2);
  399. bz->f2 = new_f2; /* next buffer */
  400. recv_Bchannel(bch);
  401. }
  402. }
  403. /*
  404. * D-channel receive procedure
  405. */
  406. static int
  407. receive_dmsg(struct hfc_pci *hc)
  408. {
  409. struct dchannel *dch = &hc->dch;
  410. int maxlen;
  411. int rcnt, total;
  412. int count = 5;
  413. u_char *ptr, *ptr1;
  414. struct dfifo *df;
  415. struct zt *zp;
  416. df = &((union fifo_area *)(hc->hw.fifos))->d_chan.d_rx;
  417. while (((df->f1 & D_FREG_MASK) != (df->f2 & D_FREG_MASK)) && count--) {
  418. zp = &df->za[df->f2 & D_FREG_MASK];
  419. rcnt = le16_to_cpu(zp->z1) - le16_to_cpu(zp->z2);
  420. if (rcnt < 0)
  421. rcnt += D_FIFO_SIZE;
  422. rcnt++;
  423. if (dch->debug & DEBUG_HW_DCHANNEL)
  424. printk(KERN_DEBUG
  425. "hfcpci recd f1(%d) f2(%d) z1(%x) z2(%x) cnt(%d)\n",
  426. df->f1, df->f2,
  427. le16_to_cpu(zp->z1),
  428. le16_to_cpu(zp->z2),
  429. rcnt);
  430. if ((rcnt > MAX_DFRAME_LEN + 3) || (rcnt < 4) ||
  431. (df->data[le16_to_cpu(zp->z1)])) {
  432. if (dch->debug & DEBUG_HW)
  433. printk(KERN_DEBUG
  434. "empty_fifo hfcpci paket inv. len "
  435. "%d or crc %d\n",
  436. rcnt,
  437. df->data[le16_to_cpu(zp->z1)]);
  438. #ifdef ERROR_STATISTIC
  439. cs->err_rx++;
  440. #endif
  441. df->f2 = ((df->f2 + 1) & MAX_D_FRAMES) |
  442. (MAX_D_FRAMES + 1); /* next buffer */
  443. df->za[df->f2 & D_FREG_MASK].z2 =
  444. cpu_to_le16((zp->z2 + rcnt) & (D_FIFO_SIZE - 1));
  445. } else {
  446. dch->rx_skb = mI_alloc_skb(rcnt - 3, GFP_ATOMIC);
  447. if (!dch->rx_skb) {
  448. printk(KERN_WARNING
  449. "HFC-PCI: D receive out of memory\n");
  450. break;
  451. }
  452. total = rcnt;
  453. rcnt -= 3;
  454. ptr = skb_put(dch->rx_skb, rcnt);
  455. if (le16_to_cpu(zp->z2) + rcnt <= D_FIFO_SIZE)
  456. maxlen = rcnt; /* complete transfer */
  457. else
  458. maxlen = D_FIFO_SIZE - le16_to_cpu(zp->z2);
  459. /* maximum */
  460. ptr1 = df->data + le16_to_cpu(zp->z2);
  461. /* start of data */
  462. memcpy(ptr, ptr1, maxlen); /* copy data */
  463. rcnt -= maxlen;
  464. if (rcnt) { /* rest remaining */
  465. ptr += maxlen;
  466. ptr1 = df->data; /* start of buffer */
  467. memcpy(ptr, ptr1, rcnt); /* rest */
  468. }
  469. df->f2 = ((df->f2 + 1) & MAX_D_FRAMES) |
  470. (MAX_D_FRAMES + 1); /* next buffer */
  471. df->za[df->f2 & D_FREG_MASK].z2 = cpu_to_le16((
  472. le16_to_cpu(zp->z2) + total) & (D_FIFO_SIZE - 1));
  473. recv_Dchannel(dch);
  474. }
  475. }
  476. return 1;
  477. }
  478. /*
  479. * check for transparent receive data and read max one threshold size if avail
  480. */
  481. int
  482. hfcpci_empty_fifo_trans(struct bchannel *bch, struct bzfifo *bz, u_char *bdata)
  483. {
  484. unsigned short *z1r, *z2r;
  485. int new_z2, fcnt, maxlen;
  486. u_char *ptr, *ptr1;
  487. z1r = &bz->za[MAX_B_FRAMES].z1; /* pointer to z reg */
  488. z2r = z1r + 1;
  489. fcnt = le16_to_cpu(*z1r) - le16_to_cpu(*z2r);
  490. if (!fcnt)
  491. return 0; /* no data avail */
  492. if (fcnt <= 0)
  493. fcnt += B_FIFO_SIZE; /* bytes actually buffered */
  494. if (fcnt > HFCPCI_BTRANS_THRESHOLD)
  495. fcnt = HFCPCI_BTRANS_THRESHOLD; /* limit size */
  496. new_z2 = le16_to_cpu(*z2r) + fcnt; /* new position in fifo */
  497. if (new_z2 >= (B_FIFO_SIZE + B_SUB_VAL))
  498. new_z2 -= B_FIFO_SIZE; /* buffer wrap */
  499. bch->rx_skb = mI_alloc_skb(fcnt, GFP_ATOMIC);
  500. if (bch->rx_skb) {
  501. ptr = skb_put(bch->rx_skb, fcnt);
  502. if (le16_to_cpu(*z2r) + fcnt <= B_FIFO_SIZE + B_SUB_VAL)
  503. maxlen = fcnt; /* complete transfer */
  504. else
  505. maxlen = B_FIFO_SIZE + B_SUB_VAL - le16_to_cpu(*z2r);
  506. /* maximum */
  507. ptr1 = bdata + (le16_to_cpu(*z2r) - B_SUB_VAL);
  508. /* start of data */
  509. memcpy(ptr, ptr1, maxlen); /* copy data */
  510. fcnt -= maxlen;
  511. if (fcnt) { /* rest remaining */
  512. ptr += maxlen;
  513. ptr1 = bdata; /* start of buffer */
  514. memcpy(ptr, ptr1, fcnt); /* rest */
  515. }
  516. recv_Bchannel(bch);
  517. } else
  518. printk(KERN_WARNING "HFCPCI: receive out of memory\n");
  519. *z2r = cpu_to_le16(new_z2); /* new position */
  520. return 1;
  521. }
  522. /*
  523. * B-channel main receive routine
  524. */
  525. void
  526. main_rec_hfcpci(struct bchannel *bch)
  527. {
  528. struct hfc_pci *hc = bch->hw;
  529. int rcnt, real_fifo;
  530. int receive, count = 5;
  531. struct bzfifo *bz;
  532. u_char *bdata;
  533. struct zt *zp;
  534. if ((bch->nr & 2) && (!hc->hw.bswapped)) {
  535. bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;
  536. bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b2;
  537. real_fifo = 1;
  538. } else {
  539. bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;
  540. bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b1;
  541. real_fifo = 0;
  542. }
  543. Begin:
  544. count--;
  545. if (bz->f1 != bz->f2) {
  546. if (bch->debug & DEBUG_HW_BCHANNEL)
  547. printk(KERN_DEBUG "hfcpci rec ch(%x) f1(%d) f2(%d)\n",
  548. bch->nr, bz->f1, bz->f2);
  549. zp = &bz->za[bz->f2];
  550. rcnt = le16_to_cpu(zp->z1) - le16_to_cpu(zp->z2);
  551. if (rcnt < 0)
  552. rcnt += B_FIFO_SIZE;
  553. rcnt++;
  554. if (bch->debug & DEBUG_HW_BCHANNEL)
  555. printk(KERN_DEBUG
  556. "hfcpci rec ch(%x) z1(%x) z2(%x) cnt(%d)\n",
  557. bch->nr, le16_to_cpu(zp->z1),
  558. le16_to_cpu(zp->z2), rcnt);
  559. hfcpci_empty_bfifo(bch, bz, bdata, rcnt);
  560. rcnt = bz->f1 - bz->f2;
  561. if (rcnt < 0)
  562. rcnt += MAX_B_FRAMES + 1;
  563. if (hc->hw.last_bfifo_cnt[real_fifo] > rcnt + 1) {
  564. rcnt = 0;
  565. hfcpci_clear_fifo_rx(hc, real_fifo);
  566. }
  567. hc->hw.last_bfifo_cnt[real_fifo] = rcnt;
  568. if (rcnt > 1)
  569. receive = 1;
  570. else
  571. receive = 0;
  572. } else if (test_bit(FLG_TRANSPARENT, &bch->Flags))
  573. receive = hfcpci_empty_fifo_trans(bch, bz, bdata);
  574. else
  575. receive = 0;
  576. if (count && receive)
  577. goto Begin;
  578. }
  579. /*
  580. * D-channel send routine
  581. */
  582. static void
  583. hfcpci_fill_dfifo(struct hfc_pci *hc)
  584. {
  585. struct dchannel *dch = &hc->dch;
  586. int fcnt;
  587. int count, new_z1, maxlen;
  588. struct dfifo *df;
  589. u_char *src, *dst, new_f1;
  590. if ((dch->debug & DEBUG_HW_DCHANNEL) && !(dch->debug & DEBUG_HW_DFIFO))
  591. printk(KERN_DEBUG "%s\n", __func__);
  592. if (!dch->tx_skb)
  593. return;
  594. count = dch->tx_skb->len - dch->tx_idx;
  595. if (count <= 0)
  596. return;
  597. df = &((union fifo_area *) (hc->hw.fifos))->d_chan.d_tx;
  598. if (dch->debug & DEBUG_HW_DFIFO)
  599. printk(KERN_DEBUG "%s:f1(%d) f2(%d) z1(f1)(%x)\n", __func__,
  600. df->f1, df->f2,
  601. le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1));
  602. fcnt = df->f1 - df->f2; /* frame count actually buffered */
  603. if (fcnt < 0)
  604. fcnt += (MAX_D_FRAMES + 1); /* if wrap around */
  605. if (fcnt > (MAX_D_FRAMES - 1)) {
  606. if (dch->debug & DEBUG_HW_DCHANNEL)
  607. printk(KERN_DEBUG
  608. "hfcpci_fill_Dfifo more as 14 frames\n");
  609. #ifdef ERROR_STATISTIC
  610. cs->err_tx++;
  611. #endif
  612. return;
  613. }
  614. /* now determine free bytes in FIFO buffer */
  615. maxlen = le16_to_cpu(df->za[df->f2 & D_FREG_MASK].z2) -
  616. le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) - 1;
  617. if (maxlen <= 0)
  618. maxlen += D_FIFO_SIZE; /* count now contains available bytes */
  619. if (dch->debug & DEBUG_HW_DCHANNEL)
  620. printk(KERN_DEBUG "hfcpci_fill_Dfifo count(%d/%d)\n",
  621. count, maxlen);
  622. if (count > maxlen) {
  623. if (dch->debug & DEBUG_HW_DCHANNEL)
  624. printk(KERN_DEBUG "hfcpci_fill_Dfifo no fifo mem\n");
  625. return;
  626. }
  627. new_z1 = (le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) + count) &
  628. (D_FIFO_SIZE - 1);
  629. new_f1 = ((df->f1 + 1) & D_FREG_MASK) | (D_FREG_MASK + 1);
  630. src = dch->tx_skb->data + dch->tx_idx; /* source pointer */
  631. dst = df->data + le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);
  632. maxlen = D_FIFO_SIZE - le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);
  633. /* end fifo */
  634. if (maxlen > count)
  635. maxlen = count; /* limit size */
  636. memcpy(dst, src, maxlen); /* first copy */
  637. count -= maxlen; /* remaining bytes */
  638. if (count) {
  639. dst = df->data; /* start of buffer */
  640. src += maxlen; /* new position */
  641. memcpy(dst, src, count);
  642. }
  643. df->za[new_f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);
  644. /* for next buffer */
  645. df->za[df->f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);
  646. /* new pos actual buffer */
  647. df->f1 = new_f1; /* next frame */
  648. dch->tx_idx = dch->tx_skb->len;
  649. }
  650. /*
  651. * B-channel send routine
  652. */
  653. static void
  654. hfcpci_fill_fifo(struct bchannel *bch)
  655. {
  656. struct hfc_pci *hc = bch->hw;
  657. int maxlen, fcnt;
  658. int count, new_z1;
  659. struct bzfifo *bz;
  660. u_char *bdata;
  661. u_char new_f1, *src, *dst;
  662. unsigned short *z1t, *z2t;
  663. if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))
  664. printk(KERN_DEBUG "%s\n", __func__);
  665. if ((!bch->tx_skb) || bch->tx_skb->len <= 0)
  666. return;
  667. count = bch->tx_skb->len - bch->tx_idx;
  668. if ((bch->nr & 2) && (!hc->hw.bswapped)) {
  669. bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
  670. bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b2;
  671. } else {
  672. bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
  673. bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b1;
  674. }
  675. if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
  676. z1t = &bz->za[MAX_B_FRAMES].z1;
  677. z2t = z1t + 1;
  678. if (bch->debug & DEBUG_HW_BCHANNEL)
  679. printk(KERN_DEBUG "hfcpci_fill_fifo_trans ch(%x) "
  680. "cnt(%d) z1(%x) z2(%x)\n", bch->nr, count,
  681. le16_to_cpu(*z1t), le16_to_cpu(*z2t));
  682. fcnt = le16_to_cpu(*z2t) - le16_to_cpu(*z1t);
  683. if (fcnt <= 0)
  684. fcnt += B_FIFO_SIZE;
  685. /* fcnt contains available bytes in fifo */
  686. fcnt = B_FIFO_SIZE - fcnt;
  687. /* remaining bytes to send (bytes in fifo) */
  688. next_t_frame:
  689. count = bch->tx_skb->len - bch->tx_idx;
  690. /* maximum fill shall be HFCPCI_BTRANS_MAX */
  691. if (count > HFCPCI_BTRANS_MAX - fcnt)
  692. count = HFCPCI_BTRANS_MAX - fcnt;
  693. if (count <= 0)
  694. return;
  695. /* data is suitable for fifo */
  696. new_z1 = le16_to_cpu(*z1t) + count;
  697. /* new buffer Position */
  698. if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
  699. new_z1 -= B_FIFO_SIZE; /* buffer wrap */
  700. src = bch->tx_skb->data + bch->tx_idx;
  701. /* source pointer */
  702. dst = bdata + (le16_to_cpu(*z1t) - B_SUB_VAL);
  703. maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(*z1t);
  704. /* end of fifo */
  705. if (bch->debug & DEBUG_HW_BFIFO)
  706. printk(KERN_DEBUG "hfcpci_FFt fcnt(%d) "
  707. "maxl(%d) nz1(%x) dst(%p)\n",
  708. fcnt, maxlen, new_z1, dst);
  709. fcnt += count;
  710. bch->tx_idx += count;
  711. if (maxlen > count)
  712. maxlen = count; /* limit size */
  713. memcpy(dst, src, maxlen); /* first copy */
  714. count -= maxlen; /* remaining bytes */
  715. if (count) {
  716. dst = bdata; /* start of buffer */
  717. src += maxlen; /* new position */
  718. memcpy(dst, src, count);
  719. }
  720. *z1t = cpu_to_le16(new_z1); /* now send data */
  721. if (bch->tx_idx < bch->tx_skb->len)
  722. return;
  723. /* send confirm, on trans, free on hdlc. */
  724. if (test_bit(FLG_TRANSPARENT, &bch->Flags))
  725. confirm_Bsend(bch);
  726. dev_kfree_skb(bch->tx_skb);
  727. if (get_next_bframe(bch))
  728. goto next_t_frame;
  729. return;
  730. }
  731. if (bch->debug & DEBUG_HW_BCHANNEL)
  732. printk(KERN_DEBUG
  733. "%s: ch(%x) f1(%d) f2(%d) z1(f1)(%x)\n",
  734. __func__, bch->nr, bz->f1, bz->f2,
  735. bz->za[bz->f1].z1);
  736. fcnt = bz->f1 - bz->f2; /* frame count actually buffered */
  737. if (fcnt < 0)
  738. fcnt += (MAX_B_FRAMES + 1); /* if wrap around */
  739. if (fcnt > (MAX_B_FRAMES - 1)) {
  740. if (bch->debug & DEBUG_HW_BCHANNEL)
  741. printk(KERN_DEBUG
  742. "hfcpci_fill_Bfifo more as 14 frames\n");
  743. return;
  744. }
  745. /* now determine free bytes in FIFO buffer */
  746. maxlen = le16_to_cpu(bz->za[bz->f2].z2) -
  747. le16_to_cpu(bz->za[bz->f1].z1) - 1;
  748. if (maxlen <= 0)
  749. maxlen += B_FIFO_SIZE; /* count now contains available bytes */
  750. if (bch->debug & DEBUG_HW_BCHANNEL)
  751. printk(KERN_DEBUG "hfcpci_fill_fifo ch(%x) count(%d/%d)\n",
  752. bch->nr, count, maxlen);
  753. if (maxlen < count) {
  754. if (bch->debug & DEBUG_HW_BCHANNEL)
  755. printk(KERN_DEBUG "hfcpci_fill_fifo no fifo mem\n");
  756. return;
  757. }
  758. new_z1 = le16_to_cpu(bz->za[bz->f1].z1) + count;
  759. /* new buffer Position */
  760. if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
  761. new_z1 -= B_FIFO_SIZE; /* buffer wrap */
  762. new_f1 = ((bz->f1 + 1) & MAX_B_FRAMES);
  763. src = bch->tx_skb->data + bch->tx_idx; /* source pointer */
  764. dst = bdata + (le16_to_cpu(bz->za[bz->f1].z1) - B_SUB_VAL);
  765. maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(bz->za[bz->f1].z1);
  766. /* end fifo */
  767. if (maxlen > count)
  768. maxlen = count; /* limit size */
  769. memcpy(dst, src, maxlen); /* first copy */
  770. count -= maxlen; /* remaining bytes */
  771. if (count) {
  772. dst = bdata; /* start of buffer */
  773. src += maxlen; /* new position */
  774. memcpy(dst, src, count);
  775. }
  776. bz->za[new_f1].z1 = cpu_to_le16(new_z1); /* for next buffer */
  777. bz->f1 = new_f1; /* next frame */
  778. dev_kfree_skb(bch->tx_skb);
  779. get_next_bframe(bch);
  780. }
  781. /*
  782. * handle L1 state changes TE
  783. */
  784. static void
  785. ph_state_te(struct dchannel *dch)
  786. {
  787. if (dch->debug)
  788. printk(KERN_DEBUG "%s: TE newstate %x\n",
  789. __func__, dch->state);
  790. switch (dch->state) {
  791. case 0:
  792. l1_event(dch->l1, HW_RESET_IND);
  793. break;
  794. case 3:
  795. l1_event(dch->l1, HW_DEACT_IND);
  796. break;
  797. case 5:
  798. case 8:
  799. l1_event(dch->l1, ANYSIGNAL);
  800. break;
  801. case 6:
  802. l1_event(dch->l1, INFO2);
  803. break;
  804. case 7:
  805. l1_event(dch->l1, INFO4_P8);
  806. break;
  807. }
  808. }
  809. /*
  810. * handle L1 state changes NT
  811. */
  812. static void
  813. handle_nt_timer3(struct dchannel *dch) {
  814. struct hfc_pci *hc = dch->hw;
  815. test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
  816. hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
  817. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  818. hc->hw.nt_timer = 0;
  819. test_and_set_bit(FLG_ACTIVE, &dch->Flags);
  820. if (test_bit(HFC_CFG_MASTER, &hc->cfg))
  821. hc->hw.mst_m |= HFCPCI_MASTER;
  822. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  823. _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
  824. MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
  825. }
  826. static void
  827. ph_state_nt(struct dchannel *dch)
  828. {
  829. struct hfc_pci *hc = dch->hw;
  830. if (dch->debug)
  831. printk(KERN_DEBUG "%s: NT newstate %x\n",
  832. __func__, dch->state);
  833. switch (dch->state) {
  834. case 2:
  835. if (hc->hw.nt_timer < 0) {
  836. hc->hw.nt_timer = 0;
  837. test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
  838. test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
  839. hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
  840. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  841. /* Clear already pending ints */
  842. if (Read_hfc(hc, HFCPCI_INT_S1));
  843. Write_hfc(hc, HFCPCI_STATES, 4 | HFCPCI_LOAD_STATE);
  844. udelay(10);
  845. Write_hfc(hc, HFCPCI_STATES, 4);
  846. dch->state = 4;
  847. } else if (hc->hw.nt_timer == 0) {
  848. hc->hw.int_m1 |= HFCPCI_INTS_TIMER;
  849. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  850. hc->hw.nt_timer = NT_T1_COUNT;
  851. hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;
  852. hc->hw.ctmt |= HFCPCI_TIM3_125;
  853. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |
  854. HFCPCI_CLTIMER);
  855. test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
  856. test_and_set_bit(FLG_HFC_TIMER_T1, &dch->Flags);
  857. /* allow G2 -> G3 transition */
  858. Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);
  859. } else {
  860. Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);
  861. }
  862. break;
  863. case 1:
  864. hc->hw.nt_timer = 0;
  865. test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
  866. test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
  867. hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
  868. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  869. test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
  870. hc->hw.mst_m &= ~HFCPCI_MASTER;
  871. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  872. test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
  873. _queue_data(&dch->dev.D, PH_DEACTIVATE_IND,
  874. MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
  875. break;
  876. case 4:
  877. hc->hw.nt_timer = 0;
  878. test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
  879. test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
  880. hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
  881. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  882. break;
  883. case 3:
  884. if (!test_and_set_bit(FLG_HFC_TIMER_T3, &dch->Flags)) {
  885. if (!test_and_clear_bit(FLG_L2_ACTIVATED,
  886. &dch->Flags)) {
  887. handle_nt_timer3(dch);
  888. break;
  889. }
  890. test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
  891. hc->hw.int_m1 |= HFCPCI_INTS_TIMER;
  892. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  893. hc->hw.nt_timer = NT_T3_COUNT;
  894. hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;
  895. hc->hw.ctmt |= HFCPCI_TIM3_125;
  896. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |
  897. HFCPCI_CLTIMER);
  898. }
  899. break;
  900. }
  901. }
  902. static void
  903. ph_state(struct dchannel *dch)
  904. {
  905. struct hfc_pci *hc = dch->hw;
  906. if (hc->hw.protocol == ISDN_P_NT_S0) {
  907. if (test_bit(FLG_HFC_TIMER_T3, &dch->Flags) &&
  908. hc->hw.nt_timer < 0)
  909. handle_nt_timer3(dch);
  910. else
  911. ph_state_nt(dch);
  912. } else
  913. ph_state_te(dch);
  914. }
  915. /*
  916. * Layer 1 callback function
  917. */
  918. static int
  919. hfc_l1callback(struct dchannel *dch, u_int cmd)
  920. {
  921. struct hfc_pci *hc = dch->hw;
  922. switch (cmd) {
  923. case INFO3_P8:
  924. case INFO3_P10:
  925. if (test_bit(HFC_CFG_MASTER, &hc->cfg))
  926. hc->hw.mst_m |= HFCPCI_MASTER;
  927. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  928. break;
  929. case HW_RESET_REQ:
  930. Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | 3);
  931. /* HFC ST 3 */
  932. udelay(6);
  933. Write_hfc(hc, HFCPCI_STATES, 3); /* HFC ST 2 */
  934. if (test_bit(HFC_CFG_MASTER, &hc->cfg))
  935. hc->hw.mst_m |= HFCPCI_MASTER;
  936. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  937. Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |
  938. HFCPCI_DO_ACTION);
  939. l1_event(dch->l1, HW_POWERUP_IND);
  940. break;
  941. case HW_DEACT_REQ:
  942. hc->hw.mst_m &= ~HFCPCI_MASTER;
  943. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  944. skb_queue_purge(&dch->squeue);
  945. if (dch->tx_skb) {
  946. dev_kfree_skb(dch->tx_skb);
  947. dch->tx_skb = NULL;
  948. }
  949. dch->tx_idx = 0;
  950. if (dch->rx_skb) {
  951. dev_kfree_skb(dch->rx_skb);
  952. dch->rx_skb = NULL;
  953. }
  954. test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
  955. if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
  956. del_timer(&dch->timer);
  957. break;
  958. case HW_POWERUP_REQ:
  959. Write_hfc(hc, HFCPCI_STATES, HFCPCI_DO_ACTION);
  960. break;
  961. case PH_ACTIVATE_IND:
  962. test_and_set_bit(FLG_ACTIVE, &dch->Flags);
  963. _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
  964. GFP_ATOMIC);
  965. break;
  966. case PH_DEACTIVATE_IND:
  967. test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
  968. _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
  969. GFP_ATOMIC);
  970. break;
  971. default:
  972. if (dch->debug & DEBUG_HW)
  973. printk(KERN_DEBUG "%s: unknown command %x\n",
  974. __func__, cmd);
  975. return -1;
  976. }
  977. return 0;
  978. }
  979. /*
  980. * Interrupt handler
  981. */
  982. static inline void
  983. tx_birq(struct bchannel *bch)
  984. {
  985. if (bch->tx_skb && bch->tx_idx < bch->tx_skb->len)
  986. hfcpci_fill_fifo(bch);
  987. else {
  988. if (bch->tx_skb)
  989. dev_kfree_skb(bch->tx_skb);
  990. if (get_next_bframe(bch))
  991. hfcpci_fill_fifo(bch);
  992. }
  993. }
  994. static inline void
  995. tx_dirq(struct dchannel *dch)
  996. {
  997. if (dch->tx_skb && dch->tx_idx < dch->tx_skb->len)
  998. hfcpci_fill_dfifo(dch->hw);
  999. else {
  1000. if (dch->tx_skb)
  1001. dev_kfree_skb(dch->tx_skb);
  1002. if (get_next_dframe(dch))
  1003. hfcpci_fill_dfifo(dch->hw);
  1004. }
  1005. }
  1006. static irqreturn_t
  1007. hfcpci_int(int intno, void *dev_id)
  1008. {
  1009. struct hfc_pci *hc = dev_id;
  1010. u_char exval;
  1011. struct bchannel *bch;
  1012. u_char val, stat;
  1013. spin_lock(&hc->lock);
  1014. if (!(hc->hw.int_m2 & 0x08)) {
  1015. spin_unlock(&hc->lock);
  1016. return IRQ_NONE; /* not initialised */
  1017. }
  1018. stat = Read_hfc(hc, HFCPCI_STATUS);
  1019. if (HFCPCI_ANYINT & stat) {
  1020. val = Read_hfc(hc, HFCPCI_INT_S1);
  1021. if (hc->dch.debug & DEBUG_HW_DCHANNEL)
  1022. printk(KERN_DEBUG
  1023. "HFC-PCI: stat(%02x) s1(%02x)\n", stat, val);
  1024. } else {
  1025. /* shared */
  1026. spin_unlock(&hc->lock);
  1027. return IRQ_NONE;
  1028. }
  1029. hc->irqcnt++;
  1030. if (hc->dch.debug & DEBUG_HW_DCHANNEL)
  1031. printk(KERN_DEBUG "HFC-PCI irq %x\n", val);
  1032. val &= hc->hw.int_m1;
  1033. if (val & 0x40) { /* state machine irq */
  1034. exval = Read_hfc(hc, HFCPCI_STATES) & 0xf;
  1035. if (hc->dch.debug & DEBUG_HW_DCHANNEL)
  1036. printk(KERN_DEBUG "ph_state chg %d->%d\n",
  1037. hc->dch.state, exval);
  1038. hc->dch.state = exval;
  1039. schedule_event(&hc->dch, FLG_PHCHANGE);
  1040. val &= ~0x40;
  1041. }
  1042. if (val & 0x80) { /* timer irq */
  1043. if (hc->hw.protocol == ISDN_P_NT_S0) {
  1044. if ((--hc->hw.nt_timer) < 0)
  1045. schedule_event(&hc->dch, FLG_PHCHANGE);
  1046. }
  1047. val &= ~0x80;
  1048. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt | HFCPCI_CLTIMER);
  1049. }
  1050. if (val & 0x08) {
  1051. bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
  1052. if (bch)
  1053. main_rec_hfcpci(bch);
  1054. else if (hc->dch.debug)
  1055. printk(KERN_DEBUG "hfcpci spurious 0x08 IRQ\n");
  1056. }
  1057. if (val & 0x10) {
  1058. bch = Sel_BCS(hc, 2);
  1059. if (bch)
  1060. main_rec_hfcpci(bch);
  1061. else if (hc->dch.debug)
  1062. printk(KERN_DEBUG "hfcpci spurious 0x10 IRQ\n");
  1063. }
  1064. if (val & 0x01) {
  1065. bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
  1066. if (bch)
  1067. tx_birq(bch);
  1068. else if (hc->dch.debug)
  1069. printk(KERN_DEBUG "hfcpci spurious 0x01 IRQ\n");
  1070. }
  1071. if (val & 0x02) {
  1072. bch = Sel_BCS(hc, 2);
  1073. if (bch)
  1074. tx_birq(bch);
  1075. else if (hc->dch.debug)
  1076. printk(KERN_DEBUG "hfcpci spurious 0x02 IRQ\n");
  1077. }
  1078. if (val & 0x20)
  1079. receive_dmsg(hc);
  1080. if (val & 0x04) { /* dframe transmitted */
  1081. if (test_and_clear_bit(FLG_BUSY_TIMER, &hc->dch.Flags))
  1082. del_timer(&hc->dch.timer);
  1083. tx_dirq(&hc->dch);
  1084. }
  1085. spin_unlock(&hc->lock);
  1086. return IRQ_HANDLED;
  1087. }
  1088. /*
  1089. * timer callback for D-chan busy resolution. Currently no function
  1090. */
  1091. static void
  1092. hfcpci_dbusy_timer(struct hfc_pci *hc)
  1093. {
  1094. }
  1095. /*
  1096. * activate/deactivate hardware for selected channels and mode
  1097. */
  1098. static int
  1099. mode_hfcpci(struct bchannel *bch, int bc, int protocol)
  1100. {
  1101. struct hfc_pci *hc = bch->hw;
  1102. int fifo2;
  1103. u_char rx_slot = 0, tx_slot = 0, pcm_mode;
  1104. if (bch->debug & DEBUG_HW_BCHANNEL)
  1105. printk(KERN_DEBUG
  1106. "HFCPCI bchannel protocol %x-->%x ch %x-->%x\n",
  1107. bch->state, protocol, bch->nr, bc);
  1108. fifo2 = bc;
  1109. pcm_mode = (bc>>24) & 0xff;
  1110. if (pcm_mode) { /* PCM SLOT USE */
  1111. if (!test_bit(HFC_CFG_PCM, &hc->cfg))
  1112. printk(KERN_WARNING
  1113. "%s: pcm channel id without HFC_CFG_PCM\n",
  1114. __func__);
  1115. rx_slot = (bc>>8) & 0xff;
  1116. tx_slot = (bc>>16) & 0xff;
  1117. bc = bc & 0xff;
  1118. } else if (test_bit(HFC_CFG_PCM, &hc->cfg) &&
  1119. (protocol > ISDN_P_NONE))
  1120. printk(KERN_WARNING "%s: no pcm channel id but HFC_CFG_PCM\n",
  1121. __func__);
  1122. if (hc->chanlimit > 1) {
  1123. hc->hw.bswapped = 0; /* B1 and B2 normal mode */
  1124. hc->hw.sctrl_e &= ~0x80;
  1125. } else {
  1126. if (bc & 2) {
  1127. if (protocol != ISDN_P_NONE) {
  1128. hc->hw.bswapped = 1; /* B1 and B2 exchanged */
  1129. hc->hw.sctrl_e |= 0x80;
  1130. } else {
  1131. hc->hw.bswapped = 0; /* B1 and B2 normal mode */
  1132. hc->hw.sctrl_e &= ~0x80;
  1133. }
  1134. fifo2 = 1;
  1135. } else {
  1136. hc->hw.bswapped = 0; /* B1 and B2 normal mode */
  1137. hc->hw.sctrl_e &= ~0x80;
  1138. }
  1139. }
  1140. switch (protocol) {
  1141. case (-1): /* used for init */
  1142. bch->state = -1;
  1143. bch->nr = bc;
  1144. case (ISDN_P_NONE):
  1145. if (bch->state == ISDN_P_NONE)
  1146. return 0;
  1147. if (bc & 2) {
  1148. hc->hw.sctrl &= ~SCTRL_B2_ENA;
  1149. hc->hw.sctrl_r &= ~SCTRL_B2_ENA;
  1150. } else {
  1151. hc->hw.sctrl &= ~SCTRL_B1_ENA;
  1152. hc->hw.sctrl_r &= ~SCTRL_B1_ENA;
  1153. }
  1154. if (fifo2 & 2) {
  1155. hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B2;
  1156. hc->hw.int_m1 &= ~(HFCPCI_INTS_B2TRANS +
  1157. HFCPCI_INTS_B2REC);
  1158. } else {
  1159. hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B1;
  1160. hc->hw.int_m1 &= ~(HFCPCI_INTS_B1TRANS +
  1161. HFCPCI_INTS_B1REC);
  1162. }
  1163. #ifdef REVERSE_BITORDER
  1164. if (bch->nr & 2)
  1165. hc->hw.cirm &= 0x7f;
  1166. else
  1167. hc->hw.cirm &= 0xbf;
  1168. #endif
  1169. bch->state = ISDN_P_NONE;
  1170. bch->nr = bc;
  1171. test_and_clear_bit(FLG_HDLC, &bch->Flags);
  1172. test_and_clear_bit(FLG_TRANSPARENT, &bch->Flags);
  1173. break;
  1174. case (ISDN_P_B_RAW):
  1175. bch->state = protocol;
  1176. bch->nr = bc;
  1177. hfcpci_clear_fifo_rx(hc, (fifo2 & 2)?1:0);
  1178. hfcpci_clear_fifo_tx(hc, (fifo2 & 2)?1:0);
  1179. if (bc & 2) {
  1180. hc->hw.sctrl |= SCTRL_B2_ENA;
  1181. hc->hw.sctrl_r |= SCTRL_B2_ENA;
  1182. #ifdef REVERSE_BITORDER
  1183. hc->hw.cirm |= 0x80;
  1184. #endif
  1185. } else {
  1186. hc->hw.sctrl |= SCTRL_B1_ENA;
  1187. hc->hw.sctrl_r |= SCTRL_B1_ENA;
  1188. #ifdef REVERSE_BITORDER
  1189. hc->hw.cirm |= 0x40;
  1190. #endif
  1191. }
  1192. if (fifo2 & 2) {
  1193. hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;
  1194. hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +
  1195. HFCPCI_INTS_B2REC);
  1196. hc->hw.ctmt |= 2;
  1197. hc->hw.conn &= ~0x18;
  1198. } else {
  1199. hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;
  1200. hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +
  1201. HFCPCI_INTS_B1REC);
  1202. hc->hw.ctmt |= 1;
  1203. hc->hw.conn &= ~0x03;
  1204. }
  1205. test_and_set_bit(FLG_TRANSPARENT, &bch->Flags);
  1206. break;
  1207. case (ISDN_P_B_HDLC):
  1208. bch->state = protocol;
  1209. bch->nr = bc;
  1210. hfcpci_clear_fifo_rx(hc, (fifo2 & 2)?1:0);
  1211. hfcpci_clear_fifo_tx(hc, (fifo2 & 2)?1:0);
  1212. if (bc & 2) {
  1213. hc->hw.sctrl |= SCTRL_B2_ENA;
  1214. hc->hw.sctrl_r |= SCTRL_B2_ENA;
  1215. } else {
  1216. hc->hw.sctrl |= SCTRL_B1_ENA;
  1217. hc->hw.sctrl_r |= SCTRL_B1_ENA;
  1218. }
  1219. if (fifo2 & 2) {
  1220. hc->hw.last_bfifo_cnt[1] = 0;
  1221. hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;
  1222. hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +
  1223. HFCPCI_INTS_B2REC);
  1224. hc->hw.ctmt &= ~2;
  1225. hc->hw.conn &= ~0x18;
  1226. } else {
  1227. hc->hw.last_bfifo_cnt[0] = 0;
  1228. hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;
  1229. hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +
  1230. HFCPCI_INTS_B1REC);
  1231. hc->hw.ctmt &= ~1;
  1232. hc->hw.conn &= ~0x03;
  1233. }
  1234. test_and_set_bit(FLG_HDLC, &bch->Flags);
  1235. break;
  1236. default:
  1237. printk(KERN_DEBUG "prot not known %x\n", protocol);
  1238. return -ENOPROTOOPT;
  1239. }
  1240. if (test_bit(HFC_CFG_PCM, &hc->cfg)) {
  1241. if ((protocol == ISDN_P_NONE) ||
  1242. (protocol == -1)) { /* init case */
  1243. rx_slot = 0;
  1244. tx_slot = 0;
  1245. } else {
  1246. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {
  1247. rx_slot |= 0xC0;
  1248. tx_slot |= 0xC0;
  1249. } else {
  1250. rx_slot |= 0x80;
  1251. tx_slot |= 0x80;
  1252. }
  1253. }
  1254. if (bc & 2) {
  1255. hc->hw.conn &= 0xc7;
  1256. hc->hw.conn |= 0x08;
  1257. printk(KERN_DEBUG "%s: Write_hfc: B2_SSL 0x%x\n",
  1258. __func__, tx_slot);
  1259. printk(KERN_DEBUG "%s: Write_hfc: B2_RSL 0x%x\n",
  1260. __func__, rx_slot);
  1261. Write_hfc(hc, HFCPCI_B2_SSL, tx_slot);
  1262. Write_hfc(hc, HFCPCI_B2_RSL, rx_slot);
  1263. } else {
  1264. hc->hw.conn &= 0xf8;
  1265. hc->hw.conn |= 0x01;
  1266. printk(KERN_DEBUG "%s: Write_hfc: B1_SSL 0x%x\n",
  1267. __func__, tx_slot);
  1268. printk(KERN_DEBUG "%s: Write_hfc: B1_RSL 0x%x\n",
  1269. __func__, rx_slot);
  1270. Write_hfc(hc, HFCPCI_B1_SSL, tx_slot);
  1271. Write_hfc(hc, HFCPCI_B1_RSL, rx_slot);
  1272. }
  1273. }
  1274. Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);
  1275. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  1276. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  1277. Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);
  1278. Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
  1279. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
  1280. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1281. #ifdef REVERSE_BITORDER
  1282. Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
  1283. #endif
  1284. return 0;
  1285. }
  1286. static int
  1287. set_hfcpci_rxtest(struct bchannel *bch, int protocol, int chan)
  1288. {
  1289. struct hfc_pci *hc = bch->hw;
  1290. if (bch->debug & DEBUG_HW_BCHANNEL)
  1291. printk(KERN_DEBUG
  1292. "HFCPCI bchannel test rx protocol %x-->%x ch %x-->%x\n",
  1293. bch->state, protocol, bch->nr, chan);
  1294. if (bch->nr != chan) {
  1295. printk(KERN_DEBUG
  1296. "HFCPCI rxtest wrong channel parameter %x/%x\n",
  1297. bch->nr, chan);
  1298. return -EINVAL;
  1299. }
  1300. switch (protocol) {
  1301. case (ISDN_P_B_RAW):
  1302. bch->state = protocol;
  1303. hfcpci_clear_fifo_rx(hc, (chan & 2)?1:0);
  1304. if (chan & 2) {
  1305. hc->hw.sctrl_r |= SCTRL_B2_ENA;
  1306. hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;
  1307. hc->hw.int_m1 |= HFCPCI_INTS_B2REC;
  1308. hc->hw.ctmt |= 2;
  1309. hc->hw.conn &= ~0x18;
  1310. #ifdef REVERSE_BITORDER
  1311. hc->hw.cirm |= 0x80;
  1312. #endif
  1313. } else {
  1314. hc->hw.sctrl_r |= SCTRL_B1_ENA;
  1315. hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;
  1316. hc->hw.int_m1 |= HFCPCI_INTS_B1REC;
  1317. hc->hw.ctmt |= 1;
  1318. hc->hw.conn &= ~0x03;
  1319. #ifdef REVERSE_BITORDER
  1320. hc->hw.cirm |= 0x40;
  1321. #endif
  1322. }
  1323. break;
  1324. case (ISDN_P_B_HDLC):
  1325. bch->state = protocol;
  1326. hfcpci_clear_fifo_rx(hc, (chan & 2)?1:0);
  1327. if (chan & 2) {
  1328. hc->hw.sctrl_r |= SCTRL_B2_ENA;
  1329. hc->hw.last_bfifo_cnt[1] = 0;
  1330. hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;
  1331. hc->hw.int_m1 |= HFCPCI_INTS_B2REC;
  1332. hc->hw.ctmt &= ~2;
  1333. hc->hw.conn &= ~0x18;
  1334. } else {
  1335. hc->hw.sctrl_r |= SCTRL_B1_ENA;
  1336. hc->hw.last_bfifo_cnt[0] = 0;
  1337. hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;
  1338. hc->hw.int_m1 |= HFCPCI_INTS_B1REC;
  1339. hc->hw.ctmt &= ~1;
  1340. hc->hw.conn &= ~0x03;
  1341. }
  1342. break;
  1343. default:
  1344. printk(KERN_DEBUG "prot not known %x\n", protocol);
  1345. return -ENOPROTOOPT;
  1346. }
  1347. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  1348. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  1349. Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
  1350. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
  1351. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1352. #ifdef REVERSE_BITORDER
  1353. Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
  1354. #endif
  1355. return 0;
  1356. }
  1357. static void
  1358. deactivate_bchannel(struct bchannel *bch)
  1359. {
  1360. struct hfc_pci *hc = bch->hw;
  1361. u_long flags;
  1362. spin_lock_irqsave(&hc->lock, flags);
  1363. if (test_and_clear_bit(FLG_TX_NEXT, &bch->Flags)) {
  1364. dev_kfree_skb(bch->next_skb);
  1365. bch->next_skb = NULL;
  1366. }
  1367. if (bch->tx_skb) {
  1368. dev_kfree_skb(bch->tx_skb);
  1369. bch->tx_skb = NULL;
  1370. }
  1371. bch->tx_idx = 0;
  1372. if (bch->rx_skb) {
  1373. dev_kfree_skb(bch->rx_skb);
  1374. bch->rx_skb = NULL;
  1375. }
  1376. mode_hfcpci(bch, bch->nr, ISDN_P_NONE);
  1377. test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
  1378. test_and_clear_bit(FLG_TX_BUSY, &bch->Flags);
  1379. spin_unlock_irqrestore(&hc->lock, flags);
  1380. }
  1381. /*
  1382. * Layer 1 B-channel hardware access
  1383. */
  1384. static int
  1385. channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
  1386. {
  1387. int ret = 0;
  1388. switch (cq->op) {
  1389. case MISDN_CTRL_GETOP:
  1390. cq->op = 0;
  1391. break;
  1392. default:
  1393. printk(KERN_WARNING "%s: unknown Op %x\n", __func__, cq->op);
  1394. ret = -EINVAL;
  1395. break;
  1396. }
  1397. return ret;
  1398. }
  1399. static int
  1400. hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1401. {
  1402. struct bchannel *bch = container_of(ch, struct bchannel, ch);
  1403. struct hfc_pci *hc = bch->hw;
  1404. int ret = -EINVAL;
  1405. u_long flags;
  1406. if (bch->debug & DEBUG_HW)
  1407. printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
  1408. switch (cmd) {
  1409. case HW_TESTRX_RAW:
  1410. spin_lock_irqsave(&hc->lock, flags);
  1411. ret = set_hfcpci_rxtest(bch, ISDN_P_B_RAW, (int)(long)arg);
  1412. spin_unlock_irqrestore(&hc->lock, flags);
  1413. break;
  1414. case HW_TESTRX_HDLC:
  1415. spin_lock_irqsave(&hc->lock, flags);
  1416. ret = set_hfcpci_rxtest(bch, ISDN_P_B_HDLC, (int)(long)arg);
  1417. spin_unlock_irqrestore(&hc->lock, flags);
  1418. break;
  1419. case HW_TESTRX_OFF:
  1420. spin_lock_irqsave(&hc->lock, flags);
  1421. mode_hfcpci(bch, bch->nr, ISDN_P_NONE);
  1422. spin_unlock_irqrestore(&hc->lock, flags);
  1423. ret = 0;
  1424. break;
  1425. case CLOSE_CHANNEL:
  1426. test_and_clear_bit(FLG_OPEN, &bch->Flags);
  1427. if (test_bit(FLG_ACTIVE, &bch->Flags))
  1428. deactivate_bchannel(bch);
  1429. ch->protocol = ISDN_P_NONE;
  1430. ch->peer = NULL;
  1431. module_put(THIS_MODULE);
  1432. ret = 0;
  1433. break;
  1434. case CONTROL_CHANNEL:
  1435. ret = channel_bctrl(bch, arg);
  1436. break;
  1437. default:
  1438. printk(KERN_WARNING "%s: unknown prim(%x)\n",
  1439. __func__, cmd);
  1440. }
  1441. return ret;
  1442. }
  1443. /*
  1444. * Layer2 -> Layer 1 Dchannel data
  1445. */
  1446. static int
  1447. hfcpci_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
  1448. {
  1449. struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
  1450. struct dchannel *dch = container_of(dev, struct dchannel, dev);
  1451. struct hfc_pci *hc = dch->hw;
  1452. int ret = -EINVAL;
  1453. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  1454. unsigned int id;
  1455. u_long flags;
  1456. switch (hh->prim) {
  1457. case PH_DATA_REQ:
  1458. spin_lock_irqsave(&hc->lock, flags);
  1459. ret = dchannel_senddata(dch, skb);
  1460. if (ret > 0) { /* direct TX */
  1461. id = hh->id; /* skb can be freed */
  1462. hfcpci_fill_dfifo(dch->hw);
  1463. ret = 0;
  1464. spin_unlock_irqrestore(&hc->lock, flags);
  1465. queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
  1466. } else
  1467. spin_unlock_irqrestore(&hc->lock, flags);
  1468. return ret;
  1469. case PH_ACTIVATE_REQ:
  1470. spin_lock_irqsave(&hc->lock, flags);
  1471. if (hc->hw.protocol == ISDN_P_NT_S0) {
  1472. ret = 0;
  1473. if (test_bit(HFC_CFG_MASTER, &hc->cfg))
  1474. hc->hw.mst_m |= HFCPCI_MASTER;
  1475. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  1476. if (test_bit(FLG_ACTIVE, &dch->Flags)) {
  1477. spin_unlock_irqrestore(&hc->lock, flags);
  1478. _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
  1479. MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
  1480. break;
  1481. }
  1482. test_and_set_bit(FLG_L2_ACTIVATED, &dch->Flags);
  1483. Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |
  1484. HFCPCI_DO_ACTION | 1);
  1485. } else
  1486. ret = l1_event(dch->l1, hh->prim);
  1487. spin_unlock_irqrestore(&hc->lock, flags);
  1488. break;
  1489. case PH_DEACTIVATE_REQ:
  1490. test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
  1491. spin_lock_irqsave(&hc->lock, flags);
  1492. if (hc->hw.protocol == ISDN_P_NT_S0) {
  1493. /* prepare deactivation */
  1494. Write_hfc(hc, HFCPCI_STATES, 0x40);
  1495. skb_queue_purge(&dch->squeue);
  1496. if (dch->tx_skb) {
  1497. dev_kfree_skb(dch->tx_skb);
  1498. dch->tx_skb = NULL;
  1499. }
  1500. dch->tx_idx = 0;
  1501. if (dch->rx_skb) {
  1502. dev_kfree_skb(dch->rx_skb);
  1503. dch->rx_skb = NULL;
  1504. }
  1505. test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
  1506. if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
  1507. del_timer(&dch->timer);
  1508. #ifdef FIXME
  1509. if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
  1510. dchannel_sched_event(&hc->dch, D_CLEARBUSY);
  1511. #endif
  1512. hc->hw.mst_m &= ~HFCPCI_MASTER;
  1513. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  1514. ret = 0;
  1515. } else {
  1516. ret = l1_event(dch->l1, hh->prim);
  1517. }
  1518. spin_unlock_irqrestore(&hc->lock, flags);
  1519. break;
  1520. }
  1521. if (!ret)
  1522. dev_kfree_skb(skb);
  1523. return ret;
  1524. }
  1525. /*
  1526. * Layer2 -> Layer 1 Bchannel data
  1527. */
  1528. static int
  1529. hfcpci_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
  1530. {
  1531. struct bchannel *bch = container_of(ch, struct bchannel, ch);
  1532. struct hfc_pci *hc = bch->hw;
  1533. int ret = -EINVAL;
  1534. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  1535. unsigned int id;
  1536. u_long flags;
  1537. switch (hh->prim) {
  1538. case PH_DATA_REQ:
  1539. spin_lock_irqsave(&hc->lock, flags);
  1540. ret = bchannel_senddata(bch, skb);
  1541. if (ret > 0) { /* direct TX */
  1542. id = hh->id; /* skb can be freed */
  1543. hfcpci_fill_fifo(bch);
  1544. ret = 0;
  1545. spin_unlock_irqrestore(&hc->lock, flags);
  1546. if (!test_bit(FLG_TRANSPARENT, &bch->Flags))
  1547. queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
  1548. } else
  1549. spin_unlock_irqrestore(&hc->lock, flags);
  1550. return ret;
  1551. case PH_ACTIVATE_REQ:
  1552. spin_lock_irqsave(&hc->lock, flags);
  1553. if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags))
  1554. ret = mode_hfcpci(bch, bch->nr, ch->protocol);
  1555. else
  1556. ret = 0;
  1557. spin_unlock_irqrestore(&hc->lock, flags);
  1558. if (!ret)
  1559. _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
  1560. NULL, GFP_KERNEL);
  1561. break;
  1562. case PH_DEACTIVATE_REQ:
  1563. deactivate_bchannel(bch);
  1564. _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
  1565. NULL, GFP_KERNEL);
  1566. ret = 0;
  1567. break;
  1568. }
  1569. if (!ret)
  1570. dev_kfree_skb(skb);
  1571. return ret;
  1572. }
  1573. /*
  1574. * called for card init message
  1575. */
  1576. void
  1577. inithfcpci(struct hfc_pci *hc)
  1578. {
  1579. printk(KERN_DEBUG "inithfcpci: entered\n");
  1580. hc->dch.timer.function = (void *) hfcpci_dbusy_timer;
  1581. hc->dch.timer.data = (long) &hc->dch;
  1582. init_timer(&hc->dch.timer);
  1583. hc->chanlimit = 2;
  1584. mode_hfcpci(&hc->bch[0], 1, -1);
  1585. mode_hfcpci(&hc->bch[1], 2, -1);
  1586. }
  1587. static int
  1588. init_card(struct hfc_pci *hc)
  1589. {
  1590. int cnt = 3;
  1591. u_long flags;
  1592. printk(KERN_DEBUG "init_card: entered\n");
  1593. spin_lock_irqsave(&hc->lock, flags);
  1594. disable_hwirq(hc);
  1595. spin_unlock_irqrestore(&hc->lock, flags);
  1596. if (request_irq(hc->irq, hfcpci_int, IRQF_SHARED, "HFC PCI", hc)) {
  1597. printk(KERN_WARNING
  1598. "mISDN: couldn't get interrupt %d\n", hc->irq);
  1599. return -EIO;
  1600. }
  1601. spin_lock_irqsave(&hc->lock, flags);
  1602. reset_hfcpci(hc);
  1603. while (cnt) {
  1604. inithfcpci(hc);
  1605. /*
  1606. * Finally enable IRQ output
  1607. * this is only allowed, if an IRQ routine is allready
  1608. * established for this HFC, so don't do that earlier
  1609. */
  1610. enable_hwirq(hc);
  1611. spin_unlock_irqrestore(&hc->lock, flags);
  1612. /* Timeout 80ms */
  1613. current->state = TASK_UNINTERRUPTIBLE;
  1614. schedule_timeout((80*HZ)/1000);
  1615. printk(KERN_INFO "HFC PCI: IRQ %d count %d\n",
  1616. hc->irq, hc->irqcnt);
  1617. /* now switch timer interrupt off */
  1618. spin_lock_irqsave(&hc->lock, flags);
  1619. hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
  1620. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  1621. /* reinit mode reg */
  1622. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  1623. if (!hc->irqcnt) {
  1624. printk(KERN_WARNING
  1625. "HFC PCI: IRQ(%d) getting no interrupts "
  1626. "during init %d\n", hc->irq, 4 - cnt);
  1627. if (cnt == 1) {
  1628. spin_unlock_irqrestore(&hc->lock, flags);
  1629. return -EIO;
  1630. } else {
  1631. reset_hfcpci(hc);
  1632. cnt--;
  1633. }
  1634. } else {
  1635. spin_unlock_irqrestore(&hc->lock, flags);
  1636. hc->initdone = 1;
  1637. return 0;
  1638. }
  1639. }
  1640. disable_hwirq(hc);
  1641. spin_unlock_irqrestore(&hc->lock, flags);
  1642. free_irq(hc->irq, hc);
  1643. return -EIO;
  1644. }
  1645. static int
  1646. channel_ctrl(struct hfc_pci *hc, struct mISDN_ctrl_req *cq)
  1647. {
  1648. int ret = 0;
  1649. u_char slot;
  1650. switch (cq->op) {
  1651. case MISDN_CTRL_GETOP:
  1652. cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
  1653. MISDN_CTRL_DISCONNECT;
  1654. break;
  1655. case MISDN_CTRL_LOOP:
  1656. /* channel 0 disabled loop */
  1657. if (cq->channel < 0 || cq->channel > 2) {
  1658. ret = -EINVAL;
  1659. break;
  1660. }
  1661. if (cq->channel & 1) {
  1662. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
  1663. slot = 0xC0;
  1664. else
  1665. slot = 0x80;
  1666. printk(KERN_DEBUG "%s: Write_hfc: B1_SSL/RSL 0x%x\n",
  1667. __func__, slot);
  1668. Write_hfc(hc, HFCPCI_B1_SSL, slot);
  1669. Write_hfc(hc, HFCPCI_B1_RSL, slot);
  1670. hc->hw.conn = (hc->hw.conn & ~7) | 6;
  1671. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1672. }
  1673. if (cq->channel & 2) {
  1674. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
  1675. slot = 0xC1;
  1676. else
  1677. slot = 0x81;
  1678. printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",
  1679. __func__, slot);
  1680. Write_hfc(hc, HFCPCI_B2_SSL, slot);
  1681. Write_hfc(hc, HFCPCI_B2_RSL, slot);
  1682. hc->hw.conn = (hc->hw.conn & ~0x38) | 0x30;
  1683. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1684. }
  1685. if (cq->channel & 3)
  1686. hc->hw.trm |= 0x80; /* enable IOM-loop */
  1687. else {
  1688. hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;
  1689. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1690. hc->hw.trm &= 0x7f; /* disable IOM-loop */
  1691. }
  1692. Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
  1693. break;
  1694. case MISDN_CTRL_CONNECT:
  1695. if (cq->channel == cq->p1) {
  1696. ret = -EINVAL;
  1697. break;
  1698. }
  1699. if (cq->channel < 1 || cq->channel > 2 ||
  1700. cq->p1 < 1 || cq->p1 > 2) {
  1701. ret = -EINVAL;
  1702. break;
  1703. }
  1704. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
  1705. slot = 0xC0;
  1706. else
  1707. slot = 0x80;
  1708. printk(KERN_DEBUG "%s: Write_hfc: B1_SSL/RSL 0x%x\n",
  1709. __func__, slot);
  1710. Write_hfc(hc, HFCPCI_B1_SSL, slot);
  1711. Write_hfc(hc, HFCPCI_B2_RSL, slot);
  1712. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
  1713. slot = 0xC1;
  1714. else
  1715. slot = 0x81;
  1716. printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",
  1717. __func__, slot);
  1718. Write_hfc(hc, HFCPCI_B2_SSL, slot);
  1719. Write_hfc(hc, HFCPCI_B1_RSL, slot);
  1720. hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x36;
  1721. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1722. hc->hw.trm |= 0x80;
  1723. Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
  1724. break;
  1725. case MISDN_CTRL_DISCONNECT:
  1726. hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;
  1727. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1728. hc->hw.trm &= 0x7f; /* disable IOM-loop */
  1729. break;
  1730. default:
  1731. printk(KERN_WARNING "%s: unknown Op %x\n",
  1732. __func__, cq->op);
  1733. ret = -EINVAL;
  1734. break;
  1735. }
  1736. return ret;
  1737. }
  1738. static int
  1739. open_dchannel(struct hfc_pci *hc, struct mISDNchannel *ch,
  1740. struct channel_req *rq)
  1741. {
  1742. int err = 0;
  1743. if (debug & DEBUG_HW_OPEN)
  1744. printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
  1745. hc->dch.dev.id, __builtin_return_address(0));
  1746. if (rq->protocol == ISDN_P_NONE)
  1747. return -EINVAL;
  1748. if (!hc->initdone) {
  1749. if (rq->protocol == ISDN_P_TE_S0) {
  1750. err = create_l1(&hc->dch, hfc_l1callback);
  1751. if (err)
  1752. return err;
  1753. }
  1754. hc->hw.protocol = rq->protocol;
  1755. ch->protocol = rq->protocol;
  1756. err = init_card(hc);
  1757. if (err)
  1758. return err;
  1759. } else {
  1760. if (rq->protocol != ch->protocol) {
  1761. if (hc->hw.protocol == ISDN_P_TE_S0)
  1762. l1_event(hc->dch.l1, CLOSE_CHANNEL);
  1763. hc->hw.protocol = rq->protocol;
  1764. ch->protocol = rq->protocol;
  1765. hfcpci_setmode(hc);
  1766. }
  1767. }
  1768. if (((ch->protocol == ISDN_P_NT_S0) && (hc->dch.state == 3)) ||
  1769. ((ch->protocol == ISDN_P_TE_S0) && (hc->dch.state == 7))) {
  1770. _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
  1771. 0, NULL, GFP_KERNEL);
  1772. }
  1773. rq->ch = ch;
  1774. if (!try_module_get(THIS_MODULE))
  1775. printk(KERN_WARNING "%s:cannot get module\n", __func__);
  1776. return 0;
  1777. }
  1778. static int
  1779. open_bchannel(struct hfc_pci *hc, struct channel_req *rq)
  1780. {
  1781. struct bchannel *bch;
  1782. if (rq->adr.channel > 2)
  1783. return -EINVAL;
  1784. if (rq->protocol == ISDN_P_NONE)
  1785. return -EINVAL;
  1786. bch = &hc->bch[rq->adr.channel - 1];
  1787. if (test_and_set_bit(FLG_OPEN, &bch->Flags))
  1788. return -EBUSY; /* b-channel can be only open once */
  1789. bch->ch.protocol = rq->protocol;
  1790. rq->ch = &bch->ch; /* TODO: E-channel */
  1791. if (!try_module_get(THIS_MODULE))
  1792. printk(KERN_WARNING "%s:cannot get module\n", __func__);
  1793. return 0;
  1794. }
  1795. /*
  1796. * device control function
  1797. */
  1798. static int
  1799. hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1800. {
  1801. struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
  1802. struct dchannel *dch = container_of(dev, struct dchannel, dev);
  1803. struct hfc_pci *hc = dch->hw;
  1804. struct channel_req *rq;
  1805. int err = 0;
  1806. if (dch->debug & DEBUG_HW)
  1807. printk(KERN_DEBUG "%s: cmd:%x %p\n",
  1808. __func__, cmd, arg);
  1809. switch (cmd) {
  1810. case OPEN_CHANNEL:
  1811. rq = arg;
  1812. if (rq->adr.channel == 0)
  1813. err = open_dchannel(hc, ch, rq);
  1814. else
  1815. err = open_bchannel(hc, rq);
  1816. break;
  1817. case CLOSE_CHANNEL:
  1818. if (debug & DEBUG_HW_OPEN)
  1819. printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
  1820. __func__, hc->dch.dev.id,
  1821. __builtin_return_address(0));
  1822. module_put(THIS_MODULE);
  1823. break;
  1824. case CONTROL_CHANNEL:
  1825. err = channel_ctrl(hc, arg);
  1826. break;
  1827. default:
  1828. if (dch->debug & DEBUG_HW)
  1829. printk(KERN_DEBUG "%s: unknown command %x\n",
  1830. __func__, cmd);
  1831. return -EINVAL;
  1832. }
  1833. return err;
  1834. }
  1835. static int
  1836. setup_hw(struct hfc_pci *hc)
  1837. {
  1838. void *buffer;
  1839. printk(KERN_INFO "mISDN: HFC-PCI driver %s\n", hfcpci_revision);
  1840. hc->hw.cirm = 0;
  1841. hc->dch.state = 0;
  1842. pci_set_master(hc->pdev);
  1843. if (!hc->irq) {
  1844. printk(KERN_WARNING "HFC-PCI: No IRQ for PCI card found\n");
  1845. return 1;
  1846. }
  1847. hc->hw.pci_io = (char *)(ulong)hc->pdev->resource[1].start;
  1848. if (!hc->hw.pci_io) {
  1849. printk(KERN_WARNING "HFC-PCI: No IO-Mem for PCI card found\n");
  1850. return 1;
  1851. }
  1852. /* Allocate memory for FIFOS */
  1853. /* the memory needs to be on a 32k boundary within the first 4G */
  1854. pci_set_dma_mask(hc->pdev, 0xFFFF8000);
  1855. buffer = pci_alloc_consistent(hc->pdev, 0x8000, &hc->hw.dmahandle);
  1856. /* We silently assume the address is okay if nonzero */
  1857. if (!buffer) {
  1858. printk(KERN_WARNING
  1859. "HFC-PCI: Error allocating memory for FIFO!\n");
  1860. return 1;
  1861. }
  1862. hc->hw.fifos = buffer;
  1863. pci_write_config_dword(hc->pdev, 0x80, hc->hw.dmahandle);
  1864. hc->hw.pci_io = ioremap((ulong) hc->hw.pci_io, 256);
  1865. printk(KERN_INFO
  1866. "HFC-PCI: defined at mem %#lx fifo %#lx(%#lx) IRQ %d HZ %d\n",
  1867. (u_long) hc->hw.pci_io, (u_long) hc->hw.fifos,
  1868. (u_long) hc->hw.dmahandle, hc->irq, HZ);
  1869. /* enable memory mapped ports, disable busmaster */
  1870. pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);
  1871. hc->hw.int_m2 = 0;
  1872. disable_hwirq(hc);
  1873. hc->hw.int_m1 = 0;
  1874. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  1875. /* At this point the needed PCI config is done */
  1876. /* fifos are still not enabled */
  1877. hc->hw.timer.function = (void *) hfcpci_Timer;
  1878. hc->hw.timer.data = (long) hc;
  1879. init_timer(&hc->hw.timer);
  1880. /* default PCM master */
  1881. test_and_set_bit(HFC_CFG_MASTER, &hc->cfg);
  1882. return 0;
  1883. }
  1884. static void
  1885. release_card(struct hfc_pci *hc) {
  1886. u_long flags;
  1887. spin_lock_irqsave(&hc->lock, flags);
  1888. hc->hw.int_m2 = 0; /* interrupt output off ! */
  1889. disable_hwirq(hc);
  1890. mode_hfcpci(&hc->bch[0], 1, ISDN_P_NONE);
  1891. mode_hfcpci(&hc->bch[1], 2, ISDN_P_NONE);
  1892. if (hc->dch.timer.function != NULL) {
  1893. del_timer(&hc->dch.timer);
  1894. hc->dch.timer.function = NULL;
  1895. }
  1896. spin_unlock_irqrestore(&hc->lock, flags);
  1897. if (hc->hw.protocol == ISDN_P_TE_S0)
  1898. l1_event(hc->dch.l1, CLOSE_CHANNEL);
  1899. if (hc->initdone)
  1900. free_irq(hc->irq, hc);
  1901. release_io_hfcpci(hc); /* must release after free_irq! */
  1902. mISDN_unregister_device(&hc->dch.dev);
  1903. mISDN_freebchannel(&hc->bch[1]);
  1904. mISDN_freebchannel(&hc->bch[0]);
  1905. mISDN_freedchannel(&hc->dch);
  1906. list_del(&hc->list);
  1907. pci_set_drvdata(hc->pdev, NULL);
  1908. kfree(hc);
  1909. }
  1910. static int
  1911. setup_card(struct hfc_pci *card)
  1912. {
  1913. int err = -EINVAL;
  1914. u_int i;
  1915. u_long flags;
  1916. char name[MISDN_MAX_IDLEN];
  1917. if (HFC_cnt >= MAX_CARDS)
  1918. return -EINVAL; /* maybe better value */
  1919. card->dch.debug = debug;
  1920. spin_lock_init(&card->lock);
  1921. mISDN_initdchannel(&card->dch, MAX_DFRAME_LEN_L1, ph_state);
  1922. card->dch.hw = card;
  1923. card->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
  1924. card->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
  1925. (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
  1926. card->dch.dev.D.send = hfcpci_l2l1D;
  1927. card->dch.dev.D.ctrl = hfc_dctrl;
  1928. card->dch.dev.nrbchan = 2;
  1929. for (i = 0; i < 2; i++) {
  1930. card->bch[i].nr = i + 1;
  1931. test_and_set_bit(i + 1, &card->dch.dev.channelmap[0]);
  1932. card->bch[i].debug = debug;
  1933. mISDN_initbchannel(&card->bch[i], MAX_DATA_MEM);
  1934. card->bch[i].hw = card;
  1935. card->bch[i].ch.send = hfcpci_l2l1B;
  1936. card->bch[i].ch.ctrl = hfc_bctrl;
  1937. card->bch[i].ch.nr = i + 1;
  1938. list_add(&card->bch[i].ch.list, &card->dch.dev.bchannels);
  1939. }
  1940. err = setup_hw(card);
  1941. if (err)
  1942. goto error;
  1943. snprintf(name, MISDN_MAX_IDLEN - 1, "hfc-pci.%d", HFC_cnt + 1);
  1944. err = mISDN_register_device(&card->dch.dev, name);
  1945. if (err)
  1946. goto error;
  1947. HFC_cnt++;
  1948. write_lock_irqsave(&HFClock, flags);
  1949. list_add_tail(&card->list, &HFClist);
  1950. write_unlock_irqrestore(&HFClock, flags);
  1951. printk(KERN_INFO "HFC %d cards installed\n", HFC_cnt);
  1952. return 0;
  1953. error:
  1954. mISDN_freebchannel(&card->bch[1]);
  1955. mISDN_freebchannel(&card->bch[0]);
  1956. mISDN_freedchannel(&card->dch);
  1957. kfree(card);
  1958. return err;
  1959. }
  1960. /* private data in the PCI devices list */
  1961. struct _hfc_map {
  1962. u_int subtype;
  1963. u_int flag;
  1964. char *name;
  1965. };
  1966. static const struct _hfc_map hfc_map[] =
  1967. {
  1968. {HFC_CCD_2BD0, 0, "CCD/Billion/Asuscom 2BD0"},
  1969. {HFC_CCD_B000, 0, "Billion B000"},
  1970. {HFC_CCD_B006, 0, "Billion B006"},
  1971. {HFC_CCD_B007, 0, "Billion B007"},
  1972. {HFC_CCD_B008, 0, "Billion B008"},
  1973. {HFC_CCD_B009, 0, "Billion B009"},
  1974. {HFC_CCD_B00A, 0, "Billion B00A"},
  1975. {HFC_CCD_B00B, 0, "Billion B00B"},
  1976. {HFC_CCD_B00C, 0, "Billion B00C"},
  1977. {HFC_CCD_B100, 0, "Seyeon B100"},
  1978. {HFC_CCD_B700, 0, "Primux II S0 B700"},
  1979. {HFC_CCD_B701, 0, "Primux II S0 NT B701"},
  1980. {HFC_ABOCOM_2BD1, 0, "Abocom/Magitek 2BD1"},
  1981. {HFC_ASUS_0675, 0, "Asuscom/Askey 675"},
  1982. {HFC_BERKOM_TCONCEPT, 0, "German telekom T-Concept"},
  1983. {HFC_BERKOM_A1T, 0, "German telekom A1T"},
  1984. {HFC_ANIGMA_MC145575, 0, "Motorola MC145575"},
  1985. {HFC_ZOLTRIX_2BD0, 0, "Zoltrix 2BD0"},
  1986. {HFC_DIGI_DF_M_IOM2_E, 0,
  1987. "Digi International DataFire Micro V IOM2 (Europe)"},
  1988. {HFC_DIGI_DF_M_E, 0,
  1989. "Digi International DataFire Micro V (Europe)"},
  1990. {HFC_DIGI_DF_M_IOM2_A, 0,
  1991. "Digi International DataFire Micro V IOM2 (North America)"},
  1992. {HFC_DIGI_DF_M_A, 0,
  1993. "Digi International DataFire Micro V (North America)"},
  1994. {HFC_SITECOM_DC105V2, 0, "Sitecom Connectivity DC-105 ISDN TA"},
  1995. {},
  1996. };
  1997. static struct pci_device_id hfc_ids[] =
  1998. {
  1999. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_2BD0,
  2000. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[0]},
  2001. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B000,
  2002. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[1]},
  2003. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B006,
  2004. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[2]},
  2005. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B007,
  2006. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[3]},
  2007. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B008,
  2008. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[4]},
  2009. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B009,
  2010. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[5]},
  2011. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00A,
  2012. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[6]},
  2013. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00B,
  2014. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[7]},
  2015. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00C,
  2016. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[8]},
  2017. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B100,
  2018. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[9]},
  2019. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B700,
  2020. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[10]},
  2021. {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B701,
  2022. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[11]},
  2023. {PCI_VENDOR_ID_ABOCOM, PCI_DEVICE_ID_ABOCOM_2BD1,
  2024. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[12]},
  2025. {PCI_VENDOR_ID_ASUSTEK, PCI_DEVICE_ID_ASUSTEK_0675,
  2026. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[13]},
  2027. {PCI_VENDOR_ID_BERKOM, PCI_DEVICE_ID_BERKOM_T_CONCEPT,
  2028. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[14]},
  2029. {PCI_VENDOR_ID_BERKOM, PCI_DEVICE_ID_BERKOM_A1T,
  2030. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[15]},
  2031. {PCI_VENDOR_ID_ANIGMA, PCI_DEVICE_ID_ANIGMA_MC145575,
  2032. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[16]},
  2033. {PCI_VENDOR_ID_ZOLTRIX, PCI_DEVICE_ID_ZOLTRIX_2BD0,
  2034. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[17]},
  2035. {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_E,
  2036. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[18]},
  2037. {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_E,
  2038. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[19]},
  2039. {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_A,
  2040. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[20]},
  2041. {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_A,
  2042. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[21]},
  2043. {PCI_VENDOR_ID_SITECOM, PCI_DEVICE_ID_SITECOM_DC105V2,
  2044. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &hfc_map[22]},
  2045. {},
  2046. };
  2047. static int __devinit
  2048. hfc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2049. {
  2050. int err = -ENOMEM;
  2051. struct hfc_pci *card;
  2052. struct _hfc_map *m = (struct _hfc_map *)ent->driver_data;
  2053. card = kzalloc(sizeof(struct hfc_pci), GFP_ATOMIC);
  2054. if (!card) {
  2055. printk(KERN_ERR "No kmem for HFC card\n");
  2056. return err;
  2057. }
  2058. card->pdev = pdev;
  2059. card->subtype = m->subtype;
  2060. err = pci_enable_device(pdev);
  2061. if (err) {
  2062. kfree(card);
  2063. return err;
  2064. }
  2065. printk(KERN_INFO "mISDN_hfcpci: found adapter %s at %s\n",
  2066. m->name, pci_name(pdev));
  2067. card->irq = pdev->irq;
  2068. pci_set_drvdata(pdev, card);
  2069. err = setup_card(card);
  2070. if (err)
  2071. pci_set_drvdata(pdev, NULL);
  2072. return err;
  2073. }
  2074. static void __devexit
  2075. hfc_remove_pci(struct pci_dev *pdev)
  2076. {
  2077. struct hfc_pci *card = pci_get_drvdata(pdev);
  2078. u_long flags;
  2079. if (card) {
  2080. write_lock_irqsave(&HFClock, flags);
  2081. release_card(card);
  2082. write_unlock_irqrestore(&HFClock, flags);
  2083. } else
  2084. if (debug)
  2085. printk(KERN_WARNING "%s: drvdata allready removed\n",
  2086. __func__);
  2087. }
  2088. static struct pci_driver hfc_driver = {
  2089. .name = "hfcpci",
  2090. .probe = hfc_probe,
  2091. .remove = __devexit_p(hfc_remove_pci),
  2092. .id_table = hfc_ids,
  2093. };
  2094. static int __init
  2095. HFC_init(void)
  2096. {
  2097. int err;
  2098. err = pci_register_driver(&hfc_driver);
  2099. return err;
  2100. }
  2101. static void __exit
  2102. HFC_cleanup(void)
  2103. {
  2104. struct hfc_pci *card, *next;
  2105. list_for_each_entry_safe(card, next, &HFClist, list) {
  2106. release_card(card);
  2107. }
  2108. pci_unregister_driver(&hfc_driver);
  2109. }
  2110. module_init(HFC_init);
  2111. module_exit(HFC_cleanup);