hfcpci.c 62 KB

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