hfcpci.c 62 KB

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