hfcpci.c 63 KB

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