hfcpci.c 64 KB

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