dahdi-base.c 280 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614
  1. /*
  2. * DAHDI Telephony Interface Driver
  3. *
  4. * Written by Mark Spencer <markster@digium.com>
  5. * Based on previous works, designs, and architectures conceived and
  6. * written by Jim Dixon <jim@lambdatel.com>.
  7. *
  8. * Special thanks to Steve Underwood <steve@coppice.org>
  9. * for substantial contributions to signal processing functions
  10. * in DAHDI and the Zapata library.
  11. *
  12. * Yury Bokhoncovich <byg@cf1.ru>
  13. * Adaptation for 2.4.20+ kernels (HDLC API was changed)
  14. * The work has been performed as a part of our move
  15. * from Cisco 3620 to IBM x305 here in F1 Group
  16. *
  17. * Copyright (C) 2001 Jim Dixon / Zapata Telephony.
  18. * Copyright (C) 2001 - 2012 Digium, Inc.
  19. *
  20. * All rights reserved.
  21. *
  22. */
  23. /*
  24. * See http://www.asterisk.org for more information about
  25. * the Asterisk project. Please do not directly contact
  26. * any of the maintainers of this project for assistance;
  27. * the project provides a web site, mailing lists and IRC
  28. * channels for your use.
  29. *
  30. * This program is free software, distributed under the terms of
  31. * the GNU General Public License Version 2 as published by the
  32. * Free Software Foundation. See the LICENSE file included with
  33. * this program for more details.
  34. */
  35. #include <linux/kernel.h>
  36. #include <linux/errno.h>
  37. #include <linux/module.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/pci.h>
  41. #include <linux/init.h>
  42. #include <linux/version.h>
  43. #include <linux/ctype.h>
  44. #include <linux/kmod.h>
  45. #include <linux/moduleparam.h>
  46. #include <linux/sched.h>
  47. #include <linux/list.h>
  48. #include <linux/delay.h>
  49. #include <linux/mutex.h>
  50. #include <linux/ktime.h>
  51. #include <linux/slab.h>
  52. #if defined(HAVE_UNLOCKED_IOCTL) && defined(CONFIG_BKL)
  53. #include <linux/smp_lock.h>
  54. #endif
  55. #include <linux/ppp_defs.h>
  56. #include <asm/atomic.h>
  57. #define DAHDI_PRINK_MACROS_USE_debug
  58. /* Grab fasthdlc with tables */
  59. #define FAST_HDLC_NEED_TABLES
  60. #include <dahdi/kernel.h>
  61. #include "ecdis.h"
  62. #include "dahdi.h"
  63. #ifdef CONFIG_DAHDI_PPP
  64. #include <linux/netdevice.h>
  65. #include <linux/if.h>
  66. #include <linux/if_ppp.h>
  67. #endif
  68. #ifdef CONFIG_DAHDI_NET
  69. #include <linux/netdevice.h>
  70. #endif
  71. #include "hpec/hpec_user.h"
  72. #include <stdbool.h>
  73. #if defined(EMPULSE) && defined(EMFLASH)
  74. #error "You cannot define both EMPULSE and EMFLASH"
  75. #endif
  76. /* Get helper arithmetic */
  77. #include "arith.h"
  78. #if defined(CONFIG_DAHDI_MMX) || defined(ECHO_CAN_FP)
  79. #include <asm/i387.h>
  80. #endif
  81. #define hdlc_to_chan(h) (((struct dahdi_hdlc *)(h))->chan)
  82. #define netdev_to_chan(h) (((struct dahdi_hdlc *)(dev_to_hdlc(h)->priv))->chan)
  83. #define chan_to_netdev(h) ((h)->hdlcnetdev->netdev)
  84. /* macro-oni for determining a unit (channel) number */
  85. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  86. #define UNIT(file) MINOR(file->f_dentry->d_inode->i_rdev)
  87. #else
  88. #define UNIT(file) MINOR(file->f_path.dentry->d_inode->i_rdev)
  89. #endif
  90. EXPORT_SYMBOL(dahdi_transcode_fops);
  91. EXPORT_SYMBOL(dahdi_init_tone_state);
  92. EXPORT_SYMBOL(dahdi_mf_tone);
  93. EXPORT_SYMBOL(__dahdi_mulaw);
  94. EXPORT_SYMBOL(__dahdi_alaw);
  95. #ifdef CONFIG_CALC_XLAW
  96. EXPORT_SYMBOL(__dahdi_lineartoulaw);
  97. EXPORT_SYMBOL(__dahdi_lineartoalaw);
  98. #else
  99. EXPORT_SYMBOL(__dahdi_lin2mu);
  100. EXPORT_SYMBOL(__dahdi_lin2a);
  101. #endif
  102. EXPORT_SYMBOL(dahdi_rbsbits);
  103. EXPORT_SYMBOL(dahdi_qevent_nolock);
  104. EXPORT_SYMBOL(dahdi_qevent_lock);
  105. EXPORT_SYMBOL(dahdi_hooksig);
  106. EXPORT_SYMBOL(dahdi_alarm_notify);
  107. EXPORT_SYMBOL(dahdi_hdlc_abort);
  108. EXPORT_SYMBOL(dahdi_hdlc_finish);
  109. EXPORT_SYMBOL(dahdi_hdlc_getbuf);
  110. EXPORT_SYMBOL(dahdi_hdlc_putbuf);
  111. EXPORT_SYMBOL(dahdi_alarm_channel);
  112. EXPORT_SYMBOL(dahdi_register_echocan_factory);
  113. EXPORT_SYMBOL(dahdi_unregister_echocan_factory);
  114. EXPORT_SYMBOL(dahdi_set_hpec_ioctl);
  115. #ifdef CONFIG_PROC_FS
  116. static struct proc_dir_entry *root_proc_entry;
  117. #endif
  118. static int deftaps = 64;
  119. int debug;
  120. #define DEBUG_MAIN (1 << 0)
  121. #define DEBUG_RBS (1 << 5)
  122. static int hwec_overrides_swec = 1;
  123. /*!
  124. * \brief states for transmit signalling
  125. */
  126. enum dahdi_txstate {
  127. DAHDI_TXSTATE_ONHOOK,
  128. DAHDI_TXSTATE_OFFHOOK,
  129. DAHDI_TXSTATE_START,
  130. DAHDI_TXSTATE_PREWINK,
  131. DAHDI_TXSTATE_WINK,
  132. DAHDI_TXSTATE_PREFLASH,
  133. DAHDI_TXSTATE_FLASH,
  134. DAHDI_TXSTATE_DEBOUNCE,
  135. DAHDI_TXSTATE_AFTERSTART,
  136. DAHDI_TXSTATE_RINGON,
  137. DAHDI_TXSTATE_RINGOFF,
  138. DAHDI_TXSTATE_KEWL,
  139. DAHDI_TXSTATE_AFTERKEWL,
  140. DAHDI_TXSTATE_PULSEBREAK,
  141. DAHDI_TXSTATE_PULSEMAKE,
  142. DAHDI_TXSTATE_PULSEAFTER,
  143. };
  144. typedef short sumtype[DAHDI_MAX_CHUNKSIZE];
  145. static sumtype sums[(DAHDI_MAX_CONF + 1) * 3];
  146. /* Translate conference aliases into actual conferences
  147. and vice-versa */
  148. static short confalias[DAHDI_MAX_CONF + 1];
  149. static short confrev[DAHDI_MAX_CONF + 1];
  150. static sumtype *conf_sums_next;
  151. static sumtype *conf_sums;
  152. static sumtype *conf_sums_prev;
  153. static struct dahdi_span *master_span;
  154. struct file_operations *dahdi_transcode_fops = NULL;
  155. #ifdef CONFIG_DAHDI_CONFLINK
  156. static struct {
  157. int src; /* source conf number */
  158. int dst; /* dst conf number */
  159. } conf_links[DAHDI_MAX_CONF + 1];
  160. static int maxlinks;
  161. #endif
  162. #ifdef CONFIG_DAHDI_CORE_TIMER
  163. static struct core_timer {
  164. struct timer_list timer;
  165. struct timespec start_interval;
  166. unsigned long interval;
  167. int dahdi_receive_used;
  168. atomic_t count;
  169. atomic_t shutdown;
  170. atomic_t last_count;
  171. } core_timer;
  172. #endif /* CONFIG_DAHDI_CORE_TIMER */
  173. enum dahdi_digit_mode {
  174. DIGIT_MODE_DTMF,
  175. DIGIT_MODE_MFR1,
  176. DIGIT_MODE_PULSE,
  177. DIGIT_MODE_MFR2_FWD,
  178. DIGIT_MODE_MFR2_REV,
  179. };
  180. /* At the end of silence, the tone stops */
  181. static struct dahdi_tone dtmf_silence = {
  182. .tonesamples = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_DTMF_LENGTH),
  183. };
  184. /* At the end of silence, the tone stops */
  185. static struct dahdi_tone mfr1_silence = {
  186. .tonesamples = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_MFR1_LENGTH),
  187. };
  188. /* At the end of silence, the tone stops */
  189. static struct dahdi_tone mfr2_silence = {
  190. .tonesamples = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_MFR2_LENGTH),
  191. };
  192. /* A pause in the dialing */
  193. static struct dahdi_tone tone_pause = {
  194. .tonesamples = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_PAUSE_LENGTH),
  195. };
  196. static struct dahdi_dialparams global_dialparams = {
  197. .dtmf_tonelen = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_DTMF_LENGTH),
  198. .mfv1_tonelen = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_MFR1_LENGTH),
  199. .mfr2_tonelen = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_MFR2_LENGTH),
  200. };
  201. static DEFINE_MUTEX(global_dialparamslock);
  202. static int dahdi_chan_ioctl(struct file *file, unsigned int cmd, unsigned long data);
  203. #if defined(CONFIG_DAHDI_MMX) || defined(ECHO_CAN_FP)
  204. #if (defined(CONFIG_X86) && !defined(CONFIG_X86_64)) || defined(CONFIG_I386)
  205. struct fpu_save_buf {
  206. unsigned long cr0;
  207. unsigned long fpu_buf[128];
  208. };
  209. static DEFINE_PER_CPU(struct fpu_save_buf, fpu_buf);
  210. /** dahdi_kernel_fpu_begin() - Save floating point registers
  211. *
  212. * This function is similar to kernel_fpu_begin() . However it is
  213. * designed to work in an interrupt context. Restoring must be done with
  214. * dahdi_kernel_fpu_end().
  215. *
  216. * Furthermore, the whole code between the call to
  217. * dahdi_kernel_fpu_begin() and dahdi_kernel_fpu_end() must reside
  218. * inside a spinlock. Otherwise the context might be restored to the
  219. * wrong process.
  220. *
  221. * Current implementation is x86/ia32-specific and will not even build on
  222. * x86_64)
  223. * */
  224. static inline void dahdi_kernel_fpu_begin(void)
  225. {
  226. struct fpu_save_buf *buf = &__get_cpu_var(fpu_buf);
  227. __asm__ __volatile__ ("movl %%cr0,%0; clts" : "=r" (buf->cr0));
  228. __asm__ __volatile__ ("fnsave %0" : "=m" (buf->fpu_buf));
  229. }
  230. /** dahdi_kernel_fpu_end() - restore floating point context
  231. *
  232. * Must be used with context saved by dahdi_kernel_fpu_begin(). See its
  233. * documentation for further information.
  234. */
  235. static inline void dahdi_kernel_fpu_end(void)
  236. {
  237. struct fpu_save_buf *buf = &__get_cpu_var(fpu_buf);
  238. __asm__ __volatile__ ("frstor %0" : "=m" (buf->fpu_buf));
  239. __asm__ __volatile__ ("movl %0,%%cr0" : : "r" (buf->cr0));
  240. }
  241. #else /* We haven't fixed FP context saving/restoring yet */
  242. /* Very strange things can happen when the context is not properly
  243. * restored. OTOH, some people do report success with this. Hence we
  244. * so far just issue a warning */
  245. #warning CONFIG_DAHDI_MMX may behave randomly on this platform
  246. #define dahdi_kernel_fpu_begin kernel_fpu_begin
  247. #define dahdi_kernel_fpu_end kernel_fpu_end
  248. #endif
  249. #endif
  250. struct dahdi_timer {
  251. spinlock_t lock;
  252. int ms; /* Countdown */
  253. int pos; /* Position */
  254. int ping; /* Whether we've been ping'd */
  255. int tripped; /* Whether we're tripped */
  256. struct list_head list;
  257. wait_queue_head_t sel;
  258. };
  259. static LIST_HEAD(dahdi_timers);
  260. static DEFINE_SPINLOCK(dahdi_timer_lock);
  261. #define DEFAULT_TONE_ZONE (-1)
  262. struct dahdi_zone {
  263. int ringcadence[DAHDI_MAX_CADENCE];
  264. struct dahdi_tone *tones[DAHDI_TONE_MAX];
  265. /* Each of these is a circular list
  266. of dahdi_tones to generate what we
  267. want. Use NULL if the tone is
  268. unavailable */
  269. struct dahdi_tone dtmf[16]; /* DTMF tones for this zone, with desired length */
  270. struct dahdi_tone dtmf_continuous[16]; /* DTMF tones for this zone, continuous play */
  271. struct dahdi_tone mfr1[15]; /* MFR1 tones for this zone, with desired length */
  272. struct dahdi_tone mfr2_fwd[15]; /* MFR2 FWD tones for this zone, with desired length */
  273. struct dahdi_tone mfr2_rev[15]; /* MFR2 REV tones for this zone, with desired length */
  274. struct dahdi_tone mfr2_fwd_continuous[16]; /* MFR2 FWD tones for this zone, continuous play */
  275. struct dahdi_tone mfr2_rev_continuous[16]; /* MFR2 REV tones for this zone, continuous play */
  276. struct list_head node;
  277. struct kref refcount;
  278. const char *name; /* Informational, only */
  279. u8 num;
  280. };
  281. static void tone_zone_release(struct kref *kref)
  282. {
  283. struct dahdi_zone *z = container_of(kref, struct dahdi_zone, refcount);
  284. kfree(z->name);
  285. kfree(z);
  286. }
  287. /**
  288. * tone_zone_put() - Release the reference on the tone_zone.
  289. *
  290. * On old kernels, since kref_put does not have a return value, we'll just
  291. * always report that we released the memory.
  292. *
  293. */
  294. static inline int tone_zone_put(struct dahdi_zone *z)
  295. {
  296. return kref_put(&z->refcount, tone_zone_release);
  297. }
  298. static inline void tone_zone_get(struct dahdi_zone *z)
  299. {
  300. kref_get(&z->refcount);
  301. }
  302. static DEFINE_SPINLOCK(zone_lock);
  303. /* The first zone on the list is the default zone. */
  304. static LIST_HEAD(tone_zones);
  305. static inline struct device *span_device(struct dahdi_span *span)
  306. {
  307. return &span->parent->dev;
  308. }
  309. /* Protects the span_list and pseudo_chans lists from concurrent access in
  310. * process context. The spin_lock is needed to synchronize with the interrupt
  311. * handler. */
  312. static DEFINE_SPINLOCK(chan_lock);
  313. struct pseudo_chan {
  314. struct dahdi_chan chan;
  315. struct list_head node;
  316. };
  317. static inline struct pseudo_chan *chan_to_pseudo(struct dahdi_chan *chan)
  318. {
  319. return container_of(chan, struct pseudo_chan, chan);
  320. }
  321. enum { FIRST_PSEUDO_CHANNEL = 0x8000, };
  322. /* This list is protected by the chan_lock. */
  323. static LIST_HEAD(pseudo_chans);
  324. /**
  325. * is_pseudo_chan() - By definition pseudo channels are not on a span.
  326. */
  327. static inline bool is_pseudo_chan(const struct dahdi_chan *chan)
  328. {
  329. return (NULL == chan->span);
  330. }
  331. static DEFINE_MUTEX(registration_mutex);
  332. static LIST_HEAD(span_list);
  333. static unsigned long
  334. __for_each_channel(unsigned long (*func)(struct dahdi_chan *chan,
  335. unsigned long data),
  336. unsigned long data)
  337. {
  338. int res;
  339. struct dahdi_span *s;
  340. struct pseudo_chan *pseudo;
  341. list_for_each_entry(s, &span_list, spans_node) {
  342. unsigned long x;
  343. for (x = 0; x < s->channels; x++) {
  344. struct dahdi_chan *const chan = s->chans[x];
  345. res = func(chan, data);
  346. if (res)
  347. return res;
  348. }
  349. }
  350. list_for_each_entry(pseudo, &pseudo_chans, node) {
  351. res = func(&pseudo->chan, data);
  352. if (res)
  353. return res;
  354. }
  355. return 0;
  356. }
  357. /**
  358. * _chan_from_num - Lookup a channel
  359. *
  360. * Must be called with the registration_mutex held.
  361. *
  362. */
  363. static struct dahdi_chan *_chan_from_num(unsigned int channo)
  364. {
  365. struct dahdi_span *s;
  366. struct pseudo_chan *pseudo;
  367. if (channo >= FIRST_PSEUDO_CHANNEL) {
  368. list_for_each_entry(pseudo, &pseudo_chans, node) {
  369. if (pseudo->chan.channo == channo)
  370. return &pseudo->chan;
  371. }
  372. return NULL;
  373. }
  374. /* When searching for the channel amongst the spans, we can use the
  375. * fact that channels on a span must be numbered consecutively to skip
  376. * checking each individual channel. */
  377. list_for_each_entry(s, &span_list, spans_node) {
  378. unsigned int basechan;
  379. struct dahdi_chan *chan;
  380. if (unlikely(!s->channels))
  381. continue;
  382. basechan = s->chans[0]->channo;
  383. if (channo >= (basechan + s->channels))
  384. continue;
  385. /* Since all the spans should be on the list in sorted order,
  386. * if channo is less than base chan, the caller must be
  387. * looking for a channel that has already been removed. */
  388. if (unlikely(channo < basechan))
  389. return NULL;
  390. chan = s->chans[channo - basechan];
  391. WARN_ON(chan->channo != channo);
  392. return chan;
  393. }
  394. return NULL;
  395. }
  396. static struct dahdi_chan *chan_from_num(unsigned int channo)
  397. {
  398. struct dahdi_chan *chan;
  399. mutex_lock(&registration_mutex);
  400. chan = _chan_from_num(channo);
  401. mutex_unlock(&registration_mutex);
  402. return chan;
  403. }
  404. static inline struct dahdi_chan *chan_from_file(struct file *file)
  405. {
  406. return (file->private_data) ?
  407. file->private_data : chan_from_num(UNIT(file));
  408. }
  409. /**
  410. * _find_span() - Find a span by span number.
  411. *
  412. * Must be called with registration_mutex held.
  413. *
  414. */
  415. static struct dahdi_span *_find_span(int spanno)
  416. {
  417. struct dahdi_span *s;
  418. list_for_each_entry(s, &span_list, spans_node) {
  419. if (s->spanno == spanno) {
  420. return s;
  421. }
  422. }
  423. return NULL;
  424. }
  425. /**
  426. * span_find_and_get() - Search for the span by number, and if found take out
  427. * a reference on it.
  428. *
  429. * When you are no longer using the returned pointer, you must release it with
  430. * a put_span call.
  431. *
  432. */
  433. static struct dahdi_span *span_find_and_get(int spanno)
  434. {
  435. struct dahdi_span *found;
  436. mutex_lock(&registration_mutex);
  437. found = _find_span(spanno);
  438. if (found && !get_span(found))
  439. found = NULL;
  440. mutex_unlock(&registration_mutex);
  441. return found;
  442. }
  443. static unsigned int span_count(void)
  444. {
  445. unsigned int count = 0;
  446. struct dahdi_span *s;
  447. unsigned long flags;
  448. spin_lock_irqsave(&chan_lock, flags);
  449. list_for_each_entry(s, &span_list, spans_node)
  450. ++count;
  451. spin_unlock_irqrestore(&chan_lock, flags);
  452. return count;
  453. }
  454. static inline bool can_provide_timing(const struct dahdi_span *const s)
  455. {
  456. return !s->cannot_provide_timing;
  457. }
  458. static int maxconfs;
  459. short __dahdi_mulaw[256];
  460. short __dahdi_alaw[256];
  461. #ifndef CONFIG_CALC_XLAW
  462. u_char __dahdi_lin2mu[16384];
  463. u_char __dahdi_lin2a[16384];
  464. #endif
  465. static u_char defgain[256];
  466. #define NUM_SIGS 10
  467. static DEFINE_SPINLOCK(ecfactory_list_lock);
  468. static LIST_HEAD(ecfactory_list);
  469. struct ecfactory {
  470. const struct dahdi_echocan_factory *ec;
  471. struct list_head list;
  472. };
  473. int dahdi_register_echocan_factory(const struct dahdi_echocan_factory *ec)
  474. {
  475. struct ecfactory *cur;
  476. struct ecfactory *new;
  477. WARN_ON(!ec->owner);
  478. new = kzalloc(sizeof(*new), GFP_KERNEL);
  479. if (!new)
  480. return -ENOMEM;
  481. INIT_LIST_HEAD(&new->list);
  482. spin_lock(&ecfactory_list_lock);
  483. /* make sure it isn't already registered */
  484. list_for_each_entry(cur, &ecfactory_list, list) {
  485. if (cur->ec == ec) {
  486. spin_unlock(&ecfactory_list_lock);
  487. kfree(new);
  488. return -EPERM;
  489. }
  490. }
  491. new->ec = ec;
  492. list_add_tail(&new->list, &ecfactory_list);
  493. spin_unlock(&ecfactory_list_lock);
  494. return 0;
  495. }
  496. void dahdi_unregister_echocan_factory(const struct dahdi_echocan_factory *ec)
  497. {
  498. struct ecfactory *cur, *next;
  499. spin_lock(&ecfactory_list_lock);
  500. list_for_each_entry_safe(cur, next, &ecfactory_list, list) {
  501. if (cur->ec == ec) {
  502. list_del(&cur->list);
  503. kfree(cur);
  504. break;
  505. }
  506. }
  507. spin_unlock(&ecfactory_list_lock);
  508. }
  509. /* Is this span our syncronization master? */
  510. int dahdi_is_sync_master(const struct dahdi_span *span)
  511. {
  512. return span == master_span;
  513. }
  514. static inline void rotate_sums(void)
  515. {
  516. /* Rotate where we sum and so forth */
  517. static int pos = 0;
  518. conf_sums_prev = sums + (DAHDI_MAX_CONF + 1) * pos;
  519. conf_sums = sums + (DAHDI_MAX_CONF + 1) * ((pos + 1) % 3);
  520. conf_sums_next = sums + (DAHDI_MAX_CONF + 1) * ((pos + 2) % 3);
  521. pos = (pos + 1) % 3;
  522. memset(conf_sums_next, 0, maxconfs * sizeof(sumtype));
  523. }
  524. /**
  525. * is_chan_dacsed() - True if chan is sourcing it's data from another channel.
  526. *
  527. */
  528. static inline bool is_chan_dacsed(const struct dahdi_chan *const chan)
  529. {
  530. return (NULL != chan->dacs_chan);
  531. }
  532. /**
  533. * can_dacs_chans() - Returns true if it may be possible to dacs two channels.
  534. *
  535. */
  536. static bool can_dacs_chans(struct dahdi_chan *dst, struct dahdi_chan *src)
  537. {
  538. if (src && dst && src->span && dst->span && src->span->ops &&
  539. dst->span->ops && src->span->ops->dacs &&
  540. (src->span->ops->dacs == dst->span->ops->dacs))
  541. return true;
  542. else
  543. return false;
  544. }
  545. /**
  546. * dahdi_chan_dacs() - Cross (or uncross) connect two channels.
  547. * @dst: Channel on which to transmit the src data.
  548. * @src: NULL to disable cross connect, otherwise the source of the
  549. * data.
  550. *
  551. * This allows those boards that support it to cross connect one channel to
  552. * another in hardware. If the cards cannot be crossed, uncross the
  553. * destination channel by default..
  554. *
  555. */
  556. static int dahdi_chan_dacs(struct dahdi_chan *dst, struct dahdi_chan *src)
  557. {
  558. int ret = 0;
  559. if (can_dacs_chans(dst, src))
  560. ret = dst->span->ops->dacs(dst, src);
  561. else if (dst->span && dst->span->ops->dacs)
  562. ret = dst->span->ops->dacs(dst, NULL);
  563. return ret;
  564. }
  565. static void dahdi_disable_dacs(struct dahdi_chan *chan)
  566. {
  567. dahdi_chan_dacs(chan, NULL);
  568. }
  569. /*!
  570. * \return quiescent (idle) signalling states, for the various signalling types
  571. */
  572. static int dahdi_q_sig(struct dahdi_chan *chan)
  573. {
  574. int x;
  575. static const unsigned int in_sig[NUM_SIGS][2] = {
  576. { DAHDI_SIG_NONE, 0 },
  577. { DAHDI_SIG_EM, (DAHDI_ABIT << 8) },
  578. { DAHDI_SIG_FXSLS, DAHDI_BBIT | (DAHDI_BBIT << 8) },
  579. { DAHDI_SIG_FXSGS, DAHDI_ABIT | DAHDI_BBIT | ((DAHDI_ABIT | DAHDI_BBIT) << 8) },
  580. { DAHDI_SIG_FXSKS, DAHDI_BBIT | ((DAHDI_ABIT | DAHDI_BBIT) << 8) },
  581. { DAHDI_SIG_FXOLS, (DAHDI_ABIT << 8) },
  582. { DAHDI_SIG_FXOGS, DAHDI_BBIT | ((DAHDI_ABIT | DAHDI_BBIT) << 8) },
  583. { DAHDI_SIG_FXOKS, (DAHDI_ABIT << 8) },
  584. { DAHDI_SIG_SF, 0 },
  585. { DAHDI_SIG_EM_E1, DAHDI_DBIT | ((DAHDI_ABIT | DAHDI_DBIT) << 8) },
  586. };
  587. /* must have span to begin with */
  588. if (!chan->span)
  589. return -1;
  590. /* if RBS does not apply, return error */
  591. if (!(chan->span->flags & DAHDI_FLAG_RBS) || !chan->span->ops->rbsbits)
  592. return -1;
  593. if (chan->sig == DAHDI_SIG_CAS)
  594. return chan->idlebits;
  595. for (x = 0; x < NUM_SIGS; x++) {
  596. if (in_sig[x][0] == chan->sig)
  597. return in_sig[x][1];
  598. }
  599. return -1; /* not found -- error */
  600. }
  601. enum spantypes dahdi_str2spantype(const char *name)
  602. {
  603. if (strcasecmp("FXS", name) == 0)
  604. return SPANTYPE_ANALOG_FXS;
  605. else if (strcasecmp("FXO", name) == 0)
  606. return SPANTYPE_ANALOG_FXO;
  607. else if (strcasecmp("ANALOG_MIXED", name) == 0)
  608. return SPANTYPE_ANALOG_MIXED;
  609. else if (strcasecmp("E1", name) == 0)
  610. return SPANTYPE_DIGITAL_E1;
  611. else if (strcasecmp("T1", name) == 0)
  612. return SPANTYPE_DIGITAL_T1;
  613. else if (strcasecmp("J1", name) == 0)
  614. return SPANTYPE_DIGITAL_J1;
  615. else if (strcasecmp("BRI_NT", name) == 0)
  616. return SPANTYPE_DIGITAL_BRI_NT;
  617. else if (strcasecmp("BRI_TE", name) == 0)
  618. return SPANTYPE_DIGITAL_BRI_TE;
  619. else if (strcasecmp("BRI_SOFT", name) == 0)
  620. return SPANTYPE_DIGITAL_BRI_SOFT;
  621. else if (strcasecmp("DYNAMIC", name) == 0)
  622. return SPANTYPE_DIGITAL_DYNAMIC;
  623. else
  624. return SPANTYPE_INVALID;
  625. }
  626. EXPORT_SYMBOL(dahdi_str2spantype);
  627. const char *dahdi_spantype2str(enum spantypes st)
  628. {
  629. switch (st) {
  630. case SPANTYPE_ANALOG_FXS: return "FXS";
  631. case SPANTYPE_ANALOG_FXO: return "FXO";
  632. case SPANTYPE_ANALOG_MIXED: return "ANALOG_MIXED";
  633. case SPANTYPE_DIGITAL_E1: return "E1";
  634. case SPANTYPE_DIGITAL_T1: return "T1";
  635. case SPANTYPE_DIGITAL_J1: return "J1";
  636. case SPANTYPE_DIGITAL_BRI_NT: return "BRI_NT";
  637. case SPANTYPE_DIGITAL_BRI_TE: return "BRI_TE";
  638. case SPANTYPE_DIGITAL_BRI_SOFT: return "BRI_SOFT";
  639. case SPANTYPE_DIGITAL_DYNAMIC: return "DYNAMIC";
  640. default:
  641. case SPANTYPE_INVALID: return "INVALID";
  642. };
  643. }
  644. EXPORT_SYMBOL(dahdi_spantype2str);
  645. const char *dahdi_lineconfig_bit_name(int lineconfig_bit)
  646. {
  647. static const char * const table[] = {
  648. /* These apply to T1 */
  649. [4] = "D4",
  650. [5] = "ESF",
  651. [6] = "AMI",
  652. [7] = "B8ZS",
  653. /* These apply to E1 */
  654. [8] = "CCS",
  655. [9] = "HDB3",
  656. [10] = "CRC4",
  657. /* These apply to BRI */
  658. [11] = "NTTE",
  659. [12] = "TERM",
  660. /* Finish */
  661. [16] = "NOTOPEN",
  662. };
  663. if (lineconfig_bit < 0 || lineconfig_bit >= ARRAY_SIZE(table))
  664. return NULL;
  665. return table[lineconfig_bit];
  666. }
  667. EXPORT_SYMBOL(dahdi_lineconfig_bit_name);
  668. ssize_t lineconfig_str(int lineconfig, char buf[], size_t size)
  669. {
  670. int framing_bit = 0;
  671. int coding_bit = 0;
  672. int crc4_bit = 0;
  673. int len = 0;
  674. int bit;
  675. bool written = false;
  676. for (bit = 4; bit <= 12; bit++) {
  677. int mask = (1 << bit);
  678. if (!(lineconfig & mask))
  679. continue;
  680. if (!framing_bit) {
  681. switch (mask) {
  682. case DAHDI_CONFIG_B8ZS:
  683. case DAHDI_CONFIG_AMI:
  684. case DAHDI_CONFIG_HDB3:
  685. framing_bit = bit;
  686. len += snprintf(buf + len, size, "%s%s",
  687. (written) ? "/" : "",
  688. dahdi_lineconfig_bit_name(bit));
  689. written = true;
  690. }
  691. }
  692. if (!coding_bit) {
  693. switch (mask) {
  694. case DAHDI_CONFIG_ESF:
  695. case DAHDI_CONFIG_D4:
  696. case DAHDI_CONFIG_CCS:
  697. coding_bit = bit;
  698. len += snprintf(buf + len, size, "%s%s",
  699. (written) ? "/" : "",
  700. dahdi_lineconfig_bit_name(bit));
  701. written = true;
  702. }
  703. }
  704. if (!crc4_bit && mask == DAHDI_CONFIG_CRC4) {
  705. crc4_bit = bit;
  706. len += snprintf(buf + len, size, "%s%s",
  707. (written) ? "/" : "",
  708. dahdi_lineconfig_bit_name(bit));
  709. written = true;
  710. }
  711. }
  712. return len;
  713. }
  714. EXPORT_SYMBOL(lineconfig_str);
  715. #ifdef CONFIG_PROC_FS
  716. const char *sigstr(int sig)
  717. {
  718. switch (sig) {
  719. case DAHDI_SIG_FXSLS:
  720. return "FXSLS";
  721. case DAHDI_SIG_FXSKS:
  722. return "FXSKS";
  723. case DAHDI_SIG_FXSGS:
  724. return "FXSGS";
  725. case DAHDI_SIG_FXOLS:
  726. return "FXOLS";
  727. case DAHDI_SIG_FXOKS:
  728. return "FXOKS";
  729. case DAHDI_SIG_FXOGS:
  730. return "FXOGS";
  731. case DAHDI_SIG_EM:
  732. return "E&M";
  733. case DAHDI_SIG_EM_E1:
  734. return "E&M-E1";
  735. case DAHDI_SIG_CLEAR:
  736. return "Clear";
  737. case DAHDI_SIG_HDLCRAW:
  738. return "HDLCRAW";
  739. case DAHDI_SIG_HDLCFCS:
  740. return "HDLCFCS";
  741. case DAHDI_SIG_HDLCNET:
  742. return "HDLCNET";
  743. case DAHDI_SIG_HARDHDLC:
  744. return "Hardware-assisted HDLC";
  745. case DAHDI_SIG_MTP2:
  746. return "MTP2";
  747. case DAHDI_SIG_SLAVE:
  748. return "Slave";
  749. case DAHDI_SIG_CAS:
  750. return "CAS";
  751. case DAHDI_SIG_DACS:
  752. return "DACS";
  753. case DAHDI_SIG_DACS_RBS:
  754. return "DACS+RBS";
  755. case DAHDI_SIG_SF:
  756. return "SF (ToneOnly)";
  757. case DAHDI_SIG_NONE:
  758. default:
  759. return "Unconfigured";
  760. }
  761. }
  762. int fill_alarm_string(char *buf, int count, int alarms)
  763. {
  764. int len;
  765. if (alarms <= 0)
  766. return 0;
  767. len = snprintf(buf, count, "%s%s%s%s%s%s",
  768. (alarms & DAHDI_ALARM_BLUE) ? "BLUE " : "",
  769. (alarms & DAHDI_ALARM_YELLOW) ? "YELLOW " : "",
  770. (alarms & DAHDI_ALARM_RED) ? "RED " : "",
  771. (alarms & DAHDI_ALARM_LOOPBACK) ? "LOOP " : "",
  772. (alarms & DAHDI_ALARM_RECOVER) ? "RECOVERING " : "",
  773. (alarms & DAHDI_ALARM_NOTOPEN) ? "NOTOPEN " : "");
  774. return len;
  775. }
  776. /*
  777. * Sequential proc interface
  778. */
  779. static void seq_fill_alarm_string(struct seq_file *sfile, int alarms)
  780. {
  781. char tmp[70];
  782. if (fill_alarm_string(tmp, sizeof(tmp), alarms))
  783. seq_printf(sfile, "%s", tmp);
  784. }
  785. static int dahdi_seq_show(struct seq_file *sfile, void *data)
  786. {
  787. long spanno = (long)sfile->private;
  788. int x;
  789. struct dahdi_span *s;
  790. s = span_find_and_get(spanno);
  791. if (!s)
  792. return -ENODEV;
  793. if (s->name)
  794. seq_printf(sfile, "Span %d: %s ", s->spanno, s->name);
  795. if (s->desc)
  796. seq_printf(sfile, "\"%s\"", s->desc);
  797. else
  798. seq_printf(sfile, "\"\"");
  799. if (dahdi_is_sync_master(s))
  800. seq_printf(sfile, " (MASTER)");
  801. if (s->lineconfig) {
  802. char tmpbuf[20];
  803. lineconfig_str(s->lineconfig, tmpbuf, sizeof(tmpbuf));
  804. seq_printf(sfile, " %s", tmpbuf);
  805. }
  806. seq_printf(sfile, " ");
  807. /* list alarms */
  808. seq_fill_alarm_string(sfile, s->alarms);
  809. if (s->syncsrc &&
  810. (s->syncsrc == s->spanno))
  811. seq_printf(sfile, "ClockSource ");
  812. seq_printf(sfile, "\n");
  813. if (s->count.bpv)
  814. seq_printf(sfile, "\tBPV count: %d\n", s->count.bpv);
  815. if (s->count.crc4)
  816. seq_printf(sfile, "\tCRC4 error count: %d\n", s->count.crc4);
  817. if (s->count.ebit)
  818. seq_printf(sfile, "\tE-bit error count: %d\n", s->count.ebit);
  819. if (s->count.fas)
  820. seq_printf(sfile, "\tFAS error count: %d\n", s->count.fas);
  821. if (s->parent->irqmisses)
  822. seq_printf(sfile, "\tIRQ misses: %d\n", s->parent->irqmisses);
  823. if (s->count.timingslips)
  824. seq_printf(sfile, "\tTiming slips: %d\n", s->count.timingslips);
  825. seq_printf(sfile, "\n");
  826. for (x = 0; x < s->channels; x++) {
  827. struct dahdi_chan *chan = s->chans[x];
  828. if (chan->name)
  829. seq_printf(sfile, "\t%4d %s ", chan->channo,
  830. chan->name);
  831. if (chan->sig) {
  832. if (chan->sig == DAHDI_SIG_SLAVE)
  833. seq_printf(sfile, "%s ",
  834. sigstr(chan->master->sig));
  835. else {
  836. seq_printf(sfile, "%s ", sigstr(chan->sig));
  837. if (chan->nextslave &&
  838. (chan->master->channo == chan->channo))
  839. seq_printf(sfile, "Master ");
  840. }
  841. } else if (!chan->sigcap) {
  842. seq_printf(sfile, "Reserved ");
  843. }
  844. if (test_bit(DAHDI_FLAGBIT_OPEN, &chan->flags))
  845. seq_printf(sfile, "(In use) ");
  846. #ifdef OPTIMIZE_CHANMUTE
  847. if (chan->chanmute)
  848. seq_printf(sfile, "(no pcm) ");
  849. #endif
  850. seq_fill_alarm_string(sfile, chan->chan_alarms);
  851. mutex_lock(&chan->mutex);
  852. if (chan->ec_factory) {
  853. seq_printf(sfile, "(EC: %s - %s) ",
  854. chan->ec_factory->get_name(chan),
  855. chan->ec_state ? "ACTIVE" : "INACTIVE");
  856. }
  857. mutex_unlock(&chan->mutex);
  858. seq_printf(sfile, "\n");
  859. }
  860. put_span(s);
  861. return 0;
  862. }
  863. static int dahdi_proc_open(struct inode *inode, struct file *file)
  864. {
  865. return single_open(file, dahdi_seq_show, PDE_DATA(inode));
  866. }
  867. static const struct file_operations dahdi_proc_ops = {
  868. .owner = THIS_MODULE,
  869. .open = dahdi_proc_open,
  870. .read = seq_read,
  871. .llseek = seq_lseek,
  872. .release = single_release,
  873. };
  874. #endif
  875. static int dahdi_first_empty_alias(void)
  876. {
  877. /* Find the first conference which has no alias pointing to it */
  878. int x;
  879. for (x=1;x<DAHDI_MAX_CONF;x++) {
  880. if (!confrev[x])
  881. return x;
  882. }
  883. return -1;
  884. }
  885. static void recalc_maxconfs(void)
  886. {
  887. int x;
  888. for (x = DAHDI_MAX_CONF - 1; x > 0; x--) {
  889. if (confrev[x]) {
  890. maxconfs = x + 1;
  891. return;
  892. }
  893. }
  894. maxconfs = 0;
  895. }
  896. static int dahdi_first_empty_conference(void)
  897. {
  898. /* Find the first conference which has no alias */
  899. int x;
  900. for (x = DAHDI_MAX_CONF - 1; x > 0; x--) {
  901. if (!confalias[x])
  902. return x;
  903. }
  904. return -1;
  905. }
  906. static int dahdi_get_conf_alias(int x)
  907. {
  908. int a;
  909. if (confalias[x])
  910. return confalias[x];
  911. /* Allocate an alias */
  912. a = dahdi_first_empty_alias();
  913. confalias[x] = a;
  914. confrev[a] = x;
  915. /* Highest conference may have changed */
  916. recalc_maxconfs();
  917. return a;
  918. }
  919. static unsigned long _chan_in_conf(struct dahdi_chan *chan, unsigned long x)
  920. {
  921. const int confmode = chan->confmode & DAHDI_CONF_MODE_MASK;
  922. return (chan && (chan->confna == x) &&
  923. (confmode == DAHDI_CONF_CONF ||
  924. confmode == DAHDI_CONF_CONFANN ||
  925. confmode == DAHDI_CONF_CONFMON ||
  926. confmode == DAHDI_CONF_CONFANNMON ||
  927. confmode == DAHDI_CONF_REALANDPSEUDO)) ? 1 : 0;
  928. }
  929. #ifdef CONFIG_DAHDI_CONFLINK
  930. static void recalc_maxlinks(void)
  931. {
  932. int x;
  933. for (x = DAHDI_MAX_CONF - 1; x > 0; x--) {
  934. if (conf_links[x].src || conf_links[x].dst) {
  935. maxlinks = x + 1;
  936. return;
  937. }
  938. }
  939. maxlinks = 0;
  940. }
  941. #endif
  942. static void dahdi_check_conf(int x)
  943. {
  944. unsigned long res;
  945. unsigned long flags;
  946. #ifdef CONFIG_DAHDI_CONFLINK
  947. int i;
  948. #endif
  949. /* return if no valid conf number */
  950. if (x <= 0)
  951. return;
  952. /* Return if there is no alias */
  953. if (!confalias[x])
  954. return;
  955. spin_lock_irqsave(&chan_lock, flags);
  956. res = __for_each_channel(_chan_in_conf, x);
  957. spin_unlock_irqrestore(&chan_lock, flags);
  958. if (res)
  959. return;
  960. /* If we get here, nobody is in the conference anymore. Clear it out
  961. both forward and reverse */
  962. confrev[confalias[x]] = 0;
  963. confalias[x] = 0;
  964. /* Highest conference may have changed */
  965. recalc_maxconfs();
  966. #ifdef CONFIG_DAHDI_CONFLINK
  967. /* And unlink it from any conflinks */
  968. for (i = DAHDI_MAX_CONF - 1; i > 0; i--) {
  969. if (conf_links[i].src == x)
  970. conf_links[i].src = 0;
  971. if (conf_links[i].dst == x)
  972. conf_links[i].dst = 0;
  973. }
  974. recalc_maxlinks();
  975. #endif
  976. }
  977. /* enqueue an event on a channel */
  978. static void __qevent(struct dahdi_chan *chan, int event)
  979. {
  980. /* if full, ignore */
  981. if ((chan->eventoutidx == 0) && (chan->eventinidx == (DAHDI_MAX_EVENTSIZE - 1)))
  982. return;
  983. /* if full, ignore */
  984. if (chan->eventinidx == (chan->eventoutidx - 1))
  985. return;
  986. /* save the event */
  987. chan->eventbuf[chan->eventinidx++] = event;
  988. /* wrap the index, if necessary */
  989. if (chan->eventinidx >= DAHDI_MAX_EVENTSIZE)
  990. chan->eventinidx = 0;
  991. /* wake em all up */
  992. wake_up_interruptible(&chan->waitq);
  993. return;
  994. }
  995. void dahdi_qevent_nolock(struct dahdi_chan *chan, int event)
  996. {
  997. __qevent(chan, event);
  998. }
  999. void dahdi_qevent_lock(struct dahdi_chan *chan, int event)
  1000. {
  1001. unsigned long flags;
  1002. spin_lock_irqsave(&chan->lock, flags);
  1003. __qevent(chan, event);
  1004. spin_unlock_irqrestore(&chan->lock, flags);
  1005. }
  1006. static inline void calc_fcs(struct dahdi_chan *ss, int inwritebuf)
  1007. {
  1008. int x;
  1009. unsigned int fcs = PPP_INITFCS;
  1010. unsigned char *data = ss->writebuf[inwritebuf];
  1011. int len = ss->writen[inwritebuf];
  1012. /* Not enough space to do FCS calculation */
  1013. if (len < 2)
  1014. return;
  1015. for (x = 0; x < len - 2; x++)
  1016. fcs = PPP_FCS(fcs, data[x]);
  1017. fcs ^= 0xffff;
  1018. /* Send out the FCS */
  1019. data[len - 2] = (fcs & 0xff);
  1020. data[len - 1] = (fcs >> 8) & 0xff;
  1021. }
  1022. static int dahdi_reallocbufs(struct dahdi_chan *ss, int blocksize, int numbufs)
  1023. {
  1024. unsigned char *newtxbuf = NULL;
  1025. unsigned char *newrxbuf = NULL;
  1026. unsigned char *oldtxbuf = NULL;
  1027. unsigned char *oldrxbuf = NULL;
  1028. unsigned long flags;
  1029. int x;
  1030. /* Check numbufs */
  1031. if (numbufs < 2)
  1032. numbufs = 2;
  1033. if (numbufs > DAHDI_MAX_NUM_BUFS)
  1034. numbufs = DAHDI_MAX_NUM_BUFS;
  1035. /* We need to allocate our buffers now */
  1036. if (blocksize) {
  1037. newtxbuf = kzalloc(blocksize * numbufs, GFP_KERNEL);
  1038. if (NULL == newtxbuf)
  1039. return -ENOMEM;
  1040. newrxbuf = kzalloc(blocksize * numbufs, GFP_KERNEL);
  1041. if (NULL == newrxbuf) {
  1042. kfree(newtxbuf);
  1043. return -ENOMEM;
  1044. }
  1045. }
  1046. /* Now that we've allocated our new buffers, we can safely
  1047. move things around... */
  1048. spin_lock_irqsave(&ss->lock, flags);
  1049. ss->blocksize = blocksize; /* set the blocksize */
  1050. oldrxbuf = ss->readbuf[0]; /* Keep track of the old buffer */
  1051. oldtxbuf = ss->writebuf[0];
  1052. ss->readbuf[0] = NULL;
  1053. if (newrxbuf) {
  1054. BUG_ON(NULL == newtxbuf);
  1055. for (x = 0; x < numbufs; x++) {
  1056. ss->readbuf[x] = newrxbuf + x * blocksize;
  1057. ss->writebuf[x] = newtxbuf + x * blocksize;
  1058. }
  1059. } else {
  1060. for (x = 0; x < numbufs; x++) {
  1061. ss->readbuf[x] = NULL;
  1062. ss->writebuf[x] = NULL;
  1063. }
  1064. }
  1065. /* Mark all buffers as empty */
  1066. for (x = 0; x < numbufs; x++) {
  1067. ss->writen[x] =
  1068. ss->writeidx[x]=
  1069. ss->readn[x]=
  1070. ss->readidx[x] = 0;
  1071. }
  1072. /* Keep track of where our data goes (if it goes
  1073. anywhere at all) */
  1074. if (newrxbuf) {
  1075. ss->inreadbuf = 0;
  1076. ss->inwritebuf = 0;
  1077. } else {
  1078. ss->inreadbuf = -1;
  1079. ss->inwritebuf = -1;
  1080. }
  1081. ss->outreadbuf = -1;
  1082. ss->outwritebuf = -1;
  1083. ss->numbufs = numbufs;
  1084. if ((ss->txbufpolicy == DAHDI_POLICY_WHEN_FULL) || (ss->txbufpolicy == DAHDI_POLICY_HALF_FULL))
  1085. ss->txdisable = 1;
  1086. else
  1087. ss->txdisable = 0;
  1088. spin_unlock_irqrestore(&ss->lock, flags);
  1089. kfree(oldtxbuf);
  1090. kfree(oldrxbuf);
  1091. return 0;
  1092. }
  1093. static int dahdi_hangup(struct dahdi_chan *chan);
  1094. static void dahdi_set_law(struct dahdi_chan *chan, int law);
  1095. /* Pull a DAHDI_CHUNKSIZE piece off the queue. Returns
  1096. 0 on success or -1 on failure. If failed, provides
  1097. silence */
  1098. static int __buf_pull(struct confq *q, u_char *data, struct dahdi_chan *c)
  1099. {
  1100. int oldoutbuf = q->outbuf;
  1101. /* Ain't nuffin to read */
  1102. if (q->outbuf < 0) {
  1103. if (data)
  1104. memset(data, DAHDI_LIN2X(0,c), DAHDI_CHUNKSIZE);
  1105. return -1;
  1106. }
  1107. if (data)
  1108. memcpy(data, q->buf[q->outbuf], DAHDI_CHUNKSIZE);
  1109. q->outbuf = (q->outbuf + 1) % DAHDI_CB_SIZE;
  1110. /* Won't be nuffin next time */
  1111. if (q->outbuf == q->inbuf) {
  1112. q->outbuf = -1;
  1113. }
  1114. /* If they thought there was no space then
  1115. there is now where we just read */
  1116. if (q->inbuf < 0)
  1117. q->inbuf = oldoutbuf;
  1118. return 0;
  1119. }
  1120. /* Returns a place to put stuff, or NULL if there is
  1121. no room */
  1122. static u_char *__buf_pushpeek(struct confq *q)
  1123. {
  1124. if (q->inbuf < 0)
  1125. return NULL;
  1126. return q->buf[q->inbuf];
  1127. }
  1128. static u_char *__buf_peek(struct confq *q)
  1129. {
  1130. if (q->outbuf < 0)
  1131. return NULL;
  1132. return q->buf[q->outbuf];
  1133. }
  1134. /* Push something onto the queue, or assume what
  1135. is there is valid if data is NULL */
  1136. static int __buf_push(struct confq *q, const u_char *data)
  1137. {
  1138. int oldinbuf = q->inbuf;
  1139. if (q->inbuf < 0) {
  1140. return -1;
  1141. }
  1142. if (data)
  1143. /* Copy in the data */
  1144. memcpy(q->buf[q->inbuf], data, DAHDI_CHUNKSIZE);
  1145. /* Advance the inbuf pointer */
  1146. q->inbuf = (q->inbuf + 1) % DAHDI_CB_SIZE;
  1147. if (q->inbuf == q->outbuf) {
  1148. /* No space anymore... */
  1149. q->inbuf = -1;
  1150. }
  1151. /* If they don't think data is ready, let
  1152. them know it is now */
  1153. if (q->outbuf < 0) {
  1154. q->outbuf = oldinbuf;
  1155. }
  1156. return 0;
  1157. }
  1158. static void reset_conf(struct dahdi_chan *chan)
  1159. {
  1160. int x;
  1161. /* Empty out buffers and reset to initialization */
  1162. for (x = 0; x < DAHDI_CB_SIZE; x++)
  1163. chan->confin.buf[x] = chan->confin.buffer + DAHDI_CHUNKSIZE * x;
  1164. chan->confin.inbuf = 0;
  1165. chan->confin.outbuf = -1;
  1166. for (x = 0; x < DAHDI_CB_SIZE; x++)
  1167. chan->confout.buf[x] = chan->confout.buffer + DAHDI_CHUNKSIZE * x;
  1168. chan->confout.inbuf = 0;
  1169. chan->confout.outbuf = -1;
  1170. }
  1171. static const struct dahdi_echocan_factory *find_echocan(const char *name)
  1172. {
  1173. struct ecfactory *cur;
  1174. char *name_upper;
  1175. char *c;
  1176. const char *d;
  1177. char modname_buf[128] = "dahdi_echocan_";
  1178. unsigned int tried_once = 0;
  1179. name_upper = kmalloc(strlen(name) + 1, GFP_KERNEL);
  1180. if (!name_upper)
  1181. return NULL;
  1182. for (c = name_upper, d = name; *d; c++, d++) {
  1183. *c = toupper(*d);
  1184. }
  1185. *c = '\0';
  1186. retry:
  1187. spin_lock(&ecfactory_list_lock);
  1188. list_for_each_entry(cur, &ecfactory_list, list) {
  1189. if (!strcmp(name_upper, cur->ec->get_name(NULL))) {
  1190. if (try_module_get(cur->ec->owner)) {
  1191. spin_unlock(&ecfactory_list_lock);
  1192. kfree(name_upper);
  1193. return cur->ec;
  1194. } else {
  1195. spin_unlock(&ecfactory_list_lock);
  1196. kfree(name_upper);
  1197. return NULL;
  1198. }
  1199. }
  1200. }
  1201. spin_unlock(&ecfactory_list_lock);
  1202. if (tried_once) {
  1203. kfree(name_upper);
  1204. return NULL;
  1205. }
  1206. /* couldn't find it, let's try to load it */
  1207. for (c = &modname_buf[strlen(modname_buf)], d = name; *d; c++, d++) {
  1208. *c = tolower(*d);
  1209. }
  1210. request_module("%s", modname_buf);
  1211. tried_once = 1;
  1212. /* and try one more time */
  1213. goto retry;
  1214. }
  1215. static void release_echocan(const struct dahdi_echocan_factory *ec)
  1216. {
  1217. if (ec)
  1218. module_put(ec->owner);
  1219. }
  1220. /**
  1221. * is_gain_allocated() - True if gain tables were dynamically allocated.
  1222. * @chan: The channel to check.
  1223. */
  1224. static inline bool is_gain_allocated(const struct dahdi_chan *chan)
  1225. {
  1226. return (chan->rxgain && (chan->rxgain != defgain));
  1227. }
  1228. static const char *hwec_def_name = "HWEC";
  1229. static const char *hwec_get_name(const struct dahdi_chan *chan)
  1230. {
  1231. if (chan && chan->span && chan->span->ops->echocan_name)
  1232. return chan->span->ops->echocan_name(chan);
  1233. else
  1234. return hwec_def_name;
  1235. }
  1236. static int hwec_echocan_create(struct dahdi_chan *chan,
  1237. struct dahdi_echocanparams *ecp, struct dahdi_echocanparam *p,
  1238. struct dahdi_echocan_state **ec)
  1239. {
  1240. if (chan->span && chan->span->ops->echocan_create)
  1241. return chan->span->ops->echocan_create(chan, ecp, p, ec);
  1242. else
  1243. return -ENODEV;
  1244. }
  1245. static const struct dahdi_echocan_factory hwec_factory = {
  1246. .get_name = hwec_get_name,
  1247. .owner = THIS_MODULE,
  1248. .echocan_create = hwec_echocan_create,
  1249. };
  1250. /**
  1251. * dahdi_enable_hw_preechocan - Let the board driver enable hwpreec if possible.
  1252. * @chan: The channel to monitor.
  1253. *
  1254. * Returns 0 on success, if there is a software echocanceler attached on
  1255. * the channel, or the span does not have an enable_hw_preechocan callback.
  1256. * Otherwise an error code.
  1257. *
  1258. */
  1259. static int dahdi_enable_hw_preechocan(struct dahdi_chan *chan)
  1260. {
  1261. int res;
  1262. mutex_lock(&chan->mutex);
  1263. if (chan->ec_factory != &hwec_factory)
  1264. res = -ENODEV;
  1265. else
  1266. res = 0;
  1267. mutex_unlock(&chan->mutex);
  1268. if (-ENODEV == res)
  1269. return 0;
  1270. if (chan->span->ops->enable_hw_preechocan)
  1271. return chan->span->ops->enable_hw_preechocan(chan);
  1272. else
  1273. return 0;
  1274. }
  1275. /**
  1276. * dahdi_disable_hw_preechocan - Disable any hardware pre echocan monitoring.
  1277. * @chan: The channel to stop monitoring.
  1278. *
  1279. * Give the board driver the option to free any resources needed to monitor
  1280. * the preechocan stream.
  1281. *
  1282. */
  1283. static void dahdi_disable_hw_preechocan(struct dahdi_chan *chan)
  1284. {
  1285. if (chan->span->ops->disable_hw_preechocan)
  1286. chan->span->ops->disable_hw_preechocan(chan);
  1287. }
  1288. /*
  1289. * close_channel - close the channel, resetting any channel variables
  1290. * @chan: the dahdi_chan to close
  1291. *
  1292. * This function is called before either the parent span is linked into the
  1293. * span list, or for pseudos, place on the psuedo_list. Therefore, this
  1294. * function nor it's callers should depend on the channel being findable
  1295. * via those methods.
  1296. */
  1297. static void close_channel(struct dahdi_chan *chan)
  1298. {
  1299. unsigned long flags;
  1300. const void *rxgain = NULL;
  1301. struct dahdi_echocan_state *ec_state;
  1302. const struct dahdi_echocan_factory *ec_current;
  1303. int oldconf;
  1304. short *readchunkpreec;
  1305. #ifdef CONFIG_DAHDI_PPP
  1306. struct ppp_channel *ppp;
  1307. #endif
  1308. might_sleep();
  1309. if (chan->conf_chan &&
  1310. ((DAHDI_CONF_MONITOR_RX_PREECHO == chan->confmode) ||
  1311. (DAHDI_CONF_MONITOR_TX_PREECHO == chan->confmode) ||
  1312. (DAHDI_CONF_MONITORBOTH_PREECHO == chan->confmode))) {
  1313. void *readchunkpreec;
  1314. spin_lock_irqsave(&chan->conf_chan->lock, flags);
  1315. readchunkpreec = chan->conf_chan->readchunkpreec;
  1316. chan->conf_chan->readchunkpreec = NULL;
  1317. spin_unlock_irqrestore(&chan->conf_chan->lock, flags);
  1318. if (readchunkpreec) {
  1319. dahdi_disable_hw_preechocan(chan->conf_chan);
  1320. kfree(readchunkpreec);
  1321. }
  1322. }
  1323. /* XXX Buffers should be send out before reallocation!!! XXX */
  1324. if (!(chan->flags & DAHDI_FLAG_NOSTDTXRX))
  1325. dahdi_reallocbufs(chan, 0, 0);
  1326. spin_lock_irqsave(&chan->lock, flags);
  1327. #ifdef CONFIG_DAHDI_PPP
  1328. ppp = chan->ppp;
  1329. chan->ppp = NULL;
  1330. #endif
  1331. ec_state = chan->ec_state;
  1332. chan->ec_state = NULL;
  1333. ec_current = chan->ec_current;
  1334. chan->ec_current = NULL;
  1335. readchunkpreec = chan->readchunkpreec;
  1336. chan->readchunkpreec = NULL;
  1337. chan->curtone = NULL;
  1338. if (chan->curzone) {
  1339. struct dahdi_zone *zone = chan->curzone;
  1340. chan->curzone = NULL;
  1341. tone_zone_put(zone);
  1342. }
  1343. chan->cadencepos = 0;
  1344. chan->pdialcount = 0;
  1345. dahdi_hangup(chan);
  1346. chan->itimerset = chan->itimer = 0;
  1347. chan->pulsecount = 0;
  1348. chan->pulsetimer = 0;
  1349. chan->ringdebtimer = 0;
  1350. chan->txdialbuf[0] = '\0';
  1351. chan->digitmode = DIGIT_MODE_DTMF;
  1352. chan->dialing = 0;
  1353. chan->afterdialingtimer = 0;
  1354. /* initialize IO MUX mask */
  1355. chan->iomask = 0;
  1356. /* save old conf number, if any */
  1357. oldconf = chan->confna;
  1358. /* initialize conference variables */
  1359. chan->_confn = 0;
  1360. chan->confna = 0;
  1361. chan->confmode = 0;
  1362. if ((chan->sig & __DAHDI_SIG_DACS) != __DAHDI_SIG_DACS)
  1363. chan->dacs_chan = NULL;
  1364. chan->confmute = 0;
  1365. chan->gotgs = 0;
  1366. reset_conf(chan);
  1367. chan->dacs_chan = NULL;
  1368. if (is_gain_allocated(chan))
  1369. rxgain = chan->rxgain;
  1370. chan->rxgain = defgain;
  1371. chan->txgain = defgain;
  1372. chan->eventinidx = chan->eventoutidx = 0;
  1373. chan->flags &= ~(DAHDI_FLAG_LOOPED | DAHDI_FLAG_LINEAR | DAHDI_FLAG_PPP | DAHDI_FLAG_SIGFREEZE);
  1374. dahdi_set_law(chan, DAHDI_LAW_DEFAULT);
  1375. memset(chan->conflast, 0, sizeof(chan->conflast));
  1376. memset(chan->conflast1, 0, sizeof(chan->conflast1));
  1377. memset(chan->conflast2, 0, sizeof(chan->conflast2));
  1378. if (chan->span && oldconf)
  1379. dahdi_disable_dacs(chan);
  1380. spin_unlock_irqrestore(&chan->lock, flags);
  1381. if (ec_state) {
  1382. ec_state->ops->echocan_free(chan, ec_state);
  1383. release_echocan(ec_current);
  1384. }
  1385. /* release conference resource, if any to release */
  1386. if (oldconf)
  1387. dahdi_check_conf(oldconf);
  1388. if (rxgain)
  1389. kfree(rxgain);
  1390. if (readchunkpreec) {
  1391. dahdi_disable_hw_preechocan(chan);
  1392. kfree(readchunkpreec);
  1393. }
  1394. #ifdef CONFIG_DAHDI_PPP
  1395. if (ppp) {
  1396. tasklet_kill(&chan->ppp_calls);
  1397. skb_queue_purge(&chan->ppp_rq);
  1398. ppp_unregister_channel(ppp);
  1399. kfree(ppp);
  1400. }
  1401. #endif
  1402. }
  1403. static int dahdi_ioctl_freezone(unsigned long data)
  1404. {
  1405. struct dahdi_zone *z;
  1406. struct dahdi_zone *found = NULL;
  1407. int num;
  1408. if (get_user(num, (int __user *) data))
  1409. return -EFAULT;
  1410. spin_lock(&zone_lock);
  1411. list_for_each_entry(z, &tone_zones, node) {
  1412. if (z->num == num) {
  1413. found = z;
  1414. break;
  1415. }
  1416. }
  1417. if (found) {
  1418. list_del(&found->node);
  1419. }
  1420. spin_unlock(&zone_lock);
  1421. if (found) {
  1422. if (debug) {
  1423. module_printk(KERN_INFO,
  1424. "Unregistering tone zone %d (%s)\n",
  1425. found->num, found->name);
  1426. }
  1427. tone_zone_put(found);
  1428. }
  1429. return 0;
  1430. }
  1431. static int dahdi_register_tone_zone(struct dahdi_zone *zone)
  1432. {
  1433. struct dahdi_zone *cur;
  1434. int res = 0;
  1435. kref_init(&zone->refcount);
  1436. spin_lock(&zone_lock);
  1437. list_for_each_entry(cur, &tone_zones, node) {
  1438. if (cur->num == zone->num) {
  1439. res = -EINVAL;
  1440. break;
  1441. }
  1442. }
  1443. if (!res) {
  1444. list_add_tail(&zone->node, &tone_zones);
  1445. if (debug) {
  1446. module_printk(KERN_INFO,
  1447. "Registered tone zone %d (%s)\n",
  1448. zone->num, zone->name);
  1449. }
  1450. }
  1451. spin_unlock(&zone_lock);
  1452. return res;
  1453. }
  1454. static int start_tone_digit(struct dahdi_chan *chan, int tone)
  1455. {
  1456. struct dahdi_tone *playtone = NULL;
  1457. int base, max;
  1458. if (!chan->curzone)
  1459. return -ENODATA;
  1460. switch (chan->digitmode) {
  1461. case DIGIT_MODE_DTMF:
  1462. /* Set dialing so that a dial operation doesn't interrupt this tone */
  1463. chan->dialing = 1;
  1464. base = DAHDI_TONE_DTMF_BASE;
  1465. max = DAHDI_TONE_DTMF_MAX;
  1466. break;
  1467. case DIGIT_MODE_MFR2_FWD:
  1468. base = DAHDI_TONE_MFR2_FWD_BASE;
  1469. max = DAHDI_TONE_MFR2_FWD_MAX;
  1470. break;
  1471. case DIGIT_MODE_MFR2_REV:
  1472. base = DAHDI_TONE_MFR2_REV_BASE;
  1473. max = DAHDI_TONE_MFR2_REV_MAX;
  1474. break;
  1475. default:
  1476. return -EINVAL;
  1477. }
  1478. if ((tone < base) || (tone > max))
  1479. return -EINVAL;
  1480. switch (chan->digitmode) {
  1481. case DIGIT_MODE_DTMF:
  1482. playtone = &chan->curzone->dtmf_continuous[tone - base];
  1483. break;
  1484. case DIGIT_MODE_MFR2_FWD:
  1485. playtone = &chan->curzone->mfr2_fwd_continuous[tone - base];
  1486. break;
  1487. case DIGIT_MODE_MFR2_REV:
  1488. playtone = &chan->curzone->mfr2_rev_continuous[tone - base];
  1489. break;
  1490. }
  1491. if (!playtone || !playtone->tonesamples)
  1492. return -ENOSYS;
  1493. chan->curtone = playtone;
  1494. return 0;
  1495. }
  1496. static int start_tone(struct dahdi_chan *chan, int tone)
  1497. {
  1498. int res = -EINVAL;
  1499. /* Stop the current tone, no matter what */
  1500. chan->tonep = 0;
  1501. chan->curtone = NULL;
  1502. chan->pdialcount = 0;
  1503. chan->txdialbuf[0] = '\0';
  1504. chan->dialing = 0;
  1505. if (tone == -1) {
  1506. /* Just stop the current tone */
  1507. res = 0;
  1508. } else if (!chan->curzone) {
  1509. static int __warnonce = 1;
  1510. if (__warnonce) {
  1511. __warnonce = 0;
  1512. /* The tonezones are loaded by dahdi_cfg based on /etc/dahdi/system.conf. */
  1513. module_printk(KERN_WARNING, "DAHDI: Cannot start tones until tone zone is loaded.\n");
  1514. }
  1515. /* Note that no tone zone exists at the moment */
  1516. res = -ENODATA;
  1517. } else if ((tone >= 0 && tone <= DAHDI_TONE_MAX)) {
  1518. /* Have a tone zone */
  1519. if (chan->curzone->tones[tone]) {
  1520. chan->curtone = chan->curzone->tones[tone];
  1521. res = 0;
  1522. } else { /* Indicate that zone is loaded but no such tone exists */
  1523. res = -ENOSYS;
  1524. }
  1525. } else if (chan->digitmode == DIGIT_MODE_DTMF ||
  1526. chan->digitmode == DIGIT_MODE_MFR2_FWD ||
  1527. chan->digitmode == DIGIT_MODE_MFR2_REV) {
  1528. res = start_tone_digit(chan, tone);
  1529. } else {
  1530. chan->dialing = 0;
  1531. res = -EINVAL;
  1532. }
  1533. if (chan->curtone)
  1534. dahdi_init_tone_state(&chan->ts, chan->curtone);
  1535. return res;
  1536. }
  1537. /**
  1538. * stop_tone - Stops any tones on a channel.
  1539. *
  1540. * Must be called with chan->lock held.
  1541. *
  1542. */
  1543. static inline int stop_tone(struct dahdi_chan *chan)
  1544. {
  1545. return start_tone(chan, -1);
  1546. }
  1547. static int set_tone_zone(struct dahdi_chan *chan, int zone)
  1548. {
  1549. int res = 0;
  1550. struct dahdi_zone *cur;
  1551. struct dahdi_zone *z;
  1552. unsigned long flags;
  1553. z = NULL;
  1554. spin_lock(&zone_lock);
  1555. if ((DEFAULT_TONE_ZONE == zone) && !list_empty(&tone_zones)) {
  1556. z = list_entry(tone_zones.next, struct dahdi_zone, node);
  1557. tone_zone_get(z);
  1558. } else {
  1559. list_for_each_entry(cur, &tone_zones, node) {
  1560. if (cur->num != (u8)zone)
  1561. continue;
  1562. z = cur;
  1563. tone_zone_get(z);
  1564. break;
  1565. }
  1566. }
  1567. spin_unlock(&zone_lock);
  1568. if (unlikely(!z))
  1569. return -ENODATA;
  1570. spin_lock_irqsave(&chan->lock, flags);
  1571. stop_tone(chan);
  1572. if (chan->curzone) {
  1573. struct dahdi_zone *zone = chan->curzone;
  1574. chan->curzone = NULL;
  1575. tone_zone_put(zone);
  1576. }
  1577. chan->curzone = z;
  1578. memcpy(chan->ringcadence, z->ringcadence, sizeof(chan->ringcadence));
  1579. spin_unlock_irqrestore(&chan->lock, flags);
  1580. return res;
  1581. }
  1582. static void dahdi_set_law(struct dahdi_chan *chan, int law)
  1583. {
  1584. if (DAHDI_LAW_DEFAULT == law) {
  1585. if (chan->deflaw)
  1586. law = chan->deflaw;
  1587. else
  1588. if (chan->span) law = chan->span->deflaw;
  1589. else law = DAHDI_LAW_MULAW;
  1590. }
  1591. if (law == DAHDI_LAW_ALAW) {
  1592. chan->xlaw = __dahdi_alaw;
  1593. #ifdef CONFIG_CALC_XLAW
  1594. chan->lineartoxlaw = __dahdi_lineartoalaw;
  1595. #else
  1596. chan->lin2x = __dahdi_lin2a;
  1597. #endif
  1598. } else {
  1599. chan->xlaw = __dahdi_mulaw;
  1600. #ifdef CONFIG_CALC_XLAW
  1601. chan->lineartoxlaw = __dahdi_lineartoulaw;
  1602. #else
  1603. chan->lin2x = __dahdi_lin2mu;
  1604. #endif
  1605. }
  1606. }
  1607. /**
  1608. * __dahdi_init_chan - Initialize the channel data structures.
  1609. * @chan: The channel to initialize
  1610. *
  1611. */
  1612. static void __dahdi_init_chan(struct dahdi_chan *chan)
  1613. {
  1614. might_sleep();
  1615. spin_lock_init(&chan->lock);
  1616. mutex_init(&chan->mutex);
  1617. init_waitqueue_head(&chan->waitq);
  1618. if (!chan->master)
  1619. chan->master = chan;
  1620. if (!chan->readchunk)
  1621. chan->readchunk = chan->sreadchunk;
  1622. if (!chan->writechunk)
  1623. chan->writechunk = chan->swritechunk;
  1624. chan->rxgain = NULL;
  1625. chan->txgain = NULL;
  1626. close_channel(chan);
  1627. }
  1628. /**
  1629. * dahdi_chan_reg - Mark the channel registered.
  1630. *
  1631. * This must be called after close channel during registration, normally
  1632. * covered by the call to __dahdi_init_chan, to avoid "HDLC hangage"
  1633. */
  1634. static inline void dahdi_chan_reg(struct dahdi_chan *chan)
  1635. {
  1636. set_bit(DAHDI_FLAGBIT_REGISTERED, &chan->flags);
  1637. }
  1638. /**
  1639. * dahdi_lboname() - Convert line build out number to string.
  1640. *
  1641. */
  1642. const char *dahdi_lboname(int lbo)
  1643. {
  1644. /* names of tx level settings */
  1645. static const char *const dahdi_txlevelnames[] = {
  1646. "0 db (CSU)/0-133 feet (DSX-1)",
  1647. "133-266 feet (DSX-1)",
  1648. "266-399 feet (DSX-1)",
  1649. "399-533 feet (DSX-1)",
  1650. "533-655 feet (DSX-1)",
  1651. "-7.5db (CSU)",
  1652. "-15db (CSU)",
  1653. "-22.5db (CSU)"
  1654. };
  1655. if ((lbo < 0) || (lbo > 7))
  1656. return "Unknown";
  1657. return dahdi_txlevelnames[lbo];
  1658. }
  1659. EXPORT_SYMBOL(dahdi_lboname);
  1660. #if defined(CONFIG_DAHDI_NET) || defined(CONFIG_DAHDI_PPP)
  1661. static inline void print_debug_writebuf(struct dahdi_chan* ss, struct sk_buff *skb, int oldbuf)
  1662. {
  1663. #ifdef CONFIG_DAHDI_DEBUG
  1664. int x;
  1665. module_printk(KERN_NOTICE, "Buffered %d bytes to go out in buffer %d\n", ss->writen[oldbuf], oldbuf);
  1666. module_printk(KERN_DEBUG "");
  1667. for (x=0;x<ss->writen[oldbuf];x++)
  1668. printk("%02x ", ss->writebuf[oldbuf][x]);
  1669. printk("\n");
  1670. #endif
  1671. }
  1672. #endif
  1673. #ifdef CONFIG_DAHDI_NET
  1674. #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 26)
  1675. static inline struct net_device_stats *hdlc_stats(struct net_device *dev)
  1676. {
  1677. return &dev->stats;
  1678. }
  1679. #endif
  1680. static int dahdi_net_open(struct net_device *dev)
  1681. {
  1682. int res = hdlc_open(dev);
  1683. struct dahdi_chan *ms = netdev_to_chan(dev);
  1684. /* if (!dev->hard_start_xmit) return res; is this really necessary? --byg */
  1685. if (res) /* this is necessary to avoid kernel panic when UNSPEC link encap, proven --byg */
  1686. return res;
  1687. if (!ms) {
  1688. module_printk(KERN_NOTICE, "dahdi_net_open: nothing??\n");
  1689. return -EINVAL;
  1690. }
  1691. if (test_bit(DAHDI_FLAGBIT_OPEN, &ms->flags)) {
  1692. module_printk(KERN_NOTICE, "%s is already open!\n", ms->name);
  1693. return -EBUSY;
  1694. }
  1695. if (!dahdi_have_netdev(ms)) {
  1696. module_printk(KERN_NOTICE, "%s is not a net device!\n", ms->name);
  1697. return -EINVAL;
  1698. }
  1699. ms->txbufpolicy = DAHDI_POLICY_IMMEDIATE;
  1700. res = dahdi_reallocbufs(ms, DAHDI_DEFAULT_MTU_MRU, DAHDI_DEFAULT_NUM_BUFS);
  1701. if (res)
  1702. return res;
  1703. fasthdlc_init(&ms->rxhdlc, (ms->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
  1704. fasthdlc_init(&ms->txhdlc, (ms->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
  1705. ms->infcs = PPP_INITFCS;
  1706. netif_start_queue(chan_to_netdev(ms));
  1707. #ifdef CONFIG_DAHDI_DEBUG
  1708. module_printk(KERN_NOTICE, "DAHDINET: Opened channel %d name %s\n", ms->channo, ms->name);
  1709. #endif
  1710. return 0;
  1711. }
  1712. static int dahdi_register_hdlc_device(struct net_device *dev, const char *dev_name)
  1713. {
  1714. int result;
  1715. if (dev_name && *dev_name) {
  1716. if ((result = dev_alloc_name(dev, dev_name)) < 0)
  1717. return result;
  1718. }
  1719. result = register_netdev(dev);
  1720. if (result != 0)
  1721. return -EIO;
  1722. return 0;
  1723. }
  1724. static int dahdi_net_stop(struct net_device *dev)
  1725. {
  1726. hdlc_device *h = dev_to_hdlc(dev);
  1727. struct dahdi_hdlc *hdlc = h->priv;
  1728. struct dahdi_chan *ms = hdlc_to_chan(hdlc);
  1729. if (!ms) {
  1730. module_printk(KERN_NOTICE, "dahdi_net_stop: nothing??\n");
  1731. return 0;
  1732. }
  1733. if (!dahdi_have_netdev(ms)) {
  1734. module_printk(KERN_NOTICE, "dahdi_net_stop: %s is not a net device!\n", ms->name);
  1735. return 0;
  1736. }
  1737. /* Not much to do here. Just deallocate the buffers */
  1738. netif_stop_queue(chan_to_netdev(ms));
  1739. dahdi_reallocbufs(ms, 0, 0);
  1740. hdlc_close(dev);
  1741. return 0;
  1742. }
  1743. /* kernel 2.4.20+ has introduced attach function, dunno what to do,
  1744. just copy sources from dscc4 to be sure and ready for further mastering,
  1745. NOOP right now (i.e. really a stub) --byg */
  1746. static int dahdi_net_attach(struct net_device *dev, unsigned short encoding,
  1747. unsigned short parity)
  1748. {
  1749. /* struct net_device *dev = hdlc_to_dev(hdlc);
  1750. struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
  1751. if (encoding != ENCODING_NRZ &&
  1752. encoding != ENCODING_NRZI &&
  1753. encoding != ENCODING_FM_MARK &&
  1754. encoding != ENCODING_FM_SPACE &&
  1755. encoding != ENCODING_MANCHESTER)
  1756. return -EINVAL;
  1757. if (parity != PARITY_NONE &&
  1758. parity != PARITY_CRC16_PR0_CCITT &&
  1759. parity != PARITY_CRC16_PR1_CCITT &&
  1760. parity != PARITY_CRC32_PR0_CCITT &&
  1761. parity != PARITY_CRC32_PR1_CCITT)
  1762. return -EINVAL;
  1763. dpriv->encoding = encoding;
  1764. dpriv->parity = parity;*/
  1765. return 0;
  1766. }
  1767. static struct dahdi_hdlc *dahdi_hdlc_alloc(void)
  1768. {
  1769. return kzalloc(sizeof(struct dahdi_hdlc), GFP_KERNEL);
  1770. }
  1771. static int dahdi_xmit(struct sk_buff *skb, struct net_device *dev)
  1772. {
  1773. /* FIXME: this construction seems to be not very optimal for me but I
  1774. * could find nothing better at the moment (Friday, 10PM :( ) --byg
  1775. * */
  1776. struct dahdi_chan *ss = netdev_to_chan(dev);
  1777. struct net_device_stats *stats = hdlc_stats(dev);
  1778. int retval = 1;
  1779. int x,oldbuf;
  1780. unsigned int fcs;
  1781. unsigned char *data;
  1782. unsigned long flags;
  1783. /* See if we have any buffers */
  1784. spin_lock_irqsave(&ss->lock, flags);
  1785. if (skb->len > ss->blocksize - 2) {
  1786. module_printk(KERN_ERR, "dahdi_xmit(%s): skb is too large (%d > %d)\n", dev->name, skb->len, ss->blocksize -2);
  1787. stats->tx_dropped++;
  1788. retval = 0;
  1789. } else if (ss->inwritebuf >= 0) {
  1790. /* We have a place to put this packet */
  1791. /* XXX We should keep the SKB and avoid the memcpy XXX */
  1792. data = ss->writebuf[ss->inwritebuf];
  1793. memcpy(data, skb->data, skb->len);
  1794. ss->writen[ss->inwritebuf] = skb->len;
  1795. ss->writeidx[ss->inwritebuf] = 0;
  1796. /* Calculate the FCS */
  1797. fcs = PPP_INITFCS;
  1798. for (x=0;x<skb->len;x++)
  1799. fcs = PPP_FCS(fcs, data[x]);
  1800. /* Invert it */
  1801. fcs ^= 0xffff;
  1802. /* Send it out LSB first */
  1803. data[ss->writen[ss->inwritebuf]++] = (fcs & 0xff);
  1804. data[ss->writen[ss->inwritebuf]++] = (fcs >> 8) & 0xff;
  1805. /* Advance to next window */
  1806. oldbuf = ss->inwritebuf;
  1807. ss->inwritebuf = (ss->inwritebuf + 1) % ss->numbufs;
  1808. if (ss->inwritebuf == ss->outwritebuf) {
  1809. /* Whoops, no more space. */
  1810. ss->inwritebuf = -1;
  1811. netif_stop_queue(chan_to_netdev(ss));
  1812. }
  1813. if (ss->outwritebuf < 0) {
  1814. /* Let the interrupt handler know there's
  1815. some space for us */
  1816. ss->outwritebuf = oldbuf;
  1817. }
  1818. dev->trans_start = jiffies;
  1819. stats->tx_packets++;
  1820. stats->tx_bytes += ss->writen[oldbuf];
  1821. print_debug_writebuf(ss, skb, oldbuf);
  1822. retval = 0;
  1823. /* Free the SKB */
  1824. dev_kfree_skb_any(skb);
  1825. }
  1826. spin_unlock_irqrestore(&ss->lock, flags);
  1827. return retval;
  1828. }
  1829. static int dahdi_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1830. {
  1831. return hdlc_ioctl(dev, ifr, cmd);
  1832. }
  1833. #endif
  1834. #ifdef CONFIG_DAHDI_PPP
  1835. static int dahdi_ppp_xmit(struct ppp_channel *ppp, struct sk_buff *skb)
  1836. {
  1837. /*
  1838. * If we can't handle the packet right now, return 0. If we
  1839. * we handle or drop it, return 1. Always free if we return
  1840. * 1 and never if we return 0
  1841. */
  1842. struct dahdi_chan *ss = ppp->private;
  1843. int x,oldbuf;
  1844. unsigned int fcs;
  1845. unsigned char *data;
  1846. unsigned long flags;
  1847. int retval = 0;
  1848. /* See if we have any buffers */
  1849. spin_lock_irqsave(&ss->lock, flags);
  1850. if (!(test_bit(DAHDI_FLAGBIT_OPEN, &ss->flags))) {
  1851. module_printk(KERN_ERR, "Can't transmit on closed channel\n");
  1852. retval = 1;
  1853. } else if (skb->len > ss->blocksize - 4) {
  1854. module_printk(KERN_ERR, "dahdi_ppp_xmit(%s): skb is too large (%d > %d)\n", ss->name, skb->len, ss->blocksize -2);
  1855. retval = 1;
  1856. } else if (ss->inwritebuf >= 0) {
  1857. /* We have a place to put this packet */
  1858. /* XXX We should keep the SKB and avoid the memcpy XXX */
  1859. data = ss->writebuf[ss->inwritebuf];
  1860. /* Start with header of two bytes */
  1861. /* Add "ALL STATIONS" and "UNNUMBERED" */
  1862. data[0] = 0xff;
  1863. data[1] = 0x03;
  1864. ss->writen[ss->inwritebuf] = 2;
  1865. /* Copy real data and increment amount written */
  1866. memcpy(data + 2, skb->data, skb->len);
  1867. ss->writen[ss->inwritebuf] += skb->len;
  1868. /* Re-set index back to zero */
  1869. ss->writeidx[ss->inwritebuf] = 0;
  1870. /* Calculate the FCS */
  1871. fcs = PPP_INITFCS;
  1872. for (x=0;x<skb->len + 2;x++)
  1873. fcs = PPP_FCS(fcs, data[x]);
  1874. /* Invert it */
  1875. fcs ^= 0xffff;
  1876. /* Point past the real data now */
  1877. data += (skb->len + 2);
  1878. /* Send FCS out LSB first */
  1879. data[0] = (fcs & 0xff);
  1880. data[1] = (fcs >> 8) & 0xff;
  1881. /* Account for FCS length */
  1882. ss->writen[ss->inwritebuf]+=2;
  1883. /* Advance to next window */
  1884. oldbuf = ss->inwritebuf;
  1885. ss->inwritebuf = (ss->inwritebuf + 1) % ss->numbufs;
  1886. if (ss->inwritebuf == ss->outwritebuf) {
  1887. /* Whoops, no more space. */
  1888. ss->inwritebuf = -1;
  1889. }
  1890. if (ss->outwritebuf < 0) {
  1891. /* Let the interrupt handler know there's
  1892. some space for us */
  1893. ss->outwritebuf = oldbuf;
  1894. }
  1895. print_debug_writebuf(ss, skb, oldbuf);
  1896. retval = 1;
  1897. }
  1898. spin_unlock_irqrestore(&ss->lock, flags);
  1899. if (retval) {
  1900. /* Get rid of the SKB if we're returning non-zero */
  1901. /* N.B. this is called in process or BH context so
  1902. dev_kfree_skb is OK. */
  1903. dev_kfree_skb(skb);
  1904. }
  1905. return retval;
  1906. }
  1907. static int dahdi_ppp_ioctl(struct ppp_channel *ppp, unsigned int cmd, unsigned long flags)
  1908. {
  1909. return -EIO;
  1910. }
  1911. static struct ppp_channel_ops ztppp_ops =
  1912. {
  1913. .start_xmit = dahdi_ppp_xmit,
  1914. .ioctl = dahdi_ppp_ioctl,
  1915. };
  1916. #endif
  1917. /**
  1918. * is_monitor_mode() - True if the confmode indicates that one channel is monitoring another.
  1919. *
  1920. */
  1921. static bool is_monitor_mode(int confmode)
  1922. {
  1923. confmode &= DAHDI_CONF_MODE_MASK;
  1924. if ((confmode == DAHDI_CONF_MONITOR) ||
  1925. (confmode == DAHDI_CONF_MONITORTX) ||
  1926. (confmode == DAHDI_CONF_MONITORBOTH) ||
  1927. (confmode == DAHDI_CONF_MONITOR_RX_PREECHO) ||
  1928. (confmode == DAHDI_CONF_MONITOR_TX_PREECHO) ||
  1929. (confmode == DAHDI_CONF_MONITORBOTH_PREECHO)) {
  1930. return true;
  1931. } else {
  1932. return false;
  1933. }
  1934. }
  1935. static unsigned long _chan_cleanup(struct dahdi_chan *pos, unsigned long data)
  1936. {
  1937. unsigned long flags;
  1938. struct dahdi_chan *const chan = (struct dahdi_chan *)data;
  1939. /* Remove anyone pointing to us as master
  1940. and make them their own thing */
  1941. if (pos->master == chan)
  1942. pos->master = pos;
  1943. if (((pos->confna == chan->channo) &&
  1944. is_monitor_mode(pos->confmode)) ||
  1945. (pos->dacs_chan == chan) ||
  1946. (pos->conf_chan == chan)) {
  1947. /* Take them out of conference with us */
  1948. /* release conference resource if any */
  1949. if (pos->confna)
  1950. dahdi_check_conf(pos->confna);
  1951. dahdi_disable_dacs(pos);
  1952. spin_lock_irqsave(&pos->lock, flags);
  1953. pos->confna = 0;
  1954. pos->_confn = 0;
  1955. pos->confmode = 0;
  1956. pos->conf_chan = NULL;
  1957. pos->dacs_chan = NULL;
  1958. spin_unlock_irqrestore(&pos->lock, flags);
  1959. }
  1960. return 0;
  1961. }
  1962. static const struct file_operations nodev_fops;
  1963. static void dahdi_chan_unreg(struct dahdi_chan *chan)
  1964. {
  1965. unsigned long flags;
  1966. might_sleep();
  1967. /* In the case of surprise removal of hardware, make sure any open
  1968. * file handles to this channel are disassociated with the actual
  1969. * dahdi_chan. */
  1970. if (chan->file) {
  1971. module_printk(KERN_NOTICE,
  1972. "%s: surprise removal: chan %d\n",
  1973. __func__, chan->channo);
  1974. chan->file->private_data = NULL;
  1975. chan->file->f_op = &nodev_fops;
  1976. /*
  1977. * From now on, any file_operations for this device
  1978. * would call the nodev_fops methods.
  1979. */
  1980. }
  1981. mutex_lock(&chan->mutex);
  1982. release_echocan(chan->ec_factory);
  1983. chan->ec_factory = NULL;
  1984. mutex_unlock(&chan->mutex);
  1985. #ifdef CONFIG_DAHDI_NET
  1986. if (dahdi_have_netdev(chan)) {
  1987. unregister_hdlc_device(chan->hdlcnetdev->netdev);
  1988. free_netdev(chan->hdlcnetdev->netdev);
  1989. kfree(chan->hdlcnetdev);
  1990. chan->hdlcnetdev = NULL;
  1991. }
  1992. #endif
  1993. clear_bit(DAHDI_FLAGBIT_REGISTERED, &chan->flags);
  1994. #ifdef CONFIG_DAHDI_PPP
  1995. if (chan->ppp) {
  1996. module_printk(KERN_NOTICE, "HUH??? PPP still attached??\n");
  1997. }
  1998. #endif
  1999. spin_lock_irqsave(&chan_lock, flags);
  2000. __for_each_channel(_chan_cleanup, (unsigned long)chan);
  2001. spin_unlock_irqrestore(&chan_lock, flags);
  2002. chan->channo = -1;
  2003. /* Let processeses out of their poll_wait() */
  2004. wake_up_interruptible(&chan->waitq);
  2005. /* release tone_zone */
  2006. close_channel(chan);
  2007. if (chan->file) {
  2008. if (test_bit(DAHDI_FLAGBIT_OPEN, &chan->flags)) {
  2009. clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
  2010. if (chan->span) {
  2011. if (chan->span->ops->close) {
  2012. int res;
  2013. res = chan->span->ops->close(chan);
  2014. if (res)
  2015. module_printk(KERN_NOTICE,
  2016. "%s: close() failed: %d\n",
  2017. __func__, res);
  2018. }
  2019. }
  2020. }
  2021. msleep(20);
  2022. /*
  2023. * FIXME: THE BIG SLEEP above, is hiding a terrible
  2024. * race condition:
  2025. * - the module_put() ahead, would allow the low-level driver
  2026. * to free the channel.
  2027. * - We should make sure no-one reference this channel
  2028. * from now on.
  2029. */
  2030. if (chan->span)
  2031. put_span(chan->span);
  2032. }
  2033. }
  2034. static ssize_t dahdi_chan_read(struct file *file, char __user *usrbuf,
  2035. size_t count, loff_t *ppos)
  2036. {
  2037. struct dahdi_chan *chan = file->private_data;
  2038. int amnt;
  2039. int res, rv;
  2040. int oldbuf,x;
  2041. unsigned long flags;
  2042. /* Make sure count never exceeds 65k, and make sure it's unsigned */
  2043. count &= 0xffff;
  2044. if (unlikely(!chan)) {
  2045. /*
  2046. * This should never happen. Surprise device removal
  2047. * should lead us to the nodev_* file_operations
  2048. */
  2049. msleep(5);
  2050. module_printk(KERN_ERR, "%s: NODEV\n", __func__);
  2051. return -ENODEV;
  2052. }
  2053. if (unlikely(count < 1))
  2054. return -EINVAL;
  2055. for (;;) {
  2056. spin_lock_irqsave(&chan->lock, flags);
  2057. if (chan->eventinidx != chan->eventoutidx) {
  2058. spin_unlock_irqrestore(&chan->lock, flags);
  2059. return -ELAST /* - chan->eventbuf[chan->eventoutidx]*/;
  2060. }
  2061. res = chan->outreadbuf;
  2062. spin_unlock_irqrestore(&chan->lock, flags);
  2063. if (res >= 0)
  2064. break;
  2065. if (file->f_flags & O_NONBLOCK)
  2066. return -EAGAIN;
  2067. /* Wake up when data is available or when the board driver
  2068. * unregistered the channel. */
  2069. rv = wait_event_interruptible(chan->waitq,
  2070. (!chan->file->private_data || chan->outreadbuf > -1));
  2071. if (rv)
  2072. return rv;
  2073. if (unlikely(!chan->file->private_data))
  2074. return -ENODEV;
  2075. }
  2076. amnt = count;
  2077. if (chan->flags & DAHDI_FLAG_LINEAR) {
  2078. if (amnt > (chan->readn[res] << 1))
  2079. amnt = chan->readn[res] << 1;
  2080. if (amnt) {
  2081. /* There seems to be a max stack size, so we have
  2082. to do this in smaller pieces */
  2083. short lindata[128];
  2084. int left = amnt >> 1; /* amnt is in bytes */
  2085. int pos = 0;
  2086. int pass;
  2087. while (left) {
  2088. pass = left;
  2089. if (pass > 128)
  2090. pass = 128;
  2091. for (x = 0; x < pass; x++)
  2092. lindata[x] = DAHDI_XLAW(chan->readbuf[res][x + pos], chan);
  2093. if (copy_to_user(usrbuf + (pos << 1), lindata, pass << 1))
  2094. return -EFAULT;
  2095. left -= pass;
  2096. pos += pass;
  2097. }
  2098. }
  2099. } else {
  2100. if (amnt > chan->readn[res])
  2101. amnt = chan->readn[res];
  2102. if (amnt) {
  2103. if (copy_to_user(usrbuf, chan->readbuf[res], amnt))
  2104. return -EFAULT;
  2105. }
  2106. }
  2107. spin_lock_irqsave(&chan->lock, flags);
  2108. chan->readidx[res] = 0;
  2109. chan->readn[res] = 0;
  2110. oldbuf = res;
  2111. chan->outreadbuf = (res + 1) % chan->numbufs;
  2112. if (chan->outreadbuf == chan->inreadbuf) {
  2113. /* Out of stuff */
  2114. chan->outreadbuf = -1;
  2115. }
  2116. if (chan->inreadbuf < 0) {
  2117. /* Notify interrupt handler that we have some space now */
  2118. chan->inreadbuf = oldbuf;
  2119. }
  2120. spin_unlock_irqrestore(&chan->lock, flags);
  2121. return amnt;
  2122. }
  2123. static int num_filled_bufs(struct dahdi_chan *chan)
  2124. {
  2125. int range1, range2;
  2126. if (chan->inwritebuf < 0) {
  2127. return chan->numbufs;
  2128. }
  2129. if (chan->outwritebuf < 0) {
  2130. return 0;
  2131. }
  2132. if (chan->outwritebuf <= chan->inwritebuf) {
  2133. return chan->inwritebuf - chan->outwritebuf;
  2134. }
  2135. /* This means (in > out) and we have wrap around */
  2136. range1 = chan->numbufs - chan->outwritebuf;
  2137. range2 = chan->inwritebuf;
  2138. return range1 + range2;
  2139. }
  2140. static ssize_t dahdi_chan_write(struct file *file, const char __user *usrbuf,
  2141. size_t count, loff_t *ppos)
  2142. {
  2143. unsigned long flags;
  2144. struct dahdi_chan *chan = file->private_data;
  2145. int res, amnt, oldbuf, rv, x;
  2146. /* Make sure count never exceeds 65k, and make sure it's unsigned */
  2147. count &= 0xffff;
  2148. if (unlikely(!chan)) {
  2149. /*
  2150. * This should never happen. Surprise device removal
  2151. * should lead us to the nodev_* file_operations
  2152. */
  2153. msleep(5);
  2154. module_printk(KERN_ERR, "%s: NODEV\n", __func__);
  2155. return -ENODEV;
  2156. }
  2157. if (unlikely(count < 1))
  2158. return -EINVAL;
  2159. for (;;) {
  2160. spin_lock_irqsave(&chan->lock, flags);
  2161. if ((chan->curtone || chan->pdialcount) && !is_pseudo_chan(chan)) {
  2162. chan->curtone = NULL;
  2163. chan->tonep = 0;
  2164. chan->dialing = 0;
  2165. chan->txdialbuf[0] = '\0';
  2166. chan->pdialcount = 0;
  2167. }
  2168. if (chan->eventinidx != chan->eventoutidx) {
  2169. spin_unlock_irqrestore(&chan->lock, flags);
  2170. return -ELAST;
  2171. }
  2172. res = chan->inwritebuf;
  2173. spin_unlock_irqrestore(&chan->lock, flags);
  2174. if (res >= 0)
  2175. break;
  2176. if (file->f_flags & O_NONBLOCK) {
  2177. #ifdef BUFFER_DEBUG
  2178. printk("Error: Nonblock\n");
  2179. #endif
  2180. return -EAGAIN;
  2181. }
  2182. /* Wake up when room in the write queue is available or when
  2183. * the board driver unregistered the channel. */
  2184. rv = wait_event_interruptible(chan->waitq,
  2185. (!chan->file->private_data || chan->inwritebuf > -1));
  2186. if (rv)
  2187. return rv;
  2188. if (unlikely(!chan->file->private_data))
  2189. return -ENODEV;
  2190. }
  2191. amnt = count;
  2192. if (chan->flags & DAHDI_FLAG_LINEAR) {
  2193. if (amnt > (chan->blocksize << 1))
  2194. amnt = chan->blocksize << 1;
  2195. } else {
  2196. if (amnt > chan->blocksize)
  2197. amnt = chan->blocksize;
  2198. }
  2199. #ifdef CONFIG_DAHDI_DEBUG
  2200. module_printk(KERN_NOTICE, "dahdi_chan_write(chan: %d, res: %d, outwritebuf: %d amnt: %d\n",
  2201. chan->channo, res, chan->outwritebuf, amnt);
  2202. #endif
  2203. if (amnt) {
  2204. if (chan->flags & DAHDI_FLAG_LINEAR) {
  2205. /* There seems to be a max stack size, so we have
  2206. to do this in smaller pieces */
  2207. short lindata[128];
  2208. int left = amnt >> 1; /* amnt is in bytes */
  2209. int pos = 0;
  2210. int pass;
  2211. while (left) {
  2212. pass = left;
  2213. if (pass > 128)
  2214. pass = 128;
  2215. if (copy_from_user(lindata, usrbuf + (pos << 1), pass << 1)) {
  2216. return -EFAULT;
  2217. }
  2218. left -= pass;
  2219. for (x = 0; x < pass; x++)
  2220. chan->writebuf[res][x + pos] = DAHDI_LIN2X(lindata[x], chan);
  2221. pos += pass;
  2222. }
  2223. chan->writen[res] = amnt >> 1;
  2224. } else {
  2225. if (copy_from_user(chan->writebuf[res], usrbuf, amnt)) {
  2226. return -EFAULT;
  2227. }
  2228. chan->writen[res] = amnt;
  2229. }
  2230. #ifdef CONFIG_DAHDI_ECHOCAN_PROCESS_TX
  2231. if ((chan->ec_state) &&
  2232. (ECHO_MODE_ACTIVE == chan->ec_state->status.mode) &&
  2233. (chan->ec_state->ops->echocan_process_tx)) {
  2234. struct dahdi_echocan_state *const ec = chan->ec_state;
  2235. for (x = 0; x < chan->writen[res]; ++x) {
  2236. short tx;
  2237. tx = DAHDI_XLAW(chan->writebuf[res][x], chan);
  2238. ec->ops->echocan_process_tx(ec, &tx, 1);
  2239. chan->writebuf[res][x] = DAHDI_LIN2X((int) tx,
  2240. chan);
  2241. }
  2242. }
  2243. #endif
  2244. chan->writeidx[res] = 0;
  2245. if (chan->flags & DAHDI_FLAG_FCS)
  2246. calc_fcs(chan, res);
  2247. oldbuf = res;
  2248. spin_lock_irqsave(&chan->lock, flags);
  2249. chan->inwritebuf = (res + 1) % chan->numbufs;
  2250. if (chan->inwritebuf == chan->outwritebuf) {
  2251. /* Don't stomp on the transmitter, just wait for them to
  2252. wake us up */
  2253. chan->inwritebuf = -1;
  2254. /* Make sure the transmitter is transmitting in case of POLICY_WHEN_FULL */
  2255. chan->txdisable = 0;
  2256. }
  2257. if (chan->outwritebuf < 0) {
  2258. /* Okay, the interrupt handler has been waiting for us. Give them a buffer */
  2259. chan->outwritebuf = oldbuf;
  2260. }
  2261. if ((chan->txbufpolicy == DAHDI_POLICY_HALF_FULL) && (chan->txdisable)) {
  2262. if (num_filled_bufs(chan) >= (chan->numbufs >> 1)) {
  2263. #ifdef BUFFER_DEBUG
  2264. printk("Reached buffer fill mark of %d\n", num_filled_bufs(chan));
  2265. #endif
  2266. chan->txdisable = 0;
  2267. }
  2268. }
  2269. #ifdef BUFFER_DEBUG
  2270. if ((chan->statcount <= 0) || (amnt != 128) || (num_filled_bufs(chan) != chan->lastnumbufs)) {
  2271. printk("amnt: %d Number of filled buffers: %d\n", amnt, num_filled_bufs(chan));
  2272. chan->statcount = 32000;
  2273. chan->lastnumbufs = num_filled_bufs(chan);
  2274. }
  2275. #endif
  2276. spin_unlock_irqrestore(&chan->lock, flags);
  2277. if (chan->flags & DAHDI_FLAG_NOSTDTXRX && chan->span->ops->hdlc_hard_xmit)
  2278. chan->span->ops->hdlc_hard_xmit(chan);
  2279. }
  2280. return amnt;
  2281. }
  2282. static int dahdi_ctl_open(struct file *file)
  2283. {
  2284. /* Nothing to do, really */
  2285. return 0;
  2286. }
  2287. static int dahdi_chan_open(struct file *file)
  2288. {
  2289. /* Nothing to do here for now either */
  2290. return 0;
  2291. }
  2292. static int dahdi_ctl_release(struct file *file)
  2293. {
  2294. /* Nothing to do */
  2295. return 0;
  2296. }
  2297. static int dahdi_chan_release(struct file *file)
  2298. {
  2299. /* Nothing to do for now */
  2300. return 0;
  2301. }
  2302. static void set_txtone(struct dahdi_chan *ss, int fac, int init_v2, int init_v3)
  2303. {
  2304. if (fac == 0) {
  2305. ss->v2_1 = 0;
  2306. ss->v3_1 = 0;
  2307. return;
  2308. }
  2309. ss->txtone = fac;
  2310. ss->v1_1 = 0;
  2311. ss->v2_1 = init_v2;
  2312. ss->v3_1 = init_v3;
  2313. return;
  2314. }
  2315. static void dahdi_rbs_sethook(struct dahdi_chan *chan, int txsig, int txstate,
  2316. int timeout)
  2317. {
  2318. static const struct {
  2319. unsigned int sig_type;
  2320. /* Index is dahdi_txsig enum */
  2321. unsigned int bits[DAHDI_TXSIG_TOTAL];
  2322. } outs[NUM_SIGS] = {
  2323. {
  2324. /*
  2325. * We set the idle case of the DAHDI_SIG_NONE to this pattern to make idle E1 CAS
  2326. * channels happy. Should not matter with T1, since on an un-configured channel,
  2327. * who cares what the sig bits are as long as they are stable
  2328. */
  2329. .sig_type = DAHDI_SIG_NONE,
  2330. .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_ACD,
  2331. }, {
  2332. .sig_type = DAHDI_SIG_EM,
  2333. .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABCD,
  2334. .bits[DAHDI_TXSIG_START] = DAHDI_BITS_ABCD,
  2335. }, {
  2336. .sig_type = DAHDI_SIG_FXSLS,
  2337. .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BD,
  2338. .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABCD,
  2339. .bits[DAHDI_TXSIG_START] = DAHDI_BITS_ABCD,
  2340. }, {
  2341. .sig_type = DAHDI_SIG_FXSGS,
  2342. .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BD,
  2343. .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABCD,
  2344. #ifndef CONFIG_CAC_GROUNDSTART
  2345. .bits[DAHDI_TXSIG_START] = DAHDI_BITS_AC,
  2346. #endif
  2347. }, {
  2348. .sig_type = DAHDI_SIG_FXSKS,
  2349. .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BD,
  2350. .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABCD,
  2351. .bits[DAHDI_TXSIG_START] = DAHDI_BITS_ABCD,
  2352. }, {
  2353. .sig_type = DAHDI_SIG_FXOLS,
  2354. .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BD,
  2355. .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_BD,
  2356. }, {
  2357. .sig_type = DAHDI_SIG_FXOGS,
  2358. .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_ABCD,
  2359. .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_BD,
  2360. }, {
  2361. .sig_type = DAHDI_SIG_FXOKS,
  2362. .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BD,
  2363. .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_BD,
  2364. .bits[DAHDI_TXSIG_KEWL] = DAHDI_BITS_ABCD,
  2365. }, {
  2366. .sig_type = DAHDI_SIG_SF,
  2367. .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BCD,
  2368. .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABCD,
  2369. .bits[DAHDI_TXSIG_START] = DAHDI_BITS_ABCD,
  2370. .bits[DAHDI_TXSIG_KEWL] = DAHDI_BITS_BCD,
  2371. }, {
  2372. .sig_type = DAHDI_SIG_EM_E1,
  2373. .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_DBIT,
  2374. .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABD,
  2375. .bits[DAHDI_TXSIG_START] = DAHDI_BITS_ABD,
  2376. .bits[DAHDI_TXSIG_KEWL] = DAHDI_DBIT,
  2377. }
  2378. };
  2379. int x;
  2380. /* if no span, return doing nothing */
  2381. if (!chan->span)
  2382. return;
  2383. if (!(chan->span->flags & DAHDI_FLAG_RBS)) {
  2384. module_printk(KERN_NOTICE, "dahdi_rbs: Tried to set RBS hook state on non-RBS channel %s\n", chan->name);
  2385. return;
  2386. }
  2387. if ((txsig > 3) || (txsig < 0)) {
  2388. module_printk(KERN_NOTICE, "dahdi_rbs: Tried to set RBS hook state %d (> 3) on channel %s\n", txsig, chan->name);
  2389. return;
  2390. }
  2391. if (!chan->span->ops->rbsbits && !chan->span->ops->hooksig) {
  2392. module_printk(KERN_NOTICE, "dahdi_rbs: Tried to set RBS hook state %d on channel %s while span %s lacks rbsbits or hooksig function\n",
  2393. txsig, chan->name, chan->span->name);
  2394. return;
  2395. }
  2396. /* Don't do anything for RBS */
  2397. if (chan->sig == DAHDI_SIG_DACS_RBS)
  2398. return;
  2399. chan->txstate = txstate;
  2400. /* if tone signalling */
  2401. if (chan->sig == DAHDI_SIG_SF) {
  2402. chan->txhooksig = txsig;
  2403. if (chan->txtone) { /* if set to make tone for tx */
  2404. if ((txsig && !(chan->toneflags & DAHDI_REVERSE_TXTONE)) ||
  2405. ((!txsig) && (chan->toneflags & DAHDI_REVERSE_TXTONE))) {
  2406. set_txtone(chan,chan->txtone,chan->tx_v2,chan->tx_v3);
  2407. } else {
  2408. set_txtone(chan,0,0,0);
  2409. }
  2410. }
  2411. chan->otimer = timeout * DAHDI_CHUNKSIZE; /* Otimer is timer in samples */
  2412. return;
  2413. }
  2414. if (chan->span->ops->hooksig) {
  2415. if (chan->txhooksig != txsig) {
  2416. chan->txhooksig = txsig;
  2417. chan->span->ops->hooksig(chan, txsig);
  2418. }
  2419. chan->otimer = timeout * DAHDI_CHUNKSIZE; /* Otimer is timer in samples */
  2420. return;
  2421. } else {
  2422. for (x = 0; x < NUM_SIGS; x++) {
  2423. if (outs[x].sig_type == chan->sig) {
  2424. #ifdef CONFIG_DAHDI_DEBUG
  2425. module_printk(KERN_NOTICE, "Setting bits to %d for channel %s state %d in %d signalling\n", outs[x].bits[txsig], chan->name, txsig, chan->sig);
  2426. #endif
  2427. chan->txhooksig = txsig;
  2428. chan->txsig = outs[x].bits[txsig];
  2429. chan->span->ops->rbsbits(chan, chan->txsig);
  2430. chan->otimer = timeout * DAHDI_CHUNKSIZE; /* Otimer is timer in samples */
  2431. return;
  2432. }
  2433. }
  2434. }
  2435. module_printk(KERN_NOTICE, "dahdi_rbs: Don't know RBS signalling type %d on channel %s\n", chan->sig, chan->name);
  2436. }
  2437. static int dahdi_cas_setbits(struct dahdi_chan *chan, int bits)
  2438. {
  2439. /* if no span, return as error */
  2440. if (!chan->span)
  2441. return -1;
  2442. if (chan->span->ops->rbsbits) {
  2443. chan->txsig = bits;
  2444. chan->span->ops->rbsbits(chan, bits);
  2445. } else {
  2446. module_printk(KERN_NOTICE, "Huh? CAS setbits, but no RBS bits function\n");
  2447. }
  2448. return 0;
  2449. }
  2450. static int dahdi_hangup(struct dahdi_chan *chan)
  2451. {
  2452. int x, res = 0;
  2453. /* Can't hangup pseudo channels */
  2454. if (!chan->span)
  2455. return 0;
  2456. /* Can't hang up a clear channel */
  2457. if (chan->flags & (DAHDI_FLAG_CLEAR | DAHDI_FLAG_NOSTDTXRX))
  2458. return -EINVAL;
  2459. chan->kewlonhook = 0;
  2460. if ((chan->sig == DAHDI_SIG_FXSLS) || (chan->sig == DAHDI_SIG_FXSKS) ||
  2461. (chan->sig == DAHDI_SIG_FXSGS)) {
  2462. chan->ringdebtimer = RING_DEBOUNCE_TIME;
  2463. }
  2464. if (chan->span->flags & DAHDI_FLAG_RBS) {
  2465. if (chan->sig == DAHDI_SIG_CAS) {
  2466. dahdi_cas_setbits(chan, chan->idlebits);
  2467. } else if ((chan->sig == DAHDI_SIG_FXOKS) && (chan->txstate != DAHDI_TXSTATE_ONHOOK)
  2468. /* if other party is already on-hook we shouldn't do any battery drop */
  2469. && !((chan->rxhooksig == DAHDI_RXSIG_ONHOOK) && (chan->itimer <= 0))) {
  2470. /* Do RBS signalling on the channel's behalf */
  2471. dahdi_rbs_sethook(chan, DAHDI_TXSIG_KEWL, DAHDI_TXSTATE_KEWL, DAHDI_KEWLTIME);
  2472. } else
  2473. dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_ONHOOK, 0);
  2474. } else {
  2475. /* Let the driver hang up the line if it wants to */
  2476. if (chan->span->ops->sethook) {
  2477. if (chan->txhooksig != DAHDI_ONHOOK) {
  2478. chan->txhooksig = DAHDI_ONHOOK;
  2479. res = chan->span->ops->sethook(chan, DAHDI_ONHOOK);
  2480. } else
  2481. res = 0;
  2482. }
  2483. }
  2484. /* if not registered yet, just return here */
  2485. if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &chan->flags))
  2486. return res;
  2487. /* Mark all buffers as empty */
  2488. for (x = 0; x < chan->numbufs; x++) {
  2489. chan->writen[x] =
  2490. chan->writeidx[x]=
  2491. chan->readn[x]=
  2492. chan->readidx[x] = 0;
  2493. }
  2494. if (chan->readbuf[0]) {
  2495. chan->inreadbuf = 0;
  2496. chan->inwritebuf = 0;
  2497. } else {
  2498. chan->inreadbuf = -1;
  2499. chan->inwritebuf = -1;
  2500. }
  2501. chan->outreadbuf = -1;
  2502. chan->outwritebuf = -1;
  2503. chan->dialing = 0;
  2504. chan->afterdialingtimer = 0;
  2505. chan->curtone = NULL;
  2506. chan->pdialcount = 0;
  2507. chan->cadencepos = 0;
  2508. chan->txdialbuf[0] = 0;
  2509. return res;
  2510. }
  2511. static int initialize_channel(struct dahdi_chan *chan)
  2512. {
  2513. int res;
  2514. unsigned long flags;
  2515. const void *rxgain = NULL;
  2516. struct dahdi_echocan_state *ec_state;
  2517. const struct dahdi_echocan_factory *ec_current;
  2518. if ((res = dahdi_reallocbufs(chan, DAHDI_DEFAULT_BLOCKSIZE, DAHDI_DEFAULT_NUM_BUFS)))
  2519. return res;
  2520. spin_lock_irqsave(&chan->lock, flags);
  2521. chan->txbufpolicy = DAHDI_POLICY_IMMEDIATE;
  2522. ec_state = chan->ec_state;
  2523. chan->ec_state = NULL;
  2524. ec_current = chan->ec_current;
  2525. chan->ec_current = NULL;
  2526. chan->txdisable = 0;
  2527. chan->digitmode = DIGIT_MODE_DTMF;
  2528. chan->dialing = 0;
  2529. chan->afterdialingtimer = 0;
  2530. chan->cadencepos = 0;
  2531. chan->firstcadencepos = 0; /* By default loop back to first cadence position */
  2532. /* HDLC & FCS stuff */
  2533. fasthdlc_init(&chan->rxhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
  2534. fasthdlc_init(&chan->txhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
  2535. chan->infcs = PPP_INITFCS;
  2536. /* Timings for RBS */
  2537. chan->prewinktime = DAHDI_DEFAULT_PREWINKTIME;
  2538. chan->preflashtime = DAHDI_DEFAULT_PREFLASHTIME;
  2539. chan->winktime = DAHDI_DEFAULT_WINKTIME;
  2540. chan->flashtime = DAHDI_DEFAULT_FLASHTIME;
  2541. if (chan->sig & __DAHDI_SIG_FXO)
  2542. chan->starttime = DAHDI_DEFAULT_RINGTIME;
  2543. else
  2544. chan->starttime = DAHDI_DEFAULT_STARTTIME;
  2545. chan->rxwinktime = DAHDI_DEFAULT_RXWINKTIME;
  2546. chan->rxflashtime = DAHDI_DEFAULT_RXFLASHTIME;
  2547. chan->debouncetime = DAHDI_DEFAULT_DEBOUNCETIME;
  2548. chan->pulsemaketime = DAHDI_DEFAULT_PULSEMAKETIME;
  2549. chan->pulsebreaktime = DAHDI_DEFAULT_PULSEBREAKTIME;
  2550. chan->pulseaftertime = DAHDI_DEFAULT_PULSEAFTERTIME;
  2551. /* Initialize RBS timers */
  2552. chan->itimerset = chan->itimer = chan->otimer = 0;
  2553. chan->ringdebtimer = 0;
  2554. /* Reset conferences */
  2555. reset_conf(chan);
  2556. chan->dacs_chan = NULL;
  2557. /* I/O Mask, etc */
  2558. chan->iomask = 0;
  2559. /* release conference resource if any */
  2560. if (chan->confna)
  2561. dahdi_check_conf(chan->confna);
  2562. if ((chan->sig & __DAHDI_SIG_DACS) != __DAHDI_SIG_DACS) {
  2563. chan->confna = 0;
  2564. chan->confmode = 0;
  2565. chan->conf_chan = NULL;
  2566. dahdi_disable_dacs(chan);
  2567. }
  2568. chan->_confn = 0;
  2569. memset(chan->conflast, 0, sizeof(chan->conflast));
  2570. memset(chan->conflast1, 0, sizeof(chan->conflast1));
  2571. memset(chan->conflast2, 0, sizeof(chan->conflast2));
  2572. chan->confmute = 0;
  2573. chan->gotgs = 0;
  2574. chan->curtone = NULL;
  2575. chan->tonep = 0;
  2576. chan->pdialcount = 0;
  2577. if (is_gain_allocated(chan))
  2578. rxgain = chan->rxgain;
  2579. chan->rxgain = defgain;
  2580. chan->txgain = defgain;
  2581. chan->eventinidx = chan->eventoutidx = 0;
  2582. dahdi_set_law(chan, DAHDI_LAW_DEFAULT);
  2583. dahdi_hangup(chan);
  2584. /* Make sure that the audio flag is cleared on a clear channel */
  2585. if ((chan->sig & DAHDI_SIG_CLEAR) || (chan->sig & DAHDI_SIG_HARDHDLC))
  2586. chan->flags &= ~DAHDI_FLAG_AUDIO;
  2587. if ((chan->sig == DAHDI_SIG_CLEAR) || (chan->sig == DAHDI_SIG_HARDHDLC))
  2588. chan->flags &= ~(DAHDI_FLAG_PPP | DAHDI_FLAG_FCS | DAHDI_FLAG_HDLC);
  2589. chan->flags &= ~DAHDI_FLAG_LINEAR;
  2590. if (chan->curzone) {
  2591. /* Take cadence from tone zone */
  2592. memcpy(chan->ringcadence, chan->curzone->ringcadence, sizeof(chan->ringcadence));
  2593. } else {
  2594. /* Do a default */
  2595. memset(chan->ringcadence, 0, sizeof(chan->ringcadence));
  2596. chan->ringcadence[0] = chan->starttime;
  2597. chan->ringcadence[1] = DAHDI_RINGOFFTIME;
  2598. }
  2599. if (ec_state) {
  2600. ec_state->ops->echocan_free(chan, ec_state);
  2601. release_echocan(ec_current);
  2602. }
  2603. spin_unlock_irqrestore(&chan->lock, flags);
  2604. set_tone_zone(chan, DEFAULT_TONE_ZONE);
  2605. if (rxgain)
  2606. kfree(rxgain);
  2607. return 0;
  2608. }
  2609. static const struct file_operations dahdi_timer_fops;
  2610. static int dahdi_timer_open(struct file *file)
  2611. {
  2612. struct dahdi_timer *t = kzalloc(sizeof(*t), GFP_KERNEL);
  2613. if (!t)
  2614. return -ENOMEM;
  2615. INIT_LIST_HEAD(&t->list);
  2616. init_waitqueue_head(&t->sel);
  2617. file->private_data = t;
  2618. spin_lock_init(&t->lock);
  2619. file->f_op = &dahdi_timer_fops;
  2620. return 0;
  2621. }
  2622. static int dahdi_timer_release(struct inode *inode, struct file *file)
  2623. {
  2624. struct dahdi_timer *timer = file->private_data;
  2625. unsigned long flags;
  2626. if (!timer)
  2627. return 0;
  2628. spin_lock_irqsave(&timer->lock, flags);
  2629. if (!list_empty(&timer->list)) {
  2630. spin_unlock(&timer->lock);
  2631. spin_lock(&dahdi_timer_lock);
  2632. spin_lock(&timer->lock);
  2633. list_del_init(&timer->list);
  2634. spin_unlock(&dahdi_timer_lock);
  2635. }
  2636. file->private_data = NULL;
  2637. spin_unlock_irqrestore(&timer->lock, flags);
  2638. kfree(timer);
  2639. return 0;
  2640. }
  2641. static const struct file_operations dahdi_chan_fops;
  2642. static int dahdi_specchan_open(struct file *file)
  2643. {
  2644. int res = -ENXIO;
  2645. struct dahdi_chan *const chan = chan_from_file(file);
  2646. if (!chan || !chan->sig)
  2647. return -ENXIO;
  2648. /* Make sure we're not already open, a net device, or a slave
  2649. * device */
  2650. if (dahdi_have_netdev(chan))
  2651. res = -EBUSY;
  2652. else if (chan->master != chan)
  2653. res = -EBUSY;
  2654. else if ((chan->sig & __DAHDI_SIG_DACS) == __DAHDI_SIG_DACS)
  2655. res = -EBUSY;
  2656. else if (!test_and_set_bit(DAHDI_FLAGBIT_OPEN, &chan->flags)) {
  2657. unsigned long flags;
  2658. const struct dahdi_span_ops *const ops =
  2659. (!is_pseudo_chan(chan)) ? chan->span->ops : NULL;
  2660. if (ops && !try_module_get(ops->owner)) {
  2661. clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
  2662. return -ENXIO;
  2663. }
  2664. res = initialize_channel(chan);
  2665. if (res) {
  2666. /* Reallocbufs must have failed */
  2667. clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
  2668. return res;
  2669. }
  2670. spin_lock_irqsave(&chan->lock, flags);
  2671. if (is_pseudo_chan(chan))
  2672. chan->flags |= DAHDI_FLAG_AUDIO;
  2673. chan->file = file;
  2674. file->private_data = chan;
  2675. /* Since we know we're a channel now, we can
  2676. * update the f_op pointer and bypass a few of
  2677. * the checks on the minor number. */
  2678. file->f_op = &dahdi_chan_fops;
  2679. spin_unlock_irqrestore(&chan->lock, flags);
  2680. if (ops && ops->open) {
  2681. res = ops->open(chan);
  2682. if (res) {
  2683. spin_lock_irqsave(&chan->lock, flags);
  2684. chan->file = NULL;
  2685. file->private_data = NULL;
  2686. spin_unlock_irqrestore(&chan->lock, flags);
  2687. module_put(ops->owner);
  2688. close_channel(chan);
  2689. clear_bit(DAHDI_FLAGBIT_OPEN,
  2690. &chan->flags);
  2691. }
  2692. }
  2693. } else {
  2694. res = -EBUSY;
  2695. }
  2696. return res;
  2697. }
  2698. static int dahdi_specchan_release(struct file *file)
  2699. {
  2700. int res=0;
  2701. unsigned long flags;
  2702. struct dahdi_chan *chan = chan_from_file(file);
  2703. if (chan) {
  2704. /* Chan lock protects contents against potentially non atomic accesses.
  2705. * So if the pointer setting is not atomic, we should protect */
  2706. #ifdef CONFIG_DAHDI_MIRROR
  2707. if (chan->srcmirror) {
  2708. struct dahdi_chan *const srcmirror = chan->srcmirror;
  2709. spin_lock_irqsave(&srcmirror->lock, flags);
  2710. if (chan == srcmirror->txmirror) {
  2711. module_printk(KERN_INFO, "Chan %d tx mirror " \
  2712. "to %d stopped\n",
  2713. srcmirror->txmirror->channo,
  2714. srcmirror->channo);
  2715. srcmirror->txmirror = NULL;
  2716. }
  2717. if (chan == srcmirror->rxmirror) {
  2718. module_printk(KERN_INFO, "Chan %d rx mirror " \
  2719. "to %d stopped\n",
  2720. srcmirror->rxmirror->channo,
  2721. srcmirror->channo);
  2722. chan->srcmirror->rxmirror = NULL;
  2723. }
  2724. spin_unlock_irqrestore(&chan->srcmirror->lock, flags);
  2725. }
  2726. #endif /* CONFIG_DAHDI_MIRROR */
  2727. spin_lock_irqsave(&chan->lock, flags);
  2728. chan->file = NULL;
  2729. file->private_data = NULL;
  2730. #ifdef CONFIG_DAHDI_MIRROR
  2731. chan->srcmirror = NULL;
  2732. #endif /* CONFIG_DAHDI_MIRROR */
  2733. spin_unlock_irqrestore(&chan->lock, flags);
  2734. close_channel(chan);
  2735. clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
  2736. if (chan->span) {
  2737. struct module *owner = chan->span->ops->owner;
  2738. if (chan->span->ops->close)
  2739. res = chan->span->ops->close(chan);
  2740. module_put(owner);
  2741. }
  2742. } else
  2743. res = -ENXIO;
  2744. return res;
  2745. }
  2746. static int can_open_timer(void)
  2747. {
  2748. #ifdef CONFIG_DAHDI_CORE_TIMER
  2749. return 1;
  2750. #else
  2751. return (list_empty(&span_list)) ? 0 : 1;
  2752. #endif
  2753. }
  2754. static unsigned int max_pseudo_channels = 512;
  2755. static unsigned int num_pseudo_channels;
  2756. /**
  2757. * dahdi_alloc_pseudo() - Returns a new pseudo channel.
  2758. *
  2759. * Call with the registration_mutex held since this function will determine a
  2760. * channel number, and must be protected from additional registrations while
  2761. * that is happening.
  2762. *
  2763. */
  2764. static struct dahdi_chan *dahdi_alloc_pseudo(struct file *file)
  2765. {
  2766. struct pseudo_chan *pseudo;
  2767. unsigned long flags;
  2768. unsigned int channo;
  2769. struct pseudo_chan *p;
  2770. struct list_head *pos = &pseudo_chans;
  2771. /* Don't allow /dev/dahdi/pseudo to open if there is not a timing
  2772. * source. */
  2773. if (!can_open_timer())
  2774. return NULL;
  2775. if (unlikely(num_pseudo_channels >= max_pseudo_channels))
  2776. return NULL;
  2777. pseudo = kzalloc(sizeof(*pseudo), GFP_KERNEL);
  2778. if (NULL == pseudo)
  2779. return NULL;
  2780. pseudo->chan.sig = DAHDI_SIG_CLEAR;
  2781. pseudo->chan.sigcap = DAHDI_SIG_CLEAR;
  2782. pseudo->chan.flags = DAHDI_FLAG_AUDIO;
  2783. pseudo->chan.span = NULL; /* No span == psuedo channel */
  2784. channo = FIRST_PSEUDO_CHANNEL;
  2785. list_for_each_entry(p, &pseudo_chans, node) {
  2786. if (channo != p->chan.channo)
  2787. break;
  2788. pos = &p->node;
  2789. ++channo;
  2790. }
  2791. pseudo->chan.channo = channo;
  2792. pseudo->chan.chanpos = channo - FIRST_PSEUDO_CHANNEL + 1;
  2793. __dahdi_init_chan(&pseudo->chan);
  2794. dahdi_chan_reg(&pseudo->chan);
  2795. snprintf(pseudo->chan.name, sizeof(pseudo->chan.name)-1,
  2796. "Pseudo/%d", pseudo->chan.chanpos);
  2797. file->private_data = &pseudo->chan;
  2798. /* Once we place the pseudo chan on the list...it's registered and
  2799. * live. */
  2800. spin_lock_irqsave(&chan_lock, flags);
  2801. ++num_pseudo_channels;
  2802. list_add(&pseudo->node, pos);
  2803. spin_unlock_irqrestore(&chan_lock, flags);
  2804. return &pseudo->chan;
  2805. }
  2806. static void dahdi_free_pseudo(struct dahdi_chan *chan)
  2807. {
  2808. struct pseudo_chan *pseudo;
  2809. unsigned long flags;
  2810. if (!chan)
  2811. return;
  2812. mutex_lock(&registration_mutex);
  2813. pseudo = chan_to_pseudo(chan);
  2814. spin_lock_irqsave(&chan_lock, flags);
  2815. list_del(&pseudo->node);
  2816. --num_pseudo_channels;
  2817. spin_unlock_irqrestore(&chan_lock, flags);
  2818. dahdi_chan_unreg(chan);
  2819. mutex_unlock(&registration_mutex);
  2820. kfree(pseudo);
  2821. }
  2822. static int dahdi_open(struct inode *inode, struct file *file)
  2823. {
  2824. int unit = UNIT(file);
  2825. struct dahdi_chan *chan;
  2826. /* Minor 0: Special "control" descriptor */
  2827. if (unit == DAHDI_CTL)
  2828. return dahdi_ctl_open(file);
  2829. if (unit == DAHDI_TRANSCODE) {
  2830. if (!dahdi_transcode_fops) {
  2831. if (request_module("dahdi_transcode")) {
  2832. return -ENXIO;
  2833. }
  2834. }
  2835. if (!try_module_get(dahdi_transcode_fops->owner)) {
  2836. return -ENXIO;
  2837. }
  2838. if (dahdi_transcode_fops && dahdi_transcode_fops->open) {
  2839. return dahdi_transcode_fops->open(inode, file);
  2840. } else {
  2841. /* dahdi_transcode module should have exported a
  2842. * file_operations table. */
  2843. WARN_ON(1);
  2844. }
  2845. return -ENXIO;
  2846. }
  2847. if (unit == DAHDI_TIMER) {
  2848. if (can_open_timer()) {
  2849. return dahdi_timer_open(file);
  2850. } else {
  2851. return -ENXIO;
  2852. }
  2853. }
  2854. if (unit == DAHDI_CHANNEL)
  2855. return dahdi_chan_open(file);
  2856. if (unit == DAHDI_PSEUDO) {
  2857. mutex_lock(&registration_mutex);
  2858. chan = dahdi_alloc_pseudo(file);
  2859. mutex_unlock(&registration_mutex);
  2860. if (unlikely(!chan))
  2861. return -ENOMEM;
  2862. return dahdi_specchan_open(file);
  2863. }
  2864. return dahdi_specchan_open(file);
  2865. }
  2866. /**
  2867. * dahdi_ioctl_defaultzone() - Set defzone to the default.
  2868. * @defzone: The number of the default zone.
  2869. *
  2870. * The default zone is the zone that will be used if the channels request the
  2871. * default zone in dahdi_ioctl_chanconfig. The first entry on the tone_zones
  2872. * list is the default zone. This function searches the list for the zone,
  2873. * and if found, moves it to the head of the list.
  2874. */
  2875. static int dahdi_ioctl_defaultzone(unsigned long data)
  2876. {
  2877. int defzone;
  2878. struct dahdi_zone *cur;
  2879. struct dahdi_zone *dz = NULL;
  2880. if (get_user(defzone, (int __user *)data))
  2881. return -EFAULT;
  2882. spin_lock(&zone_lock);
  2883. list_for_each_entry(cur, &tone_zones, node) {
  2884. if (cur->num != defzone)
  2885. continue;
  2886. dz = cur;
  2887. break;
  2888. }
  2889. if (dz)
  2890. list_move(&dz->node, &tone_zones);
  2891. spin_unlock(&zone_lock);
  2892. return (dz) ? 0 : -EINVAL;
  2893. }
  2894. /* No bigger than 32k for everything per tone zone */
  2895. #define MAX_SIZE 32768
  2896. /* No more than 128 subtones */
  2897. #define MAX_TONES 128
  2898. /* The tones to be loaded can (will) be a mix of regular tones,
  2899. DTMF tones and MF tones. We need to load DTMF and MF tones
  2900. a bit differently than regular tones because their storage
  2901. format is much simpler (an array structure field of the zone
  2902. structure, rather an array of pointers).
  2903. */
  2904. static int dahdi_ioctl_loadzone(unsigned long data)
  2905. {
  2906. struct load_zone_workarea {
  2907. struct dahdi_tone *samples[MAX_TONES];
  2908. short next[MAX_TONES];
  2909. struct dahdi_tone_def_header th;
  2910. struct dahdi_tone_def td;
  2911. } *work;
  2912. size_t space;
  2913. size_t size;
  2914. int res;
  2915. int x;
  2916. void *ptr;
  2917. struct dahdi_zone *z = NULL;
  2918. struct dahdi_tone *t = NULL;
  2919. void __user * user_data = (void __user *)data;
  2920. const unsigned char MAX_ZONE = -1;
  2921. work = kzalloc(sizeof(*work), GFP_KERNEL);
  2922. if (!work)
  2923. return -ENOMEM;
  2924. if (copy_from_user(&work->th, user_data, sizeof(work->th))) {
  2925. res = -EFAULT;
  2926. goto error_exit;
  2927. }
  2928. if ((work->th.zone < 0) || (work->th.zone > MAX_ZONE)) {
  2929. res = -EINVAL;
  2930. goto error_exit;
  2931. }
  2932. user_data += sizeof(work->th);
  2933. if ((work->th.count < 0) || (work->th.count > MAX_TONES)) {
  2934. module_printk(KERN_NOTICE, "Too many tones included\n");
  2935. res = -EINVAL;
  2936. goto error_exit;
  2937. }
  2938. space = size = sizeof(*z) + work->th.count * sizeof(*t);
  2939. if (size > MAX_SIZE) {
  2940. res = -E2BIG;
  2941. goto error_exit;
  2942. }
  2943. z = ptr = kzalloc(size, GFP_KERNEL);
  2944. if (!z) {
  2945. res = -ENOMEM;
  2946. goto error_exit;
  2947. }
  2948. ptr = (char *) ptr + sizeof(*z);
  2949. space -= sizeof(*z);
  2950. z->name = kasprintf(GFP_KERNEL, work->th.name);
  2951. if (!z->name) {
  2952. res = -ENOMEM;
  2953. goto error_exit;
  2954. }
  2955. for (x = 0; x < DAHDI_MAX_CADENCE; x++)
  2956. z->ringcadence[x] = work->th.ringcadence[x];
  2957. mutex_lock(&global_dialparamslock);
  2958. for (x = 0; x < work->th.count; x++) {
  2959. enum {
  2960. REGULAR_TONE,
  2961. DTMF_TONE,
  2962. MFR1_TONE,
  2963. MFR2_FWD_TONE,
  2964. MFR2_REV_TONE,
  2965. } tone_type;
  2966. if (space < sizeof(*t)) {
  2967. module_printk(KERN_NOTICE, "Insufficient tone zone space\n");
  2968. res = -EINVAL;
  2969. goto unlock_error_exit;
  2970. }
  2971. res = copy_from_user(&work->td, user_data,
  2972. sizeof(work->td));
  2973. if (res) {
  2974. res = -EFAULT;
  2975. goto unlock_error_exit;
  2976. }
  2977. user_data += sizeof(work->td);
  2978. if ((work->td.tone >= 0) && (work->td.tone < DAHDI_TONE_MAX)) {
  2979. tone_type = REGULAR_TONE;
  2980. t = work->samples[x] = ptr;
  2981. space -= sizeof(*t);
  2982. ptr = (char *) ptr + sizeof(*t);
  2983. /* Remember which sample is work->next */
  2984. work->next[x] = work->td.next;
  2985. /* Make sure the "next" one is sane */
  2986. if ((work->next[x] >= work->th.count) ||
  2987. (work->next[x] < 0)) {
  2988. module_printk(KERN_NOTICE,
  2989. "Invalid 'next' pointer: %d\n",
  2990. work->next[x]);
  2991. res = -EINVAL;
  2992. goto unlock_error_exit;
  2993. }
  2994. } else if ((work->td.tone >= DAHDI_TONE_DTMF_BASE) &&
  2995. (work->td.tone <= DAHDI_TONE_DTMF_MAX)) {
  2996. tone_type = DTMF_TONE;
  2997. work->td.tone -= DAHDI_TONE_DTMF_BASE;
  2998. t = &z->dtmf[work->td.tone];
  2999. } else if ((work->td.tone >= DAHDI_TONE_MFR1_BASE) &&
  3000. (work->td.tone <= DAHDI_TONE_MFR1_MAX)) {
  3001. tone_type = MFR1_TONE;
  3002. work->td.tone -= DAHDI_TONE_MFR1_BASE;
  3003. t = &z->mfr1[work->td.tone];
  3004. } else if ((work->td.tone >= DAHDI_TONE_MFR2_FWD_BASE) &&
  3005. (work->td.tone <= DAHDI_TONE_MFR2_FWD_MAX)) {
  3006. tone_type = MFR2_FWD_TONE;
  3007. work->td.tone -= DAHDI_TONE_MFR2_FWD_BASE;
  3008. t = &z->mfr2_fwd[work->td.tone];
  3009. } else if ((work->td.tone >= DAHDI_TONE_MFR2_REV_BASE) &&
  3010. (work->td.tone <= DAHDI_TONE_MFR2_REV_MAX)) {
  3011. tone_type = MFR2_REV_TONE;
  3012. work->td.tone -= DAHDI_TONE_MFR2_REV_BASE;
  3013. t = &z->mfr2_rev[work->td.tone];
  3014. } else {
  3015. module_printk(KERN_NOTICE,
  3016. "Invalid tone (%d) defined\n",
  3017. work->td.tone);
  3018. res = -EINVAL;
  3019. goto unlock_error_exit;
  3020. }
  3021. t->fac1 = work->td.fac1;
  3022. t->init_v2_1 = work->td.init_v2_1;
  3023. t->init_v3_1 = work->td.init_v3_1;
  3024. t->fac2 = work->td.fac2;
  3025. t->init_v2_2 = work->td.init_v2_2;
  3026. t->init_v3_2 = work->td.init_v3_2;
  3027. t->modulate = work->td.modulate;
  3028. switch (tone_type) {
  3029. case REGULAR_TONE:
  3030. t->tonesamples = work->td.samples;
  3031. if (!z->tones[work->td.tone])
  3032. z->tones[work->td.tone] = t;
  3033. break;
  3034. case DTMF_TONE:
  3035. t->tonesamples = global_dialparams.dtmf_tonelen;
  3036. t->next = &dtmf_silence;
  3037. z->dtmf_continuous[work->td.tone] = *t;
  3038. z->dtmf_continuous[work->td.tone].next =
  3039. &z->dtmf_continuous[work->td.tone];
  3040. break;
  3041. case MFR1_TONE:
  3042. switch (work->td.tone + DAHDI_TONE_MFR1_BASE) {
  3043. case DAHDI_TONE_MFR1_KP:
  3044. case DAHDI_TONE_MFR1_ST:
  3045. case DAHDI_TONE_MFR1_STP:
  3046. case DAHDI_TONE_MFR1_ST2P:
  3047. case DAHDI_TONE_MFR1_ST3P:
  3048. /* signaling control tones are always 100ms */
  3049. t->tonesamples = 100 * DAHDI_CHUNKSIZE;
  3050. break;
  3051. default:
  3052. t->tonesamples = global_dialparams.mfv1_tonelen;
  3053. break;
  3054. }
  3055. t->next = &mfr1_silence;
  3056. break;
  3057. case MFR2_FWD_TONE:
  3058. t->tonesamples = global_dialparams.mfr2_tonelen;
  3059. t->next = &dtmf_silence;
  3060. z->mfr2_fwd_continuous[work->td.tone] = *t;
  3061. z->mfr2_fwd_continuous[work->td.tone].next =
  3062. &z->mfr2_fwd_continuous[work->td.tone];
  3063. break;
  3064. case MFR2_REV_TONE:
  3065. t->tonesamples = global_dialparams.mfr2_tonelen;
  3066. t->next = &dtmf_silence;
  3067. z->mfr2_rev_continuous[work->td.tone] = *t;
  3068. z->mfr2_rev_continuous[work->td.tone].next =
  3069. &z->mfr2_rev_continuous[work->td.tone];
  3070. break;
  3071. }
  3072. }
  3073. mutex_unlock(&global_dialparamslock);
  3074. for (x = 0; x < work->th.count; x++) {
  3075. if (work->samples[x])
  3076. work->samples[x]->next = work->samples[work->next[x]];
  3077. }
  3078. z->num = work->th.zone;
  3079. /* After we call dahdi_register_tone_zone, the only safe way to free
  3080. * the zone is with a tone_zone_put call. */
  3081. res = dahdi_register_tone_zone(z);
  3082. if (res)
  3083. tone_zone_put(z);
  3084. kfree(work);
  3085. return res;
  3086. unlock_error_exit:
  3087. mutex_unlock(&global_dialparamslock);
  3088. error_exit:
  3089. if (z)
  3090. kfree(z->name);
  3091. kfree(z);
  3092. kfree(work);
  3093. return res;
  3094. }
  3095. void dahdi_init_tone_state(struct dahdi_tone_state *ts, struct dahdi_tone *zt)
  3096. {
  3097. ts->v1_1 = 0;
  3098. ts->v2_1 = zt->init_v2_1;
  3099. ts->v3_1 = zt->init_v3_1;
  3100. ts->v1_2 = 0;
  3101. ts->v2_2 = zt->init_v2_2;
  3102. ts->v3_2 = zt->init_v3_2;
  3103. ts->modulate = zt->modulate;
  3104. }
  3105. struct dahdi_tone *dahdi_mf_tone(const struct dahdi_chan *chan, char digit, int digitmode)
  3106. {
  3107. unsigned int tone_index;
  3108. if (!chan->curzone) {
  3109. static int __warnonce = 1;
  3110. if (__warnonce) {
  3111. __warnonce = 0;
  3112. /* The tonezones are loaded by dahdi_cfg based on /etc/dahdi/system.conf. */
  3113. module_printk(KERN_WARNING, "Cannot get dtmf tone until tone zone is loaded.\n");
  3114. }
  3115. return NULL;
  3116. }
  3117. switch (digitmode) {
  3118. case DIGIT_MODE_PULSE:
  3119. /* We should only get here with a pulse digit if we need
  3120. * to "dial" 'W' (wait 0.5 second)
  3121. */
  3122. if (digit == 'W')
  3123. return &tone_pause;
  3124. return NULL;
  3125. case DIGIT_MODE_DTMF:
  3126. switch (digit) {
  3127. case '0':
  3128. case '1':
  3129. case '2':
  3130. case '3':
  3131. case '4':
  3132. case '5':
  3133. case '6':
  3134. case '7':
  3135. case '8':
  3136. case '9':
  3137. tone_index = DAHDI_TONE_DTMF_0 + (digit - '0');
  3138. break;
  3139. case '*':
  3140. tone_index = DAHDI_TONE_DTMF_s;
  3141. break;
  3142. case '#':
  3143. tone_index = DAHDI_TONE_DTMF_p;
  3144. break;
  3145. case 'A':
  3146. case 'B':
  3147. case 'C':
  3148. case 'D':
  3149. tone_index = DAHDI_TONE_DTMF_A + (digit - 'A');
  3150. break;
  3151. case 'W':
  3152. return &tone_pause;
  3153. default:
  3154. return NULL;
  3155. }
  3156. return &chan->curzone->dtmf[tone_index - DAHDI_TONE_DTMF_BASE];
  3157. case DIGIT_MODE_MFR1:
  3158. switch (digit) {
  3159. case '0':
  3160. case '1':
  3161. case '2':
  3162. case '3':
  3163. case '4':
  3164. case '5':
  3165. case '6':
  3166. case '7':
  3167. case '8':
  3168. case '9':
  3169. tone_index = DAHDI_TONE_MFR1_0 + (digit - '0');
  3170. break;
  3171. case '*':
  3172. tone_index = DAHDI_TONE_MFR1_KP;
  3173. break;
  3174. case '#':
  3175. tone_index = DAHDI_TONE_MFR1_ST;
  3176. break;
  3177. case 'A':
  3178. tone_index = DAHDI_TONE_MFR1_STP;
  3179. break;
  3180. case 'B':
  3181. tone_index = DAHDI_TONE_MFR1_ST2P;
  3182. break;
  3183. case 'C':
  3184. tone_index = DAHDI_TONE_MFR1_ST3P;
  3185. break;
  3186. case 'W':
  3187. return &tone_pause;
  3188. default:
  3189. return NULL;
  3190. }
  3191. return &chan->curzone->mfr1[tone_index - DAHDI_TONE_MFR1_BASE];
  3192. case DIGIT_MODE_MFR2_FWD:
  3193. switch (digit) {
  3194. case '1':
  3195. case '2':
  3196. case '3':
  3197. case '4':
  3198. case '5':
  3199. case '6':
  3200. case '7':
  3201. case '8':
  3202. case '9':
  3203. tone_index = DAHDI_TONE_MFR2_FWD_1 + (digit - '1');
  3204. break;
  3205. case 'A':
  3206. case 'B':
  3207. case 'C':
  3208. case 'D':
  3209. case 'E':
  3210. case 'F':
  3211. tone_index = DAHDI_TONE_MFR2_FWD_10 + (digit - 'A');
  3212. break;
  3213. case 'W':
  3214. return &tone_pause;
  3215. default:
  3216. return NULL;
  3217. }
  3218. return &chan->curzone->mfr2_fwd[tone_index - DAHDI_TONE_MFR2_FWD_BASE];
  3219. case DIGIT_MODE_MFR2_REV:
  3220. switch (digit) {
  3221. case '1':
  3222. case '2':
  3223. case '3':
  3224. case '4':
  3225. case '5':
  3226. case '6':
  3227. case '7':
  3228. case '8':
  3229. case '9':
  3230. tone_index = DAHDI_TONE_MFR2_REV_1 + (digit - '1');
  3231. break;
  3232. case 'A':
  3233. case 'B':
  3234. case 'C':
  3235. case 'D':
  3236. case 'E':
  3237. case 'F':
  3238. tone_index = DAHDI_TONE_MFR2_REV_10 + (digit - 'A');
  3239. break;
  3240. case 'W':
  3241. return &tone_pause;
  3242. default:
  3243. return NULL;
  3244. }
  3245. return &chan->curzone->mfr2_rev[tone_index - DAHDI_TONE_MFR2_REV_BASE];
  3246. default:
  3247. return NULL;
  3248. }
  3249. }
  3250. static void __do_dtmf(struct dahdi_chan *chan)
  3251. {
  3252. char c;
  3253. /* Called with chan->lock held */
  3254. while ((c = chan->txdialbuf[0])) {
  3255. memmove(chan->txdialbuf, chan->txdialbuf + 1, sizeof(chan->txdialbuf) - 1);
  3256. switch (c) {
  3257. case 'T':
  3258. chan->digitmode = DIGIT_MODE_DTMF;
  3259. chan->tonep = 0;
  3260. break;
  3261. case 'M':
  3262. chan->digitmode = DIGIT_MODE_MFR1;
  3263. chan->tonep = 0;
  3264. break;
  3265. case 'O':
  3266. chan->digitmode = DIGIT_MODE_MFR2_FWD;
  3267. chan->tonep = 0;
  3268. break;
  3269. case 'R':
  3270. chan->digitmode = DIGIT_MODE_MFR2_REV;
  3271. chan->tonep = 0;
  3272. break;
  3273. case 'P':
  3274. chan->digitmode = DIGIT_MODE_PULSE;
  3275. chan->tonep = 0;
  3276. break;
  3277. default:
  3278. if ((c != 'W') && (chan->digitmode == DIGIT_MODE_PULSE)) {
  3279. if ((c >= '0') && (c <= '9') && (chan->txhooksig == DAHDI_TXSIG_OFFHOOK)) {
  3280. chan->pdialcount = (c == '0') ? 10 : c - '0';
  3281. dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_PULSEBREAK,
  3282. chan->pulsebreaktime);
  3283. return;
  3284. }
  3285. } else {
  3286. chan->curtone = dahdi_mf_tone(chan, c, chan->digitmode);
  3287. chan->tonep = 0;
  3288. if (chan->curtone) {
  3289. dahdi_init_tone_state(&chan->ts, chan->curtone);
  3290. return;
  3291. }
  3292. }
  3293. }
  3294. }
  3295. /* Notify userspace process if there is nothing left */
  3296. chan->dialing = 0;
  3297. __qevent(chan, DAHDI_EVENT_DIALCOMPLETE);
  3298. }
  3299. static int dahdi_release(struct inode *inode, struct file *file)
  3300. {
  3301. int unit = UNIT(file);
  3302. int res;
  3303. struct dahdi_chan *chan;
  3304. if (unit == DAHDI_CTL)
  3305. return dahdi_ctl_release(file);
  3306. if (unit == DAHDI_TIMER) {
  3307. return dahdi_timer_release(inode, file);
  3308. }
  3309. if (unit == DAHDI_TRANSCODE) {
  3310. /* We should not be here because the dahdi_transcode.ko module
  3311. * should have updated the file_operations for this file
  3312. * handle when the file was opened. */
  3313. WARN_ON(1);
  3314. return -EFAULT;
  3315. }
  3316. if (unit == DAHDI_CHANNEL) {
  3317. chan = file->private_data;
  3318. if (!chan)
  3319. return dahdi_chan_release(file);
  3320. else
  3321. return dahdi_specchan_release(file);
  3322. }
  3323. if (unit == DAHDI_PSEUDO) {
  3324. chan = file->private_data;
  3325. if (chan) {
  3326. res = dahdi_specchan_release(file);
  3327. dahdi_free_pseudo(chan);
  3328. } else {
  3329. module_printk(KERN_NOTICE, "Pseudo release and no private data??\n");
  3330. res = 0;
  3331. }
  3332. return res;
  3333. }
  3334. return dahdi_specchan_release(file);
  3335. }
  3336. /**
  3337. * dahdi_alarm_channel() - notify userspace channel is (not) in alarm
  3338. * @chan: the DAHDI channel
  3339. * @alarms: alarm bits set
  3340. *
  3341. * Notify userspace about a change in alarm status of this channel.
  3342. *
  3343. * Note that channel drivers should only use this function directly if
  3344. * they have a single port per channel. Whole-span alarms should be sent
  3345. * using dahdi_alarm_notify() .
  3346. *
  3347. * Does nothing if alarms on the channel have not changed. If they have,
  3348. * triggers sending either DAHDI_EVENT_NOALARM (if they were cleared) or
  3349. * DAHDI_EVENT_ALARM (otherwise).
  3350. *
  3351. * Currently it is only used by drivers of FXO ports to notify (with a
  3352. * red alarm) they have no battery current.
  3353. */
  3354. void dahdi_alarm_channel(struct dahdi_chan *chan, int alarms)
  3355. {
  3356. unsigned long flags;
  3357. spin_lock_irqsave(&chan->lock, flags);
  3358. if (chan->chan_alarms != alarms) {
  3359. chan->chan_alarms = alarms;
  3360. dahdi_qevent_nolock(chan, alarms ? DAHDI_EVENT_ALARM : DAHDI_EVENT_NOALARM);
  3361. }
  3362. spin_unlock_irqrestore(&chan->lock, flags);
  3363. }
  3364. struct dahdi_span *get_master_span(void)
  3365. {
  3366. return master_span;
  3367. }
  3368. void set_master_span(int spanno)
  3369. {
  3370. struct dahdi_span *s;
  3371. unsigned long flags;
  3372. struct dahdi_span *old_master;
  3373. spin_lock_irqsave(&chan_lock, flags);
  3374. old_master = master_span;
  3375. list_for_each_entry(s, &span_list, spans_node) {
  3376. if (spanno == s->spanno) {
  3377. master_span = s;
  3378. break;
  3379. }
  3380. }
  3381. spin_unlock_irqrestore(&chan_lock, flags);
  3382. if ((debug & DEBUG_MAIN) && (old_master != master_span))
  3383. module_printk(KERN_NOTICE, "Master span is set to %d (%s)\n",
  3384. master_span->spanno, master_span->name);
  3385. }
  3386. static void __dahdi_find_master_span(void)
  3387. {
  3388. struct dahdi_span *s;
  3389. unsigned long flags;
  3390. struct dahdi_span *old_master;
  3391. spin_lock_irqsave(&chan_lock, flags);
  3392. old_master = master_span;
  3393. list_for_each_entry(s, &span_list, spans_node) {
  3394. if (s->alarms && old_master)
  3395. continue;
  3396. if (dahdi_is_digital_span(s) &&
  3397. !test_bit(DAHDI_FLAGBIT_RUNNING, &s->flags) &&
  3398. old_master)
  3399. continue;
  3400. if (!can_provide_timing(s))
  3401. continue;
  3402. if (master_span == s)
  3403. continue;
  3404. master_span = s;
  3405. break;
  3406. }
  3407. spin_unlock_irqrestore(&chan_lock, flags);
  3408. if ((debug & DEBUG_MAIN) && (old_master != master_span))
  3409. module_printk(KERN_NOTICE, "Master changed to %s\n", s->name);
  3410. }
  3411. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  3412. static void _dahdi_find_master_span(void *work)
  3413. {
  3414. __dahdi_find_master_span();
  3415. }
  3416. static DECLARE_WORK(find_master_work, _dahdi_find_master_span, NULL);
  3417. #else
  3418. static void _dahdi_find_master_span(struct work_struct *work)
  3419. {
  3420. __dahdi_find_master_span();
  3421. }
  3422. static DECLARE_WORK(find_master_work, _dahdi_find_master_span);
  3423. #endif
  3424. static void dahdi_find_master_span(void)
  3425. {
  3426. schedule_work(&find_master_work);
  3427. }
  3428. void dahdi_alarm_notify(struct dahdi_span *span)
  3429. {
  3430. int x;
  3431. span->alarms &= ~DAHDI_ALARM_LOOPBACK;
  3432. /* Determine maint status */
  3433. if (span->maintstat || span->mainttimer)
  3434. span->alarms |= DAHDI_ALARM_LOOPBACK;
  3435. /* DON'T CHANGE THIS AGAIN. THIS WAS DONE FOR A REASON.
  3436. The expression (a != b) does *NOT* do the same thing
  3437. as ((!a) != (!b)) */
  3438. /* if change in general state */
  3439. if ((!span->alarms) != (!span->lastalarms)) {
  3440. span->lastalarms = span->alarms;
  3441. for (x = 0; x < span->channels; x++)
  3442. dahdi_alarm_channel(span->chans[x], span->alarms);
  3443. /* If we're going into or out of alarm we should try to find a
  3444. * new master_span that may be a better fit. */
  3445. dahdi_find_master_span();
  3446. /* Report more detailed alarms */
  3447. if (debug & DEBUG_MAIN) {
  3448. if (span->alarms & DAHDI_ALARM_LOS) {
  3449. module_printk(KERN_NOTICE,
  3450. "Span %d: Loss of signal\n",
  3451. span->spanno);
  3452. }
  3453. if (span->alarms & DAHDI_ALARM_LFA) {
  3454. module_printk(KERN_NOTICE,
  3455. "Span %d: Loss of Frame Alignment\n",
  3456. span->spanno);
  3457. }
  3458. if (span->alarms & DAHDI_ALARM_LMFA) {
  3459. module_printk(KERN_NOTICE,
  3460. "Span %d: Loss of Multi-Frame "\
  3461. "Alignment\n", span->spanno);
  3462. }
  3463. }
  3464. }
  3465. }
  3466. static long
  3467. dahdi_timer_unlocked_ioctl(struct file *file, unsigned int cmd,
  3468. unsigned long data)
  3469. {
  3470. int j;
  3471. unsigned long flags;
  3472. struct dahdi_timer *const timer = file->private_data;
  3473. switch(cmd) {
  3474. case DAHDI_TIMERCONFIG:
  3475. get_user(j, (int __user *)data);
  3476. if (j < 0)
  3477. j = 0;
  3478. spin_lock_irqsave(&timer->lock, flags);
  3479. if (timer->ms != j) {
  3480. if (j && list_empty(&timer->list)) {
  3481. /* The timer is being activated so add to the
  3482. * global timer list. */
  3483. spin_unlock(&timer->lock);
  3484. spin_lock(&dahdi_timer_lock);
  3485. spin_lock(&timer->lock);
  3486. timer->ms = timer->pos = j;
  3487. list_add(&timer->list, &dahdi_timers);
  3488. spin_unlock(&dahdi_timer_lock);
  3489. } else if (!j && !list_empty(&timer->list)) {
  3490. /* The timer is being disabled so we can remove
  3491. * from the global timer list. */
  3492. spin_unlock(&timer->lock);
  3493. spin_lock(&dahdi_timer_lock);
  3494. spin_lock(&timer->lock);
  3495. list_del_init(&timer->list);
  3496. timer->ms = timer->pos = j;
  3497. spin_unlock(&dahdi_timer_lock);
  3498. } else {
  3499. timer->ms = timer->pos = j;
  3500. }
  3501. }
  3502. spin_unlock_irqrestore(&timer->lock, flags);
  3503. break;
  3504. case DAHDI_TIMERACK:
  3505. get_user(j, (int __user *)data);
  3506. spin_lock_irqsave(&timer->lock, flags);
  3507. if ((j < 1) || (j > timer->tripped))
  3508. j = timer->tripped;
  3509. timer->tripped -= j;
  3510. spin_unlock_irqrestore(&timer->lock, flags);
  3511. break;
  3512. case DAHDI_GETEVENT: /* Get event on queue */
  3513. j = DAHDI_EVENT_NONE;
  3514. spin_lock_irqsave(&timer->lock, flags);
  3515. /* set up for no event */
  3516. if (timer->tripped)
  3517. j = DAHDI_EVENT_TIMER_EXPIRED;
  3518. if (timer->ping)
  3519. j = DAHDI_EVENT_TIMER_PING;
  3520. spin_unlock_irqrestore(&timer->lock, flags);
  3521. put_user(j, (int __user *)data);
  3522. break;
  3523. case DAHDI_TIMERPING:
  3524. spin_lock_irqsave(&timer->lock, flags);
  3525. timer->ping = 1;
  3526. wake_up_interruptible(&timer->sel);
  3527. spin_unlock_irqrestore(&timer->lock, flags);
  3528. break;
  3529. case DAHDI_TIMERPONG:
  3530. spin_lock_irqsave(&timer->lock, flags);
  3531. timer->ping = 0;
  3532. spin_unlock_irqrestore(&timer->lock, flags);
  3533. break;
  3534. default:
  3535. return -ENOTTY;
  3536. }
  3537. return 0;
  3538. }
  3539. #ifndef HAVE_UNLOCKED_IOCTL
  3540. static int dahdi_timer_ioctl(struct inode *inode, struct file *file,
  3541. unsigned int cmd, unsigned long data)
  3542. {
  3543. return dahdi_timer_unlocked_ioctl(file, cmd, data);
  3544. }
  3545. #endif
  3546. static int dahdi_ioctl_getgains(struct file *file, unsigned long data)
  3547. {
  3548. int res = 0;
  3549. struct dahdi_gains *gain;
  3550. int j;
  3551. void __user * const user_data = (void __user *)data;
  3552. struct dahdi_chan *chan;
  3553. gain = kzalloc(sizeof(*gain), GFP_KERNEL);
  3554. if (!gain)
  3555. return -ENOMEM;
  3556. if (copy_from_user(gain, user_data, sizeof(*gain))) {
  3557. res = -EFAULT;
  3558. goto cleanup;
  3559. }
  3560. chan = (!gain->chan) ? chan_from_file(file) :
  3561. chan_from_num(gain->chan);
  3562. if (!chan) {
  3563. res = -EINVAL;
  3564. goto cleanup;
  3565. }
  3566. if (!(chan->flags & DAHDI_FLAG_AUDIO)) {
  3567. res = -EINVAL;
  3568. goto cleanup;
  3569. }
  3570. gain->chan = chan->channo;
  3571. for (j = 0; j < 256; ++j) {
  3572. gain->txgain[j] = chan->txgain[j];
  3573. gain->rxgain[j] = chan->rxgain[j];
  3574. }
  3575. if (copy_to_user(user_data, gain, sizeof(*gain))) {
  3576. res = -EFAULT;
  3577. goto cleanup;
  3578. }
  3579. cleanup:
  3580. kfree(gain);
  3581. return res;
  3582. }
  3583. static int dahdi_ioctl_setgains(struct file *file, unsigned long data)
  3584. {
  3585. int res = 0;
  3586. struct dahdi_gains *gain;
  3587. unsigned char *txgain, *rxgain;
  3588. int j;
  3589. unsigned long flags;
  3590. const int GAIN_TABLE_SIZE = sizeof(defgain);
  3591. void __user * const user_data = (void __user *)data;
  3592. struct dahdi_chan *chan;
  3593. gain = kzalloc(sizeof(*gain), GFP_KERNEL);
  3594. if (!gain)
  3595. return -ENOMEM;
  3596. if (copy_from_user(gain, user_data, sizeof(*gain))) {
  3597. res = -EFAULT;
  3598. goto cleanup;
  3599. }
  3600. chan = (!gain->chan) ? chan_from_file(file) :
  3601. chan_from_num(gain->chan);
  3602. if (!chan) {
  3603. res = -EINVAL;
  3604. goto cleanup;
  3605. }
  3606. if (!(chan->flags & DAHDI_FLAG_AUDIO)) {
  3607. res = -EINVAL;
  3608. goto cleanup;
  3609. }
  3610. rxgain = kzalloc(GAIN_TABLE_SIZE*2, GFP_KERNEL);
  3611. if (!rxgain) {
  3612. res = -ENOMEM;
  3613. goto cleanup;
  3614. }
  3615. gain->chan = chan->channo;
  3616. txgain = rxgain + GAIN_TABLE_SIZE;
  3617. for (j = 0; j < GAIN_TABLE_SIZE; ++j) {
  3618. rxgain[j] = gain->rxgain[j];
  3619. txgain[j] = gain->txgain[j];
  3620. }
  3621. if (!memcmp(rxgain, defgain, GAIN_TABLE_SIZE) &&
  3622. !memcmp(txgain, defgain, GAIN_TABLE_SIZE)) {
  3623. kfree(rxgain);
  3624. spin_lock_irqsave(&chan->lock, flags);
  3625. if (is_gain_allocated(chan))
  3626. kfree(chan->rxgain);
  3627. chan->rxgain = defgain;
  3628. chan->txgain = defgain;
  3629. spin_unlock_irqrestore(&chan->lock, flags);
  3630. } else {
  3631. /* This is a custom gain setting */
  3632. spin_lock_irqsave(&chan->lock, flags);
  3633. if (is_gain_allocated(chan))
  3634. kfree(chan->rxgain);
  3635. chan->rxgain = rxgain;
  3636. chan->txgain = txgain;
  3637. spin_unlock_irqrestore(&chan->lock, flags);
  3638. }
  3639. if (copy_to_user(user_data, gain, sizeof(*gain))) {
  3640. res = -EFAULT;
  3641. goto cleanup;
  3642. }
  3643. cleanup:
  3644. kfree(gain);
  3645. return res;
  3646. }
  3647. static int dahdi_ioctl_chandiag(struct file *file, unsigned long data)
  3648. {
  3649. unsigned long flags;
  3650. int channo;
  3651. /* there really is no need to initialize this structure because when it is used it has
  3652. * already been completely overwritten, but apparently the compiler cannot figure that
  3653. * out and warns about uninitialized usage... so initialize it.
  3654. */
  3655. struct dahdi_echocan_state ec_state = { .ops = NULL, };
  3656. struct dahdi_chan *chan;
  3657. struct dahdi_chan *temp;
  3658. /* get channel number from user */
  3659. get_user(channo, (int __user *)data);
  3660. chan = chan_from_num(channo);
  3661. if (!chan)
  3662. return -EINVAL;
  3663. temp = kmalloc(sizeof(*chan), GFP_KERNEL);
  3664. if (!temp)
  3665. return -ENOMEM;
  3666. spin_lock_irqsave(&chan->lock, flags);
  3667. *temp = *chan;
  3668. if (temp->ec_state)
  3669. ec_state = *temp->ec_state;
  3670. if (temp->curzone)
  3671. tone_zone_get(temp->curzone);
  3672. spin_unlock_irqrestore(&chan->lock, flags);
  3673. module_printk(KERN_INFO, "Dump of DAHDI Channel %d (%s,%d,%d):\n\n",
  3674. channo, temp->name, temp->channo, temp->chanpos);
  3675. module_printk(KERN_INFO, "flags: %x hex, writechunk: %p, readchunk: %p\n",
  3676. (unsigned int) temp->flags, temp->writechunk, temp->readchunk);
  3677. module_printk(KERN_INFO, "rxgain: %p, txgain: %p, gainalloc: %d\n",
  3678. temp->rxgain, temp->txgain, is_gain_allocated(temp));
  3679. module_printk(KERN_INFO, "span: %p, sig: %x hex, sigcap: %x hex\n",
  3680. temp->span, temp->sig, temp->sigcap);
  3681. module_printk(KERN_INFO, "inreadbuf: %d, outreadbuf: %d, inwritebuf: %d, outwritebuf: %d\n",
  3682. temp->inreadbuf, temp->outreadbuf, temp->inwritebuf, temp->outwritebuf);
  3683. module_printk(KERN_INFO, "blocksize: %d, numbufs: %d, txbufpolicy: %d, txbufpolicy: %d\n",
  3684. temp->blocksize, temp->numbufs, temp->txbufpolicy,
  3685. DAHDI_POLICY_IMMEDIATE);
  3686. module_printk(KERN_INFO, "txdisable: %d, rxdisable: %d, iomask: %d\n",
  3687. temp->txdisable, 0, temp->iomask);
  3688. module_printk(KERN_INFO, "curzone: %p, tonezone: %d, curtone: %p, tonep: %d\n",
  3689. temp->curzone,
  3690. ((temp->curzone) ? temp->curzone->num : -1),
  3691. temp->curtone, temp->tonep);
  3692. module_printk(KERN_INFO, "digitmode: %d, txdialbuf: %s, dialing: %d, aftdialtimer: %d, cadpos. %d\n",
  3693. temp->digitmode, temp->txdialbuf, temp->dialing,
  3694. temp->afterdialingtimer, temp->cadencepos);
  3695. module_printk(KERN_INFO, "confna: %d, confn: %d, confmode: %d, confmute: %d\n",
  3696. temp->confna, temp->_confn, temp->confmode, temp->confmute);
  3697. module_printk(KERN_INFO, "ec: %p, deflaw: %d, xlaw: %p\n",
  3698. temp->ec_state, temp->deflaw, temp->xlaw);
  3699. if (temp->ec_state) {
  3700. module_printk(KERN_INFO, "echostate: %02x, echotimer: %d, echolastupdate: %d\n",
  3701. ec_state.status.mode, ec_state.status.pretrain_timer, ec_state.status.last_train_tap);
  3702. }
  3703. module_printk(KERN_INFO, "itimer: %d, otimer: %d, ringdebtimer: %d\n\n",
  3704. temp->itimer, temp->otimer, temp->ringdebtimer);
  3705. if (temp->curzone)
  3706. tone_zone_put(temp->curzone);
  3707. kfree(temp);
  3708. return 0;
  3709. }
  3710. /**
  3711. * dahdi_ioctl_getparams() - Get channel parameters.
  3712. *
  3713. */
  3714. static int dahdi_ioctl_getparams(struct file *file, unsigned long data)
  3715. {
  3716. size_t size_to_copy;
  3717. struct dahdi_params param;
  3718. bool return_master = false;
  3719. struct dahdi_chan *chan;
  3720. int j;
  3721. size_to_copy = sizeof(struct dahdi_params);
  3722. if (copy_from_user(&param, (void __user *)data, size_to_copy))
  3723. return -EFAULT;
  3724. /* check to see if the caller wants to receive our master channel
  3725. * number */
  3726. if (param.channo & DAHDI_GET_PARAMS_RETURN_MASTER) {
  3727. return_master = true;
  3728. param.channo &= ~DAHDI_GET_PARAMS_RETURN_MASTER;
  3729. }
  3730. /* Pick the right channo's */
  3731. chan = chan_from_file(file);
  3732. if (!chan)
  3733. chan = chan_from_num(param.channo);
  3734. if (!chan)
  3735. return -EINVAL;
  3736. /* point to relevant structure */
  3737. param.sigtype = chan->sig; /* get signalling type */
  3738. /* return non-zero if rx not in idle state */
  3739. if (chan->span) {
  3740. j = dahdi_q_sig(chan);
  3741. if (j >= 0) { /* if returned with success */
  3742. param.rxisoffhook = ((chan->rxsig & (j >> 8)) !=
  3743. (j & 0xff));
  3744. } else {
  3745. const int sig = chan->rxhooksig;
  3746. param.rxisoffhook = ((sig != DAHDI_RXSIG_ONHOOK) &&
  3747. (sig != DAHDI_RXSIG_INITIAL));
  3748. }
  3749. } else if ((chan->txstate == DAHDI_TXSTATE_KEWL) ||
  3750. (chan->txstate == DAHDI_TXSTATE_AFTERKEWL)) {
  3751. param.rxisoffhook = 1;
  3752. } else {
  3753. param.rxisoffhook = 0;
  3754. }
  3755. if (chan->span &&
  3756. chan->span->ops->rbsbits && !(chan->sig & DAHDI_SIG_CLEAR)) {
  3757. param.rxbits = chan->rxsig;
  3758. param.txbits = chan->txsig;
  3759. param.idlebits = chan->idlebits;
  3760. } else {
  3761. param.rxbits = -1;
  3762. param.txbits = -1;
  3763. param.idlebits = 0;
  3764. }
  3765. if (chan->span &&
  3766. (chan->span->ops->rbsbits || chan->span->ops->hooksig) &&
  3767. !(chan->sig & DAHDI_SIG_CLEAR)) {
  3768. param.rxhooksig = chan->rxhooksig;
  3769. param.txhooksig = chan->txhooksig;
  3770. } else {
  3771. param.rxhooksig = -1;
  3772. param.txhooksig = -1;
  3773. }
  3774. param.prewinktime = chan->prewinktime;
  3775. param.preflashtime = chan->preflashtime;
  3776. param.winktime = chan->winktime;
  3777. param.flashtime = chan->flashtime;
  3778. param.starttime = chan->starttime;
  3779. param.rxwinktime = chan->rxwinktime;
  3780. param.rxflashtime = chan->rxflashtime;
  3781. param.debouncetime = chan->debouncetime;
  3782. param.channo = chan->channo;
  3783. param.chan_alarms = chan->chan_alarms;
  3784. /* if requested, put the master channel number in the top 16 bits of
  3785. * the result */
  3786. if (return_master)
  3787. param.channo |= chan->master->channo << 16;
  3788. param.pulsemaketime = chan->pulsemaketime;
  3789. param.pulsebreaktime = chan->pulsebreaktime;
  3790. param.pulseaftertime = chan->pulseaftertime;
  3791. param.spanno = (chan->span) ? chan->span->spanno : 0;
  3792. strlcpy(param.name, chan->name, sizeof(param.name));
  3793. param.chanpos = chan->chanpos;
  3794. param.sigcap = chan->sigcap;
  3795. /* Return current law */
  3796. if (chan->xlaw == __dahdi_alaw)
  3797. param.curlaw = DAHDI_LAW_ALAW;
  3798. else
  3799. param.curlaw = DAHDI_LAW_MULAW;
  3800. if (copy_to_user((void __user *)data, &param, size_to_copy))
  3801. return -EFAULT;
  3802. return 0;
  3803. }
  3804. /**
  3805. * dahdi_ioctl_setparams() - Set channel parameters.
  3806. *
  3807. */
  3808. static int dahdi_ioctl_setparams(struct file *file, unsigned long data)
  3809. {
  3810. struct dahdi_params param;
  3811. struct dahdi_chan *chan;
  3812. if (copy_from_user(&param, (void __user *)data, sizeof(param)))
  3813. return -EFAULT;
  3814. param.chan_alarms = 0; /* be explicit about the above */
  3815. /* Pick the right channo's */
  3816. chan = chan_from_file(file);
  3817. if (!chan)
  3818. chan = chan_from_num(param.channo);
  3819. if (!chan)
  3820. return -EINVAL;
  3821. /* point to relevant structure */
  3822. /* NOTE: sigtype is *not* included in this */
  3823. /* get timing paramters */
  3824. chan->prewinktime = param.prewinktime;
  3825. chan->preflashtime = param.preflashtime;
  3826. chan->winktime = param.winktime;
  3827. chan->flashtime = param.flashtime;
  3828. chan->starttime = param.starttime;
  3829. /* Update ringtime if not using a tone zone */
  3830. if (!chan->curzone)
  3831. chan->ringcadence[0] = chan->starttime;
  3832. chan->rxwinktime = param.rxwinktime;
  3833. chan->rxflashtime = param.rxflashtime;
  3834. chan->debouncetime = param.debouncetime;
  3835. chan->pulsemaketime = param.pulsemaketime;
  3836. chan->pulsebreaktime = param.pulsebreaktime;
  3837. chan->pulseaftertime = param.pulseaftertime;
  3838. return 0;
  3839. }
  3840. /**
  3841. * dahdi_ioctl_spanstat() - Return statistics for a span.
  3842. *
  3843. */
  3844. static int dahdi_ioctl_spanstat(struct file *file, unsigned long data)
  3845. {
  3846. int ret = 0;
  3847. struct dahdi_spaninfo spaninfo;
  3848. struct dahdi_span *s;
  3849. int j;
  3850. size_t size_to_copy;
  3851. bool via_chan = false;
  3852. size_to_copy = sizeof(struct dahdi_spaninfo);
  3853. if (copy_from_user(&spaninfo, (void __user *)data, size_to_copy))
  3854. return -EFAULT;
  3855. if (!spaninfo.spanno) {
  3856. struct dahdi_chan *const chan = chan_from_file(file);
  3857. if (!chan)
  3858. return -EINVAL;
  3859. s = chan->span;
  3860. via_chan = true;
  3861. } else {
  3862. s = span_find_and_get(spaninfo.spanno);
  3863. }
  3864. if (!s)
  3865. return -EINVAL;
  3866. spaninfo.spanno = s->spanno; /* put the span # in here */
  3867. spaninfo.totalspans = span_count();
  3868. strlcpy(spaninfo.desc, s->desc, sizeof(spaninfo.desc));
  3869. strlcpy(spaninfo.name, s->name, sizeof(spaninfo.name));
  3870. spaninfo.alarms = s->alarms; /* get alarm status */
  3871. spaninfo.rxlevel = s->rxlevel; /* get rx level */
  3872. spaninfo.txlevel = s->txlevel; /* get tx level */
  3873. spaninfo.bpvcount = s->count.bpv;
  3874. spaninfo.crc4count = s->count.crc4;
  3875. spaninfo.ebitcount = s->count.ebit;
  3876. spaninfo.fascount = s->count.fas;
  3877. spaninfo.fecount = s->count.fe;
  3878. spaninfo.cvcount = s->count.cv;
  3879. spaninfo.becount = s->count.be;
  3880. spaninfo.prbs = s->count.prbs;
  3881. spaninfo.errsec = s->count.errsec;
  3882. spaninfo.irqmisses = s->parent->irqmisses; /* get IRQ miss count */
  3883. spaninfo.syncsrc = s->syncsrc; /* get active sync source */
  3884. spaninfo.totalchans = s->channels;
  3885. spaninfo.numchans = 0;
  3886. for (j = 0; j < s->channels; j++) {
  3887. if (s->chans[j]->sig)
  3888. spaninfo.numchans++;
  3889. }
  3890. spaninfo.lbo = s->lbo;
  3891. spaninfo.lineconfig = s->lineconfig;
  3892. spaninfo.irq = 0;
  3893. spaninfo.linecompat = s->linecompat;
  3894. strlcpy(spaninfo.lboname, dahdi_lboname(s->lbo),
  3895. sizeof(spaninfo.lboname));
  3896. if (s->parent->manufacturer) {
  3897. strlcpy(spaninfo.manufacturer, s->parent->manufacturer,
  3898. sizeof(spaninfo.manufacturer));
  3899. }
  3900. if (s->parent->devicetype) {
  3901. strlcpy(spaninfo.devicetype, s->parent->devicetype,
  3902. sizeof(spaninfo.devicetype));
  3903. }
  3904. if (s->parent->location) {
  3905. strlcpy(spaninfo.location, s->parent->location,
  3906. sizeof(spaninfo.location));
  3907. }
  3908. if (s->spantype) {
  3909. /*
  3910. * The API is brain-damaged, returning fixed length
  3911. * null terminated strings via ioctl() is...
  3912. *
  3913. * This field contain only 6 characters
  3914. * (including null termination, 5 effective characters).
  3915. *
  3916. * For backward compatibility, massage this info for dahdi-scan
  3917. * and friends, until:
  3918. * - They either learn to read the info from sysfs
  3919. * - Or this API is broken to return the enum value
  3920. */
  3921. const char *st = dahdi_spantype2str(s->spantype);
  3922. switch (s->spantype) {
  3923. case SPANTYPE_DIGITAL_BRI_NT:
  3924. strlcpy(spaninfo.spantype, "NT",
  3925. sizeof(spaninfo.spantype));
  3926. break;
  3927. case SPANTYPE_DIGITAL_BRI_TE:
  3928. strlcpy(spaninfo.spantype, "TE",
  3929. sizeof(spaninfo.spantype));
  3930. break;
  3931. default:
  3932. /*
  3933. * The rest are either short (FXS, FXO, E1, T1, J1)
  3934. * Or new (BRI_SOFT, ANALOG_MIXED, INVALID),
  3935. * so no backward compatibility for this
  3936. * broken interface.
  3937. */
  3938. strlcpy(spaninfo.spantype, st,
  3939. sizeof(spaninfo.spantype));
  3940. break;
  3941. }
  3942. }
  3943. if (copy_to_user((void __user *)data, &spaninfo, size_to_copy))
  3944. ret = -EFAULT;
  3945. if (!via_chan)
  3946. put_span(s);
  3947. return ret;
  3948. }
  3949. /**
  3950. * dahdi_ioctl_spanstat_v1() - Return statistics for a span in a legacy format.
  3951. *
  3952. */
  3953. static int dahdi_ioctl_spanstat_v1(struct file *file, unsigned long data)
  3954. {
  3955. int ret = 0;
  3956. struct dahdi_spaninfo_v1 spaninfo_v1;
  3957. struct dahdi_span *s;
  3958. int j;
  3959. bool via_chan = false;
  3960. if (copy_from_user(&spaninfo_v1, (void __user *)data,
  3961. sizeof(spaninfo_v1))) {
  3962. return -EFAULT;
  3963. }
  3964. if (!spaninfo_v1.spanno) {
  3965. struct dahdi_chan *const chan = chan_from_file(file);
  3966. if (!chan)
  3967. return -EINVAL;
  3968. s = chan->span;
  3969. via_chan = true;
  3970. } else {
  3971. s = span_find_and_get(spaninfo_v1.spanno);
  3972. }
  3973. if (!s)
  3974. return -EINVAL;
  3975. spaninfo_v1.spanno = s->spanno; /* put the span # in here */
  3976. spaninfo_v1.totalspans = 0;
  3977. spaninfo_v1.totalspans = span_count();
  3978. strlcpy(spaninfo_v1.desc,
  3979. s->desc,
  3980. sizeof(spaninfo_v1.desc));
  3981. strlcpy(spaninfo_v1.name,
  3982. s->name,
  3983. sizeof(spaninfo_v1.name));
  3984. spaninfo_v1.alarms = s->alarms;
  3985. spaninfo_v1.bpvcount = s->count.bpv;
  3986. spaninfo_v1.rxlevel = s->rxlevel;
  3987. spaninfo_v1.txlevel = s->txlevel;
  3988. spaninfo_v1.crc4count = s->count.crc4;
  3989. spaninfo_v1.ebitcount = s->count.ebit;
  3990. spaninfo_v1.fascount = s->count.fas;
  3991. spaninfo_v1.irqmisses = s->parent->irqmisses;
  3992. spaninfo_v1.syncsrc = s->syncsrc;
  3993. spaninfo_v1.totalchans = s->channels;
  3994. spaninfo_v1.numchans = 0;
  3995. for (j = 0; j < s->channels; j++) {
  3996. if (s->chans[j]->sig)
  3997. spaninfo_v1.numchans++;
  3998. }
  3999. spaninfo_v1.lbo = s->lbo;
  4000. spaninfo_v1.lineconfig = s->lineconfig;
  4001. spaninfo_v1.irq = 0;
  4002. spaninfo_v1.linecompat = s->linecompat;
  4003. strlcpy(spaninfo_v1.lboname,
  4004. dahdi_lboname(s->lbo),
  4005. sizeof(spaninfo_v1.lboname));
  4006. if (s->parent->manufacturer) {
  4007. strlcpy(spaninfo_v1.manufacturer, s->parent->manufacturer,
  4008. sizeof(spaninfo_v1.manufacturer));
  4009. }
  4010. if (s->parent->devicetype) {
  4011. strlcpy(spaninfo_v1.devicetype, s->parent->devicetype,
  4012. sizeof(spaninfo_v1.devicetype));
  4013. }
  4014. strlcpy(spaninfo_v1.location, s->parent->location,
  4015. sizeof(spaninfo_v1.location));
  4016. if (s->spantype) {
  4017. strlcpy(spaninfo_v1.spantype,
  4018. dahdi_spantype2str(s->spantype),
  4019. sizeof(spaninfo_v1.spantype));
  4020. }
  4021. if (copy_to_user((void __user *)data, &spaninfo_v1,
  4022. sizeof(spaninfo_v1))) {
  4023. ret = -EFAULT;
  4024. }
  4025. if (!via_chan)
  4026. put_span(s);
  4027. return ret;
  4028. }
  4029. #ifdef CONFIG_DAHDI_CONFLINK
  4030. static int dahdi_ioctl_conflink(struct file *file, unsigned long data)
  4031. {
  4032. struct dahdi_chan *chan;
  4033. struct dahdi_confinfo conf;
  4034. unsigned long flags;
  4035. int res = 0;
  4036. int i;
  4037. chan = chan_from_file(file);
  4038. if (!chan)
  4039. return -EINVAL;
  4040. if (!(chan->flags & DAHDI_FLAG_AUDIO))
  4041. return -EINVAL;
  4042. if (copy_from_user(&conf, (void __user *)data, sizeof(conf)))
  4043. return -EFAULT;
  4044. /* check sanity of arguments */
  4045. if ((conf.chan < 0) || (conf.chan > DAHDI_MAX_CONF))
  4046. return -EINVAL;
  4047. if ((conf.confno < 0) || (conf.confno > DAHDI_MAX_CONF))
  4048. return -EINVAL;
  4049. /* cant listen to self!! */
  4050. if (conf.chan && (conf.chan == conf.confno))
  4051. return -EINVAL;
  4052. spin_lock_irqsave(&chan_lock, flags);
  4053. spin_lock(&chan->lock);
  4054. /* if to clear all links */
  4055. if ((!conf.chan) && (!conf.confno)) {
  4056. /* clear all the links */
  4057. memset(conf_links, 0, sizeof(conf_links));
  4058. recalc_maxlinks();
  4059. spin_unlock(&chan->lock);
  4060. spin_unlock_irqrestore(&chan_lock, flags);
  4061. return 0;
  4062. }
  4063. /* look for already existant specified combination */
  4064. for (i = 1; i <= DAHDI_MAX_CONF; i++) {
  4065. /* if found, exit */
  4066. if ((conf_links[i].src == conf.chan) &&
  4067. (conf_links[i].dst == conf.confno))
  4068. break;
  4069. }
  4070. if (i <= DAHDI_MAX_CONF) { /* if found */
  4071. if (!conf.confmode) { /* if to remove link */
  4072. conf_links[i].src = 0;
  4073. conf_links[i].dst = 0;
  4074. } else { /* if to add and already there, error */
  4075. res = -EEXIST;
  4076. }
  4077. } else { /* if not found */
  4078. if (conf.confmode) { /* if to add link */
  4079. /* look for empty location */
  4080. for (i = 1; i <= DAHDI_MAX_CONF; i++) {
  4081. /* if empty, exit loop */
  4082. if ((!conf_links[i].src) &&
  4083. (!conf_links[i].dst))
  4084. break;
  4085. }
  4086. /* if empty spot found */
  4087. if (i <= DAHDI_MAX_CONF) {
  4088. conf_links[i].src = conf.chan;
  4089. conf_links[i].dst = conf.confno;
  4090. } else { /* if no empties -- error */
  4091. res = -ENOSPC;
  4092. }
  4093. } else { /* if to remove, and not found -- error */
  4094. res = -ENOENT;
  4095. }
  4096. }
  4097. recalc_maxlinks();
  4098. spin_unlock(&chan->lock);
  4099. spin_unlock_irqrestore(&chan_lock, flags);
  4100. return res;
  4101. }
  4102. #else
  4103. static int dahdi_ioctl_conflink(struct file *file, unsigned long data)
  4104. {
  4105. return -ENOSYS;
  4106. }
  4107. #endif
  4108. static int dahdi_common_ioctl(struct file *file, unsigned int cmd,
  4109. unsigned long data)
  4110. {
  4111. switch (cmd) {
  4112. /* get channel parameters */
  4113. case DAHDI_GET_PARAMS_V1: /* Intentional drop through. */
  4114. case DAHDI_GET_PARAMS:
  4115. return dahdi_ioctl_getparams(file, data);
  4116. case DAHDI_SET_PARAMS:
  4117. return dahdi_ioctl_setparams(file, data);
  4118. case DAHDI_GETGAINS_V1: /* Intentional drop through. */
  4119. case DAHDI_GETGAINS: /* get gain stuff */
  4120. return dahdi_ioctl_getgains(file, data);
  4121. case DAHDI_SETGAINS: /* set gain stuff */
  4122. return dahdi_ioctl_setgains(file, data);
  4123. case DAHDI_SPANSTAT:
  4124. return dahdi_ioctl_spanstat(file, data);
  4125. case DAHDI_SPANSTAT_V1:
  4126. return dahdi_ioctl_spanstat_v1(file, data);
  4127. case DAHDI_CHANDIAG_V1: /* Intentional drop through. */
  4128. case DAHDI_CHANDIAG:
  4129. return dahdi_ioctl_chandiag(file, data);
  4130. case DAHDI_CONFLINK:
  4131. return dahdi_ioctl_conflink(file, data);
  4132. default:
  4133. return -ENOTTY;
  4134. }
  4135. return 0;
  4136. }
  4137. static const struct dahdi_dynamic_ops *dahdi_dynamic_ops;
  4138. void dahdi_set_dynamic_ops(const struct dahdi_dynamic_ops *ops)
  4139. {
  4140. mutex_lock(&registration_mutex);
  4141. dahdi_dynamic_ops = ops;
  4142. mutex_unlock(&registration_mutex);
  4143. }
  4144. EXPORT_SYMBOL(dahdi_set_dynamic_ops);
  4145. static int (*dahdi_hpec_ioctl)(unsigned int cmd, unsigned long data);
  4146. void dahdi_set_hpec_ioctl(int (*func)(unsigned int cmd, unsigned long data))
  4147. {
  4148. dahdi_hpec_ioctl = func;
  4149. }
  4150. static void recalc_slaves(struct dahdi_chan *chan)
  4151. {
  4152. int x;
  4153. struct dahdi_chan *last = chan;
  4154. /* Makes no sense if you don't have a span */
  4155. if (!chan->span)
  4156. return;
  4157. #ifdef CONFIG_DAHDI_DEBUG
  4158. module_printk(KERN_NOTICE, "Recalculating slaves on %s\n", chan->name);
  4159. #endif
  4160. /* Link all slaves appropriately */
  4161. for (x=chan->chanpos;x<chan->span->channels;x++)
  4162. if (chan->span->chans[x]->master == chan) {
  4163. #ifdef CONFIG_DAHDI_DEBUG
  4164. module_printk(KERN_NOTICE, "Channel %s, slave to %s, last is %s, its next will be %d\n",
  4165. chan->span->chans[x]->name, chan->name, last->name, x);
  4166. #endif
  4167. last->nextslave = chan->span->chans[x];
  4168. last = last->nextslave;
  4169. }
  4170. /* Terminate list */
  4171. last->nextslave = NULL;
  4172. #ifdef CONFIG_DAHDI_DEBUG
  4173. module_printk(KERN_NOTICE, "Done Recalculating slaves on %s (last is %s)\n", chan->name, last->name);
  4174. #endif
  4175. }
  4176. #if defined(CONFIG_DAHDI_NET) && defined(HAVE_NET_DEVICE_OPS)
  4177. static const struct net_device_ops dahdi_netdev_ops = {
  4178. .ndo_open = dahdi_net_open,
  4179. .ndo_stop = dahdi_net_stop,
  4180. .ndo_do_ioctl = dahdi_net_ioctl,
  4181. .ndo_start_xmit = dahdi_xmit,
  4182. };
  4183. #endif
  4184. static int dahdi_ioctl_chanconfig(struct file *file, unsigned long data)
  4185. {
  4186. int res = 0;
  4187. int y;
  4188. struct dahdi_chanconfig ch;
  4189. struct dahdi_chan *newmaster;
  4190. struct dahdi_chan *chan;
  4191. struct dahdi_chan *dacs_chan = NULL;
  4192. unsigned long flags;
  4193. int sigcap;
  4194. if (copy_from_user(&ch, (void __user *)data, sizeof(ch)))
  4195. return -EFAULT;
  4196. chan = chan_from_num(ch.chan);
  4197. if (!chan) {
  4198. printk(KERN_NOTICE "%s: No channel for number %d\n",
  4199. __func__, ch.chan);
  4200. return -EINVAL;
  4201. }
  4202. if (ch.sigtype == DAHDI_SIG_SLAVE) {
  4203. newmaster = chan_from_num(ch.master);
  4204. if (!newmaster) {
  4205. chan_notice(chan, "%s: slave channel without master.\n",
  4206. __func__);
  4207. return -EINVAL;
  4208. }
  4209. ch.sigtype = newmaster->sig;
  4210. } else if ((ch.sigtype & __DAHDI_SIG_DACS) == __DAHDI_SIG_DACS) {
  4211. newmaster = chan;
  4212. dacs_chan = chan_from_num(ch.idlebits);
  4213. if (!dacs_chan) {
  4214. chan_notice(chan, "%s: dacs channel not found: %d.\n",
  4215. __func__, ch.idlebits);
  4216. return -EINVAL;
  4217. }
  4218. } else {
  4219. newmaster = chan;
  4220. }
  4221. spin_lock_irqsave(&chan->lock, flags);
  4222. #ifdef CONFIG_DAHDI_NET
  4223. if (dahdi_have_netdev(chan)) {
  4224. if (chan_to_netdev(chan)->flags & IFF_UP) {
  4225. spin_unlock_irqrestore(&chan->lock, flags);
  4226. module_printk(KERN_WARNING, "Can't switch HDLC net mode on channel %s, since current interface is up\n", chan->name);
  4227. return -EBUSY;
  4228. }
  4229. spin_unlock_irqrestore(&chan->lock, flags);
  4230. unregister_hdlc_device(chan->hdlcnetdev->netdev);
  4231. spin_lock_irqsave(&chan->lock, flags);
  4232. free_netdev(chan->hdlcnetdev->netdev);
  4233. kfree(chan->hdlcnetdev);
  4234. chan->hdlcnetdev = NULL;
  4235. clear_bit(DAHDI_FLAGBIT_NETDEV, &chan->flags);
  4236. }
  4237. #else
  4238. if (ch.sigtype == DAHDI_SIG_HDLCNET) {
  4239. spin_unlock_irqrestore(&chan->lock, flags);
  4240. module_printk(KERN_WARNING, "DAHDI networking not supported by this build.\n");
  4241. return -ENOSYS;
  4242. }
  4243. #endif
  4244. sigcap = chan->sigcap;
  4245. /* If they support clear channel, then they support the HDLC and such through
  4246. us. */
  4247. if (sigcap & DAHDI_SIG_CLEAR)
  4248. sigcap |= (DAHDI_SIG_HDLCRAW | DAHDI_SIG_HDLCFCS | DAHDI_SIG_HDLCNET | DAHDI_SIG_DACS);
  4249. if ((sigcap & ch.sigtype) != ch.sigtype) {
  4250. if (debug) {
  4251. chan_notice(chan, "%s: bad sigtype. sigcap: %x, sigtype: %x.\n",
  4252. __func__, sigcap, ch.sigtype);
  4253. }
  4254. res = -EINVAL;
  4255. }
  4256. if (chan->master != chan) {
  4257. struct dahdi_chan *oldmaster = chan->master;
  4258. /* Clear the master channel */
  4259. chan->master = chan;
  4260. chan->nextslave = NULL;
  4261. /* Unlink this channel from the master's channel list */
  4262. recalc_slaves(oldmaster);
  4263. }
  4264. if (!res) {
  4265. chan->sig = ch.sigtype;
  4266. if (chan->sig == DAHDI_SIG_CAS)
  4267. chan->idlebits = ch.idlebits;
  4268. else
  4269. chan->idlebits = 0;
  4270. if ((ch.sigtype & DAHDI_SIG_CLEAR) == DAHDI_SIG_CLEAR) {
  4271. /* Set clear channel flag if appropriate */
  4272. chan->flags &= ~DAHDI_FLAG_AUDIO;
  4273. chan->flags |= DAHDI_FLAG_CLEAR;
  4274. } else {
  4275. /* Set audio flag and not clear channel otherwise */
  4276. chan->flags |= DAHDI_FLAG_AUDIO;
  4277. chan->flags &= ~DAHDI_FLAG_CLEAR;
  4278. }
  4279. if ((ch.sigtype & DAHDI_SIG_HDLCRAW) == DAHDI_SIG_HDLCRAW) {
  4280. /* Set the HDLC flag */
  4281. chan->flags |= DAHDI_FLAG_HDLC;
  4282. } else {
  4283. /* Clear the HDLC flag */
  4284. chan->flags &= ~DAHDI_FLAG_HDLC;
  4285. }
  4286. if ((ch.sigtype & DAHDI_SIG_HDLCFCS) == DAHDI_SIG_HDLCFCS) {
  4287. /* Set FCS to be calculated if appropriate */
  4288. chan->flags |= DAHDI_FLAG_FCS;
  4289. } else {
  4290. /* Clear FCS flag */
  4291. chan->flags &= ~DAHDI_FLAG_FCS;
  4292. }
  4293. if ((ch.sigtype & __DAHDI_SIG_DACS) == __DAHDI_SIG_DACS) {
  4294. if (unlikely(!dacs_chan)) {
  4295. spin_unlock_irqrestore(&chan->lock, flags);
  4296. chan_notice(chan, "%s: dacs but no dacs_chan\n",
  4297. __func__);
  4298. return -EINVAL;
  4299. }
  4300. /* Setup conference properly */
  4301. chan->confmode = DAHDI_CONF_DIGITALMON;
  4302. chan->confna = ch.idlebits;
  4303. chan->dacs_chan = dacs_chan;
  4304. res = dahdi_chan_dacs(chan, dacs_chan);
  4305. } else {
  4306. dahdi_disable_dacs(chan);
  4307. }
  4308. chan->master = newmaster;
  4309. /* Note new slave if we are not our own master */
  4310. if (newmaster != chan) {
  4311. recalc_slaves(chan->master);
  4312. }
  4313. if ((ch.sigtype & DAHDI_SIG_HARDHDLC) == DAHDI_SIG_HARDHDLC) {
  4314. chan->flags &= ~DAHDI_FLAG_FCS;
  4315. chan->flags &= ~DAHDI_FLAG_HDLC;
  4316. chan->flags |= DAHDI_FLAG_NOSTDTXRX;
  4317. } else {
  4318. chan->flags &= ~DAHDI_FLAG_NOSTDTXRX;
  4319. }
  4320. if ((ch.sigtype & DAHDI_SIG_MTP2) == DAHDI_SIG_MTP2)
  4321. chan->flags |= DAHDI_FLAG_MTP2;
  4322. else
  4323. chan->flags &= ~DAHDI_FLAG_MTP2;
  4324. }
  4325. /* Chanconfig can block, do not call through the function pointer with
  4326. * the channel lock held. */
  4327. spin_unlock_irqrestore(&chan->lock, flags);
  4328. if (!res && chan->span->ops->chanconfig)
  4329. res = chan->span->ops->chanconfig(file, chan, ch.sigtype);
  4330. spin_lock_irqsave(&chan->lock, flags);
  4331. #ifdef CONFIG_DAHDI_NET
  4332. if (!res &&
  4333. (newmaster == chan) &&
  4334. (chan->sig == DAHDI_SIG_HDLCNET)) {
  4335. chan->hdlcnetdev = dahdi_hdlc_alloc();
  4336. if (chan->hdlcnetdev) {
  4337. /* struct hdlc_device *hdlc = chan->hdlcnetdev;
  4338. struct net_device *d = hdlc_to_dev(hdlc); mmm...get it right later --byg */
  4339. chan->hdlcnetdev->netdev = alloc_hdlcdev(chan->hdlcnetdev);
  4340. if (chan->hdlcnetdev->netdev) {
  4341. chan->hdlcnetdev->chan = chan;
  4342. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 23)
  4343. SET_MODULE_OWNER(chan->hdlcnetdev->netdev);
  4344. #endif
  4345. chan->hdlcnetdev->netdev->tx_queue_len = 50;
  4346. #ifdef HAVE_NET_DEVICE_OPS
  4347. chan->hdlcnetdev->netdev->netdev_ops = &dahdi_netdev_ops;
  4348. #else
  4349. chan->hdlcnetdev->netdev->do_ioctl = dahdi_net_ioctl;
  4350. chan->hdlcnetdev->netdev->open = dahdi_net_open;
  4351. chan->hdlcnetdev->netdev->stop = dahdi_net_stop;
  4352. #endif
  4353. dev_to_hdlc(chan->hdlcnetdev->netdev)->attach = dahdi_net_attach;
  4354. dev_to_hdlc(chan->hdlcnetdev->netdev)->xmit = dahdi_xmit;
  4355. spin_unlock_irqrestore(&chan->lock, flags);
  4356. /* Briefly restore interrupts while we register the device */
  4357. res = dahdi_register_hdlc_device(chan->hdlcnetdev->netdev, ch.netdev_name);
  4358. spin_lock_irqsave(&chan->lock, flags);
  4359. } else {
  4360. module_printk(KERN_NOTICE, "Unable to allocate hdlc: *shrug*\n");
  4361. res = -1;
  4362. }
  4363. if (!res)
  4364. set_bit(DAHDI_FLAGBIT_NETDEV, &chan->flags);
  4365. } else {
  4366. module_printk(KERN_NOTICE, "Unable to allocate netdev: out of memory\n");
  4367. res = -1;
  4368. }
  4369. }
  4370. #endif
  4371. if ((chan->sig == DAHDI_SIG_HDLCNET) &&
  4372. (chan == newmaster) &&
  4373. !dahdi_have_netdev(chan))
  4374. module_printk(KERN_NOTICE, "Unable to register HDLC device for channel %s\n", chan->name);
  4375. if (!res) {
  4376. /* Setup default law */
  4377. chan->deflaw = ch.deflaw;
  4378. /* Copy back any modified settings */
  4379. spin_unlock_irqrestore(&chan->lock, flags);
  4380. if (copy_to_user((void __user *)data, &ch, sizeof(ch)))
  4381. return -EFAULT;
  4382. spin_lock_irqsave(&chan->lock, flags);
  4383. /* And hangup */
  4384. dahdi_hangup(chan);
  4385. y = dahdi_q_sig(chan) & 0xff;
  4386. if (y >= 0)
  4387. chan->rxsig = (unsigned char) y;
  4388. chan->rxhooksig = DAHDI_RXSIG_INITIAL;
  4389. }
  4390. #ifdef CONFIG_DAHDI_DEBUG
  4391. module_printk(KERN_NOTICE, "Configured channel %s, flags %04lx, sig %04x\n", chan->name, chan->flags, chan->sig);
  4392. #endif
  4393. spin_unlock_irqrestore(&chan->lock, flags);
  4394. return res;
  4395. }
  4396. /**
  4397. * dahdi_ioctl_set_dialparms - Set the global dial parameters.
  4398. * @data: Pointer to user space that contains dahdi_dialparams.
  4399. */
  4400. static int dahdi_ioctl_set_dialparams(unsigned long data)
  4401. {
  4402. unsigned int i;
  4403. struct dahdi_dialparams tdp;
  4404. struct dahdi_zone *z;
  4405. struct dahdi_dialparams *const gdp = &global_dialparams;
  4406. if (copy_from_user(&tdp, (void __user *)data, sizeof(tdp)))
  4407. return -EFAULT;
  4408. mutex_lock(&global_dialparamslock);
  4409. if ((tdp.dtmf_tonelen >= 10) && (tdp.dtmf_tonelen <= 4000))
  4410. gdp->dtmf_tonelen = tdp.dtmf_tonelen;
  4411. if ((tdp.mfv1_tonelen >= 10) && (tdp.mfv1_tonelen <= 4000))
  4412. gdp->mfv1_tonelen = tdp.mfv1_tonelen;
  4413. if ((tdp.mfr2_tonelen >= 10) && (tdp.mfr2_tonelen <= 4000))
  4414. gdp->mfr2_tonelen = tdp.mfr2_tonelen;
  4415. /* update the lengths in all currently loaded zones */
  4416. spin_lock(&zone_lock);
  4417. list_for_each_entry(z, &tone_zones, node) {
  4418. for (i = 0; i < ARRAY_SIZE(z->dtmf); i++) {
  4419. z->dtmf[i].tonesamples =
  4420. gdp->dtmf_tonelen * DAHDI_CHUNKSIZE;
  4421. }
  4422. /* for MFR1, we only adjust the length of the digits */
  4423. for (i = DAHDI_TONE_MFR1_0; i <= DAHDI_TONE_MFR1_9; i++) {
  4424. z->mfr1[i - DAHDI_TONE_MFR1_BASE].tonesamples =
  4425. gdp->mfv1_tonelen * DAHDI_CHUNKSIZE;
  4426. }
  4427. for (i = 0; i < ARRAY_SIZE(z->mfr2_fwd); i++) {
  4428. z->mfr2_fwd[i].tonesamples =
  4429. gdp->mfr2_tonelen * DAHDI_CHUNKSIZE;
  4430. }
  4431. for (i = 0; i < ARRAY_SIZE(z->mfr2_rev); i++) {
  4432. z->mfr2_rev[i].tonesamples =
  4433. gdp->mfr2_tonelen * DAHDI_CHUNKSIZE;
  4434. }
  4435. }
  4436. spin_unlock(&zone_lock);
  4437. dtmf_silence.tonesamples = gdp->dtmf_tonelen * DAHDI_CHUNKSIZE;
  4438. mfr1_silence.tonesamples = gdp->mfv1_tonelen * DAHDI_CHUNKSIZE;
  4439. mfr2_silence.tonesamples = gdp->mfr2_tonelen * DAHDI_CHUNKSIZE;
  4440. mutex_unlock(&global_dialparamslock);
  4441. return 0;
  4442. }
  4443. static int dahdi_ioctl_get_dialparams(unsigned long data)
  4444. {
  4445. struct dahdi_dialparams tdp;
  4446. mutex_lock(&global_dialparamslock);
  4447. tdp = global_dialparams;
  4448. mutex_unlock(&global_dialparamslock);
  4449. if (copy_to_user((void __user *)data, &tdp, sizeof(tdp)))
  4450. return -EFAULT;
  4451. return 0;
  4452. }
  4453. static int dahdi_ioctl_indirect(struct file *file, unsigned long data)
  4454. {
  4455. int res;
  4456. struct dahdi_indirect_data ind;
  4457. void *old;
  4458. static bool warned;
  4459. struct dahdi_chan *chan;
  4460. if (copy_from_user(&ind, (void __user *)data, sizeof(ind)))
  4461. return -EFAULT;
  4462. chan = chan_from_num(ind.chan);
  4463. if (!chan)
  4464. return -EINVAL;
  4465. if (!warned) {
  4466. warned = true;
  4467. module_printk(KERN_WARNING, "Using deprecated " \
  4468. "DAHDI_INDIRECT. Please update " \
  4469. "dahdi-tools.\n");
  4470. }
  4471. /* Since dahdi_chan_ioctl expects to be called on file handles
  4472. * associated with channels, we'll temporarily set the
  4473. * private_data pointer on the ctl file handle just for this
  4474. * call. */
  4475. old = file->private_data;
  4476. file->private_data = chan;
  4477. res = dahdi_chan_ioctl(file, ind.op, (unsigned long) ind.data);
  4478. file->private_data = old;
  4479. return res;
  4480. }
  4481. static int dahdi_ioctl_spanconfig(struct file *file, unsigned long data)
  4482. {
  4483. int res = 0;
  4484. struct dahdi_lineconfig lc;
  4485. struct dahdi_span *s;
  4486. if (copy_from_user(&lc, (void __user *)data, sizeof(lc)))
  4487. return -EFAULT;
  4488. s = span_find_and_get(lc.span);
  4489. if (!s)
  4490. return -ENXIO;
  4491. if ((lc.lineconfig & 0x1ff0 & s->linecompat) !=
  4492. (lc.lineconfig & 0x1ff0)) {
  4493. put_span(s);
  4494. return -EINVAL;
  4495. }
  4496. if (s->ops->spanconfig) {
  4497. s->lineconfig = lc.lineconfig;
  4498. s->lbo = lc.lbo;
  4499. s->txlevel = lc.lbo;
  4500. s->rxlevel = 0;
  4501. res = s->ops->spanconfig(file, s, &lc);
  4502. }
  4503. put_span(s);
  4504. return res;
  4505. }
  4506. static int dahdi_ioctl_startup(struct file *file, unsigned long data)
  4507. {
  4508. /* I/O CTL's for control interface */
  4509. int j;
  4510. int res = 0;
  4511. int x, y;
  4512. unsigned long flags;
  4513. struct dahdi_span *s;
  4514. if (get_user(j, (int __user *)data))
  4515. return -EFAULT;
  4516. s = span_find_and_get(j);
  4517. if (!s)
  4518. return -ENXIO;
  4519. if (s->flags & DAHDI_FLAG_RUNNING) {
  4520. put_span(s);
  4521. return 0;
  4522. }
  4523. if (s->ops->startup)
  4524. res = s->ops->startup(file, s);
  4525. if (!res) {
  4526. /* Mark as running and hangup any channels */
  4527. s->flags |= DAHDI_FLAG_RUNNING;
  4528. for (x = 0; x < s->channels; x++) {
  4529. y = dahdi_q_sig(s->chans[x]) & 0xff;
  4530. if (y >= 0)
  4531. s->chans[x]->rxsig = (unsigned char)y;
  4532. spin_lock_irqsave(&s->chans[x]->lock, flags);
  4533. dahdi_hangup(s->chans[x]);
  4534. spin_unlock_irqrestore(&s->chans[x]->lock, flags);
  4535. /*
  4536. * Set the rxhooksig back to
  4537. * DAHDI_RXSIG_INITIAL so that new events are
  4538. * queued on the channel with the actual
  4539. * received hook state.
  4540. *
  4541. */
  4542. s->chans[x]->rxhooksig = DAHDI_RXSIG_INITIAL;
  4543. }
  4544. /* Now that this span is running, it might be selected as the
  4545. * master_span */
  4546. __dahdi_find_master_span();
  4547. }
  4548. put_span(s);
  4549. return res;
  4550. }
  4551. static int dahdi_shutdown_span(struct dahdi_span *s)
  4552. {
  4553. int res = 0;
  4554. int x;
  4555. /* Unconfigure channels */
  4556. for (x = 0; x < s->channels; x++)
  4557. s->chans[x]->sig = 0;
  4558. if (s->ops->shutdown)
  4559. res = s->ops->shutdown(s);
  4560. clear_bit(DAHDI_FLAGBIT_RUNNING, &s->flags);
  4561. return res;
  4562. }
  4563. static int dahdi_ioctl_shutdown(unsigned long data)
  4564. {
  4565. int res;
  4566. /* I/O CTL's for control interface */
  4567. int j;
  4568. struct dahdi_span *s;
  4569. if (get_user(j, (int __user *)data))
  4570. return -EFAULT;
  4571. s = span_find_and_get(j);
  4572. if (!s)
  4573. return -ENXIO;
  4574. res = dahdi_shutdown_span(s);
  4575. put_span(s);
  4576. return res;
  4577. }
  4578. /**
  4579. * dahdi_is_hwec_available - Is hardware echocan available on a channel?
  4580. * @chan: The channel to check
  4581. *
  4582. * Returns true if there is a hardware echocan available for the attached
  4583. * channel, or false otherwise.
  4584. *
  4585. */
  4586. static bool dahdi_is_hwec_available(const struct dahdi_chan *chan)
  4587. {
  4588. if (!chan->span || !chan->span->ops->echocan_name ||
  4589. !hwec_factory.get_name(chan))
  4590. return false;
  4591. return true;
  4592. }
  4593. static int dahdi_ioctl_attach_echocan(unsigned long data)
  4594. {
  4595. struct dahdi_chan *chan;
  4596. struct dahdi_attach_echocan ae;
  4597. const struct dahdi_echocan_factory *new = NULL, *old;
  4598. if (copy_from_user(&ae, (void __user *)data, sizeof(ae)))
  4599. return -EFAULT;
  4600. chan = chan_from_num(ae.chan);
  4601. if (!chan)
  4602. return -EINVAL;
  4603. ae.echocan[sizeof(ae.echocan) - 1] = '\0';
  4604. if (dahdi_is_hwec_available(chan)) {
  4605. if (hwec_overrides_swec) {
  4606. chan_dbg(GENERAL, chan,
  4607. "Using echocan '%s' instead of requested " \
  4608. "'%s'.\n", hwec_def_name, ae.echocan);
  4609. /* If there is a hardware echocan available we'll
  4610. * always use it instead of any configured software
  4611. * echocan. This matches the behavior in dahdi 2.4.1.2
  4612. * and earlier releases. */
  4613. strlcpy(ae.echocan, hwec_def_name, sizeof(ae.echocan));
  4614. } else if (strcasecmp(ae.echocan, hwec_def_name) != 0) {
  4615. chan_dbg(GENERAL, chan,
  4616. "Using '%s' on channel even though '%s' is " \
  4617. "available.\n", ae.echocan, hwec_def_name);
  4618. }
  4619. }
  4620. if (ae.echocan[0]) {
  4621. new = find_echocan(ae.echocan);
  4622. if (!new)
  4623. return -EINVAL;
  4624. if (!new->get_name(chan)) {
  4625. release_echocan(new);
  4626. return -EINVAL;
  4627. }
  4628. }
  4629. mutex_lock(&chan->mutex);
  4630. old = chan->ec_factory;
  4631. chan->ec_factory = new;
  4632. mutex_unlock(&chan->mutex);
  4633. if (old)
  4634. release_echocan(old);
  4635. return 0;
  4636. }
  4637. static int dahdi_ioctl_sfconfig(unsigned long data)
  4638. {
  4639. int res = 0;
  4640. unsigned long flags;
  4641. struct dahdi_chan *chan;
  4642. struct dahdi_sfconfig sf;
  4643. if (copy_from_user(&sf, (void __user *)data, sizeof(sf)))
  4644. return -EFAULT;
  4645. chan = chan_from_num(sf.chan);
  4646. if (!chan)
  4647. return -EINVAL;
  4648. if (chan->sig != DAHDI_SIG_SF)
  4649. return -EINVAL;
  4650. spin_lock_irqsave(&chan->lock, flags);
  4651. chan->rxp1 = sf.rxp1;
  4652. chan->rxp2 = sf.rxp2;
  4653. chan->rxp3 = sf.rxp3;
  4654. chan->txtone = sf.txtone;
  4655. chan->tx_v2 = sf.tx_v2;
  4656. chan->tx_v3 = sf.tx_v3;
  4657. chan->toneflags = sf.toneflag;
  4658. if (sf.txtone) { /* if set to make tone for tx */
  4659. if ((chan->txhooksig &&
  4660. !(sf.toneflag & DAHDI_REVERSE_TXTONE)) ||
  4661. ((!chan->txhooksig) &&
  4662. (sf.toneflag & DAHDI_REVERSE_TXTONE))) {
  4663. set_txtone(chan, sf.txtone, sf.tx_v2, sf.tx_v3);
  4664. } else {
  4665. set_txtone(chan, 0, 0, 0);
  4666. }
  4667. }
  4668. spin_unlock_irqrestore(&chan->lock, flags);
  4669. return res;
  4670. }
  4671. /* Returns true if there are any hardware echocan on any span. */
  4672. static bool dahdi_any_hwec_available(void)
  4673. {
  4674. int i;
  4675. bool hwec_available = false;
  4676. struct dahdi_span *s;
  4677. mutex_lock(&registration_mutex);
  4678. list_for_each_entry(s, &span_list, spans_node) {
  4679. for (i = 0; i < s->channels; ++i) {
  4680. struct dahdi_chan *const chan = s->chans[i];
  4681. if (dahdi_is_hwec_available(chan)) {
  4682. hwec_available = true;
  4683. break;
  4684. }
  4685. }
  4686. }
  4687. mutex_unlock(&registration_mutex);
  4688. return hwec_available;
  4689. }
  4690. static int dahdi_ioctl_get_version(unsigned long data)
  4691. {
  4692. struct dahdi_versioninfo vi;
  4693. struct ecfactory *cur;
  4694. size_t space = sizeof(vi.echo_canceller) - 1;
  4695. bool have_hwec = dahdi_any_hwec_available();
  4696. memset(&vi, 0, sizeof(vi));
  4697. strlcpy(vi.version, dahdi_version, sizeof(vi.version));
  4698. spin_lock(&ecfactory_list_lock);
  4699. list_for_each_entry(cur, &ecfactory_list, list) {
  4700. const char * const ec_name = cur->ec->get_name(NULL);
  4701. if ((ec_name == hwec_def_name) && !have_hwec) {
  4702. /*
  4703. * The hardware echocan factory is always registered so
  4704. * that hwec can be configured on the channels as if it
  4705. * was a software echocan. However, it can be confusing
  4706. * to list it as one of the available options in the
  4707. * output of dahdi_cfg if there isn't a REAL hardware
  4708. * echocanceler attached to any of the spans. In that
  4709. * case, do not report the presence of the hardware
  4710. * echocan factory to userspace.
  4711. *
  4712. */
  4713. continue;
  4714. }
  4715. strncat(vi.echo_canceller + strlen(vi.echo_canceller),
  4716. ec_name, space);
  4717. space -= strlen(ec_name);
  4718. if (space < 1)
  4719. break;
  4720. if (cur->list.next && (cur->list.next != &ecfactory_list)) {
  4721. strncat(vi.echo_canceller + strlen(vi.echo_canceller),
  4722. ", ", space);
  4723. space -= 2;
  4724. if (space < 1)
  4725. break;
  4726. }
  4727. }
  4728. spin_unlock(&ecfactory_list_lock);
  4729. if (copy_to_user((void __user *)data, &vi, sizeof(vi)))
  4730. return -EFAULT;
  4731. return 0;
  4732. }
  4733. static int dahdi_ioctl_maint(unsigned long data)
  4734. {
  4735. int i;
  4736. unsigned long flags;
  4737. int rv;
  4738. struct dahdi_span *s;
  4739. struct dahdi_maintinfo maint;
  4740. /* get struct from user */
  4741. if (copy_from_user(&maint, (void __user *)data, sizeof(maint)))
  4742. return -EFAULT;
  4743. s = span_find_and_get(maint.spanno);
  4744. if (!s)
  4745. return -EINVAL;
  4746. if (!s->ops->maint) {
  4747. put_span(s);
  4748. return -ENOSYS;
  4749. }
  4750. spin_lock_irqsave(&s->lock, flags);
  4751. /* save current maint state */
  4752. i = s->maintstat;
  4753. /* set maint mode */
  4754. s->maintstat = maint.command;
  4755. switch (maint.command) {
  4756. case DAHDI_MAINT_NONE:
  4757. case DAHDI_MAINT_LOCALLOOP:
  4758. case DAHDI_MAINT_NETWORKLINELOOP:
  4759. case DAHDI_MAINT_NETWORKPAYLOADLOOP:
  4760. /* if same, ignore it */
  4761. if (i == maint.command)
  4762. break;
  4763. rv = s->ops->maint(s, maint.command);
  4764. spin_unlock_irqrestore(&s->lock, flags);
  4765. if (rv) {
  4766. put_span(s);
  4767. /* Restore the state on error */
  4768. s->maintstat = i;
  4769. return rv;
  4770. }
  4771. spin_lock_irqsave(&s->lock, flags);
  4772. break;
  4773. case DAHDI_MAINT_LOOPUP:
  4774. case DAHDI_MAINT_LOOPDOWN:
  4775. s->mainttimer = DAHDI_LOOPCODE_TIME * DAHDI_CHUNKSIZE;
  4776. rv = s->ops->maint(s, maint.command);
  4777. spin_unlock_irqrestore(&s->lock, flags);
  4778. if (rv) {
  4779. put_span(s);
  4780. /* Restore the state on error */
  4781. s->maintstat = i;
  4782. return rv;
  4783. }
  4784. spin_lock_irqsave(&s->lock, flags);
  4785. break;
  4786. case DAHDI_MAINT_FAS_DEFECT:
  4787. case DAHDI_MAINT_MULTI_DEFECT:
  4788. case DAHDI_MAINT_CRC_DEFECT:
  4789. case DAHDI_MAINT_CAS_DEFECT:
  4790. case DAHDI_MAINT_PRBS_DEFECT:
  4791. case DAHDI_MAINT_BIPOLAR_DEFECT:
  4792. case DAHDI_MAINT_PRBS:
  4793. case DAHDI_RESET_COUNTERS:
  4794. case DAHDI_MAINT_ALARM_SIM:
  4795. /* Prevent notifying an alarm state for generic
  4796. maintenance functions, unless the driver is
  4797. already in a maint state */
  4798. if (!i)
  4799. s->maintstat = 0;
  4800. rv = s->ops->maint(s, maint.command);
  4801. spin_unlock_irqrestore(&s->lock, flags);
  4802. if (rv) {
  4803. put_span(s);
  4804. /* Restore the state on error */
  4805. s->maintstat = i;
  4806. return rv;
  4807. }
  4808. spin_lock_irqsave(&s->lock, flags);
  4809. break;
  4810. default:
  4811. spin_unlock_irqrestore(&s->lock, flags);
  4812. module_printk(KERN_NOTICE,
  4813. "Unknown maintenance event: %d\n",
  4814. maint.command);
  4815. put_span(s);
  4816. /* Restore the state on error */
  4817. s->maintstat = i;
  4818. return -ENOSYS;
  4819. }
  4820. dahdi_alarm_notify(s); /* process alarm-related events */
  4821. spin_unlock_irqrestore(&s->lock, flags);
  4822. put_span(s);
  4823. return 0;
  4824. }
  4825. static int dahdi_ioctl_dynamic(unsigned int cmd, unsigned long data)
  4826. {
  4827. bool tried_load = false;
  4828. int res;
  4829. retry_check:
  4830. mutex_lock(&registration_mutex);
  4831. if (!dahdi_dynamic_ops) {
  4832. mutex_unlock(&registration_mutex);
  4833. if (tried_load)
  4834. return -ENOSYS;
  4835. request_module("dahdi_dynamic");
  4836. tried_load = true;
  4837. goto retry_check;
  4838. }
  4839. if (!try_module_get(dahdi_dynamic_ops->owner)) {
  4840. mutex_unlock(&registration_mutex);
  4841. return -ENOSYS;
  4842. }
  4843. mutex_unlock(&registration_mutex);
  4844. res = dahdi_dynamic_ops->ioctl(cmd, data);
  4845. module_put(dahdi_dynamic_ops->owner);
  4846. return res;
  4847. }
  4848. static int
  4849. dahdi_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  4850. {
  4851. switch (cmd) {
  4852. case DAHDI_INDIRECT:
  4853. return dahdi_ioctl_indirect(file, data);
  4854. case DAHDI_SPANCONFIG:
  4855. return dahdi_ioctl_spanconfig(file, data);
  4856. case DAHDI_STARTUP:
  4857. return dahdi_ioctl_startup(file, data);
  4858. case DAHDI_SHUTDOWN:
  4859. return dahdi_ioctl_shutdown(data);
  4860. case DAHDI_ATTACH_ECHOCAN:
  4861. return dahdi_ioctl_attach_echocan(data);
  4862. case DAHDI_CHANCONFIG:
  4863. return dahdi_ioctl_chanconfig(file, data);
  4864. case DAHDI_SFCONFIG:
  4865. return dahdi_ioctl_sfconfig(data);
  4866. case DAHDI_DEFAULTZONE:
  4867. return dahdi_ioctl_defaultzone(data);
  4868. case DAHDI_LOADZONE:
  4869. return dahdi_ioctl_loadzone(data);
  4870. case DAHDI_FREEZONE:
  4871. return dahdi_ioctl_freezone(data);
  4872. case DAHDI_SET_DIALPARAMS:
  4873. return dahdi_ioctl_set_dialparams(data);
  4874. case DAHDI_GET_DIALPARAMS:
  4875. return dahdi_ioctl_get_dialparams(data);
  4876. case DAHDI_GETVERSION:
  4877. return dahdi_ioctl_get_version(data);
  4878. case DAHDI_MAINT:
  4879. return dahdi_ioctl_maint(data);
  4880. case DAHDI_DYNAMIC_CREATE:
  4881. case DAHDI_DYNAMIC_DESTROY:
  4882. return dahdi_ioctl_dynamic(cmd, data);
  4883. case DAHDI_EC_LICENSE_CHALLENGE:
  4884. case DAHDI_EC_LICENSE_RESPONSE:
  4885. if (dahdi_hpec_ioctl) {
  4886. return dahdi_hpec_ioctl(cmd, data);
  4887. } else {
  4888. request_module("dahdi_echocan_hpec");
  4889. if (dahdi_hpec_ioctl)
  4890. return dahdi_hpec_ioctl(cmd, data);
  4891. }
  4892. return -ENOSYS;
  4893. }
  4894. return dahdi_common_ioctl(file, cmd, data);
  4895. }
  4896. static int ioctl_dahdi_dial(struct dahdi_chan *chan, unsigned long data)
  4897. {
  4898. struct dahdi_dialoperation *tdo;
  4899. unsigned long flags;
  4900. char *s;
  4901. int rv;
  4902. void __user * const user_data = (void __user *)data;
  4903. tdo = kmalloc(sizeof(*tdo), GFP_KERNEL);
  4904. if (!tdo)
  4905. return -ENOMEM;
  4906. if (copy_from_user(tdo, user_data, sizeof(*tdo)))
  4907. return -EFAULT;
  4908. rv = 0;
  4909. /* Force proper NULL termination and uppercase entry */
  4910. tdo->dialstr[DAHDI_MAX_DTMF_BUF - 1] = '\0';
  4911. for (s = tdo->dialstr; *s; s++)
  4912. *s = toupper(*s);
  4913. spin_lock_irqsave(&chan->lock, flags);
  4914. if (!chan->curzone) {
  4915. spin_unlock_irqrestore(&chan->lock, flags);
  4916. /* The tone zones are loaded by dahdi_cfg from /etc/dahdi/system.conf */
  4917. module_printk(KERN_WARNING, "Cannot dial until a tone zone is loaded.\n");
  4918. return -ENODATA;
  4919. }
  4920. switch (tdo->op) {
  4921. case DAHDI_DIAL_OP_CANCEL:
  4922. chan->curtone = NULL;
  4923. chan->dialing = 0;
  4924. chan->txdialbuf[0] = '\0';
  4925. chan->tonep = 0;
  4926. chan->pdialcount = 0;
  4927. break;
  4928. case DAHDI_DIAL_OP_REPLACE:
  4929. strcpy(chan->txdialbuf, tdo->dialstr);
  4930. chan->dialing = 1;
  4931. __do_dtmf(chan);
  4932. break;
  4933. case DAHDI_DIAL_OP_APPEND:
  4934. if (strlen(tdo->dialstr) + strlen(chan->txdialbuf) >= (DAHDI_MAX_DTMF_BUF - 1)) {
  4935. rv = -EBUSY;
  4936. break;
  4937. }
  4938. strlcpy(chan->txdialbuf + strlen(chan->txdialbuf), tdo->dialstr,
  4939. DAHDI_MAX_DTMF_BUF - strlen(chan->txdialbuf));
  4940. if (!chan->dialing) {
  4941. chan->dialing = 1;
  4942. __do_dtmf(chan);
  4943. }
  4944. break;
  4945. default:
  4946. rv = -EINVAL;
  4947. }
  4948. spin_unlock_irqrestore(&chan->lock, flags);
  4949. return rv;
  4950. }
  4951. static int dahdi_ioctl_setconf(struct file *file, unsigned long data)
  4952. {
  4953. struct dahdi_confinfo conf;
  4954. struct dahdi_chan *chan;
  4955. struct dahdi_chan *conf_chan = NULL;
  4956. unsigned long flags;
  4957. unsigned int confmode;
  4958. int oldconf;
  4959. enum {NONE, ENABLE_HWPREEC, DISABLE_HWPREEC} preec = NONE;
  4960. if (copy_from_user(&conf, (void __user *)data, sizeof(conf)))
  4961. return -EFAULT;
  4962. confmode = conf.confmode & DAHDI_CONF_MODE_MASK;
  4963. chan = (conf.chan) ? chan_from_num(conf.chan) :
  4964. chan_from_file(file);
  4965. if (!chan)
  4966. return -EINVAL;
  4967. if (!(chan->flags & DAHDI_FLAG_AUDIO))
  4968. return -EINVAL;
  4969. if ((DAHDI_CONF_DIGITALMON == confmode) ||
  4970. is_monitor_mode(conf.confmode)) {
  4971. conf_chan = chan_from_num(conf.confno);
  4972. if (!conf_chan)
  4973. return -EINVAL;
  4974. } else {
  4975. /* make sure conf number makes sense, too */
  4976. if ((conf.confno < -1) || (conf.confno > DAHDI_MAX_CONF))
  4977. return -EINVAL;
  4978. }
  4979. /* if taking off of any conf, must have 0 mode */
  4980. if ((!conf.confno) && conf.confmode)
  4981. return -EINVAL;
  4982. /* likewise if 0 mode must have no conf */
  4983. if ((!conf.confmode) && conf.confno)
  4984. return -EINVAL;
  4985. dahdi_check_conf(conf.confno);
  4986. conf.chan = chan->channo; /* return with real channel # */
  4987. spin_lock_irqsave(&chan_lock, flags);
  4988. spin_lock(&chan->lock);
  4989. if (conf.confno == -1)
  4990. conf.confno = dahdi_first_empty_conference();
  4991. if ((conf.confno < 1) && (conf.confmode)) {
  4992. /* No more empty conferences */
  4993. spin_unlock(&chan->lock);
  4994. spin_unlock_irqrestore(&chan_lock, flags);
  4995. return -EBUSY;
  4996. }
  4997. /* if changing confs, clear last added info */
  4998. if (conf.confno != chan->confna) {
  4999. memset(chan->conflast, 0, DAHDI_MAX_CHUNKSIZE);
  5000. memset(chan->conflast1, 0, DAHDI_MAX_CHUNKSIZE);
  5001. memset(chan->conflast2, 0, DAHDI_MAX_CHUNKSIZE);
  5002. }
  5003. oldconf = chan->confna; /* save old conference number */
  5004. chan->confna = conf.confno; /* set conference number */
  5005. chan->conf_chan = conf_chan;
  5006. chan->confmode = conf.confmode; /* set conference mode */
  5007. chan->_confn = 0; /* Clear confn */
  5008. if (chan->span && chan->span->ops->dacs) {
  5009. if ((confmode == DAHDI_CONF_DIGITALMON) &&
  5010. (chan->txgain == defgain) &&
  5011. (chan->rxgain == defgain) &&
  5012. (conf_chan->txgain == defgain) &&
  5013. (conf_chan->rxgain == defgain)) {
  5014. dahdi_chan_dacs(chan, conf_chan);
  5015. } else {
  5016. dahdi_disable_dacs(chan);
  5017. }
  5018. }
  5019. /* if we are going onto a conf */
  5020. if (conf.confno &&
  5021. (confmode == DAHDI_CONF_CONF ||
  5022. confmode == DAHDI_CONF_CONFANN ||
  5023. confmode == DAHDI_CONF_CONFMON ||
  5024. confmode == DAHDI_CONF_CONFANNMON ||
  5025. confmode == DAHDI_CONF_REALANDPSEUDO)) {
  5026. /* Get alias */
  5027. chan->_confn = dahdi_get_conf_alias(conf.confno);
  5028. }
  5029. spin_unlock(&chan->lock);
  5030. if (conf_chan) {
  5031. if ((confmode == DAHDI_CONF_MONITOR_RX_PREECHO) ||
  5032. (confmode == DAHDI_CONF_MONITOR_TX_PREECHO) ||
  5033. (confmode == DAHDI_CONF_MONITORBOTH_PREECHO)) {
  5034. if (!conf_chan->readchunkpreec) {
  5035. void *temp = kmalloc(sizeof(short) *
  5036. DAHDI_CHUNKSIZE, GFP_ATOMIC);
  5037. if (temp) {
  5038. preec = ENABLE_HWPREEC;
  5039. spin_lock(&conf_chan->lock);
  5040. conf_chan->readchunkpreec = temp;
  5041. spin_unlock(&conf_chan->lock);
  5042. }
  5043. }
  5044. } else {
  5045. preec = DISABLE_HWPREEC;
  5046. spin_lock(&conf_chan->lock);
  5047. kfree(conf_chan->readchunkpreec);
  5048. conf_chan->readchunkpreec = NULL;
  5049. spin_unlock(&conf_chan->lock);
  5050. }
  5051. }
  5052. spin_unlock_irqrestore(&chan_lock, flags);
  5053. if (ENABLE_HWPREEC == preec) {
  5054. int res = dahdi_enable_hw_preechocan(conf_chan);
  5055. if (res) {
  5056. spin_lock_irqsave(&chan_lock, flags);
  5057. spin_lock(&conf_chan->lock);
  5058. kfree(conf_chan->readchunkpreec);
  5059. conf_chan->readchunkpreec = NULL;
  5060. spin_unlock(&conf_chan->lock);
  5061. spin_unlock_irqrestore(&chan_lock, flags);
  5062. }
  5063. return res;
  5064. } else if (DISABLE_HWPREEC == preec) {
  5065. dahdi_disable_hw_preechocan(conf_chan);
  5066. }
  5067. dahdi_check_conf(oldconf);
  5068. if (copy_to_user((void __user *)data, &conf, sizeof(conf)))
  5069. return -EFAULT;
  5070. return 0;
  5071. }
  5072. /**
  5073. * dahdi_ioctl_confdiag() - Output debug info about conferences to console.
  5074. *
  5075. * This is a pure debugging aide since the only result is to the console.
  5076. *
  5077. * TODO: Does anyone use this anymore? Should it be hidden behind a debug
  5078. * compile time option?
  5079. */
  5080. static int dahdi_ioctl_confdiag(struct file *file, unsigned long data)
  5081. {
  5082. struct dahdi_chan *chan;
  5083. unsigned long flags;
  5084. int i;
  5085. int j;
  5086. int c;
  5087. chan = chan_from_file(file);
  5088. if (!chan)
  5089. return -EINVAL;
  5090. if (!(chan->flags & DAHDI_FLAG_AUDIO))
  5091. return -EINVAL;
  5092. get_user(j, (int __user *)data); /* get conf # */
  5093. /* loop thru the interesting ones */
  5094. for (i = ((j) ? j : 1); i <= ((j) ? j : DAHDI_MAX_CONF); i++) {
  5095. struct dahdi_span *s;
  5096. struct pseudo_chan *pseudo;
  5097. int k;
  5098. c = 0;
  5099. spin_lock_irqsave(&chan_lock, flags);
  5100. list_for_each_entry(s, &span_list, spans_node) {
  5101. for (k = 0; k < s->channels; k++) {
  5102. chan = s->chans[k];
  5103. if (chan->confna != i)
  5104. continue;
  5105. if (!c)
  5106. module_printk(KERN_NOTICE, "Conf #%d:\n", i);
  5107. c = 1;
  5108. module_printk(KERN_NOTICE, "chan %d, mode %x\n",
  5109. chan->channo, chan->confmode);
  5110. }
  5111. }
  5112. list_for_each_entry(pseudo, &pseudo_chans, node) {
  5113. /* skip if not in this conf */
  5114. if (pseudo->chan.confna != i)
  5115. continue;
  5116. if (!c)
  5117. module_printk(KERN_NOTICE, "Conf #%d:\n", i);
  5118. c = 1;
  5119. module_printk(KERN_NOTICE, "chan %d, mode %x\n",
  5120. pseudo->chan.channo, pseudo->chan.confmode);
  5121. }
  5122. #ifdef CONFIG_DAHDI_CONFLINK
  5123. {
  5124. int rv;
  5125. rv = 0;
  5126. for (k = 1; k <= DAHDI_MAX_CONF; k++) {
  5127. if (conf_links[k].dst == i) {
  5128. if (!c) {
  5129. c = 1;
  5130. module_printk(KERN_NOTICE,
  5131. "Conf #%d:\n", i);
  5132. }
  5133. if (!rv) {
  5134. rv = 1;
  5135. module_printk(KERN_NOTICE,
  5136. "Snooping on:\n");
  5137. }
  5138. module_printk(KERN_NOTICE, "conf %d\n",
  5139. conf_links[k].src);
  5140. }
  5141. }
  5142. }
  5143. #endif
  5144. spin_unlock_irqrestore(&chan_lock, flags);
  5145. if (c)
  5146. module_printk(KERN_NOTICE, "\n");
  5147. }
  5148. return 0;
  5149. }
  5150. static int dahdi_ioctl_getconf(struct file *file, unsigned long data)
  5151. {
  5152. struct dahdi_confinfo conf;
  5153. struct dahdi_chan *chan;
  5154. if (copy_from_user(&conf, (void __user *)data, sizeof(conf)))
  5155. return -EFAULT;
  5156. chan = (!conf.chan) ? chan_from_file(file) :
  5157. chan_from_num(conf.chan);
  5158. if (!chan)
  5159. return -EINVAL;
  5160. if (!(chan->flags & DAHDI_FLAG_AUDIO))
  5161. return -EINVAL;
  5162. conf.chan = chan->channo; /* get channel number */
  5163. conf.confno = chan->confna; /* get conference number */
  5164. conf.confmode = chan->confmode; /* get conference mode */
  5165. if (copy_to_user((void __user *)data, &conf, sizeof(conf)))
  5166. return -EFAULT;
  5167. return 0;
  5168. }
  5169. /**
  5170. * dahdi_ioctl_iomux() - Wait for *something* to happen.
  5171. *
  5172. * This is now basically like the wait_event_interruptible function, but with
  5173. * a much more involved wait condition.
  5174. */
  5175. static int dahdi_ioctl_iomux(struct file *file, unsigned long data)
  5176. {
  5177. struct dahdi_chan *const chan = chan_from_file(file);
  5178. unsigned long flags;
  5179. unsigned int iomask;
  5180. int ret = 0;
  5181. DEFINE_WAIT(wait);
  5182. if (get_user(iomask, (int __user *)data))
  5183. return -EFAULT;
  5184. if (unlikely(!iomask || !chan))
  5185. return -EINVAL;
  5186. while (1) {
  5187. unsigned int wait_result;
  5188. wait_result = 0;
  5189. prepare_to_wait(&chan->waitq, &wait, TASK_INTERRUPTIBLE);
  5190. if (unlikely(!chan->file->private_data)) {
  5191. /*
  5192. * This should never happen. Surprise device removal
  5193. * should lead us to the nodev_* file_operations
  5194. */
  5195. msleep(5);
  5196. module_printk(KERN_ERR, "%s: NODEV\n", __func__);
  5197. ret = -ENODEV;
  5198. break;
  5199. }
  5200. spin_lock_irqsave(&chan->lock, flags);
  5201. chan->iomask = iomask;
  5202. if (iomask & DAHDI_IOMUX_READ) {
  5203. if (chan->outreadbuf > -1)
  5204. wait_result |= DAHDI_IOMUX_READ;
  5205. }
  5206. if (iomask & DAHDI_IOMUX_WRITE) {
  5207. if (chan->inwritebuf > -1)
  5208. wait_result |= DAHDI_IOMUX_WRITE;
  5209. }
  5210. if (iomask & DAHDI_IOMUX_WRITEEMPTY) {
  5211. /* if everything empty -- be sure the transmitter is
  5212. * enabled */
  5213. chan->txdisable = 0;
  5214. if (chan->outwritebuf < 0)
  5215. wait_result |= DAHDI_IOMUX_WRITEEMPTY;
  5216. }
  5217. if (iomask & DAHDI_IOMUX_SIGEVENT) {
  5218. if (chan->eventinidx != chan->eventoutidx)
  5219. wait_result |= DAHDI_IOMUX_SIGEVENT;
  5220. }
  5221. spin_unlock_irqrestore(&chan->lock, flags);
  5222. if (wait_result || (iomask & DAHDI_IOMUX_NOWAIT)) {
  5223. put_user(wait_result, (int __user *)data);
  5224. break;
  5225. }
  5226. if (signal_pending(current)) {
  5227. finish_wait(&chan->waitq, &wait);
  5228. return -ERESTARTSYS;
  5229. }
  5230. schedule();
  5231. }
  5232. finish_wait(&chan->waitq, &wait);
  5233. spin_lock_irqsave(&chan->lock, flags);
  5234. chan->iomask = 0;
  5235. spin_unlock_irqrestore(&chan->lock, flags);
  5236. return ret;
  5237. }
  5238. #ifdef CONFIG_DAHDI_MIRROR
  5239. static int dahdi_ioctl_rxmirror(struct file *file, unsigned long data)
  5240. {
  5241. int res;
  5242. int i;
  5243. unsigned long flags;
  5244. struct dahdi_chan *const chan = chan_from_file(file);
  5245. struct dahdi_chan *srcmirror;
  5246. if (!chan || chan->srcmirror)
  5247. return -ENODEV;
  5248. res = get_user(i, (int __user *)data);
  5249. if (res)
  5250. return res;
  5251. srcmirror = chan_from_num(i);
  5252. if (!srcmirror)
  5253. return -EINVAL;
  5254. module_printk(KERN_INFO, "Chan %d rx mirrored to %d\n",
  5255. srcmirror->channo, chan->channo);
  5256. spin_lock_irqsave(&srcmirror->lock, flags);
  5257. if (srcmirror->rxmirror == NULL)
  5258. srcmirror->rxmirror = chan;
  5259. spin_unlock_irqrestore(&srcmirror->lock, flags);
  5260. if (srcmirror->rxmirror != chan) {
  5261. module_printk(KERN_INFO, "Chan %d cannot be rxmirrored, " \
  5262. "already in use\n", srcmirror->channo);
  5263. return -EFAULT;
  5264. }
  5265. spin_lock_irqsave(&chan->lock, flags);
  5266. chan->srcmirror = srcmirror;
  5267. chan->flags = srcmirror->flags;
  5268. chan->sig = srcmirror->sig;
  5269. clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
  5270. spin_unlock_irqrestore(&chan->lock, flags);
  5271. return 0;
  5272. }
  5273. static int dahdi_ioctl_txmirror(struct file *file, unsigned long data)
  5274. {
  5275. int res;
  5276. int i;
  5277. unsigned long flags;
  5278. struct dahdi_chan *const chan = chan_from_file(file);
  5279. struct dahdi_chan *srcmirror;
  5280. if (!chan || chan->srcmirror)
  5281. return -ENODEV;
  5282. res = get_user(i, (int __user *)data);
  5283. if (res)
  5284. return res;
  5285. srcmirror = chan_from_num(i);
  5286. if (!srcmirror)
  5287. return -EINVAL;
  5288. module_printk(KERN_INFO, "Chan %d tx mirrored to %d\n",
  5289. srcmirror->channo, chan->channo);
  5290. spin_lock_irqsave(&srcmirror->lock, flags);
  5291. srcmirror->txmirror = chan;
  5292. if (srcmirror->txmirror == NULL)
  5293. srcmirror->txmirror = chan;
  5294. spin_unlock_irqrestore(&srcmirror->lock, flags);
  5295. if (srcmirror->txmirror != chan) {
  5296. module_printk(KERN_INFO, "Chan %d cannot be txmirrored, " \
  5297. "already in use\n", i);
  5298. return -EFAULT;
  5299. }
  5300. spin_lock_irqsave(&chan->lock, flags);
  5301. chan->srcmirror = srcmirror;
  5302. chan->flags = srcmirror->flags;
  5303. chan->sig = srcmirror->sig;
  5304. clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
  5305. spin_unlock_irqrestore(&chan->lock, flags);
  5306. return 0;
  5307. }
  5308. #endif /* CONFIG_DAHDI_MIRROR */
  5309. static int
  5310. dahdi_chanandpseudo_ioctl(struct file *file, unsigned int cmd,
  5311. unsigned long data)
  5312. {
  5313. struct dahdi_chan *chan = chan_from_file(file);
  5314. union {
  5315. struct dahdi_bufferinfo bi;
  5316. struct dahdi_ring_cadence cad;
  5317. } stack;
  5318. unsigned long flags;
  5319. int i, j, rv;
  5320. void __user * const user_data = (void __user *)data;
  5321. if (!chan)
  5322. return -EINVAL;
  5323. switch(cmd) {
  5324. #ifdef CONFIG_DAHDI_MIRROR
  5325. case DAHDI_RXMIRROR:
  5326. return dahdi_ioctl_rxmirror(file, data);
  5327. case DAHDI_TXMIRROR:
  5328. return dahdi_ioctl_txmirror(file, data);
  5329. #endif /* CONFIG_DAHDI_MIRROR */
  5330. case DAHDI_DIALING:
  5331. spin_lock_irqsave(&chan->lock, flags);
  5332. j = chan->dialing;
  5333. spin_unlock_irqrestore(&chan->lock, flags);
  5334. if (copy_to_user(user_data, &j, sizeof(int)))
  5335. return -EFAULT;
  5336. return 0;
  5337. case DAHDI_DIAL:
  5338. return ioctl_dahdi_dial(chan, data);
  5339. case DAHDI_GET_BUFINFO:
  5340. memset(&stack.bi, 0, sizeof(stack.bi));
  5341. stack.bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
  5342. stack.bi.txbufpolicy = chan->txbufpolicy;
  5343. stack.bi.numbufs = chan->numbufs;
  5344. stack.bi.bufsize = chan->blocksize;
  5345. /* XXX FIXME! XXX */
  5346. stack.bi.readbufs = -1;
  5347. stack.bi.writebufs = -1;
  5348. if (copy_to_user(user_data, &stack.bi, sizeof(stack.bi)))
  5349. return -EFAULT;
  5350. break;
  5351. case DAHDI_SET_BUFINFO:
  5352. if (copy_from_user(&stack.bi, user_data, sizeof(stack.bi)))
  5353. return -EFAULT;
  5354. if (stack.bi.bufsize > DAHDI_MAX_BLOCKSIZE)
  5355. return -EINVAL;
  5356. if (stack.bi.bufsize < 16)
  5357. return -EINVAL;
  5358. if (stack.bi.bufsize * stack.bi.numbufs > DAHDI_MAX_BUF_SPACE)
  5359. return -EINVAL;
  5360. /* It does not make sense to allow user mode to change the
  5361. * receive buffering policy. DAHDI always provides received
  5362. * buffers to upper layers immediately. Transmission is
  5363. * different since we might want to allow the kernel to build
  5364. * up a buffer in order to prevent underruns from the
  5365. * interrupt context. */
  5366. chan->txbufpolicy = stack.bi.txbufpolicy & 0x3;
  5367. if ((rv = dahdi_reallocbufs(chan, stack.bi.bufsize, stack.bi.numbufs)))
  5368. return (rv);
  5369. break;
  5370. case DAHDI_GET_BLOCKSIZE: /* get blocksize */
  5371. /* return block size */
  5372. put_user(chan->blocksize, (int __user *)data);
  5373. break;
  5374. case DAHDI_SET_BLOCKSIZE: /* set blocksize */
  5375. get_user(j, (int __user *)data);
  5376. /* cannot be larger than max amount */
  5377. if (j > DAHDI_MAX_BLOCKSIZE) return(-EINVAL);
  5378. /* cannot be less then 16 */
  5379. if (j < 16) return(-EINVAL);
  5380. /* allocate a single kernel buffer which we then
  5381. sub divide into four pieces */
  5382. if ((rv = dahdi_reallocbufs(chan, j, chan->numbufs)))
  5383. return (rv);
  5384. break;
  5385. case DAHDI_FLUSH: /* flush input buffer, output buffer, and/or event queue */
  5386. get_user(i, (int __user *)data); /* get param */
  5387. spin_lock_irqsave(&chan->lock, flags);
  5388. if (i & DAHDI_FLUSH_READ) /* if for read (input) */
  5389. {
  5390. /* initialize read buffers and pointers */
  5391. chan->inreadbuf = 0;
  5392. chan->outreadbuf = -1;
  5393. for (j=0;j<chan->numbufs;j++) {
  5394. /* Do we need this? */
  5395. chan->readn[j] = 0;
  5396. chan->readidx[j] = 0;
  5397. }
  5398. wake_up_interruptible(&chan->waitq); /* wake_up_interruptible waiting on read */
  5399. }
  5400. if (i & DAHDI_FLUSH_WRITE) /* if for write (output) */
  5401. {
  5402. /* initialize write buffers and pointers */
  5403. chan->outwritebuf = -1;
  5404. chan->inwritebuf = 0;
  5405. for (j=0;j<chan->numbufs;j++) {
  5406. /* Do we need this? */
  5407. chan->writen[j] = 0;
  5408. chan->writeidx[j] = 0;
  5409. }
  5410. wake_up_interruptible(&chan->waitq); /* wake_up_interruptible waiting on write */
  5411. }
  5412. if (i & DAHDI_FLUSH_EVENT) /* if for events */
  5413. {
  5414. /* initialize the event pointers */
  5415. chan->eventinidx = chan->eventoutidx = 0;
  5416. }
  5417. spin_unlock_irqrestore(&chan->lock, flags);
  5418. break;
  5419. case DAHDI_SYNC: /* wait for no tx */
  5420. for(;;) /* loop forever */
  5421. {
  5422. spin_lock_irqsave(&chan->lock, flags);
  5423. /* Know if there is a write pending */
  5424. i = (chan->outwritebuf > -1);
  5425. spin_unlock_irqrestore(&chan->lock, flags);
  5426. if (!i)
  5427. break; /* skip if none */
  5428. rv = wait_event_interruptible(chan->waitq,
  5429. (!chan->file->private_data || chan->outwritebuf > -1));
  5430. if (rv)
  5431. return rv;
  5432. if (unlikely(!chan->file->private_data))
  5433. return -ENODEV;
  5434. }
  5435. break;
  5436. case DAHDI_IOMUX: /* wait for something to happen */
  5437. return dahdi_ioctl_iomux(file, data);
  5438. case DAHDI_GETEVENT: /* Get event on queue */
  5439. /* set up for no event */
  5440. j = DAHDI_EVENT_NONE;
  5441. spin_lock_irqsave(&chan->lock, flags);
  5442. /* if some event in queue */
  5443. if (chan->eventinidx != chan->eventoutidx)
  5444. {
  5445. j = chan->eventbuf[chan->eventoutidx++];
  5446. /* get the data, bump index */
  5447. /* if index overflow, set to beginning */
  5448. if (chan->eventoutidx >= DAHDI_MAX_EVENTSIZE)
  5449. chan->eventoutidx = 0;
  5450. }
  5451. spin_unlock_irqrestore(&chan->lock, flags);
  5452. put_user(j, (int __user *)data);
  5453. break;
  5454. case DAHDI_CONFMUTE: /* set confmute flag */
  5455. get_user(j, (int __user *)data); /* get conf # */
  5456. if (!(chan->flags & DAHDI_FLAG_AUDIO)) return (-EINVAL);
  5457. spin_lock_irqsave(&chan_lock, flags);
  5458. chan->confmute = j;
  5459. spin_unlock_irqrestore(&chan_lock, flags);
  5460. break;
  5461. case DAHDI_GETCONFMUTE: /* get confmute flag */
  5462. if (!(chan->flags & DAHDI_FLAG_AUDIO)) return (-EINVAL);
  5463. j = chan->confmute;
  5464. put_user(j, (int __user *)data); /* get conf # */
  5465. rv = 0;
  5466. break;
  5467. case DAHDI_SETTONEZONE:
  5468. get_user(j, (int __user *) data);
  5469. rv = set_tone_zone(chan, j);
  5470. return rv;
  5471. case DAHDI_GETTONEZONE:
  5472. spin_lock_irqsave(&chan->lock, flags);
  5473. j = (chan->curzone) ? chan->curzone->num : 0;
  5474. spin_unlock_irqrestore(&chan->lock, flags);
  5475. put_user(j, (int __user *) data);
  5476. break;
  5477. case DAHDI_SENDTONE:
  5478. get_user(j, (int __user *)data);
  5479. spin_lock_irqsave(&chan->lock, flags);
  5480. rv = start_tone(chan, j);
  5481. spin_unlock_irqrestore(&chan->lock, flags);
  5482. return rv;
  5483. case DAHDI_GETCONF_V1: /* intentional drop through */
  5484. case DAHDI_GETCONF: /* get conf stuff */
  5485. return dahdi_ioctl_getconf(file, data);
  5486. case DAHDI_SETCONF_V1: /* Intentional fall through. */
  5487. case DAHDI_SETCONF: /* set conf stuff */
  5488. return dahdi_ioctl_setconf(file, data);
  5489. case DAHDI_CONFDIAG_V1: /* Intentional fall-through */
  5490. case DAHDI_CONFDIAG: /* output diagnostic info to console */
  5491. return dahdi_ioctl_confdiag(file, data);
  5492. case DAHDI_CHANNO: /* get channel number of stream */
  5493. /* return channel number */
  5494. put_user(chan->channo, (int __user *)data);
  5495. break;
  5496. case DAHDI_SETLAW:
  5497. get_user(j, (int __user *)data);
  5498. if ((j < 0) || (j > DAHDI_LAW_ALAW))
  5499. return -EINVAL;
  5500. dahdi_set_law(chan, j);
  5501. break;
  5502. case DAHDI_SETLINEAR:
  5503. get_user(j, (int __user *)data);
  5504. /* Makes no sense on non-audio channels */
  5505. if (!(chan->flags & DAHDI_FLAG_AUDIO))
  5506. return -EINVAL;
  5507. if (j)
  5508. chan->flags |= DAHDI_FLAG_LINEAR;
  5509. else
  5510. chan->flags &= ~DAHDI_FLAG_LINEAR;
  5511. break;
  5512. case DAHDI_SETCADENCE:
  5513. if (data) {
  5514. /* Use specific ring cadence */
  5515. if (copy_from_user(&stack.cad, user_data,
  5516. sizeof(stack.cad))) {
  5517. return -EFAULT;
  5518. }
  5519. memcpy(chan->ringcadence, &stack.cad, sizeof(chan->ringcadence));
  5520. chan->firstcadencepos = 0;
  5521. /* Looking for negative ringing time indicating where to loop back into ringcadence */
  5522. for (i=0; i<DAHDI_MAX_CADENCE; i+=2 ) {
  5523. if (chan->ringcadence[i]<0) {
  5524. chan->ringcadence[i] *= -1;
  5525. chan->firstcadencepos = i;
  5526. break;
  5527. }
  5528. }
  5529. } else {
  5530. /* Reset to default */
  5531. chan->firstcadencepos = 0;
  5532. if (chan->curzone) {
  5533. memcpy(chan->ringcadence, chan->curzone->ringcadence, sizeof(chan->ringcadence));
  5534. /* Looking for negative ringing time indicating where to loop back into ringcadence */
  5535. for (i=0; i<DAHDI_MAX_CADENCE; i+=2 ) {
  5536. if (chan->ringcadence[i]<0) {
  5537. chan->ringcadence[i] *= -1;
  5538. chan->firstcadencepos = i;
  5539. break;
  5540. }
  5541. }
  5542. } else {
  5543. memset(chan->ringcadence, 0, sizeof(chan->ringcadence));
  5544. chan->ringcadence[0] = chan->starttime;
  5545. chan->ringcadence[1] = DAHDI_RINGOFFTIME;
  5546. }
  5547. }
  5548. break;
  5549. default:
  5550. /* Check for common ioctl's and private ones */
  5551. rv = dahdi_common_ioctl(file, cmd, data);
  5552. /* if no span, just return with value */
  5553. if (!chan->span) return rv;
  5554. if ((rv == -ENOTTY) && chan->span->ops->ioctl)
  5555. rv = chan->span->ops->ioctl(chan, cmd, data);
  5556. return rv;
  5557. }
  5558. return 0;
  5559. }
  5560. #ifdef CONFIG_DAHDI_PPP
  5561. /*
  5562. * This is called at softirq (BH) level when there are calls
  5563. * we need to make to the ppp_generic layer. We do it this
  5564. * way because the ppp_generic layer functions may not be called
  5565. * at interrupt level.
  5566. */
  5567. static void do_ppp_calls(unsigned long data)
  5568. {
  5569. struct dahdi_chan *chan = (struct dahdi_chan *) data;
  5570. struct sk_buff *skb;
  5571. if (!chan->ppp)
  5572. return;
  5573. if (chan->do_ppp_wakeup) {
  5574. chan->do_ppp_wakeup = 0;
  5575. ppp_output_wakeup(chan->ppp);
  5576. }
  5577. while ((skb = skb_dequeue(&chan->ppp_rq)) != NULL)
  5578. ppp_input(chan->ppp, skb);
  5579. if (chan->do_ppp_error) {
  5580. chan->do_ppp_error = 0;
  5581. ppp_input_error(chan->ppp, 0);
  5582. }
  5583. }
  5584. #endif
  5585. static int
  5586. ioctl_echocancel(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
  5587. const void __user *data)
  5588. {
  5589. struct dahdi_echocan_state *ec = NULL, *ec_state;
  5590. const struct dahdi_echocan_factory *ec_current;
  5591. struct dahdi_echocanparam *params;
  5592. int ret = 0;
  5593. unsigned long flags;
  5594. if (ecp->param_count > DAHDI_MAX_ECHOCANPARAMS)
  5595. return -E2BIG;
  5596. if (ecp->tap_length == 0) {
  5597. /* disable mode, don't need to inspect params */
  5598. mutex_lock(&chan->mutex);
  5599. spin_lock_irqsave(&chan->lock, flags);
  5600. ec_state = chan->ec_state;
  5601. chan->ec_state = NULL;
  5602. ec_current = chan->ec_current;
  5603. chan->ec_current = NULL;
  5604. spin_unlock_irqrestore(&chan->lock, flags);
  5605. if (ec_state) {
  5606. ec_state->ops->echocan_free(chan, ec_state);
  5607. release_echocan(ec_current);
  5608. }
  5609. mutex_unlock(&chan->mutex);
  5610. return 0;
  5611. }
  5612. params = kmalloc(sizeof(params[0]) * DAHDI_MAX_ECHOCANPARAMS, GFP_KERNEL);
  5613. if (!params)
  5614. return -ENOMEM;
  5615. /* enable mode, need the params */
  5616. if (copy_from_user(params, data,
  5617. sizeof(params[0]) * ecp->param_count)) {
  5618. ret = -EFAULT;
  5619. goto exit_with_free;
  5620. }
  5621. mutex_lock(&chan->mutex);
  5622. /* free any echocan that may be on the channel already */
  5623. spin_lock_irqsave(&chan->lock, flags);
  5624. ec_state = chan->ec_state;
  5625. chan->ec_state = NULL;
  5626. ec_current = chan->ec_current;
  5627. chan->ec_current = NULL;
  5628. spin_unlock_irqrestore(&chan->lock, flags);
  5629. if (ec_state) {
  5630. ec_state->ops->echocan_free(chan, ec_state);
  5631. release_echocan(ec_current);
  5632. }
  5633. switch (ecp->tap_length) {
  5634. case 32:
  5635. case 64:
  5636. case 128:
  5637. case 256:
  5638. case 512:
  5639. case 1024:
  5640. break;
  5641. default:
  5642. ecp->tap_length = deftaps;
  5643. }
  5644. ec_current = NULL;
  5645. if (chan->ec_factory) {
  5646. /* try to get another reference to the module providing
  5647. this channel's echo canceler */
  5648. if (!try_module_get(chan->ec_factory->owner)) {
  5649. module_printk(KERN_ERR, "Cannot get a reference to the"
  5650. " '%s' echo canceler\n",
  5651. chan->ec_factory->get_name(chan));
  5652. goto exit_with_free;
  5653. }
  5654. /* got the reference, copy the pointer and use it for making
  5655. an echo canceler instance if possible */
  5656. ec_current = chan->ec_factory;
  5657. ret = ec_current->echocan_create(chan, ecp, params, &ec);
  5658. if (ret) {
  5659. release_echocan(ec_current);
  5660. goto exit_with_free;
  5661. }
  5662. if (!ec) {
  5663. module_printk(KERN_ERR, "%s failed to allocate an " \
  5664. "dahdi_echocan_state instance.\n",
  5665. ec_current->get_name(chan));
  5666. ret = -EFAULT;
  5667. goto exit_with_free;
  5668. }
  5669. }
  5670. if (ec) {
  5671. spin_lock_irqsave(&chan->lock, flags);
  5672. chan->ec_current = ec_current;
  5673. chan->ec_state = ec;
  5674. ec->status.mode = ECHO_MODE_ACTIVE;
  5675. if (!ec->features.CED_tx_detect) {
  5676. echo_can_disable_detector_init(&chan->ec_state->txecdis);
  5677. }
  5678. if (!ec->features.CED_rx_detect) {
  5679. echo_can_disable_detector_init(&chan->ec_state->rxecdis);
  5680. }
  5681. spin_unlock_irqrestore(&chan->lock, flags);
  5682. }
  5683. exit_with_free:
  5684. mutex_unlock(&chan->mutex);
  5685. kfree(params);
  5686. return ret;
  5687. }
  5688. static void set_echocan_fax_mode(struct dahdi_chan *chan, unsigned int channo, const char *reason, unsigned int enable)
  5689. {
  5690. if (enable) {
  5691. if (!chan->ec_state)
  5692. module_printk(KERN_NOTICE, "Ignoring FAX mode request because of %s for channel %d with no echo canceller\n", reason, channo);
  5693. else if (chan->ec_state->status.mode == ECHO_MODE_FAX)
  5694. module_printk(KERN_NOTICE, "Ignoring FAX mode request because of %s for echo canceller already in FAX mode on channel %d\n", reason, channo);
  5695. else if (chan->ec_state->status.mode != ECHO_MODE_ACTIVE)
  5696. module_printk(KERN_NOTICE, "Ignoring FAX mode request because of %s for echo canceller not in active mode on channel %d\n", reason, channo);
  5697. else if (chan->ec_state->features.NLP_automatic) {
  5698. /* for echocans that automatically do the right thing, just
  5699. * mark it as being in FAX mode without making any
  5700. * changes, as none are necessary.
  5701. */
  5702. chan->ec_state->status.mode = ECHO_MODE_FAX;
  5703. } else if (chan->ec_state->features.NLP_toggle) {
  5704. module_printk(KERN_NOTICE, "Disabled echo canceller NLP because of %s on channel %d\n", reason, channo);
  5705. dahdi_qevent_nolock(chan, DAHDI_EVENT_EC_NLP_DISABLED);
  5706. chan->ec_state->ops->echocan_NLP_toggle(chan->ec_state, 0);
  5707. chan->ec_state->status.mode = ECHO_MODE_FAX;
  5708. } else {
  5709. module_printk(KERN_NOTICE, "Idled echo canceller because of %s on channel %d\n", reason, channo);
  5710. chan->ec_state->status.mode = ECHO_MODE_IDLE;
  5711. }
  5712. } else {
  5713. if (!chan->ec_state)
  5714. module_printk(KERN_NOTICE, "Ignoring voice mode request because of %s for channel %d with no echo canceller\n", reason, channo);
  5715. else if (chan->ec_state->status.mode == ECHO_MODE_ACTIVE)
  5716. module_printk(KERN_NOTICE, "Ignoring voice mode request because of %s for echo canceller already in voice mode on channel %d\n", reason, channo);
  5717. else if ((chan->ec_state->status.mode != ECHO_MODE_FAX) &&
  5718. (chan->ec_state->status.mode != ECHO_MODE_IDLE))
  5719. module_printk(KERN_NOTICE, "Ignoring voice mode request because of %s for echo canceller not in FAX or idle mode on channel %d\n", reason, channo);
  5720. else if (chan->ec_state->features.NLP_automatic) {
  5721. /* for echocans that automatically do the right thing, just
  5722. * mark it as being in active mode without making any
  5723. * changes, as none are necessary.
  5724. */
  5725. chan->ec_state->status.mode = ECHO_MODE_ACTIVE;
  5726. } else if (chan->ec_state->features.NLP_toggle) {
  5727. module_printk(KERN_NOTICE, "Enabled echo canceller NLP because of %s on channel %d\n", reason, channo);
  5728. dahdi_qevent_nolock(chan, DAHDI_EVENT_EC_NLP_ENABLED);
  5729. chan->ec_state->ops->echocan_NLP_toggle(chan->ec_state, 1);
  5730. chan->ec_state->status.mode = ECHO_MODE_ACTIVE;
  5731. } else {
  5732. module_printk(KERN_NOTICE, "Activated echo canceller because of %s on channel %d\n", reason, channo);
  5733. chan->ec_state->status.mode = ECHO_MODE_ACTIVE;
  5734. }
  5735. }
  5736. }
  5737. static inline bool
  5738. is_txstate(struct dahdi_chan *const chan, const int txstate)
  5739. {
  5740. bool ret;
  5741. unsigned long flags;
  5742. spin_lock_irqsave(&chan->lock, flags);
  5743. ret = (txstate == chan->txstate);
  5744. spin_unlock_irqrestore(&chan->lock, flags);
  5745. return ret;
  5746. }
  5747. static int dahdi_chan_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  5748. {
  5749. struct dahdi_chan *const chan = chan_from_file(file);
  5750. unsigned long flags;
  5751. int j;
  5752. int ret;
  5753. int oldconf;
  5754. const void *rxgain = NULL;
  5755. if (!chan)
  5756. return -ENOSYS;
  5757. WARN_ON(!chan->master);
  5758. switch(cmd) {
  5759. case DAHDI_SETSIGFREEZE:
  5760. get_user(j, (int __user *)data);
  5761. spin_lock_irqsave(&chan->lock, flags);
  5762. if (j) {
  5763. chan->flags |= DAHDI_FLAG_SIGFREEZE;
  5764. } else {
  5765. chan->flags &= ~DAHDI_FLAG_SIGFREEZE;
  5766. }
  5767. spin_unlock_irqrestore(&chan->lock, flags);
  5768. break;
  5769. case DAHDI_GETSIGFREEZE:
  5770. spin_lock_irqsave(&chan->lock, flags);
  5771. if (chan->flags & DAHDI_FLAG_SIGFREEZE)
  5772. j = 1;
  5773. else
  5774. j = 0;
  5775. spin_unlock_irqrestore(&chan->lock, flags);
  5776. put_user(j, (int __user *)data);
  5777. break;
  5778. case DAHDI_AUDIOMODE:
  5779. /* Only literal clear channels can be put in */
  5780. if (chan->sig != DAHDI_SIG_CLEAR) return (-EINVAL);
  5781. get_user(j, (int __user *)data);
  5782. if (j) {
  5783. spin_lock_irqsave(&chan->lock, flags);
  5784. chan->flags |= DAHDI_FLAG_AUDIO;
  5785. chan->flags &= ~(DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS);
  5786. spin_unlock_irqrestore(&chan->lock, flags);
  5787. } else {
  5788. /* Coming out of audio mode, also clear all
  5789. conferencing and gain related info as well
  5790. as echo canceller */
  5791. struct dahdi_echocan_state *ec_state;
  5792. const struct dahdi_echocan_factory *ec_current;
  5793. spin_lock_irqsave(&chan->lock, flags);
  5794. chan->flags &= ~DAHDI_FLAG_AUDIO;
  5795. /* save old conf number, if any */
  5796. oldconf = chan->confna;
  5797. /* initialize conference variables */
  5798. chan->_confn = 0;
  5799. chan->confna = 0;
  5800. chan->conf_chan = NULL;
  5801. dahdi_disable_dacs(chan);
  5802. chan->confmode = 0;
  5803. chan->confmute = 0;
  5804. memset(chan->conflast, 0, sizeof(chan->conflast));
  5805. memset(chan->conflast1, 0, sizeof(chan->conflast1));
  5806. memset(chan->conflast2, 0, sizeof(chan->conflast2));
  5807. ec_state = chan->ec_state;
  5808. chan->ec_state = NULL;
  5809. ec_current = chan->ec_current;
  5810. chan->ec_current = NULL;
  5811. /* release conference resource, if any to release */
  5812. reset_conf(chan);
  5813. if (is_gain_allocated(chan))
  5814. rxgain = chan->rxgain;
  5815. else
  5816. rxgain = NULL;
  5817. chan->rxgain = defgain;
  5818. chan->txgain = defgain;
  5819. spin_unlock_irqrestore(&chan->lock, flags);
  5820. if (ec_state) {
  5821. ec_state->ops->echocan_free(chan, ec_state);
  5822. release_echocan(ec_current);
  5823. }
  5824. if (rxgain)
  5825. kfree(rxgain);
  5826. if (oldconf) dahdi_check_conf(oldconf);
  5827. }
  5828. #ifdef DAHDI_AUDIO_NOTIFY
  5829. if (chan->span->ops->audio_notify)
  5830. chan->span->ops->audio_notify(chan, j);
  5831. #endif
  5832. break;
  5833. case DAHDI_HDLCPPP:
  5834. #ifdef CONFIG_DAHDI_PPP
  5835. if (chan->sig != DAHDI_SIG_CLEAR) return (-EINVAL);
  5836. get_user(j, (int __user *)data);
  5837. if (j) {
  5838. if (!chan->ppp) {
  5839. chan->ppp = kzalloc(sizeof(struct ppp_channel), GFP_KERNEL);
  5840. if (chan->ppp) {
  5841. struct dahdi_echocan_state *tec;
  5842. const struct dahdi_echocan_factory *ec_current;
  5843. chan->ppp->private = chan;
  5844. chan->ppp->ops = &ztppp_ops;
  5845. chan->ppp->mtu = DAHDI_DEFAULT_MTU_MRU;
  5846. chan->ppp->hdrlen = 0;
  5847. skb_queue_head_init(&chan->ppp_rq);
  5848. chan->do_ppp_wakeup = 0;
  5849. tasklet_init(&chan->ppp_calls, do_ppp_calls,
  5850. (unsigned long)chan);
  5851. if ((ret = dahdi_reallocbufs(chan, DAHDI_DEFAULT_MTU_MRU, DAHDI_DEFAULT_NUM_BUFS))) {
  5852. kfree(chan->ppp);
  5853. chan->ppp = NULL;
  5854. return ret;
  5855. }
  5856. if ((ret = ppp_register_channel(chan->ppp))) {
  5857. kfree(chan->ppp);
  5858. chan->ppp = NULL;
  5859. return ret;
  5860. }
  5861. tec = chan->ec_state;
  5862. chan->ec_state = NULL;
  5863. ec_current = chan->ec_current;
  5864. chan->ec_current = NULL;
  5865. /* Make sure there's no gain */
  5866. if (is_gain_allocated(chan))
  5867. kfree(chan->rxgain);
  5868. chan->rxgain = defgain;
  5869. chan->txgain = defgain;
  5870. chan->flags &= ~DAHDI_FLAG_AUDIO;
  5871. chan->flags |= (DAHDI_FLAG_PPP | DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS);
  5872. if (tec) {
  5873. tec->ops->echocan_free(chan, tec);
  5874. release_echocan(ec_current);
  5875. }
  5876. } else
  5877. return -ENOMEM;
  5878. }
  5879. } else {
  5880. chan->flags &= ~(DAHDI_FLAG_PPP | DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS);
  5881. if (chan->ppp) {
  5882. struct ppp_channel *ppp = chan->ppp;
  5883. chan->ppp = NULL;
  5884. tasklet_kill(&chan->ppp_calls);
  5885. skb_queue_purge(&chan->ppp_rq);
  5886. ppp_unregister_channel(ppp);
  5887. kfree(ppp);
  5888. }
  5889. }
  5890. #else
  5891. module_printk(KERN_NOTICE, "PPP support not compiled in\n");
  5892. return -ENOSYS;
  5893. #endif
  5894. break;
  5895. case DAHDI_HDLCRAWMODE:
  5896. if (chan->sig != DAHDI_SIG_CLEAR) return (-EINVAL);
  5897. get_user(j, (int __user *)data);
  5898. chan->flags &= ~(DAHDI_FLAG_AUDIO | DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS);
  5899. if (j) {
  5900. chan->flags |= DAHDI_FLAG_HDLC;
  5901. fasthdlc_init(&chan->rxhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
  5902. fasthdlc_init(&chan->txhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
  5903. }
  5904. break;
  5905. case DAHDI_HDLCFCSMODE:
  5906. if (chan->sig != DAHDI_SIG_CLEAR) return (-EINVAL);
  5907. get_user(j, (int __user *)data);
  5908. chan->flags &= ~(DAHDI_FLAG_AUDIO | DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS);
  5909. if (j) {
  5910. chan->flags |= DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS;
  5911. fasthdlc_init(&chan->rxhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
  5912. fasthdlc_init(&chan->txhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
  5913. }
  5914. break;
  5915. case DAHDI_HDLC_RATE:
  5916. get_user(j, (int __user *)data);
  5917. if (j == 56) {
  5918. chan->flags |= DAHDI_FLAG_HDLC56;
  5919. } else {
  5920. chan->flags &= ~DAHDI_FLAG_HDLC56;
  5921. }
  5922. fasthdlc_init(&chan->rxhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
  5923. fasthdlc_init(&chan->txhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
  5924. break;
  5925. case DAHDI_ECHOCANCEL_PARAMS:
  5926. {
  5927. struct dahdi_echocanparams ecp;
  5928. if (!(chan->flags & DAHDI_FLAG_AUDIO))
  5929. return -EINVAL;
  5930. ret = copy_from_user(&ecp,
  5931. (struct dahdi_echocanparams __user *)data,
  5932. sizeof(ecp));
  5933. if (ret)
  5934. return -EFAULT;
  5935. data += sizeof(ecp);
  5936. ret = ioctl_echocancel(chan, &ecp, (void __user *)data);
  5937. if (ret)
  5938. return ret;
  5939. break;
  5940. }
  5941. case DAHDI_ECHOCANCEL:
  5942. {
  5943. struct dahdi_echocanparams ecp;
  5944. if (!(chan->flags & DAHDI_FLAG_AUDIO))
  5945. return -EINVAL;
  5946. get_user(j, (int __user *) data);
  5947. ecp.tap_length = j;
  5948. ecp.param_count = 0;
  5949. if ((ret = ioctl_echocancel(chan, &ecp, NULL)))
  5950. return ret;
  5951. break;
  5952. }
  5953. case DAHDI_ECHOTRAIN:
  5954. /* get pre-training time from user */
  5955. get_user(j, (int __user *)data);
  5956. if ((j < 0) || (j >= DAHDI_MAX_PRETRAINING))
  5957. return -EINVAL;
  5958. j <<= 3;
  5959. spin_lock_irqsave(&chan->lock, flags);
  5960. if (chan->ec_state) {
  5961. /* Start pretraining stage */
  5962. if (chan->ec_state->ops->echocan_traintap) {
  5963. chan->ec_state->status.mode = ECHO_MODE_PRETRAINING;
  5964. chan->ec_state->status.pretrain_timer = j;
  5965. }
  5966. spin_unlock_irqrestore(&chan->lock, flags);
  5967. } else {
  5968. spin_unlock_irqrestore(&chan->lock, flags);
  5969. return -EINVAL;
  5970. }
  5971. break;
  5972. case DAHDI_ECHOCANCEL_FAX_MODE:
  5973. if (!chan->ec_state) {
  5974. return -EINVAL;
  5975. } else {
  5976. get_user(j, (int __user *) data);
  5977. spin_lock_irqsave(&chan->lock, flags);
  5978. set_echocan_fax_mode(chan, chan->channo, "ioctl", j ? 1 : 0);
  5979. spin_unlock_irqrestore(&chan->lock, flags);
  5980. }
  5981. break;
  5982. case DAHDI_SETTXBITS:
  5983. if (chan->sig != DAHDI_SIG_CAS)
  5984. return -EINVAL;
  5985. get_user(j, (int __user *)data);
  5986. dahdi_cas_setbits(chan, j);
  5987. break;
  5988. case DAHDI_GETRXBITS:
  5989. put_user(chan->rxsig, (int __user *)data);
  5990. break;
  5991. case DAHDI_LOOPBACK:
  5992. get_user(j, (int __user *)data);
  5993. spin_lock_irqsave(&chan->lock, flags);
  5994. if (j)
  5995. chan->flags |= DAHDI_FLAG_LOOPED;
  5996. else
  5997. chan->flags &= ~DAHDI_FLAG_LOOPED;
  5998. spin_unlock_irqrestore(&chan->lock, flags);
  5999. break;
  6000. case DAHDI_HOOK:
  6001. get_user(j, (int __user *)data);
  6002. if (chan->flags & DAHDI_FLAG_CLEAR)
  6003. return -EINVAL;
  6004. if (chan->sig == DAHDI_SIG_CAS)
  6005. return -EINVAL;
  6006. /* if no span, just do nothing */
  6007. if (!chan->span) return(0);
  6008. spin_lock_irqsave(&chan->lock, flags);
  6009. /* if dialing, stop it */
  6010. chan->curtone = NULL;
  6011. chan->dialing = 0;
  6012. chan->txdialbuf[0] = '\0';
  6013. chan->tonep = 0;
  6014. chan->pdialcount = 0;
  6015. spin_unlock_irqrestore(&chan->lock, flags);
  6016. if (chan->span->flags & DAHDI_FLAG_RBS) {
  6017. switch (j) {
  6018. case DAHDI_ONHOOK:
  6019. spin_lock_irqsave(&chan->lock, flags);
  6020. dahdi_hangup(chan);
  6021. spin_unlock_irqrestore(&chan->lock, flags);
  6022. break;
  6023. case DAHDI_OFFHOOK:
  6024. spin_lock_irqsave(&chan->lock, flags);
  6025. if ((chan->txstate == DAHDI_TXSTATE_KEWL) ||
  6026. (chan->txstate == DAHDI_TXSTATE_AFTERKEWL)) {
  6027. spin_unlock_irqrestore(&chan->lock, flags);
  6028. return -EBUSY;
  6029. }
  6030. dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_DEBOUNCE, chan->debouncetime);
  6031. spin_unlock_irqrestore(&chan->lock, flags);
  6032. break;
  6033. case DAHDI_RING:
  6034. case DAHDI_START:
  6035. spin_lock_irqsave(&chan->lock, flags);
  6036. if (!chan->curzone) {
  6037. spin_unlock_irqrestore(&chan->lock, flags);
  6038. module_printk(KERN_WARNING, "Cannot start tone until a tone zone is loaded.\n");
  6039. return -ENODATA;
  6040. }
  6041. if (chan->txstate != DAHDI_TXSTATE_ONHOOK) {
  6042. spin_unlock_irqrestore(&chan->lock, flags);
  6043. return -EBUSY;
  6044. }
  6045. if (chan->sig & __DAHDI_SIG_FXO) {
  6046. ret = 0;
  6047. chan->cadencepos = 0;
  6048. ret = chan->ringcadence[0];
  6049. dahdi_rbs_sethook(chan, DAHDI_TXSIG_START, DAHDI_TXSTATE_RINGON, ret);
  6050. } else
  6051. dahdi_rbs_sethook(chan, DAHDI_TXSIG_START, DAHDI_TXSTATE_START, chan->starttime);
  6052. spin_unlock_irqrestore(&chan->lock, flags);
  6053. if (file->f_flags & O_NONBLOCK)
  6054. return -EINPROGRESS;
  6055. break;
  6056. case DAHDI_WINK:
  6057. spin_lock_irqsave(&chan->lock, flags);
  6058. if (chan->txstate != DAHDI_TXSTATE_ONHOOK) {
  6059. spin_unlock_irqrestore(&chan->lock, flags);
  6060. return -EBUSY;
  6061. }
  6062. dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_PREWINK, chan->prewinktime);
  6063. spin_unlock_irqrestore(&chan->lock, flags);
  6064. if (file->f_flags & O_NONBLOCK)
  6065. return -EINPROGRESS;
  6066. wait_event_interruptible(chan->waitq,
  6067. !chan->file->private_data || is_txstate(chan, DAHDI_TXSIG_ONHOOK));
  6068. if (unlikely(!chan->file->private_data))
  6069. return -ENODEV;
  6070. if (signal_pending(current))
  6071. return -ERESTARTSYS;
  6072. break;
  6073. case DAHDI_FLASH:
  6074. spin_lock_irqsave(&chan->lock, flags);
  6075. if (chan->txstate != DAHDI_TXSTATE_OFFHOOK) {
  6076. spin_unlock_irqrestore(&chan->lock, flags);
  6077. return -EBUSY;
  6078. }
  6079. dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_PREFLASH, chan->preflashtime);
  6080. spin_unlock_irqrestore(&chan->lock, flags);
  6081. if (file->f_flags & O_NONBLOCK)
  6082. return -EINPROGRESS;
  6083. wait_event_interruptible(chan->waitq,
  6084. !chan->file->private_data || is_txstate(chan, DAHDI_TXSIG_OFFHOOK));
  6085. if (unlikely(!chan->file->private_data))
  6086. return -ENODEV;
  6087. if (signal_pending(current))
  6088. return -ERESTARTSYS;
  6089. break;
  6090. case DAHDI_RINGOFF:
  6091. spin_lock_irqsave(&chan->lock, flags);
  6092. dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_ONHOOK, 0);
  6093. spin_unlock_irqrestore(&chan->lock, flags);
  6094. break;
  6095. default:
  6096. return -EINVAL;
  6097. }
  6098. } else if (chan->span->ops->sethook) {
  6099. if (chan->txhooksig != j) {
  6100. chan->txhooksig = j;
  6101. chan->span->ops->sethook(chan, j);
  6102. }
  6103. } else
  6104. return -ENOSYS;
  6105. break;
  6106. #ifdef CONFIG_DAHDI_PPP
  6107. case PPPIOCGCHAN:
  6108. if (chan->flags & DAHDI_FLAG_PPP) {
  6109. return put_user(ppp_channel_index(chan->ppp),
  6110. (int __user *)data) ? -EFAULT : 0;
  6111. } else {
  6112. return -EINVAL;
  6113. }
  6114. break;
  6115. case PPPIOCGUNIT:
  6116. if (chan->flags & DAHDI_FLAG_PPP) {
  6117. return put_user(ppp_unit_number(chan->ppp),
  6118. (int __user *)data) ? -EFAULT : 0;
  6119. } else {
  6120. return -EINVAL;
  6121. }
  6122. break;
  6123. #endif
  6124. case DAHDI_BUFFER_EVENTS:
  6125. if (get_user(j, (int __user *)data))
  6126. return -EFAULT;
  6127. if (j)
  6128. set_bit(DAHDI_FLAGBIT_BUFEVENTS, &chan->flags);
  6129. else
  6130. clear_bit(DAHDI_FLAGBIT_BUFEVENTS, &chan->flags);
  6131. break;
  6132. default:
  6133. return dahdi_chanandpseudo_ioctl(file, cmd, data);
  6134. }
  6135. return 0;
  6136. }
  6137. static int dahdi_prechan_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  6138. {
  6139. int channo;
  6140. int res;
  6141. if (file->private_data) {
  6142. module_printk(KERN_NOTICE, "Huh? Prechan already has private data??\n");
  6143. }
  6144. switch(cmd) {
  6145. case DAHDI_SPECIFY:
  6146. get_user(channo, (int __user *)data);
  6147. file->private_data = chan_from_num(channo);
  6148. if (!file->private_data)
  6149. return -EINVAL;
  6150. res = dahdi_specchan_open(file);
  6151. if (res)
  6152. file->private_data = NULL;
  6153. return res;
  6154. default:
  6155. return -ENOSYS;
  6156. }
  6157. return 0;
  6158. }
  6159. static long
  6160. dahdi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  6161. {
  6162. int unit = UNIT(file);
  6163. int ret;
  6164. if (unit == DAHDI_CTL) {
  6165. ret = dahdi_ctl_ioctl(file, cmd, data);
  6166. goto exit;
  6167. }
  6168. if (unit == DAHDI_TRANSCODE) {
  6169. /* dahdi_transcode should have updated the file_operations on
  6170. * this file object on open, so we shouldn't be here. */
  6171. WARN_ON(1);
  6172. ret = -EFAULT;
  6173. goto exit;
  6174. }
  6175. if (unit == DAHDI_TIMER) {
  6176. /* The file operations for a timer device should have been
  6177. * updated. */
  6178. WARN_ON(1);
  6179. ret = -EFAULT;
  6180. goto exit;
  6181. }
  6182. if (unit == DAHDI_CHANNEL) {
  6183. if (file->private_data)
  6184. ret = dahdi_chan_ioctl(file, cmd, data);
  6185. else
  6186. ret = dahdi_prechan_ioctl(file, cmd, data);
  6187. goto exit;
  6188. }
  6189. if (unit == DAHDI_PSEUDO) {
  6190. if (!file->private_data) {
  6191. module_printk(KERN_NOTICE, "No pseudo channel structure to read?\n");
  6192. ret = -EINVAL;
  6193. goto exit;
  6194. }
  6195. ret = dahdi_chanandpseudo_ioctl(file, cmd, data);
  6196. goto exit;
  6197. }
  6198. if (!file->private_data) {
  6199. ret = -ENXIO;
  6200. goto exit;
  6201. }
  6202. ret = dahdi_chan_ioctl(file, cmd, data);
  6203. exit:
  6204. return ret;
  6205. }
  6206. #ifndef HAVE_UNLOCKED_IOCTL
  6207. static int dahdi_ioctl(struct inode *inode, struct file *file,
  6208. unsigned int cmd, unsigned long data)
  6209. {
  6210. return dahdi_unlocked_ioctl(file, cmd, data);
  6211. }
  6212. #endif
  6213. #ifdef HAVE_COMPAT_IOCTL
  6214. static long dahdi_ioctl_compat(struct file *file, unsigned int cmd,
  6215. unsigned long data)
  6216. {
  6217. if (cmd == DAHDI_SFCONFIG)
  6218. return -ENOTTY; /* Not supported yet */
  6219. return dahdi_unlocked_ioctl(file, cmd, data);
  6220. }
  6221. #endif
  6222. /**
  6223. * _get_next_channo - Return the next taken channel number from the span list.
  6224. * @span: The span with which to start the search.
  6225. *
  6226. * Returns -1 if there aren't any channels on span or any of the following
  6227. * spans, otherwise, returns the channel number of the first channel.
  6228. *
  6229. * Must be callled with registration_mutex held.
  6230. *
  6231. */
  6232. static unsigned int _get_next_channo(const struct dahdi_span *span)
  6233. {
  6234. const struct list_head *pos = &span->spans_node;
  6235. while (pos != &span_list) {
  6236. span = list_entry(pos, struct dahdi_span, spans_node);
  6237. if (span->channels)
  6238. return span->chans[0]->channo;
  6239. pos = pos->next;
  6240. }
  6241. return -1;
  6242. }
  6243. static void
  6244. set_spanno_and_basechan(struct dahdi_span *span, u32 spanno, u32 basechan)
  6245. {
  6246. int i;
  6247. dahdi_dev_dbg(ASSIGN, span_device(span),
  6248. "set: spanno=%d, basechan=%d (span->channels=%d)\n",
  6249. spanno, basechan, span->channels);
  6250. span->spanno = spanno;
  6251. for (i = 0; i < span->channels; ++i)
  6252. span->chans[i]->channo = basechan + i;
  6253. }
  6254. /**
  6255. * _assign_spanno_and_basechan - Assign next available span and channel numbers.
  6256. *
  6257. * This function will set span->spanno and channo for all the member channels.
  6258. * It will assign the first available location.
  6259. *
  6260. * Must be called with registration_mutex held.
  6261. *
  6262. */
  6263. static int _assign_spanno_and_basechan(struct dahdi_span *span)
  6264. {
  6265. struct dahdi_span *pos;
  6266. unsigned int next_channo;
  6267. unsigned int spanno = 1;
  6268. unsigned int basechan = 1;
  6269. dahdi_dev_dbg(ASSIGN, span_device(span),
  6270. "assign: channels=%d\n", span->channels);
  6271. list_for_each_entry(pos, &span_list, spans_node) {
  6272. if (pos->spanno <= spanno) {
  6273. spanno = pos->spanno + 1;
  6274. basechan = pos->chans[0]->channo + pos->channels;
  6275. continue;
  6276. }
  6277. next_channo = _get_next_channo(pos);
  6278. if ((basechan + span->channels) <= next_channo)
  6279. break;
  6280. /* We can't fit here, let's look at the next location. */
  6281. spanno = pos->spanno + 1;
  6282. if (pos->channels)
  6283. basechan = pos->chans[0]->channo + pos->channels;
  6284. }
  6285. dahdi_dev_dbg(ASSIGN, span_device(span),
  6286. "good: spanno=%d, basechan=%d (span->channels=%d)\n",
  6287. spanno, basechan, span->channels);
  6288. set_spanno_and_basechan(span, spanno, basechan);
  6289. return 0;
  6290. }
  6291. static inline struct dahdi_span *span_from_node(struct list_head *node)
  6292. {
  6293. return container_of(node, struct dahdi_span, spans_node);
  6294. }
  6295. /*
  6296. * Call with registration_mutex held. Make sure all the spans are on the list
  6297. * ordered by span.
  6298. *
  6299. */
  6300. static void _dahdi_add_span_to_span_list(struct dahdi_span *span)
  6301. {
  6302. unsigned long flags;
  6303. struct dahdi_span *pos;
  6304. if (list_empty(&span_list)) {
  6305. list_add_tail(&span->spans_node, &span_list);
  6306. return;
  6307. }
  6308. list_for_each_entry(pos, &span_list, spans_node) {
  6309. WARN_ON(0 == pos->spanno);
  6310. if (pos->spanno > span->spanno)
  6311. break;
  6312. }
  6313. spin_lock_irqsave(&chan_lock, flags);
  6314. list_add(&span->spans_node, pos->spans_node.prev);
  6315. spin_unlock_irqrestore(&chan_lock, flags);
  6316. }
  6317. /**
  6318. * _check_spanno_and_basechan - Check if we can fit the new span in the requested location.
  6319. *
  6320. * Must be called with registration_mutex held.
  6321. *
  6322. */
  6323. static int
  6324. _check_spanno_and_basechan(struct dahdi_span *span, u32 spanno, u32 basechan)
  6325. {
  6326. struct dahdi_span *pos;
  6327. unsigned int next_channo;
  6328. dahdi_dev_dbg(ASSIGN, span_device(span),
  6329. "check: spanno=%d, basechan=%d (span->channels=%d)\n",
  6330. spanno, basechan, span->channels);
  6331. list_for_each_entry(pos, &span_list, spans_node) {
  6332. next_channo = _get_next_channo(pos);
  6333. dahdi_dev_dbg(ASSIGN, span_device(span),
  6334. "pos: spanno=%d channels=%d (next_channo=%d)\n",
  6335. pos->spanno, pos->channels, next_channo);
  6336. if (pos->spanno <= spanno) {
  6337. if (basechan < next_channo + pos->channels) {
  6338. /* Requested basechan breaks channel sorting */
  6339. dev_notice(span_device(span),
  6340. "[%d] basechan (%d) is too low for wanted span %d\n",
  6341. local_spanno(span), basechan, spanno);
  6342. return -EINVAL;
  6343. }
  6344. continue;
  6345. }
  6346. if (next_channo == -1)
  6347. break;
  6348. if ((basechan + span->channels) <= next_channo)
  6349. break;
  6350. /* Cannot fit the span into the requested location. Abort. */
  6351. dev_notice(span_device(span),
  6352. "cannot fit span %d (basechan=%d) into requested location\n",
  6353. spanno, basechan);
  6354. return -EINVAL;
  6355. }
  6356. dahdi_dev_dbg(ASSIGN, span_device(span),
  6357. "good: spanno=%d, basechan=%d (span->channels=%d)\n",
  6358. spanno, basechan, span->channels);
  6359. set_spanno_and_basechan(span, spanno, basechan);
  6360. return 0;
  6361. }
  6362. struct dahdi_device *dahdi_create_device(void)
  6363. {
  6364. struct dahdi_device *ddev;
  6365. ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
  6366. if (!ddev)
  6367. return NULL;
  6368. INIT_LIST_HEAD(&ddev->spans);
  6369. dahdi_sysfs_init_device(ddev);
  6370. return ddev;
  6371. }
  6372. EXPORT_SYMBOL(dahdi_create_device);
  6373. void dahdi_free_device(struct dahdi_device *ddev)
  6374. {
  6375. put_device(&ddev->dev);
  6376. }
  6377. EXPORT_SYMBOL(dahdi_free_device);
  6378. /**
  6379. * __dahdi_init_span - Setup all the data structures for the span.
  6380. * @span: The span of interest.
  6381. *
  6382. */
  6383. static void __dahdi_init_span(struct dahdi_span *span)
  6384. {
  6385. int x;
  6386. INIT_LIST_HEAD(&span->spans_node);
  6387. spin_lock_init(&span->lock);
  6388. clear_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags);
  6389. if (!span->deflaw) {
  6390. module_printk(KERN_NOTICE, "Span %s didn't specify default "
  6391. "law. Assuming mulaw, please fix driver!\n",
  6392. span->name);
  6393. span->deflaw = DAHDI_LAW_MULAW;
  6394. }
  6395. if (span->spantype == SPANTYPE_INVALID) {
  6396. module_printk(KERN_NOTICE,
  6397. "Warning: Span %s didn't specify a spantype. "
  6398. "Please fix driver!\n", span->name);
  6399. }
  6400. for (x = 0; x < span->channels; ++x) {
  6401. span->chans[x]->span = span;
  6402. __dahdi_init_chan(span->chans[x]);
  6403. }
  6404. }
  6405. /**
  6406. * dahdi_init_span - (Re)Initializes a dahdi span.
  6407. * @span: The span to initialize.
  6408. *
  6409. * Reinitializing a device span might be necessary if a span has been changed
  6410. * (channels added / removed) between when the dahdi_device it is on was first
  6411. * registered and when the spans are actually assigned.
  6412. *
  6413. */
  6414. void dahdi_init_span(struct dahdi_span *span)
  6415. {
  6416. mutex_lock(&registration_mutex);
  6417. __dahdi_init_span(span);
  6418. mutex_unlock(&registration_mutex);
  6419. }
  6420. EXPORT_SYMBOL(dahdi_init_span);
  6421. /**
  6422. * _dahdi_assign_span() - Assign a new DAHDI span
  6423. * @span: the DAHDI span
  6424. * @spanno: The span number we would like assigned. If 0, the first
  6425. * available spanno/basechan will be used.
  6426. * @basechan: The base channel number we would like. Ignored if spanno is 0.
  6427. * @prefmaster: will the new span be preferred as a master_span?
  6428. *
  6429. * Assigns a span for usage with DAHDI. All the channel numbers in it will
  6430. * have their numbering started at basechan.
  6431. *
  6432. * If prefmaster is set to anything > 0, span will attempt to become the
  6433. * master DAHDI span at registration time. If 0: it will only become
  6434. * master if no other span is currently the master (i.e.: it is the
  6435. * first one).
  6436. *
  6437. * Must be called with registration_mutex held, and the span must have already
  6438. * been initialized ith the __dahdi_init_span call.
  6439. *
  6440. */
  6441. static int _dahdi_assign_span(struct dahdi_span *span, unsigned int spanno,
  6442. unsigned int basechan, int prefmaster)
  6443. {
  6444. int res = 0;
  6445. unsigned int x;
  6446. unsigned long flags;
  6447. if (!span || !span->ops || !span->ops->owner)
  6448. return -EFAULT;
  6449. if (test_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags)) {
  6450. dev_notice(span_device(span),
  6451. "local span %d is already assigned span %d\n",
  6452. local_spanno(span), span->spanno);
  6453. return -EINVAL;
  6454. }
  6455. /* DAHDI_ALARM_NOTOPEN can be set when a span is disabled, i.e. via
  6456. * sysfs, so when the span is being reassigned we should make sure it's
  6457. * cleared. This eliminates the need for board drivers to re-report
  6458. * their alarm states on span reassignment. */
  6459. spin_lock_irqsave(&span->lock, flags);
  6460. span->alarms &= ~DAHDI_ALARM_NOTOPEN;
  6461. dahdi_alarm_notify(span);
  6462. spin_unlock_irqrestore(&span->lock, flags);
  6463. if (span->ops->enable_hw_preechocan ||
  6464. span->ops->disable_hw_preechocan) {
  6465. if ((NULL == span->ops->enable_hw_preechocan) ||
  6466. (NULL == span->ops->disable_hw_preechocan)) {
  6467. dev_notice(span_device(span),
  6468. "span with inconsistent enable/disable hw_preechocan");
  6469. return -EFAULT;
  6470. }
  6471. }
  6472. if (!span->deflaw) {
  6473. module_printk(KERN_NOTICE, "Span %s didn't specify default law. "
  6474. "Assuming mulaw, please fix driver!\n", span->name);
  6475. span->deflaw = DAHDI_LAW_MULAW;
  6476. }
  6477. /* Look through the span list to find the first available span number.
  6478. * The spans are kept on this list in sorted order. We'll also save
  6479. * off the next available channel number to use. */
  6480. if (0 == spanno)
  6481. res = _assign_spanno_and_basechan(span);
  6482. else
  6483. res = _check_spanno_and_basechan(span, spanno, basechan);
  6484. if (res)
  6485. return res;
  6486. for (x = 0; x < span->channels; x++)
  6487. dahdi_chan_reg(span->chans[x]);
  6488. #ifdef CONFIG_PROC_FS
  6489. {
  6490. char tempfile[17];
  6491. snprintf(tempfile, sizeof(tempfile), "%d", span->spanno);
  6492. span->proc_entry = proc_create_data(tempfile, 0444,
  6493. root_proc_entry, &dahdi_proc_ops,
  6494. (void *)((unsigned long)span->spanno));
  6495. if (!span->proc_entry) {
  6496. res = -EFAULT;
  6497. span_err(span, "Error creating procfs entry\n");
  6498. goto cleanup;
  6499. }
  6500. }
  6501. #endif
  6502. res = span_sysfs_create(span);
  6503. if (res)
  6504. goto cleanup;
  6505. if (debug & DEBUG_MAIN) {
  6506. module_printk(KERN_NOTICE, "Registered Span %d ('%s') with "
  6507. "%d channels\n", span->spanno, span->name, span->channels);
  6508. }
  6509. _dahdi_add_span_to_span_list(span);
  6510. set_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags);
  6511. if (span->ops->assigned)
  6512. span->ops->assigned(span);
  6513. __dahdi_find_master_span();
  6514. return 0;
  6515. cleanup:
  6516. #ifdef CONFIG_PROC_FS
  6517. if (span->proc_entry) {
  6518. char tempfile[17];
  6519. snprintf(tempfile, sizeof(tempfile), "dahdi/%d", span->spanno);
  6520. remove_proc_entry(tempfile, NULL);
  6521. span->proc_entry = NULL;
  6522. }
  6523. #endif
  6524. for (x = 0; x < span->channels; x++) {
  6525. struct dahdi_chan *chan = span->chans[x];
  6526. if (test_bit(DAHDI_FLAGBIT_REGISTERED, &chan->flags))
  6527. dahdi_chan_unreg(chan);
  6528. }
  6529. return res;
  6530. }
  6531. int dahdi_assign_span(struct dahdi_span *span, unsigned int spanno,
  6532. unsigned int basechan, int prefmaster)
  6533. {
  6534. int ret;
  6535. mutex_lock(&registration_mutex);
  6536. ret = _dahdi_assign_span(span, spanno, basechan, prefmaster);
  6537. mutex_unlock(&registration_mutex);
  6538. return ret;
  6539. }
  6540. int dahdi_assign_device_spans(struct dahdi_device *ddev)
  6541. {
  6542. struct dahdi_span *span;
  6543. mutex_lock(&registration_mutex);
  6544. list_for_each_entry(span, &ddev->spans, device_node)
  6545. _dahdi_assign_span(span, 0, 0, 1);
  6546. mutex_unlock(&registration_mutex);
  6547. return 0;
  6548. }
  6549. static int auto_assign_spans = 1;
  6550. static const char *UNKNOWN = "";
  6551. /**
  6552. * dahdi_auto_assign_spans - is the parameter auto_assign_spans set?
  6553. */
  6554. int dahdi_get_auto_assign_spans(void)
  6555. {
  6556. return auto_assign_spans;
  6557. }
  6558. EXPORT_SYMBOL(dahdi_get_auto_assign_spans);
  6559. /**
  6560. * _dahdi_register_device - Registers a DAHDI device and assign its spans.
  6561. * @ddev: the DAHDI device
  6562. *
  6563. * If auto_assign_spans is 0, add the device to the device list and wait for
  6564. * userspace to finish registration. Otherwise, go ahead and register the
  6565. * spans in order as was done historically.
  6566. *
  6567. * Must hold registration_mutex when this function is called.
  6568. *
  6569. */
  6570. static int _dahdi_register_device(struct dahdi_device *ddev,
  6571. struct device *parent)
  6572. {
  6573. struct dahdi_span *s;
  6574. int ret;
  6575. ddev->manufacturer = (ddev->manufacturer) ?: UNKNOWN;
  6576. ddev->location = (ddev->location) ?: UNKNOWN;
  6577. ddev->devicetype = (ddev->devicetype) ?: UNKNOWN;
  6578. list_for_each_entry(s, &ddev->spans, device_node) {
  6579. s->parent = ddev;
  6580. s->spanno = 0;
  6581. __dahdi_init_span(s);
  6582. }
  6583. ktime_get_ts(&ddev->registration_time);
  6584. ret = dahdi_sysfs_add_device(ddev, parent);
  6585. if (ret)
  6586. return ret;
  6587. if (!auto_assign_spans)
  6588. return 0;
  6589. list_for_each_entry(s, &ddev->spans, device_node)
  6590. ret = _dahdi_assign_span(s, 0, 0, 1);
  6591. if (ret)
  6592. dahdi_sysfs_unregister_device(ddev);
  6593. return ret;
  6594. }
  6595. /**
  6596. * dahdi_register_device() - unregister a new DAHDI device
  6597. * @ddev: the DAHDI device
  6598. *
  6599. * Registers a device for usage with DAHDI.
  6600. *
  6601. */
  6602. int dahdi_register_device(struct dahdi_device *ddev, struct device *parent)
  6603. {
  6604. int ret;
  6605. if (!ddev)
  6606. return -EINVAL;
  6607. mutex_lock(&registration_mutex);
  6608. ret = _dahdi_register_device(ddev, parent);
  6609. mutex_unlock(&registration_mutex);
  6610. return ret;
  6611. }
  6612. EXPORT_SYMBOL(dahdi_register_device);
  6613. static void disable_span(struct dahdi_span *span)
  6614. {
  6615. int x;
  6616. unsigned long flags;
  6617. spin_lock_irqsave(&span->lock, flags);
  6618. span->alarms = DAHDI_ALARM_NOTOPEN;
  6619. for (x = 0; x < span->channels; x++) {
  6620. /*
  6621. * This event may not make it to user space before the channel
  6622. * is gone, but let's try.
  6623. */
  6624. dahdi_qevent_lock(span->chans[x], DAHDI_EVENT_REMOVED);
  6625. }
  6626. dahdi_alarm_notify(span);
  6627. spin_unlock_irqrestore(&span->lock, flags);
  6628. module_printk(KERN_INFO, "%s: span %d\n", __func__, span->spanno);
  6629. }
  6630. #ifdef CONFIG_PROC_FS
  6631. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
  6632. static inline void proc_remove(struct proc_dir_entry *proc_entry)
  6633. {
  6634. remove_proc_entry(proc_entry->name, root_proc_entry);
  6635. }
  6636. #endif
  6637. #endif
  6638. /**
  6639. * _dahdi_unassign_span() - unassign a DAHDI span
  6640. * @span: the DAHDI span
  6641. *
  6642. * Unassigns a span that has been previously assigned with
  6643. * dahdi_assign_span().
  6644. *
  6645. * Must be called with the registration_mutex held.
  6646. *
  6647. */
  6648. static int _dahdi_unassign_span(struct dahdi_span *span)
  6649. {
  6650. int res;
  6651. int x;
  6652. struct dahdi_span *new_master, *s;
  6653. unsigned long flags;
  6654. if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags)) {
  6655. dev_info(span_device(span),
  6656. "local span %d is already unassigned\n",
  6657. local_spanno(span));
  6658. return -EINVAL;
  6659. }
  6660. spin_lock_irqsave(&chan_lock, flags);
  6661. list_del_init(&span->spans_node);
  6662. spin_unlock_irqrestore(&chan_lock, flags);
  6663. span->spanno = 0;
  6664. clear_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags);
  6665. res = dahdi_shutdown_span(span);
  6666. if (res) {
  6667. dev_err(span_device(span),
  6668. "Failed to shutdown when unassigning.\n");
  6669. }
  6670. if (debug & DEBUG_MAIN)
  6671. module_printk(KERN_NOTICE, "Unassigning Span '%s' with %d channels\n", span->name, span->channels);
  6672. #ifdef CONFIG_PROC_FS
  6673. if (span->proc_entry) {
  6674. proc_remove(span->proc_entry);
  6675. span->proc_entry = NULL;
  6676. }
  6677. #endif /* CONFIG_PROC_FS */
  6678. span_sysfs_remove(span);
  6679. for (x=0;x<span->channels;x++)
  6680. dahdi_chan_unreg(span->chans[x]);
  6681. new_master = master_span; /* FIXME: locking */
  6682. if (master_span == span)
  6683. new_master = NULL;
  6684. spin_lock_irqsave(&chan_lock, flags);
  6685. list_for_each_entry(s, &span_list, spans_node) {
  6686. if ((s == new_master) || !can_provide_timing(s))
  6687. continue;
  6688. new_master = s;
  6689. break;
  6690. }
  6691. spin_unlock_irqrestore(&chan_lock, flags);
  6692. if (master_span != new_master) {
  6693. if (debug & DEBUG_MAIN) {
  6694. module_printk(KERN_NOTICE, "%s: Span ('%s') is new master\n", __FUNCTION__,
  6695. (new_master)? new_master->name: "no master");
  6696. }
  6697. }
  6698. master_span = new_master;
  6699. return 0;
  6700. }
  6701. static int open_channel_count(const struct dahdi_span *span)
  6702. {
  6703. int i;
  6704. int open_channels = 0;
  6705. struct dahdi_chan *chan;
  6706. for (i = 0; i < span->channels; ++i) {
  6707. chan = span->chans[i];
  6708. if (test_bit(DAHDI_FLAGBIT_OPEN, &chan->flags))
  6709. ++open_channels;
  6710. }
  6711. return open_channels;
  6712. }
  6713. int dahdi_unassign_span(struct dahdi_span *span)
  6714. {
  6715. int ret;
  6716. module_printk(KERN_NOTICE, "%s: %s\n", __func__, span->name);
  6717. disable_span(span);
  6718. if (open_channel_count(span) > 0)
  6719. msleep(1000); /* Give user space a chance to read this */
  6720. mutex_lock(&registration_mutex);
  6721. ret = _dahdi_unassign_span(span);
  6722. mutex_unlock(&registration_mutex);
  6723. return ret;
  6724. }
  6725. /**
  6726. * dahdi_unregister_device() - unregister a DAHDI device
  6727. * @span: the DAHDI span
  6728. *
  6729. * Unregisters a device that has been previously registered with
  6730. * dahdi_register_device().
  6731. *
  6732. */
  6733. void dahdi_unregister_device(struct dahdi_device *ddev)
  6734. {
  6735. struct dahdi_span *s;
  6736. struct dahdi_span *next;
  6737. unsigned int spans_with_open_channels = 0;
  6738. WARN_ON(!ddev);
  6739. might_sleep();
  6740. if (unlikely(!ddev))
  6741. return;
  6742. list_for_each_entry_safe(s, next, &ddev->spans, device_node) {
  6743. disable_span(s);
  6744. if (open_channel_count(s) > 0)
  6745. ++spans_with_open_channels;
  6746. }
  6747. if (spans_with_open_channels > 0)
  6748. msleep(1000); /* give user space a chance to read this */
  6749. mutex_lock(&registration_mutex);
  6750. list_for_each_entry_safe(s, next, &ddev->spans, device_node) {
  6751. _dahdi_unassign_span(s);
  6752. list_del_init(&s->device_node);
  6753. }
  6754. mutex_unlock(&registration_mutex);
  6755. dahdi_sysfs_unregister_device(ddev);
  6756. if (UNKNOWN == ddev->location)
  6757. ddev->location = NULL;
  6758. if (UNKNOWN == ddev->manufacturer)
  6759. ddev->manufacturer = NULL;
  6760. if (UNKNOWN == ddev->devicetype)
  6761. ddev->devicetype = NULL;
  6762. }
  6763. EXPORT_SYMBOL(dahdi_unregister_device);
  6764. /*
  6765. ** This routine converts from linear to ulaw
  6766. **
  6767. ** Craig Reese: IDA/Supercomputing Research Center
  6768. ** Joe Campbell: Department of Defense
  6769. ** 29 September 1989
  6770. **
  6771. ** References:
  6772. ** 1) CCITT Recommendation G.711 (very difficult to follow)
  6773. ** 2) "A New Digital Technique for Implementation of Any
  6774. ** Continuous PCM Companding Law," Villeret, Michel,
  6775. ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
  6776. ** 1973, pg. 11.12-11.17
  6777. ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
  6778. ** for Analog-to_Digital Conversion Techniques,"
  6779. ** 17 February 1987
  6780. **
  6781. ** Input: Signed 16 bit linear sample
  6782. ** Output: 8 bit ulaw sample
  6783. */
  6784. #define ZEROTRAP /* turn on the trap as per the MIL-STD */
  6785. #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
  6786. #define CLIP 32635
  6787. #ifdef CONFIG_CALC_XLAW
  6788. unsigned char
  6789. #else
  6790. static unsigned char __init
  6791. #endif
  6792. __dahdi_lineartoulaw(short sample)
  6793. {
  6794. static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  6795. 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  6796. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  6797. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  6798. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  6799. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  6800. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  6801. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  6802. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  6803. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  6804. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  6805. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  6806. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  6807. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  6808. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  6809. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
  6810. int sign, exponent, mantissa;
  6811. unsigned char ulawbyte;
  6812. /* Get the sample into sign-magnitude. */
  6813. sign = (sample >> 8) & 0x80; /* set aside the sign */
  6814. if (sign != 0) sample = -sample; /* get magnitude */
  6815. if (sample > CLIP) sample = CLIP; /* clip the magnitude */
  6816. /* Convert from 16 bit linear to ulaw. */
  6817. sample = sample + BIAS;
  6818. exponent = exp_lut[(sample >> 7) & 0xFF];
  6819. mantissa = (sample >> (exponent + 3)) & 0x0F;
  6820. ulawbyte = ~(sign | (exponent << 4) | mantissa);
  6821. #ifdef ZEROTRAP
  6822. if (ulawbyte == 0) ulawbyte = 0x02; /* optional CCITT trap */
  6823. #endif
  6824. if (ulawbyte == 0xff) ulawbyte = 0x7f; /* never return 0xff */
  6825. return(ulawbyte);
  6826. }
  6827. #define AMI_MASK 0x55
  6828. #ifdef CONFIG_CALC_XLAW
  6829. unsigned char
  6830. #else
  6831. static inline unsigned char __init
  6832. #endif
  6833. __dahdi_lineartoalaw (short linear)
  6834. {
  6835. int mask;
  6836. int seg;
  6837. int pcm_val;
  6838. static int seg_end[8] =
  6839. {
  6840. 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
  6841. };
  6842. pcm_val = linear;
  6843. if (pcm_val >= 0)
  6844. {
  6845. /* Sign (7th) bit = 1 */
  6846. mask = AMI_MASK | 0x80;
  6847. }
  6848. else
  6849. {
  6850. /* Sign bit = 0 */
  6851. mask = AMI_MASK;
  6852. pcm_val = -pcm_val;
  6853. }
  6854. /* Convert the scaled magnitude to segment number. */
  6855. for (seg = 0; seg < 8; seg++)
  6856. {
  6857. if (pcm_val <= seg_end[seg])
  6858. break;
  6859. }
  6860. /* Combine the sign, segment, and quantization bits. */
  6861. return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask;
  6862. }
  6863. /*- End of function --------------------------------------------------------*/
  6864. static inline short int __init alaw2linear (uint8_t alaw)
  6865. {
  6866. int i;
  6867. int seg;
  6868. alaw ^= AMI_MASK;
  6869. i = ((alaw & 0x0F) << 4);
  6870. seg = (((int) alaw & 0x70) >> 4);
  6871. if (seg)
  6872. i = (i + 0x100) << (seg - 1);
  6873. return (short int) ((alaw & 0x80) ? i : -i);
  6874. }
  6875. /*- End of function --------------------------------------------------------*/
  6876. static void __init dahdi_conv_init(void)
  6877. {
  6878. int i;
  6879. /*
  6880. * Set up mu-law conversion table
  6881. */
  6882. for(i = 0;i < 256;i++)
  6883. {
  6884. short mu,e,f,y;
  6885. static short etab[]={0,132,396,924,1980,4092,8316,16764};
  6886. mu = 255-i;
  6887. e = (mu & 0x70)/16;
  6888. f = mu & 0x0f;
  6889. y = f * (1 << (e + 3));
  6890. y += etab[e];
  6891. if (mu & 0x80) y = -y;
  6892. __dahdi_mulaw[i] = y;
  6893. __dahdi_alaw[i] = alaw2linear(i);
  6894. /* Default (0.0 db) gain table */
  6895. defgain[i] = i;
  6896. }
  6897. #ifndef CONFIG_CALC_XLAW
  6898. /* set up the reverse (mu-law) conversion table */
  6899. for(i = -32768; i < 32768; i += 4)
  6900. {
  6901. __dahdi_lin2mu[((unsigned short)(short)i) >> 2] = __dahdi_lineartoulaw(i);
  6902. __dahdi_lin2a[((unsigned short)(short)i) >> 2] = __dahdi_lineartoalaw(i);
  6903. }
  6904. #endif
  6905. }
  6906. static inline void __dahdi_process_getaudio_chunk(struct dahdi_chan *ss, unsigned char *txb)
  6907. {
  6908. /* We transmit data from our master channel */
  6909. /* Called with ss->lock held */
  6910. struct dahdi_chan *ms = ss->master;
  6911. /* Linear representation */
  6912. short getlin[DAHDI_CHUNKSIZE], k[DAHDI_CHUNKSIZE];
  6913. int x;
  6914. /* Okay, now we've got something to transmit */
  6915. for (x=0;x<DAHDI_CHUNKSIZE;x++)
  6916. getlin[x] = DAHDI_XLAW(txb[x], ms);
  6917. #ifndef CONFIG_DAHDI_NO_ECHOCAN_DISABLE
  6918. if (ms->ec_state && (ms->ec_state->status.mode == ECHO_MODE_ACTIVE) && !ms->ec_state->features.CED_tx_detect) {
  6919. for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
  6920. if (echo_can_disable_detector_update(&ms->ec_state->txecdis, getlin[x])) {
  6921. set_echocan_fax_mode(ms, ss->channo, "CED tx detected", 1);
  6922. dahdi_qevent_nolock(ms, DAHDI_EVENT_TX_CED_DETECTED);
  6923. break;
  6924. }
  6925. }
  6926. }
  6927. #endif
  6928. if ((!ms->confmute && !ms->dialing) || (is_pseudo_chan(ms))) {
  6929. struct dahdi_chan *const conf_chan = ms->conf_chan;
  6930. /* Handle conferencing on non-clear channel and non-HDLC channels */
  6931. switch(ms->confmode & DAHDI_CONF_MODE_MASK) {
  6932. case DAHDI_CONF_NORMAL:
  6933. /* Do nuffin */
  6934. break;
  6935. case DAHDI_CONF_MONITOR: /* Monitor a channel's rx mode */
  6936. /* if a pseudo-channel, ignore */
  6937. if (is_pseudo_chan(ms))
  6938. break;
  6939. /* Add monitored channel */
  6940. if (is_pseudo_chan(conf_chan))
  6941. ACSS(getlin, conf_chan->getlin);
  6942. else
  6943. ACSS(getlin, conf_chan->putlin);
  6944. for (x=0;x<DAHDI_CHUNKSIZE;x++)
  6945. txb[x] = DAHDI_LIN2X(getlin[x], ms);
  6946. break;
  6947. case DAHDI_CONF_MONITORTX: /* Monitor a channel's tx mode */
  6948. /* if a pseudo-channel, ignore */
  6949. if (is_pseudo_chan(ms))
  6950. break;
  6951. /* Add monitored channel */
  6952. if (is_pseudo_chan(conf_chan))
  6953. ACSS(getlin, conf_chan->putlin);
  6954. else
  6955. ACSS(getlin, conf_chan->getlin);
  6956. for (x=0;x<DAHDI_CHUNKSIZE;x++)
  6957. txb[x] = DAHDI_LIN2X(getlin[x], ms);
  6958. break;
  6959. case DAHDI_CONF_MONITORBOTH: /* monitor a channel's rx and tx mode */
  6960. /* if a pseudo-channel, ignore */
  6961. if (is_pseudo_chan(ms))
  6962. break;
  6963. ACSS(getlin, conf_chan->putlin);
  6964. ACSS(getlin, conf_chan->getlin);
  6965. for (x=0;x<DAHDI_CHUNKSIZE;x++)
  6966. txb[x] = DAHDI_LIN2X(getlin[x], ms);
  6967. break;
  6968. case DAHDI_CONF_MONITOR_RX_PREECHO: /* Monitor a channel's rx mode */
  6969. /* if a pseudo-channel, ignore */
  6970. if (is_pseudo_chan(ms))
  6971. break;
  6972. if (!conf_chan->readchunkpreec)
  6973. break;
  6974. /* Add monitored channel */
  6975. ACSS(getlin, is_pseudo_chan(conf_chan) ?
  6976. conf_chan->readchunkpreec : conf_chan->putlin);
  6977. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  6978. txb[x] = DAHDI_LIN2X(getlin[x], ms);
  6979. break;
  6980. case DAHDI_CONF_MONITOR_TX_PREECHO: /* Monitor a channel's tx mode */
  6981. /* if a pseudo-channel, ignore */
  6982. if (is_pseudo_chan(ms))
  6983. break;
  6984. if (!conf_chan->readchunkpreec)
  6985. break;
  6986. /* Add monitored channel */
  6987. ACSS(getlin, is_pseudo_chan(conf_chan) ?
  6988. conf_chan->putlin : conf_chan->readchunkpreec);
  6989. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  6990. txb[x] = DAHDI_LIN2X(getlin[x], ms);
  6991. break;
  6992. case DAHDI_CONF_MONITORBOTH_PREECHO: /* monitor a channel's rx and tx mode */
  6993. /* if a pseudo-channel, ignore */
  6994. if (is_pseudo_chan(ms))
  6995. break;
  6996. if (!conf_chan->readchunkpreec)
  6997. break;
  6998. ACSS(getlin, conf_chan->putlin);
  6999. ACSS(getlin, conf_chan->readchunkpreec);
  7000. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  7001. txb[x] = DAHDI_LIN2X(getlin[x], ms);
  7002. break;
  7003. case DAHDI_CONF_REALANDPSEUDO:
  7004. /* This strange mode takes the transmit buffer and
  7005. puts it on the conference, minus its last sample,
  7006. then outputs from the conference minus the
  7007. real channel's last sample. */
  7008. /* if to talk on conf */
  7009. if (ms->confmode & DAHDI_CONF_PSEUDO_TALKER) {
  7010. /* Store temp value */
  7011. memcpy(k, getlin, DAHDI_CHUNKSIZE * sizeof(short));
  7012. /* Add conf value */
  7013. ACSS(k, conf_sums_next[ms->_confn]);
  7014. /* save last one */
  7015. memcpy(ms->conflast2, ms->conflast1, DAHDI_CHUNKSIZE * sizeof(short));
  7016. memcpy(ms->conflast1, k, DAHDI_CHUNKSIZE * sizeof(short));
  7017. /* get amount actually added */
  7018. SCSS(ms->conflast1, conf_sums_next[ms->_confn]);
  7019. /* Really add in new value */
  7020. ACSS(conf_sums_next[ms->_confn], ms->conflast1);
  7021. } else {
  7022. memset(ms->conflast1, 0, DAHDI_CHUNKSIZE * sizeof(short));
  7023. memset(ms->conflast2, 0, DAHDI_CHUNKSIZE * sizeof(short));
  7024. }
  7025. memset(getlin, 0, DAHDI_CHUNKSIZE * sizeof(short));
  7026. txb[0] = DAHDI_LIN2X(0, ms);
  7027. memset(txb + 1, txb[0], DAHDI_CHUNKSIZE - 1);
  7028. /* fall through to normal conf mode */
  7029. case DAHDI_CONF_CONF: /* Normal conference mode */
  7030. if (is_pseudo_chan(ms)) /* if pseudo-channel */
  7031. {
  7032. /* if to talk on conf */
  7033. if (ms->confmode & DAHDI_CONF_TALKER) {
  7034. /* Store temp value */
  7035. memcpy(k, getlin, DAHDI_CHUNKSIZE * sizeof(short));
  7036. /* Add conf value */
  7037. ACSS(k, conf_sums[ms->_confn]);
  7038. /* get amount actually added */
  7039. memcpy(ms->conflast, k, DAHDI_CHUNKSIZE * sizeof(short));
  7040. SCSS(ms->conflast, conf_sums[ms->_confn]);
  7041. /* Really add in new value */
  7042. ACSS(conf_sums[ms->_confn], ms->conflast);
  7043. memcpy(ms->getlin, getlin, DAHDI_CHUNKSIZE * sizeof(short));
  7044. } else {
  7045. memset(ms->conflast, 0, DAHDI_CHUNKSIZE * sizeof(short));
  7046. memcpy(getlin, ms->getlin, DAHDI_CHUNKSIZE * sizeof(short));
  7047. }
  7048. txb[0] = DAHDI_LIN2X(0, ms);
  7049. memset(txb + 1, txb[0], DAHDI_CHUNKSIZE - 1);
  7050. break;
  7051. }
  7052. /* fall through */
  7053. case DAHDI_CONF_CONFMON: /* Conference monitor mode */
  7054. if (ms->confmode & DAHDI_CONF_LISTENER) {
  7055. /* Subtract out last sample written to conf */
  7056. SCSS(getlin, ms->conflast);
  7057. /* Add in conference */
  7058. ACSS(getlin, conf_sums[ms->_confn]);
  7059. }
  7060. for (x=0;x<DAHDI_CHUNKSIZE;x++)
  7061. txb[x] = DAHDI_LIN2X(getlin[x], ms);
  7062. break;
  7063. case DAHDI_CONF_CONFANN:
  7064. case DAHDI_CONF_CONFANNMON:
  7065. /* First, add tx buffer to conf */
  7066. ACSS(conf_sums_next[ms->_confn], getlin);
  7067. /* Start with silence */
  7068. memset(getlin, 0, DAHDI_CHUNKSIZE * sizeof(short));
  7069. /* If a listener on the conf... */
  7070. if (ms->confmode & DAHDI_CONF_LISTENER) {
  7071. /* Subtract last value written */
  7072. SCSS(getlin, ms->conflast);
  7073. /* Add in conf */
  7074. ACSS(getlin, conf_sums[ms->_confn]);
  7075. }
  7076. for (x=0;x<DAHDI_CHUNKSIZE;x++)
  7077. txb[x] = DAHDI_LIN2X(getlin[x], ms);
  7078. break;
  7079. case DAHDI_CONF_DIGITALMON:
  7080. /* Real digital monitoring, but still echo cancel if
  7081. * desired */
  7082. if (!conf_chan)
  7083. break;
  7084. if (is_pseudo_chan(conf_chan)) {
  7085. if (ms->ec_state) {
  7086. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  7087. txb[x] = DAHDI_LIN2X(conf_chan->getlin[x], ms);
  7088. } else {
  7089. memcpy(txb, conf_chan->getraw, DAHDI_CHUNKSIZE);
  7090. }
  7091. } else {
  7092. if (ms->ec_state) {
  7093. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  7094. txb[x] = DAHDI_LIN2X(conf_chan->putlin[x], ms);
  7095. } else {
  7096. memcpy(txb, conf_chan->putraw,
  7097. DAHDI_CHUNKSIZE);
  7098. }
  7099. }
  7100. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  7101. getlin[x] = DAHDI_XLAW(txb[x], ms);
  7102. break;
  7103. }
  7104. }
  7105. if (ms->confmute || (ms->ec_state && (ms->ec_state->status.mode) & __ECHO_MODE_MUTE)) {
  7106. txb[0] = DAHDI_LIN2X(0, ms);
  7107. memset(txb + 1, txb[0], DAHDI_CHUNKSIZE - 1);
  7108. if (ms->ec_state && (ms->ec_state->status.mode == ECHO_MODE_STARTTRAINING)) {
  7109. /* Transmit impulse now */
  7110. txb[0] = DAHDI_LIN2X(16384, ms);
  7111. ms->ec_state->status.mode = ECHO_MODE_AWAITINGECHO;
  7112. }
  7113. }
  7114. /* save value from current */
  7115. memcpy(ms->getlin, getlin, DAHDI_CHUNKSIZE * sizeof(short));
  7116. /* save value from current */
  7117. memcpy(ms->getraw, txb, DAHDI_CHUNKSIZE);
  7118. /* if to make tx tone */
  7119. if (ms->v1_1 || ms->v2_1 || ms->v3_1)
  7120. {
  7121. for (x=0;x<DAHDI_CHUNKSIZE;x++)
  7122. {
  7123. getlin[x] += dahdi_txtone_nextsample(ms);
  7124. txb[x] = DAHDI_LIN2X(getlin[x], ms);
  7125. }
  7126. }
  7127. /* This is what to send (after having applied gain) */
  7128. for (x=0;x<DAHDI_CHUNKSIZE;x++)
  7129. txb[x] = ms->txgain[txb[x]];
  7130. }
  7131. static void __putbuf_chunk(struct dahdi_chan *ss, unsigned char *rxb,
  7132. int bytes);
  7133. static inline void __dahdi_getbuf_chunk(struct dahdi_chan *ss, unsigned char *txb)
  7134. {
  7135. #ifdef CONFIG_DAHDI_MIRROR
  7136. unsigned char *orig_txb = txb;
  7137. #endif /* CONFIG_DAHDI_MIRROR */
  7138. /* Called with ss->lock held */
  7139. /* We transmit data from our master channel */
  7140. struct dahdi_chan *ms = ss->master;
  7141. /* Buffer we're using */
  7142. unsigned char *buf;
  7143. /* Old buffer number */
  7144. int oldbuf;
  7145. /* Linear representation */
  7146. int getlin;
  7147. /* How many bytes we need to process */
  7148. int bytes = DAHDI_CHUNKSIZE, left;
  7149. bool needtxunderrun = false;
  7150. int x;
  7151. /* Let's pick something to transmit. First source to
  7152. try is our write-out buffer. Always check it first because
  7153. its our 'fast path' for whatever that's worth. */
  7154. while(bytes) {
  7155. if ((ms->outwritebuf > -1) && !ms->txdisable) {
  7156. buf= ms->writebuf[ms->outwritebuf];
  7157. left = ms->writen[ms->outwritebuf] - ms->writeidx[ms->outwritebuf];
  7158. if (left > bytes)
  7159. left = bytes;
  7160. if (ms->flags & DAHDI_FLAG_HDLC) {
  7161. /* If this is an HDLC channel we only send a byte of
  7162. HDLC. */
  7163. for(x=0;x<left;x++) {
  7164. if (fasthdlc_tx_need_data(&ms->txhdlc))
  7165. /* Load a byte of data only if needed */
  7166. fasthdlc_tx_load_nocheck(&ms->txhdlc, buf[ms->writeidx[ms->outwritebuf]++]);
  7167. *(txb++) = fasthdlc_tx_run_nocheck(&ms->txhdlc);
  7168. }
  7169. bytes -= left;
  7170. } else {
  7171. memcpy(txb, buf + ms->writeidx[ms->outwritebuf], left);
  7172. ms->writeidx[ms->outwritebuf]+=left;
  7173. txb += left;
  7174. bytes -= left;
  7175. }
  7176. /* Check buffer status */
  7177. if (ms->writeidx[ms->outwritebuf] >= ms->writen[ms->outwritebuf]) {
  7178. /* We've reached the end of our buffer. Go to the next. */
  7179. oldbuf = ms->outwritebuf;
  7180. /* Clear out write index and such */
  7181. ms->writeidx[oldbuf] = 0;
  7182. ms->outwritebuf = (ms->outwritebuf + 1) % ms->numbufs;
  7183. if (!(ms->flags & DAHDI_FLAG_MTP2)) {
  7184. ms->writen[oldbuf] = 0;
  7185. if (ms->outwritebuf == ms->inwritebuf) {
  7186. /* Whoopsies, we're run out of buffers. Mark ours
  7187. as -1 and wait for the filler to notify us that
  7188. there is something to write */
  7189. ms->outwritebuf = -1;
  7190. if (ms->iomask & (DAHDI_IOMUX_WRITE | DAHDI_IOMUX_WRITEEMPTY))
  7191. wake_up_interruptible(&ms->waitq);
  7192. /* If we're only supposed to start when full, disable the transmitter */
  7193. if ((ms->txbufpolicy == DAHDI_POLICY_WHEN_FULL) ||
  7194. (ms->txbufpolicy == DAHDI_POLICY_HALF_FULL))
  7195. ms->txdisable = 1;
  7196. }
  7197. } else {
  7198. if (ms->outwritebuf == ms->inwritebuf) {
  7199. ms->outwritebuf = oldbuf;
  7200. if (ms->iomask & (DAHDI_IOMUX_WRITE | DAHDI_IOMUX_WRITEEMPTY))
  7201. wake_up_interruptible(&ms->waitq);
  7202. /* If we're only supposed to start when full, disable the transmitter */
  7203. if ((ms->txbufpolicy == DAHDI_POLICY_WHEN_FULL) ||
  7204. (ms->txbufpolicy == DAHDI_POLICY_HALF_FULL))
  7205. ms->txdisable = 1;
  7206. }
  7207. }
  7208. if (ms->inwritebuf < 0) {
  7209. /* The filler doesn't have a place to put data. Now
  7210. that we're done with this buffer, notify them. */
  7211. ms->inwritebuf = oldbuf;
  7212. }
  7213. /* In the very orignal driver, it was quite well known to me (Jim) that there
  7214. was a possibility that a channel sleeping on a write block needed to
  7215. be potentially woken up EVERY time a buffer was emptied, not just on the first
  7216. one, because if only done on the first one there is a slight timing potential
  7217. of missing the wakeup (between where it senses the (lack of) active condition
  7218. (with interrupts disabled) and where it does the sleep (interrupts enabled)
  7219. in the read or iomux call, etc). That is why the write and iomux calls start
  7220. with an infinite loop that gets broken out of upon an active condition,
  7221. otherwise keeps sleeping and looking. The part in this code got "optimized"
  7222. out in the later versions, and is put back now. */
  7223. if (!(ms->flags & DAHDI_FLAG_PPP) ||
  7224. !dahdi_have_netdev(ms)) {
  7225. wake_up_interruptible(&ms->waitq);
  7226. }
  7227. /* Transmit a flag if this is an HDLC channel */
  7228. if (ms->flags & DAHDI_FLAG_HDLC)
  7229. fasthdlc_tx_frame_nocheck(&ms->txhdlc);
  7230. #ifdef CONFIG_DAHDI_NET
  7231. if (dahdi_have_netdev(ms))
  7232. netif_wake_queue(chan_to_netdev(ms));
  7233. #endif
  7234. #ifdef CONFIG_DAHDI_PPP
  7235. if (ms->flags & DAHDI_FLAG_PPP) {
  7236. ms->do_ppp_wakeup = 1;
  7237. tasklet_schedule(&ms->ppp_calls);
  7238. }
  7239. #endif
  7240. }
  7241. } else if (ms->curtone && !is_pseudo_chan(ms)) {
  7242. left = ms->curtone->tonesamples - ms->tonep;
  7243. if (left > bytes)
  7244. left = bytes;
  7245. for (x=0;x<left;x++) {
  7246. /* Pick our default value from the next sample of the current tone */
  7247. getlin = dahdi_tone_nextsample(&ms->ts, ms->curtone);
  7248. *(txb++) = DAHDI_LIN2X(getlin, ms);
  7249. }
  7250. ms->tonep+=left;
  7251. bytes -= left;
  7252. if (ms->tonep >= ms->curtone->tonesamples) {
  7253. struct dahdi_tone *last;
  7254. /* Go to the next sample of the tone */
  7255. ms->tonep = 0;
  7256. last = ms->curtone;
  7257. ms->curtone = ms->curtone->next;
  7258. if (!ms->curtone) {
  7259. /* No more tones... Is this dtmf or mf? If so, go to the next digit */
  7260. if (ms->dialing)
  7261. __do_dtmf(ms);
  7262. } else {
  7263. if (last != ms->curtone)
  7264. dahdi_init_tone_state(&ms->ts, ms->curtone);
  7265. }
  7266. }
  7267. } else if (ms->flags & DAHDI_FLAG_LOOPED) {
  7268. for (x = 0; x < bytes; x++)
  7269. txb[x] = ms->readchunk[x];
  7270. bytes = 0;
  7271. } else if (ms->flags & DAHDI_FLAG_HDLC) {
  7272. for (x=0;x<bytes;x++) {
  7273. /* Okay, if we're HDLC, then transmit a flag by default */
  7274. if (fasthdlc_tx_need_data(&ms->txhdlc))
  7275. fasthdlc_tx_frame_nocheck(&ms->txhdlc);
  7276. *(txb++) = fasthdlc_tx_run_nocheck(&ms->txhdlc);
  7277. }
  7278. bytes = 0;
  7279. } else if (ms->flags & DAHDI_FLAG_CLEAR) {
  7280. /* Clear channels that are idle in audio mode need
  7281. to send silence; in non-audio mode, always send 0xff
  7282. so stupid switches won't consider the channel active
  7283. */
  7284. if (ms->flags & DAHDI_FLAG_AUDIO) {
  7285. memset(txb, DAHDI_LIN2X(0, ms), bytes);
  7286. } else {
  7287. memset(txb, 0xFF, bytes);
  7288. }
  7289. needtxunderrun += bytes;
  7290. bytes = 0;
  7291. } else {
  7292. memset(txb, DAHDI_LIN2X(0, ms), bytes); /* Lastly we use silence on telephony channels */
  7293. needtxunderrun += bytes;
  7294. bytes = 0;
  7295. }
  7296. }
  7297. if (needtxunderrun) {
  7298. if (!test_bit(DAHDI_FLAGBIT_TXUNDERRUN, &ms->flags)) {
  7299. if (test_bit(DAHDI_FLAGBIT_BUFEVENTS, &ms->flags))
  7300. __qevent(ms, DAHDI_EVENT_WRITE_UNDERRUN);
  7301. set_bit(DAHDI_FLAGBIT_TXUNDERRUN, &ms->flags);
  7302. }
  7303. } else {
  7304. clear_bit(DAHDI_FLAGBIT_TXUNDERRUN, &ms->flags);
  7305. }
  7306. #ifdef CONFIG_DAHDI_MIRROR
  7307. if (ss->txmirror) {
  7308. spin_lock(&ss->txmirror->lock);
  7309. __putbuf_chunk(ss->txmirror, orig_txb, DAHDI_CHUNKSIZE);
  7310. spin_unlock(&ss->txmirror->lock);
  7311. }
  7312. #endif /* CONFIG_DAHDI_MIRROR */
  7313. }
  7314. static inline void rbs_itimer_expire(struct dahdi_chan *chan)
  7315. {
  7316. /* the only way this could have gotten here, is if a channel
  7317. went onf hook longer then the wink or flash detect timeout */
  7318. /* Called with chan->lock held */
  7319. switch(chan->sig)
  7320. {
  7321. case DAHDI_SIG_FXOLS: /* if FXO, its definitely on hook */
  7322. case DAHDI_SIG_FXOGS:
  7323. case DAHDI_SIG_FXOKS:
  7324. __qevent(chan,DAHDI_EVENT_ONHOOK);
  7325. chan->gotgs = 0;
  7326. break;
  7327. #if defined(EMFLASH) || defined(EMPULSE)
  7328. case DAHDI_SIG_EM:
  7329. case DAHDI_SIG_EM_E1:
  7330. if (chan->rxhooksig == DAHDI_RXSIG_ONHOOK) {
  7331. __qevent(chan,DAHDI_EVENT_ONHOOK);
  7332. break;
  7333. }
  7334. __qevent(chan,DAHDI_EVENT_RINGOFFHOOK);
  7335. break;
  7336. #endif
  7337. #ifdef FXSFLASH
  7338. case DAHDI_SIG_FXSKS:
  7339. if (chan->rxhooksig == DAHDI_RXSIG_ONHOOK) {
  7340. __qevent(chan, DAHDI_EVENT_ONHOOK);
  7341. break;
  7342. }
  7343. #endif
  7344. /* fall thru intentionally */
  7345. default: /* otherwise, its definitely off hook */
  7346. __qevent(chan,DAHDI_EVENT_RINGOFFHOOK);
  7347. break;
  7348. }
  7349. }
  7350. static inline void __rbs_otimer_expire(struct dahdi_chan *chan)
  7351. {
  7352. int len = 0;
  7353. /* Called with chan->lock held */
  7354. chan->otimer = 0;
  7355. /* Move to the next timer state */
  7356. switch(chan->txstate) {
  7357. case DAHDI_TXSTATE_RINGOFF:
  7358. /* Turn on the ringer now that the silent time has passed */
  7359. ++chan->cadencepos;
  7360. if (chan->cadencepos >= DAHDI_MAX_CADENCE)
  7361. chan->cadencepos = chan->firstcadencepos;
  7362. len = chan->ringcadence[chan->cadencepos];
  7363. if (!len) {
  7364. chan->cadencepos = chan->firstcadencepos;
  7365. len = chan->ringcadence[chan->cadencepos];
  7366. }
  7367. dahdi_rbs_sethook(chan, DAHDI_TXSIG_START, DAHDI_TXSTATE_RINGON, len);
  7368. __qevent(chan, DAHDI_EVENT_RINGERON);
  7369. break;
  7370. case DAHDI_TXSTATE_RINGON:
  7371. /* Turn off the ringer now that the loud time has passed */
  7372. ++chan->cadencepos;
  7373. if (chan->cadencepos >= DAHDI_MAX_CADENCE)
  7374. chan->cadencepos = 0;
  7375. len = chan->ringcadence[chan->cadencepos];
  7376. if (!len) {
  7377. chan->cadencepos = 0;
  7378. len = chan->curzone->ringcadence[chan->cadencepos];
  7379. }
  7380. dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_RINGOFF, len);
  7381. __qevent(chan, DAHDI_EVENT_RINGEROFF);
  7382. break;
  7383. case DAHDI_TXSTATE_START:
  7384. /* If we were starting, go off hook now ready to debounce */
  7385. dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_AFTERSTART, DAHDI_AFTERSTART_TIME);
  7386. wake_up_interruptible(&chan->waitq);
  7387. break;
  7388. case DAHDI_TXSTATE_PREWINK:
  7389. /* Actually wink */
  7390. dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_WINK, chan->winktime);
  7391. break;
  7392. case DAHDI_TXSTATE_WINK:
  7393. /* Wink complete, go on hook and stabalize */
  7394. dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_ONHOOK, 0);
  7395. if (chan->file && (chan->file->f_flags & O_NONBLOCK))
  7396. __qevent(chan, DAHDI_EVENT_HOOKCOMPLETE);
  7397. wake_up_interruptible(&chan->waitq);
  7398. break;
  7399. case DAHDI_TXSTATE_PREFLASH:
  7400. /* Actually flash */
  7401. dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_FLASH, chan->flashtime);
  7402. break;
  7403. case DAHDI_TXSTATE_FLASH:
  7404. dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_OFFHOOK, 0);
  7405. if (chan->file && (chan->file->f_flags & O_NONBLOCK))
  7406. __qevent(chan, DAHDI_EVENT_HOOKCOMPLETE);
  7407. wake_up_interruptible(&chan->waitq);
  7408. break;
  7409. case DAHDI_TXSTATE_DEBOUNCE:
  7410. dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_OFFHOOK, 0);
  7411. /* See if we've gone back on hook */
  7412. if ((chan->rxhooksig == DAHDI_RXSIG_ONHOOK) && (chan->rxflashtime > 2))
  7413. chan->itimerset = chan->itimer = chan->rxflashtime * DAHDI_CHUNKSIZE;
  7414. wake_up_interruptible(&chan->waitq);
  7415. break;
  7416. case DAHDI_TXSTATE_AFTERSTART:
  7417. dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_OFFHOOK, 0);
  7418. if (chan->file && (chan->file->f_flags & O_NONBLOCK))
  7419. __qevent(chan, DAHDI_EVENT_HOOKCOMPLETE);
  7420. wake_up_interruptible(&chan->waitq);
  7421. break;
  7422. case DAHDI_TXSTATE_KEWL:
  7423. dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_AFTERKEWL, DAHDI_AFTERKEWLTIME);
  7424. if (chan->file && (chan->file->f_flags & O_NONBLOCK))
  7425. __qevent(chan, DAHDI_EVENT_HOOKCOMPLETE);
  7426. wake_up_interruptible(&chan->waitq);
  7427. break;
  7428. case DAHDI_TXSTATE_AFTERKEWL:
  7429. if (chan->kewlonhook) {
  7430. __qevent(chan,DAHDI_EVENT_ONHOOK);
  7431. }
  7432. chan->txstate = DAHDI_TXSTATE_ONHOOK;
  7433. chan->gotgs = 0;
  7434. break;
  7435. case DAHDI_TXSTATE_PULSEBREAK:
  7436. dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_PULSEMAKE,
  7437. chan->pulsemaketime);
  7438. wake_up_interruptible(&chan->waitq);
  7439. break;
  7440. case DAHDI_TXSTATE_PULSEMAKE:
  7441. if (chan->pdialcount)
  7442. chan->pdialcount--;
  7443. if (chan->pdialcount)
  7444. {
  7445. dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK,
  7446. DAHDI_TXSTATE_PULSEBREAK, chan->pulsebreaktime);
  7447. break;
  7448. }
  7449. chan->txstate = DAHDI_TXSTATE_PULSEAFTER;
  7450. chan->otimer = chan->pulseaftertime * DAHDI_CHUNKSIZE;
  7451. wake_up_interruptible(&chan->waitq);
  7452. break;
  7453. case DAHDI_TXSTATE_PULSEAFTER:
  7454. chan->txstate = DAHDI_TXSTATE_OFFHOOK;
  7455. __do_dtmf(chan);
  7456. wake_up_interruptible(&chan->waitq);
  7457. break;
  7458. default:
  7459. break;
  7460. }
  7461. }
  7462. static void __dahdi_hooksig_pvt(struct dahdi_chan *chan, enum dahdi_rxsig rxsig)
  7463. {
  7464. /* State machines for receive hookstate transitions
  7465. called with chan->lock held */
  7466. if ((chan->rxhooksig) == rxsig) return;
  7467. if ((chan->flags & DAHDI_FLAG_SIGFREEZE)) return;
  7468. chan->rxhooksig = rxsig;
  7469. #ifdef RINGBEGIN
  7470. if ((chan->sig & __DAHDI_SIG_FXS) && (rxsig == DAHDI_RXSIG_RING) &&
  7471. (!chan->ringdebtimer))
  7472. __qevent(chan,DAHDI_EVENT_RINGBEGIN);
  7473. #endif
  7474. switch(chan->sig) {
  7475. case DAHDI_SIG_EM: /* E and M */
  7476. case DAHDI_SIG_EM_E1:
  7477. switch(rxsig) {
  7478. case DAHDI_RXSIG_OFFHOOK: /* went off hook */
  7479. /* The interface is going off hook */
  7480. #ifdef EMFLASH
  7481. if (chan->itimer)
  7482. {
  7483. __qevent(chan,DAHDI_EVENT_WINKFLASH);
  7484. chan->itimerset = chan->itimer = 0;
  7485. break;
  7486. }
  7487. #endif
  7488. #ifdef EMPULSE
  7489. if (chan->itimer) /* if timer still running */
  7490. {
  7491. int plen = chan->itimerset - chan->itimer;
  7492. if (plen <= DAHDI_MAXPULSETIME)
  7493. {
  7494. if (plen >= DAHDI_MINPULSETIME)
  7495. {
  7496. chan->pulsecount++;
  7497. chan->pulsetimer = DAHDI_PULSETIMEOUT;
  7498. chan->itimerset = chan->itimer = 0;
  7499. if (chan->pulsecount == 1)
  7500. __qevent(chan,DAHDI_EVENT_PULSE_START);
  7501. }
  7502. }
  7503. break;
  7504. }
  7505. #endif
  7506. /* set wink timer */
  7507. chan->itimerset = chan->itimer = chan->rxwinktime * DAHDI_CHUNKSIZE;
  7508. break;
  7509. case DAHDI_RXSIG_ONHOOK: /* went on hook */
  7510. /* This interface is now going on hook.
  7511. Check for WINK, etc */
  7512. if (chan->itimer)
  7513. __qevent(chan,DAHDI_EVENT_WINKFLASH);
  7514. #if defined(EMFLASH) || defined(EMPULSE)
  7515. else {
  7516. #ifdef EMFLASH
  7517. chan->itimerset = chan->itimer = chan->rxflashtime * DAHDI_CHUNKSIZE;
  7518. #else /* EMFLASH */
  7519. chan->itimerset = chan->itimer = chan->rxwinktime * DAHDI_CHUNKSIZE;
  7520. #endif /* EMFLASH */
  7521. chan->gotgs = 0;
  7522. break;
  7523. }
  7524. #else /* EMFLASH || EMPULSE */
  7525. else {
  7526. __qevent(chan,DAHDI_EVENT_ONHOOK);
  7527. chan->gotgs = 0;
  7528. }
  7529. #endif
  7530. chan->itimerset = chan->itimer = 0;
  7531. break;
  7532. default:
  7533. break;
  7534. }
  7535. break;
  7536. case DAHDI_SIG_FXSKS: /* FXS Kewlstart */
  7537. /* ignore a bit error if loop not closed and stable */
  7538. if (chan->txstate != DAHDI_TXSTATE_OFFHOOK) break;
  7539. #ifdef FXSFLASH
  7540. if (rxsig == DAHDI_RXSIG_ONHOOK) {
  7541. chan->itimer = DAHDI_FXSFLASHMAXTIME * DAHDI_CHUNKSIZE;
  7542. break;
  7543. } else if (rxsig == DAHDI_RXSIG_OFFHOOK) {
  7544. if (chan->itimer) {
  7545. /* did the offhook occur in the window? if not, ignore both events */
  7546. if (chan->itimer <= ((DAHDI_FXSFLASHMAXTIME - DAHDI_FXSFLASHMINTIME) * DAHDI_CHUNKSIZE))
  7547. __qevent(chan, DAHDI_EVENT_WINKFLASH);
  7548. }
  7549. chan->itimer = 0;
  7550. break;
  7551. }
  7552. #endif
  7553. /* fall through intentionally */
  7554. case DAHDI_SIG_FXSGS: /* FXS Groundstart */
  7555. if (rxsig == DAHDI_RXSIG_ONHOOK) {
  7556. chan->ringdebtimer = RING_DEBOUNCE_TIME;
  7557. chan->ringtrailer = 0;
  7558. if (chan->txstate != DAHDI_TXSTATE_DEBOUNCE) {
  7559. chan->gotgs = 0;
  7560. __qevent(chan,DAHDI_EVENT_ONHOOK);
  7561. }
  7562. }
  7563. break;
  7564. case DAHDI_SIG_FXOGS: /* FXO Groundstart */
  7565. if (rxsig == DAHDI_RXSIG_START) {
  7566. /* if havent got gs, report it */
  7567. if (!chan->gotgs) {
  7568. __qevent(chan,DAHDI_EVENT_RINGOFFHOOK);
  7569. chan->gotgs = 1;
  7570. }
  7571. }
  7572. /* fall through intentionally */
  7573. case DAHDI_SIG_FXOLS: /* FXO Loopstart */
  7574. case DAHDI_SIG_FXOKS: /* FXO Kewlstart */
  7575. switch(rxsig) {
  7576. case DAHDI_RXSIG_OFFHOOK: /* went off hook */
  7577. /* if asserti ng ring, stop it */
  7578. if (chan->txstate == DAHDI_TXSTATE_START) {
  7579. dahdi_rbs_sethook(chan,DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_AFTERSTART, DAHDI_AFTERSTART_TIME);
  7580. }
  7581. chan->kewlonhook = 0;
  7582. #ifdef CONFIG_DAHDI_DEBUG
  7583. module_printk(KERN_NOTICE, "Off hook on channel %d, itimer = %d, gotgs = %d\n", chan->channo, chan->itimer, chan->gotgs);
  7584. #endif
  7585. if (chan->itimer) /* if timer still running */
  7586. {
  7587. int plen = chan->itimerset - chan->itimer;
  7588. if (plen <= DAHDI_MAXPULSETIME)
  7589. {
  7590. if (plen >= DAHDI_MINPULSETIME)
  7591. {
  7592. chan->pulsecount++;
  7593. chan->pulsetimer = DAHDI_PULSETIMEOUT;
  7594. chan->itimer = chan->itimerset;
  7595. if (chan->pulsecount == 1)
  7596. __qevent(chan,DAHDI_EVENT_PULSE_START);
  7597. }
  7598. } else
  7599. __qevent(chan,DAHDI_EVENT_WINKFLASH);
  7600. } else {
  7601. /* if havent got GS detect */
  7602. if (!chan->gotgs) {
  7603. __qevent(chan,DAHDI_EVENT_RINGOFFHOOK);
  7604. chan->gotgs = 1;
  7605. chan->itimerset = chan->itimer = 0;
  7606. }
  7607. }
  7608. chan->itimerset = chan->itimer = 0;
  7609. break;
  7610. case DAHDI_RXSIG_ONHOOK: /* went on hook */
  7611. /* if not during offhook debounce time */
  7612. if ((chan->txstate != DAHDI_TXSTATE_DEBOUNCE) &&
  7613. (chan->txstate != DAHDI_TXSTATE_KEWL) &&
  7614. (chan->txstate != DAHDI_TXSTATE_AFTERKEWL)) {
  7615. chan->itimerset = chan->itimer = chan->rxflashtime * DAHDI_CHUNKSIZE;
  7616. }
  7617. if (chan->txstate == DAHDI_TXSTATE_KEWL)
  7618. chan->kewlonhook = 1;
  7619. break;
  7620. default:
  7621. break;
  7622. }
  7623. default:
  7624. break;
  7625. }
  7626. }
  7627. /**
  7628. * dahdi_hooksig() - send a signal on a channel to userspace
  7629. * @chan: the DAHDI channel
  7630. * @rxsig: signal (number) to send
  7631. *
  7632. * Called from a channel driver to send a DAHDI signal to userspace.
  7633. * The signal will be queued for delivery to userspace.
  7634. *
  7635. * If the signal is the same as previous one sent, it won't be re-sent.
  7636. */
  7637. void dahdi_hooksig(struct dahdi_chan *chan, enum dahdi_rxsig rxsig)
  7638. {
  7639. /* skip if no change */
  7640. unsigned long flags;
  7641. spin_lock_irqsave(&chan->lock, flags);
  7642. __dahdi_hooksig_pvt(chan,rxsig);
  7643. spin_unlock_irqrestore(&chan->lock, flags);
  7644. }
  7645. /**
  7646. * dahdi_rbsbits() - set Rx RBS bits on the channel
  7647. * @chan: the DAHDI channel
  7648. * @cursig: the bits to set
  7649. *
  7650. * Set the channel's rxsig (received: from device to userspace) and act
  7651. * accordingly.
  7652. */
  7653. void dahdi_rbsbits(struct dahdi_chan *chan, int cursig)
  7654. {
  7655. unsigned long flags;
  7656. if (cursig == chan->rxsig)
  7657. return;
  7658. if ((chan->flags & DAHDI_FLAG_SIGFREEZE)) return;
  7659. spin_lock_irqsave(&chan->lock, flags);
  7660. switch(chan->sig) {
  7661. case DAHDI_SIG_FXOGS: /* FXO Groundstart */
  7662. /* B-bit only matters for FXO GS */
  7663. if (!(cursig & DAHDI_BBIT)) {
  7664. __dahdi_hooksig_pvt(chan, DAHDI_RXSIG_START);
  7665. break;
  7666. }
  7667. /* Fall through */
  7668. case DAHDI_SIG_EM_E1:
  7669. case DAHDI_SIG_FXOLS: /* FXO Loopstart */
  7670. case DAHDI_SIG_FXOKS: /* FXO Kewlstart */
  7671. if (cursig & DAHDI_ABIT) /* off hook */
  7672. __dahdi_hooksig_pvt(chan,DAHDI_RXSIG_OFFHOOK);
  7673. else /* on hook */
  7674. __dahdi_hooksig_pvt(chan,DAHDI_RXSIG_ONHOOK);
  7675. break;
  7676. case DAHDI_SIG_EM: /* E and M */
  7677. /* Watch only the ABIT for changes. */
  7678. if ((cursig & DAHDI_ABIT) == (chan->rxsig & DAHDI_ABIT))
  7679. break;
  7680. __dahdi_hooksig_pvt(chan, (cursig & DAHDI_ABIT) ?
  7681. DAHDI_RXSIG_OFFHOOK : DAHDI_RXSIG_ONHOOK);
  7682. break;
  7683. case DAHDI_SIG_FXSKS: /* FXS Kewlstart */
  7684. case DAHDI_SIG_FXSGS: /* FXS Groundstart */
  7685. /* Fall through */
  7686. case DAHDI_SIG_FXSLS:
  7687. if (!(cursig & DAHDI_BBIT)) {
  7688. /* Check for ringing first */
  7689. __dahdi_hooksig_pvt(chan, DAHDI_RXSIG_RING);
  7690. break;
  7691. }
  7692. if ((chan->sig != DAHDI_SIG_FXSLS) && (cursig & DAHDI_ABIT)) {
  7693. /* if went on hook */
  7694. __dahdi_hooksig_pvt(chan, DAHDI_RXSIG_ONHOOK);
  7695. } else {
  7696. __dahdi_hooksig_pvt(chan, DAHDI_RXSIG_OFFHOOK);
  7697. }
  7698. break;
  7699. case DAHDI_SIG_CAS:
  7700. /* send event that something changed */
  7701. __qevent(chan, DAHDI_EVENT_BITSCHANGED);
  7702. break;
  7703. default:
  7704. break;
  7705. }
  7706. /* Keep track of signalling for next time */
  7707. chan->rxsig = cursig;
  7708. spin_unlock_irqrestore(&chan->lock, flags);
  7709. if ((debug & DEBUG_RBS) && printk_ratelimit()) {
  7710. chan_notice(chan, "Detected sigbits change to %04x\n", cursig);
  7711. }
  7712. }
  7713. static void process_echocan_events(struct dahdi_chan *chan)
  7714. {
  7715. union dahdi_echocan_events events = chan->ec_state->events;
  7716. if (events.bit.CED_tx_detected) {
  7717. dahdi_qevent_nolock(chan, DAHDI_EVENT_TX_CED_DETECTED);
  7718. if (chan->ec_state) {
  7719. if (chan->ec_state->status.mode == ECHO_MODE_ACTIVE)
  7720. set_echocan_fax_mode(chan, chan->channo, "CED tx detected", 1);
  7721. else
  7722. module_printk(KERN_NOTICE, "Detected CED tone (tx) on channel %d\n", chan->channo);
  7723. }
  7724. }
  7725. if (events.bit.CED_rx_detected) {
  7726. dahdi_qevent_nolock(chan, DAHDI_EVENT_RX_CED_DETECTED);
  7727. if (chan->ec_state) {
  7728. if (chan->ec_state->status.mode == ECHO_MODE_ACTIVE)
  7729. set_echocan_fax_mode(chan, chan->channo, "CED rx detected", 1);
  7730. else
  7731. module_printk(KERN_NOTICE, "Detected CED tone (rx) on channel %d\n", chan->channo);
  7732. }
  7733. }
  7734. if (events.bit.CNG_tx_detected)
  7735. dahdi_qevent_nolock(chan, DAHDI_EVENT_TX_CNG_DETECTED);
  7736. if (events.bit.CNG_rx_detected)
  7737. dahdi_qevent_nolock(chan, DAHDI_EVENT_RX_CNG_DETECTED);
  7738. if (events.bit.NLP_auto_disabled) {
  7739. dahdi_qevent_nolock(chan, DAHDI_EVENT_EC_NLP_DISABLED);
  7740. chan->ec_state->status.mode = ECHO_MODE_FAX;
  7741. }
  7742. if (events.bit.NLP_auto_enabled) {
  7743. dahdi_qevent_nolock(chan, DAHDI_EVENT_EC_NLP_ENABLED);
  7744. chan->ec_state->status.mode = ECHO_MODE_ACTIVE;
  7745. }
  7746. }
  7747. /**
  7748. * __dahdi_ec_chunk() - process echo for a single channel
  7749. * @ss: DAHDI channel
  7750. * @rxchunk: buffer to store audio with cancelled audio
  7751. * @preecchunk: chunk of audio on which to cancel echo
  7752. * @txchunk: reference chunk from the other direction
  7753. *
  7754. * The echo canceller function fixes received (from device to userspace)
  7755. * audio. In order to fix it it uses the transmitted audio as a
  7756. * reference. This call updates the echo canceller for a single chunk (8
  7757. * bytes).
  7758. *
  7759. * Call with local interrupts disabled.
  7760. */
  7761. void __dahdi_ec_chunk(struct dahdi_chan *ss, u8 *rxchunk,
  7762. const u8 *preecchunk, const u8 *txchunk)
  7763. {
  7764. short rxlin;
  7765. int x;
  7766. spin_lock(&ss->lock);
  7767. if (ss->readchunkpreec) {
  7768. /* Save a copy of the audio before the echo can has its way with it */
  7769. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  7770. /* We only ever really need to deal with signed linear - let's just convert it now */
  7771. ss->readchunkpreec[x] = DAHDI_XLAW(preecchunk[x], ss);
  7772. }
  7773. /* Perform echo cancellation on a chunk if necessary */
  7774. if (ss->ec_state) {
  7775. #if defined(CONFIG_DAHDI_MMX) || defined(ECHO_CAN_FP)
  7776. dahdi_kernel_fpu_begin();
  7777. #endif
  7778. if (ss->ec_state->status.mode & __ECHO_MODE_MUTE) {
  7779. /* Special stuff for training the echo can */
  7780. for (x=0;x<DAHDI_CHUNKSIZE;x++) {
  7781. rxlin = DAHDI_XLAW(preecchunk[x], ss);
  7782. if (ss->ec_state->status.mode == ECHO_MODE_PRETRAINING) {
  7783. if (--ss->ec_state->status.pretrain_timer <= 0) {
  7784. ss->ec_state->status.pretrain_timer = 0;
  7785. ss->ec_state->status.mode = ECHO_MODE_STARTTRAINING;
  7786. }
  7787. }
  7788. if (ss->ec_state->status.mode == ECHO_MODE_AWAITINGECHO) {
  7789. ss->ec_state->status.last_train_tap = 0;
  7790. ss->ec_state->status.mode = ECHO_MODE_TRAINING;
  7791. }
  7792. if ((ss->ec_state->status.mode == ECHO_MODE_TRAINING) &&
  7793. (ss->ec_state->ops->echocan_traintap)) {
  7794. if (ss->ec_state->ops->echocan_traintap(ss->ec_state, ss->ec_state->status.last_train_tap++, rxlin)) {
  7795. ss->ec_state->status.mode = ECHO_MODE_ACTIVE;
  7796. }
  7797. }
  7798. rxlin = 0;
  7799. rxchunk[x] = DAHDI_LIN2X((int)rxlin, ss);
  7800. }
  7801. } else if (ss->ec_state->status.mode != ECHO_MODE_IDLE) {
  7802. ss->ec_state->events.all = 0;
  7803. if (ss->ec_state->ops->echocan_process) {
  7804. short rxlins[DAHDI_CHUNKSIZE], txlins[DAHDI_CHUNKSIZE];
  7805. for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
  7806. rxlins[x] = DAHDI_XLAW(preecchunk[x],
  7807. ss);
  7808. txlins[x] = DAHDI_XLAW(txchunk[x], ss);
  7809. }
  7810. ss->ec_state->ops->echocan_process(ss->ec_state, rxlins, txlins, DAHDI_CHUNKSIZE);
  7811. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  7812. rxchunk[x] = DAHDI_LIN2X((int) rxlins[x], ss);
  7813. } else if (ss->ec_state->ops->echocan_events)
  7814. ss->ec_state->ops->echocan_events(ss->ec_state);
  7815. if (ss->ec_state->events.all)
  7816. process_echocan_events(ss);
  7817. }
  7818. #if defined(CONFIG_DAHDI_MMX) || defined(ECHO_CAN_FP)
  7819. dahdi_kernel_fpu_end();
  7820. #endif
  7821. }
  7822. spin_unlock(&ss->lock);
  7823. }
  7824. EXPORT_SYMBOL(__dahdi_ec_chunk);
  7825. /**
  7826. * dahdi_ec_span() - process echo for all channels in a span.
  7827. * @span: DAHDI span
  7828. *
  7829. * Similar to calling dahdi_ec_chunk() for each of the channels in the
  7830. * span. Uses dahdi_chunk.write_chunk for the rxchunk (the chunk to fix)
  7831. * and dahdi_chan.readchunk as the txchunk (the reference chunk).
  7832. */
  7833. void _dahdi_ec_span(struct dahdi_span *span)
  7834. {
  7835. int x;
  7836. for (x = 0; x < span->channels; x++) {
  7837. struct dahdi_chan *const chan = span->chans[x];
  7838. if (!chan->ec_current)
  7839. continue;
  7840. _dahdi_ec_chunk(chan, chan->readchunk, chan->writechunk);
  7841. }
  7842. }
  7843. EXPORT_SYMBOL(_dahdi_ec_span);
  7844. /* return 0 if nothing detected, 1 if lack of tone, 2 if presence of tone */
  7845. /* modifies buffer pointed to by 'amp' with notched-out values */
  7846. static inline int sf_detect(struct sf_detect_state *s,
  7847. short *amp,
  7848. int samples,long p1, long p2, long p3)
  7849. {
  7850. int i,rv = 0;
  7851. long x,y;
  7852. #define SF_DETECT_SAMPLES (DAHDI_CHUNKSIZE * 5)
  7853. #define SF_DETECT_MIN_ENERGY 500
  7854. #define NB 14 /* number of bits to shift left */
  7855. /* determine energy level before filtering */
  7856. for(i = 0; i < samples; i++)
  7857. {
  7858. if (amp[i] < 0) s->e1 -= amp[i];
  7859. else s->e1 += amp[i];
  7860. }
  7861. /* do 2nd order IIR notch filter at given freq. and calculate
  7862. energy */
  7863. for(i = 0; i < samples; i++)
  7864. {
  7865. x = amp[i] << NB;
  7866. y = s->x2 + (p1 * (s->x1 >> NB)) + x;
  7867. y += (p2 * (s->y2 >> NB)) +
  7868. (p3 * (s->y1 >> NB));
  7869. s->x2 = s->x1;
  7870. s->x1 = x;
  7871. s->y2 = s->y1;
  7872. s->y1 = y;
  7873. amp[i] = y >> NB;
  7874. if (amp[i] < 0) s->e2 -= amp[i];
  7875. else s->e2 += amp[i];
  7876. }
  7877. s->samps += i;
  7878. /* if time to do determination */
  7879. if ((s->samps) >= SF_DETECT_SAMPLES)
  7880. {
  7881. rv = 1; /* default to no tone */
  7882. /* if enough energy, it is determined to be a tone */
  7883. if (((s->e1 - s->e2) / s->samps) > SF_DETECT_MIN_ENERGY) rv = 2;
  7884. /* reset energy processing variables */
  7885. s->samps = 0;
  7886. s->e1 = s->e2 = 0;
  7887. }
  7888. return(rv);
  7889. }
  7890. static inline void __dahdi_process_putaudio_chunk(struct dahdi_chan *ss, unsigned char *rxb)
  7891. {
  7892. /* We transmit data from our master channel */
  7893. /* Called with ss->lock held */
  7894. struct dahdi_chan *ms = ss->master;
  7895. /* Linear version of received data */
  7896. short putlin[DAHDI_CHUNKSIZE],k[DAHDI_CHUNKSIZE];
  7897. int x,r;
  7898. if (ms->dialing) ms->afterdialingtimer = 50;
  7899. else if (ms->afterdialingtimer) ms->afterdialingtimer--;
  7900. if (ms->afterdialingtimer && !is_pseudo_chan(ms)) {
  7901. /* Be careful since memset is likely a macro */
  7902. rxb[0] = DAHDI_LIN2X(0, ms);
  7903. memset(&rxb[1], rxb[0], DAHDI_CHUNKSIZE - 1); /* receive as silence if dialing */
  7904. }
  7905. for (x=0;x<DAHDI_CHUNKSIZE;x++) {
  7906. rxb[x] = ms->rxgain[rxb[x]];
  7907. putlin[x] = DAHDI_XLAW(rxb[x], ms);
  7908. }
  7909. #ifndef CONFIG_DAHDI_NO_ECHOCAN_DISABLE
  7910. if (ms->ec_state && (ms->ec_state->status.mode == ECHO_MODE_ACTIVE) && !ms->ec_state->features.CED_rx_detect) {
  7911. for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
  7912. if (echo_can_disable_detector_update(&ms->ec_state->rxecdis, putlin[x])) {
  7913. set_echocan_fax_mode(ms, ss->channo, "CED rx detected", 1);
  7914. dahdi_qevent_nolock(ms, DAHDI_EVENT_RX_CED_DETECTED);
  7915. break;
  7916. }
  7917. }
  7918. }
  7919. #endif
  7920. /* if doing rx tone decoding */
  7921. if (ms->rxp1 && ms->rxp2 && ms->rxp3)
  7922. {
  7923. r = sf_detect(&ms->rd,putlin,DAHDI_CHUNKSIZE,ms->rxp1,
  7924. ms->rxp2,ms->rxp3);
  7925. /* Convert back */
  7926. for(x=0;x<DAHDI_CHUNKSIZE;x++)
  7927. rxb[x] = DAHDI_LIN2X(putlin[x], ms);
  7928. if (r) /* if something happened */
  7929. {
  7930. if (r != ms->rd.lastdetect)
  7931. {
  7932. if (((r == 2) && !(ms->toneflags & DAHDI_REVERSE_RXTONE)) ||
  7933. ((r == 1) && (ms->toneflags & DAHDI_REVERSE_RXTONE)))
  7934. {
  7935. __qevent(ms,DAHDI_EVENT_RINGOFFHOOK);
  7936. }
  7937. else
  7938. {
  7939. __qevent(ms,DAHDI_EVENT_ONHOOK);
  7940. }
  7941. ms->rd.lastdetect = r;
  7942. }
  7943. }
  7944. }
  7945. if (!is_pseudo_chan(ms)) {
  7946. memcpy(ms->putlin, putlin, DAHDI_CHUNKSIZE * sizeof(short));
  7947. memcpy(ms->putraw, rxb, DAHDI_CHUNKSIZE);
  7948. }
  7949. /* Take the rxc, twiddle it for conferencing if appropriate and put it
  7950. back */
  7951. if ((!ms->confmute && !ms->afterdialingtimer) || is_pseudo_chan(ms)) {
  7952. struct dahdi_chan *const conf_chan = ms->conf_chan;
  7953. switch(ms->confmode & DAHDI_CONF_MODE_MASK) {
  7954. case DAHDI_CONF_NORMAL: /* Normal mode */
  7955. /* Do nothing. rx goes output */
  7956. break;
  7957. case DAHDI_CONF_MONITOR: /* Monitor a channel's rx mode */
  7958. /* if not a pseudo-channel, ignore */
  7959. if (!is_pseudo_chan(ms))
  7960. break;
  7961. /* Add monitored channel */
  7962. if (is_pseudo_chan(conf_chan))
  7963. ACSS(putlin, conf_chan->getlin);
  7964. else
  7965. ACSS(putlin, conf_chan->putlin);
  7966. /* Convert back */
  7967. for(x=0;x<DAHDI_CHUNKSIZE;x++)
  7968. rxb[x] = DAHDI_LIN2X(putlin[x], ms);
  7969. break;
  7970. case DAHDI_CONF_MONITORTX: /* Monitor a channel's tx mode */
  7971. /* if not a pseudo-channel, ignore */
  7972. if (!is_pseudo_chan(ms))
  7973. break;
  7974. /* Add monitored channel */
  7975. if (is_pseudo_chan(conf_chan))
  7976. ACSS(putlin, conf_chan->putlin);
  7977. else
  7978. ACSS(putlin, conf_chan->getlin);
  7979. /* Convert back */
  7980. for(x=0;x<DAHDI_CHUNKSIZE;x++)
  7981. rxb[x] = DAHDI_LIN2X(putlin[x], ms);
  7982. break;
  7983. case DAHDI_CONF_MONITORBOTH: /* Monitor a channel's tx and rx mode */
  7984. /* if not a pseudo-channel, ignore */
  7985. if (!is_pseudo_chan(ms))
  7986. break;
  7987. /* Note: Technically, saturation should be done at
  7988. the end of the whole addition, but for performance
  7989. reasons, we don't do that. Besides, it only matters
  7990. when you're so loud you're clipping anyway */
  7991. ACSS(putlin, conf_chan->getlin);
  7992. ACSS(putlin, conf_chan->putlin);
  7993. /* Convert back */
  7994. for(x=0;x<DAHDI_CHUNKSIZE;x++)
  7995. rxb[x] = DAHDI_LIN2X(putlin[x], ms);
  7996. break;
  7997. case DAHDI_CONF_MONITOR_RX_PREECHO: /* Monitor a channel's rx mode */
  7998. /* if not a pseudo-channel, ignore */
  7999. if (!is_pseudo_chan(ms))
  8000. break;
  8001. if (!conf_chan->readchunkpreec)
  8002. break;
  8003. /* Add monitored channel */
  8004. ACSS(putlin, is_pseudo_chan(conf_chan) ?
  8005. conf_chan->getlin : conf_chan->readchunkpreec);
  8006. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  8007. rxb[x] = DAHDI_LIN2X(putlin[x], ms);
  8008. break;
  8009. case DAHDI_CONF_MONITOR_TX_PREECHO: /* Monitor a channel's tx mode */
  8010. /* if not a pseudo-channel, ignore */
  8011. if (!is_pseudo_chan(ms))
  8012. break;
  8013. if (!conf_chan->readchunkpreec)
  8014. break;
  8015. /* Add monitored channel */
  8016. ACSS(putlin, is_pseudo_chan(conf_chan) ?
  8017. conf_chan->readchunkpreec : conf_chan->getlin);
  8018. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  8019. rxb[x] = DAHDI_LIN2X(putlin[x], ms);
  8020. break;
  8021. case DAHDI_CONF_MONITORBOTH_PREECHO: /* Monitor a channel's tx and rx mode */
  8022. /* if not a pseudo-channel, ignore */
  8023. if (!is_pseudo_chan(ms))
  8024. break;
  8025. if (!conf_chan->readchunkpreec)
  8026. break;
  8027. /* Note: Technically, saturation should be done at
  8028. the end of the whole addition, but for performance
  8029. reasons, we don't do that. Besides, it only matters
  8030. when you're so loud you're clipping anyway */
  8031. ACSS(putlin, conf_chan->getlin);
  8032. ACSS(putlin, conf_chan->readchunkpreec);
  8033. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  8034. rxb[x] = DAHDI_LIN2X(putlin[x], ms);
  8035. break;
  8036. case DAHDI_CONF_REALANDPSEUDO:
  8037. /* do normal conf mode processing */
  8038. if (ms->confmode & DAHDI_CONF_TALKER) {
  8039. /* Store temp value */
  8040. memcpy(k, putlin, DAHDI_CHUNKSIZE * sizeof(short));
  8041. /* Add conf value */
  8042. ACSS(k, conf_sums_next[ms->_confn]);
  8043. /* get amount actually added */
  8044. memcpy(ms->conflast, k, DAHDI_CHUNKSIZE * sizeof(short));
  8045. SCSS(ms->conflast, conf_sums_next[ms->_confn]);
  8046. /* Really add in new value */
  8047. ACSS(conf_sums_next[ms->_confn], ms->conflast);
  8048. } else memset(ms->conflast, 0, DAHDI_CHUNKSIZE * sizeof(short));
  8049. /* do the pseudo-channel part processing */
  8050. memset(putlin, 0, DAHDI_CHUNKSIZE * sizeof(short));
  8051. if (ms->confmode & DAHDI_CONF_PSEUDO_LISTENER) {
  8052. /* Subtract out previous last sample written to conf */
  8053. SCSS(putlin, ms->conflast2);
  8054. /* Add in conference */
  8055. ACSS(putlin, conf_sums[ms->_confn]);
  8056. }
  8057. /* Convert back */
  8058. for(x=0;x<DAHDI_CHUNKSIZE;x++)
  8059. rxb[x] = DAHDI_LIN2X(putlin[x], ms);
  8060. break;
  8061. case DAHDI_CONF_CONF: /* Normal conference mode */
  8062. if (is_pseudo_chan(ms)) /* if a pseudo-channel */
  8063. {
  8064. if (ms->confmode & DAHDI_CONF_LISTENER) {
  8065. /* Subtract out last sample written to conf */
  8066. SCSS(putlin, ms->conflast);
  8067. /* Add in conference */
  8068. ACSS(putlin, conf_sums[ms->_confn]);
  8069. }
  8070. /* Convert back */
  8071. for(x=0;x<DAHDI_CHUNKSIZE;x++)
  8072. rxb[x] = DAHDI_LIN2X(putlin[x], ms);
  8073. memcpy(ss->putlin, putlin, DAHDI_CHUNKSIZE * sizeof(short));
  8074. break;
  8075. }
  8076. /* fall through */
  8077. case DAHDI_CONF_CONFANN: /* Conference with announce */
  8078. if (ms->confmode & DAHDI_CONF_TALKER) {
  8079. /* Store temp value */
  8080. memcpy(k, putlin, DAHDI_CHUNKSIZE * sizeof(short));
  8081. /* Add conf value */
  8082. ACSS(k, conf_sums_next[ms->_confn]);
  8083. /* get amount actually added */
  8084. memcpy(ms->conflast, k, DAHDI_CHUNKSIZE * sizeof(short));
  8085. SCSS(ms->conflast, conf_sums_next[ms->_confn]);
  8086. /* Really add in new value */
  8087. ACSS(conf_sums_next[ms->_confn], ms->conflast);
  8088. } else
  8089. memset(ms->conflast, 0, DAHDI_CHUNKSIZE * sizeof(short));
  8090. /* rxc unmodified */
  8091. break;
  8092. case DAHDI_CONF_CONFMON:
  8093. case DAHDI_CONF_CONFANNMON:
  8094. if (ms->confmode & DAHDI_CONF_TALKER) {
  8095. /* Store temp value */
  8096. memcpy(k, putlin, DAHDI_CHUNKSIZE * sizeof(short));
  8097. /* Subtract last value */
  8098. SCSS(conf_sums[ms->_confn], ms->conflast);
  8099. /* Add conf value */
  8100. ACSS(k, conf_sums[ms->_confn]);
  8101. /* get amount actually added */
  8102. memcpy(ms->conflast, k, DAHDI_CHUNKSIZE * sizeof(short));
  8103. SCSS(ms->conflast, conf_sums[ms->_confn]);
  8104. /* Really add in new value */
  8105. ACSS(conf_sums[ms->_confn], ms->conflast);
  8106. } else
  8107. memset(ms->conflast, 0, DAHDI_CHUNKSIZE * sizeof(short));
  8108. for (x=0;x<DAHDI_CHUNKSIZE;x++)
  8109. rxb[x] = DAHDI_LIN2X((int)conf_sums_prev[ms->_confn][x], ms);
  8110. break;
  8111. case DAHDI_CONF_DIGITALMON:
  8112. /* if not a pseudo-channel, ignore */
  8113. if (!is_pseudo_chan(ms))
  8114. break;
  8115. /* Add monitored channel */
  8116. if (is_pseudo_chan(conf_chan))
  8117. memcpy(rxb, conf_chan->getraw, DAHDI_CHUNKSIZE);
  8118. else
  8119. memcpy(rxb, conf_chan->putraw, DAHDI_CHUNKSIZE);
  8120. break;
  8121. }
  8122. }
  8123. }
  8124. /* HDLC (or other) receiver buffer functions for read side */
  8125. static void __putbuf_chunk(struct dahdi_chan *ss, unsigned char *rxb, int bytes)
  8126. {
  8127. /* We transmit data from our master channel */
  8128. /* Called with ss->lock held */
  8129. struct dahdi_chan *ms = ss->master;
  8130. /* Our receive buffer */
  8131. unsigned char *buf;
  8132. #if defined(CONFIG_DAHDI_NET) || defined(CONFIG_DAHDI_PPP)
  8133. /* SKB for receiving network stuff */
  8134. struct sk_buff *skb=NULL;
  8135. #endif
  8136. int oldbuf;
  8137. int eof=0;
  8138. int abort=0;
  8139. int res;
  8140. int left, x;
  8141. while(bytes) {
  8142. #if defined(CONFIG_DAHDI_NET) || defined(CONFIG_DAHDI_PPP)
  8143. skb = NULL;
  8144. #endif
  8145. abort = 0;
  8146. eof = 0;
  8147. /* Next, figure out if we've got a buffer to receive into */
  8148. if (ms->inreadbuf > -1) {
  8149. /* Read into the current buffer */
  8150. buf = ms->readbuf[ms->inreadbuf];
  8151. left = ms->blocksize - ms->readidx[ms->inreadbuf];
  8152. if (left > bytes)
  8153. left = bytes;
  8154. if (ms->flags & DAHDI_FLAG_HDLC) {
  8155. for (x=0;x<left;x++) {
  8156. /* Handle HDLC deframing */
  8157. fasthdlc_rx_load_nocheck(&ms->rxhdlc, *(rxb++));
  8158. bytes--;
  8159. res = fasthdlc_rx_run(&ms->rxhdlc);
  8160. /* If there is nothing there, continue */
  8161. if (res & RETURN_EMPTY_FLAG)
  8162. continue;
  8163. else if (res & RETURN_COMPLETE_FLAG) {
  8164. /* Only count this if it's a non-empty frame */
  8165. if (ms->readidx[ms->inreadbuf]) {
  8166. if ((ms->flags & DAHDI_FLAG_FCS) && (ms->infcs != PPP_GOODFCS)) {
  8167. abort = DAHDI_EVENT_BADFCS;
  8168. } else
  8169. eof=1;
  8170. break;
  8171. }
  8172. continue;
  8173. } else if (res & RETURN_DISCARD_FLAG) {
  8174. /* This could be someone idling with
  8175. "idle" instead of "flag" */
  8176. if (!ms->readidx[ms->inreadbuf])
  8177. continue;
  8178. abort = DAHDI_EVENT_ABORT;
  8179. break;
  8180. } else {
  8181. unsigned char rxc;
  8182. rxc = res;
  8183. ms->infcs = PPP_FCS(ms->infcs, rxc);
  8184. buf[ms->readidx[ms->inreadbuf]++] = rxc;
  8185. /* Pay attention to the possibility of an overrun */
  8186. if (ms->readidx[ms->inreadbuf] >= ms->blocksize) {
  8187. if (!ss->span->alarms)
  8188. module_printk(KERN_WARNING, "HDLC Receiver overrun on channel %s (master=%s)\n", ss->name, ss->master->name);
  8189. abort=DAHDI_EVENT_OVERRUN;
  8190. /* Force the HDLC state back to frame-search mode */
  8191. ms->rxhdlc.state = 0;
  8192. ms->rxhdlc.bits = 0;
  8193. ms->readidx[ms->inreadbuf]=0;
  8194. break;
  8195. }
  8196. }
  8197. }
  8198. } else {
  8199. /* Not HDLC */
  8200. memcpy(buf + ms->readidx[ms->inreadbuf], rxb, left);
  8201. rxb += left;
  8202. ms->readidx[ms->inreadbuf] += left;
  8203. bytes -= left;
  8204. /* End of frame is decided by block size of 'N' */
  8205. eof = (ms->readidx[ms->inreadbuf] >= ms->blocksize);
  8206. if (eof && (ss->flags & DAHDI_FLAG_NOSTDTXRX)) {
  8207. eof = 0;
  8208. abort = DAHDI_EVENT_OVERRUN;
  8209. }
  8210. }
  8211. if (eof) {
  8212. /* Finished with this buffer, try another. */
  8213. oldbuf = ms->inreadbuf;
  8214. ms->infcs = PPP_INITFCS;
  8215. ms->readn[ms->inreadbuf] = ms->readidx[ms->inreadbuf];
  8216. #ifdef CONFIG_DAHDI_DEBUG
  8217. module_printk(KERN_NOTICE, "EOF, len is %d\n", ms->readn[ms->inreadbuf]);
  8218. #endif
  8219. #if defined(CONFIG_DAHDI_NET) || defined(CONFIG_DAHDI_PPP)
  8220. if ((ms->flags & DAHDI_FLAG_PPP) ||
  8221. dahdi_have_netdev(ms)) {
  8222. #ifdef CONFIG_DAHDI_NET
  8223. #endif /* CONFIG_DAHDI_NET */
  8224. /* Our network receiver logic is MUCH
  8225. different. We actually only use a single
  8226. buffer */
  8227. if (ms->readn[ms->inreadbuf] > 1) {
  8228. /* Drop the FCS */
  8229. ms->readn[ms->inreadbuf] -= 2;
  8230. /* Allocate an SKB */
  8231. #ifdef CONFIG_DAHDI_PPP
  8232. if (!ms->do_ppp_error)
  8233. #endif
  8234. skb = dev_alloc_skb(ms->readn[ms->inreadbuf] + 2);
  8235. if (skb) {
  8236. unsigned char cisco_addr = *(ms->readbuf[ms->inreadbuf]);
  8237. if (cisco_addr != 0x0f && cisco_addr != 0x8f)
  8238. skb_reserve(skb, 2);
  8239. /* XXX Get rid of this memcpy XXX */
  8240. memcpy(skb->data, ms->readbuf[ms->inreadbuf], ms->readn[ms->inreadbuf]);
  8241. skb_put(skb, ms->readn[ms->inreadbuf]);
  8242. #ifdef CONFIG_DAHDI_NET
  8243. if (dahdi_have_netdev(ms)) {
  8244. struct net_device_stats *stats = hdlc_stats(ms->hdlcnetdev->netdev);
  8245. stats->rx_packets++;
  8246. stats->rx_bytes += ms->readn[ms->inreadbuf];
  8247. }
  8248. #endif
  8249. } else {
  8250. #ifdef CONFIG_DAHDI_NET
  8251. if (dahdi_have_netdev(ms)) {
  8252. struct net_device_stats *stats = hdlc_stats(ms->hdlcnetdev->netdev);
  8253. stats->rx_dropped++;
  8254. }
  8255. #endif
  8256. #ifdef CONFIG_DAHDI_PPP
  8257. if (ms->flags & DAHDI_FLAG_PPP) {
  8258. abort = DAHDI_EVENT_OVERRUN;
  8259. }
  8260. #endif
  8261. #if 1
  8262. #ifdef CONFIG_DAHDI_PPP
  8263. if (!ms->do_ppp_error)
  8264. #endif
  8265. module_printk(KERN_NOTICE, "Memory squeeze, dropped one\n");
  8266. #endif
  8267. }
  8268. }
  8269. /* We don't cycle through buffers, just
  8270. reuse the same one */
  8271. ms->readn[ms->inreadbuf] = 0;
  8272. ms->readidx[ms->inreadbuf] = 0;
  8273. } else
  8274. #endif
  8275. {
  8276. /* This logic might confuse and astound. Basically we need to find
  8277. * the previous buffer index. It should be safe because, regardless
  8278. * of whether or not it has been copied to user space, nothing should
  8279. * have messed around with it since then */
  8280. int comparemessage;
  8281. /* Shut compiler up */
  8282. int myres = 0;
  8283. if (ms->flags & DAHDI_FLAG_MTP2) {
  8284. comparemessage = (ms->inreadbuf - 1) & (ms->numbufs - 1);
  8285. myres = memcmp(ms->readbuf[comparemessage], ms->readbuf[ms->inreadbuf], ms->readn[ms->inreadbuf]);
  8286. }
  8287. if ((ms->flags & DAHDI_FLAG_MTP2) && !myres) {
  8288. /* Our messages are the same, so discard -
  8289. * Don't advance buffers, reset indexes and buffer sizes. */
  8290. ms->readn[ms->inreadbuf] = 0;
  8291. ms->readidx[ms->inreadbuf] = 0;
  8292. } else {
  8293. ms->inreadbuf = (ms->inreadbuf + 1) % ms->numbufs;
  8294. if (ms->inreadbuf == ms->outreadbuf) {
  8295. /* Whoops, we're full, and have no where else
  8296. to store into at the moment. We'll drop it
  8297. until there's a buffer available */
  8298. #ifdef BUFFER_DEBUG
  8299. module_printk(KERN_NOTICE, "Out of storage space\n");
  8300. #endif
  8301. ms->inreadbuf = -1;
  8302. }
  8303. if (ms->outreadbuf < 0) { /* start out buffer if not already */
  8304. ms->outreadbuf = oldbuf;
  8305. /* if there are processes waiting in poll() on this channel,
  8306. wake them up */
  8307. wake_up_interruptible(&ms->waitq);
  8308. }
  8309. /* In the very orignal driver, it was quite well known to me (Jim) that there
  8310. was a possibility that a channel sleeping on a receive block needed to
  8311. be potentially woken up EVERY time a buffer was filled, not just on the first
  8312. one, because if only done on the first one there is a slight timing potential
  8313. of missing the wakeup (between where it senses the (lack of) active condition
  8314. (with interrupts disabled) and where it does the sleep (interrupts enabled)
  8315. in the read or iomux call, etc). That is why the read and iomux calls start
  8316. with an infinite loop that gets broken out of upon an active condition,
  8317. otherwise keeps sleeping and looking. The part in this code got "optimized"
  8318. out in the later versions, and is put back now. Note that this is *NOT*
  8319. needed for poll() waiters, because the poll_wait() function that is used there
  8320. is atomic enough for this purpose; it will not go to sleep before ensuring
  8321. that the waitqueue is empty. */
  8322. /* Notify a blocked reader that there is data available
  8323. to be read, unless we're waiting for it to be full */
  8324. #ifdef CONFIG_DAHDI_DEBUG
  8325. module_printk(KERN_NOTICE, "Notifying reader data in block %d\n", oldbuf);
  8326. #endif
  8327. wake_up_interruptible(&ms->waitq);
  8328. }
  8329. }
  8330. }
  8331. if (abort) {
  8332. /* Start over reading frame */
  8333. ms->readidx[ms->inreadbuf] = 0;
  8334. ms->infcs = PPP_INITFCS;
  8335. #ifdef CONFIG_DAHDI_NET
  8336. if (dahdi_have_netdev(ms)) {
  8337. struct net_device_stats *stats = hdlc_stats(ms->hdlcnetdev->netdev);
  8338. stats->rx_errors++;
  8339. if (abort == DAHDI_EVENT_OVERRUN)
  8340. stats->rx_over_errors++;
  8341. if (abort == DAHDI_EVENT_BADFCS)
  8342. stats->rx_crc_errors++;
  8343. if (abort == DAHDI_EVENT_ABORT)
  8344. stats->rx_frame_errors++;
  8345. } else
  8346. #endif
  8347. #ifdef CONFIG_DAHDI_PPP
  8348. if (ms->flags & DAHDI_FLAG_PPP) {
  8349. ms->do_ppp_error = 1;
  8350. tasklet_schedule(&ms->ppp_calls);
  8351. } else
  8352. #endif
  8353. if (test_bit(DAHDI_FLAGBIT_OPEN, &ms->flags) && !ss->span->alarms) {
  8354. /* Notify the receiver... */
  8355. __qevent(ss->master, abort);
  8356. }
  8357. }
  8358. } else /* No place to receive -- drop on the floor */
  8359. break;
  8360. #ifdef CONFIG_DAHDI_NET
  8361. if (skb && dahdi_have_netdev(ms))
  8362. {
  8363. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
  8364. skb->mac.raw = skb->data;
  8365. #else
  8366. skb_reset_mac_header(skb);
  8367. #endif
  8368. skb->dev = chan_to_netdev(ms);
  8369. #ifdef DAHDI_HDLC_TYPE_TRANS
  8370. skb->protocol = hdlc_type_trans(skb,
  8371. chan_to_netdev(ms));
  8372. #else
  8373. skb->protocol = htons (ETH_P_HDLC);
  8374. #endif
  8375. netif_rx(skb);
  8376. }
  8377. #endif
  8378. #ifdef CONFIG_DAHDI_PPP
  8379. if (skb && (ms->flags & DAHDI_FLAG_PPP)) {
  8380. unsigned char *tmp;
  8381. tmp = skb->data;
  8382. skb_pull(skb, 2);
  8383. /* Make sure that it's addressed to ALL STATIONS and UNNUMBERED */
  8384. if (!tmp || (tmp[0] != 0xff) || (tmp[1] != 0x03)) {
  8385. /* Invalid SKB -- drop */
  8386. if (tmp)
  8387. module_printk(KERN_NOTICE, "Received invalid SKB (%02x, %02x)\n", tmp[0], tmp[1]);
  8388. dev_kfree_skb_irq(skb);
  8389. } else {
  8390. skb_queue_tail(&ms->ppp_rq, skb);
  8391. tasklet_schedule(&ms->ppp_calls);
  8392. }
  8393. }
  8394. #endif
  8395. }
  8396. if (bytes) {
  8397. if (!test_bit(DAHDI_FLAGBIT_RXOVERRUN, &ms->flags)) {
  8398. if (test_bit(DAHDI_FLAGBIT_BUFEVENTS, &ms->flags))
  8399. __qevent(ms, DAHDI_EVENT_READ_OVERRUN);
  8400. set_bit(DAHDI_FLAGBIT_RXOVERRUN, &ms->flags);
  8401. }
  8402. } else {
  8403. clear_bit(DAHDI_FLAGBIT_RXOVERRUN, &ms->flags);
  8404. }
  8405. }
  8406. static inline void __dahdi_putbuf_chunk(struct dahdi_chan *ss, unsigned char *rxb)
  8407. {
  8408. __putbuf_chunk(ss, rxb, DAHDI_CHUNKSIZE);
  8409. #ifdef CONFIG_DAHDI_MIRROR
  8410. if (ss->rxmirror) {
  8411. spin_lock(&ss->rxmirror->lock);
  8412. __putbuf_chunk(ss->rxmirror, rxb, DAHDI_CHUNKSIZE);
  8413. spin_unlock(&ss->rxmirror->lock);
  8414. }
  8415. #endif /* CONFIG_DAHDI_MIRROR */
  8416. }
  8417. static void __dahdi_hdlc_abort(struct dahdi_chan *ss, int event)
  8418. {
  8419. if (ss->inreadbuf >= 0)
  8420. ss->readidx[ss->inreadbuf] = 0;
  8421. if (test_bit(DAHDI_FLAGBIT_OPEN, &ss->flags) && !ss->span->alarms)
  8422. __qevent(ss->master, event);
  8423. }
  8424. void dahdi_hdlc_abort(struct dahdi_chan *ss, int event)
  8425. {
  8426. unsigned long flags;
  8427. spin_lock_irqsave(&ss->lock, flags);
  8428. __dahdi_hdlc_abort(ss, event);
  8429. spin_unlock_irqrestore(&ss->lock, flags);
  8430. }
  8431. void dahdi_hdlc_putbuf(struct dahdi_chan *ss, unsigned char *rxb, int bytes)
  8432. {
  8433. unsigned long flags;
  8434. int left;
  8435. spin_lock_irqsave(&ss->lock, flags);
  8436. if (ss->inreadbuf < 0) {
  8437. #ifdef CONFIG_DAHDI_DEBUG
  8438. module_printk(KERN_NOTICE, "No place to receive HDLC frame\n");
  8439. #endif
  8440. spin_unlock_irqrestore(&ss->lock, flags);
  8441. return;
  8442. }
  8443. /* Read into the current buffer */
  8444. left = ss->blocksize - ss->readidx[ss->inreadbuf];
  8445. if (left > bytes)
  8446. left = bytes;
  8447. if (left > 0) {
  8448. memcpy(ss->readbuf[ss->inreadbuf] + ss->readidx[ss->inreadbuf], rxb, left);
  8449. rxb += left;
  8450. ss->readidx[ss->inreadbuf] += left;
  8451. bytes -= left;
  8452. }
  8453. /* Something isn't fit into buffer */
  8454. if (bytes) {
  8455. #ifdef CONFIG_DAHDI_DEBUG
  8456. module_printk(KERN_NOTICE, "HDLC frame isn't fit into buffer space\n");
  8457. #endif
  8458. __dahdi_hdlc_abort(ss, DAHDI_EVENT_OVERRUN);
  8459. }
  8460. spin_unlock_irqrestore(&ss->lock, flags);
  8461. }
  8462. void dahdi_hdlc_finish(struct dahdi_chan *ss)
  8463. {
  8464. int oldreadbuf;
  8465. unsigned long flags;
  8466. spin_lock_irqsave(&ss->lock, flags);
  8467. if ((oldreadbuf = ss->inreadbuf) < 0) {
  8468. #ifdef CONFIG_DAHDI_DEBUG
  8469. module_printk(KERN_NOTICE, "No buffers to finish\n");
  8470. #endif
  8471. spin_unlock_irqrestore(&ss->lock, flags);
  8472. return;
  8473. }
  8474. if (!ss->readidx[ss->inreadbuf]) {
  8475. #ifdef CONFIG_DAHDI_DEBUG
  8476. module_printk(KERN_NOTICE, "Empty HDLC frame received\n");
  8477. #endif
  8478. spin_unlock_irqrestore(&ss->lock, flags);
  8479. return;
  8480. }
  8481. ss->readn[ss->inreadbuf] = ss->readidx[ss->inreadbuf];
  8482. ss->inreadbuf = (ss->inreadbuf + 1) % ss->numbufs;
  8483. if (ss->inreadbuf == ss->outreadbuf) {
  8484. ss->inreadbuf = -1;
  8485. #ifdef CONFIG_DAHDI_DEBUG
  8486. module_printk(KERN_NOTICE, "Notifying reader data in block %d\n", oldreadbuf);
  8487. #endif
  8488. }
  8489. if (ss->outreadbuf < 0) {
  8490. ss->outreadbuf = oldreadbuf;
  8491. }
  8492. wake_up_interruptible(&ss->waitq);
  8493. spin_unlock_irqrestore(&ss->lock, flags);
  8494. }
  8495. /* Returns 1 if EOF, 0 if data is still in frame, -1 if EOF and no buffers left */
  8496. int dahdi_hdlc_getbuf(struct dahdi_chan *ss, unsigned char *bufptr, unsigned int *size)
  8497. {
  8498. unsigned char *buf;
  8499. unsigned long flags;
  8500. int left = 0;
  8501. int res;
  8502. int oldbuf;
  8503. spin_lock_irqsave(&ss->lock, flags);
  8504. if (ss->outwritebuf > -1) {
  8505. buf = ss->writebuf[ss->outwritebuf];
  8506. left = ss->writen[ss->outwritebuf] - ss->writeidx[ss->outwritebuf];
  8507. /* Strip off the empty HDLC CRC end */
  8508. left -= 2;
  8509. if (left <= *size) {
  8510. *size = left;
  8511. res = 1;
  8512. } else
  8513. res = 0;
  8514. memcpy(bufptr, &buf[ss->writeidx[ss->outwritebuf]], *size);
  8515. ss->writeidx[ss->outwritebuf] += *size;
  8516. if (res) {
  8517. /* Rotate buffers */
  8518. oldbuf = ss->outwritebuf;
  8519. ss->writeidx[oldbuf] = 0;
  8520. ss->writen[oldbuf] = 0;
  8521. ss->outwritebuf = (ss->outwritebuf + 1) % ss->numbufs;
  8522. if (ss->outwritebuf == ss->inwritebuf) {
  8523. ss->outwritebuf = -1;
  8524. if (ss->iomask & (DAHDI_IOMUX_WRITE | DAHDI_IOMUX_WRITEEMPTY))
  8525. wake_up_interruptible(&ss->waitq);
  8526. /* If we're only supposed to start when full, disable the transmitter */
  8527. if ((ss->txbufpolicy == DAHDI_POLICY_WHEN_FULL) || (ss->txbufpolicy == DAHDI_POLICY_HALF_FULL))
  8528. ss->txdisable = 1;
  8529. res = -1;
  8530. }
  8531. if (ss->inwritebuf < 0)
  8532. ss->inwritebuf = oldbuf;
  8533. if (!(ss->flags & DAHDI_FLAG_PPP) ||
  8534. !dahdi_have_netdev(ss)) {
  8535. wake_up_interruptible(&ss->waitq);
  8536. }
  8537. }
  8538. } else {
  8539. res = -1;
  8540. *size = 0;
  8541. }
  8542. spin_unlock_irqrestore(&ss->lock, flags);
  8543. return res;
  8544. }
  8545. static void process_timers(void)
  8546. {
  8547. struct dahdi_timer *cur;
  8548. if (list_empty(&dahdi_timers))
  8549. return;
  8550. spin_lock(&dahdi_timer_lock);
  8551. list_for_each_entry(cur, &dahdi_timers, list) {
  8552. spin_lock(&cur->lock);
  8553. cur->pos -= DAHDI_CHUNKSIZE;
  8554. if (cur->pos <= 0) {
  8555. cur->tripped++;
  8556. cur->pos = cur->ms;
  8557. wake_up_interruptible(&cur->sel);
  8558. }
  8559. spin_unlock(&cur->lock);
  8560. }
  8561. spin_unlock(&dahdi_timer_lock);
  8562. }
  8563. static unsigned int dahdi_timer_poll(struct file *file, struct poll_table_struct *wait_table)
  8564. {
  8565. struct dahdi_timer *timer = file->private_data;
  8566. unsigned long flags;
  8567. int ret = 0;
  8568. if (timer) {
  8569. poll_wait(file, &timer->sel, wait_table);
  8570. spin_lock_irqsave(&timer->lock, flags);
  8571. if (timer->tripped || timer->ping)
  8572. ret |= POLLPRI;
  8573. spin_unlock_irqrestore(&timer->lock, flags);
  8574. } else {
  8575. /*
  8576. * This should never happen. Surprise device removal
  8577. * should lead us to the nodev_* file_operations
  8578. */
  8579. msleep(5);
  8580. module_printk(KERN_ERR, "%s: NODEV\n", __func__);
  8581. return POLLERR | POLLHUP | POLLRDHUP | POLLNVAL | POLLPRI;
  8582. }
  8583. return ret;
  8584. }
  8585. /* device poll routine */
  8586. static unsigned int
  8587. dahdi_chan_poll(struct file *file, struct poll_table_struct *wait_table)
  8588. {
  8589. struct dahdi_chan *const c = file->private_data;
  8590. int ret = 0;
  8591. unsigned long flags;
  8592. if (unlikely(!c)) {
  8593. /*
  8594. * This should never happen. Surprise device removal
  8595. * should lead us to the nodev_* file_operations
  8596. */
  8597. msleep(5);
  8598. module_printk(KERN_ERR, "%s: NODEV\n", __func__);
  8599. return POLLERR | POLLHUP | POLLRDHUP | POLLNVAL | POLLPRI;
  8600. }
  8601. poll_wait(file, &c->waitq, wait_table);
  8602. spin_lock_irqsave(&c->lock, flags);
  8603. ret |= (c->inwritebuf > -1) ? POLLOUT|POLLWRNORM : 0;
  8604. ret |= (c->outreadbuf > -1) ? POLLIN|POLLRDNORM : 0;
  8605. ret |= (c->eventoutidx != c->eventinidx) ? POLLPRI : 0;
  8606. spin_unlock_irqrestore(&c->lock, flags);
  8607. return ret;
  8608. }
  8609. static unsigned int dahdi_poll(struct file *file, struct poll_table_struct *wait_table)
  8610. {
  8611. const int unit = UNIT(file);
  8612. if (likely(unit == DAHDI_TIMER))
  8613. return dahdi_timer_poll(file, wait_table);
  8614. /* transcoders and channels should have updated their file_operations
  8615. * before poll is ever called. */
  8616. return -EINVAL;
  8617. }
  8618. static void __dahdi_transmit_chunk(struct dahdi_chan *chan, unsigned char *buf)
  8619. {
  8620. unsigned char silly[DAHDI_CHUNKSIZE];
  8621. /* Called with chan->lock locked */
  8622. #ifdef OPTIMIZE_CHANMUTE
  8623. if(likely(chan->chanmute))
  8624. return;
  8625. #endif
  8626. if (!buf)
  8627. buf = silly;
  8628. __dahdi_getbuf_chunk(chan, buf);
  8629. if ((chan->flags & DAHDI_FLAG_AUDIO) || (chan->confmode)) {
  8630. #ifdef CONFIG_DAHDI_MMX
  8631. dahdi_kernel_fpu_begin();
  8632. #endif
  8633. __dahdi_process_getaudio_chunk(chan, buf);
  8634. #ifdef CONFIG_DAHDI_MMX
  8635. dahdi_kernel_fpu_end();
  8636. #endif
  8637. }
  8638. }
  8639. static inline void __dahdi_real_transmit(struct dahdi_chan *chan)
  8640. {
  8641. /* Called with chan->lock held */
  8642. #ifdef OPTIMIZE_CHANMUTE
  8643. if(likely(chan->chanmute))
  8644. return;
  8645. #endif
  8646. if (chan->confmode) {
  8647. /* Pull queued data off the conference */
  8648. __buf_pull(&chan->confout, chan->writechunk, chan);
  8649. } else {
  8650. __dahdi_transmit_chunk(chan, chan->writechunk);
  8651. }
  8652. }
  8653. static void __dahdi_getempty(struct dahdi_chan *ms, unsigned char *buf)
  8654. {
  8655. int bytes = DAHDI_CHUNKSIZE;
  8656. int left;
  8657. unsigned char *txb = buf;
  8658. int x;
  8659. short getlin;
  8660. /* Called with ms->lock held */
  8661. while(bytes) {
  8662. /* Receive silence, or tone */
  8663. if (ms->curtone) {
  8664. left = ms->curtone->tonesamples - ms->tonep;
  8665. if (left > bytes)
  8666. left = bytes;
  8667. for (x=0;x<left;x++) {
  8668. /* Pick our default value from the next sample of the current tone */
  8669. getlin = dahdi_tone_nextsample(&ms->ts, ms->curtone);
  8670. *(txb++) = DAHDI_LIN2X(getlin, ms);
  8671. }
  8672. ms->tonep+=left;
  8673. bytes -= left;
  8674. if (ms->tonep >= ms->curtone->tonesamples) {
  8675. struct dahdi_tone *last;
  8676. /* Go to the next sample of the tone */
  8677. ms->tonep = 0;
  8678. last = ms->curtone;
  8679. ms->curtone = ms->curtone->next;
  8680. if (!ms->curtone) {
  8681. /* No more tones... Is this dtmf or mf? If so, go to the next digit */
  8682. if (ms->dialing)
  8683. __do_dtmf(ms);
  8684. } else {
  8685. if (last != ms->curtone)
  8686. dahdi_init_tone_state(&ms->ts, ms->curtone);
  8687. }
  8688. }
  8689. } else {
  8690. /* Use silence */
  8691. memset(txb, DAHDI_LIN2X(0, ms), bytes);
  8692. bytes = 0;
  8693. }
  8694. }
  8695. }
  8696. static void __dahdi_receive_chunk(struct dahdi_chan *chan, unsigned char *buf)
  8697. {
  8698. /* Receive chunk of audio -- called with chan->lock held */
  8699. unsigned char waste[DAHDI_CHUNKSIZE];
  8700. #ifdef OPTIMIZE_CHANMUTE
  8701. if(likely(chan->chanmute))
  8702. return;
  8703. #endif
  8704. if (!buf) {
  8705. memset(waste, DAHDI_LIN2X(0, chan), sizeof(waste));
  8706. buf = waste;
  8707. }
  8708. if ((chan->flags & DAHDI_FLAG_AUDIO) || (chan->confmode)) {
  8709. #ifdef CONFIG_DAHDI_MMX
  8710. dahdi_kernel_fpu_begin();
  8711. #endif
  8712. __dahdi_process_putaudio_chunk(chan, buf);
  8713. #ifdef CONFIG_DAHDI_MMX
  8714. dahdi_kernel_fpu_end();
  8715. #endif
  8716. }
  8717. __dahdi_putbuf_chunk(chan, buf);
  8718. }
  8719. static inline void __dahdi_real_receive(struct dahdi_chan *chan)
  8720. {
  8721. /* Called with chan->lock held */
  8722. #ifdef OPTIMIZE_CHANMUTE
  8723. if(likely(chan->chanmute))
  8724. return;
  8725. #endif
  8726. if (chan->confmode) {
  8727. /* Load into queue if we have space */
  8728. __buf_push(&chan->confin, chan->readchunk);
  8729. } else {
  8730. __dahdi_receive_chunk(chan, chan->readchunk);
  8731. }
  8732. }
  8733. /**
  8734. * __transmit_to_slaves() - Distribute the tx data to all the slave channels.
  8735. *
  8736. */
  8737. static void __transmit_to_slaves(struct dahdi_chan *const chan)
  8738. {
  8739. u_char data[DAHDI_CHUNKSIZE];
  8740. int i;
  8741. int pos = DAHDI_CHUNKSIZE;
  8742. struct dahdi_chan *slave;
  8743. for (i = 0; i < DAHDI_CHUNKSIZE; i++) {
  8744. for (slave = chan; (NULL != slave); slave = slave->nextslave) {
  8745. if (pos == DAHDI_CHUNKSIZE) {
  8746. __dahdi_transmit_chunk(chan, data);
  8747. pos = 0;
  8748. }
  8749. slave->writechunk[i] = data[pos++];
  8750. }
  8751. }
  8752. }
  8753. int _dahdi_transmit(struct dahdi_span *span)
  8754. {
  8755. unsigned int x;
  8756. for (x=0;x<span->channels;x++) {
  8757. struct dahdi_chan *const chan = span->chans[x];
  8758. spin_lock(&chan->lock);
  8759. if (unlikely(chan->flags & DAHDI_FLAG_NOSTDTXRX)) {
  8760. spin_unlock(&chan->lock);
  8761. continue;
  8762. }
  8763. if (chan == chan->master) {
  8764. if (is_chan_dacsed(chan)) {
  8765. struct dahdi_chan *const src = chan->dacs_chan;
  8766. memcpy(chan->writechunk, src->readchunk,
  8767. DAHDI_CHUNKSIZE);
  8768. if (chan->sig == DAHDI_SIG_DACS_RBS) {
  8769. /* Just set bits for our destination */
  8770. if (chan->txsig != src->rxsig) {
  8771. chan->txsig = src->rxsig;
  8772. span->ops->rbsbits(chan, src->rxsig);
  8773. }
  8774. }
  8775. /* there is no further processing to do for
  8776. * DACS channels, so jump to the next channel
  8777. * in the span */
  8778. spin_unlock(&chan->lock);
  8779. continue;
  8780. } else if (chan->nextslave) {
  8781. __transmit_to_slaves(chan);
  8782. } else {
  8783. /* Process a normal channel */
  8784. __dahdi_real_transmit(chan);
  8785. }
  8786. if (chan->otimer) {
  8787. chan->otimer -= DAHDI_CHUNKSIZE;
  8788. if (chan->otimer <= 0)
  8789. __rbs_otimer_expire(chan);
  8790. }
  8791. }
  8792. spin_unlock(&chan->lock);
  8793. }
  8794. if (span->mainttimer) {
  8795. span->mainttimer -= DAHDI_CHUNKSIZE;
  8796. if (span->mainttimer <= 0) {
  8797. span->mainttimer = 0;
  8798. span->maintstat = 0;
  8799. }
  8800. }
  8801. return 0;
  8802. }
  8803. EXPORT_SYMBOL(_dahdi_transmit);
  8804. static inline void __pseudo_rx_audio(struct dahdi_chan *chan)
  8805. {
  8806. unsigned char tmp[DAHDI_CHUNKSIZE];
  8807. spin_lock(&chan->lock);
  8808. __dahdi_getempty(chan, tmp);
  8809. __dahdi_receive_chunk(chan, tmp);
  8810. spin_unlock(&chan->lock);
  8811. }
  8812. #ifdef CONFIG_DAHDI_MIRROR
  8813. static inline void pseudo_rx_audio(struct dahdi_chan *chan)
  8814. {
  8815. if (!chan->srcmirror)
  8816. __pseudo_rx_audio(chan);
  8817. }
  8818. #else
  8819. static inline void pseudo_rx_audio(struct dahdi_chan *chan)
  8820. {
  8821. __pseudo_rx_audio(chan);
  8822. }
  8823. #endif /* CONFIG_DAHDI_MIRROR */
  8824. #ifdef DAHDI_SYNC_TICK
  8825. static inline void dahdi_sync_tick(struct dahdi_span *const s)
  8826. {
  8827. if (s->ops->sync_tick)
  8828. s->ops->sync_tick(s, dahdi_is_sync_master(s));
  8829. }
  8830. #else
  8831. #define dahdi_sync_tick(x) do { ; } while (0)
  8832. #endif
  8833. /**
  8834. * _process_masterspan - Handle conferencing and timers.
  8835. *
  8836. * There are three sets of conference sum accumulators. One for the current
  8837. * sample chunk (conf_sums), one for the next sample chunk (conf_sums_next), and
  8838. * one for the previous sample chunk (conf_sums_prev). The following routine
  8839. * (rotate_sums) "rotates" the pointers to these accululator arrays as part
  8840. * of the events of sample chink processing as follows:
  8841. *
  8842. * 1. All (real span) receive chunks are processed (with putbuf). The last one
  8843. * to be processed is the master span. The data received is loaded into the
  8844. * accumulators for the next chunk (conf_sums_next), to be in alignment with
  8845. * current data after rotate_sums() is called (which immediately follows).
  8846. * Keep in mind that putbuf is *also* a transmit routine for the pseudo parts
  8847. * of channels that are in the REALANDPSEUDO conference mode. These channels
  8848. * are processed from data in the current sample chunk (conf_sums), being
  8849. * that this is a "transmit" function (for the pseudo part).
  8850. *
  8851. * 2. rotate_sums() is called.
  8852. *
  8853. * 3. All pseudo channel receive chunks are processed. This data is loaded into
  8854. * the current sample chunk accumulators (conf_sums).
  8855. *
  8856. * 4. All conference links are processed (being that all receive data for this
  8857. * chunk has already been processed by now).
  8858. *
  8859. * 5. All pseudo channel transmit chunks are processed. This data is loaded from
  8860. * the current sample chunk accumulators (conf_sums).
  8861. *
  8862. * 6. All (real span) transmit chunks are processed (with getbuf). This data is
  8863. * loaded from the current sample chunk accumulators (conf_sums). Keep in mind
  8864. * that getbuf is *also* a receive routine for the pseudo part of channels that
  8865. * are in the REALANDPSEUDO conference mode. These samples are loaded into
  8866. * the next sample chunk accumulators (conf_sums_next) to be processed as part
  8867. * of the next sample chunk's data (next time around the world).
  8868. *
  8869. */
  8870. static void _process_masterspan(void)
  8871. {
  8872. int x;
  8873. struct pseudo_chan *pseudo;
  8874. struct dahdi_span *s;
  8875. u_char *data;
  8876. #ifdef CONFIG_DAHDI_CORE_TIMER
  8877. /* We increment the calls since start here, so that if we switch over
  8878. * to the core timer, we know how many times we need to call
  8879. * process_masterspan in order to catch up since this function needs
  8880. * to be called (1000 / (DAHDI_CHUNKSIZE / 8)) times per second. */
  8881. atomic_inc(&core_timer.count);
  8882. #endif
  8883. /* Hold the chan_lock for the duration of major
  8884. activities which touch all sorts of channels */
  8885. spin_lock(&chan_lock);
  8886. /* Process any timers */
  8887. process_timers();
  8888. list_for_each_entry(s, &span_list, spans_node) {
  8889. for (x = 0; x < s->channels; ++x) {
  8890. struct dahdi_chan *const chan = s->chans[x];
  8891. if (!chan->confmode)
  8892. continue;
  8893. spin_lock(&chan->lock);
  8894. data = __buf_peek(&chan->confin);
  8895. __dahdi_receive_chunk(chan, data);
  8896. if (data)
  8897. __buf_pull(&chan->confin, NULL, chan);
  8898. spin_unlock(&chan->lock);
  8899. }
  8900. }
  8901. /* This is the master channel, so make things switch over */
  8902. rotate_sums();
  8903. /* do all the pseudo and/or conferenced channel receives (getbuf's) */
  8904. list_for_each_entry(pseudo, &pseudo_chans, node) {
  8905. spin_lock(&pseudo->chan.lock);
  8906. __dahdi_transmit_chunk(&pseudo->chan, NULL);
  8907. spin_unlock(&pseudo->chan.lock);
  8908. }
  8909. #ifdef CONFIG_DAHDI_CONFLINK
  8910. if (maxlinks) {
  8911. int z;
  8912. int y;
  8913. #ifdef CONFIG_DAHDI_MMX
  8914. dahdi_kernel_fpu_begin();
  8915. #endif
  8916. /* process all the conf links */
  8917. for (x = 1; x <= maxlinks; x++) {
  8918. /* if we have a destination conf */
  8919. z = confalias[conf_links[x].dst];
  8920. if (z) {
  8921. y = confalias[conf_links[x].src];
  8922. if (y)
  8923. ACSS(conf_sums[z], conf_sums[y]);
  8924. }
  8925. }
  8926. #ifdef CONFIG_DAHDI_MMX
  8927. dahdi_kernel_fpu_end();
  8928. #endif
  8929. }
  8930. #endif /* CONFIG_DAHDI_CONFLINK */
  8931. /* do all the pseudo/conferenced channel transmits (putbuf's) */
  8932. list_for_each_entry(pseudo, &pseudo_chans, node) {
  8933. pseudo_rx_audio(&pseudo->chan);
  8934. }
  8935. list_for_each_entry(s, &span_list, spans_node) {
  8936. for (x = 0; x < s->channels; x++) {
  8937. struct dahdi_chan *const chan = s->chans[x];
  8938. if (!chan->confmode)
  8939. continue;
  8940. spin_lock(&chan->lock);
  8941. data = __buf_pushpeek(&chan->confout);
  8942. __dahdi_transmit_chunk(chan, data);
  8943. if (data)
  8944. __buf_push(&chan->confout, NULL);
  8945. spin_unlock(&chan->lock);
  8946. }
  8947. dahdi_sync_tick(s);
  8948. }
  8949. spin_unlock(&chan_lock);
  8950. }
  8951. #ifndef CONFIG_DAHDI_CORE_TIMER
  8952. static void coretimer_init(void)
  8953. {
  8954. return;
  8955. }
  8956. static void coretimer_cleanup(void)
  8957. {
  8958. return;
  8959. }
  8960. #else
  8961. static unsigned long core_diff_ms(struct timespec *t0, struct timespec *t1)
  8962. {
  8963. long nanosec, sec;
  8964. unsigned long ms;
  8965. sec = (t1->tv_sec - t0->tv_sec);
  8966. nanosec = (t1->tv_nsec - t0->tv_nsec);
  8967. while (nanosec >= NSEC_PER_SEC) {
  8968. nanosec -= NSEC_PER_SEC;
  8969. ++sec;
  8970. }
  8971. while (nanosec < 0) {
  8972. nanosec += NSEC_PER_SEC;
  8973. --sec;
  8974. }
  8975. ms = (sec * 1000) + (nanosec / 1000000L);
  8976. return ms;
  8977. }
  8978. static inline unsigned long msecs_processed(const struct core_timer *const ct)
  8979. {
  8980. return atomic_read(&ct->count) * DAHDI_MSECS_PER_CHUNK;
  8981. }
  8982. static void coretimer_func(unsigned long param)
  8983. {
  8984. unsigned long flags;
  8985. unsigned long ms_since_start;
  8986. struct timespec now;
  8987. const unsigned long MAX_INTERVAL = 100000L;
  8988. const unsigned long ONESEC_INTERVAL = HZ;
  8989. const long MS_LIMIT = 3000;
  8990. long difference;
  8991. ktime_get_ts(&now);
  8992. if (atomic_read(&core_timer.count) ==
  8993. atomic_read(&core_timer.last_count)) {
  8994. /* This is the code path if a board driver is not calling
  8995. * dahdi_receive, and therefore the core of dahdi needs to
  8996. * perform the master span processing itself. */
  8997. if (core_timer.dahdi_receive_used) {
  8998. core_timer.dahdi_receive_used = 0;
  8999. dahdi_dbg(GENERAL, "Master changed to core_timer\n");
  9000. }
  9001. if (!atomic_read(&core_timer.shutdown)) {
  9002. mod_timer(&core_timer.timer, jiffies +
  9003. core_timer.interval);
  9004. }
  9005. ms_since_start = core_diff_ms(&core_timer.start_interval, &now);
  9006. /*
  9007. * If the system time has changed, it is possible for us to be
  9008. * far behind. If we are more than MS_LIMIT milliseconds
  9009. * behind (or ahead in time), just reset our time base and
  9010. * continue so that we do not hang the system here.
  9011. *
  9012. */
  9013. difference = ms_since_start - msecs_processed(&core_timer);
  9014. if (unlikely((difference > MS_LIMIT) || (difference < 0))) {
  9015. if (printk_ratelimit()) {
  9016. module_printk(KERN_INFO,
  9017. "Detected time shift.\n");
  9018. }
  9019. atomic_set(&core_timer.count, 0);
  9020. atomic_set(&core_timer.last_count, 0);
  9021. core_timer.start_interval = now;
  9022. return;
  9023. }
  9024. local_irq_save(flags);
  9025. while (ms_since_start > msecs_processed(&core_timer))
  9026. _process_masterspan();
  9027. local_irq_restore(flags);
  9028. if (ms_since_start > MAX_INTERVAL) {
  9029. atomic_set(&core_timer.count, 0);
  9030. atomic_set(&core_timer.last_count, 0);
  9031. core_timer.start_interval = now;
  9032. } else {
  9033. atomic_set(&core_timer.last_count,
  9034. atomic_read(&core_timer.count));
  9035. }
  9036. } else {
  9037. /* It looks like a board driver is calling dahdi_receive. We
  9038. * will just check again in a second. */
  9039. if (!core_timer.dahdi_receive_used) {
  9040. core_timer.dahdi_receive_used = 1;
  9041. dahdi_dbg(GENERAL, "Master is no longer core_timer\n");
  9042. }
  9043. atomic_set(&core_timer.count, 0);
  9044. atomic_set(&core_timer.last_count, 0);
  9045. core_timer.start_interval = now;
  9046. if (!atomic_read(&core_timer.shutdown))
  9047. mod_timer(&core_timer.timer, jiffies + ONESEC_INTERVAL);
  9048. }
  9049. }
  9050. static void coretimer_init(void)
  9051. {
  9052. init_timer(&core_timer.timer);
  9053. core_timer.timer.function = coretimer_func;
  9054. ktime_get_ts(&core_timer.start_interval);
  9055. atomic_set(&core_timer.count, 0);
  9056. atomic_set(&core_timer.shutdown, 0);
  9057. core_timer.interval = max(msecs_to_jiffies(DAHDI_MSECS_PER_CHUNK), 1UL);
  9058. if (core_timer.interval < (HZ/250))
  9059. core_timer.interval = (HZ/250);
  9060. core_timer.timer.expires = jiffies + core_timer.interval;
  9061. add_timer(&core_timer.timer);
  9062. }
  9063. static void coretimer_cleanup(void)
  9064. {
  9065. atomic_set(&core_timer.shutdown, 1);
  9066. del_timer_sync(&core_timer.timer);
  9067. }
  9068. #endif /* CONFIG_DAHDI_CORE_TIMER */
  9069. /**
  9070. * __receive_from_slaves() - Collect the rx data from all the slave channels.
  9071. *
  9072. */
  9073. static void __receive_from_slaves(struct dahdi_chan *const chan)
  9074. {
  9075. u_char data[DAHDI_CHUNKSIZE];
  9076. int i;
  9077. int pos = 0;
  9078. struct dahdi_chan *slave;
  9079. for (i = 0; i < DAHDI_CHUNKSIZE; ++i) {
  9080. for (slave = chan; (NULL != slave); slave = slave->nextslave) {
  9081. data[pos++] = slave->readchunk[i];
  9082. if (pos == DAHDI_CHUNKSIZE) {
  9083. __dahdi_receive_chunk(chan, data);
  9084. pos = 0;
  9085. }
  9086. }
  9087. }
  9088. }
  9089. static inline bool should_skip_receive(const struct dahdi_chan *const chan)
  9090. {
  9091. return (unlikely(chan->flags & DAHDI_FLAG_NOSTDTXRX) ||
  9092. (chan->master != chan) ||
  9093. is_chan_dacsed(chan));
  9094. }
  9095. int _dahdi_receive(struct dahdi_span *span)
  9096. {
  9097. unsigned int x;
  9098. #ifdef CONFIG_DAHDI_WATCHDOG
  9099. span->watchcounter--;
  9100. #endif
  9101. for (x = 0; x < span->channels; x++) {
  9102. struct dahdi_chan *const chan = span->chans[x];
  9103. spin_lock(&chan->lock);
  9104. if (should_skip_receive(chan)) {
  9105. spin_unlock(&chan->lock);
  9106. continue;
  9107. }
  9108. if (chan->nextslave) {
  9109. __receive_from_slaves(chan);
  9110. } else {
  9111. /* Process a normal channel */
  9112. __dahdi_real_receive(chan);
  9113. }
  9114. if (chan->itimer) {
  9115. chan->itimer -= DAHDI_CHUNKSIZE;
  9116. if (chan->itimer <= 0)
  9117. rbs_itimer_expire(chan);
  9118. }
  9119. if (chan->ringdebtimer)
  9120. chan->ringdebtimer--;
  9121. if (chan->sig & __DAHDI_SIG_FXS) {
  9122. if (chan->rxhooksig == DAHDI_RXSIG_RING)
  9123. chan->ringtrailer = DAHDI_RINGTRAILER;
  9124. else if (chan->ringtrailer) {
  9125. chan->ringtrailer -= DAHDI_CHUNKSIZE;
  9126. /* See if RING trailer is expired */
  9127. if (!chan->ringtrailer && !chan->ringdebtimer)
  9128. __qevent(chan, DAHDI_EVENT_RINGOFFHOOK);
  9129. }
  9130. }
  9131. if (chan->pulsetimer) {
  9132. chan->pulsetimer--;
  9133. if (chan->pulsetimer <= 0) {
  9134. if (chan->pulsecount) {
  9135. if (chan->pulsecount > 12) {
  9136. module_printk(KERN_NOTICE, "Got pulse digit %d on %s???\n",
  9137. chan->pulsecount,
  9138. chan->name);
  9139. } else if (chan->pulsecount > 11) {
  9140. __qevent(chan, DAHDI_EVENT_PULSEDIGIT | '#');
  9141. } else if (chan->pulsecount > 10) {
  9142. __qevent(chan, DAHDI_EVENT_PULSEDIGIT | '*');
  9143. } else if (chan->pulsecount > 9) {
  9144. __qevent(chan, DAHDI_EVENT_PULSEDIGIT | '0');
  9145. } else {
  9146. __qevent(chan, DAHDI_EVENT_PULSEDIGIT | ('0' +
  9147. chan->pulsecount));
  9148. }
  9149. chan->pulsecount = 0;
  9150. }
  9151. }
  9152. }
  9153. #ifdef BUFFER_DEBUG
  9154. chan->statcount -= DAHDI_CHUNKSIZE;
  9155. #endif
  9156. spin_unlock(&chan->lock);
  9157. }
  9158. if (dahdi_is_sync_master(span))
  9159. _process_masterspan();
  9160. return 0;
  9161. }
  9162. EXPORT_SYMBOL(_dahdi_receive);
  9163. MODULE_AUTHOR("Mark Spencer <markster@digium.com>");
  9164. MODULE_DESCRIPTION("DAHDI Telephony Interface");
  9165. MODULE_LICENSE("GPL v2");
  9166. /* DAHDI now provides timing. If anybody wants dahdi_dummy it's probably
  9167. * for that. So make dahdi provide it for now. This alias may be removed
  9168. * in the future, and users are encouraged not to rely on it. */
  9169. MODULE_ALIAS("dahdi_dummy");
  9170. module_param(debug, int, 0644);
  9171. MODULE_PARM_DESC(debug, "Sets debugging verbosity as a bitfield, to see"\
  9172. " general debugging set this to 1. To see RBS debugging set"\
  9173. " this to 32");
  9174. module_param(deftaps, int, 0644);
  9175. module_param(max_pseudo_channels, int, 0644);
  9176. MODULE_PARM_DESC(max_pseudo_channels, "Maximum number of pseudo channels.");
  9177. module_param(hwec_overrides_swec, int, 0644);
  9178. MODULE_PARM_DESC(hwec_overrides_swec, "When true, a hardware echo canceller is used instead of configured SWEC.");
  9179. module_param(auto_assign_spans, int, 0644);
  9180. MODULE_PARM_DESC(auto_assign_spans,
  9181. "If 1 spans will automatically have their children span and "
  9182. "channel numbers assigned by the driver. If 0, user space "
  9183. "will need to assign them via /sys/bus/dahdi_devices.");
  9184. static ssize_t dahdi_no_read(struct file *file, char __user *usrbuf,
  9185. size_t count, loff_t *ppos)
  9186. {
  9187. return -ENOSYS;
  9188. }
  9189. static ssize_t dahdi_no_write(struct file *file, const char __user *usrbuf,
  9190. size_t count, loff_t *ppos)
  9191. {
  9192. return -ENOSYS;
  9193. }
  9194. static const struct file_operations dahdi_fops = {
  9195. .owner = THIS_MODULE,
  9196. .open = dahdi_open,
  9197. .release = dahdi_release,
  9198. #ifdef HAVE_UNLOCKED_IOCTL
  9199. .unlocked_ioctl = dahdi_unlocked_ioctl,
  9200. #ifdef HAVE_COMPAT_IOCTL
  9201. .compat_ioctl = dahdi_ioctl_compat,
  9202. #endif
  9203. #else
  9204. .ioctl = dahdi_ioctl,
  9205. #endif
  9206. .poll = dahdi_poll,
  9207. .read = dahdi_no_read,
  9208. .write = dahdi_no_write,
  9209. };
  9210. static const struct file_operations dahdi_timer_fops = {
  9211. .owner = THIS_MODULE,
  9212. .release = dahdi_timer_release,
  9213. #ifdef HAVE_UNLOCKED_IOCTL
  9214. .unlocked_ioctl = dahdi_timer_unlocked_ioctl,
  9215. #ifdef HAVE_COMPAT_IOCTL
  9216. .compat_ioctl = dahdi_timer_unlocked_ioctl,
  9217. #endif
  9218. #else
  9219. .ioctl = dahdi_timer_ioctl,
  9220. #endif
  9221. .poll = dahdi_timer_poll,
  9222. .read = dahdi_no_read,
  9223. .write = dahdi_no_write,
  9224. };
  9225. /*
  9226. * DAHDI stability should not depend on the calling process behaviour.
  9227. * In case of suprise device removal, we should be able to return
  9228. * sane results (-ENODEV) even after the underlying device was released.
  9229. *
  9230. * This should be OK even if the calling process (hint, hint Asterisk)
  9231. * ignores the system calls return value.
  9232. *
  9233. * We simply use dummy file_operations to implement this.
  9234. */
  9235. /*
  9236. * Common behaviour called from all other nodev_*() file_operations
  9237. */
  9238. static int nodev_common(const char msg[])
  9239. {
  9240. if (printk_ratelimit()) {
  9241. module_printk(KERN_NOTICE,
  9242. "nodev: %s: process %d still calling\n",
  9243. msg, current->tgid);
  9244. }
  9245. msleep(5);
  9246. return -ENODEV;
  9247. }
  9248. static ssize_t nodev_chan_read(struct file *file, char __user *usrbuf,
  9249. size_t count, loff_t *ppos)
  9250. {
  9251. return nodev_common("read");
  9252. }
  9253. static ssize_t nodev_chan_write(struct file *file, const char __user *usrbuf,
  9254. size_t count, loff_t *ppos)
  9255. {
  9256. return nodev_common("write");
  9257. }
  9258. static unsigned int
  9259. nodev_chan_poll(struct file *file, struct poll_table_struct *wait_table)
  9260. {
  9261. return nodev_common("poll");
  9262. }
  9263. static long
  9264. nodev_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  9265. {
  9266. switch (cmd) {
  9267. case DAHDI_GETEVENT: /* Get event on queue */
  9268. /*
  9269. * Hint the bugger that the channel is gone for good
  9270. */
  9271. put_user(DAHDI_EVENT_REMOVED, (int __user *)data);
  9272. break;
  9273. }
  9274. return nodev_common("ioctl");
  9275. }
  9276. #ifndef HAVE_UNLOCKED_IOCTL
  9277. static int nodev_ioctl(struct inode *inode, struct file *file,
  9278. unsigned int cmd, unsigned long data)
  9279. {
  9280. return nodev_unlocked_ioctl(file, cmd, data);
  9281. }
  9282. #endif
  9283. #ifdef HAVE_COMPAT_IOCTL
  9284. static long nodev_ioctl_compat(struct file *file, unsigned int cmd,
  9285. unsigned long data)
  9286. {
  9287. if (cmd == DAHDI_SFCONFIG)
  9288. return -ENOTTY; /* Not supported yet */
  9289. return nodev_unlocked_ioctl(file, cmd, data);
  9290. }
  9291. #endif
  9292. static const struct file_operations nodev_fops = {
  9293. .owner = THIS_MODULE,
  9294. #ifdef HAVE_UNLOCKED_IOCTL
  9295. .unlocked_ioctl = nodev_unlocked_ioctl,
  9296. #ifdef HAVE_COMPAT_IOCTL
  9297. .compat_ioctl = nodev_ioctl_compat,
  9298. #endif
  9299. #else
  9300. .ioctl = nodev_ioctl,
  9301. #endif
  9302. .read = nodev_chan_read,
  9303. .write = nodev_chan_write,
  9304. .poll = nodev_chan_poll,
  9305. };
  9306. static const struct file_operations dahdi_chan_fops = {
  9307. .owner = THIS_MODULE,
  9308. .open = dahdi_open,
  9309. .release = dahdi_release,
  9310. #ifdef HAVE_UNLOCKED_IOCTL
  9311. .unlocked_ioctl = dahdi_unlocked_ioctl,
  9312. #ifdef HAVE_COMPAT_IOCTL
  9313. .compat_ioctl = dahdi_ioctl_compat,
  9314. #endif
  9315. #else
  9316. .ioctl = dahdi_ioctl,
  9317. #endif
  9318. .read = dahdi_chan_read,
  9319. .write = dahdi_chan_write,
  9320. .poll = dahdi_chan_poll,
  9321. };
  9322. #ifdef CONFIG_DAHDI_WATCHDOG
  9323. static struct timer_list watchdogtimer;
  9324. static void watchdog_check(unsigned long ignored)
  9325. {
  9326. unsigned long flags;
  9327. static int wdcheck=0;
  9328. struct dahdi_span *s;
  9329. spin_lock_irqsave(&chan_lock, flags);
  9330. list_for_each_entry(s, &span_list, spans_node) {
  9331. if (s->flags & DAHDI_FLAG_RUNNING) {
  9332. if (s->watchcounter == DAHDI_WATCHDOG_INIT) {
  9333. /* Whoops, dead card */
  9334. if ((s->watchstate == DAHDI_WATCHSTATE_OK) ||
  9335. (s->watchstate == DAHDI_WATCHSTATE_UNKNOWN)) {
  9336. s->watchstate = DAHDI_WATCHSTATE_RECOVERING;
  9337. if (s->ops->watchdog) {
  9338. module_printk(KERN_NOTICE, "Kicking span %s\n", s->name);
  9339. s->ops->watchdog(s, DAHDI_WATCHDOG_NOINTS);
  9340. } else {
  9341. module_printk(KERN_NOTICE, "Span %s is dead with no revival\n", s->name);
  9342. s->watchstate = DAHDI_WATCHSTATE_FAILED;
  9343. }
  9344. }
  9345. } else {
  9346. if ((s->watchstate != DAHDI_WATCHSTATE_OK) &&
  9347. (s->watchstate != DAHDI_WATCHSTATE_UNKNOWN))
  9348. module_printk(KERN_NOTICE, "Span %s is alive!\n", s->name);
  9349. s->watchstate = DAHDI_WATCHSTATE_OK;
  9350. }
  9351. s->watchcounter = DAHDI_WATCHDOG_INIT;
  9352. }
  9353. }
  9354. spin_unlock_irqrestore(&chan_lock, flags);
  9355. if (!wdcheck) {
  9356. module_printk(KERN_NOTICE, "watchdog on duty!\n");
  9357. wdcheck=1;
  9358. }
  9359. mod_timer(&watchdogtimer, jiffies + 2);
  9360. }
  9361. static int __init watchdog_init(void)
  9362. {
  9363. init_timer(&watchdogtimer);
  9364. watchdogtimer.expires = 0;
  9365. watchdogtimer.data =0;
  9366. watchdogtimer.function = watchdog_check;
  9367. /* Run every couple of jiffy or so */
  9368. mod_timer(&watchdogtimer, jiffies + 2);
  9369. return 0;
  9370. }
  9371. static void __exit watchdog_cleanup(void)
  9372. {
  9373. del_timer(&watchdogtimer);
  9374. }
  9375. #endif
  9376. static int __init dahdi_init(void)
  9377. {
  9378. int res = 0;
  9379. module_printk(KERN_INFO, "Version: %s\n", dahdi_version);
  9380. #ifdef CONFIG_PROC_FS
  9381. root_proc_entry = proc_mkdir("dahdi", NULL);
  9382. if (!root_proc_entry) {
  9383. dahdi_err("dahdi init: Failed creating /proc/dahdi\n");
  9384. return -EEXIST;
  9385. }
  9386. #endif
  9387. res = dahdi_sysfs_init(&dahdi_fops);
  9388. if (res)
  9389. goto failed_driver_init;
  9390. dahdi_conv_init();
  9391. fasthdlc_precalc();
  9392. rotate_sums();
  9393. #ifdef CONFIG_DAHDI_WATCHDOG
  9394. watchdog_init();
  9395. #endif
  9396. coretimer_init();
  9397. res = dahdi_register_echocan_factory(&hwec_factory);
  9398. if (res) {
  9399. WARN_ON(1);
  9400. goto failed_register_ec_factory;
  9401. }
  9402. return 0;
  9403. failed_register_ec_factory:
  9404. coretimer_cleanup();
  9405. dahdi_sysfs_exit();
  9406. failed_driver_init:
  9407. if (root_proc_entry) {
  9408. remove_proc_entry("dahdi", NULL);
  9409. root_proc_entry = NULL;
  9410. }
  9411. return res;
  9412. }
  9413. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
  9414. #ifdef CONFIG_PCI
  9415. void dahdi_pci_disable_link_state(struct pci_dev *pdev, int state)
  9416. {
  9417. u16 reg16;
  9418. int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  9419. state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  9420. PCIE_LINK_STATE_CLKPM);
  9421. if (!pos)
  9422. return;
  9423. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
  9424. reg16 &= ~(state);
  9425. pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
  9426. }
  9427. EXPORT_SYMBOL(dahdi_pci_disable_link_state);
  9428. #endif /* CONFIG_PCI */
  9429. #endif /* 2.6.25 */
  9430. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
  9431. static inline void flush_find_master_work(void)
  9432. {
  9433. flush_scheduled_work();
  9434. }
  9435. #else
  9436. static inline void flush_find_master_work(void)
  9437. {
  9438. cancel_work_sync(&find_master_work);
  9439. }
  9440. #endif
  9441. static void __exit dahdi_cleanup(void)
  9442. {
  9443. struct dahdi_zone *z;
  9444. dahdi_unregister_echocan_factory(&hwec_factory);
  9445. coretimer_cleanup();
  9446. dahdi_sysfs_exit();
  9447. #ifdef CONFIG_PROC_FS
  9448. if (root_proc_entry) {
  9449. remove_proc_entry("dahdi", NULL);
  9450. root_proc_entry = NULL;
  9451. }
  9452. #endif
  9453. module_printk(KERN_INFO, "Telephony Interface Unloaded\n");
  9454. spin_lock(&zone_lock);
  9455. while (!list_empty(&tone_zones)) {
  9456. z = list_entry(tone_zones.next, struct dahdi_zone, node);
  9457. list_del(&z->node);
  9458. if (!tone_zone_put(z)) {
  9459. module_printk(KERN_WARNING,
  9460. "Potential memory leak detected in %s\n",
  9461. __func__);
  9462. }
  9463. }
  9464. spin_unlock(&zone_lock);
  9465. #ifdef CONFIG_DAHDI_WATCHDOG
  9466. watchdog_cleanup();
  9467. #endif
  9468. flush_find_master_work();
  9469. }
  9470. module_init(dahdi_init);
  9471. module_exit(dahdi_cleanup);