hfcpci.c 60 KB

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