wcaxx-base.c 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545
  1. /*
  2. * A4A,A4B,A8A,A8B TDM FXS/FXO Interface Driver for DAHDI Telephony interface
  3. *
  4. * Copyright (C) 2013 Digium, Inc.
  5. * All rights reserved.
  6. *
  7. */
  8. /*
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2 as published by the
  17. * Free Software Foundation. See the LICENSE file included with
  18. * this program for more details.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/version.h>
  22. #include <linux/kernel.h>
  23. #include <linux/errno.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/sched.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/delay.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/firmware.h>
  32. #include <linux/crc32.h>
  33. #include <stdbool.h>
  34. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
  35. /* Define this if you would like to load the modules in parallel. While this
  36. * can speed up loads when multiple cards handled by this driver are installed,
  37. * it also makes it impossible to abort module loads with ctrl-c */
  38. #undef USE_ASYNC_INIT
  39. #include <linux/async.h>
  40. #else
  41. #undef USE_ASYNC_INIT
  42. #endif
  43. #include <dahdi/kernel.h>
  44. #include <dahdi/wctdm_user.h>
  45. #include "proslic.h"
  46. #include <dahdi/kernel.h>
  47. #include <linux/version.h>
  48. #include <linux/mutex.h>
  49. #include <oct612x.h>
  50. #include "wcxb.h"
  51. #include "wcxb_spi.h"
  52. #include "wcxb_flash.h"
  53. /*!
  54. * \brief Default ringer debounce (in ms)
  55. */
  56. #define DEFAULT_RING_DEBOUNCE 1024
  57. #define POLARITY_DEBOUNCE 64 /* Polarity debounce (in ms) */
  58. #define OHT_TIMER 6000 /* How long after RING to retain OHT */
  59. #define FLAG_EXPRESS (1 << 0)
  60. #define NUM_MODULES 8
  61. #define CMD_WR(addr, val) (((addr<<8)&0xff00) | (val&0xff))
  62. enum battery_state {
  63. BATTERY_UNKNOWN = 0,
  64. BATTERY_DEBOUNCING_PRESENT,
  65. BATTERY_DEBOUNCING_PRESENT_FROM_LOST_ALARM,
  66. BATTERY_DEBOUNCING_PRESENT_ALARM,
  67. BATTERY_PRESENT,
  68. BATTERY_DEBOUNCING_LOST,
  69. BATTERY_DEBOUNCING_LOST_FROM_PRESENT_ALARM,
  70. BATTERY_DEBOUNCING_LOST_ALARM,
  71. BATTERY_LOST,
  72. };
  73. enum ring_detector_state {
  74. RINGOFF = 0,
  75. DEBOUNCING_RINGING_POSITIVE,
  76. DEBOUNCING_RINGING_NEGATIVE,
  77. RINGING,
  78. DEBOUNCING_RINGOFF,
  79. };
  80. enum polarity_state {
  81. UNKNOWN_POLARITY = 0,
  82. POLARITY_DEBOUNCE_POSITIVE,
  83. POLARITY_POSITIVE,
  84. POLARITY_DEBOUNCE_NEGATIVE,
  85. POLARITY_NEGATIVE,
  86. };
  87. struct wcaxx_chan {
  88. struct dahdi_chan chan;
  89. struct dahdi_echocan_state ec;
  90. int timeslot;
  91. unsigned int hwpreec_enabled:1;
  92. };
  93. struct fxo {
  94. enum ring_detector_state ring_state:4;
  95. enum battery_state battery_state:4;
  96. enum polarity_state polarity_state:4;
  97. u8 ring_polarity_change_count:4;
  98. u8 hook_ring_shadow;
  99. s8 line_voltage_status;
  100. int offhook;
  101. int neonmwi_state;
  102. int neonmwi_last_voltage;
  103. unsigned int neonmwi_debounce;
  104. unsigned int neonmwi_offcounter;
  105. unsigned long display_fxovoltage;
  106. unsigned long ringdebounce_timer;
  107. unsigned long battdebounce_timer;
  108. unsigned long poldebounce_timer;
  109. };
  110. struct fxs {
  111. int idletxhookstate; /* IDLE changing hook state */
  112. /* lasttxhook reflects the last value written to the proslic's reg 64
  113. * (LINEFEED_CONTROL) in bits 0-2. Bit 4 indicates if the last write is
  114. * pending i.e. it is in process of being written to the register NOTE:
  115. * in order for this value to actually be written to the proslic, the
  116. * appropriate matching value must be written into the sethook variable
  117. * so that it gets queued and handled by the voicebus ISR.
  118. */
  119. u8 lasttxhook;
  120. u8 linefeed_control_shadow;
  121. u8 hook_state_shadow;
  122. u8 oht_active:1;
  123. u8 off_hook:1;
  124. int palarms;
  125. struct dahdi_vmwi_info vmwisetting;
  126. int vmwi_active_messages;
  127. int vmwi_linereverse;
  128. int reversepolarity; /* polarity reversal */
  129. struct {
  130. u8 vals[12];
  131. } calregs;
  132. unsigned long check_alarm;
  133. unsigned long check_proslic;
  134. unsigned long oppending_timeout;
  135. unsigned long ohttimer;
  136. };
  137. #define fxs_lf(fxs, value) _fxs_lf((fxs), SLIC_LF_##value)
  138. static inline bool _fxs_lf(const struct fxs *fxs, const unsigned value)
  139. {
  140. return (fxs->lasttxhook & SLIC_LF_SETMASK) == value;
  141. }
  142. enum module_type {
  143. NONE = 0,
  144. FXS,
  145. FXO,
  146. };
  147. #define MODULE_POLL_TIME_MS 10
  148. struct wcaxx_mod_poll {
  149. struct wcxb_spi_message m;
  150. struct wcxb_spi_transfer t;
  151. struct wcaxx_module *mod;
  152. struct wcaxx *wc;
  153. u8 buffer[6];
  154. u8 master_buffer[6];
  155. };
  156. struct wcaxx_module {
  157. union modtypes {
  158. struct fxo fxo;
  159. struct fxs fxs;
  160. } mod;
  161. u8 card;
  162. u8 subaddr;
  163. enum module_type type;
  164. int sethook; /* pending hook state command */
  165. int dacssrc;
  166. struct wcxb_spi_device *spi;
  167. struct wcaxx_mod_poll *mod_poll;
  168. };
  169. struct _device_desc {
  170. const char *name;
  171. unsigned int ports;
  172. };
  173. static const struct _device_desc device_a8a = { "Wildcard A8A", 8};
  174. static const struct _device_desc device_a8b = { "Wildcard A8B", 8};
  175. static const struct _device_desc device_a4a = { "Wildcard A4A", 4};
  176. static const struct _device_desc device_a4b = { "Wildcard A4B", 4};
  177. struct wcaxx {
  178. const struct _device_desc *desc;
  179. const char *board_name;
  180. unsigned long framecount;
  181. unsigned long module_poll_time;
  182. int mods_per_board;
  183. spinlock_t reglock;
  184. struct wcaxx_module mods[NUM_MODULES];
  185. struct wcxb xb;
  186. struct dahdi_span span;
  187. struct wcaxx_chan *chans[NUM_MODULES];
  188. struct dahdi_echocan_state *ec[NUM_MODULES];
  189. int companding;
  190. struct dahdi_device *ddev;
  191. struct wcxb_spi_master *master;
  192. #define INITIALIZED 0
  193. unsigned long bit_flags;
  194. /* 4 SPI devices that are matched to the chip selects. The 4 port
  195. * modules will share a single SPI device since they use the same chip
  196. * select. */
  197. struct wcxb_spi_device *spi_devices[4];
  198. struct vpm450m *vpm;
  199. struct list_head card_node;
  200. u16 num;
  201. };
  202. static inline bool is_pcie(const struct wcaxx *wc)
  203. {
  204. return (wc->desc == &device_a8b) || (wc->desc == &device_a4b);
  205. }
  206. static inline bool is_four_port(const struct wcaxx *wc)
  207. {
  208. return (4 == wc->desc->ports);
  209. }
  210. #include <linux/slab.h>
  211. #include <linux/vmalloc.h>
  212. #include <linux/string.h>
  213. #include <linux/time.h>
  214. #include <linux/version.h>
  215. #include <linux/firmware.h>
  216. #include "oct6100api/oct6100_api.h"
  217. #define ECHOCAN_NUM_CHANS 8
  218. #define FLAG_DTMF (1 << 0)
  219. #define FLAG_MUTE (1 << 1)
  220. #define FLAG_ECHO (1 << 2)
  221. #define OCT_CHIP_ID 0
  222. #define OCT_MAX_TDM_STREAMS 4
  223. #define OCT_TONEEVENT_BUFFER_SIZE 128
  224. #define SOUT_STREAM 1
  225. #define RIN_STREAM 0
  226. #define SIN_STREAM 2
  227. static int vpmsupport = 1;
  228. static int wcaxx_vpm_init(struct wcaxx *wc);
  229. static void echocan_free(struct dahdi_chan *chan,
  230. struct dahdi_echocan_state *ec);
  231. static const struct dahdi_echocan_features vpm_ec_features = {
  232. .NLP_automatic = 1,
  233. .CED_tx_detect = 1,
  234. .CED_rx_detect = 1,
  235. };
  236. static const struct dahdi_echocan_ops vpm_ec_ops = {
  237. .echocan_free = echocan_free,
  238. };
  239. struct vpm450m {
  240. tPOCT6100_INSTANCE_API pApiInstance;
  241. struct oct612x_context context;
  242. UINT32 aulEchoChanHndl[32];
  243. int chanflags[32];
  244. int ecmode[32];
  245. int numchans;
  246. };
  247. static int wcaxx_oct612x_write(struct oct612x_context *context,
  248. u32 address, u16 value)
  249. {
  250. struct wcaxx *wc = dev_get_drvdata(context->dev);
  251. wcxb_set_echocan_reg(&wc->xb, address, value);
  252. return 0;
  253. }
  254. static int wcaxx_oct612x_read(struct oct612x_context *context, u32 address,
  255. u16 *value)
  256. {
  257. struct wcaxx *wc = dev_get_drvdata(context->dev);
  258. *value = wcxb_get_echocan_reg(&wc->xb, address);
  259. return 0;
  260. }
  261. static int wcaxx_oct612x_write_smear(struct oct612x_context *context,
  262. u32 address, u16 value, size_t count)
  263. {
  264. unsigned int i;
  265. struct wcaxx *wc = dev_get_drvdata(context->dev);
  266. for (i = 0; i < count; ++i)
  267. wcxb_set_echocan_reg(&wc->xb, address + (i << 1), value);
  268. return 0;
  269. }
  270. static int wcaxx_oct612x_write_burst(struct oct612x_context *context,
  271. u32 address, const u16 *buffer,
  272. size_t count)
  273. {
  274. unsigned int i;
  275. struct wcaxx *wc = dev_get_drvdata(context->dev);
  276. for (i = 0; i < count; ++i)
  277. wcxb_set_echocan_reg(&wc->xb, address + (i << 1), buffer[i]);
  278. return 0;
  279. }
  280. static int wcaxx_oct612x_read_burst(struct oct612x_context *context,
  281. u32 address, u16 *buffer, size_t count)
  282. {
  283. unsigned int i;
  284. struct wcaxx *wc = dev_get_drvdata(context->dev);
  285. for (i = 0; i < count; ++i)
  286. buffer[i] = wcxb_get_echocan_reg(&wc->xb, address + (i << 1));
  287. return 0;
  288. }
  289. static const struct oct612x_ops wcaxx_oct612x_ops = {
  290. .write = wcaxx_oct612x_write,
  291. .read = wcaxx_oct612x_read,
  292. .write_smear = wcaxx_oct612x_write_smear,
  293. .write_burst = wcaxx_oct612x_write_burst,
  294. .read_burst = wcaxx_oct612x_read_burst,
  295. };
  296. static void vpm450m_setecmode(struct vpm450m *vpm450m, int channel, int mode)
  297. {
  298. tOCT6100_CHANNEL_MODIFY *modify;
  299. UINT32 ulResult;
  300. if (vpm450m->ecmode[channel] == mode)
  301. return;
  302. modify = kzalloc(sizeof(tOCT6100_CHANNEL_MODIFY), GFP_ATOMIC);
  303. if (!modify) {
  304. pr_notice("Unable to allocate memory for setec!\n");
  305. return;
  306. }
  307. Oct6100ChannelModifyDef(modify);
  308. modify->ulEchoOperationMode = mode;
  309. modify->ulChannelHndl = vpm450m->aulEchoChanHndl[channel];
  310. ulResult = Oct6100ChannelModify(vpm450m->pApiInstance, modify);
  311. if (ulResult != GENERIC_OK) {
  312. pr_notice("Failed to apply echo can changes on channel %d %d %08x!\n",
  313. vpm450m->aulEchoChanHndl[channel], channel, ulResult);
  314. } else {
  315. #ifdef OCTASIC_DEBUG
  316. pr_debug("Echo can on channel %d set to %d\n", channel, mode);
  317. #endif
  318. vpm450m->ecmode[channel] = mode;
  319. }
  320. kfree(modify);
  321. }
  322. static void vpm450m_setec(struct vpm450m *vpm450m, int channel, int eclen)
  323. {
  324. if (eclen) {
  325. vpm450m->chanflags[channel] |= FLAG_ECHO;
  326. vpm450m_setecmode(vpm450m, channel,
  327. cOCT6100_ECHO_OP_MODE_NORMAL);
  328. } else {
  329. vpm450m->chanflags[channel] &= ~FLAG_ECHO;
  330. if (vpm450m->chanflags[channel] & (FLAG_DTMF | FLAG_MUTE)) {
  331. vpm450m_setecmode(vpm450m, channel,
  332. cOCT6100_ECHO_OP_MODE_HT_RESET);
  333. } else {
  334. vpm450m_setecmode(vpm450m, channel,
  335. cOCT6100_ECHO_OP_MODE_POWER_DOWN);
  336. }
  337. }
  338. }
  339. static UINT32 tdmmode_chan_to_slot_map(int mode, int channel)
  340. {
  341. /* Four phases on the tdm bus, skip three of them per channel */
  342. /* Due to a bug in the octasic, we had to move the data onto phase 2 */
  343. return 1+(channel*4);
  344. }
  345. static int echocan_initialize_channel(struct vpm450m *vpm,
  346. int channel, int mode)
  347. {
  348. tOCT6100_CHANNEL_OPEN ChannelOpen;
  349. UINT32 law_to_use = cOCT6100_PCM_U_LAW;
  350. UINT32 tdmslot_setting;
  351. UINT32 ulResult;
  352. if (0 > channel || ECHOCAN_NUM_CHANS <= channel)
  353. return -1;
  354. tdmslot_setting = tdmmode_chan_to_slot_map(mode, channel);
  355. /* Fill Open channel structure with defaults */
  356. Oct6100ChannelOpenDef(&ChannelOpen);
  357. /* Assign the handle memory.*/
  358. ChannelOpen.pulChannelHndl = &vpm->aulEchoChanHndl[channel];
  359. ChannelOpen.ulUserChanId = channel;
  360. /* Enable Tone disabling for Fax and Modems */
  361. ChannelOpen.fEnableToneDisabler = TRUE;
  362. /* Passthrough TDM data by default, no echocan */
  363. ChannelOpen.ulEchoOperationMode = cOCT6100_ECHO_OP_MODE_POWER_DOWN;
  364. /* Configure the TDM settings.*/
  365. /* Input from the framer */
  366. ChannelOpen.TdmConfig.ulSinStream = SIN_STREAM;
  367. ChannelOpen.TdmConfig.ulSinTimeslot = tdmslot_setting;
  368. ChannelOpen.TdmConfig.ulSinPcmLaw = law_to_use;
  369. /* Input from the Host (pre-framer) */
  370. ChannelOpen.TdmConfig.ulRinStream = RIN_STREAM;
  371. ChannelOpen.TdmConfig.ulRinTimeslot = tdmslot_setting;
  372. ChannelOpen.TdmConfig.ulRinPcmLaw = law_to_use;
  373. /* Output to the Host */
  374. ChannelOpen.TdmConfig.ulSoutStream = SOUT_STREAM;
  375. ChannelOpen.TdmConfig.ulSoutTimeslot = tdmslot_setting;
  376. ChannelOpen.TdmConfig.ulSoutPcmLaw = law_to_use;
  377. /* From asterisk after echo-cancellation - goes nowhere */
  378. ChannelOpen.TdmConfig.ulRoutStream = cOCT6100_UNASSIGNED;
  379. ChannelOpen.TdmConfig.ulRoutTimeslot = cOCT6100_UNASSIGNED;
  380. ChannelOpen.TdmConfig.ulRoutPcmLaw = law_to_use;
  381. /* Set the desired VQE features.*/
  382. ChannelOpen.VqeConfig.fEnableNlp = TRUE;
  383. ChannelOpen.VqeConfig.fRinDcOffsetRemoval = TRUE;
  384. ChannelOpen.VqeConfig.fSinDcOffsetRemoval = TRUE;
  385. ChannelOpen.VqeConfig.ulComfortNoiseMode =
  386. cOCT6100_COMFORT_NOISE_NORMAL;
  387. /* Open the channel.*/
  388. ulResult = Oct6100ChannelOpen(vpm->pApiInstance, &ChannelOpen);
  389. return ulResult;
  390. }
  391. static struct vpm450m *init_vpm450m(struct wcaxx *wc, int isalaw,
  392. const struct firmware *firmware)
  393. {
  394. tOCT6100_CHIP_OPEN *ChipOpen;
  395. tOCT6100_GET_INSTANCE_SIZE InstanceSize;
  396. tOCT6100_CHANNEL_OPEN *ChannelOpen;
  397. UINT32 ulResult;
  398. struct vpm450m *vpm450m;
  399. int x, i;
  400. vpm450m = kzalloc(sizeof(struct vpm450m), GFP_KERNEL);
  401. if (!vpm450m) {
  402. dev_info(&wc->xb.pdev->dev, "Unable to allocate vpm450m struct\n");
  403. return NULL;
  404. }
  405. vpm450m->context.dev = &wc->xb.pdev->dev;
  406. vpm450m->context.ops = &wcaxx_oct612x_ops;
  407. ChipOpen = kzalloc(sizeof(tOCT6100_CHIP_OPEN), GFP_KERNEL);
  408. if (!ChipOpen) {
  409. dev_info(&wc->xb.pdev->dev, "Unable to allocate ChipOpen\n");
  410. kfree(vpm450m);
  411. return NULL;
  412. }
  413. ChannelOpen = kzalloc(sizeof(tOCT6100_CHANNEL_OPEN), GFP_KERNEL);
  414. if (!ChannelOpen) {
  415. dev_info(&wc->xb.pdev->dev, "Unable to allocate ChannelOpen\n");
  416. kfree(vpm450m);
  417. kfree(ChipOpen);
  418. return NULL;
  419. }
  420. for (x = 0; x < ARRAY_SIZE(vpm450m->ecmode); x++)
  421. vpm450m->ecmode[x] = -1;
  422. vpm450m->numchans = ECHOCAN_NUM_CHANS;
  423. dev_info(&wc->xb.pdev->dev, "Echo cancellation for %d channels\n",
  424. wc->desc->ports);
  425. Oct6100ChipOpenDef(ChipOpen);
  426. ChipOpen->pProcessContext = &vpm450m->context;
  427. /* Change default parameters as needed */
  428. /* upclk oscillator is at 33.33 Mhz */
  429. ChipOpen->ulUpclkFreq = cOCT6100_UPCLK_FREQ_33_33_MHZ;
  430. /* mclk will be generated by internal PLL at 133 Mhz */
  431. ChipOpen->fEnableMemClkOut = TRUE;
  432. ChipOpen->ulMemClkFreq = cOCT6100_MCLK_FREQ_133_MHZ;
  433. /* User defined Chip ID.*/
  434. ChipOpen->ulUserChipId = OCT_CHIP_ID;
  435. /* Set the maximums that the chip needs to support */
  436. ChipOpen->ulMaxChannels = vpm450m->numchans;
  437. ChipOpen->ulMaxTdmStreams = OCT_MAX_TDM_STREAMS;
  438. /* External Memory Settings */
  439. /* Use DDR memory.*/
  440. ChipOpen->ulMemoryType = cOCT6100_MEM_TYPE_DDR;
  441. ChipOpen->ulNumMemoryChips = 1;
  442. ChipOpen->ulMemoryChipSize = cOCT6100_MEMORY_CHIP_SIZE_32MB;
  443. ChipOpen->pbyImageFile = (PUINT8) firmware->data;
  444. ChipOpen->ulImageSize = firmware->size;
  445. /* Set TDM data stream frequency */
  446. for (i = 0; i < ChipOpen->ulMaxTdmStreams; i++)
  447. ChipOpen->aulTdmStreamFreqs[i] = cOCT6100_TDM_STREAM_FREQ_8MHZ;
  448. /* Configure TDM sampling */
  449. ChipOpen->ulTdmSampling = cOCT6100_TDM_SAMPLE_AT_FALLING_EDGE;
  450. /* Disable to save RAM footprint space */
  451. ChipOpen->fEnableChannelRecording = FALSE;
  452. /* In this example we will maintain the API using polling so interrupts
  453. * must be disabled */
  454. ChipOpen->InterruptConfig.ulErrorH100Config =
  455. cOCT6100_INTERRUPT_DISABLE;
  456. ChipOpen->InterruptConfig.ulErrorMemoryConfig =
  457. cOCT6100_INTERRUPT_DISABLE;
  458. ChipOpen->InterruptConfig.ulFatalGeneralConfig =
  459. cOCT6100_INTERRUPT_DISABLE;
  460. ChipOpen->InterruptConfig.ulFatalMemoryConfig =
  461. cOCT6100_INTERRUPT_DISABLE;
  462. ChipOpen->ulSoftToneEventsBufSize = OCT_TONEEVENT_BUFFER_SIZE;
  463. /* Inserting default values into tOCT6100_GET_INSTANCE_SIZE structure
  464. * parameters. */
  465. Oct6100GetInstanceSizeDef(&InstanceSize);
  466. /* Get the size of the OCT6100 instance structure. */
  467. ulResult = Oct6100GetInstanceSize(ChipOpen, &InstanceSize);
  468. if (ulResult != cOCT6100_ERR_OK) {
  469. dev_info(&wc->xb.pdev->dev, "Unable to get instance size: %x\n",
  470. ulResult);
  471. return NULL;
  472. }
  473. vpm450m->pApiInstance = vmalloc(InstanceSize.ulApiInstanceSize);
  474. if (!vpm450m->pApiInstance) {
  475. dev_info(&wc->xb.pdev->dev,
  476. "Out of memory (can't allocate %d bytes)!\n",
  477. InstanceSize.ulApiInstanceSize);
  478. return NULL;
  479. }
  480. /* Perform the actual configuration of the chip. */
  481. wcxb_enable_echocan_dram(&wc->xb);
  482. ulResult = Oct6100ChipOpen(vpm450m->pApiInstance, ChipOpen);
  483. if (ulResult != cOCT6100_ERR_OK) {
  484. dev_info(&wc->xb.pdev->dev, "Unable to Oct6100ChipOpen: %x\n",
  485. ulResult);
  486. return NULL;
  487. }
  488. /* OCT6100 is now booted and channels can be opened */
  489. /* Open all channels */
  490. for (i = 0; i < ECHOCAN_NUM_CHANS; i++) {
  491. ulResult = echocan_initialize_channel(vpm450m, i, isalaw);
  492. if (0 != ulResult) {
  493. dev_info(&wc->xb.pdev->dev,
  494. "Unable to echocan_initialize_channel: %x\n",
  495. ulResult);
  496. return NULL;
  497. }
  498. }
  499. if (vpmsupport)
  500. wcxb_enable_echocan(&wc->xb);
  501. else
  502. wcxb_disable_echocan(&wc->xb);
  503. kfree(ChipOpen);
  504. kfree(ChannelOpen);
  505. return vpm450m;
  506. }
  507. static void release_vpm450m(struct vpm450m *vpm450m)
  508. {
  509. UINT32 ulResult;
  510. tOCT6100_CHIP_CLOSE ChipClose;
  511. Oct6100ChipCloseDef(&ChipClose);
  512. ulResult = Oct6100ChipClose(vpm450m->pApiInstance, &ChipClose);
  513. if (ulResult != cOCT6100_ERR_OK)
  514. pr_notice("Failed to close chip, code %08x!\n", ulResult);
  515. vfree(vpm450m->pApiInstance);
  516. kfree(vpm450m);
  517. }
  518. static const char *wcaxx_echocan_name(const struct dahdi_chan *chan)
  519. {
  520. struct wcaxx *wc = chan->pvt;
  521. if (wc->vpm)
  522. return "VPMOCT032";
  523. else
  524. return NULL;
  525. }
  526. static int wcaxx_echocan_create(struct dahdi_chan *chan,
  527. struct dahdi_echocanparams *ecp,
  528. struct dahdi_echocanparam *p,
  529. struct dahdi_echocan_state **ec)
  530. {
  531. struct wcaxx *wc = chan->pvt;
  532. int channel;
  533. const struct dahdi_echocan_ops *ops;
  534. const struct dahdi_echocan_features *features;
  535. if (!vpmsupport || !wc->vpm)
  536. return -ENODEV;
  537. ops = &vpm_ec_ops;
  538. features = &vpm_ec_features;
  539. if (ecp->param_count > 0) {
  540. dev_warn(&wc->xb.pdev->dev,
  541. "%s echo canceller does not support parameters; failing request\n",
  542. chan->ec_factory->get_name(chan));
  543. return -EINVAL;
  544. }
  545. *ec = wc->ec[chan->chanpos - 1];
  546. (*ec)->ops = ops;
  547. (*ec)->features = *features;
  548. channel = chan->chanpos-1;
  549. if (wc->vpm)
  550. vpm450m_setec(wc->vpm, channel, ecp->tap_length);
  551. return 0;
  552. }
  553. static void echocan_free(struct dahdi_chan *chan,
  554. struct dahdi_echocan_state *ec)
  555. {
  556. struct wcaxx *wc = chan->pvt;
  557. int channel;
  558. memset(ec, 0, sizeof(*ec));
  559. channel = chan->chanpos - 1;
  560. if (wc->vpm)
  561. vpm450m_setec(wc->vpm, channel, 0);
  562. }
  563. static int wcaxx_vpm_init(struct wcaxx *wc)
  564. {
  565. int companding = 0;
  566. struct firmware embedded_firmware;
  567. const struct firmware *firmware = &embedded_firmware;
  568. #if !defined(HOTPLUG_FIRMWARE)
  569. extern void _binary_dahdi_fw_oct6114_032_bin_size;
  570. extern u8 _binary_dahdi_fw_oct6114_032_bin_start[];
  571. #else
  572. static const char oct032_firmware[] = "dahdi-fw-oct6114-032.bin";
  573. #endif
  574. int res;
  575. if (!vpmsupport) {
  576. dev_info(&wc->xb.pdev->dev, "VPM: Support Disabled\n");
  577. return -1;
  578. }
  579. wcxb_reset_echocan(&wc->xb);
  580. if (!wcxb_is_echocan_present(&wc->xb)) {
  581. dev_info(&wc->xb.pdev->dev, "VPM not present.\n");
  582. return -1;
  583. }
  584. #if defined(HOTPLUG_FIRMWARE)
  585. res = request_firmware(&firmware, oct032_firmware, &wc->xb.pdev->dev);
  586. if ((0 != res) || !firmware) {
  587. dev_notice(&wc->xb.pdev->dev,
  588. "VPM450: firmware %s not available from userspace\n",
  589. oct032_firmware);
  590. return -1;
  591. }
  592. #else
  593. embedded_firmware.data = _binary_dahdi_fw_oct6114_032_bin_start;
  594. /* Yes... this is weird. objcopy gives us a symbol containing
  595. the size of the firmware, not a pointer a variable containing
  596. the size. The only way we can get the value of the symbol
  597. is to take its address, so we define it as a pointer and
  598. then cast that value to the proper type.
  599. */
  600. embedded_firmware.size = (size_t)&_binary_dahdi_fw_oct6114_032_bin_size;
  601. #endif
  602. wc->vpm = init_vpm450m(wc, companding, firmware);
  603. if (!wc->vpm) {
  604. dev_notice(&wc->xb.pdev->dev, "VPM450: Failed to initialize\n");
  605. if (firmware != &embedded_firmware)
  606. release_firmware(firmware);
  607. return -EIO;
  608. }
  609. if (firmware != &embedded_firmware)
  610. release_firmware(firmware);
  611. dev_info(&wc->xb.pdev->dev,
  612. "VPM450: Present and operational servicing %d span\n", 1);
  613. return 0;
  614. }
  615. static inline bool is_initialized(struct wcaxx *wc)
  616. {
  617. return (test_bit(INITIALIZED, &wc->bit_flags) > 0);
  618. }
  619. static inline struct wcxb_spi_device *
  620. _get_spi_device_for_8_port(struct wcaxx *wc, unsigned int port, bool altcs)
  621. {
  622. switch (port) {
  623. case 0:
  624. return wc->spi_devices[0];
  625. case 1:
  626. return (altcs) ? wc->spi_devices[0] : wc->spi_devices[1];
  627. case 2:
  628. WARN_ON(!altcs);
  629. return wc->spi_devices[0];
  630. case 3:
  631. WARN_ON(!altcs);
  632. return wc->spi_devices[0];
  633. case 4:
  634. return wc->spi_devices[2];
  635. case 5:
  636. return (altcs) ? wc->spi_devices[2] : wc->spi_devices[3];
  637. case 6:
  638. WARN_ON(!altcs);
  639. return wc->spi_devices[2];
  640. case 7:
  641. WARN_ON(!altcs);
  642. return wc->spi_devices[2];
  643. default:
  644. WARN_ON(1);
  645. return wc->spi_devices[0];
  646. }
  647. }
  648. static inline struct wcxb_spi_device *
  649. _get_spi_device_for_4_port(struct wcaxx *wc, unsigned int port)
  650. {
  651. if (port > 3) {
  652. WARN_ON(1);
  653. return wc->spi_devices[0];
  654. } else {
  655. return wc->spi_devices[port];
  656. }
  657. }
  658. static inline struct wcxb_spi_device *
  659. get_spi_device_for_port(struct wcaxx *wc, unsigned int port, bool altcs)
  660. {
  661. if (is_four_port(wc))
  662. return _get_spi_device_for_4_port(wc, port);
  663. else
  664. return _get_spi_device_for_8_port(wc, port, altcs);
  665. }
  666. static u8 wcaxx_getreg(struct wcaxx *wc,
  667. struct wcaxx_module *const mod, int addr);
  668. static void wcaxx_setreg(struct wcaxx *wc, struct wcaxx_module *const mod,
  669. int addr, int val);
  670. static DEFINE_MUTEX(card_list_lock);
  671. static LIST_HEAD(card_list);
  672. #include "adt_lec.h"
  673. /*
  674. Experimental max loop current limit for the proslic
  675. Loop current limit is from 20 mA to 41 mA in steps of 3
  676. (according to datasheet)
  677. So set the value below to:
  678. 0x00 : 20mA (default)
  679. 0x01 : 23mA
  680. 0x02 : 26mA
  681. 0x03 : 29mA
  682. 0x04 : 32mA
  683. 0x05 : 35mA
  684. 0x06 : 37mA
  685. 0x07 : 41mA
  686. */
  687. static int loopcurrent = 20;
  688. /* Following define is a logical exclusive OR to determine if the polarity of
  689. * an fxs line is to be reversed. The items taken into account are:
  690. * overall polarity reversal for the module,
  691. * polarity reversal for the port,
  692. * and the state of the line reversal MWI indicator
  693. */
  694. #define POLARITY_XOR(fxs) \
  695. ((reversepolarity != 0) ^ ((fxs)->reversepolarity != 0) ^ \
  696. ((fxs)->vmwi_linereverse != 0))
  697. static int reversepolarity;
  698. static alpha indirect_regs[] = {
  699. {0, 255, "DTMF_ROW_0_PEAK", 0x55C2},
  700. {1, 255, "DTMF_ROW_1_PEAK", 0x51E6},
  701. {2, 255, "DTMF_ROW2_PEAK", 0x4B85},
  702. {3, 255, "DTMF_ROW3_PEAK", 0x4937},
  703. {4, 255, "DTMF_COL1_PEAK", 0x3333},
  704. {5, 255, "DTMF_FWD_TWIST", 0x0202},
  705. {6, 255, "DTMF_RVS_TWIST", 0x0202},
  706. {7, 255, "DTMF_ROW_RATIO_TRES", 0x0198},
  707. {8, 255, "DTMF_COL_RATIO_TRES", 0x0198},
  708. {9, 255, "DTMF_ROW_2ND_ARM", 0x0611},
  709. {10, 255, "DTMF_COL_2ND_ARM", 0x0202},
  710. {11, 255, "DTMF_PWR_MIN_TRES", 0x00E5},
  711. {12, 255, "DTMF_OT_LIM_TRES", 0x0A1C},
  712. {13, 0, "OSC1_COEF", 0x7B30},
  713. {14, 1, "OSC1X", 0x0063},
  714. {15, 2, "OSC1Y", 0x0000},
  715. {16, 3, "OSC2_COEF", 0x7870},
  716. {17, 4, "OSC2X", 0x007D},
  717. {18, 5, "OSC2Y", 0x0000},
  718. {19, 6, "RING_V_OFF", 0x0000},
  719. {20, 7, "RING_OSC", 0x7EF0},
  720. {21, 8, "RING_X", 0x0160},
  721. {22, 9, "RING_Y", 0x0000},
  722. {23, 255, "PULSE_ENVEL", 0x2000},
  723. {24, 255, "PULSE_X", 0x2000},
  724. {25, 255, "PULSE_Y", 0x0000},
  725. {26, 13, "RECV_DIGITAL_GAIN", 0x2000}, /* playback volume set lower */
  726. {27, 14, "XMIT_DIGITAL_GAIN", 0x4000},
  727. {28, 15, "LOOP_CLOSE_TRES", 0x1000},
  728. {29, 16, "RING_TRIP_TRES", 0x3600},
  729. {30, 17, "COMMON_MIN_TRES", 0x1000},
  730. {31, 18, "COMMON_MAX_TRES", 0x0200},
  731. {32, 19, "PWR_ALARM_Q1Q2", 0x07C0},
  732. {33, 20, "PWR_ALARM_Q3Q4", 0x4C00 /* 0x2600 */},
  733. {34, 21, "PWR_ALARM_Q5Q6", 0x1B80},
  734. {35, 22, "LOOP_CLOSURE_FILTER", 0x8000},
  735. {36, 23, "RING_TRIP_FILTER", 0x0320},
  736. {37, 24, "TERM_LP_POLE_Q1Q2", 0x008C},
  737. {38, 25, "TERM_LP_POLE_Q3Q4", 0x0100},
  738. {39, 26, "TERM_LP_POLE_Q5Q6", 0x0010},
  739. {40, 27, "CM_BIAS_RINGING", 0x0C00},
  740. {41, 64, "DCDC_MIN_V", 0x0C00},
  741. {42, 255, "DCDC_XTRA", 0x1000},
  742. {43, 66, "LOOP_CLOSE_TRES_LOW", 0x1000},
  743. };
  744. /* Undefine to enable Power alarm / Transistor debug -- note: do not
  745. enable for normal operation! */
  746. /* #define PAQ_DEBUG */
  747. #define DEBUG_CARD (1 << 0)
  748. #define DEBUG_ECHOCAN (1 << 1)
  749. #include "fxo_modes.h"
  750. static inline struct dahdi_chan *
  751. get_dahdi_chan(const struct wcaxx *wc, struct wcaxx_module *const mod)
  752. {
  753. return wc->span.chans[mod->card];
  754. }
  755. static inline void mod_hooksig(struct wcaxx *wc,
  756. struct wcaxx_module *mod,
  757. enum dahdi_rxsig rxsig)
  758. {
  759. dahdi_hooksig(get_dahdi_chan(wc, mod), rxsig);
  760. }
  761. static void wcaxx_release(struct wcaxx *wc);
  762. static int fxovoltage;
  763. static unsigned int battdebounce;
  764. static unsigned int battalarm;
  765. static unsigned int battthresh;
  766. static int debug;
  767. static int int_mode;
  768. #ifdef DEBUG
  769. static int robust;
  770. static int digitalloopback;
  771. #endif
  772. static int lowpower;
  773. static int boostringer;
  774. static int fastringer;
  775. static int _opermode;
  776. static char *opermode = "FCC";
  777. static int fxshonormode;
  778. static int alawoverride;
  779. static char *companding = "auto";
  780. static int fastpickup = -1; /* -1 auto, 0 no, 1 yes */
  781. static int fxotxgain;
  782. static int fxorxgain;
  783. static int fxstxgain;
  784. static int fxsrxgain;
  785. static int nativebridge;
  786. static int ringdebounce = DEFAULT_RING_DEBOUNCE;
  787. static int latency = WCXB_DEFAULT_LATENCY;
  788. static unsigned int max_latency = WCXB_DEFAULT_MAXLATENCY;
  789. static int forceload;
  790. #define MS_PER_HOOKCHECK (1)
  791. #define NEONMWI_ON_DEBOUNCE (100/MS_PER_HOOKCHECK)
  792. static int neonmwi_monitor;
  793. static int neonmwi_level = 75; /* neon mwi trip voltage */
  794. static int neonmwi_envelope = 10;
  795. /* Time in milliseconds the monitor is checked before saying no message is
  796. * waiting */
  797. static int neonmwi_offlimit = 16000;
  798. static int neonmwi_offlimit_cycles;
  799. static int wcaxx_init_proslic(struct wcaxx *wc,
  800. struct wcaxx_module *const mod, int fast,
  801. int manual, int sane);
  802. struct wcaxx_setreg_memory {
  803. struct wcxb_spi_message m;
  804. struct wcxb_spi_transfer t;
  805. u8 buffer[3];
  806. };
  807. /**
  808. * wcxb_spi_complete_setreg - Cleanup after a SPI write.
  809. *
  810. * We don't care about the results of setreg. Just go ahead and free up the
  811. * messages.
  812. *
  813. */
  814. static void wcaxx_complete_setreg(void *arg)
  815. {
  816. struct wcaxx_setreg_memory *setreg = arg;
  817. kfree(setreg);
  818. }
  819. static void wcaxx_setreg(struct wcaxx *wc, struct wcaxx_module *mod,
  820. int addr, int val)
  821. {
  822. struct wcaxx_setreg_memory *setreg = kzalloc(sizeof(*setreg),
  823. GFP_ATOMIC);
  824. struct wcxb_spi_message *const m = &setreg->m;
  825. struct wcxb_spi_transfer *const t = &setreg->t;
  826. if (!setreg) {
  827. WARN_ON_ONCE(!setreg);
  828. return;
  829. }
  830. wcxb_spi_message_init(m);
  831. t->tx_buf = setreg->buffer;
  832. wcxb_spi_message_add_tail(t, m);
  833. if (FXO == mod->type) {
  834. static const int ADDRS[4] = {0x00, 0x08, 0x04, 0x0c};
  835. setreg->buffer[0] = 0x20 | ADDRS[mod->subaddr];
  836. } else {
  837. setreg->buffer[0] = 1 << mod->subaddr;
  838. }
  839. setreg->buffer[1] = (addr) & 0x7f;
  840. setreg->buffer[2] = val;
  841. t->len = 3;
  842. m->complete = &wcaxx_complete_setreg;
  843. m->arg = setreg;
  844. wcxb_spi_async(mod->spi, m);
  845. }
  846. /**
  847. * wcaxx_fsxinit - Initilize all SPI devices to 3 byte mode.
  848. *
  849. * All the modules on the card need to be initialized to 3 byte mode in order to
  850. * talk to the daisy-chained SLIC / DAA on the quad modules.
  851. *
  852. */
  853. static void wcaxx_fxsinit(struct wcxb_spi_device *const spi)
  854. {
  855. int res;
  856. u8 data_byte[2] = {0, 0x80};
  857. struct wcxb_spi_transfer t;
  858. struct wcxb_spi_message m;
  859. memset(&t, 0, sizeof(t));
  860. wcxb_spi_message_init(&m);
  861. t.tx_buf = data_byte;
  862. t.len = sizeof(data_byte);
  863. wcxb_spi_message_add_tail(&t, &m);
  864. res = wcxb_spi_sync(spi, &m);
  865. WARN_ON_ONCE(0 != res);
  866. return;
  867. }
  868. static u8
  869. wcaxx_getreg(struct wcaxx *wc, struct wcaxx_module *const mod, int addr)
  870. {
  871. int res;
  872. u8 buffer[3];
  873. struct wcxb_spi_message m;
  874. struct wcxb_spi_transfer t;
  875. memset(&t, 0, sizeof(t));
  876. wcxb_spi_message_init(&m);
  877. t.tx_buf = t.rx_buf = buffer;
  878. t.len = sizeof(buffer);
  879. wcxb_spi_message_add_tail(&t, &m);
  880. if (FXO == mod->type) {
  881. static const int ADDRS[4] = {0x00, 0x08, 0x04, 0x0c};
  882. buffer[0] = 0x60 | ADDRS[mod->subaddr];
  883. buffer[1] = addr & 0x7f;
  884. buffer[2] = 0;
  885. } else {
  886. buffer[0] = 1 << mod->subaddr;
  887. buffer[1] = (addr | 0x80) & 0xff;
  888. buffer[2] = 0;
  889. }
  890. res = wcxb_spi_sync(mod->spi, &m);
  891. WARN_ON_ONCE(0 != res);
  892. return buffer[2];
  893. }
  894. static int wcaxx_getregs(struct wcaxx *wc, struct wcaxx_module *const mod,
  895. int *const addresses, const size_t count)
  896. {
  897. int x;
  898. for (x = 0; x < count; ++x)
  899. addresses[x] = wcaxx_getreg(wc, mod, addresses[x]);
  900. return 0;
  901. }
  902. static int wait_access(struct wcaxx *wc, struct wcaxx_module *const mod)
  903. {
  904. unsigned char data = 0;
  905. int count = 0;
  906. #define MAX 10 /* attempts */
  907. /* Wait for indirect access */
  908. while (count++ < MAX) {
  909. data = wcaxx_getreg(wc, mod, I_STATUS);
  910. if (!data)
  911. return 0;
  912. }
  913. if (count > (MAX-1)) {
  914. dev_notice(&wc->xb.pdev->dev,
  915. " ##### Loop error (%02x) #####\n", data);
  916. }
  917. return 0;
  918. }
  919. static unsigned char translate_3215(unsigned char address)
  920. {
  921. int x;
  922. for (x = 0; x < ARRAY_SIZE(indirect_regs); x++) {
  923. if (indirect_regs[x].address == address) {
  924. address = indirect_regs[x].altaddr;
  925. break;
  926. }
  927. }
  928. return address;
  929. }
  930. static int wcaxx_proslic_setreg_indirect(struct wcaxx *wc,
  931. struct wcaxx_module *const mod,
  932. unsigned char address,
  933. unsigned short data)
  934. {
  935. int res = -1;
  936. address = translate_3215(address);
  937. if (address == 255)
  938. return 0;
  939. if (!wait_access(wc, mod)) {
  940. wcaxx_setreg(wc, mod, IDA_LO, (u8)(data & 0xFF));
  941. wcaxx_setreg(wc, mod, IDA_HI, (u8)((data & 0xFF00)>>8));
  942. wcaxx_setreg(wc, mod, IAA, address);
  943. res = 0;
  944. };
  945. return res;
  946. }
  947. static int wcaxx_proslic_getreg_indirect(struct wcaxx *wc,
  948. struct wcaxx_module *const mod,
  949. unsigned char address)
  950. {
  951. int res = -1;
  952. char *p = NULL;
  953. address = translate_3215(address);
  954. if (address == 255)
  955. return 0;
  956. if (!wait_access(wc, mod)) {
  957. wcaxx_setreg(wc, mod, IAA, address);
  958. if (!wait_access(wc, mod)) {
  959. int addresses[2] = {IDA_LO, IDA_HI};
  960. wcaxx_getregs(wc, mod, addresses,
  961. ARRAY_SIZE(addresses));
  962. res = addresses[0] | (addresses[1] << 8);
  963. } else
  964. p = "Failed to wait inside\n";
  965. } else
  966. p = "failed to wait\n";
  967. if (p)
  968. dev_notice(&wc->xb.pdev->dev, "%s", p);
  969. return res;
  970. }
  971. static int
  972. wcaxx_proslic_init_indirect_regs(struct wcaxx *wc, struct wcaxx_module *mod)
  973. {
  974. unsigned char i;
  975. for (i = 0; i < ARRAY_SIZE(indirect_regs); i++) {
  976. if (wcaxx_proslic_setreg_indirect(wc, mod,
  977. indirect_regs[i].address,
  978. indirect_regs[i].initial))
  979. return -1;
  980. }
  981. return 0;
  982. }
  983. static int wcaxx_proslic_verify_indirect_regs(struct wcaxx *wc,
  984. struct wcaxx_module *mod)
  985. {
  986. int passed = 1;
  987. unsigned short i, initial;
  988. int j;
  989. for (i = 0; i < ARRAY_SIZE(indirect_regs); i++) {
  990. j = wcaxx_proslic_getreg_indirect(wc, mod,
  991. (u8)indirect_regs[i].address);
  992. if (j < 0) {
  993. dev_notice(&wc->xb.pdev->dev,
  994. "Failed to read indirect register %d\n", i);
  995. return -1;
  996. }
  997. initial = indirect_regs[i].initial;
  998. if ((j != initial) && (indirect_regs[i].altaddr != 255)) {
  999. dev_notice(&wc->xb.pdev->dev,
  1000. "!!!!!!! %s iREG %X = %X should be %X\n",
  1001. indirect_regs[i].name,
  1002. indirect_regs[i].address, j, initial);
  1003. passed = 0;
  1004. }
  1005. }
  1006. if (passed) {
  1007. if (debug & DEBUG_CARD) {
  1008. dev_info(&wc->xb.pdev->dev,
  1009. "Init Indirect Registers completed successfully.\n");
  1010. }
  1011. } else {
  1012. dev_notice(&wc->xb.pdev->dev,
  1013. " !!!!! Init Indirect Registers UNSUCCESSFULLY.\n");
  1014. return -1;
  1015. }
  1016. return 0;
  1017. }
  1018. /**
  1019. * wcaxx_proslic_check_oppending -
  1020. *
  1021. * Ensures that a write to the line feed register on the SLIC has been
  1022. * processed. If it hasn't after the timeout value, then it will resend the
  1023. * command and wait for another timeout period.
  1024. *
  1025. */
  1026. static void wcaxx_proslic_check_oppending(struct wcaxx *wc,
  1027. struct wcaxx_module *const mod)
  1028. {
  1029. struct fxs *const fxs = &mod->mod.fxs;
  1030. unsigned long flags;
  1031. if (!(fxs->lasttxhook & SLIC_LF_OPPENDING))
  1032. return;
  1033. /* Monitor the Pending LF state change, for the next 100ms */
  1034. spin_lock_irqsave(&wc->reglock, flags);
  1035. if (!(fxs->lasttxhook & SLIC_LF_OPPENDING)) {
  1036. spin_unlock_irqrestore(&wc->reglock, flags);
  1037. return;
  1038. }
  1039. if ((fxs->linefeed_control_shadow & SLIC_LF_SETMASK) ==
  1040. (fxs->lasttxhook & SLIC_LF_SETMASK)) {
  1041. fxs->lasttxhook &= SLIC_LF_SETMASK;
  1042. if (debug & DEBUG_CARD) {
  1043. dev_info(&wc->xb.pdev->dev,
  1044. "SLIC_LF OK: card=%d shadow=%02x "
  1045. "lasttxhook=%02x framecount=%ld\n", mod->card,
  1046. fxs->linefeed_control_shadow,
  1047. fxs->lasttxhook, wc->framecount);
  1048. }
  1049. } else if (time_after(wc->framecount, fxs->oppending_timeout)) {
  1050. /* Check again in 100 ms */
  1051. fxs->oppending_timeout = wc->framecount + 100;
  1052. wcaxx_setreg(wc, mod, LINE_STATE, fxs->lasttxhook);
  1053. if (debug & DEBUG_CARD) {
  1054. dev_info(&wc->xb.pdev->dev,
  1055. "SLIC_LF RETRY: card=%d shadow=%02x "
  1056. "lasttxhook=%02x framecount=%ld\n", mod->card,
  1057. fxs->linefeed_control_shadow,
  1058. fxs->lasttxhook, wc->framecount);
  1059. }
  1060. }
  1061. spin_unlock_irqrestore(&wc->reglock, flags);
  1062. }
  1063. /* 256ms interrupt */
  1064. static void wcaxx_proslic_recheck_sanity(struct wcaxx *wc,
  1065. struct wcaxx_module *const mod)
  1066. {
  1067. struct fxs *const fxs = &mod->mod.fxs;
  1068. int res;
  1069. unsigned long flags;
  1070. const unsigned int MAX_ALARMS = 10;
  1071. #ifdef PAQ_DEBUG
  1072. res = mod->isrshadow[1];
  1073. res &= ~0x3;
  1074. if (res) {
  1075. mod->isrshadow[1] = 0;
  1076. fxs->palarms++;
  1077. if (fxs->palarms < MAX_ALARMS) {
  1078. dev_notice(&wc->xb.pdev->dev,
  1079. "Power alarm (%02x) on module %d, resetting!\n",
  1080. res, card + 1);
  1081. mod->sethook = CMD_WR(19, res);
  1082. /* Update shadow register to avoid extra power alarms
  1083. * until next read */
  1084. mod->isrshadow[1] = 0;
  1085. } else {
  1086. if (fxs->palarms == MAX_ALARMS) {
  1087. dev_notice(&wc->xb.pdev->dev,
  1088. "Too many power alarms on card %d, NOT resetting!\n",
  1089. card + 1);
  1090. }
  1091. }
  1092. }
  1093. #else
  1094. spin_lock_irqsave(&wc->reglock, flags);
  1095. /* reg 64 has to be zero at last isr read */
  1096. res = !fxs->linefeed_control_shadow &&
  1097. !(fxs->lasttxhook & SLIC_LF_OPPENDING) && /* not a transition */
  1098. fxs->lasttxhook; /* not an intended zero */
  1099. if (res) {
  1100. fxs->palarms++;
  1101. if (fxs->palarms < MAX_ALARMS) {
  1102. dev_notice(&wc->xb.pdev->dev,
  1103. "Power alarm on module %d, resetting!\n",
  1104. mod->card + 1);
  1105. if (fxs->lasttxhook == SLIC_LF_RINGING) {
  1106. fxs->lasttxhook = POLARITY_XOR(fxs) ?
  1107. SLIC_LF_ACTIVE_REV :
  1108. SLIC_LF_ACTIVE_FWD;
  1109. }
  1110. fxs->lasttxhook |= SLIC_LF_OPPENDING;
  1111. mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
  1112. fxs->oppending_timeout = wc->framecount + 100;
  1113. /* Update shadow register to avoid extra power alarms
  1114. * until next read */
  1115. fxs->linefeed_control_shadow = fxs->lasttxhook;
  1116. } else {
  1117. if (fxs->palarms == MAX_ALARMS) {
  1118. dev_notice(&wc->xb.pdev->dev,
  1119. "Too many power alarms on card %d, "
  1120. "NOT resetting!\n", mod->card + 1);
  1121. }
  1122. }
  1123. }
  1124. spin_unlock_irqrestore(&wc->reglock, flags);
  1125. #endif
  1126. }
  1127. static inline bool is_fxo_ringing(const struct fxo *const fxo)
  1128. {
  1129. return ((fxo->hook_ring_shadow & 0x60) &&
  1130. ((fxo->battery_state == BATTERY_PRESENT) ||
  1131. (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
  1132. }
  1133. static inline bool is_fxo_ringing_positive(const struct fxo *const fxo)
  1134. {
  1135. return (((fxo->hook_ring_shadow & 0x60) == 0x20) &&
  1136. ((fxo->battery_state == BATTERY_PRESENT) ||
  1137. (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
  1138. }
  1139. static inline bool is_fxo_ringing_negative(const struct fxo *const fxo)
  1140. {
  1141. return (((fxo->hook_ring_shadow & 0x60) == 0x40) &&
  1142. ((fxo->battery_state == BATTERY_PRESENT) ||
  1143. (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
  1144. }
  1145. static inline void set_ring(struct fxo *fxo, enum ring_detector_state new)
  1146. {
  1147. fxo->ring_state = new;
  1148. }
  1149. static void wcaxx_fxo_ring_detect(struct wcaxx *wc, struct wcaxx_module *mod)
  1150. {
  1151. struct fxo *const fxo = &mod->mod.fxo;
  1152. static const unsigned int POLARITY_CHANGES_NEEDED = 2;
  1153. /* Look for ring status bits (Ring Detect Signal Negative and Ring
  1154. * Detect Signal Positive) to transition back and forth
  1155. * POLARITY_CHANGES_NEEDED times to indicate that a ring is occurring.
  1156. * Provide some number of samples to allow for the transitions to occur
  1157. * before giving up. NOTE: neon mwi voltages will trigger one of these
  1158. * bits to go active but not to have transitions between the two bits
  1159. * (i.e. no negative to positive or positive to negative traversals) */
  1160. switch (fxo->ring_state) {
  1161. case DEBOUNCING_RINGING_POSITIVE:
  1162. if (is_fxo_ringing_negative(fxo)) {
  1163. if (++fxo->ring_polarity_change_count >
  1164. POLARITY_CHANGES_NEEDED) {
  1165. mod_hooksig(wc, mod, DAHDI_RXSIG_RING);
  1166. set_ring(fxo, RINGING);
  1167. if (debug) {
  1168. dev_info(&wc->xb.pdev->dev,
  1169. "RING on %s!\n",
  1170. get_dahdi_chan(wc, mod)->name);
  1171. }
  1172. } else {
  1173. set_ring(fxo, DEBOUNCING_RINGING_NEGATIVE);
  1174. }
  1175. } else if (time_after(wc->framecount,
  1176. fxo->ringdebounce_timer)) {
  1177. set_ring(fxo, RINGOFF);
  1178. }
  1179. break;
  1180. case DEBOUNCING_RINGING_NEGATIVE:
  1181. if (is_fxo_ringing_positive(fxo)) {
  1182. if (++fxo->ring_polarity_change_count >
  1183. POLARITY_CHANGES_NEEDED) {
  1184. mod_hooksig(wc, mod, DAHDI_RXSIG_RING);
  1185. set_ring(fxo, RINGING);
  1186. if (debug) {
  1187. dev_info(&wc->xb.pdev->dev,
  1188. "RING on %s!\n",
  1189. get_dahdi_chan(wc, mod)->name);
  1190. }
  1191. } else {
  1192. set_ring(fxo, DEBOUNCING_RINGING_POSITIVE);
  1193. }
  1194. } else if (time_after(wc->framecount,
  1195. fxo->ringdebounce_timer)) {
  1196. set_ring(fxo, RINGOFF);
  1197. }
  1198. break;
  1199. case RINGING:
  1200. if (!is_fxo_ringing(fxo)) {
  1201. set_ring(fxo, DEBOUNCING_RINGOFF);
  1202. fxo->ringdebounce_timer =
  1203. wc->framecount + ringdebounce / 8;
  1204. }
  1205. break;
  1206. case DEBOUNCING_RINGOFF:
  1207. if (!is_fxo_ringing(fxo)) {
  1208. if (time_after(wc->framecount,
  1209. fxo->ringdebounce_timer)) {
  1210. if (debug) {
  1211. dev_info(&wc->xb.pdev->dev,
  1212. "NO RING on %s!\n",
  1213. get_dahdi_chan(wc, mod)->name);
  1214. }
  1215. mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
  1216. set_ring(fxo, RINGOFF);
  1217. }
  1218. } else {
  1219. set_ring(fxo, RINGING);
  1220. }
  1221. break;
  1222. case RINGOFF:
  1223. if (is_fxo_ringing(fxo)) {
  1224. /* Look for positive/negative crossings in ring status
  1225. * reg */
  1226. if (is_fxo_ringing_positive(fxo))
  1227. set_ring(fxo, DEBOUNCING_RINGING_POSITIVE);
  1228. else
  1229. set_ring(fxo, DEBOUNCING_RINGING_NEGATIVE);
  1230. fxo->ringdebounce_timer =
  1231. wc->framecount + ringdebounce / 8;
  1232. fxo->ring_polarity_change_count = 0;
  1233. }
  1234. break;
  1235. }
  1236. }
  1237. #define MS_PER_CHECK_HOOK 1
  1238. static void
  1239. wcaxx_check_battery_lost(struct wcaxx *wc, struct wcaxx_module *const mod)
  1240. {
  1241. struct fxo *const fxo = &mod->mod.fxo;
  1242. /* possible existing states:
  1243. battery lost, no debounce timer
  1244. battery lost, debounce timer (going to battery present)
  1245. battery present or unknown, no debounce timer
  1246. battery present or unknown, debounce timer (going to battery lost)
  1247. */
  1248. switch (fxo->battery_state) {
  1249. case BATTERY_DEBOUNCING_PRESENT_ALARM:
  1250. fxo->battery_state = BATTERY_DEBOUNCING_LOST_FROM_PRESENT_ALARM;
  1251. fxo->battdebounce_timer = wc->framecount + battdebounce;
  1252. break;
  1253. case BATTERY_DEBOUNCING_PRESENT:
  1254. fxo->battery_state = BATTERY_LOST;
  1255. break;
  1256. case BATTERY_DEBOUNCING_PRESENT_FROM_LOST_ALARM:
  1257. fxo->battery_state = BATTERY_DEBOUNCING_LOST_ALARM;
  1258. fxo->battdebounce_timer = wc->framecount +
  1259. battalarm - battdebounce;
  1260. break;
  1261. case BATTERY_UNKNOWN:
  1262. mod_hooksig(wc, mod, DAHDI_RXSIG_ONHOOK);
  1263. case BATTERY_PRESENT:
  1264. fxo->battery_state = BATTERY_DEBOUNCING_LOST;
  1265. fxo->battdebounce_timer = wc->framecount + battdebounce;
  1266. break;
  1267. case BATTERY_DEBOUNCING_LOST_FROM_PRESENT_ALARM:
  1268. case BATTERY_DEBOUNCING_LOST: /* Intentional drop through */
  1269. if (time_after(wc->framecount, fxo->battdebounce_timer)) {
  1270. if (debug) {
  1271. dev_info(&wc->xb.pdev->dev,
  1272. "NO BATTERY on %d/%d!\n",
  1273. wc->span.spanno,
  1274. mod->card + 1);
  1275. }
  1276. #ifdef JAPAN
  1277. if (!wc->ohdebounce && wc->offhook) {
  1278. dahdi_hooksig(wc->aspan->chans[card],
  1279. DAHDI_RXSIG_ONHOOK);
  1280. if (debug) {
  1281. dev_info(&wc->vb.pdev->dev,
  1282. "Signalled On Hook\n");
  1283. }
  1284. #ifdef ZERO_BATT_RING
  1285. wc->onhook++;
  1286. #endif
  1287. }
  1288. #else
  1289. mod_hooksig(wc, mod, DAHDI_RXSIG_ONHOOK);
  1290. #endif
  1291. /* set the alarm timer, taking into account that part
  1292. * of its time period has already passed while
  1293. * debouncing occurred */
  1294. fxo->battery_state = BATTERY_DEBOUNCING_LOST_ALARM;
  1295. fxo->battdebounce_timer = wc->framecount +
  1296. battalarm - battdebounce;
  1297. }
  1298. break;
  1299. case BATTERY_DEBOUNCING_LOST_ALARM:
  1300. if (time_after(wc->framecount, fxo->battdebounce_timer)) {
  1301. fxo->battery_state = BATTERY_LOST;
  1302. dahdi_alarm_channel(get_dahdi_chan(wc, mod),
  1303. DAHDI_ALARM_RED);
  1304. }
  1305. break;
  1306. case BATTERY_LOST:
  1307. break;
  1308. }
  1309. }
  1310. static void
  1311. wcaxx_check_battery_present(struct wcaxx *wc, struct wcaxx_module *const mod)
  1312. {
  1313. struct fxo *const fxo = &mod->mod.fxo;
  1314. switch (fxo->battery_state) {
  1315. case BATTERY_DEBOUNCING_PRESENT_FROM_LOST_ALARM:
  1316. case BATTERY_DEBOUNCING_PRESENT: /* intentional drop through */
  1317. if (time_after(wc->framecount, fxo->battdebounce_timer)) {
  1318. if (debug) {
  1319. dev_info(&wc->xb.pdev->dev,
  1320. "BATTERY on %d/%d (%s)!\n",
  1321. wc->span.spanno, mod->card + 1,
  1322. (fxo->line_voltage_status < 0) ?
  1323. "-" : "+");
  1324. }
  1325. #ifdef ZERO_BATT_RING
  1326. if (wc->onhook) {
  1327. wc->onhook = 0;
  1328. dahdi_hooksig(wc->aspan->chans[card],
  1329. DAHDI_RXSIG_OFFHOOK);
  1330. if (debug) {
  1331. dev_info(&wc->vb.pdev->dev,
  1332. "Signalled Off Hook\n");
  1333. }
  1334. }
  1335. #else
  1336. mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
  1337. #endif
  1338. /* set the alarm timer, taking into account that part
  1339. * of its time period has already passed while
  1340. * debouncing occurred */
  1341. fxo->battery_state = BATTERY_DEBOUNCING_PRESENT_ALARM;
  1342. fxo->battdebounce_timer = wc->framecount +
  1343. battalarm - battdebounce;
  1344. }
  1345. break;
  1346. case BATTERY_DEBOUNCING_PRESENT_ALARM:
  1347. if (time_after(wc->framecount, fxo->battdebounce_timer)) {
  1348. fxo->battery_state = BATTERY_PRESENT;
  1349. dahdi_alarm_channel(get_dahdi_chan(wc, mod),
  1350. DAHDI_ALARM_NONE);
  1351. }
  1352. break;
  1353. case BATTERY_PRESENT:
  1354. break;
  1355. case BATTERY_DEBOUNCING_LOST_ALARM:
  1356. fxo->battery_state = BATTERY_DEBOUNCING_PRESENT_FROM_LOST_ALARM;
  1357. fxo->battdebounce_timer = wc->framecount + battdebounce;
  1358. break;
  1359. case BATTERY_DEBOUNCING_LOST_FROM_PRESENT_ALARM:
  1360. fxo->battery_state = BATTERY_DEBOUNCING_PRESENT_ALARM;
  1361. fxo->battdebounce_timer = wc->framecount +
  1362. battalarm - battdebounce;
  1363. break;
  1364. case BATTERY_DEBOUNCING_LOST:
  1365. fxo->battery_state = BATTERY_PRESENT;
  1366. break;
  1367. case BATTERY_UNKNOWN:
  1368. mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
  1369. case BATTERY_LOST: /* intentional drop through */
  1370. fxo->battery_state = BATTERY_DEBOUNCING_PRESENT;
  1371. fxo->battdebounce_timer = wc->framecount + battdebounce;
  1372. break;
  1373. }
  1374. }
  1375. static void
  1376. wcaxx_fxo_stop_debouncing_polarity(struct wcaxx *wc,
  1377. struct wcaxx_module *const mod)
  1378. {
  1379. struct fxo *const fxo = &mod->mod.fxo;
  1380. switch (fxo->polarity_state) {
  1381. case UNKNOWN_POLARITY:
  1382. break;
  1383. case POLARITY_DEBOUNCE_POSITIVE:
  1384. fxo->polarity_state = POLARITY_NEGATIVE;
  1385. break;
  1386. case POLARITY_POSITIVE:
  1387. break;
  1388. case POLARITY_DEBOUNCE_NEGATIVE:
  1389. fxo->polarity_state = POLARITY_POSITIVE;
  1390. break;
  1391. case POLARITY_NEGATIVE:
  1392. break;
  1393. };
  1394. }
  1395. static void
  1396. wcaxx_fxo_check_polarity(struct wcaxx *wc, struct wcaxx_module *const mod,
  1397. const bool positive_polarity)
  1398. {
  1399. struct fxo *const fxo = &mod->mod.fxo;
  1400. switch (fxo->polarity_state) {
  1401. case UNKNOWN_POLARITY:
  1402. fxo->polarity_state = (positive_polarity) ? POLARITY_POSITIVE :
  1403. POLARITY_NEGATIVE;
  1404. break;
  1405. case POLARITY_DEBOUNCE_POSITIVE:
  1406. if (!positive_polarity) {
  1407. fxo->polarity_state = POLARITY_NEGATIVE;
  1408. } else if (time_after(wc->framecount, fxo->poldebounce_timer)) {
  1409. fxo->polarity_state = POLARITY_POSITIVE;
  1410. dahdi_qevent_lock(get_dahdi_chan(wc, mod),
  1411. DAHDI_EVENT_POLARITY);
  1412. if (debug & DEBUG_CARD) {
  1413. dev_info(&wc->xb.pdev->dev,
  1414. "%s: Polarity NEGATIVE -> POSITIVE\n",
  1415. get_dahdi_chan(wc, mod)->name);
  1416. }
  1417. }
  1418. break;
  1419. case POLARITY_POSITIVE:
  1420. if (!positive_polarity) {
  1421. fxo->polarity_state = POLARITY_DEBOUNCE_NEGATIVE;
  1422. fxo->poldebounce_timer = wc->framecount +
  1423. POLARITY_DEBOUNCE;
  1424. }
  1425. break;
  1426. case POLARITY_DEBOUNCE_NEGATIVE:
  1427. if (positive_polarity) {
  1428. fxo->polarity_state = POLARITY_POSITIVE;
  1429. } else if (time_after(wc->framecount, fxo->poldebounce_timer)) {
  1430. dahdi_qevent_lock(get_dahdi_chan(wc, mod),
  1431. DAHDI_EVENT_POLARITY);
  1432. if (debug & DEBUG_CARD) {
  1433. dev_info(&wc->xb.pdev->dev,
  1434. "%s: Polarity POSITIVE -> NEGATIVE\n",
  1435. get_dahdi_chan(wc, mod)->name);
  1436. }
  1437. fxo->polarity_state = POLARITY_NEGATIVE;
  1438. }
  1439. break;
  1440. case POLARITY_NEGATIVE:
  1441. if (positive_polarity) {
  1442. fxo->polarity_state = POLARITY_DEBOUNCE_POSITIVE;
  1443. fxo->poldebounce_timer = wc->framecount +
  1444. POLARITY_DEBOUNCE;
  1445. }
  1446. break;
  1447. };
  1448. }
  1449. static bool is_neon_voltage_present(const struct fxo *fxo, u8 abs_voltage)
  1450. {
  1451. return (fxo->battery_state == BATTERY_PRESENT &&
  1452. abs_voltage > neonmwi_level &&
  1453. (0 == fxo->neonmwi_last_voltage ||
  1454. ((fxo->line_voltage_status >= fxo->neonmwi_last_voltage -
  1455. neonmwi_envelope) &&
  1456. (fxo->line_voltage_status <= fxo->neonmwi_last_voltage +
  1457. neonmwi_envelope)
  1458. )
  1459. )
  1460. );
  1461. }
  1462. static void do_neon_monitor(struct wcaxx *wc,
  1463. struct wcaxx_module *mod, u8 abs_voltage)
  1464. {
  1465. struct fxo *const fxo = &mod->mod.fxo;
  1466. struct dahdi_chan *const chan = get_dahdi_chan(wc, mod);
  1467. /* Look for 4 consecutive voltage readings where the voltage is over the
  1468. * neon limit but does not vary greatly from the last reading */
  1469. if (is_neon_voltage_present(fxo, abs_voltage)) {
  1470. fxo->neonmwi_last_voltage = fxo->line_voltage_status;
  1471. if (NEONMWI_ON_DEBOUNCE == fxo->neonmwi_debounce) {
  1472. fxo->neonmwi_offcounter = neonmwi_offlimit_cycles;
  1473. if (0 == fxo->neonmwi_state) {
  1474. dahdi_qevent_lock(chan,
  1475. DAHDI_EVENT_NEONMWI_ACTIVE);
  1476. fxo->neonmwi_state = 1;
  1477. if (debug) {
  1478. dev_info(&wc->xb.pdev->dev,
  1479. "NEON MWI active for card %d\n",
  1480. mod->card+1);
  1481. }
  1482. }
  1483. fxo->neonmwi_debounce++;
  1484. } else if (NEONMWI_ON_DEBOUNCE > fxo->neonmwi_debounce) {
  1485. fxo->neonmwi_debounce++;
  1486. } else {
  1487. fxo->neonmwi_offcounter = neonmwi_offlimit_cycles;
  1488. }
  1489. } else {
  1490. fxo->neonmwi_debounce = 0;
  1491. fxo->neonmwi_last_voltage = 0;
  1492. }
  1493. /* If no neon mwi pulse for given period of time, indicte no neon mwi
  1494. * state */
  1495. if (fxo->neonmwi_state && 0 < fxo->neonmwi_offcounter) {
  1496. fxo->neonmwi_offcounter--;
  1497. if (0 == fxo->neonmwi_offcounter) {
  1498. dahdi_qevent_lock(get_dahdi_chan(wc, mod),
  1499. DAHDI_EVENT_NEONMWI_INACTIVE);
  1500. fxo->neonmwi_state = 0;
  1501. if (debug) {
  1502. dev_info(&wc->xb.pdev->dev,
  1503. "NEON MWI cleared for card %d\n",
  1504. mod->card+1);
  1505. }
  1506. }
  1507. }
  1508. }
  1509. static void
  1510. wcaxx_voicedaa_check_hook(struct wcaxx *wc, struct wcaxx_module *const mod)
  1511. {
  1512. signed char b;
  1513. u8 abs_voltage;
  1514. struct fxo *const fxo = &mod->mod.fxo;
  1515. /* Try to track issues that plague slot one FXO's */
  1516. b = fxo->hook_ring_shadow & 0x9b;
  1517. if (fxo->offhook) {
  1518. if (b != 0x9)
  1519. wcaxx_setreg(wc, mod, 5, 0x9);
  1520. } else {
  1521. if (b != 0x8)
  1522. wcaxx_setreg(wc, mod, 5, 0x8);
  1523. wcaxx_fxo_ring_detect(wc, mod);
  1524. }
  1525. abs_voltage = abs(fxo->line_voltage_status);
  1526. if (fxovoltage && time_after(wc->framecount, fxo->display_fxovoltage)) {
  1527. /* Every 100 ms */
  1528. fxo->display_fxovoltage = wc->framecount + 100;
  1529. dev_info(&wc->xb.pdev->dev,
  1530. "Port %d: Voltage: %d\n",
  1531. mod->card + 1, fxo->line_voltage_status);
  1532. }
  1533. if (unlikely(DAHDI_RXSIG_INITIAL ==
  1534. get_dahdi_chan(wc, mod)->rxhooksig)) {
  1535. /*
  1536. * dahdi-base will set DAHDI_RXSIG_INITIAL after a
  1537. * DAHDI_STARTUP or DAHDI_CHANCONFIG ioctl so that new events
  1538. * will be queued on the channel with the current received
  1539. * hook state. Channels that use robbed-bit signalling always
  1540. * report the current received state via the dahdi_rbsbits
  1541. * call. Since we only call dahdi_hooksig when we've detected
  1542. * a change to report, let's forget our current state in order
  1543. * to force us to report it again via dahdi_hooksig.
  1544. *
  1545. */
  1546. fxo->battery_state = BATTERY_UNKNOWN;
  1547. }
  1548. if (abs_voltage < battthresh) {
  1549. wcaxx_fxo_stop_debouncing_polarity(wc, mod);
  1550. wcaxx_check_battery_lost(wc, mod);
  1551. } else {
  1552. wcaxx_check_battery_present(wc, mod);
  1553. wcaxx_fxo_check_polarity(wc, mod,
  1554. (fxo->line_voltage_status > 0));
  1555. }
  1556. /* Look for neon mwi pulse */
  1557. if (neonmwi_monitor && !fxo->offhook)
  1558. do_neon_monitor(wc, mod, abs_voltage);
  1559. #undef MS_PER_CHECK_HOOK
  1560. }
  1561. static void
  1562. wcaxx_fxs_hooksig(struct wcaxx *wc, struct wcaxx_module *const mod,
  1563. enum dahdi_txsig txsig)
  1564. {
  1565. int x = 0;
  1566. unsigned long flags;
  1567. struct fxs *const fxs = &mod->mod.fxs;
  1568. spin_lock_irqsave(&wc->reglock, flags);
  1569. switch (txsig) {
  1570. case DAHDI_TXSIG_ONHOOK:
  1571. switch (get_dahdi_chan(wc, mod)->sig) {
  1572. case DAHDI_SIG_FXOGS:
  1573. x = (POLARITY_XOR(fxs)) ?
  1574. SLIC_LF_RING_OPEN :
  1575. SLIC_LF_TIP_OPEN;
  1576. break;
  1577. case DAHDI_SIG_EM:
  1578. case DAHDI_SIG_FXOKS:
  1579. case DAHDI_SIG_FXOLS:
  1580. default:
  1581. x = fxs->idletxhookstate;
  1582. break;
  1583. }
  1584. break;
  1585. case DAHDI_TXSIG_OFFHOOK:
  1586. switch (get_dahdi_chan(wc, mod)->sig) {
  1587. case DAHDI_SIG_EM:
  1588. x = (POLARITY_XOR(fxs)) ?
  1589. SLIC_LF_ACTIVE_FWD :
  1590. SLIC_LF_ACTIVE_REV;
  1591. break;
  1592. default:
  1593. x = fxs->idletxhookstate;
  1594. break;
  1595. }
  1596. break;
  1597. case DAHDI_TXSIG_START:
  1598. x = SLIC_LF_RINGING;
  1599. break;
  1600. case DAHDI_TXSIG_KEWL:
  1601. x = SLIC_LF_OPEN;
  1602. break;
  1603. default:
  1604. spin_unlock_irqrestore(&wc->reglock, flags);
  1605. dev_notice(&wc->xb.pdev->dev,
  1606. "Can't set tx state to %d\n", txsig);
  1607. return;
  1608. }
  1609. if (x != fxs->lasttxhook) {
  1610. fxs->lasttxhook = x | SLIC_LF_OPPENDING;
  1611. mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
  1612. fxs->oppending_timeout = wc->framecount + 100;
  1613. spin_unlock_irqrestore(&wc->reglock, flags);
  1614. if (debug & DEBUG_CARD) {
  1615. dev_info(&wc->xb.pdev->dev,
  1616. "Setting FXS hook state to %d (%02x) framecount=%ld\n",
  1617. txsig, x, wc->framecount);
  1618. }
  1619. } else {
  1620. spin_unlock_irqrestore(&wc->reglock, flags);
  1621. }
  1622. }
  1623. static void
  1624. wcaxx_fxs_off_hook(struct wcaxx *wc, struct wcaxx_module *const mod)
  1625. {
  1626. struct fxs *const fxs = &mod->mod.fxs;
  1627. if (debug & DEBUG_CARD) {
  1628. dev_info(&wc->xb.pdev->dev,
  1629. "fxs_off_hook: Card %d Going off hook\n", mod->card);
  1630. }
  1631. switch (fxs->lasttxhook) {
  1632. case SLIC_LF_RINGING: /* Ringing */
  1633. case SLIC_LF_OHTRAN_FWD: /* Forward On Hook Transfer */
  1634. case SLIC_LF_OHTRAN_REV: /* Reverse On Hook Transfer */
  1635. /* just detected OffHook, during Ringing or OnHookTransfer */
  1636. fxs->idletxhookstate = POLARITY_XOR(fxs) ?
  1637. SLIC_LF_ACTIVE_REV :
  1638. SLIC_LF_ACTIVE_FWD;
  1639. break;
  1640. }
  1641. if ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)
  1642. wcaxx_fxs_hooksig(wc, mod, DAHDI_TXSIG_OFFHOOK);
  1643. dahdi_hooksig(get_dahdi_chan(wc, mod), DAHDI_RXSIG_OFFHOOK);
  1644. #ifdef DEBUG
  1645. if (robust)
  1646. wcaxx_init_proslic(wc, mod, 1, 0, 1);
  1647. #endif
  1648. }
  1649. /**
  1650. * wcaxx_fxs_on_hook - Report on hook to DAHDI.
  1651. * @wc: Board hosting the module.
  1652. * @card: Index of the module / port to place on hook.
  1653. *
  1654. * If we are intentionally dropping battery to signal a forward
  1655. * disconnect we do not want to place the line "On-Hook". In this
  1656. * case, the core of DAHDI will place us on hook when one of the RBS
  1657. * timers expires.
  1658. *
  1659. */
  1660. static void
  1661. wcaxx_fxs_on_hook(struct wcaxx *wc, struct wcaxx_module *const mod)
  1662. {
  1663. if (debug & DEBUG_CARD) {
  1664. dev_info(&wc->xb.pdev->dev,
  1665. "fxs_on_hook: Card %d Going on hook\n", mod->card);
  1666. }
  1667. if ((mod->mod.fxs.lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)
  1668. wcaxx_fxs_hooksig(wc, mod, DAHDI_TXSIG_ONHOOK);
  1669. dahdi_hooksig(get_dahdi_chan(wc, mod), DAHDI_RXSIG_ONHOOK);
  1670. }
  1671. static void
  1672. wcaxx_isr_misc_fxs(struct wcaxx *wc, struct wcaxx_module *const mod)
  1673. {
  1674. struct fxs *const fxs = &mod->mod.fxs;
  1675. unsigned long flags;
  1676. if (time_after(wc->framecount, fxs->check_alarm)) {
  1677. /* Accept an alarm once per 10 seconds */
  1678. fxs->check_alarm = wc->framecount + (1000*10);
  1679. if (fxs->palarms)
  1680. fxs->palarms--;
  1681. }
  1682. if (fxs->off_hook && !(fxs->hook_state_shadow & 1)) {
  1683. wcaxx_fxs_on_hook(wc, mod);
  1684. fxs->off_hook = 0;
  1685. } else if (!fxs->off_hook && (fxs->hook_state_shadow & 1)) {
  1686. wcaxx_fxs_off_hook(wc, mod);
  1687. fxs->off_hook = 1;
  1688. }
  1689. wcaxx_proslic_check_oppending(wc, mod);
  1690. if (time_after(wc->framecount, fxs->check_proslic)) {
  1691. fxs->check_proslic = wc->framecount + 250; /* every 250ms */
  1692. wcaxx_proslic_recheck_sanity(wc, mod);
  1693. }
  1694. if (SLIC_LF_RINGING == fxs->lasttxhook) {
  1695. /* RINGing, prepare for OHT */
  1696. fxs->ohttimer = wc->framecount + OHT_TIMER;
  1697. /* OHT mode when idle */
  1698. fxs->idletxhookstate = POLARITY_XOR(fxs) ? SLIC_LF_OHTRAN_REV :
  1699. SLIC_LF_OHTRAN_FWD;
  1700. } else if (fxs->oht_active) {
  1701. /* check if still OnHook */
  1702. if (!fxs->off_hook) {
  1703. if (time_before(wc->framecount, fxs->ohttimer))
  1704. return;
  1705. /* Switch to active */
  1706. fxs->idletxhookstate = POLARITY_XOR(fxs) ?
  1707. SLIC_LF_ACTIVE_REV : SLIC_LF_ACTIVE_FWD;
  1708. spin_lock_irqsave(&wc->reglock, flags);
  1709. if (SLIC_LF_OHTRAN_FWD == fxs->lasttxhook) {
  1710. /* Apply the change if appropriate */
  1711. fxs->lasttxhook = SLIC_LF_OPPENDING |
  1712. SLIC_LF_ACTIVE_FWD;
  1713. /* Data enqueued here */
  1714. mod->sethook = CMD_WR(LINE_STATE,
  1715. fxs->lasttxhook);
  1716. if (debug & DEBUG_CARD) {
  1717. dev_info(&wc->xb.pdev->dev,
  1718. "Channel %d OnHookTransfer stop\n",
  1719. mod->card);
  1720. }
  1721. } else if (SLIC_LF_OHTRAN_REV == fxs->lasttxhook) {
  1722. /* Apply the change if appropriate */
  1723. fxs->lasttxhook = SLIC_LF_OPPENDING |
  1724. SLIC_LF_ACTIVE_REV;
  1725. /* Data enqueued here */
  1726. mod->sethook = CMD_WR(LINE_STATE,
  1727. fxs->lasttxhook);
  1728. if (debug & DEBUG_CARD) {
  1729. dev_info(&wc->xb.pdev->dev,
  1730. "Channel %d OnHookTransfer stop\n",
  1731. mod->card);
  1732. }
  1733. }
  1734. spin_unlock_irqrestore(&wc->reglock, flags);
  1735. } else {
  1736. fxs->oht_active = 0;
  1737. /* Switch to active */
  1738. fxs->idletxhookstate = POLARITY_XOR(fxs) ?
  1739. SLIC_LF_ACTIVE_REV : SLIC_LF_ACTIVE_FWD;
  1740. if (debug & DEBUG_CARD) {
  1741. dev_info(&wc->xb.pdev->dev,
  1742. "Channel %d OnHookTransfer abort\n",
  1743. mod->card);
  1744. }
  1745. }
  1746. }
  1747. }
  1748. static void wcaxx_handle_receive(struct wcxb *xb, void *_frame)
  1749. {
  1750. int i, j;
  1751. struct wcaxx *wc = container_of(xb, struct wcaxx, xb);
  1752. u8 *const frame = _frame;
  1753. wc->framecount++;
  1754. if (time_after(wc->framecount, wc->module_poll_time)) {
  1755. for (i = 0; i < wc->mods_per_board; i++) {
  1756. struct wcaxx_module *const mod = &wc->mods[i];
  1757. if (mod->mod_poll) {
  1758. wcxb_spi_async(mod->spi, &mod->mod_poll->m);
  1759. mod->mod_poll = NULL;
  1760. }
  1761. }
  1762. wc->module_poll_time = wc->framecount + MODULE_POLL_TIME_MS;
  1763. }
  1764. /* TODO: This protection needs to be thought about. */
  1765. if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &wc->span.flags))
  1766. return;
  1767. for (j = 0; j < DAHDI_CHUNKSIZE; j++) {
  1768. for (i = 0; i < wc->span.channels; i++) {
  1769. wc->chans[i]->chan.readchunk[j] =
  1770. frame[j*WCXB_DMA_CHAN_SIZE+(1+i*4)];
  1771. }
  1772. }
  1773. for (i = 0; i < wc->span.channels; i++) {
  1774. struct dahdi_chan *const c = wc->span.chans[i];
  1775. __dahdi_ec_chunk(c, c->readchunk, c->readchunk, c->writechunk);
  1776. }
  1777. _dahdi_receive(&wc->span);
  1778. return;
  1779. }
  1780. static void wcaxx_handle_transmit(struct wcxb *xb, void *_frame)
  1781. {
  1782. int i, j;
  1783. struct wcaxx *wc = container_of(xb, struct wcaxx, xb);
  1784. u8 *const frame = _frame;
  1785. wcxb_spi_handle_interrupt(wc->master);
  1786. /* TODO: This protection needs to be thought about. */
  1787. if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &wc->span.flags))
  1788. return;
  1789. _dahdi_transmit(&wc->span);
  1790. for (j = 0; j < DAHDI_CHUNKSIZE; j++) {
  1791. for (i = 0; i < wc->span.channels; i++) {
  1792. struct dahdi_chan *c = &wc->chans[i]->chan;
  1793. frame[j*WCXB_DMA_CHAN_SIZE+(1+i*4)] = c->writechunk[j];
  1794. }
  1795. }
  1796. return;
  1797. }
  1798. static int wcaxx_voicedaa_insane(struct wcaxx *wc, struct wcaxx_module *mod)
  1799. {
  1800. int blah;
  1801. blah = wcaxx_getreg(wc, mod, 2);
  1802. if (blah != 0x3)
  1803. return -2;
  1804. blah = wcaxx_getreg(wc, mod, 11);
  1805. if (debug & DEBUG_CARD) {
  1806. dev_info(&wc->xb.pdev->dev,
  1807. "VoiceDAA System: %02x\n", blah & 0xf);
  1808. }
  1809. return 0;
  1810. }
  1811. static int
  1812. wcaxx_proslic_insane(struct wcaxx *wc, struct wcaxx_module *const mod)
  1813. {
  1814. int blah, reg1, insane_report;
  1815. insane_report = 0;
  1816. blah = wcaxx_getreg(wc, mod, 0);
  1817. if (blah != 0xff && (debug & DEBUG_CARD)) {
  1818. dev_info(&wc->xb.pdev->dev,
  1819. "ProSLIC on module %d, product %d, "
  1820. "version %d\n", mod->card, (blah & 0x30) >> 4,
  1821. (blah & 0xf));
  1822. }
  1823. #if 0
  1824. if ((blah & 0x30) >> 4) {
  1825. dev_info(&wc->xb.pdev->dev,
  1826. "ProSLIC on module %d is not a 3210.\n", mod->card);
  1827. return -1;
  1828. }
  1829. #endif
  1830. if (((blah & 0xf) == 0) || ((blah & 0xf) == 0xf)) {
  1831. /* SLIC not loaded */
  1832. return -1;
  1833. }
  1834. /* let's be really sure this is an FXS before we continue */
  1835. reg1 = wcaxx_getreg(wc, mod, 1);
  1836. if ((0x80 != (blah & 0xf0)) || (0x88 != reg1)) {
  1837. if (debug & DEBUG_CARD) {
  1838. dev_info(&wc->xb.pdev->dev,
  1839. "DEBUG: not FXS b/c reg0=%x or "
  1840. "reg1 != 0x88 (%x).\n", blah, reg1);
  1841. }
  1842. return -1;
  1843. }
  1844. blah = wcaxx_getreg(wc, mod, 8);
  1845. if (blah != 0x2) {
  1846. dev_notice(&wc->xb.pdev->dev,
  1847. "ProSLIC on module %d insane (1) %d should be 2\n",
  1848. mod->card, blah);
  1849. return -1;
  1850. } else if (insane_report) {
  1851. dev_notice(&wc->xb.pdev->dev,
  1852. "ProSLIC on module %d Reg 8 Reads %d Expected "
  1853. "is 0x2\n", mod->card, blah);
  1854. }
  1855. blah = wcaxx_getreg(wc, mod, 64);
  1856. if (blah != 0x0) {
  1857. dev_notice(&wc->xb.pdev->dev,
  1858. "ProSLIC on module %d insane (2)\n",
  1859. mod->card);
  1860. return -1;
  1861. } else if (insane_report) {
  1862. dev_notice(&wc->xb.pdev->dev,
  1863. "ProSLIC on module %d Reg 64 Reads %d Expected "
  1864. "is 0x0\n", mod->card, blah);
  1865. }
  1866. blah = wcaxx_getreg(wc, mod, 11);
  1867. if (blah != 0x33) {
  1868. dev_notice(&wc->xb.pdev->dev,
  1869. "ProSLIC on module %d insane (3)\n", mod->card);
  1870. return -1;
  1871. } else if (insane_report) {
  1872. dev_notice(&wc->xb.pdev->dev,
  1873. "ProSLIC on module %d Reg 11 Reads %d "
  1874. "Expected is 0x33\n", mod->card, blah);
  1875. }
  1876. /* Just be sure it's setup right. */
  1877. wcaxx_setreg(wc, mod, 30, 0);
  1878. if (debug & DEBUG_CARD) {
  1879. dev_info(&wc->xb.pdev->dev,
  1880. "ProSLIC on module %d seems sane.\n", mod->card);
  1881. }
  1882. return 0;
  1883. }
  1884. static int
  1885. wcaxx_proslic_powerleak_test(struct wcaxx *wc,
  1886. struct wcaxx_module *const mod)
  1887. {
  1888. unsigned long start;
  1889. unsigned char vbat;
  1890. /* Turn off linefeed */
  1891. wcaxx_setreg(wc, mod, LINE_STATE, 0);
  1892. /* Power down */
  1893. wcaxx_setreg(wc, mod, 14, 0x10);
  1894. start = jiffies;
  1895. /* TODO: Why is this sleep necessary. Without it, the first read
  1896. * comes back with a 0 value. */
  1897. msleep(20);
  1898. while ((vbat = wcaxx_getreg(wc, mod, 82)) > 0x6) {
  1899. if (time_after(jiffies, start + HZ/4))
  1900. break;
  1901. }
  1902. if (vbat < 0x06) {
  1903. dev_notice(&wc->xb.pdev->dev,
  1904. "Excessive leakage detected on module %d: %d "
  1905. "volts (%02x) after %d ms\n", mod->card,
  1906. 376 * vbat / 1000, vbat,
  1907. (int)((jiffies - start) * 1000 / HZ));
  1908. return -1;
  1909. } else if (debug & DEBUG_CARD) {
  1910. dev_info(&wc->xb.pdev->dev,
  1911. "Post-leakage voltage: %d volts\n", 376 * vbat / 1000);
  1912. }
  1913. return 0;
  1914. }
  1915. static int wcaxx_powerup_proslic(struct wcaxx *wc,
  1916. struct wcaxx_module *mod, int fast)
  1917. {
  1918. unsigned char vbat;
  1919. unsigned long origjiffies;
  1920. int lim;
  1921. /* Set period of DC-DC converter to 1/64 khz */
  1922. wcaxx_setreg(wc, mod, 92, 0xc0 /* was 0xff */);
  1923. /* Wait for VBat to powerup */
  1924. origjiffies = jiffies;
  1925. /* Disable powerdown */
  1926. wcaxx_setreg(wc, mod, 14, 0);
  1927. /* If fast, don't bother checking anymore */
  1928. if (fast)
  1929. return 0;
  1930. while ((vbat = wcaxx_getreg(wc, mod, 82)) < 0xc0) {
  1931. /* Wait no more than 500ms */
  1932. if ((jiffies - origjiffies) > HZ/2)
  1933. break;
  1934. }
  1935. if (vbat < 0xc0) {
  1936. dev_notice(&wc->xb.pdev->dev, "ProSLIC on module %d failed to powerup within %d ms (%d mV only)\n\n -- DID YOU REMEMBER TO PLUG IN THE HD POWER CABLE TO THE TDM CARD??\n",
  1937. mod->card, (int)(((jiffies - origjiffies) * 1000 / HZ)),
  1938. vbat * 375);
  1939. return -1;
  1940. } else if (debug & DEBUG_CARD) {
  1941. dev_info(&wc->xb.pdev->dev,
  1942. "ProSLIC on module %d powered up to -%d volts (%02x) "
  1943. "in %d ms\n", mod->card, vbat * 376 / 1000, vbat,
  1944. (int)(((jiffies - origjiffies) * 1000 / HZ)));
  1945. }
  1946. /* Proslic max allowed loop current, reg 71 LOOP_I_LIMIT */
  1947. /* If out of range, just set it to the default value */
  1948. lim = (loopcurrent - 20) / 3;
  1949. if (loopcurrent > 41) {
  1950. lim = 0;
  1951. if (debug & DEBUG_CARD) {
  1952. dev_info(&wc->xb.pdev->dev,
  1953. "Loop current out of range! Setting to default 20mA!\n");
  1954. }
  1955. } else if (debug & DEBUG_CARD) {
  1956. dev_info(&wc->xb.pdev->dev,
  1957. "Loop current set to %dmA!\n", (lim*3)+20);
  1958. }
  1959. wcaxx_setreg(wc, mod, LOOP_I_LIMIT, lim);
  1960. /* Engage DC-DC converter */
  1961. wcaxx_setreg(wc, mod, 93, 0x19 /* was 0x19 */);
  1962. return 0;
  1963. }
  1964. static int
  1965. wcaxx_proslic_manual_calibrate(struct wcaxx *wc,
  1966. struct wcaxx_module *const mod)
  1967. {
  1968. unsigned long origjiffies;
  1969. unsigned char i;
  1970. /* Disable all interupts in DR21-23 */
  1971. wcaxx_setreg(wc, mod, 21, 0);
  1972. wcaxx_setreg(wc, mod, 22, 0);
  1973. wcaxx_setreg(wc, mod, 23, 0);
  1974. wcaxx_setreg(wc, mod, 64, 0);
  1975. /* (0x18) Calibrations without the ADC and DAC offset and without
  1976. * common mode calibration. */
  1977. wcaxx_setreg(wc, mod, 97, 0x18);
  1978. /* (0x47) Calibrate common mode and differential DAC mode DAC + ILIM */
  1979. wcaxx_setreg(wc, mod, 96, 0x47);
  1980. origjiffies = jiffies;
  1981. while (wcaxx_getreg(wc, mod, 96) != 0) {
  1982. if ((jiffies-origjiffies) > 80)
  1983. return -1;
  1984. }
  1985. /* Initialized DR 98 and 99 to get consistant results. 98 and 99 are
  1986. * the results registers and the search should have same intial
  1987. * conditions.
  1988. */
  1989. /******* The following is the manual gain mismatch calibration ********/
  1990. /******* This is also available as a function *************************/
  1991. msleep(20);
  1992. wcaxx_proslic_setreg_indirect(wc, mod, 88, 0);
  1993. wcaxx_proslic_setreg_indirect(wc, mod, 89, 0);
  1994. wcaxx_proslic_setreg_indirect(wc, mod, 90, 0);
  1995. wcaxx_proslic_setreg_indirect(wc, mod, 91, 0);
  1996. wcaxx_proslic_setreg_indirect(wc, mod, 92, 0);
  1997. wcaxx_proslic_setreg_indirect(wc, mod, 93, 0);
  1998. /* This is necessary if the calibration occurs other than at reset */
  1999. wcaxx_setreg(wc, mod, 98, 0x10);
  2000. wcaxx_setreg(wc, mod, 99, 0x10);
  2001. for (i = 0x1f; i > 0; i--) {
  2002. wcaxx_setreg(wc, mod, 98, i);
  2003. msleep(40);
  2004. if ((wcaxx_getreg(wc, mod, 88)) == 0)
  2005. break;
  2006. }
  2007. for (i = 0x1f; i > 0; i--) {
  2008. wcaxx_setreg(wc, mod, 99, i);
  2009. msleep(40);
  2010. if ((wcaxx_getreg(wc, mod, 89)) == 0)
  2011. break;
  2012. }
  2013. /******** The preceding is the manual gain mismatch calibration *******/
  2014. /******** The following is the longitudinal Balance Cal ***************/
  2015. wcaxx_setreg(wc, mod, 64, 1);
  2016. msleep(100);
  2017. wcaxx_setreg(wc, mod, 64, 0);
  2018. /* enable interrupt for the balance Cal */
  2019. wcaxx_setreg(wc, mod, 23, 0x4);
  2020. /* this is a singular calibration bit for longitudinal calibration */
  2021. wcaxx_setreg(wc, mod, 97, 0x1);
  2022. wcaxx_setreg(wc, mod, 96, 0x40);
  2023. wcaxx_getreg(wc, mod, 96); /* Read Reg 96 just cause */
  2024. wcaxx_setreg(wc, mod, 21, 0xFF);
  2025. wcaxx_setreg(wc, mod, 22, 0xFF);
  2026. wcaxx_setreg(wc, mod, 23, 0xFF);
  2027. /**The preceding is the longitudinal Balance Cal***/
  2028. return 0;
  2029. }
  2030. static int
  2031. wcaxx_proslic_calibrate(struct wcaxx *wc, struct wcaxx_module *mod)
  2032. {
  2033. unsigned long origjiffies;
  2034. int x;
  2035. /* Perform all calibrations */
  2036. wcaxx_setreg(wc, mod, 97, 0x1f);
  2037. /* Begin, no speedup */
  2038. wcaxx_setreg(wc, mod, 96, 0x5f);
  2039. /* Wait for it to finish */
  2040. origjiffies = jiffies;
  2041. while (wcaxx_getreg(wc, mod, 96)) {
  2042. if (time_after(jiffies, (origjiffies + (2*HZ)))) {
  2043. dev_notice(&wc->xb.pdev->dev,
  2044. "Timeout waiting for calibration of "
  2045. "module %d\n", mod->card);
  2046. return -1;
  2047. }
  2048. }
  2049. if (debug & DEBUG_CARD) {
  2050. /* Print calibration parameters */
  2051. dev_info(&wc->xb.pdev->dev,
  2052. "Calibration Vector Regs 98 - 107:\n");
  2053. for (x = 98; x < 108; x++) {
  2054. dev_info(&wc->xb.pdev->dev,
  2055. "%d: %02x\n", x, wcaxx_getreg(wc, mod, x));
  2056. }
  2057. }
  2058. return 0;
  2059. }
  2060. /*********************************************************************
  2061. * Set the hwgain on the analog modules
  2062. *
  2063. * card = the card position for this module (0-23)
  2064. * gain = gain in dB x10 (e.g. -3.5dB would be gain=-35)
  2065. * tx = (0 for rx; 1 for tx)
  2066. *
  2067. *******************************************************************/
  2068. static int
  2069. wcaxx_set_hwgain(struct wcaxx *wc, struct wcaxx_module *mod,
  2070. __s32 gain, __u32 tx)
  2071. {
  2072. if (mod->type != FXO) {
  2073. dev_notice(&wc->xb.pdev->dev,
  2074. "Cannot adjust gain. Unsupported module type!\n");
  2075. return -1;
  2076. }
  2077. if (tx) {
  2078. if (debug) {
  2079. dev_info(&wc->xb.pdev->dev,
  2080. "setting FXO tx gain for card=%d to %d\n",
  2081. mod->card, gain);
  2082. }
  2083. if (gain >= -150 && gain <= 0) {
  2084. wcaxx_setreg(wc, mod, 38, 16 + (gain / -10));
  2085. wcaxx_setreg(wc, mod, 40, 16 + (-gain % 10));
  2086. } else if (gain <= 120 && gain > 0) {
  2087. wcaxx_setreg(wc, mod, 38, gain/10);
  2088. wcaxx_setreg(wc, mod, 40, (gain%10));
  2089. } else {
  2090. dev_notice(&wc->xb.pdev->dev,
  2091. "FXO tx gain is out of range (%d)\n", gain);
  2092. return -1;
  2093. }
  2094. } else { /* rx */
  2095. if (debug) {
  2096. dev_info(&wc->xb.pdev->dev,
  2097. "setting FXO rx gain for card=%d to %d\n",
  2098. mod->card, gain);
  2099. }
  2100. if (gain >= -150 && gain <= 0) {
  2101. wcaxx_setreg(wc, mod, 39, 16 + (gain / -10));
  2102. wcaxx_setreg(wc, mod, 41, 16 + (-gain % 10));
  2103. } else if (gain <= 120 && gain > 0) {
  2104. wcaxx_setreg(wc, mod, 39, gain/10);
  2105. wcaxx_setreg(wc, mod, 41, (gain%10));
  2106. } else {
  2107. dev_notice(&wc->xb.pdev->dev,
  2108. "FXO rx gain is out of range (%d)\n", gain);
  2109. return -1;
  2110. }
  2111. }
  2112. return 0;
  2113. }
  2114. static int set_lasttxhook_interruptible(struct wcaxx *wc, struct fxs *fxs,
  2115. unsigned newval, int *psethook)
  2116. {
  2117. int res = 0;
  2118. unsigned long flags;
  2119. int timeout = 0;
  2120. do {
  2121. spin_lock_irqsave(&wc->reglock, flags);
  2122. if (SLIC_LF_OPPENDING & fxs->lasttxhook) {
  2123. spin_unlock_irqrestore(&wc->reglock, flags);
  2124. if (timeout++ > 100)
  2125. return -1;
  2126. msleep(100);
  2127. } else {
  2128. fxs->lasttxhook = (newval & SLIC_LF_SETMASK) |
  2129. SLIC_LF_OPPENDING;
  2130. *psethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
  2131. spin_unlock_irqrestore(&wc->reglock, flags);
  2132. break;
  2133. }
  2134. } while (1);
  2135. return res;
  2136. }
  2137. /* Must be called from within an interruptible context */
  2138. static int set_vmwi(struct wcaxx *wc, struct wcaxx_module *const mod)
  2139. {
  2140. int x;
  2141. struct fxs *const fxs = &mod->mod.fxs;
  2142. /* Presently only supports line reversal MWI */
  2143. if ((fxs->vmwi_active_messages) &&
  2144. (fxs->vmwisetting.vmwi_type & DAHDI_VMWI_LREV))
  2145. fxs->vmwi_linereverse = 1;
  2146. else
  2147. fxs->vmwi_linereverse = 0;
  2148. /* Set line polarity for new VMWI state */
  2149. if (POLARITY_XOR(fxs)) {
  2150. fxs->idletxhookstate |= SLIC_LF_REVMASK;
  2151. /* Do not set while currently ringing or open */
  2152. if (((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_RINGING) &&
  2153. ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)) {
  2154. x = fxs->lasttxhook;
  2155. x |= SLIC_LF_REVMASK;
  2156. set_lasttxhook_interruptible(wc, fxs, x, &mod->sethook);
  2157. }
  2158. } else {
  2159. fxs->idletxhookstate &= ~SLIC_LF_REVMASK;
  2160. /* Do not set while currently ringing or open */
  2161. if (((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_RINGING) &&
  2162. ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)) {
  2163. x = fxs->lasttxhook;
  2164. x &= ~SLIC_LF_REVMASK;
  2165. set_lasttxhook_interruptible(wc, fxs, x, &mod->sethook);
  2166. }
  2167. }
  2168. if (debug) {
  2169. dev_info(&wc->xb.pdev->dev,
  2170. "Setting VMWI on channel %d, messages=%d, lrev=%d\n",
  2171. mod->card, fxs->vmwi_active_messages,
  2172. fxs->vmwi_linereverse);
  2173. }
  2174. return 0;
  2175. }
  2176. static void
  2177. wcaxx_voicedaa_set_ts(struct wcaxx *wc, struct wcaxx_module *mod, int ts)
  2178. {
  2179. /* 34 bits from framesysc to the first channel, 8 bits in each ts * (th
  2180. * e timeslot we're assigning + 1 to skip for VPMOCT issue on first
  2181. * timeslot + 3 in that there are 4 bytes assigned for each timeslot on
  2182. * framer which was copied to this card */
  2183. /* 34 + 8 * (ts + 1 + 3) */
  2184. wcaxx_setreg(wc, mod, 34, (ts * 8 + 42 + (ts * 3 * 8)) & 0xff);
  2185. wcaxx_setreg(wc, mod, 35, (ts * 8 + 42 + (ts * 3 * 8)) >> 8);
  2186. wcaxx_setreg(wc, mod, 36, (ts * 8 + 42 + (ts * 3 * 8)) & 0xff);
  2187. wcaxx_setreg(wc, mod, 37, (ts * 8 + 42 + (ts * 3 * 8)) >> 8);
  2188. if (debug) {
  2189. dev_info(&wc->xb.pdev->dev,
  2190. "voicedaa: card %d new timeslot: %d\n",
  2191. mod->card + 1, ts);
  2192. }
  2193. }
  2194. static int
  2195. wcaxx_init_voicedaa(struct wcaxx *wc, struct wcaxx_module *mod,
  2196. int fast, int manual, int sane)
  2197. {
  2198. unsigned char reg16 = 0, reg26 = 0, reg30 = 0, reg31 = 0;
  2199. unsigned long flags;
  2200. unsigned long newjiffies;
  2201. /* Send a short write to the device in order to reset the SPI state
  2202. * machine. It may be out of sync since the driver was probing for an
  2203. * FXS device on that chip select. */
  2204. /* wcxb_spi_short_write(mod->spi); */
  2205. spin_lock_irqsave(&wc->reglock, flags);
  2206. mod->type = FXO;
  2207. spin_unlock_irqrestore(&wc->reglock, flags);
  2208. if (!sane && wcaxx_voicedaa_insane(wc, mod))
  2209. return -2;
  2210. /* Software reset */
  2211. wcaxx_setreg(wc, mod, 1, 0x80);
  2212. msleep(100);
  2213. /* Set On-hook speed, Ringer impedence, and ringer threshold */
  2214. reg16 |= (fxo_modes[_opermode].ohs << 6);
  2215. reg16 |= (fxo_modes[_opermode].rz << 1);
  2216. reg16 |= (fxo_modes[_opermode].rt);
  2217. wcaxx_setreg(wc, mod, 16, reg16);
  2218. /* Enable ring detector full-wave rectifier mode */
  2219. wcaxx_setreg(wc, mod, 18, 2);
  2220. wcaxx_setreg(wc, mod, 24, 0);
  2221. /* Set DC Termination:
  2222. Tip/Ring voltage adjust, minimum operational current, current
  2223. limitation */
  2224. reg26 |= (fxo_modes[_opermode].dcv << 6);
  2225. reg26 |= (fxo_modes[_opermode].mini << 4);
  2226. reg26 |= (fxo_modes[_opermode].ilim << 1);
  2227. wcaxx_setreg(wc, mod, 26, reg26);
  2228. /* Set AC Impedence */
  2229. reg30 = (fxo_modes[_opermode].acim);
  2230. wcaxx_setreg(wc, mod, 30, reg30);
  2231. /* Misc. DAA parameters */
  2232. /* If fast pickup is set, then the off hook counter will be set to 8
  2233. * ms, otherwise 128 ms. */
  2234. reg31 = (fastpickup) ? 0xe3 : 0xa3;
  2235. reg31 |= (fxo_modes[_opermode].ohs2 << 3);
  2236. wcaxx_setreg(wc, mod, 31, reg31);
  2237. wcaxx_voicedaa_set_ts(wc, mod, mod->card);
  2238. /* Enable ISO-Cap */
  2239. wcaxx_setreg(wc, mod, 6, 0x00);
  2240. /* Turn off the calibration delay when fastpickup is enabled. */
  2241. if (fastpickup)
  2242. wcaxx_setreg(wc, mod, 17, wcaxx_getreg(wc, mod, 17) | 0x20);
  2243. /* Wait 2000ms for ISO-cap to come up */
  2244. newjiffies = jiffies + msecs_to_jiffies(2000);
  2245. while (time_before(jiffies, newjiffies) &&
  2246. !(wcaxx_getreg(wc, mod, 11) & 0xf0))
  2247. msleep(100);
  2248. if (!(wcaxx_getreg(wc, mod, 11) & 0xf0)) {
  2249. dev_notice(&wc->xb.pdev->dev, "VoiceDAA did not bring up ISO link properly!\n");
  2250. return -1;
  2251. }
  2252. if (debug & DEBUG_CARD) {
  2253. dev_info(&wc->xb.pdev->dev, "ISO-Cap is now up, line side: %02x rev %02x\n",
  2254. wcaxx_getreg(wc, mod, 11) >> 4,
  2255. (wcaxx_getreg(wc, mod, 13) >> 2) & 0xf);
  2256. }
  2257. /* Enable on-hook line monitor */
  2258. wcaxx_setreg(wc, mod, 5, 0x08);
  2259. /* Take values for fxotxgain and fxorxgain and apply them to module */
  2260. wcaxx_set_hwgain(wc, mod, fxotxgain, 1);
  2261. wcaxx_set_hwgain(wc, mod, fxorxgain, 0);
  2262. #ifdef DEBUG
  2263. if (digitalloopback) {
  2264. dev_info(&wc->xb.pdev->dev,
  2265. "Turning on digital loopback for port %d.\n",
  2266. mod->card + 1);
  2267. wcaxx_setreg(wc, mod, 10, 0x01);
  2268. }
  2269. #endif
  2270. if (debug) {
  2271. dev_info(&wc->xb.pdev->dev,
  2272. "DEBUG fxotxgain:%i.%i fxorxgain:%i.%i\n",
  2273. (wcaxx_getreg(wc, mod, 38)/16) ?
  2274. -(wcaxx_getreg(wc, mod, 38) - 16) :
  2275. wcaxx_getreg(wc, mod, 38),
  2276. (wcaxx_getreg(wc, mod, 40)/16) ?
  2277. -(wcaxx_getreg(wc, mod, 40) - 16) :
  2278. wcaxx_getreg(wc, mod, 40),
  2279. (wcaxx_getreg(wc, mod, 39)/16) ?
  2280. -(wcaxx_getreg(wc, mod, 39) - 16) :
  2281. wcaxx_getreg(wc, mod, 39),
  2282. (wcaxx_getreg(wc, mod, 41)/16) ?
  2283. -(wcaxx_getreg(wc, mod, 41) - 16) :
  2284. wcaxx_getreg(wc, mod, 41));
  2285. }
  2286. return 0;
  2287. }
  2288. static void
  2289. wcaxx_proslic_set_ts(struct wcaxx *wc, struct wcaxx_module *mod, int ts)
  2290. {
  2291. /* Tx Start low byte 0 */
  2292. wcaxx_setreg(wc, mod, 2, (ts * 8 + 42 + (ts * 3 * 8)) & 0xff);
  2293. /* Tx Start high byte 0 */
  2294. wcaxx_setreg(wc, mod, 3, (ts * 8 + 42 + (ts * 3 * 8)) >> 8);
  2295. /* Rx Start low byte 0 */
  2296. wcaxx_setreg(wc, mod, 4, (ts * 8 + 42 + (ts * 3 * 8)) & 0xff);
  2297. /* Rx Start high byte 0 */
  2298. wcaxx_setreg(wc, mod, 5, (ts * 8 + 42 + (ts * 3 * 8)) >> 8);
  2299. if (debug) {
  2300. dev_info(&wc->xb.pdev->dev,
  2301. "proslic: card %d new timeslot: %d\n",
  2302. mod->card + 1, ts);
  2303. }
  2304. }
  2305. static int
  2306. wcaxx_init_proslic(struct wcaxx *wc, struct wcaxx_module *const mod,
  2307. int fast, int manual, int sane)
  2308. {
  2309. struct fxs *const fxs = &mod->mod.fxs;
  2310. unsigned short tmp[5];
  2311. unsigned long flags;
  2312. unsigned char r19, r9;
  2313. int x;
  2314. int fxsmode = 0;
  2315. int addresses[ARRAY_SIZE(fxs->calregs.vals)];
  2316. #if 0 /* TODO */
  2317. if (wc->mods[mod->card & 0xfc].type == QRV)
  2318. return -2;
  2319. #endif
  2320. spin_lock_irqsave(&wc->reglock, flags);
  2321. mod->type = FXS;
  2322. spin_unlock_irqrestore(&wc->reglock, flags);
  2323. /* msleep(100); */
  2324. /* Sanity check the ProSLIC */
  2325. if (!sane && wcaxx_proslic_insane(wc, mod))
  2326. return -2;
  2327. /* Initialize VMWI settings */
  2328. memset(&(fxs->vmwisetting), 0, sizeof(fxs->vmwisetting));
  2329. fxs->vmwi_linereverse = 0;
  2330. /* By default, don't send on hook */
  2331. if (!reversepolarity != !fxs->reversepolarity)
  2332. fxs->idletxhookstate = SLIC_LF_ACTIVE_REV;
  2333. else
  2334. fxs->idletxhookstate = SLIC_LF_ACTIVE_FWD;
  2335. if (sane) {
  2336. /* Make sure we turn off the DC->DC converter to prevent
  2337. * anything from blowing up */
  2338. wcaxx_setreg(wc, mod, 14, 0x10);
  2339. }
  2340. if (wcaxx_proslic_init_indirect_regs(wc, mod)) {
  2341. dev_info(&wc->xb.pdev->dev,
  2342. "Indirect Registers failed to initialize on "
  2343. "module %d.\n", mod->card);
  2344. return -1;
  2345. }
  2346. /* Clear scratch pad area */
  2347. wcaxx_proslic_setreg_indirect(wc, mod, 97, 0);
  2348. /* Clear digital loopback */
  2349. wcaxx_setreg(wc, mod, 8, 0);
  2350. /* Revision C optimization */
  2351. wcaxx_setreg(wc, mod, 108, 0xeb);
  2352. /* Disable automatic VBat switching for safety to prevent
  2353. * Q7 from accidently turning on and burning out.
  2354. * If pulse dialing has trouble at high REN loads change this to 0x17 */
  2355. wcaxx_setreg(wc, mod, 67, 0x07);
  2356. /* Turn off Q7 */
  2357. wcaxx_setreg(wc, mod, 66, 1);
  2358. /* Flush ProSLIC digital filters by setting to clear, while
  2359. saving old values */
  2360. for (x = 0; x < 5; x++) {
  2361. tmp[x] = wcaxx_proslic_getreg_indirect(wc, mod, x + 35);
  2362. wcaxx_proslic_setreg_indirect(wc, mod, x + 35, 0x8000);
  2363. }
  2364. /* Power up the DC-DC converter */
  2365. if (wcaxx_powerup_proslic(wc, mod, fast)) {
  2366. dev_notice(&wc->xb.pdev->dev,
  2367. "Unable to do INITIAL ProSLIC powerup on "
  2368. "module %d\n", mod->card);
  2369. return -1;
  2370. }
  2371. if (!fast) {
  2372. /* Check for power leaks */
  2373. if (wcaxx_proslic_powerleak_test(wc, mod)) {
  2374. dev_notice(&wc->xb.pdev->dev,
  2375. "ProSLIC module %d failed leakage test. "
  2376. "Check for short circuit\n", mod->card);
  2377. }
  2378. /* Power up again */
  2379. if (wcaxx_powerup_proslic(wc, mod, fast)) {
  2380. dev_notice(&wc->xb.pdev->dev,
  2381. "Unable to do FINAL ProSLIC powerup on "
  2382. "module %d\n", mod->card);
  2383. return -1;
  2384. }
  2385. #ifndef NO_CALIBRATION
  2386. /* Perform calibration */
  2387. if (manual) {
  2388. if (wcaxx_proslic_manual_calibrate(wc, mod)) {
  2389. dev_dbg(&wc->xb.pdev->dev,
  2390. "Proslic failed on Manual Calibration\n");
  2391. if (wcaxx_proslic_manual_calibrate(wc, mod)) {
  2392. dev_notice(&wc->xb.pdev->dev,
  2393. "Proslic Failed on Second Attempt to Calibrate Manually. (Try -DNO_CALIBRATION in Makefile)\n");
  2394. return -1;
  2395. }
  2396. dev_info(&wc->xb.pdev->dev,
  2397. "Proslic Passed Manual Calibration on Second Attempt\n");
  2398. }
  2399. } else {
  2400. if (wcaxx_proslic_calibrate(wc, mod)) {
  2401. dev_dbg(&wc->xb.pdev->dev,
  2402. "ProSlic died on Auto Calibration.\n");
  2403. if (wcaxx_proslic_calibrate(wc, mod)) {
  2404. dev_notice(&wc->xb.pdev->dev,
  2405. "Proslic Failed on Second Attempt to Auto Calibrate\n");
  2406. return -1;
  2407. }
  2408. dev_info(&wc->xb.pdev->dev,
  2409. "Proslic Passed Auto Calibration on Second Attempt\n");
  2410. }
  2411. }
  2412. /* Perform DC-DC calibration */
  2413. wcaxx_setreg(wc, mod, 93, 0x99);
  2414. r19 = wcaxx_getreg(wc, mod, 107);
  2415. if ((r19 < 0x2) || (r19 > 0xd)) {
  2416. dev_notice(&wc->xb.pdev->dev,
  2417. "DC-DC cal has a surprising direct 107 of 0x%02x!\n",
  2418. r19);
  2419. wcaxx_setreg(wc, mod, 107, 0x8);
  2420. }
  2421. /* Save calibration vectors */
  2422. for (x = 0; x < ARRAY_SIZE(addresses); x++)
  2423. addresses[x] = 96 + x;
  2424. wcaxx_getregs(wc, mod, addresses, ARRAY_SIZE(addresses));
  2425. for (x = 0; x < ARRAY_SIZE(fxs->calregs.vals); x++)
  2426. fxs->calregs.vals[x] = addresses[x];
  2427. #endif
  2428. } else {
  2429. /* Restore calibration registers */
  2430. for (x = 0; x < ARRAY_SIZE(fxs->calregs.vals); x++)
  2431. wcaxx_setreg(wc, mod, 96 + x, fxs->calregs.vals[x]);
  2432. }
  2433. /* Calibration complete, restore original values */
  2434. for (x = 0; x < 5; x++)
  2435. wcaxx_proslic_setreg_indirect(wc, mod, x + 35, tmp[x]);
  2436. if (wcaxx_proslic_verify_indirect_regs(wc, mod)) {
  2437. dev_info(&wc->xb.pdev->dev, "Indirect Registers failed verification.\n");
  2438. return -1;
  2439. }
  2440. /* U-Law 8-bit interface */
  2441. wcaxx_proslic_set_ts(wc, mod, mod->card);
  2442. wcaxx_setreg(wc, mod, 18, 0xff); /* clear all interrupt */
  2443. wcaxx_setreg(wc, mod, 19, 0xff);
  2444. wcaxx_setreg(wc, mod, 20, 0xff);
  2445. wcaxx_setreg(wc, mod, 22, 0xff);
  2446. wcaxx_setreg(wc, mod, 73, 0x04);
  2447. wcaxx_setreg(wc, mod, 69, 0x4);
  2448. if (fxshonormode) {
  2449. static const int ACIM2TISS[16] = { 0x0, 0x1, 0x4, 0x5, 0x7,
  2450. 0x0, 0x0, 0x6, 0x0, 0x0,
  2451. 0x0, 0x2, 0x0, 0x3 };
  2452. fxsmode = ACIM2TISS[fxo_modes[_opermode].acim];
  2453. wcaxx_setreg(wc, mod, 10, 0x08 | fxsmode);
  2454. if (fxo_modes[_opermode].ring_osc) {
  2455. wcaxx_proslic_setreg_indirect(wc, mod, 20,
  2456. fxo_modes[_opermode].ring_osc);
  2457. }
  2458. if (fxo_modes[_opermode].ring_x) {
  2459. wcaxx_proslic_setreg_indirect(wc, mod, 21,
  2460. fxo_modes[_opermode].ring_x);
  2461. }
  2462. }
  2463. if (lowpower)
  2464. wcaxx_setreg(wc, mod, 72, 0x10);
  2465. if (fastringer) {
  2466. /* Speed up Ringer */
  2467. wcaxx_proslic_setreg_indirect(wc, mod, 20, 0x7e6d);
  2468. wcaxx_proslic_setreg_indirect(wc, mod, 21, 0x01b9);
  2469. /* Beef up Ringing voltage to 89V */
  2470. if (boostringer) {
  2471. wcaxx_setreg(wc, mod, 74, 0x3f);
  2472. if (wcaxx_proslic_setreg_indirect(wc, mod, 21, 0x247))
  2473. return -1;
  2474. dev_info(&wc->xb.pdev->dev,
  2475. "Boosting fast ringer on slot %d (89V peak)\n",
  2476. mod->card + 1);
  2477. } else if (lowpower) {
  2478. if (wcaxx_proslic_setreg_indirect(wc, mod, 21, 0x14b))
  2479. return -1;
  2480. dev_info(&wc->xb.pdev->dev,
  2481. "Reducing fast ring power on slot %d "
  2482. "(50V peak)\n", mod->card + 1);
  2483. } else
  2484. dev_info(&wc->xb.pdev->dev,
  2485. "Speeding up ringer on slot %d (25Hz)\n",
  2486. mod->card + 1);
  2487. } else {
  2488. /* Beef up Ringing voltage to 89V */
  2489. if (boostringer) {
  2490. wcaxx_setreg(wc, mod, 74, 0x3f);
  2491. if (wcaxx_proslic_setreg_indirect(wc, mod, 21, 0x1d1))
  2492. return -1;
  2493. dev_info(&wc->xb.pdev->dev,
  2494. "Boosting ringer on slot %d (89V peak)\n",
  2495. mod->card + 1);
  2496. } else if (lowpower) {
  2497. if (wcaxx_proslic_setreg_indirect(wc, mod, 21, 0x108))
  2498. return -1;
  2499. dev_info(&wc->xb.pdev->dev,
  2500. "Reducing ring power on slot %d "
  2501. "(50V peak)\n", mod->card + 1);
  2502. }
  2503. }
  2504. if (fxstxgain || fxsrxgain) {
  2505. r9 = wcaxx_getreg(wc, mod, 9);
  2506. switch (fxstxgain) {
  2507. case 35:
  2508. r9 += 8;
  2509. break;
  2510. case -35:
  2511. r9 += 4;
  2512. break;
  2513. case 0:
  2514. break;
  2515. }
  2516. switch (fxsrxgain) {
  2517. case 35:
  2518. r9 += 2;
  2519. break;
  2520. case -35:
  2521. r9 += 1;
  2522. break;
  2523. case 0:
  2524. break;
  2525. }
  2526. wcaxx_setreg(wc, mod, 9, r9);
  2527. }
  2528. if (debug) {
  2529. dev_info(&wc->xb.pdev->dev,
  2530. "DEBUG: fxstxgain:%s fxsrxgain:%s\n",
  2531. ((wcaxx_getreg(wc, mod, 9) / 8) == 1) ?
  2532. "3.5" : ((wcaxx_getreg(wc, mod, 9) / 4) == 1) ?
  2533. "-3.5" : "0.0",
  2534. ((wcaxx_getreg(wc, mod, 9) / 2) == 1) ?
  2535. "3.5" : ((wcaxx_getreg(wc, mod, 9) % 2) ?
  2536. "-3.5" : "0.0"));
  2537. }
  2538. fxs->lasttxhook = fxs->idletxhookstate;
  2539. wcaxx_setreg(wc, mod, LINE_STATE, fxs->lasttxhook);
  2540. /* Preset the shadow register so that we won't get a power alarm when
  2541. * we finish initialization, otherwise the line state register may not
  2542. * have been read yet. */
  2543. fxs->linefeed_control_shadow = fxs->lasttxhook;
  2544. return 0;
  2545. }
  2546. static void wcaxx_get_fxs_regs(struct wcaxx *wc, struct wcaxx_module *mod,
  2547. struct wctdm_regs *regs)
  2548. {
  2549. int x;
  2550. for (x = 0; x < NUM_INDIRECT_REGS; x++)
  2551. regs->indirect[x] = wcaxx_proslic_getreg_indirect(wc, mod, x);
  2552. for (x = 0; x < NUM_REGS; x++)
  2553. regs->direct[x] = wcaxx_getreg(wc, mod, x);
  2554. }
  2555. static void wcaxx_get_fxo_regs(struct wcaxx *wc, struct wcaxx_module *mod,
  2556. struct wctdm_regs *regs)
  2557. {
  2558. const unsigned int NUM_FXO_REGS = 60;
  2559. int x;
  2560. for (x = 0; x < NUM_FXO_REGS; x++)
  2561. regs->direct[x] = wcaxx_getreg(wc, mod, x);
  2562. }
  2563. static int
  2564. wcaxx_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
  2565. {
  2566. struct wctdm_stats stats;
  2567. struct wctdm_regop regop;
  2568. struct wctdm_echo_coefs echoregs;
  2569. struct dahdi_hwgain hwgain;
  2570. struct wcaxx *wc = chan->pvt;
  2571. int x;
  2572. struct wcaxx_module *const mod = &wc->mods[chan->chanpos - 1];
  2573. struct fxs *const fxs = &mod->mod.fxs;
  2574. switch (cmd) {
  2575. case DAHDI_ONHOOKTRANSFER:
  2576. if (mod->type != FXS)
  2577. return -EINVAL;
  2578. if (get_user(x, (__user int *) data))
  2579. return -EFAULT;
  2580. /* Active mode when idle */
  2581. fxs->idletxhookstate = POLARITY_XOR(fxs) ?
  2582. SLIC_LF_ACTIVE_REV :
  2583. SLIC_LF_ACTIVE_FWD;
  2584. if (fxs_lf(fxs, ACTIVE_FWD) || fxs_lf(fxs, ACTIVE_REV)) {
  2585. int res;
  2586. res = set_lasttxhook_interruptible(wc, fxs,
  2587. (POLARITY_XOR(fxs) ?
  2588. SLIC_LF_OHTRAN_REV : SLIC_LF_OHTRAN_FWD),
  2589. &mod->sethook);
  2590. if (debug & DEBUG_CARD) {
  2591. if (res) {
  2592. dev_info(&wc->xb.pdev->dev,
  2593. "Channel %d TIMEOUT: "
  2594. "OnHookTransfer start\n",
  2595. chan->chanpos - 1);
  2596. } else {
  2597. dev_info(&wc->xb.pdev->dev,
  2598. "Channel %d OnHookTransfer "
  2599. "start\n", chan->chanpos - 1);
  2600. }
  2601. }
  2602. }
  2603. fxs->ohttimer = wc->framecount + x;
  2604. fxs->oht_active = 1;
  2605. break;
  2606. case DAHDI_VMWI_CONFIG:
  2607. if (mod->type != FXS)
  2608. return -EINVAL;
  2609. if (copy_from_user(&(fxs->vmwisetting),
  2610. (__user void *)data,
  2611. sizeof(fxs->vmwisetting)))
  2612. return -EFAULT;
  2613. set_vmwi(wc, mod);
  2614. break;
  2615. case DAHDI_VMWI:
  2616. if (mod->type != FXS)
  2617. return -EINVAL;
  2618. if (get_user(x, (__user int *) data))
  2619. return -EFAULT;
  2620. if (0 > x)
  2621. return -EFAULT;
  2622. fxs->vmwi_active_messages = x;
  2623. set_vmwi(wc, mod);
  2624. break;
  2625. case WCTDM_GET_STATS:
  2626. if (mod->type == FXS) {
  2627. stats.tipvolt = wcaxx_getreg(wc, mod, 80) * -376;
  2628. stats.ringvolt = wcaxx_getreg(wc, mod, 81) * -376;
  2629. stats.batvolt = wcaxx_getreg(wc, mod, 82) * -376;
  2630. } else if (mod->type == FXO) {
  2631. stats.tipvolt = (s8)wcaxx_getreg(wc, mod, 29) * 1000;
  2632. stats.ringvolt = (s8)wcaxx_getreg(wc, mod, 29) * 1000;
  2633. stats.batvolt = (s8)wcaxx_getreg(wc, mod, 29) * 1000;
  2634. } else
  2635. return -EINVAL;
  2636. if (copy_to_user((__user void *) data, &stats, sizeof(stats)))
  2637. return -EFAULT;
  2638. break;
  2639. case WCTDM_GET_REGS:
  2640. {
  2641. struct wctdm_regs *regs = kzalloc(sizeof(*regs), GFP_KERNEL);
  2642. if (!regs)
  2643. return -ENOMEM;
  2644. if (mod->type == FXS)
  2645. wcaxx_get_fxs_regs(wc, mod, regs);
  2646. else
  2647. wcaxx_get_fxo_regs(wc, mod, regs);
  2648. if (copy_to_user((__user void *)data, regs, sizeof(*regs))) {
  2649. kfree(regs);
  2650. return -EFAULT;
  2651. }
  2652. kfree(regs);
  2653. break;
  2654. }
  2655. case WCTDM_SET_REG:
  2656. if (copy_from_user(&regop, (__user void *) data, sizeof(regop)))
  2657. return -EFAULT;
  2658. if (regop.indirect) {
  2659. if (mod->type != FXS)
  2660. return -EINVAL;
  2661. dev_info(&wc->xb.pdev->dev,
  2662. "Setting indirect %d to 0x%04x on %d\n",
  2663. regop.reg, regop.val, chan->chanpos);
  2664. wcaxx_proslic_setreg_indirect(wc, mod, regop.reg,
  2665. regop.val);
  2666. } else {
  2667. regop.val &= 0xff;
  2668. if (regop.reg == LINE_STATE) {
  2669. /* Set feedback register to indicate the new
  2670. * state that is being set */
  2671. fxs->lasttxhook = (regop.val & 0x0f) |
  2672. SLIC_LF_OPPENDING;
  2673. }
  2674. dev_info(&wc->xb.pdev->dev,
  2675. "Setting direct %d to %04x on %d\n",
  2676. regop.reg, regop.val, chan->chanpos);
  2677. wcaxx_setreg(wc, mod, regop.reg, regop.val);
  2678. }
  2679. break;
  2680. case WCTDM_SET_ECHOTUNE:
  2681. dev_info(&wc->xb.pdev->dev, "-- Setting echo registers:\n");
  2682. if (copy_from_user(&echoregs, (__user void *) data,
  2683. sizeof(echoregs)))
  2684. return -EFAULT;
  2685. if (mod->type == FXO) {
  2686. /* Set the ACIM register */
  2687. wcaxx_setreg(wc, mod, 30, echoregs.acim);
  2688. /* Set the digital echo canceller registers */
  2689. wcaxx_setreg(wc, mod, 45, echoregs.coef1);
  2690. wcaxx_setreg(wc, mod, 46, echoregs.coef2);
  2691. wcaxx_setreg(wc, mod, 47, echoregs.coef3);
  2692. wcaxx_setreg(wc, mod, 48, echoregs.coef4);
  2693. wcaxx_setreg(wc, mod, 49, echoregs.coef5);
  2694. wcaxx_setreg(wc, mod, 50, echoregs.coef6);
  2695. wcaxx_setreg(wc, mod, 51, echoregs.coef7);
  2696. wcaxx_setreg(wc, mod, 52, echoregs.coef8);
  2697. dev_info(&wc->xb.pdev->dev, "-- Set echo registers successfully\n");
  2698. break;
  2699. } else {
  2700. return -EINVAL;
  2701. }
  2702. break;
  2703. case DAHDI_SET_HWGAIN:
  2704. if (copy_from_user(&hwgain, (__user void *) data,
  2705. sizeof(hwgain)))
  2706. return -EFAULT;
  2707. wcaxx_set_hwgain(wc, mod, hwgain.newgain, hwgain.tx);
  2708. if (debug) {
  2709. dev_info(&wc->xb.pdev->dev,
  2710. "Setting hwgain on channel %d to %d for %s direction\n",
  2711. chan->chanpos-1, hwgain.newgain,
  2712. ((hwgain.tx) ? "tx" : "rx"));
  2713. }
  2714. break;
  2715. case DAHDI_TONEDETECT:
  2716. /* Hardware DTMF detection is not supported. */
  2717. return -ENOSYS;
  2718. case DAHDI_SETPOLARITY:
  2719. if (get_user(x, (__user int *) data))
  2720. return -EFAULT;
  2721. if (mod->type != FXS)
  2722. return -EINVAL;
  2723. /* Can't change polarity while ringing or when open */
  2724. if (((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_RINGING) ||
  2725. ((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_OPEN)) {
  2726. if (debug & DEBUG_CARD) {
  2727. dev_info(&wc->xb.pdev->dev,
  2728. "Channel %d Unable to Set Polarity\n",
  2729. chan->chanpos - 1);
  2730. }
  2731. return -EINVAL;
  2732. }
  2733. fxs->reversepolarity = (x) ? 1 : 0;
  2734. if (POLARITY_XOR(fxs)) {
  2735. fxs->idletxhookstate |= SLIC_LF_REVMASK;
  2736. x = fxs->lasttxhook & SLIC_LF_SETMASK;
  2737. x |= SLIC_LF_REVMASK;
  2738. if (x != fxs->lasttxhook) {
  2739. x = set_lasttxhook_interruptible(wc, fxs, x,
  2740. &mod->sethook);
  2741. if ((debug & DEBUG_CARD) && x) {
  2742. dev_info(&wc->xb.pdev->dev,
  2743. "Channel %d TIMEOUT: Set Reverse Polarity\n",
  2744. chan->chanpos - 1);
  2745. } else if (debug & DEBUG_CARD) {
  2746. dev_info(&wc->xb.pdev->dev,
  2747. "Channel %d Set Reverse Polarity\n",
  2748. chan->chanpos - 1);
  2749. }
  2750. }
  2751. } else {
  2752. fxs->idletxhookstate &= ~SLIC_LF_REVMASK;
  2753. x = fxs->lasttxhook & SLIC_LF_SETMASK;
  2754. x &= ~SLIC_LF_REVMASK;
  2755. if (x != fxs->lasttxhook) {
  2756. x = set_lasttxhook_interruptible(wc, fxs, x,
  2757. &mod->sethook);
  2758. if ((debug & DEBUG_CARD) & x) {
  2759. dev_info(&wc->xb.pdev->dev,
  2760. "Channel %d TIMEOUT: Set Normal Polarity\n",
  2761. chan->chanpos - 1);
  2762. } else if (debug & DEBUG_CARD) {
  2763. dev_info(&wc->xb.pdev->dev,
  2764. "Channel %d Set Normal Polarity\n",
  2765. chan->chanpos - 1);
  2766. }
  2767. }
  2768. }
  2769. break;
  2770. default:
  2771. return -ENOTTY;
  2772. }
  2773. return 0;
  2774. }
  2775. static int wcaxx_open(struct dahdi_chan *chan)
  2776. {
  2777. struct wcaxx *const wc = chan->pvt;
  2778. unsigned long flags;
  2779. struct wcaxx_module *const mod = &wc->mods[chan->chanpos - 1];
  2780. #if 0
  2781. if (wc->dead)
  2782. return -ENODEV;
  2783. #endif
  2784. if (mod->type == FXO) {
  2785. /* Reset the mwi indicators */
  2786. spin_lock_irqsave(&wc->reglock, flags);
  2787. mod->mod.fxo.neonmwi_debounce = 0;
  2788. mod->mod.fxo.neonmwi_offcounter = 0;
  2789. mod->mod.fxo.neonmwi_state = 0;
  2790. spin_unlock_irqrestore(&wc->reglock, flags);
  2791. }
  2792. return 0;
  2793. }
  2794. static inline struct wcaxx *span_to_wcaxx(struct dahdi_span *span)
  2795. {
  2796. struct wcaxx *wc = container_of(span, struct wcaxx, span);
  2797. return wc;
  2798. }
  2799. static int wcaxx_watchdog(struct dahdi_span *span, int event)
  2800. {
  2801. struct wcaxx *wc = span_to_wcaxx(span);
  2802. dev_info(&wc->xb.pdev->dev, "TDM: Called watchdog\n");
  2803. return 0;
  2804. }
  2805. static int wcaxx_close(struct dahdi_chan *chan)
  2806. {
  2807. struct wcaxx *wc;
  2808. int x;
  2809. wc = chan->pvt;
  2810. for (x = 0; x < wc->mods_per_board; x++) {
  2811. struct wcaxx_module *const mod = &wc->mods[x];
  2812. if (FXS == mod->type) {
  2813. mod->mod.fxs.idletxhookstate =
  2814. POLARITY_XOR(&mod->mod.fxs) ? SLIC_LF_ACTIVE_REV :
  2815. SLIC_LF_ACTIVE_FWD;
  2816. }
  2817. }
  2818. return 0;
  2819. }
  2820. static int wcaxx_hooksig(struct dahdi_chan *chan, enum dahdi_txsig txsig)
  2821. {
  2822. struct wcaxx *wc = chan->pvt;
  2823. struct wcaxx_module *const mod = &wc->mods[chan->chanpos - 1];
  2824. if (mod->type == FXO) {
  2825. switch (txsig) {
  2826. case DAHDI_TXSIG_START:
  2827. case DAHDI_TXSIG_OFFHOOK:
  2828. mod->mod.fxo.offhook = 1;
  2829. mod->sethook = CMD_WR(5, 0x9);
  2830. /* wcaxx_setreg(wc, chan->chanpos - 1, 5, 0x9); */
  2831. break;
  2832. case DAHDI_TXSIG_ONHOOK:
  2833. mod->mod.fxo.offhook = 0;
  2834. mod->sethook = CMD_WR(5, 0x8);
  2835. /* wcaxx_setreg(wc, chan->chanpos - 1, 5, 0x8); */
  2836. break;
  2837. default:
  2838. dev_notice(&wc->xb.pdev->dev,
  2839. "Can't set tx state to %d\n", txsig);
  2840. break;
  2841. }
  2842. } else if (mod->type == FXS) {
  2843. wcaxx_fxs_hooksig(wc, mod, txsig);
  2844. }
  2845. return 0;
  2846. }
  2847. static void wcaxx_dacs_connect(struct wcaxx *wc, int srccard, int dstcard)
  2848. {
  2849. struct wcaxx_module *const srcmod = &wc->mods[srccard];
  2850. struct wcaxx_module *const dstmod = &wc->mods[dstcard];
  2851. unsigned int type;
  2852. if (wc->mods[dstcard].dacssrc > -1) {
  2853. dev_notice(&wc->xb.pdev->dev, "wcaxx_dacs_connect: Can't have double sourcing yet!\n");
  2854. return;
  2855. }
  2856. type = wc->mods[srccard].type;
  2857. if ((type == FXS) || (type == FXO)) {
  2858. dev_notice(&wc->xb.pdev->dev,
  2859. "wcaxx_dacs_connect: Unsupported modtype for "
  2860. "card %d\n", srccard);
  2861. return;
  2862. }
  2863. type = wc->mods[dstcard].type;
  2864. if ((type != FXS) && (type != FXO)) {
  2865. dev_notice(&wc->xb.pdev->dev,
  2866. "wcaxx_dacs_connect: Unsupported modtype "
  2867. "for card %d\n", dstcard);
  2868. return;
  2869. }
  2870. if (debug) {
  2871. dev_info(&wc->xb.pdev->dev,
  2872. "connect %d => %d\n", srccard, dstcard);
  2873. }
  2874. dstmod->dacssrc = srccard;
  2875. /* make srccard transmit to srccard+24 on the TDM bus */
  2876. if (srcmod->type == FXS) {
  2877. /* proslic */
  2878. wcaxx_setreg(wc, srcmod, PCM_XMIT_START_COUNT_LSB,
  2879. ((srccard+24) * 8) & 0xff);
  2880. wcaxx_setreg(wc, srcmod, PCM_XMIT_START_COUNT_MSB,
  2881. ((srccard+24) * 8) >> 8);
  2882. } else if (srcmod->type == FXO) {
  2883. /* daa TX */
  2884. wcaxx_setreg(wc, srcmod, 34, ((srccard+24) * 8) & 0xff);
  2885. wcaxx_setreg(wc, srcmod, 35, ((srccard+24) * 8) >> 8);
  2886. }
  2887. /* have dstcard receive from srccard+24 on the TDM bus */
  2888. if (dstmod->type == FXS) {
  2889. /* proslic */
  2890. wcaxx_setreg(wc, dstmod, PCM_RCV_START_COUNT_LSB,
  2891. ((srccard+24) * 8) & 0xff);
  2892. wcaxx_setreg(wc, dstmod, PCM_RCV_START_COUNT_MSB,
  2893. ((srccard+24) * 8) >> 8);
  2894. } else if (dstmod->type == FXO) {
  2895. /* daa RX */
  2896. wcaxx_setreg(wc, dstmod, 36, ((srccard+24) * 8) & 0xff);
  2897. wcaxx_setreg(wc, dstmod, 37, ((srccard+24) * 8) >> 8);
  2898. }
  2899. }
  2900. static void wcaxx_dacs_disconnect(struct wcaxx *wc, int card)
  2901. {
  2902. struct wcaxx_module *const mod = &wc->mods[card];
  2903. struct wcaxx_module *dacssrc;
  2904. if (mod->dacssrc <= -1)
  2905. return;
  2906. dacssrc = &wc->mods[mod->dacssrc];
  2907. if (debug) {
  2908. dev_info(&wc->xb.pdev->dev,
  2909. "wcaxx_dacs_disconnect: restoring TX for %d and RX for %d\n",
  2910. mod->dacssrc, card);
  2911. }
  2912. /* restore TX (source card) */
  2913. if (dacssrc->type == FXS) {
  2914. wcaxx_setreg(wc, dacssrc, PCM_XMIT_START_COUNT_LSB,
  2915. (mod->dacssrc * 8) & 0xff);
  2916. wcaxx_setreg(wc, dacssrc, PCM_XMIT_START_COUNT_MSB,
  2917. (mod->dacssrc * 8) >> 8);
  2918. } else if (dacssrc->type == FXO) {
  2919. wcaxx_setreg(wc, mod, 34, (card * 8) & 0xff);
  2920. wcaxx_setreg(wc, mod, 35, (card * 8) >> 8);
  2921. } else {
  2922. dev_warn(&wc->xb.pdev->dev,
  2923. "WARNING: wcaxx_dacs_disconnect() called "
  2924. "on unsupported modtype\n");
  2925. }
  2926. /* restore RX (this card) */
  2927. if (FXS == mod->type) {
  2928. wcaxx_setreg(wc, mod, PCM_RCV_START_COUNT_LSB,
  2929. (card * 8) & 0xff);
  2930. wcaxx_setreg(wc, mod, PCM_RCV_START_COUNT_MSB,
  2931. (card * 8) >> 8);
  2932. } else if (FXO == mod->type) {
  2933. wcaxx_setreg(wc, mod, 36, (card * 8) & 0xff);
  2934. wcaxx_setreg(wc, mod, 37, (card * 8) >> 8);
  2935. } else {
  2936. dev_warn(&wc->xb.pdev->dev,
  2937. "WARNING: wcaxx_dacs_disconnect() called "
  2938. "on unsupported modtype\n");
  2939. }
  2940. mod->dacssrc = -1;
  2941. }
  2942. static int wcaxx_dacs(struct dahdi_chan *dst, struct dahdi_chan *src)
  2943. {
  2944. struct wcaxx *wc;
  2945. if (!nativebridge)
  2946. return 0; /* should this return -1 since unsuccessful? */
  2947. wc = dst->pvt;
  2948. if (src) {
  2949. wcaxx_dacs_connect(wc, src->chanpos - 1, dst->chanpos - 1);
  2950. if (debug) {
  2951. dev_info(&wc->xb.pdev->dev,
  2952. "dacs connecct: %d -> %d!\n\n",
  2953. src->chanpos, dst->chanpos);
  2954. }
  2955. } else {
  2956. wcaxx_dacs_disconnect(wc, dst->chanpos - 1);
  2957. if (debug) {
  2958. dev_info(&wc->xb.pdev->dev,
  2959. "dacs disconnect: %d!\n", dst->chanpos);
  2960. }
  2961. }
  2962. return 0;
  2963. }
  2964. /**
  2965. * wcaxx_chanconfig - Called when the channels are being configured.
  2966. *
  2967. * Ensure that the card is completely ready to go before we allow the channels
  2968. * to be completely configured. This is to allow lengthy initialization
  2969. * actions to take place in background on driver load and ensure we're synced
  2970. * up by the time dahdi_cfg is run.
  2971. *
  2972. */
  2973. static int
  2974. wcaxx_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
  2975. {
  2976. struct wcaxx *wc = chan->pvt;
  2977. if ((file->f_flags & O_NONBLOCK) && !is_initialized(wc))
  2978. return -EAGAIN;
  2979. return 0;
  2980. }
  2981. /*
  2982. * wcaxx_assigned - Called when span is assigned.
  2983. * @span: The span that is now assigned.
  2984. *
  2985. * This function is called by the core of DAHDI after the span number and
  2986. * channel numbers have been assigned.
  2987. *
  2988. */
  2989. static void wcaxx_assigned(struct dahdi_span *span)
  2990. {
  2991. struct dahdi_span *s;
  2992. struct dahdi_device *ddev = span->parent;
  2993. struct wcaxx *wc = NULL;
  2994. list_for_each_entry(s, &ddev->spans, device_node) {
  2995. wc = container_of(s, struct wcaxx, span);
  2996. if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &s->flags))
  2997. return;
  2998. }
  2999. }
  3000. static const struct dahdi_span_ops wcaxx_span_ops = {
  3001. .owner = THIS_MODULE,
  3002. .hooksig = wcaxx_hooksig,
  3003. .open = wcaxx_open,
  3004. .close = wcaxx_close,
  3005. .ioctl = wcaxx_ioctl,
  3006. .watchdog = wcaxx_watchdog,
  3007. .chanconfig = wcaxx_chanconfig,
  3008. .dacs = wcaxx_dacs,
  3009. .assigned = wcaxx_assigned,
  3010. .echocan_create = wcaxx_echocan_create,
  3011. .echocan_name = wcaxx_echocan_name,
  3012. };
  3013. static struct wcaxx_chan *
  3014. wcaxx_init_chan(struct wcaxx *wc, struct dahdi_span *s, int channo)
  3015. {
  3016. struct wcaxx_chan *c;
  3017. c = kzalloc(sizeof(*c), GFP_KERNEL);
  3018. if (!c)
  3019. return NULL;
  3020. snprintf(c->chan.name, sizeof(c->chan.name), "WCTDM/%d/%d",
  3021. wc->num, channo);
  3022. c->chan.chanpos = channo+1;
  3023. c->chan.span = s;
  3024. c->chan.pvt = wc;
  3025. c->timeslot = channo;
  3026. return c;
  3027. }
  3028. static void wcaxx_init_span(struct wcaxx *wc)
  3029. {
  3030. int x;
  3031. struct wcaxx_chan *c;
  3032. struct dahdi_echocan_state *ec[NUM_MODULES] = {NULL, };
  3033. /* DAHDI stuff */
  3034. wc->span.offset = 0;
  3035. sprintf(wc->span.name, "WCTDM/%d", wc->num);
  3036. snprintf(wc->span.desc, sizeof(wc->span.desc) - 1,
  3037. "%s", wc->desc->name);
  3038. if (wc->companding == DAHDI_LAW_DEFAULT) {
  3039. wc->span.deflaw = DAHDI_LAW_MULAW;
  3040. } else if (wc->companding == DAHDI_LAW_ALAW) {
  3041. /* Force everything to alaw */
  3042. wc->span.deflaw = DAHDI_LAW_ALAW;
  3043. } else {
  3044. /* Auto set to ulaw */
  3045. wc->span.deflaw = DAHDI_LAW_MULAW;
  3046. }
  3047. wc->span.ops = &wcaxx_span_ops;
  3048. wc->span.flags = DAHDI_FLAG_RBS;
  3049. wc->span.spantype = SPANTYPE_ANALOG_MIXED;
  3050. wc->span.chans = kmalloc(sizeof(wc->span.chans[0]) * wc->desc->ports,
  3051. GFP_KERNEL);
  3052. if (!wc->span.chans)
  3053. return;
  3054. /* allocate channels for the span */
  3055. for (x = 0; x < wc->desc->ports; x++) {
  3056. c = wcaxx_init_chan(wc, &wc->span, x);
  3057. if (!c)
  3058. return;
  3059. wc->chans[x] = c;
  3060. wc->span.chans[x] = &c->chan;
  3061. /* TODO: Should echocan state hide under VPM_ENABLED or does
  3062. * software ec use it? */
  3063. ec[x] = kzalloc(sizeof(*ec[x]), GFP_KERNEL);
  3064. }
  3065. wc->span.channels = wc->desc->ports;
  3066. memcpy(wc->ec, ec, sizeof(wc->ec));
  3067. memset(ec, 0, sizeof(ec));
  3068. }
  3069. /**
  3070. * should_set_alaw() - Should be called after all the spans are initialized.
  3071. *
  3072. * Returns true if the module companding should be set to alaw, otherwise
  3073. * false.
  3074. */
  3075. static bool should_set_alaw(const struct wcaxx *wc)
  3076. {
  3077. if (DAHDI_LAW_ALAW == wc->companding)
  3078. return true;
  3079. else
  3080. return false;
  3081. }
  3082. static void wcaxx_fixup_span(struct wcaxx *wc)
  3083. {
  3084. struct dahdi_span *s;
  3085. int x, y;
  3086. y = 0;
  3087. s = &wc->span;
  3088. for (x = 0; x < wc->desc->ports; x++) {
  3089. struct wcaxx_module *const mod = &wc->mods[x];
  3090. if (debug) {
  3091. dev_info(&wc->xb.pdev->dev,
  3092. "fixup_analog: x=%d, y=%d modtype=%d, "
  3093. "s->chans[%d]=%p\n", x, y, mod->type,
  3094. y, s->chans[y]);
  3095. }
  3096. if (mod->type == FXO) {
  3097. int val;
  3098. s->chans[y++]->sigcap = DAHDI_SIG_FXSKS |
  3099. DAHDI_SIG_FXSLS | DAHDI_SIG_SF |
  3100. DAHDI_SIG_CLEAR;
  3101. val = should_set_alaw(wc) ? 0x20 : 0x28;
  3102. #ifdef DEBUG
  3103. val = (digitalloopback) ? 0x30 : val;
  3104. #endif
  3105. wcaxx_setreg(wc, mod, 33, val);
  3106. wcaxx_voicedaa_set_ts(wc, mod, wc->chans[x]->timeslot);
  3107. } else if (mod->type == FXS) {
  3108. /* NOTE: Digital loopback does not work on the FXS
  3109. * modules in the same way since the data is still
  3110. * companded by the ProSLIC and doesn't appear to have
  3111. * perfect symetry. */
  3112. s->chans[y++]->sigcap = DAHDI_SIG_FXOKS |
  3113. DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS |
  3114. DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
  3115. wcaxx_setreg(wc, mod, 1,
  3116. (should_set_alaw(wc) ? 0x20 : 0x28));
  3117. wcaxx_proslic_set_ts(wc, mod, wc->chans[x]->timeslot);
  3118. } else {
  3119. s->chans[y++]->sigcap = 0;
  3120. }
  3121. }
  3122. }
  3123. static bool wcaxx_init_fxs_port(struct wcaxx *wc, struct wcaxx_module *mod)
  3124. {
  3125. u8 readi;
  3126. enum {UNKNOWN = 0, SANE = 1};
  3127. int ret = wcaxx_init_proslic(wc, mod, 0, 0, UNKNOWN);
  3128. if (!ret) {
  3129. if (debug & DEBUG_CARD) {
  3130. readi = wcaxx_getreg(wc, mod, LOOP_I_LIMIT);
  3131. dev_info(&wc->xb.pdev->dev,
  3132. "Proslic module %d loop current is %dmA\n",
  3133. mod->card, ((readi*3) + 20));
  3134. }
  3135. return true;
  3136. }
  3137. if (ret != -2) {
  3138. /* Init with Manual Calibration */
  3139. if (!wcaxx_init_proslic(wc, mod, 0, 1, SANE)) {
  3140. if (debug & DEBUG_CARD) {
  3141. readi = wcaxx_getreg(wc, mod, LOOP_I_LIMIT);
  3142. dev_info(&wc->xb.pdev->dev,
  3143. "Proslic module %d loop current is %dmA\n",
  3144. mod->card, ((readi*3)+20));
  3145. }
  3146. } else {
  3147. dev_notice(&wc->xb.pdev->dev,
  3148. "Port %d: FAILED FXS (%s)\n", mod->card + 1,
  3149. fxshonormode ? fxo_modes[_opermode].name :
  3150. "FCC");
  3151. }
  3152. return true;
  3153. }
  3154. return false;
  3155. }
  3156. static void wcaxx_reset_module(struct wcaxx *wc, struct wcaxx_module *mod)
  3157. {
  3158. u32 reg_val = (1UL << (mod->spi->chip_select + 12));
  3159. wcxb_gpio_clear(&wc->xb, reg_val);
  3160. udelay(500);
  3161. wcxb_gpio_set(&wc->xb, reg_val);
  3162. msleep(250); /* TODO: What should this value be? */
  3163. }
  3164. static bool check_for_single_fxs(struct wcaxx *wc, unsigned int port)
  3165. {
  3166. bool result;
  3167. struct wcaxx_module *mod = &wc->mods[port];
  3168. mod->spi = get_spi_device_for_port(wc, mod->card, false);
  3169. mod->subaddr = 0;
  3170. wcaxx_reset_module(wc, mod);
  3171. wcaxx_fxsinit(mod->spi);
  3172. result = wcaxx_init_fxs_port(wc, mod);
  3173. if (!result)
  3174. mod->type = NONE;
  3175. /* It is currently unclear why this read is necessary for some of the
  3176. * S100M modules to properly function. */
  3177. wcaxx_getreg(wc, mod, 0x00);
  3178. return result;
  3179. }
  3180. static bool check_for_single_fxo(struct wcaxx *wc, unsigned int port)
  3181. {
  3182. bool result;
  3183. struct wcaxx_module *mod = &wc->mods[port];
  3184. mod->spi = get_spi_device_for_port(wc, mod->card, false);
  3185. mod->subaddr = 0;
  3186. wcaxx_reset_module(wc, mod);
  3187. result = (wcaxx_init_voicedaa(wc, mod, 0, 0, 0) == 0);
  3188. if (!result)
  3189. mod->type = NONE;
  3190. return result;
  3191. }
  3192. static bool check_for_quad_fxs(struct wcaxx *wc, unsigned int base_port)
  3193. {
  3194. int port;
  3195. int offset;
  3196. struct wcaxx_module *mod = &wc->mods[base_port + 1];
  3197. /* Cannot have quad port modules on the 4 port base cards. */
  3198. if (is_four_port(wc))
  3199. return false;
  3200. /* We can assume that the base port has already been configured as an
  3201. * FXS port if we're even in this function */
  3202. mod->spi = get_spi_device_for_port(wc, mod->card, true);
  3203. mod->subaddr = offset = 1;
  3204. if (wcaxx_init_fxs_port(wc, mod)) {
  3205. /* This must be a 4 port FXS module... */
  3206. for (port = base_port + 2; port < base_port+4; ++port) {
  3207. mod = &wc->mods[port];
  3208. mod->spi = get_spi_device_for_port(wc, mod->card, true);
  3209. mod->subaddr = ++offset;
  3210. if (!wcaxx_init_fxs_port(wc, mod)) {
  3211. /* This means that a quad-module failed to
  3212. * setup ports 3 or 4? */
  3213. dev_err(&wc->xb.pdev->dev,
  3214. "Quad-FXS at base %d failed initialization.\n",
  3215. base_port);
  3216. goto error_exit;
  3217. }
  3218. }
  3219. return true;
  3220. }
  3221. error_exit:
  3222. for (port = base_port + 1; port < base_port + 4; ++port) {
  3223. mod = &wc->mods[port];
  3224. mod->type = NONE;
  3225. }
  3226. return false;
  3227. }
  3228. static bool check_for_quad_fxo(struct wcaxx *wc, unsigned int base_port)
  3229. {
  3230. int port;
  3231. int offset;
  3232. struct wcaxx_module *mod = &wc->mods[base_port + 1];
  3233. /* Cannot have quad port modules on the 4 port base cards. */
  3234. if (is_four_port(wc))
  3235. return false;
  3236. /* We can assume that the base port has already been configured as an
  3237. * FXO port if we're even in this function */
  3238. mod->spi = get_spi_device_for_port(wc, mod->card, true);
  3239. mod->subaddr = offset = 1;
  3240. if (!wcaxx_init_voicedaa(wc, mod, 0, 0, 0)) {
  3241. /* This must be a 4 port FXO module. */
  3242. for (port = base_port + 2; port < base_port + 4; ++port) {
  3243. mod = &wc->mods[port];
  3244. mod->spi = get_spi_device_for_port(wc, mod->card, true);
  3245. mod->subaddr = ++offset;
  3246. if (wcaxx_init_voicedaa(wc, mod, 0, 0, 0)) {
  3247. dev_err(&wc->xb.pdev->dev,
  3248. "Quad-FXO at base %d failed initialization.\n",
  3249. base_port);
  3250. goto error_exit;
  3251. }
  3252. }
  3253. return true;
  3254. }
  3255. error_exit:
  3256. for (port = base_port + 1; port < base_port + 4; ++port) {
  3257. mod = &wc->mods[port];
  3258. mod->type = NONE;
  3259. }
  3260. return false;
  3261. }
  3262. static void __wcaxx_identify_four_port_module_group(struct wcaxx *wc)
  3263. {
  3264. int i;
  3265. for (i = 0; i < wc->desc->ports; i++) {
  3266. if (!check_for_single_fxs(wc, i))
  3267. check_for_single_fxo(wc, i);
  3268. }
  3269. return;
  3270. }
  3271. static void
  3272. __wcaxx_identify_module_group(struct wcaxx *wc, unsigned long base)
  3273. {
  3274. if (check_for_single_fxs(wc, base)) {
  3275. if (check_for_quad_fxs(wc, base)) {
  3276. /* S400M installed */
  3277. return;
  3278. } else if (check_for_single_fxs(wc, base + 1)) {
  3279. /* Two S110M installed */
  3280. return;
  3281. } else if (check_for_single_fxo(wc, base + 1)) {
  3282. /* 1 S110M 1 X100M */
  3283. return;
  3284. } else {
  3285. /* 1 S110M 1 Empty */
  3286. return;
  3287. }
  3288. } else if (check_for_single_fxo(wc, base)) {
  3289. if (check_for_quad_fxo(wc, base)) {
  3290. /* X400M installed */
  3291. return;
  3292. } else if (check_for_single_fxo(wc, base + 1)) {
  3293. /* Two X100M installed */
  3294. return;
  3295. } else if (check_for_single_fxs(wc, base + 1)) {
  3296. /* 1 X100M 1 S100M installed */
  3297. return;
  3298. } else {
  3299. /* 1 X100M 1 Empty */
  3300. return;
  3301. }
  3302. } else if (check_for_single_fxs(wc, base + 1)) {
  3303. /* 1 Empty 1 S110M installed */
  3304. return;
  3305. } else if (check_for_single_fxo(wc, base + 1)) {
  3306. /* 1 Empty 1 X100M installed */
  3307. return;
  3308. }
  3309. /* No module */
  3310. return;
  3311. }
  3312. /**
  3313. * wcaxx_print_moule_configuration - Print the configuration to the kernel log
  3314. * @wc: The card we're interested in.
  3315. *
  3316. * This is to ensure that the module configuration from each card shows up
  3317. * sequentially in the kernel log, as opposed to interleaved with one another.
  3318. *
  3319. */
  3320. static void wcaxx_print_module_configuration(const struct wcaxx *const wc)
  3321. {
  3322. int i;
  3323. static DEFINE_MUTEX(print);
  3324. mutex_lock(&print);
  3325. for (i = 0; i < wc->mods_per_board; ++i) {
  3326. const struct wcaxx_module *const mod = &wc->mods[i];
  3327. switch (mod->type) {
  3328. case FXO:
  3329. dev_info(&wc->xb.pdev->dev,
  3330. "Port %d: Installed -- AUTO FXO (%s mode)\n",
  3331. i + 1, fxo_modes[_opermode].name);
  3332. break;
  3333. case FXS:
  3334. dev_info(&wc->xb.pdev->dev,
  3335. "Port %d: Installed -- AUTO FXS/DPO\n", i + 1);
  3336. break;
  3337. case NONE:
  3338. dev_info(&wc->xb.pdev->dev,
  3339. "Port %d: Not installed\n", i + 1);
  3340. break;
  3341. }
  3342. }
  3343. mutex_unlock(&print);
  3344. }
  3345. static void wcaxx_identify_modules(struct wcaxx *wc)
  3346. {
  3347. int x;
  3348. unsigned long flags;
  3349. /* A8A/A8B - Reset the modules. */
  3350. wcxb_gpio_clear(&wc->xb, 0xf000);
  3351. msleep(50); /* TODO: what should these values be? */
  3352. wcxb_gpio_set(&wc->xb, 0xf000);
  3353. msleep(250); /* TODO: What should these values be? */
  3354. /* Place all units in the daisy chain mode of operation. This allows
  3355. * multiple devices to share a chip select (like on the X400 and S400
  3356. * modules) */
  3357. for (x = 0; x < ARRAY_SIZE(wc->spi_devices); ++x)
  3358. wcaxx_fxsinit(wc->spi_devices[x]);
  3359. spin_lock_irqsave(&wc->reglock, flags);
  3360. wc->mods_per_board = wc->desc->ports;
  3361. spin_unlock_irqrestore(&wc->reglock, flags);
  3362. BUG_ON(wc->desc->ports % 4);
  3363. if (is_four_port(wc)) {
  3364. __wcaxx_identify_four_port_module_group(wc);
  3365. } else {
  3366. for (x = 0; x < wc->desc->ports/4; x++)
  3367. __wcaxx_identify_module_group(wc, x*4);
  3368. }
  3369. wcaxx_print_module_configuration(wc);
  3370. }
  3371. static struct pci_driver wcaxx_driver;
  3372. static void wcaxx_back_out_gracefully(struct wcaxx *wc)
  3373. {
  3374. int i;
  3375. unsigned long flags;
  3376. clear_bit(INITIALIZED, &wc->bit_flags);
  3377. smp_mb__after_atomic();
  3378. /* Make sure we're not on the card list anymore. */
  3379. mutex_lock(&card_list_lock);
  3380. list_del(&wc->card_node);
  3381. mutex_unlock(&card_list_lock);
  3382. wcxb_release(&wc->xb);
  3383. for (i = 0; i < wc->mods_per_board; i++) {
  3384. struct wcaxx_module *const mod = &wc->mods[i];
  3385. kfree(mod->mod_poll);
  3386. mod->mod_poll = NULL;
  3387. }
  3388. kfree(wc->span.chans);
  3389. wc->span.chans = NULL;
  3390. spin_lock_irqsave(&wc->reglock, flags);
  3391. for (i = 0; i < wc->span.channels; ++i) {
  3392. kfree(wc->chans[i]);
  3393. kfree(wc->ec[i]);
  3394. wc->chans[i] = NULL;
  3395. wc->ec[i] = NULL;
  3396. }
  3397. spin_unlock_irqrestore(&wc->reglock, flags);
  3398. for (i = 0; i < ARRAY_SIZE(wc->spi_devices); i++)
  3399. wcxb_spi_device_destroy(wc->spi_devices[i]);
  3400. wcxb_spi_master_destroy(wc->master);
  3401. kfree(wc->board_name);
  3402. if (wc->ddev) {
  3403. kfree(wc->ddev->devicetype);
  3404. kfree(wc->ddev->location);
  3405. kfree(wc->ddev->hardware_id);
  3406. dahdi_free_device(wc->ddev);
  3407. }
  3408. kfree(wc);
  3409. }
  3410. static void wcaxx_handle_error(struct wcxb *xb)
  3411. {
  3412. struct wcaxx *wc = container_of(xb, struct wcaxx, xb);
  3413. wc->ddev->irqmisses++;
  3414. }
  3415. static const struct wcxb_operations wcxb_operations = {
  3416. .handle_receive = wcaxx_handle_receive,
  3417. .handle_transmit = wcaxx_handle_transmit,
  3418. .handle_error = wcaxx_handle_error,
  3419. };
  3420. struct cmd_results {
  3421. u8 results[8];
  3422. };
  3423. static int wcaxx_check_firmware(struct wcaxx *wc)
  3424. {
  3425. char *filename;
  3426. u32 firmware_version;
  3427. const bool force_firmware = false;
  3428. const unsigned int A4A_VERSION = 0x0a0017;
  3429. const unsigned int A4B_VERSION = 0x0d001e;
  3430. const unsigned int A8A_VERSION = 0x1d0017;
  3431. const unsigned int A8B_VERSION = 0x1f001e;
  3432. if (wc->desc == &device_a8a) {
  3433. firmware_version = A8A_VERSION;
  3434. filename = "dahdi-fw-a8a.bin";
  3435. } else if (wc->desc == &device_a8b) {
  3436. firmware_version = A8B_VERSION;
  3437. filename = "dahdi-fw-a8b.bin";
  3438. } else if (wc->desc == &device_a4a) {
  3439. firmware_version = A4A_VERSION;
  3440. filename = "dahdi-fw-a4a.bin";
  3441. } else if (wc->desc == &device_a4b) {
  3442. firmware_version = A4B_VERSION;
  3443. filename = "dahdi-fw-a4b.bin";
  3444. } else {
  3445. /* This is a bug in the driver code */
  3446. WARN_ON(1);
  3447. return 0;
  3448. }
  3449. return wcxb_check_firmware(&wc->xb, firmware_version,
  3450. filename, force_firmware, WCXB_RESET_NOW);
  3451. }
  3452. static void wcaxx_check_sethook(struct wcaxx *wc, struct wcaxx_module *mod)
  3453. {
  3454. if (mod->sethook) {
  3455. wcaxx_setreg(wc, mod, ((mod->sethook >> 8) & 0xff),
  3456. mod->sethook & 0xff);
  3457. mod->sethook = 0;
  3458. }
  3459. }
  3460. static void wcaxx_poll_fxs_complete(void *arg)
  3461. {
  3462. struct wcaxx_mod_poll *poll_fxs = arg;
  3463. struct wcaxx *wc = poll_fxs->wc;
  3464. struct wcaxx_module *const mod = poll_fxs->mod;
  3465. if (!is_initialized(wc)) {
  3466. kfree(poll_fxs);
  3467. return;
  3468. }
  3469. mod->mod.fxs.hook_state_shadow = poll_fxs->buffer[2];
  3470. mod->mod.fxs.linefeed_control_shadow = poll_fxs->buffer[5];
  3471. wcaxx_isr_misc_fxs(poll_fxs->wc, poll_fxs->mod);
  3472. memcpy(poll_fxs->buffer, poll_fxs->master_buffer,
  3473. sizeof(poll_fxs->buffer));
  3474. wcaxx_check_sethook(poll_fxs->wc, poll_fxs->mod);
  3475. mod->mod_poll = poll_fxs;
  3476. }
  3477. /**
  3478. * wcaxx_start_poll_fxs - Starts the interrupt polling loop for FXS modules.
  3479. *
  3480. * To stop the polling loop, clear the initialized bit and then flush the
  3481. * pending wcxb_spi messages.
  3482. *
  3483. */
  3484. static int wcaxx_start_poll_fxs(struct wcaxx *wc, struct wcaxx_module *mod)
  3485. {
  3486. struct wcaxx_mod_poll *mod_poll = kzalloc(sizeof(*mod_poll),
  3487. GFP_KERNEL);
  3488. struct wcxb_spi_message *m = &mod_poll->m;
  3489. struct wcxb_spi_transfer *t = &mod_poll->t;
  3490. WARN_ON(!is_initialized(wc));
  3491. if (!mod_poll)
  3492. return -ENOMEM;
  3493. memset(t, 0, sizeof(*t));
  3494. wcxb_spi_message_init(m);
  3495. t->tx_buf = t->rx_buf = mod_poll->buffer;
  3496. t->len = sizeof(mod_poll->buffer);
  3497. wcxb_spi_message_add_tail(t, m);
  3498. mod_poll->wc = wc;
  3499. mod_poll->mod = mod;
  3500. mod_poll->master_buffer[0] = 1 << mod_poll->mod->subaddr;
  3501. mod_poll->master_buffer[1] = (LOOP_STAT | 0x80) & 0xff;
  3502. mod_poll->master_buffer[2] = 0;
  3503. mod_poll->master_buffer[3] = mod_poll->master_buffer[0];
  3504. mod_poll->master_buffer[4] = (LINE_STATE | 0x80) & 0xff;
  3505. mod_poll->master_buffer[5] = 0;
  3506. memcpy(mod_poll->buffer, mod_poll->master_buffer,
  3507. sizeof(mod_poll->buffer));
  3508. m->arg = mod_poll;
  3509. m->complete = &wcaxx_poll_fxs_complete;
  3510. wcxb_spi_async(mod->spi, m);
  3511. return 0;
  3512. }
  3513. static void wcaxx_poll_fxo_complete(void *arg)
  3514. {
  3515. struct wcaxx_mod_poll *poll_fxo = arg;
  3516. struct wcaxx *wc = poll_fxo->wc;
  3517. struct wcaxx_module *const mod = poll_fxo->mod;
  3518. if (!is_initialized(wc)) {
  3519. kfree(poll_fxo);
  3520. return;
  3521. }
  3522. mod->mod.fxo.hook_ring_shadow = poll_fxo->buffer[2];
  3523. mod->mod.fxo.line_voltage_status = poll_fxo->buffer[5];
  3524. wcaxx_voicedaa_check_hook(poll_fxo->wc, poll_fxo->mod);
  3525. memcpy(poll_fxo->buffer, poll_fxo->master_buffer,
  3526. sizeof(poll_fxo->buffer));
  3527. wcaxx_check_sethook(poll_fxo->wc, poll_fxo->mod);
  3528. mod->mod_poll = poll_fxo;
  3529. }
  3530. /**
  3531. * wcaxx_start_poll_fxo - Starts the interrupt polling loop for FXS modules.
  3532. *
  3533. * To stop the polling loop, clear the initialized bit and then flush the
  3534. * pending wcxb_spi messages.
  3535. *
  3536. */
  3537. static int wcaxx_start_poll_fxo(struct wcaxx *wc, struct wcaxx_module *mod)
  3538. {
  3539. static const int ADDRS[4] = {0x00, 0x08, 0x04, 0x0c};
  3540. struct wcaxx_mod_poll *poll_fxo = kzalloc(sizeof(*poll_fxo),
  3541. GFP_KERNEL);
  3542. struct wcxb_spi_message *m = &poll_fxo->m;
  3543. struct wcxb_spi_transfer *t = &poll_fxo->t;
  3544. WARN_ON(!is_initialized(wc));
  3545. if (!poll_fxo)
  3546. return -ENOMEM;
  3547. memset(t, 0, sizeof(*t));
  3548. wcxb_spi_message_init(m);
  3549. t->tx_buf = t->rx_buf = poll_fxo->buffer;
  3550. t->len = sizeof(poll_fxo->buffer);
  3551. wcxb_spi_message_add_tail(t, m);
  3552. poll_fxo->wc = wc;
  3553. poll_fxo->mod = mod;
  3554. poll_fxo->master_buffer[0] = 0x60 | ADDRS[poll_fxo->mod->subaddr];
  3555. poll_fxo->master_buffer[1] = 5 & 0x7f; /* Hook / Ring State */
  3556. poll_fxo->master_buffer[2] = 0;
  3557. poll_fxo->master_buffer[3] = poll_fxo->master_buffer[0];
  3558. poll_fxo->master_buffer[4] = 29 & 0x7f; /* Battery */
  3559. poll_fxo->master_buffer[5] = 0;
  3560. memcpy(poll_fxo->buffer, poll_fxo->master_buffer,
  3561. sizeof(poll_fxo->buffer));
  3562. m->arg = poll_fxo;
  3563. m->complete = &wcaxx_poll_fxo_complete;
  3564. wcxb_spi_async(mod->spi, m);
  3565. return 0;
  3566. }
  3567. /**
  3568. * wcaxx_read_serial - Returns the serial number of the board.
  3569. * @wc: The board whos serial number we are reading.
  3570. *
  3571. * The buffer returned is dynamically allocated and must be kfree'd by the
  3572. * caller. If memory could not be allocated, NULL is returned.
  3573. *
  3574. * Must be called in process context.
  3575. *
  3576. */
  3577. static char *wcaxx_read_serial(struct wcaxx *wc)
  3578. {
  3579. int i;
  3580. static const int MAX_SERIAL = 20*5;
  3581. const unsigned int SERIAL_ADDRESS = 0x1f0000;
  3582. unsigned char *serial = kzalloc(MAX_SERIAL + 1, GFP_KERNEL);
  3583. struct wcxb const *xb = &wc->xb;
  3584. struct wcxb_spi_master *flash_spi_master = NULL;
  3585. struct wcxb_spi_device *flash_spi_device = NULL;
  3586. const unsigned int FLASH_SPI_BASE = 0x200;
  3587. if (!serial)
  3588. return NULL;
  3589. flash_spi_master = wcxb_spi_master_create(&xb->pdev->dev,
  3590. xb->membase + FLASH_SPI_BASE,
  3591. false);
  3592. if (!flash_spi_master)
  3593. return NULL;
  3594. flash_spi_device = wcxb_spi_device_create(flash_spi_master, 0);
  3595. if (!flash_spi_device)
  3596. goto error_exit;
  3597. wcxb_flash_read(flash_spi_device, SERIAL_ADDRESS,
  3598. serial, MAX_SERIAL);
  3599. for (i = 0; i < MAX_SERIAL; ++i) {
  3600. if ((serial[i] < 0x20) || (serial[i] > 0x7e)) {
  3601. serial[i] = '\0';
  3602. break;
  3603. }
  3604. }
  3605. if (!i) {
  3606. kfree(serial);
  3607. serial = NULL;
  3608. } else {
  3609. /* Limit the size of the buffer to just what is needed to
  3610. * actually hold the serial number. */
  3611. unsigned char *new_serial;
  3612. new_serial = kasprintf(GFP_KERNEL, "%s", serial);
  3613. kfree(serial);
  3614. serial = new_serial;
  3615. }
  3616. error_exit:
  3617. wcxb_spi_device_destroy(flash_spi_device);
  3618. wcxb_spi_master_destroy(flash_spi_master);
  3619. return serial;
  3620. }
  3621. static void wcaxx_start_module_polling(struct wcaxx *wc)
  3622. {
  3623. int x;
  3624. WARN_ON(!is_initialized(wc));
  3625. for (x = 0; x < wc->mods_per_board; x++) {
  3626. struct wcaxx_module *const mod = &wc->mods[x];
  3627. switch (mod->type) {
  3628. case FXO:
  3629. wcaxx_start_poll_fxo(wc, mod);
  3630. break;
  3631. case FXS:
  3632. wcaxx_start_poll_fxs(wc, mod);
  3633. break;
  3634. case NONE:
  3635. break;
  3636. }
  3637. }
  3638. wc->module_poll_time = wc->framecount + MODULE_POLL_TIME_MS;
  3639. }
  3640. /**
  3641. * t43x_assign_num - Assign wc->num a unique value and place on card_list
  3642. *
  3643. */
  3644. static void wcaxx_assign_num(struct wcaxx *wc)
  3645. {
  3646. mutex_lock(&card_list_lock);
  3647. if (list_empty(&card_list)) {
  3648. wc->num = 0;
  3649. list_add(&wc->card_node, &card_list);
  3650. } else {
  3651. struct wcaxx *cur;
  3652. struct list_head *insert_pos;
  3653. int new_num = 0;
  3654. insert_pos = &card_list;
  3655. list_for_each_entry(cur, &card_list, card_node) {
  3656. if (new_num != cur->num)
  3657. break;
  3658. new_num++;
  3659. insert_pos = &cur->card_node;
  3660. }
  3661. wc->num = new_num;
  3662. list_add_tail(&wc->card_node, insert_pos);
  3663. }
  3664. mutex_unlock(&card_list_lock);
  3665. }
  3666. #ifdef USE_ASYNC_INIT
  3667. struct async_data {
  3668. struct pci_dev *pdev;
  3669. const struct pci_device_id *ent;
  3670. };
  3671. static int __devinit
  3672. __wcaxx_init_one(struct pci_dev *pdev, const struct pci_device_id *ent,
  3673. async_cookie_t cookie)
  3674. #else
  3675. static int __devinit
  3676. __wcaxx_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  3677. #endif
  3678. {
  3679. struct wcaxx *wc;
  3680. int i, ret;
  3681. int curchan;
  3682. neonmwi_offlimit_cycles = neonmwi_offlimit / MS_PER_HOOKCHECK;
  3683. wc = kzalloc(sizeof(*wc), GFP_KERNEL);
  3684. if (!wc)
  3685. return -ENOMEM;
  3686. wcaxx_assign_num(wc);
  3687. wc->desc = (struct _device_desc *)ent->driver_data;
  3688. spin_lock_init(&wc->reglock);
  3689. wc->board_name = kasprintf(GFP_KERNEL, "%s%d",
  3690. wcaxx_driver.name, wc->num);
  3691. if (!wc->board_name) {
  3692. wcaxx_back_out_gracefully(wc);
  3693. return -ENOMEM;
  3694. }
  3695. #ifdef CONFIG_VOICEBUS_DISABLE_ASPM
  3696. if (is_pcie(wc)) {
  3697. pci_disable_link_state(pdev->bus->self, PCIE_LINK_STATE_L0S |
  3698. PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
  3699. };
  3700. #endif
  3701. pci_set_drvdata(pdev, wc);
  3702. wc->xb.ops = &wcxb_operations;
  3703. wc->xb.pdev = pdev;
  3704. wc->xb.debug = &debug;
  3705. ret = wcxb_init(&wc->xb, wc->board_name, int_mode);
  3706. if (ret) {
  3707. wcaxx_back_out_gracefully(wc);
  3708. return ret;
  3709. }
  3710. wcxb_set_minlatency(&wc->xb, latency);
  3711. wcxb_set_maxlatency(&wc->xb, max_latency);
  3712. ret = wcaxx_check_firmware(wc);
  3713. if (ret) {
  3714. wcaxx_back_out_gracefully(wc);
  3715. return ret;
  3716. }
  3717. wcxb_lock_latency(&wc->xb);
  3718. wc->mods_per_board = NUM_MODULES;
  3719. if (alawoverride) {
  3720. companding = "alaw";
  3721. dev_info(&wc->xb.pdev->dev,
  3722. "The module parameter alawoverride has been deprecated. Please use the parameter companding=alaw instead");
  3723. }
  3724. if (!strcasecmp(companding, "alaw"))
  3725. /* Force this card's companding to alaw */
  3726. wc->companding = DAHDI_LAW_ALAW;
  3727. else if (!strcasecmp(companding, "ulaw"))
  3728. /* Force this card's companding to ulaw */
  3729. wc->companding = DAHDI_LAW_MULAW;
  3730. else
  3731. /* Auto detect this card's companding */
  3732. wc->companding = DAHDI_LAW_DEFAULT;
  3733. wc->master = wcxb_spi_master_create(&pdev->dev,
  3734. wc->xb.membase + 0x280, true);
  3735. for (i = 0; i < ARRAY_SIZE(wc->spi_devices); i++)
  3736. wc->spi_devices[i] = wcxb_spi_device_create(wc->master, 3-i);
  3737. for (i = 0; i < ARRAY_SIZE(wc->mods); i++) {
  3738. struct wcaxx_module *const mod = &wc->mods[i];
  3739. mod->dacssrc = -1;
  3740. mod->card = i;
  3741. mod->spi = NULL;
  3742. mod->subaddr = 0;
  3743. mod->type = NONE;
  3744. }
  3745. ret = wcaxx_vpm_init(wc);
  3746. if (!ret)
  3747. wcxb_enable_echocan(&wc->xb);
  3748. /* Now track down what modules are installed */
  3749. wcaxx_identify_modules(wc);
  3750. /* Start the hardware processing. */
  3751. if (wcxb_start(&wc->xb)) {
  3752. WARN_ON(1);
  3753. return -EIO;
  3754. }
  3755. if (fatal_signal_pending(current)) {
  3756. wcaxx_back_out_gracefully(wc);
  3757. return -EINTR;
  3758. }
  3759. curchan = 0;
  3760. wcaxx_init_span(wc);
  3761. wcaxx_fixup_span(wc);
  3762. curchan += wc->desc->ports;
  3763. #ifdef USE_ASYNC_INIT
  3764. async_synchronize_cookie(cookie);
  3765. #endif
  3766. wc->ddev = dahdi_create_device();
  3767. if (!wc->ddev) {
  3768. wcaxx_back_out_gracefully(wc);
  3769. return -ENOMEM;
  3770. }
  3771. wc->ddev->manufacturer = "Digium";
  3772. wc->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
  3773. pdev->bus->number,
  3774. PCI_SLOT(pdev->devfn) + 1);
  3775. if (!wc->ddev->location) {
  3776. wcaxx_back_out_gracefully(wc);
  3777. return -ENOMEM;
  3778. }
  3779. if (wc->vpm)
  3780. wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s (%s)",
  3781. wc->desc->name, "VPMOCT032");
  3782. else
  3783. wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s",
  3784. wc->desc->name);
  3785. if (!wc->ddev->devicetype) {
  3786. wcaxx_back_out_gracefully(wc);
  3787. return -ENOMEM;
  3788. }
  3789. wc->ddev->hardware_id = wcaxx_read_serial(wc);
  3790. list_add_tail(&wc->span.device_node, &wc->ddev->spans);
  3791. if (dahdi_register_device(wc->ddev, &wc->xb.pdev->dev)) {
  3792. dev_notice(&wc->xb.pdev->dev, "Unable to register device with DAHDI\n");
  3793. wcaxx_back_out_gracefully(wc);
  3794. return -1;
  3795. }
  3796. dev_info(&wc->xb.pdev->dev, "Found a %s (SN: %s)\n",
  3797. wc->desc->name, wc->ddev->hardware_id);
  3798. set_bit(INITIALIZED, &wc->bit_flags);
  3799. wcaxx_start_module_polling(wc);
  3800. wcxb_unlock_latency(&wc->xb);
  3801. return 0;
  3802. }
  3803. #ifdef USE_ASYNC_INIT
  3804. static __devinit void
  3805. wcaxx_init_one_async(void *data, async_cookie_t cookie)
  3806. {
  3807. struct async_data *dat = data;
  3808. __wcaxx_init_one(dat->pdev, dat->ent, cookie);
  3809. kfree(dat);
  3810. }
  3811. static int __devinit
  3812. wcaxx_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  3813. {
  3814. struct async_data *dat;
  3815. dat = kmalloc(sizeof(*dat), GFP_KERNEL);
  3816. /* If we can't allocate the memory for the async_data, odds are we won't
  3817. * be able to initialize the device either, but let's try synchronously
  3818. * anyway... */
  3819. if (!dat)
  3820. return __wcaxx_init_one(pdev, ent, 0);
  3821. dat->pdev = pdev;
  3822. dat->ent = ent;
  3823. async_schedule(wcaxx_init_one_async, dat);
  3824. return 0;
  3825. }
  3826. #else
  3827. static int __devinit
  3828. wcaxx_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  3829. {
  3830. return __wcaxx_init_one(pdev, ent);
  3831. }
  3832. #endif
  3833. static void wcaxx_release(struct wcaxx *wc)
  3834. {
  3835. if (is_initialized(wc))
  3836. dahdi_unregister_device(wc->ddev);
  3837. wcaxx_back_out_gracefully(wc);
  3838. }
  3839. static void __devexit wcaxx_remove_one(struct pci_dev *pdev)
  3840. {
  3841. struct wcaxx *wc = pci_get_drvdata(pdev);
  3842. if (!wc)
  3843. return;
  3844. dev_info(&wc->xb.pdev->dev, "Removing a %s.\n", wc->desc->name);
  3845. flush_scheduled_work();
  3846. wcxb_stop(&wc->xb);
  3847. if (wc->vpm)
  3848. release_vpm450m(wc->vpm);
  3849. wc->vpm = NULL;
  3850. wcaxx_release(wc);
  3851. }
  3852. static DEFINE_PCI_DEVICE_TABLE(wcaxx_pci_tbl) = {
  3853. { 0xd161, 0x800d, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  3854. (unsigned long) &device_a8b
  3855. },
  3856. { 0xd161, 0x800c, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  3857. (unsigned long) &device_a8a
  3858. },
  3859. { 0xd161, 0x8010, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  3860. (unsigned long) &device_a4b
  3861. },
  3862. { 0xd161, 0x800f, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  3863. (unsigned long) &device_a4a
  3864. },
  3865. { 0 }
  3866. };
  3867. MODULE_DEVICE_TABLE(pci, wcaxx_pci_tbl);
  3868. static void wcaxx_shutdown(struct pci_dev *pdev)
  3869. {
  3870. struct wcaxx *wc = pci_get_drvdata(pdev);
  3871. wcxb_stop(&wc->xb);
  3872. }
  3873. static int wcaxx_suspend(struct pci_dev *pdev, pm_message_t state)
  3874. {
  3875. return -ENOSYS;
  3876. }
  3877. static struct pci_driver wcaxx_driver = {
  3878. .name = "wcaxx",
  3879. .probe = wcaxx_init_one,
  3880. .remove = __devexit_p(wcaxx_remove_one),
  3881. .shutdown = wcaxx_shutdown,
  3882. .suspend = wcaxx_suspend,
  3883. .id_table = wcaxx_pci_tbl,
  3884. };
  3885. static int __init wcaxx_init(void)
  3886. {
  3887. int res;
  3888. int x;
  3889. for (x = 0; x < ARRAY_SIZE(fxo_modes); x++) {
  3890. if (!strcmp(fxo_modes[x].name, opermode))
  3891. break;
  3892. }
  3893. if (x < ARRAY_SIZE(fxo_modes)) {
  3894. _opermode = x;
  3895. } else {
  3896. pr_notice("Invalid/unknown operating mode '%s' specified. Please choose one of:\n",
  3897. opermode);
  3898. for (x = 0; x < ARRAY_SIZE(fxo_modes); x++)
  3899. pr_notice(" %s\n", fxo_modes[x].name);
  3900. pr_notice("Note this option is CASE SENSITIVE!\n");
  3901. return -ENODEV;
  3902. }
  3903. if (!strcmp(opermode, "AUSTRALIA")) {
  3904. boostringer = 1;
  3905. fxshonormode = 1;
  3906. }
  3907. if (-1 == fastpickup) {
  3908. if (!strcmp(opermode, "JAPAN"))
  3909. fastpickup = 1;
  3910. else
  3911. fastpickup = 0;
  3912. }
  3913. /* for the voicedaa_check_hook defaults, if the user has not
  3914. * overridden them by specifying them as module parameters, then get
  3915. * the values from the selected operating mode */
  3916. if (!battdebounce)
  3917. battdebounce = fxo_modes[_opermode].battdebounce;
  3918. if (!battalarm)
  3919. battalarm = fxo_modes[_opermode].battalarm;
  3920. if (!battthresh)
  3921. battthresh = fxo_modes[_opermode].battthresh;
  3922. res = dahdi_pci_module(&wcaxx_driver);
  3923. if (res)
  3924. return -ENODEV;
  3925. #ifdef USE_ASYNC_INIT
  3926. async_synchronize_full();
  3927. #endif
  3928. return 0;
  3929. }
  3930. static void __exit wcaxx_cleanup(void)
  3931. {
  3932. pci_unregister_driver(&wcaxx_driver);
  3933. }
  3934. module_param(debug, int, 0600);
  3935. module_param(int_mode, int, 0400);
  3936. MODULE_PARM_DESC(int_mode,
  3937. "0 = Use MSI interrupt if available. 1 = Legacy interrupt only.\n");
  3938. module_param(fastpickup, int, 0400);
  3939. MODULE_PARM_DESC(fastpickup,
  3940. "Set to 1 to shorten the calibration delay when taking an FXO port off "
  3941. "hook. This can be required for Type-II CID. If -1 the calibration "
  3942. "delay will depend on the current opermode.\n");
  3943. module_param(fxovoltage, int, 0600);
  3944. module_param(loopcurrent, int, 0600);
  3945. module_param(reversepolarity, int, 0600);
  3946. #ifdef DEBUG
  3947. module_param(robust, int, 0600);
  3948. module_param(digitalloopback, int, 0400);
  3949. MODULE_PARM_DESC(digitalloopback,
  3950. "Set to 1 to place FXO modules into loopback mode for troubleshooting.");
  3951. #endif
  3952. module_param(opermode, charp, 0600);
  3953. module_param(lowpower, int, 0600);
  3954. module_param(boostringer, int, 0600);
  3955. module_param(fastringer, int, 0600);
  3956. module_param(fxshonormode, int, 0600);
  3957. module_param(battdebounce, uint, 0600);
  3958. module_param(battalarm, uint, 0600);
  3959. module_param(battthresh, uint, 0600);
  3960. module_param(nativebridge, int, 0600);
  3961. module_param(fxotxgain, int, 0600);
  3962. module_param(fxorxgain, int, 0600);
  3963. module_param(fxstxgain, int, 0600);
  3964. module_param(fxsrxgain, int, 0600);
  3965. module_param(ringdebounce, int, 0600);
  3966. module_param(latency, int, 0400);
  3967. module_param(max_latency, int, 0400);
  3968. module_param(neonmwi_monitor, int, 0600);
  3969. module_param(neonmwi_level, int, 0600);
  3970. module_param(neonmwi_envelope, int, 0600);
  3971. module_param(neonmwi_offlimit, int, 0600);
  3972. module_param(vpmsupport, int, 0400);
  3973. module_param(forceload, int, 0600);
  3974. MODULE_PARM_DESC(forceload,
  3975. "Set to 1 in order to force an FPGA reload after power on.");
  3976. module_param(companding, charp, 0400);
  3977. MODULE_PARM_DESC(companding,
  3978. "Change the companding to \"auto\" or \"alaw\" or \"ulaw\". Auto "
  3979. "(default) will set everything to ulaw unless a BRI module is "
  3980. "installed. It will use alaw in that case.");
  3981. MODULE_DESCRIPTION("A4A,A4B,A8A,A8B Driver for Analog Telephony Cards");
  3982. MODULE_AUTHOR("Digium Incorporated <support@digium.com>");
  3983. MODULE_LICENSE("GPL v2");
  3984. module_init(wcaxx_init);
  3985. module_exit(wcaxx_cleanup);