hfcpci.c 64 KB

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