hfcpci.c 64 KB

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