hfcpci.c 63 KB

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