hfcpci.c 63 KB

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