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 <linux/slab.h>
  51. #include "hfc_pci.h"
  52. static const char *hfcpci_revision = "2.0";
  53. static int HFC_cnt;
  54. static uint debug;
  55. static uint poll, tics;
  56. static struct timer_list hfc_tl;
  57. static unsigned long hfc_jiffies;
  58. MODULE_AUTHOR("Karsten Keil");
  59. MODULE_LICENSE("GPL");
  60. module_param(debug, uint, S_IRUGO | S_IWUSR);
  61. module_param(poll, uint, S_IRUGO | S_IWUSR);
  62. enum {
  63. HFC_CCD_2BD0,
  64. HFC_CCD_B000,
  65. HFC_CCD_B006,
  66. HFC_CCD_B007,
  67. HFC_CCD_B008,
  68. HFC_CCD_B009,
  69. HFC_CCD_B00A,
  70. HFC_CCD_B00B,
  71. HFC_CCD_B00C,
  72. HFC_CCD_B100,
  73. HFC_CCD_B700,
  74. HFC_CCD_B701,
  75. HFC_ASUS_0675,
  76. HFC_BERKOM_A1T,
  77. HFC_BERKOM_TCONCEPT,
  78. HFC_ANIGMA_MC145575,
  79. HFC_ZOLTRIX_2BD0,
  80. HFC_DIGI_DF_M_IOM2_E,
  81. HFC_DIGI_DF_M_E,
  82. HFC_DIGI_DF_M_IOM2_A,
  83. HFC_DIGI_DF_M_A,
  84. HFC_ABOCOM_2BD1,
  85. HFC_SITECOM_DC105V2,
  86. };
  87. struct hfcPCI_hw {
  88. unsigned char cirm;
  89. unsigned char ctmt;
  90. unsigned char clkdel;
  91. unsigned char states;
  92. unsigned char conn;
  93. unsigned char mst_m;
  94. unsigned char int_m1;
  95. unsigned char int_m2;
  96. unsigned char sctrl;
  97. unsigned char sctrl_r;
  98. unsigned char sctrl_e;
  99. unsigned char trm;
  100. unsigned char fifo_en;
  101. unsigned char bswapped;
  102. unsigned char protocol;
  103. int nt_timer;
  104. unsigned char __iomem *pci_io; /* start of PCI IO memory */
  105. dma_addr_t dmahandle;
  106. void *fifos; /* FIFO memory */
  107. int last_bfifo_cnt[2];
  108. /* marker saving last b-fifo frame count */
  109. struct timer_list timer;
  110. };
  111. #define HFC_CFG_MASTER 1
  112. #define HFC_CFG_SLAVE 2
  113. #define HFC_CFG_PCM 3
  114. #define HFC_CFG_2HFC 4
  115. #define HFC_CFG_SLAVEHFC 5
  116. #define HFC_CFG_NEG_F0 6
  117. #define HFC_CFG_SW_DD_DU 7
  118. #define FLG_HFC_TIMER_T1 16
  119. #define FLG_HFC_TIMER_T3 17
  120. #define NT_T1_COUNT 1120 /* number of 3.125ms interrupts (3.5s) */
  121. #define NT_T3_COUNT 31 /* number of 3.125ms interrupts (97 ms) */
  122. #define CLKDEL_TE 0x0e /* CLKDEL in TE mode */
  123. #define CLKDEL_NT 0x6c /* CLKDEL in NT mode */
  124. struct hfc_pci {
  125. u_char subtype;
  126. u_char chanlimit;
  127. u_char initdone;
  128. u_long cfg;
  129. u_int irq;
  130. u_int irqcnt;
  131. struct pci_dev *pdev;
  132. struct hfcPCI_hw hw;
  133. spinlock_t lock; /* card lock */
  134. struct dchannel dch;
  135. struct bchannel bch[2];
  136. };
  137. /* Interface functions */
  138. static void
  139. enable_hwirq(struct hfc_pci *hc)
  140. {
  141. hc->hw.int_m2 |= HFCPCI_IRQ_ENABLE;
  142. Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);
  143. }
  144. static void
  145. disable_hwirq(struct hfc_pci *hc)
  146. {
  147. hc->hw.int_m2 &= ~((u_char)HFCPCI_IRQ_ENABLE);
  148. Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);
  149. }
  150. /*
  151. * free hardware resources used by driver
  152. */
  153. static void
  154. release_io_hfcpci(struct hfc_pci *hc)
  155. {
  156. /* disable memory mapped ports + busmaster */
  157. pci_write_config_word(hc->pdev, PCI_COMMAND, 0);
  158. del_timer(&hc->hw.timer);
  159. pci_free_consistent(hc->pdev, 0x8000, hc->hw.fifos, hc->hw.dmahandle);
  160. iounmap(hc->hw.pci_io);
  161. }
  162. /*
  163. * set mode (NT or TE)
  164. */
  165. static void
  166. hfcpci_setmode(struct hfc_pci *hc)
  167. {
  168. if (hc->hw.protocol == ISDN_P_NT_S0) {
  169. hc->hw.clkdel = CLKDEL_NT; /* ST-Bit delay for NT-Mode */
  170. hc->hw.sctrl |= SCTRL_MODE_NT; /* NT-MODE */
  171. hc->hw.states = 1; /* G1 */
  172. } else {
  173. hc->hw.clkdel = CLKDEL_TE; /* ST-Bit delay for TE-Mode */
  174. hc->hw.sctrl &= ~SCTRL_MODE_NT; /* TE-MODE */
  175. hc->hw.states = 2; /* F2 */
  176. }
  177. Write_hfc(hc, HFCPCI_CLKDEL, hc->hw.clkdel);
  178. Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | hc->hw.states);
  179. udelay(10);
  180. Write_hfc(hc, HFCPCI_STATES, hc->hw.states | 0x40); /* Deactivate */
  181. Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);
  182. }
  183. /*
  184. * function called to reset the HFC PCI chip. A complete software reset of chip
  185. * and fifos is done.
  186. */
  187. static void
  188. reset_hfcpci(struct hfc_pci *hc)
  189. {
  190. u_char val;
  191. int cnt = 0;
  192. printk(KERN_DEBUG "reset_hfcpci: entered\n");
  193. val = Read_hfc(hc, HFCPCI_CHIP_ID);
  194. printk(KERN_INFO "HFC_PCI: resetting HFC ChipId(%x)\n", val);
  195. /* enable memory mapped ports, disable busmaster */
  196. pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);
  197. disable_hwirq(hc);
  198. /* enable memory ports + busmaster */
  199. pci_write_config_word(hc->pdev, PCI_COMMAND,
  200. PCI_ENA_MEMIO + PCI_ENA_MASTER);
  201. val = Read_hfc(hc, HFCPCI_STATUS);
  202. printk(KERN_DEBUG "HFC-PCI status(%x) before reset\n", val);
  203. hc->hw.cirm = HFCPCI_RESET; /* Reset On */
  204. Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
  205. set_current_state(TASK_UNINTERRUPTIBLE);
  206. mdelay(10); /* Timeout 10ms */
  207. hc->hw.cirm = 0; /* Reset Off */
  208. Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
  209. val = Read_hfc(hc, HFCPCI_STATUS);
  210. printk(KERN_DEBUG "HFC-PCI status(%x) after reset\n", val);
  211. while (cnt < 50000) { /* max 50000 us */
  212. udelay(5);
  213. cnt += 5;
  214. val = Read_hfc(hc, HFCPCI_STATUS);
  215. if (!(val & 2))
  216. break;
  217. }
  218. printk(KERN_DEBUG "HFC-PCI status(%x) after %dus\n", val, cnt);
  219. hc->hw.fifo_en = 0x30; /* only D fifos enabled */
  220. hc->hw.bswapped = 0; /* no exchange */
  221. hc->hw.ctmt = HFCPCI_TIM3_125 | HFCPCI_AUTO_TIMER;
  222. hc->hw.trm = HFCPCI_BTRANS_THRESMASK; /* no echo connect , threshold */
  223. hc->hw.sctrl = 0x40; /* set tx_lo mode, error in datasheet ! */
  224. hc->hw.sctrl_r = 0;
  225. hc->hw.sctrl_e = HFCPCI_AUTO_AWAKE; /* S/T Auto awake */
  226. hc->hw.mst_m = 0;
  227. if (test_bit(HFC_CFG_MASTER, &hc->cfg))
  228. hc->hw.mst_m |= HFCPCI_MASTER; /* HFC Master Mode */
  229. if (test_bit(HFC_CFG_NEG_F0, &hc->cfg))
  230. hc->hw.mst_m |= HFCPCI_F0_NEGATIV;
  231. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  232. Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
  233. Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);
  234. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
  235. hc->hw.int_m1 = HFCPCI_INTS_DTRANS | HFCPCI_INTS_DREC |
  236. HFCPCI_INTS_L1STATE | HFCPCI_INTS_TIMER;
  237. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  238. /* Clear already pending ints */
  239. val = Read_hfc(hc, HFCPCI_INT_S1);
  240. /* set NT/TE mode */
  241. hfcpci_setmode(hc);
  242. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  243. Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
  244. /*
  245. * Init GCI/IOM2 in master mode
  246. * Slots 0 and 1 are set for B-chan 1 and 2
  247. * D- and monitor/CI channel are not enabled
  248. * STIO1 is used as output for data, B1+B2 from ST->IOM+HFC
  249. * STIO2 is used as data input, B1+B2 from IOM->ST
  250. * ST B-channel send disabled -> continuous 1s
  251. * The IOM slots are always enabled
  252. */
  253. if (test_bit(HFC_CFG_PCM, &hc->cfg)) {
  254. /* set data flow directions: connect B1,B2: HFC to/from PCM */
  255. hc->hw.conn = 0x09;
  256. } else {
  257. hc->hw.conn = 0x36; /* set data flow directions */
  258. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {
  259. Write_hfc(hc, HFCPCI_B1_SSL, 0xC0);
  260. Write_hfc(hc, HFCPCI_B2_SSL, 0xC1);
  261. Write_hfc(hc, HFCPCI_B1_RSL, 0xC0);
  262. Write_hfc(hc, HFCPCI_B2_RSL, 0xC1);
  263. } else {
  264. Write_hfc(hc, HFCPCI_B1_SSL, 0x80);
  265. Write_hfc(hc, HFCPCI_B2_SSL, 0x81);
  266. Write_hfc(hc, HFCPCI_B1_RSL, 0x80);
  267. Write_hfc(hc, HFCPCI_B2_RSL, 0x81);
  268. }
  269. }
  270. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  271. val = Read_hfc(hc, HFCPCI_INT_S2);
  272. }
  273. /*
  274. * Timer function called when kernel timer expires
  275. */
  276. static void
  277. hfcpci_Timer(struct hfc_pci *hc)
  278. {
  279. hc->hw.timer.expires = jiffies + 75;
  280. /* WD RESET */
  281. /*
  282. * WriteReg(hc, HFCD_DATA, HFCD_CTMT, hc->hw.ctmt | 0x80);
  283. * add_timer(&hc->hw.timer);
  284. */
  285. }
  286. /*
  287. * select a b-channel entry matching and active
  288. */
  289. static struct bchannel *
  290. Sel_BCS(struct hfc_pci *hc, int channel)
  291. {
  292. if (test_bit(FLG_ACTIVE, &hc->bch[0].Flags) &&
  293. (hc->bch[0].nr & channel))
  294. return &hc->bch[0];
  295. else if (test_bit(FLG_ACTIVE, &hc->bch[1].Flags) &&
  296. (hc->bch[1].nr & channel))
  297. return &hc->bch[1];
  298. else
  299. return NULL;
  300. }
  301. /*
  302. * clear the desired B-channel rx fifo
  303. */
  304. static void
  305. hfcpci_clear_fifo_rx(struct hfc_pci *hc, int fifo)
  306. {
  307. u_char fifo_state;
  308. struct bzfifo *bzr;
  309. if (fifo) {
  310. bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;
  311. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2RX;
  312. } else {
  313. bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;
  314. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1RX;
  315. }
  316. if (fifo_state)
  317. hc->hw.fifo_en ^= fifo_state;
  318. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  319. hc->hw.last_bfifo_cnt[fifo] = 0;
  320. bzr->f1 = MAX_B_FRAMES;
  321. bzr->f2 = bzr->f1; /* init F pointers to remain constant */
  322. bzr->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);
  323. bzr->za[MAX_B_FRAMES].z2 = cpu_to_le16(
  324. le16_to_cpu(bzr->za[MAX_B_FRAMES].z1));
  325. if (fifo_state)
  326. hc->hw.fifo_en |= fifo_state;
  327. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  328. }
  329. /*
  330. * clear the desired B-channel tx fifo
  331. */
  332. static void hfcpci_clear_fifo_tx(struct hfc_pci *hc, int fifo)
  333. {
  334. u_char fifo_state;
  335. struct bzfifo *bzt;
  336. if (fifo) {
  337. bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;
  338. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2TX;
  339. } else {
  340. bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;
  341. fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1TX;
  342. }
  343. if (fifo_state)
  344. hc->hw.fifo_en ^= fifo_state;
  345. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  346. if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)
  347. printk(KERN_DEBUG "hfcpci_clear_fifo_tx%d f1(%x) f2(%x) "
  348. "z1(%x) z2(%x) state(%x)\n",
  349. fifo, bzt->f1, bzt->f2,
  350. le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),
  351. le16_to_cpu(bzt->za[MAX_B_FRAMES].z2),
  352. fifo_state);
  353. bzt->f2 = MAX_B_FRAMES;
  354. bzt->f1 = bzt->f2; /* init F pointers to remain constant */
  355. bzt->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);
  356. bzt->za[MAX_B_FRAMES].z2 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 2);
  357. if (fifo_state)
  358. hc->hw.fifo_en |= fifo_state;
  359. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  360. if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)
  361. printk(KERN_DEBUG
  362. "hfcpci_clear_fifo_tx%d f1(%x) f2(%x) z1(%x) z2(%x)\n",
  363. fifo, bzt->f1, bzt->f2,
  364. le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),
  365. le16_to_cpu(bzt->za[MAX_B_FRAMES].z2));
  366. }
  367. /*
  368. * read a complete B-frame out of the buffer
  369. */
  370. static void
  371. hfcpci_empty_bfifo(struct bchannel *bch, struct bzfifo *bz,
  372. u_char *bdata, int count)
  373. {
  374. u_char *ptr, *ptr1, new_f2;
  375. int maxlen, new_z2;
  376. struct zt *zp;
  377. if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))
  378. printk(KERN_DEBUG "hfcpci_empty_fifo\n");
  379. zp = &bz->za[bz->f2]; /* point to Z-Regs */
  380. new_z2 = le16_to_cpu(zp->z2) + count; /* new position in fifo */
  381. if (new_z2 >= (B_FIFO_SIZE + B_SUB_VAL))
  382. new_z2 -= B_FIFO_SIZE; /* buffer wrap */
  383. new_f2 = (bz->f2 + 1) & MAX_B_FRAMES;
  384. if ((count > MAX_DATA_SIZE + 3) || (count < 4) ||
  385. (*(bdata + (le16_to_cpu(zp->z1) - B_SUB_VAL)))) {
  386. if (bch->debug & DEBUG_HW)
  387. printk(KERN_DEBUG "hfcpci_empty_fifo: incoming packet "
  388. "invalid length %d or crc\n", count);
  389. #ifdef ERROR_STATISTIC
  390. bch->err_inv++;
  391. #endif
  392. bz->za[new_f2].z2 = cpu_to_le16(new_z2);
  393. bz->f2 = new_f2; /* next buffer */
  394. } else {
  395. bch->rx_skb = mI_alloc_skb(count - 3, GFP_ATOMIC);
  396. if (!bch->rx_skb) {
  397. printk(KERN_WARNING "HFCPCI: receive out of memory\n");
  398. return;
  399. }
  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. if (dch->debug)
  891. printk(KERN_DEBUG "%s: NT newstate %x\n",
  892. __func__, dch->state);
  893. switch (dch->state) {
  894. case 2:
  895. if (hc->hw.nt_timer < 0) {
  896. hc->hw.nt_timer = 0;
  897. test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
  898. test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
  899. hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
  900. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  901. /* Clear already pending ints */
  902. (void) Read_hfc(hc, HFCPCI_INT_S1);
  903. Write_hfc(hc, HFCPCI_STATES, 4 | HFCPCI_LOAD_STATE);
  904. udelay(10);
  905. Write_hfc(hc, HFCPCI_STATES, 4);
  906. dch->state = 4;
  907. } else if (hc->hw.nt_timer == 0) {
  908. hc->hw.int_m1 |= HFCPCI_INTS_TIMER;
  909. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  910. hc->hw.nt_timer = NT_T1_COUNT;
  911. hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;
  912. hc->hw.ctmt |= HFCPCI_TIM3_125;
  913. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |
  914. HFCPCI_CLTIMER);
  915. test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
  916. test_and_set_bit(FLG_HFC_TIMER_T1, &dch->Flags);
  917. /* allow G2 -> G3 transition */
  918. Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);
  919. } else {
  920. Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);
  921. }
  922. break;
  923. case 1:
  924. hc->hw.nt_timer = 0;
  925. test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
  926. test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
  927. hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
  928. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  929. test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
  930. hc->hw.mst_m &= ~HFCPCI_MASTER;
  931. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  932. test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
  933. _queue_data(&dch->dev.D, PH_DEACTIVATE_IND,
  934. MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
  935. break;
  936. case 4:
  937. hc->hw.nt_timer = 0;
  938. test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);
  939. test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
  940. hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
  941. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  942. break;
  943. case 3:
  944. if (!test_and_set_bit(FLG_HFC_TIMER_T3, &dch->Flags)) {
  945. if (!test_and_clear_bit(FLG_L2_ACTIVATED,
  946. &dch->Flags)) {
  947. handle_nt_timer3(dch);
  948. break;
  949. }
  950. test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);
  951. hc->hw.int_m1 |= HFCPCI_INTS_TIMER;
  952. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  953. hc->hw.nt_timer = NT_T3_COUNT;
  954. hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;
  955. hc->hw.ctmt |= HFCPCI_TIM3_125;
  956. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |
  957. HFCPCI_CLTIMER);
  958. }
  959. break;
  960. }
  961. }
  962. static void
  963. ph_state(struct dchannel *dch)
  964. {
  965. struct hfc_pci *hc = dch->hw;
  966. if (hc->hw.protocol == ISDN_P_NT_S0) {
  967. if (test_bit(FLG_HFC_TIMER_T3, &dch->Flags) &&
  968. hc->hw.nt_timer < 0)
  969. handle_nt_timer3(dch);
  970. else
  971. ph_state_nt(dch);
  972. } else
  973. ph_state_te(dch);
  974. }
  975. /*
  976. * Layer 1 callback function
  977. */
  978. static int
  979. hfc_l1callback(struct dchannel *dch, u_int cmd)
  980. {
  981. struct hfc_pci *hc = dch->hw;
  982. switch (cmd) {
  983. case INFO3_P8:
  984. case INFO3_P10:
  985. if (test_bit(HFC_CFG_MASTER, &hc->cfg))
  986. hc->hw.mst_m |= HFCPCI_MASTER;
  987. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  988. break;
  989. case HW_RESET_REQ:
  990. Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | 3);
  991. /* HFC ST 3 */
  992. udelay(6);
  993. Write_hfc(hc, HFCPCI_STATES, 3); /* HFC ST 2 */
  994. if (test_bit(HFC_CFG_MASTER, &hc->cfg))
  995. hc->hw.mst_m |= HFCPCI_MASTER;
  996. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  997. Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |
  998. HFCPCI_DO_ACTION);
  999. l1_event(dch->l1, HW_POWERUP_IND);
  1000. break;
  1001. case HW_DEACT_REQ:
  1002. hc->hw.mst_m &= ~HFCPCI_MASTER;
  1003. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  1004. skb_queue_purge(&dch->squeue);
  1005. if (dch->tx_skb) {
  1006. dev_kfree_skb(dch->tx_skb);
  1007. dch->tx_skb = NULL;
  1008. }
  1009. dch->tx_idx = 0;
  1010. if (dch->rx_skb) {
  1011. dev_kfree_skb(dch->rx_skb);
  1012. dch->rx_skb = NULL;
  1013. }
  1014. test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
  1015. if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
  1016. del_timer(&dch->timer);
  1017. break;
  1018. case HW_POWERUP_REQ:
  1019. Write_hfc(hc, HFCPCI_STATES, HFCPCI_DO_ACTION);
  1020. break;
  1021. case PH_ACTIVATE_IND:
  1022. test_and_set_bit(FLG_ACTIVE, &dch->Flags);
  1023. _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
  1024. GFP_ATOMIC);
  1025. break;
  1026. case PH_DEACTIVATE_IND:
  1027. test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
  1028. _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
  1029. GFP_ATOMIC);
  1030. break;
  1031. default:
  1032. if (dch->debug & DEBUG_HW)
  1033. printk(KERN_DEBUG "%s: unknown command %x\n",
  1034. __func__, cmd);
  1035. return -1;
  1036. }
  1037. return 0;
  1038. }
  1039. /*
  1040. * Interrupt handler
  1041. */
  1042. static inline void
  1043. tx_birq(struct bchannel *bch)
  1044. {
  1045. if (bch->tx_skb && bch->tx_idx < bch->tx_skb->len)
  1046. hfcpci_fill_fifo(bch);
  1047. else {
  1048. if (bch->tx_skb)
  1049. dev_kfree_skb(bch->tx_skb);
  1050. if (get_next_bframe(bch))
  1051. hfcpci_fill_fifo(bch);
  1052. }
  1053. }
  1054. static inline void
  1055. tx_dirq(struct dchannel *dch)
  1056. {
  1057. if (dch->tx_skb && dch->tx_idx < dch->tx_skb->len)
  1058. hfcpci_fill_dfifo(dch->hw);
  1059. else {
  1060. if (dch->tx_skb)
  1061. dev_kfree_skb(dch->tx_skb);
  1062. if (get_next_dframe(dch))
  1063. hfcpci_fill_dfifo(dch->hw);
  1064. }
  1065. }
  1066. static irqreturn_t
  1067. hfcpci_int(int intno, void *dev_id)
  1068. {
  1069. struct hfc_pci *hc = dev_id;
  1070. u_char exval;
  1071. struct bchannel *bch;
  1072. u_char val, stat;
  1073. spin_lock(&hc->lock);
  1074. if (!(hc->hw.int_m2 & 0x08)) {
  1075. spin_unlock(&hc->lock);
  1076. return IRQ_NONE; /* not initialised */
  1077. }
  1078. stat = Read_hfc(hc, HFCPCI_STATUS);
  1079. if (HFCPCI_ANYINT & stat) {
  1080. val = Read_hfc(hc, HFCPCI_INT_S1);
  1081. if (hc->dch.debug & DEBUG_HW_DCHANNEL)
  1082. printk(KERN_DEBUG
  1083. "HFC-PCI: stat(%02x) s1(%02x)\n", stat, val);
  1084. } else {
  1085. /* shared */
  1086. spin_unlock(&hc->lock);
  1087. return IRQ_NONE;
  1088. }
  1089. hc->irqcnt++;
  1090. if (hc->dch.debug & DEBUG_HW_DCHANNEL)
  1091. printk(KERN_DEBUG "HFC-PCI irq %x\n", val);
  1092. val &= hc->hw.int_m1;
  1093. if (val & 0x40) { /* state machine irq */
  1094. exval = Read_hfc(hc, HFCPCI_STATES) & 0xf;
  1095. if (hc->dch.debug & DEBUG_HW_DCHANNEL)
  1096. printk(KERN_DEBUG "ph_state chg %d->%d\n",
  1097. hc->dch.state, exval);
  1098. hc->dch.state = exval;
  1099. schedule_event(&hc->dch, FLG_PHCHANGE);
  1100. val &= ~0x40;
  1101. }
  1102. if (val & 0x80) { /* timer irq */
  1103. if (hc->hw.protocol == ISDN_P_NT_S0) {
  1104. if ((--hc->hw.nt_timer) < 0)
  1105. schedule_event(&hc->dch, FLG_PHCHANGE);
  1106. }
  1107. val &= ~0x80;
  1108. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt | HFCPCI_CLTIMER);
  1109. }
  1110. if (val & 0x08) { /* B1 rx */
  1111. bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
  1112. if (bch)
  1113. main_rec_hfcpci(bch);
  1114. else if (hc->dch.debug)
  1115. printk(KERN_DEBUG "hfcpci spurious 0x08 IRQ\n");
  1116. }
  1117. if (val & 0x10) { /* B2 rx */
  1118. bch = Sel_BCS(hc, 2);
  1119. if (bch)
  1120. main_rec_hfcpci(bch);
  1121. else if (hc->dch.debug)
  1122. printk(KERN_DEBUG "hfcpci spurious 0x10 IRQ\n");
  1123. }
  1124. if (val & 0x01) { /* B1 tx */
  1125. bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
  1126. if (bch)
  1127. tx_birq(bch);
  1128. else if (hc->dch.debug)
  1129. printk(KERN_DEBUG "hfcpci spurious 0x01 IRQ\n");
  1130. }
  1131. if (val & 0x02) { /* B2 tx */
  1132. bch = Sel_BCS(hc, 2);
  1133. if (bch)
  1134. tx_birq(bch);
  1135. else if (hc->dch.debug)
  1136. printk(KERN_DEBUG "hfcpci spurious 0x02 IRQ\n");
  1137. }
  1138. if (val & 0x20) /* D rx */
  1139. receive_dmsg(hc);
  1140. if (val & 0x04) { /* D tx */
  1141. if (test_and_clear_bit(FLG_BUSY_TIMER, &hc->dch.Flags))
  1142. del_timer(&hc->dch.timer);
  1143. tx_dirq(&hc->dch);
  1144. }
  1145. spin_unlock(&hc->lock);
  1146. return IRQ_HANDLED;
  1147. }
  1148. /*
  1149. * timer callback for D-chan busy resolution. Currently no function
  1150. */
  1151. static void
  1152. hfcpci_dbusy_timer(struct hfc_pci *hc)
  1153. {
  1154. }
  1155. /*
  1156. * activate/deactivate hardware for selected channels and mode
  1157. */
  1158. static int
  1159. mode_hfcpci(struct bchannel *bch, int bc, int protocol)
  1160. {
  1161. struct hfc_pci *hc = bch->hw;
  1162. int fifo2;
  1163. u_char rx_slot = 0, tx_slot = 0, pcm_mode;
  1164. if (bch->debug & DEBUG_HW_BCHANNEL)
  1165. printk(KERN_DEBUG
  1166. "HFCPCI bchannel protocol %x-->%x ch %x-->%x\n",
  1167. bch->state, protocol, bch->nr, bc);
  1168. fifo2 = bc;
  1169. pcm_mode = (bc>>24) & 0xff;
  1170. if (pcm_mode) { /* PCM SLOT USE */
  1171. if (!test_bit(HFC_CFG_PCM, &hc->cfg))
  1172. printk(KERN_WARNING
  1173. "%s: pcm channel id without HFC_CFG_PCM\n",
  1174. __func__);
  1175. rx_slot = (bc>>8) & 0xff;
  1176. tx_slot = (bc>>16) & 0xff;
  1177. bc = bc & 0xff;
  1178. } else if (test_bit(HFC_CFG_PCM, &hc->cfg) && (protocol > ISDN_P_NONE))
  1179. printk(KERN_WARNING "%s: no pcm channel id but HFC_CFG_PCM\n",
  1180. __func__);
  1181. if (hc->chanlimit > 1) {
  1182. hc->hw.bswapped = 0; /* B1 and B2 normal mode */
  1183. hc->hw.sctrl_e &= ~0x80;
  1184. } else {
  1185. if (bc & 2) {
  1186. if (protocol != ISDN_P_NONE) {
  1187. hc->hw.bswapped = 1; /* B1 and B2 exchanged */
  1188. hc->hw.sctrl_e |= 0x80;
  1189. } else {
  1190. hc->hw.bswapped = 0; /* B1 and B2 normal mode */
  1191. hc->hw.sctrl_e &= ~0x80;
  1192. }
  1193. fifo2 = 1;
  1194. } else {
  1195. hc->hw.bswapped = 0; /* B1 and B2 normal mode */
  1196. hc->hw.sctrl_e &= ~0x80;
  1197. }
  1198. }
  1199. switch (protocol) {
  1200. case (-1): /* used for init */
  1201. bch->state = -1;
  1202. bch->nr = bc;
  1203. case (ISDN_P_NONE):
  1204. if (bch->state == ISDN_P_NONE)
  1205. return 0;
  1206. if (bc & 2) {
  1207. hc->hw.sctrl &= ~SCTRL_B2_ENA;
  1208. hc->hw.sctrl_r &= ~SCTRL_B2_ENA;
  1209. } else {
  1210. hc->hw.sctrl &= ~SCTRL_B1_ENA;
  1211. hc->hw.sctrl_r &= ~SCTRL_B1_ENA;
  1212. }
  1213. if (fifo2 & 2) {
  1214. hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B2;
  1215. hc->hw.int_m1 &= ~(HFCPCI_INTS_B2TRANS +
  1216. HFCPCI_INTS_B2REC);
  1217. } else {
  1218. hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B1;
  1219. hc->hw.int_m1 &= ~(HFCPCI_INTS_B1TRANS +
  1220. HFCPCI_INTS_B1REC);
  1221. }
  1222. #ifdef REVERSE_BITORDER
  1223. if (bch->nr & 2)
  1224. hc->hw.cirm &= 0x7f;
  1225. else
  1226. hc->hw.cirm &= 0xbf;
  1227. #endif
  1228. bch->state = ISDN_P_NONE;
  1229. bch->nr = bc;
  1230. test_and_clear_bit(FLG_HDLC, &bch->Flags);
  1231. test_and_clear_bit(FLG_TRANSPARENT, &bch->Flags);
  1232. break;
  1233. case (ISDN_P_B_RAW):
  1234. bch->state = protocol;
  1235. bch->nr = bc;
  1236. hfcpci_clear_fifo_rx(hc, (fifo2 & 2) ? 1 : 0);
  1237. hfcpci_clear_fifo_tx(hc, (fifo2 & 2) ? 1 : 0);
  1238. if (bc & 2) {
  1239. hc->hw.sctrl |= SCTRL_B2_ENA;
  1240. hc->hw.sctrl_r |= SCTRL_B2_ENA;
  1241. #ifdef REVERSE_BITORDER
  1242. hc->hw.cirm |= 0x80;
  1243. #endif
  1244. } else {
  1245. hc->hw.sctrl |= SCTRL_B1_ENA;
  1246. hc->hw.sctrl_r |= SCTRL_B1_ENA;
  1247. #ifdef REVERSE_BITORDER
  1248. hc->hw.cirm |= 0x40;
  1249. #endif
  1250. }
  1251. if (fifo2 & 2) {
  1252. hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;
  1253. if (!tics)
  1254. hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +
  1255. HFCPCI_INTS_B2REC);
  1256. hc->hw.ctmt |= 2;
  1257. hc->hw.conn &= ~0x18;
  1258. } else {
  1259. hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;
  1260. if (!tics)
  1261. hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +
  1262. HFCPCI_INTS_B1REC);
  1263. hc->hw.ctmt |= 1;
  1264. hc->hw.conn &= ~0x03;
  1265. }
  1266. test_and_set_bit(FLG_TRANSPARENT, &bch->Flags);
  1267. break;
  1268. case (ISDN_P_B_HDLC):
  1269. bch->state = protocol;
  1270. bch->nr = bc;
  1271. hfcpci_clear_fifo_rx(hc, (fifo2 & 2) ? 1 : 0);
  1272. hfcpci_clear_fifo_tx(hc, (fifo2 & 2) ? 1 : 0);
  1273. if (bc & 2) {
  1274. hc->hw.sctrl |= SCTRL_B2_ENA;
  1275. hc->hw.sctrl_r |= SCTRL_B2_ENA;
  1276. } else {
  1277. hc->hw.sctrl |= SCTRL_B1_ENA;
  1278. hc->hw.sctrl_r |= SCTRL_B1_ENA;
  1279. }
  1280. if (fifo2 & 2) {
  1281. hc->hw.last_bfifo_cnt[1] = 0;
  1282. hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;
  1283. hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +
  1284. HFCPCI_INTS_B2REC);
  1285. hc->hw.ctmt &= ~2;
  1286. hc->hw.conn &= ~0x18;
  1287. } else {
  1288. hc->hw.last_bfifo_cnt[0] = 0;
  1289. hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;
  1290. hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +
  1291. HFCPCI_INTS_B1REC);
  1292. hc->hw.ctmt &= ~1;
  1293. hc->hw.conn &= ~0x03;
  1294. }
  1295. test_and_set_bit(FLG_HDLC, &bch->Flags);
  1296. break;
  1297. default:
  1298. printk(KERN_DEBUG "prot not known %x\n", protocol);
  1299. return -ENOPROTOOPT;
  1300. }
  1301. if (test_bit(HFC_CFG_PCM, &hc->cfg)) {
  1302. if ((protocol == ISDN_P_NONE) ||
  1303. (protocol == -1)) { /* init case */
  1304. rx_slot = 0;
  1305. tx_slot = 0;
  1306. } else {
  1307. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {
  1308. rx_slot |= 0xC0;
  1309. tx_slot |= 0xC0;
  1310. } else {
  1311. rx_slot |= 0x80;
  1312. tx_slot |= 0x80;
  1313. }
  1314. }
  1315. if (bc & 2) {
  1316. hc->hw.conn &= 0xc7;
  1317. hc->hw.conn |= 0x08;
  1318. printk(KERN_DEBUG "%s: Write_hfc: B2_SSL 0x%x\n",
  1319. __func__, tx_slot);
  1320. printk(KERN_DEBUG "%s: Write_hfc: B2_RSL 0x%x\n",
  1321. __func__, rx_slot);
  1322. Write_hfc(hc, HFCPCI_B2_SSL, tx_slot);
  1323. Write_hfc(hc, HFCPCI_B2_RSL, rx_slot);
  1324. } else {
  1325. hc->hw.conn &= 0xf8;
  1326. hc->hw.conn |= 0x01;
  1327. printk(KERN_DEBUG "%s: Write_hfc: B1_SSL 0x%x\n",
  1328. __func__, tx_slot);
  1329. printk(KERN_DEBUG "%s: Write_hfc: B1_RSL 0x%x\n",
  1330. __func__, rx_slot);
  1331. Write_hfc(hc, HFCPCI_B1_SSL, tx_slot);
  1332. Write_hfc(hc, HFCPCI_B1_RSL, rx_slot);
  1333. }
  1334. }
  1335. Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);
  1336. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  1337. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  1338. Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);
  1339. Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
  1340. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
  1341. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1342. #ifdef REVERSE_BITORDER
  1343. Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
  1344. #endif
  1345. return 0;
  1346. }
  1347. static int
  1348. set_hfcpci_rxtest(struct bchannel *bch, int protocol, int chan)
  1349. {
  1350. struct hfc_pci *hc = bch->hw;
  1351. if (bch->debug & DEBUG_HW_BCHANNEL)
  1352. printk(KERN_DEBUG
  1353. "HFCPCI bchannel test rx protocol %x-->%x ch %x-->%x\n",
  1354. bch->state, protocol, bch->nr, chan);
  1355. if (bch->nr != chan) {
  1356. printk(KERN_DEBUG
  1357. "HFCPCI rxtest wrong channel parameter %x/%x\n",
  1358. bch->nr, chan);
  1359. return -EINVAL;
  1360. }
  1361. switch (protocol) {
  1362. case (ISDN_P_B_RAW):
  1363. bch->state = protocol;
  1364. hfcpci_clear_fifo_rx(hc, (chan & 2) ? 1 : 0);
  1365. if (chan & 2) {
  1366. hc->hw.sctrl_r |= SCTRL_B2_ENA;
  1367. hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;
  1368. if (!tics)
  1369. hc->hw.int_m1 |= HFCPCI_INTS_B2REC;
  1370. hc->hw.ctmt |= 2;
  1371. hc->hw.conn &= ~0x18;
  1372. #ifdef REVERSE_BITORDER
  1373. hc->hw.cirm |= 0x80;
  1374. #endif
  1375. } else {
  1376. hc->hw.sctrl_r |= SCTRL_B1_ENA;
  1377. hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;
  1378. if (!tics)
  1379. hc->hw.int_m1 |= HFCPCI_INTS_B1REC;
  1380. hc->hw.ctmt |= 1;
  1381. hc->hw.conn &= ~0x03;
  1382. #ifdef REVERSE_BITORDER
  1383. hc->hw.cirm |= 0x40;
  1384. #endif
  1385. }
  1386. break;
  1387. case (ISDN_P_B_HDLC):
  1388. bch->state = protocol;
  1389. hfcpci_clear_fifo_rx(hc, (chan & 2) ? 1 : 0);
  1390. if (chan & 2) {
  1391. hc->hw.sctrl_r |= SCTRL_B2_ENA;
  1392. hc->hw.last_bfifo_cnt[1] = 0;
  1393. hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;
  1394. hc->hw.int_m1 |= HFCPCI_INTS_B2REC;
  1395. hc->hw.ctmt &= ~2;
  1396. hc->hw.conn &= ~0x18;
  1397. } else {
  1398. hc->hw.sctrl_r |= SCTRL_B1_ENA;
  1399. hc->hw.last_bfifo_cnt[0] = 0;
  1400. hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;
  1401. hc->hw.int_m1 |= HFCPCI_INTS_B1REC;
  1402. hc->hw.ctmt &= ~1;
  1403. hc->hw.conn &= ~0x03;
  1404. }
  1405. break;
  1406. default:
  1407. printk(KERN_DEBUG "prot not known %x\n", protocol);
  1408. return -ENOPROTOOPT;
  1409. }
  1410. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  1411. Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);
  1412. Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);
  1413. Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);
  1414. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1415. #ifdef REVERSE_BITORDER
  1416. Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);
  1417. #endif
  1418. return 0;
  1419. }
  1420. static void
  1421. deactivate_bchannel(struct bchannel *bch)
  1422. {
  1423. struct hfc_pci *hc = bch->hw;
  1424. u_long flags;
  1425. spin_lock_irqsave(&hc->lock, flags);
  1426. mISDN_clear_bchannel(bch);
  1427. mode_hfcpci(bch, bch->nr, ISDN_P_NONE);
  1428. spin_unlock_irqrestore(&hc->lock, flags);
  1429. }
  1430. /*
  1431. * Layer 1 B-channel hardware access
  1432. */
  1433. static int
  1434. channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
  1435. {
  1436. int ret = 0;
  1437. switch (cq->op) {
  1438. case MISDN_CTRL_GETOP:
  1439. cq->op = MISDN_CTRL_FILL_EMPTY;
  1440. break;
  1441. case MISDN_CTRL_FILL_EMPTY: /* fill fifo, if empty */
  1442. test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
  1443. if (debug & DEBUG_HW_OPEN)
  1444. printk(KERN_DEBUG "%s: FILL_EMPTY request (nr=%d "
  1445. "off=%d)\n", __func__, bch->nr, !!cq->p1);
  1446. break;
  1447. default:
  1448. printk(KERN_WARNING "%s: unknown Op %x\n", __func__, cq->op);
  1449. ret = -EINVAL;
  1450. break;
  1451. }
  1452. return ret;
  1453. }
  1454. static int
  1455. hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1456. {
  1457. struct bchannel *bch = container_of(ch, struct bchannel, ch);
  1458. struct hfc_pci *hc = bch->hw;
  1459. int ret = -EINVAL;
  1460. u_long flags;
  1461. if (bch->debug & DEBUG_HW)
  1462. printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
  1463. switch (cmd) {
  1464. case HW_TESTRX_RAW:
  1465. spin_lock_irqsave(&hc->lock, flags);
  1466. ret = set_hfcpci_rxtest(bch, ISDN_P_B_RAW, (int)(long)arg);
  1467. spin_unlock_irqrestore(&hc->lock, flags);
  1468. break;
  1469. case HW_TESTRX_HDLC:
  1470. spin_lock_irqsave(&hc->lock, flags);
  1471. ret = set_hfcpci_rxtest(bch, ISDN_P_B_HDLC, (int)(long)arg);
  1472. spin_unlock_irqrestore(&hc->lock, flags);
  1473. break;
  1474. case HW_TESTRX_OFF:
  1475. spin_lock_irqsave(&hc->lock, flags);
  1476. mode_hfcpci(bch, bch->nr, ISDN_P_NONE);
  1477. spin_unlock_irqrestore(&hc->lock, flags);
  1478. ret = 0;
  1479. break;
  1480. case CLOSE_CHANNEL:
  1481. test_and_clear_bit(FLG_OPEN, &bch->Flags);
  1482. if (test_bit(FLG_ACTIVE, &bch->Flags))
  1483. deactivate_bchannel(bch);
  1484. ch->protocol = ISDN_P_NONE;
  1485. ch->peer = NULL;
  1486. module_put(THIS_MODULE);
  1487. ret = 0;
  1488. break;
  1489. case CONTROL_CHANNEL:
  1490. ret = channel_bctrl(bch, arg);
  1491. break;
  1492. default:
  1493. printk(KERN_WARNING "%s: unknown prim(%x)\n",
  1494. __func__, cmd);
  1495. }
  1496. return ret;
  1497. }
  1498. /*
  1499. * Layer2 -> Layer 1 Dchannel data
  1500. */
  1501. static int
  1502. hfcpci_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
  1503. {
  1504. struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
  1505. struct dchannel *dch = container_of(dev, struct dchannel, dev);
  1506. struct hfc_pci *hc = dch->hw;
  1507. int ret = -EINVAL;
  1508. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  1509. unsigned int id;
  1510. u_long flags;
  1511. switch (hh->prim) {
  1512. case PH_DATA_REQ:
  1513. spin_lock_irqsave(&hc->lock, flags);
  1514. ret = dchannel_senddata(dch, skb);
  1515. if (ret > 0) { /* direct TX */
  1516. id = hh->id; /* skb can be freed */
  1517. hfcpci_fill_dfifo(dch->hw);
  1518. ret = 0;
  1519. spin_unlock_irqrestore(&hc->lock, flags);
  1520. queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
  1521. } else
  1522. spin_unlock_irqrestore(&hc->lock, flags);
  1523. return ret;
  1524. case PH_ACTIVATE_REQ:
  1525. spin_lock_irqsave(&hc->lock, flags);
  1526. if (hc->hw.protocol == ISDN_P_NT_S0) {
  1527. ret = 0;
  1528. if (test_bit(HFC_CFG_MASTER, &hc->cfg))
  1529. hc->hw.mst_m |= HFCPCI_MASTER;
  1530. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  1531. if (test_bit(FLG_ACTIVE, &dch->Flags)) {
  1532. spin_unlock_irqrestore(&hc->lock, flags);
  1533. _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
  1534. MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
  1535. break;
  1536. }
  1537. test_and_set_bit(FLG_L2_ACTIVATED, &dch->Flags);
  1538. Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |
  1539. HFCPCI_DO_ACTION | 1);
  1540. } else
  1541. ret = l1_event(dch->l1, hh->prim);
  1542. spin_unlock_irqrestore(&hc->lock, flags);
  1543. break;
  1544. case PH_DEACTIVATE_REQ:
  1545. test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
  1546. spin_lock_irqsave(&hc->lock, flags);
  1547. if (hc->hw.protocol == ISDN_P_NT_S0) {
  1548. /* prepare deactivation */
  1549. Write_hfc(hc, HFCPCI_STATES, 0x40);
  1550. skb_queue_purge(&dch->squeue);
  1551. if (dch->tx_skb) {
  1552. dev_kfree_skb(dch->tx_skb);
  1553. dch->tx_skb = NULL;
  1554. }
  1555. dch->tx_idx = 0;
  1556. if (dch->rx_skb) {
  1557. dev_kfree_skb(dch->rx_skb);
  1558. dch->rx_skb = NULL;
  1559. }
  1560. test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
  1561. if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
  1562. del_timer(&dch->timer);
  1563. #ifdef FIXME
  1564. if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
  1565. dchannel_sched_event(&hc->dch, D_CLEARBUSY);
  1566. #endif
  1567. hc->hw.mst_m &= ~HFCPCI_MASTER;
  1568. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  1569. ret = 0;
  1570. } else {
  1571. ret = l1_event(dch->l1, hh->prim);
  1572. }
  1573. spin_unlock_irqrestore(&hc->lock, flags);
  1574. break;
  1575. }
  1576. if (!ret)
  1577. dev_kfree_skb(skb);
  1578. return ret;
  1579. }
  1580. /*
  1581. * Layer2 -> Layer 1 Bchannel data
  1582. */
  1583. static int
  1584. hfcpci_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
  1585. {
  1586. struct bchannel *bch = container_of(ch, struct bchannel, ch);
  1587. struct hfc_pci *hc = bch->hw;
  1588. int ret = -EINVAL;
  1589. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  1590. unsigned int id;
  1591. u_long flags;
  1592. switch (hh->prim) {
  1593. case PH_DATA_REQ:
  1594. spin_lock_irqsave(&hc->lock, flags);
  1595. ret = bchannel_senddata(bch, skb);
  1596. if (ret > 0) { /* direct TX */
  1597. id = hh->id; /* skb can be freed */
  1598. hfcpci_fill_fifo(bch);
  1599. ret = 0;
  1600. spin_unlock_irqrestore(&hc->lock, flags);
  1601. if (!test_bit(FLG_TRANSPARENT, &bch->Flags))
  1602. queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
  1603. } else
  1604. spin_unlock_irqrestore(&hc->lock, flags);
  1605. return ret;
  1606. case PH_ACTIVATE_REQ:
  1607. spin_lock_irqsave(&hc->lock, flags);
  1608. if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags))
  1609. ret = mode_hfcpci(bch, bch->nr, ch->protocol);
  1610. else
  1611. ret = 0;
  1612. spin_unlock_irqrestore(&hc->lock, flags);
  1613. if (!ret)
  1614. _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
  1615. NULL, GFP_KERNEL);
  1616. break;
  1617. case PH_DEACTIVATE_REQ:
  1618. deactivate_bchannel(bch);
  1619. _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
  1620. NULL, GFP_KERNEL);
  1621. ret = 0;
  1622. break;
  1623. }
  1624. if (!ret)
  1625. dev_kfree_skb(skb);
  1626. return ret;
  1627. }
  1628. /*
  1629. * called for card init message
  1630. */
  1631. static void
  1632. inithfcpci(struct hfc_pci *hc)
  1633. {
  1634. printk(KERN_DEBUG "inithfcpci: entered\n");
  1635. hc->dch.timer.function = (void *) hfcpci_dbusy_timer;
  1636. hc->dch.timer.data = (long) &hc->dch;
  1637. init_timer(&hc->dch.timer);
  1638. hc->chanlimit = 2;
  1639. mode_hfcpci(&hc->bch[0], 1, -1);
  1640. mode_hfcpci(&hc->bch[1], 2, -1);
  1641. }
  1642. static int
  1643. init_card(struct hfc_pci *hc)
  1644. {
  1645. int cnt = 3;
  1646. u_long flags;
  1647. printk(KERN_DEBUG "init_card: entered\n");
  1648. spin_lock_irqsave(&hc->lock, flags);
  1649. disable_hwirq(hc);
  1650. spin_unlock_irqrestore(&hc->lock, flags);
  1651. if (request_irq(hc->irq, hfcpci_int, IRQF_SHARED, "HFC PCI", hc)) {
  1652. printk(KERN_WARNING
  1653. "mISDN: couldn't get interrupt %d\n", hc->irq);
  1654. return -EIO;
  1655. }
  1656. spin_lock_irqsave(&hc->lock, flags);
  1657. reset_hfcpci(hc);
  1658. while (cnt) {
  1659. inithfcpci(hc);
  1660. /*
  1661. * Finally enable IRQ output
  1662. * this is only allowed, if an IRQ routine is already
  1663. * established for this HFC, so don't do that earlier
  1664. */
  1665. enable_hwirq(hc);
  1666. spin_unlock_irqrestore(&hc->lock, flags);
  1667. /* Timeout 80ms */
  1668. current->state = TASK_UNINTERRUPTIBLE;
  1669. schedule_timeout((80*HZ)/1000);
  1670. printk(KERN_INFO "HFC PCI: IRQ %d count %d\n",
  1671. hc->irq, hc->irqcnt);
  1672. /* now switch timer interrupt off */
  1673. spin_lock_irqsave(&hc->lock, flags);
  1674. hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;
  1675. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  1676. /* reinit mode reg */
  1677. Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);
  1678. if (!hc->irqcnt) {
  1679. printk(KERN_WARNING
  1680. "HFC PCI: IRQ(%d) getting no interrupts "
  1681. "during init %d\n", hc->irq, 4 - cnt);
  1682. if (cnt == 1)
  1683. break;
  1684. else {
  1685. reset_hfcpci(hc);
  1686. cnt--;
  1687. }
  1688. } else {
  1689. spin_unlock_irqrestore(&hc->lock, flags);
  1690. hc->initdone = 1;
  1691. return 0;
  1692. }
  1693. }
  1694. disable_hwirq(hc);
  1695. spin_unlock_irqrestore(&hc->lock, flags);
  1696. free_irq(hc->irq, hc);
  1697. return -EIO;
  1698. }
  1699. static int
  1700. channel_ctrl(struct hfc_pci *hc, struct mISDN_ctrl_req *cq)
  1701. {
  1702. int ret = 0;
  1703. u_char slot;
  1704. switch (cq->op) {
  1705. case MISDN_CTRL_GETOP:
  1706. cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
  1707. MISDN_CTRL_DISCONNECT;
  1708. break;
  1709. case MISDN_CTRL_LOOP:
  1710. /* channel 0 disabled loop */
  1711. if (cq->channel < 0 || cq->channel > 2) {
  1712. ret = -EINVAL;
  1713. break;
  1714. }
  1715. if (cq->channel & 1) {
  1716. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
  1717. slot = 0xC0;
  1718. else
  1719. slot = 0x80;
  1720. printk(KERN_DEBUG "%s: Write_hfc: B1_SSL/RSL 0x%x\n",
  1721. __func__, slot);
  1722. Write_hfc(hc, HFCPCI_B1_SSL, slot);
  1723. Write_hfc(hc, HFCPCI_B1_RSL, slot);
  1724. hc->hw.conn = (hc->hw.conn & ~7) | 6;
  1725. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1726. }
  1727. if (cq->channel & 2) {
  1728. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
  1729. slot = 0xC1;
  1730. else
  1731. slot = 0x81;
  1732. printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",
  1733. __func__, slot);
  1734. Write_hfc(hc, HFCPCI_B2_SSL, slot);
  1735. Write_hfc(hc, HFCPCI_B2_RSL, slot);
  1736. hc->hw.conn = (hc->hw.conn & ~0x38) | 0x30;
  1737. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1738. }
  1739. if (cq->channel & 3)
  1740. hc->hw.trm |= 0x80; /* enable IOM-loop */
  1741. else {
  1742. hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;
  1743. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1744. hc->hw.trm &= 0x7f; /* disable IOM-loop */
  1745. }
  1746. Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
  1747. break;
  1748. case MISDN_CTRL_CONNECT:
  1749. if (cq->channel == cq->p1) {
  1750. ret = -EINVAL;
  1751. break;
  1752. }
  1753. if (cq->channel < 1 || cq->channel > 2 ||
  1754. cq->p1 < 1 || cq->p1 > 2) {
  1755. ret = -EINVAL;
  1756. break;
  1757. }
  1758. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
  1759. slot = 0xC0;
  1760. else
  1761. slot = 0x80;
  1762. printk(KERN_DEBUG "%s: Write_hfc: B1_SSL/RSL 0x%x\n",
  1763. __func__, slot);
  1764. Write_hfc(hc, HFCPCI_B1_SSL, slot);
  1765. Write_hfc(hc, HFCPCI_B2_RSL, slot);
  1766. if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))
  1767. slot = 0xC1;
  1768. else
  1769. slot = 0x81;
  1770. printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",
  1771. __func__, slot);
  1772. Write_hfc(hc, HFCPCI_B2_SSL, slot);
  1773. Write_hfc(hc, HFCPCI_B1_RSL, slot);
  1774. hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x36;
  1775. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1776. hc->hw.trm |= 0x80;
  1777. Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);
  1778. break;
  1779. case MISDN_CTRL_DISCONNECT:
  1780. hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;
  1781. Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);
  1782. hc->hw.trm &= 0x7f; /* disable IOM-loop */
  1783. break;
  1784. default:
  1785. printk(KERN_WARNING "%s: unknown Op %x\n",
  1786. __func__, cq->op);
  1787. ret = -EINVAL;
  1788. break;
  1789. }
  1790. return ret;
  1791. }
  1792. static int
  1793. open_dchannel(struct hfc_pci *hc, struct mISDNchannel *ch,
  1794. struct channel_req *rq)
  1795. {
  1796. int err = 0;
  1797. if (debug & DEBUG_HW_OPEN)
  1798. printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
  1799. hc->dch.dev.id, __builtin_return_address(0));
  1800. if (rq->protocol == ISDN_P_NONE)
  1801. return -EINVAL;
  1802. if (rq->adr.channel == 1) {
  1803. /* TODO: E-Channel */
  1804. return -EINVAL;
  1805. }
  1806. if (!hc->initdone) {
  1807. if (rq->protocol == ISDN_P_TE_S0) {
  1808. err = create_l1(&hc->dch, hfc_l1callback);
  1809. if (err)
  1810. return err;
  1811. }
  1812. hc->hw.protocol = rq->protocol;
  1813. ch->protocol = rq->protocol;
  1814. err = init_card(hc);
  1815. if (err)
  1816. return err;
  1817. } else {
  1818. if (rq->protocol != ch->protocol) {
  1819. if (hc->hw.protocol == ISDN_P_TE_S0)
  1820. l1_event(hc->dch.l1, CLOSE_CHANNEL);
  1821. if (rq->protocol == ISDN_P_TE_S0) {
  1822. err = create_l1(&hc->dch, hfc_l1callback);
  1823. if (err)
  1824. return err;
  1825. }
  1826. hc->hw.protocol = rq->protocol;
  1827. ch->protocol = rq->protocol;
  1828. hfcpci_setmode(hc);
  1829. }
  1830. }
  1831. if (((ch->protocol == ISDN_P_NT_S0) && (hc->dch.state == 3)) ||
  1832. ((ch->protocol == ISDN_P_TE_S0) && (hc->dch.state == 7))) {
  1833. _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
  1834. 0, NULL, GFP_KERNEL);
  1835. }
  1836. rq->ch = ch;
  1837. if (!try_module_get(THIS_MODULE))
  1838. printk(KERN_WARNING "%s:cannot get module\n", __func__);
  1839. return 0;
  1840. }
  1841. static int
  1842. open_bchannel(struct hfc_pci *hc, struct channel_req *rq)
  1843. {
  1844. struct bchannel *bch;
  1845. if (rq->adr.channel > 2)
  1846. return -EINVAL;
  1847. if (rq->protocol == ISDN_P_NONE)
  1848. return -EINVAL;
  1849. bch = &hc->bch[rq->adr.channel - 1];
  1850. if (test_and_set_bit(FLG_OPEN, &bch->Flags))
  1851. return -EBUSY; /* b-channel can be only open once */
  1852. test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
  1853. bch->ch.protocol = rq->protocol;
  1854. rq->ch = &bch->ch; /* TODO: E-channel */
  1855. if (!try_module_get(THIS_MODULE))
  1856. printk(KERN_WARNING "%s:cannot get module\n", __func__);
  1857. return 0;
  1858. }
  1859. /*
  1860. * device control function
  1861. */
  1862. static int
  1863. hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1864. {
  1865. struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
  1866. struct dchannel *dch = container_of(dev, struct dchannel, dev);
  1867. struct hfc_pci *hc = dch->hw;
  1868. struct channel_req *rq;
  1869. int err = 0;
  1870. if (dch->debug & DEBUG_HW)
  1871. printk(KERN_DEBUG "%s: cmd:%x %p\n",
  1872. __func__, cmd, arg);
  1873. switch (cmd) {
  1874. case OPEN_CHANNEL:
  1875. rq = arg;
  1876. if ((rq->protocol == ISDN_P_TE_S0) ||
  1877. (rq->protocol == ISDN_P_NT_S0))
  1878. err = open_dchannel(hc, ch, rq);
  1879. else
  1880. err = open_bchannel(hc, rq);
  1881. break;
  1882. case CLOSE_CHANNEL:
  1883. if (debug & DEBUG_HW_OPEN)
  1884. printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
  1885. __func__, hc->dch.dev.id,
  1886. __builtin_return_address(0));
  1887. module_put(THIS_MODULE);
  1888. break;
  1889. case CONTROL_CHANNEL:
  1890. err = channel_ctrl(hc, arg);
  1891. break;
  1892. default:
  1893. if (dch->debug & DEBUG_HW)
  1894. printk(KERN_DEBUG "%s: unknown command %x\n",
  1895. __func__, cmd);
  1896. return -EINVAL;
  1897. }
  1898. return err;
  1899. }
  1900. static int
  1901. setup_hw(struct hfc_pci *hc)
  1902. {
  1903. void *buffer;
  1904. printk(KERN_INFO "mISDN: HFC-PCI driver %s\n", hfcpci_revision);
  1905. hc->hw.cirm = 0;
  1906. hc->dch.state = 0;
  1907. pci_set_master(hc->pdev);
  1908. if (!hc->irq) {
  1909. printk(KERN_WARNING "HFC-PCI: No IRQ for PCI card found\n");
  1910. return 1;
  1911. }
  1912. hc->hw.pci_io =
  1913. (char __iomem *)(unsigned long)hc->pdev->resource[1].start;
  1914. if (!hc->hw.pci_io) {
  1915. printk(KERN_WARNING "HFC-PCI: No IO-Mem for PCI card found\n");
  1916. return 1;
  1917. }
  1918. /* Allocate memory for FIFOS */
  1919. /* the memory needs to be on a 32k boundary within the first 4G */
  1920. pci_set_dma_mask(hc->pdev, 0xFFFF8000);
  1921. buffer = pci_alloc_consistent(hc->pdev, 0x8000, &hc->hw.dmahandle);
  1922. /* We silently assume the address is okay if nonzero */
  1923. if (!buffer) {
  1924. printk(KERN_WARNING
  1925. "HFC-PCI: Error allocating memory for FIFO!\n");
  1926. return 1;
  1927. }
  1928. hc->hw.fifos = buffer;
  1929. pci_write_config_dword(hc->pdev, 0x80, hc->hw.dmahandle);
  1930. hc->hw.pci_io = ioremap((ulong) hc->hw.pci_io, 256);
  1931. printk(KERN_INFO
  1932. "HFC-PCI: defined at mem %#lx fifo %#lx(%#lx) IRQ %d HZ %d\n",
  1933. (u_long) hc->hw.pci_io, (u_long) hc->hw.fifos,
  1934. (u_long) hc->hw.dmahandle, hc->irq, HZ);
  1935. /* enable memory mapped ports, disable busmaster */
  1936. pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);
  1937. hc->hw.int_m2 = 0;
  1938. disable_hwirq(hc);
  1939. hc->hw.int_m1 = 0;
  1940. Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);
  1941. /* At this point the needed PCI config is done */
  1942. /* fifos are still not enabled */
  1943. hc->hw.timer.function = (void *) hfcpci_Timer;
  1944. hc->hw.timer.data = (long) hc;
  1945. init_timer(&hc->hw.timer);
  1946. /* default PCM master */
  1947. test_and_set_bit(HFC_CFG_MASTER, &hc->cfg);
  1948. return 0;
  1949. }
  1950. static void
  1951. release_card(struct hfc_pci *hc) {
  1952. u_long flags;
  1953. spin_lock_irqsave(&hc->lock, flags);
  1954. hc->hw.int_m2 = 0; /* interrupt output off ! */
  1955. disable_hwirq(hc);
  1956. mode_hfcpci(&hc->bch[0], 1, ISDN_P_NONE);
  1957. mode_hfcpci(&hc->bch[1], 2, ISDN_P_NONE);
  1958. if (hc->dch.timer.function != NULL) {
  1959. del_timer(&hc->dch.timer);
  1960. hc->dch.timer.function = NULL;
  1961. }
  1962. spin_unlock_irqrestore(&hc->lock, flags);
  1963. if (hc->hw.protocol == ISDN_P_TE_S0)
  1964. l1_event(hc->dch.l1, CLOSE_CHANNEL);
  1965. if (hc->initdone)
  1966. free_irq(hc->irq, hc);
  1967. release_io_hfcpci(hc); /* must release after free_irq! */
  1968. mISDN_unregister_device(&hc->dch.dev);
  1969. mISDN_freebchannel(&hc->bch[1]);
  1970. mISDN_freebchannel(&hc->bch[0]);
  1971. mISDN_freedchannel(&hc->dch);
  1972. pci_set_drvdata(hc->pdev, NULL);
  1973. kfree(hc);
  1974. }
  1975. static int
  1976. setup_card(struct hfc_pci *card)
  1977. {
  1978. int err = -EINVAL;
  1979. u_int i;
  1980. char name[MISDN_MAX_IDLEN];
  1981. card->dch.debug = debug;
  1982. spin_lock_init(&card->lock);
  1983. mISDN_initdchannel(&card->dch, MAX_DFRAME_LEN_L1, ph_state);
  1984. card->dch.hw = card;
  1985. card->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
  1986. card->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
  1987. (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
  1988. card->dch.dev.D.send = hfcpci_l2l1D;
  1989. card->dch.dev.D.ctrl = hfc_dctrl;
  1990. card->dch.dev.nrbchan = 2;
  1991. for (i = 0; i < 2; i++) {
  1992. card->bch[i].nr = i + 1;
  1993. set_channelmap(i + 1, card->dch.dev.channelmap);
  1994. card->bch[i].debug = debug;
  1995. mISDN_initbchannel(&card->bch[i], MAX_DATA_MEM);
  1996. card->bch[i].hw = card;
  1997. card->bch[i].ch.send = hfcpci_l2l1B;
  1998. card->bch[i].ch.ctrl = hfc_bctrl;
  1999. card->bch[i].ch.nr = i + 1;
  2000. list_add(&card->bch[i].ch.list, &card->dch.dev.bchannels);
  2001. }
  2002. err = setup_hw(card);
  2003. if (err)
  2004. goto error;
  2005. snprintf(name, MISDN_MAX_IDLEN - 1, "hfc-pci.%d", HFC_cnt + 1);
  2006. err = mISDN_register_device(&card->dch.dev, &card->pdev->dev, name);
  2007. if (err)
  2008. goto error;
  2009. HFC_cnt++;
  2010. printk(KERN_INFO "HFC %d cards installed\n", HFC_cnt);
  2011. return 0;
  2012. error:
  2013. mISDN_freebchannel(&card->bch[1]);
  2014. mISDN_freebchannel(&card->bch[0]);
  2015. mISDN_freedchannel(&card->dch);
  2016. kfree(card);
  2017. return err;
  2018. }
  2019. /* private data in the PCI devices list */
  2020. struct _hfc_map {
  2021. u_int subtype;
  2022. u_int flag;
  2023. char *name;
  2024. };
  2025. static const struct _hfc_map hfc_map[] =
  2026. {
  2027. {HFC_CCD_2BD0, 0, "CCD/Billion/Asuscom 2BD0"},
  2028. {HFC_CCD_B000, 0, "Billion B000"},
  2029. {HFC_CCD_B006, 0, "Billion B006"},
  2030. {HFC_CCD_B007, 0, "Billion B007"},
  2031. {HFC_CCD_B008, 0, "Billion B008"},
  2032. {HFC_CCD_B009, 0, "Billion B009"},
  2033. {HFC_CCD_B00A, 0, "Billion B00A"},
  2034. {HFC_CCD_B00B, 0, "Billion B00B"},
  2035. {HFC_CCD_B00C, 0, "Billion B00C"},
  2036. {HFC_CCD_B100, 0, "Seyeon B100"},
  2037. {HFC_CCD_B700, 0, "Primux II S0 B700"},
  2038. {HFC_CCD_B701, 0, "Primux II S0 NT B701"},
  2039. {HFC_ABOCOM_2BD1, 0, "Abocom/Magitek 2BD1"},
  2040. {HFC_ASUS_0675, 0, "Asuscom/Askey 675"},
  2041. {HFC_BERKOM_TCONCEPT, 0, "German telekom T-Concept"},
  2042. {HFC_BERKOM_A1T, 0, "German telekom A1T"},
  2043. {HFC_ANIGMA_MC145575, 0, "Motorola MC145575"},
  2044. {HFC_ZOLTRIX_2BD0, 0, "Zoltrix 2BD0"},
  2045. {HFC_DIGI_DF_M_IOM2_E, 0,
  2046. "Digi International DataFire Micro V IOM2 (Europe)"},
  2047. {HFC_DIGI_DF_M_E, 0,
  2048. "Digi International DataFire Micro V (Europe)"},
  2049. {HFC_DIGI_DF_M_IOM2_A, 0,
  2050. "Digi International DataFire Micro V IOM2 (North America)"},
  2051. {HFC_DIGI_DF_M_A, 0,
  2052. "Digi International DataFire Micro V (North America)"},
  2053. {HFC_SITECOM_DC105V2, 0, "Sitecom Connectivity DC-105 ISDN TA"},
  2054. {},
  2055. };
  2056. static struct pci_device_id hfc_ids[] =
  2057. {
  2058. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_2BD0),
  2059. (unsigned long) &hfc_map[0] },
  2060. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B000),
  2061. (unsigned long) &hfc_map[1] },
  2062. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B006),
  2063. (unsigned long) &hfc_map[2] },
  2064. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B007),
  2065. (unsigned long) &hfc_map[3] },
  2066. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B008),
  2067. (unsigned long) &hfc_map[4] },
  2068. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B009),
  2069. (unsigned long) &hfc_map[5] },
  2070. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00A),
  2071. (unsigned long) &hfc_map[6] },
  2072. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00B),
  2073. (unsigned long) &hfc_map[7] },
  2074. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00C),
  2075. (unsigned long) &hfc_map[8] },
  2076. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B100),
  2077. (unsigned long) &hfc_map[9] },
  2078. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B700),
  2079. (unsigned long) &hfc_map[10] },
  2080. { PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B701),
  2081. (unsigned long) &hfc_map[11] },
  2082. { PCI_VDEVICE(ABOCOM, PCI_DEVICE_ID_ABOCOM_2BD1),
  2083. (unsigned long) &hfc_map[12] },
  2084. { PCI_VDEVICE(ASUSTEK, PCI_DEVICE_ID_ASUSTEK_0675),
  2085. (unsigned long) &hfc_map[13] },
  2086. { PCI_VDEVICE(BERKOM, PCI_DEVICE_ID_BERKOM_T_CONCEPT),
  2087. (unsigned long) &hfc_map[14] },
  2088. { PCI_VDEVICE(BERKOM, PCI_DEVICE_ID_BERKOM_A1T),
  2089. (unsigned long) &hfc_map[15] },
  2090. { PCI_VDEVICE(ANIGMA, PCI_DEVICE_ID_ANIGMA_MC145575),
  2091. (unsigned long) &hfc_map[16] },
  2092. { PCI_VDEVICE(ZOLTRIX, PCI_DEVICE_ID_ZOLTRIX_2BD0),
  2093. (unsigned long) &hfc_map[17] },
  2094. { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_E),
  2095. (unsigned long) &hfc_map[18] },
  2096. { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_E),
  2097. (unsigned long) &hfc_map[19] },
  2098. { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_A),
  2099. (unsigned long) &hfc_map[20] },
  2100. { PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_A),
  2101. (unsigned long) &hfc_map[21] },
  2102. { PCI_VDEVICE(SITECOM, PCI_DEVICE_ID_SITECOM_DC105V2),
  2103. (unsigned long) &hfc_map[22] },
  2104. {},
  2105. };
  2106. static int __devinit
  2107. hfc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2108. {
  2109. int err = -ENOMEM;
  2110. struct hfc_pci *card;
  2111. struct _hfc_map *m = (struct _hfc_map *)ent->driver_data;
  2112. card = kzalloc(sizeof(struct hfc_pci), GFP_ATOMIC);
  2113. if (!card) {
  2114. printk(KERN_ERR "No kmem for HFC card\n");
  2115. return err;
  2116. }
  2117. card->pdev = pdev;
  2118. card->subtype = m->subtype;
  2119. err = pci_enable_device(pdev);
  2120. if (err) {
  2121. kfree(card);
  2122. return err;
  2123. }
  2124. printk(KERN_INFO "mISDN_hfcpci: found adapter %s at %s\n",
  2125. m->name, pci_name(pdev));
  2126. card->irq = pdev->irq;
  2127. pci_set_drvdata(pdev, card);
  2128. err = setup_card(card);
  2129. if (err)
  2130. pci_set_drvdata(pdev, NULL);
  2131. return err;
  2132. }
  2133. static void __devexit
  2134. hfc_remove_pci(struct pci_dev *pdev)
  2135. {
  2136. struct hfc_pci *card = pci_get_drvdata(pdev);
  2137. if (card)
  2138. release_card(card);
  2139. else
  2140. if (debug)
  2141. printk(KERN_DEBUG "%s: drvdata already removed\n",
  2142. __func__);
  2143. }
  2144. static struct pci_driver hfc_driver = {
  2145. .name = "hfcpci",
  2146. .probe = hfc_probe,
  2147. .remove = __devexit_p(hfc_remove_pci),
  2148. .id_table = hfc_ids,
  2149. };
  2150. static int
  2151. _hfcpci_softirq(struct device *dev, void *arg)
  2152. {
  2153. struct hfc_pci *hc = dev_get_drvdata(dev);
  2154. struct bchannel *bch;
  2155. if (hc == NULL)
  2156. return 0;
  2157. if (hc->hw.int_m2 & HFCPCI_IRQ_ENABLE) {
  2158. spin_lock(&hc->lock);
  2159. bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);
  2160. if (bch && bch->state == ISDN_P_B_RAW) { /* B1 rx&tx */
  2161. main_rec_hfcpci(bch);
  2162. tx_birq(bch);
  2163. }
  2164. bch = Sel_BCS(hc, hc->hw.bswapped ? 1 : 2);
  2165. if (bch && bch->state == ISDN_P_B_RAW) { /* B2 rx&tx */
  2166. main_rec_hfcpci(bch);
  2167. tx_birq(bch);
  2168. }
  2169. spin_unlock(&hc->lock);
  2170. }
  2171. return 0;
  2172. }
  2173. static void
  2174. hfcpci_softirq(void *arg)
  2175. {
  2176. (void) driver_for_each_device(&hfc_driver.driver, NULL, arg,
  2177. _hfcpci_softirq);
  2178. /* if next event would be in the past ... */
  2179. if ((s32)(hfc_jiffies + tics - jiffies) <= 0)
  2180. hfc_jiffies = jiffies + 1;
  2181. else
  2182. hfc_jiffies += tics;
  2183. hfc_tl.expires = hfc_jiffies;
  2184. add_timer(&hfc_tl);
  2185. }
  2186. static int __init
  2187. HFC_init(void)
  2188. {
  2189. int err;
  2190. if (!poll)
  2191. poll = HFCPCI_BTRANS_THRESHOLD;
  2192. if (poll != HFCPCI_BTRANS_THRESHOLD) {
  2193. tics = (poll * HZ) / 8000;
  2194. if (tics < 1)
  2195. tics = 1;
  2196. poll = (tics * 8000) / HZ;
  2197. if (poll > 256 || poll < 8) {
  2198. printk(KERN_ERR "%s: Wrong poll value %d not in range "
  2199. "of 8..256.\n", __func__, poll);
  2200. err = -EINVAL;
  2201. return err;
  2202. }
  2203. }
  2204. if (poll != HFCPCI_BTRANS_THRESHOLD) {
  2205. printk(KERN_INFO "%s: Using alternative poll value of %d\n",
  2206. __func__, poll);
  2207. hfc_tl.function = (void *)hfcpci_softirq;
  2208. hfc_tl.data = 0;
  2209. init_timer(&hfc_tl);
  2210. hfc_tl.expires = jiffies + tics;
  2211. hfc_jiffies = hfc_tl.expires;
  2212. add_timer(&hfc_tl);
  2213. } else
  2214. tics = 0; /* indicate the use of controller's timer */
  2215. err = pci_register_driver(&hfc_driver);
  2216. if (err) {
  2217. if (timer_pending(&hfc_tl))
  2218. del_timer(&hfc_tl);
  2219. }
  2220. return err;
  2221. }
  2222. static void __exit
  2223. HFC_cleanup(void)
  2224. {
  2225. if (timer_pending(&hfc_tl))
  2226. del_timer(&hfc_tl);
  2227. pci_unregister_driver(&hfc_driver);
  2228. }
  2229. module_init(HFC_init);
  2230. module_exit(HFC_cleanup);
  2231. MODULE_DEVICE_TABLE(pci, hfc_ids);