base.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051
  1. /*
  2. * WCB410P Quad-BRI PCI Driver
  3. * Written by Andrew Kohlsmith <akohlsmith@mixdown.ca>
  4. *
  5. * Copyright (C) 2009-2011 Digium, Inc.
  6. * All rights reserved.
  7. *
  8. */
  9. /*
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2 as published by the
  18. * Free Software Foundation. See the LICENSE file included with
  19. * this program for more details.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/kernel.h> /* printk() */
  23. #include <linux/errno.h> /* error codes */
  24. #include <linux/module.h>
  25. #include <linux/types.h> /* size_t */
  26. #include <linux/fcntl.h> /* O_ACCMODE */
  27. #include <linux/fs.h>
  28. #include <linux/cdev.h>
  29. #include <linux/pci.h> /* for PCI structures */
  30. #include <linux/delay.h>
  31. #include <asm/io.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/device.h> /* dev_err() */
  34. #include <linux/interrupt.h>
  35. #include <asm/system.h> /* cli(), *_flags */
  36. #include <asm/uaccess.h> /* copy_*_user */
  37. #include <linux/workqueue.h> /* work_struct */
  38. #include <linux/timer.h> /* timer_struct */
  39. #include <linux/moduleparam.h>
  40. #include <linux/proc_fs.h>
  41. #include <dahdi/kernel.h>
  42. #include "wcb4xxp.h"
  43. #ifndef BIT /* added in 2.6.24 */
  44. #define BIT(i) (1UL << (i))
  45. #endif
  46. #define BIT_SET(x, i) ((x) |= BIT(i))
  47. #define BIT_CLR(x, i) ((x) &= ~BIT(i))
  48. #define IS_SET(x, i) (((x) & BIT(i)) != 0)
  49. #define BITMASK(i) (((u64)1 << (i)) - 1)
  50. #if (DAHDI_CHUNKSIZE != 8)
  51. #error Sorry, wcb4xxp does not support chunksize != 8
  52. #endif
  53. //#define SIMPLE_BCHAN_FIFO
  54. //#define DEBUG_LOWLEVEL_REGS /* debug __pci_in/out, not b4xxp_setreg */
  55. #define DEBUG_GENERAL (1 << 0) /* general debug messages */
  56. #define DEBUG_DTMF (1 << 1) /* emit DTMF detector messages */
  57. #define DEBUG_REGS (1 << 2) /* emit register read/write, but only if the kernel's DEBUG is defined */
  58. #define DEBUG_FOPS (1 << 3) /* emit file operation messages */
  59. #define DEBUG_ECHOCAN (1 << 4)
  60. #define DEBUG_ST_STATE (1 << 5) /* S/T state machine */
  61. #define DEBUG_HDLC (1 << 6) /* HDLC controller */
  62. #define DEBUG_ALARM (1 << 7) /* alarm changes */
  63. #define DBG (debug & DEBUG_GENERAL)
  64. #define DBG_DTMF (debug & DEBUG_DTMF)
  65. #define DBG_REGS (debug & DEBUG_REGS)
  66. #define DBG_FOPS (debug & DEBUG_FOPS)
  67. #define DBG_EC (debug & DEBUG_ECHOCAN)
  68. #define DBG_ST (debug & DEBUG_ST_STATE)
  69. #define DBG_HDLC (debug & DEBUG_HDLC)
  70. #define DBG_ALARM (debug & DEBUG_ALARM)
  71. #define DBG_SPANFILTER (BIT(bspan->port) & spanfilter)
  72. static int debug = 0;
  73. static int spanfilter = 0xFF; /* Bitmap for ports 1-8 */
  74. #ifdef LOOPBACK_SUPPORTED
  75. static int loopback = 0;
  76. #endif
  77. static int milliwatt = 0;
  78. static int pedanticpci = 0;
  79. static int teignorered = 0;
  80. static int alarmdebounce = 500;
  81. static int vpmsupport = 1;
  82. static int timer_1_ms = 2000;
  83. static int timer_3_ms = 30000;
  84. static char *companding = "alaw";
  85. #if !defined(mmiowb)
  86. #define mmiowb() barrier()
  87. #endif
  88. #define MAX_B4_CARDS 64
  89. static struct b4xxp *cards[MAX_B4_CARDS];
  90. static int led_fader_table[] = {
  91. 0, 0, 0, 1, 2, 3, 4, 6, 8, 9, 11, 13, 16, 18, 20, 22, 24,
  92. 25, 27, 28, 29, 30, 31, 31, 32, 31, 31, 30, 29, 28, 27, 25, 23, 22,
  93. 20, 18, 16, 13, 11, 9, 8, 6, 4, 3, 2, 1, 0, 0,
  94. };
  95. // #define CREATE_WCB4XXP_PROCFS_ENTRY
  96. #ifdef CREATE_WCB4XXP_PROCFS_ENTRY
  97. #define PROCFS_NAME "wcb4xxp"
  98. static struct proc_dir_entry *myproc;
  99. #endif
  100. /* Expansion; right now there's just one card and all of its idiosyncrasies. */
  101. #define FLAG_yyy (1 << 0)
  102. #define FLAG_zzz (1 << 1)
  103. struct devtype {
  104. char *desc;
  105. unsigned int flags;
  106. int ports; /* Number of ports the card has */
  107. enum cards_ids card_type; /* Card type - Digium B410P, ... */
  108. };
  109. static struct devtype wcb4xxp = {"Wildcard B410P", .ports = 4, .card_type = B410P };
  110. static struct devtype hfc2s = {"HFC-2S Junghanns.NET duoBRI PCI", .ports = 2, .card_type = DUOBRI };
  111. static struct devtype hfc4s = {"HFC-4S Junghanns.NET quadBRI PCI", .ports = 4, .card_type = QUADBRI };
  112. static struct devtype hfc8s = {"HFC-8S Junghanns.NET octoBRI PCI", .ports = 8, .card_type = OCTOBRI };
  113. static struct devtype hfc2s_OV = {"OpenVox B200P", .ports = 2, .card_type = B200P_OV };
  114. static struct devtype hfc4s_OV = {"OpenVox B400P", .ports = 4, .card_type = B400P_OV };
  115. static struct devtype hfc8s_OV = {"OpenVox B800P", .ports = 8, .card_type = B800P_OV };
  116. static struct devtype hfc2s_BN = {"BeroNet BN2S0", .ports = 2, .card_type = BN2S0 };
  117. static struct devtype hfc4s_BN = {"BeroNet BN4S0", .ports = 4, .card_type = BN4S0 };
  118. static struct devtype hfc8s_BN = {"BeroNet BN8S0", .ports = 8, .card_type = BN8S0 };
  119. static struct devtype hfc4s_SW = {"Swyx 4xS0 SX2 QuadBri", .ports = 4, .card_type = BSWYX_SX2 };
  120. static struct devtype hfc4s_EV = {"CCD HFC-4S Eval. Board", .ports = 4,
  121. .card_type = QUADBRI_EVAL };
  122. #define CARD_HAS_EC(card) ((card)->card_type == B410P)
  123. static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
  124. static const struct dahdi_echocan_features my_ec_features = {
  125. .NLP_automatic = 1,
  126. .CED_tx_detect = 1,
  127. .CED_rx_detect = 1,
  128. };
  129. static const struct dahdi_echocan_ops my_ec_ops = {
  130. .name = "HWEC",
  131. .echocan_free = echocan_free,
  132. };
  133. #if 0
  134. static const char *wcb4xxp_rcsdata = "$RCSfile: base.c,v $ $Revision$";
  135. static const char *build_stamp = "" __DATE__ " " __TIME__ "";
  136. #endif
  137. /*
  138. * lowlevel PCI access functions
  139. * These are simply wrappers for the normal PCI access functions that the kernel provides,
  140. * except that they allow us to work around specific PCI quirks with module options.
  141. * Currently the only option supported is pedanticpci, which injects a (min.) 3us delay
  142. * after any PCI access to forcibly disable fast back-to-back transactions.
  143. * In the case of a PCI write, pedanticpci will also read from the status register, which
  144. * has the effect of flushing any pending PCI writes.
  145. */
  146. static inline unsigned char __pci_in8(struct b4xxp *b4, const unsigned int reg)
  147. {
  148. unsigned char ret = ioread8(b4->addr + reg);
  149. #ifdef DEBUG_LOWLEVEL_REGS
  150. if (unlikely(DBG_REGS))
  151. drv_dbg(b4->dev, "read 0x%02x from 0x%p\n", ret, b4->addr + reg);
  152. #endif
  153. if (unlikely(pedanticpci)) {
  154. udelay(3);
  155. }
  156. return ret;
  157. }
  158. static inline unsigned short __pci_in16(struct b4xxp *b4, const unsigned int reg)
  159. {
  160. unsigned short ret = ioread16(b4->addr + reg);
  161. #ifdef DEBUG_LOWLEVEL_REGS
  162. if (unlikely(DBG_REGS))
  163. drv_dbg(b4->dev, "read 0x%04x from 0x%p\n", ret, b4->addr + reg);
  164. #endif
  165. if (unlikely(pedanticpci)) {
  166. udelay(3);
  167. }
  168. return ret;
  169. }
  170. static inline unsigned int __pci_in32(struct b4xxp *b4, const unsigned int reg)
  171. {
  172. unsigned int ret = ioread32(b4->addr + reg);
  173. #ifdef DEBUG_LOWLEVEL_REGS
  174. if (unlikely(DBG_REGS))
  175. drv_dbg(b4->dev, "read 0x%04x from 0x%p\n", ret, b4->addr + reg);
  176. #endif
  177. if (unlikely(pedanticpci)) {
  178. udelay(3);
  179. }
  180. return ret;
  181. }
  182. static inline void __pci_out32(struct b4xxp *b4, const unsigned int reg, const unsigned int val)
  183. {
  184. #ifdef DEBUG_LOWLEVEL_REGS
  185. if (unlikely(DBG_REGS))
  186. drv_dbg(b4->dev, "writing 0x%02x to 0x%p\n", val, b4->addr + reg);
  187. #endif
  188. iowrite32(val, b4->addr + reg);
  189. if (unlikely(pedanticpci)) {
  190. udelay(3);
  191. (void)ioread8(b4->addr + R_STATUS);
  192. }
  193. }
  194. static inline void __pci_out8(struct b4xxp *b4, const unsigned int reg, const unsigned char val)
  195. {
  196. #ifdef DEBUG_LOWLEVEL_REGS
  197. if (unlikely(DBG_REGS))
  198. drv_dbg(b4->dev, "writing 0x%02x to 0x%p\n", val, b4->addr + reg);
  199. #endif
  200. iowrite8(val, b4->addr + reg);
  201. if (unlikely(pedanticpci)) {
  202. udelay(3);
  203. (void)ioread8(b4->addr + R_STATUS);
  204. }
  205. }
  206. /*
  207. * Standard I/O access functions
  208. * uses spinlocks to protect against multiple I/O accesses
  209. * DOES NOT automatically memory barrier
  210. */
  211. static inline unsigned char b4xxp_getreg8(struct b4xxp *b4, const unsigned int reg)
  212. {
  213. unsigned int ret;
  214. unsigned long irq_flags;
  215. spin_lock_irqsave(&b4->reglock, irq_flags);
  216. #undef RETRY_REGISTER_READS
  217. #ifdef RETRY_REGISTER_READS
  218. switch (reg) {
  219. case A_Z1:
  220. case A_Z1H:
  221. case A_F2:
  222. case R_IRQ_OVIEW:
  223. case R_BERT_STA:
  224. case A_ST_RD_STA:
  225. case R_IRQ_FIFO_BL0:
  226. case A_Z2:
  227. case A_Z2H:
  228. case A_F1:
  229. case R_RAM_USE:
  230. case R_F0_CNTL:
  231. case A_ST_SQ_RD:
  232. case R_IRQ_FIFO_BL7:
  233. /* On pg 53 of the data sheet for the hfc, it states that we must
  234. * retry certain registers until we get two consecutive reads that are
  235. * the same. */
  236. retry:
  237. ret = __pci_in8(b4, reg);
  238. if (ret != __pci_in8(b4, reg))
  239. goto retry;
  240. break;
  241. default:
  242. #endif
  243. ret = __pci_in8(b4, reg);
  244. #ifdef RETRY_REGISTER_READS
  245. break;
  246. }
  247. #endif
  248. spin_unlock_irqrestore(&b4->reglock, irq_flags);
  249. #ifndef DEBUG_LOWLEVEL_REGS
  250. if (unlikely(DBG_REGS)) {
  251. dev_dbg(b4->dev, "read 0x%02x from 0x%p\n", ret, b4->addr + reg);
  252. }
  253. #endif
  254. return ret;
  255. }
  256. static inline unsigned int b4xxp_getreg32(struct b4xxp *b4, const unsigned int reg)
  257. {
  258. unsigned int ret;
  259. unsigned long irq_flags;
  260. spin_lock_irqsave(&b4->reglock, irq_flags);
  261. ret = __pci_in32(b4, reg);
  262. spin_unlock_irqrestore(&b4->reglock, irq_flags);
  263. #ifndef DEBUG_LOWLEVEL_REGS
  264. if (unlikely(DBG_REGS)) {
  265. dev_dbg(b4->dev, "read 0x%04x from 0x%p\n", ret, b4->addr + reg);
  266. }
  267. #endif
  268. return ret;
  269. }
  270. static inline unsigned short b4xxp_getreg16(struct b4xxp *b4, const unsigned int reg)
  271. {
  272. unsigned int ret;
  273. unsigned long irq_flags;
  274. spin_lock_irqsave(&b4->reglock, irq_flags);
  275. ret = __pci_in16(b4, reg);
  276. spin_unlock_irqrestore(&b4->reglock, irq_flags);
  277. #ifndef DEBUG_LOWLEVEL_REGS
  278. if (unlikely(DBG_REGS)) {
  279. dev_dbg(b4->dev, "read 0x%04x from 0x%p\n", ret, b4->addr + reg);
  280. }
  281. #endif
  282. return ret;
  283. }
  284. static inline void b4xxp_setreg32(struct b4xxp *b4, const unsigned int reg, const unsigned int val)
  285. {
  286. unsigned long irq_flags;
  287. #ifndef DEBUG_LOWLEVEL_REGS
  288. if (unlikely(DBG_REGS)) {
  289. dev_dbg(b4->dev, "writing 0x%02x to 0x%p\n", val, b4->addr + reg);
  290. }
  291. #endif
  292. spin_lock_irqsave(&b4->reglock, irq_flags);
  293. __pci_out32(b4, reg, val);
  294. spin_unlock_irqrestore(&b4->reglock, irq_flags);
  295. }
  296. static inline void b4xxp_setreg8(struct b4xxp *b4, const unsigned int reg, const unsigned char val)
  297. {
  298. unsigned long irq_flags;
  299. #ifndef DEBUG_LOWLEVEL_REGS
  300. if (unlikely(DBG_REGS)) {
  301. dev_dbg(b4->dev, "writing 0x%02x to 0x%p\n", val, b4->addr + reg);
  302. }
  303. #endif
  304. spin_lock_irqsave(&b4->reglock, irq_flags);
  305. __pci_out8(b4, reg, val);
  306. spin_unlock_irqrestore(&b4->reglock, irq_flags);
  307. }
  308. /*
  309. * A lot of the registers in the HFC are indexed.
  310. * this function sets the index, and then writes to the indexed register in an ordered fashion.
  311. * memory barriers are useless unless spinlocked, so that's what these wrapper functions do.
  312. */
  313. static void b4xxp_setreg_ra(struct b4xxp *b4, unsigned char r, unsigned char rd, unsigned char a, unsigned char ad)
  314. {
  315. unsigned long irq_flags;
  316. spin_lock_irqsave(&b4->seqlock, irq_flags);
  317. b4xxp_setreg8(b4, r, rd);
  318. wmb();
  319. b4xxp_setreg8(b4, a, ad);
  320. mmiowb();
  321. spin_unlock_irqrestore(&b4->seqlock, irq_flags);
  322. }
  323. static unsigned char b4xxp_getreg_ra(struct b4xxp *b4, unsigned char r, unsigned char rd, unsigned char a)
  324. {
  325. unsigned long irq_flags;
  326. unsigned char val;
  327. spin_lock_irqsave(&b4->seqlock, irq_flags);
  328. b4xxp_setreg8(b4, r, rd);
  329. wmb();
  330. val = b4xxp_getreg8(b4, a);
  331. mmiowb();
  332. spin_unlock_irqrestore(&b4->seqlock, irq_flags);
  333. return val;
  334. }
  335. /*
  336. * HFC-4S GPIO routines
  337. *
  338. * the B410P uses the HFC-4S GPIO as follows:
  339. * GPIO 8..10: output, CPLD register select
  340. * GPIO12..15: output, 1 = enable power for port 1-4
  341. * GPI16: input, 0 = echo can #1 interrupt
  342. * GPI17: input, 0 = echo can #2 interrupt
  343. * GPI23: input, 1 = NT power module installed
  344. * GPI24..27: input, NT power module problem on port 1-4
  345. * GPI28..31: input, 1 = port 1-4 in NT mode
  346. */
  347. /* initialize HFC-4S GPIO. Set up pin drivers before setting GPIO mode */
  348. static void hfc_gpio_init(struct b4xxp *b4)
  349. {
  350. unsigned long irq_flags;
  351. spin_lock_irqsave(&b4->seqlock, irq_flags);
  352. flush_pci(); /* flush any pending PCI writes */
  353. mb();
  354. b4xxp_setreg8(b4, R_GPIO_EN0, 0x00); /* GPIO0..7 input */
  355. b4xxp_setreg8(b4, R_GPIO_EN1, 0xf7); /* GPIO8..10,12..15 outputs, GPIO11 input */
  356. b4xxp_setreg8(b4, R_GPIO_OUT1, 0x00); /* disable power, CPLD reg 0 */
  357. mb();
  358. switch (b4->card_type) {
  359. case OCTOBRI: /* fall through */
  360. case B800P_OV: /* fall through */
  361. case BN8S0:
  362. /* GPIO0..15 S/T - HFC-8S uses GPIO8-15 for S/T ports 5-8 */
  363. b4xxp_setreg8(b4, R_GPIO_SEL, 0x00);
  364. break;
  365. default:
  366. /* GPIO0..7 S/T, 8..15 GPIO */
  367. b4xxp_setreg8(b4, R_GPIO_SEL, 0xf0);
  368. break;
  369. }
  370. mb();
  371. spin_unlock_irqrestore(&b4->seqlock, irq_flags);
  372. }
  373. /*
  374. * HFC SRAM interface code.
  375. * This came from mattf, I don't even pretend to understand it,
  376. * It seems to be using undocumented features in the HFC.
  377. * I just added the __pci_in8() to ensure the PCI writes made it
  378. * to hardware by the time these functions return.
  379. */
  380. static inline void enablepcibridge(struct b4xxp *b4)
  381. {
  382. b4xxp_setreg8(b4, R_BRG_PCM_CFG, 0x03);
  383. flush_pci();
  384. }
  385. static inline void disablepcibridge(struct b4xxp *b4)
  386. {
  387. b4xxp_setreg8(b4, R_BRG_PCM_CFG, 0x02);
  388. flush_pci();
  389. }
  390. /* NOTE: read/writepcibridge do not use __pci_in/out because they are using b4->ioaddr not b4->addr */
  391. static inline unsigned char readpcibridge(struct b4xxp *b4, unsigned char address)
  392. {
  393. unsigned short cipv;
  394. unsigned char data;
  395. /* slow down a PCI read access by 1 PCI clock cycle */
  396. b4xxp_setreg8(b4, R_CTRL, 0x4);
  397. wmb();
  398. if (address == 0)
  399. cipv=0x4000;
  400. else
  401. cipv=0x5800;
  402. /* select local bridge port address by writing to CIP port */
  403. iowrite16(cipv, b4->ioaddr + 4);
  404. wmb();
  405. data = ioread8(b4->ioaddr);
  406. /* restore R_CTRL for normal PCI read cycle speed */
  407. b4xxp_setreg8(b4, R_CTRL, 0x0);
  408. wmb();
  409. flush_pci();
  410. return data;
  411. }
  412. static inline void writepcibridge(struct b4xxp *b4, unsigned char address, unsigned char data)
  413. {
  414. unsigned short cipv;
  415. unsigned int datav;
  416. if (address == 0)
  417. cipv=0x4000;
  418. else
  419. cipv=0x5800;
  420. /* select local bridge port address by writing to CIP port */
  421. iowrite16(cipv, b4->ioaddr + 4);
  422. wmb();
  423. /* define a 32 bit dword with 4 identical bytes for write sequence */
  424. datav = data | ( (__u32) data <<8) | ( (__u32) data <<16) | ( (__u32) data <<24);
  425. /*
  426. * write this 32 bit dword to the bridge data port
  427. * this will initiate a write sequence of up to 4 writes to the same address on the local bus
  428. * interface
  429. * the number of write accesses is undefined but >=1 and depends on the next PCI transaction
  430. * during write sequence on the local bus
  431. */
  432. iowrite32(datav, b4->ioaddr);
  433. wmb();
  434. flush_pci();
  435. }
  436. /* CPLD access code, more or less copied verbatim from code provided by mattf. */
  437. static inline void cpld_select_reg(struct b4xxp *b4, unsigned char reg)
  438. {
  439. b4xxp_setreg8(b4, R_GPIO_OUT1, reg);
  440. flush_pci();
  441. }
  442. static inline void cpld_setreg(struct b4xxp *b4, unsigned char reg, unsigned char val)
  443. {
  444. cpld_select_reg(b4, reg);
  445. enablepcibridge(b4);
  446. writepcibridge(b4, 1, val);
  447. disablepcibridge(b4);
  448. }
  449. static inline unsigned char cpld_getreg(struct b4xxp *b4, unsigned char reg)
  450. {
  451. unsigned char data;
  452. cpld_select_reg(b4, reg);
  453. enablepcibridge(b4);
  454. data = readpcibridge(b4, 1);
  455. disablepcibridge(b4);
  456. return data;
  457. }
  458. /*
  459. * echo canceller code, verbatim from mattf.
  460. * I don't pretend to understand it.
  461. */
  462. static inline void ec_select_addr(struct b4xxp *b4, unsigned short addr)
  463. {
  464. cpld_setreg(b4, 0, 0xff & addr);
  465. cpld_setreg(b4, 1, 0x01 & (addr >> 8));
  466. }
  467. static inline unsigned short ec_read_data(struct b4xxp *b4)
  468. {
  469. unsigned short addr;
  470. unsigned short highbit;
  471. addr = cpld_getreg(b4, 0);
  472. highbit = cpld_getreg(b4, 1);
  473. addr = addr | (highbit << 8);
  474. return addr & 0x1ff;
  475. }
  476. static inline unsigned char ec_read(struct b4xxp *b4, int which, unsigned short addr)
  477. {
  478. unsigned char data;
  479. unsigned long flags;
  480. spin_lock_irqsave(&b4->seqlock, flags);
  481. ec_select_addr(b4, addr);
  482. if (!which)
  483. cpld_select_reg(b4, 2);
  484. else
  485. cpld_select_reg(b4, 3);
  486. enablepcibridge(b4);
  487. data = readpcibridge(b4, 1);
  488. disablepcibridge(b4);
  489. cpld_select_reg(b4, 0);
  490. spin_unlock_irqrestore(&b4->seqlock, flags);
  491. return data;
  492. }
  493. static inline void ec_write(struct b4xxp *b4, int which, unsigned short addr, unsigned char data)
  494. {
  495. unsigned char in;
  496. unsigned long flags;
  497. spin_lock_irqsave(&b4->seqlock, flags);
  498. ec_select_addr(b4, addr);
  499. enablepcibridge(b4);
  500. if (!which)
  501. cpld_select_reg(b4, 2);
  502. else
  503. cpld_select_reg(b4, 3);
  504. writepcibridge(b4, 1, data);
  505. cpld_select_reg(b4, 0);
  506. disablepcibridge(b4);
  507. spin_unlock_irqrestore(&b4->seqlock, flags);
  508. in = ec_read(b4, which, addr);
  509. if (in != data) {
  510. if (printk_ratelimit()) {
  511. dev_warn(b4->dev, "ec_write: Wrote 0x%02x to register 0x%02x "
  512. "of VPM %d but got back 0x%02x\n", data, addr, which, in);
  513. }
  514. }
  515. }
  516. #define NUM_EC 2
  517. #define MAX_TDM_CHAN 32
  518. #if 0
  519. void ec_set_dtmf_threshold(struct b4xxp *b4, int threshold)
  520. {
  521. unsigned int x;
  522. for (x = 0; x < NUM_EC; x++) {
  523. ec_write(b4, x, 0xC4, (threshold >> 8) & 0xFF);
  524. ec_write(b4, x, 0xC5, (threshold & 0xFF));
  525. }
  526. printk("VPM: DTMF threshold set to %d\n", threshold);
  527. }
  528. #endif
  529. static void ec_init(struct b4xxp *b4)
  530. {
  531. unsigned char b;
  532. unsigned int i, j, mask;
  533. if (!CARD_HAS_EC(b4))
  534. return;
  535. /* Setup GPIO */
  536. for (i=0; i < NUM_EC; i++) {
  537. b = ec_read(b4, i, 0x1a0);
  538. dev_info(b4->dev, "VPM %d/%d init: chip ver %02x\n", i, NUM_EC - 1, b);
  539. for (j=0; j < b4->numspans; j++) {
  540. ec_write(b4, i, 0x1a8 + j, 0x00); /* GPIO out */
  541. ec_write(b4, i, 0x1ac + j, 0x00); /* GPIO dir */
  542. ec_write(b4, i, 0x1b0 + j, 0x00); /* GPIO sel */
  543. }
  544. /* Setup TDM path - sets fsync and tdm_clk as inputs */
  545. b = ec_read(b4, i, 0x1a3); /* misc_con */
  546. ec_write(b4, i, 0x1a3, b & ~0x02);
  547. /* Setup Echo length (512 taps) */
  548. ec_write(b4, i, 0x022, 1);
  549. ec_write(b4, i, 0x023, 0xff);
  550. /* Setup timeslots */
  551. ec_write(b4, i, 0x02f, 0x00);
  552. mask = 0x02020202 << (i * 4);
  553. /* Setup the tdm channel masks for all chips*/
  554. for (j=0; j < 4; j++)
  555. ec_write(b4, i, 0x33 - j, (mask >> (j << 3)) & 0xff);
  556. /* Setup convergence rate */
  557. b = ec_read(b4, i, 0x20);
  558. b &= 0xe0;
  559. b |= 0x12;
  560. if (!strcasecmp(companding, "alaw")) {
  561. if (DBG)
  562. dev_info(b4->dev, "Setting alaw mode\n");
  563. b |= 0x01;
  564. } else {
  565. if (DBG)
  566. dev_info(b4->dev, "Setting ulaw mode");
  567. }
  568. ec_write(b4, i, 0x20, b);
  569. if (DBG)
  570. dev_info(b4->dev, "reg 0x20 is 0x%02x\n", b);
  571. // ec_write(b4, i, 0x20, 0x38);
  572. #if 0
  573. ec_write(b4, i, 0x24, 0x02);
  574. b = ec_read(b4, i, 0x24);
  575. #endif
  576. if (DBG)
  577. dev_info(b4->dev, "NLP threshold is set to %d (0x%02x)\n", b, b);
  578. /* Initialize echo cans */
  579. for (j=0; j < MAX_TDM_CHAN; j++) {
  580. if (mask & (0x00000001 << j))
  581. ec_write(b4, i, j, 0x00);
  582. }
  583. mdelay(10);
  584. /* Put in bypass mode */
  585. for (j=0; j < MAX_TDM_CHAN; j++) {
  586. if (mask & (0x00000001 << j)) {
  587. ec_write(b4, i, j, 0x01);
  588. }
  589. }
  590. /* Enable bypass */
  591. for (j=0; j < MAX_TDM_CHAN; j++) {
  592. if (mask & (0x00000001 << j))
  593. ec_write(b4, i, 0x78 + j, 0x01);
  594. }
  595. }
  596. #if 0
  597. ec_set_dtmf_threshold(b4, 1250);
  598. #endif
  599. }
  600. /* performs a register write and then waits for the HFC "busy" bit to clear */
  601. static void hfc_setreg_waitbusy(struct b4xxp *b4, const unsigned int reg, const unsigned int val)
  602. {
  603. int timeout = 0;
  604. unsigned long start;
  605. const int TIMEOUT = HZ/4; /* 250ms */
  606. start = jiffies;
  607. while (unlikely((b4xxp_getreg8(b4, R_STATUS) & V_BUSY))) {
  608. if (time_after(jiffies, start + TIMEOUT)) {
  609. timeout = 1;
  610. break;
  611. }
  612. };
  613. mb();
  614. b4xxp_setreg8(b4, reg, val);
  615. mb();
  616. start = jiffies;
  617. while (likely((b4xxp_getreg8(b4, R_STATUS) & V_BUSY))) {
  618. if (time_after(jiffies, start + TIMEOUT)) {
  619. timeout = 1;
  620. break;
  621. }
  622. };
  623. if (timeout) {
  624. if (printk_ratelimit())
  625. dev_warn(b4->dev, "hfc_setreg_waitbusy(write 0x%02x to 0x%02x) timed out waiting for busy flag to clear!\n", val, reg);
  626. }
  627. }
  628. /*
  629. * reads an 8-bit register over over and over until the same value is read twice, then returns that value.
  630. */
  631. static inline unsigned char hfc_readcounter8(struct b4xxp *b4, const unsigned int reg)
  632. {
  633. unsigned char r1, r2;
  634. unsigned long maxwait = 1048576;
  635. do {
  636. r1 = b4xxp_getreg8(b4, reg);
  637. r2 = b4xxp_getreg8(b4, reg);
  638. } while ((r1 != r2) && maxwait--);
  639. if (!maxwait) {
  640. if (printk_ratelimit())
  641. dev_warn(b4->dev, "hfc_readcounter8(reg 0x%02x) timed out waiting for data to settle!\n", reg);
  642. }
  643. return r1;
  644. }
  645. /*
  646. * reads a 16-bit register over over and over until the same value is read twice, then returns that value.
  647. */
  648. static inline unsigned short hfc_readcounter16(struct b4xxp *b4, const unsigned int reg)
  649. {
  650. unsigned short r1, r2;
  651. unsigned long maxwait = 1048576;
  652. do {
  653. r1 = b4xxp_getreg16(b4, reg);
  654. r2 = b4xxp_getreg16(b4, reg);
  655. } while ((r1 != r2) && maxwait--);
  656. if (!maxwait) {
  657. if (printk_ratelimit())
  658. dev_warn(b4->dev, "hfc_readcounter16(reg 0x%02x) timed out waiting for data to settle!\n", reg);
  659. }
  660. return r1;
  661. }
  662. static inline unsigned int hfc_readcounter32(struct b4xxp *b4, const unsigned int reg)
  663. {
  664. unsigned int r1, r2;
  665. unsigned long maxwait = 1048576;
  666. do {
  667. r1 = b4xxp_getreg32(b4, reg);
  668. r2 = b4xxp_getreg32(b4, reg);
  669. } while ((r1 != r2) && maxwait--);
  670. if (!maxwait) {
  671. if (printk_ratelimit())
  672. dev_warn(b4->dev, "hfc_readcounter32(reg 0x%02x) timed out waiting for data to settle!\n", reg);
  673. }
  674. return r1;
  675. }
  676. /* performs a soft-reset of the HFC-4S. This is as clean-slate as you can get to a hardware reset. */
  677. static void hfc_reset(struct b4xxp *b4)
  678. {
  679. int b, c;
  680. /* all 32 FIFOs the same size (384 bytes), channel select data flow mode, sized for internal RAM */
  681. b4xxp_setreg8(b4, R_FIFO_MD, V_FIFO_MD_00 | V_DF_MD_CSM | V_FIFO_SZ_00);
  682. flush_pci();
  683. /* reset everything, wait 500us, then bring everything BUT the PCM system out of reset */
  684. b4xxp_setreg8(b4, R_CIRM, HFC_FULL_RESET);
  685. flush_pci();
  686. udelay(500);
  687. b4xxp_setreg8(b4, R_CIRM, V_PCM_RES);
  688. flush_pci();
  689. udelay(500);
  690. /*
  691. * Now bring PCM out of reset and do a very basic setup of the PCM system to allow it to finish resetting correctly.
  692. * set F0IO as an output, and set up a 32-timeslot PCM bus
  693. * See Section 8.3 in the HFC-4S datasheet for more details.
  694. */
  695. b4xxp_setreg8(b4, R_CIRM, 0x00);
  696. b4xxp_setreg8(b4, R_PCM_MD0, V_PCM_MD | V_PCM_IDX_MD1);
  697. flush_pci();
  698. b4xxp_setreg8(b4, R_PCM_MD1, V_PLL_ADJ_00 | V_PCM_DR_2048);
  699. flush_pci();
  700. /* now wait for R_F0_CNTL to reach at least 2 before continuing */
  701. c=10;
  702. while ((b = b4xxp_getreg8(b4, R_F0_CNTL)) < 2 && c) { udelay(100); c--; }
  703. if (!c && b < 2) {
  704. dev_warn(b4->dev, "hfc_reset() did not get the green light from the PCM system!\n");
  705. }
  706. }
  707. static inline void hfc_enable_fifo_irqs(struct b4xxp *b4)
  708. {
  709. b4xxp_setreg8(b4, R_IRQ_CTRL, V_FIFO_IRQ | V_GLOB_IRQ_EN);
  710. flush_pci();
  711. }
  712. static inline void hfc_disable_fifo_irqs(struct b4xxp *b4)
  713. {
  714. b4xxp_setreg8(b4, R_IRQ_CTRL, V_GLOB_IRQ_EN);
  715. flush_pci();
  716. }
  717. static void hfc_enable_interrupts(struct b4xxp *b4)
  718. {
  719. b4->running = 1;
  720. /* clear any pending interrupts */
  721. b4xxp_getreg8(b4, R_STATUS);
  722. b4xxp_getreg8(b4, R_IRQ_MISC);
  723. b4xxp_getreg8(b4, R_IRQ_FIFO_BL0);
  724. b4xxp_getreg8(b4, R_IRQ_FIFO_BL1);
  725. b4xxp_getreg8(b4, R_IRQ_FIFO_BL2);
  726. b4xxp_getreg8(b4, R_IRQ_FIFO_BL3);
  727. b4xxp_getreg8(b4, R_IRQ_FIFO_BL4);
  728. b4xxp_getreg8(b4, R_IRQ_FIFO_BL5);
  729. b4xxp_getreg8(b4, R_IRQ_FIFO_BL6);
  730. b4xxp_getreg8(b4, R_IRQ_FIFO_BL7);
  731. b4xxp_setreg8(b4, R_IRQMSK_MISC, V_TI_IRQ);
  732. hfc_enable_fifo_irqs(b4);
  733. }
  734. static void hfc_disable_interrupts(struct b4xxp *b4)
  735. {
  736. b4xxp_setreg8(b4, R_IRQMSK_MISC, 0);
  737. b4xxp_setreg8(b4, R_IRQ_CTRL, 0);
  738. flush_pci();
  739. b4->running = 0;
  740. }
  741. /*
  742. * Connects an S/T port's B channel to a host-facing FIFO through the PCM busses.
  743. * This bchan flow plan should match up with the EC requirements.
  744. * TODO: Interrupts are only enabled on the host FIFO RX side, since everything is (should be) synchronous.
  745. * *** performs no error checking of parameters ***
  746. */
  747. static void hfc_assign_bchan_fifo_ec(struct b4xxp *b4, int port, int bchan)
  748. {
  749. int fifo, hfc_chan, ts;
  750. unsigned long irq_flags;
  751. static int first=1;
  752. if (first) {
  753. first = 0;
  754. dev_info(b4->dev, "Hardware echo cancellation enabled.\n");
  755. }
  756. fifo = port * 2;
  757. hfc_chan = port * 4;
  758. ts = port * 8;
  759. if (bchan) {
  760. fifo += 1;
  761. hfc_chan += 1;
  762. ts += 4;
  763. }
  764. /* record the host's FIFO # in the span fifo array */
  765. b4->spans[port].fifos[bchan] = fifo;
  766. spin_lock_irqsave(&b4->fifolock, irq_flags);
  767. if (DBG)
  768. dev_info(b4->dev, "port %d, B channel %d\n\tS/T -> PCM ts %d uses HFC chan %d via FIFO %d\n", port, bchan, ts + 1, hfc_chan, 16 + fifo);
  769. /* S/T RX -> PCM TX FIFO, transparent mode, no IRQ. */
  770. hfc_setreg_waitbusy(b4, R_FIFO, ((16 + fifo) << V_FIFO_NUM_SHIFT));
  771. b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_110);
  772. b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT));
  773. b4xxp_setreg8(b4, R_SLOT, ((ts + 1) << V_SL_NUM_SHIFT));
  774. b4xxp_setreg8(b4, A_SL_CFG, V_ROUT_TX_STIO1 | (hfc_chan << V_CH_SNUM_SHIFT));
  775. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
  776. if (DBG)
  777. pr_info("\tPCM ts %d -> host uses HFC chan %d via FIFO %d\n", ts + 1, 16 + hfc_chan, fifo);
  778. /* PCM RX -> Host TX FIFO, transparent mode, enable IRQ. */
  779. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  780. b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_001);
  781. b4xxp_setreg8(b4, A_CHANNEL, ((16 + hfc_chan) << V_CH_FNUM_SHIFT) | V_CH_FDIR);
  782. b4xxp_setreg8(b4, R_SLOT, ((ts + 1) << V_SL_NUM_SHIFT) | 1);
  783. b4xxp_setreg8(b4, A_SL_CFG, V_ROUT_RX_STIO2 | ((16 + hfc_chan) << V_CH_SNUM_SHIFT) | 1);
  784. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
  785. // b4xxp_setreg8(b4, A_IRQ_MSK, V_IRQ);
  786. if (DBG)
  787. pr_info("\thost -> PCM ts %d uses HFC chan %d via FIFO %d\n", ts, 16 + hfc_chan, fifo);
  788. /* Host FIFO -> PCM TX */
  789. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
  790. b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_001);
  791. b4xxp_setreg8(b4, A_CHANNEL, ((16 + hfc_chan) << V_CH_FNUM_SHIFT));
  792. b4xxp_setreg8(b4, R_SLOT, (ts << V_SL_NUM_SHIFT));
  793. b4xxp_setreg8(b4, A_SL_CFG, V_ROUT_RX_STIO2 | ((16 + hfc_chan) << V_CH_SNUM_SHIFT));
  794. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
  795. if (DBG)
  796. pr_info("\tPCM ts %d -> S/T uses HFC chan %d via FIFO %d\n", ts, hfc_chan, 16 + fifo);
  797. /* PCM -> S/T */
  798. hfc_setreg_waitbusy(b4, R_FIFO, ((16 + fifo) << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  799. b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_110);
  800. b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT) | V_CH_FDIR);
  801. b4xxp_setreg8(b4, R_SLOT, (ts << V_SL_NUM_SHIFT) | V_SL_DIR);
  802. b4xxp_setreg8(b4, A_SL_CFG, V_ROUT_TX_STIO2 | (hfc_chan << V_CH_SNUM_SHIFT) | V_CH_SDIR);
  803. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
  804. if (DBG)
  805. pr_info("\tPCM ts %d -> S/T uses HFC chan %d via FIFO %d\n", ts, hfc_chan, 16 + fifo);
  806. flush_pci(); /* ensure all those writes actually hit hardware */
  807. spin_unlock_irqrestore(&b4->fifolock, irq_flags);
  808. }
  809. static void hfc_assign_bchan_fifo_noec(struct b4xxp *b4, int port, int bchan)
  810. {
  811. int fifo, hfc_chan, ts;
  812. unsigned long irq_flags;
  813. static int first=1;
  814. if (first) {
  815. first = 0;
  816. dev_info(b4->dev, "NOTE: hardware echo cancellation has been disabled\n");
  817. }
  818. fifo = port * 2;
  819. hfc_chan = port * 4;
  820. ts = port * 8;
  821. if (bchan) {
  822. fifo += 1;
  823. hfc_chan += 1;
  824. ts += 4;
  825. }
  826. /* record the host's FIFO # in the span fifo array */
  827. b4->spans[port].fifos[bchan] = fifo;
  828. spin_lock_irqsave(&b4->fifolock, irq_flags);
  829. if (DBG)
  830. dev_info(b4->dev, "port %d, B channel %d\n\thost -> S/T uses HFC chan %d via FIFO %d\n", port, bchan, hfc_chan, fifo);
  831. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
  832. b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_000);
  833. b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT));
  834. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
  835. if (DBG)
  836. pr_info("\tS/T -> host uses HFC chan %d via FIFO %d\n", hfc_chan, fifo);
  837. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  838. b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_000);
  839. b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT) | V_CH_FDIR);
  840. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
  841. if (DBG)
  842. pr_info("\tPCM ts %d -> S/T uses HFC chan %d via FIFO %d\n", ts, hfc_chan, 16 + fifo);
  843. flush_pci(); /* ensure all those writes actually hit hardware */
  844. spin_unlock_irqrestore(&b4->fifolock, irq_flags);
  845. }
  846. /*
  847. * Connects an S/T port's D channel to a host-facing FIFO.
  848. * Both TX and RX interrupts are enabled!
  849. * *** performs no error checking of parameters ***
  850. */
  851. static void hfc_assign_dchan_fifo(struct b4xxp *b4, int port)
  852. {
  853. int fifo, hfc_chan;
  854. unsigned long irq_flags;
  855. switch (b4->card_type) {
  856. case B800P_OV: /* fall through */
  857. case OCTOBRI: /* fall through */
  858. case BN8S0:
  859. /* In HFC-8S cards we can't use ports 8-11 for dchan FIFOs */
  860. fifo = port + 16;
  861. break;
  862. default:
  863. fifo = port + 8;
  864. break;
  865. }
  866. hfc_chan = (port * 4) + 2;
  867. /* record the host's FIFO # in the span fifo array */
  868. b4->spans[port].fifos[2] = fifo;
  869. if (DBG)
  870. dev_info(b4->dev, "port %d, D channel\n\thost -> S/T uses HFC chan %d via FIFO %d\n", port, hfc_chan, fifo);
  871. spin_lock_irqsave(&b4->fifolock, irq_flags);
  872. /* Host FIFO -> S/T TX, HDLC mode, no IRQ. */
  873. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
  874. b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_TRP_IRQ | V_DATA_FLOW_000);
  875. b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT));
  876. b4xxp_setreg8(b4, A_SUBCH_CFG, 0x02);
  877. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
  878. if (DBG)
  879. pr_info("\tS/T -> host uses HFC chan %d via FIFO %d\n", hfc_chan, fifo);
  880. /* S/T RX -> Host FIFO, HDLC mode, IRQ will be enabled when port opened. */
  881. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  882. b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_TRP_IRQ | V_DATA_FLOW_000);
  883. b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT) | V_CH_FDIR);
  884. b4xxp_setreg8(b4, A_SUBCH_CFG, 0x02);
  885. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
  886. if (DBG)
  887. pr_info("\n");
  888. flush_pci(); /* ensure all those writes actually hit hardware */
  889. spin_unlock_irqrestore(&b4->fifolock, irq_flags);
  890. }
  891. /* takes a read/write fifo pair and optionally resets it, optionally enabling the rx/tx interrupt */
  892. static void hfc_reset_fifo_pair(struct b4xxp *b4, int fifo, int reset, int force_no_irq)
  893. {
  894. unsigned long irq_flags;
  895. spin_lock_irqsave(&b4->fifolock, irq_flags);
  896. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
  897. b4xxp_setreg8(b4, A_IRQ_MSK, (!force_no_irq && b4->fifo_en_txint & (1 << fifo)) ? V_IRQ : 0);
  898. if (reset)
  899. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
  900. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  901. b4xxp_setreg8(b4, A_IRQ_MSK, (!force_no_irq && b4->fifo_en_rxint & (1 << fifo)) ? V_IRQ : 0);
  902. if (reset)
  903. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
  904. spin_unlock_irqrestore(&b4->fifolock, irq_flags);
  905. }
  906. static void b4xxp_set_sync_src(struct b4xxp *b4, int port)
  907. {
  908. int b;
  909. #if 0
  910. printk("Setting sync to be port %d\n", (port >= 0) ? port + 1 : port);
  911. #endif
  912. if (port == -1) /* automatic */
  913. b = 0;
  914. else
  915. b = (port & V_SYNC_SEL_MASK) | V_MAN_SYNC;
  916. b4xxp_setreg8(b4, R_ST_SYNC, b);
  917. b4->syncspan = port;
  918. }
  919. /*
  920. * Finds the highest-priority sync span that is not in alarm and returns it.
  921. * Note: the span #s in b4->spans[].sync are 1-based, and this returns
  922. * a 0-based span, or -1 if no spans are found.
  923. */
  924. static int b4xxp_find_sync(struct b4xxp *b4)
  925. {
  926. int i, psrc, src;
  927. src = -1; /* default to automatic */
  928. for (i=0; i < b4->numspans; i++) {
  929. if (DBG)
  930. dev_info(b4->dev, "Checking sync pos %d, have span %d\n", i, b4->spans[i].sync);
  931. psrc = b4->spans[i].sync;
  932. if (psrc > 0 && !b4->spans[psrc - 1].span.alarms) {
  933. if (DBG)
  934. dev_info(b4->dev, "chosen\n");
  935. src = psrc;
  936. break;
  937. }
  938. }
  939. if (src >= 0)
  940. return src - 1;
  941. else
  942. return src;
  943. }
  944. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18))
  945. static ssize_t b4_timing_master_show(struct device *dev,
  946. struct device_attribute *attr,
  947. char *buf)
  948. {
  949. struct b4xxp *b4 = dev_get_drvdata(dev);
  950. return sprintf(buf, "%d\n", b4->syncspan);
  951. }
  952. static DEVICE_ATTR(timing_master, 0400, b4_timing_master_show, NULL);
  953. static void create_sysfs_files(struct b4xxp *b4)
  954. {
  955. int ret;
  956. ret = device_create_file(b4->dev,
  957. &dev_attr_timing_master);
  958. if (ret) {
  959. dev_info(b4->dev,
  960. "Failed to create device attributes.\n");
  961. }
  962. }
  963. static void remove_sysfs_files(struct b4xxp *b4)
  964. {
  965. device_remove_file(b4->dev,
  966. &dev_attr_timing_master);
  967. }
  968. #else
  969. static inline void create_sysfs_files(struct b4xxp *b4) { return; }
  970. static inline void remove_sysfs_files(struct b4xxp *b4) { return; }
  971. #endif /* LINUX_KERNEL > 2.6.18 */
  972. /*
  973. * allocates memory and pretty-prints a given S/T state engine state to it.
  974. * calling routine is responsible for freeing the pointer returned!
  975. * Performs no hardware access whatsoever, but does use GFP_KERNEL so do not call from IRQ context.
  976. * if full == 1, prints a "full" dump; otherwise just prints current state.
  977. */
  978. static char *hfc_decode_st_state(struct b4xxp *b4, int port, unsigned char state, int full)
  979. {
  980. int nt, sta;
  981. char s[128], *str;
  982. const char *ststr[2][16] = { /* TE, NT */
  983. { "RESET", "?", "SENSING", "DEACT.", "AWAIT.SIG", "IDENT.INPUT", "SYNCD", "ACTIVATED",
  984. "LOSTFRAMING", "?", "?", "?", "?", "?", "?", "?" },
  985. { "RESET", "DEACT.", "PEND.ACT", "ACTIVE", "PEND.DEACT", "?", "?", "?",
  986. "?", "?", "?", "?", "?", "?", "?", "?" }
  987. };
  988. if (!(str = kmalloc(256, GFP_KERNEL))) {
  989. dev_warn(b4->dev, "could not allocate mem for ST state decode string!\n");
  990. return NULL;
  991. }
  992. nt = (b4->spans[port].te_mode == 0);
  993. sta = (state & V_ST_STA_MASK);
  994. sprintf(str, "P%d: %s state %c%d (%s)", port + 1, (nt ? "NT" : "TE"), (nt ? 'G' : 'F'), sta, ststr[nt][sta]);
  995. if (full) {
  996. sprintf(s, " SYNC: %s, RX INFO0: %s", ((state & V_FR_SYNC) ? "yes" : "no"), ((state & V_INFO0) ? "yes" : "no"));
  997. strcat(str, s);
  998. if (nt) {
  999. sprintf(s, ", T2 %s, auto G2->G3: %s", ((state & V_T2_EXP) ? "expired" : "OK"),
  1000. ((state & V_G2_G3) ? "yes" : "no"));
  1001. strcat(str, s);
  1002. }
  1003. }
  1004. return str;
  1005. }
  1006. /*
  1007. * sets an S/T port state machine to a given state.
  1008. * if 'auto' is nonzero, will put the state machine back in auto mode after setting the state.
  1009. */
  1010. static void hfc_handle_state(struct b4xxp_span *s);
  1011. static void hfc_force_st_state(struct b4xxp *b4, int port, int state, int resume_auto)
  1012. {
  1013. b4xxp_setreg_ra(b4, R_ST_SEL, port, A_ST_RD_STA, state | V_ST_LD_STA);
  1014. udelay(6);
  1015. if (resume_auto) {
  1016. b4xxp_setreg_ra(b4, R_ST_SEL, port, A_ST_RD_STA, state);
  1017. }
  1018. if (DBG_ST) {
  1019. char *x;
  1020. x = hfc_decode_st_state(b4, port, state, 1);
  1021. dev_info(b4->dev, "forced port %d to state %d (auto: %d), new decode: %s\n", port + 1, state, resume_auto, x);
  1022. kfree(x);
  1023. }
  1024. /* make sure that we activate any timers/etc needed by this state change */
  1025. hfc_handle_state(&b4->spans[port]);
  1026. }
  1027. /* figures out what to do when an S/T port's timer expires. */
  1028. static void hfc_timer_expire(struct b4xxp_span *s, int t_no)
  1029. {
  1030. struct b4xxp *b4 = s->parent;
  1031. if (DBG_ST)
  1032. dev_info(b4->dev, "%lu: hfc_timer_expire, Port %d T%d expired (value=%lu ena=%d)\n", b4->ticks, s->port + 1, t_no + 1, s->hfc_timers[t_no], s->hfc_timer_on[t_no]);
  1033. /*
  1034. * there are three timers associated with every HFC S/T port.
  1035. * T1 is used by the NT state machine, and is the maximum time the NT side should wait for G3 (active) state.
  1036. * T2 is not actually used in the driver, it is handled by the HFC-4S internally.
  1037. * T3 is used by the TE state machine; it is the maximum time the TE side should wait for the INFO4 (activated) signal.
  1038. */
  1039. /* First, disable the expired timer; hfc_force_st_state() may activate it again. */
  1040. s->hfc_timer_on[t_no] = 0;
  1041. switch(t_no) {
  1042. case HFC_T1: /* switch to G4 (pending deact.), resume auto mode */
  1043. hfc_force_st_state(b4, s->port, 4, 1);
  1044. break;
  1045. case HFC_T2: /* switch to G1 (deactivated), resume auto mode */
  1046. hfc_force_st_state(b4, s->port, 1, 1);
  1047. break;
  1048. case HFC_T3: /* switch to F3 (deactivated), resume auto mode */
  1049. hfc_force_st_state(b4, s->port, 3, 1);
  1050. break;
  1051. default:
  1052. if (printk_ratelimit())
  1053. dev_warn(b4->dev, "hfc_timer_expire found an unknown expired timer (%d)??\n", t_no);
  1054. }
  1055. }
  1056. /*
  1057. * Run through the active timers on a card and deal with any expiries.
  1058. * Also see if the alarm debounce time has expired and if it has, tell DAHDI.
  1059. */
  1060. static void hfc_update_st_timers(struct b4xxp *b4)
  1061. {
  1062. int i, j;
  1063. struct b4xxp_span *s;
  1064. for (i=0; i < b4->numspans; i++) {
  1065. s = &b4->spans[i];
  1066. for (j=HFC_T1; j <= HFC_T3; j++) {
  1067. /* we don't really do timer2, it is expired by the state change handler */
  1068. if (j == HFC_T2)
  1069. continue;
  1070. if (s->hfc_timer_on[j] && time_after_eq(b4->ticks, s->hfc_timers[j])) {
  1071. hfc_timer_expire(s, j);
  1072. }
  1073. }
  1074. if (s->newalarm != s->span.alarms && time_after_eq(b4->ticks, s->alarmtimer)) {
  1075. s->span.alarms = s->newalarm;
  1076. if ((!s->newalarm && teignorered) || (!teignorered)) {
  1077. dahdi_alarm_notify(&s->span);
  1078. }
  1079. b4xxp_set_sync_src(b4, b4xxp_find_sync(b4));
  1080. if (DBG_ALARM) {
  1081. dev_info(b4->dev,
  1082. "span %d: alarm %d "
  1083. "debounced\n",
  1084. i + 1, s->newalarm);
  1085. }
  1086. }
  1087. }
  1088. }
  1089. /* this is the driver-level state machine for an S/T port */
  1090. static void hfc_handle_state(struct b4xxp_span *s)
  1091. {
  1092. struct b4xxp *b4;
  1093. unsigned char state, sta;
  1094. int nt, newsync, oldalarm;
  1095. unsigned long oldtimer;
  1096. b4 = s->parent;
  1097. nt = !s->te_mode;
  1098. state = b4xxp_getreg_ra(b4, R_ST_SEL, s->port, A_ST_RD_STA);
  1099. sta = (state & V_ST_STA_MASK);
  1100. if (DBG_ST) {
  1101. char *x;
  1102. x = hfc_decode_st_state(b4, s->port, state, 1);
  1103. dev_info(b4->dev, "port %d A_ST_RD_STA old=0x%02x now=0x%02x, decoded: %s\n", s->port + 1, s->oldstate, state, x);
  1104. kfree(x);
  1105. }
  1106. oldalarm = s->newalarm;
  1107. oldtimer = s->alarmtimer;
  1108. if (nt) {
  1109. switch(sta) {
  1110. default: /* Invalid NT state */
  1111. case 0x0: /* NT state G0: Reset */
  1112. case 0x1: /* NT state G1: Deactivated */
  1113. case 0x4: /* NT state G4: Pending Deactivation */
  1114. s->newalarm = DAHDI_ALARM_RED;
  1115. break;
  1116. case 0x2: /* NT state G2: Pending Activation */
  1117. s->newalarm = DAHDI_ALARM_YELLOW;
  1118. break;
  1119. case 0x3: /* NT state G3: Active */
  1120. s->hfc_timer_on[HFC_T1] = 0;
  1121. s->newalarm = 0;
  1122. break;
  1123. }
  1124. } else {
  1125. switch(sta) {
  1126. default: /* Invalid TE state */
  1127. case 0x0: /* TE state F0: Reset */
  1128. case 0x2: /* TE state F2: Sensing */
  1129. case 0x3: /* TE state F3: Deactivated */
  1130. case 0x4: /* TE state F4: Awaiting Signal */
  1131. case 0x8: /* TE state F8: Lost Framing */
  1132. s->newalarm = DAHDI_ALARM_RED;
  1133. break;
  1134. case 0x5: /* TE state F5: Identifying Input */
  1135. case 0x6: /* TE state F6: Synchronized */
  1136. s->newalarm = DAHDI_ALARM_YELLOW;
  1137. break;
  1138. case 0x7: /* TE state F7: Activated */
  1139. s->hfc_timer_on[HFC_T3] = 0;
  1140. s->newalarm = 0;
  1141. break;
  1142. }
  1143. }
  1144. s->alarmtimer = b4->ticks + alarmdebounce;
  1145. s->oldstate = state;
  1146. if (DBG_ALARM) {
  1147. dev_info(b4->dev, "span %d: old alarm %d expires %ld, new alarm %d expires %ld\n",
  1148. s->port + 1, oldalarm, oldtimer, s->newalarm, s->alarmtimer);
  1149. }
  1150. /* we only care about T2 expiry in G4. */
  1151. if (nt && (sta == 4) && (state & V_T2_EXP)) {
  1152. if (s->hfc_timer_on[HFC_T2])
  1153. hfc_timer_expire(s, HFC_T2); /* handle T2 expiry */
  1154. }
  1155. /* If we're in F3 and receiving INFO0, start T3 and jump to F4 */
  1156. if (!nt && (sta == 3) && (state & V_INFO0)) {
  1157. s->hfc_timers[HFC_T3] = b4->ticks + timer_3_ms;
  1158. s->hfc_timer_on[HFC_T3] = 1;
  1159. if (DBG_ST) {
  1160. dev_info(b4->dev, "port %d: receiving INFO0 in state 3, setting T3 and jumping to F4\n", s->port + 1);
  1161. }
  1162. hfc_force_st_state(b4, s->port, 4, 1);
  1163. }
  1164. /* read in R_BERT_STA to determine where our current sync source is */
  1165. newsync = b4xxp_getreg8(b4, R_BERT_STA) & 0x07;
  1166. if (newsync != b4->syncspan) {
  1167. if (printk_ratelimit() || DBG)
  1168. dev_info(b4->dev, "new card sync source: port %d\n", newsync + 1);
  1169. b4->syncspan = newsync;
  1170. }
  1171. }
  1172. /*
  1173. * resets an S/T interface to a given NT/TE mode
  1174. */
  1175. static void hfc_reset_st(struct b4xxp_span *s)
  1176. {
  1177. int b;
  1178. struct b4xxp *b4;
  1179. b4 = s->parent;
  1180. /* force state G0/F0 (reset), then force state 1/2 (deactivated/sensing) */
  1181. b4xxp_setreg_ra(b4, R_ST_SEL, s->port, A_ST_WR_STA, V_ST_LD_STA);
  1182. flush_pci(); /* make sure write hit hardware */
  1183. udelay(10);
  1184. /* set up the clock control register. Must be done before we activate the interface. */
  1185. if (s->te_mode)
  1186. b = 0x0e;
  1187. else
  1188. b = 0x0c | (6 << V_ST_SMPL_SHIFT);
  1189. b4xxp_setreg8(b4, A_ST_CLK_DLY, b);
  1190. /* set TE/NT mode, enable B and D channels. */
  1191. b4xxp_setreg8(b4, A_ST_CTRL0, V_B1_EN | V_B2_EN | (s->te_mode ? 0 : V_ST_MD));
  1192. b4xxp_setreg8(b4, A_ST_CTRL1, V_G2_G3_EN | V_E_IGNO);
  1193. b4xxp_setreg8(b4, A_ST_CTRL2, V_B1_RX_EN | V_B2_RX_EN);
  1194. /* enable the state machine. */
  1195. b4xxp_setreg8(b4, A_ST_WR_STA, 0x00);
  1196. flush_pci();
  1197. udelay(100);
  1198. }
  1199. static void hfc_start_st(struct b4xxp_span *s)
  1200. {
  1201. struct b4xxp *b4 = s->parent;
  1202. b4xxp_setreg_ra(b4, R_ST_SEL, s->port, A_ST_WR_STA, V_ST_ACT_ACTIVATE);
  1203. /* start T1 if in NT mode, T3 if in TE mode */
  1204. if (s->te_mode) {
  1205. s->hfc_timers[HFC_T3] = b4->ticks + 500; /* 500ms wait first time, timer_t3_ms afterward. */
  1206. s->hfc_timer_on[HFC_T3] = 1;
  1207. s->hfc_timer_on[HFC_T1] = 0;
  1208. if (DBG_ST)
  1209. dev_info(b4->dev, "setting port %d t3 timer to %lu\n", s->port + 1, s->hfc_timers[HFC_T3]);
  1210. } else {
  1211. s->hfc_timers[HFC_T1] = b4->ticks + timer_1_ms;
  1212. s->hfc_timer_on[HFC_T1] = 1;
  1213. s->hfc_timer_on[HFC_T3] = 0;
  1214. if (DBG_ST)
  1215. dev_info(b4->dev, "setting port %d t1 timer to %lu\n", s->port + 1, s->hfc_timers[HFC_T1]);
  1216. }
  1217. }
  1218. #if 0 /* TODO: This function is not called anywhere */
  1219. static void hfc_stop_st(struct b4xxp_span *s)
  1220. {
  1221. b4xxp_setreg_ra(s->parent, R_ST_SEL, s->port, A_ST_WR_STA, V_ST_ACT_DEACTIVATE);
  1222. s->hfc_timer_on[HFC_T1] = 0;
  1223. s->hfc_timer_on[HFC_T2] = 0;
  1224. s->hfc_timer_on[HFC_T3] = 0;
  1225. }
  1226. #endif
  1227. /*
  1228. * read in the HFC GPIO to determine each port's mode (TE or NT).
  1229. * Then, reset and start the port.
  1230. * the flow controller should be set up before this is called.
  1231. */
  1232. static void hfc_init_all_st(struct b4xxp *b4)
  1233. {
  1234. int i, gpio, nt;
  1235. struct b4xxp_span *s;
  1236. gpio = b4xxp_getreg8(b4, R_GPI_IN3);
  1237. for (i=0; i < b4->numspans; i++) {
  1238. s = &b4->spans[i];
  1239. s->parent = b4;
  1240. s->port = i;
  1241. /* The way the Digium B410P card reads the NT/TE mode
  1242. * jumper is the oposite of how other HFC-4S cards do:
  1243. * - In B410P: GPIO=0: NT
  1244. * - In Junghanns: GPIO=0: TE
  1245. */
  1246. if (b4->card_type == B410P)
  1247. nt = ((gpio & (1 << (i + 4))) == 0);
  1248. else
  1249. nt = ((gpio & (1 << (i + 4))) != 0);
  1250. s->te_mode = !nt;
  1251. dev_info(b4->dev, "Port %d: %s mode\n", i + 1, (nt ? "NT" : "TE"));
  1252. hfc_reset_st(s);
  1253. hfc_start_st(s);
  1254. }
  1255. }
  1256. /*
  1257. * Look at one B-channel FIFO and determine if we should exchange data with it.
  1258. * It is assumed that the S/T port is active.
  1259. * returns 1 if data was exchanged, 0 otherwise.
  1260. */
  1261. static int hfc_poll_one_bchan_fifo(struct b4xxp_span *span, int c)
  1262. {
  1263. int fifo, zlen, z1, z2, ret;
  1264. unsigned long irq_flags;
  1265. struct b4xxp *b4;
  1266. struct dahdi_chan *chan;
  1267. ret = 0;
  1268. b4 = span->parent;
  1269. fifo = span->fifos[c];
  1270. chan = span->chans[c];
  1271. spin_lock_irqsave(&b4->fifolock, irq_flags);
  1272. /* select RX FIFO */
  1273. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR | V_REV);
  1274. get_Z(z1, z2, zlen);
  1275. /* TODO: error checking, full FIFO mostly */
  1276. if (zlen >= DAHDI_CHUNKSIZE) {
  1277. *(unsigned int *)&chan->readchunk[0] = b4xxp_getreg32(b4, A_FIFO_DATA2);
  1278. *(unsigned int *)&chan->readchunk[4] = b4xxp_getreg32(b4, A_FIFO_DATA2);
  1279. /*
  1280. * now TX FIFO
  1281. *
  1282. * Note that we won't write to the TX FIFO if there wasn't room in the RX FIFO.
  1283. * The TX and RX sides should be kept pretty much lock-step.
  1284. *
  1285. * Write the last byte _NOINC so that if we don't get more data in time, we aren't leaking unknown data
  1286. * (See HFC datasheet)
  1287. */
  1288. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_REV);
  1289. b4xxp_setreg32(b4, A_FIFO_DATA2, *(unsigned int *) &chan->writechunk[0]);
  1290. b4xxp_setreg32(b4, A_FIFO_DATA2, *(unsigned int *) &chan->writechunk[4]);
  1291. ret = 1;
  1292. }
  1293. spin_unlock_irqrestore(&b4->fifolock, irq_flags);
  1294. return ret;
  1295. }
  1296. /*
  1297. * Run through all of the host-facing B-channel RX FIFOs, looking for at least 8 bytes available.
  1298. * If a B channel RX fifo has enough data, perform the data transfer in both directions.
  1299. * D channel is done in an interrupt handler.
  1300. * The S/T port state must be active or we ignore the fifo.
  1301. * Returns nonzero if there was at least DAHDI_CHUNKSIZE bytes in the FIFO
  1302. */
  1303. static int hfc_poll_fifos(struct b4xxp *b4)
  1304. {
  1305. int ret=0, span;
  1306. unsigned long irq_flags;
  1307. for (span=0; span < b4->numspans; span++) {
  1308. /* Make sure DAHDI's got this span up */
  1309. if (!(b4->spans[span].span.flags & DAHDI_FLAG_RUNNING))
  1310. continue;
  1311. /* TODO: Make sure S/T port is in active state */
  1312. #if 0
  1313. if (span_not_active(s))
  1314. continue;
  1315. #endif
  1316. ret = hfc_poll_one_bchan_fifo(&b4->spans[span], 0);
  1317. ret |= hfc_poll_one_bchan_fifo(&b4->spans[span], 1);
  1318. }
  1319. /* change the active FIFO one last time to make sure the last-changed FIFO updates its pointers (as per the datasheet) */
  1320. spin_lock_irqsave(&b4->fifolock, irq_flags);
  1321. hfc_setreg_waitbusy(b4, R_FIFO, (31 << V_FIFO_NUM_SHIFT));
  1322. spin_unlock_irqrestore(&b4->fifolock, irq_flags);
  1323. return ret;
  1324. }
  1325. /* NOTE: assumes fifo lock is held */
  1326. static inline void debug_fz(struct b4xxp *b4, int fifo, const char *prefix, char *buf)
  1327. {
  1328. int f1, f2, flen, z1, z2, zlen;
  1329. get_F(f1, f2, flen);
  1330. get_Z(z1, z2, zlen);
  1331. sprintf(buf, "%s: (fifo %d): f1/f2/flen=%d/%d/%d, z1/z2/zlen=%d/%d/%d\n", prefix, fifo, f1, f2, flen, z1, z2, zlen);
  1332. }
  1333. /* enable FIFO RX int and reset the FIFO */
  1334. static int hdlc_start(struct b4xxp *b4, int fifo)
  1335. {
  1336. b4->fifo_en_txint |= (1 << fifo);
  1337. b4->fifo_en_rxint |= (1 << fifo);
  1338. hfc_reset_fifo_pair(b4, fifo, 1, 0);
  1339. return 0;
  1340. }
  1341. /* disable FIFO ints and reset the FIFO */
  1342. static void hdlc_stop(struct b4xxp *b4, int fifo)
  1343. {
  1344. b4->fifo_en_txint &= ~(1 << fifo);
  1345. b4->fifo_en_rxint &= ~(1 << fifo);
  1346. hfc_reset_fifo_pair(b4, fifo, 1, 0);
  1347. }
  1348. /*
  1349. * Inner loop for D-channel receive function.
  1350. * Retrieves a full HDLC frame from the hardware.
  1351. * If the hardware indicates that the frame is complete,
  1352. * we check the HDLC engine's STAT byte and update DAHDI as needed.
  1353. *
  1354. * Returns the number of HDLC frames left in the FIFO.
  1355. */
  1356. static int hdlc_rx_frame(struct b4xxp_span *bspan)
  1357. {
  1358. int fifo, i, j, zleft;
  1359. int z1, z2, zlen, f1, f2, flen;
  1360. unsigned char buf[WCB4XXP_HDLC_BUF_LEN];
  1361. char debugbuf[256];
  1362. unsigned long irq_flags;
  1363. struct b4xxp *b4 = bspan->parent;
  1364. unsigned char stat;
  1365. fifo = bspan->fifos[2];
  1366. spin_lock_irqsave(&b4->fifolock, irq_flags);
  1367. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  1368. get_F(f1, f2, flen);
  1369. get_Z(z1, z2, zlen);
  1370. debug_fz(b4, fifo, "hdlc_rx_frame", debugbuf);
  1371. spin_unlock_irqrestore(&b4->fifolock, irq_flags);
  1372. if (DBG_HDLC && DBG_SPANFILTER) {
  1373. pr_info("%s", debugbuf);
  1374. }
  1375. /* first check to make sure we really do have HDLC frames available to retrieve */
  1376. if (flen == 0) {
  1377. if (DBG_HDLC && DBG_SPANFILTER) {
  1378. dev_info(b4->dev, "hdlc_rx_frame(span %d): no frames available?\n",
  1379. bspan->port + 1);
  1380. }
  1381. return flen;
  1382. }
  1383. zleft = zlen + 1; /* include STAT byte that the HFC injects after FCS */
  1384. do {
  1385. int truncated;
  1386. if (zleft > WCB4XXP_HDLC_BUF_LEN) {
  1387. truncated = 1;
  1388. j = WCB4XXP_HDLC_BUF_LEN;
  1389. } else {
  1390. truncated = 0;
  1391. j = zleft;
  1392. }
  1393. spin_lock_irqsave(&b4->fifolock, irq_flags);
  1394. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  1395. for (i=0; i < j; i++)
  1396. buf[i] = b4xxp_getreg8(b4, A_FIFO_DATA0);
  1397. spin_unlock_irqrestore(&b4->fifolock, irq_flags);
  1398. /* don't send STAT byte to DAHDI */
  1399. if ((bspan->sigchan) && (j > 1))
  1400. dahdi_hdlc_putbuf(bspan->sigchan, buf, truncated ? j : j - 1);
  1401. zleft -= j;
  1402. if (DBG_HDLC && DBG_SPANFILTER) {
  1403. dev_info(b4->dev, "hdlc_rx_frame(span %d): z1/z2/zlen=%d/%d/%d, zleft=%d\n",
  1404. bspan->port + 1, z1, z2, zlen, zleft);
  1405. for (i=0; i < j; i++) printk("%02x%c", buf[i], (i < ( j - 1)) ? ' ':'\n');
  1406. }
  1407. } while (zleft > 0);
  1408. stat = buf[j - 1];
  1409. /* Frame received, increment F2 and get an updated count of frames left */
  1410. spin_lock_irqsave(&b4->fifolock, irq_flags);
  1411. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_INC_F);
  1412. get_F(f1, f2, flen);
  1413. spin_unlock_irqrestore(&b4->fifolock, irq_flags);
  1414. /* If this channel is not configured with a signalling span we don't
  1415. * need to notify the rest of dahdi about this frame. */
  1416. if (!bspan->sigchan)
  1417. return flen;
  1418. ++bspan->frames_in;
  1419. if (zlen < 3) {
  1420. if (DBG_HDLC && DBG_SPANFILTER)
  1421. dev_notice(b4->dev, "odd, zlen less then 3?\n");
  1422. dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_ABORT);
  1423. } else {
  1424. /* if STAT != 0, indicates bad frame */
  1425. if (stat != 0x00) {
  1426. if (DBG_HDLC && DBG_SPANFILTER)
  1427. dev_info(b4->dev, "(span %d) STAT=0x%02x indicates frame problem: ", bspan->port + 1, stat);
  1428. if (stat == 0xff) {
  1429. if (DBG_HDLC && DBG_SPANFILTER)
  1430. printk("HDLC Abort\n");
  1431. dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_ABORT);
  1432. } else {
  1433. if (DBG_HDLC && DBG_SPANFILTER)
  1434. printk("Bad FCS\n");
  1435. dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_BADFCS);
  1436. }
  1437. /* STAT == 0, means frame was OK */
  1438. } else {
  1439. if (DBG_HDLC && DBG_SPANFILTER)
  1440. dev_info(b4->dev, "(span %d) Frame %d is good!\n", bspan->port + 1, bspan->frames_in);
  1441. dahdi_hdlc_finish(bspan->sigchan);
  1442. }
  1443. }
  1444. return flen;
  1445. }
  1446. /*
  1447. * Takes one blob of data from DAHDI and shoots it out to the hardware.
  1448. * The blob may or may not be a complete HDLC frame.
  1449. * If it isn't, the D-channel FIFO interrupt handler will take care of pulling the rest.
  1450. * Returns nonzero if there is still data to send in the current HDLC frame.
  1451. */
  1452. static int hdlc_tx_frame(struct b4xxp_span *bspan)
  1453. {
  1454. struct b4xxp *b4 = bspan->parent;
  1455. int res, i, fifo;
  1456. int z1, z2, zlen;
  1457. unsigned char buf[WCB4XXP_HDLC_BUF_LEN];
  1458. unsigned int size = sizeof(buf) / sizeof(buf[0]);
  1459. char debugbuf[256];
  1460. unsigned long irq_flags;
  1461. /* if we're ignoring TE red alarms and we are in alarm, restart the S/T state machine */
  1462. if (bspan->te_mode && teignorered && bspan->newalarm == DAHDI_ALARM_RED) {
  1463. hfc_force_st_state(b4, bspan->port, 3, 1);
  1464. }
  1465. fifo = bspan->fifos[2];
  1466. res = dahdi_hdlc_getbuf(bspan->sigchan, buf, &size);
  1467. spin_lock_irqsave(&b4->fifolock, irq_flags);
  1468. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
  1469. get_Z(z1, z2, zlen);
  1470. debug_fz(b4, fifo, "hdlc_tx_frame", debugbuf);
  1471. /* TODO: check zlen, etc. */
  1472. if (size > 0) {
  1473. bspan->sigactive = 1;
  1474. for (i=0; i < size; i++)
  1475. b4xxp_setreg8(b4, A_FIFO_DATA0, buf[i]);
  1476. /*
  1477. * If we got a full frame from DAHDI, increment F and decrement our HDLC pending counter.
  1478. * Otherwise, select the FIFO again (to start transmission) and make sure the
  1479. * TX IRQ is enabled so we will get called again to finish off the data
  1480. */
  1481. if (res != 0) {
  1482. ++bspan->frames_out;
  1483. bspan->sigactive = 0;
  1484. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_INC_F);
  1485. atomic_dec(&bspan->hdlc_pending);
  1486. } else {
  1487. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
  1488. b4xxp_setreg8(b4, A_IRQ_MSK, V_IRQ);
  1489. }
  1490. }
  1491. /* if there are no more frames pending, disable the interrupt. */
  1492. if (res == -1) {
  1493. b4xxp_setreg8(b4, A_IRQ_MSK, 0);
  1494. }
  1495. spin_unlock_irqrestore(&b4->fifolock, irq_flags);
  1496. if (DBG_HDLC && DBG_SPANFILTER) {
  1497. dev_info(b4->dev, "%s", debugbuf);
  1498. dev_info(b4->dev, "hdlc_tx_frame(span %d): DAHDI gave %d bytes for FIFO %d (res=%d)\n",
  1499. bspan->port + 1, size, fifo, res);
  1500. for (i=0; i < size; i++)
  1501. printk("%02x%c", buf[i], (i < (size - 1)) ? ' ' : '\n');
  1502. if (size && res != 0) {
  1503. pr_info("Transmitted frame %d on span %d\n",
  1504. bspan->frames_out - 1, bspan->port + 1);
  1505. }
  1506. }
  1507. return(res == 0);
  1508. }
  1509. /*
  1510. * b4xxp lowlevel functions
  1511. * These are functions which impact more than just the HFC controller.
  1512. * (those are named hfc_xxx())
  1513. */
  1514. /*
  1515. * Performs a total reset of the card, reinitializes GPIO.
  1516. * The card is initialized enough to have LEDs running, and that's about it.
  1517. * Anything to do with audio and enabling any kind of processing is done in stage2.
  1518. */
  1519. static void b4xxp_init_stage1(struct b4xxp *b4)
  1520. {
  1521. int i;
  1522. hfc_reset(b4); /* total reset of controller */
  1523. hfc_gpio_init(b4); /* initialize controller GPIO for CPLD access */
  1524. ec_init(b4); /* initialize VPM and VPM GPIO */
  1525. b4xxp_setreg8(b4, R_IRQ_CTRL, 0x00); /* make sure interrupts are disabled */
  1526. flush_pci(); /* make sure PCI write hits hardware */
  1527. /* disable all FIFO interrupts */
  1528. for (i=0; i < HFC_NR_FIFOS; i++) {
  1529. hfc_setreg_waitbusy(b4, R_FIFO, (i << V_FIFO_NUM_SHIFT));
  1530. b4xxp_setreg8(b4, A_IRQ_MSK, 0x00); /* disable the interrupt */
  1531. hfc_setreg_waitbusy(b4, R_FIFO, (i << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  1532. b4xxp_setreg8(b4, A_IRQ_MSK, 0x00); /* disable the interrupt */
  1533. flush_pci();
  1534. }
  1535. /* clear any pending FIFO interrupts */
  1536. b4xxp_getreg8(b4, R_IRQ_FIFO_BL0);
  1537. b4xxp_getreg8(b4, R_IRQ_FIFO_BL1);
  1538. b4xxp_getreg8(b4, R_IRQ_FIFO_BL2);
  1539. b4xxp_getreg8(b4, R_IRQ_FIFO_BL3);
  1540. b4xxp_getreg8(b4, R_IRQ_FIFO_BL4);
  1541. b4xxp_getreg8(b4, R_IRQ_FIFO_BL5);
  1542. b4xxp_getreg8(b4, R_IRQ_FIFO_BL6);
  1543. b4xxp_getreg8(b4, R_IRQ_FIFO_BL7);
  1544. b4xxp_setreg8(b4, R_SCI_MSK, 0x00); /* mask off all S/T interrupts */
  1545. b4xxp_setreg8(b4, R_IRQMSK_MISC, 0x00); /* nothing else can generate an interrupt */
  1546. /*
  1547. * set up the clock controller B410P & Cologne Eval Board have a
  1548. * 24.576MHz crystal, so the PCM clock is 2x the incoming clock.
  1549. * Other cards have a 49.152Mhz crystal, so the PCM clock equals
  1550. * incoming clock.
  1551. */
  1552. if ((b4->card_type == B410P) || (b4->card_type == QUADBRI_EVAL))
  1553. b4xxp_setreg8(b4, R_BRG_PCM_CFG, 0x02);
  1554. else
  1555. b4xxp_setreg8(b4, R_BRG_PCM_CFG, V_PCM_CLK);
  1556. flush_pci();
  1557. udelay(100); /* wait a bit for clock to settle */
  1558. create_sysfs_files(b4);
  1559. }
  1560. /*
  1561. * Stage 2 hardware init.
  1562. * Sets up the flow controller, PCM and FIFOs.
  1563. * Initializes the echo cancellers.
  1564. * S/T interfaces are not initialized here, that is done later, in hfc_init_all_st().
  1565. * Interrupts are enabled and once the s/t interfaces are configured, chip should be pretty much operational.
  1566. */
  1567. static void b4xxp_init_stage2(struct b4xxp *b4)
  1568. {
  1569. int span;
  1570. /*
  1571. * set up PCM bus.
  1572. * HFC is PCM master.
  1573. * C4IO, SYNC_I and SYNC_O unused.
  1574. * 32 channels, frame signal positive polarity, active for 2 C4 clocks.
  1575. * only the first two timeslots in each quad are active
  1576. * STIO0 is transmit-only, STIO1 is receive-only.
  1577. */
  1578. b4xxp_setreg8(b4, R_PCM_MD0, V_PCM_MD | V_PCM_IDX_MD1);
  1579. flush_pci();
  1580. b4xxp_setreg8(b4, R_PCM_MD1, V_PLL_ADJ_00 | V_PCM_DR_2048);
  1581. b4xxp_setreg8(b4, R_PWM_MD, 0xa0);
  1582. b4xxp_setreg8(b4, R_PWM0, 0x1b);
  1583. /*
  1584. * set up the flow controller.
  1585. * B channel map: (4 ports cards with Hardware Echo Cancel present & active)
  1586. * FIFO 0 connects Port 1 B0 using HFC channel 16 and PCM timeslots 0/1.
  1587. * FIFO 1 connects Port 1 B1 using HFC channel 17 and PCM timeslots 4/5.
  1588. * FIFO 2 connects Port 2 B0 using HFC channel 20 and PCM timeslots 8/9.
  1589. * FIFO 3 connects Port 2 B1 using HFC channel 21 and PCM timeslots 12/13.
  1590. * FIFO 4 connects Port 3 B0 using HFC channel 24 and PCM timeslots 16/17.
  1591. * FIFO 5 connects Port 3 B1 using HFC channel 25 and PCM timeslots 20/21.
  1592. * FIFO 6 connects Port 4 B0 using HFC channel 28 and PCM timeslots 24/25.
  1593. * FIFO 7 connects Port 4 B1 using HFC channel 29 and PCM timeslots 28/29.
  1594. *
  1595. * All B channel FIFOs have their HDLC controller in transparent mode,
  1596. * and only the FIFO for B0 on each port has its interrupt operational.
  1597. *
  1598. * D channels are handled by FIFOs 8-11.
  1599. * FIFO 8 connects Port 1 D using HFC channel 3
  1600. * FIFO 9 connects Port 2 D using HFC channel 7
  1601. * FIFO 10 connects Port 3 D using HFC channel 11
  1602. * FIFO 11 connects Port 4 D using HFC channel 15
  1603. *
  1604. * D channel FIFOs are operated in HDLC mode and interrupt on end of frame.
  1605. *
  1606. * B channel map: (8 ports cards without Hardware Echo Cancel)
  1607. * FIFO 0 connects Port 1 B0 using HFC channel 0
  1608. * FIFO 1 connects Port 1 B1 using HFC channel 1
  1609. * FIFO 2 connects Port 2 B0 using HFC channel 4
  1610. * FIFO 3 connects Port 2 B1 using HFC channel 5
  1611. * .........................
  1612. * FIFO 14 connects Port 8 B0 using HFC channel 28
  1613. * FIFO 15 connects Port 8 B1 using HFC channel 29
  1614. *
  1615. * All B channel FIFOs have their HDLC controller in transparent mode,
  1616. * and only the FIFO for B0 on each port has its interrupt operational.
  1617. *
  1618. * D channels are handled by FIFOs 16-23.
  1619. * FIFO 16 connects Port 1 D using HFC channel 3
  1620. * FIFO 17 connects Port 2 D using HFC channel 7
  1621. * FIFO 18 connects Port 3 D using HFC channel 11
  1622. * FIFO 19 connects Port 4 D using HFC channel 15
  1623. * ................
  1624. * FIFO 23 connects Port 8 D using HFC channel 31
  1625. * D channel FIFOs are operated in HDLC mode and interrupt on end of frame.
  1626. */
  1627. for (span=0; span < b4->numspans; span++) {
  1628. if ((vpmsupport) && (CARD_HAS_EC(b4))) {
  1629. hfc_assign_bchan_fifo_ec(b4, span, 0);
  1630. hfc_assign_bchan_fifo_ec(b4, span, 1);
  1631. } else {
  1632. hfc_assign_bchan_fifo_noec(b4, span, 0);
  1633. hfc_assign_bchan_fifo_noec(b4, span, 1);
  1634. }
  1635. hfc_assign_dchan_fifo(b4, span);
  1636. }
  1637. /* set up the timer interrupt for 1ms intervals */
  1638. b4xxp_setreg8(b4, R_TI_WD, (2 << V_EV_TS_SHIFT));
  1639. /*
  1640. * At this point, everything's set up and ready to go.
  1641. * Don't actually enable the global interrupt pin.
  1642. * DAHDI still needs to start up the spans, and we don't know exactly when.
  1643. */
  1644. }
  1645. static void b4xxp_setleds(struct b4xxp *b4, unsigned char val)
  1646. {
  1647. ec_write(b4, 0, 0x1a8 + 3, val);
  1648. }
  1649. static void b4xxp_update_leds_hfc_8s(struct b4xxp *b4)
  1650. {
  1651. unsigned long lled = 0; /* A bit set is a led OFF */
  1652. unsigned long leddw;
  1653. int j;
  1654. struct b4xxp_span *bspan;
  1655. b4->blinktimer++;
  1656. for (j = 7; j >= 0; j--) {
  1657. bspan = &b4->spans[7 - j];
  1658. if (!(bspan->span.flags & DAHDI_FLAG_RUNNING) ||
  1659. bspan->span.alarms) {
  1660. BIT_SET(lled, j);
  1661. continue; /* Led OFF */
  1662. }
  1663. if (bspan->span.mainttimer || bspan->span.maintstat) {
  1664. /* Led Blinking in maint state */
  1665. if (b4->blinktimer >= 0x7f)
  1666. BIT_SET(lled, j);
  1667. }
  1668. /* Else: Led on */
  1669. }
  1670. /* Write Leds...*/
  1671. leddw = lled << 24 | lled << 16 | lled << 8 | lled;
  1672. b4xxp_setreg8(b4, R_BRG_PCM_CFG, 0x21);
  1673. iowrite16(0x4000, b4->ioaddr + 4);
  1674. iowrite32(leddw, b4->ioaddr);
  1675. b4xxp_setreg8(b4, R_BRG_PCM_CFG, 0x20);
  1676. if (b4->blinktimer == 0xff)
  1677. b4->blinktimer = -1;
  1678. }
  1679. /* So far only tested for OpenVox cards. Please test it for other hardware */
  1680. static void b4xxp_update_leds_hfc(struct b4xxp *b4)
  1681. {
  1682. int i;
  1683. int leds = 0, green_leds = 0; /* Default: off */
  1684. struct b4xxp_span *bspan;
  1685. b4->blinktimer++;
  1686. for (i=0; i < b4->numspans; i++) {
  1687. bspan = &b4->spans[i];
  1688. if (!(bspan->span.flags & DAHDI_FLAG_RUNNING))
  1689. continue; /* Leds are off */
  1690. if (bspan->span.alarms) {
  1691. /* Red blinking -> Alarm */
  1692. if (b4->blinktimer >= 0x7f)
  1693. BIT_SET(leds, i);
  1694. } else if (bspan->span.mainttimer || bspan->span.maintstat) {
  1695. /* Green blinking -> Maint status */
  1696. if (b4->blinktimer >= 0x7f)
  1697. BIT_SET(green_leds, i);
  1698. } else {
  1699. /* Steady grean -> No Alarm */
  1700. BIT_SET(green_leds, i);
  1701. }
  1702. }
  1703. /* Actually set them. for red: just set the bit in R_GPIO_EN1.
  1704. For green: in both R_GPIO_EN1 and R_GPIO_OUT1. */
  1705. leds |= green_leds;
  1706. b4xxp_setreg8(b4, R_GPIO_EN1, leds);
  1707. b4xxp_setreg8(b4, R_GPIO_OUT1, green_leds);
  1708. if (b4->blinktimer == 0xff)
  1709. b4->blinktimer = -1;
  1710. }
  1711. static void b4xxp_set_span_led(struct b4xxp *b4, int span, unsigned char val)
  1712. {
  1713. int shift, spanmask;
  1714. shift = span << 1;
  1715. spanmask = ~(0x03 << shift);
  1716. b4->ledreg &= spanmask;
  1717. b4->ledreg |= (val << shift);
  1718. b4xxp_setleds(b4, b4->ledreg);
  1719. }
  1720. static void b4xxp_update_leds(struct b4xxp *b4)
  1721. {
  1722. int i;
  1723. struct b4xxp_span *bspan;
  1724. if (b4->numspans == 8) {
  1725. /* Use the alternative function for non-Digium HFC-8S cards */
  1726. b4xxp_update_leds_hfc_8s(b4);
  1727. return;
  1728. }
  1729. if (b4->card_type != B410P) {
  1730. /* Use the alternative function for non-Digium HFC-4S cards */
  1731. b4xxp_update_leds_hfc(b4);
  1732. return;
  1733. }
  1734. b4->blinktimer++;
  1735. for (i=0; i < b4->numspans; i++) {
  1736. bspan = &b4->spans[i];
  1737. if (bspan->span.flags & DAHDI_FLAG_RUNNING) {
  1738. if (bspan->span.alarms) {
  1739. if (b4->blinktimer == (led_fader_table[b4->alarmpos] >> 1))
  1740. b4xxp_set_span_led(b4, i, LED_RED);
  1741. if (b4->blinktimer == 0xf)
  1742. b4xxp_set_span_led(b4, i, LED_OFF);
  1743. } else if (bspan->span.mainttimer || bspan->span.maintstat) {
  1744. if (b4->blinktimer == (led_fader_table[b4->alarmpos] >> 1))
  1745. b4xxp_set_span_led(b4, i, LED_GREEN);
  1746. if (b4->blinktimer == 0xf)
  1747. b4xxp_set_span_led(b4, i, LED_OFF);
  1748. } else {
  1749. /* No Alarm */
  1750. b4xxp_set_span_led(b4, i, LED_GREEN);
  1751. }
  1752. } else
  1753. b4xxp_set_span_led(b4, i, LED_OFF);
  1754. }
  1755. if (b4->blinktimer == 0xf) {
  1756. b4->blinktimer = -1;
  1757. b4->alarmpos++;
  1758. if (b4->alarmpos >= (sizeof(led_fader_table) / sizeof(led_fader_table[0])))
  1759. b4->alarmpos = 0;
  1760. }
  1761. }
  1762. static int b4xxp_echocan_create(struct dahdi_chan *chan,
  1763. struct dahdi_echocanparams *ecp,
  1764. struct dahdi_echocanparam *p,
  1765. struct dahdi_echocan_state **ec)
  1766. {
  1767. struct b4xxp_span *bspan = container_of(chan->span, struct b4xxp_span, span);
  1768. int channel;
  1769. if (!vpmsupport || !CARD_HAS_EC(bspan->parent))
  1770. return -ENODEV;
  1771. if (chan->chanpos == 3) {
  1772. printk(KERN_WARNING "Cannot enable echo canceller on D channel of span %d; failing request\n", chan->span->offset);
  1773. return -EINVAL;
  1774. }
  1775. if (ecp->param_count > 0) {
  1776. printk(KERN_WARNING "wcb4xxp echo canceller does not support parameters; failing request\n");
  1777. return -EINVAL;
  1778. }
  1779. *ec = &bspan->ec[chan->chanpos];
  1780. (*ec)->ops = &my_ec_ops;
  1781. (*ec)->features = my_ec_features;
  1782. if (DBG_EC)
  1783. printk("Enabling echo cancellation on chan %d span %d\n", chan->chanpos, chan->span->offset);
  1784. channel = (chan->span->offset * 8) + ((chan->chanpos - 1) * 4) + 1;
  1785. ec_write(bspan->parent, chan->chanpos - 1, channel, 0x7e);
  1786. return 0;
  1787. }
  1788. static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
  1789. {
  1790. struct b4xxp_span *bspan = container_of(chan->span, struct b4xxp_span, span);
  1791. int channel;
  1792. memset(ec, 0, sizeof(*ec));
  1793. if (DBG_EC)
  1794. printk("Disabling echo cancellation on chan %d span %d\n", chan->chanpos, chan->span->offset);
  1795. channel = (chan->span->offset * 8) + ((chan->chanpos - 1) * 4) + 1;
  1796. ec_write(bspan->parent, chan->chanpos - 1, channel, 0x01);
  1797. }
  1798. /*
  1799. * Filesystem and DAHDI interfaces
  1800. */
  1801. static int b4xxp_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
  1802. {
  1803. switch(cmd) {
  1804. default:
  1805. return -ENOTTY;
  1806. }
  1807. return 0;
  1808. }
  1809. static int b4xxp_startup(struct dahdi_span *span)
  1810. {
  1811. struct b4xxp_span *bspan = container_of(span, struct b4xxp_span, span);
  1812. struct b4xxp *b4 = bspan->parent;
  1813. if (!b4->running)
  1814. hfc_enable_interrupts(bspan->parent);
  1815. return 0;
  1816. }
  1817. static int b4xxp_shutdown(struct dahdi_span *span)
  1818. {
  1819. struct b4xxp_span *bspan = container_of(span, struct b4xxp_span, span);
  1820. hfc_disable_interrupts(bspan->parent);
  1821. return 0;
  1822. }
  1823. /* resets all the FIFOs for a given span. Disables IRQs for the span FIFOs */
  1824. static void b4xxp_reset_span(struct b4xxp_span *bspan)
  1825. {
  1826. int i;
  1827. struct b4xxp *b4 = bspan->parent;
  1828. for (i=0; i < 3; i++) {
  1829. hfc_reset_fifo_pair(b4, bspan->fifos[i], (i == 2) ? 1 : 0, 1);
  1830. }
  1831. b4xxp_set_sync_src(b4, b4xxp_find_sync(b4));
  1832. }
  1833. /* spanconfig for us means to set up the HFC FIFO and channel mapping */
  1834. static int b4xxp_spanconfig(struct dahdi_span *span, struct dahdi_lineconfig *lc)
  1835. {
  1836. int i;
  1837. struct b4xxp_span *bspan = container_of(span, struct b4xxp_span, span);
  1838. struct b4xxp *b4 = bspan->parent;
  1839. if (DBG)
  1840. dev_info(b4->dev, "Configuring span %d offset %d to be sync %d\n", span->spanno, span->offset, lc->sync);
  1841. #if 0
  1842. if (lc->sync > 0 && !bspan->te_mode) {
  1843. dev_info(b4->dev, "Span %d is not in NT mode, removing from sync source list\n", span->spanno);
  1844. lc->sync = 0;
  1845. }
  1846. #endif
  1847. if (lc->sync < 0 || lc->sync > 4) {
  1848. dev_info(b4->dev, "Span %d has invalid sync priority (%d), removing from sync source list\n", span->spanno, lc->sync);
  1849. lc->sync = 0;
  1850. }
  1851. /* remove this span number from the current sync sources, if there */
  1852. for (i = 0; i < b4->numspans; i++) {
  1853. if (b4->spans[i].sync == (span->offset + 1)) {
  1854. b4->spans[i].sync = 0;
  1855. }
  1856. }
  1857. /* if a sync src, put it in proper place */
  1858. b4->spans[span->offset].syncpos = lc->sync;
  1859. if (lc->sync) {
  1860. b4->spans[lc->sync - 1].sync = (span->offset + 1);
  1861. }
  1862. b4xxp_reset_span(bspan);
  1863. /* call startup() manually here, because DAHDI won't call the startup function unless it receives an IOCTL to do so, and dahdi_cfg doesn't. */
  1864. b4xxp_startup(&bspan->span);
  1865. span->flags |= DAHDI_FLAG_RUNNING;
  1866. return 0;
  1867. }
  1868. /* chanconfig for us means to configure the HDLC controller, if appropriate */
  1869. static int b4xxp_chanconfig(struct dahdi_chan *chan, int sigtype)
  1870. {
  1871. int alreadyrunning;
  1872. struct b4xxp *b4 = chan->pvt;
  1873. struct b4xxp_span *bspan = &b4->spans[chan->span->offset];
  1874. int fifo = bspan->fifos[2];
  1875. alreadyrunning = bspan->span.flags & DAHDI_FLAG_RUNNING;
  1876. if (DBG_FOPS) {
  1877. dev_info(b4->dev, "%s channel %d (%s) sigtype %08x\n",
  1878. alreadyrunning ? "Reconfigured" : "Configured", chan->channo, chan->name, sigtype);
  1879. }
  1880. /* (re)configure signalling channel */
  1881. if ((sigtype == DAHDI_SIG_HARDHDLC) || (bspan->sigchan == chan)) {
  1882. if (DBG_FOPS)
  1883. dev_info(b4->dev, "%sonfiguring hardware HDLC on %s\n",
  1884. ((sigtype == DAHDI_SIG_HARDHDLC) ? "C" : "Unc"), chan->name);
  1885. if (alreadyrunning && bspan->sigchan) {
  1886. hdlc_stop(b4, fifo);
  1887. atomic_set(&bspan->hdlc_pending, 0);
  1888. bspan->sigactive = 0;
  1889. smp_mb();
  1890. bspan->sigchan = NULL;
  1891. }
  1892. if (sigtype == DAHDI_SIG_HARDHDLC) {
  1893. if (hdlc_start(b4, fifo)) {
  1894. dev_warn(b4->dev, "Error initializing signalling controller\n");
  1895. return -1;
  1896. }
  1897. }
  1898. bspan->sigchan = (sigtype == DAHDI_SIG_HARDHDLC) ? chan : NULL;
  1899. bspan->sigactive = 0;
  1900. atomic_set(&bspan->hdlc_pending, 0);
  1901. } else {
  1902. /* FIXME: shouldn't I be returning an error? */
  1903. }
  1904. return 0;
  1905. }
  1906. static int b4xxp_open(struct dahdi_chan *chan)
  1907. {
  1908. struct b4xxp *b4 = chan->pvt;
  1909. struct b4xxp_span *bspan = &b4->spans[chan->span->offset];
  1910. if (DBG_FOPS && DBG_SPANFILTER)
  1911. dev_info(b4->dev, "open() on chan %s (%i/%i)\n", chan->name, chan->channo, chan->chanpos);
  1912. hfc_reset_fifo_pair(b4, bspan->fifos[chan->chanpos - 1], 0, 0);
  1913. return 0;
  1914. }
  1915. static int b4xxp_close(struct dahdi_chan *chan)
  1916. {
  1917. struct b4xxp *b4 = chan->pvt;
  1918. struct b4xxp_span *bspan = &b4->spans[chan->span->offset];
  1919. if (DBG_FOPS && DBG_SPANFILTER)
  1920. dev_info(b4->dev, "close() on chan %s (%i/%i)\n", chan->name, chan->channo, chan->chanpos);
  1921. hfc_reset_fifo_pair(b4, bspan->fifos[chan->chanpos - 1], 1, 1);
  1922. return 0;
  1923. }
  1924. /* DAHDI calls this when it has data it wants to send to the HDLC controller */
  1925. static void b4xxp_hdlc_hard_xmit(struct dahdi_chan *chan)
  1926. {
  1927. struct b4xxp *b4 = chan->pvt;
  1928. int span = chan->span->offset;
  1929. struct b4xxp_span *bspan = &b4->spans[span];
  1930. if ((DBG_FOPS || DBG_HDLC) && DBG_SPANFILTER)
  1931. dev_info(b4->dev, "hdlc_hard_xmit on chan %s (%i/%i), span=%i\n",
  1932. chan->name, chan->channo, chan->chanpos, span + 1);
  1933. /*
  1934. * increment the hdlc_pending counter and trigger the bottom-half so it
  1935. * will be picked up and sent.
  1936. */
  1937. if (bspan->sigchan == chan) {
  1938. atomic_inc(&bspan->hdlc_pending);
  1939. }
  1940. }
  1941. /* internal functions, not specific to the hardware or DAHDI */
  1942. static const struct dahdi_span_ops b4xxp_span_ops = {
  1943. .owner = THIS_MODULE,
  1944. .spanconfig = b4xxp_spanconfig,
  1945. .chanconfig = b4xxp_chanconfig,
  1946. .startup = b4xxp_startup,
  1947. .shutdown = b4xxp_shutdown,
  1948. .open = b4xxp_open,
  1949. .close = b4xxp_close,
  1950. .ioctl = b4xxp_ioctl,
  1951. .hdlc_hard_xmit = b4xxp_hdlc_hard_xmit,
  1952. .echocan_create = b4xxp_echocan_create,
  1953. };
  1954. /* initialize the span/chan structures. Doesn't touch hardware, although the callbacks might. */
  1955. static void init_spans(struct b4xxp *b4)
  1956. {
  1957. int i, j;
  1958. struct b4xxp_span *bspan;
  1959. struct dahdi_chan *chan;
  1960. /* for each span on the card */
  1961. for (i=0; i < b4->numspans; i++) {
  1962. bspan = &b4->spans[i];
  1963. bspan->parent = b4;
  1964. bspan->span.irq = b4->pdev->irq;
  1965. bspan->span.spantype = (bspan->te_mode) ? "TE" : "NT";
  1966. bspan->span.offset = i;
  1967. bspan->span.channels = WCB4XXP_CHANNELS_PER_SPAN;
  1968. bspan->span.flags = 0;
  1969. if (!strcasecmp(companding, "ulaw"))
  1970. bspan->span.deflaw = DAHDI_LAW_MULAW;
  1971. else
  1972. bspan->span.deflaw = DAHDI_LAW_ALAW;
  1973. /* For simplicty, we'll accept all line modes since BRI
  1974. * ignores this setting anyway.*/
  1975. bspan->span.linecompat = DAHDI_CONFIG_AMI |
  1976. DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4 |
  1977. DAHDI_CONFIG_ESF | DAHDI_CONFIG_HDB3 |
  1978. DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
  1979. sprintf(bspan->span.name, "B4/%d/%d", b4->cardno, i+1);
  1980. sprintf(bspan->span.desc, "B4XXP (PCI) Card %d Span %d", b4->cardno, i+1);
  1981. bspan->span.manufacturer = "Digium";
  1982. dahdi_copy_string(bspan->span.devicetype, b4->variety, sizeof(bspan->span.devicetype));
  1983. sprintf(bspan->span.location, "PCI Bus %02d Slot %02d",
  1984. b4->pdev->bus->number, PCI_SLOT(b4->pdev->devfn) + 1);
  1985. bspan->span.ops = &b4xxp_span_ops;
  1986. /* HDLC stuff */
  1987. bspan->sigchan = NULL;
  1988. bspan->sigactive = 0;
  1989. bspan->span.chans = bspan->chans;
  1990. init_waitqueue_head(&bspan->span.maintq);
  1991. /* now initialize each channel in the span */
  1992. for (j=0; j < WCB4XXP_CHANNELS_PER_SPAN; j++) {
  1993. bspan->chans[j] = &bspan->_chans[j];
  1994. chan = bspan->chans[j];
  1995. chan->pvt = b4;
  1996. sprintf(chan->name, "B4/%d/%d/%d", b4->cardno, i + 1, j + 1);
  1997. /* The last channel in the span is the D-channel */
  1998. if (j == WCB4XXP_CHANNELS_PER_SPAN - 1) {
  1999. chan->sigcap = DAHDI_SIG_HARDHDLC;
  2000. } else {
  2001. chan->sigcap = DAHDI_SIG_CLEAR | DAHDI_SIG_DACS;
  2002. }
  2003. chan->chanpos = j + 1;
  2004. chan->writechunk = (void *)(bspan->writechunk + j * DAHDI_CHUNKSIZE);
  2005. chan->readchunk = (void *)(bspan->readchunk + j * DAHDI_CHUNKSIZE);
  2006. }
  2007. }
  2008. }
  2009. static void b4xxp_bottom_half(unsigned long data);
  2010. /* top-half interrupt handler */
  2011. DAHDI_IRQ_HANDLER(b4xxp_interrupt)
  2012. {
  2013. struct b4xxp *b4 = dev_id;
  2014. unsigned char status;
  2015. int i;
  2016. /* Make sure it's really for us */
  2017. status = __pci_in8(b4, R_STATUS);
  2018. if (!(status & HFC_INTS))
  2019. return IRQ_NONE;
  2020. /*
  2021. * since the interrupt is for us, read in the FIFO and misc IRQ status registers.
  2022. * Don't replace the struct copies; OR in the new bits instead.
  2023. * That way if we get behind, we don't lose anything.
  2024. * We don't actually do any processing here, we simply flag the bottom-half to do the heavy lifting.
  2025. */
  2026. if (status & V_FR_IRQSTA) {
  2027. b4->fifo_irqstatus[0] |= __pci_in8(b4, R_IRQ_FIFO_BL0);
  2028. b4->fifo_irqstatus[1] |= __pci_in8(b4, R_IRQ_FIFO_BL1);
  2029. b4->fifo_irqstatus[2] |= __pci_in8(b4, R_IRQ_FIFO_BL2);
  2030. b4->fifo_irqstatus[3] |= __pci_in8(b4, R_IRQ_FIFO_BL3);
  2031. b4->fifo_irqstatus[4] |= __pci_in8(b4, R_IRQ_FIFO_BL4);
  2032. b4->fifo_irqstatus[5] |= __pci_in8(b4, R_IRQ_FIFO_BL5);
  2033. b4->fifo_irqstatus[6] |= __pci_in8(b4, R_IRQ_FIFO_BL6);
  2034. b4->fifo_irqstatus[7] |= __pci_in8(b4, R_IRQ_FIFO_BL7);
  2035. }
  2036. if (status & V_MISC_IRQSTA) {
  2037. b4->misc_irqstatus |= __pci_in8(b4, R_IRQ_MISC);
  2038. }
  2039. /*
  2040. * Well, that was the plan. It appears that I can't do this in the bottom half
  2041. * or I start to see data corruption (too long a time between IRQ and tasklet??)
  2042. * So, I do the B-channel stuff right here in interrupt context. yuck.
  2043. */
  2044. if (b4->misc_irqstatus & V_TI_IRQ) {
  2045. hfc_poll_fifos(b4);
  2046. for (i=0; i < b4->numspans; i++) {
  2047. if (b4->spans[i].span.flags & DAHDI_FLAG_RUNNING) {
  2048. dahdi_ec_span(&b4->spans[i].span);
  2049. dahdi_receive(&b4->spans[i].span);
  2050. dahdi_transmit(&b4->spans[i].span);
  2051. }
  2052. }
  2053. }
  2054. /* kick off bottom-half handler */
  2055. /* tasklet_hi_schedule(&b4->b4xxp_tlet); */
  2056. b4xxp_bottom_half((unsigned long)b4);
  2057. return IRQ_RETVAL(1);
  2058. }
  2059. /*
  2060. * The bottom half of course does all the heavy lifting for the interrupt.
  2061. *
  2062. * The original plan was to have the B channel RX FIFO interrupts enabled, and
  2063. * to do the actual work here. Since that doesn't seem to work so well, we
  2064. * poll the B channel FIFOs right in the interrupt handler and take care of the B
  2065. * channel stuff there. The bottom half works for the timer interrupt and D
  2066. * channel stuff.
  2067. *
  2068. * The HFC-4S timer interrupt is used to for several things:
  2069. * - Update the S/T state machines, expire their timers, etc.
  2070. * - Provide DAHDI's timing source, if so configured
  2071. * - Update LEDs
  2072. */
  2073. static void b4xxp_bottom_half(unsigned long data)
  2074. {
  2075. struct b4xxp *b4 = (struct b4xxp *)data;
  2076. int i, j, k, gotrxfifo, fifo, fifo_low, fifo_high;
  2077. unsigned char b, b2;
  2078. if (b4->shutdown)
  2079. return;
  2080. gotrxfifo = 0;
  2081. /* HFC-4S d-chan fifos 8-11 *** HFC-8S d-chan fifos 16-23 */
  2082. if (b4->numspans == 8) {
  2083. fifo_low = 16;
  2084. fifo_high = 23;
  2085. } else {
  2086. fifo_low = 8;
  2087. fifo_high = 11;
  2088. }
  2089. for (i=0; i < 8; i++) {
  2090. b = b2 = b4->fifo_irqstatus[i];
  2091. for (j=0; j < b4->numspans; j++) {
  2092. fifo = i*4 + j;
  2093. if (b & V_IRQ_FIFOx_TX) {
  2094. if (fifo >= fifo_low && fifo <= fifo_high) {
  2095. /* d-chan fifos */
  2096. /*
  2097. * WOW I don't like this.
  2098. * It's bad enough that I have to send a fake frame to get an HDLC TX FIFO interrupt,
  2099. * but now, I have to loop until the whole frame is read, or I get RX interrupts
  2100. * (even though the chip says HDLC mode gives an IRQ when a *full frame* is received).
  2101. * Yuck. It works well, but yuck.
  2102. */
  2103. do {
  2104. k = hdlc_tx_frame(&b4->spans[fifo - fifo_low]);
  2105. } while (k);
  2106. } else {
  2107. if (printk_ratelimit())
  2108. dev_warn(b4->dev, "Got FIFO TX int from non-d-chan FIFO %d??\n", fifo);
  2109. }
  2110. }
  2111. if (b & V_IRQ_FIFOx_RX) {
  2112. if (fifo >= fifo_low && fifo <= fifo_high) { /* dchan fifos */
  2113. /*
  2114. * I have to loop here until hdlc_rx_frame says there are no more frames waiting.
  2115. * for whatever reason, the HFC will not generate another interrupt if there are
  2116. * still HDLC frames waiting to be received.
  2117. * i.e. I get an int when F1 changes, not when F1 != F2.
  2118. */
  2119. do {
  2120. k = hdlc_rx_frame(&b4->spans[fifo - fifo_low]);
  2121. } while (k);
  2122. } else {
  2123. if (printk_ratelimit())
  2124. dev_warn(b4->dev, "Got FIFO RX int from non-d-chan FIFO %d??\n", fifo);
  2125. }
  2126. }
  2127. b >>= 2;
  2128. }
  2129. /* zero the bits we just processed */
  2130. b4->fifo_irqstatus[i] &= ~b2;
  2131. }
  2132. /*
  2133. * timer interrupt
  2134. * every tick (1ms), check the FIFOs and run through the S/T port timers.
  2135. * every 100ms or so, look for S/T state machine changes.
  2136. */
  2137. if (b4->misc_irqstatus & V_TI_IRQ) {
  2138. /*
  2139. * We should check the FIFOs here, but I'm seeing this tasklet getting scheduled FAR too late to be useful.
  2140. * For now, we're handling that in the IRQ handler itself. (ICK!!)
  2141. */
  2142. b4->ticks++;
  2143. hfc_update_st_timers(b4);
  2144. b4xxp_update_leds(b4);
  2145. /* every 100ms or so, look at the S/T interfaces to see if they changed state */
  2146. if (!(b4->ticks % 100)) {
  2147. b = b4xxp_getreg8(b4, R_SCI);
  2148. if (b) {
  2149. for (i=0; i < b4->numspans; i++) {
  2150. if (b & (1 << i))
  2151. hfc_handle_state(&b4->spans[i]);
  2152. }
  2153. }
  2154. }
  2155. /* We're supposed to kick DAHDI here, too, but again, seeing too much latency between the interrupt and the bottom-half. */
  2156. /* clear the timer interrupt flag. */
  2157. b4->misc_irqstatus &= ~V_TI_IRQ;
  2158. }
  2159. /*
  2160. * Check for outgoing HDLC frame requests
  2161. * The HFC does not generate TX interrupts when there is room to send, so
  2162. * I use an atomic counter that is incremented every time DAHDI wants to send
  2163. * a frame, and decremented every time I send a frame. It'd be better if I could
  2164. * just use the interrupt handler, but the HFC seems to trigger a FIFO TX IRQ
  2165. * only when it has finished sending a frame, not when one can be sent.
  2166. */
  2167. for (i=0; i < b4->numspans; i++) {
  2168. struct b4xxp_span *bspan = &b4->spans[i];
  2169. if (atomic_read(&bspan->hdlc_pending)) {
  2170. do {
  2171. k = hdlc_tx_frame(bspan);
  2172. } while (k);
  2173. }
  2174. }
  2175. }
  2176. /********************************************************************************* proc stuff *****/
  2177. #ifdef CREATE_WCB4XXP_PROCFS_ENTRY
  2178. static int b4xxp_proc_read_one(char *buf, struct b4xxp *b4)
  2179. {
  2180. struct dahdi_chan *chan;
  2181. int len, i, j;
  2182. char str[80], sBuf[4096];
  2183. *sBuf=0;
  2184. sprintf(sBuf, "Card %d, PCI identifier %s, IRQ %d\n", b4->cardno + 1, b4->dev->bus_id, b4->irq);
  2185. strcat(sBuf,"Tx:\n");
  2186. for (j=0; j<(b4->numspans * 2) ; j++) { /* B Channels */
  2187. for (i=0; i<(b4->numspans * 3) ; i++) { /* All Channels */
  2188. chan = b4->spans[i/3].chans[i%3];
  2189. sprintf(str, "%02x ", chan->writechunk[j]);
  2190. strcat(sBuf, str);
  2191. }
  2192. strcat(sBuf, "\n");
  2193. }
  2194. strcat(sBuf, "\nRx:\n");
  2195. for (j=0; j < (b4->numspans * 2); j++) { /* B Channels */
  2196. for (i=0; i < (b4->numspans * 3); i++) { /* All Channels */
  2197. chan = b4->spans[i / 3].chans[i % 3];
  2198. sprintf(str, "%02x%c", chan->readchunk[j], (i == 11) ? '\n' : ' ');
  2199. strcat(sBuf, str);
  2200. }
  2201. }
  2202. strcat(sBuf, "\nPort states:\n");
  2203. for (i=0; i < b4->numspans; i++) {
  2204. int state;
  2205. char *x;
  2206. struct b4xxp_span *s = &b4->spans[i];
  2207. state = b4xxp_getreg_ra(b4, R_ST_SEL, s->port, A_ST_RD_STA);
  2208. x = hfc_decode_st_state(b4, s->port, state, 0);
  2209. sprintf(str, "%s\n", x);
  2210. strcat(sBuf, str);
  2211. kfree(x);
  2212. }
  2213. len = sprintf(buf, "%s\n%s\nTicks: %ld\n", sBuf, str, b4->ticks);
  2214. return len;
  2215. }
  2216. static int b4xxp_proc_read(char *buf, char **start, off_t offset, int count, int *eof, void *data)
  2217. {
  2218. struct b4xxp **b4_cards = data;
  2219. char sBuf[256];
  2220. int i, len;
  2221. len = sprintf(buf, "WCB4XXP Card Information\n");
  2222. for (i=0; b4_cards[i] != NULL; i++) {
  2223. if (i)
  2224. len += sprintf(buf + len, "\n-----\n");
  2225. len += b4xxp_proc_read_one(buf + len, b4_cards[i]);
  2226. }
  2227. *sBuf = 0;
  2228. strcat(sBuf, "\n-----\n\nAudio: ");
  2229. #ifdef LOOPBACK_SUPPORTED
  2230. if (loopback >= 3)
  2231. strcat(sBuf, "DAHDI and S/T");
  2232. else if (loopback == 2)
  2233. strcat(sBuf, "DAHDI");
  2234. else if (loopback == 1)
  2235. strcat(sBuf, "S/T");
  2236. else
  2237. strcat(sBuf, "not");
  2238. strcat(sBuf, " looped back");
  2239. #else
  2240. strcat(sBuf, "not looped back");
  2241. #endif
  2242. if (milliwatt)
  2243. strcat(sBuf, ", outgoing S/T replaced with mu-law milliwatt tone");
  2244. len += sprintf(buf + len, "%s\n", sBuf);
  2245. if (alarmdebounce)
  2246. sprintf(sBuf, "Alarms: debounced (%dms)", alarmdebounce);
  2247. else
  2248. strcpy(sBuf, "Alarms: not debounced");
  2249. len += sprintf(buf + len, "%s\nT1 timer period %dms\nT3 timer period %dms\n", sBuf, timer_1_ms, timer_3_ms);
  2250. *eof = 1;
  2251. return len;
  2252. }
  2253. #endif /* CREATE_WCB4XXP_PROCFS_ENTRY */
  2254. static int b4xxp_startdefaultspan(struct b4xxp *b4)
  2255. {
  2256. struct dahdi_lineconfig lc = {0,};
  2257. return b4xxp_spanconfig(&b4->spans[0].span, &lc);
  2258. }
  2259. static int __devinit b4xx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2260. {
  2261. int x, ret;
  2262. struct b4xxp *b4;
  2263. struct devtype *dt;
  2264. dt = (struct devtype *)(ent->driver_data);
  2265. dev_info(&pdev->dev, "probe called for b4xx...\n");
  2266. if ((ret = pci_enable_device(pdev)))
  2267. goto err_out_disable_pdev;
  2268. if ((ret = pci_request_regions(pdev, dt->desc))) {
  2269. dev_err(&pdev->dev, "Unable to request regions!\n");
  2270. goto err_out_disable_pdev;
  2271. }
  2272. if (!pdev->irq) { /* we better have an IRQ */
  2273. dev_err(&pdev->dev, "Device has no associated IRQ?\n");
  2274. ret = -EIO;
  2275. goto err_out_release_regions;
  2276. }
  2277. if (!(b4 = kzalloc(sizeof(struct b4xxp), GFP_KERNEL))) {
  2278. dev_err(&pdev->dev, "Couldn't allocate memory for b4xxp structure!\n");
  2279. ret = -ENOMEM;
  2280. goto err_out_release_regions;
  2281. }
  2282. /* card found, enabled and main struct allocated. Fill it out. */
  2283. b4->variety = dt->desc;
  2284. b4->card_type = dt->card_type;
  2285. b4->pdev = pdev;
  2286. b4->dev = &pdev->dev;
  2287. pci_set_drvdata(pdev, b4);
  2288. b4->ioaddr = pci_iomap(pdev, 0, 0);
  2289. b4->addr = pci_iomap(pdev, 1, 0);
  2290. b4->irq = pdev->irq;
  2291. spin_lock_init(&b4->reglock);
  2292. spin_lock_init(&b4->seqlock);
  2293. spin_lock_init(&b4->fifolock);
  2294. x = b4xxp_getreg8(b4, R_CHIP_ID);
  2295. if ((x != 0xc0) && (x != 0x80)) { /* wrong chip? */
  2296. dev_err(&pdev->dev, "Unknown/unsupported controller detected (R_CHIP_ID = 0x%02x)\n", x);
  2297. goto err_out_free_mem;
  2298. }
  2299. /* future proofing */
  2300. b4->chiprev = b4xxp_getreg8(b4, R_CHIP_RV);
  2301. /* check for various board-specific flags and modify init as necessary */
  2302. /*
  2303. if (dt->flags & FLAG_XXX)
  2304. use_flag_somehow();
  2305. */
  2306. /* TODO: determine whether this is a 2, 4 or 8 port card */
  2307. b4->numspans = dt->ports;
  2308. b4->syncspan = -1; /* sync span is unknown */
  2309. if (b4->numspans > MAX_SPANS_PER_CARD) {
  2310. dev_err(b4->dev, "Driver does not know how to handle a %d span card!\n", b4->numspans);
  2311. goto err_out_free_mem;
  2312. }
  2313. dev_info(b4->dev, "Identified %s (controller rev %d) at %p, IRQ %i\n",
  2314. b4->variety, b4->chiprev, b4->ioaddr, b4->irq);
  2315. /* look for the next free card structure */
  2316. for (x=0; x < MAX_B4_CARDS; x++) {
  2317. if (!cards[x])
  2318. break;
  2319. }
  2320. if (x >= MAX_B4_CARDS) {
  2321. dev_err(&pdev->dev, "Attempt to register more than %i cards, aborting!\n", MAX_B4_CARDS);
  2322. goto err_out_free_mem;
  2323. }
  2324. /* update the cards array, make sure the b4xxp struct knows where in the array it is */
  2325. b4->cardno = x;
  2326. cards[x] = b4;
  2327. b4xxp_init_stage1(b4);
  2328. if (request_irq(pdev->irq, b4xxp_interrupt, DAHDI_IRQ_SHARED_DISABLED, "b4xxp", b4)) {
  2329. dev_err(b4->dev, "Unable to request IRQ %d\n", pdev->irq);
  2330. ret = -EIO;
  2331. goto err_out_del_from_card_array;
  2332. }
  2333. /* initialize the tasklet structure */
  2334. /* TODO: perhaps only one tasklet for any number of cards in the system... don't need one per card I don't think. */
  2335. tasklet_init(&b4->b4xxp_tlet, b4xxp_bottom_half, (unsigned long)b4);
  2336. /* interrupt allocated and tasklet initialized, it's now safe to finish initializing the hardware */
  2337. b4xxp_init_stage2(b4);
  2338. hfc_init_all_st(b4);
  2339. /* initialize the DAHDI structures, and let DAHDI know it has some new hardware to play with */
  2340. init_spans(b4);
  2341. for (x=0; x < b4->numspans; x++) {
  2342. if (dahdi_register(&b4->spans[x].span, 0)) {
  2343. dev_err(b4->dev, "Unable to register span %s\n", b4->spans[x].span.name);
  2344. goto err_out_unreg_spans;
  2345. }
  2346. }
  2347. #if 0
  2348. /* Launch cards as appropriate */
  2349. for (;;) {
  2350. /* Find a card to activate */
  2351. f = 0;
  2352. for (x=0; cards[x]; x++) {
  2353. if (cards[x]->order <= highestorder) {
  2354. b4_launch(cards[x]);
  2355. if (cards[x]->order == highestorder)
  2356. f = 1;
  2357. }
  2358. }
  2359. /* If we found at least one, increment the highest order and search again, otherwise stop */
  2360. if (f)
  2361. highestorder++;
  2362. else
  2363. break;
  2364. }
  2365. #else
  2366. dev_info(b4->dev, "Did not do the highestorder stuff\n");
  2367. #endif
  2368. ret = b4xxp_startdefaultspan(b4);
  2369. if (ret)
  2370. goto err_out_unreg_spans;
  2371. ret = 0;
  2372. return ret;
  2373. /* 'x' will have the failing span #. (0-3). We need to unregister everything before it. */
  2374. err_out_unreg_spans:
  2375. while (x) {
  2376. dahdi_unregister(&b4->spans[x].span);
  2377. x--;
  2378. };
  2379. b4xxp_init_stage1(b4); /* full reset, re-init to "no-irq" state */
  2380. free_irq(pdev->irq, b4);
  2381. err_out_del_from_card_array:
  2382. for (x=0; x < MAX_B4_CARDS; x++) {
  2383. if (cards[x] == b4) {
  2384. b4->cardno = -1;
  2385. cards[x] = NULL;
  2386. break;
  2387. }
  2388. }
  2389. if (x >= MAX_B4_CARDS)
  2390. dev_err(&pdev->dev, "b4 struct @ %p should be in cards array but isn't?!\n", b4);
  2391. err_out_free_mem:
  2392. pci_set_drvdata(pdev, NULL);
  2393. pci_iounmap(pdev, b4->ioaddr);
  2394. pci_iounmap(pdev, b4->addr);
  2395. kfree(b4);
  2396. err_out_release_regions:
  2397. pci_release_regions(pdev);
  2398. err_out_disable_pdev:
  2399. pci_disable_device(pdev);
  2400. return ret;
  2401. }
  2402. static void __devexit b4xxp_remove(struct pci_dev *pdev)
  2403. {
  2404. struct b4xxp *b4 = pci_get_drvdata(pdev);
  2405. int i;
  2406. if (b4) {
  2407. b4->shutdown = 1;
  2408. for (i=b4->numspans - 1; i >= 0; i--) {
  2409. dahdi_unregister(&b4->spans[i].span);
  2410. }
  2411. b4xxp_init_stage1(b4);
  2412. remove_sysfs_files(b4);
  2413. free_irq(pdev->irq, b4);
  2414. pci_set_drvdata(pdev, NULL);
  2415. pci_iounmap(pdev, b4->ioaddr);
  2416. pci_iounmap(pdev, b4->addr);
  2417. pci_release_regions(pdev);
  2418. pci_disable_device(pdev);
  2419. b4->ioaddr = b4->addr = NULL;
  2420. tasklet_kill(&b4->b4xxp_tlet);
  2421. kfree(b4);
  2422. }
  2423. dev_info(&pdev->dev, "Driver unloaded.\n");
  2424. return;
  2425. }
  2426. static DEFINE_PCI_DEVICE_TABLE(b4xx_ids) =
  2427. {
  2428. { 0xd161, 0xb410, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wcb4xxp },
  2429. { 0x1397, 0x16b8, 0x1397, 0xb552, 0, 0, (unsigned long)&hfc8s },
  2430. { 0x1397, 0x16b8, 0x1397, 0xb55b, 0, 0, (unsigned long)&hfc8s },
  2431. { 0x1397, 0x08b4, 0x1397, 0xb520, 0, 0, (unsigned long)&hfc4s },
  2432. { 0x1397, 0x08b4, 0x1397, 0xb550, 0, 0, (unsigned long)&hfc4s },
  2433. { 0x1397, 0x08b4, 0x1397, 0xb752, 0, 0, (unsigned long)&hfc4s },
  2434. { 0x1397, 0x08b4, 0x1397, 0xb556, 0, 0, (unsigned long)&hfc2s },
  2435. { 0x1397, 0x08b4, 0x1397, 0xe884, 0, 0, (unsigned long)&hfc2s_OV },
  2436. { 0x1397, 0x08b4, 0x1397, 0xe888, 0, 0, (unsigned long)&hfc4s_OV },
  2437. { 0x1397, 0x16b8, 0x1397, 0xe998, 0, 0, (unsigned long)&hfc8s_OV },
  2438. { 0x1397, 0x08b4, 0x1397, 0xb566, 0, 0, (unsigned long)&hfc2s_BN },
  2439. { 0x1397, 0x08b4, 0x1397, 0xb761, 0, 0, (unsigned long)&hfc2s_BN },
  2440. { 0x1397, 0x08b4, 0x1397, 0xb560, 0, 0, (unsigned long)&hfc4s_BN },
  2441. { 0x1397, 0x08b4, 0x1397, 0xb550, 0, 0, (unsigned long)&hfc4s_BN },
  2442. { 0x1397, 0x16b8, 0x1397, 0xb562, 0, 0, (unsigned long)&hfc8s_BN },
  2443. { 0x1397, 0x16b8, 0x1397, 0xb56b, 0, 0, (unsigned long)&hfc8s_BN },
  2444. { 0x1397, 0x08b4, 0x1397, 0xb540, 0, 0, (unsigned long)&hfc4s_SW },
  2445. { 0x1397, 0x08b4, 0x1397, 0x08b4, 0, 0, (unsigned long)&hfc4s_EV },
  2446. {0, }
  2447. };
  2448. static struct pci_driver b4xx_driver = {
  2449. .name = "wcb4xxp",
  2450. .probe = b4xx_probe,
  2451. .remove = __devexit_p(b4xxp_remove),
  2452. .id_table = b4xx_ids,
  2453. };
  2454. static int __init b4xx_init(void)
  2455. {
  2456. #ifdef CREATE_WCB4XXP_PROCFS_ENTRY
  2457. if (!(myproc = create_proc_read_entry(PROCFS_NAME, 0444, NULL,
  2458. b4xxp_proc_read, cards))) {
  2459. printk(KERN_ERR "%s: ERROR: Could not initialize /proc/%s\n",THIS_MODULE->name, PROCFS_NAME);
  2460. }
  2461. #endif
  2462. if (dahdi_pci_module(&b4xx_driver))
  2463. return -ENODEV;
  2464. return 0;
  2465. }
  2466. static void __exit b4xx_exit(void)
  2467. {
  2468. #ifdef CREATE_WCB4XXP_PROCFS_ENTRY
  2469. remove_proc_entry(PROCFS_NAME, NULL);
  2470. #endif
  2471. pci_unregister_driver(&b4xx_driver);
  2472. }
  2473. module_param(debug, int, S_IRUGO | S_IWUSR);
  2474. module_param(spanfilter, int, S_IRUGO | S_IWUSR);
  2475. #ifdef LOOKBACK_SUPPORTED
  2476. module_param(loopback, int, S_IRUGO | S_IWUSR);
  2477. #endif
  2478. module_param(milliwatt, int, S_IRUGO | S_IWUSR);
  2479. module_param(pedanticpci, int, S_IRUGO);
  2480. module_param(teignorered, int, S_IRUGO | S_IWUSR);
  2481. module_param(alarmdebounce, int, S_IRUGO | S_IWUSR);
  2482. module_param(vpmsupport, int, S_IRUGO);
  2483. module_param(timer_1_ms, int, S_IRUGO | S_IWUSR);
  2484. module_param(timer_3_ms, int, S_IRUGO | S_IWUSR);
  2485. module_param(companding, charp, S_IRUGO);
  2486. MODULE_PARM_DESC(debug, "bitmap: 1=general 2=dtmf 4=regops 8=fops 16=ec 32=st state 64=hdlc 128=alarm");
  2487. MODULE_PARM_DESC(spanfilter, "debug filter for spans. bitmap: 1=port 1, 2=port 2, 4=port 3, 8=port 4");
  2488. #ifdef LOOKBACK_SUPPORTED
  2489. MODULE_PARM_DESC(loopback, "TODO: bitmap: 1=loop back S/T port 2=loop back DAHDI");
  2490. #endif
  2491. MODULE_PARM_DESC(milliwatt, "1=replace outgoing S/T data with mu-law milliwatt");
  2492. MODULE_PARM_DESC(pedanticpci, "1=disable PCI back-to-back transfers and flush all PCI writes immediately");
  2493. MODULE_PARM_DESC(teignorered, "1=ignore (do not inform DAHDI) if a red alarm exists in TE mode");
  2494. MODULE_PARM_DESC(alarmdebounce, "msec to wait before set/clear alarm condition");
  2495. MODULE_PARM_DESC(vpmsupport, "1=enable hardware EC, 0=disable hardware EC");
  2496. MODULE_PARM_DESC(timer_1_ms, "NT: msec to wait for link activation, TE: unused.");
  2497. MODULE_PARM_DESC(timer_3_ms, "TE: msec to wait for link activation, NT: unused.");
  2498. MODULE_PARM_DESC(companding, "Change the companding to \"alaw\" or \"ulaw\""\
  2499. "(alaw by default)");
  2500. MODULE_AUTHOR("Digium Incorporated <support@digium.com>");
  2501. MODULE_DESCRIPTION("B410P & Similars multi-port BRI module driver.");
  2502. MODULE_LICENSE("GPL");
  2503. MODULE_DEVICE_TABLE(pci, b4xx_ids);
  2504. module_init(b4xx_init);
  2505. module_exit(b4xx_exit);