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