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/interrupt.h>
  47. #include <linux/module.h>
  48. #include <linux/pci.h>
  49. #include <linux/delay.h>
  50. #include <linux/mISDNhw.h>
  51. #include <linux/slab.h>
  52. #include "hfc_pci.h"
  53. static const char *hfcpci_revision = "2.0";
  54. static int HFC_cnt;
  55. static uint debug;
  56. static uint poll, tics;
  57. static struct timer_list hfc_tl;
  58. static unsigned long hfc_jiffies;
  59. MODULE_AUTHOR("Karsten Keil");
  60. MODULE_LICENSE("GPL");
  61. module_param(debug, uint, S_IRUGO | S_IWUSR);
  62. module_param(poll, uint, S_IRUGO | S_IWUSR);
  63. enum {
  64. HFC_CCD_2BD0,
  65. HFC_CCD_B000,
  66. HFC_CCD_B006,
  67. HFC_CCD_B007,
  68. HFC_CCD_B008,
  69. HFC_CCD_B009,
  70. HFC_CCD_B00A,
  71. HFC_CCD_B00B,
  72. HFC_CCD_B00C,
  73. HFC_CCD_B100,
  74. HFC_CCD_B700,
  75. HFC_CCD_B701,
  76. HFC_ASUS_0675,
  77. HFC_BERKOM_A1T,
  78. HFC_BERKOM_TCONCEPT,
  79. HFC_ANIGMA_MC145575,
  80. HFC_ZOLTRIX_2BD0,
  81. HFC_DIGI_DF_M_IOM2_E,
  82. HFC_DIGI_DF_M_E,
  83. HFC_DIGI_DF_M_IOM2_A,
  84. HFC_DIGI_DF_M_A,
  85. HFC_ABOCOM_2BD1,
  86. HFC_SITECOM_DC105V2,
  87. };
  88. struct hfcPCI_hw {
  89. unsigned char cirm;
  90. unsigned char ctmt;
  91. unsigned char clkdel;
  92. unsigned char states;
  93. unsigned char conn;
  94. unsigned char mst_m;
  95. unsigned char int_m1;
  96. unsigned char int_m2;
  97. unsigned char sctrl;
  98. unsigned char sctrl_r;
  99. unsigned char sctrl_e;
  100. unsigned char trm;
  101. unsigned char fifo_en;
  102. unsigned char bswapped;
  103. unsigned char protocol;
  104. int nt_timer;
  105. unsigned char __iomem *pci_io; /* start of PCI IO memory */
  106. dma_addr_t dmahandle;
  107. void *fifos; /* FIFO memory */
  108. int last_bfifo_cnt[2];
  109. /* marker saving last b-fifo frame count */
  110. struct timer_list timer;
  111. };
  112. #define HFC_CFG_MASTER 1
  113. #define HFC_CFG_SLAVE 2
  114. #define HFC_CFG_PCM 3
  115. #define HFC_CFG_2HFC 4
  116. #define HFC_CFG_SLAVEHFC 5
  117. #define HFC_CFG_NEG_F0 6
  118. #define HFC_CFG_SW_DD_DU 7
  119. #define FLG_HFC_TIMER_T1 16
  120. #define FLG_HFC_TIMER_T3 17
  121. #define NT_T1_COUNT 1120 /* number of 3.125ms interrupts (3.5s) */
  122. #define NT_T3_COUNT 31 /* number of 3.125ms interrupts (97 ms) */
  123. #define CLKDEL_TE 0x0e /* CLKDEL in TE mode */
  124. #define CLKDEL_NT 0x6c /* CLKDEL in NT mode */
  125. struct hfc_pci {
  126. u_char subtype;
  127. u_char chanlimit;
  128. u_char initdone;
  129. u_long cfg;
  130. u_int irq;
  131. u_int irqcnt;
  132. struct pci_dev *pdev;
  133. struct hfcPCI_hw hw;
  134. spinlock_t lock; /* card lock */
  135. struct dchannel dch;
  136. struct bchannel bch[2];
  137. };
  138. /* Interface functions */
  139. static void
  140. enable_hwirq(struct hfc_pci *hc)
  141. {
  142. hc->hw.int_m2 |= HFCPCI_IRQ_ENABLE;
  143. Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);
  144. }
  145. static void
  146. disable_hwirq(struct hfc_pci *hc)
  147. {
  148. hc->hw.int_m2 &= ~((u_char)HFCPCI_IRQ_ENABLE);
  149. Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);
  150. }
  151. /*
  152. * free hardware resources used by driver
  153. */
  154. static void
  155. release_io_hfcpci(struct hfc_pci *hc)
  156. {
  157. /* disable memory mapped ports + busmaster */
  158. pci_write_config_word(hc->pdev, PCI_COMMAND, 0);
  159. del_timer(&hc->hw.timer);
  160. pci_free_consistent(hc->pdev, 0x8000, hc->hw.fifos, hc->hw.dmahandle);
  161. iounmap(hc->hw.pci_io);
  162. }
  163. /*
  164. * set mode (NT or TE)
  165. */
  166. static void
  167. hfcpci_setmode(struct hfc_pci *hc)
  168. {
  169. if (hc->hw.protocol == ISDN_P_NT_S0) {
  170. hc->hw.clkdel = CLKDEL_NT; /* ST-Bit delay for NT-Mode */
  171. hc->hw.sctrl |= SCTRL_MODE_NT; /* NT-MODE */
  172. hc->hw.states = 1; /* G1 */
  173. } else {
  174. hc->hw.clkdel = CLKDEL_TE; /* ST-Bit delay for TE-Mode */
  175. hc->hw.sctrl &= ~SCTRL_MODE_NT; /* TE-MODE */
  176. hc->hw.states = 2; /* F2 */
  177. }
  178. Write_hfc(hc, HFCPCI_CLKDEL, hc->hw.clkdel);
  179. Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | hc->hw.states);
  180. udelay(10);
  181. Write_hfc(hc, HFCPCI_STATES, hc->hw.states | 0x40); /* Deactivate */
  182. Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);
  183. }
  184. /*
  185. * function called to reset the HFC PCI chip. A complete software reset of chip
  186. * and fifos is done.
  187. */
  188. static void
  189. reset_hfcpci(struct hfc_pci *hc)
  190. {
  191. u_char val;
  192. int cnt = 0;
  193. printk(KERN_DEBUG "reset_hfcpci: entered\n");
  194. val = Read_hfc(hc, HFCPCI_CHIP_ID);
  195. printk(KERN_INFO "HFC_PCI: resetting HFC ChipId(%x)\n", val);
  196. /* enable memory mapped ports, disable busmaster */
  197. pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);
  198. disable_hwirq(hc);
  199. /* enable memory ports + busmaster */
  200. pci_write_config_word(hc->pdev, PCI_COMMAND,
  201. PCI_ENA_MEMIO + PCI_ENA_MASTER);
  202. val = Read_hfc(hc, HFCPCI_STATUS);
  203. printk(KERN_DEBUG "HFC-PCI status(%x) before reset\n", val);
  204. hc->hw.cirm = HFCPCI_RESET; /* Reset On */
  205. Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
  206. set_current_state(TASK_UNINTERRUPTIBLE);
  207. mdelay(10); /* Timeout 10ms */
  208. hc->hw.cirm = 0; /* Reset Off */
  209. Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
  210. val = Read_hfc(hc, HFCPCI_STATUS);
  211. printk(KERN_DEBUG "HFC-PCI status(%x) after reset\n", val);
  212. while (cnt < 50000) { /* max 50000 us */
  213. udelay(5);
  214. cnt += 5;
  215. val = Read_hfc(hc, HFCPCI_STATUS);
  216. if (!(val & 2))
  217. break;
  218. }
  219. printk(KERN_DEBUG "HFC-PCI status(%x) after %dus\n", val, cnt);
  220. hc->hw.fifo_en = 0x30; /* only D fifos enabled */
  221. hc->hw.bswapped = 0; /* no exchange */
  222. hc->hw.ctmt = HFCPCI_TIM3_125 | HFCPCI_AUTO_TIMER;
  223. hc->hw.trm = HFCPCI_BTRANS_THRESMASK; /* no echo connect , threshold */
  224. hc->hw.sctrl = 0x40; /* set tx_lo mode, error in datasheet ! */
  225. hc->hw.sctrl_r = 0;
  226. hc->hw.sctrl_e = HFCPCI_AUTO_AWAKE; /* S/T Auto awake */
  227. hc->hw.mst_m = 0;
  228. if (test_bit(HFC_CFG_MASTER, &hc->cfg))
  229. hc->hw.mst_m |= HFCPCI_MASTER; /* HFC Master Mode */
  230. if (test_bit(HFC_CFG_NEG_F0, &hc->cfg))
  231. hc->hw.mst_m |= HFCPCI_F0_NEGATIV;
  232. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  233. Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
  234. Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);
  235. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
  236. hc->hw.int_m1 = HFCPCI_INTS_DTRANS | HFCPCI_INTS_DREC |
  237. HFCPCI_INTS_L1STATE | HFCPCI_INTS_TIMER;
  238. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  239. /* Clear already pending ints */
  240. val = Read_hfc(hc, HFCPCI_INT_S1);
  241. /* set NT/TE mode */
  242. hfcpci_setmode(hc);
  243. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  244. Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
  245. /*
  246. * Init GCI/IOM2 in master mode
  247. * Slots 0 and 1 are set for B-chan 1 and 2
  248. * D- and monitor/CI channel are not enabled
  249. * STIO1 is used as output for data, B1+B2 from ST->IOM+HFC
  250. * STIO2 is used as data input, B1+B2 from IOM->ST
  251. * ST B-channel send disabled -> continuous 1s
  252. * The IOM slots are always enabled
  253. */
  254. if (test_bit(HFC_CFG_PCM, &hc->cfg)) {
  255. /* set data flow directions: connect B1,B2: HFC to/from PCM */
  256. hc->hw.conn = 0x09;
  257. } else {
  258. hc->hw.conn = 0x36; /* set data flow directions */
  259. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {
  260. Write_hfc(hc, HFCPCI_B1_SSL, 0xC0);
  261. Write_hfc(hc, HFCPCI_B2_SSL, 0xC1);
  262. Write_hfc(hc, HFCPCI_B1_RSL, 0xC0);
  263. Write_hfc(hc, HFCPCI_B2_RSL, 0xC1);
  264. } else {
  265. Write_hfc(hc, HFCPCI_B1_SSL, 0x80);
  266. Write_hfc(hc, HFCPCI_B2_SSL, 0x81);
  267. Write_hfc(hc, HFCPCI_B1_RSL, 0x80);
  268. Write_hfc(hc, HFCPCI_B2_RSL, 0x81);
  269. }
  270. }
  271. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  272. val = Read_hfc(hc, HFCPCI_INT_S2);
  273. }
  274. /*
  275. * Timer function called when kernel timer expires
  276. */
  277. static void
  278. hfcpci_Timer(struct hfc_pci *hc)
  279. {
  280. hc->hw.timer.expires = jiffies + 75;
  281. /* WD RESET */
  282. /*
  283. * WriteReg(hc, HFCD_DATA, HFCD_CTMT, hc->hw.ctmt | 0x80);
  284. * add_timer(&hc->hw.timer);
  285. */
  286. }
  287. /*
  288. * select a b-channel entry matching and active
  289. */
  290. static struct bchannel *
  291. Sel_BCS(struct hfc_pci *hc, int channel)
  292. {
  293. if (test_bit(FLG_ACTIVE, &hc->bch[0].Flags) &&
  294. (hc->bch[0].nr & channel))
  295. return &hc->bch[0];
  296. else if (test_bit(FLG_ACTIVE, &hc->bch[1].Flags) &&
  297. (hc->bch[1].nr & channel))
  298. return &hc->bch[1];
  299. else
  300. return NULL;
  301. }
  302. /*
  303. * clear the desired B-channel rx fifo
  304. */
  305. static void
  306. hfcpci_clear_fifo_rx(struct hfc_pci *hc, int fifo)
  307. {
  308. u_char fifo_state;
  309. struct bzfifo *bzr;
  310. if (fifo) {
  311. bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;
  312. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2RX;
  313. } else {
  314. bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;
  315. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1RX;
  316. }
  317. if (fifo_state)
  318. hc->hw.fifo_en ^= fifo_state;
  319. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  320. hc->hw.last_bfifo_cnt[fifo] = 0;
  321. bzr->f1 = MAX_B_FRAMES;
  322. bzr->f2 = bzr->f1; /* init F pointers to remain constant */
  323. bzr->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);
  324. bzr->za[MAX_B_FRAMES].z2 = cpu_to_le16(
  325. le16_to_cpu(bzr->za[MAX_B_FRAMES].z1));
  326. if (fifo_state)
  327. hc->hw.fifo_en |= fifo_state;
  328. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  329. }
  330. /*
  331. * clear the desired B-channel tx fifo
  332. */
  333. static void hfcpci_clear_fifo_tx(struct hfc_pci *hc, int fifo)
  334. {
  335. u_char fifo_state;
  336. struct bzfifo *bzt;
  337. if (fifo) {
  338. bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
  339. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2TX;
  340. } else {
  341. bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
  342. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1TX;
  343. }
  344. if (fifo_state)
  345. hc->hw.fifo_en ^= fifo_state;
  346. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  347. if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)
  348. printk(KERN_DEBUG "hfcpci_clear_fifo_tx%d f1(%x) f2(%x) "
  349. "z1(%x) z2(%x) state(%x)\n",
  350. fifo, bzt->f1, bzt->f2,
  351. le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),
  352. le16_to_cpu(bzt->za[MAX_B_FRAMES].z2),
  353. fifo_state);
  354. bzt->f2 = MAX_B_FRAMES;
  355. bzt->f1 = bzt->f2; /* init F pointers to remain constant */
  356. bzt->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);
  357. bzt->za[MAX_B_FRAMES].z2 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 2);
  358. if (fifo_state)
  359. hc->hw.fifo_en |= fifo_state;
  360. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  361. if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)
  362. printk(KERN_DEBUG
  363. "hfcpci_clear_fifo_tx%d f1(%x) f2(%x) z1(%x) z2(%x)\n",
  364. fifo, bzt->f1, bzt->f2,
  365. le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),
  366. le16_to_cpu(bzt->za[MAX_B_FRAMES].z2));
  367. }
  368. /*
  369. * read a complete B-frame out of the buffer
  370. */
  371. static void
  372. hfcpci_empty_bfifo(struct bchannel *bch, struct bzfifo *bz,
  373. u_char *bdata, int count)
  374. {
  375. u_char *ptr, *ptr1, new_f2;
  376. int maxlen, new_z2;
  377. struct zt *zp;
  378. if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))
  379. printk(KERN_DEBUG "hfcpci_empty_fifo\n");
  380. zp = &bz->za[bz->f2]; /* point to Z-Regs */
  381. new_z2 = le16_to_cpu(zp->z2) + count; /* new position in fifo */
  382. if (new_z2 >= (B_FIFO_SIZE + B_SUB_VAL))
  383. new_z2 -= B_FIFO_SIZE; /* buffer wrap */
  384. new_f2 = (bz->f2 + 1) & MAX_B_FRAMES;
  385. if ((count > MAX_DATA_SIZE + 3) || (count < 4) ||
  386. (*(bdata + (le16_to_cpu(zp->z1) - B_SUB_VAL)))) {
  387. if (bch->debug & DEBUG_HW)
  388. printk(KERN_DEBUG "hfcpci_empty_fifo: incoming packet "
  389. "invalid length %d or crc\n", count);
  390. #ifdef ERROR_STATISTIC
  391. bch->err_inv++;
  392. #endif
  393. bz->za[new_f2].z2 = cpu_to_le16(new_z2);
  394. bz->f2 = new_f2; /* next buffer */
  395. } else {
  396. bch->rx_skb = mI_alloc_skb(count - 3, GFP_ATOMIC);
  397. if (!bch->rx_skb) {
  398. printk(KERN_WARNING "HFCPCI: receive out of memory\n");
  399. return;
  400. }
  401. count -= 3;
  402. ptr = skb_put(bch->rx_skb, count);
  403. if (le16_to_cpu(zp->z2) + count <= B_FIFO_SIZE + B_SUB_VAL)
  404. maxlen = count; /* complete transfer */
  405. else
  406. maxlen = B_FIFO_SIZE + B_SUB_VAL -
  407. le16_to_cpu(zp->z2); /* maximum */
  408. ptr1 = bdata + (le16_to_cpu(zp->z2) - B_SUB_VAL);
  409. /* start of data */
  410. memcpy(ptr, ptr1, maxlen); /* copy data */
  411. count -= maxlen;
  412. if (count) { /* rest remaining */
  413. ptr += maxlen;
  414. ptr1 = bdata; /* start of buffer */
  415. memcpy(ptr, ptr1, count); /* rest */
  416. }
  417. bz->za[new_f2].z2 = cpu_to_le16(new_z2);
  418. bz->f2 = new_f2; /* next buffer */
  419. recv_Bchannel(bch, MISDN_ID_ANY);
  420. }
  421. }
  422. /*
  423. * D-channel receive procedure
  424. */
  425. static int
  426. receive_dmsg(struct hfc_pci *hc)
  427. {
  428. struct dchannel *dch = &hc->dch;
  429. int maxlen;
  430. int rcnt, total;
  431. int count = 5;
  432. u_char *ptr, *ptr1;
  433. struct dfifo *df;
  434. struct zt *zp;
  435. df = &((union fifo_area *)(hc->hw.fifos))->d_chan.d_rx;
  436. while (((df->f1 & D_FREG_MASK) != (df->f2 & D_FREG_MASK)) && count--) {
  437. zp = &df->za[df->f2 & D_FREG_MASK];
  438. rcnt = le16_to_cpu(zp->z1) - le16_to_cpu(zp->z2);
  439. if (rcnt < 0)
  440. rcnt += D_FIFO_SIZE;
  441. rcnt++;
  442. if (dch->debug & DEBUG_HW_DCHANNEL)
  443. printk(KERN_DEBUG
  444. "hfcpci recd f1(%d) f2(%d) z1(%x) z2(%x) cnt(%d)\n",
  445. df->f1, df->f2,
  446. le16_to_cpu(zp->z1),
  447. le16_to_cpu(zp->z2),
  448. rcnt);
  449. if ((rcnt > MAX_DFRAME_LEN + 3) || (rcnt < 4) ||
  450. (df->data[le16_to_cpu(zp->z1)])) {
  451. if (dch->debug & DEBUG_HW)
  452. printk(KERN_DEBUG
  453. "empty_fifo hfcpci paket inv. len "
  454. "%d or crc %d\n",
  455. rcnt,
  456. df->data[le16_to_cpu(zp->z1)]);
  457. #ifdef ERROR_STATISTIC
  458. cs->err_rx++;
  459. #endif
  460. df->f2 = ((df->f2 + 1) & MAX_D_FRAMES) |
  461. (MAX_D_FRAMES + 1); /* next buffer */
  462. df->za[df->f2 & D_FREG_MASK].z2 =
  463. cpu_to_le16((le16_to_cpu(zp->z2) + rcnt) &
  464. (D_FIFO_SIZE - 1));
  465. } else {
  466. dch->rx_skb = mI_alloc_skb(rcnt - 3, GFP_ATOMIC);
  467. if (!dch->rx_skb) {
  468. printk(KERN_WARNING
  469. "HFC-PCI: D receive out of memory\n");
  470. break;
  471. }
  472. total = rcnt;
  473. rcnt -= 3;
  474. ptr = skb_put(dch->rx_skb, rcnt);
  475. if (le16_to_cpu(zp->z2) + rcnt <= D_FIFO_SIZE)
  476. maxlen = rcnt; /* complete transfer */
  477. else
  478. maxlen = D_FIFO_SIZE - le16_to_cpu(zp->z2);
  479. /* maximum */
  480. ptr1 = df->data + le16_to_cpu(zp->z2);
  481. /* start of data */
  482. memcpy(ptr, ptr1, maxlen); /* copy data */
  483. rcnt -= maxlen;
  484. if (rcnt) { /* rest remaining */
  485. ptr += maxlen;
  486. ptr1 = df->data; /* start of buffer */
  487. memcpy(ptr, ptr1, rcnt); /* rest */
  488. }
  489. df->f2 = ((df->f2 + 1) & MAX_D_FRAMES) |
  490. (MAX_D_FRAMES + 1); /* next buffer */
  491. df->za[df->f2 & D_FREG_MASK].z2 = cpu_to_le16((
  492. le16_to_cpu(zp->z2) + total) & (D_FIFO_SIZE - 1));
  493. recv_Dchannel(dch);
  494. }
  495. }
  496. return 1;
  497. }
  498. /*
  499. * check for transparent receive data and read max one 'poll' size if avail
  500. */
  501. static void
  502. hfcpci_empty_fifo_trans(struct bchannel *bch, struct bzfifo *rxbz,
  503. struct bzfifo *txbz, u_char *bdata)
  504. {
  505. __le16 *z1r, *z2r, *z1t, *z2t;
  506. int new_z2, fcnt_rx, fcnt_tx, maxlen;
  507. u_char *ptr, *ptr1;
  508. z1r = &rxbz->za[MAX_B_FRAMES].z1; /* pointer to z reg */
  509. z2r = z1r + 1;
  510. z1t = &txbz->za[MAX_B_FRAMES].z1;
  511. z2t = z1t + 1;
  512. fcnt_rx = le16_to_cpu(*z1r) - le16_to_cpu(*z2r);
  513. if (!fcnt_rx)
  514. return; /* no data avail */
  515. if (fcnt_rx <= 0)
  516. fcnt_rx += B_FIFO_SIZE; /* bytes actually buffered */
  517. new_z2 = le16_to_cpu(*z2r) + fcnt_rx; /* new position in fifo */
  518. if (new_z2 >= (B_FIFO_SIZE + B_SUB_VAL))
  519. new_z2 -= B_FIFO_SIZE; /* buffer wrap */
  520. if (fcnt_rx > MAX_DATA_SIZE) { /* flush, if oversized */
  521. *z2r = cpu_to_le16(new_z2); /* new position */
  522. return;
  523. }
  524. fcnt_tx = le16_to_cpu(*z2t) - le16_to_cpu(*z1t);
  525. if (fcnt_tx <= 0)
  526. fcnt_tx += B_FIFO_SIZE;
  527. /* fcnt_tx contains available bytes in tx-fifo */
  528. fcnt_tx = B_FIFO_SIZE - fcnt_tx;
  529. /* remaining bytes to send (bytes in tx-fifo) */
  530. bch->rx_skb = mI_alloc_skb(fcnt_rx, GFP_ATOMIC);
  531. if (bch->rx_skb) {
  532. ptr = skb_put(bch->rx_skb, fcnt_rx);
  533. if (le16_to_cpu(*z2r) + fcnt_rx <= B_FIFO_SIZE + B_SUB_VAL)
  534. maxlen = fcnt_rx; /* complete transfer */
  535. else
  536. maxlen = B_FIFO_SIZE + B_SUB_VAL - le16_to_cpu(*z2r);
  537. /* maximum */
  538. ptr1 = bdata + (le16_to_cpu(*z2r) - B_SUB_VAL);
  539. /* start of data */
  540. memcpy(ptr, ptr1, maxlen); /* copy data */
  541. fcnt_rx -= maxlen;
  542. if (fcnt_rx) { /* rest remaining */
  543. ptr += maxlen;
  544. ptr1 = bdata; /* start of buffer */
  545. memcpy(ptr, ptr1, fcnt_rx); /* rest */
  546. }
  547. recv_Bchannel(bch, fcnt_tx); /* bch, id */
  548. } else
  549. printk(KERN_WARNING "HFCPCI: receive out of memory\n");
  550. *z2r = cpu_to_le16(new_z2); /* new position */
  551. }
  552. /*
  553. * B-channel main receive routine
  554. */
  555. static void
  556. main_rec_hfcpci(struct bchannel *bch)
  557. {
  558. struct hfc_pci *hc = bch->hw;
  559. int rcnt, real_fifo;
  560. int receive = 0, count = 5;
  561. struct bzfifo *txbz, *rxbz;
  562. u_char *bdata;
  563. struct zt *zp;
  564. if ((bch->nr & 2) && (!hc->hw.bswapped)) {
  565. rxbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;
  566. txbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
  567. bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b2;
  568. real_fifo = 1;
  569. } else {
  570. rxbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;
  571. txbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
  572. bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b1;
  573. real_fifo = 0;
  574. }
  575. Begin:
  576. count--;
  577. if (rxbz->f1 != rxbz->f2) {
  578. if (bch->debug & DEBUG_HW_BCHANNEL)
  579. printk(KERN_DEBUG "hfcpci rec ch(%x) f1(%d) f2(%d)\n",
  580. bch->nr, rxbz->f1, rxbz->f2);
  581. zp = &rxbz->za[rxbz->f2];
  582. rcnt = le16_to_cpu(zp->z1) - le16_to_cpu(zp->z2);
  583. if (rcnt < 0)
  584. rcnt += B_FIFO_SIZE;
  585. rcnt++;
  586. if (bch->debug & DEBUG_HW_BCHANNEL)
  587. printk(KERN_DEBUG
  588. "hfcpci rec ch(%x) z1(%x) z2(%x) cnt(%d)\n",
  589. bch->nr, le16_to_cpu(zp->z1),
  590. le16_to_cpu(zp->z2), rcnt);
  591. hfcpci_empty_bfifo(bch, rxbz, bdata, rcnt);
  592. rcnt = rxbz->f1 - rxbz->f2;
  593. if (rcnt < 0)
  594. rcnt += MAX_B_FRAMES + 1;
  595. if (hc->hw.last_bfifo_cnt[real_fifo] > rcnt + 1) {
  596. rcnt = 0;
  597. hfcpci_clear_fifo_rx(hc, real_fifo);
  598. }
  599. hc->hw.last_bfifo_cnt[real_fifo] = rcnt;
  600. if (rcnt > 1)
  601. receive = 1;
  602. else
  603. receive = 0;
  604. } else if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
  605. hfcpci_empty_fifo_trans(bch, rxbz, txbz, bdata);
  606. return;
  607. } else
  608. receive = 0;
  609. if (count && receive)
  610. goto Begin;
  611. }
  612. /*
  613. * D-channel send routine
  614. */
  615. static void
  616. hfcpci_fill_dfifo(struct hfc_pci *hc)
  617. {
  618. struct dchannel *dch = &hc->dch;
  619. int fcnt;
  620. int count, new_z1, maxlen;
  621. struct dfifo *df;
  622. u_char *src, *dst, new_f1;
  623. if ((dch->debug & DEBUG_HW_DCHANNEL) && !(dch->debug & DEBUG_HW_DFIFO))
  624. printk(KERN_DEBUG "%s\n", __func__);
  625. if (!dch->tx_skb)
  626. return;
  627. count = dch->tx_skb->len - dch->tx_idx;
  628. if (count <= 0)
  629. return;
  630. df = &((union fifo_area *) (hc->hw.fifos))->d_chan.d_tx;
  631. if (dch->debug & DEBUG_HW_DFIFO)
  632. printk(KERN_DEBUG "%s:f1(%d) f2(%d) z1(f1)(%x)\n", __func__,
  633. df->f1, df->f2,
  634. le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1));
  635. fcnt = df->f1 - df->f2; /* frame count actually buffered */
  636. if (fcnt < 0)
  637. fcnt += (MAX_D_FRAMES + 1); /* if wrap around */
  638. if (fcnt > (MAX_D_FRAMES - 1)) {
  639. if (dch->debug & DEBUG_HW_DCHANNEL)
  640. printk(KERN_DEBUG
  641. "hfcpci_fill_Dfifo more as 14 frames\n");
  642. #ifdef ERROR_STATISTIC
  643. cs->err_tx++;
  644. #endif
  645. return;
  646. }
  647. /* now determine free bytes in FIFO buffer */
  648. maxlen = le16_to_cpu(df->za[df->f2 & D_FREG_MASK].z2) -
  649. le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) - 1;
  650. if (maxlen <= 0)
  651. maxlen += D_FIFO_SIZE; /* count now contains available bytes */
  652. if (dch->debug & DEBUG_HW_DCHANNEL)
  653. printk(KERN_DEBUG "hfcpci_fill_Dfifo count(%d/%d)\n",
  654. count, maxlen);
  655. if (count > maxlen) {
  656. if (dch->debug & DEBUG_HW_DCHANNEL)
  657. printk(KERN_DEBUG "hfcpci_fill_Dfifo no fifo mem\n");
  658. return;
  659. }
  660. new_z1 = (le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) + count) &
  661. (D_FIFO_SIZE - 1);
  662. new_f1 = ((df->f1 + 1) & D_FREG_MASK) | (D_FREG_MASK + 1);
  663. src = dch->tx_skb->data + dch->tx_idx; /* source pointer */
  664. dst = df->data + le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);
  665. maxlen = D_FIFO_SIZE - le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);
  666. /* end fifo */
  667. if (maxlen > count)
  668. maxlen = count; /* limit size */
  669. memcpy(dst, src, maxlen); /* first copy */
  670. count -= maxlen; /* remaining bytes */
  671. if (count) {
  672. dst = df->data; /* start of buffer */
  673. src += maxlen; /* new position */
  674. memcpy(dst, src, count);
  675. }
  676. df->za[new_f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);
  677. /* for next buffer */
  678. df->za[df->f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);
  679. /* new pos actual buffer */
  680. df->f1 = new_f1; /* next frame */
  681. dch->tx_idx = dch->tx_skb->len;
  682. }
  683. /*
  684. * B-channel send routine
  685. */
  686. static void
  687. hfcpci_fill_fifo(struct bchannel *bch)
  688. {
  689. struct hfc_pci *hc = bch->hw;
  690. int maxlen, fcnt;
  691. int count, new_z1;
  692. struct bzfifo *bz;
  693. u_char *bdata;
  694. u_char new_f1, *src, *dst;
  695. __le16 *z1t, *z2t;
  696. if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))
  697. printk(KERN_DEBUG "%s\n", __func__);
  698. if ((!bch->tx_skb) || bch->tx_skb->len <= 0)
  699. return;
  700. count = bch->tx_skb->len - bch->tx_idx;
  701. if ((bch->nr & 2) && (!hc->hw.bswapped)) {
  702. bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
  703. bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b2;
  704. } else {
  705. bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
  706. bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b1;
  707. }
  708. if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
  709. z1t = &bz->za[MAX_B_FRAMES].z1;
  710. z2t = z1t + 1;
  711. if (bch->debug & DEBUG_HW_BCHANNEL)
  712. printk(KERN_DEBUG "hfcpci_fill_fifo_trans ch(%x) "
  713. "cnt(%d) z1(%x) z2(%x)\n", bch->nr, count,
  714. le16_to_cpu(*z1t), le16_to_cpu(*z2t));
  715. fcnt = le16_to_cpu(*z2t) - le16_to_cpu(*z1t);
  716. if (fcnt <= 0)
  717. fcnt += B_FIFO_SIZE;
  718. /* fcnt contains available bytes in fifo */
  719. fcnt = B_FIFO_SIZE - fcnt;
  720. /* remaining bytes to send (bytes in fifo) */
  721. /* "fill fifo if empty" feature */
  722. if (test_bit(FLG_FILLEMPTY, &bch->Flags) && !fcnt) {
  723. /* printk(KERN_DEBUG "%s: buffer empty, so we have "
  724. "underrun\n", __func__); */
  725. /* fill buffer, to prevent future underrun */
  726. count = HFCPCI_FILLEMPTY;
  727. new_z1 = le16_to_cpu(*z1t) + count;
  728. /* new buffer Position */
  729. if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
  730. new_z1 -= B_FIFO_SIZE; /* buffer wrap */
  731. dst = bdata + (le16_to_cpu(*z1t) - B_SUB_VAL);
  732. maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(*z1t);
  733. /* end of fifo */
  734. if (bch->debug & DEBUG_HW_BFIFO)
  735. printk(KERN_DEBUG "hfcpci_FFt fillempty "
  736. "fcnt(%d) maxl(%d) nz1(%x) dst(%p)\n",
  737. fcnt, maxlen, new_z1, dst);
  738. fcnt += count;
  739. if (maxlen > count)
  740. maxlen = count; /* limit size */
  741. memset(dst, 0x2a, maxlen); /* first copy */
  742. count -= maxlen; /* remaining bytes */
  743. if (count) {
  744. dst = bdata; /* start of buffer */
  745. memset(dst, 0x2a, count);
  746. }
  747. *z1t = cpu_to_le16(new_z1); /* now send data */
  748. }
  749. next_t_frame:
  750. count = bch->tx_skb->len - bch->tx_idx;
  751. /* maximum fill shall be poll*2 */
  752. if (count > (poll << 1) - fcnt)
  753. count = (poll << 1) - fcnt;
  754. if (count <= 0)
  755. return;
  756. /* data is suitable for fifo */
  757. new_z1 = le16_to_cpu(*z1t) + count;
  758. /* new buffer Position */
  759. if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
  760. new_z1 -= B_FIFO_SIZE; /* buffer wrap */
  761. src = bch->tx_skb->data + bch->tx_idx;
  762. /* source pointer */
  763. dst = bdata + (le16_to_cpu(*z1t) - B_SUB_VAL);
  764. maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(*z1t);
  765. /* end of fifo */
  766. if (bch->debug & DEBUG_HW_BFIFO)
  767. printk(KERN_DEBUG "hfcpci_FFt fcnt(%d) "
  768. "maxl(%d) nz1(%x) dst(%p)\n",
  769. fcnt, maxlen, new_z1, dst);
  770. fcnt += count;
  771. bch->tx_idx += count;
  772. if (maxlen > count)
  773. maxlen = count; /* limit size */
  774. memcpy(dst, src, maxlen); /* first copy */
  775. count -= maxlen; /* remaining bytes */
  776. if (count) {
  777. dst = bdata; /* start of buffer */
  778. src += maxlen; /* new position */
  779. memcpy(dst, src, count);
  780. }
  781. *z1t = cpu_to_le16(new_z1); /* now send data */
  782. if (bch->tx_idx < bch->tx_skb->len)
  783. return;
  784. /* send confirm, on trans, free on hdlc. */
  785. if (test_bit(FLG_TRANSPARENT, &bch->Flags))
  786. confirm_Bsend(bch);
  787. dev_kfree_skb(bch->tx_skb);
  788. if (get_next_bframe(bch))
  789. goto next_t_frame;
  790. return;
  791. }
  792. if (bch->debug & DEBUG_HW_BCHANNEL)
  793. printk(KERN_DEBUG
  794. "%s: ch(%x) f1(%d) f2(%d) z1(f1)(%x)\n",
  795. __func__, bch->nr, bz->f1, bz->f2,
  796. bz->za[bz->f1].z1);
  797. fcnt = bz->f1 - bz->f2; /* frame count actually buffered */
  798. if (fcnt < 0)
  799. fcnt += (MAX_B_FRAMES + 1); /* if wrap around */
  800. if (fcnt > (MAX_B_FRAMES - 1)) {
  801. if (bch->debug & DEBUG_HW_BCHANNEL)
  802. printk(KERN_DEBUG
  803. "hfcpci_fill_Bfifo more as 14 frames\n");
  804. return;
  805. }
  806. /* now determine free bytes in FIFO buffer */
  807. maxlen = le16_to_cpu(bz->za[bz->f2].z2) -
  808. le16_to_cpu(bz->za[bz->f1].z1) - 1;
  809. if (maxlen <= 0)
  810. maxlen += B_FIFO_SIZE; /* count now contains available bytes */
  811. if (bch->debug & DEBUG_HW_BCHANNEL)
  812. printk(KERN_DEBUG "hfcpci_fill_fifo ch(%x) count(%d/%d)\n",
  813. bch->nr, count, maxlen);
  814. if (maxlen < count) {
  815. if (bch->debug & DEBUG_HW_BCHANNEL)
  816. printk(KERN_DEBUG "hfcpci_fill_fifo no fifo mem\n");
  817. return;
  818. }
  819. new_z1 = le16_to_cpu(bz->za[bz->f1].z1) + count;
  820. /* new buffer Position */
  821. if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))
  822. new_z1 -= B_FIFO_SIZE; /* buffer wrap */
  823. new_f1 = ((bz->f1 + 1) & MAX_B_FRAMES);
  824. src = bch->tx_skb->data + bch->tx_idx; /* source pointer */
  825. dst = bdata + (le16_to_cpu(bz->za[bz->f1].z1) - B_SUB_VAL);
  826. maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(bz->za[bz->f1].z1);
  827. /* end fifo */
  828. if (maxlen > count)
  829. maxlen = count; /* limit size */
  830. memcpy(dst, src, maxlen); /* first copy */
  831. count -= maxlen; /* remaining bytes */
  832. if (count) {
  833. dst = bdata; /* start of buffer */
  834. src += maxlen; /* new position */
  835. memcpy(dst, src, count);
  836. }
  837. bz->za[new_f1].z1 = cpu_to_le16(new_z1); /* for next buffer */
  838. bz->f1 = new_f1; /* next frame */
  839. dev_kfree_skb(bch->tx_skb);
  840. get_next_bframe(bch);
  841. }
  842. /*
  843. * handle L1 state changes TE
  844. */
  845. static void
  846. ph_state_te(struct dchannel *dch)
  847. {
  848. if (dch->debug)
  849. printk(KERN_DEBUG "%s: TE newstate %x\n",
  850. __func__, dch->state);
  851. switch (dch->state) {
  852. case 0:
  853. l1_event(dch->l1, HW_RESET_IND);
  854. break;
  855. case 3:
  856. l1_event(dch->l1, HW_DEACT_IND);
  857. break;
  858. case 5:
  859. case 8:
  860. l1_event(dch->l1, ANYSIGNAL);
  861. break;
  862. case 6:
  863. l1_event(dch->l1, INFO2);
  864. break;
  865. case 7:
  866. l1_event(dch->l1, INFO4_P8);
  867. break;
  868. }
  869. }
  870. /*
  871. * handle L1 state changes NT
  872. */
  873. static void
  874. handle_nt_timer3(struct dchannel *dch) {
  875. struct hfc_pci *hc = dch->hw;
  876. test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
  877. hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
  878. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  879. hc->hw.nt_timer = 0;
  880. test_and_set_bit(FLG_ACTIVE, &dch->Flags);
  881. if (test_bit(HFC_CFG_MASTER, &hc->cfg))
  882. hc->hw.mst_m |= HFCPCI_MASTER;
  883. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  884. _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
  885. MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
  886. }
  887. static void
  888. ph_state_nt(struct dchannel *dch)
  889. {
  890. struct hfc_pci *hc = dch->hw;
  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. (void) 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 already
  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_VDEVICE(CCD, PCI_DEVICE_ID_CCD_2BD0),
  2060. (unsigned long) &hfc_map[0] },
  2061. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B000),
  2062. (unsigned long) &hfc_map[1] },
  2063. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B006),
  2064. (unsigned long) &hfc_map[2] },
  2065. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B007),
  2066. (unsigned long) &hfc_map[3] },
  2067. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B008),
  2068. (unsigned long) &hfc_map[4] },
  2069. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B009),
  2070. (unsigned long) &hfc_map[5] },
  2071. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00A),
  2072. (unsigned long) &hfc_map[6] },
  2073. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00B),
  2074. (unsigned long) &hfc_map[7] },
  2075. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00C),
  2076. (unsigned long) &hfc_map[8] },
  2077. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B100),
  2078. (unsigned long) &hfc_map[9] },
  2079. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B700),
  2080. (unsigned long) &hfc_map[10] },
  2081. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B701),
  2082. (unsigned long) &hfc_map[11] },
  2083. { PCI_VDEVICE(ABOCOM, PCI_DEVICE_ID_ABOCOM_2BD1),
  2084. (unsigned long) &hfc_map[12] },
  2085. { PCI_VDEVICE(ASUSTEK, PCI_DEVICE_ID_ASUSTEK_0675),
  2086. (unsigned long) &hfc_map[13] },
  2087. { PCI_VDEVICE(BERKOM, PCI_DEVICE_ID_BERKOM_T_CONCEPT),
  2088. (unsigned long) &hfc_map[14] },
  2089. { PCI_VDEVICE(BERKOM, PCI_DEVICE_ID_BERKOM_A1T),
  2090. (unsigned long) &hfc_map[15] },
  2091. { PCI_VDEVICE(ANIGMA, PCI_DEVICE_ID_ANIGMA_MC145575),
  2092. (unsigned long) &hfc_map[16] },
  2093. { PCI_VDEVICE(ZOLTRIX, PCI_DEVICE_ID_ZOLTRIX_2BD0),
  2094. (unsigned long) &hfc_map[17] },
  2095. { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_E),
  2096. (unsigned long) &hfc_map[18] },
  2097. { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_E),
  2098. (unsigned long) &hfc_map[19] },
  2099. { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_A),
  2100. (unsigned long) &hfc_map[20] },
  2101. { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_A),
  2102. (unsigned long) &hfc_map[21] },
  2103. { PCI_VDEVICE(SITECOM, PCI_DEVICE_ID_SITECOM_DC105V2),
  2104. (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);