wcte43x-base.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702
  1. /*
  2. * Digium, Inc. Wildcard te43x T1/E1 card Driver
  3. *
  4. * Written by Russ Meyerriecks <rmeyerriecks@digium.com>
  5. * Copyright (C) 2012 - 2013, Digium, Inc.
  6. * All rights reserved.
  7. *
  8. */
  9. /*
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2 as published by the
  18. * Free Software Foundation. See the LICENSE file included with
  19. * this program for more details.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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/proc_fs.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/delay.h>
  32. #include <linux/sched.h>
  33. #include <linux/crc32.h>
  34. #include <linux/slab.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/string.h>
  37. #include <linux/time.h>
  38. #include <linux/version.h>
  39. #include <linux/firmware.h>
  40. #include <oct612x.h>
  41. #include <stdbool.h>
  42. #include <dahdi/kernel.h>
  43. #include "wct4xxp/wct4xxp.h" /* For certain definitions */
  44. #include "wcxb.h"
  45. #include "wcxb_spi.h"
  46. #include "wcxb_flash.h"
  47. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  48. # ifdef RHEL_RELEASE_VERSION
  49. # if RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(5, 6)
  50. # define T43X_HAVE_CANCEL_WORK_SYNC
  51. # endif
  52. # else
  53. static inline int delayed_work_pending(struct work_struct *work)
  54. {
  55. return test_bit(0, &work->pending);
  56. }
  57. # endif
  58. #endif
  59. static const char *TE435_FW_FILENAME = "dahdi-fw-te435.bin";
  60. static const char *TE436_FW_FILENAME = "dahdi-fw-te436.bin";
  61. static const u32 TE435_VERSION = 0x13001e;
  62. static const u32 TE436_VERSION = 0x10017;
  63. /* #define RPC_RCLK */
  64. enum linemode {
  65. T1 = 1,
  66. E1,
  67. J1,
  68. };
  69. #define STATUS_LED_GREEN (1 << 9)
  70. #define STATUS_LED_RED (1 << 10)
  71. #define STATUS_LED_YELLOW (STATUS_LED_RED | STATUS_LED_GREEN)
  72. #define LED_MASK 0x7f8
  73. #define FALC_CPU_RESET (1 << 11)
  74. /* Interrupt definitions */
  75. #define FALC_INT (1<<3)
  76. #define SPI_INT (1<<4)
  77. #define DMA_STOPPED (1<<5)
  78. struct t43x;
  79. struct t43x_span {
  80. struct t43x *owner;
  81. struct dahdi_span span;
  82. #define NMF_FLAGBIT 1
  83. #define SENDINGYELLOW_FLAGBIT 2
  84. #define HAVE_OPEN_CHANNELS_FLAGBIT 3
  85. unsigned long bit_flags;
  86. unsigned char txsigs[16]; /* Copy of tx sig registers */
  87. unsigned long lofalarmtimer;
  88. unsigned long losalarmtimer;
  89. unsigned long aisalarmtimer;
  90. unsigned long yelalarmtimer;
  91. unsigned long recoverytimer;
  92. unsigned long loopuptimer;
  93. unsigned long loopdntimer;
  94. struct dahdi_chan *chans[32]; /* Channels */
  95. struct dahdi_echocan_state *ec[32]; /* Echocan state for channels */
  96. bool debounce;
  97. int syncpos;
  98. int sync;
  99. u32 lineconfig_fingerprint;
  100. };
  101. struct t43x_clksrc_work {
  102. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  103. struct work_struct work;
  104. #else
  105. struct delayed_work work;
  106. #endif
  107. spinlock_t lock;
  108. enum wcxb_clock_sources clksrc;
  109. bool is_timing_master;
  110. };
  111. /* One structure per card */
  112. struct t43x {
  113. spinlock_t reglock;
  114. const struct t43x_desc *devtype;
  115. unsigned long blinktimer;
  116. bool blink;
  117. struct dahdi_device *ddev;
  118. struct t43x_span *tspans[4];
  119. int numspans; /* Number of spans on the card */
  120. int spansstarted; /* Number of spans started */
  121. /* protected by t1.reglock */
  122. struct timer_list timer;
  123. struct work_struct timer_work;
  124. struct workqueue_struct *wq;
  125. struct t43x_clksrc_work clksrc_work;
  126. unsigned int not_ready; /* 0 when entire card is ready to go */
  127. struct vpm450m *vpm;
  128. char *vpm_name;
  129. struct mutex lock;
  130. bool latency_locked;
  131. int syncsrc;
  132. int num; /* card index */
  133. int intr_span; /* span to service next interrupt */
  134. struct wcxb xb;
  135. struct list_head card_node;
  136. };
  137. static void t43x_handle_transmit(struct wcxb *xb, void *vfp);
  138. static void t43x_handle_receive(struct wcxb *xb, void *vfp);
  139. static void t43x_handle_interrupt(struct wcxb *xb, u32 pending);
  140. static void t43x_handle_error(struct wcxb *xb)
  141. {
  142. struct t43x *wc = container_of(xb, struct t43x, xb);
  143. wc->ddev->irqmisses++;
  144. }
  145. static struct wcxb_operations xb_ops = {
  146. .handle_receive = t43x_handle_receive,
  147. .handle_transmit = t43x_handle_transmit,
  148. .handle_interrupt = t43x_handle_interrupt,
  149. .handle_error = t43x_handle_error,
  150. };
  151. /* Maintenance Mode Registers */
  152. #define LIM0 0x36
  153. #define LIM0_LL (1<<1)
  154. #define LIM1 0x37
  155. #define LIM1_RL (1<<1)
  156. #define LIM1_JATT (1<<2)
  157. /* Clear Channel Registers */
  158. #define CCB1 0x2f
  159. #define CCB2 0x30
  160. #define CCB3 0x31
  161. #define FECL_T 0x50 /* Framing Error Counter Lower Byte */
  162. #define FECH_T 0x51 /* Framing Error Counter Higher Byte */
  163. #define CVCL_T 0x52 /* Code Violation Counter Lower Byte */
  164. #define CVCH_T 0x53 /* Code Violation Counter Higher Byte */
  165. #define CEC1L_T 0x54 /* CRC Error Counter 1 Lower Byte */
  166. #define CEC1H_T 0x55 /* CRC Error Counter 1 Higher Byte */
  167. #define EBCL_T 0x56 /* E-Bit Error Counter Lower Byte */
  168. #define EBCH_T 0x57 /* E-Bit Error Counter Higher Byte */
  169. #define BECL_T 0x58 /* Bit Error Counter Lower Byte */
  170. #define BECH_T 0x59 /* Bit Error Counter Higher Byte */
  171. #define COEC_T 0x5A /* COFA Event Counter */
  172. #define PRBSSTA_T 0xDA /* PRBS Status Register */
  173. #define FRS1_T 0x4D /* Framer Receive Status Reg 1 */
  174. #define ISR3_SEC (1 << 6) /* Internal one-second interrupt bit mask */
  175. #define ISR3_ES (1 << 7) /* Errored Second interrupt bit mask */
  176. #define IMR0 0x14
  177. /* pci memory map offsets */
  178. #define DMA1 0x00 /* dma addresses */
  179. #define DMA2 0x04
  180. #define DMA3 0x08
  181. #define CNTL 0x0C /* fpga control register */
  182. #define INT_MASK 0x10 /* interrupt vectors from oct and framer */
  183. #define INT_STAT 0x14
  184. #define FRAMER_BASE 0x00000800 /* framer's address space */
  185. #define NOSYNC_ALARMS (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | \
  186. DAHDI_ALARM_LOOPBACK)
  187. static int debug;
  188. static int timingcable;
  189. static int force_firmware;
  190. static int alarmdebounce = 2500; /* LOF/LFA def to 2.5s AT&T TR54016*/
  191. static int losalarmdebounce = 2500; /* LOS def to 2.5s AT&T TR54016*/
  192. static int aisalarmdebounce = 2500; /* AIS(blue) def to 2.5s AT&T TR54016*/
  193. static int yelalarmdebounce = 500; /* RAI(yellow) def to 0.5s AT&T guide */
  194. static char *default_linemode = "t1"; /* 'e1', 't1', or 'j1' */
  195. static int latency = WCXB_DEFAULT_LATENCY;
  196. static int max_latency = WCXB_DEFAULT_MAXLATENCY;
  197. struct t43x_firm_header {
  198. u8 header[6];
  199. __le32 chksum;
  200. u8 pad[18];
  201. __le32 version;
  202. } __packed;
  203. static int t43x_check_for_interrupts(struct t43x *wc);
  204. static void t43x_check_alarms(struct t43x *wc, int span_idx);
  205. static void t43x_check_sigbits(struct t43x *wc, int span_idx);
  206. static const struct dahdi_span_ops t43x_span_ops;
  207. static void __t43x_set_timing_source_auto(struct t43x *wc);
  208. static int vpmsupport = 1;
  209. #include "oct6100api/oct6100_api.h"
  210. #define ECHOCAN_NUM_CHANS 128
  211. #define FLAGBIT_DTMF 1
  212. #define FLAGBIT_MUTE 2
  213. #define FLAGBIT_ECHO 3
  214. #define FLAGBIT_ALAW 4
  215. #define OCT_CHIP_ID 0
  216. #define OCT_MAX_TDM_STREAMS 4
  217. #define OCT_TONEEVENT_BUFFER_SIZE 128
  218. #define SOUT_STREAM 1
  219. #define RIN_STREAM 0
  220. #define SIN_STREAM 2
  221. static void t43x_vpm_init(struct t43x *wc);
  222. static void echocan_free(struct dahdi_chan *chan,
  223. struct dahdi_echocan_state *ec);
  224. static const struct dahdi_echocan_features vpm_ec_features = {
  225. .NLP_automatic = 1,
  226. .CED_tx_detect = 1,
  227. .CED_rx_detect = 1,
  228. };
  229. static const struct dahdi_echocan_ops vpm_ec_ops = {
  230. .echocan_free = echocan_free,
  231. };
  232. struct vpm450m {
  233. tPOCT6100_INSTANCE_API pApiInstance;
  234. struct oct612x_context context;
  235. UINT32 aulEchoChanHndl[ECHOCAN_NUM_CHANS];
  236. int ecmode[ECHOCAN_NUM_CHANS];
  237. unsigned long chanflags[ECHOCAN_NUM_CHANS];
  238. };
  239. static int t43x_oct612x_write(struct oct612x_context *context,
  240. u32 address, u16 value)
  241. {
  242. struct t43x *wc = dev_get_drvdata(context->dev);
  243. wcxb_set_echocan_reg(&wc->xb, address, value);
  244. return 0;
  245. }
  246. static int t43x_oct612x_read(struct oct612x_context *context, u32 address,
  247. u16 *value)
  248. {
  249. struct t43x *wc = dev_get_drvdata(context->dev);
  250. *value = wcxb_get_echocan_reg(&wc->xb, address);
  251. return 0;
  252. }
  253. static int t43x_oct612x_write_smear(struct oct612x_context *context,
  254. u32 address, u16 value, size_t count)
  255. {
  256. unsigned int i;
  257. struct t43x *wc = dev_get_drvdata(context->dev);
  258. for (i = 0; i < count; ++i)
  259. wcxb_set_echocan_reg(&wc->xb, address + (i << 1), value);
  260. return 0;
  261. }
  262. static int t43x_oct612x_write_burst(struct oct612x_context *context,
  263. u32 address, const u16 *buffer,
  264. size_t count)
  265. {
  266. unsigned int i;
  267. struct t43x *wc = dev_get_drvdata(context->dev);
  268. for (i = 0; i < count; ++i)
  269. wcxb_set_echocan_reg(&wc->xb, address + (i << 1), buffer[i]);
  270. return 0;
  271. }
  272. static int t43x_oct612x_read_burst(struct oct612x_context *context,
  273. u32 address, u16 *buffer, size_t count)
  274. {
  275. unsigned int i;
  276. struct t43x *wc = dev_get_drvdata(context->dev);
  277. for (i = 0; i < count; ++i)
  278. buffer[i] = wcxb_get_echocan_reg(&wc->xb, address + (i << 1));
  279. return 0;
  280. }
  281. static const struct oct612x_ops t43x_oct612x_ops = {
  282. .write = t43x_oct612x_write,
  283. .read = t43x_oct612x_read,
  284. .write_smear = t43x_oct612x_write_smear,
  285. .write_burst = t43x_oct612x_write_burst,
  286. .read_burst = t43x_oct612x_read_burst,
  287. };
  288. static void vpm450m_setecmode(struct vpm450m *vpm450m, int channel, int mode)
  289. {
  290. tOCT6100_CHANNEL_MODIFY *modify;
  291. UINT32 ulResult;
  292. if (vpm450m->ecmode[channel] == mode)
  293. return;
  294. modify = kzalloc(sizeof(tOCT6100_CHANNEL_MODIFY), GFP_ATOMIC);
  295. if (!modify) {
  296. pr_notice("Unable to allocate memory for setec!\n");
  297. return;
  298. }
  299. Oct6100ChannelModifyDef(modify);
  300. modify->ulEchoOperationMode = mode;
  301. modify->ulChannelHndl = vpm450m->aulEchoChanHndl[channel];
  302. ulResult = Oct6100ChannelModify(vpm450m->pApiInstance, modify);
  303. if (ulResult != GENERIC_OK) {
  304. pr_notice("Failed to apply echo can changes on channel %d %d %08x!\n",
  305. vpm450m->aulEchoChanHndl[channel], channel, ulResult);
  306. } else {
  307. #ifdef OCTASIC_DEBUG
  308. pr_debug("Echo can on channel %d set to %d\n", channel, mode);
  309. #endif
  310. vpm450m->ecmode[channel] = mode;
  311. }
  312. kfree(modify);
  313. }
  314. static void vpm450m_set_alaw_companding(struct vpm450m *vpm450m, int channel,
  315. bool alaw)
  316. {
  317. tOCT6100_CHANNEL_MODIFY *modify;
  318. UINT32 ulResult;
  319. UINT32 law_to_use = (alaw) ? cOCT6100_PCM_A_LAW :
  320. cOCT6100_PCM_U_LAW;
  321. /* If we're already in this companding mode, no need to do anything. */
  322. if (alaw == (test_bit(FLAGBIT_ALAW, &vpm450m->chanflags[channel]) > 0))
  323. return;
  324. modify = kzalloc(sizeof(tOCT6100_CHANNEL_MODIFY), GFP_ATOMIC);
  325. if (!modify) {
  326. pr_notice("Unable to allocate memory for setec!\n");
  327. return;
  328. }
  329. Oct6100ChannelModifyDef(modify);
  330. modify->ulChannelHndl = vpm450m->aulEchoChanHndl[channel];
  331. modify->fTdmConfigModified = TRUE;
  332. modify->TdmConfig.ulSinPcmLaw = law_to_use;
  333. modify->TdmConfig.ulRinPcmLaw = law_to_use;
  334. modify->TdmConfig.ulSoutPcmLaw = law_to_use;
  335. modify->TdmConfig.ulRoutPcmLaw = law_to_use;
  336. ulResult = Oct6100ChannelModify(vpm450m->pApiInstance, modify);
  337. if (ulResult != GENERIC_OK) {
  338. pr_notice("Failed to apply echo can changes on channel %d %d %08x!\n",
  339. vpm450m->aulEchoChanHndl[channel], channel, ulResult);
  340. } else {
  341. if (debug) {
  342. pr_info("Changed companding on channel %d to %s.\n",
  343. channel, (alaw) ? "alaw" : "ulaw");
  344. }
  345. if (alaw)
  346. set_bit(FLAGBIT_ALAW, &vpm450m->chanflags[channel]);
  347. else
  348. clear_bit(FLAGBIT_ALAW, &vpm450m->chanflags[channel]);
  349. }
  350. kfree(modify);
  351. }
  352. static void vpm450m_setec(struct vpm450m *vpm450m, int channel, int eclen)
  353. {
  354. if (eclen) {
  355. set_bit(FLAGBIT_ECHO, &vpm450m->chanflags[channel]);
  356. vpm450m_setecmode(vpm450m, channel,
  357. cOCT6100_ECHO_OP_MODE_NORMAL);
  358. } else {
  359. unsigned long *flags = &vpm450m->chanflags[channel];
  360. clear_bit(FLAGBIT_ECHO, &vpm450m->chanflags[channel]);
  361. if (test_bit(FLAGBIT_DTMF, flags) ||
  362. test_bit(FLAGBIT_MUTE, flags)) {
  363. vpm450m_setecmode(vpm450m, channel,
  364. cOCT6100_ECHO_OP_MODE_HT_RESET);
  365. } else {
  366. vpm450m_setecmode(vpm450m, channel,
  367. cOCT6100_ECHO_OP_MODE_POWER_DOWN);
  368. }
  369. }
  370. }
  371. static UINT32 tdmmode_chan_to_slot_map(int channel)
  372. {
  373. /* Four phases on the tdm bus, skip three of them per channel */
  374. /* Due to a bug in the octasic, we had to move the data onto phase 2 */
  375. return 1 + ((channel % 32) * 4) + (channel / 32);
  376. }
  377. static int echocan_initialize_channel(
  378. struct vpm450m *vpm, int channel, int mode)
  379. {
  380. tOCT6100_CHANNEL_OPEN ChannelOpen;
  381. UINT32 law_to_use = (mode) ? cOCT6100_PCM_A_LAW :
  382. cOCT6100_PCM_U_LAW;
  383. UINT32 tdmslot_setting;
  384. UINT32 ulResult;
  385. tdmslot_setting = tdmmode_chan_to_slot_map(channel);
  386. /* Fill Open channel structure with defaults */
  387. Oct6100ChannelOpenDef(&ChannelOpen);
  388. /* Assign the handle memory.*/
  389. ChannelOpen.pulChannelHndl = &vpm->aulEchoChanHndl[channel];
  390. ChannelOpen.ulUserChanId = channel;
  391. /* Enable Tone disabling for Fax and Modems */
  392. ChannelOpen.fEnableToneDisabler = TRUE;
  393. /* Passthrough TDM data by default, no echocan */
  394. ChannelOpen.ulEchoOperationMode = cOCT6100_ECHO_OP_MODE_POWER_DOWN;
  395. /* Configure the TDM settings.*/
  396. /* Input from the framer */
  397. ChannelOpen.TdmConfig.ulSinStream = SIN_STREAM;
  398. ChannelOpen.TdmConfig.ulSinTimeslot = tdmslot_setting;
  399. ChannelOpen.TdmConfig.ulSinPcmLaw = law_to_use;
  400. /* Input from the Host (pre-framer) */
  401. ChannelOpen.TdmConfig.ulRinStream = RIN_STREAM;
  402. ChannelOpen.TdmConfig.ulRinTimeslot = tdmslot_setting;
  403. ChannelOpen.TdmConfig.ulRinPcmLaw = law_to_use;
  404. /* Output to the Host */
  405. ChannelOpen.TdmConfig.ulSoutStream = SOUT_STREAM;
  406. ChannelOpen.TdmConfig.ulSoutTimeslot = tdmslot_setting;
  407. ChannelOpen.TdmConfig.ulSoutPcmLaw = law_to_use;
  408. /* From asterisk after echo-cancellation - goes nowhere */
  409. ChannelOpen.TdmConfig.ulRoutStream = cOCT6100_UNASSIGNED;
  410. ChannelOpen.TdmConfig.ulRoutTimeslot = cOCT6100_UNASSIGNED;
  411. ChannelOpen.TdmConfig.ulRoutPcmLaw = law_to_use;
  412. /* Set the desired VQE features.*/
  413. ChannelOpen.VqeConfig.fEnableNlp = TRUE;
  414. ChannelOpen.VqeConfig.fRinDcOffsetRemoval = TRUE;
  415. ChannelOpen.VqeConfig.fSinDcOffsetRemoval = TRUE;
  416. ChannelOpen.VqeConfig.ulComfortNoiseMode =
  417. cOCT6100_COMFORT_NOISE_NORMAL;
  418. /* Open the channel.*/
  419. ulResult = Oct6100ChannelOpen(vpm->pApiInstance, &ChannelOpen);
  420. return ulResult;
  421. }
  422. static struct vpm450m *init_vpm450m(struct t43x *wc, int *laws, int numspans,
  423. const struct firmware *firmware)
  424. {
  425. tOCT6100_CHIP_OPEN *ChipOpen;
  426. tOCT6100_GET_INSTANCE_SIZE InstanceSize;
  427. tOCT6100_CHANNEL_OPEN *ChannelOpen;
  428. UINT32 ulResult;
  429. struct vpm450m *vpm450m;
  430. int x, i;
  431. vpm450m = kzalloc(sizeof(struct vpm450m), GFP_KERNEL);
  432. if (!vpm450m) {
  433. dev_info(&wc->xb.pdev->dev,
  434. "Unable to allocate vpm450m struct\n");
  435. return NULL;
  436. }
  437. vpm450m->context.dev = &wc->xb.pdev->dev;
  438. vpm450m->context.ops = &t43x_oct612x_ops;
  439. ChipOpen = kzalloc(sizeof(tOCT6100_CHIP_OPEN), GFP_KERNEL);
  440. if (!ChipOpen) {
  441. dev_info(&wc->xb.pdev->dev, "Unable to allocate ChipOpen\n");
  442. kfree(vpm450m);
  443. return NULL;
  444. }
  445. ChannelOpen = kzalloc(sizeof(tOCT6100_CHANNEL_OPEN), GFP_KERNEL);
  446. if (!ChannelOpen) {
  447. dev_info(&wc->xb.pdev->dev, "Unable to allocate ChannelOpen\n");
  448. kfree(vpm450m);
  449. kfree(ChipOpen);
  450. return NULL;
  451. }
  452. for (x = 0; x < ARRAY_SIZE(vpm450m->ecmode); x++)
  453. vpm450m->ecmode[x] = -1;
  454. dev_info(&wc->xb.pdev->dev, "Echo cancellation for %d channels\n",
  455. wc->numspans * 32);
  456. Oct6100ChipOpenDef(ChipOpen);
  457. ChipOpen->pProcessContext = &vpm450m->context;
  458. /* Change default parameters as needed */
  459. /* upclk oscillator is at 33.33 Mhz */
  460. ChipOpen->ulUpclkFreq = cOCT6100_UPCLK_FREQ_33_33_MHZ;
  461. /* mclk will be generated by internal PLL at 133 Mhz */
  462. ChipOpen->fEnableMemClkOut = TRUE;
  463. ChipOpen->ulMemClkFreq = cOCT6100_MCLK_FREQ_133_MHZ;
  464. /* User defined Chip ID.*/
  465. ChipOpen->ulUserChipId = OCT_CHIP_ID;
  466. /* Set the maximums that the chip needs to support */
  467. ChipOpen->ulMaxChannels = wc->numspans * 32;
  468. ChipOpen->ulMaxTdmStreams = OCT_MAX_TDM_STREAMS;
  469. /* External Memory Settings */
  470. /* Use DDR memory.*/
  471. ChipOpen->ulMemoryType = cOCT6100_MEM_TYPE_DDR;
  472. ChipOpen->ulNumMemoryChips = 1;
  473. ChipOpen->ulMemoryChipSize = cOCT6100_MEMORY_CHIP_SIZE_32MB;
  474. ChipOpen->pbyImageFile = (PUINT8) firmware->data;
  475. ChipOpen->ulImageSize = firmware->size;
  476. /* Set TDM data stream frequency */
  477. for (i = 0; i < ChipOpen->ulMaxTdmStreams; i++)
  478. ChipOpen->aulTdmStreamFreqs[i] = cOCT6100_TDM_STREAM_FREQ_8MHZ;
  479. /* Configure TDM sampling */
  480. ChipOpen->ulTdmSampling = cOCT6100_TDM_SAMPLE_AT_FALLING_EDGE;
  481. /* Disable to save RAM footprint space */
  482. ChipOpen->fEnableChannelRecording = false;
  483. /* In this example we will maintain the API using polling so
  484. interrupts must be disabled */
  485. ChipOpen->InterruptConfig.ulErrorH100Config =
  486. cOCT6100_INTERRUPT_DISABLE;
  487. ChipOpen->InterruptConfig.ulErrorMemoryConfig =
  488. cOCT6100_INTERRUPT_DISABLE;
  489. ChipOpen->InterruptConfig.ulFatalGeneralConfig =
  490. cOCT6100_INTERRUPT_DISABLE;
  491. ChipOpen->InterruptConfig.ulFatalMemoryConfig =
  492. cOCT6100_INTERRUPT_DISABLE;
  493. ChipOpen->ulSoftToneEventsBufSize = OCT_TONEEVENT_BUFFER_SIZE;
  494. /* Inserting default values into tOCT6100_GET_INSTANCE_SIZE
  495. structure parameters. */
  496. Oct6100GetInstanceSizeDef(&InstanceSize);
  497. /* Reset octasic device */
  498. wcxb_reset_echocan(&wc->xb);
  499. /* Get the size of the OCT6100 instance structure. */
  500. ulResult = Oct6100GetInstanceSize(ChipOpen, &InstanceSize);
  501. if (ulResult != cOCT6100_ERR_OK) {
  502. dev_info(&wc->xb.pdev->dev, "Unable to get instance size: %x\n",
  503. ulResult);
  504. return NULL;
  505. }
  506. vpm450m->pApiInstance = vmalloc(InstanceSize.ulApiInstanceSize);
  507. if (!vpm450m->pApiInstance) {
  508. dev_info(&wc->xb.pdev->dev,
  509. "Out of memory (can't allocate %d bytes)!\n",
  510. InstanceSize.ulApiInstanceSize);
  511. return NULL;
  512. }
  513. /* Perform the actual configuration of the chip. */
  514. wcxb_enable_echocan_dram(&wc->xb);
  515. ulResult = Oct6100ChipOpen(vpm450m->pApiInstance, ChipOpen);
  516. if (ulResult != cOCT6100_ERR_OK) {
  517. dev_info(&wc->xb.pdev->dev, "Unable to Oct6100ChipOpen: %x\n",
  518. ulResult);
  519. return NULL;
  520. }
  521. /* OCT6100 is now booted and channels can be opened */
  522. /* Open 31 channels/span since we're skipping the first on the VPM */
  523. for (x = 0; x < numspans; x++) {
  524. for (i = 0; i < 31; i++) {
  525. ulResult = echocan_initialize_channel(vpm450m,
  526. (x*32)+i,
  527. laws[x]);
  528. if (0 != ulResult) {
  529. dev_info(&wc->xb.pdev->dev,
  530. "Unable to echocan_initialize_channel: %d %x\n",
  531. (x*32)+i, ulResult);
  532. return NULL;
  533. } else if (laws[x]) {
  534. set_bit(FLAGBIT_ALAW,
  535. &vpm450m->chanflags[(x*32)+i]);
  536. }
  537. }
  538. }
  539. if (vpmsupport != 0)
  540. wcxb_enable_echocan(&wc->xb);
  541. kfree(ChipOpen);
  542. kfree(ChannelOpen);
  543. return vpm450m;
  544. }
  545. static void release_vpm450m(struct vpm450m *vpm450m)
  546. {
  547. UINT32 ulResult;
  548. tOCT6100_CHIP_CLOSE ChipClose;
  549. Oct6100ChipCloseDef(&ChipClose);
  550. ulResult = Oct6100ChipClose(vpm450m->pApiInstance, &ChipClose);
  551. if (ulResult != cOCT6100_ERR_OK)
  552. pr_notice("Failed to close chip, code %08x!\n", ulResult);
  553. vfree(vpm450m->pApiInstance);
  554. kfree(vpm450m);
  555. }
  556. static const char *__t43x_echocan_name(struct t43x *wc)
  557. {
  558. if (wc->vpm)
  559. return wc->vpm_name;
  560. else
  561. return NULL;
  562. }
  563. static const char *t43x_echocan_name(const struct dahdi_chan *chan)
  564. {
  565. struct t43x *wc = chan->pvt;
  566. return __t43x_echocan_name(wc);
  567. }
  568. static int t43x_echocan_create(struct dahdi_chan *chan,
  569. struct dahdi_echocanparams *ecp,
  570. struct dahdi_echocanparam *p,
  571. struct dahdi_echocan_state **ec)
  572. {
  573. struct t43x *wc = chan->pvt;
  574. struct t43x_span *ts = container_of(chan->span, struct t43x_span, span);
  575. int channel = chan->chanpos - 1;
  576. const bool alaw = (chan->span->deflaw == 2);
  577. if (!vpmsupport || !wc->vpm)
  578. return -ENODEV;
  579. if (ecp->param_count > 0) {
  580. dev_warn(&wc->xb.pdev->dev,
  581. "%s echo canceller does not support parameters; failing request\n",
  582. chan->ec_factory->get_name(chan));
  583. return -EINVAL;
  584. }
  585. *ec = ts->ec[channel];
  586. (*ec)->ops = &vpm_ec_ops;
  587. (*ec)->features = vpm_ec_features;
  588. channel += (32*chan->span->offset);
  589. vpm450m_set_alaw_companding(wc->vpm, channel, alaw);
  590. vpm450m_setec(wc->vpm, channel, ecp->tap_length);
  591. return 0;
  592. }
  593. static void echocan_free(struct dahdi_chan *chan,
  594. struct dahdi_echocan_state *ec)
  595. {
  596. struct t43x *wc = chan->pvt;
  597. int channel = chan->chanpos - 1;
  598. if (!wc->vpm)
  599. return;
  600. memset(ec, 0, sizeof(*ec));
  601. channel += (32*chan->span->offset);
  602. vpm450m_setec(wc->vpm, channel, 0);
  603. }
  604. static void t43x_vpm_init(struct t43x *wc)
  605. {
  606. int laws[8] = {0, };
  607. int x;
  608. struct firmware embedded_firmware;
  609. const struct firmware *firmware = &embedded_firmware;
  610. #if !defined(HOTPLUG_FIRMWARE)
  611. extern void _binary_dahdi_fw_oct6114_064_bin_size;
  612. extern u8 _binary_dahdi_fw_oct6114_064_bin_start[];
  613. extern void _binary_dahdi_fw_oct6114_128_bin_size;
  614. extern u8 _binary_dahdi_fw_oct6114_128_bin_start[];
  615. #else
  616. static const char oct064_firmware[] = "dahdi-fw-oct6114-064.bin";
  617. static const char oct128_firmware[] = "dahdi-fw-oct6114-128.bin";
  618. const char *oct_firmware;
  619. #endif
  620. if (!vpmsupport) {
  621. dev_info(&wc->xb.pdev->dev, "VPM450: Support Disabled\n");
  622. return;
  623. }
  624. #if defined(HOTPLUG_FIRMWARE)
  625. if (wc->numspans == 2) {
  626. wc->vpm_name = "VPMOCT064";
  627. oct_firmware = oct064_firmware;
  628. } else {
  629. wc->vpm_name = "VPMOCT128";
  630. oct_firmware = oct128_firmware;
  631. }
  632. if ((request_firmware(&firmware, oct_firmware, &wc->xb.pdev->dev) != 0)
  633. || !firmware) {
  634. dev_notice(&wc->xb.pdev->dev,
  635. "VPM450: firmware %s not available from userspace\n",
  636. oct_firmware);
  637. return;
  638. }
  639. #else
  640. /* Yes... this is weird. objcopy gives us a symbol containing
  641. the size of the firmware, not a pointer a variable containing
  642. the size. The only way we can get the value of the symbol
  643. is to take its address, so we define it as a pointer and
  644. then cast that value to the proper type.
  645. */
  646. if (wc->numspans == 2) {
  647. embedded_firmware.data = _binary_dahdi_fw_oct6114_064_bin_start;
  648. embedded_firmware.size =
  649. (size_t)&_binary_dahdi_fw_oct6114_064_bin_size;
  650. } else {
  651. embedded_firmware.size =
  652. (size_t)&_binary_dahdi_fw_oct6114_128_bin_size;
  653. embedded_firmware.data = _binary_dahdi_fw_oct6114_128_bin_start;
  654. }
  655. #endif
  656. /* Setup alaw vs ulaw rules */
  657. for (x = 0; x < wc->numspans; x++) {
  658. if (wc->tspans[x]->span.channels > 24)
  659. laws[x] = 1;
  660. }
  661. wc->vpm = init_vpm450m(wc, laws, wc->numspans, firmware);
  662. if (!wc->vpm) {
  663. dev_notice(&wc->xb.pdev->dev, "VPM450: Failed to initialize\n");
  664. if (firmware != &embedded_firmware)
  665. release_firmware(firmware);
  666. return;
  667. }
  668. if (firmware != &embedded_firmware)
  669. release_firmware(firmware);
  670. dev_info(&wc->xb.pdev->dev,
  671. "VPM450: Present and operational servicing %d span(s)\n",
  672. wc->numspans);
  673. }
  674. static int t43x_clear_maint(struct dahdi_span *span);
  675. static DEFINE_MUTEX(card_list_lock);
  676. static LIST_HEAD(card_list);
  677. struct t43x_desc {
  678. const char *name;
  679. };
  680. static const struct t43x_desc te435 = {"Wildcard TE435"}; /* pci express quad */
  681. static const struct t43x_desc te235 = {"Wildcard TE235"}; /* pci express dual */
  682. static const struct t43x_desc te436 = {"Wildcard TE436"}; /* pci quad */
  683. static const struct t43x_desc te236 = {"Wildcard TE236"}; /* pci dual */
  684. static int __t43x_pci_get(struct t43x *wc, unsigned int addr)
  685. {
  686. unsigned int res = ioread8(wc->xb.membase + addr);
  687. return res;
  688. }
  689. static inline int __t43x_pci_set(struct t43x *wc, unsigned int addr, int val)
  690. {
  691. iowrite8(val, wc->xb.membase + addr);
  692. __t43x_pci_get(wc, 0);
  693. return 0;
  694. }
  695. static inline int t43x_pci_get(struct t43x *wc, int addr)
  696. {
  697. unsigned int ret;
  698. unsigned long flags;
  699. spin_lock_irqsave(&wc->reglock, flags);
  700. ret = __t43x_pci_get(wc, addr);
  701. spin_unlock_irqrestore(&wc->reglock, flags);
  702. return ret;
  703. }
  704. static inline int t43x_pci_set(struct t43x *wc, int addr, int val)
  705. {
  706. unsigned long flags;
  707. unsigned int ret;
  708. spin_lock_irqsave(&wc->reglock, flags);
  709. ret = __t43x_pci_set(wc, addr, val);
  710. spin_unlock_irqrestore(&wc->reglock, flags);
  711. return ret;
  712. }
  713. static inline int
  714. __t43x_framer_set(struct t43x *wc, int unit, int addr, int val)
  715. {
  716. return __t43x_pci_set(wc, FRAMER_BASE + (unit << 8) + addr, val);
  717. }
  718. static inline int t43x_framer_set(struct t43x *wc, int unit, int addr, int val)
  719. {
  720. return t43x_pci_set(wc, FRAMER_BASE + (unit << 8) + addr, val);
  721. }
  722. static inline int __t43x_framer_get(struct t43x *wc, int unit, int addr)
  723. {
  724. return __t43x_pci_get(wc, FRAMER_BASE + (unit << 8) + addr);
  725. }
  726. static inline int t43x_framer_get(struct t43x *wc, int unit, int addr)
  727. {
  728. return t43x_pci_get(wc, FRAMER_BASE + (unit << 8) + addr);
  729. }
  730. static void t43x_framer_reset(struct t43x *wc)
  731. {
  732. /*
  733. * When the framer is reset, RCLK will stop. The FPGA must be switched
  734. * to it's internal clock when this happens, but it's only safe to
  735. * switch the clock source on the FPGA when the DMA engine is stopped.
  736. *
  737. */
  738. wcxb_stop_dma(&wc->xb);
  739. wcxb_wait_for_stop(&wc->xb, 50);
  740. wcxb_set_clksrc(&wc->xb, WCXB_CLOCK_SELF);
  741. wcxb_gpio_clear(&wc->xb, FALC_CPU_RESET);
  742. msleep_interruptible(100);
  743. wcxb_gpio_set(&wc->xb, FALC_CPU_RESET);
  744. }
  745. static void t43x_setleds(struct t43x *wc, u32 leds)
  746. {
  747. wcxb_gpio_set(&wc->xb, leds & LED_MASK);
  748. wcxb_gpio_clear(&wc->xb, ~leds & LED_MASK);
  749. }
  750. static void t43x_set_cas_mode(struct t43x *wc, int span_idx)
  751. {
  752. struct t43x_span *ts = wc->tspans[span_idx];
  753. int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
  754. int i, offset;
  755. int reg;
  756. unsigned long flags;
  757. bool span_has_cas_channel = false;
  758. if (debug)
  759. dev_info(&wc->xb.pdev->dev, "%s span: %d\n", __func__,
  760. span_idx);
  761. if (dahdi_is_e1_span(&ts->span)) {
  762. span_has_cas_channel = !(ts->span.lineconfig&DAHDI_CONFIG_CCS);
  763. } else {
  764. unsigned char ccb[3] = {0, 0, 0};
  765. /* Sort out channels that use CAS signalling */
  766. for (i = 0; i < ts->span.channels; i++) {
  767. offset = i/8;
  768. if (offset >= ARRAY_SIZE(ccb)) {
  769. WARN_ON(1);
  770. break;
  771. }
  772. if (ts->span.chans[i]->flags & DAHDI_FLAG_CLEAR)
  773. ccb[offset] |= 1 << (7 - (i % 8));
  774. else
  775. ccb[offset] &= ~(1 << (7 - (i % 8)));
  776. }
  777. spin_lock_irqsave(&wc->reglock, flags);
  778. __t43x_framer_set(wc, fidx, CCB1, ccb[0]);
  779. __t43x_framer_set(wc, fidx, CCB2, ccb[1]);
  780. __t43x_framer_set(wc, fidx, CCB3, ccb[2]);
  781. spin_unlock_irqrestore(&wc->reglock, flags);
  782. if ((~ccb[0]) | (~ccb[1]) | (~ccb[2]))
  783. span_has_cas_channel = true;
  784. }
  785. /* Unmask CAS RX interrupt if any single channel is in CAS mode */
  786. /* This interrupt is called RSC in T1 and CASC in E1 */
  787. spin_lock_irqsave(&wc->reglock, flags);
  788. reg = __t43x_framer_get(wc, fidx, IMR0);
  789. if (span_has_cas_channel)
  790. __t43x_framer_set(wc, fidx, IMR0, reg & ~0x08);
  791. else
  792. __t43x_framer_set(wc, fidx, IMR0, reg | 0x08);
  793. spin_unlock_irqrestore(&wc->reglock, flags);
  794. }
  795. /**
  796. * _t43x_free_channels - Free the memory allocated for the channels.
  797. *
  798. * Must be called with wc->reglock held.
  799. *
  800. */
  801. static void _t43x_free_channels(struct t43x *wc)
  802. {
  803. unsigned int x, y;
  804. for (x = 0; x < ARRAY_SIZE(wc->tspans); x++) {
  805. if (!wc->tspans[x])
  806. continue;
  807. for (y = 0; y < ARRAY_SIZE(wc->tspans[x]->chans); y++) {
  808. kfree(wc->tspans[x]->chans[y]);
  809. kfree(wc->tspans[x]->ec[y]);
  810. }
  811. kfree(wc->tspans[x]);
  812. }
  813. }
  814. static void free_wc(struct t43x *wc)
  815. {
  816. unsigned long flags;
  817. LIST_HEAD(list);
  818. mutex_lock(&card_list_lock);
  819. list_del(&wc->card_node);
  820. mutex_unlock(&card_list_lock);
  821. spin_lock_irqsave(&wc->reglock, flags);
  822. _t43x_free_channels(wc);
  823. spin_unlock_irqrestore(&wc->reglock, flags);
  824. if (wc->wq)
  825. destroy_workqueue(wc->wq);
  826. kfree(wc->ddev->location);
  827. kfree(wc->ddev->devicetype);
  828. kfree(wc->ddev->hardware_id);
  829. if (wc->ddev)
  830. dahdi_free_device(wc->ddev);
  831. kfree(wc);
  832. }
  833. static void t43x_serial_setup(struct t43x *wc)
  834. {
  835. unsigned long flags;
  836. int slot, fidx;
  837. dev_info(&wc->xb.pdev->dev,
  838. "Setting up global serial parameters for card %d\n", wc->num);
  839. t43x_framer_reset(wc);
  840. spin_lock_irqsave(&wc->reglock, flags);
  841. /* GPC1: Multiplex mode enabled, FSC is output, active low, RCLK from
  842. * channel 0 */
  843. __t43x_framer_set(wc, 0, 0x85, 0xe0);
  844. /* GPC3: Enable Multi Purpose Switches */
  845. __t43x_framer_set(wc, 0, 0xd3, 0xa1);
  846. /* GPC4: Enable Multi Purpose Switches */
  847. __t43x_framer_set(wc, 0, 0xd4, 0x83);
  848. /* GPC5: Enable Multi Purpose Switches */
  849. __t43x_framer_set(wc, 0, 0xd5, 0xe5);
  850. /* GPC4: Enable Multi Purpose Switches */
  851. __t43x_framer_set(wc, 0, 0xd6, 0x87);
  852. /* IPC: Interrupt push/pull active low, 8KHz ref clk */
  853. __t43x_framer_set(wc, 0, 0x08, 0x05);
  854. /* Global clocks (8.192 Mhz CLK) */
  855. __t43x_framer_set(wc, 0, 0x92, 0x00);
  856. __t43x_framer_set(wc, 0, 0x93, 0x18);
  857. __t43x_framer_set(wc, 0, 0x94, 0xfb);
  858. __t43x_framer_set(wc, 0, 0x95, 0x0b);
  859. __t43x_framer_set(wc, 0, 0x96, 0x00);
  860. __t43x_framer_set(wc, 0, 0x97, 0x0b);
  861. __t43x_framer_set(wc, 0, 0x98, 0xdb);
  862. __t43x_framer_set(wc, 0, 0x99, 0xdf);
  863. spin_unlock_irqrestore(&wc->reglock, flags);
  864. for (fidx = 0; fidx < 4; fidx++) {
  865. /* For dual-span, put the second and third framers
  866. * onto the first and second timeslots */
  867. if (wc->numspans == 2)
  868. slot = (fidx - 1) & 3;
  869. else
  870. slot = fidx;
  871. spin_lock_irqsave(&wc->reglock, flags);
  872. /* Configure interrupts */
  873. /* GCR: Interrupt on Activation/Deactivation of AIX, LOS */
  874. __t43x_framer_set(wc, fidx, 0x46, 0xc0);
  875. /* Configure system interface */
  876. /* SIC1: 8.192 Mhz clock/bus, double buffer receive /
  877. * transmit, byte interleaved */
  878. __t43x_framer_set(wc, fidx, 0x3e, 0xc2);
  879. /* SIC2: No FFS, no CRB, SSC2 = 0, phase = unit */
  880. __t43x_framer_set(wc, fidx, 0x3f, (slot << 1));
  881. /* SIC3: Edges for capture - original rx rising edge */
  882. __t43x_framer_set(wc, fidx, 0x40, 0x04);
  883. /* SIC4: CES high, SYPR opposite edge */
  884. __t43x_framer_set(wc, fidx, 0x2a, 0x06);
  885. #ifndef RPC_RCLK
  886. /* CMR1: RCLK is at 8.192 Mhz dejittered */
  887. __t43x_framer_set(wc, fidx, 0x44, 0x30);
  888. #else
  889. /* CMR1: RCLK is at 8.192 Mhz dejittered, ref clock is this
  890. * channel */
  891. __t43x_framer_set(wc, fidx, 0x44, 0x38 | (fidx << 6));
  892. #endif
  893. /* CMR2: We provide sync and clock for tx and rx. */
  894. __t43x_framer_set(wc, fidx, 0x45, 0x00);
  895. /* XC0: Normal operation of Sa-bits */
  896. __t43x_framer_set(wc, fidx, 0x22, 0x07);
  897. __t43x_framer_set(wc, fidx, 0x23, 0xfa); /* XC1 */
  898. __t43x_framer_set(wc, fidx, 0x24, 0x07); /* RC0 */
  899. __t43x_framer_set(wc, fidx, 0x25, 0xfa); /* RC1 */
  900. /* Configure ports */
  901. /* PC1: SPYR/SPYX input on RPA/XPA */
  902. __t43x_framer_set(wc, fidx, 0x80, 0x00);
  903. /* PC2: Unused stuff */
  904. __t43x_framer_set(wc, fidx, 0x81, 0xBB);
  905. #ifndef RPC_RCLK
  906. /* PC3: Unused stuff */
  907. __t43x_framer_set(wc, fidx, 0x82, 0xBB);
  908. #else
  909. /* PC3: RPC is RCLK, XPC is low output */
  910. __t43x_framer_set(wc, fidx, 0x82, 0xFB);
  911. #endif
  912. /* PC4: Unused stuff */
  913. __t43x_framer_set(wc, fidx, 0x83, 0xBB);
  914. /* PC5: XMFS active low, SCLKR is input, RCLK is output */
  915. __t43x_framer_set(wc, fidx, 0x84, 0x01);
  916. __t43x_framer_set(wc, fidx, 0x3b, 0x00); /* Clear LCR1 */
  917. /* Make sure unused ports are set to T1 configuration as a
  918. * default. This is required in order to make clock recovery
  919. * works on spans that are actually configured. */
  920. __t43x_framer_set(wc, fidx, 0x1c, 0xf0);
  921. __t43x_framer_set(wc, fidx, 0x1d, 0x9c);
  922. __t43x_framer_set(wc, fidx, 0x1e, 0x20);
  923. __t43x_framer_set(wc, fidx, 0x20, 0x0c);
  924. /* FMR5: Enable RBS mode */
  925. __t43x_framer_set(wc, fidx, 0x21, 0x40);
  926. __t43x_framer_set(wc, fidx, 0x36, 0x08);
  927. __t43x_framer_set(wc, fidx, 0x37, 0xf0);
  928. __t43x_framer_set(wc, fidx, 0x3a, 0x21);
  929. __t43x_framer_set(wc, fidx, 0x02, 0x50);
  930. __t43x_framer_set(wc, fidx, 0x02, 0x00);
  931. __t43x_framer_set(wc, fidx, 0x38, 0x0a);
  932. __t43x_framer_set(wc, fidx, 0x39, 0x15);
  933. /* Tri-state the TX output for the port */
  934. __t43x_framer_set(wc, fidx, 0x28, 0x40);
  935. spin_unlock_irqrestore(&wc->reglock, flags);
  936. }
  937. }
  938. /**
  939. * t43x_span_assigned - Called when the span is assigned by DAHDI.
  940. * @span: Span that has been assigned.
  941. *
  942. * When this function is called, the span has a valid spanno and all the
  943. * channels on the span have valid channel numbers assigned.
  944. *
  945. * This function is necessary because a device may be registered, and
  946. * then user space may then later decide to assign span numbers and the
  947. * channel numbers.
  948. *
  949. */
  950. static void t43x_span_assigned(struct dahdi_span *span)
  951. {
  952. struct t43x_span *tspan = container_of(span, struct t43x_span, span);
  953. struct t43x *wc = tspan->owner;
  954. struct dahdi_span *pos;
  955. unsigned int unassigned_spans = 0;
  956. if (debug)
  957. dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
  958. span->alarms = DAHDI_ALARM_NONE;
  959. /* We use this to make sure all the spans are assigned before
  960. * running the serial setup. */
  961. list_for_each_entry(pos, &wc->ddev->spans, device_node) {
  962. if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &pos->flags))
  963. ++unassigned_spans;
  964. }
  965. if (0 == unassigned_spans) {
  966. /* Now all the spans are assigned so we can go ahead and start
  967. * things up. */
  968. t43x_serial_setup(wc);
  969. }
  970. }
  971. static int syncsrc;
  972. static int syncnum;
  973. static int syncspan;
  974. static DEFINE_SPINLOCK(synclock);
  975. static void __t43x_set_rclk_src(struct t43x *wc, int span)
  976. {
  977. #ifndef RPC_RCLK
  978. int cmr1 = 0x38; /* Clock Mode: RCLK sourced by DCO-R1
  979. by default, Disable Clock-Switching */
  980. int fidx;
  981. if (2 == wc->numspans) {
  982. u8 reg;
  983. /* Since the clock always comes from the first span (which is
  984. * not connected to a physical port on the dual span) we can
  985. * always set the framer mode register to match whatever span
  986. * we're currently sourcing the timing from. This ensures that
  987. * the DCO-R is always expecting the right clock speed from the
  988. * line. */
  989. fidx = span+1;
  990. reg = __t43x_framer_get(wc, fidx, 0x1d);
  991. __t43x_framer_set(wc, 0, 0x1d, reg);
  992. reg = __t43x_framer_get(wc, fidx, 0x1e);
  993. __t43x_framer_set(wc, 0, 0x1e, reg);
  994. } else {
  995. fidx = span;
  996. }
  997. cmr1 |= (fidx) << 6;
  998. __t43x_framer_set(wc, 0, 0x44, cmr1);
  999. #else
  1000. int fidx = (2 == wc->numspans) ? span+1 : span;
  1001. int gpc1 = __t43x_framer_get(wc, 0, 0x85);
  1002. gpc1 &= ~3;
  1003. gpc1 |= fidx & 3;
  1004. t43x_framer_set(wc, 0, 0x85, gpc1);
  1005. #endif
  1006. dev_info(&wc->xb.pdev->dev, "RCLK source set to span %d\n", span+1);
  1007. }
  1008. /* This is called from the workqueue to wait for the TDM engine stop */
  1009. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  1010. static void t43x_clksrc_work_fn(void *data)
  1011. {
  1012. struct t43x_clksrc_work *work = data;
  1013. #else
  1014. static void t43x_clksrc_work_fn(struct work_struct *data)
  1015. {
  1016. struct t43x_clksrc_work *work = container_of(to_delayed_work(data),
  1017. struct t43x_clksrc_work, work);
  1018. #endif
  1019. struct t43x *wc = container_of(work, struct t43x, clksrc_work);
  1020. if (debug) {
  1021. dev_info(&wc->xb.pdev->dev,
  1022. "t43x_clksrc_work() called from queue\n");
  1023. }
  1024. if (wcxb_is_stopped(&wc->xb)) {
  1025. /* Set new clock select */
  1026. if (work->is_timing_master)
  1027. wcxb_enable_timing_header_driver(&wc->xb);
  1028. else
  1029. wcxb_disable_timing_header_driver(&wc->xb);
  1030. wcxb_set_clksrc(&wc->xb, work->clksrc);
  1031. /* Restart DMA processing */
  1032. wcxb_start(&wc->xb);
  1033. } else {
  1034. /* Stop DMA again in case DMA underrun int restarted it */
  1035. wcxb_stop_dma(&wc->xb);
  1036. queue_delayed_work(wc->wq, &work->work, msecs_to_jiffies(10));
  1037. }
  1038. }
  1039. /**
  1040. * __t43x_set_sclk_src - Change the current source of the source clock.
  1041. * @wc: The board to change the clock source on.
  1042. * @mode: The clock mode that we would like to move to.
  1043. * @master: If true, drive the clock on the timing header.
  1044. *
  1045. * The clock source cannot be changed while DMA is active, so this function
  1046. * will stop the DMA, then queue a delayed work item in order to come back and
  1047. * check that DMA was actually stopped before changing the source of the clock.
  1048. *
  1049. */
  1050. static void
  1051. __t43x_set_sclk_src(struct t43x *wc, enum wcxb_clock_sources mode, bool master)
  1052. {
  1053. struct t43x_clksrc_work *const work = &wc->clksrc_work;
  1054. unsigned long flags;
  1055. bool changed = false;
  1056. /* Cannot drive the clock on the header while also slaving from it. */
  1057. WARN_ON(master && (mode == WCXB_CLOCK_SLAVE));
  1058. spin_lock_irqsave(&work->lock, flags);
  1059. if (!delayed_work_pending(&work->work)) {
  1060. /* We want to check the actual settings. */
  1061. changed = (wcxb_get_clksrc(&wc->xb) != mode) ||
  1062. (wcxb_is_timing_header_driver_enabled(&wc->xb) != master);
  1063. } else {
  1064. /* Otherwise, we'll check if delayed work is going to set it to
  1065. * the same value. */
  1066. changed = (work->clksrc != mode) ||
  1067. (work->is_timing_master != master);
  1068. }
  1069. if (changed) {
  1070. work->clksrc = mode;
  1071. work->is_timing_master = master;
  1072. }
  1073. spin_unlock_irqrestore(&work->lock, flags);
  1074. if (!changed) {
  1075. if (debug)
  1076. dev_info(&wc->xb.pdev->dev, "Clock source is unchanged\n");
  1077. return;
  1078. }
  1079. wcxb_stop_dma(&wc->xb);
  1080. dev_dbg(&wc->xb.pdev->dev,
  1081. "Queueing delayed work for clock source change\n");
  1082. queue_delayed_work(wc->wq, &work->work, msecs_to_jiffies(10));
  1083. }
  1084. static ssize_t t43x_timing_master_show(struct device *dev,
  1085. struct device_attribute *attr,
  1086. char *buf)
  1087. {
  1088. struct t43x *wc = dev_get_drvdata(dev);
  1089. if (wcxb_is_timing_header_driver_enabled(&wc->xb))
  1090. return sprintf(buf, "%d\n", wc->syncsrc);
  1091. else
  1092. return sprintf(buf, "%d\n", -1);
  1093. }
  1094. static DEVICE_ATTR(timing_master, 0400, t43x_timing_master_show, NULL);
  1095. static void create_sysfs_files(struct t43x *wc)
  1096. {
  1097. int ret;
  1098. ret = device_create_file(&wc->xb.pdev->dev,
  1099. &dev_attr_timing_master);
  1100. if (ret) {
  1101. dev_info(&wc->xb.pdev->dev,
  1102. "Failed to create device attributes.\n");
  1103. }
  1104. }
  1105. static void remove_sysfs_files(struct t43x *wc)
  1106. {
  1107. device_remove_file(&wc->xb.pdev->dev, &dev_attr_timing_master);
  1108. }
  1109. static inline void __t43x_update_timing(struct t43x *wc)
  1110. {
  1111. int i;
  1112. /* update sync src info */
  1113. if (wc->syncsrc != syncsrc) {
  1114. dev_info(&wc->xb.pdev->dev, "Swapping card %d from %d to %d\n",
  1115. wc->num, wc->syncsrc, syncsrc);
  1116. wc->syncsrc = syncsrc;
  1117. /* Update sync sources */
  1118. for (i = 0; i < wc->numspans; i++)
  1119. wc->tspans[i]->span.syncsrc = wc->syncsrc;
  1120. if (syncnum == wc->num) {
  1121. __t43x_set_rclk_src(wc, syncspan-1);
  1122. __t43x_set_sclk_src(wc, WCXB_CLOCK_RECOVER, 1);
  1123. if (debug) {
  1124. dev_notice(&wc->xb.pdev->dev,
  1125. "Card %d, using sync span %d, master\n",
  1126. wc->num, syncspan);
  1127. }
  1128. } else {
  1129. __t43x_set_sclk_src(wc, WCXB_CLOCK_SLAVE, 0);
  1130. if (debug) {
  1131. dev_notice(&wc->xb.pdev->dev,
  1132. "Card %d, using Timing Bus, NOT master\n",
  1133. wc->num);
  1134. }
  1135. }
  1136. }
  1137. }
  1138. static int __t43x_findsync(struct t43x *wc)
  1139. {
  1140. int i;
  1141. unsigned long flags;
  1142. int p;
  1143. int nonzero;
  1144. int newsyncsrc = 0; /* DAHDI span number */
  1145. int newsyncnum = 0; /* wct4xxp card number */
  1146. int newsyncspan = 0; /* span on given wct4xxp card */
  1147. struct t43x *cur;
  1148. spin_lock_irqsave(&synclock, flags);
  1149. if (!wc->num) {
  1150. /* If we're the first card, go through all the motions, up to 8
  1151. * levels of sync source */
  1152. p = 1;
  1153. while (p < 8) {
  1154. nonzero = 0;
  1155. list_for_each_entry(cur, &card_list, card_node) {
  1156. for (i = 0; i < cur->numspans; i++) {
  1157. struct t43x_span *const ts =
  1158. cur->tspans[i];
  1159. struct dahdi_span *const s =
  1160. &cur->tspans[i]->span;
  1161. if (!ts->syncpos)
  1162. continue;
  1163. nonzero = 1;
  1164. if ((ts->syncpos == p) &&
  1165. !(s->alarms & NOSYNC_ALARMS) &&
  1166. (s->flags & DAHDI_FLAG_RUNNING)) {
  1167. /* This makes a good sync
  1168. * source */
  1169. newsyncsrc = s->spanno;
  1170. newsyncnum = cur->num;
  1171. newsyncspan = i + 1;
  1172. /* Jump out */
  1173. goto found;
  1174. }
  1175. }
  1176. }
  1177. if (nonzero)
  1178. p++;
  1179. else
  1180. break;
  1181. }
  1182. found:
  1183. if ((syncnum != newsyncnum) ||
  1184. (syncsrc != newsyncsrc) ||
  1185. (newsyncspan != syncspan)) {
  1186. if (debug) {
  1187. dev_notice(&wc->xb.pdev->dev,
  1188. "New syncnum: %d (was %d), syncsrc: %d (was %d), syncspan: %d (was %d)\n",
  1189. newsyncnum, syncnum, newsyncsrc,
  1190. syncsrc, newsyncspan, syncspan);
  1191. }
  1192. syncnum = newsyncnum;
  1193. syncsrc = newsyncsrc;
  1194. syncspan = newsyncspan;
  1195. nonzero = 0;
  1196. list_for_each_entry(cur, &card_list, card_node)
  1197. __t43x_update_timing(cur);
  1198. }
  1199. }
  1200. __t43x_update_timing(wc);
  1201. spin_unlock_irqrestore(&synclock, flags);
  1202. return 0;
  1203. }
  1204. static void __t43x_set_timing_source_auto(struct t43x *wc)
  1205. {
  1206. int x, i;
  1207. int firstprio, secondprio;
  1208. firstprio = secondprio = 4;
  1209. if (debug)
  1210. dev_info(&wc->xb.pdev->dev, "timing source auto\n");
  1211. if (timingcable) {
  1212. __t43x_findsync(wc);
  1213. } else {
  1214. if (debug)
  1215. dev_info(&wc->xb.pdev->dev,
  1216. "Evaluating spans for timing source\n");
  1217. for (x = 0; x < wc->numspans; x++) {
  1218. if ((wc->tspans[x]->span.flags & DAHDI_FLAG_RUNNING) &&
  1219. !(wc->tspans[x]->span.alarms & (DAHDI_ALARM_RED |
  1220. DAHDI_ALARM_BLUE))) {
  1221. if (debug) {
  1222. dev_info(&wc->xb.pdev->dev,
  1223. "span %d is green : syncpos %d\n",
  1224. x+1, wc->tspans[x]->syncpos);
  1225. }
  1226. if (wc->tspans[x]->syncpos) {
  1227. /* Valid rsync source in recovered
  1228. timing mode */
  1229. if (firstprio == 4)
  1230. firstprio = x;
  1231. else if (wc->tspans[x]->syncpos <
  1232. wc->tspans[firstprio]->syncpos)
  1233. firstprio = x;
  1234. } else {
  1235. /* Valid rsync source in system timing
  1236. mode */
  1237. if (secondprio == 4)
  1238. secondprio = x;
  1239. }
  1240. }
  1241. }
  1242. if (firstprio != 4) {
  1243. wc->syncsrc = firstprio;
  1244. __t43x_set_rclk_src(wc, firstprio);
  1245. __t43x_set_sclk_src(wc, WCXB_CLOCK_RECOVER, 0);
  1246. dev_info(&wc->xb.pdev->dev,
  1247. "Recovered timing mode, RCLK set to span %d\n",
  1248. firstprio+1);
  1249. } else if (secondprio != 4) {
  1250. wc->syncsrc = -1;
  1251. __t43x_set_rclk_src(wc, secondprio);
  1252. __t43x_set_sclk_src(wc, WCXB_CLOCK_SELF, 0);
  1253. dev_info(&wc->xb.pdev->dev,
  1254. "System timing mode, RCLK set to span %d\n",
  1255. secondprio+1);
  1256. } else {
  1257. wc->syncsrc = -1;
  1258. dev_info(&wc->xb.pdev->dev,
  1259. "All spans in alarm : No valid span to source RCLK from\n");
  1260. /* Default rclk to lock with span 1 */
  1261. __t43x_set_rclk_src(wc, 0);
  1262. __t43x_set_sclk_src(wc, WCXB_CLOCK_SELF, 0);
  1263. }
  1264. /* Propagate sync selection to dahdi_span struct
  1265. * this is read by dahdi_tool to display the span's
  1266. * master/slave sync information */
  1267. for (i = 0; i < wc->numspans; i++)
  1268. wc->tspans[i]->span.syncsrc = wc->syncsrc + 1;
  1269. }
  1270. }
  1271. static void
  1272. t43x_configure_t1(struct t43x *wc, int span_idx, int lineconfig, int txlevel)
  1273. {
  1274. struct t43x_span *ts = wc->tspans[span_idx];
  1275. int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
  1276. unsigned int fmr4, fmr2, fmr1, fmr0, lim2;
  1277. char *framing, *line;
  1278. int mytxlevel, reg;
  1279. unsigned long flags;
  1280. if ((txlevel > 7) || (txlevel < 4))
  1281. mytxlevel = 0;
  1282. else
  1283. mytxlevel = txlevel - 4;
  1284. /* FMR1: Mode 0, T1 mode, CRC on for ESF, 8.192 Mhz system data rate,
  1285. * no XAIS */
  1286. fmr1 = 0x9c;
  1287. /* FMR2: no payload loopback, don't auto yellow alarm */
  1288. fmr2 = 0x20;
  1289. if (SPANTYPE_DIGITAL_J1 == ts->span.spantype) {
  1290. fmr4 = 0x1c;
  1291. } else {
  1292. /* FMR4: Lose sync on 2 out of 5 framing bits, auto resync */
  1293. fmr4 = 0x0c;
  1294. }
  1295. /* LIM2: 50% peak is a "1", Advanced Loss Recovery, Multi Purpose
  1296. * Analog Switch enabled */
  1297. lim2 = 0x23;
  1298. /* LIM2: Add line buildout */
  1299. lim2 |= (mytxlevel << 6);
  1300. spin_lock_irqsave(&wc->reglock, flags);
  1301. __t43x_framer_set(wc, fidx, 0x1d, fmr1);
  1302. __t43x_framer_set(wc, fidx, 0x1e, fmr2);
  1303. /* Configure line interface */
  1304. if (lineconfig & DAHDI_CONFIG_AMI) {
  1305. line = "AMI";
  1306. /* WCT4XX has workaround for errata fmr0 = 0xb0 */
  1307. /* was fmr0 = 0xa0; */
  1308. fmr0 = 0xb0;
  1309. } else {
  1310. line = "B8ZS";
  1311. fmr0 = 0xf0;
  1312. }
  1313. if (lineconfig & DAHDI_CONFIG_D4) {
  1314. framing = "D4";
  1315. } else {
  1316. framing = "ESF";
  1317. fmr4 |= 0x2;
  1318. fmr2 |= 0xc0;
  1319. }
  1320. /* Suppress RSC interrupt for cleared channels */
  1321. __t43x_framer_set(wc, fidx, 0x09, 0x80);
  1322. __t43x_framer_set(wc, fidx, 0x1c, fmr0);
  1323. __t43x_framer_set(wc, fidx, 0x20, fmr4);
  1324. /* FMR5: Enable RBS mode */
  1325. __t43x_framer_set(wc, fidx, 0x21, 0x40);
  1326. /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No
  1327. * remote loop, no DRS */
  1328. __t43x_framer_set(wc, fidx, 0x37, 0xf0);
  1329. /* LIM0: Enable auto long haul mode, no local loop (set after LIM1) */
  1330. __t43x_framer_set(wc, fidx, 0x36, 0x08);
  1331. /* CMDR: Reset the receiver and transmitter line interface */
  1332. __t43x_framer_set(wc, fidx, 0x02, 0x50);
  1333. /* CMDR: Reset the receiver and transmitter line interface */
  1334. __t43x_framer_set(wc, fidx, 0x02, 0x00);
  1335. if (debug) {
  1336. dev_info(&wc->xb.pdev->dev,
  1337. "card %d span %d: setting Rtx to 0ohm for T1\n",
  1338. wc->num, span_idx);
  1339. }
  1340. /* PC6: set Rtx to 0ohm for T1 */
  1341. __t43x_framer_set(wc, fidx, 0x86, 0x00);
  1342. /* Bugfix register for errata #3 */
  1343. __t43x_framer_set(wc, fidx, 0xbd, 0x05);
  1344. /* LIM2: 50% peak amplitude is a "1" */
  1345. __t43x_framer_set(wc, fidx, 0x3a, lim2);
  1346. /* PCD: LOS after 176 consecutive "zeros" */
  1347. __t43x_framer_set(wc, fidx, 0x38, 0x0a);
  1348. /* PCR: 22 "ones" clear LOS */
  1349. __t43x_framer_set(wc, fidx, 0x39, 0x15);
  1350. reg = __t43x_framer_get(wc, fidx, 0x24);
  1351. if (SPANTYPE_DIGITAL_J1 == ts->span.spantype) {
  1352. /* set J1 overide */
  1353. __t43x_framer_set(wc, fidx, 0x24, reg | 0x80);
  1354. } else {
  1355. /* clear J1 overide */
  1356. __t43x_framer_set(wc, fidx, 0x24, reg & ~0x80);
  1357. }
  1358. /* Generate pulse mask for T1 */
  1359. switch (mytxlevel) {
  1360. case 3:
  1361. __t43x_framer_set(wc, fidx, 0x26, 0x07); /* XPM0 */
  1362. __t43x_framer_set(wc, fidx, 0x27, 0x01); /* XPM1 */
  1363. __t43x_framer_set(wc, fidx, 0x28, 0x00); /* XPM2 */
  1364. break;
  1365. case 2:
  1366. __t43x_framer_set(wc, fidx, 0x26, 0x8c); /* XPM0 */
  1367. __t43x_framer_set(wc, fidx, 0x27, 0x11); /* XPM1 */
  1368. __t43x_framer_set(wc, fidx, 0x28, 0x01); /* XPM2 */
  1369. break;
  1370. case 1:
  1371. __t43x_framer_set(wc, fidx, 0x26, 0x8c); /* XPM0 */
  1372. __t43x_framer_set(wc, fidx, 0x27, 0x01); /* XPM1 */
  1373. __t43x_framer_set(wc, fidx, 0x28, 0x00); /* XPM2 */
  1374. break;
  1375. case 0:
  1376. default:
  1377. __t43x_framer_set(wc, fidx, 0x26, 0x1a); /* XPM0 */
  1378. __t43x_framer_set(wc, fidx, 0x27, 0x27); /* XPM1 */
  1379. __t43x_framer_set(wc, fidx, 0x28, 0x01); /* XPM2 */
  1380. break;
  1381. }
  1382. __t43x_framer_set(wc, fidx, 0x14, 0xff); /* IMR0 */
  1383. __t43x_framer_set(wc, fidx, 0x15, 0xff); /* IMR1 */
  1384. /* IMR2: All the alarms */
  1385. __t43x_framer_set(wc, fidx, 0x16, 0x00);
  1386. /* IMR3: ES, SEC, LLBSC, rx slips */
  1387. __t43x_framer_set(wc, fidx, 0x17, 0x34);
  1388. /* IMR4: Slips on transmit */
  1389. __t43x_framer_set(wc, fidx, 0x18, 0x3f);
  1390. spin_unlock_irqrestore(&wc->reglock, flags);
  1391. dev_info(&wc->xb.pdev->dev, "Span %d configured for %s/%s\n",
  1392. span_idx, framing, line);
  1393. }
  1394. static void t43x_configure_e1(struct t43x *wc, int span_idx, int lineconfig)
  1395. {
  1396. int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
  1397. unsigned int fmr2, fmr1, fmr0;
  1398. unsigned int cas = 0;
  1399. unsigned int imr3extra = 0;
  1400. char *crc4 = "";
  1401. char *framing, *line;
  1402. unsigned long flags;
  1403. fmr1 = 0x44; /* FMR1: E1 mode, Automatic force resync, PCM30 mode,
  1404. 8.192 Mhz backplane, no XAIS */
  1405. fmr2 = 0x03; /* FMR2: Auto transmit remote alarm, auto loss of
  1406. multiframe recovery, no payload loopback */
  1407. if (lineconfig & DAHDI_CONFIG_CRC4) {
  1408. fmr1 |= 0x08; /* CRC4 transmit */
  1409. fmr2 |= 0xc0; /* CRC4 receive */
  1410. crc4 = "/CRC4";
  1411. }
  1412. spin_lock_irqsave(&wc->reglock, flags);
  1413. __t43x_framer_set(wc, fidx, 0x1d, fmr1);
  1414. __t43x_framer_set(wc, fidx, 0x1e, fmr2);
  1415. /* Configure line interface */
  1416. if (lineconfig & DAHDI_CONFIG_AMI) {
  1417. line = "AMI";
  1418. /* workaround for errata #2 in ES v3 09-10-16 */
  1419. fmr0 = 0xb0;
  1420. } else {
  1421. line = "HDB3";
  1422. fmr0 = 0xf0;
  1423. }
  1424. if (lineconfig & DAHDI_CONFIG_CCS) {
  1425. framing = "CCS";
  1426. imr3extra = 0x28;
  1427. } else {
  1428. framing = "CAS";
  1429. cas = 0x40;
  1430. }
  1431. __t43x_framer_set(wc, fidx, 0x1c, fmr0);
  1432. /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No
  1433. * remote loop, no DRS */
  1434. __t43x_framer_set(wc, fidx, 0x37, 0xf0);
  1435. /* LIM0: Enable auto long haul mode, no local loop (must be after
  1436. * LIM1) */
  1437. __t43x_framer_set(wc, fidx, 0x36, 0x08);
  1438. /* CMDR: Reset the receiver and transmitter line interface */
  1439. __t43x_framer_set(wc, fidx, 0x02, 0x50);
  1440. /* CMDR: Reset the receiver and transmitter line interface */
  1441. __t43x_framer_set(wc, fidx, 0x02, 0x00);
  1442. if (debug)
  1443. dev_info(&wc->xb.pdev->dev,
  1444. "setting Rtx to 7.5ohm for E1\n");
  1445. /* PC6: turn on 7.5ohm Rtx for E1 */
  1446. __t43x_framer_set(wc, fidx, 0x86, 0x40);
  1447. /* Condition receive line interface for E1 after reset */
  1448. __t43x_framer_set(wc, fidx, 0xbb, 0x17);
  1449. __t43x_framer_set(wc, fidx, 0xbc, 0x55);
  1450. __t43x_framer_set(wc, fidx, 0xbb, 0x97);
  1451. __t43x_framer_set(wc, fidx, 0xbb, 0x11);
  1452. __t43x_framer_set(wc, fidx, 0xbc, 0xaa);
  1453. __t43x_framer_set(wc, fidx, 0xbb, 0x91);
  1454. __t43x_framer_set(wc, fidx, 0xbb, 0x12);
  1455. __t43x_framer_set(wc, fidx, 0xbc, 0x55);
  1456. __t43x_framer_set(wc, fidx, 0xbb, 0x92);
  1457. __t43x_framer_set(wc, fidx, 0xbb, 0x0c);
  1458. __t43x_framer_set(wc, fidx, 0xbb, 0x00);
  1459. __t43x_framer_set(wc, fidx, 0xbb, 0x8c);
  1460. /* LIM2: 50% peak amplitude isa "1", Multi Purpose Analog Switch
  1461. * enabled */
  1462. __t43x_framer_set(wc, fidx, 0x3a, 0x22);
  1463. /* PCD: LOS after 176 consecutive "zeros" */
  1464. __t43x_framer_set(wc, fidx, 0x38, 0x0a);
  1465. /* PCR: 22 "ones" clear LOS */
  1466. __t43x_framer_set(wc, fidx, 0x39, 0x15);
  1467. /* XSW: Spare bits all to 1 */
  1468. __t43x_framer_set(wc, fidx, 0x20, 0x9f);
  1469. /* XSP: E-bit set when async. AXS auto, XSIF to 1 */
  1470. __t43x_framer_set(wc, fidx, 0x21, 0x1c|cas);
  1471. /* Generate pulse mask for E1 */
  1472. __t43x_framer_set(wc, fidx, 0x26, 0x74); /* XPM0 */
  1473. __t43x_framer_set(wc, fidx, 0x27, 0x02); /* XPM1 */
  1474. __t43x_framer_set(wc, fidx, 0x28, 0x00); /* XPM2 */
  1475. __t43x_framer_set(wc, fidx, 0x14, 0xff); /* IMR0 */
  1476. __t43x_framer_set(wc, fidx, 0x15, 0xff); /* IMR1 */
  1477. __t43x_framer_set(wc, fidx, 0x16, 0x00); /* IMR2: the alarm stuff! */
  1478. __t43x_framer_set(wc, fidx, 0x17, 0x04 | imr3extra); /* IMR3: AIS */
  1479. __t43x_framer_set(wc, fidx, 0x18, 0x3f); /* IMR4: slips on transmit */
  1480. spin_unlock_irqrestore(&wc->reglock, flags);
  1481. dev_info(&wc->xb.pdev->dev, "Span configured for %s/%s%s\n",
  1482. framing, line, crc4);
  1483. }
  1484. static bool have_open_channels(const struct t43x_span *ts)
  1485. {
  1486. int x, j;
  1487. for (x = 0, j = 0; x < ts->span.channels; x++) {
  1488. const struct dahdi_chan *chan = ts->span.chans[x];
  1489. if (test_bit(DAHDI_FLAGBIT_OPEN, &chan->flags) ||
  1490. dahdi_have_netdev(chan))
  1491. return true;
  1492. }
  1493. return false;
  1494. }
  1495. static void t43x_framer_start(struct t43x *wc)
  1496. {
  1497. int unit;
  1498. unsigned long flags;
  1499. int res;
  1500. if (debug)
  1501. dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
  1502. /* Disable fpga hardware interrupts */
  1503. wcxb_disable_interrupts(&wc->xb);
  1504. /* Disable DMA */
  1505. wcxb_stop_dma(&wc->xb);
  1506. res = wcxb_wait_for_stop(&wc->xb, 50);
  1507. if (res)
  1508. dev_warn(&wc->xb.pdev->dev, "DMA engine did not stop.\n");
  1509. for (unit = 0; unit < wc->numspans; unit++) {
  1510. struct t43x_span *ts = wc->tspans[unit];
  1511. if (dahdi_is_e1_span(&ts->span)) {
  1512. t43x_configure_e1(wc, unit, ts->span.lineconfig);
  1513. } else { /* is a T1 card */
  1514. t43x_configure_t1(wc, unit, ts->span.lineconfig,
  1515. ts->span.txlevel);
  1516. }
  1517. t43x_set_cas_mode(wc, unit);
  1518. set_bit(DAHDI_FLAGBIT_RUNNING, &ts->span.flags);
  1519. /* Reset span alarm state to RED to prevent false
  1520. * temporary GREEN state on span bringup */
  1521. ts->span.alarms |= DAHDI_ALARM_RED;
  1522. }
  1523. for (unit = 0; unit < wc->numspans; unit++) {
  1524. /* Get this party started */
  1525. struct t43x_span *ts = wc->tspans[unit];
  1526. /* Check for "open channels" here in case some channels have
  1527. * netdev. */
  1528. if (have_open_channels(ts))
  1529. clear_bit(HAVE_OPEN_CHANNELS_FLAGBIT, &ts->bit_flags);
  1530. else
  1531. set_bit(HAVE_OPEN_CHANNELS_FLAGBIT, &ts->bit_flags);
  1532. local_irq_save(flags);
  1533. t43x_check_alarms(wc, unit);
  1534. t43x_check_sigbits(wc, unit);
  1535. local_irq_restore(flags);
  1536. }
  1537. dev_info(&wc->xb.pdev->dev, "Enabling DMA controller and interrupts\n");
  1538. /* start interrupts and DMA processing */
  1539. wcxb_start(&wc->xb);
  1540. t43x_check_for_interrupts(wc);
  1541. /* force re-evaluation of timing source */
  1542. wc->syncsrc = -1;
  1543. spin_lock_irqsave(&wc->reglock, flags);
  1544. __t43x_set_timing_source_auto(wc);
  1545. spin_unlock_irqrestore(&wc->reglock, flags);
  1546. /* Clear all counters */
  1547. for (unit = 0; unit < wc->numspans; unit++) {
  1548. struct t43x_span *ts = wc->tspans[unit];
  1549. memset(&ts->span.count, 0, sizeof(ts->span.count));
  1550. }
  1551. /* Invoke timer function to set leds */
  1552. mod_timer(&wc->timer, jiffies);
  1553. }
  1554. #ifndef RPC_RCLK
  1555. /**
  1556. * t43x_check_spanconfig - Return 0 if the span configuration is valid.
  1557. * @wc - The card to check.
  1558. *
  1559. * The TE435 cannot sync timing from a span in a different line mode than the
  1560. * first span. This function should be called after the spans are configured to
  1561. * ensure that the are not configured in this mode.
  1562. *
  1563. */
  1564. static int t43x_check_spanconfig(const struct t43x *wc)
  1565. {
  1566. unsigned int i;
  1567. bool span_1_is_e1;
  1568. if (&te435 != wc->devtype)
  1569. return 0;
  1570. span_1_is_e1 = dahdi_is_e1_span(&wc->tspans[0]->span);
  1571. for (i = 1; i < wc->numspans; ++i) {
  1572. struct t43x_span *ts = wc->tspans[i];
  1573. /* We only need to check spans that we could be a sync source */
  1574. if (!ts->syncpos)
  1575. continue;
  1576. if ((bool)dahdi_is_e1_span(&ts->span) == span_1_is_e1)
  1577. continue;
  1578. dev_warn(&wc->xb.pdev->dev,
  1579. "Local span %d is configured as a sync source but the line mode does not match local span 1.\n",
  1580. ts->span.offset + 1);
  1581. dev_warn(&wc->xb.pdev->dev,
  1582. "Please configure local span 1 as a sync src and ensure all other local sync sources match the line config of span 1.\n");
  1583. return -EINVAL;
  1584. }
  1585. return 0;
  1586. }
  1587. #endif
  1588. static int t43x_startup(struct file *file, struct dahdi_span *span)
  1589. {
  1590. struct t43x_span *ts = container_of(span, struct t43x_span, span);
  1591. struct t43x *wc = ts->owner;
  1592. if (debug)
  1593. dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
  1594. #ifndef RPC_RCLK
  1595. if (t43x_check_spanconfig(wc))
  1596. return -EINVAL;
  1597. #endif
  1598. /* Reset framer with proper parameters and start */
  1599. dev_info(&wc->xb.pdev->dev,
  1600. "Calling startup (flags is %lu)\n", span->flags);
  1601. t43x_framer_start(wc);
  1602. return 0;
  1603. }
  1604. static inline bool is_initialized(struct t43x *wc)
  1605. {
  1606. WARN_ON(wc->not_ready < 0);
  1607. return (wc->not_ready == 0);
  1608. }
  1609. /**
  1610. * t43x_wait_for_ready
  1611. *
  1612. * Check if the board has finished any setup and is ready to start processing
  1613. * calls.
  1614. */
  1615. static int t43x_wait_for_ready(struct t43x *wc)
  1616. {
  1617. while (!is_initialized(wc)) {
  1618. if (fatal_signal_pending(current))
  1619. return -EIO;
  1620. msleep_interruptible(250);
  1621. }
  1622. return 0;
  1623. }
  1624. static int t43x_chanconfig(struct file *file,
  1625. struct dahdi_chan *chan, int sigtype)
  1626. {
  1627. struct t43x *wc = chan->pvt;
  1628. if (file->f_flags & O_NONBLOCK && !is_initialized(wc))
  1629. return -EAGAIN;
  1630. else
  1631. t43x_wait_for_ready(wc);
  1632. if (test_bit(DAHDI_FLAGBIT_RUNNING, &chan->span->flags))
  1633. t43x_set_cas_mode(wc, chan->span->offset);
  1634. return 0;
  1635. }
  1636. static int t43x_rbsbits(struct dahdi_chan *chan, int bits)
  1637. {
  1638. u_char m, c;
  1639. int n, b;
  1640. struct t43x *wc = chan->pvt;
  1641. struct t43x_span *ts = container_of(chan->span, struct t43x_span, span);
  1642. int fidx = (2 == wc->numspans) ?
  1643. chan->span->offset+1 : chan->span->offset;
  1644. unsigned long flags;
  1645. if (dahdi_is_e1_span(&ts->span)) { /* do it E1 way */
  1646. if (chan->chanpos == 16)
  1647. return 0;
  1648. n = chan->chanpos - 1;
  1649. if (chan->chanpos > 15)
  1650. n--;
  1651. b = (n % 15);
  1652. spin_lock_irqsave(&wc->reglock, flags);
  1653. c = ts->txsigs[b];
  1654. m = (n / 15) << 2; /* nibble selector */
  1655. c &= (0xf << m); /* keep the other nibble */
  1656. c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
  1657. ts->txsigs[b] = c;
  1658. /* output them to the chip */
  1659. __t43x_framer_set(wc, fidx, 0x71 + b, c);
  1660. spin_unlock_irqrestore(&wc->reglock, flags);
  1661. } else if (ts->span.lineconfig & DAHDI_CONFIG_D4) {
  1662. n = chan->chanpos - 1;
  1663. b = (n / 4);
  1664. spin_lock_irqsave(&wc->reglock, flags);
  1665. c = ts->txsigs[b];
  1666. m = ((3 - (n % 4)) << 1); /* nibble selector */
  1667. c &= ~(0x3 << m); /* keep the other nibble */
  1668. c |= ((bits >> 2) & 0x3) << m; /* put our new nibble here */
  1669. ts->txsigs[b] = c;
  1670. /* output them to the chip */
  1671. __t43x_framer_set(wc, fidx, 0x70 + b, c);
  1672. __t43x_framer_set(wc, fidx, 0x70 + b + 6, c);
  1673. spin_unlock_irqrestore(&wc->reglock, flags);
  1674. } else if (ts->span.lineconfig & DAHDI_CONFIG_ESF) {
  1675. n = chan->chanpos - 1;
  1676. b = (n / 2);
  1677. spin_lock_irqsave(&wc->reglock, flags);
  1678. c = ts->txsigs[b];
  1679. m = ((n % 2) << 2); /* nibble selector */
  1680. c &= (0xf << m); /* keep the other nibble */
  1681. c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
  1682. ts->txsigs[b] = c;
  1683. /* output them to the chip */
  1684. __t43x_framer_set(wc, fidx, 0x70 + b, c);
  1685. spin_unlock_irqrestore(&wc->reglock, flags);
  1686. }
  1687. return 0;
  1688. }
  1689. static inline void t43x_dahdi_rbsbits(struct dahdi_chan *c, const int rxs)
  1690. {
  1691. if (!(c->sig & DAHDI_SIG_CLEAR) && (c->rxsig != rxs))
  1692. dahdi_rbsbits(c, rxs);
  1693. }
  1694. static void t43x_check_sigbits(struct t43x *wc, int span_idx)
  1695. {
  1696. int a, i, rxs;
  1697. struct t43x_span *ts = wc->tspans[span_idx];
  1698. int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
  1699. if (dahdi_is_e1_span(&ts->span)) {
  1700. for (i = 0; i < 15; i++) {
  1701. a = t43x_framer_get(wc, fidx, 0x71 + i);
  1702. rxs = (a & 0xf);
  1703. t43x_dahdi_rbsbits(ts->span.chans[i+16], rxs);
  1704. rxs = (a >> 4) & 0xf;
  1705. t43x_dahdi_rbsbits(ts->span.chans[i], rxs);
  1706. }
  1707. } else if (ts->span.lineconfig & DAHDI_CONFIG_D4) {
  1708. for (i = 0; i < 24; i += 4) {
  1709. a = t43x_framer_get(wc, fidx, 0x70 + (i>>2));
  1710. rxs = (a & 0x3) << 2;
  1711. t43x_dahdi_rbsbits(ts->span.chans[i+3], rxs);
  1712. rxs = (a & 0xc);
  1713. t43x_dahdi_rbsbits(ts->span.chans[i+2], rxs);
  1714. rxs = (a >> 2) & 0xc;
  1715. t43x_dahdi_rbsbits(ts->span.chans[i+1], rxs);
  1716. rxs = (a >> 4) & 0xc;
  1717. t43x_dahdi_rbsbits(ts->span.chans[i], rxs);
  1718. }
  1719. } else {
  1720. for (i = 0; i < 24; i += 2) {
  1721. a = t43x_framer_get(wc, fidx, 0x70 + (i>>1));
  1722. rxs = (a & 0xf);
  1723. t43x_dahdi_rbsbits(ts->span.chans[i+1], rxs);
  1724. rxs = (a >> 4) & 0xf;
  1725. t43x_dahdi_rbsbits(ts->span.chans[i], rxs);
  1726. }
  1727. }
  1728. }
  1729. struct maint_work_struct {
  1730. struct work_struct work;
  1731. struct t43x *wc;
  1732. int cmd;
  1733. struct dahdi_span *span;
  1734. };
  1735. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  1736. static void t43x_maint_work(void *data)
  1737. {
  1738. struct maint_work_struct *w = data;
  1739. #else
  1740. static void t43x_maint_work(struct work_struct *work)
  1741. {
  1742. struct maint_work_struct *w = container_of(work,
  1743. struct maint_work_struct, work);
  1744. #endif
  1745. struct t43x *wc = w->wc;
  1746. struct dahdi_span *span = w->span;
  1747. int reg = 0;
  1748. int cmd = w->cmd;
  1749. unsigned long flags;
  1750. int fidx = (wc->numspans == 2) ? span->offset+1 : span->offset;
  1751. if (dahdi_is_e1_span(span)) {
  1752. switch (cmd) {
  1753. case DAHDI_MAINT_NONE:
  1754. dev_info(&wc->xb.pdev->dev,
  1755. "Clearing all maint modes\n");
  1756. t43x_clear_maint(span);
  1757. break;
  1758. case DAHDI_MAINT_LOCALLOOP:
  1759. dev_info(&wc->xb.pdev->dev,
  1760. "Turning on local loopback\n");
  1761. t43x_clear_maint(span);
  1762. spin_lock_irqsave(&wc->reglock, flags);
  1763. reg = __t43x_framer_get(wc, fidx, LIM0);
  1764. if (reg < 0) {
  1765. spin_unlock_irqrestore(&wc->reglock, flags);
  1766. goto cleanup;
  1767. }
  1768. __t43x_framer_set(wc, fidx, LIM0, reg | LIM0_LL);
  1769. spin_unlock_irqrestore(&wc->reglock, flags);
  1770. break;
  1771. case DAHDI_MAINT_NETWORKLINELOOP:
  1772. dev_info(&wc->xb.pdev->dev,
  1773. "Turning on network line loopback\n");
  1774. t43x_clear_maint(span);
  1775. spin_lock_irqsave(&wc->reglock, flags);
  1776. reg = __t43x_framer_get(wc, fidx, LIM1);
  1777. if (reg < 0) {
  1778. spin_unlock_irqrestore(&wc->reglock, flags);
  1779. goto cleanup;
  1780. }
  1781. __t43x_framer_set(wc, fidx, LIM1, reg | LIM1_RL);
  1782. spin_unlock_irqrestore(&wc->reglock, flags);
  1783. break;
  1784. case DAHDI_MAINT_NETWORKPAYLOADLOOP:
  1785. dev_info(&wc->xb.pdev->dev,
  1786. "Turning on network payload loopback\n");
  1787. t43x_clear_maint(span);
  1788. spin_lock_irqsave(&wc->reglock, flags);
  1789. reg = __t43x_framer_get(wc, fidx, LIM1);
  1790. if (reg < 0) {
  1791. spin_unlock_irqrestore(&wc->reglock, flags);
  1792. goto cleanup;
  1793. }
  1794. __t43x_framer_set(wc, fidx, LIM1,
  1795. reg | (LIM1_RL | LIM1_JATT));
  1796. spin_unlock_irqrestore(&wc->reglock, flags);
  1797. break;
  1798. case DAHDI_MAINT_FAS_DEFECT:
  1799. t43x_framer_set(wc, fidx, 0x1b, (1 << 5));
  1800. break;
  1801. case DAHDI_MAINT_MULTI_DEFECT:
  1802. t43x_framer_set(wc, fidx, 0x1b, (1 << 4));
  1803. break;
  1804. case DAHDI_MAINT_CRC_DEFECT:
  1805. t43x_framer_set(wc, fidx, 0x1b, (1 << 3));
  1806. break;
  1807. case DAHDI_MAINT_CAS_DEFECT:
  1808. t43x_framer_set(wc, fidx, 0x1b, (1 << 2));
  1809. break;
  1810. case DAHDI_MAINT_PRBS_DEFECT:
  1811. t43x_framer_set(wc, fidx, 0x1b, (1 << 1));
  1812. break;
  1813. case DAHDI_MAINT_BIPOLAR_DEFECT:
  1814. t43x_framer_set(wc, fidx, 0x1b, (1 << 0));
  1815. break;
  1816. default:
  1817. dev_info(&wc->xb.pdev->dev,
  1818. "Unknown E1 maint command: %d\n", cmd);
  1819. goto cleanup;
  1820. }
  1821. } else {
  1822. switch (cmd) {
  1823. case DAHDI_MAINT_NONE:
  1824. dev_info(&wc->xb.pdev->dev,
  1825. "Clearing all maint modes\n");
  1826. t43x_clear_maint(span);
  1827. break;
  1828. case DAHDI_MAINT_LOCALLOOP:
  1829. dev_info(&wc->xb.pdev->dev,
  1830. "Turning on local loopback\n");
  1831. t43x_clear_maint(span);
  1832. spin_lock_irqsave(&wc->reglock, flags);
  1833. reg = __t43x_framer_get(wc, fidx, LIM0);
  1834. if (reg < 0) {
  1835. spin_unlock_irqrestore(&wc->reglock, flags);
  1836. goto cleanup;
  1837. }
  1838. __t43x_framer_set(wc, fidx, LIM0, reg | LIM0_LL);
  1839. spin_unlock_irqrestore(&wc->reglock, flags);
  1840. break;
  1841. case DAHDI_MAINT_NETWORKLINELOOP:
  1842. dev_info(&wc->xb.pdev->dev,
  1843. "Turning on network line loopback\n");
  1844. t43x_clear_maint(span);
  1845. spin_lock_irqsave(&wc->reglock, flags);
  1846. reg = __t43x_framer_get(wc, fidx, LIM1);
  1847. if (reg < 0) {
  1848. spin_unlock_irqrestore(&wc->reglock, flags);
  1849. goto cleanup;
  1850. }
  1851. __t43x_framer_set(wc, fidx, LIM1, reg | LIM1_RL);
  1852. spin_unlock_irqrestore(&wc->reglock, flags);
  1853. break;
  1854. case DAHDI_MAINT_NETWORKPAYLOADLOOP:
  1855. dev_info(&wc->xb.pdev->dev,
  1856. "Turning on network payload loopback\n");
  1857. t43x_clear_maint(span);
  1858. spin_lock_irqsave(&wc->reglock, flags);
  1859. reg = __t43x_framer_get(wc, fidx, LIM1);
  1860. if (reg < 0) {
  1861. spin_unlock_irqrestore(&wc->reglock, flags);
  1862. goto cleanup;
  1863. }
  1864. __t43x_framer_set(wc, fidx, LIM1,
  1865. reg | (LIM1_RL | LIM1_JATT));
  1866. spin_unlock_irqrestore(&wc->reglock, flags);
  1867. break;
  1868. case DAHDI_MAINT_LOOPUP:
  1869. dev_info(&wc->xb.pdev->dev,
  1870. "Transmitting loopup code\n");
  1871. t43x_clear_maint(span);
  1872. t43x_framer_set(wc, fidx, 0x21, 0x50);
  1873. break;
  1874. case DAHDI_MAINT_LOOPDOWN:
  1875. dev_info(&wc->xb.pdev->dev,
  1876. "Transmitting loopdown code\n");
  1877. t43x_clear_maint(span);
  1878. t43x_framer_set(wc, fidx, 0x21, 0x60);
  1879. break;
  1880. case DAHDI_MAINT_FAS_DEFECT:
  1881. t43x_framer_set(wc, fidx, 0x1b, (1 << 5));
  1882. break;
  1883. case DAHDI_MAINT_MULTI_DEFECT:
  1884. t43x_framer_set(wc, fidx, 0x1b, (1 << 4));
  1885. break;
  1886. case DAHDI_MAINT_CRC_DEFECT:
  1887. t43x_framer_set(wc, fidx, 0x1b, (1 << 3));
  1888. break;
  1889. case DAHDI_MAINT_CAS_DEFECT:
  1890. t43x_framer_set(wc, fidx, 0x1b, (1 << 2));
  1891. break;
  1892. case DAHDI_MAINT_PRBS_DEFECT:
  1893. t43x_framer_set(wc, fidx, 0x1b, (1 << 1));
  1894. break;
  1895. case DAHDI_MAINT_BIPOLAR_DEFECT:
  1896. t43x_framer_set(wc, fidx, 0x1b, (1 << 0));
  1897. break;
  1898. default:
  1899. dev_info(&wc->xb.pdev->dev,
  1900. "Unknown T1 maint command: %d\n", cmd);
  1901. return;
  1902. }
  1903. }
  1904. /* update DAHDI_ALARM_LOOPBACK status bit and check timing source */
  1905. spin_lock_irqsave(&wc->reglock, flags);
  1906. if (!span->maintstat)
  1907. span->alarms &= ~DAHDI_ALARM_LOOPBACK;
  1908. dahdi_alarm_notify(span);
  1909. __t43x_set_timing_source_auto(wc);
  1910. spin_unlock_irqrestore(&wc->reglock, flags);
  1911. cleanup:
  1912. kfree(w);
  1913. return;
  1914. }
  1915. static int t43x_reset_counters(struct dahdi_span *span)
  1916. {
  1917. struct t43x_span *ts = container_of(span, struct t43x_span, span);
  1918. memset(&ts->span.count, 0, sizeof(ts->span.count));
  1919. return 0;
  1920. }
  1921. static int t43x_maint(struct dahdi_span *span, int cmd)
  1922. {
  1923. struct maint_work_struct *work;
  1924. struct t43x_span *ts = container_of(span, struct t43x_span, span);
  1925. struct t43x *wc = ts->owner;
  1926. if (dahdi_is_e1_span(span)) {
  1927. switch (cmd) {
  1928. case DAHDI_MAINT_NONE:
  1929. case DAHDI_MAINT_LOCALLOOP:
  1930. case DAHDI_MAINT_NETWORKLINELOOP:
  1931. case DAHDI_MAINT_NETWORKPAYLOADLOOP:
  1932. case DAHDI_MAINT_FAS_DEFECT:
  1933. case DAHDI_MAINT_MULTI_DEFECT:
  1934. case DAHDI_MAINT_CRC_DEFECT:
  1935. case DAHDI_MAINT_CAS_DEFECT:
  1936. case DAHDI_MAINT_PRBS_DEFECT:
  1937. case DAHDI_MAINT_BIPOLAR_DEFECT:
  1938. break;
  1939. case DAHDI_MAINT_LOOPUP:
  1940. case DAHDI_MAINT_LOOPDOWN:
  1941. dev_info(&wc->xb.pdev->dev,
  1942. "Only local loop supported in E1 mode\n");
  1943. return -ENOSYS;
  1944. case DAHDI_RESET_COUNTERS:
  1945. t43x_reset_counters(span);
  1946. return 0;
  1947. default:
  1948. dev_info(&wc->xb.pdev->dev,
  1949. "Unknown E1 maint command: %d\n", cmd);
  1950. return -ENOSYS;
  1951. }
  1952. } else {
  1953. switch (cmd) {
  1954. case DAHDI_MAINT_NONE:
  1955. case DAHDI_MAINT_LOCALLOOP:
  1956. case DAHDI_MAINT_NETWORKLINELOOP:
  1957. case DAHDI_MAINT_NETWORKPAYLOADLOOP:
  1958. case DAHDI_MAINT_LOOPUP:
  1959. case DAHDI_MAINT_LOOPDOWN:
  1960. case DAHDI_MAINT_FAS_DEFECT:
  1961. case DAHDI_MAINT_MULTI_DEFECT:
  1962. case DAHDI_MAINT_CRC_DEFECT:
  1963. case DAHDI_MAINT_CAS_DEFECT:
  1964. case DAHDI_MAINT_PRBS_DEFECT:
  1965. case DAHDI_MAINT_BIPOLAR_DEFECT:
  1966. break;
  1967. case DAHDI_RESET_COUNTERS:
  1968. t43x_reset_counters(span);
  1969. return 0;
  1970. default:
  1971. dev_info(&wc->xb.pdev->dev,
  1972. "Unknown T1 maint command: %d\n", cmd);
  1973. return -ENOSYS;
  1974. }
  1975. }
  1976. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  1977. if (!work) {
  1978. dev_info(&wc->xb.pdev->dev,
  1979. "Failed to allocate memory for workqueue\n");
  1980. return -ENOMEM;
  1981. }
  1982. work->span = span;
  1983. work->wc = wc;
  1984. work->cmd = cmd;
  1985. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  1986. INIT_WORK(&work->work, t43x_maint_work, work);
  1987. #else
  1988. INIT_WORK(&work->work, t43x_maint_work);
  1989. #endif
  1990. queue_work(wc->wq, &work->work);
  1991. return 0;
  1992. }
  1993. static int t43x_clear_maint(struct dahdi_span *span)
  1994. {
  1995. struct t43x_span *ts = container_of(span, struct t43x_span, span);
  1996. struct t43x *wc = ts->owner;
  1997. int reg = 0;
  1998. unsigned long flags;
  1999. int fidx = (wc->numspans == 2) ? span->offset+1 : span->offset;
  2000. /* Turn off local loop */
  2001. spin_lock_irqsave(&wc->reglock, flags);
  2002. reg = __t43x_framer_get(wc, fidx, LIM0);
  2003. if (reg < 0) {
  2004. spin_unlock_irqrestore(&wc->reglock, flags);
  2005. return -EIO;
  2006. }
  2007. __t43x_framer_set(wc, fidx, LIM0, reg & ~LIM0_LL);
  2008. /* Turn off remote loop & jitter attenuator */
  2009. reg = __t43x_framer_get(wc, fidx, LIM1);
  2010. if (reg < 0) {
  2011. spin_unlock_irqrestore(&wc->reglock, flags);
  2012. return -EIO;
  2013. }
  2014. __t43x_framer_set(wc, fidx, LIM1, reg & ~(LIM1_RL | LIM1_JATT));
  2015. /* Clear loopup/loopdown signals on the line */
  2016. __t43x_framer_set(wc, fidx, 0x21, 0x40);
  2017. spin_unlock_irqrestore(&wc->reglock, flags);
  2018. return 0;
  2019. }
  2020. static int t43x_ioctl(struct dahdi_chan *chan, unsigned int cmd,
  2021. unsigned long data)
  2022. {
  2023. struct t4_regs regs;
  2024. unsigned int x;
  2025. struct t43x *wc;
  2026. int fidx;
  2027. switch (cmd) {
  2028. case WCT4_GET_REGS:
  2029. wc = chan->pvt;
  2030. fidx = (wc->numspans == 2) ? chan->span->offset+1 :
  2031. chan->span->offset;
  2032. for (x = 0; x < sizeof(regs.regs) / sizeof(regs.regs[0]); x++)
  2033. regs.regs[x] = t43x_framer_get(wc, fidx, x);
  2034. if (copy_to_user((void __user *) data, &regs, sizeof(regs)))
  2035. return -EFAULT;
  2036. break;
  2037. default:
  2038. return -ENOTTY;
  2039. }
  2040. return 0;
  2041. }
  2042. static void t43x_chan_set_sigcap(struct dahdi_span *span, int x)
  2043. {
  2044. struct t43x_span *ts = container_of(span, struct t43x_span, span);
  2045. struct dahdi_chan *chan = ts->chans[x];
  2046. chan->sigcap = DAHDI_SIG_CLEAR;
  2047. /* E&M variant supported depends on span type */
  2048. if (dahdi_is_e1_span(span)) {
  2049. /* E1 sigcap setup */
  2050. if (span->lineconfig & DAHDI_CONFIG_CCS) {
  2051. /* CCS setup */
  2052. chan->sigcap |= DAHDI_SIG_MTP2 | DAHDI_SIG_SF;
  2053. return;
  2054. }
  2055. /* clear out sig and sigcap for channel 16 on E1 CAS
  2056. * lines, otherwise, set it correctly */
  2057. if (x == 15) {
  2058. /* CAS signaling channel setup */
  2059. ts->chans[15]->sigcap = 0;
  2060. ts->chans[15]->sig = 0;
  2061. return;
  2062. }
  2063. /* normal CAS setup */
  2064. chan->sigcap |= DAHDI_SIG_EM_E1 | DAHDI_SIG_FXSLS |
  2065. DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_SF |
  2066. DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_FXOKS |
  2067. DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS;
  2068. } else {
  2069. /* T1 sigcap setup */
  2070. chan->sigcap |= DAHDI_SIG_EM | DAHDI_SIG_FXSLS |
  2071. DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_MTP2 |
  2072. DAHDI_SIG_SF | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS |
  2073. DAHDI_SIG_FXOKS | DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS;
  2074. }
  2075. }
  2076. static bool t43x_lineconfig_changed(struct t43x_span *ts,
  2077. const struct dahdi_lineconfig *lc)
  2078. {
  2079. unsigned long flags;
  2080. bool result;
  2081. u32 crc = crc32(~0, lc, sizeof(*lc));
  2082. spin_lock_irqsave(&ts->owner->reglock, flags);
  2083. result = (crc != ts->lineconfig_fingerprint);
  2084. spin_unlock_irqrestore(&ts->owner->reglock, flags);
  2085. return result;
  2086. }
  2087. static void t43x_save_lineconfig(struct t43x_span *ts,
  2088. const struct dahdi_lineconfig *lc)
  2089. {
  2090. unsigned long flags;
  2091. u32 crc = crc32(~0, lc, sizeof(*lc));
  2092. spin_lock_irqsave(&ts->owner->reglock, flags);
  2093. ts->lineconfig_fingerprint = crc;
  2094. spin_unlock_irqrestore(&ts->owner->reglock, flags);
  2095. }
  2096. static int
  2097. t43x_spanconfig(struct file *file, struct dahdi_span *span,
  2098. struct dahdi_lineconfig *lc)
  2099. {
  2100. struct t43x_span *ts = container_of(span, struct t43x_span, span);
  2101. struct t43x *wc = ts->owner;
  2102. int i;
  2103. int res = 0;
  2104. if (debug)
  2105. dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
  2106. if (file->f_flags & O_NONBLOCK) {
  2107. if (!is_initialized(wc))
  2108. return -EAGAIN;
  2109. } else {
  2110. t43x_wait_for_ready(wc);
  2111. }
  2112. if (lc->sync < 0)
  2113. lc->sync = 0;
  2114. if (lc->sync > wc->numspans) {
  2115. dev_warn(&wc->xb.pdev->dev,
  2116. "WARNING: Cannot set priority on span %d to %d. Please set to a number between 1 and %d\n",
  2117. span->spanno, lc->sync, wc->numspans);
  2118. lc->sync = 0;
  2119. }
  2120. /* remove this span number from the current sync sources, if there */
  2121. for (i = 0; i < wc->numspans; i++) {
  2122. if (wc->tspans[i]->sync == span->spanno)
  2123. wc->tspans[i]->sync = 0;
  2124. }
  2125. wc->tspans[span->offset]->syncpos = lc->sync;
  2126. /* if a sync src, put it in proper place */
  2127. if (lc->sync)
  2128. wc->tspans[lc->sync - 1]->sync = span->spanno;
  2129. /* make sure that sigcaps gets updated if necessary */
  2130. for (i = 0; i < span->channels; i++)
  2131. t43x_chan_set_sigcap(span, i);
  2132. /* If already running, apply changes immediately */
  2133. if (test_bit(DAHDI_FLAGBIT_RUNNING, &span->flags) &&
  2134. t43x_lineconfig_changed(ts, lc)) {
  2135. res = t43x_startup(file, span);
  2136. if (!res)
  2137. t43x_save_lineconfig(ts, lc);
  2138. }
  2139. return res;
  2140. }
  2141. /*
  2142. * Initialize a span
  2143. *
  2144. */
  2145. static int
  2146. t43x_init_one_span(struct t43x *wc, struct t43x_span *ts, enum linemode type)
  2147. {
  2148. int x;
  2149. struct dahdi_chan *chans[32] = {NULL,};
  2150. struct dahdi_echocan_state *ec[32] = {NULL,};
  2151. unsigned long flags;
  2152. int res = 0;
  2153. if (debug)
  2154. dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
  2155. for (x = 0; x < ((E1 == type) ? 31 : 24); x++) {
  2156. chans[x] = kzalloc(sizeof(*chans[x]), GFP_KERNEL);
  2157. ec[x] = kzalloc(sizeof(*ec[x]), GFP_KERNEL);
  2158. if (!chans[x] || !ec[x])
  2159. goto error_exit;
  2160. }
  2161. /* Stop the interrupt handler so that we may swap the channel array. */
  2162. disable_irq(wc->xb.pdev->irq);
  2163. spin_lock_irqsave(&wc->reglock, flags);
  2164. for (x = 0; x < ARRAY_SIZE(ts->chans); x++) {
  2165. kfree(ts->chans[x]);
  2166. kfree(ts->ec[x]);
  2167. ts->chans[x] = NULL;
  2168. ts->ec[x] = NULL;
  2169. }
  2170. memcpy(ts->chans, chans, sizeof(ts->chans));
  2171. memcpy(ts->ec, ec, sizeof(ts->ec));
  2172. switch (type) {
  2173. case E1:
  2174. ts->span.channels = 31;
  2175. ts->span.spantype = SPANTYPE_DIGITAL_E1;
  2176. ts->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_HDB3 |
  2177. DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
  2178. ts->span.deflaw = DAHDI_LAW_ALAW;
  2179. break;
  2180. case T1:
  2181. ts->span.channels = 24;
  2182. ts->span.spantype = SPANTYPE_DIGITAL_T1;
  2183. ts->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS |
  2184. DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
  2185. ts->span.deflaw = DAHDI_LAW_MULAW;
  2186. break;
  2187. case J1:
  2188. ts->span.channels = 24;
  2189. ts->span.spantype = SPANTYPE_DIGITAL_J1;
  2190. ts->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS |
  2191. DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
  2192. ts->span.deflaw = DAHDI_LAW_MULAW;
  2193. break;
  2194. default:
  2195. spin_unlock_irqrestore(&wc->reglock, flags);
  2196. res = -EINVAL;
  2197. enable_irq(wc->xb.pdev->irq);
  2198. goto error_exit;
  2199. }
  2200. spin_unlock_irqrestore(&wc->reglock, flags);
  2201. set_bit(DAHDI_FLAGBIT_RBS, &ts->span.flags);
  2202. for (x = 0; x < ts->span.channels; x++) {
  2203. sprintf(ts->chans[x]->name, "%s/%d", ts->span.name, x + 1);
  2204. t43x_chan_set_sigcap(&ts->span, x);
  2205. ts->chans[x]->pvt = wc;
  2206. ts->chans[x]->chanpos = x + 1;
  2207. }
  2208. t43x_reset_counters(&ts->span);
  2209. /* Span is in red alarm by default ? */
  2210. ts->span.alarms = DAHDI_ALARM_NONE;
  2211. enable_irq(wc->xb.pdev->irq);
  2212. return 0;
  2213. error_exit:
  2214. for (x = 0; x < ARRAY_SIZE(chans); ++x) {
  2215. kfree(chans[x]);
  2216. kfree(ec[x]);
  2217. }
  2218. return res;
  2219. }
  2220. /*
  2221. * Initialize all spans (one time)
  2222. */
  2223. static void t43x_init_spans(struct t43x *wc, enum linemode type)
  2224. {
  2225. struct t43x_span *ts;
  2226. int x;
  2227. if (debug)
  2228. dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
  2229. for (x = 0; x < wc->numspans; x++) {
  2230. ts = wc->tspans[x];
  2231. sprintf(ts->span.name,
  2232. "WCTE%d/%d/%d", wc->numspans, wc->num, x + 1);
  2233. snprintf(ts->span.desc, sizeof(ts->span.desc) - 1,
  2234. "WCTE%d3X (PCI) Card %d Span %d",
  2235. wc->numspans, wc->num, x + 1);
  2236. ts->span.chans = ts->chans;
  2237. ts->span.flags = DAHDI_FLAG_RBS;
  2238. ts->span.ops = &t43x_span_ops;
  2239. ts->span.offset = x;
  2240. ts->owner = wc;
  2241. t43x_init_one_span(wc, ts, type);
  2242. list_add_tail(&ts->span.device_node, &wc->ddev->spans);
  2243. }
  2244. }
  2245. /**
  2246. * t1xx_set_linemode - Change the type of span before assignment.
  2247. * @span: The span to change.
  2248. * @linemode: Text string for the line mode.
  2249. *
  2250. * This function may be called after the dahdi_device is registered but
  2251. * before the spans are assigned numbers (and are visible to the rest of
  2252. * DAHDI).
  2253. *
  2254. */
  2255. static int t43x_set_linemode(struct dahdi_span *span, enum spantypes linemode)
  2256. {
  2257. int res;
  2258. struct t43x_span *ts = container_of(span, struct t43x_span, span);
  2259. struct t43x *wc = ts->owner;
  2260. if (debug)
  2261. dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
  2262. /* We may already be set to the requested type. */
  2263. if (span->spantype == linemode) {
  2264. ts->span.alarms = DAHDI_ALARM_NONE;
  2265. return 0;
  2266. }
  2267. res = t43x_wait_for_ready(wc);
  2268. if (res)
  2269. return res;
  2270. mutex_lock(&wc->lock);
  2271. switch (linemode) {
  2272. case SPANTYPE_DIGITAL_T1:
  2273. dev_info(&wc->xb.pdev->dev,
  2274. "Changing from %s to T1 line mode.\n",
  2275. dahdi_spantype2str(span->spantype));
  2276. res = t43x_init_one_span(wc, ts, T1);
  2277. break;
  2278. case SPANTYPE_DIGITAL_E1:
  2279. dev_info(&wc->xb.pdev->dev,
  2280. "Changing from %s to E1 line mode.\n",
  2281. dahdi_spantype2str(span->spantype));
  2282. res = t43x_init_one_span(wc, ts, E1);
  2283. break;
  2284. case SPANTYPE_DIGITAL_J1:
  2285. dev_info(&wc->xb.pdev->dev,
  2286. "Changing from %s to J1 line mode.\n",
  2287. dahdi_spantype2str(span->spantype));
  2288. res = t43x_init_one_span(wc, ts, J1);
  2289. break;
  2290. default:
  2291. dev_err(&wc->xb.pdev->dev,
  2292. "Got invalid linemode '%s' from dahdi\n",
  2293. dahdi_spantype2str(linemode));
  2294. res = -EINVAL;
  2295. }
  2296. /* Since we probably reallocated the channels we need to make
  2297. * sure they are configured before setting INITIALIZED again. */
  2298. if (!res)
  2299. dahdi_init_span(span);
  2300. mutex_unlock(&wc->lock);
  2301. return res;
  2302. }
  2303. static int t43x_hardware_post_init(struct t43x *wc, enum linemode *type)
  2304. {
  2305. int reg;
  2306. int x;
  2307. if (!strcasecmp(default_linemode, "e1")) {
  2308. *type = E1;
  2309. } else if (!strcasecmp(default_linemode, "t1")) {
  2310. *type = T1;
  2311. } else if (!strcasecmp(default_linemode, "j1")) {
  2312. *type = J1;
  2313. } else {
  2314. dev_warn(&wc->xb.pdev->dev,
  2315. "'%s' is an unknown linemode. Defaulting to 't1'\n",
  2316. default_linemode);
  2317. *type = T1;
  2318. }
  2319. if (debug) {
  2320. dev_info(&wc->xb.pdev->dev, "linemode: %s\n",
  2321. (*type == T1) ? "T1" : ((J1 == *type) ? "J1" : "E1"));
  2322. }
  2323. /* what version of the FALC are we using? */
  2324. wcxb_gpio_set(&wc->xb, FALC_CPU_RESET);
  2325. reg = t43x_framer_get(wc, 0, 0x4a);
  2326. if (reg < 0) {
  2327. dev_info(&wc->xb.pdev->dev,
  2328. "Failed to read FALC version (%x)\n", reg);
  2329. return -EIO;
  2330. }
  2331. dev_info(&wc->xb.pdev->dev, "FALC version: %1x\n", reg);
  2332. /* make sure reads and writes work */
  2333. for (x = 0; x < 256; x++) {
  2334. t43x_framer_set(wc, 0, 0x14, x);
  2335. reg = t43x_framer_get(wc, 0, 0x14);
  2336. if (reg < 0) {
  2337. dev_info(&wc->xb.pdev->dev,
  2338. "Failed register read (%d)\n", reg);
  2339. return -EIO;
  2340. }
  2341. if (reg != x) {
  2342. dev_info(&wc->xb.pdev->dev,
  2343. "Register test failed. Wrote '%x' but read '%x'\n",
  2344. x, reg);
  2345. return -EIO;
  2346. }
  2347. }
  2348. /* Enable all the GPIO outputs. */
  2349. t43x_setleds(wc, -1);
  2350. return 0;
  2351. }
  2352. static void t43x_check_alarms(struct t43x *wc, int span_idx)
  2353. {
  2354. struct t43x_span *ts = wc->tspans[span_idx];
  2355. unsigned char c, d;
  2356. int alarms;
  2357. int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
  2358. if (!(test_bit(DAHDI_FLAGBIT_RUNNING, &ts->span.flags)))
  2359. return;
  2360. spin_lock(&wc->reglock);
  2361. c = __t43x_framer_get(wc, fidx, 0x4c);
  2362. d = __t43x_framer_get(wc, fidx, 0x4d);
  2363. /* start with existing span alarms */
  2364. alarms = ts->span.alarms;
  2365. if (dahdi_is_e1_span(&ts->span)) {
  2366. if (c & 0x04) {
  2367. /* No multiframe found, force RAI high after 400ms only
  2368. * if we haven't found a multiframe since last loss of
  2369. * frame */
  2370. if (!test_and_set_bit(NMF_FLAGBIT, &ts->bit_flags)) {
  2371. /* LIM0: Force RAI High */
  2372. __t43x_framer_set(wc, fidx, 0x20, 0x9f | 0x20);
  2373. dev_info(&wc->xb.pdev->dev,
  2374. "NMF workaround on!\n");
  2375. }
  2376. /* Reset to CRC4 mode */
  2377. __t43x_framer_set(wc, fidx, 0x1e, 0xc3);
  2378. /* Force Resync */
  2379. __t43x_framer_set(wc, fidx, 0x1c, 0xf2);
  2380. /* Force Resync */
  2381. __t43x_framer_set(wc, fidx, 0x1c, 0xf0);
  2382. } else if (!(c & 0x02)) {
  2383. if (test_and_clear_bit(NMF_FLAGBIT, &ts->bit_flags)) {
  2384. /* LIM0: Clear forced RAI */
  2385. __t43x_framer_set(wc, fidx, 0x20, 0x9f);
  2386. dev_info(&wc->xb.pdev->dev,
  2387. "NMF workaround off!\n");
  2388. }
  2389. }
  2390. }
  2391. if (ts->span.lineconfig & DAHDI_CONFIG_NOTOPEN) {
  2392. if (!test_bit(HAVE_OPEN_CHANNELS_FLAGBIT, &ts->bit_flags))
  2393. alarms |= DAHDI_ALARM_NOTOPEN;
  2394. else
  2395. alarms &= ~DAHDI_ALARM_NOTOPEN;
  2396. }
  2397. if (c & 0x20) { /* LOF/LFA */
  2398. if (!(alarms & DAHDI_ALARM_RED) && (0 == ts->lofalarmtimer)) {
  2399. ts->lofalarmtimer = (jiffies +
  2400. msecs_to_jiffies(alarmdebounce)) ?: 1;
  2401. }
  2402. } else {
  2403. ts->lofalarmtimer = 0;
  2404. }
  2405. if (c & 0x80) { /* LOS */
  2406. if (!(alarms & DAHDI_ALARM_RED) && (0 == ts->losalarmtimer)) {
  2407. ts->losalarmtimer = (jiffies +
  2408. msecs_to_jiffies(losalarmdebounce)) ?: 1;
  2409. }
  2410. } else {
  2411. ts->losalarmtimer = 0;
  2412. }
  2413. if (!(c & (0x80|0x20)))
  2414. alarms &= ~DAHDI_ALARM_RED;
  2415. if (c & 0x40) { /* AIS */
  2416. if (!(alarms & DAHDI_ALARM_BLUE) && (0 == ts->aisalarmtimer))
  2417. ts->aisalarmtimer = (jiffies +
  2418. msecs_to_jiffies(aisalarmdebounce)) ?: 1;
  2419. } else {
  2420. ts->aisalarmtimer = 0;
  2421. alarms &= ~DAHDI_ALARM_BLUE;
  2422. }
  2423. /* Keep track of recovering */
  2424. if (alarms & (DAHDI_ALARM_RED|DAHDI_ALARM_BLUE|DAHDI_ALARM_NOTOPEN)) {
  2425. ts->recoverytimer = 0;
  2426. alarms &= ~DAHDI_ALARM_RECOVER;
  2427. } else if (ts->span.alarms & (DAHDI_ALARM_RED|DAHDI_ALARM_BLUE)) {
  2428. if (0 == ts->recoverytimer) {
  2429. ts->recoverytimer = (jiffies + 5*HZ) ?: 1;
  2430. alarms |= DAHDI_ALARM_RECOVER;
  2431. }
  2432. }
  2433. if (c & 0x10) { /* receiving yellow (RAI) */
  2434. if (!(alarms & DAHDI_ALARM_YELLOW) && !ts->yelalarmtimer) {
  2435. ts->yelalarmtimer = (jiffies +
  2436. msecs_to_jiffies(yelalarmdebounce)) ?: 1;
  2437. }
  2438. } else {
  2439. ts->yelalarmtimer = 0;
  2440. alarms &= ~DAHDI_ALARM_YELLOW;
  2441. }
  2442. if (ts->span.alarms != alarms) {
  2443. /* re-evaluate timing source if alarm state is changed */
  2444. if ((alarms ^ ts->span.alarms) & NOSYNC_ALARMS) {
  2445. ts->span.alarms = alarms;
  2446. __t43x_set_timing_source_auto(wc);
  2447. } else {
  2448. ts->span.alarms = alarms;
  2449. }
  2450. spin_unlock(&wc->reglock);
  2451. dahdi_alarm_notify(&ts->span);
  2452. } else {
  2453. spin_unlock(&wc->reglock);
  2454. }
  2455. }
  2456. static void t43x_check_loopcodes(struct t43x *wc, int span_idx)
  2457. {
  2458. struct t43x_span *ts = wc->tspans[span_idx];
  2459. unsigned char frs1;
  2460. int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
  2461. frs1 = t43x_framer_get(wc, fidx, 0x4d);
  2462. /* Detect loopup code if we're not sending one */
  2463. if ((ts->span.maintstat != DAHDI_MAINT_LOOPUP) && (frs1 & 0x08)) {
  2464. /* Loop-up code detected */
  2465. if ((ts->span.maintstat != DAHDI_MAINT_REMOTELOOP) &&
  2466. (0 == ts->loopuptimer))
  2467. ts->loopuptimer = (jiffies + msecs_to_jiffies(800)) | 1;
  2468. } else {
  2469. ts->loopuptimer = 0;
  2470. }
  2471. /* Same for loopdown code */
  2472. if ((ts->span.maintstat != DAHDI_MAINT_LOOPDOWN) && (frs1 & 0x10)) {
  2473. /* Loop-down code detected */
  2474. if ((ts->span.maintstat == DAHDI_MAINT_REMOTELOOP) &&
  2475. (0 == ts->loopdntimer))
  2476. ts->loopdntimer = (jiffies + msecs_to_jiffies(800)) | 1;
  2477. } else {
  2478. ts->loopdntimer = 0;
  2479. }
  2480. }
  2481. static void t43x_debounce_alarms(struct t43x *wc, int span_idx)
  2482. {
  2483. struct t43x_span *ts = wc->tspans[span_idx];
  2484. int alarms;
  2485. unsigned long flags;
  2486. unsigned int fmr4;
  2487. int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
  2488. spin_lock_irqsave(&wc->reglock, flags);
  2489. alarms = ts->span.alarms;
  2490. if (ts->lofalarmtimer && time_after(jiffies, ts->lofalarmtimer)) {
  2491. alarms |= DAHDI_ALARM_RED;
  2492. ts->lofalarmtimer = 0;
  2493. dev_info(&wc->xb.pdev->dev, "LOF alarm detected\n");
  2494. }
  2495. if (ts->losalarmtimer && time_after(jiffies, ts->losalarmtimer)) {
  2496. alarms |= DAHDI_ALARM_RED;
  2497. ts->losalarmtimer = 0;
  2498. dev_info(&wc->xb.pdev->dev, "LOS alarm detected\n");
  2499. }
  2500. if (ts->aisalarmtimer && time_after(jiffies, ts->aisalarmtimer)) {
  2501. alarms |= DAHDI_ALARM_BLUE;
  2502. ts->aisalarmtimer = 0;
  2503. dev_info(&wc->xb.pdev->dev, "AIS alarm detected\n");
  2504. }
  2505. if (ts->yelalarmtimer && time_after(jiffies, ts->yelalarmtimer)) {
  2506. alarms |= DAHDI_ALARM_YELLOW;
  2507. ts->yelalarmtimer = 0;
  2508. dev_info(&wc->xb.pdev->dev, "YEL alarm detected\n");
  2509. }
  2510. if (ts->recoverytimer && time_after(jiffies, ts->recoverytimer)) {
  2511. alarms &= ~(DAHDI_ALARM_RECOVER);
  2512. ts->recoverytimer = 0;
  2513. memset(&ts->span.count, 0, sizeof(ts->span.count));
  2514. dev_info(&wc->xb.pdev->dev, "Alarms cleared\n");
  2515. }
  2516. if (alarms != ts->span.alarms) {
  2517. /* re-evaluate timing source if alarm state is changed */
  2518. if ((alarms ^ ts->span.alarms) & NOSYNC_ALARMS) {
  2519. ts->span.alarms = alarms;
  2520. __t43x_set_timing_source_auto(wc);
  2521. } else {
  2522. ts->span.alarms = alarms;
  2523. }
  2524. spin_unlock_irqrestore(&wc->reglock, flags);
  2525. dahdi_alarm_notify(&ts->span);
  2526. spin_lock_irqsave(&wc->reglock, flags);
  2527. }
  2528. /* If receiving alarms (except Yellow), go into Yellow alarm state */
  2529. if (alarms & (DAHDI_ALARM_RED|DAHDI_ALARM_BLUE|
  2530. DAHDI_ALARM_NOTOPEN|DAHDI_ALARM_RECOVER)) {
  2531. if (!test_and_set_bit(SENDINGYELLOW_FLAGBIT, &ts->bit_flags)) {
  2532. dev_info(&wc->xb.pdev->dev, "Setting yellow alarm\n");
  2533. /* We manually do yellow alarm to handle RECOVER
  2534. * and NOTOPEN, otherwise it's auto anyway */
  2535. fmr4 = __t43x_framer_get(wc, fidx, 0x20);
  2536. __t43x_framer_set(wc, fidx, 0x20, fmr4 | 0x20);
  2537. }
  2538. } else {
  2539. if (test_and_clear_bit(SENDINGYELLOW_FLAGBIT, &ts->bit_flags)) {
  2540. dev_info(&wc->xb.pdev->dev, "Clearing yellow alarm\n");
  2541. /* We manually do yellow alarm to handle RECOVER */
  2542. fmr4 = __t43x_framer_get(wc, fidx, 0x20);
  2543. __t43x_framer_set(wc, fidx, 0x20, fmr4 & ~0x20);
  2544. }
  2545. }
  2546. spin_unlock_irqrestore(&wc->reglock, flags);
  2547. }
  2548. static void t43x_debounce_loopcodes(struct t43x *wc, int span_idx)
  2549. {
  2550. struct t43x_span *ts = wc->tspans[span_idx];
  2551. unsigned long flags;
  2552. int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
  2553. spin_lock_irqsave(&wc->reglock, flags);
  2554. if (ts->loopuptimer && time_after(jiffies, ts->loopuptimer)) {
  2555. /* Loop-up code debounced */
  2556. dev_info(&wc->xb.pdev->dev, "Loopup detected, enabling remote loop\n");
  2557. __t43x_framer_set(wc, fidx, 0x36, 0x08); /* LIM0: Disable
  2558. any local loop */
  2559. __t43x_framer_set(wc, fidx, 0x37, 0xf6); /* LIM1: Enable
  2560. remote loop */
  2561. ts->span.maintstat = DAHDI_MAINT_REMOTELOOP;
  2562. ts->loopuptimer = 0;
  2563. dahdi_alarm_notify(&ts->span);
  2564. __t43x_set_timing_source_auto(wc);
  2565. }
  2566. if (ts->loopdntimer && time_after(jiffies, ts->loopdntimer)) {
  2567. /* Loop-down code debounced */
  2568. dev_info(&wc->xb.pdev->dev, "Loopdown detected, disabling remote loop\n");
  2569. __t43x_framer_set(wc, fidx, 0x36, 0x08); /* LIM0: Disable
  2570. any local loop */
  2571. __t43x_framer_set(wc, fidx, 0x37, 0xf0); /* LIM1: Disable
  2572. remote loop */
  2573. ts->span.maintstat = DAHDI_MAINT_NONE;
  2574. ts->loopdntimer = 0;
  2575. dahdi_alarm_notify(&ts->span);
  2576. __t43x_set_timing_source_auto(wc);
  2577. }
  2578. spin_unlock_irqrestore(&wc->reglock, flags);
  2579. }
  2580. static void handle_leds(struct t43x *wc)
  2581. {
  2582. u32 led = 0;
  2583. struct t43x_span *ts;
  2584. int span_idx;
  2585. if (time_after(jiffies, wc->blinktimer)) {
  2586. wc->blink = !wc->blink;
  2587. wc->blinktimer = jiffies + HZ/2;
  2588. }
  2589. for (span_idx = wc->numspans-1; span_idx >= 0; span_idx--) {
  2590. led >>= 2;
  2591. ts = wc->tspans[span_idx];
  2592. if ((ts->span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE)) ||
  2593. ts->losalarmtimer) {
  2594. /* When we're in red alarm, blink the led once a
  2595. * second. */
  2596. if (wc->blink)
  2597. led |= STATUS_LED_RED;
  2598. } else if (ts->span.alarms & DAHDI_ALARM_YELLOW) {
  2599. led |= STATUS_LED_YELLOW;
  2600. } else {
  2601. if (test_bit(DAHDI_FLAGBIT_RUNNING, &ts->span.flags))
  2602. led |= STATUS_LED_GREEN;
  2603. }
  2604. }
  2605. if (wc->numspans == 2)
  2606. led >>= 2;
  2607. if (led != (ioread32be(wc->xb.membase) & LED_MASK))
  2608. t43x_setleds(wc, led);
  2609. }
  2610. static void t43x_handle_receive(struct wcxb *xb, void *vfp)
  2611. {
  2612. int i, j, s;
  2613. u_char *frame = (u_char *) vfp;
  2614. struct t43x *wc = container_of(xb, struct t43x, xb);
  2615. struct t43x_span *ts;
  2616. for (s = 0; s < wc->numspans; s++) {
  2617. ts = wc->tspans[s];
  2618. if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &ts->span.flags))
  2619. continue;
  2620. for (j = 0; j < DAHDI_CHUNKSIZE; j++) {
  2621. for (i = 0; i < ts->span.channels; i++) {
  2622. ts->chans[i]->readchunk[j] =
  2623. frame[j*WCXB_DMA_CHAN_SIZE+(s+1+i*4)];
  2624. }
  2625. }
  2626. if (0 == vpmsupport) {
  2627. for (i = 0; i < ts->span.channels; i++) {
  2628. struct dahdi_chan *const c = ts->span.chans[i];
  2629. __dahdi_ec_chunk(c, c->readchunk, c->readchunk,
  2630. c->writechunk);
  2631. }
  2632. }
  2633. _dahdi_receive(&ts->span);
  2634. }
  2635. }
  2636. static void t43x_handle_transmit(struct wcxb *xb, void *vfp)
  2637. {
  2638. int i, j, s;
  2639. u_char *frame = (u_char *) vfp;
  2640. struct t43x *wc = container_of(xb, struct t43x, xb);
  2641. struct t43x_span *ts;
  2642. for (s = 0; s < wc->numspans; s++) {
  2643. ts = wc->tspans[s];
  2644. if (!test_bit(DAHDI_FLAGBIT_REGISTERED,
  2645. &ts->span.flags)) {
  2646. continue;
  2647. }
  2648. _dahdi_transmit(&ts->span);
  2649. for (j = 0; j < DAHDI_CHUNKSIZE; j++)
  2650. for (i = 0; i < ts->span.channels; i++)
  2651. frame[j*WCXB_DMA_CHAN_SIZE+(s+1+i*4)] =
  2652. ts->chans[i]->writechunk[j];
  2653. }
  2654. }
  2655. #define SPAN_DEBOUNCE \
  2656. (ts->lofalarmtimer || ts->losalarmtimer || \
  2657. ts->aisalarmtimer || ts->yelalarmtimer || \
  2658. ts->recoverytimer || \
  2659. ts->loopuptimer || ts->loopdntimer)
  2660. #define SPAN_ALARMS \
  2661. (ts->span.alarms & ~DAHDI_ALARM_NOTOPEN)
  2662. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  2663. static void timer_work_func(void *param)
  2664. {
  2665. struct t43x *wc = param;
  2666. #else
  2667. static void timer_work_func(struct work_struct *work)
  2668. {
  2669. struct t43x *wc = container_of(work, struct t43x, timer_work);
  2670. #endif
  2671. struct t43x_span *ts;
  2672. int x;
  2673. bool start_timer = false;
  2674. for (x = 0; x < wc->numspans; x++) {
  2675. ts = wc->tspans[x];
  2676. if (ts->debounce) {
  2677. /* Clear it now in case the interrupt needs to set it
  2678. * again */
  2679. ts->debounce = false;
  2680. t43x_debounce_alarms(wc, x);
  2681. if (!dahdi_is_e1_span(&ts->span))
  2682. t43x_debounce_loopcodes(wc, x);
  2683. if (SPAN_DEBOUNCE || SPAN_ALARMS)
  2684. ts->debounce = true;
  2685. if (ts->debounce)
  2686. start_timer = true;
  2687. }
  2688. }
  2689. handle_leds(wc);
  2690. if (start_timer)
  2691. mod_timer(&wc->timer, jiffies + HZ/10);
  2692. }
  2693. static void handle_falc_int(struct t43x *wc, int unit)
  2694. {
  2695. struct t43x_span *ts = wc->tspans[unit];
  2696. unsigned char gis, isr0, isr1, isr2, isr3, isr4;
  2697. static int intcount;
  2698. bool start_timer;
  2699. bool recheck_sigbits = false;
  2700. int fidx = (wc->numspans == 2) ? unit+1 : unit;
  2701. intcount++;
  2702. start_timer = false;
  2703. spin_lock(&wc->reglock);
  2704. gis = __t43x_framer_get(wc, fidx, FRMR_GIS);
  2705. isr0 = (gis&FRMR_GIS_ISR0) ? __t43x_framer_get(wc, fidx, FRMR_ISR0) : 0;
  2706. isr1 = (gis&FRMR_GIS_ISR1) ? __t43x_framer_get(wc, fidx, FRMR_ISR1) : 0;
  2707. isr2 = (gis&FRMR_GIS_ISR2) ? __t43x_framer_get(wc, fidx, FRMR_ISR2) : 0;
  2708. isr3 = (gis&FRMR_GIS_ISR3) ? __t43x_framer_get(wc, fidx, FRMR_ISR3) : 0;
  2709. isr4 = (gis&FRMR_GIS_ISR4) ? __t43x_framer_get(wc, fidx, FRMR_ISR4) : 0;
  2710. if ((debug) && !(isr3 & ISR3_SEC)) {
  2711. dev_info(&wc->xb.pdev->dev,
  2712. "span: %d gis: %02x, isr0: %02x, isr1: %02x, isr2: %02x, isr3: %02x, isr4: %02x, intcount=%u\n",
  2713. unit, gis, isr0, isr1, isr2, isr3, isr4, intcount);
  2714. }
  2715. /* Collect performance counters once per second */
  2716. if (isr3 & ISR3_SEC) {
  2717. ts->span.count.fe += __t43x_framer_get(wc, fidx, FECL_T);
  2718. ts->span.count.crc4 += __t43x_framer_get(wc, fidx, CEC1L_T);
  2719. ts->span.count.cv += __t43x_framer_get(wc, fidx, CVCL_T);
  2720. ts->span.count.ebit += __t43x_framer_get(wc, fidx, EBCL_T);
  2721. ts->span.count.be += __t43x_framer_get(wc, fidx, BECL_T);
  2722. ts->span.count.prbs = __t43x_framer_get(wc, fidx, FRS1_T);
  2723. if (DAHDI_RXSIG_INITIAL == ts->span.chans[0]->rxhooksig)
  2724. recheck_sigbits = true;
  2725. }
  2726. spin_unlock(&wc->reglock);
  2727. /* Collect errored second counter once per second */
  2728. if (isr3 & ISR3_ES)
  2729. ts->span.count.errsec += 1;
  2730. /* RSC/CASC: Received Signaling Information Changed */
  2731. /* This interrupt is enabled by set_cas_mode() for CAS channels */
  2732. if (isr0 & 0x08 || recheck_sigbits)
  2733. t43x_check_sigbits(wc, unit);
  2734. if (dahdi_is_e1_span(&ts->span)) {
  2735. /* E1 checks */
  2736. if ((isr3 & 0x68) || isr2 || (isr1 & 0x7f)) {
  2737. t43x_check_alarms(wc, unit);
  2738. start_timer = true;
  2739. }
  2740. } else {
  2741. /* T1 checks */
  2742. if (isr2) {
  2743. t43x_check_alarms(wc, unit);
  2744. start_timer = true;
  2745. }
  2746. if (isr3 & 0x08) { /* T1 LLBSC */
  2747. t43x_check_loopcodes(wc, unit);
  2748. start_timer = true;
  2749. }
  2750. }
  2751. if (!ts->span.alarms) {
  2752. if ((isr3 & 0x3) || (isr4 & 0xc0))
  2753. ts->span.count.timingslips++;
  2754. }
  2755. if (start_timer) {
  2756. ts->debounce = true;
  2757. if (!timer_pending(&wc->timer))
  2758. mod_timer(&wc->timer, jiffies + HZ/10);
  2759. }
  2760. }
  2761. static void t43x_handle_interrupt(struct wcxb *xb, u32 pending)
  2762. {
  2763. u32 status;
  2764. struct t43x *wc = container_of(xb, struct t43x, xb);
  2765. if (!(pending & FALC_INT))
  2766. return;
  2767. /* Service at most one framer per interrupt cycle */
  2768. status = t43x_framer_get(wc, 0, FRMR_CIS);
  2769. if (status & (1 << (wc->intr_span + ((wc->numspans == 2) ? 1 : 0))))
  2770. handle_falc_int(wc, wc->intr_span);
  2771. if (++wc->intr_span >= wc->numspans)
  2772. wc->intr_span = 0;
  2773. }
  2774. static void t43x_timer(unsigned long data)
  2775. {
  2776. struct t43x *wc = (struct t43x *)data;
  2777. if (!is_initialized(wc))
  2778. return;
  2779. queue_work(wc->wq, &wc->timer_work);
  2780. return;
  2781. }
  2782. static int t43x_open(struct dahdi_chan *chan)
  2783. {
  2784. struct t43x *wc = chan->pvt;
  2785. struct t43x_span *ts = container_of(chan->span, struct t43x_span, span);
  2786. unsigned long flags;
  2787. if (!(ts->span.lineconfig & DAHDI_CONFIG_NOTOPEN))
  2788. return 0;
  2789. if (!test_and_set_bit(HAVE_OPEN_CHANNELS_FLAGBIT, &ts->bit_flags)) {
  2790. local_irq_save(flags);
  2791. t43x_check_alarms(wc, ts->span.offset);
  2792. local_irq_restore(flags);
  2793. }
  2794. return 0;
  2795. }
  2796. static int t43x_close(struct dahdi_chan *chan)
  2797. {
  2798. struct t43x *wc = chan->pvt;
  2799. struct t43x_span *ts = container_of(chan->span, struct t43x_span, span);
  2800. unsigned long flags;
  2801. if (!(ts->span.lineconfig & DAHDI_CONFIG_NOTOPEN))
  2802. return 0;
  2803. if (have_open_channels(ts))
  2804. return 0;
  2805. if (!test_and_set_bit(HAVE_OPEN_CHANNELS_FLAGBIT, &ts->bit_flags)) {
  2806. local_irq_save(flags);
  2807. t43x_check_alarms(wc, ts->span.offset);
  2808. local_irq_restore(flags);
  2809. }
  2810. return 0;
  2811. }
  2812. static const struct dahdi_span_ops t43x_span_ops = {
  2813. .owner = THIS_MODULE,
  2814. .spanconfig = t43x_spanconfig,
  2815. .chanconfig = t43x_chanconfig,
  2816. .startup = t43x_startup,
  2817. .rbsbits = t43x_rbsbits,
  2818. .maint = t43x_maint,
  2819. .ioctl = t43x_ioctl,
  2820. .assigned = t43x_span_assigned,
  2821. .set_spantype = t43x_set_linemode,
  2822. .echocan_create = t43x_echocan_create,
  2823. .echocan_name = t43x_echocan_name,
  2824. .open = t43x_open,
  2825. .close = t43x_close,
  2826. };
  2827. /**
  2828. * t43x_check_for_interrupts - Return 0 if the card is generating interrupts.
  2829. * @wc: The card to check.
  2830. *
  2831. * If the card is not generating interrupts, this function will also place all
  2832. * the spans on the card into red alarm.
  2833. *
  2834. */
  2835. static int t43x_check_for_interrupts(struct t43x *wc)
  2836. {
  2837. unsigned int starting_framecount = wc->xb.framecount;
  2838. unsigned long stop_time = jiffies + HZ*2;
  2839. unsigned long flags;
  2840. int x;
  2841. msleep(20);
  2842. spin_lock_irqsave(&wc->reglock, flags);
  2843. while (starting_framecount == wc->xb.framecount) {
  2844. spin_unlock_irqrestore(&wc->reglock, flags);
  2845. if (time_after(jiffies, stop_time)) {
  2846. for (x = 0; x < wc->numspans; x++)
  2847. wc->tspans[x]->span.alarms = DAHDI_ALARM_RED;
  2848. dev_err(&wc->xb.pdev->dev, "Interrupts not detected.\n");
  2849. return -EIO;
  2850. }
  2851. msleep(100);
  2852. spin_lock_irqsave(&wc->reglock, flags);
  2853. }
  2854. spin_unlock_irqrestore(&wc->reglock, flags);
  2855. return 0;
  2856. }
  2857. /**
  2858. * t43x_read_serial - Returns the serial number of the board.
  2859. * @wc: The board whos serial number we are reading.
  2860. *
  2861. * The buffer returned is dynamically allocated and must be kfree'd by the
  2862. * caller. If memory could not be allocated, NULL is returned.
  2863. *
  2864. * Must be called in process context.
  2865. *
  2866. */
  2867. static char *t43x_read_serial(struct t43x *wc)
  2868. {
  2869. int i;
  2870. static const int MAX_SERIAL = 20*5;
  2871. const unsigned int SERIAL_ADDRESS = 0x1f0000;
  2872. unsigned char *serial = kzalloc(MAX_SERIAL + 1, GFP_KERNEL);
  2873. struct wcxb const *xb = &wc->xb;
  2874. struct wcxb_spi_master *flash_spi_master = NULL;
  2875. struct wcxb_spi_device *flash_spi_device = NULL;
  2876. const unsigned int FLASH_SPI_BASE = 0x200;
  2877. if (!serial)
  2878. return NULL;
  2879. flash_spi_master = wcxb_spi_master_create(&xb->pdev->dev,
  2880. xb->membase + FLASH_SPI_BASE,
  2881. false);
  2882. if (!flash_spi_master)
  2883. return NULL;
  2884. flash_spi_device = wcxb_spi_device_create(flash_spi_master, 0);
  2885. if (!flash_spi_device)
  2886. goto error_exit;
  2887. wcxb_flash_read(flash_spi_device, SERIAL_ADDRESS,
  2888. serial, MAX_SERIAL);
  2889. for (i = 0; i < MAX_SERIAL; ++i) {
  2890. if ((serial[i] < 0x20) || (serial[i] > 0x7e)) {
  2891. serial[i] = '\0';
  2892. break;
  2893. }
  2894. }
  2895. if (!i) {
  2896. kfree(serial);
  2897. serial = NULL;
  2898. } else {
  2899. /* Limit the size of the buffer to just what is needed to
  2900. * actually hold the serial number. */
  2901. unsigned char *new_serial;
  2902. new_serial = kasprintf(GFP_KERNEL, "%s", serial);
  2903. kfree(serial);
  2904. serial = new_serial;
  2905. }
  2906. error_exit:
  2907. wcxb_spi_device_destroy(flash_spi_device);
  2908. wcxb_spi_master_destroy(flash_spi_master);
  2909. return serial;
  2910. }
  2911. /**
  2912. * t43x_assign_num - Assign wc->num a unique value and place on card_list
  2913. *
  2914. */
  2915. static void t43x_assign_num(struct t43x *wc)
  2916. {
  2917. mutex_lock(&card_list_lock);
  2918. if (list_empty(&card_list)) {
  2919. wc->num = 0;
  2920. list_add(&wc->card_node, &card_list);
  2921. } else {
  2922. struct t43x *cur;
  2923. struct list_head *insert_pos;
  2924. int new_num = 0;
  2925. insert_pos = &card_list;
  2926. list_for_each_entry(cur, &card_list, card_node) {
  2927. if (new_num != cur->num)
  2928. break;
  2929. new_num++;
  2930. insert_pos = &cur->card_node;
  2931. }
  2932. wc->num = new_num;
  2933. list_add_tail(&wc->card_node, insert_pos);
  2934. }
  2935. mutex_unlock(&card_list_lock);
  2936. }
  2937. /*
  2938. * Initialize the card (one time)
  2939. *
  2940. */
  2941. static int __devinit t43x_init_one(struct pci_dev *pdev,
  2942. const struct pci_device_id *ent)
  2943. {
  2944. struct t43x *wc;
  2945. struct t43x_span *ts;
  2946. unsigned int x;
  2947. int res;
  2948. enum linemode type;
  2949. wc = kzalloc(sizeof(*wc), GFP_KERNEL);
  2950. if (!wc) {
  2951. pci_disable_device(pdev);
  2952. return -ENOMEM;
  2953. }
  2954. t43x_assign_num(wc);
  2955. wc->blinktimer = jiffies;
  2956. dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  2957. wc->not_ready = 1;
  2958. spin_lock_init(&wc->reglock);
  2959. pci_set_drvdata(pdev, wc);
  2960. wc->xb.pdev = pdev;
  2961. wc->xb.ops = &xb_ops;
  2962. wc->xb.debug = &debug;
  2963. res = wcxb_init(&wc->xb, KBUILD_MODNAME, 0);
  2964. if (res)
  2965. goto fail_exit;
  2966. mutex_init(&wc->lock);
  2967. setup_timer(&wc->timer, t43x_timer, (unsigned long)wc);
  2968. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  2969. INIT_WORK(&wc->timer_work, timer_work_func, wc);
  2970. #else
  2971. INIT_WORK(&wc->timer_work, timer_work_func);
  2972. #endif
  2973. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  2974. INIT_WORK(&wc->clksrc_work.work, t43x_clksrc_work_fn,
  2975. &wc->clksrc_work.work);
  2976. #else
  2977. INIT_DELAYED_WORK(&wc->clksrc_work.work, t43x_clksrc_work_fn);
  2978. #endif
  2979. spin_lock_init(&wc->clksrc_work.lock);
  2980. wc->ddev = dahdi_create_device();
  2981. if (!wc->ddev) {
  2982. res = -ENOMEM;
  2983. goto fail_exit;
  2984. }
  2985. wc->ddev->manufacturer = "Digium";
  2986. wc->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
  2987. pdev->bus->number,
  2988. PCI_SLOT(pdev->devfn) + 1);
  2989. if (!wc->ddev->location) {
  2990. res = -ENOMEM;
  2991. goto fail_exit;
  2992. }
  2993. wc->ddev->hardware_id = t43x_read_serial(wc);
  2994. if (wc->ddev->hardware_id == NULL) {
  2995. dev_info(&wc->xb.pdev->dev, "Unable to read valid serial number\n");
  2996. res = -EIO;
  2997. goto fail_exit;
  2998. }
  2999. if (strncmp(wc->ddev->hardware_id, "1TE435F", 7) == 0) {
  3000. /* Quad-span PCIE */
  3001. wc->numspans = 4;
  3002. wc->devtype = &te435;
  3003. } else if (strncmp(wc->ddev->hardware_id, "1TE235F", 7) == 0) {
  3004. /* Dual-span PCIE */
  3005. wc->numspans = 2;
  3006. wc->devtype = &te235;
  3007. } else if (strncmp(wc->ddev->hardware_id, "1TE436F", 7) == 0) {
  3008. /* Quad-span PCI */
  3009. wc->numspans = 4;
  3010. wc->devtype = &te436;
  3011. } else if (strncmp(wc->ddev->hardware_id, "1TE236F", 7) == 0) {
  3012. /* Dual-span PCI */
  3013. wc->numspans = 2;
  3014. wc->devtype = &te236;
  3015. } else {
  3016. dev_info(&wc->xb.pdev->dev,
  3017. "Unable to identify board type from serial number %s\n",
  3018. wc->ddev->hardware_id);
  3019. res = -EIO;
  3020. goto fail_exit;
  3021. }
  3022. /* Check for field updatable firmware */
  3023. if ((wc->devtype == &te435) || (wc->devtype == &te235)) {
  3024. res = wcxb_check_firmware(&wc->xb, TE435_VERSION,
  3025. TE435_FW_FILENAME, force_firmware, WCXB_RESET_NOW);
  3026. } else if ((wc->devtype == &te436) || (wc->devtype == &te236)) {
  3027. res = wcxb_check_firmware(&wc->xb, TE436_VERSION,
  3028. TE436_FW_FILENAME, force_firmware, WCXB_RESET_NOW);
  3029. } else {
  3030. res = -EIO;
  3031. }
  3032. if (res)
  3033. goto fail_exit;
  3034. for (x = 0; x < wc->numspans; x++) {
  3035. ts = kzalloc(sizeof(*wc->tspans[x]), GFP_KERNEL);
  3036. if (!ts) {
  3037. res = -ENOMEM;
  3038. goto fail_exit;
  3039. }
  3040. wc->tspans[x] = ts;
  3041. }
  3042. wc->wq = create_singlethread_workqueue(KBUILD_MODNAME);
  3043. if (!wc->wq) {
  3044. res = -ENOMEM;
  3045. goto fail_exit;
  3046. }
  3047. wcxb_set_minlatency(&wc->xb, latency);
  3048. wcxb_set_maxlatency(&wc->xb, max_latency);
  3049. create_sysfs_files(wc);
  3050. res = t43x_hardware_post_init(wc, &type);
  3051. if (res)
  3052. goto fail_exit;
  3053. t43x_init_spans(wc, type);
  3054. if (!wc->vpm)
  3055. t43x_vpm_init(wc);
  3056. if (wc->vpm) {
  3057. wc->ddev->devicetype = kasprintf(GFP_KERNEL,
  3058. "%s (%s)", wc->devtype->name,
  3059. wc->vpm_name);
  3060. } else {
  3061. wc->ddev->devicetype = kasprintf(GFP_KERNEL,
  3062. "%s", wc->devtype->name);
  3063. }
  3064. res = dahdi_register_device(wc->ddev, &wc->xb.pdev->dev);
  3065. if (res) {
  3066. dev_info(&wc->xb.pdev->dev, "Unable to register with DAHDI\n");
  3067. goto fail_exit;
  3068. }
  3069. if (wc->ddev->hardware_id) {
  3070. dev_info(&wc->xb.pdev->dev, "Found a %s (SN: %s)\n",
  3071. wc->devtype->name, wc->ddev->hardware_id);
  3072. } else {
  3073. dev_info(&wc->xb.pdev->dev, "Found a %s\n",
  3074. wc->devtype->name);
  3075. }
  3076. wc->not_ready = 0;
  3077. return 0;
  3078. fail_exit:
  3079. if (&wc->xb)
  3080. wcxb_release(&wc->xb);
  3081. if (debug)
  3082. dev_info(&wc->xb.pdev->dev, "***At fail_exit in init_one***\n");
  3083. for (x = 0; x < wc->numspans; x++)
  3084. kfree(wc->tspans[x]);
  3085. free_wc(wc);
  3086. return res;
  3087. }
  3088. static void __devexit t43x_remove_one(struct pci_dev *pdev)
  3089. {
  3090. struct t43x *wc = pci_get_drvdata(pdev);
  3091. dev_info(&wc->xb.pdev->dev, "Removing a Wildcard TE43x.\n");
  3092. if (!wc)
  3093. return;
  3094. wc->not_ready = 1;
  3095. smp_mb__after_atomic();
  3096. /* Stop everything */
  3097. wcxb_stop(&wc->xb);
  3098. /* Leave framer in reset so it no longer transmits */
  3099. wcxb_gpio_clear(&wc->xb, FALC_CPU_RESET);
  3100. /* Turn off status LEDs */
  3101. t43x_setleds(wc, 0);
  3102. if (wc->vpm)
  3103. release_vpm450m(wc->vpm);
  3104. wc->vpm = NULL;
  3105. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  3106. # ifdef T43X_HAVE_CANCEL_WORK_SYNC
  3107. cancel_work_sync(&wc->clksrc_work.work);
  3108. # else
  3109. cancel_delayed_work(&wc->clksrc_work.work);
  3110. flush_workqueue(wc->wq);
  3111. # endif
  3112. #else
  3113. cancel_delayed_work_sync(&wc->clksrc_work.work);
  3114. #endif
  3115. del_timer_sync(&wc->timer);
  3116. flush_workqueue(wc->wq);
  3117. del_timer_sync(&wc->timer);
  3118. dahdi_unregister_device(wc->ddev);
  3119. remove_sysfs_files(wc);
  3120. wcxb_release(&wc->xb);
  3121. free_wc(wc);
  3122. }
  3123. static DEFINE_PCI_DEVICE_TABLE(t43x_pci_tbl) = {
  3124. { 0xd161, 0x800e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3125. { 0xd161, 0x8013, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3126. { 0 }
  3127. };
  3128. static void t43x_shutdown(struct pci_dev *pdev)
  3129. {
  3130. struct t43x *wc = pci_get_drvdata(pdev);
  3131. dev_info(&wc->xb.pdev->dev, "Quiescing a Wildcard TE43x.\n");
  3132. if (!wc)
  3133. return;
  3134. /* Stop everything */
  3135. wcxb_stop(&wc->xb);
  3136. }
  3137. static int t43x_suspend(struct pci_dev *pdev, pm_message_t state)
  3138. {
  3139. return -ENOSYS;
  3140. }
  3141. MODULE_DEVICE_TABLE(pci, t43x_pci_tbl);
  3142. static struct pci_driver t43x_driver = {
  3143. .name = "wcte43x",
  3144. .probe = t43x_init_one,
  3145. .remove = __devexit_p(t43x_remove_one),
  3146. .shutdown = t43x_shutdown,
  3147. .suspend = t43x_suspend,
  3148. .id_table = t43x_pci_tbl,
  3149. };
  3150. static int __init t43x_init(void)
  3151. {
  3152. int res;
  3153. if (strcasecmp(default_linemode, "t1") &&
  3154. strcasecmp(default_linemode, "j1") &&
  3155. strcasecmp(default_linemode, "e1")) {
  3156. pr_err("'%s' is an unknown span type.\n", default_linemode);
  3157. default_linemode = "t1";
  3158. return -EINVAL;
  3159. }
  3160. res = dahdi_pci_module(&t43x_driver);
  3161. if (res)
  3162. return -ENODEV;
  3163. return 0;
  3164. }
  3165. static void __exit t43x_cleanup(void)
  3166. {
  3167. pci_unregister_driver(&t43x_driver);
  3168. }
  3169. module_param(debug, int, S_IRUGO | S_IWUSR);
  3170. module_param(timingcable, int, 0600);
  3171. module_param(default_linemode, charp, S_IRUGO);
  3172. MODULE_PARM_DESC(default_linemode, "\"t1\"(default), \"e1\", or \"j1\".");
  3173. module_param(alarmdebounce, int, S_IRUGO | S_IWUSR);
  3174. module_param(losalarmdebounce, int, S_IRUGO | S_IWUSR);
  3175. module_param(aisalarmdebounce, int, S_IRUGO | S_IWUSR);
  3176. module_param(yelalarmdebounce, int, S_IRUGO | S_IWUSR);
  3177. module_param(vpmsupport, int, 0600);
  3178. module_param(force_firmware, int, S_IRUGO);
  3179. module_param(latency, int, S_IRUGO);
  3180. MODULE_PARM_DESC(latency, "How many milliseconds of audio to buffer between card and host (3ms default). This number will increase during runtime, dynamically, if dahdi detects that it is too small. This is commonly refered to as a \"latency bump\"");
  3181. module_param(max_latency, int, 0600);
  3182. MODULE_PARM_DESC(max_latency, "The maximum amount of latency that the driver will permit.");
  3183. MODULE_DESCRIPTION("Wildcard Digital Card Driver");
  3184. MODULE_AUTHOR("Digium Incorporated <support@digium.com>");
  3185. MODULE_LICENSE("GPL v2");
  3186. module_init(t43x_init);
  3187. module_exit(t43x_cleanup);