hfcpci.c 64 KB

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