hfcpci.c 65 KB

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