hfcpci.c 65 KB

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