hfcpci.c 60 KB

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