hfcpci.c 62 KB

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