hfcpci.c 64 KB

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