hfcpci.c 63 KB

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